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() |
| 15 | // interface, the Mac OS X NSLinkModule() interface, and the Windows |
| 16 | // LoadLibrary() interface. |
| 17 | // |
| 18 | // Note that we assume that if dlopen() is available, then dlsym() is too. |
| 19 | // |
| 20 | //===----------------------------------------------------------------------===// |
| 21 | |
| 22 | #include "Support/DynamicLinker.h" |
| 23 | #include "Config/dlfcn.h" |
Chris Lattner | 0a576b0 | 2004-05-27 20:53:10 +0000 | [diff] [blame] | 24 | #include "Config/windows.h" |
Brian Gaeke | f212e47 | 2003-10-10 16:55:42 +0000 | [diff] [blame] | 25 | #include <cassert> |
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 | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 28 | bool llvm::LinkDynamicObject (const char *filename, std::string *ErrorMessage) { |
Brian Gaeke | f212e47 | 2003-10-10 16:55:42 +0000 | [diff] [blame] | 29 | #if defined (HAVE_DLOPEN) |
| 30 | if (dlopen (filename, RTLD_NOW | RTLD_GLOBAL) == 0) { |
| 31 | if (ErrorMessage) *ErrorMessage = dlerror (); |
| 32 | return true; |
| 33 | } |
| 34 | return false; |
Chris Lattner | 0a576b0 | 2004-05-27 20:53:10 +0000 | [diff] [blame] | 35 | #elif defined(HAVE_WINDOWS_H) |
| 36 | if (LoadLibrary(filename)) |
| 37 | return false; |
| 38 | if (ErrorMessage) { |
| 39 | char Buffer[100]; |
| 40 | // FIXME: This should use FormatMessage |
| 41 | sprintf(Buffer, "Windows error code %d\n", GetLastError()); |
| 42 | *ErrorMessage = Buffer; |
| 43 | } |
| 44 | return true; |
Brian Gaeke | f212e47 | 2003-10-10 16:55:42 +0000 | [diff] [blame] | 45 | #else |
| 46 | assert (0 && "Dynamic object linking not implemented for this platform"); |
| 47 | #endif |
| 48 | } |
| 49 | |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 50 | void *llvm::GetAddressOfSymbol (const char *symbolName) { |
Brian Gaeke | f212e47 | 2003-10-10 16:55:42 +0000 | [diff] [blame] | 51 | #if defined (HAVE_DLOPEN) |
Chris Lattner | 0a576b0 | 2004-05-27 20:53:10 +0000 | [diff] [blame] | 52 | # ifdef RTLD_DEFAULT |
| 53 | return dlsym (RTLD_DEFAULT, symbolName); |
| 54 | # else |
| 55 | static void* CurHandle = dlopen(0, RTLD_LAZY); |
| 56 | return dlsym(CurHandle, symbolName); |
| 57 | # endif |
| 58 | #elif defined(HAVE_WINDOWS_H) |
| 59 | static HMODULE ModHandle = NULL; |
| 60 | if (ModHandle == 0) ModHandle = GetModuleHandle(NULL); |
| 61 | return (void*)GetProcAddress(ModHandle, symbolName); |
Chris Lattner | 764486f | 2003-10-25 16:55:32 +0000 | [diff] [blame] | 62 | #else |
Brian Gaeke | f212e47 | 2003-10-10 16:55:42 +0000 | [diff] [blame] | 63 | assert (0 && "Dynamic symbol lookup not implemented for this platform"); |
| 64 | #endif |
| 65 | } |
| 66 | |
| 67 | // soft, cushiony C++ interface. |
Chris Lattner | 0a576b0 | 2004-05-27 20:53:10 +0000 | [diff] [blame] | 68 | void *llvm::GetAddressOfSymbol(const std::string &symbolName) { |
| 69 | return GetAddressOfSymbol(symbolName.c_str()); |
Brian Gaeke | f212e47 | 2003-10-10 16:55:42 +0000 | [diff] [blame] | 70 | } |