]> git.saurik.com Git - wxWidgets.git/blob - src/unix/dlunix.cpp
avoid setting pango matrix when not needed
[wxWidgets.git] / src / unix / dlunix.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/unix/dlunix.cpp
3 // Purpose: Unix-specific part of wxDynamicLibrary and related classes
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 2005-01-16 (extracted from common/dynlib.cpp)
7 // Copyright: (c) 2000-2005 Vadim Zeitlin <vadim@wxwindows.org>
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 #include "wx/wxprec.h"
20
21 #ifdef __BORLANDC__
22 #pragma hdrstop
23 #endif
24
25 #if wxUSE_DYNLIB_CLASS
26
27 #include "wx/dynlib.h"
28 #include "wx/ffile.h"
29
30 #ifndef WX_PRECOMP
31 #include "wx/intl.h"
32 #include "wx/log.h"
33 #endif
34
35 #ifdef HAVE_DLOPEN
36 #include <dlfcn.h>
37 #endif
38
39 #ifdef __DARWIN__
40 #include <AvailabilityMacros.h>
41 #endif
42
43 // if some flags are not supported, just ignore them
44 #ifndef RTLD_LAZY
45 #define RTLD_LAZY 0
46 #endif
47
48 #ifndef RTLD_NOW
49 #define RTLD_NOW 0
50 #endif
51
52 #ifndef RTLD_GLOBAL
53 #define RTLD_GLOBAL 0
54 #endif
55
56
57 #if defined(HAVE_DLOPEN) || defined(__DARWIN__)
58 #define USE_POSIX_DL_FUNCS
59 #elif !defined(HAVE_SHL_LOAD)
60 #error "Don't know how to load dynamic libraries on this platform!"
61 #endif
62
63 // ----------------------------------------------------------------------------
64 // constants
65 // ----------------------------------------------------------------------------
66
67 // ============================================================================
68 // wxDynamicLibrary implementation
69 // ============================================================================
70
71 // ----------------------------------------------------------------------------
72 // dlxxx() emulation for Darwin
73 // Only useful if the OS X version could be < 10.3 at runtime
74 // ----------------------------------------------------------------------------
75
76 #if defined(__DARWIN__) && (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_3)
77 // ---------------------------------------------------------------------------
78 // For Darwin/Mac OS X
79 // supply the sun style dlopen functions in terms of Darwin NS*
80 // ---------------------------------------------------------------------------
81
82 /* Porting notes:
83 * The dlopen port is a port from dl_next.xs by Anno Siegel.
84 * dl_next.xs is itself a port from dl_dlopen.xs by Paul Marquess.
85 * The method used here is just to supply the sun style dlopen etc.
86 * functions in terms of Darwin NS*.
87 */
88
89 #include <stdio.h>
90 #include <mach-o/dyld.h>
91
92 static char dl_last_error[1024];
93
94 static const char *wx_darwin_dlerror()
95 {
96 return dl_last_error;
97 }
98
99 static void *wx_darwin_dlopen(const char *path, int WXUNUSED(mode) /* mode is ignored */)
100 {
101 NSObjectFileImage ofile;
102 NSModule handle = NULL;
103
104 unsigned dyld_result = NSCreateObjectFileImageFromFile(path, &ofile);
105 if ( dyld_result != NSObjectFileImageSuccess )
106 {
107 handle = NULL;
108
109 static const char *const errorStrings[] =
110 {
111 "%d: Object Image Load Failure",
112 "%d: Object Image Load Success",
113 "%d: Not an recognisable object file",
114 "%d: No valid architecture",
115 "%d: Object image has an invalid format",
116 "%d: Invalid access (permissions?)",
117 "%d: Unknown error code from NSCreateObjectFileImageFromFile"
118 };
119
120 const int index = dyld_result < WXSIZEOF(errorStrings)
121 ? dyld_result
122 : WXSIZEOF(errorStrings) - 1;
123
124 // this call to sprintf() is safe as strings above are fixed at
125 // compile-time and are shorter than WXSIZEOF(dl_last_error)
126 sprintf(dl_last_error, errorStrings[index], dyld_result);
127 }
128 else
129 {
130 handle = NSLinkModule
131 (
132 ofile,
133 path,
134 NSLINKMODULE_OPTION_BINDNOW |
135 NSLINKMODULE_OPTION_RETURN_ON_ERROR
136 );
137
138 if ( !handle )
139 {
140 NSLinkEditErrors err;
141 int code;
142 const char *filename;
143 const char *errmsg;
144
145 NSLinkEditError(&err, &code, &filename, &errmsg);
146 strncpy(dl_last_error, errmsg, WXSIZEOF(dl_last_error)-1);
147 dl_last_error[WXSIZEOF(dl_last_error)-1] = '\0';
148 }
149 }
150
151
152 return handle;
153 }
154
155 static int wx_darwin_dlclose(void *handle)
156 {
157 NSUnLinkModule((NSModule)handle, NSUNLINKMODULE_OPTION_NONE);
158 return 0;
159 }
160
161 static void *wx_darwin_dlsym(void *handle, const char *symbol)
162 {
163 // as on many other systems, C symbols have prepended underscores under
164 // Darwin but unlike the normal dlopen(), NSLookupSymbolInModule() is not
165 // aware of this
166 wxCharBuffer buf(strlen(symbol) + 1);
167 char *p = buf.data();
168 p[0] = '_';
169 strcpy(p + 1, symbol);
170
171 NSSymbol nsSymbol = NSLookupSymbolInModule((NSModule)handle, p );
172 return nsSymbol ? NSAddressOfSymbol(nsSymbol) : NULL;
173 }
174
175 // Add the weak linking attribute to dlopen's declaration
176 extern void * dlopen(const char * __path, int __mode) AVAILABLE_MAC_OS_X_VERSION_10_3_AND_LATER;
177
178 // For all of these methods we test dlopen since all of the dl functions we use were added
179 // to OS X at the same time. This also ensures we don't dlopen with the real function then
180 // dlclose with the internal implementation.
181
182 static inline void *wx_dlopen(const char *__path, int __mode)
183 {
184 #ifdef HAVE_DLOPEN
185 if(&dlopen != NULL)
186 return dlopen(__path, __mode);
187 else
188 #endif
189 return wx_darwin_dlopen(__path, __mode);
190 }
191
192 static inline int wx_dlclose(void *__handle)
193 {
194 #ifdef HAVE_DLOPEN
195 if(&dlopen != NULL)
196 return dlclose(__handle);
197 else
198 #endif
199 return wx_darwin_dlclose(__handle);
200 }
201
202 static inline const char *wx_dlerror()
203 {
204 #ifdef HAVE_DLOPEN
205 if(&dlopen != NULL)
206 return dlerror();
207 else
208 #endif
209 return wx_darwin_dlerror();
210 }
211
212 static inline void *wx_dlsym(void *__handle, const char *__symbol)
213 {
214 #ifdef HAVE_DLOPEN
215 if(&dlopen != NULL)
216 return dlsym(__handle, __symbol);
217 else
218 #endif
219 return wx_darwin_dlsym(__handle, __symbol);
220 }
221
222 #else // __DARWIN__/!__DARWIN__
223
224 // Use preprocessor definitions for non-Darwin or OS X >= 10.3
225 #define wx_dlopen(__path,__mode) dlopen(__path,__mode)
226 #define wx_dlclose(__handle) dlclose(__handle)
227 #define wx_dlerror() dlerror()
228 #define wx_dlsym(__handle,__symbol) dlsym(__handle,__symbol)
229
230 #endif // defined(__DARWIN__)
231
232 // ----------------------------------------------------------------------------
233 // loading/unloading DLLs
234 // ----------------------------------------------------------------------------
235
236 wxDllType wxDynamicLibrary::GetProgramHandle()
237 {
238 #ifdef USE_POSIX_DL_FUNCS
239 return wx_dlopen(0, RTLD_LAZY);
240 #else
241 return PROG_HANDLE;
242 #endif
243 }
244
245 /* static */
246 wxDllType wxDynamicLibrary::RawLoad(const wxString& libname, int flags)
247 {
248 wxASSERT_MSG( !(flags & wxDL_NOW) || !(flags & wxDL_LAZY),
249 wxT("wxDL_LAZY and wxDL_NOW are mutually exclusive.") );
250
251 #ifdef USE_POSIX_DL_FUNCS
252 // we need to use either RTLD_NOW or RTLD_LAZY because if we call dlopen()
253 // with flags == 0 recent versions of glibc just fail the call, so use
254 // RTLD_NOW even if wxDL_NOW was not specified
255 int rtldFlags = flags & wxDL_LAZY ? RTLD_LAZY : RTLD_NOW;
256
257 if ( flags & wxDL_GLOBAL )
258 rtldFlags |= RTLD_GLOBAL;
259
260 return wx_dlopen(libname.fn_str(), rtldFlags);
261 #else // !USE_POSIX_DL_FUNCS
262 int shlFlags = 0;
263
264 if ( flags & wxDL_LAZY )
265 {
266 shlFlags |= BIND_DEFERRED;
267 }
268 else if ( flags & wxDL_NOW )
269 {
270 shlFlags |= BIND_IMMEDIATE;
271 }
272
273 return shl_load(libname.fn_str(), shlFlags, 0);
274 #endif // USE_POSIX_DL_FUNCS/!USE_POSIX_DL_FUNCS
275 }
276
277 /* static */
278 void wxDynamicLibrary::Unload(wxDllType handle)
279 {
280 #ifdef wxHAVE_DYNLIB_ERROR
281 int rc =
282 #endif
283
284 #ifdef USE_POSIX_DL_FUNCS
285 wx_dlclose(handle);
286 #else // !USE_POSIX_DL_FUNCS
287 shl_unload(handle);
288 #endif // USE_POSIX_DL_FUNCS/!USE_POSIX_DL_FUNCS
289
290 #if defined(USE_POSIX_DL_FUNCS) && defined(wxHAVE_DYNLIB_ERROR)
291 if ( rc != 0 )
292 Error();
293 #endif
294 }
295
296 /* static */
297 void *wxDynamicLibrary::RawGetSymbol(wxDllType handle, const wxString& name)
298 {
299 void *symbol;
300
301 #ifdef USE_POSIX_DL_FUNCS
302 symbol = wx_dlsym(handle, name.fn_str());
303 #else // !USE_POSIX_DL_FUNCS
304 // note that shl_findsym modifies the handle argument to indicate where the
305 // symbol was found, but it's ok to modify the local handle copy here
306 if ( shl_findsym(&handle, name.fn_str(), TYPE_UNDEFINED, &symbol) != 0 )
307 symbol = 0;
308 #endif // USE_POSIX_DL_FUNCS/!USE_POSIX_DL_FUNCS
309
310 return symbol;
311 }
312
313 // ----------------------------------------------------------------------------
314 // error handling
315 // ----------------------------------------------------------------------------
316
317 #ifdef wxHAVE_DYNLIB_ERROR
318
319 /* static */
320 void wxDynamicLibrary::Error()
321 {
322 wxString err(wx_dlerror());
323
324 if ( err.empty() )
325 err = _("Unknown dynamic library error");
326
327 wxLogError(wxT("%s"), err);
328 }
329
330 #endif // wxHAVE_DYNLIB_ERROR
331
332 // ----------------------------------------------------------------------------
333 // listing loaded modules
334 // ----------------------------------------------------------------------------
335
336 // wxDynamicLibraryDetails declares this class as its friend, so put the code
337 // initializing new details objects here
338 class wxDynamicLibraryDetailsCreator
339 {
340 public:
341 // create a new wxDynamicLibraryDetails from the given data
342 static wxDynamicLibraryDetails *
343 New(void *start, void *end, const wxString& path)
344 {
345 wxDynamicLibraryDetails *details = new wxDynamicLibraryDetails;
346 details->m_path = path;
347 details->m_name = path.AfterLast(wxT('/'));
348 details->m_address = start;
349 details->m_length = (char *)end - (char *)start;
350
351 // try to extract the library version from its name
352 const size_t posExt = path.rfind(wxT(".so"));
353 if ( posExt != wxString::npos )
354 {
355 if ( path.c_str()[posExt + 3] == wxT('.') )
356 {
357 // assume "libfoo.so.x.y.z" case
358 details->m_version.assign(path, posExt + 4, wxString::npos);
359 }
360 else
361 {
362 size_t posDash = path.find_last_of(wxT('-'), posExt);
363 if ( posDash != wxString::npos )
364 {
365 // assume "libbar-x.y.z.so" case
366 posDash++;
367 details->m_version.assign(path, posDash, posExt - posDash);
368 }
369 }
370 }
371
372 return details;
373 }
374 };
375
376 /* static */
377 wxDynamicLibraryDetailsArray wxDynamicLibrary::ListLoaded()
378 {
379 wxDynamicLibraryDetailsArray dlls;
380
381 #ifdef __LINUX__
382 // examine /proc/self/maps to find out what is loaded in our address space
383 wxFFile file(wxT("/proc/self/maps"));
384 if ( file.IsOpened() )
385 {
386 // details of the module currently being parsed
387 wxString pathCur;
388 void *startCur = NULL,
389 *endCur = NULL;
390
391 char path[1024];
392 char buf[1024];
393 while ( fgets(buf, WXSIZEOF(buf), file.fp()) )
394 {
395 // format is: "start-end perm offset maj:min inode path", see proc(5)
396 void *start,
397 *end;
398 switch ( sscanf(buf, "%p-%p %*4s %*p %*02x:%*02x %*d %1024s\n",
399 &start, &end, path) )
400 {
401 case 2:
402 // there may be no path column
403 path[0] = '\0';
404 break;
405
406 case 3:
407 // nothing to do, read everything we wanted
408 break;
409
410 default:
411 // chop '\n'
412 buf[strlen(buf) - 1] = '\0';
413 wxLogDebug(wxT("Failed to parse line \"%s\" in /proc/self/maps."),
414 buf);
415 continue;
416 }
417
418 wxASSERT_MSG( start >= endCur,
419 wxT("overlapping regions in /proc/self/maps?") );
420
421 wxString pathNew = wxString::FromAscii(path);
422 if ( pathCur.empty() )
423 {
424 // new module start
425 pathCur = pathNew;
426 startCur = start;
427 endCur = end;
428 }
429 else if ( pathCur == pathNew && endCur == end )
430 {
431 // continuation of the same module in the address space
432 endCur = end;
433 }
434 else // end of the current module
435 {
436 dlls.Add(wxDynamicLibraryDetailsCreator::New(startCur,
437 endCur,
438 pathCur));
439 pathCur.clear();
440 }
441 }
442 }
443 #endif // __LINUX__
444
445 return dlls;
446 }
447
448 #endif // wxUSE_DYNLIB_CLASS
449