blob: 7a7e176bcd9fda3868fb9238dbd07467bfff95ba [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
David Blaikie9ddf28d2013-09-24 19:50:00 +000031static void dumpPubSection(raw_ostream &OS, StringRef Name, StringRef Data,
32 bool LittleEndian) {
33 OS << "\n." << Name << " contents:\n";
34 DataExtractor pubNames(Data, LittleEndian, 0);
35 uint32_t offset = 0;
36 OS << "Length: " << pubNames.getU32(&offset) << "\n";
37 OS << "Version: " << pubNames.getU16(&offset) << "\n";
38 OS << "Offset in .debug_info: " << pubNames.getU32(&offset) << "\n";
39 OS << "Size: " << pubNames.getU32(&offset) << "\n";
40 OS << "Offset Linkage Kind Name\n";
41 while (offset < Data.size()) {
42 uint32_t dieRef = pubNames.getU32(&offset);
43 if (dieRef == 0)
44 break;
45 PubIndexEntryDescriptor desc(pubNames.getU8(&offset));
46 OS << format("0x%8.8x ", dieRef)
47 << format("%-8s", dwarf::GDBIndexEntryLinkageString(desc.Linkage)) << ' '
David Blaikie0c5e6c62013-09-24 20:23:36 +000048 << format("%-8s", dwarf::GDBIndexEntryKindString(desc.Kind)) << '\"'
David Blaikie9ddf28d2013-09-24 19:50:00 +000049 << pubNames.getCStr(&offset) << "\"\n";
50 }
51}
52
Eli Bendersky939a4e82013-01-25 20:26:43 +000053void DWARFContext::dump(raw_ostream &OS, DIDumpType DumpType) {
54 if (DumpType == DIDT_All || DumpType == DIDT_Abbrev) {
55 OS << ".debug_abbrev contents:\n";
56 getDebugAbbrev()->dump(OS);
57 }
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000058
Eli Bendersky939a4e82013-01-25 20:26:43 +000059 if (DumpType == DIDT_All || DumpType == DIDT_Info) {
60 OS << "\n.debug_info contents:\n";
61 for (unsigned i = 0, e = getNumCompileUnits(); i != e; ++i)
62 getCompileUnitAtIndex(i)->dump(OS);
63 }
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000064
David Blaikie438f5392013-09-23 22:44:47 +000065 if (DumpType == DIDT_All || DumpType == DIDT_Types) {
66 OS << "\n.debug_types contents:\n";
67 for (unsigned i = 0, e = getNumTypeUnits(); i != e; ++i)
68 getTypeUnitAtIndex(i)->dump(OS);
69 }
70
David Blaikie3df7d2f2013-06-19 21:37:13 +000071 if (DumpType == DIDT_All || DumpType == DIDT_Loc) {
David Blaikie438f5392013-09-23 22:44:47 +000072 OS << "\n.debug_loc contents:\n";
David Blaikie3df7d2f2013-06-19 21:37:13 +000073 getDebugLoc()->dump(OS);
74 }
75
Eli Bendersky60bdc5b2013-02-05 23:30:58 +000076 if (DumpType == DIDT_All || DumpType == DIDT_Frames) {
77 OS << "\n.debug_frame contents:\n";
78 getDebugFrame()->dump(OS);
79 }
80
Benjamin Kramer358f4fd2011-09-14 01:09:52 +000081 uint32_t offset = 0;
Eli Bendersky939a4e82013-01-25 20:26:43 +000082 if (DumpType == DIDT_All || DumpType == DIDT_Aranges) {
83 OS << "\n.debug_aranges contents:\n";
84 DataExtractor arangesData(getARangeSection(), isLittleEndian(), 0);
85 DWARFDebugArangeSet set;
86 while (set.extract(arangesData, &offset))
87 set.dump(OS);
88 }
Benjamin Kramerb848e972011-09-15 02:12:05 +000089
Alexey Samsonoveceb5b92012-08-27 07:17:47 +000090 uint8_t savedAddressByteSize = 0;
Eli Bendersky939a4e82013-01-25 20:26:43 +000091 if (DumpType == DIDT_All || DumpType == DIDT_Line) {
92 OS << "\n.debug_line contents:\n";
93 for (unsigned i = 0, e = getNumCompileUnits(); i != e; ++i) {
94 DWARFCompileUnit *cu = getCompileUnitAtIndex(i);
95 savedAddressByteSize = cu->getAddressByteSize();
96 unsigned stmtOffset =
97 cu->getCompileUnitDIE()->getAttributeValueAsUnsigned(cu, DW_AT_stmt_list,
98 -1U);
99 if (stmtOffset != -1U) {
David Blaikie9528b0e2013-09-23 17:42:01 +0000100 DataExtractor lineData(getLineSection().Data, isLittleEndian(),
Eli Bendersky939a4e82013-01-25 20:26:43 +0000101 savedAddressByteSize);
102 DWARFDebugLine::DumpingState state(OS);
David Blaikie9528b0e2013-09-23 17:42:01 +0000103 DWARFDebugLine::parseStatementTable(lineData, &getLineSection().Relocs, &stmtOffset, state);
Eli Bendersky939a4e82013-01-25 20:26:43 +0000104 }
Benjamin Kramerfe80f1d2011-09-15 18:02:20 +0000105 }
106 }
Benjamin Kramer34f864f2011-09-15 16:57:13 +0000107
Eli Bendersky939a4e82013-01-25 20:26:43 +0000108 if (DumpType == DIDT_All || DumpType == DIDT_Str) {
109 OS << "\n.debug_str contents:\n";
110 DataExtractor strData(getStringSection(), isLittleEndian(), 0);
111 offset = 0;
112 uint32_t strOffset = 0;
113 while (const char *s = strData.getCStr(&offset)) {
114 OS << format("0x%8.8x: \"%s\"\n", strOffset, s);
115 strOffset = offset;
116 }
Benjamin Kramer34f864f2011-09-15 16:57:13 +0000117 }
Alexey Samsonoveceb5b92012-08-27 07:17:47 +0000118
Eli Bendersky939a4e82013-01-25 20:26:43 +0000119 if (DumpType == DIDT_All || DumpType == DIDT_Ranges) {
120 OS << "\n.debug_ranges contents:\n";
121 // In fact, different compile units may have different address byte
122 // sizes, but for simplicity we just use the address byte size of the last
123 // compile unit (there is no easy and fast way to associate address range
124 // list and the compile unit it describes).
125 DataExtractor rangesData(getRangeSection(), isLittleEndian(),
126 savedAddressByteSize);
127 offset = 0;
128 DWARFDebugRangeList rangeList;
129 while (rangeList.extract(rangesData, &offset))
130 rangeList.dump(OS);
Eric Christopher82de10a2013-01-02 23:52:13 +0000131 }
Eric Christopher72f7bfb2013-01-15 23:56:56 +0000132
Krzysztof Parzyszeke38825f2013-02-12 16:20:28 +0000133 if (DumpType == DIDT_All || DumpType == DIDT_Pubnames) {
134 OS << "\n.debug_pubnames contents:\n";
135 DataExtractor pubNames(getPubNamesSection(), isLittleEndian(), 0);
136 offset = 0;
137 OS << "Length: " << pubNames.getU32(&offset) << "\n";
138 OS << "Version: " << pubNames.getU16(&offset) << "\n";
139 OS << "Offset in .debug_info: " << pubNames.getU32(&offset) << "\n";
140 OS << "Size: " << pubNames.getU32(&offset) << "\n";
141 OS << "\n Offset Name\n";
142 while (offset < getPubNamesSection().size()) {
143 uint32_t n = pubNames.getU32(&offset);
144 if (n == 0)
145 break;
146 OS << format("%8x ", n);
147 OS << pubNames.getCStr(&offset) << "\n";
148 }
149 }
150
David Blaikie9ddf28d2013-09-24 19:50:00 +0000151 if (DumpType == DIDT_All || DumpType == DIDT_GnuPubnames)
152 dumpPubSection(OS, "debug_gnu_pubnames", getGnuPubNamesSection(),
153 isLittleEndian());
154
155 if (DumpType == DIDT_All || DumpType == DIDT_GnuPubtypes)
156 dumpPubSection(OS, "debug_gnu_pubtypes", getGnuPubTypesSection(),
157 isLittleEndian());
David Blaikie994c37f2013-09-19 23:01:29 +0000158
Eli Bendersky939a4e82013-01-25 20:26:43 +0000159 if (DumpType == DIDT_All || DumpType == DIDT_AbbrevDwo) {
Eric Christopher93f3fed2013-05-06 17:50:42 +0000160 const DWARFDebugAbbrev *D = getDebugAbbrevDWO();
161 if (D) {
162 OS << "\n.debug_abbrev.dwo contents:\n";
163 getDebugAbbrevDWO()->dump(OS);
Eli Bendersky939a4e82013-01-25 20:26:43 +0000164 }
165 }
166
Eric Christopher93f3fed2013-05-06 17:50:42 +0000167 if (DumpType == DIDT_All || DumpType == DIDT_InfoDwo)
168 if (getNumDWOCompileUnits()) {
169 OS << "\n.debug_info.dwo contents:\n";
170 for (unsigned i = 0, e = getNumDWOCompileUnits(); i != e; ++i)
Eric Christopher5a0c3662013-05-06 21:19:41 +0000171 getDWOCompileUnitAtIndex(i)->dump(OS);
Eli Bendersky939a4e82013-01-25 20:26:43 +0000172 }
Eric Christopher93f3fed2013-05-06 17:50:42 +0000173
174 if (DumpType == DIDT_All || DumpType == DIDT_StrDwo)
175 if (!getStringDWOSection().empty()) {
176 OS << "\n.debug_str.dwo contents:\n";
177 DataExtractor strDWOData(getStringDWOSection(), isLittleEndian(), 0);
178 offset = 0;
179 uint32_t strDWOOffset = 0;
180 while (const char *s = strDWOData.getCStr(&offset)) {
Eric Christopher5a0c3662013-05-06 21:19:41 +0000181 OS << format("0x%8.8x: \"%s\"\n", strDWOOffset, s);
182 strDWOOffset = offset;
Eric Christopher93f3fed2013-05-06 17:50:42 +0000183 }
184 }
185
186 if (DumpType == DIDT_All || DumpType == DIDT_StrOffsetsDwo)
187 if (!getStringOffsetDWOSection().empty()) {
188 OS << "\n.debug_str_offsets.dwo contents:\n";
189 DataExtractor strOffsetExt(getStringOffsetDWOSection(), isLittleEndian(), 0);
190 offset = 0;
Eric Christophere305e032013-05-06 21:19:44 +0000191 uint64_t size = getStringOffsetDWOSection().size();
192 while (offset < size) {
Eric Christopher5a0c3662013-05-06 21:19:41 +0000193 OS << format("0x%8.8x: ", offset);
194 OS << format("%8.8x\n", strOffsetExt.getU32(&offset));
Eric Christopher93f3fed2013-05-06 17:50:42 +0000195 }
196 }
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000197}
198
199const DWARFDebugAbbrev *DWARFContext::getDebugAbbrev() {
200 if (Abbrev)
201 return Abbrev.get();
202
203 DataExtractor abbrData(getAbbrevSection(), isLittleEndian(), 0);
204
205 Abbrev.reset(new DWARFDebugAbbrev());
206 Abbrev->parse(abbrData);
207 return Abbrev.get();
208}
209
Eric Christopher82de10a2013-01-02 23:52:13 +0000210const DWARFDebugAbbrev *DWARFContext::getDebugAbbrevDWO() {
211 if (AbbrevDWO)
212 return AbbrevDWO.get();
213
214 DataExtractor abbrData(getAbbrevDWOSection(), isLittleEndian(), 0);
215 AbbrevDWO.reset(new DWARFDebugAbbrev());
216 AbbrevDWO->parse(abbrData);
217 return AbbrevDWO.get();
218}
219
David Blaikie3df7d2f2013-06-19 21:37:13 +0000220const DWARFDebugLoc *DWARFContext::getDebugLoc() {
221 if (Loc)
222 return Loc.get();
223
David Blaikie9528b0e2013-09-23 17:42:01 +0000224 DataExtractor LocData(getLocSection().Data, isLittleEndian(), 0);
225 Loc.reset(new DWARFDebugLoc(getLocSection().Relocs));
David Blaikie3df7d2f2013-06-19 21:37:13 +0000226 // assume all compile units have the same address byte size
227 if (getNumCompileUnits())
228 Loc->parse(LocData, getCompileUnitAtIndex(0)->getAddressByteSize());
229 return Loc.get();
230}
231
Benjamin Kramer358f4fd2011-09-14 01:09:52 +0000232const DWARFDebugAranges *DWARFContext::getDebugAranges() {
233 if (Aranges)
234 return Aranges.get();
235
236 DataExtractor arangesData(getARangeSection(), isLittleEndian(), 0);
237
238 Aranges.reset(new DWARFDebugAranges());
239 Aranges->extract(arangesData);
Alexey Samsonov63a450a2012-11-16 08:36:25 +0000240 // Generate aranges from DIEs: even if .debug_aranges section is present,
241 // it may describe only a small subset of compilation units, so we need to
242 // manually build aranges for the rest of them.
243 Aranges->generate(this);
Benjamin Kramer358f4fd2011-09-14 01:09:52 +0000244 return Aranges.get();
245}
246
Eli Bendersky60bdc5b2013-02-05 23:30:58 +0000247const DWARFDebugFrame *DWARFContext::getDebugFrame() {
248 if (DebugFrame)
249 return DebugFrame.get();
250
251 // There's a "bug" in the DWARFv3 standard with respect to the target address
252 // size within debug frame sections. While DWARF is supposed to be independent
253 // of its container, FDEs have fields with size being "target address size",
254 // which isn't specified in DWARF in general. It's only specified for CUs, but
255 // .eh_frame can appear without a .debug_info section. Follow the example of
256 // other tools (libdwarf) and extract this from the container (ObjectFile
257 // provides this information). This problem is fixed in DWARFv4
258 // See this dwarf-discuss discussion for more details:
259 // http://lists.dwarfstd.org/htdig.cgi/dwarf-discuss-dwarfstd.org/2011-December/001173.html
260 DataExtractor debugFrameData(getDebugFrameSection(), isLittleEndian(),
261 getAddressSize());
262 DebugFrame.reset(new DWARFDebugFrame());
263 DebugFrame->parse(debugFrameData);
264 return DebugFrame.get();
265}
266
Eric Christophere9403c12012-10-16 23:46:25 +0000267const DWARFLineTable *
Benjamin Kramerc26ed9b2011-09-15 20:43:18 +0000268DWARFContext::getLineTableForCompileUnit(DWARFCompileUnit *cu) {
269 if (!Line)
David Blaikie9528b0e2013-09-23 17:42:01 +0000270 Line.reset(new DWARFDebugLine(&getLineSection().Relocs));
Benjamin Kramerb848e972011-09-15 02:12:05 +0000271
Benjamin Kramerc26ed9b2011-09-15 20:43:18 +0000272 unsigned stmtOffset =
273 cu->getCompileUnitDIE()->getAttributeValueAsUnsigned(cu, DW_AT_stmt_list,
274 -1U);
275 if (stmtOffset == -1U)
276 return 0; // No line table for this compile unit.
Benjamin Kramerb848e972011-09-15 02:12:05 +0000277
Benjamin Kramerc26ed9b2011-09-15 20:43:18 +0000278 // See if the line table is cached.
Eric Christophere9403c12012-10-16 23:46:25 +0000279 if (const DWARFLineTable *lt = Line->getLineTable(stmtOffset))
Benjamin Kramerc26ed9b2011-09-15 20:43:18 +0000280 return lt;
281
282 // We have to parse it first.
David Blaikie9528b0e2013-09-23 17:42:01 +0000283 DataExtractor lineData(getLineSection().Data, isLittleEndian(),
Benjamin Kramerc26ed9b2011-09-15 20:43:18 +0000284 cu->getAddressByteSize());
285 return Line->getOrParseLineTable(lineData, stmtOffset);
Benjamin Kramerb848e972011-09-15 02:12:05 +0000286}
287
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000288void DWARFContext::parseCompileUnits() {
289 uint32_t offset = 0;
David Blaikie9528b0e2013-09-23 17:42:01 +0000290 const DataExtractor &DIData = DataExtractor(getInfoSection().Data,
Eric Christopherb69b55f2012-10-16 23:46:23 +0000291 isLittleEndian(), 0);
292 while (DIData.isValidOffset(offset)) {
Alexey Samsonoveb0c1792013-08-23 06:56:01 +0000293 OwningPtr<DWARFCompileUnit> CU(new DWARFCompileUnit(
David Blaikie9528b0e2013-09-23 17:42:01 +0000294 getDebugAbbrev(), getInfoSection().Data, getAbbrevSection(),
Alexey Samsonoveb0c1792013-08-23 06:56:01 +0000295 getRangeSection(), getStringSection(), StringRef(), getAddrSection(),
David Blaikie9528b0e2013-09-23 17:42:01 +0000296 &getInfoSection().Relocs, isLittleEndian()));
Alexey Samsonoveb0c1792013-08-23 06:56:01 +0000297 if (!CU->extract(DIData, &offset)) {
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000298 break;
299 }
Alexey Samsonoveb0c1792013-08-23 06:56:01 +0000300 CUs.push_back(CU.take());
David Blaikiecd7c4982013-09-23 22:44:40 +0000301 offset = CUs.back()->getNextUnitOffset();
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000302 }
303}
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000304
David Blaikie438f5392013-09-23 22:44:47 +0000305void DWARFContext::parseTypeUnits() {
306 const std::map<object::SectionRef, Section> &Sections = getTypesSections();
307 for (std::map<object::SectionRef, Section>::const_iterator
308 I = Sections.begin(),
309 E = Sections.end();
310 I != E; ++I) {
311 uint32_t offset = 0;
312 const DataExtractor &DIData =
313 DataExtractor(I->second.Data, isLittleEndian(), 0);
314 while (DIData.isValidOffset(offset)) {
315 OwningPtr<DWARFTypeUnit> TU(new DWARFTypeUnit(
316 getDebugAbbrev(), I->second.Data, getAbbrevSection(),
317 getRangeSection(), getStringSection(), StringRef(), getAddrSection(),
318 &I->second.Relocs, isLittleEndian()));
319 if (!TU->extract(DIData, &offset))
320 break;
321 TUs.push_back(TU.take());
322 offset = TUs.back()->getNextUnitOffset();
323 }
324 }
325}
326
Eric Christopher82de10a2013-01-02 23:52:13 +0000327void DWARFContext::parseDWOCompileUnits() {
328 uint32_t offset = 0;
David Blaikie9528b0e2013-09-23 17:42:01 +0000329 const DataExtractor &DIData =
330 DataExtractor(getInfoDWOSection().Data, isLittleEndian(), 0);
Eric Christopher82de10a2013-01-02 23:52:13 +0000331 while (DIData.isValidOffset(offset)) {
Alexey Samsonoveb0c1792013-08-23 06:56:01 +0000332 OwningPtr<DWARFCompileUnit> DWOCU(new DWARFCompileUnit(
David Blaikie9528b0e2013-09-23 17:42:01 +0000333 getDebugAbbrevDWO(), getInfoDWOSection().Data, getAbbrevDWOSection(),
Alexey Samsonoveb0c1792013-08-23 06:56:01 +0000334 getRangeDWOSection(), getStringDWOSection(),
David Blaikie9528b0e2013-09-23 17:42:01 +0000335 getStringOffsetDWOSection(), getAddrSection(),
336 &getInfoDWOSection().Relocs, isLittleEndian()));
Alexey Samsonoveb0c1792013-08-23 06:56:01 +0000337 if (!DWOCU->extract(DIData, &offset)) {
Eric Christopher82de10a2013-01-02 23:52:13 +0000338 break;
339 }
Alexey Samsonoveb0c1792013-08-23 06:56:01 +0000340 DWOCUs.push_back(DWOCU.take());
David Blaikiecd7c4982013-09-23 22:44:40 +0000341 offset = DWOCUs.back()->getNextUnitOffset();
Eric Christopher82de10a2013-01-02 23:52:13 +0000342 }
343}
344
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000345namespace {
346 struct OffsetComparator {
Alexey Samsonoveb0c1792013-08-23 06:56:01 +0000347 bool operator()(const DWARFCompileUnit *LHS,
348 const DWARFCompileUnit *RHS) const {
349 return LHS->getOffset() < RHS->getOffset();
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000350 }
Alexey Samsonoveb0c1792013-08-23 06:56:01 +0000351 bool operator()(const DWARFCompileUnit *LHS, uint32_t RHS) const {
352 return LHS->getOffset() < RHS;
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000353 }
Alexey Samsonoveb0c1792013-08-23 06:56:01 +0000354 bool operator()(uint32_t LHS, const DWARFCompileUnit *RHS) const {
355 return LHS < RHS->getOffset();
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000356 }
357 };
358}
359
Alexey Samsonov38a63812012-08-30 07:49:50 +0000360DWARFCompileUnit *DWARFContext::getCompileUnitForOffset(uint32_t Offset) {
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000361 if (CUs.empty())
362 parseCompileUnits();
363
Alexey Samsonoveb0c1792013-08-23 06:56:01 +0000364 DWARFCompileUnit **CU =
365 std::lower_bound(CUs.begin(), CUs.end(), Offset, OffsetComparator());
366 if (CU != CUs.end()) {
367 return *CU;
368 }
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000369 return 0;
370}
371
Alexey Samsonov38a63812012-08-30 07:49:50 +0000372DWARFCompileUnit *DWARFContext::getCompileUnitForAddress(uint64_t Address) {
Benjamin Kramer9013db32011-09-15 21:59:13 +0000373 // First, get the offset of the compile unit.
Alexey Samsonov38a63812012-08-30 07:49:50 +0000374 uint32_t CUOffset = getDebugAranges()->findAddress(Address);
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000375 // Retrieve the compile unit.
Alexey Samsonov38a63812012-08-30 07:49:50 +0000376 return getCompileUnitForOffset(CUOffset);
377}
378
Eric Christophere9403c12012-10-16 23:46:25 +0000379static bool getFileNameForCompileUnit(DWARFCompileUnit *CU,
380 const DWARFLineTable *LineTable,
381 uint64_t FileIndex,
382 bool NeedsAbsoluteFilePath,
383 std::string &FileName) {
Alexey Samsonov38a63812012-08-30 07:49:50 +0000384 if (CU == 0 ||
385 LineTable == 0 ||
386 !LineTable->getFileNameByIndex(FileIndex, NeedsAbsoluteFilePath,
387 FileName))
388 return false;
389 if (NeedsAbsoluteFilePath && sys::path::is_relative(FileName)) {
390 // We may still need to append compilation directory of compile unit.
391 SmallString<16> AbsolutePath;
392 if (const char *CompilationDir = CU->getCompilationDir()) {
393 sys::path::append(AbsolutePath, CompilationDir);
394 }
395 sys::path::append(AbsolutePath, FileName);
396 FileName = AbsolutePath.str();
397 }
398 return true;
399}
400
Eric Christophere9403c12012-10-16 23:46:25 +0000401static bool getFileLineInfoForCompileUnit(DWARFCompileUnit *CU,
402 const DWARFLineTable *LineTable,
403 uint64_t Address,
404 bool NeedsAbsoluteFilePath,
405 std::string &FileName,
406 uint32_t &Line, uint32_t &Column) {
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000407 if (CU == 0 || LineTable == 0)
Alexey Samsonov38a63812012-08-30 07:49:50 +0000408 return false;
409 // Get the index of row we're looking for in the line table.
410 uint32_t RowIndex = LineTable->lookupAddress(Address);
411 if (RowIndex == -1U)
412 return false;
413 // Take file number and line/column from the row.
414 const DWARFDebugLine::Row &Row = LineTable->Rows[RowIndex];
415 if (!getFileNameForCompileUnit(CU, LineTable, Row.File,
416 NeedsAbsoluteFilePath, FileName))
417 return false;
418 Line = Row.Line;
419 Column = Row.Column;
420 return true;
421}
422
423DILineInfo DWARFContext::getLineInfoForAddress(uint64_t Address,
424 DILineInfoSpecifier Specifier) {
425 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
426 if (!CU)
Alexey Samsonov3e25c4a2012-07-02 05:54:45 +0000427 return DILineInfo();
Alexey Samsonov38a63812012-08-30 07:49:50 +0000428 std::string FileName = "<invalid>";
429 std::string FunctionName = "<invalid>";
430 uint32_t Line = 0;
431 uint32_t Column = 0;
432 if (Specifier.needs(DILineInfoSpecifier::FunctionName)) {
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000433 // The address may correspond to instruction in some inlined function,
434 // so we have to build the chain of inlined functions and take the
435 // name of the topmost function in it.
Alexey Samsonove6642902013-08-06 10:49:15 +0000436 const DWARFDebugInfoEntryInlinedChain &InlinedChain =
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000437 CU->getInlinedChainForAddress(Address);
Alexey Samsonove6642902013-08-06 10:49:15 +0000438 if (InlinedChain.DIEs.size() > 0) {
439 const DWARFDebugInfoEntryMinimal &TopFunctionDIE = InlinedChain.DIEs[0];
David Blaikiecd7c4982013-09-23 22:44:40 +0000440 if (const char *Name = TopFunctionDIE.getSubroutineName(InlinedChain.U))
Alexey Samsonov38a63812012-08-30 07:49:50 +0000441 FunctionName = Name;
Alexey Samsonov9d26b0b2012-07-17 15:28:35 +0000442 }
Alexey Samsonov3e25c4a2012-07-02 05:54:45 +0000443 }
Alexey Samsonov38a63812012-08-30 07:49:50 +0000444 if (Specifier.needs(DILineInfoSpecifier::FileLineInfo)) {
Eric Christophere9403c12012-10-16 23:46:25 +0000445 const DWARFLineTable *LineTable = getLineTableForCompileUnit(CU);
Alexey Samsonov38a63812012-08-30 07:49:50 +0000446 const bool NeedsAbsoluteFilePath =
447 Specifier.needs(DILineInfoSpecifier::AbsoluteFilePath);
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000448 getFileLineInfoForCompileUnit(CU, LineTable, Address,
449 NeedsAbsoluteFilePath,
Alexey Samsonov38a63812012-08-30 07:49:50 +0000450 FileName, Line, Column);
Alexey Samsonov3e25c4a2012-07-02 05:54:45 +0000451 }
Alexey Samsonov38a63812012-08-30 07:49:50 +0000452 return DILineInfo(StringRef(FileName), StringRef(FunctionName),
453 Line, Column);
Benjamin Kramer101b1c52011-09-15 20:43:22 +0000454}
David Blaikie2d24e2a2011-12-20 02:50:00 +0000455
Andrew Kaylore27a7872013-01-26 00:28:05 +0000456DILineInfoTable DWARFContext::getLineInfoForAddressRange(uint64_t Address,
457 uint64_t Size,
458 DILineInfoSpecifier Specifier) {
459 DILineInfoTable Lines;
460 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
461 if (!CU)
462 return Lines;
463
464 std::string FunctionName = "<invalid>";
465 if (Specifier.needs(DILineInfoSpecifier::FunctionName)) {
466 // The address may correspond to instruction in some inlined function,
467 // so we have to build the chain of inlined functions and take the
468 // name of the topmost function in it.
Alexey Samsonove6642902013-08-06 10:49:15 +0000469 const DWARFDebugInfoEntryInlinedChain &InlinedChain =
Andrew Kaylore27a7872013-01-26 00:28:05 +0000470 CU->getInlinedChainForAddress(Address);
Alexey Samsonove6642902013-08-06 10:49:15 +0000471 if (InlinedChain.DIEs.size() > 0) {
472 const DWARFDebugInfoEntryMinimal &TopFunctionDIE = InlinedChain.DIEs[0];
David Blaikiecd7c4982013-09-23 22:44:40 +0000473 if (const char *Name = TopFunctionDIE.getSubroutineName(InlinedChain.U))
Andrew Kaylore27a7872013-01-26 00:28:05 +0000474 FunctionName = Name;
475 }
476 }
477
Andrew Kaylore27a7872013-01-26 00:28:05 +0000478 // If the Specifier says we don't need FileLineInfo, just
479 // return the top-most function at the starting address.
480 if (!Specifier.needs(DILineInfoSpecifier::FileLineInfo)) {
David Blaikieeaad5cd2013-09-22 17:01:50 +0000481 Lines.push_back(
482 std::make_pair(Address, DILineInfo("<invalid>", FunctionName, 0, 0)));
Andrew Kaylore27a7872013-01-26 00:28:05 +0000483 return Lines;
484 }
485
486 const DWARFLineTable *LineTable = getLineTableForCompileUnit(CU);
487 const bool NeedsAbsoluteFilePath =
488 Specifier.needs(DILineInfoSpecifier::AbsoluteFilePath);
489
490 // Get the index of row we're looking for in the line table.
491 std::vector<uint32_t> RowVector;
492 if (!LineTable->lookupAddressRange(Address, Size, RowVector))
493 return Lines;
494
495 uint32_t NumRows = RowVector.size();
496 for (uint32_t i = 0; i < NumRows; ++i) {
497 uint32_t RowIndex = RowVector[i];
498 // Take file number and line/column from the row.
499 const DWARFDebugLine::Row &Row = LineTable->Rows[RowIndex];
500 std::string FileName = "<invalid>";
501 getFileNameForCompileUnit(CU, LineTable, Row.File,
502 NeedsAbsoluteFilePath, FileName);
David Blaikieeaad5cd2013-09-22 17:01:50 +0000503 Lines.push_back(std::make_pair(
504 Row.Address, DILineInfo(FileName, FunctionName, Row.Line, Row.Column)));
Andrew Kaylore27a7872013-01-26 00:28:05 +0000505 }
506
507 return Lines;
508}
509
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000510DIInliningInfo DWARFContext::getInliningInfoForAddress(uint64_t Address,
511 DILineInfoSpecifier Specifier) {
512 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
513 if (!CU)
514 return DIInliningInfo();
515
Alexey Samsonove6642902013-08-06 10:49:15 +0000516 const DWARFDebugInfoEntryInlinedChain &InlinedChain =
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000517 CU->getInlinedChainForAddress(Address);
Alexey Samsonove6642902013-08-06 10:49:15 +0000518 if (InlinedChain.DIEs.size() == 0)
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000519 return DIInliningInfo();
520
521 DIInliningInfo InliningInfo;
522 uint32_t CallFile = 0, CallLine = 0, CallColumn = 0;
Eric Christophere9403c12012-10-16 23:46:25 +0000523 const DWARFLineTable *LineTable = 0;
Alexey Samsonove6642902013-08-06 10:49:15 +0000524 for (uint32_t i = 0, n = InlinedChain.DIEs.size(); i != n; i++) {
525 const DWARFDebugInfoEntryMinimal &FunctionDIE = InlinedChain.DIEs[i];
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000526 std::string FileName = "<invalid>";
527 std::string FunctionName = "<invalid>";
528 uint32_t Line = 0;
529 uint32_t Column = 0;
530 // Get function name if necessary.
531 if (Specifier.needs(DILineInfoSpecifier::FunctionName)) {
David Blaikiecd7c4982013-09-23 22:44:40 +0000532 if (const char *Name = FunctionDIE.getSubroutineName(InlinedChain.U))
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000533 FunctionName = Name;
534 }
535 if (Specifier.needs(DILineInfoSpecifier::FileLineInfo)) {
536 const bool NeedsAbsoluteFilePath =
537 Specifier.needs(DILineInfoSpecifier::AbsoluteFilePath);
538 if (i == 0) {
539 // For the topmost frame, initialize the line table of this
540 // compile unit and fetch file/line info from it.
541 LineTable = getLineTableForCompileUnit(CU);
542 // For the topmost routine, get file/line info from line table.
543 getFileLineInfoForCompileUnit(CU, LineTable, Address,
544 NeedsAbsoluteFilePath,
545 FileName, Line, Column);
546 } else {
547 // Otherwise, use call file, call line and call column from
548 // previous DIE in inlined chain.
549 getFileNameForCompileUnit(CU, LineTable, CallFile,
550 NeedsAbsoluteFilePath, FileName);
551 Line = CallLine;
552 Column = CallColumn;
553 }
554 // Get call file/line/column of a current DIE.
555 if (i + 1 < n) {
David Blaikiecd7c4982013-09-23 22:44:40 +0000556 FunctionDIE.getCallerFrame(InlinedChain.U, CallFile, CallLine,
Alexey Samsonove6642902013-08-06 10:49:15 +0000557 CallColumn);
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000558 }
559 }
560 DILineInfo Frame(StringRef(FileName), StringRef(FunctionName),
561 Line, Column);
562 InliningInfo.addFrame(Frame);
563 }
564 return InliningInfo;
565}
566
Alexey Samsonov005159e2013-04-23 10:17:34 +0000567static bool consumeCompressedDebugSectionHeader(StringRef &data,
568 uint64_t &OriginalSize) {
569 // Consume "ZLIB" prefix.
570 if (!data.startswith("ZLIB"))
571 return false;
572 data = data.substr(4);
573 // Consume uncompressed section size (big-endian 8 bytes).
574 DataExtractor extractor(data, false, 8);
575 uint32_t Offset = 0;
576 OriginalSize = extractor.getU64(&Offset);
577 if (Offset == 0)
578 return false;
579 data = data.substr(Offset);
580 return true;
581}
582
Eric Christopherd1726a42012-11-12 21:40:38 +0000583DWARFContextInMemory::DWARFContextInMemory(object::ObjectFile *Obj) :
Eli Bendersky60bdc5b2013-02-05 23:30:58 +0000584 IsLittleEndian(Obj->isLittleEndian()),
585 AddressSize(Obj->getBytesInAddress()) {
Eric Christopherd1726a42012-11-12 21:40:38 +0000586 error_code ec;
587 for (object::section_iterator i = Obj->begin_sections(),
588 e = Obj->end_sections();
589 i != e; i.increment(ec)) {
590 StringRef name;
591 i->getName(name);
592 StringRef data;
593 i->getContents(data);
594
Eric Christopherd1726a42012-11-12 21:40:38 +0000595 name = name.substr(name.find_first_not_of("._")); // Skip . and _ prefixes.
Alexey Samsonov784baa62013-04-17 14:27:04 +0000596
Alexey Samsonov005159e2013-04-23 10:17:34 +0000597 // Check if debug info section is compressed with zlib.
598 if (name.startswith("zdebug_")) {
599 uint64_t OriginalSize;
600 if (!zlib::isAvailable() ||
601 !consumeCompressedDebugSectionHeader(data, OriginalSize))
602 continue;
603 OwningPtr<MemoryBuffer> UncompressedSection;
604 if (zlib::uncompress(data, UncompressedSection, OriginalSize) !=
605 zlib::StatusOK)
606 continue;
607 // Make data point to uncompressed section contents and save its contents.
608 name = name.substr(1);
609 data = UncompressedSection->getBuffer();
610 UncompressedSections.push_back(UncompressedSection.take());
611 }
612
Alexey Samsonov784baa62013-04-17 14:27:04 +0000613 StringRef *Section = StringSwitch<StringRef*>(name)
David Blaikie9528b0e2013-09-23 17:42:01 +0000614 .Case("debug_info", &InfoSection.Data)
Alexey Samsonov784baa62013-04-17 14:27:04 +0000615 .Case("debug_abbrev", &AbbrevSection)
David Blaikie9528b0e2013-09-23 17:42:01 +0000616 .Case("debug_loc", &LocSection.Data)
617 .Case("debug_line", &LineSection.Data)
Alexey Samsonov784baa62013-04-17 14:27:04 +0000618 .Case("debug_aranges", &ARangeSection)
619 .Case("debug_frame", &DebugFrameSection)
620 .Case("debug_str", &StringSection)
621 .Case("debug_ranges", &RangeSection)
622 .Case("debug_pubnames", &PubNamesSection)
David Blaikie994c37f2013-09-19 23:01:29 +0000623 .Case("debug_gnu_pubnames", &GnuPubNamesSection)
David Blaikie9ddf28d2013-09-24 19:50:00 +0000624 .Case("debug_gnu_pubtypes", &GnuPubTypesSection)
David Blaikie9528b0e2013-09-23 17:42:01 +0000625 .Case("debug_info.dwo", &InfoDWOSection.Data)
Alexey Samsonov784baa62013-04-17 14:27:04 +0000626 .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() { }