blob: 9f521330c34f4b3b25b3a15bbed79c7c15df6167 [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;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000022
Eric Christophere9403c12012-10-16 23:46:25 +000023typedef DWARFDebugLine::LineTable DWARFLineTable;
24
Eli Bendersky939a4e82013-01-25 20:26:43 +000025void DWARFContext::dump(raw_ostream &OS, DIDumpType DumpType) {
26 if (DumpType == DIDT_All || DumpType == DIDT_Abbrev) {
27 OS << ".debug_abbrev contents:\n";
28 getDebugAbbrev()->dump(OS);
29 }
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000030
Eli Bendersky939a4e82013-01-25 20:26:43 +000031 if (DumpType == DIDT_All || DumpType == DIDT_Info) {
32 OS << "\n.debug_info contents:\n";
33 for (unsigned i = 0, e = getNumCompileUnits(); i != e; ++i)
34 getCompileUnitAtIndex(i)->dump(OS);
35 }
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000036
Eli Bendersky60bdc5b2013-02-05 23:30:58 +000037 if (DumpType == DIDT_All || DumpType == DIDT_Frames) {
38 OS << "\n.debug_frame contents:\n";
39 getDebugFrame()->dump(OS);
40 }
41
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000042 uint32_t offset = 0;
Eli Bendersky939a4e82013-01-25 20:26:43 +000043 if (DumpType == DIDT_All || DumpType == DIDT_Aranges) {
44 OS << "\n.debug_aranges contents:\n";
45 DataExtractor arangesData(getARangeSection(), isLittleEndian(), 0);
46 DWARFDebugArangeSet set;
47 while (set.extract(arangesData, &offset))
48 set.dump(OS);
49 }
Benjamin Kramerb848e972011-09-15 02:12:05 +000050
Alexey Samsonoveceb5b92012-08-27 07:17:47 +000051 uint8_t savedAddressByteSize = 0;
Eli Bendersky939a4e82013-01-25 20:26:43 +000052 if (DumpType == DIDT_All || DumpType == DIDT_Line) {
53 OS << "\n.debug_line contents:\n";
54 for (unsigned i = 0, e = getNumCompileUnits(); i != e; ++i) {
55 DWARFCompileUnit *cu = getCompileUnitAtIndex(i);
56 savedAddressByteSize = cu->getAddressByteSize();
57 unsigned stmtOffset =
58 cu->getCompileUnitDIE()->getAttributeValueAsUnsigned(cu, DW_AT_stmt_list,
59 -1U);
60 if (stmtOffset != -1U) {
61 DataExtractor lineData(getLineSection(), isLittleEndian(),
62 savedAddressByteSize);
63 DWARFDebugLine::DumpingState state(OS);
Andrew Kayloree7c0d22013-01-25 22:50:58 +000064 DWARFDebugLine::parseStatementTable(lineData, &lineRelocMap(), &stmtOffset, state);
Eli Bendersky939a4e82013-01-25 20:26:43 +000065 }
Benjamin Kramerfe80f1d2011-09-15 18:02:20 +000066 }
67 }
Benjamin Kramer34f864f2011-09-15 16:57:13 +000068
Eli Bendersky939a4e82013-01-25 20:26:43 +000069 if (DumpType == DIDT_All || DumpType == DIDT_Str) {
70 OS << "\n.debug_str contents:\n";
71 DataExtractor strData(getStringSection(), isLittleEndian(), 0);
72 offset = 0;
73 uint32_t strOffset = 0;
74 while (const char *s = strData.getCStr(&offset)) {
75 OS << format("0x%8.8x: \"%s\"\n", strOffset, s);
76 strOffset = offset;
77 }
Benjamin Kramer34f864f2011-09-15 16:57:13 +000078 }
Alexey Samsonoveceb5b92012-08-27 07:17:47 +000079
Eli Bendersky939a4e82013-01-25 20:26:43 +000080 if (DumpType == DIDT_All || DumpType == DIDT_Ranges) {
81 OS << "\n.debug_ranges contents:\n";
82 // In fact, different compile units may have different address byte
83 // sizes, but for simplicity we just use the address byte size of the last
84 // compile unit (there is no easy and fast way to associate address range
85 // list and the compile unit it describes).
86 DataExtractor rangesData(getRangeSection(), isLittleEndian(),
87 savedAddressByteSize);
88 offset = 0;
89 DWARFDebugRangeList rangeList;
90 while (rangeList.extract(rangesData, &offset))
91 rangeList.dump(OS);
Eric Christopher82de10a2013-01-02 23:52:13 +000092 }
Eric Christopher72f7bfb2013-01-15 23:56:56 +000093
Krzysztof Parzyszeke38825f2013-02-12 16:20:28 +000094 if (DumpType == DIDT_All || DumpType == DIDT_Pubnames) {
95 OS << "\n.debug_pubnames contents:\n";
96 DataExtractor pubNames(getPubNamesSection(), isLittleEndian(), 0);
97 offset = 0;
98 OS << "Length: " << pubNames.getU32(&offset) << "\n";
99 OS << "Version: " << pubNames.getU16(&offset) << "\n";
100 OS << "Offset in .debug_info: " << pubNames.getU32(&offset) << "\n";
101 OS << "Size: " << pubNames.getU32(&offset) << "\n";
102 OS << "\n Offset Name\n";
103 while (offset < getPubNamesSection().size()) {
104 uint32_t n = pubNames.getU32(&offset);
105 if (n == 0)
106 break;
107 OS << format("%8x ", n);
108 OS << pubNames.getCStr(&offset) << "\n";
109 }
110 }
111
Eli Bendersky939a4e82013-01-25 20:26:43 +0000112 if (DumpType == DIDT_All || DumpType == DIDT_AbbrevDwo) {
Eric Christopher93f3fed2013-05-06 17:50:42 +0000113 const DWARFDebugAbbrev *D = getDebugAbbrevDWO();
114 if (D) {
115 OS << "\n.debug_abbrev.dwo contents:\n";
116 getDebugAbbrevDWO()->dump(OS);
Eli Bendersky939a4e82013-01-25 20:26:43 +0000117 }
118 }
119
Eric Christopher93f3fed2013-05-06 17:50:42 +0000120 if (DumpType == DIDT_All || DumpType == DIDT_InfoDwo)
121 if (getNumDWOCompileUnits()) {
122 OS << "\n.debug_info.dwo contents:\n";
123 for (unsigned i = 0, e = getNumDWOCompileUnits(); i != e; ++i)
Eric Christopher5a0c3662013-05-06 21:19:41 +0000124 getDWOCompileUnitAtIndex(i)->dump(OS);
Eli Bendersky939a4e82013-01-25 20:26:43 +0000125 }
Eric Christopher93f3fed2013-05-06 17:50:42 +0000126
127 if (DumpType == DIDT_All || DumpType == DIDT_StrDwo)
128 if (!getStringDWOSection().empty()) {
129 OS << "\n.debug_str.dwo contents:\n";
130 DataExtractor strDWOData(getStringDWOSection(), isLittleEndian(), 0);
131 offset = 0;
132 uint32_t strDWOOffset = 0;
133 while (const char *s = strDWOData.getCStr(&offset)) {
Eric Christopher5a0c3662013-05-06 21:19:41 +0000134 OS << format("0x%8.8x: \"%s\"\n", strDWOOffset, s);
135 strDWOOffset = offset;
Eric Christopher93f3fed2013-05-06 17:50:42 +0000136 }
137 }
138
139 if (DumpType == DIDT_All || DumpType == DIDT_StrOffsetsDwo)
140 if (!getStringOffsetDWOSection().empty()) {
141 OS << "\n.debug_str_offsets.dwo contents:\n";
142 DataExtractor strOffsetExt(getStringOffsetDWOSection(), isLittleEndian(), 0);
143 offset = 0;
Eric Christophere305e032013-05-06 21:19:44 +0000144 uint64_t size = getStringOffsetDWOSection().size();
145 while (offset < size) {
Eric Christopher5a0c3662013-05-06 21:19:41 +0000146 OS << format("0x%8.8x: ", offset);
147 OS << format("%8.8x\n", strOffsetExt.getU32(&offset));
Eric Christopher93f3fed2013-05-06 17:50:42 +0000148 }
149 }
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000150}
151
152const DWARFDebugAbbrev *DWARFContext::getDebugAbbrev() {
153 if (Abbrev)
154 return Abbrev.get();
155
156 DataExtractor abbrData(getAbbrevSection(), isLittleEndian(), 0);
157
158 Abbrev.reset(new DWARFDebugAbbrev());
159 Abbrev->parse(abbrData);
160 return Abbrev.get();
161}
162
Eric Christopher82de10a2013-01-02 23:52:13 +0000163const DWARFDebugAbbrev *DWARFContext::getDebugAbbrevDWO() {
164 if (AbbrevDWO)
165 return AbbrevDWO.get();
166
167 DataExtractor abbrData(getAbbrevDWOSection(), isLittleEndian(), 0);
168 AbbrevDWO.reset(new DWARFDebugAbbrev());
169 AbbrevDWO->parse(abbrData);
170 return AbbrevDWO.get();
171}
172
Benjamin Kramer358f4fd2011-09-14 01:09:52 +0000173const DWARFDebugAranges *DWARFContext::getDebugAranges() {
174 if (Aranges)
175 return Aranges.get();
176
177 DataExtractor arangesData(getARangeSection(), isLittleEndian(), 0);
178
179 Aranges.reset(new DWARFDebugAranges());
180 Aranges->extract(arangesData);
Alexey Samsonov63a450a2012-11-16 08:36:25 +0000181 // Generate aranges from DIEs: even if .debug_aranges section is present,
182 // it may describe only a small subset of compilation units, so we need to
183 // manually build aranges for the rest of them.
184 Aranges->generate(this);
Benjamin Kramer358f4fd2011-09-14 01:09:52 +0000185 return Aranges.get();
186}
187
Eli Bendersky60bdc5b2013-02-05 23:30:58 +0000188const DWARFDebugFrame *DWARFContext::getDebugFrame() {
189 if (DebugFrame)
190 return DebugFrame.get();
191
192 // There's a "bug" in the DWARFv3 standard with respect to the target address
193 // size within debug frame sections. While DWARF is supposed to be independent
194 // of its container, FDEs have fields with size being "target address size",
195 // which isn't specified in DWARF in general. It's only specified for CUs, but
196 // .eh_frame can appear without a .debug_info section. Follow the example of
197 // other tools (libdwarf) and extract this from the container (ObjectFile
198 // provides this information). This problem is fixed in DWARFv4
199 // See this dwarf-discuss discussion for more details:
200 // http://lists.dwarfstd.org/htdig.cgi/dwarf-discuss-dwarfstd.org/2011-December/001173.html
201 DataExtractor debugFrameData(getDebugFrameSection(), isLittleEndian(),
202 getAddressSize());
203 DebugFrame.reset(new DWARFDebugFrame());
204 DebugFrame->parse(debugFrameData);
205 return DebugFrame.get();
206}
207
Eric Christophere9403c12012-10-16 23:46:25 +0000208const DWARFLineTable *
Benjamin Kramerc26ed9b2011-09-15 20:43:18 +0000209DWARFContext::getLineTableForCompileUnit(DWARFCompileUnit *cu) {
210 if (!Line)
Andrew Kayloree7c0d22013-01-25 22:50:58 +0000211 Line.reset(new DWARFDebugLine(&lineRelocMap()));
Benjamin Kramerb848e972011-09-15 02:12:05 +0000212
Benjamin Kramerc26ed9b2011-09-15 20:43:18 +0000213 unsigned stmtOffset =
214 cu->getCompileUnitDIE()->getAttributeValueAsUnsigned(cu, DW_AT_stmt_list,
215 -1U);
216 if (stmtOffset == -1U)
217 return 0; // No line table for this compile unit.
Benjamin Kramerb848e972011-09-15 02:12:05 +0000218
Benjamin Kramerc26ed9b2011-09-15 20:43:18 +0000219 // See if the line table is cached.
Eric Christophere9403c12012-10-16 23:46:25 +0000220 if (const DWARFLineTable *lt = Line->getLineTable(stmtOffset))
Benjamin Kramerc26ed9b2011-09-15 20:43:18 +0000221 return lt;
222
223 // We have to parse it first.
224 DataExtractor lineData(getLineSection(), isLittleEndian(),
225 cu->getAddressByteSize());
226 return Line->getOrParseLineTable(lineData, stmtOffset);
Benjamin Kramerb848e972011-09-15 02:12:05 +0000227}
228
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000229void DWARFContext::parseCompileUnits() {
230 uint32_t offset = 0;
Eric Christopherb69b55f2012-10-16 23:46:23 +0000231 const DataExtractor &DIData = DataExtractor(getInfoSection(),
232 isLittleEndian(), 0);
233 while (DIData.isValidOffset(offset)) {
Eric Christopher82de10a2013-01-02 23:52:13 +0000234 CUs.push_back(DWARFCompileUnit(getDebugAbbrev(), getInfoSection(),
235 getAbbrevSection(), getRangeSection(),
Eric Christopher72f7bfb2013-01-15 23:56:56 +0000236 getStringSection(), StringRef(),
237 getAddrSection(),
Eric Christopherdd8e9f32013-01-07 19:32:41 +0000238 &infoRelocMap(),
Eric Christopher82de10a2013-01-02 23:52:13 +0000239 isLittleEndian()));
Eric Christopherb69b55f2012-10-16 23:46:23 +0000240 if (!CUs.back().extract(DIData, &offset)) {
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000241 CUs.pop_back();
242 break;
243 }
244
245 offset = CUs.back().getNextCompileUnitOffset();
246 }
247}
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000248
Eric Christopher82de10a2013-01-02 23:52:13 +0000249void DWARFContext::parseDWOCompileUnits() {
250 uint32_t offset = 0;
251 const DataExtractor &DIData = DataExtractor(getInfoDWOSection(),
252 isLittleEndian(), 0);
253 while (DIData.isValidOffset(offset)) {
254 DWOCUs.push_back(DWARFCompileUnit(getDebugAbbrevDWO(), getInfoDWOSection(),
255 getAbbrevDWOSection(),
256 getRangeDWOSection(),
257 getStringDWOSection(),
Eric Christopherdd8e9f32013-01-07 19:32:41 +0000258 getStringOffsetDWOSection(),
Eric Christopher72f7bfb2013-01-15 23:56:56 +0000259 getAddrSection(),
Eric Christopher82de10a2013-01-02 23:52:13 +0000260 &infoDWORelocMap(),
261 isLittleEndian()));
262 if (!DWOCUs.back().extract(DIData, &offset)) {
263 DWOCUs.pop_back();
264 break;
265 }
266
267 offset = DWOCUs.back().getNextCompileUnitOffset();
268 }
269}
270
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000271namespace {
272 struct OffsetComparator {
273 bool operator()(const DWARFCompileUnit &LHS,
274 const DWARFCompileUnit &RHS) const {
275 return LHS.getOffset() < RHS.getOffset();
276 }
277 bool operator()(const DWARFCompileUnit &LHS, uint32_t RHS) const {
278 return LHS.getOffset() < RHS;
279 }
280 bool operator()(uint32_t LHS, const DWARFCompileUnit &RHS) const {
281 return LHS < RHS.getOffset();
282 }
283 };
284}
285
Alexey Samsonov38a63812012-08-30 07:49:50 +0000286DWARFCompileUnit *DWARFContext::getCompileUnitForOffset(uint32_t Offset) {
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000287 if (CUs.empty())
288 parseCompileUnits();
289
Alexey Samsonov38a63812012-08-30 07:49:50 +0000290 DWARFCompileUnit *CU = std::lower_bound(CUs.begin(), CUs.end(), Offset,
291 OffsetComparator());
292 if (CU != CUs.end())
293 return &*CU;
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000294 return 0;
295}
296
Alexey Samsonov38a63812012-08-30 07:49:50 +0000297DWARFCompileUnit *DWARFContext::getCompileUnitForAddress(uint64_t Address) {
Benjamin Kramer9013db32011-09-15 21:59:13 +0000298 // First, get the offset of the compile unit.
Alexey Samsonov38a63812012-08-30 07:49:50 +0000299 uint32_t CUOffset = getDebugAranges()->findAddress(Address);
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000300 // Retrieve the compile unit.
Alexey Samsonov38a63812012-08-30 07:49:50 +0000301 return getCompileUnitForOffset(CUOffset);
302}
303
Eric Christophere9403c12012-10-16 23:46:25 +0000304static bool getFileNameForCompileUnit(DWARFCompileUnit *CU,
305 const DWARFLineTable *LineTable,
306 uint64_t FileIndex,
307 bool NeedsAbsoluteFilePath,
308 std::string &FileName) {
Alexey Samsonov38a63812012-08-30 07:49:50 +0000309 if (CU == 0 ||
310 LineTable == 0 ||
311 !LineTable->getFileNameByIndex(FileIndex, NeedsAbsoluteFilePath,
312 FileName))
313 return false;
314 if (NeedsAbsoluteFilePath && sys::path::is_relative(FileName)) {
315 // We may still need to append compilation directory of compile unit.
316 SmallString<16> AbsolutePath;
317 if (const char *CompilationDir = CU->getCompilationDir()) {
318 sys::path::append(AbsolutePath, CompilationDir);
319 }
320 sys::path::append(AbsolutePath, FileName);
321 FileName = AbsolutePath.str();
322 }
323 return true;
324}
325
Eric Christophere9403c12012-10-16 23:46:25 +0000326static bool getFileLineInfoForCompileUnit(DWARFCompileUnit *CU,
327 const DWARFLineTable *LineTable,
328 uint64_t Address,
329 bool NeedsAbsoluteFilePath,
330 std::string &FileName,
331 uint32_t &Line, uint32_t &Column) {
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000332 if (CU == 0 || LineTable == 0)
Alexey Samsonov38a63812012-08-30 07:49:50 +0000333 return false;
334 // Get the index of row we're looking for in the line table.
335 uint32_t RowIndex = LineTable->lookupAddress(Address);
336 if (RowIndex == -1U)
337 return false;
338 // Take file number and line/column from the row.
339 const DWARFDebugLine::Row &Row = LineTable->Rows[RowIndex];
340 if (!getFileNameForCompileUnit(CU, LineTable, Row.File,
341 NeedsAbsoluteFilePath, FileName))
342 return false;
343 Line = Row.Line;
344 Column = Row.Column;
345 return true;
346}
347
348DILineInfo DWARFContext::getLineInfoForAddress(uint64_t Address,
349 DILineInfoSpecifier Specifier) {
350 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
351 if (!CU)
Alexey Samsonov3e25c4a2012-07-02 05:54:45 +0000352 return DILineInfo();
Alexey Samsonov38a63812012-08-30 07:49:50 +0000353 std::string FileName = "<invalid>";
354 std::string FunctionName = "<invalid>";
355 uint32_t Line = 0;
356 uint32_t Column = 0;
357 if (Specifier.needs(DILineInfoSpecifier::FunctionName)) {
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000358 // The address may correspond to instruction in some inlined function,
359 // so we have to build the chain of inlined functions and take the
360 // name of the topmost function in it.
361 const DWARFDebugInfoEntryMinimal::InlinedChain &InlinedChain =
362 CU->getInlinedChainForAddress(Address);
363 if (InlinedChain.size() > 0) {
364 const DWARFDebugInfoEntryMinimal &TopFunctionDIE = InlinedChain[0];
365 if (const char *Name = TopFunctionDIE.getSubroutineName(CU))
Alexey Samsonov38a63812012-08-30 07:49:50 +0000366 FunctionName = Name;
Alexey Samsonov9d26b0b2012-07-17 15:28:35 +0000367 }
Alexey Samsonov3e25c4a2012-07-02 05:54:45 +0000368 }
Alexey Samsonov38a63812012-08-30 07:49:50 +0000369 if (Specifier.needs(DILineInfoSpecifier::FileLineInfo)) {
Eric Christophere9403c12012-10-16 23:46:25 +0000370 const DWARFLineTable *LineTable = getLineTableForCompileUnit(CU);
Alexey Samsonov38a63812012-08-30 07:49:50 +0000371 const bool NeedsAbsoluteFilePath =
372 Specifier.needs(DILineInfoSpecifier::AbsoluteFilePath);
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000373 getFileLineInfoForCompileUnit(CU, LineTable, Address,
374 NeedsAbsoluteFilePath,
Alexey Samsonov38a63812012-08-30 07:49:50 +0000375 FileName, Line, Column);
Alexey Samsonov3e25c4a2012-07-02 05:54:45 +0000376 }
Alexey Samsonov38a63812012-08-30 07:49:50 +0000377 return DILineInfo(StringRef(FileName), StringRef(FunctionName),
378 Line, Column);
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000379}
David Blaikie2d24e2a2011-12-20 02:50:00 +0000380
Andrew Kaylore27a7872013-01-26 00:28:05 +0000381DILineInfoTable DWARFContext::getLineInfoForAddressRange(uint64_t Address,
382 uint64_t Size,
383 DILineInfoSpecifier Specifier) {
384 DILineInfoTable Lines;
385 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
386 if (!CU)
387 return Lines;
388
389 std::string FunctionName = "<invalid>";
390 if (Specifier.needs(DILineInfoSpecifier::FunctionName)) {
391 // The address may correspond to instruction in some inlined function,
392 // so we have to build the chain of inlined functions and take the
393 // name of the topmost function in it.
394 const DWARFDebugInfoEntryMinimal::InlinedChain &InlinedChain =
395 CU->getInlinedChainForAddress(Address);
396 if (InlinedChain.size() > 0) {
397 const DWARFDebugInfoEntryMinimal &TopFunctionDIE = InlinedChain[0];
398 if (const char *Name = TopFunctionDIE.getSubroutineName(CU))
399 FunctionName = Name;
400 }
401 }
402
403 StringRef FuncNameRef = StringRef(FunctionName);
404
405 // If the Specifier says we don't need FileLineInfo, just
406 // return the top-most function at the starting address.
407 if (!Specifier.needs(DILineInfoSpecifier::FileLineInfo)) {
408 Lines.push_back(std::make_pair(Address,
409 DILineInfo(StringRef("<invalid>"),
410 FuncNameRef, 0, 0)));
411 return Lines;
412 }
413
414 const DWARFLineTable *LineTable = getLineTableForCompileUnit(CU);
415 const bool NeedsAbsoluteFilePath =
416 Specifier.needs(DILineInfoSpecifier::AbsoluteFilePath);
417
418 // Get the index of row we're looking for in the line table.
419 std::vector<uint32_t> RowVector;
420 if (!LineTable->lookupAddressRange(Address, Size, RowVector))
421 return Lines;
422
423 uint32_t NumRows = RowVector.size();
424 for (uint32_t i = 0; i < NumRows; ++i) {
425 uint32_t RowIndex = RowVector[i];
426 // Take file number and line/column from the row.
427 const DWARFDebugLine::Row &Row = LineTable->Rows[RowIndex];
428 std::string FileName = "<invalid>";
429 getFileNameForCompileUnit(CU, LineTable, Row.File,
430 NeedsAbsoluteFilePath, FileName);
431 Lines.push_back(std::make_pair(Row.Address,
432 DILineInfo(StringRef(FileName),
433 FuncNameRef, Row.Line, Row.Column)));
434 }
435
436 return Lines;
437}
438
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000439DIInliningInfo DWARFContext::getInliningInfoForAddress(uint64_t Address,
440 DILineInfoSpecifier Specifier) {
441 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
442 if (!CU)
443 return DIInliningInfo();
444
445 const DWARFDebugInfoEntryMinimal::InlinedChain &InlinedChain =
446 CU->getInlinedChainForAddress(Address);
447 if (InlinedChain.size() == 0)
448 return DIInliningInfo();
449
450 DIInliningInfo InliningInfo;
451 uint32_t CallFile = 0, CallLine = 0, CallColumn = 0;
Eric Christophere9403c12012-10-16 23:46:25 +0000452 const DWARFLineTable *LineTable = 0;
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000453 for (uint32_t i = 0, n = InlinedChain.size(); i != n; i++) {
454 const DWARFDebugInfoEntryMinimal &FunctionDIE = InlinedChain[i];
455 std::string FileName = "<invalid>";
456 std::string FunctionName = "<invalid>";
457 uint32_t Line = 0;
458 uint32_t Column = 0;
459 // Get function name if necessary.
460 if (Specifier.needs(DILineInfoSpecifier::FunctionName)) {
461 if (const char *Name = FunctionDIE.getSubroutineName(CU))
462 FunctionName = Name;
463 }
464 if (Specifier.needs(DILineInfoSpecifier::FileLineInfo)) {
465 const bool NeedsAbsoluteFilePath =
466 Specifier.needs(DILineInfoSpecifier::AbsoluteFilePath);
467 if (i == 0) {
468 // For the topmost frame, initialize the line table of this
469 // compile unit and fetch file/line info from it.
470 LineTable = getLineTableForCompileUnit(CU);
471 // For the topmost routine, get file/line info from line table.
472 getFileLineInfoForCompileUnit(CU, LineTable, Address,
473 NeedsAbsoluteFilePath,
474 FileName, Line, Column);
475 } else {
476 // Otherwise, use call file, call line and call column from
477 // previous DIE in inlined chain.
478 getFileNameForCompileUnit(CU, LineTable, CallFile,
479 NeedsAbsoluteFilePath, FileName);
480 Line = CallLine;
481 Column = CallColumn;
482 }
483 // Get call file/line/column of a current DIE.
484 if (i + 1 < n) {
485 FunctionDIE.getCallerFrame(CU, CallFile, CallLine, CallColumn);
486 }
487 }
488 DILineInfo Frame(StringRef(FileName), StringRef(FunctionName),
489 Line, Column);
490 InliningInfo.addFrame(Frame);
491 }
492 return InliningInfo;
493}
494
Alexey Samsonov005159e2013-04-23 10:17:34 +0000495static bool consumeCompressedDebugSectionHeader(StringRef &data,
496 uint64_t &OriginalSize) {
497 // Consume "ZLIB" prefix.
498 if (!data.startswith("ZLIB"))
499 return false;
500 data = data.substr(4);
501 // Consume uncompressed section size (big-endian 8 bytes).
502 DataExtractor extractor(data, false, 8);
503 uint32_t Offset = 0;
504 OriginalSize = extractor.getU64(&Offset);
505 if (Offset == 0)
506 return false;
507 data = data.substr(Offset);
508 return true;
509}
510
Eric Christopherd1726a42012-11-12 21:40:38 +0000511DWARFContextInMemory::DWARFContextInMemory(object::ObjectFile *Obj) :
Eli Bendersky60bdc5b2013-02-05 23:30:58 +0000512 IsLittleEndian(Obj->isLittleEndian()),
513 AddressSize(Obj->getBytesInAddress()) {
Eric Christopherd1726a42012-11-12 21:40:38 +0000514 error_code ec;
515 for (object::section_iterator i = Obj->begin_sections(),
516 e = Obj->end_sections();
517 i != e; i.increment(ec)) {
518 StringRef name;
519 i->getName(name);
520 StringRef data;
521 i->getContents(data);
522
Eric Christopherd1726a42012-11-12 21:40:38 +0000523 name = name.substr(name.find_first_not_of("._")); // Skip . and _ prefixes.
Alexey Samsonov784baa62013-04-17 14:27:04 +0000524
Alexey Samsonov005159e2013-04-23 10:17:34 +0000525 // Check if debug info section is compressed with zlib.
526 if (name.startswith("zdebug_")) {
527 uint64_t OriginalSize;
528 if (!zlib::isAvailable() ||
529 !consumeCompressedDebugSectionHeader(data, OriginalSize))
530 continue;
531 OwningPtr<MemoryBuffer> UncompressedSection;
532 if (zlib::uncompress(data, UncompressedSection, OriginalSize) !=
533 zlib::StatusOK)
534 continue;
535 // Make data point to uncompressed section contents and save its contents.
536 name = name.substr(1);
537 data = UncompressedSection->getBuffer();
538 UncompressedSections.push_back(UncompressedSection.take());
539 }
540
Alexey Samsonov784baa62013-04-17 14:27:04 +0000541 StringRef *Section = StringSwitch<StringRef*>(name)
542 .Case("debug_info", &InfoSection)
543 .Case("debug_abbrev", &AbbrevSection)
544 .Case("debug_line", &LineSection)
545 .Case("debug_aranges", &ARangeSection)
546 .Case("debug_frame", &DebugFrameSection)
547 .Case("debug_str", &StringSection)
548 .Case("debug_ranges", &RangeSection)
549 .Case("debug_pubnames", &PubNamesSection)
550 .Case("debug_info.dwo", &InfoDWOSection)
551 .Case("debug_abbrev.dwo", &AbbrevDWOSection)
552 .Case("debug_str.dwo", &StringDWOSection)
553 .Case("debug_str_offsets.dwo", &StringOffsetDWOSection)
554 .Case("debug_addr", &AddrSection)
555 // Any more debug info sections go here.
556 .Default(0);
557 if (!Section)
558 continue;
559 *Section = data;
560 if (name == "debug_ranges") {
Eric Christopher82de10a2013-01-02 23:52:13 +0000561 // FIXME: Use the other dwo range section when we emit it.
562 RangeDWOSection = data;
Eric Christopher82de10a2013-01-02 23:52:13 +0000563 }
Eric Christopherd1726a42012-11-12 21:40:38 +0000564
Andrew Kayloree7c0d22013-01-25 22:50:58 +0000565 // TODO: Add support for relocations in other sections as needed.
566 // Record relocations for the debug_info and debug_line sections.
Alexey Samsonov784baa62013-04-17 14:27:04 +0000567 RelocAddrMap *Map = StringSwitch<RelocAddrMap*>(name)
568 .Case("debug_info", &InfoRelocMap)
569 .Case("debug_info.dwo", &InfoDWORelocMap)
570 .Case("debug_line", &LineRelocMap)
571 .Default(0);
572 if (!Map)
Eric Christopherd1726a42012-11-12 21:40:38 +0000573 continue;
574
575 if (i->begin_relocations() != i->end_relocations()) {
576 uint64_t SectionSize;
577 i->getSize(SectionSize);
578 for (object::relocation_iterator reloc_i = i->begin_relocations(),
579 reloc_e = i->end_relocations();
580 reloc_i != reloc_e; reloc_i.increment(ec)) {
581 uint64_t Address;
Rafael Espindola956ca722013-04-25 12:28:45 +0000582 reloc_i->getOffset(Address);
Eric Christopherd1726a42012-11-12 21:40:38 +0000583 uint64_t Type;
584 reloc_i->getType(Type);
Andrew Kayloree7c0d22013-01-25 22:50:58 +0000585 uint64_t SymAddr = 0;
586 // ELF relocations may need the symbol address
587 if (Obj->isELF()) {
588 object::SymbolRef Sym;
589 reloc_i->getSymbol(Sym);
590 Sym.getAddress(SymAddr);
591 }
Eric Christopherd1726a42012-11-12 21:40:38 +0000592
593 object::RelocVisitor V(Obj->getFileFormatName());
594 // The section address is always 0 for debug sections.
Andrew Kayloree7c0d22013-01-25 22:50:58 +0000595 object::RelocToApply R(V.visit(Type, *reloc_i, 0, SymAddr));
Eric Christopherd1726a42012-11-12 21:40:38 +0000596 if (V.error()) {
597 SmallString<32> Name;
598 error_code ec(reloc_i->getTypeName(Name));
599 if (ec) {
600 errs() << "Aaaaaa! Nameless relocation! Aaaaaa!\n";
601 }
602 errs() << "error: failed to compute relocation: "
603 << Name << "\n";
604 continue;
605 }
606
607 if (Address + R.Width > SectionSize) {
608 errs() << "error: " << R.Width << "-byte relocation starting "
609 << Address << " bytes into section " << name << " which is "
610 << SectionSize << " bytes long.\n";
611 continue;
612 }
613 if (R.Width > 8) {
614 errs() << "error: can't handle a relocation of more than 8 bytes at "
615 "a time.\n";
616 continue;
617 }
618 DEBUG(dbgs() << "Writing " << format("%p", R.Value)
619 << " at " << format("%p", Address)
620 << " with width " << format("%d", R.Width)
621 << "\n");
Eric Christopher82de10a2013-01-02 23:52:13 +0000622 Map->insert(std::make_pair(Address, std::make_pair(R.Width, R.Value)));
Eric Christopherd1726a42012-11-12 21:40:38 +0000623 }
624 }
625 }
626}
627
Alexey Samsonov005159e2013-04-23 10:17:34 +0000628DWARFContextInMemory::~DWARFContextInMemory() {
629 DeleteContainerPointers(UncompressedSections);
630}
631
David Blaikie2d24e2a2011-12-20 02:50:00 +0000632void DWARFContextInMemory::anchor() { }