DwarfFile.cpp 4.3 KB

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