blob: 74a8c36d7aba3fbb747374034ed4e746a0d853f2 [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"
Benjamin Kramerfe80f1d2011-09-15 18:02:20 +000013#include "llvm/Support/Dwarf.h"
Benjamin Kramer34f864f2011-09-15 16:57:13 +000014#include "llvm/Support/Format.h"
Alexey Samsonov71d94f82012-07-19 07:03:58 +000015#include "llvm/Support/Path.h"
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000016#include "llvm/Support/raw_ostream.h"
Benjamin Kramer101b1c52011-09-15 20:43:22 +000017#include <algorithm>
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000018using namespace llvm;
Benjamin Kramerfe80f1d2011-09-15 18:02:20 +000019using namespace dwarf;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000020
Eric Christophere9403c12012-10-16 23:46:25 +000021typedef DWARFDebugLine::LineTable DWARFLineTable;
22
Eli Bendersky939a4e82013-01-25 20:26:43 +000023void DWARFContext::dump(raw_ostream &OS, DIDumpType DumpType) {
24 if (DumpType == DIDT_All || DumpType == DIDT_Abbrev) {
25 OS << ".debug_abbrev contents:\n";
26 getDebugAbbrev()->dump(OS);
27 }
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000028
Eli Bendersky939a4e82013-01-25 20:26:43 +000029 if (DumpType == DIDT_All || DumpType == DIDT_Info) {
30 OS << "\n.debug_info contents:\n";
31 for (unsigned i = 0, e = getNumCompileUnits(); i != e; ++i)
32 getCompileUnitAtIndex(i)->dump(OS);
33 }
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000034
Eli Bendersky60bdc5b2013-02-05 23:30:58 +000035 if (DumpType == DIDT_All || DumpType == DIDT_Frames) {
36 OS << "\n.debug_frame contents:\n";
37 getDebugFrame()->dump(OS);
38 }
39
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000040 uint32_t offset = 0;
Eli Bendersky939a4e82013-01-25 20:26:43 +000041 if (DumpType == DIDT_All || DumpType == DIDT_Aranges) {
42 OS << "\n.debug_aranges contents:\n";
43 DataExtractor arangesData(getARangeSection(), isLittleEndian(), 0);
44 DWARFDebugArangeSet set;
45 while (set.extract(arangesData, &offset))
46 set.dump(OS);
47 }
Benjamin Kramerb848e972011-09-15 02:12:05 +000048
Alexey Samsonoveceb5b92012-08-27 07:17:47 +000049 uint8_t savedAddressByteSize = 0;
Eli Bendersky939a4e82013-01-25 20:26:43 +000050 if (DumpType == DIDT_All || DumpType == DIDT_Line) {
51 OS << "\n.debug_line contents:\n";
52 for (unsigned i = 0, e = getNumCompileUnits(); i != e; ++i) {
53 DWARFCompileUnit *cu = getCompileUnitAtIndex(i);
54 savedAddressByteSize = cu->getAddressByteSize();
55 unsigned stmtOffset =
56 cu->getCompileUnitDIE()->getAttributeValueAsUnsigned(cu, DW_AT_stmt_list,
57 -1U);
58 if (stmtOffset != -1U) {
59 DataExtractor lineData(getLineSection(), isLittleEndian(),
60 savedAddressByteSize);
61 DWARFDebugLine::DumpingState state(OS);
Andrew Kayloree7c0d22013-01-25 22:50:58 +000062 DWARFDebugLine::parseStatementTable(lineData, &lineRelocMap(), &stmtOffset, state);
Eli Bendersky939a4e82013-01-25 20:26:43 +000063 }
Benjamin Kramerfe80f1d2011-09-15 18:02:20 +000064 }
65 }
Benjamin Kramer34f864f2011-09-15 16:57:13 +000066
Eli Bendersky939a4e82013-01-25 20:26:43 +000067 if (DumpType == DIDT_All || DumpType == DIDT_Str) {
68 OS << "\n.debug_str contents:\n";
69 DataExtractor strData(getStringSection(), isLittleEndian(), 0);
70 offset = 0;
71 uint32_t strOffset = 0;
72 while (const char *s = strData.getCStr(&offset)) {
73 OS << format("0x%8.8x: \"%s\"\n", strOffset, s);
74 strOffset = offset;
75 }
Benjamin Kramer34f864f2011-09-15 16:57:13 +000076 }
Alexey Samsonoveceb5b92012-08-27 07:17:47 +000077
Eli Bendersky939a4e82013-01-25 20:26:43 +000078 if (DumpType == DIDT_All || DumpType == DIDT_Ranges) {
79 OS << "\n.debug_ranges contents:\n";
80 // In fact, different compile units may have different address byte
81 // sizes, but for simplicity we just use the address byte size of the last
82 // compile unit (there is no easy and fast way to associate address range
83 // list and the compile unit it describes).
84 DataExtractor rangesData(getRangeSection(), isLittleEndian(),
85 savedAddressByteSize);
86 offset = 0;
87 DWARFDebugRangeList rangeList;
88 while (rangeList.extract(rangesData, &offset))
89 rangeList.dump(OS);
Eric Christopher82de10a2013-01-02 23:52:13 +000090 }
Eric Christopher72f7bfb2013-01-15 23:56:56 +000091
Krzysztof Parzyszeke38825f2013-02-12 16:20:28 +000092 if (DumpType == DIDT_All || DumpType == DIDT_Pubnames) {
93 OS << "\n.debug_pubnames contents:\n";
94 DataExtractor pubNames(getPubNamesSection(), isLittleEndian(), 0);
95 offset = 0;
96 OS << "Length: " << pubNames.getU32(&offset) << "\n";
97 OS << "Version: " << pubNames.getU16(&offset) << "\n";
98 OS << "Offset in .debug_info: " << pubNames.getU32(&offset) << "\n";
99 OS << "Size: " << pubNames.getU32(&offset) << "\n";
100 OS << "\n Offset Name\n";
101 while (offset < getPubNamesSection().size()) {
102 uint32_t n = pubNames.getU32(&offset);
103 if (n == 0)
104 break;
105 OS << format("%8x ", n);
106 OS << pubNames.getCStr(&offset) << "\n";
107 }
108 }
109
Eli Bendersky939a4e82013-01-25 20:26:43 +0000110 if (DumpType == DIDT_All || DumpType == DIDT_AbbrevDwo) {
111 OS << "\n.debug_abbrev.dwo contents:\n";
112 getDebugAbbrevDWO()->dump(OS);
113 }
114
115 if (DumpType == DIDT_All || DumpType == DIDT_InfoDwo) {
116 OS << "\n.debug_info.dwo contents:\n";
117 for (unsigned i = 0, e = getNumDWOCompileUnits(); i != e; ++i)
118 getDWOCompileUnitAtIndex(i)->dump(OS);
119 }
120
121 if (DumpType == DIDT_All || DumpType == DIDT_StrDwo) {
122 OS << "\n.debug_str.dwo contents:\n";
123 DataExtractor strDWOData(getStringDWOSection(), isLittleEndian(), 0);
124 offset = 0;
125 uint32_t strDWOOffset = 0;
126 while (const char *s = strDWOData.getCStr(&offset)) {
127 OS << format("0x%8.8x: \"%s\"\n", strDWOOffset, s);
128 strDWOOffset = offset;
129 }
130 }
131
132 if (DumpType == DIDT_All || DumpType == DIDT_StrOffsetsDwo) {
133 OS << "\n.debug_str_offsets.dwo contents:\n";
134 DataExtractor strOffsetExt(getStringOffsetDWOSection(), isLittleEndian(), 0);
135 offset = 0;
136 while (offset < getStringOffsetDWOSection().size()) {
137 OS << format("0x%8.8x: ", offset);
138 OS << format("%8.8x\n", strOffsetExt.getU32(&offset));
139 }
Eric Christopher72f7bfb2013-01-15 23:56:56 +0000140 }
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000141}
142
143const DWARFDebugAbbrev *DWARFContext::getDebugAbbrev() {
144 if (Abbrev)
145 return Abbrev.get();
146
147 DataExtractor abbrData(getAbbrevSection(), isLittleEndian(), 0);
148
149 Abbrev.reset(new DWARFDebugAbbrev());
150 Abbrev->parse(abbrData);
151 return Abbrev.get();
152}
153
Eric Christopher82de10a2013-01-02 23:52:13 +0000154const DWARFDebugAbbrev *DWARFContext::getDebugAbbrevDWO() {
155 if (AbbrevDWO)
156 return AbbrevDWO.get();
157
158 DataExtractor abbrData(getAbbrevDWOSection(), isLittleEndian(), 0);
159 AbbrevDWO.reset(new DWARFDebugAbbrev());
160 AbbrevDWO->parse(abbrData);
161 return AbbrevDWO.get();
162}
163
Benjamin Kramer358f4fd2011-09-14 01:09:52 +0000164const DWARFDebugAranges *DWARFContext::getDebugAranges() {
165 if (Aranges)
166 return Aranges.get();
167
168 DataExtractor arangesData(getARangeSection(), isLittleEndian(), 0);
169
170 Aranges.reset(new DWARFDebugAranges());
171 Aranges->extract(arangesData);
Alexey Samsonov63a450a2012-11-16 08:36:25 +0000172 // Generate aranges from DIEs: even if .debug_aranges section is present,
173 // it may describe only a small subset of compilation units, so we need to
174 // manually build aranges for the rest of them.
175 Aranges->generate(this);
Benjamin Kramer358f4fd2011-09-14 01:09:52 +0000176 return Aranges.get();
177}
178
Eli Bendersky60bdc5b2013-02-05 23:30:58 +0000179const DWARFDebugFrame *DWARFContext::getDebugFrame() {
180 if (DebugFrame)
181 return DebugFrame.get();
182
183 // There's a "bug" in the DWARFv3 standard with respect to the target address
184 // size within debug frame sections. While DWARF is supposed to be independent
185 // of its container, FDEs have fields with size being "target address size",
186 // which isn't specified in DWARF in general. It's only specified for CUs, but
187 // .eh_frame can appear without a .debug_info section. Follow the example of
188 // other tools (libdwarf) and extract this from the container (ObjectFile
189 // provides this information). This problem is fixed in DWARFv4
190 // See this dwarf-discuss discussion for more details:
191 // http://lists.dwarfstd.org/htdig.cgi/dwarf-discuss-dwarfstd.org/2011-December/001173.html
192 DataExtractor debugFrameData(getDebugFrameSection(), isLittleEndian(),
193 getAddressSize());
194 DebugFrame.reset(new DWARFDebugFrame());
195 DebugFrame->parse(debugFrameData);
196 return DebugFrame.get();
197}
198
Eric Christophere9403c12012-10-16 23:46:25 +0000199const DWARFLineTable *
Benjamin Kramerc26ed9b2011-09-15 20:43:18 +0000200DWARFContext::getLineTableForCompileUnit(DWARFCompileUnit *cu) {
201 if (!Line)
Andrew Kayloree7c0d22013-01-25 22:50:58 +0000202 Line.reset(new DWARFDebugLine(&lineRelocMap()));
Benjamin Kramerb848e972011-09-15 02:12:05 +0000203
Benjamin Kramerc26ed9b2011-09-15 20:43:18 +0000204 unsigned stmtOffset =
205 cu->getCompileUnitDIE()->getAttributeValueAsUnsigned(cu, DW_AT_stmt_list,
206 -1U);
207 if (stmtOffset == -1U)
208 return 0; // No line table for this compile unit.
Benjamin Kramerb848e972011-09-15 02:12:05 +0000209
Benjamin Kramerc26ed9b2011-09-15 20:43:18 +0000210 // See if the line table is cached.
Eric Christophere9403c12012-10-16 23:46:25 +0000211 if (const DWARFLineTable *lt = Line->getLineTable(stmtOffset))
Benjamin Kramerc26ed9b2011-09-15 20:43:18 +0000212 return lt;
213
214 // We have to parse it first.
215 DataExtractor lineData(getLineSection(), isLittleEndian(),
216 cu->getAddressByteSize());
217 return Line->getOrParseLineTable(lineData, stmtOffset);
Benjamin Kramerb848e972011-09-15 02:12:05 +0000218}
219
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000220void DWARFContext::parseCompileUnits() {
221 uint32_t offset = 0;
Eric Christopherb69b55f2012-10-16 23:46:23 +0000222 const DataExtractor &DIData = DataExtractor(getInfoSection(),
223 isLittleEndian(), 0);
224 while (DIData.isValidOffset(offset)) {
Eric Christopher82de10a2013-01-02 23:52:13 +0000225 CUs.push_back(DWARFCompileUnit(getDebugAbbrev(), getInfoSection(),
226 getAbbrevSection(), getRangeSection(),
Eric Christopher72f7bfb2013-01-15 23:56:56 +0000227 getStringSection(), StringRef(),
228 getAddrSection(),
Eric Christopherdd8e9f32013-01-07 19:32:41 +0000229 &infoRelocMap(),
Eric Christopher82de10a2013-01-02 23:52:13 +0000230 isLittleEndian()));
Eric Christopherb69b55f2012-10-16 23:46:23 +0000231 if (!CUs.back().extract(DIData, &offset)) {
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000232 CUs.pop_back();
233 break;
234 }
235
236 offset = CUs.back().getNextCompileUnitOffset();
237 }
238}
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000239
Eric Christopher82de10a2013-01-02 23:52:13 +0000240void DWARFContext::parseDWOCompileUnits() {
241 uint32_t offset = 0;
242 const DataExtractor &DIData = DataExtractor(getInfoDWOSection(),
243 isLittleEndian(), 0);
244 while (DIData.isValidOffset(offset)) {
245 DWOCUs.push_back(DWARFCompileUnit(getDebugAbbrevDWO(), getInfoDWOSection(),
246 getAbbrevDWOSection(),
247 getRangeDWOSection(),
248 getStringDWOSection(),
Eric Christopherdd8e9f32013-01-07 19:32:41 +0000249 getStringOffsetDWOSection(),
Eric Christopher72f7bfb2013-01-15 23:56:56 +0000250 getAddrSection(),
Eric Christopher82de10a2013-01-02 23:52:13 +0000251 &infoDWORelocMap(),
252 isLittleEndian()));
253 if (!DWOCUs.back().extract(DIData, &offset)) {
254 DWOCUs.pop_back();
255 break;
256 }
257
258 offset = DWOCUs.back().getNextCompileUnitOffset();
259 }
260}
261
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000262namespace {
263 struct OffsetComparator {
264 bool operator()(const DWARFCompileUnit &LHS,
265 const DWARFCompileUnit &RHS) const {
266 return LHS.getOffset() < RHS.getOffset();
267 }
268 bool operator()(const DWARFCompileUnit &LHS, uint32_t RHS) const {
269 return LHS.getOffset() < RHS;
270 }
271 bool operator()(uint32_t LHS, const DWARFCompileUnit &RHS) const {
272 return LHS < RHS.getOffset();
273 }
274 };
275}
276
Alexey Samsonov38a63812012-08-30 07:49:50 +0000277DWARFCompileUnit *DWARFContext::getCompileUnitForOffset(uint32_t Offset) {
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000278 if (CUs.empty())
279 parseCompileUnits();
280
Alexey Samsonov38a63812012-08-30 07:49:50 +0000281 DWARFCompileUnit *CU = std::lower_bound(CUs.begin(), CUs.end(), Offset,
282 OffsetComparator());
283 if (CU != CUs.end())
284 return &*CU;
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000285 return 0;
286}
287
Alexey Samsonov38a63812012-08-30 07:49:50 +0000288DWARFCompileUnit *DWARFContext::getCompileUnitForAddress(uint64_t Address) {
Benjamin Kramer9013db32011-09-15 21:59:13 +0000289 // First, get the offset of the compile unit.
Alexey Samsonov38a63812012-08-30 07:49:50 +0000290 uint32_t CUOffset = getDebugAranges()->findAddress(Address);
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000291 // Retrieve the compile unit.
Alexey Samsonov38a63812012-08-30 07:49:50 +0000292 return getCompileUnitForOffset(CUOffset);
293}
294
Eric Christophere9403c12012-10-16 23:46:25 +0000295static bool getFileNameForCompileUnit(DWARFCompileUnit *CU,
296 const DWARFLineTable *LineTable,
297 uint64_t FileIndex,
298 bool NeedsAbsoluteFilePath,
299 std::string &FileName) {
Alexey Samsonov38a63812012-08-30 07:49:50 +0000300 if (CU == 0 ||
301 LineTable == 0 ||
302 !LineTable->getFileNameByIndex(FileIndex, NeedsAbsoluteFilePath,
303 FileName))
304 return false;
305 if (NeedsAbsoluteFilePath && sys::path::is_relative(FileName)) {
306 // We may still need to append compilation directory of compile unit.
307 SmallString<16> AbsolutePath;
308 if (const char *CompilationDir = CU->getCompilationDir()) {
309 sys::path::append(AbsolutePath, CompilationDir);
310 }
311 sys::path::append(AbsolutePath, FileName);
312 FileName = AbsolutePath.str();
313 }
314 return true;
315}
316
Eric Christophere9403c12012-10-16 23:46:25 +0000317static bool getFileLineInfoForCompileUnit(DWARFCompileUnit *CU,
318 const DWARFLineTable *LineTable,
319 uint64_t Address,
320 bool NeedsAbsoluteFilePath,
321 std::string &FileName,
322 uint32_t &Line, uint32_t &Column) {
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000323 if (CU == 0 || LineTable == 0)
Alexey Samsonov38a63812012-08-30 07:49:50 +0000324 return false;
325 // Get the index of row we're looking for in the line table.
326 uint32_t RowIndex = LineTable->lookupAddress(Address);
327 if (RowIndex == -1U)
328 return false;
329 // Take file number and line/column from the row.
330 const DWARFDebugLine::Row &Row = LineTable->Rows[RowIndex];
331 if (!getFileNameForCompileUnit(CU, LineTable, Row.File,
332 NeedsAbsoluteFilePath, FileName))
333 return false;
334 Line = Row.Line;
335 Column = Row.Column;
336 return true;
337}
338
339DILineInfo DWARFContext::getLineInfoForAddress(uint64_t Address,
340 DILineInfoSpecifier Specifier) {
341 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
342 if (!CU)
Alexey Samsonov3e25c4a2012-07-02 05:54:45 +0000343 return DILineInfo();
Alexey Samsonov38a63812012-08-30 07:49:50 +0000344 std::string FileName = "<invalid>";
345 std::string FunctionName = "<invalid>";
346 uint32_t Line = 0;
347 uint32_t Column = 0;
348 if (Specifier.needs(DILineInfoSpecifier::FunctionName)) {
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000349 // The address may correspond to instruction in some inlined function,
350 // so we have to build the chain of inlined functions and take the
351 // name of the topmost function in it.
352 const DWARFDebugInfoEntryMinimal::InlinedChain &InlinedChain =
353 CU->getInlinedChainForAddress(Address);
354 if (InlinedChain.size() > 0) {
355 const DWARFDebugInfoEntryMinimal &TopFunctionDIE = InlinedChain[0];
356 if (const char *Name = TopFunctionDIE.getSubroutineName(CU))
Alexey Samsonov38a63812012-08-30 07:49:50 +0000357 FunctionName = Name;
Alexey Samsonov9d26b0b2012-07-17 15:28:35 +0000358 }
Alexey Samsonov3e25c4a2012-07-02 05:54:45 +0000359 }
Alexey Samsonov38a63812012-08-30 07:49:50 +0000360 if (Specifier.needs(DILineInfoSpecifier::FileLineInfo)) {
Eric Christophere9403c12012-10-16 23:46:25 +0000361 const DWARFLineTable *LineTable = getLineTableForCompileUnit(CU);
Alexey Samsonov38a63812012-08-30 07:49:50 +0000362 const bool NeedsAbsoluteFilePath =
363 Specifier.needs(DILineInfoSpecifier::AbsoluteFilePath);
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000364 getFileLineInfoForCompileUnit(CU, LineTable, Address,
365 NeedsAbsoluteFilePath,
Alexey Samsonov38a63812012-08-30 07:49:50 +0000366 FileName, Line, Column);
Alexey Samsonov3e25c4a2012-07-02 05:54:45 +0000367 }
Alexey Samsonov38a63812012-08-30 07:49:50 +0000368 return DILineInfo(StringRef(FileName), StringRef(FunctionName),
369 Line, Column);
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000370}
David Blaikie2d24e2a2011-12-20 02:50:00 +0000371
Andrew Kaylore27a7872013-01-26 00:28:05 +0000372DILineInfoTable DWARFContext::getLineInfoForAddressRange(uint64_t Address,
373 uint64_t Size,
374 DILineInfoSpecifier Specifier) {
375 DILineInfoTable Lines;
376 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
377 if (!CU)
378 return Lines;
379
380 std::string FunctionName = "<invalid>";
381 if (Specifier.needs(DILineInfoSpecifier::FunctionName)) {
382 // The address may correspond to instruction in some inlined function,
383 // so we have to build the chain of inlined functions and take the
384 // name of the topmost function in it.
385 const DWARFDebugInfoEntryMinimal::InlinedChain &InlinedChain =
386 CU->getInlinedChainForAddress(Address);
387 if (InlinedChain.size() > 0) {
388 const DWARFDebugInfoEntryMinimal &TopFunctionDIE = InlinedChain[0];
389 if (const char *Name = TopFunctionDIE.getSubroutineName(CU))
390 FunctionName = Name;
391 }
392 }
393
394 StringRef FuncNameRef = StringRef(FunctionName);
395
396 // If the Specifier says we don't need FileLineInfo, just
397 // return the top-most function at the starting address.
398 if (!Specifier.needs(DILineInfoSpecifier::FileLineInfo)) {
399 Lines.push_back(std::make_pair(Address,
400 DILineInfo(StringRef("<invalid>"),
401 FuncNameRef, 0, 0)));
402 return Lines;
403 }
404
405 const DWARFLineTable *LineTable = getLineTableForCompileUnit(CU);
406 const bool NeedsAbsoluteFilePath =
407 Specifier.needs(DILineInfoSpecifier::AbsoluteFilePath);
408
409 // Get the index of row we're looking for in the line table.
410 std::vector<uint32_t> RowVector;
411 if (!LineTable->lookupAddressRange(Address, Size, RowVector))
412 return Lines;
413
414 uint32_t NumRows = RowVector.size();
415 for (uint32_t i = 0; i < NumRows; ++i) {
416 uint32_t RowIndex = RowVector[i];
417 // Take file number and line/column from the row.
418 const DWARFDebugLine::Row &Row = LineTable->Rows[RowIndex];
419 std::string FileName = "<invalid>";
420 getFileNameForCompileUnit(CU, LineTable, Row.File,
421 NeedsAbsoluteFilePath, FileName);
422 Lines.push_back(std::make_pair(Row.Address,
423 DILineInfo(StringRef(FileName),
424 FuncNameRef, Row.Line, Row.Column)));
425 }
426
427 return Lines;
428}
429
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000430DIInliningInfo DWARFContext::getInliningInfoForAddress(uint64_t Address,
431 DILineInfoSpecifier Specifier) {
432 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
433 if (!CU)
434 return DIInliningInfo();
435
436 const DWARFDebugInfoEntryMinimal::InlinedChain &InlinedChain =
437 CU->getInlinedChainForAddress(Address);
438 if (InlinedChain.size() == 0)
439 return DIInliningInfo();
440
441 DIInliningInfo InliningInfo;
442 uint32_t CallFile = 0, CallLine = 0, CallColumn = 0;
Eric Christophere9403c12012-10-16 23:46:25 +0000443 const DWARFLineTable *LineTable = 0;
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000444 for (uint32_t i = 0, n = InlinedChain.size(); i != n; i++) {
445 const DWARFDebugInfoEntryMinimal &FunctionDIE = InlinedChain[i];
446 std::string FileName = "<invalid>";
447 std::string FunctionName = "<invalid>";
448 uint32_t Line = 0;
449 uint32_t Column = 0;
450 // Get function name if necessary.
451 if (Specifier.needs(DILineInfoSpecifier::FunctionName)) {
452 if (const char *Name = FunctionDIE.getSubroutineName(CU))
453 FunctionName = Name;
454 }
455 if (Specifier.needs(DILineInfoSpecifier::FileLineInfo)) {
456 const bool NeedsAbsoluteFilePath =
457 Specifier.needs(DILineInfoSpecifier::AbsoluteFilePath);
458 if (i == 0) {
459 // For the topmost frame, initialize the line table of this
460 // compile unit and fetch file/line info from it.
461 LineTable = getLineTableForCompileUnit(CU);
462 // For the topmost routine, get file/line info from line table.
463 getFileLineInfoForCompileUnit(CU, LineTable, Address,
464 NeedsAbsoluteFilePath,
465 FileName, Line, Column);
466 } else {
467 // Otherwise, use call file, call line and call column from
468 // previous DIE in inlined chain.
469 getFileNameForCompileUnit(CU, LineTable, CallFile,
470 NeedsAbsoluteFilePath, FileName);
471 Line = CallLine;
472 Column = CallColumn;
473 }
474 // Get call file/line/column of a current DIE.
475 if (i + 1 < n) {
476 FunctionDIE.getCallerFrame(CU, CallFile, CallLine, CallColumn);
477 }
478 }
479 DILineInfo Frame(StringRef(FileName), StringRef(FunctionName),
480 Line, Column);
481 InliningInfo.addFrame(Frame);
482 }
483 return InliningInfo;
484}
485
Eric Christopherd1726a42012-11-12 21:40:38 +0000486DWARFContextInMemory::DWARFContextInMemory(object::ObjectFile *Obj) :
Eli Bendersky60bdc5b2013-02-05 23:30:58 +0000487 IsLittleEndian(Obj->isLittleEndian()),
488 AddressSize(Obj->getBytesInAddress()) {
Eric Christopherd1726a42012-11-12 21:40:38 +0000489 error_code ec;
490 for (object::section_iterator i = Obj->begin_sections(),
491 e = Obj->end_sections();
492 i != e; i.increment(ec)) {
493 StringRef name;
494 i->getName(name);
495 StringRef data;
496 i->getContents(data);
497
Eric Christopherd1726a42012-11-12 21:40:38 +0000498 name = name.substr(name.find_first_not_of("._")); // Skip . and _ prefixes.
Alexey Samsonov784baa62013-04-17 14:27:04 +0000499
500 StringRef *Section = StringSwitch<StringRef*>(name)
501 .Case("debug_info", &InfoSection)
502 .Case("debug_abbrev", &AbbrevSection)
503 .Case("debug_line", &LineSection)
504 .Case("debug_aranges", &ARangeSection)
505 .Case("debug_frame", &DebugFrameSection)
506 .Case("debug_str", &StringSection)
507 .Case("debug_ranges", &RangeSection)
508 .Case("debug_pubnames", &PubNamesSection)
509 .Case("debug_info.dwo", &InfoDWOSection)
510 .Case("debug_abbrev.dwo", &AbbrevDWOSection)
511 .Case("debug_str.dwo", &StringDWOSection)
512 .Case("debug_str_offsets.dwo", &StringOffsetDWOSection)
513 .Case("debug_addr", &AddrSection)
514 // Any more debug info sections go here.
515 .Default(0);
516 if (!Section)
517 continue;
518 *Section = data;
519 if (name == "debug_ranges") {
Eric Christopher82de10a2013-01-02 23:52:13 +0000520 // FIXME: Use the other dwo range section when we emit it.
521 RangeDWOSection = data;
Eric Christopher82de10a2013-01-02 23:52:13 +0000522 }
Eric Christopherd1726a42012-11-12 21:40:38 +0000523
Andrew Kayloree7c0d22013-01-25 22:50:58 +0000524 // TODO: Add support for relocations in other sections as needed.
525 // Record relocations for the debug_info and debug_line sections.
Alexey Samsonov784baa62013-04-17 14:27:04 +0000526 RelocAddrMap *Map = StringSwitch<RelocAddrMap*>(name)
527 .Case("debug_info", &InfoRelocMap)
528 .Case("debug_info.dwo", &InfoDWORelocMap)
529 .Case("debug_line", &LineRelocMap)
530 .Default(0);
531 if (!Map)
Eric Christopherd1726a42012-11-12 21:40:38 +0000532 continue;
533
534 if (i->begin_relocations() != i->end_relocations()) {
535 uint64_t SectionSize;
536 i->getSize(SectionSize);
537 for (object::relocation_iterator reloc_i = i->begin_relocations(),
538 reloc_e = i->end_relocations();
539 reloc_i != reloc_e; reloc_i.increment(ec)) {
540 uint64_t Address;
541 reloc_i->getAddress(Address);
542 uint64_t Type;
543 reloc_i->getType(Type);
Andrew Kayloree7c0d22013-01-25 22:50:58 +0000544 uint64_t SymAddr = 0;
545 // ELF relocations may need the symbol address
546 if (Obj->isELF()) {
547 object::SymbolRef Sym;
548 reloc_i->getSymbol(Sym);
549 Sym.getAddress(SymAddr);
550 }
Eric Christopherd1726a42012-11-12 21:40:38 +0000551
552 object::RelocVisitor V(Obj->getFileFormatName());
553 // The section address is always 0 for debug sections.
Andrew Kayloree7c0d22013-01-25 22:50:58 +0000554 object::RelocToApply R(V.visit(Type, *reloc_i, 0, SymAddr));
Eric Christopherd1726a42012-11-12 21:40:38 +0000555 if (V.error()) {
556 SmallString<32> Name;
557 error_code ec(reloc_i->getTypeName(Name));
558 if (ec) {
559 errs() << "Aaaaaa! Nameless relocation! Aaaaaa!\n";
560 }
561 errs() << "error: failed to compute relocation: "
562 << Name << "\n";
563 continue;
564 }
565
566 if (Address + R.Width > SectionSize) {
567 errs() << "error: " << R.Width << "-byte relocation starting "
568 << Address << " bytes into section " << name << " which is "
569 << SectionSize << " bytes long.\n";
570 continue;
571 }
572 if (R.Width > 8) {
573 errs() << "error: can't handle a relocation of more than 8 bytes at "
574 "a time.\n";
575 continue;
576 }
577 DEBUG(dbgs() << "Writing " << format("%p", R.Value)
578 << " at " << format("%p", Address)
579 << " with width " << format("%d", R.Width)
580 << "\n");
Eric Christopher82de10a2013-01-02 23:52:13 +0000581 Map->insert(std::make_pair(Address, std::make_pair(R.Width, R.Value)));
Eric Christopherd1726a42012-11-12 21:40:38 +0000582 }
583 }
584 }
585}
586
David Blaikie2d24e2a2011-12-20 02:50:00 +0000587void DWARFContextInMemory::anchor() { }