1 //===-- llvm/CodeGen/DwarfFile.cpp - Dwarf Debug Framework ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "DwarfFile.h"
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"
21 DwarfFile::DwarfFile(AsmPrinter *AP, const char *Pref, BumpPtrAllocator &DA)
22 : Asm(AP), StringPool(DA), NextStringPoolNumber(0), StringPref(Pref) {}
24 DwarfFile::~DwarfFile() {
27 MCSymbol *DwarfFile::getStringPoolSym() {
28 return Asm->GetTempSymbol(StringPref);
31 MCSymbol *DwarfFile::getStringPoolEntry(StringRef Str) {
32 std::pair<MCSymbol *, unsigned> &Entry =
33 StringPool.GetOrCreateValue(Str).getValue();
37 Entry.second = NextStringPoolNumber++;
38 return Entry.first = Asm->GetTempSymbol(StringPref, Entry.second);
41 unsigned DwarfFile::getStringPoolIndex(StringRef Str) {
42 std::pair<MCSymbol *, unsigned> &Entry =
43 StringPool.GetOrCreateValue(Str).getValue();
47 Entry.second = NextStringPoolNumber++;
48 Entry.first = Asm->GetTempSymbol(StringPref, Entry.second);
52 unsigned DwarfFile::getAddrPoolIndex(const MCSymbol *Sym, bool TLS) {
53 std::pair<AddrPool::iterator, bool> P = AddressPool.insert(
54 std::make_pair(Sym, AddressPoolEntry(AddressPool.size(), TLS)));
55 return P.first->second.Number;
58 // Define a unique number for the abbreviation.
60 void DwarfFile::assignAbbrevNumber(DIEAbbrev &Abbrev) {
61 // Check the set for priors.
62 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
64 // If it's newly added.
65 if (InSet == &Abbrev) {
66 // Add to abbreviation list.
67 Abbreviations.push_back(&Abbrev);
69 // Assign the vector position + 1 as its number.
70 Abbrev.setNumber(Abbreviations.size());
72 // Assign existing abbreviation number.
73 Abbrev.setNumber(InSet->getNumber());
77 void DwarfFile::addUnit(std::unique_ptr<DwarfUnit> U) {
78 CUs.push_back(std::move(U));
81 // Emit the various dwarf units to the unit section USection with
82 // the abbreviations going into ASection.
83 void DwarfFile::emitUnits(DwarfDebug *DD, const MCSymbol *ASectionSym) {
84 for (const auto &TheU : CUs) {
85 DIE *Die = TheU->getUnitDie();
86 const MCSection *USection = TheU->getSection();
87 Asm->OutStreamer.SwitchSection(USection);
89 // Emit the compile units header.
90 Asm->OutStreamer.EmitLabel(TheU->getLabelBegin());
92 // Emit size of content not including length itself
93 Asm->OutStreamer.AddComment("Length of Unit");
94 Asm->EmitInt32(TheU->getHeaderSize() + Die->getSize());
96 TheU->emitHeader(ASectionSym);
99 Asm->OutStreamer.EmitLabel(TheU->getLabelEnd());
102 // Compute the size and offset for each DIE.
103 void DwarfFile::computeSizeAndOffsets() {
104 // Offset from the first CU in the debug info section is 0 initially.
105 unsigned SecOffset = 0;
107 // Iterate over each compile unit and set the size and offsets for each
108 // DIE within each compile unit. All offsets are CU relative.
109 for (const auto &TheU : CUs) {
110 TheU->setDebugInfoOffset(SecOffset);
112 // CU-relative offset is reset to 0 here.
113 unsigned Offset = sizeof(int32_t) + // Length of Unit Info
114 TheU->getHeaderSize(); // Unit-specific headers
116 // EndOffset here is CU-relative, after laying out
117 // all of the CU DIE.
118 unsigned EndOffset = computeSizeAndOffset(*TheU->getUnitDie(), Offset);
119 SecOffset += EndOffset;
122 // Compute the size and offset of a DIE. The offset is relative to start of the
123 // CU. It returns the offset after laying out the DIE.
124 unsigned DwarfFile::computeSizeAndOffset(DIE &Die, unsigned Offset) {
125 // Record the abbreviation.
126 assignAbbrevNumber(Die.getAbbrev());
128 // Get the abbreviation for this DIE.
129 const DIEAbbrev &Abbrev = Die.getAbbrev();
132 Die.setOffset(Offset);
134 // Start the size with the size of abbreviation code.
135 Offset += getULEB128Size(Die.getAbbrevNumber());
137 const SmallVectorImpl<DIEValue *> &Values = Die.getValues();
138 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
140 // Size the DIE attribute values.
141 for (unsigned i = 0, N = Values.size(); i < N; ++i)
142 // Size attribute value.
143 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
146 const auto &Children = Die.getChildren();
148 // Size the DIE children if any.
149 if (!Children.empty()) {
150 assert(Abbrev.hasChildren() && "Children flag not set");
152 for (auto &Child : Children)
153 Offset = computeSizeAndOffset(*Child, Offset);
155 // End of children marker.
156 Offset += sizeof(int8_t);
159 Die.setSize(Offset - Die.getOffset());
162 void DwarfFile::emitAbbrevs(const MCSection *Section) {
163 // Check to see if it is worth the effort.
164 if (!Abbreviations.empty()) {
165 // Start the debug abbrev section.
166 Asm->OutStreamer.SwitchSection(Section);
168 // For each abbrevation.
169 for (const DIEAbbrev *Abbrev : Abbreviations) {
170 // Emit the abbrevations code (base 1 index.)
171 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
173 // Emit the abbreviations data.
177 // Mark end of abbreviations.
178 Asm->EmitULEB128(0, "EOM(3)");
182 // Emit strings into a string section.
183 void DwarfFile::emitStrings(const MCSection *StrSection,
184 const MCSection *OffsetSection,
185 const MCSymbol *StrSecSym) {
187 if (StringPool.empty())
190 // Start the dwarf str section.
191 Asm->OutStreamer.SwitchSection(StrSection);
193 // Get all of the string pool entries and put them in an array by their ID so
195 SmallVector<std::pair<unsigned, const StrPool::value_type *>, 64 > Entries;
197 for (const auto &I : StringPool)
198 Entries.push_back(std::make_pair(I.second.second, &I));
200 array_pod_sort(Entries.begin(), Entries.end());
202 for (const auto &Entry : Entries) {
203 // Emit a label for reference from debug information entries.
204 Asm->OutStreamer.EmitLabel(Entry.second->getValue().first);
206 // Emit the string itself with a terminating null byte.
207 Asm->OutStreamer.EmitBytes(StringRef(Entry.second->getKeyData(),
208 Entry.second->getKeyLength() + 1));
211 // If we've got an offset section go ahead and emit that now as well.
213 Asm->OutStreamer.SwitchSection(OffsetSection);
215 unsigned size = 4; // FIXME: DWARF64 is 8.
216 for (const auto &Entry : Entries) {
217 Asm->OutStreamer.EmitIntValue(offset, size);
218 offset += Entry.second->getKeyLength() + 1;
223 // Emit addresses into the section given.
224 void DwarfFile::emitAddresses(const MCSection *AddrSection) {
226 if (AddressPool.empty())
229 // Start the dwarf addr section.
230 Asm->OutStreamer.SwitchSection(AddrSection);
232 // Order the address pool entries by ID
233 SmallVector<const MCExpr *, 64> Entries(AddressPool.size());
235 for (const auto &I : AddressPool)
236 Entries[I.second.Number] =
238 ? Asm->getObjFileLowering().getDebugThreadLocalSymbol(I.first)
239 : MCSymbolRefExpr::Create(I.first, Asm->OutContext);
241 for (const MCExpr *Entry : Entries)
242 Asm->OutStreamer.EmitValue(Entry, Asm->getDataLayout().getPointerSize());