blob: a9e9044a4d60c6588c59915ceecda1090a47159e [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//
Duncan P. N. Exon Smith815a6eb52015-05-27 22:31:41 +000027DIEAbbrev &DwarfFile::assignAbbrevNumber(DIE &Die) {
28 FoldingSetNodeID ID;
29 DIEAbbrev Abbrev = Die.generateAbbrev();
30 Abbrev.Profile(ID);
David Blaikie85f80d72014-04-23 18:54:00 +000031
Duncan P. N. Exon Smith815a6eb52015-05-27 22:31:41 +000032 void *InsertPos;
33 if (DIEAbbrev *Existing =
34 AbbreviationsSet.FindNodeOrInsertPos(ID, InsertPos)) {
35 Die.setAbbrevNumber(Existing->getNumber());
36 return *Existing;
David Blaikie85f80d72014-04-23 18:54:00 +000037 }
Duncan P. N. Exon Smith815a6eb52015-05-27 22:31:41 +000038
39 // Move the abbreviation to the heap and assign a number.
40 DIEAbbrev *New = new (AbbrevAllocator) DIEAbbrev(std::move(Abbrev));
41 Abbreviations.push_back(New);
42 New->setNumber(Abbreviations.size());
43 Die.setAbbrevNumber(Abbreviations.size());
44
45 // Store it for lookup.
46 AbbreviationsSet.InsertNode(New, InsertPos);
47 return *New;
David Blaikie85f80d72014-04-23 18:54:00 +000048}
49
50void DwarfFile::addUnit(std::unique_ptr<DwarfUnit> U) {
51 CUs.push_back(std::move(U));
52}
53
54// Emit the various dwarf units to the unit section USection with
55// the abbreviations going into ASection.
Rafael Espindola063d7252015-03-10 16:58:10 +000056void DwarfFile::emitUnits(bool UseOffsets) {
David Blaikie85f80d72014-04-23 18:54:00 +000057 for (const auto &TheU : CUs) {
David Blaikieadcde362014-04-25 18:35:57 +000058 DIE &Die = TheU->getUnitDie();
Rafael Espindola0709a7b2015-05-21 19:20:38 +000059 MCSection *USection = TheU->getSection();
Lang Hames9ff69c82015-04-24 19:11:51 +000060 Asm->OutStreamer->SwitchSection(USection);
David Blaikie85f80d72014-04-23 18:54:00 +000061
Rafael Espindola063d7252015-03-10 16:58:10 +000062 TheU->emitHeader(UseOffsets);
David Blaikie85f80d72014-04-23 18:54:00 +000063
Frederic Riss9412d632015-03-04 02:30:17 +000064 Asm->emitDwarfDIE(Die);
David Blaikie85f80d72014-04-23 18:54:00 +000065 }
66}
David Blaikief2999472014-10-23 00:16:05 +000067
David Blaikie85f80d72014-04-23 18:54:00 +000068// Compute the size and offset for each DIE.
69void DwarfFile::computeSizeAndOffsets() {
70 // Offset from the first CU in the debug info section is 0 initially.
71 unsigned SecOffset = 0;
72
73 // Iterate over each compile unit and set the size and offsets for each
74 // DIE within each compile unit. All offsets are CU relative.
75 for (const auto &TheU : CUs) {
76 TheU->setDebugInfoOffset(SecOffset);
77
78 // CU-relative offset is reset to 0 here.
79 unsigned Offset = sizeof(int32_t) + // Length of Unit Info
80 TheU->getHeaderSize(); // Unit-specific headers
81
82 // EndOffset here is CU-relative, after laying out
83 // all of the CU DIE.
David Blaikieadcde362014-04-25 18:35:57 +000084 unsigned EndOffset = computeSizeAndOffset(TheU->getUnitDie(), Offset);
David Blaikie85f80d72014-04-23 18:54:00 +000085 SecOffset += EndOffset;
86 }
87}
88// Compute the size and offset of a DIE. The offset is relative to start of the
89// CU. It returns the offset after laying out the DIE.
90unsigned DwarfFile::computeSizeAndOffset(DIE &Die, unsigned Offset) {
91 // Record the abbreviation.
Duncan P. N. Exon Smith815a6eb52015-05-27 22:31:41 +000092 const DIEAbbrev &Abbrev = assignAbbrevNumber(Die);
David Blaikie85f80d72014-04-23 18:54:00 +000093
94 // Set DIE offset
95 Die.setOffset(Offset);
96
97 // Start the size with the size of abbreviation code.
98 Offset += getULEB128Size(Die.getAbbrevNumber());
99
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000100 const SmallVectorImpl<DIEValue> &Values = Die.getValues();
David Blaikie85f80d72014-04-23 18:54:00 +0000101 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
102
103 // Size the DIE attribute values.
104 for (unsigned i = 0, N = Values.size(); i < N; ++i)
105 // Size attribute value.
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000106 Offset += Values[i].SizeOf(Asm, AbbrevData[i].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()) {
113 assert(Abbrev.hasChildren() && "Children flag not set");
114
115 for (auto &Child : Children)
116 Offset = computeSizeAndOffset(*Child, Offset);
117
118 // End of children marker.
119 Offset += sizeof(int8_t);
120 }
121
122 Die.setSize(Offset - Die.getOffset());
123 return Offset;
124}
Frederic Riss9412d632015-03-04 02:30:17 +0000125
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000126void DwarfFile::emitAbbrevs(MCSection *Section) {
David Blaikie85f80d72014-04-23 18:54:00 +0000127 // Check to see if it is worth the effort.
128 if (!Abbreviations.empty()) {
129 // Start the debug abbrev section.
Lang Hames9ff69c82015-04-24 19:11:51 +0000130 Asm->OutStreamer->SwitchSection(Section);
Frederic Riss9412d632015-03-04 02:30:17 +0000131 Asm->emitDwarfAbbrevs(Abbreviations);
David Blaikie85f80d72014-04-23 18:54:00 +0000132 }
133}
134
135// Emit strings into a string section.
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000136void DwarfFile::emitStrings(MCSection *StrSection, MCSection *OffsetSection) {
David Blaikie6741bb02014-09-11 21:12:48 +0000137 StrPool.emit(*Asm, StrSection, OffsetSection);
David Blaikie85f80d72014-04-23 18:54:00 +0000138}
David Blaikief2999472014-10-23 00:16:05 +0000139
Adrian Prantlca7e4702015-02-10 23:18:28 +0000140bool DwarfFile::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
David Blaikie80e5b1e2014-10-24 17:57:34 +0000141 SmallVectorImpl<DbgVariable *> &Vars = ScopeVariables[LS];
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000142 const DILocalVariable *DV = Var->getVariable();
David Blaikie3b5c8402014-10-23 22:04:30 +0000143 // Variables with positive arg numbers are parameters.
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000144 if (unsigned ArgNum = DV->getArg()) {
David Blaikie3b5c8402014-10-23 22:04:30 +0000145 // Keep all parameters in order at the start of the variable list to ensure
146 // function types are correct (no out-of-order parameters)
147 //
148 // This could be improved by only doing it for optimized builds (unoptimized
149 // builds have the right order to begin with), searching from the back (this
150 // would catch the unoptimized case quickly), or doing a binary search
151 // rather than linear search.
152 auto I = Vars.begin();
153 while (I != Vars.end()) {
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000154 unsigned CurNum = (*I)->getVariable()->getArg();
David Blaikie3b5c8402014-10-23 22:04:30 +0000155 // A local (non-parameter) variable has been found, insert immediately
156 // before it.
157 if (CurNum == 0)
158 break;
159 // A later indexed parameter has been found, insert immediately before it.
160 if (CurNum > ArgNum)
161 break;
Adrian Prantlca7e4702015-02-10 23:18:28 +0000162 if (CurNum == ArgNum) {
163 (*I)->addMMIEntry(*Var);
164 return false;
165 }
David Blaikie3b5c8402014-10-23 22:04:30 +0000166 ++I;
167 }
168 Vars.insert(I, Var);
Adrian Prantlca7e4702015-02-10 23:18:28 +0000169 return true;
David Blaikie3b5c8402014-10-23 22:04:30 +0000170 }
171
172 Vars.push_back(Var);
Adrian Prantlca7e4702015-02-10 23:18:28 +0000173 return true;
David Blaikie3b5c8402014-10-23 22:04:30 +0000174}
David Blaikie85f80d72014-04-23 18:54:00 +0000175}