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 // Define a unique number for the abbreviation.
53 void DwarfFile::assignAbbrevNumber(DIEAbbrev &Abbrev) {
54 // Check the set for priors.
55 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
57 // If it's newly added.
58 if (InSet == &Abbrev) {
59 // Add to abbreviation list.
60 Abbreviations.push_back(&Abbrev);
62 // Assign the vector position + 1 as its number.
63 Abbrev.setNumber(Abbreviations.size());
65 // Assign existing abbreviation number.
66 Abbrev.setNumber(InSet->getNumber());
70 void DwarfFile::addUnit(std::unique_ptr<DwarfUnit> U) {
71 CUs.push_back(std::move(U));
74 // Emit the various dwarf units to the unit section USection with
75 // the abbreviations going into ASection.
76 void DwarfFile::emitUnits(DwarfDebug *DD, const MCSymbol *ASectionSym) {
77 for (const auto &TheU : CUs) {
78 DIE &Die = TheU->getUnitDie();
79 const MCSection *USection = TheU->getSection();
80 Asm->OutStreamer.SwitchSection(USection);
82 // Emit the compile units header.
83 Asm->OutStreamer.EmitLabel(TheU->getLabelBegin());
85 // Emit size of content not including length itself
86 Asm->OutStreamer.AddComment("Length of Unit");
87 Asm->EmitInt32(TheU->getHeaderSize() + Die.getSize());
89 TheU->emitHeader(ASectionSym);
92 Asm->OutStreamer.EmitLabel(TheU->getLabelEnd());
95 // Compute the size and offset for each DIE.
96 void DwarfFile::computeSizeAndOffsets() {
97 // Offset from the first CU in the debug info section is 0 initially.
98 unsigned SecOffset = 0;
100 // Iterate over each compile unit and set the size and offsets for each
101 // DIE within each compile unit. All offsets are CU relative.
102 for (const auto &TheU : CUs) {
103 TheU->setDebugInfoOffset(SecOffset);
105 // CU-relative offset is reset to 0 here.
106 unsigned Offset = sizeof(int32_t) + // Length of Unit Info
107 TheU->getHeaderSize(); // Unit-specific headers
109 // EndOffset here is CU-relative, after laying out
110 // all of the CU DIE.
111 unsigned EndOffset = computeSizeAndOffset(TheU->getUnitDie(), Offset);
112 SecOffset += EndOffset;
115 // Compute the size and offset of a DIE. The offset is relative to start of the
116 // CU. It returns the offset after laying out the DIE.
117 unsigned DwarfFile::computeSizeAndOffset(DIE &Die, unsigned Offset) {
118 // Record the abbreviation.
119 assignAbbrevNumber(Die.getAbbrev());
121 // Get the abbreviation for this DIE.
122 const DIEAbbrev &Abbrev = Die.getAbbrev();
125 Die.setOffset(Offset);
127 // Start the size with the size of abbreviation code.
128 Offset += getULEB128Size(Die.getAbbrevNumber());
130 const SmallVectorImpl<DIEValue *> &Values = Die.getValues();
131 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
133 // Size the DIE attribute values.
134 for (unsigned i = 0, N = Values.size(); i < N; ++i)
135 // Size attribute value.
136 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
139 const auto &Children = Die.getChildren();
141 // Size the DIE children if any.
142 if (!Children.empty()) {
143 assert(Abbrev.hasChildren() && "Children flag not set");
145 for (auto &Child : Children)
146 Offset = computeSizeAndOffset(*Child, Offset);
148 // End of children marker.
149 Offset += sizeof(int8_t);
152 Die.setSize(Offset - Die.getOffset());
155 void DwarfFile::emitAbbrevs(const MCSection *Section) {
156 // Check to see if it is worth the effort.
157 if (!Abbreviations.empty()) {
158 // Start the debug abbrev section.
159 Asm->OutStreamer.SwitchSection(Section);
161 // For each abbrevation.
162 for (const DIEAbbrev *Abbrev : Abbreviations) {
163 // Emit the abbrevations code (base 1 index.)
164 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
166 // Emit the abbreviations data.
170 // Mark end of abbreviations.
171 Asm->EmitULEB128(0, "EOM(3)");
175 // Emit strings into a string section.
176 void DwarfFile::emitStrings(const MCSection *StrSection,
177 const MCSection *OffsetSection,
178 const MCSymbol *StrSecSym) {
180 if (StringPool.empty())
183 // Start the dwarf str section.
184 Asm->OutStreamer.SwitchSection(StrSection);
186 // Get all of the string pool entries and put them in an array by their ID so
188 SmallVector<std::pair<unsigned, const StrPool::value_type *>, 64> Entries;
190 for (const auto &I : StringPool)
191 Entries.push_back(std::make_pair(I.second.second, &I));
193 array_pod_sort(Entries.begin(), Entries.end());
195 for (const auto &Entry : Entries) {
196 // Emit a label for reference from debug information entries.
197 Asm->OutStreamer.EmitLabel(Entry.second->getValue().first);
199 // Emit the string itself with a terminating null byte.
200 Asm->OutStreamer.EmitBytes(StringRef(Entry.second->getKeyData(),
201 Entry.second->getKeyLength() + 1));
204 // If we've got an offset section go ahead and emit that now as well.
206 Asm->OutStreamer.SwitchSection(OffsetSection);
208 unsigned size = 4; // FIXME: DWARF64 is 8.
209 for (const auto &Entry : Entries) {
210 Asm->OutStreamer.EmitIntValue(offset, size);
211 offset += Entry.second->getKeyLength() + 1;