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_DYLD)
47 # if defined(HAVE_MACH_O_DYLD_H)
48 # include <mach-o/dyld.h>
50 #elif defined(HAVE_DL_BEOS)
51 # if defined(HAVE_IMAGE_H)
54 #elif defined(HAVE_DL_WINDOWS)
56 #elif defined(HAVE_DL_DLOPEN)
57 # if defined(HAVE_DLFCN_H) /* Linux, BSD, Hurd */
60 # if defined(HAVE_SYS_DL_H)
63 #elif defined(HAVE_DL_SHL_LOAD)
64 # if defined(HAVE_DL_H)
69 /*****************************************************************************
71 *****************************************************************************/
72 #ifdef HAVE_DYNAMIC_PLUGINS
73 static void * GetSymbol ( module_handle_t, const char * );
75 #if defined(HAVE_DL_WINDOWS)
76 static char * GetWindowsError ( void );
82 * Call a symbol given its name and a module structure. The symbol MUST
83 * refer to a function returning int and taking a module_t* as an argument.
84 * \param p_module the modules
85 * \return 0 if it pass and -1 in case of a failure
87 int module_Call( vlc_object_t *obj, module_t *p_module )
89 static const char psz_name[] = "vlc_entry" MODULE_SUFFIX;
90 int (* pf_symbol) ( module_t * p_module );
92 /* Try to resolve the symbol */
93 pf_symbol = (int (*)(module_t *)) GetSymbol( p_module->handle, psz_name );
95 if( pf_symbol == NULL )
97 #if defined(HAVE_DL_DYLD) || defined(HAVE_DL_BEOS)
98 msg_Warn( obj, "cannot find symbol \"%s\" in file `%s'",
99 psz_name, p_module->psz_filename );
100 #elif defined(HAVE_DL_WINDOWS)
101 char *psz_error = GetWindowsError();
102 msg_Warn( obj, "cannot find symbol \"%s\" in file `%s' (%s)",
103 psz_name, p_module->psz_filename, psz_error );
105 #elif defined(HAVE_DL_DLOPEN)
106 msg_Warn( obj, "cannot find symbol \"%s\" in file `%s' (%s)",
107 psz_name, p_module->psz_filename, dlerror() );
108 #elif defined(HAVE_DL_SHL_LOAD)
109 msg_Warn( obj, "cannot find symbol \"%s\" in file `%s' (%m)",
110 psz_name, p_module->psz_filename );
112 # error "Something is wrong in modules.c"
117 /* We can now try to call the symbol */
118 if( pf_symbol( p_module ) != 0 )
120 /* With a well-written module we shouldn't have to print an
121 * additional error message here, but just make sure. */
122 msg_Err( obj, "Failed to call symbol \"%s\" in file `%s'",
123 psz_name, p_module->psz_filename );
127 /* Everything worked fine, we can return */
131 #if defined (RTLD_NOLOAD)
132 /* Make sure libvlccore is in the global namespace */
133 static void load_libvlccore( void )
135 if( !dlsym( RTLD_DEFAULT, "vlc_module_create" )
136 && !dlopen( "libvlccore.so", RTLD_GLOBAL|RTLD_NOLOAD ) )
137 fprintf( stderr, "ERROR: failed loading libvlccore\n" );
142 * Load a dynamically linked library using a system dependent method.
144 * \param p_this vlc object
145 * \param psz_file library file
146 * \param p_handle the module handle returned
147 * \return 0 on success as well as the module handle.
149 int module_Load( vlc_object_t *p_this, const char *psz_file,
150 module_handle_t *p_handle )
152 module_handle_t handle;
154 #if defined(HAVE_DL_DYLD)
155 NSObjectFileImage image;
156 NSObjectFileImageReturnCode ret;
158 ret = NSCreateObjectFileImageFromFile( psz_file, &image );
160 if( ret != NSObjectFileImageSuccess )
162 msg_Warn( p_this, "cannot create image from `%s'", psz_file );
166 /* Open the dynamic module */
167 handle = NSLinkModule( image, psz_file,
168 NSLINKMODULE_OPTION_RETURN_ON_ERROR );
172 NSLinkEditErrors errors;
173 const char *psz_file, *psz_err;
175 NSLinkEditError( &errors, &i_errnum, &psz_file, &psz_err );
176 msg_Warn( p_this, "cannot link module `%s' (%s)", psz_file, psz_err );
177 NSDestroyObjectFileImage( image );
181 /* Destroy our image, we won't need it */
182 NSDestroyObjectFileImage( image );
184 #elif defined(HAVE_DL_BEOS)
185 handle = load_add_on( psz_file );
188 msg_Warn( p_this, "cannot load module `%s'", psz_file );
192 #elif defined(HAVE_DL_WINDOWS)
193 wchar_t psz_wfile[MAX_PATH];
194 MultiByteToWideChar( CP_ACP, 0, psz_file, -1, psz_wfile, MAX_PATH );
197 /* FIXME: this is not thread-safe -- Courmisch */
198 UINT mode = SetErrorMode (SEM_FAILCRITICALERRORS);
199 SetErrorMode (mode|SEM_FAILCRITICALERRORS);
202 handle = LoadLibraryW( psz_wfile );
210 char *psz_err = GetWindowsError();
211 msg_Warn( p_this, "cannot load module `%s' (%s)", psz_file, psz_err );
216 #elif defined(HAVE_DL_DLOPEN) && defined(RTLD_NOW)
217 # if defined (RTLD_NOLOAD)
218 static pthread_once_t once = PTHREAD_ONCE_INIT;
219 pthread_once( &once, &load_libvlccore );
222 /* static is OK, we are called atomically */
223 handle = dlopen( psz_file, RTLD_NOW );
226 msg_Warn( p_this, "cannot load module `%s' (%s)",
227 psz_file, dlerror() );
231 #elif defined(HAVE_DL_DLOPEN)
232 # if defined(DL_LAZY)
233 handle = dlopen( psz_file, DL_LAZY );
235 handle = dlopen( psz_file, 0 );
239 msg_Warn( p_this, "cannot load module `%s' (%s)",
240 psz_file, dlerror() );
244 #elif defined(HAVE_DL_SHL_LOAD)
245 handle = shl_load( psz_file, BIND_IMMEDIATE | BIND_NONFATAL, NULL );
248 msg_Warn( p_this, "cannot load module `%s' (%m)", psz_file );
253 # error "Something is wrong in modules.c"
262 * CloseModule: unload a dynamic library
264 * This function unloads a previously opened dynamically linked library
265 * using a system dependent method. No return value is taken in consideration,
266 * since some libraries sometimes refuse to close properly.
267 * \param handle handle of the library
270 void module_Unload( module_handle_t handle )
272 #if defined(HAVE_DL_DYLD)
273 NSUnLinkModule( handle, FALSE );
275 #elif defined(HAVE_DL_BEOS)
276 unload_add_on( handle );
278 #elif defined(HAVE_DL_WINDOWS)
279 FreeLibrary( handle );
281 #elif defined(HAVE_DL_DLOPEN)
288 #elif defined(HAVE_DL_SHL_LOAD)
289 shl_unload( handle );
296 * GetSymbol: get a symbol from a dynamic library
298 * This function queries a loaded library for a symbol specified in a
299 * string, and returns a pointer to it. We don't check for dlerror() or
300 * similar functions, since we want a non-NULL symbol anyway.
301 * \param handle handle to the module
302 * \param psz_function function name
305 static void * _module_getsymbol( module_handle_t, const char * );
307 static void * GetSymbol( module_handle_t handle, const char * psz_function )
309 void * p_symbol = _module_getsymbol( handle, psz_function );
311 /* MacOS X dl library expects symbols to begin with "_". So do
312 * some other operating systems. That's really lame, but hey, what
314 if( p_symbol == NULL )
316 char psz_call[strlen( psz_function ) + 2];
319 memcpy( psz_call + 1, psz_function, sizeof (psz_call) - 1 );
320 p_symbol = _module_getsymbol( handle, psz_call );
326 static void * _module_getsymbol( module_handle_t handle,
327 const char * psz_function )
329 #if defined(HAVE_DL_DYLD)
330 NSSymbol sym = NSLookupSymbolInModule( handle, psz_function );
331 return NSAddressOfSymbol( sym );
333 #elif defined(HAVE_DL_BEOS)
335 if( B_OK == get_image_symbol( handle, psz_function,
336 B_SYMBOL_TYPE_TEXT, &p_symbol ) )
345 #elif defined(HAVE_DL_WINDOWS) && defined(UNDER_CE)
346 wchar_t psz_real[256];
347 MultiByteToWideChar( CP_ACP, 0, psz_function, -1, psz_real, 256 );
349 return (void *)GetProcAddress( handle, psz_real );
351 #elif defined(HAVE_DL_WINDOWS) && defined(WIN32)
352 return (void *)GetProcAddress( handle, (char *)psz_function );
354 #elif defined(HAVE_DL_DLOPEN)
355 return dlsym( handle, psz_function );
357 #elif defined(HAVE_DL_SHL_LOAD)
359 shl_findsym( &handle, psz_function, TYPE_UNDEFINED, &p_sym );
365 #if defined(HAVE_DL_WINDOWS)
366 static char * GetWindowsError( void )
368 #if defined(UNDER_CE)
369 wchar_t psz_tmp[MAX_PATH];
370 char * psz_buffer = malloc( MAX_PATH );
372 char * psz_tmp = malloc( MAX_PATH );
374 int i = 0, i_error = GetLastError();
376 FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
377 NULL, i_error, MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
378 (LPTSTR)psz_tmp, MAX_PATH, NULL );
380 /* Go to the end of the string */
381 while( psz_tmp[i] && psz_tmp[i] != _T('\r') && psz_tmp[i] != _T('\n') )
388 #if defined(UNDER_CE)
389 swprintf( psz_tmp + i, L" (error %i)", i_error );
390 psz_tmp[ 255 ] = L'\0';
392 snprintf( psz_tmp + i, 256 - i, " (error %i)", i_error );
393 psz_tmp[ 255 ] = '\0';
397 #if defined(UNDER_CE)
398 wcstombs( psz_buffer, psz_tmp, MAX_PATH );
404 #endif /* HAVE_DL_WINDOWS */
405 #endif /* HAVE_DYNAMIC_PLUGINS */