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 #if defined(HAVE_DLOPEN) || defined(__DARWIN__)
37 #define USE_POSIX_DL_FUNCS
38 #elif !defined(HAVE_SHL_LOAD)
39 #error "Don't know how to load dynamic libraries on this platform!"
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
46 // standard shared libraries extensions for different Unix versions
48 const wxChar
*wxDynamicLibrary::ms_dllext
= _T(".sl");
49 #elif defined(__DARWIN__)
50 const wxChar
*wxDynamicLibrary::ms_dllext
= _T(".bundle");
52 const wxChar
*wxDynamicLibrary::ms_dllext
= _T(".so");
55 // ============================================================================
56 // wxDynamicLibrary implementation
57 // ============================================================================
59 // ----------------------------------------------------------------------------
60 // dlxxx() emulation for Darwin
61 // ----------------------------------------------------------------------------
63 #if defined(__DARWIN__)
64 // ---------------------------------------------------------------------------
65 // For Darwin/Mac OS X
66 // supply the sun style dlopen functions in terms of Darwin NS*
67 // ---------------------------------------------------------------------------
70 * The dlopen port is a port from dl_next.xs by Anno Siegel.
71 * dl_next.xs is itself a port from dl_dlopen.xs by Paul Marquess.
72 * The method used here is just to supply the sun style dlopen etc.
73 * functions in terms of Darwin NS*.
77 #include <mach-o/dyld.h>
79 static char dl_last_error
[1024];
82 void TranslateError(const char *path
, int number
)
85 static char *OFIErrorStrings
[] =
87 "%s(%d): Object Image Load Failure\n",
88 "%s(%d): Object Image Load Success\n",
89 "%s(%d): Not an recognisable object file\n",
90 "%s(%d): No valid architecture\n",
91 "%s(%d): Object image has an invalid format\n",
92 "%s(%d): Invalid access (permissions?)\n",
93 "%s(%d): Unknown error code from NSCreateObjectFileImageFromFile\n",
95 #define NUM_OFI_ERRORS (sizeof(OFIErrorStrings) / sizeof(OFIErrorStrings[0]))
98 if (index
> NUM_OFI_ERRORS
- 1) {
99 index
= NUM_OFI_ERRORS
- 1;
101 sprintf(dl_last_error
, OFIErrorStrings
[index
], path
, number
);
104 const char *dlerror()
106 return dl_last_error
;
109 void *dlopen(const char *path
, int WXUNUSED(mode
) /* mode is ignored */)
111 NSObjectFileImage ofile
;
112 NSModule handle
= NULL
;
114 int dyld_result
= NSCreateObjectFileImageFromFile(path
, &ofile
);
115 if ( dyld_result
!= NSObjectFileImageSuccess
)
121 handle
= NSLinkModule
125 NSLINKMODULE_OPTION_BINDNOW
|
126 NSLINKMODULE_OPTION_RETURN_ON_ERROR
131 TranslateError(path
, dyld_result
);
136 int dlclose(void *handle
)
138 NSUnLinkModule( handle
, NSUNLINKMODULE_OPTION_NONE
);
142 void *dlsym(void *handle
, const char *symbol
)
144 // as on many other systems, C symbols have prepended underscores under
145 // Darwin but unlike the normal dlopen(), NSLookupSymbolInModule() is not
147 wxCharBuffer
buf(strlen(symbol
) + 1);
148 char *p
= buf
.data();
150 strcpy(p
+ 1, symbol
);
152 NSSymbol nsSymbol
= NSLookupSymbolInModule( handle
, p
);
153 return nsSymbol
? NSAddressOfSymbol(nsSymbol
) : NULL
;
156 #endif // defined(__DARWIN__)
158 // ----------------------------------------------------------------------------
159 // loading/unloading DLLs
160 // ----------------------------------------------------------------------------
162 wxDllType
wxDynamicLibrary::GetProgramHandle()
164 #ifdef USE_POSIX_DL_FUNCS
165 return dlopen(0, RTLD_LAZY
);
172 wxDllType
wxDynamicLibrary::RawLoad(const wxString
& libname
, int flags
)
174 wxASSERT_MSG( (flags
& wxDL_NOW
) == 0,
175 _T("wxDL_LAZY and wxDL_NOW are mutually exclusive.") );
177 #ifdef USE_POSIX_DL_FUNCS
181 if ( flags
& wxDL_LAZY
)
183 rtldFlags
|= RTLD_LAZY
;
187 if ( flags
& wxDL_NOW
)
189 rtldFlags
|= RTLD_NOW
;
193 if ( flags
& wxDL_GLOBAL
)
195 rtldFlags
|= RTLD_GLOBAL
;
199 return dlopen(libname
.fn_str(), rtldFlags
);
200 #else // !USE_POSIX_DL_FUNCS
203 if ( flags
& wxDL_LAZY
)
205 shlFlags
|= BIND_DEFERRED
;
207 else if ( flags
& wxDL_NOW
)
209 shlFlags
|= BIND_IMMEDIATE
;
212 return shl_load(libname
.fn_str(), shlFlags
, 0);
213 #endif // USE_POSIX_DL_FUNCS/!USE_POSIX_DL_FUNCS
217 void wxDynamicLibrary::Unload(wxDllType handle
)
219 #ifdef wxHAVE_DYNLIB_ERROR
223 #ifdef USE_POSIX_DL_FUNCS
225 #else // !USE_POSIX_DL_FUNCS
227 #endif // USE_POSIX_DL_FUNCS/!USE_POSIX_DL_FUNCS
229 #ifdef USE_POSIX_DL_FUNCS
236 void *wxDynamicLibrary::RawGetSymbol(wxDllType handle
, const wxString
& name
)
240 #ifdef USE_POSIX_DL_FUNCS
241 symbol
= dlsym(handle
, name
.fn_str());
242 #else // !USE_POSIX_DL_FUNCS
243 // note that shl_findsym modifies the handle argument to indicate where the
244 // symbol was found, but it's ok to modify the local handle copy here
245 if ( shl_findsym(&handle
, name
.fn_str(), TYPE_UNDEFINED
, &symbol
) != 0 )
247 #endif // USE_POSIX_DL_FUNCS/!USE_POSIX_DL_FUNCS
252 // ----------------------------------------------------------------------------
254 // ----------------------------------------------------------------------------
256 #ifdef wxHAVE_DYNLIB_ERROR
259 void wxDynamicLibrary::Error()
262 wxWCharBuffer buffer
= wxConvLocal
.cMB2WC( dlerror() );
263 const wxChar
*err
= buffer
;
265 const wxChar
*err
= dlerror();
268 wxLogError(wxT("%s"), err
? err
: _("Unknown dynamic library error"));
271 #endif // wxHAVE_DYNLIB_ERROR
273 // ----------------------------------------------------------------------------
274 // listing loaded modules
275 // ----------------------------------------------------------------------------
277 // wxDynamicLibraryDetails declares this class as its friend, so put the code
278 // initializing new details objects here
279 class wxDynamicLibraryDetailsCreator
282 // create a new wxDynamicLibraryDetails from the given data
283 static wxDynamicLibraryDetails
*
284 New(unsigned long start
, unsigned long end
, const wxString
& path
)
286 wxDynamicLibraryDetails
*details
= new wxDynamicLibraryDetails
;
287 details
->m_path
= path
;
288 details
->m_name
= path
.AfterLast(_T('/'));
289 details
->m_address
= wx_reinterpret_cast(void *, start
);
290 details
->m_length
= end
- start
;
292 // try to extract the library version from its name
293 const size_t posExt
= path
.rfind(_T(".so"));
294 if ( posExt
!= wxString::npos
)
296 if ( path
.c_str()[posExt
+ 3] == _T('.') )
298 // assume "libfoo.so.x.y.z" case
299 details
->m_version
.assign(path
, posExt
+ 4, wxString::npos
);
303 size_t posDash
= path
.find_last_of(_T('-'), posExt
);
304 if ( posDash
!= wxString::npos
)
306 // assume "libbar-x.y.z.so" case
308 details
->m_version
.assign(path
, posDash
, posExt
- posDash
);
318 wxDynamicLibraryDetailsArray
wxDynamicLibrary::ListLoaded()
320 wxDynamicLibraryDetailsArray dlls
;
323 // examine /proc/self/maps to find out what is loaded in our address space
324 wxFFile
file("/proc/self/maps");
325 if ( file
.IsOpened() )
327 // details of the module currently being parsed
329 unsigned long startCur
,
334 while ( fgets(buf
, WXSIZEOF(buf
), file
.fp()) )
336 // format is: start-end perm something? maj:min inode path
337 unsigned long start
, end
;
338 switch ( sscanf(buf
, "%08lx-%08lx %*4s %*08x %*02d:%*02d %*d %1024s\n",
339 &start
, &end
, path
) )
342 // there may be no path column
347 // nothing to do, read everything we wanted
352 buf
[strlen(buf
) - 1] = '\0';
353 wxLogDebug(_T("Failed to parse line \"%s\" in /proc/self/maps."),
358 wxString pathNew
= wxString::FromAscii(path
);
359 if ( pathCur
.empty() )
366 else if ( pathCur
== pathNew
)
368 // continuation of the same module
369 wxASSERT_MSG( start
== endCur
, _T("hole in /proc/self/maps?") );
372 else // end of the current module
374 dlls
.Add(wxDynamicLibraryDetailsCreator::New(startCur
,
386 #endif // wxUSE_DYNLIB_CLASS