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)
68 #ifdef HAVE_VALGRIND_VALGRIND_H
69 # include <valgrind/valgrind.h>
72 /*****************************************************************************
74 *****************************************************************************/
75 #ifdef HAVE_DYNAMIC_PLUGINS
76 static void *module_Lookup( module_handle_t, const char * );
78 #if defined(HAVE_DL_WINDOWS)
79 static char * GetWindowsError ( void );
85 * Call a symbol given its name and a module structure. The symbol MUST
86 * refer to a function returning int and taking a module_t* as an argument.
87 * \param p_module the modules
88 * \return 0 if it pass and -1 in case of a failure
90 int module_Call( vlc_object_t *obj, module_t *p_module )
92 static const char psz_name[] = "vlc_entry" MODULE_SUFFIX;
93 int (* pf_symbol) ( module_t * p_module );
95 /* Try to resolve the symbol */
96 pf_symbol = (int (*)(module_t *)) module_Lookup( p_module->handle,
99 if( pf_symbol == NULL )
101 #if defined(HAVE_DL_DYLD) || defined(HAVE_DL_BEOS)
102 msg_Warn( obj, "cannot find symbol \"%s\" in file `%s'",
103 psz_name, p_module->psz_filename );
104 #elif defined(HAVE_DL_WINDOWS)
105 char *psz_error = GetWindowsError();
106 msg_Warn( obj, "cannot find symbol \"%s\" in file `%s' (%s)",
107 psz_name, p_module->psz_filename, psz_error );
109 #elif defined(HAVE_DL_DLOPEN)
110 msg_Warn( obj, "cannot find symbol \"%s\" in file `%s' (%s)",
111 psz_name, p_module->psz_filename, dlerror() );
112 #elif defined(HAVE_DL_SHL_LOAD)
113 msg_Warn( obj, "cannot find symbol \"%s\" in file `%s' (%m)",
114 psz_name, p_module->psz_filename );
116 # error "Something is wrong in modules.c"
121 /* We can now try to call the symbol */
122 if( pf_symbol( p_module ) != 0 )
124 /* With a well-written module we shouldn't have to print an
125 * additional error message here, but just make sure. */
126 msg_Err( obj, "Failed to call symbol \"%s\" in file `%s'",
127 psz_name, p_module->psz_filename );
131 /* Everything worked fine, we can return */
136 * Load a dynamically linked library using a system dependent method.
138 * \param p_this vlc object
139 * \param psz_file library file
140 * \param p_handle the module handle returned
141 * \return 0 on success as well as the module handle.
143 int module_Load( vlc_object_t *p_this, const char *psz_file,
144 module_handle_t *p_handle )
146 module_handle_t handle;
148 #if defined(HAVE_DL_DYLD)
149 NSObjectFileImage image;
150 NSObjectFileImageReturnCode ret;
152 ret = NSCreateObjectFileImageFromFile( psz_file, &image );
154 if( ret != NSObjectFileImageSuccess )
156 msg_Warn( p_this, "cannot create image from `%s'", psz_file );
160 /* Open the dynamic module */
161 handle = NSLinkModule( image, psz_file,
162 NSLINKMODULE_OPTION_RETURN_ON_ERROR );
166 NSLinkEditErrors errors;
167 const char *psz_file, *psz_err;
169 NSLinkEditError( &errors, &i_errnum, &psz_file, &psz_err );
170 msg_Warn( p_this, "cannot link module `%s' (%s)", psz_file, psz_err );
171 NSDestroyObjectFileImage( image );
175 /* Destroy our image, we won't need it */
176 NSDestroyObjectFileImage( image );
178 #elif defined(HAVE_DL_BEOS)
179 handle = load_add_on( psz_file );
182 msg_Warn( p_this, "cannot load module `%s'", psz_file );
186 #elif defined(HAVE_DL_WINDOWS)
187 wchar_t psz_wfile[MAX_PATH];
188 MultiByteToWideChar( CP_ACP, 0, psz_file, -1, psz_wfile, MAX_PATH );
191 /* FIXME: this is not thread-safe -- Courmisch */
192 UINT mode = SetErrorMode (SEM_FAILCRITICALERRORS);
193 SetErrorMode (mode|SEM_FAILCRITICALERRORS);
196 handle = LoadLibraryW( psz_wfile );
204 char *psz_err = GetWindowsError();
205 msg_Warn( p_this, "cannot load module `%s' (%s)", psz_file, psz_err );
210 #elif defined(HAVE_DL_DLOPEN)
212 # if defined (RTLD_NOW)
213 const int flags = RTLD_NOW;
214 # elif defined (DL_LAZY)
215 const int flags = DL_LAZY;
220 handle = dlopen( psz_file, flags );
223 msg_Warn( p_this, "cannot load module `%s' (%s)",
224 psz_file, dlerror() );
228 #elif defined(HAVE_DL_SHL_LOAD)
229 handle = shl_load( psz_file, BIND_IMMEDIATE | BIND_NONFATAL, NULL );
232 msg_Warn( p_this, "cannot load module `%s' (%m)", psz_file );
237 # error "Something is wrong in modules.c"
246 * CloseModule: unload a dynamic library
248 * This function unloads a previously opened dynamically linked library
249 * using a system dependent method. No return value is taken in consideration,
250 * since some libraries sometimes refuse to close properly.
251 * \param handle handle of the library
254 void module_Unload( module_handle_t handle )
256 #if defined(HAVE_DL_DYLD)
257 NSUnLinkModule( handle, FALSE );
259 #elif defined(HAVE_DL_BEOS)
260 unload_add_on( handle );
262 #elif defined(HAVE_DL_WINDOWS)
263 FreeLibrary( handle );
265 #elif defined(HAVE_DL_DLOPEN)
266 # ifdef HAVE_VALGRIND_VALGRIND_H
267 if( RUNNING_ON_VALGRIND > 0 )
268 return; /* do not dlclose() so that we get proper stack traces */
272 #elif defined(HAVE_DL_SHL_LOAD)
273 shl_unload( handle );
280 * Looks up a symbol from a dynamically loaded library
282 * This function queries a loaded library for a symbol specified in a
283 * string, and returns a pointer to it. We don't check for dlerror() or
284 * similar functions, since we want a non-NULL symbol anyway.
286 * @param handle handle to the module
287 * @param psz_function function name
288 * @return NULL on error, or the address of the symbol
290 static void *module_Lookup( module_handle_t handle, const char *psz_function )
292 #if defined(HAVE_DL_DYLD)
293 char psz_call[strlen( psz_function ) + 2];
295 memcpy( psz_call + 1, psz_function, sizeof( psz_call ) - 1 );
297 NSSymbol sym = NSLookupSymbolInModule( handle, psz_call );
298 return NSAddressOfSymbol( sym );
300 #elif defined(HAVE_DL_BEOS)
302 if( B_OK == get_image_symbol( handle, psz_function,
303 B_SYMBOL_TYPE_TEXT, &p_symbol ) )
312 #elif defined(HAVE_DL_WINDOWS) && defined(UNDER_CE)
313 wchar_t wide[strlen( psz_function ) + 1];
316 wide[i] = psz_function[i]; /* UTF-16 <- ASCII */
317 while( psz_function[i++] );
319 return (void *)GetProcAddress( handle, wide );
321 #elif defined(HAVE_DL_WINDOWS) && defined(WIN32)
322 return (void *)GetProcAddress( handle, (char *)psz_function );
324 #elif defined(HAVE_DL_DLOPEN)
325 return dlsym( handle, psz_function );
327 #elif defined(HAVE_DL_SHL_LOAD)
329 shl_findsym( &handle, psz_function, TYPE_UNDEFINED, &p_sym );
335 #if defined(HAVE_DL_WINDOWS)
336 static char * GetWindowsError( void )
338 #if defined(UNDER_CE)
339 wchar_t psz_tmp[MAX_PATH];
340 char * psz_buffer = malloc( MAX_PATH );
342 char * psz_tmp = malloc( MAX_PATH );
344 int i = 0, i_error = GetLastError();
346 FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
347 NULL, i_error, MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
348 (LPTSTR)psz_tmp, MAX_PATH, NULL );
350 /* Go to the end of the string */
351 while( psz_tmp[i] && psz_tmp[i] != _T('\r') && psz_tmp[i] != _T('\n') )
358 #if defined(UNDER_CE)
359 swprintf( psz_tmp + i, L" (error %i)", i_error );
360 psz_tmp[ 255 ] = L'\0';
362 snprintf( psz_tmp + i, 256 - i, " (error %i)", i_error );
363 psz_tmp[ 255 ] = '\0';
367 #if defined(UNDER_CE)
368 wcstombs( psz_buffer, psz_tmp, MAX_PATH );
374 #endif /* HAVE_DL_WINDOWS */
375 #endif /* HAVE_DYNAMIC_PLUGINS */