blob: 9951c1d116d5c0bf80fb66bed3b2aeaad4d29342 [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
David Blaikie05e736f2014-04-23 19:44:08 +000023DwarfFile::~DwarfFile() {}
David Blaikie85f80d72014-04-23 18:54:00 +000024
David Blaikie85f80d72014-04-23 18:54:00 +000025// Define a unique number for the abbreviation.
26//
27void DwarfFile::assignAbbrevNumber(DIEAbbrev &Abbrev) {
28 // Check the set for priors.
29 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
30
31 // If it's newly added.
32 if (InSet == &Abbrev) {
33 // Add to abbreviation list.
34 Abbreviations.push_back(&Abbrev);
35
36 // Assign the vector position + 1 as its number.
37 Abbrev.setNumber(Abbreviations.size());
38 } else {
39 // Assign existing abbreviation number.
40 Abbrev.setNumber(InSet->getNumber());
41 }
42}
43
44void DwarfFile::addUnit(std::unique_ptr<DwarfUnit> U) {
45 CUs.push_back(std::move(U));
46}
47
48// Emit the various dwarf units to the unit section USection with
49// the abbreviations going into ASection.
Rafael Espindola063d7252015-03-10 16:58:10 +000050void DwarfFile::emitUnits(bool UseOffsets) {
David Blaikie85f80d72014-04-23 18:54:00 +000051 for (const auto &TheU : CUs) {
David Blaikieadcde362014-04-25 18:35:57 +000052 DIE &Die = TheU->getUnitDie();
Rafael Espindola0709a7b2015-05-21 19:20:38 +000053 MCSection *USection = TheU->getSection();
Lang Hames9ff69c82015-04-24 19:11:51 +000054 Asm->OutStreamer->SwitchSection(USection);
David Blaikie85f80d72014-04-23 18:54:00 +000055
Rafael Espindola063d7252015-03-10 16:58:10 +000056 TheU->emitHeader(UseOffsets);
David Blaikie85f80d72014-04-23 18:54:00 +000057
Frederic Riss9412d632015-03-04 02:30:17 +000058 Asm->emitDwarfDIE(Die);
David Blaikie85f80d72014-04-23 18:54:00 +000059 }
60}
David Blaikief2999472014-10-23 00:16:05 +000061
David Blaikie85f80d72014-04-23 18:54:00 +000062// Compute the size and offset for each DIE.
63void DwarfFile::computeSizeAndOffsets() {
64 // Offset from the first CU in the debug info section is 0 initially.
65 unsigned SecOffset = 0;
66
67 // Iterate over each compile unit and set the size and offsets for each
68 // DIE within each compile unit. All offsets are CU relative.
69 for (const auto &TheU : CUs) {
70 TheU->setDebugInfoOffset(SecOffset);
71
72 // CU-relative offset is reset to 0 here.
73 unsigned Offset = sizeof(int32_t) + // Length of Unit Info
74 TheU->getHeaderSize(); // Unit-specific headers
75
76 // EndOffset here is CU-relative, after laying out
77 // all of the CU DIE.
David Blaikieadcde362014-04-25 18:35:57 +000078 unsigned EndOffset = computeSizeAndOffset(TheU->getUnitDie(), Offset);
David Blaikie85f80d72014-04-23 18:54:00 +000079 SecOffset += EndOffset;
80 }
81}
82// Compute the size and offset of a DIE. The offset is relative to start of the
83// CU. It returns the offset after laying out the DIE.
84unsigned DwarfFile::computeSizeAndOffset(DIE &Die, unsigned Offset) {
85 // Record the abbreviation.
86 assignAbbrevNumber(Die.getAbbrev());
87
88 // Get the abbreviation for this DIE.
89 const DIEAbbrev &Abbrev = Die.getAbbrev();
90
91 // Set DIE offset
92 Die.setOffset(Offset);
93
94 // Start the size with the size of abbreviation code.
95 Offset += getULEB128Size(Die.getAbbrevNumber());
96
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +000097 const SmallVectorImpl<DIEValue> &Values = Die.getValues();
David Blaikie85f80d72014-04-23 18:54:00 +000098 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
99
100 // Size the DIE attribute values.
101 for (unsigned i = 0, N = Values.size(); i < N; ++i)
102 // Size attribute value.
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000103 Offset += Values[i].SizeOf(Asm, AbbrevData[i].getForm());
David Blaikie85f80d72014-04-23 18:54:00 +0000104
105 // Get the children.
106 const auto &Children = Die.getChildren();
107
108 // Size the DIE children if any.
109 if (!Children.empty()) {
110 assert(Abbrev.hasChildren() && "Children flag not set");
111
112 for (auto &Child : Children)
113 Offset = computeSizeAndOffset(*Child, Offset);
114
115 // End of children marker.
116 Offset += sizeof(int8_t);
117 }
118
119 Die.setSize(Offset - Die.getOffset());
120 return Offset;
121}
Frederic Riss9412d632015-03-04 02:30:17 +0000122
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000123void DwarfFile::emitAbbrevs(MCSection *Section) {
David Blaikie85f80d72014-04-23 18:54:00 +0000124 // Check to see if it is worth the effort.
125 if (!Abbreviations.empty()) {
126 // Start the debug abbrev section.
Lang Hames9ff69c82015-04-24 19:11:51 +0000127 Asm->OutStreamer->SwitchSection(Section);
Frederic Riss9412d632015-03-04 02:30:17 +0000128 Asm->emitDwarfAbbrevs(Abbreviations);
David Blaikie85f80d72014-04-23 18:54:00 +0000129 }
130}
131
132// Emit strings into a string section.
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000133void DwarfFile::emitStrings(MCSection *StrSection, MCSection *OffsetSection) {
David Blaikie6741bb02014-09-11 21:12:48 +0000134 StrPool.emit(*Asm, StrSection, OffsetSection);
David Blaikie85f80d72014-04-23 18:54:00 +0000135}
David Blaikief2999472014-10-23 00:16:05 +0000136
Adrian Prantlca7e4702015-02-10 23:18:28 +0000137bool DwarfFile::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
David Blaikie80e5b1e2014-10-24 17:57:34 +0000138 SmallVectorImpl<DbgVariable *> &Vars = ScopeVariables[LS];
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000139 const DILocalVariable *DV = Var->getVariable();
David Blaikie3b5c8402014-10-23 22:04:30 +0000140 // Variables with positive arg numbers are parameters.
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000141 if (unsigned ArgNum = DV->getArg()) {
David Blaikie3b5c8402014-10-23 22:04:30 +0000142 // Keep all parameters in order at the start of the variable list to ensure
143 // function types are correct (no out-of-order parameters)
144 //
145 // This could be improved by only doing it for optimized builds (unoptimized
146 // builds have the right order to begin with), searching from the back (this
147 // would catch the unoptimized case quickly), or doing a binary search
148 // rather than linear search.
149 auto I = Vars.begin();
150 while (I != Vars.end()) {
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000151 unsigned CurNum = (*I)->getVariable()->getArg();
David Blaikie3b5c8402014-10-23 22:04:30 +0000152 // A local (non-parameter) variable has been found, insert immediately
153 // before it.
154 if (CurNum == 0)
155 break;
156 // A later indexed parameter has been found, insert immediately before it.
157 if (CurNum > ArgNum)
158 break;
Adrian Prantlca7e4702015-02-10 23:18:28 +0000159 if (CurNum == ArgNum) {
160 (*I)->addMMIEntry(*Var);
161 return false;
162 }
David Blaikie3b5c8402014-10-23 22:04:30 +0000163 ++I;
164 }
165 Vars.insert(I, Var);
Adrian Prantlca7e4702015-02-10 23:18:28 +0000166 return true;
David Blaikie3b5c8402014-10-23 22:04:30 +0000167 }
168
169 Vars.push_back(Var);
Adrian Prantlca7e4702015-02-10 23:18:28 +0000170 return true;
David Blaikie3b5c8402014-10-23 22:04:30 +0000171}
David Blaikie85f80d72014-04-23 18:54:00 +0000172}