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