Brian Gaeke | f212e47 | 2003-10-10 16:55:42 +0000 | [diff] [blame] | 1 | //===-- DynamicLinker.cpp - Implement DynamicLinker interface -------------===// |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Brian Gaeke | f212e47 | 2003-10-10 16:55:42 +0000 | [diff] [blame] | 9 | // |
| 10 | // Lightweight interface to dynamic library linking and loading, and dynamic |
| 11 | // symbol lookup functionality, in whatever form the operating system |
| 12 | // provides it. |
| 13 | // |
| 14 | // Possible future extensions include support for the HPUX shl_load() |
Chris Lattner | aafb1c1 | 2004-05-28 23:54:07 +0000 | [diff] [blame] | 15 | // interface, and the Mac OS X NSLinkModule() interface. |
Brian Gaeke | f212e47 | 2003-10-10 16:55:42 +0000 | [diff] [blame] | 16 | // |
| 17 | // Note that we assume that if dlopen() is available, then dlsym() is too. |
| 18 | // |
| 19 | //===----------------------------------------------------------------------===// |
| 20 | |
| 21 | #include "Support/DynamicLinker.h" |
| 22 | #include "Config/dlfcn.h" |
Chris Lattner | 0a576b0 | 2004-05-27 20:53:10 +0000 | [diff] [blame] | 23 | #include "Config/windows.h" |
Brian Gaeke | f212e47 | 2003-10-10 16:55:42 +0000 | [diff] [blame] | 24 | #include <cassert> |
Chris Lattner | aafb1c1 | 2004-05-28 23:54:07 +0000 | [diff] [blame] | 25 | #include <vector> |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 26 | using namespace llvm; |
Brian Gaeke | f212e47 | 2003-10-10 16:55:42 +0000 | [diff] [blame] | 27 | |
Chris Lattner | aafb1c1 | 2004-05-28 23:54:07 +0000 | [diff] [blame] | 28 | #if defined(HAVE_WINDOWS_H) |
| 29 | // getLoadedLibs - Keep track of the shared objects that are loaded into the |
| 30 | // process address space, as the windows GetProcAddress function does not |
| 31 | // automatically search an entire address space, it only searches a specific |
| 32 | // object. |
| 33 | static std::vector<HMODULE> &getLoadedLibHandles() { |
| 34 | static std::vector<HMODULE> *LoadedLibHandles = 0; |
| 35 | if (LoadedLibHandles == 0) { |
| 36 | LoadedLibHandles = new std::vector<HMODULE>(); |
| 37 | if (HMODULE H = GetModuleHandle(NULL)) // JIT symbols |
| 38 | LoadedLibHandles->push_back(H); |
| 39 | if (HMODULE MH = GetModuleHandle("cygwin1.dll")) // Cygwin symbols OR |
| 40 | LoadedLibHandles->push_back(MH); |
| 41 | else if (HMODULE MH = GetModuleHandle("msvcr80.dll")) // VC++ symbols |
| 42 | LoadedLibHandles->push_back(MH); |
Brian Gaeke | f212e47 | 2003-10-10 16:55:42 +0000 | [diff] [blame] | 43 | } |
Chris Lattner | aafb1c1 | 2004-05-28 23:54:07 +0000 | [diff] [blame] | 44 | return *LoadedLibHandles; |
| 45 | } |
| 46 | #endif |
| 47 | |
| 48 | bool llvm::LinkDynamicObject(const char *filename, std::string *ErrorMessage) { |
| 49 | #if defined(HAVE_WINDOWS_H) |
| 50 | if (HMODULE Handle = LoadLibrary(filename)) { |
| 51 | // Allow GetProcAddress in this module |
| 52 | getLoadedLibHandles().push_back(Handle); |
Chris Lattner | 0a576b0 | 2004-05-27 20:53:10 +0000 | [diff] [blame] | 53 | return false; |
Chris Lattner | aafb1c1 | 2004-05-28 23:54:07 +0000 | [diff] [blame] | 54 | } |
Chris Lattner | 0a576b0 | 2004-05-27 20:53:10 +0000 | [diff] [blame] | 55 | if (ErrorMessage) { |
| 56 | char Buffer[100]; |
| 57 | // FIXME: This should use FormatMessage |
| 58 | sprintf(Buffer, "Windows error code %d\n", GetLastError()); |
| 59 | *ErrorMessage = Buffer; |
| 60 | } |
| 61 | return true; |
Chris Lattner | aafb1c1 | 2004-05-28 23:54:07 +0000 | [diff] [blame] | 62 | #elif defined (HAVE_DLOPEN) |
| 63 | if (dlopen (filename, RTLD_NOW | RTLD_GLOBAL) == 0) { |
| 64 | if (ErrorMessage) *ErrorMessage = dlerror (); |
| 65 | return true; |
| 66 | } |
| 67 | return false; |
Brian Gaeke | f212e47 | 2003-10-10 16:55:42 +0000 | [diff] [blame] | 68 | #else |
| 69 | assert (0 && "Dynamic object linking not implemented for this platform"); |
| 70 | #endif |
| 71 | } |
| 72 | |
Chris Lattner | aafb1c1 | 2004-05-28 23:54:07 +0000 | [diff] [blame] | 73 | void *llvm::GetAddressOfSymbol(const char *symbolName) { |
| 74 | #if defined(HAVE_WINDOWS_H) |
| 75 | std::vector<HMODULE> &LH = getLoadedLibHandles(); |
| 76 | for (unsigned i = 0, e = LH.size(); i != e; ++i) |
| 77 | if (void *Val = (void*)GetProcAddress(LH[i], symbolName)) |
| 78 | return Val; |
| 79 | return 0; |
| 80 | #elif defined(HAVE_DLOPEN) |
Chris Lattner | 0a576b0 | 2004-05-27 20:53:10 +0000 | [diff] [blame] | 81 | # ifdef RTLD_DEFAULT |
| 82 | return dlsym (RTLD_DEFAULT, symbolName); |
| 83 | # else |
| 84 | static void* CurHandle = dlopen(0, RTLD_LAZY); |
| 85 | return dlsym(CurHandle, symbolName); |
| 86 | # endif |
Chris Lattner | 764486f | 2003-10-25 16:55:32 +0000 | [diff] [blame] | 87 | #else |
Brian Gaeke | f212e47 | 2003-10-10 16:55:42 +0000 | [diff] [blame] | 88 | assert (0 && "Dynamic symbol lookup not implemented for this platform"); |
| 89 | #endif |
| 90 | } |
| 91 | |
| 92 | // soft, cushiony C++ interface. |
Chris Lattner | 0a576b0 | 2004-05-27 20:53:10 +0000 | [diff] [blame] | 93 | void *llvm::GetAddressOfSymbol(const std::string &symbolName) { |
| 94 | return GetAddressOfSymbol(symbolName.c_str()); |
Brian Gaeke | f212e47 | 2003-10-10 16:55:42 +0000 | [diff] [blame] | 95 | } |