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