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