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 void DwarfFile::emitStringOffsetsTableHeader(MCSection *Section) { 32 if (StrPool.empty()) 33 return; 34 Asm->OutStreamer->SwitchSection(Section); 35 unsigned EntrySize = 4; 36 // FIXME: DWARF64 37 // We are emitting the header for a contribution to the string offsets 38 // table. The header consists of an entry with the contribution's 39 // size (not including the size of the header), the DWARF version and 40 // 2 bytes of padding. 41 Asm->EmitInt32(StrPool.size() * EntrySize); 42 Asm->EmitInt16(Asm->getDwarfVersion()); 43 Asm->EmitInt16(0); 44 // Define the symbol that marks the start of the contribution. It is 45 // referenced by most unit headers via DW_AT_str_offsets_base. 46 // Split units do not use the attribute. 47 if (StringOffsetsStartSym) 48 Asm->OutStreamer->EmitLabel(StringOffsetsStartSym); 49 } 50 51 // Emit the various dwarf units to the unit section USection with 52 // the abbreviations going into ASection. 53 void DwarfFile::emitUnits(bool UseOffsets) { 54 for (const auto &TheU : CUs) 55 emitUnit(TheU.get(), UseOffsets); 56 } 57 58 void DwarfFile::emitUnit(DwarfUnit *TheU, bool UseOffsets) { 59 DIE &Die = TheU->getUnitDie(); 60 MCSection *USection = TheU->getSection(); 61 Asm->OutStreamer->SwitchSection(USection); 62 63 TheU->emitHeader(UseOffsets); 64 65 Asm->emitDwarfDIE(Die); 66 } 67 68 // Compute the size and offset for each DIE. 69 void DwarfFile::computeSizeAndOffsets() { 70 // Offset from the first CU in the debug info section is 0 initially. 71 unsigned SecOffset = 0; 72 73 // Iterate over each compile unit and set the size and offsets for each 74 // DIE within each compile unit. All offsets are CU relative. 75 for (const auto &TheU : CUs) { 76 TheU->setDebugSectionOffset(SecOffset); 77 SecOffset += computeSizeAndOffsetsForUnit(TheU.get()); 78 } 79 } 80 81 unsigned DwarfFile::computeSizeAndOffsetsForUnit(DwarfUnit *TheU) { 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 85 86 // The return value here is CU-relative, after laying out 87 // all of the CU DIE. 88 return computeSizeAndOffset(TheU->getUnitDie(), Offset); 89 } 90 91 // Compute the size and offset of a DIE. The offset is relative to start of the 92 // CU. It returns the offset after laying out the DIE. 93 unsigned DwarfFile::computeSizeAndOffset(DIE &Die, unsigned Offset) { 94 return Die.computeOffsetsAndAbbrevs(Asm, Abbrevs, Offset); 95 } 96 97 void DwarfFile::emitAbbrevs(MCSection *Section) { Abbrevs.Emit(Asm, Section); } 98 99 // Emit strings into a string section. 100 void DwarfFile::emitStrings(MCSection *StrSection, MCSection *OffsetSection, 101 bool UseRelativeOffsets) { 102 StrPool.emit(*Asm, StrSection, OffsetSection, UseRelativeOffsets); 103 } 104 105 bool DwarfFile::addScopeVariable(LexicalScope *LS, DbgVariable *Var) { 106 SmallVectorImpl<DbgVariable *> &Vars = ScopeVariables[LS]; 107 const DILocalVariable *DV = Var->getVariable(); 108 // Variables with positive arg numbers are parameters. 109 if (unsigned ArgNum = DV->getArg()) { 110 // Keep all parameters in order at the start of the variable list to ensure 111 // function types are correct (no out-of-order parameters) 112 // 113 // This could be improved by only doing it for optimized builds (unoptimized 114 // builds have the right order to begin with), searching from the back (this 115 // would catch the unoptimized case quickly), or doing a binary search 116 // rather than linear search. 117 auto I = Vars.begin(); 118 while (I != Vars.end()) { 119 unsigned CurNum = (*I)->getVariable()->getArg(); 120 // A local (non-parameter) variable has been found, insert immediately 121 // before it. 122 if (CurNum == 0) 123 break; 124 // A later indexed parameter has been found, insert immediately before it. 125 if (CurNum > ArgNum) 126 break; 127 if (CurNum == ArgNum) { 128 (*I)->addMMIEntry(*Var); 129 return false; 130 } 131 ++I; 132 } 133 Vars.insert(I, Var); 134 return true; 135 } 136 137 Vars.push_back(Var); 138 return true; 139 } 140