blob: e9df3854e780b0663d617716f38e5987bfe2ed85 [file] [log] [blame]
David Blaikie85f80d72014-04-23 18:54:00 +00001//===-- llvm/CodeGen/DwarfFile.cpp - Dwarf Debug Framework ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "DwarfFile.h"
11
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"
19
20namespace llvm {
David Blaikie2b22b1e2014-10-23 00:16:03 +000021DwarfFile::DwarfFile(AsmPrinter *AP, DwarfDebug &DD, StringRef Pref,
22 BumpPtrAllocator &DA)
David Blaikiebcfa8a52014-10-23 16:12:58 +000023 : Asm(AP), DD(DD), StrPool(DA, *Asm, Pref) {}
David Blaikie85f80d72014-04-23 18:54:00 +000024
David Blaikie05e736f2014-04-23 19:44:08 +000025DwarfFile::~DwarfFile() {}
David Blaikie85f80d72014-04-23 18:54:00 +000026
David Blaikie85f80d72014-04-23 18:54:00 +000027// Define a unique number for the abbreviation.
28//
29void DwarfFile::assignAbbrevNumber(DIEAbbrev &Abbrev) {
30 // Check the set for priors.
31 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
32
33 // If it's newly added.
34 if (InSet == &Abbrev) {
35 // Add to abbreviation list.
36 Abbreviations.push_back(&Abbrev);
37
38 // Assign the vector position + 1 as its number.
39 Abbrev.setNumber(Abbreviations.size());
40 } else {
41 // Assign existing abbreviation number.
42 Abbrev.setNumber(InSet->getNumber());
43 }
44}
45
46void DwarfFile::addUnit(std::unique_ptr<DwarfUnit> U) {
47 CUs.push_back(std::move(U));
48}
49
50// Emit the various dwarf units to the unit section USection with
51// the abbreviations going into ASection.
David Blaikie2b22b1e2014-10-23 00:16:03 +000052void DwarfFile::emitUnits(const MCSymbol *ASectionSym) {
David Blaikie85f80d72014-04-23 18:54:00 +000053 for (const auto &TheU : CUs) {
David Blaikieadcde362014-04-25 18:35:57 +000054 DIE &Die = TheU->getUnitDie();
David Blaikie85f80d72014-04-23 18:54:00 +000055 const MCSection *USection = TheU->getSection();
56 Asm->OutStreamer.SwitchSection(USection);
57
58 // Emit the compile units header.
59 Asm->OutStreamer.EmitLabel(TheU->getLabelBegin());
60
61 // Emit size of content not including length itself
62 Asm->OutStreamer.AddComment("Length of Unit");
David Blaikieadcde362014-04-25 18:35:57 +000063 Asm->EmitInt32(TheU->getHeaderSize() + Die.getSize());
David Blaikie85f80d72014-04-23 18:54:00 +000064
65 TheU->emitHeader(ASectionSym);
66
David Blaikie2b22b1e2014-10-23 00:16:03 +000067 DD.emitDIE(Die);
David Blaikie85f80d72014-04-23 18:54:00 +000068 Asm->OutStreamer.EmitLabel(TheU->getLabelEnd());
69 }
70}
David Blaikief2999472014-10-23 00:16:05 +000071
David Blaikie85f80d72014-04-23 18:54:00 +000072// Compute the size and offset for each DIE.
73void DwarfFile::computeSizeAndOffsets() {
74 // Offset from the first CU in the debug info section is 0 initially.
75 unsigned SecOffset = 0;
76
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);
81
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
85
86 // EndOffset here is CU-relative, after laying out
87 // all of the CU DIE.
David Blaikieadcde362014-04-25 18:35:57 +000088 unsigned EndOffset = computeSizeAndOffset(TheU->getUnitDie(), Offset);
David Blaikie85f80d72014-04-23 18:54:00 +000089 SecOffset += EndOffset;
90 }
91}
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.
94unsigned DwarfFile::computeSizeAndOffset(DIE &Die, unsigned Offset) {
95 // Record the abbreviation.
96 assignAbbrevNumber(Die.getAbbrev());
97
98 // Get the abbreviation for this DIE.
99 const DIEAbbrev &Abbrev = Die.getAbbrev();
100
101 // Set DIE offset
102 Die.setOffset(Offset);
103
104 // Start the size with the size of abbreviation code.
105 Offset += getULEB128Size(Die.getAbbrevNumber());
106
107 const SmallVectorImpl<DIEValue *> &Values = Die.getValues();
108 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
109
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());
114
115 // Get the children.
116 const auto &Children = Die.getChildren();
117
118 // Size the DIE children if any.
119 if (!Children.empty()) {
120 assert(Abbrev.hasChildren() && "Children flag not set");
121
122 for (auto &Child : Children)
123 Offset = computeSizeAndOffset(*Child, Offset);
124
125 // End of children marker.
126 Offset += sizeof(int8_t);
127 }
128
129 Die.setSize(Offset - Die.getOffset());
130 return Offset;
131}
132void 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);
137
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");
142
143 // Emit the abbreviations data.
144 Abbrev->Emit(Asm);
145 }
146
147 // Mark end of abbreviations.
148 Asm->EmitULEB128(0, "EOM(3)");
149 }
150}
151
152// Emit strings into a string section.
153void DwarfFile::emitStrings(const MCSection *StrSection,
David Blaikie6741bb02014-09-11 21:12:48 +0000154 const MCSection *OffsetSection) {
155 StrPool.emit(*Asm, StrSection, OffsetSection);
David Blaikie85f80d72014-04-23 18:54:00 +0000156}
David Blaikief2999472014-10-23 00:16:05 +0000157
David Blaikief4c504e2014-10-24 00:43:47 +0000158void DwarfFile::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
David Blaikie3b5c8402014-10-23 22:04:30 +0000159 SmallVectorImpl<DbgVariable *> &Vars = DD.getScopeVariables()[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)
165 //
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
174 // before it.
175 if (CurNum == 0)
176 break;
177 // A later indexed parameter has been found, insert immediately before it.
178 if (CurNum > ArgNum)
179 break;
David Blaikie1dd573d2014-10-23 22:27:50 +0000180 assert(CurNum != ArgNum);
David Blaikie3b5c8402014-10-23 22:04:30 +0000181 ++I;
182 }
183 Vars.insert(I, Var);
184 return;
185 }
186
187 Vars.push_back(Var);
188}
David Blaikie85f80d72014-04-23 18:54:00 +0000189}