LLVM  7.0.0
DWARFContext.cpp
Go to the documentation of this file.
1 //===- DWARFContext.cpp ---------------------------------------------------===//
2 //
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 
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/ADT/SmallString.h"
13 #include "llvm/ADT/SmallVector.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/ADT/StringSwitch.h"
36 #include "llvm/MC/MCRegisterInfo.h"
38 #include "llvm/Object/MachO.h"
39 #include "llvm/Object/ObjectFile.h"
41 #include "llvm/Support/Casting.h"
43 #include "llvm/Support/Error.h"
44 #include "llvm/Support/Format.h"
46 #include "llvm/Support/Path.h"
48 #include "llvm/Support/WithColor.h"
50 #include <algorithm>
51 #include <cstdint>
52 #include <deque>
53 #include <map>
54 #include <string>
55 #include <utility>
56 #include <vector>
57 
58 using namespace llvm;
59 using namespace dwarf;
60 using namespace object;
61 
62 #define DEBUG_TYPE "dwarf"
63 
67 
68 DWARFContext::DWARFContext(std::unique_ptr<const DWARFObject> DObj,
69  std::string DWPName)
70  : DIContext(CK_DWARF), DWPName(std::move(DWPName)), DObj(std::move(DObj)) {}
71 
72 DWARFContext::~DWARFContext() = default;
73 
74 /// Dump the UUID load command.
75 static void dumpUUID(raw_ostream &OS, const ObjectFile &Obj) {
76  auto *MachO = dyn_cast<MachOObjectFile>(&Obj);
77  if (!MachO)
78  return;
79  for (auto LC : MachO->load_commands()) {
81  if (LC.C.cmd == MachO::LC_UUID) {
82  if (LC.C.cmdsize < sizeof(UUID) + sizeof(LC.C)) {
83  OS << "error: UUID load command is too short.\n";
84  return;
85  }
86  OS << "UUID: ";
87  memcpy(&UUID, LC.Ptr+sizeof(LC.C), sizeof(UUID));
88  OS.write_uuid(UUID);
89  Triple T = MachO->getArchTriple();
90  OS << " (" << T.getArchName() << ')';
91  OS << ' ' << MachO->getFileName() << '\n';
92  }
93  }
94 }
95 
97  std::vector<Optional<StrOffsetsContributionDescriptor>>;
98 
99 // Collect all the contributions to the string offsets table from all units,
100 // sort them by their starting offsets and remove duplicates.
104  ContributionCollection Contributions;
105  for (const auto &CU : CUs)
106  Contributions.push_back(CU->getStringOffsetsTableContribution());
107  for (const auto &TUS : TUSs)
108  for (const auto &TU : TUS)
109  Contributions.push_back(TU->getStringOffsetsTableContribution());
110 
111  // Sort the contributions so that any invalid ones are placed at
112  // the start of the contributions vector. This way they are reported
113  // first.
114  llvm::sort(Contributions.begin(), Contributions.end(),
117  if (L && R) return L->Base < R->Base;
118  return R.hasValue();
119  });
120 
121  // Uniquify contributions, as it is possible that units (specifically
122  // type units in dwo or dwp files) share contributions. We don't want
123  // to report them more than once.
124  Contributions.erase(
125  std::unique(Contributions.begin(), Contributions.end(),
128  if (L && R)
129  return L->Base == R->Base && L->Size == R->Size;
130  return false;
131  }),
132  Contributions.end());
133  return Contributions;
134 }
135 
137  raw_ostream &OS, StringRef SectionName, const DWARFObject &Obj,
138  const DWARFSection &StringOffsetsSection, StringRef StringSection,
140  DWARFContext::tu_section_iterator_range TUSs, bool LittleEndian) {
141  auto Contributions = collectContributionData(CUs, TUSs);
142  DWARFDataExtractor StrOffsetExt(Obj, StringOffsetsSection, LittleEndian, 0);
143  DataExtractor StrData(StringSection, LittleEndian, 0);
144  uint64_t SectionSize = StringOffsetsSection.Data.size();
145  uint32_t Offset = 0;
146  for (auto &Contribution : Contributions) {
147  // Report an ill-formed contribution.
148  if (!Contribution) {
149  OS << "error: invalid contribution to string offsets table in section ."
150  << SectionName << ".\n";
151  return;
152  }
153 
154  dwarf::DwarfFormat Format = Contribution->getFormat();
155  uint16_t Version = Contribution->getVersion();
156  uint64_t ContributionHeader = Contribution->Base;
157  // In DWARF v5 there is a contribution header that immediately precedes
158  // the string offsets base (the location we have previously retrieved from
159  // the CU DIE's DW_AT_str_offsets attribute). The header is located either
160  // 8 or 16 bytes before the base, depending on the contribution's format.
161  if (Version >= 5)
162  ContributionHeader -= Format == DWARF32 ? 8 : 16;
163 
164  // Detect overlapping contributions.
165  if (Offset > ContributionHeader) {
166  OS << "error: overlapping contributions to string offsets table in "
167  "section ."
168  << SectionName << ".\n";
169  return;
170  }
171  // Report a gap in the table.
172  if (Offset < ContributionHeader) {
173  OS << format("0x%8.8x: Gap, length = ", Offset);
174  OS << (ContributionHeader - Offset) << "\n";
175  }
176  OS << format("0x%8.8x: ", (uint32_t)ContributionHeader);
177  // In DWARF v5 the contribution size in the descriptor does not equal
178  // the originally encoded length (it does not contain the length of the
179  // version field and the padding, a total of 4 bytes). Add them back in
180  // for reporting.
181  OS << "Contribution size = " << (Contribution->Size + (Version < 5 ? 0 : 4))
182  << ", Format = " << (Format == DWARF32 ? "DWARF32" : "DWARF64")
183  << ", Version = " << Version << "\n";
184 
185  Offset = Contribution->Base;
186  unsigned EntrySize = Contribution->getDwarfOffsetByteSize();
187  while (Offset - Contribution->Base < Contribution->Size) {
188  OS << format("0x%8.8x: ", Offset);
189  // FIXME: We can only extract strings if the offset fits in 32 bits.
190  uint64_t StringOffset =
191  StrOffsetExt.getRelocatedValue(EntrySize, &Offset);
192  // Extract the string if we can and display it. Otherwise just report
193  // the offset.
194  if (StringOffset <= std::numeric_limits<uint32_t>::max()) {
195  uint32_t StringOffset32 = (uint32_t)StringOffset;
196  OS << format("%8.8x ", StringOffset32);
197  const char *S = StrData.getCStr(&StringOffset32);
198  if (S)
199  OS << format("\"%s\"", S);
200  } else
201  OS << format("%16.16" PRIx64 " ", StringOffset);
202  OS << "\n";
203  }
204  }
205  // Report a gap at the end of the table.
206  if (Offset < SectionSize) {
207  OS << format("0x%8.8x: Gap, length = ", Offset);
208  OS << (SectionSize - Offset) << "\n";
209  }
210 }
211 
212 // Dump a DWARF string offsets section. This may be a DWARF v5 formatted
213 // string offsets section, where each compile or type unit contributes a
214 // number of entries (string offsets), with each contribution preceded by
215 // a header containing size and version number. Alternatively, it may be a
216 // monolithic series of string offsets, as generated by the pre-DWARF v5
217 // implementation of split DWARF.
219  raw_ostream &OS, StringRef SectionName, const DWARFObject &Obj,
220  const DWARFSection &StringOffsetsSection, StringRef StringSection,
222  DWARFContext::tu_section_iterator_range TUSs, bool LittleEndian,
223  unsigned MaxVersion) {
224  // If we have at least one (compile or type) unit with DWARF v5 or greater,
225  // we assume that the section is formatted like a DWARF v5 string offsets
226  // section.
227  if (MaxVersion >= 5)
228  dumpDWARFv5StringOffsetsSection(OS, SectionName, Obj, StringOffsetsSection,
229  StringSection, CUs, TUSs, LittleEndian);
230  else {
231  DataExtractor strOffsetExt(StringOffsetsSection.Data, LittleEndian, 0);
232  uint32_t offset = 0;
233  uint64_t size = StringOffsetsSection.Data.size();
234  // Ensure that size is a multiple of the size of an entry.
235  if (size & ((uint64_t)(sizeof(uint32_t) - 1))) {
236  OS << "error: size of ." << SectionName << " is not a multiple of "
237  << sizeof(uint32_t) << ".\n";
238  size &= -(uint64_t)sizeof(uint32_t);
239  }
240  DataExtractor StrData(StringSection, LittleEndian, 0);
241  while (offset < size) {
242  OS << format("0x%8.8x: ", offset);
243  uint32_t StringOffset = strOffsetExt.getU32(&offset);
244  OS << format("%8.8x ", StringOffset);
245  const char *S = StrData.getCStr(&StringOffset);
246  if (S)
247  OS << format("\"%s\"", S);
248  OS << "\n";
249  }
250  }
251 }
252 
253 // Dump the .debug_addr section.
254 static void dumpAddrSection(raw_ostream &OS, DWARFDataExtractor &AddrData,
255  DIDumpOptions DumpOpts, uint16_t Version,
256  uint8_t AddrSize) {
257  // TODO: Make this more general: add callback types to Error.h, create
258  // implementation and make all DWARF classes use them.
259  static auto WarnCallback = [](Error Warn) {
260  handleAllErrors(std::move(Warn), [](ErrorInfoBase &Info) {
261  WithColor::warning() << Info.message() << '\n';
262  });
263  };
264  uint32_t Offset = 0;
265  while (AddrData.isValidOffset(Offset)) {
266  DWARFDebugAddrTable AddrTable;
267  uint32_t TableOffset = Offset;
268  if (Error Err = AddrTable.extract(AddrData, &Offset, Version,
269  AddrSize, WarnCallback)) {
270  WithColor::error() << toString(std::move(Err)) << '\n';
271  // Keep going after an error, if we can, assuming that the length field
272  // could be read. If it couldn't, stop reading the section.
273  if (!AddrTable.hasValidLength())
274  break;
275  uint64_t Length = AddrTable.getLength();
276  Offset = TableOffset + Length;
277  } else {
278  AddrTable.dump(OS, DumpOpts);
279  }
280  }
281 }
282 
283 // Dump the .debug_rnglists or .debug_rnglists.dwo section (DWARF v5).
285  DWARFDataExtractor &rnglistData,
286  DIDumpOptions DumpOpts) {
287  uint32_t Offset = 0;
288  while (rnglistData.isValidOffset(Offset)) {
290  uint32_t TableOffset = Offset;
291  if (Error Err = Rnglists.extract(rnglistData, &Offset)) {
292  WithColor::error() << toString(std::move(Err)) << '\n';
293  uint64_t Length = Rnglists.length();
294  // Keep going after an error, if we can, assuming that the length field
295  // could be read. If it couldn't, stop reading the section.
296  if (Length == 0)
297  break;
298  Offset = TableOffset + Length;
299  } else {
300  Rnglists.dump(OS, DumpOpts);
301  }
302  }
303 }
304 
306  raw_ostream &OS, DIDumpOptions DumpOpts,
307  std::array<Optional<uint64_t>, DIDT_ID_Count> DumpOffsets) {
308 
309  Optional<uint64_t> DumpOffset;
310  uint64_t DumpType = DumpOpts.DumpType;
311 
312  StringRef Extension = sys::path::extension(DObj->getFileName());
313  bool IsDWO = (Extension == ".dwo") || (Extension == ".dwp");
314 
315  // Print UUID header.
316  const auto *ObjFile = DObj->getFile();
317  if (DumpType & DIDT_UUID)
318  dumpUUID(OS, *ObjFile);
319 
320  // Print a header for each explicitly-requested section.
321  // Otherwise just print one for non-empty sections.
322  // Only print empty .dwo section headers when dumping a .dwo file.
323  bool Explicit = DumpType != DIDT_All && !IsDWO;
324  bool ExplicitDWO = Explicit && IsDWO;
325  auto shouldDump = [&](bool Explicit, const char *Name, unsigned ID,
326  StringRef Section) {
327  DumpOffset = DumpOffsets[ID];
328  unsigned Mask = 1U << ID;
329  bool Should = (DumpType & Mask) && (Explicit || !Section.empty());
330  if (Should)
331  OS << "\n" << Name << " contents:\n";
332  return Should;
333  };
334 
335  // Dump individual sections.
336  if (shouldDump(Explicit, ".debug_abbrev", DIDT_ID_DebugAbbrev,
337  DObj->getAbbrevSection()))
338  getDebugAbbrev()->dump(OS);
339  if (shouldDump(ExplicitDWO, ".debug_abbrev.dwo", DIDT_ID_DebugAbbrev,
340  DObj->getAbbrevDWOSection()))
341  getDebugAbbrevDWO()->dump(OS);
342 
343  auto dumpDebugInfo = [&](bool IsExplicit, const char *Name,
345  if (shouldDump(IsExplicit, Name, DIDT_ID_DebugInfo, Section.Data)) {
346  if (DumpOffset)
347  getDIEForOffset(DumpOffset.getValue())
348  .dump(OS, 0, DumpOpts.noImplicitRecursion());
349  else
350  for (const auto &CU : CUs)
351  CU->dump(OS, DumpOpts);
352  }
353  };
354  dumpDebugInfo(Explicit, ".debug_info", DObj->getInfoSection(),
355  compile_units());
356  dumpDebugInfo(ExplicitDWO, ".debug_info.dwo", DObj->getInfoDWOSection(),
358 
359  auto dumpDebugType = [&](const char *Name,
360  tu_section_iterator_range TUSections) {
361  OS << '\n' << Name << " contents:\n";
362  DumpOffset = DumpOffsets[DIDT_ID_DebugTypes];
363  for (const auto &TUS : TUSections)
364  for (const auto &TU : TUS)
365  if (DumpOffset)
366  TU->getDIEForOffset(*DumpOffset)
367  .dump(OS, 0, DumpOpts.noImplicitRecursion());
368  else
369  TU->dump(OS, DumpOpts);
370  };
371  if ((DumpType & DIDT_DebugTypes)) {
372  if (Explicit || getNumTypeUnits())
373  dumpDebugType(".debug_types", type_unit_sections());
374  if (ExplicitDWO || getNumDWOTypeUnits())
375  dumpDebugType(".debug_types.dwo", dwo_type_unit_sections());
376  }
377 
378  if (shouldDump(Explicit, ".debug_loc", DIDT_ID_DebugLoc,
379  DObj->getLocSection().Data)) {
380  getDebugLoc()->dump(OS, getRegisterInfo(), DumpOffset);
381  }
382  if (shouldDump(ExplicitDWO, ".debug_loc.dwo", DIDT_ID_DebugLoc,
383  DObj->getLocDWOSection().Data)) {
384  getDebugLocDWO()->dump(OS, getRegisterInfo(), DumpOffset);
385  }
386 
387  if (shouldDump(Explicit, ".debug_frame", DIDT_ID_DebugFrame,
388  DObj->getDebugFrameSection()))
389  getDebugFrame()->dump(OS, getRegisterInfo(), DumpOffset);
390 
391  if (shouldDump(Explicit, ".eh_frame", DIDT_ID_DebugFrame,
392  DObj->getEHFrameSection()))
393  getEHFrame()->dump(OS, getRegisterInfo(), DumpOffset);
394 
395  if (DumpType & DIDT_DebugMacro) {
396  if (Explicit || !getDebugMacro()->empty()) {
397  OS << "\n.debug_macinfo contents:\n";
398  getDebugMacro()->dump(OS);
399  }
400  }
401 
402  if (shouldDump(Explicit, ".debug_aranges", DIDT_ID_DebugAranges,
403  DObj->getARangeSection())) {
404  uint32_t offset = 0;
405  DataExtractor arangesData(DObj->getARangeSection(), isLittleEndian(), 0);
407  while (set.extract(arangesData, &offset))
408  set.dump(OS);
409  }
410 
411  auto DumpLineSection = [&](DWARFDebugLine::SectionParser Parser,
412  DIDumpOptions DumpOpts) {
413  while (!Parser.done()) {
414  if (DumpOffset && Parser.getOffset() != *DumpOffset) {
415  Parser.skip();
416  continue;
417  }
418  OS << "debug_line[" << format("0x%8.8x", Parser.getOffset()) << "]\n";
419  if (DumpOpts.Verbose) {
421  } else {
422  DWARFDebugLine::LineTable LineTable = Parser.parseNext();
423  LineTable.dump(OS, DumpOpts);
424  }
425  }
426  };
427 
428  if (shouldDump(Explicit, ".debug_line", DIDT_ID_DebugLine,
429  DObj->getLineSection().Data)) {
430  DWARFDataExtractor LineData(*DObj, DObj->getLineSection(), isLittleEndian(),
431  0);
432  DWARFDebugLine::SectionParser Parser(LineData, *this, compile_units(),
434  DumpLineSection(Parser, DumpOpts);
435  }
436 
437  if (shouldDump(ExplicitDWO, ".debug_line.dwo", DIDT_ID_DebugLine,
438  DObj->getLineDWOSection().Data)) {
439  DWARFDataExtractor LineData(*DObj, DObj->getLineDWOSection(),
440  isLittleEndian(), 0);
441  DWARFDebugLine::SectionParser Parser(LineData, *this, dwo_compile_units(),
443  DumpLineSection(Parser, DumpOpts);
444  }
445 
446  if (shouldDump(Explicit, ".debug_cu_index", DIDT_ID_DebugCUIndex,
447  DObj->getCUIndexSection())) {
448  getCUIndex().dump(OS);
449  }
450 
451  if (shouldDump(Explicit, ".debug_tu_index", DIDT_ID_DebugTUIndex,
452  DObj->getTUIndexSection())) {
453  getTUIndex().dump(OS);
454  }
455 
456  if (shouldDump(Explicit, ".debug_str", DIDT_ID_DebugStr,
457  DObj->getStringSection())) {
458  DataExtractor strData(DObj->getStringSection(), isLittleEndian(), 0);
459  uint32_t offset = 0;
460  uint32_t strOffset = 0;
461  while (const char *s = strData.getCStr(&offset)) {
462  OS << format("0x%8.8x: \"%s\"\n", strOffset, s);
463  strOffset = offset;
464  }
465  }
466  if (shouldDump(ExplicitDWO, ".debug_str.dwo", DIDT_ID_DebugStr,
467  DObj->getStringDWOSection())) {
468  DataExtractor strDWOData(DObj->getStringDWOSection(), isLittleEndian(), 0);
469  uint32_t offset = 0;
470  uint32_t strDWOOffset = 0;
471  while (const char *s = strDWOData.getCStr(&offset)) {
472  OS << format("0x%8.8x: \"%s\"\n", strDWOOffset, s);
473  strDWOOffset = offset;
474  }
475  }
476  if (shouldDump(Explicit, ".debug_line_str", DIDT_ID_DebugLineStr,
477  DObj->getLineStringSection())) {
478  DataExtractor strData(DObj->getLineStringSection(), isLittleEndian(), 0);
479  uint32_t offset = 0;
480  uint32_t strOffset = 0;
481  while (const char *s = strData.getCStr(&offset)) {
482  OS << format("0x%8.8x: \"", strOffset);
483  OS.write_escaped(s);
484  OS << "\"\n";
485  strOffset = offset;
486  }
487  }
488 
489  if (shouldDump(Explicit, ".debug_addr", DIDT_ID_DebugAddr,
490  DObj->getAddrSection().Data)) {
491  DWARFDataExtractor AddrData(*DObj, DObj->getAddrSection(),
492  isLittleEndian(), 0);
493  dumpAddrSection(OS, AddrData, DumpOpts, getMaxVersion(), getCUAddrSize());
494  }
495 
496  if (shouldDump(Explicit, ".debug_ranges", DIDT_ID_DebugRanges,
497  DObj->getRangeSection().Data)) {
498  uint8_t savedAddressByteSize = getCUAddrSize();
499  DWARFDataExtractor rangesData(*DObj, DObj->getRangeSection(),
500  isLittleEndian(), savedAddressByteSize);
501  uint32_t offset = 0;
502  DWARFDebugRangeList rangeList;
503  while (rangesData.isValidOffset(offset)) {
504  if (Error E = rangeList.extract(rangesData, &offset)) {
505  WithColor::error() << toString(std::move(E)) << '\n';
506  break;
507  }
508  rangeList.dump(OS);
509  }
510  }
511 
512  if (shouldDump(Explicit, ".debug_rnglists", DIDT_ID_DebugRnglists,
513  DObj->getRnglistsSection().Data)) {
514  DWARFDataExtractor RnglistData(*DObj, DObj->getRnglistsSection(),
515  isLittleEndian(), 0);
516  dumpRnglistsSection(OS, RnglistData, DumpOpts);
517  }
518 
519  if (shouldDump(ExplicitDWO, ".debug_rnglists.dwo", DIDT_ID_DebugRnglists,
520  DObj->getRnglistsDWOSection().Data)) {
521  DWARFDataExtractor RnglistData(*DObj, DObj->getRnglistsDWOSection(),
522  isLittleEndian(), 0);
523  dumpRnglistsSection(OS, RnglistData, DumpOpts);
524  }
525 
526  if (shouldDump(Explicit, ".debug_pubnames", DIDT_ID_DebugPubnames,
527  DObj->getPubNamesSection()))
528  DWARFDebugPubTable(DObj->getPubNamesSection(), isLittleEndian(), false)
529  .dump(OS);
530 
531  if (shouldDump(Explicit, ".debug_pubtypes", DIDT_ID_DebugPubtypes,
532  DObj->getPubTypesSection()))
533  DWARFDebugPubTable(DObj->getPubTypesSection(), isLittleEndian(), false)
534  .dump(OS);
535 
536  if (shouldDump(Explicit, ".debug_gnu_pubnames", DIDT_ID_DebugGnuPubnames,
537  DObj->getGnuPubNamesSection()))
538  DWARFDebugPubTable(DObj->getGnuPubNamesSection(), isLittleEndian(),
539  true /* GnuStyle */)
540  .dump(OS);
541 
542  if (shouldDump(Explicit, ".debug_gnu_pubtypes", DIDT_ID_DebugGnuPubtypes,
543  DObj->getGnuPubTypesSection()))
544  DWARFDebugPubTable(DObj->getGnuPubTypesSection(), isLittleEndian(),
545  true /* GnuStyle */)
546  .dump(OS);
547 
548  if (shouldDump(Explicit, ".debug_str_offsets", DIDT_ID_DebugStrOffsets,
549  DObj->getStringOffsetSection().Data))
551  OS, "debug_str_offsets", *DObj, DObj->getStringOffsetSection(),
552  DObj->getStringSection(), compile_units(), type_unit_sections(),
554  if (shouldDump(ExplicitDWO, ".debug_str_offsets.dwo", DIDT_ID_DebugStrOffsets,
555  DObj->getStringOffsetDWOSection().Data))
557  OS, "debug_str_offsets.dwo", *DObj, DObj->getStringOffsetDWOSection(),
558  DObj->getStringDWOSection(), dwo_compile_units(),
560 
561  if (shouldDump(Explicit, ".gnu_index", DIDT_ID_GdbIndex,
562  DObj->getGdbIndexSection())) {
563  getGdbIndex().dump(OS);
564  }
565 
566  if (shouldDump(Explicit, ".apple_names", DIDT_ID_AppleNames,
567  DObj->getAppleNamesSection().Data))
568  getAppleNames().dump(OS);
569 
570  if (shouldDump(Explicit, ".apple_types", DIDT_ID_AppleTypes,
571  DObj->getAppleTypesSection().Data))
572  getAppleTypes().dump(OS);
573 
574  if (shouldDump(Explicit, ".apple_namespaces", DIDT_ID_AppleNamespaces,
575  DObj->getAppleNamespacesSection().Data))
576  getAppleNamespaces().dump(OS);
577 
578  if (shouldDump(Explicit, ".apple_objc", DIDT_ID_AppleObjC,
579  DObj->getAppleObjCSection().Data))
580  getAppleObjC().dump(OS);
581  if (shouldDump(Explicit, ".debug_names", DIDT_ID_DebugNames,
582  DObj->getDebugNamesSection().Data))
583  getDebugNames().dump(OS);
584 }
585 
587  DWOCUs.parseDWO(*this, DObj->getInfoDWOSection(), true);
588 
589  if (const auto &CUI = getCUIndex()) {
590  if (const auto *R = CUI.getFromHash(Hash))
591  return DWOCUs.getUnitForIndexEntry(*R);
592  return nullptr;
593  }
594 
595  // If there's no index, just search through the CUs in the DWO - there's
596  // probably only one unless this is something like LTO - though an in-process
597  // built/cached lookup table could be used in that case to improve repeated
598  // lookups of different CUs in the DWO.
599  for (const auto &DWOCU : dwo_compile_units()) {
600  // Might not have parsed DWO ID yet.
601  if (!DWOCU->getDWOId()) {
602  if (Optional<uint64_t> DWOId =
603  toUnsigned(DWOCU->getUnitDIE().find(DW_AT_GNU_dwo_id)))
604  DWOCU->setDWOId(*DWOId);
605  else
606  // No DWO ID?
607  continue;
608  }
609  if (DWOCU->getDWOId() == Hash)
610  return DWOCU.get();
611  }
612  return nullptr;
613 }
614 
616  parseCompileUnits();
617  if (auto *CU = CUs.getUnitForOffset(Offset))
618  return CU->getDIEForOffset(Offset);
619  return DWARFDie();
620 }
621 
623  bool Success = true;
624  DWARFVerifier verifier(OS, *this, DumpOpts);
625 
626  Success &= verifier.handleDebugAbbrev();
627  if (DumpOpts.DumpType & DIDT_DebugInfo)
628  Success &= verifier.handleDebugInfo();
629  if (DumpOpts.DumpType & DIDT_DebugLine)
630  Success &= verifier.handleDebugLine();
631  Success &= verifier.handleAccelTables();
632  return Success;
633 }
634 
636  if (CUIndex)
637  return *CUIndex;
638 
639  DataExtractor CUIndexData(DObj->getCUIndexSection(), isLittleEndian(), 0);
640 
641  CUIndex = llvm::make_unique<DWARFUnitIndex>(DW_SECT_INFO);
642  CUIndex->parse(CUIndexData);
643  return *CUIndex;
644 }
645 
647  if (TUIndex)
648  return *TUIndex;
649 
650  DataExtractor TUIndexData(DObj->getTUIndexSection(), isLittleEndian(), 0);
651 
652  TUIndex = llvm::make_unique<DWARFUnitIndex>(DW_SECT_TYPES);
653  TUIndex->parse(TUIndexData);
654  return *TUIndex;
655 }
656 
658  if (GdbIndex)
659  return *GdbIndex;
660 
661  DataExtractor GdbIndexData(DObj->getGdbIndexSection(), true /*LE*/, 0);
662  GdbIndex = llvm::make_unique<DWARFGdbIndex>();
663  GdbIndex->parse(GdbIndexData);
664  return *GdbIndex;
665 }
666 
668  if (Abbrev)
669  return Abbrev.get();
670 
671  DataExtractor abbrData(DObj->getAbbrevSection(), isLittleEndian(), 0);
672 
673  Abbrev.reset(new DWARFDebugAbbrev());
674  Abbrev->extract(abbrData);
675  return Abbrev.get();
676 }
677 
679  if (AbbrevDWO)
680  return AbbrevDWO.get();
681 
682  DataExtractor abbrData(DObj->getAbbrevDWOSection(), isLittleEndian(), 0);
683  AbbrevDWO.reset(new DWARFDebugAbbrev());
684  AbbrevDWO->extract(abbrData);
685  return AbbrevDWO.get();
686 }
687 
689  if (Loc)
690  return Loc.get();
691 
692  Loc.reset(new DWARFDebugLoc);
693  // Assume all compile units have the same address byte size.
694  if (getNumCompileUnits()) {
695  DWARFDataExtractor LocData(*DObj, DObj->getLocSection(), isLittleEndian(),
697  Loc->parse(LocData);
698  }
699  return Loc.get();
700 }
701 
703  if (LocDWO)
704  return LocDWO.get();
705 
706  LocDWO.reset(new DWARFDebugLocDWO());
707  // Assume all compile units have the same address byte size.
708  if (getNumCompileUnits()) {
709  DataExtractor LocData(DObj->getLocDWOSection().Data, isLittleEndian(),
711  LocDWO->parse(LocData);
712  }
713  return LocDWO.get();
714 }
715 
717  if (Aranges)
718  return Aranges.get();
719 
720  Aranges.reset(new DWARFDebugAranges());
721  Aranges->generate(this);
722  return Aranges.get();
723 }
724 
726  if (DebugFrame)
727  return DebugFrame.get();
728 
729  // There's a "bug" in the DWARFv3 standard with respect to the target address
730  // size within debug frame sections. While DWARF is supposed to be independent
731  // of its container, FDEs have fields with size being "target address size",
732  // which isn't specified in DWARF in general. It's only specified for CUs, but
733  // .eh_frame can appear without a .debug_info section. Follow the example of
734  // other tools (libdwarf) and extract this from the container (ObjectFile
735  // provides this information). This problem is fixed in DWARFv4
736  // See this dwarf-discuss discussion for more details:
737  // http://lists.dwarfstd.org/htdig.cgi/dwarf-discuss-dwarfstd.org/2011-December/001173.html
738  DWARFDataExtractor debugFrameData(DObj->getDebugFrameSection(),
739  isLittleEndian(), DObj->getAddressSize());
740  DebugFrame.reset(new DWARFDebugFrame(false /* IsEH */));
741  DebugFrame->parse(debugFrameData);
742  return DebugFrame.get();
743 }
744 
746  if (EHFrame)
747  return EHFrame.get();
748 
749  DWARFDataExtractor debugFrameData(DObj->getEHFrameSection(), isLittleEndian(),
750  DObj->getAddressSize());
751  DebugFrame.reset(new DWARFDebugFrame(true /* IsEH */));
752  DebugFrame->parse(debugFrameData);
753  return DebugFrame.get();
754 }
755 
757  if (Macro)
758  return Macro.get();
759 
760  DataExtractor MacinfoData(DObj->getMacinfoSection(), isLittleEndian(), 0);
761  Macro.reset(new DWARFDebugMacro());
762  Macro->parse(MacinfoData);
763  return Macro.get();
764 }
765 
766 template <typename T>
767 static T &getAccelTable(std::unique_ptr<T> &Cache, const DWARFObject &Obj,
768  const DWARFSection &Section, StringRef StringSection,
769  bool IsLittleEndian) {
770  if (Cache)
771  return *Cache;
772  DWARFDataExtractor AccelSection(Obj, Section, IsLittleEndian, 0);
773  DataExtractor StrData(StringSection, IsLittleEndian, 0);
774  Cache.reset(new T(AccelSection, StrData));
775  if (Error E = Cache->extract())
776  llvm::consumeError(std::move(E));
777  return *Cache;
778 }
779 
781  return getAccelTable(Names, *DObj, DObj->getDebugNamesSection(),
782  DObj->getStringSection(), isLittleEndian());
783 }
784 
786  return getAccelTable(AppleNames, *DObj, DObj->getAppleNamesSection(),
787  DObj->getStringSection(), isLittleEndian());
788 }
789 
791  return getAccelTable(AppleTypes, *DObj, DObj->getAppleTypesSection(),
792  DObj->getStringSection(), isLittleEndian());
793 }
794 
796  return getAccelTable(AppleNamespaces, *DObj,
797  DObj->getAppleNamespacesSection(),
798  DObj->getStringSection(), isLittleEndian());
799 }
800 
802  return getAccelTable(AppleObjC, *DObj, DObj->getAppleObjCSection(),
803  DObj->getStringSection(), isLittleEndian());
804 }
805 
810  if (!ExpectedLineTable) {
811  DWARFDebugLine::warn(ExpectedLineTable.takeError());
812  return nullptr;
813  }
814  return *ExpectedLineTable;
815 }
816 
818  DWARFUnit *U, std::function<void(Error)> RecoverableErrorCallback) {
819  if (!Line)
820  Line.reset(new DWARFDebugLine);
821 
822  auto UnitDIE = U->getUnitDIE();
823  if (!UnitDIE)
824  return nullptr;
825 
826  auto Offset = toSectionOffset(UnitDIE.find(DW_AT_stmt_list));
827  if (!Offset)
828  return nullptr; // No line table for this compile unit.
829 
830  uint32_t stmtOffset = *Offset + U->getLineTableOffset();
831  // See if the line table is cached.
832  if (const DWARFLineTable *lt = Line->getLineTable(stmtOffset))
833  return lt;
834 
835  // Make sure the offset is good before we try to parse.
836  if (stmtOffset >= U->getLineSection().Data.size())
837  return nullptr;
838 
839  // We have to parse it first.
841  U->getAddressByteSize());
842  return Line->getOrParseLineTable(lineData, stmtOffset, *this, U,
843  RecoverableErrorCallback);
844 }
845 
846 void DWARFContext::parseCompileUnits() {
847  CUs.parse(*this, DObj->getInfoSection());
848 }
849 
850 void DWARFContext::parseTypeUnits() {
851  if (!TUs.empty())
852  return;
853  DObj->forEachTypesSections([&](const DWARFSection &S) {
854  TUs.emplace_back();
855  TUs.back().parse(*this, S);
856  });
857 }
858 
859 void DWARFContext::parseDWOCompileUnits() {
860  DWOCUs.parseDWO(*this, DObj->getInfoDWOSection());
861 }
862 
863 void DWARFContext::parseDWOTypeUnits() {
864  if (!DWOTUs.empty())
865  return;
866  DObj->forEachTypesDWOSections([&](const DWARFSection &S) {
867  DWOTUs.emplace_back();
868  DWOTUs.back().parseDWO(*this, S);
869  });
870 }
871 
873  parseCompileUnits();
874  return CUs.getUnitForOffset(Offset);
875 }
876 
877 DWARFCompileUnit *DWARFContext::getCompileUnitForAddress(uint64_t Address) {
878  // First, get the offset of the compile unit.
879  uint32_t CUOffset = getDebugAranges()->findAddress(Address);
880  // Retrieve the compile unit.
881  return getCompileUnitForOffset(CUOffset);
882 }
883 
886 
887  DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
888  if (!CU)
889  return Result;
890 
891  Result.CompileUnit = CU;
892  Result.FunctionDIE = CU->getSubroutineForAddress(Address);
893 
894  std::vector<DWARFDie> Worklist;
895  Worklist.push_back(Result.FunctionDIE);
896  while (!Worklist.empty()) {
897  DWARFDie DIE = Worklist.back();
898  Worklist.pop_back();
899 
900  if (DIE.getTag() == DW_TAG_lexical_block &&
901  DIE.addressRangeContainsAddress(Address)) {
902  Result.BlockDIE = DIE;
903  break;
904  }
905 
906  for (auto Child : DIE)
907  Worklist.push_back(Child);
908  }
909 
910  return Result;
911 }
912 
914  uint64_t Address,
915  FunctionNameKind Kind,
916  std::string &FunctionName,
917  uint32_t &StartLine) {
918  // The address may correspond to instruction in some inlined function,
919  // so we have to build the chain of inlined functions and take the
920  // name of the topmost function in it.
921  SmallVector<DWARFDie, 4> InlinedChain;
922  CU->getInlinedChainForAddress(Address, InlinedChain);
923  if (InlinedChain.empty())
924  return false;
925 
926  const DWARFDie &DIE = InlinedChain[0];
927  bool FoundResult = false;
928  const char *Name = nullptr;
929  if (Kind != FunctionNameKind::None && (Name = DIE.getSubroutineName(Kind))) {
930  FunctionName = Name;
931  FoundResult = true;
932  }
933  if (auto DeclLineResult = DIE.getDeclLine()) {
934  StartLine = DeclLineResult;
935  FoundResult = true;
936  }
937 
938  return FoundResult;
939 }
940 
942  DILineInfoSpecifier Spec) {
944 
945  DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
946  if (!CU)
947  return Result;
949  Result.FunctionName,
950  Result.StartLine);
951  if (Spec.FLIKind != FileLineInfoKind::None) {
952  if (const DWARFLineTable *LineTable = getLineTableForUnit(CU))
953  LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(),
954  Spec.FLIKind, Result);
955  }
956  return Result;
957 }
958 
961  DILineInfoSpecifier Spec) {
962  DILineInfoTable Lines;
963  DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
964  if (!CU)
965  return Lines;
966 
967  std::string FunctionName = "<invalid>";
968  uint32_t StartLine = 0;
969  getFunctionNameAndStartLineForAddress(CU, Address, Spec.FNKind, FunctionName,
970  StartLine);
971 
972  // If the Specifier says we don't need FileLineInfo, just
973  // return the top-most function at the starting address.
974  if (Spec.FLIKind == FileLineInfoKind::None) {
976  Result.FunctionName = FunctionName;
977  Result.StartLine = StartLine;
978  Lines.push_back(std::make_pair(Address, Result));
979  return Lines;
980  }
981 
982  const DWARFLineTable *LineTable = getLineTableForUnit(CU);
983 
984  // Get the index of row we're looking for in the line table.
985  std::vector<uint32_t> RowVector;
986  if (!LineTable->lookupAddressRange(Address, Size, RowVector))
987  return Lines;
988 
989  for (uint32_t RowIndex : RowVector) {
990  // Take file number and line/column from the row.
991  const DWARFDebugLine::Row &Row = LineTable->Rows[RowIndex];
993  LineTable->getFileNameByIndex(Row.File, CU->getCompilationDir(),
994  Spec.FLIKind, Result.FileName);
995  Result.FunctionName = FunctionName;
996  Result.Line = Row.Line;
997  Result.Column = Row.Column;
998  Result.StartLine = StartLine;
999  Lines.push_back(std::make_pair(Row.Address, Result));
1000  }
1001 
1002  return Lines;
1003 }
1004 
1007  DILineInfoSpecifier Spec) {
1008  DIInliningInfo InliningInfo;
1009 
1010  DWARFCompileUnit *CU = getCompileUnitForAddress(Address);
1011  if (!CU)
1012  return InliningInfo;
1013 
1014  const DWARFLineTable *LineTable = nullptr;
1015  SmallVector<DWARFDie, 4> InlinedChain;
1016  CU->getInlinedChainForAddress(Address, InlinedChain);
1017  if (InlinedChain.size() == 0) {
1018  // If there is no DIE for address (e.g. it is in unavailable .dwo file),
1019  // try to at least get file/line info from symbol table.
1020  if (Spec.FLIKind != FileLineInfoKind::None) {
1021  DILineInfo Frame;
1022  LineTable = getLineTableForUnit(CU);
1023  if (LineTable &&
1024  LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(),
1025  Spec.FLIKind, Frame))
1026  InliningInfo.addFrame(Frame);
1027  }
1028  return InliningInfo;
1029  }
1030 
1031  uint32_t CallFile = 0, CallLine = 0, CallColumn = 0, CallDiscriminator = 0;
1032  for (uint32_t i = 0, n = InlinedChain.size(); i != n; i++) {
1033  DWARFDie &FunctionDIE = InlinedChain[i];
1034  DILineInfo Frame;
1035  // Get function name if necessary.
1036  if (const char *Name = FunctionDIE.getSubroutineName(Spec.FNKind))
1037  Frame.FunctionName = Name;
1038  if (auto DeclLineResult = FunctionDIE.getDeclLine())
1039  Frame.StartLine = DeclLineResult;
1040  if (Spec.FLIKind != FileLineInfoKind::None) {
1041  if (i == 0) {
1042  // For the topmost frame, initialize the line table of this
1043  // compile unit and fetch file/line info from it.
1044  LineTable = getLineTableForUnit(CU);
1045  // For the topmost routine, get file/line info from line table.
1046  if (LineTable)
1047  LineTable->getFileLineInfoForAddress(Address, CU->getCompilationDir(),
1048  Spec.FLIKind, Frame);
1049  } else {
1050  // Otherwise, use call file, call line and call column from
1051  // previous DIE in inlined chain.
1052  if (LineTable)
1053  LineTable->getFileNameByIndex(CallFile, CU->getCompilationDir(),
1054  Spec.FLIKind, Frame.FileName);
1055  Frame.Line = CallLine;
1056  Frame.Column = CallColumn;
1057  Frame.Discriminator = CallDiscriminator;
1058  }
1059  // Get call file/line/column of a current DIE.
1060  if (i + 1 < n) {
1061  FunctionDIE.getCallerFrame(CallFile, CallLine, CallColumn,
1062  CallDiscriminator);
1063  }
1064  }
1065  InliningInfo.addFrame(Frame);
1066  }
1067  return InliningInfo;
1068 }
1069 
1070 std::shared_ptr<DWARFContext>
1072  if (auto S = DWP.lock()) {
1073  DWARFContext *Ctxt = S->Context.get();
1074  return std::shared_ptr<DWARFContext>(std::move(S), Ctxt);
1075  }
1076 
1077  std::weak_ptr<DWOFile> *Entry = &DWOFiles[AbsolutePath];
1078 
1079  if (auto S = Entry->lock()) {
1080  DWARFContext *Ctxt = S->Context.get();
1081  return std::shared_ptr<DWARFContext>(std::move(S), Ctxt);
1082  }
1083 
1085  if (!CheckedForDWP) {
1086  SmallString<128> DWPName;
1088  this->DWPName.empty()
1089  ? (DObj->getFileName() + ".dwp").toStringRef(DWPName)
1090  : StringRef(this->DWPName));
1091  if (Obj) {
1092  Entry = &DWP;
1093  return Obj;
1094  } else {
1095  CheckedForDWP = true;
1096  // TODO: Should this error be handled (maybe in a high verbosity mode)
1097  // before falling back to .dwo files?
1098  consumeError(Obj.takeError());
1099  }
1100  }
1101 
1102  return object::ObjectFile::createObjectFile(AbsolutePath);
1103  }();
1104 
1105  if (!Obj) {
1106  // TODO: Actually report errors helpfully.
1107  consumeError(Obj.takeError());
1108  return nullptr;
1109  }
1110 
1111  auto S = std::make_shared<DWOFile>();
1112  S->File = std::move(Obj.get());
1113  S->Context = DWARFContext::create(*S->File.getBinary());
1114  *Entry = S;
1115  auto *Ctxt = S->Context.get();
1116  return std::shared_ptr<DWARFContext>(std::move(S), Ctxt);
1117 }
1118 
1119 static Error createError(const Twine &Reason, llvm::Error E) {
1120  return make_error<StringError>(Reason + toString(std::move(E)),
1122 }
1123 
1124 /// SymInfo contains information about symbol: it's address
1125 /// and section index which is -1LL for absolute symbols.
1126 struct SymInfo {
1127  uint64_t Address;
1128  uint64_t SectionIndex;
1129 };
1130 
1131 /// Returns the address of symbol relocation used against and a section index.
1132 /// Used for futher relocations computation. Symbol's section load address is
1134  const RelocationRef &Reloc,
1135  const LoadedObjectInfo *L,
1136  std::map<SymbolRef, SymInfo> &Cache) {
1137  SymInfo Ret = {0, (uint64_t)-1LL};
1139  object::symbol_iterator Sym = Reloc.getSymbol();
1140 
1141  std::map<SymbolRef, SymInfo>::iterator CacheIt = Cache.end();
1142  // First calculate the address of the symbol or section as it appears
1143  // in the object file
1144  if (Sym != Obj.symbol_end()) {
1145  bool New;
1146  std::tie(CacheIt, New) = Cache.insert({*Sym, {0, 0}});
1147  if (!New)
1148  return CacheIt->second;
1149 
1150  Expected<uint64_t> SymAddrOrErr = Sym->getAddress();
1151  if (!SymAddrOrErr)
1152  return createError("failed to compute symbol address: ",
1153  SymAddrOrErr.takeError());
1154 
1155  // Also remember what section this symbol is in for later
1156  auto SectOrErr = Sym->getSection();
1157  if (!SectOrErr)
1158  return createError("failed to get symbol section: ",
1159  SectOrErr.takeError());
1160 
1161  RSec = *SectOrErr;
1162  Ret.Address = *SymAddrOrErr;
1163  } else if (auto *MObj = dyn_cast<MachOObjectFile>(&Obj)) {
1164  RSec = MObj->getRelocationSection(Reloc.getRawDataRefImpl());
1165  Ret.Address = RSec->getAddress();
1166  }
1167 
1168  if (RSec != Obj.section_end())
1169  Ret.SectionIndex = RSec->getIndex();
1170 
1171  // If we are given load addresses for the sections, we need to adjust:
1172  // SymAddr = (Address of Symbol Or Section in File) -
1173  // (Address of Section in File) +
1174  // (Load Address of Section)
1175  // RSec is now either the section being targeted or the section
1176  // containing the symbol being targeted. In either case,
1177  // we need to perform the same computation.
1178  if (L && RSec != Obj.section_end())
1179  if (uint64_t SectionLoadAddress = L->getSectionLoadAddress(*RSec))
1180  Ret.Address += SectionLoadAddress - RSec->getAddress();
1181 
1182  if (CacheIt != Cache.end())
1183  CacheIt->second = Ret;
1184 
1185  return Ret;
1186 }
1187 
1188 static bool isRelocScattered(const object::ObjectFile &Obj,
1189  const RelocationRef &Reloc) {
1190  const MachOObjectFile *MachObj = dyn_cast<MachOObjectFile>(&Obj);
1191  if (!MachObj)
1192  return false;
1193  // MachO also has relocations that point to sections and
1194  // scattered relocations.
1195  auto RelocInfo = MachObj->getRelocation(Reloc.getRawDataRefImpl());
1196  return MachObj->isRelocationScattered(RelocInfo);
1197 }
1198 
1200  WithColor::error() << toString(std::move(E)) << '\n';
1201  return ErrorPolicy::Continue;
1202 }
1203 
1204 namespace {
1205 struct DWARFSectionMap final : public DWARFSection {
1206  RelocAddrMap Relocs;
1207 };
1208 
1209 class DWARFObjInMemory final : public DWARFObject {
1210  bool IsLittleEndian;
1211  uint8_t AddressSize;
1212  StringRef FileName;
1213  const object::ObjectFile *Obj = nullptr;
1214  std::vector<SectionName> SectionNames;
1215 
1216  using TypeSectionMap = MapVector<object::SectionRef, DWARFSectionMap,
1217  std::map<object::SectionRef, unsigned>>;
1218 
1219  TypeSectionMap TypesSections;
1220  TypeSectionMap TypesDWOSections;
1221 
1222  DWARFSectionMap InfoSection;
1223  DWARFSectionMap LocSection;
1224  DWARFSectionMap LineSection;
1225  DWARFSectionMap RangeSection;
1226  DWARFSectionMap RnglistsSection;
1227  DWARFSectionMap StringOffsetSection;
1228  DWARFSectionMap InfoDWOSection;
1229  DWARFSectionMap LineDWOSection;
1230  DWARFSectionMap LocDWOSection;
1231  DWARFSectionMap StringOffsetDWOSection;
1232  DWARFSectionMap RangeDWOSection;
1233  DWARFSectionMap RnglistsDWOSection;
1234  DWARFSectionMap AddrSection;
1235  DWARFSectionMap AppleNamesSection;
1236  DWARFSectionMap AppleTypesSection;
1237  DWARFSectionMap AppleNamespacesSection;
1238  DWARFSectionMap AppleObjCSection;
1239  DWARFSectionMap DebugNamesSection;
1240 
1241  DWARFSectionMap *mapNameToDWARFSection(StringRef Name) {
1243  .Case("debug_info", &InfoSection)
1244  .Case("debug_loc", &LocSection)
1245  .Case("debug_line", &LineSection)
1246  .Case("debug_str_offsets", &StringOffsetSection)
1247  .Case("debug_ranges", &RangeSection)
1248  .Case("debug_rnglists", &RnglistsSection)
1249  .Case("debug_info.dwo", &InfoDWOSection)
1250  .Case("debug_loc.dwo", &LocDWOSection)
1251  .Case("debug_line.dwo", &LineDWOSection)
1252  .Case("debug_names", &DebugNamesSection)
1253  .Case("debug_rnglists.dwo", &RnglistsDWOSection)
1254  .Case("debug_str_offsets.dwo", &StringOffsetDWOSection)
1255  .Case("debug_addr", &AddrSection)
1256  .Case("apple_names", &AppleNamesSection)
1257  .Case("apple_types", &AppleTypesSection)
1258  .Case("apple_namespaces", &AppleNamespacesSection)
1259  .Case("apple_namespac", &AppleNamespacesSection)
1260  .Case("apple_objc", &AppleObjCSection)
1261  .Default(nullptr);
1262  }
1263 
1264  StringRef AbbrevSection;
1265  StringRef ARangeSection;
1266  StringRef DebugFrameSection;
1267  StringRef EHFrameSection;
1268  StringRef StringSection;
1269  StringRef MacinfoSection;
1270  StringRef PubNamesSection;
1271  StringRef PubTypesSection;
1272  StringRef GnuPubNamesSection;
1273  StringRef AbbrevDWOSection;
1274  StringRef StringDWOSection;
1275  StringRef GnuPubTypesSection;
1276  StringRef CUIndexSection;
1277  StringRef GdbIndexSection;
1278  StringRef TUIndexSection;
1279  StringRef LineStringSection;
1280 
1281  // A deque holding section data whose iterators are not invalidated when
1282  // new decompressed sections are inserted at the end.
1283  std::deque<SmallString<0>> UncompressedSections;
1284 
1285  StringRef *mapSectionToMember(StringRef Name) {
1286  if (DWARFSection *Sec = mapNameToDWARFSection(Name))
1287  return &Sec->Data;
1289  .Case("debug_abbrev", &AbbrevSection)
1290  .Case("debug_aranges", &ARangeSection)
1291  .Case("debug_frame", &DebugFrameSection)
1292  .Case("eh_frame", &EHFrameSection)
1293  .Case("debug_str", &StringSection)
1294  .Case("debug_macinfo", &MacinfoSection)
1295  .Case("debug_pubnames", &PubNamesSection)
1296  .Case("debug_pubtypes", &PubTypesSection)
1297  .Case("debug_gnu_pubnames", &GnuPubNamesSection)
1298  .Case("debug_gnu_pubtypes", &GnuPubTypesSection)
1299  .Case("debug_abbrev.dwo", &AbbrevDWOSection)
1300  .Case("debug_str.dwo", &StringDWOSection)
1301  .Case("debug_cu_index", &CUIndexSection)
1302  .Case("debug_tu_index", &TUIndexSection)
1303  .Case("gdb_index", &GdbIndexSection)
1304  .Case("debug_line_str", &LineStringSection)
1305  // Any more debug info sections go here.
1306  .Default(nullptr);
1307  }
1308 
1309  /// If Sec is compressed section, decompresses and updates its contents
1310  /// provided by Data. Otherwise leaves it unchanged.
1311  Error maybeDecompress(const object::SectionRef &Sec, StringRef Name,
1312  StringRef &Data) {
1313  if (!Decompressor::isCompressed(Sec))
1314  return Error::success();
1315 
1316  Expected<Decompressor> Decompressor =
1317  Decompressor::create(Name, Data, IsLittleEndian, AddressSize == 8);
1318  if (!Decompressor)
1319  return Decompressor.takeError();
1320 
1321  SmallString<0> Out;
1322  if (auto Err = Decompressor->resizeAndDecompress(Out))
1323  return Err;
1324 
1325  UncompressedSections.push_back(std::move(Out));
1326  Data = UncompressedSections.back();
1327 
1328  return Error::success();
1329  }
1330 
1331 public:
1332  DWARFObjInMemory(const StringMap<std::unique_ptr<MemoryBuffer>> &Sections,
1333  uint8_t AddrSize, bool IsLittleEndian)
1334  : IsLittleEndian(IsLittleEndian) {
1335  for (const auto &SecIt : Sections) {
1336  if (StringRef *SectionData = mapSectionToMember(SecIt.first()))
1337  *SectionData = SecIt.second->getBuffer();
1338  }
1339  }
1340  DWARFObjInMemory(const object::ObjectFile &Obj, const LoadedObjectInfo *L,
1341  function_ref<ErrorPolicy(Error)> HandleError)
1342  : IsLittleEndian(Obj.isLittleEndian()),
1343  AddressSize(Obj.getBytesInAddress()), FileName(Obj.getFileName()),
1344  Obj(&Obj) {
1345 
1346  StringMap<unsigned> SectionAmountMap;
1347  for (const SectionRef &Section : Obj.sections()) {
1348  StringRef Name;
1349  Section.getName(Name);
1350  ++SectionAmountMap[Name];
1351  SectionNames.push_back({ Name, true });
1352 
1353  // Skip BSS and Virtual sections, they aren't interesting.
1354  if (Section.isBSS() || Section.isVirtual())
1355  continue;
1356 
1357  // Skip sections stripped by dsymutil.
1358  if (Section.isStripped())
1359  continue;
1360 
1361  StringRef Data;
1362  section_iterator RelocatedSection = Section.getRelocatedSection();
1363  // Try to obtain an already relocated version of this section.
1364  // Else use the unrelocated section from the object file. We'll have to
1365  // apply relocations ourselves later.
1366  if (!L || !L->getLoadedSectionContents(*RelocatedSection, Data))
1367  Section.getContents(Data);
1368 
1369  if (auto Err = maybeDecompress(Section, Name, Data)) {
1370  ErrorPolicy EP = HandleError(createError(
1371  "failed to decompress '" + Name + "', ", std::move(Err)));
1372  if (EP == ErrorPolicy::Halt)
1373  return;
1374  continue;
1375  }
1376 
1377  // Compressed sections names in GNU style starts from ".z",
1378  // at this point section is decompressed and we drop compression prefix.
1379  Name = Name.substr(
1380  Name.find_first_not_of("._z")); // Skip ".", "z" and "_" prefixes.
1381 
1382  // Map platform specific debug section names to DWARF standard section
1383  // names.
1384  Name = Obj.mapDebugSectionName(Name);
1385 
1386  if (StringRef *SectionData = mapSectionToMember(Name)) {
1387  *SectionData = Data;
1388  if (Name == "debug_ranges") {
1389  // FIXME: Use the other dwo range section when we emit it.
1390  RangeDWOSection.Data = Data;
1391  }
1392  } else if (Name == "debug_types") {
1393  // Find debug_types data by section rather than name as there are
1394  // multiple, comdat grouped, debug_types sections.
1395  TypesSections[Section].Data = Data;
1396  } else if (Name == "debug_types.dwo") {
1397  TypesDWOSections[Section].Data = Data;
1398  }
1399 
1400  if (RelocatedSection == Obj.section_end())
1401  continue;
1402 
1403  StringRef RelSecName;
1404  StringRef RelSecData;
1405  RelocatedSection->getName(RelSecName);
1406 
1407  // If the section we're relocating was relocated already by the JIT,
1408  // then we used the relocated version above, so we do not need to process
1409  // relocations for it now.
1410  if (L && L->getLoadedSectionContents(*RelocatedSection, RelSecData))
1411  continue;
1412 
1413  // In Mach-o files, the relocations do not need to be applied if
1414  // there is no load offset to apply. The value read at the
1415  // relocation point already factors in the section address
1416  // (actually applying the relocations will produce wrong results
1417  // as the section address will be added twice).
1418  if (!L && isa<MachOObjectFile>(&Obj))
1419  continue;
1420 
1421  RelSecName = RelSecName.substr(
1422  RelSecName.find_first_not_of("._z")); // Skip . and _ prefixes.
1423 
1424  // TODO: Add support for relocations in other sections as needed.
1425  // Record relocations for the debug_info and debug_line sections.
1426  DWARFSectionMap *Sec = mapNameToDWARFSection(RelSecName);
1427  RelocAddrMap *Map = Sec ? &Sec->Relocs : nullptr;
1428  if (!Map) {
1429  // Find debug_types relocs by section rather than name as there are
1430  // multiple, comdat grouped, debug_types sections.
1431  if (RelSecName == "debug_types")
1432  Map =
1433  &static_cast<DWARFSectionMap &>(TypesSections[*RelocatedSection])
1434  .Relocs;
1435  else if (RelSecName == "debug_types.dwo")
1436  Map = &static_cast<DWARFSectionMap &>(
1437  TypesDWOSections[*RelocatedSection])
1438  .Relocs;
1439  else
1440  continue;
1441  }
1442 
1443  if (Section.relocation_begin() == Section.relocation_end())
1444  continue;
1445 
1446  // Symbol to [address, section index] cache mapping.
1447  std::map<SymbolRef, SymInfo> AddrCache;
1448  for (const RelocationRef &Reloc : Section.relocations()) {
1449  // FIXME: it's not clear how to correctly handle scattered
1450  // relocations.
1451  if (isRelocScattered(Obj, Reloc))
1452  continue;
1453 
1454  Expected<SymInfo> SymInfoOrErr =
1455  getSymbolInfo(Obj, Reloc, L, AddrCache);
1456  if (!SymInfoOrErr) {
1457  if (HandleError(SymInfoOrErr.takeError()) == ErrorPolicy::Halt)
1458  return;
1459  continue;
1460  }
1461 
1462  object::RelocVisitor V(Obj);
1463  uint64_t Val = V.visit(Reloc.getType(), Reloc, SymInfoOrErr->Address);
1464  if (V.error()) {
1466  Reloc.getTypeName(Type);
1467  ErrorPolicy EP = HandleError(
1468  createError("failed to compute relocation: " + Type + ", ",
1469  errorCodeToError(object_error::parse_failed)));
1470  if (EP == ErrorPolicy::Halt)
1471  return;
1472  continue;
1473  }
1474  RelocAddrEntry Rel = {SymInfoOrErr->SectionIndex, Val};
1475  Map->insert({Reloc.getOffset(), Rel});
1476  }
1477  }
1478 
1479  for (SectionName &S : SectionNames)
1480  if (SectionAmountMap[S.Name] > 1)
1481  S.IsNameUnique = false;
1482  }
1483 
1485  uint64_t Pos) const override {
1486  auto &Sec = static_cast<const DWARFSectionMap &>(S);
1487  RelocAddrMap::const_iterator AI = Sec.Relocs.find(Pos);
1488  if (AI == Sec.Relocs.end())
1489  return None;
1490  return AI->second;
1491  }
1492 
1493  const object::ObjectFile *getFile() const override { return Obj; }
1494 
1495  ArrayRef<SectionName> getSectionNames() const override {
1496  return SectionNames;
1497  }
1498 
1499  bool isLittleEndian() const override { return IsLittleEndian; }
1500  StringRef getAbbrevDWOSection() const override { return AbbrevDWOSection; }
1501  const DWARFSection &getLineDWOSection() const override {
1502  return LineDWOSection;
1503  }
1504  const DWARFSection &getLocDWOSection() const override {
1505  return LocDWOSection;
1506  }
1507  StringRef getStringDWOSection() const override { return StringDWOSection; }
1508  const DWARFSection &getStringOffsetDWOSection() const override {
1509  return StringOffsetDWOSection;
1510  }
1511  const DWARFSection &getRangeDWOSection() const override {
1512  return RangeDWOSection;
1513  }
1514  const DWARFSection &getRnglistsDWOSection() const override {
1515  return RnglistsDWOSection;
1516  }
1517  const DWARFSection &getAddrSection() const override { return AddrSection; }
1518  StringRef getCUIndexSection() const override { return CUIndexSection; }
1519  StringRef getGdbIndexSection() const override { return GdbIndexSection; }
1520  StringRef getTUIndexSection() const override { return TUIndexSection; }
1521 
1522  // DWARF v5
1523  const DWARFSection &getStringOffsetSection() const override {
1524  return StringOffsetSection;
1525  }
1526  StringRef getLineStringSection() const override { return LineStringSection; }
1527 
1528  // Sections for DWARF5 split dwarf proposal.
1529  const DWARFSection &getInfoDWOSection() const override {
1530  return InfoDWOSection;
1531  }
1532  void forEachTypesDWOSections(
1533  function_ref<void(const DWARFSection &)> F) const override {
1534  for (auto &P : TypesDWOSections)
1535  F(P.second);
1536  }
1537 
1538  StringRef getAbbrevSection() const override { return AbbrevSection; }
1539  const DWARFSection &getLocSection() const override { return LocSection; }
1540  StringRef getARangeSection() const override { return ARangeSection; }
1541  StringRef getDebugFrameSection() const override { return DebugFrameSection; }
1542  StringRef getEHFrameSection() const override { return EHFrameSection; }
1543  const DWARFSection &getLineSection() const override { return LineSection; }
1544  StringRef getStringSection() const override { return StringSection; }
1545  const DWARFSection &getRangeSection() const override { return RangeSection; }
1546  const DWARFSection &getRnglistsSection() const override {
1547  return RnglistsSection;
1548  }
1549  StringRef getMacinfoSection() const override { return MacinfoSection; }
1550  StringRef getPubNamesSection() const override { return PubNamesSection; }
1551  StringRef getPubTypesSection() const override { return PubTypesSection; }
1552  StringRef getGnuPubNamesSection() const override {
1553  return GnuPubNamesSection;
1554  }
1555  StringRef getGnuPubTypesSection() const override {
1556  return GnuPubTypesSection;
1557  }
1558  const DWARFSection &getAppleNamesSection() const override {
1559  return AppleNamesSection;
1560  }
1561  const DWARFSection &getAppleTypesSection() const override {
1562  return AppleTypesSection;
1563  }
1564  const DWARFSection &getAppleNamespacesSection() const override {
1565  return AppleNamespacesSection;
1566  }
1567  const DWARFSection &getAppleObjCSection() const override {
1568  return AppleObjCSection;
1569  }
1570  const DWARFSection &getDebugNamesSection() const override {
1571  return DebugNamesSection;
1572  }
1573 
1574  StringRef getFileName() const override { return FileName; }
1575  uint8_t getAddressSize() const override { return AddressSize; }
1576  const DWARFSection &getInfoSection() const override { return InfoSection; }
1577  void forEachTypesSections(
1578  function_ref<void(const DWARFSection &)> F) const override {
1579  for (auto &P : TypesSections)
1580  F(P.second);
1581  }
1582 };
1583 } // namespace
1584 
1585 std::unique_ptr<DWARFContext>
1587  function_ref<ErrorPolicy(Error)> HandleError,
1588  std::string DWPName) {
1589  auto DObj = llvm::make_unique<DWARFObjInMemory>(Obj, L, HandleError);
1590  return llvm::make_unique<DWARFContext>(std::move(DObj), std::move(DWPName));
1591 }
1592 
1593 std::unique_ptr<DWARFContext>
1594 DWARFContext::create(const StringMap<std::unique_ptr<MemoryBuffer>> &Sections,
1595  uint8_t AddrSize, bool isLittleEndian) {
1596  auto DObj =
1597  llvm::make_unique<DWARFObjInMemory>(Sections, AddrSize, isLittleEndian);
1598  return llvm::make_unique<DWARFContext>(std::move(DObj), "");
1599 }
1600 
1602  // Detect the architecture from the object file. We usually don't need OS
1603  // info to lookup a target and create register info.
1604  Triple TT;
1605  TT.setArch(Triple::ArchType(Obj.getArch()));
1608  std::string TargetLookupError;
1609  const Target *TheTarget =
1610  TargetRegistry::lookupTarget(TT.str(), TargetLookupError);
1611  if (!TargetLookupError.empty())
1612  return make_error<StringError>(TargetLookupError, inconvertibleErrorCode());
1613  RegInfo.reset(TheTarget->createMCRegInfo(TT.str()));
1614  return Error::success();
1615 }
1616 
1618  // In theory, different compile units may have different address byte
1619  // sizes, but for simplicity we just use the address byte size of the
1620  // last compile unit. In practice the address size field is repeated across
1621  // various DWARF headers (at least in version 5) to make it easier to dump
1622  // them independently, not to enable varying the address size.
1623  uint8_t Addr = 0;
1624  for (const auto &CU : compile_units()) {
1625  Addr = CU->getAddressByteSize();
1626  break;
1627  }
1628  return Addr;
1629 }
const DWARFUnitIndex & getTUIndex()
uint32_t StartLine
Definition: DIContext.h:37
static Expected< OwningBinary< ObjectFile > > createObjectFile(StringRef ObjectPath)
Definition: ObjectFile.cpp:153
void getInlinedChainForAddress(uint64_t Address, SmallVectorImpl< DWARFDie > &InlinedChain)
getInlinedChainForAddress - fetches inlined chain for a given address.
Definition: DWARFUnit.cpp:521
const DWARFDebugFrame * getEHFrame()
Get a pointer to the parsed eh frame information object.
void push_back(const T &Elt)
Definition: SmallVector.h:218
uint32_t Discriminator
Definition: DIContext.h:40
const DWARFDebugFrame * getDebugFrame()
Get a pointer to the parsed frame information object.
static void warn(Error Err)
Helper function for DWARFDebugLine parse functions, to report issues identified during parsing...
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
uint8_t[16] uuid_t
Output a formatted UUID with dash separators.
Definition: raw_ostream.h:219
std::string FileName
Definition: DIContext.h:32
A parsed .debug_frame or .eh_frame section.
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
static ContributionCollection collectContributionData(DWARFContext::cu_iterator_range CUs, DWARFContext::tu_section_iterator_range TUSs)
const DWARFDebugLocDWO * getDebugLocDWO()
Get a pointer to the parsed DebugLoc object.
Represents structure for holding and parsing .debug_pub* tables.
const DWARFDebugLoc * getDebugLoc()
Get a pointer to the parsed DebugLoc object.
virtual bool getLoadedSectionContents(const object::SectionRef &Sec, StringRef &Data) const
If conveniently available, return the content of the given Section.
Definition: DIContext.h:246
void dump(raw_ostream &OS, const MCRegisterInfo *RegInfo, Optional< uint64_t > Offset) const
Print the location lists found within the debug_loc section.
ErrorPolicy
Used as a return value for a error callback passed to DWARF context.
Definition: DWARFContext.h:54
bool addressRangeContainsAddress(const uint64_t Address) const
Definition: DWARFDie.cpp:425
void setVendor(VendorType Kind)
setVendor - Set the vendor (second) component of the triple to a known type.
Definition: Triple.cpp:1125
StringRef getFileName() const
Definition: Binary.cpp:41
const AppleAcceleratorTable & getAppleNamespaces()
Get a reference to the parsed accelerator table object.
void dump(raw_ostream &OS) const
DWARFDie getDIEForOffset(uint32_t Offset)
Get a DIE given an exact offset.
uint64_t Address
DWARFGdbIndex & getGdbIndex()
std::shared_ptr< DWARFContext > getDWOContext(StringRef AbsolutePath)
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
Definition: StringRef.h:138
static raw_ostream & error()
Convenience method for printing "error: " to stderr.
Definition: WithColor.cpp:63
uint32_t findAddress(uint64_t Address) const
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition: Format.h:124
bool handleDebugLine()
Verify the information in the .debug_line section.
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLExtras.h:106
uint8_t getCUAddrSize()
Get address size from CUs.
uint32_t Line
An unsigned integer indicating a source line number.
DIEsForAddress getDIEsForAddress(uint64_t Address)
Get the compilation unit, the function DIE and lexical block DIE for the given address where applicab...
static void dumpStringOffsetsSection(raw_ostream &OS, StringRef SectionName, const DWARFObject &Obj, const DWARFSection &StringOffsetsSection, StringRef StringSection, DWARFContext::cu_iterator_range CUs, DWARFContext::tu_section_iterator_range TUSs, bool LittleEndian, unsigned MaxVersion)
This class is the base class for all object file types.
Definition: ObjectFile.h:190
virtual std::string message() const
Return the error message as a string.
Definition: Error.h:56
static raw_ostream & warning()
Convenience method for printing "warning: " to stderr.
Definition: WithColor.cpp:65
This class implements a map that also provides access to all stored values in a deterministic order...
Definition: MapVector.h:38
const DWARFDebugLine::LineTable * getLineTableForUnit(DWARFUnit *U)
Get a pointer to a parsed line table corresponding to a compile unit.
unsigned getNumTypeUnits()
Get the number of compile units in this context.
Definition: DWARFContext.h:177
F(f)
void setOS(OSType Kind)
setOS - Set the operating system (third) component of the triple to a known type. ...
Definition: Triple.cpp:1129
void addFrame(const DILineInfo &Frame)
Definition: DIContext.h:98
unsigned getMaxVersion() const
Definition: DWARFContext.h:214
const DWARFDebugAbbrev * getDebugAbbrevDWO()
Get a pointer to the parsed dwo abbreviations object.
DINameKind
A DINameKind is passed to name search methods to specify a preference regarding the type of name reso...
Definition: DIContext.h:114
Error takeError()
Take ownership of the stored error.
Definition: Error.h:546
static const Target * lookupTarget(const std::string &Triple, std::string &Error)
lookupTarget - Lookup a target based on a target triple.
Base class for error info classes.
Definition: Error.h:48
std::unique_ptr< const DWARFObject > DObj
Definition: DWARFContext.h:114
bool handleDebugInfo()
Verify the information in the .debug_info section.
raw_ostream & write_uuid(const uuid_t UUID)
A format-neutral container for source line information.
Definition: DIContext.h:31
DWARFDie getSubroutineForAddress(uint64_t Address)
Returns subprogram DIE with address range encompassing the provided address.
Definition: DWARFUnit.cpp:506
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: DenseMap.h:191
Definition: BitVector.h:921
std::string toString(Error E)
Write all error messages (if any) in E to a string.
Definition: Error.h:956
DWARFCompileUnit * getCompileUnitForOffset(uint32_t Offset)
Return the compile unit that includes an offset (relative to .debug_info).
uint32_t getU32(uint32_t *offset_ptr) const
Extract a uint32_t value from *offset_ptr.
static bool getFunctionNameAndStartLineForAddress(DWARFCompileUnit *CU, uint64_t Address, FunctionNameKind Kind, std::string &FunctionName, uint32_t &StartLine)
static T & getAccelTable(std::unique_ptr< T > &Cache, const DWARFObject &Obj, const DWARFSection &Section, StringRef StringSection, bool IsLittleEndian)
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
DwarfFormat
Constants that define the DWARF format as 32 or 64 bit.
Definition: Dwarf.h:65
const char * getCompilationDir()
Definition: DWARFUnit.cpp:203
FunctionNameKind FNKind
Definition: DIContext.h:123
A class representing an address table as specified in DWARF v5.
unsigned getNumCompileUnits()
Get the number of compile units in this context.
Definition: DWARFContext.h:171
const DWARFSection & getLineSection() const
Definition: DWARFUnit.h:346
FileLineInfoKind FLIKind
Definition: DIContext.h:122
uint64_t Address
The program-counter value corresponding to a machine instruction generated by the compiler...
void getCallerFrame(uint32_t &CallFile, uint32_t &CallLine, uint32_t &CallColumn, uint32_t &CallDiscriminator) const
Retrieves values of DW_AT_call_file, DW_AT_call_line and DW_AT_call_column from DIE (or zeroes if the...
Definition: DWARFDie.cpp:463
virtual uint64_t getSectionLoadAddress(const object::SectionRef &Sec) const
Obtain the Load Address of a section by SectionRef.
Definition: DIContext.h:231
Tagged union holding either a T or a Error.
Definition: CachePruning.h:23
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE R Default(T Value)
Definition: StringSwitch.h:203
uint64_t Size
The contribution size not including the header.
Definition: DWARFUnit.h:235
const MCRegisterInfo * getRegisterInfo() const
Definition: DWARFContext.h:307
unsigned getNumDWOTypeUnits()
Get the number of compile units in the DWO context.
Definition: DWARFContext.h:189
uint8_t getAddressByteSize() const
Definition: DWARFUnit.h:338
content_iterator< SectionRef > section_iterator
Definition: ObjectFile.h:48
bool verify(raw_ostream &OS, DIDumpOptions DumpOpts={}) override
#define T
const T & getValue() const LLVM_LVALUE_FUNCTION
Definition: Optional.h:179
const DWARFDebugAranges * getDebugAranges()
Get a pointer to the parsed DebugAranges object.
static void dumpDWARFv5StringOffsetsSection(raw_ostream &OS, StringRef SectionName, const DWARFObject &Obj, const DWARFSection &StringOffsetsSection, StringRef StringSection, DWARFContext::cu_iterator_range CUs, DWARFContext::tu_section_iterator_range TUSs, bool LittleEndian)
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
const DWARFDebugAbbrev * getDebugAbbrev()
Get a pointer to the parsed DebugAbbrev object.
section_iterator_range sections() const
Definition: ObjectFile.h:278
DWARFCompileUnit * getCompileUnitAtIndex(unsigned index)
Get the compile unit at the specified index for this compile unit.
Definition: DWARFContext.h:195
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:598
const std::string & str() const
Definition: Triple.h:354
uint64_t getDeclLine() const
Returns the declaration line (start line) for a DIE, assuming it specifies a subprogram.
Definition: DWARFDie.cpp:459
Expected< section_iterator > getSection() const
Get section this symbol is defined in reference to.
Definition: ObjectFile.h:365
void dump(raw_ostream &OS, const MCRegisterInfo *MRI, Optional< uint64_t > Offset) const
Dump the section data into the given stream.
uint64_t SectionIndex
Error loadRegisterInfo(const object::ObjectFile &Obj)
Loads register info for the architecture of the provided object file.
Container for dump options that control which debug information will be dumped.
Definition: DIContext.h:154
virtual StringRef mapDebugSectionName(StringRef Name) const
Maps a debug section name to a standard DWARF section name.
Definition: ObjectFile.h:303
static Expected< StringRef > getFileName(const DebugStringTableSubsectionRef &Strings, const DebugChecksumsSubsectionRef &Checksums, uint32_t FileID)
Optional< uint64_t > toSectionOffset(const Optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract an section offset.
#define P(N)
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:43
Utility class that carries the DWARF compile/type unit and the debug info entry in an object...
Definition: DWARFDie.h:43
This implements the Apple accelerator table format, a precursor of the DWARF 5 accelerator table form...
* if(!EatIfPresent(lltok::kw_thread_local)) return false
ParseOptionalThreadLocal := /*empty.
uint32_t Column
Definition: DIContext.h:36
virtual uint8_t getBytesInAddress() const =0
The number of bytes used to represent an address in this object file format.
Controls which fields of DILineInfo container should be filled with data.
Definition: DIContext.h:118
DIDumpOptions noImplicitRecursion() const
Return the options with RecurseDepth set to 0 unless explicitly required.
Definition: DIContext.h:175
void dump(raw_ostream &OS, DIDumpOptions DumpOpts={}) const
static void dumpAddrSection(raw_ostream &OS, DWARFDataExtractor &AddrData, DIDumpOptions DumpOpts, uint16_t Version, uint8_t AddrSize)
uint16_t File
An unsigned integer indicating the identity of the source file corresponding to a machine instruction...
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
Definition: Error.cpp:78
const DWARFDebugMacro * getDebugMacro()
Get a pointer to the parsed DebugMacro object.
A format-neutral container for inlined code description.
Definition: DIContext.h:78
A structured debug information entry.
Definition: DIE.h:662
LLVM_NODISCARD size_t find_first_not_of(char C, size_t From=0) const
Find the first character in the string that is not C or npos if not found.
Definition: StringRef.cpp:250
void dump(raw_ostream &OS) const
bool handleDebugAbbrev()
Verify the information in any of the following sections, if available: .debug_abbrev, debug_abbrev.dwo.
void skip(function_ref< void(Error)> ErrorCallback=warn)
Skip the current line table and go to the following line table (if present) immediately.
static Error createError(const Twine &Reason, llvm::Error E)
DILineInfo getLineInfoForAddress(uint64_t Address, DILineInfoSpecifier Specifier=DILineInfoSpecifier()) override
static ErrorPolicy defaultErrorHandler(Error E)
Function used to handle default error reporting policy.
MCRegisterInfo * createMCRegInfo(StringRef TT) const
createMCRegInfo - Create a MCRegisterInfo implementation.
Error extract(DWARFDataExtractor Data, uint32_t *OffsetPtr, uint16_t Version, uint8_t AddrSize, std::function< void(Error)> WarnCallback)
Extract an entire table, including all addresses.
void dump(raw_ostream &OS)
virtual basic_symbol_iterator symbol_end() const =0
void dump(raw_ostream &OS, const MCRegisterInfo *RegInfo, Optional< uint64_t > Offset) const
static void dumpUUID(raw_ostream &OS, const ObjectFile &Obj)
Dump the UUID load command.
static bool isRelocScattered(const object::ObjectFile &Obj, const RelocationRef &Reloc)
void dump(raw_ostream &OS) const
Print the macro list found within the debug_macinfo section.
StringRef toStringRef(bool B)
Construct a string ref from a boolean.
Definition: StringExtras.h:53
Expected< uint64_t > getAddress() const
Returns the symbol virtual address (i.e.
Definition: ObjectFile.h:349
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:971
size_t size() const
Definition: SmallVector.h:53
LLVM_NODISCARD char back() const
back - Get the last character in the string.
Definition: StringRef.h:149
auto find(R &&Range, const T &Val) -> decltype(adl_begin(Range))
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:942
void dump(raw_ostream &OS, DIDumpOptions DumpOptions) const
bool isLittleEndian() const
Definition: Binary.h:131
uint64_t getRelocatedValue(uint32_t Size, uint32_t *Off, uint64_t *SectionIndex=nullptr) const
Extracts a value and applies a relocation to the result if one exists for the given offset...
const AppleAcceleratorTable & getAppleObjC()
Get a reference to the parsed accelerator table object.
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
Standard .debug_line state machine structure.
const AppleAcceleratorTable & getAppleTypes()
Get a reference to the parsed accelerator table object.
static std::unique_ptr< DWARFContext > create(const object::ObjectFile &Obj, const LoadedObjectInfo *L=nullptr, function_ref< ErrorPolicy(Error)> HandleError=defaultErrorHandler, std::string DWPName="")
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:872
A DataExtractor (typically for an in-memory copy of an object-file section) plus a relocation map for...
bool lookupAddressRange(uint64_t Address, uint64_t Size, std::vector< uint32_t > &Result) const
.debug_names section consists of one or more units.
DWARFCompileUnit * getDWOCompileUnitForHash(uint64_t Hash)
void dump(raw_ostream &OS, DIDumpOptions DumpOpts={}) const
void handleAllErrors(Error E, HandlerTs &&... Handlers)
Behaves the same as handleErrors, except that by contract all errors must be handled by the given han...
Definition: Error.h:898
uint16_t Column
An unsigned integer indicating a column number within a source line.
DWARFDie getUnitDIE(bool ExtractUnitDIEOnly=true)
Definition: DWARFUnit.h:438
StringRef getArchName() const
getArchName - Get the architecture (first) component of the triple.
Definition: Triple.cpp:942
raw_ostream & write_escaped(StringRef Str, bool UseHexEscapes=false)
Output Str, turning &#39;\&#39;, &#39;&#39;, &#39; &#39;, &#39;"&#39;, and anything that doesn&#39;t satisfy llvm::isPrint into an escape...
static ErrorSuccess success()
Create a success value.
Definition: Error.h:322
uint32_t getLength() const
Returns the length of this table, including the length field, or 0 if the length has not been determi...
auto size(R &&Range, typename std::enable_if< std::is_same< typename std::iterator_traits< decltype(Range.begin())>::iterator_category, std::random_access_iterator_tag >::value, void >::type *=nullptr) -> decltype(std::distance(Range.begin(), Range.end()))
Get the size of a range.
Definition: STLExtras.h:1045
bool getFileLineInfoForAddress(uint64_t Address, const char *CompDir, DILineInfoSpecifier::FileLineInfoKind Kind, DILineInfo &Result) const
Fills the Result argument with the file and line information corresponding to Address.
uint32_t Line
Definition: DIContext.h:35
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:847
DIInliningInfo getInliningInfoForAddress(uint64_t Address, DILineInfoSpecifier Specifier=DILineInfoSpecifier()) override
void dump(raw_ostream &OS) const override
static Expected< SymInfo > getSymbolInfo(const object::ObjectFile &Obj, const RelocationRef &Reloc, const LoadedObjectInfo *L, std::map< SymbolRef, SymInfo > &Cache)
Returns the address of symbol relocation used against and a section index.
std::string FunctionName
Definition: DIContext.h:33
virtual Triple::ArchType getArch() const =0
DWARFContext This data structure is the top level entity that deals with dwarf debug information pars...
Definition: DWARFContext.h:59
tu_section_iterator_range dwo_type_unit_sections()
Get type units in the DWO context.
Definition: DWARFContext.h:165
A range adaptor for a pair of iterators.
StringMap - This is an unconventional map that is specialized for handling keys that are "strings"...
Definition: StringMap.h:220
This file contains constants used for implementing Dwarf debug support.
Target - Wrapper for Target specific information.
An inferface for inquiring the load address of a loaded object file to be used by the DIContext imple...
Definition: DIContext.h:216
loop extract
LLVM_ATTRIBUTE_ALWAYS_INLINE StringSwitch & Case(StringLiteral S, T Value)
Definition: StringSwitch.h:70
void dump(raw_ostream &OS) const
#define Success
static void dumpRnglistsSection(raw_ostream &OS, DWARFDataExtractor &rnglistData, DIDumpOptions DumpOpts)
DILineInfoTable getLineInfoForAddressRange(uint64_t Address, uint64_t Size, DILineInfoSpecifier Specifier=DILineInfoSpecifier()) override
RelocAddrEntry contains relocated value and section index.
Definition: DWARFRelocMap.h:20
const DWARFDebugNames & getDebugNames()
Get a reference to the parsed accelerator table object.
A class that verifies DWARF debug information given a DWARF Context.
Definition: DWARFVerifier.h:35
virtual section_iterator section_end() const =0
dwarf::Tag getTag() const
Definition: DWARFDie.h:72
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:56
const AppleAcceleratorTable & getAppleNames()
Get a reference to the parsed accelerator table object.
Wraps the returned DIEs for a given address.
Definition: DWARFContext.h:282
bool handleAccelTables()
Verify the information in accelerator tables, if they exist.
const char * getCStr(uint32_t *offset_ptr) const
Extract a C string from *offset_ptr.
cu_iterator_range compile_units()
Get compile units in this context.
Definition: DWARFContext.h:147
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:323
void dump(raw_ostream &OS, DIDumpOptions DumpOpts, std::array< Optional< uint64_t >, DIDT_ID_Count > DumpOffsets)
Dump a textual representation to OS.
DWARFContext(std::unique_ptr< const DWARFObject > DObj, std::string DWPName="")
bool isLittleEndian() const
Definition: DWARFContext.h:300
bool isValidOffset(uint32_t offset) const
Test the validity of offset.
const DWARFUnitIndex & getCUIndex()
void dump(raw_ostream &OS) const override
SymInfo contains information about symbol: it&#39;s address and section index which is -1LL for absolute ...
DINameKind FunctionNameKind
Definition: DIContext.h:120
Error extract(DWARFDataExtractor Data, uint32_t *OffsetPtr)
Extract an entire table, including all list entries.
cu_iterator_range dwo_compile_units()
Get compile units in the DWO context.
Definition: DWARFContext.h:159
LineTable parseNext(function_ref< void(Error)> RecoverableErrorCallback=warn, function_ref< void(Error)> UnrecoverableErrorCallback=warn, raw_ostream *OS=nullptr)
Get the next line table from the section.
bool getFileNameByIndex(uint64_t FileIndex, const char *CompDir, DILineInfoSpecifier::FileLineInfoKind Kind, std::string &Result) const
Extracts filename by its index in filename table in prologue.
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E&#39;s largest value.
Definition: BitmaskEnum.h:81
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
print Print MemDeps of function
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
bool hasValidLength() const
Verify that the given length is valid for this table.
tu_section_iterator_range type_unit_sections()
Get type units in this context.
Definition: DWARFContext.h:153
Helper to allow for parsing of an entire .debug_line section in sequence.
const uint64_t Version
Definition: InstrProf.h:895
bool done() const
Indicates if the parser has parsed as much as possible.
uint64_t visit(uint32_t Rel, RelocationRef R, uint64_t Value=0)
Definition: RelocVisitor.h:43
uint32_t getLineTableOffset() const
Definition: DWARFUnit.h:532
const char * getSubroutineName(DINameKind Kind) const
If a DIE represents a subprogram (or inlined subroutine), returns its mangled name (or short name...
Definition: DWARFDie.cpp:438
void setArch(ArchType Kind)
setArch - Set the architecture (first) component of the triple to a known type.
Definition: Triple.cpp:1121
Optional< uint64_t > toUnsigned(const Optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract an unsigned constant.
uint32_t getOffset() const
Get the offset the parser has reached.
Error extract(const DWARFDataExtractor &data, uint32_t *offset_ptr)
This is a value type class that represents a single section in the list of sections in the object fil...
Definition: ObjectFile.h:81
std::vector< Optional< StrOffsetsContributionDescriptor > > ContributionCollection
Base class for object file relocation visitors.
Definition: RelocVisitor.h:36
StringRef extension(StringRef path, Style style=Style::native)
Get extension.
Definition: Path.cpp:596
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition: Error.cpp:73