2003-10-10 16:55:42 +00:00
|
|
|
//===-- DynamicLinker.cpp - Implement DynamicLinker interface -------------===//
|
2003-10-20 19:43:21 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2003-10-10 16:55:42 +00:00
|
|
|
//
|
|
|
|
// Lightweight interface to dynamic library linking and loading, and dynamic
|
|
|
|
// symbol lookup functionality, in whatever form the operating system
|
|
|
|
// provides it.
|
|
|
|
//
|
|
|
|
// Possible future extensions include support for the HPUX shl_load()
|
|
|
|
// interface, the Mac OS X NSLinkModule() interface, and the Windows
|
|
|
|
// LoadLibrary() interface.
|
|
|
|
//
|
|
|
|
// Note that we assume that if dlopen() is available, then dlsym() is too.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Support/DynamicLinker.h"
|
|
|
|
#include "Config/dlfcn.h"
|
|
|
|
#include <cassert>
|
|
|
|
|
|
|
|
bool LinkDynamicObject (const char *filename, std::string *ErrorMessage) {
|
|
|
|
#if defined (HAVE_DLOPEN)
|
|
|
|
if (dlopen (filename, RTLD_NOW | RTLD_GLOBAL) == 0) {
|
|
|
|
if (ErrorMessage) *ErrorMessage = dlerror ();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
#else
|
|
|
|
assert (0 && "Dynamic object linking not implemented for this platform");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void *GetAddressOfSymbol (const char *symbolName) {
|
|
|
|
#if defined (HAVE_DLOPEN)
|
2003-10-25 16:55:32 +00:00
|
|
|
#ifdef RTLD_DEFAULT
|
2003-10-10 16:55:42 +00:00
|
|
|
return dlsym (RTLD_DEFAULT, symbolName);
|
2003-10-25 16:55:32 +00:00
|
|
|
#else
|
|
|
|
static void* CurHandle = dlopen(0, RTLD_LAZY);
|
|
|
|
return dlsym(CurHandle, symbolName);
|
|
|
|
#endif
|
2003-10-10 16:55:42 +00:00
|
|
|
#else
|
|
|
|
assert (0 && "Dynamic symbol lookup not implemented for this platform");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// soft, cushiony C++ interface.
|
|
|
|
void *GetAddressOfSymbol (const std::string &symbolName) {
|
|
|
|
return GetAddressOfSymbol (symbolName.c_str ());
|
|
|
|
}
|