Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
4.5 KiB

  1. //===-- llvm/Support/DynamicLibrary.h - Portable Dynamic Library -*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file declares the sys::DynamicLibrary class.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_SYSTEM_DYNAMICLIBRARY_H
  14. #define LLVM_SYSTEM_DYNAMICLIBRARY_H
  15. #include <string>
  16. namespace llvm {
  17. class StringRef;
  18. namespace sys {
  19. /// This class provides a portable interface to dynamic libraries which also
  20. /// might be known as shared libraries, shared objects, dynamic shared
  21. /// objects, or dynamic link libraries. Regardless of the terminology or the
  22. /// operating system interface, this class provides a portable interface that
  23. /// allows dynamic libraries to be loaded and searched for externally
  24. /// defined symbols. This is typically used to provide "plug-in" support.
  25. /// It also allows for symbols to be defined which don't live in any library,
  26. /// but rather the main program itself, useful on Windows where the main
  27. /// executable cannot be searched.
  28. ///
  29. /// Note: there is currently no interface for temporarily loading a library,
  30. /// or for unloading libraries when the LLVM library is unloaded.
  31. class DynamicLibrary {
  32. // Placeholder whose address represents an invalid library.
  33. // We use this instead of NULL or a pointer-int pair because the OS library
  34. // might define 0 or 1 to be "special" handles, such as "search all".
  35. static char Invalid;
  36. // Opaque data used to interface with OS-specific dynamic library handling.
  37. void *Data;
  38. explicit DynamicLibrary(void *data = &Invalid) : Data(data) {}
  39. public:
  40. /// Returns true if the object refers to a valid library.
  41. bool isValid() { return Data != &Invalid; }
  42. /// Searches through the library for the symbol \p symbolName. If it is
  43. /// found, the address of that symbol is returned. If not, NULL is returned.
  44. /// Note that NULL will also be returned if the library failed to load.
  45. /// Use isValid() to distinguish these cases if it is important.
  46. /// Note that this will \e not search symbols explicitly registered by
  47. /// AddSymbol().
  48. void *getAddressOfSymbol(const char *symbolName);
  49. /// This function permanently loads the dynamic library at the given path.
  50. /// The library will only be unloaded when the program terminates.
  51. /// This returns a valid DynamicLibrary instance on success and an invalid
  52. /// instance on failure (see isValid()). \p *errMsg will only be modified
  53. /// if the library fails to load.
  54. ///
  55. /// It is safe to call this function multiple times for the same library.
  56. /// @brief Open a dynamic library permanently.
  57. static DynamicLibrary getPermanentLibrary(const char *filename,
  58. std::string *errMsg = 0);
  59. /// This function permanently loads the dynamic library at the given path.
  60. /// Use this instead of getPermanentLibrary() when you won't need to get
  61. /// symbols from the library itself.
  62. ///
  63. /// It is safe to call this function multiple times for the same library.
  64. static bool LoadLibraryPermanently(const char *Filename,
  65. std::string *ErrMsg = 0) {
  66. return !getPermanentLibrary(Filename, ErrMsg).isValid();
  67. }
  68. /// This function will search through all previously loaded dynamic
  69. /// libraries for the symbol \p symbolName. If it is found, the address of
  70. /// that symbol is returned. If not, null is returned. Note that this will
  71. /// search permanently loaded libraries (getPermanentLibrary()) as well
  72. /// as explicitly registered symbols (AddSymbol()).
  73. /// @throws std::string on error.
  74. /// @brief Search through libraries for address of a symbol
  75. static void *SearchForAddressOfSymbol(const char *symbolName);
  76. /// @brief Convenience function for C++ophiles.
  77. static void *SearchForAddressOfSymbol(const std::string &symbolName) {
  78. return SearchForAddressOfSymbol(symbolName.c_str());
  79. }
  80. /// This functions permanently adds the symbol \p symbolName with the
  81. /// value \p symbolValue. These symbols are searched before any
  82. /// libraries.
  83. /// @brief Add searchable symbol/value pair.
  84. static void AddSymbol(StringRef symbolName, void *symbolValue);
  85. };
  86. } // End sys namespace
  87. } // End llvm namespace
  88. #endif // LLVM_SYSTEM_DYNAMIC_LIBRARY_H