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"
11 #include "DwarfDebug.h"
12 #include "DwarfUnit.h"
13 #include "llvm/ADT/STLExtras.h"
14 #include "llvm/IR/DataLayout.h"
15 #include "llvm/MC/MCStreamer.h"
16 #include "llvm/Support/LEB128.h"
17 #include "llvm/Target/TargetLoweringObjectFile.h"
20 DwarfFile::DwarfFile(AsmPrinter *AP, StringRef Pref, BumpPtrAllocator &DA)
21 : Asm(AP), StrPool(DA, *Asm, Pref) {}
23 DwarfFile::~DwarfFile() {}
25 // Define a unique number for the abbreviation.
27 void DwarfFile::assignAbbrevNumber(DIEAbbrev &Abbrev) {
28 // Check the set for priors.
29 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
31 // If it's newly added.
32 if (InSet == &Abbrev) {
33 // Add to abbreviation list.
34 Abbreviations.push_back(&Abbrev);
36 // Assign the vector position + 1 as its number.
37 Abbrev.setNumber(Abbreviations.size());
39 // Assign existing abbreviation number.
40 Abbrev.setNumber(InSet->getNumber());
44 void DwarfFile::addUnit(std::unique_ptr<DwarfUnit> U) {
45 CUs.push_back(std::move(U));
48 // Emit the various dwarf units to the unit section USection with
49 // the abbreviations going into ASection.
50 void DwarfFile::emitUnits(bool UseOffsets) {
51 for (const auto &TheU : CUs) {
52 DIE &Die = TheU->getUnitDie();
53 const MCSection *USection = TheU->getSection();
54 Asm->OutStreamer.SwitchSection(USection);
56 TheU->emitHeader(UseOffsets);
58 Asm->emitDwarfDIE(Die);
62 // Compute the size and offset for each DIE.
63 void DwarfFile::computeSizeAndOffsets() {
64 // Offset from the first CU in the debug info section is 0 initially.
65 unsigned SecOffset = 0;
67 // Iterate over each compile unit and set the size and offsets for each
68 // DIE within each compile unit. All offsets are CU relative.
69 for (const auto &TheU : CUs) {
70 TheU->setDebugInfoOffset(SecOffset);
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
76 // EndOffset here is CU-relative, after laying out
78 unsigned EndOffset = computeSizeAndOffset(TheU->getUnitDie(), Offset);
79 SecOffset += EndOffset;
82 // Compute the size and offset of a DIE. The offset is relative to start of the
83 // CU. It returns the offset after laying out the DIE.
84 unsigned DwarfFile::computeSizeAndOffset(DIE &Die, unsigned Offset) {
85 // Record the abbreviation.
86 assignAbbrevNumber(Die.getAbbrev());
88 // Get the abbreviation for this DIE.
89 const DIEAbbrev &Abbrev = Die.getAbbrev();
92 Die.setOffset(Offset);
94 // Start the size with the size of abbreviation code.
95 Offset += getULEB128Size(Die.getAbbrevNumber());
97 const SmallVectorImpl<DIEValue *> &Values = Die.getValues();
98 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
100 // Size the DIE attribute values.
101 for (unsigned i = 0, N = Values.size(); i < N; ++i)
102 // Size attribute value.
103 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
106 const auto &Children = Die.getChildren();
108 // Size the DIE children if any.
109 if (!Children.empty()) {
110 assert(Abbrev.hasChildren() && "Children flag not set");
112 for (auto &Child : Children)
113 Offset = computeSizeAndOffset(*Child, Offset);
115 // End of children marker.
116 Offset += sizeof(int8_t);
119 Die.setSize(Offset - Die.getOffset());
123 void DwarfFile::emitAbbrevs(const MCSection *Section) {
124 // Check to see if it is worth the effort.
125 if (!Abbreviations.empty()) {
126 // Start the debug abbrev section.
127 Asm->OutStreamer.SwitchSection(Section);
128 Asm->emitDwarfAbbrevs(Abbreviations);
132 // Emit strings into a string section.
133 void DwarfFile::emitStrings(const MCSection *StrSection,
134 const MCSection *OffsetSection) {
135 StrPool.emit(*Asm, StrSection, OffsetSection);
138 bool DwarfFile::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
139 SmallVectorImpl<DbgVariable *> &Vars = ScopeVariables[LS];
140 DIVariable DV = Var->getVariable();
141 // Variables with positive arg numbers are parameters.
142 if (unsigned ArgNum = DV->getArg()) {
143 // Keep all parameters in order at the start of the variable list to ensure
144 // function types are correct (no out-of-order parameters)
146 // This could be improved by only doing it for optimized builds (unoptimized
147 // builds have the right order to begin with), searching from the back (this
148 // would catch the unoptimized case quickly), or doing a binary search
149 // rather than linear search.
150 auto I = Vars.begin();
151 while (I != Vars.end()) {
152 unsigned CurNum = (*I)->getVariable()->getArg();
153 // A local (non-parameter) variable has been found, insert immediately
157 // A later indexed parameter has been found, insert immediately before it.
160 if (CurNum == ArgNum) {
161 (*I)->addMMIEntry(*Var);