blob: 1060366a8ba313671e1e2cfba5c5af6b189c5ffb [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 {
Frederic Riss9412d632015-03-04 02:30:17 +000020DwarfFile::DwarfFile(AsmPrinter *AP, StringRef Pref, BumpPtrAllocator &DA)
21 : Asm(AP), StrPool(DA, *Asm, Pref) {}
David Blaikie85f80d72014-04-23 18:54:00 +000022
Benjamin Kramer5188a2a2015-05-28 12:55:43 +000023DwarfFile::~DwarfFile() {
24 for (DIEAbbrev *Abbrev : Abbreviations)
25 Abbrev->~DIEAbbrev();
26}
David Blaikie85f80d72014-04-23 18:54:00 +000027
David Blaikie85f80d72014-04-23 18:54:00 +000028// Define a unique number for the abbreviation.
29//
Duncan P. N. Exon Smith815a6eb52015-05-27 22:31:41 +000030DIEAbbrev &DwarfFile::assignAbbrevNumber(DIE &Die) {
31 FoldingSetNodeID ID;
32 DIEAbbrev Abbrev = Die.generateAbbrev();
33 Abbrev.Profile(ID);
David Blaikie85f80d72014-04-23 18:54:00 +000034
Duncan P. N. Exon Smith815a6eb52015-05-27 22:31:41 +000035 void *InsertPos;
36 if (DIEAbbrev *Existing =
37 AbbreviationsSet.FindNodeOrInsertPos(ID, InsertPos)) {
38 Die.setAbbrevNumber(Existing->getNumber());
39 return *Existing;
David Blaikie85f80d72014-04-23 18:54:00 +000040 }
Duncan P. N. Exon Smith815a6eb52015-05-27 22:31:41 +000041
42 // Move the abbreviation to the heap and assign a number.
43 DIEAbbrev *New = new (AbbrevAllocator) DIEAbbrev(std::move(Abbrev));
44 Abbreviations.push_back(New);
45 New->setNumber(Abbreviations.size());
46 Die.setAbbrevNumber(Abbreviations.size());
47
48 // Store it for lookup.
49 AbbreviationsSet.InsertNode(New, InsertPos);
50 return *New;
David Blaikie85f80d72014-04-23 18:54:00 +000051}
52
53void DwarfFile::addUnit(std::unique_ptr<DwarfUnit> U) {
54 CUs.push_back(std::move(U));
55}
56
57// Emit the various dwarf units to the unit section USection with
58// the abbreviations going into ASection.
Rafael Espindola063d7252015-03-10 16:58:10 +000059void DwarfFile::emitUnits(bool UseOffsets) {
David Blaikie85f80d72014-04-23 18:54:00 +000060 for (const auto &TheU : CUs) {
David Blaikieadcde362014-04-25 18:35:57 +000061 DIE &Die = TheU->getUnitDie();
Rafael Espindola0709a7b2015-05-21 19:20:38 +000062 MCSection *USection = TheU->getSection();
Lang Hames9ff69c82015-04-24 19:11:51 +000063 Asm->OutStreamer->SwitchSection(USection);
David Blaikie85f80d72014-04-23 18:54:00 +000064
Rafael Espindola063d7252015-03-10 16:58:10 +000065 TheU->emitHeader(UseOffsets);
David Blaikie85f80d72014-04-23 18:54:00 +000066
Frederic Riss9412d632015-03-04 02:30:17 +000067 Asm->emitDwarfDIE(Die);
David Blaikie85f80d72014-04-23 18:54:00 +000068 }
69}
David Blaikief2999472014-10-23 00:16:05 +000070
David Blaikie85f80d72014-04-23 18:54:00 +000071// Compute the size and offset for each DIE.
72void DwarfFile::computeSizeAndOffsets() {
73 // Offset from the first CU in the debug info section is 0 initially.
74 unsigned SecOffset = 0;
75
76 // Iterate over each compile unit and set the size and offsets for each
77 // DIE within each compile unit. All offsets are CU relative.
78 for (const auto &TheU : CUs) {
79 TheU->setDebugInfoOffset(SecOffset);
80
81 // CU-relative offset is reset to 0 here.
82 unsigned Offset = sizeof(int32_t) + // Length of Unit Info
83 TheU->getHeaderSize(); // Unit-specific headers
84
85 // EndOffset here is CU-relative, after laying out
86 // all of the CU DIE.
David Blaikieadcde362014-04-25 18:35:57 +000087 unsigned EndOffset = computeSizeAndOffset(TheU->getUnitDie(), Offset);
David Blaikie85f80d72014-04-23 18:54:00 +000088 SecOffset += EndOffset;
89 }
90}
91// Compute the size and offset of a DIE. The offset is relative to start of the
92// CU. It returns the offset after laying out the DIE.
93unsigned DwarfFile::computeSizeAndOffset(DIE &Die, unsigned Offset) {
94 // Record the abbreviation.
Duncan P. N. Exon Smith815a6eb52015-05-27 22:31:41 +000095 const DIEAbbrev &Abbrev = assignAbbrevNumber(Die);
David Blaikie85f80d72014-04-23 18:54:00 +000096
97 // Set DIE offset
98 Die.setOffset(Offset);
99
100 // Start the size with the size of abbreviation code.
101 Offset += getULEB128Size(Die.getAbbrevNumber());
102
David Blaikie85f80d72014-04-23 18:54:00 +0000103 // Size the DIE attribute values.
Duncan P. N. Exon Smith88a8fc52015-05-27 22:44:06 +0000104 for (const auto &V : Die.values())
David Blaikie85f80d72014-04-23 18:54:00 +0000105 // Size attribute value.
Duncan P. N. Exon Smith88a8fc52015-05-27 22:44:06 +0000106 Offset += V.SizeOf(Asm, V.getForm());
David Blaikie85f80d72014-04-23 18:54:00 +0000107
108 // Get the children.
109 const auto &Children = Die.getChildren();
110
111 // Size the DIE children if any.
112 if (!Children.empty()) {
Duncan P. N. Exon Smitha68b8802015-05-27 23:02:36 +0000113 (void)Abbrev;
David Blaikie85f80d72014-04-23 18:54:00 +0000114 assert(Abbrev.hasChildren() && "Children flag not set");
115
116 for (auto &Child : Children)
117 Offset = computeSizeAndOffset(*Child, Offset);
118
119 // End of children marker.
120 Offset += sizeof(int8_t);
121 }
122
123 Die.setSize(Offset - Die.getOffset());
124 return Offset;
125}
Frederic Riss9412d632015-03-04 02:30:17 +0000126
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000127void DwarfFile::emitAbbrevs(MCSection *Section) {
David Blaikie85f80d72014-04-23 18:54:00 +0000128 // Check to see if it is worth the effort.
129 if (!Abbreviations.empty()) {
130 // Start the debug abbrev section.
Lang Hames9ff69c82015-04-24 19:11:51 +0000131 Asm->OutStreamer->SwitchSection(Section);
Frederic Riss9412d632015-03-04 02:30:17 +0000132 Asm->emitDwarfAbbrevs(Abbreviations);
David Blaikie85f80d72014-04-23 18:54:00 +0000133 }
134}
135
136// Emit strings into a string section.
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000137void DwarfFile::emitStrings(MCSection *StrSection, MCSection *OffsetSection) {
David Blaikie6741bb02014-09-11 21:12:48 +0000138 StrPool.emit(*Asm, StrSection, OffsetSection);
David Blaikie85f80d72014-04-23 18:54:00 +0000139}
David Blaikief2999472014-10-23 00:16:05 +0000140
Adrian Prantlca7e4702015-02-10 23:18:28 +0000141bool DwarfFile::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
David Blaikie80e5b1e2014-10-24 17:57:34 +0000142 SmallVectorImpl<DbgVariable *> &Vars = ScopeVariables[LS];
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000143 const DILocalVariable *DV = Var->getVariable();
David Blaikie3b5c8402014-10-23 22:04:30 +0000144 // Variables with positive arg numbers are parameters.
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000145 if (unsigned ArgNum = DV->getArg()) {
David Blaikie3b5c8402014-10-23 22:04:30 +0000146 // Keep all parameters in order at the start of the variable list to ensure
147 // function types are correct (no out-of-order parameters)
148 //
149 // This could be improved by only doing it for optimized builds (unoptimized
150 // builds have the right order to begin with), searching from the back (this
151 // would catch the unoptimized case quickly), or doing a binary search
152 // rather than linear search.
153 auto I = Vars.begin();
154 while (I != Vars.end()) {
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000155 unsigned CurNum = (*I)->getVariable()->getArg();
David Blaikie3b5c8402014-10-23 22:04:30 +0000156 // A local (non-parameter) variable has been found, insert immediately
157 // before it.
158 if (CurNum == 0)
159 break;
160 // A later indexed parameter has been found, insert immediately before it.
161 if (CurNum > ArgNum)
162 break;
Adrian Prantlca7e4702015-02-10 23:18:28 +0000163 if (CurNum == ArgNum) {
164 (*I)->addMMIEntry(*Var);
165 return false;
166 }
David Blaikie3b5c8402014-10-23 22:04:30 +0000167 ++I;
168 }
169 Vars.insert(I, Var);
Adrian Prantlca7e4702015-02-10 23:18:28 +0000170 return true;
David Blaikie3b5c8402014-10-23 22:04:30 +0000171 }
172
173 Vars.push_back(Var);
Adrian Prantlca7e4702015-02-10 23:18:28 +0000174 return true;
David Blaikie3b5c8402014-10-23 22:04:30 +0000175}
David Blaikie85f80d72014-04-23 18:54:00 +0000176}