blob: df4633d2c0507e8b8d56db0b2563a673a39320e2 [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);
Benjamin Kramer1fe49e62013-09-29 11:24:02 +000028 DeleteContainerPointers(TUs);
Alexey Samsonoveb0c1792013-08-23 06:56:01 +000029 DeleteContainerPointers(DWOCUs);
30}
31
David Blaikie9ddf28d2013-09-24 19:50:00 +000032static void dumpPubSection(raw_ostream &OS, StringRef Name, StringRef Data,
Eric Christopherc839df02013-09-25 23:02:36 +000033 bool LittleEndian, bool GnuStyle) {
David Blaikie9ddf28d2013-09-24 19:50:00 +000034 OS << "\n." << Name << " contents:\n";
35 DataExtractor pubNames(Data, LittleEndian, 0);
36 uint32_t offset = 0;
37 OS << "Length: " << pubNames.getU32(&offset) << "\n";
38 OS << "Version: " << pubNames.getU16(&offset) << "\n";
39 OS << "Offset in .debug_info: " << pubNames.getU32(&offset) << "\n";
40 OS << "Size: " << pubNames.getU32(&offset) << "\n";
Eric Christopherc839df02013-09-25 23:02:36 +000041 if (GnuStyle)
42 OS << "Offset Linkage Kind Name\n";
43 else
Eric Christopher5972df22013-09-27 22:10:10 +000044 OS << "Offset Name\n";
Eric Christopherc839df02013-09-25 23:02:36 +000045
David Blaikie9ddf28d2013-09-24 19:50:00 +000046 while (offset < Data.size()) {
47 uint32_t dieRef = pubNames.getU32(&offset);
48 if (dieRef == 0)
49 break;
Eric Christopher5972df22013-09-27 22:10:10 +000050 OS << format("0x%8.8x ", dieRef);
Eric Christopherc839df02013-09-25 23:02:36 +000051 if (GnuStyle) {
52 PubIndexEntryDescriptor desc(pubNames.getU8(&offset));
Eric Christopher5972df22013-09-27 22:10:10 +000053 OS << format("%-8s", dwarf::GDBIndexEntryLinkageString(desc.Linkage))
Eric Christopherc839df02013-09-25 23:02:36 +000054 << ' ' << format("%-8s", dwarf::GDBIndexEntryKindString(desc.Kind))
Eric Christopher5972df22013-09-27 22:10:10 +000055 << ' ';
Eric Christopherc839df02013-09-25 23:02:36 +000056 }
Eric Christopher5972df22013-09-27 22:10:10 +000057 OS << '\"' << pubNames.getCStr(&offset) << "\"\n";
David Blaikie9ddf28d2013-09-24 19:50:00 +000058 }
59}
60
Eli Bendersky939a4e82013-01-25 20:26:43 +000061void DWARFContext::dump(raw_ostream &OS, DIDumpType DumpType) {
62 if (DumpType == DIDT_All || DumpType == DIDT_Abbrev) {
63 OS << ".debug_abbrev contents:\n";
64 getDebugAbbrev()->dump(OS);
65 }
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000066
Eli Bendersky939a4e82013-01-25 20:26:43 +000067 if (DumpType == DIDT_All || DumpType == DIDT_Info) {
68 OS << "\n.debug_info contents:\n";
69 for (unsigned i = 0, e = getNumCompileUnits(); i != e; ++i)
70 getCompileUnitAtIndex(i)->dump(OS);
71 }
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000072
David Blaikie438f5392013-09-23 22:44:47 +000073 if (DumpType == DIDT_All || DumpType == DIDT_Types) {
74 OS << "\n.debug_types contents:\n";
75 for (unsigned i = 0, e = getNumTypeUnits(); i != e; ++i)
76 getTypeUnitAtIndex(i)->dump(OS);
77 }
78
David Blaikie3df7d2f2013-06-19 21:37:13 +000079 if (DumpType == DIDT_All || DumpType == DIDT_Loc) {
David Blaikie438f5392013-09-23 22:44:47 +000080 OS << "\n.debug_loc contents:\n";
David Blaikie3df7d2f2013-06-19 21:37:13 +000081 getDebugLoc()->dump(OS);
82 }
83
Eli Bendersky60bdc5b2013-02-05 23:30:58 +000084 if (DumpType == DIDT_All || DumpType == DIDT_Frames) {
85 OS << "\n.debug_frame contents:\n";
86 getDebugFrame()->dump(OS);
87 }
88
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000089 uint32_t offset = 0;
Eli Bendersky939a4e82013-01-25 20:26:43 +000090 if (DumpType == DIDT_All || DumpType == DIDT_Aranges) {
91 OS << "\n.debug_aranges contents:\n";
92 DataExtractor arangesData(getARangeSection(), isLittleEndian(), 0);
93 DWARFDebugArangeSet set;
94 while (set.extract(arangesData, &offset))
95 set.dump(OS);
96 }
Benjamin Kramerb848e972011-09-15 02:12:05 +000097
Alexey Samsonoveceb5b92012-08-27 07:17:47 +000098 uint8_t savedAddressByteSize = 0;
Eli Bendersky939a4e82013-01-25 20:26:43 +000099 if (DumpType == DIDT_All || DumpType == DIDT_Line) {
100 OS << "\n.debug_line contents:\n";
101 for (unsigned i = 0, e = getNumCompileUnits(); i != e; ++i) {
102 DWARFCompileUnit *cu = getCompileUnitAtIndex(i);
103 savedAddressByteSize = cu->getAddressByteSize();
104 unsigned stmtOffset =
105 cu->getCompileUnitDIE()->getAttributeValueAsUnsigned(cu, DW_AT_stmt_list,
106 -1U);
107 if (stmtOffset != -1U) {
David Blaikie9528b0e2013-09-23 17:42:01 +0000108 DataExtractor lineData(getLineSection().Data, isLittleEndian(),
Eli Bendersky939a4e82013-01-25 20:26:43 +0000109 savedAddressByteSize);
110 DWARFDebugLine::DumpingState state(OS);
David Blaikie9528b0e2013-09-23 17:42:01 +0000111 DWARFDebugLine::parseStatementTable(lineData, &getLineSection().Relocs, &stmtOffset, state);
Eli Bendersky939a4e82013-01-25 20:26:43 +0000112 }
Benjamin Kramerfe80f1d2011-09-15 18:02:20 +0000113 }
114 }
Benjamin Kramer34f864f2011-09-15 16:57:13 +0000115
Eli Bendersky939a4e82013-01-25 20:26:43 +0000116 if (DumpType == DIDT_All || DumpType == DIDT_Str) {
117 OS << "\n.debug_str contents:\n";
118 DataExtractor strData(getStringSection(), isLittleEndian(), 0);
119 offset = 0;
120 uint32_t strOffset = 0;
121 while (const char *s = strData.getCStr(&offset)) {
122 OS << format("0x%8.8x: \"%s\"\n", strOffset, s);
123 strOffset = offset;
124 }
Benjamin Kramer34f864f2011-09-15 16:57:13 +0000125 }
Alexey Samsonoveceb5b92012-08-27 07:17:47 +0000126
Eli Bendersky939a4e82013-01-25 20:26:43 +0000127 if (DumpType == DIDT_All || DumpType == DIDT_Ranges) {
128 OS << "\n.debug_ranges contents:\n";
129 // In fact, different compile units may have different address byte
130 // sizes, but for simplicity we just use the address byte size of the last
131 // compile unit (there is no easy and fast way to associate address range
132 // list and the compile unit it describes).
133 DataExtractor rangesData(getRangeSection(), isLittleEndian(),
134 savedAddressByteSize);
135 offset = 0;
136 DWARFDebugRangeList rangeList;
137 while (rangeList.extract(rangesData, &offset))
138 rangeList.dump(OS);
Eric Christopher82de10a2013-01-02 23:52:13 +0000139 }
Eric Christopher72f7bfb2013-01-15 23:56:56 +0000140
Eric Christopherc839df02013-09-25 23:02:36 +0000141 if (DumpType == DIDT_All || DumpType == DIDT_Pubnames)
142 dumpPubSection(OS, "debug_pubnames", getPubNamesSection(),
143 isLittleEndian(), false);
Krzysztof Parzyszeke38825f2013-02-12 16:20:28 +0000144
Eric Christopher7357f032013-09-25 23:02:41 +0000145 if (DumpType == DIDT_All || DumpType == DIDT_Pubtypes)
146 dumpPubSection(OS, "debug_pubtypes", getPubTypesSection(),
147 isLittleEndian(), false);
148
David Blaikie9ddf28d2013-09-24 19:50:00 +0000149 if (DumpType == DIDT_All || DumpType == DIDT_GnuPubnames)
150 dumpPubSection(OS, "debug_gnu_pubnames", getGnuPubNamesSection(),
Eric Christopherc839df02013-09-25 23:02:36 +0000151 isLittleEndian(), true /* GnuStyle */);
David Blaikie9ddf28d2013-09-24 19:50:00 +0000152
153 if (DumpType == DIDT_All || DumpType == DIDT_GnuPubtypes)
154 dumpPubSection(OS, "debug_gnu_pubtypes", getGnuPubTypesSection(),
Eric Christopherc839df02013-09-25 23:02:36 +0000155 isLittleEndian(), true /* GnuStyle */);
David Blaikie994c37f2013-09-19 23:01:29 +0000156
Eli Bendersky939a4e82013-01-25 20:26:43 +0000157 if (DumpType == DIDT_All || DumpType == DIDT_AbbrevDwo) {
Eric Christopher93f3fed2013-05-06 17:50:42 +0000158 const DWARFDebugAbbrev *D = getDebugAbbrevDWO();
159 if (D) {
160 OS << "\n.debug_abbrev.dwo contents:\n";
161 getDebugAbbrevDWO()->dump(OS);
Eli Bendersky939a4e82013-01-25 20:26:43 +0000162 }
163 }
164
Eric Christopher93f3fed2013-05-06 17:50:42 +0000165 if (DumpType == DIDT_All || DumpType == DIDT_InfoDwo)
166 if (getNumDWOCompileUnits()) {
167 OS << "\n.debug_info.dwo contents:\n";
168 for (unsigned i = 0, e = getNumDWOCompileUnits(); i != e; ++i)
Eric Christopher5a0c3662013-05-06 21:19:41 +0000169 getDWOCompileUnitAtIndex(i)->dump(OS);
Eli Bendersky939a4e82013-01-25 20:26:43 +0000170 }
Eric Christopher93f3fed2013-05-06 17:50:42 +0000171
172 if (DumpType == DIDT_All || DumpType == DIDT_StrDwo)
173 if (!getStringDWOSection().empty()) {
174 OS << "\n.debug_str.dwo contents:\n";
175 DataExtractor strDWOData(getStringDWOSection(), isLittleEndian(), 0);
176 offset = 0;
177 uint32_t strDWOOffset = 0;
178 while (const char *s = strDWOData.getCStr(&offset)) {
Eric Christopher5a0c3662013-05-06 21:19:41 +0000179 OS << format("0x%8.8x: \"%s\"\n", strDWOOffset, s);
180 strDWOOffset = offset;
Eric Christopher93f3fed2013-05-06 17:50:42 +0000181 }
182 }
183
184 if (DumpType == DIDT_All || DumpType == DIDT_StrOffsetsDwo)
185 if (!getStringOffsetDWOSection().empty()) {
186 OS << "\n.debug_str_offsets.dwo contents:\n";
187 DataExtractor strOffsetExt(getStringOffsetDWOSection(), isLittleEndian(), 0);
188 offset = 0;
Eric Christophere305e032013-05-06 21:19:44 +0000189 uint64_t size = getStringOffsetDWOSection().size();
190 while (offset < size) {
Eric Christopher5a0c3662013-05-06 21:19:41 +0000191 OS << format("0x%8.8x: ", offset);
192 OS << format("%8.8x\n", strOffsetExt.getU32(&offset));
Eric Christopher93f3fed2013-05-06 17:50:42 +0000193 }
194 }
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000195}
196
197const DWARFDebugAbbrev *DWARFContext::getDebugAbbrev() {
198 if (Abbrev)
199 return Abbrev.get();
200
201 DataExtractor abbrData(getAbbrevSection(), isLittleEndian(), 0);
202
203 Abbrev.reset(new DWARFDebugAbbrev());
204 Abbrev->parse(abbrData);
205 return Abbrev.get();
206}
207
Eric Christopher82de10a2013-01-02 23:52:13 +0000208const DWARFDebugAbbrev *DWARFContext::getDebugAbbrevDWO() {
209 if (AbbrevDWO)
210 return AbbrevDWO.get();
211
212 DataExtractor abbrData(getAbbrevDWOSection(), isLittleEndian(), 0);
213 AbbrevDWO.reset(new DWARFDebugAbbrev());
214 AbbrevDWO->parse(abbrData);
215 return AbbrevDWO.get();
216}
217
David Blaikie3df7d2f2013-06-19 21:37:13 +0000218const DWARFDebugLoc *DWARFContext::getDebugLoc() {
219 if (Loc)
220 return Loc.get();
221
David Blaikie9528b0e2013-09-23 17:42:01 +0000222 DataExtractor LocData(getLocSection().Data, isLittleEndian(), 0);
223 Loc.reset(new DWARFDebugLoc(getLocSection().Relocs));
David Blaikie3df7d2f2013-06-19 21:37:13 +0000224 // assume all compile units have the same address byte size
225 if (getNumCompileUnits())
226 Loc->parse(LocData, getCompileUnitAtIndex(0)->getAddressByteSize());
227 return Loc.get();
228}
229
Benjamin Kramer358f4fd2011-09-14 01:09:52 +0000230const DWARFDebugAranges *DWARFContext::getDebugAranges() {
231 if (Aranges)
232 return Aranges.get();
233
234 DataExtractor arangesData(getARangeSection(), isLittleEndian(), 0);
235
236 Aranges.reset(new DWARFDebugAranges());
237 Aranges->extract(arangesData);
Alexey Samsonov63a450a2012-11-16 08:36:25 +0000238 // Generate aranges from DIEs: even if .debug_aranges section is present,
239 // it may describe only a small subset of compilation units, so we need to
240 // manually build aranges for the rest of them.
241 Aranges->generate(this);
Benjamin Kramer358f4fd2011-09-14 01:09:52 +0000242 return Aranges.get();
243}
244
Eli Bendersky60bdc5b2013-02-05 23:30:58 +0000245const DWARFDebugFrame *DWARFContext::getDebugFrame() {
246 if (DebugFrame)
247 return DebugFrame.get();
248
249 // There's a "bug" in the DWARFv3 standard with respect to the target address
250 // size within debug frame sections. While DWARF is supposed to be independent
251 // of its container, FDEs have fields with size being "target address size",
252 // which isn't specified in DWARF in general. It's only specified for CUs, but
253 // .eh_frame can appear without a .debug_info section. Follow the example of
254 // other tools (libdwarf) and extract this from the container (ObjectFile
255 // provides this information). This problem is fixed in DWARFv4
256 // See this dwarf-discuss discussion for more details:
257 // http://lists.dwarfstd.org/htdig.cgi/dwarf-discuss-dwarfstd.org/2011-December/001173.html
258 DataExtractor debugFrameData(getDebugFrameSection(), isLittleEndian(),
259 getAddressSize());
260 DebugFrame.reset(new DWARFDebugFrame());
261 DebugFrame->parse(debugFrameData);
262 return DebugFrame.get();
263}
264
Eric Christophere9403c12012-10-16 23:46:25 +0000265const DWARFLineTable *
Benjamin Kramerc26ed9b2011-09-15 20:43:18 +0000266DWARFContext::getLineTableForCompileUnit(DWARFCompileUnit *cu) {
267 if (!Line)
David Blaikie9528b0e2013-09-23 17:42:01 +0000268 Line.reset(new DWARFDebugLine(&getLineSection().Relocs));
Benjamin Kramerb848e972011-09-15 02:12:05 +0000269
Benjamin Kramerc26ed9b2011-09-15 20:43:18 +0000270 unsigned stmtOffset =
271 cu->getCompileUnitDIE()->getAttributeValueAsUnsigned(cu, DW_AT_stmt_list,
272 -1U);
273 if (stmtOffset == -1U)
274 return 0; // No line table for this compile unit.
Benjamin Kramerb848e972011-09-15 02:12:05 +0000275
Benjamin Kramerc26ed9b2011-09-15 20:43:18 +0000276 // See if the line table is cached.
Eric Christophere9403c12012-10-16 23:46:25 +0000277 if (const DWARFLineTable *lt = Line->getLineTable(stmtOffset))
Benjamin Kramerc26ed9b2011-09-15 20:43:18 +0000278 return lt;
279
280 // We have to parse it first.
David Blaikie9528b0e2013-09-23 17:42:01 +0000281 DataExtractor lineData(getLineSection().Data, isLittleEndian(),
Benjamin Kramerc26ed9b2011-09-15 20:43:18 +0000282 cu->getAddressByteSize());
283 return Line->getOrParseLineTable(lineData, stmtOffset);
Benjamin Kramerb848e972011-09-15 02:12:05 +0000284}
285
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000286void DWARFContext::parseCompileUnits() {
287 uint32_t offset = 0;
David Blaikie9528b0e2013-09-23 17:42:01 +0000288 const DataExtractor &DIData = DataExtractor(getInfoSection().Data,
Eric Christopherb69b55f2012-10-16 23:46:23 +0000289 isLittleEndian(), 0);
290 while (DIData.isValidOffset(offset)) {
Alexey Samsonoveb0c1792013-08-23 06:56:01 +0000291 OwningPtr<DWARFCompileUnit> CU(new DWARFCompileUnit(
David Blaikie9528b0e2013-09-23 17:42:01 +0000292 getDebugAbbrev(), getInfoSection().Data, getAbbrevSection(),
Alexey Samsonoveb0c1792013-08-23 06:56:01 +0000293 getRangeSection(), getStringSection(), StringRef(), getAddrSection(),
David Blaikie9528b0e2013-09-23 17:42:01 +0000294 &getInfoSection().Relocs, isLittleEndian()));
Alexey Samsonoveb0c1792013-08-23 06:56:01 +0000295 if (!CU->extract(DIData, &offset)) {
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000296 break;
297 }
Alexey Samsonoveb0c1792013-08-23 06:56:01 +0000298 CUs.push_back(CU.take());
David Blaikiecd7c4982013-09-23 22:44:40 +0000299 offset = CUs.back()->getNextUnitOffset();
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000300 }
301}
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000302
David Blaikie438f5392013-09-23 22:44:47 +0000303void DWARFContext::parseTypeUnits() {
304 const std::map<object::SectionRef, Section> &Sections = getTypesSections();
305 for (std::map<object::SectionRef, Section>::const_iterator
306 I = Sections.begin(),
307 E = Sections.end();
308 I != E; ++I) {
309 uint32_t offset = 0;
310 const DataExtractor &DIData =
311 DataExtractor(I->second.Data, isLittleEndian(), 0);
312 while (DIData.isValidOffset(offset)) {
313 OwningPtr<DWARFTypeUnit> TU(new DWARFTypeUnit(
314 getDebugAbbrev(), I->second.Data, getAbbrevSection(),
315 getRangeSection(), getStringSection(), StringRef(), getAddrSection(),
316 &I->second.Relocs, isLittleEndian()));
317 if (!TU->extract(DIData, &offset))
318 break;
319 TUs.push_back(TU.take());
320 offset = TUs.back()->getNextUnitOffset();
321 }
322 }
323}
324
Eric Christopher82de10a2013-01-02 23:52:13 +0000325void DWARFContext::parseDWOCompileUnits() {
326 uint32_t offset = 0;
David Blaikie9528b0e2013-09-23 17:42:01 +0000327 const DataExtractor &DIData =
328 DataExtractor(getInfoDWOSection().Data, isLittleEndian(), 0);
Eric Christopher82de10a2013-01-02 23:52:13 +0000329 while (DIData.isValidOffset(offset)) {
Alexey Samsonoveb0c1792013-08-23 06:56:01 +0000330 OwningPtr<DWARFCompileUnit> DWOCU(new DWARFCompileUnit(
David Blaikie9528b0e2013-09-23 17:42:01 +0000331 getDebugAbbrevDWO(), getInfoDWOSection().Data, getAbbrevDWOSection(),
Alexey Samsonoveb0c1792013-08-23 06:56:01 +0000332 getRangeDWOSection(), getStringDWOSection(),
David Blaikie9528b0e2013-09-23 17:42:01 +0000333 getStringOffsetDWOSection(), getAddrSection(),
334 &getInfoDWOSection().Relocs, isLittleEndian()));
Alexey Samsonoveb0c1792013-08-23 06:56:01 +0000335 if (!DWOCU->extract(DIData, &offset)) {
Eric Christopher82de10a2013-01-02 23:52:13 +0000336 break;
337 }
Alexey Samsonoveb0c1792013-08-23 06:56:01 +0000338 DWOCUs.push_back(DWOCU.take());
David Blaikiecd7c4982013-09-23 22:44:40 +0000339 offset = DWOCUs.back()->getNextUnitOffset();
Eric Christopher82de10a2013-01-02 23:52:13 +0000340 }
341}
342
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000343namespace {
344 struct OffsetComparator {
Alexey Samsonoveb0c1792013-08-23 06:56:01 +0000345 bool operator()(const DWARFCompileUnit *LHS,
346 const DWARFCompileUnit *RHS) const {
347 return LHS->getOffset() < RHS->getOffset();
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000348 }
Alexey Samsonoveb0c1792013-08-23 06:56:01 +0000349 bool operator()(const DWARFCompileUnit *LHS, uint32_t RHS) const {
350 return LHS->getOffset() < RHS;
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000351 }
Alexey Samsonoveb0c1792013-08-23 06:56:01 +0000352 bool operator()(uint32_t LHS, const DWARFCompileUnit *RHS) const {
353 return LHS < RHS->getOffset();
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000354 }
355 };
356}
357
Alexey Samsonov38a63812012-08-30 07:49:50 +0000358DWARFCompileUnit *DWARFContext::getCompileUnitForOffset(uint32_t Offset) {
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000359 if (CUs.empty())
360 parseCompileUnits();
361
Alexey Samsonoveb0c1792013-08-23 06:56:01 +0000362 DWARFCompileUnit **CU =
363 std::lower_bound(CUs.begin(), CUs.end(), Offset, OffsetComparator());
364 if (CU != CUs.end()) {
365 return *CU;
366 }
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000367 return 0;
368}
369
Alexey Samsonov38a63812012-08-30 07:49:50 +0000370DWARFCompileUnit *DWARFContext::getCompileUnitForAddress(uint64_t Address) {
Benjamin Kramer9013db32011-09-15 21:59:13 +0000371 // First, get the offset of the compile unit.
Alexey Samsonov38a63812012-08-30 07:49:50 +0000372 uint32_t CUOffset = getDebugAranges()->findAddress(Address);
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000373 // Retrieve the compile unit.
Alexey Samsonov38a63812012-08-30 07:49:50 +0000374 return getCompileUnitForOffset(CUOffset);
375}
376
Eric Christophere9403c12012-10-16 23:46:25 +0000377static bool getFileNameForCompileUnit(DWARFCompileUnit *CU,
378 const DWARFLineTable *LineTable,
379 uint64_t FileIndex,
380 bool NeedsAbsoluteFilePath,
381 std::string &FileName) {
Alexey Samsonov38a63812012-08-30 07:49:50 +0000382 if (CU == 0 ||
383 LineTable == 0 ||
384 !LineTable->getFileNameByIndex(FileIndex, NeedsAbsoluteFilePath,
385 FileName))
386 return false;
387 if (NeedsAbsoluteFilePath && sys::path::is_relative(FileName)) {
388 // We may still need to append compilation directory of compile unit.
389 SmallString<16> AbsolutePath;
390 if (const char *CompilationDir = CU->getCompilationDir()) {
391 sys::path::append(AbsolutePath, CompilationDir);
392 }
393 sys::path::append(AbsolutePath, FileName);
394 FileName = AbsolutePath.str();
395 }
396 return true;
397}
398
Eric Christophere9403c12012-10-16 23:46:25 +0000399static bool getFileLineInfoForCompileUnit(DWARFCompileUnit *CU,
400 const DWARFLineTable *LineTable,
401 uint64_t Address,
402 bool NeedsAbsoluteFilePath,
403 std::string &FileName,
404 uint32_t &Line, uint32_t &Column) {
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000405 if (CU == 0 || LineTable == 0)
Alexey Samsonov38a63812012-08-30 07:49:50 +0000406 return false;
407 // Get the index of row we're looking for in the line table.
408 uint32_t RowIndex = LineTable->lookupAddress(Address);
409 if (RowIndex == -1U)
410 return false;
411 // Take file number and line/column from the row.
412 const DWARFDebugLine::Row &Row = LineTable->Rows[RowIndex];
413 if (!getFileNameForCompileUnit(CU, LineTable, Row.File,
414 NeedsAbsoluteFilePath, FileName))
415 return false;
416 Line = Row.Line;
417 Column = Row.Column;
418 return true;
419}
420
421DILineInfo DWARFContext::getLineInfoForAddress(uint64_t Address,
422 DILineInfoSpecifier Specifier) {
423 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
424 if (!CU)
Alexey Samsonov3e25c4a2012-07-02 05:54:45 +0000425 return DILineInfo();
Alexey Samsonov38a63812012-08-30 07:49:50 +0000426 std::string FileName = "<invalid>";
427 std::string FunctionName = "<invalid>";
428 uint32_t Line = 0;
429 uint32_t Column = 0;
430 if (Specifier.needs(DILineInfoSpecifier::FunctionName)) {
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000431 // The address may correspond to instruction in some inlined function,
432 // so we have to build the chain of inlined functions and take the
433 // name of the topmost function in it.
Alexey Samsonove6642902013-08-06 10:49:15 +0000434 const DWARFDebugInfoEntryInlinedChain &InlinedChain =
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000435 CU->getInlinedChainForAddress(Address);
Alexey Samsonove6642902013-08-06 10:49:15 +0000436 if (InlinedChain.DIEs.size() > 0) {
437 const DWARFDebugInfoEntryMinimal &TopFunctionDIE = InlinedChain.DIEs[0];
David Blaikiecd7c4982013-09-23 22:44:40 +0000438 if (const char *Name = TopFunctionDIE.getSubroutineName(InlinedChain.U))
Alexey Samsonov38a63812012-08-30 07:49:50 +0000439 FunctionName = Name;
Alexey Samsonov9d26b0b2012-07-17 15:28:35 +0000440 }
Alexey Samsonov3e25c4a2012-07-02 05:54:45 +0000441 }
Alexey Samsonov38a63812012-08-30 07:49:50 +0000442 if (Specifier.needs(DILineInfoSpecifier::FileLineInfo)) {
Eric Christophere9403c12012-10-16 23:46:25 +0000443 const DWARFLineTable *LineTable = getLineTableForCompileUnit(CU);
Alexey Samsonov38a63812012-08-30 07:49:50 +0000444 const bool NeedsAbsoluteFilePath =
445 Specifier.needs(DILineInfoSpecifier::AbsoluteFilePath);
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000446 getFileLineInfoForCompileUnit(CU, LineTable, Address,
447 NeedsAbsoluteFilePath,
Alexey Samsonov38a63812012-08-30 07:49:50 +0000448 FileName, Line, Column);
Alexey Samsonov3e25c4a2012-07-02 05:54:45 +0000449 }
Alexey Samsonov38a63812012-08-30 07:49:50 +0000450 return DILineInfo(StringRef(FileName), StringRef(FunctionName),
451 Line, Column);
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000452}
David Blaikie2d24e2a2011-12-20 02:50:00 +0000453
Andrew Kaylore27a7872013-01-26 00:28:05 +0000454DILineInfoTable DWARFContext::getLineInfoForAddressRange(uint64_t Address,
455 uint64_t Size,
456 DILineInfoSpecifier Specifier) {
457 DILineInfoTable Lines;
458 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
459 if (!CU)
460 return Lines;
461
462 std::string FunctionName = "<invalid>";
463 if (Specifier.needs(DILineInfoSpecifier::FunctionName)) {
464 // The address may correspond to instruction in some inlined function,
465 // so we have to build the chain of inlined functions and take the
466 // name of the topmost function in it.
Alexey Samsonove6642902013-08-06 10:49:15 +0000467 const DWARFDebugInfoEntryInlinedChain &InlinedChain =
Andrew Kaylore27a7872013-01-26 00:28:05 +0000468 CU->getInlinedChainForAddress(Address);
Alexey Samsonove6642902013-08-06 10:49:15 +0000469 if (InlinedChain.DIEs.size() > 0) {
470 const DWARFDebugInfoEntryMinimal &TopFunctionDIE = InlinedChain.DIEs[0];
David Blaikiecd7c4982013-09-23 22:44:40 +0000471 if (const char *Name = TopFunctionDIE.getSubroutineName(InlinedChain.U))
Andrew Kaylore27a7872013-01-26 00:28:05 +0000472 FunctionName = Name;
473 }
474 }
475
Andrew Kaylore27a7872013-01-26 00:28:05 +0000476 // If the Specifier says we don't need FileLineInfo, just
477 // return the top-most function at the starting address.
478 if (!Specifier.needs(DILineInfoSpecifier::FileLineInfo)) {
David Blaikieeaad5cd2013-09-22 17:01:50 +0000479 Lines.push_back(
480 std::make_pair(Address, DILineInfo("<invalid>", FunctionName, 0, 0)));
Andrew Kaylore27a7872013-01-26 00:28:05 +0000481 return Lines;
482 }
483
484 const DWARFLineTable *LineTable = getLineTableForCompileUnit(CU);
485 const bool NeedsAbsoluteFilePath =
486 Specifier.needs(DILineInfoSpecifier::AbsoluteFilePath);
487
488 // Get the index of row we're looking for in the line table.
489 std::vector<uint32_t> RowVector;
490 if (!LineTable->lookupAddressRange(Address, Size, RowVector))
491 return Lines;
492
493 uint32_t NumRows = RowVector.size();
494 for (uint32_t i = 0; i < NumRows; ++i) {
495 uint32_t RowIndex = RowVector[i];
496 // Take file number and line/column from the row.
497 const DWARFDebugLine::Row &Row = LineTable->Rows[RowIndex];
498 std::string FileName = "<invalid>";
499 getFileNameForCompileUnit(CU, LineTable, Row.File,
500 NeedsAbsoluteFilePath, FileName);
David Blaikieeaad5cd2013-09-22 17:01:50 +0000501 Lines.push_back(std::make_pair(
502 Row.Address, DILineInfo(FileName, FunctionName, Row.Line, Row.Column)));
Andrew Kaylore27a7872013-01-26 00:28:05 +0000503 }
504
505 return Lines;
506}
507
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000508DIInliningInfo DWARFContext::getInliningInfoForAddress(uint64_t Address,
509 DILineInfoSpecifier Specifier) {
510 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
511 if (!CU)
512 return DIInliningInfo();
513
Alexey Samsonove6642902013-08-06 10:49:15 +0000514 const DWARFDebugInfoEntryInlinedChain &InlinedChain =
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000515 CU->getInlinedChainForAddress(Address);
Alexey Samsonove6642902013-08-06 10:49:15 +0000516 if (InlinedChain.DIEs.size() == 0)
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000517 return DIInliningInfo();
518
519 DIInliningInfo InliningInfo;
520 uint32_t CallFile = 0, CallLine = 0, CallColumn = 0;
Eric Christophere9403c12012-10-16 23:46:25 +0000521 const DWARFLineTable *LineTable = 0;
Alexey Samsonove6642902013-08-06 10:49:15 +0000522 for (uint32_t i = 0, n = InlinedChain.DIEs.size(); i != n; i++) {
523 const DWARFDebugInfoEntryMinimal &FunctionDIE = InlinedChain.DIEs[i];
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000524 std::string FileName = "<invalid>";
525 std::string FunctionName = "<invalid>";
526 uint32_t Line = 0;
527 uint32_t Column = 0;
528 // Get function name if necessary.
529 if (Specifier.needs(DILineInfoSpecifier::FunctionName)) {
David Blaikiecd7c4982013-09-23 22:44:40 +0000530 if (const char *Name = FunctionDIE.getSubroutineName(InlinedChain.U))
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000531 FunctionName = Name;
532 }
533 if (Specifier.needs(DILineInfoSpecifier::FileLineInfo)) {
534 const bool NeedsAbsoluteFilePath =
535 Specifier.needs(DILineInfoSpecifier::AbsoluteFilePath);
536 if (i == 0) {
537 // For the topmost frame, initialize the line table of this
538 // compile unit and fetch file/line info from it.
539 LineTable = getLineTableForCompileUnit(CU);
540 // For the topmost routine, get file/line info from line table.
541 getFileLineInfoForCompileUnit(CU, LineTable, Address,
542 NeedsAbsoluteFilePath,
543 FileName, Line, Column);
544 } else {
545 // Otherwise, use call file, call line and call column from
546 // previous DIE in inlined chain.
547 getFileNameForCompileUnit(CU, LineTable, CallFile,
548 NeedsAbsoluteFilePath, FileName);
549 Line = CallLine;
550 Column = CallColumn;
551 }
552 // Get call file/line/column of a current DIE.
553 if (i + 1 < n) {
David Blaikiecd7c4982013-09-23 22:44:40 +0000554 FunctionDIE.getCallerFrame(InlinedChain.U, CallFile, CallLine,
Alexey Samsonove6642902013-08-06 10:49:15 +0000555 CallColumn);
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000556 }
557 }
558 DILineInfo Frame(StringRef(FileName), StringRef(FunctionName),
559 Line, Column);
560 InliningInfo.addFrame(Frame);
561 }
562 return InliningInfo;
563}
564
Alexey Samsonov005159e2013-04-23 10:17:34 +0000565static bool consumeCompressedDebugSectionHeader(StringRef &data,
566 uint64_t &OriginalSize) {
567 // Consume "ZLIB" prefix.
568 if (!data.startswith("ZLIB"))
569 return false;
570 data = data.substr(4);
571 // Consume uncompressed section size (big-endian 8 bytes).
572 DataExtractor extractor(data, false, 8);
573 uint32_t Offset = 0;
574 OriginalSize = extractor.getU64(&Offset);
575 if (Offset == 0)
576 return false;
577 data = data.substr(Offset);
578 return true;
579}
580
Eric Christopherd1726a42012-11-12 21:40:38 +0000581DWARFContextInMemory::DWARFContextInMemory(object::ObjectFile *Obj) :
Eli Bendersky60bdc5b2013-02-05 23:30:58 +0000582 IsLittleEndian(Obj->isLittleEndian()),
583 AddressSize(Obj->getBytesInAddress()) {
Eric Christopherd1726a42012-11-12 21:40:38 +0000584 error_code ec;
585 for (object::section_iterator i = Obj->begin_sections(),
586 e = Obj->end_sections();
587 i != e; i.increment(ec)) {
588 StringRef name;
589 i->getName(name);
590 StringRef data;
591 i->getContents(data);
592
Eric Christopherd1726a42012-11-12 21:40:38 +0000593 name = name.substr(name.find_first_not_of("._")); // Skip . and _ prefixes.
Alexey Samsonov784baa62013-04-17 14:27:04 +0000594
Alexey Samsonov005159e2013-04-23 10:17:34 +0000595 // Check if debug info section is compressed with zlib.
596 if (name.startswith("zdebug_")) {
597 uint64_t OriginalSize;
598 if (!zlib::isAvailable() ||
599 !consumeCompressedDebugSectionHeader(data, OriginalSize))
600 continue;
601 OwningPtr<MemoryBuffer> UncompressedSection;
602 if (zlib::uncompress(data, UncompressedSection, OriginalSize) !=
603 zlib::StatusOK)
604 continue;
605 // Make data point to uncompressed section contents and save its contents.
606 name = name.substr(1);
607 data = UncompressedSection->getBuffer();
608 UncompressedSections.push_back(UncompressedSection.take());
609 }
610
Eric Christopher7357f032013-09-25 23:02:41 +0000611 StringRef *Section =
612 StringSwitch<StringRef *>(name)
613 .Case("debug_info", &InfoSection.Data)
614 .Case("debug_abbrev", &AbbrevSection)
615 .Case("debug_loc", &LocSection.Data)
616 .Case("debug_line", &LineSection.Data)
617 .Case("debug_aranges", &ARangeSection)
618 .Case("debug_frame", &DebugFrameSection)
619 .Case("debug_str", &StringSection)
620 .Case("debug_ranges", &RangeSection)
621 .Case("debug_pubnames", &PubNamesSection)
622 .Case("debug_pubtypes", &PubTypesSection)
623 .Case("debug_gnu_pubnames", &GnuPubNamesSection)
624 .Case("debug_gnu_pubtypes", &GnuPubTypesSection)
625 .Case("debug_info.dwo", &InfoDWOSection.Data)
626 .Case("debug_abbrev.dwo", &AbbrevDWOSection)
627 .Case("debug_str.dwo", &StringDWOSection)
628 .Case("debug_str_offsets.dwo", &StringOffsetDWOSection)
629 .Case("debug_addr", &AddrSection)
630 // Any more debug info sections go here.
631 .Default(0);
Rafael Espindola7486d922013-05-30 03:05:14 +0000632 if (Section) {
633 *Section = data;
634 if (name == "debug_ranges") {
635 // FIXME: Use the other dwo range section when we emit it.
636 RangeDWOSection = data;
637 }
David Blaikie438f5392013-09-23 22:44:47 +0000638 } else if (name == "debug_types") {
David Blaikie75331652013-09-23 23:39:55 +0000639 // Find debug_types data by section rather than name as there are
640 // multiple, comdat grouped, debug_types sections.
David Blaikie438f5392013-09-23 22:44:47 +0000641 TypesSections[*i].Data = data;
Eric Christopher82de10a2013-01-02 23:52:13 +0000642 }
Eric Christopherd1726a42012-11-12 21:40:38 +0000643
Rafael Espindola7486d922013-05-30 03:05:14 +0000644 section_iterator RelocatedSection = i->getRelocatedSection();
645 if (RelocatedSection == Obj->end_sections())
646 continue;
647
648 StringRef RelSecName;
649 RelocatedSection->getName(RelSecName);
650 RelSecName = RelSecName.substr(
651 RelSecName.find_first_not_of("._")); // Skip . and _ prefixes.
652
Andrew Kayloree7c0d22013-01-25 22:50:58 +0000653 // TODO: Add support for relocations in other sections as needed.
654 // Record relocations for the debug_info and debug_line sections.
Rafael Espindola7486d922013-05-30 03:05:14 +0000655 RelocAddrMap *Map = StringSwitch<RelocAddrMap*>(RelSecName)
David Blaikie9528b0e2013-09-23 17:42:01 +0000656 .Case("debug_info", &InfoSection.Relocs)
657 .Case("debug_loc", &LocSection.Relocs)
658 .Case("debug_info.dwo", &InfoDWOSection.Relocs)
659 .Case("debug_line", &LineSection.Relocs)
Alexey Samsonov784baa62013-04-17 14:27:04 +0000660 .Default(0);
David Blaikie438f5392013-09-23 22:44:47 +0000661 if (!Map) {
662 if (RelSecName != "debug_types")
663 continue;
David Blaikie75331652013-09-23 23:39:55 +0000664 // Find debug_types relocs by section rather than name as there are
665 // multiple, comdat grouped, debug_types sections.
David Blaikie438f5392013-09-23 22:44:47 +0000666 Map = &TypesSections[*RelocatedSection].Relocs;
667 }
Eric Christopherd1726a42012-11-12 21:40:38 +0000668
669 if (i->begin_relocations() != i->end_relocations()) {
670 uint64_t SectionSize;
Rafael Espindola7486d922013-05-30 03:05:14 +0000671 RelocatedSection->getSize(SectionSize);
Eric Christopherd1726a42012-11-12 21:40:38 +0000672 for (object::relocation_iterator reloc_i = i->begin_relocations(),
673 reloc_e = i->end_relocations();
674 reloc_i != reloc_e; reloc_i.increment(ec)) {
675 uint64_t Address;
Rafael Espindola956ca722013-04-25 12:28:45 +0000676 reloc_i->getOffset(Address);
Eric Christopherd1726a42012-11-12 21:40:38 +0000677 uint64_t Type;
678 reloc_i->getType(Type);
Andrew Kayloree7c0d22013-01-25 22:50:58 +0000679 uint64_t SymAddr = 0;
680 // ELF relocations may need the symbol address
681 if (Obj->isELF()) {
Rafael Espindola6c1202c2013-06-05 01:33:53 +0000682 object::symbol_iterator Sym = reloc_i->getSymbol();
683 Sym->getAddress(SymAddr);
Andrew Kayloree7c0d22013-01-25 22:50:58 +0000684 }
Eric Christopherd1726a42012-11-12 21:40:38 +0000685
686 object::RelocVisitor V(Obj->getFileFormatName());
687 // The section address is always 0 for debug sections.
Andrew Kayloree7c0d22013-01-25 22:50:58 +0000688 object::RelocToApply R(V.visit(Type, *reloc_i, 0, SymAddr));
Eric Christopherd1726a42012-11-12 21:40:38 +0000689 if (V.error()) {
690 SmallString<32> Name;
691 error_code ec(reloc_i->getTypeName(Name));
692 if (ec) {
693 errs() << "Aaaaaa! Nameless relocation! Aaaaaa!\n";
694 }
695 errs() << "error: failed to compute relocation: "
696 << Name << "\n";
697 continue;
698 }
699
700 if (Address + R.Width > SectionSize) {
701 errs() << "error: " << R.Width << "-byte relocation starting "
702 << Address << " bytes into section " << name << " which is "
703 << SectionSize << " bytes long.\n";
704 continue;
705 }
706 if (R.Width > 8) {
707 errs() << "error: can't handle a relocation of more than 8 bytes at "
708 "a time.\n";
709 continue;
710 }
711 DEBUG(dbgs() << "Writing " << format("%p", R.Value)
712 << " at " << format("%p", Address)
713 << " with width " << format("%d", R.Width)
714 << "\n");
Eric Christopher82de10a2013-01-02 23:52:13 +0000715 Map->insert(std::make_pair(Address, std::make_pair(R.Width, R.Value)));
Eric Christopherd1726a42012-11-12 21:40:38 +0000716 }
717 }
718 }
719}
720
Alexey Samsonov005159e2013-04-23 10:17:34 +0000721DWARFContextInMemory::~DWARFContextInMemory() {
722 DeleteContainerPointers(UncompressedSections);
723}
724
David Blaikie2d24e2a2011-12-20 02:50:00 +0000725void DWARFContextInMemory::anchor() { }