1 //===-- llvm/System/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
17 #include "llvm/System/Path.h"
23 /// This class provides a portable interface to dynamic libraries which also
24 /// might be known as shared libraries, shared objects, dynamic shared
25 /// objects, or dynamic link libraries. Regardless of the terminology or the
26 /// operating system interface, this class provides a portable interface that
27 /// allows dynamic libraries to be loaded and and searched for externally
28 /// defined symbols. This is typically used to provide "plug-in" support.
29 /// It also allows for symbols to be defined which don't live in any library,
30 /// but rather the main program itself, useful on Windows where the main
31 /// executable cannot be searched.
32 class DynamicLibrary {
33 /// @name Constructors
36 /// Construct a DynamicLibrary that represents the currently executing
37 /// program. The program must have been linked with -export-dynamic or
38 /// -dlopen self for this to work.
39 /// @throws std::string indicating why the program couldn't be opened.
40 /// @brief Open program as dynamic library.
43 /// After destruction, the symbols of the library will no longer be
44 /// available to the program.
45 /// @brief Closes the DynamicLibrary
52 /// This function allows a library to be loaded without instantiating a
53 /// DynamicLibrary object. Consequently, it is marked as being permanent
54 /// and will only be unloaded when the program terminates. This returns
55 /// false on success or returns true and fills in *ErrMsg on failure.
56 /// @brief Open a dynamic library permanently.
57 static bool LoadLibraryPermanently(const char* filename,
58 std::string *ErrMsg = 0);
60 /// This function will search through all previously loaded dynamic
61 /// libraries for the symbol \p symbolName. If it is found, the addressof
62 /// that symbol is returned. If not, null is returned. Note that this will
63 /// search permanently loaded libraries (LoadLibraryPermanently) as well
64 /// as ephemerally loaded libraries (constructors).
65 /// @throws std::string on error.
66 /// @brief Search through libraries for address of a symbol
67 static void* SearchForAddressOfSymbol(const char* symbolName);
69 /// @brief Convenience function for C++ophiles.
70 static void* SearchForAddressOfSymbol(const std::string& symbolName) {
71 return SearchForAddressOfSymbol(symbolName.c_str());
74 /// This functions permanently adds the symbol \p symbolName with the
75 /// value \p symbolValue. These symbols are searched before any
77 /// @brief Add searchable symbol/value pair.
78 static void AddSymbol(const char* symbolName, void *symbolValue);
80 /// @brief Convenience function for C++ophiles.
81 static void AddSymbol(const std::string& symbolName, void *symbolValue) {
82 AddSymbol(symbolName.c_str(), symbolValue);
86 /// @name Implementation
89 void* handle; // Opaque handle for information about the library
90 DynamicLibrary(const DynamicLibrary&); ///< Do not implement
91 DynamicLibrary& operator=(const DynamicLibrary&); ///< Do not implement
95 } // End sys namespace
96 } // End llvm namespace
98 #endif // LLVM_SYSTEM_DYNAMIC_LIBRARY_H