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"
|
2009-06-25 18:12:44 +00:00
|
|
|
#include "llvm/Support/ManagedStatic.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
|
|
|
|
2009-08-31 16:12:29 +00:00
|
|
|
static struct ExplicitSymbolsDeleter {
|
|
|
|
~ExplicitSymbolsDeleter() {
|
|
|
|
if (ExplicitSymbols)
|
|
|
|
delete ExplicitSymbols;
|
|
|
|
}
|
|
|
|
} Dummy;
|
|
|
|
|
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
|
|
|
|
|
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
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
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;
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2004-11-29 13:33:28 +00:00
|
|
|
void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
|
2006-01-30 04:33:51 +00:00
|
|
|
// First check symbols added via AddSymbol().
|
2009-07-07 18:17:07 +00:00
|
|
|
if (ExplicitSymbols) {
|
|
|
|
std::map<std::string, void *>::iterator I =
|
|
|
|
ExplicitSymbols->find(symbolName);
|
|
|
|
std::map<std::string, void *>::iterator E = ExplicitSymbols->end();
|
2009-06-25 18:12:44 +00:00
|
|
|
|
2009-07-07 18:17:07 +00:00
|
|
|
if (I != E)
|
|
|
|
return I->second;
|
|
|
|
}
|
2006-01-30 04:33:51 +00:00
|
|
|
|
|
|
|
// Now search the libraries.
|
2009-07-07 18:17:07 +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;
|
|
|
|
}
|
2009-06-25 18:12:44 +00:00
|
|
|
}
|
2004-11-29 13:33:28 +00:00
|
|
|
}
|
2004-12-03 23:02:42 +00:00
|
|
|
|
2007-12-03 05:30:41 +00:00
|
|
|
#define EXPLICIT_SYMBOL(SYM) \
|
|
|
|
extern void *SYM; if (!strcmp(symbolName, #SYM)) return &SYM
|
|
|
|
|
2004-12-03 23:02:42 +00:00
|
|
|
// If this is darwin, it has some funky issues, try to solve them here. Some
|
|
|
|
// important symbols are marked 'private external' which doesn't allow
|
|
|
|
// SearchForAddressOfSymbol to find them. As such, we special case them here,
|
|
|
|
// there is only a small handful of them.
|
2007-01-10 19:50:43 +00:00
|
|
|
|
2004-12-03 23:02:42 +00:00
|
|
|
#ifdef __APPLE__
|
2007-01-10 19:50:43 +00:00
|
|
|
{
|
2004-12-04 04:17:20 +00:00
|
|
|
EXPLICIT_SYMBOL(__ashldi3);
|
|
|
|
EXPLICIT_SYMBOL(__ashrdi3);
|
|
|
|
EXPLICIT_SYMBOL(__cmpdi2);
|
|
|
|
EXPLICIT_SYMBOL(__divdi3);
|
|
|
|
EXPLICIT_SYMBOL(__eprintf);
|
|
|
|
EXPLICIT_SYMBOL(__fixdfdi);
|
|
|
|
EXPLICIT_SYMBOL(__fixsfdi);
|
|
|
|
EXPLICIT_SYMBOL(__fixunsdfdi);
|
|
|
|
EXPLICIT_SYMBOL(__fixunssfdi);
|
|
|
|
EXPLICIT_SYMBOL(__floatdidf);
|
|
|
|
EXPLICIT_SYMBOL(__floatdisf);
|
|
|
|
EXPLICIT_SYMBOL(__lshrdi3);
|
|
|
|
EXPLICIT_SYMBOL(__moddi3);
|
|
|
|
EXPLICIT_SYMBOL(__udivdi3);
|
|
|
|
EXPLICIT_SYMBOL(__umoddi3);
|
2004-12-03 23:02:42 +00:00
|
|
|
}
|
|
|
|
#endif
|
2007-01-19 21:41:04 +00:00
|
|
|
|
2007-12-03 05:30:41 +00:00
|
|
|
#ifdef __CYGWIN__
|
|
|
|
{
|
|
|
|
EXPLICIT_SYMBOL(_alloca);
|
2008-02-22 10:08:31 +00:00
|
|
|
EXPLICIT_SYMBOL(__main);
|
2007-12-03 05:30:41 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#undef EXPLICIT_SYMBOL
|
|
|
|
|
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
|