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 DIE &Die = TheU->getUnitDie();
40 MCSection *USection = TheU->getSection();
41 Asm->OutStreamer->SwitchSection(USection);
42
43 TheU->emitHeader(UseOffsets);
44
45 Asm->emitDwarfDIE(Die);
46 }
47
48 // Compute the size and offset for each DIE.
computeSizeAndOffsets()49 void DwarfFile::computeSizeAndOffsets() {
50 // Offset from the first CU in the debug info section is 0 initially.
51 unsigned SecOffset = 0;
52
53 // Iterate over each compile unit and set the size and offsets for each
54 // DIE within each compile unit. All offsets are CU relative.
55 for (const auto &TheU : CUs) {
56 TheU->setDebugSectionOffset(SecOffset);
57 SecOffset += computeSizeAndOffsetsForUnit(TheU.get());
58 }
59 }
60
computeSizeAndOffsetsForUnit(DwarfUnit * TheU)61 unsigned DwarfFile::computeSizeAndOffsetsForUnit(DwarfUnit *TheU) {
62 // CU-relative offset is reset to 0 here.
63 unsigned Offset = sizeof(int32_t) + // Length of Unit Info
64 TheU->getHeaderSize(); // Unit-specific headers
65
66 // The return value here is CU-relative, after laying out
67 // all of the CU DIE.
68 return computeSizeAndOffset(TheU->getUnitDie(), Offset);
69 }
70
71 // Compute the size and offset of a DIE. The offset is relative to start of the
72 // CU. It returns the offset after laying out the DIE.
computeSizeAndOffset(DIE & Die,unsigned Offset)73 unsigned DwarfFile::computeSizeAndOffset(DIE &Die, unsigned Offset) {
74 return Die.computeOffsetsAndAbbrevs(Asm, Abbrevs, Offset);
75 }
76
emitAbbrevs(MCSection * Section)77 void DwarfFile::emitAbbrevs(MCSection *Section) { Abbrevs.Emit(Asm, Section); }
78
79 // Emit strings into a string section.
emitStrings(MCSection * StrSection,MCSection * OffsetSection,bool UseRelativeOffsets)80 void DwarfFile::emitStrings(MCSection *StrSection, MCSection *OffsetSection,
81 bool UseRelativeOffsets) {
82 StrPool.emit(*Asm, StrSection, OffsetSection, UseRelativeOffsets);
83 }
84
addScopeVariable(LexicalScope * LS,DbgVariable * Var)85 bool DwarfFile::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
86 auto &ScopeVars = ScopeVariables[LS];
87 const DILocalVariable *DV = Var->getVariable();
88 if (unsigned ArgNum = DV->getArg()) {
89 auto Cached = ScopeVars.Args.find(ArgNum);
90 if (Cached == ScopeVars.Args.end())
91 ScopeVars.Args[ArgNum] = Var;
92 else {
93 Cached->second->addMMIEntry(*Var);
94 return false;
95 }
96 } else {
97 ScopeVars.Locals.push_back(Var);
98 }
99 return true;
100 }
101