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"
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"
20 DwarfFile::DwarfFile(AsmPrinter *AP, DwarfDebug &DD, StringRef Pref,
22 : Asm(AP), DD(DD), StrPool(DA, *Asm, Pref) {}
24 DwarfFile::~DwarfFile() {}
26 // Define a unique number for the abbreviation.
28 void DwarfFile::assignAbbrevNumber(DIEAbbrev &Abbrev) {
29 // Check the set for priors.
30 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
32 // If it's newly added.
33 if (InSet == &Abbrev) {
34 // Add to abbreviation list.
35 Abbreviations.push_back(&Abbrev);
37 // Assign the vector position + 1 as its number.
38 Abbrev.setNumber(Abbreviations.size());
40 // Assign existing abbreviation number.
41 Abbrev.setNumber(InSet->getNumber());
45 void DwarfFile::addUnit(std::unique_ptr<DwarfUnit> U) {
46 CUs.push_back(std::move(U));
49 // Emit the various dwarf units to the unit section USection with
50 // the abbreviations going into ASection.
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);
57 TheU->emitHeader(ASectionSym);
63 // Compute the size and offset for each DIE.
64 void DwarfFile::computeSizeAndOffsets() {
65 // Offset from the first CU in the debug info section is 0 initially.
66 unsigned SecOffset = 0;
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);
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
77 // EndOffset here is CU-relative, after laying out
79 unsigned EndOffset = computeSizeAndOffset(TheU->getUnitDie(), Offset);
80 SecOffset += EndOffset;
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.
85 unsigned DwarfFile::computeSizeAndOffset(DIE &Die, unsigned Offset) {
86 // Record the abbreviation.
87 assignAbbrevNumber(Die.getAbbrev());
89 // Get the abbreviation for this DIE.
90 const DIEAbbrev &Abbrev = Die.getAbbrev();
93 Die.setOffset(Offset);
95 // Start the size with the size of abbreviation code.
96 Offset += getULEB128Size(Die.getAbbrevNumber());
98 const SmallVectorImpl<DIEValue *> &Values = Die.getValues();
99 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
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());
107 const auto &Children = Die.getChildren();
109 // Size the DIE children if any.
110 if (!Children.empty()) {
111 assert(Abbrev.hasChildren() && "Children flag not set");
113 for (auto &Child : Children)
114 Offset = computeSizeAndOffset(*Child, Offset);
116 // End of children marker.
117 Offset += sizeof(int8_t);
120 Die.setSize(Offset - Die.getOffset());
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);
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");
134 // Emit the abbreviations data.
138 // Mark end of abbreviations.
139 Asm->EmitULEB128(0, "EOM(3)");
143 // Emit strings into a string section.
144 void DwarfFile::emitStrings(const MCSection *StrSection,
145 const MCSection *OffsetSection) {
146 StrPool.emit(*Asm, StrSection, OffsetSection);
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)
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
168 // A later indexed parameter has been found, insert immediately before it.
171 assert(CurNum != ArgNum && "Duplicate argument");