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 //===----------------------------------------------------------------------===//
8
9 #include "DwarfFile.h"
10 #include "DwarfCompileUnit.h"
11 #include "DwarfDebug.h"
12 #include "DwarfUnit.h"
13 #include "llvm/ADT/SmallVector.h"
14 #include "llvm/CodeGen/AsmPrinter.h"
15 #include "llvm/CodeGen/DIE.h"
16 #include "llvm/IR/DebugInfoMetadata.h"
17 #include "llvm/MC/MCStreamer.h"
18 #include <algorithm>
19 #include <cstdint>
20
21 using namespace llvm;
22
DwarfFile(AsmPrinter * AP,StringRef Pref,BumpPtrAllocator & DA)23 DwarfFile::DwarfFile(AsmPrinter *AP, StringRef Pref, BumpPtrAllocator &DA)
24 : Asm(AP), Abbrevs(AbbrevAllocator), StrPool(DA, *Asm, Pref) {}
25
addUnit(std::unique_ptr<DwarfCompileUnit> U)26 void DwarfFile::addUnit(std::unique_ptr<DwarfCompileUnit> U) {
27 CUs.push_back(std::move(U));
28 }
29
30 // Emit the various dwarf units to the unit section USection with
31 // the abbreviations going into ASection.
emitUnits(bool UseOffsets)32 void DwarfFile::emitUnits(bool UseOffsets) {
33 for (const auto &TheU : CUs)
34 emitUnit(TheU.get(), UseOffsets);
35 }
36
emitUnit(DwarfUnit * TheU,bool UseOffsets)37 void DwarfFile::emitUnit(DwarfUnit *TheU, bool UseOffsets) {
38 if (TheU->getCUNode()->isDebugDirectivesOnly())
39 return;
40
41 MCSection *S = TheU->getSection();
42
43 if (!S)
44 return;
45
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;
50
51 Asm->OutStreamer->SwitchSection(S);
52 TheU->emitHeader(UseOffsets);
53 Asm->emitDwarfDIE(TheU->getUnitDie());
54
55 if (MCSymbol *EndLabel = TheU->getEndLabel())
56 Asm->OutStreamer->EmitLabel(EndLabel);
57 }
58
59 // Compute the size and offset for each DIE.
computeSizeAndOffsets()60 void DwarfFile::computeSizeAndOffsets() {
61 // Offset from the first CU in the debug info section is 0 initially.
62 unsigned SecOffset = 0;
63
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;
69
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;
74
75 TheU->setDebugSectionOffset(SecOffset);
76 SecOffset += computeSizeAndOffsetsForUnit(TheU.get());
77 }
78 }
79
computeSizeAndOffsetsForUnit(DwarfUnit * TheU)80 unsigned DwarfFile::computeSizeAndOffsetsForUnit(DwarfUnit *TheU) {
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
84
85 // The return value here is CU-relative, after laying out
86 // all of the CU DIE.
87 return computeSizeAndOffset(TheU->getUnitDie(), Offset);
88 }
89
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.
computeSizeAndOffset(DIE & Die,unsigned Offset)92 unsigned DwarfFile::computeSizeAndOffset(DIE &Die, unsigned Offset) {
93 return Die.computeOffsetsAndAbbrevs(Asm, Abbrevs, Offset);
94 }
95
emitAbbrevs(MCSection * Section)96 void DwarfFile::emitAbbrevs(MCSection *Section) { Abbrevs.Emit(Asm, Section); }
97
98 // Emit strings into a string section.
emitStrings(MCSection * StrSection,MCSection * OffsetSection,bool UseRelativeOffsets)99 void DwarfFile::emitStrings(MCSection *StrSection, MCSection *OffsetSection,
100 bool UseRelativeOffsets) {
101 StrPool.emit(*Asm, StrSection, OffsetSection, UseRelativeOffsets);
102 }
103
addScopeVariable(LexicalScope * LS,DbgVariable * Var)104 bool DwarfFile::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
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 }
120
addScopeLabel(LexicalScope * LS,DbgLabel * Label)121 void DwarfFile::addScopeLabel(LexicalScope *LS, DbgLabel *Label) {
122 SmallVectorImpl<DbgLabel *> &Labels = ScopeLabels[LS];
123 Labels.push_back(Label);
124 }
125
126 std::pair<uint32_t, RangeSpanList *>
addRange(const DwarfCompileUnit & CU,SmallVector<RangeSpan,2> R)127 DwarfFile::addRange(const DwarfCompileUnit &CU, SmallVector<RangeSpan, 2> R) {
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 }
132