blob: 9e67b231167ccb6c22a6df240ef3f1f656e8651b [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;
56 uint32_t lastOffset = 0;
57 while (const char *s = strData.getCStr(&offset)) {
58 OS << format("0x%8.8x: \"%s\"\n", lastOffset, s);
59 lastOffset = offset;
60 }
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);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000073}
74
75const DWARFDebugAbbrev *DWARFContext::getDebugAbbrev() {
76 if (Abbrev)
77 return Abbrev.get();
78
79 DataExtractor abbrData(getAbbrevSection(), isLittleEndian(), 0);
80
81 Abbrev.reset(new DWARFDebugAbbrev());
82 Abbrev->parse(abbrData);
83 return Abbrev.get();
84}
85
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000086const DWARFDebugAranges *DWARFContext::getDebugAranges() {
87 if (Aranges)
88 return Aranges.get();
89
90 DataExtractor arangesData(getARangeSection(), isLittleEndian(), 0);
91
92 Aranges.reset(new DWARFDebugAranges());
93 Aranges->extract(arangesData);
Alexey Samsonov63a450a2012-11-16 08:36:25 +000094 // Generate aranges from DIEs: even if .debug_aranges section is present,
95 // it may describe only a small subset of compilation units, so we need to
96 // manually build aranges for the rest of them.
97 Aranges->generate(this);
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000098 return Aranges.get();
99}
100
Eric Christophere9403c12012-10-16 23:46:25 +0000101const DWARFLineTable *
Benjamin Kramerc26ed9b2011-09-15 20:43:18 +0000102DWARFContext::getLineTableForCompileUnit(DWARFCompileUnit *cu) {
103 if (!Line)
104 Line.reset(new DWARFDebugLine());
Benjamin Kramerb848e972011-09-15 02:12:05 +0000105
Benjamin Kramerc26ed9b2011-09-15 20:43:18 +0000106 unsigned stmtOffset =
107 cu->getCompileUnitDIE()->getAttributeValueAsUnsigned(cu, DW_AT_stmt_list,
108 -1U);
109 if (stmtOffset == -1U)
110 return 0; // No line table for this compile unit.
Benjamin Kramerb848e972011-09-15 02:12:05 +0000111
Benjamin Kramerc26ed9b2011-09-15 20:43:18 +0000112 // See if the line table is cached.
Eric Christophere9403c12012-10-16 23:46:25 +0000113 if (const DWARFLineTable *lt = Line->getLineTable(stmtOffset))
Benjamin Kramerc26ed9b2011-09-15 20:43:18 +0000114 return lt;
115
116 // We have to parse it first.
117 DataExtractor lineData(getLineSection(), isLittleEndian(),
118 cu->getAddressByteSize());
119 return Line->getOrParseLineTable(lineData, stmtOffset);
Benjamin Kramerb848e972011-09-15 02:12:05 +0000120}
121
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000122void DWARFContext::parseCompileUnits() {
123 uint32_t offset = 0;
Eric Christopherb69b55f2012-10-16 23:46:23 +0000124 const DataExtractor &DIData = DataExtractor(getInfoSection(),
125 isLittleEndian(), 0);
126 while (DIData.isValidOffset(offset)) {
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000127 CUs.push_back(DWARFCompileUnit(*this));
Eric Christopherb69b55f2012-10-16 23:46:23 +0000128 if (!CUs.back().extract(DIData, &offset)) {
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000129 CUs.pop_back();
130 break;
131 }
132
133 offset = CUs.back().getNextCompileUnitOffset();
134 }
135}
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000136
137namespace {
138 struct OffsetComparator {
139 bool operator()(const DWARFCompileUnit &LHS,
140 const DWARFCompileUnit &RHS) const {
141 return LHS.getOffset() < RHS.getOffset();
142 }
143 bool operator()(const DWARFCompileUnit &LHS, uint32_t RHS) const {
144 return LHS.getOffset() < RHS;
145 }
146 bool operator()(uint32_t LHS, const DWARFCompileUnit &RHS) const {
147 return LHS < RHS.getOffset();
148 }
149 };
150}
151
Alexey Samsonov38a63812012-08-30 07:49:50 +0000152DWARFCompileUnit *DWARFContext::getCompileUnitForOffset(uint32_t Offset) {
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000153 if (CUs.empty())
154 parseCompileUnits();
155
Alexey Samsonov38a63812012-08-30 07:49:50 +0000156 DWARFCompileUnit *CU = std::lower_bound(CUs.begin(), CUs.end(), Offset,
157 OffsetComparator());
158 if (CU != CUs.end())
159 return &*CU;
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000160 return 0;
161}
162
Alexey Samsonov38a63812012-08-30 07:49:50 +0000163DWARFCompileUnit *DWARFContext::getCompileUnitForAddress(uint64_t Address) {
Benjamin Kramer9013db32011-09-15 21:59:13 +0000164 // First, get the offset of the compile unit.
Alexey Samsonov38a63812012-08-30 07:49:50 +0000165 uint32_t CUOffset = getDebugAranges()->findAddress(Address);
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000166 // Retrieve the compile unit.
Alexey Samsonov38a63812012-08-30 07:49:50 +0000167 return getCompileUnitForOffset(CUOffset);
168}
169
Eric Christophere9403c12012-10-16 23:46:25 +0000170static bool getFileNameForCompileUnit(DWARFCompileUnit *CU,
171 const DWARFLineTable *LineTable,
172 uint64_t FileIndex,
173 bool NeedsAbsoluteFilePath,
174 std::string &FileName) {
Alexey Samsonov38a63812012-08-30 07:49:50 +0000175 if (CU == 0 ||
176 LineTable == 0 ||
177 !LineTable->getFileNameByIndex(FileIndex, NeedsAbsoluteFilePath,
178 FileName))
179 return false;
180 if (NeedsAbsoluteFilePath && sys::path::is_relative(FileName)) {
181 // We may still need to append compilation directory of compile unit.
182 SmallString<16> AbsolutePath;
183 if (const char *CompilationDir = CU->getCompilationDir()) {
184 sys::path::append(AbsolutePath, CompilationDir);
185 }
186 sys::path::append(AbsolutePath, FileName);
187 FileName = AbsolutePath.str();
188 }
189 return true;
190}
191
Eric Christophere9403c12012-10-16 23:46:25 +0000192static bool getFileLineInfoForCompileUnit(DWARFCompileUnit *CU,
193 const DWARFLineTable *LineTable,
194 uint64_t Address,
195 bool NeedsAbsoluteFilePath,
196 std::string &FileName,
197 uint32_t &Line, uint32_t &Column) {
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000198 if (CU == 0 || LineTable == 0)
Alexey Samsonov38a63812012-08-30 07:49:50 +0000199 return false;
200 // Get the index of row we're looking for in the line table.
201 uint32_t RowIndex = LineTable->lookupAddress(Address);
202 if (RowIndex == -1U)
203 return false;
204 // Take file number and line/column from the row.
205 const DWARFDebugLine::Row &Row = LineTable->Rows[RowIndex];
206 if (!getFileNameForCompileUnit(CU, LineTable, Row.File,
207 NeedsAbsoluteFilePath, FileName))
208 return false;
209 Line = Row.Line;
210 Column = Row.Column;
211 return true;
212}
213
214DILineInfo DWARFContext::getLineInfoForAddress(uint64_t Address,
215 DILineInfoSpecifier Specifier) {
216 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
217 if (!CU)
Alexey Samsonov3e25c4a2012-07-02 05:54:45 +0000218 return DILineInfo();
Alexey Samsonov38a63812012-08-30 07:49:50 +0000219 std::string FileName = "<invalid>";
220 std::string FunctionName = "<invalid>";
221 uint32_t Line = 0;
222 uint32_t Column = 0;
223 if (Specifier.needs(DILineInfoSpecifier::FunctionName)) {
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000224 // The address may correspond to instruction in some inlined function,
225 // so we have to build the chain of inlined functions and take the
226 // name of the topmost function in it.
227 const DWARFDebugInfoEntryMinimal::InlinedChain &InlinedChain =
228 CU->getInlinedChainForAddress(Address);
229 if (InlinedChain.size() > 0) {
230 const DWARFDebugInfoEntryMinimal &TopFunctionDIE = InlinedChain[0];
231 if (const char *Name = TopFunctionDIE.getSubroutineName(CU))
Alexey Samsonov38a63812012-08-30 07:49:50 +0000232 FunctionName = Name;
Alexey Samsonov9d26b0b2012-07-17 15:28:35 +0000233 }
Alexey Samsonov3e25c4a2012-07-02 05:54:45 +0000234 }
Alexey Samsonov38a63812012-08-30 07:49:50 +0000235 if (Specifier.needs(DILineInfoSpecifier::FileLineInfo)) {
Eric Christophere9403c12012-10-16 23:46:25 +0000236 const DWARFLineTable *LineTable = getLineTableForCompileUnit(CU);
Alexey Samsonov38a63812012-08-30 07:49:50 +0000237 const bool NeedsAbsoluteFilePath =
238 Specifier.needs(DILineInfoSpecifier::AbsoluteFilePath);
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000239 getFileLineInfoForCompileUnit(CU, LineTable, Address,
240 NeedsAbsoluteFilePath,
Alexey Samsonov38a63812012-08-30 07:49:50 +0000241 FileName, Line, Column);
Alexey Samsonov3e25c4a2012-07-02 05:54:45 +0000242 }
Alexey Samsonov38a63812012-08-30 07:49:50 +0000243 return DILineInfo(StringRef(FileName), StringRef(FunctionName),
244 Line, Column);
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000245}
David Blaikie2d24e2a2011-12-20 02:50:00 +0000246
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000247DIInliningInfo DWARFContext::getInliningInfoForAddress(uint64_t Address,
248 DILineInfoSpecifier Specifier) {
249 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
250 if (!CU)
251 return DIInliningInfo();
252
253 const DWARFDebugInfoEntryMinimal::InlinedChain &InlinedChain =
254 CU->getInlinedChainForAddress(Address);
255 if (InlinedChain.size() == 0)
256 return DIInliningInfo();
257
258 DIInliningInfo InliningInfo;
259 uint32_t CallFile = 0, CallLine = 0, CallColumn = 0;
Eric Christophere9403c12012-10-16 23:46:25 +0000260 const DWARFLineTable *LineTable = 0;
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000261 for (uint32_t i = 0, n = InlinedChain.size(); i != n; i++) {
262 const DWARFDebugInfoEntryMinimal &FunctionDIE = InlinedChain[i];
263 std::string FileName = "<invalid>";
264 std::string FunctionName = "<invalid>";
265 uint32_t Line = 0;
266 uint32_t Column = 0;
267 // Get function name if necessary.
268 if (Specifier.needs(DILineInfoSpecifier::FunctionName)) {
269 if (const char *Name = FunctionDIE.getSubroutineName(CU))
270 FunctionName = Name;
271 }
272 if (Specifier.needs(DILineInfoSpecifier::FileLineInfo)) {
273 const bool NeedsAbsoluteFilePath =
274 Specifier.needs(DILineInfoSpecifier::AbsoluteFilePath);
275 if (i == 0) {
276 // For the topmost frame, initialize the line table of this
277 // compile unit and fetch file/line info from it.
278 LineTable = getLineTableForCompileUnit(CU);
279 // For the topmost routine, get file/line info from line table.
280 getFileLineInfoForCompileUnit(CU, LineTable, Address,
281 NeedsAbsoluteFilePath,
282 FileName, Line, Column);
283 } else {
284 // Otherwise, use call file, call line and call column from
285 // previous DIE in inlined chain.
286 getFileNameForCompileUnit(CU, LineTable, CallFile,
287 NeedsAbsoluteFilePath, FileName);
288 Line = CallLine;
289 Column = CallColumn;
290 }
291 // Get call file/line/column of a current DIE.
292 if (i + 1 < n) {
293 FunctionDIE.getCallerFrame(CU, CallFile, CallLine, CallColumn);
294 }
295 }
296 DILineInfo Frame(StringRef(FileName), StringRef(FunctionName),
297 Line, Column);
298 InliningInfo.addFrame(Frame);
299 }
300 return InliningInfo;
301}
302
Eric Christopherd1726a42012-11-12 21:40:38 +0000303DWARFContextInMemory::DWARFContextInMemory(object::ObjectFile *Obj) :
304 IsLittleEndian(true /* FIXME */) {
305 error_code ec;
306 for (object::section_iterator i = Obj->begin_sections(),
307 e = Obj->end_sections();
308 i != e; i.increment(ec)) {
309 StringRef name;
310 i->getName(name);
311 StringRef data;
312 i->getContents(data);
313
Eric Christopherd1726a42012-11-12 21:40:38 +0000314 name = name.substr(name.find_first_not_of("._")); // Skip . and _ prefixes.
315 if (name == "debug_info")
316 InfoSection = data;
317 else if (name == "debug_abbrev")
318 AbbrevSection = data;
319 else if (name == "debug_line")
320 LineSection = data;
321 else if (name == "debug_aranges")
322 ARangeSection = data;
323 else if (name == "debug_str")
324 StringSection = data;
325 else if (name == "debug_ranges")
326 RangeSection = data;
327 // Any more debug info sections go here.
328 else
329 continue;
330
331 // TODO: For now only handle relocations for the debug_info section.
332 if (name != "debug_info")
333 continue;
334
335 if (i->begin_relocations() != i->end_relocations()) {
336 uint64_t SectionSize;
337 i->getSize(SectionSize);
338 for (object::relocation_iterator reloc_i = i->begin_relocations(),
339 reloc_e = i->end_relocations();
340 reloc_i != reloc_e; reloc_i.increment(ec)) {
341 uint64_t Address;
342 reloc_i->getAddress(Address);
343 uint64_t Type;
344 reloc_i->getType(Type);
345
346 object::RelocVisitor V(Obj->getFileFormatName());
347 // The section address is always 0 for debug sections.
348 object::RelocToApply R(V.visit(Type, *reloc_i));
349 if (V.error()) {
350 SmallString<32> Name;
351 error_code ec(reloc_i->getTypeName(Name));
352 if (ec) {
353 errs() << "Aaaaaa! Nameless relocation! Aaaaaa!\n";
354 }
355 errs() << "error: failed to compute relocation: "
356 << Name << "\n";
357 continue;
358 }
359
360 if (Address + R.Width > SectionSize) {
361 errs() << "error: " << R.Width << "-byte relocation starting "
362 << Address << " bytes into section " << name << " which is "
363 << SectionSize << " bytes long.\n";
364 continue;
365 }
366 if (R.Width > 8) {
367 errs() << "error: can't handle a relocation of more than 8 bytes at "
368 "a time.\n";
369 continue;
370 }
371 DEBUG(dbgs() << "Writing " << format("%p", R.Value)
372 << " at " << format("%p", Address)
373 << " with width " << format("%d", R.Width)
374 << "\n");
375 RelocMap[Address] = std::make_pair(R.Width, R.Value);
376 }
377 }
378 }
379}
380
David Blaikie2d24e2a2011-12-20 02:50:00 +0000381void DWARFContextInMemory::anchor() { }