blob: e5daf55982f7d7085d0faa8376bc4b37b463d75c [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) {
113 OS << "\n.debug_abbrev.dwo contents:\n";
114 getDebugAbbrevDWO()->dump(OS);
115 }
116
117 if (DumpType == DIDT_All || DumpType == DIDT_InfoDwo) {
118 OS << "\n.debug_info.dwo contents:\n";
119 for (unsigned i = 0, e = getNumDWOCompileUnits(); i != e; ++i)
120 getDWOCompileUnitAtIndex(i)->dump(OS);
121 }
122
123 if (DumpType == DIDT_All || DumpType == DIDT_StrDwo) {
124 OS << "\n.debug_str.dwo contents:\n";
125 DataExtractor strDWOData(getStringDWOSection(), isLittleEndian(), 0);
126 offset = 0;
127 uint32_t strDWOOffset = 0;
128 while (const char *s = strDWOData.getCStr(&offset)) {
129 OS << format("0x%8.8x: \"%s\"\n", strDWOOffset, s);
130 strDWOOffset = offset;
131 }
132 }
133
134 if (DumpType == DIDT_All || DumpType == DIDT_StrOffsetsDwo) {
135 OS << "\n.debug_str_offsets.dwo contents:\n";
136 DataExtractor strOffsetExt(getStringOffsetDWOSection(), isLittleEndian(), 0);
137 offset = 0;
138 while (offset < getStringOffsetDWOSection().size()) {
139 OS << format("0x%8.8x: ", offset);
140 OS << format("%8.8x\n", strOffsetExt.getU32(&offset));
141 }
Eric Christopher72f7bfb2013-01-15 23:56:56 +0000142 }
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000143}
144
145const DWARFDebugAbbrev *DWARFContext::getDebugAbbrev() {
146 if (Abbrev)
147 return Abbrev.get();
148
149 DataExtractor abbrData(getAbbrevSection(), isLittleEndian(), 0);
150
151 Abbrev.reset(new DWARFDebugAbbrev());
152 Abbrev->parse(abbrData);
153 return Abbrev.get();
154}
155
Eric Christopher82de10a2013-01-02 23:52:13 +0000156const DWARFDebugAbbrev *DWARFContext::getDebugAbbrevDWO() {
157 if (AbbrevDWO)
158 return AbbrevDWO.get();
159
160 DataExtractor abbrData(getAbbrevDWOSection(), isLittleEndian(), 0);
161 AbbrevDWO.reset(new DWARFDebugAbbrev());
162 AbbrevDWO->parse(abbrData);
163 return AbbrevDWO.get();
164}
165
Benjamin Kramer358f4fd2011-09-14 01:09:52 +0000166const DWARFDebugAranges *DWARFContext::getDebugAranges() {
167 if (Aranges)
168 return Aranges.get();
169
170 DataExtractor arangesData(getARangeSection(), isLittleEndian(), 0);
171
172 Aranges.reset(new DWARFDebugAranges());
173 Aranges->extract(arangesData);
Alexey Samsonov63a450a2012-11-16 08:36:25 +0000174 // Generate aranges from DIEs: even if .debug_aranges section is present,
175 // it may describe only a small subset of compilation units, so we need to
176 // manually build aranges for the rest of them.
177 Aranges->generate(this);
Benjamin Kramer358f4fd2011-09-14 01:09:52 +0000178 return Aranges.get();
179}
180
Eli Bendersky60bdc5b2013-02-05 23:30:58 +0000181const DWARFDebugFrame *DWARFContext::getDebugFrame() {
182 if (DebugFrame)
183 return DebugFrame.get();
184
185 // There's a "bug" in the DWARFv3 standard with respect to the target address
186 // size within debug frame sections. While DWARF is supposed to be independent
187 // of its container, FDEs have fields with size being "target address size",
188 // which isn't specified in DWARF in general. It's only specified for CUs, but
189 // .eh_frame can appear without a .debug_info section. Follow the example of
190 // other tools (libdwarf) and extract this from the container (ObjectFile
191 // provides this information). This problem is fixed in DWARFv4
192 // See this dwarf-discuss discussion for more details:
193 // http://lists.dwarfstd.org/htdig.cgi/dwarf-discuss-dwarfstd.org/2011-December/001173.html
194 DataExtractor debugFrameData(getDebugFrameSection(), isLittleEndian(),
195 getAddressSize());
196 DebugFrame.reset(new DWARFDebugFrame());
197 DebugFrame->parse(debugFrameData);
198 return DebugFrame.get();
199}
200
Eric Christophere9403c12012-10-16 23:46:25 +0000201const DWARFLineTable *
Benjamin Kramerc26ed9b2011-09-15 20:43:18 +0000202DWARFContext::getLineTableForCompileUnit(DWARFCompileUnit *cu) {
203 if (!Line)
Andrew Kayloree7c0d22013-01-25 22:50:58 +0000204 Line.reset(new DWARFDebugLine(&lineRelocMap()));
Benjamin Kramerb848e972011-09-15 02:12:05 +0000205
Benjamin Kramerc26ed9b2011-09-15 20:43:18 +0000206 unsigned stmtOffset =
207 cu->getCompileUnitDIE()->getAttributeValueAsUnsigned(cu, DW_AT_stmt_list,
208 -1U);
209 if (stmtOffset == -1U)
210 return 0; // No line table for this compile unit.
Benjamin Kramerb848e972011-09-15 02:12:05 +0000211
Benjamin Kramerc26ed9b2011-09-15 20:43:18 +0000212 // See if the line table is cached.
Eric Christophere9403c12012-10-16 23:46:25 +0000213 if (const DWARFLineTable *lt = Line->getLineTable(stmtOffset))
Benjamin Kramerc26ed9b2011-09-15 20:43:18 +0000214 return lt;
215
216 // We have to parse it first.
217 DataExtractor lineData(getLineSection(), isLittleEndian(),
218 cu->getAddressByteSize());
219 return Line->getOrParseLineTable(lineData, stmtOffset);
Benjamin Kramerb848e972011-09-15 02:12:05 +0000220}
221
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000222void DWARFContext::parseCompileUnits() {
223 uint32_t offset = 0;
Eric Christopherb69b55f2012-10-16 23:46:23 +0000224 const DataExtractor &DIData = DataExtractor(getInfoSection(),
225 isLittleEndian(), 0);
226 while (DIData.isValidOffset(offset)) {
Eric Christopher82de10a2013-01-02 23:52:13 +0000227 CUs.push_back(DWARFCompileUnit(getDebugAbbrev(), getInfoSection(),
228 getAbbrevSection(), getRangeSection(),
Eric Christopher72f7bfb2013-01-15 23:56:56 +0000229 getStringSection(), StringRef(),
230 getAddrSection(),
Eric Christopherdd8e9f32013-01-07 19:32:41 +0000231 &infoRelocMap(),
Eric Christopher82de10a2013-01-02 23:52:13 +0000232 isLittleEndian()));
Eric Christopherb69b55f2012-10-16 23:46:23 +0000233 if (!CUs.back().extract(DIData, &offset)) {
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000234 CUs.pop_back();
235 break;
236 }
237
238 offset = CUs.back().getNextCompileUnitOffset();
239 }
240}
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000241
Eric Christopher82de10a2013-01-02 23:52:13 +0000242void DWARFContext::parseDWOCompileUnits() {
243 uint32_t offset = 0;
244 const DataExtractor &DIData = DataExtractor(getInfoDWOSection(),
245 isLittleEndian(), 0);
246 while (DIData.isValidOffset(offset)) {
247 DWOCUs.push_back(DWARFCompileUnit(getDebugAbbrevDWO(), getInfoDWOSection(),
248 getAbbrevDWOSection(),
249 getRangeDWOSection(),
250 getStringDWOSection(),
Eric Christopherdd8e9f32013-01-07 19:32:41 +0000251 getStringOffsetDWOSection(),
Eric Christopher72f7bfb2013-01-15 23:56:56 +0000252 getAddrSection(),
Eric Christopher82de10a2013-01-02 23:52:13 +0000253 &infoDWORelocMap(),
254 isLittleEndian()));
255 if (!DWOCUs.back().extract(DIData, &offset)) {
256 DWOCUs.pop_back();
257 break;
258 }
259
260 offset = DWOCUs.back().getNextCompileUnitOffset();
261 }
262}
263
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000264namespace {
265 struct OffsetComparator {
266 bool operator()(const DWARFCompileUnit &LHS,
267 const DWARFCompileUnit &RHS) const {
268 return LHS.getOffset() < RHS.getOffset();
269 }
270 bool operator()(const DWARFCompileUnit &LHS, uint32_t RHS) const {
271 return LHS.getOffset() < RHS;
272 }
273 bool operator()(uint32_t LHS, const DWARFCompileUnit &RHS) const {
274 return LHS < RHS.getOffset();
275 }
276 };
277}
278
Alexey Samsonov38a63812012-08-30 07:49:50 +0000279DWARFCompileUnit *DWARFContext::getCompileUnitForOffset(uint32_t Offset) {
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000280 if (CUs.empty())
281 parseCompileUnits();
282
Alexey Samsonov38a63812012-08-30 07:49:50 +0000283 DWARFCompileUnit *CU = std::lower_bound(CUs.begin(), CUs.end(), Offset,
284 OffsetComparator());
285 if (CU != CUs.end())
286 return &*CU;
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000287 return 0;
288}
289
Alexey Samsonov38a63812012-08-30 07:49:50 +0000290DWARFCompileUnit *DWARFContext::getCompileUnitForAddress(uint64_t Address) {
Benjamin Kramer9013db32011-09-15 21:59:13 +0000291 // First, get the offset of the compile unit.
Alexey Samsonov38a63812012-08-30 07:49:50 +0000292 uint32_t CUOffset = getDebugAranges()->findAddress(Address);
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000293 // Retrieve the compile unit.
Alexey Samsonov38a63812012-08-30 07:49:50 +0000294 return getCompileUnitForOffset(CUOffset);
295}
296
Eric Christophere9403c12012-10-16 23:46:25 +0000297static bool getFileNameForCompileUnit(DWARFCompileUnit *CU,
298 const DWARFLineTable *LineTable,
299 uint64_t FileIndex,
300 bool NeedsAbsoluteFilePath,
301 std::string &FileName) {
Alexey Samsonov38a63812012-08-30 07:49:50 +0000302 if (CU == 0 ||
303 LineTable == 0 ||
304 !LineTable->getFileNameByIndex(FileIndex, NeedsAbsoluteFilePath,
305 FileName))
306 return false;
307 if (NeedsAbsoluteFilePath && sys::path::is_relative(FileName)) {
308 // We may still need to append compilation directory of compile unit.
309 SmallString<16> AbsolutePath;
310 if (const char *CompilationDir = CU->getCompilationDir()) {
311 sys::path::append(AbsolutePath, CompilationDir);
312 }
313 sys::path::append(AbsolutePath, FileName);
314 FileName = AbsolutePath.str();
315 }
316 return true;
317}
318
Eric Christophere9403c12012-10-16 23:46:25 +0000319static bool getFileLineInfoForCompileUnit(DWARFCompileUnit *CU,
320 const DWARFLineTable *LineTable,
321 uint64_t Address,
322 bool NeedsAbsoluteFilePath,
323 std::string &FileName,
324 uint32_t &Line, uint32_t &Column) {
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000325 if (CU == 0 || LineTable == 0)
Alexey Samsonov38a63812012-08-30 07:49:50 +0000326 return false;
327 // Get the index of row we're looking for in the line table.
328 uint32_t RowIndex = LineTable->lookupAddress(Address);
329 if (RowIndex == -1U)
330 return false;
331 // Take file number and line/column from the row.
332 const DWARFDebugLine::Row &Row = LineTable->Rows[RowIndex];
333 if (!getFileNameForCompileUnit(CU, LineTable, Row.File,
334 NeedsAbsoluteFilePath, FileName))
335 return false;
336 Line = Row.Line;
337 Column = Row.Column;
338 return true;
339}
340
341DILineInfo DWARFContext::getLineInfoForAddress(uint64_t Address,
342 DILineInfoSpecifier Specifier) {
343 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
344 if (!CU)
Alexey Samsonov3e25c4a2012-07-02 05:54:45 +0000345 return DILineInfo();
Alexey Samsonov38a63812012-08-30 07:49:50 +0000346 std::string FileName = "<invalid>";
347 std::string FunctionName = "<invalid>";
348 uint32_t Line = 0;
349 uint32_t Column = 0;
350 if (Specifier.needs(DILineInfoSpecifier::FunctionName)) {
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000351 // The address may correspond to instruction in some inlined function,
352 // so we have to build the chain of inlined functions and take the
353 // name of the topmost function in it.
354 const DWARFDebugInfoEntryMinimal::InlinedChain &InlinedChain =
355 CU->getInlinedChainForAddress(Address);
356 if (InlinedChain.size() > 0) {
357 const DWARFDebugInfoEntryMinimal &TopFunctionDIE = InlinedChain[0];
358 if (const char *Name = TopFunctionDIE.getSubroutineName(CU))
Alexey Samsonov38a63812012-08-30 07:49:50 +0000359 FunctionName = Name;
Alexey Samsonov9d26b0b2012-07-17 15:28:35 +0000360 }
Alexey Samsonov3e25c4a2012-07-02 05:54:45 +0000361 }
Alexey Samsonov38a63812012-08-30 07:49:50 +0000362 if (Specifier.needs(DILineInfoSpecifier::FileLineInfo)) {
Eric Christophere9403c12012-10-16 23:46:25 +0000363 const DWARFLineTable *LineTable = getLineTableForCompileUnit(CU);
Alexey Samsonov38a63812012-08-30 07:49:50 +0000364 const bool NeedsAbsoluteFilePath =
365 Specifier.needs(DILineInfoSpecifier::AbsoluteFilePath);
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000366 getFileLineInfoForCompileUnit(CU, LineTable, Address,
367 NeedsAbsoluteFilePath,
Alexey Samsonov38a63812012-08-30 07:49:50 +0000368 FileName, Line, Column);
Alexey Samsonov3e25c4a2012-07-02 05:54:45 +0000369 }
Alexey Samsonov38a63812012-08-30 07:49:50 +0000370 return DILineInfo(StringRef(FileName), StringRef(FunctionName),
371 Line, Column);
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000372}
David Blaikie2d24e2a2011-12-20 02:50:00 +0000373
Andrew Kaylore27a7872013-01-26 00:28:05 +0000374DILineInfoTable DWARFContext::getLineInfoForAddressRange(uint64_t Address,
375 uint64_t Size,
376 DILineInfoSpecifier Specifier) {
377 DILineInfoTable Lines;
378 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
379 if (!CU)
380 return Lines;
381
382 std::string FunctionName = "<invalid>";
383 if (Specifier.needs(DILineInfoSpecifier::FunctionName)) {
384 // The address may correspond to instruction in some inlined function,
385 // so we have to build the chain of inlined functions and take the
386 // name of the topmost function in it.
387 const DWARFDebugInfoEntryMinimal::InlinedChain &InlinedChain =
388 CU->getInlinedChainForAddress(Address);
389 if (InlinedChain.size() > 0) {
390 const DWARFDebugInfoEntryMinimal &TopFunctionDIE = InlinedChain[0];
391 if (const char *Name = TopFunctionDIE.getSubroutineName(CU))
392 FunctionName = Name;
393 }
394 }
395
396 StringRef FuncNameRef = StringRef(FunctionName);
397
398 // If the Specifier says we don't need FileLineInfo, just
399 // return the top-most function at the starting address.
400 if (!Specifier.needs(DILineInfoSpecifier::FileLineInfo)) {
401 Lines.push_back(std::make_pair(Address,
402 DILineInfo(StringRef("<invalid>"),
403 FuncNameRef, 0, 0)));
404 return Lines;
405 }
406
407 const DWARFLineTable *LineTable = getLineTableForCompileUnit(CU);
408 const bool NeedsAbsoluteFilePath =
409 Specifier.needs(DILineInfoSpecifier::AbsoluteFilePath);
410
411 // Get the index of row we're looking for in the line table.
412 std::vector<uint32_t> RowVector;
413 if (!LineTable->lookupAddressRange(Address, Size, RowVector))
414 return Lines;
415
416 uint32_t NumRows = RowVector.size();
417 for (uint32_t i = 0; i < NumRows; ++i) {
418 uint32_t RowIndex = RowVector[i];
419 // Take file number and line/column from the row.
420 const DWARFDebugLine::Row &Row = LineTable->Rows[RowIndex];
421 std::string FileName = "<invalid>";
422 getFileNameForCompileUnit(CU, LineTable, Row.File,
423 NeedsAbsoluteFilePath, FileName);
424 Lines.push_back(std::make_pair(Row.Address,
425 DILineInfo(StringRef(FileName),
426 FuncNameRef, Row.Line, Row.Column)));
427 }
428
429 return Lines;
430}
431
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000432DIInliningInfo DWARFContext::getInliningInfoForAddress(uint64_t Address,
433 DILineInfoSpecifier Specifier) {
434 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
435 if (!CU)
436 return DIInliningInfo();
437
438 const DWARFDebugInfoEntryMinimal::InlinedChain &InlinedChain =
439 CU->getInlinedChainForAddress(Address);
440 if (InlinedChain.size() == 0)
441 return DIInliningInfo();
442
443 DIInliningInfo InliningInfo;
444 uint32_t CallFile = 0, CallLine = 0, CallColumn = 0;
Eric Christophere9403c12012-10-16 23:46:25 +0000445 const DWARFLineTable *LineTable = 0;
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000446 for (uint32_t i = 0, n = InlinedChain.size(); i != n; i++) {
447 const DWARFDebugInfoEntryMinimal &FunctionDIE = InlinedChain[i];
448 std::string FileName = "<invalid>";
449 std::string FunctionName = "<invalid>";
450 uint32_t Line = 0;
451 uint32_t Column = 0;
452 // Get function name if necessary.
453 if (Specifier.needs(DILineInfoSpecifier::FunctionName)) {
454 if (const char *Name = FunctionDIE.getSubroutineName(CU))
455 FunctionName = Name;
456 }
457 if (Specifier.needs(DILineInfoSpecifier::FileLineInfo)) {
458 const bool NeedsAbsoluteFilePath =
459 Specifier.needs(DILineInfoSpecifier::AbsoluteFilePath);
460 if (i == 0) {
461 // For the topmost frame, initialize the line table of this
462 // compile unit and fetch file/line info from it.
463 LineTable = getLineTableForCompileUnit(CU);
464 // For the topmost routine, get file/line info from line table.
465 getFileLineInfoForCompileUnit(CU, LineTable, Address,
466 NeedsAbsoluteFilePath,
467 FileName, Line, Column);
468 } else {
469 // Otherwise, use call file, call line and call column from
470 // previous DIE in inlined chain.
471 getFileNameForCompileUnit(CU, LineTable, CallFile,
472 NeedsAbsoluteFilePath, FileName);
473 Line = CallLine;
474 Column = CallColumn;
475 }
476 // Get call file/line/column of a current DIE.
477 if (i + 1 < n) {
478 FunctionDIE.getCallerFrame(CU, CallFile, CallLine, CallColumn);
479 }
480 }
481 DILineInfo Frame(StringRef(FileName), StringRef(FunctionName),
482 Line, Column);
483 InliningInfo.addFrame(Frame);
484 }
485 return InliningInfo;
486}
487
Alexey Samsonov005159e2013-04-23 10:17:34 +0000488static bool consumeCompressedDebugSectionHeader(StringRef &data,
489 uint64_t &OriginalSize) {
490 // Consume "ZLIB" prefix.
491 if (!data.startswith("ZLIB"))
492 return false;
493 data = data.substr(4);
494 // Consume uncompressed section size (big-endian 8 bytes).
495 DataExtractor extractor(data, false, 8);
496 uint32_t Offset = 0;
497 OriginalSize = extractor.getU64(&Offset);
498 if (Offset == 0)
499 return false;
500 data = data.substr(Offset);
501 return true;
502}
503
Eric Christopherd1726a42012-11-12 21:40:38 +0000504DWARFContextInMemory::DWARFContextInMemory(object::ObjectFile *Obj) :
Eli Bendersky60bdc5b2013-02-05 23:30:58 +0000505 IsLittleEndian(Obj->isLittleEndian()),
506 AddressSize(Obj->getBytesInAddress()) {
Eric Christopherd1726a42012-11-12 21:40:38 +0000507 error_code ec;
508 for (object::section_iterator i = Obj->begin_sections(),
509 e = Obj->end_sections();
510 i != e; i.increment(ec)) {
511 StringRef name;
512 i->getName(name);
513 StringRef data;
514 i->getContents(data);
515
Eric Christopherd1726a42012-11-12 21:40:38 +0000516 name = name.substr(name.find_first_not_of("._")); // Skip . and _ prefixes.
Alexey Samsonov784baa62013-04-17 14:27:04 +0000517
Alexey Samsonov005159e2013-04-23 10:17:34 +0000518 // Check if debug info section is compressed with zlib.
519 if (name.startswith("zdebug_")) {
520 uint64_t OriginalSize;
521 if (!zlib::isAvailable() ||
522 !consumeCompressedDebugSectionHeader(data, OriginalSize))
523 continue;
524 OwningPtr<MemoryBuffer> UncompressedSection;
525 if (zlib::uncompress(data, UncompressedSection, OriginalSize) !=
526 zlib::StatusOK)
527 continue;
528 // Make data point to uncompressed section contents and save its contents.
529 name = name.substr(1);
530 data = UncompressedSection->getBuffer();
531 UncompressedSections.push_back(UncompressedSection.take());
532 }
533
Alexey Samsonov784baa62013-04-17 14:27:04 +0000534 StringRef *Section = StringSwitch<StringRef*>(name)
535 .Case("debug_info", &InfoSection)
536 .Case("debug_abbrev", &AbbrevSection)
537 .Case("debug_line", &LineSection)
538 .Case("debug_aranges", &ARangeSection)
539 .Case("debug_frame", &DebugFrameSection)
540 .Case("debug_str", &StringSection)
541 .Case("debug_ranges", &RangeSection)
542 .Case("debug_pubnames", &PubNamesSection)
543 .Case("debug_info.dwo", &InfoDWOSection)
544 .Case("debug_abbrev.dwo", &AbbrevDWOSection)
545 .Case("debug_str.dwo", &StringDWOSection)
546 .Case("debug_str_offsets.dwo", &StringOffsetDWOSection)
547 .Case("debug_addr", &AddrSection)
548 // Any more debug info sections go here.
549 .Default(0);
550 if (!Section)
551 continue;
552 *Section = data;
553 if (name == "debug_ranges") {
Eric Christopher82de10a2013-01-02 23:52:13 +0000554 // FIXME: Use the other dwo range section when we emit it.
555 RangeDWOSection = data;
Eric Christopher82de10a2013-01-02 23:52:13 +0000556 }
Eric Christopherd1726a42012-11-12 21:40:38 +0000557
Andrew Kayloree7c0d22013-01-25 22:50:58 +0000558 // TODO: Add support for relocations in other sections as needed.
559 // Record relocations for the debug_info and debug_line sections.
Alexey Samsonov784baa62013-04-17 14:27:04 +0000560 RelocAddrMap *Map = StringSwitch<RelocAddrMap*>(name)
561 .Case("debug_info", &InfoRelocMap)
562 .Case("debug_info.dwo", &InfoDWORelocMap)
563 .Case("debug_line", &LineRelocMap)
564 .Default(0);
565 if (!Map)
Eric Christopherd1726a42012-11-12 21:40:38 +0000566 continue;
567
568 if (i->begin_relocations() != i->end_relocations()) {
569 uint64_t SectionSize;
570 i->getSize(SectionSize);
571 for (object::relocation_iterator reloc_i = i->begin_relocations(),
572 reloc_e = i->end_relocations();
573 reloc_i != reloc_e; reloc_i.increment(ec)) {
574 uint64_t Address;
Rafael Espindola956ca722013-04-25 12:28:45 +0000575 reloc_i->getOffset(Address);
Eric Christopherd1726a42012-11-12 21:40:38 +0000576 uint64_t Type;
577 reloc_i->getType(Type);
Andrew Kayloree7c0d22013-01-25 22:50:58 +0000578 uint64_t SymAddr = 0;
579 // ELF relocations may need the symbol address
580 if (Obj->isELF()) {
581 object::SymbolRef Sym;
582 reloc_i->getSymbol(Sym);
583 Sym.getAddress(SymAddr);
584 }
Eric Christopherd1726a42012-11-12 21:40:38 +0000585
586 object::RelocVisitor V(Obj->getFileFormatName());
587 // The section address is always 0 for debug sections.
Andrew Kayloree7c0d22013-01-25 22:50:58 +0000588 object::RelocToApply R(V.visit(Type, *reloc_i, 0, SymAddr));
Eric Christopherd1726a42012-11-12 21:40:38 +0000589 if (V.error()) {
590 SmallString<32> Name;
591 error_code ec(reloc_i->getTypeName(Name));
592 if (ec) {
593 errs() << "Aaaaaa! Nameless relocation! Aaaaaa!\n";
594 }
595 errs() << "error: failed to compute relocation: "
596 << Name << "\n";
597 continue;
598 }
599
600 if (Address + R.Width > SectionSize) {
601 errs() << "error: " << R.Width << "-byte relocation starting "
602 << Address << " bytes into section " << name << " which is "
603 << SectionSize << " bytes long.\n";
604 continue;
605 }
606 if (R.Width > 8) {
607 errs() << "error: can't handle a relocation of more than 8 bytes at "
608 "a time.\n";
609 continue;
610 }
611 DEBUG(dbgs() << "Writing " << format("%p", R.Value)
612 << " at " << format("%p", Address)
613 << " with width " << format("%d", R.Width)
614 << "\n");
Eric Christopher82de10a2013-01-02 23:52:13 +0000615 Map->insert(std::make_pair(Address, std::make_pair(R.Width, R.Value)));
Eric Christopherd1726a42012-11-12 21:40:38 +0000616 }
617 }
618 }
619}
620
Alexey Samsonov005159e2013-04-23 10:17:34 +0000621DWARFContextInMemory::~DWARFContextInMemory() {
622 DeleteContainerPointers(UncompressedSections);
623}
624
David Blaikie2d24e2a2011-12-20 02:50:00 +0000625void DWARFContextInMemory::anchor() { }