2004-11-18 04:33:39 +00:00
|
|
|
//===-- DynamicLibrary.cpp - Runtime link/load libraries --------*- C++ -*-===//
|
2005-04-21 22:55:34 +00:00
|
|
|
//
|
2004-11-18 04:33:39 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 22:55:34 +00:00
|
|
|
//
|
2004-11-18 04:33:39 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This header file implements the operating system DynamicLibrary concept.
|
|
|
|
//
|
2009-07-07 18:17:07 +00:00
|
|
|
// FIXME: This file leaks the ExplicitSymbols and OpenedHandles vector, and is
|
|
|
|
// not thread safe!
|
|
|
|
//
|
2004-11-18 04:33:39 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/System/DynamicLibrary.h"
|
2010-11-09 00:36:06 +00:00
|
|
|
#include "llvm/System/Mutex.h"
|
2004-12-24 16:26:47 +00:00
|
|
|
#include "llvm/Config/config.h"
|
2008-10-08 07:23:46 +00:00
|
|
|
#include <cstdio>
|
2008-01-09 19:42:09 +00:00
|
|
|
#include <cstring>
|
2006-01-30 04:33:51 +00:00
|
|
|
#include <map>
|
2009-07-07 18:01:58 +00:00
|
|
|
#include <vector>
|
2006-01-30 04:33:51 +00:00
|
|
|
|
|
|
|
// Collection of symbol name/value pairs to be searched prior to any libraries.
|
2009-07-07 18:17:07 +00:00
|
|
|
static std::map<std::string, void*> *ExplicitSymbols = 0;
|
2009-06-25 18:12:44 +00:00
|
|
|
|
2010-04-15 17:08:50 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
struct ExplicitSymbolsDeleter {
|
2009-08-31 16:12:29 +00:00
|
|
|
~ExplicitSymbolsDeleter() {
|
|
|
|
if (ExplicitSymbols)
|
|
|
|
delete ExplicitSymbols;
|
|
|
|
}
|
2010-04-15 17:08:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static ExplicitSymbolsDeleter Dummy;
|
2009-08-31 16:12:29 +00:00
|
|
|
|
2006-07-07 17:12:36 +00:00
|
|
|
void llvm::sys::DynamicLibrary::AddSymbol(const char* symbolName,
|
|
|
|
void *symbolValue) {
|
2009-07-07 18:17:07 +00:00
|
|
|
if (ExplicitSymbols == 0)
|
|
|
|
ExplicitSymbols = new std::map<std::string, void*>();
|
|
|
|
(*ExplicitSymbols)[symbolName] = symbolValue;
|
2006-01-30 04:33:51 +00:00
|
|
|
}
|
2004-12-24 07:57:09 +00:00
|
|
|
|
2004-12-24 16:26:47 +00:00
|
|
|
#ifdef LLVM_ON_WIN32
|
2004-12-24 07:57:09 +00:00
|
|
|
|
2005-01-09 23:29:00 +00:00
|
|
|
#include "Win32/DynamicLibrary.inc"
|
2004-12-24 07:57:09 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
2010-04-09 20:45:04 +00:00
|
|
|
#if HAVE_DLFCN_H
|
2008-02-13 17:11:39 +00:00
|
|
|
#include <dlfcn.h>
|
2004-12-03 23:02:42 +00:00
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::sys;
|
2004-11-18 04:33:39 +00:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//=== WARNING: Implementation here must contain only TRULY operating system
|
2005-04-21 22:55:34 +00:00
|
|
|
//=== independent code.
|
2004-11-18 04:33:39 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-11-09 00:36:06 +00:00
|
|
|
static SmartMutex<true> HandlesMutex;
|
2009-07-07 18:17:07 +00:00
|
|
|
static std::vector<void *> *OpenedHandles = 0;
|
2004-11-18 04:33:39 +00:00
|
|
|
|
|
|
|
|
2006-07-07 17:12:36 +00:00
|
|
|
bool DynamicLibrary::LoadLibraryPermanently(const char *Filename,
|
|
|
|
std::string *ErrMsg) {
|
2008-07-10 00:52:20 +00:00
|
|
|
void *H = dlopen(Filename, RTLD_LAZY|RTLD_GLOBAL);
|
2008-02-13 17:11:39 +00:00
|
|
|
if (H == 0) {
|
2009-07-07 18:17:07 +00:00
|
|
|
if (ErrMsg) *ErrMsg = dlerror();
|
2006-07-07 17:12:36 +00:00
|
|
|
return true;
|
|
|
|
}
|
2010-08-17 15:42:43 +00:00
|
|
|
#ifdef __CYGWIN__
|
|
|
|
// Cygwin searches symbols only in the main
|
|
|
|
// with the handle of dlopen(NULL, RTLD_GLOBAL).
|
|
|
|
if (Filename == NULL)
|
|
|
|
H = RTLD_DEFAULT;
|
|
|
|
#endif
|
2010-11-09 00:36:06 +00:00
|
|
|
SmartScopedLock<true> Lock(HandlesMutex);
|
2009-07-07 18:17:07 +00:00
|
|
|
if (OpenedHandles == 0)
|
|
|
|
OpenedHandles = new std::vector<void *>();
|
|
|
|
OpenedHandles->push_back(H);
|
2006-07-07 17:12:36 +00:00
|
|
|
return false;
|
2004-11-18 04:33:39 +00:00
|
|
|
}
|
2010-04-09 20:45:04 +00:00
|
|
|
#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;
|
|
|
|
}
|
|
|
|
#endif
|
2004-11-18 04:33:39 +00:00
|
|
|
|
2010-03-11 06:14:32 +00:00
|
|
|
namespace llvm {
|
|
|
|
void *SearchForAddressOfSpecialSymbol(const char* symbolName);
|
2009-12-23 19:12:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2010-04-09 20:45:04 +00:00
|
|
|
#if HAVE_DLFCN_H
|
2009-12-23 19:12:50 +00:00
|
|
|
// Now search the libraries.
|
2010-11-09 00:36:06 +00:00
|
|
|
SmartScopedLock<true> Lock(HandlesMutex);
|
2009-12-23 19:12:50 +00:00
|
|
|
if (OpenedHandles) {
|
|
|
|
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) {
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-04-09 20:45:04 +00:00
|
|
|
#endif
|
2009-12-23 19:12:50 +00:00
|
|
|
|
2010-03-11 06:14:32 +00:00
|
|
|
if (void *Result = llvm::SearchForAddressOfSpecialSymbol(symbolName))
|
2009-12-23 19:12:50 +00:00
|
|
|
return Result;
|
2007-12-03 05:30:41 +00:00
|
|
|
|
2007-01-19 21:41:04 +00:00
|
|
|
// This macro returns the address of a well-known, explicit symbol
|
2007-01-10 19:50:43 +00:00
|
|
|
#define EXPLICIT_SYMBOL(SYM) \
|
|
|
|
if (!strcmp(symbolName, #SYM)) return &SYM
|
2007-01-19 21:41:04 +00:00
|
|
|
|
|
|
|
// On linux we have a weird situation. The stderr/out/in symbols are both
|
|
|
|
// macros and global variables because of standards requirements. So, we
|
|
|
|
// boldly use the EXPLICIT_SYMBOL macro without checking for a #define first.
|
|
|
|
#if defined(__linux__)
|
|
|
|
{
|
|
|
|
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.
|
2007-01-10 19:50:43 +00:00
|
|
|
{
|
2007-01-11 00:35:10 +00:00
|
|
|
#ifndef stdin
|
2007-01-10 19:50:43 +00:00
|
|
|
EXPLICIT_SYMBOL(stdin);
|
2007-01-19 21:30:39 +00:00
|
|
|
#endif
|
|
|
|
#ifndef stdout
|
2007-01-10 19:50:43 +00:00
|
|
|
EXPLICIT_SYMBOL(stdout);
|
2007-01-19 21:30:39 +00:00
|
|
|
#endif
|
|
|
|
#ifndef stderr
|
2007-01-10 19:50:43 +00:00
|
|
|
EXPLICIT_SYMBOL(stderr);
|
2007-01-11 00:35:10 +00:00
|
|
|
#endif
|
2007-01-10 19:50:43 +00:00
|
|
|
}
|
2007-01-19 21:41:04 +00:00
|
|
|
#endif
|
2007-01-10 19:50:43 +00:00
|
|
|
#undef EXPLICIT_SYMBOL
|
2004-12-03 23:02:42 +00:00
|
|
|
|
2004-11-18 04:33:39 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-12-24 16:26:47 +00:00
|
|
|
#endif // LLVM_ON_WIN32
|