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 
17 namespace llvm {
18 
19 bool DWARFYAML::Data::isEmpty() const {
20   return DebugStrings.empty() && AbbrevDecls.empty() && DebugAranges &&
21          DebugRanges.empty() && !PubNames && !PubTypes && !GNUPubNames &&
22          !GNUPubTypes && CompileUnits.empty() && DebugLines.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 (!AbbrevDecls.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 namespace yaml {
59 
60 void MappingTraits<DWARFYAML::Data>::mapping(IO &IO, DWARFYAML::Data &DWARF) {
61   void *OldContext = IO.getContext();
62   DWARFYAML::DWARFContext DWARFCtx;
63   IO.setContext(&DWARFCtx);
64   IO.mapOptional("debug_str", DWARF.DebugStrings);
65   IO.mapOptional("debug_abbrev", DWARF.AbbrevDecls);
66   IO.mapOptional("debug_aranges", DWARF.DebugAranges);
67   if (!DWARF.DebugRanges.empty() || !IO.outputting())
68     IO.mapOptional("debug_ranges", DWARF.DebugRanges);
69   IO.mapOptional("debug_pubnames", DWARF.PubNames);
70   IO.mapOptional("debug_pubtypes", DWARF.PubTypes);
71   DWARFCtx.IsGNUPubSec = true;
72   IO.mapOptional("debug_gnu_pubnames", DWARF.GNUPubNames);
73   IO.mapOptional("debug_gnu_pubtypes", DWARF.GNUPubTypes);
74   IO.mapOptional("debug_info", DWARF.CompileUnits);
75   IO.mapOptional("debug_line", DWARF.DebugLines);
76   IO.mapOptional("debug_addr", DWARF.DebugAddr);
77   IO.mapOptional("debug_str_offsets", DWARF.DebugStrOffsets);
78   IO.mapOptional("debug_rnglists", DWARF.DebugRnglists);
79   IO.mapOptional("debug_loclists", DWARF.DebugLoclists);
80   IO.setContext(OldContext);
81 }
82 
83 void MappingTraits<DWARFYAML::Abbrev>::mapping(IO &IO,
84                                                DWARFYAML::Abbrev &Abbrev) {
85   IO.mapOptional("Code", Abbrev.Code);
86   IO.mapRequired("Tag", Abbrev.Tag);
87   IO.mapRequired("Children", Abbrev.Children);
88   IO.mapRequired("Attributes", Abbrev.Attributes);
89 }
90 
91 void MappingTraits<DWARFYAML::AttributeAbbrev>::mapping(
92     IO &IO, DWARFYAML::AttributeAbbrev &AttAbbrev) {
93   IO.mapRequired("Attribute", AttAbbrev.Attribute);
94   IO.mapRequired("Form", AttAbbrev.Form);
95   if(AttAbbrev.Form == dwarf::DW_FORM_implicit_const)
96     IO.mapRequired("Value", AttAbbrev.Value);
97 }
98 
99 void MappingTraits<DWARFYAML::ARangeDescriptor>::mapping(
100     IO &IO, DWARFYAML::ARangeDescriptor &Descriptor) {
101   IO.mapRequired("Address", Descriptor.Address);
102   IO.mapRequired("Length", Descriptor.Length);
103 }
104 
105 void MappingTraits<DWARFYAML::ARange>::mapping(IO &IO,
106                                                DWARFYAML::ARange &ARange) {
107   IO.mapOptional("Format", ARange.Format, dwarf::DWARF32);
108   IO.mapOptional("Length", ARange.Length);
109   IO.mapRequired("Version", ARange.Version);
110   IO.mapRequired("CuOffset", ARange.CuOffset);
111   IO.mapOptional("AddressSize", ARange.AddrSize);
112   IO.mapOptional("SegmentSelectorSize", ARange.SegSize, 0);
113   IO.mapOptional("Descriptors", ARange.Descriptors);
114 }
115 
116 void MappingTraits<DWARFYAML::RangeEntry>::mapping(
117     IO &IO, DWARFYAML::RangeEntry &Descriptor) {
118   IO.mapRequired("LowOffset", Descriptor.LowOffset);
119   IO.mapRequired("HighOffset", Descriptor.HighOffset);
120 }
121 
122 void MappingTraits<DWARFYAML::Ranges>::mapping(IO &IO,
123                                                DWARFYAML::Ranges &DebugRanges) {
124   IO.mapOptional("Offset", DebugRanges.Offset);
125   IO.mapOptional("AddrSize", DebugRanges.AddrSize);
126   IO.mapRequired("Entries", DebugRanges.Entries);
127 }
128 
129 void MappingTraits<DWARFYAML::PubEntry>::mapping(IO &IO,
130                                                  DWARFYAML::PubEntry &Entry) {
131   IO.mapRequired("DieOffset", Entry.DieOffset);
132   if (static_cast<DWARFYAML::DWARFContext *>(IO.getContext())->IsGNUPubSec)
133     IO.mapRequired("Descriptor", Entry.Descriptor);
134   IO.mapRequired("Name", Entry.Name);
135 }
136 
137 void MappingTraits<DWARFYAML::PubSection>::mapping(
138     IO &IO, DWARFYAML::PubSection &Section) {
139   IO.mapRequired("Length", Section.Length);
140   IO.mapRequired("Version", Section.Version);
141   IO.mapRequired("UnitOffset", Section.UnitOffset);
142   IO.mapRequired("UnitSize", Section.UnitSize);
143   IO.mapRequired("Entries", Section.Entries);
144 }
145 
146 void MappingTraits<DWARFYAML::Unit>::mapping(IO &IO, DWARFYAML::Unit &Unit) {
147   IO.mapOptional("Format", Unit.Format, dwarf::DWARF32);
148   IO.mapOptional("Length", Unit.Length);
149   IO.mapRequired("Version", Unit.Version);
150   if (Unit.Version >= 5)
151     IO.mapRequired("UnitType", Unit.Type);
152   IO.mapRequired("AbbrOffset", Unit.AbbrOffset);
153   IO.mapRequired("AddrSize", Unit.AddrSize);
154   IO.mapOptional("Entries", Unit.Entries);
155 }
156 
157 void MappingTraits<DWARFYAML::Entry>::mapping(IO &IO, DWARFYAML::Entry &Entry) {
158   IO.mapRequired("AbbrCode", Entry.AbbrCode);
159   IO.mapOptional("Values", Entry.Values);
160 }
161 
162 void MappingTraits<DWARFYAML::FormValue>::mapping(
163     IO &IO, DWARFYAML::FormValue &FormValue) {
164   IO.mapOptional("Value", FormValue.Value);
165   if (!FormValue.CStr.empty() || !IO.outputting())
166     IO.mapOptional("CStr", FormValue.CStr);
167   if (!FormValue.BlockData.empty() || !IO.outputting())
168     IO.mapOptional("BlockData", FormValue.BlockData);
169 }
170 
171 void MappingTraits<DWARFYAML::File>::mapping(IO &IO, DWARFYAML::File &File) {
172   IO.mapRequired("Name", File.Name);
173   IO.mapRequired("DirIdx", File.DirIdx);
174   IO.mapRequired("ModTime", File.ModTime);
175   IO.mapRequired("Length", File.Length);
176 }
177 
178 void MappingTraits<DWARFYAML::LineTableOpcode>::mapping(
179     IO &IO, DWARFYAML::LineTableOpcode &LineTableOpcode) {
180   IO.mapRequired("Opcode", LineTableOpcode.Opcode);
181   if (LineTableOpcode.Opcode == dwarf::DW_LNS_extended_op) {
182     IO.mapRequired("ExtLen", LineTableOpcode.ExtLen);
183     IO.mapRequired("SubOpcode", LineTableOpcode.SubOpcode);
184   }
185 
186   if (!LineTableOpcode.UnknownOpcodeData.empty() || !IO.outputting())
187     IO.mapOptional("UnknownOpcodeData", LineTableOpcode.UnknownOpcodeData);
188   if (!LineTableOpcode.UnknownOpcodeData.empty() || !IO.outputting())
189     IO.mapOptional("StandardOpcodeData", LineTableOpcode.StandardOpcodeData);
190   if (!LineTableOpcode.FileEntry.Name.empty() || !IO.outputting())
191     IO.mapOptional("FileEntry", LineTableOpcode.FileEntry);
192   if (LineTableOpcode.Opcode == dwarf::DW_LNS_advance_line || !IO.outputting())
193     IO.mapOptional("SData", LineTableOpcode.SData);
194   IO.mapOptional("Data", LineTableOpcode.Data);
195 }
196 
197 void MappingTraits<DWARFYAML::LineTable>::mapping(
198     IO &IO, DWARFYAML::LineTable &LineTable) {
199   IO.mapOptional("Format", LineTable.Format, dwarf::DWARF32);
200   IO.mapRequired("Length", LineTable.Length);
201   IO.mapRequired("Version", LineTable.Version);
202   IO.mapRequired("PrologueLength", LineTable.PrologueLength);
203   IO.mapRequired("MinInstLength", LineTable.MinInstLength);
204   if(LineTable.Version >= 4)
205     IO.mapRequired("MaxOpsPerInst", LineTable.MaxOpsPerInst);
206   IO.mapRequired("DefaultIsStmt", LineTable.DefaultIsStmt);
207   IO.mapRequired("LineBase", LineTable.LineBase);
208   IO.mapRequired("LineRange", LineTable.LineRange);
209   IO.mapRequired("OpcodeBase", LineTable.OpcodeBase);
210   IO.mapRequired("StandardOpcodeLengths", LineTable.StandardOpcodeLengths);
211   IO.mapRequired("IncludeDirs", LineTable.IncludeDirs);
212   IO.mapRequired("Files", LineTable.Files);
213   IO.mapRequired("Opcodes", LineTable.Opcodes);
214 }
215 
216 void MappingTraits<DWARFYAML::SegAddrPair>::mapping(
217     IO &IO, DWARFYAML::SegAddrPair &SegAddrPair) {
218   IO.mapOptional("Segment", SegAddrPair.Segment, 0);
219   IO.mapOptional("Address", SegAddrPair.Address, 0);
220 }
221 
222 void MappingTraits<DWARFYAML::AddrTableEntry>::mapping(
223     IO &IO, DWARFYAML::AddrTableEntry &AddrTable) {
224   IO.mapOptional("Format", AddrTable.Format, dwarf::DWARF32);
225   IO.mapOptional("Length", AddrTable.Length);
226   IO.mapRequired("Version", AddrTable.Version);
227   IO.mapOptional("AddressSize", AddrTable.AddrSize);
228   IO.mapOptional("SegmentSelectorSize", AddrTable.SegSelectorSize, 0);
229   IO.mapOptional("Entries", AddrTable.SegAddrPairs);
230 }
231 
232 void MappingTraits<DWARFYAML::StringOffsetsTable>::mapping(
233     IO &IO, DWARFYAML::StringOffsetsTable &StrOffsetsTable) {
234   IO.mapOptional("Format", StrOffsetsTable.Format, dwarf::DWARF32);
235   IO.mapOptional("Length", StrOffsetsTable.Length);
236   IO.mapOptional("Version", StrOffsetsTable.Version, 5);
237   IO.mapOptional("Padding", StrOffsetsTable.Padding, 0);
238   IO.mapOptional("Offsets", StrOffsetsTable.Offsets);
239 }
240 
241 void MappingTraits<DWARFYAML::DWARFOperation>::mapping(
242     IO &IO, DWARFYAML::DWARFOperation &DWARFOperation) {
243   IO.mapRequired("Operator", DWARFOperation.Operator);
244   IO.mapOptional("Values", DWARFOperation.Values);
245 }
246 
247 void MappingTraits<DWARFYAML::RnglistEntry>::mapping(
248     IO &IO, DWARFYAML::RnglistEntry &RnglistEntry) {
249   IO.mapRequired("Operator", RnglistEntry.Operator);
250   IO.mapOptional("Values", RnglistEntry.Values);
251 }
252 
253 void MappingTraits<DWARFYAML::LoclistEntry>::mapping(
254     IO &IO, DWARFYAML::LoclistEntry &LoclistEntry) {
255   IO.mapRequired("Operator", LoclistEntry.Operator);
256   IO.mapOptional("Values", LoclistEntry.Values);
257   IO.mapOptional("DescriptionsLength", LoclistEntry.DescriptionsLength);
258   IO.mapOptional("Descriptions", LoclistEntry.Descriptions);
259 }
260 
261 template <typename EntryType>
262 void MappingTraits<DWARFYAML::ListEntries<EntryType>>::mapping(
263     IO &IO, DWARFYAML::ListEntries<EntryType> &ListEntries) {
264   IO.mapOptional("Entries", ListEntries.Entries);
265   IO.mapOptional("Content", ListEntries.Content);
266 }
267 
268 template <typename EntryType>
269 StringRef MappingTraits<DWARFYAML::ListEntries<EntryType>>::validate(
270     IO &IO, DWARFYAML::ListEntries<EntryType> &ListEntries) {
271   if (ListEntries.Entries && ListEntries.Content)
272     return "Entries and Content can't be used together";
273   return StringRef();
274 }
275 
276 template <typename EntryType>
277 void MappingTraits<DWARFYAML::ListTable<EntryType>>::mapping(
278     IO &IO, DWARFYAML::ListTable<EntryType> &ListTable) {
279   IO.mapOptional("Format", ListTable.Format, dwarf::DWARF32);
280   IO.mapOptional("Length", ListTable.Length);
281   IO.mapOptional("Version", ListTable.Version, 5);
282   IO.mapOptional("AddressSize", ListTable.AddrSize);
283   IO.mapOptional("SegmentSelectorSize", ListTable.SegSelectorSize, 0);
284   IO.mapOptional("OffsetEntryCount", ListTable.OffsetEntryCount);
285   IO.mapOptional("Offsets", ListTable.Offsets);
286   IO.mapOptional("Lists", ListTable.Lists);
287 }
288 
289 void MappingTraits<DWARFYAML::InitialLength>::mapping(
290     IO &IO, DWARFYAML::InitialLength &InitialLength) {
291   IO.mapRequired("TotalLength", InitialLength.TotalLength);
292   if (InitialLength.isDWARF64())
293     IO.mapRequired("TotalLength64", InitialLength.TotalLength64);
294 }
295 
296 } // end namespace yaml
297 
298 } // end namespace llvm
299