10b57cec5SDimitry Andric //===- llvm/CodeGen/DwarfCompileUnit.cpp - Dwarf Compile Units ------------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This file contains support for constructing a dwarf compile unit. 100b57cec5SDimitry Andric // 110b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 120b57cec5SDimitry Andric 130b57cec5SDimitry Andric #include "DwarfCompileUnit.h" 140b57cec5SDimitry Andric #include "AddressPool.h" 150b57cec5SDimitry Andric #include "DwarfExpression.h" 160b57cec5SDimitry Andric #include "llvm/ADT/None.h" 170b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h" 180b57cec5SDimitry Andric #include "llvm/ADT/SmallString.h" 190b57cec5SDimitry Andric #include "llvm/CodeGen/AsmPrinter.h" 200b57cec5SDimitry Andric #include "llvm/CodeGen/DIE.h" 210b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h" 220b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstr.h" 230b57cec5SDimitry Andric #include "llvm/CodeGen/MachineOperand.h" 240b57cec5SDimitry Andric #include "llvm/CodeGen/TargetFrameLowering.h" 250b57cec5SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h" 260b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h" 270b57cec5SDimitry Andric #include "llvm/IR/DataLayout.h" 280b57cec5SDimitry Andric #include "llvm/IR/DebugInfo.h" 290b57cec5SDimitry Andric #include "llvm/IR/GlobalVariable.h" 300b57cec5SDimitry Andric #include "llvm/MC/MCSection.h" 310b57cec5SDimitry Andric #include "llvm/MC/MCStreamer.h" 320b57cec5SDimitry Andric #include "llvm/MC/MCSymbol.h" 335ffd83dbSDimitry Andric #include "llvm/MC/MCSymbolWasm.h" 340b57cec5SDimitry Andric #include "llvm/MC/MachineLocation.h" 350b57cec5SDimitry Andric #include "llvm/Target/TargetLoweringObjectFile.h" 360b57cec5SDimitry Andric #include "llvm/Target/TargetMachine.h" 370b57cec5SDimitry Andric #include "llvm/Target/TargetOptions.h" 380b57cec5SDimitry Andric #include <iterator> 390b57cec5SDimitry Andric #include <string> 400b57cec5SDimitry Andric #include <utility> 410b57cec5SDimitry Andric 420b57cec5SDimitry Andric using namespace llvm; 430b57cec5SDimitry Andric 44480093f4SDimitry Andric static dwarf::Tag GetCompileUnitType(UnitKind Kind, DwarfDebug *DW) { 45480093f4SDimitry Andric 46480093f4SDimitry Andric // According to DWARF Debugging Information Format Version 5, 47480093f4SDimitry Andric // 3.1.2 Skeleton Compilation Unit Entries: 48480093f4SDimitry Andric // "When generating a split DWARF object file (see Section 7.3.2 49480093f4SDimitry Andric // on page 187), the compilation unit in the .debug_info section 50480093f4SDimitry Andric // is a "skeleton" compilation unit with the tag DW_TAG_skeleton_unit" 51480093f4SDimitry Andric if (DW->getDwarfVersion() >= 5 && Kind == UnitKind::Skeleton) 52480093f4SDimitry Andric return dwarf::DW_TAG_skeleton_unit; 53480093f4SDimitry Andric 54480093f4SDimitry Andric return dwarf::DW_TAG_compile_unit; 55480093f4SDimitry Andric } 56480093f4SDimitry Andric 570b57cec5SDimitry Andric DwarfCompileUnit::DwarfCompileUnit(unsigned UID, const DICompileUnit *Node, 580b57cec5SDimitry Andric AsmPrinter *A, DwarfDebug *DW, 59480093f4SDimitry Andric DwarfFile *DWU, UnitKind Kind) 60480093f4SDimitry Andric : DwarfUnit(GetCompileUnitType(Kind, DW), Node, A, DW, DWU), UniqueID(UID) { 610b57cec5SDimitry Andric insertDIE(Node, &getUnitDie()); 620b57cec5SDimitry Andric MacroLabelBegin = Asm->createTempSymbol("cu_macro_begin"); 630b57cec5SDimitry Andric } 640b57cec5SDimitry Andric 650b57cec5SDimitry Andric /// addLabelAddress - Add a dwarf label attribute data and value using 660b57cec5SDimitry Andric /// DW_FORM_addr or DW_FORM_GNU_addr_index. 670b57cec5SDimitry Andric void DwarfCompileUnit::addLabelAddress(DIE &Die, dwarf::Attribute Attribute, 680b57cec5SDimitry Andric const MCSymbol *Label) { 690b57cec5SDimitry Andric // Don't use the address pool in non-fission or in the skeleton unit itself. 700b57cec5SDimitry Andric if ((!DD->useSplitDwarf() || !Skeleton) && DD->getDwarfVersion() < 5) 710b57cec5SDimitry Andric return addLocalLabelAddress(Die, Attribute, Label); 720b57cec5SDimitry Andric 730b57cec5SDimitry Andric if (Label) 740b57cec5SDimitry Andric DD->addArangeLabel(SymbolCU(this, Label)); 750b57cec5SDimitry Andric 760b57cec5SDimitry Andric unsigned idx = DD->getAddressPool().getIndex(Label); 770b57cec5SDimitry Andric Die.addValue(DIEValueAllocator, Attribute, 780b57cec5SDimitry Andric DD->getDwarfVersion() >= 5 ? dwarf::DW_FORM_addrx 790b57cec5SDimitry Andric : dwarf::DW_FORM_GNU_addr_index, 800b57cec5SDimitry Andric DIEInteger(idx)); 810b57cec5SDimitry Andric } 820b57cec5SDimitry Andric 830b57cec5SDimitry Andric void DwarfCompileUnit::addLocalLabelAddress(DIE &Die, 840b57cec5SDimitry Andric dwarf::Attribute Attribute, 850b57cec5SDimitry Andric const MCSymbol *Label) { 860b57cec5SDimitry Andric if (Label) 870b57cec5SDimitry Andric DD->addArangeLabel(SymbolCU(this, Label)); 880b57cec5SDimitry Andric 890b57cec5SDimitry Andric if (Label) 900b57cec5SDimitry Andric Die.addValue(DIEValueAllocator, Attribute, dwarf::DW_FORM_addr, 910b57cec5SDimitry Andric DIELabel(Label)); 920b57cec5SDimitry Andric else 930b57cec5SDimitry Andric Die.addValue(DIEValueAllocator, Attribute, dwarf::DW_FORM_addr, 940b57cec5SDimitry Andric DIEInteger(0)); 950b57cec5SDimitry Andric } 960b57cec5SDimitry Andric 970b57cec5SDimitry Andric unsigned DwarfCompileUnit::getOrCreateSourceID(const DIFile *File) { 980b57cec5SDimitry Andric // If we print assembly, we can't separate .file entries according to 990b57cec5SDimitry Andric // compile units. Thus all files will belong to the default compile unit. 1000b57cec5SDimitry Andric 1010b57cec5SDimitry Andric // FIXME: add a better feature test than hasRawTextSupport. Even better, 1020b57cec5SDimitry Andric // extend .file to support this. 1030b57cec5SDimitry Andric unsigned CUID = Asm->OutStreamer->hasRawTextSupport() ? 0 : getUniqueID(); 1040b57cec5SDimitry Andric if (!File) 1055ffd83dbSDimitry Andric return Asm->OutStreamer->emitDwarfFileDirective(0, "", "", None, None, 1065ffd83dbSDimitry Andric CUID); 1075ffd83dbSDimitry Andric return Asm->OutStreamer->emitDwarfFileDirective( 108*e8d8bef9SDimitry Andric 0, File->getDirectory(), File->getFilename(), DD->getMD5AsBytes(File), 1090b57cec5SDimitry Andric File->getSource(), CUID); 1100b57cec5SDimitry Andric } 1110b57cec5SDimitry Andric 1120b57cec5SDimitry Andric DIE *DwarfCompileUnit::getOrCreateGlobalVariableDIE( 1130b57cec5SDimitry Andric const DIGlobalVariable *GV, ArrayRef<GlobalExpr> GlobalExprs) { 1140b57cec5SDimitry Andric // Check for pre-existence. 1150b57cec5SDimitry Andric if (DIE *Die = getDIE(GV)) 1160b57cec5SDimitry Andric return Die; 1170b57cec5SDimitry Andric 1180b57cec5SDimitry Andric assert(GV); 1190b57cec5SDimitry Andric 1200b57cec5SDimitry Andric auto *GVContext = GV->getScope(); 1210b57cec5SDimitry Andric const DIType *GTy = GV->getType(); 1220b57cec5SDimitry Andric 1230b57cec5SDimitry Andric // Construct the context before querying for the existence of the DIE in 1240b57cec5SDimitry Andric // case such construction creates the DIE. 1250b57cec5SDimitry Andric auto *CB = GVContext ? dyn_cast<DICommonBlock>(GVContext) : nullptr; 1260b57cec5SDimitry Andric DIE *ContextDIE = CB ? getOrCreateCommonBlock(CB, GlobalExprs) 1270b57cec5SDimitry Andric : getOrCreateContextDIE(GVContext); 1280b57cec5SDimitry Andric 1290b57cec5SDimitry Andric // Add to map. 1300b57cec5SDimitry Andric DIE *VariableDIE = &createAndAddDIE(GV->getTag(), *ContextDIE, GV); 1310b57cec5SDimitry Andric DIScope *DeclContext; 1320b57cec5SDimitry Andric if (auto *SDMDecl = GV->getStaticDataMemberDeclaration()) { 1330b57cec5SDimitry Andric DeclContext = SDMDecl->getScope(); 1340b57cec5SDimitry Andric assert(SDMDecl->isStaticMember() && "Expected static member decl"); 1350b57cec5SDimitry Andric assert(GV->isDefinition()); 1360b57cec5SDimitry Andric // We need the declaration DIE that is in the static member's class. 1370b57cec5SDimitry Andric DIE *VariableSpecDIE = getOrCreateStaticMemberDIE(SDMDecl); 1380b57cec5SDimitry Andric addDIEEntry(*VariableDIE, dwarf::DW_AT_specification, *VariableSpecDIE); 1390b57cec5SDimitry Andric // If the global variable's type is different from the one in the class 1400b57cec5SDimitry Andric // member type, assume that it's more specific and also emit it. 1410b57cec5SDimitry Andric if (GTy != SDMDecl->getBaseType()) 1420b57cec5SDimitry Andric addType(*VariableDIE, GTy); 1430b57cec5SDimitry Andric } else { 1440b57cec5SDimitry Andric DeclContext = GV->getScope(); 1450b57cec5SDimitry Andric // Add name and type. 1460b57cec5SDimitry Andric addString(*VariableDIE, dwarf::DW_AT_name, GV->getDisplayName()); 1475ffd83dbSDimitry Andric if (GTy) 1480b57cec5SDimitry Andric addType(*VariableDIE, GTy); 1490b57cec5SDimitry Andric 1500b57cec5SDimitry Andric // Add scoping info. 1510b57cec5SDimitry Andric if (!GV->isLocalToUnit()) 1520b57cec5SDimitry Andric addFlag(*VariableDIE, dwarf::DW_AT_external); 1530b57cec5SDimitry Andric 1540b57cec5SDimitry Andric // Add line number info. 1550b57cec5SDimitry Andric addSourceLine(*VariableDIE, GV); 1560b57cec5SDimitry Andric } 1570b57cec5SDimitry Andric 1580b57cec5SDimitry Andric if (!GV->isDefinition()) 1590b57cec5SDimitry Andric addFlag(*VariableDIE, dwarf::DW_AT_declaration); 1600b57cec5SDimitry Andric else 1610b57cec5SDimitry Andric addGlobalName(GV->getName(), *VariableDIE, DeclContext); 1620b57cec5SDimitry Andric 1630b57cec5SDimitry Andric if (uint32_t AlignInBytes = GV->getAlignInBytes()) 1640b57cec5SDimitry Andric addUInt(*VariableDIE, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata, 1650b57cec5SDimitry Andric AlignInBytes); 1660b57cec5SDimitry Andric 1670b57cec5SDimitry Andric if (MDTuple *TP = GV->getTemplateParams()) 1680b57cec5SDimitry Andric addTemplateParams(*VariableDIE, DINodeArray(TP)); 1690b57cec5SDimitry Andric 1700b57cec5SDimitry Andric // Add location. 1710b57cec5SDimitry Andric addLocationAttribute(VariableDIE, GV, GlobalExprs); 1720b57cec5SDimitry Andric 1730b57cec5SDimitry Andric return VariableDIE; 1740b57cec5SDimitry Andric } 1750b57cec5SDimitry Andric 1760b57cec5SDimitry Andric void DwarfCompileUnit::addLocationAttribute( 1770b57cec5SDimitry Andric DIE *VariableDIE, const DIGlobalVariable *GV, ArrayRef<GlobalExpr> GlobalExprs) { 1780b57cec5SDimitry Andric bool addToAccelTable = false; 1790b57cec5SDimitry Andric DIELoc *Loc = nullptr; 1800b57cec5SDimitry Andric Optional<unsigned> NVPTXAddressSpace; 1810b57cec5SDimitry Andric std::unique_ptr<DIEDwarfExpression> DwarfExpr; 1820b57cec5SDimitry Andric for (const auto &GE : GlobalExprs) { 1830b57cec5SDimitry Andric const GlobalVariable *Global = GE.Var; 1840b57cec5SDimitry Andric const DIExpression *Expr = GE.Expr; 1850b57cec5SDimitry Andric 1860b57cec5SDimitry Andric // For compatibility with DWARF 3 and earlier, 1870b57cec5SDimitry Andric // DW_AT_location(DW_OP_constu, X, DW_OP_stack_value) becomes 1880b57cec5SDimitry Andric // DW_AT_const_value(X). 1890b57cec5SDimitry Andric if (GlobalExprs.size() == 1 && Expr && Expr->isConstant()) { 1900b57cec5SDimitry Andric addToAccelTable = true; 1910b57cec5SDimitry Andric addConstantValue(*VariableDIE, /*Unsigned=*/true, Expr->getElement(1)); 1920b57cec5SDimitry Andric break; 1930b57cec5SDimitry Andric } 1940b57cec5SDimitry Andric 1950b57cec5SDimitry Andric // We cannot describe the location of dllimport'd variables: the 1960b57cec5SDimitry Andric // computation of their address requires loads from the IAT. 1970b57cec5SDimitry Andric if (Global && Global->hasDLLImportStorageClass()) 1980b57cec5SDimitry Andric continue; 1990b57cec5SDimitry Andric 2000b57cec5SDimitry Andric // Nothing to describe without address or constant. 2010b57cec5SDimitry Andric if (!Global && (!Expr || !Expr->isConstant())) 2020b57cec5SDimitry Andric continue; 2030b57cec5SDimitry Andric 2040b57cec5SDimitry Andric if (Global && Global->isThreadLocal() && 2050b57cec5SDimitry Andric !Asm->getObjFileLowering().supportDebugThreadLocalLocation()) 2060b57cec5SDimitry Andric continue; 2070b57cec5SDimitry Andric 2080b57cec5SDimitry Andric if (!Loc) { 2090b57cec5SDimitry Andric addToAccelTable = true; 2100b57cec5SDimitry Andric Loc = new (DIEValueAllocator) DIELoc; 2118bcb0991SDimitry Andric DwarfExpr = std::make_unique<DIEDwarfExpression>(*Asm, *this, *Loc); 2120b57cec5SDimitry Andric } 2130b57cec5SDimitry Andric 2140b57cec5SDimitry Andric if (Expr) { 2150b57cec5SDimitry Andric // According to 2160b57cec5SDimitry Andric // https://docs.nvidia.com/cuda/archive/10.0/ptx-writers-guide-to-interoperability/index.html#cuda-specific-dwarf 2170b57cec5SDimitry Andric // cuda-gdb requires DW_AT_address_class for all variables to be able to 2180b57cec5SDimitry Andric // correctly interpret address space of the variable address. 2190b57cec5SDimitry Andric // Decode DW_OP_constu <DWARF Address Space> DW_OP_swap DW_OP_xderef 2200b57cec5SDimitry Andric // sequence for the NVPTX + gdb target. 2210b57cec5SDimitry Andric unsigned LocalNVPTXAddressSpace; 2220b57cec5SDimitry Andric if (Asm->TM.getTargetTriple().isNVPTX() && DD->tuneForGDB()) { 2230b57cec5SDimitry Andric const DIExpression *NewExpr = 2240b57cec5SDimitry Andric DIExpression::extractAddressClass(Expr, LocalNVPTXAddressSpace); 2250b57cec5SDimitry Andric if (NewExpr != Expr) { 2260b57cec5SDimitry Andric Expr = NewExpr; 2270b57cec5SDimitry Andric NVPTXAddressSpace = LocalNVPTXAddressSpace; 2280b57cec5SDimitry Andric } 2290b57cec5SDimitry Andric } 2300b57cec5SDimitry Andric DwarfExpr->addFragmentOffset(Expr); 2310b57cec5SDimitry Andric } 2320b57cec5SDimitry Andric 2330b57cec5SDimitry Andric if (Global) { 2340b57cec5SDimitry Andric const MCSymbol *Sym = Asm->getSymbol(Global); 2350b57cec5SDimitry Andric if (Global->isThreadLocal()) { 2360b57cec5SDimitry Andric if (Asm->TM.useEmulatedTLS()) { 2370b57cec5SDimitry Andric // TODO: add debug info for emulated thread local mode. 2380b57cec5SDimitry Andric } else { 2390b57cec5SDimitry Andric // FIXME: Make this work with -gsplit-dwarf. 2400b57cec5SDimitry Andric unsigned PointerSize = Asm->getDataLayout().getPointerSize(); 2410b57cec5SDimitry Andric assert((PointerSize == 4 || PointerSize == 8) && 2420b57cec5SDimitry Andric "Add support for other sizes if necessary"); 2430b57cec5SDimitry Andric // Based on GCC's support for TLS: 2440b57cec5SDimitry Andric if (!DD->useSplitDwarf()) { 2450b57cec5SDimitry Andric // 1) Start with a constNu of the appropriate pointer size 2460b57cec5SDimitry Andric addUInt(*Loc, dwarf::DW_FORM_data1, 2470b57cec5SDimitry Andric PointerSize == 4 ? dwarf::DW_OP_const4u 2480b57cec5SDimitry Andric : dwarf::DW_OP_const8u); 2490b57cec5SDimitry Andric // 2) containing the (relocated) offset of the TLS variable 2500b57cec5SDimitry Andric // within the module's TLS block. 251*e8d8bef9SDimitry Andric addExpr(*Loc, 252*e8d8bef9SDimitry Andric PointerSize == 4 ? dwarf::DW_FORM_data4 253*e8d8bef9SDimitry Andric : dwarf::DW_FORM_data8, 2540b57cec5SDimitry Andric Asm->getObjFileLowering().getDebugThreadLocalSymbol(Sym)); 2550b57cec5SDimitry Andric } else { 2560b57cec5SDimitry Andric addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_const_index); 2570b57cec5SDimitry Andric addUInt(*Loc, dwarf::DW_FORM_udata, 2580b57cec5SDimitry Andric DD->getAddressPool().getIndex(Sym, /* TLS */ true)); 2590b57cec5SDimitry Andric } 2600b57cec5SDimitry Andric // 3) followed by an OP to make the debugger do a TLS lookup. 2610b57cec5SDimitry Andric addUInt(*Loc, dwarf::DW_FORM_data1, 2620b57cec5SDimitry Andric DD->useGNUTLSOpcode() ? dwarf::DW_OP_GNU_push_tls_address 2630b57cec5SDimitry Andric : dwarf::DW_OP_form_tls_address); 2640b57cec5SDimitry Andric } 2650b57cec5SDimitry Andric } else { 2660b57cec5SDimitry Andric DD->addArangeLabel(SymbolCU(this, Sym)); 2670b57cec5SDimitry Andric addOpAddress(*Loc, Sym); 2680b57cec5SDimitry Andric } 2690b57cec5SDimitry Andric } 2700b57cec5SDimitry Andric // Global variables attached to symbols are memory locations. 2710b57cec5SDimitry Andric // It would be better if this were unconditional, but malformed input that 2720b57cec5SDimitry Andric // mixes non-fragments and fragments for the same variable is too expensive 2730b57cec5SDimitry Andric // to detect in the verifier. 2740b57cec5SDimitry Andric if (DwarfExpr->isUnknownLocation()) 2750b57cec5SDimitry Andric DwarfExpr->setMemoryLocationKind(); 2760b57cec5SDimitry Andric DwarfExpr->addExpression(Expr); 2770b57cec5SDimitry Andric } 2780b57cec5SDimitry Andric if (Asm->TM.getTargetTriple().isNVPTX() && DD->tuneForGDB()) { 2790b57cec5SDimitry Andric // According to 2800b57cec5SDimitry Andric // https://docs.nvidia.com/cuda/archive/10.0/ptx-writers-guide-to-interoperability/index.html#cuda-specific-dwarf 2810b57cec5SDimitry Andric // cuda-gdb requires DW_AT_address_class for all variables to be able to 2820b57cec5SDimitry Andric // correctly interpret address space of the variable address. 2830b57cec5SDimitry Andric const unsigned NVPTX_ADDR_global_space = 5; 2840b57cec5SDimitry Andric addUInt(*VariableDIE, dwarf::DW_AT_address_class, dwarf::DW_FORM_data1, 2850b57cec5SDimitry Andric NVPTXAddressSpace ? *NVPTXAddressSpace : NVPTX_ADDR_global_space); 2860b57cec5SDimitry Andric } 2870b57cec5SDimitry Andric if (Loc) 2880b57cec5SDimitry Andric addBlock(*VariableDIE, dwarf::DW_AT_location, DwarfExpr->finalize()); 2890b57cec5SDimitry Andric 2900b57cec5SDimitry Andric if (DD->useAllLinkageNames()) 2910b57cec5SDimitry Andric addLinkageName(*VariableDIE, GV->getLinkageName()); 2920b57cec5SDimitry Andric 2930b57cec5SDimitry Andric if (addToAccelTable) { 2940b57cec5SDimitry Andric DD->addAccelName(*CUNode, GV->getName(), *VariableDIE); 2950b57cec5SDimitry Andric 2960b57cec5SDimitry Andric // If the linkage name is different than the name, go ahead and output 2970b57cec5SDimitry Andric // that as well into the name table. 2980b57cec5SDimitry Andric if (GV->getLinkageName() != "" && GV->getName() != GV->getLinkageName() && 2990b57cec5SDimitry Andric DD->useAllLinkageNames()) 3000b57cec5SDimitry Andric DD->addAccelName(*CUNode, GV->getLinkageName(), *VariableDIE); 3010b57cec5SDimitry Andric } 3020b57cec5SDimitry Andric } 3030b57cec5SDimitry Andric 3040b57cec5SDimitry Andric DIE *DwarfCompileUnit::getOrCreateCommonBlock( 3050b57cec5SDimitry Andric const DICommonBlock *CB, ArrayRef<GlobalExpr> GlobalExprs) { 3060b57cec5SDimitry Andric // Construct the context before querying for the existence of the DIE in case 3070b57cec5SDimitry Andric // such construction creates the DIE. 3080b57cec5SDimitry Andric DIE *ContextDIE = getOrCreateContextDIE(CB->getScope()); 3090b57cec5SDimitry Andric 3100b57cec5SDimitry Andric if (DIE *NDie = getDIE(CB)) 3110b57cec5SDimitry Andric return NDie; 3120b57cec5SDimitry Andric DIE &NDie = createAndAddDIE(dwarf::DW_TAG_common_block, *ContextDIE, CB); 3130b57cec5SDimitry Andric StringRef Name = CB->getName().empty() ? "_BLNK_" : CB->getName(); 3140b57cec5SDimitry Andric addString(NDie, dwarf::DW_AT_name, Name); 3150b57cec5SDimitry Andric addGlobalName(Name, NDie, CB->getScope()); 3160b57cec5SDimitry Andric if (CB->getFile()) 3170b57cec5SDimitry Andric addSourceLine(NDie, CB->getLineNo(), CB->getFile()); 3180b57cec5SDimitry Andric if (DIGlobalVariable *V = CB->getDecl()) 3190b57cec5SDimitry Andric getCU().addLocationAttribute(&NDie, V, GlobalExprs); 3200b57cec5SDimitry Andric return &NDie; 3210b57cec5SDimitry Andric } 3220b57cec5SDimitry Andric 3230b57cec5SDimitry Andric void DwarfCompileUnit::addRange(RangeSpan Range) { 3245ffd83dbSDimitry Andric DD->insertSectionLabel(Range.Begin); 3255ffd83dbSDimitry Andric 3260b57cec5SDimitry Andric bool SameAsPrevCU = this == DD->getPrevCU(); 3270b57cec5SDimitry Andric DD->setPrevCU(this); 3280b57cec5SDimitry Andric // If we have no current ranges just add the range and return, otherwise, 3290b57cec5SDimitry Andric // check the current section and CU against the previous section and CU we 3300b57cec5SDimitry Andric // emitted into and the subprogram was contained within. If these are the 3310b57cec5SDimitry Andric // same then extend our current range, otherwise add this as a new range. 3320b57cec5SDimitry Andric if (CURanges.empty() || !SameAsPrevCU || 3338bcb0991SDimitry Andric (&CURanges.back().End->getSection() != 3348bcb0991SDimitry Andric &Range.End->getSection())) { 3350b57cec5SDimitry Andric CURanges.push_back(Range); 3360b57cec5SDimitry Andric return; 3370b57cec5SDimitry Andric } 3380b57cec5SDimitry Andric 3398bcb0991SDimitry Andric CURanges.back().End = Range.End; 3400b57cec5SDimitry Andric } 3410b57cec5SDimitry Andric 3420b57cec5SDimitry Andric void DwarfCompileUnit::initStmtList() { 3430b57cec5SDimitry Andric if (CUNode->isDebugDirectivesOnly()) 3440b57cec5SDimitry Andric return; 3450b57cec5SDimitry Andric 3460b57cec5SDimitry Andric const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering(); 3470b57cec5SDimitry Andric if (DD->useSectionsAsReferences()) { 3480b57cec5SDimitry Andric LineTableStartSym = TLOF.getDwarfLineSection()->getBeginSymbol(); 3490b57cec5SDimitry Andric } else { 3500b57cec5SDimitry Andric LineTableStartSym = 3510b57cec5SDimitry Andric Asm->OutStreamer->getDwarfLineTableSymbol(getUniqueID()); 3520b57cec5SDimitry Andric } 3530b57cec5SDimitry Andric 3540b57cec5SDimitry Andric // DW_AT_stmt_list is a offset of line number information for this 3550b57cec5SDimitry Andric // compile unit in debug_line section. For split dwarf this is 3560b57cec5SDimitry Andric // left in the skeleton CU and so not included. 3570b57cec5SDimitry Andric // The line table entries are not always emitted in assembly, so it 3580b57cec5SDimitry Andric // is not okay to use line_table_start here. 3590b57cec5SDimitry Andric addSectionLabel(getUnitDie(), dwarf::DW_AT_stmt_list, LineTableStartSym, 3600b57cec5SDimitry Andric TLOF.getDwarfLineSection()->getBeginSymbol()); 3610b57cec5SDimitry Andric } 3620b57cec5SDimitry Andric 3630b57cec5SDimitry Andric void DwarfCompileUnit::applyStmtList(DIE &D) { 3645ffd83dbSDimitry Andric const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering(); 3655ffd83dbSDimitry Andric addSectionLabel(D, dwarf::DW_AT_stmt_list, LineTableStartSym, 3665ffd83dbSDimitry Andric TLOF.getDwarfLineSection()->getBeginSymbol()); 3670b57cec5SDimitry Andric } 3680b57cec5SDimitry Andric 3690b57cec5SDimitry Andric void DwarfCompileUnit::attachLowHighPC(DIE &D, const MCSymbol *Begin, 3700b57cec5SDimitry Andric const MCSymbol *End) { 3710b57cec5SDimitry Andric assert(Begin && "Begin label should not be null!"); 3720b57cec5SDimitry Andric assert(End && "End label should not be null!"); 3730b57cec5SDimitry Andric assert(Begin->isDefined() && "Invalid starting label"); 3740b57cec5SDimitry Andric assert(End->isDefined() && "Invalid end label"); 3750b57cec5SDimitry Andric 3760b57cec5SDimitry Andric addLabelAddress(D, dwarf::DW_AT_low_pc, Begin); 3770b57cec5SDimitry Andric if (DD->getDwarfVersion() < 4) 3780b57cec5SDimitry Andric addLabelAddress(D, dwarf::DW_AT_high_pc, End); 3790b57cec5SDimitry Andric else 3800b57cec5SDimitry Andric addLabelDelta(D, dwarf::DW_AT_high_pc, End, Begin); 3810b57cec5SDimitry Andric } 3820b57cec5SDimitry Andric 3830b57cec5SDimitry Andric // Find DIE for the given subprogram and attach appropriate DW_AT_low_pc 3840b57cec5SDimitry Andric // and DW_AT_high_pc attributes. If there are global variables in this 3850b57cec5SDimitry Andric // scope then create and insert DIEs for these variables. 3860b57cec5SDimitry Andric DIE &DwarfCompileUnit::updateSubprogramScopeDIE(const DISubprogram *SP) { 3870b57cec5SDimitry Andric DIE *SPDie = getOrCreateSubprogramDIE(SP, includeMinimalInlineScopes()); 3880b57cec5SDimitry Andric 3895ffd83dbSDimitry Andric SmallVector<RangeSpan, 2> BB_List; 3905ffd83dbSDimitry Andric // If basic block sections are on, ranges for each basic block section has 3915ffd83dbSDimitry Andric // to be emitted separately. 3925ffd83dbSDimitry Andric for (const auto &R : Asm->MBBSectionRanges) 3935ffd83dbSDimitry Andric BB_List.push_back({R.second.BeginLabel, R.second.EndLabel}); 3945ffd83dbSDimitry Andric 3955ffd83dbSDimitry Andric attachRangesOrLowHighPC(*SPDie, BB_List); 3965ffd83dbSDimitry Andric 3970b57cec5SDimitry Andric if (DD->useAppleExtensionAttributes() && 3980b57cec5SDimitry Andric !DD->getCurrentFunction()->getTarget().Options.DisableFramePointerElim( 3990b57cec5SDimitry Andric *DD->getCurrentFunction())) 4000b57cec5SDimitry Andric addFlag(*SPDie, dwarf::DW_AT_APPLE_omit_frame_ptr); 4010b57cec5SDimitry Andric 4020b57cec5SDimitry Andric // Only include DW_AT_frame_base in full debug info 4030b57cec5SDimitry Andric if (!includeMinimalInlineScopes()) { 4045ffd83dbSDimitry Andric const TargetFrameLowering *TFI = Asm->MF->getSubtarget().getFrameLowering(); 4055ffd83dbSDimitry Andric TargetFrameLowering::DwarfFrameBase FrameBase = 4065ffd83dbSDimitry Andric TFI->getDwarfFrameBase(*Asm->MF); 4075ffd83dbSDimitry Andric switch (FrameBase.Kind) { 4085ffd83dbSDimitry Andric case TargetFrameLowering::DwarfFrameBase::Register: { 4095ffd83dbSDimitry Andric if (Register::isPhysicalRegister(FrameBase.Location.Reg)) { 4105ffd83dbSDimitry Andric MachineLocation Location(FrameBase.Location.Reg); 4115ffd83dbSDimitry Andric addAddress(*SPDie, dwarf::DW_AT_frame_base, Location); 4125ffd83dbSDimitry Andric } 4135ffd83dbSDimitry Andric break; 4145ffd83dbSDimitry Andric } 4155ffd83dbSDimitry Andric case TargetFrameLowering::DwarfFrameBase::CFA: { 4160b57cec5SDimitry Andric DIELoc *Loc = new (DIEValueAllocator) DIELoc; 4170b57cec5SDimitry Andric addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_call_frame_cfa); 4180b57cec5SDimitry Andric addBlock(*SPDie, dwarf::DW_AT_frame_base, Loc); 4195ffd83dbSDimitry Andric break; 4205ffd83dbSDimitry Andric } 4215ffd83dbSDimitry Andric case TargetFrameLowering::DwarfFrameBase::WasmFrameBase: { 4225ffd83dbSDimitry Andric // FIXME: duplicated from Target/WebAssembly/WebAssembly.h 4235ffd83dbSDimitry Andric // don't want to depend on target specific headers in this code? 4245ffd83dbSDimitry Andric const unsigned TI_GLOBAL_RELOC = 3; 425*e8d8bef9SDimitry Andric // FIXME: when writing dwo, we need to avoid relocations. Probably 426*e8d8bef9SDimitry Andric // the "right" solution is to treat globals the way func and data symbols 427*e8d8bef9SDimitry Andric // are (with entries in .debug_addr). 428*e8d8bef9SDimitry Andric if (FrameBase.Location.WasmLoc.Kind == TI_GLOBAL_RELOC && !isDwoUnit()) { 4295ffd83dbSDimitry Andric // These need to be relocatable. 4305ffd83dbSDimitry Andric assert(FrameBase.Location.WasmLoc.Index == 0); // Only SP so far. 4315ffd83dbSDimitry Andric auto SPSym = cast<MCSymbolWasm>( 4325ffd83dbSDimitry Andric Asm->GetExternalSymbolSymbol("__stack_pointer")); 4335ffd83dbSDimitry Andric // FIXME: this repeats what WebAssemblyMCInstLower:: 4345ffd83dbSDimitry Andric // GetExternalSymbolSymbol does, since if there's no code that 4355ffd83dbSDimitry Andric // refers to this symbol, we have to set it here. 4365ffd83dbSDimitry Andric SPSym->setType(wasm::WASM_SYMBOL_TYPE_GLOBAL); 4375ffd83dbSDimitry Andric SPSym->setGlobalType(wasm::WasmGlobalType{ 4385ffd83dbSDimitry Andric uint8_t(Asm->getSubtargetInfo().getTargetTriple().getArch() == 4395ffd83dbSDimitry Andric Triple::wasm64 4405ffd83dbSDimitry Andric ? wasm::WASM_TYPE_I64 4415ffd83dbSDimitry Andric : wasm::WASM_TYPE_I32), 4425ffd83dbSDimitry Andric true}); 4435ffd83dbSDimitry Andric DIELoc *Loc = new (DIEValueAllocator) DIELoc; 4445ffd83dbSDimitry Andric addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_WASM_location); 445*e8d8bef9SDimitry Andric addSInt(*Loc, dwarf::DW_FORM_sdata, TI_GLOBAL_RELOC); 446*e8d8bef9SDimitry Andric addLabel(*Loc, dwarf::DW_FORM_data4, SPSym); 4475ffd83dbSDimitry Andric DD->addArangeLabel(SymbolCU(this, SPSym)); 4485ffd83dbSDimitry Andric addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_stack_value); 4495ffd83dbSDimitry Andric addBlock(*SPDie, dwarf::DW_AT_frame_base, Loc); 4500b57cec5SDimitry Andric } else { 4515ffd83dbSDimitry Andric DIELoc *Loc = new (DIEValueAllocator) DIELoc; 4525ffd83dbSDimitry Andric DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc); 4535ffd83dbSDimitry Andric DIExpressionCursor Cursor({}); 4545ffd83dbSDimitry Andric DwarfExpr.addWasmLocation(FrameBase.Location.WasmLoc.Kind, 4555ffd83dbSDimitry Andric FrameBase.Location.WasmLoc.Index); 4565ffd83dbSDimitry Andric DwarfExpr.addExpression(std::move(Cursor)); 4575ffd83dbSDimitry Andric addBlock(*SPDie, dwarf::DW_AT_frame_base, DwarfExpr.finalize()); 4585ffd83dbSDimitry Andric } 4595ffd83dbSDimitry Andric break; 4605ffd83dbSDimitry Andric } 4610b57cec5SDimitry Andric } 4620b57cec5SDimitry Andric } 4630b57cec5SDimitry Andric 4640b57cec5SDimitry Andric // Add name to the name table, we do this here because we're guaranteed 4650b57cec5SDimitry Andric // to have concrete versions of our DW_TAG_subprogram nodes. 4660b57cec5SDimitry Andric DD->addSubprogramNames(*CUNode, SP, *SPDie); 4670b57cec5SDimitry Andric 4680b57cec5SDimitry Andric return *SPDie; 4690b57cec5SDimitry Andric } 4700b57cec5SDimitry Andric 4710b57cec5SDimitry Andric // Construct a DIE for this scope. 4720b57cec5SDimitry Andric void DwarfCompileUnit::constructScopeDIE( 4730b57cec5SDimitry Andric LexicalScope *Scope, SmallVectorImpl<DIE *> &FinalChildren) { 4740b57cec5SDimitry Andric if (!Scope || !Scope->getScopeNode()) 4750b57cec5SDimitry Andric return; 4760b57cec5SDimitry Andric 4770b57cec5SDimitry Andric auto *DS = Scope->getScopeNode(); 4780b57cec5SDimitry Andric 4790b57cec5SDimitry Andric assert((Scope->getInlinedAt() || !isa<DISubprogram>(DS)) && 4800b57cec5SDimitry Andric "Only handle inlined subprograms here, use " 4810b57cec5SDimitry Andric "constructSubprogramScopeDIE for non-inlined " 4820b57cec5SDimitry Andric "subprograms"); 4830b57cec5SDimitry Andric 4840b57cec5SDimitry Andric SmallVector<DIE *, 8> Children; 4850b57cec5SDimitry Andric 4860b57cec5SDimitry Andric // We try to create the scope DIE first, then the children DIEs. This will 4870b57cec5SDimitry Andric // avoid creating un-used children then removing them later when we find out 4880b57cec5SDimitry Andric // the scope DIE is null. 4890b57cec5SDimitry Andric DIE *ScopeDIE; 4900b57cec5SDimitry Andric if (Scope->getParent() && isa<DISubprogram>(DS)) { 4910b57cec5SDimitry Andric ScopeDIE = constructInlinedScopeDIE(Scope); 4920b57cec5SDimitry Andric if (!ScopeDIE) 4930b57cec5SDimitry Andric return; 4940b57cec5SDimitry Andric // We create children when the scope DIE is not null. 4950b57cec5SDimitry Andric createScopeChildrenDIE(Scope, Children); 4960b57cec5SDimitry Andric } else { 4970b57cec5SDimitry Andric // Early exit when we know the scope DIE is going to be null. 4980b57cec5SDimitry Andric if (DD->isLexicalScopeDIENull(Scope)) 4990b57cec5SDimitry Andric return; 5000b57cec5SDimitry Andric 5010b57cec5SDimitry Andric bool HasNonScopeChildren = false; 5020b57cec5SDimitry Andric 5030b57cec5SDimitry Andric // We create children here when we know the scope DIE is not going to be 5040b57cec5SDimitry Andric // null and the children will be added to the scope DIE. 5050b57cec5SDimitry Andric createScopeChildrenDIE(Scope, Children, &HasNonScopeChildren); 5060b57cec5SDimitry Andric 5070b57cec5SDimitry Andric // If there are only other scopes as children, put them directly in the 5080b57cec5SDimitry Andric // parent instead, as this scope would serve no purpose. 5090b57cec5SDimitry Andric if (!HasNonScopeChildren) { 5100b57cec5SDimitry Andric FinalChildren.insert(FinalChildren.end(), 5110b57cec5SDimitry Andric std::make_move_iterator(Children.begin()), 5120b57cec5SDimitry Andric std::make_move_iterator(Children.end())); 5130b57cec5SDimitry Andric return; 5140b57cec5SDimitry Andric } 5150b57cec5SDimitry Andric ScopeDIE = constructLexicalScopeDIE(Scope); 5160b57cec5SDimitry Andric assert(ScopeDIE && "Scope DIE should not be null."); 5170b57cec5SDimitry Andric } 5180b57cec5SDimitry Andric 5190b57cec5SDimitry Andric // Add children 5200b57cec5SDimitry Andric for (auto &I : Children) 5210b57cec5SDimitry Andric ScopeDIE->addChild(std::move(I)); 5220b57cec5SDimitry Andric 5230b57cec5SDimitry Andric FinalChildren.push_back(std::move(ScopeDIE)); 5240b57cec5SDimitry Andric } 5250b57cec5SDimitry Andric 5260b57cec5SDimitry Andric void DwarfCompileUnit::addScopeRangeList(DIE &ScopeDIE, 5270b57cec5SDimitry Andric SmallVector<RangeSpan, 2> Range) { 5280b57cec5SDimitry Andric 5290b57cec5SDimitry Andric HasRangeLists = true; 5300b57cec5SDimitry Andric 5310b57cec5SDimitry Andric // Add the range list to the set of ranges to be emitted. 5320b57cec5SDimitry Andric auto IndexAndList = 5330b57cec5SDimitry Andric (DD->getDwarfVersion() < 5 && Skeleton ? Skeleton->DU : DU) 5340b57cec5SDimitry Andric ->addRange(*(Skeleton ? Skeleton : this), std::move(Range)); 5350b57cec5SDimitry Andric 5360b57cec5SDimitry Andric uint32_t Index = IndexAndList.first; 5370b57cec5SDimitry Andric auto &List = *IndexAndList.second; 5380b57cec5SDimitry Andric 5390b57cec5SDimitry Andric // Under fission, ranges are specified by constant offsets relative to the 5400b57cec5SDimitry Andric // CU's DW_AT_GNU_ranges_base. 5410b57cec5SDimitry Andric // FIXME: For DWARF v5, do not generate the DW_AT_ranges attribute under 5420b57cec5SDimitry Andric // fission until we support the forms using the .debug_addr section 5430b57cec5SDimitry Andric // (DW_RLE_startx_endx etc.). 5440b57cec5SDimitry Andric if (DD->getDwarfVersion() >= 5) 5450b57cec5SDimitry Andric addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_rnglistx, Index); 5468bcb0991SDimitry Andric else { 5478bcb0991SDimitry Andric const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering(); 5488bcb0991SDimitry Andric const MCSymbol *RangeSectionSym = 5498bcb0991SDimitry Andric TLOF.getDwarfRangesSection()->getBeginSymbol(); 5508bcb0991SDimitry Andric if (isDwoUnit()) 551480093f4SDimitry Andric addSectionDelta(ScopeDIE, dwarf::DW_AT_ranges, List.Label, 5520b57cec5SDimitry Andric RangeSectionSym); 5530b57cec5SDimitry Andric else 554480093f4SDimitry Andric addSectionLabel(ScopeDIE, dwarf::DW_AT_ranges, List.Label, 5550b57cec5SDimitry Andric RangeSectionSym); 5560b57cec5SDimitry Andric } 5578bcb0991SDimitry Andric } 5580b57cec5SDimitry Andric 5590b57cec5SDimitry Andric void DwarfCompileUnit::attachRangesOrLowHighPC( 5600b57cec5SDimitry Andric DIE &Die, SmallVector<RangeSpan, 2> Ranges) { 561*e8d8bef9SDimitry Andric assert(!Ranges.empty()); 562*e8d8bef9SDimitry Andric if (!DD->useRangesSection() || 563*e8d8bef9SDimitry Andric (Ranges.size() == 1 && 564*e8d8bef9SDimitry Andric (!DD->alwaysUseRanges() || 565*e8d8bef9SDimitry Andric DD->getSectionLabel(&Ranges.front().Begin->getSection()) == 566*e8d8bef9SDimitry Andric Ranges.front().Begin))) { 5670b57cec5SDimitry Andric const RangeSpan &Front = Ranges.front(); 5680b57cec5SDimitry Andric const RangeSpan &Back = Ranges.back(); 5698bcb0991SDimitry Andric attachLowHighPC(Die, Front.Begin, Back.End); 5700b57cec5SDimitry Andric } else 5710b57cec5SDimitry Andric addScopeRangeList(Die, std::move(Ranges)); 5720b57cec5SDimitry Andric } 5730b57cec5SDimitry Andric 5740b57cec5SDimitry Andric void DwarfCompileUnit::attachRangesOrLowHighPC( 5750b57cec5SDimitry Andric DIE &Die, const SmallVectorImpl<InsnRange> &Ranges) { 5760b57cec5SDimitry Andric SmallVector<RangeSpan, 2> List; 5770b57cec5SDimitry Andric List.reserve(Ranges.size()); 5785ffd83dbSDimitry Andric for (const InsnRange &R : Ranges) { 5795ffd83dbSDimitry Andric auto *BeginLabel = DD->getLabelBeforeInsn(R.first); 5805ffd83dbSDimitry Andric auto *EndLabel = DD->getLabelAfterInsn(R.second); 5815ffd83dbSDimitry Andric 5825ffd83dbSDimitry Andric const auto *BeginMBB = R.first->getParent(); 5835ffd83dbSDimitry Andric const auto *EndMBB = R.second->getParent(); 5845ffd83dbSDimitry Andric 5855ffd83dbSDimitry Andric const auto *MBB = BeginMBB; 5865ffd83dbSDimitry Andric // Basic block sections allows basic block subsets to be placed in unique 5875ffd83dbSDimitry Andric // sections. For each section, the begin and end label must be added to the 5885ffd83dbSDimitry Andric // list. If there is more than one range, debug ranges must be used. 5895ffd83dbSDimitry Andric // Otherwise, low/high PC can be used. 5905ffd83dbSDimitry Andric // FIXME: Debug Info Emission depends on block order and this assumes that 5915ffd83dbSDimitry Andric // the order of blocks will be frozen beyond this point. 5925ffd83dbSDimitry Andric do { 5935ffd83dbSDimitry Andric if (MBB->sameSection(EndMBB) || MBB->isEndSection()) { 5945ffd83dbSDimitry Andric auto MBBSectionRange = Asm->MBBSectionRanges[MBB->getSectionIDNum()]; 5958bcb0991SDimitry Andric List.push_back( 5965ffd83dbSDimitry Andric {MBB->sameSection(BeginMBB) ? BeginLabel 5975ffd83dbSDimitry Andric : MBBSectionRange.BeginLabel, 5985ffd83dbSDimitry Andric MBB->sameSection(EndMBB) ? EndLabel : MBBSectionRange.EndLabel}); 5995ffd83dbSDimitry Andric } 6005ffd83dbSDimitry Andric if (MBB->sameSection(EndMBB)) 6015ffd83dbSDimitry Andric break; 6025ffd83dbSDimitry Andric MBB = MBB->getNextNode(); 6035ffd83dbSDimitry Andric } while (true); 6045ffd83dbSDimitry Andric } 6050b57cec5SDimitry Andric attachRangesOrLowHighPC(Die, std::move(List)); 6060b57cec5SDimitry Andric } 6070b57cec5SDimitry Andric 6080b57cec5SDimitry Andric // This scope represents inlined body of a function. Construct DIE to 6090b57cec5SDimitry Andric // represent this concrete inlined copy of the function. 6100b57cec5SDimitry Andric DIE *DwarfCompileUnit::constructInlinedScopeDIE(LexicalScope *Scope) { 6110b57cec5SDimitry Andric assert(Scope->getScopeNode()); 6120b57cec5SDimitry Andric auto *DS = Scope->getScopeNode(); 6130b57cec5SDimitry Andric auto *InlinedSP = getDISubprogram(DS); 6140b57cec5SDimitry Andric // Find the subprogram's DwarfCompileUnit in the SPMap in case the subprogram 6150b57cec5SDimitry Andric // was inlined from another compile unit. 6160b57cec5SDimitry Andric DIE *OriginDIE = getAbstractSPDies()[InlinedSP]; 6170b57cec5SDimitry Andric assert(OriginDIE && "Unable to find original DIE for an inlined subprogram."); 6180b57cec5SDimitry Andric 6190b57cec5SDimitry Andric auto ScopeDIE = DIE::get(DIEValueAllocator, dwarf::DW_TAG_inlined_subroutine); 6200b57cec5SDimitry Andric addDIEEntry(*ScopeDIE, dwarf::DW_AT_abstract_origin, *OriginDIE); 6210b57cec5SDimitry Andric 6220b57cec5SDimitry Andric attachRangesOrLowHighPC(*ScopeDIE, Scope->getRanges()); 6230b57cec5SDimitry Andric 6240b57cec5SDimitry Andric // Add the call site information to the DIE. 6250b57cec5SDimitry Andric const DILocation *IA = Scope->getInlinedAt(); 6260b57cec5SDimitry Andric addUInt(*ScopeDIE, dwarf::DW_AT_call_file, None, 6270b57cec5SDimitry Andric getOrCreateSourceID(IA->getFile())); 6280b57cec5SDimitry Andric addUInt(*ScopeDIE, dwarf::DW_AT_call_line, None, IA->getLine()); 6290b57cec5SDimitry Andric if (IA->getColumn()) 6300b57cec5SDimitry Andric addUInt(*ScopeDIE, dwarf::DW_AT_call_column, None, IA->getColumn()); 6310b57cec5SDimitry Andric if (IA->getDiscriminator() && DD->getDwarfVersion() >= 4) 6320b57cec5SDimitry Andric addUInt(*ScopeDIE, dwarf::DW_AT_GNU_discriminator, None, 6330b57cec5SDimitry Andric IA->getDiscriminator()); 6340b57cec5SDimitry Andric 6350b57cec5SDimitry Andric // Add name to the name table, we do this here because we're guaranteed 6360b57cec5SDimitry Andric // to have concrete versions of our DW_TAG_inlined_subprogram nodes. 6370b57cec5SDimitry Andric DD->addSubprogramNames(*CUNode, InlinedSP, *ScopeDIE); 6380b57cec5SDimitry Andric 6390b57cec5SDimitry Andric return ScopeDIE; 6400b57cec5SDimitry Andric } 6410b57cec5SDimitry Andric 6420b57cec5SDimitry Andric // Construct new DW_TAG_lexical_block for this scope and attach 6430b57cec5SDimitry Andric // DW_AT_low_pc/DW_AT_high_pc labels. 6440b57cec5SDimitry Andric DIE *DwarfCompileUnit::constructLexicalScopeDIE(LexicalScope *Scope) { 6450b57cec5SDimitry Andric if (DD->isLexicalScopeDIENull(Scope)) 6460b57cec5SDimitry Andric return nullptr; 6470b57cec5SDimitry Andric 6480b57cec5SDimitry Andric auto ScopeDIE = DIE::get(DIEValueAllocator, dwarf::DW_TAG_lexical_block); 6490b57cec5SDimitry Andric if (Scope->isAbstractScope()) 6500b57cec5SDimitry Andric return ScopeDIE; 6510b57cec5SDimitry Andric 6520b57cec5SDimitry Andric attachRangesOrLowHighPC(*ScopeDIE, Scope->getRanges()); 6530b57cec5SDimitry Andric 6540b57cec5SDimitry Andric return ScopeDIE; 6550b57cec5SDimitry Andric } 6560b57cec5SDimitry Andric 6570b57cec5SDimitry Andric /// constructVariableDIE - Construct a DIE for the given DbgVariable. 6580b57cec5SDimitry Andric DIE *DwarfCompileUnit::constructVariableDIE(DbgVariable &DV, bool Abstract) { 6590b57cec5SDimitry Andric auto D = constructVariableDIEImpl(DV, Abstract); 6600b57cec5SDimitry Andric DV.setDIE(*D); 6610b57cec5SDimitry Andric return D; 6620b57cec5SDimitry Andric } 6630b57cec5SDimitry Andric 6640b57cec5SDimitry Andric DIE *DwarfCompileUnit::constructLabelDIE(DbgLabel &DL, 6650b57cec5SDimitry Andric const LexicalScope &Scope) { 6660b57cec5SDimitry Andric auto LabelDie = DIE::get(DIEValueAllocator, DL.getTag()); 6670b57cec5SDimitry Andric insertDIE(DL.getLabel(), LabelDie); 6680b57cec5SDimitry Andric DL.setDIE(*LabelDie); 6690b57cec5SDimitry Andric 6700b57cec5SDimitry Andric if (Scope.isAbstractScope()) 6710b57cec5SDimitry Andric applyLabelAttributes(DL, *LabelDie); 6720b57cec5SDimitry Andric 6730b57cec5SDimitry Andric return LabelDie; 6740b57cec5SDimitry Andric } 6750b57cec5SDimitry Andric 6760b57cec5SDimitry Andric DIE *DwarfCompileUnit::constructVariableDIEImpl(const DbgVariable &DV, 6770b57cec5SDimitry Andric bool Abstract) { 6780b57cec5SDimitry Andric // Define variable debug information entry. 6790b57cec5SDimitry Andric auto VariableDie = DIE::get(DIEValueAllocator, DV.getTag()); 6800b57cec5SDimitry Andric insertDIE(DV.getVariable(), VariableDie); 6810b57cec5SDimitry Andric 6820b57cec5SDimitry Andric if (Abstract) { 6830b57cec5SDimitry Andric applyVariableAttributes(DV, *VariableDie); 6840b57cec5SDimitry Andric return VariableDie; 6850b57cec5SDimitry Andric } 6860b57cec5SDimitry Andric 6870b57cec5SDimitry Andric // Add variable address. 6880b57cec5SDimitry Andric 689*e8d8bef9SDimitry Andric unsigned Index = DV.getDebugLocListIndex(); 690*e8d8bef9SDimitry Andric if (Index != ~0U) { 691*e8d8bef9SDimitry Andric addLocationList(*VariableDie, dwarf::DW_AT_location, Index); 692480093f4SDimitry Andric auto TagOffset = DV.getDebugLocListTagOffset(); 693480093f4SDimitry Andric if (TagOffset) 694480093f4SDimitry Andric addUInt(*VariableDie, dwarf::DW_AT_LLVM_tag_offset, dwarf::DW_FORM_data1, 695480093f4SDimitry Andric *TagOffset); 6960b57cec5SDimitry Andric return VariableDie; 6970b57cec5SDimitry Andric } 6980b57cec5SDimitry Andric 6990b57cec5SDimitry Andric // Check if variable has a single location description. 7000b57cec5SDimitry Andric if (auto *DVal = DV.getValueLoc()) { 7010b57cec5SDimitry Andric if (DVal->isLocation()) 7020b57cec5SDimitry Andric addVariableAddress(DV, *VariableDie, DVal->getLoc()); 7030b57cec5SDimitry Andric else if (DVal->isInt()) { 7040b57cec5SDimitry Andric auto *Expr = DV.getSingleExpression(); 7050b57cec5SDimitry Andric if (Expr && Expr->getNumElements()) { 7060b57cec5SDimitry Andric DIELoc *Loc = new (DIEValueAllocator) DIELoc; 7070b57cec5SDimitry Andric DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc); 7080b57cec5SDimitry Andric // If there is an expression, emit raw unsigned bytes. 7090b57cec5SDimitry Andric DwarfExpr.addFragmentOffset(Expr); 7100b57cec5SDimitry Andric DwarfExpr.addUnsignedConstant(DVal->getInt()); 7110b57cec5SDimitry Andric DwarfExpr.addExpression(Expr); 7120b57cec5SDimitry Andric addBlock(*VariableDie, dwarf::DW_AT_location, DwarfExpr.finalize()); 713480093f4SDimitry Andric if (DwarfExpr.TagOffset) 714480093f4SDimitry Andric addUInt(*VariableDie, dwarf::DW_AT_LLVM_tag_offset, 715480093f4SDimitry Andric dwarf::DW_FORM_data1, *DwarfExpr.TagOffset); 716480093f4SDimitry Andric 7170b57cec5SDimitry Andric } else 7180b57cec5SDimitry Andric addConstantValue(*VariableDie, DVal->getInt(), DV.getType()); 7190b57cec5SDimitry Andric } else if (DVal->isConstantFP()) { 7200b57cec5SDimitry Andric addConstantFPValue(*VariableDie, DVal->getConstantFP()); 7210b57cec5SDimitry Andric } else if (DVal->isConstantInt()) { 7220b57cec5SDimitry Andric addConstantValue(*VariableDie, DVal->getConstantInt(), DV.getType()); 723*e8d8bef9SDimitry Andric } else if (DVal->isTargetIndexLocation()) { 724*e8d8bef9SDimitry Andric DIELoc *Loc = new (DIEValueAllocator) DIELoc; 725*e8d8bef9SDimitry Andric DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc); 726*e8d8bef9SDimitry Andric const DIBasicType *BT = dyn_cast<DIBasicType>( 727*e8d8bef9SDimitry Andric static_cast<const Metadata *>(DV.getVariable()->getType())); 728*e8d8bef9SDimitry Andric DwarfDebug::emitDebugLocValue(*Asm, BT, *DVal, DwarfExpr); 729*e8d8bef9SDimitry Andric addBlock(*VariableDie, dwarf::DW_AT_location, DwarfExpr.finalize()); 7300b57cec5SDimitry Andric } 7310b57cec5SDimitry Andric return VariableDie; 7320b57cec5SDimitry Andric } 7330b57cec5SDimitry Andric 7340b57cec5SDimitry Andric // .. else use frame index. 7350b57cec5SDimitry Andric if (!DV.hasFrameIndexExprs()) 7360b57cec5SDimitry Andric return VariableDie; 7370b57cec5SDimitry Andric 7380b57cec5SDimitry Andric Optional<unsigned> NVPTXAddressSpace; 7390b57cec5SDimitry Andric DIELoc *Loc = new (DIEValueAllocator) DIELoc; 7400b57cec5SDimitry Andric DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc); 7410b57cec5SDimitry Andric for (auto &Fragment : DV.getFrameIndexExprs()) { 7425ffd83dbSDimitry Andric Register FrameReg; 7430b57cec5SDimitry Andric const DIExpression *Expr = Fragment.Expr; 7440b57cec5SDimitry Andric const TargetFrameLowering *TFI = Asm->MF->getSubtarget().getFrameLowering(); 745*e8d8bef9SDimitry Andric StackOffset Offset = 746*e8d8bef9SDimitry Andric TFI->getFrameIndexReference(*Asm->MF, Fragment.FI, FrameReg); 7470b57cec5SDimitry Andric DwarfExpr.addFragmentOffset(Expr); 748*e8d8bef9SDimitry Andric 749*e8d8bef9SDimitry Andric auto *TRI = Asm->MF->getSubtarget().getRegisterInfo(); 7500b57cec5SDimitry Andric SmallVector<uint64_t, 8> Ops; 751*e8d8bef9SDimitry Andric TRI->getOffsetOpcodes(Offset, Ops); 752*e8d8bef9SDimitry Andric 7530b57cec5SDimitry Andric // According to 7540b57cec5SDimitry Andric // https://docs.nvidia.com/cuda/archive/10.0/ptx-writers-guide-to-interoperability/index.html#cuda-specific-dwarf 7550b57cec5SDimitry Andric // cuda-gdb requires DW_AT_address_class for all variables to be able to 7560b57cec5SDimitry Andric // correctly interpret address space of the variable address. 7570b57cec5SDimitry Andric // Decode DW_OP_constu <DWARF Address Space> DW_OP_swap DW_OP_xderef 7580b57cec5SDimitry Andric // sequence for the NVPTX + gdb target. 7590b57cec5SDimitry Andric unsigned LocalNVPTXAddressSpace; 7600b57cec5SDimitry Andric if (Asm->TM.getTargetTriple().isNVPTX() && DD->tuneForGDB()) { 7610b57cec5SDimitry Andric const DIExpression *NewExpr = 7620b57cec5SDimitry Andric DIExpression::extractAddressClass(Expr, LocalNVPTXAddressSpace); 7630b57cec5SDimitry Andric if (NewExpr != Expr) { 7640b57cec5SDimitry Andric Expr = NewExpr; 7650b57cec5SDimitry Andric NVPTXAddressSpace = LocalNVPTXAddressSpace; 7660b57cec5SDimitry Andric } 7670b57cec5SDimitry Andric } 7680b57cec5SDimitry Andric if (Expr) 7690b57cec5SDimitry Andric Ops.append(Expr->elements_begin(), Expr->elements_end()); 7700b57cec5SDimitry Andric DIExpressionCursor Cursor(Ops); 7710b57cec5SDimitry Andric DwarfExpr.setMemoryLocationKind(); 7720b57cec5SDimitry Andric if (const MCSymbol *FrameSymbol = Asm->getFunctionFrameSymbol()) 7730b57cec5SDimitry Andric addOpAddress(*Loc, FrameSymbol); 7740b57cec5SDimitry Andric else 7750b57cec5SDimitry Andric DwarfExpr.addMachineRegExpression( 7760b57cec5SDimitry Andric *Asm->MF->getSubtarget().getRegisterInfo(), Cursor, FrameReg); 7770b57cec5SDimitry Andric DwarfExpr.addExpression(std::move(Cursor)); 7780b57cec5SDimitry Andric } 7790b57cec5SDimitry Andric if (Asm->TM.getTargetTriple().isNVPTX() && DD->tuneForGDB()) { 7800b57cec5SDimitry Andric // According to 7810b57cec5SDimitry Andric // https://docs.nvidia.com/cuda/archive/10.0/ptx-writers-guide-to-interoperability/index.html#cuda-specific-dwarf 7820b57cec5SDimitry Andric // cuda-gdb requires DW_AT_address_class for all variables to be able to 7830b57cec5SDimitry Andric // correctly interpret address space of the variable address. 7840b57cec5SDimitry Andric const unsigned NVPTX_ADDR_local_space = 6; 7850b57cec5SDimitry Andric addUInt(*VariableDie, dwarf::DW_AT_address_class, dwarf::DW_FORM_data1, 7860b57cec5SDimitry Andric NVPTXAddressSpace ? *NVPTXAddressSpace : NVPTX_ADDR_local_space); 7870b57cec5SDimitry Andric } 7880b57cec5SDimitry Andric addBlock(*VariableDie, dwarf::DW_AT_location, DwarfExpr.finalize()); 7890b57cec5SDimitry Andric if (DwarfExpr.TagOffset) 7900b57cec5SDimitry Andric addUInt(*VariableDie, dwarf::DW_AT_LLVM_tag_offset, dwarf::DW_FORM_data1, 7910b57cec5SDimitry Andric *DwarfExpr.TagOffset); 7920b57cec5SDimitry Andric 7930b57cec5SDimitry Andric return VariableDie; 7940b57cec5SDimitry Andric } 7950b57cec5SDimitry Andric 7960b57cec5SDimitry Andric DIE *DwarfCompileUnit::constructVariableDIE(DbgVariable &DV, 7970b57cec5SDimitry Andric const LexicalScope &Scope, 7980b57cec5SDimitry Andric DIE *&ObjectPointer) { 7990b57cec5SDimitry Andric auto Var = constructVariableDIE(DV, Scope.isAbstractScope()); 8000b57cec5SDimitry Andric if (DV.isObjectPointer()) 8010b57cec5SDimitry Andric ObjectPointer = Var; 8020b57cec5SDimitry Andric return Var; 8030b57cec5SDimitry Andric } 8040b57cec5SDimitry Andric 8050b57cec5SDimitry Andric /// Return all DIVariables that appear in count: expressions. 8060b57cec5SDimitry Andric static SmallVector<const DIVariable *, 2> dependencies(DbgVariable *Var) { 8070b57cec5SDimitry Andric SmallVector<const DIVariable *, 2> Result; 8080b57cec5SDimitry Andric auto *Array = dyn_cast<DICompositeType>(Var->getType()); 8090b57cec5SDimitry Andric if (!Array || Array->getTag() != dwarf::DW_TAG_array_type) 8100b57cec5SDimitry Andric return Result; 8115ffd83dbSDimitry Andric if (auto *DLVar = Array->getDataLocation()) 8125ffd83dbSDimitry Andric Result.push_back(DLVar); 813*e8d8bef9SDimitry Andric if (auto *AsVar = Array->getAssociated()) 814*e8d8bef9SDimitry Andric Result.push_back(AsVar); 815*e8d8bef9SDimitry Andric if (auto *AlVar = Array->getAllocated()) 816*e8d8bef9SDimitry Andric Result.push_back(AlVar); 8170b57cec5SDimitry Andric for (auto *El : Array->getElements()) { 8180b57cec5SDimitry Andric if (auto *Subrange = dyn_cast<DISubrange>(El)) { 8195ffd83dbSDimitry Andric if (auto Count = Subrange->getCount()) 8200b57cec5SDimitry Andric if (auto *Dependency = Count.dyn_cast<DIVariable *>()) 8210b57cec5SDimitry Andric Result.push_back(Dependency); 8225ffd83dbSDimitry Andric if (auto LB = Subrange->getLowerBound()) 8235ffd83dbSDimitry Andric if (auto *Dependency = LB.dyn_cast<DIVariable *>()) 8245ffd83dbSDimitry Andric Result.push_back(Dependency); 8255ffd83dbSDimitry Andric if (auto UB = Subrange->getUpperBound()) 8265ffd83dbSDimitry Andric if (auto *Dependency = UB.dyn_cast<DIVariable *>()) 8275ffd83dbSDimitry Andric Result.push_back(Dependency); 8285ffd83dbSDimitry Andric if (auto ST = Subrange->getStride()) 8295ffd83dbSDimitry Andric if (auto *Dependency = ST.dyn_cast<DIVariable *>()) 8305ffd83dbSDimitry Andric Result.push_back(Dependency); 831*e8d8bef9SDimitry Andric } else if (auto *GenericSubrange = dyn_cast<DIGenericSubrange>(El)) { 832*e8d8bef9SDimitry Andric if (auto Count = GenericSubrange->getCount()) 833*e8d8bef9SDimitry Andric if (auto *Dependency = Count.dyn_cast<DIVariable *>()) 834*e8d8bef9SDimitry Andric Result.push_back(Dependency); 835*e8d8bef9SDimitry Andric if (auto LB = GenericSubrange->getLowerBound()) 836*e8d8bef9SDimitry Andric if (auto *Dependency = LB.dyn_cast<DIVariable *>()) 837*e8d8bef9SDimitry Andric Result.push_back(Dependency); 838*e8d8bef9SDimitry Andric if (auto UB = GenericSubrange->getUpperBound()) 839*e8d8bef9SDimitry Andric if (auto *Dependency = UB.dyn_cast<DIVariable *>()) 840*e8d8bef9SDimitry Andric Result.push_back(Dependency); 841*e8d8bef9SDimitry Andric if (auto ST = GenericSubrange->getStride()) 842*e8d8bef9SDimitry Andric if (auto *Dependency = ST.dyn_cast<DIVariable *>()) 843*e8d8bef9SDimitry Andric Result.push_back(Dependency); 8440b57cec5SDimitry Andric } 8450b57cec5SDimitry Andric } 8460b57cec5SDimitry Andric return Result; 8470b57cec5SDimitry Andric } 8480b57cec5SDimitry Andric 8490b57cec5SDimitry Andric /// Sort local variables so that variables appearing inside of helper 8500b57cec5SDimitry Andric /// expressions come first. 8510b57cec5SDimitry Andric static SmallVector<DbgVariable *, 8> 8520b57cec5SDimitry Andric sortLocalVars(SmallVectorImpl<DbgVariable *> &Input) { 8530b57cec5SDimitry Andric SmallVector<DbgVariable *, 8> Result; 8540b57cec5SDimitry Andric SmallVector<PointerIntPair<DbgVariable *, 1>, 8> WorkList; 8550b57cec5SDimitry Andric // Map back from a DIVariable to its containing DbgVariable. 8560b57cec5SDimitry Andric SmallDenseMap<const DILocalVariable *, DbgVariable *> DbgVar; 8570b57cec5SDimitry Andric // Set of DbgVariables in Result. 8580b57cec5SDimitry Andric SmallDenseSet<DbgVariable *, 8> Visited; 8590b57cec5SDimitry Andric // For cycle detection. 8600b57cec5SDimitry Andric SmallDenseSet<DbgVariable *, 8> Visiting; 8610b57cec5SDimitry Andric 8620b57cec5SDimitry Andric // Initialize the worklist and the DIVariable lookup table. 8630b57cec5SDimitry Andric for (auto Var : reverse(Input)) { 8640b57cec5SDimitry Andric DbgVar.insert({Var->getVariable(), Var}); 8650b57cec5SDimitry Andric WorkList.push_back({Var, 0}); 8660b57cec5SDimitry Andric } 8670b57cec5SDimitry Andric 8680b57cec5SDimitry Andric // Perform a stable topological sort by doing a DFS. 8690b57cec5SDimitry Andric while (!WorkList.empty()) { 8700b57cec5SDimitry Andric auto Item = WorkList.back(); 8710b57cec5SDimitry Andric DbgVariable *Var = Item.getPointer(); 8720b57cec5SDimitry Andric bool visitedAllDependencies = Item.getInt(); 8730b57cec5SDimitry Andric WorkList.pop_back(); 8740b57cec5SDimitry Andric 8750b57cec5SDimitry Andric // Dependency is in a different lexical scope or a global. 8760b57cec5SDimitry Andric if (!Var) 8770b57cec5SDimitry Andric continue; 8780b57cec5SDimitry Andric 8790b57cec5SDimitry Andric // Already handled. 8800b57cec5SDimitry Andric if (Visited.count(Var)) 8810b57cec5SDimitry Andric continue; 8820b57cec5SDimitry Andric 8830b57cec5SDimitry Andric // Add to Result if all dependencies are visited. 8840b57cec5SDimitry Andric if (visitedAllDependencies) { 8850b57cec5SDimitry Andric Visited.insert(Var); 8860b57cec5SDimitry Andric Result.push_back(Var); 8870b57cec5SDimitry Andric continue; 8880b57cec5SDimitry Andric } 8890b57cec5SDimitry Andric 8900b57cec5SDimitry Andric // Detect cycles. 8910b57cec5SDimitry Andric auto Res = Visiting.insert(Var); 8920b57cec5SDimitry Andric if (!Res.second) { 8930b57cec5SDimitry Andric assert(false && "dependency cycle in local variables"); 8940b57cec5SDimitry Andric return Result; 8950b57cec5SDimitry Andric } 8960b57cec5SDimitry Andric 8970b57cec5SDimitry Andric // Push dependencies and this node onto the worklist, so that this node is 8980b57cec5SDimitry Andric // visited again after all of its dependencies are handled. 8990b57cec5SDimitry Andric WorkList.push_back({Var, 1}); 9000b57cec5SDimitry Andric for (auto *Dependency : dependencies(Var)) { 9010b57cec5SDimitry Andric auto Dep = dyn_cast_or_null<const DILocalVariable>(Dependency); 9020b57cec5SDimitry Andric WorkList.push_back({DbgVar[Dep], 0}); 9030b57cec5SDimitry Andric } 9040b57cec5SDimitry Andric } 9050b57cec5SDimitry Andric return Result; 9060b57cec5SDimitry Andric } 9070b57cec5SDimitry Andric 9080b57cec5SDimitry Andric DIE *DwarfCompileUnit::createScopeChildrenDIE(LexicalScope *Scope, 9090b57cec5SDimitry Andric SmallVectorImpl<DIE *> &Children, 9100b57cec5SDimitry Andric bool *HasNonScopeChildren) { 9110b57cec5SDimitry Andric assert(Children.empty()); 9120b57cec5SDimitry Andric DIE *ObjectPointer = nullptr; 9130b57cec5SDimitry Andric 9140b57cec5SDimitry Andric // Emit function arguments (order is significant). 9150b57cec5SDimitry Andric auto Vars = DU->getScopeVariables().lookup(Scope); 9160b57cec5SDimitry Andric for (auto &DV : Vars.Args) 9170b57cec5SDimitry Andric Children.push_back(constructVariableDIE(*DV.second, *Scope, ObjectPointer)); 9180b57cec5SDimitry Andric 9190b57cec5SDimitry Andric // Emit local variables. 9200b57cec5SDimitry Andric auto Locals = sortLocalVars(Vars.Locals); 9210b57cec5SDimitry Andric for (DbgVariable *DV : Locals) 9220b57cec5SDimitry Andric Children.push_back(constructVariableDIE(*DV, *Scope, ObjectPointer)); 9230b57cec5SDimitry Andric 9240b57cec5SDimitry Andric // Skip imported directives in gmlt-like data. 9250b57cec5SDimitry Andric if (!includeMinimalInlineScopes()) { 9260b57cec5SDimitry Andric // There is no need to emit empty lexical block DIE. 9270b57cec5SDimitry Andric for (const auto *IE : ImportedEntities[Scope->getScopeNode()]) 9280b57cec5SDimitry Andric Children.push_back( 9290b57cec5SDimitry Andric constructImportedEntityDIE(cast<DIImportedEntity>(IE))); 9300b57cec5SDimitry Andric } 9310b57cec5SDimitry Andric 9320b57cec5SDimitry Andric if (HasNonScopeChildren) 9330b57cec5SDimitry Andric *HasNonScopeChildren = !Children.empty(); 9340b57cec5SDimitry Andric 9350b57cec5SDimitry Andric for (DbgLabel *DL : DU->getScopeLabels().lookup(Scope)) 9360b57cec5SDimitry Andric Children.push_back(constructLabelDIE(*DL, *Scope)); 9370b57cec5SDimitry Andric 9380b57cec5SDimitry Andric for (LexicalScope *LS : Scope->getChildren()) 9390b57cec5SDimitry Andric constructScopeDIE(LS, Children); 9400b57cec5SDimitry Andric 9410b57cec5SDimitry Andric return ObjectPointer; 9420b57cec5SDimitry Andric } 9430b57cec5SDimitry Andric 9440b57cec5SDimitry Andric DIE &DwarfCompileUnit::constructSubprogramScopeDIE(const DISubprogram *Sub, 9450b57cec5SDimitry Andric LexicalScope *Scope) { 9460b57cec5SDimitry Andric DIE &ScopeDIE = updateSubprogramScopeDIE(Sub); 9470b57cec5SDimitry Andric 9480b57cec5SDimitry Andric if (Scope) { 9490b57cec5SDimitry Andric assert(!Scope->getInlinedAt()); 9500b57cec5SDimitry Andric assert(!Scope->isAbstractScope()); 9510b57cec5SDimitry Andric // Collect lexical scope children first. 9520b57cec5SDimitry Andric // ObjectPointer might be a local (non-argument) local variable if it's a 9530b57cec5SDimitry Andric // block's synthetic this pointer. 9540b57cec5SDimitry Andric if (DIE *ObjectPointer = createAndAddScopeChildren(Scope, ScopeDIE)) 9550b57cec5SDimitry Andric addDIEEntry(ScopeDIE, dwarf::DW_AT_object_pointer, *ObjectPointer); 9560b57cec5SDimitry Andric } 9570b57cec5SDimitry Andric 9580b57cec5SDimitry Andric // If this is a variadic function, add an unspecified parameter. 9590b57cec5SDimitry Andric DITypeRefArray FnArgs = Sub->getType()->getTypeArray(); 9600b57cec5SDimitry Andric 9610b57cec5SDimitry Andric // If we have a single element of null, it is a function that returns void. 9620b57cec5SDimitry Andric // If we have more than one elements and the last one is null, it is a 9630b57cec5SDimitry Andric // variadic function. 9640b57cec5SDimitry Andric if (FnArgs.size() > 1 && !FnArgs[FnArgs.size() - 1] && 9650b57cec5SDimitry Andric !includeMinimalInlineScopes()) 9660b57cec5SDimitry Andric ScopeDIE.addChild( 9670b57cec5SDimitry Andric DIE::get(DIEValueAllocator, dwarf::DW_TAG_unspecified_parameters)); 9680b57cec5SDimitry Andric 9690b57cec5SDimitry Andric return ScopeDIE; 9700b57cec5SDimitry Andric } 9710b57cec5SDimitry Andric 9720b57cec5SDimitry Andric DIE *DwarfCompileUnit::createAndAddScopeChildren(LexicalScope *Scope, 9730b57cec5SDimitry Andric DIE &ScopeDIE) { 9740b57cec5SDimitry Andric // We create children when the scope DIE is not null. 9750b57cec5SDimitry Andric SmallVector<DIE *, 8> Children; 9760b57cec5SDimitry Andric DIE *ObjectPointer = createScopeChildrenDIE(Scope, Children); 9770b57cec5SDimitry Andric 9780b57cec5SDimitry Andric // Add children 9790b57cec5SDimitry Andric for (auto &I : Children) 9800b57cec5SDimitry Andric ScopeDIE.addChild(std::move(I)); 9810b57cec5SDimitry Andric 9820b57cec5SDimitry Andric return ObjectPointer; 9830b57cec5SDimitry Andric } 9840b57cec5SDimitry Andric 9850b57cec5SDimitry Andric void DwarfCompileUnit::constructAbstractSubprogramScopeDIE( 9860b57cec5SDimitry Andric LexicalScope *Scope) { 9870b57cec5SDimitry Andric DIE *&AbsDef = getAbstractSPDies()[Scope->getScopeNode()]; 9880b57cec5SDimitry Andric if (AbsDef) 9890b57cec5SDimitry Andric return; 9900b57cec5SDimitry Andric 9910b57cec5SDimitry Andric auto *SP = cast<DISubprogram>(Scope->getScopeNode()); 9920b57cec5SDimitry Andric 9930b57cec5SDimitry Andric DIE *ContextDIE; 9940b57cec5SDimitry Andric DwarfCompileUnit *ContextCU = this; 9950b57cec5SDimitry Andric 9960b57cec5SDimitry Andric if (includeMinimalInlineScopes()) 9970b57cec5SDimitry Andric ContextDIE = &getUnitDie(); 9980b57cec5SDimitry Andric // Some of this is duplicated from DwarfUnit::getOrCreateSubprogramDIE, with 9990b57cec5SDimitry Andric // the important distinction that the debug node is not associated with the 10000b57cec5SDimitry Andric // DIE (since the debug node will be associated with the concrete DIE, if 10010b57cec5SDimitry Andric // any). It could be refactored to some common utility function. 10020b57cec5SDimitry Andric else if (auto *SPDecl = SP->getDeclaration()) { 10030b57cec5SDimitry Andric ContextDIE = &getUnitDie(); 10040b57cec5SDimitry Andric getOrCreateSubprogramDIE(SPDecl); 10050b57cec5SDimitry Andric } else { 10060b57cec5SDimitry Andric ContextDIE = getOrCreateContextDIE(SP->getScope()); 10070b57cec5SDimitry Andric // The scope may be shared with a subprogram that has already been 10080b57cec5SDimitry Andric // constructed in another CU, in which case we need to construct this 10090b57cec5SDimitry Andric // subprogram in the same CU. 10100b57cec5SDimitry Andric ContextCU = DD->lookupCU(ContextDIE->getUnitDie()); 10110b57cec5SDimitry Andric } 10120b57cec5SDimitry Andric 10130b57cec5SDimitry Andric // Passing null as the associated node because the abstract definition 10140b57cec5SDimitry Andric // shouldn't be found by lookup. 10150b57cec5SDimitry Andric AbsDef = &ContextCU->createAndAddDIE(dwarf::DW_TAG_subprogram, *ContextDIE, nullptr); 10160b57cec5SDimitry Andric ContextCU->applySubprogramAttributesToDefinition(SP, *AbsDef); 10170b57cec5SDimitry Andric 10180b57cec5SDimitry Andric if (!ContextCU->includeMinimalInlineScopes()) 10190b57cec5SDimitry Andric ContextCU->addUInt(*AbsDef, dwarf::DW_AT_inline, None, dwarf::DW_INL_inlined); 10200b57cec5SDimitry Andric if (DIE *ObjectPointer = ContextCU->createAndAddScopeChildren(Scope, *AbsDef)) 10210b57cec5SDimitry Andric ContextCU->addDIEEntry(*AbsDef, dwarf::DW_AT_object_pointer, *ObjectPointer); 10220b57cec5SDimitry Andric } 10230b57cec5SDimitry Andric 10245ffd83dbSDimitry Andric bool DwarfCompileUnit::useGNUAnalogForDwarf5Feature() const { 1025*e8d8bef9SDimitry Andric return DD->getDwarfVersion() == 4 && !DD->tuneForLLDB(); 10268bcb0991SDimitry Andric } 10278bcb0991SDimitry Andric 10288bcb0991SDimitry Andric dwarf::Tag DwarfCompileUnit::getDwarf5OrGNUTag(dwarf::Tag Tag) const { 10295ffd83dbSDimitry Andric if (!useGNUAnalogForDwarf5Feature()) 10308bcb0991SDimitry Andric return Tag; 10318bcb0991SDimitry Andric switch (Tag) { 10328bcb0991SDimitry Andric case dwarf::DW_TAG_call_site: 10338bcb0991SDimitry Andric return dwarf::DW_TAG_GNU_call_site; 10348bcb0991SDimitry Andric case dwarf::DW_TAG_call_site_parameter: 10358bcb0991SDimitry Andric return dwarf::DW_TAG_GNU_call_site_parameter; 10368bcb0991SDimitry Andric default: 10378bcb0991SDimitry Andric llvm_unreachable("DWARF5 tag with no GNU analog"); 10388bcb0991SDimitry Andric } 10398bcb0991SDimitry Andric } 10408bcb0991SDimitry Andric 10418bcb0991SDimitry Andric dwarf::Attribute 10428bcb0991SDimitry Andric DwarfCompileUnit::getDwarf5OrGNUAttr(dwarf::Attribute Attr) const { 10435ffd83dbSDimitry Andric if (!useGNUAnalogForDwarf5Feature()) 10448bcb0991SDimitry Andric return Attr; 10458bcb0991SDimitry Andric switch (Attr) { 10468bcb0991SDimitry Andric case dwarf::DW_AT_call_all_calls: 10478bcb0991SDimitry Andric return dwarf::DW_AT_GNU_all_call_sites; 10488bcb0991SDimitry Andric case dwarf::DW_AT_call_target: 10498bcb0991SDimitry Andric return dwarf::DW_AT_GNU_call_site_target; 10508bcb0991SDimitry Andric case dwarf::DW_AT_call_origin: 10518bcb0991SDimitry Andric return dwarf::DW_AT_abstract_origin; 10525ffd83dbSDimitry Andric case dwarf::DW_AT_call_return_pc: 10538bcb0991SDimitry Andric return dwarf::DW_AT_low_pc; 10548bcb0991SDimitry Andric case dwarf::DW_AT_call_value: 10558bcb0991SDimitry Andric return dwarf::DW_AT_GNU_call_site_value; 10568bcb0991SDimitry Andric case dwarf::DW_AT_call_tail_call: 10578bcb0991SDimitry Andric return dwarf::DW_AT_GNU_tail_call; 10588bcb0991SDimitry Andric default: 10598bcb0991SDimitry Andric llvm_unreachable("DWARF5 attribute with no GNU analog"); 10608bcb0991SDimitry Andric } 10618bcb0991SDimitry Andric } 10628bcb0991SDimitry Andric 10638bcb0991SDimitry Andric dwarf::LocationAtom 10648bcb0991SDimitry Andric DwarfCompileUnit::getDwarf5OrGNULocationAtom(dwarf::LocationAtom Loc) const { 10655ffd83dbSDimitry Andric if (!useGNUAnalogForDwarf5Feature()) 10668bcb0991SDimitry Andric return Loc; 10678bcb0991SDimitry Andric switch (Loc) { 10688bcb0991SDimitry Andric case dwarf::DW_OP_entry_value: 10698bcb0991SDimitry Andric return dwarf::DW_OP_GNU_entry_value; 10708bcb0991SDimitry Andric default: 10718bcb0991SDimitry Andric llvm_unreachable("DWARF5 location atom with no GNU analog"); 10728bcb0991SDimitry Andric } 10738bcb0991SDimitry Andric } 10748bcb0991SDimitry Andric 10755ffd83dbSDimitry Andric DIE &DwarfCompileUnit::constructCallSiteEntryDIE(DIE &ScopeDIE, 10765ffd83dbSDimitry Andric DIE *CalleeDIE, 10775ffd83dbSDimitry Andric bool IsTail, 10785ffd83dbSDimitry Andric const MCSymbol *PCAddr, 10795ffd83dbSDimitry Andric const MCSymbol *CallAddr, 10805ffd83dbSDimitry Andric unsigned CallReg) { 10810b57cec5SDimitry Andric // Insert a call site entry DIE within ScopeDIE. 10828bcb0991SDimitry Andric DIE &CallSiteDIE = createAndAddDIE(getDwarf5OrGNUTag(dwarf::DW_TAG_call_site), 10838bcb0991SDimitry Andric ScopeDIE, nullptr); 10840b57cec5SDimitry Andric 10858bcb0991SDimitry Andric if (CallReg) { 10868bcb0991SDimitry Andric // Indirect call. 10878bcb0991SDimitry Andric addAddress(CallSiteDIE, getDwarf5OrGNUAttr(dwarf::DW_AT_call_target), 10888bcb0991SDimitry Andric MachineLocation(CallReg)); 10890b57cec5SDimitry Andric } else { 10905ffd83dbSDimitry Andric assert(CalleeDIE && "No DIE for call site entry origin"); 10918bcb0991SDimitry Andric addDIEEntry(CallSiteDIE, getDwarf5OrGNUAttr(dwarf::DW_AT_call_origin), 10928bcb0991SDimitry Andric *CalleeDIE); 10938bcb0991SDimitry Andric } 10948bcb0991SDimitry Andric 10955ffd83dbSDimitry Andric if (IsTail) { 10968bcb0991SDimitry Andric // Attach DW_AT_call_tail_call to tail calls for standards compliance. 10978bcb0991SDimitry Andric addFlag(CallSiteDIE, getDwarf5OrGNUAttr(dwarf::DW_AT_call_tail_call)); 10988bcb0991SDimitry Andric 10995ffd83dbSDimitry Andric // Attach the address of the branch instruction to allow the debugger to 11005ffd83dbSDimitry Andric // show where the tail call occurred. This attribute has no GNU analog. 11015ffd83dbSDimitry Andric // 11025ffd83dbSDimitry Andric // GDB works backwards from non-standard usage of DW_AT_low_pc (in DWARF4 11035ffd83dbSDimitry Andric // mode -- equivalently, in DWARF5 mode, DW_AT_call_return_pc) at tail-call 11045ffd83dbSDimitry Andric // site entries to figure out the PC of tail-calling branch instructions. 11055ffd83dbSDimitry Andric // This means it doesn't need the compiler to emit DW_AT_call_pc, so we 11065ffd83dbSDimitry Andric // don't emit it here. 11075ffd83dbSDimitry Andric // 11085ffd83dbSDimitry Andric // There's no need to tie non-GDB debuggers to this non-standardness, as it 11095ffd83dbSDimitry Andric // adds unnecessary complexity to the debugger. For non-GDB debuggers, emit 11105ffd83dbSDimitry Andric // the standard DW_AT_call_pc info. 11115ffd83dbSDimitry Andric if (!useGNUAnalogForDwarf5Feature()) 11125ffd83dbSDimitry Andric addLabelAddress(CallSiteDIE, dwarf::DW_AT_call_pc, CallAddr); 11135ffd83dbSDimitry Andric } 11145ffd83dbSDimitry Andric 11150b57cec5SDimitry Andric // Attach the return PC to allow the debugger to disambiguate call paths 11160b57cec5SDimitry Andric // from one function to another. 11175ffd83dbSDimitry Andric // 11185ffd83dbSDimitry Andric // The return PC is only really needed when the call /isn't/ a tail call, but 11195ffd83dbSDimitry Andric // GDB expects it in DWARF4 mode, even for tail calls (see the comment above 11205ffd83dbSDimitry Andric // the DW_AT_call_pc emission logic for an explanation). 11215ffd83dbSDimitry Andric if (!IsTail || useGNUAnalogForDwarf5Feature()) { 11225ffd83dbSDimitry Andric assert(PCAddr && "Missing return PC information for a call"); 11235ffd83dbSDimitry Andric addLabelAddress(CallSiteDIE, 11245ffd83dbSDimitry Andric getDwarf5OrGNUAttr(dwarf::DW_AT_call_return_pc), PCAddr); 11250b57cec5SDimitry Andric } 11268bcb0991SDimitry Andric 11270b57cec5SDimitry Andric return CallSiteDIE; 11280b57cec5SDimitry Andric } 11290b57cec5SDimitry Andric 11308bcb0991SDimitry Andric void DwarfCompileUnit::constructCallSiteParmEntryDIEs( 11318bcb0991SDimitry Andric DIE &CallSiteDIE, SmallVector<DbgCallSiteParam, 4> &Params) { 11328bcb0991SDimitry Andric for (const auto &Param : Params) { 11338bcb0991SDimitry Andric unsigned Register = Param.getRegister(); 11348bcb0991SDimitry Andric auto CallSiteDieParam = 11358bcb0991SDimitry Andric DIE::get(DIEValueAllocator, 11368bcb0991SDimitry Andric getDwarf5OrGNUTag(dwarf::DW_TAG_call_site_parameter)); 11378bcb0991SDimitry Andric insertDIE(CallSiteDieParam); 11388bcb0991SDimitry Andric addAddress(*CallSiteDieParam, dwarf::DW_AT_location, 11398bcb0991SDimitry Andric MachineLocation(Register)); 11408bcb0991SDimitry Andric 11418bcb0991SDimitry Andric DIELoc *Loc = new (DIEValueAllocator) DIELoc; 11428bcb0991SDimitry Andric DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc); 11438bcb0991SDimitry Andric DwarfExpr.setCallSiteParamValueFlag(); 11448bcb0991SDimitry Andric 11458bcb0991SDimitry Andric DwarfDebug::emitDebugLocValue(*Asm, nullptr, Param.getValue(), DwarfExpr); 11468bcb0991SDimitry Andric 11478bcb0991SDimitry Andric addBlock(*CallSiteDieParam, getDwarf5OrGNUAttr(dwarf::DW_AT_call_value), 11488bcb0991SDimitry Andric DwarfExpr.finalize()); 11498bcb0991SDimitry Andric 11508bcb0991SDimitry Andric CallSiteDIE.addChild(CallSiteDieParam); 11518bcb0991SDimitry Andric } 11528bcb0991SDimitry Andric } 11538bcb0991SDimitry Andric 11540b57cec5SDimitry Andric DIE *DwarfCompileUnit::constructImportedEntityDIE( 11550b57cec5SDimitry Andric const DIImportedEntity *Module) { 11560b57cec5SDimitry Andric DIE *IMDie = DIE::get(DIEValueAllocator, (dwarf::Tag)Module->getTag()); 11570b57cec5SDimitry Andric insertDIE(Module, IMDie); 11580b57cec5SDimitry Andric DIE *EntityDie; 11590b57cec5SDimitry Andric auto *Entity = Module->getEntity(); 11600b57cec5SDimitry Andric if (auto *NS = dyn_cast<DINamespace>(Entity)) 11610b57cec5SDimitry Andric EntityDie = getOrCreateNameSpace(NS); 11620b57cec5SDimitry Andric else if (auto *M = dyn_cast<DIModule>(Entity)) 11630b57cec5SDimitry Andric EntityDie = getOrCreateModule(M); 11640b57cec5SDimitry Andric else if (auto *SP = dyn_cast<DISubprogram>(Entity)) 11650b57cec5SDimitry Andric EntityDie = getOrCreateSubprogramDIE(SP); 11660b57cec5SDimitry Andric else if (auto *T = dyn_cast<DIType>(Entity)) 11670b57cec5SDimitry Andric EntityDie = getOrCreateTypeDIE(T); 11680b57cec5SDimitry Andric else if (auto *GV = dyn_cast<DIGlobalVariable>(Entity)) 11690b57cec5SDimitry Andric EntityDie = getOrCreateGlobalVariableDIE(GV, {}); 11700b57cec5SDimitry Andric else 11710b57cec5SDimitry Andric EntityDie = getDIE(Entity); 11720b57cec5SDimitry Andric assert(EntityDie); 11730b57cec5SDimitry Andric addSourceLine(*IMDie, Module->getLine(), Module->getFile()); 11740b57cec5SDimitry Andric addDIEEntry(*IMDie, dwarf::DW_AT_import, *EntityDie); 11750b57cec5SDimitry Andric StringRef Name = Module->getName(); 11760b57cec5SDimitry Andric if (!Name.empty()) 11770b57cec5SDimitry Andric addString(*IMDie, dwarf::DW_AT_name, Name); 11780b57cec5SDimitry Andric 11790b57cec5SDimitry Andric return IMDie; 11800b57cec5SDimitry Andric } 11810b57cec5SDimitry Andric 11820b57cec5SDimitry Andric void DwarfCompileUnit::finishSubprogramDefinition(const DISubprogram *SP) { 11830b57cec5SDimitry Andric DIE *D = getDIE(SP); 11840b57cec5SDimitry Andric if (DIE *AbsSPDIE = getAbstractSPDies().lookup(SP)) { 11850b57cec5SDimitry Andric if (D) 11860b57cec5SDimitry Andric // If this subprogram has an abstract definition, reference that 11870b57cec5SDimitry Andric addDIEEntry(*D, dwarf::DW_AT_abstract_origin, *AbsSPDIE); 11880b57cec5SDimitry Andric } else { 11890b57cec5SDimitry Andric assert(D || includeMinimalInlineScopes()); 11900b57cec5SDimitry Andric if (D) 11910b57cec5SDimitry Andric // And attach the attributes 11920b57cec5SDimitry Andric applySubprogramAttributesToDefinition(SP, *D); 11930b57cec5SDimitry Andric } 11940b57cec5SDimitry Andric } 11950b57cec5SDimitry Andric 11960b57cec5SDimitry Andric void DwarfCompileUnit::finishEntityDefinition(const DbgEntity *Entity) { 11970b57cec5SDimitry Andric DbgEntity *AbsEntity = getExistingAbstractEntity(Entity->getEntity()); 11980b57cec5SDimitry Andric 11990b57cec5SDimitry Andric auto *Die = Entity->getDIE(); 12000b57cec5SDimitry Andric /// Label may be used to generate DW_AT_low_pc, so put it outside 12010b57cec5SDimitry Andric /// if/else block. 12020b57cec5SDimitry Andric const DbgLabel *Label = nullptr; 12030b57cec5SDimitry Andric if (AbsEntity && AbsEntity->getDIE()) { 12040b57cec5SDimitry Andric addDIEEntry(*Die, dwarf::DW_AT_abstract_origin, *AbsEntity->getDIE()); 12050b57cec5SDimitry Andric Label = dyn_cast<const DbgLabel>(Entity); 12060b57cec5SDimitry Andric } else { 12070b57cec5SDimitry Andric if (const DbgVariable *Var = dyn_cast<const DbgVariable>(Entity)) 12080b57cec5SDimitry Andric applyVariableAttributes(*Var, *Die); 12090b57cec5SDimitry Andric else if ((Label = dyn_cast<const DbgLabel>(Entity))) 12100b57cec5SDimitry Andric applyLabelAttributes(*Label, *Die); 12110b57cec5SDimitry Andric else 12120b57cec5SDimitry Andric llvm_unreachable("DbgEntity must be DbgVariable or DbgLabel."); 12130b57cec5SDimitry Andric } 12140b57cec5SDimitry Andric 12150b57cec5SDimitry Andric if (Label) 12160b57cec5SDimitry Andric if (const auto *Sym = Label->getSymbol()) 12170b57cec5SDimitry Andric addLabelAddress(*Die, dwarf::DW_AT_low_pc, Sym); 12180b57cec5SDimitry Andric } 12190b57cec5SDimitry Andric 12200b57cec5SDimitry Andric DbgEntity *DwarfCompileUnit::getExistingAbstractEntity(const DINode *Node) { 12210b57cec5SDimitry Andric auto &AbstractEntities = getAbstractEntities(); 12220b57cec5SDimitry Andric auto I = AbstractEntities.find(Node); 12230b57cec5SDimitry Andric if (I != AbstractEntities.end()) 12240b57cec5SDimitry Andric return I->second.get(); 12250b57cec5SDimitry Andric return nullptr; 12260b57cec5SDimitry Andric } 12270b57cec5SDimitry Andric 12280b57cec5SDimitry Andric void DwarfCompileUnit::createAbstractEntity(const DINode *Node, 12290b57cec5SDimitry Andric LexicalScope *Scope) { 12300b57cec5SDimitry Andric assert(Scope && Scope->isAbstractScope()); 12310b57cec5SDimitry Andric auto &Entity = getAbstractEntities()[Node]; 12320b57cec5SDimitry Andric if (isa<const DILocalVariable>(Node)) { 12338bcb0991SDimitry Andric Entity = std::make_unique<DbgVariable>( 12340b57cec5SDimitry Andric cast<const DILocalVariable>(Node), nullptr /* IA */);; 12350b57cec5SDimitry Andric DU->addScopeVariable(Scope, cast<DbgVariable>(Entity.get())); 12360b57cec5SDimitry Andric } else if (isa<const DILabel>(Node)) { 12378bcb0991SDimitry Andric Entity = std::make_unique<DbgLabel>( 12380b57cec5SDimitry Andric cast<const DILabel>(Node), nullptr /* IA */); 12390b57cec5SDimitry Andric DU->addScopeLabel(Scope, cast<DbgLabel>(Entity.get())); 12400b57cec5SDimitry Andric } 12410b57cec5SDimitry Andric } 12420b57cec5SDimitry Andric 12430b57cec5SDimitry Andric void DwarfCompileUnit::emitHeader(bool UseOffsets) { 12440b57cec5SDimitry Andric // Don't bother labeling the .dwo unit, as its offset isn't used. 12450b57cec5SDimitry Andric if (!Skeleton && !DD->useSectionsAsReferences()) { 12460b57cec5SDimitry Andric LabelBegin = Asm->createTempSymbol("cu_begin"); 12475ffd83dbSDimitry Andric Asm->OutStreamer->emitLabel(LabelBegin); 12480b57cec5SDimitry Andric } 12490b57cec5SDimitry Andric 12500b57cec5SDimitry Andric dwarf::UnitType UT = Skeleton ? dwarf::DW_UT_split_compile 12510b57cec5SDimitry Andric : DD->useSplitDwarf() ? dwarf::DW_UT_skeleton 12520b57cec5SDimitry Andric : dwarf::DW_UT_compile; 12530b57cec5SDimitry Andric DwarfUnit::emitCommonHeader(UseOffsets, UT); 12540b57cec5SDimitry Andric if (DD->getDwarfVersion() >= 5 && UT != dwarf::DW_UT_compile) 12550b57cec5SDimitry Andric Asm->emitInt64(getDWOId()); 12560b57cec5SDimitry Andric } 12570b57cec5SDimitry Andric 12580b57cec5SDimitry Andric bool DwarfCompileUnit::hasDwarfPubSections() const { 12590b57cec5SDimitry Andric switch (CUNode->getNameTableKind()) { 12600b57cec5SDimitry Andric case DICompileUnit::DebugNameTableKind::None: 12610b57cec5SDimitry Andric return false; 12620b57cec5SDimitry Andric // Opting in to GNU Pubnames/types overrides the default to ensure these are 12630b57cec5SDimitry Andric // generated for things like Gold's gdb_index generation. 12640b57cec5SDimitry Andric case DICompileUnit::DebugNameTableKind::GNU: 12650b57cec5SDimitry Andric return true; 12660b57cec5SDimitry Andric case DICompileUnit::DebugNameTableKind::Default: 12670b57cec5SDimitry Andric return DD->tuneForGDB() && !includeMinimalInlineScopes() && 12680b57cec5SDimitry Andric !CUNode->isDebugDirectivesOnly() && 12690b57cec5SDimitry Andric DD->getAccelTableKind() != AccelTableKind::Apple && 12700b57cec5SDimitry Andric DD->getDwarfVersion() < 5; 12710b57cec5SDimitry Andric } 12720b57cec5SDimitry Andric llvm_unreachable("Unhandled DICompileUnit::DebugNameTableKind enum"); 12730b57cec5SDimitry Andric } 12740b57cec5SDimitry Andric 12750b57cec5SDimitry Andric /// addGlobalName - Add a new global name to the compile unit. 12760b57cec5SDimitry Andric void DwarfCompileUnit::addGlobalName(StringRef Name, const DIE &Die, 12770b57cec5SDimitry Andric const DIScope *Context) { 12780b57cec5SDimitry Andric if (!hasDwarfPubSections()) 12790b57cec5SDimitry Andric return; 12800b57cec5SDimitry Andric std::string FullName = getParentContextString(Context) + Name.str(); 12810b57cec5SDimitry Andric GlobalNames[FullName] = &Die; 12820b57cec5SDimitry Andric } 12830b57cec5SDimitry Andric 12840b57cec5SDimitry Andric void DwarfCompileUnit::addGlobalNameForTypeUnit(StringRef Name, 12850b57cec5SDimitry Andric const DIScope *Context) { 12860b57cec5SDimitry Andric if (!hasDwarfPubSections()) 12870b57cec5SDimitry Andric return; 12880b57cec5SDimitry Andric std::string FullName = getParentContextString(Context) + Name.str(); 12890b57cec5SDimitry Andric // Insert, allowing the entry to remain as-is if it's already present 12900b57cec5SDimitry Andric // This way the CU-level type DIE is preferred over the "can't describe this 12910b57cec5SDimitry Andric // type as a unit offset because it's not really in the CU at all, it's only 12920b57cec5SDimitry Andric // in a type unit" 12930b57cec5SDimitry Andric GlobalNames.insert(std::make_pair(std::move(FullName), &getUnitDie())); 12940b57cec5SDimitry Andric } 12950b57cec5SDimitry Andric 12960b57cec5SDimitry Andric /// Add a new global type to the unit. 12970b57cec5SDimitry Andric void DwarfCompileUnit::addGlobalType(const DIType *Ty, const DIE &Die, 12980b57cec5SDimitry Andric const DIScope *Context) { 12990b57cec5SDimitry Andric if (!hasDwarfPubSections()) 13000b57cec5SDimitry Andric return; 13010b57cec5SDimitry Andric std::string FullName = getParentContextString(Context) + Ty->getName().str(); 13020b57cec5SDimitry Andric GlobalTypes[FullName] = &Die; 13030b57cec5SDimitry Andric } 13040b57cec5SDimitry Andric 13050b57cec5SDimitry Andric void DwarfCompileUnit::addGlobalTypeUnitType(const DIType *Ty, 13060b57cec5SDimitry Andric const DIScope *Context) { 13070b57cec5SDimitry Andric if (!hasDwarfPubSections()) 13080b57cec5SDimitry Andric return; 13090b57cec5SDimitry Andric std::string FullName = getParentContextString(Context) + Ty->getName().str(); 13100b57cec5SDimitry Andric // Insert, allowing the entry to remain as-is if it's already present 13110b57cec5SDimitry Andric // This way the CU-level type DIE is preferred over the "can't describe this 13120b57cec5SDimitry Andric // type as a unit offset because it's not really in the CU at all, it's only 13130b57cec5SDimitry Andric // in a type unit" 13140b57cec5SDimitry Andric GlobalTypes.insert(std::make_pair(std::move(FullName), &getUnitDie())); 13150b57cec5SDimitry Andric } 13160b57cec5SDimitry Andric 13170b57cec5SDimitry Andric void DwarfCompileUnit::addVariableAddress(const DbgVariable &DV, DIE &Die, 13180b57cec5SDimitry Andric MachineLocation Location) { 13190b57cec5SDimitry Andric if (DV.hasComplexAddress()) 13200b57cec5SDimitry Andric addComplexAddress(DV, Die, dwarf::DW_AT_location, Location); 13210b57cec5SDimitry Andric else 13220b57cec5SDimitry Andric addAddress(Die, dwarf::DW_AT_location, Location); 13230b57cec5SDimitry Andric } 13240b57cec5SDimitry Andric 13250b57cec5SDimitry Andric /// Add an address attribute to a die based on the location provided. 13260b57cec5SDimitry Andric void DwarfCompileUnit::addAddress(DIE &Die, dwarf::Attribute Attribute, 13270b57cec5SDimitry Andric const MachineLocation &Location) { 13280b57cec5SDimitry Andric DIELoc *Loc = new (DIEValueAllocator) DIELoc; 13290b57cec5SDimitry Andric DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc); 13300b57cec5SDimitry Andric if (Location.isIndirect()) 13310b57cec5SDimitry Andric DwarfExpr.setMemoryLocationKind(); 13320b57cec5SDimitry Andric 13330b57cec5SDimitry Andric DIExpressionCursor Cursor({}); 13340b57cec5SDimitry Andric const TargetRegisterInfo &TRI = *Asm->MF->getSubtarget().getRegisterInfo(); 13350b57cec5SDimitry Andric if (!DwarfExpr.addMachineRegExpression(TRI, Cursor, Location.getReg())) 13360b57cec5SDimitry Andric return; 13370b57cec5SDimitry Andric DwarfExpr.addExpression(std::move(Cursor)); 13380b57cec5SDimitry Andric 13390b57cec5SDimitry Andric // Now attach the location information to the DIE. 13400b57cec5SDimitry Andric addBlock(Die, Attribute, DwarfExpr.finalize()); 1341480093f4SDimitry Andric 1342480093f4SDimitry Andric if (DwarfExpr.TagOffset) 1343480093f4SDimitry Andric addUInt(Die, dwarf::DW_AT_LLVM_tag_offset, dwarf::DW_FORM_data1, 1344480093f4SDimitry Andric *DwarfExpr.TagOffset); 13450b57cec5SDimitry Andric } 13460b57cec5SDimitry Andric 13470b57cec5SDimitry Andric /// Start with the address based on the location provided, and generate the 13480b57cec5SDimitry Andric /// DWARF information necessary to find the actual variable given the extra 13490b57cec5SDimitry Andric /// address information encoded in the DbgVariable, starting from the starting 13500b57cec5SDimitry Andric /// location. Add the DWARF information to the die. 13510b57cec5SDimitry Andric void DwarfCompileUnit::addComplexAddress(const DbgVariable &DV, DIE &Die, 13520b57cec5SDimitry Andric dwarf::Attribute Attribute, 13530b57cec5SDimitry Andric const MachineLocation &Location) { 13540b57cec5SDimitry Andric DIELoc *Loc = new (DIEValueAllocator) DIELoc; 13550b57cec5SDimitry Andric DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc); 13560b57cec5SDimitry Andric const DIExpression *DIExpr = DV.getSingleExpression(); 13570b57cec5SDimitry Andric DwarfExpr.addFragmentOffset(DIExpr); 13585ffd83dbSDimitry Andric DwarfExpr.setLocation(Location, DIExpr); 13590b57cec5SDimitry Andric 13600b57cec5SDimitry Andric DIExpressionCursor Cursor(DIExpr); 13610b57cec5SDimitry Andric 13625ffd83dbSDimitry Andric if (DIExpr->isEntryValue()) 13638bcb0991SDimitry Andric DwarfExpr.beginEntryValueExpression(Cursor); 13640b57cec5SDimitry Andric 13650b57cec5SDimitry Andric const TargetRegisterInfo &TRI = *Asm->MF->getSubtarget().getRegisterInfo(); 13660b57cec5SDimitry Andric if (!DwarfExpr.addMachineRegExpression(TRI, Cursor, Location.getReg())) 13670b57cec5SDimitry Andric return; 13680b57cec5SDimitry Andric DwarfExpr.addExpression(std::move(Cursor)); 13690b57cec5SDimitry Andric 13700b57cec5SDimitry Andric // Now attach the location information to the DIE. 13710b57cec5SDimitry Andric addBlock(Die, Attribute, DwarfExpr.finalize()); 1372480093f4SDimitry Andric 1373480093f4SDimitry Andric if (DwarfExpr.TagOffset) 1374480093f4SDimitry Andric addUInt(Die, dwarf::DW_AT_LLVM_tag_offset, dwarf::DW_FORM_data1, 1375480093f4SDimitry Andric *DwarfExpr.TagOffset); 13760b57cec5SDimitry Andric } 13770b57cec5SDimitry Andric 13780b57cec5SDimitry Andric /// Add a Dwarf loclistptr attribute data and value. 13790b57cec5SDimitry Andric void DwarfCompileUnit::addLocationList(DIE &Die, dwarf::Attribute Attribute, 13800b57cec5SDimitry Andric unsigned Index) { 1381*e8d8bef9SDimitry Andric dwarf::Form Form = (DD->getDwarfVersion() >= 5) 1382*e8d8bef9SDimitry Andric ? dwarf::DW_FORM_loclistx 1383*e8d8bef9SDimitry Andric : DD->getDwarfSectionOffsetForm(); 13840b57cec5SDimitry Andric Die.addValue(DIEValueAllocator, Attribute, Form, DIELocList(Index)); 13850b57cec5SDimitry Andric } 13860b57cec5SDimitry Andric 13870b57cec5SDimitry Andric void DwarfCompileUnit::applyVariableAttributes(const DbgVariable &Var, 13880b57cec5SDimitry Andric DIE &VariableDie) { 13890b57cec5SDimitry Andric StringRef Name = Var.getName(); 13900b57cec5SDimitry Andric if (!Name.empty()) 13910b57cec5SDimitry Andric addString(VariableDie, dwarf::DW_AT_name, Name); 13920b57cec5SDimitry Andric const auto *DIVar = Var.getVariable(); 13930b57cec5SDimitry Andric if (DIVar) 13940b57cec5SDimitry Andric if (uint32_t AlignInBytes = DIVar->getAlignInBytes()) 13950b57cec5SDimitry Andric addUInt(VariableDie, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata, 13960b57cec5SDimitry Andric AlignInBytes); 13970b57cec5SDimitry Andric 13980b57cec5SDimitry Andric addSourceLine(VariableDie, DIVar); 13990b57cec5SDimitry Andric addType(VariableDie, Var.getType()); 14000b57cec5SDimitry Andric if (Var.isArtificial()) 14010b57cec5SDimitry Andric addFlag(VariableDie, dwarf::DW_AT_artificial); 14020b57cec5SDimitry Andric } 14030b57cec5SDimitry Andric 14040b57cec5SDimitry Andric void DwarfCompileUnit::applyLabelAttributes(const DbgLabel &Label, 14050b57cec5SDimitry Andric DIE &LabelDie) { 14060b57cec5SDimitry Andric StringRef Name = Label.getName(); 14070b57cec5SDimitry Andric if (!Name.empty()) 14080b57cec5SDimitry Andric addString(LabelDie, dwarf::DW_AT_name, Name); 14090b57cec5SDimitry Andric const auto *DILabel = Label.getLabel(); 14100b57cec5SDimitry Andric addSourceLine(LabelDie, DILabel); 14110b57cec5SDimitry Andric } 14120b57cec5SDimitry Andric 14130b57cec5SDimitry Andric /// Add a Dwarf expression attribute data and value. 14140b57cec5SDimitry Andric void DwarfCompileUnit::addExpr(DIELoc &Die, dwarf::Form Form, 14150b57cec5SDimitry Andric const MCExpr *Expr) { 14160b57cec5SDimitry Andric Die.addValue(DIEValueAllocator, (dwarf::Attribute)0, Form, DIEExpr(Expr)); 14170b57cec5SDimitry Andric } 14180b57cec5SDimitry Andric 14190b57cec5SDimitry Andric void DwarfCompileUnit::applySubprogramAttributesToDefinition( 14200b57cec5SDimitry Andric const DISubprogram *SP, DIE &SPDie) { 14210b57cec5SDimitry Andric auto *SPDecl = SP->getDeclaration(); 14220b57cec5SDimitry Andric auto *Context = SPDecl ? SPDecl->getScope() : SP->getScope(); 14230b57cec5SDimitry Andric applySubprogramAttributes(SP, SPDie, includeMinimalInlineScopes()); 14240b57cec5SDimitry Andric addGlobalName(SP->getName(), SPDie, Context); 14250b57cec5SDimitry Andric } 14260b57cec5SDimitry Andric 14270b57cec5SDimitry Andric bool DwarfCompileUnit::isDwoUnit() const { 14280b57cec5SDimitry Andric return DD->useSplitDwarf() && Skeleton; 14290b57cec5SDimitry Andric } 14300b57cec5SDimitry Andric 14310b57cec5SDimitry Andric void DwarfCompileUnit::finishNonUnitTypeDIE(DIE& D, const DICompositeType *CTy) { 14320b57cec5SDimitry Andric constructTypeDIE(D, CTy); 14330b57cec5SDimitry Andric } 14340b57cec5SDimitry Andric 14350b57cec5SDimitry Andric bool DwarfCompileUnit::includeMinimalInlineScopes() const { 14360b57cec5SDimitry Andric return getCUNode()->getEmissionKind() == DICompileUnit::LineTablesOnly || 14370b57cec5SDimitry Andric (DD->useSplitDwarf() && !Skeleton); 14380b57cec5SDimitry Andric } 14390b57cec5SDimitry Andric 14400b57cec5SDimitry Andric void DwarfCompileUnit::addAddrTableBase() { 14410b57cec5SDimitry Andric const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering(); 14420b57cec5SDimitry Andric MCSymbol *Label = DD->getAddressPool().getLabel(); 14430b57cec5SDimitry Andric addSectionLabel(getUnitDie(), 1444*e8d8bef9SDimitry Andric DD->getDwarfVersion() >= 5 ? dwarf::DW_AT_addr_base 14450b57cec5SDimitry Andric : dwarf::DW_AT_GNU_addr_base, 14460b57cec5SDimitry Andric Label, TLOF.getDwarfAddrSection()->getBeginSymbol()); 14470b57cec5SDimitry Andric } 14480b57cec5SDimitry Andric 14490b57cec5SDimitry Andric void DwarfCompileUnit::addBaseTypeRef(DIEValueList &Die, int64_t Idx) { 14500b57cec5SDimitry Andric Die.addValue(DIEValueAllocator, (dwarf::Attribute)0, dwarf::DW_FORM_udata, 14510b57cec5SDimitry Andric new (DIEValueAllocator) DIEBaseTypeRef(this, Idx)); 14520b57cec5SDimitry Andric } 14530b57cec5SDimitry Andric 14540b57cec5SDimitry Andric void DwarfCompileUnit::createBaseTypeDIEs() { 14550b57cec5SDimitry Andric // Insert the base_type DIEs directly after the CU so that their offsets will 14560b57cec5SDimitry Andric // fit in the fixed size ULEB128 used inside the location expressions. 14570b57cec5SDimitry Andric // Maintain order by iterating backwards and inserting to the front of CU 14580b57cec5SDimitry Andric // child list. 14590b57cec5SDimitry Andric for (auto &Btr : reverse(ExprRefedBaseTypes)) { 14600b57cec5SDimitry Andric DIE &Die = getUnitDie().addChildFront( 14610b57cec5SDimitry Andric DIE::get(DIEValueAllocator, dwarf::DW_TAG_base_type)); 14620b57cec5SDimitry Andric SmallString<32> Str; 14630b57cec5SDimitry Andric addString(Die, dwarf::DW_AT_name, 14640b57cec5SDimitry Andric Twine(dwarf::AttributeEncodingString(Btr.Encoding) + 14650b57cec5SDimitry Andric "_" + Twine(Btr.BitSize)).toStringRef(Str)); 14660b57cec5SDimitry Andric addUInt(Die, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1, Btr.Encoding); 14670b57cec5SDimitry Andric addUInt(Die, dwarf::DW_AT_byte_size, None, Btr.BitSize / 8); 14680b57cec5SDimitry Andric 14690b57cec5SDimitry Andric Btr.Die = &Die; 14700b57cec5SDimitry Andric } 14710b57cec5SDimitry Andric } 1472