1 /////////////////////////////////////////////////////////////////////////////
2 // Name: unix/dlunix.cpp
3 // Purpose: Unix-specific part of wxDynamicLibrary and related classes
4 // Author: Vadim Zeitlin
6 // Created: 2005-01-16 (extracted from common/dynlib.cpp)
8 // Copyright: (c) 2000-2005 Vadim Zeitlin <vadim@wxwindows.org>
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #include "wx/wxprec.h"
26 #if wxUSE_DYNLIB_CLASS
28 #include "wx/dynlib.h"
36 // only Mac OS X 10.3+ has dlfcn.h, and it is simpler to always provide our own
37 // wrappers using the native functions instead of doing checks for OS version
42 // if some flags are not supported, just ignore them
56 #if defined(HAVE_DLOPEN) || defined(__DARWIN__)
57 #define USE_POSIX_DL_FUNCS
58 #elif !defined(HAVE_SHL_LOAD)
59 #error "Don't know how to load dynamic libraries on this platform!"
62 // ----------------------------------------------------------------------------
64 // ----------------------------------------------------------------------------
66 // standard shared libraries extensions for different Unix versions
68 const wxChar
*wxDynamicLibrary::ms_dllext
= _T(".sl");
69 #elif defined(__DARWIN__)
70 const wxChar
*wxDynamicLibrary::ms_dllext
= _T(".bundle");
72 const wxChar
*wxDynamicLibrary::ms_dllext
= _T(".so");
75 // ============================================================================
76 // wxDynamicLibrary implementation
77 // ============================================================================
79 // ----------------------------------------------------------------------------
80 // dlxxx() emulation for Darwin
81 // ----------------------------------------------------------------------------
83 #if defined(__DARWIN__)
84 // ---------------------------------------------------------------------------
85 // For Darwin/Mac OS X
86 // supply the sun style dlopen functions in terms of Darwin NS*
87 // ---------------------------------------------------------------------------
90 * The dlopen port is a port from dl_next.xs by Anno Siegel.
91 * dl_next.xs is itself a port from dl_dlopen.xs by Paul Marquess.
92 * The method used here is just to supply the sun style dlopen etc.
93 * functions in terms of Darwin NS*.
97 #include <mach-o/dyld.h>
99 static char dl_last_error
[1024];
102 void TranslateError(const char *path
, int number
)
105 static char *OFIErrorStrings
[] =
107 "%s(%d): Object Image Load Failure\n",
108 "%s(%d): Object Image Load Success\n",
109 "%s(%d): Not an recognisable object file\n",
110 "%s(%d): No valid architecture\n",
111 "%s(%d): Object image has an invalid format\n",
112 "%s(%d): Invalid access (permissions?)\n",
113 "%s(%d): Unknown error code from NSCreateObjectFileImageFromFile\n",
115 #define NUM_OFI_ERRORS (sizeof(OFIErrorStrings) / sizeof(OFIErrorStrings[0]))
118 if (index
> NUM_OFI_ERRORS
- 1) {
119 index
= NUM_OFI_ERRORS
- 1;
121 sprintf(dl_last_error
, OFIErrorStrings
[index
], path
, number
);
124 const char *dlerror()
126 return dl_last_error
;
129 void *dlopen(const char *path
, int WXUNUSED(mode
) /* mode is ignored */)
131 NSObjectFileImage ofile
;
132 NSModule handle
= NULL
;
134 int dyld_result
= NSCreateObjectFileImageFromFile(path
, &ofile
);
135 if ( dyld_result
!= NSObjectFileImageSuccess
)
141 handle
= NSLinkModule
145 NSLINKMODULE_OPTION_BINDNOW
|
146 NSLINKMODULE_OPTION_RETURN_ON_ERROR
151 TranslateError(path
, dyld_result
);
156 int dlclose(void *handle
)
158 NSUnLinkModule((NSModule
)handle
, NSUNLINKMODULE_OPTION_NONE
);
162 void *dlsym(void *handle
, const char *symbol
)
164 // as on many other systems, C symbols have prepended underscores under
165 // Darwin but unlike the normal dlopen(), NSLookupSymbolInModule() is not
167 wxCharBuffer
buf(strlen(symbol
) + 1);
168 char *p
= buf
.data();
170 strcpy(p
+ 1, symbol
);
172 NSSymbol nsSymbol
= NSLookupSymbolInModule((NSModule
)handle
, p
);
173 return nsSymbol
? NSAddressOfSymbol(nsSymbol
) : NULL
;
176 #endif // defined(__DARWIN__)
178 // ----------------------------------------------------------------------------
179 // loading/unloading DLLs
180 // ----------------------------------------------------------------------------
182 wxDllType
wxDynamicLibrary::GetProgramHandle()
184 #ifdef USE_POSIX_DL_FUNCS
185 return dlopen(0, RTLD_LAZY
);
192 wxDllType
wxDynamicLibrary::RawLoad(const wxString
& libname
, int flags
)
194 wxASSERT_MSG( (flags
& wxDL_NOW
) == 0,
195 _T("wxDL_LAZY and wxDL_NOW are mutually exclusive.") );
197 #ifdef USE_POSIX_DL_FUNCS
200 if ( flags
& wxDL_LAZY
)
202 rtldFlags
|= RTLD_LAZY
;
204 if ( flags
& wxDL_NOW
)
206 rtldFlags
|= RTLD_NOW
;
208 if ( flags
& wxDL_GLOBAL
)
210 rtldFlags
|= RTLD_GLOBAL
;
213 return dlopen(libname
.fn_str(), rtldFlags
);
214 #else // !USE_POSIX_DL_FUNCS
217 if ( flags
& wxDL_LAZY
)
219 shlFlags
|= BIND_DEFERRED
;
221 else if ( flags
& wxDL_NOW
)
223 shlFlags
|= BIND_IMMEDIATE
;
226 return shl_load(libname
.fn_str(), shlFlags
, 0);
227 #endif // USE_POSIX_DL_FUNCS/!USE_POSIX_DL_FUNCS
231 void wxDynamicLibrary::Unload(wxDllType handle
)
233 #ifdef wxHAVE_DYNLIB_ERROR
237 #ifdef USE_POSIX_DL_FUNCS
239 #else // !USE_POSIX_DL_FUNCS
241 #endif // USE_POSIX_DL_FUNCS/!USE_POSIX_DL_FUNCS
243 #if defined(USE_POSIX_DL_FUNCS) && defined(wxHAVE_DYNLIB_ERROR)
250 void *wxDynamicLibrary::RawGetSymbol(wxDllType handle
, const wxString
& name
)
254 #ifdef USE_POSIX_DL_FUNCS
255 symbol
= dlsym(handle
, name
.fn_str());
256 #else // !USE_POSIX_DL_FUNCS
257 // note that shl_findsym modifies the handle argument to indicate where the
258 // symbol was found, but it's ok to modify the local handle copy here
259 if ( shl_findsym(&handle
, name
.fn_str(), TYPE_UNDEFINED
, &symbol
) != 0 )
261 #endif // USE_POSIX_DL_FUNCS/!USE_POSIX_DL_FUNCS
266 // ----------------------------------------------------------------------------
268 // ----------------------------------------------------------------------------
270 #ifdef wxHAVE_DYNLIB_ERROR
273 void wxDynamicLibrary::Error()
276 wxWCharBuffer buffer
= wxConvLocal
.cMB2WC( dlerror() );
277 const wxChar
*err
= buffer
;
279 const wxChar
*err
= dlerror();
282 wxLogError(wxT("%s"), err
? err
: _("Unknown dynamic library error"));
285 #endif // wxHAVE_DYNLIB_ERROR
287 // ----------------------------------------------------------------------------
288 // listing loaded modules
289 // ----------------------------------------------------------------------------
291 // wxDynamicLibraryDetails declares this class as its friend, so put the code
292 // initializing new details objects here
293 class wxDynamicLibraryDetailsCreator
296 // create a new wxDynamicLibraryDetails from the given data
297 static wxDynamicLibraryDetails
*
298 New(unsigned long start
, unsigned long end
, const wxString
& path
)
300 wxDynamicLibraryDetails
*details
= new wxDynamicLibraryDetails
;
301 details
->m_path
= path
;
302 details
->m_name
= path
.AfterLast(_T('/'));
303 details
->m_address
= wx_reinterpret_cast(void *, start
);
304 details
->m_length
= end
- start
;
306 // try to extract the library version from its name
307 const size_t posExt
= path
.rfind(_T(".so"));
308 if ( posExt
!= wxString::npos
)
310 if ( path
.c_str()[posExt
+ 3] == _T('.') )
312 // assume "libfoo.so.x.y.z" case
313 details
->m_version
.assign(path
, posExt
+ 4, wxString::npos
);
317 size_t posDash
= path
.find_last_of(_T('-'), posExt
);
318 if ( posDash
!= wxString::npos
)
320 // assume "libbar-x.y.z.so" case
322 details
->m_version
.assign(path
, posDash
, posExt
- posDash
);
332 wxDynamicLibraryDetailsArray
wxDynamicLibrary::ListLoaded()
334 wxDynamicLibraryDetailsArray dlls
;
337 // examine /proc/self/maps to find out what is loaded in our address space
338 wxFFile
file(_T("/proc/self/maps"));
339 if ( file
.IsOpened() )
341 // details of the module currently being parsed
343 unsigned long startCur
= 0,
348 while ( fgets(buf
, WXSIZEOF(buf
), file
.fp()) )
350 // format is: start-end perm something? maj:min inode path
351 unsigned long start
, end
;
352 switch ( sscanf(buf
, "%08lx-%08lx %*4s %*08x %*02d:%*02d %*d %1024s\n",
353 &start
, &end
, path
) )
356 // there may be no path column
361 // nothing to do, read everything we wanted
366 buf
[strlen(buf
) - 1] = '\0';
367 wxLogDebug(_T("Failed to parse line \"%s\" in /proc/self/maps."),
372 wxString pathNew
= wxString::FromAscii(path
);
373 if ( pathCur
.empty() )
380 else if ( pathCur
== pathNew
)
382 // continuation of the same module
383 wxASSERT_MSG( start
== endCur
, _T("hole in /proc/self/maps?") );
386 else // end of the current module
388 dlls
.Add(wxDynamicLibraryDetailsCreator::New(startCur
,
400 #endif // wxUSE_DYNLIB_CLASS