qlibrary_unix.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "qlibrary_p.h"
00022
00023 #ifndef QT_NO_COMPONENT
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #if defined(Q_OS_HPUX)
00034
00035 #include <dl.h>
00036
00037 bool QLibraryPrivate::loadLibrary()
00038 {
00039 if ( pHnd )
00040 return TRUE;
00041
00042 QString filename = library->library();
00043
00044 pHnd = (void*)shl_load( filename.latin1(), BIND_DEFERRED | BIND_NONFATAL | DYNAMIC_PATH, 0 );
00045 #if defined(QT_DEBUG) || defined(QT_DEBUG_COMPONENT)
00046 if ( !pHnd )
00047 qDebug( "Failed to load library %s!", filename.latin1() );
00048 #endif
00049 return pHnd != 0;
00050 }
00051
00052 bool QLibraryPrivate::freeLibrary()
00053 {
00054 if ( !pHnd )
00055 return TRUE;
00056
00057 if ( !shl_unload( (shl_t)pHnd ) ) {
00058 pHnd = 0;
00059 return TRUE;
00060 }
00061 return FALSE;
00062 }
00063
00064 void* QLibraryPrivate::resolveSymbol( const char* symbol )
00065 {
00066 if ( !pHnd )
00067 return 0;
00068
00069 void* address = 0;
00070 if ( shl_findsym( (shl_t*)&pHnd, symbol, TYPE_UNDEFINED, address ) < 0 ) {
00071 #if defined(QT_DEBUG) || defined(QT_DEBUG_COMPONENT)
00072 qDebug( "Couldn't resolve symbol \"%s\"", symbol );
00073 #endif
00074 return 0;
00075 }
00076 return address;
00077 }
00078
00079 #elif defined(_NULL_LIB_)
00080
00081 bool QLibraryPrivate::loadLibrary()
00082 {
00083
00084 return FALSE;
00085 }
00086 bool QLibraryPrivate::freeLibrary()
00087 {
00088
00089 return FALSE;
00090 }
00091 void* QLibraryPrivate::resolveSymbol( const char* symbol )
00092 {
00093
00094 return FALSE;
00095 }
00096
00097 #elif defined(Q_OS_MACX)
00098
00099 #define ENUM_DYLD_BOOL
00100 enum DYLD_BOOL {
00101 DYLD_FALSE,
00102 DYLD_TRUE
00103 };
00104 #include <mach-o/dyld.h>
00105 typedef struct {
00106 NSObjectFileImage img;
00107 NSModule mod;
00108 } DyldLibDesc;
00109
00110 bool QLibraryPrivate::loadLibrary()
00111 {
00112
00113
00114 if ( pHnd )
00115 return TRUE;
00116
00117 QString filename = library->library();
00118
00119 NSObjectFileImage img = 0;
00120 NSModule mod = 0;
00121 NSObjectFileImageReturnCode ret = NSCreateObjectFileImageFromFile( filename.latin1() , &img );
00122 if ( ret != NSObjectFileImageSuccess ) {
00123 qWarning( "Error in NSCreateObjectFileImageFromFile(): %d; Filename: %s", ret, filename.latin1() );
00124 if (ret == NSObjectFileImageAccess) {
00125 qWarning ("(NSObjectFileImageAccess)" );
00126 }
00127 } else {
00128 mod = NSLinkModule(img, filename.latin1(), NSLINKMODULE_OPTION_BINDNOW |
00129 NSLINKMODULE_OPTION_PRIVATE |
00130 NSLINKMODULE_OPTION_RETURN_ON_ERROR);
00131 if (mod == 0) {
00132 qWarning( "Error in NSLinkModule()" );
00133 NSDestroyObjectFileImage(img);
00134 }
00135 }
00136 DyldLibDesc* desc = 0;
00137 if (img != 0 && mod != 0) {
00138 desc = new DyldLibDesc;
00139 desc->img = img;
00140 desc->mod = mod;
00141 }
00142 pHnd = desc;
00143 return pHnd != 0;
00144 }
00145
00146 bool QLibraryPrivate::freeLibrary()
00147 {
00148
00149
00150 if ( !pHnd )
00151 return TRUE;
00152
00153 DyldLibDesc* desc = (DyldLibDesc*) pHnd;
00154 NSModule mod = desc->mod;
00155 NSObjectFileImage img = desc->img;
00156 DYLD_BOOL success = NSUnLinkModule(mod, NSUNLINKMODULE_OPTION_NONE);
00157 if ( success ) {
00158 NSDestroyObjectFileImage(img);
00159 delete desc;
00160 pHnd = 0;
00161 }
00162 #if defined(QT_DEBUG) || defined(QT_DEBUG_COMPONENT)
00163 else {
00164 qWarning( "Error in NSUnLinkModule(): %d", ret );
00165 }
00166 #endif
00167 return pHnd == 0;
00168 }
00169
00170 void* QLibraryPrivate::resolveSymbol( const char* symbol )
00171 {
00172
00173
00174 if ( !pHnd )
00175 return 0;
00176
00177 DyldLibDesc* desc = (DyldLibDesc*) pHnd;
00178 NSSymbol sym = NSLookupSymbolInModule(desc->mod, symbol);
00179 void* address = 0;
00180 if (sym != 0) {
00181 address = NSAddressOfSymbol(sym);
00182 }
00183 #if defined(QT_DEBUG) || defined(QT_DEBUG_COMPONENT)
00184 if ( address == 0 )
00185 qWarning( "Cannot find symbol: %s", symbol );
00186 #endif
00187 return address;
00188 }
00189
00190 #else
00191
00192 #include <dlfcn.h>
00193
00194 bool QLibraryPrivate::loadLibrary()
00195 {
00196 if ( pHnd )
00197 return TRUE;
00198
00199 QString filename = library->library();
00200
00201 pHnd = dlopen( filename.latin1() , RTLD_LAZY );
00202 #if defined(QT_DEBUG) || defined(QT_DEBUG_COMPONENT)
00203 if ( !pHnd )
00204 qWarning( "%s", dlerror() );
00205 #endif
00206 return pHnd != 0;
00207 }
00208
00209 bool QLibraryPrivate::freeLibrary()
00210 {
00211 if ( !pHnd )
00212 return TRUE;
00213
00214 int ec = dlclose( pHnd );
00215 if ( !ec )
00216 pHnd = 0;
00217 #if defined(QT_DEBUG) || defined(QT_DEBUG_COMPONENT)
00218 else {
00219 const char* error = dlerror();
00220 if ( error )
00221 qWarning( "%s", error );
00222 }
00223 #endif
00224 return pHnd == 0;
00225 }
00226
00227 void* QLibraryPrivate::resolveSymbol( const char* f )
00228 {
00229 if ( !pHnd )
00230 return 0;
00231
00232 void* address = dlsym( pHnd, f );
00233 #if defined(QT_DEBUG) || defined(QT_DEBUG_COMPONENT)
00234 const char* error = dlerror();
00235 if ( error )
00236 qWarning( "%s", error );
00237 #endif
00238 return address;
00239 }
00240
00241 #endif // POSIX
00242
00243 #endif // QT_NO_COMPONENT
This file is part of the documentation for OPIE Version 1.5.5.