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, StringRef Pref, BumpPtrAllocator &DA)
22 : Asm(AP), StrPool(DA, *Asm, Pref) {}
24 DwarfFile::~DwarfFile() {}
26 // Define a unique number for the abbreviation.
28 void DwarfFile::assignAbbrevNumber(DIEAbbrev &Abbrev) {
29 // Check the set for priors.
30 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
32 // If it's newly added.
33 if (InSet == &Abbrev) {
34 // Add to abbreviation list.
35 Abbreviations.push_back(&Abbrev);
37 // Assign the vector position + 1 as its number.
38 Abbrev.setNumber(Abbreviations.size());
40 // Assign existing abbreviation number.
41 Abbrev.setNumber(InSet->getNumber());
45 void DwarfFile::addUnit(std::unique_ptr<DwarfUnit> U) {
46 CUs.push_back(std::move(U));
49 // Emit the various dwarf units to the unit section USection with
50 // the abbreviations going into ASection.
51 void DwarfFile::emitUnits(DwarfDebug *DD, const MCSymbol *ASectionSym) {
52 for (const auto &TheU : CUs) {
53 DIE &Die = TheU->getUnitDie();
54 const MCSection *USection = TheU->getSection();
55 Asm->OutStreamer.SwitchSection(USection);
57 // Emit the compile units header.
58 Asm->OutStreamer.EmitLabel(TheU->getLabelBegin());
60 // Emit size of content not including length itself
61 Asm->OutStreamer.AddComment("Length of Unit");
62 Asm->EmitInt32(TheU->getHeaderSize() + Die.getSize());
64 TheU->emitHeader(ASectionSym);
67 Asm->OutStreamer.EmitLabel(TheU->getLabelEnd());
70 // Compute the size and offset for each DIE.
71 void DwarfFile::computeSizeAndOffsets() {
72 // Offset from the first CU in the debug info section is 0 initially.
73 unsigned SecOffset = 0;
75 // Iterate over each compile unit and set the size and offsets for each
76 // DIE within each compile unit. All offsets are CU relative.
77 for (const auto &TheU : CUs) {
78 TheU->setDebugInfoOffset(SecOffset);
80 // CU-relative offset is reset to 0 here.
81 unsigned Offset = sizeof(int32_t) + // Length of Unit Info
82 TheU->getHeaderSize(); // Unit-specific headers
84 // EndOffset here is CU-relative, after laying out
86 unsigned EndOffset = computeSizeAndOffset(TheU->getUnitDie(), Offset);
87 SecOffset += EndOffset;
90 // Compute the size and offset of a DIE. The offset is relative to start of the
91 // CU. It returns the offset after laying out the DIE.
92 unsigned DwarfFile::computeSizeAndOffset(DIE &Die, unsigned Offset) {
93 // Record the abbreviation.
94 assignAbbrevNumber(Die.getAbbrev());
96 // Get the abbreviation for this DIE.
97 const DIEAbbrev &Abbrev = Die.getAbbrev();
100 Die.setOffset(Offset);
102 // Start the size with the size of abbreviation code.
103 Offset += getULEB128Size(Die.getAbbrevNumber());
105 const SmallVectorImpl<DIEValue *> &Values = Die.getValues();
106 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
108 // Size the DIE attribute values.
109 for (unsigned i = 0, N = Values.size(); i < N; ++i)
110 // Size attribute value.
111 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
114 const auto &Children = Die.getChildren();
116 // Size the DIE children if any.
117 if (!Children.empty()) {
118 assert(Abbrev.hasChildren() && "Children flag not set");
120 for (auto &Child : Children)
121 Offset = computeSizeAndOffset(*Child, Offset);
123 // End of children marker.
124 Offset += sizeof(int8_t);
127 Die.setSize(Offset - Die.getOffset());
130 void DwarfFile::emitAbbrevs(const MCSection *Section) {
131 // Check to see if it is worth the effort.
132 if (!Abbreviations.empty()) {
133 // Start the debug abbrev section.
134 Asm->OutStreamer.SwitchSection(Section);
136 // For each abbrevation.
137 for (const DIEAbbrev *Abbrev : Abbreviations) {
138 // Emit the abbrevations code (base 1 index.)
139 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
141 // Emit the abbreviations data.
145 // Mark end of abbreviations.
146 Asm->EmitULEB128(0, "EOM(3)");
150 // Emit strings into a string section.
151 void DwarfFile::emitStrings(const MCSection *StrSection,
152 const MCSection *OffsetSection,
153 const MCSymbol *StrSecSym) {
154 StrPool.emit(*Asm, StrSection, OffsetSection, StrSecSym);