]> git.saurik.com Git - wxWidgets.git/blame - src/common/dynlib.cpp
removed previously unused and now uncompilable code from wxMGL
[wxWidgets.git] / src / common / dynlib.cpp
CommitLineData
7a4b9130
GL
1/////////////////////////////////////////////////////////////////////////////
2// Name: dynlib.cpp
3// Purpose: Dynamic library management
4// Author: Guilhem Lavaux
5// Modified by:
6// Created: 20/07/98
7// RCS-ID: $Id$
8// Copyright: (c) Guilhem Lavaux
9// Licence: wxWindows license
10/////////////////////////////////////////////////////////////////////////////
11
7b0bfbb2
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
7a4b9130 20#ifdef __GNUG__
a585ca5c 21# pragma implementation "dynlib.h"
7a4b9130
GL
22#endif
23
0c32066b
JS
24#include "wx/wxprec.h"
25
2df7be7f
RR
26#ifdef __BORLANDC__
27 #pragma hdrstop
28#endif
f6bcfd97 29
481fcc78 30#if wxUSE_DYNLIB_CLASS && !wxUSE_DYNAMIC_LOADER
8a0d4cf6 31
f6bcfd97
BP
32#if defined(__WINDOWS__)
33 #include "wx/msw/private.h"
34#endif
35
7b0bfbb2
VZ
36#include "wx/dynlib.h"
37#include "wx/filefn.h"
38#include "wx/intl.h"
39#include "wx/log.h"
40
41// ----------------------------------------------------------------------------
42// conditional compilation
43// ----------------------------------------------------------------------------
44
1a787c5d
SN
45#if defined(__WXPM__) || defined(__EMX__)
46# define INCL_DOS
47# include <os2.h>
48# define wxDllOpen(error, lib, handle) DosLoadModule(error, sizeof(error), lib, &handle)
49# define wxDllGetSymbol(handle, modaddr) DosQueryProcAddr(handle, 1L, NULL, (PFN*)modaddr)
50# define wxDllClose(handle) DosFreeModule(handle)
51#elif defined(HAVE_DLOPEN)
237c5c02
VZ
52 // note about dlopen() flags: we use RTLD_NOW to have more Windows-like
53 // behaviour (Win won't let you load a library with missing symbols) and
54 // RTLD_GLOBAL because it is needed sometimes and probably doesn't hurt
343a06a5
JJ
55 // otherwise. On True64-Unix RTLD_GLOBAL is not allowed and on VMS the
56 // second argument on dlopen is ignored.
99d96a2b
JJ
57#ifdef __VMS
58# define wxDllOpen(lib) dlopen(lib.fn_str(), 0 )
343a06a5
JJ
59#elif defined( __osf__ )
60# define wxDllOpen(lib) dlopen(lib.fn_str(), RTLD_LAZY )
99d96a2b
JJ
61#else
62# define wxDllOpen(lib) dlopen(lib.fn_str(), RTLD_LAZY | RTLD_GLOBAL)
63#endif
64#define wxDllGetSymbol(handle, name) dlsym(handle, name)
a585ca5c 65# define wxDllClose dlclose
8a0d4cf6 66#elif defined(HAVE_SHL_LOAD)
a585ca5c 67# define wxDllOpen(lib) shl_load(lib.fn_str(), BIND_DEFERRED, 0)
237c5c02 68# define wxDllClose shl_unload
846e1424 69
0b9ab0bd
RL
70static inline void *wxDllGetSymbol(shl_t handle, const wxString& name)
71{
72 void *sym;
73 if ( shl_findsym(&handle, name.mb_str(), TYPE_UNDEFINED, &sym) == 0 )
74 return sym;
75 else
76 return 0;
77}
78
f11bdd03
GD
79#elif defined(__DARWIN__)
80/* Porting notes:
81 * The dlopen port is a port from dl_next.xs by Anno Siegel.
82 * dl_next.xs is itself a port from dl_dlopen.xs by Paul Marquess.
83 * The method used here is just to supply the sun style dlopen etc.
84 * functions in terms of Darwin NS*.
85 */
4c27c3c6
GD
86void *dlopen(const char *path, int mode /* mode is ignored */);
87void *dlsym(void *handle, const char *symbol);
03e11df5 88int dlclose(void *handle);
4c27c3c6 89const char *dlerror(void);
03e11df5
GD
90
91# define wxDllOpen(lib) dlopen(lib.fn_str(), 0)
92# define wxDllGetSymbol(handle, name) dlsym(handle, name)
93# define wxDllClose dlclose
7b0bfbb2 94#elif defined(__WINDOWS__)
90022ddc 95 // using LoadLibraryEx under Win32 to avoid name clash with LoadLibrary
a585ca5c 96# ifdef __WIN32__
f6bcfd97
BP
97#ifdef _UNICODE
98# define wxDllOpen(lib) ::LoadLibraryExW(lib, 0, 0)
99#else
100# define wxDllOpen(lib) ::LoadLibraryExA(lib, 0, 0)
101#endif
a585ca5c
KB
102# else // Win16
103# define wxDllOpen(lib) ::LoadLibrary(lib)
104# endif // Win32/16
105# define wxDllGetSymbol(handle, name) ::GetProcAddress(handle, name)
106# define wxDllClose ::FreeLibrary
763ca781
SC
107#elif defined(__WXMAC__)
108# define wxDllClose(handle) CloseConnection(&handle)
7b0bfbb2 109#else
a585ca5c 110# error "Don't know how to load shared libraries on this platform."
7b0bfbb2 111#endif // OS
7a4b9130
GL
112
113// ---------------------------------------------------------------------------
7b0bfbb2 114// Global variables
7a4b9130
GL
115// ---------------------------------------------------------------------------
116
7b0bfbb2 117wxLibraries wxTheLibraries;
7a4b9130 118
f6bcfd97
BP
119// ============================================================================
120// implementation
121// ============================================================================
123a7fdd 122
7b0bfbb2
VZ
123// construct the full name from the base shared object name: adds a .dll
124// suffix under Windows or .so under Unix
125static wxString ConstructLibraryName(const wxString& basename)
126{
f6bcfd97
BP
127 wxString fullname;
128 fullname << basename << wxDllLoader::GetDllExt();
27529614 129
7b0bfbb2
VZ
130 return fullname;
131}
7a4b9130 132
7a4b9130 133// ---------------------------------------------------------------------------
7b0bfbb2 134// wxLibrary (one instance per dynamic library)
7a4b9130
GL
135// ---------------------------------------------------------------------------
136
7b0bfbb2 137wxLibrary::wxLibrary(wxDllType handle)
7a4b9130 138{
7b0bfbb2
VZ
139 typedef wxClassInfo *(*t_get_first)(void);
140 t_get_first get_first;
7a4b9130 141
7b0bfbb2 142 m_handle = handle;
7a4b9130 143
7b0bfbb2
VZ
144 // Some system may use a local heap for library.
145 get_first = (t_get_first)GetSymbol("wxGetClassFirst");
146 // It is a wxWindows DLL.
147 if (get_first)
148 PrepareClasses(get_first());
7a4b9130
GL
149}
150
151wxLibrary::~wxLibrary()
152{
7b0bfbb2
VZ
153 if ( m_handle )
154 {
155 wxDllClose(m_handle);
156 }
7a4b9130
GL
157}
158
159wxObject *wxLibrary::CreateObject(const wxString& name)
160{
7b0bfbb2 161 wxClassInfo *info = (wxClassInfo *)classTable.Get(name);
f4a8c29f 162
7b0bfbb2
VZ
163 if (!info)
164 return NULL;
f4a8c29f 165
7b0bfbb2 166 return info->CreateObject();
f4a8c29f
GL
167}
168
856d2e52 169void wxLibrary::PrepareClasses(wxClassInfo *first)
f4a8c29f 170{
7b0bfbb2
VZ
171 // Index all class infos by their class name
172 wxClassInfo *info = first;
173 while (info)
174 {
175 if (info->m_className)
176 classTable.Put(info->m_className, (wxObject *)info);
0b9ab0bd 177 info = info->m_next;
7b0bfbb2
VZ
178 }
179
180 // Set base pointers for each wxClassInfo
181 info = first;
182 while (info)
183 {
184 if (info->GetBaseClassName1())
185 info->m_baseInfo1 = (wxClassInfo *)classTable.Get(info->GetBaseClassName1());
186 if (info->GetBaseClassName2())
187 info->m_baseInfo2 = (wxClassInfo *)classTable.Get(info->GetBaseClassName2());
188 info = info->m_next;
189 }
7a4b9130
GL
190}
191
192void *wxLibrary::GetSymbol(const wxString& symbname)
193{
a585ca5c
KB
194 return wxDllLoader::GetSymbol(m_handle, symbname);
195}
7b0bfbb2 196
a585ca5c
KB
197// ---------------------------------------------------------------------------
198// wxDllLoader
199// ---------------------------------------------------------------------------
7b0bfbb2 200
f6bcfd97
BP
201
202#if defined(__WINDOWS__) || defined(__WXPM__) || defined(__EMX__)
0b9ab0bd 203const wxString wxDllLoader::ms_dllext( _T(".dll") );
f6bcfd97 204#elif defined(__UNIX__)
0b9ab0bd
RL
205#if defined(__HPUX__)
206const wxString wxDllLoader::ms_dllext( _T(".sl") );
207#else
208const wxString wxDllLoader::ms_dllext( _T(".so") );
209#endif
f6bcfd97 210#endif
f6bcfd97 211
0868079c 212/* static */
0b9ab0bd 213wxDllType wxDllLoader::GetProgramHandle()
0868079c 214{
1a787c5d 215#if defined( HAVE_DLOPEN ) && !defined(__EMX__)
7742efff 216 // optain handle for main program
d236a959 217 return dlopen(NULL, RTLD_NOW/*RTLD_LAZY*/);
7742efff
KB
218#elif defined (HAVE_SHL_LOAD)
219 // shl_findsymbol with NULL handle looks up in main program
d236a959 220 return 0;
0868079c 221#else
58c837a4 222 wxFAIL_MSG( wxT("This method is not implemented under Windows or OS/2"));
7742efff 223 return 0;
7cc98b3e 224#endif
0868079c
KB
225}
226
a585ca5c 227/* static */
0b9ab0bd 228wxDllType wxDllLoader::LoadLibrary(const wxString & libname, bool *success)
a585ca5c 229{
0b9ab0bd
RL
230 wxDllType handle;
231 bool failed = FALSE;
7b0bfbb2 232
03e11df5 233#if defined(__WXMAC__) && !defined(__UNIX__)
0b9ab0bd
RL
234 FSSpec myFSSpec;
235 Ptr myMainAddr;
236 Str255 myErrName;
237
238 wxMacFilename2FSSpec( libname , &myFSSpec );
239
240 if( GetDiskFragment( &myFSSpec,
241 0,
242 kCFragGoesToEOF,
243 "\p",
244 kPrivateCFragCopy,
245 &handle,
246 &myMainAddr,
247 myErrName ) != noErr )
7cc98b3e 248 {
0b9ab0bd
RL
249 p2cstr( myErrName );
250 wxLogSysError( _("Failed to load shared library '%s' Error '%s'"),
251 libname.c_str(),
252 (char*)myErrName );
253 handle = 0;
254 failed = TRUE;
7cc98b3e 255 }
0b9ab0bd 256
1a787c5d 257#elif defined(__WXPM__) || defined(__EMX__)
0b9ab0bd 258 char zError[256] = "";
c2ff79b1 259 wxDllOpen(zError, libname, handle);
0b9ab0bd
RL
260
261#else
93d2bf44 262 handle = wxDllOpen(libname);
0b9ab0bd
RL
263
264#endif
a585ca5c 265
7cc98b3e
VZ
266 if ( !handle )
267 {
f6bcfd97
BP
268 wxString msg(_("Failed to load shared library '%s'"));
269
270#ifdef HAVE_DLERROR
d0166913 271 const wxChar *err = dlerror();
0b9ab0bd 272 if( err )
f6bcfd97 273 {
0b9ab0bd
RL
274 failed = TRUE;
275 wxLogError( msg, err );
f6bcfd97 276 }
0b9ab0bd
RL
277#else
278 failed = TRUE;
279 wxLogSysError( msg, libname.c_str() );
280#endif
7cc98b3e
VZ
281 }
282
283 if ( success )
0b9ab0bd 284 *success = !failed;
7cc98b3e
VZ
285
286 return handle;
a585ca5c
KB
287}
288
752c7d6b
KB
289
290/* static */
0b9ab0bd 291void wxDllLoader::UnloadLibrary(wxDllType handle)
752c7d6b
KB
292{
293 wxDllClose(handle);
294}
295
a585ca5c 296/* static */
0b9ab0bd 297void *wxDllLoader::GetSymbol(wxDllType dllHandle, const wxString &name, bool *success)
a585ca5c 298{
0b9ab0bd
RL
299 bool failed = FALSE;
300 void *symbol = 0;
a585ca5c 301
03e11df5 302#if defined(__WXMAC__) && !defined(__UNIX__)
0b9ab0bd
RL
303 Ptr symAddress;
304 CFragSymbolClass symClass;
305 Str255 symName;
7cc98b3e 306
03e11df5 307#if TARGET_CARBON
0b9ab0bd 308 c2pstrcpy( (StringPtr) symName, name );
03e11df5 309#else
0b9ab0bd
RL
310 strcpy( (char *) symName, name );
311 c2pstr( (char *) symName );
03e11df5 312#endif
0b9ab0bd
RL
313 if( FindSymbol( dllHandle, symName, &symAddress, &symClass ) == noErr )
314 symbol = (void *)symAddress;
7cc98b3e 315
0b9ab0bd 316#elif defined(__WXPM__) || defined(__EMX__)
c2ff79b1 317 wxDllGetSymbol(dllHandle, symbol);
0b9ab0bd 318
7b0bfbb2 319#else
f6bcfd97 320 // mb_str() is necessary in Unicode build
4c27c3c6 321 symbol = wxDllGetSymbol(dllHandle, name.mb_str());
0b9ab0bd 322
123a7fdd 323#endif
7b0bfbb2
VZ
324
325 if ( !symbol )
326 {
0b9ab0bd
RL
327 wxString msg(_("wxDllLoader failed to GetSymbol '%s'"));
328
329#ifdef HAVE_DLERROR
d0166913 330 const wxChar *err = dlerror();
0b9ab0bd
RL
331 if( err )
332 {
333 failed = TRUE;
334 wxLogError( msg, err );
335 }
336#else
337 failed = TRUE;
7cc98b3e
VZ
338 wxLogSysError(_("Couldn't find symbol '%s' in a dynamic library"),
339 name.c_str());
0b9ab0bd 340#endif
7b0bfbb2 341 }
0b9ab0bd
RL
342
343 if( success )
344 *success = !failed;
345
7b0bfbb2 346 return symbol;
7a4b9130
GL
347}
348
349// ---------------------------------------------------------------------------
350// wxLibraries (only one instance should normally exist)
351// ---------------------------------------------------------------------------
352
65fd5cb0 353wxLibraries::wxLibraries():m_loaded(wxKEY_STRING)
7a4b9130
GL
354{
355}
356
357wxLibraries::~wxLibraries()
358{
7b0bfbb2 359 wxNode *node = m_loaded.First();
7a4b9130 360
7b0bfbb2
VZ
361 while (node) {
362 wxLibrary *lib = (wxLibrary *)node->Data();
363 delete lib;
7a4b9130 364
7b0bfbb2
VZ
365 node = node->Next();
366 }
7a4b9130
GL
367}
368
369wxLibrary *wxLibraries::LoadLibrary(const wxString& name)
370{
0b9ab0bd 371 wxLibrary *lib;
7b0bfbb2 372 wxClassInfo *old_sm_first;
0b9ab0bd 373 wxNode *node = m_loaded.Find(name.GetData());
7a4b9130 374
c2ff79b1
DW
375 if (node != NULL)
376 return ((wxLibrary *)node->Data());
0b9ab0bd 377
7b0bfbb2
VZ
378 // If DLL shares data, this is necessary.
379 old_sm_first = wxClassInfo::sm_first;
380 wxClassInfo::sm_first = NULL;
381
7cc98b3e 382 wxString libname = ConstructLibraryName(name);
856d2e52 383
4dc2c3bb 384 bool success = FALSE;
7cc98b3e 385 wxDllType handle = wxDllLoader::LoadLibrary(libname, &success);
a585ca5c 386 if(success)
7b0bfbb2 387 {
a585ca5c
KB
388 lib = new wxLibrary(handle);
389 wxClassInfo::sm_first = old_sm_first;
0b9ab0bd 390
a585ca5c 391 m_loaded.Append(name.GetData(), lib);
7b0bfbb2 392 }
a585ca5c
KB
393 else
394 lib = NULL;
7b0bfbb2 395 return lib;
7a4b9130
GL
396}
397
398wxObject *wxLibraries::CreateObject(const wxString& path)
399{
7b0bfbb2
VZ
400 wxNode *node = m_loaded.First();
401 wxObject *obj;
7a4b9130 402
7b0bfbb2
VZ
403 while (node) {
404 obj = ((wxLibrary *)node->Data())->CreateObject(path);
405 if (obj)
406 return obj;
7a4b9130 407
7b0bfbb2
VZ
408 node = node->Next();
409 }
410 return NULL;
7a4b9130 411}
8a0d4cf6 412
f11bdd03
GD
413#ifdef __DARWIN__
414// ---------------------------------------------------------------------------
415// For Darwin/Mac OS X
416// supply the sun style dlopen functions in terms of Darwin NS*
417// ---------------------------------------------------------------------------
418
419extern "C" {
420#import <mach-o/dyld.h>
421};
422
423enum dyldErrorSource
424{
425 OFImage,
426};
427
428static char dl_last_error[1024];
429
430static
431void TranslateError(const char *path, enum dyldErrorSource type, int number)
432{
433 unsigned int index;
434 static char *OFIErrorStrings[] =
435 {
436 "%s(%d): Object Image Load Failure\n",
437 "%s(%d): Object Image Load Success\n",
438 "%s(%d): Not an recognisable object file\n",
439 "%s(%d): No valid architecture\n",
440 "%s(%d): Object image has an invalid format\n",
441 "%s(%d): Invalid access (permissions?)\n",
442 "%s(%d): Unknown error code from NSCreateObjectFileImageFromFile\n",
443 };
444#define NUM_OFI_ERRORS (sizeof(OFIErrorStrings) / sizeof(OFIErrorStrings[0]))
445
446 switch (type)
447 {
448 case OFImage:
449 index = number;
450 if (index > NUM_OFI_ERRORS - 1) {
451 index = NUM_OFI_ERRORS - 1;
452 }
453 sprintf(dl_last_error, OFIErrorStrings[index], path, number);
454 break;
455
456 default:
457 sprintf(dl_last_error, "%s(%d): Totally unknown error type %d\n",
458 path, number, type);
459 break;
460 }
461}
462
463const char *dlerror()
464{
465 return dl_last_error;
466}
467
468void *dlopen(const char *path, int mode /* mode is ignored */)
469{
470 int dyld_result;
471 NSObjectFileImage ofile;
472 NSModule handle = NULL;
473
474 dyld_result = NSCreateObjectFileImageFromFile(path, &ofile);
475 if (dyld_result != NSObjectFileImageSuccess)
476 {
477 TranslateError(path, OFImage, dyld_result);
478 }
479 else
480 {
481 // NSLinkModule will cause the run to abort on any link error's
482 // not very friendly but the error recovery functionality is limited.
483 handle = NSLinkModule(ofile, path, TRUE);
484 }
485
486 return handle;
487}
488
489int dlclose(void *handle) /* stub only */
490{
491 return 0;
492}
493
494void *dlsym(void *handle, const char *symbol)
495{
496 void *addr;
497
498 if (NSIsSymbolNameDefined(symbol)) {
499 addr = NSAddressOfSymbol(NSLookupAndBindSymbol(symbol));
500 }
501 else {
502 addr = NULL;
503 }
504 return addr;
505}
506
507#endif // __DARWIN__
508
8a0d4cf6 509#endif // wxUSE_DYNLIB_CLASS