blob: 4ce8f7a4d796a35b0027c7dadc6f22960747eeb6 [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)
124 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)) {
134 OS << format("0x%8.8x: \"%s\"\n", strDWOOffset, s);
135 strDWOOffset = offset;
136 }
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;
144 while (offset < getStringOffsetDWOSection().size()) {
145 OS << format("0x%8.8x: ", offset);
146 OS << format("%8.8x\n", strOffsetExt.getU32(&offset));
147 }
148 }
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000149}
150
151const DWARFDebugAbbrev *DWARFContext::getDebugAbbrev() {
152 if (Abbrev)
153 return Abbrev.get();
154
155 DataExtractor abbrData(getAbbrevSection(), isLittleEndian(), 0);
156
157 Abbrev.reset(new DWARFDebugAbbrev());
158 Abbrev->parse(abbrData);
159 return Abbrev.get();
160}
161
Eric Christopher82de10a2013-01-02 23:52:13 +0000162const DWARFDebugAbbrev *DWARFContext::getDebugAbbrevDWO() {
163 if (AbbrevDWO)
164 return AbbrevDWO.get();
165
166 DataExtractor abbrData(getAbbrevDWOSection(), isLittleEndian(), 0);
167 AbbrevDWO.reset(new DWARFDebugAbbrev());
168 AbbrevDWO->parse(abbrData);
169 return AbbrevDWO.get();
170}
171
Benjamin Kramer358f4fd2011-09-14 01:09:52 +0000172const DWARFDebugAranges *DWARFContext::getDebugAranges() {
173 if (Aranges)
174 return Aranges.get();
175
176 DataExtractor arangesData(getARangeSection(), isLittleEndian(), 0);
177
178 Aranges.reset(new DWARFDebugAranges());
179 Aranges->extract(arangesData);
Alexey Samsonov63a450a2012-11-16 08:36:25 +0000180 // Generate aranges from DIEs: even if .debug_aranges section is present,
181 // it may describe only a small subset of compilation units, so we need to
182 // manually build aranges for the rest of them.
183 Aranges->generate(this);
Benjamin Kramer358f4fd2011-09-14 01:09:52 +0000184 return Aranges.get();
185}
186
Eli Bendersky60bdc5b2013-02-05 23:30:58 +0000187const DWARFDebugFrame *DWARFContext::getDebugFrame() {
188 if (DebugFrame)
189 return DebugFrame.get();
190
191 // There's a "bug" in the DWARFv3 standard with respect to the target address
192 // size within debug frame sections. While DWARF is supposed to be independent
193 // of its container, FDEs have fields with size being "target address size",
194 // which isn't specified in DWARF in general. It's only specified for CUs, but
195 // .eh_frame can appear without a .debug_info section. Follow the example of
196 // other tools (libdwarf) and extract this from the container (ObjectFile
197 // provides this information). This problem is fixed in DWARFv4
198 // See this dwarf-discuss discussion for more details:
199 // http://lists.dwarfstd.org/htdig.cgi/dwarf-discuss-dwarfstd.org/2011-December/001173.html
200 DataExtractor debugFrameData(getDebugFrameSection(), isLittleEndian(),
201 getAddressSize());
202 DebugFrame.reset(new DWARFDebugFrame());
203 DebugFrame->parse(debugFrameData);
204 return DebugFrame.get();
205}
206
Eric Christophere9403c12012-10-16 23:46:25 +0000207const DWARFLineTable *
Benjamin Kramerc26ed9b2011-09-15 20:43:18 +0000208DWARFContext::getLineTableForCompileUnit(DWARFCompileUnit *cu) {
209 if (!Line)
Andrew Kayloree7c0d22013-01-25 22:50:58 +0000210 Line.reset(new DWARFDebugLine(&lineRelocMap()));
Benjamin Kramerb848e972011-09-15 02:12:05 +0000211
Benjamin Kramerc26ed9b2011-09-15 20:43:18 +0000212 unsigned stmtOffset =
213 cu->getCompileUnitDIE()->getAttributeValueAsUnsigned(cu, DW_AT_stmt_list,
214 -1U);
215 if (stmtOffset == -1U)
216 return 0; // No line table for this compile unit.
Benjamin Kramerb848e972011-09-15 02:12:05 +0000217
Benjamin Kramerc26ed9b2011-09-15 20:43:18 +0000218 // See if the line table is cached.
Eric Christophere9403c12012-10-16 23:46:25 +0000219 if (const DWARFLineTable *lt = Line->getLineTable(stmtOffset))
Benjamin Kramerc26ed9b2011-09-15 20:43:18 +0000220 return lt;
221
222 // We have to parse it first.
223 DataExtractor lineData(getLineSection(), isLittleEndian(),
224 cu->getAddressByteSize());
225 return Line->getOrParseLineTable(lineData, stmtOffset);
Benjamin Kramerb848e972011-09-15 02:12:05 +0000226}
227
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000228void DWARFContext::parseCompileUnits() {
229 uint32_t offset = 0;
Eric Christopherb69b55f2012-10-16 23:46:23 +0000230 const DataExtractor &DIData = DataExtractor(getInfoSection(),
231 isLittleEndian(), 0);
232 while (DIData.isValidOffset(offset)) {
Eric Christopher82de10a2013-01-02 23:52:13 +0000233 CUs.push_back(DWARFCompileUnit(getDebugAbbrev(), getInfoSection(),
234 getAbbrevSection(), getRangeSection(),
Eric Christopher72f7bfb2013-01-15 23:56:56 +0000235 getStringSection(), StringRef(),
236 getAddrSection(),
Eric Christopherdd8e9f32013-01-07 19:32:41 +0000237 &infoRelocMap(),
Eric Christopher82de10a2013-01-02 23:52:13 +0000238 isLittleEndian()));
Eric Christopherb69b55f2012-10-16 23:46:23 +0000239 if (!CUs.back().extract(DIData, &offset)) {
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000240 CUs.pop_back();
241 break;
242 }
243
244 offset = CUs.back().getNextCompileUnitOffset();
245 }
246}
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000247
Eric Christopher82de10a2013-01-02 23:52:13 +0000248void DWARFContext::parseDWOCompileUnits() {
249 uint32_t offset = 0;
250 const DataExtractor &DIData = DataExtractor(getInfoDWOSection(),
251 isLittleEndian(), 0);
252 while (DIData.isValidOffset(offset)) {
253 DWOCUs.push_back(DWARFCompileUnit(getDebugAbbrevDWO(), getInfoDWOSection(),
254 getAbbrevDWOSection(),
255 getRangeDWOSection(),
256 getStringDWOSection(),
Eric Christopherdd8e9f32013-01-07 19:32:41 +0000257 getStringOffsetDWOSection(),
Eric Christopher72f7bfb2013-01-15 23:56:56 +0000258 getAddrSection(),
Eric Christopher82de10a2013-01-02 23:52:13 +0000259 &infoDWORelocMap(),
260 isLittleEndian()));
261 if (!DWOCUs.back().extract(DIData, &offset)) {
262 DWOCUs.pop_back();
263 break;
264 }
265
266 offset = DWOCUs.back().getNextCompileUnitOffset();
267 }
268}
269
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000270namespace {
271 struct OffsetComparator {
272 bool operator()(const DWARFCompileUnit &LHS,
273 const DWARFCompileUnit &RHS) const {
274 return LHS.getOffset() < RHS.getOffset();
275 }
276 bool operator()(const DWARFCompileUnit &LHS, uint32_t RHS) const {
277 return LHS.getOffset() < RHS;
278 }
279 bool operator()(uint32_t LHS, const DWARFCompileUnit &RHS) const {
280 return LHS < RHS.getOffset();
281 }
282 };
283}
284
Alexey Samsonov38a63812012-08-30 07:49:50 +0000285DWARFCompileUnit *DWARFContext::getCompileUnitForOffset(uint32_t Offset) {
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000286 if (CUs.empty())
287 parseCompileUnits();
288
Alexey Samsonov38a63812012-08-30 07:49:50 +0000289 DWARFCompileUnit *CU = std::lower_bound(CUs.begin(), CUs.end(), Offset,
290 OffsetComparator());
291 if (CU != CUs.end())
292 return &*CU;
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000293 return 0;
294}
295
Alexey Samsonov38a63812012-08-30 07:49:50 +0000296DWARFCompileUnit *DWARFContext::getCompileUnitForAddress(uint64_t Address) {
Benjamin Kramer9013db32011-09-15 21:59:13 +0000297 // First, get the offset of the compile unit.
Alexey Samsonov38a63812012-08-30 07:49:50 +0000298 uint32_t CUOffset = getDebugAranges()->findAddress(Address);
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000299 // Retrieve the compile unit.
Alexey Samsonov38a63812012-08-30 07:49:50 +0000300 return getCompileUnitForOffset(CUOffset);
301}
302
Eric Christophere9403c12012-10-16 23:46:25 +0000303static bool getFileNameForCompileUnit(DWARFCompileUnit *CU,
304 const DWARFLineTable *LineTable,
305 uint64_t FileIndex,
306 bool NeedsAbsoluteFilePath,
307 std::string &FileName) {
Alexey Samsonov38a63812012-08-30 07:49:50 +0000308 if (CU == 0 ||
309 LineTable == 0 ||
310 !LineTable->getFileNameByIndex(FileIndex, NeedsAbsoluteFilePath,
311 FileName))
312 return false;
313 if (NeedsAbsoluteFilePath && sys::path::is_relative(FileName)) {
314 // We may still need to append compilation directory of compile unit.
315 SmallString<16> AbsolutePath;
316 if (const char *CompilationDir = CU->getCompilationDir()) {
317 sys::path::append(AbsolutePath, CompilationDir);
318 }
319 sys::path::append(AbsolutePath, FileName);
320 FileName = AbsolutePath.str();
321 }
322 return true;
323}
324
Eric Christophere9403c12012-10-16 23:46:25 +0000325static bool getFileLineInfoForCompileUnit(DWARFCompileUnit *CU,
326 const DWARFLineTable *LineTable,
327 uint64_t Address,
328 bool NeedsAbsoluteFilePath,
329 std::string &FileName,
330 uint32_t &Line, uint32_t &Column) {
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000331 if (CU == 0 || LineTable == 0)
Alexey Samsonov38a63812012-08-30 07:49:50 +0000332 return false;
333 // Get the index of row we're looking for in the line table.
334 uint32_t RowIndex = LineTable->lookupAddress(Address);
335 if (RowIndex == -1U)
336 return false;
337 // Take file number and line/column from the row.
338 const DWARFDebugLine::Row &Row = LineTable->Rows[RowIndex];
339 if (!getFileNameForCompileUnit(CU, LineTable, Row.File,
340 NeedsAbsoluteFilePath, FileName))
341 return false;
342 Line = Row.Line;
343 Column = Row.Column;
344 return true;
345}
346
347DILineInfo DWARFContext::getLineInfoForAddress(uint64_t Address,
348 DILineInfoSpecifier Specifier) {
349 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
350 if (!CU)
Alexey Samsonov3e25c4a2012-07-02 05:54:45 +0000351 return DILineInfo();
Alexey Samsonov38a63812012-08-30 07:49:50 +0000352 std::string FileName = "<invalid>";
353 std::string FunctionName = "<invalid>";
354 uint32_t Line = 0;
355 uint32_t Column = 0;
356 if (Specifier.needs(DILineInfoSpecifier::FunctionName)) {
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000357 // The address may correspond to instruction in some inlined function,
358 // so we have to build the chain of inlined functions and take the
359 // name of the topmost function in it.
360 const DWARFDebugInfoEntryMinimal::InlinedChain &InlinedChain =
361 CU->getInlinedChainForAddress(Address);
362 if (InlinedChain.size() > 0) {
363 const DWARFDebugInfoEntryMinimal &TopFunctionDIE = InlinedChain[0];
364 if (const char *Name = TopFunctionDIE.getSubroutineName(CU))
Alexey Samsonov38a63812012-08-30 07:49:50 +0000365 FunctionName = Name;
Alexey Samsonov9d26b0b2012-07-17 15:28:35 +0000366 }
Alexey Samsonov3e25c4a2012-07-02 05:54:45 +0000367 }
Alexey Samsonov38a63812012-08-30 07:49:50 +0000368 if (Specifier.needs(DILineInfoSpecifier::FileLineInfo)) {
Eric Christophere9403c12012-10-16 23:46:25 +0000369 const DWARFLineTable *LineTable = getLineTableForCompileUnit(CU);
Alexey Samsonov38a63812012-08-30 07:49:50 +0000370 const bool NeedsAbsoluteFilePath =
371 Specifier.needs(DILineInfoSpecifier::AbsoluteFilePath);
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000372 getFileLineInfoForCompileUnit(CU, LineTable, Address,
373 NeedsAbsoluteFilePath,
Alexey Samsonov38a63812012-08-30 07:49:50 +0000374 FileName, Line, Column);
Alexey Samsonov3e25c4a2012-07-02 05:54:45 +0000375 }
Alexey Samsonov38a63812012-08-30 07:49:50 +0000376 return DILineInfo(StringRef(FileName), StringRef(FunctionName),
377 Line, Column);
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000378}
David Blaikie2d24e2a2011-12-20 02:50:00 +0000379
Andrew Kaylore27a7872013-01-26 00:28:05 +0000380DILineInfoTable DWARFContext::getLineInfoForAddressRange(uint64_t Address,
381 uint64_t Size,
382 DILineInfoSpecifier Specifier) {
383 DILineInfoTable Lines;
384 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
385 if (!CU)
386 return Lines;
387
388 std::string FunctionName = "<invalid>";
389 if (Specifier.needs(DILineInfoSpecifier::FunctionName)) {
390 // The address may correspond to instruction in some inlined function,
391 // so we have to build the chain of inlined functions and take the
392 // name of the topmost function in it.
393 const DWARFDebugInfoEntryMinimal::InlinedChain &InlinedChain =
394 CU->getInlinedChainForAddress(Address);
395 if (InlinedChain.size() > 0) {
396 const DWARFDebugInfoEntryMinimal &TopFunctionDIE = InlinedChain[0];
397 if (const char *Name = TopFunctionDIE.getSubroutineName(CU))
398 FunctionName = Name;
399 }
400 }
401
402 StringRef FuncNameRef = StringRef(FunctionName);
403
404 // If the Specifier says we don't need FileLineInfo, just
405 // return the top-most function at the starting address.
406 if (!Specifier.needs(DILineInfoSpecifier::FileLineInfo)) {
407 Lines.push_back(std::make_pair(Address,
408 DILineInfo(StringRef("<invalid>"),
409 FuncNameRef, 0, 0)));
410 return Lines;
411 }
412
413 const DWARFLineTable *LineTable = getLineTableForCompileUnit(CU);
414 const bool NeedsAbsoluteFilePath =
415 Specifier.needs(DILineInfoSpecifier::AbsoluteFilePath);
416
417 // Get the index of row we're looking for in the line table.
418 std::vector<uint32_t> RowVector;
419 if (!LineTable->lookupAddressRange(Address, Size, RowVector))
420 return Lines;
421
422 uint32_t NumRows = RowVector.size();
423 for (uint32_t i = 0; i < NumRows; ++i) {
424 uint32_t RowIndex = RowVector[i];
425 // Take file number and line/column from the row.
426 const DWARFDebugLine::Row &Row = LineTable->Rows[RowIndex];
427 std::string FileName = "<invalid>";
428 getFileNameForCompileUnit(CU, LineTable, Row.File,
429 NeedsAbsoluteFilePath, FileName);
430 Lines.push_back(std::make_pair(Row.Address,
431 DILineInfo(StringRef(FileName),
432 FuncNameRef, Row.Line, Row.Column)));
433 }
434
435 return Lines;
436}
437
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000438DIInliningInfo DWARFContext::getInliningInfoForAddress(uint64_t Address,
439 DILineInfoSpecifier Specifier) {
440 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
441 if (!CU)
442 return DIInliningInfo();
443
444 const DWARFDebugInfoEntryMinimal::InlinedChain &InlinedChain =
445 CU->getInlinedChainForAddress(Address);
446 if (InlinedChain.size() == 0)
447 return DIInliningInfo();
448
449 DIInliningInfo InliningInfo;
450 uint32_t CallFile = 0, CallLine = 0, CallColumn = 0;
Eric Christophere9403c12012-10-16 23:46:25 +0000451 const DWARFLineTable *LineTable = 0;
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000452 for (uint32_t i = 0, n = InlinedChain.size(); i != n; i++) {
453 const DWARFDebugInfoEntryMinimal &FunctionDIE = InlinedChain[i];
454 std::string FileName = "<invalid>";
455 std::string FunctionName = "<invalid>";
456 uint32_t Line = 0;
457 uint32_t Column = 0;
458 // Get function name if necessary.
459 if (Specifier.needs(DILineInfoSpecifier::FunctionName)) {
460 if (const char *Name = FunctionDIE.getSubroutineName(CU))
461 FunctionName = Name;
462 }
463 if (Specifier.needs(DILineInfoSpecifier::FileLineInfo)) {
464 const bool NeedsAbsoluteFilePath =
465 Specifier.needs(DILineInfoSpecifier::AbsoluteFilePath);
466 if (i == 0) {
467 // For the topmost frame, initialize the line table of this
468 // compile unit and fetch file/line info from it.
469 LineTable = getLineTableForCompileUnit(CU);
470 // For the topmost routine, get file/line info from line table.
471 getFileLineInfoForCompileUnit(CU, LineTable, Address,
472 NeedsAbsoluteFilePath,
473 FileName, Line, Column);
474 } else {
475 // Otherwise, use call file, call line and call column from
476 // previous DIE in inlined chain.
477 getFileNameForCompileUnit(CU, LineTable, CallFile,
478 NeedsAbsoluteFilePath, FileName);
479 Line = CallLine;
480 Column = CallColumn;
481 }
482 // Get call file/line/column of a current DIE.
483 if (i + 1 < n) {
484 FunctionDIE.getCallerFrame(CU, CallFile, CallLine, CallColumn);
485 }
486 }
487 DILineInfo Frame(StringRef(FileName), StringRef(FunctionName),
488 Line, Column);
489 InliningInfo.addFrame(Frame);
490 }
491 return InliningInfo;
492}
493
Alexey Samsonov005159e2013-04-23 10:17:34 +0000494static bool consumeCompressedDebugSectionHeader(StringRef &data,
495 uint64_t &OriginalSize) {
496 // Consume "ZLIB" prefix.
497 if (!data.startswith("ZLIB"))
498 return false;
499 data = data.substr(4);
500 // Consume uncompressed section size (big-endian 8 bytes).
501 DataExtractor extractor(data, false, 8);
502 uint32_t Offset = 0;
503 OriginalSize = extractor.getU64(&Offset);
504 if (Offset == 0)
505 return false;
506 data = data.substr(Offset);
507 return true;
508}
509
Eric Christopherd1726a42012-11-12 21:40:38 +0000510DWARFContextInMemory::DWARFContextInMemory(object::ObjectFile *Obj) :
Eli Bendersky60bdc5b2013-02-05 23:30:58 +0000511 IsLittleEndian(Obj->isLittleEndian()),
512 AddressSize(Obj->getBytesInAddress()) {
Eric Christopherd1726a42012-11-12 21:40:38 +0000513 error_code ec;
514 for (object::section_iterator i = Obj->begin_sections(),
515 e = Obj->end_sections();
516 i != e; i.increment(ec)) {
517 StringRef name;
518 i->getName(name);
519 StringRef data;
520 i->getContents(data);
521
Eric Christopherd1726a42012-11-12 21:40:38 +0000522 name = name.substr(name.find_first_not_of("._")); // Skip . and _ prefixes.
Alexey Samsonov784baa62013-04-17 14:27:04 +0000523
Alexey Samsonov005159e2013-04-23 10:17:34 +0000524 // Check if debug info section is compressed with zlib.
525 if (name.startswith("zdebug_")) {
526 uint64_t OriginalSize;
527 if (!zlib::isAvailable() ||
528 !consumeCompressedDebugSectionHeader(data, OriginalSize))
529 continue;
530 OwningPtr<MemoryBuffer> UncompressedSection;
531 if (zlib::uncompress(data, UncompressedSection, OriginalSize) !=
532 zlib::StatusOK)
533 continue;
534 // Make data point to uncompressed section contents and save its contents.
535 name = name.substr(1);
536 data = UncompressedSection->getBuffer();
537 UncompressedSections.push_back(UncompressedSection.take());
538 }
539
Alexey Samsonov784baa62013-04-17 14:27:04 +0000540 StringRef *Section = StringSwitch<StringRef*>(name)
541 .Case("debug_info", &InfoSection)
542 .Case("debug_abbrev", &AbbrevSection)
543 .Case("debug_line", &LineSection)
544 .Case("debug_aranges", &ARangeSection)
545 .Case("debug_frame", &DebugFrameSection)
546 .Case("debug_str", &StringSection)
547 .Case("debug_ranges", &RangeSection)
548 .Case("debug_pubnames", &PubNamesSection)
549 .Case("debug_info.dwo", &InfoDWOSection)
550 .Case("debug_abbrev.dwo", &AbbrevDWOSection)
551 .Case("debug_str.dwo", &StringDWOSection)
552 .Case("debug_str_offsets.dwo", &StringOffsetDWOSection)
553 .Case("debug_addr", &AddrSection)
554 // Any more debug info sections go here.
555 .Default(0);
556 if (!Section)
557 continue;
558 *Section = data;
559 if (name == "debug_ranges") {
Eric Christopher82de10a2013-01-02 23:52:13 +0000560 // FIXME: Use the other dwo range section when we emit it.
561 RangeDWOSection = data;
Eric Christopher82de10a2013-01-02 23:52:13 +0000562 }
Eric Christopherd1726a42012-11-12 21:40:38 +0000563
Andrew Kayloree7c0d22013-01-25 22:50:58 +0000564 // TODO: Add support for relocations in other sections as needed.
565 // Record relocations for the debug_info and debug_line sections.
Alexey Samsonov784baa62013-04-17 14:27:04 +0000566 RelocAddrMap *Map = StringSwitch<RelocAddrMap*>(name)
567 .Case("debug_info", &InfoRelocMap)
568 .Case("debug_info.dwo", &InfoDWORelocMap)
569 .Case("debug_line", &LineRelocMap)
570 .Default(0);
571 if (!Map)
Eric Christopherd1726a42012-11-12 21:40:38 +0000572 continue;
573
574 if (i->begin_relocations() != i->end_relocations()) {
575 uint64_t SectionSize;
576 i->getSize(SectionSize);
577 for (object::relocation_iterator reloc_i = i->begin_relocations(),
578 reloc_e = i->end_relocations();
579 reloc_i != reloc_e; reloc_i.increment(ec)) {
580 uint64_t Address;
Rafael Espindola956ca722013-04-25 12:28:45 +0000581 reloc_i->getOffset(Address);
Eric Christopherd1726a42012-11-12 21:40:38 +0000582 uint64_t Type;
583 reloc_i->getType(Type);
Andrew Kayloree7c0d22013-01-25 22:50:58 +0000584 uint64_t SymAddr = 0;
585 // ELF relocations may need the symbol address
586 if (Obj->isELF()) {
587 object::SymbolRef Sym;
588 reloc_i->getSymbol(Sym);
589 Sym.getAddress(SymAddr);
590 }
Eric Christopherd1726a42012-11-12 21:40:38 +0000591
592 object::RelocVisitor V(Obj->getFileFormatName());
593 // The section address is always 0 for debug sections.
Andrew Kayloree7c0d22013-01-25 22:50:58 +0000594 object::RelocToApply R(V.visit(Type, *reloc_i, 0, SymAddr));
Eric Christopherd1726a42012-11-12 21:40:38 +0000595 if (V.error()) {
596 SmallString<32> Name;
597 error_code ec(reloc_i->getTypeName(Name));
598 if (ec) {
599 errs() << "Aaaaaa! Nameless relocation! Aaaaaa!\n";
600 }
601 errs() << "error: failed to compute relocation: "
602 << Name << "\n";
603 continue;
604 }
605
606 if (Address + R.Width > SectionSize) {
607 errs() << "error: " << R.Width << "-byte relocation starting "
608 << Address << " bytes into section " << name << " which is "
609 << SectionSize << " bytes long.\n";
610 continue;
611 }
612 if (R.Width > 8) {
613 errs() << "error: can't handle a relocation of more than 8 bytes at "
614 "a time.\n";
615 continue;
616 }
617 DEBUG(dbgs() << "Writing " << format("%p", R.Value)
618 << " at " << format("%p", Address)
619 << " with width " << format("%d", R.Width)
620 << "\n");
Eric Christopher82de10a2013-01-02 23:52:13 +0000621 Map->insert(std::make_pair(Address, std::make_pair(R.Width, R.Value)));
Eric Christopherd1726a42012-11-12 21:40:38 +0000622 }
623 }
624 }
625}
626
Alexey Samsonov005159e2013-04-23 10:17:34 +0000627DWARFContextInMemory::~DWARFContextInMemory() {
628 DeleteContainerPointers(UncompressedSections);
629}
630
David Blaikie2d24e2a2011-12-20 02:50:00 +0000631void DWARFContextInMemory::anchor() { }