blob: 3a974dddc4eb93dd73f84690311d55bf0c7cec3f [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"
Benjamin Kramera6002fd2011-09-14 01:09:52 +000047#include "llvm/Support/raw_ostream.h"
Benjamin Kramer2602ca62011-09-15 20:43:22 +000048#include <algorithm>
Eugene Zelenko28db7e62017-03-01 01:14:23 +000049#include <cstdint>
Greg Claytonc7695a82017-05-02 20:28:33 +000050#include <map>
Eugene Zelenko28db7e62017-03-01 01:14:23 +000051#include <string>
52#include <utility>
53#include <vector>
54
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000055using namespace llvm;
Benjamin Kramer6dda0322011-09-15 18:02:20 +000056using namespace dwarf;
Rafael Espindola4f60a382013-05-30 03:05:14 +000057using namespace object;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000058
Chandler Carruthe96dd892014-04-21 22:55:11 +000059#define DEBUG_TYPE "dwarf"
60
Eugene Zelenko2db0cfa2017-06-23 21:57:40 +000061using DWARFLineTable = DWARFDebugLine::LineTable;
62using FileLineInfoKind = DILineInfoSpecifier::FileLineInfoKind;
63using FunctionNameKind = DILineInfoSpecifier::FunctionNameKind;
Eric Christopher494109b2012-10-16 23:46:25 +000064
Reid Klecknera0587362017-08-29 21:41:21 +000065DWARFContext::DWARFContext(std::unique_ptr<const DWARFObject> DObj,
66 std::string DWPName)
67 : DIContext(CK_DWARF), DWPName(std::move(DWPName)), DObj(std::move(DObj)) {}
68
69DWARFContext::~DWARFContext() = default;
70
Adrian Prantl3dcd1222017-09-13 18:22:59 +000071/// Dump the UUID load command.
72static void dumpUUID(raw_ostream &OS, const ObjectFile &Obj) {
73 auto *MachO = dyn_cast<MachOObjectFile>(&Obj);
74 if (!MachO)
75 return;
76 for (auto LC : MachO->load_commands()) {
77 raw_ostream::uuid_t UUID;
78 if (LC.C.cmd == MachO::LC_UUID) {
79 if (LC.C.cmdsize < sizeof(UUID) + sizeof(LC.C)) {
80 OS << "error: UUID load command is too short.\n";
81 return;
82 }
83 OS << "UUID: ";
84 memcpy(&UUID, LC.Ptr+sizeof(LC.C), sizeof(UUID));
85 OS.write_uuid(UUID);
Adrian Prantl146ed402018-01-05 21:44:17 +000086 Triple T = MachO->getArchTriple();
87 OS << " (" << T.getArchName() << ')';
Adrian Prantl3dcd1222017-09-13 18:22:59 +000088 OS << ' ' << MachO->getFileName() << '\n';
89 }
90 }
91}
92
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +000093using ContributionCollection =
94 std::vector<Optional<StrOffsetsContributionDescriptor>>;
95
96// Collect all the contributions to the string offsets table from all units,
97// sort them by their starting offsets and remove duplicates.
98static ContributionCollection
99collectContributionData(DWARFContext::cu_iterator_range CUs,
100 DWARFContext::tu_section_iterator_range TUSs) {
101 ContributionCollection Contributions;
102 for (const auto &CU : CUs)
103 Contributions.push_back(CU->getStringOffsetsTableContribution());
104 for (const auto &TUS : TUSs)
105 for (const auto &TU : TUS)
106 Contributions.push_back(TU->getStringOffsetsTableContribution());
107
108 // Sort the contributions so that any invalid ones are placed at
109 // the start of the contributions vector. This way they are reported
110 // first.
111 std::sort(Contributions.begin(), Contributions.end(),
112 [](const Optional<StrOffsetsContributionDescriptor> &L,
113 const Optional<StrOffsetsContributionDescriptor> &R) {
114 if (L && R) return L->Base < R->Base;
115 return R.hasValue();
116 });
117
118 // Uniquify contributions, as it is possible that units (specifically
119 // type units in dwo or dwp files) share contributions. We don't want
120 // to report them more than once.
121 Contributions.erase(
122 std::unique(Contributions.begin(), Contributions.end(),
123 [](const Optional<StrOffsetsContributionDescriptor> &L,
124 const Optional<StrOffsetsContributionDescriptor> &R) {
125 if (L && R)
126 return L->Base == R->Base && L->Size == R->Size;
127 return false;
128 }),
129 Contributions.end());
130 return Contributions;
131}
132
133static void dumpDWARFv5StringOffsetsSection(
134 raw_ostream &OS, StringRef SectionName, const DWARFObject &Obj,
135 const DWARFSection &StringOffsetsSection, StringRef StringSection,
136 DWARFContext::cu_iterator_range CUs,
137 DWARFContext::tu_section_iterator_range TUSs, bool LittleEndian) {
138 auto Contributions = collectContributionData(CUs, TUSs);
Rafael Espindolac398e672017-07-19 22:27:28 +0000139 DWARFDataExtractor StrOffsetExt(Obj, StringOffsetsSection, LittleEndian, 0);
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000140 DataExtractor StrData(StringSection, LittleEndian, 0);
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000141 uint64_t SectionSize = StringOffsetsSection.Data.size();
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000142 uint32_t Offset = 0;
143 for (auto &Contribution : Contributions) {
144 // Report an ill-formed contribution.
145 if (!Contribution) {
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000146 OS << "error: invalid contribution to string offsets table in section ."
147 << SectionName << ".\n";
148 return;
149 }
150
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000151 dwarf::DwarfFormat Format = Contribution->getFormat();
152 uint16_t Version = Contribution->getVersion();
153 uint64_t ContributionHeader = Contribution->Base;
154 // In DWARF v5 there is a contribution header that immediately precedes
155 // the string offsets base (the location we have previously retrieved from
156 // the CU DIE's DW_AT_str_offsets attribute). The header is located either
157 // 8 or 16 bytes before the base, depending on the contribution's format.
158 if (Version >= 5)
159 ContributionHeader -= Format == DWARF32 ? 8 : 16;
160
161 // Detect overlapping contributions.
162 if (Offset > ContributionHeader) {
163 OS << "error: overlapping contributions to string offsets table in "
164 "section ."
165 << SectionName << ".\n";
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000166 return;
167 }
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000168 // Report a gap in the table.
169 if (Offset < ContributionHeader) {
170 OS << format("0x%8.8x: Gap, length = ", Offset);
171 OS << (ContributionHeader - Offset) << "\n";
172 }
Wolfgang Piebb4ba1aa2017-12-22 01:12:24 +0000173 OS << format("0x%8.8x: ", (uint32_t)ContributionHeader);
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000174 OS << "Contribution size = " << Contribution->Size
175 << ", Format = " << (Format == DWARF32 ? "DWARF32" : "DWARF64")
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000176 << ", Version = " << Version << "\n";
177
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000178 Offset = Contribution->Base;
179 unsigned EntrySize = Contribution->getDwarfOffsetByteSize();
180 while (Offset - Contribution->Base < Contribution->Size) {
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000181 OS << format("0x%8.8x: ", Offset);
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000182 // FIXME: We can only extract strings if the offset fits in 32 bits.
Paul Robinson17536b92017-06-29 16:52:08 +0000183 uint64_t StringOffset =
184 StrOffsetExt.getRelocatedValue(EntrySize, &Offset);
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000185 // Extract the string if we can and display it. Otherwise just report
186 // the offset.
187 if (StringOffset <= std::numeric_limits<uint32_t>::max()) {
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000188 uint32_t StringOffset32 = (uint32_t)StringOffset;
Simon Dardisb1b52c02017-08-07 16:08:11 +0000189 OS << format("%8.8x ", StringOffset32);
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000190 const char *S = StrData.getCStr(&StringOffset32);
191 if (S)
192 OS << format("\"%s\"", S);
193 } else
Simon Dardisec4ea992017-08-07 13:30:03 +0000194 OS << format("%16.16" PRIx64 " ", StringOffset);
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000195 OS << "\n";
196 }
197 }
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000198 // Report a gap at the end of the table.
199 if (Offset < SectionSize) {
200 OS << format("0x%8.8x: Gap, length = ", Offset);
201 OS << (SectionSize - Offset) << "\n";
202 }
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000203}
204
205// Dump a DWARF string offsets section. This may be a DWARF v5 formatted
206// string offsets section, where each compile or type unit contributes a
207// number of entries (string offsets), with each contribution preceded by
208// a header containing size and version number. Alternatively, it may be a
209// monolithic series of string offsets, as generated by the pre-DWARF v5
210// implementation of split DWARF.
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000211static void dumpStringOffsetsSection(
212 raw_ostream &OS, StringRef SectionName, const DWARFObject &Obj,
213 const DWARFSection &StringOffsetsSection, StringRef StringSection,
214 DWARFContext::cu_iterator_range CUs,
215 DWARFContext::tu_section_iterator_range TUSs, bool LittleEndian,
216 unsigned MaxVersion) {
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000217 // If we have at least one (compile or type) unit with DWARF v5 or greater,
218 // we assume that the section is formatted like a DWARF v5 string offsets
219 // section.
220 if (MaxVersion >= 5)
Rafael Espindolac398e672017-07-19 22:27:28 +0000221 dumpDWARFv5StringOffsetsSection(OS, SectionName, Obj, StringOffsetsSection,
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000222 StringSection, CUs, TUSs, LittleEndian);
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000223 else {
224 DataExtractor strOffsetExt(StringOffsetsSection.Data, LittleEndian, 0);
225 uint32_t offset = 0;
226 uint64_t size = StringOffsetsSection.Data.size();
227 // Ensure that size is a multiple of the size of an entry.
228 if (size & ((uint64_t)(sizeof(uint32_t) - 1))) {
229 OS << "error: size of ." << SectionName << " is not a multiple of "
230 << sizeof(uint32_t) << ".\n";
231 size &= -(uint64_t)sizeof(uint32_t);
232 }
233 DataExtractor StrData(StringSection, LittleEndian, 0);
234 while (offset < size) {
235 OS << format("0x%8.8x: ", offset);
236 uint32_t StringOffset = strOffsetExt.getU32(&offset);
237 OS << format("%8.8x ", StringOffset);
238 const char *S = StrData.getCStr(&StringOffset);
239 if (S)
240 OS << format("\"%s\"", S);
241 OS << "\n";
242 }
243 }
244}
245
Paul Robinson63811a42017-11-22 15:33:17 +0000246// We want to supply the Unit associated with a .debug_line[.dwo] table when
247// we dump it, if possible, but still dump the table even if there isn't a Unit.
248// Therefore, collect up handles on all the Units that point into the
249// line-table section.
250typedef std::map<uint64_t, DWARFUnit *> LineToUnitMap;
251
252static LineToUnitMap
253buildLineToUnitMap(DWARFContext::cu_iterator_range CUs,
254 DWARFContext::tu_section_iterator_range TUSections) {
255 LineToUnitMap LineToUnit;
256 for (const auto &CU : CUs)
257 if (auto CUDIE = CU->getUnitDIE())
258 if (auto StmtOffset = toSectionOffset(CUDIE.find(DW_AT_stmt_list)))
259 LineToUnit.insert(std::make_pair(*StmtOffset, &*CU));
260 for (const auto &TUS : TUSections)
261 for (const auto &TU : TUS)
262 if (auto TUDIE = TU->getUnitDIE())
263 if (auto StmtOffset = toSectionOffset(TUDIE.find(DW_AT_stmt_list)))
264 LineToUnit.insert(std::make_pair(*StmtOffset, &*TU));
265 return LineToUnit;
266}
267
Adrian Prantl057d3362017-09-15 23:04:04 +0000268void DWARFContext::dump(
269 raw_ostream &OS, DIDumpOptions DumpOpts,
270 std::array<Optional<uint64_t>, DIDT_ID_Count> DumpOffsets) {
271
272 Optional<uint64_t> DumpOffset;
Adrian Prantl7bc1b282017-09-11 22:59:45 +0000273 uint64_t DumpType = DumpOpts.DumpType;
Adrian Prantlf4bc1f72017-06-01 18:18:23 +0000274
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000275 StringRef Extension = sys::path::extension(DObj->getFileName());
276 bool IsDWO = (Extension == ".dwo") || (Extension == ".dwp");
277
278 // Print UUID header.
Adrian Prantl3dcd1222017-09-13 18:22:59 +0000279 const auto *ObjFile = DObj->getFile();
Adrian Prantl3dcd1222017-09-13 18:22:59 +0000280 if (DumpType & DIDT_UUID)
281 dumpUUID(OS, *ObjFile);
282
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000283 // Print a header for each explicitly-requested section.
284 // Otherwise just print one for non-empty sections.
Adrian Prantl057d3362017-09-15 23:04:04 +0000285 // Only print empty .dwo section headers when dumping a .dwo file.
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000286 bool Explicit = DumpType != DIDT_All && !IsDWO;
Adrian Prantl057d3362017-09-15 23:04:04 +0000287 bool ExplicitDWO = Explicit && IsDWO;
288 auto shouldDump = [&](bool Explicit, const char *Name, unsigned ID,
289 StringRef Section) {
290 DumpOffset = DumpOffsets[ID];
291 unsigned Mask = 1U << ID;
292 bool Should = (DumpType & Mask) && (Explicit || !Section.empty());
Adrian Prantl84168022017-09-15 17:39:50 +0000293 if (Should)
Adrian Prantl057d3362017-09-15 23:04:04 +0000294 OS << "\n" << Name << " contents:\n";
Adrian Prantl84168022017-09-15 17:39:50 +0000295 return Should;
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000296 };
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000297
298 // Dump individual sections.
Adrian Prantl057d3362017-09-15 23:04:04 +0000299 if (shouldDump(Explicit, ".debug_abbrev", DIDT_ID_DebugAbbrev,
300 DObj->getAbbrevSection()))
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000301 getDebugAbbrev()->dump(OS);
Adrian Prantl057d3362017-09-15 23:04:04 +0000302 if (shouldDump(ExplicitDWO, ".debug_abbrev.dwo", DIDT_ID_DebugAbbrev,
303 DObj->getAbbrevDWOSection()))
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000304 getDebugAbbrevDWO()->dump(OS);
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000305
Adrian Prantl057d3362017-09-15 23:04:04 +0000306 auto dumpDebugInfo = [&](bool IsExplicit, const char *Name,
307 DWARFSection Section, cu_iterator_range CUs) {
308 if (shouldDump(IsExplicit, Name, DIDT_ID_DebugInfo, Section.Data)) {
Adrian Prantlc8d86532017-09-18 21:44:40 +0000309 if (DumpOffset)
Adrian Prantld3f9f212017-09-20 17:44:00 +0000310 getDIEForOffset(DumpOffset.getValue())
311 .dump(OS, 0, DumpOpts.noImplicitRecursion());
Adrian Prantlc8d86532017-09-18 21:44:40 +0000312 else
313 for (const auto &CU : CUs)
Adrian Prantl057d3362017-09-15 23:04:04 +0000314 CU->dump(OS, DumpOpts);
315 }
316 };
317 dumpDebugInfo(Explicit, ".debug_info", DObj->getInfoSection(),
318 compile_units());
319 dumpDebugInfo(ExplicitDWO, ".debug_info.dwo", DObj->getInfoDWOSection(),
320 dwo_compile_units());
David Blaikie622dce42014-01-08 23:29:59 +0000321
Adrian Prantl099d7e42017-09-16 16:58:18 +0000322 auto dumpDebugType = [&](const char *Name,
323 tu_section_iterator_range TUSections) {
324 OS << '\n' << Name << " contents:\n";
325 DumpOffset = DumpOffsets[DIDT_ID_DebugTypes];
326 for (const auto &TUS : TUSections)
327 for (const auto &TU : TUS)
328 if (DumpOffset)
Adrian Prantld3f9f212017-09-20 17:44:00 +0000329 TU->getDIEForOffset(*DumpOffset)
330 .dump(OS, 0, DumpOpts.noImplicitRecursion());
Adrian Prantl099d7e42017-09-16 16:58:18 +0000331 else
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000332 TU->dump(OS, DumpOpts);
Adrian Prantl099d7e42017-09-16 16:58:18 +0000333 };
334 if ((DumpType & DIDT_DebugTypes)) {
335 if (Explicit || getNumTypeUnits())
336 dumpDebugType(".debug_types", type_unit_sections());
337 if (ExplicitDWO || getNumDWOTypeUnits())
338 dumpDebugType(".debug_types.dwo", dwo_type_unit_sections());
David Blaikie03c089c2013-09-23 22:44:47 +0000339 }
340
Adrian Prantl057d3362017-09-15 23:04:04 +0000341 if (shouldDump(Explicit, ".debug_loc", DIDT_ID_DebugLoc,
Adrian Prantl84168022017-09-15 17:39:50 +0000342 DObj->getLocSection().Data)) {
Jonas Devlieghere622c5632017-09-27 09:33:36 +0000343 getDebugLoc()->dump(OS, getRegisterInfo(), DumpOffset);
David Blaikie18e73502013-06-19 21:37:13 +0000344 }
Adrian Prantl057d3362017-09-15 23:04:04 +0000345 if (shouldDump(ExplicitDWO, ".debug_loc.dwo", DIDT_ID_DebugLoc,
Adrian Prantl84168022017-09-15 17:39:50 +0000346 DObj->getLocDWOSection().Data)) {
Jonas Devlieghere622c5632017-09-27 09:33:36 +0000347 getDebugLocDWO()->dump(OS, getRegisterInfo(), DumpOffset);
David Blaikie9c550ac2014-03-25 01:44:02 +0000348 }
349
Jonas Devliegherec0a758d2017-09-18 14:15:57 +0000350 if (shouldDump(Explicit, ".debug_frame", DIDT_ID_DebugFrame,
Adrian Prantl62528e62017-09-21 18:52:03 +0000351 DObj->getDebugFrameSection()))
Rafael Auler86fb7bf2018-03-08 00:46:53 +0000352 getDebugFrame()->dump(OS, getRegisterInfo(), DumpOffset);
Jonas Devliegherec0a758d2017-09-18 14:15:57 +0000353
354 if (shouldDump(Explicit, ".eh_frame", DIDT_ID_DebugFrame,
Adrian Prantl62528e62017-09-21 18:52:03 +0000355 DObj->getEHFrameSection()))
Rafael Auler86fb7bf2018-03-08 00:46:53 +0000356 getEHFrame()->dump(OS, getRegisterInfo(), DumpOffset);
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000357
Adrian Prantl7bc1b282017-09-11 22:59:45 +0000358 if (DumpType & DIDT_DebugMacro) {
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000359 if (Explicit || !getDebugMacro()->empty()) {
360 OS << "\n.debug_macinfo contents:\n";
361 getDebugMacro()->dump(OS);
362 }
Amjad Aboude59cc3e2015-11-12 09:38:54 +0000363 }
364
Adrian Prantl057d3362017-09-15 23:04:04 +0000365 if (shouldDump(Explicit, ".debug_aranges", DIDT_ID_DebugAranges,
Adrian Prantl84168022017-09-15 17:39:50 +0000366 DObj->getARangeSection())) {
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000367 uint32_t offset = 0;
Rafael Espindolac398e672017-07-19 22:27:28 +0000368 DataExtractor arangesData(DObj->getARangeSection(), isLittleEndian(), 0);
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000369 DWARFDebugArangeSet set;
370 while (set.extract(arangesData, &offset))
371 set.dump(OS);
372 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000373
Adrian Prantl057d3362017-09-15 23:04:04 +0000374 if (shouldDump(Explicit, ".debug_line", DIDT_ID_DebugLine,
Adrian Prantl84168022017-09-15 17:39:50 +0000375 DObj->getLineSection().Data)) {
Paul Robinson63811a42017-11-22 15:33:17 +0000376 LineToUnitMap LineToUnit =
377 buildLineToUnitMap(compile_units(), type_unit_sections());
378 unsigned Offset = 0;
379 DWARFDataExtractor LineData(*DObj, DObj->getLineSection(), isLittleEndian(),
380 0);
381 while (Offset < LineData.getData().size()) {
382 DWARFUnit *U = nullptr;
383 auto It = LineToUnit.find(Offset);
Paul Robinson511b54c2017-11-22 15:48:30 +0000384 if (It != LineToUnit.end())
Paul Robinson63811a42017-11-22 15:33:17 +0000385 U = It->second;
Paul Robinson511b54c2017-11-22 15:48:30 +0000386 LineData.setAddressSize(U ? U->getAddressByteSize() : 0);
Paul Robinson63811a42017-11-22 15:33:17 +0000387 DWARFDebugLine::LineTable LineTable;
388 if (DumpOffset && Offset != *DumpOffset) {
389 // Find the size of this part of the line table section and skip it.
390 unsigned OldOffset = Offset;
Paul Robinsonbf750c82018-01-29 20:57:43 +0000391 LineTable.Prologue.parse(LineData, &Offset, *this, U);
Paul Robinson63811a42017-11-22 15:33:17 +0000392 Offset = OldOffset + LineTable.Prologue.TotalLength +
393 LineTable.Prologue.sizeofTotalLength();
Alexey Samsonov7a18c062015-05-19 21:54:32 +0000394 continue;
Paul Robinson63811a42017-11-22 15:33:17 +0000395 }
396 // Verbose dumping is done during parsing and not on the intermediate
397 // representation.
398 OS << "debug_line[" << format("0x%8.8x", Offset) << "]\n";
Paul Robinsonab69b472017-12-01 18:25:30 +0000399 unsigned OldOffset = Offset;
Paul Robinson63811a42017-11-22 15:33:17 +0000400 if (DumpOpts.Verbose) {
Paul Robinsonbf750c82018-01-29 20:57:43 +0000401 LineTable.parse(LineData, &Offset, *this, U, &OS);
Paul Robinson63811a42017-11-22 15:33:17 +0000402 } else {
Paul Robinsonbf750c82018-01-29 20:57:43 +0000403 LineTable.parse(LineData, &Offset, *this, U);
Paul Robinson0a227092018-02-05 20:43:15 +0000404 LineTable.dump(OS, DIDumpOptions());
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000405 }
Paul Robinsonab69b472017-12-01 18:25:30 +0000406 // Check for unparseable prologue, to avoid infinite loops.
407 if (OldOffset == Offset)
408 break;
Benjamin Kramer6dda0322011-09-15 18:02:20 +0000409 }
410 }
Benjamin Kramer07d4b1c2011-09-15 16:57:13 +0000411
Adrian Prantl057d3362017-09-15 23:04:04 +0000412 if (shouldDump(ExplicitDWO, ".debug_line.dwo", DIDT_ID_DebugLine,
Adrian Prantl84168022017-09-15 17:39:50 +0000413 DObj->getLineDWOSection().Data)) {
Paul Robinson63811a42017-11-22 15:33:17 +0000414 LineToUnitMap LineToUnit =
415 buildLineToUnitMap(dwo_compile_units(), dwo_type_unit_sections());
416 unsigned Offset = 0;
417 DWARFDataExtractor LineData(*DObj, DObj->getLineDWOSection(),
418 isLittleEndian(), 0);
419 while (Offset < LineData.getData().size()) {
420 DWARFUnit *U = nullptr;
421 auto It = LineToUnit.find(Offset);
422 if (It != LineToUnit.end())
423 U = It->second;
424 DWARFDebugLine::LineTable LineTable;
Paul Robinson6ca1dd62017-11-22 18:23:55 +0000425 unsigned OldOffset = Offset;
Paul Robinsonbf750c82018-01-29 20:57:43 +0000426 if (!LineTable.Prologue.parse(LineData, &Offset, *this, U))
Paul Robinson63811a42017-11-22 15:33:17 +0000427 break;
Paul Robinson6ca1dd62017-11-22 18:23:55 +0000428 if (!DumpOffset || OldOffset == *DumpOffset)
Paul Robinson0a227092018-02-05 20:43:15 +0000429 LineTable.dump(OS, DumpOpts);
Alexey Samsonov110d5952014-04-30 00:09:19 +0000430 }
David Blaikie1d4736e2014-02-24 23:58:54 +0000431 }
432
Adrian Prantl057d3362017-09-15 23:04:04 +0000433 if (shouldDump(Explicit, ".debug_cu_index", DIDT_ID_DebugCUIndex,
Adrian Prantl84168022017-09-15 17:39:50 +0000434 DObj->getCUIndexSection())) {
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000435 getCUIndex().dump(OS);
436 }
437
Adrian Prantl057d3362017-09-15 23:04:04 +0000438 if (shouldDump(Explicit, ".debug_tu_index", DIDT_ID_DebugTUIndex,
Adrian Prantl84168022017-09-15 17:39:50 +0000439 DObj->getTUIndexSection())) {
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000440 getTUIndex().dump(OS);
441 }
442
Adrian Prantl057d3362017-09-15 23:04:04 +0000443 if (shouldDump(Explicit, ".debug_str", DIDT_ID_DebugStr,
Adrian Prantl84168022017-09-15 17:39:50 +0000444 DObj->getStringSection())) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000445 DataExtractor strData(DObj->getStringSection(), isLittleEndian(), 0);
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000446 uint32_t offset = 0;
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000447 uint32_t strOffset = 0;
448 while (const char *s = strData.getCStr(&offset)) {
449 OS << format("0x%8.8x: \"%s\"\n", strOffset, s);
450 strOffset = offset;
451 }
Benjamin Kramer07d4b1c2011-09-15 16:57:13 +0000452 }
Adrian Prantl057d3362017-09-15 23:04:04 +0000453 if (shouldDump(ExplicitDWO, ".debug_str.dwo", DIDT_ID_DebugStr,
Adrian Prantl84168022017-09-15 17:39:50 +0000454 DObj->getStringDWOSection())) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000455 DataExtractor strDWOData(DObj->getStringDWOSection(), isLittleEndian(), 0);
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000456 uint32_t offset = 0;
David Blaikie66865d62014-01-09 00:13:35 +0000457 uint32_t strDWOOffset = 0;
458 while (const char *s = strDWOData.getCStr(&offset)) {
459 OS << format("0x%8.8x: \"%s\"\n", strDWOOffset, s);
460 strDWOOffset = offset;
David Blaikie622dce42014-01-08 23:29:59 +0000461 }
David Blaikie66865d62014-01-09 00:13:35 +0000462 }
Paul Robinsonb6aa01c2018-01-25 22:02:36 +0000463 if (shouldDump(Explicit, ".debug_line_str", DIDT_ID_DebugLineStr,
464 DObj->getLineStringSection())) {
465 DataExtractor strData(DObj->getLineStringSection(), isLittleEndian(), 0);
466 uint32_t offset = 0;
467 uint32_t strOffset = 0;
468 while (const char *s = strData.getCStr(&offset)) {
Scott Linder16c7bda2018-02-23 23:01:06 +0000469 OS << format("0x%8.8x: \"", strOffset);
470 OS.write_escaped(s);
471 OS << "\"\n";
Paul Robinsonb6aa01c2018-01-25 22:02:36 +0000472 strOffset = offset;
473 }
474 }
David Blaikie622dce42014-01-08 23:29:59 +0000475
Adrian Prantl057d3362017-09-15 23:04:04 +0000476 if (shouldDump(Explicit, ".debug_ranges", DIDT_ID_DebugRanges,
Adrian Prantl84168022017-09-15 17:39:50 +0000477 DObj->getRangeSection().Data)) {
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000478 // In fact, different compile units may have different address byte
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000479 // sizes, but for simplicity we just use the address byte size of the
480 // last compile unit (there is no easy and fast way to associate address
481 // range list and the compile unit it describes).
482 // FIXME: savedAddressByteSize seems sketchy.
Paul Robinson63811a42017-11-22 15:33:17 +0000483 uint8_t savedAddressByteSize = 0;
484 for (const auto &CU : compile_units()) {
485 savedAddressByteSize = CU->getAddressByteSize();
486 break;
487 }
Rafael Espindolac398e672017-07-19 22:27:28 +0000488 DWARFDataExtractor rangesData(*DObj, DObj->getRangeSection(),
489 isLittleEndian(), savedAddressByteSize);
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000490 uint32_t offset = 0;
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000491 DWARFDebugRangeList rangeList;
Paul Robinson17536b92017-06-29 16:52:08 +0000492 while (rangeList.extract(rangesData, &offset))
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000493 rangeList.dump(OS);
Eric Christopherda4b2192013-01-02 23:52:13 +0000494 }
Eric Christopher962c9082013-01-15 23:56:56 +0000495
James Henderson3fcc7452018-02-02 12:35:52 +0000496 if (shouldDump(Explicit, ".debug_rnglists", DIDT_ID_DebugRnglists,
497 DObj->getRnglistsSection().Data)) {
498 DWARFDataExtractor rnglistData(*DObj, DObj->getRnglistsSection(),
499 isLittleEndian(), 0);
500 uint32_t Offset = 0;
501 while (rnglistData.isValidOffset(Offset)) {
502 DWARFDebugRnglists Rnglists;
503 uint32_t TableOffset = Offset;
504 if (Error Err = Rnglists.extract(rnglistData, &Offset)) {
505 errs() << "error: " + toString(std::move(Err)) << '\n';
506 uint64_t Length = Rnglists.length();
507 // Keep going after an error, if we can, assuming that the length field
508 // could be read. If it couldn't, stop reading the section.
509 if (Length == 0)
510 break;
511 Offset = TableOffset + Length;
Wolfgang Pieba0729d42018-03-08 20:52:35 +0000512 } else {
513 Rnglists.dump(OS, DumpOpts);
514 }
James Henderson3fcc7452018-02-02 12:35:52 +0000515 }
516 }
517
Adrian Prantl057d3362017-09-15 23:04:04 +0000518 if (shouldDump(Explicit, ".debug_pubnames", DIDT_ID_DebugPubnames,
Adrian Prantl84168022017-09-15 17:39:50 +0000519 DObj->getPubNamesSection()))
Rafael Espindolac398e672017-07-19 22:27:28 +0000520 DWARFDebugPubTable(DObj->getPubNamesSection(), isLittleEndian(), false)
Adrian Prantl84168022017-09-15 17:39:50 +0000521 .dump(OS);
Krzysztof Parzyszek97438dc2013-02-12 16:20:28 +0000522
Adrian Prantl057d3362017-09-15 23:04:04 +0000523 if (shouldDump(Explicit, ".debug_pubtypes", DIDT_ID_DebugPubtypes,
Adrian Prantl84168022017-09-15 17:39:50 +0000524 DObj->getPubTypesSection()))
Rafael Espindolac398e672017-07-19 22:27:28 +0000525 DWARFDebugPubTable(DObj->getPubTypesSection(), isLittleEndian(), false)
Adrian Prantl84168022017-09-15 17:39:50 +0000526 .dump(OS);
Eric Christopher4c7e6ba2013-09-25 23:02:41 +0000527
Adrian Prantl057d3362017-09-15 23:04:04 +0000528 if (shouldDump(Explicit, ".debug_gnu_pubnames", DIDT_ID_DebugGnuPubnames,
Adrian Prantl84168022017-09-15 17:39:50 +0000529 DObj->getGnuPubNamesSection()))
Rafael Espindolac398e672017-07-19 22:27:28 +0000530 DWARFDebugPubTable(DObj->getGnuPubNamesSection(), isLittleEndian(),
George Rimare71e33f2016-12-17 09:10:32 +0000531 true /* GnuStyle */)
Adrian Prantl84168022017-09-15 17:39:50 +0000532 .dump(OS);
David Blaikieecd21ff2013-09-24 19:50:00 +0000533
Adrian Prantl057d3362017-09-15 23:04:04 +0000534 if (shouldDump(Explicit, ".debug_gnu_pubtypes", DIDT_ID_DebugGnuPubtypes,
Adrian Prantl84168022017-09-15 17:39:50 +0000535 DObj->getGnuPubTypesSection()))
Rafael Espindolac398e672017-07-19 22:27:28 +0000536 DWARFDebugPubTable(DObj->getGnuPubTypesSection(), isLittleEndian(),
George Rimare71e33f2016-12-17 09:10:32 +0000537 true /* GnuStyle */)
Adrian Prantl84168022017-09-15 17:39:50 +0000538 .dump(OS);
David Blaikie404d3042013-09-19 23:01:29 +0000539
Adrian Prantl057d3362017-09-15 23:04:04 +0000540 if (shouldDump(Explicit, ".debug_str_offsets", DIDT_ID_DebugStrOffsets,
Adrian Prantl84168022017-09-15 17:39:50 +0000541 DObj->getStringOffsetSection().Data))
Rafael Espindolac398e672017-07-19 22:27:28 +0000542 dumpStringOffsetsSection(
543 OS, "debug_str_offsets", *DObj, DObj->getStringOffsetSection(),
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000544 DObj->getStringSection(), compile_units(), type_unit_sections(),
545 isLittleEndian(), getMaxVersion());
Adrian Prantl057d3362017-09-15 23:04:04 +0000546 if (shouldDump(ExplicitDWO, ".debug_str_offsets.dwo", DIDT_ID_DebugStrOffsets,
Adrian Prantl84168022017-09-15 17:39:50 +0000547 DObj->getStringOffsetDWOSection().Data))
Rafael Espindolac398e672017-07-19 22:27:28 +0000548 dumpStringOffsetsSection(
549 OS, "debug_str_offsets.dwo", *DObj, DObj->getStringOffsetDWOSection(),
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000550 DObj->getStringDWOSection(), dwo_compile_units(),
551 dwo_type_unit_sections(), isLittleEndian(), getMaxVersion());
Frederic Risse837ec22014-11-14 16:15:53 +0000552
Adrian Prantl057d3362017-09-15 23:04:04 +0000553 if (shouldDump(Explicit, ".gnu_index", DIDT_ID_GdbIndex,
Adrian Prantl84168022017-09-15 17:39:50 +0000554 DObj->getGdbIndexSection())) {
George Rimar4f82df52016-09-23 11:01:53 +0000555 getGdbIndex().dump(OS);
556 }
557
Adrian Prantl057d3362017-09-15 23:04:04 +0000558 if (shouldDump(Explicit, ".apple_names", DIDT_ID_AppleNames,
Adrian Prantl84168022017-09-15 17:39:50 +0000559 DObj->getAppleNamesSection().Data))
Adrian Prantl99fdb9d2017-09-28 18:10:52 +0000560 getAppleNames().dump(OS);
Frederic Risse837ec22014-11-14 16:15:53 +0000561
Adrian Prantl057d3362017-09-15 23:04:04 +0000562 if (shouldDump(Explicit, ".apple_types", DIDT_ID_AppleTypes,
Adrian Prantl84168022017-09-15 17:39:50 +0000563 DObj->getAppleTypesSection().Data))
Adrian Prantl714ee4d2017-09-29 00:33:22 +0000564 getAppleTypes().dump(OS);
Frederic Risse837ec22014-11-14 16:15:53 +0000565
Adrian Prantl057d3362017-09-15 23:04:04 +0000566 if (shouldDump(Explicit, ".apple_namespaces", DIDT_ID_AppleNamespaces,
Adrian Prantl84168022017-09-15 17:39:50 +0000567 DObj->getAppleNamespacesSection().Data))
Adrian Prantlf51e7802017-09-29 00:52:33 +0000568 getAppleNamespaces().dump(OS);
Frederic Risse837ec22014-11-14 16:15:53 +0000569
Adrian Prantl057d3362017-09-15 23:04:04 +0000570 if (shouldDump(Explicit, ".apple_objc", DIDT_ID_AppleObjC,
Adrian Prantl84168022017-09-15 17:39:50 +0000571 DObj->getAppleObjCSection().Data))
Adrian Prantlf51e7802017-09-29 00:52:33 +0000572 getAppleObjC().dump(OS);
Pavel Labath3c9a9182018-01-29 11:08:32 +0000573 if (shouldDump(Explicit, ".debug_names", DIDT_ID_DebugNames,
574 DObj->getDebugNamesSection().Data))
575 getDebugNames().dump(OS);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000576}
577
David Blaikie15d85fc2017-05-23 06:48:53 +0000578DWARFCompileUnit *DWARFContext::getDWOCompileUnitForHash(uint64_t Hash) {
David Blaikiee79dda32017-09-19 18:36:11 +0000579 DWOCUs.parseDWO(*this, DObj->getInfoDWOSection(), true);
David Blaikiea62f1cb2017-07-30 15:15:58 +0000580
David Blaikieebac0b92017-07-30 08:12:07 +0000581 if (const auto &CUI = getCUIndex()) {
582 if (const auto *R = CUI.getFromHash(Hash))
David Blaikiee79dda32017-09-19 18:36:11 +0000583 return DWOCUs.getUnitForIndexEntry(*R);
David Blaikieebac0b92017-07-30 08:12:07 +0000584 return nullptr;
585 }
586
587 // If there's no index, just search through the CUs in the DWO - there's
588 // probably only one unless this is something like LTO - though an in-process
589 // built/cached lookup table could be used in that case to improve repeated
590 // lookups of different CUs in the DWO.
David Blaikie15d85fc2017-05-23 06:48:53 +0000591 for (const auto &DWOCU : dwo_compile_units())
592 if (DWOCU->getDWOId() == Hash)
593 return DWOCU.get();
594 return nullptr;
595}
596
Greg Claytonc7695a82017-05-02 20:28:33 +0000597DWARFDie DWARFContext::getDIEForOffset(uint32_t Offset) {
598 parseCompileUnits();
599 if (auto *CU = CUs.getUnitForOffset(Offset))
600 return CU->getDIEForOffset(Offset);
601 return DWARFDie();
602}
603
Jonas Devliegherec0a758d2017-09-18 14:15:57 +0000604bool DWARFContext::verify(raw_ostream &OS, DIDumpOptions DumpOpts) {
Greg Claytonc7695a82017-05-02 20:28:33 +0000605 bool Success = true;
Jonas Devlieghere27476ce2017-09-13 09:43:05 +0000606 DWARFVerifier verifier(OS, *this, DumpOpts);
Spyridoula Gravani73e17962017-07-27 00:59:33 +0000607
Spyridoula Gravani364b5352017-07-20 02:06:52 +0000608 Success &= verifier.handleDebugAbbrev();
Jonas Devliegherec0a758d2017-09-18 14:15:57 +0000609 if (DumpOpts.DumpType & DIDT_DebugInfo)
Spyridoula Gravani73e17962017-07-27 00:59:33 +0000610 Success &= verifier.handleDebugInfo();
Jonas Devliegherec0a758d2017-09-18 14:15:57 +0000611 if (DumpOpts.DumpType & DIDT_DebugLine)
Spyridoula Gravani73e17962017-07-27 00:59:33 +0000612 Success &= verifier.handleDebugLine();
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000613 Success &= verifier.handleAccelTables();
Greg Clayton48432cf2017-05-01 22:07:02 +0000614 return Success;
615}
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000616
David Blaikie82641be2015-11-17 00:39:55 +0000617const DWARFUnitIndex &DWARFContext::getCUIndex() {
618 if (CUIndex)
619 return *CUIndex;
620
Rafael Espindolac398e672017-07-19 22:27:28 +0000621 DataExtractor CUIndexData(DObj->getCUIndexSection(), isLittleEndian(), 0);
David Blaikie82641be2015-11-17 00:39:55 +0000622
David Blaikieb073cb92015-12-02 06:21:34 +0000623 CUIndex = llvm::make_unique<DWARFUnitIndex>(DW_SECT_INFO);
David Blaikie82641be2015-11-17 00:39:55 +0000624 CUIndex->parse(CUIndexData);
625 return *CUIndex;
626}
627
628const DWARFUnitIndex &DWARFContext::getTUIndex() {
629 if (TUIndex)
630 return *TUIndex;
631
Rafael Espindolac398e672017-07-19 22:27:28 +0000632 DataExtractor TUIndexData(DObj->getTUIndexSection(), isLittleEndian(), 0);
David Blaikie82641be2015-11-17 00:39:55 +0000633
David Blaikieb073cb92015-12-02 06:21:34 +0000634 TUIndex = llvm::make_unique<DWARFUnitIndex>(DW_SECT_TYPES);
David Blaikie82641be2015-11-17 00:39:55 +0000635 TUIndex->parse(TUIndexData);
636 return *TUIndex;
637}
638
George Rimar4f82df52016-09-23 11:01:53 +0000639DWARFGdbIndex &DWARFContext::getGdbIndex() {
640 if (GdbIndex)
641 return *GdbIndex;
642
Rafael Espindolac398e672017-07-19 22:27:28 +0000643 DataExtractor GdbIndexData(DObj->getGdbIndexSection(), true /*LE*/, 0);
George Rimar4f82df52016-09-23 11:01:53 +0000644 GdbIndex = llvm::make_unique<DWARFGdbIndex>();
645 GdbIndex->parse(GdbIndexData);
646 return *GdbIndex;
647}
648
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000649const DWARFDebugAbbrev *DWARFContext::getDebugAbbrev() {
650 if (Abbrev)
651 return Abbrev.get();
652
Rafael Espindolac398e672017-07-19 22:27:28 +0000653 DataExtractor abbrData(DObj->getAbbrevSection(), isLittleEndian(), 0);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000654
655 Abbrev.reset(new DWARFDebugAbbrev());
Alexey Samsonov4316df52014-04-25 21:10:56 +0000656 Abbrev->extract(abbrData);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000657 return Abbrev.get();
658}
659
Eric Christopherda4b2192013-01-02 23:52:13 +0000660const DWARFDebugAbbrev *DWARFContext::getDebugAbbrevDWO() {
661 if (AbbrevDWO)
662 return AbbrevDWO.get();
663
Rafael Espindolac398e672017-07-19 22:27:28 +0000664 DataExtractor abbrData(DObj->getAbbrevDWOSection(), isLittleEndian(), 0);
Eric Christopherda4b2192013-01-02 23:52:13 +0000665 AbbrevDWO.reset(new DWARFDebugAbbrev());
Alexey Samsonov4316df52014-04-25 21:10:56 +0000666 AbbrevDWO->extract(abbrData);
Eric Christopherda4b2192013-01-02 23:52:13 +0000667 return AbbrevDWO.get();
668}
669
David Blaikie18e73502013-06-19 21:37:13 +0000670const DWARFDebugLoc *DWARFContext::getDebugLoc() {
671 if (Loc)
672 return Loc.get();
673
Paul Robinson17536b92017-06-29 16:52:08 +0000674 Loc.reset(new DWARFDebugLoc);
David Blaikie18e73502013-06-19 21:37:13 +0000675 // assume all compile units have the same address byte size
Paul Robinson17536b92017-06-29 16:52:08 +0000676 if (getNumCompileUnits()) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000677 DWARFDataExtractor LocData(*DObj, DObj->getLocSection(), isLittleEndian(),
Paul Robinson17536b92017-06-29 16:52:08 +0000678 getCompileUnitAtIndex(0)->getAddressByteSize());
679 Loc->parse(LocData);
680 }
David Blaikie18e73502013-06-19 21:37:13 +0000681 return Loc.get();
682}
683
David Blaikie9c550ac2014-03-25 01:44:02 +0000684const DWARFDebugLocDWO *DWARFContext::getDebugLocDWO() {
685 if (LocDWO)
686 return LocDWO.get();
687
Rafael Espindolac398e672017-07-19 22:27:28 +0000688 DataExtractor LocData(DObj->getLocDWOSection().Data, isLittleEndian(), 0);
David Blaikie9c550ac2014-03-25 01:44:02 +0000689 LocDWO.reset(new DWARFDebugLocDWO());
690 LocDWO->parse(LocData);
691 return LocDWO.get();
692}
693
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000694const DWARFDebugAranges *DWARFContext::getDebugAranges() {
695 if (Aranges)
696 return Aranges.get();
697
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000698 Aranges.reset(new DWARFDebugAranges());
Alexey Samsonova1694c12012-11-16 08:36:25 +0000699 Aranges->generate(this);
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000700 return Aranges.get();
701}
702
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000703const DWARFDebugFrame *DWARFContext::getDebugFrame() {
704 if (DebugFrame)
705 return DebugFrame.get();
706
707 // There's a "bug" in the DWARFv3 standard with respect to the target address
708 // size within debug frame sections. While DWARF is supposed to be independent
709 // of its container, FDEs have fields with size being "target address size",
710 // which isn't specified in DWARF in general. It's only specified for CUs, but
711 // .eh_frame can appear without a .debug_info section. Follow the example of
712 // other tools (libdwarf) and extract this from the container (ObjectFile
713 // provides this information). This problem is fixed in DWARFv4
714 // See this dwarf-discuss discussion for more details:
715 // http://lists.dwarfstd.org/htdig.cgi/dwarf-discuss-dwarfstd.org/2011-December/001173.html
Rafael Auler86fb7bf2018-03-08 00:46:53 +0000716 DWARFDataExtractor debugFrameData(DObj->getDebugFrameSection(),
717 isLittleEndian(), DObj->getAddressSize());
Igor Laevsky03a670c2016-01-26 15:09:42 +0000718 DebugFrame.reset(new DWARFDebugFrame(false /* IsEH */));
719 DebugFrame->parse(debugFrameData);
720 return DebugFrame.get();
721}
722
723const DWARFDebugFrame *DWARFContext::getEHFrame() {
724 if (EHFrame)
725 return EHFrame.get();
726
Rafael Auler86fb7bf2018-03-08 00:46:53 +0000727 DWARFDataExtractor debugFrameData(DObj->getEHFrameSection(), isLittleEndian(),
728 DObj->getAddressSize());
Igor Laevsky03a670c2016-01-26 15:09:42 +0000729 DebugFrame.reset(new DWARFDebugFrame(true /* IsEH */));
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000730 DebugFrame->parse(debugFrameData);
731 return DebugFrame.get();
732}
733
Amjad Aboude59cc3e2015-11-12 09:38:54 +0000734const DWARFDebugMacro *DWARFContext::getDebugMacro() {
735 if (Macro)
736 return Macro.get();
737
Rafael Espindolac398e672017-07-19 22:27:28 +0000738 DataExtractor MacinfoData(DObj->getMacinfoSection(), isLittleEndian(), 0);
Amjad Aboude59cc3e2015-11-12 09:38:54 +0000739 Macro.reset(new DWARFDebugMacro());
740 Macro->parse(MacinfoData);
741 return Macro.get();
742}
743
Pavel Labath3c9a9182018-01-29 11:08:32 +0000744template <typename T>
745static T &getAccelTable(std::unique_ptr<T> &Cache, const DWARFObject &Obj,
746 const DWARFSection &Section, StringRef StringSection,
747 bool IsLittleEndian) {
Adrian Prantl99fdb9d2017-09-28 18:10:52 +0000748 if (Cache)
749 return *Cache;
750 DWARFDataExtractor AccelSection(Obj, Section, IsLittleEndian, 0);
751 DataExtractor StrData(StringSection, IsLittleEndian, 0);
Pavel Labath3c9a9182018-01-29 11:08:32 +0000752 Cache.reset(new T(AccelSection, StrData));
Jonas Devlieghereba915892017-12-11 18:22:47 +0000753 if (Error E = Cache->extract())
754 llvm::consumeError(std::move(E));
Adrian Prantl99fdb9d2017-09-28 18:10:52 +0000755 return *Cache;
756}
757
Pavel Labath3c9a9182018-01-29 11:08:32 +0000758const DWARFDebugNames &DWARFContext::getDebugNames() {
759 return getAccelTable(Names, *DObj, DObj->getDebugNamesSection(),
760 DObj->getStringSection(), isLittleEndian());
761}
762
Pavel Labath9b36fd22018-01-22 13:17:23 +0000763const AppleAcceleratorTable &DWARFContext::getAppleNames() {
Adrian Prantl99fdb9d2017-09-28 18:10:52 +0000764 return getAccelTable(AppleNames, *DObj, DObj->getAppleNamesSection(),
765 DObj->getStringSection(), isLittleEndian());
766}
767
Pavel Labath9b36fd22018-01-22 13:17:23 +0000768const AppleAcceleratorTable &DWARFContext::getAppleTypes() {
Adrian Prantl714ee4d2017-09-29 00:33:22 +0000769 return getAccelTable(AppleTypes, *DObj, DObj->getAppleTypesSection(),
770 DObj->getStringSection(), isLittleEndian());
771}
772
Pavel Labath9b36fd22018-01-22 13:17:23 +0000773const AppleAcceleratorTable &DWARFContext::getAppleNamespaces() {
Adrian Prantlf51e7802017-09-29 00:52:33 +0000774 return getAccelTable(AppleNamespaces, *DObj,
775 DObj->getAppleNamespacesSection(),
776 DObj->getStringSection(), isLittleEndian());
777}
778
Pavel Labath9b36fd22018-01-22 13:17:23 +0000779const AppleAcceleratorTable &DWARFContext::getAppleObjC() {
Adrian Prantlf51e7802017-09-29 00:52:33 +0000780 return getAccelTable(AppleObjC, *DObj, DObj->getAppleObjCSection(),
781 DObj->getStringSection(), isLittleEndian());
782}
783
Eric Christopher494109b2012-10-16 23:46:25 +0000784const DWARFLineTable *
Alexey Samsonov7a18c062015-05-19 21:54:32 +0000785DWARFContext::getLineTableForUnit(DWARFUnit *U) {
Benjamin Kramer679e1752011-09-15 20:43:18 +0000786 if (!Line)
Paul Robinson17536b92017-06-29 16:52:08 +0000787 Line.reset(new DWARFDebugLine);
David Blaikiec4e2bed2015-11-17 21:08:05 +0000788
Greg Claytonc8c10322016-12-13 18:25:19 +0000789 auto UnitDIE = U->getUnitDIE();
790 if (!UnitDIE)
Alexey Samsonov7a18c062015-05-19 21:54:32 +0000791 return nullptr;
David Blaikiec4e2bed2015-11-17 21:08:05 +0000792
Greg Clayton97d22182017-01-13 21:08:18 +0000793 auto Offset = toSectionOffset(UnitDIE.find(DW_AT_stmt_list));
Greg Clayton52fe1f62016-12-14 22:38:08 +0000794 if (!Offset)
Craig Topper2617dcc2014-04-15 06:32:26 +0000795 return nullptr; // No line table for this compile unit.
Benjamin Kramer5acab502011-09-15 02:12:05 +0000796
Greg Clayton52fe1f62016-12-14 22:38:08 +0000797 uint32_t stmtOffset = *Offset + U->getLineTableOffset();
Benjamin Kramer679e1752011-09-15 20:43:18 +0000798 // See if the line table is cached.
Eric Christopher494109b2012-10-16 23:46:25 +0000799 if (const DWARFLineTable *lt = Line->getLineTable(stmtOffset))
Benjamin Kramer679e1752011-09-15 20:43:18 +0000800 return lt;
801
Greg Clayton48ff66a2017-05-04 18:29:44 +0000802 // Make sure the offset is good before we try to parse.
Paul Robinson17536b92017-06-29 16:52:08 +0000803 if (stmtOffset >= U->getLineSection().Data.size())
Jonas Devlieghere27476ce2017-09-13 09:43:05 +0000804 return nullptr;
Greg Clayton48ff66a2017-05-04 18:29:44 +0000805
Benjamin Kramer679e1752011-09-15 20:43:18 +0000806 // We have to parse it first.
Rafael Espindolac398e672017-07-19 22:27:28 +0000807 DWARFDataExtractor lineData(*DObj, U->getLineSection(), isLittleEndian(),
Paul Robinson17536b92017-06-29 16:52:08 +0000808 U->getAddressByteSize());
Paul Robinsonbf750c82018-01-29 20:57:43 +0000809 return Line->getOrParseLineTable(lineData, stmtOffset, *this, U);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000810}
811
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000812void DWARFContext::parseCompileUnits() {
Rafael Espindolac398e672017-07-19 22:27:28 +0000813 CUs.parse(*this, DObj->getInfoSection());
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000814}
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000815
David Blaikie03c089c2013-09-23 22:44:47 +0000816void DWARFContext::parseTypeUnits() {
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000817 if (!TUs.empty())
818 return;
Rafael Espindolac398e672017-07-19 22:27:28 +0000819 DObj->forEachTypesSections([&](const DWARFSection &S) {
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000820 TUs.emplace_back();
Rafael Espindola5e5dfa12017-07-12 21:08:24 +0000821 TUs.back().parse(*this, S);
822 });
David Blaikie03c089c2013-09-23 22:44:47 +0000823}
824
Eric Christopherda4b2192013-01-02 23:52:13 +0000825void DWARFContext::parseDWOCompileUnits() {
Rafael Espindolac398e672017-07-19 22:27:28 +0000826 DWOCUs.parseDWO(*this, DObj->getInfoDWOSection());
Eric Christopherda4b2192013-01-02 23:52:13 +0000827}
828
David Blaikie92d9d622014-01-09 05:08:24 +0000829void DWARFContext::parseDWOTypeUnits() {
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000830 if (!DWOTUs.empty())
831 return;
Rafael Espindolac398e672017-07-19 22:27:28 +0000832 DObj->forEachTypesDWOSections([&](const DWARFSection &S) {
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000833 DWOTUs.emplace_back();
Rafael Espindola5e5dfa12017-07-12 21:08:24 +0000834 DWOTUs.back().parseDWO(*this, S);
835 });
David Blaikie92d9d622014-01-09 05:08:24 +0000836}
837
Alexey Samsonov45be7932012-08-30 07:49:50 +0000838DWARFCompileUnit *DWARFContext::getCompileUnitForOffset(uint32_t Offset) {
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000839 parseCompileUnits();
Frederic Riss4e126a02014-09-15 07:50:27 +0000840 return CUs.getUnitForOffset(Offset);
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000841}
842
Alexey Samsonov45be7932012-08-30 07:49:50 +0000843DWARFCompileUnit *DWARFContext::getCompileUnitForAddress(uint64_t Address) {
Benjamin Kramer112ec172011-09-15 21:59:13 +0000844 // First, get the offset of the compile unit.
Alexey Samsonov45be7932012-08-30 07:49:50 +0000845 uint32_t CUOffset = getDebugAranges()->findAddress(Address);
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000846 // Retrieve the compile unit.
Alexey Samsonov45be7932012-08-30 07:49:50 +0000847 return getCompileUnitForOffset(CUOffset);
848}
849
Jonas Devliegheref63ee642017-10-25 21:56:41 +0000850DWARFContext::DIEsForAddress DWARFContext::getDIEsForAddress(uint64_t Address) {
851 DIEsForAddress Result;
852
853 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
854 if (!CU)
855 return Result;
856
857 Result.CompileUnit = CU;
858 Result.FunctionDIE = CU->getSubroutineForAddress(Address);
859
860 std::vector<DWARFDie> Worklist;
861 Worklist.push_back(Result.FunctionDIE);
862 while (!Worklist.empty()) {
863 DWARFDie DIE = Worklist.back();
864 Worklist.pop_back();
865
866 if (DIE.getTag() == DW_TAG_lexical_block &&
867 DIE.addressRangeContainsAddress(Address)) {
868 Result.BlockDIE = DIE;
869 break;
870 }
871
872 for (auto Child : DIE)
873 Worklist.push_back(Child);
874 }
875
876 return Result;
877}
878
David Blaikieefc4eba2017-02-06 20:19:02 +0000879static bool getFunctionNameAndStartLineForAddress(DWARFCompileUnit *CU,
880 uint64_t Address,
881 FunctionNameKind Kind,
882 std::string &FunctionName,
883 uint32_t &StartLine) {
Alexey Samsonovd0109992014-04-18 21:36:39 +0000884 // The address may correspond to instruction in some inlined function,
885 // so we have to build the chain of inlined functions and take the
David Blaikieefc4eba2017-02-06 20:19:02 +0000886 // name of the topmost function in it.
Greg Claytonc8c10322016-12-13 18:25:19 +0000887 SmallVector<DWARFDie, 4> InlinedChain;
888 CU->getInlinedChainForAddress(Address, InlinedChain);
David Blaikieefc4eba2017-02-06 20:19:02 +0000889 if (InlinedChain.empty())
Alexey Samsonovd0109992014-04-18 21:36:39 +0000890 return false;
David Blaikieefc4eba2017-02-06 20:19:02 +0000891
892 const DWARFDie &DIE = InlinedChain[0];
893 bool FoundResult = false;
894 const char *Name = nullptr;
895 if (Kind != FunctionNameKind::None && (Name = DIE.getSubroutineName(Kind))) {
Alexey Samsonovd0109992014-04-18 21:36:39 +0000896 FunctionName = Name;
David Blaikieefc4eba2017-02-06 20:19:02 +0000897 FoundResult = true;
Alexey Samsonovd0109992014-04-18 21:36:39 +0000898 }
David Blaikieefc4eba2017-02-06 20:19:02 +0000899 if (auto DeclLineResult = DIE.getDeclLine()) {
900 StartLine = DeclLineResult;
901 FoundResult = true;
902 }
903
904 return FoundResult;
Alexey Samsonovd0109992014-04-18 21:36:39 +0000905}
906
Alexey Samsonov45be7932012-08-30 07:49:50 +0000907DILineInfo DWARFContext::getLineInfoForAddress(uint64_t Address,
Alexey Samsonovdce67342014-05-15 21:24:32 +0000908 DILineInfoSpecifier Spec) {
Alexey Samsonovd0109992014-04-18 21:36:39 +0000909 DILineInfo Result;
910
Alexey Samsonov45be7932012-08-30 07:49:50 +0000911 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
912 if (!CU)
Alexey Samsonovd0109992014-04-18 21:36:39 +0000913 return Result;
David Blaikieefc4eba2017-02-06 20:19:02 +0000914 getFunctionNameAndStartLineForAddress(CU, Address, Spec.FNKind,
915 Result.FunctionName,
916 Result.StartLine);
Alexey Samsonovdce67342014-05-15 21:24:32 +0000917 if (Spec.FLIKind != FileLineInfoKind::None) {
Frederic Riss101b5e22014-09-19 15:11:51 +0000918 if (const DWARFLineTable *LineTable = getLineTableForUnit(CU))
919 LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(),
920 Spec.FLIKind, Result);
Alexey Samsonovf4462fa2012-07-02 05:54:45 +0000921 }
Alexey Samsonovd0109992014-04-18 21:36:39 +0000922 return Result;
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000923}
David Blaikiea379b1812011-12-20 02:50:00 +0000924
Alexey Samsonovdce67342014-05-15 21:24:32 +0000925DILineInfoTable
926DWARFContext::getLineInfoForAddressRange(uint64_t Address, uint64_t Size,
927 DILineInfoSpecifier Spec) {
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000928 DILineInfoTable Lines;
929 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
930 if (!CU)
931 return Lines;
932
933 std::string FunctionName = "<invalid>";
David Blaikieefc4eba2017-02-06 20:19:02 +0000934 uint32_t StartLine = 0;
935 getFunctionNameAndStartLineForAddress(CU, Address, Spec.FNKind, FunctionName,
936 StartLine);
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000937
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000938 // If the Specifier says we don't need FileLineInfo, just
939 // return the top-most function at the starting address.
Alexey Samsonovdce67342014-05-15 21:24:32 +0000940 if (Spec.FLIKind == FileLineInfoKind::None) {
Alexey Samsonovd0109992014-04-18 21:36:39 +0000941 DILineInfo Result;
942 Result.FunctionName = FunctionName;
David Blaikieefc4eba2017-02-06 20:19:02 +0000943 Result.StartLine = StartLine;
Alexey Samsonovd0109992014-04-18 21:36:39 +0000944 Lines.push_back(std::make_pair(Address, Result));
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000945 return Lines;
946 }
947
Frederic Rissec8a5ba2014-09-04 06:14:40 +0000948 const DWARFLineTable *LineTable = getLineTableForUnit(CU);
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000949
950 // Get the index of row we're looking for in the line table.
951 std::vector<uint32_t> RowVector;
952 if (!LineTable->lookupAddressRange(Address, Size, RowVector))
953 return Lines;
954
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000955 for (uint32_t RowIndex : RowVector) {
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000956 // Take file number and line/column from the row.
957 const DWARFDebugLine::Row &Row = LineTable->Rows[RowIndex];
Alexey Samsonovd0109992014-04-18 21:36:39 +0000958 DILineInfo Result;
Frederic Riss101b5e22014-09-19 15:11:51 +0000959 LineTable->getFileNameByIndex(Row.File, CU->getCompilationDir(),
960 Spec.FLIKind, Result.FileName);
Alexey Samsonovd0109992014-04-18 21:36:39 +0000961 Result.FunctionName = FunctionName;
962 Result.Line = Row.Line;
963 Result.Column = Row.Column;
David Blaikieefc4eba2017-02-06 20:19:02 +0000964 Result.StartLine = StartLine;
Alexey Samsonovd0109992014-04-18 21:36:39 +0000965 Lines.push_back(std::make_pair(Row.Address, Result));
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000966 }
967
968 return Lines;
969}
970
Alexey Samsonovdce67342014-05-15 21:24:32 +0000971DIInliningInfo
972DWARFContext::getInliningInfoForAddress(uint64_t Address,
973 DILineInfoSpecifier Spec) {
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000974 DIInliningInfo InliningInfo;
975
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000976 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
977 if (!CU)
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000978 return InliningInfo;
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000979
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000980 const DWARFLineTable *LineTable = nullptr;
Greg Claytonc8c10322016-12-13 18:25:19 +0000981 SmallVector<DWARFDie, 4> InlinedChain;
982 CU->getInlinedChainForAddress(Address, InlinedChain);
983 if (InlinedChain.size() == 0) {
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000984 // If there is no DIE for address (e.g. it is in unavailable .dwo file),
985 // try to at least get file/line info from symbol table.
Alexey Samsonovdce67342014-05-15 21:24:32 +0000986 if (Spec.FLIKind != FileLineInfoKind::None) {
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000987 DILineInfo Frame;
Frederic Rissec8a5ba2014-09-04 06:14:40 +0000988 LineTable = getLineTableForUnit(CU);
Frederic Riss101b5e22014-09-19 15:11:51 +0000989 if (LineTable &&
990 LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(),
991 Spec.FLIKind, Frame))
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000992 InliningInfo.addFrame(Frame);
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000993 }
994 return InliningInfo;
995 }
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000996
Dehao Chenef700d52017-04-17 20:10:39 +0000997 uint32_t CallFile = 0, CallLine = 0, CallColumn = 0, CallDiscriminator = 0;
Greg Claytonc8c10322016-12-13 18:25:19 +0000998 for (uint32_t i = 0, n = InlinedChain.size(); i != n; i++) {
999 DWARFDie &FunctionDIE = InlinedChain[i];
Alexey Samsonovd0109992014-04-18 21:36:39 +00001000 DILineInfo Frame;
Alexey Samsonovc942e6b2012-09-04 08:12:33 +00001001 // Get function name if necessary.
Greg Claytonc8c10322016-12-13 18:25:19 +00001002 if (const char *Name = FunctionDIE.getSubroutineName(Spec.FNKind))
Alexey Samsonovdce67342014-05-15 21:24:32 +00001003 Frame.FunctionName = Name;
David Blaikieefc4eba2017-02-06 20:19:02 +00001004 if (auto DeclLineResult = FunctionDIE.getDeclLine())
1005 Frame.StartLine = DeclLineResult;
Alexey Samsonovdce67342014-05-15 21:24:32 +00001006 if (Spec.FLIKind != FileLineInfoKind::None) {
Alexey Samsonovc942e6b2012-09-04 08:12:33 +00001007 if (i == 0) {
1008 // For the topmost frame, initialize the line table of this
1009 // compile unit and fetch file/line info from it.
Frederic Rissec8a5ba2014-09-04 06:14:40 +00001010 LineTable = getLineTableForUnit(CU);
Alexey Samsonovc942e6b2012-09-04 08:12:33 +00001011 // For the topmost routine, get file/line info from line table.
Frederic Riss101b5e22014-09-19 15:11:51 +00001012 if (LineTable)
1013 LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(),
1014 Spec.FLIKind, Frame);
Alexey Samsonovc942e6b2012-09-04 08:12:33 +00001015 } else {
1016 // Otherwise, use call file, call line and call column from
1017 // previous DIE in inlined chain.
Frederic Riss101b5e22014-09-19 15:11:51 +00001018 if (LineTable)
1019 LineTable->getFileNameByIndex(CallFile, CU->getCompilationDir(),
1020 Spec.FLIKind, Frame.FileName);
Alexey Samsonovd0109992014-04-18 21:36:39 +00001021 Frame.Line = CallLine;
1022 Frame.Column = CallColumn;
Dehao Chenef700d52017-04-17 20:10:39 +00001023 Frame.Discriminator = CallDiscriminator;
Alexey Samsonovc942e6b2012-09-04 08:12:33 +00001024 }
1025 // Get call file/line/column of a current DIE.
1026 if (i + 1 < n) {
Dehao Chenef700d52017-04-17 20:10:39 +00001027 FunctionDIE.getCallerFrame(CallFile, CallLine, CallColumn,
1028 CallDiscriminator);
Alexey Samsonovc942e6b2012-09-04 08:12:33 +00001029 }
1030 }
Alexey Samsonovc942e6b2012-09-04 08:12:33 +00001031 InliningInfo.addFrame(Frame);
1032 }
1033 return InliningInfo;
1034}
1035
David Blaikief9803fb2017-05-23 00:30:42 +00001036std::shared_ptr<DWARFContext>
1037DWARFContext::getDWOContext(StringRef AbsolutePath) {
David Blaikie15d85fc2017-05-23 06:48:53 +00001038 if (auto S = DWP.lock()) {
David Blaikief9803fb2017-05-23 00:30:42 +00001039 DWARFContext *Ctxt = S->Context.get();
1040 return std::shared_ptr<DWARFContext>(std::move(S), Ctxt);
1041 }
1042
David Blaikie15d85fc2017-05-23 06:48:53 +00001043 std::weak_ptr<DWOFile> *Entry = &DWOFiles[AbsolutePath];
1044
1045 if (auto S = Entry->lock()) {
1046 DWARFContext *Ctxt = S->Context.get();
1047 return std::shared_ptr<DWARFContext>(std::move(S), Ctxt);
1048 }
1049
David Blaikie15d85fc2017-05-23 06:48:53 +00001050 Expected<OwningBinary<ObjectFile>> Obj = [&] {
1051 if (!CheckedForDWP) {
David Blaikiee5adb682017-07-30 01:34:08 +00001052 SmallString<128> DWPName;
1053 auto Obj = object::ObjectFile::createObjectFile(
1054 this->DWPName.empty()
1055 ? (DObj->getFileName() + ".dwp").toStringRef(DWPName)
1056 : StringRef(this->DWPName));
David Blaikie15d85fc2017-05-23 06:48:53 +00001057 if (Obj) {
1058 Entry = &DWP;
1059 return Obj;
1060 } else {
1061 CheckedForDWP = true;
1062 // TODO: Should this error be handled (maybe in a high verbosity mode)
1063 // before falling back to .dwo files?
1064 consumeError(Obj.takeError());
1065 }
1066 }
1067
1068 return object::ObjectFile::createObjectFile(AbsolutePath);
1069 }();
1070
David Blaikief9803fb2017-05-23 00:30:42 +00001071 if (!Obj) {
1072 // TODO: Actually report errors helpfully.
1073 consumeError(Obj.takeError());
1074 return nullptr;
1075 }
David Blaikie15d85fc2017-05-23 06:48:53 +00001076
1077 auto S = std::make_shared<DWOFile>();
David Blaikief9803fb2017-05-23 00:30:42 +00001078 S->File = std::move(Obj.get());
Rafael Espindolac398e672017-07-19 22:27:28 +00001079 S->Context = DWARFContext::create(*S->File.getBinary());
David Blaikie15d85fc2017-05-23 06:48:53 +00001080 *Entry = S;
David Blaikief9803fb2017-05-23 00:30:42 +00001081 auto *Ctxt = S->Context.get();
1082 return std::shared_ptr<DWARFContext>(std::move(S), Ctxt);
1083}
1084
George Rimar702dac62017-04-12 08:59:15 +00001085static Error createError(const Twine &Reason, llvm::Error E) {
1086 return make_error<StringError>(Reason + toString(std::move(E)),
1087 inconvertibleErrorCode());
1088}
1089
George Rimara25d3292017-05-27 18:10:23 +00001090/// SymInfo contains information about symbol: it's address
1091/// and section index which is -1LL for absolute symbols.
1092struct SymInfo {
1093 uint64_t Address;
1094 uint64_t SectionIndex;
1095};
1096
1097/// Returns the address of symbol relocation used against and a section index.
1098/// Used for futher relocations computation. Symbol's section load address is
1099static Expected<SymInfo> getSymbolInfo(const object::ObjectFile &Obj,
1100 const RelocationRef &Reloc,
1101 const LoadedObjectInfo *L,
1102 std::map<SymbolRef, SymInfo> &Cache) {
1103 SymInfo Ret = {0, (uint64_t)-1LL};
George Rimar702dac62017-04-12 08:59:15 +00001104 object::section_iterator RSec = Obj.section_end();
1105 object::symbol_iterator Sym = Reloc.getSymbol();
1106
George Rimara25d3292017-05-27 18:10:23 +00001107 std::map<SymbolRef, SymInfo>::iterator CacheIt = Cache.end();
George Rimar702dac62017-04-12 08:59:15 +00001108 // First calculate the address of the symbol or section as it appears
1109 // in the object file
1110 if (Sym != Obj.symbol_end()) {
George Rimar958b01a2017-05-15 11:45:28 +00001111 bool New;
George Rimara25d3292017-05-27 18:10:23 +00001112 std::tie(CacheIt, New) = Cache.insert({*Sym, {0, 0}});
George Rimar958b01a2017-05-15 11:45:28 +00001113 if (!New)
1114 return CacheIt->second;
1115
George Rimar702dac62017-04-12 08:59:15 +00001116 Expected<uint64_t> SymAddrOrErr = Sym->getAddress();
1117 if (!SymAddrOrErr)
George Rimar1af3cb22017-06-28 08:21:19 +00001118 return createError("failed to compute symbol address: ",
George Rimar702dac62017-04-12 08:59:15 +00001119 SymAddrOrErr.takeError());
1120
1121 // Also remember what section this symbol is in for later
1122 auto SectOrErr = Sym->getSection();
1123 if (!SectOrErr)
George Rimar1af3cb22017-06-28 08:21:19 +00001124 return createError("failed to get symbol section: ",
George Rimar702dac62017-04-12 08:59:15 +00001125 SectOrErr.takeError());
1126
1127 RSec = *SectOrErr;
George Rimara25d3292017-05-27 18:10:23 +00001128 Ret.Address = *SymAddrOrErr;
George Rimar702dac62017-04-12 08:59:15 +00001129 } else if (auto *MObj = dyn_cast<MachOObjectFile>(&Obj)) {
1130 RSec = MObj->getRelocationSection(Reloc.getRawDataRefImpl());
George Rimara25d3292017-05-27 18:10:23 +00001131 Ret.Address = RSec->getAddress();
George Rimar702dac62017-04-12 08:59:15 +00001132 }
1133
George Rimara25d3292017-05-27 18:10:23 +00001134 if (RSec != Obj.section_end())
1135 Ret.SectionIndex = RSec->getIndex();
1136
George Rimar702dac62017-04-12 08:59:15 +00001137 // If we are given load addresses for the sections, we need to adjust:
1138 // SymAddr = (Address of Symbol Or Section in File) -
1139 // (Address of Section in File) +
1140 // (Load Address of Section)
1141 // RSec is now either the section being targeted or the section
1142 // containing the symbol being targeted. In either case,
1143 // we need to perform the same computation.
1144 if (L && RSec != Obj.section_end())
1145 if (uint64_t SectionLoadAddress = L->getSectionLoadAddress(*RSec))
George Rimara25d3292017-05-27 18:10:23 +00001146 Ret.Address += SectionLoadAddress - RSec->getAddress();
George Rimar958b01a2017-05-15 11:45:28 +00001147
1148 if (CacheIt != Cache.end())
1149 CacheIt->second = Ret;
1150
George Rimar702dac62017-04-12 08:59:15 +00001151 return Ret;
1152}
1153
1154static bool isRelocScattered(const object::ObjectFile &Obj,
1155 const RelocationRef &Reloc) {
George Rimard4998b02017-04-13 09:52:50 +00001156 const MachOObjectFile *MachObj = dyn_cast<MachOObjectFile>(&Obj);
1157 if (!MachObj)
George Rimar702dac62017-04-12 08:59:15 +00001158 return false;
1159 // MachO also has relocations that point to sections and
1160 // scattered relocations.
George Rimar702dac62017-04-12 08:59:15 +00001161 auto RelocInfo = MachObj->getRelocation(Reloc.getRawDataRefImpl());
1162 return MachObj->isRelocationScattered(RelocInfo);
1163}
1164
Rafael Espindolac398e672017-07-19 22:27:28 +00001165ErrorPolicy DWARFContext::defaultErrorHandler(Error E) {
George Rimar1af3cb22017-06-28 08:21:19 +00001166 errs() << "error: " + toString(std::move(E)) << '\n';
1167 return ErrorPolicy::Continue;
1168}
1169
Rafael Espindola87c3f4a92017-07-24 19:34:26 +00001170namespace {
1171struct DWARFSectionMap final : public DWARFSection {
1172 RelocAddrMap Relocs;
1173};
Rafael Espindola87c3f4a92017-07-24 19:34:26 +00001174
Rafael Espindolac398e672017-07-19 22:27:28 +00001175class DWARFObjInMemory final : public DWARFObject {
1176 bool IsLittleEndian;
1177 uint8_t AddressSize;
1178 StringRef FileName;
George Rimar6957ab52017-08-15 12:32:54 +00001179 const object::ObjectFile *Obj = nullptr;
George Rimare1c30f72017-08-15 15:54:43 +00001180 std::vector<SectionName> SectionNames;
Keno Fischerc780e8e2015-05-21 21:24:32 +00001181
Rafael Espindolac398e672017-07-19 22:27:28 +00001182 using TypeSectionMap = MapVector<object::SectionRef, DWARFSectionMap,
1183 std::map<object::SectionRef, unsigned>>;
Eric Christopher7370b552012-11-12 21:40:38 +00001184
Rafael Espindolac398e672017-07-19 22:27:28 +00001185 TypeSectionMap TypesSections;
1186 TypeSectionMap TypesDWOSections;
1187
1188 DWARFSectionMap InfoSection;
1189 DWARFSectionMap LocSection;
1190 DWARFSectionMap LineSection;
1191 DWARFSectionMap RangeSection;
James Henderson3fcc7452018-02-02 12:35:52 +00001192 DWARFSectionMap RnglistsSection;
Rafael Espindolac398e672017-07-19 22:27:28 +00001193 DWARFSectionMap StringOffsetSection;
1194 DWARFSectionMap InfoDWOSection;
1195 DWARFSectionMap LineDWOSection;
1196 DWARFSectionMap LocDWOSection;
1197 DWARFSectionMap StringOffsetDWOSection;
1198 DWARFSectionMap RangeDWOSection;
1199 DWARFSectionMap AddrSection;
1200 DWARFSectionMap AppleNamesSection;
1201 DWARFSectionMap AppleTypesSection;
1202 DWARFSectionMap AppleNamespacesSection;
1203 DWARFSectionMap AppleObjCSection;
Pavel Labath3c9a9182018-01-29 11:08:32 +00001204 DWARFSectionMap DebugNamesSection;
Rafael Espindolac398e672017-07-19 22:27:28 +00001205
1206 DWARFSectionMap *mapNameToDWARFSection(StringRef Name) {
1207 return StringSwitch<DWARFSectionMap *>(Name)
1208 .Case("debug_info", &InfoSection)
1209 .Case("debug_loc", &LocSection)
1210 .Case("debug_line", &LineSection)
1211 .Case("debug_str_offsets", &StringOffsetSection)
1212 .Case("debug_ranges", &RangeSection)
James Henderson3fcc7452018-02-02 12:35:52 +00001213 .Case("debug_rnglists", &RnglistsSection)
Rafael Espindolac398e672017-07-19 22:27:28 +00001214 .Case("debug_info.dwo", &InfoDWOSection)
1215 .Case("debug_loc.dwo", &LocDWOSection)
1216 .Case("debug_line.dwo", &LineDWOSection)
Pavel Labath3c9a9182018-01-29 11:08:32 +00001217 .Case("debug_names", &DebugNamesSection)
Rafael Espindolac398e672017-07-19 22:27:28 +00001218 .Case("debug_str_offsets.dwo", &StringOffsetDWOSection)
1219 .Case("debug_addr", &AddrSection)
1220 .Case("apple_names", &AppleNamesSection)
1221 .Case("apple_types", &AppleTypesSection)
1222 .Case("apple_namespaces", &AppleNamespacesSection)
1223 .Case("apple_namespac", &AppleNamespacesSection)
1224 .Case("apple_objc", &AppleObjCSection)
1225 .Default(nullptr);
1226 }
1227
1228 StringRef AbbrevSection;
1229 StringRef ARangeSection;
1230 StringRef DebugFrameSection;
1231 StringRef EHFrameSection;
1232 StringRef StringSection;
1233 StringRef MacinfoSection;
1234 StringRef PubNamesSection;
1235 StringRef PubTypesSection;
1236 StringRef GnuPubNamesSection;
1237 StringRef AbbrevDWOSection;
1238 StringRef StringDWOSection;
1239 StringRef GnuPubTypesSection;
1240 StringRef CUIndexSection;
1241 StringRef GdbIndexSection;
1242 StringRef TUIndexSection;
Paul Robinsonb6aa01c2018-01-25 22:02:36 +00001243 StringRef LineStringSection;
Rafael Espindolac398e672017-07-19 22:27:28 +00001244
1245 SmallVector<SmallString<32>, 4> UncompressedSections;
1246
1247 StringRef *mapSectionToMember(StringRef Name) {
1248 if (DWARFSection *Sec = mapNameToDWARFSection(Name))
1249 return &Sec->Data;
1250 return StringSwitch<StringRef *>(Name)
1251 .Case("debug_abbrev", &AbbrevSection)
1252 .Case("debug_aranges", &ARangeSection)
1253 .Case("debug_frame", &DebugFrameSection)
1254 .Case("eh_frame", &EHFrameSection)
1255 .Case("debug_str", &StringSection)
1256 .Case("debug_macinfo", &MacinfoSection)
1257 .Case("debug_pubnames", &PubNamesSection)
1258 .Case("debug_pubtypes", &PubTypesSection)
1259 .Case("debug_gnu_pubnames", &GnuPubNamesSection)
1260 .Case("debug_gnu_pubtypes", &GnuPubTypesSection)
1261 .Case("debug_abbrev.dwo", &AbbrevDWOSection)
1262 .Case("debug_str.dwo", &StringDWOSection)
1263 .Case("debug_cu_index", &CUIndexSection)
1264 .Case("debug_tu_index", &TUIndexSection)
1265 .Case("gdb_index", &GdbIndexSection)
Paul Robinsonb6aa01c2018-01-25 22:02:36 +00001266 .Case("debug_line_str", &LineStringSection)
Rafael Espindolac398e672017-07-19 22:27:28 +00001267 // Any more debug info sections go here.
1268 .Default(nullptr);
1269 }
1270
1271 /// If Sec is compressed section, decompresses and updates its contents
1272 /// provided by Data. Otherwise leaves it unchanged.
1273 Error maybeDecompress(const object::SectionRef &Sec, StringRef Name,
1274 StringRef &Data) {
1275 if (!Decompressor::isCompressed(Sec))
1276 return Error::success();
1277
1278 Expected<Decompressor> Decompressor =
1279 Decompressor::create(Name, Data, IsLittleEndian, AddressSize == 8);
1280 if (!Decompressor)
1281 return Decompressor.takeError();
1282
1283 SmallString<32> Out;
1284 if (auto Err = Decompressor->resizeAndDecompress(Out))
1285 return Err;
1286
1287 UncompressedSections.emplace_back(std::move(Out));
1288 Data = UncompressedSections.back();
1289
1290 return Error::success();
1291 }
1292
1293public:
1294 DWARFObjInMemory(const StringMap<std::unique_ptr<MemoryBuffer>> &Sections,
1295 uint8_t AddrSize, bool IsLittleEndian)
1296 : IsLittleEndian(IsLittleEndian) {
1297 for (const auto &SecIt : Sections) {
1298 if (StringRef *SectionData = mapSectionToMember(SecIt.first()))
1299 *SectionData = SecIt.second->getBuffer();
Alexey Samsonov068fc8a2013-04-23 10:17:34 +00001300 }
Rafael Espindolac398e672017-07-19 22:27:28 +00001301 }
1302 DWARFObjInMemory(const object::ObjectFile &Obj, const LoadedObjectInfo *L,
1303 function_ref<ErrorPolicy(Error)> HandleError)
1304 : IsLittleEndian(Obj.isLittleEndian()),
George Rimar6957ab52017-08-15 12:32:54 +00001305 AddressSize(Obj.getBytesInAddress()), FileName(Obj.getFileName()),
1306 Obj(&Obj) {
Alexey Samsonov068fc8a2013-04-23 10:17:34 +00001307
George Rimare1c30f72017-08-15 15:54:43 +00001308 StringMap<unsigned> SectionAmountMap;
Rafael Espindolac398e672017-07-19 22:27:28 +00001309 for (const SectionRef &Section : Obj.sections()) {
1310 StringRef Name;
1311 Section.getName(Name);
George Rimare1c30f72017-08-15 15:54:43 +00001312 ++SectionAmountMap[Name];
1313 SectionNames.push_back({ Name, true });
1314
Rafael Espindolac398e672017-07-19 22:27:28 +00001315 // Skip BSS and Virtual sections, they aren't interesting.
1316 if (Section.isBSS() || Section.isVirtual())
George Rimarfed9f092017-05-17 12:10:51 +00001317 continue;
Keno Fischerc780e8e2015-05-21 21:24:32 +00001318
Jonas Devlieghere8af23872017-09-26 14:22:35 +00001319 // Skip sections stripped by dsymutil.
1320 if (Section.isStripped())
1321 continue;
1322
Rafael Espindolac398e672017-07-19 22:27:28 +00001323 StringRef Data;
1324 section_iterator RelocatedSection = Section.getRelocatedSection();
1325 // Try to obtain an already relocated version of this section.
1326 // Else use the unrelocated section from the object file. We'll have to
1327 // apply relocations ourselves later.
1328 if (!L || !L->getLoadedSectionContents(*RelocatedSection, Data))
1329 Section.getContents(Data);
George Rimarfed9f092017-05-17 12:10:51 +00001330
Rafael Espindolac398e672017-07-19 22:27:28 +00001331 if (auto Err = maybeDecompress(Section, Name, Data)) {
1332 ErrorPolicy EP = HandleError(createError(
1333 "failed to decompress '" + Name + "', ", std::move(Err)));
George Rimar1af3cb22017-06-28 08:21:19 +00001334 if (EP == ErrorPolicy::Halt)
1335 return;
George Rimarfed9f092017-05-17 12:10:51 +00001336 continue;
1337 }
Rafael Espindolac398e672017-07-19 22:27:28 +00001338
1339 // Compressed sections names in GNU style starts from ".z",
1340 // at this point section is decompressed and we drop compression prefix.
1341 Name = Name.substr(
1342 Name.find_first_not_of("._z")); // Skip ".", "z" and "_" prefixes.
1343
1344 // Map platform specific debug section names to DWARF standard section
1345 // names.
1346 Name = Obj.mapDebugSectionName(Name);
1347
1348 if (StringRef *SectionData = mapSectionToMember(Name)) {
1349 *SectionData = Data;
1350 if (Name == "debug_ranges") {
1351 // FIXME: Use the other dwo range section when we emit it.
1352 RangeDWOSection.Data = Data;
1353 }
1354 } else if (Name == "debug_types") {
1355 // Find debug_types data by section rather than name as there are
1356 // multiple, comdat grouped, debug_types sections.
1357 TypesSections[Section].Data = Data;
1358 } else if (Name == "debug_types.dwo") {
1359 TypesDWOSections[Section].Data = Data;
1360 }
1361
1362 if (RelocatedSection == Obj.section_end())
1363 continue;
1364
1365 StringRef RelSecName;
1366 StringRef RelSecData;
1367 RelocatedSection->getName(RelSecName);
1368
1369 // If the section we're relocating was relocated already by the JIT,
1370 // then we used the relocated version above, so we do not need to process
1371 // relocations for it now.
1372 if (L && L->getLoadedSectionContents(*RelocatedSection, RelSecData))
1373 continue;
1374
1375 // In Mach-o files, the relocations do not need to be applied if
1376 // there is no load offset to apply. The value read at the
1377 // relocation point already factors in the section address
1378 // (actually applying the relocations will produce wrong results
1379 // as the section address will be added twice).
1380 if (!L && isa<MachOObjectFile>(&Obj))
1381 continue;
1382
1383 RelSecName = RelSecName.substr(
1384 RelSecName.find_first_not_of("._z")); // Skip . and _ prefixes.
1385
1386 // TODO: Add support for relocations in other sections as needed.
1387 // Record relocations for the debug_info and debug_line sections.
1388 DWARFSectionMap *Sec = mapNameToDWARFSection(RelSecName);
1389 RelocAddrMap *Map = Sec ? &Sec->Relocs : nullptr;
1390 if (!Map) {
1391 // Find debug_types relocs by section rather than name as there are
1392 // multiple, comdat grouped, debug_types sections.
1393 if (RelSecName == "debug_types")
1394 Map =
1395 &static_cast<DWARFSectionMap &>(TypesSections[*RelocatedSection])
1396 .Relocs;
1397 else if (RelSecName == "debug_types.dwo")
1398 Map = &static_cast<DWARFSectionMap &>(
1399 TypesDWOSections[*RelocatedSection])
1400 .Relocs;
1401 else
1402 continue;
1403 }
1404
1405 if (Section.relocation_begin() == Section.relocation_end())
1406 continue;
1407
1408 // Symbol to [address, section index] cache mapping.
1409 std::map<SymbolRef, SymInfo> AddrCache;
1410 for (const RelocationRef &Reloc : Section.relocations()) {
1411 // FIXME: it's not clear how to correctly handle scattered
1412 // relocations.
1413 if (isRelocScattered(Obj, Reloc))
1414 continue;
1415
1416 Expected<SymInfo> SymInfoOrErr =
1417 getSymbolInfo(Obj, Reloc, L, AddrCache);
1418 if (!SymInfoOrErr) {
1419 if (HandleError(SymInfoOrErr.takeError()) == ErrorPolicy::Halt)
1420 return;
1421 continue;
1422 }
1423
1424 object::RelocVisitor V(Obj);
1425 uint64_t Val = V.visit(Reloc.getType(), Reloc, SymInfoOrErr->Address);
1426 if (V.error()) {
1427 SmallString<32> Type;
1428 Reloc.getTypeName(Type);
1429 ErrorPolicy EP = HandleError(
1430 createError("failed to compute relocation: " + Type + ", ",
1431 errorCodeToError(object_error::parse_failed)));
1432 if (EP == ErrorPolicy::Halt)
1433 return;
1434 continue;
1435 }
1436 RelocAddrEntry Rel = {SymInfoOrErr->SectionIndex, Val};
1437 Map->insert({Reloc.getOffset(), Rel});
1438 }
Eric Christopher7370b552012-11-12 21:40:38 +00001439 }
George Rimare1c30f72017-08-15 15:54:43 +00001440
1441 for (SectionName &S : SectionNames)
1442 if (SectionAmountMap[S.Name] > 1)
1443 S.IsNameUnique = false;
Eric Christopher7370b552012-11-12 21:40:38 +00001444 }
Eric Christopher7370b552012-11-12 21:40:38 +00001445
Rafael Espindolac398e672017-07-19 22:27:28 +00001446 Optional<RelocAddrEntry> find(const DWARFSection &S,
1447 uint64_t Pos) const override {
1448 auto &Sec = static_cast<const DWARFSectionMap &>(S);
1449 RelocAddrMap::const_iterator AI = Sec.Relocs.find(Pos);
1450 if (AI == Sec.Relocs.end())
1451 return None;
1452 return AI->second;
Chris Bieneman2e752db2017-01-20 19:03:14 +00001453 }
Rafael Espindolac398e672017-07-19 22:27:28 +00001454
George Rimar6957ab52017-08-15 12:32:54 +00001455 const object::ObjectFile *getFile() const override { return Obj; }
1456
George Rimare1c30f72017-08-15 15:54:43 +00001457 ArrayRef<SectionName> getSectionNames() const override {
1458 return SectionNames;
1459 }
1460
Rafael Espindolac398e672017-07-19 22:27:28 +00001461 bool isLittleEndian() const override { return IsLittleEndian; }
1462 StringRef getAbbrevDWOSection() const override { return AbbrevDWOSection; }
1463 const DWARFSection &getLineDWOSection() const override {
1464 return LineDWOSection;
1465 }
1466 const DWARFSection &getLocDWOSection() const override {
1467 return LocDWOSection;
1468 }
1469 StringRef getStringDWOSection() const override { return StringDWOSection; }
1470 const DWARFSection &getStringOffsetDWOSection() const override {
1471 return StringOffsetDWOSection;
1472 }
1473 const DWARFSection &getRangeDWOSection() const override {
1474 return RangeDWOSection;
1475 }
1476 const DWARFSection &getAddrSection() const override { return AddrSection; }
1477 StringRef getCUIndexSection() const override { return CUIndexSection; }
1478 StringRef getGdbIndexSection() const override { return GdbIndexSection; }
1479 StringRef getTUIndexSection() const override { return TUIndexSection; }
1480
1481 // DWARF v5
1482 const DWARFSection &getStringOffsetSection() const override {
1483 return StringOffsetSection;
1484 }
Paul Robinsonb6aa01c2018-01-25 22:02:36 +00001485 StringRef getLineStringSection() const override { return LineStringSection; }
Rafael Espindolac398e672017-07-19 22:27:28 +00001486
1487 // Sections for DWARF5 split dwarf proposal.
1488 const DWARFSection &getInfoDWOSection() const override {
1489 return InfoDWOSection;
1490 }
1491 void forEachTypesDWOSections(
1492 function_ref<void(const DWARFSection &)> F) const override {
1493 for (auto &P : TypesDWOSections)
1494 F(P.second);
1495 }
1496
1497 StringRef getAbbrevSection() const override { return AbbrevSection; }
1498 const DWARFSection &getLocSection() const override { return LocSection; }
1499 StringRef getARangeSection() const override { return ARangeSection; }
1500 StringRef getDebugFrameSection() const override { return DebugFrameSection; }
1501 StringRef getEHFrameSection() const override { return EHFrameSection; }
1502 const DWARFSection &getLineSection() const override { return LineSection; }
1503 StringRef getStringSection() const override { return StringSection; }
1504 const DWARFSection &getRangeSection() const override { return RangeSection; }
James Henderson3fcc7452018-02-02 12:35:52 +00001505 const DWARFSection &getRnglistsSection() const override {
1506 return RnglistsSection;
1507 }
Rafael Espindolac398e672017-07-19 22:27:28 +00001508 StringRef getMacinfoSection() const override { return MacinfoSection; }
1509 StringRef getPubNamesSection() const override { return PubNamesSection; }
1510 StringRef getPubTypesSection() const override { return PubTypesSection; }
1511 StringRef getGnuPubNamesSection() const override {
1512 return GnuPubNamesSection;
1513 }
1514 StringRef getGnuPubTypesSection() const override {
1515 return GnuPubTypesSection;
1516 }
1517 const DWARFSection &getAppleNamesSection() const override {
1518 return AppleNamesSection;
1519 }
1520 const DWARFSection &getAppleTypesSection() const override {
1521 return AppleTypesSection;
1522 }
1523 const DWARFSection &getAppleNamespacesSection() const override {
1524 return AppleNamespacesSection;
1525 }
1526 const DWARFSection &getAppleObjCSection() const override {
1527 return AppleObjCSection;
1528 }
Pavel Labath3c9a9182018-01-29 11:08:32 +00001529 const DWARFSection &getDebugNamesSection() const override {
1530 return DebugNamesSection;
1531 }
Rafael Espindolac398e672017-07-19 22:27:28 +00001532
1533 StringRef getFileName() const override { return FileName; }
1534 uint8_t getAddressSize() const override { return AddressSize; }
1535 const DWARFSection &getInfoSection() const override { return InfoSection; }
1536 void forEachTypesSections(
1537 function_ref<void(const DWARFSection &)> F) const override {
1538 for (auto &P : TypesSections)
1539 F(P.second);
1540 }
1541};
Benjamin Kramer49a49fe2017-08-20 13:03:48 +00001542} // namespace
Rafael Espindolac398e672017-07-19 22:27:28 +00001543
1544std::unique_ptr<DWARFContext>
1545DWARFContext::create(const object::ObjectFile &Obj, const LoadedObjectInfo *L,
David Blaikiee5adb682017-07-30 01:34:08 +00001546 function_ref<ErrorPolicy(Error)> HandleError,
1547 std::string DWPName) {
Reid Kleckner388f8802017-07-19 23:42:53 +00001548 auto DObj = llvm::make_unique<DWARFObjInMemory>(Obj, L, HandleError);
David Blaikiee5adb682017-07-30 01:34:08 +00001549 return llvm::make_unique<DWARFContext>(std::move(DObj), std::move(DWPName));
Chris Bieneman2e752db2017-01-20 19:03:14 +00001550}
1551
Rafael Espindolac398e672017-07-19 22:27:28 +00001552std::unique_ptr<DWARFContext>
1553DWARFContext::create(const StringMap<std::unique_ptr<MemoryBuffer>> &Sections,
1554 uint8_t AddrSize, bool isLittleEndian) {
Reid Kleckner388f8802017-07-19 23:42:53 +00001555 auto DObj =
1556 llvm::make_unique<DWARFObjInMemory>(Sections, AddrSize, isLittleEndian);
David Blaikiee5adb682017-07-30 01:34:08 +00001557 return llvm::make_unique<DWARFContext>(std::move(DObj), "");
Rafael Espindola77e87b32017-07-07 05:36:53 +00001558}
Reid Klecknera0587362017-08-29 21:41:21 +00001559
1560Error DWARFContext::loadRegisterInfo(const object::ObjectFile &Obj) {
1561 // Detect the architecture from the object file. We usually don't need OS
1562 // info to lookup a target and create register info.
1563 Triple TT;
1564 TT.setArch(Triple::ArchType(Obj.getArch()));
1565 TT.setVendor(Triple::UnknownVendor);
1566 TT.setOS(Triple::UnknownOS);
1567 std::string TargetLookupError;
1568 const Target *TheTarget =
1569 TargetRegistry::lookupTarget(TT.str(), TargetLookupError);
1570 if (!TargetLookupError.empty())
1571 return make_error<StringError>(TargetLookupError, inconvertibleErrorCode());
1572 RegInfo.reset(TheTarget->createMCRegInfo(TT.str()));
1573 return Error::success();
1574}