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/Support/ManagedStatic.h"
19 #include "llvm/Config/config.h"
25 // Collection of symbol name/value pairs to be searched prior to any libraries.
26 static std::map<std::string, void*> *ExplicitSymbols = 0;
28 void llvm::sys::DynamicLibrary::AddSymbol(const char* symbolName,
30 if (ExplicitSymbols == 0)
31 ExplicitSymbols = new std::map<std::string, void*>();
32 (*ExplicitSymbols)[symbolName] = symbolValue;
37 #include "Win32/DynamicLibrary.inc"
43 using namespace llvm::sys;
45 //===----------------------------------------------------------------------===//
46 //=== WARNING: Implementation here must contain only TRULY operating system
47 //=== independent code.
48 //===----------------------------------------------------------------------===//
50 static std::vector<void *> *OpenedHandles = 0;
53 bool DynamicLibrary::LoadLibraryPermanently(const char *Filename,
54 std::string *ErrMsg) {
55 void *H = dlopen(Filename, RTLD_LAZY|RTLD_GLOBAL);
57 if (ErrMsg) *ErrMsg = dlerror();
60 if (OpenedHandles == 0)
61 OpenedHandles = new std::vector<void *>();
62 OpenedHandles->push_back(H);
66 void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
67 // First check symbols added via AddSymbol().
68 if (ExplicitSymbols) {
69 std::map<std::string, void *>::iterator I =
70 ExplicitSymbols->find(symbolName);
71 std::map<std::string, void *>::iterator E = ExplicitSymbols->end();
77 // Now search the libraries.
79 for (std::vector<void *>::iterator I = OpenedHandles->begin(),
80 E = OpenedHandles->end(); I != E; ++I) {
81 //lt_ptr ptr = lt_dlsym(*I, symbolName);
82 void *ptr = dlsym(*I, symbolName);
89 #define EXPLICIT_SYMBOL(SYM) \
90 extern void *SYM; if (!strcmp(symbolName, #SYM)) return &SYM
92 // If this is darwin, it has some funky issues, try to solve them here. Some
93 // important symbols are marked 'private external' which doesn't allow
94 // SearchForAddressOfSymbol to find them. As such, we special case them here,
95 // there is only a small handful of them.
99 EXPLICIT_SYMBOL(__ashldi3);
100 EXPLICIT_SYMBOL(__ashrdi3);
101 EXPLICIT_SYMBOL(__cmpdi2);
102 EXPLICIT_SYMBOL(__divdi3);
103 EXPLICIT_SYMBOL(__eprintf);
104 EXPLICIT_SYMBOL(__fixdfdi);
105 EXPLICIT_SYMBOL(__fixsfdi);
106 EXPLICIT_SYMBOL(__fixunsdfdi);
107 EXPLICIT_SYMBOL(__fixunssfdi);
108 EXPLICIT_SYMBOL(__floatdidf);
109 EXPLICIT_SYMBOL(__floatdisf);
110 EXPLICIT_SYMBOL(__lshrdi3);
111 EXPLICIT_SYMBOL(__moddi3);
112 EXPLICIT_SYMBOL(__udivdi3);
113 EXPLICIT_SYMBOL(__umoddi3);
119 EXPLICIT_SYMBOL(_alloca);
120 EXPLICIT_SYMBOL(__main);
124 #undef EXPLICIT_SYMBOL
126 // This macro returns the address of a well-known, explicit symbol
127 #define EXPLICIT_SYMBOL(SYM) \
128 if (!strcmp(symbolName, #SYM)) return &SYM
130 // On linux we have a weird situation. The stderr/out/in symbols are both
131 // macros and global variables because of standards requirements. So, we
132 // boldly use the EXPLICIT_SYMBOL macro without checking for a #define first.
133 #if defined(__linux__)
135 EXPLICIT_SYMBOL(stderr);
136 EXPLICIT_SYMBOL(stdout);
137 EXPLICIT_SYMBOL(stdin);
140 // For everything else, we want to check to make sure the symbol isn't defined
141 // as a macro before using EXPLICIT_SYMBOL.
144 EXPLICIT_SYMBOL(stdin);
147 EXPLICIT_SYMBOL(stdout);
150 EXPLICIT_SYMBOL(stderr);
154 #undef EXPLICIT_SYMBOL
159 #endif // LLVM_ON_WIN32