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