blob: e8c47802a58efec8519e4aa802c901ce1cecde2e [file] [log] [blame]
Benjamin Krameraa2f78f2011-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"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000011#include "llvm/ADT/STLExtras.h"
Alexey Samsonove16e16a2012-07-19 07:03:58 +000012#include "llvm/ADT/SmallString.h"
Alexey Samsonov3d0e3ed2013-04-17 14:27:04 +000013#include "llvm/ADT/StringSwitch.h"
Alexey Samsonov068fc8a2013-04-23 10:17:34 +000014#include "llvm/Support/Compression.h"
Benjamin Kramer6dda0322011-09-15 18:02:20 +000015#include "llvm/Support/Dwarf.h"
Benjamin Kramer07d4b1c2011-09-15 16:57:13 +000016#include "llvm/Support/Format.h"
Alexey Samsonove16e16a2012-07-19 07:03:58 +000017#include "llvm/Support/Path.h"
Benjamin Kramera6002fd2011-09-14 01:09:52 +000018#include "llvm/Support/raw_ostream.h"
Benjamin Kramer2602ca62011-09-15 20:43:22 +000019#include <algorithm>
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000020using namespace llvm;
Benjamin Kramer6dda0322011-09-15 18:02:20 +000021using namespace dwarf;
Rafael Espindola4f60a382013-05-30 03:05:14 +000022using namespace object;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000023
Eric Christopher494109b2012-10-16 23:46:25 +000024typedef DWARFDebugLine::LineTable DWARFLineTable;
25
Alexey Samsonova9debbf2013-08-23 06:56:01 +000026DWARFContext::~DWARFContext() {
27 DeleteContainerPointers(CUs);
Benjamin Kramer41fe88e2013-09-29 11:24:02 +000028 DeleteContainerPointers(TUs);
Alexey Samsonova9debbf2013-08-23 06:56:01 +000029 DeleteContainerPointers(DWOCUs);
30}
31
David Blaikieecd21ff2013-09-24 19:50:00 +000032static void dumpPubSection(raw_ostream &OS, StringRef Name, StringRef Data,
Eric Christopher0de53592013-09-25 23:02:36 +000033 bool LittleEndian, bool GnuStyle) {
David Blaikieecd21ff2013-09-24 19:50:00 +000034 OS << "\n." << Name << " contents:\n";
35 DataExtractor pubNames(Data, LittleEndian, 0);
36 uint32_t offset = 0;
David Blaikie8a263cb2013-11-26 00:22:37 +000037 while (pubNames.isValidOffset(offset)) {
38 OS << "length = " << format("0x%08x", pubNames.getU32(&offset));
39 OS << " version = " << format("0x%04x", pubNames.getU16(&offset));
40 OS << " unit_offset = " << format("0x%08x", pubNames.getU32(&offset));
41 OS << " unit_size = " << format("0x%08x", pubNames.getU32(&offset)) << '\n';
42 if (GnuStyle)
43 OS << "Offset Linkage Kind Name\n";
44 else
45 OS << "Offset Name\n";
Eric Christopher0de53592013-09-25 23:02:36 +000046
David Blaikie8a263cb2013-11-26 00:22:37 +000047 while (offset < Data.size()) {
48 uint32_t dieRef = pubNames.getU32(&offset);
49 if (dieRef == 0)
50 break;
51 OS << format("0x%8.8x ", dieRef);
52 if (GnuStyle) {
53 PubIndexEntryDescriptor desc(pubNames.getU8(&offset));
54 OS << format("%-8s", dwarf::GDBIndexEntryLinkageString(desc.Linkage))
55 << ' ' << format("%-8s", dwarf::GDBIndexEntryKindString(desc.Kind))
56 << ' ';
57 }
58 OS << '\"' << pubNames.getCStr(&offset) << "\"\n";
Eric Christopher0de53592013-09-25 23:02:36 +000059 }
David Blaikieecd21ff2013-09-24 19:50:00 +000060 }
61}
62
Eli Bendersky7a94daa2013-01-25 20:26:43 +000063void DWARFContext::dump(raw_ostream &OS, DIDumpType DumpType) {
64 if (DumpType == DIDT_All || DumpType == DIDT_Abbrev) {
65 OS << ".debug_abbrev contents:\n";
66 getDebugAbbrev()->dump(OS);
67 }
Benjamin Kramera6002fd2011-09-14 01:09:52 +000068
David Blaikie66865d62014-01-09 00:13:35 +000069 if (DumpType == DIDT_All || DumpType == DIDT_AbbrevDwo)
70 if (const DWARFDebugAbbrev *D = getDebugAbbrevDWO()) {
David Blaikie622dce42014-01-08 23:29:59 +000071 OS << "\n.debug_abbrev.dwo contents:\n";
David Blaikie66865d62014-01-09 00:13:35 +000072 D->dump(OS);
David Blaikie622dce42014-01-08 23:29:59 +000073 }
David Blaikie622dce42014-01-08 23:29:59 +000074
Eli Bendersky7a94daa2013-01-25 20:26:43 +000075 if (DumpType == DIDT_All || DumpType == DIDT_Info) {
76 OS << "\n.debug_info contents:\n";
77 for (unsigned i = 0, e = getNumCompileUnits(); i != e; ++i)
78 getCompileUnitAtIndex(i)->dump(OS);
79 }
Benjamin Kramera6002fd2011-09-14 01:09:52 +000080
David Blaikie66865d62014-01-09 00:13:35 +000081 if ((DumpType == DIDT_All || DumpType == DIDT_InfoDwo) &&
82 getNumDWOCompileUnits()) {
83 OS << "\n.debug_info.dwo contents:\n";
84 for (unsigned i = 0, e = getNumDWOCompileUnits(); i != e; ++i)
85 getDWOCompileUnitAtIndex(i)->dump(OS);
86 }
David Blaikie622dce42014-01-08 23:29:59 +000087
David Blaikie66865d62014-01-09 00:13:35 +000088 if ((DumpType == DIDT_All || DumpType == DIDT_Types) && getNumTypeUnits()) {
David Blaikie03c089c2013-09-23 22:44:47 +000089 OS << "\n.debug_types contents:\n";
90 for (unsigned i = 0, e = getNumTypeUnits(); i != e; ++i)
91 getTypeUnitAtIndex(i)->dump(OS);
92 }
93
David Blaikie18e73502013-06-19 21:37:13 +000094 if (DumpType == DIDT_All || DumpType == DIDT_Loc) {
David Blaikie03c089c2013-09-23 22:44:47 +000095 OS << "\n.debug_loc contents:\n";
David Blaikie18e73502013-06-19 21:37:13 +000096 getDebugLoc()->dump(OS);
97 }
98
Eli Benderskyfd08bc12013-02-05 23:30:58 +000099 if (DumpType == DIDT_All || DumpType == DIDT_Frames) {
100 OS << "\n.debug_frame contents:\n";
101 getDebugFrame()->dump(OS);
102 }
103
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000104 uint32_t offset = 0;
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000105 if (DumpType == DIDT_All || DumpType == DIDT_Aranges) {
106 OS << "\n.debug_aranges contents:\n";
107 DataExtractor arangesData(getARangeSection(), isLittleEndian(), 0);
108 DWARFDebugArangeSet set;
109 while (set.extract(arangesData, &offset))
110 set.dump(OS);
111 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000112
Alexey Samsonov034e57a2012-08-27 07:17:47 +0000113 uint8_t savedAddressByteSize = 0;
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000114 if (DumpType == DIDT_All || DumpType == DIDT_Line) {
115 OS << "\n.debug_line contents:\n";
116 for (unsigned i = 0, e = getNumCompileUnits(); i != e; ++i) {
117 DWARFCompileUnit *cu = getCompileUnitAtIndex(i);
118 savedAddressByteSize = cu->getAddressByteSize();
119 unsigned stmtOffset =
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000120 cu->getCompileUnitDIE()->getAttributeValueAsSectionOffset(
121 cu, DW_AT_stmt_list, -1U);
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000122 if (stmtOffset != -1U) {
David Blaikie1b5ee5d2013-09-23 17:42:01 +0000123 DataExtractor lineData(getLineSection().Data, isLittleEndian(),
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000124 savedAddressByteSize);
125 DWARFDebugLine::DumpingState state(OS);
David Blaikie1b5ee5d2013-09-23 17:42:01 +0000126 DWARFDebugLine::parseStatementTable(lineData, &getLineSection().Relocs, &stmtOffset, state);
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000127 }
Benjamin Kramer6dda0322011-09-15 18:02:20 +0000128 }
129 }
Benjamin Kramer07d4b1c2011-09-15 16:57:13 +0000130
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000131 if (DumpType == DIDT_All || DumpType == DIDT_Str) {
132 OS << "\n.debug_str contents:\n";
133 DataExtractor strData(getStringSection(), isLittleEndian(), 0);
134 offset = 0;
135 uint32_t strOffset = 0;
136 while (const char *s = strData.getCStr(&offset)) {
137 OS << format("0x%8.8x: \"%s\"\n", strOffset, s);
138 strOffset = offset;
139 }
Benjamin Kramer07d4b1c2011-09-15 16:57:13 +0000140 }
Alexey Samsonov034e57a2012-08-27 07:17:47 +0000141
David Blaikie66865d62014-01-09 00:13:35 +0000142 if ((DumpType == DIDT_All || DumpType == DIDT_StrDwo) &&
143 !getStringDWOSection().empty()) {
144 OS << "\n.debug_str.dwo contents:\n";
145 DataExtractor strDWOData(getStringDWOSection(), isLittleEndian(), 0);
146 offset = 0;
147 uint32_t strDWOOffset = 0;
148 while (const char *s = strDWOData.getCStr(&offset)) {
149 OS << format("0x%8.8x: \"%s\"\n", strDWOOffset, s);
150 strDWOOffset = offset;
David Blaikie622dce42014-01-08 23:29:59 +0000151 }
David Blaikie66865d62014-01-09 00:13:35 +0000152 }
David Blaikie622dce42014-01-08 23:29:59 +0000153
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000154 if (DumpType == DIDT_All || DumpType == DIDT_Ranges) {
155 OS << "\n.debug_ranges contents:\n";
156 // In fact, different compile units may have different address byte
157 // sizes, but for simplicity we just use the address byte size of the last
158 // compile unit (there is no easy and fast way to associate address range
159 // list and the compile unit it describes).
160 DataExtractor rangesData(getRangeSection(), isLittleEndian(),
161 savedAddressByteSize);
162 offset = 0;
163 DWARFDebugRangeList rangeList;
164 while (rangeList.extract(rangesData, &offset))
165 rangeList.dump(OS);
Eric Christopherda4b2192013-01-02 23:52:13 +0000166 }
Eric Christopher962c9082013-01-15 23:56:56 +0000167
Eric Christopher0de53592013-09-25 23:02:36 +0000168 if (DumpType == DIDT_All || DumpType == DIDT_Pubnames)
169 dumpPubSection(OS, "debug_pubnames", getPubNamesSection(),
170 isLittleEndian(), false);
Krzysztof Parzyszek97438dc2013-02-12 16:20:28 +0000171
Eric Christopher4c7e6ba2013-09-25 23:02:41 +0000172 if (DumpType == DIDT_All || DumpType == DIDT_Pubtypes)
173 dumpPubSection(OS, "debug_pubtypes", getPubTypesSection(),
174 isLittleEndian(), false);
175
David Blaikieecd21ff2013-09-24 19:50:00 +0000176 if (DumpType == DIDT_All || DumpType == DIDT_GnuPubnames)
177 dumpPubSection(OS, "debug_gnu_pubnames", getGnuPubNamesSection(),
Eric Christopher0de53592013-09-25 23:02:36 +0000178 isLittleEndian(), true /* GnuStyle */);
David Blaikieecd21ff2013-09-24 19:50:00 +0000179
180 if (DumpType == DIDT_All || DumpType == DIDT_GnuPubtypes)
181 dumpPubSection(OS, "debug_gnu_pubtypes", getGnuPubTypesSection(),
Eric Christopher0de53592013-09-25 23:02:36 +0000182 isLittleEndian(), true /* GnuStyle */);
David Blaikie404d3042013-09-19 23:01:29 +0000183
David Blaikie66865d62014-01-09 00:13:35 +0000184 if ((DumpType == DIDT_All || DumpType == DIDT_StrOffsetsDwo) &&
185 !getStringOffsetDWOSection().empty()) {
186 OS << "\n.debug_str_offsets.dwo contents:\n";
187 DataExtractor strOffsetExt(getStringOffsetDWOSection(), isLittleEndian(),
188 0);
189 offset = 0;
190 uint64_t size = getStringOffsetDWOSection().size();
191 while (offset < size) {
192 OS << format("0x%8.8x: ", offset);
193 OS << format("%8.8x\n", strOffsetExt.getU32(&offset));
Eric Christopher92f3c0b2013-05-06 17:50:42 +0000194 }
David Blaikie66865d62014-01-09 00:13:35 +0000195 }
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000196}
197
198const DWARFDebugAbbrev *DWARFContext::getDebugAbbrev() {
199 if (Abbrev)
200 return Abbrev.get();
201
202 DataExtractor abbrData(getAbbrevSection(), isLittleEndian(), 0);
203
204 Abbrev.reset(new DWARFDebugAbbrev());
205 Abbrev->parse(abbrData);
206 return Abbrev.get();
207}
208
Eric Christopherda4b2192013-01-02 23:52:13 +0000209const DWARFDebugAbbrev *DWARFContext::getDebugAbbrevDWO() {
210 if (AbbrevDWO)
211 return AbbrevDWO.get();
212
213 DataExtractor abbrData(getAbbrevDWOSection(), isLittleEndian(), 0);
214 AbbrevDWO.reset(new DWARFDebugAbbrev());
215 AbbrevDWO->parse(abbrData);
216 return AbbrevDWO.get();
217}
218
David Blaikie18e73502013-06-19 21:37:13 +0000219const DWARFDebugLoc *DWARFContext::getDebugLoc() {
220 if (Loc)
221 return Loc.get();
222
David Blaikie1b5ee5d2013-09-23 17:42:01 +0000223 DataExtractor LocData(getLocSection().Data, isLittleEndian(), 0);
224 Loc.reset(new DWARFDebugLoc(getLocSection().Relocs));
David Blaikie18e73502013-06-19 21:37:13 +0000225 // assume all compile units have the same address byte size
226 if (getNumCompileUnits())
227 Loc->parse(LocData, getCompileUnitAtIndex(0)->getAddressByteSize());
228 return Loc.get();
229}
230
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000231const DWARFDebugAranges *DWARFContext::getDebugAranges() {
232 if (Aranges)
233 return Aranges.get();
234
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000235 Aranges.reset(new DWARFDebugAranges());
Alexey Samsonova1694c12012-11-16 08:36:25 +0000236 Aranges->generate(this);
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000237 return Aranges.get();
238}
239
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000240const DWARFDebugFrame *DWARFContext::getDebugFrame() {
241 if (DebugFrame)
242 return DebugFrame.get();
243
244 // There's a "bug" in the DWARFv3 standard with respect to the target address
245 // size within debug frame sections. While DWARF is supposed to be independent
246 // of its container, FDEs have fields with size being "target address size",
247 // which isn't specified in DWARF in general. It's only specified for CUs, but
248 // .eh_frame can appear without a .debug_info section. Follow the example of
249 // other tools (libdwarf) and extract this from the container (ObjectFile
250 // provides this information). This problem is fixed in DWARFv4
251 // See this dwarf-discuss discussion for more details:
252 // http://lists.dwarfstd.org/htdig.cgi/dwarf-discuss-dwarfstd.org/2011-December/001173.html
253 DataExtractor debugFrameData(getDebugFrameSection(), isLittleEndian(),
254 getAddressSize());
255 DebugFrame.reset(new DWARFDebugFrame());
256 DebugFrame->parse(debugFrameData);
257 return DebugFrame.get();
258}
259
Eric Christopher494109b2012-10-16 23:46:25 +0000260const DWARFLineTable *
Benjamin Kramer679e1752011-09-15 20:43:18 +0000261DWARFContext::getLineTableForCompileUnit(DWARFCompileUnit *cu) {
262 if (!Line)
David Blaikie1b5ee5d2013-09-23 17:42:01 +0000263 Line.reset(new DWARFDebugLine(&getLineSection().Relocs));
Benjamin Kramer5acab502011-09-15 02:12:05 +0000264
Benjamin Kramer679e1752011-09-15 20:43:18 +0000265 unsigned stmtOffset =
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000266 cu->getCompileUnitDIE()->getAttributeValueAsSectionOffset(
267 cu, DW_AT_stmt_list, -1U);
Benjamin Kramer679e1752011-09-15 20:43:18 +0000268 if (stmtOffset == -1U)
269 return 0; // No line table for this compile unit.
Benjamin Kramer5acab502011-09-15 02:12:05 +0000270
Benjamin Kramer679e1752011-09-15 20:43:18 +0000271 // See if the line table is cached.
Eric Christopher494109b2012-10-16 23:46:25 +0000272 if (const DWARFLineTable *lt = Line->getLineTable(stmtOffset))
Benjamin Kramer679e1752011-09-15 20:43:18 +0000273 return lt;
274
275 // We have to parse it first.
David Blaikie1b5ee5d2013-09-23 17:42:01 +0000276 DataExtractor lineData(getLineSection().Data, isLittleEndian(),
Benjamin Kramer679e1752011-09-15 20:43:18 +0000277 cu->getAddressByteSize());
278 return Line->getOrParseLineTable(lineData, stmtOffset);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000279}
280
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000281void DWARFContext::parseCompileUnits() {
282 uint32_t offset = 0;
David Blaikie1b5ee5d2013-09-23 17:42:01 +0000283 const DataExtractor &DIData = DataExtractor(getInfoSection().Data,
Eric Christopher02509482012-10-16 23:46:23 +0000284 isLittleEndian(), 0);
285 while (DIData.isValidOffset(offset)) {
Alexey Samsonova9debbf2013-08-23 06:56:01 +0000286 OwningPtr<DWARFCompileUnit> CU(new DWARFCompileUnit(
David Blaikie1b5ee5d2013-09-23 17:42:01 +0000287 getDebugAbbrev(), getInfoSection().Data, getAbbrevSection(),
Alexey Samsonova9debbf2013-08-23 06:56:01 +0000288 getRangeSection(), getStringSection(), StringRef(), getAddrSection(),
David Blaikie1b5ee5d2013-09-23 17:42:01 +0000289 &getInfoSection().Relocs, isLittleEndian()));
Alexey Samsonova9debbf2013-08-23 06:56:01 +0000290 if (!CU->extract(DIData, &offset)) {
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000291 break;
292 }
Alexey Samsonova9debbf2013-08-23 06:56:01 +0000293 CUs.push_back(CU.take());
David Blaikie07e22442013-09-23 22:44:40 +0000294 offset = CUs.back()->getNextUnitOffset();
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000295 }
296}
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000297
David Blaikie03c089c2013-09-23 22:44:47 +0000298void DWARFContext::parseTypeUnits() {
David Blaikiebc563272013-12-13 21:33:40 +0000299 const TypeSectionMap &Sections = getTypesSections();
300 for (TypeSectionMap::const_iterator I = Sections.begin(), E = Sections.end();
David Blaikie03c089c2013-09-23 22:44:47 +0000301 I != E; ++I) {
302 uint32_t offset = 0;
303 const DataExtractor &DIData =
304 DataExtractor(I->second.Data, isLittleEndian(), 0);
305 while (DIData.isValidOffset(offset)) {
306 OwningPtr<DWARFTypeUnit> TU(new DWARFTypeUnit(
307 getDebugAbbrev(), I->second.Data, getAbbrevSection(),
308 getRangeSection(), getStringSection(), StringRef(), getAddrSection(),
309 &I->second.Relocs, isLittleEndian()));
310 if (!TU->extract(DIData, &offset))
311 break;
312 TUs.push_back(TU.take());
313 offset = TUs.back()->getNextUnitOffset();
314 }
315 }
316}
317
Eric Christopherda4b2192013-01-02 23:52:13 +0000318void DWARFContext::parseDWOCompileUnits() {
319 uint32_t offset = 0;
David Blaikie1b5ee5d2013-09-23 17:42:01 +0000320 const DataExtractor &DIData =
321 DataExtractor(getInfoDWOSection().Data, isLittleEndian(), 0);
Eric Christopherda4b2192013-01-02 23:52:13 +0000322 while (DIData.isValidOffset(offset)) {
Alexey Samsonova9debbf2013-08-23 06:56:01 +0000323 OwningPtr<DWARFCompileUnit> DWOCU(new DWARFCompileUnit(
David Blaikie1b5ee5d2013-09-23 17:42:01 +0000324 getDebugAbbrevDWO(), getInfoDWOSection().Data, getAbbrevDWOSection(),
Alexey Samsonova9debbf2013-08-23 06:56:01 +0000325 getRangeDWOSection(), getStringDWOSection(),
David Blaikie1b5ee5d2013-09-23 17:42:01 +0000326 getStringOffsetDWOSection(), getAddrSection(),
327 &getInfoDWOSection().Relocs, isLittleEndian()));
Alexey Samsonova9debbf2013-08-23 06:56:01 +0000328 if (!DWOCU->extract(DIData, &offset)) {
Eric Christopherda4b2192013-01-02 23:52:13 +0000329 break;
330 }
Alexey Samsonova9debbf2013-08-23 06:56:01 +0000331 DWOCUs.push_back(DWOCU.take());
David Blaikie07e22442013-09-23 22:44:40 +0000332 offset = DWOCUs.back()->getNextUnitOffset();
Eric Christopherda4b2192013-01-02 23:52:13 +0000333 }
334}
335
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000336namespace {
337 struct OffsetComparator {
Alexey Samsonova9debbf2013-08-23 06:56:01 +0000338 bool operator()(const DWARFCompileUnit *LHS,
339 const DWARFCompileUnit *RHS) const {
340 return LHS->getOffset() < RHS->getOffset();
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000341 }
Alexey Samsonova9debbf2013-08-23 06:56:01 +0000342 bool operator()(const DWARFCompileUnit *LHS, uint32_t RHS) const {
343 return LHS->getOffset() < RHS;
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000344 }
Alexey Samsonova9debbf2013-08-23 06:56:01 +0000345 bool operator()(uint32_t LHS, const DWARFCompileUnit *RHS) const {
346 return LHS < RHS->getOffset();
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000347 }
348 };
349}
350
Alexey Samsonov45be7932012-08-30 07:49:50 +0000351DWARFCompileUnit *DWARFContext::getCompileUnitForOffset(uint32_t Offset) {
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000352 if (CUs.empty())
353 parseCompileUnits();
354
Alexey Samsonova9debbf2013-08-23 06:56:01 +0000355 DWARFCompileUnit **CU =
356 std::lower_bound(CUs.begin(), CUs.end(), Offset, OffsetComparator());
357 if (CU != CUs.end()) {
358 return *CU;
359 }
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000360 return 0;
361}
362
Alexey Samsonov45be7932012-08-30 07:49:50 +0000363DWARFCompileUnit *DWARFContext::getCompileUnitForAddress(uint64_t Address) {
Benjamin Kramer112ec172011-09-15 21:59:13 +0000364 // First, get the offset of the compile unit.
Alexey Samsonov45be7932012-08-30 07:49:50 +0000365 uint32_t CUOffset = getDebugAranges()->findAddress(Address);
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000366 // Retrieve the compile unit.
Alexey Samsonov45be7932012-08-30 07:49:50 +0000367 return getCompileUnitForOffset(CUOffset);
368}
369
Eric Christopher494109b2012-10-16 23:46:25 +0000370static bool getFileNameForCompileUnit(DWARFCompileUnit *CU,
371 const DWARFLineTable *LineTable,
372 uint64_t FileIndex,
373 bool NeedsAbsoluteFilePath,
374 std::string &FileName) {
Alexey Samsonov45be7932012-08-30 07:49:50 +0000375 if (CU == 0 ||
376 LineTable == 0 ||
377 !LineTable->getFileNameByIndex(FileIndex, NeedsAbsoluteFilePath,
378 FileName))
379 return false;
380 if (NeedsAbsoluteFilePath && sys::path::is_relative(FileName)) {
381 // We may still need to append compilation directory of compile unit.
382 SmallString<16> AbsolutePath;
383 if (const char *CompilationDir = CU->getCompilationDir()) {
384 sys::path::append(AbsolutePath, CompilationDir);
385 }
386 sys::path::append(AbsolutePath, FileName);
387 FileName = AbsolutePath.str();
388 }
389 return true;
390}
391
Eric Christopher494109b2012-10-16 23:46:25 +0000392static bool getFileLineInfoForCompileUnit(DWARFCompileUnit *CU,
393 const DWARFLineTable *LineTable,
394 uint64_t Address,
395 bool NeedsAbsoluteFilePath,
396 std::string &FileName,
397 uint32_t &Line, uint32_t &Column) {
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000398 if (CU == 0 || LineTable == 0)
Alexey Samsonov45be7932012-08-30 07:49:50 +0000399 return false;
400 // Get the index of row we're looking for in the line table.
401 uint32_t RowIndex = LineTable->lookupAddress(Address);
402 if (RowIndex == -1U)
403 return false;
404 // Take file number and line/column from the row.
405 const DWARFDebugLine::Row &Row = LineTable->Rows[RowIndex];
406 if (!getFileNameForCompileUnit(CU, LineTable, Row.File,
407 NeedsAbsoluteFilePath, FileName))
408 return false;
409 Line = Row.Line;
410 Column = Row.Column;
411 return true;
412}
413
414DILineInfo DWARFContext::getLineInfoForAddress(uint64_t Address,
415 DILineInfoSpecifier Specifier) {
416 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
417 if (!CU)
Alexey Samsonovf4462fa2012-07-02 05:54:45 +0000418 return DILineInfo();
Alexey Samsonov45be7932012-08-30 07:49:50 +0000419 std::string FileName = "<invalid>";
420 std::string FunctionName = "<invalid>";
421 uint32_t Line = 0;
422 uint32_t Column = 0;
423 if (Specifier.needs(DILineInfoSpecifier::FunctionName)) {
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000424 // The address may correspond to instruction in some inlined function,
425 // so we have to build the chain of inlined functions and take the
426 // name of the topmost function in it.
Alexey Samsonov3211e612013-08-06 10:49:15 +0000427 const DWARFDebugInfoEntryInlinedChain &InlinedChain =
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000428 CU->getInlinedChainForAddress(Address);
Alexey Samsonov3211e612013-08-06 10:49:15 +0000429 if (InlinedChain.DIEs.size() > 0) {
430 const DWARFDebugInfoEntryMinimal &TopFunctionDIE = InlinedChain.DIEs[0];
David Blaikie07e22442013-09-23 22:44:40 +0000431 if (const char *Name = TopFunctionDIE.getSubroutineName(InlinedChain.U))
Alexey Samsonov45be7932012-08-30 07:49:50 +0000432 FunctionName = Name;
Alexey Samsonovb604ff22012-07-17 15:28:35 +0000433 }
Alexey Samsonovf4462fa2012-07-02 05:54:45 +0000434 }
Alexey Samsonov45be7932012-08-30 07:49:50 +0000435 if (Specifier.needs(DILineInfoSpecifier::FileLineInfo)) {
Eric Christopher494109b2012-10-16 23:46:25 +0000436 const DWARFLineTable *LineTable = getLineTableForCompileUnit(CU);
Alexey Samsonov45be7932012-08-30 07:49:50 +0000437 const bool NeedsAbsoluteFilePath =
438 Specifier.needs(DILineInfoSpecifier::AbsoluteFilePath);
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000439 getFileLineInfoForCompileUnit(CU, LineTable, Address,
440 NeedsAbsoluteFilePath,
Alexey Samsonov45be7932012-08-30 07:49:50 +0000441 FileName, Line, Column);
Alexey Samsonovf4462fa2012-07-02 05:54:45 +0000442 }
Alexey Samsonov45be7932012-08-30 07:49:50 +0000443 return DILineInfo(StringRef(FileName), StringRef(FunctionName),
444 Line, Column);
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000445}
David Blaikiea379b1812011-12-20 02:50:00 +0000446
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000447DILineInfoTable DWARFContext::getLineInfoForAddressRange(uint64_t Address,
448 uint64_t Size,
449 DILineInfoSpecifier Specifier) {
450 DILineInfoTable Lines;
451 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
452 if (!CU)
453 return Lines;
454
455 std::string FunctionName = "<invalid>";
456 if (Specifier.needs(DILineInfoSpecifier::FunctionName)) {
457 // The address may correspond to instruction in some inlined function,
458 // so we have to build the chain of inlined functions and take the
459 // name of the topmost function in it.
Alexey Samsonov3211e612013-08-06 10:49:15 +0000460 const DWARFDebugInfoEntryInlinedChain &InlinedChain =
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000461 CU->getInlinedChainForAddress(Address);
Alexey Samsonov3211e612013-08-06 10:49:15 +0000462 if (InlinedChain.DIEs.size() > 0) {
463 const DWARFDebugInfoEntryMinimal &TopFunctionDIE = InlinedChain.DIEs[0];
David Blaikie07e22442013-09-23 22:44:40 +0000464 if (const char *Name = TopFunctionDIE.getSubroutineName(InlinedChain.U))
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000465 FunctionName = Name;
466 }
467 }
468
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000469 // If the Specifier says we don't need FileLineInfo, just
470 // return the top-most function at the starting address.
471 if (!Specifier.needs(DILineInfoSpecifier::FileLineInfo)) {
David Blaikieba860d72013-09-22 17:01:50 +0000472 Lines.push_back(
473 std::make_pair(Address, DILineInfo("<invalid>", FunctionName, 0, 0)));
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000474 return Lines;
475 }
476
477 const DWARFLineTable *LineTable = getLineTableForCompileUnit(CU);
478 const bool NeedsAbsoluteFilePath =
479 Specifier.needs(DILineInfoSpecifier::AbsoluteFilePath);
480
481 // Get the index of row we're looking for in the line table.
482 std::vector<uint32_t> RowVector;
483 if (!LineTable->lookupAddressRange(Address, Size, RowVector))
484 return Lines;
485
486 uint32_t NumRows = RowVector.size();
487 for (uint32_t i = 0; i < NumRows; ++i) {
488 uint32_t RowIndex = RowVector[i];
489 // Take file number and line/column from the row.
490 const DWARFDebugLine::Row &Row = LineTable->Rows[RowIndex];
491 std::string FileName = "<invalid>";
492 getFileNameForCompileUnit(CU, LineTable, Row.File,
493 NeedsAbsoluteFilePath, FileName);
David Blaikieba860d72013-09-22 17:01:50 +0000494 Lines.push_back(std::make_pair(
495 Row.Address, DILineInfo(FileName, FunctionName, Row.Line, Row.Column)));
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000496 }
497
498 return Lines;
499}
500
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000501DIInliningInfo DWARFContext::getInliningInfoForAddress(uint64_t Address,
502 DILineInfoSpecifier Specifier) {
503 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
504 if (!CU)
505 return DIInliningInfo();
506
Alexey Samsonov3211e612013-08-06 10:49:15 +0000507 const DWARFDebugInfoEntryInlinedChain &InlinedChain =
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000508 CU->getInlinedChainForAddress(Address);
Alexey Samsonov3211e612013-08-06 10:49:15 +0000509 if (InlinedChain.DIEs.size() == 0)
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000510 return DIInliningInfo();
511
512 DIInliningInfo InliningInfo;
513 uint32_t CallFile = 0, CallLine = 0, CallColumn = 0;
Eric Christopher494109b2012-10-16 23:46:25 +0000514 const DWARFLineTable *LineTable = 0;
Alexey Samsonov3211e612013-08-06 10:49:15 +0000515 for (uint32_t i = 0, n = InlinedChain.DIEs.size(); i != n; i++) {
516 const DWARFDebugInfoEntryMinimal &FunctionDIE = InlinedChain.DIEs[i];
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000517 std::string FileName = "<invalid>";
518 std::string FunctionName = "<invalid>";
519 uint32_t Line = 0;
520 uint32_t Column = 0;
521 // Get function name if necessary.
522 if (Specifier.needs(DILineInfoSpecifier::FunctionName)) {
David Blaikie07e22442013-09-23 22:44:40 +0000523 if (const char *Name = FunctionDIE.getSubroutineName(InlinedChain.U))
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000524 FunctionName = Name;
525 }
526 if (Specifier.needs(DILineInfoSpecifier::FileLineInfo)) {
527 const bool NeedsAbsoluteFilePath =
528 Specifier.needs(DILineInfoSpecifier::AbsoluteFilePath);
529 if (i == 0) {
530 // For the topmost frame, initialize the line table of this
531 // compile unit and fetch file/line info from it.
532 LineTable = getLineTableForCompileUnit(CU);
533 // For the topmost routine, get file/line info from line table.
534 getFileLineInfoForCompileUnit(CU, LineTable, Address,
535 NeedsAbsoluteFilePath,
536 FileName, Line, Column);
537 } else {
538 // Otherwise, use call file, call line and call column from
539 // previous DIE in inlined chain.
540 getFileNameForCompileUnit(CU, LineTable, CallFile,
541 NeedsAbsoluteFilePath, FileName);
542 Line = CallLine;
543 Column = CallColumn;
544 }
545 // Get call file/line/column of a current DIE.
546 if (i + 1 < n) {
David Blaikie07e22442013-09-23 22:44:40 +0000547 FunctionDIE.getCallerFrame(InlinedChain.U, CallFile, CallLine,
Alexey Samsonov3211e612013-08-06 10:49:15 +0000548 CallColumn);
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000549 }
550 }
551 DILineInfo Frame(StringRef(FileName), StringRef(FunctionName),
552 Line, Column);
553 InliningInfo.addFrame(Frame);
554 }
555 return InliningInfo;
556}
557
Alexey Samsonov068fc8a2013-04-23 10:17:34 +0000558static bool consumeCompressedDebugSectionHeader(StringRef &data,
559 uint64_t &OriginalSize) {
560 // Consume "ZLIB" prefix.
561 if (!data.startswith("ZLIB"))
562 return false;
563 data = data.substr(4);
564 // Consume uncompressed section size (big-endian 8 bytes).
565 DataExtractor extractor(data, false, 8);
566 uint32_t Offset = 0;
567 OriginalSize = extractor.getU64(&Offset);
568 if (Offset == 0)
569 return false;
570 data = data.substr(Offset);
571 return true;
572}
573
Eric Christopher7370b552012-11-12 21:40:38 +0000574DWARFContextInMemory::DWARFContextInMemory(object::ObjectFile *Obj) :
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000575 IsLittleEndian(Obj->isLittleEndian()),
576 AddressSize(Obj->getBytesInAddress()) {
Eric Christopher7370b552012-11-12 21:40:38 +0000577 error_code ec;
578 for (object::section_iterator i = Obj->begin_sections(),
579 e = Obj->end_sections();
580 i != e; i.increment(ec)) {
581 StringRef name;
582 i->getName(name);
583 StringRef data;
584 i->getContents(data);
585
Eric Christopher7370b552012-11-12 21:40:38 +0000586 name = name.substr(name.find_first_not_of("._")); // Skip . and _ prefixes.
Alexey Samsonov3d0e3ed2013-04-17 14:27:04 +0000587
Alexey Samsonov068fc8a2013-04-23 10:17:34 +0000588 // Check if debug info section is compressed with zlib.
589 if (name.startswith("zdebug_")) {
590 uint64_t OriginalSize;
591 if (!zlib::isAvailable() ||
592 !consumeCompressedDebugSectionHeader(data, OriginalSize))
593 continue;
594 OwningPtr<MemoryBuffer> UncompressedSection;
595 if (zlib::uncompress(data, UncompressedSection, OriginalSize) !=
596 zlib::StatusOK)
597 continue;
598 // Make data point to uncompressed section contents and save its contents.
599 name = name.substr(1);
600 data = UncompressedSection->getBuffer();
601 UncompressedSections.push_back(UncompressedSection.take());
602 }
603
Eric Christopher4c7e6ba2013-09-25 23:02:41 +0000604 StringRef *Section =
605 StringSwitch<StringRef *>(name)
606 .Case("debug_info", &InfoSection.Data)
607 .Case("debug_abbrev", &AbbrevSection)
608 .Case("debug_loc", &LocSection.Data)
609 .Case("debug_line", &LineSection.Data)
610 .Case("debug_aranges", &ARangeSection)
611 .Case("debug_frame", &DebugFrameSection)
612 .Case("debug_str", &StringSection)
613 .Case("debug_ranges", &RangeSection)
614 .Case("debug_pubnames", &PubNamesSection)
615 .Case("debug_pubtypes", &PubTypesSection)
616 .Case("debug_gnu_pubnames", &GnuPubNamesSection)
617 .Case("debug_gnu_pubtypes", &GnuPubTypesSection)
618 .Case("debug_info.dwo", &InfoDWOSection.Data)
619 .Case("debug_abbrev.dwo", &AbbrevDWOSection)
620 .Case("debug_str.dwo", &StringDWOSection)
621 .Case("debug_str_offsets.dwo", &StringOffsetDWOSection)
622 .Case("debug_addr", &AddrSection)
623 // Any more debug info sections go here.
624 .Default(0);
Rafael Espindola4f60a382013-05-30 03:05:14 +0000625 if (Section) {
626 *Section = data;
627 if (name == "debug_ranges") {
628 // FIXME: Use the other dwo range section when we emit it.
629 RangeDWOSection = data;
630 }
David Blaikie03c089c2013-09-23 22:44:47 +0000631 } else if (name == "debug_types") {
David Blaikie427e4352013-09-23 23:39:55 +0000632 // Find debug_types data by section rather than name as there are
633 // multiple, comdat grouped, debug_types sections.
David Blaikie03c089c2013-09-23 22:44:47 +0000634 TypesSections[*i].Data = data;
Eric Christopherda4b2192013-01-02 23:52:13 +0000635 }
Eric Christopher7370b552012-11-12 21:40:38 +0000636
Rafael Espindola4f60a382013-05-30 03:05:14 +0000637 section_iterator RelocatedSection = i->getRelocatedSection();
638 if (RelocatedSection == Obj->end_sections())
639 continue;
640
641 StringRef RelSecName;
642 RelocatedSection->getName(RelSecName);
643 RelSecName = RelSecName.substr(
644 RelSecName.find_first_not_of("._")); // Skip . and _ prefixes.
645
Andrew Kaylord55d7012013-01-25 22:50:58 +0000646 // TODO: Add support for relocations in other sections as needed.
647 // Record relocations for the debug_info and debug_line sections.
Rafael Espindola4f60a382013-05-30 03:05:14 +0000648 RelocAddrMap *Map = StringSwitch<RelocAddrMap*>(RelSecName)
David Blaikie1b5ee5d2013-09-23 17:42:01 +0000649 .Case("debug_info", &InfoSection.Relocs)
650 .Case("debug_loc", &LocSection.Relocs)
651 .Case("debug_info.dwo", &InfoDWOSection.Relocs)
652 .Case("debug_line", &LineSection.Relocs)
Alexey Samsonov3d0e3ed2013-04-17 14:27:04 +0000653 .Default(0);
David Blaikie03c089c2013-09-23 22:44:47 +0000654 if (!Map) {
655 if (RelSecName != "debug_types")
656 continue;
David Blaikie427e4352013-09-23 23:39:55 +0000657 // Find debug_types relocs by section rather than name as there are
658 // multiple, comdat grouped, debug_types sections.
David Blaikie03c089c2013-09-23 22:44:47 +0000659 Map = &TypesSections[*RelocatedSection].Relocs;
660 }
Eric Christopher7370b552012-11-12 21:40:38 +0000661
662 if (i->begin_relocations() != i->end_relocations()) {
663 uint64_t SectionSize;
Rafael Espindola4f60a382013-05-30 03:05:14 +0000664 RelocatedSection->getSize(SectionSize);
Eric Christopher7370b552012-11-12 21:40:38 +0000665 for (object::relocation_iterator reloc_i = i->begin_relocations(),
666 reloc_e = i->end_relocations();
667 reloc_i != reloc_e; reloc_i.increment(ec)) {
668 uint64_t Address;
Rafael Espindola1e483872013-04-25 12:28:45 +0000669 reloc_i->getOffset(Address);
Eric Christopher7370b552012-11-12 21:40:38 +0000670 uint64_t Type;
671 reloc_i->getType(Type);
Andrew Kaylord55d7012013-01-25 22:50:58 +0000672 uint64_t SymAddr = 0;
673 // ELF relocations may need the symbol address
674 if (Obj->isELF()) {
Rafael Espindola806f0062013-06-05 01:33:53 +0000675 object::symbol_iterator Sym = reloc_i->getSymbol();
676 Sym->getAddress(SymAddr);
Andrew Kaylord55d7012013-01-25 22:50:58 +0000677 }
Eric Christopher7370b552012-11-12 21:40:38 +0000678
679 object::RelocVisitor V(Obj->getFileFormatName());
680 // The section address is always 0 for debug sections.
Andrew Kaylord55d7012013-01-25 22:50:58 +0000681 object::RelocToApply R(V.visit(Type, *reloc_i, 0, SymAddr));
Eric Christopher7370b552012-11-12 21:40:38 +0000682 if (V.error()) {
683 SmallString<32> Name;
684 error_code ec(reloc_i->getTypeName(Name));
685 if (ec) {
686 errs() << "Aaaaaa! Nameless relocation! Aaaaaa!\n";
687 }
688 errs() << "error: failed to compute relocation: "
689 << Name << "\n";
690 continue;
691 }
692
693 if (Address + R.Width > SectionSize) {
694 errs() << "error: " << R.Width << "-byte relocation starting "
695 << Address << " bytes into section " << name << " which is "
696 << SectionSize << " bytes long.\n";
697 continue;
698 }
699 if (R.Width > 8) {
700 errs() << "error: can't handle a relocation of more than 8 bytes at "
701 "a time.\n";
702 continue;
703 }
704 DEBUG(dbgs() << "Writing " << format("%p", R.Value)
705 << " at " << format("%p", Address)
706 << " with width " << format("%d", R.Width)
707 << "\n");
Eric Christopherda4b2192013-01-02 23:52:13 +0000708 Map->insert(std::make_pair(Address, std::make_pair(R.Width, R.Value)));
Eric Christopher7370b552012-11-12 21:40:38 +0000709 }
710 }
711 }
712}
713
Alexey Samsonov068fc8a2013-04-23 10:17:34 +0000714DWARFContextInMemory::~DWARFContextInMemory() {
715 DeleteContainerPointers(UncompressedSections);
716}
717
David Blaikiea379b1812011-12-20 02:50:00 +0000718void DWARFContextInMemory::anchor() { }