mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-06-14 14:24:05 +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:
@ -28,36 +28,57 @@ namespace sys {
|
|||||||
/// It also allows for symbols to be defined which don't live in any library,
|
/// 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
|
/// but rather the main program itself, useful on Windows where the main
|
||||||
/// executable cannot be searched.
|
/// 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 {
|
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:
|
public:
|
||||||
/// This function allows a library to be loaded without instantiating a
|
/// Returns true if the object refers to a valid library.
|
||||||
/// DynamicLibrary object. Consequently, it is marked as being permanent
|
bool isValid() { return Data != 0; }
|
||||||
/// and will only be unloaded when the program terminates. This returns
|
|
||||||
/// false on success or returns true and fills in *ErrMsg on failure.
|
/// 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.
|
/// @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.
|
/// It is safe to call this function multiple times for the same library.
|
||||||
///
|
static bool LoadLibraryPermanently(const char *Filename,
|
||||||
static bool LoadLibraryPermanently(const char *filename,
|
std::string *ErrMsg = 0) {
|
||||||
std::string *ErrMsg = 0);
|
return !getPermanentLibrary(Filename, ErrMsg).isValid();
|
||||||
|
}
|
||||||
|
|
||||||
/// This function will search through all previously loaded dynamic
|
/// 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
|
/// that symbol is returned. If not, null is returned. Note that this will
|
||||||
/// search permanently loaded libraries (LoadLibraryPermanently) as well
|
/// search permanently loaded libraries (getPermanentLibrary()) as well
|
||||||
/// as ephemerally loaded libraries (constructors).
|
/// as explicitly registered symbols (AddSymbol()).
|
||||||
/// @throws std::string on error.
|
/// @throws std::string on error.
|
||||||
/// @brief Search through libraries for address of a symbol
|
/// @brief Search through libraries for address of a symbol
|
||||||
///
|
|
||||||
/// NOTE: This function is not thread safe.
|
|
||||||
///
|
|
||||||
static void *SearchForAddressOfSymbol(const char *symbolName);
|
static void *SearchForAddressOfSymbol(const char *symbolName);
|
||||||
|
|
||||||
/// @brief Convenience function for C++ophiles.
|
/// @brief Convenience function for C++ophiles.
|
||||||
///
|
|
||||||
/// NOTE: This function is not thread safe.
|
|
||||||
///
|
|
||||||
static void *SearchForAddressOfSymbol(const std::string &symbolName) {
|
static void *SearchForAddressOfSymbol(const std::string &symbolName) {
|
||||||
return SearchForAddressOfSymbol(symbolName.c_str());
|
return SearchForAddressOfSymbol(symbolName.c_str());
|
||||||
}
|
}
|
||||||
@ -66,18 +87,7 @@ namespace sys {
|
|||||||
/// value \p symbolValue. These symbols are searched before any
|
/// value \p symbolValue. These symbols are searched before any
|
||||||
/// libraries.
|
/// libraries.
|
||||||
/// @brief Add searchable symbol/value pair.
|
/// @brief Add searchable symbol/value pair.
|
||||||
///
|
static void AddSymbol(StringRef symbolName, void *symbolValue);
|
||||||
/// 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);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // End sys namespace
|
} // End sys namespace
|
||||||
|
@ -9,28 +9,26 @@
|
|||||||
//
|
//
|
||||||
// This header file implements the operating system DynamicLibrary concept.
|
// This header file implements the operating system DynamicLibrary concept.
|
||||||
//
|
//
|
||||||
// FIXME: This file leaks the ExplicitSymbols and OpenedHandles vector, and is
|
// FIXME: This file leaks ExplicitSymbols and OpenedHandles!
|
||||||
// not thread safe!
|
|
||||||
//
|
//
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
|
#include "llvm/ADT/StringMap.h"
|
||||||
|
#include "llvm/ADT/DenseSet.h"
|
||||||
#include "llvm/Support/DynamicLibrary.h"
|
#include "llvm/Support/DynamicLibrary.h"
|
||||||
#include "llvm/Support/Mutex.h"
|
#include "llvm/Support/Mutex.h"
|
||||||
#include "llvm/Config/config.h"
|
#include "llvm/Config/config.h"
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <map>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
// Collection of symbol name/value pairs to be searched prior to any libraries.
|
// 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 {
|
namespace {
|
||||||
|
|
||||||
struct ExplicitSymbolsDeleter {
|
struct ExplicitSymbolsDeleter {
|
||||||
~ExplicitSymbolsDeleter() {
|
~ExplicitSymbolsDeleter() {
|
||||||
if (ExplicitSymbols)
|
delete ExplicitSymbols;
|
||||||
delete ExplicitSymbols;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -38,10 +36,17 @@ struct ExplicitSymbolsDeleter {
|
|||||||
|
|
||||||
static ExplicitSymbolsDeleter Dummy;
|
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) {
|
void *symbolValue) {
|
||||||
|
SmartScopedLock<true> lock(getMutex());
|
||||||
if (ExplicitSymbols == 0)
|
if (ExplicitSymbols == 0)
|
||||||
ExplicitSymbols = new std::map<std::string, void*>();
|
ExplicitSymbols = new llvm::StringMap<void*>();
|
||||||
(*ExplicitSymbols)[symbolName] = symbolValue;
|
(*ExplicitSymbols)[symbolName] = symbolValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -61,66 +66,77 @@ using namespace llvm::sys;
|
|||||||
//=== independent code.
|
//=== independent code.
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
static std::vector<void *> *OpenedHandles = 0;
|
static DenseSet<void *> *OpenedHandles = 0;
|
||||||
|
|
||||||
|
DynamicLibrary DynamicLibrary::getPermanentLibrary(const char *filename,
|
||||||
static SmartMutex<true>& getMutex() {
|
std::string *errMsg) {
|
||||||
static SmartMutex<true> HandlesMutex;
|
void *handle = dlopen(filename, RTLD_LAZY|RTLD_GLOBAL);
|
||||||
return HandlesMutex;
|
if (handle == 0) {
|
||||||
}
|
if (errMsg) *errMsg = dlerror();
|
||||||
|
return DynamicLibrary();
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef __CYGWIN__
|
#ifdef __CYGWIN__
|
||||||
// Cygwin searches symbols only in the main
|
// Cygwin searches symbols only in the main
|
||||||
// with the handle of dlopen(NULL, RTLD_GLOBAL).
|
// with the handle of dlopen(NULL, RTLD_GLOBAL).
|
||||||
if (Filename == NULL)
|
if (filename == NULL)
|
||||||
H = RTLD_DEFAULT;
|
handle = RTLD_DEFAULT;
|
||||||
#endif
|
#endif
|
||||||
SmartScopedLock<true> Lock(getMutex());
|
|
||||||
|
SmartScopedLock<true> lock(getMutex());
|
||||||
if (OpenedHandles == 0)
|
if (OpenedHandles == 0)
|
||||||
OpenedHandles = new std::vector<void *>();
|
OpenedHandles = new DenseSet<void *>();
|
||||||
OpenedHandles->push_back(H);
|
|
||||||
return false;
|
// 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
|
#else
|
||||||
|
|
||||||
using namespace llvm;
|
using namespace llvm;
|
||||||
using namespace llvm::sys;
|
using namespace llvm::sys;
|
||||||
|
|
||||||
bool DynamicLibrary::LoadLibraryPermanently(const char *Filename,
|
DynamicLibrary DynamicLibrary::getPermanentLibrary(const char *filename,
|
||||||
std::string *ErrMsg) {
|
std::string *errMsg) {
|
||||||
if (ErrMsg) *ErrMsg = "dlopen() not supported on this platform";
|
if (errMsg) *errMsg = "dlopen() not supported on this platform";
|
||||||
return true;
|
return DynamicLibrary();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void *DynamicLibrary::getAddressOfSymbol(const char *symbolName) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
namespace llvm {
|
namespace llvm {
|
||||||
void *SearchForAddressOfSpecialSymbol(const char* symbolName);
|
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().
|
// First check symbols added via AddSymbol().
|
||||||
if (ExplicitSymbols) {
|
if (ExplicitSymbols) {
|
||||||
std::map<std::string, void *>::iterator I =
|
StringMap<void *>::iterator i = ExplicitSymbols->find(symbolName);
|
||||||
ExplicitSymbols->find(symbolName);
|
|
||||||
std::map<std::string, void *>::iterator E = ExplicitSymbols->end();
|
|
||||||
|
|
||||||
if (I != E)
|
if (i != ExplicitSymbols->end())
|
||||||
return I->second;
|
return i->second;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if HAVE_DLFCN_H
|
#if HAVE_DLFCN_H
|
||||||
// Now search the libraries.
|
// Now search the libraries.
|
||||||
SmartScopedLock<true> Lock(getMutex());
|
|
||||||
if (OpenedHandles) {
|
if (OpenedHandles) {
|
||||||
for (std::vector<void *>::iterator I = OpenedHandles->begin(),
|
for (DenseSet<void *>::iterator I = OpenedHandles->begin(),
|
||||||
E = OpenedHandles->end(); I != E; ++I) {
|
E = OpenedHandles->end(); I != E; ++I) {
|
||||||
//lt_ptr ptr = lt_dlsym(*I, symbolName);
|
//lt_ptr ptr = lt_dlsym(*I, symbolName);
|
||||||
void *ptr = dlsym(*I, symbolName);
|
void *ptr = dlsym(*I, symbolName);
|
||||||
|
@ -39,7 +39,7 @@ using namespace sys;
|
|||||||
//=== and must not be UNIX code.
|
//=== and must not be UNIX code.
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
static std::vector<HMODULE> OpenedHandles;
|
static DenseSet<HMODULE> *OpenedHandles;
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
|
|
||||||
@ -63,30 +63,43 @@ extern "C" {
|
|||||||
#endif
|
#endif
|
||||||
stricmp(ModuleName, "msvcrt20") != 0 &&
|
stricmp(ModuleName, "msvcrt20") != 0 &&
|
||||||
stricmp(ModuleName, "msvcrt40") != 0) {
|
stricmp(ModuleName, "msvcrt40") != 0) {
|
||||||
OpenedHandles.push_back((HMODULE)ModuleBase);
|
OpenedHandles->push_back((HMODULE)ModuleBase);
|
||||||
}
|
}
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool DynamicLibrary::LoadLibraryPermanently(const char *filename,
|
DynamicLibrary DynamicLibrary::getPermanentLibrary(const char *filename,
|
||||||
std::string *ErrMsg) {
|
std::string *errMsg) {
|
||||||
if (filename) {
|
if (!filename) {
|
||||||
HMODULE a_handle = LoadLibrary(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);
|
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,
|
SmartScopedLock<true> lock(getMutex());
|
||||||
// it is loaded permanently.
|
if (OpenedHandles == 0)
|
||||||
return false;
|
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
|
// 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
|
#undef EXPLICIT_SYMBOL2
|
||||||
|
|
||||||
void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
|
void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
|
||||||
|
SmartScopedLock<true> Lock(getMutex());
|
||||||
|
|
||||||
// First check symbols added via AddSymbol().
|
// First check symbols added via AddSymbol().
|
||||||
if (ExplicitSymbols) {
|
if (ExplicitSymbols) {
|
||||||
std::map<std::string, void *>::iterator I =
|
StringMap<void *>::iterator i = ExplicitSymbols->find(symbolName);
|
||||||
ExplicitSymbols->find(symbolName);
|
|
||||||
std::map<std::string, void *>::iterator E = ExplicitSymbols->end();
|
if (i != ExplicitSymbols->end())
|
||||||
if (I != E)
|
return i->second;
|
||||||
return I->second;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now search the libraries.
|
// Now search the libraries.
|
||||||
for (std::vector<HMODULE>::iterator I = OpenedHandles.begin(),
|
if (OpenedHandles) {
|
||||||
E = OpenedHandles.end(); I != E; ++I) {
|
for (DenseSet<HMODULE>::iterator I = OpenedHandles->begin(),
|
||||||
FARPROC ptr = GetProcAddress((HMODULE)*I, symbolName);
|
E = OpenedHandles->end(); I != E; ++I) {
|
||||||
if (ptr) {
|
FARPROC ptr = GetProcAddress((HMODULE)*I, symbolName);
|
||||||
return (void *)(intptr_t)ptr;
|
if (ptr) {
|
||||||
|
return (void *)(intptr_t)ptr;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -134,4 +150,14 @@ void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
|
|||||||
return 0;
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user