DynamicLibrary.h 6.8 KB

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