annotate tools/obj2yaml/dwarf2yaml.cpp @ 121:803732b1fca8

LLVM 5.0
author kono
date Fri, 27 Oct 2017 17:07:41 +0900
parents
children c2174574ed3a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
121
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1 //===------ dwarf2yaml.cpp - obj2yaml conversion tool -----------*- C++ -*-===//
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
2 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
3 // The LLVM Compiler Infrastructure
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
4 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
5 // This file is distributed under the University of Illinois Open Source
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
6 // License. See LICENSE.TXT for details.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
7 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
8 //===----------------------------------------------------------------------===//
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
9
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
10 #include "Error.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
11 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
12 #include "llvm/DebugInfo/DWARF/DWARFDebugArangeSet.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
13 #include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
14 #include "llvm/ObjectYAML/DWARFYAML.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
15
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
16 #include <algorithm>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
17
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
18 using namespace llvm;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
19
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
20 void dumpInitialLength(DataExtractor &Data, uint32_t &Offset,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
21 DWARFYAML::InitialLength &InitialLength) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
22 InitialLength.TotalLength = Data.getU32(&Offset);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
23 if (InitialLength.isDWARF64())
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
24 InitialLength.TotalLength64 = Data.getU64(&Offset);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
25 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
26
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
27 void dumpDebugAbbrev(DWARFContext &DCtx, DWARFYAML::Data &Y) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
28 auto AbbrevSetPtr = DCtx.getDebugAbbrev();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
29 if (AbbrevSetPtr) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
30 for (auto AbbrvDeclSet : *AbbrevSetPtr) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
31 for (auto AbbrvDecl : AbbrvDeclSet.second) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
32 DWARFYAML::Abbrev Abbrv;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
33 Abbrv.Code = AbbrvDecl.getCode();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
34 Abbrv.Tag = AbbrvDecl.getTag();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
35 Abbrv.Children = AbbrvDecl.hasChildren() ? dwarf::DW_CHILDREN_yes
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
36 : dwarf::DW_CHILDREN_no;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
37 for (auto Attribute : AbbrvDecl.attributes()) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
38 DWARFYAML::AttributeAbbrev AttAbrv;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
39 AttAbrv.Attribute = Attribute.Attr;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
40 AttAbrv.Form = Attribute.Form;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
41 if (AttAbrv.Form == dwarf::DW_FORM_implicit_const)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
42 AttAbrv.Value = Attribute.getImplicitConstValue();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
43 Abbrv.Attributes.push_back(AttAbrv);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
44 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
45 Y.AbbrevDecls.push_back(Abbrv);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
46 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
47 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
48 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
49 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
50
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
51 void dumpDebugStrings(DWARFContext &DCtx, DWARFYAML::Data &Y) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
52 StringRef RemainingTable = DCtx.getDWARFObj().getStringSection();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
53 while (RemainingTable.size() > 0) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
54 auto SymbolPair = RemainingTable.split('\0');
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
55 RemainingTable = SymbolPair.second;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
56 Y.DebugStrings.push_back(SymbolPair.first);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
57 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
58 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
59
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
60 void dumpDebugARanges(DWARFContext &DCtx, DWARFYAML::Data &Y) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
61 DataExtractor ArangesData(DCtx.getDWARFObj().getARangeSection(),
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
62 DCtx.isLittleEndian(), 0);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
63 uint32_t Offset = 0;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
64 DWARFDebugArangeSet Set;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
65
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
66 while (Set.extract(ArangesData, &Offset)) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
67 DWARFYAML::ARange Range;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
68 Range.Length.setLength(Set.getHeader().Length);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
69 Range.Version = Set.getHeader().Version;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
70 Range.CuOffset = Set.getHeader().CuOffset;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
71 Range.AddrSize = Set.getHeader().AddrSize;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
72 Range.SegSize = Set.getHeader().SegSize;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
73 for (auto Descriptor : Set.descriptors()) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
74 DWARFYAML::ARangeDescriptor Desc;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
75 Desc.Address = Descriptor.Address;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
76 Desc.Length = Descriptor.Length;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
77 Range.Descriptors.push_back(Desc);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
78 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
79 Y.ARanges.push_back(Range);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
80 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
81 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
82
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
83 void dumpPubSection(DWARFContext &DCtx, DWARFYAML::PubSection &Y,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
84 StringRef Section) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
85 DataExtractor PubSectionData(Section, DCtx.isLittleEndian(), 0);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
86 uint32_t Offset = 0;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
87 dumpInitialLength(PubSectionData, Offset, Y.Length);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
88 Y.Version = PubSectionData.getU16(&Offset);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
89 Y.UnitOffset = PubSectionData.getU32(&Offset);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
90 Y.UnitSize = PubSectionData.getU32(&Offset);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
91 while (Offset < Y.Length.getLength()) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
92 DWARFYAML::PubEntry NewEntry;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
93 NewEntry.DieOffset = PubSectionData.getU32(&Offset);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
94 if (Y.IsGNUStyle)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
95 NewEntry.Descriptor = PubSectionData.getU8(&Offset);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
96 NewEntry.Name = PubSectionData.getCStr(&Offset);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
97 Y.Entries.push_back(NewEntry);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
98 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
99 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
100
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
101 void dumpDebugPubSections(DWARFContext &DCtx, DWARFYAML::Data &Y) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
102 const DWARFObject &D = DCtx.getDWARFObj();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
103 Y.PubNames.IsGNUStyle = false;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
104 dumpPubSection(DCtx, Y.PubNames, D.getPubNamesSection());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
105
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
106 Y.PubTypes.IsGNUStyle = false;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
107 dumpPubSection(DCtx, Y.PubTypes, D.getPubTypesSection());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
108
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
109 Y.GNUPubNames.IsGNUStyle = true;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
110 dumpPubSection(DCtx, Y.GNUPubNames, D.getGnuPubNamesSection());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
111
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
112 Y.GNUPubTypes.IsGNUStyle = true;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
113 dumpPubSection(DCtx, Y.GNUPubTypes, D.getGnuPubTypesSection());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
114 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
115
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
116 void dumpDebugInfo(DWARFContext &DCtx, DWARFYAML::Data &Y) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
117 for (const auto &CU : DCtx.compile_units()) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
118 DWARFYAML::Unit NewUnit;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
119 NewUnit.Length.setLength(CU->getLength());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
120 NewUnit.Version = CU->getVersion();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
121 if(NewUnit.Version >= 5)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
122 NewUnit.Type = (dwarf::UnitType)CU->getUnitType();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
123 NewUnit.AbbrOffset = CU->getAbbreviations()->getOffset();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
124 NewUnit.AddrSize = CU->getAddressByteSize();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
125 for (auto DIE : CU->dies()) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
126 DWARFYAML::Entry NewEntry;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
127 DataExtractor EntryData = CU->getDebugInfoExtractor();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
128 uint32_t offset = DIE.getOffset();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
129
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
130 assert(EntryData.isValidOffset(offset) && "Invalid DIE Offset");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
131 if (!EntryData.isValidOffset(offset))
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
132 continue;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
133
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
134 NewEntry.AbbrCode = EntryData.getULEB128(&offset);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
135
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
136 auto AbbrevDecl = DIE.getAbbreviationDeclarationPtr();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
137 if (AbbrevDecl) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
138 for (const auto &AttrSpec : AbbrevDecl->attributes()) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
139 DWARFYAML::FormValue NewValue;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
140 NewValue.Value = 0xDEADBEEFDEADBEEF;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
141 DWARFDie DIEWrapper(CU.get(), &DIE);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
142 auto FormValue = DIEWrapper.find(AttrSpec.Attr);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
143 if (!FormValue)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
144 return;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
145 auto Form = FormValue.getValue().getForm();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
146 bool indirect = false;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
147 do {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
148 indirect = false;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
149 switch (Form) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
150 case dwarf::DW_FORM_addr:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
151 case dwarf::DW_FORM_GNU_addr_index:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
152 if (auto Val = FormValue.getValue().getAsAddress())
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
153 NewValue.Value = Val.getValue();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
154 break;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
155 case dwarf::DW_FORM_ref_addr:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
156 case dwarf::DW_FORM_ref1:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
157 case dwarf::DW_FORM_ref2:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
158 case dwarf::DW_FORM_ref4:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
159 case dwarf::DW_FORM_ref8:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
160 case dwarf::DW_FORM_ref_udata:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
161 case dwarf::DW_FORM_ref_sig8:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
162 if (auto Val = FormValue.getValue().getAsReferenceUVal())
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
163 NewValue.Value = Val.getValue();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
164 break;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
165 case dwarf::DW_FORM_exprloc:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
166 case dwarf::DW_FORM_block:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
167 case dwarf::DW_FORM_block1:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
168 case dwarf::DW_FORM_block2:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
169 case dwarf::DW_FORM_block4:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
170 if (auto Val = FormValue.getValue().getAsBlock()) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
171 auto BlockData = Val.getValue();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
172 std::copy(BlockData.begin(), BlockData.end(),
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
173 std::back_inserter(NewValue.BlockData));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
174 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
175 NewValue.Value = NewValue.BlockData.size();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
176 break;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
177 case dwarf::DW_FORM_data1:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
178 case dwarf::DW_FORM_flag:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
179 case dwarf::DW_FORM_data2:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
180 case dwarf::DW_FORM_data4:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
181 case dwarf::DW_FORM_data8:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
182 case dwarf::DW_FORM_sdata:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
183 case dwarf::DW_FORM_udata:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
184 case dwarf::DW_FORM_ref_sup4:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
185 case dwarf::DW_FORM_ref_sup8:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
186 if (auto Val = FormValue.getValue().getAsUnsignedConstant())
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
187 NewValue.Value = Val.getValue();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
188 break;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
189 case dwarf::DW_FORM_string:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
190 if (auto Val = FormValue.getValue().getAsCString())
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
191 NewValue.CStr = Val.getValue();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
192 break;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
193 case dwarf::DW_FORM_indirect:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
194 indirect = true;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
195 if (auto Val = FormValue.getValue().getAsUnsignedConstant()) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
196 NewValue.Value = Val.getValue();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
197 NewEntry.Values.push_back(NewValue);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
198 Form = static_cast<dwarf::Form>(Val.getValue());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
199 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
200 break;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
201 case dwarf::DW_FORM_strp:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
202 case dwarf::DW_FORM_sec_offset:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
203 case dwarf::DW_FORM_GNU_ref_alt:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
204 case dwarf::DW_FORM_GNU_strp_alt:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
205 case dwarf::DW_FORM_line_strp:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
206 case dwarf::DW_FORM_strp_sup:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
207 case dwarf::DW_FORM_GNU_str_index:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
208 case dwarf::DW_FORM_strx:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
209 if (auto Val = FormValue.getValue().getAsCStringOffset())
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
210 NewValue.Value = Val.getValue();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
211 break;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
212 case dwarf::DW_FORM_flag_present:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
213 NewValue.Value = 1;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
214 break;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
215 default:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
216 break;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
217 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
218 } while (indirect);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
219 NewEntry.Values.push_back(NewValue);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
220 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
221 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
222
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
223 NewUnit.Entries.push_back(NewEntry);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
224 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
225 Y.CompileUnits.push_back(NewUnit);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
226 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
227 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
228
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
229 bool dumpFileEntry(DataExtractor &Data, uint32_t &Offset,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
230 DWARFYAML::File &File) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
231 File.Name = Data.getCStr(&Offset);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
232 if (File.Name.empty())
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
233 return false;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
234 File.DirIdx = Data.getULEB128(&Offset);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
235 File.ModTime = Data.getULEB128(&Offset);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
236 File.Length = Data.getULEB128(&Offset);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
237 return true;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
238 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
239
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
240 void dumpDebugLines(DWARFContext &DCtx, DWARFYAML::Data &Y) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
241 for (const auto &CU : DCtx.compile_units()) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
242 auto CUDIE = CU->getUnitDIE();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
243 if (!CUDIE)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
244 continue;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
245 if (auto StmtOffset =
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
246 dwarf::toSectionOffset(CUDIE.find(dwarf::DW_AT_stmt_list))) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
247 DWARFYAML::LineTable DebugLines;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
248 DataExtractor LineData(DCtx.getDWARFObj().getLineSection().Data,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
249 DCtx.isLittleEndian(), CU->getAddressByteSize());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
250 uint32_t Offset = *StmtOffset;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
251 dumpInitialLength(LineData, Offset, DebugLines.Length);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
252 uint64_t LineTableLength = DebugLines.Length.getLength();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
253 uint64_t SizeOfPrologueLength = DebugLines.Length.isDWARF64() ? 8 : 4;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
254 DebugLines.Version = LineData.getU16(&Offset);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
255 DebugLines.PrologueLength =
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
256 LineData.getUnsigned(&Offset, SizeOfPrologueLength);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
257 const uint64_t EndPrologue = DebugLines.PrologueLength + Offset;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
258
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
259 DebugLines.MinInstLength = LineData.getU8(&Offset);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
260 if (DebugLines.Version >= 4)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
261 DebugLines.MaxOpsPerInst = LineData.getU8(&Offset);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
262 DebugLines.DefaultIsStmt = LineData.getU8(&Offset);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
263 DebugLines.LineBase = LineData.getU8(&Offset);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
264 DebugLines.LineRange = LineData.getU8(&Offset);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
265 DebugLines.OpcodeBase = LineData.getU8(&Offset);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
266
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
267 DebugLines.StandardOpcodeLengths.reserve(DebugLines.OpcodeBase - 1);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
268 for (uint8_t i = 1; i < DebugLines.OpcodeBase; ++i)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
269 DebugLines.StandardOpcodeLengths.push_back(LineData.getU8(&Offset));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
270
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
271 while (Offset < EndPrologue) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
272 StringRef Dir = LineData.getCStr(&Offset);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
273 if (!Dir.empty())
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
274 DebugLines.IncludeDirs.push_back(Dir);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
275 else
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
276 break;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
277 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
278
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
279 while (Offset < EndPrologue) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
280 DWARFYAML::File TmpFile;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
281 if (dumpFileEntry(LineData, Offset, TmpFile))
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
282 DebugLines.Files.push_back(TmpFile);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
283 else
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
284 break;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
285 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
286
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
287 const uint64_t LineEnd =
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
288 LineTableLength + *StmtOffset + SizeOfPrologueLength;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
289 while (Offset < LineEnd) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
290 DWARFYAML::LineTableOpcode NewOp;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
291 NewOp.Opcode = (dwarf::LineNumberOps)LineData.getU8(&Offset);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
292 if (NewOp.Opcode == 0) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
293 auto StartExt = Offset;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
294 NewOp.ExtLen = LineData.getULEB128(&Offset);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
295 NewOp.SubOpcode =
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
296 (dwarf::LineNumberExtendedOps)LineData.getU8(&Offset);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
297 switch (NewOp.SubOpcode) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
298 case dwarf::DW_LNE_set_address:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
299 case dwarf::DW_LNE_set_discriminator:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
300 NewOp.Data = LineData.getAddress(&Offset);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
301 break;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
302 case dwarf::DW_LNE_define_file:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
303 dumpFileEntry(LineData, Offset, NewOp.FileEntry);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
304 break;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
305 case dwarf::DW_LNE_end_sequence:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
306 break;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
307 default:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
308 while (Offset < StartExt + NewOp.ExtLen)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
309 NewOp.UnknownOpcodeData.push_back(LineData.getU8(&Offset));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
310 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
311 } else if (NewOp.Opcode < DebugLines.OpcodeBase) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
312 switch (NewOp.Opcode) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
313 case dwarf::DW_LNS_copy:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
314 case dwarf::DW_LNS_negate_stmt:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
315 case dwarf::DW_LNS_set_basic_block:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
316 case dwarf::DW_LNS_const_add_pc:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
317 case dwarf::DW_LNS_set_prologue_end:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
318 case dwarf::DW_LNS_set_epilogue_begin:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
319 break;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
320
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
321 case dwarf::DW_LNS_advance_pc:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
322 case dwarf::DW_LNS_set_file:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
323 case dwarf::DW_LNS_set_column:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
324 case dwarf::DW_LNS_set_isa:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
325 NewOp.Data = LineData.getULEB128(&Offset);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
326 break;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
327
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
328 case dwarf::DW_LNS_advance_line:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
329 NewOp.SData = LineData.getSLEB128(&Offset);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
330 break;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
331
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
332 case dwarf::DW_LNS_fixed_advance_pc:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
333 NewOp.Data = LineData.getU16(&Offset);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
334 break;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
335
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
336 default:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
337 for (uint8_t i = 0;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
338 i < DebugLines.StandardOpcodeLengths[NewOp.Opcode - 1]; ++i)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
339 NewOp.StandardOpcodeData.push_back(LineData.getULEB128(&Offset));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
340 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
341 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
342 DebugLines.Opcodes.push_back(NewOp);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
343 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
344 Y.DebugLines.push_back(DebugLines);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
345 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
346 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
347 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
348
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
349 std::error_code dwarf2yaml(DWARFContext &DCtx, DWARFYAML::Data &Y) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
350 dumpDebugAbbrev(DCtx, Y);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
351 dumpDebugStrings(DCtx, Y);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
352 dumpDebugARanges(DCtx, Y);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
353 dumpDebugPubSections(DCtx, Y);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
354 dumpDebugInfo(DCtx, Y);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
355 dumpDebugLines(DCtx, Y);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
356 return obj2yaml_error::success;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
357 }