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(__DARWIN__)
40 #if defined(HAVE_DLOPEN) || defined(__DARWIN__)
41 #define USE_POSIX_DL_FUNCS
42 #elif !defined(HAVE_SHL_LOAD)
43 #error "Don't know how to load dynamic libraries on this platform!"
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 // standard shared libraries extensions for different Unix versions
52 const wxChar
*wxDynamicLibrary::ms_dllext
= _T(".sl");
53 #elif defined(__DARWIN__)
54 const wxChar
*wxDynamicLibrary::ms_dllext
= _T(".bundle");
56 const wxChar
*wxDynamicLibrary::ms_dllext
= _T(".so");
59 // ============================================================================
60 // wxDynamicLibrary implementation
61 // ============================================================================
63 // ----------------------------------------------------------------------------
64 // dlxxx() emulation for Darwin
65 // ----------------------------------------------------------------------------
67 #if defined(__DARWIN__)
68 // ---------------------------------------------------------------------------
69 // For Darwin/Mac OS X
70 // supply the sun style dlopen functions in terms of Darwin NS*
71 // ---------------------------------------------------------------------------
74 * The dlopen port is a port from dl_next.xs by Anno Siegel.
75 * dl_next.xs is itself a port from dl_dlopen.xs by Paul Marquess.
76 * The method used here is just to supply the sun style dlopen etc.
77 * functions in terms of Darwin NS*.
81 #include <mach-o/dyld.h>
83 static char dl_last_error
[1024];
86 void TranslateError(const char *path
, int number
)
89 static char *OFIErrorStrings
[] =
91 "%s(%d): Object Image Load Failure\n",
92 "%s(%d): Object Image Load Success\n",
93 "%s(%d): Not an recognisable object file\n",
94 "%s(%d): No valid architecture\n",
95 "%s(%d): Object image has an invalid format\n",
96 "%s(%d): Invalid access (permissions?)\n",
97 "%s(%d): Unknown error code from NSCreateObjectFileImageFromFile\n",
99 #define NUM_OFI_ERRORS (sizeof(OFIErrorStrings) / sizeof(OFIErrorStrings[0]))
102 if (index
> NUM_OFI_ERRORS
- 1) {
103 index
= NUM_OFI_ERRORS
- 1;
105 sprintf(dl_last_error
, OFIErrorStrings
[index
], path
, number
);
108 const char *dlerror()
110 return dl_last_error
;
113 void *dlopen(const char *path
, int WXUNUSED(mode
) /* mode is ignored */)
115 NSObjectFileImage ofile
;
116 NSModule handle
= NULL
;
118 int dyld_result
= NSCreateObjectFileImageFromFile(path
, &ofile
);
119 if ( dyld_result
!= NSObjectFileImageSuccess
)
125 handle
= NSLinkModule
129 NSLINKMODULE_OPTION_BINDNOW
|
130 NSLINKMODULE_OPTION_RETURN_ON_ERROR
135 TranslateError(path
, dyld_result
);
140 int dlclose(void *handle
)
142 NSUnLinkModule( handle
, NSUNLINKMODULE_OPTION_NONE
);
146 void *dlsym(void *handle
, const char *symbol
)
148 // as on many other systems, C symbols have prepended underscores under
149 // Darwin but unlike the normal dlopen(), NSLookupSymbolInModule() is not
151 wxCharBuffer
buf(strlen(symbol
) + 1);
152 char *p
= buf
.data();
154 strcpy(p
+ 1, symbol
);
156 NSSymbol nsSymbol
= NSLookupSymbolInModule( handle
, p
);
157 return nsSymbol
? NSAddressOfSymbol(nsSymbol
) : NULL
;
160 #endif // defined(__DARWIN__)
162 // ----------------------------------------------------------------------------
163 // loading/unloading DLLs
164 // ----------------------------------------------------------------------------
166 wxDllType
wxDynamicLibrary::GetProgramHandle()
168 #ifdef USE_POSIX_DL_FUNCS
169 return dlopen(0, RTLD_LAZY
);
176 wxDllType
wxDynamicLibrary::RawLoad(const wxString
& libname
, int flags
)
178 wxASSERT_MSG( (flags
& wxDL_NOW
) == 0,
179 _T("wxDL_LAZY and wxDL_NOW are mutually exclusive.") );
181 #ifdef USE_POSIX_DL_FUNCS
185 if ( flags
& wxDL_LAZY
)
187 rtldFlags
|= RTLD_LAZY
;
191 if ( flags
& wxDL_NOW
)
193 rtldFlags
|= RTLD_NOW
;
197 if ( flags
& wxDL_GLOBAL
)
199 rtldFlags
|= RTLD_GLOBAL
;
203 return dlopen(libname
.fn_str(), rtldFlags
);
204 #else // !USE_POSIX_DL_FUNCS
207 if ( flags
& wxDL_LAZY
)
209 shlFlags
|= BIND_DEFERRED
;
211 else if ( flags
& wxDL_NOW
)
213 shlFlags
|= BIND_IMMEDIATE
;
216 return shl_load(libname
.fn_str(), shlFlags
, 0);
217 #endif // USE_POSIX_DL_FUNCS/!USE_POSIX_DL_FUNCS
221 void wxDynamicLibrary::Unload(wxDllType handle
)
223 #ifdef wxHAVE_DYNLIB_ERROR
227 #ifdef USE_POSIX_DL_FUNCS
229 #else // !USE_POSIX_DL_FUNCS
231 #endif // USE_POSIX_DL_FUNCS/!USE_POSIX_DL_FUNCS
233 #if defined(USE_POSIX_DL_FUNCS) && defined(wxHAVE_DYNLIB_ERROR)
240 void *wxDynamicLibrary::RawGetSymbol(wxDllType handle
, const wxString
& name
)
244 #ifdef USE_POSIX_DL_FUNCS
245 symbol
= dlsym(handle
, name
.fn_str());
246 #else // !USE_POSIX_DL_FUNCS
247 // note that shl_findsym modifies the handle argument to indicate where the
248 // symbol was found, but it's ok to modify the local handle copy here
249 if ( shl_findsym(&handle
, name
.fn_str(), TYPE_UNDEFINED
, &symbol
) != 0 )
251 #endif // USE_POSIX_DL_FUNCS/!USE_POSIX_DL_FUNCS
256 // ----------------------------------------------------------------------------
258 // ----------------------------------------------------------------------------
260 #ifdef wxHAVE_DYNLIB_ERROR
263 void wxDynamicLibrary::Error()
266 wxWCharBuffer buffer
= wxConvLocal
.cMB2WC( dlerror() );
267 const wxChar
*err
= buffer
;
269 const wxChar
*err
= dlerror();
272 wxLogError(wxT("%s"), err
? err
: _("Unknown dynamic library error"));
275 #endif // wxHAVE_DYNLIB_ERROR
277 // ----------------------------------------------------------------------------
278 // listing loaded modules
279 // ----------------------------------------------------------------------------
281 // wxDynamicLibraryDetails declares this class as its friend, so put the code
282 // initializing new details objects here
283 class wxDynamicLibraryDetailsCreator
286 // create a new wxDynamicLibraryDetails from the given data
287 static wxDynamicLibraryDetails
*
288 New(unsigned long start
, unsigned long end
, const wxString
& path
)
290 wxDynamicLibraryDetails
*details
= new wxDynamicLibraryDetails
;
291 details
->m_path
= path
;
292 details
->m_name
= path
.AfterLast(_T('/'));
293 details
->m_address
= wx_reinterpret_cast(void *, start
);
294 details
->m_length
= end
- start
;
296 // try to extract the library version from its name
297 const size_t posExt
= path
.rfind(_T(".so"));
298 if ( posExt
!= wxString::npos
)
300 if ( path
.c_str()[posExt
+ 3] == _T('.') )
302 // assume "libfoo.so.x.y.z" case
303 details
->m_version
.assign(path
, posExt
+ 4, wxString::npos
);
307 size_t posDash
= path
.find_last_of(_T('-'), posExt
);
308 if ( posDash
!= wxString::npos
)
310 // assume "libbar-x.y.z.so" case
312 details
->m_version
.assign(path
, posDash
, posExt
- posDash
);
322 wxDynamicLibraryDetailsArray
wxDynamicLibrary::ListLoaded()
324 wxDynamicLibraryDetailsArray dlls
;
327 // examine /proc/self/maps to find out what is loaded in our address space
328 wxFFile
file(_T("/proc/self/maps"));
329 if ( file
.IsOpened() )
331 // details of the module currently being parsed
333 unsigned long startCur
,
338 while ( fgets(buf
, WXSIZEOF(buf
), file
.fp()) )
340 // format is: start-end perm something? maj:min inode path
341 unsigned long start
, end
;
342 switch ( sscanf(buf
, "%08lx-%08lx %*4s %*08x %*02d:%*02d %*d %1024s\n",
343 &start
, &end
, path
) )
346 // there may be no path column
351 // nothing to do, read everything we wanted
356 buf
[strlen(buf
) - 1] = '\0';
357 wxLogDebug(_T("Failed to parse line \"%s\" in /proc/self/maps."),
362 wxString pathNew
= wxString::FromAscii(path
);
363 if ( pathCur
.empty() )
370 else if ( pathCur
== pathNew
)
372 // continuation of the same module
373 wxASSERT_MSG( start
== endCur
, _T("hole in /proc/self/maps?") );
376 else // end of the current module
378 dlls
.Add(wxDynamicLibraryDetailsCreator::New(startCur
,
390 #endif // wxUSE_DYNLIB_CLASS