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 { |
| 21 | DwarfFile::DwarfFile(AsmPrinter *AP, const char *Pref, BumpPtrAllocator &DA) |
| 22 | : Asm(AP), StringPool(DA), NextStringPoolNumber(0), StringPref(Pref) {} |
| 23 | |
David Blaikie | 05e736f | 2014-04-23 19:44:08 +0000 | [diff] [blame^] | 24 | DwarfFile::~DwarfFile() {} |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 25 | |
| 26 | MCSymbol *DwarfFile::getStringPoolSym() { |
| 27 | return Asm->GetTempSymbol(StringPref); |
| 28 | } |
| 29 | |
| 30 | MCSymbol *DwarfFile::getStringPoolEntry(StringRef Str) { |
| 31 | std::pair<MCSymbol *, unsigned> &Entry = |
| 32 | StringPool.GetOrCreateValue(Str).getValue(); |
| 33 | if (Entry.first) |
| 34 | return Entry.first; |
| 35 | |
| 36 | Entry.second = NextStringPoolNumber++; |
| 37 | return Entry.first = Asm->GetTempSymbol(StringPref, Entry.second); |
| 38 | } |
| 39 | |
| 40 | unsigned DwarfFile::getStringPoolIndex(StringRef Str) { |
| 41 | std::pair<MCSymbol *, unsigned> &Entry = |
| 42 | StringPool.GetOrCreateValue(Str).getValue(); |
| 43 | if (Entry.first) |
| 44 | return Entry.second; |
| 45 | |
| 46 | Entry.second = NextStringPoolNumber++; |
| 47 | Entry.first = Asm->GetTempSymbol(StringPref, Entry.second); |
| 48 | return Entry.second; |
| 49 | } |
| 50 | |
| 51 | unsigned DwarfFile::getAddrPoolIndex(const MCSymbol *Sym, bool TLS) { |
| 52 | std::pair<AddrPool::iterator, bool> P = AddressPool.insert( |
| 53 | std::make_pair(Sym, AddressPoolEntry(AddressPool.size(), TLS))); |
| 54 | return P.first->second.Number; |
| 55 | } |
| 56 | |
| 57 | // Define a unique number for the abbreviation. |
| 58 | // |
| 59 | void DwarfFile::assignAbbrevNumber(DIEAbbrev &Abbrev) { |
| 60 | // Check the set for priors. |
| 61 | DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev); |
| 62 | |
| 63 | // If it's newly added. |
| 64 | if (InSet == &Abbrev) { |
| 65 | // Add to abbreviation list. |
| 66 | Abbreviations.push_back(&Abbrev); |
| 67 | |
| 68 | // Assign the vector position + 1 as its number. |
| 69 | Abbrev.setNumber(Abbreviations.size()); |
| 70 | } else { |
| 71 | // Assign existing abbreviation number. |
| 72 | Abbrev.setNumber(InSet->getNumber()); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | void DwarfFile::addUnit(std::unique_ptr<DwarfUnit> U) { |
| 77 | CUs.push_back(std::move(U)); |
| 78 | } |
| 79 | |
| 80 | // Emit the various dwarf units to the unit section USection with |
| 81 | // the abbreviations going into ASection. |
| 82 | void DwarfFile::emitUnits(DwarfDebug *DD, const MCSymbol *ASectionSym) { |
| 83 | for (const auto &TheU : CUs) { |
| 84 | DIE *Die = TheU->getUnitDie(); |
| 85 | const MCSection *USection = TheU->getSection(); |
| 86 | Asm->OutStreamer.SwitchSection(USection); |
| 87 | |
| 88 | // Emit the compile units header. |
| 89 | Asm->OutStreamer.EmitLabel(TheU->getLabelBegin()); |
| 90 | |
| 91 | // Emit size of content not including length itself |
| 92 | Asm->OutStreamer.AddComment("Length of Unit"); |
| 93 | Asm->EmitInt32(TheU->getHeaderSize() + Die->getSize()); |
| 94 | |
| 95 | TheU->emitHeader(ASectionSym); |
| 96 | |
| 97 | DD->emitDIE(*Die); |
| 98 | Asm->OutStreamer.EmitLabel(TheU->getLabelEnd()); |
| 99 | } |
| 100 | } |
| 101 | // Compute the size and offset for each DIE. |
| 102 | void DwarfFile::computeSizeAndOffsets() { |
| 103 | // Offset from the first CU in the debug info section is 0 initially. |
| 104 | unsigned SecOffset = 0; |
| 105 | |
| 106 | // Iterate over each compile unit and set the size and offsets for each |
| 107 | // DIE within each compile unit. All offsets are CU relative. |
| 108 | for (const auto &TheU : CUs) { |
| 109 | TheU->setDebugInfoOffset(SecOffset); |
| 110 | |
| 111 | // CU-relative offset is reset to 0 here. |
| 112 | unsigned Offset = sizeof(int32_t) + // Length of Unit Info |
| 113 | TheU->getHeaderSize(); // Unit-specific headers |
| 114 | |
| 115 | // EndOffset here is CU-relative, after laying out |
| 116 | // all of the CU DIE. |
| 117 | unsigned EndOffset = computeSizeAndOffset(*TheU->getUnitDie(), Offset); |
| 118 | SecOffset += EndOffset; |
| 119 | } |
| 120 | } |
| 121 | // Compute the size and offset of a DIE. The offset is relative to start of the |
| 122 | // CU. It returns the offset after laying out the DIE. |
| 123 | unsigned DwarfFile::computeSizeAndOffset(DIE &Die, unsigned Offset) { |
| 124 | // Record the abbreviation. |
| 125 | assignAbbrevNumber(Die.getAbbrev()); |
| 126 | |
| 127 | // Get the abbreviation for this DIE. |
| 128 | const DIEAbbrev &Abbrev = Die.getAbbrev(); |
| 129 | |
| 130 | // Set DIE offset |
| 131 | Die.setOffset(Offset); |
| 132 | |
| 133 | // Start the size with the size of abbreviation code. |
| 134 | Offset += getULEB128Size(Die.getAbbrevNumber()); |
| 135 | |
| 136 | const SmallVectorImpl<DIEValue *> &Values = Die.getValues(); |
| 137 | const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData(); |
| 138 | |
| 139 | // Size the DIE attribute values. |
| 140 | for (unsigned i = 0, N = Values.size(); i < N; ++i) |
| 141 | // Size attribute value. |
| 142 | Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm()); |
| 143 | |
| 144 | // Get the children. |
| 145 | const auto &Children = Die.getChildren(); |
| 146 | |
| 147 | // Size the DIE children if any. |
| 148 | if (!Children.empty()) { |
| 149 | assert(Abbrev.hasChildren() && "Children flag not set"); |
| 150 | |
| 151 | for (auto &Child : Children) |
| 152 | Offset = computeSizeAndOffset(*Child, Offset); |
| 153 | |
| 154 | // End of children marker. |
| 155 | Offset += sizeof(int8_t); |
| 156 | } |
| 157 | |
| 158 | Die.setSize(Offset - Die.getOffset()); |
| 159 | return Offset; |
| 160 | } |
| 161 | void DwarfFile::emitAbbrevs(const MCSection *Section) { |
| 162 | // Check to see if it is worth the effort. |
| 163 | if (!Abbreviations.empty()) { |
| 164 | // Start the debug abbrev section. |
| 165 | Asm->OutStreamer.SwitchSection(Section); |
| 166 | |
| 167 | // For each abbrevation. |
| 168 | for (const DIEAbbrev *Abbrev : Abbreviations) { |
| 169 | // Emit the abbrevations code (base 1 index.) |
| 170 | Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code"); |
| 171 | |
| 172 | // Emit the abbreviations data. |
| 173 | Abbrev->Emit(Asm); |
| 174 | } |
| 175 | |
| 176 | // Mark end of abbreviations. |
| 177 | Asm->EmitULEB128(0, "EOM(3)"); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | // Emit strings into a string section. |
| 182 | void DwarfFile::emitStrings(const MCSection *StrSection, |
| 183 | const MCSection *OffsetSection, |
| 184 | const MCSymbol *StrSecSym) { |
| 185 | |
| 186 | if (StringPool.empty()) |
| 187 | return; |
| 188 | |
| 189 | // Start the dwarf str section. |
| 190 | Asm->OutStreamer.SwitchSection(StrSection); |
| 191 | |
| 192 | // Get all of the string pool entries and put them in an array by their ID so |
| 193 | // we can sort them. |
David Blaikie | 05e736f | 2014-04-23 19:44:08 +0000 | [diff] [blame^] | 194 | SmallVector<std::pair<unsigned, const StrPool::value_type *>, 64> Entries; |
David Blaikie | 85f80d7 | 2014-04-23 18:54:00 +0000 | [diff] [blame] | 195 | |
| 196 | for (const auto &I : StringPool) |
| 197 | Entries.push_back(std::make_pair(I.second.second, &I)); |
| 198 | |
| 199 | array_pod_sort(Entries.begin(), Entries.end()); |
| 200 | |
| 201 | for (const auto &Entry : Entries) { |
| 202 | // Emit a label for reference from debug information entries. |
| 203 | Asm->OutStreamer.EmitLabel(Entry.second->getValue().first); |
| 204 | |
| 205 | // Emit the string itself with a terminating null byte. |
| 206 | Asm->OutStreamer.EmitBytes(StringRef(Entry.second->getKeyData(), |
| 207 | Entry.second->getKeyLength() + 1)); |
| 208 | } |
| 209 | |
| 210 | // If we've got an offset section go ahead and emit that now as well. |
| 211 | if (OffsetSection) { |
| 212 | Asm->OutStreamer.SwitchSection(OffsetSection); |
| 213 | unsigned offset = 0; |
| 214 | unsigned size = 4; // FIXME: DWARF64 is 8. |
| 215 | for (const auto &Entry : Entries) { |
| 216 | Asm->OutStreamer.EmitIntValue(offset, size); |
| 217 | offset += Entry.second->getKeyLength() + 1; |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | // Emit addresses into the section given. |
| 223 | void DwarfFile::emitAddresses(const MCSection *AddrSection) { |
| 224 | |
| 225 | if (AddressPool.empty()) |
| 226 | return; |
| 227 | |
| 228 | // Start the dwarf addr section. |
| 229 | Asm->OutStreamer.SwitchSection(AddrSection); |
| 230 | |
| 231 | // Order the address pool entries by ID |
| 232 | SmallVector<const MCExpr *, 64> Entries(AddressPool.size()); |
| 233 | |
| 234 | for (const auto &I : AddressPool) |
| 235 | Entries[I.second.Number] = |
| 236 | I.second.TLS |
| 237 | ? Asm->getObjFileLowering().getDebugThreadLocalSymbol(I.first) |
| 238 | : MCSymbolRefExpr::Create(I.first, Asm->OutContext); |
| 239 | |
| 240 | for (const MCExpr *Entry : Entries) |
| 241 | Asm->OutStreamer.EmitValue(Entry, Asm->getDataLayout().getPointerSize()); |
| 242 | } |
| 243 | } |