DWARFYAML.h 15 KB

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