DynamicLibrary.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //===-- llvm/Support/DynamicLibrary.h - Portable Dynamic Library -*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file declares the sys::DynamicLibrary class.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_SUPPORT_DYNAMICLIBRARY_H
  13. #define LLVM_SUPPORT_DYNAMICLIBRARY_H
  14. #include <string>
  15. namespace llvm {
  16. class StringRef;
  17. namespace sys {
  18. /// This class provides a portable interface to dynamic libraries which also
  19. /// might be known as shared libraries, shared objects, dynamic shared
  20. /// objects, or dynamic link libraries. Regardless of the terminology or the
  21. /// operating system interface, this class provides a portable interface that
  22. /// allows dynamic libraries to be loaded and searched for externally
  23. /// defined symbols. This is typically used to provide "plug-in" support.
  24. /// It also allows for symbols to be defined which don't live in any library,
  25. /// but rather the main program itself, useful on Windows where the main
  26. /// executable cannot be searched.
  27. ///
  28. /// Note: there is currently no interface for temporarily loading a library,
  29. /// or for unloading libraries when the LLVM library is unloaded.
  30. class DynamicLibrary {
  31. // Placeholder whose address represents an invalid library.
  32. // We use this instead of NULL or a pointer-int pair because the OS library
  33. // might define 0 or 1 to be "special" handles, such as "search all".
  34. static char Invalid;
  35. // Opaque data used to interface with OS-specific dynamic library handling.
  36. void *Data;
  37. public:
  38. explicit DynamicLibrary(void *data = &Invalid) : Data(data) {}
  39. /// Returns true if the object refers to a valid library.
  40. bool isValid() const { return Data != &Invalid; }
  41. /// Searches through the library for the symbol \p symbolName. If it is
  42. /// found, the address of that symbol is returned. If not, NULL is returned.
  43. /// Note that NULL will also be returned if the library failed to load.
  44. /// Use isValid() to distinguish these cases if it is important.
  45. /// Note that this will \e not search symbols explicitly registered by
  46. /// AddSymbol().
  47. void *getAddressOfSymbol(const char *symbolName);
  48. /// This function permanently loads the dynamic library at the given path.
  49. /// The library will only be unloaded when llvm_shutdown() is called.
  50. /// This returns a valid DynamicLibrary instance on success and an invalid
  51. /// instance on failure (see isValid()). \p *errMsg will only be modified
  52. /// if the library fails to load.
  53. ///
  54. /// It is safe to call this function multiple times for the same library.
  55. /// Open a dynamic library permanently.
  56. static DynamicLibrary getPermanentLibrary(const char *filename,
  57. std::string *errMsg = nullptr);
  58. /// Registers an externally loaded library. The library will be unloaded
  59. /// when the program terminates.
  60. ///
  61. /// It is safe to call this function multiple times for the same library,
  62. /// though ownership is only taken if there was no error.
  63. ///
  64. /// \returns An empty \p DynamicLibrary if the library was already loaded.
  65. static DynamicLibrary addPermanentLibrary(void *handle,
  66. std::string *errMsg = nullptr);
  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 = nullptr) {
  74. return !getPermanentLibrary(Filename, ErrMsg).isValid();
  75. }
  76. enum SearchOrdering {
  77. /// SO_Linker - Search as a call to dlsym(dlopen(NULL)) would when
  78. /// DynamicLibrary::getPermanentLibrary(NULL) has been called or
  79. /// search the list of explcitly loaded symbols if not.
  80. SO_Linker,
  81. /// SO_LoadedFirst - Search all loaded libraries, then as SO_Linker would.
  82. SO_LoadedFirst,
  83. /// SO_LoadedLast - Search as SO_Linker would, then loaded libraries.
  84. /// Only useful to search if libraries with RTLD_LOCAL have been added.
  85. SO_LoadedLast,
  86. /// SO_LoadOrder - Or this in to search libraries in the ordered loaded.
  87. /// The default bahaviour is to search loaded libraries in reverse.
  88. SO_LoadOrder = 4
  89. };
  90. static SearchOrdering SearchOrder; // = SO_Linker
  91. /// This function will search through all previously loaded dynamic
  92. /// libraries for the symbol \p symbolName. If it is found, the address of
  93. /// that symbol is returned. If not, null is returned. Note that this will
  94. /// search permanently loaded libraries (getPermanentLibrary()) as well
  95. /// as explicitly registered symbols (AddSymbol()).
  96. /// @throws std::string on error.
  97. /// Search through libraries for address of a symbol
  98. static void *SearchForAddressOfSymbol(const char *symbolName);
  99. /// Convenience function for C++ophiles.
  100. static void *SearchForAddressOfSymbol(const std::string &symbolName) {
  101. return SearchForAddressOfSymbol(symbolName.c_str());
  102. }
  103. /// This functions permanently adds the symbol \p symbolName with the
  104. /// value \p symbolValue. These symbols are searched before any
  105. /// libraries.
  106. /// Add searchable symbol/value pair.
  107. static void AddSymbol(StringRef symbolName, void *symbolValue);
  108. class HandleSet;
  109. };
  110. } // End sys namespace
  111. } // End llvm namespace
  112. #endif