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 file implements the operating system DynamicLibrary concept.
12 // FIXME: This file leaks ExplicitSymbols and OpenedHandles!
14 //===----------------------------------------------------------------------===//
16 #include "llvm/Support/DynamicLibrary.h"
17 #include "llvm-c/Support.h"
18 #include "llvm/ADT/DenseSet.h"
19 #include "llvm/ADT/StringMap.h"
20 #include "llvm/Config/config.h"
21 #include "llvm/Support/ManagedStatic.h"
22 #include "llvm/Support/Mutex.h"
26 // Collection of symbol name/value pairs to be searched prior to any libraries.
27 static llvm::ManagedStatic<llvm::StringMap<void *> > ExplicitSymbols;
28 static llvm::ManagedStatic<llvm::sys::SmartMutex<true> > SymbolsMutex;
30 void llvm::sys::DynamicLibrary::AddSymbol(StringRef symbolName,
32 SmartScopedLock<true> lock(*SymbolsMutex);
33 (*ExplicitSymbols)[symbolName] = symbolValue;
36 char llvm::sys::DynamicLibrary::Invalid = 0;
40 #include "Windows/DynamicLibrary.inc"
47 using namespace llvm::sys;
49 //===----------------------------------------------------------------------===//
50 //=== WARNING: Implementation here must contain only TRULY operating system
51 //=== independent code.
52 //===----------------------------------------------------------------------===//
54 static DenseSet<void *> *OpenedHandles = nullptr;
56 DynamicLibrary DynamicLibrary::getPermanentLibrary(const char *filename,
57 std::string *errMsg) {
58 SmartScopedLock<true> lock(*SymbolsMutex);
60 void *handle = dlopen(filename, RTLD_LAZY|RTLD_GLOBAL);
62 if (errMsg) *errMsg = dlerror();
63 return DynamicLibrary();
67 // Cygwin searches symbols only in the main
68 // with the handle of dlopen(NULL, RTLD_GLOBAL).
70 handle = RTLD_DEFAULT;
74 OpenedHandles = new DenseSet<void *>();
76 // If we've already loaded this library, dlclose() the handle in order to
77 // keep the internal refcount at +1.
78 if (!OpenedHandles->insert(handle).second)
81 return DynamicLibrary(handle);
84 void *DynamicLibrary::getAddressOfSymbol(const char *symbolName) {
87 return dlsym(Data, symbolName);
93 using namespace llvm::sys;
95 DynamicLibrary DynamicLibrary::getPermanentLibrary(const char *filename,
96 std::string *errMsg) {
97 if (errMsg) *errMsg = "dlopen() not supported on this platform";
98 return DynamicLibrary();
101 void *DynamicLibrary::getAddressOfSymbol(const char *symbolName) {
108 void *SearchForAddressOfSpecialSymbol(const char* symbolName);
111 void* DynamicLibrary::SearchForAddressOfSymbol(const char *symbolName) {
112 SmartScopedLock<true> Lock(*SymbolsMutex);
114 // First check symbols added via AddSymbol().
115 if (ExplicitSymbols.isConstructed()) {
116 StringMap<void *>::iterator i = ExplicitSymbols->find(symbolName);
118 if (i != ExplicitSymbols->end())
123 // Now search the libraries.
125 for (DenseSet<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);
136 if (void *Result = llvm::SearchForAddressOfSpecialSymbol(symbolName))
139 // This macro returns the address of a well-known, explicit symbol
140 #define EXPLICIT_SYMBOL(SYM) \
141 if (!strcmp(symbolName, #SYM)) return &SYM
143 // On linux we have a weird situation. The stderr/out/in symbols are both
144 // macros and global variables because of standards requirements. So, we
145 // boldly use the EXPLICIT_SYMBOL macro without checking for a #define first.
146 #if defined(__linux__) and !defined(__ANDROID__)
148 EXPLICIT_SYMBOL(stderr);
149 EXPLICIT_SYMBOL(stdout);
150 EXPLICIT_SYMBOL(stdin);
153 // For everything else, we want to check to make sure the symbol isn't defined
154 // as a macro before using EXPLICIT_SYMBOL.
157 EXPLICIT_SYMBOL(stdin);
160 EXPLICIT_SYMBOL(stdout);
163 EXPLICIT_SYMBOL(stderr);
167 #undef EXPLICIT_SYMBOL
172 #endif // LLVM_ON_WIN32
174 //===----------------------------------------------------------------------===//
176 //===----------------------------------------------------------------------===//
178 LLVMBool LLVMLoadLibraryPermanently(const char* Filename) {
179 return llvm::sys::DynamicLibrary::LoadLibraryPermanently(Filename);
182 void *LLVMSearchForAddressOfSymbol(const char *symbolName) {
183 return llvm::sys::DynamicLibrary::SearchForAddressOfSymbol(symbolName);
186 void LLVMAddSymbol(const char *symbolName, void *symbolValue) {
187 return llvm::sys::DynamicLibrary::AddSymbol(symbolName, symbolValue);