SymbolicFile.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. //===- SymbolicFile.h - Interface that only provides symbols ----*- 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 SymbolicFile interface.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_OBJECT_SYMBOLICFILE_H
  13. #define LLVM_OBJECT_SYMBOLICFILE_H
  14. #include "llvm/ADT/StringRef.h"
  15. #include "llvm/ADT/iterator_range.h"
  16. #include "llvm/BinaryFormat/Magic.h"
  17. #include "llvm/Object/Binary.h"
  18. #include "llvm/Support/Error.h"
  19. #include "llvm/Support/Format.h"
  20. #include "llvm/Support/MemoryBuffer.h"
  21. #include <cinttypes>
  22. #include <cstdint>
  23. #include <cstring>
  24. #include <iterator>
  25. #include <memory>
  26. #include <system_error>
  27. namespace llvm {
  28. namespace object {
  29. union DataRefImpl {
  30. // This entire union should probably be a
  31. // char[max(8, sizeof(uintptr_t))] and require the impl to cast.
  32. struct {
  33. uint32_t a, b;
  34. } d;
  35. uintptr_t p;
  36. DataRefImpl() { std::memset(this, 0, sizeof(DataRefImpl)); }
  37. };
  38. template <typename OStream>
  39. OStream& operator<<(OStream &OS, const DataRefImpl &D) {
  40. OS << "(" << format("0x%08" PRIxPTR, D.p) << " (" << format("0x%08x", D.d.a)
  41. << ", " << format("0x%08x", D.d.b) << "))";
  42. return OS;
  43. }
  44. inline bool operator==(const DataRefImpl &a, const DataRefImpl &b) {
  45. // Check bitwise identical. This is the only legal way to compare a union w/o
  46. // knowing which member is in use.
  47. return std::memcmp(&a, &b, sizeof(DataRefImpl)) == 0;
  48. }
  49. inline bool operator!=(const DataRefImpl &a, const DataRefImpl &b) {
  50. return !operator==(a, b);
  51. }
  52. inline bool operator<(const DataRefImpl &a, const DataRefImpl &b) {
  53. // Check bitwise identical. This is the only legal way to compare a union w/o
  54. // knowing which member is in use.
  55. return std::memcmp(&a, &b, sizeof(DataRefImpl)) < 0;
  56. }
  57. template <class content_type> class content_iterator {
  58. content_type Current;
  59. public:
  60. using iterator_category = std::forward_iterator_tag;
  61. using value_type = content_type;
  62. using difference_type = std::ptrdiff_t;
  63. using pointer = value_type *;
  64. using reference = value_type &;
  65. content_iterator(content_type symb) : Current(std::move(symb)) {}
  66. const content_type *operator->() const { return &Current; }
  67. const content_type &operator*() const { return Current; }
  68. bool operator==(const content_iterator &other) const {
  69. return Current == other.Current;
  70. }
  71. bool operator!=(const content_iterator &other) const {
  72. return !(*this == other);
  73. }
  74. content_iterator &operator++() { // preincrement
  75. Current.moveNext();
  76. return *this;
  77. }
  78. };
  79. class SymbolicFile;
  80. /// This is a value type class that represents a single symbol in the list of
  81. /// symbols in the object file.
  82. class BasicSymbolRef {
  83. DataRefImpl SymbolPimpl;
  84. const SymbolicFile *OwningObject = nullptr;
  85. public:
  86. enum Flags : unsigned {
  87. SF_None = 0,
  88. SF_Undefined = 1U << 0, // Symbol is defined in another object file
  89. SF_Global = 1U << 1, // Global symbol
  90. SF_Weak = 1U << 2, // Weak symbol
  91. SF_Absolute = 1U << 3, // Absolute symbol
  92. SF_Common = 1U << 4, // Symbol has common linkage
  93. SF_Indirect = 1U << 5, // Symbol is an alias to another symbol
  94. SF_Exported = 1U << 6, // Symbol is visible to other DSOs
  95. SF_FormatSpecific = 1U << 7, // Specific to the object file format
  96. // (e.g. section symbols)
  97. SF_Thumb = 1U << 8, // Thumb symbol in a 32-bit ARM binary
  98. SF_Hidden = 1U << 9, // Symbol has hidden visibility
  99. SF_Const = 1U << 10, // Symbol value is constant
  100. SF_Executable = 1U << 11, // Symbol points to an executable section
  101. // (IR only)
  102. };
  103. BasicSymbolRef() = default;
  104. BasicSymbolRef(DataRefImpl SymbolP, const SymbolicFile *Owner);
  105. bool operator==(const BasicSymbolRef &Other) const;
  106. bool operator<(const BasicSymbolRef &Other) const;
  107. void moveNext();
  108. Error printName(raw_ostream &OS) const;
  109. /// Get symbol flags (bitwise OR of SymbolRef::Flags)
  110. Expected<uint32_t> getFlags() const;
  111. DataRefImpl getRawDataRefImpl() const;
  112. const SymbolicFile *getObject() const;
  113. };
  114. using basic_symbol_iterator = content_iterator<BasicSymbolRef>;
  115. class SymbolicFile : public Binary {
  116. public:
  117. SymbolicFile(unsigned int Type, MemoryBufferRef Source);
  118. ~SymbolicFile() override;
  119. // virtual interface.
  120. virtual void moveSymbolNext(DataRefImpl &Symb) const = 0;
  121. virtual Error printSymbolName(raw_ostream &OS, DataRefImpl Symb) const = 0;
  122. virtual Expected<uint32_t> getSymbolFlags(DataRefImpl Symb) const = 0;
  123. virtual basic_symbol_iterator symbol_begin() const = 0;
  124. virtual basic_symbol_iterator symbol_end() const = 0;
  125. // convenience wrappers.
  126. using basic_symbol_iterator_range = iterator_range<basic_symbol_iterator>;
  127. basic_symbol_iterator_range symbols() const {
  128. return basic_symbol_iterator_range(symbol_begin(), symbol_end());
  129. }
  130. // construction aux.
  131. static Expected<std::unique_ptr<SymbolicFile>>
  132. createSymbolicFile(MemoryBufferRef Object, llvm::file_magic Type,
  133. LLVMContext *Context, bool InitContent = true);
  134. static Expected<std::unique_ptr<SymbolicFile>>
  135. createSymbolicFile(MemoryBufferRef Object) {
  136. return createSymbolicFile(Object, llvm::file_magic::unknown, nullptr);
  137. }
  138. static bool classof(const Binary *v) {
  139. return v->isSymbolic();
  140. }
  141. static bool isSymbolicFile(file_magic Type, const LLVMContext *Context);
  142. };
  143. inline BasicSymbolRef::BasicSymbolRef(DataRefImpl SymbolP,
  144. const SymbolicFile *Owner)
  145. : SymbolPimpl(SymbolP), OwningObject(Owner) {}
  146. inline bool BasicSymbolRef::operator==(const BasicSymbolRef &Other) const {
  147. return SymbolPimpl == Other.SymbolPimpl;
  148. }
  149. inline bool BasicSymbolRef::operator<(const BasicSymbolRef &Other) const {
  150. return SymbolPimpl < Other.SymbolPimpl;
  151. }
  152. inline void BasicSymbolRef::moveNext() {
  153. return OwningObject->moveSymbolNext(SymbolPimpl);
  154. }
  155. inline Error BasicSymbolRef::printName(raw_ostream &OS) const {
  156. return OwningObject->printSymbolName(OS, SymbolPimpl);
  157. }
  158. inline Expected<uint32_t> BasicSymbolRef::getFlags() const {
  159. return OwningObject->getSymbolFlags(SymbolPimpl);
  160. }
  161. inline DataRefImpl BasicSymbolRef::getRawDataRefImpl() const {
  162. return SymbolPimpl;
  163. }
  164. inline const SymbolicFile *BasicSymbolRef::getObject() const {
  165. return OwningObject;
  166. }
  167. } // end namespace object
  168. } // end namespace llvm
  169. #endif // LLVM_OBJECT_SYMBOLICFILE_H