blob: 08b7a88cbb3d5f572cd4b12c6464536e198f2bf4 [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 inline void check_ltdl_initialization() {
Reid Spencer99655e12006-08-25 19:54:53 +000049 static bool did_initialize_ltdl = false;
Reid Spencer19cd4a92004-11-29 13:33:28 +000050 if (!did_initialize_ltdl) {
Reid Spencer99655e12006-08-25 19:54:53 +000051 assert(0 == lt_dlinit() || "Can't init the ltdl library");
Reid Spencer19cd4a92004-11-29 13:33:28 +000052 did_initialize_ltdl = true;
53 }
54}
55
56static std::vector<lt_dlhandle> OpenedHandles;
57
Reid Spencer441cc2a2004-11-29 10:39:46 +000058DynamicLibrary::DynamicLibrary() : handle(0) {
Reid Spencer19cd4a92004-11-29 13:33:28 +000059 check_ltdl_initialization();
Reid Spencer441cc2a2004-11-29 10:39:46 +000060
Reid Spencer19cd4a92004-11-29 13:33:28 +000061 lt_dlhandle a_handle = lt_dlopen(0);
Reid Spencer441cc2a2004-11-29 10:39:46 +000062
Reid Spencer99655e12006-08-25 19:54:53 +000063 assert(a_handle == 0 || "Can't open program as dynamic library");
Misha Brukmanf976c852005-04-21 22:55:34 +000064
Reid Spencer19cd4a92004-11-29 13:33:28 +000065 handle = a_handle;
66 OpenedHandles.push_back(a_handle);
Reid Spencer441cc2a2004-11-29 10:39:46 +000067}
68
Reid Spencer99655e12006-08-25 19:54:53 +000069/*
Reid Spencer0de02a62004-11-18 04:33:39 +000070DynamicLibrary::DynamicLibrary(const char*filename) : handle(0) {
Reid Spencer19cd4a92004-11-29 13:33:28 +000071 check_ltdl_initialization();
Reid Spencer0de02a62004-11-18 04:33:39 +000072
Reid Spencer19cd4a92004-11-29 13:33:28 +000073 lt_dlhandle a_handle = lt_dlopen(filename);
Reid Spencer0de02a62004-11-18 04:33:39 +000074
Reid Spencer19cd4a92004-11-29 13:33:28 +000075 if (a_handle == 0)
76 a_handle = lt_dlopenext(filename);
Reid Spencer0de02a62004-11-18 04:33:39 +000077
Reid Spencer19cd4a92004-11-29 13:33:28 +000078 if (a_handle == 0)
79 throw std::string("Can't open :") + filename + ": " + lt_dlerror();
80
81 handle = a_handle;
82 OpenedHandles.push_back(a_handle);
Reid Spencer0de02a62004-11-18 04:33:39 +000083}
Reid Spencer99655e12006-08-25 19:54:53 +000084*/
Reid Spencer0de02a62004-11-18 04:33:39 +000085
86DynamicLibrary::~DynamicLibrary() {
Reid Spencer19cd4a92004-11-29 13:33:28 +000087 lt_dlhandle a_handle = (lt_dlhandle) handle;
88 if (a_handle) {
89 lt_dlclose(a_handle);
Reid Spencer0de02a62004-11-18 04:33:39 +000090
Reid Spencer19cd4a92004-11-29 13:33:28 +000091 for (std::vector<lt_dlhandle>::iterator I = OpenedHandles.begin(),
92 E = OpenedHandles.end(); I != E; ++I) {
93 if (*I == a_handle) {
94 // Note: don't use the swap/pop_back trick here. Order is important.
95 OpenedHandles.erase(I);
Chris Lattner2b80e8d2006-05-12 18:13:11 +000096 return;
Reid Spencer19cd4a92004-11-29 13:33:28 +000097 }
98 }
99 }
100}
101
Chris Lattneradcbce02006-07-07 17:12:36 +0000102bool DynamicLibrary::LoadLibraryPermanently(const char *Filename,
103 std::string *ErrMsg) {
Reid Spencer19cd4a92004-11-29 13:33:28 +0000104 check_ltdl_initialization();
Chris Lattneradcbce02006-07-07 17:12:36 +0000105 lt_dlhandle a_handle = lt_dlopen(Filename);
Reid Spencer19cd4a92004-11-29 13:33:28 +0000106
107 if (a_handle == 0)
Chris Lattneradcbce02006-07-07 17:12:36 +0000108 a_handle = lt_dlopenext(Filename);
Reid Spencer19cd4a92004-11-29 13:33:28 +0000109
Chris Lattneradcbce02006-07-07 17:12:36 +0000110 if (a_handle == 0) {
111 if (ErrMsg)
112 *ErrMsg = std::string("Can't open :") +
113 (Filename ? Filename : "<current process>") + ": " + lt_dlerror();
114 return true;
115 }
Reid Spencer19cd4a92004-11-29 13:33:28 +0000116
117 lt_dlmakeresident(a_handle);
118
119 OpenedHandles.push_back(a_handle);
Chris Lattneradcbce02006-07-07 17:12:36 +0000120 return false;
Reid Spencer19cd4a92004-11-29 13:33:28 +0000121}
122
123void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
124 check_ltdl_initialization();
Jeff Cohen85046902006-01-30 04:33:51 +0000125
126 // First check symbols added via AddSymbol().
127 std::map<std::string, void *>::iterator I = g_symbols.find(symbolName);
128 if (I != g_symbols.end())
129 return I->second;
130
131 // Now search the libraries.
Reid Spencer19cd4a92004-11-29 13:33:28 +0000132 for (std::vector<lt_dlhandle>::iterator I = OpenedHandles.begin(),
133 E = OpenedHandles.end(); I != E; ++I) {
134 lt_ptr ptr = lt_dlsym(*I, symbolName);
135 if (ptr)
136 return ptr;
137 }
Chris Lattner28dabf72004-12-03 23:02:42 +0000138
139 // If this is darwin, it has some funky issues, try to solve them here. Some
140 // important symbols are marked 'private external' which doesn't allow
141 // SearchForAddressOfSymbol to find them. As such, we special case them here,
142 // there is only a small handful of them.
143#ifdef __APPLE__
144 {
Chris Lattner368cb8e2004-12-04 04:17:20 +0000145#define EXPLICIT_SYMBOL(SYM) \
146 extern void *SYM; if (!strcmp(symbolName, #SYM)) return &SYM
147 EXPLICIT_SYMBOL(__ashldi3);
148 EXPLICIT_SYMBOL(__ashrdi3);
149 EXPLICIT_SYMBOL(__cmpdi2);
150 EXPLICIT_SYMBOL(__divdi3);
151 EXPLICIT_SYMBOL(__eprintf);
152 EXPLICIT_SYMBOL(__fixdfdi);
153 EXPLICIT_SYMBOL(__fixsfdi);
154 EXPLICIT_SYMBOL(__fixunsdfdi);
155 EXPLICIT_SYMBOL(__fixunssfdi);
156 EXPLICIT_SYMBOL(__floatdidf);
157 EXPLICIT_SYMBOL(__floatdisf);
158 EXPLICIT_SYMBOL(__lshrdi3);
159 EXPLICIT_SYMBOL(__moddi3);
160 EXPLICIT_SYMBOL(__udivdi3);
161 EXPLICIT_SYMBOL(__umoddi3);
162#undef EXPLICIT_SYMBOL
Chris Lattner28dabf72004-12-03 23:02:42 +0000163 }
164#endif
165
Reid Spencer19cd4a92004-11-29 13:33:28 +0000166 return 0;
Reid Spencer0de02a62004-11-18 04:33:39 +0000167}
168
169void *DynamicLibrary::GetAddressOfSymbol(const char *symbolName) {
170 assert(handle != 0 && "Invalid DynamicLibrary handle");
Reid Spencer19cd4a92004-11-29 13:33:28 +0000171 return lt_dlsym((lt_dlhandle) handle, symbolName);
Reid Spencer0de02a62004-11-18 04:33:39 +0000172}
173
Jeff Cohena4c97512004-12-24 16:26:47 +0000174#endif // LLVM_ON_WIN32
Reid Spencer23dd3322006-07-26 16:55:39 +0000175
176DEFINING_FILE_FOR(SystemDynamicLibrary)