DwarfFile.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //===- llvm/CodeGen/DwarfFile.cpp - Dwarf Debug Framework -----------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "DwarfFile.h"
  9. #include "DwarfCompileUnit.h"
  10. #include "DwarfDebug.h"
  11. #include "DwarfUnit.h"
  12. #include "llvm/CodeGen/AsmPrinter.h"
  13. #include "llvm/IR/DebugInfoMetadata.h"
  14. #include "llvm/MC/MCStreamer.h"
  15. #include <cstdint>
  16. using namespace llvm;
  17. DwarfFile::DwarfFile(AsmPrinter *AP, StringRef Pref, BumpPtrAllocator &DA)
  18. : Asm(AP), Abbrevs(AbbrevAllocator), StrPool(DA, *Asm, Pref) {}
  19. void DwarfFile::addUnit(std::unique_ptr<DwarfCompileUnit> U) {
  20. CUs.push_back(std::move(U));
  21. }
  22. // Emit the various dwarf units to the unit section USection with
  23. // the abbreviations going into ASection.
  24. void DwarfFile::emitUnits(bool UseOffsets) {
  25. for (const auto &TheU : CUs)
  26. emitUnit(TheU.get(), UseOffsets);
  27. }
  28. void DwarfFile::emitUnit(DwarfUnit *TheU, bool UseOffsets) {
  29. if (TheU->getCUNode()->isDebugDirectivesOnly())
  30. return;
  31. MCSection *S = TheU->getSection();
  32. if (!S)
  33. return;
  34. // Skip CUs that ended up not being needed (split CUs that were abandoned
  35. // because they added no information beyond the non-split CU)
  36. if (TheU->getUnitDie().values().empty())
  37. return;
  38. Asm->OutStreamer->switchSection(S);
  39. TheU->emitHeader(UseOffsets);
  40. Asm->emitDwarfDIE(TheU->getUnitDie());
  41. if (MCSymbol *EndLabel = TheU->getEndLabel())
  42. Asm->OutStreamer->emitLabel(EndLabel);
  43. }
  44. // Compute the size and offset for each DIE.
  45. void DwarfFile::computeSizeAndOffsets() {
  46. // Offset from the first CU in the debug info section is 0 initially.
  47. uint64_t SecOffset = 0;
  48. // Iterate over each compile unit and set the size and offsets for each
  49. // DIE within each compile unit. All offsets are CU relative.
  50. for (const auto &TheU : CUs) {
  51. if (TheU->getCUNode()->isDebugDirectivesOnly())
  52. continue;
  53. // Skip CUs that ended up not being needed (split CUs that were abandoned
  54. // because they added no information beyond the non-split CU)
  55. if (TheU->getUnitDie().values().empty())
  56. return;
  57. TheU->setDebugSectionOffset(SecOffset);
  58. SecOffset += computeSizeAndOffsetsForUnit(TheU.get());
  59. }
  60. if (SecOffset > UINT32_MAX && !Asm->isDwarf64())
  61. report_fatal_error("The generated debug information is too large "
  62. "for the 32-bit DWARF format.");
  63. }
  64. unsigned DwarfFile::computeSizeAndOffsetsForUnit(DwarfUnit *TheU) {
  65. // CU-relative offset is reset to 0 here.
  66. unsigned Offset = Asm->getUnitLengthFieldByteSize() + // Length of Unit Info
  67. TheU->getHeaderSize(); // Unit-specific headers
  68. // The return value here is CU-relative, after laying out
  69. // all of the CU DIE.
  70. return computeSizeAndOffset(TheU->getUnitDie(), Offset);
  71. }
  72. // Compute the size and offset of a DIE. The offset is relative to start of the
  73. // CU. It returns the offset after laying out the DIE.
  74. unsigned DwarfFile::computeSizeAndOffset(DIE &Die, unsigned Offset) {
  75. return Die.computeOffsetsAndAbbrevs(Asm->getDwarfFormParams(), Abbrevs,
  76. Offset);
  77. }
  78. void DwarfFile::emitAbbrevs(MCSection *Section) { Abbrevs.Emit(Asm, Section); }
  79. // Emit strings into a string section.
  80. void DwarfFile::emitStrings(MCSection *StrSection, MCSection *OffsetSection,
  81. bool UseRelativeOffsets) {
  82. StrPool.emit(*Asm, StrSection, OffsetSection, UseRelativeOffsets);
  83. }
  84. bool DwarfFile::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
  85. auto &ScopeVars = ScopeVariables[LS];
  86. const DILocalVariable *DV = Var->getVariable();
  87. if (unsigned ArgNum = DV->getArg()) {
  88. auto Cached = ScopeVars.Args.find(ArgNum);
  89. if (Cached == ScopeVars.Args.end())
  90. ScopeVars.Args[ArgNum] = Var;
  91. else {
  92. Cached->second->addMMIEntry(*Var);
  93. return false;
  94. }
  95. } else {
  96. ScopeVars.Locals.push_back(Var);
  97. }
  98. return true;
  99. }
  100. void DwarfFile::addScopeLabel(LexicalScope *LS, DbgLabel *Label) {
  101. SmallVectorImpl<DbgLabel *> &Labels = ScopeLabels[LS];
  102. Labels.push_back(Label);
  103. }
  104. std::pair<uint32_t, RangeSpanList *>
  105. DwarfFile::addRange(const DwarfCompileUnit &CU, SmallVector<RangeSpan, 2> R) {
  106. CURangeLists.push_back(
  107. RangeSpanList{Asm->createTempSymbol("debug_ranges"), &CU, std::move(R)});
  108. return std::make_pair(CURangeLists.size() - 1, &CURangeLists.back());
  109. }