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