mirror of
https://github.com/RPCSX/llvm.git
synced 2024-11-29 22:50:47 +00:00
Use DynamicLibrary instances as a way to get symbols from a specific library. Preparation for upcoming (preliminary) support for plugins for the static analyzer.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@137791 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
89e2aa6afd
commit
0bce85fbfa
@ -28,36 +28,57 @@ namespace sys {
|
||||
/// It also allows for symbols to be defined which don't live in any library,
|
||||
/// but rather the main program itself, useful on Windows where the main
|
||||
/// executable cannot be searched.
|
||||
///
|
||||
/// Note: there is currently no interface for temporarily loading a library,
|
||||
/// or for unloading libraries when the LLVM library is unloaded.
|
||||
class DynamicLibrary {
|
||||
DynamicLibrary(); // DO NOT IMPLEMENT
|
||||
// Opaque data used to interface with OS-specific dynamic library handling.
|
||||
void *Data;
|
||||
|
||||
explicit DynamicLibrary(void *data = 0) : Data(data) {}
|
||||
public:
|
||||
/// This function allows a library to be loaded without instantiating a
|
||||
/// DynamicLibrary object. Consequently, it is marked as being permanent
|
||||
/// and will only be unloaded when the program terminates. This returns
|
||||
/// false on success or returns true and fills in *ErrMsg on failure.
|
||||
/// Returns true if the object refers to a valid library.
|
||||
bool isValid() { return Data != 0; }
|
||||
|
||||
/// Searches through the library for the symbol \p symbolName. If it is
|
||||
/// found, the address of that symbol is returned. If not, NULL is returned.
|
||||
/// Note that NULL will also be returned if the library failed to load.
|
||||
/// Use isValid() to distinguish these cases if it is important.
|
||||
/// Note that this will \e not search symbols explicitly registered by
|
||||
/// AddSymbol().
|
||||
void *getAddressOfSymbol(const char *symbolName);
|
||||
|
||||
/// This function permanently loads the dynamic library at the given path.
|
||||
/// The library will only be unloaded when the program terminates.
|
||||
/// This returns a valid DynamicLibrary instance on success and an invalid
|
||||
/// instance on failure (see isValid()). \p *errMsg will only be modified
|
||||
/// if the library fails to load.
|
||||
///
|
||||
/// It is safe to call this function multiple times for the same library.
|
||||
/// @brief Open a dynamic library permanently.
|
||||
static DynamicLibrary getPermanentLibrary(const char *filename,
|
||||
std::string *errMsg = 0);
|
||||
|
||||
/// This function permanently loads the dynamic library at the given path.
|
||||
/// Use this instead of getPermanentLibrary() when you won't need to get
|
||||
/// symbols from the library itself.
|
||||
///
|
||||
/// NOTE: This function is not thread safe.
|
||||
///
|
||||
static bool LoadLibraryPermanently(const char *filename,
|
||||
std::string *ErrMsg = 0);
|
||||
/// It is safe to call this function multiple times for the same library.
|
||||
static bool LoadLibraryPermanently(const char *Filename,
|
||||
std::string *ErrMsg = 0) {
|
||||
return !getPermanentLibrary(Filename, ErrMsg).isValid();
|
||||
}
|
||||
|
||||
/// This function will search through all previously loaded dynamic
|
||||
/// libraries for the symbol \p symbolName. If it is found, the addressof
|
||||
/// libraries for the symbol \p symbolName. If it is found, the address of
|
||||
/// that symbol is returned. If not, null is returned. Note that this will
|
||||
/// search permanently loaded libraries (LoadLibraryPermanently) as well
|
||||
/// as ephemerally loaded libraries (constructors).
|
||||
/// search permanently loaded libraries (getPermanentLibrary()) as well
|
||||
/// as explicitly registered symbols (AddSymbol()).
|
||||
/// @throws std::string on error.
|
||||
/// @brief Search through libraries for address of a symbol
|
||||
///
|
||||
/// NOTE: This function is not thread safe.
|
||||
///
|
||||
static void *SearchForAddressOfSymbol(const char *symbolName);
|
||||
|
||||
/// @brief Convenience function for C++ophiles.
|
||||
///
|
||||
/// NOTE: This function is not thread safe.
|
||||
///
|
||||
static void *SearchForAddressOfSymbol(const std::string &symbolName) {
|
||||
return SearchForAddressOfSymbol(symbolName.c_str());
|
||||
}
|
||||
@ -66,18 +87,7 @@ namespace sys {
|
||||
/// value \p symbolValue. These symbols are searched before any
|
||||
/// libraries.
|
||||
/// @brief Add searchable symbol/value pair.
|
||||
///
|
||||
/// NOTE: This function is not thread safe.
|
||||
///
|
||||
static void AddSymbol(const char *symbolName, void *symbolValue);
|
||||
|
||||
/// @brief Convenience function for C++ophiles.
|
||||
///
|
||||
/// NOTE: This function is not thread safe.
|
||||
///
|
||||
static void AddSymbol(const std::string &symbolName, void *symbolValue) {
|
||||
AddSymbol(symbolName.c_str(), symbolValue);
|
||||
}
|
||||
static void AddSymbol(StringRef symbolName, void *symbolValue);
|
||||
};
|
||||
|
||||
} // End sys namespace
|
||||
|
@ -9,28 +9,26 @@
|
||||
//
|
||||
// This header file implements the operating system DynamicLibrary concept.
|
||||
//
|
||||
// FIXME: This file leaks the ExplicitSymbols and OpenedHandles vector, and is
|
||||
// not thread safe!
|
||||
// FIXME: This file leaks ExplicitSymbols and OpenedHandles!
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#include "llvm/ADT/StringMap.h"
|
||||
#include "llvm/ADT/DenseSet.h"
|
||||
#include "llvm/Support/DynamicLibrary.h"
|
||||
#include "llvm/Support/Mutex.h"
|
||||
#include "llvm/Config/config.h"
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
|
||||
// Collection of symbol name/value pairs to be searched prior to any libraries.
|
||||
static std::map<std::string, void*> *ExplicitSymbols = 0;
|
||||
static llvm::StringMap<void *> *ExplicitSymbols = 0;
|
||||
|
||||
namespace {
|
||||
|
||||
struct ExplicitSymbolsDeleter {
|
||||
~ExplicitSymbolsDeleter() {
|
||||
if (ExplicitSymbols)
|
||||
delete ExplicitSymbols;
|
||||
delete ExplicitSymbols;
|
||||
}
|
||||
};
|
||||
|
||||
@ -38,10 +36,17 @@ struct ExplicitSymbolsDeleter {
|
||||
|
||||
static ExplicitSymbolsDeleter Dummy;
|
||||
|
||||
void llvm::sys::DynamicLibrary::AddSymbol(const char* symbolName,
|
||||
|
||||
static llvm::sys::SmartMutex<true>& getMutex() {
|
||||
static llvm::sys::SmartMutex<true> HandlesMutex;
|
||||
return HandlesMutex;
|
||||
}
|
||||
|
||||
void llvm::sys::DynamicLibrary::AddSymbol(StringRef symbolName,
|
||||
void *symbolValue) {
|
||||
SmartScopedLock<true> lock(getMutex());
|
||||
if (ExplicitSymbols == 0)
|
||||
ExplicitSymbols = new std::map<std::string, void*>();
|
||||
ExplicitSymbols = new llvm::StringMap<void*>();
|
||||
(*ExplicitSymbols)[symbolName] = symbolValue;
|
||||
}
|
||||
|
||||
@ -61,66 +66,77 @@ using namespace llvm::sys;
|
||||
//=== independent code.
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
static std::vector<void *> *OpenedHandles = 0;
|
||||
static DenseSet<void *> *OpenedHandles = 0;
|
||||
|
||||
|
||||
static SmartMutex<true>& getMutex() {
|
||||
static SmartMutex<true> HandlesMutex;
|
||||
return HandlesMutex;
|
||||
}
|
||||
|
||||
|
||||
bool DynamicLibrary::LoadLibraryPermanently(const char *Filename,
|
||||
std::string *ErrMsg) {
|
||||
void *H = dlopen(Filename, RTLD_LAZY|RTLD_GLOBAL);
|
||||
if (H == 0) {
|
||||
if (ErrMsg) *ErrMsg = dlerror();
|
||||
return true;
|
||||
DynamicLibrary DynamicLibrary::getPermanentLibrary(const char *filename,
|
||||
std::string *errMsg) {
|
||||
void *handle = dlopen(filename, RTLD_LAZY|RTLD_GLOBAL);
|
||||
if (handle == 0) {
|
||||
if (errMsg) *errMsg = dlerror();
|
||||
return DynamicLibrary();
|
||||
}
|
||||
|
||||
#ifdef __CYGWIN__
|
||||
// Cygwin searches symbols only in the main
|
||||
// with the handle of dlopen(NULL, RTLD_GLOBAL).
|
||||
if (Filename == NULL)
|
||||
H = RTLD_DEFAULT;
|
||||
if (filename == NULL)
|
||||
handle = RTLD_DEFAULT;
|
||||
#endif
|
||||
SmartScopedLock<true> Lock(getMutex());
|
||||
|
||||
SmartScopedLock<true> lock(getMutex());
|
||||
if (OpenedHandles == 0)
|
||||
OpenedHandles = new std::vector<void *>();
|
||||
OpenedHandles->push_back(H);
|
||||
return false;
|
||||
OpenedHandles = new DenseSet<void *>();
|
||||
|
||||
// If we've already loaded this library, dlclose() the handle in order to
|
||||
// keep the internal refcount at +1.
|
||||
if (!OpenedHandles->insert(handle).second)
|
||||
dlclose(handle);
|
||||
|
||||
return DynamicLibrary(handle);
|
||||
}
|
||||
|
||||
void *DynamicLibrary::getAddressOfSymbol(const char *symbolName) {
|
||||
if (!isValid())
|
||||
return NULL;
|
||||
return dlsym(Data, symbolName);
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
using namespace llvm;
|
||||
using namespace llvm::sys;
|
||||
|
||||
bool DynamicLibrary::LoadLibraryPermanently(const char *Filename,
|
||||
std::string *ErrMsg) {
|
||||
if (ErrMsg) *ErrMsg = "dlopen() not supported on this platform";
|
||||
return true;
|
||||
DynamicLibrary DynamicLibrary::getPermanentLibrary(const char *filename,
|
||||
std::string *errMsg) {
|
||||
if (errMsg) *errMsg = "dlopen() not supported on this platform";
|
||||
return DynamicLibrary();
|
||||
}
|
||||
|
||||
void *DynamicLibrary::getAddressOfSymbol(const char *symbolName) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
namespace llvm {
|
||||
void *SearchForAddressOfSpecialSymbol(const char* symbolName);
|
||||
}
|
||||
|
||||
void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
|
||||
void* DynamicLibrary::SearchForAddressOfSymbol(const char *symbolName) {
|
||||
SmartScopedLock<true> Lock(getMutex());
|
||||
|
||||
// First check symbols added via AddSymbol().
|
||||
if (ExplicitSymbols) {
|
||||
std::map<std::string, void *>::iterator I =
|
||||
ExplicitSymbols->find(symbolName);
|
||||
std::map<std::string, void *>::iterator E = ExplicitSymbols->end();
|
||||
StringMap<void *>::iterator i = ExplicitSymbols->find(symbolName);
|
||||
|
||||
if (I != E)
|
||||
return I->second;
|
||||
if (i != ExplicitSymbols->end())
|
||||
return i->second;
|
||||
}
|
||||
|
||||
#if HAVE_DLFCN_H
|
||||
// Now search the libraries.
|
||||
SmartScopedLock<true> Lock(getMutex());
|
||||
if (OpenedHandles) {
|
||||
for (std::vector<void *>::iterator I = OpenedHandles->begin(),
|
||||
for (DenseSet<void *>::iterator I = OpenedHandles->begin(),
|
||||
E = OpenedHandles->end(); I != E; ++I) {
|
||||
//lt_ptr ptr = lt_dlsym(*I, symbolName);
|
||||
void *ptr = dlsym(*I, symbolName);
|
||||
|
@ -39,7 +39,7 @@ using namespace sys;
|
||||
//=== and must not be UNIX code.
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
static std::vector<HMODULE> OpenedHandles;
|
||||
static DenseSet<HMODULE> *OpenedHandles;
|
||||
|
||||
extern "C" {
|
||||
|
||||
@ -63,30 +63,43 @@ extern "C" {
|
||||
#endif
|
||||
stricmp(ModuleName, "msvcrt20") != 0 &&
|
||||
stricmp(ModuleName, "msvcrt40") != 0) {
|
||||
OpenedHandles.push_back((HMODULE)ModuleBase);
|
||||
OpenedHandles->push_back((HMODULE)ModuleBase);
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
}
|
||||
|
||||
bool DynamicLibrary::LoadLibraryPermanently(const char *filename,
|
||||
std::string *ErrMsg) {
|
||||
if (filename) {
|
||||
HMODULE a_handle = LoadLibrary(filename);
|
||||
DynamicLibrary DynamicLibrary::getPermanentLibrary(const char *filename,
|
||||
std::string *errMsg) {
|
||||
if (!filename) {
|
||||
// When no file is specified, enumerate all DLLs and EXEs in the process.
|
||||
SmartScopedLock<true> lock(getMutex());
|
||||
if (OpenedHandles == 0)
|
||||
OpenedHandles = new DenseSet<HMODULE>();
|
||||
|
||||
if (a_handle == 0)
|
||||
return MakeErrMsg(ErrMsg, std::string(filename) + ": Can't open : ");
|
||||
|
||||
OpenedHandles.push_back(a_handle);
|
||||
} else {
|
||||
// When no file is specified, enumerate all DLLs and EXEs in the
|
||||
// process.
|
||||
EnumerateLoadedModules(GetCurrentProcess(), ELM_Callback, 0);
|
||||
// Dummy library that represents "search all handles".
|
||||
// This is mostly to ensure that the return value still shows up as "valid".
|
||||
return DynamicLibrary(&OpenedHandles);
|
||||
}
|
||||
|
||||
HMODULE a_handle = LoadLibrary(filename);
|
||||
|
||||
if (a_handle == 0) {
|
||||
MakeErrMsg(ErrMsg, std::string(filename) + ": Can't open : ");
|
||||
return DynamicLibrary();
|
||||
}
|
||||
|
||||
// Because we don't remember the handle, we will never free it; hence,
|
||||
// it is loaded permanently.
|
||||
return false;
|
||||
SmartScopedLock<true> lock(getMutex());
|
||||
if (OpenedHandles == 0)
|
||||
OpenedHandles = new DenseSet<HMODULE>();
|
||||
|
||||
// If we've already loaded this library, FreeLibrary() the handle in order to
|
||||
// keep the internal refcount at +1.
|
||||
if (!OpenedHandles->insert(a_handle).second)
|
||||
FreeLibrary(a_handle);
|
||||
|
||||
return DynamicLibrary(a_handle);
|
||||
}
|
||||
|
||||
// Stack probing routines are in the support library (e.g. libgcc), but we don't
|
||||
@ -101,21 +114,24 @@ bool DynamicLibrary::LoadLibraryPermanently(const char *filename,
|
||||
#undef EXPLICIT_SYMBOL2
|
||||
|
||||
void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
|
||||
SmartScopedLock<true> Lock(getMutex());
|
||||
|
||||
// First check symbols added via AddSymbol().
|
||||
if (ExplicitSymbols) {
|
||||
std::map<std::string, void *>::iterator I =
|
||||
ExplicitSymbols->find(symbolName);
|
||||
std::map<std::string, void *>::iterator E = ExplicitSymbols->end();
|
||||
if (I != E)
|
||||
return I->second;
|
||||
StringMap<void *>::iterator i = ExplicitSymbols->find(symbolName);
|
||||
|
||||
if (i != ExplicitSymbols->end())
|
||||
return i->second;
|
||||
}
|
||||
|
||||
// Now search the libraries.
|
||||
for (std::vector<HMODULE>::iterator I = OpenedHandles.begin(),
|
||||
E = OpenedHandles.end(); I != E; ++I) {
|
||||
FARPROC ptr = GetProcAddress((HMODULE)*I, symbolName);
|
||||
if (ptr) {
|
||||
return (void *)(intptr_t)ptr;
|
||||
if (OpenedHandles) {
|
||||
for (DenseSet<HMODULE>::iterator I = OpenedHandles->begin(),
|
||||
E = OpenedHandles->end(); I != E; ++I) {
|
||||
FARPROC ptr = GetProcAddress((HMODULE)*I, symbolName);
|
||||
if (ptr) {
|
||||
return (void *)(intptr_t)ptr;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -134,4 +150,14 @@ void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
void *DynamicLibrary::getAddressOfSymbol(const char *symbolName) {
|
||||
if (!isValid())
|
||||
return NULL;
|
||||
if (Data == &OpenedHandles)
|
||||
return SearchForAddressOfSymbol(symbolName);
|
||||
return (void *)(intptr_t)GetProcAddress((HMODULE)Data, symbolName);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user