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 ExplicitSymbols and OpenedHandles!
14 //===----------------------------------------------------------------------===//
16 #include "llvm/ADT/StringMap.h"
17 #include "llvm/ADT/DenseSet.h"
18 #include "llvm/Support/DynamicLibrary.h"
19 #include "llvm/Support/Mutex.h"
20 #include "llvm/Config/config.h"
24 // Collection of symbol name/value pairs to be searched prior to any libraries.
25 static llvm::StringMap<void *> *ExplicitSymbols = 0;
29 struct ExplicitSymbolsDeleter {
30 ~ExplicitSymbolsDeleter() {
31 delete ExplicitSymbols;
37 static ExplicitSymbolsDeleter Dummy;
40 static llvm::sys::SmartMutex<true>& getMutex() {
41 static llvm::sys::SmartMutex<true> HandlesMutex;
45 void llvm::sys::DynamicLibrary::AddSymbol(StringRef symbolName,
47 SmartScopedLock<true> lock(getMutex());
48 if (ExplicitSymbols == 0)
49 ExplicitSymbols = new llvm::StringMap<void*>();
50 (*ExplicitSymbols)[symbolName] = symbolValue;
53 char llvm::sys::DynamicLibrary::Invalid = 0;
57 #include "Windows/DynamicLibrary.inc"
64 using namespace llvm::sys;
66 //===----------------------------------------------------------------------===//
67 //=== WARNING: Implementation here must contain only TRULY operating system
68 //=== independent code.
69 //===----------------------------------------------------------------------===//
71 static DenseSet<void *> *OpenedHandles = 0;
73 DynamicLibrary DynamicLibrary::getPermanentLibrary(const char *filename,
74 std::string *errMsg) {
75 SmartScopedLock<true> lock(getMutex());
77 void *handle = dlopen(filename, RTLD_LAZY|RTLD_GLOBAL);
79 if (errMsg) *errMsg = dlerror();
80 return DynamicLibrary();
84 // Cygwin searches symbols only in the main
85 // with the handle of dlopen(NULL, RTLD_GLOBAL).
87 handle = RTLD_DEFAULT;
90 if (OpenedHandles == 0)
91 OpenedHandles = new DenseSet<void *>();
93 // If we've already loaded this library, dlclose() the handle in order to
94 // keep the internal refcount at +1.
95 if (!OpenedHandles->insert(handle).second)
98 return DynamicLibrary(handle);
101 void *DynamicLibrary::getAddressOfSymbol(const char *symbolName) {
104 return dlsym(Data, symbolName);
109 using namespace llvm;
110 using namespace llvm::sys;
112 DynamicLibrary DynamicLibrary::getPermanentLibrary(const char *filename,
113 std::string *errMsg) {
114 if (errMsg) *errMsg = "dlopen() not supported on this platform";
115 return DynamicLibrary();
118 void *DynamicLibrary::getAddressOfSymbol(const char *symbolName) {
125 void *SearchForAddressOfSpecialSymbol(const char* symbolName);
128 void* DynamicLibrary::SearchForAddressOfSymbol(const char *symbolName) {
129 SmartScopedLock<true> Lock(getMutex());
131 // First check symbols added via AddSymbol().
132 if (ExplicitSymbols) {
133 StringMap<void *>::iterator i = ExplicitSymbols->find(symbolName);
135 if (i != ExplicitSymbols->end())
140 // Now search the libraries.
142 for (DenseSet<void *>::iterator I = OpenedHandles->begin(),
143 E = OpenedHandles->end(); I != E; ++I) {
144 //lt_ptr ptr = lt_dlsym(*I, symbolName);
145 void *ptr = dlsym(*I, symbolName);
153 if (void *Result = llvm::SearchForAddressOfSpecialSymbol(symbolName))
156 // This macro returns the address of a well-known, explicit symbol
157 #define EXPLICIT_SYMBOL(SYM) \
158 if (!strcmp(symbolName, #SYM)) return &SYM
160 // On linux we have a weird situation. The stderr/out/in symbols are both
161 // macros and global variables because of standards requirements. So, we
162 // boldly use the EXPLICIT_SYMBOL macro without checking for a #define first.
163 #if defined(__linux__) and !defined(__ANDROID__)
165 EXPLICIT_SYMBOL(stderr);
166 EXPLICIT_SYMBOL(stdout);
167 EXPLICIT_SYMBOL(stdin);
170 // For everything else, we want to check to make sure the symbol isn't defined
171 // as a macro before using EXPLICIT_SYMBOL.
174 EXPLICIT_SYMBOL(stdin);
177 EXPLICIT_SYMBOL(stdout);
180 EXPLICIT_SYMBOL(stderr);
184 #undef EXPLICIT_SYMBOL
189 #endif // LLVM_ON_WIN32