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