blob: 8a245a0301d749162e551d011cdab66dc446f755 [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"
Alexey Samsonove16e16a2012-07-19 07:03:58 +000011#include "llvm/ADT/SmallString.h"
Alexey Samsonov3d0e3ed2013-04-17 14:27:04 +000012#include "llvm/ADT/StringSwitch.h"
Alexey Samsonov068fc8a2013-04-23 10:17:34 +000013#include "llvm/Support/Compression.h"
Benjamin Kramer6dda0322011-09-15 18:02:20 +000014#include "llvm/Support/Dwarf.h"
Benjamin Kramer07d4b1c2011-09-15 16:57:13 +000015#include "llvm/Support/Format.h"
Alexey Samsonove16e16a2012-07-19 07:03:58 +000016#include "llvm/Support/Path.h"
Benjamin Kramera6002fd2011-09-14 01:09:52 +000017#include "llvm/Support/raw_ostream.h"
Benjamin Kramer2602ca62011-09-15 20:43:22 +000018#include <algorithm>
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000019using namespace llvm;
Benjamin Kramer6dda0322011-09-15 18:02:20 +000020using namespace dwarf;
Rafael Espindola4f60a382013-05-30 03:05:14 +000021using namespace object;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000022
Eric Christopher494109b2012-10-16 23:46:25 +000023typedef DWARFDebugLine::LineTable DWARFLineTable;
24
David Blaikieecd21ff2013-09-24 19:50:00 +000025static void dumpPubSection(raw_ostream &OS, StringRef Name, StringRef Data,
Eric Christopher0de53592013-09-25 23:02:36 +000026 bool LittleEndian, bool GnuStyle) {
David Blaikieecd21ff2013-09-24 19:50:00 +000027 OS << "\n." << Name << " contents:\n";
28 DataExtractor pubNames(Data, LittleEndian, 0);
29 uint32_t offset = 0;
David Blaikie8a263cb2013-11-26 00:22:37 +000030 while (pubNames.isValidOffset(offset)) {
31 OS << "length = " << format("0x%08x", pubNames.getU32(&offset));
32 OS << " version = " << format("0x%04x", pubNames.getU16(&offset));
33 OS << " unit_offset = " << format("0x%08x", pubNames.getU32(&offset));
34 OS << " unit_size = " << format("0x%08x", pubNames.getU32(&offset)) << '\n';
35 if (GnuStyle)
36 OS << "Offset Linkage Kind Name\n";
37 else
38 OS << "Offset Name\n";
Eric Christopher0de53592013-09-25 23:02:36 +000039
David Blaikie8a263cb2013-11-26 00:22:37 +000040 while (offset < Data.size()) {
41 uint32_t dieRef = pubNames.getU32(&offset);
42 if (dieRef == 0)
43 break;
44 OS << format("0x%8.8x ", dieRef);
45 if (GnuStyle) {
46 PubIndexEntryDescriptor desc(pubNames.getU8(&offset));
47 OS << format("%-8s", dwarf::GDBIndexEntryLinkageString(desc.Linkage))
48 << ' ' << format("%-8s", dwarf::GDBIndexEntryKindString(desc.Kind))
49 << ' ';
50 }
51 OS << '\"' << pubNames.getCStr(&offset) << "\"\n";
Eric Christopher0de53592013-09-25 23:02:36 +000052 }
David Blaikieecd21ff2013-09-24 19:50:00 +000053 }
54}
55
Eli Bendersky7a94daa2013-01-25 20:26:43 +000056void DWARFContext::dump(raw_ostream &OS, DIDumpType DumpType) {
57 if (DumpType == DIDT_All || DumpType == DIDT_Abbrev) {
58 OS << ".debug_abbrev contents:\n";
59 getDebugAbbrev()->dump(OS);
60 }
Benjamin Kramera6002fd2011-09-14 01:09:52 +000061
David Blaikie66865d62014-01-09 00:13:35 +000062 if (DumpType == DIDT_All || DumpType == DIDT_AbbrevDwo)
63 if (const DWARFDebugAbbrev *D = getDebugAbbrevDWO()) {
David Blaikie622dce42014-01-08 23:29:59 +000064 OS << "\n.debug_abbrev.dwo contents:\n";
David Blaikie66865d62014-01-09 00:13:35 +000065 D->dump(OS);
David Blaikie622dce42014-01-08 23:29:59 +000066 }
David Blaikie622dce42014-01-08 23:29:59 +000067
Eli Bendersky7a94daa2013-01-25 20:26:43 +000068 if (DumpType == DIDT_All || DumpType == DIDT_Info) {
69 OS << "\n.debug_info contents:\n";
Alexey Samsonov1eabf982014-03-13 07:52:54 +000070 for (const auto &CU : compile_units())
71 CU->dump(OS);
Eli Bendersky7a94daa2013-01-25 20:26:43 +000072 }
Benjamin Kramera6002fd2011-09-14 01:09:52 +000073
David Blaikie66865d62014-01-09 00:13:35 +000074 if ((DumpType == DIDT_All || DumpType == DIDT_InfoDwo) &&
75 getNumDWOCompileUnits()) {
76 OS << "\n.debug_info.dwo contents:\n";
Alexey Samsonov1eabf982014-03-13 07:52:54 +000077 for (const auto &DWOCU : dwo_compile_units())
78 DWOCU->dump(OS);
David Blaikie66865d62014-01-09 00:13:35 +000079 }
David Blaikie622dce42014-01-08 23:29:59 +000080
David Blaikie66865d62014-01-09 00:13:35 +000081 if ((DumpType == DIDT_All || DumpType == DIDT_Types) && getNumTypeUnits()) {
David Blaikie03c089c2013-09-23 22:44:47 +000082 OS << "\n.debug_types contents:\n";
Alexey Samsonov1eabf982014-03-13 07:52:54 +000083 for (const auto &TU : type_units())
84 TU->dump(OS);
David Blaikie03c089c2013-09-23 22:44:47 +000085 }
86
Alexey Samsonov1eabf982014-03-13 07:52:54 +000087 if ((DumpType == DIDT_All || DumpType == DIDT_TypesDwo) &&
88 getNumDWOTypeUnits()) {
89 OS << "\n.debug_types.dwo contents:\n";
90 for (const auto &DWOTU : dwo_type_units())
91 DWOTU->dump(OS);
92 }
David Blaikie92d9d622014-01-09 05:08:24 +000093
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";
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000116 for (const auto &CU : compile_units()) {
117 savedAddressByteSize = CU->getAddressByteSize();
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000118 unsigned stmtOffset =
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000119 CU->getCompileUnitDIE()->getAttributeValueAsSectionOffset(
Alexey Samsonov96dc29c2014-03-13 08:19:59 +0000120 CU.get(), DW_AT_stmt_list, -1U);
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000121 if (stmtOffset != -1U) {
David Blaikie1b5ee5d2013-09-23 17:42:01 +0000122 DataExtractor lineData(getLineSection().Data, isLittleEndian(),
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000123 savedAddressByteSize);
124 DWARFDebugLine::DumpingState state(OS);
David Blaikie1b5ee5d2013-09-23 17:42:01 +0000125 DWARFDebugLine::parseStatementTable(lineData, &getLineSection().Relocs, &stmtOffset, state);
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000126 }
Benjamin Kramer6dda0322011-09-15 18:02:20 +0000127 }
128 }
Benjamin Kramer07d4b1c2011-09-15 16:57:13 +0000129
David Blaikie1d4736e2014-02-24 23:58:54 +0000130 if (DumpType == DIDT_All || DumpType == DIDT_LineDwo) {
131 OS << "\n.debug_line.dwo contents:\n";
132 unsigned stmtOffset = 0;
133 DataExtractor lineData(getLineDWOSection().Data, isLittleEndian(),
134 savedAddressByteSize);
135 DWARFDebugLine::DumpingState state(OS);
136 while (DWARFDebugLine::parsePrologue(lineData, &stmtOffset, &state.Prologue))
137 state.finalize();
138 }
139
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000140 if (DumpType == DIDT_All || DumpType == DIDT_Str) {
141 OS << "\n.debug_str contents:\n";
142 DataExtractor strData(getStringSection(), isLittleEndian(), 0);
143 offset = 0;
144 uint32_t strOffset = 0;
145 while (const char *s = strData.getCStr(&offset)) {
146 OS << format("0x%8.8x: \"%s\"\n", strOffset, s);
147 strOffset = offset;
148 }
Benjamin Kramer07d4b1c2011-09-15 16:57:13 +0000149 }
Alexey Samsonov034e57a2012-08-27 07:17:47 +0000150
David Blaikie66865d62014-01-09 00:13:35 +0000151 if ((DumpType == DIDT_All || DumpType == DIDT_StrDwo) &&
152 !getStringDWOSection().empty()) {
153 OS << "\n.debug_str.dwo contents:\n";
154 DataExtractor strDWOData(getStringDWOSection(), isLittleEndian(), 0);
155 offset = 0;
156 uint32_t strDWOOffset = 0;
157 while (const char *s = strDWOData.getCStr(&offset)) {
158 OS << format("0x%8.8x: \"%s\"\n", strDWOOffset, s);
159 strDWOOffset = offset;
David Blaikie622dce42014-01-08 23:29:59 +0000160 }
David Blaikie66865d62014-01-09 00:13:35 +0000161 }
David Blaikie622dce42014-01-08 23:29:59 +0000162
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000163 if (DumpType == DIDT_All || DumpType == DIDT_Ranges) {
164 OS << "\n.debug_ranges contents:\n";
165 // In fact, different compile units may have different address byte
166 // sizes, but for simplicity we just use the address byte size of the last
167 // compile unit (there is no easy and fast way to associate address range
168 // list and the compile unit it describes).
169 DataExtractor rangesData(getRangeSection(), isLittleEndian(),
170 savedAddressByteSize);
171 offset = 0;
172 DWARFDebugRangeList rangeList;
173 while (rangeList.extract(rangesData, &offset))
174 rangeList.dump(OS);
Eric Christopherda4b2192013-01-02 23:52:13 +0000175 }
Eric Christopher962c9082013-01-15 23:56:56 +0000176
Eric Christopher0de53592013-09-25 23:02:36 +0000177 if (DumpType == DIDT_All || DumpType == DIDT_Pubnames)
178 dumpPubSection(OS, "debug_pubnames", getPubNamesSection(),
179 isLittleEndian(), false);
Krzysztof Parzyszek97438dc2013-02-12 16:20:28 +0000180
Eric Christopher4c7e6ba2013-09-25 23:02:41 +0000181 if (DumpType == DIDT_All || DumpType == DIDT_Pubtypes)
182 dumpPubSection(OS, "debug_pubtypes", getPubTypesSection(),
183 isLittleEndian(), false);
184
David Blaikieecd21ff2013-09-24 19:50:00 +0000185 if (DumpType == DIDT_All || DumpType == DIDT_GnuPubnames)
186 dumpPubSection(OS, "debug_gnu_pubnames", getGnuPubNamesSection(),
Eric Christopher0de53592013-09-25 23:02:36 +0000187 isLittleEndian(), true /* GnuStyle */);
David Blaikieecd21ff2013-09-24 19:50:00 +0000188
189 if (DumpType == DIDT_All || DumpType == DIDT_GnuPubtypes)
190 dumpPubSection(OS, "debug_gnu_pubtypes", getGnuPubTypesSection(),
Eric Christopher0de53592013-09-25 23:02:36 +0000191 isLittleEndian(), true /* GnuStyle */);
David Blaikie404d3042013-09-19 23:01:29 +0000192
David Blaikie66865d62014-01-09 00:13:35 +0000193 if ((DumpType == DIDT_All || DumpType == DIDT_StrOffsetsDwo) &&
194 !getStringOffsetDWOSection().empty()) {
195 OS << "\n.debug_str_offsets.dwo contents:\n";
196 DataExtractor strOffsetExt(getStringOffsetDWOSection(), isLittleEndian(),
197 0);
198 offset = 0;
199 uint64_t size = getStringOffsetDWOSection().size();
200 while (offset < size) {
201 OS << format("0x%8.8x: ", offset);
202 OS << format("%8.8x\n", strOffsetExt.getU32(&offset));
Eric Christopher92f3c0b2013-05-06 17:50:42 +0000203 }
David Blaikie66865d62014-01-09 00:13:35 +0000204 }
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000205}
206
207const DWARFDebugAbbrev *DWARFContext::getDebugAbbrev() {
208 if (Abbrev)
209 return Abbrev.get();
210
211 DataExtractor abbrData(getAbbrevSection(), isLittleEndian(), 0);
212
213 Abbrev.reset(new DWARFDebugAbbrev());
214 Abbrev->parse(abbrData);
215 return Abbrev.get();
216}
217
Eric Christopherda4b2192013-01-02 23:52:13 +0000218const DWARFDebugAbbrev *DWARFContext::getDebugAbbrevDWO() {
219 if (AbbrevDWO)
220 return AbbrevDWO.get();
221
222 DataExtractor abbrData(getAbbrevDWOSection(), isLittleEndian(), 0);
223 AbbrevDWO.reset(new DWARFDebugAbbrev());
224 AbbrevDWO->parse(abbrData);
225 return AbbrevDWO.get();
226}
227
David Blaikie18e73502013-06-19 21:37:13 +0000228const DWARFDebugLoc *DWARFContext::getDebugLoc() {
229 if (Loc)
230 return Loc.get();
231
David Blaikie1b5ee5d2013-09-23 17:42:01 +0000232 DataExtractor LocData(getLocSection().Data, isLittleEndian(), 0);
233 Loc.reset(new DWARFDebugLoc(getLocSection().Relocs));
David Blaikie18e73502013-06-19 21:37:13 +0000234 // assume all compile units have the same address byte size
235 if (getNumCompileUnits())
236 Loc->parse(LocData, getCompileUnitAtIndex(0)->getAddressByteSize());
237 return Loc.get();
238}
239
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000240const DWARFDebugAranges *DWARFContext::getDebugAranges() {
241 if (Aranges)
242 return Aranges.get();
243
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000244 Aranges.reset(new DWARFDebugAranges());
Alexey Samsonova1694c12012-11-16 08:36:25 +0000245 Aranges->generate(this);
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000246 return Aranges.get();
247}
248
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000249const DWARFDebugFrame *DWARFContext::getDebugFrame() {
250 if (DebugFrame)
251 return DebugFrame.get();
252
253 // There's a "bug" in the DWARFv3 standard with respect to the target address
254 // size within debug frame sections. While DWARF is supposed to be independent
255 // of its container, FDEs have fields with size being "target address size",
256 // which isn't specified in DWARF in general. It's only specified for CUs, but
257 // .eh_frame can appear without a .debug_info section. Follow the example of
258 // other tools (libdwarf) and extract this from the container (ObjectFile
259 // provides this information). This problem is fixed in DWARFv4
260 // See this dwarf-discuss discussion for more details:
261 // http://lists.dwarfstd.org/htdig.cgi/dwarf-discuss-dwarfstd.org/2011-December/001173.html
262 DataExtractor debugFrameData(getDebugFrameSection(), isLittleEndian(),
263 getAddressSize());
264 DebugFrame.reset(new DWARFDebugFrame());
265 DebugFrame->parse(debugFrameData);
266 return DebugFrame.get();
267}
268
Eric Christopher494109b2012-10-16 23:46:25 +0000269const DWARFLineTable *
Benjamin Kramer679e1752011-09-15 20:43:18 +0000270DWARFContext::getLineTableForCompileUnit(DWARFCompileUnit *cu) {
271 if (!Line)
David Blaikie1b5ee5d2013-09-23 17:42:01 +0000272 Line.reset(new DWARFDebugLine(&getLineSection().Relocs));
Benjamin Kramer5acab502011-09-15 02:12:05 +0000273
Benjamin Kramer679e1752011-09-15 20:43:18 +0000274 unsigned stmtOffset =
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000275 cu->getCompileUnitDIE()->getAttributeValueAsSectionOffset(
276 cu, DW_AT_stmt_list, -1U);
Benjamin Kramer679e1752011-09-15 20:43:18 +0000277 if (stmtOffset == -1U)
278 return 0; // No line table for this compile unit.
Benjamin Kramer5acab502011-09-15 02:12:05 +0000279
Benjamin Kramer679e1752011-09-15 20:43:18 +0000280 // See if the line table is cached.
Eric Christopher494109b2012-10-16 23:46:25 +0000281 if (const DWARFLineTable *lt = Line->getLineTable(stmtOffset))
Benjamin Kramer679e1752011-09-15 20:43:18 +0000282 return lt;
283
284 // We have to parse it first.
David Blaikie1b5ee5d2013-09-23 17:42:01 +0000285 DataExtractor lineData(getLineSection().Data, isLittleEndian(),
Benjamin Kramer679e1752011-09-15 20:43:18 +0000286 cu->getAddressByteSize());
287 return Line->getOrParseLineTable(lineData, stmtOffset);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000288}
289
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000290void DWARFContext::parseCompileUnits() {
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000291 if (!CUs.empty())
292 return;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000293 uint32_t offset = 0;
David Blaikie1b5ee5d2013-09-23 17:42:01 +0000294 const DataExtractor &DIData = DataExtractor(getInfoSection().Data,
Eric Christopher02509482012-10-16 23:46:23 +0000295 isLittleEndian(), 0);
296 while (DIData.isValidOffset(offset)) {
Ahmed Charles56440fd2014-03-06 05:51:42 +0000297 std::unique_ptr<DWARFCompileUnit> CU(new DWARFCompileUnit(
David Blaikie1b5ee5d2013-09-23 17:42:01 +0000298 getDebugAbbrev(), getInfoSection().Data, getAbbrevSection(),
Alexey Samsonova9debbf2013-08-23 06:56:01 +0000299 getRangeSection(), getStringSection(), StringRef(), getAddrSection(),
David Blaikie1b5ee5d2013-09-23 17:42:01 +0000300 &getInfoSection().Relocs, isLittleEndian()));
Alexey Samsonova9debbf2013-08-23 06:56:01 +0000301 if (!CU->extract(DIData, &offset)) {
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000302 break;
303 }
Alexey Samsonov96dc29c2014-03-13 08:19:59 +0000304 CUs.push_back(std::move(CU));
David Blaikie07e22442013-09-23 22:44:40 +0000305 offset = CUs.back()->getNextUnitOffset();
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000306 }
307}
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000308
David Blaikie03c089c2013-09-23 22:44:47 +0000309void DWARFContext::parseTypeUnits() {
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000310 if (!TUs.empty())
311 return;
312 for (const auto &I : getTypesSections()) {
David Blaikie03c089c2013-09-23 22:44:47 +0000313 uint32_t offset = 0;
314 const DataExtractor &DIData =
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000315 DataExtractor(I.second.Data, isLittleEndian(), 0);
David Blaikie03c089c2013-09-23 22:44:47 +0000316 while (DIData.isValidOffset(offset)) {
Ahmed Charles56440fd2014-03-06 05:51:42 +0000317 std::unique_ptr<DWARFTypeUnit> TU(new DWARFTypeUnit(
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000318 getDebugAbbrev(), I.second.Data, getAbbrevSection(),
David Blaikie03c089c2013-09-23 22:44:47 +0000319 getRangeSection(), getStringSection(), StringRef(), getAddrSection(),
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000320 &I.second.Relocs, isLittleEndian()));
David Blaikie03c089c2013-09-23 22:44:47 +0000321 if (!TU->extract(DIData, &offset))
322 break;
Alexey Samsonov96dc29c2014-03-13 08:19:59 +0000323 TUs.push_back(std::move(TU));
David Blaikie03c089c2013-09-23 22:44:47 +0000324 offset = TUs.back()->getNextUnitOffset();
325 }
326 }
327}
328
Eric Christopherda4b2192013-01-02 23:52:13 +0000329void DWARFContext::parseDWOCompileUnits() {
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000330 if (!DWOCUs.empty())
331 return;
Eric Christopherda4b2192013-01-02 23:52:13 +0000332 uint32_t offset = 0;
David Blaikie1b5ee5d2013-09-23 17:42:01 +0000333 const DataExtractor &DIData =
334 DataExtractor(getInfoDWOSection().Data, isLittleEndian(), 0);
Eric Christopherda4b2192013-01-02 23:52:13 +0000335 while (DIData.isValidOffset(offset)) {
Ahmed Charles56440fd2014-03-06 05:51:42 +0000336 std::unique_ptr<DWARFCompileUnit> DWOCU(new DWARFCompileUnit(
David Blaikie1b5ee5d2013-09-23 17:42:01 +0000337 getDebugAbbrevDWO(), getInfoDWOSection().Data, getAbbrevDWOSection(),
Alexey Samsonova9debbf2013-08-23 06:56:01 +0000338 getRangeDWOSection(), getStringDWOSection(),
David Blaikie1b5ee5d2013-09-23 17:42:01 +0000339 getStringOffsetDWOSection(), getAddrSection(),
340 &getInfoDWOSection().Relocs, isLittleEndian()));
Alexey Samsonova9debbf2013-08-23 06:56:01 +0000341 if (!DWOCU->extract(DIData, &offset)) {
Eric Christopherda4b2192013-01-02 23:52:13 +0000342 break;
343 }
Alexey Samsonov96dc29c2014-03-13 08:19:59 +0000344 DWOCUs.push_back(std::move(DWOCU));
David Blaikie07e22442013-09-23 22:44:40 +0000345 offset = DWOCUs.back()->getNextUnitOffset();
Eric Christopherda4b2192013-01-02 23:52:13 +0000346 }
347}
348
David Blaikie92d9d622014-01-09 05:08:24 +0000349void DWARFContext::parseDWOTypeUnits() {
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000350 if (!DWOTUs.empty())
351 return;
352 for (const auto &I : getTypesDWOSections()) {
David Blaikie92d9d622014-01-09 05:08:24 +0000353 uint32_t offset = 0;
354 const DataExtractor &DIData =
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000355 DataExtractor(I.second.Data, isLittleEndian(), 0);
David Blaikie92d9d622014-01-09 05:08:24 +0000356 while (DIData.isValidOffset(offset)) {
Ahmed Charles56440fd2014-03-06 05:51:42 +0000357 std::unique_ptr<DWARFTypeUnit> TU(new DWARFTypeUnit(
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000358 getDebugAbbrevDWO(), I.second.Data, getAbbrevDWOSection(),
David Blaikie92d9d622014-01-09 05:08:24 +0000359 getRangeDWOSection(), getStringDWOSection(),
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000360 getStringOffsetDWOSection(), getAddrSection(), &I.second.Relocs,
David Blaikie92d9d622014-01-09 05:08:24 +0000361 isLittleEndian()));
362 if (!TU->extract(DIData, &offset))
363 break;
Alexey Samsonov96dc29c2014-03-13 08:19:59 +0000364 DWOTUs.push_back(std::move(TU));
David Blaikie92d9d622014-01-09 05:08:24 +0000365 offset = DWOTUs.back()->getNextUnitOffset();
366 }
367 }
368}
369
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000370namespace {
371 struct OffsetComparator {
Alexey Samsonov96dc29c2014-03-13 08:19:59 +0000372
373 bool operator()(const std::unique_ptr<DWARFCompileUnit> &LHS,
374 const std::unique_ptr<DWARFCompileUnit> &RHS) const {
Alexey Samsonova9debbf2013-08-23 06:56:01 +0000375 return LHS->getOffset() < RHS->getOffset();
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000376 }
Alexey Samsonov96dc29c2014-03-13 08:19:59 +0000377 bool operator()(const std::unique_ptr<DWARFCompileUnit> &LHS,
378 uint32_t RHS) const {
Alexey Samsonova9debbf2013-08-23 06:56:01 +0000379 return LHS->getOffset() < RHS;
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000380 }
Alexey Samsonov96dc29c2014-03-13 08:19:59 +0000381 bool operator()(uint32_t LHS,
382 const std::unique_ptr<DWARFCompileUnit> &RHS) const {
Alexey Samsonova9debbf2013-08-23 06:56:01 +0000383 return LHS < RHS->getOffset();
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000384 }
385 };
386}
387
Alexey Samsonov45be7932012-08-30 07:49:50 +0000388DWARFCompileUnit *DWARFContext::getCompileUnitForOffset(uint32_t Offset) {
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000389 parseCompileUnits();
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000390
Alexey Samsonov96dc29c2014-03-13 08:19:59 +0000391 std::unique_ptr<DWARFCompileUnit> *CU =
Alexey Samsonova9debbf2013-08-23 06:56:01 +0000392 std::lower_bound(CUs.begin(), CUs.end(), Offset, OffsetComparator());
393 if (CU != CUs.end()) {
Alexey Samsonov96dc29c2014-03-13 08:19:59 +0000394 return CU->get();
Alexey Samsonova9debbf2013-08-23 06:56:01 +0000395 }
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000396 return 0;
397}
398
Alexey Samsonov45be7932012-08-30 07:49:50 +0000399DWARFCompileUnit *DWARFContext::getCompileUnitForAddress(uint64_t Address) {
Benjamin Kramer112ec172011-09-15 21:59:13 +0000400 // First, get the offset of the compile unit.
Alexey Samsonov45be7932012-08-30 07:49:50 +0000401 uint32_t CUOffset = getDebugAranges()->findAddress(Address);
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000402 // Retrieve the compile unit.
Alexey Samsonov45be7932012-08-30 07:49:50 +0000403 return getCompileUnitForOffset(CUOffset);
404}
405
Eric Christopher494109b2012-10-16 23:46:25 +0000406static bool getFileNameForCompileUnit(DWARFCompileUnit *CU,
407 const DWARFLineTable *LineTable,
408 uint64_t FileIndex,
409 bool NeedsAbsoluteFilePath,
410 std::string &FileName) {
Alexey Samsonov45be7932012-08-30 07:49:50 +0000411 if (CU == 0 ||
412 LineTable == 0 ||
413 !LineTable->getFileNameByIndex(FileIndex, NeedsAbsoluteFilePath,
414 FileName))
415 return false;
416 if (NeedsAbsoluteFilePath && sys::path::is_relative(FileName)) {
417 // We may still need to append compilation directory of compile unit.
418 SmallString<16> AbsolutePath;
419 if (const char *CompilationDir = CU->getCompilationDir()) {
420 sys::path::append(AbsolutePath, CompilationDir);
421 }
422 sys::path::append(AbsolutePath, FileName);
423 FileName = AbsolutePath.str();
424 }
425 return true;
426}
427
Eric Christopher494109b2012-10-16 23:46:25 +0000428static bool getFileLineInfoForCompileUnit(DWARFCompileUnit *CU,
429 const DWARFLineTable *LineTable,
430 uint64_t Address,
431 bool NeedsAbsoluteFilePath,
432 std::string &FileName,
433 uint32_t &Line, uint32_t &Column) {
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000434 if (CU == 0 || LineTable == 0)
Alexey Samsonov45be7932012-08-30 07:49:50 +0000435 return false;
436 // Get the index of row we're looking for in the line table.
437 uint32_t RowIndex = LineTable->lookupAddress(Address);
438 if (RowIndex == -1U)
439 return false;
440 // Take file number and line/column from the row.
441 const DWARFDebugLine::Row &Row = LineTable->Rows[RowIndex];
442 if (!getFileNameForCompileUnit(CU, LineTable, Row.File,
443 NeedsAbsoluteFilePath, FileName))
444 return false;
445 Line = Row.Line;
446 Column = Row.Column;
447 return true;
448}
449
450DILineInfo DWARFContext::getLineInfoForAddress(uint64_t Address,
451 DILineInfoSpecifier Specifier) {
452 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
453 if (!CU)
Alexey Samsonovf4462fa2012-07-02 05:54:45 +0000454 return DILineInfo();
Alexey Samsonov45be7932012-08-30 07:49:50 +0000455 std::string FileName = "<invalid>";
456 std::string FunctionName = "<invalid>";
457 uint32_t Line = 0;
458 uint32_t Column = 0;
459 if (Specifier.needs(DILineInfoSpecifier::FunctionName)) {
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000460 // The address may correspond to instruction in some inlined function,
461 // so we have to build the chain of inlined functions and take the
462 // name of the topmost function in it.
Alexey Samsonov3211e612013-08-06 10:49:15 +0000463 const DWARFDebugInfoEntryInlinedChain &InlinedChain =
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000464 CU->getInlinedChainForAddress(Address);
Alexey Samsonov3211e612013-08-06 10:49:15 +0000465 if (InlinedChain.DIEs.size() > 0) {
466 const DWARFDebugInfoEntryMinimal &TopFunctionDIE = InlinedChain.DIEs[0];
David Blaikie07e22442013-09-23 22:44:40 +0000467 if (const char *Name = TopFunctionDIE.getSubroutineName(InlinedChain.U))
Alexey Samsonov45be7932012-08-30 07:49:50 +0000468 FunctionName = Name;
Alexey Samsonovb604ff22012-07-17 15:28:35 +0000469 }
Alexey Samsonovf4462fa2012-07-02 05:54:45 +0000470 }
Alexey Samsonov45be7932012-08-30 07:49:50 +0000471 if (Specifier.needs(DILineInfoSpecifier::FileLineInfo)) {
Eric Christopher494109b2012-10-16 23:46:25 +0000472 const DWARFLineTable *LineTable = getLineTableForCompileUnit(CU);
Alexey Samsonov45be7932012-08-30 07:49:50 +0000473 const bool NeedsAbsoluteFilePath =
474 Specifier.needs(DILineInfoSpecifier::AbsoluteFilePath);
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000475 getFileLineInfoForCompileUnit(CU, LineTable, Address,
476 NeedsAbsoluteFilePath,
Alexey Samsonov45be7932012-08-30 07:49:50 +0000477 FileName, Line, Column);
Alexey Samsonovf4462fa2012-07-02 05:54:45 +0000478 }
Alexey Samsonov45be7932012-08-30 07:49:50 +0000479 return DILineInfo(StringRef(FileName), StringRef(FunctionName),
480 Line, Column);
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000481}
David Blaikiea379b1812011-12-20 02:50:00 +0000482
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000483DILineInfoTable DWARFContext::getLineInfoForAddressRange(uint64_t Address,
484 uint64_t Size,
485 DILineInfoSpecifier Specifier) {
486 DILineInfoTable Lines;
487 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
488 if (!CU)
489 return Lines;
490
491 std::string FunctionName = "<invalid>";
492 if (Specifier.needs(DILineInfoSpecifier::FunctionName)) {
493 // The address may correspond to instruction in some inlined function,
494 // so we have to build the chain of inlined functions and take the
495 // name of the topmost function in it.
Alexey Samsonov3211e612013-08-06 10:49:15 +0000496 const DWARFDebugInfoEntryInlinedChain &InlinedChain =
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000497 CU->getInlinedChainForAddress(Address);
Alexey Samsonov3211e612013-08-06 10:49:15 +0000498 if (InlinedChain.DIEs.size() > 0) {
499 const DWARFDebugInfoEntryMinimal &TopFunctionDIE = InlinedChain.DIEs[0];
David Blaikie07e22442013-09-23 22:44:40 +0000500 if (const char *Name = TopFunctionDIE.getSubroutineName(InlinedChain.U))
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000501 FunctionName = Name;
502 }
503 }
504
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000505 // If the Specifier says we don't need FileLineInfo, just
506 // return the top-most function at the starting address.
507 if (!Specifier.needs(DILineInfoSpecifier::FileLineInfo)) {
David Blaikieba860d72013-09-22 17:01:50 +0000508 Lines.push_back(
509 std::make_pair(Address, DILineInfo("<invalid>", FunctionName, 0, 0)));
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000510 return Lines;
511 }
512
513 const DWARFLineTable *LineTable = getLineTableForCompileUnit(CU);
514 const bool NeedsAbsoluteFilePath =
515 Specifier.needs(DILineInfoSpecifier::AbsoluteFilePath);
516
517 // Get the index of row we're looking for in the line table.
518 std::vector<uint32_t> RowVector;
519 if (!LineTable->lookupAddressRange(Address, Size, RowVector))
520 return Lines;
521
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000522 for (uint32_t RowIndex : RowVector) {
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000523 // Take file number and line/column from the row.
524 const DWARFDebugLine::Row &Row = LineTable->Rows[RowIndex];
525 std::string FileName = "<invalid>";
526 getFileNameForCompileUnit(CU, LineTable, Row.File,
527 NeedsAbsoluteFilePath, FileName);
David Blaikieba860d72013-09-22 17:01:50 +0000528 Lines.push_back(std::make_pair(
529 Row.Address, DILineInfo(FileName, FunctionName, Row.Line, Row.Column)));
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000530 }
531
532 return Lines;
533}
534
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000535DIInliningInfo DWARFContext::getInliningInfoForAddress(uint64_t Address,
536 DILineInfoSpecifier Specifier) {
537 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
538 if (!CU)
539 return DIInliningInfo();
540
Alexey Samsonov3211e612013-08-06 10:49:15 +0000541 const DWARFDebugInfoEntryInlinedChain &InlinedChain =
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000542 CU->getInlinedChainForAddress(Address);
Alexey Samsonov3211e612013-08-06 10:49:15 +0000543 if (InlinedChain.DIEs.size() == 0)
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000544 return DIInliningInfo();
545
546 DIInliningInfo InliningInfo;
547 uint32_t CallFile = 0, CallLine = 0, CallColumn = 0;
Eric Christopher494109b2012-10-16 23:46:25 +0000548 const DWARFLineTable *LineTable = 0;
Alexey Samsonov3211e612013-08-06 10:49:15 +0000549 for (uint32_t i = 0, n = InlinedChain.DIEs.size(); i != n; i++) {
550 const DWARFDebugInfoEntryMinimal &FunctionDIE = InlinedChain.DIEs[i];
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000551 std::string FileName = "<invalid>";
552 std::string FunctionName = "<invalid>";
553 uint32_t Line = 0;
554 uint32_t Column = 0;
555 // Get function name if necessary.
556 if (Specifier.needs(DILineInfoSpecifier::FunctionName)) {
David Blaikie07e22442013-09-23 22:44:40 +0000557 if (const char *Name = FunctionDIE.getSubroutineName(InlinedChain.U))
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000558 FunctionName = Name;
559 }
560 if (Specifier.needs(DILineInfoSpecifier::FileLineInfo)) {
561 const bool NeedsAbsoluteFilePath =
562 Specifier.needs(DILineInfoSpecifier::AbsoluteFilePath);
563 if (i == 0) {
564 // For the topmost frame, initialize the line table of this
565 // compile unit and fetch file/line info from it.
566 LineTable = getLineTableForCompileUnit(CU);
567 // For the topmost routine, get file/line info from line table.
568 getFileLineInfoForCompileUnit(CU, LineTable, Address,
569 NeedsAbsoluteFilePath,
570 FileName, Line, Column);
571 } else {
572 // Otherwise, use call file, call line and call column from
573 // previous DIE in inlined chain.
574 getFileNameForCompileUnit(CU, LineTable, CallFile,
575 NeedsAbsoluteFilePath, FileName);
576 Line = CallLine;
577 Column = CallColumn;
578 }
579 // Get call file/line/column of a current DIE.
580 if (i + 1 < n) {
David Blaikie07e22442013-09-23 22:44:40 +0000581 FunctionDIE.getCallerFrame(InlinedChain.U, CallFile, CallLine,
Alexey Samsonov3211e612013-08-06 10:49:15 +0000582 CallColumn);
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000583 }
584 }
585 DILineInfo Frame(StringRef(FileName), StringRef(FunctionName),
586 Line, Column);
587 InliningInfo.addFrame(Frame);
588 }
589 return InliningInfo;
590}
591
Alexey Samsonov068fc8a2013-04-23 10:17:34 +0000592static bool consumeCompressedDebugSectionHeader(StringRef &data,
593 uint64_t &OriginalSize) {
594 // Consume "ZLIB" prefix.
595 if (!data.startswith("ZLIB"))
596 return false;
597 data = data.substr(4);
598 // Consume uncompressed section size (big-endian 8 bytes).
599 DataExtractor extractor(data, false, 8);
600 uint32_t Offset = 0;
601 OriginalSize = extractor.getU64(&Offset);
602 if (Offset == 0)
603 return false;
604 data = data.substr(Offset);
605 return true;
606}
607
Alexey Samsonov063eb3f2014-03-13 13:52:54 +0000608DWARFContextInMemory::DWARFContextInMemory(object::ObjectFile *Obj)
609 : IsLittleEndian(Obj->isLittleEndian()),
610 AddressSize(Obj->getBytesInAddress()) {
611 for (const SectionRef &Section : Obj->sections()) {
Eric Christopher7370b552012-11-12 21:40:38 +0000612 StringRef name;
Alexey Samsonov063eb3f2014-03-13 13:52:54 +0000613 Section.getName(name);
Eric Christopher7370b552012-11-12 21:40:38 +0000614 StringRef data;
Alexey Samsonov063eb3f2014-03-13 13:52:54 +0000615 Section.getContents(data);
Eric Christopher7370b552012-11-12 21:40:38 +0000616
Eric Christopher7370b552012-11-12 21:40:38 +0000617 name = name.substr(name.find_first_not_of("._")); // Skip . and _ prefixes.
Alexey Samsonov3d0e3ed2013-04-17 14:27:04 +0000618
Alexey Samsonov068fc8a2013-04-23 10:17:34 +0000619 // Check if debug info section is compressed with zlib.
620 if (name.startswith("zdebug_")) {
621 uint64_t OriginalSize;
622 if (!zlib::isAvailable() ||
623 !consumeCompressedDebugSectionHeader(data, OriginalSize))
624 continue;
Ahmed Charles56440fd2014-03-06 05:51:42 +0000625 std::unique_ptr<MemoryBuffer> UncompressedSection;
Alexey Samsonov068fc8a2013-04-23 10:17:34 +0000626 if (zlib::uncompress(data, UncompressedSection, OriginalSize) !=
627 zlib::StatusOK)
628 continue;
629 // Make data point to uncompressed section contents and save its contents.
630 name = name.substr(1);
631 data = UncompressedSection->getBuffer();
Alexey Samsonov96dc29c2014-03-13 08:19:59 +0000632 UncompressedSections.push_back(std::move(UncompressedSection));
Alexey Samsonov068fc8a2013-04-23 10:17:34 +0000633 }
634
Alexey Samsonov063eb3f2014-03-13 13:52:54 +0000635 StringRef *SectionData =
Eric Christopher4c7e6ba2013-09-25 23:02:41 +0000636 StringSwitch<StringRef *>(name)
637 .Case("debug_info", &InfoSection.Data)
638 .Case("debug_abbrev", &AbbrevSection)
639 .Case("debug_loc", &LocSection.Data)
640 .Case("debug_line", &LineSection.Data)
641 .Case("debug_aranges", &ARangeSection)
642 .Case("debug_frame", &DebugFrameSection)
643 .Case("debug_str", &StringSection)
644 .Case("debug_ranges", &RangeSection)
645 .Case("debug_pubnames", &PubNamesSection)
646 .Case("debug_pubtypes", &PubTypesSection)
647 .Case("debug_gnu_pubnames", &GnuPubNamesSection)
648 .Case("debug_gnu_pubtypes", &GnuPubTypesSection)
649 .Case("debug_info.dwo", &InfoDWOSection.Data)
650 .Case("debug_abbrev.dwo", &AbbrevDWOSection)
David Blaikie1d4736e2014-02-24 23:58:54 +0000651 .Case("debug_line.dwo", &LineDWOSection.Data)
Eric Christopher4c7e6ba2013-09-25 23:02:41 +0000652 .Case("debug_str.dwo", &StringDWOSection)
653 .Case("debug_str_offsets.dwo", &StringOffsetDWOSection)
654 .Case("debug_addr", &AddrSection)
655 // Any more debug info sections go here.
656 .Default(0);
Alexey Samsonov063eb3f2014-03-13 13:52:54 +0000657 if (SectionData) {
658 *SectionData = data;
Rafael Espindola4f60a382013-05-30 03:05:14 +0000659 if (name == "debug_ranges") {
660 // FIXME: Use the other dwo range section when we emit it.
661 RangeDWOSection = data;
662 }
David Blaikie03c089c2013-09-23 22:44:47 +0000663 } else if (name == "debug_types") {
David Blaikie427e4352013-09-23 23:39:55 +0000664 // Find debug_types data by section rather than name as there are
665 // multiple, comdat grouped, debug_types sections.
Alexey Samsonov063eb3f2014-03-13 13:52:54 +0000666 TypesSections[Section].Data = data;
David Blaikie92d9d622014-01-09 05:08:24 +0000667 } else if (name == "debug_types.dwo") {
Alexey Samsonov063eb3f2014-03-13 13:52:54 +0000668 TypesDWOSections[Section].Data = data;
Eric Christopherda4b2192013-01-02 23:52:13 +0000669 }
Eric Christopher7370b552012-11-12 21:40:38 +0000670
Alexey Samsonov063eb3f2014-03-13 13:52:54 +0000671 section_iterator RelocatedSection = Section.getRelocatedSection();
Rafael Espindolab5155a52014-02-10 20:24:04 +0000672 if (RelocatedSection == Obj->section_end())
Rafael Espindola4f60a382013-05-30 03:05:14 +0000673 continue;
674
675 StringRef RelSecName;
676 RelocatedSection->getName(RelSecName);
677 RelSecName = RelSecName.substr(
678 RelSecName.find_first_not_of("._")); // Skip . and _ prefixes.
679
Andrew Kaylord55d7012013-01-25 22:50:58 +0000680 // TODO: Add support for relocations in other sections as needed.
681 // Record relocations for the debug_info and debug_line sections.
Rafael Espindola4f60a382013-05-30 03:05:14 +0000682 RelocAddrMap *Map = StringSwitch<RelocAddrMap*>(RelSecName)
David Blaikie1b5ee5d2013-09-23 17:42:01 +0000683 .Case("debug_info", &InfoSection.Relocs)
684 .Case("debug_loc", &LocSection.Relocs)
685 .Case("debug_info.dwo", &InfoDWOSection.Relocs)
686 .Case("debug_line", &LineSection.Relocs)
Alexey Samsonov3d0e3ed2013-04-17 14:27:04 +0000687 .Default(0);
David Blaikie03c089c2013-09-23 22:44:47 +0000688 if (!Map) {
David Blaikie427e4352013-09-23 23:39:55 +0000689 // Find debug_types relocs by section rather than name as there are
690 // multiple, comdat grouped, debug_types sections.
David Blaikie92d9d622014-01-09 05:08:24 +0000691 if (RelSecName == "debug_types")
692 Map = &TypesSections[*RelocatedSection].Relocs;
693 else if (RelSecName == "debug_types.dwo")
694 Map = &TypesDWOSections[*RelocatedSection].Relocs;
695 else
696 continue;
David Blaikie03c089c2013-09-23 22:44:47 +0000697 }
Eric Christopher7370b552012-11-12 21:40:38 +0000698
Alexey Samsonov063eb3f2014-03-13 13:52:54 +0000699 if (Section.relocation_begin() != Section.relocation_end()) {
Eric Christopher7370b552012-11-12 21:40:38 +0000700 uint64_t SectionSize;
Rafael Espindola4f60a382013-05-30 03:05:14 +0000701 RelocatedSection->getSize(SectionSize);
Alexey Samsonov063eb3f2014-03-13 13:52:54 +0000702 for (object::relocation_iterator reloc_i = Section.relocation_begin(),
703 reloc_e = Section.relocation_end();
Rafael Espindola5e812af2014-01-30 02:49:50 +0000704 reloc_i != reloc_e; ++reloc_i) {
Eric Christopher7370b552012-11-12 21:40:38 +0000705 uint64_t Address;
Rafael Espindola1e483872013-04-25 12:28:45 +0000706 reloc_i->getOffset(Address);
Eric Christopher7370b552012-11-12 21:40:38 +0000707 uint64_t Type;
708 reloc_i->getType(Type);
Andrew Kaylord55d7012013-01-25 22:50:58 +0000709 uint64_t SymAddr = 0;
710 // ELF relocations may need the symbol address
711 if (Obj->isELF()) {
Rafael Espindola806f0062013-06-05 01:33:53 +0000712 object::symbol_iterator Sym = reloc_i->getSymbol();
713 Sym->getAddress(SymAddr);
Andrew Kaylord55d7012013-01-25 22:50:58 +0000714 }
Eric Christopher7370b552012-11-12 21:40:38 +0000715
716 object::RelocVisitor V(Obj->getFileFormatName());
717 // The section address is always 0 for debug sections.
Andrew Kaylord55d7012013-01-25 22:50:58 +0000718 object::RelocToApply R(V.visit(Type, *reloc_i, 0, SymAddr));
Eric Christopher7370b552012-11-12 21:40:38 +0000719 if (V.error()) {
720 SmallString<32> Name;
721 error_code ec(reloc_i->getTypeName(Name));
722 if (ec) {
723 errs() << "Aaaaaa! Nameless relocation! Aaaaaa!\n";
724 }
725 errs() << "error: failed to compute relocation: "
726 << Name << "\n";
727 continue;
728 }
729
730 if (Address + R.Width > SectionSize) {
731 errs() << "error: " << R.Width << "-byte relocation starting "
732 << Address << " bytes into section " << name << " which is "
733 << SectionSize << " bytes long.\n";
734 continue;
735 }
736 if (R.Width > 8) {
737 errs() << "error: can't handle a relocation of more than 8 bytes at "
738 "a time.\n";
739 continue;
740 }
741 DEBUG(dbgs() << "Writing " << format("%p", R.Value)
742 << " at " << format("%p", Address)
743 << " with width " << format("%d", R.Width)
744 << "\n");
Eric Christopherda4b2192013-01-02 23:52:13 +0000745 Map->insert(std::make_pair(Address, std::make_pair(R.Width, R.Value)));
Eric Christopher7370b552012-11-12 21:40:38 +0000746 }
747 }
748 }
749}
750
David Blaikiea379b1812011-12-20 02:50:00 +0000751void DWARFContextInMemory::anchor() { }