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   Asm->OutStreamer->SwitchSection(S);
47   TheU->emitHeader(UseOffsets);
48   Asm->emitDwarfDIE(TheU->getUnitDie());
49 
50   if (MCSymbol *EndLabel = TheU->getEndLabel())
51     Asm->OutStreamer->EmitLabel(EndLabel);
52 }
53 
54 // Compute the size and offset for each DIE.
55 void DwarfFile::computeSizeAndOffsets() {
56   // Offset from the first CU in the debug info section is 0 initially.
57   unsigned SecOffset = 0;
58 
59   // Iterate over each compile unit and set the size and offsets for each
60   // DIE within each compile unit. All offsets are CU relative.
61   for (const auto &TheU : CUs) {
62     if (TheU->getCUNode()->isDebugDirectivesOnly())
63       continue;
64 
65     TheU->setDebugSectionOffset(SecOffset);
66     SecOffset += computeSizeAndOffsetsForUnit(TheU.get());
67   }
68 }
69 
70 unsigned DwarfFile::computeSizeAndOffsetsForUnit(DwarfUnit *TheU) {
71   // CU-relative offset is reset to 0 here.
72   unsigned Offset = sizeof(int32_t) +      // Length of Unit Info
73                     TheU->getHeaderSize(); // Unit-specific headers
74 
75   // The return value here is CU-relative, after laying out
76   // all of the CU DIE.
77   return computeSizeAndOffset(TheU->getUnitDie(), Offset);
78 }
79 
80 // Compute the size and offset of a DIE. The offset is relative to start of the
81 // CU. It returns the offset after laying out the DIE.
82 unsigned DwarfFile::computeSizeAndOffset(DIE &Die, unsigned Offset) {
83   return Die.computeOffsetsAndAbbrevs(Asm, Abbrevs, Offset);
84 }
85 
86 void DwarfFile::emitAbbrevs(MCSection *Section) { Abbrevs.Emit(Asm, Section); }
87 
88 // Emit strings into a string section.
89 void DwarfFile::emitStrings(MCSection *StrSection, MCSection *OffsetSection,
90                             bool UseRelativeOffsets) {
91   StrPool.emit(*Asm, StrSection, OffsetSection, UseRelativeOffsets);
92 }
93 
94 bool DwarfFile::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
95   auto &ScopeVars = ScopeVariables[LS];
96   const DILocalVariable *DV = Var->getVariable();
97   if (unsigned ArgNum = DV->getArg()) {
98     auto Cached = ScopeVars.Args.find(ArgNum);
99     if (Cached == ScopeVars.Args.end())
100       ScopeVars.Args[ArgNum] = Var;
101     else {
102       Cached->second->addMMIEntry(*Var);
103       return false;
104     }
105   } else {
106     ScopeVars.Locals.push_back(Var);
107   }
108   return true;
109 }
110 
111 void DwarfFile::addScopeLabel(LexicalScope *LS, DbgLabel *Label) {
112   SmallVectorImpl<DbgLabel *> &Labels = ScopeLabels[LS];
113   Labels.push_back(Label);
114 }
115 
116 std::pair<uint32_t, RangeSpanList *>
117 DwarfFile::addRange(const DwarfCompileUnit &CU, SmallVector<RangeSpan, 2> R) {
118   CURangeLists.push_back(
119       RangeSpanList(Asm->createTempSymbol("debug_ranges"), CU, std::move(R)));
120   return std::make_pair(CURangeLists.size() - 1, &CURangeLists.back());
121 }
122