DWARFYAML.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. //===- DWARFYAML.h - DWARF YAMLIO implementation ----------------*- 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. /// \file
  10. /// This file declares classes for handling the YAML representation
  11. /// of DWARF Debug Info.
  12. ///
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_OBJECTYAML_DWARFYAML_H
  15. #define LLVM_OBJECTYAML_DWARFYAML_H
  16. #include "llvm/ADT/SetVector.h"
  17. #include "llvm/ADT/StringRef.h"
  18. #include "llvm/BinaryFormat/Dwarf.h"
  19. #include "llvm/ObjectYAML/YAML.h"
  20. #include "llvm/Support/YAMLTraits.h"
  21. #include <cstdint>
  22. #include <unordered_map>
  23. #include <vector>
  24. namespace llvm {
  25. namespace DWARFYAML {
  26. struct AttributeAbbrev {
  27. llvm::dwarf::Attribute Attribute;
  28. llvm::dwarf::Form Form;
  29. llvm::yaml::Hex64 Value; // Some DWARF5 attributes have values
  30. };
  31. struct Abbrev {
  32. Optional<yaml::Hex64> Code;
  33. llvm::dwarf::Tag Tag;
  34. llvm::dwarf::Constants Children;
  35. std::vector<AttributeAbbrev> Attributes;
  36. };
  37. struct AbbrevTable {
  38. Optional<uint64_t> ID;
  39. std::vector<Abbrev> Table;
  40. };
  41. struct ARangeDescriptor {
  42. llvm::yaml::Hex64 Address;
  43. yaml::Hex64 Length;
  44. };
  45. struct ARange {
  46. dwarf::DwarfFormat Format;
  47. Optional<yaml::Hex64> Length;
  48. uint16_t Version;
  49. yaml::Hex64 CuOffset;
  50. Optional<yaml::Hex8> AddrSize;
  51. yaml::Hex8 SegSize;
  52. std::vector<ARangeDescriptor> Descriptors;
  53. };
  54. /// Class that describes a range list entry, or a base address selection entry
  55. /// within a range list in the .debug_ranges section.
  56. struct RangeEntry {
  57. llvm::yaml::Hex64 LowOffset;
  58. llvm::yaml::Hex64 HighOffset;
  59. };
  60. /// Class that describes a single range list inside the .debug_ranges section.
  61. struct Ranges {
  62. Optional<llvm::yaml::Hex64> Offset;
  63. Optional<llvm::yaml::Hex8> AddrSize;
  64. std::vector<RangeEntry> Entries;
  65. };
  66. struct PubEntry {
  67. llvm::yaml::Hex32 DieOffset;
  68. llvm::yaml::Hex8 Descriptor;
  69. StringRef Name;
  70. };
  71. struct PubSection {
  72. dwarf::DwarfFormat Format;
  73. yaml::Hex64 Length;
  74. uint16_t Version;
  75. uint32_t UnitOffset;
  76. uint32_t UnitSize;
  77. std::vector<PubEntry> Entries;
  78. };
  79. struct FormValue {
  80. llvm::yaml::Hex64 Value;
  81. StringRef CStr;
  82. std::vector<llvm::yaml::Hex8> BlockData;
  83. };
  84. struct Entry {
  85. llvm::yaml::Hex32 AbbrCode;
  86. std::vector<FormValue> Values;
  87. };
  88. /// Class that contains helpful context information when mapping YAML into DWARF
  89. /// data structures.
  90. struct DWARFContext {
  91. bool IsGNUPubSec = false;
  92. };
  93. struct Unit {
  94. dwarf::DwarfFormat Format;
  95. Optional<yaml::Hex64> Length;
  96. uint16_t Version;
  97. Optional<uint8_t> AddrSize;
  98. llvm::dwarf::UnitType Type; // Added in DWARF 5
  99. Optional<uint64_t> AbbrevTableID;
  100. Optional<yaml::Hex64> AbbrOffset;
  101. std::vector<Entry> Entries;
  102. };
  103. struct File {
  104. StringRef Name;
  105. uint64_t DirIdx;
  106. uint64_t ModTime;
  107. uint64_t Length;
  108. };
  109. struct LineTableOpcode {
  110. dwarf::LineNumberOps Opcode;
  111. Optional<uint64_t> ExtLen;
  112. dwarf::LineNumberExtendedOps SubOpcode;
  113. uint64_t Data;
  114. int64_t SData;
  115. File FileEntry;
  116. std::vector<llvm::yaml::Hex8> UnknownOpcodeData;
  117. std::vector<llvm::yaml::Hex64> StandardOpcodeData;
  118. };
  119. struct LineTable {
  120. dwarf::DwarfFormat Format;
  121. Optional<uint64_t> Length;
  122. uint16_t Version;
  123. Optional<uint64_t> PrologueLength;
  124. uint8_t MinInstLength;
  125. uint8_t MaxOpsPerInst;
  126. uint8_t DefaultIsStmt;
  127. uint8_t LineBase;
  128. uint8_t LineRange;
  129. Optional<uint8_t> OpcodeBase;
  130. Optional<std::vector<uint8_t>> StandardOpcodeLengths;
  131. std::vector<StringRef> IncludeDirs;
  132. std::vector<File> Files;
  133. std::vector<LineTableOpcode> Opcodes;
  134. };
  135. struct SegAddrPair {
  136. yaml::Hex64 Segment;
  137. yaml::Hex64 Address;
  138. };
  139. struct AddrTableEntry {
  140. dwarf::DwarfFormat Format;
  141. Optional<yaml::Hex64> Length;
  142. yaml::Hex16 Version;
  143. Optional<yaml::Hex8> AddrSize;
  144. yaml::Hex8 SegSelectorSize;
  145. std::vector<SegAddrPair> SegAddrPairs;
  146. };
  147. struct StringOffsetsTable {
  148. dwarf::DwarfFormat Format;
  149. Optional<yaml::Hex64> Length;
  150. yaml::Hex16 Version;
  151. yaml::Hex16 Padding;
  152. std::vector<yaml::Hex64> Offsets;
  153. };
  154. struct DWARFOperation {
  155. dwarf::LocationAtom Operator;
  156. std::vector<yaml::Hex64> Values;
  157. };
  158. struct RnglistEntry {
  159. dwarf::RnglistEntries Operator;
  160. std::vector<yaml::Hex64> Values;
  161. };
  162. struct LoclistEntry {
  163. dwarf::LoclistEntries Operator;
  164. std::vector<yaml::Hex64> Values;
  165. Optional<yaml::Hex64> DescriptionsLength;
  166. std::vector<DWARFOperation> Descriptions;
  167. };
  168. template <typename EntryType> struct ListEntries {
  169. Optional<std::vector<EntryType>> Entries;
  170. Optional<yaml::BinaryRef> Content;
  171. };
  172. template <typename EntryType> struct ListTable {
  173. dwarf::DwarfFormat Format;
  174. Optional<yaml::Hex64> Length;
  175. yaml::Hex16 Version;
  176. Optional<yaml::Hex8> AddrSize;
  177. yaml::Hex8 SegSelectorSize;
  178. Optional<uint32_t> OffsetEntryCount;
  179. Optional<std::vector<yaml::Hex64>> Offsets;
  180. std::vector<ListEntries<EntryType>> Lists;
  181. };
  182. struct Data {
  183. bool IsLittleEndian;
  184. bool Is64BitAddrSize;
  185. std::vector<AbbrevTable> DebugAbbrev;
  186. Optional<std::vector<StringRef>> DebugStrings;
  187. Optional<std::vector<StringOffsetsTable>> DebugStrOffsets;
  188. Optional<std::vector<ARange>> DebugAranges;
  189. Optional<std::vector<Ranges>> DebugRanges;
  190. Optional<std::vector<AddrTableEntry>> DebugAddr;
  191. Optional<PubSection> PubNames;
  192. Optional<PubSection> PubTypes;
  193. Optional<PubSection> GNUPubNames;
  194. Optional<PubSection> GNUPubTypes;
  195. std::vector<Unit> CompileUnits;
  196. std::vector<LineTable> DebugLines;
  197. Optional<std::vector<ListTable<RnglistEntry>>> DebugRnglists;
  198. Optional<std::vector<ListTable<LoclistEntry>>> DebugLoclists;
  199. bool isEmpty() const;
  200. SetVector<StringRef> getNonEmptySectionNames() const;
  201. struct AbbrevTableInfo {
  202. uint64_t Index;
  203. uint64_t Offset;
  204. };
  205. Expected<AbbrevTableInfo> getAbbrevTableInfoByID(uint64_t ID) const;
  206. StringRef getAbbrevTableContentByIndex(uint64_t Index) const;
  207. private:
  208. mutable std::unordered_map<uint64_t, AbbrevTableInfo> AbbrevTableInfoMap;
  209. mutable std::unordered_map<uint64_t, std::string> AbbrevTableContents;
  210. };
  211. } // end namespace DWARFYAML
  212. } // end namespace llvm
  213. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::DWARFYAML::AttributeAbbrev)
  214. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::DWARFYAML::Abbrev)
  215. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::DWARFYAML::AbbrevTable)
  216. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::DWARFYAML::ARangeDescriptor)
  217. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::DWARFYAML::ARange)
  218. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::DWARFYAML::RangeEntry)
  219. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::DWARFYAML::Ranges)
  220. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::DWARFYAML::PubEntry)
  221. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::DWARFYAML::Unit)
  222. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::DWARFYAML::FormValue)
  223. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::DWARFYAML::Entry)
  224. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::DWARFYAML::File)
  225. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::DWARFYAML::LineTable)
  226. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::DWARFYAML::LineTableOpcode)
  227. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::DWARFYAML::SegAddrPair)
  228. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::DWARFYAML::AddrTableEntry)
  229. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::DWARFYAML::StringOffsetsTable)
  230. LLVM_YAML_IS_SEQUENCE_VECTOR(
  231. llvm::DWARFYAML::ListTable<DWARFYAML::RnglistEntry>)
  232. LLVM_YAML_IS_SEQUENCE_VECTOR(
  233. llvm::DWARFYAML::ListEntries<DWARFYAML::RnglistEntry>)
  234. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::DWARFYAML::RnglistEntry)
  235. LLVM_YAML_IS_SEQUENCE_VECTOR(
  236. llvm::DWARFYAML::ListTable<DWARFYAML::LoclistEntry>)
  237. LLVM_YAML_IS_SEQUENCE_VECTOR(
  238. llvm::DWARFYAML::ListEntries<DWARFYAML::LoclistEntry>)
  239. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::DWARFYAML::LoclistEntry)
  240. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::DWARFYAML::DWARFOperation)
  241. namespace llvm {
  242. namespace yaml {
  243. template <> struct MappingTraits<DWARFYAML::Data> {
  244. static void mapping(IO &IO, DWARFYAML::Data &DWARF);
  245. };
  246. template <> struct MappingTraits<DWARFYAML::AbbrevTable> {
  247. static void mapping(IO &IO, DWARFYAML::AbbrevTable &AbbrevTable);
  248. };
  249. template <> struct MappingTraits<DWARFYAML::Abbrev> {
  250. static void mapping(IO &IO, DWARFYAML::Abbrev &Abbrev);
  251. };
  252. template <> struct MappingTraits<DWARFYAML::AttributeAbbrev> {
  253. static void mapping(IO &IO, DWARFYAML::AttributeAbbrev &AttAbbrev);
  254. };
  255. template <> struct MappingTraits<DWARFYAML::ARangeDescriptor> {
  256. static void mapping(IO &IO, DWARFYAML::ARangeDescriptor &Descriptor);
  257. };
  258. template <> struct MappingTraits<DWARFYAML::ARange> {
  259. static void mapping(IO &IO, DWARFYAML::ARange &ARange);
  260. };
  261. template <> struct MappingTraits<DWARFYAML::RangeEntry> {
  262. static void mapping(IO &IO, DWARFYAML::RangeEntry &Entry);
  263. };
  264. template <> struct MappingTraits<DWARFYAML::Ranges> {
  265. static void mapping(IO &IO, DWARFYAML::Ranges &Ranges);
  266. };
  267. template <> struct MappingTraits<DWARFYAML::PubEntry> {
  268. static void mapping(IO &IO, DWARFYAML::PubEntry &Entry);
  269. };
  270. template <> struct MappingTraits<DWARFYAML::PubSection> {
  271. static void mapping(IO &IO, DWARFYAML::PubSection &Section);
  272. };
  273. template <> struct MappingTraits<DWARFYAML::Unit> {
  274. static void mapping(IO &IO, DWARFYAML::Unit &Unit);
  275. };
  276. template <> struct MappingTraits<DWARFYAML::Entry> {
  277. static void mapping(IO &IO, DWARFYAML::Entry &Entry);
  278. };
  279. template <> struct MappingTraits<DWARFYAML::FormValue> {
  280. static void mapping(IO &IO, DWARFYAML::FormValue &FormValue);
  281. };
  282. template <> struct MappingTraits<DWARFYAML::File> {
  283. static void mapping(IO &IO, DWARFYAML::File &File);
  284. };
  285. template <> struct MappingTraits<DWARFYAML::LineTableOpcode> {
  286. static void mapping(IO &IO, DWARFYAML::LineTableOpcode &LineTableOpcode);
  287. };
  288. template <> struct MappingTraits<DWARFYAML::LineTable> {
  289. static void mapping(IO &IO, DWARFYAML::LineTable &LineTable);
  290. };
  291. template <> struct MappingTraits<DWARFYAML::SegAddrPair> {
  292. static void mapping(IO &IO, DWARFYAML::SegAddrPair &SegAddrPair);
  293. };
  294. template <> struct MappingTraits<DWARFYAML::DWARFOperation> {
  295. static void mapping(IO &IO, DWARFYAML::DWARFOperation &DWARFOperation);
  296. };
  297. template <typename EntryType>
  298. struct MappingTraits<DWARFYAML::ListTable<EntryType>> {
  299. static void mapping(IO &IO, DWARFYAML::ListTable<EntryType> &ListTable);
  300. };
  301. template <typename EntryType>
  302. struct MappingTraits<DWARFYAML::ListEntries<EntryType>> {
  303. static void mapping(IO &IO, DWARFYAML::ListEntries<EntryType> &ListEntries);
  304. static std::string validate(IO &IO,
  305. DWARFYAML::ListEntries<EntryType> &ListEntries);
  306. };
  307. template <> struct MappingTraits<DWARFYAML::RnglistEntry> {
  308. static void mapping(IO &IO, DWARFYAML::RnglistEntry &RnglistEntry);
  309. };
  310. template <> struct MappingTraits<DWARFYAML::LoclistEntry> {
  311. static void mapping(IO &IO, DWARFYAML::LoclistEntry &LoclistEntry);
  312. };
  313. template <> struct MappingTraits<DWARFYAML::AddrTableEntry> {
  314. static void mapping(IO &IO, DWARFYAML::AddrTableEntry &AddrTable);
  315. };
  316. template <> struct MappingTraits<DWARFYAML::StringOffsetsTable> {
  317. static void mapping(IO &IO, DWARFYAML::StringOffsetsTable &StrOffsetsTable);
  318. };
  319. template <> struct ScalarEnumerationTraits<dwarf::DwarfFormat> {
  320. static void enumeration(IO &IO, dwarf::DwarfFormat &Format) {
  321. IO.enumCase(Format, "DWARF32", dwarf::DWARF32);
  322. IO.enumCase(Format, "DWARF64", dwarf::DWARF64);
  323. }
  324. };
  325. #define HANDLE_DW_TAG(unused, name, unused2, unused3, unused4) \
  326. io.enumCase(value, "DW_TAG_" #name, dwarf::DW_TAG_##name);
  327. template <> struct ScalarEnumerationTraits<dwarf::Tag> {
  328. static void enumeration(IO &io, dwarf::Tag &value) {
  329. #include "llvm/BinaryFormat/Dwarf.def"
  330. io.enumFallback<Hex16>(value);
  331. }
  332. };
  333. #define HANDLE_DW_LNS(unused, name) \
  334. io.enumCase(value, "DW_LNS_" #name, dwarf::DW_LNS_##name);
  335. template <> struct ScalarEnumerationTraits<dwarf::LineNumberOps> {
  336. static void enumeration(IO &io, dwarf::LineNumberOps &value) {
  337. #include "llvm/BinaryFormat/Dwarf.def"
  338. io.enumFallback<Hex8>(value);
  339. }
  340. };
  341. #define HANDLE_DW_LNE(unused, name) \
  342. io.enumCase(value, "DW_LNE_" #name, dwarf::DW_LNE_##name);
  343. template <> struct ScalarEnumerationTraits<dwarf::LineNumberExtendedOps> {
  344. static void enumeration(IO &io, dwarf::LineNumberExtendedOps &value) {
  345. #include "llvm/BinaryFormat/Dwarf.def"
  346. io.enumFallback<Hex16>(value);
  347. }
  348. };
  349. #define HANDLE_DW_AT(unused, name, unused2, unused3) \
  350. io.enumCase(value, "DW_AT_" #name, dwarf::DW_AT_##name);
  351. template <> struct ScalarEnumerationTraits<dwarf::Attribute> {
  352. static void enumeration(IO &io, dwarf::Attribute &value) {
  353. #include "llvm/BinaryFormat/Dwarf.def"
  354. io.enumFallback<Hex16>(value);
  355. }
  356. };
  357. #define HANDLE_DW_FORM(unused, name, unused2, unused3) \
  358. io.enumCase(value, "DW_FORM_" #name, dwarf::DW_FORM_##name);
  359. template <> struct ScalarEnumerationTraits<dwarf::Form> {
  360. static void enumeration(IO &io, dwarf::Form &value) {
  361. #include "llvm/BinaryFormat/Dwarf.def"
  362. io.enumFallback<Hex16>(value);
  363. }
  364. };
  365. #define HANDLE_DW_UT(unused, name) \
  366. io.enumCase(value, "DW_UT_" #name, dwarf::DW_UT_##name);
  367. template <> struct ScalarEnumerationTraits<dwarf::UnitType> {
  368. static void enumeration(IO &io, dwarf::UnitType &value) {
  369. #include "llvm/BinaryFormat/Dwarf.def"
  370. io.enumFallback<Hex8>(value);
  371. }
  372. };
  373. template <> struct ScalarEnumerationTraits<dwarf::Constants> {
  374. static void enumeration(IO &io, dwarf::Constants &value) {
  375. io.enumCase(value, "DW_CHILDREN_no", dwarf::DW_CHILDREN_no);
  376. io.enumCase(value, "DW_CHILDREN_yes", dwarf::DW_CHILDREN_yes);
  377. io.enumFallback<Hex16>(value);
  378. }
  379. };
  380. #define HANDLE_DW_RLE(unused, name) \
  381. io.enumCase(value, "DW_RLE_" #name, dwarf::DW_RLE_##name);
  382. template <> struct ScalarEnumerationTraits<dwarf::RnglistEntries> {
  383. static void enumeration(IO &io, dwarf::RnglistEntries &value) {
  384. #include "llvm/BinaryFormat/Dwarf.def"
  385. }
  386. };
  387. #define HANDLE_DW_LLE(unused, name) \
  388. io.enumCase(value, "DW_LLE_" #name, dwarf::DW_LLE_##name);
  389. template <> struct ScalarEnumerationTraits<dwarf::LoclistEntries> {
  390. static void enumeration(IO &io, dwarf::LoclistEntries &value) {
  391. #include "llvm/BinaryFormat/Dwarf.def"
  392. }
  393. };
  394. #define HANDLE_DW_OP(id, name, version, vendor) \
  395. io.enumCase(value, "DW_OP_" #name, dwarf::DW_OP_##name);
  396. template <> struct ScalarEnumerationTraits<dwarf::LocationAtom> {
  397. static void enumeration(IO &io, dwarf::LocationAtom &value) {
  398. #include "llvm/BinaryFormat/Dwarf.def"
  399. io.enumFallback<yaml::Hex8>(value);
  400. }
  401. };
  402. } // end namespace yaml
  403. } // end namespace llvm
  404. #endif // LLVM_OBJECTYAML_DWARFYAML_H