1f4a2713aSLionel Sambuc //===-- DWARFContext.cpp --------------------------------------------------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc
10*0a6a1f1dSLionel Sambuc #include "llvm/DebugInfo/DWARFContext.h"
11f4a2713aSLionel Sambuc #include "llvm/ADT/SmallString.h"
12f4a2713aSLionel Sambuc #include "llvm/ADT/StringSwitch.h"
13*0a6a1f1dSLionel Sambuc #include "llvm/DebugInfo/DWARFAcceleratorTable.h"
14*0a6a1f1dSLionel Sambuc #include "llvm/DebugInfo/DWARFDebugArangeSet.h"
15f4a2713aSLionel Sambuc #include "llvm/Support/Compression.h"
16f4a2713aSLionel Sambuc #include "llvm/Support/Dwarf.h"
17f4a2713aSLionel Sambuc #include "llvm/Support/Format.h"
18f4a2713aSLionel Sambuc #include "llvm/Support/Path.h"
19f4a2713aSLionel Sambuc #include "llvm/Support/raw_ostream.h"
20f4a2713aSLionel Sambuc #include <algorithm>
21f4a2713aSLionel Sambuc using namespace llvm;
22f4a2713aSLionel Sambuc using namespace dwarf;
23f4a2713aSLionel Sambuc using namespace object;
24f4a2713aSLionel Sambuc
25*0a6a1f1dSLionel Sambuc #define DEBUG_TYPE "dwarf"
26f4a2713aSLionel Sambuc
27*0a6a1f1dSLionel Sambuc typedef DWARFDebugLine::LineTable DWARFLineTable;
28*0a6a1f1dSLionel Sambuc typedef DILineInfoSpecifier::FileLineInfoKind FileLineInfoKind;
29*0a6a1f1dSLionel Sambuc typedef DILineInfoSpecifier::FunctionNameKind FunctionNameKind;
30f4a2713aSLionel Sambuc
dumpPubSection(raw_ostream & OS,StringRef Name,StringRef Data,bool LittleEndian,bool GnuStyle)31f4a2713aSLionel Sambuc static void dumpPubSection(raw_ostream &OS, StringRef Name, StringRef Data,
32f4a2713aSLionel Sambuc bool LittleEndian, bool GnuStyle) {
33f4a2713aSLionel Sambuc OS << "\n." << Name << " contents:\n";
34f4a2713aSLionel Sambuc DataExtractor pubNames(Data, LittleEndian, 0);
35f4a2713aSLionel Sambuc uint32_t offset = 0;
36*0a6a1f1dSLionel Sambuc while (pubNames.isValidOffset(offset)) {
37f4a2713aSLionel Sambuc OS << "length = " << format("0x%08x", pubNames.getU32(&offset));
38f4a2713aSLionel Sambuc OS << " version = " << format("0x%04x", pubNames.getU16(&offset));
39f4a2713aSLionel Sambuc OS << " unit_offset = " << format("0x%08x", pubNames.getU32(&offset));
40f4a2713aSLionel Sambuc OS << " unit_size = " << format("0x%08x", pubNames.getU32(&offset)) << '\n';
41f4a2713aSLionel Sambuc if (GnuStyle)
42f4a2713aSLionel Sambuc OS << "Offset Linkage Kind Name\n";
43f4a2713aSLionel Sambuc else
44f4a2713aSLionel Sambuc OS << "Offset Name\n";
45f4a2713aSLionel Sambuc
46f4a2713aSLionel Sambuc while (offset < Data.size()) {
47f4a2713aSLionel Sambuc uint32_t dieRef = pubNames.getU32(&offset);
48f4a2713aSLionel Sambuc if (dieRef == 0)
49f4a2713aSLionel Sambuc break;
50f4a2713aSLionel Sambuc OS << format("0x%8.8x ", dieRef);
51f4a2713aSLionel Sambuc if (GnuStyle) {
52f4a2713aSLionel Sambuc PubIndexEntryDescriptor desc(pubNames.getU8(&offset));
53f4a2713aSLionel Sambuc OS << format("%-8s", dwarf::GDBIndexEntryLinkageString(desc.Linkage))
54f4a2713aSLionel Sambuc << ' ' << format("%-8s", dwarf::GDBIndexEntryKindString(desc.Kind))
55f4a2713aSLionel Sambuc << ' ';
56f4a2713aSLionel Sambuc }
57f4a2713aSLionel Sambuc OS << '\"' << pubNames.getCStr(&offset) << "\"\n";
58f4a2713aSLionel Sambuc }
59f4a2713aSLionel Sambuc }
60*0a6a1f1dSLionel Sambuc }
61*0a6a1f1dSLionel Sambuc
dumpAccelSection(raw_ostream & OS,StringRef Name,const DWARFSection & Section,StringRef StringSection,bool LittleEndian)62*0a6a1f1dSLionel Sambuc static void dumpAccelSection(raw_ostream &OS, StringRef Name,
63*0a6a1f1dSLionel Sambuc const DWARFSection& Section, StringRef StringSection,
64*0a6a1f1dSLionel Sambuc bool LittleEndian) {
65*0a6a1f1dSLionel Sambuc DataExtractor AccelSection(Section.Data, LittleEndian, 0);
66*0a6a1f1dSLionel Sambuc DataExtractor StrData(StringSection, LittleEndian, 0);
67*0a6a1f1dSLionel Sambuc OS << "\n." << Name << " contents:\n";
68*0a6a1f1dSLionel Sambuc DWARFAcceleratorTable Accel(AccelSection, StrData, Section.Relocs);
69*0a6a1f1dSLionel Sambuc if (!Accel.extract())
70*0a6a1f1dSLionel Sambuc return;
71*0a6a1f1dSLionel Sambuc Accel.dump(OS);
72*0a6a1f1dSLionel Sambuc }
73f4a2713aSLionel Sambuc
dump(raw_ostream & OS,DIDumpType DumpType)74f4a2713aSLionel Sambuc void DWARFContext::dump(raw_ostream &OS, DIDumpType DumpType) {
75f4a2713aSLionel Sambuc if (DumpType == DIDT_All || DumpType == DIDT_Abbrev) {
76f4a2713aSLionel Sambuc OS << ".debug_abbrev contents:\n";
77f4a2713aSLionel Sambuc getDebugAbbrev()->dump(OS);
78f4a2713aSLionel Sambuc }
79f4a2713aSLionel Sambuc
80*0a6a1f1dSLionel Sambuc if (DumpType == DIDT_All || DumpType == DIDT_AbbrevDwo)
81*0a6a1f1dSLionel Sambuc if (const DWARFDebugAbbrev *D = getDebugAbbrevDWO()) {
82*0a6a1f1dSLionel Sambuc OS << "\n.debug_abbrev.dwo contents:\n";
83*0a6a1f1dSLionel Sambuc D->dump(OS);
84f4a2713aSLionel Sambuc }
85f4a2713aSLionel Sambuc
86*0a6a1f1dSLionel Sambuc if (DumpType == DIDT_All || DumpType == DIDT_Info) {
87*0a6a1f1dSLionel Sambuc OS << "\n.debug_info contents:\n";
88*0a6a1f1dSLionel Sambuc for (const auto &CU : compile_units())
89*0a6a1f1dSLionel Sambuc CU->dump(OS);
90*0a6a1f1dSLionel Sambuc }
91*0a6a1f1dSLionel Sambuc
92*0a6a1f1dSLionel Sambuc if ((DumpType == DIDT_All || DumpType == DIDT_InfoDwo) &&
93*0a6a1f1dSLionel Sambuc getNumDWOCompileUnits()) {
94*0a6a1f1dSLionel Sambuc OS << "\n.debug_info.dwo contents:\n";
95*0a6a1f1dSLionel Sambuc for (const auto &DWOCU : dwo_compile_units())
96*0a6a1f1dSLionel Sambuc DWOCU->dump(OS);
97*0a6a1f1dSLionel Sambuc }
98*0a6a1f1dSLionel Sambuc
99*0a6a1f1dSLionel Sambuc if ((DumpType == DIDT_All || DumpType == DIDT_Types) && getNumTypeUnits()) {
100f4a2713aSLionel Sambuc OS << "\n.debug_types contents:\n";
101*0a6a1f1dSLionel Sambuc for (const auto &TUS : type_unit_sections())
102*0a6a1f1dSLionel Sambuc for (const auto &TU : TUS)
103*0a6a1f1dSLionel Sambuc TU->dump(OS);
104*0a6a1f1dSLionel Sambuc }
105*0a6a1f1dSLionel Sambuc
106*0a6a1f1dSLionel Sambuc if ((DumpType == DIDT_All || DumpType == DIDT_TypesDwo) &&
107*0a6a1f1dSLionel Sambuc getNumDWOTypeUnits()) {
108*0a6a1f1dSLionel Sambuc OS << "\n.debug_types.dwo contents:\n";
109*0a6a1f1dSLionel Sambuc for (const auto &DWOTUS : dwo_type_unit_sections())
110*0a6a1f1dSLionel Sambuc for (const auto &DWOTU : DWOTUS)
111*0a6a1f1dSLionel Sambuc DWOTU->dump(OS);
112f4a2713aSLionel Sambuc }
113f4a2713aSLionel Sambuc
114f4a2713aSLionel Sambuc if (DumpType == DIDT_All || DumpType == DIDT_Loc) {
115f4a2713aSLionel Sambuc OS << "\n.debug_loc contents:\n";
116f4a2713aSLionel Sambuc getDebugLoc()->dump(OS);
117f4a2713aSLionel Sambuc }
118f4a2713aSLionel Sambuc
119*0a6a1f1dSLionel Sambuc if (DumpType == DIDT_All || DumpType == DIDT_LocDwo) {
120*0a6a1f1dSLionel Sambuc OS << "\n.debug_loc.dwo contents:\n";
121*0a6a1f1dSLionel Sambuc getDebugLocDWO()->dump(OS);
122*0a6a1f1dSLionel Sambuc }
123*0a6a1f1dSLionel Sambuc
124f4a2713aSLionel Sambuc if (DumpType == DIDT_All || DumpType == DIDT_Frames) {
125f4a2713aSLionel Sambuc OS << "\n.debug_frame contents:\n";
126f4a2713aSLionel Sambuc getDebugFrame()->dump(OS);
127f4a2713aSLionel Sambuc }
128f4a2713aSLionel Sambuc
129f4a2713aSLionel Sambuc uint32_t offset = 0;
130f4a2713aSLionel Sambuc if (DumpType == DIDT_All || DumpType == DIDT_Aranges) {
131f4a2713aSLionel Sambuc OS << "\n.debug_aranges contents:\n";
132f4a2713aSLionel Sambuc DataExtractor arangesData(getARangeSection(), isLittleEndian(), 0);
133f4a2713aSLionel Sambuc DWARFDebugArangeSet set;
134f4a2713aSLionel Sambuc while (set.extract(arangesData, &offset))
135f4a2713aSLionel Sambuc set.dump(OS);
136f4a2713aSLionel Sambuc }
137f4a2713aSLionel Sambuc
138f4a2713aSLionel Sambuc uint8_t savedAddressByteSize = 0;
139f4a2713aSLionel Sambuc if (DumpType == DIDT_All || DumpType == DIDT_Line) {
140f4a2713aSLionel Sambuc OS << "\n.debug_line contents:\n";
141*0a6a1f1dSLionel Sambuc for (const auto &CU : compile_units()) {
142*0a6a1f1dSLionel Sambuc savedAddressByteSize = CU->getAddressByteSize();
143f4a2713aSLionel Sambuc unsigned stmtOffset =
144*0a6a1f1dSLionel Sambuc CU->getCompileUnitDIE()->getAttributeValueAsSectionOffset(
145*0a6a1f1dSLionel Sambuc CU.get(), DW_AT_stmt_list, -1U);
146f4a2713aSLionel Sambuc if (stmtOffset != -1U) {
147f4a2713aSLionel Sambuc DataExtractor lineData(getLineSection().Data, isLittleEndian(),
148f4a2713aSLionel Sambuc savedAddressByteSize);
149*0a6a1f1dSLionel Sambuc DWARFDebugLine::LineTable LineTable;
150*0a6a1f1dSLionel Sambuc LineTable.parse(lineData, &getLineSection().Relocs, &stmtOffset);
151*0a6a1f1dSLionel Sambuc LineTable.dump(OS);
152f4a2713aSLionel Sambuc }
153f4a2713aSLionel Sambuc }
154f4a2713aSLionel Sambuc }
155f4a2713aSLionel Sambuc
156*0a6a1f1dSLionel Sambuc if (DumpType == DIDT_All || DumpType == DIDT_LineDwo) {
157*0a6a1f1dSLionel Sambuc OS << "\n.debug_line.dwo contents:\n";
158*0a6a1f1dSLionel Sambuc unsigned stmtOffset = 0;
159*0a6a1f1dSLionel Sambuc DataExtractor lineData(getLineDWOSection().Data, isLittleEndian(),
160*0a6a1f1dSLionel Sambuc savedAddressByteSize);
161*0a6a1f1dSLionel Sambuc DWARFDebugLine::LineTable LineTable;
162*0a6a1f1dSLionel Sambuc while (LineTable.Prologue.parse(lineData, &stmtOffset)) {
163*0a6a1f1dSLionel Sambuc LineTable.dump(OS);
164*0a6a1f1dSLionel Sambuc LineTable.clear();
165*0a6a1f1dSLionel Sambuc }
166*0a6a1f1dSLionel Sambuc }
167*0a6a1f1dSLionel Sambuc
168f4a2713aSLionel Sambuc if (DumpType == DIDT_All || DumpType == DIDT_Str) {
169f4a2713aSLionel Sambuc OS << "\n.debug_str contents:\n";
170f4a2713aSLionel Sambuc DataExtractor strData(getStringSection(), isLittleEndian(), 0);
171f4a2713aSLionel Sambuc offset = 0;
172f4a2713aSLionel Sambuc uint32_t strOffset = 0;
173f4a2713aSLionel Sambuc while (const char *s = strData.getCStr(&offset)) {
174f4a2713aSLionel Sambuc OS << format("0x%8.8x: \"%s\"\n", strOffset, s);
175f4a2713aSLionel Sambuc strOffset = offset;
176f4a2713aSLionel Sambuc }
177f4a2713aSLionel Sambuc }
178f4a2713aSLionel Sambuc
179*0a6a1f1dSLionel Sambuc if ((DumpType == DIDT_All || DumpType == DIDT_StrDwo) &&
180*0a6a1f1dSLionel Sambuc !getStringDWOSection().empty()) {
181*0a6a1f1dSLionel Sambuc OS << "\n.debug_str.dwo contents:\n";
182*0a6a1f1dSLionel Sambuc DataExtractor strDWOData(getStringDWOSection(), isLittleEndian(), 0);
183*0a6a1f1dSLionel Sambuc offset = 0;
184*0a6a1f1dSLionel Sambuc uint32_t strDWOOffset = 0;
185*0a6a1f1dSLionel Sambuc while (const char *s = strDWOData.getCStr(&offset)) {
186*0a6a1f1dSLionel Sambuc OS << format("0x%8.8x: \"%s\"\n", strDWOOffset, s);
187*0a6a1f1dSLionel Sambuc strDWOOffset = offset;
188*0a6a1f1dSLionel Sambuc }
189*0a6a1f1dSLionel Sambuc }
190*0a6a1f1dSLionel Sambuc
191f4a2713aSLionel Sambuc if (DumpType == DIDT_All || DumpType == DIDT_Ranges) {
192f4a2713aSLionel Sambuc OS << "\n.debug_ranges contents:\n";
193f4a2713aSLionel Sambuc // In fact, different compile units may have different address byte
194f4a2713aSLionel Sambuc // sizes, but for simplicity we just use the address byte size of the last
195f4a2713aSLionel Sambuc // compile unit (there is no easy and fast way to associate address range
196f4a2713aSLionel Sambuc // list and the compile unit it describes).
197f4a2713aSLionel Sambuc DataExtractor rangesData(getRangeSection(), isLittleEndian(),
198f4a2713aSLionel Sambuc savedAddressByteSize);
199f4a2713aSLionel Sambuc offset = 0;
200f4a2713aSLionel Sambuc DWARFDebugRangeList rangeList;
201f4a2713aSLionel Sambuc while (rangeList.extract(rangesData, &offset))
202f4a2713aSLionel Sambuc rangeList.dump(OS);
203f4a2713aSLionel Sambuc }
204f4a2713aSLionel Sambuc
205f4a2713aSLionel Sambuc if (DumpType == DIDT_All || DumpType == DIDT_Pubnames)
206f4a2713aSLionel Sambuc dumpPubSection(OS, "debug_pubnames", getPubNamesSection(),
207f4a2713aSLionel Sambuc isLittleEndian(), false);
208f4a2713aSLionel Sambuc
209f4a2713aSLionel Sambuc if (DumpType == DIDT_All || DumpType == DIDT_Pubtypes)
210f4a2713aSLionel Sambuc dumpPubSection(OS, "debug_pubtypes", getPubTypesSection(),
211f4a2713aSLionel Sambuc isLittleEndian(), false);
212f4a2713aSLionel Sambuc
213f4a2713aSLionel Sambuc if (DumpType == DIDT_All || DumpType == DIDT_GnuPubnames)
214f4a2713aSLionel Sambuc dumpPubSection(OS, "debug_gnu_pubnames", getGnuPubNamesSection(),
215f4a2713aSLionel Sambuc isLittleEndian(), true /* GnuStyle */);
216f4a2713aSLionel Sambuc
217f4a2713aSLionel Sambuc if (DumpType == DIDT_All || DumpType == DIDT_GnuPubtypes)
218f4a2713aSLionel Sambuc dumpPubSection(OS, "debug_gnu_pubtypes", getGnuPubTypesSection(),
219f4a2713aSLionel Sambuc isLittleEndian(), true /* GnuStyle */);
220f4a2713aSLionel Sambuc
221*0a6a1f1dSLionel Sambuc if ((DumpType == DIDT_All || DumpType == DIDT_StrOffsetsDwo) &&
222*0a6a1f1dSLionel Sambuc !getStringOffsetDWOSection().empty()) {
223f4a2713aSLionel Sambuc OS << "\n.debug_str_offsets.dwo contents:\n";
224*0a6a1f1dSLionel Sambuc DataExtractor strOffsetExt(getStringOffsetDWOSection(), isLittleEndian(),
225*0a6a1f1dSLionel Sambuc 0);
226f4a2713aSLionel Sambuc offset = 0;
227f4a2713aSLionel Sambuc uint64_t size = getStringOffsetDWOSection().size();
228f4a2713aSLionel Sambuc while (offset < size) {
229f4a2713aSLionel Sambuc OS << format("0x%8.8x: ", offset);
230f4a2713aSLionel Sambuc OS << format("%8.8x\n", strOffsetExt.getU32(&offset));
231f4a2713aSLionel Sambuc }
232f4a2713aSLionel Sambuc }
233*0a6a1f1dSLionel Sambuc
234*0a6a1f1dSLionel Sambuc if (DumpType == DIDT_All || DumpType == DIDT_AppleNames)
235*0a6a1f1dSLionel Sambuc dumpAccelSection(OS, "apple_names", getAppleNamesSection(),
236*0a6a1f1dSLionel Sambuc getStringSection(), isLittleEndian());
237*0a6a1f1dSLionel Sambuc
238*0a6a1f1dSLionel Sambuc if (DumpType == DIDT_All || DumpType == DIDT_AppleTypes)
239*0a6a1f1dSLionel Sambuc dumpAccelSection(OS, "apple_types", getAppleTypesSection(),
240*0a6a1f1dSLionel Sambuc getStringSection(), isLittleEndian());
241*0a6a1f1dSLionel Sambuc
242*0a6a1f1dSLionel Sambuc if (DumpType == DIDT_All || DumpType == DIDT_AppleNamespaces)
243*0a6a1f1dSLionel Sambuc dumpAccelSection(OS, "apple_namespaces", getAppleNamespacesSection(),
244*0a6a1f1dSLionel Sambuc getStringSection(), isLittleEndian());
245*0a6a1f1dSLionel Sambuc
246*0a6a1f1dSLionel Sambuc if (DumpType == DIDT_All || DumpType == DIDT_AppleObjC)
247*0a6a1f1dSLionel Sambuc dumpAccelSection(OS, "apple_objc", getAppleObjCSection(),
248*0a6a1f1dSLionel Sambuc getStringSection(), isLittleEndian());
249f4a2713aSLionel Sambuc }
250f4a2713aSLionel Sambuc
getDebugAbbrev()251f4a2713aSLionel Sambuc const DWARFDebugAbbrev *DWARFContext::getDebugAbbrev() {
252f4a2713aSLionel Sambuc if (Abbrev)
253f4a2713aSLionel Sambuc return Abbrev.get();
254f4a2713aSLionel Sambuc
255f4a2713aSLionel Sambuc DataExtractor abbrData(getAbbrevSection(), isLittleEndian(), 0);
256f4a2713aSLionel Sambuc
257f4a2713aSLionel Sambuc Abbrev.reset(new DWARFDebugAbbrev());
258*0a6a1f1dSLionel Sambuc Abbrev->extract(abbrData);
259f4a2713aSLionel Sambuc return Abbrev.get();
260f4a2713aSLionel Sambuc }
261f4a2713aSLionel Sambuc
getDebugAbbrevDWO()262f4a2713aSLionel Sambuc const DWARFDebugAbbrev *DWARFContext::getDebugAbbrevDWO() {
263f4a2713aSLionel Sambuc if (AbbrevDWO)
264f4a2713aSLionel Sambuc return AbbrevDWO.get();
265f4a2713aSLionel Sambuc
266f4a2713aSLionel Sambuc DataExtractor abbrData(getAbbrevDWOSection(), isLittleEndian(), 0);
267f4a2713aSLionel Sambuc AbbrevDWO.reset(new DWARFDebugAbbrev());
268*0a6a1f1dSLionel Sambuc AbbrevDWO->extract(abbrData);
269f4a2713aSLionel Sambuc return AbbrevDWO.get();
270f4a2713aSLionel Sambuc }
271f4a2713aSLionel Sambuc
getDebugLoc()272f4a2713aSLionel Sambuc const DWARFDebugLoc *DWARFContext::getDebugLoc() {
273f4a2713aSLionel Sambuc if (Loc)
274f4a2713aSLionel Sambuc return Loc.get();
275f4a2713aSLionel Sambuc
276f4a2713aSLionel Sambuc DataExtractor LocData(getLocSection().Data, isLittleEndian(), 0);
277f4a2713aSLionel Sambuc Loc.reset(new DWARFDebugLoc(getLocSection().Relocs));
278f4a2713aSLionel Sambuc // assume all compile units have the same address byte size
279f4a2713aSLionel Sambuc if (getNumCompileUnits())
280f4a2713aSLionel Sambuc Loc->parse(LocData, getCompileUnitAtIndex(0)->getAddressByteSize());
281f4a2713aSLionel Sambuc return Loc.get();
282f4a2713aSLionel Sambuc }
283f4a2713aSLionel Sambuc
getDebugLocDWO()284*0a6a1f1dSLionel Sambuc const DWARFDebugLocDWO *DWARFContext::getDebugLocDWO() {
285*0a6a1f1dSLionel Sambuc if (LocDWO)
286*0a6a1f1dSLionel Sambuc return LocDWO.get();
287*0a6a1f1dSLionel Sambuc
288*0a6a1f1dSLionel Sambuc DataExtractor LocData(getLocDWOSection().Data, isLittleEndian(), 0);
289*0a6a1f1dSLionel Sambuc LocDWO.reset(new DWARFDebugLocDWO());
290*0a6a1f1dSLionel Sambuc LocDWO->parse(LocData);
291*0a6a1f1dSLionel Sambuc return LocDWO.get();
292*0a6a1f1dSLionel Sambuc }
293*0a6a1f1dSLionel Sambuc
getDebugAranges()294f4a2713aSLionel Sambuc const DWARFDebugAranges *DWARFContext::getDebugAranges() {
295f4a2713aSLionel Sambuc if (Aranges)
296f4a2713aSLionel Sambuc return Aranges.get();
297f4a2713aSLionel Sambuc
298f4a2713aSLionel Sambuc Aranges.reset(new DWARFDebugAranges());
299f4a2713aSLionel Sambuc Aranges->generate(this);
300f4a2713aSLionel Sambuc return Aranges.get();
301f4a2713aSLionel Sambuc }
302f4a2713aSLionel Sambuc
getDebugFrame()303f4a2713aSLionel Sambuc const DWARFDebugFrame *DWARFContext::getDebugFrame() {
304f4a2713aSLionel Sambuc if (DebugFrame)
305f4a2713aSLionel Sambuc return DebugFrame.get();
306f4a2713aSLionel Sambuc
307f4a2713aSLionel Sambuc // There's a "bug" in the DWARFv3 standard with respect to the target address
308f4a2713aSLionel Sambuc // size within debug frame sections. While DWARF is supposed to be independent
309f4a2713aSLionel Sambuc // of its container, FDEs have fields with size being "target address size",
310f4a2713aSLionel Sambuc // which isn't specified in DWARF in general. It's only specified for CUs, but
311f4a2713aSLionel Sambuc // .eh_frame can appear without a .debug_info section. Follow the example of
312f4a2713aSLionel Sambuc // other tools (libdwarf) and extract this from the container (ObjectFile
313f4a2713aSLionel Sambuc // provides this information). This problem is fixed in DWARFv4
314f4a2713aSLionel Sambuc // See this dwarf-discuss discussion for more details:
315f4a2713aSLionel Sambuc // http://lists.dwarfstd.org/htdig.cgi/dwarf-discuss-dwarfstd.org/2011-December/001173.html
316f4a2713aSLionel Sambuc DataExtractor debugFrameData(getDebugFrameSection(), isLittleEndian(),
317f4a2713aSLionel Sambuc getAddressSize());
318f4a2713aSLionel Sambuc DebugFrame.reset(new DWARFDebugFrame());
319f4a2713aSLionel Sambuc DebugFrame->parse(debugFrameData);
320f4a2713aSLionel Sambuc return DebugFrame.get();
321f4a2713aSLionel Sambuc }
322f4a2713aSLionel Sambuc
323f4a2713aSLionel Sambuc const DWARFLineTable *
getLineTableForUnit(DWARFUnit * cu)324*0a6a1f1dSLionel Sambuc DWARFContext::getLineTableForUnit(DWARFUnit *cu) {
325f4a2713aSLionel Sambuc if (!Line)
326f4a2713aSLionel Sambuc Line.reset(new DWARFDebugLine(&getLineSection().Relocs));
327f4a2713aSLionel Sambuc
328f4a2713aSLionel Sambuc unsigned stmtOffset =
329f4a2713aSLionel Sambuc cu->getCompileUnitDIE()->getAttributeValueAsSectionOffset(
330f4a2713aSLionel Sambuc cu, DW_AT_stmt_list, -1U);
331f4a2713aSLionel Sambuc if (stmtOffset == -1U)
332*0a6a1f1dSLionel Sambuc return nullptr; // No line table for this compile unit.
333f4a2713aSLionel Sambuc
334f4a2713aSLionel Sambuc // See if the line table is cached.
335f4a2713aSLionel Sambuc if (const DWARFLineTable *lt = Line->getLineTable(stmtOffset))
336f4a2713aSLionel Sambuc return lt;
337f4a2713aSLionel Sambuc
338f4a2713aSLionel Sambuc // We have to parse it first.
339f4a2713aSLionel Sambuc DataExtractor lineData(getLineSection().Data, isLittleEndian(),
340f4a2713aSLionel Sambuc cu->getAddressByteSize());
341f4a2713aSLionel Sambuc return Line->getOrParseLineTable(lineData, stmtOffset);
342f4a2713aSLionel Sambuc }
343f4a2713aSLionel Sambuc
parseCompileUnits()344f4a2713aSLionel Sambuc void DWARFContext::parseCompileUnits() {
345*0a6a1f1dSLionel Sambuc CUs.parse(*this, getInfoSection());
346f4a2713aSLionel Sambuc }
347f4a2713aSLionel Sambuc
parseTypeUnits()348f4a2713aSLionel Sambuc void DWARFContext::parseTypeUnits() {
349*0a6a1f1dSLionel Sambuc if (!TUs.empty())
350*0a6a1f1dSLionel Sambuc return;
351*0a6a1f1dSLionel Sambuc for (const auto &I : getTypesSections()) {
352*0a6a1f1dSLionel Sambuc TUs.push_back(DWARFUnitSection<DWARFTypeUnit>());
353*0a6a1f1dSLionel Sambuc TUs.back().parse(*this, I.second);
354f4a2713aSLionel Sambuc }
355f4a2713aSLionel Sambuc }
356f4a2713aSLionel Sambuc
parseDWOCompileUnits()357f4a2713aSLionel Sambuc void DWARFContext::parseDWOCompileUnits() {
358*0a6a1f1dSLionel Sambuc DWOCUs.parseDWO(*this, getInfoDWOSection());
359f4a2713aSLionel Sambuc }
360f4a2713aSLionel Sambuc
parseDWOTypeUnits()361*0a6a1f1dSLionel Sambuc void DWARFContext::parseDWOTypeUnits() {
362*0a6a1f1dSLionel Sambuc if (!DWOTUs.empty())
363*0a6a1f1dSLionel Sambuc return;
364*0a6a1f1dSLionel Sambuc for (const auto &I : getTypesDWOSections()) {
365*0a6a1f1dSLionel Sambuc DWOTUs.push_back(DWARFUnitSection<DWARFTypeUnit>());
366*0a6a1f1dSLionel Sambuc DWOTUs.back().parseDWO(*this, I.second);
367f4a2713aSLionel Sambuc }
368f4a2713aSLionel Sambuc }
369f4a2713aSLionel Sambuc
getCompileUnitForOffset(uint32_t Offset)370f4a2713aSLionel Sambuc DWARFCompileUnit *DWARFContext::getCompileUnitForOffset(uint32_t Offset) {
371f4a2713aSLionel Sambuc parseCompileUnits();
372*0a6a1f1dSLionel Sambuc return CUs.getUnitForOffset(Offset);
373f4a2713aSLionel Sambuc }
374f4a2713aSLionel Sambuc
getCompileUnitForAddress(uint64_t Address)375f4a2713aSLionel Sambuc DWARFCompileUnit *DWARFContext::getCompileUnitForAddress(uint64_t Address) {
376f4a2713aSLionel Sambuc // First, get the offset of the compile unit.
377f4a2713aSLionel Sambuc uint32_t CUOffset = getDebugAranges()->findAddress(Address);
378f4a2713aSLionel Sambuc // Retrieve the compile unit.
379f4a2713aSLionel Sambuc return getCompileUnitForOffset(CUOffset);
380f4a2713aSLionel Sambuc }
381f4a2713aSLionel Sambuc
getFunctionNameForAddress(DWARFCompileUnit * CU,uint64_t Address,FunctionNameKind Kind,std::string & FunctionName)382*0a6a1f1dSLionel Sambuc static bool getFunctionNameForAddress(DWARFCompileUnit *CU, uint64_t Address,
383*0a6a1f1dSLionel Sambuc FunctionNameKind Kind,
384*0a6a1f1dSLionel Sambuc std::string &FunctionName) {
385*0a6a1f1dSLionel Sambuc if (Kind == FunctionNameKind::None)
386f4a2713aSLionel Sambuc return false;
387f4a2713aSLionel Sambuc // The address may correspond to instruction in some inlined function,
388f4a2713aSLionel Sambuc // so we have to build the chain of inlined functions and take the
389f4a2713aSLionel Sambuc // name of the topmost function in it.
390f4a2713aSLionel Sambuc const DWARFDebugInfoEntryInlinedChain &InlinedChain =
391f4a2713aSLionel Sambuc CU->getInlinedChainForAddress(Address);
392*0a6a1f1dSLionel Sambuc if (InlinedChain.DIEs.size() == 0)
393*0a6a1f1dSLionel Sambuc return false;
394f4a2713aSLionel Sambuc const DWARFDebugInfoEntryMinimal &TopFunctionDIE = InlinedChain.DIEs[0];
395*0a6a1f1dSLionel Sambuc if (const char *Name =
396*0a6a1f1dSLionel Sambuc TopFunctionDIE.getSubroutineName(InlinedChain.U, Kind)) {
397f4a2713aSLionel Sambuc FunctionName = Name;
398*0a6a1f1dSLionel Sambuc return true;
399f4a2713aSLionel Sambuc }
400*0a6a1f1dSLionel Sambuc return false;
401f4a2713aSLionel Sambuc }
402f4a2713aSLionel Sambuc
getLineInfoForAddress(uint64_t Address,DILineInfoSpecifier Spec)403*0a6a1f1dSLionel Sambuc DILineInfo DWARFContext::getLineInfoForAddress(uint64_t Address,
404*0a6a1f1dSLionel Sambuc DILineInfoSpecifier Spec) {
405*0a6a1f1dSLionel Sambuc DILineInfo Result;
406*0a6a1f1dSLionel Sambuc
407*0a6a1f1dSLionel Sambuc DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
408*0a6a1f1dSLionel Sambuc if (!CU)
409*0a6a1f1dSLionel Sambuc return Result;
410*0a6a1f1dSLionel Sambuc getFunctionNameForAddress(CU, Address, Spec.FNKind, Result.FunctionName);
411*0a6a1f1dSLionel Sambuc if (Spec.FLIKind != FileLineInfoKind::None) {
412*0a6a1f1dSLionel Sambuc if (const DWARFLineTable *LineTable = getLineTableForUnit(CU))
413*0a6a1f1dSLionel Sambuc LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(),
414*0a6a1f1dSLionel Sambuc Spec.FLIKind, Result);
415*0a6a1f1dSLionel Sambuc }
416*0a6a1f1dSLionel Sambuc return Result;
417*0a6a1f1dSLionel Sambuc }
418*0a6a1f1dSLionel Sambuc
419*0a6a1f1dSLionel Sambuc DILineInfoTable
getLineInfoForAddressRange(uint64_t Address,uint64_t Size,DILineInfoSpecifier Spec)420*0a6a1f1dSLionel Sambuc DWARFContext::getLineInfoForAddressRange(uint64_t Address, uint64_t Size,
421*0a6a1f1dSLionel Sambuc DILineInfoSpecifier Spec) {
422f4a2713aSLionel Sambuc DILineInfoTable Lines;
423f4a2713aSLionel Sambuc DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
424f4a2713aSLionel Sambuc if (!CU)
425f4a2713aSLionel Sambuc return Lines;
426f4a2713aSLionel Sambuc
427f4a2713aSLionel Sambuc std::string FunctionName = "<invalid>";
428*0a6a1f1dSLionel Sambuc getFunctionNameForAddress(CU, Address, Spec.FNKind, FunctionName);
429f4a2713aSLionel Sambuc
430f4a2713aSLionel Sambuc // If the Specifier says we don't need FileLineInfo, just
431f4a2713aSLionel Sambuc // return the top-most function at the starting address.
432*0a6a1f1dSLionel Sambuc if (Spec.FLIKind == FileLineInfoKind::None) {
433*0a6a1f1dSLionel Sambuc DILineInfo Result;
434*0a6a1f1dSLionel Sambuc Result.FunctionName = FunctionName;
435*0a6a1f1dSLionel Sambuc Lines.push_back(std::make_pair(Address, Result));
436f4a2713aSLionel Sambuc return Lines;
437f4a2713aSLionel Sambuc }
438f4a2713aSLionel Sambuc
439*0a6a1f1dSLionel Sambuc const DWARFLineTable *LineTable = getLineTableForUnit(CU);
440f4a2713aSLionel Sambuc
441f4a2713aSLionel Sambuc // Get the index of row we're looking for in the line table.
442f4a2713aSLionel Sambuc std::vector<uint32_t> RowVector;
443f4a2713aSLionel Sambuc if (!LineTable->lookupAddressRange(Address, Size, RowVector))
444f4a2713aSLionel Sambuc return Lines;
445f4a2713aSLionel Sambuc
446*0a6a1f1dSLionel Sambuc for (uint32_t RowIndex : RowVector) {
447f4a2713aSLionel Sambuc // Take file number and line/column from the row.
448f4a2713aSLionel Sambuc const DWARFDebugLine::Row &Row = LineTable->Rows[RowIndex];
449*0a6a1f1dSLionel Sambuc DILineInfo Result;
450*0a6a1f1dSLionel Sambuc LineTable->getFileNameByIndex(Row.File, CU->getCompilationDir(),
451*0a6a1f1dSLionel Sambuc Spec.FLIKind, Result.FileName);
452*0a6a1f1dSLionel Sambuc Result.FunctionName = FunctionName;
453*0a6a1f1dSLionel Sambuc Result.Line = Row.Line;
454*0a6a1f1dSLionel Sambuc Result.Column = Row.Column;
455*0a6a1f1dSLionel Sambuc Lines.push_back(std::make_pair(Row.Address, Result));
456f4a2713aSLionel Sambuc }
457f4a2713aSLionel Sambuc
458f4a2713aSLionel Sambuc return Lines;
459f4a2713aSLionel Sambuc }
460f4a2713aSLionel Sambuc
461*0a6a1f1dSLionel Sambuc DIInliningInfo
getInliningInfoForAddress(uint64_t Address,DILineInfoSpecifier Spec)462*0a6a1f1dSLionel Sambuc DWARFContext::getInliningInfoForAddress(uint64_t Address,
463*0a6a1f1dSLionel Sambuc DILineInfoSpecifier Spec) {
464*0a6a1f1dSLionel Sambuc DIInliningInfo InliningInfo;
465*0a6a1f1dSLionel Sambuc
466f4a2713aSLionel Sambuc DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
467f4a2713aSLionel Sambuc if (!CU)
468*0a6a1f1dSLionel Sambuc return InliningInfo;
469f4a2713aSLionel Sambuc
470*0a6a1f1dSLionel Sambuc const DWARFLineTable *LineTable = nullptr;
471f4a2713aSLionel Sambuc const DWARFDebugInfoEntryInlinedChain &InlinedChain =
472f4a2713aSLionel Sambuc CU->getInlinedChainForAddress(Address);
473*0a6a1f1dSLionel Sambuc if (InlinedChain.DIEs.size() == 0) {
474*0a6a1f1dSLionel Sambuc // If there is no DIE for address (e.g. it is in unavailable .dwo file),
475*0a6a1f1dSLionel Sambuc // try to at least get file/line info from symbol table.
476*0a6a1f1dSLionel Sambuc if (Spec.FLIKind != FileLineInfoKind::None) {
477*0a6a1f1dSLionel Sambuc DILineInfo Frame;
478*0a6a1f1dSLionel Sambuc LineTable = getLineTableForUnit(CU);
479*0a6a1f1dSLionel Sambuc if (LineTable &&
480*0a6a1f1dSLionel Sambuc LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(),
481*0a6a1f1dSLionel Sambuc Spec.FLIKind, Frame))
482*0a6a1f1dSLionel Sambuc InliningInfo.addFrame(Frame);
483*0a6a1f1dSLionel Sambuc }
484*0a6a1f1dSLionel Sambuc return InliningInfo;
485*0a6a1f1dSLionel Sambuc }
486f4a2713aSLionel Sambuc
487f4a2713aSLionel Sambuc uint32_t CallFile = 0, CallLine = 0, CallColumn = 0;
488f4a2713aSLionel Sambuc for (uint32_t i = 0, n = InlinedChain.DIEs.size(); i != n; i++) {
489f4a2713aSLionel Sambuc const DWARFDebugInfoEntryMinimal &FunctionDIE = InlinedChain.DIEs[i];
490*0a6a1f1dSLionel Sambuc DILineInfo Frame;
491f4a2713aSLionel Sambuc // Get function name if necessary.
492*0a6a1f1dSLionel Sambuc if (const char *Name =
493*0a6a1f1dSLionel Sambuc FunctionDIE.getSubroutineName(InlinedChain.U, Spec.FNKind))
494*0a6a1f1dSLionel Sambuc Frame.FunctionName = Name;
495*0a6a1f1dSLionel Sambuc if (Spec.FLIKind != FileLineInfoKind::None) {
496f4a2713aSLionel Sambuc if (i == 0) {
497f4a2713aSLionel Sambuc // For the topmost frame, initialize the line table of this
498f4a2713aSLionel Sambuc // compile unit and fetch file/line info from it.
499*0a6a1f1dSLionel Sambuc LineTable = getLineTableForUnit(CU);
500f4a2713aSLionel Sambuc // For the topmost routine, get file/line info from line table.
501*0a6a1f1dSLionel Sambuc if (LineTable)
502*0a6a1f1dSLionel Sambuc LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(),
503*0a6a1f1dSLionel Sambuc Spec.FLIKind, Frame);
504f4a2713aSLionel Sambuc } else {
505f4a2713aSLionel Sambuc // Otherwise, use call file, call line and call column from
506f4a2713aSLionel Sambuc // previous DIE in inlined chain.
507*0a6a1f1dSLionel Sambuc if (LineTable)
508*0a6a1f1dSLionel Sambuc LineTable->getFileNameByIndex(CallFile, CU->getCompilationDir(),
509*0a6a1f1dSLionel Sambuc Spec.FLIKind, Frame.FileName);
510*0a6a1f1dSLionel Sambuc Frame.Line = CallLine;
511*0a6a1f1dSLionel Sambuc Frame.Column = CallColumn;
512f4a2713aSLionel Sambuc }
513f4a2713aSLionel Sambuc // Get call file/line/column of a current DIE.
514f4a2713aSLionel Sambuc if (i + 1 < n) {
515f4a2713aSLionel Sambuc FunctionDIE.getCallerFrame(InlinedChain.U, CallFile, CallLine,
516f4a2713aSLionel Sambuc CallColumn);
517f4a2713aSLionel Sambuc }
518f4a2713aSLionel Sambuc }
519f4a2713aSLionel Sambuc InliningInfo.addFrame(Frame);
520f4a2713aSLionel Sambuc }
521f4a2713aSLionel Sambuc return InliningInfo;
522f4a2713aSLionel Sambuc }
523f4a2713aSLionel Sambuc
consumeCompressedDebugSectionHeader(StringRef & data,uint64_t & OriginalSize)524f4a2713aSLionel Sambuc static bool consumeCompressedDebugSectionHeader(StringRef &data,
525f4a2713aSLionel Sambuc uint64_t &OriginalSize) {
526f4a2713aSLionel Sambuc // Consume "ZLIB" prefix.
527f4a2713aSLionel Sambuc if (!data.startswith("ZLIB"))
528f4a2713aSLionel Sambuc return false;
529f4a2713aSLionel Sambuc data = data.substr(4);
530f4a2713aSLionel Sambuc // Consume uncompressed section size (big-endian 8 bytes).
531f4a2713aSLionel Sambuc DataExtractor extractor(data, false, 8);
532f4a2713aSLionel Sambuc uint32_t Offset = 0;
533f4a2713aSLionel Sambuc OriginalSize = extractor.getU64(&Offset);
534f4a2713aSLionel Sambuc if (Offset == 0)
535f4a2713aSLionel Sambuc return false;
536f4a2713aSLionel Sambuc data = data.substr(Offset);
537f4a2713aSLionel Sambuc return true;
538f4a2713aSLionel Sambuc }
539f4a2713aSLionel Sambuc
DWARFContextInMemory(const object::ObjectFile & Obj)540*0a6a1f1dSLionel Sambuc DWARFContextInMemory::DWARFContextInMemory(const object::ObjectFile &Obj)
541*0a6a1f1dSLionel Sambuc : IsLittleEndian(Obj.isLittleEndian()),
542*0a6a1f1dSLionel Sambuc AddressSize(Obj.getBytesInAddress()) {
543*0a6a1f1dSLionel Sambuc for (const SectionRef &Section : Obj.sections()) {
544f4a2713aSLionel Sambuc StringRef name;
545*0a6a1f1dSLionel Sambuc Section.getName(name);
546*0a6a1f1dSLionel Sambuc // Skip BSS and Virtual sections, they aren't interesting.
547*0a6a1f1dSLionel Sambuc bool IsBSS = Section.isBSS();
548*0a6a1f1dSLionel Sambuc if (IsBSS)
549*0a6a1f1dSLionel Sambuc continue;
550*0a6a1f1dSLionel Sambuc bool IsVirtual = Section.isVirtual();
551*0a6a1f1dSLionel Sambuc if (IsVirtual)
552*0a6a1f1dSLionel Sambuc continue;
553f4a2713aSLionel Sambuc StringRef data;
554*0a6a1f1dSLionel Sambuc Section.getContents(data);
555f4a2713aSLionel Sambuc
556f4a2713aSLionel Sambuc name = name.substr(name.find_first_not_of("._")); // Skip . and _ prefixes.
557f4a2713aSLionel Sambuc
558f4a2713aSLionel Sambuc // Check if debug info section is compressed with zlib.
559f4a2713aSLionel Sambuc if (name.startswith("zdebug_")) {
560f4a2713aSLionel Sambuc uint64_t OriginalSize;
561f4a2713aSLionel Sambuc if (!zlib::isAvailable() ||
562f4a2713aSLionel Sambuc !consumeCompressedDebugSectionHeader(data, OriginalSize))
563f4a2713aSLionel Sambuc continue;
564*0a6a1f1dSLionel Sambuc UncompressedSections.resize(UncompressedSections.size() + 1);
565*0a6a1f1dSLionel Sambuc if (zlib::uncompress(data, UncompressedSections.back(), OriginalSize) !=
566*0a6a1f1dSLionel Sambuc zlib::StatusOK) {
567*0a6a1f1dSLionel Sambuc UncompressedSections.pop_back();
568f4a2713aSLionel Sambuc continue;
569*0a6a1f1dSLionel Sambuc }
570f4a2713aSLionel Sambuc // Make data point to uncompressed section contents and save its contents.
571f4a2713aSLionel Sambuc name = name.substr(1);
572*0a6a1f1dSLionel Sambuc data = UncompressedSections.back();
573f4a2713aSLionel Sambuc }
574f4a2713aSLionel Sambuc
575*0a6a1f1dSLionel Sambuc StringRef *SectionData =
576f4a2713aSLionel Sambuc StringSwitch<StringRef *>(name)
577f4a2713aSLionel Sambuc .Case("debug_info", &InfoSection.Data)
578f4a2713aSLionel Sambuc .Case("debug_abbrev", &AbbrevSection)
579f4a2713aSLionel Sambuc .Case("debug_loc", &LocSection.Data)
580f4a2713aSLionel Sambuc .Case("debug_line", &LineSection.Data)
581f4a2713aSLionel Sambuc .Case("debug_aranges", &ARangeSection)
582f4a2713aSLionel Sambuc .Case("debug_frame", &DebugFrameSection)
583f4a2713aSLionel Sambuc .Case("debug_str", &StringSection)
584f4a2713aSLionel Sambuc .Case("debug_ranges", &RangeSection)
585f4a2713aSLionel Sambuc .Case("debug_pubnames", &PubNamesSection)
586f4a2713aSLionel Sambuc .Case("debug_pubtypes", &PubTypesSection)
587f4a2713aSLionel Sambuc .Case("debug_gnu_pubnames", &GnuPubNamesSection)
588f4a2713aSLionel Sambuc .Case("debug_gnu_pubtypes", &GnuPubTypesSection)
589f4a2713aSLionel Sambuc .Case("debug_info.dwo", &InfoDWOSection.Data)
590f4a2713aSLionel Sambuc .Case("debug_abbrev.dwo", &AbbrevDWOSection)
591*0a6a1f1dSLionel Sambuc .Case("debug_loc.dwo", &LocDWOSection.Data)
592*0a6a1f1dSLionel Sambuc .Case("debug_line.dwo", &LineDWOSection.Data)
593f4a2713aSLionel Sambuc .Case("debug_str.dwo", &StringDWOSection)
594f4a2713aSLionel Sambuc .Case("debug_str_offsets.dwo", &StringOffsetDWOSection)
595f4a2713aSLionel Sambuc .Case("debug_addr", &AddrSection)
596*0a6a1f1dSLionel Sambuc .Case("apple_names", &AppleNamesSection.Data)
597*0a6a1f1dSLionel Sambuc .Case("apple_types", &AppleTypesSection.Data)
598*0a6a1f1dSLionel Sambuc .Case("apple_namespaces", &AppleNamespacesSection.Data)
599*0a6a1f1dSLionel Sambuc .Case("apple_namespac", &AppleNamespacesSection.Data)
600*0a6a1f1dSLionel Sambuc .Case("apple_objc", &AppleObjCSection.Data)
601f4a2713aSLionel Sambuc // Any more debug info sections go here.
602*0a6a1f1dSLionel Sambuc .Default(nullptr);
603*0a6a1f1dSLionel Sambuc if (SectionData) {
604*0a6a1f1dSLionel Sambuc *SectionData = data;
605f4a2713aSLionel Sambuc if (name == "debug_ranges") {
606f4a2713aSLionel Sambuc // FIXME: Use the other dwo range section when we emit it.
607f4a2713aSLionel Sambuc RangeDWOSection = data;
608f4a2713aSLionel Sambuc }
609f4a2713aSLionel Sambuc } else if (name == "debug_types") {
610f4a2713aSLionel Sambuc // Find debug_types data by section rather than name as there are
611f4a2713aSLionel Sambuc // multiple, comdat grouped, debug_types sections.
612*0a6a1f1dSLionel Sambuc TypesSections[Section].Data = data;
613*0a6a1f1dSLionel Sambuc } else if (name == "debug_types.dwo") {
614*0a6a1f1dSLionel Sambuc TypesDWOSections[Section].Data = data;
615f4a2713aSLionel Sambuc }
616f4a2713aSLionel Sambuc
617*0a6a1f1dSLionel Sambuc section_iterator RelocatedSection = Section.getRelocatedSection();
618*0a6a1f1dSLionel Sambuc if (RelocatedSection == Obj.section_end())
619f4a2713aSLionel Sambuc continue;
620f4a2713aSLionel Sambuc
621f4a2713aSLionel Sambuc StringRef RelSecName;
622f4a2713aSLionel Sambuc RelocatedSection->getName(RelSecName);
623f4a2713aSLionel Sambuc RelSecName = RelSecName.substr(
624f4a2713aSLionel Sambuc RelSecName.find_first_not_of("._")); // Skip . and _ prefixes.
625f4a2713aSLionel Sambuc
626f4a2713aSLionel Sambuc // TODO: Add support for relocations in other sections as needed.
627f4a2713aSLionel Sambuc // Record relocations for the debug_info and debug_line sections.
628f4a2713aSLionel Sambuc RelocAddrMap *Map = StringSwitch<RelocAddrMap*>(RelSecName)
629f4a2713aSLionel Sambuc .Case("debug_info", &InfoSection.Relocs)
630f4a2713aSLionel Sambuc .Case("debug_loc", &LocSection.Relocs)
631f4a2713aSLionel Sambuc .Case("debug_info.dwo", &InfoDWOSection.Relocs)
632f4a2713aSLionel Sambuc .Case("debug_line", &LineSection.Relocs)
633*0a6a1f1dSLionel Sambuc .Case("apple_names", &AppleNamesSection.Relocs)
634*0a6a1f1dSLionel Sambuc .Case("apple_types", &AppleTypesSection.Relocs)
635*0a6a1f1dSLionel Sambuc .Case("apple_namespaces", &AppleNamespacesSection.Relocs)
636*0a6a1f1dSLionel Sambuc .Case("apple_namespac", &AppleNamespacesSection.Relocs)
637*0a6a1f1dSLionel Sambuc .Case("apple_objc", &AppleObjCSection.Relocs)
638*0a6a1f1dSLionel Sambuc .Default(nullptr);
639f4a2713aSLionel Sambuc if (!Map) {
640f4a2713aSLionel Sambuc // Find debug_types relocs by section rather than name as there are
641f4a2713aSLionel Sambuc // multiple, comdat grouped, debug_types sections.
642*0a6a1f1dSLionel Sambuc if (RelSecName == "debug_types")
643f4a2713aSLionel Sambuc Map = &TypesSections[*RelocatedSection].Relocs;
644*0a6a1f1dSLionel Sambuc else if (RelSecName == "debug_types.dwo")
645*0a6a1f1dSLionel Sambuc Map = &TypesDWOSections[*RelocatedSection].Relocs;
646*0a6a1f1dSLionel Sambuc else
647*0a6a1f1dSLionel Sambuc continue;
648f4a2713aSLionel Sambuc }
649f4a2713aSLionel Sambuc
650*0a6a1f1dSLionel Sambuc if (Section.relocation_begin() != Section.relocation_end()) {
651*0a6a1f1dSLionel Sambuc uint64_t SectionSize = RelocatedSection->getSize();
652*0a6a1f1dSLionel Sambuc for (const RelocationRef &Reloc : Section.relocations()) {
653f4a2713aSLionel Sambuc uint64_t Address;
654*0a6a1f1dSLionel Sambuc Reloc.getOffset(Address);
655f4a2713aSLionel Sambuc uint64_t Type;
656*0a6a1f1dSLionel Sambuc Reloc.getType(Type);
657f4a2713aSLionel Sambuc uint64_t SymAddr = 0;
658*0a6a1f1dSLionel Sambuc object::symbol_iterator Sym = Reloc.getSymbol();
659*0a6a1f1dSLionel Sambuc if (Sym != Obj.symbol_end())
660f4a2713aSLionel Sambuc Sym->getAddress(SymAddr);
661f4a2713aSLionel Sambuc
662*0a6a1f1dSLionel Sambuc object::RelocVisitor V(Obj);
663*0a6a1f1dSLionel Sambuc object::RelocToApply R(V.visit(Type, Reloc, SymAddr));
664f4a2713aSLionel Sambuc if (V.error()) {
665f4a2713aSLionel Sambuc SmallString<32> Name;
666*0a6a1f1dSLionel Sambuc std::error_code ec(Reloc.getTypeName(Name));
667f4a2713aSLionel Sambuc if (ec) {
668f4a2713aSLionel Sambuc errs() << "Aaaaaa! Nameless relocation! Aaaaaa!\n";
669f4a2713aSLionel Sambuc }
670f4a2713aSLionel Sambuc errs() << "error: failed to compute relocation: "
671f4a2713aSLionel Sambuc << Name << "\n";
672f4a2713aSLionel Sambuc continue;
673f4a2713aSLionel Sambuc }
674f4a2713aSLionel Sambuc
675f4a2713aSLionel Sambuc if (Address + R.Width > SectionSize) {
676f4a2713aSLionel Sambuc errs() << "error: " << R.Width << "-byte relocation starting "
677f4a2713aSLionel Sambuc << Address << " bytes into section " << name << " which is "
678f4a2713aSLionel Sambuc << SectionSize << " bytes long.\n";
679f4a2713aSLionel Sambuc continue;
680f4a2713aSLionel Sambuc }
681f4a2713aSLionel Sambuc if (R.Width > 8) {
682f4a2713aSLionel Sambuc errs() << "error: can't handle a relocation of more than 8 bytes at "
683f4a2713aSLionel Sambuc "a time.\n";
684f4a2713aSLionel Sambuc continue;
685f4a2713aSLionel Sambuc }
686f4a2713aSLionel Sambuc DEBUG(dbgs() << "Writing " << format("%p", R.Value)
687f4a2713aSLionel Sambuc << " at " << format("%p", Address)
688f4a2713aSLionel Sambuc << " with width " << format("%d", R.Width)
689f4a2713aSLionel Sambuc << "\n");
690f4a2713aSLionel Sambuc Map->insert(std::make_pair(Address, std::make_pair(R.Width, R.Value)));
691f4a2713aSLionel Sambuc }
692f4a2713aSLionel Sambuc }
693f4a2713aSLionel Sambuc }
694f4a2713aSLionel Sambuc }
695f4a2713aSLionel Sambuc
anchor()696f4a2713aSLionel Sambuc void DWARFContextInMemory::anchor() { }
697