blob: a21f16ab327e71e45f68a4eb1cf35b8f882476c9 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- DynamicLibrary.cpp - Runtime link/load libraries --------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This header file implements the operating system DynamicLibrary concept.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/System/DynamicLibrary.h"
15#include "llvm/Config/config.h"
Duncan Sandsfca20142008-01-09 19:42:09 +000016#include <cstring>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000017#include <map>
18
19// Collection of symbol name/value pairs to be searched prior to any libraries.
20static std::map<std::string, void *> g_symbols;
21
22void llvm::sys::DynamicLibrary::AddSymbol(const char* symbolName,
23 void *symbolValue) {
24 g_symbols[symbolName] = symbolValue;
25}
26
27// It is not possible to use ltdl.c on VC++ builds as the terms of its LGPL
28// license and special exception would cause all of LLVM to be placed under
29// the LGPL. This is because the exception applies only when libtool is
30// used, and obviously libtool is not used with Visual Studio. An entirely
31// separate implementation is provided in win32/DynamicLibrary.cpp.
32
33#ifdef LLVM_ON_WIN32
34
35#include "Win32/DynamicLibrary.inc"
36
37#else
38
39#include "ltdl.h"
40#include <cassert>
41using namespace llvm;
42using namespace llvm::sys;
43
44//===----------------------------------------------------------------------===//
45//=== WARNING: Implementation here must contain only TRULY operating system
46//=== independent code.
47//===----------------------------------------------------------------------===//
48
49static inline void check_ltdl_initialization() {
50 static bool did_initialize_ltdl = false;
51 if (!did_initialize_ltdl) {
52 int Err = lt_dlinit();
53 Err = Err; // Silence warning.
54 assert(0 == Err && "Can't init the ltdl library");
55 did_initialize_ltdl = true;
56 }
57}
58
59static std::vector<lt_dlhandle> OpenedHandles;
60
61DynamicLibrary::DynamicLibrary() : handle(0) {
62 check_ltdl_initialization();
63
64 lt_dlhandle a_handle = lt_dlopen(0);
65
Chris Lattnerf7d1aad2007-09-28 20:50:50 +000066 assert(a_handle && "Can't open program as dynamic library");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000067
68 handle = a_handle;
69 OpenedHandles.push_back(a_handle);
70}
71
72/*
73DynamicLibrary::DynamicLibrary(const char*filename) : handle(0) {
74 check_ltdl_initialization();
75
76 lt_dlhandle a_handle = lt_dlopen(filename);
77
78 if (a_handle == 0)
79 a_handle = lt_dlopenext(filename);
80
81 if (a_handle == 0)
82 throw std::string("Can't open :") + filename + ": " + lt_dlerror();
83
84 handle = a_handle;
85 OpenedHandles.push_back(a_handle);
86}
87*/
88
89DynamicLibrary::~DynamicLibrary() {
90 lt_dlhandle a_handle = (lt_dlhandle) handle;
91 if (a_handle) {
92 lt_dlclose(a_handle);
93
94 for (std::vector<lt_dlhandle>::iterator I = OpenedHandles.begin(),
95 E = OpenedHandles.end(); I != E; ++I) {
96 if (*I == a_handle) {
97 // Note: don't use the swap/pop_back trick here. Order is important.
98 OpenedHandles.erase(I);
99 return;
100 }
101 }
102 }
103}
104
105bool DynamicLibrary::LoadLibraryPermanently(const char *Filename,
106 std::string *ErrMsg) {
107 check_ltdl_initialization();
108 lt_dlhandle a_handle = lt_dlopen(Filename);
109
110 if (a_handle == 0)
111 a_handle = lt_dlopenext(Filename);
112
113 if (a_handle == 0) {
114 if (ErrMsg)
115 *ErrMsg = std::string("Can't open :") +
116 (Filename ? Filename : "<current process>") + ": " + lt_dlerror();
117 return true;
118 }
119
120 lt_dlmakeresident(a_handle);
121
122 OpenedHandles.push_back(a_handle);
123 return false;
124}
125
126void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
127 check_ltdl_initialization();
128
129 // First check symbols added via AddSymbol().
130 std::map<std::string, void *>::iterator I = g_symbols.find(symbolName);
131 if (I != g_symbols.end())
132 return I->second;
133
134 // Now search the libraries.
135 for (std::vector<lt_dlhandle>::iterator I = OpenedHandles.begin(),
136 E = OpenedHandles.end(); I != E; ++I) {
137 lt_ptr ptr = lt_dlsym(*I, symbolName);
138 if (ptr)
139 return ptr;
140 }
141
Anton Korobeynikov942cbab2007-12-03 05:30:41 +0000142#define EXPLICIT_SYMBOL(SYM) \
143 extern void *SYM; if (!strcmp(symbolName, #SYM)) return &SYM
144
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000145 // If this is darwin, it has some funky issues, try to solve them here. Some
146 // important symbols are marked 'private external' which doesn't allow
147 // SearchForAddressOfSymbol to find them. As such, we special case them here,
148 // there is only a small handful of them.
149
150#ifdef __APPLE__
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000151 {
152 EXPLICIT_SYMBOL(__ashldi3);
153 EXPLICIT_SYMBOL(__ashrdi3);
154 EXPLICIT_SYMBOL(__cmpdi2);
155 EXPLICIT_SYMBOL(__divdi3);
156 EXPLICIT_SYMBOL(__eprintf);
157 EXPLICIT_SYMBOL(__fixdfdi);
158 EXPLICIT_SYMBOL(__fixsfdi);
159 EXPLICIT_SYMBOL(__fixunsdfdi);
160 EXPLICIT_SYMBOL(__fixunssfdi);
161 EXPLICIT_SYMBOL(__floatdidf);
162 EXPLICIT_SYMBOL(__floatdisf);
163 EXPLICIT_SYMBOL(__lshrdi3);
164 EXPLICIT_SYMBOL(__moddi3);
165 EXPLICIT_SYMBOL(__udivdi3);
166 EXPLICIT_SYMBOL(__umoddi3);
167 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000168#endif
169
Anton Korobeynikov942cbab2007-12-03 05:30:41 +0000170#ifdef __CYGWIN__
171 {
172 EXPLICIT_SYMBOL(_alloca);
173 }
174#endif
175
176#undef EXPLICIT_SYMBOL
177
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000178// This macro returns the address of a well-known, explicit symbol
179#define EXPLICIT_SYMBOL(SYM) \
180 if (!strcmp(symbolName, #SYM)) return &SYM
181
182// On linux we have a weird situation. The stderr/out/in symbols are both
183// macros and global variables because of standards requirements. So, we
184// boldly use the EXPLICIT_SYMBOL macro without checking for a #define first.
185#if defined(__linux__)
186 {
187 EXPLICIT_SYMBOL(stderr);
188 EXPLICIT_SYMBOL(stdout);
189 EXPLICIT_SYMBOL(stdin);
190 }
191#else
192 // For everything else, we want to check to make sure the symbol isn't defined
193 // as a macro before using EXPLICIT_SYMBOL.
194 {
195#ifndef stdin
196 EXPLICIT_SYMBOL(stdin);
197#endif
198#ifndef stdout
199 EXPLICIT_SYMBOL(stdout);
200#endif
201#ifndef stderr
202 EXPLICIT_SYMBOL(stderr);
203#endif
204 }
205#endif
206#undef EXPLICIT_SYMBOL
207
208 return 0;
209}
210
211void *DynamicLibrary::GetAddressOfSymbol(const char *symbolName) {
212 assert(handle != 0 && "Invalid DynamicLibrary handle");
213 return lt_dlsym((lt_dlhandle) handle, symbolName);
214}
215
216#endif // LLVM_ON_WIN32
217
218DEFINING_FILE_FOR(SystemDynamicLibrary)