DebugMap.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. //===- tools/dsymutil/DebugMap.h - Generic debug map representation -------===//
  2. //
  3. // The LLVM Linker
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. ///
  10. /// \file
  11. ///
  12. /// This file contains the class declaration of the DebugMap
  13. /// entity. A DebugMap lists all the object files linked together to
  14. /// produce an executable along with the linked address of all the
  15. /// atoms used in these object files.
  16. /// The DebugMap is an input to the DwarfLinker class that will
  17. /// extract the Dwarf debug information from the referenced object
  18. /// files and link their usefull debug info together.
  19. ///
  20. //===----------------------------------------------------------------------===//
  21. #ifndef LLVM_TOOLS_DSYMUTIL_DEBUGMAP_H
  22. #define LLVM_TOOLS_DSYMUTIL_DEBUGMAP_H
  23. #include "llvm/ADT/DenseMap.h"
  24. #include "llvm/ADT/StringMap.h"
  25. #include "llvm/ADT/Triple.h"
  26. #include "llvm/ADT/iterator_range.h"
  27. #include "llvm/Object/ObjectFile.h"
  28. #include "llvm/Support/ErrorOr.h"
  29. #include "llvm/Support/Format.h"
  30. #include "llvm/Support/Path.h"
  31. #include "llvm/Support/YAMLTraits.h"
  32. #include <vector>
  33. namespace llvm {
  34. class raw_ostream;
  35. namespace dsymutil {
  36. class DebugMapObject;
  37. /// \brief The DebugMap object stores the list of object files to
  38. /// query for debug information along with the mapping between the
  39. /// symbols' addresses in the object file to their linked address in
  40. /// the linked binary.
  41. ///
  42. /// A DebugMap producer could look like this:
  43. /// DebugMap *DM = new DebugMap();
  44. /// for (const auto &Obj: LinkedObjects) {
  45. /// DebugMapObject &DMO = DM->addDebugMapObject(Obj.getPath());
  46. /// for (const auto &Sym: Obj.getLinkedSymbols())
  47. /// DMO.addSymbol(Sym.getName(), Sym.getObjectFileAddress(),
  48. /// Sym.getBinaryAddress());
  49. /// }
  50. ///
  51. /// A DebugMap consumer can then use the map to link the debug
  52. /// information. For example something along the lines of:
  53. /// for (const auto &DMO: DM->objects()) {
  54. /// auto Obj = createBinary(DMO.getObjectFilename());
  55. /// for (auto &DIE: Obj.getDwarfDIEs()) {
  56. /// if (SymbolMapping *Sym = DMO.lookup(DIE.getName()))
  57. /// DIE.relocate(Sym->ObjectAddress, Sym->BinaryAddress);
  58. /// else
  59. /// DIE.discardSubtree();
  60. /// }
  61. /// }
  62. class DebugMap {
  63. Triple BinaryTriple;
  64. typedef std::vector<std::unique_ptr<DebugMapObject>> ObjectContainer;
  65. ObjectContainer Objects;
  66. /// For YAML IO support.
  67. ///@{
  68. friend yaml::MappingTraits<std::unique_ptr<DebugMap>>;
  69. friend yaml::MappingTraits<DebugMap>;
  70. DebugMap() = default;
  71. ///@}
  72. public:
  73. DebugMap(const Triple &BinaryTriple) : BinaryTriple(BinaryTriple) {}
  74. typedef ObjectContainer::const_iterator const_iterator;
  75. iterator_range<const_iterator> objects() const {
  76. return make_range(begin(), end());
  77. }
  78. const_iterator begin() const { return Objects.begin(); }
  79. const_iterator end() const { return Objects.end(); }
  80. /// This function adds an DebugMapObject to the list owned by this
  81. /// debug map.
  82. DebugMapObject &addDebugMapObject(StringRef ObjectFilePath);
  83. const Triple &getTriple() const { return BinaryTriple; }
  84. void print(raw_ostream &OS) const;
  85. #ifndef NDEBUG
  86. void dump() const;
  87. #endif
  88. /// Read a debug map for \a InputFile.
  89. static ErrorOr<std::unique_ptr<DebugMap>>
  90. parseYAMLDebugMap(StringRef InputFile, StringRef PrependPath, bool Verbose);
  91. };
  92. /// \brief The DebugMapObject represents one object file described by
  93. /// the DebugMap. It contains a list of mappings between addresses in
  94. /// the object file and in the linked binary for all the linked atoms
  95. /// in this object file.
  96. class DebugMapObject {
  97. public:
  98. struct SymbolMapping {
  99. yaml::Hex64 ObjectAddress;
  100. yaml::Hex64 BinaryAddress;
  101. yaml::Hex32 Size;
  102. SymbolMapping(uint64_t ObjectAddress, uint64_t BinaryAddress, uint32_t Size)
  103. : ObjectAddress(ObjectAddress), BinaryAddress(BinaryAddress),
  104. Size(Size) {}
  105. /// For YAML IO support
  106. SymbolMapping() = default;
  107. };
  108. typedef StringMapEntry<SymbolMapping> DebugMapEntry;
  109. /// \brief Adds a symbol mapping to this DebugMapObject.
  110. /// \returns false if the symbol was already registered. The request
  111. /// is discarded in this case.
  112. bool addSymbol(llvm::StringRef SymName, uint64_t ObjectAddress,
  113. uint64_t LinkedAddress, uint32_t Size);
  114. /// \brief Lookup a symbol mapping.
  115. /// \returns null if the symbol isn't found.
  116. const DebugMapEntry *lookupSymbol(StringRef SymbolName) const;
  117. /// \brief Lookup an objectfile address.
  118. /// \returns null if the address isn't found.
  119. const DebugMapEntry *lookupObjectAddress(uint64_t Address) const;
  120. llvm::StringRef getObjectFilename() const { return Filename; }
  121. iterator_range<StringMap<SymbolMapping>::const_iterator> symbols() const {
  122. return make_range(Symbols.begin(), Symbols.end());
  123. }
  124. void print(raw_ostream &OS) const;
  125. #ifndef NDEBUG
  126. void dump() const;
  127. #endif
  128. private:
  129. friend class DebugMap;
  130. /// DebugMapObjects can only be constructed by the owning DebugMap.
  131. DebugMapObject(StringRef ObjectFilename);
  132. std::string Filename;
  133. StringMap<SymbolMapping> Symbols;
  134. DenseMap<uint64_t, DebugMapEntry *> AddressToMapping;
  135. /// For YAMLIO support.
  136. ///@{
  137. typedef std::pair<std::string, SymbolMapping> YAMLSymbolMapping;
  138. friend yaml::MappingTraits<dsymutil::DebugMapObject>;
  139. friend yaml::SequenceTraits<std::vector<std::unique_ptr<DebugMapObject>>>;
  140. friend yaml::SequenceTraits<std::vector<YAMLSymbolMapping>>;
  141. DebugMapObject() = default;
  142. public:
  143. DebugMapObject &operator=(DebugMapObject RHS) {
  144. std::swap(Filename, RHS.Filename);
  145. std::swap(Symbols, RHS.Symbols);
  146. std::swap(AddressToMapping, RHS.AddressToMapping);
  147. return *this;
  148. }
  149. DebugMapObject(DebugMapObject &&RHS) {
  150. Filename = std::move(RHS.Filename);
  151. Symbols = std::move(RHS.Symbols);
  152. AddressToMapping = std::move(RHS.AddressToMapping);
  153. }
  154. ///@}
  155. };
  156. }
  157. }
  158. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::dsymutil::DebugMapObject::YAMLSymbolMapping)
  159. namespace llvm {
  160. namespace yaml {
  161. using namespace llvm::dsymutil;
  162. template <>
  163. struct MappingTraits<std::pair<std::string, DebugMapObject::SymbolMapping>> {
  164. static void mapping(IO &io,
  165. std::pair<std::string, DebugMapObject::SymbolMapping> &s);
  166. static const bool flow = true;
  167. };
  168. template <> struct MappingTraits<dsymutil::DebugMapObject> {
  169. struct YamlDMO;
  170. static void mapping(IO &io, dsymutil::DebugMapObject &DMO);
  171. };
  172. template <> struct ScalarTraits<Triple> {
  173. static void output(const Triple &val, void *, llvm::raw_ostream &out);
  174. static StringRef input(StringRef scalar, void *, Triple &value);
  175. static bool mustQuote(StringRef) { return true; }
  176. };
  177. template <>
  178. struct SequenceTraits<std::vector<std::unique_ptr<dsymutil::DebugMapObject>>> {
  179. static size_t
  180. size(IO &io, std::vector<std::unique_ptr<dsymutil::DebugMapObject>> &seq);
  181. static dsymutil::DebugMapObject &
  182. element(IO &, std::vector<std::unique_ptr<dsymutil::DebugMapObject>> &seq,
  183. size_t index);
  184. };
  185. template <> struct MappingTraits<dsymutil::DebugMap> {
  186. static void mapping(IO &io, dsymutil::DebugMap &DM);
  187. };
  188. template <> struct MappingTraits<std::unique_ptr<dsymutil::DebugMap>> {
  189. static void mapping(IO &io, std::unique_ptr<dsymutil::DebugMap> &DM);
  190. };
  191. }
  192. }
  193. #endif // LLVM_TOOLS_DSYMUTIL_DEBUGMAP_H