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 
isEmpty() const19 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 
getUsedSectionNames() const25 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   return SecNames;
50 }
51 
52 namespace yaml {
53 
mapping(IO & IO,DWARFYAML::Data & DWARF)54 void MappingTraits<DWARFYAML::Data>::mapping(IO &IO, DWARFYAML::Data &DWARF) {
55   void *OldContext = IO.getContext();
56   DWARFYAML::DWARFContext DWARFCtx;
57   IO.setContext(&DWARFCtx);
58   IO.mapOptional("debug_str", DWARF.DebugStrings);
59   IO.mapOptional("debug_abbrev", DWARF.AbbrevDecls);
60   if (!DWARF.ARanges.empty() || !IO.outputting())
61     IO.mapOptional("debug_aranges", DWARF.ARanges);
62   if (!DWARF.DebugRanges.empty() || !IO.outputting())
63     IO.mapOptional("debug_ranges", DWARF.DebugRanges);
64   IO.mapOptional("debug_pubnames", DWARF.PubNames);
65   IO.mapOptional("debug_pubtypes", DWARF.PubTypes);
66   DWARFCtx.IsGNUPubSec = true;
67   IO.mapOptional("debug_gnu_pubnames", DWARF.GNUPubNames);
68   IO.mapOptional("debug_gnu_pubtypes", DWARF.GNUPubTypes);
69   IO.mapOptional("debug_info", DWARF.CompileUnits);
70   IO.mapOptional("debug_line", DWARF.DebugLines);
71   IO.mapOptional("debug_addr", DWARF.DebugAddr);
72   IO.setContext(OldContext);
73 }
74 
mapping(IO & IO,DWARFYAML::Abbrev & Abbrev)75 void MappingTraits<DWARFYAML::Abbrev>::mapping(IO &IO,
76                                                DWARFYAML::Abbrev &Abbrev) {
77   IO.mapOptional("Code", Abbrev.Code);
78   IO.mapRequired("Tag", Abbrev.Tag);
79   IO.mapRequired("Children", Abbrev.Children);
80   IO.mapRequired("Attributes", Abbrev.Attributes);
81 }
82 
mapping(IO & IO,DWARFYAML::AttributeAbbrev & AttAbbrev)83 void MappingTraits<DWARFYAML::AttributeAbbrev>::mapping(
84     IO &IO, DWARFYAML::AttributeAbbrev &AttAbbrev) {
85   IO.mapRequired("Attribute", AttAbbrev.Attribute);
86   IO.mapRequired("Form", AttAbbrev.Form);
87   if(AttAbbrev.Form == dwarf::DW_FORM_implicit_const)
88     IO.mapRequired("Value", AttAbbrev.Value);
89 }
90 
mapping(IO & IO,DWARFYAML::ARangeDescriptor & Descriptor)91 void MappingTraits<DWARFYAML::ARangeDescriptor>::mapping(
92     IO &IO, DWARFYAML::ARangeDescriptor &Descriptor) {
93   IO.mapRequired("Address", Descriptor.Address);
94   IO.mapRequired("Length", Descriptor.Length);
95 }
96 
mapping(IO & IO,DWARFYAML::ARange & ARange)97 void MappingTraits<DWARFYAML::ARange>::mapping(IO &IO,
98                                                DWARFYAML::ARange &ARange) {
99   IO.mapOptional("Format", ARange.Format, dwarf::DWARF32);
100   IO.mapRequired("Length", ARange.Length);
101   IO.mapRequired("Version", ARange.Version);
102   IO.mapRequired("CuOffset", ARange.CuOffset);
103   IO.mapRequired("AddrSize", ARange.AddrSize);
104   IO.mapRequired("SegSize", ARange.SegSize);
105   IO.mapRequired("Descriptors", ARange.Descriptors);
106 }
107 
mapping(IO & IO,DWARFYAML::RangeEntry & Descriptor)108 void MappingTraits<DWARFYAML::RangeEntry>::mapping(
109     IO &IO, DWARFYAML::RangeEntry &Descriptor) {
110   IO.mapRequired("LowOffset", Descriptor.LowOffset);
111   IO.mapRequired("HighOffset", Descriptor.HighOffset);
112 }
113 
mapping(IO & IO,DWARFYAML::Ranges & DebugRanges)114 void MappingTraits<DWARFYAML::Ranges>::mapping(IO &IO,
115                                                DWARFYAML::Ranges &DebugRanges) {
116   IO.mapOptional("Offset", DebugRanges.Offset);
117   IO.mapOptional("AddrSize", DebugRanges.AddrSize);
118   IO.mapRequired("Entries", DebugRanges.Entries);
119 }
120 
mapping(IO & IO,DWARFYAML::PubEntry & Entry)121 void MappingTraits<DWARFYAML::PubEntry>::mapping(IO &IO,
122                                                  DWARFYAML::PubEntry &Entry) {
123   IO.mapRequired("DieOffset", Entry.DieOffset);
124   if (static_cast<DWARFYAML::DWARFContext *>(IO.getContext())->IsGNUPubSec)
125     IO.mapRequired("Descriptor", Entry.Descriptor);
126   IO.mapRequired("Name", Entry.Name);
127 }
128 
mapping(IO & IO,DWARFYAML::PubSection & Section)129 void MappingTraits<DWARFYAML::PubSection>::mapping(
130     IO &IO, DWARFYAML::PubSection &Section) {
131   IO.mapRequired("Length", Section.Length);
132   IO.mapRequired("Version", Section.Version);
133   IO.mapRequired("UnitOffset", Section.UnitOffset);
134   IO.mapRequired("UnitSize", Section.UnitSize);
135   IO.mapRequired("Entries", Section.Entries);
136 }
137 
mapping(IO & IO,DWARFYAML::Unit & Unit)138 void MappingTraits<DWARFYAML::Unit>::mapping(IO &IO, DWARFYAML::Unit &Unit) {
139   IO.mapOptional("Format", Unit.Format, dwarf::DWARF32);
140   IO.mapRequired("Length", Unit.Length);
141   IO.mapRequired("Version", Unit.Version);
142   if (Unit.Version >= 5)
143     IO.mapRequired("UnitType", Unit.Type);
144   IO.mapRequired("AbbrOffset", Unit.AbbrOffset);
145   IO.mapRequired("AddrSize", Unit.AddrSize);
146   IO.mapOptional("Entries", Unit.Entries);
147 }
148 
mapping(IO & IO,DWARFYAML::Entry & Entry)149 void MappingTraits<DWARFYAML::Entry>::mapping(IO &IO, DWARFYAML::Entry &Entry) {
150   IO.mapRequired("AbbrCode", Entry.AbbrCode);
151   IO.mapRequired("Values", Entry.Values);
152 }
153 
mapping(IO & IO,DWARFYAML::FormValue & FormValue)154 void MappingTraits<DWARFYAML::FormValue>::mapping(
155     IO &IO, DWARFYAML::FormValue &FormValue) {
156   IO.mapOptional("Value", FormValue.Value);
157   if (!FormValue.CStr.empty() || !IO.outputting())
158     IO.mapOptional("CStr", FormValue.CStr);
159   if (!FormValue.BlockData.empty() || !IO.outputting())
160     IO.mapOptional("BlockData", FormValue.BlockData);
161 }
162 
mapping(IO & IO,DWARFYAML::File & File)163 void MappingTraits<DWARFYAML::File>::mapping(IO &IO, DWARFYAML::File &File) {
164   IO.mapRequired("Name", File.Name);
165   IO.mapRequired("DirIdx", File.DirIdx);
166   IO.mapRequired("ModTime", File.ModTime);
167   IO.mapRequired("Length", File.Length);
168 }
169 
mapping(IO & IO,DWARFYAML::LineTableOpcode & LineTableOpcode)170 void MappingTraits<DWARFYAML::LineTableOpcode>::mapping(
171     IO &IO, DWARFYAML::LineTableOpcode &LineTableOpcode) {
172   IO.mapRequired("Opcode", LineTableOpcode.Opcode);
173   if (LineTableOpcode.Opcode == dwarf::DW_LNS_extended_op) {
174     IO.mapRequired("ExtLen", LineTableOpcode.ExtLen);
175     IO.mapRequired("SubOpcode", LineTableOpcode.SubOpcode);
176   }
177 
178   if (!LineTableOpcode.UnknownOpcodeData.empty() || !IO.outputting())
179     IO.mapOptional("UnknownOpcodeData", LineTableOpcode.UnknownOpcodeData);
180   if (!LineTableOpcode.UnknownOpcodeData.empty() || !IO.outputting())
181     IO.mapOptional("StandardOpcodeData", LineTableOpcode.StandardOpcodeData);
182   if (!LineTableOpcode.FileEntry.Name.empty() || !IO.outputting())
183     IO.mapOptional("FileEntry", LineTableOpcode.FileEntry);
184   if (LineTableOpcode.Opcode == dwarf::DW_LNS_advance_line || !IO.outputting())
185     IO.mapOptional("SData", LineTableOpcode.SData);
186   IO.mapOptional("Data", LineTableOpcode.Data);
187 }
188 
mapping(IO & IO,DWARFYAML::LineTable & LineTable)189 void MappingTraits<DWARFYAML::LineTable>::mapping(
190     IO &IO, DWARFYAML::LineTable &LineTable) {
191   IO.mapOptional("Format", LineTable.Format, dwarf::DWARF32);
192   IO.mapRequired("Length", LineTable.Length);
193   IO.mapRequired("Version", LineTable.Version);
194   IO.mapRequired("PrologueLength", LineTable.PrologueLength);
195   IO.mapRequired("MinInstLength", LineTable.MinInstLength);
196   if(LineTable.Version >= 4)
197     IO.mapRequired("MaxOpsPerInst", LineTable.MaxOpsPerInst);
198   IO.mapRequired("DefaultIsStmt", LineTable.DefaultIsStmt);
199   IO.mapRequired("LineBase", LineTable.LineBase);
200   IO.mapRequired("LineRange", LineTable.LineRange);
201   IO.mapRequired("OpcodeBase", LineTable.OpcodeBase);
202   IO.mapRequired("StandardOpcodeLengths", LineTable.StandardOpcodeLengths);
203   IO.mapRequired("IncludeDirs", LineTable.IncludeDirs);
204   IO.mapRequired("Files", LineTable.Files);
205   IO.mapRequired("Opcodes", LineTable.Opcodes);
206 }
207 
mapping(IO & IO,DWARFYAML::SegAddrPair & SegAddrPair)208 void MappingTraits<DWARFYAML::SegAddrPair>::mapping(
209     IO &IO, DWARFYAML::SegAddrPair &SegAddrPair) {
210   IO.mapOptional("Segment", SegAddrPair.Segment, 0);
211   IO.mapOptional("Address", SegAddrPair.Address, 0);
212 }
213 
mapping(IO & IO,DWARFYAML::AddrTableEntry & AddrTable)214 void MappingTraits<DWARFYAML::AddrTableEntry>::mapping(
215     IO &IO, DWARFYAML::AddrTableEntry &AddrTable) {
216   IO.mapOptional("Format", AddrTable.Format, dwarf::DWARF32);
217   IO.mapOptional("Length", AddrTable.Length);
218   IO.mapRequired("Version", AddrTable.Version);
219   IO.mapOptional("AddressSize", AddrTable.AddrSize);
220   IO.mapOptional("SegmentSelectorSize", AddrTable.SegSelectorSize, 0);
221   IO.mapOptional("Entries", AddrTable.SegAddrPairs);
222 }
223 
mapping(IO & IO,DWARFYAML::InitialLength & InitialLength)224 void MappingTraits<DWARFYAML::InitialLength>::mapping(
225     IO &IO, DWARFYAML::InitialLength &InitialLength) {
226   IO.mapRequired("TotalLength", InitialLength.TotalLength);
227   if (InitialLength.isDWARF64())
228     IO.mapRequired("TotalLength64", InitialLength.TotalLength64);
229 }
230 
231 } // end namespace yaml
232 
233 } // end namespace llvm
234