1 //===-- llvm/CodeGen/DwarfFile.cpp - Dwarf Debug Framework ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "DwarfFile.h"
11 #include "DwarfDebug.h"
12 #include "DwarfUnit.h"
13 #include "llvm/ADT/STLExtras.h"
14 #include "llvm/IR/DataLayout.h"
15 #include "llvm/MC/MCStreamer.h"
16 #include "llvm/Support/LEB128.h"
17 #include "llvm/Target/TargetLoweringObjectFile.h"
20 DwarfFile::DwarfFile(AsmPrinter *AP, StringRef Pref, BumpPtrAllocator &DA)
21 : Asm(AP), StrPool(DA, *Asm, Pref) {}
23 DwarfFile::~DwarfFile() {}
25 // Define a unique number for the abbreviation.
27 DIEAbbrev &DwarfFile::assignAbbrevNumber(DIE &Die) {
29 DIEAbbrev Abbrev = Die.generateAbbrev();
33 if (DIEAbbrev *Existing =
34 AbbreviationsSet.FindNodeOrInsertPos(ID, InsertPos)) {
35 Die.setAbbrevNumber(Existing->getNumber());
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());
45 // Store it for lookup.
46 AbbreviationsSet.InsertNode(New, InsertPos);
50 void DwarfFile::addUnit(std::unique_ptr<DwarfUnit> U) {
51 CUs.push_back(std::move(U));
54 // Emit the various dwarf units to the unit section USection with
55 // the abbreviations going into ASection.
56 void DwarfFile::emitUnits(bool UseOffsets) {
57 for (const auto &TheU : CUs) {
58 DIE &Die = TheU->getUnitDie();
59 MCSection *USection = TheU->getSection();
60 Asm->OutStreamer->SwitchSection(USection);
62 TheU->emitHeader(UseOffsets);
64 Asm->emitDwarfDIE(Die);
68 // Compute the size and offset for each DIE.
69 void DwarfFile::computeSizeAndOffsets() {
70 // Offset from the first CU in the debug info section is 0 initially.
71 unsigned SecOffset = 0;
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);
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
82 // EndOffset here is CU-relative, after laying out
84 unsigned EndOffset = computeSizeAndOffset(TheU->getUnitDie(), Offset);
85 SecOffset += EndOffset;
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.
90 unsigned DwarfFile::computeSizeAndOffset(DIE &Die, unsigned Offset) {
91 // Record the abbreviation.
92 const DIEAbbrev &Abbrev = assignAbbrevNumber(Die);
95 Die.setOffset(Offset);
97 // Start the size with the size of abbreviation code.
98 Offset += getULEB128Size(Die.getAbbrevNumber());
100 const SmallVectorImpl<DIEValue> &Values = Die.getValues();
101 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
103 // Size the DIE attribute values.
104 for (unsigned i = 0, N = Values.size(); i < N; ++i)
105 // Size attribute value.
106 Offset += Values[i].SizeOf(Asm, AbbrevData[i].getForm());
109 const auto &Children = Die.getChildren();
111 // Size the DIE children if any.
112 if (!Children.empty()) {
113 assert(Abbrev.hasChildren() && "Children flag not set");
115 for (auto &Child : Children)
116 Offset = computeSizeAndOffset(*Child, Offset);
118 // End of children marker.
119 Offset += sizeof(int8_t);
122 Die.setSize(Offset - Die.getOffset());
126 void DwarfFile::emitAbbrevs(MCSection *Section) {
127 // Check to see if it is worth the effort.
128 if (!Abbreviations.empty()) {
129 // Start the debug abbrev section.
130 Asm->OutStreamer->SwitchSection(Section);
131 Asm->emitDwarfAbbrevs(Abbreviations);
135 // Emit strings into a string section.
136 void DwarfFile::emitStrings(MCSection *StrSection, MCSection *OffsetSection) {
137 StrPool.emit(*Asm, StrSection, OffsetSection);
140 bool DwarfFile::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
141 SmallVectorImpl<DbgVariable *> &Vars = ScopeVariables[LS];
142 const DILocalVariable *DV = Var->getVariable();
143 // Variables with positive arg numbers are parameters.
144 if (unsigned ArgNum = DV->getArg()) {
145 // Keep all parameters in order at the start of the variable list to ensure
146 // function types are correct (no out-of-order parameters)
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()) {
154 unsigned CurNum = (*I)->getVariable()->getArg();
155 // A local (non-parameter) variable has been found, insert immediately
159 // A later indexed parameter has been found, insert immediately before it.
162 if (CurNum == ArgNum) {
163 (*I)->addMMIEntry(*Var);