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 //===----------------------------------------------------------------------===//
14 #include "llvm/Support/DynamicLibrary.h"
15 #include "llvm-c/Support.h"
16 #include "llvm/ADT/DenseSet.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/StringMap.h"
19 #include "llvm/Config/config.h"
20 #include "llvm/Support/ManagedStatic.h"
21 #include "llvm/Support/Mutex.h"
27 using namespace llvm::sys
;
29 // All methods for HandleSet should be used holding SymbolsMutex.
30 class DynamicLibrary::HandleSet
{
31 typedef std::vector
<void *> HandleList
;
36 static void *DLOpen(const char *Filename
, std::string
*Err
);
37 static void DLClose(void *Handle
);
38 static void *DLSym(void *Handle
, const char *Symbol
);
40 HandleSet() : Process(nullptr) {}
43 HandleList::iterator
Find(void *Handle
) {
44 return std::find(Handles
.begin(), Handles
.end(), Handle
);
47 bool Contains(void *Handle
) {
48 return Handle
== Process
|| Find(Handle
) != Handles
.end();
51 bool AddLibrary(void *Handle
, bool IsProcess
= false, bool CanClose
= true) {
53 assert((Handle
== this ? IsProcess
: !IsProcess
) && "Bad Handle.");
56 if (LLVM_LIKELY(!IsProcess
)) {
57 if (Find(Handle
) != Handles
.end()) {
62 Handles
.push_back(Handle
);
68 if (Process
== Handle
)
77 void *LibLookup(const char *Symbol
, DynamicLibrary::SearchOrdering Order
) {
78 if (Order
& SO_LoadOrder
) {
79 for (void *Handle
: Handles
) {
80 if (void *Ptr
= DLSym(Handle
, Symbol
))
84 for (void *Handle
: llvm::reverse(Handles
)) {
85 if (void *Ptr
= DLSym(Handle
, Symbol
))
92 void *Lookup(const char *Symbol
, DynamicLibrary::SearchOrdering Order
) {
93 assert(!((Order
& SO_LoadedFirst
) && (Order
& SO_LoadedLast
)) &&
96 if (!Process
|| (Order
& SO_LoadedFirst
)) {
97 if (void *Ptr
= LibLookup(Symbol
, Order
))
101 // Use OS facilities to search the current binary and all loaded libs.
102 if (void *Ptr
= DLSym(Process
, Symbol
))
105 // Search any libs that might have been skipped because of RTLD_LOCAL.
106 if (Order
& SO_LoadedLast
) {
107 if (void *Ptr
= LibLookup(Symbol
, Order
))
116 // Collection of symbol name/value pairs to be searched prior to any libraries.
117 static llvm::ManagedStatic
<llvm::StringMap
<void *>> ExplicitSymbols
;
118 // Collection of known library handles.
119 static llvm::ManagedStatic
<DynamicLibrary::HandleSet
> OpenedHandles
;
120 // Lock for ExplicitSymbols and OpenedHandles.
121 static llvm::ManagedStatic
<llvm::sys::SmartMutex
<true>> SymbolsMutex
;
126 #include "Windows/DynamicLibrary.inc"
130 #include "Unix/DynamicLibrary.inc"
134 char DynamicLibrary::Invalid
;
135 DynamicLibrary::SearchOrdering
DynamicLibrary::SearchOrder
=
136 DynamicLibrary::SO_Linker
;
139 void *SearchForAddressOfSpecialSymbol(const char *SymbolName
) {
140 return DoSearch(SymbolName
); // DynamicLibrary.inc
144 void DynamicLibrary::AddSymbol(StringRef SymbolName
, void *SymbolValue
) {
145 SmartScopedLock
<true> Lock(*SymbolsMutex
);
146 (*ExplicitSymbols
)[SymbolName
] = SymbolValue
;
149 DynamicLibrary
DynamicLibrary::getPermanentLibrary(const char *FileName
,
151 // Force OpenedHandles to be added into the ManagedStatic list before any
152 // ManagedStatic can be added from static constructors in HandleSet::DLOpen.
153 HandleSet
& HS
= *OpenedHandles
;
155 void *Handle
= HandleSet::DLOpen(FileName
, Err
);
156 if (Handle
!= &Invalid
) {
157 SmartScopedLock
<true> Lock(*SymbolsMutex
);
158 HS
.AddLibrary(Handle
, /*IsProcess*/ FileName
== nullptr);
161 return DynamicLibrary(Handle
);
164 DynamicLibrary
DynamicLibrary::addPermanentLibrary(void *Handle
,
166 SmartScopedLock
<true> Lock(*SymbolsMutex
);
167 // If we've already loaded this library, tell the caller.
168 if (!OpenedHandles
->AddLibrary(Handle
, /*IsProcess*/false, /*CanClose*/false))
169 *Err
= "Library already loaded";
171 return DynamicLibrary(Handle
);
174 void *DynamicLibrary::getAddressOfSymbol(const char *SymbolName
) {
177 return HandleSet::DLSym(Data
, SymbolName
);
180 void *DynamicLibrary::SearchForAddressOfSymbol(const char *SymbolName
) {
182 SmartScopedLock
<true> Lock(*SymbolsMutex
);
184 // First check symbols added via AddSymbol().
185 if (ExplicitSymbols
.isConstructed()) {
186 StringMap
<void *>::iterator i
= ExplicitSymbols
->find(SymbolName
);
188 if (i
!= ExplicitSymbols
->end())
192 // Now search the libraries.
193 if (OpenedHandles
.isConstructed()) {
194 if (void *Ptr
= OpenedHandles
->Lookup(SymbolName
, SearchOrder
))
199 return llvm::SearchForAddressOfSpecialSymbol(SymbolName
);
202 //===----------------------------------------------------------------------===//
204 //===----------------------------------------------------------------------===//
206 LLVMBool
LLVMLoadLibraryPermanently(const char *Filename
) {
207 return llvm::sys::DynamicLibrary::LoadLibraryPermanently(Filename
);
210 void *LLVMSearchForAddressOfSymbol(const char *symbolName
) {
211 return llvm::sys::DynamicLibrary::SearchForAddressOfSymbol(symbolName
);
214 void LLVMAddSymbol(const char *symbolName
, void *symbolValue
) {
215 return llvm::sys::DynamicLibrary::AddSymbol(symbolName
, symbolValue
);