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/Support/DynamicLibrary.h"
17 #include "llvm/Support/ManagedStatic.h"
18 #include "llvm/ADT/DenseSet.h"
19 #include "llvm/ADT/StringMap.h"
20 #include "llvm/Config/config.h"
21 #include "llvm/Support/Mutex.h"
25 // Collection of symbol name/value pairs to be searched prior to any libraries.
26 static llvm::ManagedStatic<llvm::StringMap<void *> > ExplicitSymbols;
27 static llvm::ManagedStatic<llvm::sys::SmartMutex<true> > SymbolsMutex;
29 void llvm::sys::DynamicLibrary::AddSymbol(StringRef symbolName,
31 SmartScopedLock<true> lock(*SymbolsMutex);
32 (*ExplicitSymbols)[symbolName] = symbolValue;
35 char llvm::sys::DynamicLibrary::Invalid = 0;
39 #include "Windows/DynamicLibrary.inc"
46 using namespace llvm::sys;
48 //===----------------------------------------------------------------------===//
49 //=== WARNING: Implementation here must contain only TRULY operating system
50 //=== independent code.
51 //===----------------------------------------------------------------------===//
53 static DenseSet<void *> *OpenedHandles = 0;
55 DynamicLibrary DynamicLibrary::getPermanentLibrary(const char *filename,
56 std::string *errMsg) {
57 SmartScopedLock<true> lock(*SymbolsMutex);
59 void *handle = dlopen(filename, RTLD_LAZY|RTLD_GLOBAL);
61 if (errMsg) *errMsg = dlerror();
62 return DynamicLibrary();
66 // Cygwin searches symbols only in the main
67 // with the handle of dlopen(NULL, RTLD_GLOBAL).
69 handle = RTLD_DEFAULT;
72 if (OpenedHandles == 0)
73 OpenedHandles = new DenseSet<void *>();
75 // If we've already loaded this library, dlclose() the handle in order to
76 // keep the internal refcount at +1.
77 if (!OpenedHandles->insert(handle).second)
80 return DynamicLibrary(handle);
83 void *DynamicLibrary::getAddressOfSymbol(const char *symbolName) {
86 return dlsym(Data, symbolName);
92 using namespace llvm::sys;
94 DynamicLibrary DynamicLibrary::getPermanentLibrary(const char *filename,
95 std::string *errMsg) {
96 if (errMsg) *errMsg = "dlopen() not supported on this platform";
97 return DynamicLibrary();
100 void *DynamicLibrary::getAddressOfSymbol(const char *symbolName) {
107 void *SearchForAddressOfSpecialSymbol(const char* symbolName);
110 void* DynamicLibrary::SearchForAddressOfSymbol(const char *symbolName) {
111 SmartScopedLock<true> Lock(*SymbolsMutex);
113 // First check symbols added via AddSymbol().
114 if (ExplicitSymbols.isConstructed()) {
115 StringMap<void *>::iterator i = ExplicitSymbols->find(symbolName);
117 if (i != ExplicitSymbols->end())
122 // Now search the libraries.
124 for (DenseSet<void *>::iterator I = OpenedHandles->begin(),
125 E = OpenedHandles->end(); I != E; ++I) {
126 //lt_ptr ptr = lt_dlsym(*I, symbolName);
127 void *ptr = dlsym(*I, symbolName);
135 if (void *Result = llvm::SearchForAddressOfSpecialSymbol(symbolName))
138 // This macro returns the address of a well-known, explicit symbol
139 #define EXPLICIT_SYMBOL(SYM) \
140 if (!strcmp(symbolName, #SYM)) return &SYM
142 // On linux we have a weird situation. The stderr/out/in symbols are both
143 // macros and global variables because of standards requirements. So, we
144 // boldly use the EXPLICIT_SYMBOL macro without checking for a #define first.
145 #if defined(__linux__) and !defined(__ANDROID__)
147 EXPLICIT_SYMBOL(stderr);
148 EXPLICIT_SYMBOL(stdout);
149 EXPLICIT_SYMBOL(stdin);
152 // For everything else, we want to check to make sure the symbol isn't defined
153 // as a macro before using EXPLICIT_SYMBOL.
156 EXPLICIT_SYMBOL(stdin);
159 EXPLICIT_SYMBOL(stdout);
162 EXPLICIT_SYMBOL(stderr);
166 #undef EXPLICIT_SYMBOL
171 #endif // LLVM_ON_WIN32