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