LLVM  12.0.0git
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"
15 #include "llvm/IR/Metadata.h"
16 #include "llvm/MC/MCStreamer.h"
17 #include <algorithm>
18 #include <cstdint>
20 using namespace llvm;
23  : Asm(AP), Abbrevs(AbbrevAllocator), StrPool(DA, *Asm, Pref) {}
25 void DwarfFile::addUnit(std::unique_ptr<DwarfCompileUnit> U) {
26  CUs.push_back(std::move(U));
27 }
29 // Emit the various dwarf units to the unit section USection with
30 // the abbreviations going into ASection.
31 void DwarfFile::emitUnits(bool UseOffsets) {
32  for (const auto &TheU : CUs)
33  emitUnit(TheU.get(), UseOffsets);
34 }
36 void DwarfFile::emitUnit(DwarfUnit *TheU, bool UseOffsets) {
37  if (TheU->getCUNode()->isDebugDirectivesOnly())
38  return;
40  MCSection *S = TheU->getSection();
42  if (!S)
43  return;
45  // Skip CUs that ended up not being needed (split CUs that were abandoned
46  // because they added no information beyond the non-split CU)
47  if (llvm::empty(TheU->getUnitDie().values()))
48  return;
50  Asm->OutStreamer->SwitchSection(S);
51  TheU->emitHeader(UseOffsets);
52  Asm->emitDwarfDIE(TheU->getUnitDie());
54  if (MCSymbol *EndLabel = TheU->getEndLabel())
55  Asm->OutStreamer->emitLabel(EndLabel);
56 }
58 // Compute the size and offset for each DIE.
60  // Offset from the first CU in the debug info section is 0 initially.
61  uint64_t SecOffset = 0;
63  // Iterate over each compile unit and set the size and offsets for each
64  // DIE within each compile unit. All offsets are CU relative.
65  for (const auto &TheU : CUs) {
66  if (TheU->getCUNode()->isDebugDirectivesOnly())
67  continue;
69  // Skip CUs that ended up not being needed (split CUs that were abandoned
70  // because they added no information beyond the non-split CU)
71  if (llvm::empty(TheU->getUnitDie().values()))
72  return;
74  TheU->setDebugSectionOffset(SecOffset);
75  SecOffset += computeSizeAndOffsetsForUnit(TheU.get());
76  }
77  if (SecOffset > UINT32_MAX && !Asm->isDwarf64())
78  report_fatal_error("The generated debug information is too large "
79  "for the 32-bit DWARF format.");
80 }
83  // CU-relative offset is reset to 0 here.
84  unsigned Offset = Asm->getUnitLengthFieldByteSize() + // Length of Unit Info
85  TheU->getHeaderSize(); // Unit-specific headers
87  // The return value here is CU-relative, after laying out
88  // all of the CU DIE.
89  return computeSizeAndOffset(TheU->getUnitDie(), Offset);
90 }
92 // Compute the size and offset of a DIE. The offset is relative to start of the
93 // CU. It returns the offset after laying out the DIE.
94 unsigned DwarfFile::computeSizeAndOffset(DIE &Die, unsigned Offset) {
95  return Die.computeOffsetsAndAbbrevs(Asm, Abbrevs, Offset);
96 }
100 // Emit strings into a string section.
101 void DwarfFile::emitStrings(MCSection *StrSection, MCSection *OffsetSection,
102  bool UseRelativeOffsets) {
103  StrPool.emit(*Asm, StrSection, OffsetSection, UseRelativeOffsets);
104 }
107  auto &ScopeVars = ScopeVariables[LS];
108  const DILocalVariable *DV = Var->getVariable();
109  if (unsigned ArgNum = DV->getArg()) {
110  auto Cached = ScopeVars.Args.find(ArgNum);
111  if (Cached == ScopeVars.Args.end())
112  ScopeVars.Args[ArgNum] = Var;
113  else {
114  Cached->second->addMMIEntry(*Var);
115  return false;
116  }
117  } else {
118  ScopeVars.Locals.push_back(Var);
119  }
120  return true;
121 }
123 void DwarfFile::addScopeLabel(LexicalScope *LS, DbgLabel *Label) {
124  SmallVectorImpl<DbgLabel *> &Labels = ScopeLabels[LS];
125  Labels.push_back(Label);
126 }
128 std::pair<uint32_t, RangeSpanList *>
130  CURangeLists.push_back(
131  RangeSpanList{Asm->createTempSymbol("debug_ranges"), &CU, std::move(R)});
132  return std::make_pair(CURangeLists.size() - 1, &CURangeLists.back());
133 }
const DICompileUnit * getCUNode() const
Definition: DwarfUnit.h:84
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:25
void addMMIEntry(const DbgVariable &V)
Definition: DwarfDebug.cpp:283
virtual unsigned getHeaderSize() const
Compute the size of a header for this unit, not including the initial length field.
Definition: DwarfUnit.h:250
std::unique_ptr< MCStreamer > OutStreamer
This is the MCStreamer object for the file we are generating.
Definition: AsmPrinter.h:97
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:140
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:94
This file contains the declarations for metadata subclasses.
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:129
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:43
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:167
This class is used to track label information.
Definition: DwarfDebug.h:233
unsigned computeSizeAndOffsetsForUnit(DwarfUnit *TheU)
Compute the size and offset of all the DIEs in the given unit.
Definition: DwarfFile.cpp:82
This dwarf writer support class manages information associated with a source file.
Definition: DwarfUnit.h:36
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:67
value_range values()
Definition: DIE.h:710
A structured debug information entry.
Definition: DIE.h:722
DIE & getUnitDie()
Definition: DIE.h:894
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:82
void computeSizeAndOffsets()
Compute the size and offset of all the DIEs.
Definition: DwarfFile.cpp:59
bool addScopeVariable(LexicalScope *LS, DbgVariable *Var)
Definition: DwarfFile.cpp:106
void emitUnit(DwarfUnit *TheU, bool UseOffsets)
Emit the given unit to its section.
Definition: DwarfFile.cpp:36
DwarfFile(AsmPrinter *AP, StringRef Pref, BumpPtrAllocator &DA)
Definition: DwarfFile.cpp:22
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:263
uint64_t Offset
MCSection * getSection() const
Return the section that this DIEUnit will be emitted into.
Definition: DIE.h:891
MCSymbol * getEndLabel() const
Definition: DwarfUnit.h:82
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1116
void emitStrings(MCSection *StrSection, MCSection *OffsetSection=nullptr, bool UseRelativeOffsets=false)
Emit all of the strings to the section given.
Definition: DwarfFile.cpp:101
void emitAbbrevs(MCSection *)
Emit a set of abbreviations to the specific section.
Definition: DwarfFile.cpp:98
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1556
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
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:31
unsigned int getUnitLengthFieldByteSize() const
Returns 4 for DWARF32 and 12 for DWARF64.
bool isDwarf64() const
MCSymbol * createTempSymbol(const Twine &Name) const