LLVM API Documentation

DynamicLibrary.h
Go to the documentation of this file.
00001 //===-- llvm/Support/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_SUPPORT_DYNAMICLIBRARY_H
00015 #define LLVM_SUPPORT_DYNAMICLIBRARY_H
00016 
00017 #include <string>
00018 
00019 namespace llvm {
00020 
00021 class StringRef;
00022 
00023 namespace sys {
00024 
00025   /// This class provides a portable interface to dynamic libraries which also
00026   /// might be known as shared libraries, shared objects, dynamic shared
00027   /// objects, or dynamic link libraries. Regardless of the terminology or the
00028   /// operating system interface, this class provides a portable interface that
00029   /// allows dynamic libraries to be loaded and searched for externally
00030   /// defined symbols. This is typically used to provide "plug-in" support.
00031   /// It also allows for symbols to be defined which don't live in any library,
00032   /// but rather the main program itself, useful on Windows where the main
00033   /// executable cannot be searched.
00034   ///
00035   /// Note: there is currently no interface for temporarily loading a library,
00036   /// or for unloading libraries when the LLVM library is unloaded.
00037   class DynamicLibrary {
00038     // Placeholder whose address represents an invalid library.
00039     // We use this instead of NULL or a pointer-int pair because the OS library
00040     // might define 0 or 1 to be "special" handles, such as "search all".
00041     static char Invalid;
00042 
00043     // Opaque data used to interface with OS-specific dynamic library handling.
00044     void *Data;
00045 
00046   public:
00047     explicit DynamicLibrary(void *data = &Invalid) : Data(data) {}
00048 
00049     /// Returns true if the object refers to a valid library.
00050     bool isValid() const { return Data != &Invalid; }
00051 
00052     /// Searches through the library for the symbol \p symbolName. If it is
00053     /// found, the address of that symbol is returned. If not, NULL is returned.
00054     /// Note that NULL will also be returned if the library failed to load.
00055     /// Use isValid() to distinguish these cases if it is important.
00056     /// Note that this will \e not search symbols explicitly registered by
00057     /// AddSymbol().
00058     void *getAddressOfSymbol(const char *symbolName);
00059 
00060     /// This function permanently loads the dynamic library at the given path.
00061     /// The library will only be unloaded when the program terminates.
00062     /// This returns a valid DynamicLibrary instance on success and an invalid
00063     /// instance on failure (see isValid()). \p *errMsg will only be modified
00064     /// if the library fails to load.
00065     ///
00066     /// It is safe to call this function multiple times for the same library.
00067     /// @brief Open a dynamic library permanently.
00068     static DynamicLibrary getPermanentLibrary(const char *filename,
00069                                               std::string *errMsg = nullptr);
00070 
00071     /// This function permanently loads the dynamic library at the given path.
00072     /// Use this instead of getPermanentLibrary() when you won't need to get
00073     /// symbols from the library itself.
00074     ///
00075     /// It is safe to call this function multiple times for the same library.
00076     static bool LoadLibraryPermanently(const char *Filename,
00077                                        std::string *ErrMsg = nullptr) {
00078       return !getPermanentLibrary(Filename, ErrMsg).isValid();
00079     }
00080 
00081     /// This function will search through all previously loaded dynamic
00082     /// libraries for the symbol \p symbolName. If it is found, the address of
00083     /// that symbol is returned. If not, null is returned. Note that this will
00084     /// search permanently loaded libraries (getPermanentLibrary()) as well
00085     /// as explicitly registered symbols (AddSymbol()).
00086     /// @throws std::string on error.
00087     /// @brief Search through libraries for address of a symbol
00088     static void *SearchForAddressOfSymbol(const char *symbolName);
00089 
00090     /// @brief Convenience function for C++ophiles.
00091     static void *SearchForAddressOfSymbol(const std::string &symbolName) {
00092       return SearchForAddressOfSymbol(symbolName.c_str());
00093     }
00094 
00095     /// This functions permanently adds the symbol \p symbolName with the
00096     /// value \p symbolValue.  These symbols are searched before any
00097     /// libraries.
00098     /// @brief Add searchable symbol/value pair.
00099     static void AddSymbol(StringRef symbolName, void *symbolValue);
00100   };
00101 
00102 } // End sys namespace
00103 } // End llvm namespace
00104 
00105 #endif