1 //===-- DynamicLibrary.cpp - Runtime link/load libraries --------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This header file implements the operating system DynamicLibrary concept.
12 // FIXME: This file leaks the ExplicitSymbols and OpenedHandles vector, and is
15 //===----------------------------------------------------------------------===//
17 #include "llvm/System/DynamicLibrary.h"
18 #include "llvm/Config/config.h"
24 // Collection of symbol name/value pairs to be searched prior to any libraries.
25 static std::map<std::string, void*> *ExplicitSymbols = 0;
27 static struct ExplicitSymbolsDeleter {
28 ~ExplicitSymbolsDeleter() {
30 delete ExplicitSymbols;
34 void llvm::sys::DynamicLibrary::AddSymbol(const char* symbolName,
36 if (ExplicitSymbols == 0)
37 ExplicitSymbols = new std::map<std::string, void*>();
38 (*ExplicitSymbols)[symbolName] = symbolValue;
43 #include "Win32/DynamicLibrary.inc"
49 using namespace llvm::sys;
51 //===----------------------------------------------------------------------===//
52 //=== WARNING: Implementation here must contain only TRULY operating system
53 //=== independent code.
54 //===----------------------------------------------------------------------===//
56 static std::vector<void *> *OpenedHandles = 0;
59 bool DynamicLibrary::LoadLibraryPermanently(const char *Filename,
60 std::string *ErrMsg) {
61 void *H = dlopen(Filename, RTLD_LAZY|RTLD_GLOBAL);
63 if (ErrMsg) *ErrMsg = dlerror();
66 if (OpenedHandles == 0)
67 OpenedHandles = new std::vector<void *>();
68 OpenedHandles->push_back(H);
72 static void *SearchForAddressOfSpecialSymbol(const char* symbolName) {
73 #define EXPLICIT_SYMBOL(SYM) \
74 extern void *SYM; if (!strcmp(symbolName, #SYM)) return &SYM
76 // If this is darwin, it has some funky issues, try to solve them here. Some
77 // important symbols are marked 'private external' which doesn't allow
78 // SearchForAddressOfSymbol to find them. As such, we special case them here,
79 // there is only a small handful of them.
83 EXPLICIT_SYMBOL(__ashldi3);
84 EXPLICIT_SYMBOL(__ashrdi3);
85 EXPLICIT_SYMBOL(__cmpdi2);
86 EXPLICIT_SYMBOL(__divdi3);
87 EXPLICIT_SYMBOL(__eprintf);
88 EXPLICIT_SYMBOL(__fixdfdi);
89 EXPLICIT_SYMBOL(__fixsfdi);
90 EXPLICIT_SYMBOL(__fixunsdfdi);
91 EXPLICIT_SYMBOL(__fixunssfdi);
92 EXPLICIT_SYMBOL(__floatdidf);
93 EXPLICIT_SYMBOL(__floatdisf);
94 EXPLICIT_SYMBOL(__lshrdi3);
95 EXPLICIT_SYMBOL(__moddi3);
96 EXPLICIT_SYMBOL(__udivdi3);
97 EXPLICIT_SYMBOL(__umoddi3);
103 EXPLICIT_SYMBOL(_alloca);
104 EXPLICIT_SYMBOL(__main);
108 #undef EXPLICIT_SYMBOL
112 void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
113 // First check symbols added via AddSymbol().
114 if (ExplicitSymbols) {
115 std::map<std::string, void *>::iterator I =
116 ExplicitSymbols->find(symbolName);
117 std::map<std::string, void *>::iterator E = ExplicitSymbols->end();
123 // Now search the libraries.
125 for (std::vector<void *>::iterator I = OpenedHandles->begin(),
126 E = OpenedHandles->end(); I != E; ++I) {
127 //lt_ptr ptr = lt_dlsym(*I, symbolName);
128 void *ptr = dlsym(*I, symbolName);
135 if (void *Result = SearchForAddressOfSpecialSymbol(symbolName))
138 // This macro returns the address of a well-known, explicit symbol
139 #define EXPLICIT_SYMBOL(SYM) \
140 if (!strcmp(symbolName, #SYM)) return &SYM
142 // On linux we have a weird situation. The stderr/out/in symbols are both
143 // macros and global variables because of standards requirements. So, we
144 // boldly use the EXPLICIT_SYMBOL macro without checking for a #define first.
145 #if defined(__linux__)
147 EXPLICIT_SYMBOL(stderr);
148 EXPLICIT_SYMBOL(stdout);
149 EXPLICIT_SYMBOL(stdin);
152 // For everything else, we want to check to make sure the symbol isn't defined
153 // as a macro before using EXPLICIT_SYMBOL.
156 EXPLICIT_SYMBOL(stdin);
159 EXPLICIT_SYMBOL(stdout);
162 EXPLICIT_SYMBOL(stderr);
166 #undef EXPLICIT_SYMBOL
171 #endif // LLVM_ON_WIN32