1 //===-- DWARFContext.cpp --------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "DWARFContext.h"
11 #include "llvm/ADT/SmallString.h"
12 #include "llvm/Support/Dwarf.h"
13 #include "llvm/Support/Format.h"
14 #include "llvm/Support/Path.h"
15 #include "llvm/Support/raw_ostream.h"
18 using namespace dwarf;
20 typedef DWARFDebugLine::LineTable DWARFLineTable;
22 void DWARFContext::dump(raw_ostream &OS) {
23 OS << ".debug_abbrev contents:\n";
24 getDebugAbbrev()->dump(OS);
26 OS << "\n.debug_info contents:\n";
27 for (unsigned i = 0, e = getNumCompileUnits(); i != e; ++i)
28 getCompileUnitAtIndex(i)->dump(OS);
30 OS << "\n.debug_aranges contents:\n";
31 DataExtractor arangesData(getARangeSection(), isLittleEndian(), 0);
33 DWARFDebugArangeSet set;
34 while (set.extract(arangesData, &offset))
37 uint8_t savedAddressByteSize = 0;
38 OS << "\n.debug_lines contents:\n";
39 for (unsigned i = 0, e = getNumCompileUnits(); i != e; ++i) {
40 DWARFCompileUnit *cu = getCompileUnitAtIndex(i);
41 savedAddressByteSize = cu->getAddressByteSize();
43 cu->getCompileUnitDIE()->getAttributeValueAsUnsigned(cu, DW_AT_stmt_list,
45 if (stmtOffset != -1U) {
46 DataExtractor lineData(getLineSection(), isLittleEndian(),
47 savedAddressByteSize);
48 DWARFDebugLine::DumpingState state(OS);
49 DWARFDebugLine::parseStatementTable(lineData, &stmtOffset, state);
53 OS << "\n.debug_str contents:\n";
54 DataExtractor strData(getStringSection(), isLittleEndian(), 0);
56 uint32_t lastOffset = 0;
57 while (const char *s = strData.getCStr(&offset)) {
58 OS << format("0x%8.8x: \"%s\"\n", lastOffset, s);
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);
70 DWARFDebugRangeList rangeList;
71 while (rangeList.extract(rangesData, &offset))
75 const DWARFDebugAbbrev *DWARFContext::getDebugAbbrev() {
79 DataExtractor abbrData(getAbbrevSection(), isLittleEndian(), 0);
81 Abbrev.reset(new DWARFDebugAbbrev());
82 Abbrev->parse(abbrData);
86 const DWARFDebugAranges *DWARFContext::getDebugAranges() {
90 DataExtractor arangesData(getARangeSection(), isLittleEndian(), 0);
92 Aranges.reset(new DWARFDebugAranges());
93 Aranges->extract(arangesData);
94 // 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);
101 const DWARFLineTable *
102 DWARFContext::getLineTableForCompileUnit(DWARFCompileUnit *cu) {
104 Line.reset(new DWARFDebugLine());
106 unsigned stmtOffset =
107 cu->getCompileUnitDIE()->getAttributeValueAsUnsigned(cu, DW_AT_stmt_list,
109 if (stmtOffset == -1U)
110 return 0; // No line table for this compile unit.
112 // See if the line table is cached.
113 if (const DWARFLineTable *lt = Line->getLineTable(stmtOffset))
116 // We have to parse it first.
117 DataExtractor lineData(getLineSection(), isLittleEndian(),
118 cu->getAddressByteSize());
119 return Line->getOrParseLineTable(lineData, stmtOffset);
122 void DWARFContext::parseCompileUnits() {
124 const DataExtractor &DIData = DataExtractor(getInfoSection(),
125 isLittleEndian(), 0);
126 while (DIData.isValidOffset(offset)) {
127 CUs.push_back(DWARFCompileUnit(*this));
128 if (!CUs.back().extract(DIData, &offset)) {
133 offset = CUs.back().getNextCompileUnitOffset();
138 struct OffsetComparator {
139 bool operator()(const DWARFCompileUnit &LHS,
140 const DWARFCompileUnit &RHS) const {
141 return LHS.getOffset() < RHS.getOffset();
143 bool operator()(const DWARFCompileUnit &LHS, uint32_t RHS) const {
144 return LHS.getOffset() < RHS;
146 bool operator()(uint32_t LHS, const DWARFCompileUnit &RHS) const {
147 return LHS < RHS.getOffset();
152 DWARFCompileUnit *DWARFContext::getCompileUnitForOffset(uint32_t Offset) {
156 DWARFCompileUnit *CU = std::lower_bound(CUs.begin(), CUs.end(), Offset,
163 DWARFCompileUnit *DWARFContext::getCompileUnitForAddress(uint64_t Address) {
164 // First, get the offset of the compile unit.
165 uint32_t CUOffset = getDebugAranges()->findAddress(Address);
166 // Retrieve the compile unit.
167 return getCompileUnitForOffset(CUOffset);
170 static bool getFileNameForCompileUnit(DWARFCompileUnit *CU,
171 const DWARFLineTable *LineTable,
173 bool NeedsAbsoluteFilePath,
174 std::string &FileName) {
177 !LineTable->getFileNameByIndex(FileIndex, NeedsAbsoluteFilePath,
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);
186 sys::path::append(AbsolutePath, FileName);
187 FileName = AbsolutePath.str();
192 static bool getFileLineInfoForCompileUnit(DWARFCompileUnit *CU,
193 const DWARFLineTable *LineTable,
195 bool NeedsAbsoluteFilePath,
196 std::string &FileName,
197 uint32_t &Line, uint32_t &Column) {
198 if (CU == 0 || LineTable == 0)
200 // Get the index of row we're looking for in the line table.
201 uint32_t RowIndex = LineTable->lookupAddress(Address);
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))
214 DILineInfo DWARFContext::getLineInfoForAddress(uint64_t Address,
215 DILineInfoSpecifier Specifier) {
216 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
219 std::string FileName = "<invalid>";
220 std::string FunctionName = "<invalid>";
223 if (Specifier.needs(DILineInfoSpecifier::FunctionName)) {
224 // 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))
235 if (Specifier.needs(DILineInfoSpecifier::FileLineInfo)) {
236 const DWARFLineTable *LineTable = getLineTableForCompileUnit(CU);
237 const bool NeedsAbsoluteFilePath =
238 Specifier.needs(DILineInfoSpecifier::AbsoluteFilePath);
239 getFileLineInfoForCompileUnit(CU, LineTable, Address,
240 NeedsAbsoluteFilePath,
241 FileName, Line, Column);
243 return DILineInfo(StringRef(FileName), StringRef(FunctionName),
247 DIInliningInfo DWARFContext::getInliningInfoForAddress(uint64_t Address,
248 DILineInfoSpecifier Specifier) {
249 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
251 return DIInliningInfo();
253 const DWARFDebugInfoEntryMinimal::InlinedChain &InlinedChain =
254 CU->getInlinedChainForAddress(Address);
255 if (InlinedChain.size() == 0)
256 return DIInliningInfo();
258 DIInliningInfo InliningInfo;
259 uint32_t CallFile = 0, CallLine = 0, CallColumn = 0;
260 const DWARFLineTable *LineTable = 0;
261 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>";
267 // Get function name if necessary.
268 if (Specifier.needs(DILineInfoSpecifier::FunctionName)) {
269 if (const char *Name = FunctionDIE.getSubroutineName(CU))
272 if (Specifier.needs(DILineInfoSpecifier::FileLineInfo)) {
273 const bool NeedsAbsoluteFilePath =
274 Specifier.needs(DILineInfoSpecifier::AbsoluteFilePath);
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);
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);
291 // Get call file/line/column of a current DIE.
293 FunctionDIE.getCallerFrame(CU, CallFile, CallLine, CallColumn);
296 DILineInfo Frame(StringRef(FileName), StringRef(FunctionName),
298 InliningInfo.addFrame(Frame);
303 DWARFContextInMemory::DWARFContextInMemory(object::ObjectFile *Obj) :
304 IsLittleEndian(true /* FIXME */) {
306 for (object::section_iterator i = Obj->begin_sections(),
307 e = Obj->end_sections();
308 i != e; i.increment(ec)) {
312 i->getContents(data);
314 if (name.startswith("__DWARF,"))
315 name = name.substr(8); // Skip "__DWARF," prefix.
316 name = name.substr(name.find_first_not_of("._")); // Skip . and _ prefixes.
317 if (name == "debug_info")
319 else if (name == "debug_abbrev")
320 AbbrevSection = data;
321 else if (name == "debug_line")
323 else if (name == "debug_aranges")
324 ARangeSection = data;
325 else if (name == "debug_str")
326 StringSection = data;
327 else if (name == "debug_ranges")
329 // Any more debug info sections go here.
333 // TODO: For now only handle relocations for the debug_info section.
334 if (name != "debug_info")
337 if (i->begin_relocations() != i->end_relocations()) {
338 uint64_t SectionSize;
339 i->getSize(SectionSize);
340 for (object::relocation_iterator reloc_i = i->begin_relocations(),
341 reloc_e = i->end_relocations();
342 reloc_i != reloc_e; reloc_i.increment(ec)) {
344 reloc_i->getAddress(Address);
346 reloc_i->getType(Type);
348 object::RelocVisitor V(Obj->getFileFormatName());
349 // The section address is always 0 for debug sections.
350 object::RelocToApply R(V.visit(Type, *reloc_i));
352 SmallString<32> Name;
353 error_code ec(reloc_i->getTypeName(Name));
355 errs() << "Aaaaaa! Nameless relocation! Aaaaaa!\n";
357 errs() << "error: failed to compute relocation: "
362 if (Address + R.Width > SectionSize) {
363 errs() << "error: " << R.Width << "-byte relocation starting "
364 << Address << " bytes into section " << name << " which is "
365 << SectionSize << " bytes long.\n";
369 errs() << "error: can't handle a relocation of more than 8 bytes at "
373 DEBUG(dbgs() << "Writing " << format("%p", R.Value)
374 << " at " << format("%p", Address)
375 << " with width " << format("%d", R.Width)
377 RelocMap[Address] = std::make_pair(R.Width, R.Value);
383 void DWARFContextInMemory::anchor() { }