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