1 //===- llvm/CodeGen/DwarfFile.cpp - Dwarf Debug Framework -----------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "Dwarf2BTF.h"
11 #include "DwarfFile.h"
12 #include "DwarfCompileUnit.h"
13 #include "DwarfDebug.h"
14 #include "DwarfUnit.h"
15 #include "llvm/ADT/SmallVector.h"
16 #include "llvm/CodeGen/AsmPrinter.h"
17 #include "llvm/CodeGen/DIE.h"
18 #include "llvm/IR/DebugInfoMetadata.h"
19 #include "llvm/MC/MCBTFContext.h"
20 #include "llvm/MC/MCContext.h"
21 #include "llvm/MC/MCStreamer.h"
22 #include <algorithm>
23 #include <cstdint>
24 
25 using namespace llvm;
26 
27 DwarfFile::DwarfFile(AsmPrinter *AP, StringRef Pref, BumpPtrAllocator &DA)
28     : Asm(AP), Abbrevs(AbbrevAllocator), StrPool(DA, *Asm, Pref) {}
29 
30 void DwarfFile::addUnit(std::unique_ptr<DwarfCompileUnit> U) {
31   CUs.push_back(std::move(U));
32 }
33 
34 // Emit the various dwarf units to the unit section USection with
35 // the abbreviations going into ASection.
36 void DwarfFile::emitUnits(bool UseOffsets) {
37   for (const auto &TheU : CUs)
38     emitUnit(TheU.get(), UseOffsets);
39 }
40 
41 void DwarfFile::emitUnit(DwarfUnit *TheU, bool UseOffsets) {
42   if (TheU->getCUNode()->isDebugDirectivesOnly())
43     return;
44 
45   DIE &Die = TheU->getUnitDie();
46   MCSection *USection = TheU->getSection();
47   Asm->OutStreamer->SwitchSection(USection);
48 
49   TheU->emitHeader(UseOffsets);
50 
51   Asm->emitDwarfDIE(Die);
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 void DwarfFile::emitBTFSection(bool IsLittleEndian) {
95   Dwarf2BTF Dwarf2BTF(Asm->OutContext, IsLittleEndian);
96   for (auto &TheU : CUs)
97     Dwarf2BTF.addDwarfCU(TheU.get());
98   Dwarf2BTF.finish();
99 }
100 
101 bool DwarfFile::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
102   auto &ScopeVars = ScopeVariables[LS];
103   const DILocalVariable *DV = Var->getVariable();
104   if (unsigned ArgNum = DV->getArg()) {
105     auto Cached = ScopeVars.Args.find(ArgNum);
106     if (Cached == ScopeVars.Args.end())
107       ScopeVars.Args[ArgNum] = Var;
108     else {
109       Cached->second->addMMIEntry(*Var);
110       return false;
111     }
112   } else {
113     ScopeVars.Locals.push_back(Var);
114   }
115   return true;
116 }
117 
118 void DwarfFile::addScopeLabel(LexicalScope *LS, DbgLabel *Label) {
119   SmallVectorImpl<DbgLabel *> &Labels = ScopeLabels[LS];
120   Labels.push_back(Label);
121 }
122