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;
68 bool DynamicLibrary::LoadLibraryPermanently(const char *Filename,
69 std::string *ErrMsg) {
70 void *H = dlopen(Filename, RTLD_LAZY|RTLD_GLOBAL);
72 if (ErrMsg) *ErrMsg = dlerror();
76 // Cygwin searches symbols only in the main
77 // with the handle of dlopen(NULL, RTLD_GLOBAL).
81 SmartScopedLock<true> Lock(HandlesMutex);
82 if (OpenedHandles == 0)
83 OpenedHandles = new std::vector<void *>();
84 OpenedHandles->push_back(H);
90 using namespace llvm::sys;
92 bool DynamicLibrary::LoadLibraryPermanently(const char *Filename,
93 std::string *ErrMsg) {
94 if (ErrMsg) *ErrMsg = "dlopen() not supported on this platform";
100 void *SearchForAddressOfSpecialSymbol(const char* symbolName);
103 void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
104 // First check symbols added via AddSymbol().
105 if (ExplicitSymbols) {
106 std::map<std::string, void *>::iterator I =
107 ExplicitSymbols->find(symbolName);
108 std::map<std::string, void *>::iterator E = ExplicitSymbols->end();
115 // Now search the libraries.
116 SmartScopedLock<true> Lock(HandlesMutex);
118 for (std::vector<void *>::iterator I = OpenedHandles->begin(),
119 E = OpenedHandles->end(); I != E; ++I) {
120 //lt_ptr ptr = lt_dlsym(*I, symbolName);
121 void *ptr = dlsym(*I, symbolName);
129 if (void *Result = llvm::SearchForAddressOfSpecialSymbol(symbolName))
132 // This macro returns the address of a well-known, explicit symbol
133 #define EXPLICIT_SYMBOL(SYM) \
134 if (!strcmp(symbolName, #SYM)) return &SYM
136 // On linux we have a weird situation. The stderr/out/in symbols are both
137 // macros and global variables because of standards requirements. So, we
138 // boldly use the EXPLICIT_SYMBOL macro without checking for a #define first.
139 #if defined(__linux__)
141 EXPLICIT_SYMBOL(stderr);
142 EXPLICIT_SYMBOL(stdout);
143 EXPLICIT_SYMBOL(stdin);
146 // For everything else, we want to check to make sure the symbol isn't defined
147 // as a macro before using EXPLICIT_SYMBOL.
150 EXPLICIT_SYMBOL(stdin);
153 EXPLICIT_SYMBOL(stdout);
156 EXPLICIT_SYMBOL(stderr);
160 #undef EXPLICIT_SYMBOL
165 #endif // LLVM_ON_WIN32