LLVM  7.0.0svn
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/SmallVector.h"
16 #include "llvm/CodeGen/DIE.h"
18 #include "llvm/MC/MCStreamer.h"
19 #include <algorithm>
20 #include <cstdint>
21 
22 using namespace llvm;
23 
25  : Asm(AP), Abbrevs(AbbrevAllocator), StrPool(DA, *Asm, Pref) {}
26 
27 void DwarfFile::addUnit(std::unique_ptr<DwarfCompileUnit> U) {
28  CUs.push_back(std::move(U));
29 }
30 
32  if (StrPool.empty())
33  return;
34  Asm->OutStreamer->SwitchSection(Section);
35  unsigned EntrySize = 4;
36  // FIXME: DWARF64
37  // We are emitting the header for a contribution to the string offsets
38  // table. The header consists of an entry with the contribution's
39  // size (not including the size of the length field), the DWARF version and
40  // 2 bytes of padding.
41  Asm->emitInt32(StrPool.size() * EntrySize + 4);
42  Asm->emitInt16(Asm->getDwarfVersion());
43  Asm->emitInt16(0);
44  // Define the symbol that marks the start of the contribution. It is
45  // referenced by most unit headers via DW_AT_str_offsets_base.
46  // Split units do not use the attribute.
47  if (StringOffsetsStartSym)
48  Asm->OutStreamer->EmitLabel(StringOffsetsStartSym);
49 }
50 
51 // Emit the various dwarf units to the unit section USection with
52 // the abbreviations going into ASection.
53 void DwarfFile::emitUnits(bool UseOffsets) {
54  for (const auto &TheU : CUs)
55  emitUnit(TheU.get(), UseOffsets);
56 }
57 
58 void DwarfFile::emitUnit(DwarfUnit *TheU, bool UseOffsets) {
59  DIE &Die = TheU->getUnitDie();
60  MCSection *USection = TheU->getSection();
61  Asm->OutStreamer->SwitchSection(USection);
62 
63  TheU->emitHeader(UseOffsets);
64 
65  Asm->emitDwarfDIE(Die);
66 }
67 
68 // Compute the size and offset for each DIE.
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->setDebugSectionOffset(SecOffset);
77  SecOffset += computeSizeAndOffsetsForUnit(TheU.get());
78  }
79 }
80 
82  // CU-relative offset is reset to 0 here.
83  unsigned Offset = sizeof(int32_t) + // Length of Unit Info
84  TheU->getHeaderSize(); // Unit-specific headers
85 
86  // The return value here is CU-relative, after laying out
87  // all of the CU DIE.
88  return computeSizeAndOffset(TheU->getUnitDie(), Offset);
89 }
90 
91 // Compute the size and offset of a DIE. The offset is relative to start of the
92 // CU. It returns the offset after laying out the DIE.
93 unsigned DwarfFile::computeSizeAndOffset(DIE &Die, unsigned Offset) {
94  return Die.computeOffsetsAndAbbrevs(Asm, Abbrevs, Offset);
95 }
96 
97 void DwarfFile::emitAbbrevs(MCSection *Section) { Abbrevs.Emit(Asm, Section); }
98 
99 // Emit strings into a string section.
100 void DwarfFile::emitStrings(MCSection *StrSection, MCSection *OffsetSection,
101  bool UseRelativeOffsets) {
102  StrPool.emit(*Asm, StrSection, OffsetSection, UseRelativeOffsets);
103 }
104 
106  auto &ScopeVars = ScopeVariables[LS];
107  const DILocalVariable *DV = Var->getVariable();
108  if (unsigned ArgNum = DV->getArg()) {
109  auto Cached = ScopeVars.Args.find(ArgNum);
110  if (Cached == ScopeVars.Args.end())
111  ScopeVars.Args[ArgNum] = Var;
112  else {
113  Cached->second->addMMIEntry(*Var);
114  return false;
115  }
116  } else {
117  ScopeVars.Locals.push_back(Var);
118  }
119  return true;
120 }
Instances of this class represent a uniqued identifier for a section in the current translation unit...
Definition: MCSection.h:39
void addUnit(std::unique_ptr< DwarfCompileUnit > U)
Add a unit to the list of CUs.
Definition: DwarfFile.cpp:27
void addMMIEntry(const DbgVariable &V)
Definition: DwarfDebug.cpp:253
virtual unsigned getHeaderSize() const
Compute the size of a header for this unit, not including the initial length field.
Definition: DwarfUnit.h:284
std::unique_ptr< MCStreamer > OutStreamer
This is the MCStreamer object for the file we are generating.
Definition: AsmPrinter.h:93
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
unsigned computeSizeAndOffset(DIE &Die, unsigned Offset)
Compute the size and offset of a DIE given an incoming Offset.
Definition: DwarfFile.cpp:93
uint16_t getDwarfVersion() const
void emit(AsmPrinter &Asm, MCSection *StrSection, MCSection *OffsetSection=nullptr, bool UseRelativeOffsets=false)
LexicalScope - This class is used to track scope information.
Definition: LexicalScopes.h:45
void emitDwarfDIE(const DIE &Die) const
Recursively emit Dwarf DIE tree.
virtual void emitHeader(bool UseOffsets)=0
Emit the header for this unit, not including the initial length field.
This class is used to track local variable information.
Definition: DwarfDebug.h:76
const DILocalVariable * getVariable() const
Definition: DwarfDebug.h:124
unsigned computeSizeAndOffsetsForUnit(DwarfUnit *TheU)
Compute the size and offset of all the DIEs in the given unit.
Definition: DwarfFile.cpp:81
This dwarf writer support class manages information associated with a source file.
Definition: DwarfUnit.h:68
void emitUnit(DwarfUnit *U, bool UseOffsets)
Emit the given unit to its section.
Definition: DwarfFile.cpp:58
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:140
A structured debug information entry.
Definition: DIE.h:662
DIE & getUnitDie()
Definition: DIE.h:834
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:78
unsigned size() const
void computeSizeAndOffsets()
Compute the size and offset of all the DIEs.
Definition: DwarfFile.cpp:69
bool addScopeVariable(LexicalScope *LS, DbgVariable *Var)
Definition: DwarfFile.cpp:105
DwarfFile(AsmPrinter *AP, StringRef Pref, BumpPtrAllocator &DA)
Definition: DwarfFile.cpp:24
unsigned computeOffsetsAndAbbrevs(const AsmPrinter *AP, DIEAbbrevSet &AbbrevSet, unsigned CUOffset)
Compute the offset of this DIE and all its children.
Definition: DIE.cpp:278
MCSection * getSection() const
Return the section that this DIEUnit will be emitted into.
Definition: DIE.h:827
void emitInt32(int Value) const
Emit a long directive and value.
void emitStrings(MCSection *StrSection, MCSection *OffsetSection=nullptr, bool UseRelativeOffsets=false)
Emit all of the strings to the section given.
Definition: DwarfFile.cpp:100
void emitStringOffsetsTableHeader(MCSection *Section)
Emit the string table offsets header.
Definition: DwarfFile.cpp:31
void emitAbbrevs(MCSection *)
Emit a set of abbreviations to the specific section.
Definition: DwarfFile.cpp:97
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
void Emit(const AsmPrinter *AP, MCSection *Section) const
Print all abbreviations using the specified asm printer.
Definition: DIE.cpp:171
void emitUnits(bool UseOffsets)
Emit all of the units to the section listed with the given abbreviation section.
Definition: DwarfFile.cpp:53
void emitInt16(int Value) const
Emit a short directive and value.