summaryrefslogtreecommitdiff
path: root/src/OSD/OSD_SharedLibrary.cxx
blob: 1a7ec0fcfa640a80c388d971f355194797fa8f8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346

#ifdef HAVE_CONFIG_H
# include <oce-config.h>
#endif

#ifndef WNT

#include <OSD_LoadMode.hxx>
#include <OSD_SharedLibrary.ixx>
#include <OSD_Function.hxx>

#include <stdio.h>

#ifdef HAVE_MALLOC_H
# include <malloc.h>
#endif

#ifdef __some_crappy_system__
/*
 * Values for 'mode' argument in dlopen().
 *
*/
#define RTLD_LAZY		1
#define RTLD_NOW	        2
/*
 * Interface to rld via unsupported __rld_libdl_interface() call.
 *
 */
#define _LIBDL_RLD_DLOPEN	1
#define _LIBDL_RLD_DLCLOSE	2
#define _LIBDL_RLD_DLSYM	3
#define _LIBDL_RLD_DLERROR	4
extern "C" {void	*dlopen(char *path, int mode);}
extern "C" {void*   dlsym   (       void*  handle,char* name);}
extern "C" {int     dlclose (       void  *handle  );}
extern "C" {void    *dlerror (void);}
#endif

#ifdef HAVE_DLFCN_H
# include <dlfcn.h>
#endif

#ifdef HAVE_DL_H
# include <dl.h>
#endif

extern "C" {size_t  strlen  (const  char*  s      );}


#define BAD(X)  ((X) == NULL)

// ----------------------------------------------------------------
//
// Create and initialize a shared library object to NULL
//
// ----------------------------------------------------------------
OSD_SharedLibrary::OSD_SharedLibrary():myHandle(NULL),myName(NULL){
}
// ----------------------------------------------------------------
//
// Create and initialize a shared library object to the
// name given as argument
//
// ----------------------------------------------------------------
OSD_SharedLibrary::OSD_SharedLibrary(const Standard_CString aName):myHandle(NULL) 
{
  if (aName != NULL) {
    myName = new char [(strlen (aName) + 1 )];
    strcpy (myName,aName);
  }
}
// ----------------------------------------------------------------
//
// Name: Returns the shared library name
//
// ----------------------------------------------------------------
Standard_CString  OSD_SharedLibrary::Name() const {
  return myName; 
}
// ----------------------------------------------------------------
//
// SetName: Sets a name to a shared library object
//
// ----------------------------------------------------------------
void  OSD_SharedLibrary::SetName(const Standard_CString aName)  {
  if (aName != NULL) {
    myName = new char [(strlen (aName) + 1 )];
    strcpy (myName,aName);
  }
}
// ----------------------------------------------------------------
//
// DlOpen:   The dlopen function provides an interface to the dynamic 
// library loader to allow shared libraries to be loaded and called at
// runtime.  
// The dlopen function attempts to load filename, in the address space 
// of the process, resolving symbols as appropriate.  Any libraries that      
// filename depends upon are also loaded.
//
// If mode is RTLD_LAZY, then the runtime loader does symbol resolution 
// only as needed.  Typically, this means that the first call	
// to a function in the newly loaded library will cause the resolution 
// of the address of that function to occur.  
//
// If mode is RTLD_NOW, then the runtime loader must do all
// symbol binding during the dlopen call.  
// The dlopen function returns a handle that is used by dlsym or 
// dlclose call.  If there is an error, a NULLpointer is returned.
//
// If a NULL filename is specified, dlopen returns a handle for the main      
// executable, which allows access to dynamic symbols in the running program.
//
// ----------------------------------------------------------------
Standard_Boolean  OSD_SharedLibrary::DlOpen(const OSD_LoadMode aMode ) {

#ifdef HAVE_DL_H
if (aMode == OSD_RTLD_LAZY){
//  myHandle = cxxshl_load(myName, BIND_FIRST | BIND_TOGETHER | BIND_DEFERRED | BIND_VERBOSE | DYNAMIC_PATH, 0L);
  myHandle = shl_load(myName, BIND_FIRST | BIND_TOGETHER | BIND_DEFERRED | BIND_VERBOSE | DYNAMIC_PATH, 0L);
}
else if (aMode == OSD_RTLD_NOW){
//  myHandle = cxxshl_load(myName, BIND_FIRST | BIND_TOGETHER | BIND_IMMEDIATE | BIND_VERBOSE | DYNAMIC_PATH, 0L);
  myHandle = shl_load(myName, BIND_FIRST | BIND_TOGETHER | BIND_IMMEDIATE | BIND_VERBOSE | DYNAMIC_PATH, 0L);

}
#else
if (aMode == OSD_RTLD_LAZY){
  myHandle = dlopen (myName,RTLD_LAZY);
}
else if (aMode == OSD_RTLD_NOW){
  myHandle = dlopen (myName,RTLD_NOW);
}
#endif

if (!BAD(myHandle)){
  return Standard_True;
 }
else {
  return Standard_False;
 }
}
// ----------------------------------------------------------------
//
// DlSymb: The dlsym function returns the address of the	
// symbol name found in the shared library corresponding to handle.  
// If the symbol is not	found, a NULL
// pointer is returned.
//
// ----------------------------------------------------------------
OSD_Function  OSD_SharedLibrary::DlSymb(const Standard_CString aName )const{

#ifndef HAVE_DL_H
void (*fp)();
fp =  (void (*)()) dlsym (myHandle,aName);
if (!BAD(fp)){
  return (OSD_Function)fp;
 }
else {
  return (OSD_Function)NULL;
 }
#else
  void *adr_get = NULL;
//  shl_t handlesym=0 ;

  errno = 0 ;
  //  if (  shl_findsym( &handlesym,aName,TYPE_PROCEDURE,&adr_get) == -1 ) {
  if ( shl_findsym((shl_t *)&myHandle,aName,TYPE_PROCEDURE,&adr_get) == -1 ) {
    if ( errno != 0 )
      perror("OSD_SharedLibrary : shl_findsym perror : ") ;
    return (OSD_Function)NULL;
  }
  else return (OSD_Function) adr_get;
#endif

}
// ----------------------------------------------------------------
//
//DlClose: The dlclose function deallocates the address space for the library
//corresponding	to handle.  If any user	function continues to call a symbol
//resolved in the address space	of a library that has been since been deallo-
//cated	by dlclose, the	results	are undefined.
//
// ----------------------------------------------------------------
void OSD_SharedLibrary::DlClose()const{

#ifndef HAVE_DL_H
 dlclose(myHandle);
#else
 shl_unload((shl_t)myHandle);
#endif

}
// ----------------------------------------------------------------
//
// DlError:  returns a string	describing the last error that
// occurred from a call to dlopen, dlclose or dlsym.
//
// ----------------------------------------------------------------
Standard_CString OSD_SharedLibrary::DlError()const{
#ifndef HAVE_DL_H
return (char*) dlerror();
#else
perror("shl_load, shl_findsym, or shl_unload : perror : ") ;
return (char*) errno;
#endif
}
// ----------------------------------------------------------------------------
// Destroy
// ----------------------------------------------------------------------------
void OSD_SharedLibrary::Destroy() {
  if (myName != NULL) {
     delete [] myName;
     myName = NULL;
     myHandle = NULL;
  }
}

