Home | History | Annotate | Download | only in Support
      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_DYNAMIC_LIBRARY_H
     15 #define LLVM_SYSTEM_DYNAMIC_LIBRARY_H
     16 
     17 #include <string>
     18 
     19 namespace llvm {
     20 namespace sys {
     21 
     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.
     31   ///
     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     // Placeholder whose address represents an invalid library.
     36     // We use this instead of NULL or a pointer-int pair because the OS library
     37     // might define 0 or 1 to be "special" handles, such as "search all".
     38     static char Invalid;
     39 
     40     // Opaque data used to interface with OS-specific dynamic library handling.
     41     void *Data;
     42 
     43     explicit DynamicLibrary(void *data = &Invalid) : Data(data) {}
     44   public:
     45     /// Returns true if the object refers to a valid library.
     46     bool isValid() { return Data != &Invalid; }
     47 
     48     /// Searches through the library for the symbol \p symbolName. If it is
     49     /// found, the address of that symbol is returned. If not, NULL is returned.
     50     /// Note that NULL will also be returned if the library failed to load.
     51     /// Use isValid() to distinguish these cases if it is important.
     52     /// Note that this will \e not search symbols explicitly registered by
     53     /// AddSymbol().
     54     void *getAddressOfSymbol(const char *symbolName);
     55 
     56     /// This function permanently loads the dynamic library at the given path.
     57     /// The library will only be unloaded when the program terminates.
     58     /// This returns a valid DynamicLibrary instance on success and an invalid
     59     /// instance on failure (see isValid()). \p *errMsg will only be modified
     60     /// if the library fails to load.
     61     ///
     62     /// It is safe to call this function multiple times for the same library.
     63     /// @brief Open a dynamic library permanently.
     64     static DynamicLibrary getPermanentLibrary(const char *filename,
     65                                               std::string *errMsg = 0);
     66 
     67     /// This function permanently loads the dynamic library at the given path.
     68     /// Use this instead of getPermanentLibrary() when you won't need to get
     69     /// symbols from the library itself.
     70     ///
     71     /// It is safe to call this function multiple times for the same library.
     72     static bool LoadLibraryPermanently(const char *Filename,
     73                                        std::string *ErrMsg = 0) {
     74       return !getPermanentLibrary(Filename, ErrMsg).isValid();
     75     }
     76 
     77     /// This function will search through all previously loaded dynamic
     78     /// libraries for the symbol \p symbolName. If it is found, the address of
     79     /// that symbol is returned. If not, null is returned. Note that this will
     80     /// search permanently loaded libraries (getPermanentLibrary()) as well
     81     /// as explicitly registered symbols (AddSymbol()).
     82     /// @throws std::string on error.
     83     /// @brief Search through libraries for address of a symbol
     84     static void *SearchForAddressOfSymbol(const char *symbolName);
     85 
     86     /// @brief Convenience function for C++ophiles.
     87     static void *SearchForAddressOfSymbol(const std::string &symbolName) {
     88       return SearchForAddressOfSymbol(symbolName.c_str());
     89     }
     90 
     91     /// This functions permanently adds the symbol \p symbolName with the
     92     /// value \p symbolValue.  These symbols are searched before any
     93     /// libraries.
     94     /// @brief Add searchable symbol/value pair.
     95     static void AddSymbol(StringRef symbolName, void *symbolValue);
     96   };
     97 
     98 } // End sys namespace
     99 } // End llvm namespace
    100 
    101 #endif // LLVM_SYSTEM_DYNAMIC_LIBRARY_H
    102