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 //===----------------------------------------------------------------------===//
14 #include "llvm/System/DynamicLibrary.h"
15 #include "llvm/Config/config.h"
20 // Collection of symbol name/value pairs to be searched prior to any libraries.
21 static std::map<std::string, void *> g_symbols;
23 void llvm::sys::DynamicLibrary::AddSymbol(const char* symbolName,
25 g_symbols[symbolName] = symbolValue;
28 // It is not possible to use ltdl.c on VC++ builds as the terms of its LGPL
29 // license and special exception would cause all of LLVM to be placed under
30 // the LGPL. This is because the exception applies only when libtool is
31 // used, and obviously libtool is not used with Visual Studio. An entirely
32 // separate implementation is provided in win32/DynamicLibrary.cpp.
36 #include "Win32/DynamicLibrary.inc"
44 using namespace llvm::sys;
46 //===----------------------------------------------------------------------===//
47 //=== WARNING: Implementation here must contain only TRULY operating system
48 //=== independent code.
49 //===----------------------------------------------------------------------===//
51 //static std::vector<lt_dlhandle> OpenedHandles;
52 static std::vector<void *> OpenedHandles;
54 DynamicLibrary::DynamicLibrary() {}
56 DynamicLibrary::~DynamicLibrary() {
57 while(!OpenedHandles.empty()) {
58 void *H = OpenedHandles.back(); OpenedHandles.pop_back();
63 bool DynamicLibrary::LoadLibraryPermanently(const char *Filename,
64 std::string *ErrMsg) {
65 void *H = dlopen(Filename, RTLD_LAZY|RTLD_GLOBAL);
71 OpenedHandles.push_back(H);
75 void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
76 // check_ltdl_initialization();
78 // First check symbols added via AddSymbol().
79 std::map<std::string, void *>::iterator I = g_symbols.find(symbolName);
80 if (I != g_symbols.end())
83 // Now search the libraries.
84 for (std::vector<void *>::iterator I = OpenedHandles.begin(),
85 E = OpenedHandles.end(); I != E; ++I) {
86 //lt_ptr ptr = lt_dlsym(*I, symbolName);
87 void *ptr = dlsym(*I, symbolName);
92 #define EXPLICIT_SYMBOL(SYM) \
93 extern void *SYM; if (!strcmp(symbolName, #SYM)) return &SYM
95 // If this is darwin, it has some funky issues, try to solve them here. Some
96 // important symbols are marked 'private external' which doesn't allow
97 // SearchForAddressOfSymbol to find them. As such, we special case them here,
98 // there is only a small handful of them.
102 EXPLICIT_SYMBOL(__ashldi3);
103 EXPLICIT_SYMBOL(__ashrdi3);
104 EXPLICIT_SYMBOL(__cmpdi2);
105 EXPLICIT_SYMBOL(__divdi3);
106 EXPLICIT_SYMBOL(__eprintf);
107 EXPLICIT_SYMBOL(__fixdfdi);
108 EXPLICIT_SYMBOL(__fixsfdi);
109 EXPLICIT_SYMBOL(__fixunsdfdi);
110 EXPLICIT_SYMBOL(__fixunssfdi);
111 EXPLICIT_SYMBOL(__floatdidf);
112 EXPLICIT_SYMBOL(__floatdisf);
113 EXPLICIT_SYMBOL(__lshrdi3);
114 EXPLICIT_SYMBOL(__moddi3);
115 EXPLICIT_SYMBOL(__udivdi3);
116 EXPLICIT_SYMBOL(__umoddi3);
122 EXPLICIT_SYMBOL(_alloca);
123 EXPLICIT_SYMBOL(__main);
127 #undef EXPLICIT_SYMBOL
129 // This macro returns the address of a well-known, explicit symbol
130 #define EXPLICIT_SYMBOL(SYM) \
131 if (!strcmp(symbolName, #SYM)) return &SYM
133 // On linux we have a weird situation. The stderr/out/in symbols are both
134 // macros and global variables because of standards requirements. So, we
135 // boldly use the EXPLICIT_SYMBOL macro without checking for a #define first.
136 #if defined(__linux__)
138 EXPLICIT_SYMBOL(stderr);
139 EXPLICIT_SYMBOL(stdout);
140 EXPLICIT_SYMBOL(stdin);
143 // For everything else, we want to check to make sure the symbol isn't defined
144 // as a macro before using EXPLICIT_SYMBOL.
147 EXPLICIT_SYMBOL(stdin);
150 EXPLICIT_SYMBOL(stdout);
153 EXPLICIT_SYMBOL(stderr);
157 #undef EXPLICIT_SYMBOL
162 #endif // LLVM_ON_WIN32