Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 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 { |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 21 | DwarfFile::DwarfFile(AsmPrinter *AP, DwarfDebug &DD, StringRef Pref, |
| 22 | BumpPtrAllocator &DA) |
| 23 | : Asm(AP), DD(DD), StrPool(DA, *Asm, Pref) {} |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 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. |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 52 | void DwarfFile::emitUnits(const MCSymbol *ASectionSym) { |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 53 | for (const auto &TheU : CUs) { |
| 54 | DIE &Die = TheU->getUnitDie(); |
| 55 | const MCSection *USection = TheU->getSection(); |
| 56 | Asm->OutStreamer.SwitchSection(USection); |
| 57 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 58 | TheU->emitHeader(ASectionSym); |
| 59 | |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 60 | DD.emitDIE(Die); |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 61 | } |
| 62 | } |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 63 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 64 | // Compute the size and offset for each DIE. |
| 65 | void DwarfFile::computeSizeAndOffsets() { |
| 66 | // Offset from the first CU in the debug info section is 0 initially. |
| 67 | unsigned SecOffset = 0; |
| 68 | |
| 69 | // Iterate over each compile unit and set the size and offsets for each |
| 70 | // DIE within each compile unit. All offsets are CU relative. |
| 71 | for (const auto &TheU : CUs) { |
| 72 | TheU->setDebugInfoOffset(SecOffset); |
| 73 | |
| 74 | // CU-relative offset is reset to 0 here. |
| 75 | unsigned Offset = sizeof(int32_t) + // Length of Unit Info |
| 76 | TheU->getHeaderSize(); // Unit-specific headers |
| 77 | |
| 78 | // EndOffset here is CU-relative, after laying out |
| 79 | // all of the CU DIE. |
| 80 | unsigned EndOffset = computeSizeAndOffset(TheU->getUnitDie(), Offset); |
| 81 | SecOffset += EndOffset; |
| 82 | } |
| 83 | } |
| 84 | // Compute the size and offset of a DIE. The offset is relative to start of the |
| 85 | // CU. It returns the offset after laying out the DIE. |
| 86 | unsigned DwarfFile::computeSizeAndOffset(DIE &Die, unsigned Offset) { |
| 87 | // Record the abbreviation. |
| 88 | assignAbbrevNumber(Die.getAbbrev()); |
| 89 | |
| 90 | // Get the abbreviation for this DIE. |
| 91 | const DIEAbbrev &Abbrev = Die.getAbbrev(); |
| 92 | |
| 93 | // Set DIE offset |
| 94 | Die.setOffset(Offset); |
| 95 | |
| 96 | // Start the size with the size of abbreviation code. |
| 97 | Offset += getULEB128Size(Die.getAbbrevNumber()); |
| 98 | |
| 99 | const SmallVectorImpl<DIEValue *> &Values = Die.getValues(); |
| 100 | const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData(); |
| 101 | |
| 102 | // Size the DIE attribute values. |
| 103 | for (unsigned i = 0, N = Values.size(); i < N; ++i) |
| 104 | // Size attribute value. |
| 105 | Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm()); |
| 106 | |
| 107 | // Get the children. |
| 108 | const auto &Children = Die.getChildren(); |
| 109 | |
| 110 | // Size the DIE children if any. |
| 111 | if (!Children.empty()) { |
| 112 | assert(Abbrev.hasChildren() && "Children flag not set"); |
| 113 | |
| 114 | for (auto &Child : Children) |
| 115 | Offset = computeSizeAndOffset(*Child, Offset); |
| 116 | |
| 117 | // End of children marker. |
| 118 | Offset += sizeof(int8_t); |
| 119 | } |
| 120 | |
| 121 | Die.setSize(Offset - Die.getOffset()); |
| 122 | return Offset; |
| 123 | } |
| 124 | void DwarfFile::emitAbbrevs(const MCSection *Section) { |
| 125 | // Check to see if it is worth the effort. |
| 126 | if (!Abbreviations.empty()) { |
| 127 | // Start the debug abbrev section. |
| 128 | Asm->OutStreamer.SwitchSection(Section); |
| 129 | |
| 130 | // For each abbrevation. |
| 131 | for (const DIEAbbrev *Abbrev : Abbreviations) { |
| 132 | // Emit the abbrevations code (base 1 index.) |
| 133 | Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code"); |
| 134 | |
| 135 | // Emit the abbreviations data. |
| 136 | Abbrev->Emit(Asm); |
| 137 | } |
| 138 | |
| 139 | // Mark end of abbreviations. |
| 140 | Asm->EmitULEB128(0, "EOM(3)"); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | // Emit strings into a string section. |
| 145 | void DwarfFile::emitStrings(const MCSection *StrSection, |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame^] | 146 | const MCSection *OffsetSection) { |
| 147 | StrPool.emit(*Asm, StrSection, OffsetSection); |
| 148 | } |
| 149 | |
| 150 | void DwarfFile::addScopeVariable(LexicalScope *LS, DbgVariable *Var) { |
| 151 | SmallVectorImpl<DbgVariable *> &Vars = ScopeVariables[LS]; |
| 152 | DIVariable DV = Var->getVariable(); |
| 153 | // Variables with positive arg numbers are parameters. |
| 154 | if (unsigned ArgNum = DV.getArgNumber()) { |
| 155 | // Keep all parameters in order at the start of the variable list to ensure |
| 156 | // function types are correct (no out-of-order parameters) |
| 157 | // |
| 158 | // This could be improved by only doing it for optimized builds (unoptimized |
| 159 | // builds have the right order to begin with), searching from the back (this |
| 160 | // would catch the unoptimized case quickly), or doing a binary search |
| 161 | // rather than linear search. |
| 162 | auto I = Vars.begin(); |
| 163 | while (I != Vars.end()) { |
| 164 | unsigned CurNum = (*I)->getVariable().getArgNumber(); |
| 165 | // A local (non-parameter) variable has been found, insert immediately |
| 166 | // before it. |
| 167 | if (CurNum == 0) |
| 168 | break; |
| 169 | // A later indexed parameter has been found, insert immediately before it. |
| 170 | if (CurNum > ArgNum) |
| 171 | break; |
| 172 | // FIXME: There are still some cases where two inlined functions are |
| 173 | // conflated together (two calls to the same function at the same |
| 174 | // location (eg: via a macro, or without column info, etc)) and then |
| 175 | // their arguments are conflated as well. |
| 176 | assert((LS->getParent() || CurNum != ArgNum) && |
| 177 | "Duplicate argument for top level (non-inlined) function"); |
| 178 | ++I; |
| 179 | } |
| 180 | Vars.insert(I, Var); |
| 181 | return; |
| 182 | } |
| 183 | |
| 184 | Vars.push_back(Var); |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 185 | } |
| 186 | } |