blob: 1270e6ec72ae5951b3595ac515cabc85d70bfedc [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"
Benjamin Kramerfe80f1d2011-09-15 18:02:20 +000012#include "llvm/Support/Dwarf.h"
Benjamin Kramer34f864f2011-09-15 16:57:13 +000013#include "llvm/Support/Format.h"
Alexey Samsonov71d94f82012-07-19 07:03:58 +000014#include "llvm/Support/Path.h"
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000015#include "llvm/Support/raw_ostream.h"
Benjamin Kramer101b1c52011-09-15 20:43:22 +000016#include <algorithm>
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000017using namespace llvm;
Benjamin Kramerfe80f1d2011-09-15 18:02:20 +000018using namespace dwarf;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000019
Eric Christophere9403c12012-10-16 23:46:25 +000020typedef DWARFDebugLine::LineTable DWARFLineTable;
21
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000022void DWARFContext::dump(raw_ostream &OS) {
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000023 OS << ".debug_abbrev contents:\n";
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000024 getDebugAbbrev()->dump(OS);
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000025
26 OS << "\n.debug_info contents:\n";
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000027 for (unsigned i = 0, e = getNumCompileUnits(); i != e; ++i)
28 getCompileUnitAtIndex(i)->dump(OS);
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000029
30 OS << "\n.debug_aranges contents:\n";
31 DataExtractor arangesData(getARangeSection(), isLittleEndian(), 0);
32 uint32_t offset = 0;
33 DWARFDebugArangeSet set;
34 while (set.extract(arangesData, &offset))
35 set.dump(OS);
Benjamin Kramerb848e972011-09-15 02:12:05 +000036
Alexey Samsonoveceb5b92012-08-27 07:17:47 +000037 uint8_t savedAddressByteSize = 0;
Eric Christophereb6363a2012-11-27 01:40:36 +000038 OS << "\n.debug_line contents:\n";
Benjamin Kramerfe80f1d2011-09-15 18:02:20 +000039 for (unsigned i = 0, e = getNumCompileUnits(); i != e; ++i) {
40 DWARFCompileUnit *cu = getCompileUnitAtIndex(i);
Alexey Samsonoveceb5b92012-08-27 07:17:47 +000041 savedAddressByteSize = cu->getAddressByteSize();
Benjamin Kramerfe80f1d2011-09-15 18:02:20 +000042 unsigned stmtOffset =
43 cu->getCompileUnitDIE()->getAttributeValueAsUnsigned(cu, DW_AT_stmt_list,
44 -1U);
45 if (stmtOffset != -1U) {
46 DataExtractor lineData(getLineSection(), isLittleEndian(),
Alexey Samsonoveceb5b92012-08-27 07:17:47 +000047 savedAddressByteSize);
Benjamin Kramerfe80f1d2011-09-15 18:02:20 +000048 DWARFDebugLine::DumpingState state(OS);
49 DWARFDebugLine::parseStatementTable(lineData, &stmtOffset, state);
50 }
51 }
Benjamin Kramer34f864f2011-09-15 16:57:13 +000052
53 OS << "\n.debug_str contents:\n";
54 DataExtractor strData(getStringSection(), isLittleEndian(), 0);
55 offset = 0;
Eric Christopher82de10a2013-01-02 23:52:13 +000056 uint32_t strOffset = 0;
Benjamin Kramer34f864f2011-09-15 16:57:13 +000057 while (const char *s = strData.getCStr(&offset)) {
Eric Christopher82de10a2013-01-02 23:52:13 +000058 OS << format("0x%8.8x: \"%s\"\n", strOffset, s);
59 strOffset = offset;
Benjamin Kramer34f864f2011-09-15 16:57:13 +000060 }
Alexey Samsonoveceb5b92012-08-27 07:17:47 +000061
62 OS << "\n.debug_ranges contents:\n";
63 // In fact, different compile units may have different address byte
64 // sizes, but for simplicity we just use the address byte size of the last
65 // compile unit (there is no easy and fast way to associate address range
66 // list and the compile unit it describes).
67 DataExtractor rangesData(getRangeSection(), isLittleEndian(),
68 savedAddressByteSize);
69 offset = 0;
70 DWARFDebugRangeList rangeList;
71 while (rangeList.extract(rangesData, &offset))
72 rangeList.dump(OS);
Eric Christopher82de10a2013-01-02 23:52:13 +000073
74 OS << "\n.debug_abbrev.dwo contents:\n";
75 getDebugAbbrevDWO()->dump(OS);
76
77 OS << "\n.debug_info.dwo contents:\n";
78 for (unsigned i = 0, e = getNumDWOCompileUnits(); i != e; ++i)
79 getDWOCompileUnitAtIndex(i)->dump(OS);
80
81 OS << "\n.debug_str.dwo contents:\n";
82 DataExtractor strDWOData(getStringDWOSection(), isLittleEndian(), 0);
83 offset = 0;
84 uint32_t strDWOOffset = 0;
85 while (const char *s = strDWOData.getCStr(&offset)) {
86 OS << format("0x%8.8x: \"%s\"\n", strDWOOffset, s);
87 strDWOOffset = offset;
88 }
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000089}
90
91const DWARFDebugAbbrev *DWARFContext::getDebugAbbrev() {
92 if (Abbrev)
93 return Abbrev.get();
94
95 DataExtractor abbrData(getAbbrevSection(), isLittleEndian(), 0);
96
97 Abbrev.reset(new DWARFDebugAbbrev());
98 Abbrev->parse(abbrData);
99 return Abbrev.get();
100}
101
Eric Christopher82de10a2013-01-02 23:52:13 +0000102const DWARFDebugAbbrev *DWARFContext::getDebugAbbrevDWO() {
103 if (AbbrevDWO)
104 return AbbrevDWO.get();
105
106 DataExtractor abbrData(getAbbrevDWOSection(), isLittleEndian(), 0);
107 AbbrevDWO.reset(new DWARFDebugAbbrev());
108 AbbrevDWO->parse(abbrData);
109 return AbbrevDWO.get();
110}
111
Benjamin Kramer358f4fd2011-09-14 01:09:52 +0000112const DWARFDebugAranges *DWARFContext::getDebugAranges() {
113 if (Aranges)
114 return Aranges.get();
115
116 DataExtractor arangesData(getARangeSection(), isLittleEndian(), 0);
117
118 Aranges.reset(new DWARFDebugAranges());
119 Aranges->extract(arangesData);
Alexey Samsonov63a450a2012-11-16 08:36:25 +0000120 // Generate aranges from DIEs: even if .debug_aranges section is present,
121 // it may describe only a small subset of compilation units, so we need to
122 // manually build aranges for the rest of them.
123 Aranges->generate(this);
Benjamin Kramer358f4fd2011-09-14 01:09:52 +0000124 return Aranges.get();
125}
126
Eric Christophere9403c12012-10-16 23:46:25 +0000127const DWARFLineTable *
Benjamin Kramerc26ed9b2011-09-15 20:43:18 +0000128DWARFContext::getLineTableForCompileUnit(DWARFCompileUnit *cu) {
129 if (!Line)
130 Line.reset(new DWARFDebugLine());
Benjamin Kramerb848e972011-09-15 02:12:05 +0000131
Benjamin Kramerc26ed9b2011-09-15 20:43:18 +0000132 unsigned stmtOffset =
133 cu->getCompileUnitDIE()->getAttributeValueAsUnsigned(cu, DW_AT_stmt_list,
134 -1U);
135 if (stmtOffset == -1U)
136 return 0; // No line table for this compile unit.
Benjamin Kramerb848e972011-09-15 02:12:05 +0000137
Benjamin Kramerc26ed9b2011-09-15 20:43:18 +0000138 // See if the line table is cached.
Eric Christophere9403c12012-10-16 23:46:25 +0000139 if (const DWARFLineTable *lt = Line->getLineTable(stmtOffset))
Benjamin Kramerc26ed9b2011-09-15 20:43:18 +0000140 return lt;
141
142 // We have to parse it first.
143 DataExtractor lineData(getLineSection(), isLittleEndian(),
144 cu->getAddressByteSize());
145 return Line->getOrParseLineTable(lineData, stmtOffset);
Benjamin Kramerb848e972011-09-15 02:12:05 +0000146}
147
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000148void DWARFContext::parseCompileUnits() {
149 uint32_t offset = 0;
Eric Christopherb69b55f2012-10-16 23:46:23 +0000150 const DataExtractor &DIData = DataExtractor(getInfoSection(),
151 isLittleEndian(), 0);
152 while (DIData.isValidOffset(offset)) {
Eric Christopher82de10a2013-01-02 23:52:13 +0000153 CUs.push_back(DWARFCompileUnit(getDebugAbbrev(), getInfoSection(),
154 getAbbrevSection(), getRangeSection(),
155 getStringSection(), &infoRelocMap(),
156 isLittleEndian()));
Eric Christopherb69b55f2012-10-16 23:46:23 +0000157 if (!CUs.back().extract(DIData, &offset)) {
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000158 CUs.pop_back();
159 break;
160 }
161
162 offset = CUs.back().getNextCompileUnitOffset();
163 }
164}
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000165
Eric Christopher82de10a2013-01-02 23:52:13 +0000166void DWARFContext::parseDWOCompileUnits() {
167 uint32_t offset = 0;
168 const DataExtractor &DIData = DataExtractor(getInfoDWOSection(),
169 isLittleEndian(), 0);
170 while (DIData.isValidOffset(offset)) {
171 DWOCUs.push_back(DWARFCompileUnit(getDebugAbbrevDWO(), getInfoDWOSection(),
172 getAbbrevDWOSection(),
173 getRangeDWOSection(),
174 getStringDWOSection(),
175 &infoDWORelocMap(),
176 isLittleEndian()));
177 if (!DWOCUs.back().extract(DIData, &offset)) {
178 DWOCUs.pop_back();
179 break;
180 }
181
182 offset = DWOCUs.back().getNextCompileUnitOffset();
183 }
184}
185
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000186namespace {
187 struct OffsetComparator {
188 bool operator()(const DWARFCompileUnit &LHS,
189 const DWARFCompileUnit &RHS) const {
190 return LHS.getOffset() < RHS.getOffset();
191 }
192 bool operator()(const DWARFCompileUnit &LHS, uint32_t RHS) const {
193 return LHS.getOffset() < RHS;
194 }
195 bool operator()(uint32_t LHS, const DWARFCompileUnit &RHS) const {
196 return LHS < RHS.getOffset();
197 }
198 };
199}
200
Alexey Samsonov38a63812012-08-30 07:49:50 +0000201DWARFCompileUnit *DWARFContext::getCompileUnitForOffset(uint32_t Offset) {
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000202 if (CUs.empty())
203 parseCompileUnits();
204
Alexey Samsonov38a63812012-08-30 07:49:50 +0000205 DWARFCompileUnit *CU = std::lower_bound(CUs.begin(), CUs.end(), Offset,
206 OffsetComparator());
207 if (CU != CUs.end())
208 return &*CU;
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000209 return 0;
210}
211
Alexey Samsonov38a63812012-08-30 07:49:50 +0000212DWARFCompileUnit *DWARFContext::getCompileUnitForAddress(uint64_t Address) {
Benjamin Kramer9013db32011-09-15 21:59:13 +0000213 // First, get the offset of the compile unit.
Alexey Samsonov38a63812012-08-30 07:49:50 +0000214 uint32_t CUOffset = getDebugAranges()->findAddress(Address);
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000215 // Retrieve the compile unit.
Alexey Samsonov38a63812012-08-30 07:49:50 +0000216 return getCompileUnitForOffset(CUOffset);
217}
218
Eric Christophere9403c12012-10-16 23:46:25 +0000219static bool getFileNameForCompileUnit(DWARFCompileUnit *CU,
220 const DWARFLineTable *LineTable,
221 uint64_t FileIndex,
222 bool NeedsAbsoluteFilePath,
223 std::string &FileName) {
Alexey Samsonov38a63812012-08-30 07:49:50 +0000224 if (CU == 0 ||
225 LineTable == 0 ||
226 !LineTable->getFileNameByIndex(FileIndex, NeedsAbsoluteFilePath,
227 FileName))
228 return false;
229 if (NeedsAbsoluteFilePath && sys::path::is_relative(FileName)) {
230 // We may still need to append compilation directory of compile unit.
231 SmallString<16> AbsolutePath;
232 if (const char *CompilationDir = CU->getCompilationDir()) {
233 sys::path::append(AbsolutePath, CompilationDir);
234 }
235 sys::path::append(AbsolutePath, FileName);
236 FileName = AbsolutePath.str();
237 }
238 return true;
239}
240
Eric Christophere9403c12012-10-16 23:46:25 +0000241static bool getFileLineInfoForCompileUnit(DWARFCompileUnit *CU,
242 const DWARFLineTable *LineTable,
243 uint64_t Address,
244 bool NeedsAbsoluteFilePath,
245 std::string &FileName,
246 uint32_t &Line, uint32_t &Column) {
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000247 if (CU == 0 || LineTable == 0)
Alexey Samsonov38a63812012-08-30 07:49:50 +0000248 return false;
249 // Get the index of row we're looking for in the line table.
250 uint32_t RowIndex = LineTable->lookupAddress(Address);
251 if (RowIndex == -1U)
252 return false;
253 // Take file number and line/column from the row.
254 const DWARFDebugLine::Row &Row = LineTable->Rows[RowIndex];
255 if (!getFileNameForCompileUnit(CU, LineTable, Row.File,
256 NeedsAbsoluteFilePath, FileName))
257 return false;
258 Line = Row.Line;
259 Column = Row.Column;
260 return true;
261}
262
263DILineInfo DWARFContext::getLineInfoForAddress(uint64_t Address,
264 DILineInfoSpecifier Specifier) {
265 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
266 if (!CU)
Alexey Samsonov3e25c4a2012-07-02 05:54:45 +0000267 return DILineInfo();
Alexey Samsonov38a63812012-08-30 07:49:50 +0000268 std::string FileName = "<invalid>";
269 std::string FunctionName = "<invalid>";
270 uint32_t Line = 0;
271 uint32_t Column = 0;
272 if (Specifier.needs(DILineInfoSpecifier::FunctionName)) {
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000273 // The address may correspond to instruction in some inlined function,
274 // so we have to build the chain of inlined functions and take the
275 // name of the topmost function in it.
276 const DWARFDebugInfoEntryMinimal::InlinedChain &InlinedChain =
277 CU->getInlinedChainForAddress(Address);
278 if (InlinedChain.size() > 0) {
279 const DWARFDebugInfoEntryMinimal &TopFunctionDIE = InlinedChain[0];
280 if (const char *Name = TopFunctionDIE.getSubroutineName(CU))
Alexey Samsonov38a63812012-08-30 07:49:50 +0000281 FunctionName = Name;
Alexey Samsonov9d26b0b2012-07-17 15:28:35 +0000282 }
Alexey Samsonov3e25c4a2012-07-02 05:54:45 +0000283 }
Alexey Samsonov38a63812012-08-30 07:49:50 +0000284 if (Specifier.needs(DILineInfoSpecifier::FileLineInfo)) {
Eric Christophere9403c12012-10-16 23:46:25 +0000285 const DWARFLineTable *LineTable = getLineTableForCompileUnit(CU);
Alexey Samsonov38a63812012-08-30 07:49:50 +0000286 const bool NeedsAbsoluteFilePath =
287 Specifier.needs(DILineInfoSpecifier::AbsoluteFilePath);
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000288 getFileLineInfoForCompileUnit(CU, LineTable, Address,
289 NeedsAbsoluteFilePath,
Alexey Samsonov38a63812012-08-30 07:49:50 +0000290 FileName, Line, Column);
Alexey Samsonov3e25c4a2012-07-02 05:54:45 +0000291 }
Alexey Samsonov38a63812012-08-30 07:49:50 +0000292 return DILineInfo(StringRef(FileName), StringRef(FunctionName),
293 Line, Column);
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000294}
David Blaikie2d24e2a2011-12-20 02:50:00 +0000295
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000296DIInliningInfo DWARFContext::getInliningInfoForAddress(uint64_t Address,
297 DILineInfoSpecifier Specifier) {
298 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
299 if (!CU)
300 return DIInliningInfo();
301
302 const DWARFDebugInfoEntryMinimal::InlinedChain &InlinedChain =
303 CU->getInlinedChainForAddress(Address);
304 if (InlinedChain.size() == 0)
305 return DIInliningInfo();
306
307 DIInliningInfo InliningInfo;
308 uint32_t CallFile = 0, CallLine = 0, CallColumn = 0;
Eric Christophere9403c12012-10-16 23:46:25 +0000309 const DWARFLineTable *LineTable = 0;
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000310 for (uint32_t i = 0, n = InlinedChain.size(); i != n; i++) {
311 const DWARFDebugInfoEntryMinimal &FunctionDIE = InlinedChain[i];
312 std::string FileName = "<invalid>";
313 std::string FunctionName = "<invalid>";
314 uint32_t Line = 0;
315 uint32_t Column = 0;
316 // Get function name if necessary.
317 if (Specifier.needs(DILineInfoSpecifier::FunctionName)) {
318 if (const char *Name = FunctionDIE.getSubroutineName(CU))
319 FunctionName = Name;
320 }
321 if (Specifier.needs(DILineInfoSpecifier::FileLineInfo)) {
322 const bool NeedsAbsoluteFilePath =
323 Specifier.needs(DILineInfoSpecifier::AbsoluteFilePath);
324 if (i == 0) {
325 // For the topmost frame, initialize the line table of this
326 // compile unit and fetch file/line info from it.
327 LineTable = getLineTableForCompileUnit(CU);
328 // For the topmost routine, get file/line info from line table.
329 getFileLineInfoForCompileUnit(CU, LineTable, Address,
330 NeedsAbsoluteFilePath,
331 FileName, Line, Column);
332 } else {
333 // Otherwise, use call file, call line and call column from
334 // previous DIE in inlined chain.
335 getFileNameForCompileUnit(CU, LineTable, CallFile,
336 NeedsAbsoluteFilePath, FileName);
337 Line = CallLine;
338 Column = CallColumn;
339 }
340 // Get call file/line/column of a current DIE.
341 if (i + 1 < n) {
342 FunctionDIE.getCallerFrame(CU, CallFile, CallLine, CallColumn);
343 }
344 }
345 DILineInfo Frame(StringRef(FileName), StringRef(FunctionName),
346 Line, Column);
347 InliningInfo.addFrame(Frame);
348 }
349 return InliningInfo;
350}
351
Eric Christopherd1726a42012-11-12 21:40:38 +0000352DWARFContextInMemory::DWARFContextInMemory(object::ObjectFile *Obj) :
353 IsLittleEndian(true /* FIXME */) {
354 error_code ec;
355 for (object::section_iterator i = Obj->begin_sections(),
356 e = Obj->end_sections();
357 i != e; i.increment(ec)) {
358 StringRef name;
359 i->getName(name);
360 StringRef data;
361 i->getContents(data);
362
Eric Christopherd1726a42012-11-12 21:40:38 +0000363 name = name.substr(name.find_first_not_of("._")); // Skip . and _ prefixes.
364 if (name == "debug_info")
365 InfoSection = data;
366 else if (name == "debug_abbrev")
367 AbbrevSection = data;
368 else if (name == "debug_line")
369 LineSection = data;
370 else if (name == "debug_aranges")
371 ARangeSection = data;
372 else if (name == "debug_str")
373 StringSection = data;
Eric Christopher82de10a2013-01-02 23:52:13 +0000374 else if (name == "debug_ranges") {
375 // FIXME: Use the other dwo range section when we emit it.
376 RangeDWOSection = data;
Eric Christopherd1726a42012-11-12 21:40:38 +0000377 RangeSection = data;
Eric Christopher82de10a2013-01-02 23:52:13 +0000378 }
379 else if (name == "debug_info.dwo")
380 InfoDWOSection = data;
381 else if (name == "debug_abbrev.dwo")
382 AbbrevDWOSection = data;
383 else if (name == "debug_str.dwo")
384 StringDWOSection = data;
Eric Christopherd1726a42012-11-12 21:40:38 +0000385 // Any more debug info sections go here.
386 else
387 continue;
388
389 // TODO: For now only handle relocations for the debug_info section.
Eric Christopher82de10a2013-01-02 23:52:13 +0000390 RelocAddrMap *Map;
391 if (name == "debug_info")
392 Map = &InfoRelocMap;
393 else if (name == "debug_info.dwo")
394 Map = &InfoDWORelocMap;
395 else
Eric Christopherd1726a42012-11-12 21:40:38 +0000396 continue;
397
398 if (i->begin_relocations() != i->end_relocations()) {
399 uint64_t SectionSize;
400 i->getSize(SectionSize);
401 for (object::relocation_iterator reloc_i = i->begin_relocations(),
402 reloc_e = i->end_relocations();
403 reloc_i != reloc_e; reloc_i.increment(ec)) {
404 uint64_t Address;
405 reloc_i->getAddress(Address);
406 uint64_t Type;
407 reloc_i->getType(Type);
408
409 object::RelocVisitor V(Obj->getFileFormatName());
410 // The section address is always 0 for debug sections.
411 object::RelocToApply R(V.visit(Type, *reloc_i));
412 if (V.error()) {
413 SmallString<32> Name;
414 error_code ec(reloc_i->getTypeName(Name));
415 if (ec) {
416 errs() << "Aaaaaa! Nameless relocation! Aaaaaa!\n";
417 }
418 errs() << "error: failed to compute relocation: "
419 << Name << "\n";
420 continue;
421 }
422
423 if (Address + R.Width > SectionSize) {
424 errs() << "error: " << R.Width << "-byte relocation starting "
425 << Address << " bytes into section " << name << " which is "
426 << SectionSize << " bytes long.\n";
427 continue;
428 }
429 if (R.Width > 8) {
430 errs() << "error: can't handle a relocation of more than 8 bytes at "
431 "a time.\n";
432 continue;
433 }
434 DEBUG(dbgs() << "Writing " << format("%p", R.Value)
435 << " at " << format("%p", Address)
436 << " with width " << format("%d", R.Width)
437 << "\n");
Eric Christopher82de10a2013-01-02 23:52:13 +0000438 Map->insert(std::make_pair(Address, std::make_pair(R.Width, R.Value)));
Eric Christopherd1726a42012-11-12 21:40:38 +0000439 }
440 }
441 }
442}
443
David Blaikie2d24e2a2011-12-20 02:50:00 +0000444void DWARFContextInMemory::anchor() { }