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