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