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 */
36 #include <stdlib.h> /* free(), strtol() */
37 #include <stdio.h> /* sprintf() */
38 #include <string.h> /* strdup() */
40 #ifdef HAVE_SYS_TYPES_H
41 # include <sys/types.h>
44 #if !defined(HAVE_DYNAMIC_PLUGINS)
45 /* no support for plugins */
46 #elif defined(HAVE_DL_BEOS)
47 # if defined(HAVE_IMAGE_H)
50 #elif defined(__APPLE__)
52 #elif defined(HAVE_DL_WINDOWS)
54 #elif defined(HAVE_DL_DLOPEN)
55 # if defined(HAVE_DLFCN_H) /* Linux, BSD, Hurd */
58 # if defined(HAVE_SYS_DL_H)
61 #elif defined(HAVE_DL_SHL_LOAD)
62 # if defined(HAVE_DL_H)
66 #ifdef HAVE_VALGRIND_VALGRIND_H
67 # include <valgrind/valgrind.h>
70 /*****************************************************************************
72 *****************************************************************************/
73 #ifdef HAVE_DYNAMIC_PLUGINS
74 static void *module_Lookup( module_handle_t, const char * );
76 #if defined(HAVE_DL_WINDOWS)
77 static char * GetWindowsError ( void );
83 * Call a symbol given its name and a module structure. The symbol MUST
84 * refer to a function returning int and taking a module_t* as an argument.
85 * \param p_module the modules
86 * \return 0 if it pass and -1 in case of a failure
88 int module_Call( vlc_object_t *obj, module_t *p_module )
90 static const char psz_name[] = "vlc_entry" MODULE_SUFFIX;
91 int (* pf_symbol) ( module_t * p_module );
93 /* Try to resolve the symbol */
94 pf_symbol = (int (*)(module_t *)) module_Lookup( p_module->handle,
97 if( pf_symbol == NULL )
99 #if defined(HAVE_DL_BEOS)
100 msg_Warn( obj, "cannot find symbol \"%s\" in file `%s'",
101 psz_name, p_module->psz_filename );
102 #elif defined(HAVE_DL_WINDOWS)
103 char *psz_error = GetWindowsError();
104 msg_Warn( obj, "cannot find symbol \"%s\" in file `%s' (%s)",
105 psz_name, p_module->psz_filename, psz_error );
107 #elif defined(HAVE_DL_DLOPEN)
108 msg_Warn( obj, "cannot find symbol \"%s\" in file `%s' (%s)",
109 psz_name, p_module->psz_filename, dlerror() );
110 #elif defined(HAVE_DL_SHL_LOAD)
111 msg_Warn( obj, "cannot find symbol \"%s\" in file `%s' (%m)",
112 psz_name, p_module->psz_filename );
114 # error "Something is wrong in modules.c"
119 /* We can now try to call the symbol */
120 if( pf_symbol( p_module ) != 0 )
122 /* With a well-written module we shouldn't have to print an
123 * additional error message here, but just make sure. */
124 msg_Err( obj, "Failed to call symbol \"%s\" in file `%s'",
125 psz_name, p_module->psz_filename );
129 /* Everything worked fine, we can return */
134 * Load a dynamically linked library using a system dependent method.
136 * \param p_this vlc object
137 * \param psz_file library file
138 * \param p_handle the module handle returned
139 * \return 0 on success as well as the module handle.
141 int module_Load( vlc_object_t *p_this, const char *psz_file,
142 module_handle_t *p_handle )
144 module_handle_t handle;
146 #if defined(HAVE_DL_BEOS)
147 handle = load_add_on( psz_file );
150 msg_Warn( p_this, "cannot load module `%s'", psz_file );
154 #elif defined(HAVE_DL_WINDOWS)
155 wchar_t psz_wfile[MAX_PATH];
156 MultiByteToWideChar( CP_ACP, 0, psz_file, -1, psz_wfile, MAX_PATH );
159 /* FIXME: this is not thread-safe -- Courmisch */
160 UINT mode = SetErrorMode (SEM_FAILCRITICALERRORS);
161 SetErrorMode (mode|SEM_FAILCRITICALERRORS);
164 handle = LoadLibraryW( psz_wfile );
172 char *psz_err = GetWindowsError();
173 msg_Warn( p_this, "cannot load module `%s' (%s)", psz_file, psz_err );
178 #elif defined(HAVE_DL_DLOPEN)
180 # if defined (RTLD_NOW)
181 const int flags = RTLD_NOW;
182 # elif defined (DL_LAZY)
183 const int flags = DL_LAZY;
188 handle = dlopen( psz_file, flags );
191 msg_Warn( p_this, "cannot load module `%s' (%s)",
192 psz_file, dlerror() );
196 #elif defined(HAVE_DL_SHL_LOAD)
197 handle = shl_load( psz_file, BIND_IMMEDIATE | BIND_NONFATAL, NULL );
200 msg_Warn( p_this, "cannot load module `%s' (%m)", psz_file );
205 # error "Something is wrong in modules.c"
214 * CloseModule: unload a dynamic library
216 * This function unloads a previously opened dynamically linked library
217 * using a system dependent method. No return value is taken in consideration,
218 * since some libraries sometimes refuse to close properly.
219 * \param handle handle of the library
222 void module_Unload( module_handle_t handle )
224 #if defined(HAVE_DL_BEOS)
225 unload_add_on( handle );
227 #elif defined(HAVE_DL_WINDOWS)
228 FreeLibrary( handle );
230 #elif defined(HAVE_DL_DLOPEN)
231 # ifdef HAVE_VALGRIND_VALGRIND_H
232 if( RUNNING_ON_VALGRIND > 0 )
233 return; /* do not dlclose() so that we get proper stack traces */
237 #elif defined(HAVE_DL_SHL_LOAD)
238 shl_unload( handle );
245 * Looks up a symbol from a dynamically loaded library
247 * This function queries a loaded library for a symbol specified in a
248 * string, and returns a pointer to it. We don't check for dlerror() or
249 * similar functions, since we want a non-NULL symbol anyway.
251 * @param handle handle to the module
252 * @param psz_function function name
253 * @return NULL on error, or the address of the symbol
255 static void *module_Lookup( module_handle_t handle, const char *psz_function )
257 #if defined(HAVE_DL_BEOS)
259 if( B_OK == get_image_symbol( handle, psz_function,
260 B_SYMBOL_TYPE_TEXT, &p_symbol ) )
269 #elif defined(HAVE_DL_WINDOWS) && defined(UNDER_CE)
270 wchar_t wide[strlen( psz_function ) + 1];
273 wide[i] = psz_function[i]; /* UTF-16 <- ASCII */
274 while( psz_function[i++] );
276 return (void *)GetProcAddress( handle, wide );
278 #elif defined(HAVE_DL_WINDOWS) && defined(WIN32)
279 return (void *)GetProcAddress( handle, (char *)psz_function );
281 #elif defined(HAVE_DL_DLOPEN)
282 return dlsym( handle, psz_function );
284 #elif defined(HAVE_DL_SHL_LOAD)
286 shl_findsym( &handle, psz_function, TYPE_UNDEFINED, &p_sym );
292 #if defined(HAVE_DL_WINDOWS)
293 static char * GetWindowsError( void )
295 #if defined(UNDER_CE)
296 wchar_t psz_tmp[MAX_PATH];
297 char * psz_buffer = malloc( MAX_PATH );
299 char * psz_tmp = malloc( MAX_PATH );
301 int i = 0, i_error = GetLastError();
303 FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
304 NULL, i_error, MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
305 (LPTSTR)psz_tmp, MAX_PATH, NULL );
307 /* Go to the end of the string */
308 while( psz_tmp[i] && psz_tmp[i] != _T('\r') && psz_tmp[i] != _T('\n') )
315 #if defined(UNDER_CE)
316 swprintf( psz_tmp + i, L" (error %i)", i_error );
317 psz_tmp[ 255 ] = L'\0';
319 snprintf( psz_tmp + i, 256 - i, " (error %i)", i_error );
320 psz_tmp[ 255 ] = '\0';
324 #if defined(UNDER_CE)
325 wcstombs( psz_buffer, psz_tmp, MAX_PATH );
331 #endif /* HAVE_DL_WINDOWS */
332 #endif /* HAVE_DYNAMIC_PLUGINS */