DwarfFile.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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/MC/MCStreamer.h"
  13. #include "llvm/Support/LEB128.h"
  14. #include "llvm/IR/DataLayout.h"
  15. #include "llvm/ADT/STLExtras.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. // Emit the compile units header.
  49. Asm->OutStreamer.EmitLabel(TheU->getLabelBegin());
  50. // Emit size of content not including length itself
  51. Asm->OutStreamer.AddComment("Length of Unit");
  52. Asm->EmitInt32(TheU->getHeaderSize() + Die.getSize());
  53. TheU->emitHeader(ASectionSym);
  54. DD.emitDIE(Die);
  55. Asm->OutStreamer.EmitLabel(TheU->getLabelEnd());
  56. }
  57. }
  58. // Compute the size and offset for each DIE.
  59. void DwarfFile::computeSizeAndOffsets() {
  60. // Offset from the first CU in the debug info section is 0 initially.
  61. unsigned SecOffset = 0;
  62. // Iterate over each compile unit and set the size and offsets for each
  63. // DIE within each compile unit. All offsets are CU relative.
  64. for (const auto &TheU : CUs) {
  65. TheU->setDebugInfoOffset(SecOffset);
  66. // CU-relative offset is reset to 0 here.
  67. unsigned Offset = sizeof(int32_t) + // Length of Unit Info
  68. TheU->getHeaderSize(); // Unit-specific headers
  69. // EndOffset here is CU-relative, after laying out
  70. // all of the CU DIE.
  71. unsigned EndOffset = computeSizeAndOffset(TheU->getUnitDie(), Offset);
  72. SecOffset += EndOffset;
  73. }
  74. }
  75. // Compute the size and offset of a DIE. The offset is relative to start of the
  76. // CU. It returns the offset after laying out the DIE.
  77. unsigned DwarfFile::computeSizeAndOffset(DIE &Die, unsigned Offset) {
  78. // Record the abbreviation.
  79. assignAbbrevNumber(Die.getAbbrev());
  80. // Get the abbreviation for this DIE.
  81. const DIEAbbrev &Abbrev = Die.getAbbrev();
  82. // Set DIE offset
  83. Die.setOffset(Offset);
  84. // Start the size with the size of abbreviation code.
  85. Offset += getULEB128Size(Die.getAbbrevNumber());
  86. const SmallVectorImpl<DIEValue *> &Values = Die.getValues();
  87. const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
  88. // Size the DIE attribute values.
  89. for (unsigned i = 0, N = Values.size(); i < N; ++i)
  90. // Size attribute value.
  91. Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
  92. // Get the children.
  93. const auto &Children = Die.getChildren();
  94. // Size the DIE children if any.
  95. if (!Children.empty()) {
  96. assert(Abbrev.hasChildren() && "Children flag not set");
  97. for (auto &Child : Children)
  98. Offset = computeSizeAndOffset(*Child, Offset);
  99. // End of children marker.
  100. Offset += sizeof(int8_t);
  101. }
  102. Die.setSize(Offset - Die.getOffset());
  103. return Offset;
  104. }
  105. void DwarfFile::emitAbbrevs(const MCSection *Section) {
  106. // Check to see if it is worth the effort.
  107. if (!Abbreviations.empty()) {
  108. // Start the debug abbrev section.
  109. Asm->OutStreamer.SwitchSection(Section);
  110. // For each abbrevation.
  111. for (const DIEAbbrev *Abbrev : Abbreviations) {
  112. // Emit the abbrevations code (base 1 index.)
  113. Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
  114. // Emit the abbreviations data.
  115. Abbrev->Emit(Asm);
  116. }
  117. // Mark end of abbreviations.
  118. Asm->EmitULEB128(0, "EOM(3)");
  119. }
  120. }
  121. // Emit strings into a string section.
  122. void DwarfFile::emitStrings(const MCSection *StrSection,
  123. const MCSection *OffsetSection) {
  124. StrPool.emit(*Asm, StrSection, OffsetSection);
  125. }
  126. // If Var is a current function argument then add it to CurrentFnArguments list.
  127. bool DwarfFile::addCurrentFnArgument(DbgVariable *Var, LexicalScope *Scope) {
  128. if (Scope->getParent())
  129. return false;
  130. DIVariable DV = Var->getVariable();
  131. if (DV.getTag() != dwarf::DW_TAG_arg_variable)
  132. return false;
  133. unsigned ArgNo = DV.getArgNumber();
  134. if (ArgNo == 0)
  135. return false;
  136. auto &CurrentFnArguments = DD.getCurrentFnArguments();
  137. // llvm::Function argument size is not good indicator of how many
  138. // arguments does the function have at source level.
  139. if (ArgNo > CurrentFnArguments.size())
  140. CurrentFnArguments.resize(ArgNo * 2);
  141. assert(!CurrentFnArguments[ArgNo - 1]);
  142. CurrentFnArguments[ArgNo - 1] = Var;
  143. return true;
  144. }
  145. void DwarfFile::addNonArgumentScopeVariable(LexicalScope *LS,
  146. DbgVariable *Var) {
  147. SmallVectorImpl<DbgVariable *> &Vars = DD.getScopeVariables()[LS];
  148. DIVariable DV = Var->getVariable();
  149. // Variables with positive arg numbers are parameters.
  150. if (unsigned ArgNum = DV.getArgNumber()) {
  151. // Keep all parameters in order at the start of the variable list to ensure
  152. // function types are correct (no out-of-order parameters)
  153. //
  154. // This could be improved by only doing it for optimized builds (unoptimized
  155. // builds have the right order to begin with), searching from the back (this
  156. // would catch the unoptimized case quickly), or doing a binary search
  157. // rather than linear search.
  158. auto I = Vars.begin();
  159. while (I != Vars.end()) {
  160. unsigned CurNum = (*I)->getVariable().getArgNumber();
  161. // A local (non-parameter) variable has been found, insert immediately
  162. // before it.
  163. if (CurNum == 0)
  164. break;
  165. // A later indexed parameter has been found, insert immediately before it.
  166. if (CurNum > ArgNum)
  167. break;
  168. ++I;
  169. }
  170. Vars.insert(I, Var);
  171. return;
  172. }
  173. Vars.push_back(Var);
  174. }
  175. }