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