1*0b57cec5SDimitry Andric //===- llvm/CodeGen/DwarfDebug.cpp - Dwarf Debug Framework ----------------===// 2*0b57cec5SDimitry Andric // 3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*0b57cec5SDimitry Andric // 7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 8*0b57cec5SDimitry Andric // 9*0b57cec5SDimitry Andric // This file contains support for writing dwarf debug info into asm files. 10*0b57cec5SDimitry Andric // 11*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 12*0b57cec5SDimitry Andric 13*0b57cec5SDimitry Andric #include "DwarfDebug.h" 14*0b57cec5SDimitry Andric #include "ByteStreamer.h" 15*0b57cec5SDimitry Andric #include "DIEHash.h" 16*0b57cec5SDimitry Andric #include "DebugLocEntry.h" 17*0b57cec5SDimitry Andric #include "DebugLocStream.h" 18*0b57cec5SDimitry Andric #include "DwarfCompileUnit.h" 19*0b57cec5SDimitry Andric #include "DwarfExpression.h" 20*0b57cec5SDimitry Andric #include "DwarfFile.h" 21*0b57cec5SDimitry Andric #include "DwarfUnit.h" 22*0b57cec5SDimitry Andric #include "llvm/ADT/APInt.h" 23*0b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h" 24*0b57cec5SDimitry Andric #include "llvm/ADT/DenseSet.h" 25*0b57cec5SDimitry Andric #include "llvm/ADT/MapVector.h" 26*0b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h" 27*0b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h" 28*0b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h" 29*0b57cec5SDimitry Andric #include "llvm/ADT/Triple.h" 30*0b57cec5SDimitry Andric #include "llvm/ADT/Twine.h" 31*0b57cec5SDimitry Andric #include "llvm/BinaryFormat/Dwarf.h" 32*0b57cec5SDimitry Andric #include "llvm/CodeGen/AccelTable.h" 33*0b57cec5SDimitry Andric #include "llvm/CodeGen/AsmPrinter.h" 34*0b57cec5SDimitry Andric #include "llvm/CodeGen/DIE.h" 35*0b57cec5SDimitry Andric #include "llvm/CodeGen/LexicalScopes.h" 36*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h" 37*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h" 38*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstr.h" 39*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineModuleInfo.h" 40*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineOperand.h" 41*0b57cec5SDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h" 42*0b57cec5SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h" 43*0b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h" 44*0b57cec5SDimitry Andric #include "llvm/DebugInfo/DWARF/DWARFExpression.h" 45*0b57cec5SDimitry Andric #include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h" 46*0b57cec5SDimitry Andric #include "llvm/IR/Constants.h" 47*0b57cec5SDimitry Andric #include "llvm/IR/DebugInfoMetadata.h" 48*0b57cec5SDimitry Andric #include "llvm/IR/DebugLoc.h" 49*0b57cec5SDimitry Andric #include "llvm/IR/Function.h" 50*0b57cec5SDimitry Andric #include "llvm/IR/GlobalVariable.h" 51*0b57cec5SDimitry Andric #include "llvm/IR/Module.h" 52*0b57cec5SDimitry Andric #include "llvm/MC/MCAsmInfo.h" 53*0b57cec5SDimitry Andric #include "llvm/MC/MCContext.h" 54*0b57cec5SDimitry Andric #include "llvm/MC/MCDwarf.h" 55*0b57cec5SDimitry Andric #include "llvm/MC/MCSection.h" 56*0b57cec5SDimitry Andric #include "llvm/MC/MCStreamer.h" 57*0b57cec5SDimitry Andric #include "llvm/MC/MCSymbol.h" 58*0b57cec5SDimitry Andric #include "llvm/MC/MCTargetOptions.h" 59*0b57cec5SDimitry Andric #include "llvm/MC/MachineLocation.h" 60*0b57cec5SDimitry Andric #include "llvm/MC/SectionKind.h" 61*0b57cec5SDimitry Andric #include "llvm/Pass.h" 62*0b57cec5SDimitry Andric #include "llvm/Support/Casting.h" 63*0b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h" 64*0b57cec5SDimitry Andric #include "llvm/Support/Debug.h" 65*0b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h" 66*0b57cec5SDimitry Andric #include "llvm/Support/MD5.h" 67*0b57cec5SDimitry Andric #include "llvm/Support/MathExtras.h" 68*0b57cec5SDimitry Andric #include "llvm/Support/Timer.h" 69*0b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 70*0b57cec5SDimitry Andric #include "llvm/Target/TargetLoweringObjectFile.h" 71*0b57cec5SDimitry Andric #include "llvm/Target/TargetMachine.h" 72*0b57cec5SDimitry Andric #include "llvm/Target/TargetOptions.h" 73*0b57cec5SDimitry Andric #include <algorithm> 74*0b57cec5SDimitry Andric #include <cassert> 75*0b57cec5SDimitry Andric #include <cstddef> 76*0b57cec5SDimitry Andric #include <cstdint> 77*0b57cec5SDimitry Andric #include <iterator> 78*0b57cec5SDimitry Andric #include <string> 79*0b57cec5SDimitry Andric #include <utility> 80*0b57cec5SDimitry Andric #include <vector> 81*0b57cec5SDimitry Andric 82*0b57cec5SDimitry Andric using namespace llvm; 83*0b57cec5SDimitry Andric 84*0b57cec5SDimitry Andric #define DEBUG_TYPE "dwarfdebug" 85*0b57cec5SDimitry Andric 86*0b57cec5SDimitry Andric static cl::opt<bool> 87*0b57cec5SDimitry Andric DisableDebugInfoPrinting("disable-debug-info-print", cl::Hidden, 88*0b57cec5SDimitry Andric cl::desc("Disable debug info printing")); 89*0b57cec5SDimitry Andric 90*0b57cec5SDimitry Andric static cl::opt<bool> UseDwarfRangesBaseAddressSpecifier( 91*0b57cec5SDimitry Andric "use-dwarf-ranges-base-address-specifier", cl::Hidden, 92*0b57cec5SDimitry Andric cl::desc("Use base address specifiers in debug_ranges"), cl::init(false)); 93*0b57cec5SDimitry Andric 94*0b57cec5SDimitry Andric static cl::opt<bool> GenerateARangeSection("generate-arange-section", 95*0b57cec5SDimitry Andric cl::Hidden, 96*0b57cec5SDimitry Andric cl::desc("Generate dwarf aranges"), 97*0b57cec5SDimitry Andric cl::init(false)); 98*0b57cec5SDimitry Andric 99*0b57cec5SDimitry Andric static cl::opt<bool> 100*0b57cec5SDimitry Andric GenerateDwarfTypeUnits("generate-type-units", cl::Hidden, 101*0b57cec5SDimitry Andric cl::desc("Generate DWARF4 type units."), 102*0b57cec5SDimitry Andric cl::init(false)); 103*0b57cec5SDimitry Andric 104*0b57cec5SDimitry Andric static cl::opt<bool> SplitDwarfCrossCuReferences( 105*0b57cec5SDimitry Andric "split-dwarf-cross-cu-references", cl::Hidden, 106*0b57cec5SDimitry Andric cl::desc("Enable cross-cu references in DWO files"), cl::init(false)); 107*0b57cec5SDimitry Andric 108*0b57cec5SDimitry Andric enum DefaultOnOff { Default, Enable, Disable }; 109*0b57cec5SDimitry Andric 110*0b57cec5SDimitry Andric static cl::opt<DefaultOnOff> UnknownLocations( 111*0b57cec5SDimitry Andric "use-unknown-locations", cl::Hidden, 112*0b57cec5SDimitry Andric cl::desc("Make an absence of debug location information explicit."), 113*0b57cec5SDimitry Andric cl::values(clEnumVal(Default, "At top of block or after label"), 114*0b57cec5SDimitry Andric clEnumVal(Enable, "In all cases"), clEnumVal(Disable, "Never")), 115*0b57cec5SDimitry Andric cl::init(Default)); 116*0b57cec5SDimitry Andric 117*0b57cec5SDimitry Andric static cl::opt<AccelTableKind> AccelTables( 118*0b57cec5SDimitry Andric "accel-tables", cl::Hidden, cl::desc("Output dwarf accelerator tables."), 119*0b57cec5SDimitry Andric cl::values(clEnumValN(AccelTableKind::Default, "Default", 120*0b57cec5SDimitry Andric "Default for platform"), 121*0b57cec5SDimitry Andric clEnumValN(AccelTableKind::None, "Disable", "Disabled."), 122*0b57cec5SDimitry Andric clEnumValN(AccelTableKind::Apple, "Apple", "Apple"), 123*0b57cec5SDimitry Andric clEnumValN(AccelTableKind::Dwarf, "Dwarf", "DWARF")), 124*0b57cec5SDimitry Andric cl::init(AccelTableKind::Default)); 125*0b57cec5SDimitry Andric 126*0b57cec5SDimitry Andric static cl::opt<DefaultOnOff> 127*0b57cec5SDimitry Andric DwarfInlinedStrings("dwarf-inlined-strings", cl::Hidden, 128*0b57cec5SDimitry Andric cl::desc("Use inlined strings rather than string section."), 129*0b57cec5SDimitry Andric cl::values(clEnumVal(Default, "Default for platform"), 130*0b57cec5SDimitry Andric clEnumVal(Enable, "Enabled"), 131*0b57cec5SDimitry Andric clEnumVal(Disable, "Disabled")), 132*0b57cec5SDimitry Andric cl::init(Default)); 133*0b57cec5SDimitry Andric 134*0b57cec5SDimitry Andric static cl::opt<bool> 135*0b57cec5SDimitry Andric NoDwarfRangesSection("no-dwarf-ranges-section", cl::Hidden, 136*0b57cec5SDimitry Andric cl::desc("Disable emission .debug_ranges section."), 137*0b57cec5SDimitry Andric cl::init(false)); 138*0b57cec5SDimitry Andric 139*0b57cec5SDimitry Andric static cl::opt<DefaultOnOff> DwarfSectionsAsReferences( 140*0b57cec5SDimitry Andric "dwarf-sections-as-references", cl::Hidden, 141*0b57cec5SDimitry Andric cl::desc("Use sections+offset as references rather than labels."), 142*0b57cec5SDimitry Andric cl::values(clEnumVal(Default, "Default for platform"), 143*0b57cec5SDimitry Andric clEnumVal(Enable, "Enabled"), clEnumVal(Disable, "Disabled")), 144*0b57cec5SDimitry Andric cl::init(Default)); 145*0b57cec5SDimitry Andric 146*0b57cec5SDimitry Andric enum LinkageNameOption { 147*0b57cec5SDimitry Andric DefaultLinkageNames, 148*0b57cec5SDimitry Andric AllLinkageNames, 149*0b57cec5SDimitry Andric AbstractLinkageNames 150*0b57cec5SDimitry Andric }; 151*0b57cec5SDimitry Andric 152*0b57cec5SDimitry Andric static cl::opt<LinkageNameOption> 153*0b57cec5SDimitry Andric DwarfLinkageNames("dwarf-linkage-names", cl::Hidden, 154*0b57cec5SDimitry Andric cl::desc("Which DWARF linkage-name attributes to emit."), 155*0b57cec5SDimitry Andric cl::values(clEnumValN(DefaultLinkageNames, "Default", 156*0b57cec5SDimitry Andric "Default for platform"), 157*0b57cec5SDimitry Andric clEnumValN(AllLinkageNames, "All", "All"), 158*0b57cec5SDimitry Andric clEnumValN(AbstractLinkageNames, "Abstract", 159*0b57cec5SDimitry Andric "Abstract subprograms")), 160*0b57cec5SDimitry Andric cl::init(DefaultLinkageNames)); 161*0b57cec5SDimitry Andric 162*0b57cec5SDimitry Andric static const char *const DWARFGroupName = "dwarf"; 163*0b57cec5SDimitry Andric static const char *const DWARFGroupDescription = "DWARF Emission"; 164*0b57cec5SDimitry Andric static const char *const DbgTimerName = "writer"; 165*0b57cec5SDimitry Andric static const char *const DbgTimerDescription = "DWARF Debug Writer"; 166*0b57cec5SDimitry Andric static constexpr unsigned ULEB128PadSize = 4; 167*0b57cec5SDimitry Andric 168*0b57cec5SDimitry Andric void DebugLocDwarfExpression::emitOp(uint8_t Op, const char *Comment) { 169*0b57cec5SDimitry Andric BS.EmitInt8( 170*0b57cec5SDimitry Andric Op, Comment ? Twine(Comment) + " " + dwarf::OperationEncodingString(Op) 171*0b57cec5SDimitry Andric : dwarf::OperationEncodingString(Op)); 172*0b57cec5SDimitry Andric } 173*0b57cec5SDimitry Andric 174*0b57cec5SDimitry Andric void DebugLocDwarfExpression::emitSigned(int64_t Value) { 175*0b57cec5SDimitry Andric BS.EmitSLEB128(Value, Twine(Value)); 176*0b57cec5SDimitry Andric } 177*0b57cec5SDimitry Andric 178*0b57cec5SDimitry Andric void DebugLocDwarfExpression::emitUnsigned(uint64_t Value) { 179*0b57cec5SDimitry Andric BS.EmitULEB128(Value, Twine(Value)); 180*0b57cec5SDimitry Andric } 181*0b57cec5SDimitry Andric 182*0b57cec5SDimitry Andric void DebugLocDwarfExpression::emitData1(uint8_t Value) { 183*0b57cec5SDimitry Andric BS.EmitInt8(Value, Twine(Value)); 184*0b57cec5SDimitry Andric } 185*0b57cec5SDimitry Andric 186*0b57cec5SDimitry Andric void DebugLocDwarfExpression::emitBaseTypeRef(uint64_t Idx) { 187*0b57cec5SDimitry Andric assert(Idx < (1ULL << (ULEB128PadSize * 7)) && "Idx wont fit"); 188*0b57cec5SDimitry Andric BS.EmitULEB128(Idx, Twine(Idx), ULEB128PadSize); 189*0b57cec5SDimitry Andric } 190*0b57cec5SDimitry Andric 191*0b57cec5SDimitry Andric bool DebugLocDwarfExpression::isFrameRegister(const TargetRegisterInfo &TRI, 192*0b57cec5SDimitry Andric unsigned MachineReg) { 193*0b57cec5SDimitry Andric // This information is not available while emitting .debug_loc entries. 194*0b57cec5SDimitry Andric return false; 195*0b57cec5SDimitry Andric } 196*0b57cec5SDimitry Andric 197*0b57cec5SDimitry Andric bool DbgVariable::isBlockByrefVariable() const { 198*0b57cec5SDimitry Andric assert(getVariable() && "Invalid complex DbgVariable!"); 199*0b57cec5SDimitry Andric return getVariable()->getType()->isBlockByrefStruct(); 200*0b57cec5SDimitry Andric } 201*0b57cec5SDimitry Andric 202*0b57cec5SDimitry Andric const DIType *DbgVariable::getType() const { 203*0b57cec5SDimitry Andric DIType *Ty = getVariable()->getType(); 204*0b57cec5SDimitry Andric // FIXME: isBlockByrefVariable should be reformulated in terms of complex 205*0b57cec5SDimitry Andric // addresses instead. 206*0b57cec5SDimitry Andric if (Ty->isBlockByrefStruct()) { 207*0b57cec5SDimitry Andric /* Byref variables, in Blocks, are declared by the programmer as 208*0b57cec5SDimitry Andric "SomeType VarName;", but the compiler creates a 209*0b57cec5SDimitry Andric __Block_byref_x_VarName struct, and gives the variable VarName 210*0b57cec5SDimitry Andric either the struct, or a pointer to the struct, as its type. This 211*0b57cec5SDimitry Andric is necessary for various behind-the-scenes things the compiler 212*0b57cec5SDimitry Andric needs to do with by-reference variables in blocks. 213*0b57cec5SDimitry Andric 214*0b57cec5SDimitry Andric However, as far as the original *programmer* is concerned, the 215*0b57cec5SDimitry Andric variable should still have type 'SomeType', as originally declared. 216*0b57cec5SDimitry Andric 217*0b57cec5SDimitry Andric The following function dives into the __Block_byref_x_VarName 218*0b57cec5SDimitry Andric struct to find the original type of the variable. This will be 219*0b57cec5SDimitry Andric passed back to the code generating the type for the Debug 220*0b57cec5SDimitry Andric Information Entry for the variable 'VarName'. 'VarName' will then 221*0b57cec5SDimitry Andric have the original type 'SomeType' in its debug information. 222*0b57cec5SDimitry Andric 223*0b57cec5SDimitry Andric The original type 'SomeType' will be the type of the field named 224*0b57cec5SDimitry Andric 'VarName' inside the __Block_byref_x_VarName struct. 225*0b57cec5SDimitry Andric 226*0b57cec5SDimitry Andric NOTE: In order for this to not completely fail on the debugger 227*0b57cec5SDimitry Andric side, the Debug Information Entry for the variable VarName needs to 228*0b57cec5SDimitry Andric have a DW_AT_location that tells the debugger how to unwind through 229*0b57cec5SDimitry Andric the pointers and __Block_byref_x_VarName struct to find the actual 230*0b57cec5SDimitry Andric value of the variable. The function addBlockByrefType does this. */ 231*0b57cec5SDimitry Andric DIType *subType = Ty; 232*0b57cec5SDimitry Andric uint16_t tag = Ty->getTag(); 233*0b57cec5SDimitry Andric 234*0b57cec5SDimitry Andric if (tag == dwarf::DW_TAG_pointer_type) 235*0b57cec5SDimitry Andric subType = cast<DIDerivedType>(Ty)->getBaseType(); 236*0b57cec5SDimitry Andric 237*0b57cec5SDimitry Andric auto Elements = cast<DICompositeType>(subType)->getElements(); 238*0b57cec5SDimitry Andric for (unsigned i = 0, N = Elements.size(); i < N; ++i) { 239*0b57cec5SDimitry Andric auto *DT = cast<DIDerivedType>(Elements[i]); 240*0b57cec5SDimitry Andric if (getName() == DT->getName()) 241*0b57cec5SDimitry Andric return DT->getBaseType(); 242*0b57cec5SDimitry Andric } 243*0b57cec5SDimitry Andric } 244*0b57cec5SDimitry Andric return Ty; 245*0b57cec5SDimitry Andric } 246*0b57cec5SDimitry Andric 247*0b57cec5SDimitry Andric /// Get .debug_loc entry for the instruction range starting at MI. 248*0b57cec5SDimitry Andric static DbgValueLoc getDebugLocValue(const MachineInstr *MI) { 249*0b57cec5SDimitry Andric const DIExpression *Expr = MI->getDebugExpression(); 250*0b57cec5SDimitry Andric assert(MI->getNumOperands() == 4); 251*0b57cec5SDimitry Andric if (MI->getOperand(0).isReg()) { 252*0b57cec5SDimitry Andric auto RegOp = MI->getOperand(0); 253*0b57cec5SDimitry Andric auto Op1 = MI->getOperand(1); 254*0b57cec5SDimitry Andric // If the second operand is an immediate, this is a 255*0b57cec5SDimitry Andric // register-indirect address. 256*0b57cec5SDimitry Andric assert((!Op1.isImm() || (Op1.getImm() == 0)) && "unexpected offset"); 257*0b57cec5SDimitry Andric MachineLocation MLoc(RegOp.getReg(), Op1.isImm()); 258*0b57cec5SDimitry Andric return DbgValueLoc(Expr, MLoc); 259*0b57cec5SDimitry Andric } 260*0b57cec5SDimitry Andric if (MI->getOperand(0).isImm()) 261*0b57cec5SDimitry Andric return DbgValueLoc(Expr, MI->getOperand(0).getImm()); 262*0b57cec5SDimitry Andric if (MI->getOperand(0).isFPImm()) 263*0b57cec5SDimitry Andric return DbgValueLoc(Expr, MI->getOperand(0).getFPImm()); 264*0b57cec5SDimitry Andric if (MI->getOperand(0).isCImm()) 265*0b57cec5SDimitry Andric return DbgValueLoc(Expr, MI->getOperand(0).getCImm()); 266*0b57cec5SDimitry Andric 267*0b57cec5SDimitry Andric llvm_unreachable("Unexpected 4-operand DBG_VALUE instruction!"); 268*0b57cec5SDimitry Andric } 269*0b57cec5SDimitry Andric 270*0b57cec5SDimitry Andric void DbgVariable::initializeDbgValue(const MachineInstr *DbgValue) { 271*0b57cec5SDimitry Andric assert(FrameIndexExprs.empty() && "Already initialized?"); 272*0b57cec5SDimitry Andric assert(!ValueLoc.get() && "Already initialized?"); 273*0b57cec5SDimitry Andric 274*0b57cec5SDimitry Andric assert(getVariable() == DbgValue->getDebugVariable() && "Wrong variable"); 275*0b57cec5SDimitry Andric assert(getInlinedAt() == DbgValue->getDebugLoc()->getInlinedAt() && 276*0b57cec5SDimitry Andric "Wrong inlined-at"); 277*0b57cec5SDimitry Andric 278*0b57cec5SDimitry Andric ValueLoc = llvm::make_unique<DbgValueLoc>(getDebugLocValue(DbgValue)); 279*0b57cec5SDimitry Andric if (auto *E = DbgValue->getDebugExpression()) 280*0b57cec5SDimitry Andric if (E->getNumElements()) 281*0b57cec5SDimitry Andric FrameIndexExprs.push_back({0, E}); 282*0b57cec5SDimitry Andric } 283*0b57cec5SDimitry Andric 284*0b57cec5SDimitry Andric ArrayRef<DbgVariable::FrameIndexExpr> DbgVariable::getFrameIndexExprs() const { 285*0b57cec5SDimitry Andric if (FrameIndexExprs.size() == 1) 286*0b57cec5SDimitry Andric return FrameIndexExprs; 287*0b57cec5SDimitry Andric 288*0b57cec5SDimitry Andric assert(llvm::all_of(FrameIndexExprs, 289*0b57cec5SDimitry Andric [](const FrameIndexExpr &A) { 290*0b57cec5SDimitry Andric return A.Expr->isFragment(); 291*0b57cec5SDimitry Andric }) && 292*0b57cec5SDimitry Andric "multiple FI expressions without DW_OP_LLVM_fragment"); 293*0b57cec5SDimitry Andric llvm::sort(FrameIndexExprs, 294*0b57cec5SDimitry Andric [](const FrameIndexExpr &A, const FrameIndexExpr &B) -> bool { 295*0b57cec5SDimitry Andric return A.Expr->getFragmentInfo()->OffsetInBits < 296*0b57cec5SDimitry Andric B.Expr->getFragmentInfo()->OffsetInBits; 297*0b57cec5SDimitry Andric }); 298*0b57cec5SDimitry Andric 299*0b57cec5SDimitry Andric return FrameIndexExprs; 300*0b57cec5SDimitry Andric } 301*0b57cec5SDimitry Andric 302*0b57cec5SDimitry Andric void DbgVariable::addMMIEntry(const DbgVariable &V) { 303*0b57cec5SDimitry Andric assert(DebugLocListIndex == ~0U && !ValueLoc.get() && "not an MMI entry"); 304*0b57cec5SDimitry Andric assert(V.DebugLocListIndex == ~0U && !V.ValueLoc.get() && "not an MMI entry"); 305*0b57cec5SDimitry Andric assert(V.getVariable() == getVariable() && "conflicting variable"); 306*0b57cec5SDimitry Andric assert(V.getInlinedAt() == getInlinedAt() && "conflicting inlined-at location"); 307*0b57cec5SDimitry Andric 308*0b57cec5SDimitry Andric assert(!FrameIndexExprs.empty() && "Expected an MMI entry"); 309*0b57cec5SDimitry Andric assert(!V.FrameIndexExprs.empty() && "Expected an MMI entry"); 310*0b57cec5SDimitry Andric 311*0b57cec5SDimitry Andric // FIXME: This logic should not be necessary anymore, as we now have proper 312*0b57cec5SDimitry Andric // deduplication. However, without it, we currently run into the assertion 313*0b57cec5SDimitry Andric // below, which means that we are likely dealing with broken input, i.e. two 314*0b57cec5SDimitry Andric // non-fragment entries for the same variable at different frame indices. 315*0b57cec5SDimitry Andric if (FrameIndexExprs.size()) { 316*0b57cec5SDimitry Andric auto *Expr = FrameIndexExprs.back().Expr; 317*0b57cec5SDimitry Andric if (!Expr || !Expr->isFragment()) 318*0b57cec5SDimitry Andric return; 319*0b57cec5SDimitry Andric } 320*0b57cec5SDimitry Andric 321*0b57cec5SDimitry Andric for (const auto &FIE : V.FrameIndexExprs) 322*0b57cec5SDimitry Andric // Ignore duplicate entries. 323*0b57cec5SDimitry Andric if (llvm::none_of(FrameIndexExprs, [&](const FrameIndexExpr &Other) { 324*0b57cec5SDimitry Andric return FIE.FI == Other.FI && FIE.Expr == Other.Expr; 325*0b57cec5SDimitry Andric })) 326*0b57cec5SDimitry Andric FrameIndexExprs.push_back(FIE); 327*0b57cec5SDimitry Andric 328*0b57cec5SDimitry Andric assert((FrameIndexExprs.size() == 1 || 329*0b57cec5SDimitry Andric llvm::all_of(FrameIndexExprs, 330*0b57cec5SDimitry Andric [](FrameIndexExpr &FIE) { 331*0b57cec5SDimitry Andric return FIE.Expr && FIE.Expr->isFragment(); 332*0b57cec5SDimitry Andric })) && 333*0b57cec5SDimitry Andric "conflicting locations for variable"); 334*0b57cec5SDimitry Andric } 335*0b57cec5SDimitry Andric 336*0b57cec5SDimitry Andric static AccelTableKind computeAccelTableKind(unsigned DwarfVersion, 337*0b57cec5SDimitry Andric bool GenerateTypeUnits, 338*0b57cec5SDimitry Andric DebuggerKind Tuning, 339*0b57cec5SDimitry Andric const Triple &TT) { 340*0b57cec5SDimitry Andric // Honor an explicit request. 341*0b57cec5SDimitry Andric if (AccelTables != AccelTableKind::Default) 342*0b57cec5SDimitry Andric return AccelTables; 343*0b57cec5SDimitry Andric 344*0b57cec5SDimitry Andric // Accelerator tables with type units are currently not supported. 345*0b57cec5SDimitry Andric if (GenerateTypeUnits) 346*0b57cec5SDimitry Andric return AccelTableKind::None; 347*0b57cec5SDimitry Andric 348*0b57cec5SDimitry Andric // Accelerator tables get emitted if targetting DWARF v5 or LLDB. DWARF v5 349*0b57cec5SDimitry Andric // always implies debug_names. For lower standard versions we use apple 350*0b57cec5SDimitry Andric // accelerator tables on apple platforms and debug_names elsewhere. 351*0b57cec5SDimitry Andric if (DwarfVersion >= 5) 352*0b57cec5SDimitry Andric return AccelTableKind::Dwarf; 353*0b57cec5SDimitry Andric if (Tuning == DebuggerKind::LLDB) 354*0b57cec5SDimitry Andric return TT.isOSBinFormatMachO() ? AccelTableKind::Apple 355*0b57cec5SDimitry Andric : AccelTableKind::Dwarf; 356*0b57cec5SDimitry Andric return AccelTableKind::None; 357*0b57cec5SDimitry Andric } 358*0b57cec5SDimitry Andric 359*0b57cec5SDimitry Andric DwarfDebug::DwarfDebug(AsmPrinter *A, Module *M) 360*0b57cec5SDimitry Andric : DebugHandlerBase(A), DebugLocs(A->OutStreamer->isVerboseAsm()), 361*0b57cec5SDimitry Andric InfoHolder(A, "info_string", DIEValueAllocator), 362*0b57cec5SDimitry Andric SkeletonHolder(A, "skel_string", DIEValueAllocator), 363*0b57cec5SDimitry Andric IsDarwin(A->TM.getTargetTriple().isOSDarwin()) { 364*0b57cec5SDimitry Andric const Triple &TT = Asm->TM.getTargetTriple(); 365*0b57cec5SDimitry Andric 366*0b57cec5SDimitry Andric // Make sure we know our "debugger tuning". The target option takes 367*0b57cec5SDimitry Andric // precedence; fall back to triple-based defaults. 368*0b57cec5SDimitry Andric if (Asm->TM.Options.DebuggerTuning != DebuggerKind::Default) 369*0b57cec5SDimitry Andric DebuggerTuning = Asm->TM.Options.DebuggerTuning; 370*0b57cec5SDimitry Andric else if (IsDarwin) 371*0b57cec5SDimitry Andric DebuggerTuning = DebuggerKind::LLDB; 372*0b57cec5SDimitry Andric else if (TT.isPS4CPU()) 373*0b57cec5SDimitry Andric DebuggerTuning = DebuggerKind::SCE; 374*0b57cec5SDimitry Andric else 375*0b57cec5SDimitry Andric DebuggerTuning = DebuggerKind::GDB; 376*0b57cec5SDimitry Andric 377*0b57cec5SDimitry Andric if (DwarfInlinedStrings == Default) 378*0b57cec5SDimitry Andric UseInlineStrings = TT.isNVPTX(); 379*0b57cec5SDimitry Andric else 380*0b57cec5SDimitry Andric UseInlineStrings = DwarfInlinedStrings == Enable; 381*0b57cec5SDimitry Andric 382*0b57cec5SDimitry Andric UseLocSection = !TT.isNVPTX(); 383*0b57cec5SDimitry Andric 384*0b57cec5SDimitry Andric HasAppleExtensionAttributes = tuneForLLDB(); 385*0b57cec5SDimitry Andric 386*0b57cec5SDimitry Andric // Handle split DWARF. 387*0b57cec5SDimitry Andric HasSplitDwarf = !Asm->TM.Options.MCOptions.SplitDwarfFile.empty(); 388*0b57cec5SDimitry Andric 389*0b57cec5SDimitry Andric // SCE defaults to linkage names only for abstract subprograms. 390*0b57cec5SDimitry Andric if (DwarfLinkageNames == DefaultLinkageNames) 391*0b57cec5SDimitry Andric UseAllLinkageNames = !tuneForSCE(); 392*0b57cec5SDimitry Andric else 393*0b57cec5SDimitry Andric UseAllLinkageNames = DwarfLinkageNames == AllLinkageNames; 394*0b57cec5SDimitry Andric 395*0b57cec5SDimitry Andric unsigned DwarfVersionNumber = Asm->TM.Options.MCOptions.DwarfVersion; 396*0b57cec5SDimitry Andric unsigned DwarfVersion = DwarfVersionNumber ? DwarfVersionNumber 397*0b57cec5SDimitry Andric : MMI->getModule()->getDwarfVersion(); 398*0b57cec5SDimitry Andric // Use dwarf 4 by default if nothing is requested. For NVPTX, use dwarf 2. 399*0b57cec5SDimitry Andric DwarfVersion = 400*0b57cec5SDimitry Andric TT.isNVPTX() ? 2 : (DwarfVersion ? DwarfVersion : dwarf::DWARF_VERSION); 401*0b57cec5SDimitry Andric 402*0b57cec5SDimitry Andric UseRangesSection = !NoDwarfRangesSection && !TT.isNVPTX(); 403*0b57cec5SDimitry Andric 404*0b57cec5SDimitry Andric // Use sections as references. Force for NVPTX. 405*0b57cec5SDimitry Andric if (DwarfSectionsAsReferences == Default) 406*0b57cec5SDimitry Andric UseSectionsAsReferences = TT.isNVPTX(); 407*0b57cec5SDimitry Andric else 408*0b57cec5SDimitry Andric UseSectionsAsReferences = DwarfSectionsAsReferences == Enable; 409*0b57cec5SDimitry Andric 410*0b57cec5SDimitry Andric // Don't generate type units for unsupported object file formats. 411*0b57cec5SDimitry Andric GenerateTypeUnits = 412*0b57cec5SDimitry Andric A->TM.getTargetTriple().isOSBinFormatELF() && GenerateDwarfTypeUnits; 413*0b57cec5SDimitry Andric 414*0b57cec5SDimitry Andric TheAccelTableKind = computeAccelTableKind( 415*0b57cec5SDimitry Andric DwarfVersion, GenerateTypeUnits, DebuggerTuning, A->TM.getTargetTriple()); 416*0b57cec5SDimitry Andric 417*0b57cec5SDimitry Andric // Work around a GDB bug. GDB doesn't support the standard opcode; 418*0b57cec5SDimitry Andric // SCE doesn't support GNU's; LLDB prefers the standard opcode, which 419*0b57cec5SDimitry Andric // is defined as of DWARF 3. 420*0b57cec5SDimitry Andric // See GDB bug 11616 - DW_OP_form_tls_address is unimplemented 421*0b57cec5SDimitry Andric // https://sourceware.org/bugzilla/show_bug.cgi?id=11616 422*0b57cec5SDimitry Andric UseGNUTLSOpcode = tuneForGDB() || DwarfVersion < 3; 423*0b57cec5SDimitry Andric 424*0b57cec5SDimitry Andric // GDB does not fully support the DWARF 4 representation for bitfields. 425*0b57cec5SDimitry Andric UseDWARF2Bitfields = (DwarfVersion < 4) || tuneForGDB(); 426*0b57cec5SDimitry Andric 427*0b57cec5SDimitry Andric // The DWARF v5 string offsets table has - possibly shared - contributions 428*0b57cec5SDimitry Andric // from each compile and type unit each preceded by a header. The string 429*0b57cec5SDimitry Andric // offsets table used by the pre-DWARF v5 split-DWARF implementation uses 430*0b57cec5SDimitry Andric // a monolithic string offsets table without any header. 431*0b57cec5SDimitry Andric UseSegmentedStringOffsetsTable = DwarfVersion >= 5; 432*0b57cec5SDimitry Andric 433*0b57cec5SDimitry Andric Asm->OutStreamer->getContext().setDwarfVersion(DwarfVersion); 434*0b57cec5SDimitry Andric } 435*0b57cec5SDimitry Andric 436*0b57cec5SDimitry Andric // Define out of line so we don't have to include DwarfUnit.h in DwarfDebug.h. 437*0b57cec5SDimitry Andric DwarfDebug::~DwarfDebug() = default; 438*0b57cec5SDimitry Andric 439*0b57cec5SDimitry Andric static bool isObjCClass(StringRef Name) { 440*0b57cec5SDimitry Andric return Name.startswith("+") || Name.startswith("-"); 441*0b57cec5SDimitry Andric } 442*0b57cec5SDimitry Andric 443*0b57cec5SDimitry Andric static bool hasObjCCategory(StringRef Name) { 444*0b57cec5SDimitry Andric if (!isObjCClass(Name)) 445*0b57cec5SDimitry Andric return false; 446*0b57cec5SDimitry Andric 447*0b57cec5SDimitry Andric return Name.find(") ") != StringRef::npos; 448*0b57cec5SDimitry Andric } 449*0b57cec5SDimitry Andric 450*0b57cec5SDimitry Andric static void getObjCClassCategory(StringRef In, StringRef &Class, 451*0b57cec5SDimitry Andric StringRef &Category) { 452*0b57cec5SDimitry Andric if (!hasObjCCategory(In)) { 453*0b57cec5SDimitry Andric Class = In.slice(In.find('[') + 1, In.find(' ')); 454*0b57cec5SDimitry Andric Category = ""; 455*0b57cec5SDimitry Andric return; 456*0b57cec5SDimitry Andric } 457*0b57cec5SDimitry Andric 458*0b57cec5SDimitry Andric Class = In.slice(In.find('[') + 1, In.find('(')); 459*0b57cec5SDimitry Andric Category = In.slice(In.find('[') + 1, In.find(' ')); 460*0b57cec5SDimitry Andric } 461*0b57cec5SDimitry Andric 462*0b57cec5SDimitry Andric static StringRef getObjCMethodName(StringRef In) { 463*0b57cec5SDimitry Andric return In.slice(In.find(' ') + 1, In.find(']')); 464*0b57cec5SDimitry Andric } 465*0b57cec5SDimitry Andric 466*0b57cec5SDimitry Andric // Add the various names to the Dwarf accelerator table names. 467*0b57cec5SDimitry Andric void DwarfDebug::addSubprogramNames(const DICompileUnit &CU, 468*0b57cec5SDimitry Andric const DISubprogram *SP, DIE &Die) { 469*0b57cec5SDimitry Andric if (getAccelTableKind() != AccelTableKind::Apple && 470*0b57cec5SDimitry Andric CU.getNameTableKind() == DICompileUnit::DebugNameTableKind::None) 471*0b57cec5SDimitry Andric return; 472*0b57cec5SDimitry Andric 473*0b57cec5SDimitry Andric if (!SP->isDefinition()) 474*0b57cec5SDimitry Andric return; 475*0b57cec5SDimitry Andric 476*0b57cec5SDimitry Andric if (SP->getName() != "") 477*0b57cec5SDimitry Andric addAccelName(CU, SP->getName(), Die); 478*0b57cec5SDimitry Andric 479*0b57cec5SDimitry Andric // If the linkage name is different than the name, go ahead and output that as 480*0b57cec5SDimitry Andric // well into the name table. Only do that if we are going to actually emit 481*0b57cec5SDimitry Andric // that name. 482*0b57cec5SDimitry Andric if (SP->getLinkageName() != "" && SP->getName() != SP->getLinkageName() && 483*0b57cec5SDimitry Andric (useAllLinkageNames() || InfoHolder.getAbstractSPDies().lookup(SP))) 484*0b57cec5SDimitry Andric addAccelName(CU, SP->getLinkageName(), Die); 485*0b57cec5SDimitry Andric 486*0b57cec5SDimitry Andric // If this is an Objective-C selector name add it to the ObjC accelerator 487*0b57cec5SDimitry Andric // too. 488*0b57cec5SDimitry Andric if (isObjCClass(SP->getName())) { 489*0b57cec5SDimitry Andric StringRef Class, Category; 490*0b57cec5SDimitry Andric getObjCClassCategory(SP->getName(), Class, Category); 491*0b57cec5SDimitry Andric addAccelObjC(CU, Class, Die); 492*0b57cec5SDimitry Andric if (Category != "") 493*0b57cec5SDimitry Andric addAccelObjC(CU, Category, Die); 494*0b57cec5SDimitry Andric // Also add the base method name to the name table. 495*0b57cec5SDimitry Andric addAccelName(CU, getObjCMethodName(SP->getName()), Die); 496*0b57cec5SDimitry Andric } 497*0b57cec5SDimitry Andric } 498*0b57cec5SDimitry Andric 499*0b57cec5SDimitry Andric /// Check whether we should create a DIE for the given Scope, return true 500*0b57cec5SDimitry Andric /// if we don't create a DIE (the corresponding DIE is null). 501*0b57cec5SDimitry Andric bool DwarfDebug::isLexicalScopeDIENull(LexicalScope *Scope) { 502*0b57cec5SDimitry Andric if (Scope->isAbstractScope()) 503*0b57cec5SDimitry Andric return false; 504*0b57cec5SDimitry Andric 505*0b57cec5SDimitry Andric // We don't create a DIE if there is no Range. 506*0b57cec5SDimitry Andric const SmallVectorImpl<InsnRange> &Ranges = Scope->getRanges(); 507*0b57cec5SDimitry Andric if (Ranges.empty()) 508*0b57cec5SDimitry Andric return true; 509*0b57cec5SDimitry Andric 510*0b57cec5SDimitry Andric if (Ranges.size() > 1) 511*0b57cec5SDimitry Andric return false; 512*0b57cec5SDimitry Andric 513*0b57cec5SDimitry Andric // We don't create a DIE if we have a single Range and the end label 514*0b57cec5SDimitry Andric // is null. 515*0b57cec5SDimitry Andric return !getLabelAfterInsn(Ranges.front().second); 516*0b57cec5SDimitry Andric } 517*0b57cec5SDimitry Andric 518*0b57cec5SDimitry Andric template <typename Func> static void forBothCUs(DwarfCompileUnit &CU, Func F) { 519*0b57cec5SDimitry Andric F(CU); 520*0b57cec5SDimitry Andric if (auto *SkelCU = CU.getSkeleton()) 521*0b57cec5SDimitry Andric if (CU.getCUNode()->getSplitDebugInlining()) 522*0b57cec5SDimitry Andric F(*SkelCU); 523*0b57cec5SDimitry Andric } 524*0b57cec5SDimitry Andric 525*0b57cec5SDimitry Andric bool DwarfDebug::shareAcrossDWOCUs() const { 526*0b57cec5SDimitry Andric return SplitDwarfCrossCuReferences; 527*0b57cec5SDimitry Andric } 528*0b57cec5SDimitry Andric 529*0b57cec5SDimitry Andric void DwarfDebug::constructAbstractSubprogramScopeDIE(DwarfCompileUnit &SrcCU, 530*0b57cec5SDimitry Andric LexicalScope *Scope) { 531*0b57cec5SDimitry Andric assert(Scope && Scope->getScopeNode()); 532*0b57cec5SDimitry Andric assert(Scope->isAbstractScope()); 533*0b57cec5SDimitry Andric assert(!Scope->getInlinedAt()); 534*0b57cec5SDimitry Andric 535*0b57cec5SDimitry Andric auto *SP = cast<DISubprogram>(Scope->getScopeNode()); 536*0b57cec5SDimitry Andric 537*0b57cec5SDimitry Andric // Find the subprogram's DwarfCompileUnit in the SPMap in case the subprogram 538*0b57cec5SDimitry Andric // was inlined from another compile unit. 539*0b57cec5SDimitry Andric if (useSplitDwarf() && !shareAcrossDWOCUs() && !SP->getUnit()->getSplitDebugInlining()) 540*0b57cec5SDimitry Andric // Avoid building the original CU if it won't be used 541*0b57cec5SDimitry Andric SrcCU.constructAbstractSubprogramScopeDIE(Scope); 542*0b57cec5SDimitry Andric else { 543*0b57cec5SDimitry Andric auto &CU = getOrCreateDwarfCompileUnit(SP->getUnit()); 544*0b57cec5SDimitry Andric if (auto *SkelCU = CU.getSkeleton()) { 545*0b57cec5SDimitry Andric (shareAcrossDWOCUs() ? CU : SrcCU) 546*0b57cec5SDimitry Andric .constructAbstractSubprogramScopeDIE(Scope); 547*0b57cec5SDimitry Andric if (CU.getCUNode()->getSplitDebugInlining()) 548*0b57cec5SDimitry Andric SkelCU->constructAbstractSubprogramScopeDIE(Scope); 549*0b57cec5SDimitry Andric } else 550*0b57cec5SDimitry Andric CU.constructAbstractSubprogramScopeDIE(Scope); 551*0b57cec5SDimitry Andric } 552*0b57cec5SDimitry Andric } 553*0b57cec5SDimitry Andric 554*0b57cec5SDimitry Andric void DwarfDebug::constructCallSiteEntryDIEs(const DISubprogram &SP, 555*0b57cec5SDimitry Andric DwarfCompileUnit &CU, DIE &ScopeDIE, 556*0b57cec5SDimitry Andric const MachineFunction &MF) { 557*0b57cec5SDimitry Andric // Add a call site-related attribute (DWARF5, Sec. 3.3.1.3). Do this only if 558*0b57cec5SDimitry Andric // the subprogram is required to have one. 559*0b57cec5SDimitry Andric if (!SP.areAllCallsDescribed() || !SP.isDefinition()) 560*0b57cec5SDimitry Andric return; 561*0b57cec5SDimitry Andric 562*0b57cec5SDimitry Andric // Use DW_AT_call_all_calls to express that call site entries are present 563*0b57cec5SDimitry Andric // for both tail and non-tail calls. Don't use DW_AT_call_all_source_calls 564*0b57cec5SDimitry Andric // because one of its requirements is not met: call site entries for 565*0b57cec5SDimitry Andric // optimized-out calls are elided. 566*0b57cec5SDimitry Andric CU.addFlag(ScopeDIE, dwarf::DW_AT_call_all_calls); 567*0b57cec5SDimitry Andric 568*0b57cec5SDimitry Andric const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo(); 569*0b57cec5SDimitry Andric assert(TII && "TargetInstrInfo not found: cannot label tail calls"); 570*0b57cec5SDimitry Andric 571*0b57cec5SDimitry Andric // Emit call site entries for each call or tail call in the function. 572*0b57cec5SDimitry Andric for (const MachineBasicBlock &MBB : MF) { 573*0b57cec5SDimitry Andric for (const MachineInstr &MI : MBB.instrs()) { 574*0b57cec5SDimitry Andric // Skip instructions which aren't calls. Both calls and tail-calling jump 575*0b57cec5SDimitry Andric // instructions (e.g TAILJMPd64) are classified correctly here. 576*0b57cec5SDimitry Andric if (!MI.isCall()) 577*0b57cec5SDimitry Andric continue; 578*0b57cec5SDimitry Andric 579*0b57cec5SDimitry Andric // TODO: Add support for targets with delay slots (see: beginInstruction). 580*0b57cec5SDimitry Andric if (MI.hasDelaySlot()) 581*0b57cec5SDimitry Andric return; 582*0b57cec5SDimitry Andric 583*0b57cec5SDimitry Andric // If this is a direct call, find the callee's subprogram. 584*0b57cec5SDimitry Andric const MachineOperand &CalleeOp = MI.getOperand(0); 585*0b57cec5SDimitry Andric if (!CalleeOp.isGlobal()) 586*0b57cec5SDimitry Andric continue; 587*0b57cec5SDimitry Andric const Function *CalleeDecl = dyn_cast<Function>(CalleeOp.getGlobal()); 588*0b57cec5SDimitry Andric if (!CalleeDecl || !CalleeDecl->getSubprogram()) 589*0b57cec5SDimitry Andric continue; 590*0b57cec5SDimitry Andric 591*0b57cec5SDimitry Andric // TODO: Omit call site entries for runtime calls (objc_msgSend, etc). 592*0b57cec5SDimitry Andric // TODO: Add support for indirect calls. 593*0b57cec5SDimitry Andric 594*0b57cec5SDimitry Andric bool IsTail = TII->isTailCall(MI); 595*0b57cec5SDimitry Andric 596*0b57cec5SDimitry Andric // For tail calls, no return PC information is needed. For regular calls, 597*0b57cec5SDimitry Andric // the return PC is needed to disambiguate paths in the call graph which 598*0b57cec5SDimitry Andric // could lead to some target function. 599*0b57cec5SDimitry Andric const MCExpr *PCOffset = 600*0b57cec5SDimitry Andric IsTail ? nullptr : getFunctionLocalOffsetAfterInsn(&MI); 601*0b57cec5SDimitry Andric 602*0b57cec5SDimitry Andric assert((IsTail || PCOffset) && "Call without return PC information"); 603*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "CallSiteEntry: " << MF.getName() << " -> " 604*0b57cec5SDimitry Andric << CalleeDecl->getName() << (IsTail ? " [tail]" : "") 605*0b57cec5SDimitry Andric << "\n"); 606*0b57cec5SDimitry Andric CU.constructCallSiteEntryDIE(ScopeDIE, *CalleeDecl->getSubprogram(), 607*0b57cec5SDimitry Andric IsTail, PCOffset); 608*0b57cec5SDimitry Andric } 609*0b57cec5SDimitry Andric } 610*0b57cec5SDimitry Andric } 611*0b57cec5SDimitry Andric 612*0b57cec5SDimitry Andric void DwarfDebug::addGnuPubAttributes(DwarfCompileUnit &U, DIE &D) const { 613*0b57cec5SDimitry Andric if (!U.hasDwarfPubSections()) 614*0b57cec5SDimitry Andric return; 615*0b57cec5SDimitry Andric 616*0b57cec5SDimitry Andric U.addFlag(D, dwarf::DW_AT_GNU_pubnames); 617*0b57cec5SDimitry Andric } 618*0b57cec5SDimitry Andric 619*0b57cec5SDimitry Andric void DwarfDebug::finishUnitAttributes(const DICompileUnit *DIUnit, 620*0b57cec5SDimitry Andric DwarfCompileUnit &NewCU) { 621*0b57cec5SDimitry Andric DIE &Die = NewCU.getUnitDie(); 622*0b57cec5SDimitry Andric StringRef FN = DIUnit->getFilename(); 623*0b57cec5SDimitry Andric 624*0b57cec5SDimitry Andric StringRef Producer = DIUnit->getProducer(); 625*0b57cec5SDimitry Andric StringRef Flags = DIUnit->getFlags(); 626*0b57cec5SDimitry Andric if (!Flags.empty() && !useAppleExtensionAttributes()) { 627*0b57cec5SDimitry Andric std::string ProducerWithFlags = Producer.str() + " " + Flags.str(); 628*0b57cec5SDimitry Andric NewCU.addString(Die, dwarf::DW_AT_producer, ProducerWithFlags); 629*0b57cec5SDimitry Andric } else 630*0b57cec5SDimitry Andric NewCU.addString(Die, dwarf::DW_AT_producer, Producer); 631*0b57cec5SDimitry Andric 632*0b57cec5SDimitry Andric NewCU.addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data2, 633*0b57cec5SDimitry Andric DIUnit->getSourceLanguage()); 634*0b57cec5SDimitry Andric NewCU.addString(Die, dwarf::DW_AT_name, FN); 635*0b57cec5SDimitry Andric 636*0b57cec5SDimitry Andric // Add DW_str_offsets_base to the unit DIE, except for split units. 637*0b57cec5SDimitry Andric if (useSegmentedStringOffsetsTable() && !useSplitDwarf()) 638*0b57cec5SDimitry Andric NewCU.addStringOffsetsStart(); 639*0b57cec5SDimitry Andric 640*0b57cec5SDimitry Andric if (!useSplitDwarf()) { 641*0b57cec5SDimitry Andric NewCU.initStmtList(); 642*0b57cec5SDimitry Andric 643*0b57cec5SDimitry Andric // If we're using split dwarf the compilation dir is going to be in the 644*0b57cec5SDimitry Andric // skeleton CU and so we don't need to duplicate it here. 645*0b57cec5SDimitry Andric if (!CompilationDir.empty()) 646*0b57cec5SDimitry Andric NewCU.addString(Die, dwarf::DW_AT_comp_dir, CompilationDir); 647*0b57cec5SDimitry Andric 648*0b57cec5SDimitry Andric addGnuPubAttributes(NewCU, Die); 649*0b57cec5SDimitry Andric } 650*0b57cec5SDimitry Andric 651*0b57cec5SDimitry Andric if (useAppleExtensionAttributes()) { 652*0b57cec5SDimitry Andric if (DIUnit->isOptimized()) 653*0b57cec5SDimitry Andric NewCU.addFlag(Die, dwarf::DW_AT_APPLE_optimized); 654*0b57cec5SDimitry Andric 655*0b57cec5SDimitry Andric StringRef Flags = DIUnit->getFlags(); 656*0b57cec5SDimitry Andric if (!Flags.empty()) 657*0b57cec5SDimitry Andric NewCU.addString(Die, dwarf::DW_AT_APPLE_flags, Flags); 658*0b57cec5SDimitry Andric 659*0b57cec5SDimitry Andric if (unsigned RVer = DIUnit->getRuntimeVersion()) 660*0b57cec5SDimitry Andric NewCU.addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers, 661*0b57cec5SDimitry Andric dwarf::DW_FORM_data1, RVer); 662*0b57cec5SDimitry Andric } 663*0b57cec5SDimitry Andric 664*0b57cec5SDimitry Andric if (DIUnit->getDWOId()) { 665*0b57cec5SDimitry Andric // This CU is either a clang module DWO or a skeleton CU. 666*0b57cec5SDimitry Andric NewCU.addUInt(Die, dwarf::DW_AT_GNU_dwo_id, dwarf::DW_FORM_data8, 667*0b57cec5SDimitry Andric DIUnit->getDWOId()); 668*0b57cec5SDimitry Andric if (!DIUnit->getSplitDebugFilename().empty()) 669*0b57cec5SDimitry Andric // This is a prefabricated skeleton CU. 670*0b57cec5SDimitry Andric NewCU.addString(Die, dwarf::DW_AT_GNU_dwo_name, 671*0b57cec5SDimitry Andric DIUnit->getSplitDebugFilename()); 672*0b57cec5SDimitry Andric } 673*0b57cec5SDimitry Andric } 674*0b57cec5SDimitry Andric // Create new DwarfCompileUnit for the given metadata node with tag 675*0b57cec5SDimitry Andric // DW_TAG_compile_unit. 676*0b57cec5SDimitry Andric DwarfCompileUnit & 677*0b57cec5SDimitry Andric DwarfDebug::getOrCreateDwarfCompileUnit(const DICompileUnit *DIUnit) { 678*0b57cec5SDimitry Andric if (auto *CU = CUMap.lookup(DIUnit)) 679*0b57cec5SDimitry Andric return *CU; 680*0b57cec5SDimitry Andric 681*0b57cec5SDimitry Andric CompilationDir = DIUnit->getDirectory(); 682*0b57cec5SDimitry Andric 683*0b57cec5SDimitry Andric auto OwnedUnit = llvm::make_unique<DwarfCompileUnit>( 684*0b57cec5SDimitry Andric InfoHolder.getUnits().size(), DIUnit, Asm, this, &InfoHolder); 685*0b57cec5SDimitry Andric DwarfCompileUnit &NewCU = *OwnedUnit; 686*0b57cec5SDimitry Andric InfoHolder.addUnit(std::move(OwnedUnit)); 687*0b57cec5SDimitry Andric 688*0b57cec5SDimitry Andric for (auto *IE : DIUnit->getImportedEntities()) 689*0b57cec5SDimitry Andric NewCU.addImportedEntity(IE); 690*0b57cec5SDimitry Andric 691*0b57cec5SDimitry Andric // LTO with assembly output shares a single line table amongst multiple CUs. 692*0b57cec5SDimitry Andric // To avoid the compilation directory being ambiguous, let the line table 693*0b57cec5SDimitry Andric // explicitly describe the directory of all files, never relying on the 694*0b57cec5SDimitry Andric // compilation directory. 695*0b57cec5SDimitry Andric if (!Asm->OutStreamer->hasRawTextSupport() || SingleCU) 696*0b57cec5SDimitry Andric Asm->OutStreamer->emitDwarfFile0Directive( 697*0b57cec5SDimitry Andric CompilationDir, DIUnit->getFilename(), 698*0b57cec5SDimitry Andric NewCU.getMD5AsBytes(DIUnit->getFile()), DIUnit->getSource(), 699*0b57cec5SDimitry Andric NewCU.getUniqueID()); 700*0b57cec5SDimitry Andric 701*0b57cec5SDimitry Andric if (useSplitDwarf()) { 702*0b57cec5SDimitry Andric NewCU.setSkeleton(constructSkeletonCU(NewCU)); 703*0b57cec5SDimitry Andric NewCU.setSection(Asm->getObjFileLowering().getDwarfInfoDWOSection()); 704*0b57cec5SDimitry Andric } else { 705*0b57cec5SDimitry Andric finishUnitAttributes(DIUnit, NewCU); 706*0b57cec5SDimitry Andric NewCU.setSection(Asm->getObjFileLowering().getDwarfInfoSection()); 707*0b57cec5SDimitry Andric } 708*0b57cec5SDimitry Andric 709*0b57cec5SDimitry Andric // Create DIEs for function declarations used for call site debug info. 710*0b57cec5SDimitry Andric for (auto Scope : DIUnit->getRetainedTypes()) 711*0b57cec5SDimitry Andric if (auto *SP = dyn_cast_or_null<DISubprogram>(Scope)) 712*0b57cec5SDimitry Andric NewCU.getOrCreateSubprogramDIE(SP); 713*0b57cec5SDimitry Andric 714*0b57cec5SDimitry Andric CUMap.insert({DIUnit, &NewCU}); 715*0b57cec5SDimitry Andric CUDieMap.insert({&NewCU.getUnitDie(), &NewCU}); 716*0b57cec5SDimitry Andric return NewCU; 717*0b57cec5SDimitry Andric } 718*0b57cec5SDimitry Andric 719*0b57cec5SDimitry Andric void DwarfDebug::constructAndAddImportedEntityDIE(DwarfCompileUnit &TheCU, 720*0b57cec5SDimitry Andric const DIImportedEntity *N) { 721*0b57cec5SDimitry Andric if (isa<DILocalScope>(N->getScope())) 722*0b57cec5SDimitry Andric return; 723*0b57cec5SDimitry Andric if (DIE *D = TheCU.getOrCreateContextDIE(N->getScope())) 724*0b57cec5SDimitry Andric D->addChild(TheCU.constructImportedEntityDIE(N)); 725*0b57cec5SDimitry Andric } 726*0b57cec5SDimitry Andric 727*0b57cec5SDimitry Andric /// Sort and unique GVEs by comparing their fragment offset. 728*0b57cec5SDimitry Andric static SmallVectorImpl<DwarfCompileUnit::GlobalExpr> & 729*0b57cec5SDimitry Andric sortGlobalExprs(SmallVectorImpl<DwarfCompileUnit::GlobalExpr> &GVEs) { 730*0b57cec5SDimitry Andric llvm::sort( 731*0b57cec5SDimitry Andric GVEs, [](DwarfCompileUnit::GlobalExpr A, DwarfCompileUnit::GlobalExpr B) { 732*0b57cec5SDimitry Andric // Sort order: first null exprs, then exprs without fragment 733*0b57cec5SDimitry Andric // info, then sort by fragment offset in bits. 734*0b57cec5SDimitry Andric // FIXME: Come up with a more comprehensive comparator so 735*0b57cec5SDimitry Andric // the sorting isn't non-deterministic, and so the following 736*0b57cec5SDimitry Andric // std::unique call works correctly. 737*0b57cec5SDimitry Andric if (!A.Expr || !B.Expr) 738*0b57cec5SDimitry Andric return !!B.Expr; 739*0b57cec5SDimitry Andric auto FragmentA = A.Expr->getFragmentInfo(); 740*0b57cec5SDimitry Andric auto FragmentB = B.Expr->getFragmentInfo(); 741*0b57cec5SDimitry Andric if (!FragmentA || !FragmentB) 742*0b57cec5SDimitry Andric return !!FragmentB; 743*0b57cec5SDimitry Andric return FragmentA->OffsetInBits < FragmentB->OffsetInBits; 744*0b57cec5SDimitry Andric }); 745*0b57cec5SDimitry Andric GVEs.erase(std::unique(GVEs.begin(), GVEs.end(), 746*0b57cec5SDimitry Andric [](DwarfCompileUnit::GlobalExpr A, 747*0b57cec5SDimitry Andric DwarfCompileUnit::GlobalExpr B) { 748*0b57cec5SDimitry Andric return A.Expr == B.Expr; 749*0b57cec5SDimitry Andric }), 750*0b57cec5SDimitry Andric GVEs.end()); 751*0b57cec5SDimitry Andric return GVEs; 752*0b57cec5SDimitry Andric } 753*0b57cec5SDimitry Andric 754*0b57cec5SDimitry Andric // Emit all Dwarf sections that should come prior to the content. Create 755*0b57cec5SDimitry Andric // global DIEs and emit initial debug info sections. This is invoked by 756*0b57cec5SDimitry Andric // the target AsmPrinter. 757*0b57cec5SDimitry Andric void DwarfDebug::beginModule() { 758*0b57cec5SDimitry Andric NamedRegionTimer T(DbgTimerName, DbgTimerDescription, DWARFGroupName, 759*0b57cec5SDimitry Andric DWARFGroupDescription, TimePassesIsEnabled); 760*0b57cec5SDimitry Andric if (DisableDebugInfoPrinting) { 761*0b57cec5SDimitry Andric MMI->setDebugInfoAvailability(false); 762*0b57cec5SDimitry Andric return; 763*0b57cec5SDimitry Andric } 764*0b57cec5SDimitry Andric 765*0b57cec5SDimitry Andric const Module *M = MMI->getModule(); 766*0b57cec5SDimitry Andric 767*0b57cec5SDimitry Andric unsigned NumDebugCUs = std::distance(M->debug_compile_units_begin(), 768*0b57cec5SDimitry Andric M->debug_compile_units_end()); 769*0b57cec5SDimitry Andric // Tell MMI whether we have debug info. 770*0b57cec5SDimitry Andric assert(MMI->hasDebugInfo() == (NumDebugCUs > 0) && 771*0b57cec5SDimitry Andric "DebugInfoAvailabilty initialized unexpectedly"); 772*0b57cec5SDimitry Andric SingleCU = NumDebugCUs == 1; 773*0b57cec5SDimitry Andric DenseMap<DIGlobalVariable *, SmallVector<DwarfCompileUnit::GlobalExpr, 1>> 774*0b57cec5SDimitry Andric GVMap; 775*0b57cec5SDimitry Andric for (const GlobalVariable &Global : M->globals()) { 776*0b57cec5SDimitry Andric SmallVector<DIGlobalVariableExpression *, 1> GVs; 777*0b57cec5SDimitry Andric Global.getDebugInfo(GVs); 778*0b57cec5SDimitry Andric for (auto *GVE : GVs) 779*0b57cec5SDimitry Andric GVMap[GVE->getVariable()].push_back({&Global, GVE->getExpression()}); 780*0b57cec5SDimitry Andric } 781*0b57cec5SDimitry Andric 782*0b57cec5SDimitry Andric // Create the symbol that designates the start of the unit's contribution 783*0b57cec5SDimitry Andric // to the string offsets table. In a split DWARF scenario, only the skeleton 784*0b57cec5SDimitry Andric // unit has the DW_AT_str_offsets_base attribute (and hence needs the symbol). 785*0b57cec5SDimitry Andric if (useSegmentedStringOffsetsTable()) 786*0b57cec5SDimitry Andric (useSplitDwarf() ? SkeletonHolder : InfoHolder) 787*0b57cec5SDimitry Andric .setStringOffsetsStartSym(Asm->createTempSymbol("str_offsets_base")); 788*0b57cec5SDimitry Andric 789*0b57cec5SDimitry Andric 790*0b57cec5SDimitry Andric // Create the symbols that designates the start of the DWARF v5 range list 791*0b57cec5SDimitry Andric // and locations list tables. They are located past the table headers. 792*0b57cec5SDimitry Andric if (getDwarfVersion() >= 5) { 793*0b57cec5SDimitry Andric DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder; 794*0b57cec5SDimitry Andric Holder.setRnglistsTableBaseSym( 795*0b57cec5SDimitry Andric Asm->createTempSymbol("rnglists_table_base")); 796*0b57cec5SDimitry Andric Holder.setLoclistsTableBaseSym( 797*0b57cec5SDimitry Andric Asm->createTempSymbol("loclists_table_base")); 798*0b57cec5SDimitry Andric 799*0b57cec5SDimitry Andric if (useSplitDwarf()) 800*0b57cec5SDimitry Andric InfoHolder.setRnglistsTableBaseSym( 801*0b57cec5SDimitry Andric Asm->createTempSymbol("rnglists_dwo_table_base")); 802*0b57cec5SDimitry Andric } 803*0b57cec5SDimitry Andric 804*0b57cec5SDimitry Andric // Create the symbol that points to the first entry following the debug 805*0b57cec5SDimitry Andric // address table (.debug_addr) header. 806*0b57cec5SDimitry Andric AddrPool.setLabel(Asm->createTempSymbol("addr_table_base")); 807*0b57cec5SDimitry Andric 808*0b57cec5SDimitry Andric for (DICompileUnit *CUNode : M->debug_compile_units()) { 809*0b57cec5SDimitry Andric // FIXME: Move local imported entities into a list attached to the 810*0b57cec5SDimitry Andric // subprogram, then this search won't be needed and a 811*0b57cec5SDimitry Andric // getImportedEntities().empty() test should go below with the rest. 812*0b57cec5SDimitry Andric bool HasNonLocalImportedEntities = llvm::any_of( 813*0b57cec5SDimitry Andric CUNode->getImportedEntities(), [](const DIImportedEntity *IE) { 814*0b57cec5SDimitry Andric return !isa<DILocalScope>(IE->getScope()); 815*0b57cec5SDimitry Andric }); 816*0b57cec5SDimitry Andric 817*0b57cec5SDimitry Andric if (!HasNonLocalImportedEntities && CUNode->getEnumTypes().empty() && 818*0b57cec5SDimitry Andric CUNode->getRetainedTypes().empty() && 819*0b57cec5SDimitry Andric CUNode->getGlobalVariables().empty() && CUNode->getMacros().empty()) 820*0b57cec5SDimitry Andric continue; 821*0b57cec5SDimitry Andric 822*0b57cec5SDimitry Andric DwarfCompileUnit &CU = getOrCreateDwarfCompileUnit(CUNode); 823*0b57cec5SDimitry Andric 824*0b57cec5SDimitry Andric // Global Variables. 825*0b57cec5SDimitry Andric for (auto *GVE : CUNode->getGlobalVariables()) { 826*0b57cec5SDimitry Andric // Don't bother adding DIGlobalVariableExpressions listed in the CU if we 827*0b57cec5SDimitry Andric // already know about the variable and it isn't adding a constant 828*0b57cec5SDimitry Andric // expression. 829*0b57cec5SDimitry Andric auto &GVMapEntry = GVMap[GVE->getVariable()]; 830*0b57cec5SDimitry Andric auto *Expr = GVE->getExpression(); 831*0b57cec5SDimitry Andric if (!GVMapEntry.size() || (Expr && Expr->isConstant())) 832*0b57cec5SDimitry Andric GVMapEntry.push_back({nullptr, Expr}); 833*0b57cec5SDimitry Andric } 834*0b57cec5SDimitry Andric DenseSet<DIGlobalVariable *> Processed; 835*0b57cec5SDimitry Andric for (auto *GVE : CUNode->getGlobalVariables()) { 836*0b57cec5SDimitry Andric DIGlobalVariable *GV = GVE->getVariable(); 837*0b57cec5SDimitry Andric if (Processed.insert(GV).second) 838*0b57cec5SDimitry Andric CU.getOrCreateGlobalVariableDIE(GV, sortGlobalExprs(GVMap[GV])); 839*0b57cec5SDimitry Andric } 840*0b57cec5SDimitry Andric 841*0b57cec5SDimitry Andric for (auto *Ty : CUNode->getEnumTypes()) { 842*0b57cec5SDimitry Andric // The enum types array by design contains pointers to 843*0b57cec5SDimitry Andric // MDNodes rather than DIRefs. Unique them here. 844*0b57cec5SDimitry Andric CU.getOrCreateTypeDIE(cast<DIType>(Ty)); 845*0b57cec5SDimitry Andric } 846*0b57cec5SDimitry Andric for (auto *Ty : CUNode->getRetainedTypes()) { 847*0b57cec5SDimitry Andric // The retained types array by design contains pointers to 848*0b57cec5SDimitry Andric // MDNodes rather than DIRefs. Unique them here. 849*0b57cec5SDimitry Andric if (DIType *RT = dyn_cast<DIType>(Ty)) 850*0b57cec5SDimitry Andric // There is no point in force-emitting a forward declaration. 851*0b57cec5SDimitry Andric CU.getOrCreateTypeDIE(RT); 852*0b57cec5SDimitry Andric } 853*0b57cec5SDimitry Andric // Emit imported_modules last so that the relevant context is already 854*0b57cec5SDimitry Andric // available. 855*0b57cec5SDimitry Andric for (auto *IE : CUNode->getImportedEntities()) 856*0b57cec5SDimitry Andric constructAndAddImportedEntityDIE(CU, IE); 857*0b57cec5SDimitry Andric } 858*0b57cec5SDimitry Andric } 859*0b57cec5SDimitry Andric 860*0b57cec5SDimitry Andric void DwarfDebug::finishEntityDefinitions() { 861*0b57cec5SDimitry Andric for (const auto &Entity : ConcreteEntities) { 862*0b57cec5SDimitry Andric DIE *Die = Entity->getDIE(); 863*0b57cec5SDimitry Andric assert(Die); 864*0b57cec5SDimitry Andric // FIXME: Consider the time-space tradeoff of just storing the unit pointer 865*0b57cec5SDimitry Andric // in the ConcreteEntities list, rather than looking it up again here. 866*0b57cec5SDimitry Andric // DIE::getUnit isn't simple - it walks parent pointers, etc. 867*0b57cec5SDimitry Andric DwarfCompileUnit *Unit = CUDieMap.lookup(Die->getUnitDie()); 868*0b57cec5SDimitry Andric assert(Unit); 869*0b57cec5SDimitry Andric Unit->finishEntityDefinition(Entity.get()); 870*0b57cec5SDimitry Andric } 871*0b57cec5SDimitry Andric } 872*0b57cec5SDimitry Andric 873*0b57cec5SDimitry Andric void DwarfDebug::finishSubprogramDefinitions() { 874*0b57cec5SDimitry Andric for (const DISubprogram *SP : ProcessedSPNodes) { 875*0b57cec5SDimitry Andric assert(SP->getUnit()->getEmissionKind() != DICompileUnit::NoDebug); 876*0b57cec5SDimitry Andric forBothCUs( 877*0b57cec5SDimitry Andric getOrCreateDwarfCompileUnit(SP->getUnit()), 878*0b57cec5SDimitry Andric [&](DwarfCompileUnit &CU) { CU.finishSubprogramDefinition(SP); }); 879*0b57cec5SDimitry Andric } 880*0b57cec5SDimitry Andric } 881*0b57cec5SDimitry Andric 882*0b57cec5SDimitry Andric void DwarfDebug::finalizeModuleInfo() { 883*0b57cec5SDimitry Andric const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering(); 884*0b57cec5SDimitry Andric 885*0b57cec5SDimitry Andric finishSubprogramDefinitions(); 886*0b57cec5SDimitry Andric 887*0b57cec5SDimitry Andric finishEntityDefinitions(); 888*0b57cec5SDimitry Andric 889*0b57cec5SDimitry Andric // Include the DWO file name in the hash if there's more than one CU. 890*0b57cec5SDimitry Andric // This handles ThinLTO's situation where imported CUs may very easily be 891*0b57cec5SDimitry Andric // duplicate with the same CU partially imported into another ThinLTO unit. 892*0b57cec5SDimitry Andric StringRef DWOName; 893*0b57cec5SDimitry Andric if (CUMap.size() > 1) 894*0b57cec5SDimitry Andric DWOName = Asm->TM.Options.MCOptions.SplitDwarfFile; 895*0b57cec5SDimitry Andric 896*0b57cec5SDimitry Andric // Handle anything that needs to be done on a per-unit basis after 897*0b57cec5SDimitry Andric // all other generation. 898*0b57cec5SDimitry Andric for (const auto &P : CUMap) { 899*0b57cec5SDimitry Andric auto &TheCU = *P.second; 900*0b57cec5SDimitry Andric if (TheCU.getCUNode()->isDebugDirectivesOnly()) 901*0b57cec5SDimitry Andric continue; 902*0b57cec5SDimitry Andric // Emit DW_AT_containing_type attribute to connect types with their 903*0b57cec5SDimitry Andric // vtable holding type. 904*0b57cec5SDimitry Andric TheCU.constructContainingTypeDIEs(); 905*0b57cec5SDimitry Andric 906*0b57cec5SDimitry Andric // Add CU specific attributes if we need to add any. 907*0b57cec5SDimitry Andric // If we're splitting the dwarf out now that we've got the entire 908*0b57cec5SDimitry Andric // CU then add the dwo id to it. 909*0b57cec5SDimitry Andric auto *SkCU = TheCU.getSkeleton(); 910*0b57cec5SDimitry Andric if (useSplitDwarf() && !empty(TheCU.getUnitDie().children())) { 911*0b57cec5SDimitry Andric finishUnitAttributes(TheCU.getCUNode(), TheCU); 912*0b57cec5SDimitry Andric TheCU.addString(TheCU.getUnitDie(), dwarf::DW_AT_GNU_dwo_name, 913*0b57cec5SDimitry Andric Asm->TM.Options.MCOptions.SplitDwarfFile); 914*0b57cec5SDimitry Andric SkCU->addString(SkCU->getUnitDie(), dwarf::DW_AT_GNU_dwo_name, 915*0b57cec5SDimitry Andric Asm->TM.Options.MCOptions.SplitDwarfFile); 916*0b57cec5SDimitry Andric // Emit a unique identifier for this CU. 917*0b57cec5SDimitry Andric uint64_t ID = 918*0b57cec5SDimitry Andric DIEHash(Asm).computeCUSignature(DWOName, TheCU.getUnitDie()); 919*0b57cec5SDimitry Andric if (getDwarfVersion() >= 5) { 920*0b57cec5SDimitry Andric TheCU.setDWOId(ID); 921*0b57cec5SDimitry Andric SkCU->setDWOId(ID); 922*0b57cec5SDimitry Andric } else { 923*0b57cec5SDimitry Andric TheCU.addUInt(TheCU.getUnitDie(), dwarf::DW_AT_GNU_dwo_id, 924*0b57cec5SDimitry Andric dwarf::DW_FORM_data8, ID); 925*0b57cec5SDimitry Andric SkCU->addUInt(SkCU->getUnitDie(), dwarf::DW_AT_GNU_dwo_id, 926*0b57cec5SDimitry Andric dwarf::DW_FORM_data8, ID); 927*0b57cec5SDimitry Andric } 928*0b57cec5SDimitry Andric 929*0b57cec5SDimitry Andric if (getDwarfVersion() < 5 && !SkeletonHolder.getRangeLists().empty()) { 930*0b57cec5SDimitry Andric const MCSymbol *Sym = TLOF.getDwarfRangesSection()->getBeginSymbol(); 931*0b57cec5SDimitry Andric SkCU->addSectionLabel(SkCU->getUnitDie(), dwarf::DW_AT_GNU_ranges_base, 932*0b57cec5SDimitry Andric Sym, Sym); 933*0b57cec5SDimitry Andric } 934*0b57cec5SDimitry Andric } else if (SkCU) { 935*0b57cec5SDimitry Andric finishUnitAttributes(SkCU->getCUNode(), *SkCU); 936*0b57cec5SDimitry Andric } 937*0b57cec5SDimitry Andric 938*0b57cec5SDimitry Andric // If we have code split among multiple sections or non-contiguous 939*0b57cec5SDimitry Andric // ranges of code then emit a DW_AT_ranges attribute on the unit that will 940*0b57cec5SDimitry Andric // remain in the .o file, otherwise add a DW_AT_low_pc. 941*0b57cec5SDimitry Andric // FIXME: We should use ranges allow reordering of code ala 942*0b57cec5SDimitry Andric // .subsections_via_symbols in mach-o. This would mean turning on 943*0b57cec5SDimitry Andric // ranges for all subprogram DIEs for mach-o. 944*0b57cec5SDimitry Andric DwarfCompileUnit &U = SkCU ? *SkCU : TheCU; 945*0b57cec5SDimitry Andric 946*0b57cec5SDimitry Andric if (unsigned NumRanges = TheCU.getRanges().size()) { 947*0b57cec5SDimitry Andric if (NumRanges > 1 && useRangesSection()) 948*0b57cec5SDimitry Andric // A DW_AT_low_pc attribute may also be specified in combination with 949*0b57cec5SDimitry Andric // DW_AT_ranges to specify the default base address for use in 950*0b57cec5SDimitry Andric // location lists (see Section 2.6.2) and range lists (see Section 951*0b57cec5SDimitry Andric // 2.17.3). 952*0b57cec5SDimitry Andric U.addUInt(U.getUnitDie(), dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, 0); 953*0b57cec5SDimitry Andric else 954*0b57cec5SDimitry Andric U.setBaseAddress(TheCU.getRanges().front().getStart()); 955*0b57cec5SDimitry Andric U.attachRangesOrLowHighPC(U.getUnitDie(), TheCU.takeRanges()); 956*0b57cec5SDimitry Andric } 957*0b57cec5SDimitry Andric 958*0b57cec5SDimitry Andric // We don't keep track of which addresses are used in which CU so this 959*0b57cec5SDimitry Andric // is a bit pessimistic under LTO. 960*0b57cec5SDimitry Andric if (!AddrPool.isEmpty() && 961*0b57cec5SDimitry Andric (getDwarfVersion() >= 5 || 962*0b57cec5SDimitry Andric (SkCU && !empty(TheCU.getUnitDie().children())))) 963*0b57cec5SDimitry Andric U.addAddrTableBase(); 964*0b57cec5SDimitry Andric 965*0b57cec5SDimitry Andric if (getDwarfVersion() >= 5) { 966*0b57cec5SDimitry Andric if (U.hasRangeLists()) 967*0b57cec5SDimitry Andric U.addRnglistsBase(); 968*0b57cec5SDimitry Andric 969*0b57cec5SDimitry Andric if (!DebugLocs.getLists().empty() && !useSplitDwarf()) 970*0b57cec5SDimitry Andric U.addLoclistsBase(); 971*0b57cec5SDimitry Andric } 972*0b57cec5SDimitry Andric 973*0b57cec5SDimitry Andric auto *CUNode = cast<DICompileUnit>(P.first); 974*0b57cec5SDimitry Andric // If compile Unit has macros, emit "DW_AT_macro_info" attribute. 975*0b57cec5SDimitry Andric if (CUNode->getMacros()) 976*0b57cec5SDimitry Andric U.addSectionLabel(U.getUnitDie(), dwarf::DW_AT_macro_info, 977*0b57cec5SDimitry Andric U.getMacroLabelBegin(), 978*0b57cec5SDimitry Andric TLOF.getDwarfMacinfoSection()->getBeginSymbol()); 979*0b57cec5SDimitry Andric } 980*0b57cec5SDimitry Andric 981*0b57cec5SDimitry Andric // Emit all frontend-produced Skeleton CUs, i.e., Clang modules. 982*0b57cec5SDimitry Andric for (auto *CUNode : MMI->getModule()->debug_compile_units()) 983*0b57cec5SDimitry Andric if (CUNode->getDWOId()) 984*0b57cec5SDimitry Andric getOrCreateDwarfCompileUnit(CUNode); 985*0b57cec5SDimitry Andric 986*0b57cec5SDimitry Andric // Compute DIE offsets and sizes. 987*0b57cec5SDimitry Andric InfoHolder.computeSizeAndOffsets(); 988*0b57cec5SDimitry Andric if (useSplitDwarf()) 989*0b57cec5SDimitry Andric SkeletonHolder.computeSizeAndOffsets(); 990*0b57cec5SDimitry Andric } 991*0b57cec5SDimitry Andric 992*0b57cec5SDimitry Andric // Emit all Dwarf sections that should come after the content. 993*0b57cec5SDimitry Andric void DwarfDebug::endModule() { 994*0b57cec5SDimitry Andric assert(CurFn == nullptr); 995*0b57cec5SDimitry Andric assert(CurMI == nullptr); 996*0b57cec5SDimitry Andric 997*0b57cec5SDimitry Andric for (const auto &P : CUMap) { 998*0b57cec5SDimitry Andric auto &CU = *P.second; 999*0b57cec5SDimitry Andric CU.createBaseTypeDIEs(); 1000*0b57cec5SDimitry Andric } 1001*0b57cec5SDimitry Andric 1002*0b57cec5SDimitry Andric // If we aren't actually generating debug info (check beginModule - 1003*0b57cec5SDimitry Andric // conditionalized on !DisableDebugInfoPrinting and the presence of the 1004*0b57cec5SDimitry Andric // llvm.dbg.cu metadata node) 1005*0b57cec5SDimitry Andric if (!MMI->hasDebugInfo()) 1006*0b57cec5SDimitry Andric return; 1007*0b57cec5SDimitry Andric 1008*0b57cec5SDimitry Andric // Finalize the debug info for the module. 1009*0b57cec5SDimitry Andric finalizeModuleInfo(); 1010*0b57cec5SDimitry Andric 1011*0b57cec5SDimitry Andric emitDebugStr(); 1012*0b57cec5SDimitry Andric 1013*0b57cec5SDimitry Andric if (useSplitDwarf()) 1014*0b57cec5SDimitry Andric emitDebugLocDWO(); 1015*0b57cec5SDimitry Andric else 1016*0b57cec5SDimitry Andric // Emit info into a debug loc section. 1017*0b57cec5SDimitry Andric emitDebugLoc(); 1018*0b57cec5SDimitry Andric 1019*0b57cec5SDimitry Andric // Corresponding abbreviations into a abbrev section. 1020*0b57cec5SDimitry Andric emitAbbreviations(); 1021*0b57cec5SDimitry Andric 1022*0b57cec5SDimitry Andric // Emit all the DIEs into a debug info section. 1023*0b57cec5SDimitry Andric emitDebugInfo(); 1024*0b57cec5SDimitry Andric 1025*0b57cec5SDimitry Andric // Emit info into a debug aranges section. 1026*0b57cec5SDimitry Andric if (GenerateARangeSection) 1027*0b57cec5SDimitry Andric emitDebugARanges(); 1028*0b57cec5SDimitry Andric 1029*0b57cec5SDimitry Andric // Emit info into a debug ranges section. 1030*0b57cec5SDimitry Andric emitDebugRanges(); 1031*0b57cec5SDimitry Andric 1032*0b57cec5SDimitry Andric // Emit info into a debug macinfo section. 1033*0b57cec5SDimitry Andric emitDebugMacinfo(); 1034*0b57cec5SDimitry Andric 1035*0b57cec5SDimitry Andric if (useSplitDwarf()) { 1036*0b57cec5SDimitry Andric emitDebugStrDWO(); 1037*0b57cec5SDimitry Andric emitDebugInfoDWO(); 1038*0b57cec5SDimitry Andric emitDebugAbbrevDWO(); 1039*0b57cec5SDimitry Andric emitDebugLineDWO(); 1040*0b57cec5SDimitry Andric emitDebugRangesDWO(); 1041*0b57cec5SDimitry Andric } 1042*0b57cec5SDimitry Andric 1043*0b57cec5SDimitry Andric emitDebugAddr(); 1044*0b57cec5SDimitry Andric 1045*0b57cec5SDimitry Andric // Emit info into the dwarf accelerator table sections. 1046*0b57cec5SDimitry Andric switch (getAccelTableKind()) { 1047*0b57cec5SDimitry Andric case AccelTableKind::Apple: 1048*0b57cec5SDimitry Andric emitAccelNames(); 1049*0b57cec5SDimitry Andric emitAccelObjC(); 1050*0b57cec5SDimitry Andric emitAccelNamespaces(); 1051*0b57cec5SDimitry Andric emitAccelTypes(); 1052*0b57cec5SDimitry Andric break; 1053*0b57cec5SDimitry Andric case AccelTableKind::Dwarf: 1054*0b57cec5SDimitry Andric emitAccelDebugNames(); 1055*0b57cec5SDimitry Andric break; 1056*0b57cec5SDimitry Andric case AccelTableKind::None: 1057*0b57cec5SDimitry Andric break; 1058*0b57cec5SDimitry Andric case AccelTableKind::Default: 1059*0b57cec5SDimitry Andric llvm_unreachable("Default should have already been resolved."); 1060*0b57cec5SDimitry Andric } 1061*0b57cec5SDimitry Andric 1062*0b57cec5SDimitry Andric // Emit the pubnames and pubtypes sections if requested. 1063*0b57cec5SDimitry Andric emitDebugPubSections(); 1064*0b57cec5SDimitry Andric 1065*0b57cec5SDimitry Andric // clean up. 1066*0b57cec5SDimitry Andric // FIXME: AbstractVariables.clear(); 1067*0b57cec5SDimitry Andric } 1068*0b57cec5SDimitry Andric 1069*0b57cec5SDimitry Andric void DwarfDebug::ensureAbstractEntityIsCreated(DwarfCompileUnit &CU, 1070*0b57cec5SDimitry Andric const DINode *Node, 1071*0b57cec5SDimitry Andric const MDNode *ScopeNode) { 1072*0b57cec5SDimitry Andric if (CU.getExistingAbstractEntity(Node)) 1073*0b57cec5SDimitry Andric return; 1074*0b57cec5SDimitry Andric 1075*0b57cec5SDimitry Andric CU.createAbstractEntity(Node, LScopes.getOrCreateAbstractScope( 1076*0b57cec5SDimitry Andric cast<DILocalScope>(ScopeNode))); 1077*0b57cec5SDimitry Andric } 1078*0b57cec5SDimitry Andric 1079*0b57cec5SDimitry Andric void DwarfDebug::ensureAbstractEntityIsCreatedIfScoped(DwarfCompileUnit &CU, 1080*0b57cec5SDimitry Andric const DINode *Node, const MDNode *ScopeNode) { 1081*0b57cec5SDimitry Andric if (CU.getExistingAbstractEntity(Node)) 1082*0b57cec5SDimitry Andric return; 1083*0b57cec5SDimitry Andric 1084*0b57cec5SDimitry Andric if (LexicalScope *Scope = 1085*0b57cec5SDimitry Andric LScopes.findAbstractScope(cast_or_null<DILocalScope>(ScopeNode))) 1086*0b57cec5SDimitry Andric CU.createAbstractEntity(Node, Scope); 1087*0b57cec5SDimitry Andric } 1088*0b57cec5SDimitry Andric 1089*0b57cec5SDimitry Andric // Collect variable information from side table maintained by MF. 1090*0b57cec5SDimitry Andric void DwarfDebug::collectVariableInfoFromMFTable( 1091*0b57cec5SDimitry Andric DwarfCompileUnit &TheCU, DenseSet<InlinedEntity> &Processed) { 1092*0b57cec5SDimitry Andric SmallDenseMap<InlinedEntity, DbgVariable *> MFVars; 1093*0b57cec5SDimitry Andric for (const auto &VI : Asm->MF->getVariableDbgInfo()) { 1094*0b57cec5SDimitry Andric if (!VI.Var) 1095*0b57cec5SDimitry Andric continue; 1096*0b57cec5SDimitry Andric assert(VI.Var->isValidLocationForIntrinsic(VI.Loc) && 1097*0b57cec5SDimitry Andric "Expected inlined-at fields to agree"); 1098*0b57cec5SDimitry Andric 1099*0b57cec5SDimitry Andric InlinedEntity Var(VI.Var, VI.Loc->getInlinedAt()); 1100*0b57cec5SDimitry Andric Processed.insert(Var); 1101*0b57cec5SDimitry Andric LexicalScope *Scope = LScopes.findLexicalScope(VI.Loc); 1102*0b57cec5SDimitry Andric 1103*0b57cec5SDimitry Andric // If variable scope is not found then skip this variable. 1104*0b57cec5SDimitry Andric if (!Scope) 1105*0b57cec5SDimitry Andric continue; 1106*0b57cec5SDimitry Andric 1107*0b57cec5SDimitry Andric ensureAbstractEntityIsCreatedIfScoped(TheCU, Var.first, Scope->getScopeNode()); 1108*0b57cec5SDimitry Andric auto RegVar = llvm::make_unique<DbgVariable>( 1109*0b57cec5SDimitry Andric cast<DILocalVariable>(Var.first), Var.second); 1110*0b57cec5SDimitry Andric RegVar->initializeMMI(VI.Expr, VI.Slot); 1111*0b57cec5SDimitry Andric if (DbgVariable *DbgVar = MFVars.lookup(Var)) 1112*0b57cec5SDimitry Andric DbgVar->addMMIEntry(*RegVar); 1113*0b57cec5SDimitry Andric else if (InfoHolder.addScopeVariable(Scope, RegVar.get())) { 1114*0b57cec5SDimitry Andric MFVars.insert({Var, RegVar.get()}); 1115*0b57cec5SDimitry Andric ConcreteEntities.push_back(std::move(RegVar)); 1116*0b57cec5SDimitry Andric } 1117*0b57cec5SDimitry Andric } 1118*0b57cec5SDimitry Andric } 1119*0b57cec5SDimitry Andric 1120*0b57cec5SDimitry Andric /// Determine whether a *singular* DBG_VALUE is valid for the entirety of its 1121*0b57cec5SDimitry Andric /// enclosing lexical scope. The check ensures there are no other instructions 1122*0b57cec5SDimitry Andric /// in the same lexical scope preceding the DBG_VALUE and that its range is 1123*0b57cec5SDimitry Andric /// either open or otherwise rolls off the end of the scope. 1124*0b57cec5SDimitry Andric static bool validThroughout(LexicalScopes &LScopes, 1125*0b57cec5SDimitry Andric const MachineInstr *DbgValue, 1126*0b57cec5SDimitry Andric const MachineInstr *RangeEnd) { 1127*0b57cec5SDimitry Andric assert(DbgValue->getDebugLoc() && "DBG_VALUE without a debug location"); 1128*0b57cec5SDimitry Andric auto MBB = DbgValue->getParent(); 1129*0b57cec5SDimitry Andric auto DL = DbgValue->getDebugLoc(); 1130*0b57cec5SDimitry Andric auto *LScope = LScopes.findLexicalScope(DL); 1131*0b57cec5SDimitry Andric // Scope doesn't exist; this is a dead DBG_VALUE. 1132*0b57cec5SDimitry Andric if (!LScope) 1133*0b57cec5SDimitry Andric return false; 1134*0b57cec5SDimitry Andric auto &LSRange = LScope->getRanges(); 1135*0b57cec5SDimitry Andric if (LSRange.size() == 0) 1136*0b57cec5SDimitry Andric return false; 1137*0b57cec5SDimitry Andric 1138*0b57cec5SDimitry Andric // Determine if the DBG_VALUE is valid at the beginning of its lexical block. 1139*0b57cec5SDimitry Andric const MachineInstr *LScopeBegin = LSRange.front().first; 1140*0b57cec5SDimitry Andric // Early exit if the lexical scope begins outside of the current block. 1141*0b57cec5SDimitry Andric if (LScopeBegin->getParent() != MBB) 1142*0b57cec5SDimitry Andric return false; 1143*0b57cec5SDimitry Andric MachineBasicBlock::const_reverse_iterator Pred(DbgValue); 1144*0b57cec5SDimitry Andric for (++Pred; Pred != MBB->rend(); ++Pred) { 1145*0b57cec5SDimitry Andric if (Pred->getFlag(MachineInstr::FrameSetup)) 1146*0b57cec5SDimitry Andric break; 1147*0b57cec5SDimitry Andric auto PredDL = Pred->getDebugLoc(); 1148*0b57cec5SDimitry Andric if (!PredDL || Pred->isMetaInstruction()) 1149*0b57cec5SDimitry Andric continue; 1150*0b57cec5SDimitry Andric // Check whether the instruction preceding the DBG_VALUE is in the same 1151*0b57cec5SDimitry Andric // (sub)scope as the DBG_VALUE. 1152*0b57cec5SDimitry Andric if (DL->getScope() == PredDL->getScope()) 1153*0b57cec5SDimitry Andric return false; 1154*0b57cec5SDimitry Andric auto *PredScope = LScopes.findLexicalScope(PredDL); 1155*0b57cec5SDimitry Andric if (!PredScope || LScope->dominates(PredScope)) 1156*0b57cec5SDimitry Andric return false; 1157*0b57cec5SDimitry Andric } 1158*0b57cec5SDimitry Andric 1159*0b57cec5SDimitry Andric // If the range of the DBG_VALUE is open-ended, report success. 1160*0b57cec5SDimitry Andric if (!RangeEnd) 1161*0b57cec5SDimitry Andric return true; 1162*0b57cec5SDimitry Andric 1163*0b57cec5SDimitry Andric // Fail if there are instructions belonging to our scope in another block. 1164*0b57cec5SDimitry Andric const MachineInstr *LScopeEnd = LSRange.back().second; 1165*0b57cec5SDimitry Andric if (LScopeEnd->getParent() != MBB) 1166*0b57cec5SDimitry Andric return false; 1167*0b57cec5SDimitry Andric 1168*0b57cec5SDimitry Andric // Single, constant DBG_VALUEs in the prologue are promoted to be live 1169*0b57cec5SDimitry Andric // throughout the function. This is a hack, presumably for DWARF v2 and not 1170*0b57cec5SDimitry Andric // necessarily correct. It would be much better to use a dbg.declare instead 1171*0b57cec5SDimitry Andric // if we know the constant is live throughout the scope. 1172*0b57cec5SDimitry Andric if (DbgValue->getOperand(0).isImm() && MBB->pred_empty()) 1173*0b57cec5SDimitry Andric return true; 1174*0b57cec5SDimitry Andric 1175*0b57cec5SDimitry Andric return false; 1176*0b57cec5SDimitry Andric } 1177*0b57cec5SDimitry Andric 1178*0b57cec5SDimitry Andric /// Build the location list for all DBG_VALUEs in the function that 1179*0b57cec5SDimitry Andric /// describe the same variable. The resulting DebugLocEntries will have 1180*0b57cec5SDimitry Andric /// strict monotonically increasing begin addresses and will never 1181*0b57cec5SDimitry Andric /// overlap. If the resulting list has only one entry that is valid 1182*0b57cec5SDimitry Andric /// throughout variable's scope return true. 1183*0b57cec5SDimitry Andric // 1184*0b57cec5SDimitry Andric // See the definition of DbgValueHistoryMap::Entry for an explanation of the 1185*0b57cec5SDimitry Andric // different kinds of history map entries. One thing to be aware of is that if 1186*0b57cec5SDimitry Andric // a debug value is ended by another entry (rather than being valid until the 1187*0b57cec5SDimitry Andric // end of the function), that entry's instruction may or may not be included in 1188*0b57cec5SDimitry Andric // the range, depending on if the entry is a clobbering entry (it has an 1189*0b57cec5SDimitry Andric // instruction that clobbers one or more preceding locations), or if it is an 1190*0b57cec5SDimitry Andric // (overlapping) debug value entry. This distinction can be seen in the example 1191*0b57cec5SDimitry Andric // below. The first debug value is ended by the clobbering entry 2, and the 1192*0b57cec5SDimitry Andric // second and third debug values are ended by the overlapping debug value entry 1193*0b57cec5SDimitry Andric // 4. 1194*0b57cec5SDimitry Andric // 1195*0b57cec5SDimitry Andric // Input: 1196*0b57cec5SDimitry Andric // 1197*0b57cec5SDimitry Andric // History map entries [type, end index, mi] 1198*0b57cec5SDimitry Andric // 1199*0b57cec5SDimitry Andric // 0 | [DbgValue, 2, DBG_VALUE $reg0, [...] (fragment 0, 32)] 1200*0b57cec5SDimitry Andric // 1 | | [DbgValue, 4, DBG_VALUE $reg1, [...] (fragment 32, 32)] 1201*0b57cec5SDimitry Andric // 2 | | [Clobber, $reg0 = [...], -, -] 1202*0b57cec5SDimitry Andric // 3 | | [DbgValue, 4, DBG_VALUE 123, [...] (fragment 64, 32)] 1203*0b57cec5SDimitry Andric // 4 [DbgValue, ~0, DBG_VALUE @g, [...] (fragment 0, 96)] 1204*0b57cec5SDimitry Andric // 1205*0b57cec5SDimitry Andric // Output [start, end) [Value...]: 1206*0b57cec5SDimitry Andric // 1207*0b57cec5SDimitry Andric // [0-1) [(reg0, fragment 0, 32)] 1208*0b57cec5SDimitry Andric // [1-3) [(reg0, fragment 0, 32), (reg1, fragment 32, 32)] 1209*0b57cec5SDimitry Andric // [3-4) [(reg1, fragment 32, 32), (123, fragment 64, 32)] 1210*0b57cec5SDimitry Andric // [4-) [(@g, fragment 0, 96)] 1211*0b57cec5SDimitry Andric bool DwarfDebug::buildLocationList(SmallVectorImpl<DebugLocEntry> &DebugLoc, 1212*0b57cec5SDimitry Andric const DbgValueHistoryMap::Entries &Entries) { 1213*0b57cec5SDimitry Andric using OpenRange = 1214*0b57cec5SDimitry Andric std::pair<DbgValueHistoryMap::EntryIndex, DbgValueLoc>; 1215*0b57cec5SDimitry Andric SmallVector<OpenRange, 4> OpenRanges; 1216*0b57cec5SDimitry Andric bool isSafeForSingleLocation = true; 1217*0b57cec5SDimitry Andric const MachineInstr *StartDebugMI = nullptr; 1218*0b57cec5SDimitry Andric const MachineInstr *EndMI = nullptr; 1219*0b57cec5SDimitry Andric 1220*0b57cec5SDimitry Andric for (auto EB = Entries.begin(), EI = EB, EE = Entries.end(); EI != EE; ++EI) { 1221*0b57cec5SDimitry Andric const MachineInstr *Instr = EI->getInstr(); 1222*0b57cec5SDimitry Andric 1223*0b57cec5SDimitry Andric // Remove all values that are no longer live. 1224*0b57cec5SDimitry Andric size_t Index = std::distance(EB, EI); 1225*0b57cec5SDimitry Andric auto Last = 1226*0b57cec5SDimitry Andric remove_if(OpenRanges, [&](OpenRange &R) { return R.first <= Index; }); 1227*0b57cec5SDimitry Andric OpenRanges.erase(Last, OpenRanges.end()); 1228*0b57cec5SDimitry Andric 1229*0b57cec5SDimitry Andric // If we are dealing with a clobbering entry, this iteration will result in 1230*0b57cec5SDimitry Andric // a location list entry starting after the clobbering instruction. 1231*0b57cec5SDimitry Andric const MCSymbol *StartLabel = 1232*0b57cec5SDimitry Andric EI->isClobber() ? getLabelAfterInsn(Instr) : getLabelBeforeInsn(Instr); 1233*0b57cec5SDimitry Andric assert(StartLabel && 1234*0b57cec5SDimitry Andric "Forgot label before/after instruction starting a range!"); 1235*0b57cec5SDimitry Andric 1236*0b57cec5SDimitry Andric const MCSymbol *EndLabel; 1237*0b57cec5SDimitry Andric if (std::next(EI) == Entries.end()) { 1238*0b57cec5SDimitry Andric EndLabel = Asm->getFunctionEnd(); 1239*0b57cec5SDimitry Andric if (EI->isClobber()) 1240*0b57cec5SDimitry Andric EndMI = EI->getInstr(); 1241*0b57cec5SDimitry Andric } 1242*0b57cec5SDimitry Andric else if (std::next(EI)->isClobber()) 1243*0b57cec5SDimitry Andric EndLabel = getLabelAfterInsn(std::next(EI)->getInstr()); 1244*0b57cec5SDimitry Andric else 1245*0b57cec5SDimitry Andric EndLabel = getLabelBeforeInsn(std::next(EI)->getInstr()); 1246*0b57cec5SDimitry Andric assert(EndLabel && "Forgot label after instruction ending a range!"); 1247*0b57cec5SDimitry Andric 1248*0b57cec5SDimitry Andric if (EI->isDbgValue()) 1249*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "DotDebugLoc: " << *Instr << "\n"); 1250*0b57cec5SDimitry Andric 1251*0b57cec5SDimitry Andric // If this history map entry has a debug value, add that to the list of 1252*0b57cec5SDimitry Andric // open ranges and check if its location is valid for a single value 1253*0b57cec5SDimitry Andric // location. 1254*0b57cec5SDimitry Andric if (EI->isDbgValue()) { 1255*0b57cec5SDimitry Andric // Do not add undef debug values, as they are redundant information in 1256*0b57cec5SDimitry Andric // the location list entries. An undef debug results in an empty location 1257*0b57cec5SDimitry Andric // description. If there are any non-undef fragments then padding pieces 1258*0b57cec5SDimitry Andric // with empty location descriptions will automatically be inserted, and if 1259*0b57cec5SDimitry Andric // all fragments are undef then the whole location list entry is 1260*0b57cec5SDimitry Andric // redundant. 1261*0b57cec5SDimitry Andric if (!Instr->isUndefDebugValue()) { 1262*0b57cec5SDimitry Andric auto Value = getDebugLocValue(Instr); 1263*0b57cec5SDimitry Andric OpenRanges.emplace_back(EI->getEndIndex(), Value); 1264*0b57cec5SDimitry Andric 1265*0b57cec5SDimitry Andric // TODO: Add support for single value fragment locations. 1266*0b57cec5SDimitry Andric if (Instr->getDebugExpression()->isFragment()) 1267*0b57cec5SDimitry Andric isSafeForSingleLocation = false; 1268*0b57cec5SDimitry Andric 1269*0b57cec5SDimitry Andric if (!StartDebugMI) 1270*0b57cec5SDimitry Andric StartDebugMI = Instr; 1271*0b57cec5SDimitry Andric } else { 1272*0b57cec5SDimitry Andric isSafeForSingleLocation = false; 1273*0b57cec5SDimitry Andric } 1274*0b57cec5SDimitry Andric } 1275*0b57cec5SDimitry Andric 1276*0b57cec5SDimitry Andric // Location list entries with empty location descriptions are redundant 1277*0b57cec5SDimitry Andric // information in DWARF, so do not emit those. 1278*0b57cec5SDimitry Andric if (OpenRanges.empty()) 1279*0b57cec5SDimitry Andric continue; 1280*0b57cec5SDimitry Andric 1281*0b57cec5SDimitry Andric // Omit entries with empty ranges as they do not have any effect in DWARF. 1282*0b57cec5SDimitry Andric if (StartLabel == EndLabel) { 1283*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Omitting location list entry with empty range.\n"); 1284*0b57cec5SDimitry Andric continue; 1285*0b57cec5SDimitry Andric } 1286*0b57cec5SDimitry Andric 1287*0b57cec5SDimitry Andric SmallVector<DbgValueLoc, 4> Values; 1288*0b57cec5SDimitry Andric for (auto &R : OpenRanges) 1289*0b57cec5SDimitry Andric Values.push_back(R.second); 1290*0b57cec5SDimitry Andric DebugLoc.emplace_back(StartLabel, EndLabel, Values); 1291*0b57cec5SDimitry Andric 1292*0b57cec5SDimitry Andric // Attempt to coalesce the ranges of two otherwise identical 1293*0b57cec5SDimitry Andric // DebugLocEntries. 1294*0b57cec5SDimitry Andric auto CurEntry = DebugLoc.rbegin(); 1295*0b57cec5SDimitry Andric LLVM_DEBUG({ 1296*0b57cec5SDimitry Andric dbgs() << CurEntry->getValues().size() << " Values:\n"; 1297*0b57cec5SDimitry Andric for (auto &Value : CurEntry->getValues()) 1298*0b57cec5SDimitry Andric Value.dump(); 1299*0b57cec5SDimitry Andric dbgs() << "-----\n"; 1300*0b57cec5SDimitry Andric }); 1301*0b57cec5SDimitry Andric 1302*0b57cec5SDimitry Andric auto PrevEntry = std::next(CurEntry); 1303*0b57cec5SDimitry Andric if (PrevEntry != DebugLoc.rend() && PrevEntry->MergeRanges(*CurEntry)) 1304*0b57cec5SDimitry Andric DebugLoc.pop_back(); 1305*0b57cec5SDimitry Andric } 1306*0b57cec5SDimitry Andric 1307*0b57cec5SDimitry Andric return DebugLoc.size() == 1 && isSafeForSingleLocation && 1308*0b57cec5SDimitry Andric validThroughout(LScopes, StartDebugMI, EndMI); 1309*0b57cec5SDimitry Andric } 1310*0b57cec5SDimitry Andric 1311*0b57cec5SDimitry Andric DbgEntity *DwarfDebug::createConcreteEntity(DwarfCompileUnit &TheCU, 1312*0b57cec5SDimitry Andric LexicalScope &Scope, 1313*0b57cec5SDimitry Andric const DINode *Node, 1314*0b57cec5SDimitry Andric const DILocation *Location, 1315*0b57cec5SDimitry Andric const MCSymbol *Sym) { 1316*0b57cec5SDimitry Andric ensureAbstractEntityIsCreatedIfScoped(TheCU, Node, Scope.getScopeNode()); 1317*0b57cec5SDimitry Andric if (isa<const DILocalVariable>(Node)) { 1318*0b57cec5SDimitry Andric ConcreteEntities.push_back( 1319*0b57cec5SDimitry Andric llvm::make_unique<DbgVariable>(cast<const DILocalVariable>(Node), 1320*0b57cec5SDimitry Andric Location)); 1321*0b57cec5SDimitry Andric InfoHolder.addScopeVariable(&Scope, 1322*0b57cec5SDimitry Andric cast<DbgVariable>(ConcreteEntities.back().get())); 1323*0b57cec5SDimitry Andric } else if (isa<const DILabel>(Node)) { 1324*0b57cec5SDimitry Andric ConcreteEntities.push_back( 1325*0b57cec5SDimitry Andric llvm::make_unique<DbgLabel>(cast<const DILabel>(Node), 1326*0b57cec5SDimitry Andric Location, Sym)); 1327*0b57cec5SDimitry Andric InfoHolder.addScopeLabel(&Scope, 1328*0b57cec5SDimitry Andric cast<DbgLabel>(ConcreteEntities.back().get())); 1329*0b57cec5SDimitry Andric } 1330*0b57cec5SDimitry Andric return ConcreteEntities.back().get(); 1331*0b57cec5SDimitry Andric } 1332*0b57cec5SDimitry Andric 1333*0b57cec5SDimitry Andric // Find variables for each lexical scope. 1334*0b57cec5SDimitry Andric void DwarfDebug::collectEntityInfo(DwarfCompileUnit &TheCU, 1335*0b57cec5SDimitry Andric const DISubprogram *SP, 1336*0b57cec5SDimitry Andric DenseSet<InlinedEntity> &Processed) { 1337*0b57cec5SDimitry Andric // Grab the variable info that was squirreled away in the MMI side-table. 1338*0b57cec5SDimitry Andric collectVariableInfoFromMFTable(TheCU, Processed); 1339*0b57cec5SDimitry Andric 1340*0b57cec5SDimitry Andric for (const auto &I : DbgValues) { 1341*0b57cec5SDimitry Andric InlinedEntity IV = I.first; 1342*0b57cec5SDimitry Andric if (Processed.count(IV)) 1343*0b57cec5SDimitry Andric continue; 1344*0b57cec5SDimitry Andric 1345*0b57cec5SDimitry Andric // Instruction ranges, specifying where IV is accessible. 1346*0b57cec5SDimitry Andric const auto &HistoryMapEntries = I.second; 1347*0b57cec5SDimitry Andric if (HistoryMapEntries.empty()) 1348*0b57cec5SDimitry Andric continue; 1349*0b57cec5SDimitry Andric 1350*0b57cec5SDimitry Andric LexicalScope *Scope = nullptr; 1351*0b57cec5SDimitry Andric const DILocalVariable *LocalVar = cast<DILocalVariable>(IV.first); 1352*0b57cec5SDimitry Andric if (const DILocation *IA = IV.second) 1353*0b57cec5SDimitry Andric Scope = LScopes.findInlinedScope(LocalVar->getScope(), IA); 1354*0b57cec5SDimitry Andric else 1355*0b57cec5SDimitry Andric Scope = LScopes.findLexicalScope(LocalVar->getScope()); 1356*0b57cec5SDimitry Andric // If variable scope is not found then skip this variable. 1357*0b57cec5SDimitry Andric if (!Scope) 1358*0b57cec5SDimitry Andric continue; 1359*0b57cec5SDimitry Andric 1360*0b57cec5SDimitry Andric Processed.insert(IV); 1361*0b57cec5SDimitry Andric DbgVariable *RegVar = cast<DbgVariable>(createConcreteEntity(TheCU, 1362*0b57cec5SDimitry Andric *Scope, LocalVar, IV.second)); 1363*0b57cec5SDimitry Andric 1364*0b57cec5SDimitry Andric const MachineInstr *MInsn = HistoryMapEntries.front().getInstr(); 1365*0b57cec5SDimitry Andric assert(MInsn->isDebugValue() && "History must begin with debug value"); 1366*0b57cec5SDimitry Andric 1367*0b57cec5SDimitry Andric // Check if there is a single DBG_VALUE, valid throughout the var's scope. 1368*0b57cec5SDimitry Andric // If the history map contains a single debug value, there may be an 1369*0b57cec5SDimitry Andric // additional entry which clobbers the debug value. 1370*0b57cec5SDimitry Andric size_t HistSize = HistoryMapEntries.size(); 1371*0b57cec5SDimitry Andric bool SingleValueWithClobber = 1372*0b57cec5SDimitry Andric HistSize == 2 && HistoryMapEntries[1].isClobber(); 1373*0b57cec5SDimitry Andric if (HistSize == 1 || SingleValueWithClobber) { 1374*0b57cec5SDimitry Andric const auto *End = 1375*0b57cec5SDimitry Andric SingleValueWithClobber ? HistoryMapEntries[1].getInstr() : nullptr; 1376*0b57cec5SDimitry Andric if (validThroughout(LScopes, MInsn, End)) { 1377*0b57cec5SDimitry Andric RegVar->initializeDbgValue(MInsn); 1378*0b57cec5SDimitry Andric continue; 1379*0b57cec5SDimitry Andric } 1380*0b57cec5SDimitry Andric } 1381*0b57cec5SDimitry Andric 1382*0b57cec5SDimitry Andric // Do not emit location lists if .debug_loc secton is disabled. 1383*0b57cec5SDimitry Andric if (!useLocSection()) 1384*0b57cec5SDimitry Andric continue; 1385*0b57cec5SDimitry Andric 1386*0b57cec5SDimitry Andric // Handle multiple DBG_VALUE instructions describing one variable. 1387*0b57cec5SDimitry Andric DebugLocStream::ListBuilder List(DebugLocs, TheCU, *Asm, *RegVar, *MInsn); 1388*0b57cec5SDimitry Andric 1389*0b57cec5SDimitry Andric // Build the location list for this variable. 1390*0b57cec5SDimitry Andric SmallVector<DebugLocEntry, 8> Entries; 1391*0b57cec5SDimitry Andric bool isValidSingleLocation = buildLocationList(Entries, HistoryMapEntries); 1392*0b57cec5SDimitry Andric 1393*0b57cec5SDimitry Andric // Check whether buildLocationList managed to merge all locations to one 1394*0b57cec5SDimitry Andric // that is valid throughout the variable's scope. If so, produce single 1395*0b57cec5SDimitry Andric // value location. 1396*0b57cec5SDimitry Andric if (isValidSingleLocation) { 1397*0b57cec5SDimitry Andric RegVar->initializeDbgValue(Entries[0].getValues()[0]); 1398*0b57cec5SDimitry Andric continue; 1399*0b57cec5SDimitry Andric } 1400*0b57cec5SDimitry Andric 1401*0b57cec5SDimitry Andric // If the variable has a DIBasicType, extract it. Basic types cannot have 1402*0b57cec5SDimitry Andric // unique identifiers, so don't bother resolving the type with the 1403*0b57cec5SDimitry Andric // identifier map. 1404*0b57cec5SDimitry Andric const DIBasicType *BT = dyn_cast<DIBasicType>( 1405*0b57cec5SDimitry Andric static_cast<const Metadata *>(LocalVar->getType())); 1406*0b57cec5SDimitry Andric 1407*0b57cec5SDimitry Andric // Finalize the entry by lowering it into a DWARF bytestream. 1408*0b57cec5SDimitry Andric for (auto &Entry : Entries) 1409*0b57cec5SDimitry Andric Entry.finalize(*Asm, List, BT, TheCU); 1410*0b57cec5SDimitry Andric } 1411*0b57cec5SDimitry Andric 1412*0b57cec5SDimitry Andric // For each InlinedEntity collected from DBG_LABEL instructions, convert to 1413*0b57cec5SDimitry Andric // DWARF-related DbgLabel. 1414*0b57cec5SDimitry Andric for (const auto &I : DbgLabels) { 1415*0b57cec5SDimitry Andric InlinedEntity IL = I.first; 1416*0b57cec5SDimitry Andric const MachineInstr *MI = I.second; 1417*0b57cec5SDimitry Andric if (MI == nullptr) 1418*0b57cec5SDimitry Andric continue; 1419*0b57cec5SDimitry Andric 1420*0b57cec5SDimitry Andric LexicalScope *Scope = nullptr; 1421*0b57cec5SDimitry Andric const DILabel *Label = cast<DILabel>(IL.first); 1422*0b57cec5SDimitry Andric // Get inlined DILocation if it is inlined label. 1423*0b57cec5SDimitry Andric if (const DILocation *IA = IL.second) 1424*0b57cec5SDimitry Andric Scope = LScopes.findInlinedScope(Label->getScope(), IA); 1425*0b57cec5SDimitry Andric else 1426*0b57cec5SDimitry Andric Scope = LScopes.findLexicalScope(Label->getScope()); 1427*0b57cec5SDimitry Andric // If label scope is not found then skip this label. 1428*0b57cec5SDimitry Andric if (!Scope) 1429*0b57cec5SDimitry Andric continue; 1430*0b57cec5SDimitry Andric 1431*0b57cec5SDimitry Andric Processed.insert(IL); 1432*0b57cec5SDimitry Andric /// At this point, the temporary label is created. 1433*0b57cec5SDimitry Andric /// Save the temporary label to DbgLabel entity to get the 1434*0b57cec5SDimitry Andric /// actually address when generating Dwarf DIE. 1435*0b57cec5SDimitry Andric MCSymbol *Sym = getLabelBeforeInsn(MI); 1436*0b57cec5SDimitry Andric createConcreteEntity(TheCU, *Scope, Label, IL.second, Sym); 1437*0b57cec5SDimitry Andric } 1438*0b57cec5SDimitry Andric 1439*0b57cec5SDimitry Andric // Collect info for variables/labels that were optimized out. 1440*0b57cec5SDimitry Andric for (const DINode *DN : SP->getRetainedNodes()) { 1441*0b57cec5SDimitry Andric if (!Processed.insert(InlinedEntity(DN, nullptr)).second) 1442*0b57cec5SDimitry Andric continue; 1443*0b57cec5SDimitry Andric LexicalScope *Scope = nullptr; 1444*0b57cec5SDimitry Andric if (auto *DV = dyn_cast<DILocalVariable>(DN)) { 1445*0b57cec5SDimitry Andric Scope = LScopes.findLexicalScope(DV->getScope()); 1446*0b57cec5SDimitry Andric } else if (auto *DL = dyn_cast<DILabel>(DN)) { 1447*0b57cec5SDimitry Andric Scope = LScopes.findLexicalScope(DL->getScope()); 1448*0b57cec5SDimitry Andric } 1449*0b57cec5SDimitry Andric 1450*0b57cec5SDimitry Andric if (Scope) 1451*0b57cec5SDimitry Andric createConcreteEntity(TheCU, *Scope, DN, nullptr); 1452*0b57cec5SDimitry Andric } 1453*0b57cec5SDimitry Andric } 1454*0b57cec5SDimitry Andric 1455*0b57cec5SDimitry Andric // Process beginning of an instruction. 1456*0b57cec5SDimitry Andric void DwarfDebug::beginInstruction(const MachineInstr *MI) { 1457*0b57cec5SDimitry Andric DebugHandlerBase::beginInstruction(MI); 1458*0b57cec5SDimitry Andric assert(CurMI); 1459*0b57cec5SDimitry Andric 1460*0b57cec5SDimitry Andric const auto *SP = MI->getMF()->getFunction().getSubprogram(); 1461*0b57cec5SDimitry Andric if (!SP || SP->getUnit()->getEmissionKind() == DICompileUnit::NoDebug) 1462*0b57cec5SDimitry Andric return; 1463*0b57cec5SDimitry Andric 1464*0b57cec5SDimitry Andric // Check if source location changes, but ignore DBG_VALUE and CFI locations. 1465*0b57cec5SDimitry Andric // If the instruction is part of the function frame setup code, do not emit 1466*0b57cec5SDimitry Andric // any line record, as there is no correspondence with any user code. 1467*0b57cec5SDimitry Andric if (MI->isMetaInstruction() || MI->getFlag(MachineInstr::FrameSetup)) 1468*0b57cec5SDimitry Andric return; 1469*0b57cec5SDimitry Andric const DebugLoc &DL = MI->getDebugLoc(); 1470*0b57cec5SDimitry Andric // When we emit a line-0 record, we don't update PrevInstLoc; so look at 1471*0b57cec5SDimitry Andric // the last line number actually emitted, to see if it was line 0. 1472*0b57cec5SDimitry Andric unsigned LastAsmLine = 1473*0b57cec5SDimitry Andric Asm->OutStreamer->getContext().getCurrentDwarfLoc().getLine(); 1474*0b57cec5SDimitry Andric 1475*0b57cec5SDimitry Andric // Request a label after the call in order to emit AT_return_pc information 1476*0b57cec5SDimitry Andric // in call site entries. TODO: Add support for targets with delay slots. 1477*0b57cec5SDimitry Andric if (SP->areAllCallsDescribed() && MI->isCall() && !MI->hasDelaySlot()) 1478*0b57cec5SDimitry Andric requestLabelAfterInsn(MI); 1479*0b57cec5SDimitry Andric 1480*0b57cec5SDimitry Andric if (DL == PrevInstLoc) { 1481*0b57cec5SDimitry Andric // If we have an ongoing unspecified location, nothing to do here. 1482*0b57cec5SDimitry Andric if (!DL) 1483*0b57cec5SDimitry Andric return; 1484*0b57cec5SDimitry Andric // We have an explicit location, same as the previous location. 1485*0b57cec5SDimitry Andric // But we might be coming back to it after a line 0 record. 1486*0b57cec5SDimitry Andric if (LastAsmLine == 0 && DL.getLine() != 0) { 1487*0b57cec5SDimitry Andric // Reinstate the source location but not marked as a statement. 1488*0b57cec5SDimitry Andric const MDNode *Scope = DL.getScope(); 1489*0b57cec5SDimitry Andric recordSourceLine(DL.getLine(), DL.getCol(), Scope, /*Flags=*/0); 1490*0b57cec5SDimitry Andric } 1491*0b57cec5SDimitry Andric return; 1492*0b57cec5SDimitry Andric } 1493*0b57cec5SDimitry Andric 1494*0b57cec5SDimitry Andric if (!DL) { 1495*0b57cec5SDimitry Andric // We have an unspecified location, which might want to be line 0. 1496*0b57cec5SDimitry Andric // If we have already emitted a line-0 record, don't repeat it. 1497*0b57cec5SDimitry Andric if (LastAsmLine == 0) 1498*0b57cec5SDimitry Andric return; 1499*0b57cec5SDimitry Andric // If user said Don't Do That, don't do that. 1500*0b57cec5SDimitry Andric if (UnknownLocations == Disable) 1501*0b57cec5SDimitry Andric return; 1502*0b57cec5SDimitry Andric // See if we have a reason to emit a line-0 record now. 1503*0b57cec5SDimitry Andric // Reasons to emit a line-0 record include: 1504*0b57cec5SDimitry Andric // - User asked for it (UnknownLocations). 1505*0b57cec5SDimitry Andric // - Instruction has a label, so it's referenced from somewhere else, 1506*0b57cec5SDimitry Andric // possibly debug information; we want it to have a source location. 1507*0b57cec5SDimitry Andric // - Instruction is at the top of a block; we don't want to inherit the 1508*0b57cec5SDimitry Andric // location from the physically previous (maybe unrelated) block. 1509*0b57cec5SDimitry Andric if (UnknownLocations == Enable || PrevLabel || 1510*0b57cec5SDimitry Andric (PrevInstBB && PrevInstBB != MI->getParent())) { 1511*0b57cec5SDimitry Andric // Preserve the file and column numbers, if we can, to save space in 1512*0b57cec5SDimitry Andric // the encoded line table. 1513*0b57cec5SDimitry Andric // Do not update PrevInstLoc, it remembers the last non-0 line. 1514*0b57cec5SDimitry Andric const MDNode *Scope = nullptr; 1515*0b57cec5SDimitry Andric unsigned Column = 0; 1516*0b57cec5SDimitry Andric if (PrevInstLoc) { 1517*0b57cec5SDimitry Andric Scope = PrevInstLoc.getScope(); 1518*0b57cec5SDimitry Andric Column = PrevInstLoc.getCol(); 1519*0b57cec5SDimitry Andric } 1520*0b57cec5SDimitry Andric recordSourceLine(/*Line=*/0, Column, Scope, /*Flags=*/0); 1521*0b57cec5SDimitry Andric } 1522*0b57cec5SDimitry Andric return; 1523*0b57cec5SDimitry Andric } 1524*0b57cec5SDimitry Andric 1525*0b57cec5SDimitry Andric // We have an explicit location, different from the previous location. 1526*0b57cec5SDimitry Andric // Don't repeat a line-0 record, but otherwise emit the new location. 1527*0b57cec5SDimitry Andric // (The new location might be an explicit line 0, which we do emit.) 1528*0b57cec5SDimitry Andric if (DL.getLine() == 0 && LastAsmLine == 0) 1529*0b57cec5SDimitry Andric return; 1530*0b57cec5SDimitry Andric unsigned Flags = 0; 1531*0b57cec5SDimitry Andric if (DL == PrologEndLoc) { 1532*0b57cec5SDimitry Andric Flags |= DWARF2_FLAG_PROLOGUE_END | DWARF2_FLAG_IS_STMT; 1533*0b57cec5SDimitry Andric PrologEndLoc = DebugLoc(); 1534*0b57cec5SDimitry Andric } 1535*0b57cec5SDimitry Andric // If the line changed, we call that a new statement; unless we went to 1536*0b57cec5SDimitry Andric // line 0 and came back, in which case it is not a new statement. 1537*0b57cec5SDimitry Andric unsigned OldLine = PrevInstLoc ? PrevInstLoc.getLine() : LastAsmLine; 1538*0b57cec5SDimitry Andric if (DL.getLine() && DL.getLine() != OldLine) 1539*0b57cec5SDimitry Andric Flags |= DWARF2_FLAG_IS_STMT; 1540*0b57cec5SDimitry Andric 1541*0b57cec5SDimitry Andric const MDNode *Scope = DL.getScope(); 1542*0b57cec5SDimitry Andric recordSourceLine(DL.getLine(), DL.getCol(), Scope, Flags); 1543*0b57cec5SDimitry Andric 1544*0b57cec5SDimitry Andric // If we're not at line 0, remember this location. 1545*0b57cec5SDimitry Andric if (DL.getLine()) 1546*0b57cec5SDimitry Andric PrevInstLoc = DL; 1547*0b57cec5SDimitry Andric } 1548*0b57cec5SDimitry Andric 1549*0b57cec5SDimitry Andric static DebugLoc findPrologueEndLoc(const MachineFunction *MF) { 1550*0b57cec5SDimitry Andric // First known non-DBG_VALUE and non-frame setup location marks 1551*0b57cec5SDimitry Andric // the beginning of the function body. 1552*0b57cec5SDimitry Andric for (const auto &MBB : *MF) 1553*0b57cec5SDimitry Andric for (const auto &MI : MBB) 1554*0b57cec5SDimitry Andric if (!MI.isMetaInstruction() && !MI.getFlag(MachineInstr::FrameSetup) && 1555*0b57cec5SDimitry Andric MI.getDebugLoc()) 1556*0b57cec5SDimitry Andric return MI.getDebugLoc(); 1557*0b57cec5SDimitry Andric return DebugLoc(); 1558*0b57cec5SDimitry Andric } 1559*0b57cec5SDimitry Andric 1560*0b57cec5SDimitry Andric /// Register a source line with debug info. Returns the unique label that was 1561*0b57cec5SDimitry Andric /// emitted and which provides correspondence to the source line list. 1562*0b57cec5SDimitry Andric static void recordSourceLine(AsmPrinter &Asm, unsigned Line, unsigned Col, 1563*0b57cec5SDimitry Andric const MDNode *S, unsigned Flags, unsigned CUID, 1564*0b57cec5SDimitry Andric uint16_t DwarfVersion, 1565*0b57cec5SDimitry Andric ArrayRef<std::unique_ptr<DwarfCompileUnit>> DCUs) { 1566*0b57cec5SDimitry Andric StringRef Fn; 1567*0b57cec5SDimitry Andric unsigned FileNo = 1; 1568*0b57cec5SDimitry Andric unsigned Discriminator = 0; 1569*0b57cec5SDimitry Andric if (auto *Scope = cast_or_null<DIScope>(S)) { 1570*0b57cec5SDimitry Andric Fn = Scope->getFilename(); 1571*0b57cec5SDimitry Andric if (Line != 0 && DwarfVersion >= 4) 1572*0b57cec5SDimitry Andric if (auto *LBF = dyn_cast<DILexicalBlockFile>(Scope)) 1573*0b57cec5SDimitry Andric Discriminator = LBF->getDiscriminator(); 1574*0b57cec5SDimitry Andric 1575*0b57cec5SDimitry Andric FileNo = static_cast<DwarfCompileUnit &>(*DCUs[CUID]) 1576*0b57cec5SDimitry Andric .getOrCreateSourceID(Scope->getFile()); 1577*0b57cec5SDimitry Andric } 1578*0b57cec5SDimitry Andric Asm.OutStreamer->EmitDwarfLocDirective(FileNo, Line, Col, Flags, 0, 1579*0b57cec5SDimitry Andric Discriminator, Fn); 1580*0b57cec5SDimitry Andric } 1581*0b57cec5SDimitry Andric 1582*0b57cec5SDimitry Andric DebugLoc DwarfDebug::emitInitialLocDirective(const MachineFunction &MF, 1583*0b57cec5SDimitry Andric unsigned CUID) { 1584*0b57cec5SDimitry Andric // Get beginning of function. 1585*0b57cec5SDimitry Andric if (DebugLoc PrologEndLoc = findPrologueEndLoc(&MF)) { 1586*0b57cec5SDimitry Andric // Ensure the compile unit is created if the function is called before 1587*0b57cec5SDimitry Andric // beginFunction(). 1588*0b57cec5SDimitry Andric (void)getOrCreateDwarfCompileUnit( 1589*0b57cec5SDimitry Andric MF.getFunction().getSubprogram()->getUnit()); 1590*0b57cec5SDimitry Andric // We'd like to list the prologue as "not statements" but GDB behaves 1591*0b57cec5SDimitry Andric // poorly if we do that. Revisit this with caution/GDB (7.5+) testing. 1592*0b57cec5SDimitry Andric const DISubprogram *SP = PrologEndLoc->getInlinedAtScope()->getSubprogram(); 1593*0b57cec5SDimitry Andric ::recordSourceLine(*Asm, SP->getScopeLine(), 0, SP, DWARF2_FLAG_IS_STMT, 1594*0b57cec5SDimitry Andric CUID, getDwarfVersion(), getUnits()); 1595*0b57cec5SDimitry Andric return PrologEndLoc; 1596*0b57cec5SDimitry Andric } 1597*0b57cec5SDimitry Andric return DebugLoc(); 1598*0b57cec5SDimitry Andric } 1599*0b57cec5SDimitry Andric 1600*0b57cec5SDimitry Andric // Gather pre-function debug information. Assumes being called immediately 1601*0b57cec5SDimitry Andric // after the function entry point has been emitted. 1602*0b57cec5SDimitry Andric void DwarfDebug::beginFunctionImpl(const MachineFunction *MF) { 1603*0b57cec5SDimitry Andric CurFn = MF; 1604*0b57cec5SDimitry Andric 1605*0b57cec5SDimitry Andric auto *SP = MF->getFunction().getSubprogram(); 1606*0b57cec5SDimitry Andric assert(LScopes.empty() || SP == LScopes.getCurrentFunctionScope()->getScopeNode()); 1607*0b57cec5SDimitry Andric if (SP->getUnit()->getEmissionKind() == DICompileUnit::NoDebug) 1608*0b57cec5SDimitry Andric return; 1609*0b57cec5SDimitry Andric 1610*0b57cec5SDimitry Andric DwarfCompileUnit &CU = getOrCreateDwarfCompileUnit(SP->getUnit()); 1611*0b57cec5SDimitry Andric 1612*0b57cec5SDimitry Andric // Set DwarfDwarfCompileUnitID in MCContext to the Compile Unit this function 1613*0b57cec5SDimitry Andric // belongs to so that we add to the correct per-cu line table in the 1614*0b57cec5SDimitry Andric // non-asm case. 1615*0b57cec5SDimitry Andric if (Asm->OutStreamer->hasRawTextSupport()) 1616*0b57cec5SDimitry Andric // Use a single line table if we are generating assembly. 1617*0b57cec5SDimitry Andric Asm->OutStreamer->getContext().setDwarfCompileUnitID(0); 1618*0b57cec5SDimitry Andric else 1619*0b57cec5SDimitry Andric Asm->OutStreamer->getContext().setDwarfCompileUnitID(CU.getUniqueID()); 1620*0b57cec5SDimitry Andric 1621*0b57cec5SDimitry Andric // Record beginning of function. 1622*0b57cec5SDimitry Andric PrologEndLoc = emitInitialLocDirective( 1623*0b57cec5SDimitry Andric *MF, Asm->OutStreamer->getContext().getDwarfCompileUnitID()); 1624*0b57cec5SDimitry Andric } 1625*0b57cec5SDimitry Andric 1626*0b57cec5SDimitry Andric void DwarfDebug::skippedNonDebugFunction() { 1627*0b57cec5SDimitry Andric // If we don't have a subprogram for this function then there will be a hole 1628*0b57cec5SDimitry Andric // in the range information. Keep note of this by setting the previously used 1629*0b57cec5SDimitry Andric // section to nullptr. 1630*0b57cec5SDimitry Andric PrevCU = nullptr; 1631*0b57cec5SDimitry Andric CurFn = nullptr; 1632*0b57cec5SDimitry Andric } 1633*0b57cec5SDimitry Andric 1634*0b57cec5SDimitry Andric // Gather and emit post-function debug information. 1635*0b57cec5SDimitry Andric void DwarfDebug::endFunctionImpl(const MachineFunction *MF) { 1636*0b57cec5SDimitry Andric const DISubprogram *SP = MF->getFunction().getSubprogram(); 1637*0b57cec5SDimitry Andric 1638*0b57cec5SDimitry Andric assert(CurFn == MF && 1639*0b57cec5SDimitry Andric "endFunction should be called with the same function as beginFunction"); 1640*0b57cec5SDimitry Andric 1641*0b57cec5SDimitry Andric // Set DwarfDwarfCompileUnitID in MCContext to default value. 1642*0b57cec5SDimitry Andric Asm->OutStreamer->getContext().setDwarfCompileUnitID(0); 1643*0b57cec5SDimitry Andric 1644*0b57cec5SDimitry Andric LexicalScope *FnScope = LScopes.getCurrentFunctionScope(); 1645*0b57cec5SDimitry Andric assert(!FnScope || SP == FnScope->getScopeNode()); 1646*0b57cec5SDimitry Andric DwarfCompileUnit &TheCU = *CUMap.lookup(SP->getUnit()); 1647*0b57cec5SDimitry Andric if (TheCU.getCUNode()->isDebugDirectivesOnly()) { 1648*0b57cec5SDimitry Andric PrevLabel = nullptr; 1649*0b57cec5SDimitry Andric CurFn = nullptr; 1650*0b57cec5SDimitry Andric return; 1651*0b57cec5SDimitry Andric } 1652*0b57cec5SDimitry Andric 1653*0b57cec5SDimitry Andric DenseSet<InlinedEntity> Processed; 1654*0b57cec5SDimitry Andric collectEntityInfo(TheCU, SP, Processed); 1655*0b57cec5SDimitry Andric 1656*0b57cec5SDimitry Andric // Add the range of this function to the list of ranges for the CU. 1657*0b57cec5SDimitry Andric TheCU.addRange(RangeSpan(Asm->getFunctionBegin(), Asm->getFunctionEnd())); 1658*0b57cec5SDimitry Andric 1659*0b57cec5SDimitry Andric // Under -gmlt, skip building the subprogram if there are no inlined 1660*0b57cec5SDimitry Andric // subroutines inside it. But with -fdebug-info-for-profiling, the subprogram 1661*0b57cec5SDimitry Andric // is still needed as we need its source location. 1662*0b57cec5SDimitry Andric if (!TheCU.getCUNode()->getDebugInfoForProfiling() && 1663*0b57cec5SDimitry Andric TheCU.getCUNode()->getEmissionKind() == DICompileUnit::LineTablesOnly && 1664*0b57cec5SDimitry Andric LScopes.getAbstractScopesList().empty() && !IsDarwin) { 1665*0b57cec5SDimitry Andric assert(InfoHolder.getScopeVariables().empty()); 1666*0b57cec5SDimitry Andric PrevLabel = nullptr; 1667*0b57cec5SDimitry Andric CurFn = nullptr; 1668*0b57cec5SDimitry Andric return; 1669*0b57cec5SDimitry Andric } 1670*0b57cec5SDimitry Andric 1671*0b57cec5SDimitry Andric #ifndef NDEBUG 1672*0b57cec5SDimitry Andric size_t NumAbstractScopes = LScopes.getAbstractScopesList().size(); 1673*0b57cec5SDimitry Andric #endif 1674*0b57cec5SDimitry Andric // Construct abstract scopes. 1675*0b57cec5SDimitry Andric for (LexicalScope *AScope : LScopes.getAbstractScopesList()) { 1676*0b57cec5SDimitry Andric auto *SP = cast<DISubprogram>(AScope->getScopeNode()); 1677*0b57cec5SDimitry Andric for (const DINode *DN : SP->getRetainedNodes()) { 1678*0b57cec5SDimitry Andric if (!Processed.insert(InlinedEntity(DN, nullptr)).second) 1679*0b57cec5SDimitry Andric continue; 1680*0b57cec5SDimitry Andric 1681*0b57cec5SDimitry Andric const MDNode *Scope = nullptr; 1682*0b57cec5SDimitry Andric if (auto *DV = dyn_cast<DILocalVariable>(DN)) 1683*0b57cec5SDimitry Andric Scope = DV->getScope(); 1684*0b57cec5SDimitry Andric else if (auto *DL = dyn_cast<DILabel>(DN)) 1685*0b57cec5SDimitry Andric Scope = DL->getScope(); 1686*0b57cec5SDimitry Andric else 1687*0b57cec5SDimitry Andric llvm_unreachable("Unexpected DI type!"); 1688*0b57cec5SDimitry Andric 1689*0b57cec5SDimitry Andric // Collect info for variables/labels that were optimized out. 1690*0b57cec5SDimitry Andric ensureAbstractEntityIsCreated(TheCU, DN, Scope); 1691*0b57cec5SDimitry Andric assert(LScopes.getAbstractScopesList().size() == NumAbstractScopes 1692*0b57cec5SDimitry Andric && "ensureAbstractEntityIsCreated inserted abstract scopes"); 1693*0b57cec5SDimitry Andric } 1694*0b57cec5SDimitry Andric constructAbstractSubprogramScopeDIE(TheCU, AScope); 1695*0b57cec5SDimitry Andric } 1696*0b57cec5SDimitry Andric 1697*0b57cec5SDimitry Andric ProcessedSPNodes.insert(SP); 1698*0b57cec5SDimitry Andric DIE &ScopeDIE = TheCU.constructSubprogramScopeDIE(SP, FnScope); 1699*0b57cec5SDimitry Andric if (auto *SkelCU = TheCU.getSkeleton()) 1700*0b57cec5SDimitry Andric if (!LScopes.getAbstractScopesList().empty() && 1701*0b57cec5SDimitry Andric TheCU.getCUNode()->getSplitDebugInlining()) 1702*0b57cec5SDimitry Andric SkelCU->constructSubprogramScopeDIE(SP, FnScope); 1703*0b57cec5SDimitry Andric 1704*0b57cec5SDimitry Andric // Construct call site entries. 1705*0b57cec5SDimitry Andric constructCallSiteEntryDIEs(*SP, TheCU, ScopeDIE, *MF); 1706*0b57cec5SDimitry Andric 1707*0b57cec5SDimitry Andric // Clear debug info 1708*0b57cec5SDimitry Andric // Ownership of DbgVariables is a bit subtle - ScopeVariables owns all the 1709*0b57cec5SDimitry Andric // DbgVariables except those that are also in AbstractVariables (since they 1710*0b57cec5SDimitry Andric // can be used cross-function) 1711*0b57cec5SDimitry Andric InfoHolder.getScopeVariables().clear(); 1712*0b57cec5SDimitry Andric InfoHolder.getScopeLabels().clear(); 1713*0b57cec5SDimitry Andric PrevLabel = nullptr; 1714*0b57cec5SDimitry Andric CurFn = nullptr; 1715*0b57cec5SDimitry Andric } 1716*0b57cec5SDimitry Andric 1717*0b57cec5SDimitry Andric // Register a source line with debug info. Returns the unique label that was 1718*0b57cec5SDimitry Andric // emitted and which provides correspondence to the source line list. 1719*0b57cec5SDimitry Andric void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S, 1720*0b57cec5SDimitry Andric unsigned Flags) { 1721*0b57cec5SDimitry Andric ::recordSourceLine(*Asm, Line, Col, S, Flags, 1722*0b57cec5SDimitry Andric Asm->OutStreamer->getContext().getDwarfCompileUnitID(), 1723*0b57cec5SDimitry Andric getDwarfVersion(), getUnits()); 1724*0b57cec5SDimitry Andric } 1725*0b57cec5SDimitry Andric 1726*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 1727*0b57cec5SDimitry Andric // Emit Methods 1728*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 1729*0b57cec5SDimitry Andric 1730*0b57cec5SDimitry Andric // Emit the debug info section. 1731*0b57cec5SDimitry Andric void DwarfDebug::emitDebugInfo() { 1732*0b57cec5SDimitry Andric DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder; 1733*0b57cec5SDimitry Andric Holder.emitUnits(/* UseOffsets */ false); 1734*0b57cec5SDimitry Andric } 1735*0b57cec5SDimitry Andric 1736*0b57cec5SDimitry Andric // Emit the abbreviation section. 1737*0b57cec5SDimitry Andric void DwarfDebug::emitAbbreviations() { 1738*0b57cec5SDimitry Andric DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder; 1739*0b57cec5SDimitry Andric 1740*0b57cec5SDimitry Andric Holder.emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevSection()); 1741*0b57cec5SDimitry Andric } 1742*0b57cec5SDimitry Andric 1743*0b57cec5SDimitry Andric void DwarfDebug::emitStringOffsetsTableHeader() { 1744*0b57cec5SDimitry Andric DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder; 1745*0b57cec5SDimitry Andric Holder.getStringPool().emitStringOffsetsTableHeader( 1746*0b57cec5SDimitry Andric *Asm, Asm->getObjFileLowering().getDwarfStrOffSection(), 1747*0b57cec5SDimitry Andric Holder.getStringOffsetsStartSym()); 1748*0b57cec5SDimitry Andric } 1749*0b57cec5SDimitry Andric 1750*0b57cec5SDimitry Andric template <typename AccelTableT> 1751*0b57cec5SDimitry Andric void DwarfDebug::emitAccel(AccelTableT &Accel, MCSection *Section, 1752*0b57cec5SDimitry Andric StringRef TableName) { 1753*0b57cec5SDimitry Andric Asm->OutStreamer->SwitchSection(Section); 1754*0b57cec5SDimitry Andric 1755*0b57cec5SDimitry Andric // Emit the full data. 1756*0b57cec5SDimitry Andric emitAppleAccelTable(Asm, Accel, TableName, Section->getBeginSymbol()); 1757*0b57cec5SDimitry Andric } 1758*0b57cec5SDimitry Andric 1759*0b57cec5SDimitry Andric void DwarfDebug::emitAccelDebugNames() { 1760*0b57cec5SDimitry Andric // Don't emit anything if we have no compilation units to index. 1761*0b57cec5SDimitry Andric if (getUnits().empty()) 1762*0b57cec5SDimitry Andric return; 1763*0b57cec5SDimitry Andric 1764*0b57cec5SDimitry Andric emitDWARF5AccelTable(Asm, AccelDebugNames, *this, getUnits()); 1765*0b57cec5SDimitry Andric } 1766*0b57cec5SDimitry Andric 1767*0b57cec5SDimitry Andric // Emit visible names into a hashed accelerator table section. 1768*0b57cec5SDimitry Andric void DwarfDebug::emitAccelNames() { 1769*0b57cec5SDimitry Andric emitAccel(AccelNames, Asm->getObjFileLowering().getDwarfAccelNamesSection(), 1770*0b57cec5SDimitry Andric "Names"); 1771*0b57cec5SDimitry Andric } 1772*0b57cec5SDimitry Andric 1773*0b57cec5SDimitry Andric // Emit objective C classes and categories into a hashed accelerator table 1774*0b57cec5SDimitry Andric // section. 1775*0b57cec5SDimitry Andric void DwarfDebug::emitAccelObjC() { 1776*0b57cec5SDimitry Andric emitAccel(AccelObjC, Asm->getObjFileLowering().getDwarfAccelObjCSection(), 1777*0b57cec5SDimitry Andric "ObjC"); 1778*0b57cec5SDimitry Andric } 1779*0b57cec5SDimitry Andric 1780*0b57cec5SDimitry Andric // Emit namespace dies into a hashed accelerator table. 1781*0b57cec5SDimitry Andric void DwarfDebug::emitAccelNamespaces() { 1782*0b57cec5SDimitry Andric emitAccel(AccelNamespace, 1783*0b57cec5SDimitry Andric Asm->getObjFileLowering().getDwarfAccelNamespaceSection(), 1784*0b57cec5SDimitry Andric "namespac"); 1785*0b57cec5SDimitry Andric } 1786*0b57cec5SDimitry Andric 1787*0b57cec5SDimitry Andric // Emit type dies into a hashed accelerator table. 1788*0b57cec5SDimitry Andric void DwarfDebug::emitAccelTypes() { 1789*0b57cec5SDimitry Andric emitAccel(AccelTypes, Asm->getObjFileLowering().getDwarfAccelTypesSection(), 1790*0b57cec5SDimitry Andric "types"); 1791*0b57cec5SDimitry Andric } 1792*0b57cec5SDimitry Andric 1793*0b57cec5SDimitry Andric // Public name handling. 1794*0b57cec5SDimitry Andric // The format for the various pubnames: 1795*0b57cec5SDimitry Andric // 1796*0b57cec5SDimitry Andric // dwarf pubnames - offset/name pairs where the offset is the offset into the CU 1797*0b57cec5SDimitry Andric // for the DIE that is named. 1798*0b57cec5SDimitry Andric // 1799*0b57cec5SDimitry Andric // gnu pubnames - offset/index value/name tuples where the offset is the offset 1800*0b57cec5SDimitry Andric // into the CU and the index value is computed according to the type of value 1801*0b57cec5SDimitry Andric // for the DIE that is named. 1802*0b57cec5SDimitry Andric // 1803*0b57cec5SDimitry Andric // For type units the offset is the offset of the skeleton DIE. For split dwarf 1804*0b57cec5SDimitry Andric // it's the offset within the debug_info/debug_types dwo section, however, the 1805*0b57cec5SDimitry Andric // reference in the pubname header doesn't change. 1806*0b57cec5SDimitry Andric 1807*0b57cec5SDimitry Andric /// computeIndexValue - Compute the gdb index value for the DIE and CU. 1808*0b57cec5SDimitry Andric static dwarf::PubIndexEntryDescriptor computeIndexValue(DwarfUnit *CU, 1809*0b57cec5SDimitry Andric const DIE *Die) { 1810*0b57cec5SDimitry Andric // Entities that ended up only in a Type Unit reference the CU instead (since 1811*0b57cec5SDimitry Andric // the pub entry has offsets within the CU there's no real offset that can be 1812*0b57cec5SDimitry Andric // provided anyway). As it happens all such entities (namespaces and types, 1813*0b57cec5SDimitry Andric // types only in C++ at that) are rendered as TYPE+EXTERNAL. If this turns out 1814*0b57cec5SDimitry Andric // not to be true it would be necessary to persist this information from the 1815*0b57cec5SDimitry Andric // point at which the entry is added to the index data structure - since by 1816*0b57cec5SDimitry Andric // the time the index is built from that, the original type/namespace DIE in a 1817*0b57cec5SDimitry Andric // type unit has already been destroyed so it can't be queried for properties 1818*0b57cec5SDimitry Andric // like tag, etc. 1819*0b57cec5SDimitry Andric if (Die->getTag() == dwarf::DW_TAG_compile_unit) 1820*0b57cec5SDimitry Andric return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_TYPE, 1821*0b57cec5SDimitry Andric dwarf::GIEL_EXTERNAL); 1822*0b57cec5SDimitry Andric dwarf::GDBIndexEntryLinkage Linkage = dwarf::GIEL_STATIC; 1823*0b57cec5SDimitry Andric 1824*0b57cec5SDimitry Andric // We could have a specification DIE that has our most of our knowledge, 1825*0b57cec5SDimitry Andric // look for that now. 1826*0b57cec5SDimitry Andric if (DIEValue SpecVal = Die->findAttribute(dwarf::DW_AT_specification)) { 1827*0b57cec5SDimitry Andric DIE &SpecDIE = SpecVal.getDIEEntry().getEntry(); 1828*0b57cec5SDimitry Andric if (SpecDIE.findAttribute(dwarf::DW_AT_external)) 1829*0b57cec5SDimitry Andric Linkage = dwarf::GIEL_EXTERNAL; 1830*0b57cec5SDimitry Andric } else if (Die->findAttribute(dwarf::DW_AT_external)) 1831*0b57cec5SDimitry Andric Linkage = dwarf::GIEL_EXTERNAL; 1832*0b57cec5SDimitry Andric 1833*0b57cec5SDimitry Andric switch (Die->getTag()) { 1834*0b57cec5SDimitry Andric case dwarf::DW_TAG_class_type: 1835*0b57cec5SDimitry Andric case dwarf::DW_TAG_structure_type: 1836*0b57cec5SDimitry Andric case dwarf::DW_TAG_union_type: 1837*0b57cec5SDimitry Andric case dwarf::DW_TAG_enumeration_type: 1838*0b57cec5SDimitry Andric return dwarf::PubIndexEntryDescriptor( 1839*0b57cec5SDimitry Andric dwarf::GIEK_TYPE, CU->getLanguage() != dwarf::DW_LANG_C_plus_plus 1840*0b57cec5SDimitry Andric ? dwarf::GIEL_STATIC 1841*0b57cec5SDimitry Andric : dwarf::GIEL_EXTERNAL); 1842*0b57cec5SDimitry Andric case dwarf::DW_TAG_typedef: 1843*0b57cec5SDimitry Andric case dwarf::DW_TAG_base_type: 1844*0b57cec5SDimitry Andric case dwarf::DW_TAG_subrange_type: 1845*0b57cec5SDimitry Andric return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_TYPE, dwarf::GIEL_STATIC); 1846*0b57cec5SDimitry Andric case dwarf::DW_TAG_namespace: 1847*0b57cec5SDimitry Andric return dwarf::GIEK_TYPE; 1848*0b57cec5SDimitry Andric case dwarf::DW_TAG_subprogram: 1849*0b57cec5SDimitry Andric return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_FUNCTION, Linkage); 1850*0b57cec5SDimitry Andric case dwarf::DW_TAG_variable: 1851*0b57cec5SDimitry Andric return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_VARIABLE, Linkage); 1852*0b57cec5SDimitry Andric case dwarf::DW_TAG_enumerator: 1853*0b57cec5SDimitry Andric return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_VARIABLE, 1854*0b57cec5SDimitry Andric dwarf::GIEL_STATIC); 1855*0b57cec5SDimitry Andric default: 1856*0b57cec5SDimitry Andric return dwarf::GIEK_NONE; 1857*0b57cec5SDimitry Andric } 1858*0b57cec5SDimitry Andric } 1859*0b57cec5SDimitry Andric 1860*0b57cec5SDimitry Andric /// emitDebugPubSections - Emit visible names and types into debug pubnames and 1861*0b57cec5SDimitry Andric /// pubtypes sections. 1862*0b57cec5SDimitry Andric void DwarfDebug::emitDebugPubSections() { 1863*0b57cec5SDimitry Andric for (const auto &NU : CUMap) { 1864*0b57cec5SDimitry Andric DwarfCompileUnit *TheU = NU.second; 1865*0b57cec5SDimitry Andric if (!TheU->hasDwarfPubSections()) 1866*0b57cec5SDimitry Andric continue; 1867*0b57cec5SDimitry Andric 1868*0b57cec5SDimitry Andric bool GnuStyle = TheU->getCUNode()->getNameTableKind() == 1869*0b57cec5SDimitry Andric DICompileUnit::DebugNameTableKind::GNU; 1870*0b57cec5SDimitry Andric 1871*0b57cec5SDimitry Andric Asm->OutStreamer->SwitchSection( 1872*0b57cec5SDimitry Andric GnuStyle ? Asm->getObjFileLowering().getDwarfGnuPubNamesSection() 1873*0b57cec5SDimitry Andric : Asm->getObjFileLowering().getDwarfPubNamesSection()); 1874*0b57cec5SDimitry Andric emitDebugPubSection(GnuStyle, "Names", TheU, TheU->getGlobalNames()); 1875*0b57cec5SDimitry Andric 1876*0b57cec5SDimitry Andric Asm->OutStreamer->SwitchSection( 1877*0b57cec5SDimitry Andric GnuStyle ? Asm->getObjFileLowering().getDwarfGnuPubTypesSection() 1878*0b57cec5SDimitry Andric : Asm->getObjFileLowering().getDwarfPubTypesSection()); 1879*0b57cec5SDimitry Andric emitDebugPubSection(GnuStyle, "Types", TheU, TheU->getGlobalTypes()); 1880*0b57cec5SDimitry Andric } 1881*0b57cec5SDimitry Andric } 1882*0b57cec5SDimitry Andric 1883*0b57cec5SDimitry Andric void DwarfDebug::emitSectionReference(const DwarfCompileUnit &CU) { 1884*0b57cec5SDimitry Andric if (useSectionsAsReferences()) 1885*0b57cec5SDimitry Andric Asm->EmitDwarfOffset(CU.getSection()->getBeginSymbol(), 1886*0b57cec5SDimitry Andric CU.getDebugSectionOffset()); 1887*0b57cec5SDimitry Andric else 1888*0b57cec5SDimitry Andric Asm->emitDwarfSymbolReference(CU.getLabelBegin()); 1889*0b57cec5SDimitry Andric } 1890*0b57cec5SDimitry Andric 1891*0b57cec5SDimitry Andric void DwarfDebug::emitDebugPubSection(bool GnuStyle, StringRef Name, 1892*0b57cec5SDimitry Andric DwarfCompileUnit *TheU, 1893*0b57cec5SDimitry Andric const StringMap<const DIE *> &Globals) { 1894*0b57cec5SDimitry Andric if (auto *Skeleton = TheU->getSkeleton()) 1895*0b57cec5SDimitry Andric TheU = Skeleton; 1896*0b57cec5SDimitry Andric 1897*0b57cec5SDimitry Andric // Emit the header. 1898*0b57cec5SDimitry Andric Asm->OutStreamer->AddComment("Length of Public " + Name + " Info"); 1899*0b57cec5SDimitry Andric MCSymbol *BeginLabel = Asm->createTempSymbol("pub" + Name + "_begin"); 1900*0b57cec5SDimitry Andric MCSymbol *EndLabel = Asm->createTempSymbol("pub" + Name + "_end"); 1901*0b57cec5SDimitry Andric Asm->EmitLabelDifference(EndLabel, BeginLabel, 4); 1902*0b57cec5SDimitry Andric 1903*0b57cec5SDimitry Andric Asm->OutStreamer->EmitLabel(BeginLabel); 1904*0b57cec5SDimitry Andric 1905*0b57cec5SDimitry Andric Asm->OutStreamer->AddComment("DWARF Version"); 1906*0b57cec5SDimitry Andric Asm->emitInt16(dwarf::DW_PUBNAMES_VERSION); 1907*0b57cec5SDimitry Andric 1908*0b57cec5SDimitry Andric Asm->OutStreamer->AddComment("Offset of Compilation Unit Info"); 1909*0b57cec5SDimitry Andric emitSectionReference(*TheU); 1910*0b57cec5SDimitry Andric 1911*0b57cec5SDimitry Andric Asm->OutStreamer->AddComment("Compilation Unit Length"); 1912*0b57cec5SDimitry Andric Asm->emitInt32(TheU->getLength()); 1913*0b57cec5SDimitry Andric 1914*0b57cec5SDimitry Andric // Emit the pubnames for this compilation unit. 1915*0b57cec5SDimitry Andric for (const auto &GI : Globals) { 1916*0b57cec5SDimitry Andric const char *Name = GI.getKeyData(); 1917*0b57cec5SDimitry Andric const DIE *Entity = GI.second; 1918*0b57cec5SDimitry Andric 1919*0b57cec5SDimitry Andric Asm->OutStreamer->AddComment("DIE offset"); 1920*0b57cec5SDimitry Andric Asm->emitInt32(Entity->getOffset()); 1921*0b57cec5SDimitry Andric 1922*0b57cec5SDimitry Andric if (GnuStyle) { 1923*0b57cec5SDimitry Andric dwarf::PubIndexEntryDescriptor Desc = computeIndexValue(TheU, Entity); 1924*0b57cec5SDimitry Andric Asm->OutStreamer->AddComment( 1925*0b57cec5SDimitry Andric Twine("Attributes: ") + dwarf::GDBIndexEntryKindString(Desc.Kind) + 1926*0b57cec5SDimitry Andric ", " + dwarf::GDBIndexEntryLinkageString(Desc.Linkage)); 1927*0b57cec5SDimitry Andric Asm->emitInt8(Desc.toBits()); 1928*0b57cec5SDimitry Andric } 1929*0b57cec5SDimitry Andric 1930*0b57cec5SDimitry Andric Asm->OutStreamer->AddComment("External Name"); 1931*0b57cec5SDimitry Andric Asm->OutStreamer->EmitBytes(StringRef(Name, GI.getKeyLength() + 1)); 1932*0b57cec5SDimitry Andric } 1933*0b57cec5SDimitry Andric 1934*0b57cec5SDimitry Andric Asm->OutStreamer->AddComment("End Mark"); 1935*0b57cec5SDimitry Andric Asm->emitInt32(0); 1936*0b57cec5SDimitry Andric Asm->OutStreamer->EmitLabel(EndLabel); 1937*0b57cec5SDimitry Andric } 1938*0b57cec5SDimitry Andric 1939*0b57cec5SDimitry Andric /// Emit null-terminated strings into a debug str section. 1940*0b57cec5SDimitry Andric void DwarfDebug::emitDebugStr() { 1941*0b57cec5SDimitry Andric MCSection *StringOffsetsSection = nullptr; 1942*0b57cec5SDimitry Andric if (useSegmentedStringOffsetsTable()) { 1943*0b57cec5SDimitry Andric emitStringOffsetsTableHeader(); 1944*0b57cec5SDimitry Andric StringOffsetsSection = Asm->getObjFileLowering().getDwarfStrOffSection(); 1945*0b57cec5SDimitry Andric } 1946*0b57cec5SDimitry Andric DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder; 1947*0b57cec5SDimitry Andric Holder.emitStrings(Asm->getObjFileLowering().getDwarfStrSection(), 1948*0b57cec5SDimitry Andric StringOffsetsSection, /* UseRelativeOffsets = */ true); 1949*0b57cec5SDimitry Andric } 1950*0b57cec5SDimitry Andric 1951*0b57cec5SDimitry Andric void DwarfDebug::emitDebugLocEntry(ByteStreamer &Streamer, 1952*0b57cec5SDimitry Andric const DebugLocStream::Entry &Entry, 1953*0b57cec5SDimitry Andric const DwarfCompileUnit *CU) { 1954*0b57cec5SDimitry Andric auto &&Comments = DebugLocs.getComments(Entry); 1955*0b57cec5SDimitry Andric auto Comment = Comments.begin(); 1956*0b57cec5SDimitry Andric auto End = Comments.end(); 1957*0b57cec5SDimitry Andric 1958*0b57cec5SDimitry Andric // The expressions are inserted into a byte stream rather early (see 1959*0b57cec5SDimitry Andric // DwarfExpression::addExpression) so for those ops (e.g. DW_OP_convert) that 1960*0b57cec5SDimitry Andric // need to reference a base_type DIE the offset of that DIE is not yet known. 1961*0b57cec5SDimitry Andric // To deal with this we instead insert a placeholder early and then extract 1962*0b57cec5SDimitry Andric // it here and replace it with the real reference. 1963*0b57cec5SDimitry Andric unsigned PtrSize = Asm->MAI->getCodePointerSize(); 1964*0b57cec5SDimitry Andric DWARFDataExtractor Data(StringRef(DebugLocs.getBytes(Entry).data(), 1965*0b57cec5SDimitry Andric DebugLocs.getBytes(Entry).size()), 1966*0b57cec5SDimitry Andric Asm->getDataLayout().isLittleEndian(), PtrSize); 1967*0b57cec5SDimitry Andric DWARFExpression Expr(Data, getDwarfVersion(), PtrSize); 1968*0b57cec5SDimitry Andric 1969*0b57cec5SDimitry Andric using Encoding = DWARFExpression::Operation::Encoding; 1970*0b57cec5SDimitry Andric uint32_t Offset = 0; 1971*0b57cec5SDimitry Andric for (auto &Op : Expr) { 1972*0b57cec5SDimitry Andric assert(Op.getCode() != dwarf::DW_OP_const_type && 1973*0b57cec5SDimitry Andric "3 operand ops not yet supported"); 1974*0b57cec5SDimitry Andric Streamer.EmitInt8(Op.getCode(), Comment != End ? *(Comment++) : ""); 1975*0b57cec5SDimitry Andric Offset++; 1976*0b57cec5SDimitry Andric for (unsigned I = 0; I < 2; ++I) { 1977*0b57cec5SDimitry Andric if (Op.getDescription().Op[I] == Encoding::SizeNA) 1978*0b57cec5SDimitry Andric continue; 1979*0b57cec5SDimitry Andric if (Op.getDescription().Op[I] == Encoding::BaseTypeRef) { 1980*0b57cec5SDimitry Andric if (CU) { 1981*0b57cec5SDimitry Andric uint64_t Offset = CU->ExprRefedBaseTypes[Op.getRawOperand(I)].Die->getOffset(); 1982*0b57cec5SDimitry Andric assert(Offset < (1ULL << (ULEB128PadSize * 7)) && "Offset wont fit"); 1983*0b57cec5SDimitry Andric Asm->EmitULEB128(Offset, nullptr, ULEB128PadSize); 1984*0b57cec5SDimitry Andric } else { 1985*0b57cec5SDimitry Andric // Emit a reference to the 'generic type'. 1986*0b57cec5SDimitry Andric Asm->EmitULEB128(0, nullptr, ULEB128PadSize); 1987*0b57cec5SDimitry Andric } 1988*0b57cec5SDimitry Andric // Make sure comments stay aligned. 1989*0b57cec5SDimitry Andric for (unsigned J = 0; J < ULEB128PadSize; ++J) 1990*0b57cec5SDimitry Andric if (Comment != End) 1991*0b57cec5SDimitry Andric Comment++; 1992*0b57cec5SDimitry Andric } else { 1993*0b57cec5SDimitry Andric for (uint32_t J = Offset; J < Op.getOperandEndOffset(I); ++J) 1994*0b57cec5SDimitry Andric Streamer.EmitInt8(Data.getData()[J], Comment != End ? *(Comment++) : ""); 1995*0b57cec5SDimitry Andric } 1996*0b57cec5SDimitry Andric Offset = Op.getOperandEndOffset(I); 1997*0b57cec5SDimitry Andric } 1998*0b57cec5SDimitry Andric assert(Offset == Op.getEndOffset()); 1999*0b57cec5SDimitry Andric } 2000*0b57cec5SDimitry Andric } 2001*0b57cec5SDimitry Andric 2002*0b57cec5SDimitry Andric void DwarfDebug::emitDebugLocValue(const AsmPrinter &AP, const DIBasicType *BT, 2003*0b57cec5SDimitry Andric const DbgValueLoc &Value, 2004*0b57cec5SDimitry Andric DwarfExpression &DwarfExpr) { 2005*0b57cec5SDimitry Andric auto *DIExpr = Value.getExpression(); 2006*0b57cec5SDimitry Andric DIExpressionCursor ExprCursor(DIExpr); 2007*0b57cec5SDimitry Andric DwarfExpr.addFragmentOffset(DIExpr); 2008*0b57cec5SDimitry Andric // Regular entry. 2009*0b57cec5SDimitry Andric if (Value.isInt()) { 2010*0b57cec5SDimitry Andric if (BT && (BT->getEncoding() == dwarf::DW_ATE_signed || 2011*0b57cec5SDimitry Andric BT->getEncoding() == dwarf::DW_ATE_signed_char)) 2012*0b57cec5SDimitry Andric DwarfExpr.addSignedConstant(Value.getInt()); 2013*0b57cec5SDimitry Andric else 2014*0b57cec5SDimitry Andric DwarfExpr.addUnsignedConstant(Value.getInt()); 2015*0b57cec5SDimitry Andric } else if (Value.isLocation()) { 2016*0b57cec5SDimitry Andric MachineLocation Location = Value.getLoc(); 2017*0b57cec5SDimitry Andric if (Location.isIndirect()) 2018*0b57cec5SDimitry Andric DwarfExpr.setMemoryLocationKind(); 2019*0b57cec5SDimitry Andric DIExpressionCursor Cursor(DIExpr); 2020*0b57cec5SDimitry Andric 2021*0b57cec5SDimitry Andric if (DIExpr->isEntryValue()) { 2022*0b57cec5SDimitry Andric DwarfExpr.setEntryValueFlag(); 2023*0b57cec5SDimitry Andric DwarfExpr.addEntryValueExpression(Cursor); 2024*0b57cec5SDimitry Andric } 2025*0b57cec5SDimitry Andric 2026*0b57cec5SDimitry Andric const TargetRegisterInfo &TRI = *AP.MF->getSubtarget().getRegisterInfo(); 2027*0b57cec5SDimitry Andric if (!DwarfExpr.addMachineRegExpression(TRI, Cursor, Location.getReg())) 2028*0b57cec5SDimitry Andric return; 2029*0b57cec5SDimitry Andric return DwarfExpr.addExpression(std::move(Cursor)); 2030*0b57cec5SDimitry Andric } else if (Value.isConstantFP()) { 2031*0b57cec5SDimitry Andric APInt RawBytes = Value.getConstantFP()->getValueAPF().bitcastToAPInt(); 2032*0b57cec5SDimitry Andric DwarfExpr.addUnsignedConstant(RawBytes); 2033*0b57cec5SDimitry Andric } 2034*0b57cec5SDimitry Andric DwarfExpr.addExpression(std::move(ExprCursor)); 2035*0b57cec5SDimitry Andric } 2036*0b57cec5SDimitry Andric 2037*0b57cec5SDimitry Andric void DebugLocEntry::finalize(const AsmPrinter &AP, 2038*0b57cec5SDimitry Andric DebugLocStream::ListBuilder &List, 2039*0b57cec5SDimitry Andric const DIBasicType *BT, 2040*0b57cec5SDimitry Andric DwarfCompileUnit &TheCU) { 2041*0b57cec5SDimitry Andric assert(!Values.empty() && 2042*0b57cec5SDimitry Andric "location list entries without values are redundant"); 2043*0b57cec5SDimitry Andric assert(Begin != End && "unexpected location list entry with empty range"); 2044*0b57cec5SDimitry Andric DebugLocStream::EntryBuilder Entry(List, Begin, End); 2045*0b57cec5SDimitry Andric BufferByteStreamer Streamer = Entry.getStreamer(); 2046*0b57cec5SDimitry Andric DebugLocDwarfExpression DwarfExpr(AP.getDwarfVersion(), Streamer, TheCU); 2047*0b57cec5SDimitry Andric const DbgValueLoc &Value = Values[0]; 2048*0b57cec5SDimitry Andric if (Value.isFragment()) { 2049*0b57cec5SDimitry Andric // Emit all fragments that belong to the same variable and range. 2050*0b57cec5SDimitry Andric assert(llvm::all_of(Values, [](DbgValueLoc P) { 2051*0b57cec5SDimitry Andric return P.isFragment(); 2052*0b57cec5SDimitry Andric }) && "all values are expected to be fragments"); 2053*0b57cec5SDimitry Andric assert(std::is_sorted(Values.begin(), Values.end()) && 2054*0b57cec5SDimitry Andric "fragments are expected to be sorted"); 2055*0b57cec5SDimitry Andric 2056*0b57cec5SDimitry Andric for (auto Fragment : Values) 2057*0b57cec5SDimitry Andric DwarfDebug::emitDebugLocValue(AP, BT, Fragment, DwarfExpr); 2058*0b57cec5SDimitry Andric 2059*0b57cec5SDimitry Andric } else { 2060*0b57cec5SDimitry Andric assert(Values.size() == 1 && "only fragments may have >1 value"); 2061*0b57cec5SDimitry Andric DwarfDebug::emitDebugLocValue(AP, BT, Value, DwarfExpr); 2062*0b57cec5SDimitry Andric } 2063*0b57cec5SDimitry Andric DwarfExpr.finalize(); 2064*0b57cec5SDimitry Andric } 2065*0b57cec5SDimitry Andric 2066*0b57cec5SDimitry Andric void DwarfDebug::emitDebugLocEntryLocation(const DebugLocStream::Entry &Entry, 2067*0b57cec5SDimitry Andric const DwarfCompileUnit *CU) { 2068*0b57cec5SDimitry Andric // Emit the size. 2069*0b57cec5SDimitry Andric Asm->OutStreamer->AddComment("Loc expr size"); 2070*0b57cec5SDimitry Andric if (getDwarfVersion() >= 5) 2071*0b57cec5SDimitry Andric Asm->EmitULEB128(DebugLocs.getBytes(Entry).size()); 2072*0b57cec5SDimitry Andric else if (DebugLocs.getBytes(Entry).size() <= std::numeric_limits<uint16_t>::max()) 2073*0b57cec5SDimitry Andric Asm->emitInt16(DebugLocs.getBytes(Entry).size()); 2074*0b57cec5SDimitry Andric else { 2075*0b57cec5SDimitry Andric // The entry is too big to fit into 16 bit, drop it as there is nothing we 2076*0b57cec5SDimitry Andric // can do. 2077*0b57cec5SDimitry Andric Asm->emitInt16(0); 2078*0b57cec5SDimitry Andric return; 2079*0b57cec5SDimitry Andric } 2080*0b57cec5SDimitry Andric // Emit the entry. 2081*0b57cec5SDimitry Andric APByteStreamer Streamer(*Asm); 2082*0b57cec5SDimitry Andric emitDebugLocEntry(Streamer, Entry, CU); 2083*0b57cec5SDimitry Andric } 2084*0b57cec5SDimitry Andric 2085*0b57cec5SDimitry Andric // Emit the common part of the DWARF 5 range/locations list tables header. 2086*0b57cec5SDimitry Andric static void emitListsTableHeaderStart(AsmPrinter *Asm, const DwarfFile &Holder, 2087*0b57cec5SDimitry Andric MCSymbol *TableStart, 2088*0b57cec5SDimitry Andric MCSymbol *TableEnd) { 2089*0b57cec5SDimitry Andric // Build the table header, which starts with the length field. 2090*0b57cec5SDimitry Andric Asm->OutStreamer->AddComment("Length"); 2091*0b57cec5SDimitry Andric Asm->EmitLabelDifference(TableEnd, TableStart, 4); 2092*0b57cec5SDimitry Andric Asm->OutStreamer->EmitLabel(TableStart); 2093*0b57cec5SDimitry Andric // Version number (DWARF v5 and later). 2094*0b57cec5SDimitry Andric Asm->OutStreamer->AddComment("Version"); 2095*0b57cec5SDimitry Andric Asm->emitInt16(Asm->OutStreamer->getContext().getDwarfVersion()); 2096*0b57cec5SDimitry Andric // Address size. 2097*0b57cec5SDimitry Andric Asm->OutStreamer->AddComment("Address size"); 2098*0b57cec5SDimitry Andric Asm->emitInt8(Asm->MAI->getCodePointerSize()); 2099*0b57cec5SDimitry Andric // Segment selector size. 2100*0b57cec5SDimitry Andric Asm->OutStreamer->AddComment("Segment selector size"); 2101*0b57cec5SDimitry Andric Asm->emitInt8(0); 2102*0b57cec5SDimitry Andric } 2103*0b57cec5SDimitry Andric 2104*0b57cec5SDimitry Andric // Emit the header of a DWARF 5 range list table list table. Returns the symbol 2105*0b57cec5SDimitry Andric // that designates the end of the table for the caller to emit when the table is 2106*0b57cec5SDimitry Andric // complete. 2107*0b57cec5SDimitry Andric static MCSymbol *emitRnglistsTableHeader(AsmPrinter *Asm, 2108*0b57cec5SDimitry Andric const DwarfFile &Holder) { 2109*0b57cec5SDimitry Andric MCSymbol *TableStart = Asm->createTempSymbol("debug_rnglist_table_start"); 2110*0b57cec5SDimitry Andric MCSymbol *TableEnd = Asm->createTempSymbol("debug_rnglist_table_end"); 2111*0b57cec5SDimitry Andric emitListsTableHeaderStart(Asm, Holder, TableStart, TableEnd); 2112*0b57cec5SDimitry Andric 2113*0b57cec5SDimitry Andric Asm->OutStreamer->AddComment("Offset entry count"); 2114*0b57cec5SDimitry Andric Asm->emitInt32(Holder.getRangeLists().size()); 2115*0b57cec5SDimitry Andric Asm->OutStreamer->EmitLabel(Holder.getRnglistsTableBaseSym()); 2116*0b57cec5SDimitry Andric 2117*0b57cec5SDimitry Andric for (const RangeSpanList &List : Holder.getRangeLists()) 2118*0b57cec5SDimitry Andric Asm->EmitLabelDifference(List.getSym(), Holder.getRnglistsTableBaseSym(), 2119*0b57cec5SDimitry Andric 4); 2120*0b57cec5SDimitry Andric 2121*0b57cec5SDimitry Andric return TableEnd; 2122*0b57cec5SDimitry Andric } 2123*0b57cec5SDimitry Andric 2124*0b57cec5SDimitry Andric // Emit the header of a DWARF 5 locations list table. Returns the symbol that 2125*0b57cec5SDimitry Andric // designates the end of the table for the caller to emit when the table is 2126*0b57cec5SDimitry Andric // complete. 2127*0b57cec5SDimitry Andric static MCSymbol *emitLoclistsTableHeader(AsmPrinter *Asm, 2128*0b57cec5SDimitry Andric const DwarfFile &Holder) { 2129*0b57cec5SDimitry Andric MCSymbol *TableStart = Asm->createTempSymbol("debug_loclist_table_start"); 2130*0b57cec5SDimitry Andric MCSymbol *TableEnd = Asm->createTempSymbol("debug_loclist_table_end"); 2131*0b57cec5SDimitry Andric emitListsTableHeaderStart(Asm, Holder, TableStart, TableEnd); 2132*0b57cec5SDimitry Andric 2133*0b57cec5SDimitry Andric // FIXME: Generate the offsets table and use DW_FORM_loclistx with the 2134*0b57cec5SDimitry Andric // DW_AT_loclists_base attribute. Until then set the number of offsets to 0. 2135*0b57cec5SDimitry Andric Asm->OutStreamer->AddComment("Offset entry count"); 2136*0b57cec5SDimitry Andric Asm->emitInt32(0); 2137*0b57cec5SDimitry Andric Asm->OutStreamer->EmitLabel(Holder.getLoclistsTableBaseSym()); 2138*0b57cec5SDimitry Andric 2139*0b57cec5SDimitry Andric return TableEnd; 2140*0b57cec5SDimitry Andric } 2141*0b57cec5SDimitry Andric 2142*0b57cec5SDimitry Andric // Emit locations into the .debug_loc/.debug_rnglists section. 2143*0b57cec5SDimitry Andric void DwarfDebug::emitDebugLoc() { 2144*0b57cec5SDimitry Andric if (DebugLocs.getLists().empty()) 2145*0b57cec5SDimitry Andric return; 2146*0b57cec5SDimitry Andric 2147*0b57cec5SDimitry Andric bool IsLocLists = getDwarfVersion() >= 5; 2148*0b57cec5SDimitry Andric MCSymbol *TableEnd = nullptr; 2149*0b57cec5SDimitry Andric if (IsLocLists) { 2150*0b57cec5SDimitry Andric Asm->OutStreamer->SwitchSection( 2151*0b57cec5SDimitry Andric Asm->getObjFileLowering().getDwarfLoclistsSection()); 2152*0b57cec5SDimitry Andric TableEnd = emitLoclistsTableHeader(Asm, useSplitDwarf() ? SkeletonHolder 2153*0b57cec5SDimitry Andric : InfoHolder); 2154*0b57cec5SDimitry Andric } else { 2155*0b57cec5SDimitry Andric Asm->OutStreamer->SwitchSection( 2156*0b57cec5SDimitry Andric Asm->getObjFileLowering().getDwarfLocSection()); 2157*0b57cec5SDimitry Andric } 2158*0b57cec5SDimitry Andric 2159*0b57cec5SDimitry Andric unsigned char Size = Asm->MAI->getCodePointerSize(); 2160*0b57cec5SDimitry Andric for (const auto &List : DebugLocs.getLists()) { 2161*0b57cec5SDimitry Andric Asm->OutStreamer->EmitLabel(List.Label); 2162*0b57cec5SDimitry Andric 2163*0b57cec5SDimitry Andric const DwarfCompileUnit *CU = List.CU; 2164*0b57cec5SDimitry Andric const MCSymbol *Base = CU->getBaseAddress(); 2165*0b57cec5SDimitry Andric for (const auto &Entry : DebugLocs.getEntries(List)) { 2166*0b57cec5SDimitry Andric if (Base) { 2167*0b57cec5SDimitry Andric // Set up the range. This range is relative to the entry point of the 2168*0b57cec5SDimitry Andric // compile unit. This is a hard coded 0 for low_pc when we're emitting 2169*0b57cec5SDimitry Andric // ranges, or the DW_AT_low_pc on the compile unit otherwise. 2170*0b57cec5SDimitry Andric if (IsLocLists) { 2171*0b57cec5SDimitry Andric Asm->OutStreamer->AddComment("DW_LLE_offset_pair"); 2172*0b57cec5SDimitry Andric Asm->OutStreamer->EmitIntValue(dwarf::DW_LLE_offset_pair, 1); 2173*0b57cec5SDimitry Andric Asm->OutStreamer->AddComment(" starting offset"); 2174*0b57cec5SDimitry Andric Asm->EmitLabelDifferenceAsULEB128(Entry.BeginSym, Base); 2175*0b57cec5SDimitry Andric Asm->OutStreamer->AddComment(" ending offset"); 2176*0b57cec5SDimitry Andric Asm->EmitLabelDifferenceAsULEB128(Entry.EndSym, Base); 2177*0b57cec5SDimitry Andric } else { 2178*0b57cec5SDimitry Andric Asm->EmitLabelDifference(Entry.BeginSym, Base, Size); 2179*0b57cec5SDimitry Andric Asm->EmitLabelDifference(Entry.EndSym, Base, Size); 2180*0b57cec5SDimitry Andric } 2181*0b57cec5SDimitry Andric 2182*0b57cec5SDimitry Andric emitDebugLocEntryLocation(Entry, CU); 2183*0b57cec5SDimitry Andric continue; 2184*0b57cec5SDimitry Andric } 2185*0b57cec5SDimitry Andric 2186*0b57cec5SDimitry Andric // We have no base address. 2187*0b57cec5SDimitry Andric if (IsLocLists) { 2188*0b57cec5SDimitry Andric // TODO: Use DW_LLE_base_addressx + DW_LLE_offset_pair, or 2189*0b57cec5SDimitry Andric // DW_LLE_startx_length in case if there is only a single range. 2190*0b57cec5SDimitry Andric // That should reduce the size of the debug data emited. 2191*0b57cec5SDimitry Andric // For now just use the DW_LLE_startx_length for all cases. 2192*0b57cec5SDimitry Andric Asm->OutStreamer->AddComment("DW_LLE_startx_length"); 2193*0b57cec5SDimitry Andric Asm->emitInt8(dwarf::DW_LLE_startx_length); 2194*0b57cec5SDimitry Andric Asm->OutStreamer->AddComment(" start idx"); 2195*0b57cec5SDimitry Andric Asm->EmitULEB128(AddrPool.getIndex(Entry.BeginSym)); 2196*0b57cec5SDimitry Andric Asm->OutStreamer->AddComment(" length"); 2197*0b57cec5SDimitry Andric Asm->EmitLabelDifferenceAsULEB128(Entry.EndSym, Entry.BeginSym); 2198*0b57cec5SDimitry Andric } else { 2199*0b57cec5SDimitry Andric Asm->OutStreamer->EmitSymbolValue(Entry.BeginSym, Size); 2200*0b57cec5SDimitry Andric Asm->OutStreamer->EmitSymbolValue(Entry.EndSym, Size); 2201*0b57cec5SDimitry Andric } 2202*0b57cec5SDimitry Andric 2203*0b57cec5SDimitry Andric emitDebugLocEntryLocation(Entry, CU); 2204*0b57cec5SDimitry Andric } 2205*0b57cec5SDimitry Andric 2206*0b57cec5SDimitry Andric if (IsLocLists) { 2207*0b57cec5SDimitry Andric // .debug_loclists section ends with DW_LLE_end_of_list. 2208*0b57cec5SDimitry Andric Asm->OutStreamer->AddComment("DW_LLE_end_of_list"); 2209*0b57cec5SDimitry Andric Asm->OutStreamer->EmitIntValue(dwarf::DW_LLE_end_of_list, 1); 2210*0b57cec5SDimitry Andric } else { 2211*0b57cec5SDimitry Andric // Terminate the .debug_loc list with two 0 values. 2212*0b57cec5SDimitry Andric Asm->OutStreamer->EmitIntValue(0, Size); 2213*0b57cec5SDimitry Andric Asm->OutStreamer->EmitIntValue(0, Size); 2214*0b57cec5SDimitry Andric } 2215*0b57cec5SDimitry Andric } 2216*0b57cec5SDimitry Andric 2217*0b57cec5SDimitry Andric if (TableEnd) 2218*0b57cec5SDimitry Andric Asm->OutStreamer->EmitLabel(TableEnd); 2219*0b57cec5SDimitry Andric } 2220*0b57cec5SDimitry Andric 2221*0b57cec5SDimitry Andric void DwarfDebug::emitDebugLocDWO() { 2222*0b57cec5SDimitry Andric for (const auto &List : DebugLocs.getLists()) { 2223*0b57cec5SDimitry Andric Asm->OutStreamer->SwitchSection( 2224*0b57cec5SDimitry Andric Asm->getObjFileLowering().getDwarfLocDWOSection()); 2225*0b57cec5SDimitry Andric Asm->OutStreamer->EmitLabel(List.Label); 2226*0b57cec5SDimitry Andric for (const auto &Entry : DebugLocs.getEntries(List)) { 2227*0b57cec5SDimitry Andric // GDB only supports startx_length in pre-standard split-DWARF. 2228*0b57cec5SDimitry Andric // (in v5 standard loclists, it currently* /only/ supports base_address + 2229*0b57cec5SDimitry Andric // offset_pair, so the implementations can't really share much since they 2230*0b57cec5SDimitry Andric // need to use different representations) 2231*0b57cec5SDimitry Andric // * as of October 2018, at least 2232*0b57cec5SDimitry Andric // Ideally/in v5, this could use SectionLabels to reuse existing addresses 2233*0b57cec5SDimitry Andric // in the address pool to minimize object size/relocations. 2234*0b57cec5SDimitry Andric Asm->emitInt8(dwarf::DW_LLE_startx_length); 2235*0b57cec5SDimitry Andric unsigned idx = AddrPool.getIndex(Entry.BeginSym); 2236*0b57cec5SDimitry Andric Asm->EmitULEB128(idx); 2237*0b57cec5SDimitry Andric Asm->EmitLabelDifference(Entry.EndSym, Entry.BeginSym, 4); 2238*0b57cec5SDimitry Andric 2239*0b57cec5SDimitry Andric emitDebugLocEntryLocation(Entry, List.CU); 2240*0b57cec5SDimitry Andric } 2241*0b57cec5SDimitry Andric Asm->emitInt8(dwarf::DW_LLE_end_of_list); 2242*0b57cec5SDimitry Andric } 2243*0b57cec5SDimitry Andric } 2244*0b57cec5SDimitry Andric 2245*0b57cec5SDimitry Andric struct ArangeSpan { 2246*0b57cec5SDimitry Andric const MCSymbol *Start, *End; 2247*0b57cec5SDimitry Andric }; 2248*0b57cec5SDimitry Andric 2249*0b57cec5SDimitry Andric // Emit a debug aranges section, containing a CU lookup for any 2250*0b57cec5SDimitry Andric // address we can tie back to a CU. 2251*0b57cec5SDimitry Andric void DwarfDebug::emitDebugARanges() { 2252*0b57cec5SDimitry Andric // Provides a unique id per text section. 2253*0b57cec5SDimitry Andric MapVector<MCSection *, SmallVector<SymbolCU, 8>> SectionMap; 2254*0b57cec5SDimitry Andric 2255*0b57cec5SDimitry Andric // Filter labels by section. 2256*0b57cec5SDimitry Andric for (const SymbolCU &SCU : ArangeLabels) { 2257*0b57cec5SDimitry Andric if (SCU.Sym->isInSection()) { 2258*0b57cec5SDimitry Andric // Make a note of this symbol and it's section. 2259*0b57cec5SDimitry Andric MCSection *Section = &SCU.Sym->getSection(); 2260*0b57cec5SDimitry Andric if (!Section->getKind().isMetadata()) 2261*0b57cec5SDimitry Andric SectionMap[Section].push_back(SCU); 2262*0b57cec5SDimitry Andric } else { 2263*0b57cec5SDimitry Andric // Some symbols (e.g. common/bss on mach-o) can have no section but still 2264*0b57cec5SDimitry Andric // appear in the output. This sucks as we rely on sections to build 2265*0b57cec5SDimitry Andric // arange spans. We can do it without, but it's icky. 2266*0b57cec5SDimitry Andric SectionMap[nullptr].push_back(SCU); 2267*0b57cec5SDimitry Andric } 2268*0b57cec5SDimitry Andric } 2269*0b57cec5SDimitry Andric 2270*0b57cec5SDimitry Andric DenseMap<DwarfCompileUnit *, std::vector<ArangeSpan>> Spans; 2271*0b57cec5SDimitry Andric 2272*0b57cec5SDimitry Andric for (auto &I : SectionMap) { 2273*0b57cec5SDimitry Andric MCSection *Section = I.first; 2274*0b57cec5SDimitry Andric SmallVector<SymbolCU, 8> &List = I.second; 2275*0b57cec5SDimitry Andric if (List.size() < 1) 2276*0b57cec5SDimitry Andric continue; 2277*0b57cec5SDimitry Andric 2278*0b57cec5SDimitry Andric // If we have no section (e.g. common), just write out 2279*0b57cec5SDimitry Andric // individual spans for each symbol. 2280*0b57cec5SDimitry Andric if (!Section) { 2281*0b57cec5SDimitry Andric for (const SymbolCU &Cur : List) { 2282*0b57cec5SDimitry Andric ArangeSpan Span; 2283*0b57cec5SDimitry Andric Span.Start = Cur.Sym; 2284*0b57cec5SDimitry Andric Span.End = nullptr; 2285*0b57cec5SDimitry Andric assert(Cur.CU); 2286*0b57cec5SDimitry Andric Spans[Cur.CU].push_back(Span); 2287*0b57cec5SDimitry Andric } 2288*0b57cec5SDimitry Andric continue; 2289*0b57cec5SDimitry Andric } 2290*0b57cec5SDimitry Andric 2291*0b57cec5SDimitry Andric // Sort the symbols by offset within the section. 2292*0b57cec5SDimitry Andric llvm::stable_sort(List, [&](const SymbolCU &A, const SymbolCU &B) { 2293*0b57cec5SDimitry Andric unsigned IA = A.Sym ? Asm->OutStreamer->GetSymbolOrder(A.Sym) : 0; 2294*0b57cec5SDimitry Andric unsigned IB = B.Sym ? Asm->OutStreamer->GetSymbolOrder(B.Sym) : 0; 2295*0b57cec5SDimitry Andric 2296*0b57cec5SDimitry Andric // Symbols with no order assigned should be placed at the end. 2297*0b57cec5SDimitry Andric // (e.g. section end labels) 2298*0b57cec5SDimitry Andric if (IA == 0) 2299*0b57cec5SDimitry Andric return false; 2300*0b57cec5SDimitry Andric if (IB == 0) 2301*0b57cec5SDimitry Andric return true; 2302*0b57cec5SDimitry Andric return IA < IB; 2303*0b57cec5SDimitry Andric }); 2304*0b57cec5SDimitry Andric 2305*0b57cec5SDimitry Andric // Insert a final terminator. 2306*0b57cec5SDimitry Andric List.push_back(SymbolCU(nullptr, Asm->OutStreamer->endSection(Section))); 2307*0b57cec5SDimitry Andric 2308*0b57cec5SDimitry Andric // Build spans between each label. 2309*0b57cec5SDimitry Andric const MCSymbol *StartSym = List[0].Sym; 2310*0b57cec5SDimitry Andric for (size_t n = 1, e = List.size(); n < e; n++) { 2311*0b57cec5SDimitry Andric const SymbolCU &Prev = List[n - 1]; 2312*0b57cec5SDimitry Andric const SymbolCU &Cur = List[n]; 2313*0b57cec5SDimitry Andric 2314*0b57cec5SDimitry Andric // Try and build the longest span we can within the same CU. 2315*0b57cec5SDimitry Andric if (Cur.CU != Prev.CU) { 2316*0b57cec5SDimitry Andric ArangeSpan Span; 2317*0b57cec5SDimitry Andric Span.Start = StartSym; 2318*0b57cec5SDimitry Andric Span.End = Cur.Sym; 2319*0b57cec5SDimitry Andric assert(Prev.CU); 2320*0b57cec5SDimitry Andric Spans[Prev.CU].push_back(Span); 2321*0b57cec5SDimitry Andric StartSym = Cur.Sym; 2322*0b57cec5SDimitry Andric } 2323*0b57cec5SDimitry Andric } 2324*0b57cec5SDimitry Andric } 2325*0b57cec5SDimitry Andric 2326*0b57cec5SDimitry Andric // Start the dwarf aranges section. 2327*0b57cec5SDimitry Andric Asm->OutStreamer->SwitchSection( 2328*0b57cec5SDimitry Andric Asm->getObjFileLowering().getDwarfARangesSection()); 2329*0b57cec5SDimitry Andric 2330*0b57cec5SDimitry Andric unsigned PtrSize = Asm->MAI->getCodePointerSize(); 2331*0b57cec5SDimitry Andric 2332*0b57cec5SDimitry Andric // Build a list of CUs used. 2333*0b57cec5SDimitry Andric std::vector<DwarfCompileUnit *> CUs; 2334*0b57cec5SDimitry Andric for (const auto &it : Spans) { 2335*0b57cec5SDimitry Andric DwarfCompileUnit *CU = it.first; 2336*0b57cec5SDimitry Andric CUs.push_back(CU); 2337*0b57cec5SDimitry Andric } 2338*0b57cec5SDimitry Andric 2339*0b57cec5SDimitry Andric // Sort the CU list (again, to ensure consistent output order). 2340*0b57cec5SDimitry Andric llvm::sort(CUs, [](const DwarfCompileUnit *A, const DwarfCompileUnit *B) { 2341*0b57cec5SDimitry Andric return A->getUniqueID() < B->getUniqueID(); 2342*0b57cec5SDimitry Andric }); 2343*0b57cec5SDimitry Andric 2344*0b57cec5SDimitry Andric // Emit an arange table for each CU we used. 2345*0b57cec5SDimitry Andric for (DwarfCompileUnit *CU : CUs) { 2346*0b57cec5SDimitry Andric std::vector<ArangeSpan> &List = Spans[CU]; 2347*0b57cec5SDimitry Andric 2348*0b57cec5SDimitry Andric // Describe the skeleton CU's offset and length, not the dwo file's. 2349*0b57cec5SDimitry Andric if (auto *Skel = CU->getSkeleton()) 2350*0b57cec5SDimitry Andric CU = Skel; 2351*0b57cec5SDimitry Andric 2352*0b57cec5SDimitry Andric // Emit size of content not including length itself. 2353*0b57cec5SDimitry Andric unsigned ContentSize = 2354*0b57cec5SDimitry Andric sizeof(int16_t) + // DWARF ARange version number 2355*0b57cec5SDimitry Andric sizeof(int32_t) + // Offset of CU in the .debug_info section 2356*0b57cec5SDimitry Andric sizeof(int8_t) + // Pointer Size (in bytes) 2357*0b57cec5SDimitry Andric sizeof(int8_t); // Segment Size (in bytes) 2358*0b57cec5SDimitry Andric 2359*0b57cec5SDimitry Andric unsigned TupleSize = PtrSize * 2; 2360*0b57cec5SDimitry Andric 2361*0b57cec5SDimitry Andric // 7.20 in the Dwarf specs requires the table to be aligned to a tuple. 2362*0b57cec5SDimitry Andric unsigned Padding = 2363*0b57cec5SDimitry Andric OffsetToAlignment(sizeof(int32_t) + ContentSize, TupleSize); 2364*0b57cec5SDimitry Andric 2365*0b57cec5SDimitry Andric ContentSize += Padding; 2366*0b57cec5SDimitry Andric ContentSize += (List.size() + 1) * TupleSize; 2367*0b57cec5SDimitry Andric 2368*0b57cec5SDimitry Andric // For each compile unit, write the list of spans it covers. 2369*0b57cec5SDimitry Andric Asm->OutStreamer->AddComment("Length of ARange Set"); 2370*0b57cec5SDimitry Andric Asm->emitInt32(ContentSize); 2371*0b57cec5SDimitry Andric Asm->OutStreamer->AddComment("DWARF Arange version number"); 2372*0b57cec5SDimitry Andric Asm->emitInt16(dwarf::DW_ARANGES_VERSION); 2373*0b57cec5SDimitry Andric Asm->OutStreamer->AddComment("Offset Into Debug Info Section"); 2374*0b57cec5SDimitry Andric emitSectionReference(*CU); 2375*0b57cec5SDimitry Andric Asm->OutStreamer->AddComment("Address Size (in bytes)"); 2376*0b57cec5SDimitry Andric Asm->emitInt8(PtrSize); 2377*0b57cec5SDimitry Andric Asm->OutStreamer->AddComment("Segment Size (in bytes)"); 2378*0b57cec5SDimitry Andric Asm->emitInt8(0); 2379*0b57cec5SDimitry Andric 2380*0b57cec5SDimitry Andric Asm->OutStreamer->emitFill(Padding, 0xff); 2381*0b57cec5SDimitry Andric 2382*0b57cec5SDimitry Andric for (const ArangeSpan &Span : List) { 2383*0b57cec5SDimitry Andric Asm->EmitLabelReference(Span.Start, PtrSize); 2384*0b57cec5SDimitry Andric 2385*0b57cec5SDimitry Andric // Calculate the size as being from the span start to it's end. 2386*0b57cec5SDimitry Andric if (Span.End) { 2387*0b57cec5SDimitry Andric Asm->EmitLabelDifference(Span.End, Span.Start, PtrSize); 2388*0b57cec5SDimitry Andric } else { 2389*0b57cec5SDimitry Andric // For symbols without an end marker (e.g. common), we 2390*0b57cec5SDimitry Andric // write a single arange entry containing just that one symbol. 2391*0b57cec5SDimitry Andric uint64_t Size = SymSize[Span.Start]; 2392*0b57cec5SDimitry Andric if (Size == 0) 2393*0b57cec5SDimitry Andric Size = 1; 2394*0b57cec5SDimitry Andric 2395*0b57cec5SDimitry Andric Asm->OutStreamer->EmitIntValue(Size, PtrSize); 2396*0b57cec5SDimitry Andric } 2397*0b57cec5SDimitry Andric } 2398*0b57cec5SDimitry Andric 2399*0b57cec5SDimitry Andric Asm->OutStreamer->AddComment("ARange terminator"); 2400*0b57cec5SDimitry Andric Asm->OutStreamer->EmitIntValue(0, PtrSize); 2401*0b57cec5SDimitry Andric Asm->OutStreamer->EmitIntValue(0, PtrSize); 2402*0b57cec5SDimitry Andric } 2403*0b57cec5SDimitry Andric } 2404*0b57cec5SDimitry Andric 2405*0b57cec5SDimitry Andric /// Emit a single range list. We handle both DWARF v5 and earlier. 2406*0b57cec5SDimitry Andric static void emitRangeList(DwarfDebug &DD, AsmPrinter *Asm, 2407*0b57cec5SDimitry Andric const RangeSpanList &List) { 2408*0b57cec5SDimitry Andric 2409*0b57cec5SDimitry Andric auto DwarfVersion = DD.getDwarfVersion(); 2410*0b57cec5SDimitry Andric // Emit our symbol so we can find the beginning of the range. 2411*0b57cec5SDimitry Andric Asm->OutStreamer->EmitLabel(List.getSym()); 2412*0b57cec5SDimitry Andric // Gather all the ranges that apply to the same section so they can share 2413*0b57cec5SDimitry Andric // a base address entry. 2414*0b57cec5SDimitry Andric MapVector<const MCSection *, std::vector<const RangeSpan *>> SectionRanges; 2415*0b57cec5SDimitry Andric // Size for our labels. 2416*0b57cec5SDimitry Andric auto Size = Asm->MAI->getCodePointerSize(); 2417*0b57cec5SDimitry Andric 2418*0b57cec5SDimitry Andric for (const RangeSpan &Range : List.getRanges()) 2419*0b57cec5SDimitry Andric SectionRanges[&Range.getStart()->getSection()].push_back(&Range); 2420*0b57cec5SDimitry Andric 2421*0b57cec5SDimitry Andric const DwarfCompileUnit &CU = List.getCU(); 2422*0b57cec5SDimitry Andric const MCSymbol *CUBase = CU.getBaseAddress(); 2423*0b57cec5SDimitry Andric bool BaseIsSet = false; 2424*0b57cec5SDimitry Andric for (const auto &P : SectionRanges) { 2425*0b57cec5SDimitry Andric // Don't bother with a base address entry if there's only one range in 2426*0b57cec5SDimitry Andric // this section in this range list - for example ranges for a CU will 2427*0b57cec5SDimitry Andric // usually consist of single regions from each of many sections 2428*0b57cec5SDimitry Andric // (-ffunction-sections, or just C++ inline functions) except under LTO 2429*0b57cec5SDimitry Andric // or optnone where there may be holes in a single CU's section 2430*0b57cec5SDimitry Andric // contributions. 2431*0b57cec5SDimitry Andric auto *Base = CUBase; 2432*0b57cec5SDimitry Andric if (!Base && (P.second.size() > 1 || DwarfVersion < 5) && 2433*0b57cec5SDimitry Andric (CU.getCUNode()->getRangesBaseAddress() || DwarfVersion >= 5)) { 2434*0b57cec5SDimitry Andric BaseIsSet = true; 2435*0b57cec5SDimitry Andric // FIXME/use care: This may not be a useful base address if it's not 2436*0b57cec5SDimitry Andric // the lowest address/range in this object. 2437*0b57cec5SDimitry Andric Base = P.second.front()->getStart(); 2438*0b57cec5SDimitry Andric if (DwarfVersion >= 5) { 2439*0b57cec5SDimitry Andric Base = DD.getSectionLabel(&Base->getSection()); 2440*0b57cec5SDimitry Andric Asm->OutStreamer->AddComment("DW_RLE_base_addressx"); 2441*0b57cec5SDimitry Andric Asm->OutStreamer->EmitIntValue(dwarf::DW_RLE_base_addressx, 1); 2442*0b57cec5SDimitry Andric Asm->OutStreamer->AddComment(" base address index"); 2443*0b57cec5SDimitry Andric Asm->EmitULEB128(DD.getAddressPool().getIndex(Base)); 2444*0b57cec5SDimitry Andric } else { 2445*0b57cec5SDimitry Andric Asm->OutStreamer->EmitIntValue(-1, Size); 2446*0b57cec5SDimitry Andric Asm->OutStreamer->AddComment(" base address"); 2447*0b57cec5SDimitry Andric Asm->OutStreamer->EmitSymbolValue(Base, Size); 2448*0b57cec5SDimitry Andric } 2449*0b57cec5SDimitry Andric } else if (BaseIsSet && DwarfVersion < 5) { 2450*0b57cec5SDimitry Andric BaseIsSet = false; 2451*0b57cec5SDimitry Andric assert(!Base); 2452*0b57cec5SDimitry Andric Asm->OutStreamer->EmitIntValue(-1, Size); 2453*0b57cec5SDimitry Andric Asm->OutStreamer->EmitIntValue(0, Size); 2454*0b57cec5SDimitry Andric } 2455*0b57cec5SDimitry Andric 2456*0b57cec5SDimitry Andric for (const auto *RS : P.second) { 2457*0b57cec5SDimitry Andric const MCSymbol *Begin = RS->getStart(); 2458*0b57cec5SDimitry Andric const MCSymbol *End = RS->getEnd(); 2459*0b57cec5SDimitry Andric assert(Begin && "Range without a begin symbol?"); 2460*0b57cec5SDimitry Andric assert(End && "Range without an end symbol?"); 2461*0b57cec5SDimitry Andric if (Base) { 2462*0b57cec5SDimitry Andric if (DwarfVersion >= 5) { 2463*0b57cec5SDimitry Andric // Emit DW_RLE_offset_pair when we have a base. 2464*0b57cec5SDimitry Andric Asm->OutStreamer->AddComment("DW_RLE_offset_pair"); 2465*0b57cec5SDimitry Andric Asm->OutStreamer->EmitIntValue(dwarf::DW_RLE_offset_pair, 1); 2466*0b57cec5SDimitry Andric Asm->OutStreamer->AddComment(" starting offset"); 2467*0b57cec5SDimitry Andric Asm->EmitLabelDifferenceAsULEB128(Begin, Base); 2468*0b57cec5SDimitry Andric Asm->OutStreamer->AddComment(" ending offset"); 2469*0b57cec5SDimitry Andric Asm->EmitLabelDifferenceAsULEB128(End, Base); 2470*0b57cec5SDimitry Andric } else { 2471*0b57cec5SDimitry Andric Asm->EmitLabelDifference(Begin, Base, Size); 2472*0b57cec5SDimitry Andric Asm->EmitLabelDifference(End, Base, Size); 2473*0b57cec5SDimitry Andric } 2474*0b57cec5SDimitry Andric } else if (DwarfVersion >= 5) { 2475*0b57cec5SDimitry Andric Asm->OutStreamer->AddComment("DW_RLE_startx_length"); 2476*0b57cec5SDimitry Andric Asm->OutStreamer->EmitIntValue(dwarf::DW_RLE_startx_length, 1); 2477*0b57cec5SDimitry Andric Asm->OutStreamer->AddComment(" start index"); 2478*0b57cec5SDimitry Andric Asm->EmitULEB128(DD.getAddressPool().getIndex(Begin)); 2479*0b57cec5SDimitry Andric Asm->OutStreamer->AddComment(" length"); 2480*0b57cec5SDimitry Andric Asm->EmitLabelDifferenceAsULEB128(End, Begin); 2481*0b57cec5SDimitry Andric } else { 2482*0b57cec5SDimitry Andric Asm->OutStreamer->EmitSymbolValue(Begin, Size); 2483*0b57cec5SDimitry Andric Asm->OutStreamer->EmitSymbolValue(End, Size); 2484*0b57cec5SDimitry Andric } 2485*0b57cec5SDimitry Andric } 2486*0b57cec5SDimitry Andric } 2487*0b57cec5SDimitry Andric if (DwarfVersion >= 5) { 2488*0b57cec5SDimitry Andric Asm->OutStreamer->AddComment("DW_RLE_end_of_list"); 2489*0b57cec5SDimitry Andric Asm->OutStreamer->EmitIntValue(dwarf::DW_RLE_end_of_list, 1); 2490*0b57cec5SDimitry Andric } else { 2491*0b57cec5SDimitry Andric // Terminate the list with two 0 values. 2492*0b57cec5SDimitry Andric Asm->OutStreamer->EmitIntValue(0, Size); 2493*0b57cec5SDimitry Andric Asm->OutStreamer->EmitIntValue(0, Size); 2494*0b57cec5SDimitry Andric } 2495*0b57cec5SDimitry Andric } 2496*0b57cec5SDimitry Andric 2497*0b57cec5SDimitry Andric static void emitDebugRangesImpl(DwarfDebug &DD, AsmPrinter *Asm, 2498*0b57cec5SDimitry Andric const DwarfFile &Holder, MCSymbol *TableEnd) { 2499*0b57cec5SDimitry Andric for (const RangeSpanList &List : Holder.getRangeLists()) 2500*0b57cec5SDimitry Andric emitRangeList(DD, Asm, List); 2501*0b57cec5SDimitry Andric 2502*0b57cec5SDimitry Andric if (TableEnd) 2503*0b57cec5SDimitry Andric Asm->OutStreamer->EmitLabel(TableEnd); 2504*0b57cec5SDimitry Andric } 2505*0b57cec5SDimitry Andric 2506*0b57cec5SDimitry Andric /// Emit address ranges into the .debug_ranges section or into the DWARF v5 2507*0b57cec5SDimitry Andric /// .debug_rnglists section. 2508*0b57cec5SDimitry Andric void DwarfDebug::emitDebugRanges() { 2509*0b57cec5SDimitry Andric if (CUMap.empty()) 2510*0b57cec5SDimitry Andric return; 2511*0b57cec5SDimitry Andric 2512*0b57cec5SDimitry Andric const auto &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder; 2513*0b57cec5SDimitry Andric 2514*0b57cec5SDimitry Andric if (Holder.getRangeLists().empty()) 2515*0b57cec5SDimitry Andric return; 2516*0b57cec5SDimitry Andric 2517*0b57cec5SDimitry Andric assert(useRangesSection()); 2518*0b57cec5SDimitry Andric assert(llvm::none_of(CUMap, [](const decltype(CUMap)::value_type &Pair) { 2519*0b57cec5SDimitry Andric return Pair.second->getCUNode()->isDebugDirectivesOnly(); 2520*0b57cec5SDimitry Andric })); 2521*0b57cec5SDimitry Andric 2522*0b57cec5SDimitry Andric // Start the dwarf ranges section. 2523*0b57cec5SDimitry Andric MCSymbol *TableEnd = nullptr; 2524*0b57cec5SDimitry Andric if (getDwarfVersion() >= 5) { 2525*0b57cec5SDimitry Andric Asm->OutStreamer->SwitchSection( 2526*0b57cec5SDimitry Andric Asm->getObjFileLowering().getDwarfRnglistsSection()); 2527*0b57cec5SDimitry Andric TableEnd = emitRnglistsTableHeader(Asm, Holder); 2528*0b57cec5SDimitry Andric } else 2529*0b57cec5SDimitry Andric Asm->OutStreamer->SwitchSection( 2530*0b57cec5SDimitry Andric Asm->getObjFileLowering().getDwarfRangesSection()); 2531*0b57cec5SDimitry Andric 2532*0b57cec5SDimitry Andric emitDebugRangesImpl(*this, Asm, Holder, TableEnd); 2533*0b57cec5SDimitry Andric } 2534*0b57cec5SDimitry Andric 2535*0b57cec5SDimitry Andric void DwarfDebug::emitDebugRangesDWO() { 2536*0b57cec5SDimitry Andric assert(useSplitDwarf()); 2537*0b57cec5SDimitry Andric 2538*0b57cec5SDimitry Andric if (CUMap.empty()) 2539*0b57cec5SDimitry Andric return; 2540*0b57cec5SDimitry Andric 2541*0b57cec5SDimitry Andric const auto &Holder = InfoHolder; 2542*0b57cec5SDimitry Andric 2543*0b57cec5SDimitry Andric if (Holder.getRangeLists().empty()) 2544*0b57cec5SDimitry Andric return; 2545*0b57cec5SDimitry Andric 2546*0b57cec5SDimitry Andric assert(getDwarfVersion() >= 5); 2547*0b57cec5SDimitry Andric assert(useRangesSection()); 2548*0b57cec5SDimitry Andric assert(llvm::none_of(CUMap, [](const decltype(CUMap)::value_type &Pair) { 2549*0b57cec5SDimitry Andric return Pair.second->getCUNode()->isDebugDirectivesOnly(); 2550*0b57cec5SDimitry Andric })); 2551*0b57cec5SDimitry Andric 2552*0b57cec5SDimitry Andric // Start the dwarf ranges section. 2553*0b57cec5SDimitry Andric Asm->OutStreamer->SwitchSection( 2554*0b57cec5SDimitry Andric Asm->getObjFileLowering().getDwarfRnglistsDWOSection()); 2555*0b57cec5SDimitry Andric MCSymbol *TableEnd = emitRnglistsTableHeader(Asm, Holder); 2556*0b57cec5SDimitry Andric 2557*0b57cec5SDimitry Andric emitDebugRangesImpl(*this, Asm, Holder, TableEnd); 2558*0b57cec5SDimitry Andric } 2559*0b57cec5SDimitry Andric 2560*0b57cec5SDimitry Andric void DwarfDebug::handleMacroNodes(DIMacroNodeArray Nodes, DwarfCompileUnit &U) { 2561*0b57cec5SDimitry Andric for (auto *MN : Nodes) { 2562*0b57cec5SDimitry Andric if (auto *M = dyn_cast<DIMacro>(MN)) 2563*0b57cec5SDimitry Andric emitMacro(*M); 2564*0b57cec5SDimitry Andric else if (auto *F = dyn_cast<DIMacroFile>(MN)) 2565*0b57cec5SDimitry Andric emitMacroFile(*F, U); 2566*0b57cec5SDimitry Andric else 2567*0b57cec5SDimitry Andric llvm_unreachable("Unexpected DI type!"); 2568*0b57cec5SDimitry Andric } 2569*0b57cec5SDimitry Andric } 2570*0b57cec5SDimitry Andric 2571*0b57cec5SDimitry Andric void DwarfDebug::emitMacro(DIMacro &M) { 2572*0b57cec5SDimitry Andric Asm->EmitULEB128(M.getMacinfoType()); 2573*0b57cec5SDimitry Andric Asm->EmitULEB128(M.getLine()); 2574*0b57cec5SDimitry Andric StringRef Name = M.getName(); 2575*0b57cec5SDimitry Andric StringRef Value = M.getValue(); 2576*0b57cec5SDimitry Andric Asm->OutStreamer->EmitBytes(Name); 2577*0b57cec5SDimitry Andric if (!Value.empty()) { 2578*0b57cec5SDimitry Andric // There should be one space between macro name and macro value. 2579*0b57cec5SDimitry Andric Asm->emitInt8(' '); 2580*0b57cec5SDimitry Andric Asm->OutStreamer->EmitBytes(Value); 2581*0b57cec5SDimitry Andric } 2582*0b57cec5SDimitry Andric Asm->emitInt8('\0'); 2583*0b57cec5SDimitry Andric } 2584*0b57cec5SDimitry Andric 2585*0b57cec5SDimitry Andric void DwarfDebug::emitMacroFile(DIMacroFile &F, DwarfCompileUnit &U) { 2586*0b57cec5SDimitry Andric assert(F.getMacinfoType() == dwarf::DW_MACINFO_start_file); 2587*0b57cec5SDimitry Andric Asm->EmitULEB128(dwarf::DW_MACINFO_start_file); 2588*0b57cec5SDimitry Andric Asm->EmitULEB128(F.getLine()); 2589*0b57cec5SDimitry Andric Asm->EmitULEB128(U.getOrCreateSourceID(F.getFile())); 2590*0b57cec5SDimitry Andric handleMacroNodes(F.getElements(), U); 2591*0b57cec5SDimitry Andric Asm->EmitULEB128(dwarf::DW_MACINFO_end_file); 2592*0b57cec5SDimitry Andric } 2593*0b57cec5SDimitry Andric 2594*0b57cec5SDimitry Andric /// Emit macros into a debug macinfo section. 2595*0b57cec5SDimitry Andric void DwarfDebug::emitDebugMacinfo() { 2596*0b57cec5SDimitry Andric if (CUMap.empty()) 2597*0b57cec5SDimitry Andric return; 2598*0b57cec5SDimitry Andric 2599*0b57cec5SDimitry Andric if (llvm::all_of(CUMap, [](const decltype(CUMap)::value_type &Pair) { 2600*0b57cec5SDimitry Andric return Pair.second->getCUNode()->isDebugDirectivesOnly(); 2601*0b57cec5SDimitry Andric })) 2602*0b57cec5SDimitry Andric return; 2603*0b57cec5SDimitry Andric 2604*0b57cec5SDimitry Andric // Start the dwarf macinfo section. 2605*0b57cec5SDimitry Andric Asm->OutStreamer->SwitchSection( 2606*0b57cec5SDimitry Andric Asm->getObjFileLowering().getDwarfMacinfoSection()); 2607*0b57cec5SDimitry Andric 2608*0b57cec5SDimitry Andric for (const auto &P : CUMap) { 2609*0b57cec5SDimitry Andric auto &TheCU = *P.second; 2610*0b57cec5SDimitry Andric if (TheCU.getCUNode()->isDebugDirectivesOnly()) 2611*0b57cec5SDimitry Andric continue; 2612*0b57cec5SDimitry Andric auto *SkCU = TheCU.getSkeleton(); 2613*0b57cec5SDimitry Andric DwarfCompileUnit &U = SkCU ? *SkCU : TheCU; 2614*0b57cec5SDimitry Andric auto *CUNode = cast<DICompileUnit>(P.first); 2615*0b57cec5SDimitry Andric DIMacroNodeArray Macros = CUNode->getMacros(); 2616*0b57cec5SDimitry Andric if (!Macros.empty()) { 2617*0b57cec5SDimitry Andric Asm->OutStreamer->EmitLabel(U.getMacroLabelBegin()); 2618*0b57cec5SDimitry Andric handleMacroNodes(Macros, U); 2619*0b57cec5SDimitry Andric } 2620*0b57cec5SDimitry Andric } 2621*0b57cec5SDimitry Andric Asm->OutStreamer->AddComment("End Of Macro List Mark"); 2622*0b57cec5SDimitry Andric Asm->emitInt8(0); 2623*0b57cec5SDimitry Andric } 2624*0b57cec5SDimitry Andric 2625*0b57cec5SDimitry Andric // DWARF5 Experimental Separate Dwarf emitters. 2626*0b57cec5SDimitry Andric 2627*0b57cec5SDimitry Andric void DwarfDebug::initSkeletonUnit(const DwarfUnit &U, DIE &Die, 2628*0b57cec5SDimitry Andric std::unique_ptr<DwarfCompileUnit> NewU) { 2629*0b57cec5SDimitry Andric 2630*0b57cec5SDimitry Andric if (!CompilationDir.empty()) 2631*0b57cec5SDimitry Andric NewU->addString(Die, dwarf::DW_AT_comp_dir, CompilationDir); 2632*0b57cec5SDimitry Andric 2633*0b57cec5SDimitry Andric addGnuPubAttributes(*NewU, Die); 2634*0b57cec5SDimitry Andric 2635*0b57cec5SDimitry Andric SkeletonHolder.addUnit(std::move(NewU)); 2636*0b57cec5SDimitry Andric } 2637*0b57cec5SDimitry Andric 2638*0b57cec5SDimitry Andric DwarfCompileUnit &DwarfDebug::constructSkeletonCU(const DwarfCompileUnit &CU) { 2639*0b57cec5SDimitry Andric 2640*0b57cec5SDimitry Andric auto OwnedUnit = llvm::make_unique<DwarfCompileUnit>( 2641*0b57cec5SDimitry Andric CU.getUniqueID(), CU.getCUNode(), Asm, this, &SkeletonHolder); 2642*0b57cec5SDimitry Andric DwarfCompileUnit &NewCU = *OwnedUnit; 2643*0b57cec5SDimitry Andric NewCU.setSection(Asm->getObjFileLowering().getDwarfInfoSection()); 2644*0b57cec5SDimitry Andric 2645*0b57cec5SDimitry Andric NewCU.initStmtList(); 2646*0b57cec5SDimitry Andric 2647*0b57cec5SDimitry Andric if (useSegmentedStringOffsetsTable()) 2648*0b57cec5SDimitry Andric NewCU.addStringOffsetsStart(); 2649*0b57cec5SDimitry Andric 2650*0b57cec5SDimitry Andric initSkeletonUnit(CU, NewCU.getUnitDie(), std::move(OwnedUnit)); 2651*0b57cec5SDimitry Andric 2652*0b57cec5SDimitry Andric return NewCU; 2653*0b57cec5SDimitry Andric } 2654*0b57cec5SDimitry Andric 2655*0b57cec5SDimitry Andric // Emit the .debug_info.dwo section for separated dwarf. This contains the 2656*0b57cec5SDimitry Andric // compile units that would normally be in debug_info. 2657*0b57cec5SDimitry Andric void DwarfDebug::emitDebugInfoDWO() { 2658*0b57cec5SDimitry Andric assert(useSplitDwarf() && "No split dwarf debug info?"); 2659*0b57cec5SDimitry Andric // Don't emit relocations into the dwo file. 2660*0b57cec5SDimitry Andric InfoHolder.emitUnits(/* UseOffsets */ true); 2661*0b57cec5SDimitry Andric } 2662*0b57cec5SDimitry Andric 2663*0b57cec5SDimitry Andric // Emit the .debug_abbrev.dwo section for separated dwarf. This contains the 2664*0b57cec5SDimitry Andric // abbreviations for the .debug_info.dwo section. 2665*0b57cec5SDimitry Andric void DwarfDebug::emitDebugAbbrevDWO() { 2666*0b57cec5SDimitry Andric assert(useSplitDwarf() && "No split dwarf?"); 2667*0b57cec5SDimitry Andric InfoHolder.emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevDWOSection()); 2668*0b57cec5SDimitry Andric } 2669*0b57cec5SDimitry Andric 2670*0b57cec5SDimitry Andric void DwarfDebug::emitDebugLineDWO() { 2671*0b57cec5SDimitry Andric assert(useSplitDwarf() && "No split dwarf?"); 2672*0b57cec5SDimitry Andric SplitTypeUnitFileTable.Emit( 2673*0b57cec5SDimitry Andric *Asm->OutStreamer, MCDwarfLineTableParams(), 2674*0b57cec5SDimitry Andric Asm->getObjFileLowering().getDwarfLineDWOSection()); 2675*0b57cec5SDimitry Andric } 2676*0b57cec5SDimitry Andric 2677*0b57cec5SDimitry Andric void DwarfDebug::emitStringOffsetsTableHeaderDWO() { 2678*0b57cec5SDimitry Andric assert(useSplitDwarf() && "No split dwarf?"); 2679*0b57cec5SDimitry Andric InfoHolder.getStringPool().emitStringOffsetsTableHeader( 2680*0b57cec5SDimitry Andric *Asm, Asm->getObjFileLowering().getDwarfStrOffDWOSection(), 2681*0b57cec5SDimitry Andric InfoHolder.getStringOffsetsStartSym()); 2682*0b57cec5SDimitry Andric } 2683*0b57cec5SDimitry Andric 2684*0b57cec5SDimitry Andric // Emit the .debug_str.dwo section for separated dwarf. This contains the 2685*0b57cec5SDimitry Andric // string section and is identical in format to traditional .debug_str 2686*0b57cec5SDimitry Andric // sections. 2687*0b57cec5SDimitry Andric void DwarfDebug::emitDebugStrDWO() { 2688*0b57cec5SDimitry Andric if (useSegmentedStringOffsetsTable()) 2689*0b57cec5SDimitry Andric emitStringOffsetsTableHeaderDWO(); 2690*0b57cec5SDimitry Andric assert(useSplitDwarf() && "No split dwarf?"); 2691*0b57cec5SDimitry Andric MCSection *OffSec = Asm->getObjFileLowering().getDwarfStrOffDWOSection(); 2692*0b57cec5SDimitry Andric InfoHolder.emitStrings(Asm->getObjFileLowering().getDwarfStrDWOSection(), 2693*0b57cec5SDimitry Andric OffSec, /* UseRelativeOffsets = */ false); 2694*0b57cec5SDimitry Andric } 2695*0b57cec5SDimitry Andric 2696*0b57cec5SDimitry Andric // Emit address pool. 2697*0b57cec5SDimitry Andric void DwarfDebug::emitDebugAddr() { 2698*0b57cec5SDimitry Andric AddrPool.emit(*Asm, Asm->getObjFileLowering().getDwarfAddrSection()); 2699*0b57cec5SDimitry Andric } 2700*0b57cec5SDimitry Andric 2701*0b57cec5SDimitry Andric MCDwarfDwoLineTable *DwarfDebug::getDwoLineTable(const DwarfCompileUnit &CU) { 2702*0b57cec5SDimitry Andric if (!useSplitDwarf()) 2703*0b57cec5SDimitry Andric return nullptr; 2704*0b57cec5SDimitry Andric const DICompileUnit *DIUnit = CU.getCUNode(); 2705*0b57cec5SDimitry Andric SplitTypeUnitFileTable.maybeSetRootFile( 2706*0b57cec5SDimitry Andric DIUnit->getDirectory(), DIUnit->getFilename(), 2707*0b57cec5SDimitry Andric CU.getMD5AsBytes(DIUnit->getFile()), DIUnit->getSource()); 2708*0b57cec5SDimitry Andric return &SplitTypeUnitFileTable; 2709*0b57cec5SDimitry Andric } 2710*0b57cec5SDimitry Andric 2711*0b57cec5SDimitry Andric uint64_t DwarfDebug::makeTypeSignature(StringRef Identifier) { 2712*0b57cec5SDimitry Andric MD5 Hash; 2713*0b57cec5SDimitry Andric Hash.update(Identifier); 2714*0b57cec5SDimitry Andric // ... take the least significant 8 bytes and return those. Our MD5 2715*0b57cec5SDimitry Andric // implementation always returns its results in little endian, so we actually 2716*0b57cec5SDimitry Andric // need the "high" word. 2717*0b57cec5SDimitry Andric MD5::MD5Result Result; 2718*0b57cec5SDimitry Andric Hash.final(Result); 2719*0b57cec5SDimitry Andric return Result.high(); 2720*0b57cec5SDimitry Andric } 2721*0b57cec5SDimitry Andric 2722*0b57cec5SDimitry Andric void DwarfDebug::addDwarfTypeUnitType(DwarfCompileUnit &CU, 2723*0b57cec5SDimitry Andric StringRef Identifier, DIE &RefDie, 2724*0b57cec5SDimitry Andric const DICompositeType *CTy) { 2725*0b57cec5SDimitry Andric // Fast path if we're building some type units and one has already used the 2726*0b57cec5SDimitry Andric // address pool we know we're going to throw away all this work anyway, so 2727*0b57cec5SDimitry Andric // don't bother building dependent types. 2728*0b57cec5SDimitry Andric if (!TypeUnitsUnderConstruction.empty() && AddrPool.hasBeenUsed()) 2729*0b57cec5SDimitry Andric return; 2730*0b57cec5SDimitry Andric 2731*0b57cec5SDimitry Andric auto Ins = TypeSignatures.insert(std::make_pair(CTy, 0)); 2732*0b57cec5SDimitry Andric if (!Ins.second) { 2733*0b57cec5SDimitry Andric CU.addDIETypeSignature(RefDie, Ins.first->second); 2734*0b57cec5SDimitry Andric return; 2735*0b57cec5SDimitry Andric } 2736*0b57cec5SDimitry Andric 2737*0b57cec5SDimitry Andric bool TopLevelType = TypeUnitsUnderConstruction.empty(); 2738*0b57cec5SDimitry Andric AddrPool.resetUsedFlag(); 2739*0b57cec5SDimitry Andric 2740*0b57cec5SDimitry Andric auto OwnedUnit = llvm::make_unique<DwarfTypeUnit>(CU, Asm, this, &InfoHolder, 2741*0b57cec5SDimitry Andric getDwoLineTable(CU)); 2742*0b57cec5SDimitry Andric DwarfTypeUnit &NewTU = *OwnedUnit; 2743*0b57cec5SDimitry Andric DIE &UnitDie = NewTU.getUnitDie(); 2744*0b57cec5SDimitry Andric TypeUnitsUnderConstruction.emplace_back(std::move(OwnedUnit), CTy); 2745*0b57cec5SDimitry Andric 2746*0b57cec5SDimitry Andric NewTU.addUInt(UnitDie, dwarf::DW_AT_language, dwarf::DW_FORM_data2, 2747*0b57cec5SDimitry Andric CU.getLanguage()); 2748*0b57cec5SDimitry Andric 2749*0b57cec5SDimitry Andric uint64_t Signature = makeTypeSignature(Identifier); 2750*0b57cec5SDimitry Andric NewTU.setTypeSignature(Signature); 2751*0b57cec5SDimitry Andric Ins.first->second = Signature; 2752*0b57cec5SDimitry Andric 2753*0b57cec5SDimitry Andric if (useSplitDwarf()) { 2754*0b57cec5SDimitry Andric MCSection *Section = 2755*0b57cec5SDimitry Andric getDwarfVersion() <= 4 2756*0b57cec5SDimitry Andric ? Asm->getObjFileLowering().getDwarfTypesDWOSection() 2757*0b57cec5SDimitry Andric : Asm->getObjFileLowering().getDwarfInfoDWOSection(); 2758*0b57cec5SDimitry Andric NewTU.setSection(Section); 2759*0b57cec5SDimitry Andric } else { 2760*0b57cec5SDimitry Andric MCSection *Section = 2761*0b57cec5SDimitry Andric getDwarfVersion() <= 4 2762*0b57cec5SDimitry Andric ? Asm->getObjFileLowering().getDwarfTypesSection(Signature) 2763*0b57cec5SDimitry Andric : Asm->getObjFileLowering().getDwarfInfoSection(Signature); 2764*0b57cec5SDimitry Andric NewTU.setSection(Section); 2765*0b57cec5SDimitry Andric // Non-split type units reuse the compile unit's line table. 2766*0b57cec5SDimitry Andric CU.applyStmtList(UnitDie); 2767*0b57cec5SDimitry Andric } 2768*0b57cec5SDimitry Andric 2769*0b57cec5SDimitry Andric // Add DW_AT_str_offsets_base to the type unit DIE, but not for split type 2770*0b57cec5SDimitry Andric // units. 2771*0b57cec5SDimitry Andric if (useSegmentedStringOffsetsTable() && !useSplitDwarf()) 2772*0b57cec5SDimitry Andric NewTU.addStringOffsetsStart(); 2773*0b57cec5SDimitry Andric 2774*0b57cec5SDimitry Andric NewTU.setType(NewTU.createTypeDIE(CTy)); 2775*0b57cec5SDimitry Andric 2776*0b57cec5SDimitry Andric if (TopLevelType) { 2777*0b57cec5SDimitry Andric auto TypeUnitsToAdd = std::move(TypeUnitsUnderConstruction); 2778*0b57cec5SDimitry Andric TypeUnitsUnderConstruction.clear(); 2779*0b57cec5SDimitry Andric 2780*0b57cec5SDimitry Andric // Types referencing entries in the address table cannot be placed in type 2781*0b57cec5SDimitry Andric // units. 2782*0b57cec5SDimitry Andric if (AddrPool.hasBeenUsed()) { 2783*0b57cec5SDimitry Andric 2784*0b57cec5SDimitry Andric // Remove all the types built while building this type. 2785*0b57cec5SDimitry Andric // This is pessimistic as some of these types might not be dependent on 2786*0b57cec5SDimitry Andric // the type that used an address. 2787*0b57cec5SDimitry Andric for (const auto &TU : TypeUnitsToAdd) 2788*0b57cec5SDimitry Andric TypeSignatures.erase(TU.second); 2789*0b57cec5SDimitry Andric 2790*0b57cec5SDimitry Andric // Construct this type in the CU directly. 2791*0b57cec5SDimitry Andric // This is inefficient because all the dependent types will be rebuilt 2792*0b57cec5SDimitry Andric // from scratch, including building them in type units, discovering that 2793*0b57cec5SDimitry Andric // they depend on addresses, throwing them out and rebuilding them. 2794*0b57cec5SDimitry Andric CU.constructTypeDIE(RefDie, cast<DICompositeType>(CTy)); 2795*0b57cec5SDimitry Andric return; 2796*0b57cec5SDimitry Andric } 2797*0b57cec5SDimitry Andric 2798*0b57cec5SDimitry Andric // If the type wasn't dependent on fission addresses, finish adding the type 2799*0b57cec5SDimitry Andric // and all its dependent types. 2800*0b57cec5SDimitry Andric for (auto &TU : TypeUnitsToAdd) { 2801*0b57cec5SDimitry Andric InfoHolder.computeSizeAndOffsetsForUnit(TU.first.get()); 2802*0b57cec5SDimitry Andric InfoHolder.emitUnit(TU.first.get(), useSplitDwarf()); 2803*0b57cec5SDimitry Andric } 2804*0b57cec5SDimitry Andric } 2805*0b57cec5SDimitry Andric CU.addDIETypeSignature(RefDie, Signature); 2806*0b57cec5SDimitry Andric } 2807*0b57cec5SDimitry Andric 2808*0b57cec5SDimitry Andric DwarfDebug::NonTypeUnitContext::NonTypeUnitContext(DwarfDebug *DD) 2809*0b57cec5SDimitry Andric : DD(DD), 2810*0b57cec5SDimitry Andric TypeUnitsUnderConstruction(std::move(DD->TypeUnitsUnderConstruction)) { 2811*0b57cec5SDimitry Andric DD->TypeUnitsUnderConstruction.clear(); 2812*0b57cec5SDimitry Andric assert(TypeUnitsUnderConstruction.empty() || !DD->AddrPool.hasBeenUsed()); 2813*0b57cec5SDimitry Andric } 2814*0b57cec5SDimitry Andric 2815*0b57cec5SDimitry Andric DwarfDebug::NonTypeUnitContext::~NonTypeUnitContext() { 2816*0b57cec5SDimitry Andric DD->TypeUnitsUnderConstruction = std::move(TypeUnitsUnderConstruction); 2817*0b57cec5SDimitry Andric DD->AddrPool.resetUsedFlag(); 2818*0b57cec5SDimitry Andric } 2819*0b57cec5SDimitry Andric 2820*0b57cec5SDimitry Andric DwarfDebug::NonTypeUnitContext DwarfDebug::enterNonTypeUnitContext() { 2821*0b57cec5SDimitry Andric return NonTypeUnitContext(this); 2822*0b57cec5SDimitry Andric } 2823*0b57cec5SDimitry Andric 2824*0b57cec5SDimitry Andric // Add the Name along with its companion DIE to the appropriate accelerator 2825*0b57cec5SDimitry Andric // table (for AccelTableKind::Dwarf it's always AccelDebugNames, for 2826*0b57cec5SDimitry Andric // AccelTableKind::Apple, we use the table we got as an argument). If 2827*0b57cec5SDimitry Andric // accelerator tables are disabled, this function does nothing. 2828*0b57cec5SDimitry Andric template <typename DataT> 2829*0b57cec5SDimitry Andric void DwarfDebug::addAccelNameImpl(const DICompileUnit &CU, 2830*0b57cec5SDimitry Andric AccelTable<DataT> &AppleAccel, StringRef Name, 2831*0b57cec5SDimitry Andric const DIE &Die) { 2832*0b57cec5SDimitry Andric if (getAccelTableKind() == AccelTableKind::None) 2833*0b57cec5SDimitry Andric return; 2834*0b57cec5SDimitry Andric 2835*0b57cec5SDimitry Andric if (getAccelTableKind() != AccelTableKind::Apple && 2836*0b57cec5SDimitry Andric CU.getNameTableKind() != DICompileUnit::DebugNameTableKind::Default) 2837*0b57cec5SDimitry Andric return; 2838*0b57cec5SDimitry Andric 2839*0b57cec5SDimitry Andric DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder; 2840*0b57cec5SDimitry Andric DwarfStringPoolEntryRef Ref = Holder.getStringPool().getEntry(*Asm, Name); 2841*0b57cec5SDimitry Andric 2842*0b57cec5SDimitry Andric switch (getAccelTableKind()) { 2843*0b57cec5SDimitry Andric case AccelTableKind::Apple: 2844*0b57cec5SDimitry Andric AppleAccel.addName(Ref, Die); 2845*0b57cec5SDimitry Andric break; 2846*0b57cec5SDimitry Andric case AccelTableKind::Dwarf: 2847*0b57cec5SDimitry Andric AccelDebugNames.addName(Ref, Die); 2848*0b57cec5SDimitry Andric break; 2849*0b57cec5SDimitry Andric case AccelTableKind::Default: 2850*0b57cec5SDimitry Andric llvm_unreachable("Default should have already been resolved."); 2851*0b57cec5SDimitry Andric case AccelTableKind::None: 2852*0b57cec5SDimitry Andric llvm_unreachable("None handled above"); 2853*0b57cec5SDimitry Andric } 2854*0b57cec5SDimitry Andric } 2855*0b57cec5SDimitry Andric 2856*0b57cec5SDimitry Andric void DwarfDebug::addAccelName(const DICompileUnit &CU, StringRef Name, 2857*0b57cec5SDimitry Andric const DIE &Die) { 2858*0b57cec5SDimitry Andric addAccelNameImpl(CU, AccelNames, Name, Die); 2859*0b57cec5SDimitry Andric } 2860*0b57cec5SDimitry Andric 2861*0b57cec5SDimitry Andric void DwarfDebug::addAccelObjC(const DICompileUnit &CU, StringRef Name, 2862*0b57cec5SDimitry Andric const DIE &Die) { 2863*0b57cec5SDimitry Andric // ObjC names go only into the Apple accelerator tables. 2864*0b57cec5SDimitry Andric if (getAccelTableKind() == AccelTableKind::Apple) 2865*0b57cec5SDimitry Andric addAccelNameImpl(CU, AccelObjC, Name, Die); 2866*0b57cec5SDimitry Andric } 2867*0b57cec5SDimitry Andric 2868*0b57cec5SDimitry Andric void DwarfDebug::addAccelNamespace(const DICompileUnit &CU, StringRef Name, 2869*0b57cec5SDimitry Andric const DIE &Die) { 2870*0b57cec5SDimitry Andric addAccelNameImpl(CU, AccelNamespace, Name, Die); 2871*0b57cec5SDimitry Andric } 2872*0b57cec5SDimitry Andric 2873*0b57cec5SDimitry Andric void DwarfDebug::addAccelType(const DICompileUnit &CU, StringRef Name, 2874*0b57cec5SDimitry Andric const DIE &Die, char Flags) { 2875*0b57cec5SDimitry Andric addAccelNameImpl(CU, AccelTypes, Name, Die); 2876*0b57cec5SDimitry Andric } 2877*0b57cec5SDimitry Andric 2878*0b57cec5SDimitry Andric uint16_t DwarfDebug::getDwarfVersion() const { 2879*0b57cec5SDimitry Andric return Asm->OutStreamer->getContext().getDwarfVersion(); 2880*0b57cec5SDimitry Andric } 2881*0b57cec5SDimitry Andric 2882*0b57cec5SDimitry Andric void DwarfDebug::addSectionLabel(const MCSymbol *Sym) { 2883*0b57cec5SDimitry Andric SectionLabels.insert(std::make_pair(&Sym->getSection(), Sym)); 2884*0b57cec5SDimitry Andric } 2885*0b57cec5SDimitry Andric 2886*0b57cec5SDimitry Andric const MCSymbol *DwarfDebug::getSectionLabel(const MCSection *S) { 2887*0b57cec5SDimitry Andric return SectionLabels.find(S)->second; 2888*0b57cec5SDimitry Andric } 2889