mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2024-08-23 02:29:18 +00:00
Guard dynamic library loading.
I did my best at implementing this for Win32, but I don't have a way to test it. Can someone with access to a Win32 machine test/fix this? git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@74188 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
cbd5f0a6b9
commit
f37feb954f
@ -12,20 +12,22 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#include "llvm/System/DynamicLibrary.h"
|
||||
#include "llvm/Support/ManagedStatic.h"
|
||||
#include "llvm/System/RWMutex.h"
|
||||
#include "llvm/Config/config.h"
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <map>
|
||||
|
||||
// Collection of symbol name/value pairs to be searched prior to any libraries.
|
||||
std::map<std::string, void *> &g_symbols() {
|
||||
static std::map<std::string, void *> symbols;
|
||||
return symbols;
|
||||
}
|
||||
static std::map<std::string, void*> symbols;
|
||||
static llvm::sys::SmartRWMutex<true> SymbolsLock;
|
||||
|
||||
|
||||
void llvm::sys::DynamicLibrary::AddSymbol(const char* symbolName,
|
||||
void *symbolValue) {
|
||||
g_symbols()[symbolName] = symbolValue;
|
||||
llvm::sys::SmartScopedWriter<true> Writer(&SymbolsLock);
|
||||
symbols[symbolName] = symbolValue;
|
||||
}
|
||||
|
||||
// It is not possible to use ltdl.c on VC++ builds as the terms of its LGPL
|
||||
@ -57,6 +59,7 @@ static std::vector<void *> OpenedHandles;
|
||||
DynamicLibrary::DynamicLibrary() {}
|
||||
|
||||
DynamicLibrary::~DynamicLibrary() {
|
||||
SmartScopedWriter<true> Writer(&SymbolsLock);
|
||||
while(!OpenedHandles.empty()) {
|
||||
void *H = OpenedHandles.back(); OpenedHandles.pop_back();
|
||||
dlclose(H);
|
||||
@ -65,6 +68,7 @@ DynamicLibrary::~DynamicLibrary() {
|
||||
|
||||
bool DynamicLibrary::LoadLibraryPermanently(const char *Filename,
|
||||
std::string *ErrMsg) {
|
||||
SmartScopedWriter<true> Writer(&SymbolsLock);
|
||||
void *H = dlopen(Filename, RTLD_LAZY|RTLD_GLOBAL);
|
||||
if (H == 0) {
|
||||
if (ErrMsg)
|
||||
@ -77,20 +81,28 @@ bool DynamicLibrary::LoadLibraryPermanently(const char *Filename,
|
||||
|
||||
void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
|
||||
// check_ltdl_initialization();
|
||||
|
||||
|
||||
// First check symbols added via AddSymbol().
|
||||
std::map<std::string, void *>::iterator I = g_symbols().find(symbolName);
|
||||
if (I != g_symbols().end())
|
||||
SymbolsLock.reader_acquire();
|
||||
std::map<std::string, void *>::iterator I = symbols.find(symbolName);
|
||||
std::map<std::string, void *>::iterator E = symbols.end();
|
||||
SymbolsLock.reader_release();
|
||||
|
||||
if (I != E)
|
||||
return I->second;
|
||||
|
||||
SymbolsLock.writer_acquire();
|
||||
// Now search the libraries.
|
||||
for (std::vector<void *>::iterator I = OpenedHandles.begin(),
|
||||
E = OpenedHandles.end(); I != E; ++I) {
|
||||
//lt_ptr ptr = lt_dlsym(*I, symbolName);
|
||||
void *ptr = dlsym(*I, symbolName);
|
||||
if (ptr)
|
||||
if (ptr) {
|
||||
SymbolsLock.writer_release();
|
||||
return ptr;
|
||||
}
|
||||
}
|
||||
SymbolsLock.writer_release();
|
||||
|
||||
#define EXPLICIT_SYMBOL(SYM) \
|
||||
extern void *SYM; if (!strcmp(symbolName, #SYM)) return &SYM
|
||||
|
@ -67,6 +67,7 @@ extern "C" {
|
||||
PVOID UserContext)
|
||||
#endif
|
||||
{
|
||||
llvm::sys::SmartScopedWriter<true> Writer(&SymbolsLock);
|
||||
// Ignore VC++ runtimes prior to 7.1. Somehow some of them get loaded
|
||||
// into the process.
|
||||
if (stricmp(ModuleName, "msvci70") != 0 &&
|
||||
@ -89,11 +90,13 @@ extern "C" {
|
||||
}
|
||||
|
||||
DynamicLibrary::DynamicLibrary() : handle(0) {
|
||||
SmartScopedWriter<true> Writer(&SymbolsLock);
|
||||
handle = GetModuleHandle(NULL);
|
||||
OpenedHandles.push_back((HMODULE)handle);
|
||||
}
|
||||
|
||||
DynamicLibrary::~DynamicLibrary() {
|
||||
llvm::sys::SmartScopedWriter<true> Writer(&SymbolsLock);
|
||||
if (handle == 0)
|
||||
return;
|
||||
|
||||
@ -113,8 +116,9 @@ DynamicLibrary::~DynamicLibrary() {
|
||||
}
|
||||
|
||||
bool DynamicLibrary::LoadLibraryPermanently(const char *filename,
|
||||
std::string *ErrMsg) {
|
||||
std::string *ErrMsg) {
|
||||
if (filename) {
|
||||
llvm::sys::SmartScopedWriter<true> Writer(&SymbolsLock);
|
||||
HMODULE a_handle = LoadLibrary(filename);
|
||||
|
||||
if (a_handle == 0)
|
||||
@ -166,17 +170,24 @@ bool DynamicLibrary::LoadLibraryPermanently(const char *filename,
|
||||
|
||||
void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
|
||||
// First check symbols added via AddSymbol().
|
||||
std::map<std::string, void *>::iterator I = g_symbols().find(symbolName);
|
||||
if (I != g_symbols().end())
|
||||
SymbolsLock.reader_acquire();
|
||||
std::map<std::string, void *>::iterator I = symbols.find(symbolName);
|
||||
std::map<std::string, void *>::iterator E = symbols.end();
|
||||
SymbolsLock.reader_release();
|
||||
if (I != E)
|
||||
return I->second;
|
||||
|
||||
// Now search the libraries.
|
||||
SymbolsLock.writer_acquire();
|
||||
for (std::vector<HMODULE>::iterator I = OpenedHandles.begin(),
|
||||
E = OpenedHandles.end(); I != E; ++I) {
|
||||
FARPROC ptr = GetProcAddress((HMODULE)*I, symbolName);
|
||||
if (ptr)
|
||||
if (ptr) {
|
||||
SymbolsLock.writer_release();
|
||||
return (void *) ptr;
|
||||
}
|
||||
}
|
||||
SymbolsLock.writer_release();
|
||||
|
||||
#if defined(__MINGW32__)
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user