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 #include "DwarfDebug.h"
12 #include "DwarfUnit.h"
13 #include "llvm/ADT/STLExtras.h"
14 #include "llvm/IR/DataLayout.h"
15 #include "llvm/MC/MCStreamer.h"
16 #include "llvm/Support/LEB128.h"
17 #include "llvm/Target/TargetLoweringObjectFile.h"
18
19 namespace llvm {
DwarfFile(AsmPrinter * AP,DwarfDebug & DD,StringRef Pref,BumpPtrAllocator & DA)20 DwarfFile::DwarfFile(AsmPrinter *AP, DwarfDebug &DD, StringRef Pref,
21 BumpPtrAllocator &DA)
22 : Asm(AP), DD(DD), StrPool(DA, *Asm, Pref) {}
23
~DwarfFile()24 DwarfFile::~DwarfFile() {}
25
26 // Define a unique number for the abbreviation.
27 //
assignAbbrevNumber(DIEAbbrev & Abbrev)28 void DwarfFile::assignAbbrevNumber(DIEAbbrev &Abbrev) {
29 // Check the set for priors.
30 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
31
32 // If it's newly added.
33 if (InSet == &Abbrev) {
34 // Add to abbreviation list.
35 Abbreviations.push_back(&Abbrev);
36
37 // Assign the vector position + 1 as its number.
38 Abbrev.setNumber(Abbreviations.size());
39 } else {
40 // Assign existing abbreviation number.
41 Abbrev.setNumber(InSet->getNumber());
42 }
43 }
44
addUnit(std::unique_ptr<DwarfUnit> U)45 void DwarfFile::addUnit(std::unique_ptr<DwarfUnit> U) {
46 CUs.push_back(std::move(U));
47 }
48
49 // Emit the various dwarf units to the unit section USection with
50 // the abbreviations going into ASection.
emitUnits(const MCSymbol * ASectionSym)51 void DwarfFile::emitUnits(const MCSymbol *ASectionSym) {
52 for (const auto &TheU : CUs) {
53 DIE &Die = TheU->getUnitDie();
54 const MCSection *USection = TheU->getSection();
55 Asm->OutStreamer.SwitchSection(USection);
56
57 TheU->emitHeader(ASectionSym);
58
59 DD.emitDIE(Die);
60 }
61 }
62
63 // Compute the size and offset for each DIE.
computeSizeAndOffsets()64 void DwarfFile::computeSizeAndOffsets() {
65 // Offset from the first CU in the debug info section is 0 initially.
66 unsigned SecOffset = 0;
67
68 // Iterate over each compile unit and set the size and offsets for each
69 // DIE within each compile unit. All offsets are CU relative.
70 for (const auto &TheU : CUs) {
71 TheU->setDebugInfoOffset(SecOffset);
72
73 // CU-relative offset is reset to 0 here.
74 unsigned Offset = sizeof(int32_t) + // Length of Unit Info
75 TheU->getHeaderSize(); // Unit-specific headers
76
77 // EndOffset here is CU-relative, after laying out
78 // all of the CU DIE.
79 unsigned EndOffset = computeSizeAndOffset(TheU->getUnitDie(), Offset);
80 SecOffset += EndOffset;
81 }
82 }
83 // Compute the size and offset of a DIE. The offset is relative to start of the
84 // CU. It returns the offset after laying out the DIE.
computeSizeAndOffset(DIE & Die,unsigned Offset)85 unsigned DwarfFile::computeSizeAndOffset(DIE &Die, unsigned Offset) {
86 // Record the abbreviation.
87 assignAbbrevNumber(Die.getAbbrev());
88
89 // Get the abbreviation for this DIE.
90 const DIEAbbrev &Abbrev = Die.getAbbrev();
91
92 // Set DIE offset
93 Die.setOffset(Offset);
94
95 // Start the size with the size of abbreviation code.
96 Offset += getULEB128Size(Die.getAbbrevNumber());
97
98 const SmallVectorImpl<DIEValue *> &Values = Die.getValues();
99 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
100
101 // Size the DIE attribute values.
102 for (unsigned i = 0, N = Values.size(); i < N; ++i)
103 // Size attribute value.
104 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
105
106 // Get the children.
107 const auto &Children = Die.getChildren();
108
109 // Size the DIE children if any.
110 if (!Children.empty()) {
111 assert(Abbrev.hasChildren() && "Children flag not set");
112
113 for (auto &Child : Children)
114 Offset = computeSizeAndOffset(*Child, Offset);
115
116 // End of children marker.
117 Offset += sizeof(int8_t);
118 }
119
120 Die.setSize(Offset - Die.getOffset());
121 return Offset;
122 }
emitAbbrevs(const MCSection * Section)123 void DwarfFile::emitAbbrevs(const MCSection *Section) {
124 // Check to see if it is worth the effort.
125 if (!Abbreviations.empty()) {
126 // Start the debug abbrev section.
127 Asm->OutStreamer.SwitchSection(Section);
128
129 // For each abbrevation.
130 for (const DIEAbbrev *Abbrev : Abbreviations) {
131 // Emit the abbrevations code (base 1 index.)
132 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
133
134 // Emit the abbreviations data.
135 Abbrev->Emit(Asm);
136 }
137
138 // Mark end of abbreviations.
139 Asm->EmitULEB128(0, "EOM(3)");
140 }
141 }
142
143 // Emit strings into a string section.
emitStrings(const MCSection * StrSection,const MCSection * OffsetSection)144 void DwarfFile::emitStrings(const MCSection *StrSection,
145 const MCSection *OffsetSection) {
146 StrPool.emit(*Asm, StrSection, OffsetSection);
147 }
148
addScopeVariable(LexicalScope * LS,DbgVariable * Var)149 void DwarfFile::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
150 SmallVectorImpl<DbgVariable *> &Vars = ScopeVariables[LS];
151 DIVariable DV = Var->getVariable();
152 // Variables with positive arg numbers are parameters.
153 if (unsigned ArgNum = DV.getArgNumber()) {
154 // Keep all parameters in order at the start of the variable list to ensure
155 // function types are correct (no out-of-order parameters)
156 //
157 // This could be improved by only doing it for optimized builds (unoptimized
158 // builds have the right order to begin with), searching from the back (this
159 // would catch the unoptimized case quickly), or doing a binary search
160 // rather than linear search.
161 auto I = Vars.begin();
162 while (I != Vars.end()) {
163 unsigned CurNum = (*I)->getVariable().getArgNumber();
164 // A local (non-parameter) variable has been found, insert immediately
165 // before it.
166 if (CurNum == 0)
167 break;
168 // A later indexed parameter has been found, insert immediately before it.
169 if (CurNum > ArgNum)
170 break;
171 // FIXME: There are still some cases where two inlined functions are
172 // conflated together (two calls to the same function at the same
173 // location (eg: via a macro, or without column info, etc)) and then
174 // their arguments are conflated as well.
175 assert((LS->getParent() || CurNum != ArgNum) &&
176 "Duplicate argument for top level (non-inlined) function");
177 ++I;
178 }
179 Vars.insert(I, Var);
180 return;
181 }
182
183 Vars.push_back(Var);
184 }
185 }
186