Brian Gaeke | f212e47 | 2003-10-10 16:55:42 +0000 | [diff] [blame^] | 1 | //===-- DynamicLinker.cpp - Implement DynamicLinker interface -------------===// |
| 2 | // |
| 3 | // Lightweight interface to dynamic library linking and loading, and dynamic |
| 4 | // symbol lookup functionality, in whatever form the operating system |
| 5 | // provides it. |
| 6 | // |
| 7 | // Possible future extensions include support for the HPUX shl_load() |
| 8 | // interface, the Mac OS X NSLinkModule() interface, and the Windows |
| 9 | // LoadLibrary() interface. |
| 10 | // |
| 11 | // Note that we assume that if dlopen() is available, then dlsym() is too. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "Support/DynamicLinker.h" |
| 16 | #include "Config/dlfcn.h" |
| 17 | #include <cassert> |
| 18 | |
| 19 | bool LinkDynamicObject (const char *filename, std::string *ErrorMessage) { |
| 20 | #if defined (HAVE_DLOPEN) |
| 21 | if (dlopen (filename, RTLD_NOW | RTLD_GLOBAL) == 0) { |
| 22 | if (ErrorMessage) *ErrorMessage = dlerror (); |
| 23 | return true; |
| 24 | } |
| 25 | return false; |
| 26 | #else |
| 27 | assert (0 && "Dynamic object linking not implemented for this platform"); |
| 28 | #endif |
| 29 | } |
| 30 | |
| 31 | void *GetAddressOfSymbol (const char *symbolName) { |
| 32 | #if defined (HAVE_DLOPEN) |
| 33 | return dlsym (RTLD_DEFAULT, symbolName); |
| 34 | #else |
| 35 | assert (0 && "Dynamic symbol lookup not implemented for this platform"); |
| 36 | #endif |
| 37 | } |
| 38 | |
| 39 | // soft, cushiony C++ interface. |
| 40 | void *GetAddressOfSymbol (const std::string &symbolName) { |
| 41 | return GetAddressOfSymbol (symbolName.c_str ()); |
| 42 | } |