blob: 88efcd8f49c4048e58226c9c69dd62785aafd4d9 [file] [log] [blame]
Eugene Zelenko28db7e62017-03-01 01:14:23 +00001//===- DWARFContext.cpp ---------------------------------------------------===//
Benjamin Krameraa2f78f2011-09-13 19:42:23 +00002//
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
Greg Claytonb8c162b2017-05-03 16:02:29 +000010#include "llvm/DebugInfo/DWARF/DWARFContext.h"
11#include "llvm/ADT/STLExtras.h"
Alexey Samsonove16e16a2012-07-19 07:03:58 +000012#include "llvm/ADT/SmallString.h"
Eugene Zelenko28db7e62017-03-01 01:14:23 +000013#include "llvm/ADT/SmallVector.h"
Eugene Zelenko28db7e62017-03-01 01:14:23 +000014#include "llvm/ADT/StringRef.h"
Greg Claytonb8c162b2017-05-03 16:02:29 +000015#include "llvm/ADT/StringSwitch.h"
Eugene Zelenko2db0cfa2017-06-23 21:57:40 +000016#include "llvm/BinaryFormat/Dwarf.h"
Zachary Turner82af9432015-01-30 18:07:45 +000017#include "llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h"
Eugene Zelenko28db7e62017-03-01 01:14:23 +000018#include "llvm/DebugInfo/DWARF/DWARFCompileUnit.h"
Eugene Zelenko28db7e62017-03-01 01:14:23 +000019#include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h"
Zachary Turner82af9432015-01-30 18:07:45 +000020#include "llvm/DebugInfo/DWARF/DWARFDebugArangeSet.h"
Greg Claytonb8c162b2017-05-03 16:02:29 +000021#include "llvm/DebugInfo/DWARF/DWARFDebugAranges.h"
Eugene Zelenko28db7e62017-03-01 01:14:23 +000022#include "llvm/DebugInfo/DWARF/DWARFDebugFrame.h"
23#include "llvm/DebugInfo/DWARF/DWARFDebugLine.h"
24#include "llvm/DebugInfo/DWARF/DWARFDebugLoc.h"
25#include "llvm/DebugInfo/DWARF/DWARFDebugMacro.h"
George Rimare71e33f2016-12-17 09:10:32 +000026#include "llvm/DebugInfo/DWARF/DWARFDebugPubTable.h"
Eugene Zelenko28db7e62017-03-01 01:14:23 +000027#include "llvm/DebugInfo/DWARF/DWARFDebugRangeList.h"
28#include "llvm/DebugInfo/DWARF/DWARFDie.h"
29#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
30#include "llvm/DebugInfo/DWARF/DWARFGdbIndex.h"
31#include "llvm/DebugInfo/DWARF/DWARFSection.h"
David Blaikie65a8efe2015-11-11 19:28:21 +000032#include "llvm/DebugInfo/DWARF/DWARFUnitIndex.h"
Greg Claytonb8c162b2017-05-03 16:02:29 +000033#include "llvm/DebugInfo/DWARF/DWARFVerifier.h"
George Rimar4bf30832017-01-11 15:26:41 +000034#include "llvm/Object/Decompressor.h"
Reid Klecknerdafc5d72016-07-06 16:56:42 +000035#include "llvm/Object/MachO.h"
Eugene Zelenko28db7e62017-03-01 01:14:23 +000036#include "llvm/Object/ObjectFile.h"
Reid Klecknerdafc5d72016-07-06 16:56:42 +000037#include "llvm/Object/RelocVisitor.h"
Eugene Zelenko28db7e62017-03-01 01:14:23 +000038#include "llvm/Support/Casting.h"
39#include "llvm/Support/DataExtractor.h"
Eugene Zelenko28db7e62017-03-01 01:14:23 +000040#include "llvm/Support/Error.h"
Benjamin Kramer07d4b1c2011-09-15 16:57:13 +000041#include "llvm/Support/Format.h"
Eugene Zelenko28db7e62017-03-01 01:14:23 +000042#include "llvm/Support/MemoryBuffer.h"
Benjamin Kramera6002fd2011-09-14 01:09:52 +000043#include "llvm/Support/raw_ostream.h"
Benjamin Kramer2602ca62011-09-15 20:43:22 +000044#include <algorithm>
Eugene Zelenko28db7e62017-03-01 01:14:23 +000045#include <cstdint>
Greg Claytonc7695a82017-05-02 20:28:33 +000046#include <map>
Eugene Zelenko28db7e62017-03-01 01:14:23 +000047#include <string>
Eugene Zelenko2db0cfa2017-06-23 21:57:40 +000048#include <tuple>
Eugene Zelenko28db7e62017-03-01 01:14:23 +000049#include <utility>
50#include <vector>
51
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000052using namespace llvm;
Benjamin Kramer6dda0322011-09-15 18:02:20 +000053using namespace dwarf;
Rafael Espindola4f60a382013-05-30 03:05:14 +000054using namespace object;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000055
Chandler Carruthe96dd892014-04-21 22:55:11 +000056#define DEBUG_TYPE "dwarf"
57
Eugene Zelenko2db0cfa2017-06-23 21:57:40 +000058using DWARFLineTable = DWARFDebugLine::LineTable;
59using FileLineInfoKind = DILineInfoSpecifier::FileLineInfoKind;
60using FunctionNameKind = DILineInfoSpecifier::FunctionNameKind;
Eric Christopher494109b2012-10-16 23:46:25 +000061
Frederic Riss7c500472014-11-14 19:30:08 +000062static void dumpAccelSection(raw_ostream &OS, StringRef Name,
Rafael Espindolac398e672017-07-19 22:27:28 +000063 const DWARFObject &Obj,
64 const DWARFSection &Section,
65 StringRef StringSection, bool LittleEndian) {
66 DWARFDataExtractor AccelSection(Obj, Section, LittleEndian, 0);
Frederic Risse837ec22014-11-14 16:15:53 +000067 DataExtractor StrData(StringSection, LittleEndian, 0);
68 OS << "\n." << Name << " contents:\n";
Paul Robinson17536b92017-06-29 16:52:08 +000069 DWARFAcceleratorTable Accel(AccelSection, StrData);
Frederic Risse837ec22014-11-14 16:15:53 +000070 if (!Accel.extract())
71 return;
72 Accel.dump(OS);
73}
74
Wolfgang Pieb77d3e932017-06-06 01:22:34 +000075static void
76dumpDWARFv5StringOffsetsSection(raw_ostream &OS, StringRef SectionName,
Rafael Espindolac398e672017-07-19 22:27:28 +000077 const DWARFObject &Obj,
Wolfgang Pieb77d3e932017-06-06 01:22:34 +000078 const DWARFSection &StringOffsetsSection,
79 StringRef StringSection, bool LittleEndian) {
Rafael Espindolac398e672017-07-19 22:27:28 +000080 DWARFDataExtractor StrOffsetExt(Obj, StringOffsetsSection, LittleEndian, 0);
Wolfgang Pieb77d3e932017-06-06 01:22:34 +000081 uint32_t Offset = 0;
82 uint64_t SectionSize = StringOffsetsSection.Data.size();
83
84 while (Offset < SectionSize) {
85 unsigned Version = 0;
86 DwarfFormat Format = DWARF32;
87 unsigned EntrySize = 4;
88 // Perform validation and extract the segment size from the header.
89 if (!StrOffsetExt.isValidOffsetForDataOfSize(Offset, 4)) {
90 OS << "error: invalid contribution to string offsets table in section ."
91 << SectionName << ".\n";
92 return;
93 }
94 uint32_t ContributionStart = Offset;
95 uint64_t ContributionSize = StrOffsetExt.getU32(&Offset);
96 // A contribution size of 0xffffffff indicates DWARF64, with the actual size
97 // in the following 8 bytes. Otherwise, the DWARF standard mandates that
98 // the contribution size must be at most 0xfffffff0.
99 if (ContributionSize == 0xffffffff) {
100 if (!StrOffsetExt.isValidOffsetForDataOfSize(Offset, 8)) {
101 OS << "error: invalid contribution to string offsets table in section ."
102 << SectionName << ".\n";
103 return;
104 }
105 Format = DWARF64;
106 EntrySize = 8;
107 ContributionSize = StrOffsetExt.getU64(&Offset);
108 } else if (ContributionSize > 0xfffffff0) {
109 OS << "error: invalid contribution to string offsets table in section ."
110 << SectionName << ".\n";
111 return;
112 }
113
114 // We must ensure that we don't read a partial record at the end, so we
115 // validate for a multiple of EntrySize. Also, we're expecting a version
116 // number and padding, which adds an additional 4 bytes.
117 uint64_t ValidationSize =
118 4 + ((ContributionSize + EntrySize - 1) & (-(uint64_t)EntrySize));
119 if (!StrOffsetExt.isValidOffsetForDataOfSize(Offset, ValidationSize)) {
120 OS << "error: contribution to string offsets table in section ."
121 << SectionName << " has invalid length.\n";
122 return;
123 }
124
125 Version = StrOffsetExt.getU16(&Offset);
126 Offset += 2;
127 OS << format("0x%8.8x: ", ContributionStart);
128 OS << "Contribution size = " << ContributionSize
129 << ", Version = " << Version << "\n";
130
131 uint32_t ContributionBase = Offset;
132 DataExtractor StrData(StringSection, LittleEndian, 0);
133 while (Offset - ContributionBase < ContributionSize) {
134 OS << format("0x%8.8x: ", Offset);
135 // FIXME: We can only extract strings in DWARF32 format at the moment.
Paul Robinson17536b92017-06-29 16:52:08 +0000136 uint64_t StringOffset =
137 StrOffsetExt.getRelocatedValue(EntrySize, &Offset);
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000138 if (Format == DWARF32) {
139 OS << format("%8.8x ", StringOffset);
140 uint32_t StringOffset32 = (uint32_t)StringOffset;
141 const char *S = StrData.getCStr(&StringOffset32);
142 if (S)
143 OS << format("\"%s\"", S);
144 } else
145 OS << format("%16.16x ", StringOffset);
146 OS << "\n";
147 }
148 }
149}
150
151// Dump a DWARF string offsets section. This may be a DWARF v5 formatted
152// string offsets section, where each compile or type unit contributes a
153// number of entries (string offsets), with each contribution preceded by
154// a header containing size and version number. Alternatively, it may be a
155// monolithic series of string offsets, as generated by the pre-DWARF v5
156// implementation of split DWARF.
157static void dumpStringOffsetsSection(raw_ostream &OS, StringRef SectionName,
Rafael Espindolac398e672017-07-19 22:27:28 +0000158 const DWARFObject &Obj,
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000159 const DWARFSection &StringOffsetsSection,
160 StringRef StringSection, bool LittleEndian,
161 unsigned MaxVersion) {
162 if (StringOffsetsSection.Data.empty())
163 return;
164 OS << "\n." << SectionName << " contents:\n";
165 // If we have at least one (compile or type) unit with DWARF v5 or greater,
166 // we assume that the section is formatted like a DWARF v5 string offsets
167 // section.
168 if (MaxVersion >= 5)
Rafael Espindolac398e672017-07-19 22:27:28 +0000169 dumpDWARFv5StringOffsetsSection(OS, SectionName, Obj, StringOffsetsSection,
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000170 StringSection, LittleEndian);
171 else {
172 DataExtractor strOffsetExt(StringOffsetsSection.Data, LittleEndian, 0);
173 uint32_t offset = 0;
174 uint64_t size = StringOffsetsSection.Data.size();
175 // Ensure that size is a multiple of the size of an entry.
176 if (size & ((uint64_t)(sizeof(uint32_t) - 1))) {
177 OS << "error: size of ." << SectionName << " is not a multiple of "
178 << sizeof(uint32_t) << ".\n";
179 size &= -(uint64_t)sizeof(uint32_t);
180 }
181 DataExtractor StrData(StringSection, LittleEndian, 0);
182 while (offset < size) {
183 OS << format("0x%8.8x: ", offset);
184 uint32_t StringOffset = strOffsetExt.getU32(&offset);
185 OS << format("%8.8x ", StringOffset);
186 const char *S = StrData.getCStr(&StringOffset);
187 if (S)
188 OS << format("\"%s\"", S);
189 OS << "\n";
190 }
191 }
192}
193
Eugene Zelenko2db0cfa2017-06-23 21:57:40 +0000194void DWARFContext::dump(raw_ostream &OS, DIDumpOptions DumpOpts) {
Adrian Prantlf4bc1f72017-06-01 18:18:23 +0000195 DIDumpType DumpType = DumpOpts.DumpType;
196 bool DumpEH = DumpOpts.DumpEH;
197 bool SummarizeTypes = DumpOpts.SummarizeTypes;
198
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000199 if (DumpType == DIDT_All || DumpType == DIDT_Abbrev) {
200 OS << ".debug_abbrev contents:\n";
201 getDebugAbbrev()->dump(OS);
202 }
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000203
David Blaikie66865d62014-01-09 00:13:35 +0000204 if (DumpType == DIDT_All || DumpType == DIDT_AbbrevDwo)
205 if (const DWARFDebugAbbrev *D = getDebugAbbrevDWO()) {
David Blaikie622dce42014-01-08 23:29:59 +0000206 OS << "\n.debug_abbrev.dwo contents:\n";
David Blaikie66865d62014-01-09 00:13:35 +0000207 D->dump(OS);
David Blaikie622dce42014-01-08 23:29:59 +0000208 }
David Blaikie622dce42014-01-08 23:29:59 +0000209
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000210 if (DumpType == DIDT_All || DumpType == DIDT_Info) {
211 OS << "\n.debug_info contents:\n";
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000212 for (const auto &CU : compile_units())
Adrian Prantl318d1192017-06-06 23:28:45 +0000213 CU->dump(OS, DumpOpts);
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000214 }
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000215
David Blaikie66865d62014-01-09 00:13:35 +0000216 if ((DumpType == DIDT_All || DumpType == DIDT_InfoDwo) &&
217 getNumDWOCompileUnits()) {
218 OS << "\n.debug_info.dwo contents:\n";
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000219 for (const auto &DWOCU : dwo_compile_units())
Adrian Prantl318d1192017-06-06 23:28:45 +0000220 DWOCU->dump(OS, DumpOpts);
David Blaikie66865d62014-01-09 00:13:35 +0000221 }
David Blaikie622dce42014-01-08 23:29:59 +0000222
David Blaikie66865d62014-01-09 00:13:35 +0000223 if ((DumpType == DIDT_All || DumpType == DIDT_Types) && getNumTypeUnits()) {
David Blaikie03c089c2013-09-23 22:44:47 +0000224 OS << "\n.debug_types contents:\n";
Frederic Riss312a02e2014-09-29 13:56:39 +0000225 for (const auto &TUS : type_unit_sections())
226 for (const auto &TU : TUS)
David Blaikie50cc27e2016-10-18 21:09:48 +0000227 TU->dump(OS, SummarizeTypes);
David Blaikie03c089c2013-09-23 22:44:47 +0000228 }
229
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000230 if ((DumpType == DIDT_All || DumpType == DIDT_TypesDwo) &&
231 getNumDWOTypeUnits()) {
232 OS << "\n.debug_types.dwo contents:\n";
Frederic Riss312a02e2014-09-29 13:56:39 +0000233 for (const auto &DWOTUS : dwo_type_unit_sections())
234 for (const auto &DWOTU : DWOTUS)
David Blaikie50cc27e2016-10-18 21:09:48 +0000235 DWOTU->dump(OS, SummarizeTypes);
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000236 }
David Blaikie92d9d622014-01-09 05:08:24 +0000237
David Blaikie18e73502013-06-19 21:37:13 +0000238 if (DumpType == DIDT_All || DumpType == DIDT_Loc) {
David Blaikie03c089c2013-09-23 22:44:47 +0000239 OS << "\n.debug_loc contents:\n";
David Blaikie18e73502013-06-19 21:37:13 +0000240 getDebugLoc()->dump(OS);
241 }
242
David Blaikie9c550ac2014-03-25 01:44:02 +0000243 if (DumpType == DIDT_All || DumpType == DIDT_LocDwo) {
244 OS << "\n.debug_loc.dwo contents:\n";
245 getDebugLocDWO()->dump(OS);
246 }
247
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000248 if (DumpType == DIDT_All || DumpType == DIDT_Frames) {
249 OS << "\n.debug_frame contents:\n";
250 getDebugFrame()->dump(OS);
Igor Laevsky03a670c2016-01-26 15:09:42 +0000251 if (DumpEH) {
252 OS << "\n.eh_frame contents:\n";
253 getEHFrame()->dump(OS);
254 }
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000255 }
256
Amjad Aboude59cc3e2015-11-12 09:38:54 +0000257 if (DumpType == DIDT_All || DumpType == DIDT_Macro) {
258 OS << "\n.debug_macinfo contents:\n";
259 getDebugMacro()->dump(OS);
260 }
261
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000262 uint32_t offset = 0;
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000263 if (DumpType == DIDT_All || DumpType == DIDT_Aranges) {
264 OS << "\n.debug_aranges contents:\n";
Rafael Espindolac398e672017-07-19 22:27:28 +0000265 DataExtractor arangesData(DObj->getARangeSection(), isLittleEndian(), 0);
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000266 DWARFDebugArangeSet set;
267 while (set.extract(arangesData, &offset))
268 set.dump(OS);
269 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000270
Alexey Samsonov034e57a2012-08-27 07:17:47 +0000271 uint8_t savedAddressByteSize = 0;
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000272 if (DumpType == DIDT_All || DumpType == DIDT_Line) {
273 OS << "\n.debug_line contents:\n";
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000274 for (const auto &CU : compile_units()) {
275 savedAddressByteSize = CU->getAddressByteSize();
Greg Claytonc8c10322016-12-13 18:25:19 +0000276 auto CUDIE = CU->getUnitDIE();
277 if (!CUDIE)
Alexey Samsonov7a18c062015-05-19 21:54:32 +0000278 continue;
Greg Clayton97d22182017-01-13 21:08:18 +0000279 if (auto StmtOffset = toSectionOffset(CUDIE.find(DW_AT_stmt_list))) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000280 DWARFDataExtractor lineData(*DObj, DObj->getLineSection(),
281 isLittleEndian(), savedAddressByteSize);
Alexey Samsonov110d5952014-04-30 00:09:19 +0000282 DWARFDebugLine::LineTable LineTable;
Greg Clayton52fe1f62016-12-14 22:38:08 +0000283 uint32_t Offset = *StmtOffset;
Paul Robinson17536b92017-06-29 16:52:08 +0000284 LineTable.parse(lineData, &Offset);
Alexey Samsonov110d5952014-04-30 00:09:19 +0000285 LineTable.dump(OS);
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000286 }
Benjamin Kramer6dda0322011-09-15 18:02:20 +0000287 }
288 }
Benjamin Kramer07d4b1c2011-09-15 16:57:13 +0000289
David Blaikie65a8efe2015-11-11 19:28:21 +0000290 if (DumpType == DIDT_All || DumpType == DIDT_CUIndex) {
291 OS << "\n.debug_cu_index contents:\n";
David Blaikieb073cb92015-12-02 06:21:34 +0000292 getCUIndex().dump(OS);
David Blaikie65a8efe2015-11-11 19:28:21 +0000293 }
294
David Blaikie51c40282015-11-11 19:40:49 +0000295 if (DumpType == DIDT_All || DumpType == DIDT_TUIndex) {
296 OS << "\n.debug_tu_index contents:\n";
David Blaikieb073cb92015-12-02 06:21:34 +0000297 getTUIndex().dump(OS);
David Blaikie51c40282015-11-11 19:40:49 +0000298 }
299
David Blaikie1d4736e2014-02-24 23:58:54 +0000300 if (DumpType == DIDT_All || DumpType == DIDT_LineDwo) {
301 OS << "\n.debug_line.dwo contents:\n";
302 unsigned stmtOffset = 0;
Rafael Espindolac398e672017-07-19 22:27:28 +0000303 DWARFDataExtractor lineData(*DObj, DObj->getLineDWOSection(),
304 isLittleEndian(), savedAddressByteSize);
Alexey Samsonov110d5952014-04-30 00:09:19 +0000305 DWARFDebugLine::LineTable LineTable;
306 while (LineTable.Prologue.parse(lineData, &stmtOffset)) {
307 LineTable.dump(OS);
308 LineTable.clear();
309 }
David Blaikie1d4736e2014-02-24 23:58:54 +0000310 }
311
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000312 if (DumpType == DIDT_All || DumpType == DIDT_Str) {
313 OS << "\n.debug_str contents:\n";
Rafael Espindolac398e672017-07-19 22:27:28 +0000314 DataExtractor strData(DObj->getStringSection(), isLittleEndian(), 0);
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000315 offset = 0;
316 uint32_t strOffset = 0;
317 while (const char *s = strData.getCStr(&offset)) {
318 OS << format("0x%8.8x: \"%s\"\n", strOffset, s);
319 strOffset = offset;
320 }
Benjamin Kramer07d4b1c2011-09-15 16:57:13 +0000321 }
Alexey Samsonov034e57a2012-08-27 07:17:47 +0000322
David Blaikie66865d62014-01-09 00:13:35 +0000323 if ((DumpType == DIDT_All || DumpType == DIDT_StrDwo) &&
Rafael Espindolac398e672017-07-19 22:27:28 +0000324 !DObj->getStringDWOSection().empty()) {
David Blaikie66865d62014-01-09 00:13:35 +0000325 OS << "\n.debug_str.dwo contents:\n";
Rafael Espindolac398e672017-07-19 22:27:28 +0000326 DataExtractor strDWOData(DObj->getStringDWOSection(), isLittleEndian(), 0);
David Blaikie66865d62014-01-09 00:13:35 +0000327 offset = 0;
328 uint32_t strDWOOffset = 0;
329 while (const char *s = strDWOData.getCStr(&offset)) {
330 OS << format("0x%8.8x: \"%s\"\n", strDWOOffset, s);
331 strDWOOffset = offset;
David Blaikie622dce42014-01-08 23:29:59 +0000332 }
David Blaikie66865d62014-01-09 00:13:35 +0000333 }
David Blaikie622dce42014-01-08 23:29:59 +0000334
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000335 if (DumpType == DIDT_All || DumpType == DIDT_Ranges) {
336 OS << "\n.debug_ranges contents:\n";
337 // In fact, different compile units may have different address byte
338 // sizes, but for simplicity we just use the address byte size of the last
339 // compile unit (there is no easy and fast way to associate address range
340 // list and the compile unit it describes).
Rafael Espindolac398e672017-07-19 22:27:28 +0000341 DWARFDataExtractor rangesData(*DObj, DObj->getRangeSection(),
342 isLittleEndian(), savedAddressByteSize);
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000343 offset = 0;
344 DWARFDebugRangeList rangeList;
Paul Robinson17536b92017-06-29 16:52:08 +0000345 while (rangeList.extract(rangesData, &offset))
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000346 rangeList.dump(OS);
Eric Christopherda4b2192013-01-02 23:52:13 +0000347 }
Eric Christopher962c9082013-01-15 23:56:56 +0000348
Eric Christopher0de53592013-09-25 23:02:36 +0000349 if (DumpType == DIDT_All || DumpType == DIDT_Pubnames)
Rafael Espindolac398e672017-07-19 22:27:28 +0000350 DWARFDebugPubTable(DObj->getPubNamesSection(), isLittleEndian(), false)
George Rimare71e33f2016-12-17 09:10:32 +0000351 .dump("debug_pubnames", OS);
Krzysztof Parzyszek97438dc2013-02-12 16:20:28 +0000352
Eric Christopher4c7e6ba2013-09-25 23:02:41 +0000353 if (DumpType == DIDT_All || DumpType == DIDT_Pubtypes)
Rafael Espindolac398e672017-07-19 22:27:28 +0000354 DWARFDebugPubTable(DObj->getPubTypesSection(), isLittleEndian(), false)
George Rimare71e33f2016-12-17 09:10:32 +0000355 .dump("debug_pubtypes", OS);
Eric Christopher4c7e6ba2013-09-25 23:02:41 +0000356
David Blaikieecd21ff2013-09-24 19:50:00 +0000357 if (DumpType == DIDT_All || DumpType == DIDT_GnuPubnames)
Rafael Espindolac398e672017-07-19 22:27:28 +0000358 DWARFDebugPubTable(DObj->getGnuPubNamesSection(), isLittleEndian(),
George Rimare71e33f2016-12-17 09:10:32 +0000359 true /* GnuStyle */)
360 .dump("debug_gnu_pubnames", OS);
David Blaikieecd21ff2013-09-24 19:50:00 +0000361
362 if (DumpType == DIDT_All || DumpType == DIDT_GnuPubtypes)
Rafael Espindolac398e672017-07-19 22:27:28 +0000363 DWARFDebugPubTable(DObj->getGnuPubTypesSection(), isLittleEndian(),
George Rimare71e33f2016-12-17 09:10:32 +0000364 true /* GnuStyle */)
365 .dump("debug_gnu_pubtypes", OS);
David Blaikie404d3042013-09-19 23:01:29 +0000366
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000367 if (DumpType == DIDT_All || DumpType == DIDT_StrOffsets)
Rafael Espindolac398e672017-07-19 22:27:28 +0000368 dumpStringOffsetsSection(
369 OS, "debug_str_offsets", *DObj, DObj->getStringOffsetSection(),
370 DObj->getStringSection(), isLittleEndian(), getMaxVersion());
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000371
372 if (DumpType == DIDT_All || DumpType == DIDT_StrOffsetsDwo) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000373 dumpStringOffsetsSection(
374 OS, "debug_str_offsets.dwo", *DObj, DObj->getStringOffsetDWOSection(),
375 DObj->getStringDWOSection(), isLittleEndian(), getMaxVersion());
David Blaikie66865d62014-01-09 00:13:35 +0000376 }
Frederic Risse837ec22014-11-14 16:15:53 +0000377
George Rimar4f82df52016-09-23 11:01:53 +0000378 if ((DumpType == DIDT_All || DumpType == DIDT_GdbIndex) &&
Rafael Espindolac398e672017-07-19 22:27:28 +0000379 !DObj->getGdbIndexSection().empty()) {
George Rimar4f82df52016-09-23 11:01:53 +0000380 OS << "\n.gnu_index contents:\n";
381 getGdbIndex().dump(OS);
382 }
383
Frederic Risse837ec22014-11-14 16:15:53 +0000384 if (DumpType == DIDT_All || DumpType == DIDT_AppleNames)
Rafael Espindolac398e672017-07-19 22:27:28 +0000385 dumpAccelSection(OS, "apple_names", *DObj, DObj->getAppleNamesSection(),
386 DObj->getStringSection(), isLittleEndian());
Frederic Risse837ec22014-11-14 16:15:53 +0000387
388 if (DumpType == DIDT_All || DumpType == DIDT_AppleTypes)
Rafael Espindolac398e672017-07-19 22:27:28 +0000389 dumpAccelSection(OS, "apple_types", *DObj, DObj->getAppleTypesSection(),
390 DObj->getStringSection(), isLittleEndian());
Frederic Risse837ec22014-11-14 16:15:53 +0000391
392 if (DumpType == DIDT_All || DumpType == DIDT_AppleNamespaces)
Rafael Espindolac398e672017-07-19 22:27:28 +0000393 dumpAccelSection(OS, "apple_namespaces", *DObj,
394 DObj->getAppleNamespacesSection(),
395 DObj->getStringSection(), isLittleEndian());
Frederic Risse837ec22014-11-14 16:15:53 +0000396
397 if (DumpType == DIDT_All || DumpType == DIDT_AppleObjC)
Rafael Espindolac398e672017-07-19 22:27:28 +0000398 dumpAccelSection(OS, "apple_objc", *DObj, DObj->getAppleObjCSection(),
399 DObj->getStringSection(), isLittleEndian());
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000400}
401
David Blaikie15d85fc2017-05-23 06:48:53 +0000402DWARFCompileUnit *DWARFContext::getDWOCompileUnitForHash(uint64_t Hash) {
403 // FIXME: Improve this for the case where this DWO file is really a DWP file
404 // with an index - use the index for lookup instead of a linear search.
405 for (const auto &DWOCU : dwo_compile_units())
406 if (DWOCU->getDWOId() == Hash)
407 return DWOCU.get();
408 return nullptr;
409}
410
Greg Claytonc7695a82017-05-02 20:28:33 +0000411DWARFDie DWARFContext::getDIEForOffset(uint32_t Offset) {
412 parseCompileUnits();
413 if (auto *CU = CUs.getUnitForOffset(Offset))
414 return CU->getDIEForOffset(Offset);
415 return DWARFDie();
416}
417
Greg Claytonc7695a82017-05-02 20:28:33 +0000418bool DWARFContext::verify(raw_ostream &OS, DIDumpType DumpType) {
419 bool Success = true;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000420 DWARFVerifier verifier(OS, *this);
Greg Claytonc7695a82017-05-02 20:28:33 +0000421 if (DumpType == DIDT_All || DumpType == DIDT_Info) {
Greg Claytonb8c162b2017-05-03 16:02:29 +0000422 if (!verifier.handleDebugInfo())
Greg Claytonc7695a82017-05-02 20:28:33 +0000423 Success = false;
Greg Clayton48432cf2017-05-01 22:07:02 +0000424 }
Greg Clayton67070462017-05-02 22:48:52 +0000425 if (DumpType == DIDT_All || DumpType == DIDT_Line) {
Greg Claytonb8c162b2017-05-03 16:02:29 +0000426 if (!verifier.handleDebugLine())
Greg Clayton67070462017-05-02 22:48:52 +0000427 Success = false;
428 }
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000429 if (DumpType == DIDT_All || DumpType == DIDT_AppleNames) {
430 if (!verifier.handleAppleNames())
431 Success = false;
432 }
Greg Clayton48432cf2017-05-01 22:07:02 +0000433 return Success;
434}
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000435
David Blaikie82641be2015-11-17 00:39:55 +0000436const DWARFUnitIndex &DWARFContext::getCUIndex() {
437 if (CUIndex)
438 return *CUIndex;
439
Rafael Espindolac398e672017-07-19 22:27:28 +0000440 DataExtractor CUIndexData(DObj->getCUIndexSection(), isLittleEndian(), 0);
David Blaikie82641be2015-11-17 00:39:55 +0000441
David Blaikieb073cb92015-12-02 06:21:34 +0000442 CUIndex = llvm::make_unique<DWARFUnitIndex>(DW_SECT_INFO);
David Blaikie82641be2015-11-17 00:39:55 +0000443 CUIndex->parse(CUIndexData);
444 return *CUIndex;
445}
446
447const DWARFUnitIndex &DWARFContext::getTUIndex() {
448 if (TUIndex)
449 return *TUIndex;
450
Rafael Espindolac398e672017-07-19 22:27:28 +0000451 DataExtractor TUIndexData(DObj->getTUIndexSection(), isLittleEndian(), 0);
David Blaikie82641be2015-11-17 00:39:55 +0000452
David Blaikieb073cb92015-12-02 06:21:34 +0000453 TUIndex = llvm::make_unique<DWARFUnitIndex>(DW_SECT_TYPES);
David Blaikie82641be2015-11-17 00:39:55 +0000454 TUIndex->parse(TUIndexData);
455 return *TUIndex;
456}
457
George Rimar4f82df52016-09-23 11:01:53 +0000458DWARFGdbIndex &DWARFContext::getGdbIndex() {
459 if (GdbIndex)
460 return *GdbIndex;
461
Rafael Espindolac398e672017-07-19 22:27:28 +0000462 DataExtractor GdbIndexData(DObj->getGdbIndexSection(), true /*LE*/, 0);
George Rimar4f82df52016-09-23 11:01:53 +0000463 GdbIndex = llvm::make_unique<DWARFGdbIndex>();
464 GdbIndex->parse(GdbIndexData);
465 return *GdbIndex;
466}
467
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000468const DWARFDebugAbbrev *DWARFContext::getDebugAbbrev() {
469 if (Abbrev)
470 return Abbrev.get();
471
Rafael Espindolac398e672017-07-19 22:27:28 +0000472 DataExtractor abbrData(DObj->getAbbrevSection(), isLittleEndian(), 0);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000473
474 Abbrev.reset(new DWARFDebugAbbrev());
Alexey Samsonov4316df52014-04-25 21:10:56 +0000475 Abbrev->extract(abbrData);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000476 return Abbrev.get();
477}
478
Eric Christopherda4b2192013-01-02 23:52:13 +0000479const DWARFDebugAbbrev *DWARFContext::getDebugAbbrevDWO() {
480 if (AbbrevDWO)
481 return AbbrevDWO.get();
482
Rafael Espindolac398e672017-07-19 22:27:28 +0000483 DataExtractor abbrData(DObj->getAbbrevDWOSection(), isLittleEndian(), 0);
Eric Christopherda4b2192013-01-02 23:52:13 +0000484 AbbrevDWO.reset(new DWARFDebugAbbrev());
Alexey Samsonov4316df52014-04-25 21:10:56 +0000485 AbbrevDWO->extract(abbrData);
Eric Christopherda4b2192013-01-02 23:52:13 +0000486 return AbbrevDWO.get();
487}
488
David Blaikie18e73502013-06-19 21:37:13 +0000489const DWARFDebugLoc *DWARFContext::getDebugLoc() {
490 if (Loc)
491 return Loc.get();
492
Paul Robinson17536b92017-06-29 16:52:08 +0000493 Loc.reset(new DWARFDebugLoc);
David Blaikie18e73502013-06-19 21:37:13 +0000494 // assume all compile units have the same address byte size
Paul Robinson17536b92017-06-29 16:52:08 +0000495 if (getNumCompileUnits()) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000496 DWARFDataExtractor LocData(*DObj, DObj->getLocSection(), isLittleEndian(),
Paul Robinson17536b92017-06-29 16:52:08 +0000497 getCompileUnitAtIndex(0)->getAddressByteSize());
498 Loc->parse(LocData);
499 }
David Blaikie18e73502013-06-19 21:37:13 +0000500 return Loc.get();
501}
502
David Blaikie9c550ac2014-03-25 01:44:02 +0000503const DWARFDebugLocDWO *DWARFContext::getDebugLocDWO() {
504 if (LocDWO)
505 return LocDWO.get();
506
Rafael Espindolac398e672017-07-19 22:27:28 +0000507 DataExtractor LocData(DObj->getLocDWOSection().Data, isLittleEndian(), 0);
David Blaikie9c550ac2014-03-25 01:44:02 +0000508 LocDWO.reset(new DWARFDebugLocDWO());
509 LocDWO->parse(LocData);
510 return LocDWO.get();
511}
512
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000513const DWARFDebugAranges *DWARFContext::getDebugAranges() {
514 if (Aranges)
515 return Aranges.get();
516
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000517 Aranges.reset(new DWARFDebugAranges());
Alexey Samsonova1694c12012-11-16 08:36:25 +0000518 Aranges->generate(this);
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000519 return Aranges.get();
520}
521
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000522const DWARFDebugFrame *DWARFContext::getDebugFrame() {
523 if (DebugFrame)
524 return DebugFrame.get();
525
526 // There's a "bug" in the DWARFv3 standard with respect to the target address
527 // size within debug frame sections. While DWARF is supposed to be independent
528 // of its container, FDEs have fields with size being "target address size",
529 // which isn't specified in DWARF in general. It's only specified for CUs, but
530 // .eh_frame can appear without a .debug_info section. Follow the example of
531 // other tools (libdwarf) and extract this from the container (ObjectFile
532 // provides this information). This problem is fixed in DWARFv4
533 // See this dwarf-discuss discussion for more details:
534 // http://lists.dwarfstd.org/htdig.cgi/dwarf-discuss-dwarfstd.org/2011-December/001173.html
Rafael Espindolac398e672017-07-19 22:27:28 +0000535 DataExtractor debugFrameData(DObj->getDebugFrameSection(), isLittleEndian(),
536 DObj->getAddressSize());
Igor Laevsky03a670c2016-01-26 15:09:42 +0000537 DebugFrame.reset(new DWARFDebugFrame(false /* IsEH */));
538 DebugFrame->parse(debugFrameData);
539 return DebugFrame.get();
540}
541
542const DWARFDebugFrame *DWARFContext::getEHFrame() {
543 if (EHFrame)
544 return EHFrame.get();
545
Rafael Espindolac398e672017-07-19 22:27:28 +0000546 DataExtractor debugFrameData(DObj->getEHFrameSection(), isLittleEndian(),
547 DObj->getAddressSize());
Igor Laevsky03a670c2016-01-26 15:09:42 +0000548 DebugFrame.reset(new DWARFDebugFrame(true /* IsEH */));
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000549 DebugFrame->parse(debugFrameData);
550 return DebugFrame.get();
551}
552
Amjad Aboude59cc3e2015-11-12 09:38:54 +0000553const DWARFDebugMacro *DWARFContext::getDebugMacro() {
554 if (Macro)
555 return Macro.get();
556
Rafael Espindolac398e672017-07-19 22:27:28 +0000557 DataExtractor MacinfoData(DObj->getMacinfoSection(), isLittleEndian(), 0);
Amjad Aboude59cc3e2015-11-12 09:38:54 +0000558 Macro.reset(new DWARFDebugMacro());
559 Macro->parse(MacinfoData);
560 return Macro.get();
561}
562
Eric Christopher494109b2012-10-16 23:46:25 +0000563const DWARFLineTable *
Alexey Samsonov7a18c062015-05-19 21:54:32 +0000564DWARFContext::getLineTableForUnit(DWARFUnit *U) {
Benjamin Kramer679e1752011-09-15 20:43:18 +0000565 if (!Line)
Paul Robinson17536b92017-06-29 16:52:08 +0000566 Line.reset(new DWARFDebugLine);
David Blaikiec4e2bed2015-11-17 21:08:05 +0000567
Greg Claytonc8c10322016-12-13 18:25:19 +0000568 auto UnitDIE = U->getUnitDIE();
569 if (!UnitDIE)
Alexey Samsonov7a18c062015-05-19 21:54:32 +0000570 return nullptr;
David Blaikiec4e2bed2015-11-17 21:08:05 +0000571
Greg Clayton97d22182017-01-13 21:08:18 +0000572 auto Offset = toSectionOffset(UnitDIE.find(DW_AT_stmt_list));
Greg Clayton52fe1f62016-12-14 22:38:08 +0000573 if (!Offset)
Craig Topper2617dcc2014-04-15 06:32:26 +0000574 return nullptr; // No line table for this compile unit.
Benjamin Kramer5acab502011-09-15 02:12:05 +0000575
Greg Clayton52fe1f62016-12-14 22:38:08 +0000576 uint32_t stmtOffset = *Offset + U->getLineTableOffset();
Benjamin Kramer679e1752011-09-15 20:43:18 +0000577 // See if the line table is cached.
Eric Christopher494109b2012-10-16 23:46:25 +0000578 if (const DWARFLineTable *lt = Line->getLineTable(stmtOffset))
Benjamin Kramer679e1752011-09-15 20:43:18 +0000579 return lt;
580
Greg Clayton48ff66a2017-05-04 18:29:44 +0000581 // Make sure the offset is good before we try to parse.
Paul Robinson17536b92017-06-29 16:52:08 +0000582 if (stmtOffset >= U->getLineSection().Data.size())
Greg Clayton48ff66a2017-05-04 18:29:44 +0000583 return nullptr;
584
Benjamin Kramer679e1752011-09-15 20:43:18 +0000585 // We have to parse it first.
Rafael Espindolac398e672017-07-19 22:27:28 +0000586 DWARFDataExtractor lineData(*DObj, U->getLineSection(), isLittleEndian(),
Paul Robinson17536b92017-06-29 16:52:08 +0000587 U->getAddressByteSize());
Benjamin Kramer679e1752011-09-15 20:43:18 +0000588 return Line->getOrParseLineTable(lineData, stmtOffset);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000589}
590
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000591void DWARFContext::parseCompileUnits() {
Rafael Espindolac398e672017-07-19 22:27:28 +0000592 CUs.parse(*this, DObj->getInfoSection());
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000593}
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000594
David Blaikie03c089c2013-09-23 22:44:47 +0000595void DWARFContext::parseTypeUnits() {
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000596 if (!TUs.empty())
597 return;
Rafael Espindolac398e672017-07-19 22:27:28 +0000598 DObj->forEachTypesSections([&](const DWARFSection &S) {
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000599 TUs.emplace_back();
Rafael Espindola5e5dfa12017-07-12 21:08:24 +0000600 TUs.back().parse(*this, S);
601 });
David Blaikie03c089c2013-09-23 22:44:47 +0000602}
603
Eric Christopherda4b2192013-01-02 23:52:13 +0000604void DWARFContext::parseDWOCompileUnits() {
Rafael Espindolac398e672017-07-19 22:27:28 +0000605 DWOCUs.parseDWO(*this, DObj->getInfoDWOSection());
Eric Christopherda4b2192013-01-02 23:52:13 +0000606}
607
David Blaikie92d9d622014-01-09 05:08:24 +0000608void DWARFContext::parseDWOTypeUnits() {
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000609 if (!DWOTUs.empty())
610 return;
Rafael Espindolac398e672017-07-19 22:27:28 +0000611 DObj->forEachTypesDWOSections([&](const DWARFSection &S) {
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000612 DWOTUs.emplace_back();
Rafael Espindola5e5dfa12017-07-12 21:08:24 +0000613 DWOTUs.back().parseDWO(*this, S);
614 });
David Blaikie92d9d622014-01-09 05:08:24 +0000615}
616
Alexey Samsonov45be7932012-08-30 07:49:50 +0000617DWARFCompileUnit *DWARFContext::getCompileUnitForOffset(uint32_t Offset) {
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000618 parseCompileUnits();
Frederic Riss4e126a02014-09-15 07:50:27 +0000619 return CUs.getUnitForOffset(Offset);
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000620}
621
Alexey Samsonov45be7932012-08-30 07:49:50 +0000622DWARFCompileUnit *DWARFContext::getCompileUnitForAddress(uint64_t Address) {
Benjamin Kramer112ec172011-09-15 21:59:13 +0000623 // First, get the offset of the compile unit.
Alexey Samsonov45be7932012-08-30 07:49:50 +0000624 uint32_t CUOffset = getDebugAranges()->findAddress(Address);
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000625 // Retrieve the compile unit.
Alexey Samsonov45be7932012-08-30 07:49:50 +0000626 return getCompileUnitForOffset(CUOffset);
627}
628
David Blaikieefc4eba2017-02-06 20:19:02 +0000629static bool getFunctionNameAndStartLineForAddress(DWARFCompileUnit *CU,
630 uint64_t Address,
631 FunctionNameKind Kind,
632 std::string &FunctionName,
633 uint32_t &StartLine) {
Alexey Samsonovd0109992014-04-18 21:36:39 +0000634 // The address may correspond to instruction in some inlined function,
635 // so we have to build the chain of inlined functions and take the
David Blaikieefc4eba2017-02-06 20:19:02 +0000636 // name of the topmost function in it.
Greg Claytonc8c10322016-12-13 18:25:19 +0000637 SmallVector<DWARFDie, 4> InlinedChain;
638 CU->getInlinedChainForAddress(Address, InlinedChain);
David Blaikieefc4eba2017-02-06 20:19:02 +0000639 if (InlinedChain.empty())
Alexey Samsonovd0109992014-04-18 21:36:39 +0000640 return false;
David Blaikieefc4eba2017-02-06 20:19:02 +0000641
642 const DWARFDie &DIE = InlinedChain[0];
643 bool FoundResult = false;
644 const char *Name = nullptr;
645 if (Kind != FunctionNameKind::None && (Name = DIE.getSubroutineName(Kind))) {
Alexey Samsonovd0109992014-04-18 21:36:39 +0000646 FunctionName = Name;
David Blaikieefc4eba2017-02-06 20:19:02 +0000647 FoundResult = true;
Alexey Samsonovd0109992014-04-18 21:36:39 +0000648 }
David Blaikieefc4eba2017-02-06 20:19:02 +0000649 if (auto DeclLineResult = DIE.getDeclLine()) {
650 StartLine = DeclLineResult;
651 FoundResult = true;
652 }
653
654 return FoundResult;
Alexey Samsonovd0109992014-04-18 21:36:39 +0000655}
656
Alexey Samsonov45be7932012-08-30 07:49:50 +0000657DILineInfo DWARFContext::getLineInfoForAddress(uint64_t Address,
Alexey Samsonovdce67342014-05-15 21:24:32 +0000658 DILineInfoSpecifier Spec) {
Alexey Samsonovd0109992014-04-18 21:36:39 +0000659 DILineInfo Result;
660
Alexey Samsonov45be7932012-08-30 07:49:50 +0000661 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
662 if (!CU)
Alexey Samsonovd0109992014-04-18 21:36:39 +0000663 return Result;
David Blaikieefc4eba2017-02-06 20:19:02 +0000664 getFunctionNameAndStartLineForAddress(CU, Address, Spec.FNKind,
665 Result.FunctionName,
666 Result.StartLine);
Alexey Samsonovdce67342014-05-15 21:24:32 +0000667 if (Spec.FLIKind != FileLineInfoKind::None) {
Frederic Riss101b5e22014-09-19 15:11:51 +0000668 if (const DWARFLineTable *LineTable = getLineTableForUnit(CU))
669 LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(),
670 Spec.FLIKind, Result);
Alexey Samsonovf4462fa2012-07-02 05:54:45 +0000671 }
Alexey Samsonovd0109992014-04-18 21:36:39 +0000672 return Result;
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000673}
David Blaikiea379b1812011-12-20 02:50:00 +0000674
Alexey Samsonovdce67342014-05-15 21:24:32 +0000675DILineInfoTable
676DWARFContext::getLineInfoForAddressRange(uint64_t Address, uint64_t Size,
677 DILineInfoSpecifier Spec) {
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000678 DILineInfoTable Lines;
679 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
680 if (!CU)
681 return Lines;
682
683 std::string FunctionName = "<invalid>";
David Blaikieefc4eba2017-02-06 20:19:02 +0000684 uint32_t StartLine = 0;
685 getFunctionNameAndStartLineForAddress(CU, Address, Spec.FNKind, FunctionName,
686 StartLine);
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000687
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000688 // If the Specifier says we don't need FileLineInfo, just
689 // return the top-most function at the starting address.
Alexey Samsonovdce67342014-05-15 21:24:32 +0000690 if (Spec.FLIKind == FileLineInfoKind::None) {
Alexey Samsonovd0109992014-04-18 21:36:39 +0000691 DILineInfo Result;
692 Result.FunctionName = FunctionName;
David Blaikieefc4eba2017-02-06 20:19:02 +0000693 Result.StartLine = StartLine;
Alexey Samsonovd0109992014-04-18 21:36:39 +0000694 Lines.push_back(std::make_pair(Address, Result));
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000695 return Lines;
696 }
697
Frederic Rissec8a5ba2014-09-04 06:14:40 +0000698 const DWARFLineTable *LineTable = getLineTableForUnit(CU);
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000699
700 // Get the index of row we're looking for in the line table.
701 std::vector<uint32_t> RowVector;
702 if (!LineTable->lookupAddressRange(Address, Size, RowVector))
703 return Lines;
704
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000705 for (uint32_t RowIndex : RowVector) {
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000706 // Take file number and line/column from the row.
707 const DWARFDebugLine::Row &Row = LineTable->Rows[RowIndex];
Alexey Samsonovd0109992014-04-18 21:36:39 +0000708 DILineInfo Result;
Frederic Riss101b5e22014-09-19 15:11:51 +0000709 LineTable->getFileNameByIndex(Row.File, CU->getCompilationDir(),
710 Spec.FLIKind, Result.FileName);
Alexey Samsonovd0109992014-04-18 21:36:39 +0000711 Result.FunctionName = FunctionName;
712 Result.Line = Row.Line;
713 Result.Column = Row.Column;
David Blaikieefc4eba2017-02-06 20:19:02 +0000714 Result.StartLine = StartLine;
Alexey Samsonovd0109992014-04-18 21:36:39 +0000715 Lines.push_back(std::make_pair(Row.Address, Result));
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000716 }
717
718 return Lines;
719}
720
Alexey Samsonovdce67342014-05-15 21:24:32 +0000721DIInliningInfo
722DWARFContext::getInliningInfoForAddress(uint64_t Address,
723 DILineInfoSpecifier Spec) {
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000724 DIInliningInfo InliningInfo;
725
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000726 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
727 if (!CU)
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000728 return InliningInfo;
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000729
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000730 const DWARFLineTable *LineTable = nullptr;
Greg Claytonc8c10322016-12-13 18:25:19 +0000731 SmallVector<DWARFDie, 4> InlinedChain;
732 CU->getInlinedChainForAddress(Address, InlinedChain);
733 if (InlinedChain.size() == 0) {
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000734 // If there is no DIE for address (e.g. it is in unavailable .dwo file),
735 // try to at least get file/line info from symbol table.
Alexey Samsonovdce67342014-05-15 21:24:32 +0000736 if (Spec.FLIKind != FileLineInfoKind::None) {
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000737 DILineInfo Frame;
Frederic Rissec8a5ba2014-09-04 06:14:40 +0000738 LineTable = getLineTableForUnit(CU);
Frederic Riss101b5e22014-09-19 15:11:51 +0000739 if (LineTable &&
740 LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(),
741 Spec.FLIKind, Frame))
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000742 InliningInfo.addFrame(Frame);
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000743 }
744 return InliningInfo;
745 }
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000746
Dehao Chenef700d52017-04-17 20:10:39 +0000747 uint32_t CallFile = 0, CallLine = 0, CallColumn = 0, CallDiscriminator = 0;
Greg Claytonc8c10322016-12-13 18:25:19 +0000748 for (uint32_t i = 0, n = InlinedChain.size(); i != n; i++) {
749 DWARFDie &FunctionDIE = InlinedChain[i];
Alexey Samsonovd0109992014-04-18 21:36:39 +0000750 DILineInfo Frame;
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000751 // Get function name if necessary.
Greg Claytonc8c10322016-12-13 18:25:19 +0000752 if (const char *Name = FunctionDIE.getSubroutineName(Spec.FNKind))
Alexey Samsonovdce67342014-05-15 21:24:32 +0000753 Frame.FunctionName = Name;
David Blaikieefc4eba2017-02-06 20:19:02 +0000754 if (auto DeclLineResult = FunctionDIE.getDeclLine())
755 Frame.StartLine = DeclLineResult;
Alexey Samsonovdce67342014-05-15 21:24:32 +0000756 if (Spec.FLIKind != FileLineInfoKind::None) {
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000757 if (i == 0) {
758 // For the topmost frame, initialize the line table of this
759 // compile unit and fetch file/line info from it.
Frederic Rissec8a5ba2014-09-04 06:14:40 +0000760 LineTable = getLineTableForUnit(CU);
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000761 // For the topmost routine, get file/line info from line table.
Frederic Riss101b5e22014-09-19 15:11:51 +0000762 if (LineTable)
763 LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(),
764 Spec.FLIKind, Frame);
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000765 } else {
766 // Otherwise, use call file, call line and call column from
767 // previous DIE in inlined chain.
Frederic Riss101b5e22014-09-19 15:11:51 +0000768 if (LineTable)
769 LineTable->getFileNameByIndex(CallFile, CU->getCompilationDir(),
770 Spec.FLIKind, Frame.FileName);
Alexey Samsonovd0109992014-04-18 21:36:39 +0000771 Frame.Line = CallLine;
772 Frame.Column = CallColumn;
Dehao Chenef700d52017-04-17 20:10:39 +0000773 Frame.Discriminator = CallDiscriminator;
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000774 }
775 // Get call file/line/column of a current DIE.
776 if (i + 1 < n) {
Dehao Chenef700d52017-04-17 20:10:39 +0000777 FunctionDIE.getCallerFrame(CallFile, CallLine, CallColumn,
778 CallDiscriminator);
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000779 }
780 }
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000781 InliningInfo.addFrame(Frame);
782 }
783 return InliningInfo;
784}
785
David Blaikief9803fb2017-05-23 00:30:42 +0000786std::shared_ptr<DWARFContext>
787DWARFContext::getDWOContext(StringRef AbsolutePath) {
David Blaikie15d85fc2017-05-23 06:48:53 +0000788 if (auto S = DWP.lock()) {
David Blaikief9803fb2017-05-23 00:30:42 +0000789 DWARFContext *Ctxt = S->Context.get();
790 return std::shared_ptr<DWARFContext>(std::move(S), Ctxt);
791 }
792
David Blaikie15d85fc2017-05-23 06:48:53 +0000793 std::weak_ptr<DWOFile> *Entry = &DWOFiles[AbsolutePath];
794
795 if (auto S = Entry->lock()) {
796 DWARFContext *Ctxt = S->Context.get();
797 return std::shared_ptr<DWARFContext>(std::move(S), Ctxt);
798 }
799
800 SmallString<128> DWPName;
801 Expected<OwningBinary<ObjectFile>> Obj = [&] {
802 if (!CheckedForDWP) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000803 (DObj->getFileName() + ".dwp").toVector(DWPName);
David Blaikie15d85fc2017-05-23 06:48:53 +0000804 auto Obj = object::ObjectFile::createObjectFile(DWPName);
805 if (Obj) {
806 Entry = &DWP;
807 return Obj;
808 } else {
809 CheckedForDWP = true;
810 // TODO: Should this error be handled (maybe in a high verbosity mode)
811 // before falling back to .dwo files?
812 consumeError(Obj.takeError());
813 }
814 }
815
816 return object::ObjectFile::createObjectFile(AbsolutePath);
817 }();
818
David Blaikief9803fb2017-05-23 00:30:42 +0000819 if (!Obj) {
820 // TODO: Actually report errors helpfully.
821 consumeError(Obj.takeError());
822 return nullptr;
823 }
David Blaikie15d85fc2017-05-23 06:48:53 +0000824
825 auto S = std::make_shared<DWOFile>();
David Blaikief9803fb2017-05-23 00:30:42 +0000826 S->File = std::move(Obj.get());
Rafael Espindolac398e672017-07-19 22:27:28 +0000827 S->Context = DWARFContext::create(*S->File.getBinary());
David Blaikie15d85fc2017-05-23 06:48:53 +0000828 *Entry = S;
David Blaikief9803fb2017-05-23 00:30:42 +0000829 auto *Ctxt = S->Context.get();
830 return std::shared_ptr<DWARFContext>(std::move(S), Ctxt);
831}
832
George Rimar702dac62017-04-12 08:59:15 +0000833static Error createError(const Twine &Reason, llvm::Error E) {
834 return make_error<StringError>(Reason + toString(std::move(E)),
835 inconvertibleErrorCode());
836}
837
George Rimara25d3292017-05-27 18:10:23 +0000838/// SymInfo contains information about symbol: it's address
839/// and section index which is -1LL for absolute symbols.
840struct SymInfo {
841 uint64_t Address;
842 uint64_t SectionIndex;
843};
844
845/// Returns the address of symbol relocation used against and a section index.
846/// Used for futher relocations computation. Symbol's section load address is
847static Expected<SymInfo> getSymbolInfo(const object::ObjectFile &Obj,
848 const RelocationRef &Reloc,
849 const LoadedObjectInfo *L,
850 std::map<SymbolRef, SymInfo> &Cache) {
851 SymInfo Ret = {0, (uint64_t)-1LL};
George Rimar702dac62017-04-12 08:59:15 +0000852 object::section_iterator RSec = Obj.section_end();
853 object::symbol_iterator Sym = Reloc.getSymbol();
854
George Rimara25d3292017-05-27 18:10:23 +0000855 std::map<SymbolRef, SymInfo>::iterator CacheIt = Cache.end();
George Rimar702dac62017-04-12 08:59:15 +0000856 // First calculate the address of the symbol or section as it appears
857 // in the object file
858 if (Sym != Obj.symbol_end()) {
George Rimar958b01a2017-05-15 11:45:28 +0000859 bool New;
George Rimara25d3292017-05-27 18:10:23 +0000860 std::tie(CacheIt, New) = Cache.insert({*Sym, {0, 0}});
George Rimar958b01a2017-05-15 11:45:28 +0000861 if (!New)
862 return CacheIt->second;
863
George Rimar702dac62017-04-12 08:59:15 +0000864 Expected<uint64_t> SymAddrOrErr = Sym->getAddress();
865 if (!SymAddrOrErr)
George Rimar1af3cb22017-06-28 08:21:19 +0000866 return createError("failed to compute symbol address: ",
George Rimar702dac62017-04-12 08:59:15 +0000867 SymAddrOrErr.takeError());
868
869 // Also remember what section this symbol is in for later
870 auto SectOrErr = Sym->getSection();
871 if (!SectOrErr)
George Rimar1af3cb22017-06-28 08:21:19 +0000872 return createError("failed to get symbol section: ",
George Rimar702dac62017-04-12 08:59:15 +0000873 SectOrErr.takeError());
874
875 RSec = *SectOrErr;
George Rimara25d3292017-05-27 18:10:23 +0000876 Ret.Address = *SymAddrOrErr;
George Rimar702dac62017-04-12 08:59:15 +0000877 } else if (auto *MObj = dyn_cast<MachOObjectFile>(&Obj)) {
878 RSec = MObj->getRelocationSection(Reloc.getRawDataRefImpl());
George Rimara25d3292017-05-27 18:10:23 +0000879 Ret.Address = RSec->getAddress();
George Rimar702dac62017-04-12 08:59:15 +0000880 }
881
George Rimara25d3292017-05-27 18:10:23 +0000882 if (RSec != Obj.section_end())
883 Ret.SectionIndex = RSec->getIndex();
884
George Rimar702dac62017-04-12 08:59:15 +0000885 // If we are given load addresses for the sections, we need to adjust:
886 // SymAddr = (Address of Symbol Or Section in File) -
887 // (Address of Section in File) +
888 // (Load Address of Section)
889 // RSec is now either the section being targeted or the section
890 // containing the symbol being targeted. In either case,
891 // we need to perform the same computation.
892 if (L && RSec != Obj.section_end())
893 if (uint64_t SectionLoadAddress = L->getSectionLoadAddress(*RSec))
George Rimara25d3292017-05-27 18:10:23 +0000894 Ret.Address += SectionLoadAddress - RSec->getAddress();
George Rimar958b01a2017-05-15 11:45:28 +0000895
896 if (CacheIt != Cache.end())
897 CacheIt->second = Ret;
898
George Rimar702dac62017-04-12 08:59:15 +0000899 return Ret;
900}
901
902static bool isRelocScattered(const object::ObjectFile &Obj,
903 const RelocationRef &Reloc) {
George Rimard4998b02017-04-13 09:52:50 +0000904 const MachOObjectFile *MachObj = dyn_cast<MachOObjectFile>(&Obj);
905 if (!MachObj)
George Rimar702dac62017-04-12 08:59:15 +0000906 return false;
907 // MachO also has relocations that point to sections and
908 // scattered relocations.
George Rimar702dac62017-04-12 08:59:15 +0000909 auto RelocInfo = MachObj->getRelocation(Reloc.getRawDataRefImpl());
910 return MachObj->isRelocationScattered(RelocInfo);
911}
912
Rafael Espindolac398e672017-07-19 22:27:28 +0000913ErrorPolicy DWARFContext::defaultErrorHandler(Error E) {
George Rimar1af3cb22017-06-28 08:21:19 +0000914 errs() << "error: " + toString(std::move(E)) << '\n';
915 return ErrorPolicy::Continue;
916}
917
Rafael Espindolac398e672017-07-19 22:27:28 +0000918class DWARFObjInMemory final : public DWARFObject {
919 bool IsLittleEndian;
920 uint8_t AddressSize;
921 StringRef FileName;
Keno Fischerc780e8e2015-05-21 21:24:32 +0000922
Rafael Espindolac398e672017-07-19 22:27:28 +0000923 using TypeSectionMap = MapVector<object::SectionRef, DWARFSectionMap,
924 std::map<object::SectionRef, unsigned>>;
Eric Christopher7370b552012-11-12 21:40:38 +0000925
Rafael Espindolac398e672017-07-19 22:27:28 +0000926 TypeSectionMap TypesSections;
927 TypeSectionMap TypesDWOSections;
928
929 DWARFSectionMap InfoSection;
930 DWARFSectionMap LocSection;
931 DWARFSectionMap LineSection;
932 DWARFSectionMap RangeSection;
933 DWARFSectionMap StringOffsetSection;
934 DWARFSectionMap InfoDWOSection;
935 DWARFSectionMap LineDWOSection;
936 DWARFSectionMap LocDWOSection;
937 DWARFSectionMap StringOffsetDWOSection;
938 DWARFSectionMap RangeDWOSection;
939 DWARFSectionMap AddrSection;
940 DWARFSectionMap AppleNamesSection;
941 DWARFSectionMap AppleTypesSection;
942 DWARFSectionMap AppleNamespacesSection;
943 DWARFSectionMap AppleObjCSection;
944
945 DWARFSectionMap *mapNameToDWARFSection(StringRef Name) {
946 return StringSwitch<DWARFSectionMap *>(Name)
947 .Case("debug_info", &InfoSection)
948 .Case("debug_loc", &LocSection)
949 .Case("debug_line", &LineSection)
950 .Case("debug_str_offsets", &StringOffsetSection)
951 .Case("debug_ranges", &RangeSection)
952 .Case("debug_info.dwo", &InfoDWOSection)
953 .Case("debug_loc.dwo", &LocDWOSection)
954 .Case("debug_line.dwo", &LineDWOSection)
955 .Case("debug_str_offsets.dwo", &StringOffsetDWOSection)
956 .Case("debug_addr", &AddrSection)
957 .Case("apple_names", &AppleNamesSection)
958 .Case("apple_types", &AppleTypesSection)
959 .Case("apple_namespaces", &AppleNamespacesSection)
960 .Case("apple_namespac", &AppleNamespacesSection)
961 .Case("apple_objc", &AppleObjCSection)
962 .Default(nullptr);
963 }
964
965 StringRef AbbrevSection;
966 StringRef ARangeSection;
967 StringRef DebugFrameSection;
968 StringRef EHFrameSection;
969 StringRef StringSection;
970 StringRef MacinfoSection;
971 StringRef PubNamesSection;
972 StringRef PubTypesSection;
973 StringRef GnuPubNamesSection;
974 StringRef AbbrevDWOSection;
975 StringRef StringDWOSection;
976 StringRef GnuPubTypesSection;
977 StringRef CUIndexSection;
978 StringRef GdbIndexSection;
979 StringRef TUIndexSection;
980
981 SmallVector<SmallString<32>, 4> UncompressedSections;
982
983 StringRef *mapSectionToMember(StringRef Name) {
984 if (DWARFSection *Sec = mapNameToDWARFSection(Name))
985 return &Sec->Data;
986 return StringSwitch<StringRef *>(Name)
987 .Case("debug_abbrev", &AbbrevSection)
988 .Case("debug_aranges", &ARangeSection)
989 .Case("debug_frame", &DebugFrameSection)
990 .Case("eh_frame", &EHFrameSection)
991 .Case("debug_str", &StringSection)
992 .Case("debug_macinfo", &MacinfoSection)
993 .Case("debug_pubnames", &PubNamesSection)
994 .Case("debug_pubtypes", &PubTypesSection)
995 .Case("debug_gnu_pubnames", &GnuPubNamesSection)
996 .Case("debug_gnu_pubtypes", &GnuPubTypesSection)
997 .Case("debug_abbrev.dwo", &AbbrevDWOSection)
998 .Case("debug_str.dwo", &StringDWOSection)
999 .Case("debug_cu_index", &CUIndexSection)
1000 .Case("debug_tu_index", &TUIndexSection)
1001 .Case("gdb_index", &GdbIndexSection)
1002 // Any more debug info sections go here.
1003 .Default(nullptr);
1004 }
1005
1006 /// If Sec is compressed section, decompresses and updates its contents
1007 /// provided by Data. Otherwise leaves it unchanged.
1008 Error maybeDecompress(const object::SectionRef &Sec, StringRef Name,
1009 StringRef &Data) {
1010 if (!Decompressor::isCompressed(Sec))
1011 return Error::success();
1012
1013 Expected<Decompressor> Decompressor =
1014 Decompressor::create(Name, Data, IsLittleEndian, AddressSize == 8);
1015 if (!Decompressor)
1016 return Decompressor.takeError();
1017
1018 SmallString<32> Out;
1019 if (auto Err = Decompressor->resizeAndDecompress(Out))
1020 return Err;
1021
1022 UncompressedSections.emplace_back(std::move(Out));
1023 Data = UncompressedSections.back();
1024
1025 return Error::success();
1026 }
1027
1028public:
1029 DWARFObjInMemory(const StringMap<std::unique_ptr<MemoryBuffer>> &Sections,
1030 uint8_t AddrSize, bool IsLittleEndian)
1031 : IsLittleEndian(IsLittleEndian) {
1032 for (const auto &SecIt : Sections) {
1033 if (StringRef *SectionData = mapSectionToMember(SecIt.first()))
1034 *SectionData = SecIt.second->getBuffer();
Alexey Samsonov068fc8a2013-04-23 10:17:34 +00001035 }
Rafael Espindolac398e672017-07-19 22:27:28 +00001036 }
1037 DWARFObjInMemory(const object::ObjectFile &Obj, const LoadedObjectInfo *L,
1038 function_ref<ErrorPolicy(Error)> HandleError)
1039 : IsLittleEndian(Obj.isLittleEndian()),
1040 AddressSize(Obj.getBytesInAddress()), FileName(Obj.getFileName()) {
Alexey Samsonov068fc8a2013-04-23 10:17:34 +00001041
Rafael Espindolac398e672017-07-19 22:27:28 +00001042 for (const SectionRef &Section : Obj.sections()) {
1043 StringRef Name;
1044 Section.getName(Name);
1045 // Skip BSS and Virtual sections, they aren't interesting.
1046 if (Section.isBSS() || Section.isVirtual())
George Rimarfed9f092017-05-17 12:10:51 +00001047 continue;
Keno Fischerc780e8e2015-05-21 21:24:32 +00001048
Rafael Espindolac398e672017-07-19 22:27:28 +00001049 StringRef Data;
1050 section_iterator RelocatedSection = Section.getRelocatedSection();
1051 // Try to obtain an already relocated version of this section.
1052 // Else use the unrelocated section from the object file. We'll have to
1053 // apply relocations ourselves later.
1054 if (!L || !L->getLoadedSectionContents(*RelocatedSection, Data))
1055 Section.getContents(Data);
George Rimarfed9f092017-05-17 12:10:51 +00001056
Rafael Espindolac398e672017-07-19 22:27:28 +00001057 if (auto Err = maybeDecompress(Section, Name, Data)) {
1058 ErrorPolicy EP = HandleError(createError(
1059 "failed to decompress '" + Name + "', ", std::move(Err)));
George Rimar1af3cb22017-06-28 08:21:19 +00001060 if (EP == ErrorPolicy::Halt)
1061 return;
George Rimarfed9f092017-05-17 12:10:51 +00001062 continue;
1063 }
Rafael Espindolac398e672017-07-19 22:27:28 +00001064
1065 // Compressed sections names in GNU style starts from ".z",
1066 // at this point section is decompressed and we drop compression prefix.
1067 Name = Name.substr(
1068 Name.find_first_not_of("._z")); // Skip ".", "z" and "_" prefixes.
1069
1070 // Map platform specific debug section names to DWARF standard section
1071 // names.
1072 Name = Obj.mapDebugSectionName(Name);
1073
1074 if (StringRef *SectionData = mapSectionToMember(Name)) {
1075 *SectionData = Data;
1076 if (Name == "debug_ranges") {
1077 // FIXME: Use the other dwo range section when we emit it.
1078 RangeDWOSection.Data = Data;
1079 }
1080 } else if (Name == "debug_types") {
1081 // Find debug_types data by section rather than name as there are
1082 // multiple, comdat grouped, debug_types sections.
1083 TypesSections[Section].Data = Data;
1084 } else if (Name == "debug_types.dwo") {
1085 TypesDWOSections[Section].Data = Data;
1086 }
1087
1088 if (RelocatedSection == Obj.section_end())
1089 continue;
1090
1091 StringRef RelSecName;
1092 StringRef RelSecData;
1093 RelocatedSection->getName(RelSecName);
1094
1095 // If the section we're relocating was relocated already by the JIT,
1096 // then we used the relocated version above, so we do not need to process
1097 // relocations for it now.
1098 if (L && L->getLoadedSectionContents(*RelocatedSection, RelSecData))
1099 continue;
1100
1101 // In Mach-o files, the relocations do not need to be applied if
1102 // there is no load offset to apply. The value read at the
1103 // relocation point already factors in the section address
1104 // (actually applying the relocations will produce wrong results
1105 // as the section address will be added twice).
1106 if (!L && isa<MachOObjectFile>(&Obj))
1107 continue;
1108
1109 RelSecName = RelSecName.substr(
1110 RelSecName.find_first_not_of("._z")); // Skip . and _ prefixes.
1111
1112 // TODO: Add support for relocations in other sections as needed.
1113 // Record relocations for the debug_info and debug_line sections.
1114 DWARFSectionMap *Sec = mapNameToDWARFSection(RelSecName);
1115 RelocAddrMap *Map = Sec ? &Sec->Relocs : nullptr;
1116 if (!Map) {
1117 // Find debug_types relocs by section rather than name as there are
1118 // multiple, comdat grouped, debug_types sections.
1119 if (RelSecName == "debug_types")
1120 Map =
1121 &static_cast<DWARFSectionMap &>(TypesSections[*RelocatedSection])
1122 .Relocs;
1123 else if (RelSecName == "debug_types.dwo")
1124 Map = &static_cast<DWARFSectionMap &>(
1125 TypesDWOSections[*RelocatedSection])
1126 .Relocs;
1127 else
1128 continue;
1129 }
1130
1131 if (Section.relocation_begin() == Section.relocation_end())
1132 continue;
1133
1134 // Symbol to [address, section index] cache mapping.
1135 std::map<SymbolRef, SymInfo> AddrCache;
1136 for (const RelocationRef &Reloc : Section.relocations()) {
1137 // FIXME: it's not clear how to correctly handle scattered
1138 // relocations.
1139 if (isRelocScattered(Obj, Reloc))
1140 continue;
1141
1142 Expected<SymInfo> SymInfoOrErr =
1143 getSymbolInfo(Obj, Reloc, L, AddrCache);
1144 if (!SymInfoOrErr) {
1145 if (HandleError(SymInfoOrErr.takeError()) == ErrorPolicy::Halt)
1146 return;
1147 continue;
1148 }
1149
1150 object::RelocVisitor V(Obj);
1151 uint64_t Val = V.visit(Reloc.getType(), Reloc, SymInfoOrErr->Address);
1152 if (V.error()) {
1153 SmallString<32> Type;
1154 Reloc.getTypeName(Type);
1155 ErrorPolicy EP = HandleError(
1156 createError("failed to compute relocation: " + Type + ", ",
1157 errorCodeToError(object_error::parse_failed)));
1158 if (EP == ErrorPolicy::Halt)
1159 return;
1160 continue;
1161 }
1162 RelocAddrEntry Rel = {SymInfoOrErr->SectionIndex, Val};
1163 Map->insert({Reloc.getOffset(), Rel});
1164 }
Eric Christopher7370b552012-11-12 21:40:38 +00001165 }
1166 }
Eric Christopher7370b552012-11-12 21:40:38 +00001167
Rafael Espindolac398e672017-07-19 22:27:28 +00001168 Optional<RelocAddrEntry> find(const DWARFSection &S,
1169 uint64_t Pos) const override {
1170 auto &Sec = static_cast<const DWARFSectionMap &>(S);
1171 RelocAddrMap::const_iterator AI = Sec.Relocs.find(Pos);
1172 if (AI == Sec.Relocs.end())
1173 return None;
1174 return AI->second;
Chris Bieneman2e752db2017-01-20 19:03:14 +00001175 }
Rafael Espindolac398e672017-07-19 22:27:28 +00001176
1177 bool isLittleEndian() const override { return IsLittleEndian; }
1178 StringRef getAbbrevDWOSection() const override { return AbbrevDWOSection; }
1179 const DWARFSection &getLineDWOSection() const override {
1180 return LineDWOSection;
1181 }
1182 const DWARFSection &getLocDWOSection() const override {
1183 return LocDWOSection;
1184 }
1185 StringRef getStringDWOSection() const override { return StringDWOSection; }
1186 const DWARFSection &getStringOffsetDWOSection() const override {
1187 return StringOffsetDWOSection;
1188 }
1189 const DWARFSection &getRangeDWOSection() const override {
1190 return RangeDWOSection;
1191 }
1192 const DWARFSection &getAddrSection() const override { return AddrSection; }
1193 StringRef getCUIndexSection() const override { return CUIndexSection; }
1194 StringRef getGdbIndexSection() const override { return GdbIndexSection; }
1195 StringRef getTUIndexSection() const override { return TUIndexSection; }
1196
1197 // DWARF v5
1198 const DWARFSection &getStringOffsetSection() const override {
1199 return StringOffsetSection;
1200 }
1201
1202 // Sections for DWARF5 split dwarf proposal.
1203 const DWARFSection &getInfoDWOSection() const override {
1204 return InfoDWOSection;
1205 }
1206 void forEachTypesDWOSections(
1207 function_ref<void(const DWARFSection &)> F) const override {
1208 for (auto &P : TypesDWOSections)
1209 F(P.second);
1210 }
1211
1212 StringRef getAbbrevSection() const override { return AbbrevSection; }
1213 const DWARFSection &getLocSection() const override { return LocSection; }
1214 StringRef getARangeSection() const override { return ARangeSection; }
1215 StringRef getDebugFrameSection() const override { return DebugFrameSection; }
1216 StringRef getEHFrameSection() const override { return EHFrameSection; }
1217 const DWARFSection &getLineSection() const override { return LineSection; }
1218 StringRef getStringSection() const override { return StringSection; }
1219 const DWARFSection &getRangeSection() const override { return RangeSection; }
1220 StringRef getMacinfoSection() const override { return MacinfoSection; }
1221 StringRef getPubNamesSection() const override { return PubNamesSection; }
1222 StringRef getPubTypesSection() const override { return PubTypesSection; }
1223 StringRef getGnuPubNamesSection() const override {
1224 return GnuPubNamesSection;
1225 }
1226 StringRef getGnuPubTypesSection() const override {
1227 return GnuPubTypesSection;
1228 }
1229 const DWARFSection &getAppleNamesSection() const override {
1230 return AppleNamesSection;
1231 }
1232 const DWARFSection &getAppleTypesSection() const override {
1233 return AppleTypesSection;
1234 }
1235 const DWARFSection &getAppleNamespacesSection() const override {
1236 return AppleNamespacesSection;
1237 }
1238 const DWARFSection &getAppleObjCSection() const override {
1239 return AppleObjCSection;
1240 }
1241
1242 StringRef getFileName() const override { return FileName; }
1243 uint8_t getAddressSize() const override { return AddressSize; }
1244 const DWARFSection &getInfoSection() const override { return InfoSection; }
1245 void forEachTypesSections(
1246 function_ref<void(const DWARFSection &)> F) const override {
1247 for (auto &P : TypesSections)
1248 F(P.second);
1249 }
1250};
1251
1252std::unique_ptr<DWARFContext>
1253DWARFContext::create(const object::ObjectFile &Obj, const LoadedObjectInfo *L,
1254 function_ref<ErrorPolicy(Error)> HandleError) {
Reid Kleckner388f8802017-07-19 23:42:53 +00001255 auto DObj = llvm::make_unique<DWARFObjInMemory>(Obj, L, HandleError);
Rafael Espindola2e942fb2017-07-19 23:38:54 +00001256 return llvm::make_unique<DWARFContext>(std::move(DObj));
Chris Bieneman2e752db2017-01-20 19:03:14 +00001257}
1258
Rafael Espindolac398e672017-07-19 22:27:28 +00001259std::unique_ptr<DWARFContext>
1260DWARFContext::create(const StringMap<std::unique_ptr<MemoryBuffer>> &Sections,
1261 uint8_t AddrSize, bool isLittleEndian) {
Reid Kleckner388f8802017-07-19 23:42:53 +00001262 auto DObj =
1263 llvm::make_unique<DWARFObjInMemory>(Sections, AddrSize, isLittleEndian);
Rafael Espindola2e942fb2017-07-19 23:38:54 +00001264 return llvm::make_unique<DWARFContext>(std::move(DObj));
Rafael Espindola77e87b32017-07-07 05:36:53 +00001265}