LLVM API Documentation
00001 //===-- llvm/System/DynamicLibrary.h - Portable Dynamic Library -*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file declares the sys::DynamicLibrary class. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #ifndef LLVM_SYSTEM_DYNAMIC_LIBRARY_H 00015 #define LLVM_SYSTEM_DYNAMIC_LIBRARY_H 00016 00017 #include "llvm/System/Path.h" 00018 #include <string> 00019 00020 namespace llvm { 00021 namespace sys { 00022 00023 /// This class provides a portable interface to dynamic libraries which also 00024 /// might be known as shared libraries, shared objects, dynamic shared 00025 /// objects, or dynamic link libraries. Regardless of the terminology or the 00026 /// operating system interface, this class provides a portable interface that 00027 /// allows dynamic libraries to be loaded and and searched for externally 00028 /// defined symbols. This is typically used to provide "plug-in" support. 00029 /// It also allows for symbols to be defined which don't live in any library, 00030 /// but rather the main program itself, useful on Windows where the main 00031 /// executable cannot be searched. 00032 class DynamicLibrary { 00033 /// @name Constructors 00034 /// @{ 00035 public: 00036 /// Construct a DynamicLibrary that represents the currently executing 00037 /// program. The program must have been linked with -export-dynamic or 00038 /// -dlopen self for this to work. 00039 /// @throws std::string indicating why the program couldn't be opened. 00040 /// @brief Open program as dynamic library. 00041 DynamicLibrary(); 00042 00043 /// After destruction, the symbols of the library will no longer be 00044 /// available to the program. 00045 /// @brief Closes the DynamicLibrary 00046 ~DynamicLibrary(); 00047 00048 /// @} 00049 /// @name Functions 00050 /// @{ 00051 public: 00052 /// This function allows a library to be loaded without instantiating a 00053 /// DynamicLibrary object. Consequently, it is marked as being permanent 00054 /// and will only be unloaded when the program terminates. This returns 00055 /// false on success or returns true and fills in *ErrMsg on failure. 00056 /// @brief Open a dynamic library permanently. 00057 static bool LoadLibraryPermanently(const char* filename, 00058 std::string *ErrMsg = 0); 00059 00060 /// This function will search through all previously loaded dynamic 00061 /// libraries for the symbol \p symbolName. If it is found, the addressof 00062 /// that symbol is returned. If not, null is returned. Note that this will 00063 /// search permanently loaded libraries (LoadLibraryPermanently) as well 00064 /// as ephemerally loaded libraries (constructors). 00065 /// @throws std::string on error. 00066 /// @brief Search through libraries for address of a symbol 00067 static void* SearchForAddressOfSymbol(const char* symbolName); 00068 00069 /// @brief Convenience function for C++ophiles. 00070 static void* SearchForAddressOfSymbol(const std::string& symbolName) { 00071 return SearchForAddressOfSymbol(symbolName.c_str()); 00072 } 00073 00074 /// This functions permanently adds the symbol \p symbolName with the 00075 /// value \p symbolValue. These symbols are searched before any 00076 /// libraries. 00077 /// @brief Add searchable symbol/value pair. 00078 static void AddSymbol(const char* symbolName, void *symbolValue); 00079 00080 /// @brief Convenience function for C++ophiles. 00081 static void AddSymbol(const std::string& symbolName, void *symbolValue) { 00082 AddSymbol(symbolName.c_str(), symbolValue); 00083 } 00084 00085 /// @} 00086 /// @name Implementation 00087 /// @{ 00088 protected: 00089 void* handle; // Opaque handle for information about the library 00090 DynamicLibrary(const DynamicLibrary&); ///< Do not implement 00091 DynamicLibrary& operator=(const DynamicLibrary&); ///< Do not implement 00092 /// @} 00093 }; 00094 00095 } // End sys namespace 00096 } // End llvm namespace 00097 00098 #endif // LLVM_SYSTEM_DYNAMIC_LIBRARY_H
This web site is hosted by the Computer Science Department at the University of Illinois at Urbana-Champaign.