blob: a4053a30536a3b6b75d295c647e1776612890eb7 [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>
Greg Claytonc7695a82017-05-02 20:28:33 +000051#include <map>
Eugene Zelenko28db7e62017-03-01 01:14:23 +000052#include <string>
53#include <utility>
54#include <vector>
55
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000056using namespace llvm;
Benjamin Kramer6dda0322011-09-15 18:02:20 +000057using namespace dwarf;
Rafael Espindola4f60a382013-05-30 03:05:14 +000058using namespace object;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000059
Chandler Carruthe96dd892014-04-21 22:55:11 +000060#define DEBUG_TYPE "dwarf"
61
Eugene Zelenko2db0cfa2017-06-23 21:57:40 +000062using DWARFLineTable = DWARFDebugLine::LineTable;
63using FileLineInfoKind = DILineInfoSpecifier::FileLineInfoKind;
64using FunctionNameKind = DILineInfoSpecifier::FunctionNameKind;
Eric Christopher494109b2012-10-16 23:46:25 +000065
Reid Klecknera0587362017-08-29 21:41:21 +000066DWARFContext::DWARFContext(std::unique_ptr<const DWARFObject> DObj,
67 std::string DWPName)
68 : DIContext(CK_DWARF), DWPName(std::move(DWPName)), DObj(std::move(DObj)) {}
69
70DWARFContext::~DWARFContext() = default;
71
Adrian Prantl3dcd1222017-09-13 18:22:59 +000072/// Dump the UUID load command.
73static void dumpUUID(raw_ostream &OS, const ObjectFile &Obj) {
74 auto *MachO = dyn_cast<MachOObjectFile>(&Obj);
75 if (!MachO)
76 return;
77 for (auto LC : MachO->load_commands()) {
78 raw_ostream::uuid_t UUID;
79 if (LC.C.cmd == MachO::LC_UUID) {
80 if (LC.C.cmdsize < sizeof(UUID) + sizeof(LC.C)) {
81 OS << "error: UUID load command is too short.\n";
82 return;
83 }
84 OS << "UUID: ";
85 memcpy(&UUID, LC.Ptr+sizeof(LC.C), sizeof(UUID));
86 OS.write_uuid(UUID);
Adrian Prantl146ed402018-01-05 21:44:17 +000087 Triple T = MachO->getArchTriple();
88 OS << " (" << T.getArchName() << ')';
Adrian Prantl3dcd1222017-09-13 18:22:59 +000089 OS << ' ' << MachO->getFileName() << '\n';
90 }
91 }
92}
93
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +000094using ContributionCollection =
95 std::vector<Optional<StrOffsetsContributionDescriptor>>;
96
97// Collect all the contributions to the string offsets table from all units,
98// sort them by their starting offsets and remove duplicates.
99static ContributionCollection
100collectContributionData(DWARFContext::cu_iterator_range CUs,
101 DWARFContext::tu_section_iterator_range TUSs) {
102 ContributionCollection Contributions;
103 for (const auto &CU : CUs)
104 Contributions.push_back(CU->getStringOffsetsTableContribution());
105 for (const auto &TUS : TUSs)
106 for (const auto &TU : TUS)
107 Contributions.push_back(TU->getStringOffsetsTableContribution());
108
109 // Sort the contributions so that any invalid ones are placed at
110 // the start of the contributions vector. This way they are reported
111 // first.
Mandeep Singh Grangfe1d28e2018-04-01 16:18:49 +0000112 llvm::sort(Contributions.begin(), Contributions.end(),
113 [](const Optional<StrOffsetsContributionDescriptor> &L,
114 const Optional<StrOffsetsContributionDescriptor> &R) {
115 if (L && R) return L->Base < R->Base;
116 return R.hasValue();
117 });
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000118
119 // Uniquify contributions, as it is possible that units (specifically
120 // type units in dwo or dwp files) share contributions. We don't want
121 // to report them more than once.
122 Contributions.erase(
123 std::unique(Contributions.begin(), Contributions.end(),
124 [](const Optional<StrOffsetsContributionDescriptor> &L,
125 const Optional<StrOffsetsContributionDescriptor> &R) {
126 if (L && R)
127 return L->Base == R->Base && L->Size == R->Size;
128 return false;
129 }),
130 Contributions.end());
131 return Contributions;
132}
133
134static void dumpDWARFv5StringOffsetsSection(
135 raw_ostream &OS, StringRef SectionName, const DWARFObject &Obj,
136 const DWARFSection &StringOffsetsSection, StringRef StringSection,
137 DWARFContext::cu_iterator_range CUs,
138 DWARFContext::tu_section_iterator_range TUSs, bool LittleEndian) {
139 auto Contributions = collectContributionData(CUs, TUSs);
Rafael Espindolac398e672017-07-19 22:27:28 +0000140 DWARFDataExtractor StrOffsetExt(Obj, StringOffsetsSection, LittleEndian, 0);
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000141 DataExtractor StrData(StringSection, LittleEndian, 0);
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000142 uint64_t SectionSize = StringOffsetsSection.Data.size();
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000143 uint32_t Offset = 0;
144 for (auto &Contribution : Contributions) {
145 // Report an ill-formed contribution.
146 if (!Contribution) {
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000147 OS << "error: invalid contribution to string offsets table in section ."
148 << SectionName << ".\n";
149 return;
150 }
151
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000152 dwarf::DwarfFormat Format = Contribution->getFormat();
153 uint16_t Version = Contribution->getVersion();
154 uint64_t ContributionHeader = Contribution->Base;
155 // In DWARF v5 there is a contribution header that immediately precedes
156 // the string offsets base (the location we have previously retrieved from
157 // the CU DIE's DW_AT_str_offsets attribute). The header is located either
158 // 8 or 16 bytes before the base, depending on the contribution's format.
159 if (Version >= 5)
160 ContributionHeader -= Format == DWARF32 ? 8 : 16;
161
162 // Detect overlapping contributions.
163 if (Offset > ContributionHeader) {
164 OS << "error: overlapping contributions to string offsets table in "
165 "section ."
166 << SectionName << ".\n";
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000167 return;
168 }
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000169 // Report a gap in the table.
170 if (Offset < ContributionHeader) {
171 OS << format("0x%8.8x: Gap, length = ", Offset);
172 OS << (ContributionHeader - Offset) << "\n";
173 }
Wolfgang Piebb4ba1aa2017-12-22 01:12:24 +0000174 OS << format("0x%8.8x: ", (uint32_t)ContributionHeader);
Wolfgang Piebf2b6915e2018-05-10 20:02:34 +0000175 // In DWARF v5 the contribution size in the descriptor does not equal
176 // the originally encoded length (it does not contain the length of the
177 // version field and the padding, a total of 4 bytes). Add them back in
178 // for reporting.
179 OS << "Contribution size = " << (Contribution->Size + (Version < 5 ? 0 : 4))
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000180 << ", Format = " << (Format == DWARF32 ? "DWARF32" : "DWARF64")
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000181 << ", Version = " << Version << "\n";
182
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000183 Offset = Contribution->Base;
184 unsigned EntrySize = Contribution->getDwarfOffsetByteSize();
185 while (Offset - Contribution->Base < Contribution->Size) {
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000186 OS << format("0x%8.8x: ", Offset);
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000187 // FIXME: We can only extract strings if the offset fits in 32 bits.
Paul Robinson17536b92017-06-29 16:52:08 +0000188 uint64_t StringOffset =
189 StrOffsetExt.getRelocatedValue(EntrySize, &Offset);
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000190 // Extract the string if we can and display it. Otherwise just report
191 // the offset.
192 if (StringOffset <= std::numeric_limits<uint32_t>::max()) {
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000193 uint32_t StringOffset32 = (uint32_t)StringOffset;
Simon Dardisb1b52c02017-08-07 16:08:11 +0000194 OS << format("%8.8x ", StringOffset32);
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000195 const char *S = StrData.getCStr(&StringOffset32);
196 if (S)
197 OS << format("\"%s\"", S);
198 } else
Simon Dardisec4ea992017-08-07 13:30:03 +0000199 OS << format("%16.16" PRIx64 " ", StringOffset);
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000200 OS << "\n";
201 }
202 }
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000203 // Report a gap at the end of the table.
204 if (Offset < SectionSize) {
205 OS << format("0x%8.8x: Gap, length = ", Offset);
206 OS << (SectionSize - Offset) << "\n";
207 }
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000208}
209
210// Dump a DWARF string offsets section. This may be a DWARF v5 formatted
211// string offsets section, where each compile or type unit contributes a
212// number of entries (string offsets), with each contribution preceded by
213// a header containing size and version number. Alternatively, it may be a
214// monolithic series of string offsets, as generated by the pre-DWARF v5
215// implementation of split DWARF.
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000216static void dumpStringOffsetsSection(
217 raw_ostream &OS, StringRef SectionName, const DWARFObject &Obj,
218 const DWARFSection &StringOffsetsSection, StringRef StringSection,
219 DWARFContext::cu_iterator_range CUs,
220 DWARFContext::tu_section_iterator_range TUSs, bool LittleEndian,
221 unsigned MaxVersion) {
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000222 // If we have at least one (compile or type) unit with DWARF v5 or greater,
223 // we assume that the section is formatted like a DWARF v5 string offsets
224 // section.
225 if (MaxVersion >= 5)
Rafael Espindolac398e672017-07-19 22:27:28 +0000226 dumpDWARFv5StringOffsetsSection(OS, SectionName, Obj, StringOffsetsSection,
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000227 StringSection, CUs, TUSs, LittleEndian);
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000228 else {
229 DataExtractor strOffsetExt(StringOffsetsSection.Data, LittleEndian, 0);
230 uint32_t offset = 0;
231 uint64_t size = StringOffsetsSection.Data.size();
232 // Ensure that size is a multiple of the size of an entry.
233 if (size & ((uint64_t)(sizeof(uint32_t) - 1))) {
234 OS << "error: size of ." << SectionName << " is not a multiple of "
235 << sizeof(uint32_t) << ".\n";
236 size &= -(uint64_t)sizeof(uint32_t);
237 }
238 DataExtractor StrData(StringSection, LittleEndian, 0);
239 while (offset < size) {
240 OS << format("0x%8.8x: ", offset);
241 uint32_t StringOffset = strOffsetExt.getU32(&offset);
242 OS << format("%8.8x ", StringOffset);
243 const char *S = StrData.getCStr(&StringOffset);
244 if (S)
245 OS << format("\"%s\"", S);
246 OS << "\n";
247 }
248 }
249}
250
Wolfgang Piebad605592018-05-18 20:12:54 +0000251// Dump the .debug_rnglists or .debug_rnglists.dwo section (DWARF v5).
252static void dumpRnglistsSection(raw_ostream &OS,
253 DWARFDataExtractor &rnglistData,
254 DIDumpOptions DumpOpts) {
255 uint32_t Offset = 0;
256 while (rnglistData.isValidOffset(Offset)) {
257 llvm::DWARFDebugRnglistTable Rnglists;
258 uint32_t TableOffset = Offset;
259 if (Error Err = Rnglists.extract(rnglistData, &Offset)) {
260 WithColor::error() << toString(std::move(Err)) << '\n';
261 uint64_t Length = Rnglists.length();
262 // Keep going after an error, if we can, assuming that the length field
263 // could be read. If it couldn't, stop reading the section.
264 if (Length == 0)
265 break;
266 Offset = TableOffset + Length;
267 } else {
268 Rnglists.dump(OS, DumpOpts);
269 }
270 }
271}
272
Adrian Prantl057d3362017-09-15 23:04:04 +0000273void DWARFContext::dump(
274 raw_ostream &OS, DIDumpOptions DumpOpts,
275 std::array<Optional<uint64_t>, DIDT_ID_Count> DumpOffsets) {
276
277 Optional<uint64_t> DumpOffset;
Adrian Prantl7bc1b282017-09-11 22:59:45 +0000278 uint64_t DumpType = DumpOpts.DumpType;
Adrian Prantlf4bc1f72017-06-01 18:18:23 +0000279
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000280 StringRef Extension = sys::path::extension(DObj->getFileName());
281 bool IsDWO = (Extension == ".dwo") || (Extension == ".dwp");
282
283 // Print UUID header.
Adrian Prantl3dcd1222017-09-13 18:22:59 +0000284 const auto *ObjFile = DObj->getFile();
Adrian Prantl3dcd1222017-09-13 18:22:59 +0000285 if (DumpType & DIDT_UUID)
286 dumpUUID(OS, *ObjFile);
287
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000288 // Print a header for each explicitly-requested section.
289 // Otherwise just print one for non-empty sections.
Adrian Prantl057d3362017-09-15 23:04:04 +0000290 // Only print empty .dwo section headers when dumping a .dwo file.
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000291 bool Explicit = DumpType != DIDT_All && !IsDWO;
Adrian Prantl057d3362017-09-15 23:04:04 +0000292 bool ExplicitDWO = Explicit && IsDWO;
293 auto shouldDump = [&](bool Explicit, const char *Name, unsigned ID,
294 StringRef Section) {
295 DumpOffset = DumpOffsets[ID];
296 unsigned Mask = 1U << ID;
297 bool Should = (DumpType & Mask) && (Explicit || !Section.empty());
Adrian Prantl84168022017-09-15 17:39:50 +0000298 if (Should)
Adrian Prantl057d3362017-09-15 23:04:04 +0000299 OS << "\n" << Name << " contents:\n";
Adrian Prantl84168022017-09-15 17:39:50 +0000300 return Should;
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000301 };
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000302
303 // Dump individual sections.
Adrian Prantl057d3362017-09-15 23:04:04 +0000304 if (shouldDump(Explicit, ".debug_abbrev", DIDT_ID_DebugAbbrev,
305 DObj->getAbbrevSection()))
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000306 getDebugAbbrev()->dump(OS);
Adrian Prantl057d3362017-09-15 23:04:04 +0000307 if (shouldDump(ExplicitDWO, ".debug_abbrev.dwo", DIDT_ID_DebugAbbrev,
308 DObj->getAbbrevDWOSection()))
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000309 getDebugAbbrevDWO()->dump(OS);
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000310
Adrian Prantl057d3362017-09-15 23:04:04 +0000311 auto dumpDebugInfo = [&](bool IsExplicit, const char *Name,
312 DWARFSection Section, cu_iterator_range CUs) {
313 if (shouldDump(IsExplicit, Name, DIDT_ID_DebugInfo, Section.Data)) {
Adrian Prantlc8d86532017-09-18 21:44:40 +0000314 if (DumpOffset)
Adrian Prantld3f9f212017-09-20 17:44:00 +0000315 getDIEForOffset(DumpOffset.getValue())
316 .dump(OS, 0, DumpOpts.noImplicitRecursion());
Adrian Prantlc8d86532017-09-18 21:44:40 +0000317 else
318 for (const auto &CU : CUs)
Adrian Prantl057d3362017-09-15 23:04:04 +0000319 CU->dump(OS, DumpOpts);
320 }
321 };
322 dumpDebugInfo(Explicit, ".debug_info", DObj->getInfoSection(),
323 compile_units());
324 dumpDebugInfo(ExplicitDWO, ".debug_info.dwo", DObj->getInfoDWOSection(),
325 dwo_compile_units());
David Blaikie622dce42014-01-08 23:29:59 +0000326
Adrian Prantl099d7e42017-09-16 16:58:18 +0000327 auto dumpDebugType = [&](const char *Name,
328 tu_section_iterator_range TUSections) {
329 OS << '\n' << Name << " contents:\n";
330 DumpOffset = DumpOffsets[DIDT_ID_DebugTypes];
331 for (const auto &TUS : TUSections)
332 for (const auto &TU : TUS)
333 if (DumpOffset)
Adrian Prantld3f9f212017-09-20 17:44:00 +0000334 TU->getDIEForOffset(*DumpOffset)
335 .dump(OS, 0, DumpOpts.noImplicitRecursion());
Adrian Prantl099d7e42017-09-16 16:58:18 +0000336 else
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000337 TU->dump(OS, DumpOpts);
Adrian Prantl099d7e42017-09-16 16:58:18 +0000338 };
339 if ((DumpType & DIDT_DebugTypes)) {
340 if (Explicit || getNumTypeUnits())
341 dumpDebugType(".debug_types", type_unit_sections());
342 if (ExplicitDWO || getNumDWOTypeUnits())
343 dumpDebugType(".debug_types.dwo", dwo_type_unit_sections());
David Blaikie03c089c2013-09-23 22:44:47 +0000344 }
345
Adrian Prantl057d3362017-09-15 23:04:04 +0000346 if (shouldDump(Explicit, ".debug_loc", DIDT_ID_DebugLoc,
Adrian Prantl84168022017-09-15 17:39:50 +0000347 DObj->getLocSection().Data)) {
Jonas Devlieghere622c5632017-09-27 09:33:36 +0000348 getDebugLoc()->dump(OS, getRegisterInfo(), DumpOffset);
David Blaikie18e73502013-06-19 21:37:13 +0000349 }
Adrian Prantl057d3362017-09-15 23:04:04 +0000350 if (shouldDump(ExplicitDWO, ".debug_loc.dwo", DIDT_ID_DebugLoc,
Adrian Prantl84168022017-09-15 17:39:50 +0000351 DObj->getLocDWOSection().Data)) {
Jonas Devlieghere622c5632017-09-27 09:33:36 +0000352 getDebugLocDWO()->dump(OS, getRegisterInfo(), DumpOffset);
David Blaikie9c550ac2014-03-25 01:44:02 +0000353 }
354
Jonas Devliegherec0a758d2017-09-18 14:15:57 +0000355 if (shouldDump(Explicit, ".debug_frame", DIDT_ID_DebugFrame,
Adrian Prantl62528e62017-09-21 18:52:03 +0000356 DObj->getDebugFrameSection()))
Rafael Auler86fb7bf2018-03-08 00:46:53 +0000357 getDebugFrame()->dump(OS, getRegisterInfo(), DumpOffset);
Jonas Devliegherec0a758d2017-09-18 14:15:57 +0000358
359 if (shouldDump(Explicit, ".eh_frame", DIDT_ID_DebugFrame,
Adrian Prantl62528e62017-09-21 18:52:03 +0000360 DObj->getEHFrameSection()))
Rafael Auler86fb7bf2018-03-08 00:46:53 +0000361 getEHFrame()->dump(OS, getRegisterInfo(), DumpOffset);
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000362
Adrian Prantl7bc1b282017-09-11 22:59:45 +0000363 if (DumpType & DIDT_DebugMacro) {
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000364 if (Explicit || !getDebugMacro()->empty()) {
365 OS << "\n.debug_macinfo contents:\n";
366 getDebugMacro()->dump(OS);
367 }
Amjad Aboude59cc3e2015-11-12 09:38:54 +0000368 }
369
Adrian Prantl057d3362017-09-15 23:04:04 +0000370 if (shouldDump(Explicit, ".debug_aranges", DIDT_ID_DebugAranges,
Adrian Prantl84168022017-09-15 17:39:50 +0000371 DObj->getARangeSection())) {
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000372 uint32_t offset = 0;
Rafael Espindolac398e672017-07-19 22:27:28 +0000373 DataExtractor arangesData(DObj->getARangeSection(), isLittleEndian(), 0);
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000374 DWARFDebugArangeSet set;
375 while (set.extract(arangesData, &offset))
376 set.dump(OS);
377 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000378
James Hendersona3acf992018-05-10 10:51:33 +0000379 auto DumpLineSection = [&](DWARFDebugLine::SectionParser Parser,
380 DIDumpOptions DumpOpts) {
381 while (!Parser.done()) {
382 if (DumpOffset && Parser.getOffset() != *DumpOffset) {
383 Parser.skip();
Alexey Samsonov7a18c062015-05-19 21:54:32 +0000384 continue;
Paul Robinson63811a42017-11-22 15:33:17 +0000385 }
James Hendersona3acf992018-05-10 10:51:33 +0000386 OS << "debug_line[" << format("0x%8.8x", Parser.getOffset()) << "]\n";
Paul Robinson63811a42017-11-22 15:33:17 +0000387 if (DumpOpts.Verbose) {
James Henderson004b7292018-05-21 15:30:54 +0000388 Parser.parseNext(DWARFDebugLine::warn, DWARFDebugLine::warn, &OS);
Paul Robinson63811a42017-11-22 15:33:17 +0000389 } else {
James Hendersona3acf992018-05-10 10:51:33 +0000390 DWARFDebugLine::LineTable LineTable = Parser.parseNext();
391 LineTable.dump(OS, DumpOpts);
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000392 }
Benjamin Kramer6dda0322011-09-15 18:02:20 +0000393 }
James Hendersona3acf992018-05-10 10:51:33 +0000394 };
395
396 if (shouldDump(Explicit, ".debug_line", DIDT_ID_DebugLine,
397 DObj->getLineSection().Data)) {
398 DWARFDataExtractor LineData(*DObj, DObj->getLineSection(), isLittleEndian(),
399 0);
400 DWARFDebugLine::SectionParser Parser(LineData, *this, compile_units(),
401 type_unit_sections());
402 DumpLineSection(Parser, DumpOpts);
Benjamin Kramer6dda0322011-09-15 18:02:20 +0000403 }
Benjamin Kramer07d4b1c2011-09-15 16:57:13 +0000404
Adrian Prantl057d3362017-09-15 23:04:04 +0000405 if (shouldDump(ExplicitDWO, ".debug_line.dwo", DIDT_ID_DebugLine,
Adrian Prantl84168022017-09-15 17:39:50 +0000406 DObj->getLineDWOSection().Data)) {
Paul Robinson63811a42017-11-22 15:33:17 +0000407 DWARFDataExtractor LineData(*DObj, DObj->getLineDWOSection(),
408 isLittleEndian(), 0);
James Hendersona3acf992018-05-10 10:51:33 +0000409 DWARFDebugLine::SectionParser Parser(LineData, *this, dwo_compile_units(),
410 dwo_type_unit_sections());
411 DumpLineSection(Parser, DumpOpts);
David Blaikie1d4736e2014-02-24 23:58:54 +0000412 }
413
Adrian Prantl057d3362017-09-15 23:04:04 +0000414 if (shouldDump(Explicit, ".debug_cu_index", DIDT_ID_DebugCUIndex,
Adrian Prantl84168022017-09-15 17:39:50 +0000415 DObj->getCUIndexSection())) {
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000416 getCUIndex().dump(OS);
417 }
418
Adrian Prantl057d3362017-09-15 23:04:04 +0000419 if (shouldDump(Explicit, ".debug_tu_index", DIDT_ID_DebugTUIndex,
Adrian Prantl84168022017-09-15 17:39:50 +0000420 DObj->getTUIndexSection())) {
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000421 getTUIndex().dump(OS);
422 }
423
Adrian Prantl057d3362017-09-15 23:04:04 +0000424 if (shouldDump(Explicit, ".debug_str", DIDT_ID_DebugStr,
Adrian Prantl84168022017-09-15 17:39:50 +0000425 DObj->getStringSection())) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000426 DataExtractor strData(DObj->getStringSection(), isLittleEndian(), 0);
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000427 uint32_t offset = 0;
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000428 uint32_t strOffset = 0;
429 while (const char *s = strData.getCStr(&offset)) {
430 OS << format("0x%8.8x: \"%s\"\n", strOffset, s);
431 strOffset = offset;
432 }
Benjamin Kramer07d4b1c2011-09-15 16:57:13 +0000433 }
Adrian Prantl057d3362017-09-15 23:04:04 +0000434 if (shouldDump(ExplicitDWO, ".debug_str.dwo", DIDT_ID_DebugStr,
Adrian Prantl84168022017-09-15 17:39:50 +0000435 DObj->getStringDWOSection())) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000436 DataExtractor strDWOData(DObj->getStringDWOSection(), isLittleEndian(), 0);
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000437 uint32_t offset = 0;
David Blaikie66865d62014-01-09 00:13:35 +0000438 uint32_t strDWOOffset = 0;
439 while (const char *s = strDWOData.getCStr(&offset)) {
440 OS << format("0x%8.8x: \"%s\"\n", strDWOOffset, s);
441 strDWOOffset = offset;
David Blaikie622dce42014-01-08 23:29:59 +0000442 }
David Blaikie66865d62014-01-09 00:13:35 +0000443 }
Paul Robinsonb6aa01c2018-01-25 22:02:36 +0000444 if (shouldDump(Explicit, ".debug_line_str", DIDT_ID_DebugLineStr,
445 DObj->getLineStringSection())) {
446 DataExtractor strData(DObj->getLineStringSection(), isLittleEndian(), 0);
447 uint32_t offset = 0;
448 uint32_t strOffset = 0;
449 while (const char *s = strData.getCStr(&offset)) {
Scott Linder16c7bda2018-02-23 23:01:06 +0000450 OS << format("0x%8.8x: \"", strOffset);
451 OS.write_escaped(s);
452 OS << "\"\n";
Paul Robinsonb6aa01c2018-01-25 22:02:36 +0000453 strOffset = offset;
454 }
455 }
David Blaikie622dce42014-01-08 23:29:59 +0000456
Adrian Prantl057d3362017-09-15 23:04:04 +0000457 if (shouldDump(Explicit, ".debug_ranges", DIDT_ID_DebugRanges,
Adrian Prantl84168022017-09-15 17:39:50 +0000458 DObj->getRangeSection().Data)) {
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000459 // In fact, different compile units may have different address byte
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000460 // sizes, but for simplicity we just use the address byte size of the
461 // last compile unit (there is no easy and fast way to associate address
462 // range list and the compile unit it describes).
463 // FIXME: savedAddressByteSize seems sketchy.
Paul Robinson63811a42017-11-22 15:33:17 +0000464 uint8_t savedAddressByteSize = 0;
465 for (const auto &CU : compile_units()) {
466 savedAddressByteSize = CU->getAddressByteSize();
467 break;
468 }
Rafael Espindolac398e672017-07-19 22:27:28 +0000469 DWARFDataExtractor rangesData(*DObj, DObj->getRangeSection(),
470 isLittleEndian(), savedAddressByteSize);
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000471 uint32_t offset = 0;
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000472 DWARFDebugRangeList rangeList;
Paul Robinson17536b92017-06-29 16:52:08 +0000473 while (rangeList.extract(rangesData, &offset))
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000474 rangeList.dump(OS);
Eric Christopherda4b2192013-01-02 23:52:13 +0000475 }
Eric Christopher962c9082013-01-15 23:56:56 +0000476
James Henderson3fcc7452018-02-02 12:35:52 +0000477 if (shouldDump(Explicit, ".debug_rnglists", DIDT_ID_DebugRnglists,
478 DObj->getRnglistsSection().Data)) {
Wolfgang Piebad605592018-05-18 20:12:54 +0000479 DWARFDataExtractor RnglistData(*DObj, DObj->getRnglistsSection(),
James Henderson3fcc7452018-02-02 12:35:52 +0000480 isLittleEndian(), 0);
Wolfgang Piebad605592018-05-18 20:12:54 +0000481 dumpRnglistsSection(OS, RnglistData, DumpOpts);
482 }
483
484 if (shouldDump(ExplicitDWO, ".debug_rnglists.dwo", DIDT_ID_DebugRnglists,
485 DObj->getRnglistsDWOSection().Data)) {
486 DWARFDataExtractor RnglistData(*DObj, DObj->getRnglistsDWOSection(),
487 isLittleEndian(), 0);
488 dumpRnglistsSection(OS, RnglistData, DumpOpts);
James Henderson3fcc7452018-02-02 12:35:52 +0000489 }
490
Adrian Prantl057d3362017-09-15 23:04:04 +0000491 if (shouldDump(Explicit, ".debug_pubnames", DIDT_ID_DebugPubnames,
Adrian Prantl84168022017-09-15 17:39:50 +0000492 DObj->getPubNamesSection()))
Rafael Espindolac398e672017-07-19 22:27:28 +0000493 DWARFDebugPubTable(DObj->getPubNamesSection(), isLittleEndian(), false)
Adrian Prantl84168022017-09-15 17:39:50 +0000494 .dump(OS);
Krzysztof Parzyszek97438dc2013-02-12 16:20:28 +0000495
Adrian Prantl057d3362017-09-15 23:04:04 +0000496 if (shouldDump(Explicit, ".debug_pubtypes", DIDT_ID_DebugPubtypes,
Adrian Prantl84168022017-09-15 17:39:50 +0000497 DObj->getPubTypesSection()))
Rafael Espindolac398e672017-07-19 22:27:28 +0000498 DWARFDebugPubTable(DObj->getPubTypesSection(), isLittleEndian(), false)
Adrian Prantl84168022017-09-15 17:39:50 +0000499 .dump(OS);
Eric Christopher4c7e6ba2013-09-25 23:02:41 +0000500
Adrian Prantl057d3362017-09-15 23:04:04 +0000501 if (shouldDump(Explicit, ".debug_gnu_pubnames", DIDT_ID_DebugGnuPubnames,
Adrian Prantl84168022017-09-15 17:39:50 +0000502 DObj->getGnuPubNamesSection()))
Rafael Espindolac398e672017-07-19 22:27:28 +0000503 DWARFDebugPubTable(DObj->getGnuPubNamesSection(), isLittleEndian(),
George Rimare71e33f2016-12-17 09:10:32 +0000504 true /* GnuStyle */)
Adrian Prantl84168022017-09-15 17:39:50 +0000505 .dump(OS);
David Blaikieecd21ff2013-09-24 19:50:00 +0000506
Adrian Prantl057d3362017-09-15 23:04:04 +0000507 if (shouldDump(Explicit, ".debug_gnu_pubtypes", DIDT_ID_DebugGnuPubtypes,
Adrian Prantl84168022017-09-15 17:39:50 +0000508 DObj->getGnuPubTypesSection()))
Rafael Espindolac398e672017-07-19 22:27:28 +0000509 DWARFDebugPubTable(DObj->getGnuPubTypesSection(), isLittleEndian(),
George Rimare71e33f2016-12-17 09:10:32 +0000510 true /* GnuStyle */)
Adrian Prantl84168022017-09-15 17:39:50 +0000511 .dump(OS);
David Blaikie404d3042013-09-19 23:01:29 +0000512
Adrian Prantl057d3362017-09-15 23:04:04 +0000513 if (shouldDump(Explicit, ".debug_str_offsets", DIDT_ID_DebugStrOffsets,
Adrian Prantl84168022017-09-15 17:39:50 +0000514 DObj->getStringOffsetSection().Data))
Rafael Espindolac398e672017-07-19 22:27:28 +0000515 dumpStringOffsetsSection(
516 OS, "debug_str_offsets", *DObj, DObj->getStringOffsetSection(),
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000517 DObj->getStringSection(), compile_units(), type_unit_sections(),
518 isLittleEndian(), getMaxVersion());
Adrian Prantl057d3362017-09-15 23:04:04 +0000519 if (shouldDump(ExplicitDWO, ".debug_str_offsets.dwo", DIDT_ID_DebugStrOffsets,
Adrian Prantl84168022017-09-15 17:39:50 +0000520 DObj->getStringOffsetDWOSection().Data))
Rafael Espindolac398e672017-07-19 22:27:28 +0000521 dumpStringOffsetsSection(
522 OS, "debug_str_offsets.dwo", *DObj, DObj->getStringOffsetDWOSection(),
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000523 DObj->getStringDWOSection(), dwo_compile_units(),
524 dwo_type_unit_sections(), isLittleEndian(), getMaxVersion());
Frederic Risse837ec22014-11-14 16:15:53 +0000525
Adrian Prantl057d3362017-09-15 23:04:04 +0000526 if (shouldDump(Explicit, ".gnu_index", DIDT_ID_GdbIndex,
Adrian Prantl84168022017-09-15 17:39:50 +0000527 DObj->getGdbIndexSection())) {
George Rimar4f82df52016-09-23 11:01:53 +0000528 getGdbIndex().dump(OS);
529 }
530
Adrian Prantl057d3362017-09-15 23:04:04 +0000531 if (shouldDump(Explicit, ".apple_names", DIDT_ID_AppleNames,
Adrian Prantl84168022017-09-15 17:39:50 +0000532 DObj->getAppleNamesSection().Data))
Adrian Prantl99fdb9d2017-09-28 18:10:52 +0000533 getAppleNames().dump(OS);
Frederic Risse837ec22014-11-14 16:15:53 +0000534
Adrian Prantl057d3362017-09-15 23:04:04 +0000535 if (shouldDump(Explicit, ".apple_types", DIDT_ID_AppleTypes,
Adrian Prantl84168022017-09-15 17:39:50 +0000536 DObj->getAppleTypesSection().Data))
Adrian Prantl714ee4d2017-09-29 00:33:22 +0000537 getAppleTypes().dump(OS);
Frederic Risse837ec22014-11-14 16:15:53 +0000538
Adrian Prantl057d3362017-09-15 23:04:04 +0000539 if (shouldDump(Explicit, ".apple_namespaces", DIDT_ID_AppleNamespaces,
Adrian Prantl84168022017-09-15 17:39:50 +0000540 DObj->getAppleNamespacesSection().Data))
Adrian Prantlf51e7802017-09-29 00:52:33 +0000541 getAppleNamespaces().dump(OS);
Frederic Risse837ec22014-11-14 16:15:53 +0000542
Adrian Prantl057d3362017-09-15 23:04:04 +0000543 if (shouldDump(Explicit, ".apple_objc", DIDT_ID_AppleObjC,
Adrian Prantl84168022017-09-15 17:39:50 +0000544 DObj->getAppleObjCSection().Data))
Adrian Prantlf51e7802017-09-29 00:52:33 +0000545 getAppleObjC().dump(OS);
Pavel Labath3c9a9182018-01-29 11:08:32 +0000546 if (shouldDump(Explicit, ".debug_names", DIDT_ID_DebugNames,
547 DObj->getDebugNamesSection().Data))
548 getDebugNames().dump(OS);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000549}
550
David Blaikie15d85fc2017-05-23 06:48:53 +0000551DWARFCompileUnit *DWARFContext::getDWOCompileUnitForHash(uint64_t Hash) {
David Blaikiee79dda32017-09-19 18:36:11 +0000552 DWOCUs.parseDWO(*this, DObj->getInfoDWOSection(), true);
David Blaikiea62f1cb2017-07-30 15:15:58 +0000553
David Blaikieebac0b92017-07-30 08:12:07 +0000554 if (const auto &CUI = getCUIndex()) {
555 if (const auto *R = CUI.getFromHash(Hash))
David Blaikiee79dda32017-09-19 18:36:11 +0000556 return DWOCUs.getUnitForIndexEntry(*R);
David Blaikieebac0b92017-07-30 08:12:07 +0000557 return nullptr;
558 }
559
560 // If there's no index, just search through the CUs in the DWO - there's
561 // probably only one unless this is something like LTO - though an in-process
562 // built/cached lookup table could be used in that case to improve repeated
563 // lookups of different CUs in the DWO.
Paul Robinson543c0e12018-05-22 17:27:31 +0000564 for (const auto &DWOCU : dwo_compile_units()) {
565 // Might not have parsed DWO ID yet.
566 if (!DWOCU->getDWOId()) {
567 if (Optional<uint64_t> DWOId =
568 toUnsigned(DWOCU->getUnitDIE().find(DW_AT_GNU_dwo_id)))
569 DWOCU->setDWOId(*DWOId);
570 else
571 // No DWO ID?
572 continue;
573 }
David Blaikie15d85fc2017-05-23 06:48:53 +0000574 if (DWOCU->getDWOId() == Hash)
575 return DWOCU.get();
Paul Robinson543c0e12018-05-22 17:27:31 +0000576 }
David Blaikie15d85fc2017-05-23 06:48:53 +0000577 return nullptr;
578}
579
Greg Claytonc7695a82017-05-02 20:28:33 +0000580DWARFDie DWARFContext::getDIEForOffset(uint32_t Offset) {
581 parseCompileUnits();
582 if (auto *CU = CUs.getUnitForOffset(Offset))
583 return CU->getDIEForOffset(Offset);
584 return DWARFDie();
585}
586
Jonas Devliegherec0a758d2017-09-18 14:15:57 +0000587bool DWARFContext::verify(raw_ostream &OS, DIDumpOptions DumpOpts) {
Greg Claytonc7695a82017-05-02 20:28:33 +0000588 bool Success = true;
Jonas Devlieghere27476ce2017-09-13 09:43:05 +0000589 DWARFVerifier verifier(OS, *this, DumpOpts);
Spyridoula Gravani73e17962017-07-27 00:59:33 +0000590
Spyridoula Gravani364b5352017-07-20 02:06:52 +0000591 Success &= verifier.handleDebugAbbrev();
Jonas Devliegherec0a758d2017-09-18 14:15:57 +0000592 if (DumpOpts.DumpType & DIDT_DebugInfo)
Spyridoula Gravani73e17962017-07-27 00:59:33 +0000593 Success &= verifier.handleDebugInfo();
Jonas Devliegherec0a758d2017-09-18 14:15:57 +0000594 if (DumpOpts.DumpType & DIDT_DebugLine)
Spyridoula Gravani73e17962017-07-27 00:59:33 +0000595 Success &= verifier.handleDebugLine();
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000596 Success &= verifier.handleAccelTables();
Greg Clayton48432cf2017-05-01 22:07:02 +0000597 return Success;
598}
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000599
David Blaikie82641be2015-11-17 00:39:55 +0000600const DWARFUnitIndex &DWARFContext::getCUIndex() {
601 if (CUIndex)
602 return *CUIndex;
603
Rafael Espindolac398e672017-07-19 22:27:28 +0000604 DataExtractor CUIndexData(DObj->getCUIndexSection(), isLittleEndian(), 0);
David Blaikie82641be2015-11-17 00:39:55 +0000605
David Blaikieb073cb92015-12-02 06:21:34 +0000606 CUIndex = llvm::make_unique<DWARFUnitIndex>(DW_SECT_INFO);
David Blaikie82641be2015-11-17 00:39:55 +0000607 CUIndex->parse(CUIndexData);
608 return *CUIndex;
609}
610
611const DWARFUnitIndex &DWARFContext::getTUIndex() {
612 if (TUIndex)
613 return *TUIndex;
614
Rafael Espindolac398e672017-07-19 22:27:28 +0000615 DataExtractor TUIndexData(DObj->getTUIndexSection(), isLittleEndian(), 0);
David Blaikie82641be2015-11-17 00:39:55 +0000616
David Blaikieb073cb92015-12-02 06:21:34 +0000617 TUIndex = llvm::make_unique<DWARFUnitIndex>(DW_SECT_TYPES);
David Blaikie82641be2015-11-17 00:39:55 +0000618 TUIndex->parse(TUIndexData);
619 return *TUIndex;
620}
621
George Rimar4f82df52016-09-23 11:01:53 +0000622DWARFGdbIndex &DWARFContext::getGdbIndex() {
623 if (GdbIndex)
624 return *GdbIndex;
625
Rafael Espindolac398e672017-07-19 22:27:28 +0000626 DataExtractor GdbIndexData(DObj->getGdbIndexSection(), true /*LE*/, 0);
George Rimar4f82df52016-09-23 11:01:53 +0000627 GdbIndex = llvm::make_unique<DWARFGdbIndex>();
628 GdbIndex->parse(GdbIndexData);
629 return *GdbIndex;
630}
631
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000632const DWARFDebugAbbrev *DWARFContext::getDebugAbbrev() {
633 if (Abbrev)
634 return Abbrev.get();
635
Rafael Espindolac398e672017-07-19 22:27:28 +0000636 DataExtractor abbrData(DObj->getAbbrevSection(), isLittleEndian(), 0);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000637
638 Abbrev.reset(new DWARFDebugAbbrev());
Alexey Samsonov4316df52014-04-25 21:10:56 +0000639 Abbrev->extract(abbrData);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000640 return Abbrev.get();
641}
642
Eric Christopherda4b2192013-01-02 23:52:13 +0000643const DWARFDebugAbbrev *DWARFContext::getDebugAbbrevDWO() {
644 if (AbbrevDWO)
645 return AbbrevDWO.get();
646
Rafael Espindolac398e672017-07-19 22:27:28 +0000647 DataExtractor abbrData(DObj->getAbbrevDWOSection(), isLittleEndian(), 0);
Eric Christopherda4b2192013-01-02 23:52:13 +0000648 AbbrevDWO.reset(new DWARFDebugAbbrev());
Alexey Samsonov4316df52014-04-25 21:10:56 +0000649 AbbrevDWO->extract(abbrData);
Eric Christopherda4b2192013-01-02 23:52:13 +0000650 return AbbrevDWO.get();
651}
652
David Blaikie18e73502013-06-19 21:37:13 +0000653const DWARFDebugLoc *DWARFContext::getDebugLoc() {
654 if (Loc)
655 return Loc.get();
656
Paul Robinson17536b92017-06-29 16:52:08 +0000657 Loc.reset(new DWARFDebugLoc);
Pavel Labath54ca2d62018-04-06 08:49:57 +0000658 // Assume all compile units have the same address byte size.
Paul Robinson17536b92017-06-29 16:52:08 +0000659 if (getNumCompileUnits()) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000660 DWARFDataExtractor LocData(*DObj, DObj->getLocSection(), isLittleEndian(),
Paul Robinson17536b92017-06-29 16:52:08 +0000661 getCompileUnitAtIndex(0)->getAddressByteSize());
662 Loc->parse(LocData);
663 }
David Blaikie18e73502013-06-19 21:37:13 +0000664 return Loc.get();
665}
666
David Blaikie9c550ac2014-03-25 01:44:02 +0000667const DWARFDebugLocDWO *DWARFContext::getDebugLocDWO() {
668 if (LocDWO)
669 return LocDWO.get();
670
David Blaikie9c550ac2014-03-25 01:44:02 +0000671 LocDWO.reset(new DWARFDebugLocDWO());
Pavel Labath54ca2d62018-04-06 08:49:57 +0000672 // Assume all compile units have the same address byte size.
673 if (getNumCompileUnits()) {
674 DataExtractor LocData(DObj->getLocDWOSection().Data, isLittleEndian(),
675 getCompileUnitAtIndex(0)->getAddressByteSize());
676 LocDWO->parse(LocData);
677 }
David Blaikie9c550ac2014-03-25 01:44:02 +0000678 return LocDWO.get();
679}
680
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000681const DWARFDebugAranges *DWARFContext::getDebugAranges() {
682 if (Aranges)
683 return Aranges.get();
684
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000685 Aranges.reset(new DWARFDebugAranges());
Alexey Samsonova1694c12012-11-16 08:36:25 +0000686 Aranges->generate(this);
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000687 return Aranges.get();
688}
689
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000690const DWARFDebugFrame *DWARFContext::getDebugFrame() {
691 if (DebugFrame)
692 return DebugFrame.get();
693
694 // There's a "bug" in the DWARFv3 standard with respect to the target address
695 // size within debug frame sections. While DWARF is supposed to be independent
696 // of its container, FDEs have fields with size being "target address size",
697 // which isn't specified in DWARF in general. It's only specified for CUs, but
698 // .eh_frame can appear without a .debug_info section. Follow the example of
699 // other tools (libdwarf) and extract this from the container (ObjectFile
700 // provides this information). This problem is fixed in DWARFv4
701 // See this dwarf-discuss discussion for more details:
702 // http://lists.dwarfstd.org/htdig.cgi/dwarf-discuss-dwarfstd.org/2011-December/001173.html
Rafael Auler86fb7bf2018-03-08 00:46:53 +0000703 DWARFDataExtractor debugFrameData(DObj->getDebugFrameSection(),
704 isLittleEndian(), DObj->getAddressSize());
Igor Laevsky03a670c2016-01-26 15:09:42 +0000705 DebugFrame.reset(new DWARFDebugFrame(false /* IsEH */));
706 DebugFrame->parse(debugFrameData);
707 return DebugFrame.get();
708}
709
710const DWARFDebugFrame *DWARFContext::getEHFrame() {
711 if (EHFrame)
712 return EHFrame.get();
713
Rafael Auler86fb7bf2018-03-08 00:46:53 +0000714 DWARFDataExtractor debugFrameData(DObj->getEHFrameSection(), isLittleEndian(),
715 DObj->getAddressSize());
Igor Laevsky03a670c2016-01-26 15:09:42 +0000716 DebugFrame.reset(new DWARFDebugFrame(true /* IsEH */));
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000717 DebugFrame->parse(debugFrameData);
718 return DebugFrame.get();
719}
720
Amjad Aboude59cc3e2015-11-12 09:38:54 +0000721const DWARFDebugMacro *DWARFContext::getDebugMacro() {
722 if (Macro)
723 return Macro.get();
724
Rafael Espindolac398e672017-07-19 22:27:28 +0000725 DataExtractor MacinfoData(DObj->getMacinfoSection(), isLittleEndian(), 0);
Amjad Aboude59cc3e2015-11-12 09:38:54 +0000726 Macro.reset(new DWARFDebugMacro());
727 Macro->parse(MacinfoData);
728 return Macro.get();
729}
730
Pavel Labath3c9a9182018-01-29 11:08:32 +0000731template <typename T>
732static T &getAccelTable(std::unique_ptr<T> &Cache, const DWARFObject &Obj,
733 const DWARFSection &Section, StringRef StringSection,
734 bool IsLittleEndian) {
Adrian Prantl99fdb9d2017-09-28 18:10:52 +0000735 if (Cache)
736 return *Cache;
737 DWARFDataExtractor AccelSection(Obj, Section, IsLittleEndian, 0);
738 DataExtractor StrData(StringSection, IsLittleEndian, 0);
Pavel Labath3c9a9182018-01-29 11:08:32 +0000739 Cache.reset(new T(AccelSection, StrData));
Jonas Devlieghereba915892017-12-11 18:22:47 +0000740 if (Error E = Cache->extract())
741 llvm::consumeError(std::move(E));
Adrian Prantl99fdb9d2017-09-28 18:10:52 +0000742 return *Cache;
743}
744
Pavel Labath3c9a9182018-01-29 11:08:32 +0000745const DWARFDebugNames &DWARFContext::getDebugNames() {
746 return getAccelTable(Names, *DObj, DObj->getDebugNamesSection(),
747 DObj->getStringSection(), isLittleEndian());
748}
749
Pavel Labath9b36fd22018-01-22 13:17:23 +0000750const AppleAcceleratorTable &DWARFContext::getAppleNames() {
Adrian Prantl99fdb9d2017-09-28 18:10:52 +0000751 return getAccelTable(AppleNames, *DObj, DObj->getAppleNamesSection(),
752 DObj->getStringSection(), isLittleEndian());
753}
754
Pavel Labath9b36fd22018-01-22 13:17:23 +0000755const AppleAcceleratorTable &DWARFContext::getAppleTypes() {
Adrian Prantl714ee4d2017-09-29 00:33:22 +0000756 return getAccelTable(AppleTypes, *DObj, DObj->getAppleTypesSection(),
757 DObj->getStringSection(), isLittleEndian());
758}
759
Pavel Labath9b36fd22018-01-22 13:17:23 +0000760const AppleAcceleratorTable &DWARFContext::getAppleNamespaces() {
Adrian Prantlf51e7802017-09-29 00:52:33 +0000761 return getAccelTable(AppleNamespaces, *DObj,
762 DObj->getAppleNamespacesSection(),
763 DObj->getStringSection(), isLittleEndian());
764}
765
Pavel Labath9b36fd22018-01-22 13:17:23 +0000766const AppleAcceleratorTable &DWARFContext::getAppleObjC() {
Adrian Prantlf51e7802017-09-29 00:52:33 +0000767 return getAccelTable(AppleObjC, *DObj, DObj->getAppleObjCSection(),
768 DObj->getStringSection(), isLittleEndian());
769}
770
James Hendersona3acf992018-05-10 10:51:33 +0000771const DWARFDebugLine::LineTable *
Alexey Samsonov7a18c062015-05-19 21:54:32 +0000772DWARFContext::getLineTableForUnit(DWARFUnit *U) {
James Hendersona3acf992018-05-10 10:51:33 +0000773 Expected<const DWARFDebugLine::LineTable *> ExpectedLineTable =
774 getLineTableForUnit(U, DWARFDebugLine::warn);
775 if (!ExpectedLineTable) {
James Henderson004b7292018-05-21 15:30:54 +0000776 DWARFDebugLine::warn(ExpectedLineTable.takeError());
James Hendersona3acf992018-05-10 10:51:33 +0000777 return nullptr;
778 }
779 return *ExpectedLineTable;
780}
781
James Henderson004b7292018-05-21 15:30:54 +0000782Expected<const DWARFDebugLine::LineTable *> DWARFContext::getLineTableForUnit(
783 DWARFUnit *U, std::function<void(Error)> RecoverableErrorCallback) {
Benjamin Kramer679e1752011-09-15 20:43:18 +0000784 if (!Line)
Paul Robinson17536b92017-06-29 16:52:08 +0000785 Line.reset(new DWARFDebugLine);
David Blaikiec4e2bed2015-11-17 21:08:05 +0000786
Greg Claytonc8c10322016-12-13 18:25:19 +0000787 auto UnitDIE = U->getUnitDIE();
788 if (!UnitDIE)
Alexey Samsonov7a18c062015-05-19 21:54:32 +0000789 return nullptr;
David Blaikiec4e2bed2015-11-17 21:08:05 +0000790
Greg Clayton97d22182017-01-13 21:08:18 +0000791 auto Offset = toSectionOffset(UnitDIE.find(DW_AT_stmt_list));
Greg Clayton52fe1f62016-12-14 22:38:08 +0000792 if (!Offset)
Craig Topper2617dcc2014-04-15 06:32:26 +0000793 return nullptr; // No line table for this compile unit.
Benjamin Kramer5acab502011-09-15 02:12:05 +0000794
Greg Clayton52fe1f62016-12-14 22:38:08 +0000795 uint32_t stmtOffset = *Offset + U->getLineTableOffset();
Benjamin Kramer679e1752011-09-15 20:43:18 +0000796 // See if the line table is cached.
Eric Christopher494109b2012-10-16 23:46:25 +0000797 if (const DWARFLineTable *lt = Line->getLineTable(stmtOffset))
Benjamin Kramer679e1752011-09-15 20:43:18 +0000798 return lt;
799
Greg Clayton48ff66a2017-05-04 18:29:44 +0000800 // Make sure the offset is good before we try to parse.
Paul Robinson17536b92017-06-29 16:52:08 +0000801 if (stmtOffset >= U->getLineSection().Data.size())
Jonas Devlieghere27476ce2017-09-13 09:43:05 +0000802 return nullptr;
Greg Clayton48ff66a2017-05-04 18:29:44 +0000803
Benjamin Kramer679e1752011-09-15 20:43:18 +0000804 // We have to parse it first.
Rafael Espindolac398e672017-07-19 22:27:28 +0000805 DWARFDataExtractor lineData(*DObj, U->getLineSection(), isLittleEndian(),
Paul Robinson17536b92017-06-29 16:52:08 +0000806 U->getAddressByteSize());
James Hendersona3acf992018-05-10 10:51:33 +0000807 return Line->getOrParseLineTable(lineData, stmtOffset, *this, U,
James Henderson004b7292018-05-21 15:30:54 +0000808 RecoverableErrorCallback);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000809}
810
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000811void DWARFContext::parseCompileUnits() {
Rafael Espindolac398e672017-07-19 22:27:28 +0000812 CUs.parse(*this, DObj->getInfoSection());
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000813}
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000814
David Blaikie03c089c2013-09-23 22:44:47 +0000815void DWARFContext::parseTypeUnits() {
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000816 if (!TUs.empty())
817 return;
Rafael Espindolac398e672017-07-19 22:27:28 +0000818 DObj->forEachTypesSections([&](const DWARFSection &S) {
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000819 TUs.emplace_back();
Rafael Espindola5e5dfa12017-07-12 21:08:24 +0000820 TUs.back().parse(*this, S);
821 });
David Blaikie03c089c2013-09-23 22:44:47 +0000822}
823
Eric Christopherda4b2192013-01-02 23:52:13 +0000824void DWARFContext::parseDWOCompileUnits() {
Rafael Espindolac398e672017-07-19 22:27:28 +0000825 DWOCUs.parseDWO(*this, DObj->getInfoDWOSection());
Eric Christopherda4b2192013-01-02 23:52:13 +0000826}
827
David Blaikie92d9d622014-01-09 05:08:24 +0000828void DWARFContext::parseDWOTypeUnits() {
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000829 if (!DWOTUs.empty())
830 return;
Rafael Espindolac398e672017-07-19 22:27:28 +0000831 DObj->forEachTypesDWOSections([&](const DWARFSection &S) {
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000832 DWOTUs.emplace_back();
Rafael Espindola5e5dfa12017-07-12 21:08:24 +0000833 DWOTUs.back().parseDWO(*this, S);
834 });
David Blaikie92d9d622014-01-09 05:08:24 +0000835}
836
Alexey Samsonov45be7932012-08-30 07:49:50 +0000837DWARFCompileUnit *DWARFContext::getCompileUnitForOffset(uint32_t Offset) {
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000838 parseCompileUnits();
Frederic Riss4e126a02014-09-15 07:50:27 +0000839 return CUs.getUnitForOffset(Offset);
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000840}
841
Alexey Samsonov45be7932012-08-30 07:49:50 +0000842DWARFCompileUnit *DWARFContext::getCompileUnitForAddress(uint64_t Address) {
Benjamin Kramer112ec172011-09-15 21:59:13 +0000843 // First, get the offset of the compile unit.
Alexey Samsonov45be7932012-08-30 07:49:50 +0000844 uint32_t CUOffset = getDebugAranges()->findAddress(Address);
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000845 // Retrieve the compile unit.
Alexey Samsonov45be7932012-08-30 07:49:50 +0000846 return getCompileUnitForOffset(CUOffset);
847}
848
Jonas Devliegheref63ee642017-10-25 21:56:41 +0000849DWARFContext::DIEsForAddress DWARFContext::getDIEsForAddress(uint64_t Address) {
850 DIEsForAddress Result;
851
852 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
853 if (!CU)
854 return Result;
855
856 Result.CompileUnit = CU;
857 Result.FunctionDIE = CU->getSubroutineForAddress(Address);
858
859 std::vector<DWARFDie> Worklist;
860 Worklist.push_back(Result.FunctionDIE);
861 while (!Worklist.empty()) {
862 DWARFDie DIE = Worklist.back();
863 Worklist.pop_back();
864
865 if (DIE.getTag() == DW_TAG_lexical_block &&
866 DIE.addressRangeContainsAddress(Address)) {
867 Result.BlockDIE = DIE;
868 break;
869 }
870
871 for (auto Child : DIE)
872 Worklist.push_back(Child);
873 }
874
875 return Result;
876}
877
David Blaikieefc4eba2017-02-06 20:19:02 +0000878static bool getFunctionNameAndStartLineForAddress(DWARFCompileUnit *CU,
879 uint64_t Address,
880 FunctionNameKind Kind,
881 std::string &FunctionName,
882 uint32_t &StartLine) {
Alexey Samsonovd0109992014-04-18 21:36:39 +0000883 // The address may correspond to instruction in some inlined function,
884 // so we have to build the chain of inlined functions and take the
David Blaikieefc4eba2017-02-06 20:19:02 +0000885 // name of the topmost function in it.
Greg Claytonc8c10322016-12-13 18:25:19 +0000886 SmallVector<DWARFDie, 4> InlinedChain;
887 CU->getInlinedChainForAddress(Address, InlinedChain);
David Blaikieefc4eba2017-02-06 20:19:02 +0000888 if (InlinedChain.empty())
Alexey Samsonovd0109992014-04-18 21:36:39 +0000889 return false;
David Blaikieefc4eba2017-02-06 20:19:02 +0000890
891 const DWARFDie &DIE = InlinedChain[0];
892 bool FoundResult = false;
893 const char *Name = nullptr;
894 if (Kind != FunctionNameKind::None && (Name = DIE.getSubroutineName(Kind))) {
Alexey Samsonovd0109992014-04-18 21:36:39 +0000895 FunctionName = Name;
David Blaikieefc4eba2017-02-06 20:19:02 +0000896 FoundResult = true;
Alexey Samsonovd0109992014-04-18 21:36:39 +0000897 }
David Blaikieefc4eba2017-02-06 20:19:02 +0000898 if (auto DeclLineResult = DIE.getDeclLine()) {
899 StartLine = DeclLineResult;
900 FoundResult = true;
901 }
902
903 return FoundResult;
Alexey Samsonovd0109992014-04-18 21:36:39 +0000904}
905
Alexey Samsonov45be7932012-08-30 07:49:50 +0000906DILineInfo DWARFContext::getLineInfoForAddress(uint64_t Address,
Alexey Samsonovdce67342014-05-15 21:24:32 +0000907 DILineInfoSpecifier Spec) {
Alexey Samsonovd0109992014-04-18 21:36:39 +0000908 DILineInfo Result;
909
Alexey Samsonov45be7932012-08-30 07:49:50 +0000910 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
911 if (!CU)
Alexey Samsonovd0109992014-04-18 21:36:39 +0000912 return Result;
David Blaikieefc4eba2017-02-06 20:19:02 +0000913 getFunctionNameAndStartLineForAddress(CU, Address, Spec.FNKind,
914 Result.FunctionName,
915 Result.StartLine);
Alexey Samsonovdce67342014-05-15 21:24:32 +0000916 if (Spec.FLIKind != FileLineInfoKind::None) {
Frederic Riss101b5e22014-09-19 15:11:51 +0000917 if (const DWARFLineTable *LineTable = getLineTableForUnit(CU))
918 LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(),
919 Spec.FLIKind, Result);
Alexey Samsonovf4462fa2012-07-02 05:54:45 +0000920 }
Alexey Samsonovd0109992014-04-18 21:36:39 +0000921 return Result;
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000922}
David Blaikiea379b1812011-12-20 02:50:00 +0000923
Alexey Samsonovdce67342014-05-15 21:24:32 +0000924DILineInfoTable
925DWARFContext::getLineInfoForAddressRange(uint64_t Address, uint64_t Size,
926 DILineInfoSpecifier Spec) {
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000927 DILineInfoTable Lines;
928 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
929 if (!CU)
930 return Lines;
931
932 std::string FunctionName = "<invalid>";
David Blaikieefc4eba2017-02-06 20:19:02 +0000933 uint32_t StartLine = 0;
934 getFunctionNameAndStartLineForAddress(CU, Address, Spec.FNKind, FunctionName,
935 StartLine);
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000936
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000937 // If the Specifier says we don't need FileLineInfo, just
938 // return the top-most function at the starting address.
Alexey Samsonovdce67342014-05-15 21:24:32 +0000939 if (Spec.FLIKind == FileLineInfoKind::None) {
Alexey Samsonovd0109992014-04-18 21:36:39 +0000940 DILineInfo Result;
941 Result.FunctionName = FunctionName;
David Blaikieefc4eba2017-02-06 20:19:02 +0000942 Result.StartLine = StartLine;
Alexey Samsonovd0109992014-04-18 21:36:39 +0000943 Lines.push_back(std::make_pair(Address, Result));
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000944 return Lines;
945 }
946
Frederic Rissec8a5ba2014-09-04 06:14:40 +0000947 const DWARFLineTable *LineTable = getLineTableForUnit(CU);
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000948
949 // Get the index of row we're looking for in the line table.
950 std::vector<uint32_t> RowVector;
951 if (!LineTable->lookupAddressRange(Address, Size, RowVector))
952 return Lines;
953
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000954 for (uint32_t RowIndex : RowVector) {
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000955 // Take file number and line/column from the row.
956 const DWARFDebugLine::Row &Row = LineTable->Rows[RowIndex];
Alexey Samsonovd0109992014-04-18 21:36:39 +0000957 DILineInfo Result;
Frederic Riss101b5e22014-09-19 15:11:51 +0000958 LineTable->getFileNameByIndex(Row.File, CU->getCompilationDir(),
959 Spec.FLIKind, Result.FileName);
Alexey Samsonovd0109992014-04-18 21:36:39 +0000960 Result.FunctionName = FunctionName;
961 Result.Line = Row.Line;
962 Result.Column = Row.Column;
David Blaikieefc4eba2017-02-06 20:19:02 +0000963 Result.StartLine = StartLine;
Alexey Samsonovd0109992014-04-18 21:36:39 +0000964 Lines.push_back(std::make_pair(Row.Address, Result));
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000965 }
966
967 return Lines;
968}
969
Alexey Samsonovdce67342014-05-15 21:24:32 +0000970DIInliningInfo
971DWARFContext::getInliningInfoForAddress(uint64_t Address,
972 DILineInfoSpecifier Spec) {
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000973 DIInliningInfo InliningInfo;
974
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000975 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
976 if (!CU)
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000977 return InliningInfo;
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000978
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000979 const DWARFLineTable *LineTable = nullptr;
Greg Claytonc8c10322016-12-13 18:25:19 +0000980 SmallVector<DWARFDie, 4> InlinedChain;
981 CU->getInlinedChainForAddress(Address, InlinedChain);
982 if (InlinedChain.size() == 0) {
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000983 // If there is no DIE for address (e.g. it is in unavailable .dwo file),
984 // try to at least get file/line info from symbol table.
Alexey Samsonovdce67342014-05-15 21:24:32 +0000985 if (Spec.FLIKind != FileLineInfoKind::None) {
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000986 DILineInfo Frame;
Frederic Rissec8a5ba2014-09-04 06:14:40 +0000987 LineTable = getLineTableForUnit(CU);
Frederic Riss101b5e22014-09-19 15:11:51 +0000988 if (LineTable &&
989 LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(),
990 Spec.FLIKind, Frame))
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000991 InliningInfo.addFrame(Frame);
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000992 }
993 return InliningInfo;
994 }
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000995
Dehao Chenef700d52017-04-17 20:10:39 +0000996 uint32_t CallFile = 0, CallLine = 0, CallColumn = 0, CallDiscriminator = 0;
Greg Claytonc8c10322016-12-13 18:25:19 +0000997 for (uint32_t i = 0, n = InlinedChain.size(); i != n; i++) {
998 DWARFDie &FunctionDIE = InlinedChain[i];
Alexey Samsonovd0109992014-04-18 21:36:39 +0000999 DILineInfo Frame;
Alexey Samsonovc942e6b2012-09-04 08:12:33 +00001000 // Get function name if necessary.
Greg Claytonc8c10322016-12-13 18:25:19 +00001001 if (const char *Name = FunctionDIE.getSubroutineName(Spec.FNKind))
Alexey Samsonovdce67342014-05-15 21:24:32 +00001002 Frame.FunctionName = Name;
David Blaikieefc4eba2017-02-06 20:19:02 +00001003 if (auto DeclLineResult = FunctionDIE.getDeclLine())
1004 Frame.StartLine = DeclLineResult;
Alexey Samsonovdce67342014-05-15 21:24:32 +00001005 if (Spec.FLIKind != FileLineInfoKind::None) {
Alexey Samsonovc942e6b2012-09-04 08:12:33 +00001006 if (i == 0) {
1007 // For the topmost frame, initialize the line table of this
1008 // compile unit and fetch file/line info from it.
Frederic Rissec8a5ba2014-09-04 06:14:40 +00001009 LineTable = getLineTableForUnit(CU);
Alexey Samsonovc942e6b2012-09-04 08:12:33 +00001010 // For the topmost routine, get file/line info from line table.
Frederic Riss101b5e22014-09-19 15:11:51 +00001011 if (LineTable)
1012 LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(),
1013 Spec.FLIKind, Frame);
Alexey Samsonovc942e6b2012-09-04 08:12:33 +00001014 } else {
1015 // Otherwise, use call file, call line and call column from
1016 // previous DIE in inlined chain.
Frederic Riss101b5e22014-09-19 15:11:51 +00001017 if (LineTable)
1018 LineTable->getFileNameByIndex(CallFile, CU->getCompilationDir(),
1019 Spec.FLIKind, Frame.FileName);
Alexey Samsonovd0109992014-04-18 21:36:39 +00001020 Frame.Line = CallLine;
1021 Frame.Column = CallColumn;
Dehao Chenef700d52017-04-17 20:10:39 +00001022 Frame.Discriminator = CallDiscriminator;
Alexey Samsonovc942e6b2012-09-04 08:12:33 +00001023 }
1024 // Get call file/line/column of a current DIE.
1025 if (i + 1 < n) {
Dehao Chenef700d52017-04-17 20:10:39 +00001026 FunctionDIE.getCallerFrame(CallFile, CallLine, CallColumn,
1027 CallDiscriminator);
Alexey Samsonovc942e6b2012-09-04 08:12:33 +00001028 }
1029 }
Alexey Samsonovc942e6b2012-09-04 08:12:33 +00001030 InliningInfo.addFrame(Frame);
1031 }
1032 return InliningInfo;
1033}
1034
David Blaikief9803fb2017-05-23 00:30:42 +00001035std::shared_ptr<DWARFContext>
1036DWARFContext::getDWOContext(StringRef AbsolutePath) {
David Blaikie15d85fc2017-05-23 06:48:53 +00001037 if (auto S = DWP.lock()) {
David Blaikief9803fb2017-05-23 00:30:42 +00001038 DWARFContext *Ctxt = S->Context.get();
1039 return std::shared_ptr<DWARFContext>(std::move(S), Ctxt);
1040 }
1041
David Blaikie15d85fc2017-05-23 06:48:53 +00001042 std::weak_ptr<DWOFile> *Entry = &DWOFiles[AbsolutePath];
1043
1044 if (auto S = Entry->lock()) {
1045 DWARFContext *Ctxt = S->Context.get();
1046 return std::shared_ptr<DWARFContext>(std::move(S), Ctxt);
1047 }
1048
David Blaikie15d85fc2017-05-23 06:48:53 +00001049 Expected<OwningBinary<ObjectFile>> Obj = [&] {
1050 if (!CheckedForDWP) {
David Blaikiee5adb682017-07-30 01:34:08 +00001051 SmallString<128> DWPName;
1052 auto Obj = object::ObjectFile::createObjectFile(
1053 this->DWPName.empty()
1054 ? (DObj->getFileName() + ".dwp").toStringRef(DWPName)
1055 : StringRef(this->DWPName));
David Blaikie15d85fc2017-05-23 06:48:53 +00001056 if (Obj) {
1057 Entry = &DWP;
1058 return Obj;
1059 } else {
1060 CheckedForDWP = true;
1061 // TODO: Should this error be handled (maybe in a high verbosity mode)
1062 // before falling back to .dwo files?
1063 consumeError(Obj.takeError());
1064 }
1065 }
1066
1067 return object::ObjectFile::createObjectFile(AbsolutePath);
1068 }();
1069
David Blaikief9803fb2017-05-23 00:30:42 +00001070 if (!Obj) {
1071 // TODO: Actually report errors helpfully.
1072 consumeError(Obj.takeError());
1073 return nullptr;
1074 }
David Blaikie15d85fc2017-05-23 06:48:53 +00001075
1076 auto S = std::make_shared<DWOFile>();
David Blaikief9803fb2017-05-23 00:30:42 +00001077 S->File = std::move(Obj.get());
Rafael Espindolac398e672017-07-19 22:27:28 +00001078 S->Context = DWARFContext::create(*S->File.getBinary());
David Blaikie15d85fc2017-05-23 06:48:53 +00001079 *Entry = S;
David Blaikief9803fb2017-05-23 00:30:42 +00001080 auto *Ctxt = S->Context.get();
1081 return std::shared_ptr<DWARFContext>(std::move(S), Ctxt);
1082}
1083
George Rimar702dac62017-04-12 08:59:15 +00001084static Error createError(const Twine &Reason, llvm::Error E) {
1085 return make_error<StringError>(Reason + toString(std::move(E)),
1086 inconvertibleErrorCode());
1087}
1088
George Rimara25d3292017-05-27 18:10:23 +00001089/// SymInfo contains information about symbol: it's address
1090/// and section index which is -1LL for absolute symbols.
1091struct SymInfo {
1092 uint64_t Address;
1093 uint64_t SectionIndex;
1094};
1095
1096/// Returns the address of symbol relocation used against and a section index.
1097/// Used for futher relocations computation. Symbol's section load address is
1098static Expected<SymInfo> getSymbolInfo(const object::ObjectFile &Obj,
1099 const RelocationRef &Reloc,
1100 const LoadedObjectInfo *L,
1101 std::map<SymbolRef, SymInfo> &Cache) {
1102 SymInfo Ret = {0, (uint64_t)-1LL};
George Rimar702dac62017-04-12 08:59:15 +00001103 object::section_iterator RSec = Obj.section_end();
1104 object::symbol_iterator Sym = Reloc.getSymbol();
1105
George Rimara25d3292017-05-27 18:10:23 +00001106 std::map<SymbolRef, SymInfo>::iterator CacheIt = Cache.end();
George Rimar702dac62017-04-12 08:59:15 +00001107 // First calculate the address of the symbol or section as it appears
1108 // in the object file
1109 if (Sym != Obj.symbol_end()) {
George Rimar958b01a2017-05-15 11:45:28 +00001110 bool New;
George Rimara25d3292017-05-27 18:10:23 +00001111 std::tie(CacheIt, New) = Cache.insert({*Sym, {0, 0}});
George Rimar958b01a2017-05-15 11:45:28 +00001112 if (!New)
1113 return CacheIt->second;
1114
George Rimar702dac62017-04-12 08:59:15 +00001115 Expected<uint64_t> SymAddrOrErr = Sym->getAddress();
1116 if (!SymAddrOrErr)
George Rimar1af3cb22017-06-28 08:21:19 +00001117 return createError("failed to compute symbol address: ",
George Rimar702dac62017-04-12 08:59:15 +00001118 SymAddrOrErr.takeError());
1119
1120 // Also remember what section this symbol is in for later
1121 auto SectOrErr = Sym->getSection();
1122 if (!SectOrErr)
George Rimar1af3cb22017-06-28 08:21:19 +00001123 return createError("failed to get symbol section: ",
George Rimar702dac62017-04-12 08:59:15 +00001124 SectOrErr.takeError());
1125
1126 RSec = *SectOrErr;
George Rimara25d3292017-05-27 18:10:23 +00001127 Ret.Address = *SymAddrOrErr;
George Rimar702dac62017-04-12 08:59:15 +00001128 } else if (auto *MObj = dyn_cast<MachOObjectFile>(&Obj)) {
1129 RSec = MObj->getRelocationSection(Reloc.getRawDataRefImpl());
George Rimara25d3292017-05-27 18:10:23 +00001130 Ret.Address = RSec->getAddress();
George Rimar702dac62017-04-12 08:59:15 +00001131 }
1132
George Rimara25d3292017-05-27 18:10:23 +00001133 if (RSec != Obj.section_end())
1134 Ret.SectionIndex = RSec->getIndex();
1135
George Rimar702dac62017-04-12 08:59:15 +00001136 // If we are given load addresses for the sections, we need to adjust:
1137 // SymAddr = (Address of Symbol Or Section in File) -
1138 // (Address of Section in File) +
1139 // (Load Address of Section)
1140 // RSec is now either the section being targeted or the section
1141 // containing the symbol being targeted. In either case,
1142 // we need to perform the same computation.
1143 if (L && RSec != Obj.section_end())
1144 if (uint64_t SectionLoadAddress = L->getSectionLoadAddress(*RSec))
George Rimara25d3292017-05-27 18:10:23 +00001145 Ret.Address += SectionLoadAddress - RSec->getAddress();
George Rimar958b01a2017-05-15 11:45:28 +00001146
1147 if (CacheIt != Cache.end())
1148 CacheIt->second = Ret;
1149
George Rimar702dac62017-04-12 08:59:15 +00001150 return Ret;
1151}
1152
1153static bool isRelocScattered(const object::ObjectFile &Obj,
1154 const RelocationRef &Reloc) {
George Rimard4998b02017-04-13 09:52:50 +00001155 const MachOObjectFile *MachObj = dyn_cast<MachOObjectFile>(&Obj);
1156 if (!MachObj)
George Rimar702dac62017-04-12 08:59:15 +00001157 return false;
1158 // MachO also has relocations that point to sections and
1159 // scattered relocations.
George Rimar702dac62017-04-12 08:59:15 +00001160 auto RelocInfo = MachObj->getRelocation(Reloc.getRawDataRefImpl());
1161 return MachObj->isRelocationScattered(RelocInfo);
1162}
1163
Rafael Espindolac398e672017-07-19 22:27:28 +00001164ErrorPolicy DWARFContext::defaultErrorHandler(Error E) {
Jonas Devlieghere84e99262018-04-14 22:07:23 +00001165 WithColor::error() << toString(std::move(E)) << '\n';
George Rimar1af3cb22017-06-28 08:21:19 +00001166 return ErrorPolicy::Continue;
1167}
1168
Rafael Espindola87c3f4a92017-07-24 19:34:26 +00001169namespace {
1170struct DWARFSectionMap final : public DWARFSection {
1171 RelocAddrMap Relocs;
1172};
Rafael Espindola87c3f4a92017-07-24 19:34:26 +00001173
Rafael Espindolac398e672017-07-19 22:27:28 +00001174class DWARFObjInMemory final : public DWARFObject {
1175 bool IsLittleEndian;
1176 uint8_t AddressSize;
1177 StringRef FileName;
George Rimar6957ab52017-08-15 12:32:54 +00001178 const object::ObjectFile *Obj = nullptr;
George Rimare1c30f72017-08-15 15:54:43 +00001179 std::vector<SectionName> SectionNames;
Keno Fischerc780e8e2015-05-21 21:24:32 +00001180
Rafael Espindolac398e672017-07-19 22:27:28 +00001181 using TypeSectionMap = MapVector<object::SectionRef, DWARFSectionMap,
1182 std::map<object::SectionRef, unsigned>>;
Eric Christopher7370b552012-11-12 21:40:38 +00001183
Rafael Espindolac398e672017-07-19 22:27:28 +00001184 TypeSectionMap TypesSections;
1185 TypeSectionMap TypesDWOSections;
1186
1187 DWARFSectionMap InfoSection;
1188 DWARFSectionMap LocSection;
1189 DWARFSectionMap LineSection;
1190 DWARFSectionMap RangeSection;
James Henderson3fcc7452018-02-02 12:35:52 +00001191 DWARFSectionMap RnglistsSection;
Rafael Espindolac398e672017-07-19 22:27:28 +00001192 DWARFSectionMap StringOffsetSection;
1193 DWARFSectionMap InfoDWOSection;
1194 DWARFSectionMap LineDWOSection;
1195 DWARFSectionMap LocDWOSection;
1196 DWARFSectionMap StringOffsetDWOSection;
1197 DWARFSectionMap RangeDWOSection;
Wolfgang Piebad605592018-05-18 20:12:54 +00001198 DWARFSectionMap RnglistsDWOSection;
Rafael Espindolac398e672017-07-19 22:27:28 +00001199 DWARFSectionMap AddrSection;
1200 DWARFSectionMap AppleNamesSection;
1201 DWARFSectionMap AppleTypesSection;
1202 DWARFSectionMap AppleNamespacesSection;
1203 DWARFSectionMap AppleObjCSection;
Pavel Labath3c9a9182018-01-29 11:08:32 +00001204 DWARFSectionMap DebugNamesSection;
Rafael Espindolac398e672017-07-19 22:27:28 +00001205
1206 DWARFSectionMap *mapNameToDWARFSection(StringRef Name) {
1207 return StringSwitch<DWARFSectionMap *>(Name)
1208 .Case("debug_info", &InfoSection)
1209 .Case("debug_loc", &LocSection)
1210 .Case("debug_line", &LineSection)
1211 .Case("debug_str_offsets", &StringOffsetSection)
1212 .Case("debug_ranges", &RangeSection)
James Henderson3fcc7452018-02-02 12:35:52 +00001213 .Case("debug_rnglists", &RnglistsSection)
Rafael Espindolac398e672017-07-19 22:27:28 +00001214 .Case("debug_info.dwo", &InfoDWOSection)
1215 .Case("debug_loc.dwo", &LocDWOSection)
1216 .Case("debug_line.dwo", &LineDWOSection)
Pavel Labath3c9a9182018-01-29 11:08:32 +00001217 .Case("debug_names", &DebugNamesSection)
Wolfgang Piebad605592018-05-18 20:12:54 +00001218 .Case("debug_rnglists.dwo", &RnglistsDWOSection)
Rafael Espindolac398e672017-07-19 22:27:28 +00001219 .Case("debug_str_offsets.dwo", &StringOffsetDWOSection)
1220 .Case("debug_addr", &AddrSection)
1221 .Case("apple_names", &AppleNamesSection)
1222 .Case("apple_types", &AppleTypesSection)
1223 .Case("apple_namespaces", &AppleNamespacesSection)
1224 .Case("apple_namespac", &AppleNamespacesSection)
1225 .Case("apple_objc", &AppleObjCSection)
1226 .Default(nullptr);
1227 }
1228
1229 StringRef AbbrevSection;
1230 StringRef ARangeSection;
1231 StringRef DebugFrameSection;
1232 StringRef EHFrameSection;
1233 StringRef StringSection;
1234 StringRef MacinfoSection;
1235 StringRef PubNamesSection;
1236 StringRef PubTypesSection;
1237 StringRef GnuPubNamesSection;
1238 StringRef AbbrevDWOSection;
1239 StringRef StringDWOSection;
1240 StringRef GnuPubTypesSection;
1241 StringRef CUIndexSection;
1242 StringRef GdbIndexSection;
1243 StringRef TUIndexSection;
Paul Robinsonb6aa01c2018-01-25 22:02:36 +00001244 StringRef LineStringSection;
Rafael Espindolac398e672017-07-19 22:27:28 +00001245
1246 SmallVector<SmallString<32>, 4> UncompressedSections;
1247
1248 StringRef *mapSectionToMember(StringRef Name) {
1249 if (DWARFSection *Sec = mapNameToDWARFSection(Name))
1250 return &Sec->Data;
1251 return StringSwitch<StringRef *>(Name)
1252 .Case("debug_abbrev", &AbbrevSection)
1253 .Case("debug_aranges", &ARangeSection)
1254 .Case("debug_frame", &DebugFrameSection)
1255 .Case("eh_frame", &EHFrameSection)
1256 .Case("debug_str", &StringSection)
1257 .Case("debug_macinfo", &MacinfoSection)
1258 .Case("debug_pubnames", &PubNamesSection)
1259 .Case("debug_pubtypes", &PubTypesSection)
1260 .Case("debug_gnu_pubnames", &GnuPubNamesSection)
1261 .Case("debug_gnu_pubtypes", &GnuPubTypesSection)
1262 .Case("debug_abbrev.dwo", &AbbrevDWOSection)
1263 .Case("debug_str.dwo", &StringDWOSection)
1264 .Case("debug_cu_index", &CUIndexSection)
1265 .Case("debug_tu_index", &TUIndexSection)
1266 .Case("gdb_index", &GdbIndexSection)
Paul Robinsonb6aa01c2018-01-25 22:02:36 +00001267 .Case("debug_line_str", &LineStringSection)
Rafael Espindolac398e672017-07-19 22:27:28 +00001268 // Any more debug info sections go here.
1269 .Default(nullptr);
1270 }
1271
1272 /// If Sec is compressed section, decompresses and updates its contents
1273 /// provided by Data. Otherwise leaves it unchanged.
1274 Error maybeDecompress(const object::SectionRef &Sec, StringRef Name,
1275 StringRef &Data) {
1276 if (!Decompressor::isCompressed(Sec))
1277 return Error::success();
1278
1279 Expected<Decompressor> Decompressor =
1280 Decompressor::create(Name, Data, IsLittleEndian, AddressSize == 8);
1281 if (!Decompressor)
1282 return Decompressor.takeError();
1283
1284 SmallString<32> Out;
1285 if (auto Err = Decompressor->resizeAndDecompress(Out))
1286 return Err;
1287
1288 UncompressedSections.emplace_back(std::move(Out));
1289 Data = UncompressedSections.back();
1290
1291 return Error::success();
1292 }
1293
1294public:
1295 DWARFObjInMemory(const StringMap<std::unique_ptr<MemoryBuffer>> &Sections,
1296 uint8_t AddrSize, bool IsLittleEndian)
1297 : IsLittleEndian(IsLittleEndian) {
1298 for (const auto &SecIt : Sections) {
1299 if (StringRef *SectionData = mapSectionToMember(SecIt.first()))
1300 *SectionData = SecIt.second->getBuffer();
Alexey Samsonov068fc8a2013-04-23 10:17:34 +00001301 }
Rafael Espindolac398e672017-07-19 22:27:28 +00001302 }
1303 DWARFObjInMemory(const object::ObjectFile &Obj, const LoadedObjectInfo *L,
1304 function_ref<ErrorPolicy(Error)> HandleError)
1305 : IsLittleEndian(Obj.isLittleEndian()),
George Rimar6957ab52017-08-15 12:32:54 +00001306 AddressSize(Obj.getBytesInAddress()), FileName(Obj.getFileName()),
1307 Obj(&Obj) {
Alexey Samsonov068fc8a2013-04-23 10:17:34 +00001308
George Rimare1c30f72017-08-15 15:54:43 +00001309 StringMap<unsigned> SectionAmountMap;
Rafael Espindolac398e672017-07-19 22:27:28 +00001310 for (const SectionRef &Section : Obj.sections()) {
1311 StringRef Name;
1312 Section.getName(Name);
George Rimare1c30f72017-08-15 15:54:43 +00001313 ++SectionAmountMap[Name];
1314 SectionNames.push_back({ Name, true });
1315
Rafael Espindolac398e672017-07-19 22:27:28 +00001316 // Skip BSS and Virtual sections, they aren't interesting.
1317 if (Section.isBSS() || Section.isVirtual())
George Rimarfed9f092017-05-17 12:10:51 +00001318 continue;
Keno Fischerc780e8e2015-05-21 21:24:32 +00001319
Jonas Devlieghere8af23872017-09-26 14:22:35 +00001320 // Skip sections stripped by dsymutil.
1321 if (Section.isStripped())
1322 continue;
1323
Rafael Espindolac398e672017-07-19 22:27:28 +00001324 StringRef Data;
1325 section_iterator RelocatedSection = Section.getRelocatedSection();
1326 // Try to obtain an already relocated version of this section.
1327 // Else use the unrelocated section from the object file. We'll have to
1328 // apply relocations ourselves later.
1329 if (!L || !L->getLoadedSectionContents(*RelocatedSection, Data))
1330 Section.getContents(Data);
George Rimarfed9f092017-05-17 12:10:51 +00001331
Rafael Espindolac398e672017-07-19 22:27:28 +00001332 if (auto Err = maybeDecompress(Section, Name, Data)) {
1333 ErrorPolicy EP = HandleError(createError(
1334 "failed to decompress '" + Name + "', ", std::move(Err)));
George Rimar1af3cb22017-06-28 08:21:19 +00001335 if (EP == ErrorPolicy::Halt)
1336 return;
George Rimarfed9f092017-05-17 12:10:51 +00001337 continue;
1338 }
Rafael Espindolac398e672017-07-19 22:27:28 +00001339
1340 // Compressed sections names in GNU style starts from ".z",
1341 // at this point section is decompressed and we drop compression prefix.
1342 Name = Name.substr(
1343 Name.find_first_not_of("._z")); // Skip ".", "z" and "_" prefixes.
1344
1345 // Map platform specific debug section names to DWARF standard section
1346 // names.
1347 Name = Obj.mapDebugSectionName(Name);
1348
1349 if (StringRef *SectionData = mapSectionToMember(Name)) {
1350 *SectionData = Data;
1351 if (Name == "debug_ranges") {
1352 // FIXME: Use the other dwo range section when we emit it.
1353 RangeDWOSection.Data = Data;
1354 }
1355 } else if (Name == "debug_types") {
1356 // Find debug_types data by section rather than name as there are
1357 // multiple, comdat grouped, debug_types sections.
1358 TypesSections[Section].Data = Data;
1359 } else if (Name == "debug_types.dwo") {
1360 TypesDWOSections[Section].Data = Data;
1361 }
1362
1363 if (RelocatedSection == Obj.section_end())
1364 continue;
1365
1366 StringRef RelSecName;
1367 StringRef RelSecData;
1368 RelocatedSection->getName(RelSecName);
1369
1370 // If the section we're relocating was relocated already by the JIT,
1371 // then we used the relocated version above, so we do not need to process
1372 // relocations for it now.
1373 if (L && L->getLoadedSectionContents(*RelocatedSection, RelSecData))
1374 continue;
1375
1376 // In Mach-o files, the relocations do not need to be applied if
1377 // there is no load offset to apply. The value read at the
1378 // relocation point already factors in the section address
1379 // (actually applying the relocations will produce wrong results
1380 // as the section address will be added twice).
1381 if (!L && isa<MachOObjectFile>(&Obj))
1382 continue;
1383
1384 RelSecName = RelSecName.substr(
1385 RelSecName.find_first_not_of("._z")); // Skip . and _ prefixes.
1386
1387 // TODO: Add support for relocations in other sections as needed.
1388 // Record relocations for the debug_info and debug_line sections.
1389 DWARFSectionMap *Sec = mapNameToDWARFSection(RelSecName);
1390 RelocAddrMap *Map = Sec ? &Sec->Relocs : nullptr;
1391 if (!Map) {
1392 // Find debug_types relocs by section rather than name as there are
1393 // multiple, comdat grouped, debug_types sections.
1394 if (RelSecName == "debug_types")
1395 Map =
1396 &static_cast<DWARFSectionMap &>(TypesSections[*RelocatedSection])
1397 .Relocs;
1398 else if (RelSecName == "debug_types.dwo")
1399 Map = &static_cast<DWARFSectionMap &>(
1400 TypesDWOSections[*RelocatedSection])
1401 .Relocs;
1402 else
1403 continue;
1404 }
1405
1406 if (Section.relocation_begin() == Section.relocation_end())
1407 continue;
1408
1409 // Symbol to [address, section index] cache mapping.
1410 std::map<SymbolRef, SymInfo> AddrCache;
1411 for (const RelocationRef &Reloc : Section.relocations()) {
1412 // FIXME: it's not clear how to correctly handle scattered
1413 // relocations.
1414 if (isRelocScattered(Obj, Reloc))
1415 continue;
1416
1417 Expected<SymInfo> SymInfoOrErr =
1418 getSymbolInfo(Obj, Reloc, L, AddrCache);
1419 if (!SymInfoOrErr) {
1420 if (HandleError(SymInfoOrErr.takeError()) == ErrorPolicy::Halt)
1421 return;
1422 continue;
1423 }
1424
1425 object::RelocVisitor V(Obj);
1426 uint64_t Val = V.visit(Reloc.getType(), Reloc, SymInfoOrErr->Address);
1427 if (V.error()) {
1428 SmallString<32> Type;
1429 Reloc.getTypeName(Type);
1430 ErrorPolicy EP = HandleError(
1431 createError("failed to compute relocation: " + Type + ", ",
1432 errorCodeToError(object_error::parse_failed)));
1433 if (EP == ErrorPolicy::Halt)
1434 return;
1435 continue;
1436 }
1437 RelocAddrEntry Rel = {SymInfoOrErr->SectionIndex, Val};
1438 Map->insert({Reloc.getOffset(), Rel});
1439 }
Eric Christopher7370b552012-11-12 21:40:38 +00001440 }
George Rimare1c30f72017-08-15 15:54:43 +00001441
1442 for (SectionName &S : SectionNames)
1443 if (SectionAmountMap[S.Name] > 1)
1444 S.IsNameUnique = false;
Eric Christopher7370b552012-11-12 21:40:38 +00001445 }
Eric Christopher7370b552012-11-12 21:40:38 +00001446
Rafael Espindolac398e672017-07-19 22:27:28 +00001447 Optional<RelocAddrEntry> find(const DWARFSection &S,
1448 uint64_t Pos) const override {
1449 auto &Sec = static_cast<const DWARFSectionMap &>(S);
1450 RelocAddrMap::const_iterator AI = Sec.Relocs.find(Pos);
1451 if (AI == Sec.Relocs.end())
1452 return None;
1453 return AI->second;
Chris Bieneman2e752db2017-01-20 19:03:14 +00001454 }
Rafael Espindolac398e672017-07-19 22:27:28 +00001455
George Rimar6957ab52017-08-15 12:32:54 +00001456 const object::ObjectFile *getFile() const override { return Obj; }
1457
George Rimare1c30f72017-08-15 15:54:43 +00001458 ArrayRef<SectionName> getSectionNames() const override {
1459 return SectionNames;
1460 }
1461
Rafael Espindolac398e672017-07-19 22:27:28 +00001462 bool isLittleEndian() const override { return IsLittleEndian; }
1463 StringRef getAbbrevDWOSection() const override { return AbbrevDWOSection; }
1464 const DWARFSection &getLineDWOSection() const override {
1465 return LineDWOSection;
1466 }
1467 const DWARFSection &getLocDWOSection() const override {
1468 return LocDWOSection;
1469 }
1470 StringRef getStringDWOSection() const override { return StringDWOSection; }
1471 const DWARFSection &getStringOffsetDWOSection() const override {
1472 return StringOffsetDWOSection;
1473 }
1474 const DWARFSection &getRangeDWOSection() const override {
1475 return RangeDWOSection;
1476 }
Wolfgang Piebad605592018-05-18 20:12:54 +00001477 const DWARFSection &getRnglistsDWOSection() const override {
1478 return RnglistsDWOSection;
1479 }
Rafael Espindolac398e672017-07-19 22:27:28 +00001480 const DWARFSection &getAddrSection() const override { return AddrSection; }
1481 StringRef getCUIndexSection() const override { return CUIndexSection; }
1482 StringRef getGdbIndexSection() const override { return GdbIndexSection; }
1483 StringRef getTUIndexSection() const override { return TUIndexSection; }
1484
1485 // DWARF v5
1486 const DWARFSection &getStringOffsetSection() const override {
1487 return StringOffsetSection;
1488 }
Paul Robinsonb6aa01c2018-01-25 22:02:36 +00001489 StringRef getLineStringSection() const override { return LineStringSection; }
Rafael Espindolac398e672017-07-19 22:27:28 +00001490
1491 // Sections for DWARF5 split dwarf proposal.
1492 const DWARFSection &getInfoDWOSection() const override {
1493 return InfoDWOSection;
1494 }
1495 void forEachTypesDWOSections(
1496 function_ref<void(const DWARFSection &)> F) const override {
1497 for (auto &P : TypesDWOSections)
1498 F(P.second);
1499 }
1500
1501 StringRef getAbbrevSection() const override { return AbbrevSection; }
1502 const DWARFSection &getLocSection() const override { return LocSection; }
1503 StringRef getARangeSection() const override { return ARangeSection; }
1504 StringRef getDebugFrameSection() const override { return DebugFrameSection; }
1505 StringRef getEHFrameSection() const override { return EHFrameSection; }
1506 const DWARFSection &getLineSection() const override { return LineSection; }
1507 StringRef getStringSection() const override { return StringSection; }
1508 const DWARFSection &getRangeSection() const override { return RangeSection; }
James Henderson3fcc7452018-02-02 12:35:52 +00001509 const DWARFSection &getRnglistsSection() const override {
1510 return RnglistsSection;
1511 }
Rafael Espindolac398e672017-07-19 22:27:28 +00001512 StringRef getMacinfoSection() const override { return MacinfoSection; }
1513 StringRef getPubNamesSection() const override { return PubNamesSection; }
1514 StringRef getPubTypesSection() const override { return PubTypesSection; }
1515 StringRef getGnuPubNamesSection() const override {
1516 return GnuPubNamesSection;
1517 }
1518 StringRef getGnuPubTypesSection() const override {
1519 return GnuPubTypesSection;
1520 }
1521 const DWARFSection &getAppleNamesSection() const override {
1522 return AppleNamesSection;
1523 }
1524 const DWARFSection &getAppleTypesSection() const override {
1525 return AppleTypesSection;
1526 }
1527 const DWARFSection &getAppleNamespacesSection() const override {
1528 return AppleNamespacesSection;
1529 }
1530 const DWARFSection &getAppleObjCSection() const override {
1531 return AppleObjCSection;
1532 }
Pavel Labath3c9a9182018-01-29 11:08:32 +00001533 const DWARFSection &getDebugNamesSection() const override {
1534 return DebugNamesSection;
1535 }
Rafael Espindolac398e672017-07-19 22:27:28 +00001536
1537 StringRef getFileName() const override { return FileName; }
1538 uint8_t getAddressSize() const override { return AddressSize; }
1539 const DWARFSection &getInfoSection() const override { return InfoSection; }
1540 void forEachTypesSections(
1541 function_ref<void(const DWARFSection &)> F) const override {
1542 for (auto &P : TypesSections)
1543 F(P.second);
1544 }
1545};
Benjamin Kramer49a49fe2017-08-20 13:03:48 +00001546} // namespace
Rafael Espindolac398e672017-07-19 22:27:28 +00001547
1548std::unique_ptr<DWARFContext>
1549DWARFContext::create(const object::ObjectFile &Obj, const LoadedObjectInfo *L,
David Blaikiee5adb682017-07-30 01:34:08 +00001550 function_ref<ErrorPolicy(Error)> HandleError,
1551 std::string DWPName) {
Reid Kleckner388f8802017-07-19 23:42:53 +00001552 auto DObj = llvm::make_unique<DWARFObjInMemory>(Obj, L, HandleError);
David Blaikiee5adb682017-07-30 01:34:08 +00001553 return llvm::make_unique<DWARFContext>(std::move(DObj), std::move(DWPName));
Chris Bieneman2e752db2017-01-20 19:03:14 +00001554}
1555
Rafael Espindolac398e672017-07-19 22:27:28 +00001556std::unique_ptr<DWARFContext>
1557DWARFContext::create(const StringMap<std::unique_ptr<MemoryBuffer>> &Sections,
1558 uint8_t AddrSize, bool isLittleEndian) {
Reid Kleckner388f8802017-07-19 23:42:53 +00001559 auto DObj =
1560 llvm::make_unique<DWARFObjInMemory>(Sections, AddrSize, isLittleEndian);
David Blaikiee5adb682017-07-30 01:34:08 +00001561 return llvm::make_unique<DWARFContext>(std::move(DObj), "");
Rafael Espindola77e87b32017-07-07 05:36:53 +00001562}
Reid Klecknera0587362017-08-29 21:41:21 +00001563
1564Error DWARFContext::loadRegisterInfo(const object::ObjectFile &Obj) {
1565 // Detect the architecture from the object file. We usually don't need OS
1566 // info to lookup a target and create register info.
1567 Triple TT;
1568 TT.setArch(Triple::ArchType(Obj.getArch()));
1569 TT.setVendor(Triple::UnknownVendor);
1570 TT.setOS(Triple::UnknownOS);
1571 std::string TargetLookupError;
1572 const Target *TheTarget =
1573 TargetRegistry::lookupTarget(TT.str(), TargetLookupError);
1574 if (!TargetLookupError.empty())
1575 return make_error<StringError>(TargetLookupError, inconvertibleErrorCode());
1576 RegInfo.reset(TheTarget->createMCRegInfo(TT.str()));
1577 return Error::success();
1578}