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_SYSTEM_DYNAMIC_LIBRARY_H
00015 #define LLVM_SYSTEM_DYNAMIC_LIBRARY_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     explicit DynamicLibrary(void *data = &Invalid) : Data(data) {}
00047   public:
00048     /// Returns true if the object refers to a valid library.
00049     bool isValid() { return Data != &Invalid; }
00050 
00051     /// Searches through the library for the symbol \p symbolName. If it is
00052     /// found, the address of that symbol is returned. If not, NULL is returned.
00053     /// Note that NULL will also be returned if the library failed to load.
00054     /// Use isValid() to distinguish these cases if it is important.
00055     /// Note that this will \e not search symbols explicitly registered by
00056     /// AddSymbol().
00057     void *getAddressOfSymbol(const char *symbolName);
00058 
00059     /// This function permanently loads the dynamic library at the given path.
00060     /// The library will only be unloaded when the program terminates.
00061     /// This returns a valid DynamicLibrary instance on success and an invalid
00062     /// instance on failure (see isValid()). \p *errMsg will only be modified
00063     /// if the library fails to load.
00064     ///
00065     /// It is safe to call this function multiple times for the same library.
00066     /// @brief Open a dynamic library permanently.
00067     static DynamicLibrary getPermanentLibrary(const char *filename,
00068                                               std::string *errMsg = 0);
00069 
00070     /// This function permanently loads the dynamic library at the given path.
00071     /// Use this instead of getPermanentLibrary() when you won't need to get
00072     /// symbols from the library itself.
00073     ///
00074     /// It is safe to call this function multiple times for the same library.
00075     static bool LoadLibraryPermanently(const char *Filename,
00076                                        std::string *ErrMsg = 0) {
00077       return !getPermanentLibrary(Filename, ErrMsg).isValid();
00078     }
00079 
00080     /// This function will search through all previously loaded dynamic
00081     /// libraries for the symbol \p symbolName. If it is found, the address of
00082     /// that symbol is returned. If not, null is returned. Note that this will
00083     /// search permanently loaded libraries (getPermanentLibrary()) as well
00084     /// as explicitly registered symbols (AddSymbol()).
00085     /// @throws std::string on error.
00086     /// @brief Search through libraries for address of a symbol
00087     static void *SearchForAddressOfSymbol(const char *symbolName);
00088 
00089     /// @brief Convenience function for C++ophiles.
00090     static void *SearchForAddressOfSymbol(const std::string &symbolName) {
00091       return SearchForAddressOfSymbol(symbolName.c_str());
00092     }
00093 
00094     /// This functions permanently adds the symbol \p symbolName with the
00095     /// value \p symbolValue.  These symbols are searched before any
00096     /// libraries.
00097     /// @brief Add searchable symbol/value pair.
00098     static void AddSymbol(StringRef symbolName, void *symbolValue);
00099   };
00100 
00101 } // End sys namespace
00102 } // End llvm namespace
00103 
00104 #endif // LLVM_SYSTEM_DYNAMIC_LIBRARY_H