2010-11-29 19:44:50 +00:00
|
|
|
//===-- llvm/Support/DynamicLibrary.h - Portable Dynamic Library -*- C++ -*-===//
|
2005-04-21 20:48:15 +00:00
|
|
|
//
|
2004-11-18 04:33:39 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 19:59:42 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 20:48:15 +00:00
|
|
|
//
|
2004-11-18 04:33:39 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file declares the sys::DynamicLibrary class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-01-10 00:45:19 +00:00
|
|
|
#ifndef LLVM_SYSTEM_DYNAMICLIBRARY_H
|
|
|
|
#define LLVM_SYSTEM_DYNAMICLIBRARY_H
|
2004-11-18 04:33:39 +00:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
namespace llvm {
|
2011-11-04 18:30:30 +00:00
|
|
|
|
|
|
|
class StringRef;
|
|
|
|
|
2004-11-18 04:33:39 +00:00
|
|
|
namespace sys {
|
|
|
|
|
|
|
|
/// This class provides a portable interface to dynamic libraries which also
|
2005-04-21 20:48:15 +00:00
|
|
|
/// might be known as shared libraries, shared objects, dynamic shared
|
2004-11-18 04:33:39 +00:00
|
|
|
/// objects, or dynamic link libraries. Regardless of the terminology or the
|
|
|
|
/// operating system interface, this class provides a portable interface that
|
2010-02-10 16:03:48 +00:00
|
|
|
/// allows dynamic libraries to be loaded and searched for externally
|
2004-11-18 04:33:39 +00:00
|
|
|
/// defined symbols. This is typically used to provide "plug-in" support.
|
2006-01-30 04:33:51 +00:00
|
|
|
/// It also allows for symbols to be defined which don't live in any library,
|
|
|
|
/// but rather the main program itself, useful on Windows where the main
|
|
|
|
/// executable cannot be searched.
|
2011-08-17 00:29:32 +00:00
|
|
|
///
|
|
|
|
/// Note: there is currently no interface for temporarily loading a library,
|
|
|
|
/// or for unloading libraries when the LLVM library is unloaded.
|
2004-11-18 04:33:39 +00:00
|
|
|
class DynamicLibrary {
|
2011-08-17 18:23:17 +00:00
|
|
|
// Placeholder whose address represents an invalid library.
|
|
|
|
// We use this instead of NULL or a pointer-int pair because the OS library
|
|
|
|
// might define 0 or 1 to be "special" handles, such as "search all".
|
2011-08-17 18:28:14 +00:00
|
|
|
static char Invalid;
|
2011-08-17 18:23:17 +00:00
|
|
|
|
2011-08-17 00:29:32 +00:00
|
|
|
// Opaque data used to interface with OS-specific dynamic library handling.
|
|
|
|
void *Data;
|
|
|
|
|
2011-08-17 18:23:17 +00:00
|
|
|
explicit DynamicLibrary(void *data = &Invalid) : Data(data) {}
|
2009-07-07 18:01:58 +00:00
|
|
|
public:
|
2011-08-17 00:29:32 +00:00
|
|
|
/// Returns true if the object refers to a valid library.
|
2011-08-17 18:23:17 +00:00
|
|
|
bool isValid() { return Data != &Invalid; }
|
2011-08-17 00:29:32 +00:00
|
|
|
|
|
|
|
/// Searches through the library for the symbol \p symbolName. If it is
|
|
|
|
/// found, the address of that symbol is returned. If not, NULL is returned.
|
|
|
|
/// Note that NULL will also be returned if the library failed to load.
|
|
|
|
/// Use isValid() to distinguish these cases if it is important.
|
|
|
|
/// Note that this will \e not search symbols explicitly registered by
|
|
|
|
/// AddSymbol().
|
|
|
|
void *getAddressOfSymbol(const char *symbolName);
|
|
|
|
|
|
|
|
/// This function permanently loads the dynamic library at the given path.
|
|
|
|
/// The library will only be unloaded when the program terminates.
|
|
|
|
/// This returns a valid DynamicLibrary instance on success and an invalid
|
|
|
|
/// instance on failure (see isValid()). \p *errMsg will only be modified
|
|
|
|
/// if the library fails to load.
|
2009-07-07 18:17:07 +00:00
|
|
|
///
|
2011-08-17 00:29:32 +00:00
|
|
|
/// It is safe to call this function multiple times for the same library.
|
|
|
|
/// @brief Open a dynamic library permanently.
|
|
|
|
static DynamicLibrary getPermanentLibrary(const char *filename,
|
|
|
|
std::string *errMsg = 0);
|
|
|
|
|
|
|
|
/// This function permanently loads the dynamic library at the given path.
|
|
|
|
/// Use this instead of getPermanentLibrary() when you won't need to get
|
|
|
|
/// symbols from the library itself.
|
2009-07-07 18:17:07 +00:00
|
|
|
///
|
2011-08-17 00:29:32 +00:00
|
|
|
/// It is safe to call this function multiple times for the same library.
|
|
|
|
static bool LoadLibraryPermanently(const char *Filename,
|
|
|
|
std::string *ErrMsg = 0) {
|
|
|
|
return !getPermanentLibrary(Filename, ErrMsg).isValid();
|
|
|
|
}
|
2004-11-29 10:38:54 +00:00
|
|
|
|
2009-07-07 18:01:58 +00:00
|
|
|
/// This function will search through all previously loaded dynamic
|
2011-08-17 00:29:32 +00:00
|
|
|
/// libraries for the symbol \p symbolName. If it is found, the address of
|
2009-07-07 18:01:58 +00:00
|
|
|
/// that symbol is returned. If not, null is returned. Note that this will
|
2011-08-17 00:29:32 +00:00
|
|
|
/// search permanently loaded libraries (getPermanentLibrary()) as well
|
|
|
|
/// as explicitly registered symbols (AddSymbol()).
|
2009-07-07 18:01:58 +00:00
|
|
|
/// @throws std::string on error.
|
|
|
|
/// @brief Search through libraries for address of a symbol
|
|
|
|
static void *SearchForAddressOfSymbol(const char *symbolName);
|
2004-11-18 04:33:39 +00:00
|
|
|
|
2009-07-07 18:01:58 +00:00
|
|
|
/// @brief Convenience function for C++ophiles.
|
|
|
|
static void *SearchForAddressOfSymbol(const std::string &symbolName) {
|
|
|
|
return SearchForAddressOfSymbol(symbolName.c_str());
|
|
|
|
}
|
2004-11-29 13:27:56 +00:00
|
|
|
|
2009-07-07 18:01:58 +00:00
|
|
|
/// This functions permanently adds the symbol \p symbolName with the
|
|
|
|
/// value \p symbolValue. These symbols are searched before any
|
|
|
|
/// libraries.
|
|
|
|
/// @brief Add searchable symbol/value pair.
|
2011-08-17 00:29:32 +00:00
|
|
|
static void AddSymbol(StringRef symbolName, void *symbolValue);
|
2004-11-18 04:33:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // End sys namespace
|
|
|
|
} // End llvm namespace
|
|
|
|
|
|
|
|
#endif // LLVM_SYSTEM_DYNAMIC_LIBRARY_H
|