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