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" |
| 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 { |
David Blaikie | 2b22b1e | 2014-10-23 00:16:03 +0000 | [diff] [blame] | 21 | DwarfFile::DwarfFile(AsmPrinter *AP, DwarfDebug &DD, StringRef Pref, |
| 22 | BumpPtrAllocator &DA) |
| 23 | : Asm(AP), DD(DD), StrPool(DA, *Asm, Pref) { |
| 24 | (void)this->DD; |
| 25 | } |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 26 | |
David Blaikie | 05e736f | 2014-04-23 19:44:08 +0000 | [diff] [blame] | 27 | DwarfFile::~DwarfFile() {} |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 28 | |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 29 | // Define a unique number for the abbreviation. |
| 30 | // |
| 31 | void DwarfFile::assignAbbrevNumber(DIEAbbrev &Abbrev) { |
| 32 | // Check the set for priors. |
| 33 | DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev); |
| 34 | |
| 35 | // If it's newly added. |
| 36 | if (InSet == &Abbrev) { |
| 37 | // Add to abbreviation list. |
| 38 | Abbreviations.push_back(&Abbrev); |
| 39 | |
| 40 | // Assign the vector position + 1 as its number. |
| 41 | Abbrev.setNumber(Abbreviations.size()); |
| 42 | } else { |
| 43 | // Assign existing abbreviation number. |
| 44 | Abbrev.setNumber(InSet->getNumber()); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | void DwarfFile::addUnit(std::unique_ptr<DwarfUnit> U) { |
| 49 | CUs.push_back(std::move(U)); |
| 50 | } |
| 51 | |
| 52 | // Emit the various dwarf units to the unit section USection with |
| 53 | // the abbreviations going into ASection. |
David Blaikie | 2b22b1e | 2014-10-23 00:16:03 +0000 | [diff] [blame] | 54 | void DwarfFile::emitUnits(const MCSymbol *ASectionSym) { |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 55 | for (const auto &TheU : CUs) { |
David Blaikie | adcde36 | 2014-04-25 18:35:57 +0000 | [diff] [blame] | 56 | DIE &Die = TheU->getUnitDie(); |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 57 | const MCSection *USection = TheU->getSection(); |
| 58 | Asm->OutStreamer.SwitchSection(USection); |
| 59 | |
| 60 | // Emit the compile units header. |
| 61 | Asm->OutStreamer.EmitLabel(TheU->getLabelBegin()); |
| 62 | |
| 63 | // Emit size of content not including length itself |
| 64 | Asm->OutStreamer.AddComment("Length of Unit"); |
David Blaikie | adcde36 | 2014-04-25 18:35:57 +0000 | [diff] [blame] | 65 | Asm->EmitInt32(TheU->getHeaderSize() + Die.getSize()); |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 66 | |
| 67 | TheU->emitHeader(ASectionSym); |
| 68 | |
David Blaikie | 2b22b1e | 2014-10-23 00:16:03 +0000 | [diff] [blame] | 69 | DD.emitDIE(Die); |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 70 | Asm->OutStreamer.EmitLabel(TheU->getLabelEnd()); |
| 71 | } |
| 72 | } |
David Blaikie | f299947 | 2014-10-23 00:16:05 +0000 | [diff] [blame^] | 73 | |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 74 | // Compute the size and offset for each DIE. |
| 75 | void DwarfFile::computeSizeAndOffsets() { |
| 76 | // Offset from the first CU in the debug info section is 0 initially. |
| 77 | unsigned SecOffset = 0; |
| 78 | |
| 79 | // Iterate over each compile unit and set the size and offsets for each |
| 80 | // DIE within each compile unit. All offsets are CU relative. |
| 81 | for (const auto &TheU : CUs) { |
| 82 | TheU->setDebugInfoOffset(SecOffset); |
| 83 | |
| 84 | // CU-relative offset is reset to 0 here. |
| 85 | unsigned Offset = sizeof(int32_t) + // Length of Unit Info |
| 86 | TheU->getHeaderSize(); // Unit-specific headers |
| 87 | |
| 88 | // EndOffset here is CU-relative, after laying out |
| 89 | // all of the CU DIE. |
David Blaikie | adcde36 | 2014-04-25 18:35:57 +0000 | [diff] [blame] | 90 | unsigned EndOffset = computeSizeAndOffset(TheU->getUnitDie(), Offset); |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 91 | SecOffset += EndOffset; |
| 92 | } |
| 93 | } |
| 94 | // Compute the size and offset of a DIE. The offset is relative to start of the |
| 95 | // CU. It returns the offset after laying out the DIE. |
| 96 | unsigned DwarfFile::computeSizeAndOffset(DIE &Die, unsigned Offset) { |
| 97 | // Record the abbreviation. |
| 98 | assignAbbrevNumber(Die.getAbbrev()); |
| 99 | |
| 100 | // Get the abbreviation for this DIE. |
| 101 | const DIEAbbrev &Abbrev = Die.getAbbrev(); |
| 102 | |
| 103 | // Set DIE offset |
| 104 | Die.setOffset(Offset); |
| 105 | |
| 106 | // Start the size with the size of abbreviation code. |
| 107 | Offset += getULEB128Size(Die.getAbbrevNumber()); |
| 108 | |
| 109 | const SmallVectorImpl<DIEValue *> &Values = Die.getValues(); |
| 110 | const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData(); |
| 111 | |
| 112 | // Size the DIE attribute values. |
| 113 | for (unsigned i = 0, N = Values.size(); i < N; ++i) |
| 114 | // Size attribute value. |
| 115 | Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm()); |
| 116 | |
| 117 | // Get the children. |
| 118 | const auto &Children = Die.getChildren(); |
| 119 | |
| 120 | // Size the DIE children if any. |
| 121 | if (!Children.empty()) { |
| 122 | assert(Abbrev.hasChildren() && "Children flag not set"); |
| 123 | |
| 124 | for (auto &Child : Children) |
| 125 | Offset = computeSizeAndOffset(*Child, Offset); |
| 126 | |
| 127 | // End of children marker. |
| 128 | Offset += sizeof(int8_t); |
| 129 | } |
| 130 | |
| 131 | Die.setSize(Offset - Die.getOffset()); |
| 132 | return Offset; |
| 133 | } |
| 134 | void DwarfFile::emitAbbrevs(const MCSection *Section) { |
| 135 | // Check to see if it is worth the effort. |
| 136 | if (!Abbreviations.empty()) { |
| 137 | // Start the debug abbrev section. |
| 138 | Asm->OutStreamer.SwitchSection(Section); |
| 139 | |
| 140 | // For each abbrevation. |
| 141 | for (const DIEAbbrev *Abbrev : Abbreviations) { |
| 142 | // Emit the abbrevations code (base 1 index.) |
| 143 | Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code"); |
| 144 | |
| 145 | // Emit the abbreviations data. |
| 146 | Abbrev->Emit(Asm); |
| 147 | } |
| 148 | |
| 149 | // Mark end of abbreviations. |
| 150 | Asm->EmitULEB128(0, "EOM(3)"); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | // Emit strings into a string section. |
| 155 | void DwarfFile::emitStrings(const MCSection *StrSection, |
David Blaikie | 6741bb0 | 2014-09-11 21:12:48 +0000 | [diff] [blame] | 156 | const MCSection *OffsetSection) { |
| 157 | StrPool.emit(*Asm, StrSection, OffsetSection); |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 158 | } |
David Blaikie | f299947 | 2014-10-23 00:16:05 +0000 | [diff] [blame^] | 159 | |
| 160 | // If Var is a current function argument then add it to CurrentFnArguments list. |
| 161 | bool DwarfFile::addCurrentFnArgument(DbgVariable *Var, LexicalScope *Scope) { |
| 162 | if (Scope->getParent()) |
| 163 | return false; |
| 164 | DIVariable DV = Var->getVariable(); |
| 165 | if (DV.getTag() != dwarf::DW_TAG_arg_variable) |
| 166 | return false; |
| 167 | unsigned ArgNo = DV.getArgNumber(); |
| 168 | if (ArgNo == 0) |
| 169 | return false; |
| 170 | |
| 171 | auto &CurrentFnArguments = DD.getCurrentFnArguments(); |
| 172 | |
| 173 | // llvm::Function argument size is not good indicator of how many |
| 174 | // arguments does the function have at source level. |
| 175 | if (ArgNo > CurrentFnArguments.size()) |
| 176 | CurrentFnArguments.resize(ArgNo * 2); |
| 177 | assert(!CurrentFnArguments[ArgNo - 1]); |
| 178 | CurrentFnArguments[ArgNo - 1] = Var; |
| 179 | return true; |
| 180 | } |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 181 | } |