#else

//------------------------------------------------------------------------
//-------------------  Windows NT sources for OSD_SharedLibrary ----------
//------------------------------------------------------------------------

//it is important to define STRICT and enforce including <windows.h> before
//Standard_Macro.hxx undefines it and includes <windows.h> causing compilation errors
#ifndef STRICT
#define STRICT
#endif
#include <windows.h>

#include <OSD_SharedLibrary.ixx>

#include <OSD_Path.hxx>

#include <TCollection_AsciiString.hxx>


static DWORD              lastDLLError;
static Standard_Character errMsg[ 1024 ];

OSD_SharedLibrary :: OSD_SharedLibrary () {

 myHandle = NULL;
 myName   = NULL;

}  // end constructor ( 1 )

OSD_SharedLibrary :: OSD_SharedLibrary ( const Standard_CString aFilename ) {

 myHandle = NULL;
 myName   = NULL;

 SetName ( aFilename );

}  // end constructro ( 2 )

void OSD_SharedLibrary :: SetName ( const Standard_CString aName ) {

 OSD_Path                path ( aName );
 TCollection_AsciiString name ( aName );

 if ( myName != NULL )

  delete [] myName;

 myName = new Standard_Character[ strlen ( aName ) + 1 ];

 strcpy ( myName, aName );

 name = path.Name ();
 name.AssignCat (  path.Extension ()  );

 myHandle = GetModuleHandle (  name.ToCString ()  );

}  // end OSD_SharedLibrary :: SetName

Standard_CString OSD_SharedLibrary :: Name () const {

 return myName;

}  // end OSD_SharedLibrary :: Name

Standard_Boolean OSD_SharedLibrary :: DlOpen ( const OSD_LoadMode Mode ) {

 Standard_Boolean retVal = Standard_True;

 if (  (  myHandle ) == NULL &&
       (  myHandle = ( HINSTANCE )LoadLibraryEx (
                                   myName, NULL, LOAD_WITH_ALTERED_SEARCH_PATH
                                  )  ) == NULL
 ) {
 
  lastDLLError = GetLastError ();
  retVal       = Standard_False;
 
 }  // end if

 return retVal;

}  // end OSD_SharedLibrary :: DlOpen

OSD_Function OSD_SharedLibrary :: DlSymb ( const Standard_CString Name ) const {

 OSD_Function func = ( OSD_Function )GetProcAddress (  ( HMODULE )myHandle, Name  );

 if ( func == NULL ) {
#ifdef __BORLANDC__ //borland decorates exported C style routines with '_'
  Standard_Character* myAlternativeName = new Standard_Character[ strlen ( Name ) + 1 + 1 ];
  static Standard_Character* myAddition = "_";
  strcpy(myAlternativeName, myAddition);
  strcat(myAlternativeName, Name);
  func = ( OSD_Function )GetProcAddress (  ( HMODULE )myHandle, myAlternativeName  );
  delete [] myAlternativeName;
 if ( func == NULL )
#endif
  lastDLLError = GetLastError ();
 }

 return func;

}  // end OSD_SharedLibrary :: DlSymb

void OSD_SharedLibrary :: DlClose () const {

 FreeLibrary (  ( HMODULE )myHandle  );

}  // end OSD_SharedLibrary :: DlClose

Standard_CString OSD_SharedLibrary :: DlError () const {

 FormatMessage (
  FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ARGUMENT_ARRAY,
  0, lastDLLError, MAKELANGID( LANG_NEUTRAL, SUBLANG_NEUTRAL ), errMsg, 1024, ( va_list* )&myName
 );

 return errMsg;

}  // end OSD_SharedLibrary :: DlError

void OSD_SharedLibrary :: Destroy () {

 if ( myName != NULL ) delete [] myName;

}  // end OSD_SharedLibrary :: Destroy

#endif