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