blob: 6fbe55bbdc70c95636dc518c817d1f3209449880 [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 Pieb6ecd6a82017-12-21 19:38:13 +0000175 OS << "Contribution size = " << Contribution->Size
176 << ", Format = " << (Format == DWARF32 ? "DWARF32" : "DWARF64")
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000177 << ", Version = " << Version << "\n";
178
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000179 Offset = Contribution->Base;
180 unsigned EntrySize = Contribution->getDwarfOffsetByteSize();
181 while (Offset - Contribution->Base < Contribution->Size) {
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000182 OS << format("0x%8.8x: ", Offset);
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000183 // FIXME: We can only extract strings if the offset fits in 32 bits.
Paul Robinson17536b92017-06-29 16:52:08 +0000184 uint64_t StringOffset =
185 StrOffsetExt.getRelocatedValue(EntrySize, &Offset);
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000186 // Extract the string if we can and display it. Otherwise just report
187 // the offset.
188 if (StringOffset <= std::numeric_limits<uint32_t>::max()) {
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000189 uint32_t StringOffset32 = (uint32_t)StringOffset;
Simon Dardisb1b52c02017-08-07 16:08:11 +0000190 OS << format("%8.8x ", StringOffset32);
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000191 const char *S = StrData.getCStr(&StringOffset32);
192 if (S)
193 OS << format("\"%s\"", S);
194 } else
Simon Dardisec4ea992017-08-07 13:30:03 +0000195 OS << format("%16.16" PRIx64 " ", StringOffset);
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000196 OS << "\n";
197 }
198 }
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000199 // Report a gap at the end of the table.
200 if (Offset < SectionSize) {
201 OS << format("0x%8.8x: Gap, length = ", Offset);
202 OS << (SectionSize - Offset) << "\n";
203 }
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000204}
205
206// Dump a DWARF string offsets section. This may be a DWARF v5 formatted
207// string offsets section, where each compile or type unit contributes a
208// number of entries (string offsets), with each contribution preceded by
209// a header containing size and version number. Alternatively, it may be a
210// monolithic series of string offsets, as generated by the pre-DWARF v5
211// implementation of split DWARF.
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000212static void dumpStringOffsetsSection(
213 raw_ostream &OS, StringRef SectionName, const DWARFObject &Obj,
214 const DWARFSection &StringOffsetsSection, StringRef StringSection,
215 DWARFContext::cu_iterator_range CUs,
216 DWARFContext::tu_section_iterator_range TUSs, bool LittleEndian,
217 unsigned MaxVersion) {
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000218 // If we have at least one (compile or type) unit with DWARF v5 or greater,
219 // we assume that the section is formatted like a DWARF v5 string offsets
220 // section.
221 if (MaxVersion >= 5)
Rafael Espindolac398e672017-07-19 22:27:28 +0000222 dumpDWARFv5StringOffsetsSection(OS, SectionName, Obj, StringOffsetsSection,
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000223 StringSection, CUs, TUSs, LittleEndian);
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000224 else {
225 DataExtractor strOffsetExt(StringOffsetsSection.Data, LittleEndian, 0);
226 uint32_t offset = 0;
227 uint64_t size = StringOffsetsSection.Data.size();
228 // Ensure that size is a multiple of the size of an entry.
229 if (size & ((uint64_t)(sizeof(uint32_t) - 1))) {
230 OS << "error: size of ." << SectionName << " is not a multiple of "
231 << sizeof(uint32_t) << ".\n";
232 size &= -(uint64_t)sizeof(uint32_t);
233 }
234 DataExtractor StrData(StringSection, LittleEndian, 0);
235 while (offset < size) {
236 OS << format("0x%8.8x: ", offset);
237 uint32_t StringOffset = strOffsetExt.getU32(&offset);
238 OS << format("%8.8x ", StringOffset);
239 const char *S = StrData.getCStr(&StringOffset);
240 if (S)
241 OS << format("\"%s\"", S);
242 OS << "\n";
243 }
244 }
245}
246
Adrian Prantl057d3362017-09-15 23:04:04 +0000247void DWARFContext::dump(
248 raw_ostream &OS, DIDumpOptions DumpOpts,
249 std::array<Optional<uint64_t>, DIDT_ID_Count> DumpOffsets) {
250
251 Optional<uint64_t> DumpOffset;
Adrian Prantl7bc1b282017-09-11 22:59:45 +0000252 uint64_t DumpType = DumpOpts.DumpType;
Adrian Prantlf4bc1f72017-06-01 18:18:23 +0000253
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000254 StringRef Extension = sys::path::extension(DObj->getFileName());
255 bool IsDWO = (Extension == ".dwo") || (Extension == ".dwp");
256
257 // Print UUID header.
Adrian Prantl3dcd1222017-09-13 18:22:59 +0000258 const auto *ObjFile = DObj->getFile();
Adrian Prantl3dcd1222017-09-13 18:22:59 +0000259 if (DumpType & DIDT_UUID)
260 dumpUUID(OS, *ObjFile);
261
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000262 // Print a header for each explicitly-requested section.
263 // Otherwise just print one for non-empty sections.
Adrian Prantl057d3362017-09-15 23:04:04 +0000264 // Only print empty .dwo section headers when dumping a .dwo file.
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000265 bool Explicit = DumpType != DIDT_All && !IsDWO;
Adrian Prantl057d3362017-09-15 23:04:04 +0000266 bool ExplicitDWO = Explicit && IsDWO;
267 auto shouldDump = [&](bool Explicit, const char *Name, unsigned ID,
268 StringRef Section) {
269 DumpOffset = DumpOffsets[ID];
270 unsigned Mask = 1U << ID;
271 bool Should = (DumpType & Mask) && (Explicit || !Section.empty());
Adrian Prantl84168022017-09-15 17:39:50 +0000272 if (Should)
Adrian Prantl057d3362017-09-15 23:04:04 +0000273 OS << "\n" << Name << " contents:\n";
Adrian Prantl84168022017-09-15 17:39:50 +0000274 return Should;
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000275 };
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000276
277 // Dump individual sections.
Adrian Prantl057d3362017-09-15 23:04:04 +0000278 if (shouldDump(Explicit, ".debug_abbrev", DIDT_ID_DebugAbbrev,
279 DObj->getAbbrevSection()))
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000280 getDebugAbbrev()->dump(OS);
Adrian Prantl057d3362017-09-15 23:04:04 +0000281 if (shouldDump(ExplicitDWO, ".debug_abbrev.dwo", DIDT_ID_DebugAbbrev,
282 DObj->getAbbrevDWOSection()))
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000283 getDebugAbbrevDWO()->dump(OS);
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000284
Adrian Prantl057d3362017-09-15 23:04:04 +0000285 auto dumpDebugInfo = [&](bool IsExplicit, const char *Name,
286 DWARFSection Section, cu_iterator_range CUs) {
287 if (shouldDump(IsExplicit, Name, DIDT_ID_DebugInfo, Section.Data)) {
Adrian Prantlc8d86532017-09-18 21:44:40 +0000288 if (DumpOffset)
Adrian Prantld3f9f212017-09-20 17:44:00 +0000289 getDIEForOffset(DumpOffset.getValue())
290 .dump(OS, 0, DumpOpts.noImplicitRecursion());
Adrian Prantlc8d86532017-09-18 21:44:40 +0000291 else
292 for (const auto &CU : CUs)
Adrian Prantl057d3362017-09-15 23:04:04 +0000293 CU->dump(OS, DumpOpts);
294 }
295 };
296 dumpDebugInfo(Explicit, ".debug_info", DObj->getInfoSection(),
297 compile_units());
298 dumpDebugInfo(ExplicitDWO, ".debug_info.dwo", DObj->getInfoDWOSection(),
299 dwo_compile_units());
David Blaikie622dce42014-01-08 23:29:59 +0000300
Adrian Prantl099d7e42017-09-16 16:58:18 +0000301 auto dumpDebugType = [&](const char *Name,
302 tu_section_iterator_range TUSections) {
303 OS << '\n' << Name << " contents:\n";
304 DumpOffset = DumpOffsets[DIDT_ID_DebugTypes];
305 for (const auto &TUS : TUSections)
306 for (const auto &TU : TUS)
307 if (DumpOffset)
Adrian Prantld3f9f212017-09-20 17:44:00 +0000308 TU->getDIEForOffset(*DumpOffset)
309 .dump(OS, 0, DumpOpts.noImplicitRecursion());
Adrian Prantl099d7e42017-09-16 16:58:18 +0000310 else
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000311 TU->dump(OS, DumpOpts);
Adrian Prantl099d7e42017-09-16 16:58:18 +0000312 };
313 if ((DumpType & DIDT_DebugTypes)) {
314 if (Explicit || getNumTypeUnits())
315 dumpDebugType(".debug_types", type_unit_sections());
316 if (ExplicitDWO || getNumDWOTypeUnits())
317 dumpDebugType(".debug_types.dwo", dwo_type_unit_sections());
David Blaikie03c089c2013-09-23 22:44:47 +0000318 }
319
Adrian Prantl057d3362017-09-15 23:04:04 +0000320 if (shouldDump(Explicit, ".debug_loc", DIDT_ID_DebugLoc,
Adrian Prantl84168022017-09-15 17:39:50 +0000321 DObj->getLocSection().Data)) {
Jonas Devlieghere622c5632017-09-27 09:33:36 +0000322 getDebugLoc()->dump(OS, getRegisterInfo(), DumpOffset);
David Blaikie18e73502013-06-19 21:37:13 +0000323 }
Adrian Prantl057d3362017-09-15 23:04:04 +0000324 if (shouldDump(ExplicitDWO, ".debug_loc.dwo", DIDT_ID_DebugLoc,
Adrian Prantl84168022017-09-15 17:39:50 +0000325 DObj->getLocDWOSection().Data)) {
Jonas Devlieghere622c5632017-09-27 09:33:36 +0000326 getDebugLocDWO()->dump(OS, getRegisterInfo(), DumpOffset);
David Blaikie9c550ac2014-03-25 01:44:02 +0000327 }
328
Jonas Devliegherec0a758d2017-09-18 14:15:57 +0000329 if (shouldDump(Explicit, ".debug_frame", DIDT_ID_DebugFrame,
Adrian Prantl62528e62017-09-21 18:52:03 +0000330 DObj->getDebugFrameSection()))
Rafael Auler86fb7bf2018-03-08 00:46:53 +0000331 getDebugFrame()->dump(OS, getRegisterInfo(), DumpOffset);
Jonas Devliegherec0a758d2017-09-18 14:15:57 +0000332
333 if (shouldDump(Explicit, ".eh_frame", DIDT_ID_DebugFrame,
Adrian Prantl62528e62017-09-21 18:52:03 +0000334 DObj->getEHFrameSection()))
Rafael Auler86fb7bf2018-03-08 00:46:53 +0000335 getEHFrame()->dump(OS, getRegisterInfo(), DumpOffset);
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000336
Adrian Prantl7bc1b282017-09-11 22:59:45 +0000337 if (DumpType & DIDT_DebugMacro) {
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000338 if (Explicit || !getDebugMacro()->empty()) {
339 OS << "\n.debug_macinfo contents:\n";
340 getDebugMacro()->dump(OS);
341 }
Amjad Aboude59cc3e2015-11-12 09:38:54 +0000342 }
343
Adrian Prantl057d3362017-09-15 23:04:04 +0000344 if (shouldDump(Explicit, ".debug_aranges", DIDT_ID_DebugAranges,
Adrian Prantl84168022017-09-15 17:39:50 +0000345 DObj->getARangeSection())) {
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000346 uint32_t offset = 0;
Rafael Espindolac398e672017-07-19 22:27:28 +0000347 DataExtractor arangesData(DObj->getARangeSection(), isLittleEndian(), 0);
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000348 DWARFDebugArangeSet set;
349 while (set.extract(arangesData, &offset))
350 set.dump(OS);
351 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000352
James Hendersona3acf992018-05-10 10:51:33 +0000353 auto DumpLineSection = [&](DWARFDebugLine::SectionParser Parser,
354 DIDumpOptions DumpOpts) {
355 while (!Parser.done()) {
356 if (DumpOffset && Parser.getOffset() != *DumpOffset) {
357 Parser.skip();
Alexey Samsonov7a18c062015-05-19 21:54:32 +0000358 continue;
Paul Robinson63811a42017-11-22 15:33:17 +0000359 }
James Hendersona3acf992018-05-10 10:51:33 +0000360 OS << "debug_line[" << format("0x%8.8x", Parser.getOffset()) << "]\n";
Paul Robinson63811a42017-11-22 15:33:17 +0000361 if (DumpOpts.Verbose) {
James Hendersona3acf992018-05-10 10:51:33 +0000362 Parser.parseNext(DWARFDebugLine::warn, DWARFDebugLine::warnForError,
363 &OS);
Paul Robinson63811a42017-11-22 15:33:17 +0000364 } else {
James Hendersona3acf992018-05-10 10:51:33 +0000365 DWARFDebugLine::LineTable LineTable = Parser.parseNext();
366 LineTable.dump(OS, DumpOpts);
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000367 }
Benjamin Kramer6dda0322011-09-15 18:02:20 +0000368 }
James Hendersona3acf992018-05-10 10:51:33 +0000369 };
370
371 if (shouldDump(Explicit, ".debug_line", DIDT_ID_DebugLine,
372 DObj->getLineSection().Data)) {
373 DWARFDataExtractor LineData(*DObj, DObj->getLineSection(), isLittleEndian(),
374 0);
375 DWARFDebugLine::SectionParser Parser(LineData, *this, compile_units(),
376 type_unit_sections());
377 DumpLineSection(Parser, DumpOpts);
Benjamin Kramer6dda0322011-09-15 18:02:20 +0000378 }
Benjamin Kramer07d4b1c2011-09-15 16:57:13 +0000379
Adrian Prantl057d3362017-09-15 23:04:04 +0000380 if (shouldDump(ExplicitDWO, ".debug_line.dwo", DIDT_ID_DebugLine,
Adrian Prantl84168022017-09-15 17:39:50 +0000381 DObj->getLineDWOSection().Data)) {
Paul Robinson63811a42017-11-22 15:33:17 +0000382 DWARFDataExtractor LineData(*DObj, DObj->getLineDWOSection(),
383 isLittleEndian(), 0);
James Hendersona3acf992018-05-10 10:51:33 +0000384 DWARFDebugLine::SectionParser Parser(LineData, *this, dwo_compile_units(),
385 dwo_type_unit_sections());
386 DumpLineSection(Parser, DumpOpts);
David Blaikie1d4736e2014-02-24 23:58:54 +0000387 }
388
Adrian Prantl057d3362017-09-15 23:04:04 +0000389 if (shouldDump(Explicit, ".debug_cu_index", DIDT_ID_DebugCUIndex,
Adrian Prantl84168022017-09-15 17:39:50 +0000390 DObj->getCUIndexSection())) {
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000391 getCUIndex().dump(OS);
392 }
393
Adrian Prantl057d3362017-09-15 23:04:04 +0000394 if (shouldDump(Explicit, ".debug_tu_index", DIDT_ID_DebugTUIndex,
Adrian Prantl84168022017-09-15 17:39:50 +0000395 DObj->getTUIndexSection())) {
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000396 getTUIndex().dump(OS);
397 }
398
Adrian Prantl057d3362017-09-15 23:04:04 +0000399 if (shouldDump(Explicit, ".debug_str", DIDT_ID_DebugStr,
Adrian Prantl84168022017-09-15 17:39:50 +0000400 DObj->getStringSection())) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000401 DataExtractor strData(DObj->getStringSection(), isLittleEndian(), 0);
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000402 uint32_t offset = 0;
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000403 uint32_t strOffset = 0;
404 while (const char *s = strData.getCStr(&offset)) {
405 OS << format("0x%8.8x: \"%s\"\n", strOffset, s);
406 strOffset = offset;
407 }
Benjamin Kramer07d4b1c2011-09-15 16:57:13 +0000408 }
Adrian Prantl057d3362017-09-15 23:04:04 +0000409 if (shouldDump(ExplicitDWO, ".debug_str.dwo", DIDT_ID_DebugStr,
Adrian Prantl84168022017-09-15 17:39:50 +0000410 DObj->getStringDWOSection())) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000411 DataExtractor strDWOData(DObj->getStringDWOSection(), isLittleEndian(), 0);
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000412 uint32_t offset = 0;
David Blaikie66865d62014-01-09 00:13:35 +0000413 uint32_t strDWOOffset = 0;
414 while (const char *s = strDWOData.getCStr(&offset)) {
415 OS << format("0x%8.8x: \"%s\"\n", strDWOOffset, s);
416 strDWOOffset = offset;
David Blaikie622dce42014-01-08 23:29:59 +0000417 }
David Blaikie66865d62014-01-09 00:13:35 +0000418 }
Paul Robinsonb6aa01c2018-01-25 22:02:36 +0000419 if (shouldDump(Explicit, ".debug_line_str", DIDT_ID_DebugLineStr,
420 DObj->getLineStringSection())) {
421 DataExtractor strData(DObj->getLineStringSection(), isLittleEndian(), 0);
422 uint32_t offset = 0;
423 uint32_t strOffset = 0;
424 while (const char *s = strData.getCStr(&offset)) {
Scott Linder16c7bda2018-02-23 23:01:06 +0000425 OS << format("0x%8.8x: \"", strOffset);
426 OS.write_escaped(s);
427 OS << "\"\n";
Paul Robinsonb6aa01c2018-01-25 22:02:36 +0000428 strOffset = offset;
429 }
430 }
David Blaikie622dce42014-01-08 23:29:59 +0000431
Adrian Prantl057d3362017-09-15 23:04:04 +0000432 if (shouldDump(Explicit, ".debug_ranges", DIDT_ID_DebugRanges,
Adrian Prantl84168022017-09-15 17:39:50 +0000433 DObj->getRangeSection().Data)) {
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000434 // In fact, different compile units may have different address byte
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000435 // sizes, but for simplicity we just use the address byte size of the
436 // last compile unit (there is no easy and fast way to associate address
437 // range list and the compile unit it describes).
438 // FIXME: savedAddressByteSize seems sketchy.
Paul Robinson63811a42017-11-22 15:33:17 +0000439 uint8_t savedAddressByteSize = 0;
440 for (const auto &CU : compile_units()) {
441 savedAddressByteSize = CU->getAddressByteSize();
442 break;
443 }
Rafael Espindolac398e672017-07-19 22:27:28 +0000444 DWARFDataExtractor rangesData(*DObj, DObj->getRangeSection(),
445 isLittleEndian(), savedAddressByteSize);
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000446 uint32_t offset = 0;
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000447 DWARFDebugRangeList rangeList;
Paul Robinson17536b92017-06-29 16:52:08 +0000448 while (rangeList.extract(rangesData, &offset))
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000449 rangeList.dump(OS);
Eric Christopherda4b2192013-01-02 23:52:13 +0000450 }
Eric Christopher962c9082013-01-15 23:56:56 +0000451
James Henderson3fcc7452018-02-02 12:35:52 +0000452 if (shouldDump(Explicit, ".debug_rnglists", DIDT_ID_DebugRnglists,
453 DObj->getRnglistsSection().Data)) {
454 DWARFDataExtractor rnglistData(*DObj, DObj->getRnglistsSection(),
455 isLittleEndian(), 0);
456 uint32_t Offset = 0;
457 while (rnglistData.isValidOffset(Offset)) {
Wolfgang Pieb3fb9e3f2018-04-05 21:01:49 +0000458 DWARFDebugRnglistTable Rnglists;
James Henderson3fcc7452018-02-02 12:35:52 +0000459 uint32_t TableOffset = Offset;
460 if (Error Err = Rnglists.extract(rnglistData, &Offset)) {
Jonas Devlieghere84e99262018-04-14 22:07:23 +0000461 WithColor::error() << toString(std::move(Err)) << '\n';
James Henderson3fcc7452018-02-02 12:35:52 +0000462 uint64_t Length = Rnglists.length();
463 // Keep going after an error, if we can, assuming that the length field
464 // could be read. If it couldn't, stop reading the section.
465 if (Length == 0)
466 break;
467 Offset = TableOffset + Length;
Wolfgang Pieba0729d42018-03-08 20:52:35 +0000468 } else {
469 Rnglists.dump(OS, DumpOpts);
470 }
James Henderson3fcc7452018-02-02 12:35:52 +0000471 }
472 }
473
Adrian Prantl057d3362017-09-15 23:04:04 +0000474 if (shouldDump(Explicit, ".debug_pubnames", DIDT_ID_DebugPubnames,
Adrian Prantl84168022017-09-15 17:39:50 +0000475 DObj->getPubNamesSection()))
Rafael Espindolac398e672017-07-19 22:27:28 +0000476 DWARFDebugPubTable(DObj->getPubNamesSection(), isLittleEndian(), false)
Adrian Prantl84168022017-09-15 17:39:50 +0000477 .dump(OS);
Krzysztof Parzyszek97438dc2013-02-12 16:20:28 +0000478
Adrian Prantl057d3362017-09-15 23:04:04 +0000479 if (shouldDump(Explicit, ".debug_pubtypes", DIDT_ID_DebugPubtypes,
Adrian Prantl84168022017-09-15 17:39:50 +0000480 DObj->getPubTypesSection()))
Rafael Espindolac398e672017-07-19 22:27:28 +0000481 DWARFDebugPubTable(DObj->getPubTypesSection(), isLittleEndian(), false)
Adrian Prantl84168022017-09-15 17:39:50 +0000482 .dump(OS);
Eric Christopher4c7e6ba2013-09-25 23:02:41 +0000483
Adrian Prantl057d3362017-09-15 23:04:04 +0000484 if (shouldDump(Explicit, ".debug_gnu_pubnames", DIDT_ID_DebugGnuPubnames,
Adrian Prantl84168022017-09-15 17:39:50 +0000485 DObj->getGnuPubNamesSection()))
Rafael Espindolac398e672017-07-19 22:27:28 +0000486 DWARFDebugPubTable(DObj->getGnuPubNamesSection(), isLittleEndian(),
George Rimare71e33f2016-12-17 09:10:32 +0000487 true /* GnuStyle */)
Adrian Prantl84168022017-09-15 17:39:50 +0000488 .dump(OS);
David Blaikieecd21ff2013-09-24 19:50:00 +0000489
Adrian Prantl057d3362017-09-15 23:04:04 +0000490 if (shouldDump(Explicit, ".debug_gnu_pubtypes", DIDT_ID_DebugGnuPubtypes,
Adrian Prantl84168022017-09-15 17:39:50 +0000491 DObj->getGnuPubTypesSection()))
Rafael Espindolac398e672017-07-19 22:27:28 +0000492 DWARFDebugPubTable(DObj->getGnuPubTypesSection(), isLittleEndian(),
George Rimare71e33f2016-12-17 09:10:32 +0000493 true /* GnuStyle */)
Adrian Prantl84168022017-09-15 17:39:50 +0000494 .dump(OS);
David Blaikie404d3042013-09-19 23:01:29 +0000495
Adrian Prantl057d3362017-09-15 23:04:04 +0000496 if (shouldDump(Explicit, ".debug_str_offsets", DIDT_ID_DebugStrOffsets,
Adrian Prantl84168022017-09-15 17:39:50 +0000497 DObj->getStringOffsetSection().Data))
Rafael Espindolac398e672017-07-19 22:27:28 +0000498 dumpStringOffsetsSection(
499 OS, "debug_str_offsets", *DObj, DObj->getStringOffsetSection(),
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000500 DObj->getStringSection(), compile_units(), type_unit_sections(),
501 isLittleEndian(), getMaxVersion());
Adrian Prantl057d3362017-09-15 23:04:04 +0000502 if (shouldDump(ExplicitDWO, ".debug_str_offsets.dwo", DIDT_ID_DebugStrOffsets,
Adrian Prantl84168022017-09-15 17:39:50 +0000503 DObj->getStringOffsetDWOSection().Data))
Rafael Espindolac398e672017-07-19 22:27:28 +0000504 dumpStringOffsetsSection(
505 OS, "debug_str_offsets.dwo", *DObj, DObj->getStringOffsetDWOSection(),
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000506 DObj->getStringDWOSection(), dwo_compile_units(),
507 dwo_type_unit_sections(), isLittleEndian(), getMaxVersion());
Frederic Risse837ec22014-11-14 16:15:53 +0000508
Adrian Prantl057d3362017-09-15 23:04:04 +0000509 if (shouldDump(Explicit, ".gnu_index", DIDT_ID_GdbIndex,
Adrian Prantl84168022017-09-15 17:39:50 +0000510 DObj->getGdbIndexSection())) {
George Rimar4f82df52016-09-23 11:01:53 +0000511 getGdbIndex().dump(OS);
512 }
513
Adrian Prantl057d3362017-09-15 23:04:04 +0000514 if (shouldDump(Explicit, ".apple_names", DIDT_ID_AppleNames,
Adrian Prantl84168022017-09-15 17:39:50 +0000515 DObj->getAppleNamesSection().Data))
Adrian Prantl99fdb9d2017-09-28 18:10:52 +0000516 getAppleNames().dump(OS);
Frederic Risse837ec22014-11-14 16:15:53 +0000517
Adrian Prantl057d3362017-09-15 23:04:04 +0000518 if (shouldDump(Explicit, ".apple_types", DIDT_ID_AppleTypes,
Adrian Prantl84168022017-09-15 17:39:50 +0000519 DObj->getAppleTypesSection().Data))
Adrian Prantl714ee4d2017-09-29 00:33:22 +0000520 getAppleTypes().dump(OS);
Frederic Risse837ec22014-11-14 16:15:53 +0000521
Adrian Prantl057d3362017-09-15 23:04:04 +0000522 if (shouldDump(Explicit, ".apple_namespaces", DIDT_ID_AppleNamespaces,
Adrian Prantl84168022017-09-15 17:39:50 +0000523 DObj->getAppleNamespacesSection().Data))
Adrian Prantlf51e7802017-09-29 00:52:33 +0000524 getAppleNamespaces().dump(OS);
Frederic Risse837ec22014-11-14 16:15:53 +0000525
Adrian Prantl057d3362017-09-15 23:04:04 +0000526 if (shouldDump(Explicit, ".apple_objc", DIDT_ID_AppleObjC,
Adrian Prantl84168022017-09-15 17:39:50 +0000527 DObj->getAppleObjCSection().Data))
Adrian Prantlf51e7802017-09-29 00:52:33 +0000528 getAppleObjC().dump(OS);
Pavel Labath3c9a9182018-01-29 11:08:32 +0000529 if (shouldDump(Explicit, ".debug_names", DIDT_ID_DebugNames,
530 DObj->getDebugNamesSection().Data))
531 getDebugNames().dump(OS);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000532}
533
David Blaikie15d85fc2017-05-23 06:48:53 +0000534DWARFCompileUnit *DWARFContext::getDWOCompileUnitForHash(uint64_t Hash) {
David Blaikiee79dda32017-09-19 18:36:11 +0000535 DWOCUs.parseDWO(*this, DObj->getInfoDWOSection(), true);
David Blaikiea62f1cb2017-07-30 15:15:58 +0000536
David Blaikieebac0b92017-07-30 08:12:07 +0000537 if (const auto &CUI = getCUIndex()) {
538 if (const auto *R = CUI.getFromHash(Hash))
David Blaikiee79dda32017-09-19 18:36:11 +0000539 return DWOCUs.getUnitForIndexEntry(*R);
David Blaikieebac0b92017-07-30 08:12:07 +0000540 return nullptr;
541 }
542
543 // If there's no index, just search through the CUs in the DWO - there's
544 // probably only one unless this is something like LTO - though an in-process
545 // built/cached lookup table could be used in that case to improve repeated
546 // lookups of different CUs in the DWO.
David Blaikie15d85fc2017-05-23 06:48:53 +0000547 for (const auto &DWOCU : dwo_compile_units())
548 if (DWOCU->getDWOId() == Hash)
549 return DWOCU.get();
550 return nullptr;
551}
552
Greg Claytonc7695a82017-05-02 20:28:33 +0000553DWARFDie DWARFContext::getDIEForOffset(uint32_t Offset) {
554 parseCompileUnits();
555 if (auto *CU = CUs.getUnitForOffset(Offset))
556 return CU->getDIEForOffset(Offset);
557 return DWARFDie();
558}
559
Jonas Devliegherec0a758d2017-09-18 14:15:57 +0000560bool DWARFContext::verify(raw_ostream &OS, DIDumpOptions DumpOpts) {
Greg Claytonc7695a82017-05-02 20:28:33 +0000561 bool Success = true;
Jonas Devlieghere27476ce2017-09-13 09:43:05 +0000562 DWARFVerifier verifier(OS, *this, DumpOpts);
Spyridoula Gravani73e17962017-07-27 00:59:33 +0000563
Spyridoula Gravani364b5352017-07-20 02:06:52 +0000564 Success &= verifier.handleDebugAbbrev();
Jonas Devliegherec0a758d2017-09-18 14:15:57 +0000565 if (DumpOpts.DumpType & DIDT_DebugInfo)
Spyridoula Gravani73e17962017-07-27 00:59:33 +0000566 Success &= verifier.handleDebugInfo();
Jonas Devliegherec0a758d2017-09-18 14:15:57 +0000567 if (DumpOpts.DumpType & DIDT_DebugLine)
Spyridoula Gravani73e17962017-07-27 00:59:33 +0000568 Success &= verifier.handleDebugLine();
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000569 Success &= verifier.handleAccelTables();
Greg Clayton48432cf2017-05-01 22:07:02 +0000570 return Success;
571}
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000572
David Blaikie82641be2015-11-17 00:39:55 +0000573const DWARFUnitIndex &DWARFContext::getCUIndex() {
574 if (CUIndex)
575 return *CUIndex;
576
Rafael Espindolac398e672017-07-19 22:27:28 +0000577 DataExtractor CUIndexData(DObj->getCUIndexSection(), isLittleEndian(), 0);
David Blaikie82641be2015-11-17 00:39:55 +0000578
David Blaikieb073cb92015-12-02 06:21:34 +0000579 CUIndex = llvm::make_unique<DWARFUnitIndex>(DW_SECT_INFO);
David Blaikie82641be2015-11-17 00:39:55 +0000580 CUIndex->parse(CUIndexData);
581 return *CUIndex;
582}
583
584const DWARFUnitIndex &DWARFContext::getTUIndex() {
585 if (TUIndex)
586 return *TUIndex;
587
Rafael Espindolac398e672017-07-19 22:27:28 +0000588 DataExtractor TUIndexData(DObj->getTUIndexSection(), isLittleEndian(), 0);
David Blaikie82641be2015-11-17 00:39:55 +0000589
David Blaikieb073cb92015-12-02 06:21:34 +0000590 TUIndex = llvm::make_unique<DWARFUnitIndex>(DW_SECT_TYPES);
David Blaikie82641be2015-11-17 00:39:55 +0000591 TUIndex->parse(TUIndexData);
592 return *TUIndex;
593}
594
George Rimar4f82df52016-09-23 11:01:53 +0000595DWARFGdbIndex &DWARFContext::getGdbIndex() {
596 if (GdbIndex)
597 return *GdbIndex;
598
Rafael Espindolac398e672017-07-19 22:27:28 +0000599 DataExtractor GdbIndexData(DObj->getGdbIndexSection(), true /*LE*/, 0);
George Rimar4f82df52016-09-23 11:01:53 +0000600 GdbIndex = llvm::make_unique<DWARFGdbIndex>();
601 GdbIndex->parse(GdbIndexData);
602 return *GdbIndex;
603}
604
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000605const DWARFDebugAbbrev *DWARFContext::getDebugAbbrev() {
606 if (Abbrev)
607 return Abbrev.get();
608
Rafael Espindolac398e672017-07-19 22:27:28 +0000609 DataExtractor abbrData(DObj->getAbbrevSection(), isLittleEndian(), 0);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000610
611 Abbrev.reset(new DWARFDebugAbbrev());
Alexey Samsonov4316df52014-04-25 21:10:56 +0000612 Abbrev->extract(abbrData);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000613 return Abbrev.get();
614}
615
Eric Christopherda4b2192013-01-02 23:52:13 +0000616const DWARFDebugAbbrev *DWARFContext::getDebugAbbrevDWO() {
617 if (AbbrevDWO)
618 return AbbrevDWO.get();
619
Rafael Espindolac398e672017-07-19 22:27:28 +0000620 DataExtractor abbrData(DObj->getAbbrevDWOSection(), isLittleEndian(), 0);
Eric Christopherda4b2192013-01-02 23:52:13 +0000621 AbbrevDWO.reset(new DWARFDebugAbbrev());
Alexey Samsonov4316df52014-04-25 21:10:56 +0000622 AbbrevDWO->extract(abbrData);
Eric Christopherda4b2192013-01-02 23:52:13 +0000623 return AbbrevDWO.get();
624}
625
David Blaikie18e73502013-06-19 21:37:13 +0000626const DWARFDebugLoc *DWARFContext::getDebugLoc() {
627 if (Loc)
628 return Loc.get();
629
Paul Robinson17536b92017-06-29 16:52:08 +0000630 Loc.reset(new DWARFDebugLoc);
Pavel Labath54ca2d62018-04-06 08:49:57 +0000631 // Assume all compile units have the same address byte size.
Paul Robinson17536b92017-06-29 16:52:08 +0000632 if (getNumCompileUnits()) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000633 DWARFDataExtractor LocData(*DObj, DObj->getLocSection(), isLittleEndian(),
Paul Robinson17536b92017-06-29 16:52:08 +0000634 getCompileUnitAtIndex(0)->getAddressByteSize());
635 Loc->parse(LocData);
636 }
David Blaikie18e73502013-06-19 21:37:13 +0000637 return Loc.get();
638}
639
David Blaikie9c550ac2014-03-25 01:44:02 +0000640const DWARFDebugLocDWO *DWARFContext::getDebugLocDWO() {
641 if (LocDWO)
642 return LocDWO.get();
643
David Blaikie9c550ac2014-03-25 01:44:02 +0000644 LocDWO.reset(new DWARFDebugLocDWO());
Pavel Labath54ca2d62018-04-06 08:49:57 +0000645 // Assume all compile units have the same address byte size.
646 if (getNumCompileUnits()) {
647 DataExtractor LocData(DObj->getLocDWOSection().Data, isLittleEndian(),
648 getCompileUnitAtIndex(0)->getAddressByteSize());
649 LocDWO->parse(LocData);
650 }
David Blaikie9c550ac2014-03-25 01:44:02 +0000651 return LocDWO.get();
652}
653
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000654const DWARFDebugAranges *DWARFContext::getDebugAranges() {
655 if (Aranges)
656 return Aranges.get();
657
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000658 Aranges.reset(new DWARFDebugAranges());
Alexey Samsonova1694c12012-11-16 08:36:25 +0000659 Aranges->generate(this);
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000660 return Aranges.get();
661}
662
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000663const DWARFDebugFrame *DWARFContext::getDebugFrame() {
664 if (DebugFrame)
665 return DebugFrame.get();
666
667 // There's a "bug" in the DWARFv3 standard with respect to the target address
668 // size within debug frame sections. While DWARF is supposed to be independent
669 // of its container, FDEs have fields with size being "target address size",
670 // which isn't specified in DWARF in general. It's only specified for CUs, but
671 // .eh_frame can appear without a .debug_info section. Follow the example of
672 // other tools (libdwarf) and extract this from the container (ObjectFile
673 // provides this information). This problem is fixed in DWARFv4
674 // See this dwarf-discuss discussion for more details:
675 // http://lists.dwarfstd.org/htdig.cgi/dwarf-discuss-dwarfstd.org/2011-December/001173.html
Rafael Auler86fb7bf2018-03-08 00:46:53 +0000676 DWARFDataExtractor debugFrameData(DObj->getDebugFrameSection(),
677 isLittleEndian(), DObj->getAddressSize());
Igor Laevsky03a670c2016-01-26 15:09:42 +0000678 DebugFrame.reset(new DWARFDebugFrame(false /* IsEH */));
679 DebugFrame->parse(debugFrameData);
680 return DebugFrame.get();
681}
682
683const DWARFDebugFrame *DWARFContext::getEHFrame() {
684 if (EHFrame)
685 return EHFrame.get();
686
Rafael Auler86fb7bf2018-03-08 00:46:53 +0000687 DWARFDataExtractor debugFrameData(DObj->getEHFrameSection(), isLittleEndian(),
688 DObj->getAddressSize());
Igor Laevsky03a670c2016-01-26 15:09:42 +0000689 DebugFrame.reset(new DWARFDebugFrame(true /* IsEH */));
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000690 DebugFrame->parse(debugFrameData);
691 return DebugFrame.get();
692}
693
Amjad Aboude59cc3e2015-11-12 09:38:54 +0000694const DWARFDebugMacro *DWARFContext::getDebugMacro() {
695 if (Macro)
696 return Macro.get();
697
Rafael Espindolac398e672017-07-19 22:27:28 +0000698 DataExtractor MacinfoData(DObj->getMacinfoSection(), isLittleEndian(), 0);
Amjad Aboude59cc3e2015-11-12 09:38:54 +0000699 Macro.reset(new DWARFDebugMacro());
700 Macro->parse(MacinfoData);
701 return Macro.get();
702}
703
Pavel Labath3c9a9182018-01-29 11:08:32 +0000704template <typename T>
705static T &getAccelTable(std::unique_ptr<T> &Cache, const DWARFObject &Obj,
706 const DWARFSection &Section, StringRef StringSection,
707 bool IsLittleEndian) {
Adrian Prantl99fdb9d2017-09-28 18:10:52 +0000708 if (Cache)
709 return *Cache;
710 DWARFDataExtractor AccelSection(Obj, Section, IsLittleEndian, 0);
711 DataExtractor StrData(StringSection, IsLittleEndian, 0);
Pavel Labath3c9a9182018-01-29 11:08:32 +0000712 Cache.reset(new T(AccelSection, StrData));
Jonas Devlieghereba915892017-12-11 18:22:47 +0000713 if (Error E = Cache->extract())
714 llvm::consumeError(std::move(E));
Adrian Prantl99fdb9d2017-09-28 18:10:52 +0000715 return *Cache;
716}
717
Pavel Labath3c9a9182018-01-29 11:08:32 +0000718const DWARFDebugNames &DWARFContext::getDebugNames() {
719 return getAccelTable(Names, *DObj, DObj->getDebugNamesSection(),
720 DObj->getStringSection(), isLittleEndian());
721}
722
Pavel Labath9b36fd22018-01-22 13:17:23 +0000723const AppleAcceleratorTable &DWARFContext::getAppleNames() {
Adrian Prantl99fdb9d2017-09-28 18:10:52 +0000724 return getAccelTable(AppleNames, *DObj, DObj->getAppleNamesSection(),
725 DObj->getStringSection(), isLittleEndian());
726}
727
Pavel Labath9b36fd22018-01-22 13:17:23 +0000728const AppleAcceleratorTable &DWARFContext::getAppleTypes() {
Adrian Prantl714ee4d2017-09-29 00:33:22 +0000729 return getAccelTable(AppleTypes, *DObj, DObj->getAppleTypesSection(),
730 DObj->getStringSection(), isLittleEndian());
731}
732
Pavel Labath9b36fd22018-01-22 13:17:23 +0000733const AppleAcceleratorTable &DWARFContext::getAppleNamespaces() {
Adrian Prantlf51e7802017-09-29 00:52:33 +0000734 return getAccelTable(AppleNamespaces, *DObj,
735 DObj->getAppleNamespacesSection(),
736 DObj->getStringSection(), isLittleEndian());
737}
738
Pavel Labath9b36fd22018-01-22 13:17:23 +0000739const AppleAcceleratorTable &DWARFContext::getAppleObjC() {
Adrian Prantlf51e7802017-09-29 00:52:33 +0000740 return getAccelTable(AppleObjC, *DObj, DObj->getAppleObjCSection(),
741 DObj->getStringSection(), isLittleEndian());
742}
743
James Hendersona3acf992018-05-10 10:51:33 +0000744const DWARFDebugLine::LineTable *
Alexey Samsonov7a18c062015-05-19 21:54:32 +0000745DWARFContext::getLineTableForUnit(DWARFUnit *U) {
James Hendersona3acf992018-05-10 10:51:33 +0000746 Expected<const DWARFDebugLine::LineTable *> ExpectedLineTable =
747 getLineTableForUnit(U, DWARFDebugLine::warn);
748 if (!ExpectedLineTable) {
749 DWARFDebugLine::warnForError(ExpectedLineTable.takeError());
750 return nullptr;
751 }
752 return *ExpectedLineTable;
753}
754
755Expected<const DWARFDebugLine::LineTable *>
756DWARFContext::getLineTableForUnit(DWARFUnit *U,
757 std::function<void(StringRef)> WarnCallback) {
Benjamin Kramer679e1752011-09-15 20:43:18 +0000758 if (!Line)
Paul Robinson17536b92017-06-29 16:52:08 +0000759 Line.reset(new DWARFDebugLine);
David Blaikiec4e2bed2015-11-17 21:08:05 +0000760
Greg Claytonc8c10322016-12-13 18:25:19 +0000761 auto UnitDIE = U->getUnitDIE();
762 if (!UnitDIE)
Alexey Samsonov7a18c062015-05-19 21:54:32 +0000763 return nullptr;
David Blaikiec4e2bed2015-11-17 21:08:05 +0000764
Greg Clayton97d22182017-01-13 21:08:18 +0000765 auto Offset = toSectionOffset(UnitDIE.find(DW_AT_stmt_list));
Greg Clayton52fe1f62016-12-14 22:38:08 +0000766 if (!Offset)
Craig Topper2617dcc2014-04-15 06:32:26 +0000767 return nullptr; // No line table for this compile unit.
Benjamin Kramer5acab502011-09-15 02:12:05 +0000768
Greg Clayton52fe1f62016-12-14 22:38:08 +0000769 uint32_t stmtOffset = *Offset + U->getLineTableOffset();
Benjamin Kramer679e1752011-09-15 20:43:18 +0000770 // See if the line table is cached.
Eric Christopher494109b2012-10-16 23:46:25 +0000771 if (const DWARFLineTable *lt = Line->getLineTable(stmtOffset))
Benjamin Kramer679e1752011-09-15 20:43:18 +0000772 return lt;
773
Greg Clayton48ff66a2017-05-04 18:29:44 +0000774 // Make sure the offset is good before we try to parse.
Paul Robinson17536b92017-06-29 16:52:08 +0000775 if (stmtOffset >= U->getLineSection().Data.size())
Jonas Devlieghere27476ce2017-09-13 09:43:05 +0000776 return nullptr;
Greg Clayton48ff66a2017-05-04 18:29:44 +0000777
Benjamin Kramer679e1752011-09-15 20:43:18 +0000778 // We have to parse it first.
Rafael Espindolac398e672017-07-19 22:27:28 +0000779 DWARFDataExtractor lineData(*DObj, U->getLineSection(), isLittleEndian(),
Paul Robinson17536b92017-06-29 16:52:08 +0000780 U->getAddressByteSize());
James Hendersona3acf992018-05-10 10:51:33 +0000781 return Line->getOrParseLineTable(lineData, stmtOffset, *this, U,
782 WarnCallback);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000783}
784
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000785void DWARFContext::parseCompileUnits() {
Rafael Espindolac398e672017-07-19 22:27:28 +0000786 CUs.parse(*this, DObj->getInfoSection());
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000787}
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000788
David Blaikie03c089c2013-09-23 22:44:47 +0000789void DWARFContext::parseTypeUnits() {
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000790 if (!TUs.empty())
791 return;
Rafael Espindolac398e672017-07-19 22:27:28 +0000792 DObj->forEachTypesSections([&](const DWARFSection &S) {
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000793 TUs.emplace_back();
Rafael Espindola5e5dfa12017-07-12 21:08:24 +0000794 TUs.back().parse(*this, S);
795 });
David Blaikie03c089c2013-09-23 22:44:47 +0000796}
797
Eric Christopherda4b2192013-01-02 23:52:13 +0000798void DWARFContext::parseDWOCompileUnits() {
Rafael Espindolac398e672017-07-19 22:27:28 +0000799 DWOCUs.parseDWO(*this, DObj->getInfoDWOSection());
Eric Christopherda4b2192013-01-02 23:52:13 +0000800}
801
David Blaikie92d9d622014-01-09 05:08:24 +0000802void DWARFContext::parseDWOTypeUnits() {
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000803 if (!DWOTUs.empty())
804 return;
Rafael Espindolac398e672017-07-19 22:27:28 +0000805 DObj->forEachTypesDWOSections([&](const DWARFSection &S) {
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000806 DWOTUs.emplace_back();
Rafael Espindola5e5dfa12017-07-12 21:08:24 +0000807 DWOTUs.back().parseDWO(*this, S);
808 });
David Blaikie92d9d622014-01-09 05:08:24 +0000809}
810
Alexey Samsonov45be7932012-08-30 07:49:50 +0000811DWARFCompileUnit *DWARFContext::getCompileUnitForOffset(uint32_t Offset) {
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000812 parseCompileUnits();
Frederic Riss4e126a02014-09-15 07:50:27 +0000813 return CUs.getUnitForOffset(Offset);
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000814}
815
Alexey Samsonov45be7932012-08-30 07:49:50 +0000816DWARFCompileUnit *DWARFContext::getCompileUnitForAddress(uint64_t Address) {
Benjamin Kramer112ec172011-09-15 21:59:13 +0000817 // First, get the offset of the compile unit.
Alexey Samsonov45be7932012-08-30 07:49:50 +0000818 uint32_t CUOffset = getDebugAranges()->findAddress(Address);
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000819 // Retrieve the compile unit.
Alexey Samsonov45be7932012-08-30 07:49:50 +0000820 return getCompileUnitForOffset(CUOffset);
821}
822
Jonas Devliegheref63ee642017-10-25 21:56:41 +0000823DWARFContext::DIEsForAddress DWARFContext::getDIEsForAddress(uint64_t Address) {
824 DIEsForAddress Result;
825
826 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
827 if (!CU)
828 return Result;
829
830 Result.CompileUnit = CU;
831 Result.FunctionDIE = CU->getSubroutineForAddress(Address);
832
833 std::vector<DWARFDie> Worklist;
834 Worklist.push_back(Result.FunctionDIE);
835 while (!Worklist.empty()) {
836 DWARFDie DIE = Worklist.back();
837 Worklist.pop_back();
838
839 if (DIE.getTag() == DW_TAG_lexical_block &&
840 DIE.addressRangeContainsAddress(Address)) {
841 Result.BlockDIE = DIE;
842 break;
843 }
844
845 for (auto Child : DIE)
846 Worklist.push_back(Child);
847 }
848
849 return Result;
850}
851
David Blaikieefc4eba2017-02-06 20:19:02 +0000852static bool getFunctionNameAndStartLineForAddress(DWARFCompileUnit *CU,
853 uint64_t Address,
854 FunctionNameKind Kind,
855 std::string &FunctionName,
856 uint32_t &StartLine) {
Alexey Samsonovd0109992014-04-18 21:36:39 +0000857 // The address may correspond to instruction in some inlined function,
858 // so we have to build the chain of inlined functions and take the
David Blaikieefc4eba2017-02-06 20:19:02 +0000859 // name of the topmost function in it.
Greg Claytonc8c10322016-12-13 18:25:19 +0000860 SmallVector<DWARFDie, 4> InlinedChain;
861 CU->getInlinedChainForAddress(Address, InlinedChain);
David Blaikieefc4eba2017-02-06 20:19:02 +0000862 if (InlinedChain.empty())
Alexey Samsonovd0109992014-04-18 21:36:39 +0000863 return false;
David Blaikieefc4eba2017-02-06 20:19:02 +0000864
865 const DWARFDie &DIE = InlinedChain[0];
866 bool FoundResult = false;
867 const char *Name = nullptr;
868 if (Kind != FunctionNameKind::None && (Name = DIE.getSubroutineName(Kind))) {
Alexey Samsonovd0109992014-04-18 21:36:39 +0000869 FunctionName = Name;
David Blaikieefc4eba2017-02-06 20:19:02 +0000870 FoundResult = true;
Alexey Samsonovd0109992014-04-18 21:36:39 +0000871 }
David Blaikieefc4eba2017-02-06 20:19:02 +0000872 if (auto DeclLineResult = DIE.getDeclLine()) {
873 StartLine = DeclLineResult;
874 FoundResult = true;
875 }
876
877 return FoundResult;
Alexey Samsonovd0109992014-04-18 21:36:39 +0000878}
879
Alexey Samsonov45be7932012-08-30 07:49:50 +0000880DILineInfo DWARFContext::getLineInfoForAddress(uint64_t Address,
Alexey Samsonovdce67342014-05-15 21:24:32 +0000881 DILineInfoSpecifier Spec) {
Alexey Samsonovd0109992014-04-18 21:36:39 +0000882 DILineInfo Result;
883
Alexey Samsonov45be7932012-08-30 07:49:50 +0000884 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
885 if (!CU)
Alexey Samsonovd0109992014-04-18 21:36:39 +0000886 return Result;
David Blaikieefc4eba2017-02-06 20:19:02 +0000887 getFunctionNameAndStartLineForAddress(CU, Address, Spec.FNKind,
888 Result.FunctionName,
889 Result.StartLine);
Alexey Samsonovdce67342014-05-15 21:24:32 +0000890 if (Spec.FLIKind != FileLineInfoKind::None) {
Frederic Riss101b5e22014-09-19 15:11:51 +0000891 if (const DWARFLineTable *LineTable = getLineTableForUnit(CU))
892 LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(),
893 Spec.FLIKind, Result);
Alexey Samsonovf4462fa2012-07-02 05:54:45 +0000894 }
Alexey Samsonovd0109992014-04-18 21:36:39 +0000895 return Result;
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000896}
David Blaikiea379b1812011-12-20 02:50:00 +0000897
Alexey Samsonovdce67342014-05-15 21:24:32 +0000898DILineInfoTable
899DWARFContext::getLineInfoForAddressRange(uint64_t Address, uint64_t Size,
900 DILineInfoSpecifier Spec) {
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000901 DILineInfoTable Lines;
902 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
903 if (!CU)
904 return Lines;
905
906 std::string FunctionName = "<invalid>";
David Blaikieefc4eba2017-02-06 20:19:02 +0000907 uint32_t StartLine = 0;
908 getFunctionNameAndStartLineForAddress(CU, Address, Spec.FNKind, FunctionName,
909 StartLine);
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000910
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000911 // If the Specifier says we don't need FileLineInfo, just
912 // return the top-most function at the starting address.
Alexey Samsonovdce67342014-05-15 21:24:32 +0000913 if (Spec.FLIKind == FileLineInfoKind::None) {
Alexey Samsonovd0109992014-04-18 21:36:39 +0000914 DILineInfo Result;
915 Result.FunctionName = FunctionName;
David Blaikieefc4eba2017-02-06 20:19:02 +0000916 Result.StartLine = StartLine;
Alexey Samsonovd0109992014-04-18 21:36:39 +0000917 Lines.push_back(std::make_pair(Address, Result));
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000918 return Lines;
919 }
920
Frederic Rissec8a5ba2014-09-04 06:14:40 +0000921 const DWARFLineTable *LineTable = getLineTableForUnit(CU);
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000922
923 // Get the index of row we're looking for in the line table.
924 std::vector<uint32_t> RowVector;
925 if (!LineTable->lookupAddressRange(Address, Size, RowVector))
926 return Lines;
927
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000928 for (uint32_t RowIndex : RowVector) {
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000929 // Take file number and line/column from the row.
930 const DWARFDebugLine::Row &Row = LineTable->Rows[RowIndex];
Alexey Samsonovd0109992014-04-18 21:36:39 +0000931 DILineInfo Result;
Frederic Riss101b5e22014-09-19 15:11:51 +0000932 LineTable->getFileNameByIndex(Row.File, CU->getCompilationDir(),
933 Spec.FLIKind, Result.FileName);
Alexey Samsonovd0109992014-04-18 21:36:39 +0000934 Result.FunctionName = FunctionName;
935 Result.Line = Row.Line;
936 Result.Column = Row.Column;
David Blaikieefc4eba2017-02-06 20:19:02 +0000937 Result.StartLine = StartLine;
Alexey Samsonovd0109992014-04-18 21:36:39 +0000938 Lines.push_back(std::make_pair(Row.Address, Result));
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000939 }
940
941 return Lines;
942}
943
Alexey Samsonovdce67342014-05-15 21:24:32 +0000944DIInliningInfo
945DWARFContext::getInliningInfoForAddress(uint64_t Address,
946 DILineInfoSpecifier Spec) {
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000947 DIInliningInfo InliningInfo;
948
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000949 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
950 if (!CU)
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000951 return InliningInfo;
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000952
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000953 const DWARFLineTable *LineTable = nullptr;
Greg Claytonc8c10322016-12-13 18:25:19 +0000954 SmallVector<DWARFDie, 4> InlinedChain;
955 CU->getInlinedChainForAddress(Address, InlinedChain);
956 if (InlinedChain.size() == 0) {
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000957 // If there is no DIE for address (e.g. it is in unavailable .dwo file),
958 // try to at least get file/line info from symbol table.
Alexey Samsonovdce67342014-05-15 21:24:32 +0000959 if (Spec.FLIKind != FileLineInfoKind::None) {
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000960 DILineInfo Frame;
Frederic Rissec8a5ba2014-09-04 06:14:40 +0000961 LineTable = getLineTableForUnit(CU);
Frederic Riss101b5e22014-09-19 15:11:51 +0000962 if (LineTable &&
963 LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(),
964 Spec.FLIKind, Frame))
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000965 InliningInfo.addFrame(Frame);
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000966 }
967 return InliningInfo;
968 }
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000969
Dehao Chenef700d52017-04-17 20:10:39 +0000970 uint32_t CallFile = 0, CallLine = 0, CallColumn = 0, CallDiscriminator = 0;
Greg Claytonc8c10322016-12-13 18:25:19 +0000971 for (uint32_t i = 0, n = InlinedChain.size(); i != n; i++) {
972 DWARFDie &FunctionDIE = InlinedChain[i];
Alexey Samsonovd0109992014-04-18 21:36:39 +0000973 DILineInfo Frame;
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000974 // Get function name if necessary.
Greg Claytonc8c10322016-12-13 18:25:19 +0000975 if (const char *Name = FunctionDIE.getSubroutineName(Spec.FNKind))
Alexey Samsonovdce67342014-05-15 21:24:32 +0000976 Frame.FunctionName = Name;
David Blaikieefc4eba2017-02-06 20:19:02 +0000977 if (auto DeclLineResult = FunctionDIE.getDeclLine())
978 Frame.StartLine = DeclLineResult;
Alexey Samsonovdce67342014-05-15 21:24:32 +0000979 if (Spec.FLIKind != FileLineInfoKind::None) {
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000980 if (i == 0) {
981 // For the topmost frame, initialize the line table of this
982 // compile unit and fetch file/line info from it.
Frederic Rissec8a5ba2014-09-04 06:14:40 +0000983 LineTable = getLineTableForUnit(CU);
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000984 // For the topmost routine, get file/line info from line table.
Frederic Riss101b5e22014-09-19 15:11:51 +0000985 if (LineTable)
986 LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(),
987 Spec.FLIKind, Frame);
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000988 } else {
989 // Otherwise, use call file, call line and call column from
990 // previous DIE in inlined chain.
Frederic Riss101b5e22014-09-19 15:11:51 +0000991 if (LineTable)
992 LineTable->getFileNameByIndex(CallFile, CU->getCompilationDir(),
993 Spec.FLIKind, Frame.FileName);
Alexey Samsonovd0109992014-04-18 21:36:39 +0000994 Frame.Line = CallLine;
995 Frame.Column = CallColumn;
Dehao Chenef700d52017-04-17 20:10:39 +0000996 Frame.Discriminator = CallDiscriminator;
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000997 }
998 // Get call file/line/column of a current DIE.
999 if (i + 1 < n) {
Dehao Chenef700d52017-04-17 20:10:39 +00001000 FunctionDIE.getCallerFrame(CallFile, CallLine, CallColumn,
1001 CallDiscriminator);
Alexey Samsonovc942e6b2012-09-04 08:12:33 +00001002 }
1003 }
Alexey Samsonovc942e6b2012-09-04 08:12:33 +00001004 InliningInfo.addFrame(Frame);
1005 }
1006 return InliningInfo;
1007}
1008
David Blaikief9803fb2017-05-23 00:30:42 +00001009std::shared_ptr<DWARFContext>
1010DWARFContext::getDWOContext(StringRef AbsolutePath) {
David Blaikie15d85fc2017-05-23 06:48:53 +00001011 if (auto S = DWP.lock()) {
David Blaikief9803fb2017-05-23 00:30:42 +00001012 DWARFContext *Ctxt = S->Context.get();
1013 return std::shared_ptr<DWARFContext>(std::move(S), Ctxt);
1014 }
1015
David Blaikie15d85fc2017-05-23 06:48:53 +00001016 std::weak_ptr<DWOFile> *Entry = &DWOFiles[AbsolutePath];
1017
1018 if (auto S = Entry->lock()) {
1019 DWARFContext *Ctxt = S->Context.get();
1020 return std::shared_ptr<DWARFContext>(std::move(S), Ctxt);
1021 }
1022
David Blaikie15d85fc2017-05-23 06:48:53 +00001023 Expected<OwningBinary<ObjectFile>> Obj = [&] {
1024 if (!CheckedForDWP) {
David Blaikiee5adb682017-07-30 01:34:08 +00001025 SmallString<128> DWPName;
1026 auto Obj = object::ObjectFile::createObjectFile(
1027 this->DWPName.empty()
1028 ? (DObj->getFileName() + ".dwp").toStringRef(DWPName)
1029 : StringRef(this->DWPName));
David Blaikie15d85fc2017-05-23 06:48:53 +00001030 if (Obj) {
1031 Entry = &DWP;
1032 return Obj;
1033 } else {
1034 CheckedForDWP = true;
1035 // TODO: Should this error be handled (maybe in a high verbosity mode)
1036 // before falling back to .dwo files?
1037 consumeError(Obj.takeError());
1038 }
1039 }
1040
1041 return object::ObjectFile::createObjectFile(AbsolutePath);
1042 }();
1043
David Blaikief9803fb2017-05-23 00:30:42 +00001044 if (!Obj) {
1045 // TODO: Actually report errors helpfully.
1046 consumeError(Obj.takeError());
1047 return nullptr;
1048 }
David Blaikie15d85fc2017-05-23 06:48:53 +00001049
1050 auto S = std::make_shared<DWOFile>();
David Blaikief9803fb2017-05-23 00:30:42 +00001051 S->File = std::move(Obj.get());
Rafael Espindolac398e672017-07-19 22:27:28 +00001052 S->Context = DWARFContext::create(*S->File.getBinary());
David Blaikie15d85fc2017-05-23 06:48:53 +00001053 *Entry = S;
David Blaikief9803fb2017-05-23 00:30:42 +00001054 auto *Ctxt = S->Context.get();
1055 return std::shared_ptr<DWARFContext>(std::move(S), Ctxt);
1056}
1057
George Rimar702dac62017-04-12 08:59:15 +00001058static Error createError(const Twine &Reason, llvm::Error E) {
1059 return make_error<StringError>(Reason + toString(std::move(E)),
1060 inconvertibleErrorCode());
1061}
1062
George Rimara25d3292017-05-27 18:10:23 +00001063/// SymInfo contains information about symbol: it's address
1064/// and section index which is -1LL for absolute symbols.
1065struct SymInfo {
1066 uint64_t Address;
1067 uint64_t SectionIndex;
1068};
1069
1070/// Returns the address of symbol relocation used against and a section index.
1071/// Used for futher relocations computation. Symbol's section load address is
1072static Expected<SymInfo> getSymbolInfo(const object::ObjectFile &Obj,
1073 const RelocationRef &Reloc,
1074 const LoadedObjectInfo *L,
1075 std::map<SymbolRef, SymInfo> &Cache) {
1076 SymInfo Ret = {0, (uint64_t)-1LL};
George Rimar702dac62017-04-12 08:59:15 +00001077 object::section_iterator RSec = Obj.section_end();
1078 object::symbol_iterator Sym = Reloc.getSymbol();
1079
George Rimara25d3292017-05-27 18:10:23 +00001080 std::map<SymbolRef, SymInfo>::iterator CacheIt = Cache.end();
George Rimar702dac62017-04-12 08:59:15 +00001081 // First calculate the address of the symbol or section as it appears
1082 // in the object file
1083 if (Sym != Obj.symbol_end()) {
George Rimar958b01a2017-05-15 11:45:28 +00001084 bool New;
George Rimara25d3292017-05-27 18:10:23 +00001085 std::tie(CacheIt, New) = Cache.insert({*Sym, {0, 0}});
George Rimar958b01a2017-05-15 11:45:28 +00001086 if (!New)
1087 return CacheIt->second;
1088
George Rimar702dac62017-04-12 08:59:15 +00001089 Expected<uint64_t> SymAddrOrErr = Sym->getAddress();
1090 if (!SymAddrOrErr)
George Rimar1af3cb22017-06-28 08:21:19 +00001091 return createError("failed to compute symbol address: ",
George Rimar702dac62017-04-12 08:59:15 +00001092 SymAddrOrErr.takeError());
1093
1094 // Also remember what section this symbol is in for later
1095 auto SectOrErr = Sym->getSection();
1096 if (!SectOrErr)
George Rimar1af3cb22017-06-28 08:21:19 +00001097 return createError("failed to get symbol section: ",
George Rimar702dac62017-04-12 08:59:15 +00001098 SectOrErr.takeError());
1099
1100 RSec = *SectOrErr;
George Rimara25d3292017-05-27 18:10:23 +00001101 Ret.Address = *SymAddrOrErr;
George Rimar702dac62017-04-12 08:59:15 +00001102 } else if (auto *MObj = dyn_cast<MachOObjectFile>(&Obj)) {
1103 RSec = MObj->getRelocationSection(Reloc.getRawDataRefImpl());
George Rimara25d3292017-05-27 18:10:23 +00001104 Ret.Address = RSec->getAddress();
George Rimar702dac62017-04-12 08:59:15 +00001105 }
1106
George Rimara25d3292017-05-27 18:10:23 +00001107 if (RSec != Obj.section_end())
1108 Ret.SectionIndex = RSec->getIndex();
1109
George Rimar702dac62017-04-12 08:59:15 +00001110 // If we are given load addresses for the sections, we need to adjust:
1111 // SymAddr = (Address of Symbol Or Section in File) -
1112 // (Address of Section in File) +
1113 // (Load Address of Section)
1114 // RSec is now either the section being targeted or the section
1115 // containing the symbol being targeted. In either case,
1116 // we need to perform the same computation.
1117 if (L && RSec != Obj.section_end())
1118 if (uint64_t SectionLoadAddress = L->getSectionLoadAddress(*RSec))
George Rimara25d3292017-05-27 18:10:23 +00001119 Ret.Address += SectionLoadAddress - RSec->getAddress();
George Rimar958b01a2017-05-15 11:45:28 +00001120
1121 if (CacheIt != Cache.end())
1122 CacheIt->second = Ret;
1123
George Rimar702dac62017-04-12 08:59:15 +00001124 return Ret;
1125}
1126
1127static bool isRelocScattered(const object::ObjectFile &Obj,
1128 const RelocationRef &Reloc) {
George Rimard4998b02017-04-13 09:52:50 +00001129 const MachOObjectFile *MachObj = dyn_cast<MachOObjectFile>(&Obj);
1130 if (!MachObj)
George Rimar702dac62017-04-12 08:59:15 +00001131 return false;
1132 // MachO also has relocations that point to sections and
1133 // scattered relocations.
George Rimar702dac62017-04-12 08:59:15 +00001134 auto RelocInfo = MachObj->getRelocation(Reloc.getRawDataRefImpl());
1135 return MachObj->isRelocationScattered(RelocInfo);
1136}
1137
Rafael Espindolac398e672017-07-19 22:27:28 +00001138ErrorPolicy DWARFContext::defaultErrorHandler(Error E) {
Jonas Devlieghere84e99262018-04-14 22:07:23 +00001139 WithColor::error() << toString(std::move(E)) << '\n';
George Rimar1af3cb22017-06-28 08:21:19 +00001140 return ErrorPolicy::Continue;
1141}
1142
Rafael Espindola87c3f4a92017-07-24 19:34:26 +00001143namespace {
1144struct DWARFSectionMap final : public DWARFSection {
1145 RelocAddrMap Relocs;
1146};
Rafael Espindola87c3f4a92017-07-24 19:34:26 +00001147
Rafael Espindolac398e672017-07-19 22:27:28 +00001148class DWARFObjInMemory final : public DWARFObject {
1149 bool IsLittleEndian;
1150 uint8_t AddressSize;
1151 StringRef FileName;
George Rimar6957ab52017-08-15 12:32:54 +00001152 const object::ObjectFile *Obj = nullptr;
George Rimare1c30f72017-08-15 15:54:43 +00001153 std::vector<SectionName> SectionNames;
Keno Fischerc780e8e2015-05-21 21:24:32 +00001154
Rafael Espindolac398e672017-07-19 22:27:28 +00001155 using TypeSectionMap = MapVector<object::SectionRef, DWARFSectionMap,
1156 std::map<object::SectionRef, unsigned>>;
Eric Christopher7370b552012-11-12 21:40:38 +00001157
Rafael Espindolac398e672017-07-19 22:27:28 +00001158 TypeSectionMap TypesSections;
1159 TypeSectionMap TypesDWOSections;
1160
1161 DWARFSectionMap InfoSection;
1162 DWARFSectionMap LocSection;
1163 DWARFSectionMap LineSection;
1164 DWARFSectionMap RangeSection;
James Henderson3fcc7452018-02-02 12:35:52 +00001165 DWARFSectionMap RnglistsSection;
Rafael Espindolac398e672017-07-19 22:27:28 +00001166 DWARFSectionMap StringOffsetSection;
1167 DWARFSectionMap InfoDWOSection;
1168 DWARFSectionMap LineDWOSection;
1169 DWARFSectionMap LocDWOSection;
1170 DWARFSectionMap StringOffsetDWOSection;
1171 DWARFSectionMap RangeDWOSection;
1172 DWARFSectionMap AddrSection;
1173 DWARFSectionMap AppleNamesSection;
1174 DWARFSectionMap AppleTypesSection;
1175 DWARFSectionMap AppleNamespacesSection;
1176 DWARFSectionMap AppleObjCSection;
Pavel Labath3c9a9182018-01-29 11:08:32 +00001177 DWARFSectionMap DebugNamesSection;
Rafael Espindolac398e672017-07-19 22:27:28 +00001178
1179 DWARFSectionMap *mapNameToDWARFSection(StringRef Name) {
1180 return StringSwitch<DWARFSectionMap *>(Name)
1181 .Case("debug_info", &InfoSection)
1182 .Case("debug_loc", &LocSection)
1183 .Case("debug_line", &LineSection)
1184 .Case("debug_str_offsets", &StringOffsetSection)
1185 .Case("debug_ranges", &RangeSection)
James Henderson3fcc7452018-02-02 12:35:52 +00001186 .Case("debug_rnglists", &RnglistsSection)
Rafael Espindolac398e672017-07-19 22:27:28 +00001187 .Case("debug_info.dwo", &InfoDWOSection)
1188 .Case("debug_loc.dwo", &LocDWOSection)
1189 .Case("debug_line.dwo", &LineDWOSection)
Pavel Labath3c9a9182018-01-29 11:08:32 +00001190 .Case("debug_names", &DebugNamesSection)
Rafael Espindolac398e672017-07-19 22:27:28 +00001191 .Case("debug_str_offsets.dwo", &StringOffsetDWOSection)
1192 .Case("debug_addr", &AddrSection)
1193 .Case("apple_names", &AppleNamesSection)
1194 .Case("apple_types", &AppleTypesSection)
1195 .Case("apple_namespaces", &AppleNamespacesSection)
1196 .Case("apple_namespac", &AppleNamespacesSection)
1197 .Case("apple_objc", &AppleObjCSection)
1198 .Default(nullptr);
1199 }
1200
1201 StringRef AbbrevSection;
1202 StringRef ARangeSection;
1203 StringRef DebugFrameSection;
1204 StringRef EHFrameSection;
1205 StringRef StringSection;
1206 StringRef MacinfoSection;
1207 StringRef PubNamesSection;
1208 StringRef PubTypesSection;
1209 StringRef GnuPubNamesSection;
1210 StringRef AbbrevDWOSection;
1211 StringRef StringDWOSection;
1212 StringRef GnuPubTypesSection;
1213 StringRef CUIndexSection;
1214 StringRef GdbIndexSection;
1215 StringRef TUIndexSection;
Paul Robinsonb6aa01c2018-01-25 22:02:36 +00001216 StringRef LineStringSection;
Rafael Espindolac398e672017-07-19 22:27:28 +00001217
1218 SmallVector<SmallString<32>, 4> UncompressedSections;
1219
1220 StringRef *mapSectionToMember(StringRef Name) {
1221 if (DWARFSection *Sec = mapNameToDWARFSection(Name))
1222 return &Sec->Data;
1223 return StringSwitch<StringRef *>(Name)
1224 .Case("debug_abbrev", &AbbrevSection)
1225 .Case("debug_aranges", &ARangeSection)
1226 .Case("debug_frame", &DebugFrameSection)
1227 .Case("eh_frame", &EHFrameSection)
1228 .Case("debug_str", &StringSection)
1229 .Case("debug_macinfo", &MacinfoSection)
1230 .Case("debug_pubnames", &PubNamesSection)
1231 .Case("debug_pubtypes", &PubTypesSection)
1232 .Case("debug_gnu_pubnames", &GnuPubNamesSection)
1233 .Case("debug_gnu_pubtypes", &GnuPubTypesSection)
1234 .Case("debug_abbrev.dwo", &AbbrevDWOSection)
1235 .Case("debug_str.dwo", &StringDWOSection)
1236 .Case("debug_cu_index", &CUIndexSection)
1237 .Case("debug_tu_index", &TUIndexSection)
1238 .Case("gdb_index", &GdbIndexSection)
Paul Robinsonb6aa01c2018-01-25 22:02:36 +00001239 .Case("debug_line_str", &LineStringSection)
Rafael Espindolac398e672017-07-19 22:27:28 +00001240 // Any more debug info sections go here.
1241 .Default(nullptr);
1242 }
1243
1244 /// If Sec is compressed section, decompresses and updates its contents
1245 /// provided by Data. Otherwise leaves it unchanged.
1246 Error maybeDecompress(const object::SectionRef &Sec, StringRef Name,
1247 StringRef &Data) {
1248 if (!Decompressor::isCompressed(Sec))
1249 return Error::success();
1250
1251 Expected<Decompressor> Decompressor =
1252 Decompressor::create(Name, Data, IsLittleEndian, AddressSize == 8);
1253 if (!Decompressor)
1254 return Decompressor.takeError();
1255
1256 SmallString<32> Out;
1257 if (auto Err = Decompressor->resizeAndDecompress(Out))
1258 return Err;
1259
1260 UncompressedSections.emplace_back(std::move(Out));
1261 Data = UncompressedSections.back();
1262
1263 return Error::success();
1264 }
1265
1266public:
1267 DWARFObjInMemory(const StringMap<std::unique_ptr<MemoryBuffer>> &Sections,
1268 uint8_t AddrSize, bool IsLittleEndian)
1269 : IsLittleEndian(IsLittleEndian) {
1270 for (const auto &SecIt : Sections) {
1271 if (StringRef *SectionData = mapSectionToMember(SecIt.first()))
1272 *SectionData = SecIt.second->getBuffer();
Alexey Samsonov068fc8a2013-04-23 10:17:34 +00001273 }
Rafael Espindolac398e672017-07-19 22:27:28 +00001274 }
1275 DWARFObjInMemory(const object::ObjectFile &Obj, const LoadedObjectInfo *L,
1276 function_ref<ErrorPolicy(Error)> HandleError)
1277 : IsLittleEndian(Obj.isLittleEndian()),
George Rimar6957ab52017-08-15 12:32:54 +00001278 AddressSize(Obj.getBytesInAddress()), FileName(Obj.getFileName()),
1279 Obj(&Obj) {
Alexey Samsonov068fc8a2013-04-23 10:17:34 +00001280
George Rimare1c30f72017-08-15 15:54:43 +00001281 StringMap<unsigned> SectionAmountMap;
Rafael Espindolac398e672017-07-19 22:27:28 +00001282 for (const SectionRef &Section : Obj.sections()) {
1283 StringRef Name;
1284 Section.getName(Name);
George Rimare1c30f72017-08-15 15:54:43 +00001285 ++SectionAmountMap[Name];
1286 SectionNames.push_back({ Name, true });
1287
Rafael Espindolac398e672017-07-19 22:27:28 +00001288 // Skip BSS and Virtual sections, they aren't interesting.
1289 if (Section.isBSS() || Section.isVirtual())
George Rimarfed9f092017-05-17 12:10:51 +00001290 continue;
Keno Fischerc780e8e2015-05-21 21:24:32 +00001291
Jonas Devlieghere8af23872017-09-26 14:22:35 +00001292 // Skip sections stripped by dsymutil.
1293 if (Section.isStripped())
1294 continue;
1295
Rafael Espindolac398e672017-07-19 22:27:28 +00001296 StringRef Data;
1297 section_iterator RelocatedSection = Section.getRelocatedSection();
1298 // Try to obtain an already relocated version of this section.
1299 // Else use the unrelocated section from the object file. We'll have to
1300 // apply relocations ourselves later.
1301 if (!L || !L->getLoadedSectionContents(*RelocatedSection, Data))
1302 Section.getContents(Data);
George Rimarfed9f092017-05-17 12:10:51 +00001303
Rafael Espindolac398e672017-07-19 22:27:28 +00001304 if (auto Err = maybeDecompress(Section, Name, Data)) {
1305 ErrorPolicy EP = HandleError(createError(
1306 "failed to decompress '" + Name + "', ", std::move(Err)));
George Rimar1af3cb22017-06-28 08:21:19 +00001307 if (EP == ErrorPolicy::Halt)
1308 return;
George Rimarfed9f092017-05-17 12:10:51 +00001309 continue;
1310 }
Rafael Espindolac398e672017-07-19 22:27:28 +00001311
1312 // Compressed sections names in GNU style starts from ".z",
1313 // at this point section is decompressed and we drop compression prefix.
1314 Name = Name.substr(
1315 Name.find_first_not_of("._z")); // Skip ".", "z" and "_" prefixes.
1316
1317 // Map platform specific debug section names to DWARF standard section
1318 // names.
1319 Name = Obj.mapDebugSectionName(Name);
1320
1321 if (StringRef *SectionData = mapSectionToMember(Name)) {
1322 *SectionData = Data;
1323 if (Name == "debug_ranges") {
1324 // FIXME: Use the other dwo range section when we emit it.
1325 RangeDWOSection.Data = Data;
1326 }
1327 } else if (Name == "debug_types") {
1328 // Find debug_types data by section rather than name as there are
1329 // multiple, comdat grouped, debug_types sections.
1330 TypesSections[Section].Data = Data;
1331 } else if (Name == "debug_types.dwo") {
1332 TypesDWOSections[Section].Data = Data;
1333 }
1334
1335 if (RelocatedSection == Obj.section_end())
1336 continue;
1337
1338 StringRef RelSecName;
1339 StringRef RelSecData;
1340 RelocatedSection->getName(RelSecName);
1341
1342 // If the section we're relocating was relocated already by the JIT,
1343 // then we used the relocated version above, so we do not need to process
1344 // relocations for it now.
1345 if (L && L->getLoadedSectionContents(*RelocatedSection, RelSecData))
1346 continue;
1347
1348 // In Mach-o files, the relocations do not need to be applied if
1349 // there is no load offset to apply. The value read at the
1350 // relocation point already factors in the section address
1351 // (actually applying the relocations will produce wrong results
1352 // as the section address will be added twice).
1353 if (!L && isa<MachOObjectFile>(&Obj))
1354 continue;
1355
1356 RelSecName = RelSecName.substr(
1357 RelSecName.find_first_not_of("._z")); // Skip . and _ prefixes.
1358
1359 // TODO: Add support for relocations in other sections as needed.
1360 // Record relocations for the debug_info and debug_line sections.
1361 DWARFSectionMap *Sec = mapNameToDWARFSection(RelSecName);
1362 RelocAddrMap *Map = Sec ? &Sec->Relocs : nullptr;
1363 if (!Map) {
1364 // Find debug_types relocs by section rather than name as there are
1365 // multiple, comdat grouped, debug_types sections.
1366 if (RelSecName == "debug_types")
1367 Map =
1368 &static_cast<DWARFSectionMap &>(TypesSections[*RelocatedSection])
1369 .Relocs;
1370 else if (RelSecName == "debug_types.dwo")
1371 Map = &static_cast<DWARFSectionMap &>(
1372 TypesDWOSections[*RelocatedSection])
1373 .Relocs;
1374 else
1375 continue;
1376 }
1377
1378 if (Section.relocation_begin() == Section.relocation_end())
1379 continue;
1380
1381 // Symbol to [address, section index] cache mapping.
1382 std::map<SymbolRef, SymInfo> AddrCache;
1383 for (const RelocationRef &Reloc : Section.relocations()) {
1384 // FIXME: it's not clear how to correctly handle scattered
1385 // relocations.
1386 if (isRelocScattered(Obj, Reloc))
1387 continue;
1388
1389 Expected<SymInfo> SymInfoOrErr =
1390 getSymbolInfo(Obj, Reloc, L, AddrCache);
1391 if (!SymInfoOrErr) {
1392 if (HandleError(SymInfoOrErr.takeError()) == ErrorPolicy::Halt)
1393 return;
1394 continue;
1395 }
1396
1397 object::RelocVisitor V(Obj);
1398 uint64_t Val = V.visit(Reloc.getType(), Reloc, SymInfoOrErr->Address);
1399 if (V.error()) {
1400 SmallString<32> Type;
1401 Reloc.getTypeName(Type);
1402 ErrorPolicy EP = HandleError(
1403 createError("failed to compute relocation: " + Type + ", ",
1404 errorCodeToError(object_error::parse_failed)));
1405 if (EP == ErrorPolicy::Halt)
1406 return;
1407 continue;
1408 }
1409 RelocAddrEntry Rel = {SymInfoOrErr->SectionIndex, Val};
1410 Map->insert({Reloc.getOffset(), Rel});
1411 }
Eric Christopher7370b552012-11-12 21:40:38 +00001412 }
George Rimare1c30f72017-08-15 15:54:43 +00001413
1414 for (SectionName &S : SectionNames)
1415 if (SectionAmountMap[S.Name] > 1)
1416 S.IsNameUnique = false;
Eric Christopher7370b552012-11-12 21:40:38 +00001417 }
Eric Christopher7370b552012-11-12 21:40:38 +00001418
Rafael Espindolac398e672017-07-19 22:27:28 +00001419 Optional<RelocAddrEntry> find(const DWARFSection &S,
1420 uint64_t Pos) const override {
1421 auto &Sec = static_cast<const DWARFSectionMap &>(S);
1422 RelocAddrMap::const_iterator AI = Sec.Relocs.find(Pos);
1423 if (AI == Sec.Relocs.end())
1424 return None;
1425 return AI->second;
Chris Bieneman2e752db2017-01-20 19:03:14 +00001426 }
Rafael Espindolac398e672017-07-19 22:27:28 +00001427
George Rimar6957ab52017-08-15 12:32:54 +00001428 const object::ObjectFile *getFile() const override { return Obj; }
1429
George Rimare1c30f72017-08-15 15:54:43 +00001430 ArrayRef<SectionName> getSectionNames() const override {
1431 return SectionNames;
1432 }
1433
Rafael Espindolac398e672017-07-19 22:27:28 +00001434 bool isLittleEndian() const override { return IsLittleEndian; }
1435 StringRef getAbbrevDWOSection() const override { return AbbrevDWOSection; }
1436 const DWARFSection &getLineDWOSection() const override {
1437 return LineDWOSection;
1438 }
1439 const DWARFSection &getLocDWOSection() const override {
1440 return LocDWOSection;
1441 }
1442 StringRef getStringDWOSection() const override { return StringDWOSection; }
1443 const DWARFSection &getStringOffsetDWOSection() const override {
1444 return StringOffsetDWOSection;
1445 }
1446 const DWARFSection &getRangeDWOSection() const override {
1447 return RangeDWOSection;
1448 }
1449 const DWARFSection &getAddrSection() const override { return AddrSection; }
1450 StringRef getCUIndexSection() const override { return CUIndexSection; }
1451 StringRef getGdbIndexSection() const override { return GdbIndexSection; }
1452 StringRef getTUIndexSection() const override { return TUIndexSection; }
1453
1454 // DWARF v5
1455 const DWARFSection &getStringOffsetSection() const override {
1456 return StringOffsetSection;
1457 }
Paul Robinsonb6aa01c2018-01-25 22:02:36 +00001458 StringRef getLineStringSection() const override { return LineStringSection; }
Rafael Espindolac398e672017-07-19 22:27:28 +00001459
1460 // Sections for DWARF5 split dwarf proposal.
1461 const DWARFSection &getInfoDWOSection() const override {
1462 return InfoDWOSection;
1463 }
1464 void forEachTypesDWOSections(
1465 function_ref<void(const DWARFSection &)> F) const override {
1466 for (auto &P : TypesDWOSections)
1467 F(P.second);
1468 }
1469
1470 StringRef getAbbrevSection() const override { return AbbrevSection; }
1471 const DWARFSection &getLocSection() const override { return LocSection; }
1472 StringRef getARangeSection() const override { return ARangeSection; }
1473 StringRef getDebugFrameSection() const override { return DebugFrameSection; }
1474 StringRef getEHFrameSection() const override { return EHFrameSection; }
1475 const DWARFSection &getLineSection() const override { return LineSection; }
1476 StringRef getStringSection() const override { return StringSection; }
1477 const DWARFSection &getRangeSection() const override { return RangeSection; }
James Henderson3fcc7452018-02-02 12:35:52 +00001478 const DWARFSection &getRnglistsSection() const override {
1479 return RnglistsSection;
1480 }
Rafael Espindolac398e672017-07-19 22:27:28 +00001481 StringRef getMacinfoSection() const override { return MacinfoSection; }
1482 StringRef getPubNamesSection() const override { return PubNamesSection; }
1483 StringRef getPubTypesSection() const override { return PubTypesSection; }
1484 StringRef getGnuPubNamesSection() const override {
1485 return GnuPubNamesSection;
1486 }
1487 StringRef getGnuPubTypesSection() const override {
1488 return GnuPubTypesSection;
1489 }
1490 const DWARFSection &getAppleNamesSection() const override {
1491 return AppleNamesSection;
1492 }
1493 const DWARFSection &getAppleTypesSection() const override {
1494 return AppleTypesSection;
1495 }
1496 const DWARFSection &getAppleNamespacesSection() const override {
1497 return AppleNamespacesSection;
1498 }
1499 const DWARFSection &getAppleObjCSection() const override {
1500 return AppleObjCSection;
1501 }
Pavel Labath3c9a9182018-01-29 11:08:32 +00001502 const DWARFSection &getDebugNamesSection() const override {
1503 return DebugNamesSection;
1504 }
Rafael Espindolac398e672017-07-19 22:27:28 +00001505
1506 StringRef getFileName() const override { return FileName; }
1507 uint8_t getAddressSize() const override { return AddressSize; }
1508 const DWARFSection &getInfoSection() const override { return InfoSection; }
1509 void forEachTypesSections(
1510 function_ref<void(const DWARFSection &)> F) const override {
1511 for (auto &P : TypesSections)
1512 F(P.second);
1513 }
1514};
Benjamin Kramer49a49fe2017-08-20 13:03:48 +00001515} // namespace
Rafael Espindolac398e672017-07-19 22:27:28 +00001516
1517std::unique_ptr<DWARFContext>
1518DWARFContext::create(const object::ObjectFile &Obj, const LoadedObjectInfo *L,
David Blaikiee5adb682017-07-30 01:34:08 +00001519 function_ref<ErrorPolicy(Error)> HandleError,
1520 std::string DWPName) {
Reid Kleckner388f8802017-07-19 23:42:53 +00001521 auto DObj = llvm::make_unique<DWARFObjInMemory>(Obj, L, HandleError);
David Blaikiee5adb682017-07-30 01:34:08 +00001522 return llvm::make_unique<DWARFContext>(std::move(DObj), std::move(DWPName));
Chris Bieneman2e752db2017-01-20 19:03:14 +00001523}
1524
Rafael Espindolac398e672017-07-19 22:27:28 +00001525std::unique_ptr<DWARFContext>
1526DWARFContext::create(const StringMap<std::unique_ptr<MemoryBuffer>> &Sections,
1527 uint8_t AddrSize, bool isLittleEndian) {
Reid Kleckner388f8802017-07-19 23:42:53 +00001528 auto DObj =
1529 llvm::make_unique<DWARFObjInMemory>(Sections, AddrSize, isLittleEndian);
David Blaikiee5adb682017-07-30 01:34:08 +00001530 return llvm::make_unique<DWARFContext>(std::move(DObj), "");
Rafael Espindola77e87b32017-07-07 05:36:53 +00001531}
Reid Klecknera0587362017-08-29 21:41:21 +00001532
1533Error DWARFContext::loadRegisterInfo(const object::ObjectFile &Obj) {
1534 // Detect the architecture from the object file. We usually don't need OS
1535 // info to lookup a target and create register info.
1536 Triple TT;
1537 TT.setArch(Triple::ArchType(Obj.getArch()));
1538 TT.setVendor(Triple::UnknownVendor);
1539 TT.setOS(Triple::UnknownOS);
1540 std::string TargetLookupError;
1541 const Target *TheTarget =
1542 TargetRegistry::lookupTarget(TT.str(), TargetLookupError);
1543 if (!TargetLookupError.empty())
1544 return make_error<StringError>(TargetLookupError, inconvertibleErrorCode());
1545 RegInfo.reset(TheTarget->createMCRegInfo(TT.str()));
1546 return Error::success();
1547}