blob: 5397994effc9808f4f4af4dec58ff2d863f64a62 [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"
28#include "llvm/DebugInfo/DWARF/DWARFDie.h"
29#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
30#include "llvm/DebugInfo/DWARF/DWARFGdbIndex.h"
31#include "llvm/DebugInfo/DWARF/DWARFSection.h"
David Blaikie65a8efe2015-11-11 19:28:21 +000032#include "llvm/DebugInfo/DWARF/DWARFUnitIndex.h"
Greg Claytonb8c162b2017-05-03 16:02:29 +000033#include "llvm/DebugInfo/DWARF/DWARFVerifier.h"
Reid Klecknera0587362017-08-29 21:41:21 +000034#include "llvm/MC/MCRegisterInfo.h"
George Rimar4bf30832017-01-11 15:26:41 +000035#include "llvm/Object/Decompressor.h"
Reid Klecknerdafc5d72016-07-06 16:56:42 +000036#include "llvm/Object/MachO.h"
Eugene Zelenko28db7e62017-03-01 01:14:23 +000037#include "llvm/Object/ObjectFile.h"
Reid Klecknerdafc5d72016-07-06 16:56:42 +000038#include "llvm/Object/RelocVisitor.h"
Eugene Zelenko28db7e62017-03-01 01:14:23 +000039#include "llvm/Support/Casting.h"
40#include "llvm/Support/DataExtractor.h"
Eugene Zelenko28db7e62017-03-01 01:14:23 +000041#include "llvm/Support/Error.h"
Benjamin Kramer07d4b1c2011-09-15 16:57:13 +000042#include "llvm/Support/Format.h"
Eugene Zelenko28db7e62017-03-01 01:14:23 +000043#include "llvm/Support/MemoryBuffer.h"
Adrian Prantl3ae35eb2017-09-13 22:09:01 +000044#include "llvm/Support/Path.h"
Reid Klecknera0587362017-08-29 21:41:21 +000045#include "llvm/Support/TargetRegistry.h"
Benjamin Kramera6002fd2011-09-14 01:09:52 +000046#include "llvm/Support/raw_ostream.h"
Benjamin Kramer2602ca62011-09-15 20:43:22 +000047#include <algorithm>
Eugene Zelenko28db7e62017-03-01 01:14:23 +000048#include <cstdint>
Greg Claytonc7695a82017-05-02 20:28:33 +000049#include <map>
Eugene Zelenko28db7e62017-03-01 01:14:23 +000050#include <string>
51#include <utility>
52#include <vector>
53
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000054using namespace llvm;
Benjamin Kramer6dda0322011-09-15 18:02:20 +000055using namespace dwarf;
Rafael Espindola4f60a382013-05-30 03:05:14 +000056using namespace object;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000057
Chandler Carruthe96dd892014-04-21 22:55:11 +000058#define DEBUG_TYPE "dwarf"
59
Eugene Zelenko2db0cfa2017-06-23 21:57:40 +000060using DWARFLineTable = DWARFDebugLine::LineTable;
61using FileLineInfoKind = DILineInfoSpecifier::FileLineInfoKind;
62using FunctionNameKind = DILineInfoSpecifier::FunctionNameKind;
Eric Christopher494109b2012-10-16 23:46:25 +000063
Reid Klecknera0587362017-08-29 21:41:21 +000064DWARFContext::DWARFContext(std::unique_ptr<const DWARFObject> DObj,
65 std::string DWPName)
66 : DIContext(CK_DWARF), DWPName(std::move(DWPName)), DObj(std::move(DObj)) {}
67
68DWARFContext::~DWARFContext() = default;
69
Adrian Prantl3dcd1222017-09-13 18:22:59 +000070/// Dump the UUID load command.
71static void dumpUUID(raw_ostream &OS, const ObjectFile &Obj) {
72 auto *MachO = dyn_cast<MachOObjectFile>(&Obj);
73 if (!MachO)
74 return;
75 for (auto LC : MachO->load_commands()) {
76 raw_ostream::uuid_t UUID;
77 if (LC.C.cmd == MachO::LC_UUID) {
78 if (LC.C.cmdsize < sizeof(UUID) + sizeof(LC.C)) {
79 OS << "error: UUID load command is too short.\n";
80 return;
81 }
82 OS << "UUID: ";
83 memcpy(&UUID, LC.Ptr+sizeof(LC.C), sizeof(UUID));
84 OS.write_uuid(UUID);
85 OS << ' ' << MachO->getFileFormatName();
86 OS << ' ' << MachO->getFileName() << '\n';
87 }
88 }
89}
90
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +000091using ContributionCollection =
92 std::vector<Optional<StrOffsetsContributionDescriptor>>;
93
94// Collect all the contributions to the string offsets table from all units,
95// sort them by their starting offsets and remove duplicates.
96static ContributionCollection
97collectContributionData(DWARFContext::cu_iterator_range CUs,
98 DWARFContext::tu_section_iterator_range TUSs) {
99 ContributionCollection Contributions;
100 for (const auto &CU : CUs)
101 Contributions.push_back(CU->getStringOffsetsTableContribution());
102 for (const auto &TUS : TUSs)
103 for (const auto &TU : TUS)
104 Contributions.push_back(TU->getStringOffsetsTableContribution());
105
106 // Sort the contributions so that any invalid ones are placed at
107 // the start of the contributions vector. This way they are reported
108 // first.
109 std::sort(Contributions.begin(), Contributions.end(),
110 [](const Optional<StrOffsetsContributionDescriptor> &L,
111 const Optional<StrOffsetsContributionDescriptor> &R) {
112 if (L && R) return L->Base < R->Base;
113 return R.hasValue();
114 });
115
116 // Uniquify contributions, as it is possible that units (specifically
117 // type units in dwo or dwp files) share contributions. We don't want
118 // to report them more than once.
119 Contributions.erase(
120 std::unique(Contributions.begin(), Contributions.end(),
121 [](const Optional<StrOffsetsContributionDescriptor> &L,
122 const Optional<StrOffsetsContributionDescriptor> &R) {
123 if (L && R)
124 return L->Base == R->Base && L->Size == R->Size;
125 return false;
126 }),
127 Contributions.end());
128 return Contributions;
129}
130
131static void dumpDWARFv5StringOffsetsSection(
132 raw_ostream &OS, StringRef SectionName, const DWARFObject &Obj,
133 const DWARFSection &StringOffsetsSection, StringRef StringSection,
134 DWARFContext::cu_iterator_range CUs,
135 DWARFContext::tu_section_iterator_range TUSs, bool LittleEndian) {
136 auto Contributions = collectContributionData(CUs, TUSs);
Rafael Espindolac398e672017-07-19 22:27:28 +0000137 DWARFDataExtractor StrOffsetExt(Obj, StringOffsetsSection, LittleEndian, 0);
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000138 DataExtractor StrData(StringSection, LittleEndian, 0);
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000139 uint64_t SectionSize = StringOffsetsSection.Data.size();
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000140 uint32_t Offset = 0;
141 for (auto &Contribution : Contributions) {
142 // Report an ill-formed contribution.
143 if (!Contribution) {
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000144 OS << "error: invalid contribution to string offsets table in section ."
145 << SectionName << ".\n";
146 return;
147 }
148
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000149 dwarf::DwarfFormat Format = Contribution->getFormat();
150 uint16_t Version = Contribution->getVersion();
151 uint64_t ContributionHeader = Contribution->Base;
152 // In DWARF v5 there is a contribution header that immediately precedes
153 // the string offsets base (the location we have previously retrieved from
154 // the CU DIE's DW_AT_str_offsets attribute). The header is located either
155 // 8 or 16 bytes before the base, depending on the contribution's format.
156 if (Version >= 5)
157 ContributionHeader -= Format == DWARF32 ? 8 : 16;
158
159 // Detect overlapping contributions.
160 if (Offset > ContributionHeader) {
161 OS << "error: overlapping contributions to string offsets table in "
162 "section ."
163 << SectionName << ".\n";
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000164 return;
165 }
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000166 // Report a gap in the table.
167 if (Offset < ContributionHeader) {
168 OS << format("0x%8.8x: Gap, length = ", Offset);
169 OS << (ContributionHeader - Offset) << "\n";
170 }
171 OS << format("0x%8.8x: ", ContributionHeader);
172 OS << "Contribution size = " << Contribution->Size
173 << ", Format = " << (Format == DWARF32 ? "DWARF32" : "DWARF64")
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000174 << ", Version = " << Version << "\n";
175
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000176 Offset = Contribution->Base;
177 unsigned EntrySize = Contribution->getDwarfOffsetByteSize();
178 while (Offset - Contribution->Base < Contribution->Size) {
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000179 OS << format("0x%8.8x: ", Offset);
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000180 // FIXME: We can only extract strings if the offset fits in 32 bits.
Paul Robinson17536b92017-06-29 16:52:08 +0000181 uint64_t StringOffset =
182 StrOffsetExt.getRelocatedValue(EntrySize, &Offset);
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000183 // Extract the string if we can and display it. Otherwise just report
184 // the offset.
185 if (StringOffset <= std::numeric_limits<uint32_t>::max()) {
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000186 uint32_t StringOffset32 = (uint32_t)StringOffset;
Simon Dardisb1b52c02017-08-07 16:08:11 +0000187 OS << format("%8.8x ", StringOffset32);
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000188 const char *S = StrData.getCStr(&StringOffset32);
189 if (S)
190 OS << format("\"%s\"", S);
191 } else
Simon Dardisec4ea992017-08-07 13:30:03 +0000192 OS << format("%16.16" PRIx64 " ", StringOffset);
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000193 OS << "\n";
194 }
195 }
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000196 // Report a gap at the end of the table.
197 if (Offset < SectionSize) {
198 OS << format("0x%8.8x: Gap, length = ", Offset);
199 OS << (SectionSize - Offset) << "\n";
200 }
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000201}
202
203// Dump a DWARF string offsets section. This may be a DWARF v5 formatted
204// string offsets section, where each compile or type unit contributes a
205// number of entries (string offsets), with each contribution preceded by
206// a header containing size and version number. Alternatively, it may be a
207// monolithic series of string offsets, as generated by the pre-DWARF v5
208// implementation of split DWARF.
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000209static void dumpStringOffsetsSection(
210 raw_ostream &OS, StringRef SectionName, const DWARFObject &Obj,
211 const DWARFSection &StringOffsetsSection, StringRef StringSection,
212 DWARFContext::cu_iterator_range CUs,
213 DWARFContext::tu_section_iterator_range TUSs, bool LittleEndian,
214 unsigned MaxVersion) {
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000215 // If we have at least one (compile or type) unit with DWARF v5 or greater,
216 // we assume that the section is formatted like a DWARF v5 string offsets
217 // section.
218 if (MaxVersion >= 5)
Rafael Espindolac398e672017-07-19 22:27:28 +0000219 dumpDWARFv5StringOffsetsSection(OS, SectionName, Obj, StringOffsetsSection,
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000220 StringSection, CUs, TUSs, LittleEndian);
Wolfgang Pieb77d3e932017-06-06 01:22:34 +0000221 else {
222 DataExtractor strOffsetExt(StringOffsetsSection.Data, LittleEndian, 0);
223 uint32_t offset = 0;
224 uint64_t size = StringOffsetsSection.Data.size();
225 // Ensure that size is a multiple of the size of an entry.
226 if (size & ((uint64_t)(sizeof(uint32_t) - 1))) {
227 OS << "error: size of ." << SectionName << " is not a multiple of "
228 << sizeof(uint32_t) << ".\n";
229 size &= -(uint64_t)sizeof(uint32_t);
230 }
231 DataExtractor StrData(StringSection, LittleEndian, 0);
232 while (offset < size) {
233 OS << format("0x%8.8x: ", offset);
234 uint32_t StringOffset = strOffsetExt.getU32(&offset);
235 OS << format("%8.8x ", StringOffset);
236 const char *S = StrData.getCStr(&StringOffset);
237 if (S)
238 OS << format("\"%s\"", S);
239 OS << "\n";
240 }
241 }
242}
243
Paul Robinson63811a42017-11-22 15:33:17 +0000244// We want to supply the Unit associated with a .debug_line[.dwo] table when
245// we dump it, if possible, but still dump the table even if there isn't a Unit.
246// Therefore, collect up handles on all the Units that point into the
247// line-table section.
248typedef std::map<uint64_t, DWARFUnit *> LineToUnitMap;
249
250static LineToUnitMap
251buildLineToUnitMap(DWARFContext::cu_iterator_range CUs,
252 DWARFContext::tu_section_iterator_range TUSections) {
253 LineToUnitMap LineToUnit;
254 for (const auto &CU : CUs)
255 if (auto CUDIE = CU->getUnitDIE())
256 if (auto StmtOffset = toSectionOffset(CUDIE.find(DW_AT_stmt_list)))
257 LineToUnit.insert(std::make_pair(*StmtOffset, &*CU));
258 for (const auto &TUS : TUSections)
259 for (const auto &TU : TUS)
260 if (auto TUDIE = TU->getUnitDIE())
261 if (auto StmtOffset = toSectionOffset(TUDIE.find(DW_AT_stmt_list)))
262 LineToUnit.insert(std::make_pair(*StmtOffset, &*TU));
263 return LineToUnit;
264}
265
Adrian Prantl057d3362017-09-15 23:04:04 +0000266void DWARFContext::dump(
267 raw_ostream &OS, DIDumpOptions DumpOpts,
268 std::array<Optional<uint64_t>, DIDT_ID_Count> DumpOffsets) {
269
270 Optional<uint64_t> DumpOffset;
Adrian Prantl7bc1b282017-09-11 22:59:45 +0000271 uint64_t DumpType = DumpOpts.DumpType;
Adrian Prantlf4bc1f72017-06-01 18:18:23 +0000272
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000273 StringRef Extension = sys::path::extension(DObj->getFileName());
274 bool IsDWO = (Extension == ".dwo") || (Extension == ".dwp");
275
276 // Print UUID header.
Adrian Prantl3dcd1222017-09-13 18:22:59 +0000277 const auto *ObjFile = DObj->getFile();
Adrian Prantl3dcd1222017-09-13 18:22:59 +0000278 if (DumpType & DIDT_UUID)
279 dumpUUID(OS, *ObjFile);
280
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000281 // Print a header for each explicitly-requested section.
282 // Otherwise just print one for non-empty sections.
Adrian Prantl057d3362017-09-15 23:04:04 +0000283 // Only print empty .dwo section headers when dumping a .dwo file.
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000284 bool Explicit = DumpType != DIDT_All && !IsDWO;
Adrian Prantl057d3362017-09-15 23:04:04 +0000285 bool ExplicitDWO = Explicit && IsDWO;
286 auto shouldDump = [&](bool Explicit, const char *Name, unsigned ID,
287 StringRef Section) {
288 DumpOffset = DumpOffsets[ID];
289 unsigned Mask = 1U << ID;
290 bool Should = (DumpType & Mask) && (Explicit || !Section.empty());
Adrian Prantl84168022017-09-15 17:39:50 +0000291 if (Should)
Adrian Prantl057d3362017-09-15 23:04:04 +0000292 OS << "\n" << Name << " contents:\n";
Adrian Prantl84168022017-09-15 17:39:50 +0000293 return Should;
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000294 };
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000295
296 // Dump individual sections.
Adrian Prantl057d3362017-09-15 23:04:04 +0000297 if (shouldDump(Explicit, ".debug_abbrev", DIDT_ID_DebugAbbrev,
298 DObj->getAbbrevSection()))
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000299 getDebugAbbrev()->dump(OS);
Adrian Prantl057d3362017-09-15 23:04:04 +0000300 if (shouldDump(ExplicitDWO, ".debug_abbrev.dwo", DIDT_ID_DebugAbbrev,
301 DObj->getAbbrevDWOSection()))
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000302 getDebugAbbrevDWO()->dump(OS);
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000303
Adrian Prantl057d3362017-09-15 23:04:04 +0000304 auto dumpDebugInfo = [&](bool IsExplicit, const char *Name,
305 DWARFSection Section, cu_iterator_range CUs) {
306 if (shouldDump(IsExplicit, Name, DIDT_ID_DebugInfo, Section.Data)) {
Adrian Prantlc8d86532017-09-18 21:44:40 +0000307 if (DumpOffset)
Adrian Prantld3f9f212017-09-20 17:44:00 +0000308 getDIEForOffset(DumpOffset.getValue())
309 .dump(OS, 0, DumpOpts.noImplicitRecursion());
Adrian Prantlc8d86532017-09-18 21:44:40 +0000310 else
311 for (const auto &CU : CUs)
Adrian Prantl057d3362017-09-15 23:04:04 +0000312 CU->dump(OS, DumpOpts);
313 }
314 };
315 dumpDebugInfo(Explicit, ".debug_info", DObj->getInfoSection(),
316 compile_units());
317 dumpDebugInfo(ExplicitDWO, ".debug_info.dwo", DObj->getInfoDWOSection(),
318 dwo_compile_units());
David Blaikie622dce42014-01-08 23:29:59 +0000319
Adrian Prantl099d7e42017-09-16 16:58:18 +0000320 auto dumpDebugType = [&](const char *Name,
321 tu_section_iterator_range TUSections) {
322 OS << '\n' << Name << " contents:\n";
323 DumpOffset = DumpOffsets[DIDT_ID_DebugTypes];
324 for (const auto &TUS : TUSections)
325 for (const auto &TU : TUS)
326 if (DumpOffset)
Adrian Prantld3f9f212017-09-20 17:44:00 +0000327 TU->getDIEForOffset(*DumpOffset)
328 .dump(OS, 0, DumpOpts.noImplicitRecursion());
Adrian Prantl099d7e42017-09-16 16:58:18 +0000329 else
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000330 TU->dump(OS, DumpOpts);
Adrian Prantl099d7e42017-09-16 16:58:18 +0000331 };
332 if ((DumpType & DIDT_DebugTypes)) {
333 if (Explicit || getNumTypeUnits())
334 dumpDebugType(".debug_types", type_unit_sections());
335 if (ExplicitDWO || getNumDWOTypeUnits())
336 dumpDebugType(".debug_types.dwo", dwo_type_unit_sections());
David Blaikie03c089c2013-09-23 22:44:47 +0000337 }
338
Adrian Prantl057d3362017-09-15 23:04:04 +0000339 if (shouldDump(Explicit, ".debug_loc", DIDT_ID_DebugLoc,
Adrian Prantl84168022017-09-15 17:39:50 +0000340 DObj->getLocSection().Data)) {
Jonas Devlieghere622c5632017-09-27 09:33:36 +0000341 getDebugLoc()->dump(OS, getRegisterInfo(), DumpOffset);
David Blaikie18e73502013-06-19 21:37:13 +0000342 }
Adrian Prantl057d3362017-09-15 23:04:04 +0000343 if (shouldDump(ExplicitDWO, ".debug_loc.dwo", DIDT_ID_DebugLoc,
Adrian Prantl84168022017-09-15 17:39:50 +0000344 DObj->getLocDWOSection().Data)) {
Jonas Devlieghere622c5632017-09-27 09:33:36 +0000345 getDebugLocDWO()->dump(OS, getRegisterInfo(), DumpOffset);
David Blaikie9c550ac2014-03-25 01:44:02 +0000346 }
347
Jonas Devliegherec0a758d2017-09-18 14:15:57 +0000348 if (shouldDump(Explicit, ".debug_frame", DIDT_ID_DebugFrame,
Adrian Prantl62528e62017-09-21 18:52:03 +0000349 DObj->getDebugFrameSection()))
350 getDebugFrame()->dump(OS, DumpOffset);
Jonas Devliegherec0a758d2017-09-18 14:15:57 +0000351
352 if (shouldDump(Explicit, ".eh_frame", DIDT_ID_DebugFrame,
Adrian Prantl62528e62017-09-21 18:52:03 +0000353 DObj->getEHFrameSection()))
354 getEHFrame()->dump(OS, DumpOffset);
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000355
Adrian Prantl7bc1b282017-09-11 22:59:45 +0000356 if (DumpType & DIDT_DebugMacro) {
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000357 if (Explicit || !getDebugMacro()->empty()) {
358 OS << "\n.debug_macinfo contents:\n";
359 getDebugMacro()->dump(OS);
360 }
Amjad Aboude59cc3e2015-11-12 09:38:54 +0000361 }
362
Adrian Prantl057d3362017-09-15 23:04:04 +0000363 if (shouldDump(Explicit, ".debug_aranges", DIDT_ID_DebugAranges,
Adrian Prantl84168022017-09-15 17:39:50 +0000364 DObj->getARangeSection())) {
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000365 uint32_t offset = 0;
Rafael Espindolac398e672017-07-19 22:27:28 +0000366 DataExtractor arangesData(DObj->getARangeSection(), isLittleEndian(), 0);
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000367 DWARFDebugArangeSet set;
368 while (set.extract(arangesData, &offset))
369 set.dump(OS);
370 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000371
Adrian Prantl057d3362017-09-15 23:04:04 +0000372 if (shouldDump(Explicit, ".debug_line", DIDT_ID_DebugLine,
Adrian Prantl84168022017-09-15 17:39:50 +0000373 DObj->getLineSection().Data)) {
Paul Robinson63811a42017-11-22 15:33:17 +0000374 LineToUnitMap LineToUnit =
375 buildLineToUnitMap(compile_units(), type_unit_sections());
376 unsigned Offset = 0;
377 DWARFDataExtractor LineData(*DObj, DObj->getLineSection(), isLittleEndian(),
378 0);
379 while (Offset < LineData.getData().size()) {
380 DWARFUnit *U = nullptr;
381 auto It = LineToUnit.find(Offset);
Paul Robinson511b54c2017-11-22 15:48:30 +0000382 if (It != LineToUnit.end())
Paul Robinson63811a42017-11-22 15:33:17 +0000383 U = It->second;
Paul Robinson511b54c2017-11-22 15:48:30 +0000384 LineData.setAddressSize(U ? U->getAddressByteSize() : 0);
Paul Robinson63811a42017-11-22 15:33:17 +0000385 DWARFDebugLine::LineTable LineTable;
386 if (DumpOffset && Offset != *DumpOffset) {
387 // Find the size of this part of the line table section and skip it.
388 unsigned OldOffset = Offset;
389 LineTable.Prologue.parse(LineData, &Offset, U);
390 Offset = OldOffset + LineTable.Prologue.TotalLength +
391 LineTable.Prologue.sizeofTotalLength();
Alexey Samsonov7a18c062015-05-19 21:54:32 +0000392 continue;
Paul Robinson63811a42017-11-22 15:33:17 +0000393 }
394 // Verbose dumping is done during parsing and not on the intermediate
395 // representation.
396 OS << "debug_line[" << format("0x%8.8x", Offset) << "]\n";
Paul Robinsonab69b472017-12-01 18:25:30 +0000397 unsigned OldOffset = Offset;
Paul Robinson63811a42017-11-22 15:33:17 +0000398 if (DumpOpts.Verbose) {
399 LineTable.parse(LineData, &Offset, U, &OS);
400 } else {
401 LineTable.parse(LineData, &Offset, U);
402 LineTable.dump(OS);
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000403 }
Paul Robinsonab69b472017-12-01 18:25:30 +0000404 // Check for unparseable prologue, to avoid infinite loops.
405 if (OldOffset == Offset)
406 break;
Benjamin Kramer6dda0322011-09-15 18:02:20 +0000407 }
408 }
Benjamin Kramer07d4b1c2011-09-15 16:57:13 +0000409
Adrian Prantl057d3362017-09-15 23:04:04 +0000410 if (shouldDump(ExplicitDWO, ".debug_line.dwo", DIDT_ID_DebugLine,
Adrian Prantl84168022017-09-15 17:39:50 +0000411 DObj->getLineDWOSection().Data)) {
Paul Robinson63811a42017-11-22 15:33:17 +0000412 LineToUnitMap LineToUnit =
413 buildLineToUnitMap(dwo_compile_units(), dwo_type_unit_sections());
414 unsigned Offset = 0;
415 DWARFDataExtractor LineData(*DObj, DObj->getLineDWOSection(),
416 isLittleEndian(), 0);
417 while (Offset < LineData.getData().size()) {
418 DWARFUnit *U = nullptr;
419 auto It = LineToUnit.find(Offset);
420 if (It != LineToUnit.end())
421 U = It->second;
422 DWARFDebugLine::LineTable LineTable;
Paul Robinson6ca1dd62017-11-22 18:23:55 +0000423 unsigned OldOffset = Offset;
Paul Robinson63811a42017-11-22 15:33:17 +0000424 if (!LineTable.Prologue.parse(LineData, &Offset, U))
425 break;
Paul Robinson6ca1dd62017-11-22 18:23:55 +0000426 if (!DumpOffset || OldOffset == *DumpOffset)
427 LineTable.dump(OS);
Alexey Samsonov110d5952014-04-30 00:09:19 +0000428 }
David Blaikie1d4736e2014-02-24 23:58:54 +0000429 }
430
Adrian Prantl057d3362017-09-15 23:04:04 +0000431 if (shouldDump(Explicit, ".debug_cu_index", DIDT_ID_DebugCUIndex,
Adrian Prantl84168022017-09-15 17:39:50 +0000432 DObj->getCUIndexSection())) {
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000433 getCUIndex().dump(OS);
434 }
435
Adrian Prantl057d3362017-09-15 23:04:04 +0000436 if (shouldDump(Explicit, ".debug_tu_index", DIDT_ID_DebugTUIndex,
Adrian Prantl84168022017-09-15 17:39:50 +0000437 DObj->getTUIndexSection())) {
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000438 getTUIndex().dump(OS);
439 }
440
Adrian Prantl057d3362017-09-15 23:04:04 +0000441 if (shouldDump(Explicit, ".debug_str", DIDT_ID_DebugStr,
Adrian Prantl84168022017-09-15 17:39:50 +0000442 DObj->getStringSection())) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000443 DataExtractor strData(DObj->getStringSection(), isLittleEndian(), 0);
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000444 uint32_t offset = 0;
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000445 uint32_t strOffset = 0;
446 while (const char *s = strData.getCStr(&offset)) {
447 OS << format("0x%8.8x: \"%s\"\n", strOffset, s);
448 strOffset = offset;
449 }
Benjamin Kramer07d4b1c2011-09-15 16:57:13 +0000450 }
Adrian Prantl057d3362017-09-15 23:04:04 +0000451 if (shouldDump(ExplicitDWO, ".debug_str.dwo", DIDT_ID_DebugStr,
Adrian Prantl84168022017-09-15 17:39:50 +0000452 DObj->getStringDWOSection())) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000453 DataExtractor strDWOData(DObj->getStringDWOSection(), isLittleEndian(), 0);
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000454 uint32_t offset = 0;
David Blaikie66865d62014-01-09 00:13:35 +0000455 uint32_t strDWOOffset = 0;
456 while (const char *s = strDWOData.getCStr(&offset)) {
457 OS << format("0x%8.8x: \"%s\"\n", strDWOOffset, s);
458 strDWOOffset = offset;
David Blaikie622dce42014-01-08 23:29:59 +0000459 }
David Blaikie66865d62014-01-09 00:13:35 +0000460 }
David Blaikie622dce42014-01-08 23:29:59 +0000461
Adrian Prantl057d3362017-09-15 23:04:04 +0000462 if (shouldDump(Explicit, ".debug_ranges", DIDT_ID_DebugRanges,
Adrian Prantl84168022017-09-15 17:39:50 +0000463 DObj->getRangeSection().Data)) {
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000464 // In fact, different compile units may have different address byte
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000465 // sizes, but for simplicity we just use the address byte size of the
466 // last compile unit (there is no easy and fast way to associate address
467 // range list and the compile unit it describes).
468 // FIXME: savedAddressByteSize seems sketchy.
Paul Robinson63811a42017-11-22 15:33:17 +0000469 uint8_t savedAddressByteSize = 0;
470 for (const auto &CU : compile_units()) {
471 savedAddressByteSize = CU->getAddressByteSize();
472 break;
473 }
Rafael Espindolac398e672017-07-19 22:27:28 +0000474 DWARFDataExtractor rangesData(*DObj, DObj->getRangeSection(),
475 isLittleEndian(), savedAddressByteSize);
Adrian Prantl3ae35eb2017-09-13 22:09:01 +0000476 uint32_t offset = 0;
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000477 DWARFDebugRangeList rangeList;
Paul Robinson17536b92017-06-29 16:52:08 +0000478 while (rangeList.extract(rangesData, &offset))
Eli Bendersky7a94daa2013-01-25 20:26:43 +0000479 rangeList.dump(OS);
Eric Christopherda4b2192013-01-02 23:52:13 +0000480 }
Eric Christopher962c9082013-01-15 23:56:56 +0000481
Adrian Prantl057d3362017-09-15 23:04:04 +0000482 if (shouldDump(Explicit, ".debug_pubnames", DIDT_ID_DebugPubnames,
Adrian Prantl84168022017-09-15 17:39:50 +0000483 DObj->getPubNamesSection()))
Rafael Espindolac398e672017-07-19 22:27:28 +0000484 DWARFDebugPubTable(DObj->getPubNamesSection(), isLittleEndian(), false)
Adrian Prantl84168022017-09-15 17:39:50 +0000485 .dump(OS);
Krzysztof Parzyszek97438dc2013-02-12 16:20:28 +0000486
Adrian Prantl057d3362017-09-15 23:04:04 +0000487 if (shouldDump(Explicit, ".debug_pubtypes", DIDT_ID_DebugPubtypes,
Adrian Prantl84168022017-09-15 17:39:50 +0000488 DObj->getPubTypesSection()))
Rafael Espindolac398e672017-07-19 22:27:28 +0000489 DWARFDebugPubTable(DObj->getPubTypesSection(), isLittleEndian(), false)
Adrian Prantl84168022017-09-15 17:39:50 +0000490 .dump(OS);
Eric Christopher4c7e6ba2013-09-25 23:02:41 +0000491
Adrian Prantl057d3362017-09-15 23:04:04 +0000492 if (shouldDump(Explicit, ".debug_gnu_pubnames", DIDT_ID_DebugGnuPubnames,
Adrian Prantl84168022017-09-15 17:39:50 +0000493 DObj->getGnuPubNamesSection()))
Rafael Espindolac398e672017-07-19 22:27:28 +0000494 DWARFDebugPubTable(DObj->getGnuPubNamesSection(), isLittleEndian(),
George Rimare71e33f2016-12-17 09:10:32 +0000495 true /* GnuStyle */)
Adrian Prantl84168022017-09-15 17:39:50 +0000496 .dump(OS);
David Blaikieecd21ff2013-09-24 19:50:00 +0000497
Adrian Prantl057d3362017-09-15 23:04:04 +0000498 if (shouldDump(Explicit, ".debug_gnu_pubtypes", DIDT_ID_DebugGnuPubtypes,
Adrian Prantl84168022017-09-15 17:39:50 +0000499 DObj->getGnuPubTypesSection()))
Rafael Espindolac398e672017-07-19 22:27:28 +0000500 DWARFDebugPubTable(DObj->getGnuPubTypesSection(), isLittleEndian(),
George Rimare71e33f2016-12-17 09:10:32 +0000501 true /* GnuStyle */)
Adrian Prantl84168022017-09-15 17:39:50 +0000502 .dump(OS);
David Blaikie404d3042013-09-19 23:01:29 +0000503
Adrian Prantl057d3362017-09-15 23:04:04 +0000504 if (shouldDump(Explicit, ".debug_str_offsets", DIDT_ID_DebugStrOffsets,
Adrian Prantl84168022017-09-15 17:39:50 +0000505 DObj->getStringOffsetSection().Data))
Rafael Espindolac398e672017-07-19 22:27:28 +0000506 dumpStringOffsetsSection(
507 OS, "debug_str_offsets", *DObj, DObj->getStringOffsetSection(),
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000508 DObj->getStringSection(), compile_units(), type_unit_sections(),
509 isLittleEndian(), getMaxVersion());
Adrian Prantl057d3362017-09-15 23:04:04 +0000510 if (shouldDump(ExplicitDWO, ".debug_str_offsets.dwo", DIDT_ID_DebugStrOffsets,
Adrian Prantl84168022017-09-15 17:39:50 +0000511 DObj->getStringOffsetDWOSection().Data))
Rafael Espindolac398e672017-07-19 22:27:28 +0000512 dumpStringOffsetsSection(
513 OS, "debug_str_offsets.dwo", *DObj, DObj->getStringOffsetDWOSection(),
Wolfgang Pieb6ecd6a82017-12-21 19:38:13 +0000514 DObj->getStringDWOSection(), dwo_compile_units(),
515 dwo_type_unit_sections(), isLittleEndian(), getMaxVersion());
Frederic Risse837ec22014-11-14 16:15:53 +0000516
Adrian Prantl057d3362017-09-15 23:04:04 +0000517 if (shouldDump(Explicit, ".gnu_index", DIDT_ID_GdbIndex,
Adrian Prantl84168022017-09-15 17:39:50 +0000518 DObj->getGdbIndexSection())) {
George Rimar4f82df52016-09-23 11:01:53 +0000519 getGdbIndex().dump(OS);
520 }
521
Adrian Prantl057d3362017-09-15 23:04:04 +0000522 if (shouldDump(Explicit, ".apple_names", DIDT_ID_AppleNames,
Adrian Prantl84168022017-09-15 17:39:50 +0000523 DObj->getAppleNamesSection().Data))
Adrian Prantl99fdb9d2017-09-28 18:10:52 +0000524 getAppleNames().dump(OS);
Frederic Risse837ec22014-11-14 16:15:53 +0000525
Adrian Prantl057d3362017-09-15 23:04:04 +0000526 if (shouldDump(Explicit, ".apple_types", DIDT_ID_AppleTypes,
Adrian Prantl84168022017-09-15 17:39:50 +0000527 DObj->getAppleTypesSection().Data))
Adrian Prantl714ee4d2017-09-29 00:33:22 +0000528 getAppleTypes().dump(OS);
Frederic Risse837ec22014-11-14 16:15:53 +0000529
Adrian Prantl057d3362017-09-15 23:04:04 +0000530 if (shouldDump(Explicit, ".apple_namespaces", DIDT_ID_AppleNamespaces,
Adrian Prantl84168022017-09-15 17:39:50 +0000531 DObj->getAppleNamespacesSection().Data))
Adrian Prantlf51e7802017-09-29 00:52:33 +0000532 getAppleNamespaces().dump(OS);
Frederic Risse837ec22014-11-14 16:15:53 +0000533
Adrian Prantl057d3362017-09-15 23:04:04 +0000534 if (shouldDump(Explicit, ".apple_objc", DIDT_ID_AppleObjC,
Adrian Prantl84168022017-09-15 17:39:50 +0000535 DObj->getAppleObjCSection().Data))
Adrian Prantlf51e7802017-09-29 00:52:33 +0000536 getAppleObjC().dump(OS);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000537}
538
David Blaikie15d85fc2017-05-23 06:48:53 +0000539DWARFCompileUnit *DWARFContext::getDWOCompileUnitForHash(uint64_t Hash) {
David Blaikiee79dda32017-09-19 18:36:11 +0000540 DWOCUs.parseDWO(*this, DObj->getInfoDWOSection(), true);
David Blaikiea62f1cb2017-07-30 15:15:58 +0000541
David Blaikieebac0b92017-07-30 08:12:07 +0000542 if (const auto &CUI = getCUIndex()) {
543 if (const auto *R = CUI.getFromHash(Hash))
David Blaikiee79dda32017-09-19 18:36:11 +0000544 return DWOCUs.getUnitForIndexEntry(*R);
David Blaikieebac0b92017-07-30 08:12:07 +0000545 return nullptr;
546 }
547
548 // If there's no index, just search through the CUs in the DWO - there's
549 // probably only one unless this is something like LTO - though an in-process
550 // built/cached lookup table could be used in that case to improve repeated
551 // lookups of different CUs in the DWO.
David Blaikie15d85fc2017-05-23 06:48:53 +0000552 for (const auto &DWOCU : dwo_compile_units())
553 if (DWOCU->getDWOId() == Hash)
554 return DWOCU.get();
555 return nullptr;
556}
557
Greg Claytonc7695a82017-05-02 20:28:33 +0000558DWARFDie DWARFContext::getDIEForOffset(uint32_t Offset) {
559 parseCompileUnits();
560 if (auto *CU = CUs.getUnitForOffset(Offset))
561 return CU->getDIEForOffset(Offset);
562 return DWARFDie();
563}
564
Jonas Devliegherec0a758d2017-09-18 14:15:57 +0000565bool DWARFContext::verify(raw_ostream &OS, DIDumpOptions DumpOpts) {
Greg Claytonc7695a82017-05-02 20:28:33 +0000566 bool Success = true;
Jonas Devlieghere27476ce2017-09-13 09:43:05 +0000567 DWARFVerifier verifier(OS, *this, DumpOpts);
Spyridoula Gravani73e17962017-07-27 00:59:33 +0000568
Spyridoula Gravani364b5352017-07-20 02:06:52 +0000569 Success &= verifier.handleDebugAbbrev();
Jonas Devliegherec0a758d2017-09-18 14:15:57 +0000570 if (DumpOpts.DumpType & DIDT_DebugInfo)
Spyridoula Gravani73e17962017-07-27 00:59:33 +0000571 Success &= verifier.handleDebugInfo();
Jonas Devliegherec0a758d2017-09-18 14:15:57 +0000572 if (DumpOpts.DumpType & DIDT_DebugLine)
Spyridoula Gravani73e17962017-07-27 00:59:33 +0000573 Success &= verifier.handleDebugLine();
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000574 Success &= verifier.handleAccelTables();
Greg Clayton48432cf2017-05-01 22:07:02 +0000575 return Success;
576}
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000577
David Blaikie82641be2015-11-17 00:39:55 +0000578const DWARFUnitIndex &DWARFContext::getCUIndex() {
579 if (CUIndex)
580 return *CUIndex;
581
Rafael Espindolac398e672017-07-19 22:27:28 +0000582 DataExtractor CUIndexData(DObj->getCUIndexSection(), isLittleEndian(), 0);
David Blaikie82641be2015-11-17 00:39:55 +0000583
David Blaikieb073cb92015-12-02 06:21:34 +0000584 CUIndex = llvm::make_unique<DWARFUnitIndex>(DW_SECT_INFO);
David Blaikie82641be2015-11-17 00:39:55 +0000585 CUIndex->parse(CUIndexData);
586 return *CUIndex;
587}
588
589const DWARFUnitIndex &DWARFContext::getTUIndex() {
590 if (TUIndex)
591 return *TUIndex;
592
Rafael Espindolac398e672017-07-19 22:27:28 +0000593 DataExtractor TUIndexData(DObj->getTUIndexSection(), isLittleEndian(), 0);
David Blaikie82641be2015-11-17 00:39:55 +0000594
David Blaikieb073cb92015-12-02 06:21:34 +0000595 TUIndex = llvm::make_unique<DWARFUnitIndex>(DW_SECT_TYPES);
David Blaikie82641be2015-11-17 00:39:55 +0000596 TUIndex->parse(TUIndexData);
597 return *TUIndex;
598}
599
George Rimar4f82df52016-09-23 11:01:53 +0000600DWARFGdbIndex &DWARFContext::getGdbIndex() {
601 if (GdbIndex)
602 return *GdbIndex;
603
Rafael Espindolac398e672017-07-19 22:27:28 +0000604 DataExtractor GdbIndexData(DObj->getGdbIndexSection(), true /*LE*/, 0);
George Rimar4f82df52016-09-23 11:01:53 +0000605 GdbIndex = llvm::make_unique<DWARFGdbIndex>();
606 GdbIndex->parse(GdbIndexData);
607 return *GdbIndex;
608}
609
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000610const DWARFDebugAbbrev *DWARFContext::getDebugAbbrev() {
611 if (Abbrev)
612 return Abbrev.get();
613
Rafael Espindolac398e672017-07-19 22:27:28 +0000614 DataExtractor abbrData(DObj->getAbbrevSection(), isLittleEndian(), 0);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000615
616 Abbrev.reset(new DWARFDebugAbbrev());
Alexey Samsonov4316df52014-04-25 21:10:56 +0000617 Abbrev->extract(abbrData);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000618 return Abbrev.get();
619}
620
Eric Christopherda4b2192013-01-02 23:52:13 +0000621const DWARFDebugAbbrev *DWARFContext::getDebugAbbrevDWO() {
622 if (AbbrevDWO)
623 return AbbrevDWO.get();
624
Rafael Espindolac398e672017-07-19 22:27:28 +0000625 DataExtractor abbrData(DObj->getAbbrevDWOSection(), isLittleEndian(), 0);
Eric Christopherda4b2192013-01-02 23:52:13 +0000626 AbbrevDWO.reset(new DWARFDebugAbbrev());
Alexey Samsonov4316df52014-04-25 21:10:56 +0000627 AbbrevDWO->extract(abbrData);
Eric Christopherda4b2192013-01-02 23:52:13 +0000628 return AbbrevDWO.get();
629}
630
David Blaikie18e73502013-06-19 21:37:13 +0000631const DWARFDebugLoc *DWARFContext::getDebugLoc() {
632 if (Loc)
633 return Loc.get();
634
Paul Robinson17536b92017-06-29 16:52:08 +0000635 Loc.reset(new DWARFDebugLoc);
David Blaikie18e73502013-06-19 21:37:13 +0000636 // assume all compile units have the same address byte size
Paul Robinson17536b92017-06-29 16:52:08 +0000637 if (getNumCompileUnits()) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000638 DWARFDataExtractor LocData(*DObj, DObj->getLocSection(), isLittleEndian(),
Paul Robinson17536b92017-06-29 16:52:08 +0000639 getCompileUnitAtIndex(0)->getAddressByteSize());
640 Loc->parse(LocData);
641 }
David Blaikie18e73502013-06-19 21:37:13 +0000642 return Loc.get();
643}
644
David Blaikie9c550ac2014-03-25 01:44:02 +0000645const DWARFDebugLocDWO *DWARFContext::getDebugLocDWO() {
646 if (LocDWO)
647 return LocDWO.get();
648
Rafael Espindolac398e672017-07-19 22:27:28 +0000649 DataExtractor LocData(DObj->getLocDWOSection().Data, isLittleEndian(), 0);
David Blaikie9c550ac2014-03-25 01:44:02 +0000650 LocDWO.reset(new DWARFDebugLocDWO());
651 LocDWO->parse(LocData);
652 return LocDWO.get();
653}
654
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000655const DWARFDebugAranges *DWARFContext::getDebugAranges() {
656 if (Aranges)
657 return Aranges.get();
658
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000659 Aranges.reset(new DWARFDebugAranges());
Alexey Samsonova1694c12012-11-16 08:36:25 +0000660 Aranges->generate(this);
Benjamin Kramera6002fd2011-09-14 01:09:52 +0000661 return Aranges.get();
662}
663
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000664const DWARFDebugFrame *DWARFContext::getDebugFrame() {
665 if (DebugFrame)
666 return DebugFrame.get();
667
668 // There's a "bug" in the DWARFv3 standard with respect to the target address
669 // size within debug frame sections. While DWARF is supposed to be independent
670 // of its container, FDEs have fields with size being "target address size",
671 // which isn't specified in DWARF in general. It's only specified for CUs, but
672 // .eh_frame can appear without a .debug_info section. Follow the example of
673 // other tools (libdwarf) and extract this from the container (ObjectFile
674 // provides this information). This problem is fixed in DWARFv4
675 // See this dwarf-discuss discussion for more details:
676 // http://lists.dwarfstd.org/htdig.cgi/dwarf-discuss-dwarfstd.org/2011-December/001173.html
Rafael Espindolac398e672017-07-19 22:27:28 +0000677 DataExtractor debugFrameData(DObj->getDebugFrameSection(), isLittleEndian(),
678 DObj->getAddressSize());
Igor Laevsky03a670c2016-01-26 15:09:42 +0000679 DebugFrame.reset(new DWARFDebugFrame(false /* IsEH */));
680 DebugFrame->parse(debugFrameData);
681 return DebugFrame.get();
682}
683
684const DWARFDebugFrame *DWARFContext::getEHFrame() {
685 if (EHFrame)
686 return EHFrame.get();
687
Rafael Espindolac398e672017-07-19 22:27:28 +0000688 DataExtractor debugFrameData(DObj->getEHFrameSection(), isLittleEndian(),
689 DObj->getAddressSize());
Igor Laevsky03a670c2016-01-26 15:09:42 +0000690 DebugFrame.reset(new DWARFDebugFrame(true /* IsEH */));
Eli Benderskyfd08bc12013-02-05 23:30:58 +0000691 DebugFrame->parse(debugFrameData);
692 return DebugFrame.get();
693}
694
Amjad Aboude59cc3e2015-11-12 09:38:54 +0000695const DWARFDebugMacro *DWARFContext::getDebugMacro() {
696 if (Macro)
697 return Macro.get();
698
Rafael Espindolac398e672017-07-19 22:27:28 +0000699 DataExtractor MacinfoData(DObj->getMacinfoSection(), isLittleEndian(), 0);
Amjad Aboude59cc3e2015-11-12 09:38:54 +0000700 Macro.reset(new DWARFDebugMacro());
701 Macro->parse(MacinfoData);
702 return Macro.get();
703}
704
Adrian Prantl99fdb9d2017-09-28 18:10:52 +0000705static DWARFAcceleratorTable &
706getAccelTable(std::unique_ptr<DWARFAcceleratorTable> &Cache,
707 const DWARFObject &Obj, const DWARFSection &Section,
708 StringRef StringSection, bool IsLittleEndian) {
709 if (Cache)
710 return *Cache;
711 DWARFDataExtractor AccelSection(Obj, Section, IsLittleEndian, 0);
712 DataExtractor StrData(StringSection, IsLittleEndian, 0);
713 Cache.reset(new DWARFAcceleratorTable(AccelSection, StrData));
Jonas Devlieghereba915892017-12-11 18:22:47 +0000714 if (Error E = Cache->extract())
715 llvm::consumeError(std::move(E));
Adrian Prantl99fdb9d2017-09-28 18:10:52 +0000716 return *Cache;
717}
718
719const DWARFAcceleratorTable &DWARFContext::getAppleNames() {
720 return getAccelTable(AppleNames, *DObj, DObj->getAppleNamesSection(),
721 DObj->getStringSection(), isLittleEndian());
722}
723
Adrian Prantl714ee4d2017-09-29 00:33:22 +0000724const DWARFAcceleratorTable &DWARFContext::getAppleTypes() {
725 return getAccelTable(AppleTypes, *DObj, DObj->getAppleTypesSection(),
726 DObj->getStringSection(), isLittleEndian());
727}
728
Adrian Prantlf51e7802017-09-29 00:52:33 +0000729const DWARFAcceleratorTable &DWARFContext::getAppleNamespaces() {
730 return getAccelTable(AppleNamespaces, *DObj,
731 DObj->getAppleNamespacesSection(),
732 DObj->getStringSection(), isLittleEndian());
733}
734
735const DWARFAcceleratorTable &DWARFContext::getAppleObjC() {
736 return getAccelTable(AppleObjC, *DObj, DObj->getAppleObjCSection(),
737 DObj->getStringSection(), isLittleEndian());
738}
739
Eric Christopher494109b2012-10-16 23:46:25 +0000740const DWARFLineTable *
Alexey Samsonov7a18c062015-05-19 21:54:32 +0000741DWARFContext::getLineTableForUnit(DWARFUnit *U) {
Benjamin Kramer679e1752011-09-15 20:43:18 +0000742 if (!Line)
Paul Robinson17536b92017-06-29 16:52:08 +0000743 Line.reset(new DWARFDebugLine);
David Blaikiec4e2bed2015-11-17 21:08:05 +0000744
Greg Claytonc8c10322016-12-13 18:25:19 +0000745 auto UnitDIE = U->getUnitDIE();
746 if (!UnitDIE)
Alexey Samsonov7a18c062015-05-19 21:54:32 +0000747 return nullptr;
David Blaikiec4e2bed2015-11-17 21:08:05 +0000748
Greg Clayton97d22182017-01-13 21:08:18 +0000749 auto Offset = toSectionOffset(UnitDIE.find(DW_AT_stmt_list));
Greg Clayton52fe1f62016-12-14 22:38:08 +0000750 if (!Offset)
Craig Topper2617dcc2014-04-15 06:32:26 +0000751 return nullptr; // No line table for this compile unit.
Benjamin Kramer5acab502011-09-15 02:12:05 +0000752
Greg Clayton52fe1f62016-12-14 22:38:08 +0000753 uint32_t stmtOffset = *Offset + U->getLineTableOffset();
Benjamin Kramer679e1752011-09-15 20:43:18 +0000754 // See if the line table is cached.
Eric Christopher494109b2012-10-16 23:46:25 +0000755 if (const DWARFLineTable *lt = Line->getLineTable(stmtOffset))
Benjamin Kramer679e1752011-09-15 20:43:18 +0000756 return lt;
757
Greg Clayton48ff66a2017-05-04 18:29:44 +0000758 // Make sure the offset is good before we try to parse.
Paul Robinson17536b92017-06-29 16:52:08 +0000759 if (stmtOffset >= U->getLineSection().Data.size())
Jonas Devlieghere27476ce2017-09-13 09:43:05 +0000760 return nullptr;
Greg Clayton48ff66a2017-05-04 18:29:44 +0000761
Benjamin Kramer679e1752011-09-15 20:43:18 +0000762 // We have to parse it first.
Rafael Espindolac398e672017-07-19 22:27:28 +0000763 DWARFDataExtractor lineData(*DObj, U->getLineSection(), isLittleEndian(),
Paul Robinson17536b92017-06-29 16:52:08 +0000764 U->getAddressByteSize());
Paul Robinsone5400f82017-11-07 19:57:12 +0000765 return Line->getOrParseLineTable(lineData, stmtOffset, U);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000766}
767
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000768void DWARFContext::parseCompileUnits() {
Rafael Espindolac398e672017-07-19 22:27:28 +0000769 CUs.parse(*this, DObj->getInfoSection());
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000770}
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000771
David Blaikie03c089c2013-09-23 22:44:47 +0000772void DWARFContext::parseTypeUnits() {
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000773 if (!TUs.empty())
774 return;
Rafael Espindolac398e672017-07-19 22:27:28 +0000775 DObj->forEachTypesSections([&](const DWARFSection &S) {
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000776 TUs.emplace_back();
Rafael Espindola5e5dfa12017-07-12 21:08:24 +0000777 TUs.back().parse(*this, S);
778 });
David Blaikie03c089c2013-09-23 22:44:47 +0000779}
780
Eric Christopherda4b2192013-01-02 23:52:13 +0000781void DWARFContext::parseDWOCompileUnits() {
Rafael Espindolac398e672017-07-19 22:27:28 +0000782 DWOCUs.parseDWO(*this, DObj->getInfoDWOSection());
Eric Christopherda4b2192013-01-02 23:52:13 +0000783}
784
David Blaikie92d9d622014-01-09 05:08:24 +0000785void DWARFContext::parseDWOTypeUnits() {
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000786 if (!DWOTUs.empty())
787 return;
Rafael Espindolac398e672017-07-19 22:27:28 +0000788 DObj->forEachTypesDWOSections([&](const DWARFSection &S) {
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000789 DWOTUs.emplace_back();
Rafael Espindola5e5dfa12017-07-12 21:08:24 +0000790 DWOTUs.back().parseDWO(*this, S);
791 });
David Blaikie92d9d622014-01-09 05:08:24 +0000792}
793
Alexey Samsonov45be7932012-08-30 07:49:50 +0000794DWARFCompileUnit *DWARFContext::getCompileUnitForOffset(uint32_t Offset) {
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000795 parseCompileUnits();
Frederic Riss4e126a02014-09-15 07:50:27 +0000796 return CUs.getUnitForOffset(Offset);
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000797}
798
Alexey Samsonov45be7932012-08-30 07:49:50 +0000799DWARFCompileUnit *DWARFContext::getCompileUnitForAddress(uint64_t Address) {
Benjamin Kramer112ec172011-09-15 21:59:13 +0000800 // First, get the offset of the compile unit.
Alexey Samsonov45be7932012-08-30 07:49:50 +0000801 uint32_t CUOffset = getDebugAranges()->findAddress(Address);
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000802 // Retrieve the compile unit.
Alexey Samsonov45be7932012-08-30 07:49:50 +0000803 return getCompileUnitForOffset(CUOffset);
804}
805
Jonas Devliegheref63ee642017-10-25 21:56:41 +0000806DWARFContext::DIEsForAddress DWARFContext::getDIEsForAddress(uint64_t Address) {
807 DIEsForAddress Result;
808
809 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
810 if (!CU)
811 return Result;
812
813 Result.CompileUnit = CU;
814 Result.FunctionDIE = CU->getSubroutineForAddress(Address);
815
816 std::vector<DWARFDie> Worklist;
817 Worklist.push_back(Result.FunctionDIE);
818 while (!Worklist.empty()) {
819 DWARFDie DIE = Worklist.back();
820 Worklist.pop_back();
821
822 if (DIE.getTag() == DW_TAG_lexical_block &&
823 DIE.addressRangeContainsAddress(Address)) {
824 Result.BlockDIE = DIE;
825 break;
826 }
827
828 for (auto Child : DIE)
829 Worklist.push_back(Child);
830 }
831
832 return Result;
833}
834
David Blaikieefc4eba2017-02-06 20:19:02 +0000835static bool getFunctionNameAndStartLineForAddress(DWARFCompileUnit *CU,
836 uint64_t Address,
837 FunctionNameKind Kind,
838 std::string &FunctionName,
839 uint32_t &StartLine) {
Alexey Samsonovd0109992014-04-18 21:36:39 +0000840 // The address may correspond to instruction in some inlined function,
841 // so we have to build the chain of inlined functions and take the
David Blaikieefc4eba2017-02-06 20:19:02 +0000842 // name of the topmost function in it.
Greg Claytonc8c10322016-12-13 18:25:19 +0000843 SmallVector<DWARFDie, 4> InlinedChain;
844 CU->getInlinedChainForAddress(Address, InlinedChain);
David Blaikieefc4eba2017-02-06 20:19:02 +0000845 if (InlinedChain.empty())
Alexey Samsonovd0109992014-04-18 21:36:39 +0000846 return false;
David Blaikieefc4eba2017-02-06 20:19:02 +0000847
848 const DWARFDie &DIE = InlinedChain[0];
849 bool FoundResult = false;
850 const char *Name = nullptr;
851 if (Kind != FunctionNameKind::None && (Name = DIE.getSubroutineName(Kind))) {
Alexey Samsonovd0109992014-04-18 21:36:39 +0000852 FunctionName = Name;
David Blaikieefc4eba2017-02-06 20:19:02 +0000853 FoundResult = true;
Alexey Samsonovd0109992014-04-18 21:36:39 +0000854 }
David Blaikieefc4eba2017-02-06 20:19:02 +0000855 if (auto DeclLineResult = DIE.getDeclLine()) {
856 StartLine = DeclLineResult;
857 FoundResult = true;
858 }
859
860 return FoundResult;
Alexey Samsonovd0109992014-04-18 21:36:39 +0000861}
862
Alexey Samsonov45be7932012-08-30 07:49:50 +0000863DILineInfo DWARFContext::getLineInfoForAddress(uint64_t Address,
Alexey Samsonovdce67342014-05-15 21:24:32 +0000864 DILineInfoSpecifier Spec) {
Alexey Samsonovd0109992014-04-18 21:36:39 +0000865 DILineInfo Result;
866
Alexey Samsonov45be7932012-08-30 07:49:50 +0000867 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
868 if (!CU)
Alexey Samsonovd0109992014-04-18 21:36:39 +0000869 return Result;
David Blaikieefc4eba2017-02-06 20:19:02 +0000870 getFunctionNameAndStartLineForAddress(CU, Address, Spec.FNKind,
871 Result.FunctionName,
872 Result.StartLine);
Alexey Samsonovdce67342014-05-15 21:24:32 +0000873 if (Spec.FLIKind != FileLineInfoKind::None) {
Frederic Riss101b5e22014-09-19 15:11:51 +0000874 if (const DWARFLineTable *LineTable = getLineTableForUnit(CU))
875 LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(),
876 Spec.FLIKind, Result);
Alexey Samsonovf4462fa2012-07-02 05:54:45 +0000877 }
Alexey Samsonovd0109992014-04-18 21:36:39 +0000878 return Result;
Benjamin Kramer2602ca62011-09-15 20:43:22 +0000879}
David Blaikiea379b1812011-12-20 02:50:00 +0000880
Alexey Samsonovdce67342014-05-15 21:24:32 +0000881DILineInfoTable
882DWARFContext::getLineInfoForAddressRange(uint64_t Address, uint64_t Size,
883 DILineInfoSpecifier Spec) {
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000884 DILineInfoTable Lines;
885 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
886 if (!CU)
887 return Lines;
888
889 std::string FunctionName = "<invalid>";
David Blaikieefc4eba2017-02-06 20:19:02 +0000890 uint32_t StartLine = 0;
891 getFunctionNameAndStartLineForAddress(CU, Address, Spec.FNKind, FunctionName,
892 StartLine);
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000893
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000894 // If the Specifier says we don't need FileLineInfo, just
895 // return the top-most function at the starting address.
Alexey Samsonovdce67342014-05-15 21:24:32 +0000896 if (Spec.FLIKind == FileLineInfoKind::None) {
Alexey Samsonovd0109992014-04-18 21:36:39 +0000897 DILineInfo Result;
898 Result.FunctionName = FunctionName;
David Blaikieefc4eba2017-02-06 20:19:02 +0000899 Result.StartLine = StartLine;
Alexey Samsonovd0109992014-04-18 21:36:39 +0000900 Lines.push_back(std::make_pair(Address, Result));
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000901 return Lines;
902 }
903
Frederic Rissec8a5ba2014-09-04 06:14:40 +0000904 const DWARFLineTable *LineTable = getLineTableForUnit(CU);
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000905
906 // Get the index of row we're looking for in the line table.
907 std::vector<uint32_t> RowVector;
908 if (!LineTable->lookupAddressRange(Address, Size, RowVector))
909 return Lines;
910
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000911 for (uint32_t RowIndex : RowVector) {
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000912 // Take file number and line/column from the row.
913 const DWARFDebugLine::Row &Row = LineTable->Rows[RowIndex];
Alexey Samsonovd0109992014-04-18 21:36:39 +0000914 DILineInfo Result;
Frederic Riss101b5e22014-09-19 15:11:51 +0000915 LineTable->getFileNameByIndex(Row.File, CU->getCompilationDir(),
916 Spec.FLIKind, Result.FileName);
Alexey Samsonovd0109992014-04-18 21:36:39 +0000917 Result.FunctionName = FunctionName;
918 Result.Line = Row.Line;
919 Result.Column = Row.Column;
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(Row.Address, Result));
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000922 }
923
924 return Lines;
925}
926
Alexey Samsonovdce67342014-05-15 21:24:32 +0000927DIInliningInfo
928DWARFContext::getInliningInfoForAddress(uint64_t Address,
929 DILineInfoSpecifier Spec) {
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000930 DIInliningInfo InliningInfo;
931
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000932 DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
933 if (!CU)
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000934 return InliningInfo;
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000935
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000936 const DWARFLineTable *LineTable = nullptr;
Greg Claytonc8c10322016-12-13 18:25:19 +0000937 SmallVector<DWARFDie, 4> InlinedChain;
938 CU->getInlinedChainForAddress(Address, InlinedChain);
939 if (InlinedChain.size() == 0) {
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000940 // If there is no DIE for address (e.g. it is in unavailable .dwo file),
941 // try to at least get file/line info from symbol table.
Alexey Samsonovdce67342014-05-15 21:24:32 +0000942 if (Spec.FLIKind != FileLineInfoKind::None) {
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000943 DILineInfo Frame;
Frederic Rissec8a5ba2014-09-04 06:14:40 +0000944 LineTable = getLineTableForUnit(CU);
Frederic Riss101b5e22014-09-19 15:11:51 +0000945 if (LineTable &&
946 LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(),
947 Spec.FLIKind, Frame))
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000948 InliningInfo.addFrame(Frame);
Alexey Samsonov5c39fdf2014-04-18 22:22:44 +0000949 }
950 return InliningInfo;
951 }
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000952
Dehao Chenef700d52017-04-17 20:10:39 +0000953 uint32_t CallFile = 0, CallLine = 0, CallColumn = 0, CallDiscriminator = 0;
Greg Claytonc8c10322016-12-13 18:25:19 +0000954 for (uint32_t i = 0, n = InlinedChain.size(); i != n; i++) {
955 DWARFDie &FunctionDIE = InlinedChain[i];
Alexey Samsonovd0109992014-04-18 21:36:39 +0000956 DILineInfo Frame;
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000957 // Get function name if necessary.
Greg Claytonc8c10322016-12-13 18:25:19 +0000958 if (const char *Name = FunctionDIE.getSubroutineName(Spec.FNKind))
Alexey Samsonovdce67342014-05-15 21:24:32 +0000959 Frame.FunctionName = Name;
David Blaikieefc4eba2017-02-06 20:19:02 +0000960 if (auto DeclLineResult = FunctionDIE.getDeclLine())
961 Frame.StartLine = DeclLineResult;
Alexey Samsonovdce67342014-05-15 21:24:32 +0000962 if (Spec.FLIKind != FileLineInfoKind::None) {
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000963 if (i == 0) {
964 // For the topmost frame, initialize the line table of this
965 // compile unit and fetch file/line info from it.
Frederic Rissec8a5ba2014-09-04 06:14:40 +0000966 LineTable = getLineTableForUnit(CU);
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000967 // For the topmost routine, get file/line info from line table.
Frederic Riss101b5e22014-09-19 15:11:51 +0000968 if (LineTable)
969 LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(),
970 Spec.FLIKind, Frame);
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000971 } else {
972 // Otherwise, use call file, call line and call column from
973 // previous DIE in inlined chain.
Frederic Riss101b5e22014-09-19 15:11:51 +0000974 if (LineTable)
975 LineTable->getFileNameByIndex(CallFile, CU->getCompilationDir(),
976 Spec.FLIKind, Frame.FileName);
Alexey Samsonovd0109992014-04-18 21:36:39 +0000977 Frame.Line = CallLine;
978 Frame.Column = CallColumn;
Dehao Chenef700d52017-04-17 20:10:39 +0000979 Frame.Discriminator = CallDiscriminator;
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000980 }
981 // Get call file/line/column of a current DIE.
982 if (i + 1 < n) {
Dehao Chenef700d52017-04-17 20:10:39 +0000983 FunctionDIE.getCallerFrame(CallFile, CallLine, CallColumn,
984 CallDiscriminator);
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000985 }
986 }
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000987 InliningInfo.addFrame(Frame);
988 }
989 return InliningInfo;
990}
991
David Blaikief9803fb2017-05-23 00:30:42 +0000992std::shared_ptr<DWARFContext>
993DWARFContext::getDWOContext(StringRef AbsolutePath) {
David Blaikie15d85fc2017-05-23 06:48:53 +0000994 if (auto S = DWP.lock()) {
David Blaikief9803fb2017-05-23 00:30:42 +0000995 DWARFContext *Ctxt = S->Context.get();
996 return std::shared_ptr<DWARFContext>(std::move(S), Ctxt);
997 }
998
David Blaikie15d85fc2017-05-23 06:48:53 +0000999 std::weak_ptr<DWOFile> *Entry = &DWOFiles[AbsolutePath];
1000
1001 if (auto S = Entry->lock()) {
1002 DWARFContext *Ctxt = S->Context.get();
1003 return std::shared_ptr<DWARFContext>(std::move(S), Ctxt);
1004 }
1005
David Blaikie15d85fc2017-05-23 06:48:53 +00001006 Expected<OwningBinary<ObjectFile>> Obj = [&] {
1007 if (!CheckedForDWP) {
David Blaikiee5adb682017-07-30 01:34:08 +00001008 SmallString<128> DWPName;
1009 auto Obj = object::ObjectFile::createObjectFile(
1010 this->DWPName.empty()
1011 ? (DObj->getFileName() + ".dwp").toStringRef(DWPName)
1012 : StringRef(this->DWPName));
David Blaikie15d85fc2017-05-23 06:48:53 +00001013 if (Obj) {
1014 Entry = &DWP;
1015 return Obj;
1016 } else {
1017 CheckedForDWP = true;
1018 // TODO: Should this error be handled (maybe in a high verbosity mode)
1019 // before falling back to .dwo files?
1020 consumeError(Obj.takeError());
1021 }
1022 }
1023
1024 return object::ObjectFile::createObjectFile(AbsolutePath);
1025 }();
1026
David Blaikief9803fb2017-05-23 00:30:42 +00001027 if (!Obj) {
1028 // TODO: Actually report errors helpfully.
1029 consumeError(Obj.takeError());
1030 return nullptr;
1031 }
David Blaikie15d85fc2017-05-23 06:48:53 +00001032
1033 auto S = std::make_shared<DWOFile>();
David Blaikief9803fb2017-05-23 00:30:42 +00001034 S->File = std::move(Obj.get());
Rafael Espindolac398e672017-07-19 22:27:28 +00001035 S->Context = DWARFContext::create(*S->File.getBinary());
David Blaikie15d85fc2017-05-23 06:48:53 +00001036 *Entry = S;
David Blaikief9803fb2017-05-23 00:30:42 +00001037 auto *Ctxt = S->Context.get();
1038 return std::shared_ptr<DWARFContext>(std::move(S), Ctxt);
1039}
1040
George Rimar702dac62017-04-12 08:59:15 +00001041static Error createError(const Twine &Reason, llvm::Error E) {
1042 return make_error<StringError>(Reason + toString(std::move(E)),
1043 inconvertibleErrorCode());
1044}
1045
George Rimara25d3292017-05-27 18:10:23 +00001046/// SymInfo contains information about symbol: it's address
1047/// and section index which is -1LL for absolute symbols.
1048struct SymInfo {
1049 uint64_t Address;
1050 uint64_t SectionIndex;
1051};
1052
1053/// Returns the address of symbol relocation used against and a section index.
1054/// Used for futher relocations computation. Symbol's section load address is
1055static Expected<SymInfo> getSymbolInfo(const object::ObjectFile &Obj,
1056 const RelocationRef &Reloc,
1057 const LoadedObjectInfo *L,
1058 std::map<SymbolRef, SymInfo> &Cache) {
1059 SymInfo Ret = {0, (uint64_t)-1LL};
George Rimar702dac62017-04-12 08:59:15 +00001060 object::section_iterator RSec = Obj.section_end();
1061 object::symbol_iterator Sym = Reloc.getSymbol();
1062
George Rimara25d3292017-05-27 18:10:23 +00001063 std::map<SymbolRef, SymInfo>::iterator CacheIt = Cache.end();
George Rimar702dac62017-04-12 08:59:15 +00001064 // First calculate the address of the symbol or section as it appears
1065 // in the object file
1066 if (Sym != Obj.symbol_end()) {
George Rimar958b01a2017-05-15 11:45:28 +00001067 bool New;
George Rimara25d3292017-05-27 18:10:23 +00001068 std::tie(CacheIt, New) = Cache.insert({*Sym, {0, 0}});
George Rimar958b01a2017-05-15 11:45:28 +00001069 if (!New)
1070 return CacheIt->second;
1071
George Rimar702dac62017-04-12 08:59:15 +00001072 Expected<uint64_t> SymAddrOrErr = Sym->getAddress();
1073 if (!SymAddrOrErr)
George Rimar1af3cb22017-06-28 08:21:19 +00001074 return createError("failed to compute symbol address: ",
George Rimar702dac62017-04-12 08:59:15 +00001075 SymAddrOrErr.takeError());
1076
1077 // Also remember what section this symbol is in for later
1078 auto SectOrErr = Sym->getSection();
1079 if (!SectOrErr)
George Rimar1af3cb22017-06-28 08:21:19 +00001080 return createError("failed to get symbol section: ",
George Rimar702dac62017-04-12 08:59:15 +00001081 SectOrErr.takeError());
1082
1083 RSec = *SectOrErr;
George Rimara25d3292017-05-27 18:10:23 +00001084 Ret.Address = *SymAddrOrErr;
George Rimar702dac62017-04-12 08:59:15 +00001085 } else if (auto *MObj = dyn_cast<MachOObjectFile>(&Obj)) {
1086 RSec = MObj->getRelocationSection(Reloc.getRawDataRefImpl());
George Rimara25d3292017-05-27 18:10:23 +00001087 Ret.Address = RSec->getAddress();
George Rimar702dac62017-04-12 08:59:15 +00001088 }
1089
George Rimara25d3292017-05-27 18:10:23 +00001090 if (RSec != Obj.section_end())
1091 Ret.SectionIndex = RSec->getIndex();
1092
George Rimar702dac62017-04-12 08:59:15 +00001093 // If we are given load addresses for the sections, we need to adjust:
1094 // SymAddr = (Address of Symbol Or Section in File) -
1095 // (Address of Section in File) +
1096 // (Load Address of Section)
1097 // RSec is now either the section being targeted or the section
1098 // containing the symbol being targeted. In either case,
1099 // we need to perform the same computation.
1100 if (L && RSec != Obj.section_end())
1101 if (uint64_t SectionLoadAddress = L->getSectionLoadAddress(*RSec))
George Rimara25d3292017-05-27 18:10:23 +00001102 Ret.Address += SectionLoadAddress - RSec->getAddress();
George Rimar958b01a2017-05-15 11:45:28 +00001103
1104 if (CacheIt != Cache.end())
1105 CacheIt->second = Ret;
1106
George Rimar702dac62017-04-12 08:59:15 +00001107 return Ret;
1108}
1109
1110static bool isRelocScattered(const object::ObjectFile &Obj,
1111 const RelocationRef &Reloc) {
George Rimard4998b02017-04-13 09:52:50 +00001112 const MachOObjectFile *MachObj = dyn_cast<MachOObjectFile>(&Obj);
1113 if (!MachObj)
George Rimar702dac62017-04-12 08:59:15 +00001114 return false;
1115 // MachO also has relocations that point to sections and
1116 // scattered relocations.
George Rimar702dac62017-04-12 08:59:15 +00001117 auto RelocInfo = MachObj->getRelocation(Reloc.getRawDataRefImpl());
1118 return MachObj->isRelocationScattered(RelocInfo);
1119}
1120
Rafael Espindolac398e672017-07-19 22:27:28 +00001121ErrorPolicy DWARFContext::defaultErrorHandler(Error E) {
George Rimar1af3cb22017-06-28 08:21:19 +00001122 errs() << "error: " + toString(std::move(E)) << '\n';
1123 return ErrorPolicy::Continue;
1124}
1125
Rafael Espindola87c3f4a92017-07-24 19:34:26 +00001126namespace {
1127struct DWARFSectionMap final : public DWARFSection {
1128 RelocAddrMap Relocs;
1129};
Rafael Espindola87c3f4a92017-07-24 19:34:26 +00001130
Rafael Espindolac398e672017-07-19 22:27:28 +00001131class DWARFObjInMemory final : public DWARFObject {
1132 bool IsLittleEndian;
1133 uint8_t AddressSize;
1134 StringRef FileName;
George Rimar6957ab52017-08-15 12:32:54 +00001135 const object::ObjectFile *Obj = nullptr;
George Rimare1c30f72017-08-15 15:54:43 +00001136 std::vector<SectionName> SectionNames;
Keno Fischerc780e8e2015-05-21 21:24:32 +00001137
Rafael Espindolac398e672017-07-19 22:27:28 +00001138 using TypeSectionMap = MapVector<object::SectionRef, DWARFSectionMap,
1139 std::map<object::SectionRef, unsigned>>;
Eric Christopher7370b552012-11-12 21:40:38 +00001140
Rafael Espindolac398e672017-07-19 22:27:28 +00001141 TypeSectionMap TypesSections;
1142 TypeSectionMap TypesDWOSections;
1143
1144 DWARFSectionMap InfoSection;
1145 DWARFSectionMap LocSection;
1146 DWARFSectionMap LineSection;
1147 DWARFSectionMap RangeSection;
1148 DWARFSectionMap StringOffsetSection;
1149 DWARFSectionMap InfoDWOSection;
1150 DWARFSectionMap LineDWOSection;
1151 DWARFSectionMap LocDWOSection;
1152 DWARFSectionMap StringOffsetDWOSection;
1153 DWARFSectionMap RangeDWOSection;
1154 DWARFSectionMap AddrSection;
1155 DWARFSectionMap AppleNamesSection;
1156 DWARFSectionMap AppleTypesSection;
1157 DWARFSectionMap AppleNamespacesSection;
1158 DWARFSectionMap AppleObjCSection;
1159
1160 DWARFSectionMap *mapNameToDWARFSection(StringRef Name) {
1161 return StringSwitch<DWARFSectionMap *>(Name)
1162 .Case("debug_info", &InfoSection)
1163 .Case("debug_loc", &LocSection)
1164 .Case("debug_line", &LineSection)
1165 .Case("debug_str_offsets", &StringOffsetSection)
1166 .Case("debug_ranges", &RangeSection)
1167 .Case("debug_info.dwo", &InfoDWOSection)
1168 .Case("debug_loc.dwo", &LocDWOSection)
1169 .Case("debug_line.dwo", &LineDWOSection)
1170 .Case("debug_str_offsets.dwo", &StringOffsetDWOSection)
1171 .Case("debug_addr", &AddrSection)
1172 .Case("apple_names", &AppleNamesSection)
1173 .Case("apple_types", &AppleTypesSection)
1174 .Case("apple_namespaces", &AppleNamespacesSection)
1175 .Case("apple_namespac", &AppleNamespacesSection)
1176 .Case("apple_objc", &AppleObjCSection)
1177 .Default(nullptr);
1178 }
1179
1180 StringRef AbbrevSection;
1181 StringRef ARangeSection;
1182 StringRef DebugFrameSection;
1183 StringRef EHFrameSection;
1184 StringRef StringSection;
1185 StringRef MacinfoSection;
1186 StringRef PubNamesSection;
1187 StringRef PubTypesSection;
1188 StringRef GnuPubNamesSection;
1189 StringRef AbbrevDWOSection;
1190 StringRef StringDWOSection;
1191 StringRef GnuPubTypesSection;
1192 StringRef CUIndexSection;
1193 StringRef GdbIndexSection;
1194 StringRef TUIndexSection;
1195
1196 SmallVector<SmallString<32>, 4> UncompressedSections;
1197
1198 StringRef *mapSectionToMember(StringRef Name) {
1199 if (DWARFSection *Sec = mapNameToDWARFSection(Name))
1200 return &Sec->Data;
1201 return StringSwitch<StringRef *>(Name)
1202 .Case("debug_abbrev", &AbbrevSection)
1203 .Case("debug_aranges", &ARangeSection)
1204 .Case("debug_frame", &DebugFrameSection)
1205 .Case("eh_frame", &EHFrameSection)
1206 .Case("debug_str", &StringSection)
1207 .Case("debug_macinfo", &MacinfoSection)
1208 .Case("debug_pubnames", &PubNamesSection)
1209 .Case("debug_pubtypes", &PubTypesSection)
1210 .Case("debug_gnu_pubnames", &GnuPubNamesSection)
1211 .Case("debug_gnu_pubtypes", &GnuPubTypesSection)
1212 .Case("debug_abbrev.dwo", &AbbrevDWOSection)
1213 .Case("debug_str.dwo", &StringDWOSection)
1214 .Case("debug_cu_index", &CUIndexSection)
1215 .Case("debug_tu_index", &TUIndexSection)
1216 .Case("gdb_index", &GdbIndexSection)
1217 // Any more debug info sections go here.
1218 .Default(nullptr);
1219 }
1220
1221 /// If Sec is compressed section, decompresses and updates its contents
1222 /// provided by Data. Otherwise leaves it unchanged.
1223 Error maybeDecompress(const object::SectionRef &Sec, StringRef Name,
1224 StringRef &Data) {
1225 if (!Decompressor::isCompressed(Sec))
1226 return Error::success();
1227
1228 Expected<Decompressor> Decompressor =
1229 Decompressor::create(Name, Data, IsLittleEndian, AddressSize == 8);
1230 if (!Decompressor)
1231 return Decompressor.takeError();
1232
1233 SmallString<32> Out;
1234 if (auto Err = Decompressor->resizeAndDecompress(Out))
1235 return Err;
1236
1237 UncompressedSections.emplace_back(std::move(Out));
1238 Data = UncompressedSections.back();
1239
1240 return Error::success();
1241 }
1242
1243public:
1244 DWARFObjInMemory(const StringMap<std::unique_ptr<MemoryBuffer>> &Sections,
1245 uint8_t AddrSize, bool IsLittleEndian)
1246 : IsLittleEndian(IsLittleEndian) {
1247 for (const auto &SecIt : Sections) {
1248 if (StringRef *SectionData = mapSectionToMember(SecIt.first()))
1249 *SectionData = SecIt.second->getBuffer();
Alexey Samsonov068fc8a2013-04-23 10:17:34 +00001250 }
Rafael Espindolac398e672017-07-19 22:27:28 +00001251 }
1252 DWARFObjInMemory(const object::ObjectFile &Obj, const LoadedObjectInfo *L,
1253 function_ref<ErrorPolicy(Error)> HandleError)
1254 : IsLittleEndian(Obj.isLittleEndian()),
George Rimar6957ab52017-08-15 12:32:54 +00001255 AddressSize(Obj.getBytesInAddress()), FileName(Obj.getFileName()),
1256 Obj(&Obj) {
Alexey Samsonov068fc8a2013-04-23 10:17:34 +00001257
George Rimare1c30f72017-08-15 15:54:43 +00001258 StringMap<unsigned> SectionAmountMap;
Rafael Espindolac398e672017-07-19 22:27:28 +00001259 for (const SectionRef &Section : Obj.sections()) {
1260 StringRef Name;
1261 Section.getName(Name);
George Rimare1c30f72017-08-15 15:54:43 +00001262 ++SectionAmountMap[Name];
1263 SectionNames.push_back({ Name, true });
1264
Rafael Espindolac398e672017-07-19 22:27:28 +00001265 // Skip BSS and Virtual sections, they aren't interesting.
1266 if (Section.isBSS() || Section.isVirtual())
George Rimarfed9f092017-05-17 12:10:51 +00001267 continue;
Keno Fischerc780e8e2015-05-21 21:24:32 +00001268
Jonas Devlieghere8af23872017-09-26 14:22:35 +00001269 // Skip sections stripped by dsymutil.
1270 if (Section.isStripped())
1271 continue;
1272
Rafael Espindolac398e672017-07-19 22:27:28 +00001273 StringRef Data;
1274 section_iterator RelocatedSection = Section.getRelocatedSection();
1275 // Try to obtain an already relocated version of this section.
1276 // Else use the unrelocated section from the object file. We'll have to
1277 // apply relocations ourselves later.
1278 if (!L || !L->getLoadedSectionContents(*RelocatedSection, Data))
1279 Section.getContents(Data);
George Rimarfed9f092017-05-17 12:10:51 +00001280
Rafael Espindolac398e672017-07-19 22:27:28 +00001281 if (auto Err = maybeDecompress(Section, Name, Data)) {
1282 ErrorPolicy EP = HandleError(createError(
1283 "failed to decompress '" + Name + "', ", std::move(Err)));
George Rimar1af3cb22017-06-28 08:21:19 +00001284 if (EP == ErrorPolicy::Halt)
1285 return;
George Rimarfed9f092017-05-17 12:10:51 +00001286 continue;
1287 }
Rafael Espindolac398e672017-07-19 22:27:28 +00001288
1289 // Compressed sections names in GNU style starts from ".z",
1290 // at this point section is decompressed and we drop compression prefix.
1291 Name = Name.substr(
1292 Name.find_first_not_of("._z")); // Skip ".", "z" and "_" prefixes.
1293
1294 // Map platform specific debug section names to DWARF standard section
1295 // names.
1296 Name = Obj.mapDebugSectionName(Name);
1297
1298 if (StringRef *SectionData = mapSectionToMember(Name)) {
1299 *SectionData = Data;
1300 if (Name == "debug_ranges") {
1301 // FIXME: Use the other dwo range section when we emit it.
1302 RangeDWOSection.Data = Data;
1303 }
1304 } else if (Name == "debug_types") {
1305 // Find debug_types data by section rather than name as there are
1306 // multiple, comdat grouped, debug_types sections.
1307 TypesSections[Section].Data = Data;
1308 } else if (Name == "debug_types.dwo") {
1309 TypesDWOSections[Section].Data = Data;
1310 }
1311
1312 if (RelocatedSection == Obj.section_end())
1313 continue;
1314
1315 StringRef RelSecName;
1316 StringRef RelSecData;
1317 RelocatedSection->getName(RelSecName);
1318
1319 // If the section we're relocating was relocated already by the JIT,
1320 // then we used the relocated version above, so we do not need to process
1321 // relocations for it now.
1322 if (L && L->getLoadedSectionContents(*RelocatedSection, RelSecData))
1323 continue;
1324
1325 // In Mach-o files, the relocations do not need to be applied if
1326 // there is no load offset to apply. The value read at the
1327 // relocation point already factors in the section address
1328 // (actually applying the relocations will produce wrong results
1329 // as the section address will be added twice).
1330 if (!L && isa<MachOObjectFile>(&Obj))
1331 continue;
1332
1333 RelSecName = RelSecName.substr(
1334 RelSecName.find_first_not_of("._z")); // Skip . and _ prefixes.
1335
1336 // TODO: Add support for relocations in other sections as needed.
1337 // Record relocations for the debug_info and debug_line sections.
1338 DWARFSectionMap *Sec = mapNameToDWARFSection(RelSecName);
1339 RelocAddrMap *Map = Sec ? &Sec->Relocs : nullptr;
1340 if (!Map) {
1341 // Find debug_types relocs by section rather than name as there are
1342 // multiple, comdat grouped, debug_types sections.
1343 if (RelSecName == "debug_types")
1344 Map =
1345 &static_cast<DWARFSectionMap &>(TypesSections[*RelocatedSection])
1346 .Relocs;
1347 else if (RelSecName == "debug_types.dwo")
1348 Map = &static_cast<DWARFSectionMap &>(
1349 TypesDWOSections[*RelocatedSection])
1350 .Relocs;
1351 else
1352 continue;
1353 }
1354
1355 if (Section.relocation_begin() == Section.relocation_end())
1356 continue;
1357
1358 // Symbol to [address, section index] cache mapping.
1359 std::map<SymbolRef, SymInfo> AddrCache;
1360 for (const RelocationRef &Reloc : Section.relocations()) {
1361 // FIXME: it's not clear how to correctly handle scattered
1362 // relocations.
1363 if (isRelocScattered(Obj, Reloc))
1364 continue;
1365
1366 Expected<SymInfo> SymInfoOrErr =
1367 getSymbolInfo(Obj, Reloc, L, AddrCache);
1368 if (!SymInfoOrErr) {
1369 if (HandleError(SymInfoOrErr.takeError()) == ErrorPolicy::Halt)
1370 return;
1371 continue;
1372 }
1373
1374 object::RelocVisitor V(Obj);
1375 uint64_t Val = V.visit(Reloc.getType(), Reloc, SymInfoOrErr->Address);
1376 if (V.error()) {
1377 SmallString<32> Type;
1378 Reloc.getTypeName(Type);
1379 ErrorPolicy EP = HandleError(
1380 createError("failed to compute relocation: " + Type + ", ",
1381 errorCodeToError(object_error::parse_failed)));
1382 if (EP == ErrorPolicy::Halt)
1383 return;
1384 continue;
1385 }
1386 RelocAddrEntry Rel = {SymInfoOrErr->SectionIndex, Val};
1387 Map->insert({Reloc.getOffset(), Rel});
1388 }
Eric Christopher7370b552012-11-12 21:40:38 +00001389 }
George Rimare1c30f72017-08-15 15:54:43 +00001390
1391 for (SectionName &S : SectionNames)
1392 if (SectionAmountMap[S.Name] > 1)
1393 S.IsNameUnique = false;
Eric Christopher7370b552012-11-12 21:40:38 +00001394 }
Eric Christopher7370b552012-11-12 21:40:38 +00001395
Rafael Espindolac398e672017-07-19 22:27:28 +00001396 Optional<RelocAddrEntry> find(const DWARFSection &S,
1397 uint64_t Pos) const override {
1398 auto &Sec = static_cast<const DWARFSectionMap &>(S);
1399 RelocAddrMap::const_iterator AI = Sec.Relocs.find(Pos);
1400 if (AI == Sec.Relocs.end())
1401 return None;
1402 return AI->second;
Chris Bieneman2e752db2017-01-20 19:03:14 +00001403 }
Rafael Espindolac398e672017-07-19 22:27:28 +00001404
George Rimar6957ab52017-08-15 12:32:54 +00001405 const object::ObjectFile *getFile() const override { return Obj; }
1406
George Rimare1c30f72017-08-15 15:54:43 +00001407 ArrayRef<SectionName> getSectionNames() const override {
1408 return SectionNames;
1409 }
1410
Rafael Espindolac398e672017-07-19 22:27:28 +00001411 bool isLittleEndian() const override { return IsLittleEndian; }
1412 StringRef getAbbrevDWOSection() const override { return AbbrevDWOSection; }
1413 const DWARFSection &getLineDWOSection() const override {
1414 return LineDWOSection;
1415 }
1416 const DWARFSection &getLocDWOSection() const override {
1417 return LocDWOSection;
1418 }
1419 StringRef getStringDWOSection() const override { return StringDWOSection; }
1420 const DWARFSection &getStringOffsetDWOSection() const override {
1421 return StringOffsetDWOSection;
1422 }
1423 const DWARFSection &getRangeDWOSection() const override {
1424 return RangeDWOSection;
1425 }
1426 const DWARFSection &getAddrSection() const override { return AddrSection; }
1427 StringRef getCUIndexSection() const override { return CUIndexSection; }
1428 StringRef getGdbIndexSection() const override { return GdbIndexSection; }
1429 StringRef getTUIndexSection() const override { return TUIndexSection; }
1430
1431 // DWARF v5
1432 const DWARFSection &getStringOffsetSection() const override {
1433 return StringOffsetSection;
1434 }
1435
1436 // Sections for DWARF5 split dwarf proposal.
1437 const DWARFSection &getInfoDWOSection() const override {
1438 return InfoDWOSection;
1439 }
1440 void forEachTypesDWOSections(
1441 function_ref<void(const DWARFSection &)> F) const override {
1442 for (auto &P : TypesDWOSections)
1443 F(P.second);
1444 }
1445
1446 StringRef getAbbrevSection() const override { return AbbrevSection; }
1447 const DWARFSection &getLocSection() const override { return LocSection; }
1448 StringRef getARangeSection() const override { return ARangeSection; }
1449 StringRef getDebugFrameSection() const override { return DebugFrameSection; }
1450 StringRef getEHFrameSection() const override { return EHFrameSection; }
1451 const DWARFSection &getLineSection() const override { return LineSection; }
1452 StringRef getStringSection() const override { return StringSection; }
1453 const DWARFSection &getRangeSection() const override { return RangeSection; }
1454 StringRef getMacinfoSection() const override { return MacinfoSection; }
1455 StringRef getPubNamesSection() const override { return PubNamesSection; }
1456 StringRef getPubTypesSection() const override { return PubTypesSection; }
1457 StringRef getGnuPubNamesSection() const override {
1458 return GnuPubNamesSection;
1459 }
1460 StringRef getGnuPubTypesSection() const override {
1461 return GnuPubTypesSection;
1462 }
1463 const DWARFSection &getAppleNamesSection() const override {
1464 return AppleNamesSection;
1465 }
1466 const DWARFSection &getAppleTypesSection() const override {
1467 return AppleTypesSection;
1468 }
1469 const DWARFSection &getAppleNamespacesSection() const override {
1470 return AppleNamespacesSection;
1471 }
1472 const DWARFSection &getAppleObjCSection() const override {
1473 return AppleObjCSection;
1474 }
1475
1476 StringRef getFileName() const override { return FileName; }
1477 uint8_t getAddressSize() const override { return AddressSize; }
1478 const DWARFSection &getInfoSection() const override { return InfoSection; }
1479 void forEachTypesSections(
1480 function_ref<void(const DWARFSection &)> F) const override {
1481 for (auto &P : TypesSections)
1482 F(P.second);
1483 }
1484};
Benjamin Kramer49a49fe2017-08-20 13:03:48 +00001485} // namespace
Rafael Espindolac398e672017-07-19 22:27:28 +00001486
1487std::unique_ptr<DWARFContext>
1488DWARFContext::create(const object::ObjectFile &Obj, const LoadedObjectInfo *L,
David Blaikiee5adb682017-07-30 01:34:08 +00001489 function_ref<ErrorPolicy(Error)> HandleError,
1490 std::string DWPName) {
Reid Kleckner388f8802017-07-19 23:42:53 +00001491 auto DObj = llvm::make_unique<DWARFObjInMemory>(Obj, L, HandleError);
David Blaikiee5adb682017-07-30 01:34:08 +00001492 return llvm::make_unique<DWARFContext>(std::move(DObj), std::move(DWPName));
Chris Bieneman2e752db2017-01-20 19:03:14 +00001493}
1494
Rafael Espindolac398e672017-07-19 22:27:28 +00001495std::unique_ptr<DWARFContext>
1496DWARFContext::create(const StringMap<std::unique_ptr<MemoryBuffer>> &Sections,
1497 uint8_t AddrSize, bool isLittleEndian) {
Reid Kleckner388f8802017-07-19 23:42:53 +00001498 auto DObj =
1499 llvm::make_unique<DWARFObjInMemory>(Sections, AddrSize, isLittleEndian);
David Blaikiee5adb682017-07-30 01:34:08 +00001500 return llvm::make_unique<DWARFContext>(std::move(DObj), "");
Rafael Espindola77e87b32017-07-07 05:36:53 +00001501}
Reid Klecknera0587362017-08-29 21:41:21 +00001502
1503Error DWARFContext::loadRegisterInfo(const object::ObjectFile &Obj) {
1504 // Detect the architecture from the object file. We usually don't need OS
1505 // info to lookup a target and create register info.
1506 Triple TT;
1507 TT.setArch(Triple::ArchType(Obj.getArch()));
1508 TT.setVendor(Triple::UnknownVendor);
1509 TT.setOS(Triple::UnknownOS);
1510 std::string TargetLookupError;
1511 const Target *TheTarget =
1512 TargetRegistry::lookupTarget(TT.str(), TargetLookupError);
1513 if (!TargetLookupError.empty())
1514 return make_error<StringError>(TargetLookupError, inconvertibleErrorCode());
1515 RegInfo.reset(TheTarget->createMCRegInfo(TT.str()));
1516 return Error::success();
1517}