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 | |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 21 | #include "llvm/Support/DynamicLinker.h" |
| 22 | #include "llvm/Config/dlfcn.h" |
| 23 | #include "llvm/Config/windows.h" |
Chris Lattner | aafb1c1 | 2004-05-28 23:54:07 +0000 | [diff] [blame] | 24 | #include <vector> |
Alkis Evlogimenos | c72c617 | 2004-09-28 14:42:44 +0000 | [diff] [blame] | 25 | #include <cassert> |
| 26 | #include <cstdio> |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 27 | using namespace llvm; |
Brian Gaeke | f212e47 | 2003-10-10 16:55:42 +0000 | [diff] [blame] | 28 | |
Chris Lattner | aafb1c1 | 2004-05-28 23:54:07 +0000 | [diff] [blame] | 29 | #if defined(HAVE_WINDOWS_H) |
| 30 | // getLoadedLibs - Keep track of the shared objects that are loaded into the |
| 31 | // process address space, as the windows GetProcAddress function does not |
| 32 | // automatically search an entire address space, it only searches a specific |
| 33 | // object. |
| 34 | static std::vector<HMODULE> &getLoadedLibHandles() { |
| 35 | static std::vector<HMODULE> *LoadedLibHandles = 0; |
| 36 | if (LoadedLibHandles == 0) { |
| 37 | LoadedLibHandles = new std::vector<HMODULE>(); |
| 38 | if (HMODULE H = GetModuleHandle(NULL)) // JIT symbols |
| 39 | LoadedLibHandles->push_back(H); |
| 40 | if (HMODULE MH = GetModuleHandle("cygwin1.dll")) // Cygwin symbols OR |
| 41 | LoadedLibHandles->push_back(MH); |
| 42 | else if (HMODULE MH = GetModuleHandle("msvcr80.dll")) // VC++ symbols |
| 43 | LoadedLibHandles->push_back(MH); |
Brian Gaeke | f212e47 | 2003-10-10 16:55:42 +0000 | [diff] [blame] | 44 | } |
Chris Lattner | aafb1c1 | 2004-05-28 23:54:07 +0000 | [diff] [blame] | 45 | return *LoadedLibHandles; |
| 46 | } |
| 47 | #endif |
| 48 | |
| 49 | bool llvm::LinkDynamicObject(const char *filename, std::string *ErrorMessage) { |
| 50 | #if defined(HAVE_WINDOWS_H) |
| 51 | if (HMODULE Handle = LoadLibrary(filename)) { |
| 52 | // Allow GetProcAddress in this module |
| 53 | getLoadedLibHandles().push_back(Handle); |
Chris Lattner | 0a576b0 | 2004-05-27 20:53:10 +0000 | [diff] [blame] | 54 | return false; |
Chris Lattner | aafb1c1 | 2004-05-28 23:54:07 +0000 | [diff] [blame] | 55 | } |
Chris Lattner | 0a576b0 | 2004-05-27 20:53:10 +0000 | [diff] [blame] | 56 | if (ErrorMessage) { |
| 57 | char Buffer[100]; |
| 58 | // FIXME: This should use FormatMessage |
| 59 | sprintf(Buffer, "Windows error code %d\n", GetLastError()); |
| 60 | *ErrorMessage = Buffer; |
| 61 | } |
| 62 | return true; |
Chris Lattner | aafb1c1 | 2004-05-28 23:54:07 +0000 | [diff] [blame] | 63 | #elif defined (HAVE_DLOPEN) |
| 64 | if (dlopen (filename, RTLD_NOW | RTLD_GLOBAL) == 0) { |
| 65 | if (ErrorMessage) *ErrorMessage = dlerror (); |
| 66 | return true; |
| 67 | } |
| 68 | return false; |
Brian Gaeke | f212e47 | 2003-10-10 16:55:42 +0000 | [diff] [blame] | 69 | #else |
| 70 | assert (0 && "Dynamic object linking not implemented for this platform"); |
| 71 | #endif |
| 72 | } |
| 73 | |
Chris Lattner | aafb1c1 | 2004-05-28 23:54:07 +0000 | [diff] [blame] | 74 | void *llvm::GetAddressOfSymbol(const char *symbolName) { |
| 75 | #if defined(HAVE_WINDOWS_H) |
| 76 | std::vector<HMODULE> &LH = getLoadedLibHandles(); |
| 77 | for (unsigned i = 0, e = LH.size(); i != e; ++i) |
| 78 | if (void *Val = (void*)GetProcAddress(LH[i], symbolName)) |
| 79 | return Val; |
| 80 | return 0; |
| 81 | #elif defined(HAVE_DLOPEN) |
Chris Lattner | 0a576b0 | 2004-05-27 20:53:10 +0000 | [diff] [blame] | 82 | # ifdef RTLD_DEFAULT |
| 83 | return dlsym (RTLD_DEFAULT, symbolName); |
| 84 | # else |
| 85 | static void* CurHandle = dlopen(0, RTLD_LAZY); |
| 86 | return dlsym(CurHandle, symbolName); |
| 87 | # endif |
Chris Lattner | 764486f | 2003-10-25 16:55:32 +0000 | [diff] [blame] | 88 | #else |
Brian Gaeke | f212e47 | 2003-10-10 16:55:42 +0000 | [diff] [blame] | 89 | assert (0 && "Dynamic symbol lookup not implemented for this platform"); |
| 90 | #endif |
| 91 | } |
| 92 | |
| 93 | // soft, cushiony C++ interface. |
Chris Lattner | 0a576b0 | 2004-05-27 20:53:10 +0000 | [diff] [blame] | 94 | void *llvm::GetAddressOfSymbol(const std::string &symbolName) { |
| 95 | return GetAddressOfSymbol(symbolName.c_str()); |
Brian Gaeke | f212e47 | 2003-10-10 16:55:42 +0000 | [diff] [blame] | 96 | } |