opnsense-src/lib/Support/DynamicLibrary.cpp

190 lines
4.9 KiB
C++
Raw Normal View History

2009-06-02 13:52:33 -04:00
//===-- DynamicLibrary.cpp - Runtime link/load libraries --------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This header file implements the operating system DynamicLibrary concept.
//
// FIXME: This file leaks ExplicitSymbols and OpenedHandles!
2009-10-14 13:57:32 -04:00
//
2009-06-02 13:52:33 -04:00
//===----------------------------------------------------------------------===//
#include "llvm/Support/DynamicLibrary.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/StringMap.h"
2009-06-02 13:52:33 -04:00
#include "llvm/Config/config.h"
#include "llvm/Support/Mutex.h"
2009-06-02 13:52:33 -04:00
#include <cstdio>
#include <cstring>
// Collection of symbol name/value pairs to be searched prior to any libraries.
static llvm::StringMap<void *> *ExplicitSymbols = 0;
2009-06-27 06:44:33 -04:00
2010-05-04 12:11:02 -04:00
namespace {
struct ExplicitSymbolsDeleter {
2009-10-14 13:57:32 -04:00
~ExplicitSymbolsDeleter() {
delete ExplicitSymbols;
2009-10-14 13:57:32 -04:00
}
2010-05-04 12:11:02 -04:00
};
}
static ExplicitSymbolsDeleter Dummy;
2009-06-02 13:52:33 -04:00
static llvm::sys::SmartMutex<true>& getMutex() {
static llvm::sys::SmartMutex<true> HandlesMutex;
return HandlesMutex;
}
void llvm::sys::DynamicLibrary::AddSymbol(StringRef symbolName,
2009-06-02 13:52:33 -04:00
void *symbolValue) {
SmartScopedLock<true> lock(getMutex());
2009-10-14 13:57:32 -04:00
if (ExplicitSymbols == 0)
ExplicitSymbols = new StringMap<void*>();
2009-10-14 13:57:32 -04:00
(*ExplicitSymbols)[symbolName] = symbolValue;
2009-06-02 13:52:33 -04:00
}
char llvm::sys::DynamicLibrary::Invalid = 0;
2009-06-02 13:52:33 -04:00
#ifdef LLVM_ON_WIN32
#include "Windows/DynamicLibrary.inc"
2009-06-02 13:52:33 -04:00
#else
2010-05-04 12:11:02 -04:00
#if HAVE_DLFCN_H
2009-06-02 13:52:33 -04:00
#include <dlfcn.h>
using namespace llvm;
using namespace llvm::sys;
//===----------------------------------------------------------------------===//
//=== WARNING: Implementation here must contain only TRULY operating system
//=== independent code.
//===----------------------------------------------------------------------===//
static DenseSet<void *> *OpenedHandles = 0;
DynamicLibrary DynamicLibrary::getPermanentLibrary(const char *filename,
std::string *errMsg) {
SmartScopedLock<true> lock(getMutex());
void *handle = dlopen(filename, RTLD_LAZY|RTLD_GLOBAL);
if (handle == 0) {
if (errMsg) *errMsg = dlerror();
return DynamicLibrary();
2009-06-02 13:52:33 -04:00
}
#ifdef __CYGWIN__
// Cygwin searches symbols only in the main
// with the handle of dlopen(NULL, RTLD_GLOBAL).
if (filename == NULL)
handle = RTLD_DEFAULT;
#endif
2009-10-14 13:57:32 -04:00
if (OpenedHandles == 0)
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);
2009-06-02 13:52:33 -04:00
}
2010-05-04 12:11:02 -04:00
#else
using namespace llvm;
using namespace llvm::sys;
DynamicLibrary DynamicLibrary::getPermanentLibrary(const char *filename,
std::string *errMsg) {
if (errMsg) *errMsg = "dlopen() not supported on this platform";
return DynamicLibrary();
2010-05-04 12:11:02 -04:00
}
void *DynamicLibrary::getAddressOfSymbol(const char *symbolName) {
return NULL;
}
2010-05-04 12:11:02 -04:00
#endif
2009-06-02 13:52:33 -04:00
2010-03-16 12:51:38 -04:00
namespace llvm {
void *SearchForAddressOfSpecialSymbol(const char* symbolName);
2010-01-01 05:31:22 -05:00
}
void* DynamicLibrary::SearchForAddressOfSymbol(const char *symbolName) {
SmartScopedLock<true> Lock(getMutex());
2010-01-01 05:31:22 -05:00
// First check symbols added via AddSymbol().
if (ExplicitSymbols) {
StringMap<void *>::iterator i = ExplicitSymbols->find(symbolName);
if (i != ExplicitSymbols->end())
return i->second;
2010-01-01 05:31:22 -05:00
}
2010-05-04 12:11:02 -04:00
#if HAVE_DLFCN_H
2010-01-01 05:31:22 -05:00
// Now search the libraries.
if (OpenedHandles) {
for (DenseSet<void *>::iterator I = OpenedHandles->begin(),
2010-01-01 05:31:22 -05:00
E = OpenedHandles->end(); I != E; ++I) {
//lt_ptr ptr = lt_dlsym(*I, symbolName);
void *ptr = dlsym(*I, symbolName);
if (ptr) {
return ptr;
}
}
}
2010-05-04 12:11:02 -04:00
#endif
2010-01-01 05:31:22 -05:00
2010-03-16 12:51:38 -04:00
if (void *Result = llvm::SearchForAddressOfSpecialSymbol(symbolName))
2010-01-01 05:31:22 -05:00
return Result;
2009-06-02 13:52:33 -04:00
// This macro returns the address of a well-known, explicit symbol
#define EXPLICIT_SYMBOL(SYM) \
if (!strcmp(symbolName, #SYM)) return &SYM
// On linux we have a weird situation. The stderr/out/in symbols are both
// macros and global variables because of standards requirements. So, we
2009-06-02 13:52:33 -04:00
// boldly use the EXPLICIT_SYMBOL macro without checking for a #define first.
#if defined(__linux__) and !defined(__ANDROID__)
2009-06-02 13:52:33 -04:00
{
EXPLICIT_SYMBOL(stderr);
EXPLICIT_SYMBOL(stdout);
EXPLICIT_SYMBOL(stdin);
}
#else
// For everything else, we want to check to make sure the symbol isn't defined
// as a macro before using EXPLICIT_SYMBOL.
{
#ifndef stdin
EXPLICIT_SYMBOL(stdin);
#endif
#ifndef stdout
EXPLICIT_SYMBOL(stdout);
#endif
#ifndef stderr
EXPLICIT_SYMBOL(stderr);
#endif
}
#endif
#undef EXPLICIT_SYMBOL
return 0;
}
#endif // LLVM_ON_WIN32