| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
#ifdef HAVE_CONFIG_H |
|---|
| 28 |
# include "config.h" |
|---|
| 29 |
#endif |
|---|
| 30 |
|
|---|
| 31 |
#include <vlc_common.h> |
|---|
| 32 |
#include <vlc_plugin.h> |
|---|
| 33 |
#include "libvlc.h" |
|---|
| 34 |
#include "modules.h" |
|---|
| 35 |
|
|---|
| 36 |
#include <stdlib.h> |
|---|
| 37 |
#include <stdio.h> |
|---|
| 38 |
#include <string.h> |
|---|
| 39 |
|
|---|
| 40 |
#ifdef HAVE_SYS_TYPES_H |
|---|
| 41 |
# include <sys/types.h> |
|---|
| 42 |
#endif |
|---|
| 43 |
|
|---|
| 44 |
#if !defined(HAVE_DYNAMIC_PLUGINS) |
|---|
| 45 |
|
|---|
| 46 |
#elif defined(HAVE_DL_DYLD) |
|---|
| 47 |
# if defined(HAVE_MACH_O_DYLD_H) |
|---|
| 48 |
# include <mach-o/dyld.h> |
|---|
| 49 |
# endif |
|---|
| 50 |
#elif defined(HAVE_DL_BEOS) |
|---|
| 51 |
# if defined(HAVE_IMAGE_H) |
|---|
| 52 |
# include <image.h> |
|---|
| 53 |
# endif |
|---|
| 54 |
#elif defined(HAVE_DL_WINDOWS) |
|---|
| 55 |
# include <windows.h> |
|---|
| 56 |
#elif defined(HAVE_DL_DLOPEN) |
|---|
| 57 |
# if defined(HAVE_DLFCN_H) |
|---|
| 58 |
# include <dlfcn.h> |
|---|
| 59 |
# endif |
|---|
| 60 |
# if defined(HAVE_SYS_DL_H) |
|---|
| 61 |
# include <sys/dl.h> |
|---|
| 62 |
# endif |
|---|
| 63 |
#elif defined(HAVE_DL_SHL_LOAD) |
|---|
| 64 |
# if defined(HAVE_DL_H) |
|---|
| 65 |
# include <dl.h> |
|---|
| 66 |
# endif |
|---|
| 67 |
#endif |
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 |
#ifdef HAVE_DYNAMIC_PLUGINS |
|---|
| 73 |
static void * GetSymbol ( module_handle_t, const char * ); |
|---|
| 74 |
|
|---|
| 75 |
#if defined(HAVE_DL_WINDOWS) |
|---|
| 76 |
static char * GetWindowsError ( void ); |
|---|
| 77 |
#endif |
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 |
int module_Call( module_t *p_module ) |
|---|
| 88 |
{ |
|---|
| 89 |
static const char psz_name[] = "vlc_entry" MODULE_SUFFIX; |
|---|
| 90 |
int (* pf_symbol) ( module_t * p_module ); |
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 |
pf_symbol = (int (*)(module_t *)) GetSymbol( p_module->handle, psz_name ); |
|---|
| 94 |
|
|---|
| 95 |
if( pf_symbol == NULL ) |
|---|
| 96 |
{ |
|---|
| 97 |
#if defined(HAVE_DL_DYLD) || defined(HAVE_DL_BEOS) |
|---|
| 98 |
msg_Warn( p_module, "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( p_module, "cannot find symbol \"%s\" in file `%s' (%s)", |
|---|
| 103 |
psz_name, p_module->psz_filename, psz_error ); |
|---|
| 104 |
free( psz_error ); |
|---|
| 105 |
#elif defined(HAVE_DL_DLOPEN) |
|---|
| 106 |
msg_Warn( p_module, "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( p_module, "cannot find symbol \"%s\" in file `%s' (%m)", |
|---|
| 110 |
psz_name, p_module->psz_filename ); |
|---|
| 111 |
#else |
|---|
| 112 |
# error "Something is wrong in modules.c" |
|---|
| 113 |
#endif |
|---|
| 114 |
return -1; |
|---|
| 115 |
} |
|---|
| 116 |
|
|---|
| 117 |
|
|---|
| 118 |
if( pf_symbol( p_module ) != 0 ) |
|---|
| 119 |
{ |
|---|
| 120 |
|
|---|
| 121 |
|
|---|
| 122 |
msg_Err( p_module, "Failed to call symbol \"%s\" in file `%s'", |
|---|
| 123 |
psz_name, p_module->psz_filename ); |
|---|
| 124 |
return -1; |
|---|
| 125 |
} |
|---|
| 126 |
|
|---|
| 127 |
|
|---|
| 128 |
return 0; |
|---|
| 129 |
} |
|---|
| 130 |
|
|---|
| 131 |
|
|---|
| 132 |
|
|---|
| 133 |
|
|---|
| 134 |
|
|---|
| 135 |
|
|---|
| 136 |
|
|---|
| 137 |
|
|---|
| 138 |
|
|---|
| 139 |
int module_Load( vlc_object_t *p_this, const char *psz_file, |
|---|
| 140 |
module_handle_t *p_handle ) |
|---|
| 141 |
{ |
|---|
| 142 |
module_handle_t handle; |
|---|
| 143 |
|
|---|
| 144 |
#if defined(HAVE_DL_DYLD) |
|---|
| 145 |
NSObjectFileImage image; |
|---|
| 146 |
NSObjectFileImageReturnCode ret; |
|---|
| 147 |
|
|---|
| 148 |
ret = NSCreateObjectFileImageFromFile( psz_file, &image ); |
|---|
| 149 |
|
|---|
| 150 |
if( ret != NSObjectFileImageSuccess ) |
|---|
| 151 |
{ |
|---|
| 152 |
msg_Warn( p_this, "cannot create image from `%s'", psz_file ); |
|---|
| 153 |
return -1; |
|---|
| 154 |
} |
|---|
| 155 |
|
|---|
| 156 |
|
|---|
| 157 |
handle = NSLinkModule( image, psz_file, |
|---|
| 158 |
NSLINKMODULE_OPTION_RETURN_ON_ERROR ); |
|---|
| 159 |
|
|---|
| 160 |
if( !handle ) |
|---|
| 161 |
{ |
|---|
| 162 |
NSLinkEditErrors errors; |
|---|
| 163 |
const char *psz_file, *psz_err; |
|---|
| 164 |
int i_errnum; |
|---|
| 165 |
NSLinkEditError( &errors, &i_errnum, &psz_file, &psz_err ); |
|---|
| 166 |
msg_Warn( p_this, "cannot link module `%s' (%s)", psz_file, psz_err ); |
|---|
| 167 |
NSDestroyObjectFileImage( image ); |
|---|
| 168 |
return -1; |
|---|
| 169 |
} |
|---|
| 170 |
|
|---|
| 171 |
|
|---|
| 172 |
NSDestroyObjectFileImage( image ); |
|---|
| 173 |
|
|---|
| 174 |
#elif defined(HAVE_DL_BEOS) |
|---|
| 175 |
handle = load_add_on( psz_file ); |
|---|
| 176 |
if( handle < 0 ) |
|---|
| 177 |
{ |
|---|
| 178 |
msg_Warn( p_this, "cannot load module `%s'", psz_file ); |
|---|
| 179 |
return -1; |
|---|
| 180 |
} |
|---|
| 181 |
|
|---|
| 182 |
#elif defined(HAVE_DL_WINDOWS) |
|---|
| 183 |
#ifdef UNDER_CE |
|---|
| 184 |
{ |
|---|
| 185 |
wchar_t psz_wfile[MAX_PATH]; |
|---|
| 186 |
MultiByteToWideChar( CP_ACP, 0, psz_file, -1, psz_wfile, MAX_PATH ); |
|---|
| 187 |
handle = LoadLibrary( psz_wfile ); |
|---|
| 188 |
} |
|---|
| 189 |
#else |
|---|
| 190 |
handle = LoadLibrary( psz_file ); |
|---|
| 191 |
#endif |
|---|
| 192 |
if( handle == NULL ) |
|---|
| 193 |
{ |
|---|
| 194 |
char *psz_err = GetWindowsError(); |
|---|
| 195 |
msg_Warn( p_this, "cannot load module `%s' (%s)", psz_file, psz_err ); |
|---|
| 196 |
free( psz_err ); |
|---|
| 197 |
return -1; |
|---|
| 198 |
} |
|---|
| 199 |
|
|---|
| 200 |
#elif defined(HAVE_DL_DLOPEN) && defined(RTLD_NOW) |
|---|
| 201 |
|
|---|
| 202 |
handle = dlopen( psz_file, RTLD_NOW ); |
|---|
| 203 |
if( handle == NULL ) |
|---|
| 204 |
{ |
|---|
| 205 |
msg_Warn( p_this, "cannot load module `%s' (%s)", |
|---|
| 206 |
psz_file, dlerror() ); |
|---|
| 207 |
return -1; |
|---|
| 208 |
} |
|---|
| 209 |
|
|---|
| 210 |
#elif defined(HAVE_DL_DLOPEN) |
|---|
| 211 |
# if defined(DL_LAZY) |
|---|
| 212 |
handle = dlopen( psz_file, DL_LAZY ); |
|---|
| 213 |
# else |
|---|
| 214 |
handle = dlopen( psz_file, 0 ); |
|---|
| 215 |
# endif |
|---|
| 216 |
if( handle == NULL ) |
|---|
| 217 |
{ |
|---|
| 218 |
msg_Warn( p_this, "cannot load module `%s' (%s)", |
|---|
| 219 |
psz_file, dlerror() ); |
|---|
| 220 |
return -1; |
|---|
| 221 |
} |
|---|
| 222 |
|
|---|
| 223 |
#elif defined(HAVE_DL_SHL_LOAD) |
|---|
| 224 |
handle = shl_load( psz_file, BIND_IMMEDIATE | BIND_NONFATAL, NULL ); |
|---|
| 225 |
if( handle == NULL ) |
|---|
| 226 |
{ |
|---|
| 227 |
msg_Warn( p_this, "cannot load module `%s' (%m)", psz_file ); |
|---|
| 228 |
return -1; |
|---|
| 229 |
} |
|---|
| 230 |
|
|---|
| 231 |
#else |
|---|
| 232 |
# error "Something is wrong in modules.c" |
|---|
| 233 |
|
|---|
| 234 |
#endif |
|---|
| 235 |
|
|---|
| 236 |
*p_handle = handle; |
|---|
| 237 |
return 0; |
|---|
| 238 |
} |
|---|
| 239 |
|
|---|
| 240 |
|
|---|
| 241 |
|
|---|
| 242 |
|
|---|
| 243 |
|
|---|
| 244 |
|
|---|
| 245 |
|
|---|
| 246 |
|
|---|
| 247 |
|
|---|
| 248 |
|
|---|
| 249 |
void module_Unload( module_handle_t handle ) |
|---|
| 250 |
{ |
|---|
| 251 |
#if defined(HAVE_DL_DYLD) |
|---|
| 252 |
NSUnLinkModule( handle, FALSE ); |
|---|
| 253 |
|
|---|
| 254 |
#elif defined(HAVE_DL_BEOS) |
|---|
| 255 |
unload_add_on( handle ); |
|---|
| 256 |
|
|---|
| 257 |
#elif defined(HAVE_DL_WINDOWS) |
|---|
| 258 |
FreeLibrary( handle ); |
|---|
| 259 |
|
|---|
| 260 |
#elif defined(HAVE_DL_DLOPEN) |
|---|
| 261 |
# ifdef NDEBUG |
|---|
| 262 |
dlclose( handle ); |
|---|
| 263 |
# else |
|---|
| 264 |
(void)handle; |
|---|
| 265 |
# endif |
|---|
| 266 |
|
|---|
| 267 |
#elif defined(HAVE_DL_SHL_LOAD) |
|---|
| 268 |
shl_unload( handle ); |
|---|
| 269 |
|
|---|
| 270 |
#endif |
|---|
| 271 |
return; |
|---|
| 272 |
} |
|---|
| 273 |
|
|---|
| 274 |
|
|---|
| 275 |
|
|---|
| 276 |
|
|---|
| 277 |
|
|---|
| 278 |
|
|---|
| 279 |
|
|---|
| 280 |
|
|---|
| 281 |
|
|---|
| 282 |
|
|---|
| 283 |
|
|---|
| 284 |
static void * _module_getsymbol( module_handle_t, const char * ); |
|---|
| 285 |
|
|---|
| 286 |
static void * GetSymbol( module_handle_t handle, const char * psz_function ) |
|---|
| 287 |
{ |
|---|
| 288 |
void * p_symbol = _module_getsymbol( handle, psz_function ); |
|---|
| 289 |
|
|---|
| 290 |
|
|---|
| 291 |
|
|---|
| 292 |
|
|---|
| 293 |
if( p_symbol == NULL ) |
|---|
| 294 |
{ |
|---|
| 295 |
char psz_call[strlen( psz_function ) + 2]; |
|---|
| 296 |
|
|---|
| 297 |
psz_call[ 0 ] = '_'; |
|---|
| 298 |
memcpy( psz_call + 1, psz_function, sizeof (psz_call) - 1 ); |
|---|
| 299 |
p_symbol = _module_getsymbol( handle, psz_call ); |
|---|
| 300 |
} |
|---|
| 301 |
|
|---|
| 302 |
return p_symbol; |
|---|
| 303 |
} |
|---|
| 304 |
|
|---|
| 305 |
static void * _module_getsymbol( module_handle_t handle, |
|---|
| 306 |
const char * psz_function ) |
|---|
| 307 |
{ |
|---|
| 308 |
#if defined(HAVE_DL_DYLD) |
|---|
| 309 |
NSSymbol sym = NSLookupSymbolInModule( handle, psz_function ); |
|---|
| 310 |
return NSAddressOfSymbol( sym ); |
|---|
| 311 |
|
|---|
| 312 |
#elif defined(HAVE_DL_BEOS) |
|---|
| 313 |
void * p_symbol; |
|---|
| 314 |
if( B_OK == get_image_symbol( handle, psz_function, |
|---|
| 315 |
B_SYMBOL_TYPE_TEXT, &p_symbol ) ) |
|---|
| 316 |
{ |
|---|
| 317 |
return p_symbol; |
|---|
| 318 |
} |
|---|
| 319 |
else |
|---|
| 320 |
{ |
|---|
| 321 |
return NULL; |
|---|
| 322 |
} |
|---|
| 323 |
|
|---|
| 324 |
#elif defined(HAVE_DL_WINDOWS) && defined(UNDER_CE) |
|---|
| 325 |
wchar_t psz_real[256]; |
|---|
| 326 |
MultiByteToWideChar( CP_ACP, 0, psz_function, -1, psz_real, 256 ); |
|---|
| 327 |
|
|---|
| 328 |
return (void *)GetProcAddress( handle, psz_real ); |
|---|
| 329 |
|
|---|
| 330 |
#elif defined(HAVE_DL_WINDOWS) && defined(WIN32) |
|---|
| 331 |
return (void *)GetProcAddress( handle, (char *)psz_function ); |
|---|
| 332 |
|
|---|
| 333 |
#elif defined(HAVE_DL_DLOPEN) |
|---|
| 334 |
return dlsym( handle, psz_function ); |
|---|
| 335 |
|
|---|
| 336 |
#elif defined(HAVE_DL_SHL_LOAD) |
|---|
| 337 |
void *p_sym; |
|---|
| 338 |
shl_findsym( &handle, psz_function, TYPE_UNDEFINED, &p_sym ); |
|---|
| 339 |
return p_sym; |
|---|
| 340 |
|
|---|
| 341 |
#endif |
|---|
| 342 |
} |
|---|
| 343 |
|
|---|
| 344 |
#if defined(HAVE_DL_WINDOWS) |
|---|
| 345 |
static char * GetWindowsError( void ) |
|---|
| 346 |
{ |
|---|
| 347 |
#if defined(UNDER_CE) |
|---|
| 348 |
wchar_t psz_tmp[MAX_PATH]; |
|---|
| 349 |
char * psz_buffer = malloc( MAX_PATH ); |
|---|
| 350 |
#else |
|---|
| 351 |
char * psz_tmp = malloc( MAX_PATH ); |
|---|
| 352 |
#endif |
|---|
| 353 |
int i = 0, i_error = GetLastError(); |
|---|
| 354 |
|
|---|
| 355 |
FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, |
|---|
| 356 |
NULL, i_error, MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), |
|---|
| 357 |
(LPTSTR)psz_tmp, MAX_PATH, NULL ); |
|---|
| 358 |
|
|---|
| 359 |
|
|---|
| 360 |
while( psz_tmp[i] && psz_tmp[i] != _T('\r') && psz_tmp[i] != _T('\n') ) |
|---|
| 361 |
{ |
|---|
| 362 |
i++; |
|---|
| 363 |
} |
|---|
| 364 |
|
|---|
| 365 |
if( psz_tmp[i] ) |
|---|
| 366 |
{ |
|---|
| 367 |
#if defined(UNDER_CE) |
|---|
| 368 |
swprintf( psz_tmp + i, L" (error %i)", i_error ); |
|---|
| 369 |
psz_tmp[ 255 ] = L'\0'; |
|---|
| 370 |
#else |
|---|
| 371 |
snprintf( psz_tmp + i, 256 - i, " (error %i)", i_error ); |
|---|
| 372 |
psz_tmp[ 255 ] = '\0'; |
|---|
| 373 |
#endif |
|---|
| 374 |
} |
|---|
| 375 |
|
|---|
| 376 |
#if defined(UNDER_CE) |
|---|
| 377 |
wcstombs( psz_buffer, psz_tmp, MAX_PATH ); |
|---|
| 378 |
return psz_buffer; |
|---|
| 379 |
#else |
|---|
| 380 |
return psz_tmp; |
|---|
| 381 |
#endif |
|---|
| 382 |
} |
|---|
| 383 |
#endif |
|---|
| 384 |
#endif |
|---|