DwarfFile.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. //===-- llvm/CodeGen/DwarfFile.cpp - Dwarf Debug Framework ----------------===//
  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. #include "DwarfFile.h"
  10. #include "DwarfDebug.h"
  11. #include "DwarfUnit.h"
  12. #include "llvm/ADT/STLExtras.h"
  13. #include "llvm/IR/DataLayout.h"
  14. #include "llvm/MC/MCStreamer.h"
  15. #include "llvm/Support/LEB128.h"
  16. #include "llvm/Target/TargetLoweringObjectFile.h"
  17. namespace llvm {
  18. DwarfFile::DwarfFile(AsmPrinter *AP, DwarfDebug &DD, StringRef Pref,
  19. BumpPtrAllocator &DA)
  20. : Asm(AP), DD(DD), StrPool(DA, *Asm, Pref) {}
  21. DwarfFile::~DwarfFile() {}
  22. // Define a unique number for the abbreviation.
  23. //
  24. void DwarfFile::assignAbbrevNumber(DIEAbbrev &Abbrev) {
  25. // Check the set for priors.
  26. DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
  27. // If it's newly added.
  28. if (InSet == &Abbrev) {
  29. // Add to abbreviation list.
  30. Abbreviations.push_back(&Abbrev);
  31. // Assign the vector position + 1 as its number.
  32. Abbrev.setNumber(Abbreviations.size());
  33. } else {
  34. // Assign existing abbreviation number.
  35. Abbrev.setNumber(InSet->getNumber());
  36. }
  37. }
  38. void DwarfFile::addUnit(std::unique_ptr<DwarfUnit> U) {
  39. CUs.push_back(std::move(U));
  40. }
  41. // Emit the various dwarf units to the unit section USection with
  42. // the abbreviations going into ASection.
  43. void DwarfFile::emitUnits(const MCSymbol *ASectionSym) {
  44. for (const auto &TheU : CUs) {
  45. DIE &Die = TheU->getUnitDie();
  46. const MCSection *USection = TheU->getSection();
  47. Asm->OutStreamer.SwitchSection(USection);
  48. TheU->emitHeader(ASectionSym);
  49. DD.emitDIE(Die);
  50. }
  51. }
  52. // Compute the size and offset for each DIE.
  53. void DwarfFile::computeSizeAndOffsets() {
  54. // Offset from the first CU in the debug info section is 0 initially.
  55. unsigned SecOffset = 0;
  56. // Iterate over each compile unit and set the size and offsets for each
  57. // DIE within each compile unit. All offsets are CU relative.
  58. for (const auto &TheU : CUs) {
  59. TheU->setDebugInfoOffset(SecOffset);
  60. // CU-relative offset is reset to 0 here.
  61. unsigned Offset = sizeof(int32_t) + // Length of Unit Info
  62. TheU->getHeaderSize(); // Unit-specific headers
  63. // EndOffset here is CU-relative, after laying out
  64. // all of the CU DIE.
  65. unsigned EndOffset = computeSizeAndOffset(TheU->getUnitDie(), Offset);
  66. SecOffset += EndOffset;
  67. }
  68. }
  69. // Compute the size and offset of a DIE. The offset is relative to start of the
  70. // CU. It returns the offset after laying out the DIE.
  71. unsigned DwarfFile::computeSizeAndOffset(DIE &Die, unsigned Offset) {
  72. // Record the abbreviation.
  73. assignAbbrevNumber(Die.getAbbrev());
  74. // Get the abbreviation for this DIE.
  75. const DIEAbbrev &Abbrev = Die.getAbbrev();
  76. // Set DIE offset
  77. Die.setOffset(Offset);
  78. // Start the size with the size of abbreviation code.
  79. Offset += getULEB128Size(Die.getAbbrevNumber());
  80. const SmallVectorImpl<DIEValue *> &Values = Die.getValues();
  81. const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
  82. // Size the DIE attribute values.
  83. for (unsigned i = 0, N = Values.size(); i < N; ++i)
  84. // Size attribute value.
  85. Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
  86. // Get the children.
  87. const auto &Children = Die.getChildren();
  88. // Size the DIE children if any.
  89. if (!Children.empty()) {
  90. assert(Abbrev.hasChildren() && "Children flag not set");
  91. for (auto &Child : Children)
  92. Offset = computeSizeAndOffset(*Child, Offset);
  93. // End of children marker.
  94. Offset += sizeof(int8_t);
  95. }
  96. Die.setSize(Offset - Die.getOffset());
  97. return Offset;
  98. }
  99. void DwarfFile::emitAbbrevs(const MCSection *Section) {
  100. // Check to see if it is worth the effort.
  101. if (!Abbreviations.empty()) {
  102. // Start the debug abbrev section.
  103. Asm->OutStreamer.SwitchSection(Section);
  104. // For each abbrevation.
  105. for (const DIEAbbrev *Abbrev : Abbreviations) {
  106. // Emit the abbrevations code (base 1 index.)
  107. Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
  108. // Emit the abbreviations data.
  109. Abbrev->Emit(Asm);
  110. }
  111. // Mark end of abbreviations.
  112. Asm->EmitULEB128(0, "EOM(3)");
  113. }
  114. }
  115. // Emit strings into a string section.
  116. void DwarfFile::emitStrings(const MCSection *StrSection,
  117. const MCSection *OffsetSection) {
  118. StrPool.emit(*Asm, StrSection, OffsetSection);
  119. }
  120. void DwarfFile::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
  121. SmallVectorImpl<DbgVariable *> &Vars = ScopeVariables[LS];
  122. DIVariable DV = Var->getVariable();
  123. // Variables with positive arg numbers are parameters.
  124. if (unsigned ArgNum = DV.getArgNumber()) {
  125. // Keep all parameters in order at the start of the variable list to ensure
  126. // function types are correct (no out-of-order parameters)
  127. //
  128. // This could be improved by only doing it for optimized builds (unoptimized
  129. // builds have the right order to begin with), searching from the back (this
  130. // would catch the unoptimized case quickly), or doing a binary search
  131. // rather than linear search.
  132. auto I = Vars.begin();
  133. while (I != Vars.end()) {
  134. unsigned CurNum = (*I)->getVariable().getArgNumber();
  135. // A local (non-parameter) variable has been found, insert immediately
  136. // before it.
  137. if (CurNum == 0)
  138. break;
  139. // A later indexed parameter has been found, insert immediately before it.
  140. if (CurNum > ArgNum)
  141. break;
  142. // FIXME: There are still some cases where two inlined functions are
  143. // conflated together (two calls to the same function at the same
  144. // location (eg: via a macro, or without column info, etc)) and then
  145. // their arguments are conflated as well.
  146. assert((LS->getParent() || CurNum != ArgNum) &&
  147. "Duplicate argument for top level (non-inlined) function");
  148. ++I;
  149. }
  150. Vars.insert(I, Var);
  151. return;
  152. }
  153. Vars.push_back(Var);
  154. }
  155. }