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);
59 int flags = RTLD_LAZY | RTLD_GLOBAL;
60 #if defined(__APPLE__)
61 // RTLD_FIRST is an apple specific flag which causes dlsym() to search only
62 // the module specified in |filename|, and not dependent modules. This
63 // behavior would be desirable for other platforms as well, except that
64 // there's not a good way to implement it.
67 void *handle = dlopen(filename, flags);
69 if (errMsg) *errMsg = dlerror();
70 return DynamicLibrary();
74 // Cygwin searches symbols only in the main
75 // with the handle of dlopen(NULL, RTLD_GLOBAL).
77 handle = RTLD_DEFAULT;
81 OpenedHandles = new DenseSet<void *>();
83 // If we've already loaded this library, dlclose() the handle in order to
84 // keep the internal refcount at +1.
85 if (!OpenedHandles->insert(handle).second)
88 return DynamicLibrary(handle);
91 void *DynamicLibrary::getAddressOfSymbol(const char *symbolName) {
94 return dlsym(Data, symbolName);
100 using namespace llvm::sys;
102 DynamicLibrary DynamicLibrary::getPermanentLibrary(const char *filename,
103 std::string *errMsg) {
104 if (errMsg) *errMsg = "dlopen() not supported on this platform";
105 return DynamicLibrary();
108 void *DynamicLibrary::getAddressOfSymbol(const char *symbolName) {
115 void *SearchForAddressOfSpecialSymbol(const char* symbolName);
118 void* DynamicLibrary::SearchForAddressOfSymbol(const char *symbolName) {
119 SmartScopedLock<true> Lock(*SymbolsMutex);
121 // First check symbols added via AddSymbol().
122 if (ExplicitSymbols.isConstructed()) {
123 StringMap<void *>::iterator i = ExplicitSymbols->find(symbolName);
125 if (i != ExplicitSymbols->end())
130 // Now search the libraries.
132 for (DenseSet<void *>::iterator I = OpenedHandles->begin(),
133 E = OpenedHandles->end(); I != E; ++I) {
134 //lt_ptr ptr = lt_dlsym(*I, symbolName);
135 void *ptr = dlsym(*I, symbolName);
143 if (void *Result = llvm::SearchForAddressOfSpecialSymbol(symbolName))
146 // This macro returns the address of a well-known, explicit symbol
147 #define EXPLICIT_SYMBOL(SYM) \
148 if (!strcmp(symbolName, #SYM)) return &SYM
150 // On linux we have a weird situation. The stderr/out/in symbols are both
151 // macros and global variables because of standards requirements. So, we
152 // boldly use the EXPLICIT_SYMBOL macro without checking for a #define first.
153 #if defined(__linux__) and !defined(__ANDROID__)
155 EXPLICIT_SYMBOL(stderr);
156 EXPLICIT_SYMBOL(stdout);
157 EXPLICIT_SYMBOL(stdin);
160 // For everything else, we want to check to make sure the symbol isn't defined
161 // as a macro before using EXPLICIT_SYMBOL.
164 EXPLICIT_SYMBOL(stdin);
167 EXPLICIT_SYMBOL(stdout);
170 EXPLICIT_SYMBOL(stderr);
174 #undef EXPLICIT_SYMBOL
179 #endif // LLVM_ON_WIN32
181 //===----------------------------------------------------------------------===//
183 //===----------------------------------------------------------------------===//
185 LLVMBool LLVMLoadLibraryPermanently(const char* Filename) {
186 return llvm::sys::DynamicLibrary::LoadLibraryPermanently(Filename);