LLVM  4.0.0
DwarfFile.cpp
Go to the documentation of this file.
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 "DwarfCompileUnit.h"
12 #include "DwarfDebug.h"
13 #include "DwarfUnit.h"
14 #include "llvm/ADT/STLExtras.h"
15 #include "llvm/IR/DataLayout.h"
16 #include "llvm/MC/MCStreamer.h"
17 #include "llvm/Support/LEB128.h"
19 
20 namespace llvm {
22  : Asm(AP), Abbrevs(AbbrevAllocator), StrPool(DA, *Asm, Pref) {}
23 
24 void DwarfFile::addUnit(std::unique_ptr<DwarfCompileUnit> U) {
25  CUs.push_back(std::move(U));
26 }
27 
28 // Emit the various dwarf units to the unit section USection with
29 // the abbreviations going into ASection.
30 void DwarfFile::emitUnits(bool UseOffsets) {
31  for (const auto &TheU : CUs)
32  emitUnit(TheU.get(), UseOffsets);
33 }
34 
35 void DwarfFile::emitUnit(DwarfUnit *TheU, bool UseOffsets) {
36  DIE &Die = TheU->getUnitDie();
37  MCSection *USection = TheU->getSection();
38  Asm->OutStreamer->SwitchSection(USection);
39 
40  TheU->emitHeader(UseOffsets);
41 
42  Asm->emitDwarfDIE(Die);
43 }
44 
45 // Compute the size and offset for each DIE.
47  // Offset from the first CU in the debug info section is 0 initially.
48  unsigned SecOffset = 0;
49 
50  // Iterate over each compile unit and set the size and offsets for each
51  // DIE within each compile unit. All offsets are CU relative.
52  for (const auto &TheU : CUs) {
53  TheU->setDebugSectionOffset(SecOffset);
54  SecOffset += computeSizeAndOffsetsForUnit(TheU.get());
55  }
56 }
57 
59  // CU-relative offset is reset to 0 here.
60  unsigned Offset = sizeof(int32_t) + // Length of Unit Info
61  TheU->getHeaderSize(); // Unit-specific headers
62 
63  // The return value here is CU-relative, after laying out
64  // all of the CU DIE.
65  return computeSizeAndOffset(TheU->getUnitDie(), Offset);
66 }
67 
68 // Compute the size and offset of a DIE. The offset is relative to start of the
69 // CU. It returns the offset after laying out the DIE.
70 unsigned DwarfFile::computeSizeAndOffset(DIE &Die, unsigned Offset) {
71  return Die.computeOffsetsAndAbbrevs(Asm, Abbrevs, Offset);
72 }
73 
74 void DwarfFile::emitAbbrevs(MCSection *Section) { Abbrevs.Emit(Asm, Section); }
75 
76 // Emit strings into a string section.
77 void DwarfFile::emitStrings(MCSection *StrSection, MCSection *OffsetSection) {
78  StrPool.emit(*Asm, StrSection, OffsetSection);
79 }
80 
82  SmallVectorImpl<DbgVariable *> &Vars = ScopeVariables[LS];
83  const DILocalVariable *DV = Var->getVariable();
84  // Variables with positive arg numbers are parameters.
85  if (unsigned ArgNum = DV->getArg()) {
86  // Keep all parameters in order at the start of the variable list to ensure
87  // function types are correct (no out-of-order parameters)
88  //
89  // This could be improved by only doing it for optimized builds (unoptimized
90  // builds have the right order to begin with), searching from the back (this
91  // would catch the unoptimized case quickly), or doing a binary search
92  // rather than linear search.
93  auto I = Vars.begin();
94  while (I != Vars.end()) {
95  unsigned CurNum = (*I)->getVariable()->getArg();
96  // A local (non-parameter) variable has been found, insert immediately
97  // before it.
98  if (CurNum == 0)
99  break;
100  // A later indexed parameter has been found, insert immediately before it.
101  if (CurNum > ArgNum)
102  break;
103  if (CurNum == ArgNum) {
104  (*I)->addMMIEntry(*Var);
105  return false;
106  }
107  ++I;
108  }
109  Vars.insert(I, Var);
110  return true;
111  }
112 
113  Vars.push_back(Var);
114  return true;
115 }
116 }
Instances of this class represent a uniqued identifier for a section in the current translation unit...
Definition: MCSection.h:40
std::unique_ptr< MCStreamer > OutStreamer
This is the MCStreamer object for the file we are generating.
Definition: AsmPrinter.h:84
DwarfFile(AsmPrinter *AP, StringRef Pref, BumpPtrAllocator &DA)
Definition: DwarfFile.cpp:21
LexicalScope - This class is used to track scope information.
Definition: LexicalScopes.h:45
virtual void emitHeader(bool UseOffsets)
Emit the header for this unit, not including the initial length field.
Definition: DwarfUnit.cpp:1529
void emitDwarfDIE(const DIE &Die) const
Recursively emit Dwarf DIE tree.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:32
This class is used to track local variable information.
Definition: DwarfDebug.h:64
virtual unsigned getHeaderSize() const
Compute the size of a header for this unit, not including the initial length field.
Definition: DwarfUnit.h:282
void addUnit(std::unique_ptr< DwarfCompileUnit > U)
Add a unit to the list of CUs.
Definition: DwarfFile.cpp:24
This dwarf writer support class manages information associated with a source file.
Definition: DwarfUnit.h:68
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:138
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
Definition: SmallVector.h:115
A structured debug information entry.
Definition: DIE.h:655
void Emit(const AsmPrinter *AP, MCSection *Section) const
Print all abbreviations using the specified asm printer.
Definition: DIE.cpp:151
DIE & getUnitDie()
Definition: DIE.h:821
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:67
void emitUnits(bool UseOffsets)
Emit all of the units to the section listed with the given abbreviation section.
Definition: DwarfFile.cpp:30
uint32_t Offset
unsigned getArg() const
unsigned computeOffsetsAndAbbrevs(const AsmPrinter *AP, DIEAbbrevSet &AbbrevSet, unsigned CUOffset)
Compute the offset of this DIE and all its children.
Definition: DIE.cpp:257
void computeSizeAndOffsets()
Compute the size and offset of all the DIEs.
Definition: DwarfFile.cpp:46
unsigned computeSizeAndOffsetsForUnit(DwarfUnit *TheU)
Compute the size and offset of all the DIEs in the given unit.
Definition: DwarfFile.cpp:58
void emitUnit(DwarfUnit *U, bool UseOffsets)
Emit the given unit to its section.
Definition: DwarfFile.cpp:35
const DILocalVariable * getVariable() const
Definition: DwarfDebug.h:112
void emitAbbrevs(MCSection *)
Emit a set of abbreviations to the specific section.
Definition: DwarfFile.cpp:74
iterator insert(iterator I, T &&Elt)
Definition: SmallVector.h:464
bool addScopeVariable(LexicalScope *LS, DbgVariable *Var)
Definition: DwarfFile.cpp:81
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
Definition: SmallVector.h:119
#define I(x, y, z)
Definition: MD5.cpp:54
unsigned computeSizeAndOffset(DIE &Die, unsigned Offset)
Compute the size and offset of a DIE given an incoming Offset.
Definition: DwarfFile.cpp:70
void emitStrings(MCSection *StrSection, MCSection *OffsetSection=nullptr)
Emit all of the strings to the section given.
Definition: DwarfFile.cpp:77
MCSection * getSection() const
Return the section that this DIEUnit will be emitted into.
Definition: DIE.h:814
void emit(AsmPrinter &Asm, MCSection *StrSection, MCSection *OffsetSection=nullptr)
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47