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"
31 #if defined(HAVE_DLOPEN) || defined(__DARWIN__)
32 #define USE_POSIX_DL_FUNCS
33 #elif !defined(HAVE_SHL_LOAD)
34 #error "Don't know how to load dynamic libraries on this platform!"
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
41 // standard shared libraries extensions for different Unix versions
43 const wxChar
*wxDynamicLibrary::ms_dllext
= _T(".sl");
44 #elif defined(__DARWIN__)
45 const wxChar
*wxDynamicLibrary::ms_dllext
= _T(".bundle");
47 const wxChar
*wxDynamicLibrary::ms_dllext
= _T(".so");
50 // ============================================================================
51 // wxDynamicLibrary implementation
52 // ============================================================================
54 // ----------------------------------------------------------------------------
55 // dlxxx() emulation for Darwin
56 // ----------------------------------------------------------------------------
58 #if defined(__DARWIN__)
59 // ---------------------------------------------------------------------------
60 // For Darwin/Mac OS X
61 // supply the sun style dlopen functions in terms of Darwin NS*
62 // ---------------------------------------------------------------------------
65 * The dlopen port is a port from dl_next.xs by Anno Siegel.
66 * dl_next.xs is itself a port from dl_dlopen.xs by Paul Marquess.
67 * The method used here is just to supply the sun style dlopen etc.
68 * functions in terms of Darwin NS*.
72 #include <mach-o/dyld.h>
74 static char dl_last_error
[1024];
77 void TranslateError(const char *path
, int number
)
80 static char *OFIErrorStrings
[] =
82 "%s(%d): Object Image Load Failure\n",
83 "%s(%d): Object Image Load Success\n",
84 "%s(%d): Not an recognisable object file\n",
85 "%s(%d): No valid architecture\n",
86 "%s(%d): Object image has an invalid format\n",
87 "%s(%d): Invalid access (permissions?)\n",
88 "%s(%d): Unknown error code from NSCreateObjectFileImageFromFile\n",
90 #define NUM_OFI_ERRORS (sizeof(OFIErrorStrings) / sizeof(OFIErrorStrings[0]))
93 if (index
> NUM_OFI_ERRORS
- 1) {
94 index
= NUM_OFI_ERRORS
- 1;
96 sprintf(dl_last_error
, OFIErrorStrings
[index
], path
, number
);
101 return dl_last_error
;
104 void *dlopen(const char *path
, int WXUNUSED(mode
) /* mode is ignored */)
106 NSObjectFileImage ofile
;
107 NSModule handle
= NULL
;
109 int dyld_result
= NSCreateObjectFileImageFromFile(path
, &ofile
);
110 if ( dyld_result
!= NSObjectFileImageSuccess
)
116 handle
= NSLinkModule
120 NSLINKMODULE_OPTION_BINDNOW
|
121 NSLINKMODULE_OPTION_RETURN_ON_ERROR
126 TranslateError(path
, dyld_result
);
131 int dlclose(void *handle
)
133 NSUnLinkModule( handle
, NSUNLINKMODULE_OPTION_NONE
);
137 void *dlsym(void *handle
, const char *symbol
)
139 // as on many other systems, C symbols have prepended underscores under
140 // Darwin but unlike the normal dlopen(), NSLookupSymbolInModule() is not
142 wxCharBuffer
buf(strlen(symbol
) + 1);
143 char *p
= buf
.data();
145 strcpy(p
+ 1, symbol
);
147 NSSymbol nsSymbol
= NSLookupSymbolInModule( handle
, p
);
148 return nsSymbol
? NSAddressOfSymbol(nsSymbol
) : NULL
;
151 #endif // defined(__DARWIN__)
153 // ----------------------------------------------------------------------------
154 // loading/unloading DLLs
155 // ----------------------------------------------------------------------------
157 wxDllType
wxDynamicLibrary::GetProgramHandle()
159 #ifdef USE_POSIX_DL_FUNCS
160 return dlopen(0, RTLD_LAZY
);
167 wxDllType
wxDynamicLibrary::RawLoad(const wxString
& libname
, int flags
)
169 wxASSERT_MSG( (flags
& wxDL_NOW
) == 0,
170 _T("wxDL_LAZY and wxDL_NOW are mutually exclusive.") );
172 #ifdef USE_POSIX_DL_FUNCS
176 if ( flags
& wxDL_LAZY
)
178 rtldFlags
|= RTLD_LAZY
;
182 if ( flags
& wxDL_NOW
)
184 rtldFlags
|= RTLD_NOW
;
188 if ( flags
& wxDL_GLOBAL
)
190 rtldFlags
|= RTLD_GLOBAL
;
194 return dlopen(libname
.fn_str(), rtldFlags
);
195 #else // !USE_POSIX_DL_FUNCS
198 if ( flags
& wxDL_LAZY
)
200 shlFlags
|= BIND_DEFERRED
;
202 else if ( flags
& wxDL_NOW
)
204 shlFlags
|= BIND_IMMEDIATE
;
207 return shl_load(libname
.fn_str(), shlFlags
, 0);
208 #endif // USE_POSIX_DL_FUNCS/!USE_POSIX_DL_FUNCS
212 void wxDynamicLibrary::Unload(wxDllType handle
)
214 #ifdef wxHAVE_DYNLIB_ERROR
218 #ifdef USE_POSIX_DL_FUNCS
220 #else // !USE_POSIX_DL_FUNCS
222 #endif // USE_POSIX_DL_FUNCS/!USE_POSIX_DL_FUNCS
224 #ifdef USE_POSIX_DL_FUNCS
231 void *wxDynamicLibrary::RawGetSymbol(wxDllType handle
, const wxString
& name
)
235 #ifdef USE_POSIX_DL_FUNCS
236 symbol
= dlsym(handle
, name
.fn_str());
237 #else // !USE_POSIX_DL_FUNCS
238 // note that shl_findsym modifies the handle argument to indicate where the
239 // symbol was found, but it's ok to modify the local handle copy here
240 if ( shl_findsym(&handle
, name
.fn_str(), TYPE_UNDEFINED
, &symbol
) != 0 )
242 #endif // USE_POSIX_DL_FUNCS/!USE_POSIX_DL_FUNCS
247 // ----------------------------------------------------------------------------
249 // ----------------------------------------------------------------------------
251 #ifdef wxHAVE_DYNLIB_ERROR
254 void wxDynamicLibrary::Error()
257 wxWCharBuffer buffer
= wxConvLocal
.cMB2WC( dlerror() );
258 const wxChar
*err
= buffer
;
260 const wxChar
*err
= dlerror();
263 wxLogError(wxT("%s"), err
? err
: _("Unknown dynamic library error"));
266 #endif // wxHAVE_DYNLIB_ERROR
268 // ----------------------------------------------------------------------------
269 // listing loaded modules
270 // ----------------------------------------------------------------------------
272 // wxDynamicLibraryDetails declares this class as its friend, so put the code
273 // initializing new details objects here
274 class wxDynamicLibraryDetailsCreator
277 // create a new wxDynamicLibraryDetails from the given data
278 static wxDynamicLibraryDetails
*
279 New(unsigned long start
, unsigned long end
, const wxString
& path
)
281 wxDynamicLibraryDetails
*details
= new wxDynamicLibraryDetails
;
282 details
->m_path
= path
;
283 details
->m_name
= path
.AfterLast(_T('/'));
284 details
->m_address
= wx_reinterpret_cast(void *, start
);
285 details
->m_length
= end
- start
;
287 // try to extract the library version from its name
288 const size_t posExt
= path
.rfind(_T(".so"));
289 if ( posExt
!= wxString::npos
)
291 if ( path
.c_str()[posExt
+ 3] == _T('.') )
293 // assume "libfoo.so.x.y.z" case
294 details
->m_version
.assign(path
, posExt
+ 4, wxString::npos
);
298 size_t posDash
= path
.find_last_of(_T('-'), posExt
);
299 if ( posDash
!= wxString::npos
)
301 // assume "libbar-x.y.z.so" case
303 details
->m_version
.assign(path
, posDash
, posExt
- posDash
);
313 wxDynamicLibraryDetailsArray
wxDynamicLibrary::ListLoaded()
315 wxDynamicLibraryDetailsArray dlls
;
318 // examine /proc/self/maps to find out what is loaded in our address space
319 wxFFile
file("/proc/self/maps");
320 if ( file
.IsOpened() )
322 // details of the module currently being parsed
324 unsigned long startCur
,
329 while ( fgets(buf
, WXSIZEOF(buf
), file
.fp()) )
331 // format is: start-end perm something? maj:min inode path
332 unsigned long start
, end
;
333 switch ( sscanf(buf
, "%08lx-%08lx %*4s %*08x %*02d:%*02d %*d %1024s\n",
334 &start
, &end
, path
) )
337 // there may be no path column
342 // nothing to do, read everything we wanted
347 buf
[strlen(buf
) - 1] = '\0';
348 wxLogDebug(_T("Failed to parse line \"%s\" in /proc/self/maps."),
353 wxString pathNew
= wxString::FromAscii(path
);
354 if ( pathCur
.empty() )
361 else if ( pathCur
== pathNew
)
363 // continuation of the same module
364 wxASSERT_MSG( start
== endCur
, _T("hole in /proc/self/maps?") );
367 else // end of the current module
369 dlls
.Add(wxDynamicLibraryDetailsCreator::New(startCur
,
381 #endif // wxUSE_DYNLIB_CLASS