blob: 8488fe8ced7b623290d8adb37179e86d28e4a3f8 [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
Adrian Prantl057d3362017-09-15 23:04:04 +0000251void DWARFContext::dump(
252 raw_ostream &OS, DIDumpOptions DumpOpts,
253 std::array<Optional<uint64_t>, DIDT_ID_Count> DumpOffsets) {
254
255 Optional<uint64_t> DumpOffset;
Adrian Prantl7bc1b282017-09-11 22:59:45 +0000256 uint64_t DumpType = DumpOpts.DumpType;
Adrian Prantlf4bc1f72017-06-01 18:18:23 +0000257
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000258 StringRef Extension = sys::path::extension(DObj->getFileName());
259 bool IsDWO = (Extension == ".dwo") || (Extension == ".dwp");
260
261 // Print UUID header.
Adrian Prantl3dcd1222017-09-13 18:22:59 +0000262 const auto *ObjFile = DObj->getFile();
Adrian Prantl3dcd1222017-09-13 18:22:59 +0000263 if (DumpType & DIDT_UUID)
264 dumpUUID(OS, *ObjFile);
265
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000266 // Print a header for each explicitly-requested section.
267 // Otherwise just print one for non-empty sections.
Adrian Prantl057d3362017-09-15 23:04:04 +0000268 // Only print empty .dwo section headers when dumping a .dwo file.
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000269 bool Explicit = DumpType != DIDT_All && !IsDWO;
Adrian Prantl057d3362017-09-15 23:04:04 +0000270 bool ExplicitDWO = Explicit && IsDWO;
271 auto shouldDump = [&](bool Explicit, const char *Name, unsigned ID,
272 StringRef Section) {
273 DumpOffset = DumpOffsets[ID];
274 unsigned Mask = 1U << ID;
275 bool Should = (DumpType & Mask) && (Explicit || !Section.empty());
Adrian Prantl84168022017-09-15 17:39:50 +0000276 if (Should)
Adrian Prantl057d3362017-09-15 23:04:04 +0000277 OS << "\n" << Name << " contents:\n";
Adrian Prantl84168022017-09-15 17:39:50 +0000278 return Should;
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000279 };
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000280
281 // Dump individual sections.
Adrian Prantl057d3362017-09-15 23:04:04 +0000282 if (shouldDump(Explicit, ".debug_abbrev", DIDT_ID_DebugAbbrev,
283 DObj->getAbbrevSection()))
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000284 getDebugAbbrev()->dump(OS);
Adrian Prantl057d3362017-09-15 23:04:04 +0000285 if (shouldDump(ExplicitDWO, ".debug_abbrev.dwo", DIDT_ID_DebugAbbrev,
286 DObj->getAbbrevDWOSection()))
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000287 getDebugAbbrevDWO()->dump(OS);
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000288
Adrian Prantl057d3362017-09-15 23:04:04 +0000289 auto dumpDebugInfo = [&](bool IsExplicit, const char *Name,
290 DWARFSection Section, cu_iterator_range CUs) {
291 if (shouldDump(IsExplicit, Name, DIDT_ID_DebugInfo, Section.Data)) {
Adrian Prantlc8d86532017-09-18 21:44:40 +0000292 if (DumpOffset)
Adrian Prantld3f9f212017-09-20 17:44:00 +0000293 getDIEForOffset(DumpOffset.getValue())
294 .dump(OS, 0, DumpOpts.noImplicitRecursion());
Adrian Prantlc8d86532017-09-18 21:44:40 +0000295 else
296 for (const auto &CU : CUs)
Adrian Prantl057d3362017-09-15 23:04:04 +0000297 CU->dump(OS, DumpOpts);
298 }
299 };
300 dumpDebugInfo(Explicit, ".debug_info", DObj->getInfoSection(),
301 compile_units());
302 dumpDebugInfo(ExplicitDWO, ".debug_info.dwo", DObj->getInfoDWOSection(),
303 dwo_compile_units());
David Blaikie622dce42014-01-08 23:29:59 +0000304
Adrian Prantl099d7e42017-09-16 16:58:18 +0000305 auto dumpDebugType = [&](const char *Name,
306 tu_section_iterator_range TUSections) {
307 OS << '\n' << Name << " contents:\n";
308 DumpOffset = DumpOffsets[DIDT_ID_DebugTypes];
309 for (const auto &TUS : TUSections)
310 for (const auto &TU : TUS)
311 if (DumpOffset)
Adrian Prantld3f9f212017-09-20 17:44:00 +0000312 TU->getDIEForOffset(*DumpOffset)
313 .dump(OS, 0, DumpOpts.noImplicitRecursion());
Adrian Prantl099d7e42017-09-16 16:58:18 +0000314 else
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000315 TU->dump(OS, DumpOpts);
Adrian Prantl099d7e42017-09-16 16:58:18 +0000316 };
317 if ((DumpType & DIDT_DebugTypes)) {
318 if (Explicit || getNumTypeUnits())
319 dumpDebugType(".debug_types", type_unit_sections());
320 if (ExplicitDWO || getNumDWOTypeUnits())
321 dumpDebugType(".debug_types.dwo", dwo_type_unit_sections());
David Blaikie03c089c2013-09-23 22:44:47 +0000322 }
323
Adrian Prantl057d3362017-09-15 23:04:04 +0000324 if (shouldDump(Explicit, ".debug_loc", DIDT_ID_DebugLoc,
Adrian Prantl84168022017-09-15 17:39:50 +0000325 DObj->getLocSection().Data)) {
Jonas Devlieghere622c5632017-09-27 09:33:36 +0000326 getDebugLoc()->dump(OS, getRegisterInfo(), DumpOffset);
David Blaikie18e73502013-06-19 21:37:13 +0000327 }
Adrian Prantl057d3362017-09-15 23:04:04 +0000328 if (shouldDump(ExplicitDWO, ".debug_loc.dwo", DIDT_ID_DebugLoc,
Adrian Prantl84168022017-09-15 17:39:50 +0000329 DObj->getLocDWOSection().Data)) {
Jonas Devlieghere622c5632017-09-27 09:33:36 +0000330 getDebugLocDWO()->dump(OS, getRegisterInfo(), DumpOffset);
David Blaikie9c550ac2014-03-25 01:44:02 +0000331 }
332
Jonas Devliegherec0a758d2017-09-18 14:15:57 +0000333 if (shouldDump(Explicit, ".debug_frame", DIDT_ID_DebugFrame,
Adrian Prantl62528e62017-09-21 18:52:03 +0000334 DObj->getDebugFrameSection()))
Rafael Auler86fb7bf2018-03-08 00:46:53 +0000335 getDebugFrame()->dump(OS, getRegisterInfo(), DumpOffset);
Jonas Devliegherec0a758d2017-09-18 14:15:57 +0000336
337 if (shouldDump(Explicit, ".eh_frame", DIDT_ID_DebugFrame,
Adrian Prantl62528e62017-09-21 18:52:03 +0000338 DObj->getEHFrameSection()))
Rafael Auler86fb7bf2018-03-08 00:46:53 +0000339 getEHFrame()->dump(OS, getRegisterInfo(), DumpOffset);
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000340
Adrian Prantl7bc1b282017-09-11 22:59:45 +0000341 if (DumpType & DIDT_DebugMacro) {
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000342 if (Explicit || !getDebugMacro()->empty()) {
343 OS << "\n.debug_macinfo contents:\n";
344 getDebugMacro()->dump(OS);
345 }
Amjad Aboude59cc3e2015-11-12 09:38:54 +0000346 }
347
Adrian Prantl057d3362017-09-15 23:04:04 +0000348 if (shouldDump(Explicit, ".debug_aranges", DIDT_ID_DebugAranges,
Adrian Prantl84168022017-09-15 17:39:50 +0000349 DObj->getARangeSection())) {
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000350 uint32_t offset = 0;
Rafael Espindolac398e672017-07-19 22:27:28 +0000351 DataExtractor arangesData(DObj->getARangeSection(), isLittleEndian(), 0);
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000352 DWARFDebugArangeSet set;
353 while (set.extract(arangesData, &offset))
354 set.dump(OS);
355 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000356
James Hendersona3acf992018-05-10 10:51:33 +0000357 auto DumpLineSection = [&](DWARFDebugLine::SectionParser Parser,
358 DIDumpOptions DumpOpts) {
359 while (!Parser.done()) {
360 if (DumpOffset && Parser.getOffset() != *DumpOffset) {
361 Parser.skip();
Alexey Samsonov7a18c062015-05-19 21:54:32 +0000362 continue;
Paul Robinson63811a42017-11-22 15:33:17 +0000363 }
James Hendersona3acf992018-05-10 10:51:33 +0000364 OS << "debug_line[" << format("0x%8.8x", Parser.getOffset()) << "]\n";
Paul Robinson63811a42017-11-22 15:33:17 +0000365 if (DumpOpts.Verbose) {
James Hendersona3acf992018-05-10 10:51:33 +0000366 Parser.parseNext(DWARFDebugLine::warn, DWARFDebugLine::warnForError,
367 &OS);
Paul Robinson63811a42017-11-22 15:33:17 +0000368 } else {
James Hendersona3acf992018-05-10 10:51:33 +0000369 DWARFDebugLine::LineTable LineTable = Parser.parseNext();
370 LineTable.dump(OS, DumpOpts);
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000371 }
Benjamin Kramer6dda0322011-09-15 18:02:20 +0000372 }
James Hendersona3acf992018-05-10 10:51:33 +0000373 };
374
375 if (shouldDump(Explicit, ".debug_line", DIDT_ID_DebugLine,
376 DObj->getLineSection().Data)) {
377 DWARFDataExtractor LineData(*DObj, DObj->getLineSection(), isLittleEndian(),
378 0);
379 DWARFDebugLine::SectionParser Parser(LineData, *this, compile_units(),
380 type_unit_sections());
381 DumpLineSection(Parser, DumpOpts);
Benjamin Kramer6dda0322011-09-15 18:02:20 +0000382 }
Benjamin Kramer07d4b1c2011-09-15 16:57:13 +0000383
Adrian Prantl057d3362017-09-15 23:04:04 +0000384 if (shouldDump(ExplicitDWO, ".debug_line.dwo", DIDT_ID_DebugLine,
Adrian Prantl84168022017-09-15 17:39:50 +0000385 DObj->getLineDWOSection().Data)) {
Paul Robinson63811a42017-11-22 15:33:17 +0000386 DWARFDataExtractor LineData(*DObj, DObj->getLineDWOSection(),
387 isLittleEndian(), 0);
James Hendersona3acf992018-05-10 10:51:33 +0000388 DWARFDebugLine::SectionParser Parser(LineData, *this, dwo_compile_units(),
389 dwo_type_unit_sections());
390 DumpLineSection(Parser, DumpOpts);
David Blaikie1d4736e2014-02-24 23:58:54 +0000391 }
392
Adrian Prantl057d3362017-09-15 23:04:04 +0000393 if (shouldDump(Explicit, ".debug_cu_index", DIDT_ID_DebugCUIndex,
Adrian Prantl84168022017-09-15 17:39:50 +0000394 DObj->getCUIndexSection())) {
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000395 getCUIndex().dump(OS);
396 }
397
Adrian Prantl057d3362017-09-15 23:04:04 +0000398 if (shouldDump(Explicit, ".debug_tu_index", DIDT_ID_DebugTUIndex,
Adrian Prantl84168022017-09-15 17:39:50 +0000399 DObj->getTUIndexSection())) {
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000400 getTUIndex().dump(OS);
401 }
402
Adrian Prantl057d3362017-09-15 23:04:04 +0000403 if (shouldDump(Explicit, ".debug_str", DIDT_ID_DebugStr,
Adrian Prantl84168022017-09-15 17:39:50 +0000404 DObj->getStringSection())) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000405 DataExtractor strData(DObj->getStringSection(), isLittleEndian(), 0);
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000406 uint32_t offset = 0;
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000407 uint32_t strOffset = 0;
408 while (const char *s = strData.getCStr(&offset)) {
409 OS << format("0x%8.8x: \"%s\"\n", strOffset, s);
410 strOffset = offset;
411 }
Benjamin Kramer07d4b1c2011-09-15 16:57:13 +0000412 }
Adrian Prantl057d3362017-09-15 23:04:04 +0000413 if (shouldDump(ExplicitDWO, ".debug_str.dwo", DIDT_ID_DebugStr,
Adrian Prantl84168022017-09-15 17:39:50 +0000414 DObj->getStringDWOSection())) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000415 DataExtractor strDWOData(DObj->getStringDWOSection(), isLittleEndian(), 0);
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000416 uint32_t offset = 0;
David Blaikie66865d62014-01-09 00:13:35 +0000417 uint32_t strDWOOffset = 0;
418 while (const char *s = strDWOData.getCStr(&offset)) {
419 OS << format("0x%8.8x: \"%s\"\n", strDWOOffset, s);
420 strDWOOffset = offset;
David Blaikie622dce42014-01-08 23:29:59 +0000421 }
David Blaikie66865d62014-01-09 00:13:35 +0000422 }
Paul Robinsonb6aa01c2018-01-25 22:02:36 +0000423 if (shouldDump(Explicit, ".debug_line_str", DIDT_ID_DebugLineStr,
424 DObj->getLineStringSection())) {
425 DataExtractor strData(DObj->getLineStringSection(), isLittleEndian(), 0);
426 uint32_t offset = 0;
427 uint32_t strOffset = 0;
428 while (const char *s = strData.getCStr(&offset)) {
Scott Linder16c7bda2018-02-23 23:01:06 +0000429 OS << format("0x%8.8x: \"", strOffset);
430 OS.write_escaped(s);
431 OS << "\"\n";
Paul Robinsonb6aa01c2018-01-25 22:02:36 +0000432 strOffset = offset;
433 }
434 }
David Blaikie622dce42014-01-08 23:29:59 +0000435
Adrian Prantl057d3362017-09-15 23:04:04 +0000436 if (shouldDump(Explicit, ".debug_ranges", DIDT_ID_DebugRanges,
Adrian Prantl84168022017-09-15 17:39:50 +0000437 DObj->getRangeSection().Data)) {
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000438 // In fact, different compile units may have different address byte
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000439 // sizes, but for simplicity we just use the address byte size of the
440 // last compile unit (there is no easy and fast way to associate address
441 // range list and the compile unit it describes).
442 // FIXME: savedAddressByteSize seems sketchy.
Paul Robinson63811a42017-11-22 15:33:17 +0000443 uint8_t savedAddressByteSize = 0;
444 for (const auto &CU : compile_units()) {
445 savedAddressByteSize = CU->getAddressByteSize();
446 break;
447 }
Rafael Espindolac398e672017-07-19 22:27:28 +0000448 DWARFDataExtractor rangesData(*DObj, DObj->getRangeSection(),
449 isLittleEndian(), savedAddressByteSize);
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000450 uint32_t offset = 0;
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000451 DWARFDebugRangeList rangeList;
Paul Robinson17536b92017-06-29 16:52:08 +0000452 while (rangeList.extract(rangesData, &offset))
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000453 rangeList.dump(OS);
Eric Christopherda4b2192013-01-02 23:52:13 +0000454 }
Eric Christopher962c9082013-01-15 23:56:56 +0000455
James Henderson3fcc7452018-02-02 12:35:52 +0000456 if (shouldDump(Explicit, ".debug_rnglists", DIDT_ID_DebugRnglists,
457 DObj->getRnglistsSection().Data)) {
458 DWARFDataExtractor rnglistData(*DObj, DObj->getRnglistsSection(),
459 isLittleEndian(), 0);
460 uint32_t Offset = 0;
461 while (rnglistData.isValidOffset(Offset)) {
Wolfgang Pieb3fb9e3f2018-04-05 21:01:49 +0000462 DWARFDebugRnglistTable Rnglists;
James Henderson3fcc7452018-02-02 12:35:52 +0000463 uint32_t TableOffset = Offset;
464 if (Error Err = Rnglists.extract(rnglistData, &Offset)) {
Jonas Devlieghere84e99262018-04-14 22:07:23 +0000465 WithColor::error() << toString(std::move(Err)) << '\n';
James Henderson3fcc7452018-02-02 12:35:52 +0000466 uint64_t Length = Rnglists.length();
467 // Keep going after an error, if we can, assuming that the length field
468 // could be read. If it couldn't, stop reading the section.
469 if (Length == 0)
470 break;
471 Offset = TableOffset + Length;
Wolfgang Pieba0729d42018-03-08 20:52:35 +0000472 } else {
473 Rnglists.dump(OS, DumpOpts);
474 }
James Henderson3fcc7452018-02-02 12:35:52 +0000475 }
476 }
477
Adrian Prantl057d3362017-09-15 23:04:04 +0000478 if (shouldDump(Explicit, ".debug_pubnames", DIDT_ID_DebugPubnames,
Adrian Prantl84168022017-09-15 17:39:50 +0000479 DObj->getPubNamesSection()))
Rafael Espindolac398e672017-07-19 22:27:28 +0000480 DWARFDebugPubTable(DObj->getPubNamesSection(), isLittleEndian(), false)
Adrian Prantl84168022017-09-15 17:39:50 +0000481 .dump(OS);
Krzysztof Parzyszek97438dc2013-02-12 16:20:28 +0000482
Adrian Prantl057d3362017-09-15 23:04:04 +0000483 if (shouldDump(Explicit, ".debug_pubtypes", DIDT_ID_DebugPubtypes,
Adrian Prantl84168022017-09-15 17:39:50 +0000484 DObj->getPubTypesSection()))
Rafael Espindolac398e672017-07-19 22:27:28 +0000485 DWARFDebugPubTable(DObj->getPubTypesSection(), isLittleEndian(), false)
Adrian Prantl84168022017-09-15 17:39:50 +0000486 .dump(OS);
Eric Christopher4c7e6ba2013-09-25 23:02:41 +0000487
Adrian Prantl057d3362017-09-15 23:04:04 +0000488 if (shouldDump(Explicit, ".debug_gnu_pubnames", DIDT_ID_DebugGnuPubnames,
Adrian Prantl84168022017-09-15 17:39:50 +0000489 DObj->getGnuPubNamesSection()))
Rafael Espindolac398e672017-07-19 22:27:28 +0000490 DWARFDebugPubTable(DObj->getGnuPubNamesSection(), isLittleEndian(),
George Rimare71e33f2016-12-17 09:10:32 +0000491 true /* GnuStyle */)
Adrian Prantl84168022017-09-15 17:39:50 +0000492 .dump(OS);
David Blaikieecd21ff2013-09-24 19:50:00 +0000493
Adrian Prantl057d3362017-09-15 23:04:04 +0000494 if (shouldDump(Explicit, ".debug_gnu_pubtypes", DIDT_ID_DebugGnuPubtypes,
Adrian Prantl84168022017-09-15 17:39:50 +0000495 DObj->getGnuPubTypesSection()))
Rafael Espindolac398e672017-07-19 22:27:28 +0000496 DWARFDebugPubTable(DObj->getGnuPubTypesSection(), isLittleEndian(),
George Rimare71e33f2016-12-17 09:10:32 +0000497 true /* GnuStyle */)
Adrian Prantl84168022017-09-15 17:39:50 +0000498 .dump(OS);
David Blaikie404d3042013-09-19 23:01:29 +0000499
Adrian Prantl057d3362017-09-15 23:04:04 +0000500 if (shouldDump(Explicit, ".debug_str_offsets", DIDT_ID_DebugStrOffsets,
Adrian Prantl84168022017-09-15 17:39:50 +0000501 DObj->getStringOffsetSection().Data))
Rafael Espindolac398e672017-07-19 22:27:28 +0000502 dumpStringOffsetsSection(
503 OS, "debug_str_offsets", *DObj, DObj->getStringOffsetSection(),
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000504 DObj->getStringSection(), compile_units(), type_unit_sections(),
505 isLittleEndian(), getMaxVersion());
Adrian Prantl057d3362017-09-15 23:04:04 +0000506 if (shouldDump(ExplicitDWO, ".debug_str_offsets.dwo", DIDT_ID_DebugStrOffsets,
Adrian Prantl84168022017-09-15 17:39:50 +0000507 DObj->getStringOffsetDWOSection().Data))
Rafael Espindolac398e672017-07-19 22:27:28 +0000508 dumpStringOffsetsSection(
509 OS, "debug_str_offsets.dwo", *DObj, DObj->getStringOffsetDWOSection(),
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000510 DObj->getStringDWOSection(), dwo_compile_units(),
511 dwo_type_unit_sections(), isLittleEndian(), getMaxVersion());
Frederic Risse837ec22014-11-14 16:15:53 +0000512
Adrian Prantl057d3362017-09-15 23:04:04 +0000513 if (shouldDump(Explicit, ".gnu_index", DIDT_ID_GdbIndex,
Adrian Prantl84168022017-09-15 17:39:50 +0000514 DObj->getGdbIndexSection())) {
George Rimar4f82df52016-09-23 11:01:53 +0000515 getGdbIndex().dump(OS);
516 }
517
Adrian Prantl057d3362017-09-15 23:04:04 +0000518 if (shouldDump(Explicit, ".apple_names", DIDT_ID_AppleNames,
Adrian Prantl84168022017-09-15 17:39:50 +0000519 DObj->getAppleNamesSection().Data))
Adrian Prantl99fdb9d2017-09-28 18:10:52 +0000520 getAppleNames().dump(OS);
Frederic Risse837ec22014-11-14 16:15:53 +0000521
Adrian Prantl057d3362017-09-15 23:04:04 +0000522 if (shouldDump(Explicit, ".apple_types", DIDT_ID_AppleTypes,
Adrian Prantl84168022017-09-15 17:39:50 +0000523 DObj->getAppleTypesSection().Data))
Adrian Prantl714ee4d2017-09-29 00:33:22 +0000524 getAppleTypes().dump(OS);
Frederic Risse837ec22014-11-14 16:15:53 +0000525
Adrian Prantl057d3362017-09-15 23:04:04 +0000526 if (shouldDump(Explicit, ".apple_namespaces", DIDT_ID_AppleNamespaces,
Adrian Prantl84168022017-09-15 17:39:50 +0000527 DObj->getAppleNamespacesSection().Data))
Adrian Prantlf51e7802017-09-29 00:52:33 +0000528 getAppleNamespaces().dump(OS);
Frederic Risse837ec22014-11-14 16:15:53 +0000529
Adrian Prantl057d3362017-09-15 23:04:04 +0000530 if (shouldDump(Explicit, ".apple_objc", DIDT_ID_AppleObjC,
Adrian Prantl84168022017-09-15 17:39:50 +0000531 DObj->getAppleObjCSection().Data))
Adrian Prantlf51e7802017-09-29 00:52:33 +0000532 getAppleObjC().dump(OS);
Pavel Labath3c9a9182018-01-29 11:08:32 +0000533 if (shouldDump(Explicit, ".debug_names", DIDT_ID_DebugNames,
534 DObj->getDebugNamesSection().Data))
535 getDebugNames().dump(OS);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000536}
537
David Blaikie15d85fc2017-05-23 06:48:53 +0000538DWARFCompileUnit *DWARFContext::getDWOCompileUnitForHash(uint64_t Hash) {
David Blaikiee79dda32017-09-19 18:36:11 +0000539 DWOCUs.parseDWO(*this, DObj->getInfoDWOSection(), true);
David Blaikiea62f1cb2017-07-30 15:15:58 +0000540
David Blaikieebac0b92017-07-30 08:12:07 +0000541 if (const auto &CUI = getCUIndex()) {
542 if (const auto *R = CUI.getFromHash(Hash))
David Blaikiee79dda32017-09-19 18:36:11 +0000543 return DWOCUs.getUnitForIndexEntry(*R);
David Blaikieebac0b92017-07-30 08:12:07 +0000544 return nullptr;
545 }
546
547 // If there's no index, just search through the CUs in the DWO - there's
548 // probably only one unless this is something like LTO - though an in-process
549 // built/cached lookup table could be used in that case to improve repeated
550 // lookups of different CUs in the DWO.
David Blaikie15d85fc2017-05-23 06:48:53 +0000551 for (const auto &DWOCU : dwo_compile_units())
552 if (DWOCU->getDWOId() == Hash)
553 return DWOCU.get();
554 return nullptr;
555}
556
Greg Claytonc7695a82017-05-02 20:28:33 +0000557DWARFDie DWARFContext::getDIEForOffset(uint32_t Offset) {
558 parseCompileUnits();
559 if (auto *CU = CUs.getUnitForOffset(Offset))
560 return CU->getDIEForOffset(Offset);
561 return DWARFDie();
562}
563
Jonas Devliegherec0a758d2017-09-18 14:15:57 +0000564bool DWARFContext::verify(raw_ostream &OS, DIDumpOptions DumpOpts) {
Greg Claytonc7695a82017-05-02 20:28:33 +0000565 bool Success = true;
Jonas Devlieghere27476ce2017-09-13 09:43:05 +0000566 DWARFVerifier verifier(OS, *this, DumpOpts);
Spyridoula Gravani73e17962017-07-27 00:59:33 +0000567
Spyridoula Gravani364b5352017-07-20 02:06:52 +0000568 Success &= verifier.handleDebugAbbrev();
Jonas Devliegherec0a758d2017-09-18 14:15:57 +0000569 if (DumpOpts.DumpType & DIDT_DebugInfo)
Spyridoula Gravani73e17962017-07-27 00:59:33 +0000570 Success &= verifier.handleDebugInfo();
Jonas Devliegherec0a758d2017-09-18 14:15:57 +0000571 if (DumpOpts.DumpType & DIDT_DebugLine)
Spyridoula Gravani73e17962017-07-27 00:59:33 +0000572 Success &= verifier.handleDebugLine();
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000573 Success &= verifier.handleAccelTables();
Greg Clayton48432cf2017-05-01 22:07:02 +0000574 return Success;
575}
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000576
David Blaikie82641be2015-11-17 00:39:55 +0000577const DWARFUnitIndex &DWARFContext::getCUIndex() {
578 if (CUIndex)
579 return *CUIndex;
580
Rafael Espindolac398e672017-07-19 22:27:28 +0000581 DataExtractor CUIndexData(DObj->getCUIndexSection(), isLittleEndian(), 0);
David Blaikie82641be2015-11-17 00:39:55 +0000582
David Blaikieb073cb92015-12-02 06:21:34 +0000583 CUIndex = llvm::make_unique<DWARFUnitIndex>(DW_SECT_INFO);
David Blaikie82641be2015-11-17 00:39:55 +0000584 CUIndex->parse(CUIndexData);
585 return *CUIndex;
586}
587
588const DWARFUnitIndex &DWARFContext::getTUIndex() {
589 if (TUIndex)
590 return *TUIndex;
591
Rafael Espindolac398e672017-07-19 22:27:28 +0000592 DataExtractor TUIndexData(DObj->getTUIndexSection(), isLittleEndian(), 0);
David Blaikie82641be2015-11-17 00:39:55 +0000593
David Blaikieb073cb92015-12-02 06:21:34 +0000594 TUIndex = llvm::make_unique<DWARFUnitIndex>(DW_SECT_TYPES);
David Blaikie82641be2015-11-17 00:39:55 +0000595 TUIndex->parse(TUIndexData);
596 return *TUIndex;
597}
598
George Rimar4f82df52016-09-23 11:01:53 +0000599DWARFGdbIndex &DWARFContext::getGdbIndex() {
600 if (GdbIndex)
601 return *GdbIndex;
602
Rafael Espindolac398e672017-07-19 22:27:28 +0000603 DataExtractor GdbIndexData(DObj->getGdbIndexSection(), true /*LE*/, 0);
George Rimar4f82df52016-09-23 11:01:53 +0000604 GdbIndex = llvm::make_unique<DWARFGdbIndex>();
605 GdbIndex->parse(GdbIndexData);
606 return *GdbIndex;
607}
608
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000609const DWARFDebugAbbrev *DWARFContext::getDebugAbbrev() {
610 if (Abbrev)
611 return Abbrev.get();
612
Rafael Espindolac398e672017-07-19 22:27:28 +0000613 DataExtractor abbrData(DObj->getAbbrevSection(), isLittleEndian(), 0);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000614
615 Abbrev.reset(new DWARFDebugAbbrev());
Alexey Samsonov4316df52014-04-25 21:10:56 +0000616 Abbrev->extract(abbrData);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000617 return Abbrev.get();
618}
619
Eric Christopherda4b2192013-01-02 23:52:13 +0000620const DWARFDebugAbbrev *DWARFContext::getDebugAbbrevDWO() {
621 if (AbbrevDWO)
622 return AbbrevDWO.get();
623
Rafael Espindolac398e672017-07-19 22:27:28 +0000624 DataExtractor abbrData(DObj->getAbbrevDWOSection(), isLittleEndian(), 0);
Eric Christopherda4b2192013-01-02 23:52:13 +0000625 AbbrevDWO.reset(new DWARFDebugAbbrev());
Alexey Samsonov4316df52014-04-25 21:10:56 +0000626 AbbrevDWO->extract(abbrData);
Eric Christopherda4b2192013-01-02 23:52:13 +0000627 return AbbrevDWO.get();
628}
629
David Blaikie18e73502013-06-19 21:37:13 +0000630const DWARFDebugLoc *DWARFContext::getDebugLoc() {
631 if (Loc)
632 return Loc.get();
633
Paul Robinson17536b92017-06-29 16:52:08 +0000634 Loc.reset(new DWARFDebugLoc);
Pavel Labath54ca2d62018-04-06 08:49:57 +0000635 // Assume all compile units have the same address byte size.
Paul Robinson17536b92017-06-29 16:52:08 +0000636 if (getNumCompileUnits()) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000637 DWARFDataExtractor LocData(*DObj, DObj->getLocSection(), isLittleEndian(),
Paul Robinson17536b92017-06-29 16:52:08 +0000638 getCompileUnitAtIndex(0)->getAddressByteSize());
639 Loc->parse(LocData);
640 }
David Blaikie18e73502013-06-19 21:37:13 +0000641 return Loc.get();
642}
643
David Blaikie9c550ac2014-03-25 01:44:02 +0000644const DWARFDebugLocDWO *DWARFContext::getDebugLocDWO() {
645 if (LocDWO)
646 return LocDWO.get();
647
David Blaikie9c550ac2014-03-25 01:44:02 +0000648 LocDWO.reset(new DWARFDebugLocDWO());
Pavel Labath54ca2d62018-04-06 08:49:57 +0000649 // Assume all compile units have the same address byte size.
650 if (getNumCompileUnits()) {
651 DataExtractor LocData(DObj->getLocDWOSection().Data, isLittleEndian(),
652 getCompileUnitAtIndex(0)->getAddressByteSize());
653 LocDWO->parse(LocData);
654 }
David Blaikie9c550ac2014-03-25 01:44:02 +0000655 return LocDWO.get();
656}
657
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000658const DWARFDebugAranges *DWARFContext::getDebugAranges() {
659 if (Aranges)
660 return Aranges.get();
661
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000662 Aranges.reset(new DWARFDebugAranges());
Alexey Samsonova1694c12012-11-16 08:36:25 +0000663 Aranges->generate(this);
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000664 return Aranges.get();
665}
666
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000667const DWARFDebugFrame *DWARFContext::getDebugFrame() {
668 if (DebugFrame)
669 return DebugFrame.get();
670
671 // There's a "bug" in the DWARFv3 standard with respect to the target address
672 // size within debug frame sections. While DWARF is supposed to be independent
673 // of its container, FDEs have fields with size being "target address size",
674 // which isn't specified in DWARF in general. It's only specified for CUs, but
675 // .eh_frame can appear without a .debug_info section. Follow the example of
676 // other tools (libdwarf) and extract this from the container (ObjectFile
677 // provides this information). This problem is fixed in DWARFv4
678 // See this dwarf-discuss discussion for more details:
679 // http://lists.dwarfstd.org/htdig.cgi/dwarf-discuss-dwarfstd.org/2011-December/001173.html
Rafael Auler86fb7bf2018-03-08 00:46:53 +0000680 DWARFDataExtractor debugFrameData(DObj->getDebugFrameSection(),
681 isLittleEndian(), DObj->getAddressSize());
Igor Laevsky03a670c2016-01-26 15:09:42 +0000682 DebugFrame.reset(new DWARFDebugFrame(false /* IsEH */));
683 DebugFrame->parse(debugFrameData);
684 return DebugFrame.get();
685}
686
687const DWARFDebugFrame *DWARFContext::getEHFrame() {
688 if (EHFrame)
689 return EHFrame.get();
690
Rafael Auler86fb7bf2018-03-08 00:46:53 +0000691 DWARFDataExtractor debugFrameData(DObj->getEHFrameSection(), isLittleEndian(),
692 DObj->getAddressSize());
Igor Laevsky03a670c2016-01-26 15:09:42 +0000693 DebugFrame.reset(new DWARFDebugFrame(true /* IsEH */));
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000694 DebugFrame->parse(debugFrameData);
695 return DebugFrame.get();
696}
697
Amjad Aboude59cc3e2015-11-12 09:38:54 +0000698const DWARFDebugMacro *DWARFContext::getDebugMacro() {
699 if (Macro)
700 return Macro.get();
701
Rafael Espindolac398e672017-07-19 22:27:28 +0000702 DataExtractor MacinfoData(DObj->getMacinfoSection(), isLittleEndian(), 0);
Amjad Aboude59cc3e2015-11-12 09:38:54 +0000703 Macro.reset(new DWARFDebugMacro());
704 Macro->parse(MacinfoData);
705 return Macro.get();
706}
707
Pavel Labath3c9a9182018-01-29 11:08:32 +0000708template <typename T>
709static T &getAccelTable(std::unique_ptr<T> &Cache, const DWARFObject &Obj,
710 const DWARFSection &Section, StringRef StringSection,
711 bool IsLittleEndian) {
Adrian Prantl99fdb9d2017-09-28 18:10:52 +0000712 if (Cache)
713 return *Cache;
714 DWARFDataExtractor AccelSection(Obj, Section, IsLittleEndian, 0);
715 DataExtractor StrData(StringSection, IsLittleEndian, 0);
Pavel Labath3c9a9182018-01-29 11:08:32 +0000716 Cache.reset(new T(AccelSection, StrData));
Jonas Devlieghereba915892017-12-11 18:22:47 +0000717 if (Error E = Cache->extract())
718 llvm::consumeError(std::move(E));
Adrian Prantl99fdb9d2017-09-28 18:10:52 +0000719 return *Cache;
720}
721
Pavel Labath3c9a9182018-01-29 11:08:32 +0000722const DWARFDebugNames &DWARFContext::getDebugNames() {
723 return getAccelTable(Names, *DObj, DObj->getDebugNamesSection(),
724 DObj->getStringSection(), isLittleEndian());
725}
726
Pavel Labath9b36fd22018-01-22 13:17:23 +0000727const AppleAcceleratorTable &DWARFContext::getAppleNames() {
Adrian Prantl99fdb9d2017-09-28 18:10:52 +0000728 return getAccelTable(AppleNames, *DObj, DObj->getAppleNamesSection(),
729 DObj->getStringSection(), isLittleEndian());
730}
731
Pavel Labath9b36fd22018-01-22 13:17:23 +0000732const AppleAcceleratorTable &DWARFContext::getAppleTypes() {
Adrian Prantl714ee4d2017-09-29 00:33:22 +0000733 return getAccelTable(AppleTypes, *DObj, DObj->getAppleTypesSection(),
734 DObj->getStringSection(), isLittleEndian());
735}
736
Pavel Labath9b36fd22018-01-22 13:17:23 +0000737const AppleAcceleratorTable &DWARFContext::getAppleNamespaces() {
Adrian Prantlf51e7802017-09-29 00:52:33 +0000738 return getAccelTable(AppleNamespaces, *DObj,
739 DObj->getAppleNamespacesSection(),
740 DObj->getStringSection(), isLittleEndian());
741}
742
Pavel Labath9b36fd22018-01-22 13:17:23 +0000743const AppleAcceleratorTable &DWARFContext::getAppleObjC() {
Adrian Prantlf51e7802017-09-29 00:52:33 +0000744 return getAccelTable(AppleObjC, *DObj, DObj->getAppleObjCSection(),
745 DObj->getStringSection(), isLittleEndian());
746}
747
James Hendersona3acf992018-05-10 10:51:33 +0000748const DWARFDebugLine::LineTable *
Alexey Samsonov7a18c062015-05-19 21:54:32 +0000749DWARFContext::getLineTableForUnit(DWARFUnit *U) {
James Hendersona3acf992018-05-10 10:51:33 +0000750 Expected<const DWARFDebugLine::LineTable *> ExpectedLineTable =
751 getLineTableForUnit(U, DWARFDebugLine::warn);
752 if (!ExpectedLineTable) {
753 DWARFDebugLine::warnForError(ExpectedLineTable.takeError());
754 return nullptr;
755 }
756 return *ExpectedLineTable;
757}
758
759Expected<const DWARFDebugLine::LineTable *>
760DWARFContext::getLineTableForUnit(DWARFUnit *U,
761 std::function<void(StringRef)> WarnCallback) {
Benjamin Kramer679e1752011-09-15 20:43:18 +0000762 if (!Line)
Paul Robinson17536b92017-06-29 16:52:08 +0000763 Line.reset(new DWARFDebugLine);
David Blaikiec4e2bed2015-11-17 21:08:05 +0000764
Greg Claytonc8c10322016-12-13 18:25:19 +0000765 auto UnitDIE = U->getUnitDIE();
766 if (!UnitDIE)
Alexey Samsonov7a18c062015-05-19 21:54:32 +0000767 return nullptr;
David Blaikiec4e2bed2015-11-17 21:08:05 +0000768
Greg Clayton97d22182017-01-13 21:08:18 +0000769 auto Offset = toSectionOffset(UnitDIE.find(DW_AT_stmt_list));
Greg Clayton52fe1f62016-12-14 22:38:08 +0000770 if (!Offset)
Craig Topper2617dcc2014-04-15 06:32:26 +0000771 return nullptr; // No line table for this compile unit.
Benjamin Kramer5acab502011-09-15 02:12:05 +0000772
Greg Clayton52fe1f62016-12-14 22:38:08 +0000773 uint32_t stmtOffset = *Offset + U->getLineTableOffset();
Benjamin Kramer679e1752011-09-15 20:43:18 +0000774 // See if the line table is cached.
Eric Christopher494109b2012-10-16 23:46:25 +0000775 if (const DWARFLineTable *lt = Line->getLineTable(stmtOffset))
Benjamin Kramer679e1752011-09-15 20:43:18 +0000776 return lt;
777
Greg Clayton48ff66a2017-05-04 18:29:44 +0000778 // Make sure the offset is good before we try to parse.
Paul Robinson17536b92017-06-29 16:52:08 +0000779 if (stmtOffset >= U->getLineSection().Data.size())
Jonas Devlieghere27476ce2017-09-13 09:43:05 +0000780 return nullptr;
Greg Clayton48ff66a2017-05-04 18:29:44 +0000781
Benjamin Kramer679e1752011-09-15 20:43:18 +0000782 // We have to parse it first.
Rafael Espindolac398e672017-07-19 22:27:28 +0000783 DWARFDataExtractor lineData(*DObj, U->getLineSection(), isLittleEndian(),
Paul Robinson17536b92017-06-29 16:52:08 +0000784 U->getAddressByteSize());
James Hendersona3acf992018-05-10 10:51:33 +0000785 return Line->getOrParseLineTable(lineData, stmtOffset, *this, U,
786 WarnCallback);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000787}
788
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000789void DWARFContext::parseCompileUnits() {
Rafael Espindolac398e672017-07-19 22:27:28 +0000790 CUs.parse(*this, DObj->getInfoSection());
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000791}
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000792
David Blaikie03c089c2013-09-23 22:44:47 +0000793void DWARFContext::parseTypeUnits() {
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000794 if (!TUs.empty())
795 return;
Rafael Espindolac398e672017-07-19 22:27:28 +0000796 DObj->forEachTypesSections([&](const DWARFSection &S) {
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000797 TUs.emplace_back();
Rafael Espindola5e5dfa12017-07-12 21:08:24 +0000798 TUs.back().parse(*this, S);
799 });
David Blaikie03c089c2013-09-23 22:44:47 +0000800}
801
Eric Christopherda4b2192013-01-02 23:52:13 +0000802void DWARFContext::parseDWOCompileUnits() {
Rafael Espindolac398e672017-07-19 22:27:28 +0000803 DWOCUs.parseDWO(*this, DObj->getInfoDWOSection());
Eric Christopherda4b2192013-01-02 23:52:13 +0000804}
805
David Blaikie92d9d622014-01-09 05:08:24 +0000806void DWARFContext::parseDWOTypeUnits() {
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000807 if (!DWOTUs.empty())
808 return;
Rafael Espindolac398e672017-07-19 22:27:28 +0000809 DObj->forEachTypesDWOSections([&](const DWARFSection &S) {
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000810 DWOTUs.emplace_back();
Rafael Espindola5e5dfa12017-07-12 21:08:24 +0000811 DWOTUs.back().parseDWO(*this, S);
812 });
David Blaikie92d9d622014-01-09 05:08:24 +0000813}
814
Alexey Samsonov45be7932012-08-30 07:49:50 +0000815DWARFCompileUnit *DWARFContext::getCompileUnitForOffset(uint32_t Offset) {
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000816 parseCompileUnits();
Frederic Riss4e126a02014-09-15 07:50:27 +0000817 return CUs.getUnitForOffset(Offset);
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000818}
819
Alexey Samsonov45be7932012-08-30 07:49:50 +0000820DWARFCompileUnit *DWARFContext::getCompileUnitForAddress(uint64_t Address) {
Benjamin Kramer112ec172011-09-15 21:59:13 +0000821 // First, get the offset of the compile unit.
Alexey Samsonov45be7932012-08-30 07:49:50 +0000822 uint32_t CUOffset = getDebugAranges()->findAddress(Address);
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000823 // Retrieve the compile unit.
Alexey Samsonov45be7932012-08-30 07:49:50 +0000824 return getCompileUnitForOffset(CUOffset);
825}
826
Jonas Devliegheref63ee642017-10-25 21:56:41 +0000827DWARFContext::DIEsForAddress DWARFContext::getDIEsForAddress(uint64_t Address) {
828 DIEsForAddress Result;
829
830 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
831 if (!CU)
832 return Result;
833
834 Result.CompileUnit = CU;
835 Result.FunctionDIE = CU->getSubroutineForAddress(Address);
836
837 std::vector<DWARFDie> Worklist;
838 Worklist.push_back(Result.FunctionDIE);
839 while (!Worklist.empty()) {
840 DWARFDie DIE = Worklist.back();
841 Worklist.pop_back();
842
843 if (DIE.getTag() == DW_TAG_lexical_block &&
844 DIE.addressRangeContainsAddress(Address)) {
845 Result.BlockDIE = DIE;
846 break;
847 }
848
849 for (auto Child : DIE)
850 Worklist.push_back(Child);
851 }
852
853 return Result;
854}
855
David Blaikieefc4eba2017-02-06 20:19:02 +0000856static bool getFunctionNameAndStartLineForAddress(DWARFCompileUnit *CU,
857 uint64_t Address,
858 FunctionNameKind Kind,
859 std::string &FunctionName,
860 uint32_t &StartLine) {
Alexey Samsonovd0109992014-04-18 21:36:39 +0000861 // The address may correspond to instruction in some inlined function,
862 // so we have to build the chain of inlined functions and take the
David Blaikieefc4eba2017-02-06 20:19:02 +0000863 // name of the topmost function in it.
Greg Claytonc8c10322016-12-13 18:25:19 +0000864 SmallVector<DWARFDie, 4> InlinedChain;
865 CU->getInlinedChainForAddress(Address, InlinedChain);
David Blaikieefc4eba2017-02-06 20:19:02 +0000866 if (InlinedChain.empty())
Alexey Samsonovd0109992014-04-18 21:36:39 +0000867 return false;
David Blaikieefc4eba2017-02-06 20:19:02 +0000868
869 const DWARFDie &DIE = InlinedChain[0];
870 bool FoundResult = false;
871 const char *Name = nullptr;
872 if (Kind != FunctionNameKind::None && (Name = DIE.getSubroutineName(Kind))) {
Alexey Samsonovd0109992014-04-18 21:36:39 +0000873 FunctionName = Name;
David Blaikieefc4eba2017-02-06 20:19:02 +0000874 FoundResult = true;
Alexey Samsonovd0109992014-04-18 21:36:39 +0000875 }
David Blaikieefc4eba2017-02-06 20:19:02 +0000876 if (auto DeclLineResult = DIE.getDeclLine()) {
877 StartLine = DeclLineResult;
878 FoundResult = true;
879 }
880
881 return FoundResult;
Alexey Samsonovd0109992014-04-18 21:36:39 +0000882}
883
Alexey Samsonov45be7932012-08-30 07:49:50 +0000884DILineInfo DWARFContext::getLineInfoForAddress(uint64_t Address,
Alexey Samsonovdce67342014-05-15 21:24:32 +0000885 DILineInfoSpecifier Spec) {
Alexey Samsonovd0109992014-04-18 21:36:39 +0000886 DILineInfo Result;
887
Alexey Samsonov45be7932012-08-30 07:49:50 +0000888 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
889 if (!CU)
Alexey Samsonovd0109992014-04-18 21:36:39 +0000890 return Result;
David Blaikieefc4eba2017-02-06 20:19:02 +0000891 getFunctionNameAndStartLineForAddress(CU, Address, Spec.FNKind,
892 Result.FunctionName,
893 Result.StartLine);
Alexey Samsonovdce67342014-05-15 21:24:32 +0000894 if (Spec.FLIKind != FileLineInfoKind::None) {
Frederic Riss101b5e22014-09-19 15:11:51 +0000895 if (const DWARFLineTable *LineTable = getLineTableForUnit(CU))
896 LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(),
897 Spec.FLIKind, Result);
Alexey Samsonovf4462fa2012-07-02 05:54:45 +0000898 }
Alexey Samsonovd0109992014-04-18 21:36:39 +0000899 return Result;
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000900}
David Blaikiea379b1812011-12-20 02:50:00 +0000901
Alexey Samsonovdce67342014-05-15 21:24:32 +0000902DILineInfoTable
903DWARFContext::getLineInfoForAddressRange(uint64_t Address, uint64_t Size,
904 DILineInfoSpecifier Spec) {
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000905 DILineInfoTable Lines;
906 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
907 if (!CU)
908 return Lines;
909
910 std::string FunctionName = "<invalid>";
David Blaikieefc4eba2017-02-06 20:19:02 +0000911 uint32_t StartLine = 0;
912 getFunctionNameAndStartLineForAddress(CU, Address, Spec.FNKind, FunctionName,
913 StartLine);
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000914
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000915 // If the Specifier says we don't need FileLineInfo, just
916 // return the top-most function at the starting address.
Alexey Samsonovdce67342014-05-15 21:24:32 +0000917 if (Spec.FLIKind == FileLineInfoKind::None) {
Alexey Samsonovd0109992014-04-18 21:36:39 +0000918 DILineInfo Result;
919 Result.FunctionName = FunctionName;
David Blaikieefc4eba2017-02-06 20:19:02 +0000920 Result.StartLine = StartLine;
Alexey Samsonovd0109992014-04-18 21:36:39 +0000921 Lines.push_back(std::make_pair(Address, Result));
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000922 return Lines;
923 }
924
Frederic Rissec8a5ba2014-09-04 06:14:40 +0000925 const DWARFLineTable *LineTable = getLineTableForUnit(CU);
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000926
927 // Get the index of row we're looking for in the line table.
928 std::vector<uint32_t> RowVector;
929 if (!LineTable->lookupAddressRange(Address, Size, RowVector))
930 return Lines;
931
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000932 for (uint32_t RowIndex : RowVector) {
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000933 // Take file number and line/column from the row.
934 const DWARFDebugLine::Row &Row = LineTable->Rows[RowIndex];
Alexey Samsonovd0109992014-04-18 21:36:39 +0000935 DILineInfo Result;
Frederic Riss101b5e22014-09-19 15:11:51 +0000936 LineTable->getFileNameByIndex(Row.File, CU->getCompilationDir(),
937 Spec.FLIKind, Result.FileName);
Alexey Samsonovd0109992014-04-18 21:36:39 +0000938 Result.FunctionName = FunctionName;
939 Result.Line = Row.Line;
940 Result.Column = Row.Column;
David Blaikieefc4eba2017-02-06 20:19:02 +0000941 Result.StartLine = StartLine;
Alexey Samsonovd0109992014-04-18 21:36:39 +0000942 Lines.push_back(std::make_pair(Row.Address, Result));
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000943 }
944
945 return Lines;
946}
947
Alexey Samsonovdce67342014-05-15 21:24:32 +0000948DIInliningInfo
949DWARFContext::getInliningInfoForAddress(uint64_t Address,
950 DILineInfoSpecifier Spec) {
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000951 DIInliningInfo InliningInfo;
952
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000953 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
954 if (!CU)
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000955 return InliningInfo;
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000956
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000957 const DWARFLineTable *LineTable = nullptr;
Greg Claytonc8c10322016-12-13 18:25:19 +0000958 SmallVector<DWARFDie, 4> InlinedChain;
959 CU->getInlinedChainForAddress(Address, InlinedChain);
960 if (InlinedChain.size() == 0) {
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000961 // If there is no DIE for address (e.g. it is in unavailable .dwo file),
962 // try to at least get file/line info from symbol table.
Alexey Samsonovdce67342014-05-15 21:24:32 +0000963 if (Spec.FLIKind != FileLineInfoKind::None) {
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000964 DILineInfo Frame;
Frederic Rissec8a5ba2014-09-04 06:14:40 +0000965 LineTable = getLineTableForUnit(CU);
Frederic Riss101b5e22014-09-19 15:11:51 +0000966 if (LineTable &&
967 LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(),
968 Spec.FLIKind, Frame))
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000969 InliningInfo.addFrame(Frame);
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000970 }
971 return InliningInfo;
972 }
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000973
Dehao Chenef700d52017-04-17 20:10:39 +0000974 uint32_t CallFile = 0, CallLine = 0, CallColumn = 0, CallDiscriminator = 0;
Greg Claytonc8c10322016-12-13 18:25:19 +0000975 for (uint32_t i = 0, n = InlinedChain.size(); i != n; i++) {
976 DWARFDie &FunctionDIE = InlinedChain[i];
Alexey Samsonovd0109992014-04-18 21:36:39 +0000977 DILineInfo Frame;
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000978 // Get function name if necessary.
Greg Claytonc8c10322016-12-13 18:25:19 +0000979 if (const char *Name = FunctionDIE.getSubroutineName(Spec.FNKind))
Alexey Samsonovdce67342014-05-15 21:24:32 +0000980 Frame.FunctionName = Name;
David Blaikieefc4eba2017-02-06 20:19:02 +0000981 if (auto DeclLineResult = FunctionDIE.getDeclLine())
982 Frame.StartLine = DeclLineResult;
Alexey Samsonovdce67342014-05-15 21:24:32 +0000983 if (Spec.FLIKind != FileLineInfoKind::None) {
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000984 if (i == 0) {
985 // For the topmost frame, initialize the line table of this
986 // compile unit and fetch file/line info from it.
Frederic Rissec8a5ba2014-09-04 06:14:40 +0000987 LineTable = getLineTableForUnit(CU);
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000988 // For the topmost routine, get file/line info from line table.
Frederic Riss101b5e22014-09-19 15:11:51 +0000989 if (LineTable)
990 LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(),
991 Spec.FLIKind, Frame);
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000992 } else {
993 // Otherwise, use call file, call line and call column from
994 // previous DIE in inlined chain.
Frederic Riss101b5e22014-09-19 15:11:51 +0000995 if (LineTable)
996 LineTable->getFileNameByIndex(CallFile, CU->getCompilationDir(),
997 Spec.FLIKind, Frame.FileName);
Alexey Samsonovd0109992014-04-18 21:36:39 +0000998 Frame.Line = CallLine;
999 Frame.Column = CallColumn;
Dehao Chenef700d52017-04-17 20:10:39 +00001000 Frame.Discriminator = CallDiscriminator;
Alexey Samsonovc942e6b2012-09-04 08:12:33 +00001001 }
1002 // Get call file/line/column of a current DIE.
1003 if (i + 1 < n) {
Dehao Chenef700d52017-04-17 20:10:39 +00001004 FunctionDIE.getCallerFrame(CallFile, CallLine, CallColumn,
1005 CallDiscriminator);
Alexey Samsonovc942e6b2012-09-04 08:12:33 +00001006 }
1007 }
Alexey Samsonovc942e6b2012-09-04 08:12:33 +00001008 InliningInfo.addFrame(Frame);
1009 }
1010 return InliningInfo;
1011}
1012
David Blaikief9803fb2017-05-23 00:30:42 +00001013std::shared_ptr<DWARFContext>
1014DWARFContext::getDWOContext(StringRef AbsolutePath) {
David Blaikie15d85fc2017-05-23 06:48:53 +00001015 if (auto S = DWP.lock()) {
David Blaikief9803fb2017-05-23 00:30:42 +00001016 DWARFContext *Ctxt = S->Context.get();
1017 return std::shared_ptr<DWARFContext>(std::move(S), Ctxt);
1018 }
1019
David Blaikie15d85fc2017-05-23 06:48:53 +00001020 std::weak_ptr<DWOFile> *Entry = &DWOFiles[AbsolutePath];
1021
1022 if (auto S = Entry->lock()) {
1023 DWARFContext *Ctxt = S->Context.get();
1024 return std::shared_ptr<DWARFContext>(std::move(S), Ctxt);
1025 }
1026
David Blaikie15d85fc2017-05-23 06:48:53 +00001027 Expected<OwningBinary<ObjectFile>> Obj = [&] {
1028 if (!CheckedForDWP) {
David Blaikiee5adb682017-07-30 01:34:08 +00001029 SmallString<128> DWPName;
1030 auto Obj = object::ObjectFile::createObjectFile(
1031 this->DWPName.empty()
1032 ? (DObj->getFileName() + ".dwp").toStringRef(DWPName)
1033 : StringRef(this->DWPName));
David Blaikie15d85fc2017-05-23 06:48:53 +00001034 if (Obj) {
1035 Entry = &DWP;
1036 return Obj;
1037 } else {
1038 CheckedForDWP = true;
1039 // TODO: Should this error be handled (maybe in a high verbosity mode)
1040 // before falling back to .dwo files?
1041 consumeError(Obj.takeError());
1042 }
1043 }
1044
1045 return object::ObjectFile::createObjectFile(AbsolutePath);
1046 }();
1047
David Blaikief9803fb2017-05-23 00:30:42 +00001048 if (!Obj) {
1049 // TODO: Actually report errors helpfully.
1050 consumeError(Obj.takeError());
1051 return nullptr;
1052 }
David Blaikie15d85fc2017-05-23 06:48:53 +00001053
1054 auto S = std::make_shared<DWOFile>();
David Blaikief9803fb2017-05-23 00:30:42 +00001055 S->File = std::move(Obj.get());
Rafael Espindolac398e672017-07-19 22:27:28 +00001056 S->Context = DWARFContext::create(*S->File.getBinary());
David Blaikie15d85fc2017-05-23 06:48:53 +00001057 *Entry = S;
David Blaikief9803fb2017-05-23 00:30:42 +00001058 auto *Ctxt = S->Context.get();
1059 return std::shared_ptr<DWARFContext>(std::move(S), Ctxt);
1060}
1061
George Rimar702dac62017-04-12 08:59:15 +00001062static Error createError(const Twine &Reason, llvm::Error E) {
1063 return make_error<StringError>(Reason + toString(std::move(E)),
1064 inconvertibleErrorCode());
1065}
1066
George Rimara25d3292017-05-27 18:10:23 +00001067/// SymInfo contains information about symbol: it's address
1068/// and section index which is -1LL for absolute symbols.
1069struct SymInfo {
1070 uint64_t Address;
1071 uint64_t SectionIndex;
1072};
1073
1074/// Returns the address of symbol relocation used against and a section index.
1075/// Used for futher relocations computation. Symbol's section load address is
1076static Expected<SymInfo> getSymbolInfo(const object::ObjectFile &Obj,
1077 const RelocationRef &Reloc,
1078 const LoadedObjectInfo *L,
1079 std::map<SymbolRef, SymInfo> &Cache) {
1080 SymInfo Ret = {0, (uint64_t)-1LL};
George Rimar702dac62017-04-12 08:59:15 +00001081 object::section_iterator RSec = Obj.section_end();
1082 object::symbol_iterator Sym = Reloc.getSymbol();
1083
George Rimara25d3292017-05-27 18:10:23 +00001084 std::map<SymbolRef, SymInfo>::iterator CacheIt = Cache.end();
George Rimar702dac62017-04-12 08:59:15 +00001085 // First calculate the address of the symbol or section as it appears
1086 // in the object file
1087 if (Sym != Obj.symbol_end()) {
George Rimar958b01a2017-05-15 11:45:28 +00001088 bool New;
George Rimara25d3292017-05-27 18:10:23 +00001089 std::tie(CacheIt, New) = Cache.insert({*Sym, {0, 0}});
George Rimar958b01a2017-05-15 11:45:28 +00001090 if (!New)
1091 return CacheIt->second;
1092
George Rimar702dac62017-04-12 08:59:15 +00001093 Expected<uint64_t> SymAddrOrErr = Sym->getAddress();
1094 if (!SymAddrOrErr)
George Rimar1af3cb22017-06-28 08:21:19 +00001095 return createError("failed to compute symbol address: ",
George Rimar702dac62017-04-12 08:59:15 +00001096 SymAddrOrErr.takeError());
1097
1098 // Also remember what section this symbol is in for later
1099 auto SectOrErr = Sym->getSection();
1100 if (!SectOrErr)
George Rimar1af3cb22017-06-28 08:21:19 +00001101 return createError("failed to get symbol section: ",
George Rimar702dac62017-04-12 08:59:15 +00001102 SectOrErr.takeError());
1103
1104 RSec = *SectOrErr;
George Rimara25d3292017-05-27 18:10:23 +00001105 Ret.Address = *SymAddrOrErr;
George Rimar702dac62017-04-12 08:59:15 +00001106 } else if (auto *MObj = dyn_cast<MachOObjectFile>(&Obj)) {
1107 RSec = MObj->getRelocationSection(Reloc.getRawDataRefImpl());
George Rimara25d3292017-05-27 18:10:23 +00001108 Ret.Address = RSec->getAddress();
George Rimar702dac62017-04-12 08:59:15 +00001109 }
1110
George Rimara25d3292017-05-27 18:10:23 +00001111 if (RSec != Obj.section_end())
1112 Ret.SectionIndex = RSec->getIndex();
1113
George Rimar702dac62017-04-12 08:59:15 +00001114 // If we are given load addresses for the sections, we need to adjust:
1115 // SymAddr = (Address of Symbol Or Section in File) -
1116 // (Address of Section in File) +
1117 // (Load Address of Section)
1118 // RSec is now either the section being targeted or the section
1119 // containing the symbol being targeted. In either case,
1120 // we need to perform the same computation.
1121 if (L && RSec != Obj.section_end())
1122 if (uint64_t SectionLoadAddress = L->getSectionLoadAddress(*RSec))
George Rimara25d3292017-05-27 18:10:23 +00001123 Ret.Address += SectionLoadAddress - RSec->getAddress();
George Rimar958b01a2017-05-15 11:45:28 +00001124
1125 if (CacheIt != Cache.end())
1126 CacheIt->second = Ret;
1127
George Rimar702dac62017-04-12 08:59:15 +00001128 return Ret;
1129}
1130
1131static bool isRelocScattered(const object::ObjectFile &Obj,
1132 const RelocationRef &Reloc) {
George Rimard4998b02017-04-13 09:52:50 +00001133 const MachOObjectFile *MachObj = dyn_cast<MachOObjectFile>(&Obj);
1134 if (!MachObj)
George Rimar702dac62017-04-12 08:59:15 +00001135 return false;
1136 // MachO also has relocations that point to sections and
1137 // scattered relocations.
George Rimar702dac62017-04-12 08:59:15 +00001138 auto RelocInfo = MachObj->getRelocation(Reloc.getRawDataRefImpl());
1139 return MachObj->isRelocationScattered(RelocInfo);
1140}
1141
Rafael Espindolac398e672017-07-19 22:27:28 +00001142ErrorPolicy DWARFContext::defaultErrorHandler(Error E) {
Jonas Devlieghere84e99262018-04-14 22:07:23 +00001143 WithColor::error() << toString(std::move(E)) << '\n';
George Rimar1af3cb22017-06-28 08:21:19 +00001144 return ErrorPolicy::Continue;
1145}
1146
Rafael Espindola87c3f4a92017-07-24 19:34:26 +00001147namespace {
1148struct DWARFSectionMap final : public DWARFSection {
1149 RelocAddrMap Relocs;
1150};
Rafael Espindola87c3f4a92017-07-24 19:34:26 +00001151
Rafael Espindolac398e672017-07-19 22:27:28 +00001152class DWARFObjInMemory final : public DWARFObject {
1153 bool IsLittleEndian;
1154 uint8_t AddressSize;
1155 StringRef FileName;
George Rimar6957ab52017-08-15 12:32:54 +00001156 const object::ObjectFile *Obj = nullptr;
George Rimare1c30f72017-08-15 15:54:43 +00001157 std::vector<SectionName> SectionNames;
Keno Fischerc780e8e2015-05-21 21:24:32 +00001158
Rafael Espindolac398e672017-07-19 22:27:28 +00001159 using TypeSectionMap = MapVector<object::SectionRef, DWARFSectionMap,
1160 std::map<object::SectionRef, unsigned>>;
Eric Christopher7370b552012-11-12 21:40:38 +00001161
Rafael Espindolac398e672017-07-19 22:27:28 +00001162 TypeSectionMap TypesSections;
1163 TypeSectionMap TypesDWOSections;
1164
1165 DWARFSectionMap InfoSection;
1166 DWARFSectionMap LocSection;
1167 DWARFSectionMap LineSection;
1168 DWARFSectionMap RangeSection;
James Henderson3fcc7452018-02-02 12:35:52 +00001169 DWARFSectionMap RnglistsSection;
Rafael Espindolac398e672017-07-19 22:27:28 +00001170 DWARFSectionMap StringOffsetSection;
1171 DWARFSectionMap InfoDWOSection;
1172 DWARFSectionMap LineDWOSection;
1173 DWARFSectionMap LocDWOSection;
1174 DWARFSectionMap StringOffsetDWOSection;
1175 DWARFSectionMap RangeDWOSection;
1176 DWARFSectionMap AddrSection;
1177 DWARFSectionMap AppleNamesSection;
1178 DWARFSectionMap AppleTypesSection;
1179 DWARFSectionMap AppleNamespacesSection;
1180 DWARFSectionMap AppleObjCSection;
Pavel Labath3c9a9182018-01-29 11:08:32 +00001181 DWARFSectionMap DebugNamesSection;
Rafael Espindolac398e672017-07-19 22:27:28 +00001182
1183 DWARFSectionMap *mapNameToDWARFSection(StringRef Name) {
1184 return StringSwitch<DWARFSectionMap *>(Name)
1185 .Case("debug_info", &InfoSection)
1186 .Case("debug_loc", &LocSection)
1187 .Case("debug_line", &LineSection)
1188 .Case("debug_str_offsets", &StringOffsetSection)
1189 .Case("debug_ranges", &RangeSection)
James Henderson3fcc7452018-02-02 12:35:52 +00001190 .Case("debug_rnglists", &RnglistsSection)
Rafael Espindolac398e672017-07-19 22:27:28 +00001191 .Case("debug_info.dwo", &InfoDWOSection)
1192 .Case("debug_loc.dwo", &LocDWOSection)
1193 .Case("debug_line.dwo", &LineDWOSection)
Pavel Labath3c9a9182018-01-29 11:08:32 +00001194 .Case("debug_names", &DebugNamesSection)
Rafael Espindolac398e672017-07-19 22:27:28 +00001195 .Case("debug_str_offsets.dwo", &StringOffsetDWOSection)
1196 .Case("debug_addr", &AddrSection)
1197 .Case("apple_names", &AppleNamesSection)
1198 .Case("apple_types", &AppleTypesSection)
1199 .Case("apple_namespaces", &AppleNamespacesSection)
1200 .Case("apple_namespac", &AppleNamespacesSection)
1201 .Case("apple_objc", &AppleObjCSection)
1202 .Default(nullptr);
1203 }
1204
1205 StringRef AbbrevSection;
1206 StringRef ARangeSection;
1207 StringRef DebugFrameSection;
1208 StringRef EHFrameSection;
1209 StringRef StringSection;
1210 StringRef MacinfoSection;
1211 StringRef PubNamesSection;
1212 StringRef PubTypesSection;
1213 StringRef GnuPubNamesSection;
1214 StringRef AbbrevDWOSection;
1215 StringRef StringDWOSection;
1216 StringRef GnuPubTypesSection;
1217 StringRef CUIndexSection;
1218 StringRef GdbIndexSection;
1219 StringRef TUIndexSection;
Paul Robinsonb6aa01c2018-01-25 22:02:36 +00001220 StringRef LineStringSection;
Rafael Espindolac398e672017-07-19 22:27:28 +00001221
1222 SmallVector<SmallString<32>, 4> UncompressedSections;
1223
1224 StringRef *mapSectionToMember(StringRef Name) {
1225 if (DWARFSection *Sec = mapNameToDWARFSection(Name))
1226 return &Sec->Data;
1227 return StringSwitch<StringRef *>(Name)
1228 .Case("debug_abbrev", &AbbrevSection)
1229 .Case("debug_aranges", &ARangeSection)
1230 .Case("debug_frame", &DebugFrameSection)
1231 .Case("eh_frame", &EHFrameSection)
1232 .Case("debug_str", &StringSection)
1233 .Case("debug_macinfo", &MacinfoSection)
1234 .Case("debug_pubnames", &PubNamesSection)
1235 .Case("debug_pubtypes", &PubTypesSection)
1236 .Case("debug_gnu_pubnames", &GnuPubNamesSection)
1237 .Case("debug_gnu_pubtypes", &GnuPubTypesSection)
1238 .Case("debug_abbrev.dwo", &AbbrevDWOSection)
1239 .Case("debug_str.dwo", &StringDWOSection)
1240 .Case("debug_cu_index", &CUIndexSection)
1241 .Case("debug_tu_index", &TUIndexSection)
1242 .Case("gdb_index", &GdbIndexSection)
Paul Robinsonb6aa01c2018-01-25 22:02:36 +00001243 .Case("debug_line_str", &LineStringSection)
Rafael Espindolac398e672017-07-19 22:27:28 +00001244 // Any more debug info sections go here.
1245 .Default(nullptr);
1246 }
1247
1248 /// If Sec is compressed section, decompresses and updates its contents
1249 /// provided by Data. Otherwise leaves it unchanged.
1250 Error maybeDecompress(const object::SectionRef &Sec, StringRef Name,
1251 StringRef &Data) {
1252 if (!Decompressor::isCompressed(Sec))
1253 return Error::success();
1254
1255 Expected<Decompressor> Decompressor =
1256 Decompressor::create(Name, Data, IsLittleEndian, AddressSize == 8);
1257 if (!Decompressor)
1258 return Decompressor.takeError();
1259
1260 SmallString<32> Out;
1261 if (auto Err = Decompressor->resizeAndDecompress(Out))
1262 return Err;
1263
1264 UncompressedSections.emplace_back(std::move(Out));
1265 Data = UncompressedSections.back();
1266
1267 return Error::success();
1268 }
1269
1270public:
1271 DWARFObjInMemory(const StringMap<std::unique_ptr<MemoryBuffer>> &Sections,
1272 uint8_t AddrSize, bool IsLittleEndian)
1273 : IsLittleEndian(IsLittleEndian) {
1274 for (const auto &SecIt : Sections) {
1275 if (StringRef *SectionData = mapSectionToMember(SecIt.first()))
1276 *SectionData = SecIt.second->getBuffer();
Alexey Samsonov068fc8a2013-04-23 10:17:34 +00001277 }
Rafael Espindolac398e672017-07-19 22:27:28 +00001278 }
1279 DWARFObjInMemory(const object::ObjectFile &Obj, const LoadedObjectInfo *L,
1280 function_ref<ErrorPolicy(Error)> HandleError)
1281 : IsLittleEndian(Obj.isLittleEndian()),
George Rimar6957ab52017-08-15 12:32:54 +00001282 AddressSize(Obj.getBytesInAddress()), FileName(Obj.getFileName()),
1283 Obj(&Obj) {
Alexey Samsonov068fc8a2013-04-23 10:17:34 +00001284
George Rimare1c30f72017-08-15 15:54:43 +00001285 StringMap<unsigned> SectionAmountMap;
Rafael Espindolac398e672017-07-19 22:27:28 +00001286 for (const SectionRef &Section : Obj.sections()) {
1287 StringRef Name;
1288 Section.getName(Name);
George Rimare1c30f72017-08-15 15:54:43 +00001289 ++SectionAmountMap[Name];
1290 SectionNames.push_back({ Name, true });
1291
Rafael Espindolac398e672017-07-19 22:27:28 +00001292 // Skip BSS and Virtual sections, they aren't interesting.
1293 if (Section.isBSS() || Section.isVirtual())
George Rimarfed9f092017-05-17 12:10:51 +00001294 continue;
Keno Fischerc780e8e2015-05-21 21:24:32 +00001295
Jonas Devlieghere8af23872017-09-26 14:22:35 +00001296 // Skip sections stripped by dsymutil.
1297 if (Section.isStripped())
1298 continue;
1299
Rafael Espindolac398e672017-07-19 22:27:28 +00001300 StringRef Data;
1301 section_iterator RelocatedSection = Section.getRelocatedSection();
1302 // Try to obtain an already relocated version of this section.
1303 // Else use the unrelocated section from the object file. We'll have to
1304 // apply relocations ourselves later.
1305 if (!L || !L->getLoadedSectionContents(*RelocatedSection, Data))
1306 Section.getContents(Data);
George Rimarfed9f092017-05-17 12:10:51 +00001307
Rafael Espindolac398e672017-07-19 22:27:28 +00001308 if (auto Err = maybeDecompress(Section, Name, Data)) {
1309 ErrorPolicy EP = HandleError(createError(
1310 "failed to decompress '" + Name + "', ", std::move(Err)));
George Rimar1af3cb22017-06-28 08:21:19 +00001311 if (EP == ErrorPolicy::Halt)
1312 return;
George Rimarfed9f092017-05-17 12:10:51 +00001313 continue;
1314 }
Rafael Espindolac398e672017-07-19 22:27:28 +00001315
1316 // Compressed sections names in GNU style starts from ".z",
1317 // at this point section is decompressed and we drop compression prefix.
1318 Name = Name.substr(
1319 Name.find_first_not_of("._z")); // Skip ".", "z" and "_" prefixes.
1320
1321 // Map platform specific debug section names to DWARF standard section
1322 // names.
1323 Name = Obj.mapDebugSectionName(Name);
1324
1325 if (StringRef *SectionData = mapSectionToMember(Name)) {
1326 *SectionData = Data;
1327 if (Name == "debug_ranges") {
1328 // FIXME: Use the other dwo range section when we emit it.
1329 RangeDWOSection.Data = Data;
1330 }
1331 } else if (Name == "debug_types") {
1332 // Find debug_types data by section rather than name as there are
1333 // multiple, comdat grouped, debug_types sections.
1334 TypesSections[Section].Data = Data;
1335 } else if (Name == "debug_types.dwo") {
1336 TypesDWOSections[Section].Data = Data;
1337 }
1338
1339 if (RelocatedSection == Obj.section_end())
1340 continue;
1341
1342 StringRef RelSecName;
1343 StringRef RelSecData;
1344 RelocatedSection->getName(RelSecName);
1345
1346 // If the section we're relocating was relocated already by the JIT,
1347 // then we used the relocated version above, so we do not need to process
1348 // relocations for it now.
1349 if (L && L->getLoadedSectionContents(*RelocatedSection, RelSecData))
1350 continue;
1351
1352 // In Mach-o files, the relocations do not need to be applied if
1353 // there is no load offset to apply. The value read at the
1354 // relocation point already factors in the section address
1355 // (actually applying the relocations will produce wrong results
1356 // as the section address will be added twice).
1357 if (!L && isa<MachOObjectFile>(&Obj))
1358 continue;
1359
1360 RelSecName = RelSecName.substr(
1361 RelSecName.find_first_not_of("._z")); // Skip . and _ prefixes.
1362
1363 // TODO: Add support for relocations in other sections as needed.
1364 // Record relocations for the debug_info and debug_line sections.
1365 DWARFSectionMap *Sec = mapNameToDWARFSection(RelSecName);
1366 RelocAddrMap *Map = Sec ? &Sec->Relocs : nullptr;
1367 if (!Map) {
1368 // Find debug_types relocs by section rather than name as there are
1369 // multiple, comdat grouped, debug_types sections.
1370 if (RelSecName == "debug_types")
1371 Map =
1372 &static_cast<DWARFSectionMap &>(TypesSections[*RelocatedSection])
1373 .Relocs;
1374 else if (RelSecName == "debug_types.dwo")
1375 Map = &static_cast<DWARFSectionMap &>(
1376 TypesDWOSections[*RelocatedSection])
1377 .Relocs;
1378 else
1379 continue;
1380 }
1381
1382 if (Section.relocation_begin() == Section.relocation_end())
1383 continue;
1384
1385 // Symbol to [address, section index] cache mapping.
1386 std::map<SymbolRef, SymInfo> AddrCache;
1387 for (const RelocationRef &Reloc : Section.relocations()) {
1388 // FIXME: it's not clear how to correctly handle scattered
1389 // relocations.
1390 if (isRelocScattered(Obj, Reloc))
1391 continue;
1392
1393 Expected<SymInfo> SymInfoOrErr =
1394 getSymbolInfo(Obj, Reloc, L, AddrCache);
1395 if (!SymInfoOrErr) {
1396 if (HandleError(SymInfoOrErr.takeError()) == ErrorPolicy::Halt)
1397 return;
1398 continue;
1399 }
1400
1401 object::RelocVisitor V(Obj);
1402 uint64_t Val = V.visit(Reloc.getType(), Reloc, SymInfoOrErr->Address);
1403 if (V.error()) {
1404 SmallString<32> Type;
1405 Reloc.getTypeName(Type);
1406 ErrorPolicy EP = HandleError(
1407 createError("failed to compute relocation: " + Type + ", ",
1408 errorCodeToError(object_error::parse_failed)));
1409 if (EP == ErrorPolicy::Halt)
1410 return;
1411 continue;
1412 }
1413 RelocAddrEntry Rel = {SymInfoOrErr->SectionIndex, Val};
1414 Map->insert({Reloc.getOffset(), Rel});
1415 }
Eric Christopher7370b552012-11-12 21:40:38 +00001416 }
George Rimare1c30f72017-08-15 15:54:43 +00001417
1418 for (SectionName &S : SectionNames)
1419 if (SectionAmountMap[S.Name] > 1)
1420 S.IsNameUnique = false;
Eric Christopher7370b552012-11-12 21:40:38 +00001421 }
Eric Christopher7370b552012-11-12 21:40:38 +00001422
Rafael Espindolac398e672017-07-19 22:27:28 +00001423 Optional<RelocAddrEntry> find(const DWARFSection &S,
1424 uint64_t Pos) const override {
1425 auto &Sec = static_cast<const DWARFSectionMap &>(S);
1426 RelocAddrMap::const_iterator AI = Sec.Relocs.find(Pos);
1427 if (AI == Sec.Relocs.end())
1428 return None;
1429 return AI->second;
Chris Bieneman2e752db2017-01-20 19:03:14 +00001430 }
Rafael Espindolac398e672017-07-19 22:27:28 +00001431
George Rimar6957ab52017-08-15 12:32:54 +00001432 const object::ObjectFile *getFile() const override { return Obj; }
1433
George Rimare1c30f72017-08-15 15:54:43 +00001434 ArrayRef<SectionName> getSectionNames() const override {
1435 return SectionNames;
1436 }
1437
Rafael Espindolac398e672017-07-19 22:27:28 +00001438 bool isLittleEndian() const override { return IsLittleEndian; }
1439 StringRef getAbbrevDWOSection() const override { return AbbrevDWOSection; }
1440 const DWARFSection &getLineDWOSection() const override {
1441 return LineDWOSection;
1442 }
1443 const DWARFSection &getLocDWOSection() const override {
1444 return LocDWOSection;
1445 }
1446 StringRef getStringDWOSection() const override { return StringDWOSection; }
1447 const DWARFSection &getStringOffsetDWOSection() const override {
1448 return StringOffsetDWOSection;
1449 }
1450 const DWARFSection &getRangeDWOSection() const override {
1451 return RangeDWOSection;
1452 }
1453 const DWARFSection &getAddrSection() const override { return AddrSection; }
1454 StringRef getCUIndexSection() const override { return CUIndexSection; }
1455 StringRef getGdbIndexSection() const override { return GdbIndexSection; }
1456 StringRef getTUIndexSection() const override { return TUIndexSection; }
1457
1458 // DWARF v5
1459 const DWARFSection &getStringOffsetSection() const override {
1460 return StringOffsetSection;
1461 }
Paul Robinsonb6aa01c2018-01-25 22:02:36 +00001462 StringRef getLineStringSection() const override { return LineStringSection; }
Rafael Espindolac398e672017-07-19 22:27:28 +00001463
1464 // Sections for DWARF5 split dwarf proposal.
1465 const DWARFSection &getInfoDWOSection() const override {
1466 return InfoDWOSection;
1467 }
1468 void forEachTypesDWOSections(
1469 function_ref<void(const DWARFSection &)> F) const override {
1470 for (auto &P : TypesDWOSections)
1471 F(P.second);
1472 }
1473
1474 StringRef getAbbrevSection() const override { return AbbrevSection; }
1475 const DWARFSection &getLocSection() const override { return LocSection; }
1476 StringRef getARangeSection() const override { return ARangeSection; }
1477 StringRef getDebugFrameSection() const override { return DebugFrameSection; }
1478 StringRef getEHFrameSection() const override { return EHFrameSection; }
1479 const DWARFSection &getLineSection() const override { return LineSection; }
1480 StringRef getStringSection() const override { return StringSection; }
1481 const DWARFSection &getRangeSection() const override { return RangeSection; }
James Henderson3fcc7452018-02-02 12:35:52 +00001482 const DWARFSection &getRnglistsSection() const override {
1483 return RnglistsSection;
1484 }
Rafael Espindolac398e672017-07-19 22:27:28 +00001485 StringRef getMacinfoSection() const override { return MacinfoSection; }
1486 StringRef getPubNamesSection() const override { return PubNamesSection; }
1487 StringRef getPubTypesSection() const override { return PubTypesSection; }
1488 StringRef getGnuPubNamesSection() const override {
1489 return GnuPubNamesSection;
1490 }
1491 StringRef getGnuPubTypesSection() const override {
1492 return GnuPubTypesSection;
1493 }
1494 const DWARFSection &getAppleNamesSection() const override {
1495 return AppleNamesSection;
1496 }
1497 const DWARFSection &getAppleTypesSection() const override {
1498 return AppleTypesSection;
1499 }
1500 const DWARFSection &getAppleNamespacesSection() const override {
1501 return AppleNamespacesSection;
1502 }
1503 const DWARFSection &getAppleObjCSection() const override {
1504 return AppleObjCSection;
1505 }
Pavel Labath3c9a9182018-01-29 11:08:32 +00001506 const DWARFSection &getDebugNamesSection() const override {
1507 return DebugNamesSection;
1508 }
Rafael Espindolac398e672017-07-19 22:27:28 +00001509
1510 StringRef getFileName() const override { return FileName; }
1511 uint8_t getAddressSize() const override { return AddressSize; }
1512 const DWARFSection &getInfoSection() const override { return InfoSection; }
1513 void forEachTypesSections(
1514 function_ref<void(const DWARFSection &)> F) const override {
1515 for (auto &P : TypesSections)
1516 F(P.second);
1517 }
1518};
Benjamin Kramer49a49fe2017-08-20 13:03:48 +00001519} // namespace
Rafael Espindolac398e672017-07-19 22:27:28 +00001520
1521std::unique_ptr<DWARFContext>
1522DWARFContext::create(const object::ObjectFile &Obj, const LoadedObjectInfo *L,
David Blaikiee5adb682017-07-30 01:34:08 +00001523 function_ref<ErrorPolicy(Error)> HandleError,
1524 std::string DWPName) {
Reid Kleckner388f8802017-07-19 23:42:53 +00001525 auto DObj = llvm::make_unique<DWARFObjInMemory>(Obj, L, HandleError);
David Blaikiee5adb682017-07-30 01:34:08 +00001526 return llvm::make_unique<DWARFContext>(std::move(DObj), std::move(DWPName));
Chris Bieneman2e752db2017-01-20 19:03:14 +00001527}
1528
Rafael Espindolac398e672017-07-19 22:27:28 +00001529std::unique_ptr<DWARFContext>
1530DWARFContext::create(const StringMap<std::unique_ptr<MemoryBuffer>> &Sections,
1531 uint8_t AddrSize, bool isLittleEndian) {
Reid Kleckner388f8802017-07-19 23:42:53 +00001532 auto DObj =
1533 llvm::make_unique<DWARFObjInMemory>(Sections, AddrSize, isLittleEndian);
David Blaikiee5adb682017-07-30 01:34:08 +00001534 return llvm::make_unique<DWARFContext>(std::move(DObj), "");
Rafael Espindola77e87b32017-07-07 05:36:53 +00001535}
Reid Klecknera0587362017-08-29 21:41:21 +00001536
1537Error DWARFContext::loadRegisterInfo(const object::ObjectFile &Obj) {
1538 // Detect the architecture from the object file. We usually don't need OS
1539 // info to lookup a target and create register info.
1540 Triple TT;
1541 TT.setArch(Triple::ArchType(Obj.getArch()));
1542 TT.setVendor(Triple::UnknownVendor);
1543 TT.setOS(Triple::UnknownOS);
1544 std::string TargetLookupError;
1545 const Target *TheTarget =
1546 TargetRegistry::lookupTarget(TT.str(), TargetLookupError);
1547 if (!TargetLookupError.empty())
1548 return make_error<StringError>(TargetLookupError, inconvertibleErrorCode());
1549 RegInfo.reset(TheTarget->createMCRegInfo(TT.str()));
1550 return Error::success();
1551}