blob: 982dd7cf479a6afbda1a28542e6f53827b80b1b4 [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.
David Blaikie15d85fc2017-05-23 06:48:53 +0000564 for (const auto &DWOCU : dwo_compile_units())
565 if (DWOCU->getDWOId() == Hash)
566 return DWOCU.get();
567 return nullptr;
568}
569
Greg Claytonc7695a82017-05-02 20:28:33 +0000570DWARFDie DWARFContext::getDIEForOffset(uint32_t Offset) {
571 parseCompileUnits();
572 if (auto *CU = CUs.getUnitForOffset(Offset))
573 return CU->getDIEForOffset(Offset);
574 return DWARFDie();
575}
576
Jonas Devliegherec0a758d2017-09-18 14:15:57 +0000577bool DWARFContext::verify(raw_ostream &OS, DIDumpOptions DumpOpts) {
Greg Claytonc7695a82017-05-02 20:28:33 +0000578 bool Success = true;
Jonas Devlieghere27476ce2017-09-13 09:43:05 +0000579 DWARFVerifier verifier(OS, *this, DumpOpts);
Spyridoula Gravani73e17962017-07-27 00:59:33 +0000580
Spyridoula Gravani364b5352017-07-20 02:06:52 +0000581 Success &= verifier.handleDebugAbbrev();
Jonas Devliegherec0a758d2017-09-18 14:15:57 +0000582 if (DumpOpts.DumpType & DIDT_DebugInfo)
Spyridoula Gravani73e17962017-07-27 00:59:33 +0000583 Success &= verifier.handleDebugInfo();
Jonas Devliegherec0a758d2017-09-18 14:15:57 +0000584 if (DumpOpts.DumpType & DIDT_DebugLine)
Spyridoula Gravani73e17962017-07-27 00:59:33 +0000585 Success &= verifier.handleDebugLine();
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000586 Success &= verifier.handleAccelTables();
Greg Clayton48432cf2017-05-01 22:07:02 +0000587 return Success;
588}
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000589
David Blaikie82641be2015-11-17 00:39:55 +0000590const DWARFUnitIndex &DWARFContext::getCUIndex() {
591 if (CUIndex)
592 return *CUIndex;
593
Rafael Espindolac398e672017-07-19 22:27:28 +0000594 DataExtractor CUIndexData(DObj->getCUIndexSection(), isLittleEndian(), 0);
David Blaikie82641be2015-11-17 00:39:55 +0000595
David Blaikieb073cb92015-12-02 06:21:34 +0000596 CUIndex = llvm::make_unique<DWARFUnitIndex>(DW_SECT_INFO);
David Blaikie82641be2015-11-17 00:39:55 +0000597 CUIndex->parse(CUIndexData);
598 return *CUIndex;
599}
600
601const DWARFUnitIndex &DWARFContext::getTUIndex() {
602 if (TUIndex)
603 return *TUIndex;
604
Rafael Espindolac398e672017-07-19 22:27:28 +0000605 DataExtractor TUIndexData(DObj->getTUIndexSection(), isLittleEndian(), 0);
David Blaikie82641be2015-11-17 00:39:55 +0000606
David Blaikieb073cb92015-12-02 06:21:34 +0000607 TUIndex = llvm::make_unique<DWARFUnitIndex>(DW_SECT_TYPES);
David Blaikie82641be2015-11-17 00:39:55 +0000608 TUIndex->parse(TUIndexData);
609 return *TUIndex;
610}
611
George Rimar4f82df52016-09-23 11:01:53 +0000612DWARFGdbIndex &DWARFContext::getGdbIndex() {
613 if (GdbIndex)
614 return *GdbIndex;
615
Rafael Espindolac398e672017-07-19 22:27:28 +0000616 DataExtractor GdbIndexData(DObj->getGdbIndexSection(), true /*LE*/, 0);
George Rimar4f82df52016-09-23 11:01:53 +0000617 GdbIndex = llvm::make_unique<DWARFGdbIndex>();
618 GdbIndex->parse(GdbIndexData);
619 return *GdbIndex;
620}
621
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000622const DWARFDebugAbbrev *DWARFContext::getDebugAbbrev() {
623 if (Abbrev)
624 return Abbrev.get();
625
Rafael Espindolac398e672017-07-19 22:27:28 +0000626 DataExtractor abbrData(DObj->getAbbrevSection(), isLittleEndian(), 0);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000627
628 Abbrev.reset(new DWARFDebugAbbrev());
Alexey Samsonov4316df52014-04-25 21:10:56 +0000629 Abbrev->extract(abbrData);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000630 return Abbrev.get();
631}
632
Eric Christopherda4b2192013-01-02 23:52:13 +0000633const DWARFDebugAbbrev *DWARFContext::getDebugAbbrevDWO() {
634 if (AbbrevDWO)
635 return AbbrevDWO.get();
636
Rafael Espindolac398e672017-07-19 22:27:28 +0000637 DataExtractor abbrData(DObj->getAbbrevDWOSection(), isLittleEndian(), 0);
Eric Christopherda4b2192013-01-02 23:52:13 +0000638 AbbrevDWO.reset(new DWARFDebugAbbrev());
Alexey Samsonov4316df52014-04-25 21:10:56 +0000639 AbbrevDWO->extract(abbrData);
Eric Christopherda4b2192013-01-02 23:52:13 +0000640 return AbbrevDWO.get();
641}
642
David Blaikie18e73502013-06-19 21:37:13 +0000643const DWARFDebugLoc *DWARFContext::getDebugLoc() {
644 if (Loc)
645 return Loc.get();
646
Paul Robinson17536b92017-06-29 16:52:08 +0000647 Loc.reset(new DWARFDebugLoc);
Pavel Labath54ca2d62018-04-06 08:49:57 +0000648 // Assume all compile units have the same address byte size.
Paul Robinson17536b92017-06-29 16:52:08 +0000649 if (getNumCompileUnits()) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000650 DWARFDataExtractor LocData(*DObj, DObj->getLocSection(), isLittleEndian(),
Paul Robinson17536b92017-06-29 16:52:08 +0000651 getCompileUnitAtIndex(0)->getAddressByteSize());
652 Loc->parse(LocData);
653 }
David Blaikie18e73502013-06-19 21:37:13 +0000654 return Loc.get();
655}
656
David Blaikie9c550ac2014-03-25 01:44:02 +0000657const DWARFDebugLocDWO *DWARFContext::getDebugLocDWO() {
658 if (LocDWO)
659 return LocDWO.get();
660
David Blaikie9c550ac2014-03-25 01:44:02 +0000661 LocDWO.reset(new DWARFDebugLocDWO());
Pavel Labath54ca2d62018-04-06 08:49:57 +0000662 // Assume all compile units have the same address byte size.
663 if (getNumCompileUnits()) {
664 DataExtractor LocData(DObj->getLocDWOSection().Data, isLittleEndian(),
665 getCompileUnitAtIndex(0)->getAddressByteSize());
666 LocDWO->parse(LocData);
667 }
David Blaikie9c550ac2014-03-25 01:44:02 +0000668 return LocDWO.get();
669}
670
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000671const DWARFDebugAranges *DWARFContext::getDebugAranges() {
672 if (Aranges)
673 return Aranges.get();
674
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000675 Aranges.reset(new DWARFDebugAranges());
Alexey Samsonova1694c12012-11-16 08:36:25 +0000676 Aranges->generate(this);
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000677 return Aranges.get();
678}
679
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000680const DWARFDebugFrame *DWARFContext::getDebugFrame() {
681 if (DebugFrame)
682 return DebugFrame.get();
683
684 // There's a "bug" in the DWARFv3 standard with respect to the target address
685 // size within debug frame sections. While DWARF is supposed to be independent
686 // of its container, FDEs have fields with size being "target address size",
687 // which isn't specified in DWARF in general. It's only specified for CUs, but
688 // .eh_frame can appear without a .debug_info section. Follow the example of
689 // other tools (libdwarf) and extract this from the container (ObjectFile
690 // provides this information). This problem is fixed in DWARFv4
691 // See this dwarf-discuss discussion for more details:
692 // http://lists.dwarfstd.org/htdig.cgi/dwarf-discuss-dwarfstd.org/2011-December/001173.html
Rafael Auler86fb7bf2018-03-08 00:46:53 +0000693 DWARFDataExtractor debugFrameData(DObj->getDebugFrameSection(),
694 isLittleEndian(), DObj->getAddressSize());
Igor Laevsky03a670c2016-01-26 15:09:42 +0000695 DebugFrame.reset(new DWARFDebugFrame(false /* IsEH */));
696 DebugFrame->parse(debugFrameData);
697 return DebugFrame.get();
698}
699
700const DWARFDebugFrame *DWARFContext::getEHFrame() {
701 if (EHFrame)
702 return EHFrame.get();
703
Rafael Auler86fb7bf2018-03-08 00:46:53 +0000704 DWARFDataExtractor debugFrameData(DObj->getEHFrameSection(), isLittleEndian(),
705 DObj->getAddressSize());
Igor Laevsky03a670c2016-01-26 15:09:42 +0000706 DebugFrame.reset(new DWARFDebugFrame(true /* IsEH */));
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000707 DebugFrame->parse(debugFrameData);
708 return DebugFrame.get();
709}
710
Amjad Aboude59cc3e2015-11-12 09:38:54 +0000711const DWARFDebugMacro *DWARFContext::getDebugMacro() {
712 if (Macro)
713 return Macro.get();
714
Rafael Espindolac398e672017-07-19 22:27:28 +0000715 DataExtractor MacinfoData(DObj->getMacinfoSection(), isLittleEndian(), 0);
Amjad Aboude59cc3e2015-11-12 09:38:54 +0000716 Macro.reset(new DWARFDebugMacro());
717 Macro->parse(MacinfoData);
718 return Macro.get();
719}
720
Pavel Labath3c9a9182018-01-29 11:08:32 +0000721template <typename T>
722static T &getAccelTable(std::unique_ptr<T> &Cache, const DWARFObject &Obj,
723 const DWARFSection &Section, StringRef StringSection,
724 bool IsLittleEndian) {
Adrian Prantl99fdb9d2017-09-28 18:10:52 +0000725 if (Cache)
726 return *Cache;
727 DWARFDataExtractor AccelSection(Obj, Section, IsLittleEndian, 0);
728 DataExtractor StrData(StringSection, IsLittleEndian, 0);
Pavel Labath3c9a9182018-01-29 11:08:32 +0000729 Cache.reset(new T(AccelSection, StrData));
Jonas Devlieghereba915892017-12-11 18:22:47 +0000730 if (Error E = Cache->extract())
731 llvm::consumeError(std::move(E));
Adrian Prantl99fdb9d2017-09-28 18:10:52 +0000732 return *Cache;
733}
734
Pavel Labath3c9a9182018-01-29 11:08:32 +0000735const DWARFDebugNames &DWARFContext::getDebugNames() {
736 return getAccelTable(Names, *DObj, DObj->getDebugNamesSection(),
737 DObj->getStringSection(), isLittleEndian());
738}
739
Pavel Labath9b36fd22018-01-22 13:17:23 +0000740const AppleAcceleratorTable &DWARFContext::getAppleNames() {
Adrian Prantl99fdb9d2017-09-28 18:10:52 +0000741 return getAccelTable(AppleNames, *DObj, DObj->getAppleNamesSection(),
742 DObj->getStringSection(), isLittleEndian());
743}
744
Pavel Labath9b36fd22018-01-22 13:17:23 +0000745const AppleAcceleratorTable &DWARFContext::getAppleTypes() {
Adrian Prantl714ee4d2017-09-29 00:33:22 +0000746 return getAccelTable(AppleTypes, *DObj, DObj->getAppleTypesSection(),
747 DObj->getStringSection(), isLittleEndian());
748}
749
Pavel Labath9b36fd22018-01-22 13:17:23 +0000750const AppleAcceleratorTable &DWARFContext::getAppleNamespaces() {
Adrian Prantlf51e7802017-09-29 00:52:33 +0000751 return getAccelTable(AppleNamespaces, *DObj,
752 DObj->getAppleNamespacesSection(),
753 DObj->getStringSection(), isLittleEndian());
754}
755
Pavel Labath9b36fd22018-01-22 13:17:23 +0000756const AppleAcceleratorTable &DWARFContext::getAppleObjC() {
Adrian Prantlf51e7802017-09-29 00:52:33 +0000757 return getAccelTable(AppleObjC, *DObj, DObj->getAppleObjCSection(),
758 DObj->getStringSection(), isLittleEndian());
759}
760
James Hendersona3acf992018-05-10 10:51:33 +0000761const DWARFDebugLine::LineTable *
Alexey Samsonov7a18c062015-05-19 21:54:32 +0000762DWARFContext::getLineTableForUnit(DWARFUnit *U) {
James Hendersona3acf992018-05-10 10:51:33 +0000763 Expected<const DWARFDebugLine::LineTable *> ExpectedLineTable =
764 getLineTableForUnit(U, DWARFDebugLine::warn);
765 if (!ExpectedLineTable) {
James Henderson004b7292018-05-21 15:30:54 +0000766 DWARFDebugLine::warn(ExpectedLineTable.takeError());
James Hendersona3acf992018-05-10 10:51:33 +0000767 return nullptr;
768 }
769 return *ExpectedLineTable;
770}
771
James Henderson004b7292018-05-21 15:30:54 +0000772Expected<const DWARFDebugLine::LineTable *> DWARFContext::getLineTableForUnit(
773 DWARFUnit *U, std::function<void(Error)> RecoverableErrorCallback) {
Benjamin Kramer679e1752011-09-15 20:43:18 +0000774 if (!Line)
Paul Robinson17536b92017-06-29 16:52:08 +0000775 Line.reset(new DWARFDebugLine);
David Blaikiec4e2bed2015-11-17 21:08:05 +0000776
Greg Claytonc8c10322016-12-13 18:25:19 +0000777 auto UnitDIE = U->getUnitDIE();
778 if (!UnitDIE)
Alexey Samsonov7a18c062015-05-19 21:54:32 +0000779 return nullptr;
David Blaikiec4e2bed2015-11-17 21:08:05 +0000780
Greg Clayton97d22182017-01-13 21:08:18 +0000781 auto Offset = toSectionOffset(UnitDIE.find(DW_AT_stmt_list));
Greg Clayton52fe1f62016-12-14 22:38:08 +0000782 if (!Offset)
Craig Topper2617dcc2014-04-15 06:32:26 +0000783 return nullptr; // No line table for this compile unit.
Benjamin Kramer5acab502011-09-15 02:12:05 +0000784
Greg Clayton52fe1f62016-12-14 22:38:08 +0000785 uint32_t stmtOffset = *Offset + U->getLineTableOffset();
Benjamin Kramer679e1752011-09-15 20:43:18 +0000786 // See if the line table is cached.
Eric Christopher494109b2012-10-16 23:46:25 +0000787 if (const DWARFLineTable *lt = Line->getLineTable(stmtOffset))
Benjamin Kramer679e1752011-09-15 20:43:18 +0000788 return lt;
789
Greg Clayton48ff66a2017-05-04 18:29:44 +0000790 // Make sure the offset is good before we try to parse.
Paul Robinson17536b92017-06-29 16:52:08 +0000791 if (stmtOffset >= U->getLineSection().Data.size())
Jonas Devlieghere27476ce2017-09-13 09:43:05 +0000792 return nullptr;
Greg Clayton48ff66a2017-05-04 18:29:44 +0000793
Benjamin Kramer679e1752011-09-15 20:43:18 +0000794 // We have to parse it first.
Rafael Espindolac398e672017-07-19 22:27:28 +0000795 DWARFDataExtractor lineData(*DObj, U->getLineSection(), isLittleEndian(),
Paul Robinson17536b92017-06-29 16:52:08 +0000796 U->getAddressByteSize());
James Hendersona3acf992018-05-10 10:51:33 +0000797 return Line->getOrParseLineTable(lineData, stmtOffset, *this, U,
James Henderson004b7292018-05-21 15:30:54 +0000798 RecoverableErrorCallback);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000799}
800
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000801void DWARFContext::parseCompileUnits() {
Rafael Espindolac398e672017-07-19 22:27:28 +0000802 CUs.parse(*this, DObj->getInfoSection());
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000803}
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000804
David Blaikie03c089c2013-09-23 22:44:47 +0000805void DWARFContext::parseTypeUnits() {
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000806 if (!TUs.empty())
807 return;
Rafael Espindolac398e672017-07-19 22:27:28 +0000808 DObj->forEachTypesSections([&](const DWARFSection &S) {
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000809 TUs.emplace_back();
Rafael Espindola5e5dfa12017-07-12 21:08:24 +0000810 TUs.back().parse(*this, S);
811 });
David Blaikie03c089c2013-09-23 22:44:47 +0000812}
813
Eric Christopherda4b2192013-01-02 23:52:13 +0000814void DWARFContext::parseDWOCompileUnits() {
Rafael Espindolac398e672017-07-19 22:27:28 +0000815 DWOCUs.parseDWO(*this, DObj->getInfoDWOSection());
Eric Christopherda4b2192013-01-02 23:52:13 +0000816}
817
David Blaikie92d9d622014-01-09 05:08:24 +0000818void DWARFContext::parseDWOTypeUnits() {
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000819 if (!DWOTUs.empty())
820 return;
Rafael Espindolac398e672017-07-19 22:27:28 +0000821 DObj->forEachTypesDWOSections([&](const DWARFSection &S) {
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000822 DWOTUs.emplace_back();
Rafael Espindola5e5dfa12017-07-12 21:08:24 +0000823 DWOTUs.back().parseDWO(*this, S);
824 });
David Blaikie92d9d622014-01-09 05:08:24 +0000825}
826
Alexey Samsonov45be7932012-08-30 07:49:50 +0000827DWARFCompileUnit *DWARFContext::getCompileUnitForOffset(uint32_t Offset) {
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000828 parseCompileUnits();
Frederic Riss4e126a02014-09-15 07:50:27 +0000829 return CUs.getUnitForOffset(Offset);
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000830}
831
Alexey Samsonov45be7932012-08-30 07:49:50 +0000832DWARFCompileUnit *DWARFContext::getCompileUnitForAddress(uint64_t Address) {
Benjamin Kramer112ec172011-09-15 21:59:13 +0000833 // First, get the offset of the compile unit.
Alexey Samsonov45be7932012-08-30 07:49:50 +0000834 uint32_t CUOffset = getDebugAranges()->findAddress(Address);
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000835 // Retrieve the compile unit.
Alexey Samsonov45be7932012-08-30 07:49:50 +0000836 return getCompileUnitForOffset(CUOffset);
837}
838
Jonas Devliegheref63ee642017-10-25 21:56:41 +0000839DWARFContext::DIEsForAddress DWARFContext::getDIEsForAddress(uint64_t Address) {
840 DIEsForAddress Result;
841
842 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
843 if (!CU)
844 return Result;
845
846 Result.CompileUnit = CU;
847 Result.FunctionDIE = CU->getSubroutineForAddress(Address);
848
849 std::vector<DWARFDie> Worklist;
850 Worklist.push_back(Result.FunctionDIE);
851 while (!Worklist.empty()) {
852 DWARFDie DIE = Worklist.back();
853 Worklist.pop_back();
854
855 if (DIE.getTag() == DW_TAG_lexical_block &&
856 DIE.addressRangeContainsAddress(Address)) {
857 Result.BlockDIE = DIE;
858 break;
859 }
860
861 for (auto Child : DIE)
862 Worklist.push_back(Child);
863 }
864
865 return Result;
866}
867
David Blaikieefc4eba2017-02-06 20:19:02 +0000868static bool getFunctionNameAndStartLineForAddress(DWARFCompileUnit *CU,
869 uint64_t Address,
870 FunctionNameKind Kind,
871 std::string &FunctionName,
872 uint32_t &StartLine) {
Alexey Samsonovd0109992014-04-18 21:36:39 +0000873 // The address may correspond to instruction in some inlined function,
874 // so we have to build the chain of inlined functions and take the
David Blaikieefc4eba2017-02-06 20:19:02 +0000875 // name of the topmost function in it.
Greg Claytonc8c10322016-12-13 18:25:19 +0000876 SmallVector<DWARFDie, 4> InlinedChain;
877 CU->getInlinedChainForAddress(Address, InlinedChain);
David Blaikieefc4eba2017-02-06 20:19:02 +0000878 if (InlinedChain.empty())
Alexey Samsonovd0109992014-04-18 21:36:39 +0000879 return false;
David Blaikieefc4eba2017-02-06 20:19:02 +0000880
881 const DWARFDie &DIE = InlinedChain[0];
882 bool FoundResult = false;
883 const char *Name = nullptr;
884 if (Kind != FunctionNameKind::None && (Name = DIE.getSubroutineName(Kind))) {
Alexey Samsonovd0109992014-04-18 21:36:39 +0000885 FunctionName = Name;
David Blaikieefc4eba2017-02-06 20:19:02 +0000886 FoundResult = true;
Alexey Samsonovd0109992014-04-18 21:36:39 +0000887 }
David Blaikieefc4eba2017-02-06 20:19:02 +0000888 if (auto DeclLineResult = DIE.getDeclLine()) {
889 StartLine = DeclLineResult;
890 FoundResult = true;
891 }
892
893 return FoundResult;
Alexey Samsonovd0109992014-04-18 21:36:39 +0000894}
895
Alexey Samsonov45be7932012-08-30 07:49:50 +0000896DILineInfo DWARFContext::getLineInfoForAddress(uint64_t Address,
Alexey Samsonovdce67342014-05-15 21:24:32 +0000897 DILineInfoSpecifier Spec) {
Alexey Samsonovd0109992014-04-18 21:36:39 +0000898 DILineInfo Result;
899
Alexey Samsonov45be7932012-08-30 07:49:50 +0000900 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
901 if (!CU)
Alexey Samsonovd0109992014-04-18 21:36:39 +0000902 return Result;
David Blaikieefc4eba2017-02-06 20:19:02 +0000903 getFunctionNameAndStartLineForAddress(CU, Address, Spec.FNKind,
904 Result.FunctionName,
905 Result.StartLine);
Alexey Samsonovdce67342014-05-15 21:24:32 +0000906 if (Spec.FLIKind != FileLineInfoKind::None) {
Frederic Riss101b5e22014-09-19 15:11:51 +0000907 if (const DWARFLineTable *LineTable = getLineTableForUnit(CU))
908 LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(),
909 Spec.FLIKind, Result);
Alexey Samsonovf4462fa2012-07-02 05:54:45 +0000910 }
Alexey Samsonovd0109992014-04-18 21:36:39 +0000911 return Result;
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000912}
David Blaikiea379b1812011-12-20 02:50:00 +0000913
Alexey Samsonovdce67342014-05-15 21:24:32 +0000914DILineInfoTable
915DWARFContext::getLineInfoForAddressRange(uint64_t Address, uint64_t Size,
916 DILineInfoSpecifier Spec) {
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000917 DILineInfoTable Lines;
918 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
919 if (!CU)
920 return Lines;
921
922 std::string FunctionName = "<invalid>";
David Blaikieefc4eba2017-02-06 20:19:02 +0000923 uint32_t StartLine = 0;
924 getFunctionNameAndStartLineForAddress(CU, Address, Spec.FNKind, FunctionName,
925 StartLine);
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000926
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000927 // If the Specifier says we don't need FileLineInfo, just
928 // return the top-most function at the starting address.
Alexey Samsonovdce67342014-05-15 21:24:32 +0000929 if (Spec.FLIKind == FileLineInfoKind::None) {
Alexey Samsonovd0109992014-04-18 21:36:39 +0000930 DILineInfo Result;
931 Result.FunctionName = FunctionName;
David Blaikieefc4eba2017-02-06 20:19:02 +0000932 Result.StartLine = StartLine;
Alexey Samsonovd0109992014-04-18 21:36:39 +0000933 Lines.push_back(std::make_pair(Address, Result));
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000934 return Lines;
935 }
936
Frederic Rissec8a5ba2014-09-04 06:14:40 +0000937 const DWARFLineTable *LineTable = getLineTableForUnit(CU);
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000938
939 // Get the index of row we're looking for in the line table.
940 std::vector<uint32_t> RowVector;
941 if (!LineTable->lookupAddressRange(Address, Size, RowVector))
942 return Lines;
943
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000944 for (uint32_t RowIndex : RowVector) {
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000945 // Take file number and line/column from the row.
946 const DWARFDebugLine::Row &Row = LineTable->Rows[RowIndex];
Alexey Samsonovd0109992014-04-18 21:36:39 +0000947 DILineInfo Result;
Frederic Riss101b5e22014-09-19 15:11:51 +0000948 LineTable->getFileNameByIndex(Row.File, CU->getCompilationDir(),
949 Spec.FLIKind, Result.FileName);
Alexey Samsonovd0109992014-04-18 21:36:39 +0000950 Result.FunctionName = FunctionName;
951 Result.Line = Row.Line;
952 Result.Column = Row.Column;
David Blaikieefc4eba2017-02-06 20:19:02 +0000953 Result.StartLine = StartLine;
Alexey Samsonovd0109992014-04-18 21:36:39 +0000954 Lines.push_back(std::make_pair(Row.Address, Result));
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000955 }
956
957 return Lines;
958}
959
Alexey Samsonovdce67342014-05-15 21:24:32 +0000960DIInliningInfo
961DWARFContext::getInliningInfoForAddress(uint64_t Address,
962 DILineInfoSpecifier Spec) {
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000963 DIInliningInfo InliningInfo;
964
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000965 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
966 if (!CU)
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000967 return InliningInfo;
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000968
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000969 const DWARFLineTable *LineTable = nullptr;
Greg Claytonc8c10322016-12-13 18:25:19 +0000970 SmallVector<DWARFDie, 4> InlinedChain;
971 CU->getInlinedChainForAddress(Address, InlinedChain);
972 if (InlinedChain.size() == 0) {
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000973 // If there is no DIE for address (e.g. it is in unavailable .dwo file),
974 // try to at least get file/line info from symbol table.
Alexey Samsonovdce67342014-05-15 21:24:32 +0000975 if (Spec.FLIKind != FileLineInfoKind::None) {
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000976 DILineInfo Frame;
Frederic Rissec8a5ba2014-09-04 06:14:40 +0000977 LineTable = getLineTableForUnit(CU);
Frederic Riss101b5e22014-09-19 15:11:51 +0000978 if (LineTable &&
979 LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(),
980 Spec.FLIKind, Frame))
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000981 InliningInfo.addFrame(Frame);
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000982 }
983 return InliningInfo;
984 }
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000985
Dehao Chenef700d52017-04-17 20:10:39 +0000986 uint32_t CallFile = 0, CallLine = 0, CallColumn = 0, CallDiscriminator = 0;
Greg Claytonc8c10322016-12-13 18:25:19 +0000987 for (uint32_t i = 0, n = InlinedChain.size(); i != n; i++) {
988 DWARFDie &FunctionDIE = InlinedChain[i];
Alexey Samsonovd0109992014-04-18 21:36:39 +0000989 DILineInfo Frame;
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000990 // Get function name if necessary.
Greg Claytonc8c10322016-12-13 18:25:19 +0000991 if (const char *Name = FunctionDIE.getSubroutineName(Spec.FNKind))
Alexey Samsonovdce67342014-05-15 21:24:32 +0000992 Frame.FunctionName = Name;
David Blaikieefc4eba2017-02-06 20:19:02 +0000993 if (auto DeclLineResult = FunctionDIE.getDeclLine())
994 Frame.StartLine = DeclLineResult;
Alexey Samsonovdce67342014-05-15 21:24:32 +0000995 if (Spec.FLIKind != FileLineInfoKind::None) {
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000996 if (i == 0) {
997 // For the topmost frame, initialize the line table of this
998 // compile unit and fetch file/line info from it.
Frederic Rissec8a5ba2014-09-04 06:14:40 +0000999 LineTable = getLineTableForUnit(CU);
Alexey Samsonovc942e6b2012-09-04 08:12:33 +00001000 // For the topmost routine, get file/line info from line table.
Frederic Riss101b5e22014-09-19 15:11:51 +00001001 if (LineTable)
1002 LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(),
1003 Spec.FLIKind, Frame);
Alexey Samsonovc942e6b2012-09-04 08:12:33 +00001004 } else {
1005 // Otherwise, use call file, call line and call column from
1006 // previous DIE in inlined chain.
Frederic Riss101b5e22014-09-19 15:11:51 +00001007 if (LineTable)
1008 LineTable->getFileNameByIndex(CallFile, CU->getCompilationDir(),
1009 Spec.FLIKind, Frame.FileName);
Alexey Samsonovd0109992014-04-18 21:36:39 +00001010 Frame.Line = CallLine;
1011 Frame.Column = CallColumn;
Dehao Chenef700d52017-04-17 20:10:39 +00001012 Frame.Discriminator = CallDiscriminator;
Alexey Samsonovc942e6b2012-09-04 08:12:33 +00001013 }
1014 // Get call file/line/column of a current DIE.
1015 if (i + 1 < n) {
Dehao Chenef700d52017-04-17 20:10:39 +00001016 FunctionDIE.getCallerFrame(CallFile, CallLine, CallColumn,
1017 CallDiscriminator);
Alexey Samsonovc942e6b2012-09-04 08:12:33 +00001018 }
1019 }
Alexey Samsonovc942e6b2012-09-04 08:12:33 +00001020 InliningInfo.addFrame(Frame);
1021 }
1022 return InliningInfo;
1023}
1024
David Blaikief9803fb2017-05-23 00:30:42 +00001025std::shared_ptr<DWARFContext>
1026DWARFContext::getDWOContext(StringRef AbsolutePath) {
David Blaikie15d85fc2017-05-23 06:48:53 +00001027 if (auto S = DWP.lock()) {
David Blaikief9803fb2017-05-23 00:30:42 +00001028 DWARFContext *Ctxt = S->Context.get();
1029 return std::shared_ptr<DWARFContext>(std::move(S), Ctxt);
1030 }
1031
David Blaikie15d85fc2017-05-23 06:48:53 +00001032 std::weak_ptr<DWOFile> *Entry = &DWOFiles[AbsolutePath];
1033
1034 if (auto S = Entry->lock()) {
1035 DWARFContext *Ctxt = S->Context.get();
1036 return std::shared_ptr<DWARFContext>(std::move(S), Ctxt);
1037 }
1038
David Blaikie15d85fc2017-05-23 06:48:53 +00001039 Expected<OwningBinary<ObjectFile>> Obj = [&] {
1040 if (!CheckedForDWP) {
David Blaikiee5adb682017-07-30 01:34:08 +00001041 SmallString<128> DWPName;
1042 auto Obj = object::ObjectFile::createObjectFile(
1043 this->DWPName.empty()
1044 ? (DObj->getFileName() + ".dwp").toStringRef(DWPName)
1045 : StringRef(this->DWPName));
David Blaikie15d85fc2017-05-23 06:48:53 +00001046 if (Obj) {
1047 Entry = &DWP;
1048 return Obj;
1049 } else {
1050 CheckedForDWP = true;
1051 // TODO: Should this error be handled (maybe in a high verbosity mode)
1052 // before falling back to .dwo files?
1053 consumeError(Obj.takeError());
1054 }
1055 }
1056
1057 return object::ObjectFile::createObjectFile(AbsolutePath);
1058 }();
1059
David Blaikief9803fb2017-05-23 00:30:42 +00001060 if (!Obj) {
1061 // TODO: Actually report errors helpfully.
1062 consumeError(Obj.takeError());
1063 return nullptr;
1064 }
David Blaikie15d85fc2017-05-23 06:48:53 +00001065
1066 auto S = std::make_shared<DWOFile>();
David Blaikief9803fb2017-05-23 00:30:42 +00001067 S->File = std::move(Obj.get());
Rafael Espindolac398e672017-07-19 22:27:28 +00001068 S->Context = DWARFContext::create(*S->File.getBinary());
David Blaikie15d85fc2017-05-23 06:48:53 +00001069 *Entry = S;
David Blaikief9803fb2017-05-23 00:30:42 +00001070 auto *Ctxt = S->Context.get();
1071 return std::shared_ptr<DWARFContext>(std::move(S), Ctxt);
1072}
1073
George Rimar702dac62017-04-12 08:59:15 +00001074static Error createError(const Twine &Reason, llvm::Error E) {
1075 return make_error<StringError>(Reason + toString(std::move(E)),
1076 inconvertibleErrorCode());
1077}
1078
George Rimara25d3292017-05-27 18:10:23 +00001079/// SymInfo contains information about symbol: it's address
1080/// and section index which is -1LL for absolute symbols.
1081struct SymInfo {
1082 uint64_t Address;
1083 uint64_t SectionIndex;
1084};
1085
1086/// Returns the address of symbol relocation used against and a section index.
1087/// Used for futher relocations computation. Symbol's section load address is
1088static Expected<SymInfo> getSymbolInfo(const object::ObjectFile &Obj,
1089 const RelocationRef &Reloc,
1090 const LoadedObjectInfo *L,
1091 std::map<SymbolRef, SymInfo> &Cache) {
1092 SymInfo Ret = {0, (uint64_t)-1LL};
George Rimar702dac62017-04-12 08:59:15 +00001093 object::section_iterator RSec = Obj.section_end();
1094 object::symbol_iterator Sym = Reloc.getSymbol();
1095
George Rimara25d3292017-05-27 18:10:23 +00001096 std::map<SymbolRef, SymInfo>::iterator CacheIt = Cache.end();
George Rimar702dac62017-04-12 08:59:15 +00001097 // First calculate the address of the symbol or section as it appears
1098 // in the object file
1099 if (Sym != Obj.symbol_end()) {
George Rimar958b01a2017-05-15 11:45:28 +00001100 bool New;
George Rimara25d3292017-05-27 18:10:23 +00001101 std::tie(CacheIt, New) = Cache.insert({*Sym, {0, 0}});
George Rimar958b01a2017-05-15 11:45:28 +00001102 if (!New)
1103 return CacheIt->second;
1104
George Rimar702dac62017-04-12 08:59:15 +00001105 Expected<uint64_t> SymAddrOrErr = Sym->getAddress();
1106 if (!SymAddrOrErr)
George Rimar1af3cb22017-06-28 08:21:19 +00001107 return createError("failed to compute symbol address: ",
George Rimar702dac62017-04-12 08:59:15 +00001108 SymAddrOrErr.takeError());
1109
1110 // Also remember what section this symbol is in for later
1111 auto SectOrErr = Sym->getSection();
1112 if (!SectOrErr)
George Rimar1af3cb22017-06-28 08:21:19 +00001113 return createError("failed to get symbol section: ",
George Rimar702dac62017-04-12 08:59:15 +00001114 SectOrErr.takeError());
1115
1116 RSec = *SectOrErr;
George Rimara25d3292017-05-27 18:10:23 +00001117 Ret.Address = *SymAddrOrErr;
George Rimar702dac62017-04-12 08:59:15 +00001118 } else if (auto *MObj = dyn_cast<MachOObjectFile>(&Obj)) {
1119 RSec = MObj->getRelocationSection(Reloc.getRawDataRefImpl());
George Rimara25d3292017-05-27 18:10:23 +00001120 Ret.Address = RSec->getAddress();
George Rimar702dac62017-04-12 08:59:15 +00001121 }
1122
George Rimara25d3292017-05-27 18:10:23 +00001123 if (RSec != Obj.section_end())
1124 Ret.SectionIndex = RSec->getIndex();
1125
George Rimar702dac62017-04-12 08:59:15 +00001126 // If we are given load addresses for the sections, we need to adjust:
1127 // SymAddr = (Address of Symbol Or Section in File) -
1128 // (Address of Section in File) +
1129 // (Load Address of Section)
1130 // RSec is now either the section being targeted or the section
1131 // containing the symbol being targeted. In either case,
1132 // we need to perform the same computation.
1133 if (L && RSec != Obj.section_end())
1134 if (uint64_t SectionLoadAddress = L->getSectionLoadAddress(*RSec))
George Rimara25d3292017-05-27 18:10:23 +00001135 Ret.Address += SectionLoadAddress - RSec->getAddress();
George Rimar958b01a2017-05-15 11:45:28 +00001136
1137 if (CacheIt != Cache.end())
1138 CacheIt->second = Ret;
1139
George Rimar702dac62017-04-12 08:59:15 +00001140 return Ret;
1141}
1142
1143static bool isRelocScattered(const object::ObjectFile &Obj,
1144 const RelocationRef &Reloc) {
George Rimard4998b02017-04-13 09:52:50 +00001145 const MachOObjectFile *MachObj = dyn_cast<MachOObjectFile>(&Obj);
1146 if (!MachObj)
George Rimar702dac62017-04-12 08:59:15 +00001147 return false;
1148 // MachO also has relocations that point to sections and
1149 // scattered relocations.
George Rimar702dac62017-04-12 08:59:15 +00001150 auto RelocInfo = MachObj->getRelocation(Reloc.getRawDataRefImpl());
1151 return MachObj->isRelocationScattered(RelocInfo);
1152}
1153
Rafael Espindolac398e672017-07-19 22:27:28 +00001154ErrorPolicy DWARFContext::defaultErrorHandler(Error E) {
Jonas Devlieghere84e99262018-04-14 22:07:23 +00001155 WithColor::error() << toString(std::move(E)) << '\n';
George Rimar1af3cb22017-06-28 08:21:19 +00001156 return ErrorPolicy::Continue;
1157}
1158
Rafael Espindola87c3f4a92017-07-24 19:34:26 +00001159namespace {
1160struct DWARFSectionMap final : public DWARFSection {
1161 RelocAddrMap Relocs;
1162};
Rafael Espindola87c3f4a92017-07-24 19:34:26 +00001163
Rafael Espindolac398e672017-07-19 22:27:28 +00001164class DWARFObjInMemory final : public DWARFObject {
1165 bool IsLittleEndian;
1166 uint8_t AddressSize;
1167 StringRef FileName;
George Rimar6957ab52017-08-15 12:32:54 +00001168 const object::ObjectFile *Obj = nullptr;
George Rimare1c30f72017-08-15 15:54:43 +00001169 std::vector<SectionName> SectionNames;
Keno Fischerc780e8e2015-05-21 21:24:32 +00001170
Rafael Espindolac398e672017-07-19 22:27:28 +00001171 using TypeSectionMap = MapVector<object::SectionRef, DWARFSectionMap,
1172 std::map<object::SectionRef, unsigned>>;
Eric Christopher7370b552012-11-12 21:40:38 +00001173
Rafael Espindolac398e672017-07-19 22:27:28 +00001174 TypeSectionMap TypesSections;
1175 TypeSectionMap TypesDWOSections;
1176
1177 DWARFSectionMap InfoSection;
1178 DWARFSectionMap LocSection;
1179 DWARFSectionMap LineSection;
1180 DWARFSectionMap RangeSection;
James Henderson3fcc7452018-02-02 12:35:52 +00001181 DWARFSectionMap RnglistsSection;
Rafael Espindolac398e672017-07-19 22:27:28 +00001182 DWARFSectionMap StringOffsetSection;
1183 DWARFSectionMap InfoDWOSection;
1184 DWARFSectionMap LineDWOSection;
1185 DWARFSectionMap LocDWOSection;
1186 DWARFSectionMap StringOffsetDWOSection;
1187 DWARFSectionMap RangeDWOSection;
Wolfgang Piebad605592018-05-18 20:12:54 +00001188 DWARFSectionMap RnglistsDWOSection;
Rafael Espindolac398e672017-07-19 22:27:28 +00001189 DWARFSectionMap AddrSection;
1190 DWARFSectionMap AppleNamesSection;
1191 DWARFSectionMap AppleTypesSection;
1192 DWARFSectionMap AppleNamespacesSection;
1193 DWARFSectionMap AppleObjCSection;
Pavel Labath3c9a9182018-01-29 11:08:32 +00001194 DWARFSectionMap DebugNamesSection;
Rafael Espindolac398e672017-07-19 22:27:28 +00001195
1196 DWARFSectionMap *mapNameToDWARFSection(StringRef Name) {
1197 return StringSwitch<DWARFSectionMap *>(Name)
1198 .Case("debug_info", &InfoSection)
1199 .Case("debug_loc", &LocSection)
1200 .Case("debug_line", &LineSection)
1201 .Case("debug_str_offsets", &StringOffsetSection)
1202 .Case("debug_ranges", &RangeSection)
James Henderson3fcc7452018-02-02 12:35:52 +00001203 .Case("debug_rnglists", &RnglistsSection)
Rafael Espindolac398e672017-07-19 22:27:28 +00001204 .Case("debug_info.dwo", &InfoDWOSection)
1205 .Case("debug_loc.dwo", &LocDWOSection)
1206 .Case("debug_line.dwo", &LineDWOSection)
Pavel Labath3c9a9182018-01-29 11:08:32 +00001207 .Case("debug_names", &DebugNamesSection)
Wolfgang Piebad605592018-05-18 20:12:54 +00001208 .Case("debug_rnglists.dwo", &RnglistsDWOSection)
Rafael Espindolac398e672017-07-19 22:27:28 +00001209 .Case("debug_str_offsets.dwo", &StringOffsetDWOSection)
1210 .Case("debug_addr", &AddrSection)
1211 .Case("apple_names", &AppleNamesSection)
1212 .Case("apple_types", &AppleTypesSection)
1213 .Case("apple_namespaces", &AppleNamespacesSection)
1214 .Case("apple_namespac", &AppleNamespacesSection)
1215 .Case("apple_objc", &AppleObjCSection)
1216 .Default(nullptr);
1217 }
1218
1219 StringRef AbbrevSection;
1220 StringRef ARangeSection;
1221 StringRef DebugFrameSection;
1222 StringRef EHFrameSection;
1223 StringRef StringSection;
1224 StringRef MacinfoSection;
1225 StringRef PubNamesSection;
1226 StringRef PubTypesSection;
1227 StringRef GnuPubNamesSection;
1228 StringRef AbbrevDWOSection;
1229 StringRef StringDWOSection;
1230 StringRef GnuPubTypesSection;
1231 StringRef CUIndexSection;
1232 StringRef GdbIndexSection;
1233 StringRef TUIndexSection;
Paul Robinsonb6aa01c2018-01-25 22:02:36 +00001234 StringRef LineStringSection;
Rafael Espindolac398e672017-07-19 22:27:28 +00001235
1236 SmallVector<SmallString<32>, 4> UncompressedSections;
1237
1238 StringRef *mapSectionToMember(StringRef Name) {
1239 if (DWARFSection *Sec = mapNameToDWARFSection(Name))
1240 return &Sec->Data;
1241 return StringSwitch<StringRef *>(Name)
1242 .Case("debug_abbrev", &AbbrevSection)
1243 .Case("debug_aranges", &ARangeSection)
1244 .Case("debug_frame", &DebugFrameSection)
1245 .Case("eh_frame", &EHFrameSection)
1246 .Case("debug_str", &StringSection)
1247 .Case("debug_macinfo", &MacinfoSection)
1248 .Case("debug_pubnames", &PubNamesSection)
1249 .Case("debug_pubtypes", &PubTypesSection)
1250 .Case("debug_gnu_pubnames", &GnuPubNamesSection)
1251 .Case("debug_gnu_pubtypes", &GnuPubTypesSection)
1252 .Case("debug_abbrev.dwo", &AbbrevDWOSection)
1253 .Case("debug_str.dwo", &StringDWOSection)
1254 .Case("debug_cu_index", &CUIndexSection)
1255 .Case("debug_tu_index", &TUIndexSection)
1256 .Case("gdb_index", &GdbIndexSection)
Paul Robinsonb6aa01c2018-01-25 22:02:36 +00001257 .Case("debug_line_str", &LineStringSection)
Rafael Espindolac398e672017-07-19 22:27:28 +00001258 // Any more debug info sections go here.
1259 .Default(nullptr);
1260 }
1261
1262 /// If Sec is compressed section, decompresses and updates its contents
1263 /// provided by Data. Otherwise leaves it unchanged.
1264 Error maybeDecompress(const object::SectionRef &Sec, StringRef Name,
1265 StringRef &Data) {
1266 if (!Decompressor::isCompressed(Sec))
1267 return Error::success();
1268
1269 Expected<Decompressor> Decompressor =
1270 Decompressor::create(Name, Data, IsLittleEndian, AddressSize == 8);
1271 if (!Decompressor)
1272 return Decompressor.takeError();
1273
1274 SmallString<32> Out;
1275 if (auto Err = Decompressor->resizeAndDecompress(Out))
1276 return Err;
1277
1278 UncompressedSections.emplace_back(std::move(Out));
1279 Data = UncompressedSections.back();
1280
1281 return Error::success();
1282 }
1283
1284public:
1285 DWARFObjInMemory(const StringMap<std::unique_ptr<MemoryBuffer>> &Sections,
1286 uint8_t AddrSize, bool IsLittleEndian)
1287 : IsLittleEndian(IsLittleEndian) {
1288 for (const auto &SecIt : Sections) {
1289 if (StringRef *SectionData = mapSectionToMember(SecIt.first()))
1290 *SectionData = SecIt.second->getBuffer();
Alexey Samsonov068fc8a2013-04-23 10:17:34 +00001291 }
Rafael Espindolac398e672017-07-19 22:27:28 +00001292 }
1293 DWARFObjInMemory(const object::ObjectFile &Obj, const LoadedObjectInfo *L,
1294 function_ref<ErrorPolicy(Error)> HandleError)
1295 : IsLittleEndian(Obj.isLittleEndian()),
George Rimar6957ab52017-08-15 12:32:54 +00001296 AddressSize(Obj.getBytesInAddress()), FileName(Obj.getFileName()),
1297 Obj(&Obj) {
Alexey Samsonov068fc8a2013-04-23 10:17:34 +00001298
George Rimare1c30f72017-08-15 15:54:43 +00001299 StringMap<unsigned> SectionAmountMap;
Rafael Espindolac398e672017-07-19 22:27:28 +00001300 for (const SectionRef &Section : Obj.sections()) {
1301 StringRef Name;
1302 Section.getName(Name);
George Rimare1c30f72017-08-15 15:54:43 +00001303 ++SectionAmountMap[Name];
1304 SectionNames.push_back({ Name, true });
1305
Rafael Espindolac398e672017-07-19 22:27:28 +00001306 // Skip BSS and Virtual sections, they aren't interesting.
1307 if (Section.isBSS() || Section.isVirtual())
George Rimarfed9f092017-05-17 12:10:51 +00001308 continue;
Keno Fischerc780e8e2015-05-21 21:24:32 +00001309
Jonas Devlieghere8af23872017-09-26 14:22:35 +00001310 // Skip sections stripped by dsymutil.
1311 if (Section.isStripped())
1312 continue;
1313
Rafael Espindolac398e672017-07-19 22:27:28 +00001314 StringRef Data;
1315 section_iterator RelocatedSection = Section.getRelocatedSection();
1316 // Try to obtain an already relocated version of this section.
1317 // Else use the unrelocated section from the object file. We'll have to
1318 // apply relocations ourselves later.
1319 if (!L || !L->getLoadedSectionContents(*RelocatedSection, Data))
1320 Section.getContents(Data);
George Rimarfed9f092017-05-17 12:10:51 +00001321
Rafael Espindolac398e672017-07-19 22:27:28 +00001322 if (auto Err = maybeDecompress(Section, Name, Data)) {
1323 ErrorPolicy EP = HandleError(createError(
1324 "failed to decompress '" + Name + "', ", std::move(Err)));
George Rimar1af3cb22017-06-28 08:21:19 +00001325 if (EP == ErrorPolicy::Halt)
1326 return;
George Rimarfed9f092017-05-17 12:10:51 +00001327 continue;
1328 }
Rafael Espindolac398e672017-07-19 22:27:28 +00001329
1330 // Compressed sections names in GNU style starts from ".z",
1331 // at this point section is decompressed and we drop compression prefix.
1332 Name = Name.substr(
1333 Name.find_first_not_of("._z")); // Skip ".", "z" and "_" prefixes.
1334
1335 // Map platform specific debug section names to DWARF standard section
1336 // names.
1337 Name = Obj.mapDebugSectionName(Name);
1338
1339 if (StringRef *SectionData = mapSectionToMember(Name)) {
1340 *SectionData = Data;
1341 if (Name == "debug_ranges") {
1342 // FIXME: Use the other dwo range section when we emit it.
1343 RangeDWOSection.Data = Data;
1344 }
1345 } else if (Name == "debug_types") {
1346 // Find debug_types data by section rather than name as there are
1347 // multiple, comdat grouped, debug_types sections.
1348 TypesSections[Section].Data = Data;
1349 } else if (Name == "debug_types.dwo") {
1350 TypesDWOSections[Section].Data = Data;
1351 }
1352
1353 if (RelocatedSection == Obj.section_end())
1354 continue;
1355
1356 StringRef RelSecName;
1357 StringRef RelSecData;
1358 RelocatedSection->getName(RelSecName);
1359
1360 // If the section we're relocating was relocated already by the JIT,
1361 // then we used the relocated version above, so we do not need to process
1362 // relocations for it now.
1363 if (L && L->getLoadedSectionContents(*RelocatedSection, RelSecData))
1364 continue;
1365
1366 // In Mach-o files, the relocations do not need to be applied if
1367 // there is no load offset to apply. The value read at the
1368 // relocation point already factors in the section address
1369 // (actually applying the relocations will produce wrong results
1370 // as the section address will be added twice).
1371 if (!L && isa<MachOObjectFile>(&Obj))
1372 continue;
1373
1374 RelSecName = RelSecName.substr(
1375 RelSecName.find_first_not_of("._z")); // Skip . and _ prefixes.
1376
1377 // TODO: Add support for relocations in other sections as needed.
1378 // Record relocations for the debug_info and debug_line sections.
1379 DWARFSectionMap *Sec = mapNameToDWARFSection(RelSecName);
1380 RelocAddrMap *Map = Sec ? &Sec->Relocs : nullptr;
1381 if (!Map) {
1382 // Find debug_types relocs by section rather than name as there are
1383 // multiple, comdat grouped, debug_types sections.
1384 if (RelSecName == "debug_types")
1385 Map =
1386 &static_cast<DWARFSectionMap &>(TypesSections[*RelocatedSection])
1387 .Relocs;
1388 else if (RelSecName == "debug_types.dwo")
1389 Map = &static_cast<DWARFSectionMap &>(
1390 TypesDWOSections[*RelocatedSection])
1391 .Relocs;
1392 else
1393 continue;
1394 }
1395
1396 if (Section.relocation_begin() == Section.relocation_end())
1397 continue;
1398
1399 // Symbol to [address, section index] cache mapping.
1400 std::map<SymbolRef, SymInfo> AddrCache;
1401 for (const RelocationRef &Reloc : Section.relocations()) {
1402 // FIXME: it's not clear how to correctly handle scattered
1403 // relocations.
1404 if (isRelocScattered(Obj, Reloc))
1405 continue;
1406
1407 Expected<SymInfo> SymInfoOrErr =
1408 getSymbolInfo(Obj, Reloc, L, AddrCache);
1409 if (!SymInfoOrErr) {
1410 if (HandleError(SymInfoOrErr.takeError()) == ErrorPolicy::Halt)
1411 return;
1412 continue;
1413 }
1414
1415 object::RelocVisitor V(Obj);
1416 uint64_t Val = V.visit(Reloc.getType(), Reloc, SymInfoOrErr->Address);
1417 if (V.error()) {
1418 SmallString<32> Type;
1419 Reloc.getTypeName(Type);
1420 ErrorPolicy EP = HandleError(
1421 createError("failed to compute relocation: " + Type + ", ",
1422 errorCodeToError(object_error::parse_failed)));
1423 if (EP == ErrorPolicy::Halt)
1424 return;
1425 continue;
1426 }
1427 RelocAddrEntry Rel = {SymInfoOrErr->SectionIndex, Val};
1428 Map->insert({Reloc.getOffset(), Rel});
1429 }
Eric Christopher7370b552012-11-12 21:40:38 +00001430 }
George Rimare1c30f72017-08-15 15:54:43 +00001431
1432 for (SectionName &S : SectionNames)
1433 if (SectionAmountMap[S.Name] > 1)
1434 S.IsNameUnique = false;
Eric Christopher7370b552012-11-12 21:40:38 +00001435 }
Eric Christopher7370b552012-11-12 21:40:38 +00001436
Rafael Espindolac398e672017-07-19 22:27:28 +00001437 Optional<RelocAddrEntry> find(const DWARFSection &S,
1438 uint64_t Pos) const override {
1439 auto &Sec = static_cast<const DWARFSectionMap &>(S);
1440 RelocAddrMap::const_iterator AI = Sec.Relocs.find(Pos);
1441 if (AI == Sec.Relocs.end())
1442 return None;
1443 return AI->second;
Chris Bieneman2e752db2017-01-20 19:03:14 +00001444 }
Rafael Espindolac398e672017-07-19 22:27:28 +00001445
George Rimar6957ab52017-08-15 12:32:54 +00001446 const object::ObjectFile *getFile() const override { return Obj; }
1447
George Rimare1c30f72017-08-15 15:54:43 +00001448 ArrayRef<SectionName> getSectionNames() const override {
1449 return SectionNames;
1450 }
1451
Rafael Espindolac398e672017-07-19 22:27:28 +00001452 bool isLittleEndian() const override { return IsLittleEndian; }
1453 StringRef getAbbrevDWOSection() const override { return AbbrevDWOSection; }
1454 const DWARFSection &getLineDWOSection() const override {
1455 return LineDWOSection;
1456 }
1457 const DWARFSection &getLocDWOSection() const override {
1458 return LocDWOSection;
1459 }
1460 StringRef getStringDWOSection() const override { return StringDWOSection; }
1461 const DWARFSection &getStringOffsetDWOSection() const override {
1462 return StringOffsetDWOSection;
1463 }
1464 const DWARFSection &getRangeDWOSection() const override {
1465 return RangeDWOSection;
1466 }
Wolfgang Piebad605592018-05-18 20:12:54 +00001467 const DWARFSection &getRnglistsDWOSection() const override {
1468 return RnglistsDWOSection;
1469 }
Rafael Espindolac398e672017-07-19 22:27:28 +00001470 const DWARFSection &getAddrSection() const override { return AddrSection; }
1471 StringRef getCUIndexSection() const override { return CUIndexSection; }
1472 StringRef getGdbIndexSection() const override { return GdbIndexSection; }
1473 StringRef getTUIndexSection() const override { return TUIndexSection; }
1474
1475 // DWARF v5
1476 const DWARFSection &getStringOffsetSection() const override {
1477 return StringOffsetSection;
1478 }
Paul Robinsonb6aa01c2018-01-25 22:02:36 +00001479 StringRef getLineStringSection() const override { return LineStringSection; }
Rafael Espindolac398e672017-07-19 22:27:28 +00001480
1481 // Sections for DWARF5 split dwarf proposal.
1482 const DWARFSection &getInfoDWOSection() const override {
1483 return InfoDWOSection;
1484 }
1485 void forEachTypesDWOSections(
1486 function_ref<void(const DWARFSection &)> F) const override {
1487 for (auto &P : TypesDWOSections)
1488 F(P.second);
1489 }
1490
1491 StringRef getAbbrevSection() const override { return AbbrevSection; }
1492 const DWARFSection &getLocSection() const override { return LocSection; }
1493 StringRef getARangeSection() const override { return ARangeSection; }
1494 StringRef getDebugFrameSection() const override { return DebugFrameSection; }
1495 StringRef getEHFrameSection() const override { return EHFrameSection; }
1496 const DWARFSection &getLineSection() const override { return LineSection; }
1497 StringRef getStringSection() const override { return StringSection; }
1498 const DWARFSection &getRangeSection() const override { return RangeSection; }
James Henderson3fcc7452018-02-02 12:35:52 +00001499 const DWARFSection &getRnglistsSection() const override {
1500 return RnglistsSection;
1501 }
Rafael Espindolac398e672017-07-19 22:27:28 +00001502 StringRef getMacinfoSection() const override { return MacinfoSection; }
1503 StringRef getPubNamesSection() const override { return PubNamesSection; }
1504 StringRef getPubTypesSection() const override { return PubTypesSection; }
1505 StringRef getGnuPubNamesSection() const override {
1506 return GnuPubNamesSection;
1507 }
1508 StringRef getGnuPubTypesSection() const override {
1509 return GnuPubTypesSection;
1510 }
1511 const DWARFSection &getAppleNamesSection() const override {
1512 return AppleNamesSection;
1513 }
1514 const DWARFSection &getAppleTypesSection() const override {
1515 return AppleTypesSection;
1516 }
1517 const DWARFSection &getAppleNamespacesSection() const override {
1518 return AppleNamespacesSection;
1519 }
1520 const DWARFSection &getAppleObjCSection() const override {
1521 return AppleObjCSection;
1522 }
Pavel Labath3c9a9182018-01-29 11:08:32 +00001523 const DWARFSection &getDebugNamesSection() const override {
1524 return DebugNamesSection;
1525 }
Rafael Espindolac398e672017-07-19 22:27:28 +00001526
1527 StringRef getFileName() const override { return FileName; }
1528 uint8_t getAddressSize() const override { return AddressSize; }
1529 const DWARFSection &getInfoSection() const override { return InfoSection; }
1530 void forEachTypesSections(
1531 function_ref<void(const DWARFSection &)> F) const override {
1532 for (auto &P : TypesSections)
1533 F(P.second);
1534 }
1535};
Benjamin Kramer49a49fe2017-08-20 13:03:48 +00001536} // namespace
Rafael Espindolac398e672017-07-19 22:27:28 +00001537
1538std::unique_ptr<DWARFContext>
1539DWARFContext::create(const object::ObjectFile &Obj, const LoadedObjectInfo *L,
David Blaikiee5adb682017-07-30 01:34:08 +00001540 function_ref<ErrorPolicy(Error)> HandleError,
1541 std::string DWPName) {
Reid Kleckner388f8802017-07-19 23:42:53 +00001542 auto DObj = llvm::make_unique<DWARFObjInMemory>(Obj, L, HandleError);
David Blaikiee5adb682017-07-30 01:34:08 +00001543 return llvm::make_unique<DWARFContext>(std::move(DObj), std::move(DWPName));
Chris Bieneman2e752db2017-01-20 19:03:14 +00001544}
1545
Rafael Espindolac398e672017-07-19 22:27:28 +00001546std::unique_ptr<DWARFContext>
1547DWARFContext::create(const StringMap<std::unique_ptr<MemoryBuffer>> &Sections,
1548 uint8_t AddrSize, bool isLittleEndian) {
Reid Kleckner388f8802017-07-19 23:42:53 +00001549 auto DObj =
1550 llvm::make_unique<DWARFObjInMemory>(Sections, AddrSize, isLittleEndian);
David Blaikiee5adb682017-07-30 01:34:08 +00001551 return llvm::make_unique<DWARFContext>(std::move(DObj), "");
Rafael Espindola77e87b32017-07-07 05:36:53 +00001552}
Reid Klecknera0587362017-08-29 21:41:21 +00001553
1554Error DWARFContext::loadRegisterInfo(const object::ObjectFile &Obj) {
1555 // Detect the architecture from the object file. We usually don't need OS
1556 // info to lookup a target and create register info.
1557 Triple TT;
1558 TT.setArch(Triple::ArchType(Obj.getArch()));
1559 TT.setVendor(Triple::UnknownVendor);
1560 TT.setOS(Triple::UnknownOS);
1561 std::string TargetLookupError;
1562 const Target *TheTarget =
1563 TargetRegistry::lookupTarget(TT.str(), TargetLookupError);
1564 if (!TargetLookupError.empty())
1565 return make_error<StringError>(TargetLookupError, inconvertibleErrorCode());
1566 RegInfo.reset(TheTarget->createMCRegInfo(TT.str()));
1567 return Error::success();
1568}