* `DynamicLibrary` loads and fetches function pointers for dynamic libraries * (.so, .dylib, etc). After the libray's function pointer is acquired, then you * call `get(symbol)` to retreive a pointer to an exported symbol. You need to * call `get___()` on the pointer to dereference it into its ac
(path, mode)
| 29 | */ |
| 30 | |
| 31 | function DynamicLibrary (path, mode) { |
| 32 | if (!(this instanceof DynamicLibrary)) { |
| 33 | return new DynamicLibrary(path, mode) |
| 34 | } |
| 35 | debug('new DynamicLibrary()', path, mode) |
| 36 | |
| 37 | if (null == mode) { |
| 38 | mode = DynamicLibrary.FLAGS.RTLD_LAZY |
| 39 | } |
| 40 | |
| 41 | this._handle = dlopen(path, mode) |
| 42 | assert(Buffer.isBuffer(this._handle), 'expected a Buffer instance to be returned from `dlopen()`') |
| 43 | |
| 44 | if (this._handle.isNull()) { |
| 45 | var err = this.error() |
| 46 | |
| 47 | // THIS CODE IS BASED ON GHC Trac ticket #2615 |
| 48 | // http://hackage.haskell.org/trac/ghc/attachment/ticket/2615 |
| 49 | |
| 50 | // On some systems (e.g., Gentoo Linux) dynamic files (e.g. libc.so) |
| 51 | // contain linker scripts rather than ELF-format object code. This |
| 52 | // code handles the situation by recognizing the real object code |
| 53 | // file name given in the linker script. |
| 54 | |
| 55 | // If an "invalid ELF header" error occurs, it is assumed that the |
| 56 | // .so file contains a linker script instead of ELF object code. |
| 57 | // In this case, the code looks for the GROUP ( ... ) linker |
| 58 | // directive. If one is found, the first file name inside the |
| 59 | // parentheses is treated as the name of a dynamic library and the |
| 60 | // code attempts to dlopen that file. If this is also unsuccessful, |
| 61 | // an error message is returned. |
| 62 | |
| 63 | // see if the error message is due to an invalid ELF header |
| 64 | var match |
| 65 | |
| 66 | if (match = err.match(/^(([^ \t()])+\.so([^ \t:()])*):([ \t])*/)) { |
| 67 | var content = read(match[1], 'ascii') |
| 68 | // try to find a GROUP ( ... ) command |
| 69 | if (match = content.match(/GROUP *\( *(([^ )])+)/)){ |
| 70 | return DynamicLibrary.call(this, match[1], mode) |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | throw new Error('Dynamic Linking Error: ' + err) |
| 75 | } |
| 76 | } |
| 77 | module.exports = DynamicLibrary |
| 78 | |
| 79 | /** |
no outgoing calls
no test coverage detected