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 the ExplicitSymbols and OpenedHandles vector, and is
15 //===----------------------------------------------------------------------===//
17 #include "llvm/System/DynamicLibrary.h"
18 #include "llvm/System/Mutex.h"
19 #include "llvm/Config/config.h"
25 // Collection of symbol name/value pairs to be searched prior to any libraries.
26 static std::map<std::string, void*> *ExplicitSymbols = 0;
30 struct ExplicitSymbolsDeleter {
31 ~ExplicitSymbolsDeleter() {
33 delete ExplicitSymbols;
39 static ExplicitSymbolsDeleter Dummy;
41 void llvm::sys::DynamicLibrary::AddSymbol(const char* symbolName,
43 if (ExplicitSymbols == 0)
44 ExplicitSymbols = new std::map<std::string, void*>();
45 (*ExplicitSymbols)[symbolName] = symbolValue;
50 #include "Win32/DynamicLibrary.inc"
57 using namespace llvm::sys;
59 //===----------------------------------------------------------------------===//
60 //=== WARNING: Implementation here must contain only TRULY operating system
61 //=== independent code.
62 //===----------------------------------------------------------------------===//
64 static SmartMutex<true>* HandlesMutex;
65 static std::vector<void *> *OpenedHandles = 0;
67 static bool InitializeMutex() {
68 HandlesMutex = new SmartMutex<true>;
69 return HandlesMutex != 0;
72 static bool EnsureMutexInitialized() {
73 static bool result = InitializeMutex();
78 bool DynamicLibrary::LoadLibraryPermanently(const char *Filename,
79 std::string *ErrMsg) {
80 void *H = dlopen(Filename, RTLD_LAZY|RTLD_GLOBAL);
82 if (ErrMsg) *ErrMsg = dlerror();
86 // Cygwin searches symbols only in the main
87 // with the handle of dlopen(NULL, RTLD_GLOBAL).
91 EnsureMutexInitialized();
92 SmartScopedLock<true> Lock(*HandlesMutex);
93 if (OpenedHandles == 0)
94 OpenedHandles = new std::vector<void *>();
95 OpenedHandles->push_back(H);
100 using namespace llvm;
101 using namespace llvm::sys;
103 bool DynamicLibrary::LoadLibraryPermanently(const char *Filename,
104 std::string *ErrMsg) {
105 if (ErrMsg) *ErrMsg = "dlopen() not supported on this platform";
111 void *SearchForAddressOfSpecialSymbol(const char* symbolName);
114 void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
115 // First check symbols added via AddSymbol().
116 if (ExplicitSymbols) {
117 std::map<std::string, void *>::iterator I =
118 ExplicitSymbols->find(symbolName);
119 std::map<std::string, void *>::iterator E = ExplicitSymbols->end();
126 // Now search the libraries.
127 EnsureMutexInitialized();
128 SmartScopedLock<true> Lock(*HandlesMutex);
130 for (std::vector<void *>::iterator I = OpenedHandles->begin(),
131 E = OpenedHandles->end(); I != E; ++I) {
132 //lt_ptr ptr = lt_dlsym(*I, symbolName);
133 void *ptr = dlsym(*I, symbolName);
141 if (void *Result = llvm::SearchForAddressOfSpecialSymbol(symbolName))
144 // This macro returns the address of a well-known, explicit symbol
145 #define EXPLICIT_SYMBOL(SYM) \
146 if (!strcmp(symbolName, #SYM)) return &SYM
148 // On linux we have a weird situation. The stderr/out/in symbols are both
149 // macros and global variables because of standards requirements. So, we
150 // boldly use the EXPLICIT_SYMBOL macro without checking for a #define first.
151 #if defined(__linux__)
153 EXPLICIT_SYMBOL(stderr);
154 EXPLICIT_SYMBOL(stdout);
155 EXPLICIT_SYMBOL(stdin);
158 // For everything else, we want to check to make sure the symbol isn't defined
159 // as a macro before using EXPLICIT_SYMBOL.
162 EXPLICIT_SYMBOL(stdin);
165 EXPLICIT_SYMBOL(stdout);
168 EXPLICIT_SYMBOL(stderr);
172 #undef EXPLICIT_SYMBOL
177 #endif // LLVM_ON_WIN32