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
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
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
|
|
|
|
2017-04-24 11:33:30 +08:00
|
|
|
// Collection of symbol name/value pairs to be searched prior to any libraries.
|
|
|
|
static llvm::ManagedStatic<llvm::StringMap<void *> > ExplicitSymbols;
|
|
|
|
static llvm::ManagedStatic<llvm::sys::SmartMutex<true> > SymbolsMutex;
|
|
|
|
|
|
|
|
void llvm::sys::DynamicLibrary::AddSymbol(StringRef symbolName,
|
|
|
|
void *symbolValue) {
|
|
|
|
SmartScopedLock<true> lock(*SymbolsMutex);
|
|
|
|
(*ExplicitSymbols)[symbolName] = symbolValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
char llvm::sys::DynamicLibrary::Invalid = 0;
|
|
|
|
|
|
|
|
#ifdef LLVM_ON_WIN32
|
|
|
|
|
|
|
|
#include "Windows/DynamicLibrary.inc"
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#if defined(HAVE_DLFCN_H) && defined(HAVE_DLOPEN)
|
|
|
|
#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
|
|
|
|
2017-04-24 11:33:30 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//=== WARNING: Implementation here must contain only TRULY operating system
|
|
|
|
//=== independent code.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2004-11-18 12:33:39 +08:00
|
|
|
|
2017-04-24 11:33:30 +08:00
|
|
|
static llvm::ManagedStatic<DenseSet<void *> > OpenedHandles;
|
2014-08-28 01:51:43 +08:00
|
|
|
|
2017-04-24 11:33:30 +08:00
|
|
|
DynamicLibrary DynamicLibrary::getPermanentLibrary(const char *filename,
|
|
|
|
std::string *errMsg) {
|
|
|
|
SmartScopedLock<true> lock(*SymbolsMutex);
|
2011-08-17 08:29:32 +08:00
|
|
|
|
2017-04-24 11:33:30 +08:00
|
|
|
void *handle = dlopen(filename, RTLD_LAZY|RTLD_GLOBAL);
|
|
|
|
if (!handle) {
|
|
|
|
if (errMsg) *errMsg = dlerror();
|
|
|
|
return DynamicLibrary();
|
Refactor DynamicLibrary so searching for a symbol will have a defined order and
libraries are properly unloaded when llvm_shutdown is called.
Summary:
This was mostly affecting usage of the JIT, where storing the library handles in
a set made iteration unordered/undefined. This lead to disagreement between the
JIT and native code as to what the address and implementation of particularly on
Windows with stdlib functions:
JIT: putenv_s("TEST", "VALUE") // called msvcrt.dll, putenv_s
JIT: getenv("TEST") -> "VALUE" // called msvcrt.dll, getenv
Native: getenv("TEST") -> NULL // called ucrt.dll, getenv
Also fixed is the issue of DynamicLibrary::getPermanentLibrary(0,0) on Windows
not giving priority to the process' symbols as it did on Unix.
Reviewers: chapuni, v.g.vassilev, lhames
Reviewed By: lhames
Subscribers: danalbert, srhines, mgorny, vsk, llvm-commits
Differential Revision: https://reviews.llvm.org/D30107
llvm-svn: 301153
2017-04-24 10:30:12 +08:00
|
|
|
}
|
2011-08-17 08:29:32 +08:00
|
|
|
|
2017-04-24 11:33:30 +08:00
|
|
|
#ifdef __CYGWIN__
|
|
|
|
// Cygwin searches symbols only in the main
|
|
|
|
// with the handle of dlopen(NULL, RTLD_GLOBAL).
|
|
|
|
if (!filename)
|
|
|
|
handle = RTLD_DEFAULT;
|
Refactor DynamicLibrary so searching for a symbol will have a defined order and
libraries are properly unloaded when llvm_shutdown is called.
Summary:
This was mostly affecting usage of the JIT, where storing the library handles in
a set made iteration unordered/undefined. This lead to disagreement between the
JIT and native code as to what the address and implementation of particularly on
Windows with stdlib functions:
JIT: putenv_s("TEST", "VALUE") // called msvcrt.dll, putenv_s
JIT: getenv("TEST") -> "VALUE" // called msvcrt.dll, getenv
Native: getenv("TEST") -> NULL // called ucrt.dll, getenv
Also fixed is the issue of DynamicLibrary::getPermanentLibrary(0,0) on Windows
not giving priority to the process' symbols as it did on Unix.
Reviewers: chapuni, v.g.vassilev, lhames
Reviewed By: lhames
Subscribers: danalbert, srhines, mgorny, vsk, llvm-commits
Differential Revision: https://reviews.llvm.org/D30107
llvm-svn: 301153
2017-04-24 10:30:12 +08:00
|
|
|
#endif
|
2011-08-17 08:29:32 +08:00
|
|
|
|
2017-04-24 11:33:30 +08:00
|
|
|
// 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);
|
2011-08-17 08:29:32 +08:00
|
|
|
|
2017-04-24 11:33:30 +08:00
|
|
|
return DynamicLibrary(handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
DynamicLibrary DynamicLibrary::addPermanentLibrary(void *handle,
|
|
|
|
std::string *errMsg) {
|
|
|
|
SmartScopedLock<true> lock(*SymbolsMutex);
|
|
|
|
// If we've already loaded this library, tell the caller.
|
|
|
|
if (!OpenedHandles->insert(handle).second) {
|
|
|
|
if (errMsg) *errMsg = "Library already loaded";
|
|
|
|
return DynamicLibrary();
|
2017-03-03 01:56:45 +08:00
|
|
|
}
|
|
|
|
|
2017-04-24 11:33:30 +08:00
|
|
|
return DynamicLibrary(handle);
|
2017-03-03 01:56:45 +08:00
|
|
|
}
|
|
|
|
|
2017-04-24 11:33:30 +08:00
|
|
|
void *DynamicLibrary::getAddressOfSymbol(const char *symbolName) {
|
|
|
|
if (!isValid())
|
|
|
|
return nullptr;
|
|
|
|
return dlsym(Data, symbolName);
|
|
|
|
}
|
2011-08-17 08:29:32 +08:00
|
|
|
|
2010-04-10 04:45:04 +08:00
|
|
|
#else
|
|
|
|
|
2017-04-24 11:33:30 +08:00
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::sys;
|
Refactor DynamicLibrary so searching for a symbol will have a defined order and
libraries are properly unloaded when llvm_shutdown is called.
Summary:
This was mostly affecting usage of the JIT, where storing the library handles in
a set made iteration unordered/undefined. This lead to disagreement between the
JIT and native code as to what the address and implementation of particularly on
Windows with stdlib functions:
JIT: putenv_s("TEST", "VALUE") // called msvcrt.dll, putenv_s
JIT: getenv("TEST") -> "VALUE" // called msvcrt.dll, getenv
Native: getenv("TEST") -> NULL // called ucrt.dll, getenv
Also fixed is the issue of DynamicLibrary::getPermanentLibrary(0,0) on Windows
not giving priority to the process' symbols as it did on Unix.
Reviewers: chapuni, v.g.vassilev, lhames
Reviewed By: lhames
Subscribers: danalbert, srhines, mgorny, vsk, llvm-commits
Differential Revision: https://reviews.llvm.org/D30107
llvm-svn: 301153
2017-04-24 10:30:12 +08:00
|
|
|
|
2017-04-24 11:33:30 +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
|
|
|
|
2017-04-24 11:33:30 +08:00
|
|
|
void *DynamicLibrary::getAddressOfSymbol(const char *symbolName) {
|
|
|
|
return NULL;
|
2011-08-17 08:29:32 +08:00
|
|
|
}
|
|
|
|
|
2017-04-24 11:33:30 +08:00
|
|
|
#endif
|
2004-11-18 12:33:39 +08:00
|
|
|
|
2017-04-24 11:33:30 +08:00
|
|
|
namespace llvm {
|
|
|
|
void *SearchForAddressOfSpecialSymbol(const char* symbolName);
|
2009-12-24 03:12:50 +08:00
|
|
|
}
|
|
|
|
|
2017-04-24 11:33:30 +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
|
|
|
|
2017-04-24 11:33:30 +08:00
|
|
|
// First check symbols added via AddSymbol().
|
|
|
|
if (ExplicitSymbols.isConstructed()) {
|
|
|
|
StringMap<void *>::iterator i = ExplicitSymbols->find(symbolName);
|
2010-11-30 02:16:10 +08:00
|
|
|
|
2017-04-24 11:33:30 +08:00
|
|
|
if (i != ExplicitSymbols->end())
|
|
|
|
return i->second;
|
|
|
|
}
|
2009-12-24 03:12:50 +08:00
|
|
|
|
2017-04-24 11:33:30 +08:00
|
|
|
#if defined(HAVE_DLFCN_H) && defined(HAVE_DLOPEN)
|
|
|
|
// Now search the libraries.
|
|
|
|
if (OpenedHandles.isConstructed()) {
|
|
|
|
for (DenseSet<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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2009-12-24 03:12:50 +08:00
|
|
|
|
2017-04-24 11:33:30 +08:00
|
|
|
if (void *Result = llvm::SearchForAddressOfSpecialSymbol(symbolName))
|
|
|
|
return Result;
|
2007-12-03 13:30:41 +08:00
|
|
|
|
2017-04-24 11:33:30 +08:00
|
|
|
// This macro returns the address of a well-known, explicit symbol
|
|
|
|
#define EXPLICIT_SYMBOL(SYM) \
|
|
|
|
if (!strcmp(symbolName, #SYM)) return &SYM
|
2007-01-20 05:41:04 +08:00
|
|
|
|
2017-04-24 11:33:30 +08: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__) and !defined(__ANDROID__)
|
|
|
|
{
|
|
|
|
EXPLICIT_SYMBOL(stderr);
|
|
|
|
EXPLICIT_SYMBOL(stdout);
|
|
|
|
EXPLICIT_SYMBOL(stdin);
|
2007-01-11 03:50:43 +08:00
|
|
|
}
|
2017-04-24 11:33:30 +08:00
|
|
|
#else
|
|
|
|
// For everything else, we want to check to make sure the symbol isn't defined
|
|
|
|
// as a macro before using EXPLICIT_SYMBOL.
|
|
|
|
{
|
|
|
|
#ifndef stdin
|
|
|
|
EXPLICIT_SYMBOL(stdin);
|
|
|
|
#endif
|
|
|
|
#ifndef stdout
|
|
|
|
EXPLICIT_SYMBOL(stdout);
|
|
|
|
#endif
|
|
|
|
#ifndef stderr
|
|
|
|
EXPLICIT_SYMBOL(stderr);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#undef EXPLICIT_SYMBOL
|
2004-12-04 07:02:42 +08:00
|
|
|
|
2017-04-24 11:33:30 +08:00
|
|
|
return nullptr;
|
2004-11-18 12:33:39 +08:00
|
|
|
}
|
|
|
|
|
2017-04-24 11:33:30 +08:00
|
|
|
#endif // LLVM_ON_WIN32
|
|
|
|
|
2013-11-06 17:21:31 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// C API.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-04-24 11:33:30 +08:00
|
|
|
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);
|
|
|
|
}
|