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 TheU->emitHeader(ASectionSym);
64 // Compute the size and offset for each DIE.
65 void DwarfFile::computeSizeAndOffsets() {
66 // Offset from the first CU in the debug info section is 0 initially.
67 unsigned SecOffset = 0;
69 // Iterate over each compile unit and set the size and offsets for each
70 // DIE within each compile unit. All offsets are CU relative.
71 for (const auto &TheU : CUs) {
72 TheU->setDebugInfoOffset(SecOffset);
74 // CU-relative offset is reset to 0 here.
75 unsigned Offset = sizeof(int32_t) + // Length of Unit Info
76 TheU->getHeaderSize(); // Unit-specific headers
78 // EndOffset here is CU-relative, after laying out
80 unsigned EndOffset = computeSizeAndOffset(TheU->getUnitDie(), Offset);
81 SecOffset += EndOffset;
84 // Compute the size and offset of a DIE. The offset is relative to start of the
85 // CU. It returns the offset after laying out the DIE.
86 unsigned DwarfFile::computeSizeAndOffset(DIE &Die, unsigned Offset) {
87 // Record the abbreviation.
88 assignAbbrevNumber(Die.getAbbrev());
90 // Get the abbreviation for this DIE.
91 const DIEAbbrev &Abbrev = Die.getAbbrev();
94 Die.setOffset(Offset);
96 // Start the size with the size of abbreviation code.
97 Offset += getULEB128Size(Die.getAbbrevNumber());
99 const SmallVectorImpl<DIEValue *> &Values = Die.getValues();
100 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
102 // Size the DIE attribute values.
103 for (unsigned i = 0, N = Values.size(); i < N; ++i)
104 // Size attribute value.
105 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
108 const auto &Children = Die.getChildren();
110 // Size the DIE children if any.
111 if (!Children.empty()) {
112 assert(Abbrev.hasChildren() && "Children flag not set");
114 for (auto &Child : Children)
115 Offset = computeSizeAndOffset(*Child, Offset);
117 // End of children marker.
118 Offset += sizeof(int8_t);
121 Die.setSize(Offset - Die.getOffset());
124 void DwarfFile::emitAbbrevs(const MCSection *Section) {
125 // Check to see if it is worth the effort.
126 if (!Abbreviations.empty()) {
127 // Start the debug abbrev section.
128 Asm->OutStreamer.SwitchSection(Section);
130 // For each abbrevation.
131 for (const DIEAbbrev *Abbrev : Abbreviations) {
132 // Emit the abbrevations code (base 1 index.)
133 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
135 // Emit the abbreviations data.
139 // Mark end of abbreviations.
140 Asm->EmitULEB128(0, "EOM(3)");
144 // Emit strings into a string section.
145 void DwarfFile::emitStrings(const MCSection *StrSection,
146 const MCSection *OffsetSection) {
147 StrPool.emit(*Asm, StrSection, OffsetSection);
150 void DwarfFile::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
151 SmallVectorImpl<DbgVariable *> &Vars = ScopeVariables[LS];
152 DIVariable DV = Var->getVariable();
153 // Variables with positive arg numbers are parameters.
154 if (unsigned ArgNum = DV.getArgNumber()) {
155 // Keep all parameters in order at the start of the variable list to ensure
156 // function types are correct (no out-of-order parameters)
158 // This could be improved by only doing it for optimized builds (unoptimized
159 // builds have the right order to begin with), searching from the back (this
160 // would catch the unoptimized case quickly), or doing a binary search
161 // rather than linear search.
162 auto I = Vars.begin();
163 while (I != Vars.end()) {
164 unsigned CurNum = (*I)->getVariable().getArgNumber();
165 // A local (non-parameter) variable has been found, insert immediately
169 // A later indexed parameter has been found, insert immediately before it.
172 // FIXME: There are still some cases where two inlined functions are
173 // conflated together (two calls to the same function at the same
174 // location (eg: via a macro, or without column info, etc)) and then
175 // their arguments are conflated as well.
176 assert((LS->getParent() || CurNum != ArgNum) &&
177 "Duplicate argument for top level (non-inlined) function");