| 1 | // | 
|---|
| 2 | // Gestionnaire de lien dynamique   -   R. Ansari 12/98 | 
|---|
| 3 | // LAL (Orsay) / IN2P3-CNRS  DAPNIA/SPP (Saclay) / CEA | 
|---|
| 4 | // | 
|---|
| 5 | #include <machdefs.h> | 
|---|
| 6 |  | 
|---|
| 7 | #include "pdlmgr.h" | 
|---|
| 8 | #include <stdio.h> | 
|---|
| 9 | #include <stdlib.h> | 
|---|
| 10 |  | 
|---|
| 11 | #include <iostream.h> | 
|---|
| 12 |  | 
|---|
| 13 |  | 
|---|
| 14 | // Extension de noms de fichiers Shared libs | 
|---|
| 15 | static const char* sofext = ".so"; | 
|---|
| 16 | static const char* sofext_HPUX = ".sl"; | 
|---|
| 17 | // static const char* sofext_Darwin = ".dylib"; pas necessaire - Reza 02/2002 | 
|---|
| 18 |  | 
|---|
| 19 | // Variables et methodes static | 
|---|
| 20 | int PDynLinkMgr::numSO = 0; | 
|---|
| 21 | string*  PDynLinkMgr::tmpDir = NULL; | 
|---|
| 22 |  | 
|---|
| 23 | /*! | 
|---|
| 24 | \class SOPHYA::PDynLinkMgr | 
|---|
| 25 | \ingroup SysTools | 
|---|
| 26 | This classes handles the run-time operations related to using shared | 
|---|
| 27 | libraries. The present version has been adapted for different Unix | 
|---|
| 28 | flavours (Linux, Compaq/Digital Unix, SGI IRIX, IBM AIX, Sun Solaris, | 
|---|
| 29 | MacOS X/Darwin). | 
|---|
| 30 | The example here shows the linking of shared library named "mylib.so" | 
|---|
| 31 | containing a function \c double \c myfunction(double x). | 
|---|
| 32 | \code | 
|---|
| 33 | #include "pdlmgr.h" | 
|---|
| 34 | typedef double (* AFunctionOfX) (double x); | 
|---|
| 35 | { | 
|---|
| 36 | // ... | 
|---|
| 37 | string soname = "mylib.so"; | 
|---|
| 38 | string funcname = "myfunction"; | 
|---|
| 39 | PDynLinkMgr dyl(son); | 
|---|
| 40 | AFunctionOfX f = (AFunctionOfX)dyl.GetFunction(funcname); | 
|---|
| 41 | double x = 3.1425; | 
|---|
| 42 | if (f != NULL) | 
|---|
| 43 | cout << " X= " << x << " myfunction(x)=" << f(x) << endl; | 
|---|
| 44 | // ... | 
|---|
| 45 | } | 
|---|
| 46 | \endcode | 
|---|
| 47 | */ | 
|---|
| 48 |  | 
|---|
| 49 | /* --Methode-Static-- */ | 
|---|
| 50 | /*! Sets the path for a temporary space where shared libraries are copied. | 
|---|
| 51 | The path is appended to \b LD_LIBRARY_PATH | 
|---|
| 52 | */ | 
|---|
| 53 | void PDynLinkMgr::SetTmpDir(string const & path) | 
|---|
| 54 | { | 
|---|
| 55 | if ( (path.length() > 0) && (path[path.length()-1] != '/')  ) GetTmpDir() = path + '/'; | 
|---|
| 56 | else GetTmpDir() = path; | 
|---|
| 57 | #if defined(OSF1) || defined(Linux) || defined(SunOS) || defined(IRIX64) | 
|---|
| 58 | char* varenv=NULL; | 
|---|
| 59 | #if !defined(IRIX64) | 
|---|
| 60 | string cmd = "LD_LIBRARY_PATH="; | 
|---|
| 61 | varenv=getenv("LD_LIBRARY_PATH"); | 
|---|
| 62 | #else | 
|---|
| 63 | #ifdef SGI_ARCH64 | 
|---|
| 64 | string cmd = "LD_LIBRARYN32_PATH="; | 
|---|
| 65 | varenv=getenv("LD_LIBRARYN32_PATH"); | 
|---|
| 66 | #else | 
|---|
| 67 | string cmd = "LD_LIBRARYN64_PATH="; | 
|---|
| 68 | varenv=getenv("LD_LIBRARYN64_PATH"); | 
|---|
| 69 | #endif | 
|---|
| 70 | #endif | 
|---|
| 71 |  | 
|---|
| 72 | if (varenv == NULL) { | 
|---|
| 73 | cmd += '.'; | 
|---|
| 74 | if (path.length() > 0)  cmd += ':' + path; | 
|---|
| 75 | } | 
|---|
| 76 | else { | 
|---|
| 77 | if (varenv[0] != '.') cmd += ".:"; | 
|---|
| 78 | if (path.length() > 0)  cmd += path + ':'; | 
|---|
| 79 | cmd += varenv; | 
|---|
| 80 | putenv(const_cast<char *>(cmd.c_str())); | 
|---|
| 81 | } | 
|---|
| 82 | #elif defined(AIX) | 
|---|
| 83 | string cmd = "LIBPATH="; | 
|---|
| 84 | char* varenv=NULL; | 
|---|
| 85 | varenv=getenv("LIBPATH"); | 
|---|
| 86 | if (varenv == NULL) { | 
|---|
| 87 | cmd += '.'; | 
|---|
| 88 | if (path.length() > 0)  cmd += ':' + path; | 
|---|
| 89 | cmd += ":/usr/lib:/lib"; | 
|---|
| 90 | } | 
|---|
| 91 | else { | 
|---|
| 92 | if (varenv[0] != '.') cmd += ".:"; | 
|---|
| 93 | if (path.length() > 0)  cmd += path + ':'; | 
|---|
| 94 | cmd += varenv; | 
|---|
| 95 | putenv(const_cast<char *>(cmd.c_str())); | 
|---|
| 96 | } | 
|---|
| 97 |  | 
|---|
| 98 | #endif | 
|---|
| 99 | return; | 
|---|
| 100 | } | 
|---|
| 101 |  | 
|---|
| 102 | /* --Methode-Static-- */ | 
|---|
| 103 | /*! Returns the temporary space path */ | 
|---|
| 104 | string& PDynLinkMgr::GetTmpDir() | 
|---|
| 105 | { | 
|---|
| 106 | if (tmpDir == NULL) { | 
|---|
| 107 | tmpDir = new string(""); | 
|---|
| 108 | char* varenv; | 
|---|
| 109 | if ( (varenv=getenv("TMPDIR")) != NULL )  { | 
|---|
| 110 | *tmpDir = varenv; | 
|---|
| 111 | if ((*tmpDir)[tmpDir->length()-1] != '/') (*tmpDir) += '/'; | 
|---|
| 112 | } | 
|---|
| 113 | } | 
|---|
| 114 | return(*tmpDir); | 
|---|
| 115 | } | 
|---|
| 116 |  | 
|---|
| 117 | /* --Methode-Static-- */ | 
|---|
| 118 | /*! Compiles the C source file named \b fname and creates the | 
|---|
| 119 | corresponding shared library linking against the standard | 
|---|
| 120 | C library (-lc) and the math library (-lm). | 
|---|
| 121 | Returns a pointer to the created PDynLinkMgr object (by new). | 
|---|
| 122 | Returns the NULL pointer in case of errors. | 
|---|
| 123 | */ | 
|---|
| 124 | PDynLinkMgr* PDynLinkMgr::BuildFromCFile(string const & fname) | 
|---|
| 125 | { | 
|---|
| 126 | size_t l = fname.length(); | 
|---|
| 127 | if (l < 1)  return(NULL); | 
|---|
| 128 | string fnameobj = GetTmpDir()+"tmp_pdl.o"; | 
|---|
| 129 |  | 
|---|
| 130 | string cmd; | 
|---|
| 131 | int rc; | 
|---|
| 132 |  | 
|---|
| 133 | // Compilation du fichier | 
|---|
| 134 | #ifndef __mac__ | 
|---|
| 135 | #ifdef SGI_ARCH64 | 
|---|
| 136 | cmd = "cc -64 -c -o " + fnameobj + " " + fname; | 
|---|
| 137 | #else | 
|---|
| 138 | cmd = "cc -c -o " + fnameobj + " " + fname; | 
|---|
| 139 | #endif | 
|---|
| 140 | #else | 
|---|
| 141 | cmd = "Il faut compiler !!!" + fnameobj + " " + fname; | 
|---|
| 142 | #endif | 
|---|
| 143 | rc = system(cmd.c_str()); | 
|---|
| 144 | if (rc != 0)  { | 
|---|
| 145 | cerr << "PDynLinkMgr::BuildFromCFile() Error Rc(" << cmd <<")= "<< rc << endl; | 
|---|
| 146 | return(NULL); | 
|---|
| 147 | } | 
|---|
| 148 |  | 
|---|
| 149 | char buff[32]; | 
|---|
| 150 | numSO++; | 
|---|
| 151 | #ifndef HPUX | 
|---|
| 152 | sprintf(buff,"pdlmgr%d%s", numSO,sofext); | 
|---|
| 153 | #endif | 
|---|
| 154 | string fnameso = GetTmpDir()+buff; | 
|---|
| 155 |  | 
|---|
| 156 | // Creation du shared-lib | 
|---|
| 157 | #if defined(OSF1) | 
|---|
| 158 | cmd = "ld -shared -o " + fnameso + " -all " + fnameobj + " -none -lm -lc"; | 
|---|
| 159 | #elif defined(Linux) | 
|---|
| 160 | cmd = "ld -shared -o " + fnameso + " " + fnameobj + " -lm -lc"; | 
|---|
| 161 | #elif defined(SunOS) | 
|---|
| 162 | cmd = "ld -G -o " + fnameso + " " + fnameobj + " -lm -lc"; | 
|---|
| 163 | #elif defined(IRIX64) | 
|---|
| 164 | #ifdef SGI_ARCH64 | 
|---|
| 165 | cmd = "ld -64 -shared -o " + fnameso + " " + fnameobj + " -lm -lc"; | 
|---|
| 166 | #else | 
|---|
| 167 | cmd = "ld -shared -o " + fnameso + " " + fnameobj + " -lm -lc"; | 
|---|
| 168 | #endif | 
|---|
| 169 | #elif defined(AIX) | 
|---|
| 170 | cmd = "ld -G -bnogc -bexpall -bM:1L -o " + fnameso + " " + fnameobj; | 
|---|
| 171 | #elif defined(HPUX) | 
|---|
| 172 | cmd = "ld -b -o " + fnameso + " " + fnameobj + " -lm -lc"; | 
|---|
| 173 | #elif defined(Darwin) | 
|---|
| 174 | cmd = "cc -bundle -flat_namespace -undefined suppress -o " + fnameso + " " + fnameobj + " -lm  -lcc_dynamic -lSystem "; | 
|---|
| 175 | #else | 
|---|
| 176 | cmd = "ld -o " + fnameso + " " + fnameobj + " -lm -lc"; | 
|---|
| 177 | #endif | 
|---|
| 178 | rc = system(cmd.c_str()); | 
|---|
| 179 | if (rc != 0)  { | 
|---|
| 180 | cerr << "PDynLinkMgr::BuildFromCFile() Error Rc(" << cmd <<")= "<< rc << endl; | 
|---|
| 181 | return(NULL); | 
|---|
| 182 | } | 
|---|
| 183 | PDynLinkMgr* rdyn = new PDynLinkMgr(fnameso, false); | 
|---|
| 184 | rdyn->copy = true; | 
|---|
| 185 | return(rdyn); | 
|---|
| 186 |  | 
|---|
| 187 | } | 
|---|
| 188 |  | 
|---|
| 189 | /* --Methode-- */ | 
|---|
| 190 | /*! The constructor. | 
|---|
| 191 | \param soname : Name of the shared library. ".so" is appended | 
|---|
| 192 | to the name if no dot "." is found in the name. | 
|---|
| 193 | \param cp : if true, copies the shared library in the temporary space. | 
|---|
| 194 | */ | 
|---|
| 195 | PDynLinkMgr::PDynLinkMgr(string& soname, bool cp) | 
|---|
| 196 | { | 
|---|
| 197 | #ifndef Darwin | 
|---|
| 198 | dlhandle = NULL; | 
|---|
| 199 | #endif | 
|---|
| 200 | soName = ""; | 
|---|
| 201 |  | 
|---|
| 202 | if (soname.find_last_of(".") > soname.length()) | 
|---|
| 203 | #ifdef HPUX | 
|---|
| 204 | soname += sofext_HPUX; | 
|---|
| 205 | #else | 
|---|
| 206 | soname += sofext; | 
|---|
| 207 | #endif | 
|---|
| 208 |  | 
|---|
| 209 | string fnameso; | 
|---|
| 210 | if (cp) { | 
|---|
| 211 | numSO++; | 
|---|
| 212 | char buff[32]; | 
|---|
| 213 | #ifndef HPUX | 
|---|
| 214 | sprintf(buff,"pdlmgr%d%s", numSO,sofext); | 
|---|
| 215 | #elif defined(Darwin) | 
|---|
| 216 | sprintf(buff,"pdlmgr%d%s", numSO,sofext_Darwin); | 
|---|
| 217 | #else | 
|---|
| 218 | sprintf(buff,"pdlmgr%d%s", numSO,sofext_HPUX); | 
|---|
| 219 | #endif | 
|---|
| 220 | fnameso = GetTmpDir()+buff; | 
|---|
| 221 | string cmd = "cp " + soname + " " + fnameso; | 
|---|
| 222 | int rc = system(cmd.c_str()); | 
|---|
| 223 | if (rc != 0)  { | 
|---|
| 224 | cerr << "PDynLinkMgr::PDynLinkMgr() Error Rc(" << cmd <<")= "<< rc << endl; | 
|---|
| 225 | return; | 
|---|
| 226 | } | 
|---|
| 227 | } | 
|---|
| 228 | else fnameso = soname; | 
|---|
| 229 | copy = cp; | 
|---|
| 230 | soName = fnameso; | 
|---|
| 231 |  | 
|---|
| 232 | #if defined(HPUX) | 
|---|
| 233 | dlhandle = NULL; | 
|---|
| 234 | cerr << "PDynLinkMgr::PDynLinkMgr() Not yet available on HP-UX " << endl; | 
|---|
| 235 | return; | 
|---|
| 236 | #else | 
|---|
| 237 | dlhandle =  dlopen(fnameso.c_str(), RTLD_NOW); | 
|---|
| 238 | if (dlhandle == NULL) { | 
|---|
| 239 | cerr << "PDynLinkMgr::PDynLinkMgr(): Error opening SO " << fnameso | 
|---|
| 240 | << " (" << soname << ")" << endl; | 
|---|
| 241 | string sn = dlerror(); | 
|---|
| 242 | cerr << "Loader Error (dlerror()) :" << sn << endl; | 
|---|
| 243 | return; | 
|---|
| 244 | } | 
|---|
| 245 | #endif | 
|---|
| 246 | } | 
|---|
| 247 |  | 
|---|
| 248 | /* --Methode-- */ | 
|---|
| 249 | /*! Destructor. Closes the shared library. Removes the file if it had been | 
|---|
| 250 | copied in the temporary space, or generated by \b BuildFromCFile */ | 
|---|
| 251 | PDynLinkMgr::~PDynLinkMgr() | 
|---|
| 252 | { | 
|---|
| 253 | #if defined(HPUX) | 
|---|
| 254 | cerr << "PDynLinkMgr::~PDynLinkMgr() Not yet available on HP-UX " << endl; | 
|---|
| 255 | return; | 
|---|
| 256 | #else | 
|---|
| 257 | if (dlhandle) dlclose(dlhandle);    dlhandle = NULL; | 
|---|
| 258 | if (copy) { | 
|---|
| 259 | string cmd = "rm -f " + soName; | 
|---|
| 260 | system(cmd.c_str()); | 
|---|
| 261 | } | 
|---|
| 262 | #endif | 
|---|
| 263 | } | 
|---|
| 264 |  | 
|---|
| 265 | /* --Methode-- */ | 
|---|
| 266 | /*! Returns a handle to the function named \b funcname. | 
|---|
| 267 | Returns the NULL pointer in case of error          */ | 
|---|
| 268 | DlFunction PDynLinkMgr::GetFunction(string const & funcname) | 
|---|
| 269 | { | 
|---|
| 270 | DlFunction f = NULL; | 
|---|
| 271 | #if defined(HPUX) | 
|---|
| 272 | cerr << "PDynLinkMgr::GetFunction() Not yet available on HP-UX " << endl; | 
|---|
| 273 | return f; | 
|---|
| 274 | #else | 
|---|
| 275 | #if defined(Darwin) | 
|---|
| 276 | string funame = "_" + funcname; | 
|---|
| 277 | #else | 
|---|
| 278 | string const & funame = funcname; | 
|---|
| 279 | #endif | 
|---|
| 280 | if (dlhandle != NULL) | 
|---|
| 281 | f = (DlFunction)dlsym(dlhandle, funame.c_str()); | 
|---|
| 282 | if (f == NULL) cerr << "PDynLinkMgr::GetFunction(): Error linking " << funcname << endl; | 
|---|
| 283 | return(f); | 
|---|
| 284 | #endif | 
|---|
| 285 | } | 
|---|
| 286 |  | 
|---|
| 287 |  | 
|---|