LLVM  9.0.0svn
Go to the documentation of this file.
1 //===- llvm/CodeGen/DwarfFile.cpp - Dwarf Debug Framework -----------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include "DwarfFile.h"
10 #include "DwarfCompileUnit.h"
11 #include "DwarfDebug.h"
12 #include "DwarfUnit.h"
13 #include "llvm/ADT/SmallVector.h"
15 #include "llvm/CodeGen/DIE.h"
17 #include "llvm/MC/MCStreamer.h"
18 #include <algorithm>
19 #include <cstdint>
21 using namespace llvm;
24  : Asm(AP), Abbrevs(AbbrevAllocator), StrPool(DA, *Asm, Pref) {}
26 void DwarfFile::addUnit(std::unique_ptr<DwarfCompileUnit> U) {
27  CUs.push_back(std::move(U));
28 }
30 // Emit the various dwarf units to the unit section USection with
31 // the abbreviations going into ASection.
32 void DwarfFile::emitUnits(bool UseOffsets) {
33  for (const auto &TheU : CUs)
34  emitUnit(TheU.get(), UseOffsets);
35 }
37 void DwarfFile::emitUnit(DwarfUnit *TheU, bool UseOffsets) {
38  if (TheU->getCUNode()->isDebugDirectivesOnly())
39  return;
41  MCSection *S = TheU->getSection();
43  if (!S)
44  return;
46  // Skip CUs that ended up not being needed (split CUs that were abandoned
47  // because they added no information beyond the non-split CU)
48  if (llvm::empty(TheU->getUnitDie().values()))
49  return;
51  Asm->OutStreamer->SwitchSection(S);
52  TheU->emitHeader(UseOffsets);
53  Asm->emitDwarfDIE(TheU->getUnitDie());
55  if (MCSymbol *EndLabel = TheU->getEndLabel())
56  Asm->OutStreamer->EmitLabel(EndLabel);
57 }
59 // Compute the size and offset for each DIE.
61  // Offset from the first CU in the debug info section is 0 initially.
62  unsigned SecOffset = 0;
64  // Iterate over each compile unit and set the size and offsets for each
65  // DIE within each compile unit. All offsets are CU relative.
66  for (const auto &TheU : CUs) {
67  if (TheU->getCUNode()->isDebugDirectivesOnly())
68  continue;
70  // Skip CUs that ended up not being needed (split CUs that were abandoned
71  // because they added no information beyond the non-split CU)
72  if (llvm::empty(TheU->getUnitDie().values()))
73  return;
75  TheU->setDebugSectionOffset(SecOffset);
76  SecOffset += computeSizeAndOffsetsForUnit(TheU.get());
77  }
78 }
81  // CU-relative offset is reset to 0 here.
82  unsigned Offset = sizeof(int32_t) + // Length of Unit Info
83  TheU->getHeaderSize(); // Unit-specific headers
85  // The return value here is CU-relative, after laying out
86  // all of the CU DIE.
87  return computeSizeAndOffset(TheU->getUnitDie(), Offset);
88 }
90 // Compute the size and offset of a DIE. The offset is relative to start of the
91 // CU. It returns the offset after laying out the DIE.
92 unsigned DwarfFile::computeSizeAndOffset(DIE &Die, unsigned Offset) {
93  return Die.computeOffsetsAndAbbrevs(Asm, Abbrevs, Offset);
94 }
96 void DwarfFile::emitAbbrevs(MCSection *Section) { Abbrevs.Emit(Asm, Section); }
98 // Emit strings into a string section.
99 void DwarfFile::emitStrings(MCSection *StrSection, MCSection *OffsetSection,
100  bool UseRelativeOffsets) {
101  StrPool.emit(*Asm, StrSection, OffsetSection, UseRelativeOffsets);
102 }
105  auto &ScopeVars = ScopeVariables[LS];
106  const DILocalVariable *DV = Var->getVariable();
107  if (unsigned ArgNum = DV->getArg()) {
108  auto Cached = ScopeVars.Args.find(ArgNum);
109  if (Cached == ScopeVars.Args.end())
110  ScopeVars.Args[ArgNum] = Var;
111  else {
112  Cached->second->addMMIEntry(*Var);
113  return false;
114  }
115  } else {
116  ScopeVars.Locals.push_back(Var);
117  }
118  return true;
119 }
122  SmallVectorImpl<DbgLabel *> &Labels = ScopeLabels[LS];
123  Labels.push_back(Label);
124 }
126 std::pair<uint32_t, RangeSpanList *>
128  CURangeLists.push_back(
129  RangeSpanList(Asm->createTempSymbol("debug_ranges"), CU, std::move(R)));
130  return std::make_pair(CURangeLists.size() - 1, &CURangeLists.back());
131 }
const DICompileUnit * getCUNode() const
Definition: DwarfUnit.h:89
Instances of this class represent a uniqued identifier for a section in the current translation unit...
Definition: MCSection.h:38
void addUnit(std::unique_ptr< DwarfCompileUnit > U)
Add a unit to the list of CUs.
Definition: DwarfFile.cpp:26
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:261
std::unique_ptr< MCStreamer > OutStreamer
This is the MCStreamer object for the file we are generating.
Definition: AsmPrinter.h:93
This class represents lattice values for constants.
Definition: AllocatorList.h:23
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
unsigned computeSizeAndOffset(DIE &Die, unsigned Offset)
Compute the size and offset of a DIE given an incoming Offset.
Definition: DwarfFile.cpp:92
void emit(AsmPrinter &Asm, MCSection *StrSection, MCSection *OffsetSection=nullptr, bool UseRelativeOffsets=false)
LexicalScope - This class is used to track scope information.
Definition: LexicalScopes.h:44
std::pair< uint32_t, RangeSpanList * > addRange(const DwarfCompileUnit &CU, SmallVector< RangeSpan, 2 > R)
Definition: DwarfFile.cpp:127
void emitDwarfDIE(const DIE &Die) const
Recursively emit Dwarf DIE tree.
bool isDebugDirectivesOnly() const
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:41
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:116
const DILocalVariable * getVariable() const
Definition: DwarfDebug.h:162
This class is used to track label information.
Definition: DwarfDebug.h:232
unsigned computeSizeAndOffsetsForUnit(DwarfUnit *TheU)
Compute the size and offset of all the DIEs in the given unit.
Definition: DwarfFile.cpp:80
This dwarf writer support class manages information associated with a source file.
Definition: DwarfUnit.h:40
void emitUnit(DwarfUnit *U, bool UseOffsets)
Emit the given unit to its section.
Definition: DwarfFile.cpp:37
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:140
value_range values()
Definition: DIE.h:649
A structured debug information entry.
Definition: DIE.h:661
DIE & getUnitDie()
Definition: DIE.h:833
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:78
void computeSizeAndOffsets()
Compute the size and offset of all the DIEs.
Definition: DwarfFile.cpp:60
bool addScopeVariable(LexicalScope *LS, DbgVariable *Var)
Definition: DwarfFile.cpp:104
DwarfFile(AsmPrinter *AP, StringRef Pref, BumpPtrAllocator &DA)
Definition: DwarfFile.cpp:23
unsigned computeOffsetsAndAbbrevs(const AsmPrinter *AP, DIEAbbrevSet &AbbrevSet, unsigned CUOffset)
Compute the offset of this DIE and all its children.
Definition: DIE.cpp:277
constexpr bool empty(const T &RangeOrContainer)
Test whether RangeOrContainer is empty. Similar to C++17 std::empty.
Definition: STLExtras.h:209
void addScopeLabel(LexicalScope *LS, DbgLabel *Label)
Definition: DwarfFile.cpp:121
MCSection * getSection() const
Return the section that this DIEUnit will be emitted into.
Definition: DIE.h:826
MCSymbol * getEndLabel() const
Definition: DwarfUnit.h:87
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:839
void emitStrings(MCSection *StrSection, MCSection *OffsetSection=nullptr, bool UseRelativeOffsets=false)
Emit all of the strings to the section given.
Definition: DwarfFile.cpp:99
void emitAbbrevs(MCSection *)
Emit a set of abbreviations to the specific section.
Definition: DwarfFile.cpp:96
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
void Emit(const AsmPrinter *AP, MCSection *Section) const
Print all abbreviations using the specified asm printer.
Definition: DIE.cpp:170
void emitUnits(bool UseOffsets)
Emit all of the units to the section listed with the given abbreviation section.
Definition: DwarfFile.cpp:32
MCSymbol * createTempSymbol(const Twine &Name) const