DwarfFile.cpp revision 4c5e43da7792f75567b693105cc53e3f1992ad98
1//===-- 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"
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"
18
19namespace llvm {
20DwarfFile::DwarfFile(AsmPrinter *AP, StringRef Pref, BumpPtrAllocator &DA)
21    : Asm(AP), StrPool(DA, *Asm, Pref) {}
22
23DwarfFile::~DwarfFile() {}
24
25// 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.
50void DwarfFile::emitUnits(bool UseOffsets) {
51  for (const auto &TheU : CUs) {
52    DIE &Die = TheU->getUnitDie();
53    const MCSection *USection = TheU->getSection();
54    Asm->OutStreamer.SwitchSection(USection);
55
56    TheU->emitHeader(UseOffsets);
57
58    Asm->emitDwarfDIE(Die);
59  }
60}
61
62// 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.
78    unsigned EndOffset = computeSizeAndOffset(TheU->getUnitDie(), Offset);
79    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
97  const SmallVectorImpl<DIEValue *> &Values = Die.getValues();
98  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.
103    Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
104
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}
122
123void DwarfFile::emitAbbrevs(const MCSection *Section) {
124  // Check to see if it is worth the effort.
125  if (!Abbreviations.empty()) {
126    // Start the debug abbrev section.
127    Asm->OutStreamer.SwitchSection(Section);
128    Asm->emitDwarfAbbrevs(Abbreviations);
129  }
130}
131
132// Emit strings into a string section.
133void DwarfFile::emitStrings(const MCSection *StrSection,
134                            const MCSection *OffsetSection) {
135  StrPool.emit(*Asm, StrSection, OffsetSection);
136}
137
138bool DwarfFile::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
139  SmallVectorImpl<DbgVariable *> &Vars = ScopeVariables[LS];
140  DIVariable DV = Var->getVariable();
141  // Variables with positive arg numbers are parameters.
142  if (unsigned ArgNum = DV.getArgNumber()) {
143    // Keep all parameters in order at the start of the variable list to ensure
144    // function types are correct (no out-of-order parameters)
145    //
146    // This could be improved by only doing it for optimized builds (unoptimized
147    // builds have the right order to begin with), searching from the back (this
148    // would catch the unoptimized case quickly), or doing a binary search
149    // rather than linear search.
150    auto I = Vars.begin();
151    while (I != Vars.end()) {
152      unsigned CurNum = (*I)->getVariable().getArgNumber();
153      // A local (non-parameter) variable has been found, insert immediately
154      // before it.
155      if (CurNum == 0)
156        break;
157      // A later indexed parameter has been found, insert immediately before it.
158      if (CurNum > ArgNum)
159        break;
160      if (CurNum == ArgNum) {
161        (*I)->addMMIEntry(*Var);
162        return false;
163      }
164      ++I;
165    }
166    Vars.insert(I, Var);
167    return true;
168  }
169
170  Vars.push_back(Var);
171  return true;
172}
173}
174