blob: 3f03e63ce03ab0b714ff21f09316e903b6c32a6a [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
Zachary Turner82af9432015-01-30 18:07:45 +000010#include "llvm/DebugInfo/DWARF/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"
Zachary Turner82af9432015-01-30 18:07:45 +000013#include "llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h"
14#include "llvm/DebugInfo/DWARF/DWARFDebugArangeSet.h"
David Blaikie65a8efe2015-11-11 19:28:21 +000015#include "llvm/DebugInfo/DWARF/DWARFUnitIndex.h"
Alexey Samsonov068fc8a2013-04-23 10:17:34 +000016#include "llvm/Support/Compression.h"
Benjamin Kramer6dda0322011-09-15 18:02:20 +000017#include "llvm/Support/Dwarf.h"
George Rimar401e4e52016-05-24 12:48:46 +000018#include "llvm/Support/ELF.h"
Benjamin Kramer07d4b1c2011-09-15 16:57:13 +000019#include "llvm/Support/Format.h"
Alexey Samsonove16e16a2012-07-19 07:03:58 +000020#include "llvm/Support/Path.h"
Benjamin Kramera6002fd2011-09-14 01:09:52 +000021#include "llvm/Support/raw_ostream.h"
Benjamin Kramer2602ca62011-09-15 20:43:22 +000022#include <algorithm>
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000023using namespace llvm;
Benjamin Kramer6dda0322011-09-15 18:02:20 +000024using namespace dwarf;
Rafael Espindola4f60a382013-05-30 03:05:14 +000025using namespace object;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000026
Chandler Carruthe96dd892014-04-21 22:55:11 +000027#define DEBUG_TYPE "dwarf"
28
Eric Christopher494109b2012-10-16 23:46:25 +000029typedef DWARFDebugLine::LineTable DWARFLineTable;
Alexey Samsonovdce67342014-05-15 21:24:32 +000030typedef DILineInfoSpecifier::FileLineInfoKind FileLineInfoKind;
31typedef DILineInfoSpecifier::FunctionNameKind FunctionNameKind;
Eric Christopher494109b2012-10-16 23:46:25 +000032
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
Frederic Riss7c500472014-11-14 19:30:08 +000064static void dumpAccelSection(raw_ostream &OS, StringRef Name,
65 const DWARFSection& Section, StringRef StringSection,
66 bool LittleEndian) {
67 DataExtractor AccelSection(Section.Data, LittleEndian, 0);
Frederic Risse837ec22014-11-14 16:15:53 +000068 DataExtractor StrData(StringSection, LittleEndian, 0);
69 OS << "\n." << Name << " contents:\n";
Frederic Riss7c500472014-11-14 19:30:08 +000070 DWARFAcceleratorTable Accel(AccelSection, StrData, Section.Relocs);
Frederic Risse837ec22014-11-14 16:15:53 +000071 if (!Accel.extract())
72 return;
73 Accel.dump(OS);
74}
75
Igor Laevsky03a670c2016-01-26 15:09:42 +000076void DWARFContext::dump(raw_ostream &OS, DIDumpType DumpType, bool DumpEH) {
Eli Bendersky7a94daa2013-01-25 20:26:43 +000077 if (DumpType == DIDT_All || DumpType == DIDT_Abbrev) {
78 OS << ".debug_abbrev contents:\n";
79 getDebugAbbrev()->dump(OS);
80 }
Benjamin Kramera6002fd2011-09-14 01:09:52 +000081
David Blaikie66865d62014-01-09 00:13:35 +000082 if (DumpType == DIDT_All || DumpType == DIDT_AbbrevDwo)
83 if (const DWARFDebugAbbrev *D = getDebugAbbrevDWO()) {
David Blaikie622dce42014-01-08 23:29:59 +000084 OS << "\n.debug_abbrev.dwo contents:\n";
David Blaikie66865d62014-01-09 00:13:35 +000085 D->dump(OS);
David Blaikie622dce42014-01-08 23:29:59 +000086 }
David Blaikie622dce42014-01-08 23:29:59 +000087
Eli Bendersky7a94daa2013-01-25 20:26:43 +000088 if (DumpType == DIDT_All || DumpType == DIDT_Info) {
89 OS << "\n.debug_info contents:\n";
Alexey Samsonov1eabf982014-03-13 07:52:54 +000090 for (const auto &CU : compile_units())
91 CU->dump(OS);
Eli Bendersky7a94daa2013-01-25 20:26:43 +000092 }
Benjamin Kramera6002fd2011-09-14 01:09:52 +000093
David Blaikie66865d62014-01-09 00:13:35 +000094 if ((DumpType == DIDT_All || DumpType == DIDT_InfoDwo) &&
95 getNumDWOCompileUnits()) {
96 OS << "\n.debug_info.dwo contents:\n";
Alexey Samsonov1eabf982014-03-13 07:52:54 +000097 for (const auto &DWOCU : dwo_compile_units())
98 DWOCU->dump(OS);
David Blaikie66865d62014-01-09 00:13:35 +000099 }
David Blaikie622dce42014-01-08 23:29:59 +0000100
David Blaikie66865d62014-01-09 00:13:35 +0000101 if ((DumpType == DIDT_All || DumpType == DIDT_Types) && getNumTypeUnits()) {
David Blaikie03c089c2013-09-23 22:44:47 +0000102 OS << "\n.debug_types contents:\n";
Frederic Riss312a02e2014-09-29 13:56:39 +0000103 for (const auto &TUS : type_unit_sections())
104 for (const auto &TU : TUS)
105 TU->dump(OS);
David Blaikie03c089c2013-09-23 22:44:47 +0000106 }
107
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000108 if ((DumpType == DIDT_All || DumpType == DIDT_TypesDwo) &&
109 getNumDWOTypeUnits()) {
110 OS << "\n.debug_types.dwo contents:\n";
Frederic Riss312a02e2014-09-29 13:56:39 +0000111 for (const auto &DWOTUS : dwo_type_unit_sections())
112 for (const auto &DWOTU : DWOTUS)
113 DWOTU->dump(OS);
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000114 }
David Blaikie92d9d622014-01-09 05:08:24 +0000115
David Blaikie18e73502013-06-19 21:37:13 +0000116 if (DumpType == DIDT_All || DumpType == DIDT_Loc) {
David Blaikie03c089c2013-09-23 22:44:47 +0000117 OS << "\n.debug_loc contents:\n";
David Blaikie18e73502013-06-19 21:37:13 +0000118 getDebugLoc()->dump(OS);
119 }
120
David Blaikie9c550ac2014-03-25 01:44:02 +0000121 if (DumpType == DIDT_All || DumpType == DIDT_LocDwo) {
122 OS << "\n.debug_loc.dwo contents:\n";
123 getDebugLocDWO()->dump(OS);
124 }
125
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000126 if (DumpType == DIDT_All || DumpType == DIDT_Frames) {
127 OS << "\n.debug_frame contents:\n";
128 getDebugFrame()->dump(OS);
Igor Laevsky03a670c2016-01-26 15:09:42 +0000129 if (DumpEH) {
130 OS << "\n.eh_frame contents:\n";
131 getEHFrame()->dump(OS);
132 }
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000133 }
134
Amjad Aboude59cc3e2015-11-12 09:38:54 +0000135 if (DumpType == DIDT_All || DumpType == DIDT_Macro) {
136 OS << "\n.debug_macinfo contents:\n";
137 getDebugMacro()->dump(OS);
138 }
139
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000140 uint32_t offset = 0;
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000141 if (DumpType == DIDT_All || DumpType == DIDT_Aranges) {
142 OS << "\n.debug_aranges contents:\n";
143 DataExtractor arangesData(getARangeSection(), isLittleEndian(), 0);
144 DWARFDebugArangeSet set;
145 while (set.extract(arangesData, &offset))
146 set.dump(OS);
147 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000148
Alexey Samsonov034e57a2012-08-27 07:17:47 +0000149 uint8_t savedAddressByteSize = 0;
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000150 if (DumpType == DIDT_All || DumpType == DIDT_Line) {
151 OS << "\n.debug_line contents:\n";
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000152 for (const auto &CU : compile_units()) {
153 savedAddressByteSize = CU->getAddressByteSize();
Alexey Samsonov7a18c062015-05-19 21:54:32 +0000154 const auto *CUDIE = CU->getUnitDIE();
155 if (CUDIE == nullptr)
156 continue;
157 unsigned stmtOffset = CUDIE->getAttributeValueAsSectionOffset(
158 CU.get(), DW_AT_stmt_list, -1U);
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000159 if (stmtOffset != -1U) {
David Blaikie1b5ee5d2013-09-23 17:42:01 +0000160 DataExtractor lineData(getLineSection().Data, isLittleEndian(),
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000161 savedAddressByteSize);
Alexey Samsonov110d5952014-04-30 00:09:19 +0000162 DWARFDebugLine::LineTable LineTable;
163 LineTable.parse(lineData, &getLineSection().Relocs, &stmtOffset);
164 LineTable.dump(OS);
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000165 }
Benjamin Kramer6dda0322011-09-15 18:02:20 +0000166 }
167 }
Benjamin Kramer07d4b1c2011-09-15 16:57:13 +0000168
David Blaikie65a8efe2015-11-11 19:28:21 +0000169 if (DumpType == DIDT_All || DumpType == DIDT_CUIndex) {
170 OS << "\n.debug_cu_index contents:\n";
David Blaikieb073cb92015-12-02 06:21:34 +0000171 getCUIndex().dump(OS);
David Blaikie65a8efe2015-11-11 19:28:21 +0000172 }
173
David Blaikie51c40282015-11-11 19:40:49 +0000174 if (DumpType == DIDT_All || DumpType == DIDT_TUIndex) {
175 OS << "\n.debug_tu_index contents:\n";
David Blaikieb073cb92015-12-02 06:21:34 +0000176 getTUIndex().dump(OS);
David Blaikie51c40282015-11-11 19:40:49 +0000177 }
178
David Blaikie1d4736e2014-02-24 23:58:54 +0000179 if (DumpType == DIDT_All || DumpType == DIDT_LineDwo) {
180 OS << "\n.debug_line.dwo contents:\n";
181 unsigned stmtOffset = 0;
182 DataExtractor lineData(getLineDWOSection().Data, isLittleEndian(),
183 savedAddressByteSize);
Alexey Samsonov110d5952014-04-30 00:09:19 +0000184 DWARFDebugLine::LineTable LineTable;
185 while (LineTable.Prologue.parse(lineData, &stmtOffset)) {
186 LineTable.dump(OS);
187 LineTable.clear();
188 }
David Blaikie1d4736e2014-02-24 23:58:54 +0000189 }
190
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000191 if (DumpType == DIDT_All || DumpType == DIDT_Str) {
192 OS << "\n.debug_str contents:\n";
193 DataExtractor strData(getStringSection(), isLittleEndian(), 0);
194 offset = 0;
195 uint32_t strOffset = 0;
196 while (const char *s = strData.getCStr(&offset)) {
197 OS << format("0x%8.8x: \"%s\"\n", strOffset, s);
198 strOffset = offset;
199 }
Benjamin Kramer07d4b1c2011-09-15 16:57:13 +0000200 }
Alexey Samsonov034e57a2012-08-27 07:17:47 +0000201
David Blaikie66865d62014-01-09 00:13:35 +0000202 if ((DumpType == DIDT_All || DumpType == DIDT_StrDwo) &&
203 !getStringDWOSection().empty()) {
204 OS << "\n.debug_str.dwo contents:\n";
205 DataExtractor strDWOData(getStringDWOSection(), isLittleEndian(), 0);
206 offset = 0;
207 uint32_t strDWOOffset = 0;
208 while (const char *s = strDWOData.getCStr(&offset)) {
209 OS << format("0x%8.8x: \"%s\"\n", strDWOOffset, s);
210 strDWOOffset = offset;
David Blaikie622dce42014-01-08 23:29:59 +0000211 }
David Blaikie66865d62014-01-09 00:13:35 +0000212 }
David Blaikie622dce42014-01-08 23:29:59 +0000213
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000214 if (DumpType == DIDT_All || DumpType == DIDT_Ranges) {
215 OS << "\n.debug_ranges contents:\n";
216 // In fact, different compile units may have different address byte
217 // sizes, but for simplicity we just use the address byte size of the last
218 // compile unit (there is no easy and fast way to associate address range
219 // list and the compile unit it describes).
220 DataExtractor rangesData(getRangeSection(), isLittleEndian(),
221 savedAddressByteSize);
222 offset = 0;
223 DWARFDebugRangeList rangeList;
224 while (rangeList.extract(rangesData, &offset))
225 rangeList.dump(OS);
Eric Christopherda4b2192013-01-02 23:52:13 +0000226 }
Eric Christopher962c9082013-01-15 23:56:56 +0000227
Eric Christopher0de53592013-09-25 23:02:36 +0000228 if (DumpType == DIDT_All || DumpType == DIDT_Pubnames)
229 dumpPubSection(OS, "debug_pubnames", getPubNamesSection(),
230 isLittleEndian(), false);
Krzysztof Parzyszek97438dc2013-02-12 16:20:28 +0000231
Eric Christopher4c7e6ba2013-09-25 23:02:41 +0000232 if (DumpType == DIDT_All || DumpType == DIDT_Pubtypes)
233 dumpPubSection(OS, "debug_pubtypes", getPubTypesSection(),
234 isLittleEndian(), false);
235
David Blaikieecd21ff2013-09-24 19:50:00 +0000236 if (DumpType == DIDT_All || DumpType == DIDT_GnuPubnames)
237 dumpPubSection(OS, "debug_gnu_pubnames", getGnuPubNamesSection(),
Eric Christopher0de53592013-09-25 23:02:36 +0000238 isLittleEndian(), true /* GnuStyle */);
David Blaikieecd21ff2013-09-24 19:50:00 +0000239
240 if (DumpType == DIDT_All || DumpType == DIDT_GnuPubtypes)
241 dumpPubSection(OS, "debug_gnu_pubtypes", getGnuPubTypesSection(),
Eric Christopher0de53592013-09-25 23:02:36 +0000242 isLittleEndian(), true /* GnuStyle */);
David Blaikie404d3042013-09-19 23:01:29 +0000243
David Blaikie66865d62014-01-09 00:13:35 +0000244 if ((DumpType == DIDT_All || DumpType == DIDT_StrOffsetsDwo) &&
245 !getStringOffsetDWOSection().empty()) {
246 OS << "\n.debug_str_offsets.dwo contents:\n";
247 DataExtractor strOffsetExt(getStringOffsetDWOSection(), isLittleEndian(),
248 0);
249 offset = 0;
250 uint64_t size = getStringOffsetDWOSection().size();
251 while (offset < size) {
252 OS << format("0x%8.8x: ", offset);
253 OS << format("%8.8x\n", strOffsetExt.getU32(&offset));
Eric Christopher92f3c0b2013-05-06 17:50:42 +0000254 }
David Blaikie66865d62014-01-09 00:13:35 +0000255 }
Frederic Risse837ec22014-11-14 16:15:53 +0000256
257 if (DumpType == DIDT_All || DumpType == DIDT_AppleNames)
258 dumpAccelSection(OS, "apple_names", getAppleNamesSection(),
259 getStringSection(), isLittleEndian());
260
261 if (DumpType == DIDT_All || DumpType == DIDT_AppleTypes)
262 dumpAccelSection(OS, "apple_types", getAppleTypesSection(),
263 getStringSection(), isLittleEndian());
264
265 if (DumpType == DIDT_All || DumpType == DIDT_AppleNamespaces)
266 dumpAccelSection(OS, "apple_namespaces", getAppleNamespacesSection(),
267 getStringSection(), isLittleEndian());
268
269 if (DumpType == DIDT_All || DumpType == DIDT_AppleObjC)
270 dumpAccelSection(OS, "apple_objc", getAppleObjCSection(),
271 getStringSection(), isLittleEndian());
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000272}
273
David Blaikie82641be2015-11-17 00:39:55 +0000274const DWARFUnitIndex &DWARFContext::getCUIndex() {
275 if (CUIndex)
276 return *CUIndex;
277
278 DataExtractor CUIndexData(getCUIndexSection(), isLittleEndian(), 0);
279
David Blaikieb073cb92015-12-02 06:21:34 +0000280 CUIndex = llvm::make_unique<DWARFUnitIndex>(DW_SECT_INFO);
David Blaikie82641be2015-11-17 00:39:55 +0000281 CUIndex->parse(CUIndexData);
282 return *CUIndex;
283}
284
285const DWARFUnitIndex &DWARFContext::getTUIndex() {
286 if (TUIndex)
287 return *TUIndex;
288
289 DataExtractor TUIndexData(getTUIndexSection(), isLittleEndian(), 0);
290
David Blaikieb073cb92015-12-02 06:21:34 +0000291 TUIndex = llvm::make_unique<DWARFUnitIndex>(DW_SECT_TYPES);
David Blaikie82641be2015-11-17 00:39:55 +0000292 TUIndex->parse(TUIndexData);
293 return *TUIndex;
294}
295
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000296const DWARFDebugAbbrev *DWARFContext::getDebugAbbrev() {
297 if (Abbrev)
298 return Abbrev.get();
299
300 DataExtractor abbrData(getAbbrevSection(), isLittleEndian(), 0);
301
302 Abbrev.reset(new DWARFDebugAbbrev());
Alexey Samsonov4316df52014-04-25 21:10:56 +0000303 Abbrev->extract(abbrData);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000304 return Abbrev.get();
305}
306
Eric Christopherda4b2192013-01-02 23:52:13 +0000307const DWARFDebugAbbrev *DWARFContext::getDebugAbbrevDWO() {
308 if (AbbrevDWO)
309 return AbbrevDWO.get();
310
311 DataExtractor abbrData(getAbbrevDWOSection(), isLittleEndian(), 0);
312 AbbrevDWO.reset(new DWARFDebugAbbrev());
Alexey Samsonov4316df52014-04-25 21:10:56 +0000313 AbbrevDWO->extract(abbrData);
Eric Christopherda4b2192013-01-02 23:52:13 +0000314 return AbbrevDWO.get();
315}
316
David Blaikie18e73502013-06-19 21:37:13 +0000317const DWARFDebugLoc *DWARFContext::getDebugLoc() {
318 if (Loc)
319 return Loc.get();
320
David Blaikie1b5ee5d2013-09-23 17:42:01 +0000321 DataExtractor LocData(getLocSection().Data, isLittleEndian(), 0);
322 Loc.reset(new DWARFDebugLoc(getLocSection().Relocs));
David Blaikie18e73502013-06-19 21:37:13 +0000323 // assume all compile units have the same address byte size
324 if (getNumCompileUnits())
325 Loc->parse(LocData, getCompileUnitAtIndex(0)->getAddressByteSize());
326 return Loc.get();
327}
328
David Blaikie9c550ac2014-03-25 01:44:02 +0000329const DWARFDebugLocDWO *DWARFContext::getDebugLocDWO() {
330 if (LocDWO)
331 return LocDWO.get();
332
333 DataExtractor LocData(getLocDWOSection().Data, isLittleEndian(), 0);
334 LocDWO.reset(new DWARFDebugLocDWO());
335 LocDWO->parse(LocData);
336 return LocDWO.get();
337}
338
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000339const DWARFDebugAranges *DWARFContext::getDebugAranges() {
340 if (Aranges)
341 return Aranges.get();
342
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000343 Aranges.reset(new DWARFDebugAranges());
Alexey Samsonova1694c12012-11-16 08:36:25 +0000344 Aranges->generate(this);
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000345 return Aranges.get();
346}
347
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000348const DWARFDebugFrame *DWARFContext::getDebugFrame() {
349 if (DebugFrame)
350 return DebugFrame.get();
351
352 // There's a "bug" in the DWARFv3 standard with respect to the target address
353 // size within debug frame sections. While DWARF is supposed to be independent
354 // of its container, FDEs have fields with size being "target address size",
355 // which isn't specified in DWARF in general. It's only specified for CUs, but
356 // .eh_frame can appear without a .debug_info section. Follow the example of
357 // other tools (libdwarf) and extract this from the container (ObjectFile
358 // provides this information). This problem is fixed in DWARFv4
359 // See this dwarf-discuss discussion for more details:
360 // http://lists.dwarfstd.org/htdig.cgi/dwarf-discuss-dwarfstd.org/2011-December/001173.html
361 DataExtractor debugFrameData(getDebugFrameSection(), isLittleEndian(),
362 getAddressSize());
Igor Laevsky03a670c2016-01-26 15:09:42 +0000363 DebugFrame.reset(new DWARFDebugFrame(false /* IsEH */));
364 DebugFrame->parse(debugFrameData);
365 return DebugFrame.get();
366}
367
368const DWARFDebugFrame *DWARFContext::getEHFrame() {
369 if (EHFrame)
370 return EHFrame.get();
371
372 DataExtractor debugFrameData(getEHFrameSection(), isLittleEndian(),
373 getAddressSize());
374 DebugFrame.reset(new DWARFDebugFrame(true /* IsEH */));
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000375 DebugFrame->parse(debugFrameData);
376 return DebugFrame.get();
377}
378
Amjad Aboude59cc3e2015-11-12 09:38:54 +0000379const DWARFDebugMacro *DWARFContext::getDebugMacro() {
380 if (Macro)
381 return Macro.get();
382
383 DataExtractor MacinfoData(getMacinfoSection(), isLittleEndian(), 0);
384 Macro.reset(new DWARFDebugMacro());
385 Macro->parse(MacinfoData);
386 return Macro.get();
387}
388
Eric Christopher494109b2012-10-16 23:46:25 +0000389const DWARFLineTable *
Alexey Samsonov7a18c062015-05-19 21:54:32 +0000390DWARFContext::getLineTableForUnit(DWARFUnit *U) {
Benjamin Kramer679e1752011-09-15 20:43:18 +0000391 if (!Line)
David Blaikie1b5ee5d2013-09-23 17:42:01 +0000392 Line.reset(new DWARFDebugLine(&getLineSection().Relocs));
David Blaikiec4e2bed2015-11-17 21:08:05 +0000393
Alexey Samsonov7a18c062015-05-19 21:54:32 +0000394 const auto *UnitDIE = U->getUnitDIE();
395 if (UnitDIE == nullptr)
396 return nullptr;
David Blaikiec4e2bed2015-11-17 21:08:05 +0000397
Benjamin Kramer679e1752011-09-15 20:43:18 +0000398 unsigned stmtOffset =
Alexey Samsonov7a18c062015-05-19 21:54:32 +0000399 UnitDIE->getAttributeValueAsSectionOffset(U, DW_AT_stmt_list, -1U);
Benjamin Kramer679e1752011-09-15 20:43:18 +0000400 if (stmtOffset == -1U)
Craig Topper2617dcc2014-04-15 06:32:26 +0000401 return nullptr; // No line table for this compile unit.
Benjamin Kramer5acab502011-09-15 02:12:05 +0000402
David Blaikiec4e2bed2015-11-17 21:08:05 +0000403 stmtOffset += U->getLineTableOffset();
Benjamin Kramer679e1752011-09-15 20:43:18 +0000404 // See if the line table is cached.
Eric Christopher494109b2012-10-16 23:46:25 +0000405 if (const DWARFLineTable *lt = Line->getLineTable(stmtOffset))
Benjamin Kramer679e1752011-09-15 20:43:18 +0000406 return lt;
407
408 // We have to parse it first.
David Blaikiec4e2bed2015-11-17 21:08:05 +0000409 DataExtractor lineData(U->getLineSection(), isLittleEndian(),
Alexey Samsonov7a18c062015-05-19 21:54:32 +0000410 U->getAddressByteSize());
Benjamin Kramer679e1752011-09-15 20:43:18 +0000411 return Line->getOrParseLineTable(lineData, stmtOffset);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000412}
413
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000414void DWARFContext::parseCompileUnits() {
Alexey Samsonov8cd4c9d2014-10-08 00:07:53 +0000415 CUs.parse(*this, getInfoSection());
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000416}
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000417
David Blaikie03c089c2013-09-23 22:44:47 +0000418void DWARFContext::parseTypeUnits() {
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000419 if (!TUs.empty())
420 return;
421 for (const auto &I : getTypesSections()) {
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000422 TUs.emplace_back();
Alexey Samsonov8cd4c9d2014-10-08 00:07:53 +0000423 TUs.back().parse(*this, I.second);
David Blaikie03c089c2013-09-23 22:44:47 +0000424 }
425}
426
Eric Christopherda4b2192013-01-02 23:52:13 +0000427void DWARFContext::parseDWOCompileUnits() {
Alexey Samsonov8cd4c9d2014-10-08 00:07:53 +0000428 DWOCUs.parseDWO(*this, getInfoDWOSection());
Eric Christopherda4b2192013-01-02 23:52:13 +0000429}
430
David Blaikie92d9d622014-01-09 05:08:24 +0000431void DWARFContext::parseDWOTypeUnits() {
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000432 if (!DWOTUs.empty())
433 return;
434 for (const auto &I : getTypesDWOSections()) {
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000435 DWOTUs.emplace_back();
Alexey Samsonov8cd4c9d2014-10-08 00:07:53 +0000436 DWOTUs.back().parseDWO(*this, I.second);
David Blaikie92d9d622014-01-09 05:08:24 +0000437 }
438}
439
Alexey Samsonov45be7932012-08-30 07:49:50 +0000440DWARFCompileUnit *DWARFContext::getCompileUnitForOffset(uint32_t Offset) {
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000441 parseCompileUnits();
Frederic Riss4e126a02014-09-15 07:50:27 +0000442 return CUs.getUnitForOffset(Offset);
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000443}
444
Alexey Samsonov45be7932012-08-30 07:49:50 +0000445DWARFCompileUnit *DWARFContext::getCompileUnitForAddress(uint64_t Address) {
Benjamin Kramer112ec172011-09-15 21:59:13 +0000446 // First, get the offset of the compile unit.
Alexey Samsonov45be7932012-08-30 07:49:50 +0000447 uint32_t CUOffset = getDebugAranges()->findAddress(Address);
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000448 // Retrieve the compile unit.
Alexey Samsonov45be7932012-08-30 07:49:50 +0000449 return getCompileUnitForOffset(CUOffset);
450}
451
Alexey Samsonovd0109992014-04-18 21:36:39 +0000452static bool getFunctionNameForAddress(DWARFCompileUnit *CU, uint64_t Address,
Alexey Samsonovdce67342014-05-15 21:24:32 +0000453 FunctionNameKind Kind,
Alexey Samsonovd0109992014-04-18 21:36:39 +0000454 std::string &FunctionName) {
Alexey Samsonovdce67342014-05-15 21:24:32 +0000455 if (Kind == FunctionNameKind::None)
456 return false;
Alexey Samsonovd0109992014-04-18 21:36:39 +0000457 // 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.
460 const DWARFDebugInfoEntryInlinedChain &InlinedChain =
461 CU->getInlinedChainForAddress(Address);
462 if (InlinedChain.DIEs.size() == 0)
463 return false;
464 const DWARFDebugInfoEntryMinimal &TopFunctionDIE = InlinedChain.DIEs[0];
Alexey Samsonovdce67342014-05-15 21:24:32 +0000465 if (const char *Name =
466 TopFunctionDIE.getSubroutineName(InlinedChain.U, Kind)) {
Alexey Samsonovd0109992014-04-18 21:36:39 +0000467 FunctionName = Name;
468 return true;
469 }
470 return false;
471}
472
Alexey Samsonov45be7932012-08-30 07:49:50 +0000473DILineInfo DWARFContext::getLineInfoForAddress(uint64_t Address,
Alexey Samsonovdce67342014-05-15 21:24:32 +0000474 DILineInfoSpecifier Spec) {
Alexey Samsonovd0109992014-04-18 21:36:39 +0000475 DILineInfo Result;
476
Alexey Samsonov45be7932012-08-30 07:49:50 +0000477 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
478 if (!CU)
Alexey Samsonovd0109992014-04-18 21:36:39 +0000479 return Result;
Alexey Samsonovdce67342014-05-15 21:24:32 +0000480 getFunctionNameForAddress(CU, Address, Spec.FNKind, Result.FunctionName);
481 if (Spec.FLIKind != FileLineInfoKind::None) {
Frederic Riss101b5e22014-09-19 15:11:51 +0000482 if (const DWARFLineTable *LineTable = getLineTableForUnit(CU))
483 LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(),
484 Spec.FLIKind, Result);
Alexey Samsonovf4462fa2012-07-02 05:54:45 +0000485 }
Alexey Samsonovd0109992014-04-18 21:36:39 +0000486 return Result;
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000487}
David Blaikiea379b1812011-12-20 02:50:00 +0000488
Alexey Samsonovdce67342014-05-15 21:24:32 +0000489DILineInfoTable
490DWARFContext::getLineInfoForAddressRange(uint64_t Address, uint64_t Size,
491 DILineInfoSpecifier Spec) {
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000492 DILineInfoTable Lines;
493 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
494 if (!CU)
495 return Lines;
496
497 std::string FunctionName = "<invalid>";
Alexey Samsonovdce67342014-05-15 21:24:32 +0000498 getFunctionNameForAddress(CU, Address, Spec.FNKind, FunctionName);
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000499
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000500 // If the Specifier says we don't need FileLineInfo, just
501 // return the top-most function at the starting address.
Alexey Samsonovdce67342014-05-15 21:24:32 +0000502 if (Spec.FLIKind == FileLineInfoKind::None) {
Alexey Samsonovd0109992014-04-18 21:36:39 +0000503 DILineInfo Result;
504 Result.FunctionName = FunctionName;
505 Lines.push_back(std::make_pair(Address, Result));
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000506 return Lines;
507 }
508
Frederic Rissec8a5ba2014-09-04 06:14:40 +0000509 const DWARFLineTable *LineTable = getLineTableForUnit(CU);
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000510
511 // Get the index of row we're looking for in the line table.
512 std::vector<uint32_t> RowVector;
513 if (!LineTable->lookupAddressRange(Address, Size, RowVector))
514 return Lines;
515
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000516 for (uint32_t RowIndex : RowVector) {
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000517 // Take file number and line/column from the row.
518 const DWARFDebugLine::Row &Row = LineTable->Rows[RowIndex];
Alexey Samsonovd0109992014-04-18 21:36:39 +0000519 DILineInfo Result;
Frederic Riss101b5e22014-09-19 15:11:51 +0000520 LineTable->getFileNameByIndex(Row.File, CU->getCompilationDir(),
521 Spec.FLIKind, Result.FileName);
Alexey Samsonovd0109992014-04-18 21:36:39 +0000522 Result.FunctionName = FunctionName;
523 Result.Line = Row.Line;
524 Result.Column = Row.Column;
525 Lines.push_back(std::make_pair(Row.Address, Result));
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000526 }
527
528 return Lines;
529}
530
Alexey Samsonovdce67342014-05-15 21:24:32 +0000531DIInliningInfo
532DWARFContext::getInliningInfoForAddress(uint64_t Address,
533 DILineInfoSpecifier Spec) {
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000534 DIInliningInfo InliningInfo;
535
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000536 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
537 if (!CU)
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000538 return InliningInfo;
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000539
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000540 const DWARFLineTable *LineTable = nullptr;
Alexey Samsonov3211e612013-08-06 10:49:15 +0000541 const DWARFDebugInfoEntryInlinedChain &InlinedChain =
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000542 CU->getInlinedChainForAddress(Address);
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000543 if (InlinedChain.DIEs.size() == 0) {
544 // If there is no DIE for address (e.g. it is in unavailable .dwo file),
545 // try to at least get file/line info from symbol table.
Alexey Samsonovdce67342014-05-15 21:24:32 +0000546 if (Spec.FLIKind != FileLineInfoKind::None) {
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000547 DILineInfo Frame;
Frederic Rissec8a5ba2014-09-04 06:14:40 +0000548 LineTable = getLineTableForUnit(CU);
Frederic Riss101b5e22014-09-19 15:11:51 +0000549 if (LineTable &&
550 LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(),
551 Spec.FLIKind, Frame))
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000552 InliningInfo.addFrame(Frame);
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000553 }
554 return InliningInfo;
555 }
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000556
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000557 uint32_t CallFile = 0, CallLine = 0, CallColumn = 0;
Alexey Samsonov3211e612013-08-06 10:49:15 +0000558 for (uint32_t i = 0, n = InlinedChain.DIEs.size(); i != n; i++) {
559 const DWARFDebugInfoEntryMinimal &FunctionDIE = InlinedChain.DIEs[i];
Alexey Samsonovd0109992014-04-18 21:36:39 +0000560 DILineInfo Frame;
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000561 // Get function name if necessary.
Alexey Samsonovdce67342014-05-15 21:24:32 +0000562 if (const char *Name =
563 FunctionDIE.getSubroutineName(InlinedChain.U, Spec.FNKind))
564 Frame.FunctionName = Name;
565 if (Spec.FLIKind != FileLineInfoKind::None) {
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000566 if (i == 0) {
567 // For the topmost frame, initialize the line table of this
568 // compile unit and fetch file/line info from it.
Frederic Rissec8a5ba2014-09-04 06:14:40 +0000569 LineTable = getLineTableForUnit(CU);
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000570 // For the topmost routine, get file/line info from line table.
Frederic Riss101b5e22014-09-19 15:11:51 +0000571 if (LineTable)
572 LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(),
573 Spec.FLIKind, Frame);
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000574 } else {
575 // Otherwise, use call file, call line and call column from
576 // previous DIE in inlined chain.
Frederic Riss101b5e22014-09-19 15:11:51 +0000577 if (LineTable)
578 LineTable->getFileNameByIndex(CallFile, CU->getCompilationDir(),
579 Spec.FLIKind, Frame.FileName);
Alexey Samsonovd0109992014-04-18 21:36:39 +0000580 Frame.Line = CallLine;
581 Frame.Column = CallColumn;
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000582 }
583 // Get call file/line/column of a current DIE.
584 if (i + 1 < n) {
David Blaikie07e22442013-09-23 22:44:40 +0000585 FunctionDIE.getCallerFrame(InlinedChain.U, CallFile, CallLine,
Alexey Samsonov3211e612013-08-06 10:49:15 +0000586 CallColumn);
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000587 }
588 }
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000589 InliningInfo.addFrame(Frame);
590 }
591 return InliningInfo;
592}
593
George Rimar401e4e52016-05-24 12:48:46 +0000594static bool consumeCompressedGnuHeader(StringRef &data,
595 uint64_t &OriginalSize) {
Alexey Samsonov068fc8a2013-04-23 10:17:34 +0000596 // Consume "ZLIB" prefix.
597 if (!data.startswith("ZLIB"))
598 return false;
599 data = data.substr(4);
600 // Consume uncompressed section size (big-endian 8 bytes).
601 DataExtractor extractor(data, false, 8);
602 uint32_t Offset = 0;
603 OriginalSize = extractor.getU64(&Offset);
604 if (Offset == 0)
605 return false;
606 data = data.substr(Offset);
607 return true;
608}
609
George Rimar401e4e52016-05-24 12:48:46 +0000610static bool consumeCompressedZLibHeader(StringRef &Data, uint64_t &OriginalSize,
611 bool IsLE, bool Is64Bit) {
612 using namespace ELF;
613 uint64_t HdrSize = Is64Bit ? sizeof(Elf64_Chdr) : sizeof(Elf32_Chdr);
614 if (Data.size() < HdrSize)
615 return false;
616
617 DataExtractor Extractor(Data, IsLE, 0);
618 uint32_t Offset = 0;
619 if (Extractor.getUnsigned(&Offset, Is64Bit ? sizeof(Elf64_Word)
620 : sizeof(Elf32_Word)) !=
621 ELFCOMPRESS_ZLIB)
622 return false;
623
624 // Skip Elf64_Chdr::ch_reserved field.
625 if (Is64Bit)
626 Offset += sizeof(Elf64_Word);
627
628 OriginalSize = Extractor.getUnsigned(&Offset, Is64Bit ? sizeof(Elf64_Xword)
629 : sizeof(Elf32_Word));
630 Data = Data.substr(HdrSize);
631 return true;
632}
633
634static bool tryDecompress(StringRef &Name, StringRef &Data,
635 SmallString<32> &Out, bool ZLibStyle, bool IsLE,
636 bool Is64Bit) {
637 if (!zlib::isAvailable())
638 return false;
639
640 uint64_t OriginalSize;
641 bool Result =
642 ZLibStyle ? consumeCompressedZLibHeader(Data, OriginalSize, IsLE, Is64Bit)
643 : consumeCompressedGnuHeader(Data, OriginalSize);
644
645 if (!Result || zlib::uncompress(Data, Out, OriginalSize) != zlib::StatusOK)
646 return false;
647
648 // gnu-style names are started from "z", consume that.
649 if (!ZLibStyle)
650 Name = Name.substr(1);
651 return true;
652}
653
Keno Fischerc780e8e2015-05-21 21:24:32 +0000654DWARFContextInMemory::DWARFContextInMemory(const object::ObjectFile &Obj,
655 const LoadedObjectInfo *L)
Rafael Espindolaa04bb5b2014-07-31 20:19:36 +0000656 : IsLittleEndian(Obj.isLittleEndian()),
657 AddressSize(Obj.getBytesInAddress()) {
658 for (const SectionRef &Section : Obj.sections()) {
Eric Christopher7370b552012-11-12 21:40:38 +0000659 StringRef name;
Alexey Samsonov063eb3f2014-03-13 13:52:54 +0000660 Section.getName(name);
David Majnemerdac39852014-09-26 22:32:16 +0000661 // Skip BSS and Virtual sections, they aren't interesting.
Rafael Espindola80291272014-10-08 15:28:58 +0000662 bool IsBSS = Section.isBSS();
David Majnemerdac39852014-09-26 22:32:16 +0000663 if (IsBSS)
664 continue;
Rafael Espindola80291272014-10-08 15:28:58 +0000665 bool IsVirtual = Section.isVirtual();
David Majnemerdac39852014-09-26 22:32:16 +0000666 if (IsVirtual)
667 continue;
Eric Christopher7370b552012-11-12 21:40:38 +0000668 StringRef data;
Keno Fischerc780e8e2015-05-21 21:24:32 +0000669
Lang Hames2e88f4f2015-07-28 17:52:11 +0000670 section_iterator RelocatedSection = Section.getRelocatedSection();
Keno Fischerc780e8e2015-05-21 21:24:32 +0000671 // Try to obtain an already relocated version of this section.
672 // Else use the unrelocated section from the object file. We'll have to
673 // apply relocations ourselves later.
Lang Hames2e88f4f2015-07-28 17:52:11 +0000674 if (!L || !L->getLoadedSectionContents(*RelocatedSection,data))
Keno Fischerc780e8e2015-05-21 21:24:32 +0000675 Section.getContents(data);
Eric Christopher7370b552012-11-12 21:40:38 +0000676
Eric Christopher7370b552012-11-12 21:40:38 +0000677 name = name.substr(name.find_first_not_of("._")); // Skip . and _ prefixes.
Alexey Samsonov3d0e3ed2013-04-17 14:27:04 +0000678
George Rimar401e4e52016-05-24 12:48:46 +0000679 bool ZLibStyleCompressed = Section.isCompressed();
680 if (ZLibStyleCompressed || name.startswith("zdebug_")) {
681 SmallString<32> Out;
682 if (!tryDecompress(name, data, Out, ZLibStyleCompressed, IsLittleEndian,
683 AddressSize == 8))
Alexey Samsonov068fc8a2013-04-23 10:17:34 +0000684 continue;
George Rimar401e4e52016-05-24 12:48:46 +0000685 UncompressedSections.emplace_back(std::move(Out));
David Blaikiea505f242014-04-05 21:26:44 +0000686 data = UncompressedSections.back();
Alexey Samsonov068fc8a2013-04-23 10:17:34 +0000687 }
688
Alexey Samsonov063eb3f2014-03-13 13:52:54 +0000689 StringRef *SectionData =
Eric Christopher4c7e6ba2013-09-25 23:02:41 +0000690 StringSwitch<StringRef *>(name)
691 .Case("debug_info", &InfoSection.Data)
692 .Case("debug_abbrev", &AbbrevSection)
693 .Case("debug_loc", &LocSection.Data)
694 .Case("debug_line", &LineSection.Data)
695 .Case("debug_aranges", &ARangeSection)
696 .Case("debug_frame", &DebugFrameSection)
Igor Laevsky03a670c2016-01-26 15:09:42 +0000697 .Case("eh_frame", &EHFrameSection)
Eric Christopher4c7e6ba2013-09-25 23:02:41 +0000698 .Case("debug_str", &StringSection)
699 .Case("debug_ranges", &RangeSection)
Amjad Aboude59cc3e2015-11-12 09:38:54 +0000700 .Case("debug_macinfo", &MacinfoSection)
Eric Christopher4c7e6ba2013-09-25 23:02:41 +0000701 .Case("debug_pubnames", &PubNamesSection)
702 .Case("debug_pubtypes", &PubTypesSection)
703 .Case("debug_gnu_pubnames", &GnuPubNamesSection)
704 .Case("debug_gnu_pubtypes", &GnuPubTypesSection)
705 .Case("debug_info.dwo", &InfoDWOSection.Data)
706 .Case("debug_abbrev.dwo", &AbbrevDWOSection)
David Blaikie9c550ac2014-03-25 01:44:02 +0000707 .Case("debug_loc.dwo", &LocDWOSection.Data)
David Blaikie1d4736e2014-02-24 23:58:54 +0000708 .Case("debug_line.dwo", &LineDWOSection.Data)
Eric Christopher4c7e6ba2013-09-25 23:02:41 +0000709 .Case("debug_str.dwo", &StringDWOSection)
710 .Case("debug_str_offsets.dwo", &StringOffsetDWOSection)
711 .Case("debug_addr", &AddrSection)
Frederic Riss7c500472014-11-14 19:30:08 +0000712 .Case("apple_names", &AppleNamesSection.Data)
713 .Case("apple_types", &AppleTypesSection.Data)
714 .Case("apple_namespaces", &AppleNamespacesSection.Data)
715 .Case("apple_namespac", &AppleNamespacesSection.Data)
716 .Case("apple_objc", &AppleObjCSection.Data)
David Blaikie65a8efe2015-11-11 19:28:21 +0000717 .Case("debug_cu_index", &CUIndexSection)
David Blaikie51c40282015-11-11 19:40:49 +0000718 .Case("debug_tu_index", &TUIndexSection)
Eric Christopher4c7e6ba2013-09-25 23:02:41 +0000719 // Any more debug info sections go here.
Craig Topper2617dcc2014-04-15 06:32:26 +0000720 .Default(nullptr);
Alexey Samsonov063eb3f2014-03-13 13:52:54 +0000721 if (SectionData) {
722 *SectionData = data;
Rafael Espindola4f60a382013-05-30 03:05:14 +0000723 if (name == "debug_ranges") {
724 // FIXME: Use the other dwo range section when we emit it.
725 RangeDWOSection = data;
726 }
David Blaikie03c089c2013-09-23 22:44:47 +0000727 } else if (name == "debug_types") {
David Blaikie427e4352013-09-23 23:39:55 +0000728 // Find debug_types data by section rather than name as there are
729 // multiple, comdat grouped, debug_types sections.
Alexey Samsonov063eb3f2014-03-13 13:52:54 +0000730 TypesSections[Section].Data = data;
David Blaikie92d9d622014-01-09 05:08:24 +0000731 } else if (name == "debug_types.dwo") {
Alexey Samsonov063eb3f2014-03-13 13:52:54 +0000732 TypesDWOSections[Section].Data = data;
Eric Christopherda4b2192013-01-02 23:52:13 +0000733 }
Eric Christopher7370b552012-11-12 21:40:38 +0000734
Rafael Espindolaa04bb5b2014-07-31 20:19:36 +0000735 if (RelocatedSection == Obj.section_end())
Rafael Espindola4f60a382013-05-30 03:05:14 +0000736 continue;
737
738 StringRef RelSecName;
Keno Fischerc780e8e2015-05-21 21:24:32 +0000739 StringRef RelSecData;
Rafael Espindola4f60a382013-05-30 03:05:14 +0000740 RelocatedSection->getName(RelSecName);
Keno Fischerc780e8e2015-05-21 21:24:32 +0000741
742 // If the section we're relocating was relocated already by the JIT,
743 // then we used the relocated version above, so we do not need to process
744 // relocations for it now.
Lang Hames2e88f4f2015-07-28 17:52:11 +0000745 if (L && L->getLoadedSectionContents(*RelocatedSection,RelSecData))
Keno Fischerc780e8e2015-05-21 21:24:32 +0000746 continue;
747
Frederic Riss7bb12262015-08-23 04:44:21 +0000748 // In Mach-o files, the relocations do not need to be applied if
749 // there is no load offset to apply. The value read at the
750 // relocation point already factors in the section address
751 // (actually applying the relocations will produce wrong results
752 // as the section address will be added twice).
Craig Topper66059c92015-11-18 07:07:59 +0000753 if (!L && isa<MachOObjectFile>(&Obj))
Frederic Riss7bb12262015-08-23 04:44:21 +0000754 continue;
755
Rafael Espindola4f60a382013-05-30 03:05:14 +0000756 RelSecName = RelSecName.substr(
757 RelSecName.find_first_not_of("._")); // Skip . and _ prefixes.
758
Andrew Kaylord55d7012013-01-25 22:50:58 +0000759 // TODO: Add support for relocations in other sections as needed.
760 // Record relocations for the debug_info and debug_line sections.
Rafael Espindola4f60a382013-05-30 03:05:14 +0000761 RelocAddrMap *Map = StringSwitch<RelocAddrMap*>(RelSecName)
David Blaikie1b5ee5d2013-09-23 17:42:01 +0000762 .Case("debug_info", &InfoSection.Relocs)
763 .Case("debug_loc", &LocSection.Relocs)
764 .Case("debug_info.dwo", &InfoDWOSection.Relocs)
765 .Case("debug_line", &LineSection.Relocs)
Frederic Riss7c500472014-11-14 19:30:08 +0000766 .Case("apple_names", &AppleNamesSection.Relocs)
767 .Case("apple_types", &AppleTypesSection.Relocs)
768 .Case("apple_namespaces", &AppleNamespacesSection.Relocs)
769 .Case("apple_namespac", &AppleNamespacesSection.Relocs)
770 .Case("apple_objc", &AppleObjCSection.Relocs)
Craig Topper2617dcc2014-04-15 06:32:26 +0000771 .Default(nullptr);
David Blaikie03c089c2013-09-23 22:44:47 +0000772 if (!Map) {
David Blaikie427e4352013-09-23 23:39:55 +0000773 // Find debug_types relocs by section rather than name as there are
774 // multiple, comdat grouped, debug_types sections.
David Blaikie92d9d622014-01-09 05:08:24 +0000775 if (RelSecName == "debug_types")
776 Map = &TypesSections[*RelocatedSection].Relocs;
777 else if (RelSecName == "debug_types.dwo")
778 Map = &TypesDWOSections[*RelocatedSection].Relocs;
779 else
780 continue;
David Blaikie03c089c2013-09-23 22:44:47 +0000781 }
Eric Christopher7370b552012-11-12 21:40:38 +0000782
Alexey Samsonov063eb3f2014-03-13 13:52:54 +0000783 if (Section.relocation_begin() != Section.relocation_end()) {
Rafael Espindola80291272014-10-08 15:28:58 +0000784 uint64_t SectionSize = RelocatedSection->getSize();
Alexey Samsonovaa4d2952014-03-14 14:22:49 +0000785 for (const RelocationRef &Reloc : Section.relocations()) {
Rafael Espindola96d071c2015-06-29 23:29:12 +0000786 uint64_t Address = Reloc.getOffset();
Rafael Espindola99c041b2015-06-30 01:53:01 +0000787 uint64_t Type = Reloc.getType();
Andrew Kaylord55d7012013-01-25 22:50:58 +0000788 uint64_t SymAddr = 0;
Keno Fischerc780e8e2015-05-21 21:24:32 +0000789 uint64_t SectionLoadAddress = 0;
David Majnemer5fbe3242014-10-08 06:38:50 +0000790 object::symbol_iterator Sym = Reloc.getSymbol();
Rafael Espindola63a88ce2015-06-19 17:54:28 +0000791 object::section_iterator RSec = Obj.section_end();
Keno Fischerc780e8e2015-05-21 21:24:32 +0000792
793 // First calculate the address of the symbol or section as it appears
794 // in the objct file
795 if (Sym != Obj.symbol_end()) {
Kevin Enderby931cb652016-06-24 18:24:42 +0000796 Expected<uint64_t> SymAddrOrErr = Sym->getAddress();
797 if (!SymAddrOrErr) {
798 std::string Buf;
799 raw_string_ostream OS(Buf);
800 logAllUnhandledErrors(SymAddrOrErr.takeError(), OS, "");
801 OS.flush();
Rafael Espindolaed067c42015-07-03 18:19:00 +0000802 errs() << "error: failed to compute symbol address: "
Kevin Enderby931cb652016-06-24 18:24:42 +0000803 << Buf << '\n';
Rafael Espindolaed067c42015-07-03 18:19:00 +0000804 continue;
805 }
806 SymAddr = *SymAddrOrErr;
Keno Fischerc780e8e2015-05-21 21:24:32 +0000807 // Also remember what section this symbol is in for later
Kevin Enderby7bd8d992016-05-02 20:28:12 +0000808 auto SectOrErr = Sym->getSection();
809 if (!SectOrErr) {
810 std::string Buf;
811 raw_string_ostream OS(Buf);
812 logAllUnhandledErrors(SectOrErr.takeError(), OS, "");
813 OS.flush();
814 errs() << "error: failed to get symbol section: "
815 << Buf << '\n';
816 continue;
817 }
818 RSec = *SectOrErr;
Rafael Espindola63a88ce2015-06-19 17:54:28 +0000819 } else if (auto *MObj = dyn_cast<MachOObjectFile>(&Obj)) {
820 // MachO also has relocations that point to sections and
821 // scattered relocations.
Frederic Rissf6402bf2015-07-31 20:22:50 +0000822 auto RelocInfo = MObj->getRelocation(Reloc.getRawDataRefImpl());
823 if (MObj->isRelocationScattered(RelocInfo)) {
824 // FIXME: it's not clear how to correctly handle scattered
825 // relocations.
826 continue;
827 } else {
828 RSec = MObj->getRelocationSection(Reloc.getRawDataRefImpl());
829 SymAddr = RSec->getAddress();
830 }
Rafael Espindola63a88ce2015-06-19 17:54:28 +0000831 }
Keno Fischerc780e8e2015-05-21 21:24:32 +0000832
833 // If we are given load addresses for the sections, we need to adjust:
834 // SymAddr = (Address of Symbol Or Section in File) -
835 // (Address of Section in File) +
836 // (Load Address of Section)
837 if (L != nullptr && RSec != Obj.section_end()) {
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000838 // RSec is now either the section being targeted or the section
839 // containing the symbol being targeted. In either case,
Keno Fischerc780e8e2015-05-21 21:24:32 +0000840 // we need to perform the same computation.
841 StringRef SecName;
842 RSec->getName(SecName);
Lang Hames2e88f4f2015-07-28 17:52:11 +0000843// llvm::dbgs() << "Name: '" << SecName
844// << "', RSec: " << RSec->getRawDataRefImpl()
845// << ", Section: " << Section.getRawDataRefImpl() << "\n";
846 SectionLoadAddress = L->getSectionLoadAddress(*RSec);
Keno Fischerc780e8e2015-05-21 21:24:32 +0000847 if (SectionLoadAddress != 0)
848 SymAddr += SectionLoadAddress - RSec->getAddress();
849 }
Eric Christopher7370b552012-11-12 21:40:38 +0000850
Eric Christopher47e079d2014-10-06 06:55:55 +0000851 object::RelocVisitor V(Obj);
David Majnemer5fbe3242014-10-08 06:38:50 +0000852 object::RelocToApply R(V.visit(Type, Reloc, SymAddr));
Eric Christopher7370b552012-11-12 21:40:38 +0000853 if (V.error()) {
854 SmallString<32> Name;
Rafael Espindola41bb4322015-06-30 04:08:37 +0000855 Reloc.getTypeName(Name);
Eric Christopher7370b552012-11-12 21:40:38 +0000856 errs() << "error: failed to compute relocation: "
857 << Name << "\n";
858 continue;
859 }
860
861 if (Address + R.Width > SectionSize) {
862 errs() << "error: " << R.Width << "-byte relocation starting "
863 << Address << " bytes into section " << name << " which is "
864 << SectionSize << " bytes long.\n";
865 continue;
866 }
867 if (R.Width > 8) {
868 errs() << "error: can't handle a relocation of more than 8 bytes at "
869 "a time.\n";
870 continue;
871 }
872 DEBUG(dbgs() << "Writing " << format("%p", R.Value)
873 << " at " << format("%p", Address)
874 << " with width " << format("%d", R.Width)
875 << "\n");
Eric Christopherda4b2192013-01-02 23:52:13 +0000876 Map->insert(std::make_pair(Address, std::make_pair(R.Width, R.Value)));
Eric Christopher7370b552012-11-12 21:40:38 +0000877 }
878 }
879 }
880}
881
David Blaikiea379b1812011-12-20 02:50:00 +0000882void DWARFContextInMemory::anchor() { }