DWARFYAML.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. //===- DWARFYAML.cpp - DWARF YAMLIO implementation ------------------------===//
  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. // This file defines classes for handling the YAML representation of DWARF Debug
  10. // Info.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/ObjectYAML/DWARFYAML.h"
  14. #include "llvm/BinaryFormat/Dwarf.h"
  15. #include "llvm/Support/Errc.h"
  16. #include "llvm/Support/Error.h"
  17. namespace llvm {
  18. bool DWARFYAML::Data::isEmpty() const {
  19. return getNonEmptySectionNames().empty();
  20. }
  21. SetVector<StringRef> DWARFYAML::Data::getNonEmptySectionNames() const {
  22. SetVector<StringRef> SecNames;
  23. if (DebugStrings)
  24. SecNames.insert("debug_str");
  25. if (DebugAranges)
  26. SecNames.insert("debug_aranges");
  27. if (DebugRanges)
  28. SecNames.insert("debug_ranges");
  29. if (!DebugLines.empty())
  30. SecNames.insert("debug_line");
  31. if (DebugAddr)
  32. SecNames.insert("debug_addr");
  33. if (!DebugAbbrev.empty())
  34. SecNames.insert("debug_abbrev");
  35. if (!CompileUnits.empty())
  36. SecNames.insert("debug_info");
  37. if (PubNames)
  38. SecNames.insert("debug_pubnames");
  39. if (PubTypes)
  40. SecNames.insert("debug_pubtypes");
  41. if (GNUPubNames)
  42. SecNames.insert("debug_gnu_pubnames");
  43. if (GNUPubTypes)
  44. SecNames.insert("debug_gnu_pubtypes");
  45. if (DebugStrOffsets)
  46. SecNames.insert("debug_str_offsets");
  47. if (DebugRnglists)
  48. SecNames.insert("debug_rnglists");
  49. if (DebugLoclists)
  50. SecNames.insert("debug_loclists");
  51. return SecNames;
  52. }
  53. Expected<DWARFYAML::Data::AbbrevTableInfo>
  54. DWARFYAML::Data::getAbbrevTableInfoByID(uint64_t ID) const {
  55. if (AbbrevTableInfoMap.empty()) {
  56. uint64_t AbbrevTableOffset = 0;
  57. for (auto &AbbrevTable : enumerate(DebugAbbrev)) {
  58. // If the abbrev table's ID isn't specified, we use the index as its ID.
  59. uint64_t AbbrevTableID =
  60. AbbrevTable.value().ID.value_or(AbbrevTable.index());
  61. auto It = AbbrevTableInfoMap.insert(
  62. {AbbrevTableID, AbbrevTableInfo{/*Index=*/AbbrevTable.index(),
  63. /*Offset=*/AbbrevTableOffset}});
  64. if (!It.second)
  65. return createStringError(
  66. errc::invalid_argument,
  67. "the ID (%" PRIu64 ") of abbrev table with index %zu has been used "
  68. "by abbrev table with index %" PRIu64,
  69. AbbrevTableID, AbbrevTable.index(), It.first->second.Index);
  70. AbbrevTableOffset +=
  71. getAbbrevTableContentByIndex(AbbrevTable.index()).size();
  72. }
  73. }
  74. auto It = AbbrevTableInfoMap.find(ID);
  75. if (It == AbbrevTableInfoMap.end())
  76. return createStringError(errc::invalid_argument,
  77. "cannot find abbrev table whose ID is %" PRIu64,
  78. ID);
  79. return It->second;
  80. }
  81. namespace yaml {
  82. void MappingTraits<DWARFYAML::Data>::mapping(IO &IO, DWARFYAML::Data &DWARF) {
  83. void *OldContext = IO.getContext();
  84. DWARFYAML::DWARFContext DWARFCtx;
  85. IO.setContext(&DWARFCtx);
  86. IO.mapOptional("debug_str", DWARF.DebugStrings);
  87. IO.mapOptional("debug_abbrev", DWARF.DebugAbbrev);
  88. IO.mapOptional("debug_aranges", DWARF.DebugAranges);
  89. IO.mapOptional("debug_ranges", DWARF.DebugRanges);
  90. IO.mapOptional("debug_pubnames", DWARF.PubNames);
  91. IO.mapOptional("debug_pubtypes", DWARF.PubTypes);
  92. DWARFCtx.IsGNUPubSec = true;
  93. IO.mapOptional("debug_gnu_pubnames", DWARF.GNUPubNames);
  94. IO.mapOptional("debug_gnu_pubtypes", DWARF.GNUPubTypes);
  95. IO.mapOptional("debug_info", DWARF.CompileUnits);
  96. IO.mapOptional("debug_line", DWARF.DebugLines);
  97. IO.mapOptional("debug_addr", DWARF.DebugAddr);
  98. IO.mapOptional("debug_str_offsets", DWARF.DebugStrOffsets);
  99. IO.mapOptional("debug_rnglists", DWARF.DebugRnglists);
  100. IO.mapOptional("debug_loclists", DWARF.DebugLoclists);
  101. IO.setContext(OldContext);
  102. }
  103. void MappingTraits<DWARFYAML::AbbrevTable>::mapping(
  104. IO &IO, DWARFYAML::AbbrevTable &AbbrevTable) {
  105. IO.mapOptional("ID", AbbrevTable.ID);
  106. IO.mapOptional("Table", AbbrevTable.Table);
  107. }
  108. void MappingTraits<DWARFYAML::Abbrev>::mapping(IO &IO,
  109. DWARFYAML::Abbrev &Abbrev) {
  110. IO.mapOptional("Code", Abbrev.Code);
  111. IO.mapRequired("Tag", Abbrev.Tag);
  112. IO.mapRequired("Children", Abbrev.Children);
  113. IO.mapOptional("Attributes", Abbrev.Attributes);
  114. }
  115. void MappingTraits<DWARFYAML::AttributeAbbrev>::mapping(
  116. IO &IO, DWARFYAML::AttributeAbbrev &AttAbbrev) {
  117. IO.mapRequired("Attribute", AttAbbrev.Attribute);
  118. IO.mapRequired("Form", AttAbbrev.Form);
  119. if(AttAbbrev.Form == dwarf::DW_FORM_implicit_const)
  120. IO.mapRequired("Value", AttAbbrev.Value);
  121. }
  122. void MappingTraits<DWARFYAML::ARangeDescriptor>::mapping(
  123. IO &IO, DWARFYAML::ARangeDescriptor &Descriptor) {
  124. IO.mapRequired("Address", Descriptor.Address);
  125. IO.mapRequired("Length", Descriptor.Length);
  126. }
  127. void MappingTraits<DWARFYAML::ARange>::mapping(IO &IO,
  128. DWARFYAML::ARange &ARange) {
  129. IO.mapOptional("Format", ARange.Format, dwarf::DWARF32);
  130. IO.mapOptional("Length", ARange.Length);
  131. IO.mapRequired("Version", ARange.Version);
  132. IO.mapRequired("CuOffset", ARange.CuOffset);
  133. IO.mapOptional("AddressSize", ARange.AddrSize);
  134. IO.mapOptional("SegmentSelectorSize", ARange.SegSize, 0);
  135. IO.mapOptional("Descriptors", ARange.Descriptors);
  136. }
  137. void MappingTraits<DWARFYAML::RangeEntry>::mapping(
  138. IO &IO, DWARFYAML::RangeEntry &Descriptor) {
  139. IO.mapRequired("LowOffset", Descriptor.LowOffset);
  140. IO.mapRequired("HighOffset", Descriptor.HighOffset);
  141. }
  142. void MappingTraits<DWARFYAML::Ranges>::mapping(IO &IO,
  143. DWARFYAML::Ranges &DebugRanges) {
  144. IO.mapOptional("Offset", DebugRanges.Offset);
  145. IO.mapOptional("AddrSize", DebugRanges.AddrSize);
  146. IO.mapRequired("Entries", DebugRanges.Entries);
  147. }
  148. void MappingTraits<DWARFYAML::PubEntry>::mapping(IO &IO,
  149. DWARFYAML::PubEntry &Entry) {
  150. IO.mapRequired("DieOffset", Entry.DieOffset);
  151. if (static_cast<DWARFYAML::DWARFContext *>(IO.getContext())->IsGNUPubSec)
  152. IO.mapRequired("Descriptor", Entry.Descriptor);
  153. IO.mapRequired("Name", Entry.Name);
  154. }
  155. void MappingTraits<DWARFYAML::PubSection>::mapping(
  156. IO &IO, DWARFYAML::PubSection &Section) {
  157. IO.mapOptional("Format", Section.Format, dwarf::DWARF32);
  158. IO.mapRequired("Length", Section.Length);
  159. IO.mapRequired("Version", Section.Version);
  160. IO.mapRequired("UnitOffset", Section.UnitOffset);
  161. IO.mapRequired("UnitSize", Section.UnitSize);
  162. IO.mapRequired("Entries", Section.Entries);
  163. }
  164. void MappingTraits<DWARFYAML::Unit>::mapping(IO &IO, DWARFYAML::Unit &Unit) {
  165. IO.mapOptional("Format", Unit.Format, dwarf::DWARF32);
  166. IO.mapOptional("Length", Unit.Length);
  167. IO.mapRequired("Version", Unit.Version);
  168. if (Unit.Version >= 5)
  169. IO.mapRequired("UnitType", Unit.Type);
  170. IO.mapOptional("AbbrevTableID", Unit.AbbrevTableID);
  171. IO.mapOptional("AbbrOffset", Unit.AbbrOffset);
  172. IO.mapOptional("AddrSize", Unit.AddrSize);
  173. IO.mapOptional("Entries", Unit.Entries);
  174. }
  175. void MappingTraits<DWARFYAML::Entry>::mapping(IO &IO, DWARFYAML::Entry &Entry) {
  176. IO.mapRequired("AbbrCode", Entry.AbbrCode);
  177. IO.mapOptional("Values", Entry.Values);
  178. }
  179. void MappingTraits<DWARFYAML::FormValue>::mapping(
  180. IO &IO, DWARFYAML::FormValue &FormValue) {
  181. IO.mapOptional("Value", FormValue.Value);
  182. if (!FormValue.CStr.empty() || !IO.outputting())
  183. IO.mapOptional("CStr", FormValue.CStr);
  184. if (!FormValue.BlockData.empty() || !IO.outputting())
  185. IO.mapOptional("BlockData", FormValue.BlockData);
  186. }
  187. void MappingTraits<DWARFYAML::File>::mapping(IO &IO, DWARFYAML::File &File) {
  188. IO.mapRequired("Name", File.Name);
  189. IO.mapRequired("DirIdx", File.DirIdx);
  190. IO.mapRequired("ModTime", File.ModTime);
  191. IO.mapRequired("Length", File.Length);
  192. }
  193. void MappingTraits<DWARFYAML::LineTableOpcode>::mapping(
  194. IO &IO, DWARFYAML::LineTableOpcode &LineTableOpcode) {
  195. IO.mapRequired("Opcode", LineTableOpcode.Opcode);
  196. if (LineTableOpcode.Opcode == dwarf::DW_LNS_extended_op) {
  197. IO.mapOptional("ExtLen", LineTableOpcode.ExtLen);
  198. IO.mapRequired("SubOpcode", LineTableOpcode.SubOpcode);
  199. }
  200. if (!LineTableOpcode.UnknownOpcodeData.empty() || !IO.outputting())
  201. IO.mapOptional("UnknownOpcodeData", LineTableOpcode.UnknownOpcodeData);
  202. if (!LineTableOpcode.UnknownOpcodeData.empty() || !IO.outputting())
  203. IO.mapOptional("StandardOpcodeData", LineTableOpcode.StandardOpcodeData);
  204. if (!LineTableOpcode.FileEntry.Name.empty() || !IO.outputting())
  205. IO.mapOptional("FileEntry", LineTableOpcode.FileEntry);
  206. if (LineTableOpcode.Opcode == dwarf::DW_LNS_advance_line || !IO.outputting())
  207. IO.mapOptional("SData", LineTableOpcode.SData);
  208. IO.mapOptional("Data", LineTableOpcode.Data);
  209. }
  210. void MappingTraits<DWARFYAML::LineTable>::mapping(
  211. IO &IO, DWARFYAML::LineTable &LineTable) {
  212. IO.mapOptional("Format", LineTable.Format, dwarf::DWARF32);
  213. IO.mapOptional("Length", LineTable.Length);
  214. IO.mapRequired("Version", LineTable.Version);
  215. IO.mapOptional("PrologueLength", LineTable.PrologueLength);
  216. IO.mapRequired("MinInstLength", LineTable.MinInstLength);
  217. if(LineTable.Version >= 4)
  218. IO.mapRequired("MaxOpsPerInst", LineTable.MaxOpsPerInst);
  219. IO.mapRequired("DefaultIsStmt", LineTable.DefaultIsStmt);
  220. IO.mapRequired("LineBase", LineTable.LineBase);
  221. IO.mapRequired("LineRange", LineTable.LineRange);
  222. IO.mapOptional("OpcodeBase", LineTable.OpcodeBase);
  223. IO.mapOptional("StandardOpcodeLengths", LineTable.StandardOpcodeLengths);
  224. IO.mapOptional("IncludeDirs", LineTable.IncludeDirs);
  225. IO.mapOptional("Files", LineTable.Files);
  226. IO.mapOptional("Opcodes", LineTable.Opcodes);
  227. }
  228. void MappingTraits<DWARFYAML::SegAddrPair>::mapping(
  229. IO &IO, DWARFYAML::SegAddrPair &SegAddrPair) {
  230. IO.mapOptional("Segment", SegAddrPair.Segment, 0);
  231. IO.mapOptional("Address", SegAddrPair.Address, 0);
  232. }
  233. void MappingTraits<DWARFYAML::AddrTableEntry>::mapping(
  234. IO &IO, DWARFYAML::AddrTableEntry &AddrTable) {
  235. IO.mapOptional("Format", AddrTable.Format, dwarf::DWARF32);
  236. IO.mapOptional("Length", AddrTable.Length);
  237. IO.mapRequired("Version", AddrTable.Version);
  238. IO.mapOptional("AddressSize", AddrTable.AddrSize);
  239. IO.mapOptional("SegmentSelectorSize", AddrTable.SegSelectorSize, 0);
  240. IO.mapOptional("Entries", AddrTable.SegAddrPairs);
  241. }
  242. void MappingTraits<DWARFYAML::StringOffsetsTable>::mapping(
  243. IO &IO, DWARFYAML::StringOffsetsTable &StrOffsetsTable) {
  244. IO.mapOptional("Format", StrOffsetsTable.Format, dwarf::DWARF32);
  245. IO.mapOptional("Length", StrOffsetsTable.Length);
  246. IO.mapOptional("Version", StrOffsetsTable.Version, 5);
  247. IO.mapOptional("Padding", StrOffsetsTable.Padding, 0);
  248. IO.mapOptional("Offsets", StrOffsetsTable.Offsets);
  249. }
  250. void MappingTraits<DWARFYAML::DWARFOperation>::mapping(
  251. IO &IO, DWARFYAML::DWARFOperation &DWARFOperation) {
  252. IO.mapRequired("Operator", DWARFOperation.Operator);
  253. IO.mapOptional("Values", DWARFOperation.Values);
  254. }
  255. void MappingTraits<DWARFYAML::RnglistEntry>::mapping(
  256. IO &IO, DWARFYAML::RnglistEntry &RnglistEntry) {
  257. IO.mapRequired("Operator", RnglistEntry.Operator);
  258. IO.mapOptional("Values", RnglistEntry.Values);
  259. }
  260. void MappingTraits<DWARFYAML::LoclistEntry>::mapping(
  261. IO &IO, DWARFYAML::LoclistEntry &LoclistEntry) {
  262. IO.mapRequired("Operator", LoclistEntry.Operator);
  263. IO.mapOptional("Values", LoclistEntry.Values);
  264. IO.mapOptional("DescriptionsLength", LoclistEntry.DescriptionsLength);
  265. IO.mapOptional("Descriptions", LoclistEntry.Descriptions);
  266. }
  267. template <typename EntryType>
  268. void MappingTraits<DWARFYAML::ListEntries<EntryType>>::mapping(
  269. IO &IO, DWARFYAML::ListEntries<EntryType> &ListEntries) {
  270. IO.mapOptional("Entries", ListEntries.Entries);
  271. IO.mapOptional("Content", ListEntries.Content);
  272. }
  273. template <typename EntryType>
  274. std::string MappingTraits<DWARFYAML::ListEntries<EntryType>>::validate(
  275. IO &IO, DWARFYAML::ListEntries<EntryType> &ListEntries) {
  276. if (ListEntries.Entries && ListEntries.Content)
  277. return "Entries and Content can't be used together";
  278. return "";
  279. }
  280. template <typename EntryType>
  281. void MappingTraits<DWARFYAML::ListTable<EntryType>>::mapping(
  282. IO &IO, DWARFYAML::ListTable<EntryType> &ListTable) {
  283. IO.mapOptional("Format", ListTable.Format, dwarf::DWARF32);
  284. IO.mapOptional("Length", ListTable.Length);
  285. IO.mapOptional("Version", ListTable.Version, 5);
  286. IO.mapOptional("AddressSize", ListTable.AddrSize);
  287. IO.mapOptional("SegmentSelectorSize", ListTable.SegSelectorSize, 0);
  288. IO.mapOptional("OffsetEntryCount", ListTable.OffsetEntryCount);
  289. IO.mapOptional("Offsets", ListTable.Offsets);
  290. IO.mapOptional("Lists", ListTable.Lists);
  291. }
  292. } // end namespace yaml
  293. } // end namespace llvm