blob: 119e3602d0edad4c33aa595e9be4a92686b8467b [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);
NAKAMURA Takumi7409e842014-01-24 13:40:43 +000030 DeleteContainerPointers(DWOTUs);
Alexey Samsonova9debbf2013-08-23 06:56:01 +000031}
32
David Blaikieecd21ff2013-09-24 19:50:00 +000033static void dumpPubSection(raw_ostream &OS, StringRef Name, StringRef Data,
Eric Christopher0de53592013-09-25 23:02:36 +000034 bool LittleEndian, bool GnuStyle) {
David Blaikieecd21ff2013-09-24 19:50:00 +000035 OS << "\n." << Name << " contents:\n";
36 DataExtractor pubNames(Data, LittleEndian, 0);
37 uint32_t offset = 0;
David Blaikie8a263cb2013-11-26 00:22:37 +000038 while (pubNames.isValidOffset(offset)) {
39 OS << "length = " << format("0x%08x", pubNames.getU32(&offset));
40 OS << " version = " << format("0x%04x", pubNames.getU16(&offset));
41 OS << " unit_offset = " << format("0x%08x", pubNames.getU32(&offset));
42 OS << " unit_size = " << format("0x%08x", pubNames.getU32(&offset)) << '\n';
43 if (GnuStyle)
44 OS << "Offset Linkage Kind Name\n";
45 else
46 OS << "Offset Name\n";
Eric Christopher0de53592013-09-25 23:02:36 +000047
David Blaikie8a263cb2013-11-26 00:22:37 +000048 while (offset < Data.size()) {
49 uint32_t dieRef = pubNames.getU32(&offset);
50 if (dieRef == 0)
51 break;
52 OS << format("0x%8.8x ", dieRef);
53 if (GnuStyle) {
54 PubIndexEntryDescriptor desc(pubNames.getU8(&offset));
55 OS << format("%-8s", dwarf::GDBIndexEntryLinkageString(desc.Linkage))
56 << ' ' << format("%-8s", dwarf::GDBIndexEntryKindString(desc.Kind))
57 << ' ';
58 }
59 OS << '\"' << pubNames.getCStr(&offset) << "\"\n";
Eric Christopher0de53592013-09-25 23:02:36 +000060 }
David Blaikieecd21ff2013-09-24 19:50:00 +000061 }
62}
63
Eli Bendersky7a94daa2013-01-25 20:26:43 +000064void DWARFContext::dump(raw_ostream &OS, DIDumpType DumpType) {
65 if (DumpType == DIDT_All || DumpType == DIDT_Abbrev) {
66 OS << ".debug_abbrev contents:\n";
67 getDebugAbbrev()->dump(OS);
68 }
Benjamin Kramera6002fd2011-09-14 01:09:52 +000069
David Blaikie66865d62014-01-09 00:13:35 +000070 if (DumpType == DIDT_All || DumpType == DIDT_AbbrevDwo)
71 if (const DWARFDebugAbbrev *D = getDebugAbbrevDWO()) {
David Blaikie622dce42014-01-08 23:29:59 +000072 OS << "\n.debug_abbrev.dwo contents:\n";
David Blaikie66865d62014-01-09 00:13:35 +000073 D->dump(OS);
David Blaikie622dce42014-01-08 23:29:59 +000074 }
David Blaikie622dce42014-01-08 23:29:59 +000075
Eli Bendersky7a94daa2013-01-25 20:26:43 +000076 if (DumpType == DIDT_All || DumpType == DIDT_Info) {
77 OS << "\n.debug_info contents:\n";
Alexey Samsonov1eabf982014-03-13 07:52:54 +000078 for (const auto &CU : compile_units())
79 CU->dump(OS);
Eli Bendersky7a94daa2013-01-25 20:26:43 +000080 }
Benjamin Kramera6002fd2011-09-14 01:09:52 +000081
David Blaikie66865d62014-01-09 00:13:35 +000082 if ((DumpType == DIDT_All || DumpType == DIDT_InfoDwo) &&
83 getNumDWOCompileUnits()) {
84 OS << "\n.debug_info.dwo contents:\n";
Alexey Samsonov1eabf982014-03-13 07:52:54 +000085 for (const auto &DWOCU : dwo_compile_units())
86 DWOCU->dump(OS);
David Blaikie66865d62014-01-09 00:13:35 +000087 }
David Blaikie622dce42014-01-08 23:29:59 +000088
David Blaikie66865d62014-01-09 00:13:35 +000089 if ((DumpType == DIDT_All || DumpType == DIDT_Types) && getNumTypeUnits()) {
David Blaikie03c089c2013-09-23 22:44:47 +000090 OS << "\n.debug_types contents:\n";
Alexey Samsonov1eabf982014-03-13 07:52:54 +000091 for (const auto &TU : type_units())
92 TU->dump(OS);
David Blaikie03c089c2013-09-23 22:44:47 +000093 }
94
Alexey Samsonov1eabf982014-03-13 07:52:54 +000095 if ((DumpType == DIDT_All || DumpType == DIDT_TypesDwo) &&
96 getNumDWOTypeUnits()) {
97 OS << "\n.debug_types.dwo contents:\n";
98 for (const auto &DWOTU : dwo_type_units())
99 DWOTU->dump(OS);
100 }
David Blaikie92d9d622014-01-09 05:08:24 +0000101
David Blaikie18e73502013-06-19 21:37:13 +0000102 if (DumpType == DIDT_All || DumpType == DIDT_Loc) {
David Blaikie03c089c2013-09-23 22:44:47 +0000103 OS << "\n.debug_loc contents:\n";
David Blaikie18e73502013-06-19 21:37:13 +0000104 getDebugLoc()->dump(OS);
105 }
106
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000107 if (DumpType == DIDT_All || DumpType == DIDT_Frames) {
108 OS << "\n.debug_frame contents:\n";
109 getDebugFrame()->dump(OS);
110 }
111
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000112 uint32_t offset = 0;
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000113 if (DumpType == DIDT_All || DumpType == DIDT_Aranges) {
114 OS << "\n.debug_aranges contents:\n";
115 DataExtractor arangesData(getARangeSection(), isLittleEndian(), 0);
116 DWARFDebugArangeSet set;
117 while (set.extract(arangesData, &offset))
118 set.dump(OS);
119 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000120
Alexey Samsonov034e57a2012-08-27 07:17:47 +0000121 uint8_t savedAddressByteSize = 0;
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000122 if (DumpType == DIDT_All || DumpType == DIDT_Line) {
123 OS << "\n.debug_line contents:\n";
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000124 for (const auto &CU : compile_units()) {
125 savedAddressByteSize = CU->getAddressByteSize();
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000126 unsigned stmtOffset =
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000127 CU->getCompileUnitDIE()->getAttributeValueAsSectionOffset(
128 CU, DW_AT_stmt_list, -1U);
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000129 if (stmtOffset != -1U) {
David Blaikie1b5ee5d2013-09-23 17:42:01 +0000130 DataExtractor lineData(getLineSection().Data, isLittleEndian(),
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000131 savedAddressByteSize);
132 DWARFDebugLine::DumpingState state(OS);
David Blaikie1b5ee5d2013-09-23 17:42:01 +0000133 DWARFDebugLine::parseStatementTable(lineData, &getLineSection().Relocs, &stmtOffset, state);
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000134 }
Benjamin Kramer6dda0322011-09-15 18:02:20 +0000135 }
136 }
Benjamin Kramer07d4b1c2011-09-15 16:57:13 +0000137
David Blaikie1d4736e2014-02-24 23:58:54 +0000138 if (DumpType == DIDT_All || DumpType == DIDT_LineDwo) {
139 OS << "\n.debug_line.dwo contents:\n";
140 unsigned stmtOffset = 0;
141 DataExtractor lineData(getLineDWOSection().Data, isLittleEndian(),
142 savedAddressByteSize);
143 DWARFDebugLine::DumpingState state(OS);
144 while (DWARFDebugLine::parsePrologue(lineData, &stmtOffset, &state.Prologue))
145 state.finalize();
146 }
147
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000148 if (DumpType == DIDT_All || DumpType == DIDT_Str) {
149 OS << "\n.debug_str contents:\n";
150 DataExtractor strData(getStringSection(), isLittleEndian(), 0);
151 offset = 0;
152 uint32_t strOffset = 0;
153 while (const char *s = strData.getCStr(&offset)) {
154 OS << format("0x%8.8x: \"%s\"\n", strOffset, s);
155 strOffset = offset;
156 }
Benjamin Kramer07d4b1c2011-09-15 16:57:13 +0000157 }
Alexey Samsonov034e57a2012-08-27 07:17:47 +0000158
David Blaikie66865d62014-01-09 00:13:35 +0000159 if ((DumpType == DIDT_All || DumpType == DIDT_StrDwo) &&
160 !getStringDWOSection().empty()) {
161 OS << "\n.debug_str.dwo contents:\n";
162 DataExtractor strDWOData(getStringDWOSection(), isLittleEndian(), 0);
163 offset = 0;
164 uint32_t strDWOOffset = 0;
165 while (const char *s = strDWOData.getCStr(&offset)) {
166 OS << format("0x%8.8x: \"%s\"\n", strDWOOffset, s);
167 strDWOOffset = offset;
David Blaikie622dce42014-01-08 23:29:59 +0000168 }
David Blaikie66865d62014-01-09 00:13:35 +0000169 }
David Blaikie622dce42014-01-08 23:29:59 +0000170
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000171 if (DumpType == DIDT_All || DumpType == DIDT_Ranges) {
172 OS << "\n.debug_ranges contents:\n";
173 // In fact, different compile units may have different address byte
174 // sizes, but for simplicity we just use the address byte size of the last
175 // compile unit (there is no easy and fast way to associate address range
176 // list and the compile unit it describes).
177 DataExtractor rangesData(getRangeSection(), isLittleEndian(),
178 savedAddressByteSize);
179 offset = 0;
180 DWARFDebugRangeList rangeList;
181 while (rangeList.extract(rangesData, &offset))
182 rangeList.dump(OS);
Eric Christopherda4b2192013-01-02 23:52:13 +0000183 }
Eric Christopher962c9082013-01-15 23:56:56 +0000184
Eric Christopher0de53592013-09-25 23:02:36 +0000185 if (DumpType == DIDT_All || DumpType == DIDT_Pubnames)
186 dumpPubSection(OS, "debug_pubnames", getPubNamesSection(),
187 isLittleEndian(), false);
Krzysztof Parzyszek97438dc2013-02-12 16:20:28 +0000188
Eric Christopher4c7e6ba2013-09-25 23:02:41 +0000189 if (DumpType == DIDT_All || DumpType == DIDT_Pubtypes)
190 dumpPubSection(OS, "debug_pubtypes", getPubTypesSection(),
191 isLittleEndian(), false);
192
David Blaikieecd21ff2013-09-24 19:50:00 +0000193 if (DumpType == DIDT_All || DumpType == DIDT_GnuPubnames)
194 dumpPubSection(OS, "debug_gnu_pubnames", getGnuPubNamesSection(),
Eric Christopher0de53592013-09-25 23:02:36 +0000195 isLittleEndian(), true /* GnuStyle */);
David Blaikieecd21ff2013-09-24 19:50:00 +0000196
197 if (DumpType == DIDT_All || DumpType == DIDT_GnuPubtypes)
198 dumpPubSection(OS, "debug_gnu_pubtypes", getGnuPubTypesSection(),
Eric Christopher0de53592013-09-25 23:02:36 +0000199 isLittleEndian(), true /* GnuStyle */);
David Blaikie404d3042013-09-19 23:01:29 +0000200
David Blaikie66865d62014-01-09 00:13:35 +0000201 if ((DumpType == DIDT_All || DumpType == DIDT_StrOffsetsDwo) &&
202 !getStringOffsetDWOSection().empty()) {
203 OS << "\n.debug_str_offsets.dwo contents:\n";
204 DataExtractor strOffsetExt(getStringOffsetDWOSection(), isLittleEndian(),
205 0);
206 offset = 0;
207 uint64_t size = getStringOffsetDWOSection().size();
208 while (offset < size) {
209 OS << format("0x%8.8x: ", offset);
210 OS << format("%8.8x\n", strOffsetExt.getU32(&offset));
Eric Christopher92f3c0b2013-05-06 17:50:42 +0000211 }
David Blaikie66865d62014-01-09 00:13:35 +0000212 }
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000213}
214
215const DWARFDebugAbbrev *DWARFContext::getDebugAbbrev() {
216 if (Abbrev)
217 return Abbrev.get();
218
219 DataExtractor abbrData(getAbbrevSection(), isLittleEndian(), 0);
220
221 Abbrev.reset(new DWARFDebugAbbrev());
222 Abbrev->parse(abbrData);
223 return Abbrev.get();
224}
225
Eric Christopherda4b2192013-01-02 23:52:13 +0000226const DWARFDebugAbbrev *DWARFContext::getDebugAbbrevDWO() {
227 if (AbbrevDWO)
228 return AbbrevDWO.get();
229
230 DataExtractor abbrData(getAbbrevDWOSection(), isLittleEndian(), 0);
231 AbbrevDWO.reset(new DWARFDebugAbbrev());
232 AbbrevDWO->parse(abbrData);
233 return AbbrevDWO.get();
234}
235
David Blaikie18e73502013-06-19 21:37:13 +0000236const DWARFDebugLoc *DWARFContext::getDebugLoc() {
237 if (Loc)
238 return Loc.get();
239
David Blaikie1b5ee5d2013-09-23 17:42:01 +0000240 DataExtractor LocData(getLocSection().Data, isLittleEndian(), 0);
241 Loc.reset(new DWARFDebugLoc(getLocSection().Relocs));
David Blaikie18e73502013-06-19 21:37:13 +0000242 // assume all compile units have the same address byte size
243 if (getNumCompileUnits())
244 Loc->parse(LocData, getCompileUnitAtIndex(0)->getAddressByteSize());
245 return Loc.get();
246}
247
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000248const DWARFDebugAranges *DWARFContext::getDebugAranges() {
249 if (Aranges)
250 return Aranges.get();
251
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000252 Aranges.reset(new DWARFDebugAranges());
Alexey Samsonova1694c12012-11-16 08:36:25 +0000253 Aranges->generate(this);
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000254 return Aranges.get();
255}
256
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000257const DWARFDebugFrame *DWARFContext::getDebugFrame() {
258 if (DebugFrame)
259 return DebugFrame.get();
260
261 // There's a "bug" in the DWARFv3 standard with respect to the target address
262 // size within debug frame sections. While DWARF is supposed to be independent
263 // of its container, FDEs have fields with size being "target address size",
264 // which isn't specified in DWARF in general. It's only specified for CUs, but
265 // .eh_frame can appear without a .debug_info section. Follow the example of
266 // other tools (libdwarf) and extract this from the container (ObjectFile
267 // provides this information). This problem is fixed in DWARFv4
268 // See this dwarf-discuss discussion for more details:
269 // http://lists.dwarfstd.org/htdig.cgi/dwarf-discuss-dwarfstd.org/2011-December/001173.html
270 DataExtractor debugFrameData(getDebugFrameSection(), isLittleEndian(),
271 getAddressSize());
272 DebugFrame.reset(new DWARFDebugFrame());
273 DebugFrame->parse(debugFrameData);
274 return DebugFrame.get();
275}
276
Eric Christopher494109b2012-10-16 23:46:25 +0000277const DWARFLineTable *
Benjamin Kramer679e1752011-09-15 20:43:18 +0000278DWARFContext::getLineTableForCompileUnit(DWARFCompileUnit *cu) {
279 if (!Line)
David Blaikie1b5ee5d2013-09-23 17:42:01 +0000280 Line.reset(new DWARFDebugLine(&getLineSection().Relocs));
Benjamin Kramer5acab502011-09-15 02:12:05 +0000281
Benjamin Kramer679e1752011-09-15 20:43:18 +0000282 unsigned stmtOffset =
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000283 cu->getCompileUnitDIE()->getAttributeValueAsSectionOffset(
284 cu, DW_AT_stmt_list, -1U);
Benjamin Kramer679e1752011-09-15 20:43:18 +0000285 if (stmtOffset == -1U)
286 return 0; // No line table for this compile unit.
Benjamin Kramer5acab502011-09-15 02:12:05 +0000287
Benjamin Kramer679e1752011-09-15 20:43:18 +0000288 // See if the line table is cached.
Eric Christopher494109b2012-10-16 23:46:25 +0000289 if (const DWARFLineTable *lt = Line->getLineTable(stmtOffset))
Benjamin Kramer679e1752011-09-15 20:43:18 +0000290 return lt;
291
292 // We have to parse it first.
David Blaikie1b5ee5d2013-09-23 17:42:01 +0000293 DataExtractor lineData(getLineSection().Data, isLittleEndian(),
Benjamin Kramer679e1752011-09-15 20:43:18 +0000294 cu->getAddressByteSize());
295 return Line->getOrParseLineTable(lineData, stmtOffset);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000296}
297
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000298void DWARFContext::parseCompileUnits() {
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000299 if (!CUs.empty())
300 return;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000301 uint32_t offset = 0;
David Blaikie1b5ee5d2013-09-23 17:42:01 +0000302 const DataExtractor &DIData = DataExtractor(getInfoSection().Data,
Eric Christopher02509482012-10-16 23:46:23 +0000303 isLittleEndian(), 0);
304 while (DIData.isValidOffset(offset)) {
Ahmed Charles56440fd2014-03-06 05:51:42 +0000305 std::unique_ptr<DWARFCompileUnit> CU(new DWARFCompileUnit(
David Blaikie1b5ee5d2013-09-23 17:42:01 +0000306 getDebugAbbrev(), getInfoSection().Data, getAbbrevSection(),
Alexey Samsonova9debbf2013-08-23 06:56:01 +0000307 getRangeSection(), getStringSection(), StringRef(), getAddrSection(),
David Blaikie1b5ee5d2013-09-23 17:42:01 +0000308 &getInfoSection().Relocs, isLittleEndian()));
Alexey Samsonova9debbf2013-08-23 06:56:01 +0000309 if (!CU->extract(DIData, &offset)) {
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000310 break;
311 }
Ahmed Charles96c9d952014-03-05 10:19:29 +0000312 CUs.push_back(CU.release());
David Blaikie07e22442013-09-23 22:44:40 +0000313 offset = CUs.back()->getNextUnitOffset();
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000314 }
315}
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000316
David Blaikie03c089c2013-09-23 22:44:47 +0000317void DWARFContext::parseTypeUnits() {
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000318 if (!TUs.empty())
319 return;
320 for (const auto &I : getTypesSections()) {
David Blaikie03c089c2013-09-23 22:44:47 +0000321 uint32_t offset = 0;
322 const DataExtractor &DIData =
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000323 DataExtractor(I.second.Data, isLittleEndian(), 0);
David Blaikie03c089c2013-09-23 22:44:47 +0000324 while (DIData.isValidOffset(offset)) {
Ahmed Charles56440fd2014-03-06 05:51:42 +0000325 std::unique_ptr<DWARFTypeUnit> TU(new DWARFTypeUnit(
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000326 getDebugAbbrev(), I.second.Data, getAbbrevSection(),
David Blaikie03c089c2013-09-23 22:44:47 +0000327 getRangeSection(), getStringSection(), StringRef(), getAddrSection(),
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000328 &I.second.Relocs, isLittleEndian()));
David Blaikie03c089c2013-09-23 22:44:47 +0000329 if (!TU->extract(DIData, &offset))
330 break;
Ahmed Charles96c9d952014-03-05 10:19:29 +0000331 TUs.push_back(TU.release());
David Blaikie03c089c2013-09-23 22:44:47 +0000332 offset = TUs.back()->getNextUnitOffset();
333 }
334 }
335}
336
Eric Christopherda4b2192013-01-02 23:52:13 +0000337void DWARFContext::parseDWOCompileUnits() {
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000338 if (!DWOCUs.empty())
339 return;
Eric Christopherda4b2192013-01-02 23:52:13 +0000340 uint32_t offset = 0;
David Blaikie1b5ee5d2013-09-23 17:42:01 +0000341 const DataExtractor &DIData =
342 DataExtractor(getInfoDWOSection().Data, isLittleEndian(), 0);
Eric Christopherda4b2192013-01-02 23:52:13 +0000343 while (DIData.isValidOffset(offset)) {
Ahmed Charles56440fd2014-03-06 05:51:42 +0000344 std::unique_ptr<DWARFCompileUnit> DWOCU(new DWARFCompileUnit(
David Blaikie1b5ee5d2013-09-23 17:42:01 +0000345 getDebugAbbrevDWO(), getInfoDWOSection().Data, getAbbrevDWOSection(),
Alexey Samsonova9debbf2013-08-23 06:56:01 +0000346 getRangeDWOSection(), getStringDWOSection(),
David Blaikie1b5ee5d2013-09-23 17:42:01 +0000347 getStringOffsetDWOSection(), getAddrSection(),
348 &getInfoDWOSection().Relocs, isLittleEndian()));
Alexey Samsonova9debbf2013-08-23 06:56:01 +0000349 if (!DWOCU->extract(DIData, &offset)) {
Eric Christopherda4b2192013-01-02 23:52:13 +0000350 break;
351 }
Ahmed Charles96c9d952014-03-05 10:19:29 +0000352 DWOCUs.push_back(DWOCU.release());
David Blaikie07e22442013-09-23 22:44:40 +0000353 offset = DWOCUs.back()->getNextUnitOffset();
Eric Christopherda4b2192013-01-02 23:52:13 +0000354 }
355}
356
David Blaikie92d9d622014-01-09 05:08:24 +0000357void DWARFContext::parseDWOTypeUnits() {
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000358 if (!DWOTUs.empty())
359 return;
360 for (const auto &I : getTypesDWOSections()) {
David Blaikie92d9d622014-01-09 05:08:24 +0000361 uint32_t offset = 0;
362 const DataExtractor &DIData =
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000363 DataExtractor(I.second.Data, isLittleEndian(), 0);
David Blaikie92d9d622014-01-09 05:08:24 +0000364 while (DIData.isValidOffset(offset)) {
Ahmed Charles56440fd2014-03-06 05:51:42 +0000365 std::unique_ptr<DWARFTypeUnit> TU(new DWARFTypeUnit(
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000366 getDebugAbbrevDWO(), I.second.Data, getAbbrevDWOSection(),
David Blaikie92d9d622014-01-09 05:08:24 +0000367 getRangeDWOSection(), getStringDWOSection(),
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000368 getStringOffsetDWOSection(), getAddrSection(), &I.second.Relocs,
David Blaikie92d9d622014-01-09 05:08:24 +0000369 isLittleEndian()));
370 if (!TU->extract(DIData, &offset))
371 break;
Ahmed Charles96c9d952014-03-05 10:19:29 +0000372 DWOTUs.push_back(TU.release());
David Blaikie92d9d622014-01-09 05:08:24 +0000373 offset = DWOTUs.back()->getNextUnitOffset();
374 }
375 }
376}
377
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000378namespace {
379 struct OffsetComparator {
Alexey Samsonova9debbf2013-08-23 06:56:01 +0000380 bool operator()(const DWARFCompileUnit *LHS,
381 const DWARFCompileUnit *RHS) const {
382 return LHS->getOffset() < RHS->getOffset();
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000383 }
Alexey Samsonova9debbf2013-08-23 06:56:01 +0000384 bool operator()(const DWARFCompileUnit *LHS, uint32_t RHS) const {
385 return LHS->getOffset() < RHS;
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000386 }
Alexey Samsonova9debbf2013-08-23 06:56:01 +0000387 bool operator()(uint32_t LHS, const DWARFCompileUnit *RHS) const {
388 return LHS < RHS->getOffset();
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000389 }
390 };
391}
392
Alexey Samsonov45be7932012-08-30 07:49:50 +0000393DWARFCompileUnit *DWARFContext::getCompileUnitForOffset(uint32_t Offset) {
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000394 parseCompileUnits();
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000395
Alexey Samsonova9debbf2013-08-23 06:56:01 +0000396 DWARFCompileUnit **CU =
397 std::lower_bound(CUs.begin(), CUs.end(), Offset, OffsetComparator());
398 if (CU != CUs.end()) {
399 return *CU;
400 }
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000401 return 0;
402}
403
Alexey Samsonov45be7932012-08-30 07:49:50 +0000404DWARFCompileUnit *DWARFContext::getCompileUnitForAddress(uint64_t Address) {
Benjamin Kramer112ec172011-09-15 21:59:13 +0000405 // First, get the offset of the compile unit.
Alexey Samsonov45be7932012-08-30 07:49:50 +0000406 uint32_t CUOffset = getDebugAranges()->findAddress(Address);
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000407 // Retrieve the compile unit.
Alexey Samsonov45be7932012-08-30 07:49:50 +0000408 return getCompileUnitForOffset(CUOffset);
409}
410
Eric Christopher494109b2012-10-16 23:46:25 +0000411static bool getFileNameForCompileUnit(DWARFCompileUnit *CU,
412 const DWARFLineTable *LineTable,
413 uint64_t FileIndex,
414 bool NeedsAbsoluteFilePath,
415 std::string &FileName) {
Alexey Samsonov45be7932012-08-30 07:49:50 +0000416 if (CU == 0 ||
417 LineTable == 0 ||
418 !LineTable->getFileNameByIndex(FileIndex, NeedsAbsoluteFilePath,
419 FileName))
420 return false;
421 if (NeedsAbsoluteFilePath && sys::path::is_relative(FileName)) {
422 // We may still need to append compilation directory of compile unit.
423 SmallString<16> AbsolutePath;
424 if (const char *CompilationDir = CU->getCompilationDir()) {
425 sys::path::append(AbsolutePath, CompilationDir);
426 }
427 sys::path::append(AbsolutePath, FileName);
428 FileName = AbsolutePath.str();
429 }
430 return true;
431}
432
Eric Christopher494109b2012-10-16 23:46:25 +0000433static bool getFileLineInfoForCompileUnit(DWARFCompileUnit *CU,
434 const DWARFLineTable *LineTable,
435 uint64_t Address,
436 bool NeedsAbsoluteFilePath,
437 std::string &FileName,
438 uint32_t &Line, uint32_t &Column) {
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000439 if (CU == 0 || LineTable == 0)
Alexey Samsonov45be7932012-08-30 07:49:50 +0000440 return false;
441 // Get the index of row we're looking for in the line table.
442 uint32_t RowIndex = LineTable->lookupAddress(Address);
443 if (RowIndex == -1U)
444 return false;
445 // Take file number and line/column from the row.
446 const DWARFDebugLine::Row &Row = LineTable->Rows[RowIndex];
447 if (!getFileNameForCompileUnit(CU, LineTable, Row.File,
448 NeedsAbsoluteFilePath, FileName))
449 return false;
450 Line = Row.Line;
451 Column = Row.Column;
452 return true;
453}
454
455DILineInfo DWARFContext::getLineInfoForAddress(uint64_t Address,
456 DILineInfoSpecifier Specifier) {
457 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
458 if (!CU)
Alexey Samsonovf4462fa2012-07-02 05:54:45 +0000459 return DILineInfo();
Alexey Samsonov45be7932012-08-30 07:49:50 +0000460 std::string FileName = "<invalid>";
461 std::string FunctionName = "<invalid>";
462 uint32_t Line = 0;
463 uint32_t Column = 0;
464 if (Specifier.needs(DILineInfoSpecifier::FunctionName)) {
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000465 // The address may correspond to instruction in some inlined function,
466 // so we have to build the chain of inlined functions and take the
467 // name of the topmost function in it.
Alexey Samsonov3211e612013-08-06 10:49:15 +0000468 const DWARFDebugInfoEntryInlinedChain &InlinedChain =
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000469 CU->getInlinedChainForAddress(Address);
Alexey Samsonov3211e612013-08-06 10:49:15 +0000470 if (InlinedChain.DIEs.size() > 0) {
471 const DWARFDebugInfoEntryMinimal &TopFunctionDIE = InlinedChain.DIEs[0];
David Blaikie07e22442013-09-23 22:44:40 +0000472 if (const char *Name = TopFunctionDIE.getSubroutineName(InlinedChain.U))
Alexey Samsonov45be7932012-08-30 07:49:50 +0000473 FunctionName = Name;
Alexey Samsonovb604ff22012-07-17 15:28:35 +0000474 }
Alexey Samsonovf4462fa2012-07-02 05:54:45 +0000475 }
Alexey Samsonov45be7932012-08-30 07:49:50 +0000476 if (Specifier.needs(DILineInfoSpecifier::FileLineInfo)) {
Eric Christopher494109b2012-10-16 23:46:25 +0000477 const DWARFLineTable *LineTable = getLineTableForCompileUnit(CU);
Alexey Samsonov45be7932012-08-30 07:49:50 +0000478 const bool NeedsAbsoluteFilePath =
479 Specifier.needs(DILineInfoSpecifier::AbsoluteFilePath);
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000480 getFileLineInfoForCompileUnit(CU, LineTable, Address,
481 NeedsAbsoluteFilePath,
Alexey Samsonov45be7932012-08-30 07:49:50 +0000482 FileName, Line, Column);
Alexey Samsonovf4462fa2012-07-02 05:54:45 +0000483 }
Alexey Samsonov45be7932012-08-30 07:49:50 +0000484 return DILineInfo(StringRef(FileName), StringRef(FunctionName),
485 Line, Column);
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000486}
David Blaikiea379b1812011-12-20 02:50:00 +0000487
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000488DILineInfoTable DWARFContext::getLineInfoForAddressRange(uint64_t Address,
489 uint64_t Size,
490 DILineInfoSpecifier Specifier) {
491 DILineInfoTable Lines;
492 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
493 if (!CU)
494 return Lines;
495
496 std::string FunctionName = "<invalid>";
497 if (Specifier.needs(DILineInfoSpecifier::FunctionName)) {
498 // The address may correspond to instruction in some inlined function,
499 // so we have to build the chain of inlined functions and take the
500 // name of the topmost function in it.
Alexey Samsonov3211e612013-08-06 10:49:15 +0000501 const DWARFDebugInfoEntryInlinedChain &InlinedChain =
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000502 CU->getInlinedChainForAddress(Address);
Alexey Samsonov3211e612013-08-06 10:49:15 +0000503 if (InlinedChain.DIEs.size() > 0) {
504 const DWARFDebugInfoEntryMinimal &TopFunctionDIE = InlinedChain.DIEs[0];
David Blaikie07e22442013-09-23 22:44:40 +0000505 if (const char *Name = TopFunctionDIE.getSubroutineName(InlinedChain.U))
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000506 FunctionName = Name;
507 }
508 }
509
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000510 // If the Specifier says we don't need FileLineInfo, just
511 // return the top-most function at the starting address.
512 if (!Specifier.needs(DILineInfoSpecifier::FileLineInfo)) {
David Blaikieba860d72013-09-22 17:01:50 +0000513 Lines.push_back(
514 std::make_pair(Address, DILineInfo("<invalid>", FunctionName, 0, 0)));
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000515 return Lines;
516 }
517
518 const DWARFLineTable *LineTable = getLineTableForCompileUnit(CU);
519 const bool NeedsAbsoluteFilePath =
520 Specifier.needs(DILineInfoSpecifier::AbsoluteFilePath);
521
522 // Get the index of row we're looking for in the line table.
523 std::vector<uint32_t> RowVector;
524 if (!LineTable->lookupAddressRange(Address, Size, RowVector))
525 return Lines;
526
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000527 for (uint32_t RowIndex : RowVector) {
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000528 // Take file number and line/column from the row.
529 const DWARFDebugLine::Row &Row = LineTable->Rows[RowIndex];
530 std::string FileName = "<invalid>";
531 getFileNameForCompileUnit(CU, LineTable, Row.File,
532 NeedsAbsoluteFilePath, FileName);
David Blaikieba860d72013-09-22 17:01:50 +0000533 Lines.push_back(std::make_pair(
534 Row.Address, DILineInfo(FileName, FunctionName, Row.Line, Row.Column)));
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000535 }
536
537 return Lines;
538}
539
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000540DIInliningInfo DWARFContext::getInliningInfoForAddress(uint64_t Address,
541 DILineInfoSpecifier Specifier) {
542 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
543 if (!CU)
544 return DIInliningInfo();
545
Alexey Samsonov3211e612013-08-06 10:49:15 +0000546 const DWARFDebugInfoEntryInlinedChain &InlinedChain =
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000547 CU->getInlinedChainForAddress(Address);
Alexey Samsonov3211e612013-08-06 10:49:15 +0000548 if (InlinedChain.DIEs.size() == 0)
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000549 return DIInliningInfo();
550
551 DIInliningInfo InliningInfo;
552 uint32_t CallFile = 0, CallLine = 0, CallColumn = 0;
Eric Christopher494109b2012-10-16 23:46:25 +0000553 const DWARFLineTable *LineTable = 0;
Alexey Samsonov3211e612013-08-06 10:49:15 +0000554 for (uint32_t i = 0, n = InlinedChain.DIEs.size(); i != n; i++) {
555 const DWARFDebugInfoEntryMinimal &FunctionDIE = InlinedChain.DIEs[i];
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000556 std::string FileName = "<invalid>";
557 std::string FunctionName = "<invalid>";
558 uint32_t Line = 0;
559 uint32_t Column = 0;
560 // Get function name if necessary.
561 if (Specifier.needs(DILineInfoSpecifier::FunctionName)) {
David Blaikie07e22442013-09-23 22:44:40 +0000562 if (const char *Name = FunctionDIE.getSubroutineName(InlinedChain.U))
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000563 FunctionName = Name;
564 }
565 if (Specifier.needs(DILineInfoSpecifier::FileLineInfo)) {
566 const bool NeedsAbsoluteFilePath =
567 Specifier.needs(DILineInfoSpecifier::AbsoluteFilePath);
568 if (i == 0) {
569 // For the topmost frame, initialize the line table of this
570 // compile unit and fetch file/line info from it.
571 LineTable = getLineTableForCompileUnit(CU);
572 // For the topmost routine, get file/line info from line table.
573 getFileLineInfoForCompileUnit(CU, LineTable, Address,
574 NeedsAbsoluteFilePath,
575 FileName, Line, Column);
576 } else {
577 // Otherwise, use call file, call line and call column from
578 // previous DIE in inlined chain.
579 getFileNameForCompileUnit(CU, LineTable, CallFile,
580 NeedsAbsoluteFilePath, FileName);
581 Line = CallLine;
582 Column = CallColumn;
583 }
584 // Get call file/line/column of a current DIE.
585 if (i + 1 < n) {
David Blaikie07e22442013-09-23 22:44:40 +0000586 FunctionDIE.getCallerFrame(InlinedChain.U, CallFile, CallLine,
Alexey Samsonov3211e612013-08-06 10:49:15 +0000587 CallColumn);
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000588 }
589 }
590 DILineInfo Frame(StringRef(FileName), StringRef(FunctionName),
591 Line, Column);
592 InliningInfo.addFrame(Frame);
593 }
594 return InliningInfo;
595}
596
Alexey Samsonov068fc8a2013-04-23 10:17:34 +0000597static bool consumeCompressedDebugSectionHeader(StringRef &data,
598 uint64_t &OriginalSize) {
599 // Consume "ZLIB" prefix.
600 if (!data.startswith("ZLIB"))
601 return false;
602 data = data.substr(4);
603 // Consume uncompressed section size (big-endian 8 bytes).
604 DataExtractor extractor(data, false, 8);
605 uint32_t Offset = 0;
606 OriginalSize = extractor.getU64(&Offset);
607 if (Offset == 0)
608 return false;
609 data = data.substr(Offset);
610 return true;
611}
612
Eric Christopher7370b552012-11-12 21:40:38 +0000613DWARFContextInMemory::DWARFContextInMemory(object::ObjectFile *Obj) :
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000614 IsLittleEndian(Obj->isLittleEndian()),
615 AddressSize(Obj->getBytesInAddress()) {
Rafael Espindolab5155a52014-02-10 20:24:04 +0000616 for (object::section_iterator i = Obj->section_begin(),
617 e = Obj->section_end();
Rafael Espindola5e812af2014-01-30 02:49:50 +0000618 i != e; ++i) {
Eric Christopher7370b552012-11-12 21:40:38 +0000619 StringRef name;
620 i->getName(name);
621 StringRef data;
622 i->getContents(data);
623
Eric Christopher7370b552012-11-12 21:40:38 +0000624 name = name.substr(name.find_first_not_of("._")); // Skip . and _ prefixes.
Alexey Samsonov3d0e3ed2013-04-17 14:27:04 +0000625
Alexey Samsonov068fc8a2013-04-23 10:17:34 +0000626 // Check if debug info section is compressed with zlib.
627 if (name.startswith("zdebug_")) {
628 uint64_t OriginalSize;
629 if (!zlib::isAvailable() ||
630 !consumeCompressedDebugSectionHeader(data, OriginalSize))
631 continue;
Ahmed Charles56440fd2014-03-06 05:51:42 +0000632 std::unique_ptr<MemoryBuffer> UncompressedSection;
Alexey Samsonov068fc8a2013-04-23 10:17:34 +0000633 if (zlib::uncompress(data, UncompressedSection, OriginalSize) !=
634 zlib::StatusOK)
635 continue;
636 // Make data point to uncompressed section contents and save its contents.
637 name = name.substr(1);
638 data = UncompressedSection->getBuffer();
Ahmed Charles96c9d952014-03-05 10:19:29 +0000639 UncompressedSections.push_back(UncompressedSection.release());
Alexey Samsonov068fc8a2013-04-23 10:17:34 +0000640 }
641
Eric Christopher4c7e6ba2013-09-25 23:02:41 +0000642 StringRef *Section =
643 StringSwitch<StringRef *>(name)
644 .Case("debug_info", &InfoSection.Data)
645 .Case("debug_abbrev", &AbbrevSection)
646 .Case("debug_loc", &LocSection.Data)
647 .Case("debug_line", &LineSection.Data)
648 .Case("debug_aranges", &ARangeSection)
649 .Case("debug_frame", &DebugFrameSection)
650 .Case("debug_str", &StringSection)
651 .Case("debug_ranges", &RangeSection)
652 .Case("debug_pubnames", &PubNamesSection)
653 .Case("debug_pubtypes", &PubTypesSection)
654 .Case("debug_gnu_pubnames", &GnuPubNamesSection)
655 .Case("debug_gnu_pubtypes", &GnuPubTypesSection)
656 .Case("debug_info.dwo", &InfoDWOSection.Data)
657 .Case("debug_abbrev.dwo", &AbbrevDWOSection)
David Blaikie1d4736e2014-02-24 23:58:54 +0000658 .Case("debug_line.dwo", &LineDWOSection.Data)
Eric Christopher4c7e6ba2013-09-25 23:02:41 +0000659 .Case("debug_str.dwo", &StringDWOSection)
660 .Case("debug_str_offsets.dwo", &StringOffsetDWOSection)
661 .Case("debug_addr", &AddrSection)
662 // Any more debug info sections go here.
663 .Default(0);
Rafael Espindola4f60a382013-05-30 03:05:14 +0000664 if (Section) {
665 *Section = data;
666 if (name == "debug_ranges") {
667 // FIXME: Use the other dwo range section when we emit it.
668 RangeDWOSection = data;
669 }
David Blaikie03c089c2013-09-23 22:44:47 +0000670 } else if (name == "debug_types") {
David Blaikie427e4352013-09-23 23:39:55 +0000671 // Find debug_types data by section rather than name as there are
672 // multiple, comdat grouped, debug_types sections.
David Blaikie03c089c2013-09-23 22:44:47 +0000673 TypesSections[*i].Data = data;
David Blaikie92d9d622014-01-09 05:08:24 +0000674 } else if (name == "debug_types.dwo") {
675 TypesDWOSections[*i].Data = data;
Eric Christopherda4b2192013-01-02 23:52:13 +0000676 }
Eric Christopher7370b552012-11-12 21:40:38 +0000677
Rafael Espindola4f60a382013-05-30 03:05:14 +0000678 section_iterator RelocatedSection = i->getRelocatedSection();
Rafael Espindolab5155a52014-02-10 20:24:04 +0000679 if (RelocatedSection == Obj->section_end())
Rafael Espindola4f60a382013-05-30 03:05:14 +0000680 continue;
681
682 StringRef RelSecName;
683 RelocatedSection->getName(RelSecName);
684 RelSecName = RelSecName.substr(
685 RelSecName.find_first_not_of("._")); // Skip . and _ prefixes.
686
Andrew Kaylord55d7012013-01-25 22:50:58 +0000687 // TODO: Add support for relocations in other sections as needed.
688 // Record relocations for the debug_info and debug_line sections.
Rafael Espindola4f60a382013-05-30 03:05:14 +0000689 RelocAddrMap *Map = StringSwitch<RelocAddrMap*>(RelSecName)
David Blaikie1b5ee5d2013-09-23 17:42:01 +0000690 .Case("debug_info", &InfoSection.Relocs)
691 .Case("debug_loc", &LocSection.Relocs)
692 .Case("debug_info.dwo", &InfoDWOSection.Relocs)
693 .Case("debug_line", &LineSection.Relocs)
Alexey Samsonov3d0e3ed2013-04-17 14:27:04 +0000694 .Default(0);
David Blaikie03c089c2013-09-23 22:44:47 +0000695 if (!Map) {
David Blaikie427e4352013-09-23 23:39:55 +0000696 // Find debug_types relocs by section rather than name as there are
697 // multiple, comdat grouped, debug_types sections.
David Blaikie92d9d622014-01-09 05:08:24 +0000698 if (RelSecName == "debug_types")
699 Map = &TypesSections[*RelocatedSection].Relocs;
700 else if (RelSecName == "debug_types.dwo")
701 Map = &TypesDWOSections[*RelocatedSection].Relocs;
702 else
703 continue;
David Blaikie03c089c2013-09-23 22:44:47 +0000704 }
Eric Christopher7370b552012-11-12 21:40:38 +0000705
Rafael Espindolab5155a52014-02-10 20:24:04 +0000706 if (i->relocation_begin() != i->relocation_end()) {
Eric Christopher7370b552012-11-12 21:40:38 +0000707 uint64_t SectionSize;
Rafael Espindola4f60a382013-05-30 03:05:14 +0000708 RelocatedSection->getSize(SectionSize);
Rafael Espindolab5155a52014-02-10 20:24:04 +0000709 for (object::relocation_iterator reloc_i = i->relocation_begin(),
710 reloc_e = i->relocation_end();
Rafael Espindola5e812af2014-01-30 02:49:50 +0000711 reloc_i != reloc_e; ++reloc_i) {
Eric Christopher7370b552012-11-12 21:40:38 +0000712 uint64_t Address;
Rafael Espindola1e483872013-04-25 12:28:45 +0000713 reloc_i->getOffset(Address);
Eric Christopher7370b552012-11-12 21:40:38 +0000714 uint64_t Type;
715 reloc_i->getType(Type);
Andrew Kaylord55d7012013-01-25 22:50:58 +0000716 uint64_t SymAddr = 0;
717 // ELF relocations may need the symbol address
718 if (Obj->isELF()) {
Rafael Espindola806f0062013-06-05 01:33:53 +0000719 object::symbol_iterator Sym = reloc_i->getSymbol();
720 Sym->getAddress(SymAddr);
Andrew Kaylord55d7012013-01-25 22:50:58 +0000721 }
Eric Christopher7370b552012-11-12 21:40:38 +0000722
723 object::RelocVisitor V(Obj->getFileFormatName());
724 // The section address is always 0 for debug sections.
Andrew Kaylord55d7012013-01-25 22:50:58 +0000725 object::RelocToApply R(V.visit(Type, *reloc_i, 0, SymAddr));
Eric Christopher7370b552012-11-12 21:40:38 +0000726 if (V.error()) {
727 SmallString<32> Name;
728 error_code ec(reloc_i->getTypeName(Name));
729 if (ec) {
730 errs() << "Aaaaaa! Nameless relocation! Aaaaaa!\n";
731 }
732 errs() << "error: failed to compute relocation: "
733 << Name << "\n";
734 continue;
735 }
736
737 if (Address + R.Width > SectionSize) {
738 errs() << "error: " << R.Width << "-byte relocation starting "
739 << Address << " bytes into section " << name << " which is "
740 << SectionSize << " bytes long.\n";
741 continue;
742 }
743 if (R.Width > 8) {
744 errs() << "error: can't handle a relocation of more than 8 bytes at "
745 "a time.\n";
746 continue;
747 }
748 DEBUG(dbgs() << "Writing " << format("%p", R.Value)
749 << " at " << format("%p", Address)
750 << " with width " << format("%d", R.Width)
751 << "\n");
Eric Christopherda4b2192013-01-02 23:52:13 +0000752 Map->insert(std::make_pair(Address, std::make_pair(R.Width, R.Value)));
Eric Christopher7370b552012-11-12 21:40:38 +0000753 }
754 }
755 }
756}
757
Alexey Samsonov068fc8a2013-04-23 10:17:34 +0000758DWARFContextInMemory::~DWARFContextInMemory() {
759 DeleteContainerPointers(UncompressedSections);
760}
761
David Blaikiea379b1812011-12-20 02:50:00 +0000762void DWARFContextInMemory::anchor() { }