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 "DwarfFile.h"
11 #include "DwarfCompileUnit.h"
12 #include "DwarfDebug.h"
13 #include "DwarfUnit.h"
14 #include "llvm/ADT/SmallVector.h"
15 #include "llvm/CodeGen/AsmPrinter.h"
16 #include "llvm/CodeGen/DIE.h"
17 #include "llvm/IR/DebugInfoMetadata.h"
18 #include "llvm/MC/MCStreamer.h"
19 #include <algorithm>
20 #include <cstdint>
21 
22 using namespace llvm;
23 
24 DwarfFile::DwarfFile(AsmPrinter *AP, StringRef Pref, BumpPtrAllocator &DA)
25     : Asm(AP), Abbrevs(AbbrevAllocator), StrPool(DA, *Asm, Pref) {}
26 
27 void DwarfFile::addUnit(std::unique_ptr<DwarfCompileUnit> U) {
28   CUs.push_back(std::move(U));
29 }
30 
31 void DwarfFile::emitStringOffsetsTableHeader(MCSection *Section) {
32   if (StrPool.empty())
33     return;
34   Asm->OutStreamer->SwitchSection(Section);
35   unsigned EntrySize = 4;
36   // FIXME: DWARF64
37   // We are emitting the header for a contribution to the string offsets
38   // table. The header consists of an entry with the contribution's
39   // size (not including the size of the header), the DWARF version and
40   // 2 bytes of padding.
41   Asm->EmitInt32(StrPool.size() * EntrySize);
42   Asm->EmitInt16(Asm->getDwarfVersion());
43   Asm->EmitInt16(0);
44   // Define the symbol that marks the start of the contribution. It is
45   // referenced by most unit headers via DW_AT_str_offsets_base.
46   // Split units do not use the attribute.
47   if (StringOffsetsStartSym)
48     Asm->OutStreamer->EmitLabel(StringOffsetsStartSym);
49 }
50 
51 // Emit the various dwarf units to the unit section USection with
52 // the abbreviations going into ASection.
53 void DwarfFile::emitUnits(bool UseOffsets) {
54   for (const auto &TheU : CUs)
55     emitUnit(TheU.get(), UseOffsets);
56 }
57 
58 void DwarfFile::emitUnit(DwarfUnit *TheU, bool UseOffsets) {
59   DIE &Die = TheU->getUnitDie();
60   MCSection *USection = TheU->getSection();
61   Asm->OutStreamer->SwitchSection(USection);
62 
63   TheU->emitHeader(UseOffsets);
64 
65   Asm->emitDwarfDIE(Die);
66 }
67 
68 // Compute the size and offset for each DIE.
69 void DwarfFile::computeSizeAndOffsets() {
70   // Offset from the first CU in the debug info section is 0 initially.
71   unsigned SecOffset = 0;
72 
73   // Iterate over each compile unit and set the size and offsets for each
74   // DIE within each compile unit. All offsets are CU relative.
75   for (const auto &TheU : CUs) {
76     TheU->setDebugSectionOffset(SecOffset);
77     SecOffset += computeSizeAndOffsetsForUnit(TheU.get());
78   }
79 }
80 
81 unsigned DwarfFile::computeSizeAndOffsetsForUnit(DwarfUnit *TheU) {
82   // CU-relative offset is reset to 0 here.
83   unsigned Offset = sizeof(int32_t) +      // Length of Unit Info
84                     TheU->getHeaderSize(); // Unit-specific headers
85 
86   // The return value here is CU-relative, after laying out
87   // all of the CU DIE.
88   return computeSizeAndOffset(TheU->getUnitDie(), Offset);
89 }
90 
91 // Compute the size and offset of a DIE. The offset is relative to start of the
92 // CU. It returns the offset after laying out the DIE.
93 unsigned DwarfFile::computeSizeAndOffset(DIE &Die, unsigned Offset) {
94   return Die.computeOffsetsAndAbbrevs(Asm, Abbrevs, Offset);
95 }
96 
97 void DwarfFile::emitAbbrevs(MCSection *Section) { Abbrevs.Emit(Asm, Section); }
98 
99 // Emit strings into a string section.
100 void DwarfFile::emitStrings(MCSection *StrSection, MCSection *OffsetSection,
101                             bool UseRelativeOffsets) {
102   StrPool.emit(*Asm, StrSection, OffsetSection, UseRelativeOffsets);
103 }
104 
105 bool DwarfFile::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
106   auto &ScopeVars = ScopeVariables[LS];
107   const DILocalVariable *DV = Var->getVariable();
108   if (unsigned ArgNum = DV->getArg()) {
109     auto Cached = ScopeVars.Args.find(ArgNum);
110     if (Cached == ScopeVars.Args.end())
111       ScopeVars.Args[ArgNum] = Var;
112     else {
113       Cached->second->addMMIEntry(*Var);
114       return false;
115     }
116   } else {
117     ScopeVars.Locals.push_back(Var);
118   }
119   return true;
120 }
121