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 
23 DwarfFile::DwarfFile(AsmPrinter *AP, StringRef Pref, BumpPtrAllocator &DA)
24     : Asm(AP), Abbrevs(AbbrevAllocator), StrPool(DA, *Asm, Pref) {}
25 
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.
32 void DwarfFile::emitUnits(bool UseOffsets) {
33   for (const auto &TheU : CUs)
34     emitUnit(TheU.get(), UseOffsets);
35 }
36 
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.
60 void DwarfFile::computeSizeAndOffsets() {
61   // Offset from the first CU in the debug info section is 0 initially.
62   uint64_t 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   if (SecOffset > UINT32_MAX && !Asm->isDwarf64())
79     report_fatal_error("The generated debug information is too large "
80                        "for the 32-bit DWARF format.");
81 }
82 
83 unsigned DwarfFile::computeSizeAndOffsetsForUnit(DwarfUnit *TheU) {
84   // CU-relative offset is reset to 0 here.
85   unsigned Offset = Asm->getUnitLengthFieldByteSize() + // Length of Unit Info
86                     TheU->getHeaderSize();              // Unit-specific headers
87 
88   // The return value here is CU-relative, after laying out
89   // all of the CU DIE.
90   return computeSizeAndOffset(TheU->getUnitDie(), Offset);
91 }
92 
93 // Compute the size and offset of a DIE. The offset is relative to start of the
94 // CU. It returns the offset after laying out the DIE.
95 unsigned DwarfFile::computeSizeAndOffset(DIE &Die, unsigned Offset) {
96   return Die.computeOffsetsAndAbbrevs(Asm, Abbrevs, Offset);
97 }
98 
99 void DwarfFile::emitAbbrevs(MCSection *Section) { Abbrevs.Emit(Asm, Section); }
100 
101 // Emit strings into a string section.
102 void DwarfFile::emitStrings(MCSection *StrSection, MCSection *OffsetSection,
103                             bool UseRelativeOffsets) {
104   StrPool.emit(*Asm, StrSection, OffsetSection, UseRelativeOffsets);
105 }
106 
107 bool DwarfFile::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
108   auto &ScopeVars = ScopeVariables[LS];
109   const DILocalVariable *DV = Var->getVariable();
110   if (unsigned ArgNum = DV->getArg()) {
111     auto Cached = ScopeVars.Args.find(ArgNum);
112     if (Cached == ScopeVars.Args.end())
113       ScopeVars.Args[ArgNum] = Var;
114     else {
115       Cached->second->addMMIEntry(*Var);
116       return false;
117     }
118   } else {
119     ScopeVars.Locals.push_back(Var);
120   }
121   return true;
122 }
123 
124 void DwarfFile::addScopeLabel(LexicalScope *LS, DbgLabel *Label) {
125   SmallVectorImpl<DbgLabel *> &Labels = ScopeLabels[LS];
126   Labels.push_back(Label);
127 }
128 
129 std::pair<uint32_t, RangeSpanList *>
130 DwarfFile::addRange(const DwarfCompileUnit &CU, SmallVector<RangeSpan, 2> R) {
131   CURangeLists.push_back(
132       RangeSpanList{Asm->createTempSymbol("debug_ranges"), &CU, std::move(R)});
133   return std::make_pair(CURangeLists.size() - 1, &CURangeLists.back());
134 }
135