blob: 056fed0eb38e5493a378704bb0df759f87861aff [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
21void llvm::sys::DynamicLibrary::AddSymbol(const char* symbolName, void *symbolValue) {
22 g_symbols[symbolName] = symbolValue;
23}
Jeff Cohen1a466352004-12-24 07:57:09 +000024
25// It is not possible to use ltdl.c on VC++ builds as the terms of its LGPL
26// license and special exception would cause all of LLVM to be placed under
27// the LGPL. This is because the exception applies only when libtool is
28// used, and obviously libtool is not used with Visual Studio. An entirely
29// separate implementation is provided in win32/DynamicLibrary.cpp.
30
Jeff Cohena4c97512004-12-24 16:26:47 +000031#ifdef LLVM_ON_WIN32
Jeff Cohen1a466352004-12-24 07:57:09 +000032
Reid Spencerbccc8ab2005-01-09 23:29:00 +000033#include "Win32/DynamicLibrary.inc"
Jeff Cohen1a466352004-12-24 07:57:09 +000034
35#else
36
Reid Spencer29ae1772004-11-29 12:39:10 +000037#include "ltdl.h"
Reid Spencer0de02a62004-11-18 04:33:39 +000038#include <cassert>
Chris Lattner28dabf72004-12-03 23:02:42 +000039using namespace llvm;
40using namespace llvm::sys;
Reid Spencer0de02a62004-11-18 04:33:39 +000041
42//===----------------------------------------------------------------------===//
43//=== WARNING: Implementation here must contain only TRULY operating system
Misha Brukmanf976c852005-04-21 22:55:34 +000044//=== independent code.
Reid Spencer0de02a62004-11-18 04:33:39 +000045//===----------------------------------------------------------------------===//
46
Reid Spencer19cd4a92004-11-29 13:33:28 +000047static bool did_initialize_ltdl = false;
48
49static inline void check_ltdl_initialization() {
50 if (!did_initialize_ltdl) {
51 if (0 != lt_dlinit())
52 throw std::string(lt_dlerror());
53 did_initialize_ltdl = true;
54 }
55}
56
57static std::vector<lt_dlhandle> OpenedHandles;
58
Reid Spencer441cc2a2004-11-29 10:39:46 +000059DynamicLibrary::DynamicLibrary() : handle(0) {
Reid Spencer19cd4a92004-11-29 13:33:28 +000060 check_ltdl_initialization();
Reid Spencer441cc2a2004-11-29 10:39:46 +000061
Reid Spencer19cd4a92004-11-29 13:33:28 +000062 lt_dlhandle a_handle = lt_dlopen(0);
Reid Spencer441cc2a2004-11-29 10:39:46 +000063
Reid Spencer19cd4a92004-11-29 13:33:28 +000064 if (a_handle == 0)
Reid Spencer441cc2a2004-11-29 10:39:46 +000065 throw std::string("Can't open program as dynamic library");
Misha Brukmanf976c852005-04-21 22:55:34 +000066
Reid Spencer19cd4a92004-11-29 13:33:28 +000067 handle = a_handle;
68 OpenedHandles.push_back(a_handle);
Reid Spencer441cc2a2004-11-29 10:39:46 +000069}
70
Reid Spencer0de02a62004-11-18 04:33:39 +000071DynamicLibrary::DynamicLibrary(const char*filename) : handle(0) {
Reid Spencer19cd4a92004-11-29 13:33:28 +000072 check_ltdl_initialization();
Reid Spencer0de02a62004-11-18 04:33:39 +000073
Reid Spencer19cd4a92004-11-29 13:33:28 +000074 lt_dlhandle a_handle = lt_dlopen(filename);
Reid Spencer0de02a62004-11-18 04:33:39 +000075
Reid Spencer19cd4a92004-11-29 13:33:28 +000076 if (a_handle == 0)
77 a_handle = lt_dlopenext(filename);
Reid Spencer0de02a62004-11-18 04:33:39 +000078
Reid Spencer19cd4a92004-11-29 13:33:28 +000079 if (a_handle == 0)
80 throw std::string("Can't open :") + filename + ": " + lt_dlerror();
81
82 handle = a_handle;
83 OpenedHandles.push_back(a_handle);
Reid Spencer0de02a62004-11-18 04:33:39 +000084}
85
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
102void DynamicLibrary::LoadLibraryPermanently(const char* filename) {
103 check_ltdl_initialization();
104 lt_dlhandle a_handle = lt_dlopen(filename);
105
106 if (a_handle == 0)
107 a_handle = lt_dlopenext(filename);
108
109 if (a_handle == 0)
110 throw std::string("Can't open :") + filename + ": " + lt_dlerror();
111
112 lt_dlmakeresident(a_handle);
113
114 OpenedHandles.push_back(a_handle);
115}
116
117void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
118 check_ltdl_initialization();
Jeff Cohen85046902006-01-30 04:33:51 +0000119
120 // First check symbols added via AddSymbol().
121 std::map<std::string, void *>::iterator I = g_symbols.find(symbolName);
122 if (I != g_symbols.end())
123 return I->second;
124
125 // Now search the libraries.
Reid Spencer19cd4a92004-11-29 13:33:28 +0000126 for (std::vector<lt_dlhandle>::iterator I = OpenedHandles.begin(),
127 E = OpenedHandles.end(); I != E; ++I) {
128 lt_ptr ptr = lt_dlsym(*I, symbolName);
129 if (ptr)
130 return ptr;
131 }
Chris Lattner28dabf72004-12-03 23:02:42 +0000132
133 // If this is darwin, it has some funky issues, try to solve them here. Some
134 // important symbols are marked 'private external' which doesn't allow
135 // SearchForAddressOfSymbol to find them. As such, we special case them here,
136 // there is only a small handful of them.
137#ifdef __APPLE__
138 {
Chris Lattner368cb8e2004-12-04 04:17:20 +0000139#define EXPLICIT_SYMBOL(SYM) \
140 extern void *SYM; if (!strcmp(symbolName, #SYM)) return &SYM
141 EXPLICIT_SYMBOL(__ashldi3);
142 EXPLICIT_SYMBOL(__ashrdi3);
143 EXPLICIT_SYMBOL(__cmpdi2);
144 EXPLICIT_SYMBOL(__divdi3);
145 EXPLICIT_SYMBOL(__eprintf);
146 EXPLICIT_SYMBOL(__fixdfdi);
147 EXPLICIT_SYMBOL(__fixsfdi);
148 EXPLICIT_SYMBOL(__fixunsdfdi);
149 EXPLICIT_SYMBOL(__fixunssfdi);
150 EXPLICIT_SYMBOL(__floatdidf);
151 EXPLICIT_SYMBOL(__floatdisf);
152 EXPLICIT_SYMBOL(__lshrdi3);
153 EXPLICIT_SYMBOL(__moddi3);
154 EXPLICIT_SYMBOL(__udivdi3);
155 EXPLICIT_SYMBOL(__umoddi3);
156#undef EXPLICIT_SYMBOL
Chris Lattner28dabf72004-12-03 23:02:42 +0000157 }
158#endif
159
Reid Spencer19cd4a92004-11-29 13:33:28 +0000160 return 0;
Reid Spencer0de02a62004-11-18 04:33:39 +0000161}
162
163void *DynamicLibrary::GetAddressOfSymbol(const char *symbolName) {
164 assert(handle != 0 && "Invalid DynamicLibrary handle");
Reid Spencer19cd4a92004-11-29 13:33:28 +0000165 return lt_dlsym((lt_dlhandle) handle, symbolName);
Reid Spencer0de02a62004-11-18 04:33:39 +0000166}
167
Jeff Cohena4c97512004-12-24 16:26:47 +0000168#endif // LLVM_ON_WIN32