]> git.saurik.com Git - wxWidgets.git/blob - src/common/dynlib.cpp
dlopen fixes
[wxWidgets.git] / src / common / dynlib.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: dynlib.cpp
3 // Purpose: Dynamic library management
4 // Author: Guilhem Lavaux
5 // Modified by:
6 // Created: 20/07/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Guilhem Lavaux
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "dynlib.h"
22 #endif
23
24 #include "wx/wxprec.h"
25
26 #ifdef __BORLANDC__
27 #pragma hdrstop
28 #endif //__BORLANDC__
29
30 #include "wx/dynlib.h"
31 #include "wx/filefn.h"
32 #include "wx/intl.h"
33 #include "wx/log.h"
34
35 // ----------------------------------------------------------------------------
36 // conditional compilation
37 // ----------------------------------------------------------------------------
38
39 #if defined(HAVE_DLOPEN)
40 #define wxDllOpen(lib) dlopen(lib, RTLD_LAZY)
41 #define wxDllGetSymbol(handle, name) dlsym(handle, (char *)name)
42 #define wxDllClose dlclose
43 #elif defined(HAVE_SHLLOAD)
44 #define wxDllOpen(lib) shl_open(lib, BIND_DEFERRED, 0)
45 #define wxDllClose shl_unload
46
47 static inline void *wxDllGetSymbol(shl_t *handle, const char *name)
48 {
49 void *sym;
50 if ( shl_findsym(handle, name, TYPE_UNDEFINED, &sym) == 0 )
51 return sym;
52 else
53 return (void *)0;
54 }
55 #elif defined(__WINDOWS__)
56 #define wxDllOpen(lib) ::LoadLibrary(lib)
57 #define wxDllGetSymbol(handle, name) ::GetProcAddress(handle, name)
58 #define wxDllClose ::FreeLibrary
59 #else
60 #error "Don't know how to load shared libraries on this platform."
61 #endif // OS
62
63 // ---------------------------------------------------------------------------
64 // Global variables
65 // ---------------------------------------------------------------------------
66
67 wxLibraries wxTheLibraries;
68
69 // ----------------------------------------------------------------------------
70 // private functions
71 // ----------------------------------------------------------------------------
72
73 // construct the full name from the base shared object name: adds a .dll
74 // suffix under Windows or .so under Unix
75 static wxString ConstructLibraryName(const wxString& basename)
76 {
77 wxString fullname(basename);
78
79 #if defined(__UNIX__)
80 fullname << ".so";
81 #elif defined(__WINDOWS__)
82 fullname << ".dll";
83 #endif
84
85 return fullname;
86 }
87
88 // ============================================================================
89 // implementation
90 // ============================================================================
91
92 // ---------------------------------------------------------------------------
93 // wxLibrary (one instance per dynamic library)
94 // ---------------------------------------------------------------------------
95
96 wxLibrary::wxLibrary(wxDllType handle)
97 {
98 typedef wxClassInfo *(*t_get_first)(void);
99 t_get_first get_first;
100
101 m_handle = handle;
102
103 // Some system may use a local heap for library.
104 get_first = (t_get_first)GetSymbol("wxGetClassFirst");
105 // It is a wxWindows DLL.
106 if (get_first)
107 PrepareClasses(get_first());
108 }
109
110 wxLibrary::~wxLibrary()
111 {
112 if ( m_handle )
113 {
114 wxDllClose(m_handle);
115 }
116 }
117
118 wxObject *wxLibrary::CreateObject(const wxString& name)
119 {
120 wxClassInfo *info = (wxClassInfo *)classTable.Get(name);
121
122 if (!info)
123 return NULL;
124
125 return info->CreateObject();
126 }
127
128 void wxLibrary::PrepareClasses(wxClassInfo *first)
129 {
130 // Index all class infos by their class name
131 wxClassInfo *info = first;
132 while (info)
133 {
134 if (info->m_className)
135 classTable.Put(info->m_className, (wxObject *)info);
136 info = info->GetNext();
137 }
138
139 // Set base pointers for each wxClassInfo
140 info = first;
141 while (info)
142 {
143 if (info->GetBaseClassName1())
144 info->m_baseInfo1 = (wxClassInfo *)classTable.Get(info->GetBaseClassName1());
145 if (info->GetBaseClassName2())
146 info->m_baseInfo2 = (wxClassInfo *)classTable.Get(info->GetBaseClassName2());
147 info = info->m_next;
148 }
149 }
150
151 void *wxLibrary::GetSymbol(const wxString& symbname)
152 {
153 void *symbol = NULL; // return value
154
155 #if defined( __WXMAC__ )
156 Ptr symAddress ;
157 CFragSymbolClass symClass ;
158 Str255 symName ;
159
160 strcpy( (char*) symName , symbname ) ;
161 c2pstr( (char*) symName ) ;
162
163 if ( FindSymbol( m_handle , symName , &symAddress , &symClass ) == noErr )
164 {
165 symbol = (void *)symAddress ;
166 }
167 #else
168 // VZ: hmm... why is WXSTRINGCAST needed? if it's really modified, we
169 // should make a copy of it
170 symbol = wxDllGetSymbol(m_handle, WXSTRINGCAST symbname);
171 #endif
172
173 if ( !symbol )
174 {
175 wxLogSysError(_("Couldn't find symbol '%s' in a dynamic library"),
176 symbname.c_str());
177 }
178
179 return symbol;
180 }
181
182 // ---------------------------------------------------------------------------
183 // wxLibraries (only one instance should normally exist)
184 // ---------------------------------------------------------------------------
185
186 wxLibraries::wxLibraries()
187 {
188 }
189
190 wxLibraries::~wxLibraries()
191 {
192 wxNode *node = m_loaded.First();
193
194 while (node) {
195 wxLibrary *lib = (wxLibrary *)node->Data();
196 delete lib;
197
198 node = node->Next();
199 }
200 }
201
202 wxLibrary *wxLibraries::LoadLibrary(const wxString& name)
203 {
204 wxNode *node;
205 wxLibrary *lib;
206 wxClassInfo *old_sm_first;
207
208 if ( (node = m_loaded.Find(name.GetData())) )
209 return ((wxLibrary *)node->Data());
210
211 // If DLL shares data, this is necessary.
212 old_sm_first = wxClassInfo::sm_first;
213 wxClassInfo::sm_first = NULL;
214
215 wxString lib_name = ConstructLibraryName(name);
216
217 #if defined(__UNIX__)
218 // TODO use LD_LIBRARY_PATH!
219 lib_name.Prepend("/lib");
220 #endif // __UNIX__
221
222 wxDllType handle ;
223
224 #if defined(__WXMAC__)
225 FSSpec myFSSpec ;
226 Ptr myMainAddr ;
227 Str255 myErrName ;
228
229 wxMacPathToFSSpec( lib_name , &myFSSpec ) ;
230 if (GetDiskFragment( &myFSSpec , 0 , kCFragGoesToEOF , "\p" , kPrivateCFragCopy , &handle , &myMainAddr ,
231 myErrName ) != noErr )
232 {
233 p2cstr( myErrName ) ;
234 wxASSERT_MSG( 1 , (char*)myErrName ) ;
235 return NULL ;
236 }
237 #else // !Mac
238 handle = wxDllOpen(lib_name);
239 #endif // OS
240
241 if ( !handle )
242 {
243 wxLogSysError(_("Failed to load shared library '%s'"),
244 lib_name.c_str());
245
246 return NULL;
247 }
248
249 lib = new wxLibrary(handle);
250
251 wxClassInfo::sm_first = old_sm_first;
252
253 m_loaded.Append(name.GetData(), lib);
254
255 return lib;
256 }
257
258 wxObject *wxLibraries::CreateObject(const wxString& path)
259 {
260 wxNode *node = m_loaded.First();
261 wxObject *obj;
262
263 while (node) {
264 obj = ((wxLibrary *)node->Data())->CreateObject(path);
265 if (obj)
266 return obj;
267
268 node = node->Next();
269 }
270 return NULL;
271 }