1 //===-- llvm/Support/DynamicLibrary.h - Portable Dynamic Library -*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file declares the sys::DynamicLibrary class. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef LLVM_SYSTEM_DYNAMICLIBRARY_H 15 #define LLVM_SYSTEM_DYNAMICLIBRARY_H 16 17 #include <string> 18 19 namespace llvm { 20 21 class StringRef; 22 23 namespace sys { 24 25 /// This class provides a portable interface to dynamic libraries which also 26 /// might be known as shared libraries, shared objects, dynamic shared 27 /// objects, or dynamic link libraries. Regardless of the terminology or the 28 /// operating system interface, this class provides a portable interface that 29 /// allows dynamic libraries to be loaded and searched for externally 30 /// defined symbols. This is typically used to provide "plug-in" support. 31 /// It also allows for symbols to be defined which don't live in any library, 32 /// but rather the main program itself, useful on Windows where the main 33 /// executable cannot be searched. 34 /// 35 /// Note: there is currently no interface for temporarily loading a library, 36 /// or for unloading libraries when the LLVM library is unloaded. 37 class DynamicLibrary { 38 // Placeholder whose address represents an invalid library. 39 // We use this instead of NULL or a pointer-int pair because the OS library 40 // might define 0 or 1 to be "special" handles, such as "search all". 41 static char Invalid; 42 43 // Opaque data used to interface with OS-specific dynamic library handling. 44 void *Data; 45 46 explicit DynamicLibrary(void *data = &Invalid) : Data(data) {} 47 public: 48 /// Returns true if the object refers to a valid library. 49 bool isValid() { return Data != &Invalid; } 50 51 /// Searches through the library for the symbol \p symbolName. If it is 52 /// found, the address of that symbol is returned. If not, NULL is returned. 53 /// Note that NULL will also be returned if the library failed to load. 54 /// Use isValid() to distinguish these cases if it is important. 55 /// Note that this will \e not search symbols explicitly registered by 56 /// AddSymbol(). 57 void *getAddressOfSymbol(const char *symbolName); 58 59 /// This function permanently loads the dynamic library at the given path. 60 /// The library will only be unloaded when the program terminates. 61 /// This returns a valid DynamicLibrary instance on success and an invalid 62 /// instance on failure (see isValid()). \p *errMsg will only be modified 63 /// if the library fails to load. 64 /// 65 /// It is safe to call this function multiple times for the same library. 66 /// @brief Open a dynamic library permanently. 67 static DynamicLibrary getPermanentLibrary(const char *filename, 68 std::string *errMsg = 0); 69 70 /// This function permanently loads the dynamic library at the given path. 71 /// Use this instead of getPermanentLibrary() when you won't need to get 72 /// symbols from the library itself. 73 /// 74 /// It is safe to call this function multiple times for the same library. 75 static bool LoadLibraryPermanently(const char *Filename, 76 std::string *ErrMsg = 0) { 77 return !getPermanentLibrary(Filename, ErrMsg).isValid(); 78 } 79 80 /// This function will search through all previously loaded dynamic 81 /// libraries for the symbol \p symbolName. If it is found, the address of 82 /// that symbol is returned. If not, null is returned. Note that this will 83 /// search permanently loaded libraries (getPermanentLibrary()) as well 84 /// as explicitly registered symbols (AddSymbol()). 85 /// @throws std::string on error. 86 /// @brief Search through libraries for address of a symbol 87 static void *SearchForAddressOfSymbol(const char *symbolName); 88 89 /// @brief Convenience function for C++ophiles. 90 static void *SearchForAddressOfSymbol(const std::string &symbolName) { 91 return SearchForAddressOfSymbol(symbolName.c_str()); 92 } 93 94 /// This functions permanently adds the symbol \p symbolName with the 95 /// value \p symbolValue. These symbols are searched before any 96 /// libraries. 97 /// @brief Add searchable symbol/value pair. 98 static void AddSymbol(StringRef symbolName, void *symbolValue); 99 }; 100 101 } // End sys namespace 102 } // End llvm namespace 103 104 #endif // LLVM_SYSTEM_DYNAMIC_LIBRARY_H 105