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() {}
26 MCSymbol *DwarfFile::getStringPoolSym() {
27 return Asm->GetTempSymbol(StringPref);
30 MCSymbol *DwarfFile::getStringPoolEntry(StringRef Str) {
31 std::pair<MCSymbol *, unsigned> &Entry =
32 StringPool.GetOrCreateValue(Str).getValue();
36 Entry.second = NextStringPoolNumber++;
37 return Entry.first = Asm->GetTempSymbol(StringPref, Entry.second);
40 unsigned DwarfFile::getStringPoolIndex(StringRef Str) {
41 std::pair<MCSymbol *, unsigned> &Entry =
42 StringPool.GetOrCreateValue(Str).getValue();
46 Entry.second = NextStringPoolNumber++;
47 Entry.first = Asm->GetTempSymbol(StringPref, Entry.second);
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;
57 // Define a unique number for the abbreviation.
59 void DwarfFile::assignAbbrevNumber(DIEAbbrev &Abbrev) {
60 // Check the set for priors.
61 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
63 // If it's newly added.
64 if (InSet == &Abbrev) {
65 // Add to abbreviation list.
66 Abbreviations.push_back(&Abbrev);
68 // Assign the vector position + 1 as its number.
69 Abbrev.setNumber(Abbreviations.size());
71 // Assign existing abbreviation number.
72 Abbrev.setNumber(InSet->getNumber());
76 void DwarfFile::addUnit(std::unique_ptr<DwarfUnit> U) {
77 CUs.push_back(std::move(U));
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);
88 // Emit the compile units header.
89 Asm->OutStreamer.EmitLabel(TheU->getLabelBegin());
91 // Emit size of content not including length itself
92 Asm->OutStreamer.AddComment("Length of Unit");
93 Asm->EmitInt32(TheU->getHeaderSize() + Die->getSize());
95 TheU->emitHeader(ASectionSym);
98 Asm->OutStreamer.EmitLabel(TheU->getLabelEnd());
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;
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);
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
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;
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());
127 // Get the abbreviation for this DIE.
128 const DIEAbbrev &Abbrev = Die.getAbbrev();
131 Die.setOffset(Offset);
133 // Start the size with the size of abbreviation code.
134 Offset += getULEB128Size(Die.getAbbrevNumber());
136 const SmallVectorImpl<DIEValue *> &Values = Die.getValues();
137 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
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());
145 const auto &Children = Die.getChildren();
147 // Size the DIE children if any.
148 if (!Children.empty()) {
149 assert(Abbrev.hasChildren() && "Children flag not set");
151 for (auto &Child : Children)
152 Offset = computeSizeAndOffset(*Child, Offset);
154 // End of children marker.
155 Offset += sizeof(int8_t);
158 Die.setSize(Offset - Die.getOffset());
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);
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");
172 // Emit the abbreviations data.
176 // Mark end of abbreviations.
177 Asm->EmitULEB128(0, "EOM(3)");
181 // Emit strings into a string section.
182 void DwarfFile::emitStrings(const MCSection *StrSection,
183 const MCSection *OffsetSection,
184 const MCSymbol *StrSecSym) {
186 if (StringPool.empty())
189 // Start the dwarf str section.
190 Asm->OutStreamer.SwitchSection(StrSection);
192 // Get all of the string pool entries and put them in an array by their ID so
194 SmallVector<std::pair<unsigned, const StrPool::value_type *>, 64> Entries;
196 for (const auto &I : StringPool)
197 Entries.push_back(std::make_pair(I.second.second, &I));
199 array_pod_sort(Entries.begin(), Entries.end());
201 for (const auto &Entry : Entries) {
202 // Emit a label for reference from debug information entries.
203 Asm->OutStreamer.EmitLabel(Entry.second->getValue().first);
205 // Emit the string itself with a terminating null byte.
206 Asm->OutStreamer.EmitBytes(StringRef(Entry.second->getKeyData(),
207 Entry.second->getKeyLength() + 1));
210 // If we've got an offset section go ahead and emit that now as well.
212 Asm->OutStreamer.SwitchSection(OffsetSection);
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;
222 // Emit addresses into the section given.
223 void DwarfFile::emitAddresses(const MCSection *AddrSection) {
225 if (AddressPool.empty())
228 // Start the dwarf addr section.
229 Asm->OutStreamer.SwitchSection(AddrSection);
231 // Order the address pool entries by ID
232 SmallVector<const MCExpr *, 64> Entries(AddressPool.size());
234 for (const auto &I : AddressPool)
235 Entries[I.second.Number] =
237 ? Asm->getObjFileLowering().getDebugThreadLocalSymbol(I.first)
238 : MCSymbolRefExpr::Create(I.first, Asm->OutContext);
240 for (const MCExpr *Entry : Entries)
241 Asm->OutStreamer.EmitValue(Entry, Asm->getDataLayout().getPointerSize());