blob: 075d8cddc0d26c77ab7d04990691b81416fa76c0 [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;
Rafael Espindola7486d922013-05-30 03:05:14 +000022using namespace object;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000023
Eric Christophere9403c12012-10-16 23:46:25 +000024typedef DWARFDebugLine::LineTable DWARFLineTable;
25
Alexey Samsonoveb0c1792013-08-23 06:56:01 +000026DWARFContext::~DWARFContext() {
27 DeleteContainerPointers(CUs);
28 DeleteContainerPointers(DWOCUs);
29}
30
Eli Bendersky939a4e82013-01-25 20:26:43 +000031void DWARFContext::dump(raw_ostream &OS, DIDumpType DumpType) {
32 if (DumpType == DIDT_All || DumpType == DIDT_Abbrev) {
33 OS << ".debug_abbrev contents:\n";
34 getDebugAbbrev()->dump(OS);
35 }
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000036
Eli Bendersky939a4e82013-01-25 20:26:43 +000037 if (DumpType == DIDT_All || DumpType == DIDT_Info) {
38 OS << "\n.debug_info contents:\n";
39 for (unsigned i = 0, e = getNumCompileUnits(); i != e; ++i)
40 getCompileUnitAtIndex(i)->dump(OS);
41 }
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000042
David Blaikie3df7d2f2013-06-19 21:37:13 +000043 if (DumpType == DIDT_All || DumpType == DIDT_Loc) {
44 OS << ".debug_loc contents:\n";
45 getDebugLoc()->dump(OS);
46 }
47
Eli Bendersky60bdc5b2013-02-05 23:30:58 +000048 if (DumpType == DIDT_All || DumpType == DIDT_Frames) {
49 OS << "\n.debug_frame contents:\n";
50 getDebugFrame()->dump(OS);
51 }
52
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000053 uint32_t offset = 0;
Eli Bendersky939a4e82013-01-25 20:26:43 +000054 if (DumpType == DIDT_All || DumpType == DIDT_Aranges) {
55 OS << "\n.debug_aranges contents:\n";
56 DataExtractor arangesData(getARangeSection(), isLittleEndian(), 0);
57 DWARFDebugArangeSet set;
58 while (set.extract(arangesData, &offset))
59 set.dump(OS);
60 }
Benjamin Kramerb848e972011-09-15 02:12:05 +000061
Alexey Samsonoveceb5b92012-08-27 07:17:47 +000062 uint8_t savedAddressByteSize = 0;
Eli Bendersky939a4e82013-01-25 20:26:43 +000063 if (DumpType == DIDT_All || DumpType == DIDT_Line) {
64 OS << "\n.debug_line contents:\n";
65 for (unsigned i = 0, e = getNumCompileUnits(); i != e; ++i) {
66 DWARFCompileUnit *cu = getCompileUnitAtIndex(i);
67 savedAddressByteSize = cu->getAddressByteSize();
68 unsigned stmtOffset =
69 cu->getCompileUnitDIE()->getAttributeValueAsUnsigned(cu, DW_AT_stmt_list,
70 -1U);
71 if (stmtOffset != -1U) {
David Blaikie9528b0e2013-09-23 17:42:01 +000072 DataExtractor lineData(getLineSection().Data, isLittleEndian(),
Eli Bendersky939a4e82013-01-25 20:26:43 +000073 savedAddressByteSize);
74 DWARFDebugLine::DumpingState state(OS);
David Blaikie9528b0e2013-09-23 17:42:01 +000075 DWARFDebugLine::parseStatementTable(lineData, &getLineSection().Relocs, &stmtOffset, state);
Eli Bendersky939a4e82013-01-25 20:26:43 +000076 }
Benjamin Kramerfe80f1d2011-09-15 18:02:20 +000077 }
78 }
Benjamin Kramer34f864f2011-09-15 16:57:13 +000079
Eli Bendersky939a4e82013-01-25 20:26:43 +000080 if (DumpType == DIDT_All || DumpType == DIDT_Str) {
81 OS << "\n.debug_str contents:\n";
82 DataExtractor strData(getStringSection(), isLittleEndian(), 0);
83 offset = 0;
84 uint32_t strOffset = 0;
85 while (const char *s = strData.getCStr(&offset)) {
86 OS << format("0x%8.8x: \"%s\"\n", strOffset, s);
87 strOffset = offset;
88 }
Benjamin Kramer34f864f2011-09-15 16:57:13 +000089 }
Alexey Samsonoveceb5b92012-08-27 07:17:47 +000090
Eli Bendersky939a4e82013-01-25 20:26:43 +000091 if (DumpType == DIDT_All || DumpType == DIDT_Ranges) {
92 OS << "\n.debug_ranges contents:\n";
93 // In fact, different compile units may have different address byte
94 // sizes, but for simplicity we just use the address byte size of the last
95 // compile unit (there is no easy and fast way to associate address range
96 // list and the compile unit it describes).
97 DataExtractor rangesData(getRangeSection(), isLittleEndian(),
98 savedAddressByteSize);
99 offset = 0;
100 DWARFDebugRangeList rangeList;
101 while (rangeList.extract(rangesData, &offset))
102 rangeList.dump(OS);
Eric Christopher82de10a2013-01-02 23:52:13 +0000103 }
Eric Christopher72f7bfb2013-01-15 23:56:56 +0000104
Krzysztof Parzyszeke38825f2013-02-12 16:20:28 +0000105 if (DumpType == DIDT_All || DumpType == DIDT_Pubnames) {
106 OS << "\n.debug_pubnames contents:\n";
107 DataExtractor pubNames(getPubNamesSection(), isLittleEndian(), 0);
108 offset = 0;
109 OS << "Length: " << pubNames.getU32(&offset) << "\n";
110 OS << "Version: " << pubNames.getU16(&offset) << "\n";
111 OS << "Offset in .debug_info: " << pubNames.getU32(&offset) << "\n";
112 OS << "Size: " << pubNames.getU32(&offset) << "\n";
113 OS << "\n Offset Name\n";
114 while (offset < getPubNamesSection().size()) {
115 uint32_t n = pubNames.getU32(&offset);
116 if (n == 0)
117 break;
118 OS << format("%8x ", n);
119 OS << pubNames.getCStr(&offset) << "\n";
120 }
121 }
122
David Blaikie994c37f2013-09-19 23:01:29 +0000123 if (DumpType == DIDT_All || DumpType == DIDT_GnuPubnames) {
124 OS << "\n.debug_gnu_pubnames contents:\n";
125 DataExtractor pubNames(getGnuPubNamesSection(), isLittleEndian(), 0);
126 offset = 0;
127 OS << "Length: " << pubNames.getU32(&offset) << "\n";
128 OS << "Version: " << pubNames.getU16(&offset) << "\n";
129 OS << "Offset in .debug_info: " << pubNames.getU32(&offset) << "\n";
130 OS << "Size: " << pubNames.getU32(&offset) << "\n";
131 OS << "Offset Linkage Kind Name\n";
132 while (offset < getGnuPubNamesSection().size()) {
133 uint32_t dieRef = pubNames.getU32(&offset);
134 if (dieRef == 0)
135 break;
136 PubIndexEntryDescriptor desc(pubNames.getU8(&offset));
137 OS << format("0x%8.8x ", dieRef)
Richard Smithc87f9482013-09-20 01:24:10 +0000138 << format("%-8s", dwarf::GDBIndexEntryLinkageString(desc.Linkage))
139 << ' ' << dwarf::GDBIndexEntryKindString(desc.Kind) << " \""
David Blaikie5cfcb362013-09-19 23:43:46 +0000140 << pubNames.getCStr(&offset) << "\"\n";
David Blaikie994c37f2013-09-19 23:01:29 +0000141 }
142 }
143
Eli Bendersky939a4e82013-01-25 20:26:43 +0000144 if (DumpType == DIDT_All || DumpType == DIDT_AbbrevDwo) {
Eric Christopher93f3fed2013-05-06 17:50:42 +0000145 const DWARFDebugAbbrev *D = getDebugAbbrevDWO();
146 if (D) {
147 OS << "\n.debug_abbrev.dwo contents:\n";
148 getDebugAbbrevDWO()->dump(OS);
Eli Bendersky939a4e82013-01-25 20:26:43 +0000149 }
150 }
151
Eric Christopher93f3fed2013-05-06 17:50:42 +0000152 if (DumpType == DIDT_All || DumpType == DIDT_InfoDwo)
153 if (getNumDWOCompileUnits()) {
154 OS << "\n.debug_info.dwo contents:\n";
155 for (unsigned i = 0, e = getNumDWOCompileUnits(); i != e; ++i)
Eric Christopher5a0c3662013-05-06 21:19:41 +0000156 getDWOCompileUnitAtIndex(i)->dump(OS);
Eli Bendersky939a4e82013-01-25 20:26:43 +0000157 }
Eric Christopher93f3fed2013-05-06 17:50:42 +0000158
159 if (DumpType == DIDT_All || DumpType == DIDT_StrDwo)
160 if (!getStringDWOSection().empty()) {
161 OS << "\n.debug_str.dwo contents:\n";
162 DataExtractor strDWOData(getStringDWOSection(), isLittleEndian(), 0);
163 offset = 0;
164 uint32_t strDWOOffset = 0;
165 while (const char *s = strDWOData.getCStr(&offset)) {
Eric Christopher5a0c3662013-05-06 21:19:41 +0000166 OS << format("0x%8.8x: \"%s\"\n", strDWOOffset, s);
167 strDWOOffset = offset;
Eric Christopher93f3fed2013-05-06 17:50:42 +0000168 }
169 }
170
171 if (DumpType == DIDT_All || DumpType == DIDT_StrOffsetsDwo)
172 if (!getStringOffsetDWOSection().empty()) {
173 OS << "\n.debug_str_offsets.dwo contents:\n";
174 DataExtractor strOffsetExt(getStringOffsetDWOSection(), isLittleEndian(), 0);
175 offset = 0;
Eric Christophere305e032013-05-06 21:19:44 +0000176 uint64_t size = getStringOffsetDWOSection().size();
177 while (offset < size) {
Eric Christopher5a0c3662013-05-06 21:19:41 +0000178 OS << format("0x%8.8x: ", offset);
179 OS << format("%8.8x\n", strOffsetExt.getU32(&offset));
Eric Christopher93f3fed2013-05-06 17:50:42 +0000180 }
181 }
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000182}
183
184const DWARFDebugAbbrev *DWARFContext::getDebugAbbrev() {
185 if (Abbrev)
186 return Abbrev.get();
187
188 DataExtractor abbrData(getAbbrevSection(), isLittleEndian(), 0);
189
190 Abbrev.reset(new DWARFDebugAbbrev());
191 Abbrev->parse(abbrData);
192 return Abbrev.get();
193}
194
Eric Christopher82de10a2013-01-02 23:52:13 +0000195const DWARFDebugAbbrev *DWARFContext::getDebugAbbrevDWO() {
196 if (AbbrevDWO)
197 return AbbrevDWO.get();
198
199 DataExtractor abbrData(getAbbrevDWOSection(), isLittleEndian(), 0);
200 AbbrevDWO.reset(new DWARFDebugAbbrev());
201 AbbrevDWO->parse(abbrData);
202 return AbbrevDWO.get();
203}
204
David Blaikie3df7d2f2013-06-19 21:37:13 +0000205const DWARFDebugLoc *DWARFContext::getDebugLoc() {
206 if (Loc)
207 return Loc.get();
208
David Blaikie9528b0e2013-09-23 17:42:01 +0000209 DataExtractor LocData(getLocSection().Data, isLittleEndian(), 0);
210 Loc.reset(new DWARFDebugLoc(getLocSection().Relocs));
David Blaikie3df7d2f2013-06-19 21:37:13 +0000211 // assume all compile units have the same address byte size
212 if (getNumCompileUnits())
213 Loc->parse(LocData, getCompileUnitAtIndex(0)->getAddressByteSize());
214 return Loc.get();
215}
216
Benjamin Kramer358f4fd2011-09-14 01:09:52 +0000217const DWARFDebugAranges *DWARFContext::getDebugAranges() {
218 if (Aranges)
219 return Aranges.get();
220
221 DataExtractor arangesData(getARangeSection(), isLittleEndian(), 0);
222
223 Aranges.reset(new DWARFDebugAranges());
224 Aranges->extract(arangesData);
Alexey Samsonov63a450a2012-11-16 08:36:25 +0000225 // Generate aranges from DIEs: even if .debug_aranges section is present,
226 // it may describe only a small subset of compilation units, so we need to
227 // manually build aranges for the rest of them.
228 Aranges->generate(this);
Benjamin Kramer358f4fd2011-09-14 01:09:52 +0000229 return Aranges.get();
230}
231
Eli Bendersky60bdc5b2013-02-05 23:30:58 +0000232const DWARFDebugFrame *DWARFContext::getDebugFrame() {
233 if (DebugFrame)
234 return DebugFrame.get();
235
236 // There's a "bug" in the DWARFv3 standard with respect to the target address
237 // size within debug frame sections. While DWARF is supposed to be independent
238 // of its container, FDEs have fields with size being "target address size",
239 // which isn't specified in DWARF in general. It's only specified for CUs, but
240 // .eh_frame can appear without a .debug_info section. Follow the example of
241 // other tools (libdwarf) and extract this from the container (ObjectFile
242 // provides this information). This problem is fixed in DWARFv4
243 // See this dwarf-discuss discussion for more details:
244 // http://lists.dwarfstd.org/htdig.cgi/dwarf-discuss-dwarfstd.org/2011-December/001173.html
245 DataExtractor debugFrameData(getDebugFrameSection(), isLittleEndian(),
246 getAddressSize());
247 DebugFrame.reset(new DWARFDebugFrame());
248 DebugFrame->parse(debugFrameData);
249 return DebugFrame.get();
250}
251
Eric Christophere9403c12012-10-16 23:46:25 +0000252const DWARFLineTable *
Benjamin Kramerc26ed9b2011-09-15 20:43:18 +0000253DWARFContext::getLineTableForCompileUnit(DWARFCompileUnit *cu) {
254 if (!Line)
David Blaikie9528b0e2013-09-23 17:42:01 +0000255 Line.reset(new DWARFDebugLine(&getLineSection().Relocs));
Benjamin Kramerb848e972011-09-15 02:12:05 +0000256
Benjamin Kramerc26ed9b2011-09-15 20:43:18 +0000257 unsigned stmtOffset =
258 cu->getCompileUnitDIE()->getAttributeValueAsUnsigned(cu, DW_AT_stmt_list,
259 -1U);
260 if (stmtOffset == -1U)
261 return 0; // No line table for this compile unit.
Benjamin Kramerb848e972011-09-15 02:12:05 +0000262
Benjamin Kramerc26ed9b2011-09-15 20:43:18 +0000263 // See if the line table is cached.
Eric Christophere9403c12012-10-16 23:46:25 +0000264 if (const DWARFLineTable *lt = Line->getLineTable(stmtOffset))
Benjamin Kramerc26ed9b2011-09-15 20:43:18 +0000265 return lt;
266
267 // We have to parse it first.
David Blaikie9528b0e2013-09-23 17:42:01 +0000268 DataExtractor lineData(getLineSection().Data, isLittleEndian(),
Benjamin Kramerc26ed9b2011-09-15 20:43:18 +0000269 cu->getAddressByteSize());
270 return Line->getOrParseLineTable(lineData, stmtOffset);
Benjamin Kramerb848e972011-09-15 02:12:05 +0000271}
272
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000273void DWARFContext::parseCompileUnits() {
274 uint32_t offset = 0;
David Blaikie9528b0e2013-09-23 17:42:01 +0000275 const DataExtractor &DIData = DataExtractor(getInfoSection().Data,
Eric Christopherb69b55f2012-10-16 23:46:23 +0000276 isLittleEndian(), 0);
277 while (DIData.isValidOffset(offset)) {
Alexey Samsonoveb0c1792013-08-23 06:56:01 +0000278 OwningPtr<DWARFCompileUnit> CU(new DWARFCompileUnit(
David Blaikie9528b0e2013-09-23 17:42:01 +0000279 getDebugAbbrev(), getInfoSection().Data, getAbbrevSection(),
Alexey Samsonoveb0c1792013-08-23 06:56:01 +0000280 getRangeSection(), getStringSection(), StringRef(), getAddrSection(),
David Blaikie9528b0e2013-09-23 17:42:01 +0000281 &getInfoSection().Relocs, isLittleEndian()));
Alexey Samsonoveb0c1792013-08-23 06:56:01 +0000282 if (!CU->extract(DIData, &offset)) {
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000283 break;
284 }
Alexey Samsonoveb0c1792013-08-23 06:56:01 +0000285 CUs.push_back(CU.take());
286 offset = CUs.back()->getNextCompileUnitOffset();
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000287 }
288}
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000289
Eric Christopher82de10a2013-01-02 23:52:13 +0000290void DWARFContext::parseDWOCompileUnits() {
291 uint32_t offset = 0;
David Blaikie9528b0e2013-09-23 17:42:01 +0000292 const DataExtractor &DIData =
293 DataExtractor(getInfoDWOSection().Data, isLittleEndian(), 0);
Eric Christopher82de10a2013-01-02 23:52:13 +0000294 while (DIData.isValidOffset(offset)) {
Alexey Samsonoveb0c1792013-08-23 06:56:01 +0000295 OwningPtr<DWARFCompileUnit> DWOCU(new DWARFCompileUnit(
David Blaikie9528b0e2013-09-23 17:42:01 +0000296 getDebugAbbrevDWO(), getInfoDWOSection().Data, getAbbrevDWOSection(),
Alexey Samsonoveb0c1792013-08-23 06:56:01 +0000297 getRangeDWOSection(), getStringDWOSection(),
David Blaikie9528b0e2013-09-23 17:42:01 +0000298 getStringOffsetDWOSection(), getAddrSection(),
299 &getInfoDWOSection().Relocs, isLittleEndian()));
Alexey Samsonoveb0c1792013-08-23 06:56:01 +0000300 if (!DWOCU->extract(DIData, &offset)) {
Eric Christopher82de10a2013-01-02 23:52:13 +0000301 break;
302 }
Alexey Samsonoveb0c1792013-08-23 06:56:01 +0000303 DWOCUs.push_back(DWOCU.take());
304 offset = DWOCUs.back()->getNextCompileUnitOffset();
Eric Christopher82de10a2013-01-02 23:52:13 +0000305 }
306}
307
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000308namespace {
309 struct OffsetComparator {
Alexey Samsonoveb0c1792013-08-23 06:56:01 +0000310 bool operator()(const DWARFCompileUnit *LHS,
311 const DWARFCompileUnit *RHS) const {
312 return LHS->getOffset() < RHS->getOffset();
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000313 }
Alexey Samsonoveb0c1792013-08-23 06:56:01 +0000314 bool operator()(const DWARFCompileUnit *LHS, uint32_t RHS) const {
315 return LHS->getOffset() < RHS;
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000316 }
Alexey Samsonoveb0c1792013-08-23 06:56:01 +0000317 bool operator()(uint32_t LHS, const DWARFCompileUnit *RHS) const {
318 return LHS < RHS->getOffset();
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000319 }
320 };
321}
322
Alexey Samsonov38a63812012-08-30 07:49:50 +0000323DWARFCompileUnit *DWARFContext::getCompileUnitForOffset(uint32_t Offset) {
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000324 if (CUs.empty())
325 parseCompileUnits();
326
Alexey Samsonoveb0c1792013-08-23 06:56:01 +0000327 DWARFCompileUnit **CU =
328 std::lower_bound(CUs.begin(), CUs.end(), Offset, OffsetComparator());
329 if (CU != CUs.end()) {
330 return *CU;
331 }
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000332 return 0;
333}
334
Alexey Samsonov38a63812012-08-30 07:49:50 +0000335DWARFCompileUnit *DWARFContext::getCompileUnitForAddress(uint64_t Address) {
Benjamin Kramer9013db32011-09-15 21:59:13 +0000336 // First, get the offset of the compile unit.
Alexey Samsonov38a63812012-08-30 07:49:50 +0000337 uint32_t CUOffset = getDebugAranges()->findAddress(Address);
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000338 // Retrieve the compile unit.
Alexey Samsonov38a63812012-08-30 07:49:50 +0000339 return getCompileUnitForOffset(CUOffset);
340}
341
Eric Christophere9403c12012-10-16 23:46:25 +0000342static bool getFileNameForCompileUnit(DWARFCompileUnit *CU,
343 const DWARFLineTable *LineTable,
344 uint64_t FileIndex,
345 bool NeedsAbsoluteFilePath,
346 std::string &FileName) {
Alexey Samsonov38a63812012-08-30 07:49:50 +0000347 if (CU == 0 ||
348 LineTable == 0 ||
349 !LineTable->getFileNameByIndex(FileIndex, NeedsAbsoluteFilePath,
350 FileName))
351 return false;
352 if (NeedsAbsoluteFilePath && sys::path::is_relative(FileName)) {
353 // We may still need to append compilation directory of compile unit.
354 SmallString<16> AbsolutePath;
355 if (const char *CompilationDir = CU->getCompilationDir()) {
356 sys::path::append(AbsolutePath, CompilationDir);
357 }
358 sys::path::append(AbsolutePath, FileName);
359 FileName = AbsolutePath.str();
360 }
361 return true;
362}
363
Eric Christophere9403c12012-10-16 23:46:25 +0000364static bool getFileLineInfoForCompileUnit(DWARFCompileUnit *CU,
365 const DWARFLineTable *LineTable,
366 uint64_t Address,
367 bool NeedsAbsoluteFilePath,
368 std::string &FileName,
369 uint32_t &Line, uint32_t &Column) {
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000370 if (CU == 0 || LineTable == 0)
Alexey Samsonov38a63812012-08-30 07:49:50 +0000371 return false;
372 // Get the index of row we're looking for in the line table.
373 uint32_t RowIndex = LineTable->lookupAddress(Address);
374 if (RowIndex == -1U)
375 return false;
376 // Take file number and line/column from the row.
377 const DWARFDebugLine::Row &Row = LineTable->Rows[RowIndex];
378 if (!getFileNameForCompileUnit(CU, LineTable, Row.File,
379 NeedsAbsoluteFilePath, FileName))
380 return false;
381 Line = Row.Line;
382 Column = Row.Column;
383 return true;
384}
385
386DILineInfo DWARFContext::getLineInfoForAddress(uint64_t Address,
387 DILineInfoSpecifier Specifier) {
388 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
389 if (!CU)
Alexey Samsonov3e25c4a2012-07-02 05:54:45 +0000390 return DILineInfo();
Alexey Samsonov38a63812012-08-30 07:49:50 +0000391 std::string FileName = "<invalid>";
392 std::string FunctionName = "<invalid>";
393 uint32_t Line = 0;
394 uint32_t Column = 0;
395 if (Specifier.needs(DILineInfoSpecifier::FunctionName)) {
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000396 // The address may correspond to instruction in some inlined function,
397 // so we have to build the chain of inlined functions and take the
398 // name of the topmost function in it.
Alexey Samsonove6642902013-08-06 10:49:15 +0000399 const DWARFDebugInfoEntryInlinedChain &InlinedChain =
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000400 CU->getInlinedChainForAddress(Address);
Alexey Samsonove6642902013-08-06 10:49:15 +0000401 if (InlinedChain.DIEs.size() > 0) {
402 const DWARFDebugInfoEntryMinimal &TopFunctionDIE = InlinedChain.DIEs[0];
403 if (const char *Name = TopFunctionDIE.getSubroutineName(InlinedChain.CU))
Alexey Samsonov38a63812012-08-30 07:49:50 +0000404 FunctionName = Name;
Alexey Samsonov9d26b0b2012-07-17 15:28:35 +0000405 }
Alexey Samsonov3e25c4a2012-07-02 05:54:45 +0000406 }
Alexey Samsonov38a63812012-08-30 07:49:50 +0000407 if (Specifier.needs(DILineInfoSpecifier::FileLineInfo)) {
Eric Christophere9403c12012-10-16 23:46:25 +0000408 const DWARFLineTable *LineTable = getLineTableForCompileUnit(CU);
Alexey Samsonov38a63812012-08-30 07:49:50 +0000409 const bool NeedsAbsoluteFilePath =
410 Specifier.needs(DILineInfoSpecifier::AbsoluteFilePath);
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000411 getFileLineInfoForCompileUnit(CU, LineTable, Address,
412 NeedsAbsoluteFilePath,
Alexey Samsonov38a63812012-08-30 07:49:50 +0000413 FileName, Line, Column);
Alexey Samsonov3e25c4a2012-07-02 05:54:45 +0000414 }
Alexey Samsonov38a63812012-08-30 07:49:50 +0000415 return DILineInfo(StringRef(FileName), StringRef(FunctionName),
416 Line, Column);
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000417}
David Blaikie2d24e2a2011-12-20 02:50:00 +0000418
Andrew Kaylore27a7872013-01-26 00:28:05 +0000419DILineInfoTable DWARFContext::getLineInfoForAddressRange(uint64_t Address,
420 uint64_t Size,
421 DILineInfoSpecifier Specifier) {
422 DILineInfoTable Lines;
423 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
424 if (!CU)
425 return Lines;
426
427 std::string FunctionName = "<invalid>";
428 if (Specifier.needs(DILineInfoSpecifier::FunctionName)) {
429 // The address may correspond to instruction in some inlined function,
430 // so we have to build the chain of inlined functions and take the
431 // name of the topmost function in it.
Alexey Samsonove6642902013-08-06 10:49:15 +0000432 const DWARFDebugInfoEntryInlinedChain &InlinedChain =
Andrew Kaylore27a7872013-01-26 00:28:05 +0000433 CU->getInlinedChainForAddress(Address);
Alexey Samsonove6642902013-08-06 10:49:15 +0000434 if (InlinedChain.DIEs.size() > 0) {
435 const DWARFDebugInfoEntryMinimal &TopFunctionDIE = InlinedChain.DIEs[0];
436 if (const char *Name = TopFunctionDIE.getSubroutineName(InlinedChain.CU))
Andrew Kaylore27a7872013-01-26 00:28:05 +0000437 FunctionName = Name;
438 }
439 }
440
Andrew Kaylore27a7872013-01-26 00:28:05 +0000441 // If the Specifier says we don't need FileLineInfo, just
442 // return the top-most function at the starting address.
443 if (!Specifier.needs(DILineInfoSpecifier::FileLineInfo)) {
David Blaikieeaad5cd2013-09-22 17:01:50 +0000444 Lines.push_back(
445 std::make_pair(Address, DILineInfo("<invalid>", FunctionName, 0, 0)));
Andrew Kaylore27a7872013-01-26 00:28:05 +0000446 return Lines;
447 }
448
449 const DWARFLineTable *LineTable = getLineTableForCompileUnit(CU);
450 const bool NeedsAbsoluteFilePath =
451 Specifier.needs(DILineInfoSpecifier::AbsoluteFilePath);
452
453 // Get the index of row we're looking for in the line table.
454 std::vector<uint32_t> RowVector;
455 if (!LineTable->lookupAddressRange(Address, Size, RowVector))
456 return Lines;
457
458 uint32_t NumRows = RowVector.size();
459 for (uint32_t i = 0; i < NumRows; ++i) {
460 uint32_t RowIndex = RowVector[i];
461 // Take file number and line/column from the row.
462 const DWARFDebugLine::Row &Row = LineTable->Rows[RowIndex];
463 std::string FileName = "<invalid>";
464 getFileNameForCompileUnit(CU, LineTable, Row.File,
465 NeedsAbsoluteFilePath, FileName);
David Blaikieeaad5cd2013-09-22 17:01:50 +0000466 Lines.push_back(std::make_pair(
467 Row.Address, DILineInfo(FileName, FunctionName, Row.Line, Row.Column)));
Andrew Kaylore27a7872013-01-26 00:28:05 +0000468 }
469
470 return Lines;
471}
472
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000473DIInliningInfo DWARFContext::getInliningInfoForAddress(uint64_t Address,
474 DILineInfoSpecifier Specifier) {
475 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
476 if (!CU)
477 return DIInliningInfo();
478
Alexey Samsonove6642902013-08-06 10:49:15 +0000479 const DWARFDebugInfoEntryInlinedChain &InlinedChain =
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000480 CU->getInlinedChainForAddress(Address);
Alexey Samsonove6642902013-08-06 10:49:15 +0000481 if (InlinedChain.DIEs.size() == 0)
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000482 return DIInliningInfo();
483
484 DIInliningInfo InliningInfo;
485 uint32_t CallFile = 0, CallLine = 0, CallColumn = 0;
Eric Christophere9403c12012-10-16 23:46:25 +0000486 const DWARFLineTable *LineTable = 0;
Alexey Samsonove6642902013-08-06 10:49:15 +0000487 for (uint32_t i = 0, n = InlinedChain.DIEs.size(); i != n; i++) {
488 const DWARFDebugInfoEntryMinimal &FunctionDIE = InlinedChain.DIEs[i];
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000489 std::string FileName = "<invalid>";
490 std::string FunctionName = "<invalid>";
491 uint32_t Line = 0;
492 uint32_t Column = 0;
493 // Get function name if necessary.
494 if (Specifier.needs(DILineInfoSpecifier::FunctionName)) {
Alexey Samsonove6642902013-08-06 10:49:15 +0000495 if (const char *Name = FunctionDIE.getSubroutineName(InlinedChain.CU))
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000496 FunctionName = Name;
497 }
498 if (Specifier.needs(DILineInfoSpecifier::FileLineInfo)) {
499 const bool NeedsAbsoluteFilePath =
500 Specifier.needs(DILineInfoSpecifier::AbsoluteFilePath);
501 if (i == 0) {
502 // For the topmost frame, initialize the line table of this
503 // compile unit and fetch file/line info from it.
504 LineTable = getLineTableForCompileUnit(CU);
505 // For the topmost routine, get file/line info from line table.
506 getFileLineInfoForCompileUnit(CU, LineTable, Address,
507 NeedsAbsoluteFilePath,
508 FileName, Line, Column);
509 } else {
510 // Otherwise, use call file, call line and call column from
511 // previous DIE in inlined chain.
512 getFileNameForCompileUnit(CU, LineTable, CallFile,
513 NeedsAbsoluteFilePath, FileName);
514 Line = CallLine;
515 Column = CallColumn;
516 }
517 // Get call file/line/column of a current DIE.
518 if (i + 1 < n) {
Alexey Samsonove6642902013-08-06 10:49:15 +0000519 FunctionDIE.getCallerFrame(InlinedChain.CU, CallFile, CallLine,
520 CallColumn);
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000521 }
522 }
523 DILineInfo Frame(StringRef(FileName), StringRef(FunctionName),
524 Line, Column);
525 InliningInfo.addFrame(Frame);
526 }
527 return InliningInfo;
528}
529
Alexey Samsonov005159e2013-04-23 10:17:34 +0000530static bool consumeCompressedDebugSectionHeader(StringRef &data,
531 uint64_t &OriginalSize) {
532 // Consume "ZLIB" prefix.
533 if (!data.startswith("ZLIB"))
534 return false;
535 data = data.substr(4);
536 // Consume uncompressed section size (big-endian 8 bytes).
537 DataExtractor extractor(data, false, 8);
538 uint32_t Offset = 0;
539 OriginalSize = extractor.getU64(&Offset);
540 if (Offset == 0)
541 return false;
542 data = data.substr(Offset);
543 return true;
544}
545
Eric Christopherd1726a42012-11-12 21:40:38 +0000546DWARFContextInMemory::DWARFContextInMemory(object::ObjectFile *Obj) :
Eli Bendersky60bdc5b2013-02-05 23:30:58 +0000547 IsLittleEndian(Obj->isLittleEndian()),
548 AddressSize(Obj->getBytesInAddress()) {
Eric Christopherd1726a42012-11-12 21:40:38 +0000549 error_code ec;
550 for (object::section_iterator i = Obj->begin_sections(),
551 e = Obj->end_sections();
552 i != e; i.increment(ec)) {
553 StringRef name;
554 i->getName(name);
555 StringRef data;
556 i->getContents(data);
557
Eric Christopherd1726a42012-11-12 21:40:38 +0000558 name = name.substr(name.find_first_not_of("._")); // Skip . and _ prefixes.
Alexey Samsonov784baa62013-04-17 14:27:04 +0000559
Alexey Samsonov005159e2013-04-23 10:17:34 +0000560 // Check if debug info section is compressed with zlib.
561 if (name.startswith("zdebug_")) {
562 uint64_t OriginalSize;
563 if (!zlib::isAvailable() ||
564 !consumeCompressedDebugSectionHeader(data, OriginalSize))
565 continue;
566 OwningPtr<MemoryBuffer> UncompressedSection;
567 if (zlib::uncompress(data, UncompressedSection, OriginalSize) !=
568 zlib::StatusOK)
569 continue;
570 // Make data point to uncompressed section contents and save its contents.
571 name = name.substr(1);
572 data = UncompressedSection->getBuffer();
573 UncompressedSections.push_back(UncompressedSection.take());
574 }
575
Alexey Samsonov784baa62013-04-17 14:27:04 +0000576 StringRef *Section = StringSwitch<StringRef*>(name)
David Blaikie9528b0e2013-09-23 17:42:01 +0000577 .Case("debug_info", &InfoSection.Data)
Alexey Samsonov784baa62013-04-17 14:27:04 +0000578 .Case("debug_abbrev", &AbbrevSection)
David Blaikie9528b0e2013-09-23 17:42:01 +0000579 .Case("debug_loc", &LocSection.Data)
580 .Case("debug_line", &LineSection.Data)
Alexey Samsonov784baa62013-04-17 14:27:04 +0000581 .Case("debug_aranges", &ARangeSection)
582 .Case("debug_frame", &DebugFrameSection)
583 .Case("debug_str", &StringSection)
584 .Case("debug_ranges", &RangeSection)
585 .Case("debug_pubnames", &PubNamesSection)
David Blaikie994c37f2013-09-19 23:01:29 +0000586 .Case("debug_gnu_pubnames", &GnuPubNamesSection)
David Blaikie9528b0e2013-09-23 17:42:01 +0000587 .Case("debug_info.dwo", &InfoDWOSection.Data)
Alexey Samsonov784baa62013-04-17 14:27:04 +0000588 .Case("debug_abbrev.dwo", &AbbrevDWOSection)
589 .Case("debug_str.dwo", &StringDWOSection)
590 .Case("debug_str_offsets.dwo", &StringOffsetDWOSection)
591 .Case("debug_addr", &AddrSection)
592 // Any more debug info sections go here.
593 .Default(0);
Rafael Espindola7486d922013-05-30 03:05:14 +0000594 if (Section) {
595 *Section = data;
596 if (name == "debug_ranges") {
597 // FIXME: Use the other dwo range section when we emit it.
598 RangeDWOSection = data;
599 }
Eric Christopher82de10a2013-01-02 23:52:13 +0000600 }
Eric Christopherd1726a42012-11-12 21:40:38 +0000601
Rafael Espindola7486d922013-05-30 03:05:14 +0000602 section_iterator RelocatedSection = i->getRelocatedSection();
603 if (RelocatedSection == Obj->end_sections())
604 continue;
605
606 StringRef RelSecName;
607 RelocatedSection->getName(RelSecName);
608 RelSecName = RelSecName.substr(
609 RelSecName.find_first_not_of("._")); // Skip . and _ prefixes.
610
Andrew Kayloree7c0d22013-01-25 22:50:58 +0000611 // TODO: Add support for relocations in other sections as needed.
612 // Record relocations for the debug_info and debug_line sections.
Rafael Espindola7486d922013-05-30 03:05:14 +0000613 RelocAddrMap *Map = StringSwitch<RelocAddrMap*>(RelSecName)
David Blaikie9528b0e2013-09-23 17:42:01 +0000614 .Case("debug_info", &InfoSection.Relocs)
615 .Case("debug_loc", &LocSection.Relocs)
616 .Case("debug_info.dwo", &InfoDWOSection.Relocs)
617 .Case("debug_line", &LineSection.Relocs)
Alexey Samsonov784baa62013-04-17 14:27:04 +0000618 .Default(0);
619 if (!Map)
Eric Christopherd1726a42012-11-12 21:40:38 +0000620 continue;
621
622 if (i->begin_relocations() != i->end_relocations()) {
623 uint64_t SectionSize;
Rafael Espindola7486d922013-05-30 03:05:14 +0000624 RelocatedSection->getSize(SectionSize);
Eric Christopherd1726a42012-11-12 21:40:38 +0000625 for (object::relocation_iterator reloc_i = i->begin_relocations(),
626 reloc_e = i->end_relocations();
627 reloc_i != reloc_e; reloc_i.increment(ec)) {
628 uint64_t Address;
Rafael Espindola956ca722013-04-25 12:28:45 +0000629 reloc_i->getOffset(Address);
Eric Christopherd1726a42012-11-12 21:40:38 +0000630 uint64_t Type;
631 reloc_i->getType(Type);
Andrew Kayloree7c0d22013-01-25 22:50:58 +0000632 uint64_t SymAddr = 0;
633 // ELF relocations may need the symbol address
634 if (Obj->isELF()) {
Rafael Espindola6c1202c2013-06-05 01:33:53 +0000635 object::symbol_iterator Sym = reloc_i->getSymbol();
636 Sym->getAddress(SymAddr);
Andrew Kayloree7c0d22013-01-25 22:50:58 +0000637 }
Eric Christopherd1726a42012-11-12 21:40:38 +0000638
639 object::RelocVisitor V(Obj->getFileFormatName());
640 // The section address is always 0 for debug sections.
Andrew Kayloree7c0d22013-01-25 22:50:58 +0000641 object::RelocToApply R(V.visit(Type, *reloc_i, 0, SymAddr));
Eric Christopherd1726a42012-11-12 21:40:38 +0000642 if (V.error()) {
643 SmallString<32> Name;
644 error_code ec(reloc_i->getTypeName(Name));
645 if (ec) {
646 errs() << "Aaaaaa! Nameless relocation! Aaaaaa!\n";
647 }
648 errs() << "error: failed to compute relocation: "
649 << Name << "\n";
650 continue;
651 }
652
653 if (Address + R.Width > SectionSize) {
654 errs() << "error: " << R.Width << "-byte relocation starting "
655 << Address << " bytes into section " << name << " which is "
656 << SectionSize << " bytes long.\n";
657 continue;
658 }
659 if (R.Width > 8) {
660 errs() << "error: can't handle a relocation of more than 8 bytes at "
661 "a time.\n";
662 continue;
663 }
664 DEBUG(dbgs() << "Writing " << format("%p", R.Value)
665 << " at " << format("%p", Address)
666 << " with width " << format("%d", R.Width)
667 << "\n");
Eric Christopher82de10a2013-01-02 23:52:13 +0000668 Map->insert(std::make_pair(Address, std::make_pair(R.Width, R.Value)));
Eric Christopherd1726a42012-11-12 21:40:38 +0000669 }
670 }
671 }
672}
673
Alexey Samsonov005159e2013-04-23 10:17:34 +0000674DWARFContextInMemory::~DWARFContextInMemory() {
675 DeleteContainerPointers(UncompressedSections);
676}
677
David Blaikie2d24e2a2011-12-20 02:50:00 +0000678void DWARFContextInMemory::anchor() { }