1 //===-- llvm/CodeGen/DwarfFile.cpp - Dwarf Debug Framework ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "DwarfFile.h"
12 #include "DwarfDebug.h"
13 #include "DwarfUnit.h"
14 #include "llvm/MC/MCStreamer.h"
15 #include "llvm/Support/LEB128.h"
16 #include "llvm/IR/DataLayout.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/Target/TargetLoweringObjectFile.h"
21 DwarfFile::DwarfFile(AsmPrinter *AP, DwarfDebug &DD, StringRef Pref,
23 : Asm(AP), DD(DD), StrPool(DA, *Asm, Pref) {}
25 DwarfFile::~DwarfFile() {}
27 // Define a unique number for the abbreviation.
29 void DwarfFile::assignAbbrevNumber(DIEAbbrev &Abbrev) {
30 // Check the set for priors.
31 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
33 // If it's newly added.
34 if (InSet == &Abbrev) {
35 // Add to abbreviation list.
36 Abbreviations.push_back(&Abbrev);
38 // Assign the vector position + 1 as its number.
39 Abbrev.setNumber(Abbreviations.size());
41 // Assign existing abbreviation number.
42 Abbrev.setNumber(InSet->getNumber());
46 void DwarfFile::addUnit(std::unique_ptr<DwarfUnit> U) {
47 CUs.push_back(std::move(U));
50 // Emit the various dwarf units to the unit section USection with
51 // the abbreviations going into ASection.
52 void DwarfFile::emitUnits(const MCSymbol *ASectionSym) {
53 for (const auto &TheU : CUs) {
54 DIE &Die = TheU->getUnitDie();
55 const MCSection *USection = TheU->getSection();
56 Asm->OutStreamer.SwitchSection(USection);
58 // Emit the compile units header.
59 Asm->OutStreamer.EmitLabel(TheU->getLabelBegin());
61 // Emit size of content not including length itself
62 Asm->OutStreamer.AddComment("Length of Unit");
63 Asm->EmitInt32(TheU->getHeaderSize() + Die.getSize());
65 TheU->emitHeader(ASectionSym);
68 Asm->OutStreamer.EmitLabel(TheU->getLabelEnd());
72 // Compute the size and offset for each DIE.
73 void DwarfFile::computeSizeAndOffsets() {
74 // Offset from the first CU in the debug info section is 0 initially.
75 unsigned SecOffset = 0;
77 // Iterate over each compile unit and set the size and offsets for each
78 // DIE within each compile unit. All offsets are CU relative.
79 for (const auto &TheU : CUs) {
80 TheU->setDebugInfoOffset(SecOffset);
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
86 // EndOffset here is CU-relative, after laying out
88 unsigned EndOffset = computeSizeAndOffset(TheU->getUnitDie(), Offset);
89 SecOffset += EndOffset;
92 // Compute the size and offset of a DIE. The offset is relative to start of the
93 // CU. It returns the offset after laying out the DIE.
94 unsigned DwarfFile::computeSizeAndOffset(DIE &Die, unsigned Offset) {
95 // Record the abbreviation.
96 assignAbbrevNumber(Die.getAbbrev());
98 // Get the abbreviation for this DIE.
99 const DIEAbbrev &Abbrev = Die.getAbbrev();
102 Die.setOffset(Offset);
104 // Start the size with the size of abbreviation code.
105 Offset += getULEB128Size(Die.getAbbrevNumber());
107 const SmallVectorImpl<DIEValue *> &Values = Die.getValues();
108 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
110 // Size the DIE attribute values.
111 for (unsigned i = 0, N = Values.size(); i < N; ++i)
112 // Size attribute value.
113 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
116 const auto &Children = Die.getChildren();
118 // Size the DIE children if any.
119 if (!Children.empty()) {
120 assert(Abbrev.hasChildren() && "Children flag not set");
122 for (auto &Child : Children)
123 Offset = computeSizeAndOffset(*Child, Offset);
125 // End of children marker.
126 Offset += sizeof(int8_t);
129 Die.setSize(Offset - Die.getOffset());
132 void DwarfFile::emitAbbrevs(const MCSection *Section) {
133 // Check to see if it is worth the effort.
134 if (!Abbreviations.empty()) {
135 // Start the debug abbrev section.
136 Asm->OutStreamer.SwitchSection(Section);
138 // For each abbrevation.
139 for (const DIEAbbrev *Abbrev : Abbreviations) {
140 // Emit the abbrevations code (base 1 index.)
141 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
143 // Emit the abbreviations data.
147 // Mark end of abbreviations.
148 Asm->EmitULEB128(0, "EOM(3)");
152 // Emit strings into a string section.
153 void DwarfFile::emitStrings(const MCSection *StrSection,
154 const MCSection *OffsetSection) {
155 StrPool.emit(*Asm, StrSection, OffsetSection);
158 // If Var is a current function argument then add it to CurrentFnArguments list.
159 bool DwarfFile::addCurrentFnArgument(DbgVariable *Var, LexicalScope *Scope) {
160 if (Scope->getParent())
162 DIVariable DV = Var->getVariable();
163 if (DV.getTag() != dwarf::DW_TAG_arg_variable)
165 unsigned ArgNo = DV.getArgNumber();
169 auto &CurrentFnArguments = DD.getCurrentFnArguments();
171 // llvm::Function argument size is not good indicator of how many
172 // arguments does the function have at source level.
173 if (ArgNo > CurrentFnArguments.size())
174 CurrentFnArguments.resize(ArgNo * 2);
175 assert(!CurrentFnArguments[ArgNo - 1]);
176 CurrentFnArguments[ArgNo - 1] = Var;