blob: 435513d4ff1edd71163d46fca7d9a6954e74488a [file] [log] [blame]
Reid Spencer0de02a62004-11-18 04:33:39 +00001//===-- DynamicLibrary.cpp - Runtime link/load libraries --------*- C++ -*-===//
Misha Brukmanf976c852005-04-21 22:55:34 +00002//
Reid Spencer0de02a62004-11-18 04:33:39 +00003// The LLVM Compiler Infrastructure
4//
Misha Brukmanf976c852005-04-21 22:55:34 +00005// This file was developed by Reid Spencer and is distributed under the
Reid Spencer0de02a62004-11-18 04:33:39 +00006// University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanf976c852005-04-21 22:55:34 +00007//
Reid Spencer0de02a62004-11-18 04:33:39 +00008//===----------------------------------------------------------------------===//
9//
10// This header file implements the operating system DynamicLibrary concept.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/System/DynamicLibrary.h"
Jeff Cohena4c97512004-12-24 16:26:47 +000015#include "llvm/Config/config.h"
Jeff Cohen85046902006-01-30 04:33:51 +000016#include <map>
17
18// Collection of symbol name/value pairs to be searched prior to any libraries.
19static std::map<std::string, void *> g_symbols;
20
Chris Lattneradcbce02006-07-07 17:12:36 +000021void llvm::sys::DynamicLibrary::AddSymbol(const char* symbolName,
22 void *symbolValue) {
Jeff Cohen85046902006-01-30 04:33:51 +000023 g_symbols[symbolName] = symbolValue;
24}
Jeff Cohen1a466352004-12-24 07:57:09 +000025
26// It is not possible to use ltdl.c on VC++ builds as the terms of its LGPL
27// license and special exception would cause all of LLVM to be placed under
28// the LGPL. This is because the exception applies only when libtool is
29// used, and obviously libtool is not used with Visual Studio. An entirely
30// separate implementation is provided in win32/DynamicLibrary.cpp.
31
Jeff Cohena4c97512004-12-24 16:26:47 +000032#ifdef LLVM_ON_WIN32
Jeff Cohen1a466352004-12-24 07:57:09 +000033
Reid Spencerbccc8ab2005-01-09 23:29:00 +000034#include "Win32/DynamicLibrary.inc"
Jeff Cohen1a466352004-12-24 07:57:09 +000035
36#else
37
Reid Spencer29ae1772004-11-29 12:39:10 +000038#include "ltdl.h"
Reid Spencer0de02a62004-11-18 04:33:39 +000039#include <cassert>
Chris Lattner28dabf72004-12-03 23:02:42 +000040using namespace llvm;
41using namespace llvm::sys;
Reid Spencer0de02a62004-11-18 04:33:39 +000042
43//===----------------------------------------------------------------------===//
44//=== WARNING: Implementation here must contain only TRULY operating system
Misha Brukmanf976c852005-04-21 22:55:34 +000045//=== independent code.
Reid Spencer0de02a62004-11-18 04:33:39 +000046//===----------------------------------------------------------------------===//
47
Reid Spencer19cd4a92004-11-29 13:33:28 +000048static bool did_initialize_ltdl = false;
49
50static inline void check_ltdl_initialization() {
51 if (!did_initialize_ltdl) {
52 if (0 != lt_dlinit())
53 throw std::string(lt_dlerror());
54 did_initialize_ltdl = true;
55 }
56}
57
58static std::vector<lt_dlhandle> OpenedHandles;
59
Reid Spencer441cc2a2004-11-29 10:39:46 +000060DynamicLibrary::DynamicLibrary() : handle(0) {
Reid Spencer19cd4a92004-11-29 13:33:28 +000061 check_ltdl_initialization();
Reid Spencer441cc2a2004-11-29 10:39:46 +000062
Reid Spencer19cd4a92004-11-29 13:33:28 +000063 lt_dlhandle a_handle = lt_dlopen(0);
Reid Spencer441cc2a2004-11-29 10:39:46 +000064
Reid Spencer19cd4a92004-11-29 13:33:28 +000065 if (a_handle == 0)
Reid Spencer441cc2a2004-11-29 10:39:46 +000066 throw std::string("Can't open program as dynamic library");
Misha Brukmanf976c852005-04-21 22:55:34 +000067
Reid Spencer19cd4a92004-11-29 13:33:28 +000068 handle = a_handle;
69 OpenedHandles.push_back(a_handle);
Reid Spencer441cc2a2004-11-29 10:39:46 +000070}
71
Reid Spencer0de02a62004-11-18 04:33:39 +000072DynamicLibrary::DynamicLibrary(const char*filename) : handle(0) {
Reid Spencer19cd4a92004-11-29 13:33:28 +000073 check_ltdl_initialization();
Reid Spencer0de02a62004-11-18 04:33:39 +000074
Reid Spencer19cd4a92004-11-29 13:33:28 +000075 lt_dlhandle a_handle = lt_dlopen(filename);
Reid Spencer0de02a62004-11-18 04:33:39 +000076
Reid Spencer19cd4a92004-11-29 13:33:28 +000077 if (a_handle == 0)
78 a_handle = lt_dlopenext(filename);
Reid Spencer0de02a62004-11-18 04:33:39 +000079
Reid Spencer19cd4a92004-11-29 13:33:28 +000080 if (a_handle == 0)
81 throw std::string("Can't open :") + filename + ": " + lt_dlerror();
82
83 handle = a_handle;
84 OpenedHandles.push_back(a_handle);
Reid Spencer0de02a62004-11-18 04:33:39 +000085}
86
87DynamicLibrary::~DynamicLibrary() {
Reid Spencer19cd4a92004-11-29 13:33:28 +000088 lt_dlhandle a_handle = (lt_dlhandle) handle;
89 if (a_handle) {
90 lt_dlclose(a_handle);
Reid Spencer0de02a62004-11-18 04:33:39 +000091
Reid Spencer19cd4a92004-11-29 13:33:28 +000092 for (std::vector<lt_dlhandle>::iterator I = OpenedHandles.begin(),
93 E = OpenedHandles.end(); I != E; ++I) {
94 if (*I == a_handle) {
95 // Note: don't use the swap/pop_back trick here. Order is important.
96 OpenedHandles.erase(I);
Chris Lattner2b80e8d2006-05-12 18:13:11 +000097 return;
Reid Spencer19cd4a92004-11-29 13:33:28 +000098 }
99 }
100 }
101}
102
Chris Lattneradcbce02006-07-07 17:12:36 +0000103bool DynamicLibrary::LoadLibraryPermanently(const char *Filename,
104 std::string *ErrMsg) {
Reid Spencer19cd4a92004-11-29 13:33:28 +0000105 check_ltdl_initialization();
Chris Lattneradcbce02006-07-07 17:12:36 +0000106 lt_dlhandle a_handle = lt_dlopen(Filename);
Reid Spencer19cd4a92004-11-29 13:33:28 +0000107
108 if (a_handle == 0)
Chris Lattneradcbce02006-07-07 17:12:36 +0000109 a_handle = lt_dlopenext(Filename);
Reid Spencer19cd4a92004-11-29 13:33:28 +0000110
Chris Lattneradcbce02006-07-07 17:12:36 +0000111 if (a_handle == 0) {
112 if (ErrMsg)
113 *ErrMsg = std::string("Can't open :") +
114 (Filename ? Filename : "<current process>") + ": " + lt_dlerror();
115 return true;
116 }
Reid Spencer19cd4a92004-11-29 13:33:28 +0000117
118 lt_dlmakeresident(a_handle);
119
120 OpenedHandles.push_back(a_handle);
Chris Lattneradcbce02006-07-07 17:12:36 +0000121 return false;
Reid Spencer19cd4a92004-11-29 13:33:28 +0000122}
123
124void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
125 check_ltdl_initialization();
Jeff Cohen85046902006-01-30 04:33:51 +0000126
127 // First check symbols added via AddSymbol().
128 std::map<std::string, void *>::iterator I = g_symbols.find(symbolName);
129 if (I != g_symbols.end())
130 return I->second;
131
132 // Now search the libraries.
Reid Spencer19cd4a92004-11-29 13:33:28 +0000133 for (std::vector<lt_dlhandle>::iterator I = OpenedHandles.begin(),
134 E = OpenedHandles.end(); I != E; ++I) {
135 lt_ptr ptr = lt_dlsym(*I, symbolName);
136 if (ptr)
137 return ptr;
138 }
Chris Lattner28dabf72004-12-03 23:02:42 +0000139
140 // If this is darwin, it has some funky issues, try to solve them here. Some
141 // important symbols are marked 'private external' which doesn't allow
142 // SearchForAddressOfSymbol to find them. As such, we special case them here,
143 // there is only a small handful of them.
144#ifdef __APPLE__
145 {
Chris Lattner368cb8e2004-12-04 04:17:20 +0000146#define EXPLICIT_SYMBOL(SYM) \
147 extern void *SYM; if (!strcmp(symbolName, #SYM)) return &SYM
148 EXPLICIT_SYMBOL(__ashldi3);
149 EXPLICIT_SYMBOL(__ashrdi3);
150 EXPLICIT_SYMBOL(__cmpdi2);
151 EXPLICIT_SYMBOL(__divdi3);
152 EXPLICIT_SYMBOL(__eprintf);
153 EXPLICIT_SYMBOL(__fixdfdi);
154 EXPLICIT_SYMBOL(__fixsfdi);
155 EXPLICIT_SYMBOL(__fixunsdfdi);
156 EXPLICIT_SYMBOL(__fixunssfdi);
157 EXPLICIT_SYMBOL(__floatdidf);
158 EXPLICIT_SYMBOL(__floatdisf);
159 EXPLICIT_SYMBOL(__lshrdi3);
160 EXPLICIT_SYMBOL(__moddi3);
161 EXPLICIT_SYMBOL(__udivdi3);
162 EXPLICIT_SYMBOL(__umoddi3);
163#undef EXPLICIT_SYMBOL
Chris Lattner28dabf72004-12-03 23:02:42 +0000164 }
165#endif
166
Reid Spencer19cd4a92004-11-29 13:33:28 +0000167 return 0;
Reid Spencer0de02a62004-11-18 04:33:39 +0000168}
169
170void *DynamicLibrary::GetAddressOfSymbol(const char *symbolName) {
171 assert(handle != 0 && "Invalid DynamicLibrary handle");
Reid Spencer19cd4a92004-11-29 13:33:28 +0000172 return lt_dlsym((lt_dlhandle) handle, symbolName);
Reid Spencer0de02a62004-11-18 04:33:39 +0000173}
174
Jeff Cohena4c97512004-12-24 16:26:47 +0000175#endif // LLVM_ON_WIN32