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);
68 Asm->OutStreamer.EmitLabel(TheU->getLabelEnd());
72 // Compute the size and offset for each DIE.
73 void DwarfFile::computeSizeAndOffsets() {
74 // Offset from the first CU in the debug info section is 0 initially.
75 unsigned SecOffset = 0;
77 // Iterate over each compile unit and set the size and offsets for each
78 // DIE within each compile unit. All offsets are CU relative.
79 for (const auto &TheU : CUs) {
80 TheU->setDebugInfoOffset(SecOffset);
82 // CU-relative offset is reset to 0 here.
83 unsigned Offset = sizeof(int32_t) + // Length of Unit Info
84 TheU->getHeaderSize(); // Unit-specific headers
86 // EndOffset here is CU-relative, after laying out
88 unsigned EndOffset = computeSizeAndOffset(TheU->getUnitDie(), Offset);
89 SecOffset += EndOffset;
92 // Compute the size and offset of a DIE. The offset is relative to start of the
93 // CU. It returns the offset after laying out the DIE.
94 unsigned DwarfFile::computeSizeAndOffset(DIE &Die, unsigned Offset) {
95 // Record the abbreviation.
96 assignAbbrevNumber(Die.getAbbrev());
98 // Get the abbreviation for this DIE.
99 const DIEAbbrev &Abbrev = Die.getAbbrev();
102 Die.setOffset(Offset);
104 // Start the size with the size of abbreviation code.
105 Offset += getULEB128Size(Die.getAbbrevNumber());
107 const SmallVectorImpl<DIEValue *> &Values = Die.getValues();
108 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
110 // Size the DIE attribute values.
111 for (unsigned i = 0, N = Values.size(); i < N; ++i)
112 // Size attribute value.
113 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
116 const auto &Children = Die.getChildren();
118 // Size the DIE children if any.
119 if (!Children.empty()) {
120 assert(Abbrev.hasChildren() && "Children flag not set");
122 for (auto &Child : Children)
123 Offset = computeSizeAndOffset(*Child, Offset);
125 // End of children marker.
126 Offset += sizeof(int8_t);
129 Die.setSize(Offset - Die.getOffset());
132 void DwarfFile::emitAbbrevs(const MCSection *Section) {
133 // Check to see if it is worth the effort.
134 if (!Abbreviations.empty()) {
135 // Start the debug abbrev section.
136 Asm->OutStreamer.SwitchSection(Section);
138 // For each abbrevation.
139 for (const DIEAbbrev *Abbrev : Abbreviations) {
140 // Emit the abbrevations code (base 1 index.)
141 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
143 // Emit the abbreviations data.
147 // Mark end of abbreviations.
148 Asm->EmitULEB128(0, "EOM(3)");
152 // Emit strings into a string section.
153 void DwarfFile::emitStrings(const MCSection *StrSection,
154 const MCSection *OffsetSection) {
155 StrPool.emit(*Asm, StrSection, OffsetSection);
158 void DwarfFile::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
159 SmallVectorImpl<DbgVariable *> &Vars = ScopeVariables[LS];
160 DIVariable DV = Var->getVariable();
161 // Variables with positive arg numbers are parameters.
162 if (unsigned ArgNum = DV.getArgNumber()) {
163 // Keep all parameters in order at the start of the variable list to ensure
164 // function types are correct (no out-of-order parameters)
166 // This could be improved by only doing it for optimized builds (unoptimized
167 // builds have the right order to begin with), searching from the back (this
168 // would catch the unoptimized case quickly), or doing a binary search
169 // rather than linear search.
170 auto I = Vars.begin();
171 while (I != Vars.end()) {
172 unsigned CurNum = (*I)->getVariable().getArgNumber();
173 // A local (non-parameter) variable has been found, insert immediately
177 // A later indexed parameter has been found, insert immediately before it.
180 assert(CurNum != ArgNum);