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//===----------------------------------------------------------------------===// 17#include "llvm/Config/config.h" 24// All methods for HandleSet should be used holding SymbolsMutex. 26typedef std::vector<void *> HandleList;
31staticvoid *
DLOpen(
constchar *Filename, std::string *Err);
33staticvoid *
DLSym(
void *Handle,
constchar *Symbol);
38 HandleList::iterator
Find(
void *Handle) {
returnfind(Handles, Handle); }
41return Handle ==
Process ||
Find(Handle) != Handles.end();
44boolAddLibrary(
void *Handle,
bool IsProcess =
false,
bool CanClose =
true,
45bool AllowDuplicates =
false) {
47assert((Handle ==
this ? IsProcess : !IsProcess) &&
"Bad Handle.");
49assert((!AllowDuplicates || !CanClose) &&
50"CanClose must be false if AllowDuplicates is true.");
53if (!AllowDuplicates &&
Find(Handle) != Handles.end()) {
58 Handles.push_back(Handle);
75 HandleList::iterator it =
Find(Handle);
76if (it != Handles.end()) {
83for (
void *Handle : Handles) {
105// Use OS facilities to search the current binary and all loaded libs. 109// Search any libs that might have been skipped because of RTLD_LOCAL. 122// Collection of symbol name/value pairs to be searched prior to any 125// Collections of known library handles. 128// Lock for ExplicitSymbols, OpenedHandles, and OpenedTemporaryHandles. 132Globals &getGlobals() {
149char DynamicLibrary::Invalid;
155return DoSearch(SymbolName);
// DynamicLibrary.inc 160auto &
G = getGlobals();
162G.ExplicitSymbols[SymbolName] = SymbolValue;
167auto &
G = getGlobals();
169if (Handle != &Invalid) {
171G.OpenedHandles.AddLibrary(Handle,
/*IsProcess*/ FileName ==
nullptr);
179auto &
G = getGlobals();
181// If we've already loaded this library, tell the caller. 182if (!
G.OpenedHandles.AddLibrary(Handle,
/*IsProcess*/false,
184 *Err =
"Library already loaded";
191assert(FileName &&
"Use getPermanentLibrary() for opening process handle");
193if (Handle != &Invalid) {
194auto &
G = getGlobals();
196G.OpenedTemporaryHandles.AddLibrary(Handle,
/*IsProcess*/false,
198/*AllowDuplicates*/true);
204auto &
G = getGlobals();
207G.OpenedTemporaryHandles.CloseLibrary(
Lib.Data);
220auto &
G = getGlobals();
223// First check symbols added via AddSymbol(). 226if (i !=
G.ExplicitSymbols.end())
229// Now search the libraries. 239//===----------------------------------------------------------------------===// 241//===----------------------------------------------------------------------===// This file defines the StringMap class.
#define LLVM_LIKELY(EXPR)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file contains some templates that are useful if you are working with the STL at all.
bool AddLibrary(void *Handle, bool IsProcess=false, bool CanClose=true, bool AllowDuplicates=false)
void * Lookup(const char *Symbol, DynamicLibrary::SearchOrdering Order)
bool Contains(void *Handle)
HandleList::iterator Find(void *Handle)
static void * DLOpen(const char *Filename, std::string *Err)
void * LibLookup(const char *Symbol, DynamicLibrary::SearchOrdering Order)
static void * DLSym(void *Handle, const char *Symbol)
static void DLClose(void *Handle)
void CloseLibrary(void *Handle)
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
StringRef - Represent a constant reference to a string, i.e.
This class provides a portable interface to dynamic libraries which also might be known as shared lib...
static DynamicLibrary getLibrary(const char *FileName, std::string *Err=nullptr)
This function loads the dynamic library at the given path, using the library load operation from the ...
static DynamicLibrary addPermanentLibrary(void *handle, std::string *errMsg=nullptr)
Registers an externally loaded library.
static void AddSymbol(StringRef symbolName, void *symbolValue)
This functions permanently adds the symbol symbolName with the value symbolValue.
static bool LoadLibraryPermanently(const char *Filename, std::string *ErrMsg=nullptr)
This function permanently loads the dynamic library at the given path.
@ SO_LoadedLast
SO_LoadedLast - Search as SO_Linker would, then loaded libraries.
@ SO_LoadOrder
SO_LoadOrder - Or this in to search libraries in the ordered loaded.
@ SO_Linker
SO_Linker - Search as a call to dlsym(dlopen(NULL)) would when DynamicLibrary::getPermanentLibrary(NU...
@ SO_LoadedFirst
SO_LoadedFirst - Search all loaded libraries, then as SO_Linker would.
static DynamicLibrary getPermanentLibrary(const char *filename, std::string *errMsg=nullptr)
This function permanently loads the dynamic library at the given path using the library load operatio...
static SearchOrdering SearchOrder
void * getAddressOfSymbol(const char *symbolName)
Searches through the library for the symbol symbolName.
bool isValid() const
Returns true if the object refers to a valid library.
static void * SearchForAddressOfSymbol(const char *symbolName)
This function will search through all previously loaded dynamic libraries for the symbol symbolName.
static void closeLibrary(DynamicLibrary &Lib)
This function closes the dynamic library at the given path, using the library close operation of the ...
A collection of legacy interfaces for querying information about the current executing process.
SmartMutex - A mutex with a compile time constant parameter that indicates whether this mutex should ...
LLVMBool LLVMLoadLibraryPermanently(const char *Filename)
This function permanently loads the dynamic library at the given path.
void LLVMAddSymbol(const char *symbolName, void *symbolValue)
This functions permanently adds the symbol symbolName with the value symbolValue.
void * LLVMSearchForAddressOfSymbol(const char *symbolName)
This function will search through all previously loaded dynamic libraries for the symbol symbolName.
std::lock_guard< SmartMutex< mt_only > > SmartScopedLock
This is an optimization pass for GlobalISel generic memory operations.
auto find(R &&Range, const T &Val)
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly.
auto reverse(ContainerTy &&C)
void * SearchForAddressOfSpecialSymbol(const char *SymbolName)