Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 1 | //===-- DynamicLibrary.cpp - Runtime link/load libraries --------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Reid Spencer and is distributed under the |
| 6 | // University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This header file implements the operating system DynamicLibrary concept. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/System/DynamicLibrary.h" |
Reid Spencer | 29ae177 | 2004-11-29 12:39:10 +0000 | [diff] [blame] | 15 | #include "ltdl.h" |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 16 | #include <cassert> |
| 17 | |
| 18 | //===----------------------------------------------------------------------===// |
| 19 | //=== WARNING: Implementation here must contain only TRULY operating system |
| 20 | //=== independent code. |
| 21 | //===----------------------------------------------------------------------===// |
| 22 | |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame^] | 23 | static bool did_initialize_ltdl = false; |
| 24 | |
| 25 | static inline void check_ltdl_initialization() { |
| 26 | if (!did_initialize_ltdl) { |
| 27 | if (0 != lt_dlinit()) |
| 28 | throw std::string(lt_dlerror()); |
| 29 | did_initialize_ltdl = true; |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | static std::vector<lt_dlhandle> OpenedHandles; |
| 34 | |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 35 | namespace llvm { |
| 36 | |
| 37 | using namespace sys; |
| 38 | |
Reid Spencer | 441cc2a | 2004-11-29 10:39:46 +0000 | [diff] [blame] | 39 | DynamicLibrary::DynamicLibrary() : handle(0) { |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame^] | 40 | check_ltdl_initialization(); |
Reid Spencer | 441cc2a | 2004-11-29 10:39:46 +0000 | [diff] [blame] | 41 | |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame^] | 42 | lt_dlhandle a_handle = lt_dlopen(0); |
Reid Spencer | 441cc2a | 2004-11-29 10:39:46 +0000 | [diff] [blame] | 43 | |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame^] | 44 | if (a_handle == 0) |
Reid Spencer | 441cc2a | 2004-11-29 10:39:46 +0000 | [diff] [blame] | 45 | throw std::string("Can't open program as dynamic library"); |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame^] | 46 | |
| 47 | handle = a_handle; |
| 48 | OpenedHandles.push_back(a_handle); |
Reid Spencer | 441cc2a | 2004-11-29 10:39:46 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 51 | DynamicLibrary::DynamicLibrary(const char*filename) : handle(0) { |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame^] | 52 | check_ltdl_initialization(); |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 53 | |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame^] | 54 | lt_dlhandle a_handle = lt_dlopen(filename); |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 55 | |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame^] | 56 | if (a_handle == 0) |
| 57 | a_handle = lt_dlopenext(filename); |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 58 | |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame^] | 59 | if (a_handle == 0) |
| 60 | throw std::string("Can't open :") + filename + ": " + lt_dlerror(); |
| 61 | |
| 62 | handle = a_handle; |
| 63 | OpenedHandles.push_back(a_handle); |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | DynamicLibrary::~DynamicLibrary() { |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame^] | 67 | lt_dlhandle a_handle = (lt_dlhandle) handle; |
| 68 | if (a_handle) { |
| 69 | lt_dlclose(a_handle); |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 70 | |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame^] | 71 | for (std::vector<lt_dlhandle>::iterator I = OpenedHandles.begin(), |
| 72 | E = OpenedHandles.end(); I != E; ++I) { |
| 73 | if (*I == a_handle) { |
| 74 | // Note: don't use the swap/pop_back trick here. Order is important. |
| 75 | OpenedHandles.erase(I); |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | void DynamicLibrary::LoadLibraryPermanently(const char* filename) { |
| 82 | check_ltdl_initialization(); |
| 83 | lt_dlhandle a_handle = lt_dlopen(filename); |
| 84 | |
| 85 | if (a_handle == 0) |
| 86 | a_handle = lt_dlopenext(filename); |
| 87 | |
| 88 | if (a_handle == 0) |
| 89 | throw std::string("Can't open :") + filename + ": " + lt_dlerror(); |
| 90 | |
| 91 | lt_dlmakeresident(a_handle); |
| 92 | |
| 93 | OpenedHandles.push_back(a_handle); |
| 94 | } |
| 95 | |
| 96 | void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) { |
| 97 | check_ltdl_initialization(); |
| 98 | for (std::vector<lt_dlhandle>::iterator I = OpenedHandles.begin(), |
| 99 | E = OpenedHandles.end(); I != E; ++I) { |
| 100 | lt_ptr ptr = lt_dlsym(*I, symbolName); |
| 101 | if (ptr) |
| 102 | return ptr; |
| 103 | } |
| 104 | return 0; |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | void *DynamicLibrary::GetAddressOfSymbol(const char *symbolName) { |
| 108 | assert(handle != 0 && "Invalid DynamicLibrary handle"); |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame^] | 109 | return lt_dlsym((lt_dlhandle) handle, symbolName); |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 112 | } // namespace llvm |