]> git.saurik.com Git - wxWidgets.git/blame - src/unix/dlunix.cpp
Tried to restore wxPopupTransientWindow functionality
[wxWidgets.git] / src / unix / dlunix.cpp
CommitLineData
da55d064
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Name: 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// RCS-ID: $Id$
8// Copyright: (c) 2000-2005 Vadim Zeitlin <vadim@wxwindows.org>
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20#include "wx/wxprec.h"
21
22#ifdef __BORLANDC__
23 #pragma hdrstop
24#endif
25
26#if wxUSE_DYNLIB_CLASS
27
28#include "wx/dynlib.h"
297ebe6b 29#include "wx/ffile.h"
da55d064 30
ac6b7b3c
VZ
31#ifndef WX_PRECOMP
32 #include "wx/intl.h"
33 #include "wx/log.h"
34#endif
35
d8e342b7
DE
36#if defined(__DARWIN__)
37 #include <dlfcn.h>
38#endif
39
da55d064
VZ
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!"
44#endif
45
46// ----------------------------------------------------------------------------
47// constants
48// ----------------------------------------------------------------------------
49
50// standard shared libraries extensions for different Unix versions
51#if defined(__HPUX__)
52 const wxChar *wxDynamicLibrary::ms_dllext = _T(".sl");
53#elif defined(__DARWIN__)
54 const wxChar *wxDynamicLibrary::ms_dllext = _T(".bundle");
55#else
56 const wxChar *wxDynamicLibrary::ms_dllext = _T(".so");
57#endif
58
59// ============================================================================
60// wxDynamicLibrary implementation
61// ============================================================================
62
63// ----------------------------------------------------------------------------
64// dlxxx() emulation for Darwin
65// ----------------------------------------------------------------------------
66
67#if defined(__DARWIN__)
68// ---------------------------------------------------------------------------
69// For Darwin/Mac OS X
70// supply the sun style dlopen functions in terms of Darwin NS*
71// ---------------------------------------------------------------------------
72
73/* Porting notes:
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*.
78 */
79
80#include <stdio.h>
81#include <mach-o/dyld.h>
82
83static char dl_last_error[1024];
84
85static
86void TranslateError(const char *path, int number)
87{
88 unsigned int index;
89 static char *OFIErrorStrings[] =
90 {
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",
98 };
99#define NUM_OFI_ERRORS (sizeof(OFIErrorStrings) / sizeof(OFIErrorStrings[0]))
100
101 index = number;
102 if (index > NUM_OFI_ERRORS - 1) {
103 index = NUM_OFI_ERRORS - 1;
104 }
105 sprintf(dl_last_error, OFIErrorStrings[index], path, number);
106}
107
108const char *dlerror()
109{
110 return dl_last_error;
111}
112
113void *dlopen(const char *path, int WXUNUSED(mode) /* mode is ignored */)
114{
115 NSObjectFileImage ofile;
116 NSModule handle = NULL;
117
118 int dyld_result = NSCreateObjectFileImageFromFile(path, &ofile);
119 if ( dyld_result != NSObjectFileImageSuccess )
120 {
121 handle = NULL;
122 }
123 else
124 {
125 handle = NSLinkModule
126 (
127 ofile,
128 path,
129 NSLINKMODULE_OPTION_BINDNOW |
130 NSLINKMODULE_OPTION_RETURN_ON_ERROR
131 );
132 }
133
134 if ( !handle )
135 TranslateError(path, dyld_result);
136
137 return handle;
138}
139
140int dlclose(void *handle)
141{
142 NSUnLinkModule( handle, NSUNLINKMODULE_OPTION_NONE);
143 return 0;
144}
145
146void *dlsym(void *handle, const char *symbol)
147{
148 // as on many other systems, C symbols have prepended underscores under
149 // Darwin but unlike the normal dlopen(), NSLookupSymbolInModule() is not
150 // aware of this
151 wxCharBuffer buf(strlen(symbol) + 1);
152 char *p = buf.data();
153 p[0] = '_';
154 strcpy(p + 1, symbol);
155
156 NSSymbol nsSymbol = NSLookupSymbolInModule( handle, p );
157 return nsSymbol ? NSAddressOfSymbol(nsSymbol) : NULL;
158}
159
160#endif // defined(__DARWIN__)
161
162// ----------------------------------------------------------------------------
163// loading/unloading DLLs
164// ----------------------------------------------------------------------------
165
166wxDllType wxDynamicLibrary::GetProgramHandle()
167{
168#ifdef USE_POSIX_DL_FUNCS
169 return dlopen(0, RTLD_LAZY);
170#else
171 return PROG_HANDLE;
172#endif
173}
174
175/* static */
176wxDllType wxDynamicLibrary::RawLoad(const wxString& libname, int flags)
177{
178 wxASSERT_MSG( (flags & wxDL_NOW) == 0,
179 _T("wxDL_LAZY and wxDL_NOW are mutually exclusive.") );
180
181#ifdef USE_POSIX_DL_FUNCS
182 int rtldFlags = 0;
183
184#ifdef RTLD_LAZY
185 if ( flags & wxDL_LAZY )
186 {
187 rtldFlags |= RTLD_LAZY;
188 }
189#endif
190#ifdef RTLD_NOW
191 if ( flags & wxDL_NOW )
192 {
193 rtldFlags |= RTLD_NOW;
194 }
195#endif
196#ifdef RTLD_GLOBAL
197 if ( flags & wxDL_GLOBAL )
198 {
199 rtldFlags |= RTLD_GLOBAL;
200 }
201#endif
202
203 return dlopen(libname.fn_str(), rtldFlags);
204#else // !USE_POSIX_DL_FUNCS
205 int shlFlags = 0;
206
207 if ( flags & wxDL_LAZY )
208 {
209 shlFlags |= BIND_DEFERRED;
210 }
211 else if ( flags & wxDL_NOW )
212 {
213 shlFlags |= BIND_IMMEDIATE;
214 }
215
216 return shl_load(libname.fn_str(), shlFlags, 0);
217#endif // USE_POSIX_DL_FUNCS/!USE_POSIX_DL_FUNCS
218}
219
220/* static */
221void wxDynamicLibrary::Unload(wxDllType handle)
222{
223#ifdef wxHAVE_DYNLIB_ERROR
224 int rc =
225#endif
226
227#ifdef USE_POSIX_DL_FUNCS
228 dlclose(handle);
229#else // !USE_POSIX_DL_FUNCS
230 shl_unload(handle);
231#endif // USE_POSIX_DL_FUNCS/!USE_POSIX_DL_FUNCS
232
d8e342b7 233#if defined(USE_POSIX_DL_FUNCS) && defined(wxHAVE_DYNLIB_ERROR)
da55d064
VZ
234 if ( rc != 0 )
235 Error();
236#endif
237}
238
239/* static */
240void *wxDynamicLibrary::RawGetSymbol(wxDllType handle, const wxString& name)
241{
242 void *symbol;
243
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 )
250 symbol = 0;
251#endif // USE_POSIX_DL_FUNCS/!USE_POSIX_DL_FUNCS
252
253 return symbol;
254}
255
297ebe6b
VZ
256// ----------------------------------------------------------------------------
257// error handling
258// ----------------------------------------------------------------------------
259
da55d064
VZ
260#ifdef wxHAVE_DYNLIB_ERROR
261
262/* static */
263void wxDynamicLibrary::Error()
264{
265#if wxUSE_UNICODE
266 wxWCharBuffer buffer = wxConvLocal.cMB2WC( dlerror() );
267 const wxChar *err = buffer;
268#else
269 const wxChar *err = dlerror();
270#endif
271
272 wxLogError(wxT("%s"), err ? err : _("Unknown dynamic library error"));
273}
274
275#endif // wxHAVE_DYNLIB_ERROR
276
297ebe6b
VZ
277// ----------------------------------------------------------------------------
278// listing loaded modules
279// ----------------------------------------------------------------------------
280
281// wxDynamicLibraryDetails declares this class as its friend, so put the code
282// initializing new details objects here
283class wxDynamicLibraryDetailsCreator
284{
285public:
286 // create a new wxDynamicLibraryDetails from the given data
287 static wxDynamicLibraryDetails *
288 New(unsigned long start, unsigned long end, const wxString& path)
289 {
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;
295
296 // try to extract the library version from its name
297 const size_t posExt = path.rfind(_T(".so"));
298 if ( posExt != wxString::npos )
299 {
300 if ( path.c_str()[posExt + 3] == _T('.') )
301 {
302 // assume "libfoo.so.x.y.z" case
303 details->m_version.assign(path, posExt + 4, wxString::npos);
304 }
305 else
306 {
307 size_t posDash = path.find_last_of(_T('-'), posExt);
308 if ( posDash != wxString::npos )
309 {
310 // assume "libbar-x.y.z.so" case
311 posDash++;
312 details->m_version.assign(path, posDash, posExt - posDash);
313 }
314 }
315 }
316
317 return details;
318 }
319};
320
321/* static */
322wxDynamicLibraryDetailsArray wxDynamicLibrary::ListLoaded()
323{
324 wxDynamicLibraryDetailsArray dlls;
325
326#ifdef __LINUX__
327 // examine /proc/self/maps to find out what is loaded in our address space
5e4f63c8 328 wxFFile file(_T("/proc/self/maps"));
297ebe6b
VZ
329 if ( file.IsOpened() )
330 {
331 // details of the module currently being parsed
332 wxString pathCur;
333 unsigned long startCur,
334 endCur;
335
336 char path[1024];
337 char buf[1024];
338 while ( fgets(buf, WXSIZEOF(buf), file.fp()) )
339 {
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) )
344 {
345 case 2:
346 // there may be no path column
347 path[0] = '\0';
348 break;
349
350 case 3:
351 // nothing to do, read everything we wanted
352 break;
353
354 default:
355 // chop '\n'
356 buf[strlen(buf) - 1] = '\0';
357 wxLogDebug(_T("Failed to parse line \"%s\" in /proc/self/maps."),
358 buf);
359 continue;
360 }
361
362 wxString pathNew = wxString::FromAscii(path);
363 if ( pathCur.empty() )
364 {
365 // new module start
366 pathCur = pathNew;
367 startCur = start;
368 endCur = end;
369 }
370 else if ( pathCur == pathNew )
371 {
372 // continuation of the same module
373 wxASSERT_MSG( start == endCur, _T("hole in /proc/self/maps?") );
374 endCur = end;
375 }
376 else // end of the current module
377 {
378 dlls.Add(wxDynamicLibraryDetailsCreator::New(startCur,
379 endCur,
380 pathCur));
381 pathCur.clear();
382 }
383 }
384 }
385#endif // __LINUX__
386
387 return dlls;
388}
389
da55d064
VZ
390#endif // wxUSE_DYNLIB_CLASS
391