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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/System/DynamicLibrary.h"
|
2004-12-24 16:26:47 +00:00
|
|
|
#include "llvm/Config/config.h"
|
2008-01-09 19:42:09 +00:00
|
|
|
#include <cstring>
|
2006-01-30 04:33:51 +00:00
|
|
|
#include <map>
|
|
|
|
|
|
|
|
// Collection of symbol name/value pairs to be searched prior to any libraries.
|
|
|
|
static std::map<std::string, void *> g_symbols;
|
|
|
|
|
2006-07-07 17:12:36 +00:00
|
|
|
void llvm::sys::DynamicLibrary::AddSymbol(const char* symbolName,
|
|
|
|
void *symbolValue) {
|
2006-01-30 04:33:51 +00:00
|
|
|
g_symbols[symbolName] = symbolValue;
|
|
|
|
}
|
2004-12-24 07:57:09 +00:00
|
|
|
|
|
|
|
// It is not possible to use ltdl.c on VC++ builds as the terms of its LGPL
|
|
|
|
// license and special exception would cause all of LLVM to be placed under
|
|
|
|
// the LGPL. This is because the exception applies only when libtool is
|
|
|
|
// used, and obviously libtool is not used with Visual Studio. An entirely
|
|
|
|
// separate implementation is provided in win32/DynamicLibrary.cpp.
|
|
|
|
|
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 "ltdl.h"
|
|
|
|
#include <dlfcn.h>
|
2004-11-18 04:33:39 +00:00
|
|
|
#include <cassert>
|
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
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-02-13 17:11:39 +00:00
|
|
|
//static std::vector<lt_dlhandle> OpenedHandles;
|
|
|
|
static std::vector<void *> OpenedHandles;
|
2004-11-18 04:33:39 +00:00
|
|
|
|
2008-03-13 16:55:34 +00:00
|
|
|
DynamicLibrary::DynamicLibrary() {}
|
2004-11-18 04:33:39 +00:00
|
|
|
|
|
|
|
DynamicLibrary::~DynamicLibrary() {
|
2008-02-13 17:11:39 +00:00
|
|
|
while(!OpenedHandles.empty()) {
|
|
|
|
void *H = OpenedHandles.back(); OpenedHandles.pop_back();
|
|
|
|
dlclose(H);
|
2004-11-29 13:33:28 +00:00
|
|
|
}
|
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) {
|
2008-03-12 00:50:01 +00:00
|
|
|
if (ErrMsg)
|
|
|
|
*ErrMsg = dlerror();
|
2006-07-07 17:12:36 +00:00
|
|
|
return true;
|
|
|
|
}
|
2008-02-13 17:11:39 +00:00
|
|
|
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) {
|
2008-02-13 17:11:39 +00:00
|
|
|
// check_ltdl_initialization();
|
2006-01-30 04:33:51 +00:00
|
|
|
|
|
|
|
// First check symbols added via AddSymbol().
|
|
|
|
std::map<std::string, void *>::iterator I = g_symbols.find(symbolName);
|
|
|
|
if (I != g_symbols.end())
|
|
|
|
return I->second;
|
|
|
|
|
|
|
|
// Now search the libraries.
|
2008-02-13 17:11:39 +00:00
|
|
|
for (std::vector<void *>::iterator I = OpenedHandles.begin(),
|
2004-11-29 13:33:28 +00:00
|
|
|
E = OpenedHandles.end(); I != E; ++I) {
|
2008-02-13 17:11:39 +00:00
|
|
|
//lt_ptr ptr = lt_dlsym(*I, symbolName);
|
|
|
|
void *ptr = dlsym(*I, symbolName);
|
2004-11-29 13:33:28 +00:00
|
|
|
if (ptr)
|
|
|
|
return ptr;
|
|
|
|
}
|
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
|