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