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