DynamicLibrary.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. ///
  33. /// Note: there is currently no interface for temporarily loading a library,
  34. /// or for unloading libraries when the LLVM library is unloaded.
  35. class DynamicLibrary {
  36. // Placeholder whose address represents an invalid library.
  37. // We use this instead of NULL or a pointer-int pair because the OS library
  38. // might define 0 or 1 to be "special" handles, such as "search all".
  39. static char Invalid;
  40. // Opaque data used to interface with OS-specific dynamic library handling.
  41. void *Data;
  42. public:
  43. explicit DynamicLibrary(void *data = &Invalid) : Data(data) {}
  44. /// Returns true if the object refers to a valid library.
  45. bool isValid() const { return Data != &Invalid; }
  46. /// Searches through the library for the symbol \p symbolName. If it is
  47. /// found, the address of that symbol is returned. If not, NULL is returned.
  48. /// Note that NULL will also be returned if the library failed to load.
  49. /// Use isValid() to distinguish these cases if it is important.
  50. /// Note that this will \e not search symbols explicitly registered by
  51. /// AddSymbol().
  52. void *getAddressOfSymbol(const char *symbolName);
  53. /// This function permanently loads the dynamic library at the given path.
  54. /// The library will only be unloaded when llvm_shutdown() is called.
  55. /// This returns a valid DynamicLibrary instance on success and an invalid
  56. /// instance on failure (see isValid()). \p *errMsg will only be modified
  57. /// if the library fails to load.
  58. ///
  59. /// It is safe to call this function multiple times for the same library.
  60. /// Open a dynamic library permanently.
  61. static DynamicLibrary getPermanentLibrary(const char *filename,
  62. std::string *errMsg = nullptr);
  63. /// Registers an externally loaded library. The library will be unloaded
  64. /// when the program terminates.
  65. ///
  66. /// It is safe to call this function multiple times for the same library,
  67. /// though ownership is only taken if there was no error.
  68. ///
  69. /// \returns An empty \p DynamicLibrary if the library was already loaded.
  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. enum SearchOrdering {
  82. /// SO_Linker - Search as a call to dlsym(dlopen(NULL)) would when
  83. /// DynamicLibrary::getPermanentLibrary(NULL) has been called or
  84. /// search the list of explcitly loaded symbols if not.
  85. SO_Linker,
  86. /// SO_LoadedFirst - Search all loaded libraries, then as SO_Linker would.
  87. SO_LoadedFirst,
  88. /// SO_LoadedLast - Search as SO_Linker would, then loaded libraries.
  89. /// Only useful to search if libraries with RTLD_LOCAL have been added.
  90. SO_LoadedLast,
  91. /// SO_LoadOrder - Or this in to search libraries in the ordered loaded.
  92. /// The default bahaviour is to search loaded libraries in reverse.
  93. SO_LoadOrder = 4
  94. };
  95. static SearchOrdering SearchOrder; // = SO_Linker
  96. /// This function will search through all previously loaded dynamic
  97. /// libraries for the symbol \p symbolName. If it is found, the address of
  98. /// that symbol is returned. If not, null is returned. Note that this will
  99. /// search permanently loaded libraries (getPermanentLibrary()) as well
  100. /// as explicitly registered symbols (AddSymbol()).
  101. /// @throws std::string on error.
  102. /// Search through libraries for address of a symbol
  103. static void *SearchForAddressOfSymbol(const char *symbolName);
  104. /// Convenience function for C++ophiles.
  105. static void *SearchForAddressOfSymbol(const std::string &symbolName) {
  106. return SearchForAddressOfSymbol(symbolName.c_str());
  107. }
  108. /// This functions permanently adds the symbol \p symbolName with the
  109. /// value \p symbolValue. These symbols are searched before any
  110. /// libraries.
  111. /// Add searchable symbol/value pair.
  112. static void AddSymbol(StringRef symbolName, void *symbolValue);
  113. class HandleSet;
  114. };
  115. } // End sys namespace
  116. } // End llvm namespace
  117. #endif
  118. #ifdef __GNUC__
  119. #pragma GCC diagnostic pop
  120. #endif