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