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