1 /*****************************************************************************
2 * os.c : Low-level dynamic library handling
3 *****************************************************************************
4 * Copyright (C) 2001-2007 the VideoLAN team
7 * Authors: Sam Hocevar <sam@zoy.org>
8 * Ethan C. Baldridge <BaldridgeE@cadmus.com>
9 * Hans-Peter Jansen <hpj@urpla.net>
10 * Gildas Bazin <gbazin@videolan.org>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_plugin.h> /* MODULE_SUFFIX */
33 #include <vlc_charset.h>
37 #include <stdlib.h> /* free(), strtol() */
38 #include <stdio.h> /* sprintf() */
39 #include <string.h> /* strdup() */
41 #ifdef HAVE_SYS_TYPES_H
42 # include <sys/types.h>
45 #if !defined(HAVE_DYNAMIC_PLUGINS)
46 /* no support for plugins */
47 #elif defined(HAVE_DL_BEOS)
48 # if defined(HAVE_IMAGE_H)
51 #elif defined(__APPLE__)
53 #elif defined(HAVE_DL_WINDOWS)
55 #elif defined(HAVE_DL_DLOPEN)
56 # if defined(HAVE_DLFCN_H) /* Linux, BSD, Hurd */
59 # if defined(HAVE_SYS_DL_H)
62 #elif defined(HAVE_DL_SHL_LOAD)
63 # if defined(HAVE_DL_H)
67 #ifdef HAVE_VALGRIND_VALGRIND_H
68 # include <valgrind/valgrind.h>
71 /*****************************************************************************
73 *****************************************************************************/
74 #ifdef HAVE_DYNAMIC_PLUGINS
75 static void *module_Lookup( module_handle_t, const char * );
77 #if defined(HAVE_DL_WINDOWS)
78 static char * GetWindowsError ( void );
84 * Call a symbol given its name and a module structure. The symbol MUST
85 * refer to a function returning int and taking a module_t* as an argument.
86 * \param p_module the modules
87 * \return 0 if it pass and -1 in case of a failure
89 int module_Call( vlc_object_t *obj, module_t *p_module )
91 static const char psz_name[] = "vlc_entry" MODULE_SUFFIX;
92 int (* pf_symbol) ( module_t * p_module );
94 /* Try to resolve the symbol */
95 pf_symbol = (int (*)(module_t *)) module_Lookup( p_module->handle,
98 if( pf_symbol == NULL )
100 #if defined(HAVE_DL_BEOS)
101 msg_Warn( obj, "cannot find symbol \"%s\" in file `%s'",
102 psz_name, p_module->psz_filename );
103 #elif defined(HAVE_DL_WINDOWS)
104 char *psz_error = GetWindowsError();
105 msg_Warn( obj, "cannot find symbol \"%s\" in file `%s' (%s)",
106 psz_name, p_module->psz_filename, psz_error );
108 #elif defined(HAVE_DL_DLOPEN)
109 msg_Warn( obj, "cannot find symbol \"%s\" in file `%s' (%s)",
110 psz_name, p_module->psz_filename, dlerror() );
111 #elif defined(HAVE_DL_SHL_LOAD)
112 msg_Warn( obj, "cannot find symbol \"%s\" in file `%s' (%m)",
113 psz_name, p_module->psz_filename );
115 # error "Something is wrong in modules.c"
120 /* We can now try to call the symbol */
121 if( pf_symbol( p_module ) != 0 )
123 /* With a well-written module we shouldn't have to print an
124 * additional error message here, but just make sure. */
125 msg_Err( obj, "Failed to call symbol \"%s\" in file `%s'",
126 psz_name, p_module->psz_filename );
130 /* Everything worked fine, we can return */
135 * Load a dynamically linked library using a system dependent method.
137 * \param p_this vlc object
138 * \param psz_file library file
139 * \param p_handle the module handle returned
140 * \return 0 on success as well as the module handle.
142 int module_Load( vlc_object_t *p_this, const char *psz_file,
143 module_handle_t *p_handle )
145 module_handle_t handle;
147 #if defined(HAVE_DL_BEOS)
148 handle = load_add_on( psz_file );
151 msg_Warn( p_this, "cannot load module `%s'", psz_file );
155 #elif defined(HAVE_DL_WINDOWS)
156 wchar_t psz_wfile[MAX_PATH];
157 MultiByteToWideChar( CP_UTF8, 0, psz_file, -1, psz_wfile, MAX_PATH );
160 /* FIXME: this is not thread-safe -- Courmisch */
161 UINT mode = SetErrorMode (SEM_FAILCRITICALERRORS);
162 SetErrorMode (mode|SEM_FAILCRITICALERRORS);
165 handle = LoadLibraryW( psz_wfile );
173 char *psz_err = GetWindowsError();
174 msg_Warn( p_this, "cannot load module `%s' (%s)", psz_file, psz_err );
179 #elif defined(HAVE_DL_DLOPEN)
181 # if defined (RTLD_NOW)
182 const int flags = RTLD_NOW;
183 # elif defined (DL_LAZY)
184 const int flags = DL_LAZY;
188 char *path = ToLocale( psz_file );
190 handle = dlopen( path, flags );
193 msg_Warn( p_this, "cannot load module `%s' (%s)", path, dlerror() );
199 #elif defined(HAVE_DL_SHL_LOAD)
200 handle = shl_load( psz_file, BIND_IMMEDIATE | BIND_NONFATAL, NULL );
203 msg_Warn( p_this, "cannot load module `%s' (%m)", psz_file );
208 # error "Something is wrong in modules.c"
217 * CloseModule: unload a dynamic library
219 * This function unloads a previously opened dynamically linked library
220 * using a system dependent method. No return value is taken in consideration,
221 * since some libraries sometimes refuse to close properly.
222 * \param handle handle of the library
225 void module_Unload( module_handle_t handle )
227 #if defined(HAVE_DL_BEOS)
228 unload_add_on( handle );
230 #elif defined(HAVE_DL_WINDOWS)
231 FreeLibrary( handle );
233 #elif defined(HAVE_DL_DLOPEN)
234 # ifdef HAVE_VALGRIND_VALGRIND_H
235 if( RUNNING_ON_VALGRIND > 0 )
236 return; /* do not dlclose() so that we get proper stack traces */
240 #elif defined(HAVE_DL_SHL_LOAD)
241 shl_unload( handle );
248 * Looks up a symbol from a dynamically loaded library
250 * This function queries a loaded library for a symbol specified in a
251 * string, and returns a pointer to it. We don't check for dlerror() or
252 * similar functions, since we want a non-NULL symbol anyway.
254 * @param handle handle to the module
255 * @param psz_function function name
256 * @return NULL on error, or the address of the symbol
258 static void *module_Lookup( module_handle_t handle, const char *psz_function )
260 #if defined(HAVE_DL_BEOS)
262 if( B_OK == get_image_symbol( handle, psz_function,
263 B_SYMBOL_TYPE_TEXT, &p_symbol ) )
272 #elif defined(HAVE_DL_WINDOWS) && defined(UNDER_CE)
273 wchar_t wide[strlen( psz_function ) + 1];
276 wide[i] = psz_function[i]; /* UTF-16 <- ASCII */
277 while( psz_function[i++] );
279 return (void *)GetProcAddress( handle, wide );
281 #elif defined(HAVE_DL_WINDOWS) && defined(WIN32)
282 return (void *)GetProcAddress( handle, (char *)psz_function );
284 #elif defined(HAVE_DL_DLOPEN)
285 return dlsym( handle, psz_function );
287 #elif defined(HAVE_DL_SHL_LOAD)
289 shl_findsym( &handle, psz_function, TYPE_UNDEFINED, &p_sym );
295 #if defined(HAVE_DL_WINDOWS)
296 static char * GetWindowsError( void )
298 #if defined(UNDER_CE)
299 wchar_t psz_tmp[MAX_PATH];
300 char * psz_buffer = malloc( MAX_PATH );
302 char * psz_tmp = malloc( MAX_PATH );
304 int i = 0, i_error = GetLastError();
306 FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
307 NULL, i_error, MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
308 (LPTSTR)psz_tmp, MAX_PATH, NULL );
310 /* Go to the end of the string */
311 while( psz_tmp[i] && psz_tmp[i] != _T('\r') && psz_tmp[i] != _T('\n') )
318 #if defined(UNDER_CE)
319 swprintf( psz_tmp + i, L" (error %i)", i_error );
320 psz_tmp[ 255 ] = L'\0';
322 snprintf( psz_tmp + i, 256 - i, " (error %i)", i_error );
323 psz_tmp[ 255 ] = '\0';
327 #if defined(UNDER_CE)
328 wcstombs( psz_buffer, psz_tmp, MAX_PATH );
334 #endif /* HAVE_DL_WINDOWS */
335 #endif /* HAVE_DYNAMIC_PLUGINS */