blob: 3988f0def3176c3ce40b9685d8604f1d9aa184c8 [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"
David Blaikie85f80d72014-04-23 18:54:00 +000011#include "DwarfDebug.h"
12#include "DwarfUnit.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000013#include "llvm/ADT/STLExtras.h"
14#include "llvm/IR/DataLayout.h"
David Blaikie85f80d72014-04-23 18:54:00 +000015#include "llvm/MC/MCStreamer.h"
16#include "llvm/Support/LEB128.h"
David Blaikie85f80d72014-04-23 18:54:00 +000017#include "llvm/Target/TargetLoweringObjectFile.h"
18
19namespace llvm {
David Blaikie2b22b1e2014-10-23 00:16:03 +000020DwarfFile::DwarfFile(AsmPrinter *AP, DwarfDebug &DD, StringRef Pref,
21 BumpPtrAllocator &DA)
David Blaikiebcfa8a52014-10-23 16:12:58 +000022 : Asm(AP), DD(DD), StrPool(DA, *Asm, Pref) {}
David Blaikie85f80d72014-04-23 18:54:00 +000023
David Blaikie05e736f2014-04-23 19:44:08 +000024DwarfFile::~DwarfFile() {}
David Blaikie85f80d72014-04-23 18:54:00 +000025
David Blaikie85f80d72014-04-23 18:54:00 +000026// Define a unique number for the abbreviation.
27//
28void DwarfFile::assignAbbrevNumber(DIEAbbrev &Abbrev) {
29 // Check the set for priors.
30 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
31
32 // If it's newly added.
33 if (InSet == &Abbrev) {
34 // Add to abbreviation list.
35 Abbreviations.push_back(&Abbrev);
36
37 // Assign the vector position + 1 as its number.
38 Abbrev.setNumber(Abbreviations.size());
39 } else {
40 // Assign existing abbreviation number.
41 Abbrev.setNumber(InSet->getNumber());
42 }
43}
44
45void DwarfFile::addUnit(std::unique_ptr<DwarfUnit> U) {
46 CUs.push_back(std::move(U));
47}
48
49// Emit the various dwarf units to the unit section USection with
50// the abbreviations going into ASection.
David Blaikie2b22b1e2014-10-23 00:16:03 +000051void DwarfFile::emitUnits(const MCSymbol *ASectionSym) {
David Blaikie85f80d72014-04-23 18:54:00 +000052 for (const auto &TheU : CUs) {
David Blaikieadcde362014-04-25 18:35:57 +000053 DIE &Die = TheU->getUnitDie();
David Blaikie85f80d72014-04-23 18:54:00 +000054 const MCSection *USection = TheU->getSection();
55 Asm->OutStreamer.SwitchSection(USection);
56
David Blaikie85f80d72014-04-23 18:54:00 +000057 TheU->emitHeader(ASectionSym);
58
David Blaikie2b22b1e2014-10-23 00:16:03 +000059 DD.emitDIE(Die);
David Blaikie85f80d72014-04-23 18:54:00 +000060 }
61}
David Blaikief2999472014-10-23 00:16:05 +000062
David Blaikie85f80d72014-04-23 18:54:00 +000063// Compute the size and offset for each DIE.
64void DwarfFile::computeSizeAndOffsets() {
65 // Offset from the first CU in the debug info section is 0 initially.
66 unsigned SecOffset = 0;
67
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);
72
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
76
77 // EndOffset here is CU-relative, after laying out
78 // all of the CU DIE.
David Blaikieadcde362014-04-25 18:35:57 +000079 unsigned EndOffset = computeSizeAndOffset(TheU->getUnitDie(), Offset);
David Blaikie85f80d72014-04-23 18:54:00 +000080 SecOffset += EndOffset;
81 }
82}
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.
85unsigned DwarfFile::computeSizeAndOffset(DIE &Die, unsigned Offset) {
86 // Record the abbreviation.
87 assignAbbrevNumber(Die.getAbbrev());
88
89 // Get the abbreviation for this DIE.
90 const DIEAbbrev &Abbrev = Die.getAbbrev();
91
92 // Set DIE offset
93 Die.setOffset(Offset);
94
95 // Start the size with the size of abbreviation code.
96 Offset += getULEB128Size(Die.getAbbrevNumber());
97
98 const SmallVectorImpl<DIEValue *> &Values = Die.getValues();
99 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
100
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());
105
106 // Get the children.
107 const auto &Children = Die.getChildren();
108
109 // Size the DIE children if any.
110 if (!Children.empty()) {
111 assert(Abbrev.hasChildren() && "Children flag not set");
112
113 for (auto &Child : Children)
114 Offset = computeSizeAndOffset(*Child, Offset);
115
116 // End of children marker.
117 Offset += sizeof(int8_t);
118 }
119
120 Die.setSize(Offset - Die.getOffset());
121 return Offset;
122}
123void 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);
128
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");
133
134 // Emit the abbreviations data.
135 Abbrev->Emit(Asm);
136 }
137
138 // Mark end of abbreviations.
139 Asm->EmitULEB128(0, "EOM(3)");
140 }
141}
142
143// Emit strings into a string section.
144void DwarfFile::emitStrings(const MCSection *StrSection,
David Blaikie6741bb02014-09-11 21:12:48 +0000145 const MCSection *OffsetSection) {
146 StrPool.emit(*Asm, StrSection, OffsetSection);
David Blaikie85f80d72014-04-23 18:54:00 +0000147}
David Blaikief2999472014-10-23 00:16:05 +0000148
Adrian Prantlca7e4702015-02-10 23:18:28 +0000149bool DwarfFile::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
David Blaikie80e5b1e2014-10-24 17:57:34 +0000150 SmallVectorImpl<DbgVariable *> &Vars = ScopeVariables[LS];
David Blaikie3b5c8402014-10-23 22:04:30 +0000151 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)
156 //
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
165 // before it.
166 if (CurNum == 0)
167 break;
168 // A later indexed parameter has been found, insert immediately before it.
169 if (CurNum > ArgNum)
170 break;
Adrian Prantlca7e4702015-02-10 23:18:28 +0000171 if (CurNum == ArgNum) {
172 (*I)->addMMIEntry(*Var);
173 return false;
174 }
David Blaikie3b5c8402014-10-23 22:04:30 +0000175 ++I;
176 }
177 Vars.insert(I, Var);
Adrian Prantlca7e4702015-02-10 23:18:28 +0000178 return true;
David Blaikie3b5c8402014-10-23 22:04:30 +0000179 }
180
181 Vars.push_back(Var);
Adrian Prantlca7e4702015-02-10 23:18:28 +0000182 return true;
David Blaikie3b5c8402014-10-23 22:04:30 +0000183}
David Blaikie85f80d72014-04-23 18:54:00 +0000184}