DWARFYAML.h 15 KB

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