blob: aaae952da5b10c114fa1a204fde8c8ab28814442 [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
Eli Bendersky939a4e82013-01-25 20:26:43 +000026void DWARFContext::dump(raw_ostream &OS, DIDumpType DumpType) {
27 if (DumpType == DIDT_All || DumpType == DIDT_Abbrev) {
28 OS << ".debug_abbrev contents:\n";
29 getDebugAbbrev()->dump(OS);
30 }
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000031
Eli Bendersky939a4e82013-01-25 20:26:43 +000032 if (DumpType == DIDT_All || DumpType == DIDT_Info) {
33 OS << "\n.debug_info contents:\n";
34 for (unsigned i = 0, e = getNumCompileUnits(); i != e; ++i)
35 getCompileUnitAtIndex(i)->dump(OS);
36 }
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000037
Eli Bendersky60bdc5b2013-02-05 23:30:58 +000038 if (DumpType == DIDT_All || DumpType == DIDT_Frames) {
39 OS << "\n.debug_frame contents:\n";
40 getDebugFrame()->dump(OS);
41 }
42
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000043 uint32_t offset = 0;
Eli Bendersky939a4e82013-01-25 20:26:43 +000044 if (DumpType == DIDT_All || DumpType == DIDT_Aranges) {
45 OS << "\n.debug_aranges contents:\n";
46 DataExtractor arangesData(getARangeSection(), isLittleEndian(), 0);
47 DWARFDebugArangeSet set;
48 while (set.extract(arangesData, &offset))
49 set.dump(OS);
50 }
Benjamin Kramerb848e972011-09-15 02:12:05 +000051
Alexey Samsonoveceb5b92012-08-27 07:17:47 +000052 uint8_t savedAddressByteSize = 0;
Eli Bendersky939a4e82013-01-25 20:26:43 +000053 if (DumpType == DIDT_All || DumpType == DIDT_Line) {
54 OS << "\n.debug_line contents:\n";
55 for (unsigned i = 0, e = getNumCompileUnits(); i != e; ++i) {
56 DWARFCompileUnit *cu = getCompileUnitAtIndex(i);
57 savedAddressByteSize = cu->getAddressByteSize();
58 unsigned stmtOffset =
59 cu->getCompileUnitDIE()->getAttributeValueAsUnsigned(cu, DW_AT_stmt_list,
60 -1U);
61 if (stmtOffset != -1U) {
62 DataExtractor lineData(getLineSection(), isLittleEndian(),
63 savedAddressByteSize);
64 DWARFDebugLine::DumpingState state(OS);
Andrew Kayloree7c0d22013-01-25 22:50:58 +000065 DWARFDebugLine::parseStatementTable(lineData, &lineRelocMap(), &stmtOffset, state);
Eli Bendersky939a4e82013-01-25 20:26:43 +000066 }
Benjamin Kramerfe80f1d2011-09-15 18:02:20 +000067 }
68 }
Benjamin Kramer34f864f2011-09-15 16:57:13 +000069
Eli Bendersky939a4e82013-01-25 20:26:43 +000070 if (DumpType == DIDT_All || DumpType == DIDT_Str) {
71 OS << "\n.debug_str contents:\n";
72 DataExtractor strData(getStringSection(), isLittleEndian(), 0);
73 offset = 0;
74 uint32_t strOffset = 0;
75 while (const char *s = strData.getCStr(&offset)) {
76 OS << format("0x%8.8x: \"%s\"\n", strOffset, s);
77 strOffset = offset;
78 }
Benjamin Kramer34f864f2011-09-15 16:57:13 +000079 }
Alexey Samsonoveceb5b92012-08-27 07:17:47 +000080
Eli Bendersky939a4e82013-01-25 20:26:43 +000081 if (DumpType == DIDT_All || DumpType == DIDT_Ranges) {
82 OS << "\n.debug_ranges contents:\n";
83 // In fact, different compile units may have different address byte
84 // sizes, but for simplicity we just use the address byte size of the last
85 // compile unit (there is no easy and fast way to associate address range
86 // list and the compile unit it describes).
87 DataExtractor rangesData(getRangeSection(), isLittleEndian(),
88 savedAddressByteSize);
89 offset = 0;
90 DWARFDebugRangeList rangeList;
91 while (rangeList.extract(rangesData, &offset))
92 rangeList.dump(OS);
Eric Christopher82de10a2013-01-02 23:52:13 +000093 }
Eric Christopher72f7bfb2013-01-15 23:56:56 +000094
Krzysztof Parzyszeke38825f2013-02-12 16:20:28 +000095 if (DumpType == DIDT_All || DumpType == DIDT_Pubnames) {
96 OS << "\n.debug_pubnames contents:\n";
97 DataExtractor pubNames(getPubNamesSection(), isLittleEndian(), 0);
98 offset = 0;
99 OS << "Length: " << pubNames.getU32(&offset) << "\n";
100 OS << "Version: " << pubNames.getU16(&offset) << "\n";
101 OS << "Offset in .debug_info: " << pubNames.getU32(&offset) << "\n";
102 OS << "Size: " << pubNames.getU32(&offset) << "\n";
103 OS << "\n Offset Name\n";
104 while (offset < getPubNamesSection().size()) {
105 uint32_t n = pubNames.getU32(&offset);
106 if (n == 0)
107 break;
108 OS << format("%8x ", n);
109 OS << pubNames.getCStr(&offset) << "\n";
110 }
111 }
112
Eli Bendersky939a4e82013-01-25 20:26:43 +0000113 if (DumpType == DIDT_All || DumpType == DIDT_AbbrevDwo) {
Eric Christopher93f3fed2013-05-06 17:50:42 +0000114 const DWARFDebugAbbrev *D = getDebugAbbrevDWO();
115 if (D) {
116 OS << "\n.debug_abbrev.dwo contents:\n";
117 getDebugAbbrevDWO()->dump(OS);
Eli Bendersky939a4e82013-01-25 20:26:43 +0000118 }
119 }
120
Eric Christopher93f3fed2013-05-06 17:50:42 +0000121 if (DumpType == DIDT_All || DumpType == DIDT_InfoDwo)
122 if (getNumDWOCompileUnits()) {
123 OS << "\n.debug_info.dwo contents:\n";
124 for (unsigned i = 0, e = getNumDWOCompileUnits(); i != e; ++i)
Eric Christopher5a0c3662013-05-06 21:19:41 +0000125 getDWOCompileUnitAtIndex(i)->dump(OS);
Eli Bendersky939a4e82013-01-25 20:26:43 +0000126 }
Eric Christopher93f3fed2013-05-06 17:50:42 +0000127
128 if (DumpType == DIDT_All || DumpType == DIDT_StrDwo)
129 if (!getStringDWOSection().empty()) {
130 OS << "\n.debug_str.dwo contents:\n";
131 DataExtractor strDWOData(getStringDWOSection(), isLittleEndian(), 0);
132 offset = 0;
133 uint32_t strDWOOffset = 0;
134 while (const char *s = strDWOData.getCStr(&offset)) {
Eric Christopher5a0c3662013-05-06 21:19:41 +0000135 OS << format("0x%8.8x: \"%s\"\n", strDWOOffset, s);
136 strDWOOffset = offset;
Eric Christopher93f3fed2013-05-06 17:50:42 +0000137 }
138 }
139
140 if (DumpType == DIDT_All || DumpType == DIDT_StrOffsetsDwo)
141 if (!getStringOffsetDWOSection().empty()) {
142 OS << "\n.debug_str_offsets.dwo contents:\n";
143 DataExtractor strOffsetExt(getStringOffsetDWOSection(), isLittleEndian(), 0);
144 offset = 0;
Eric Christophere305e032013-05-06 21:19:44 +0000145 uint64_t size = getStringOffsetDWOSection().size();
146 while (offset < size) {
Eric Christopher5a0c3662013-05-06 21:19:41 +0000147 OS << format("0x%8.8x: ", offset);
148 OS << format("%8.8x\n", strOffsetExt.getU32(&offset));
Eric Christopher93f3fed2013-05-06 17:50:42 +0000149 }
150 }
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000151}
152
153const DWARFDebugAbbrev *DWARFContext::getDebugAbbrev() {
154 if (Abbrev)
155 return Abbrev.get();
156
157 DataExtractor abbrData(getAbbrevSection(), isLittleEndian(), 0);
158
159 Abbrev.reset(new DWARFDebugAbbrev());
160 Abbrev->parse(abbrData);
161 return Abbrev.get();
162}
163
Eric Christopher82de10a2013-01-02 23:52:13 +0000164const DWARFDebugAbbrev *DWARFContext::getDebugAbbrevDWO() {
165 if (AbbrevDWO)
166 return AbbrevDWO.get();
167
168 DataExtractor abbrData(getAbbrevDWOSection(), isLittleEndian(), 0);
169 AbbrevDWO.reset(new DWARFDebugAbbrev());
170 AbbrevDWO->parse(abbrData);
171 return AbbrevDWO.get();
172}
173
Benjamin Kramer358f4fd2011-09-14 01:09:52 +0000174const DWARFDebugAranges *DWARFContext::getDebugAranges() {
175 if (Aranges)
176 return Aranges.get();
177
178 DataExtractor arangesData(getARangeSection(), isLittleEndian(), 0);
179
180 Aranges.reset(new DWARFDebugAranges());
181 Aranges->extract(arangesData);
Alexey Samsonov63a450a2012-11-16 08:36:25 +0000182 // Generate aranges from DIEs: even if .debug_aranges section is present,
183 // it may describe only a small subset of compilation units, so we need to
184 // manually build aranges for the rest of them.
185 Aranges->generate(this);
Benjamin Kramer358f4fd2011-09-14 01:09:52 +0000186 return Aranges.get();
187}
188
Eli Bendersky60bdc5b2013-02-05 23:30:58 +0000189const DWARFDebugFrame *DWARFContext::getDebugFrame() {
190 if (DebugFrame)
191 return DebugFrame.get();
192
193 // There's a "bug" in the DWARFv3 standard with respect to the target address
194 // size within debug frame sections. While DWARF is supposed to be independent
195 // of its container, FDEs have fields with size being "target address size",
196 // which isn't specified in DWARF in general. It's only specified for CUs, but
197 // .eh_frame can appear without a .debug_info section. Follow the example of
198 // other tools (libdwarf) and extract this from the container (ObjectFile
199 // provides this information). This problem is fixed in DWARFv4
200 // See this dwarf-discuss discussion for more details:
201 // http://lists.dwarfstd.org/htdig.cgi/dwarf-discuss-dwarfstd.org/2011-December/001173.html
202 DataExtractor debugFrameData(getDebugFrameSection(), isLittleEndian(),
203 getAddressSize());
204 DebugFrame.reset(new DWARFDebugFrame());
205 DebugFrame->parse(debugFrameData);
206 return DebugFrame.get();
207}
208
Eric Christophere9403c12012-10-16 23:46:25 +0000209const DWARFLineTable *
Benjamin Kramerc26ed9b2011-09-15 20:43:18 +0000210DWARFContext::getLineTableForCompileUnit(DWARFCompileUnit *cu) {
211 if (!Line)
Andrew Kayloree7c0d22013-01-25 22:50:58 +0000212 Line.reset(new DWARFDebugLine(&lineRelocMap()));
Benjamin Kramerb848e972011-09-15 02:12:05 +0000213
Benjamin Kramerc26ed9b2011-09-15 20:43:18 +0000214 unsigned stmtOffset =
215 cu->getCompileUnitDIE()->getAttributeValueAsUnsigned(cu, DW_AT_stmt_list,
216 -1U);
217 if (stmtOffset == -1U)
218 return 0; // No line table for this compile unit.
Benjamin Kramerb848e972011-09-15 02:12:05 +0000219
Benjamin Kramerc26ed9b2011-09-15 20:43:18 +0000220 // See if the line table is cached.
Eric Christophere9403c12012-10-16 23:46:25 +0000221 if (const DWARFLineTable *lt = Line->getLineTable(stmtOffset))
Benjamin Kramerc26ed9b2011-09-15 20:43:18 +0000222 return lt;
223
224 // We have to parse it first.
225 DataExtractor lineData(getLineSection(), isLittleEndian(),
226 cu->getAddressByteSize());
227 return Line->getOrParseLineTable(lineData, stmtOffset);
Benjamin Kramerb848e972011-09-15 02:12:05 +0000228}
229
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000230void DWARFContext::parseCompileUnits() {
231 uint32_t offset = 0;
Eric Christopherb69b55f2012-10-16 23:46:23 +0000232 const DataExtractor &DIData = DataExtractor(getInfoSection(),
233 isLittleEndian(), 0);
234 while (DIData.isValidOffset(offset)) {
Eric Christopher82de10a2013-01-02 23:52:13 +0000235 CUs.push_back(DWARFCompileUnit(getDebugAbbrev(), getInfoSection(),
236 getAbbrevSection(), getRangeSection(),
Eric Christopher72f7bfb2013-01-15 23:56:56 +0000237 getStringSection(), StringRef(),
238 getAddrSection(),
Eric Christopherdd8e9f32013-01-07 19:32:41 +0000239 &infoRelocMap(),
Eric Christopher82de10a2013-01-02 23:52:13 +0000240 isLittleEndian()));
Eric Christopherb69b55f2012-10-16 23:46:23 +0000241 if (!CUs.back().extract(DIData, &offset)) {
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000242 CUs.pop_back();
243 break;
244 }
245
246 offset = CUs.back().getNextCompileUnitOffset();
247 }
248}
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000249
Eric Christopher82de10a2013-01-02 23:52:13 +0000250void DWARFContext::parseDWOCompileUnits() {
251 uint32_t offset = 0;
252 const DataExtractor &DIData = DataExtractor(getInfoDWOSection(),
253 isLittleEndian(), 0);
254 while (DIData.isValidOffset(offset)) {
255 DWOCUs.push_back(DWARFCompileUnit(getDebugAbbrevDWO(), getInfoDWOSection(),
256 getAbbrevDWOSection(),
257 getRangeDWOSection(),
258 getStringDWOSection(),
Eric Christopherdd8e9f32013-01-07 19:32:41 +0000259 getStringOffsetDWOSection(),
Eric Christopher72f7bfb2013-01-15 23:56:56 +0000260 getAddrSection(),
Eric Christopher82de10a2013-01-02 23:52:13 +0000261 &infoDWORelocMap(),
262 isLittleEndian()));
263 if (!DWOCUs.back().extract(DIData, &offset)) {
264 DWOCUs.pop_back();
265 break;
266 }
267
268 offset = DWOCUs.back().getNextCompileUnitOffset();
269 }
270}
271
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000272namespace {
273 struct OffsetComparator {
274 bool operator()(const DWARFCompileUnit &LHS,
275 const DWARFCompileUnit &RHS) const {
276 return LHS.getOffset() < RHS.getOffset();
277 }
278 bool operator()(const DWARFCompileUnit &LHS, uint32_t RHS) const {
279 return LHS.getOffset() < RHS;
280 }
281 bool operator()(uint32_t LHS, const DWARFCompileUnit &RHS) const {
282 return LHS < RHS.getOffset();
283 }
284 };
285}
286
Alexey Samsonov38a63812012-08-30 07:49:50 +0000287DWARFCompileUnit *DWARFContext::getCompileUnitForOffset(uint32_t Offset) {
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000288 if (CUs.empty())
289 parseCompileUnits();
290
Alexey Samsonov38a63812012-08-30 07:49:50 +0000291 DWARFCompileUnit *CU = std::lower_bound(CUs.begin(), CUs.end(), Offset,
292 OffsetComparator());
293 if (CU != CUs.end())
294 return &*CU;
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000295 return 0;
296}
297
Alexey Samsonov38a63812012-08-30 07:49:50 +0000298DWARFCompileUnit *DWARFContext::getCompileUnitForAddress(uint64_t Address) {
Benjamin Kramer9013db32011-09-15 21:59:13 +0000299 // First, get the offset of the compile unit.
Alexey Samsonov38a63812012-08-30 07:49:50 +0000300 uint32_t CUOffset = getDebugAranges()->findAddress(Address);
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000301 // Retrieve the compile unit.
Alexey Samsonov38a63812012-08-30 07:49:50 +0000302 return getCompileUnitForOffset(CUOffset);
303}
304
Eric Christophere9403c12012-10-16 23:46:25 +0000305static bool getFileNameForCompileUnit(DWARFCompileUnit *CU,
306 const DWARFLineTable *LineTable,
307 uint64_t FileIndex,
308 bool NeedsAbsoluteFilePath,
309 std::string &FileName) {
Alexey Samsonov38a63812012-08-30 07:49:50 +0000310 if (CU == 0 ||
311 LineTable == 0 ||
312 !LineTable->getFileNameByIndex(FileIndex, NeedsAbsoluteFilePath,
313 FileName))
314 return false;
315 if (NeedsAbsoluteFilePath && sys::path::is_relative(FileName)) {
316 // We may still need to append compilation directory of compile unit.
317 SmallString<16> AbsolutePath;
318 if (const char *CompilationDir = CU->getCompilationDir()) {
319 sys::path::append(AbsolutePath, CompilationDir);
320 }
321 sys::path::append(AbsolutePath, FileName);
322 FileName = AbsolutePath.str();
323 }
324 return true;
325}
326
Eric Christophere9403c12012-10-16 23:46:25 +0000327static bool getFileLineInfoForCompileUnit(DWARFCompileUnit *CU,
328 const DWARFLineTable *LineTable,
329 uint64_t Address,
330 bool NeedsAbsoluteFilePath,
331 std::string &FileName,
332 uint32_t &Line, uint32_t &Column) {
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000333 if (CU == 0 || LineTable == 0)
Alexey Samsonov38a63812012-08-30 07:49:50 +0000334 return false;
335 // Get the index of row we're looking for in the line table.
336 uint32_t RowIndex = LineTable->lookupAddress(Address);
337 if (RowIndex == -1U)
338 return false;
339 // Take file number and line/column from the row.
340 const DWARFDebugLine::Row &Row = LineTable->Rows[RowIndex];
341 if (!getFileNameForCompileUnit(CU, LineTable, Row.File,
342 NeedsAbsoluteFilePath, FileName))
343 return false;
344 Line = Row.Line;
345 Column = Row.Column;
346 return true;
347}
348
349DILineInfo DWARFContext::getLineInfoForAddress(uint64_t Address,
350 DILineInfoSpecifier Specifier) {
351 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
352 if (!CU)
Alexey Samsonov3e25c4a2012-07-02 05:54:45 +0000353 return DILineInfo();
Alexey Samsonov38a63812012-08-30 07:49:50 +0000354 std::string FileName = "<invalid>";
355 std::string FunctionName = "<invalid>";
356 uint32_t Line = 0;
357 uint32_t Column = 0;
358 if (Specifier.needs(DILineInfoSpecifier::FunctionName)) {
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000359 // The address may correspond to instruction in some inlined function,
360 // so we have to build the chain of inlined functions and take the
361 // name of the topmost function in it.
362 const DWARFDebugInfoEntryMinimal::InlinedChain &InlinedChain =
363 CU->getInlinedChainForAddress(Address);
364 if (InlinedChain.size() > 0) {
365 const DWARFDebugInfoEntryMinimal &TopFunctionDIE = InlinedChain[0];
366 if (const char *Name = TopFunctionDIE.getSubroutineName(CU))
Alexey Samsonov38a63812012-08-30 07:49:50 +0000367 FunctionName = Name;
Alexey Samsonov9d26b0b2012-07-17 15:28:35 +0000368 }
Alexey Samsonov3e25c4a2012-07-02 05:54:45 +0000369 }
Alexey Samsonov38a63812012-08-30 07:49:50 +0000370 if (Specifier.needs(DILineInfoSpecifier::FileLineInfo)) {
Eric Christophere9403c12012-10-16 23:46:25 +0000371 const DWARFLineTable *LineTable = getLineTableForCompileUnit(CU);
Alexey Samsonov38a63812012-08-30 07:49:50 +0000372 const bool NeedsAbsoluteFilePath =
373 Specifier.needs(DILineInfoSpecifier::AbsoluteFilePath);
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000374 getFileLineInfoForCompileUnit(CU, LineTable, Address,
375 NeedsAbsoluteFilePath,
Alexey Samsonov38a63812012-08-30 07:49:50 +0000376 FileName, Line, Column);
Alexey Samsonov3e25c4a2012-07-02 05:54:45 +0000377 }
Alexey Samsonov38a63812012-08-30 07:49:50 +0000378 return DILineInfo(StringRef(FileName), StringRef(FunctionName),
379 Line, Column);
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000380}
David Blaikie2d24e2a2011-12-20 02:50:00 +0000381
Andrew Kaylore27a7872013-01-26 00:28:05 +0000382DILineInfoTable DWARFContext::getLineInfoForAddressRange(uint64_t Address,
383 uint64_t Size,
384 DILineInfoSpecifier Specifier) {
385 DILineInfoTable Lines;
386 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
387 if (!CU)
388 return Lines;
389
390 std::string FunctionName = "<invalid>";
391 if (Specifier.needs(DILineInfoSpecifier::FunctionName)) {
392 // The address may correspond to instruction in some inlined function,
393 // so we have to build the chain of inlined functions and take the
394 // name of the topmost function in it.
395 const DWARFDebugInfoEntryMinimal::InlinedChain &InlinedChain =
396 CU->getInlinedChainForAddress(Address);
397 if (InlinedChain.size() > 0) {
398 const DWARFDebugInfoEntryMinimal &TopFunctionDIE = InlinedChain[0];
399 if (const char *Name = TopFunctionDIE.getSubroutineName(CU))
400 FunctionName = Name;
401 }
402 }
403
404 StringRef FuncNameRef = StringRef(FunctionName);
405
406 // If the Specifier says we don't need FileLineInfo, just
407 // return the top-most function at the starting address.
408 if (!Specifier.needs(DILineInfoSpecifier::FileLineInfo)) {
409 Lines.push_back(std::make_pair(Address,
410 DILineInfo(StringRef("<invalid>"),
411 FuncNameRef, 0, 0)));
412 return Lines;
413 }
414
415 const DWARFLineTable *LineTable = getLineTableForCompileUnit(CU);
416 const bool NeedsAbsoluteFilePath =
417 Specifier.needs(DILineInfoSpecifier::AbsoluteFilePath);
418
419 // Get the index of row we're looking for in the line table.
420 std::vector<uint32_t> RowVector;
421 if (!LineTable->lookupAddressRange(Address, Size, RowVector))
422 return Lines;
423
424 uint32_t NumRows = RowVector.size();
425 for (uint32_t i = 0; i < NumRows; ++i) {
426 uint32_t RowIndex = RowVector[i];
427 // Take file number and line/column from the row.
428 const DWARFDebugLine::Row &Row = LineTable->Rows[RowIndex];
429 std::string FileName = "<invalid>";
430 getFileNameForCompileUnit(CU, LineTable, Row.File,
431 NeedsAbsoluteFilePath, FileName);
432 Lines.push_back(std::make_pair(Row.Address,
433 DILineInfo(StringRef(FileName),
434 FuncNameRef, Row.Line, Row.Column)));
435 }
436
437 return Lines;
438}
439
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000440DIInliningInfo DWARFContext::getInliningInfoForAddress(uint64_t Address,
441 DILineInfoSpecifier Specifier) {
442 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
443 if (!CU)
444 return DIInliningInfo();
445
446 const DWARFDebugInfoEntryMinimal::InlinedChain &InlinedChain =
447 CU->getInlinedChainForAddress(Address);
448 if (InlinedChain.size() == 0)
449 return DIInliningInfo();
450
451 DIInliningInfo InliningInfo;
452 uint32_t CallFile = 0, CallLine = 0, CallColumn = 0;
Eric Christophere9403c12012-10-16 23:46:25 +0000453 const DWARFLineTable *LineTable = 0;
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000454 for (uint32_t i = 0, n = InlinedChain.size(); i != n; i++) {
455 const DWARFDebugInfoEntryMinimal &FunctionDIE = InlinedChain[i];
456 std::string FileName = "<invalid>";
457 std::string FunctionName = "<invalid>";
458 uint32_t Line = 0;
459 uint32_t Column = 0;
460 // Get function name if necessary.
461 if (Specifier.needs(DILineInfoSpecifier::FunctionName)) {
462 if (const char *Name = FunctionDIE.getSubroutineName(CU))
463 FunctionName = Name;
464 }
465 if (Specifier.needs(DILineInfoSpecifier::FileLineInfo)) {
466 const bool NeedsAbsoluteFilePath =
467 Specifier.needs(DILineInfoSpecifier::AbsoluteFilePath);
468 if (i == 0) {
469 // For the topmost frame, initialize the line table of this
470 // compile unit and fetch file/line info from it.
471 LineTable = getLineTableForCompileUnit(CU);
472 // For the topmost routine, get file/line info from line table.
473 getFileLineInfoForCompileUnit(CU, LineTable, Address,
474 NeedsAbsoluteFilePath,
475 FileName, Line, Column);
476 } else {
477 // Otherwise, use call file, call line and call column from
478 // previous DIE in inlined chain.
479 getFileNameForCompileUnit(CU, LineTable, CallFile,
480 NeedsAbsoluteFilePath, FileName);
481 Line = CallLine;
482 Column = CallColumn;
483 }
484 // Get call file/line/column of a current DIE.
485 if (i + 1 < n) {
486 FunctionDIE.getCallerFrame(CU, CallFile, CallLine, CallColumn);
487 }
488 }
489 DILineInfo Frame(StringRef(FileName), StringRef(FunctionName),
490 Line, Column);
491 InliningInfo.addFrame(Frame);
492 }
493 return InliningInfo;
494}
495
Alexey Samsonov005159e2013-04-23 10:17:34 +0000496static bool consumeCompressedDebugSectionHeader(StringRef &data,
497 uint64_t &OriginalSize) {
498 // Consume "ZLIB" prefix.
499 if (!data.startswith("ZLIB"))
500 return false;
501 data = data.substr(4);
502 // Consume uncompressed section size (big-endian 8 bytes).
503 DataExtractor extractor(data, false, 8);
504 uint32_t Offset = 0;
505 OriginalSize = extractor.getU64(&Offset);
506 if (Offset == 0)
507 return false;
508 data = data.substr(Offset);
509 return true;
510}
511
Eric Christopherd1726a42012-11-12 21:40:38 +0000512DWARFContextInMemory::DWARFContextInMemory(object::ObjectFile *Obj) :
Eli Bendersky60bdc5b2013-02-05 23:30:58 +0000513 IsLittleEndian(Obj->isLittleEndian()),
514 AddressSize(Obj->getBytesInAddress()) {
Eric Christopherd1726a42012-11-12 21:40:38 +0000515 error_code ec;
516 for (object::section_iterator i = Obj->begin_sections(),
517 e = Obj->end_sections();
518 i != e; i.increment(ec)) {
519 StringRef name;
520 i->getName(name);
521 StringRef data;
522 i->getContents(data);
523
Eric Christopherd1726a42012-11-12 21:40:38 +0000524 name = name.substr(name.find_first_not_of("._")); // Skip . and _ prefixes.
Alexey Samsonov784baa62013-04-17 14:27:04 +0000525
Alexey Samsonov005159e2013-04-23 10:17:34 +0000526 // Check if debug info section is compressed with zlib.
527 if (name.startswith("zdebug_")) {
528 uint64_t OriginalSize;
529 if (!zlib::isAvailable() ||
530 !consumeCompressedDebugSectionHeader(data, OriginalSize))
531 continue;
532 OwningPtr<MemoryBuffer> UncompressedSection;
533 if (zlib::uncompress(data, UncompressedSection, OriginalSize) !=
534 zlib::StatusOK)
535 continue;
536 // Make data point to uncompressed section contents and save its contents.
537 name = name.substr(1);
538 data = UncompressedSection->getBuffer();
539 UncompressedSections.push_back(UncompressedSection.take());
540 }
541
Alexey Samsonov784baa62013-04-17 14:27:04 +0000542 StringRef *Section = StringSwitch<StringRef*>(name)
543 .Case("debug_info", &InfoSection)
544 .Case("debug_abbrev", &AbbrevSection)
545 .Case("debug_line", &LineSection)
546 .Case("debug_aranges", &ARangeSection)
547 .Case("debug_frame", &DebugFrameSection)
548 .Case("debug_str", &StringSection)
549 .Case("debug_ranges", &RangeSection)
550 .Case("debug_pubnames", &PubNamesSection)
551 .Case("debug_info.dwo", &InfoDWOSection)
552 .Case("debug_abbrev.dwo", &AbbrevDWOSection)
553 .Case("debug_str.dwo", &StringDWOSection)
554 .Case("debug_str_offsets.dwo", &StringOffsetDWOSection)
555 .Case("debug_addr", &AddrSection)
556 // Any more debug info sections go here.
557 .Default(0);
Rafael Espindola7486d922013-05-30 03:05:14 +0000558 if (Section) {
559 *Section = data;
560 if (name == "debug_ranges") {
561 // FIXME: Use the other dwo range section when we emit it.
562 RangeDWOSection = data;
563 }
Eric Christopher82de10a2013-01-02 23:52:13 +0000564 }
Eric Christopherd1726a42012-11-12 21:40:38 +0000565
Rafael Espindola7486d922013-05-30 03:05:14 +0000566 section_iterator RelocatedSection = i->getRelocatedSection();
567 if (RelocatedSection == Obj->end_sections())
568 continue;
569
570 StringRef RelSecName;
571 RelocatedSection->getName(RelSecName);
572 RelSecName = RelSecName.substr(
573 RelSecName.find_first_not_of("._")); // Skip . and _ prefixes.
574
Andrew Kayloree7c0d22013-01-25 22:50:58 +0000575 // TODO: Add support for relocations in other sections as needed.
576 // Record relocations for the debug_info and debug_line sections.
Rafael Espindola7486d922013-05-30 03:05:14 +0000577 RelocAddrMap *Map = StringSwitch<RelocAddrMap*>(RelSecName)
Alexey Samsonov784baa62013-04-17 14:27:04 +0000578 .Case("debug_info", &InfoRelocMap)
579 .Case("debug_info.dwo", &InfoDWORelocMap)
580 .Case("debug_line", &LineRelocMap)
581 .Default(0);
582 if (!Map)
Eric Christopherd1726a42012-11-12 21:40:38 +0000583 continue;
584
585 if (i->begin_relocations() != i->end_relocations()) {
586 uint64_t SectionSize;
Rafael Espindola7486d922013-05-30 03:05:14 +0000587 RelocatedSection->getSize(SectionSize);
Eric Christopherd1726a42012-11-12 21:40:38 +0000588 for (object::relocation_iterator reloc_i = i->begin_relocations(),
589 reloc_e = i->end_relocations();
590 reloc_i != reloc_e; reloc_i.increment(ec)) {
591 uint64_t Address;
Rafael Espindola956ca722013-04-25 12:28:45 +0000592 reloc_i->getOffset(Address);
Eric Christopherd1726a42012-11-12 21:40:38 +0000593 uint64_t Type;
594 reloc_i->getType(Type);
Andrew Kayloree7c0d22013-01-25 22:50:58 +0000595 uint64_t SymAddr = 0;
596 // ELF relocations may need the symbol address
597 if (Obj->isELF()) {
Rafael Espindola6c1202c2013-06-05 01:33:53 +0000598 object::symbol_iterator Sym = reloc_i->getSymbol();
599 Sym->getAddress(SymAddr);
Andrew Kayloree7c0d22013-01-25 22:50:58 +0000600 }
Eric Christopherd1726a42012-11-12 21:40:38 +0000601
602 object::RelocVisitor V(Obj->getFileFormatName());
603 // The section address is always 0 for debug sections.
Andrew Kayloree7c0d22013-01-25 22:50:58 +0000604 object::RelocToApply R(V.visit(Type, *reloc_i, 0, SymAddr));
Eric Christopherd1726a42012-11-12 21:40:38 +0000605 if (V.error()) {
606 SmallString<32> Name;
607 error_code ec(reloc_i->getTypeName(Name));
608 if (ec) {
609 errs() << "Aaaaaa! Nameless relocation! Aaaaaa!\n";
610 }
611 errs() << "error: failed to compute relocation: "
612 << Name << "\n";
613 continue;
614 }
615
616 if (Address + R.Width > SectionSize) {
617 errs() << "error: " << R.Width << "-byte relocation starting "
618 << Address << " bytes into section " << name << " which is "
619 << SectionSize << " bytes long.\n";
620 continue;
621 }
622 if (R.Width > 8) {
623 errs() << "error: can't handle a relocation of more than 8 bytes at "
624 "a time.\n";
625 continue;
626 }
627 DEBUG(dbgs() << "Writing " << format("%p", R.Value)
628 << " at " << format("%p", Address)
629 << " with width " << format("%d", R.Width)
630 << "\n");
Eric Christopher82de10a2013-01-02 23:52:13 +0000631 Map->insert(std::make_pair(Address, std::make_pair(R.Width, R.Value)));
Eric Christopherd1726a42012-11-12 21:40:38 +0000632 }
633 }
634 }
635}
636
Alexey Samsonov005159e2013-04-23 10:17:34 +0000637DWARFContextInMemory::~DWARFContextInMemory() {
638 DeleteContainerPointers(UncompressedSections);
639}
640
David Blaikie2d24e2a2011-12-20 02:50:00 +0000641void DWARFContextInMemory::anchor() { }