1 //===-- DynamicLibrary.cpp - Runtime link/load libraries --------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements the operating system DynamicLibrary concept.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/Support/DynamicLibrary.h"
14 #include "llvm-c/Support.h"
15 #include "llvm/ADT/DenseSet.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/ADT/StringMap.h"
18 #include "llvm/Config/config.h"
19 #include "llvm/Support/ManagedStatic.h"
20 #include "llvm/Support/Mutex.h"
26 using namespace llvm::sys
;
28 // All methods for HandleSet should be used holding SymbolsMutex.
29 class DynamicLibrary::HandleSet
{
30 typedef std::vector
<void *> HandleList
;
35 static void *DLOpen(const char *Filename
, std::string
*Err
);
36 static void DLClose(void *Handle
);
37 static void *DLSym(void *Handle
, const char *Symbol
);
39 HandleSet() : Process(nullptr) {}
42 HandleList::iterator
Find(void *Handle
) {
43 return std::find(Handles
.begin(), Handles
.end(), Handle
);
46 bool Contains(void *Handle
) {
47 return Handle
== Process
|| Find(Handle
) != Handles
.end();
50 bool AddLibrary(void *Handle
, bool IsProcess
= false, bool CanClose
= true) {
52 assert((Handle
== this ? IsProcess
: !IsProcess
) && "Bad Handle.");
55 if (LLVM_LIKELY(!IsProcess
)) {
56 if (Find(Handle
) != Handles
.end()) {
61 Handles
.push_back(Handle
);
67 if (Process
== Handle
)
76 void *LibLookup(const char *Symbol
, DynamicLibrary::SearchOrdering Order
) {
77 if (Order
& SO_LoadOrder
) {
78 for (void *Handle
: Handles
) {
79 if (void *Ptr
= DLSym(Handle
, Symbol
))
83 for (void *Handle
: llvm::reverse(Handles
)) {
84 if (void *Ptr
= DLSym(Handle
, Symbol
))
91 void *Lookup(const char *Symbol
, DynamicLibrary::SearchOrdering Order
) {
92 assert(!((Order
& SO_LoadedFirst
) && (Order
& SO_LoadedLast
)) &&
95 if (!Process
|| (Order
& SO_LoadedFirst
)) {
96 if (void *Ptr
= LibLookup(Symbol
, Order
))
100 // Use OS facilities to search the current binary and all loaded libs.
101 if (void *Ptr
= DLSym(Process
, Symbol
))
104 // Search any libs that might have been skipped because of RTLD_LOCAL.
105 if (Order
& SO_LoadedLast
) {
106 if (void *Ptr
= LibLookup(Symbol
, Order
))
115 // Collection of symbol name/value pairs to be searched prior to any libraries.
116 static llvm::ManagedStatic
<llvm::StringMap
<void *>> ExplicitSymbols
;
117 // Collection of known library handles.
118 static llvm::ManagedStatic
<DynamicLibrary::HandleSet
> OpenedHandles
;
119 // Lock for ExplicitSymbols and OpenedHandles.
120 static llvm::ManagedStatic
<llvm::sys::SmartMutex
<true>> SymbolsMutex
;
125 #include "Windows/DynamicLibrary.inc"
129 #include "Unix/DynamicLibrary.inc"
133 char DynamicLibrary::Invalid
;
134 DynamicLibrary::SearchOrdering
DynamicLibrary::SearchOrder
=
135 DynamicLibrary::SO_Linker
;
138 void *SearchForAddressOfSpecialSymbol(const char *SymbolName
) {
139 return DoSearch(SymbolName
); // DynamicLibrary.inc
143 void DynamicLibrary::AddSymbol(StringRef SymbolName
, void *SymbolValue
) {
144 SmartScopedLock
<true> Lock(*SymbolsMutex
);
145 (*ExplicitSymbols
)[SymbolName
] = SymbolValue
;
148 DynamicLibrary
DynamicLibrary::getPermanentLibrary(const char *FileName
,
150 // Force OpenedHandles to be added into the ManagedStatic list before any
151 // ManagedStatic can be added from static constructors in HandleSet::DLOpen.
152 HandleSet
& HS
= *OpenedHandles
;
154 void *Handle
= HandleSet::DLOpen(FileName
, Err
);
155 if (Handle
!= &Invalid
) {
156 SmartScopedLock
<true> Lock(*SymbolsMutex
);
157 HS
.AddLibrary(Handle
, /*IsProcess*/ FileName
== nullptr);
160 return DynamicLibrary(Handle
);
163 DynamicLibrary
DynamicLibrary::addPermanentLibrary(void *Handle
,
165 SmartScopedLock
<true> Lock(*SymbolsMutex
);
166 // If we've already loaded this library, tell the caller.
167 if (!OpenedHandles
->AddLibrary(Handle
, /*IsProcess*/false, /*CanClose*/false))
168 *Err
= "Library already loaded";
170 return DynamicLibrary(Handle
);
173 void *DynamicLibrary::getAddressOfSymbol(const char *SymbolName
) {
176 return HandleSet::DLSym(Data
, SymbolName
);
179 void *DynamicLibrary::SearchForAddressOfSymbol(const char *SymbolName
) {
181 SmartScopedLock
<true> Lock(*SymbolsMutex
);
183 // First check symbols added via AddSymbol().
184 if (ExplicitSymbols
.isConstructed()) {
185 StringMap
<void *>::iterator i
= ExplicitSymbols
->find(SymbolName
);
187 if (i
!= ExplicitSymbols
->end())
191 // Now search the libraries.
192 if (OpenedHandles
.isConstructed()) {
193 if (void *Ptr
= OpenedHandles
->Lookup(SymbolName
, SearchOrder
))
198 return llvm::SearchForAddressOfSpecialSymbol(SymbolName
);
201 //===----------------------------------------------------------------------===//
203 //===----------------------------------------------------------------------===//
205 LLVMBool
LLVMLoadLibraryPermanently(const char *Filename
) {
206 return llvm::sys::DynamicLibrary::LoadLibraryPermanently(Filename
);
209 void *LLVMSearchForAddressOfSymbol(const char *symbolName
) {
210 return llvm::sys::DynamicLibrary::SearchForAddressOfSymbol(symbolName
);
213 void LLVMAddSymbol(const char *symbolName
, void *symbolValue
) {
214 return llvm::sys::DynamicLibrary::AddSymbol(symbolName
, symbolValue
);