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