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