SymbolicFile.h 6.6 KB

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