blob: 3179ab154202c49e722997fbf60dcfcb1f81bcb3 [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"
Peter Collingbourne7c384cc2016-02-11 19:57:46 +000011#include "DwarfCompileUnit.h"
David Blaikie85f80d72014-04-23 18:54:00 +000012#include "DwarfDebug.h"
13#include "DwarfUnit.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000014#include "llvm/ADT/STLExtras.h"
15#include "llvm/IR/DataLayout.h"
David Blaikie85f80d72014-04-23 18:54:00 +000016#include "llvm/MC/MCStreamer.h"
17#include "llvm/Support/LEB128.h"
David Blaikie85f80d72014-04-23 18:54:00 +000018#include "llvm/Target/TargetLoweringObjectFile.h"
19
20namespace llvm {
Frederic Riss9412d632015-03-04 02:30:17 +000021DwarfFile::DwarfFile(AsmPrinter *AP, StringRef Pref, BumpPtrAllocator &DA)
22 : Asm(AP), StrPool(DA, *Asm, Pref) {}
David Blaikie85f80d72014-04-23 18:54:00 +000023
Benjamin Kramer5188a2a2015-05-28 12:55:43 +000024DwarfFile::~DwarfFile() {
25 for (DIEAbbrev *Abbrev : Abbreviations)
26 Abbrev->~DIEAbbrev();
27}
David Blaikie85f80d72014-04-23 18:54:00 +000028
David Blaikie85f80d72014-04-23 18:54:00 +000029// Define a unique number for the abbreviation.
30//
Duncan P. N. Exon Smith815a6eb52015-05-27 22:31:41 +000031DIEAbbrev &DwarfFile::assignAbbrevNumber(DIE &Die) {
32 FoldingSetNodeID ID;
33 DIEAbbrev Abbrev = Die.generateAbbrev();
34 Abbrev.Profile(ID);
David Blaikie85f80d72014-04-23 18:54:00 +000035
Duncan P. N. Exon Smith815a6eb52015-05-27 22:31:41 +000036 void *InsertPos;
37 if (DIEAbbrev *Existing =
38 AbbreviationsSet.FindNodeOrInsertPos(ID, InsertPos)) {
39 Die.setAbbrevNumber(Existing->getNumber());
40 return *Existing;
David Blaikie85f80d72014-04-23 18:54:00 +000041 }
Duncan P. N. Exon Smith815a6eb52015-05-27 22:31:41 +000042
43 // Move the abbreviation to the heap and assign a number.
44 DIEAbbrev *New = new (AbbrevAllocator) DIEAbbrev(std::move(Abbrev));
45 Abbreviations.push_back(New);
46 New->setNumber(Abbreviations.size());
47 Die.setAbbrevNumber(Abbreviations.size());
48
49 // Store it for lookup.
50 AbbreviationsSet.InsertNode(New, InsertPos);
51 return *New;
David Blaikie85f80d72014-04-23 18:54:00 +000052}
53
Peter Collingbourne7c384cc2016-02-11 19:57:46 +000054void DwarfFile::addUnit(std::unique_ptr<DwarfCompileUnit> U) {
David Blaikie85f80d72014-04-23 18:54:00 +000055 CUs.push_back(std::move(U));
56}
57
58// Emit the various dwarf units to the unit section USection with
59// the abbreviations going into ASection.
Rafael Espindola063d7252015-03-10 16:58:10 +000060void DwarfFile::emitUnits(bool UseOffsets) {
Peter Collingbourne7c384cc2016-02-11 19:57:46 +000061 for (const auto &TheU : CUs)
62 emitUnit(TheU.get(), UseOffsets);
63}
David Blaikie85f80d72014-04-23 18:54:00 +000064
Peter Collingbourne7c384cc2016-02-11 19:57:46 +000065void DwarfFile::emitUnit(DwarfUnit *TheU, bool UseOffsets) {
66 DIE &Die = TheU->getUnitDie();
67 MCSection *USection = TheU->getSection();
68 Asm->OutStreamer->SwitchSection(USection);
David Blaikie85f80d72014-04-23 18:54:00 +000069
Peter Collingbourne7c384cc2016-02-11 19:57:46 +000070 TheU->emitHeader(UseOffsets);
71
72 Asm->emitDwarfDIE(Die);
David Blaikie85f80d72014-04-23 18:54:00 +000073}
David Blaikief2999472014-10-23 00:16:05 +000074
David Blaikie85f80d72014-04-23 18:54:00 +000075// Compute the size and offset for each DIE.
76void DwarfFile::computeSizeAndOffsets() {
77 // Offset from the first CU in the debug info section is 0 initially.
78 unsigned SecOffset = 0;
79
80 // Iterate over each compile unit and set the size and offsets for each
81 // DIE within each compile unit. All offsets are CU relative.
82 for (const auto &TheU : CUs) {
Greg Clayton35630c32016-12-01 18:56:29 +000083 TheU->setDebugSectionOffset(SecOffset);
Peter Collingbourne7c384cc2016-02-11 19:57:46 +000084 SecOffset += computeSizeAndOffsetsForUnit(TheU.get());
David Blaikie85f80d72014-04-23 18:54:00 +000085 }
86}
Peter Collingbourne7c384cc2016-02-11 19:57:46 +000087
88unsigned DwarfFile::computeSizeAndOffsetsForUnit(DwarfUnit *TheU) {
89 // CU-relative offset is reset to 0 here.
90 unsigned Offset = sizeof(int32_t) + // Length of Unit Info
91 TheU->getHeaderSize(); // Unit-specific headers
92
93 // The return value here is CU-relative, after laying out
94 // all of the CU DIE.
95 return computeSizeAndOffset(TheU->getUnitDie(), Offset);
96}
97
David Blaikie85f80d72014-04-23 18:54:00 +000098// Compute the size and offset of a DIE. The offset is relative to start of the
99// CU. It returns the offset after laying out the DIE.
100unsigned DwarfFile::computeSizeAndOffset(DIE &Die, unsigned Offset) {
101 // Record the abbreviation.
Duncan P. N. Exon Smith815a6eb52015-05-27 22:31:41 +0000102 const DIEAbbrev &Abbrev = assignAbbrevNumber(Die);
David Blaikie85f80d72014-04-23 18:54:00 +0000103
104 // Set DIE offset
105 Die.setOffset(Offset);
106
107 // Start the size with the size of abbreviation code.
108 Offset += getULEB128Size(Die.getAbbrevNumber());
109
David Blaikie85f80d72014-04-23 18:54:00 +0000110 // Size the DIE attribute values.
Duncan P. N. Exon Smith88a8fc52015-05-27 22:44:06 +0000111 for (const auto &V : Die.values())
David Blaikie85f80d72014-04-23 18:54:00 +0000112 // Size attribute value.
Duncan P. N. Exon Smith9dbb5012015-06-24 18:48:11 +0000113 Offset += V.SizeOf(Asm);
David Blaikie85f80d72014-04-23 18:54:00 +0000114
David Blaikie85f80d72014-04-23 18:54:00 +0000115 // Size the DIE children if any.
Duncan P. N. Exon Smith8d3197f2015-05-28 19:56:34 +0000116 if (Die.hasChildren()) {
Duncan P. N. Exon Smitha68b8802015-05-27 23:02:36 +0000117 (void)Abbrev;
David Blaikie85f80d72014-04-23 18:54:00 +0000118 assert(Abbrev.hasChildren() && "Children flag not set");
119
Duncan P. N. Exon Smith8d3197f2015-05-28 19:56:34 +0000120 for (auto &Child : Die.children())
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000121 Offset = computeSizeAndOffset(Child, Offset);
David Blaikie85f80d72014-04-23 18:54:00 +0000122
123 // End of children marker.
124 Offset += sizeof(int8_t);
125 }
126
127 Die.setSize(Offset - Die.getOffset());
128 return Offset;
129}
Frederic Riss9412d632015-03-04 02:30:17 +0000130
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000131void DwarfFile::emitAbbrevs(MCSection *Section) {
David Blaikie85f80d72014-04-23 18:54:00 +0000132 // Check to see if it is worth the effort.
133 if (!Abbreviations.empty()) {
134 // Start the debug abbrev section.
Lang Hames9ff69c82015-04-24 19:11:51 +0000135 Asm->OutStreamer->SwitchSection(Section);
Frederic Riss9412d632015-03-04 02:30:17 +0000136 Asm->emitDwarfAbbrevs(Abbreviations);
David Blaikie85f80d72014-04-23 18:54:00 +0000137 }
138}
139
140// Emit strings into a string section.
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000141void DwarfFile::emitStrings(MCSection *StrSection, MCSection *OffsetSection) {
David Blaikie6741bb02014-09-11 21:12:48 +0000142 StrPool.emit(*Asm, StrSection, OffsetSection);
David Blaikie85f80d72014-04-23 18:54:00 +0000143}
David Blaikief2999472014-10-23 00:16:05 +0000144
Adrian Prantlca7e4702015-02-10 23:18:28 +0000145bool DwarfFile::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
David Blaikie80e5b1e2014-10-24 17:57:34 +0000146 SmallVectorImpl<DbgVariable *> &Vars = ScopeVariables[LS];
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000147 const DILocalVariable *DV = Var->getVariable();
David Blaikie3b5c8402014-10-23 22:04:30 +0000148 // Variables with positive arg numbers are parameters.
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000149 if (unsigned ArgNum = DV->getArg()) {
David Blaikie3b5c8402014-10-23 22:04:30 +0000150 // Keep all parameters in order at the start of the variable list to ensure
151 // function types are correct (no out-of-order parameters)
152 //
153 // This could be improved by only doing it for optimized builds (unoptimized
154 // builds have the right order to begin with), searching from the back (this
155 // would catch the unoptimized case quickly), or doing a binary search
156 // rather than linear search.
157 auto I = Vars.begin();
158 while (I != Vars.end()) {
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000159 unsigned CurNum = (*I)->getVariable()->getArg();
David Blaikie3b5c8402014-10-23 22:04:30 +0000160 // A local (non-parameter) variable has been found, insert immediately
161 // before it.
162 if (CurNum == 0)
163 break;
164 // A later indexed parameter has been found, insert immediately before it.
165 if (CurNum > ArgNum)
166 break;
Adrian Prantlca7e4702015-02-10 23:18:28 +0000167 if (CurNum == ArgNum) {
168 (*I)->addMMIEntry(*Var);
169 return false;
170 }
David Blaikie3b5c8402014-10-23 22:04:30 +0000171 ++I;
172 }
173 Vars.insert(I, Var);
Adrian Prantlca7e4702015-02-10 23:18:28 +0000174 return true;
David Blaikie3b5c8402014-10-23 22:04:30 +0000175 }
176
177 Vars.push_back(Var);
Adrian Prantlca7e4702015-02-10 23:18:28 +0000178 return true;
David Blaikie3b5c8402014-10-23 22:04:30 +0000179}
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000180}