blob: f41ca0dad574cf5f14720fad541a5d1675795f42 [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"
James Henderson3fcc7452018-02-02 12:35:52 +000028#include "llvm/DebugInfo/DWARF/DWARFDebugRnglists.h"
Eugene Zelenko28db7e62017-03-01 01:14:23 +000029#include "llvm/DebugInfo/DWARF/DWARFDie.h"
30#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
31#include "llvm/DebugInfo/DWARF/DWARFGdbIndex.h"
32#include "llvm/DebugInfo/DWARF/DWARFSection.h"
David Blaikie65a8efe2015-11-11 19:28:21 +000033#include "llvm/DebugInfo/DWARF/DWARFUnitIndex.h"
Greg Claytonb8c162b2017-05-03 16:02:29 +000034#include "llvm/DebugInfo/DWARF/DWARFVerifier.h"
Reid Klecknera0587362017-08-29 21:41:21 +000035#include "llvm/MC/MCRegisterInfo.h"
George Rimar4bf30832017-01-11 15:26:41 +000036#include "llvm/Object/Decompressor.h"
Reid Klecknerdafc5d72016-07-06 16:56:42 +000037#include "llvm/Object/MachO.h"
Eugene Zelenko28db7e62017-03-01 01:14:23 +000038#include "llvm/Object/ObjectFile.h"
Reid Klecknerdafc5d72016-07-06 16:56:42 +000039#include "llvm/Object/RelocVisitor.h"
Eugene Zelenko28db7e62017-03-01 01:14:23 +000040#include "llvm/Support/Casting.h"
41#include "llvm/Support/DataExtractor.h"
Eugene Zelenko28db7e62017-03-01 01:14:23 +000042#include "llvm/Support/Error.h"
Benjamin Kramer07d4b1c2011-09-15 16:57:13 +000043#include "llvm/Support/Format.h"
Eugene Zelenko28db7e62017-03-01 01:14:23 +000044#include "llvm/Support/MemoryBuffer.h"
Adrian Prantl3ae35eb2017-09-13 22:09:01 +000045#include "llvm/Support/Path.h"
Reid Klecknera0587362017-08-29 21:41:21 +000046#include "llvm/Support/TargetRegistry.h"
Jonas Devlieghere84e99262018-04-14 22:07:23 +000047#include "llvm/Support/WithColor.h"
Benjamin Kramera6002fd2011-09-14 01:09:52 +000048#include "llvm/Support/raw_ostream.h"
Benjamin Kramer2602ca62011-09-15 20:43:22 +000049#include <algorithm>
Eugene Zelenko28db7e62017-03-01 01:14:23 +000050#include <cstdint>
Fangrui Song5bad9d82018-07-23 23:27:45 +000051#include <deque>
Greg Claytonc7695a82017-05-02 20:28:33 +000052#include <map>
Eugene Zelenko28db7e62017-03-01 01:14:23 +000053#include <string>
54#include <utility>
55#include <vector>
56
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000057using namespace llvm;
Benjamin Kramer6dda0322011-09-15 18:02:20 +000058using namespace dwarf;
Rafael Espindola4f60a382013-05-30 03:05:14 +000059using namespace object;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000060
Chandler Carruthe96dd892014-04-21 22:55:11 +000061#define DEBUG_TYPE "dwarf"
62
Eugene Zelenko2db0cfa2017-06-23 21:57:40 +000063using DWARFLineTable = DWARFDebugLine::LineTable;
64using FileLineInfoKind = DILineInfoSpecifier::FileLineInfoKind;
65using FunctionNameKind = DILineInfoSpecifier::FunctionNameKind;
Eric Christopher494109b2012-10-16 23:46:25 +000066
Reid Klecknera0587362017-08-29 21:41:21 +000067DWARFContext::DWARFContext(std::unique_ptr<const DWARFObject> DObj,
68 std::string DWPName)
69 : DIContext(CK_DWARF), DWPName(std::move(DWPName)), DObj(std::move(DObj)) {}
70
71DWARFContext::~DWARFContext() = default;
72
Adrian Prantl3dcd1222017-09-13 18:22:59 +000073/// Dump the UUID load command.
74static void dumpUUID(raw_ostream &OS, const ObjectFile &Obj) {
75 auto *MachO = dyn_cast<MachOObjectFile>(&Obj);
76 if (!MachO)
77 return;
78 for (auto LC : MachO->load_commands()) {
79 raw_ostream::uuid_t UUID;
80 if (LC.C.cmd == MachO::LC_UUID) {
81 if (LC.C.cmdsize < sizeof(UUID) + sizeof(LC.C)) {
82 OS << "error: UUID load command is too short.\n";
83 return;
84 }
85 OS << "UUID: ";
86 memcpy(&UUID, LC.Ptr+sizeof(LC.C), sizeof(UUID));
87 OS.write_uuid(UUID);
Adrian Prantl146ed402018-01-05 21:44:17 +000088 Triple T = MachO->getArchTriple();
89 OS << " (" << T.getArchName() << ')';
Adrian Prantl3dcd1222017-09-13 18:22:59 +000090 OS << ' ' << MachO->getFileName() << '\n';
91 }
92 }
93}
94
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +000095using ContributionCollection =
96 std::vector<Optional<StrOffsetsContributionDescriptor>>;
97
98// Collect all the contributions to the string offsets table from all units,
99// sort them by their starting offsets and remove duplicates.
100static ContributionCollection
101collectContributionData(DWARFContext::cu_iterator_range CUs,
102 DWARFContext::tu_section_iterator_range TUSs) {
103 ContributionCollection Contributions;
104 for (const auto &CU : CUs)
105 Contributions.push_back(CU->getStringOffsetsTableContribution());
106 for (const auto &TUS : TUSs)
107 for (const auto &TU : TUS)
108 Contributions.push_back(TU->getStringOffsetsTableContribution());
109
110 // Sort the contributions so that any invalid ones are placed at
111 // the start of the contributions vector. This way they are reported
112 // first.
Mandeep Singh Grangfe1d28e2018-04-01 16:18:49 +0000113 llvm::sort(Contributions.begin(), Contributions.end(),
114 [](const Optional<StrOffsetsContributionDescriptor> &L,
115 const Optional<StrOffsetsContributionDescriptor> &R) {
116 if (L && R) return L->Base < R->Base;
117 return R.hasValue();
118 });
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000119
120 // Uniquify contributions, as it is possible that units (specifically
121 // type units in dwo or dwp files) share contributions. We don't want
122 // to report them more than once.
123 Contributions.erase(
124 std::unique(Contributions.begin(), Contributions.end(),
125 [](const Optional<StrOffsetsContributionDescriptor> &L,
126 const Optional<StrOffsetsContributionDescriptor> &R) {
127 if (L && R)
128 return L->Base == R->Base && L->Size == R->Size;
129 return false;
130 }),
131 Contributions.end());
132 return Contributions;
133}
134
135static void dumpDWARFv5StringOffsetsSection(
136 raw_ostream &OS, StringRef SectionName, const DWARFObject &Obj,
137 const DWARFSection &StringOffsetsSection, StringRef StringSection,
138 DWARFContext::cu_iterator_range CUs,
139 DWARFContext::tu_section_iterator_range TUSs, bool LittleEndian) {
140 auto Contributions = collectContributionData(CUs, TUSs);
Rafael Espindolac398e672017-07-19 22:27:28 +0000141 DWARFDataExtractor StrOffsetExt(Obj, StringOffsetsSection, LittleEndian, 0);
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000142 DataExtractor StrData(StringSection, LittleEndian, 0);
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000143 uint64_t SectionSize = StringOffsetsSection.Data.size();
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000144 uint32_t Offset = 0;
145 for (auto &Contribution : Contributions) {
146 // Report an ill-formed contribution.
147 if (!Contribution) {
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000148 OS << "error: invalid contribution to string offsets table in section ."
149 << SectionName << ".\n";
150 return;
151 }
152
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000153 dwarf::DwarfFormat Format = Contribution->getFormat();
154 uint16_t Version = Contribution->getVersion();
155 uint64_t ContributionHeader = Contribution->Base;
156 // In DWARF v5 there is a contribution header that immediately precedes
157 // the string offsets base (the location we have previously retrieved from
158 // the CU DIE's DW_AT_str_offsets attribute). The header is located either
159 // 8 or 16 bytes before the base, depending on the contribution's format.
160 if (Version >= 5)
161 ContributionHeader -= Format == DWARF32 ? 8 : 16;
162
163 // Detect overlapping contributions.
164 if (Offset > ContributionHeader) {
165 OS << "error: overlapping contributions to string offsets table in "
166 "section ."
167 << SectionName << ".\n";
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000168 return;
169 }
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000170 // Report a gap in the table.
171 if (Offset < ContributionHeader) {
172 OS << format("0x%8.8x: Gap, length = ", Offset);
173 OS << (ContributionHeader - Offset) << "\n";
174 }
Wolfgang Piebb4ba1aa2017-12-22 01:12:24 +0000175 OS << format("0x%8.8x: ", (uint32_t)ContributionHeader);
Wolfgang Piebf2b6915e2018-05-10 20:02:34 +0000176 // In DWARF v5 the contribution size in the descriptor does not equal
177 // the originally encoded length (it does not contain the length of the
178 // version field and the padding, a total of 4 bytes). Add them back in
179 // for reporting.
180 OS << "Contribution size = " << (Contribution->Size + (Version < 5 ? 0 : 4))
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000181 << ", Format = " << (Format == DWARF32 ? "DWARF32" : "DWARF64")
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000182 << ", Version = " << Version << "\n";
183
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000184 Offset = Contribution->Base;
185 unsigned EntrySize = Contribution->getDwarfOffsetByteSize();
186 while (Offset - Contribution->Base < Contribution->Size) {
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000187 OS << format("0x%8.8x: ", Offset);
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000188 // FIXME: We can only extract strings if the offset fits in 32 bits.
Paul Robinson17536b92017-06-29 16:52:08 +0000189 uint64_t StringOffset =
190 StrOffsetExt.getRelocatedValue(EntrySize, &Offset);
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000191 // Extract the string if we can and display it. Otherwise just report
192 // the offset.
193 if (StringOffset <= std::numeric_limits<uint32_t>::max()) {
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000194 uint32_t StringOffset32 = (uint32_t)StringOffset;
Simon Dardisb1b52c02017-08-07 16:08:11 +0000195 OS << format("%8.8x ", StringOffset32);
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000196 const char *S = StrData.getCStr(&StringOffset32);
197 if (S)
198 OS << format("\"%s\"", S);
199 } else
Simon Dardisec4ea992017-08-07 13:30:03 +0000200 OS << format("%16.16" PRIx64 " ", StringOffset);
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000201 OS << "\n";
202 }
203 }
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000204 // Report a gap at the end of the table.
205 if (Offset < SectionSize) {
206 OS << format("0x%8.8x: Gap, length = ", Offset);
207 OS << (SectionSize - Offset) << "\n";
208 }
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000209}
210
211// Dump a DWARF string offsets section. This may be a DWARF v5 formatted
212// string offsets section, where each compile or type unit contributes a
213// number of entries (string offsets), with each contribution preceded by
214// a header containing size and version number. Alternatively, it may be a
215// monolithic series of string offsets, as generated by the pre-DWARF v5
216// implementation of split DWARF.
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000217static void dumpStringOffsetsSection(
218 raw_ostream &OS, StringRef SectionName, const DWARFObject &Obj,
219 const DWARFSection &StringOffsetsSection, StringRef StringSection,
220 DWARFContext::cu_iterator_range CUs,
221 DWARFContext::tu_section_iterator_range TUSs, bool LittleEndian,
222 unsigned MaxVersion) {
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000223 // If we have at least one (compile or type) unit with DWARF v5 or greater,
224 // we assume that the section is formatted like a DWARF v5 string offsets
225 // section.
226 if (MaxVersion >= 5)
Rafael Espindolac398e672017-07-19 22:27:28 +0000227 dumpDWARFv5StringOffsetsSection(OS, SectionName, Obj, StringOffsetsSection,
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000228 StringSection, CUs, TUSs, LittleEndian);
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000229 else {
230 DataExtractor strOffsetExt(StringOffsetsSection.Data, LittleEndian, 0);
231 uint32_t offset = 0;
232 uint64_t size = StringOffsetsSection.Data.size();
233 // Ensure that size is a multiple of the size of an entry.
234 if (size & ((uint64_t)(sizeof(uint32_t) - 1))) {
235 OS << "error: size of ." << SectionName << " is not a multiple of "
236 << sizeof(uint32_t) << ".\n";
237 size &= -(uint64_t)sizeof(uint32_t);
238 }
239 DataExtractor StrData(StringSection, LittleEndian, 0);
240 while (offset < size) {
241 OS << format("0x%8.8x: ", offset);
242 uint32_t StringOffset = strOffsetExt.getU32(&offset);
243 OS << format("%8.8x ", StringOffset);
244 const char *S = StrData.getCStr(&StringOffset);
245 if (S)
246 OS << format("\"%s\"", S);
247 OS << "\n";
248 }
249 }
250}
251
Wolfgang Piebad605592018-05-18 20:12:54 +0000252// Dump the .debug_rnglists or .debug_rnglists.dwo section (DWARF v5).
253static void dumpRnglistsSection(raw_ostream &OS,
254 DWARFDataExtractor &rnglistData,
255 DIDumpOptions DumpOpts) {
256 uint32_t Offset = 0;
257 while (rnglistData.isValidOffset(Offset)) {
258 llvm::DWARFDebugRnglistTable Rnglists;
259 uint32_t TableOffset = Offset;
260 if (Error Err = Rnglists.extract(rnglistData, &Offset)) {
261 WithColor::error() << toString(std::move(Err)) << '\n';
262 uint64_t Length = Rnglists.length();
263 // Keep going after an error, if we can, assuming that the length field
264 // could be read. If it couldn't, stop reading the section.
265 if (Length == 0)
266 break;
267 Offset = TableOffset + Length;
268 } else {
269 Rnglists.dump(OS, DumpOpts);
270 }
271 }
272}
273
Adrian Prantl057d3362017-09-15 23:04:04 +0000274void DWARFContext::dump(
275 raw_ostream &OS, DIDumpOptions DumpOpts,
276 std::array<Optional<uint64_t>, DIDT_ID_Count> DumpOffsets) {
277
278 Optional<uint64_t> DumpOffset;
Adrian Prantl7bc1b282017-09-11 22:59:45 +0000279 uint64_t DumpType = DumpOpts.DumpType;
Adrian Prantlf4bc1f72017-06-01 18:18:23 +0000280
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000281 StringRef Extension = sys::path::extension(DObj->getFileName());
282 bool IsDWO = (Extension == ".dwo") || (Extension == ".dwp");
283
284 // Print UUID header.
Adrian Prantl3dcd1222017-09-13 18:22:59 +0000285 const auto *ObjFile = DObj->getFile();
Adrian Prantl3dcd1222017-09-13 18:22:59 +0000286 if (DumpType & DIDT_UUID)
287 dumpUUID(OS, *ObjFile);
288
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000289 // Print a header for each explicitly-requested section.
290 // Otherwise just print one for non-empty sections.
Adrian Prantl057d3362017-09-15 23:04:04 +0000291 // Only print empty .dwo section headers when dumping a .dwo file.
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000292 bool Explicit = DumpType != DIDT_All && !IsDWO;
Adrian Prantl057d3362017-09-15 23:04:04 +0000293 bool ExplicitDWO = Explicit && IsDWO;
294 auto shouldDump = [&](bool Explicit, const char *Name, unsigned ID,
295 StringRef Section) {
296 DumpOffset = DumpOffsets[ID];
297 unsigned Mask = 1U << ID;
298 bool Should = (DumpType & Mask) && (Explicit || !Section.empty());
Adrian Prantl84168022017-09-15 17:39:50 +0000299 if (Should)
Adrian Prantl057d3362017-09-15 23:04:04 +0000300 OS << "\n" << Name << " contents:\n";
Adrian Prantl84168022017-09-15 17:39:50 +0000301 return Should;
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000302 };
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000303
304 // Dump individual sections.
Adrian Prantl057d3362017-09-15 23:04:04 +0000305 if (shouldDump(Explicit, ".debug_abbrev", DIDT_ID_DebugAbbrev,
306 DObj->getAbbrevSection()))
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000307 getDebugAbbrev()->dump(OS);
Adrian Prantl057d3362017-09-15 23:04:04 +0000308 if (shouldDump(ExplicitDWO, ".debug_abbrev.dwo", DIDT_ID_DebugAbbrev,
309 DObj->getAbbrevDWOSection()))
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000310 getDebugAbbrevDWO()->dump(OS);
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000311
Adrian Prantl057d3362017-09-15 23:04:04 +0000312 auto dumpDebugInfo = [&](bool IsExplicit, const char *Name,
313 DWARFSection Section, cu_iterator_range CUs) {
314 if (shouldDump(IsExplicit, Name, DIDT_ID_DebugInfo, Section.Data)) {
Adrian Prantlc8d86532017-09-18 21:44:40 +0000315 if (DumpOffset)
Adrian Prantld3f9f212017-09-20 17:44:00 +0000316 getDIEForOffset(DumpOffset.getValue())
317 .dump(OS, 0, DumpOpts.noImplicitRecursion());
Adrian Prantlc8d86532017-09-18 21:44:40 +0000318 else
319 for (const auto &CU : CUs)
Adrian Prantl057d3362017-09-15 23:04:04 +0000320 CU->dump(OS, DumpOpts);
321 }
322 };
323 dumpDebugInfo(Explicit, ".debug_info", DObj->getInfoSection(),
324 compile_units());
325 dumpDebugInfo(ExplicitDWO, ".debug_info.dwo", DObj->getInfoDWOSection(),
326 dwo_compile_units());
David Blaikie622dce42014-01-08 23:29:59 +0000327
Adrian Prantl099d7e42017-09-16 16:58:18 +0000328 auto dumpDebugType = [&](const char *Name,
329 tu_section_iterator_range TUSections) {
330 OS << '\n' << Name << " contents:\n";
331 DumpOffset = DumpOffsets[DIDT_ID_DebugTypes];
332 for (const auto &TUS : TUSections)
333 for (const auto &TU : TUS)
334 if (DumpOffset)
Adrian Prantld3f9f212017-09-20 17:44:00 +0000335 TU->getDIEForOffset(*DumpOffset)
336 .dump(OS, 0, DumpOpts.noImplicitRecursion());
Adrian Prantl099d7e42017-09-16 16:58:18 +0000337 else
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000338 TU->dump(OS, DumpOpts);
Adrian Prantl099d7e42017-09-16 16:58:18 +0000339 };
340 if ((DumpType & DIDT_DebugTypes)) {
341 if (Explicit || getNumTypeUnits())
342 dumpDebugType(".debug_types", type_unit_sections());
343 if (ExplicitDWO || getNumDWOTypeUnits())
344 dumpDebugType(".debug_types.dwo", dwo_type_unit_sections());
David Blaikie03c089c2013-09-23 22:44:47 +0000345 }
346
Adrian Prantl057d3362017-09-15 23:04:04 +0000347 if (shouldDump(Explicit, ".debug_loc", DIDT_ID_DebugLoc,
Adrian Prantl84168022017-09-15 17:39:50 +0000348 DObj->getLocSection().Data)) {
Jonas Devlieghere622c5632017-09-27 09:33:36 +0000349 getDebugLoc()->dump(OS, getRegisterInfo(), DumpOffset);
David Blaikie18e73502013-06-19 21:37:13 +0000350 }
Adrian Prantl057d3362017-09-15 23:04:04 +0000351 if (shouldDump(ExplicitDWO, ".debug_loc.dwo", DIDT_ID_DebugLoc,
Adrian Prantl84168022017-09-15 17:39:50 +0000352 DObj->getLocDWOSection().Data)) {
Jonas Devlieghere622c5632017-09-27 09:33:36 +0000353 getDebugLocDWO()->dump(OS, getRegisterInfo(), DumpOffset);
David Blaikie9c550ac2014-03-25 01:44:02 +0000354 }
355
Jonas Devliegherec0a758d2017-09-18 14:15:57 +0000356 if (shouldDump(Explicit, ".debug_frame", DIDT_ID_DebugFrame,
Adrian Prantl62528e62017-09-21 18:52:03 +0000357 DObj->getDebugFrameSection()))
Rafael Auler86fb7bf2018-03-08 00:46:53 +0000358 getDebugFrame()->dump(OS, getRegisterInfo(), DumpOffset);
Jonas Devliegherec0a758d2017-09-18 14:15:57 +0000359
360 if (shouldDump(Explicit, ".eh_frame", DIDT_ID_DebugFrame,
Adrian Prantl62528e62017-09-21 18:52:03 +0000361 DObj->getEHFrameSection()))
Rafael Auler86fb7bf2018-03-08 00:46:53 +0000362 getEHFrame()->dump(OS, getRegisterInfo(), DumpOffset);
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000363
Adrian Prantl7bc1b282017-09-11 22:59:45 +0000364 if (DumpType & DIDT_DebugMacro) {
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000365 if (Explicit || !getDebugMacro()->empty()) {
366 OS << "\n.debug_macinfo contents:\n";
367 getDebugMacro()->dump(OS);
368 }
Amjad Aboude59cc3e2015-11-12 09:38:54 +0000369 }
370
Adrian Prantl057d3362017-09-15 23:04:04 +0000371 if (shouldDump(Explicit, ".debug_aranges", DIDT_ID_DebugAranges,
Adrian Prantl84168022017-09-15 17:39:50 +0000372 DObj->getARangeSection())) {
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000373 uint32_t offset = 0;
Rafael Espindolac398e672017-07-19 22:27:28 +0000374 DataExtractor arangesData(DObj->getARangeSection(), isLittleEndian(), 0);
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000375 DWARFDebugArangeSet set;
376 while (set.extract(arangesData, &offset))
377 set.dump(OS);
378 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000379
James Hendersona3acf992018-05-10 10:51:33 +0000380 auto DumpLineSection = [&](DWARFDebugLine::SectionParser Parser,
381 DIDumpOptions DumpOpts) {
382 while (!Parser.done()) {
383 if (DumpOffset && Parser.getOffset() != *DumpOffset) {
384 Parser.skip();
Alexey Samsonov7a18c062015-05-19 21:54:32 +0000385 continue;
Paul Robinson63811a42017-11-22 15:33:17 +0000386 }
James Hendersona3acf992018-05-10 10:51:33 +0000387 OS << "debug_line[" << format("0x%8.8x", Parser.getOffset()) << "]\n";
Paul Robinson63811a42017-11-22 15:33:17 +0000388 if (DumpOpts.Verbose) {
James Henderson004b7292018-05-21 15:30:54 +0000389 Parser.parseNext(DWARFDebugLine::warn, DWARFDebugLine::warn, &OS);
Paul Robinson63811a42017-11-22 15:33:17 +0000390 } else {
James Hendersona3acf992018-05-10 10:51:33 +0000391 DWARFDebugLine::LineTable LineTable = Parser.parseNext();
392 LineTable.dump(OS, DumpOpts);
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000393 }
Benjamin Kramer6dda0322011-09-15 18:02:20 +0000394 }
James Hendersona3acf992018-05-10 10:51:33 +0000395 };
396
397 if (shouldDump(Explicit, ".debug_line", DIDT_ID_DebugLine,
398 DObj->getLineSection().Data)) {
399 DWARFDataExtractor LineData(*DObj, DObj->getLineSection(), isLittleEndian(),
400 0);
401 DWARFDebugLine::SectionParser Parser(LineData, *this, compile_units(),
402 type_unit_sections());
403 DumpLineSection(Parser, DumpOpts);
Benjamin Kramer6dda0322011-09-15 18:02:20 +0000404 }
Benjamin Kramer07d4b1c2011-09-15 16:57:13 +0000405
Adrian Prantl057d3362017-09-15 23:04:04 +0000406 if (shouldDump(ExplicitDWO, ".debug_line.dwo", DIDT_ID_DebugLine,
Adrian Prantl84168022017-09-15 17:39:50 +0000407 DObj->getLineDWOSection().Data)) {
Paul Robinson63811a42017-11-22 15:33:17 +0000408 DWARFDataExtractor LineData(*DObj, DObj->getLineDWOSection(),
409 isLittleEndian(), 0);
James Hendersona3acf992018-05-10 10:51:33 +0000410 DWARFDebugLine::SectionParser Parser(LineData, *this, dwo_compile_units(),
411 dwo_type_unit_sections());
412 DumpLineSection(Parser, DumpOpts);
David Blaikie1d4736e2014-02-24 23:58:54 +0000413 }
414
Adrian Prantl057d3362017-09-15 23:04:04 +0000415 if (shouldDump(Explicit, ".debug_cu_index", DIDT_ID_DebugCUIndex,
Adrian Prantl84168022017-09-15 17:39:50 +0000416 DObj->getCUIndexSection())) {
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000417 getCUIndex().dump(OS);
418 }
419
Adrian Prantl057d3362017-09-15 23:04:04 +0000420 if (shouldDump(Explicit, ".debug_tu_index", DIDT_ID_DebugTUIndex,
Adrian Prantl84168022017-09-15 17:39:50 +0000421 DObj->getTUIndexSection())) {
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000422 getTUIndex().dump(OS);
423 }
424
Adrian Prantl057d3362017-09-15 23:04:04 +0000425 if (shouldDump(Explicit, ".debug_str", DIDT_ID_DebugStr,
Adrian Prantl84168022017-09-15 17:39:50 +0000426 DObj->getStringSection())) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000427 DataExtractor strData(DObj->getStringSection(), isLittleEndian(), 0);
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000428 uint32_t offset = 0;
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000429 uint32_t strOffset = 0;
430 while (const char *s = strData.getCStr(&offset)) {
431 OS << format("0x%8.8x: \"%s\"\n", strOffset, s);
432 strOffset = offset;
433 }
Benjamin Kramer07d4b1c2011-09-15 16:57:13 +0000434 }
Adrian Prantl057d3362017-09-15 23:04:04 +0000435 if (shouldDump(ExplicitDWO, ".debug_str.dwo", DIDT_ID_DebugStr,
Adrian Prantl84168022017-09-15 17:39:50 +0000436 DObj->getStringDWOSection())) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000437 DataExtractor strDWOData(DObj->getStringDWOSection(), isLittleEndian(), 0);
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000438 uint32_t offset = 0;
David Blaikie66865d62014-01-09 00:13:35 +0000439 uint32_t strDWOOffset = 0;
440 while (const char *s = strDWOData.getCStr(&offset)) {
441 OS << format("0x%8.8x: \"%s\"\n", strDWOOffset, s);
442 strDWOOffset = offset;
David Blaikie622dce42014-01-08 23:29:59 +0000443 }
David Blaikie66865d62014-01-09 00:13:35 +0000444 }
Paul Robinsonb6aa01c2018-01-25 22:02:36 +0000445 if (shouldDump(Explicit, ".debug_line_str", DIDT_ID_DebugLineStr,
446 DObj->getLineStringSection())) {
447 DataExtractor strData(DObj->getLineStringSection(), isLittleEndian(), 0);
448 uint32_t offset = 0;
449 uint32_t strOffset = 0;
450 while (const char *s = strData.getCStr(&offset)) {
Scott Linder16c7bda2018-02-23 23:01:06 +0000451 OS << format("0x%8.8x: \"", strOffset);
452 OS.write_escaped(s);
453 OS << "\"\n";
Paul Robinsonb6aa01c2018-01-25 22:02:36 +0000454 strOffset = offset;
455 }
456 }
David Blaikie622dce42014-01-08 23:29:59 +0000457
Adrian Prantl057d3362017-09-15 23:04:04 +0000458 if (shouldDump(Explicit, ".debug_ranges", DIDT_ID_DebugRanges,
Adrian Prantl84168022017-09-15 17:39:50 +0000459 DObj->getRangeSection().Data)) {
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000460 // In fact, different compile units may have different address byte
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000461 // sizes, but for simplicity we just use the address byte size of the
462 // last compile unit (there is no easy and fast way to associate address
463 // range list and the compile unit it describes).
464 // FIXME: savedAddressByteSize seems sketchy.
Paul Robinson63811a42017-11-22 15:33:17 +0000465 uint8_t savedAddressByteSize = 0;
466 for (const auto &CU : compile_units()) {
467 savedAddressByteSize = CU->getAddressByteSize();
468 break;
469 }
Rafael Espindolac398e672017-07-19 22:27:28 +0000470 DWARFDataExtractor rangesData(*DObj, DObj->getRangeSection(),
471 isLittleEndian(), savedAddressByteSize);
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000472 uint32_t offset = 0;
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000473 DWARFDebugRangeList rangeList;
Wolfgang Pieb61d8c8d2018-06-20 22:56:37 +0000474 while (rangesData.isValidOffset(offset)) {
475 if (Error E = rangeList.extract(rangesData, &offset)) {
476 WithColor::error() << toString(std::move(E)) << '\n';
Fangrui Songf78650a2018-07-30 19:41:25 +0000477 break;
Wolfgang Pieb61d8c8d2018-06-20 22:56:37 +0000478 }
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000479 rangeList.dump(OS);
Wolfgang Pieb61d8c8d2018-06-20 22:56:37 +0000480 }
Eric Christopherda4b2192013-01-02 23:52:13 +0000481 }
Eric Christopher962c9082013-01-15 23:56:56 +0000482
James Henderson3fcc7452018-02-02 12:35:52 +0000483 if (shouldDump(Explicit, ".debug_rnglists", DIDT_ID_DebugRnglists,
484 DObj->getRnglistsSection().Data)) {
Wolfgang Piebad605592018-05-18 20:12:54 +0000485 DWARFDataExtractor RnglistData(*DObj, DObj->getRnglistsSection(),
James Henderson3fcc7452018-02-02 12:35:52 +0000486 isLittleEndian(), 0);
Wolfgang Piebad605592018-05-18 20:12:54 +0000487 dumpRnglistsSection(OS, RnglistData, DumpOpts);
488 }
489
490 if (shouldDump(ExplicitDWO, ".debug_rnglists.dwo", DIDT_ID_DebugRnglists,
491 DObj->getRnglistsDWOSection().Data)) {
492 DWARFDataExtractor RnglistData(*DObj, DObj->getRnglistsDWOSection(),
493 isLittleEndian(), 0);
494 dumpRnglistsSection(OS, RnglistData, DumpOpts);
James Henderson3fcc7452018-02-02 12:35:52 +0000495 }
496
Adrian Prantl057d3362017-09-15 23:04:04 +0000497 if (shouldDump(Explicit, ".debug_pubnames", DIDT_ID_DebugPubnames,
Adrian Prantl84168022017-09-15 17:39:50 +0000498 DObj->getPubNamesSection()))
Rafael Espindolac398e672017-07-19 22:27:28 +0000499 DWARFDebugPubTable(DObj->getPubNamesSection(), isLittleEndian(), false)
Adrian Prantl84168022017-09-15 17:39:50 +0000500 .dump(OS);
Krzysztof Parzyszek97438dc2013-02-12 16:20:28 +0000501
Adrian Prantl057d3362017-09-15 23:04:04 +0000502 if (shouldDump(Explicit, ".debug_pubtypes", DIDT_ID_DebugPubtypes,
Adrian Prantl84168022017-09-15 17:39:50 +0000503 DObj->getPubTypesSection()))
Rafael Espindolac398e672017-07-19 22:27:28 +0000504 DWARFDebugPubTable(DObj->getPubTypesSection(), isLittleEndian(), false)
Adrian Prantl84168022017-09-15 17:39:50 +0000505 .dump(OS);
Eric Christopher4c7e6ba2013-09-25 23:02:41 +0000506
Adrian Prantl057d3362017-09-15 23:04:04 +0000507 if (shouldDump(Explicit, ".debug_gnu_pubnames", DIDT_ID_DebugGnuPubnames,
Adrian Prantl84168022017-09-15 17:39:50 +0000508 DObj->getGnuPubNamesSection()))
Rafael Espindolac398e672017-07-19 22:27:28 +0000509 DWARFDebugPubTable(DObj->getGnuPubNamesSection(), isLittleEndian(),
George Rimare71e33f2016-12-17 09:10:32 +0000510 true /* GnuStyle */)
Adrian Prantl84168022017-09-15 17:39:50 +0000511 .dump(OS);
David Blaikieecd21ff2013-09-24 19:50:00 +0000512
Adrian Prantl057d3362017-09-15 23:04:04 +0000513 if (shouldDump(Explicit, ".debug_gnu_pubtypes", DIDT_ID_DebugGnuPubtypes,
Adrian Prantl84168022017-09-15 17:39:50 +0000514 DObj->getGnuPubTypesSection()))
Rafael Espindolac398e672017-07-19 22:27:28 +0000515 DWARFDebugPubTable(DObj->getGnuPubTypesSection(), isLittleEndian(),
George Rimare71e33f2016-12-17 09:10:32 +0000516 true /* GnuStyle */)
Adrian Prantl84168022017-09-15 17:39:50 +0000517 .dump(OS);
David Blaikie404d3042013-09-19 23:01:29 +0000518
Adrian Prantl057d3362017-09-15 23:04:04 +0000519 if (shouldDump(Explicit, ".debug_str_offsets", DIDT_ID_DebugStrOffsets,
Adrian Prantl84168022017-09-15 17:39:50 +0000520 DObj->getStringOffsetSection().Data))
Rafael Espindolac398e672017-07-19 22:27:28 +0000521 dumpStringOffsetsSection(
522 OS, "debug_str_offsets", *DObj, DObj->getStringOffsetSection(),
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000523 DObj->getStringSection(), compile_units(), type_unit_sections(),
524 isLittleEndian(), getMaxVersion());
Adrian Prantl057d3362017-09-15 23:04:04 +0000525 if (shouldDump(ExplicitDWO, ".debug_str_offsets.dwo", DIDT_ID_DebugStrOffsets,
Adrian Prantl84168022017-09-15 17:39:50 +0000526 DObj->getStringOffsetDWOSection().Data))
Rafael Espindolac398e672017-07-19 22:27:28 +0000527 dumpStringOffsetsSection(
528 OS, "debug_str_offsets.dwo", *DObj, DObj->getStringOffsetDWOSection(),
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000529 DObj->getStringDWOSection(), dwo_compile_units(),
530 dwo_type_unit_sections(), isLittleEndian(), getMaxVersion());
Frederic Risse837ec22014-11-14 16:15:53 +0000531
Adrian Prantl057d3362017-09-15 23:04:04 +0000532 if (shouldDump(Explicit, ".gnu_index", DIDT_ID_GdbIndex,
Adrian Prantl84168022017-09-15 17:39:50 +0000533 DObj->getGdbIndexSection())) {
George Rimar4f82df52016-09-23 11:01:53 +0000534 getGdbIndex().dump(OS);
535 }
536
Adrian Prantl057d3362017-09-15 23:04:04 +0000537 if (shouldDump(Explicit, ".apple_names", DIDT_ID_AppleNames,
Adrian Prantl84168022017-09-15 17:39:50 +0000538 DObj->getAppleNamesSection().Data))
Adrian Prantl99fdb9d2017-09-28 18:10:52 +0000539 getAppleNames().dump(OS);
Frederic Risse837ec22014-11-14 16:15:53 +0000540
Adrian Prantl057d3362017-09-15 23:04:04 +0000541 if (shouldDump(Explicit, ".apple_types", DIDT_ID_AppleTypes,
Adrian Prantl84168022017-09-15 17:39:50 +0000542 DObj->getAppleTypesSection().Data))
Adrian Prantl714ee4d2017-09-29 00:33:22 +0000543 getAppleTypes().dump(OS);
Frederic Risse837ec22014-11-14 16:15:53 +0000544
Adrian Prantl057d3362017-09-15 23:04:04 +0000545 if (shouldDump(Explicit, ".apple_namespaces", DIDT_ID_AppleNamespaces,
Adrian Prantl84168022017-09-15 17:39:50 +0000546 DObj->getAppleNamespacesSection().Data))
Adrian Prantlf51e7802017-09-29 00:52:33 +0000547 getAppleNamespaces().dump(OS);
Frederic Risse837ec22014-11-14 16:15:53 +0000548
Adrian Prantl057d3362017-09-15 23:04:04 +0000549 if (shouldDump(Explicit, ".apple_objc", DIDT_ID_AppleObjC,
Adrian Prantl84168022017-09-15 17:39:50 +0000550 DObj->getAppleObjCSection().Data))
Adrian Prantlf51e7802017-09-29 00:52:33 +0000551 getAppleObjC().dump(OS);
Pavel Labath3c9a9182018-01-29 11:08:32 +0000552 if (shouldDump(Explicit, ".debug_names", DIDT_ID_DebugNames,
553 DObj->getDebugNamesSection().Data))
554 getDebugNames().dump(OS);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000555}
556
David Blaikie15d85fc2017-05-23 06:48:53 +0000557DWARFCompileUnit *DWARFContext::getDWOCompileUnitForHash(uint64_t Hash) {
David Blaikiee79dda32017-09-19 18:36:11 +0000558 DWOCUs.parseDWO(*this, DObj->getInfoDWOSection(), true);
David Blaikiea62f1cb2017-07-30 15:15:58 +0000559
David Blaikieebac0b92017-07-30 08:12:07 +0000560 if (const auto &CUI = getCUIndex()) {
561 if (const auto *R = CUI.getFromHash(Hash))
David Blaikiee79dda32017-09-19 18:36:11 +0000562 return DWOCUs.getUnitForIndexEntry(*R);
David Blaikieebac0b92017-07-30 08:12:07 +0000563 return nullptr;
564 }
565
566 // If there's no index, just search through the CUs in the DWO - there's
567 // probably only one unless this is something like LTO - though an in-process
568 // built/cached lookup table could be used in that case to improve repeated
569 // lookups of different CUs in the DWO.
Paul Robinson543c0e12018-05-22 17:27:31 +0000570 for (const auto &DWOCU : dwo_compile_units()) {
571 // Might not have parsed DWO ID yet.
572 if (!DWOCU->getDWOId()) {
573 if (Optional<uint64_t> DWOId =
574 toUnsigned(DWOCU->getUnitDIE().find(DW_AT_GNU_dwo_id)))
575 DWOCU->setDWOId(*DWOId);
576 else
577 // No DWO ID?
578 continue;
579 }
David Blaikie15d85fc2017-05-23 06:48:53 +0000580 if (DWOCU->getDWOId() == Hash)
581 return DWOCU.get();
Paul Robinson543c0e12018-05-22 17:27:31 +0000582 }
David Blaikie15d85fc2017-05-23 06:48:53 +0000583 return nullptr;
584}
585
Greg Claytonc7695a82017-05-02 20:28:33 +0000586DWARFDie DWARFContext::getDIEForOffset(uint32_t Offset) {
587 parseCompileUnits();
588 if (auto *CU = CUs.getUnitForOffset(Offset))
589 return CU->getDIEForOffset(Offset);
590 return DWARFDie();
591}
592
Jonas Devliegherec0a758d2017-09-18 14:15:57 +0000593bool DWARFContext::verify(raw_ostream &OS, DIDumpOptions DumpOpts) {
Greg Claytonc7695a82017-05-02 20:28:33 +0000594 bool Success = true;
Jonas Devlieghere27476ce2017-09-13 09:43:05 +0000595 DWARFVerifier verifier(OS, *this, DumpOpts);
Spyridoula Gravani73e17962017-07-27 00:59:33 +0000596
Spyridoula Gravani364b5352017-07-20 02:06:52 +0000597 Success &= verifier.handleDebugAbbrev();
Jonas Devliegherec0a758d2017-09-18 14:15:57 +0000598 if (DumpOpts.DumpType & DIDT_DebugInfo)
Spyridoula Gravani73e17962017-07-27 00:59:33 +0000599 Success &= verifier.handleDebugInfo();
Jonas Devliegherec0a758d2017-09-18 14:15:57 +0000600 if (DumpOpts.DumpType & DIDT_DebugLine)
Spyridoula Gravani73e17962017-07-27 00:59:33 +0000601 Success &= verifier.handleDebugLine();
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000602 Success &= verifier.handleAccelTables();
Greg Clayton48432cf2017-05-01 22:07:02 +0000603 return Success;
604}
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000605
David Blaikie82641be2015-11-17 00:39:55 +0000606const DWARFUnitIndex &DWARFContext::getCUIndex() {
607 if (CUIndex)
608 return *CUIndex;
609
Rafael Espindolac398e672017-07-19 22:27:28 +0000610 DataExtractor CUIndexData(DObj->getCUIndexSection(), isLittleEndian(), 0);
David Blaikie82641be2015-11-17 00:39:55 +0000611
David Blaikieb073cb92015-12-02 06:21:34 +0000612 CUIndex = llvm::make_unique<DWARFUnitIndex>(DW_SECT_INFO);
David Blaikie82641be2015-11-17 00:39:55 +0000613 CUIndex->parse(CUIndexData);
614 return *CUIndex;
615}
616
617const DWARFUnitIndex &DWARFContext::getTUIndex() {
618 if (TUIndex)
619 return *TUIndex;
620
Rafael Espindolac398e672017-07-19 22:27:28 +0000621 DataExtractor TUIndexData(DObj->getTUIndexSection(), isLittleEndian(), 0);
David Blaikie82641be2015-11-17 00:39:55 +0000622
David Blaikieb073cb92015-12-02 06:21:34 +0000623 TUIndex = llvm::make_unique<DWARFUnitIndex>(DW_SECT_TYPES);
David Blaikie82641be2015-11-17 00:39:55 +0000624 TUIndex->parse(TUIndexData);
625 return *TUIndex;
626}
627
George Rimar4f82df52016-09-23 11:01:53 +0000628DWARFGdbIndex &DWARFContext::getGdbIndex() {
629 if (GdbIndex)
630 return *GdbIndex;
631
Rafael Espindolac398e672017-07-19 22:27:28 +0000632 DataExtractor GdbIndexData(DObj->getGdbIndexSection(), true /*LE*/, 0);
George Rimar4f82df52016-09-23 11:01:53 +0000633 GdbIndex = llvm::make_unique<DWARFGdbIndex>();
634 GdbIndex->parse(GdbIndexData);
635 return *GdbIndex;
636}
637
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000638const DWARFDebugAbbrev *DWARFContext::getDebugAbbrev() {
639 if (Abbrev)
640 return Abbrev.get();
641
Rafael Espindolac398e672017-07-19 22:27:28 +0000642 DataExtractor abbrData(DObj->getAbbrevSection(), isLittleEndian(), 0);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000643
644 Abbrev.reset(new DWARFDebugAbbrev());
Alexey Samsonov4316df52014-04-25 21:10:56 +0000645 Abbrev->extract(abbrData);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000646 return Abbrev.get();
647}
648
Eric Christopherda4b2192013-01-02 23:52:13 +0000649const DWARFDebugAbbrev *DWARFContext::getDebugAbbrevDWO() {
650 if (AbbrevDWO)
651 return AbbrevDWO.get();
652
Rafael Espindolac398e672017-07-19 22:27:28 +0000653 DataExtractor abbrData(DObj->getAbbrevDWOSection(), isLittleEndian(), 0);
Eric Christopherda4b2192013-01-02 23:52:13 +0000654 AbbrevDWO.reset(new DWARFDebugAbbrev());
Alexey Samsonov4316df52014-04-25 21:10:56 +0000655 AbbrevDWO->extract(abbrData);
Eric Christopherda4b2192013-01-02 23:52:13 +0000656 return AbbrevDWO.get();
657}
658
David Blaikie18e73502013-06-19 21:37:13 +0000659const DWARFDebugLoc *DWARFContext::getDebugLoc() {
660 if (Loc)
661 return Loc.get();
662
Paul Robinson17536b92017-06-29 16:52:08 +0000663 Loc.reset(new DWARFDebugLoc);
Pavel Labath54ca2d62018-04-06 08:49:57 +0000664 // Assume all compile units have the same address byte size.
Paul Robinson17536b92017-06-29 16:52:08 +0000665 if (getNumCompileUnits()) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000666 DWARFDataExtractor LocData(*DObj, DObj->getLocSection(), isLittleEndian(),
Paul Robinson17536b92017-06-29 16:52:08 +0000667 getCompileUnitAtIndex(0)->getAddressByteSize());
668 Loc->parse(LocData);
669 }
David Blaikie18e73502013-06-19 21:37:13 +0000670 return Loc.get();
671}
672
David Blaikie9c550ac2014-03-25 01:44:02 +0000673const DWARFDebugLocDWO *DWARFContext::getDebugLocDWO() {
674 if (LocDWO)
675 return LocDWO.get();
676
David Blaikie9c550ac2014-03-25 01:44:02 +0000677 LocDWO.reset(new DWARFDebugLocDWO());
Pavel Labath54ca2d62018-04-06 08:49:57 +0000678 // Assume all compile units have the same address byte size.
679 if (getNumCompileUnits()) {
680 DataExtractor LocData(DObj->getLocDWOSection().Data, isLittleEndian(),
681 getCompileUnitAtIndex(0)->getAddressByteSize());
682 LocDWO->parse(LocData);
683 }
David Blaikie9c550ac2014-03-25 01:44:02 +0000684 return LocDWO.get();
685}
686
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000687const DWARFDebugAranges *DWARFContext::getDebugAranges() {
688 if (Aranges)
689 return Aranges.get();
690
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000691 Aranges.reset(new DWARFDebugAranges());
Alexey Samsonova1694c12012-11-16 08:36:25 +0000692 Aranges->generate(this);
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000693 return Aranges.get();
694}
695
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000696const DWARFDebugFrame *DWARFContext::getDebugFrame() {
697 if (DebugFrame)
698 return DebugFrame.get();
699
700 // There's a "bug" in the DWARFv3 standard with respect to the target address
701 // size within debug frame sections. While DWARF is supposed to be independent
702 // of its container, FDEs have fields with size being "target address size",
703 // which isn't specified in DWARF in general. It's only specified for CUs, but
704 // .eh_frame can appear without a .debug_info section. Follow the example of
705 // other tools (libdwarf) and extract this from the container (ObjectFile
706 // provides this information). This problem is fixed in DWARFv4
707 // See this dwarf-discuss discussion for more details:
708 // http://lists.dwarfstd.org/htdig.cgi/dwarf-discuss-dwarfstd.org/2011-December/001173.html
Rafael Auler86fb7bf2018-03-08 00:46:53 +0000709 DWARFDataExtractor debugFrameData(DObj->getDebugFrameSection(),
710 isLittleEndian(), DObj->getAddressSize());
Igor Laevsky03a670c2016-01-26 15:09:42 +0000711 DebugFrame.reset(new DWARFDebugFrame(false /* IsEH */));
712 DebugFrame->parse(debugFrameData);
713 return DebugFrame.get();
714}
715
716const DWARFDebugFrame *DWARFContext::getEHFrame() {
717 if (EHFrame)
718 return EHFrame.get();
719
Rafael Auler86fb7bf2018-03-08 00:46:53 +0000720 DWARFDataExtractor debugFrameData(DObj->getEHFrameSection(), isLittleEndian(),
721 DObj->getAddressSize());
Igor Laevsky03a670c2016-01-26 15:09:42 +0000722 DebugFrame.reset(new DWARFDebugFrame(true /* IsEH */));
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000723 DebugFrame->parse(debugFrameData);
724 return DebugFrame.get();
725}
726
Amjad Aboude59cc3e2015-11-12 09:38:54 +0000727const DWARFDebugMacro *DWARFContext::getDebugMacro() {
728 if (Macro)
729 return Macro.get();
730
Rafael Espindolac398e672017-07-19 22:27:28 +0000731 DataExtractor MacinfoData(DObj->getMacinfoSection(), isLittleEndian(), 0);
Amjad Aboude59cc3e2015-11-12 09:38:54 +0000732 Macro.reset(new DWARFDebugMacro());
733 Macro->parse(MacinfoData);
734 return Macro.get();
735}
736
Pavel Labath3c9a9182018-01-29 11:08:32 +0000737template <typename T>
738static T &getAccelTable(std::unique_ptr<T> &Cache, const DWARFObject &Obj,
739 const DWARFSection &Section, StringRef StringSection,
740 bool IsLittleEndian) {
Adrian Prantl99fdb9d2017-09-28 18:10:52 +0000741 if (Cache)
742 return *Cache;
743 DWARFDataExtractor AccelSection(Obj, Section, IsLittleEndian, 0);
744 DataExtractor StrData(StringSection, IsLittleEndian, 0);
Pavel Labath3c9a9182018-01-29 11:08:32 +0000745 Cache.reset(new T(AccelSection, StrData));
Jonas Devlieghereba915892017-12-11 18:22:47 +0000746 if (Error E = Cache->extract())
747 llvm::consumeError(std::move(E));
Adrian Prantl99fdb9d2017-09-28 18:10:52 +0000748 return *Cache;
749}
750
Pavel Labath3c9a9182018-01-29 11:08:32 +0000751const DWARFDebugNames &DWARFContext::getDebugNames() {
752 return getAccelTable(Names, *DObj, DObj->getDebugNamesSection(),
753 DObj->getStringSection(), isLittleEndian());
754}
755
Pavel Labath9b36fd22018-01-22 13:17:23 +0000756const AppleAcceleratorTable &DWARFContext::getAppleNames() {
Adrian Prantl99fdb9d2017-09-28 18:10:52 +0000757 return getAccelTable(AppleNames, *DObj, DObj->getAppleNamesSection(),
758 DObj->getStringSection(), isLittleEndian());
759}
760
Pavel Labath9b36fd22018-01-22 13:17:23 +0000761const AppleAcceleratorTable &DWARFContext::getAppleTypes() {
Adrian Prantl714ee4d2017-09-29 00:33:22 +0000762 return getAccelTable(AppleTypes, *DObj, DObj->getAppleTypesSection(),
763 DObj->getStringSection(), isLittleEndian());
764}
765
Pavel Labath9b36fd22018-01-22 13:17:23 +0000766const AppleAcceleratorTable &DWARFContext::getAppleNamespaces() {
Adrian Prantlf51e7802017-09-29 00:52:33 +0000767 return getAccelTable(AppleNamespaces, *DObj,
768 DObj->getAppleNamespacesSection(),
769 DObj->getStringSection(), isLittleEndian());
770}
771
Pavel Labath9b36fd22018-01-22 13:17:23 +0000772const AppleAcceleratorTable &DWARFContext::getAppleObjC() {
Adrian Prantlf51e7802017-09-29 00:52:33 +0000773 return getAccelTable(AppleObjC, *DObj, DObj->getAppleObjCSection(),
774 DObj->getStringSection(), isLittleEndian());
775}
776
James Hendersona3acf992018-05-10 10:51:33 +0000777const DWARFDebugLine::LineTable *
Alexey Samsonov7a18c062015-05-19 21:54:32 +0000778DWARFContext::getLineTableForUnit(DWARFUnit *U) {
James Hendersona3acf992018-05-10 10:51:33 +0000779 Expected<const DWARFDebugLine::LineTable *> ExpectedLineTable =
780 getLineTableForUnit(U, DWARFDebugLine::warn);
781 if (!ExpectedLineTable) {
James Henderson004b7292018-05-21 15:30:54 +0000782 DWARFDebugLine::warn(ExpectedLineTable.takeError());
James Hendersona3acf992018-05-10 10:51:33 +0000783 return nullptr;
784 }
785 return *ExpectedLineTable;
786}
787
James Henderson004b7292018-05-21 15:30:54 +0000788Expected<const DWARFDebugLine::LineTable *> DWARFContext::getLineTableForUnit(
789 DWARFUnit *U, std::function<void(Error)> RecoverableErrorCallback) {
Benjamin Kramer679e1752011-09-15 20:43:18 +0000790 if (!Line)
Paul Robinson17536b92017-06-29 16:52:08 +0000791 Line.reset(new DWARFDebugLine);
David Blaikiec4e2bed2015-11-17 21:08:05 +0000792
Greg Claytonc8c10322016-12-13 18:25:19 +0000793 auto UnitDIE = U->getUnitDIE();
794 if (!UnitDIE)
Alexey Samsonov7a18c062015-05-19 21:54:32 +0000795 return nullptr;
David Blaikiec4e2bed2015-11-17 21:08:05 +0000796
Greg Clayton97d22182017-01-13 21:08:18 +0000797 auto Offset = toSectionOffset(UnitDIE.find(DW_AT_stmt_list));
Greg Clayton52fe1f62016-12-14 22:38:08 +0000798 if (!Offset)
Craig Topper2617dcc2014-04-15 06:32:26 +0000799 return nullptr; // No line table for this compile unit.
Benjamin Kramer5acab502011-09-15 02:12:05 +0000800
Greg Clayton52fe1f62016-12-14 22:38:08 +0000801 uint32_t stmtOffset = *Offset + U->getLineTableOffset();
Benjamin Kramer679e1752011-09-15 20:43:18 +0000802 // See if the line table is cached.
Eric Christopher494109b2012-10-16 23:46:25 +0000803 if (const DWARFLineTable *lt = Line->getLineTable(stmtOffset))
Benjamin Kramer679e1752011-09-15 20:43:18 +0000804 return lt;
805
Greg Clayton48ff66a2017-05-04 18:29:44 +0000806 // Make sure the offset is good before we try to parse.
Paul Robinson17536b92017-06-29 16:52:08 +0000807 if (stmtOffset >= U->getLineSection().Data.size())
Jonas Devlieghere27476ce2017-09-13 09:43:05 +0000808 return nullptr;
Greg Clayton48ff66a2017-05-04 18:29:44 +0000809
Benjamin Kramer679e1752011-09-15 20:43:18 +0000810 // We have to parse it first.
Rafael Espindolac398e672017-07-19 22:27:28 +0000811 DWARFDataExtractor lineData(*DObj, U->getLineSection(), isLittleEndian(),
Paul Robinson17536b92017-06-29 16:52:08 +0000812 U->getAddressByteSize());
James Hendersona3acf992018-05-10 10:51:33 +0000813 return Line->getOrParseLineTable(lineData, stmtOffset, *this, U,
James Henderson004b7292018-05-21 15:30:54 +0000814 RecoverableErrorCallback);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000815}
816
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000817void DWARFContext::parseCompileUnits() {
Rafael Espindolac398e672017-07-19 22:27:28 +0000818 CUs.parse(*this, DObj->getInfoSection());
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000819}
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000820
David Blaikie03c089c2013-09-23 22:44:47 +0000821void DWARFContext::parseTypeUnits() {
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000822 if (!TUs.empty())
823 return;
Rafael Espindolac398e672017-07-19 22:27:28 +0000824 DObj->forEachTypesSections([&](const DWARFSection &S) {
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000825 TUs.emplace_back();
Rafael Espindola5e5dfa12017-07-12 21:08:24 +0000826 TUs.back().parse(*this, S);
827 });
David Blaikie03c089c2013-09-23 22:44:47 +0000828}
829
Eric Christopherda4b2192013-01-02 23:52:13 +0000830void DWARFContext::parseDWOCompileUnits() {
Rafael Espindolac398e672017-07-19 22:27:28 +0000831 DWOCUs.parseDWO(*this, DObj->getInfoDWOSection());
Eric Christopherda4b2192013-01-02 23:52:13 +0000832}
833
David Blaikie92d9d622014-01-09 05:08:24 +0000834void DWARFContext::parseDWOTypeUnits() {
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000835 if (!DWOTUs.empty())
836 return;
Rafael Espindolac398e672017-07-19 22:27:28 +0000837 DObj->forEachTypesDWOSections([&](const DWARFSection &S) {
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000838 DWOTUs.emplace_back();
Rafael Espindola5e5dfa12017-07-12 21:08:24 +0000839 DWOTUs.back().parseDWO(*this, S);
840 });
David Blaikie92d9d622014-01-09 05:08:24 +0000841}
842
Alexey Samsonov45be7932012-08-30 07:49:50 +0000843DWARFCompileUnit *DWARFContext::getCompileUnitForOffset(uint32_t Offset) {
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000844 parseCompileUnits();
Frederic Riss4e126a02014-09-15 07:50:27 +0000845 return CUs.getUnitForOffset(Offset);
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000846}
847
Alexey Samsonov45be7932012-08-30 07:49:50 +0000848DWARFCompileUnit *DWARFContext::getCompileUnitForAddress(uint64_t Address) {
Benjamin Kramer112ec172011-09-15 21:59:13 +0000849 // First, get the offset of the compile unit.
Alexey Samsonov45be7932012-08-30 07:49:50 +0000850 uint32_t CUOffset = getDebugAranges()->findAddress(Address);
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000851 // Retrieve the compile unit.
Alexey Samsonov45be7932012-08-30 07:49:50 +0000852 return getCompileUnitForOffset(CUOffset);
853}
854
Jonas Devliegheref63ee642017-10-25 21:56:41 +0000855DWARFContext::DIEsForAddress DWARFContext::getDIEsForAddress(uint64_t Address) {
856 DIEsForAddress Result;
857
858 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
859 if (!CU)
860 return Result;
861
862 Result.CompileUnit = CU;
863 Result.FunctionDIE = CU->getSubroutineForAddress(Address);
864
865 std::vector<DWARFDie> Worklist;
866 Worklist.push_back(Result.FunctionDIE);
867 while (!Worklist.empty()) {
868 DWARFDie DIE = Worklist.back();
869 Worklist.pop_back();
870
871 if (DIE.getTag() == DW_TAG_lexical_block &&
872 DIE.addressRangeContainsAddress(Address)) {
873 Result.BlockDIE = DIE;
874 break;
875 }
876
877 for (auto Child : DIE)
878 Worklist.push_back(Child);
879 }
880
881 return Result;
882}
883
David Blaikieefc4eba2017-02-06 20:19:02 +0000884static bool getFunctionNameAndStartLineForAddress(DWARFCompileUnit *CU,
885 uint64_t Address,
886 FunctionNameKind Kind,
887 std::string &FunctionName,
888 uint32_t &StartLine) {
Alexey Samsonovd0109992014-04-18 21:36:39 +0000889 // The address may correspond to instruction in some inlined function,
890 // so we have to build the chain of inlined functions and take the
David Blaikieefc4eba2017-02-06 20:19:02 +0000891 // name of the topmost function in it.
Greg Claytonc8c10322016-12-13 18:25:19 +0000892 SmallVector<DWARFDie, 4> InlinedChain;
893 CU->getInlinedChainForAddress(Address, InlinedChain);
David Blaikieefc4eba2017-02-06 20:19:02 +0000894 if (InlinedChain.empty())
Alexey Samsonovd0109992014-04-18 21:36:39 +0000895 return false;
David Blaikieefc4eba2017-02-06 20:19:02 +0000896
897 const DWARFDie &DIE = InlinedChain[0];
898 bool FoundResult = false;
899 const char *Name = nullptr;
900 if (Kind != FunctionNameKind::None && (Name = DIE.getSubroutineName(Kind))) {
Alexey Samsonovd0109992014-04-18 21:36:39 +0000901 FunctionName = Name;
David Blaikieefc4eba2017-02-06 20:19:02 +0000902 FoundResult = true;
Alexey Samsonovd0109992014-04-18 21:36:39 +0000903 }
David Blaikieefc4eba2017-02-06 20:19:02 +0000904 if (auto DeclLineResult = DIE.getDeclLine()) {
905 StartLine = DeclLineResult;
906 FoundResult = true;
907 }
908
909 return FoundResult;
Alexey Samsonovd0109992014-04-18 21:36:39 +0000910}
911
Alexey Samsonov45be7932012-08-30 07:49:50 +0000912DILineInfo DWARFContext::getLineInfoForAddress(uint64_t Address,
Alexey Samsonovdce67342014-05-15 21:24:32 +0000913 DILineInfoSpecifier Spec) {
Alexey Samsonovd0109992014-04-18 21:36:39 +0000914 DILineInfo Result;
915
Alexey Samsonov45be7932012-08-30 07:49:50 +0000916 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
917 if (!CU)
Alexey Samsonovd0109992014-04-18 21:36:39 +0000918 return Result;
David Blaikieefc4eba2017-02-06 20:19:02 +0000919 getFunctionNameAndStartLineForAddress(CU, Address, Spec.FNKind,
920 Result.FunctionName,
921 Result.StartLine);
Alexey Samsonovdce67342014-05-15 21:24:32 +0000922 if (Spec.FLIKind != FileLineInfoKind::None) {
Frederic Riss101b5e22014-09-19 15:11:51 +0000923 if (const DWARFLineTable *LineTable = getLineTableForUnit(CU))
924 LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(),
925 Spec.FLIKind, Result);
Alexey Samsonovf4462fa2012-07-02 05:54:45 +0000926 }
Alexey Samsonovd0109992014-04-18 21:36:39 +0000927 return Result;
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000928}
David Blaikiea379b1812011-12-20 02:50:00 +0000929
Alexey Samsonovdce67342014-05-15 21:24:32 +0000930DILineInfoTable
931DWARFContext::getLineInfoForAddressRange(uint64_t Address, uint64_t Size,
932 DILineInfoSpecifier Spec) {
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000933 DILineInfoTable Lines;
934 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
935 if (!CU)
936 return Lines;
937
938 std::string FunctionName = "<invalid>";
David Blaikieefc4eba2017-02-06 20:19:02 +0000939 uint32_t StartLine = 0;
940 getFunctionNameAndStartLineForAddress(CU, Address, Spec.FNKind, FunctionName,
941 StartLine);
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000942
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000943 // If the Specifier says we don't need FileLineInfo, just
944 // return the top-most function at the starting address.
Alexey Samsonovdce67342014-05-15 21:24:32 +0000945 if (Spec.FLIKind == FileLineInfoKind::None) {
Alexey Samsonovd0109992014-04-18 21:36:39 +0000946 DILineInfo Result;
947 Result.FunctionName = FunctionName;
David Blaikieefc4eba2017-02-06 20:19:02 +0000948 Result.StartLine = StartLine;
Alexey Samsonovd0109992014-04-18 21:36:39 +0000949 Lines.push_back(std::make_pair(Address, Result));
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000950 return Lines;
951 }
952
Frederic Rissec8a5ba2014-09-04 06:14:40 +0000953 const DWARFLineTable *LineTable = getLineTableForUnit(CU);
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000954
955 // Get the index of row we're looking for in the line table.
956 std::vector<uint32_t> RowVector;
957 if (!LineTable->lookupAddressRange(Address, Size, RowVector))
958 return Lines;
959
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000960 for (uint32_t RowIndex : RowVector) {
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000961 // Take file number and line/column from the row.
962 const DWARFDebugLine::Row &Row = LineTable->Rows[RowIndex];
Alexey Samsonovd0109992014-04-18 21:36:39 +0000963 DILineInfo Result;
Frederic Riss101b5e22014-09-19 15:11:51 +0000964 LineTable->getFileNameByIndex(Row.File, CU->getCompilationDir(),
965 Spec.FLIKind, Result.FileName);
Alexey Samsonovd0109992014-04-18 21:36:39 +0000966 Result.FunctionName = FunctionName;
967 Result.Line = Row.Line;
968 Result.Column = Row.Column;
David Blaikieefc4eba2017-02-06 20:19:02 +0000969 Result.StartLine = StartLine;
Alexey Samsonovd0109992014-04-18 21:36:39 +0000970 Lines.push_back(std::make_pair(Row.Address, Result));
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000971 }
972
973 return Lines;
974}
975
Alexey Samsonovdce67342014-05-15 21:24:32 +0000976DIInliningInfo
977DWARFContext::getInliningInfoForAddress(uint64_t Address,
978 DILineInfoSpecifier Spec) {
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000979 DIInliningInfo InliningInfo;
980
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000981 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
982 if (!CU)
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000983 return InliningInfo;
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000984
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000985 const DWARFLineTable *LineTable = nullptr;
Greg Claytonc8c10322016-12-13 18:25:19 +0000986 SmallVector<DWARFDie, 4> InlinedChain;
987 CU->getInlinedChainForAddress(Address, InlinedChain);
988 if (InlinedChain.size() == 0) {
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000989 // If there is no DIE for address (e.g. it is in unavailable .dwo file),
990 // try to at least get file/line info from symbol table.
Alexey Samsonovdce67342014-05-15 21:24:32 +0000991 if (Spec.FLIKind != FileLineInfoKind::None) {
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000992 DILineInfo Frame;
Frederic Rissec8a5ba2014-09-04 06:14:40 +0000993 LineTable = getLineTableForUnit(CU);
Frederic Riss101b5e22014-09-19 15:11:51 +0000994 if (LineTable &&
995 LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(),
996 Spec.FLIKind, Frame))
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000997 InliningInfo.addFrame(Frame);
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000998 }
999 return InliningInfo;
1000 }
Alexey Samsonovc942e6b2012-09-04 08:12:33 +00001001
Dehao Chenef700d52017-04-17 20:10:39 +00001002 uint32_t CallFile = 0, CallLine = 0, CallColumn = 0, CallDiscriminator = 0;
Greg Claytonc8c10322016-12-13 18:25:19 +00001003 for (uint32_t i = 0, n = InlinedChain.size(); i != n; i++) {
1004 DWARFDie &FunctionDIE = InlinedChain[i];
Alexey Samsonovd0109992014-04-18 21:36:39 +00001005 DILineInfo Frame;
Alexey Samsonovc942e6b2012-09-04 08:12:33 +00001006 // Get function name if necessary.
Greg Claytonc8c10322016-12-13 18:25:19 +00001007 if (const char *Name = FunctionDIE.getSubroutineName(Spec.FNKind))
Alexey Samsonovdce67342014-05-15 21:24:32 +00001008 Frame.FunctionName = Name;
David Blaikieefc4eba2017-02-06 20:19:02 +00001009 if (auto DeclLineResult = FunctionDIE.getDeclLine())
1010 Frame.StartLine = DeclLineResult;
Alexey Samsonovdce67342014-05-15 21:24:32 +00001011 if (Spec.FLIKind != FileLineInfoKind::None) {
Alexey Samsonovc942e6b2012-09-04 08:12:33 +00001012 if (i == 0) {
1013 // For the topmost frame, initialize the line table of this
1014 // compile unit and fetch file/line info from it.
Frederic Rissec8a5ba2014-09-04 06:14:40 +00001015 LineTable = getLineTableForUnit(CU);
Alexey Samsonovc942e6b2012-09-04 08:12:33 +00001016 // For the topmost routine, get file/line info from line table.
Frederic Riss101b5e22014-09-19 15:11:51 +00001017 if (LineTable)
1018 LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(),
1019 Spec.FLIKind, Frame);
Alexey Samsonovc942e6b2012-09-04 08:12:33 +00001020 } else {
1021 // Otherwise, use call file, call line and call column from
1022 // previous DIE in inlined chain.
Frederic Riss101b5e22014-09-19 15:11:51 +00001023 if (LineTable)
1024 LineTable->getFileNameByIndex(CallFile, CU->getCompilationDir(),
1025 Spec.FLIKind, Frame.FileName);
Alexey Samsonovd0109992014-04-18 21:36:39 +00001026 Frame.Line = CallLine;
1027 Frame.Column = CallColumn;
Dehao Chenef700d52017-04-17 20:10:39 +00001028 Frame.Discriminator = CallDiscriminator;
Alexey Samsonovc942e6b2012-09-04 08:12:33 +00001029 }
1030 // Get call file/line/column of a current DIE.
1031 if (i + 1 < n) {
Dehao Chenef700d52017-04-17 20:10:39 +00001032 FunctionDIE.getCallerFrame(CallFile, CallLine, CallColumn,
1033 CallDiscriminator);
Alexey Samsonovc942e6b2012-09-04 08:12:33 +00001034 }
1035 }
Alexey Samsonovc942e6b2012-09-04 08:12:33 +00001036 InliningInfo.addFrame(Frame);
1037 }
1038 return InliningInfo;
1039}
1040
David Blaikief9803fb2017-05-23 00:30:42 +00001041std::shared_ptr<DWARFContext>
1042DWARFContext::getDWOContext(StringRef AbsolutePath) {
David Blaikie15d85fc2017-05-23 06:48:53 +00001043 if (auto S = DWP.lock()) {
David Blaikief9803fb2017-05-23 00:30:42 +00001044 DWARFContext *Ctxt = S->Context.get();
1045 return std::shared_ptr<DWARFContext>(std::move(S), Ctxt);
1046 }
1047
David Blaikie15d85fc2017-05-23 06:48:53 +00001048 std::weak_ptr<DWOFile> *Entry = &DWOFiles[AbsolutePath];
1049
1050 if (auto S = Entry->lock()) {
1051 DWARFContext *Ctxt = S->Context.get();
1052 return std::shared_ptr<DWARFContext>(std::move(S), Ctxt);
1053 }
1054
David Blaikie15d85fc2017-05-23 06:48:53 +00001055 Expected<OwningBinary<ObjectFile>> Obj = [&] {
1056 if (!CheckedForDWP) {
David Blaikiee5adb682017-07-30 01:34:08 +00001057 SmallString<128> DWPName;
1058 auto Obj = object::ObjectFile::createObjectFile(
1059 this->DWPName.empty()
1060 ? (DObj->getFileName() + ".dwp").toStringRef(DWPName)
1061 : StringRef(this->DWPName));
David Blaikie15d85fc2017-05-23 06:48:53 +00001062 if (Obj) {
1063 Entry = &DWP;
1064 return Obj;
1065 } else {
1066 CheckedForDWP = true;
1067 // TODO: Should this error be handled (maybe in a high verbosity mode)
1068 // before falling back to .dwo files?
1069 consumeError(Obj.takeError());
1070 }
1071 }
1072
1073 return object::ObjectFile::createObjectFile(AbsolutePath);
1074 }();
1075
David Blaikief9803fb2017-05-23 00:30:42 +00001076 if (!Obj) {
1077 // TODO: Actually report errors helpfully.
1078 consumeError(Obj.takeError());
1079 return nullptr;
1080 }
David Blaikie15d85fc2017-05-23 06:48:53 +00001081
1082 auto S = std::make_shared<DWOFile>();
David Blaikief9803fb2017-05-23 00:30:42 +00001083 S->File = std::move(Obj.get());
Rafael Espindolac398e672017-07-19 22:27:28 +00001084 S->Context = DWARFContext::create(*S->File.getBinary());
David Blaikie15d85fc2017-05-23 06:48:53 +00001085 *Entry = S;
David Blaikief9803fb2017-05-23 00:30:42 +00001086 auto *Ctxt = S->Context.get();
1087 return std::shared_ptr<DWARFContext>(std::move(S), Ctxt);
1088}
1089
George Rimar702dac62017-04-12 08:59:15 +00001090static Error createError(const Twine &Reason, llvm::Error E) {
1091 return make_error<StringError>(Reason + toString(std::move(E)),
1092 inconvertibleErrorCode());
1093}
1094
George Rimara25d3292017-05-27 18:10:23 +00001095/// SymInfo contains information about symbol: it's address
1096/// and section index which is -1LL for absolute symbols.
1097struct SymInfo {
1098 uint64_t Address;
1099 uint64_t SectionIndex;
1100};
1101
1102/// Returns the address of symbol relocation used against and a section index.
1103/// Used for futher relocations computation. Symbol's section load address is
1104static Expected<SymInfo> getSymbolInfo(const object::ObjectFile &Obj,
1105 const RelocationRef &Reloc,
1106 const LoadedObjectInfo *L,
1107 std::map<SymbolRef, SymInfo> &Cache) {
1108 SymInfo Ret = {0, (uint64_t)-1LL};
George Rimar702dac62017-04-12 08:59:15 +00001109 object::section_iterator RSec = Obj.section_end();
1110 object::symbol_iterator Sym = Reloc.getSymbol();
1111
George Rimara25d3292017-05-27 18:10:23 +00001112 std::map<SymbolRef, SymInfo>::iterator CacheIt = Cache.end();
George Rimar702dac62017-04-12 08:59:15 +00001113 // First calculate the address of the symbol or section as it appears
1114 // in the object file
1115 if (Sym != Obj.symbol_end()) {
George Rimar958b01a2017-05-15 11:45:28 +00001116 bool New;
George Rimara25d3292017-05-27 18:10:23 +00001117 std::tie(CacheIt, New) = Cache.insert({*Sym, {0, 0}});
George Rimar958b01a2017-05-15 11:45:28 +00001118 if (!New)
1119 return CacheIt->second;
1120
George Rimar702dac62017-04-12 08:59:15 +00001121 Expected<uint64_t> SymAddrOrErr = Sym->getAddress();
1122 if (!SymAddrOrErr)
George Rimar1af3cb22017-06-28 08:21:19 +00001123 return createError("failed to compute symbol address: ",
George Rimar702dac62017-04-12 08:59:15 +00001124 SymAddrOrErr.takeError());
1125
1126 // Also remember what section this symbol is in for later
1127 auto SectOrErr = Sym->getSection();
1128 if (!SectOrErr)
George Rimar1af3cb22017-06-28 08:21:19 +00001129 return createError("failed to get symbol section: ",
George Rimar702dac62017-04-12 08:59:15 +00001130 SectOrErr.takeError());
1131
1132 RSec = *SectOrErr;
George Rimara25d3292017-05-27 18:10:23 +00001133 Ret.Address = *SymAddrOrErr;
George Rimar702dac62017-04-12 08:59:15 +00001134 } else if (auto *MObj = dyn_cast<MachOObjectFile>(&Obj)) {
1135 RSec = MObj->getRelocationSection(Reloc.getRawDataRefImpl());
George Rimara25d3292017-05-27 18:10:23 +00001136 Ret.Address = RSec->getAddress();
George Rimar702dac62017-04-12 08:59:15 +00001137 }
1138
George Rimara25d3292017-05-27 18:10:23 +00001139 if (RSec != Obj.section_end())
1140 Ret.SectionIndex = RSec->getIndex();
1141
George Rimar702dac62017-04-12 08:59:15 +00001142 // If we are given load addresses for the sections, we need to adjust:
1143 // SymAddr = (Address of Symbol Or Section in File) -
1144 // (Address of Section in File) +
1145 // (Load Address of Section)
1146 // RSec is now either the section being targeted or the section
1147 // containing the symbol being targeted. In either case,
1148 // we need to perform the same computation.
1149 if (L && RSec != Obj.section_end())
1150 if (uint64_t SectionLoadAddress = L->getSectionLoadAddress(*RSec))
George Rimara25d3292017-05-27 18:10:23 +00001151 Ret.Address += SectionLoadAddress - RSec->getAddress();
George Rimar958b01a2017-05-15 11:45:28 +00001152
1153 if (CacheIt != Cache.end())
1154 CacheIt->second = Ret;
1155
George Rimar702dac62017-04-12 08:59:15 +00001156 return Ret;
1157}
1158
1159static bool isRelocScattered(const object::ObjectFile &Obj,
1160 const RelocationRef &Reloc) {
George Rimard4998b02017-04-13 09:52:50 +00001161 const MachOObjectFile *MachObj = dyn_cast<MachOObjectFile>(&Obj);
1162 if (!MachObj)
George Rimar702dac62017-04-12 08:59:15 +00001163 return false;
1164 // MachO also has relocations that point to sections and
1165 // scattered relocations.
George Rimar702dac62017-04-12 08:59:15 +00001166 auto RelocInfo = MachObj->getRelocation(Reloc.getRawDataRefImpl());
1167 return MachObj->isRelocationScattered(RelocInfo);
1168}
1169
Rafael Espindolac398e672017-07-19 22:27:28 +00001170ErrorPolicy DWARFContext::defaultErrorHandler(Error E) {
Jonas Devlieghere84e99262018-04-14 22:07:23 +00001171 WithColor::error() << toString(std::move(E)) << '\n';
George Rimar1af3cb22017-06-28 08:21:19 +00001172 return ErrorPolicy::Continue;
1173}
1174
Rafael Espindola87c3f4a92017-07-24 19:34:26 +00001175namespace {
1176struct DWARFSectionMap final : public DWARFSection {
1177 RelocAddrMap Relocs;
1178};
Rafael Espindola87c3f4a92017-07-24 19:34:26 +00001179
Rafael Espindolac398e672017-07-19 22:27:28 +00001180class DWARFObjInMemory final : public DWARFObject {
1181 bool IsLittleEndian;
1182 uint8_t AddressSize;
1183 StringRef FileName;
George Rimar6957ab52017-08-15 12:32:54 +00001184 const object::ObjectFile *Obj = nullptr;
George Rimare1c30f72017-08-15 15:54:43 +00001185 std::vector<SectionName> SectionNames;
Keno Fischerc780e8e2015-05-21 21:24:32 +00001186
Rafael Espindolac398e672017-07-19 22:27:28 +00001187 using TypeSectionMap = MapVector<object::SectionRef, DWARFSectionMap,
1188 std::map<object::SectionRef, unsigned>>;
Eric Christopher7370b552012-11-12 21:40:38 +00001189
Rafael Espindolac398e672017-07-19 22:27:28 +00001190 TypeSectionMap TypesSections;
1191 TypeSectionMap TypesDWOSections;
1192
1193 DWARFSectionMap InfoSection;
1194 DWARFSectionMap LocSection;
1195 DWARFSectionMap LineSection;
1196 DWARFSectionMap RangeSection;
James Henderson3fcc7452018-02-02 12:35:52 +00001197 DWARFSectionMap RnglistsSection;
Rafael Espindolac398e672017-07-19 22:27:28 +00001198 DWARFSectionMap StringOffsetSection;
1199 DWARFSectionMap InfoDWOSection;
1200 DWARFSectionMap LineDWOSection;
1201 DWARFSectionMap LocDWOSection;
1202 DWARFSectionMap StringOffsetDWOSection;
1203 DWARFSectionMap RangeDWOSection;
Wolfgang Piebad605592018-05-18 20:12:54 +00001204 DWARFSectionMap RnglistsDWOSection;
Rafael Espindolac398e672017-07-19 22:27:28 +00001205 DWARFSectionMap AddrSection;
1206 DWARFSectionMap AppleNamesSection;
1207 DWARFSectionMap AppleTypesSection;
1208 DWARFSectionMap AppleNamespacesSection;
1209 DWARFSectionMap AppleObjCSection;
Pavel Labath3c9a9182018-01-29 11:08:32 +00001210 DWARFSectionMap DebugNamesSection;
Rafael Espindolac398e672017-07-19 22:27:28 +00001211
1212 DWARFSectionMap *mapNameToDWARFSection(StringRef Name) {
1213 return StringSwitch<DWARFSectionMap *>(Name)
1214 .Case("debug_info", &InfoSection)
1215 .Case("debug_loc", &LocSection)
1216 .Case("debug_line", &LineSection)
1217 .Case("debug_str_offsets", &StringOffsetSection)
1218 .Case("debug_ranges", &RangeSection)
James Henderson3fcc7452018-02-02 12:35:52 +00001219 .Case("debug_rnglists", &RnglistsSection)
Rafael Espindolac398e672017-07-19 22:27:28 +00001220 .Case("debug_info.dwo", &InfoDWOSection)
1221 .Case("debug_loc.dwo", &LocDWOSection)
1222 .Case("debug_line.dwo", &LineDWOSection)
Pavel Labath3c9a9182018-01-29 11:08:32 +00001223 .Case("debug_names", &DebugNamesSection)
Wolfgang Piebad605592018-05-18 20:12:54 +00001224 .Case("debug_rnglists.dwo", &RnglistsDWOSection)
Rafael Espindolac398e672017-07-19 22:27:28 +00001225 .Case("debug_str_offsets.dwo", &StringOffsetDWOSection)
1226 .Case("debug_addr", &AddrSection)
1227 .Case("apple_names", &AppleNamesSection)
1228 .Case("apple_types", &AppleTypesSection)
1229 .Case("apple_namespaces", &AppleNamespacesSection)
1230 .Case("apple_namespac", &AppleNamespacesSection)
1231 .Case("apple_objc", &AppleObjCSection)
1232 .Default(nullptr);
1233 }
1234
1235 StringRef AbbrevSection;
1236 StringRef ARangeSection;
1237 StringRef DebugFrameSection;
1238 StringRef EHFrameSection;
1239 StringRef StringSection;
1240 StringRef MacinfoSection;
1241 StringRef PubNamesSection;
1242 StringRef PubTypesSection;
1243 StringRef GnuPubNamesSection;
1244 StringRef AbbrevDWOSection;
1245 StringRef StringDWOSection;
1246 StringRef GnuPubTypesSection;
1247 StringRef CUIndexSection;
1248 StringRef GdbIndexSection;
1249 StringRef TUIndexSection;
Paul Robinsonb6aa01c2018-01-25 22:02:36 +00001250 StringRef LineStringSection;
Rafael Espindolac398e672017-07-19 22:27:28 +00001251
Fangrui Song5bad9d82018-07-23 23:27:45 +00001252 // A deque holding section data whose iterators are not invalidated when
1253 // new decompressed sections are inserted at the end.
1254 std::deque<SmallString<0>> UncompressedSections;
Rafael Espindolac398e672017-07-19 22:27:28 +00001255
1256 StringRef *mapSectionToMember(StringRef Name) {
1257 if (DWARFSection *Sec = mapNameToDWARFSection(Name))
1258 return &Sec->Data;
1259 return StringSwitch<StringRef *>(Name)
1260 .Case("debug_abbrev", &AbbrevSection)
1261 .Case("debug_aranges", &ARangeSection)
1262 .Case("debug_frame", &DebugFrameSection)
1263 .Case("eh_frame", &EHFrameSection)
1264 .Case("debug_str", &StringSection)
1265 .Case("debug_macinfo", &MacinfoSection)
1266 .Case("debug_pubnames", &PubNamesSection)
1267 .Case("debug_pubtypes", &PubTypesSection)
1268 .Case("debug_gnu_pubnames", &GnuPubNamesSection)
1269 .Case("debug_gnu_pubtypes", &GnuPubTypesSection)
1270 .Case("debug_abbrev.dwo", &AbbrevDWOSection)
1271 .Case("debug_str.dwo", &StringDWOSection)
1272 .Case("debug_cu_index", &CUIndexSection)
1273 .Case("debug_tu_index", &TUIndexSection)
1274 .Case("gdb_index", &GdbIndexSection)
Paul Robinsonb6aa01c2018-01-25 22:02:36 +00001275 .Case("debug_line_str", &LineStringSection)
Rafael Espindolac398e672017-07-19 22:27:28 +00001276 // Any more debug info sections go here.
1277 .Default(nullptr);
1278 }
1279
1280 /// If Sec is compressed section, decompresses and updates its contents
1281 /// provided by Data. Otherwise leaves it unchanged.
1282 Error maybeDecompress(const object::SectionRef &Sec, StringRef Name,
1283 StringRef &Data) {
1284 if (!Decompressor::isCompressed(Sec))
1285 return Error::success();
1286
1287 Expected<Decompressor> Decompressor =
1288 Decompressor::create(Name, Data, IsLittleEndian, AddressSize == 8);
1289 if (!Decompressor)
1290 return Decompressor.takeError();
1291
Fangrui Song5bad9d82018-07-23 23:27:45 +00001292 SmallString<0> Out;
Rafael Espindolac398e672017-07-19 22:27:28 +00001293 if (auto Err = Decompressor->resizeAndDecompress(Out))
1294 return Err;
1295
Fangrui Song5bad9d82018-07-23 23:27:45 +00001296 UncompressedSections.push_back(std::move(Out));
Rafael Espindolac398e672017-07-19 22:27:28 +00001297 Data = UncompressedSections.back();
1298
1299 return Error::success();
1300 }
1301
1302public:
1303 DWARFObjInMemory(const StringMap<std::unique_ptr<MemoryBuffer>> &Sections,
1304 uint8_t AddrSize, bool IsLittleEndian)
1305 : IsLittleEndian(IsLittleEndian) {
1306 for (const auto &SecIt : Sections) {
1307 if (StringRef *SectionData = mapSectionToMember(SecIt.first()))
1308 *SectionData = SecIt.second->getBuffer();
Alexey Samsonov068fc8a2013-04-23 10:17:34 +00001309 }
Rafael Espindolac398e672017-07-19 22:27:28 +00001310 }
1311 DWARFObjInMemory(const object::ObjectFile &Obj, const LoadedObjectInfo *L,
1312 function_ref<ErrorPolicy(Error)> HandleError)
1313 : IsLittleEndian(Obj.isLittleEndian()),
George Rimar6957ab52017-08-15 12:32:54 +00001314 AddressSize(Obj.getBytesInAddress()), FileName(Obj.getFileName()),
1315 Obj(&Obj) {
Alexey Samsonov068fc8a2013-04-23 10:17:34 +00001316
George Rimare1c30f72017-08-15 15:54:43 +00001317 StringMap<unsigned> SectionAmountMap;
Rafael Espindolac398e672017-07-19 22:27:28 +00001318 for (const SectionRef &Section : Obj.sections()) {
1319 StringRef Name;
1320 Section.getName(Name);
George Rimare1c30f72017-08-15 15:54:43 +00001321 ++SectionAmountMap[Name];
1322 SectionNames.push_back({ Name, true });
1323
Rafael Espindolac398e672017-07-19 22:27:28 +00001324 // Skip BSS and Virtual sections, they aren't interesting.
1325 if (Section.isBSS() || Section.isVirtual())
George Rimarfed9f092017-05-17 12:10:51 +00001326 continue;
Keno Fischerc780e8e2015-05-21 21:24:32 +00001327
Jonas Devlieghere8af23872017-09-26 14:22:35 +00001328 // Skip sections stripped by dsymutil.
1329 if (Section.isStripped())
1330 continue;
1331
Rafael Espindolac398e672017-07-19 22:27:28 +00001332 StringRef Data;
1333 section_iterator RelocatedSection = Section.getRelocatedSection();
1334 // Try to obtain an already relocated version of this section.
1335 // Else use the unrelocated section from the object file. We'll have to
1336 // apply relocations ourselves later.
1337 if (!L || !L->getLoadedSectionContents(*RelocatedSection, Data))
1338 Section.getContents(Data);
George Rimarfed9f092017-05-17 12:10:51 +00001339
Rafael Espindolac398e672017-07-19 22:27:28 +00001340 if (auto Err = maybeDecompress(Section, Name, Data)) {
1341 ErrorPolicy EP = HandleError(createError(
1342 "failed to decompress '" + Name + "', ", std::move(Err)));
George Rimar1af3cb22017-06-28 08:21:19 +00001343 if (EP == ErrorPolicy::Halt)
1344 return;
George Rimarfed9f092017-05-17 12:10:51 +00001345 continue;
1346 }
Rafael Espindolac398e672017-07-19 22:27:28 +00001347
1348 // Compressed sections names in GNU style starts from ".z",
1349 // at this point section is decompressed and we drop compression prefix.
1350 Name = Name.substr(
1351 Name.find_first_not_of("._z")); // Skip ".", "z" and "_" prefixes.
1352
1353 // Map platform specific debug section names to DWARF standard section
1354 // names.
1355 Name = Obj.mapDebugSectionName(Name);
1356
1357 if (StringRef *SectionData = mapSectionToMember(Name)) {
1358 *SectionData = Data;
1359 if (Name == "debug_ranges") {
1360 // FIXME: Use the other dwo range section when we emit it.
1361 RangeDWOSection.Data = Data;
1362 }
1363 } else if (Name == "debug_types") {
1364 // Find debug_types data by section rather than name as there are
1365 // multiple, comdat grouped, debug_types sections.
1366 TypesSections[Section].Data = Data;
1367 } else if (Name == "debug_types.dwo") {
1368 TypesDWOSections[Section].Data = Data;
1369 }
1370
1371 if (RelocatedSection == Obj.section_end())
1372 continue;
1373
1374 StringRef RelSecName;
1375 StringRef RelSecData;
1376 RelocatedSection->getName(RelSecName);
1377
1378 // If the section we're relocating was relocated already by the JIT,
1379 // then we used the relocated version above, so we do not need to process
1380 // relocations for it now.
1381 if (L && L->getLoadedSectionContents(*RelocatedSection, RelSecData))
1382 continue;
1383
1384 // In Mach-o files, the relocations do not need to be applied if
1385 // there is no load offset to apply. The value read at the
1386 // relocation point already factors in the section address
1387 // (actually applying the relocations will produce wrong results
1388 // as the section address will be added twice).
1389 if (!L && isa<MachOObjectFile>(&Obj))
1390 continue;
1391
1392 RelSecName = RelSecName.substr(
1393 RelSecName.find_first_not_of("._z")); // Skip . and _ prefixes.
1394
1395 // TODO: Add support for relocations in other sections as needed.
1396 // Record relocations for the debug_info and debug_line sections.
1397 DWARFSectionMap *Sec = mapNameToDWARFSection(RelSecName);
1398 RelocAddrMap *Map = Sec ? &Sec->Relocs : nullptr;
1399 if (!Map) {
1400 // Find debug_types relocs by section rather than name as there are
1401 // multiple, comdat grouped, debug_types sections.
1402 if (RelSecName == "debug_types")
1403 Map =
1404 &static_cast<DWARFSectionMap &>(TypesSections[*RelocatedSection])
1405 .Relocs;
1406 else if (RelSecName == "debug_types.dwo")
1407 Map = &static_cast<DWARFSectionMap &>(
1408 TypesDWOSections[*RelocatedSection])
1409 .Relocs;
1410 else
1411 continue;
1412 }
1413
1414 if (Section.relocation_begin() == Section.relocation_end())
1415 continue;
1416
1417 // Symbol to [address, section index] cache mapping.
1418 std::map<SymbolRef, SymInfo> AddrCache;
1419 for (const RelocationRef &Reloc : Section.relocations()) {
1420 // FIXME: it's not clear how to correctly handle scattered
1421 // relocations.
1422 if (isRelocScattered(Obj, Reloc))
1423 continue;
1424
1425 Expected<SymInfo> SymInfoOrErr =
1426 getSymbolInfo(Obj, Reloc, L, AddrCache);
1427 if (!SymInfoOrErr) {
1428 if (HandleError(SymInfoOrErr.takeError()) == ErrorPolicy::Halt)
1429 return;
1430 continue;
1431 }
1432
1433 object::RelocVisitor V(Obj);
1434 uint64_t Val = V.visit(Reloc.getType(), Reloc, SymInfoOrErr->Address);
1435 if (V.error()) {
1436 SmallString<32> Type;
1437 Reloc.getTypeName(Type);
1438 ErrorPolicy EP = HandleError(
1439 createError("failed to compute relocation: " + Type + ", ",
1440 errorCodeToError(object_error::parse_failed)));
1441 if (EP == ErrorPolicy::Halt)
1442 return;
1443 continue;
1444 }
1445 RelocAddrEntry Rel = {SymInfoOrErr->SectionIndex, Val};
1446 Map->insert({Reloc.getOffset(), Rel});
1447 }
Eric Christopher7370b552012-11-12 21:40:38 +00001448 }
George Rimare1c30f72017-08-15 15:54:43 +00001449
1450 for (SectionName &S : SectionNames)
1451 if (SectionAmountMap[S.Name] > 1)
1452 S.IsNameUnique = false;
Eric Christopher7370b552012-11-12 21:40:38 +00001453 }
Eric Christopher7370b552012-11-12 21:40:38 +00001454
Rafael Espindolac398e672017-07-19 22:27:28 +00001455 Optional<RelocAddrEntry> find(const DWARFSection &S,
1456 uint64_t Pos) const override {
1457 auto &Sec = static_cast<const DWARFSectionMap &>(S);
1458 RelocAddrMap::const_iterator AI = Sec.Relocs.find(Pos);
1459 if (AI == Sec.Relocs.end())
1460 return None;
1461 return AI->second;
Chris Bieneman2e752db2017-01-20 19:03:14 +00001462 }
Rafael Espindolac398e672017-07-19 22:27:28 +00001463
George Rimar6957ab52017-08-15 12:32:54 +00001464 const object::ObjectFile *getFile() const override { return Obj; }
1465
George Rimare1c30f72017-08-15 15:54:43 +00001466 ArrayRef<SectionName> getSectionNames() const override {
1467 return SectionNames;
1468 }
1469
Rafael Espindolac398e672017-07-19 22:27:28 +00001470 bool isLittleEndian() const override { return IsLittleEndian; }
1471 StringRef getAbbrevDWOSection() const override { return AbbrevDWOSection; }
1472 const DWARFSection &getLineDWOSection() const override {
1473 return LineDWOSection;
1474 }
1475 const DWARFSection &getLocDWOSection() const override {
1476 return LocDWOSection;
1477 }
1478 StringRef getStringDWOSection() const override { return StringDWOSection; }
1479 const DWARFSection &getStringOffsetDWOSection() const override {
1480 return StringOffsetDWOSection;
1481 }
1482 const DWARFSection &getRangeDWOSection() const override {
1483 return RangeDWOSection;
1484 }
Wolfgang Piebad605592018-05-18 20:12:54 +00001485 const DWARFSection &getRnglistsDWOSection() const override {
1486 return RnglistsDWOSection;
1487 }
Rafael Espindolac398e672017-07-19 22:27:28 +00001488 const DWARFSection &getAddrSection() const override { return AddrSection; }
1489 StringRef getCUIndexSection() const override { return CUIndexSection; }
1490 StringRef getGdbIndexSection() const override { return GdbIndexSection; }
1491 StringRef getTUIndexSection() const override { return TUIndexSection; }
1492
1493 // DWARF v5
1494 const DWARFSection &getStringOffsetSection() const override {
1495 return StringOffsetSection;
1496 }
Paul Robinsonb6aa01c2018-01-25 22:02:36 +00001497 StringRef getLineStringSection() const override { return LineStringSection; }
Rafael Espindolac398e672017-07-19 22:27:28 +00001498
1499 // Sections for DWARF5 split dwarf proposal.
1500 const DWARFSection &getInfoDWOSection() const override {
1501 return InfoDWOSection;
1502 }
1503 void forEachTypesDWOSections(
1504 function_ref<void(const DWARFSection &)> F) const override {
1505 for (auto &P : TypesDWOSections)
1506 F(P.second);
1507 }
1508
1509 StringRef getAbbrevSection() const override { return AbbrevSection; }
1510 const DWARFSection &getLocSection() const override { return LocSection; }
1511 StringRef getARangeSection() const override { return ARangeSection; }
1512 StringRef getDebugFrameSection() const override { return DebugFrameSection; }
1513 StringRef getEHFrameSection() const override { return EHFrameSection; }
1514 const DWARFSection &getLineSection() const override { return LineSection; }
1515 StringRef getStringSection() const override { return StringSection; }
1516 const DWARFSection &getRangeSection() const override { return RangeSection; }
James Henderson3fcc7452018-02-02 12:35:52 +00001517 const DWARFSection &getRnglistsSection() const override {
1518 return RnglistsSection;
1519 }
Rafael Espindolac398e672017-07-19 22:27:28 +00001520 StringRef getMacinfoSection() const override { return MacinfoSection; }
1521 StringRef getPubNamesSection() const override { return PubNamesSection; }
1522 StringRef getPubTypesSection() const override { return PubTypesSection; }
1523 StringRef getGnuPubNamesSection() const override {
1524 return GnuPubNamesSection;
1525 }
1526 StringRef getGnuPubTypesSection() const override {
1527 return GnuPubTypesSection;
1528 }
1529 const DWARFSection &getAppleNamesSection() const override {
1530 return AppleNamesSection;
1531 }
1532 const DWARFSection &getAppleTypesSection() const override {
1533 return AppleTypesSection;
1534 }
1535 const DWARFSection &getAppleNamespacesSection() const override {
1536 return AppleNamespacesSection;
1537 }
1538 const DWARFSection &getAppleObjCSection() const override {
1539 return AppleObjCSection;
1540 }
Pavel Labath3c9a9182018-01-29 11:08:32 +00001541 const DWARFSection &getDebugNamesSection() const override {
1542 return DebugNamesSection;
1543 }
Rafael Espindolac398e672017-07-19 22:27:28 +00001544
1545 StringRef getFileName() const override { return FileName; }
1546 uint8_t getAddressSize() const override { return AddressSize; }
1547 const DWARFSection &getInfoSection() const override { return InfoSection; }
1548 void forEachTypesSections(
1549 function_ref<void(const DWARFSection &)> F) const override {
1550 for (auto &P : TypesSections)
1551 F(P.second);
1552 }
1553};
Benjamin Kramer49a49fe2017-08-20 13:03:48 +00001554} // namespace
Rafael Espindolac398e672017-07-19 22:27:28 +00001555
1556std::unique_ptr<DWARFContext>
1557DWARFContext::create(const object::ObjectFile &Obj, const LoadedObjectInfo *L,
David Blaikiee5adb682017-07-30 01:34:08 +00001558 function_ref<ErrorPolicy(Error)> HandleError,
1559 std::string DWPName) {
Reid Kleckner388f8802017-07-19 23:42:53 +00001560 auto DObj = llvm::make_unique<DWARFObjInMemory>(Obj, L, HandleError);
David Blaikiee5adb682017-07-30 01:34:08 +00001561 return llvm::make_unique<DWARFContext>(std::move(DObj), std::move(DWPName));
Chris Bieneman2e752db2017-01-20 19:03:14 +00001562}
1563
Rafael Espindolac398e672017-07-19 22:27:28 +00001564std::unique_ptr<DWARFContext>
1565DWARFContext::create(const StringMap<std::unique_ptr<MemoryBuffer>> &Sections,
1566 uint8_t AddrSize, bool isLittleEndian) {
Reid Kleckner388f8802017-07-19 23:42:53 +00001567 auto DObj =
1568 llvm::make_unique<DWARFObjInMemory>(Sections, AddrSize, isLittleEndian);
David Blaikiee5adb682017-07-30 01:34:08 +00001569 return llvm::make_unique<DWARFContext>(std::move(DObj), "");
Rafael Espindola77e87b32017-07-07 05:36:53 +00001570}
Reid Klecknera0587362017-08-29 21:41:21 +00001571
1572Error DWARFContext::loadRegisterInfo(const object::ObjectFile &Obj) {
1573 // Detect the architecture from the object file. We usually don't need OS
1574 // info to lookup a target and create register info.
1575 Triple TT;
1576 TT.setArch(Triple::ArchType(Obj.getArch()));
1577 TT.setVendor(Triple::UnknownVendor);
1578 TT.setOS(Triple::UnknownOS);
1579 std::string TargetLookupError;
1580 const Target *TheTarget =
1581 TargetRegistry::lookupTarget(TT.str(), TargetLookupError);
1582 if (!TargetLookupError.empty())
1583 return make_error<StringError>(TargetLookupError, inconvertibleErrorCode());
1584 RegInfo.reset(TheTarget->createMCRegInfo(TT.str()));
1585 return Error::success();
1586}