blob: 4e3b4b67e3323d851d28303dd7956a6928a08dae [file] [log] [blame]
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +00001//===-- DWARFContext.cpp --------------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "DWARFContext.h"
Alexey Samsonov71d94f82012-07-19 07:03:58 +000011#include "llvm/ADT/SmallString.h"
Alexey Samsonov784baa62013-04-17 14:27:04 +000012#include "llvm/ADT/StringSwitch.h"
Alexey Samsonov005159e2013-04-23 10:17:34 +000013#include "llvm/ADT/STLExtras.h"
14#include "llvm/Support/Compression.h"
Benjamin Kramerfe80f1d2011-09-15 18:02:20 +000015#include "llvm/Support/Dwarf.h"
Benjamin Kramer34f864f2011-09-15 16:57:13 +000016#include "llvm/Support/Format.h"
Alexey Samsonov71d94f82012-07-19 07:03:58 +000017#include "llvm/Support/Path.h"
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000018#include "llvm/Support/raw_ostream.h"
Benjamin Kramer101b1c52011-09-15 20:43:22 +000019#include <algorithm>
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000020using namespace llvm;
Benjamin Kramerfe80f1d2011-09-15 18:02:20 +000021using namespace dwarf;
Rafael Espindola7486d922013-05-30 03:05:14 +000022using namespace object;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000023
Eric Christophere9403c12012-10-16 23:46:25 +000024typedef DWARFDebugLine::LineTable DWARFLineTable;
25
Alexey Samsonoveb0c1792013-08-23 06:56:01 +000026DWARFContext::~DWARFContext() {
27 DeleteContainerPointers(CUs);
28 DeleteContainerPointers(DWOCUs);
29}
30
David Blaikie9ddf28d2013-09-24 19:50:00 +000031static void dumpPubSection(raw_ostream &OS, StringRef Name, StringRef Data,
Eric Christopherc839df02013-09-25 23:02:36 +000032 bool LittleEndian, bool GnuStyle) {
David Blaikie9ddf28d2013-09-24 19:50:00 +000033 OS << "\n." << Name << " contents:\n";
34 DataExtractor pubNames(Data, LittleEndian, 0);
35 uint32_t offset = 0;
36 OS << "Length: " << pubNames.getU32(&offset) << "\n";
37 OS << "Version: " << pubNames.getU16(&offset) << "\n";
38 OS << "Offset in .debug_info: " << pubNames.getU32(&offset) << "\n";
39 OS << "Size: " << pubNames.getU32(&offset) << "\n";
Eric Christopherc839df02013-09-25 23:02:36 +000040 if (GnuStyle)
41 OS << "Offset Linkage Kind Name\n";
42 else
43 OS << "Offset Name\n";
44
David Blaikie9ddf28d2013-09-24 19:50:00 +000045 while (offset < Data.size()) {
46 uint32_t dieRef = pubNames.getU32(&offset);
47 if (dieRef == 0)
48 break;
Eric Christopherc839df02013-09-25 23:02:36 +000049 if (GnuStyle) {
50 PubIndexEntryDescriptor desc(pubNames.getU8(&offset));
51 OS << format("0x%8.8x ", dieRef)
52 << format("%-8s", dwarf::GDBIndexEntryLinkageString(desc.Linkage))
53 << ' ' << format("%-8s", dwarf::GDBIndexEntryKindString(desc.Kind))
54 << ' ' << '\"' << pubNames.getCStr(&offset) << "\"\n";
55 } else {
56 OS << format("0x%8.8x ", dieRef);
Eric Christopher7357f032013-09-25 23:02:41 +000057 OS << '\"' << pubNames.getCStr(&offset) << "\"\n";
Eric Christopherc839df02013-09-25 23:02:36 +000058 }
David Blaikie9ddf28d2013-09-24 19:50:00 +000059 }
60}
61
Eli Bendersky939a4e82013-01-25 20:26:43 +000062void DWARFContext::dump(raw_ostream &OS, DIDumpType DumpType) {
63 if (DumpType == DIDT_All || DumpType == DIDT_Abbrev) {
64 OS << ".debug_abbrev contents:\n";
65 getDebugAbbrev()->dump(OS);
66 }
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000067
Eli Bendersky939a4e82013-01-25 20:26:43 +000068 if (DumpType == DIDT_All || DumpType == DIDT_Info) {
69 OS << "\n.debug_info contents:\n";
70 for (unsigned i = 0, e = getNumCompileUnits(); i != e; ++i)
71 getCompileUnitAtIndex(i)->dump(OS);
72 }
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000073
David Blaikie438f5392013-09-23 22:44:47 +000074 if (DumpType == DIDT_All || DumpType == DIDT_Types) {
75 OS << "\n.debug_types contents:\n";
76 for (unsigned i = 0, e = getNumTypeUnits(); i != e; ++i)
77 getTypeUnitAtIndex(i)->dump(OS);
78 }
79
David Blaikie3df7d2f2013-06-19 21:37:13 +000080 if (DumpType == DIDT_All || DumpType == DIDT_Loc) {
David Blaikie438f5392013-09-23 22:44:47 +000081 OS << "\n.debug_loc contents:\n";
David Blaikie3df7d2f2013-06-19 21:37:13 +000082 getDebugLoc()->dump(OS);
83 }
84
Eli Bendersky60bdc5b2013-02-05 23:30:58 +000085 if (DumpType == DIDT_All || DumpType == DIDT_Frames) {
86 OS << "\n.debug_frame contents:\n";
87 getDebugFrame()->dump(OS);
88 }
89
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000090 uint32_t offset = 0;
Eli Bendersky939a4e82013-01-25 20:26:43 +000091 if (DumpType == DIDT_All || DumpType == DIDT_Aranges) {
92 OS << "\n.debug_aranges contents:\n";
93 DataExtractor arangesData(getARangeSection(), isLittleEndian(), 0);
94 DWARFDebugArangeSet set;
95 while (set.extract(arangesData, &offset))
96 set.dump(OS);
97 }
Benjamin Kramerb848e972011-09-15 02:12:05 +000098
Alexey Samsonoveceb5b92012-08-27 07:17:47 +000099 uint8_t savedAddressByteSize = 0;
Eli Bendersky939a4e82013-01-25 20:26:43 +0000100 if (DumpType == DIDT_All || DumpType == DIDT_Line) {
101 OS << "\n.debug_line contents:\n";
102 for (unsigned i = 0, e = getNumCompileUnits(); i != e; ++i) {
103 DWARFCompileUnit *cu = getCompileUnitAtIndex(i);
104 savedAddressByteSize = cu->getAddressByteSize();
105 unsigned stmtOffset =
106 cu->getCompileUnitDIE()->getAttributeValueAsUnsigned(cu, DW_AT_stmt_list,
107 -1U);
108 if (stmtOffset != -1U) {
David Blaikie9528b0e2013-09-23 17:42:01 +0000109 DataExtractor lineData(getLineSection().Data, isLittleEndian(),
Eli Bendersky939a4e82013-01-25 20:26:43 +0000110 savedAddressByteSize);
111 DWARFDebugLine::DumpingState state(OS);
David Blaikie9528b0e2013-09-23 17:42:01 +0000112 DWARFDebugLine::parseStatementTable(lineData, &getLineSection().Relocs, &stmtOffset, state);
Eli Bendersky939a4e82013-01-25 20:26:43 +0000113 }
Benjamin Kramerfe80f1d2011-09-15 18:02:20 +0000114 }
115 }
Benjamin Kramer34f864f2011-09-15 16:57:13 +0000116
Eli Bendersky939a4e82013-01-25 20:26:43 +0000117 if (DumpType == DIDT_All || DumpType == DIDT_Str) {
118 OS << "\n.debug_str contents:\n";
119 DataExtractor strData(getStringSection(), isLittleEndian(), 0);
120 offset = 0;
121 uint32_t strOffset = 0;
122 while (const char *s = strData.getCStr(&offset)) {
123 OS << format("0x%8.8x: \"%s\"\n", strOffset, s);
124 strOffset = offset;
125 }
Benjamin Kramer34f864f2011-09-15 16:57:13 +0000126 }
Alexey Samsonoveceb5b92012-08-27 07:17:47 +0000127
Eli Bendersky939a4e82013-01-25 20:26:43 +0000128 if (DumpType == DIDT_All || DumpType == DIDT_Ranges) {
129 OS << "\n.debug_ranges contents:\n";
130 // In fact, different compile units may have different address byte
131 // sizes, but for simplicity we just use the address byte size of the last
132 // compile unit (there is no easy and fast way to associate address range
133 // list and the compile unit it describes).
134 DataExtractor rangesData(getRangeSection(), isLittleEndian(),
135 savedAddressByteSize);
136 offset = 0;
137 DWARFDebugRangeList rangeList;
138 while (rangeList.extract(rangesData, &offset))
139 rangeList.dump(OS);
Eric Christopher82de10a2013-01-02 23:52:13 +0000140 }
Eric Christopher72f7bfb2013-01-15 23:56:56 +0000141
Eric Christopherc839df02013-09-25 23:02:36 +0000142 if (DumpType == DIDT_All || DumpType == DIDT_Pubnames)
143 dumpPubSection(OS, "debug_pubnames", getPubNamesSection(),
144 isLittleEndian(), false);
Krzysztof Parzyszeke38825f2013-02-12 16:20:28 +0000145
Eric Christopher7357f032013-09-25 23:02:41 +0000146 if (DumpType == DIDT_All || DumpType == DIDT_Pubtypes)
147 dumpPubSection(OS, "debug_pubtypes", getPubTypesSection(),
148 isLittleEndian(), false);
149
David Blaikie9ddf28d2013-09-24 19:50:00 +0000150 if (DumpType == DIDT_All || DumpType == DIDT_GnuPubnames)
151 dumpPubSection(OS, "debug_gnu_pubnames", getGnuPubNamesSection(),
Eric Christopherc839df02013-09-25 23:02:36 +0000152 isLittleEndian(), true /* GnuStyle */);
David Blaikie9ddf28d2013-09-24 19:50:00 +0000153
154 if (DumpType == DIDT_All || DumpType == DIDT_GnuPubtypes)
155 dumpPubSection(OS, "debug_gnu_pubtypes", getGnuPubTypesSection(),
Eric Christopherc839df02013-09-25 23:02:36 +0000156 isLittleEndian(), true /* GnuStyle */);
David Blaikie994c37f2013-09-19 23:01:29 +0000157
Eli Bendersky939a4e82013-01-25 20:26:43 +0000158 if (DumpType == DIDT_All || DumpType == DIDT_AbbrevDwo) {
Eric Christopher93f3fed2013-05-06 17:50:42 +0000159 const DWARFDebugAbbrev *D = getDebugAbbrevDWO();
160 if (D) {
161 OS << "\n.debug_abbrev.dwo contents:\n";
162 getDebugAbbrevDWO()->dump(OS);
Eli Bendersky939a4e82013-01-25 20:26:43 +0000163 }
164 }
165
Eric Christopher93f3fed2013-05-06 17:50:42 +0000166 if (DumpType == DIDT_All || DumpType == DIDT_InfoDwo)
167 if (getNumDWOCompileUnits()) {
168 OS << "\n.debug_info.dwo contents:\n";
169 for (unsigned i = 0, e = getNumDWOCompileUnits(); i != e; ++i)
Eric Christopher5a0c3662013-05-06 21:19:41 +0000170 getDWOCompileUnitAtIndex(i)->dump(OS);
Eli Bendersky939a4e82013-01-25 20:26:43 +0000171 }
Eric Christopher93f3fed2013-05-06 17:50:42 +0000172
173 if (DumpType == DIDT_All || DumpType == DIDT_StrDwo)
174 if (!getStringDWOSection().empty()) {
175 OS << "\n.debug_str.dwo contents:\n";
176 DataExtractor strDWOData(getStringDWOSection(), isLittleEndian(), 0);
177 offset = 0;
178 uint32_t strDWOOffset = 0;
179 while (const char *s = strDWOData.getCStr(&offset)) {
Eric Christopher5a0c3662013-05-06 21:19:41 +0000180 OS << format("0x%8.8x: \"%s\"\n", strDWOOffset, s);
181 strDWOOffset = offset;
Eric Christopher93f3fed2013-05-06 17:50:42 +0000182 }
183 }
184
185 if (DumpType == DIDT_All || DumpType == DIDT_StrOffsetsDwo)
186 if (!getStringOffsetDWOSection().empty()) {
187 OS << "\n.debug_str_offsets.dwo contents:\n";
188 DataExtractor strOffsetExt(getStringOffsetDWOSection(), isLittleEndian(), 0);
189 offset = 0;
Eric Christophere305e032013-05-06 21:19:44 +0000190 uint64_t size = getStringOffsetDWOSection().size();
191 while (offset < size) {
Eric Christopher5a0c3662013-05-06 21:19:41 +0000192 OS << format("0x%8.8x: ", offset);
193 OS << format("%8.8x\n", strOffsetExt.getU32(&offset));
Eric Christopher93f3fed2013-05-06 17:50:42 +0000194 }
195 }
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000196}
197
198const DWARFDebugAbbrev *DWARFContext::getDebugAbbrev() {
199 if (Abbrev)
200 return Abbrev.get();
201
202 DataExtractor abbrData(getAbbrevSection(), isLittleEndian(), 0);
203
204 Abbrev.reset(new DWARFDebugAbbrev());
205 Abbrev->parse(abbrData);
206 return Abbrev.get();
207}
208
Eric Christopher82de10a2013-01-02 23:52:13 +0000209const DWARFDebugAbbrev *DWARFContext::getDebugAbbrevDWO() {
210 if (AbbrevDWO)
211 return AbbrevDWO.get();
212
213 DataExtractor abbrData(getAbbrevDWOSection(), isLittleEndian(), 0);
214 AbbrevDWO.reset(new DWARFDebugAbbrev());
215 AbbrevDWO->parse(abbrData);
216 return AbbrevDWO.get();
217}
218
David Blaikie3df7d2f2013-06-19 21:37:13 +0000219const DWARFDebugLoc *DWARFContext::getDebugLoc() {
220 if (Loc)
221 return Loc.get();
222
David Blaikie9528b0e2013-09-23 17:42:01 +0000223 DataExtractor LocData(getLocSection().Data, isLittleEndian(), 0);
224 Loc.reset(new DWARFDebugLoc(getLocSection().Relocs));
David Blaikie3df7d2f2013-06-19 21:37:13 +0000225 // assume all compile units have the same address byte size
226 if (getNumCompileUnits())
227 Loc->parse(LocData, getCompileUnitAtIndex(0)->getAddressByteSize());
228 return Loc.get();
229}
230
Benjamin Kramer358f4fd2011-09-14 01:09:52 +0000231const DWARFDebugAranges *DWARFContext::getDebugAranges() {
232 if (Aranges)
233 return Aranges.get();
234
235 DataExtractor arangesData(getARangeSection(), isLittleEndian(), 0);
236
237 Aranges.reset(new DWARFDebugAranges());
238 Aranges->extract(arangesData);
Alexey Samsonov63a450a2012-11-16 08:36:25 +0000239 // Generate aranges from DIEs: even if .debug_aranges section is present,
240 // it may describe only a small subset of compilation units, so we need to
241 // manually build aranges for the rest of them.
242 Aranges->generate(this);
Benjamin Kramer358f4fd2011-09-14 01:09:52 +0000243 return Aranges.get();
244}
245
Eli Bendersky60bdc5b2013-02-05 23:30:58 +0000246const DWARFDebugFrame *DWARFContext::getDebugFrame() {
247 if (DebugFrame)
248 return DebugFrame.get();
249
250 // There's a "bug" in the DWARFv3 standard with respect to the target address
251 // size within debug frame sections. While DWARF is supposed to be independent
252 // of its container, FDEs have fields with size being "target address size",
253 // which isn't specified in DWARF in general. It's only specified for CUs, but
254 // .eh_frame can appear without a .debug_info section. Follow the example of
255 // other tools (libdwarf) and extract this from the container (ObjectFile
256 // provides this information). This problem is fixed in DWARFv4
257 // See this dwarf-discuss discussion for more details:
258 // http://lists.dwarfstd.org/htdig.cgi/dwarf-discuss-dwarfstd.org/2011-December/001173.html
259 DataExtractor debugFrameData(getDebugFrameSection(), isLittleEndian(),
260 getAddressSize());
261 DebugFrame.reset(new DWARFDebugFrame());
262 DebugFrame->parse(debugFrameData);
263 return DebugFrame.get();
264}
265
Eric Christophere9403c12012-10-16 23:46:25 +0000266const DWARFLineTable *
Benjamin Kramerc26ed9b2011-09-15 20:43:18 +0000267DWARFContext::getLineTableForCompileUnit(DWARFCompileUnit *cu) {
268 if (!Line)
David Blaikie9528b0e2013-09-23 17:42:01 +0000269 Line.reset(new DWARFDebugLine(&getLineSection().Relocs));
Benjamin Kramerb848e972011-09-15 02:12:05 +0000270
Benjamin Kramerc26ed9b2011-09-15 20:43:18 +0000271 unsigned stmtOffset =
272 cu->getCompileUnitDIE()->getAttributeValueAsUnsigned(cu, DW_AT_stmt_list,
273 -1U);
274 if (stmtOffset == -1U)
275 return 0; // No line table for this compile unit.
Benjamin Kramerb848e972011-09-15 02:12:05 +0000276
Benjamin Kramerc26ed9b2011-09-15 20:43:18 +0000277 // See if the line table is cached.
Eric Christophere9403c12012-10-16 23:46:25 +0000278 if (const DWARFLineTable *lt = Line->getLineTable(stmtOffset))
Benjamin Kramerc26ed9b2011-09-15 20:43:18 +0000279 return lt;
280
281 // We have to parse it first.
David Blaikie9528b0e2013-09-23 17:42:01 +0000282 DataExtractor lineData(getLineSection().Data, isLittleEndian(),
Benjamin Kramerc26ed9b2011-09-15 20:43:18 +0000283 cu->getAddressByteSize());
284 return Line->getOrParseLineTable(lineData, stmtOffset);
Benjamin Kramerb848e972011-09-15 02:12:05 +0000285}
286
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000287void DWARFContext::parseCompileUnits() {
288 uint32_t offset = 0;
David Blaikie9528b0e2013-09-23 17:42:01 +0000289 const DataExtractor &DIData = DataExtractor(getInfoSection().Data,
Eric Christopherb69b55f2012-10-16 23:46:23 +0000290 isLittleEndian(), 0);
291 while (DIData.isValidOffset(offset)) {
Alexey Samsonoveb0c1792013-08-23 06:56:01 +0000292 OwningPtr<DWARFCompileUnit> CU(new DWARFCompileUnit(
David Blaikie9528b0e2013-09-23 17:42:01 +0000293 getDebugAbbrev(), getInfoSection().Data, getAbbrevSection(),
Alexey Samsonoveb0c1792013-08-23 06:56:01 +0000294 getRangeSection(), getStringSection(), StringRef(), getAddrSection(),
David Blaikie9528b0e2013-09-23 17:42:01 +0000295 &getInfoSection().Relocs, isLittleEndian()));
Alexey Samsonoveb0c1792013-08-23 06:56:01 +0000296 if (!CU->extract(DIData, &offset)) {
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000297 break;
298 }
Alexey Samsonoveb0c1792013-08-23 06:56:01 +0000299 CUs.push_back(CU.take());
David Blaikiecd7c4982013-09-23 22:44:40 +0000300 offset = CUs.back()->getNextUnitOffset();
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000301 }
302}
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000303
David Blaikie438f5392013-09-23 22:44:47 +0000304void DWARFContext::parseTypeUnits() {
305 const std::map<object::SectionRef, Section> &Sections = getTypesSections();
306 for (std::map<object::SectionRef, Section>::const_iterator
307 I = Sections.begin(),
308 E = Sections.end();
309 I != E; ++I) {
310 uint32_t offset = 0;
311 const DataExtractor &DIData =
312 DataExtractor(I->second.Data, isLittleEndian(), 0);
313 while (DIData.isValidOffset(offset)) {
314 OwningPtr<DWARFTypeUnit> TU(new DWARFTypeUnit(
315 getDebugAbbrev(), I->second.Data, getAbbrevSection(),
316 getRangeSection(), getStringSection(), StringRef(), getAddrSection(),
317 &I->second.Relocs, isLittleEndian()));
318 if (!TU->extract(DIData, &offset))
319 break;
320 TUs.push_back(TU.take());
321 offset = TUs.back()->getNextUnitOffset();
322 }
323 }
324}
325
Eric Christopher82de10a2013-01-02 23:52:13 +0000326void DWARFContext::parseDWOCompileUnits() {
327 uint32_t offset = 0;
David Blaikie9528b0e2013-09-23 17:42:01 +0000328 const DataExtractor &DIData =
329 DataExtractor(getInfoDWOSection().Data, isLittleEndian(), 0);
Eric Christopher82de10a2013-01-02 23:52:13 +0000330 while (DIData.isValidOffset(offset)) {
Alexey Samsonoveb0c1792013-08-23 06:56:01 +0000331 OwningPtr<DWARFCompileUnit> DWOCU(new DWARFCompileUnit(
David Blaikie9528b0e2013-09-23 17:42:01 +0000332 getDebugAbbrevDWO(), getInfoDWOSection().Data, getAbbrevDWOSection(),
Alexey Samsonoveb0c1792013-08-23 06:56:01 +0000333 getRangeDWOSection(), getStringDWOSection(),
David Blaikie9528b0e2013-09-23 17:42:01 +0000334 getStringOffsetDWOSection(), getAddrSection(),
335 &getInfoDWOSection().Relocs, isLittleEndian()));
Alexey Samsonoveb0c1792013-08-23 06:56:01 +0000336 if (!DWOCU->extract(DIData, &offset)) {
Eric Christopher82de10a2013-01-02 23:52:13 +0000337 break;
338 }
Alexey Samsonoveb0c1792013-08-23 06:56:01 +0000339 DWOCUs.push_back(DWOCU.take());
David Blaikiecd7c4982013-09-23 22:44:40 +0000340 offset = DWOCUs.back()->getNextUnitOffset();
Eric Christopher82de10a2013-01-02 23:52:13 +0000341 }
342}
343
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000344namespace {
345 struct OffsetComparator {
Alexey Samsonoveb0c1792013-08-23 06:56:01 +0000346 bool operator()(const DWARFCompileUnit *LHS,
347 const DWARFCompileUnit *RHS) const {
348 return LHS->getOffset() < RHS->getOffset();
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000349 }
Alexey Samsonoveb0c1792013-08-23 06:56:01 +0000350 bool operator()(const DWARFCompileUnit *LHS, uint32_t RHS) const {
351 return LHS->getOffset() < RHS;
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000352 }
Alexey Samsonoveb0c1792013-08-23 06:56:01 +0000353 bool operator()(uint32_t LHS, const DWARFCompileUnit *RHS) const {
354 return LHS < RHS->getOffset();
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000355 }
356 };
357}
358
Alexey Samsonov38a63812012-08-30 07:49:50 +0000359DWARFCompileUnit *DWARFContext::getCompileUnitForOffset(uint32_t Offset) {
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000360 if (CUs.empty())
361 parseCompileUnits();
362
Alexey Samsonoveb0c1792013-08-23 06:56:01 +0000363 DWARFCompileUnit **CU =
364 std::lower_bound(CUs.begin(), CUs.end(), Offset, OffsetComparator());
365 if (CU != CUs.end()) {
366 return *CU;
367 }
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000368 return 0;
369}
370
Alexey Samsonov38a63812012-08-30 07:49:50 +0000371DWARFCompileUnit *DWARFContext::getCompileUnitForAddress(uint64_t Address) {
Benjamin Kramer9013db32011-09-15 21:59:13 +0000372 // First, get the offset of the compile unit.
Alexey Samsonov38a63812012-08-30 07:49:50 +0000373 uint32_t CUOffset = getDebugAranges()->findAddress(Address);
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000374 // Retrieve the compile unit.
Alexey Samsonov38a63812012-08-30 07:49:50 +0000375 return getCompileUnitForOffset(CUOffset);
376}
377
Eric Christophere9403c12012-10-16 23:46:25 +0000378static bool getFileNameForCompileUnit(DWARFCompileUnit *CU,
379 const DWARFLineTable *LineTable,
380 uint64_t FileIndex,
381 bool NeedsAbsoluteFilePath,
382 std::string &FileName) {
Alexey Samsonov38a63812012-08-30 07:49:50 +0000383 if (CU == 0 ||
384 LineTable == 0 ||
385 !LineTable->getFileNameByIndex(FileIndex, NeedsAbsoluteFilePath,
386 FileName))
387 return false;
388 if (NeedsAbsoluteFilePath && sys::path::is_relative(FileName)) {
389 // We may still need to append compilation directory of compile unit.
390 SmallString<16> AbsolutePath;
391 if (const char *CompilationDir = CU->getCompilationDir()) {
392 sys::path::append(AbsolutePath, CompilationDir);
393 }
394 sys::path::append(AbsolutePath, FileName);
395 FileName = AbsolutePath.str();
396 }
397 return true;
398}
399
Eric Christophere9403c12012-10-16 23:46:25 +0000400static bool getFileLineInfoForCompileUnit(DWARFCompileUnit *CU,
401 const DWARFLineTable *LineTable,
402 uint64_t Address,
403 bool NeedsAbsoluteFilePath,
404 std::string &FileName,
405 uint32_t &Line, uint32_t &Column) {
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000406 if (CU == 0 || LineTable == 0)
Alexey Samsonov38a63812012-08-30 07:49:50 +0000407 return false;
408 // Get the index of row we're looking for in the line table.
409 uint32_t RowIndex = LineTable->lookupAddress(Address);
410 if (RowIndex == -1U)
411 return false;
412 // Take file number and line/column from the row.
413 const DWARFDebugLine::Row &Row = LineTable->Rows[RowIndex];
414 if (!getFileNameForCompileUnit(CU, LineTable, Row.File,
415 NeedsAbsoluteFilePath, FileName))
416 return false;
417 Line = Row.Line;
418 Column = Row.Column;
419 return true;
420}
421
422DILineInfo DWARFContext::getLineInfoForAddress(uint64_t Address,
423 DILineInfoSpecifier Specifier) {
424 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
425 if (!CU)
Alexey Samsonov3e25c4a2012-07-02 05:54:45 +0000426 return DILineInfo();
Alexey Samsonov38a63812012-08-30 07:49:50 +0000427 std::string FileName = "<invalid>";
428 std::string FunctionName = "<invalid>";
429 uint32_t Line = 0;
430 uint32_t Column = 0;
431 if (Specifier.needs(DILineInfoSpecifier::FunctionName)) {
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000432 // The address may correspond to instruction in some inlined function,
433 // so we have to build the chain of inlined functions and take the
434 // name of the topmost function in it.
Alexey Samsonove6642902013-08-06 10:49:15 +0000435 const DWARFDebugInfoEntryInlinedChain &InlinedChain =
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000436 CU->getInlinedChainForAddress(Address);
Alexey Samsonove6642902013-08-06 10:49:15 +0000437 if (InlinedChain.DIEs.size() > 0) {
438 const DWARFDebugInfoEntryMinimal &TopFunctionDIE = InlinedChain.DIEs[0];
David Blaikiecd7c4982013-09-23 22:44:40 +0000439 if (const char *Name = TopFunctionDIE.getSubroutineName(InlinedChain.U))
Alexey Samsonov38a63812012-08-30 07:49:50 +0000440 FunctionName = Name;
Alexey Samsonov9d26b0b2012-07-17 15:28:35 +0000441 }
Alexey Samsonov3e25c4a2012-07-02 05:54:45 +0000442 }
Alexey Samsonov38a63812012-08-30 07:49:50 +0000443 if (Specifier.needs(DILineInfoSpecifier::FileLineInfo)) {
Eric Christophere9403c12012-10-16 23:46:25 +0000444 const DWARFLineTable *LineTable = getLineTableForCompileUnit(CU);
Alexey Samsonov38a63812012-08-30 07:49:50 +0000445 const bool NeedsAbsoluteFilePath =
446 Specifier.needs(DILineInfoSpecifier::AbsoluteFilePath);
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000447 getFileLineInfoForCompileUnit(CU, LineTable, Address,
448 NeedsAbsoluteFilePath,
Alexey Samsonov38a63812012-08-30 07:49:50 +0000449 FileName, Line, Column);
Alexey Samsonov3e25c4a2012-07-02 05:54:45 +0000450 }
Alexey Samsonov38a63812012-08-30 07:49:50 +0000451 return DILineInfo(StringRef(FileName), StringRef(FunctionName),
452 Line, Column);
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000453}
David Blaikie2d24e2a2011-12-20 02:50:00 +0000454
Andrew Kaylore27a7872013-01-26 00:28:05 +0000455DILineInfoTable DWARFContext::getLineInfoForAddressRange(uint64_t Address,
456 uint64_t Size,
457 DILineInfoSpecifier Specifier) {
458 DILineInfoTable Lines;
459 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
460 if (!CU)
461 return Lines;
462
463 std::string FunctionName = "<invalid>";
464 if (Specifier.needs(DILineInfoSpecifier::FunctionName)) {
465 // The address may correspond to instruction in some inlined function,
466 // so we have to build the chain of inlined functions and take the
467 // name of the topmost function in it.
Alexey Samsonove6642902013-08-06 10:49:15 +0000468 const DWARFDebugInfoEntryInlinedChain &InlinedChain =
Andrew Kaylore27a7872013-01-26 00:28:05 +0000469 CU->getInlinedChainForAddress(Address);
Alexey Samsonove6642902013-08-06 10:49:15 +0000470 if (InlinedChain.DIEs.size() > 0) {
471 const DWARFDebugInfoEntryMinimal &TopFunctionDIE = InlinedChain.DIEs[0];
David Blaikiecd7c4982013-09-23 22:44:40 +0000472 if (const char *Name = TopFunctionDIE.getSubroutineName(InlinedChain.U))
Andrew Kaylore27a7872013-01-26 00:28:05 +0000473 FunctionName = Name;
474 }
475 }
476
Andrew Kaylore27a7872013-01-26 00:28:05 +0000477 // If the Specifier says we don't need FileLineInfo, just
478 // return the top-most function at the starting address.
479 if (!Specifier.needs(DILineInfoSpecifier::FileLineInfo)) {
David Blaikieeaad5cd2013-09-22 17:01:50 +0000480 Lines.push_back(
481 std::make_pair(Address, DILineInfo("<invalid>", FunctionName, 0, 0)));
Andrew Kaylore27a7872013-01-26 00:28:05 +0000482 return Lines;
483 }
484
485 const DWARFLineTable *LineTable = getLineTableForCompileUnit(CU);
486 const bool NeedsAbsoluteFilePath =
487 Specifier.needs(DILineInfoSpecifier::AbsoluteFilePath);
488
489 // Get the index of row we're looking for in the line table.
490 std::vector<uint32_t> RowVector;
491 if (!LineTable->lookupAddressRange(Address, Size, RowVector))
492 return Lines;
493
494 uint32_t NumRows = RowVector.size();
495 for (uint32_t i = 0; i < NumRows; ++i) {
496 uint32_t RowIndex = RowVector[i];
497 // Take file number and line/column from the row.
498 const DWARFDebugLine::Row &Row = LineTable->Rows[RowIndex];
499 std::string FileName = "<invalid>";
500 getFileNameForCompileUnit(CU, LineTable, Row.File,
501 NeedsAbsoluteFilePath, FileName);
David Blaikieeaad5cd2013-09-22 17:01:50 +0000502 Lines.push_back(std::make_pair(
503 Row.Address, DILineInfo(FileName, FunctionName, Row.Line, Row.Column)));
Andrew Kaylore27a7872013-01-26 00:28:05 +0000504 }
505
506 return Lines;
507}
508
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000509DIInliningInfo DWARFContext::getInliningInfoForAddress(uint64_t Address,
510 DILineInfoSpecifier Specifier) {
511 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
512 if (!CU)
513 return DIInliningInfo();
514
Alexey Samsonove6642902013-08-06 10:49:15 +0000515 const DWARFDebugInfoEntryInlinedChain &InlinedChain =
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000516 CU->getInlinedChainForAddress(Address);
Alexey Samsonove6642902013-08-06 10:49:15 +0000517 if (InlinedChain.DIEs.size() == 0)
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000518 return DIInliningInfo();
519
520 DIInliningInfo InliningInfo;
521 uint32_t CallFile = 0, CallLine = 0, CallColumn = 0;
Eric Christophere9403c12012-10-16 23:46:25 +0000522 const DWARFLineTable *LineTable = 0;
Alexey Samsonove6642902013-08-06 10:49:15 +0000523 for (uint32_t i = 0, n = InlinedChain.DIEs.size(); i != n; i++) {
524 const DWARFDebugInfoEntryMinimal &FunctionDIE = InlinedChain.DIEs[i];
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000525 std::string FileName = "<invalid>";
526 std::string FunctionName = "<invalid>";
527 uint32_t Line = 0;
528 uint32_t Column = 0;
529 // Get function name if necessary.
530 if (Specifier.needs(DILineInfoSpecifier::FunctionName)) {
David Blaikiecd7c4982013-09-23 22:44:40 +0000531 if (const char *Name = FunctionDIE.getSubroutineName(InlinedChain.U))
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000532 FunctionName = Name;
533 }
534 if (Specifier.needs(DILineInfoSpecifier::FileLineInfo)) {
535 const bool NeedsAbsoluteFilePath =
536 Specifier.needs(DILineInfoSpecifier::AbsoluteFilePath);
537 if (i == 0) {
538 // For the topmost frame, initialize the line table of this
539 // compile unit and fetch file/line info from it.
540 LineTable = getLineTableForCompileUnit(CU);
541 // For the topmost routine, get file/line info from line table.
542 getFileLineInfoForCompileUnit(CU, LineTable, Address,
543 NeedsAbsoluteFilePath,
544 FileName, Line, Column);
545 } else {
546 // Otherwise, use call file, call line and call column from
547 // previous DIE in inlined chain.
548 getFileNameForCompileUnit(CU, LineTable, CallFile,
549 NeedsAbsoluteFilePath, FileName);
550 Line = CallLine;
551 Column = CallColumn;
552 }
553 // Get call file/line/column of a current DIE.
554 if (i + 1 < n) {
David Blaikiecd7c4982013-09-23 22:44:40 +0000555 FunctionDIE.getCallerFrame(InlinedChain.U, CallFile, CallLine,
Alexey Samsonove6642902013-08-06 10:49:15 +0000556 CallColumn);
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000557 }
558 }
559 DILineInfo Frame(StringRef(FileName), StringRef(FunctionName),
560 Line, Column);
561 InliningInfo.addFrame(Frame);
562 }
563 return InliningInfo;
564}
565
Alexey Samsonov005159e2013-04-23 10:17:34 +0000566static bool consumeCompressedDebugSectionHeader(StringRef &data,
567 uint64_t &OriginalSize) {
568 // Consume "ZLIB" prefix.
569 if (!data.startswith("ZLIB"))
570 return false;
571 data = data.substr(4);
572 // Consume uncompressed section size (big-endian 8 bytes).
573 DataExtractor extractor(data, false, 8);
574 uint32_t Offset = 0;
575 OriginalSize = extractor.getU64(&Offset);
576 if (Offset == 0)
577 return false;
578 data = data.substr(Offset);
579 return true;
580}
581
Eric Christopherd1726a42012-11-12 21:40:38 +0000582DWARFContextInMemory::DWARFContextInMemory(object::ObjectFile *Obj) :
Eli Bendersky60bdc5b2013-02-05 23:30:58 +0000583 IsLittleEndian(Obj->isLittleEndian()),
584 AddressSize(Obj->getBytesInAddress()) {
Eric Christopherd1726a42012-11-12 21:40:38 +0000585 error_code ec;
586 for (object::section_iterator i = Obj->begin_sections(),
587 e = Obj->end_sections();
588 i != e; i.increment(ec)) {
589 StringRef name;
590 i->getName(name);
591 StringRef data;
592 i->getContents(data);
593
Eric Christopherd1726a42012-11-12 21:40:38 +0000594 name = name.substr(name.find_first_not_of("._")); // Skip . and _ prefixes.
Alexey Samsonov784baa62013-04-17 14:27:04 +0000595
Alexey Samsonov005159e2013-04-23 10:17:34 +0000596 // Check if debug info section is compressed with zlib.
597 if (name.startswith("zdebug_")) {
598 uint64_t OriginalSize;
599 if (!zlib::isAvailable() ||
600 !consumeCompressedDebugSectionHeader(data, OriginalSize))
601 continue;
602 OwningPtr<MemoryBuffer> UncompressedSection;
603 if (zlib::uncompress(data, UncompressedSection, OriginalSize) !=
604 zlib::StatusOK)
605 continue;
606 // Make data point to uncompressed section contents and save its contents.
607 name = name.substr(1);
608 data = UncompressedSection->getBuffer();
609 UncompressedSections.push_back(UncompressedSection.take());
610 }
611
Eric Christopher7357f032013-09-25 23:02:41 +0000612 StringRef *Section =
613 StringSwitch<StringRef *>(name)
614 .Case("debug_info", &InfoSection.Data)
615 .Case("debug_abbrev", &AbbrevSection)
616 .Case("debug_loc", &LocSection.Data)
617 .Case("debug_line", &LineSection.Data)
618 .Case("debug_aranges", &ARangeSection)
619 .Case("debug_frame", &DebugFrameSection)
620 .Case("debug_str", &StringSection)
621 .Case("debug_ranges", &RangeSection)
622 .Case("debug_pubnames", &PubNamesSection)
623 .Case("debug_pubtypes", &PubTypesSection)
624 .Case("debug_gnu_pubnames", &GnuPubNamesSection)
625 .Case("debug_gnu_pubtypes", &GnuPubTypesSection)
626 .Case("debug_info.dwo", &InfoDWOSection.Data)
627 .Case("debug_abbrev.dwo", &AbbrevDWOSection)
628 .Case("debug_str.dwo", &StringDWOSection)
629 .Case("debug_str_offsets.dwo", &StringOffsetDWOSection)
630 .Case("debug_addr", &AddrSection)
631 // Any more debug info sections go here.
632 .Default(0);
Rafael Espindola7486d922013-05-30 03:05:14 +0000633 if (Section) {
634 *Section = data;
635 if (name == "debug_ranges") {
636 // FIXME: Use the other dwo range section when we emit it.
637 RangeDWOSection = data;
638 }
David Blaikie438f5392013-09-23 22:44:47 +0000639 } else if (name == "debug_types") {
David Blaikie75331652013-09-23 23:39:55 +0000640 // Find debug_types data by section rather than name as there are
641 // multiple, comdat grouped, debug_types sections.
David Blaikie438f5392013-09-23 22:44:47 +0000642 TypesSections[*i].Data = data;
Eric Christopher82de10a2013-01-02 23:52:13 +0000643 }
Eric Christopherd1726a42012-11-12 21:40:38 +0000644
Rafael Espindola7486d922013-05-30 03:05:14 +0000645 section_iterator RelocatedSection = i->getRelocatedSection();
646 if (RelocatedSection == Obj->end_sections())
647 continue;
648
649 StringRef RelSecName;
650 RelocatedSection->getName(RelSecName);
651 RelSecName = RelSecName.substr(
652 RelSecName.find_first_not_of("._")); // Skip . and _ prefixes.
653
Andrew Kayloree7c0d22013-01-25 22:50:58 +0000654 // TODO: Add support for relocations in other sections as needed.
655 // Record relocations for the debug_info and debug_line sections.
Rafael Espindola7486d922013-05-30 03:05:14 +0000656 RelocAddrMap *Map = StringSwitch<RelocAddrMap*>(RelSecName)
David Blaikie9528b0e2013-09-23 17:42:01 +0000657 .Case("debug_info", &InfoSection.Relocs)
658 .Case("debug_loc", &LocSection.Relocs)
659 .Case("debug_info.dwo", &InfoDWOSection.Relocs)
660 .Case("debug_line", &LineSection.Relocs)
Alexey Samsonov784baa62013-04-17 14:27:04 +0000661 .Default(0);
David Blaikie438f5392013-09-23 22:44:47 +0000662 if (!Map) {
663 if (RelSecName != "debug_types")
664 continue;
David Blaikie75331652013-09-23 23:39:55 +0000665 // Find debug_types relocs by section rather than name as there are
666 // multiple, comdat grouped, debug_types sections.
David Blaikie438f5392013-09-23 22:44:47 +0000667 Map = &TypesSections[*RelocatedSection].Relocs;
668 }
Eric Christopherd1726a42012-11-12 21:40:38 +0000669
670 if (i->begin_relocations() != i->end_relocations()) {
671 uint64_t SectionSize;
Rafael Espindola7486d922013-05-30 03:05:14 +0000672 RelocatedSection->getSize(SectionSize);
Eric Christopherd1726a42012-11-12 21:40:38 +0000673 for (object::relocation_iterator reloc_i = i->begin_relocations(),
674 reloc_e = i->end_relocations();
675 reloc_i != reloc_e; reloc_i.increment(ec)) {
676 uint64_t Address;
Rafael Espindola956ca722013-04-25 12:28:45 +0000677 reloc_i->getOffset(Address);
Eric Christopherd1726a42012-11-12 21:40:38 +0000678 uint64_t Type;
679 reloc_i->getType(Type);
Andrew Kayloree7c0d22013-01-25 22:50:58 +0000680 uint64_t SymAddr = 0;
681 // ELF relocations may need the symbol address
682 if (Obj->isELF()) {
Rafael Espindola6c1202c2013-06-05 01:33:53 +0000683 object::symbol_iterator Sym = reloc_i->getSymbol();
684 Sym->getAddress(SymAddr);
Andrew Kayloree7c0d22013-01-25 22:50:58 +0000685 }
Eric Christopherd1726a42012-11-12 21:40:38 +0000686
687 object::RelocVisitor V(Obj->getFileFormatName());
688 // The section address is always 0 for debug sections.
Andrew Kayloree7c0d22013-01-25 22:50:58 +0000689 object::RelocToApply R(V.visit(Type, *reloc_i, 0, SymAddr));
Eric Christopherd1726a42012-11-12 21:40:38 +0000690 if (V.error()) {
691 SmallString<32> Name;
692 error_code ec(reloc_i->getTypeName(Name));
693 if (ec) {
694 errs() << "Aaaaaa! Nameless relocation! Aaaaaa!\n";
695 }
696 errs() << "error: failed to compute relocation: "
697 << Name << "\n";
698 continue;
699 }
700
701 if (Address + R.Width > SectionSize) {
702 errs() << "error: " << R.Width << "-byte relocation starting "
703 << Address << " bytes into section " << name << " which is "
704 << SectionSize << " bytes long.\n";
705 continue;
706 }
707 if (R.Width > 8) {
708 errs() << "error: can't handle a relocation of more than 8 bytes at "
709 "a time.\n";
710 continue;
711 }
712 DEBUG(dbgs() << "Writing " << format("%p", R.Value)
713 << " at " << format("%p", Address)
714 << " with width " << format("%d", R.Width)
715 << "\n");
Eric Christopher82de10a2013-01-02 23:52:13 +0000716 Map->insert(std::make_pair(Address, std::make_pair(R.Width, R.Value)));
Eric Christopherd1726a42012-11-12 21:40:38 +0000717 }
718 }
719 }
720}
721
Alexey Samsonov005159e2013-04-23 10:17:34 +0000722DWARFContextInMemory::~DWARFContextInMemory() {
723 DeleteContainerPointers(UncompressedSections);
724}
725
David Blaikie2d24e2a2011-12-20 02:50:00 +0000726void DWARFContextInMemory::anchor() { }