Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:29

0001 //===-- llvm/Support/DynamicLibrary.h - Portable Dynamic Library -*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file declares the sys::DynamicLibrary class.
0010 //
0011 //===----------------------------------------------------------------------===//
0012 
0013 #ifndef LLVM_SUPPORT_DYNAMICLIBRARY_H
0014 #define LLVM_SUPPORT_DYNAMICLIBRARY_H
0015 
0016 #include <string>
0017 
0018 namespace llvm {
0019 
0020 class StringRef;
0021 
0022 namespace sys {
0023 
0024 /// This class provides a portable interface to dynamic libraries which also
0025 /// might be known as shared libraries, shared objects, dynamic shared
0026 /// objects, or dynamic link libraries. Regardless of the terminology or the
0027 /// operating system interface, this class provides a portable interface that
0028 /// allows dynamic libraries to be loaded and searched for externally
0029 /// defined symbols. This is typically used to provide "plug-in" support.
0030 /// It also allows for symbols to be defined which don't live in any library,
0031 /// but rather the main program itself, useful on Windows where the main
0032 /// executable cannot be searched.
0033 class DynamicLibrary {
0034   // Placeholder whose address represents an invalid library.
0035   // We use this instead of NULL or a pointer-int pair because the OS library
0036   // might define 0 or 1 to be "special" handles, such as "search all".
0037   static char Invalid;
0038 
0039   // Opaque data used to interface with OS-specific dynamic library handling.
0040   void *Data;
0041 
0042 public:
0043   explicit DynamicLibrary(void *data = &Invalid) : Data(data) {}
0044 
0045   /// Return the OS specific handle value.
0046   void *getOSSpecificHandle() const { return Data; }
0047 
0048   /// Returns true if the object refers to a valid library.
0049   bool isValid() const { return Data != &Invalid; }
0050 
0051   /// Searches through the library for the symbol \p symbolName. If it is
0052   /// found, the address of that symbol is returned. If not, NULL is returned.
0053   /// Note that NULL will also be returned if the library failed to load.
0054   /// Use isValid() to distinguish these cases if it is important.
0055   /// Note that this will \e not search symbols explicitly registered by
0056   /// AddSymbol().
0057   void *getAddressOfSymbol(const char *symbolName);
0058 
0059   /// This function permanently loads the dynamic library at the given path
0060   /// using the library load operation from the host operating system. The
0061   /// library instance will only be closed when global destructors run, and
0062   /// there is no guarantee when the library will be unloaded.
0063   ///
0064   /// This returns a valid DynamicLibrary instance on success and an invalid
0065   /// instance on failure (see isValid()). \p *errMsg will only be modified if
0066   /// the library fails to load.
0067   ///
0068   /// It is safe to call this function multiple times for the same library.
0069   /// Open a dynamic library permanently.
0070   static DynamicLibrary getPermanentLibrary(const char *filename,
0071                                             std::string *errMsg = nullptr);
0072 
0073   /// Registers an externally loaded library. The library will be unloaded
0074   /// when the program terminates.
0075   ///
0076   /// It is safe to call this function multiple times for the same library,
0077   /// though ownership is only taken if there was no error.
0078   static DynamicLibrary addPermanentLibrary(void *handle,
0079                                             std::string *errMsg = nullptr);
0080 
0081   /// This function permanently loads the dynamic library at the given path.
0082   /// Use this instead of getPermanentLibrary() when you won't need to get
0083   /// symbols from the library itself.
0084   ///
0085   /// It is safe to call this function multiple times for the same library.
0086   static bool LoadLibraryPermanently(const char *Filename,
0087                                      std::string *ErrMsg = nullptr) {
0088     return !getPermanentLibrary(Filename, ErrMsg).isValid();
0089   }
0090 
0091   /// This function loads the dynamic library at the given path, using the
0092   /// library load operation from the host operating system. The library
0093   /// instance will be closed when closeLibrary is called or global destructors
0094   /// are run, but there is no guarantee when the library will be unloaded.
0095   ///
0096   /// This returns a valid DynamicLibrary instance on success and an invalid
0097   /// instance on failure (see isValid()). \p *Err will only be modified if the
0098   /// library fails to load.
0099   ///
0100   /// It is safe to call this function multiple times for the same library.
0101   static DynamicLibrary getLibrary(const char *FileName,
0102                                    std::string *Err = nullptr);
0103 
0104   /// This function closes the dynamic library at the given path, using the
0105   /// library close operation of the host operating system, and there is no
0106   /// guarantee if or when this will cause the library to be unloaded.
0107   ///
0108   /// This function should be called only if the library was loaded using the
0109   /// getLibrary() function.
0110   static void closeLibrary(DynamicLibrary &Lib);
0111 
0112   enum SearchOrdering {
0113     /// SO_Linker - Search as a call to dlsym(dlopen(NULL)) would when
0114     /// DynamicLibrary::getPermanentLibrary(NULL) has been called or
0115     /// search the list of explcitly loaded symbols if not.
0116     SO_Linker,
0117     /// SO_LoadedFirst - Search all loaded libraries, then as SO_Linker would.
0118     SO_LoadedFirst,
0119     /// SO_LoadedLast - Search as SO_Linker would, then loaded libraries.
0120     /// Only useful to search if libraries with RTLD_LOCAL have been added.
0121     SO_LoadedLast,
0122     /// SO_LoadOrder - Or this in to search libraries in the ordered loaded.
0123     /// The default bahaviour is to search loaded libraries in reverse.
0124     SO_LoadOrder = 4
0125   };
0126   static SearchOrdering SearchOrder; // = SO_Linker
0127 
0128   /// This function will search through all previously loaded dynamic
0129   /// libraries for the symbol \p symbolName. If it is found, the address of
0130   /// that symbol is returned. If not, null is returned. Note that this will
0131   /// search permanently loaded libraries (getPermanentLibrary()) as well
0132   /// as explicitly registered symbols (AddSymbol()).
0133   /// @throws std::string on error.
0134   /// Search through libraries for address of a symbol
0135   static void *SearchForAddressOfSymbol(const char *symbolName);
0136 
0137   /// Convenience function for C++ophiles.
0138   static void *SearchForAddressOfSymbol(const std::string &symbolName) {
0139     return SearchForAddressOfSymbol(symbolName.c_str());
0140   }
0141 
0142   /// This functions permanently adds the symbol \p symbolName with the
0143   /// value \p symbolValue.  These symbols are searched before any
0144   /// libraries.
0145   /// Add searchable symbol/value pair.
0146   static void AddSymbol(StringRef symbolName, void *symbolValue);
0147 
0148   class HandleSet;
0149 };
0150 
0151 } // End sys namespace
0152 } // End llvm namespace
0153 
0154 #endif