DwarfFile.cpp 4.5 KB

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