Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 1 | //===- llvm/CodeGen/DwarfCompileUnit.cpp - Dwarf Compile Units ------------===// |
Paul Robinson | 3443575 | 2017-03-01 00:14:42 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Paul Robinson | 3443575 | 2017-03-01 00:14:42 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file contains support for constructing a dwarf compile unit. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 13 | #include "DwarfCompileUnit.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 14 | #include "AddressPool.h" |
| 15 | #include "DwarfDebug.h" |
Adrian Prantl | 7813d9c | 2015-01-14 01:01:30 +0000 | [diff] [blame] | 16 | #include "DwarfExpression.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 17 | #include "DwarfUnit.h" |
| 18 | #include "llvm/ADT/None.h" |
| 19 | #include "llvm/ADT/STLExtras.h" |
Markus Lavin | b86ce21 | 2019-03-19 13:16:28 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/SmallString.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallVector.h" |
| 22 | #include "llvm/ADT/StringRef.h" |
| 23 | #include "llvm/BinaryFormat/Dwarf.h" |
| 24 | #include "llvm/CodeGen/AsmPrinter.h" |
| 25 | #include "llvm/CodeGen/DIE.h" |
| 26 | #include "llvm/CodeGen/LexicalScopes.h" |
David Blaikie | cda2aa8 | 2014-10-04 16:24:00 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/MachineFunction.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/MachineInstr.h" |
| 29 | #include "llvm/CodeGen/MachineOperand.h" |
David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/TargetFrameLowering.h" |
David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/TargetRegisterInfo.h" |
| 32 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 33 | #include "llvm/IR/DataLayout.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 34 | #include "llvm/IR/DebugInfo.h" |
| 35 | #include "llvm/IR/DebugInfoMetadata.h" |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 36 | #include "llvm/IR/GlobalVariable.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 37 | #include "llvm/MC/MCSection.h" |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 38 | #include "llvm/MC/MCStreamer.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 39 | #include "llvm/MC/MCSymbol.h" |
David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 40 | #include "llvm/MC/MachineLocation.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 41 | #include "llvm/Support/Casting.h" |
David Blaikie | 6054e65 | 2018-03-23 23:58:19 +0000 | [diff] [blame] | 42 | #include "llvm/Target/TargetLoweringObjectFile.h" |
David Blaikie | cda2aa8 | 2014-10-04 16:24:00 +0000 | [diff] [blame] | 43 | #include "llvm/Target/TargetMachine.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 44 | #include "llvm/Target/TargetOptions.h" |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 45 | #include <algorithm> |
| 46 | #include <cassert> |
| 47 | #include <cstdint> |
| 48 | #include <iterator> |
| 49 | #include <memory> |
| 50 | #include <string> |
| 51 | #include <utility> |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 52 | |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 53 | using namespace llvm; |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 54 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 55 | DwarfCompileUnit::DwarfCompileUnit(unsigned UID, const DICompileUnit *Node, |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 56 | AsmPrinter *A, DwarfDebug *DW, |
| 57 | DwarfFile *DWU) |
Eugene Zelenko | 6e07bfd | 2017-08-17 21:26:39 +0000 | [diff] [blame] | 58 | : DwarfUnit(dwarf::DW_TAG_compile_unit, Node, A, DW, DWU), UniqueID(UID) { |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 59 | insertDIE(Node, &getUnitDie()); |
Amjad Aboud | 8bbce8a | 2016-02-01 14:09:41 +0000 | [diff] [blame] | 60 | MacroLabelBegin = Asm->createTempSymbol("cu_macro_begin"); |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | /// addLabelAddress - Add a dwarf label attribute data and value using |
| 64 | /// DW_FORM_addr or DW_FORM_GNU_addr_index. |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 65 | void DwarfCompileUnit::addLabelAddress(DIE &Die, dwarf::Attribute Attribute, |
| 66 | const MCSymbol *Label) { |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 67 | // Don't use the address pool in non-fission or in the skeleton unit itself. |
| 68 | // FIXME: Once GDB supports this, it's probably worthwhile using the address |
| 69 | // pool from the skeleton - maybe even in non-fission (possibly fewer |
| 70 | // relocations by sharing them in the pool, but we have other ideas about how |
| 71 | // to reduce the number of relocations as well/instead). |
David Blaikie | 161dd3c | 2018-10-20 06:02:15 +0000 | [diff] [blame] | 72 | if ((!DD->useSplitDwarf() || !Skeleton) && DD->getDwarfVersion() < 5) |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 73 | return addLocalLabelAddress(Die, Attribute, Label); |
| 74 | |
| 75 | if (Label) |
| 76 | DD->addArangeLabel(SymbolCU(this, Label)); |
| 77 | |
| 78 | unsigned idx = DD->getAddressPool().getIndex(Label); |
David Blaikie | 161dd3c | 2018-10-20 06:02:15 +0000 | [diff] [blame] | 79 | Die.addValue(DIEValueAllocator, Attribute, |
| 80 | DD->getDwarfVersion() >= 5 ? dwarf::DW_FORM_addrx |
| 81 | : dwarf::DW_FORM_GNU_addr_index, |
Duncan P. N. Exon Smith | 4fb1f9c | 2015-06-25 23:46:41 +0000 | [diff] [blame] | 82 | DIEInteger(idx)); |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | void DwarfCompileUnit::addLocalLabelAddress(DIE &Die, |
| 86 | dwarf::Attribute Attribute, |
| 87 | const MCSymbol *Label) { |
| 88 | if (Label) |
| 89 | DD->addArangeLabel(SymbolCU(this, Label)); |
| 90 | |
Duncan P. N. Exon Smith | 815a6eb5 | 2015-05-27 22:31:41 +0000 | [diff] [blame] | 91 | if (Label) |
Duncan P. N. Exon Smith | 4fb1f9c | 2015-06-25 23:46:41 +0000 | [diff] [blame] | 92 | Die.addValue(DIEValueAllocator, Attribute, dwarf::DW_FORM_addr, |
| 93 | DIELabel(Label)); |
Duncan P. N. Exon Smith | 815a6eb5 | 2015-05-27 22:31:41 +0000 | [diff] [blame] | 94 | else |
Duncan P. N. Exon Smith | 4fb1f9c | 2015-06-25 23:46:41 +0000 | [diff] [blame] | 95 | Die.addValue(DIEValueAllocator, Attribute, dwarf::DW_FORM_addr, |
| 96 | DIEInteger(0)); |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Paul Robinson | 612e89d | 2018-01-12 19:17:50 +0000 | [diff] [blame] | 99 | unsigned DwarfCompileUnit::getOrCreateSourceID(const DIFile *File) { |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 100 | // If we print assembly, we can't separate .file entries according to |
| 101 | // compile units. Thus all files will belong to the default compile unit. |
| 102 | |
| 103 | // FIXME: add a better feature test than hasRawTextSupport. Even better, |
| 104 | // extend .file to support this. |
Paul Robinson | 612e89d | 2018-01-12 19:17:50 +0000 | [diff] [blame] | 105 | unsigned CUID = Asm->OutStreamer->hasRawTextSupport() ? 0 : getUniqueID(); |
| 106 | if (!File) |
Eric Christopher | 798e83b | 2019-04-04 23:34:38 +0000 | [diff] [blame] | 107 | return Asm->OutStreamer->EmitDwarfFileDirective(0, "", "", None, None, CUID); |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 108 | return Asm->OutStreamer->EmitDwarfFileDirective( |
Scott Linder | 16c7bda | 2018-02-23 23:01:06 +0000 | [diff] [blame] | 109 | 0, File->getDirectory(), File->getFilename(), getMD5AsBytes(File), |
| 110 | File->getSource(), CUID); |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Duncan P. N. Exon Smith | 60635e3 | 2015-04-21 18:44:06 +0000 | [diff] [blame] | 113 | DIE *DwarfCompileUnit::getOrCreateGlobalVariableDIE( |
Adrian Prantl | bceaaa9 | 2016-12-20 02:09:43 +0000 | [diff] [blame] | 114 | const DIGlobalVariable *GV, ArrayRef<GlobalExpr> GlobalExprs) { |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 115 | // Check for pre-existence. |
| 116 | if (DIE *Die = getDIE(GV)) |
| 117 | return Die; |
| 118 | |
Duncan P. N. Exon Smith | e686f15 | 2015-04-06 23:27:40 +0000 | [diff] [blame] | 119 | assert(GV); |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 120 | |
Duncan P. N. Exon Smith | be9e4fe | 2015-04-20 18:32:29 +0000 | [diff] [blame] | 121 | auto *GVContext = GV->getScope(); |
Fangrui Song | da82ce9 | 2019-05-07 02:06:37 +0000 | [diff] [blame] | 122 | const DIType *GTy = GV->getType(); |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 123 | |
David Blaikie | 49cfc8c | 2014-10-23 19:12:43 +0000 | [diff] [blame] | 124 | // Construct the context before querying for the existence of the DIE in |
| 125 | // case such construction creates the DIE. |
Adrian Prantl | 6ed5706 | 2019-04-08 19:13:55 +0000 | [diff] [blame] | 126 | auto *CB = GVContext ? dyn_cast<DICommonBlock>(GVContext) : nullptr; |
| 127 | DIE *ContextDIE = CB ? getOrCreateCommonBlock(CB, GlobalExprs) |
| 128 | : getOrCreateContextDIE(GVContext); |
David Blaikie | 49cfc8c | 2014-10-23 19:12:43 +0000 | [diff] [blame] | 129 | |
Amjad Aboud | 72da939 | 2016-04-30 01:44:07 +0000 | [diff] [blame] | 130 | // Add to map. |
| 131 | DIE *VariableDIE = &createAndAddDIE(GV->getTag(), *ContextDIE, GV); |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 132 | DIScope *DeclContext; |
Duncan P. N. Exon Smith | b105564 | 2015-04-16 01:01:28 +0000 | [diff] [blame] | 133 | if (auto *SDMDecl = GV->getStaticDataMemberDeclaration()) { |
Fangrui Song | da82ce9 | 2019-05-07 02:06:37 +0000 | [diff] [blame] | 134 | DeclContext = SDMDecl->getScope(); |
Duncan P. N. Exon Smith | b105564 | 2015-04-16 01:01:28 +0000 | [diff] [blame] | 135 | assert(SDMDecl->isStaticMember() && "Expected static member decl"); |
Duncan P. N. Exon Smith | 7348dda | 2015-04-14 02:22:36 +0000 | [diff] [blame] | 136 | assert(GV->isDefinition()); |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 137 | // We need the declaration DIE that is in the static member's class. |
David Blaikie | 49cfc8c | 2014-10-23 19:12:43 +0000 | [diff] [blame] | 138 | DIE *VariableSpecDIE = getOrCreateStaticMemberDIE(SDMDecl); |
| 139 | addDIEEntry(*VariableDIE, dwarf::DW_AT_specification, *VariableSpecDIE); |
Adrian Prantl | 3502f20 | 2016-11-08 22:11:38 +0000 | [diff] [blame] | 140 | // If the global variable's type is different from the one in the class |
| 141 | // member type, assume that it's more specific and also emit it. |
Fangrui Song | da82ce9 | 2019-05-07 02:06:37 +0000 | [diff] [blame] | 142 | if (GTy != SDMDecl->getBaseType()) |
Adrian Prantl | 3502f20 | 2016-11-08 22:11:38 +0000 | [diff] [blame] | 143 | addType(*VariableDIE, GTy); |
David Blaikie | 49cfc8c | 2014-10-23 19:12:43 +0000 | [diff] [blame] | 144 | } else { |
Duncan P. N. Exon Smith | 7348dda | 2015-04-14 02:22:36 +0000 | [diff] [blame] | 145 | DeclContext = GV->getScope(); |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 146 | // Add name and type. |
Duncan P. N. Exon Smith | 7348dda | 2015-04-14 02:22:36 +0000 | [diff] [blame] | 147 | addString(*VariableDIE, dwarf::DW_AT_name, GV->getDisplayName()); |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 148 | addType(*VariableDIE, GTy); |
| 149 | |
| 150 | // Add scoping info. |
Duncan P. N. Exon Smith | 7348dda | 2015-04-14 02:22:36 +0000 | [diff] [blame] | 151 | if (!GV->isLocalToUnit()) |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 152 | addFlag(*VariableDIE, dwarf::DW_AT_external); |
| 153 | |
| 154 | // Add line number info. |
| 155 | addSourceLine(*VariableDIE, GV); |
| 156 | } |
| 157 | |
Duncan P. N. Exon Smith | 7348dda | 2015-04-14 02:22:36 +0000 | [diff] [blame] | 158 | if (!GV->isDefinition()) |
David Blaikie | 49cfc8c | 2014-10-23 19:12:43 +0000 | [diff] [blame] | 159 | addFlag(*VariableDIE, dwarf::DW_AT_declaration); |
David Blaikie | 877354a | 2015-04-14 18:08:25 +0000 | [diff] [blame] | 160 | else |
| 161 | addGlobalName(GV->getName(), *VariableDIE, DeclContext); |
David Blaikie | 49cfc8c | 2014-10-23 19:12:43 +0000 | [diff] [blame] | 162 | |
Victor Leschuk | 3c98998 | 2016-10-26 11:59:03 +0000 | [diff] [blame] | 163 | if (uint32_t AlignInBytes = GV->getAlignInBytes()) |
| 164 | addUInt(*VariableDIE, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata, |
| 165 | AlignInBytes); |
| 166 | |
Matthew Voss | f8ab35a | 2018-10-03 18:44:53 +0000 | [diff] [blame] | 167 | if (MDTuple *TP = GV->getTemplateParams()) |
| 168 | addTemplateParams(*VariableDIE, DINodeArray(TP)); |
| 169 | |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 170 | // Add location. |
Adrian Prantl | 6ed5706 | 2019-04-08 19:13:55 +0000 | [diff] [blame] | 171 | addLocationAttribute(VariableDIE, GV, GlobalExprs); |
| 172 | |
| 173 | return VariableDIE; |
| 174 | } |
| 175 | |
| 176 | void DwarfCompileUnit::addLocationAttribute( |
| 177 | DIE *VariableDIE, const DIGlobalVariable *GV, ArrayRef<GlobalExpr> GlobalExprs) { |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 178 | bool addToAccelTable = false; |
Adrian Prantl | bceaaa9 | 2016-12-20 02:09:43 +0000 | [diff] [blame] | 179 | DIELoc *Loc = nullptr; |
Alexey Bataev | f3a9150 | 2019-02-05 19:33:47 +0000 | [diff] [blame] | 180 | Optional<unsigned> NVPTXAddressSpace; |
Adrian Prantl | bceaaa9 | 2016-12-20 02:09:43 +0000 | [diff] [blame] | 181 | std::unique_ptr<DIEDwarfExpression> DwarfExpr; |
Adrian Prantl | bceaaa9 | 2016-12-20 02:09:43 +0000 | [diff] [blame] | 182 | for (const auto &GE : GlobalExprs) { |
| 183 | const GlobalVariable *Global = GE.Var; |
| 184 | const DIExpression *Expr = GE.Expr; |
Adrian Prantl | b216e3e | 2017-03-23 23:35:09 +0000 | [diff] [blame] | 185 | |
Adrian Prantl | bceaaa9 | 2016-12-20 02:09:43 +0000 | [diff] [blame] | 186 | // For compatibility with DWARF 3 and earlier, |
| 187 | // DW_AT_location(DW_OP_constu, X, DW_OP_stack_value) becomes |
| 188 | // DW_AT_const_value(X). |
| 189 | if (GlobalExprs.size() == 1 && Expr && Expr->isConstant()) { |
Adrian Prantl | bceaaa9 | 2016-12-20 02:09:43 +0000 | [diff] [blame] | 190 | addToAccelTable = true; |
Adrian Prantl | b216e3e | 2017-03-23 23:35:09 +0000 | [diff] [blame] | 191 | addConstantValue(*VariableDIE, /*Unsigned=*/true, Expr->getElement(1)); |
| 192 | break; |
| 193 | } |
| 194 | |
| 195 | // We cannot describe the location of dllimport'd variables: the |
| 196 | // computation of their address requires loads from the IAT. |
| 197 | if (Global && Global->hasDLLImportStorageClass()) |
| 198 | continue; |
| 199 | |
| 200 | // Nothing to describe without address or constant. |
| 201 | if (!Global && (!Expr || !Expr->isConstant())) |
| 202 | continue; |
| 203 | |
Lei Liu | 8e422b8 | 2018-08-01 23:46:49 +0000 | [diff] [blame] | 204 | if (Global && Global->isThreadLocal() && |
| 205 | !Asm->getObjFileLowering().supportDebugThreadLocalLocation()) |
| 206 | continue; |
| 207 | |
Adrian Prantl | b216e3e | 2017-03-23 23:35:09 +0000 | [diff] [blame] | 208 | if (!Loc) { |
| 209 | addToAccelTable = true; |
| 210 | Loc = new (DIEValueAllocator) DIELoc; |
| 211 | DwarfExpr = llvm::make_unique<DIEDwarfExpression>(*Asm, *this, *Loc); |
| 212 | } |
| 213 | |
Alexey Bataev | f3a9150 | 2019-02-05 19:33:47 +0000 | [diff] [blame] | 214 | if (Expr) { |
| 215 | // According to |
| 216 | // https://docs.nvidia.com/cuda/archive/10.0/ptx-writers-guide-to-interoperability/index.html#cuda-specific-dwarf |
| 217 | // cuda-gdb requires DW_AT_address_class for all variables to be able to |
| 218 | // correctly interpret address space of the variable address. |
| 219 | // Decode DW_OP_constu <DWARF Address Space> DW_OP_swap DW_OP_xderef |
| 220 | // sequence for the NVPTX + gdb target. |
| 221 | unsigned LocalNVPTXAddressSpace; |
| 222 | if (Asm->TM.getTargetTriple().isNVPTX() && DD->tuneForGDB()) { |
| 223 | const DIExpression *NewExpr = |
| 224 | DIExpression::extractAddressClass(Expr, LocalNVPTXAddressSpace); |
| 225 | if (NewExpr != Expr) { |
| 226 | Expr = NewExpr; |
| 227 | NVPTXAddressSpace = LocalNVPTXAddressSpace; |
| 228 | } |
| 229 | } |
Mikael Holmen | a9e3153 | 2018-01-29 12:37:30 +0000 | [diff] [blame] | 230 | DwarfExpr->addFragmentOffset(Expr); |
Alexey Bataev | f3a9150 | 2019-02-05 19:33:47 +0000 | [diff] [blame] | 231 | } |
Mikael Holmen | a9e3153 | 2018-01-29 12:37:30 +0000 | [diff] [blame] | 232 | |
Adrian Prantl | b216e3e | 2017-03-23 23:35:09 +0000 | [diff] [blame] | 233 | if (Global) { |
| 234 | const MCSymbol *Sym = Asm->getSymbol(Global); |
| 235 | if (Global->isThreadLocal()) { |
Chih-Hung Hsieh | 9f9e468 | 2018-02-28 17:48:55 +0000 | [diff] [blame] | 236 | if (Asm->TM.useEmulatedTLS()) { |
Adrian Prantl | b216e3e | 2017-03-23 23:35:09 +0000 | [diff] [blame] | 237 | // TODO: add debug info for emulated thread local mode. |
Adrian Prantl | bceaaa9 | 2016-12-20 02:09:43 +0000 | [diff] [blame] | 238 | } else { |
Adrian Prantl | b216e3e | 2017-03-23 23:35:09 +0000 | [diff] [blame] | 239 | // FIXME: Make this work with -gsplit-dwarf. |
| 240 | unsigned PointerSize = Asm->getDataLayout().getPointerSize(); |
| 241 | assert((PointerSize == 4 || PointerSize == 8) && |
| 242 | "Add support for other sizes if necessary"); |
| 243 | // Based on GCC's support for TLS: |
| 244 | if (!DD->useSplitDwarf()) { |
| 245 | // 1) Start with a constNu of the appropriate pointer size |
| 246 | addUInt(*Loc, dwarf::DW_FORM_data1, |
| 247 | PointerSize == 4 ? dwarf::DW_OP_const4u |
| 248 | : dwarf::DW_OP_const8u); |
| 249 | // 2) containing the (relocated) offset of the TLS variable |
| 250 | // within the module's TLS block. |
| 251 | addExpr(*Loc, dwarf::DW_FORM_udata, |
| 252 | Asm->getObjFileLowering().getDebugThreadLocalSymbol(Sym)); |
| 253 | } else { |
| 254 | addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_const_index); |
| 255 | addUInt(*Loc, dwarf::DW_FORM_udata, |
| 256 | DD->getAddressPool().getIndex(Sym, /* TLS */ true)); |
| 257 | } |
| 258 | // 3) followed by an OP to make the debugger do a TLS lookup. |
| 259 | addUInt(*Loc, dwarf::DW_FORM_data1, |
| 260 | DD->useGNUTLSOpcode() ? dwarf::DW_OP_GNU_push_tls_address |
| 261 | : dwarf::DW_OP_form_tls_address); |
Adrian Prantl | bceaaa9 | 2016-12-20 02:09:43 +0000 | [diff] [blame] | 262 | } |
Adrian Prantl | b216e3e | 2017-03-23 23:35:09 +0000 | [diff] [blame] | 263 | } else { |
| 264 | DD->addArangeLabel(SymbolCU(this, Sym)); |
| 265 | addOpAddress(*Loc, Sym); |
Adrian Prantl | bceaaa9 | 2016-12-20 02:09:43 +0000 | [diff] [blame] | 266 | } |
Adrian Prantl | b216e3e | 2017-03-23 23:35:09 +0000 | [diff] [blame] | 267 | } |
Adrian Prantl | 4b542c6 | 2018-04-27 22:05:31 +0000 | [diff] [blame] | 268 | // Global variables attached to symbols are memory locations. |
| 269 | // It would be better if this were unconditional, but malformed input that |
| 270 | // mixes non-fragments and fragments for the same variable is too expensive |
| 271 | // to detect in the verifier. |
Adrian Prantl | 3edc63a | 2018-05-04 16:10:43 +0000 | [diff] [blame] | 272 | if (DwarfExpr->isUnknownLocation()) |
Adrian Prantl | 4b542c6 | 2018-04-27 22:05:31 +0000 | [diff] [blame] | 273 | DwarfExpr->setMemoryLocationKind(); |
| 274 | DwarfExpr->addExpression(Expr); |
Adrian Prantl | 73ec065 | 2016-12-16 19:39:01 +0000 | [diff] [blame] | 275 | } |
Alexey Bataev | f3a9150 | 2019-02-05 19:33:47 +0000 | [diff] [blame] | 276 | if (Asm->TM.getTargetTriple().isNVPTX() && DD->tuneForGDB()) { |
| 277 | // According to |
| 278 | // https://docs.nvidia.com/cuda/archive/10.0/ptx-writers-guide-to-interoperability/index.html#cuda-specific-dwarf |
| 279 | // cuda-gdb requires DW_AT_address_class for all variables to be able to |
| 280 | // correctly interpret address space of the variable address. |
| 281 | const unsigned NVPTX_ADDR_global_space = 5; |
| 282 | addUInt(*VariableDIE, dwarf::DW_AT_address_class, dwarf::DW_FORM_data1, |
| 283 | NVPTXAddressSpace ? *NVPTXAddressSpace : NVPTX_ADDR_global_space); |
| 284 | } |
Adrian Prantl | bceaaa9 | 2016-12-20 02:09:43 +0000 | [diff] [blame] | 285 | if (Loc) |
| 286 | addBlock(*VariableDIE, dwarf::DW_AT_location, DwarfExpr->finalize()); |
| 287 | |
| 288 | if (DD->useAllLinkageNames()) |
| 289 | addLinkageName(*VariableDIE, GV->getLinkageName()); |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 290 | |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 291 | if (addToAccelTable) { |
David Blaikie | 66cf14d | 2018-08-16 21:29:55 +0000 | [diff] [blame] | 292 | DD->addAccelName(*CUNode, GV->getName(), *VariableDIE); |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 293 | |
| 294 | // If the linkage name is different than the name, go ahead and output |
| 295 | // that as well into the name table. |
Pavel Labath | 2a6afe5 | 2018-05-14 14:13:20 +0000 | [diff] [blame] | 296 | if (GV->getLinkageName() != "" && GV->getName() != GV->getLinkageName() && |
| 297 | DD->useAllLinkageNames()) |
David Blaikie | 66cf14d | 2018-08-16 21:29:55 +0000 | [diff] [blame] | 298 | DD->addAccelName(*CUNode, GV->getLinkageName(), *VariableDIE); |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 299 | } |
Adrian Prantl | 6ed5706 | 2019-04-08 19:13:55 +0000 | [diff] [blame] | 300 | } |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 301 | |
Adrian Prantl | 6ed5706 | 2019-04-08 19:13:55 +0000 | [diff] [blame] | 302 | DIE *DwarfCompileUnit::getOrCreateCommonBlock( |
| 303 | const DICommonBlock *CB, ArrayRef<GlobalExpr> GlobalExprs) { |
| 304 | // Construct the context before querying for the existence of the DIE in case |
| 305 | // such construction creates the DIE. |
| 306 | DIE *ContextDIE = getOrCreateContextDIE(CB->getScope()); |
| 307 | |
| 308 | if (DIE *NDie = getDIE(CB)) |
| 309 | return NDie; |
| 310 | DIE &NDie = createAndAddDIE(dwarf::DW_TAG_common_block, *ContextDIE, CB); |
| 311 | StringRef Name = CB->getName().empty() ? "_BLNK_" : CB->getName(); |
| 312 | addString(NDie, dwarf::DW_AT_name, Name); |
| 313 | addGlobalName(Name, NDie, CB->getScope()); |
| 314 | if (CB->getFile()) |
| 315 | addSourceLine(NDie, CB->getLineNo(), CB->getFile()); |
| 316 | if (DIGlobalVariable *V = CB->getDecl()) |
| 317 | getCU().addLocationAttribute(&NDie, V, GlobalExprs); |
| 318 | return &NDie; |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | void DwarfCompileUnit::addRange(RangeSpan Range) { |
| 322 | bool SameAsPrevCU = this == DD->getPrevCU(); |
| 323 | DD->setPrevCU(this); |
| 324 | // If we have no current ranges just add the range and return, otherwise, |
| 325 | // check the current section and CU against the previous section and CU we |
| 326 | // emitted into and the subprogram was contained within. If these are the |
| 327 | // same then extend our current range, otherwise add this as a new range. |
| 328 | if (CURanges.empty() || !SameAsPrevCU || |
| 329 | (&CURanges.back().getEnd()->getSection() != |
| 330 | &Range.getEnd()->getSection())) { |
| 331 | CURanges.push_back(Range); |
David Blaikie | 60fddac | 2018-10-24 23:36:29 +0000 | [diff] [blame] | 332 | DD->addSectionLabel(Range.getStart()); |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 333 | return; |
| 334 | } |
| 335 | |
| 336 | CURanges.back().setEnd(Range.getEnd()); |
| 337 | } |
| 338 | |
Rafael Espindola | 063d725 | 2015-03-10 16:58:10 +0000 | [diff] [blame] | 339 | void DwarfCompileUnit::initStmtList() { |
Alexey Bataev | d4dd721 | 2018-08-01 19:38:20 +0000 | [diff] [blame] | 340 | if (CUNode->isDebugDirectivesOnly()) |
| 341 | return; |
| 342 | |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 343 | // Define start line table label for each Compile Unit. |
Alexey Bataev | bff3608 | 2018-03-23 13:35:54 +0000 | [diff] [blame] | 344 | MCSymbol *LineTableStartSym; |
| 345 | const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering(); |
| 346 | if (DD->useSectionsAsReferences()) { |
| 347 | LineTableStartSym = TLOF.getDwarfLineSection()->getBeginSymbol(); |
| 348 | } else { |
| 349 | LineTableStartSym = |
| 350 | Asm->OutStreamer->getDwarfLineTableSymbol(getUniqueID()); |
| 351 | } |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 352 | |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 353 | // DW_AT_stmt_list is a offset of line number information for this |
| 354 | // compile unit in debug_line section. For split dwarf this is |
| 355 | // left in the skeleton CU and so not included. |
| 356 | // The line table entries are not always emitted in assembly, so it |
| 357 | // is not okay to use line_table_start here. |
Duncan P. N. Exon Smith | 4fb1f9c | 2015-06-25 23:46:41 +0000 | [diff] [blame] | 358 | StmtListValue = |
Greg Clayton | 35630c3 | 2016-12-01 18:56:29 +0000 | [diff] [blame] | 359 | addSectionLabel(getUnitDie(), dwarf::DW_AT_stmt_list, LineTableStartSym, |
Duncan P. N. Exon Smith | 4fb1f9c | 2015-06-25 23:46:41 +0000 | [diff] [blame] | 360 | TLOF.getDwarfLineSection()->getBeginSymbol()); |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | void DwarfCompileUnit::applyStmtList(DIE &D) { |
Duncan P. N. Exon Smith | 4fb1f9c | 2015-06-25 23:46:41 +0000 | [diff] [blame] | 364 | D.addValue(DIEValueAllocator, *StmtListValue); |
David Blaikie | 37c5231 | 2014-10-04 15:49:50 +0000 | [diff] [blame] | 365 | } |
| 366 | |
David Blaikie | 14499a7 | 2014-10-04 15:58:47 +0000 | [diff] [blame] | 367 | void DwarfCompileUnit::attachLowHighPC(DIE &D, const MCSymbol *Begin, |
| 368 | const MCSymbol *End) { |
| 369 | assert(Begin && "Begin label should not be null!"); |
| 370 | assert(End && "End label should not be null!"); |
| 371 | assert(Begin->isDefined() && "Invalid starting label"); |
| 372 | assert(End->isDefined() && "Invalid end label"); |
| 373 | |
| 374 | addLabelAddress(D, dwarf::DW_AT_low_pc, Begin); |
| 375 | if (DD->getDwarfVersion() < 4) |
| 376 | addLabelAddress(D, dwarf::DW_AT_high_pc, End); |
| 377 | else |
| 378 | addLabelDelta(D, dwarf::DW_AT_high_pc, End, Begin); |
| 379 | } |
| 380 | |
David Blaikie | cda2aa8 | 2014-10-04 16:24:00 +0000 | [diff] [blame] | 381 | // Find DIE for the given subprogram and attach appropriate DW_AT_low_pc |
| 382 | // and DW_AT_high_pc attributes. If there are global variables in this |
| 383 | // scope then create and insert DIEs for these variables. |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 384 | DIE &DwarfCompileUnit::updateSubprogramScopeDIE(const DISubprogram *SP) { |
David Blaikie | 3a443c2 | 2014-11-04 22:12:25 +0000 | [diff] [blame] | 385 | DIE *SPDie = getOrCreateSubprogramDIE(SP, includeMinimalInlineScopes()); |
David Blaikie | cda2aa8 | 2014-10-04 16:24:00 +0000 | [diff] [blame] | 386 | |
Rafael Espindola | 07c03d3 | 2015-03-05 02:05:42 +0000 | [diff] [blame] | 387 | attachLowHighPC(*SPDie, Asm->getFunctionBegin(), Asm->getFunctionEnd()); |
David Blaikie | c53e18d | 2016-05-24 21:19:28 +0000 | [diff] [blame] | 388 | if (DD->useAppleExtensionAttributes() && |
| 389 | !DD->getCurrentFunction()->getTarget().Options.DisableFramePointerElim( |
David Blaikie | cda2aa8 | 2014-10-04 16:24:00 +0000 | [diff] [blame] | 390 | *DD->getCurrentFunction())) |
| 391 | addFlag(*SPDie, dwarf::DW_AT_APPLE_omit_frame_ptr); |
| 392 | |
| 393 | // Only include DW_AT_frame_base in full debug info |
David Blaikie | 3a443c2 | 2014-11-04 22:12:25 +0000 | [diff] [blame] | 394 | if (!includeMinimalInlineScopes()) { |
Alexey Bataev | 7ae86fe | 2018-07-26 16:10:05 +0000 | [diff] [blame] | 395 | if (Asm->MF->getTarget().getTargetTriple().isNVPTX()) { |
| 396 | DIELoc *Loc = new (DIEValueAllocator) DIELoc; |
| 397 | addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_call_frame_cfa); |
| 398 | addBlock(*SPDie, dwarf::DW_AT_frame_base, Loc); |
| 399 | } else { |
| 400 | const TargetRegisterInfo *RI = Asm->MF->getSubtarget().getRegisterInfo(); |
| 401 | MachineLocation Location(RI->getFrameRegister(*Asm->MF)); |
| 402 | if (RI->isPhysicalRegister(Location.getReg())) |
| 403 | addAddress(*SPDie, dwarf::DW_AT_frame_base, Location); |
| 404 | } |
David Blaikie | cda2aa8 | 2014-10-04 16:24:00 +0000 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | // Add name to the name table, we do this here because we're guaranteed |
| 408 | // to have concrete versions of our DW_TAG_subprogram nodes. |
David Blaikie | 66cf14d | 2018-08-16 21:29:55 +0000 | [diff] [blame] | 409 | DD->addSubprogramNames(*CUNode, SP, *SPDie); |
David Blaikie | cda2aa8 | 2014-10-04 16:24:00 +0000 | [diff] [blame] | 410 | |
| 411 | return *SPDie; |
| 412 | } |
| 413 | |
David Blaikie | 9c65b13 | 2014-10-08 22:20:02 +0000 | [diff] [blame] | 414 | // Construct a DIE for this scope. |
| 415 | void DwarfCompileUnit::constructScopeDIE( |
Duncan P. N. Exon Smith | 827200c | 2015-06-25 23:52:10 +0000 | [diff] [blame] | 416 | LexicalScope *Scope, SmallVectorImpl<DIE *> &FinalChildren) { |
David Blaikie | 9c65b13 | 2014-10-08 22:20:02 +0000 | [diff] [blame] | 417 | if (!Scope || !Scope->getScopeNode()) |
| 418 | return; |
| 419 | |
Duncan P. N. Exon Smith | be9e4fe | 2015-04-20 18:32:29 +0000 | [diff] [blame] | 420 | auto *DS = Scope->getScopeNode(); |
David Blaikie | 9c65b13 | 2014-10-08 22:20:02 +0000 | [diff] [blame] | 421 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 422 | assert((Scope->getInlinedAt() || !isa<DISubprogram>(DS)) && |
David Blaikie | 9c65b13 | 2014-10-08 22:20:02 +0000 | [diff] [blame] | 423 | "Only handle inlined subprograms here, use " |
| 424 | "constructSubprogramScopeDIE for non-inlined " |
| 425 | "subprograms"); |
| 426 | |
Duncan P. N. Exon Smith | 827200c | 2015-06-25 23:52:10 +0000 | [diff] [blame] | 427 | SmallVector<DIE *, 8> Children; |
David Blaikie | 9c65b13 | 2014-10-08 22:20:02 +0000 | [diff] [blame] | 428 | |
| 429 | // We try to create the scope DIE first, then the children DIEs. This will |
| 430 | // avoid creating un-used children then removing them later when we find out |
| 431 | // the scope DIE is null. |
Duncan P. N. Exon Smith | 827200c | 2015-06-25 23:52:10 +0000 | [diff] [blame] | 432 | DIE *ScopeDIE; |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 433 | if (Scope->getParent() && isa<DISubprogram>(DS)) { |
David Blaikie | 01b48a8 | 2014-10-09 16:50:53 +0000 | [diff] [blame] | 434 | ScopeDIE = constructInlinedScopeDIE(Scope); |
David Blaikie | 9c65b13 | 2014-10-08 22:20:02 +0000 | [diff] [blame] | 435 | if (!ScopeDIE) |
| 436 | return; |
| 437 | // We create children when the scope DIE is not null. |
David Blaikie | 8b2fdb8 | 2014-10-09 18:24:28 +0000 | [diff] [blame] | 438 | createScopeChildrenDIE(Scope, Children); |
David Blaikie | 9c65b13 | 2014-10-08 22:20:02 +0000 | [diff] [blame] | 439 | } else { |
| 440 | // Early exit when we know the scope DIE is going to be null. |
| 441 | if (DD->isLexicalScopeDIENull(Scope)) |
| 442 | return; |
| 443 | |
David Blaikie | 2195e13 | 2017-07-27 00:06:53 +0000 | [diff] [blame] | 444 | bool HasNonScopeChildren = false; |
David Blaikie | 9c65b13 | 2014-10-08 22:20:02 +0000 | [diff] [blame] | 445 | |
| 446 | // We create children here when we know the scope DIE is not going to be |
| 447 | // null and the children will be added to the scope DIE. |
David Blaikie | 2195e13 | 2017-07-27 00:06:53 +0000 | [diff] [blame] | 448 | createScopeChildrenDIE(Scope, Children, &HasNonScopeChildren); |
David Blaikie | 3a443c2 | 2014-11-04 22:12:25 +0000 | [diff] [blame] | 449 | |
David Blaikie | 9c65b13 | 2014-10-08 22:20:02 +0000 | [diff] [blame] | 450 | // If there are only other scopes as children, put them directly in the |
| 451 | // parent instead, as this scope would serve no purpose. |
David Blaikie | 2195e13 | 2017-07-27 00:06:53 +0000 | [diff] [blame] | 452 | if (!HasNonScopeChildren) { |
David Blaikie | 9c65b13 | 2014-10-08 22:20:02 +0000 | [diff] [blame] | 453 | FinalChildren.insert(FinalChildren.end(), |
| 454 | std::make_move_iterator(Children.begin()), |
| 455 | std::make_move_iterator(Children.end())); |
| 456 | return; |
| 457 | } |
David Blaikie | 0fbf8bd | 2014-10-09 17:08:42 +0000 | [diff] [blame] | 458 | ScopeDIE = constructLexicalScopeDIE(Scope); |
David Blaikie | 9c65b13 | 2014-10-08 22:20:02 +0000 | [diff] [blame] | 459 | assert(ScopeDIE && "Scope DIE should not be null."); |
| 460 | } |
| 461 | |
| 462 | // Add children |
| 463 | for (auto &I : Children) |
| 464 | ScopeDIE->addChild(std::move(I)); |
| 465 | |
| 466 | FinalChildren.push_back(std::move(ScopeDIE)); |
| 467 | } |
| 468 | |
David Blaikie | 5b02a19 | 2014-11-03 23:10:59 +0000 | [diff] [blame] | 469 | void DwarfCompileUnit::addScopeRangeList(DIE &ScopeDIE, |
| 470 | SmallVector<RangeSpan, 2> Range) { |
Rafael Espindola | 063d725 | 2015-03-10 16:58:10 +0000 | [diff] [blame] | 471 | const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering(); |
| 472 | |
Wolfgang Pieb | fcf3810 | 2018-07-12 18:18:21 +0000 | [diff] [blame] | 473 | // Emit the offset into .debug_ranges or .debug_rnglists as a relocatable |
| 474 | // label. emitDIE() will handle emitting it appropriately. |
Rafael Espindola | 063d725 | 2015-03-10 16:58:10 +0000 | [diff] [blame] | 475 | const MCSymbol *RangeSectionSym = |
Wolfgang Pieb | fcf3810 | 2018-07-12 18:18:21 +0000 | [diff] [blame] | 476 | DD->getDwarfVersion() >= 5 |
| 477 | ? TLOF.getDwarfRnglistsSection()->getBeginSymbol() |
| 478 | : TLOF.getDwarfRangesSection()->getBeginSymbol(); |
David Blaikie | 5240020 | 2014-10-09 00:11:39 +0000 | [diff] [blame] | 479 | |
David Blaikie | c4af8bf | 2018-10-20 07:36:39 +0000 | [diff] [blame] | 480 | HasRangeLists = true; |
| 481 | |
| 482 | // Add the range list to the set of ranges to be emitted. |
| 483 | auto IndexAndList = |
| 484 | (DD->getDwarfVersion() < 5 && Skeleton ? Skeleton->DU : DU) |
David Blaikie | c8f7e6c | 2018-11-08 00:35:54 +0000 | [diff] [blame] | 485 | ->addRange(*(Skeleton ? Skeleton : this), std::move(Range)); |
David Blaikie | c4af8bf | 2018-10-20 07:36:39 +0000 | [diff] [blame] | 486 | |
| 487 | uint32_t Index = IndexAndList.first; |
| 488 | auto &List = *IndexAndList.second; |
David Blaikie | 5b02a19 | 2014-11-03 23:10:59 +0000 | [diff] [blame] | 489 | |
David Blaikie | 5240020 | 2014-10-09 00:11:39 +0000 | [diff] [blame] | 490 | // Under fission, ranges are specified by constant offsets relative to the |
| 491 | // CU's DW_AT_GNU_ranges_base. |
Wolfgang Pieb | fcf3810 | 2018-07-12 18:18:21 +0000 | [diff] [blame] | 492 | // FIXME: For DWARF v5, do not generate the DW_AT_ranges attribute under |
| 493 | // fission until we support the forms using the .debug_addr section |
| 494 | // (DW_RLE_startx_endx etc.). |
David Blaikie | 32e09de | 2018-10-20 08:12:36 +0000 | [diff] [blame] | 495 | if (DD->getDwarfVersion() >= 5) |
| 496 | addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_rnglistx, Index); |
| 497 | else if (isDwoUnit()) |
David Blaikie | c4af8bf | 2018-10-20 07:36:39 +0000 | [diff] [blame] | 498 | addSectionDelta(ScopeDIE, dwarf::DW_AT_ranges, List.getSym(), |
| 499 | RangeSectionSym); |
David Blaikie | c4af8bf | 2018-10-20 07:36:39 +0000 | [diff] [blame] | 500 | else |
David Blaikie | 5b02a19 | 2014-11-03 23:10:59 +0000 | [diff] [blame] | 501 | addSectionLabel(ScopeDIE, dwarf::DW_AT_ranges, List.getSym(), |
| 502 | RangeSectionSym); |
David Blaikie | 5240020 | 2014-10-09 00:11:39 +0000 | [diff] [blame] | 503 | } |
| 504 | |
David Blaikie | de12375 | 2014-10-09 00:21:42 +0000 | [diff] [blame] | 505 | void DwarfCompileUnit::attachRangesOrLowHighPC( |
David Blaikie | 5b02a19 | 2014-11-03 23:10:59 +0000 | [diff] [blame] | 506 | DIE &Die, SmallVector<RangeSpan, 2> Ranges) { |
Alexey Bataev | 858a7dd | 2018-03-20 20:21:38 +0000 | [diff] [blame] | 507 | if (Ranges.size() == 1 || !DD->useRangesSection()) { |
| 508 | const RangeSpan &Front = Ranges.front(); |
| 509 | const RangeSpan &Back = Ranges.back(); |
| 510 | attachLowHighPC(Die, Front.getStart(), Back.getEnd()); |
David Blaikie | 5b02a19 | 2014-11-03 23:10:59 +0000 | [diff] [blame] | 511 | } else |
| 512 | addScopeRangeList(Die, std::move(Ranges)); |
| 513 | } |
| 514 | |
| 515 | void DwarfCompileUnit::attachRangesOrLowHighPC( |
David Blaikie | de12375 | 2014-10-09 00:21:42 +0000 | [diff] [blame] | 516 | DIE &Die, const SmallVectorImpl<InsnRange> &Ranges) { |
David Blaikie | 5b02a19 | 2014-11-03 23:10:59 +0000 | [diff] [blame] | 517 | SmallVector<RangeSpan, 2> List; |
| 518 | List.reserve(Ranges.size()); |
| 519 | for (const InsnRange &R : Ranges) |
| 520 | List.push_back(RangeSpan(DD->getLabelBeforeInsn(R.first), |
| 521 | DD->getLabelAfterInsn(R.second))); |
| 522 | attachRangesOrLowHighPC(Die, std::move(List)); |
David Blaikie | de12375 | 2014-10-09 00:21:42 +0000 | [diff] [blame] | 523 | } |
| 524 | |
David Blaikie | 01b48a8 | 2014-10-09 16:50:53 +0000 | [diff] [blame] | 525 | // This scope represents inlined body of a function. Construct DIE to |
| 526 | // represent this concrete inlined copy of the function. |
Duncan P. N. Exon Smith | 827200c | 2015-06-25 23:52:10 +0000 | [diff] [blame] | 527 | DIE *DwarfCompileUnit::constructInlinedScopeDIE(LexicalScope *Scope) { |
David Blaikie | 01b48a8 | 2014-10-09 16:50:53 +0000 | [diff] [blame] | 528 | assert(Scope->getScopeNode()); |
Duncan P. N. Exon Smith | be9e4fe | 2015-04-20 18:32:29 +0000 | [diff] [blame] | 529 | auto *DS = Scope->getScopeNode(); |
| 530 | auto *InlinedSP = getDISubprogram(DS); |
David Blaikie | 01b48a8 | 2014-10-09 16:50:53 +0000 | [diff] [blame] | 531 | // Find the subprogram's DwarfCompileUnit in the SPMap in case the subprogram |
| 532 | // was inlined from another compile unit. |
David Blaikie | 488393f | 2017-05-12 01:13:45 +0000 | [diff] [blame] | 533 | DIE *OriginDIE = getAbstractSPDies()[InlinedSP]; |
David Blaikie | 01b48a8 | 2014-10-09 16:50:53 +0000 | [diff] [blame] | 534 | assert(OriginDIE && "Unable to find original DIE for an inlined subprogram."); |
| 535 | |
Duncan P. N. Exon Smith | 827200c | 2015-06-25 23:52:10 +0000 | [diff] [blame] | 536 | auto ScopeDIE = DIE::get(DIEValueAllocator, dwarf::DW_TAG_inlined_subroutine); |
David Blaikie | 01b48a8 | 2014-10-09 16:50:53 +0000 | [diff] [blame] | 537 | addDIEEntry(*ScopeDIE, dwarf::DW_AT_abstract_origin, *OriginDIE); |
| 538 | |
| 539 | attachRangesOrLowHighPC(*ScopeDIE, Scope->getRanges()); |
| 540 | |
| 541 | // Add the call site information to the DIE. |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 542 | const DILocation *IA = Scope->getInlinedAt(); |
David Blaikie | 01b48a8 | 2014-10-09 16:50:53 +0000 | [diff] [blame] | 543 | addUInt(*ScopeDIE, dwarf::DW_AT_call_file, None, |
Paul Robinson | 612e89d | 2018-01-12 19:17:50 +0000 | [diff] [blame] | 544 | getOrCreateSourceID(IA->getFile())); |
Duncan P. N. Exon Smith | b7e221b | 2015-04-14 01:35:55 +0000 | [diff] [blame] | 545 | addUInt(*ScopeDIE, dwarf::DW_AT_call_line, None, IA->getLine()); |
Dehao Chen | 6e0c844 | 2016-10-07 15:21:31 +0000 | [diff] [blame] | 546 | if (IA->getDiscriminator() && DD->getDwarfVersion() >= 4) |
Dehao Chen | 5451135 | 2015-11-11 18:09:47 +0000 | [diff] [blame] | 547 | addUInt(*ScopeDIE, dwarf::DW_AT_GNU_discriminator, None, |
| 548 | IA->getDiscriminator()); |
David Blaikie | 01b48a8 | 2014-10-09 16:50:53 +0000 | [diff] [blame] | 549 | |
| 550 | // Add name to the name table, we do this here because we're guaranteed |
| 551 | // to have concrete versions of our DW_TAG_inlined_subprogram nodes. |
David Blaikie | 66cf14d | 2018-08-16 21:29:55 +0000 | [diff] [blame] | 552 | DD->addSubprogramNames(*CUNode, InlinedSP, *ScopeDIE); |
David Blaikie | 01b48a8 | 2014-10-09 16:50:53 +0000 | [diff] [blame] | 553 | |
| 554 | return ScopeDIE; |
| 555 | } |
| 556 | |
David Blaikie | 0fbf8bd | 2014-10-09 17:08:42 +0000 | [diff] [blame] | 557 | // Construct new DW_TAG_lexical_block for this scope and attach |
| 558 | // DW_AT_low_pc/DW_AT_high_pc labels. |
Duncan P. N. Exon Smith | 827200c | 2015-06-25 23:52:10 +0000 | [diff] [blame] | 559 | DIE *DwarfCompileUnit::constructLexicalScopeDIE(LexicalScope *Scope) { |
David Blaikie | 0fbf8bd | 2014-10-09 17:08:42 +0000 | [diff] [blame] | 560 | if (DD->isLexicalScopeDIENull(Scope)) |
| 561 | return nullptr; |
| 562 | |
Duncan P. N. Exon Smith | 827200c | 2015-06-25 23:52:10 +0000 | [diff] [blame] | 563 | auto ScopeDIE = DIE::get(DIEValueAllocator, dwarf::DW_TAG_lexical_block); |
David Blaikie | 0fbf8bd | 2014-10-09 17:08:42 +0000 | [diff] [blame] | 564 | if (Scope->isAbstractScope()) |
| 565 | return ScopeDIE; |
| 566 | |
| 567 | attachRangesOrLowHighPC(*ScopeDIE, Scope->getRanges()); |
| 568 | |
| 569 | return ScopeDIE; |
| 570 | } |
| 571 | |
David Blaikie | ee7df55 | 2014-10-09 17:56:36 +0000 | [diff] [blame] | 572 | /// constructVariableDIE - Construct a DIE for the given DbgVariable. |
Duncan P. N. Exon Smith | 827200c | 2015-06-25 23:52:10 +0000 | [diff] [blame] | 573 | DIE *DwarfCompileUnit::constructVariableDIE(DbgVariable &DV, bool Abstract) { |
David Blaikie | ee7df55 | 2014-10-09 17:56:36 +0000 | [diff] [blame] | 574 | auto D = constructVariableDIEImpl(DV, Abstract); |
| 575 | DV.setDIE(*D); |
| 576 | return D; |
| 577 | } |
| 578 | |
Hsiangkai Wang | 2532ac8 | 2018-08-17 15:22:04 +0000 | [diff] [blame] | 579 | DIE *DwarfCompileUnit::constructLabelDIE(DbgLabel &DL, |
| 580 | const LexicalScope &Scope) { |
| 581 | auto LabelDie = DIE::get(DIEValueAllocator, DL.getTag()); |
| 582 | insertDIE(DL.getLabel(), LabelDie); |
| 583 | DL.setDIE(*LabelDie); |
| 584 | |
| 585 | if (Scope.isAbstractScope()) |
| 586 | applyLabelAttributes(DL, *LabelDie); |
| 587 | |
| 588 | return LabelDie; |
| 589 | } |
| 590 | |
Duncan P. N. Exon Smith | 827200c | 2015-06-25 23:52:10 +0000 | [diff] [blame] | 591 | DIE *DwarfCompileUnit::constructVariableDIEImpl(const DbgVariable &DV, |
| 592 | bool Abstract) { |
David Blaikie | ee7df55 | 2014-10-09 17:56:36 +0000 | [diff] [blame] | 593 | // Define variable debug information entry. |
Duncan P. N. Exon Smith | 827200c | 2015-06-25 23:52:10 +0000 | [diff] [blame] | 594 | auto VariableDie = DIE::get(DIEValueAllocator, DV.getTag()); |
Sander de Smalen | dc00bec | 2018-01-24 13:35:54 +0000 | [diff] [blame] | 595 | insertDIE(DV.getVariable(), VariableDie); |
David Blaikie | ee7df55 | 2014-10-09 17:56:36 +0000 | [diff] [blame] | 596 | |
| 597 | if (Abstract) { |
| 598 | applyVariableAttributes(DV, *VariableDie); |
| 599 | return VariableDie; |
| 600 | } |
| 601 | |
| 602 | // Add variable address. |
| 603 | |
Duncan P. N. Exon Smith | 364a300 | 2015-04-17 21:34:47 +0000 | [diff] [blame] | 604 | unsigned Offset = DV.getDebugLocListIndex(); |
David Blaikie | ee7df55 | 2014-10-09 17:56:36 +0000 | [diff] [blame] | 605 | if (Offset != ~0U) { |
| 606 | addLocationList(*VariableDie, dwarf::DW_AT_location, Offset); |
| 607 | return VariableDie; |
| 608 | } |
| 609 | |
Nikola Prica | abc1dff | 2019-06-10 08:41:06 +0000 | [diff] [blame] | 610 | // Check if variable has a single location description. |
| 611 | if (auto *DVal = DV.getValueLoc()) { |
| 612 | if (DVal->isLocation()) |
| 613 | addVariableAddress(DV, *VariableDie, DVal->getLoc()); |
| 614 | else if (DVal->isInt()) { |
Adrian Prantl | 3b89e66 | 2016-02-17 22:20:08 +0000 | [diff] [blame] | 615 | auto *Expr = DV.getSingleExpression(); |
| 616 | if (Expr && Expr->getNumElements()) { |
| 617 | DIELoc *Loc = new (DIEValueAllocator) DIELoc; |
| 618 | DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc); |
| 619 | // If there is an expression, emit raw unsigned bytes. |
Adrian Prantl | 8fafb8d | 2016-12-09 20:43:40 +0000 | [diff] [blame] | 620 | DwarfExpr.addFragmentOffset(Expr); |
Nikola Prica | abc1dff | 2019-06-10 08:41:06 +0000 | [diff] [blame] | 621 | DwarfExpr.addUnsignedConstant(DVal->getInt()); |
Adrian Prantl | a63b8e8 | 2017-03-16 17:42:45 +0000 | [diff] [blame] | 622 | DwarfExpr.addExpression(Expr); |
Adrian Prantl | bceaaa9 | 2016-12-20 02:09:43 +0000 | [diff] [blame] | 623 | addBlock(*VariableDie, dwarf::DW_AT_location, DwarfExpr.finalize()); |
Adrian Prantl | 3b89e66 | 2016-02-17 22:20:08 +0000 | [diff] [blame] | 624 | } else |
Nikola Prica | abc1dff | 2019-06-10 08:41:06 +0000 | [diff] [blame] | 625 | addConstantValue(*VariableDie, DVal->getInt(), DV.getType()); |
| 626 | } else if (DVal->isConstantFP()) { |
| 627 | addConstantFPValue(*VariableDie, DVal->getConstantFP()); |
| 628 | } else if (DVal->isConstantInt()) { |
| 629 | addConstantValue(*VariableDie, DVal->getConstantInt(), DV.getType()); |
| 630 | } |
David Blaikie | ee7df55 | 2014-10-09 17:56:36 +0000 | [diff] [blame] | 631 | return VariableDie; |
| 632 | } |
| 633 | |
| 634 | // .. else use frame index. |
Adrian Prantl | 67c2442 | 2017-02-17 19:42:32 +0000 | [diff] [blame] | 635 | if (!DV.hasFrameIndexExprs()) |
Adrian Prantl | ca7e470 | 2015-02-10 23:18:28 +0000 | [diff] [blame] | 636 | return VariableDie; |
| 637 | |
Alexey Bataev | f3a9150 | 2019-02-05 19:33:47 +0000 | [diff] [blame] | 638 | Optional<unsigned> NVPTXAddressSpace; |
Duncan P. N. Exon Smith | e7e1d0c | 2015-05-27 22:14:58 +0000 | [diff] [blame] | 639 | DIELoc *Loc = new (DIEValueAllocator) DIELoc; |
Adrian Prantl | ca7e470 | 2015-02-10 23:18:28 +0000 | [diff] [blame] | 640 | DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc); |
Adrian Prantl | 67c2442 | 2017-02-17 19:42:32 +0000 | [diff] [blame] | 641 | for (auto &Fragment : DV.getFrameIndexExprs()) { |
David Blaikie | ee7df55 | 2014-10-09 17:56:36 +0000 | [diff] [blame] | 642 | unsigned FrameReg = 0; |
Adrian Prantl | 6825fb6 | 2017-04-18 01:21:53 +0000 | [diff] [blame] | 643 | const DIExpression *Expr = Fragment.Expr; |
Eric Christopher | d4e723f | 2015-02-20 22:36:11 +0000 | [diff] [blame] | 644 | const TargetFrameLowering *TFI = Asm->MF->getSubtarget().getFrameLowering(); |
Adrian Prantl | 67c2442 | 2017-02-17 19:42:32 +0000 | [diff] [blame] | 645 | int Offset = TFI->getFrameIndexReference(*Asm->MF, Fragment.FI, FrameReg); |
Adrian Prantl | 6825fb6 | 2017-04-18 01:21:53 +0000 | [diff] [blame] | 646 | DwarfExpr.addFragmentOffset(Expr); |
Adrian Prantl | 956484b | 2017-03-20 21:35:09 +0000 | [diff] [blame] | 647 | SmallVector<uint64_t, 8> Ops; |
Florian Hahn | ffc498d | 2017-06-14 13:14:38 +0000 | [diff] [blame] | 648 | Ops.push_back(dwarf::DW_OP_plus_uconst); |
Adrian Prantl | 956484b | 2017-03-20 21:35:09 +0000 | [diff] [blame] | 649 | Ops.push_back(Offset); |
Alexey Bataev | f3a9150 | 2019-02-05 19:33:47 +0000 | [diff] [blame] | 650 | // According to |
| 651 | // https://docs.nvidia.com/cuda/archive/10.0/ptx-writers-guide-to-interoperability/index.html#cuda-specific-dwarf |
| 652 | // cuda-gdb requires DW_AT_address_class for all variables to be able to |
| 653 | // correctly interpret address space of the variable address. |
| 654 | // Decode DW_OP_constu <DWARF Address Space> DW_OP_swap DW_OP_xderef |
| 655 | // sequence for the NVPTX + gdb target. |
| 656 | unsigned LocalNVPTXAddressSpace; |
| 657 | if (Asm->TM.getTargetTriple().isNVPTX() && DD->tuneForGDB()) { |
| 658 | const DIExpression *NewExpr = |
| 659 | DIExpression::extractAddressClass(Expr, LocalNVPTXAddressSpace); |
| 660 | if (NewExpr != Expr) { |
| 661 | Expr = NewExpr; |
| 662 | NVPTXAddressSpace = LocalNVPTXAddressSpace; |
| 663 | } |
| 664 | } |
| 665 | if (Expr) |
| 666 | Ops.append(Expr->elements_begin(), Expr->elements_end()); |
Adrian Prantl | 6825fb6 | 2017-04-18 01:21:53 +0000 | [diff] [blame] | 667 | DIExpressionCursor Cursor(Ops); |
Adrian Prantl | c12cee3 | 2017-04-19 23:42:25 +0000 | [diff] [blame] | 668 | DwarfExpr.setMemoryLocationKind(); |
Alexey Bataev | 4dd7558 | 2018-07-26 16:29:52 +0000 | [diff] [blame] | 669 | if (const MCSymbol *FrameSymbol = Asm->getFunctionFrameSymbol()) |
| 670 | addOpAddress(*Loc, FrameSymbol); |
| 671 | else |
| 672 | DwarfExpr.addMachineRegExpression( |
| 673 | *Asm->MF->getSubtarget().getRegisterInfo(), Cursor, FrameReg); |
Adrian Prantl | 6825fb6 | 2017-04-18 01:21:53 +0000 | [diff] [blame] | 674 | DwarfExpr.addExpression(std::move(Cursor)); |
David Blaikie | ee7df55 | 2014-10-09 17:56:36 +0000 | [diff] [blame] | 675 | } |
Alexey Bataev | f3a9150 | 2019-02-05 19:33:47 +0000 | [diff] [blame] | 676 | if (Asm->TM.getTargetTriple().isNVPTX() && DD->tuneForGDB()) { |
| 677 | // According to |
| 678 | // https://docs.nvidia.com/cuda/archive/10.0/ptx-writers-guide-to-interoperability/index.html#cuda-specific-dwarf |
| 679 | // cuda-gdb requires DW_AT_address_class for all variables to be able to |
| 680 | // correctly interpret address space of the variable address. |
| 681 | const unsigned NVPTX_ADDR_local_space = 6; |
| 682 | addUInt(*VariableDie, dwarf::DW_AT_address_class, dwarf::DW_FORM_data1, |
| 683 | NVPTXAddressSpace ? *NVPTXAddressSpace : NVPTX_ADDR_local_space); |
| 684 | } |
Adrian Prantl | bceaaa9 | 2016-12-20 02:09:43 +0000 | [diff] [blame] | 685 | addBlock(*VariableDie, dwarf::DW_AT_location, DwarfExpr.finalize()); |
Peter Collingbourne | fb9ce10 | 2019-06-17 23:39:41 +0000 | [diff] [blame] | 686 | if (DwarfExpr.TagOffset) |
| 687 | addUInt(*VariableDie, dwarf::DW_AT_LLVM_tag_offset, dwarf::DW_FORM_data1, |
| 688 | *DwarfExpr.TagOffset); |
David Blaikie | ee7df55 | 2014-10-09 17:56:36 +0000 | [diff] [blame] | 689 | |
| 690 | return VariableDie; |
| 691 | } |
| 692 | |
Duncan P. N. Exon Smith | 827200c | 2015-06-25 23:52:10 +0000 | [diff] [blame] | 693 | DIE *DwarfCompileUnit::constructVariableDIE(DbgVariable &DV, |
| 694 | const LexicalScope &Scope, |
| 695 | DIE *&ObjectPointer) { |
David Blaikie | 4a1a44e | 2014-10-09 17:56:39 +0000 | [diff] [blame] | 696 | auto Var = constructVariableDIE(DV, Scope.isAbstractScope()); |
| 697 | if (DV.isObjectPointer()) |
Duncan P. N. Exon Smith | 827200c | 2015-06-25 23:52:10 +0000 | [diff] [blame] | 698 | ObjectPointer = Var; |
David Blaikie | 4a1a44e | 2014-10-09 17:56:39 +0000 | [diff] [blame] | 699 | return Var; |
| 700 | } |
| 701 | |
Adrian Prantl | 03dd6f5 | 2018-02-08 23:21:15 +0000 | [diff] [blame] | 702 | /// Return all DIVariables that appear in count: expressions. |
| 703 | static SmallVector<const DIVariable *, 2> dependencies(DbgVariable *Var) { |
| 704 | SmallVector<const DIVariable *, 2> Result; |
| 705 | auto *Array = dyn_cast<DICompositeType>(Var->getType()); |
Adrian Prantl | c929f7a | 2018-02-06 22:17:45 +0000 | [diff] [blame] | 706 | if (!Array || Array->getTag() != dwarf::DW_TAG_array_type) |
Adrian Prantl | 03dd6f5 | 2018-02-08 23:21:15 +0000 | [diff] [blame] | 707 | return Result; |
| 708 | for (auto *El : Array->getElements()) { |
Adrian Prantl | c929f7a | 2018-02-06 22:17:45 +0000 | [diff] [blame] | 709 | if (auto *Subrange = dyn_cast<DISubrange>(El)) { |
| 710 | auto Count = Subrange->getCount(); |
Adrian Prantl | 03dd6f5 | 2018-02-08 23:21:15 +0000 | [diff] [blame] | 711 | if (auto *Dependency = Count.dyn_cast<DIVariable *>()) |
| 712 | Result.push_back(Dependency); |
Adrian Prantl | c929f7a | 2018-02-06 22:17:45 +0000 | [diff] [blame] | 713 | } |
Adrian Prantl | 03dd6f5 | 2018-02-08 23:21:15 +0000 | [diff] [blame] | 714 | } |
| 715 | return Result; |
Adrian Prantl | c929f7a | 2018-02-06 22:17:45 +0000 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | /// Sort local variables so that variables appearing inside of helper |
| 719 | /// expressions come first. |
Adrian Prantl | 03dd6f5 | 2018-02-08 23:21:15 +0000 | [diff] [blame] | 720 | static SmallVector<DbgVariable *, 8> |
| 721 | sortLocalVars(SmallVectorImpl<DbgVariable *> &Input) { |
| 722 | SmallVector<DbgVariable *, 8> Result; |
| 723 | SmallVector<PointerIntPair<DbgVariable *, 1>, 8> WorkList; |
| 724 | // Map back from a DIVariable to its containing DbgVariable. |
| 725 | SmallDenseMap<const DILocalVariable *, DbgVariable *> DbgVar; |
| 726 | // Set of DbgVariables in Result. |
| 727 | SmallDenseSet<DbgVariable *, 8> Visited; |
| 728 | // For cycle detection. |
| 729 | SmallDenseSet<DbgVariable *, 8> Visiting; |
| 730 | |
| 731 | // Initialize the worklist and the DIVariable lookup table. |
| 732 | for (auto Var : reverse(Input)) { |
| 733 | DbgVar.insert({Var->getVariable(), Var}); |
| 734 | WorkList.push_back({Var, 0}); |
| 735 | } |
| 736 | |
| 737 | // Perform a stable topological sort by doing a DFS. |
| 738 | while (!WorkList.empty()) { |
| 739 | auto Item = WorkList.back(); |
| 740 | DbgVariable *Var = Item.getPointer(); |
| 741 | bool visitedAllDependencies = Item.getInt(); |
| 742 | WorkList.pop_back(); |
| 743 | |
| 744 | // Dependency is in a different lexical scope or a global. |
| 745 | if (!Var) |
| 746 | continue; |
| 747 | |
| 748 | // Already handled. |
| 749 | if (Visited.count(Var)) |
| 750 | continue; |
| 751 | |
| 752 | // Add to Result if all dependencies are visited. |
| 753 | if (visitedAllDependencies) { |
| 754 | Visited.insert(Var); |
| 755 | Result.push_back(Var); |
| 756 | continue; |
| 757 | } |
| 758 | |
| 759 | // Detect cycles. |
| 760 | auto Res = Visiting.insert(Var); |
| 761 | if (!Res.second) { |
| 762 | assert(false && "dependency cycle in local variables"); |
| 763 | return Result; |
| 764 | } |
| 765 | |
| 766 | // Push dependencies and this node onto the worklist, so that this node is |
| 767 | // visited again after all of its dependencies are handled. |
| 768 | WorkList.push_back({Var, 1}); |
| 769 | for (auto *Dependency : dependencies(Var)) { |
| 770 | auto Dep = dyn_cast_or_null<const DILocalVariable>(Dependency); |
| 771 | WorkList.push_back({DbgVar[Dep], 0}); |
| 772 | } |
| 773 | } |
| 774 | return Result; |
Adrian Prantl | c929f7a | 2018-02-06 22:17:45 +0000 | [diff] [blame] | 775 | } |
| 776 | |
Duncan P. N. Exon Smith | 827200c | 2015-06-25 23:52:10 +0000 | [diff] [blame] | 777 | DIE *DwarfCompileUnit::createScopeChildrenDIE(LexicalScope *Scope, |
| 778 | SmallVectorImpl<DIE *> &Children, |
David Blaikie | 2195e13 | 2017-07-27 00:06:53 +0000 | [diff] [blame] | 779 | bool *HasNonScopeChildren) { |
| 780 | assert(Children.empty()); |
David Blaikie | 8b2fdb8 | 2014-10-09 18:24:28 +0000 | [diff] [blame] | 781 | DIE *ObjectPointer = nullptr; |
| 782 | |
Adrian Prantl | c929f7a | 2018-02-06 22:17:45 +0000 | [diff] [blame] | 783 | // Emit function arguments (order is significant). |
| 784 | auto Vars = DU->getScopeVariables().lookup(Scope); |
| 785 | for (auto &DV : Vars.Args) |
| 786 | Children.push_back(constructVariableDIE(*DV.second, *Scope, ObjectPointer)); |
| 787 | |
| 788 | // Emit local variables. |
Adrian Prantl | 03dd6f5 | 2018-02-08 23:21:15 +0000 | [diff] [blame] | 789 | auto Locals = sortLocalVars(Vars.Locals); |
| 790 | for (DbgVariable *DV : Locals) |
David Blaikie | 8b2fdb8 | 2014-10-09 18:24:28 +0000 | [diff] [blame] | 791 | Children.push_back(constructVariableDIE(*DV, *Scope, ObjectPointer)); |
| 792 | |
David Blaikie | 2195e13 | 2017-07-27 00:06:53 +0000 | [diff] [blame] | 793 | // Skip imported directives in gmlt-like data. |
| 794 | if (!includeMinimalInlineScopes()) { |
| 795 | // There is no need to emit empty lexical block DIE. |
| 796 | for (const auto *IE : ImportedEntities[Scope->getScopeNode()]) |
| 797 | Children.push_back( |
| 798 | constructImportedEntityDIE(cast<DIImportedEntity>(IE))); |
| 799 | } |
| 800 | |
| 801 | if (HasNonScopeChildren) |
| 802 | *HasNonScopeChildren = !Children.empty(); |
David Blaikie | 8b2fdb8 | 2014-10-09 18:24:28 +0000 | [diff] [blame] | 803 | |
Hsiangkai Wang | 2532ac8 | 2018-08-17 15:22:04 +0000 | [diff] [blame] | 804 | for (DbgLabel *DL : DU->getScopeLabels().lookup(Scope)) |
| 805 | Children.push_back(constructLabelDIE(*DL, *Scope)); |
| 806 | |
David Blaikie | 8b2fdb8 | 2014-10-09 18:24:28 +0000 | [diff] [blame] | 807 | for (LexicalScope *LS : Scope->getChildren()) |
| 808 | constructScopeDIE(LS, Children); |
| 809 | |
David Blaikie | 8b2fdb8 | 2014-10-09 18:24:28 +0000 | [diff] [blame] | 810 | return ObjectPointer; |
| 811 | } |
| 812 | |
Vedant Kumar | 5931b4e | 2018-10-05 20:37:17 +0000 | [diff] [blame] | 813 | DIE &DwarfCompileUnit::constructSubprogramScopeDIE(const DISubprogram *Sub, |
| 814 | LexicalScope *Scope) { |
David Blaikie | 1d07234 | 2014-10-09 20:21:36 +0000 | [diff] [blame] | 815 | DIE &ScopeDIE = updateSubprogramScopeDIE(Sub); |
| 816 | |
David Blaikie | 3e3eb33 | 2016-12-15 23:17:52 +0000 | [diff] [blame] | 817 | if (Scope) { |
| 818 | assert(!Scope->getInlinedAt()); |
| 819 | assert(!Scope->isAbstractScope()); |
| 820 | // Collect lexical scope children first. |
| 821 | // ObjectPointer might be a local (non-argument) local variable if it's a |
| 822 | // block's synthetic this pointer. |
| 823 | if (DIE *ObjectPointer = createAndAddScopeChildren(Scope, ScopeDIE)) |
| 824 | addDIEEntry(ScopeDIE, dwarf::DW_AT_object_pointer, *ObjectPointer); |
| 825 | } |
| 826 | |
David Blaikie | 1d07234 | 2014-10-09 20:21:36 +0000 | [diff] [blame] | 827 | // If this is a variadic function, add an unspecified parameter. |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 828 | DITypeRefArray FnArgs = Sub->getType()->getTypeArray(); |
David Blaikie | 1dd573d | 2014-10-23 22:27:50 +0000 | [diff] [blame] | 829 | |
David Blaikie | 1d07234 | 2014-10-09 20:21:36 +0000 | [diff] [blame] | 830 | // If we have a single element of null, it is a function that returns void. |
| 831 | // If we have more than one elements and the last one is null, it is a |
| 832 | // variadic function. |
Duncan P. N. Exon Smith | 000fa2c | 2015-04-07 04:14:33 +0000 | [diff] [blame] | 833 | if (FnArgs.size() > 1 && !FnArgs[FnArgs.size() - 1] && |
David Blaikie | 3a443c2 | 2014-11-04 22:12:25 +0000 | [diff] [blame] | 834 | !includeMinimalInlineScopes()) |
Duncan P. N. Exon Smith | 827200c | 2015-06-25 23:52:10 +0000 | [diff] [blame] | 835 | ScopeDIE.addChild( |
| 836 | DIE::get(DIEValueAllocator, dwarf::DW_TAG_unspecified_parameters)); |
Vedant Kumar | 5931b4e | 2018-10-05 20:37:17 +0000 | [diff] [blame] | 837 | |
| 838 | return ScopeDIE; |
David Blaikie | 1d07234 | 2014-10-09 20:21:36 +0000 | [diff] [blame] | 839 | } |
| 840 | |
David Blaikie | 78b65b6 | 2014-10-09 20:26:15 +0000 | [diff] [blame] | 841 | DIE *DwarfCompileUnit::createAndAddScopeChildren(LexicalScope *Scope, |
| 842 | DIE &ScopeDIE) { |
| 843 | // We create children when the scope DIE is not null. |
Duncan P. N. Exon Smith | 827200c | 2015-06-25 23:52:10 +0000 | [diff] [blame] | 844 | SmallVector<DIE *, 8> Children; |
David Blaikie | 78b65b6 | 2014-10-09 20:26:15 +0000 | [diff] [blame] | 845 | DIE *ObjectPointer = createScopeChildrenDIE(Scope, Children); |
| 846 | |
| 847 | // Add children |
| 848 | for (auto &I : Children) |
| 849 | ScopeDIE.addChild(std::move(I)); |
| 850 | |
| 851 | return ObjectPointer; |
| 852 | } |
| 853 | |
Dehao Chen | 5451135 | 2015-11-11 18:09:47 +0000 | [diff] [blame] | 854 | void DwarfCompileUnit::constructAbstractSubprogramScopeDIE( |
| 855 | LexicalScope *Scope) { |
David Blaikie | 488393f | 2017-05-12 01:13:45 +0000 | [diff] [blame] | 856 | DIE *&AbsDef = getAbstractSPDies()[Scope->getScopeNode()]; |
David Blaikie | 49be5b3 | 2014-10-31 21:57:02 +0000 | [diff] [blame] | 857 | if (AbsDef) |
| 858 | return; |
| 859 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 860 | auto *SP = cast<DISubprogram>(Scope->getScopeNode()); |
David Blaikie | 58410f2 | 2014-10-10 06:39:26 +0000 | [diff] [blame] | 861 | |
| 862 | DIE *ContextDIE; |
Jonas Devlieghere | 294e689 | 2017-11-15 10:57:05 +0000 | [diff] [blame] | 863 | DwarfCompileUnit *ContextCU = this; |
David Blaikie | 58410f2 | 2014-10-10 06:39:26 +0000 | [diff] [blame] | 864 | |
David Blaikie | 3a443c2 | 2014-11-04 22:12:25 +0000 | [diff] [blame] | 865 | if (includeMinimalInlineScopes()) |
| 866 | ContextDIE = &getUnitDie(); |
David Blaikie | 58410f2 | 2014-10-10 06:39:26 +0000 | [diff] [blame] | 867 | // Some of this is duplicated from DwarfUnit::getOrCreateSubprogramDIE, with |
Duncan P. N. Exon Smith | 7c60f20 | 2015-04-18 00:35:36 +0000 | [diff] [blame] | 868 | // the important distinction that the debug node is not associated with the |
| 869 | // DIE (since the debug node will be associated with the concrete DIE, if |
David Blaikie | 58410f2 | 2014-10-10 06:39:26 +0000 | [diff] [blame] | 870 | // any). It could be refactored to some common utility function. |
Duncan P. N. Exon Smith | 537b4a8 | 2015-04-14 03:40:37 +0000 | [diff] [blame] | 871 | else if (auto *SPDecl = SP->getDeclaration()) { |
David Blaikie | 58410f2 | 2014-10-10 06:39:26 +0000 | [diff] [blame] | 872 | ContextDIE = &getUnitDie(); |
| 873 | getOrCreateSubprogramDIE(SPDecl); |
Jonas Devlieghere | 294e689 | 2017-11-15 10:57:05 +0000 | [diff] [blame] | 874 | } else { |
Fangrui Song | da82ce9 | 2019-05-07 02:06:37 +0000 | [diff] [blame] | 875 | ContextDIE = getOrCreateContextDIE(SP->getScope()); |
Jonas Devlieghere | 294e689 | 2017-11-15 10:57:05 +0000 | [diff] [blame] | 876 | // The scope may be shared with a subprogram that has already been |
| 877 | // constructed in another CU, in which case we need to construct this |
| 878 | // subprogram in the same CU. |
| 879 | ContextCU = DD->lookupCU(ContextDIE->getUnitDie()); |
| 880 | } |
David Blaikie | 58410f2 | 2014-10-10 06:39:26 +0000 | [diff] [blame] | 881 | |
Duncan P. N. Exon Smith | 7c60f20 | 2015-04-18 00:35:36 +0000 | [diff] [blame] | 882 | // Passing null as the associated node because the abstract definition |
David Blaikie | 58410f2 | 2014-10-10 06:39:26 +0000 | [diff] [blame] | 883 | // shouldn't be found by lookup. |
Jonas Devlieghere | 294e689 | 2017-11-15 10:57:05 +0000 | [diff] [blame] | 884 | AbsDef = &ContextCU->createAndAddDIE(dwarf::DW_TAG_subprogram, *ContextDIE, nullptr); |
| 885 | ContextCU->applySubprogramAttributesToDefinition(SP, *AbsDef); |
David Blaikie | 58410f2 | 2014-10-10 06:39:26 +0000 | [diff] [blame] | 886 | |
Jonas Devlieghere | 294e689 | 2017-11-15 10:57:05 +0000 | [diff] [blame] | 887 | if (!ContextCU->includeMinimalInlineScopes()) |
| 888 | ContextCU->addUInt(*AbsDef, dwarf::DW_AT_inline, None, dwarf::DW_INL_inlined); |
| 889 | if (DIE *ObjectPointer = ContextCU->createAndAddScopeChildren(Scope, *AbsDef)) |
| 890 | ContextCU->addDIEEntry(*AbsDef, dwarf::DW_AT_object_pointer, *ObjectPointer); |
David Blaikie | 58410f2 | 2014-10-10 06:39:26 +0000 | [diff] [blame] | 891 | } |
| 892 | |
Vedant Kumar | 5931b4e | 2018-10-05 20:37:17 +0000 | [diff] [blame] | 893 | DIE &DwarfCompileUnit::constructCallSiteEntryDIE(DIE &ScopeDIE, |
| 894 | const DISubprogram &CalleeSP, |
| 895 | bool IsTail, |
Vedant Kumar | 74533bd | 2018-10-22 21:44:21 +0000 | [diff] [blame] | 896 | const MCExpr *PCOffset) { |
Vedant Kumar | 5931b4e | 2018-10-05 20:37:17 +0000 | [diff] [blame] | 897 | // Insert a call site entry DIE within ScopeDIE. |
| 898 | DIE &CallSiteDIE = |
| 899 | createAndAddDIE(dwarf::DW_TAG_call_site, ScopeDIE, nullptr); |
| 900 | |
| 901 | // For the purposes of showing tail call frames in backtraces, a key piece of |
| 902 | // information is DW_AT_call_origin, a pointer to the callee DIE. |
| 903 | DIE *CalleeDIE = getOrCreateSubprogramDIE(&CalleeSP); |
| 904 | assert(CalleeDIE && "Could not create DIE for call site entry origin"); |
| 905 | addDIEEntry(CallSiteDIE, dwarf::DW_AT_call_origin, *CalleeDIE); |
| 906 | |
| 907 | if (IsTail) { |
| 908 | // Attach DW_AT_call_tail_call to tail calls for standards compliance. |
| 909 | addFlag(CallSiteDIE, dwarf::DW_AT_call_tail_call); |
| 910 | } else { |
| 911 | // Attach the return PC to allow the debugger to disambiguate call paths |
| 912 | // from one function to another. |
Vedant Kumar | 74533bd | 2018-10-22 21:44:21 +0000 | [diff] [blame] | 913 | assert(PCOffset && "Missing return PC information for a call"); |
| 914 | addAddressExpr(CallSiteDIE, dwarf::DW_AT_call_return_pc, PCOffset); |
Vedant Kumar | 5931b4e | 2018-10-05 20:37:17 +0000 | [diff] [blame] | 915 | } |
| 916 | return CallSiteDIE; |
| 917 | } |
| 918 | |
Duncan P. N. Exon Smith | 827200c | 2015-06-25 23:52:10 +0000 | [diff] [blame] | 919 | DIE *DwarfCompileUnit::constructImportedEntityDIE( |
| 920 | const DIImportedEntity *Module) { |
Amjad Aboud | 72da939 | 2016-04-30 01:44:07 +0000 | [diff] [blame] | 921 | DIE *IMDie = DIE::get(DIEValueAllocator, (dwarf::Tag)Module->getTag()); |
| 922 | insertDIE(Module, IMDie); |
Frederic Riss | 987fe22 | 2014-10-24 21:31:09 +0000 | [diff] [blame] | 923 | DIE *EntityDie; |
Fangrui Song | da82ce9 | 2019-05-07 02:06:37 +0000 | [diff] [blame] | 924 | auto *Entity = Module->getEntity(); |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 925 | if (auto *NS = dyn_cast<DINamespace>(Entity)) |
Duncan P. N. Exon Smith | e686f15 | 2015-04-06 23:27:40 +0000 | [diff] [blame] | 926 | EntityDie = getOrCreateNameSpace(NS); |
Adrian Prantl | 08a388b | 2015-06-30 02:13:04 +0000 | [diff] [blame] | 927 | else if (auto *M = dyn_cast<DIModule>(Entity)) |
| 928 | EntityDie = getOrCreateModule(M); |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 929 | else if (auto *SP = dyn_cast<DISubprogram>(Entity)) |
Duncan P. N. Exon Smith | e686f15 | 2015-04-06 23:27:40 +0000 | [diff] [blame] | 930 | EntityDie = getOrCreateSubprogramDIE(SP); |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 931 | else if (auto *T = dyn_cast<DIType>(Entity)) |
Duncan P. N. Exon Smith | e686f15 | 2015-04-06 23:27:40 +0000 | [diff] [blame] | 932 | EntityDie = getOrCreateTypeDIE(T); |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 933 | else if (auto *GV = dyn_cast<DIGlobalVariable>(Entity)) |
Adrian Prantl | bceaaa9 | 2016-12-20 02:09:43 +0000 | [diff] [blame] | 934 | EntityDie = getOrCreateGlobalVariableDIE(GV, {}); |
Frederic Riss | 987fe22 | 2014-10-24 21:31:09 +0000 | [diff] [blame] | 935 | else |
| 936 | EntityDie = getDIE(Entity); |
| 937 | assert(EntityDie); |
Paul Robinson | 612e89d | 2018-01-12 19:17:50 +0000 | [diff] [blame] | 938 | addSourceLine(*IMDie, Module->getLine(), Module->getFile()); |
Frederic Riss | 987fe22 | 2014-10-24 21:31:09 +0000 | [diff] [blame] | 939 | addDIEEntry(*IMDie, dwarf::DW_AT_import, *EntityDie); |
Duncan P. N. Exon Smith | de8e427 | 2015-04-14 01:46:44 +0000 | [diff] [blame] | 940 | StringRef Name = Module->getName(); |
Frederic Riss | 987fe22 | 2014-10-24 21:31:09 +0000 | [diff] [blame] | 941 | if (!Name.empty()) |
| 942 | addString(*IMDie, dwarf::DW_AT_name, Name); |
| 943 | |
| 944 | return IMDie; |
| 945 | } |
| 946 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 947 | void DwarfCompileUnit::finishSubprogramDefinition(const DISubprogram *SP) { |
Amjad Aboud | 72da939 | 2016-04-30 01:44:07 +0000 | [diff] [blame] | 948 | DIE *D = getDIE(SP); |
David Blaikie | 488393f | 2017-05-12 01:13:45 +0000 | [diff] [blame] | 949 | if (DIE *AbsSPDIE = getAbstractSPDies().lookup(SP)) { |
Amjad Aboud | 72da939 | 2016-04-30 01:44:07 +0000 | [diff] [blame] | 950 | if (D) |
David Blaikie | 4191cbc | 2014-10-10 06:39:29 +0000 | [diff] [blame] | 951 | // If this subprogram has an abstract definition, reference that |
| 952 | addDIEEntry(*D, dwarf::DW_AT_abstract_origin, *AbsSPDIE); |
Amjad Aboud | 72da939 | 2016-04-30 01:44:07 +0000 | [diff] [blame] | 953 | } else { |
David Blaikie | 3e3eb33 | 2016-12-15 23:17:52 +0000 | [diff] [blame] | 954 | assert(D || includeMinimalInlineScopes()); |
Amjad Aboud | 72da939 | 2016-04-30 01:44:07 +0000 | [diff] [blame] | 955 | if (D) |
David Blaikie | 4191cbc | 2014-10-10 06:39:29 +0000 | [diff] [blame] | 956 | // And attach the attributes |
| 957 | applySubprogramAttributesToDefinition(SP, *D); |
| 958 | } |
| 959 | } |
| 960 | |
Hsiangkai Wang | 2532ac8 | 2018-08-17 15:22:04 +0000 | [diff] [blame] | 961 | void DwarfCompileUnit::finishEntityDefinition(const DbgEntity *Entity) { |
| 962 | DbgEntity *AbsEntity = getExistingAbstractEntity(Entity->getEntity()); |
| 963 | |
| 964 | auto *Die = Entity->getDIE(); |
| 965 | /// Label may be used to generate DW_AT_low_pc, so put it outside |
| 966 | /// if/else block. |
| 967 | const DbgLabel *Label = nullptr; |
| 968 | if (AbsEntity && AbsEntity->getDIE()) { |
| 969 | addDIEEntry(*Die, dwarf::DW_AT_abstract_origin, *AbsEntity->getDIE()); |
| 970 | Label = dyn_cast<const DbgLabel>(Entity); |
| 971 | } else { |
| 972 | if (const DbgVariable *Var = dyn_cast<const DbgVariable>(Entity)) |
| 973 | applyVariableAttributes(*Var, *Die); |
| 974 | else if ((Label = dyn_cast<const DbgLabel>(Entity))) |
| 975 | applyLabelAttributes(*Label, *Die); |
| 976 | else |
| 977 | llvm_unreachable("DbgEntity must be DbgVariable or DbgLabel."); |
| 978 | } |
| 979 | |
Hsiangkai Wang | 55321d8 | 2018-09-26 04:19:23 +0000 | [diff] [blame] | 980 | if (Label) |
| 981 | if (const auto *Sym = Label->getSymbol()) |
| 982 | addLabelAddress(*Die, dwarf::DW_AT_low_pc, Sym); |
David Blaikie | 488393f | 2017-05-12 01:13:45 +0000 | [diff] [blame] | 983 | } |
| 984 | |
Hsiangkai Wang | 2532ac8 | 2018-08-17 15:22:04 +0000 | [diff] [blame] | 985 | DbgEntity *DwarfCompileUnit::getExistingAbstractEntity(const DINode *Node) { |
| 986 | auto &AbstractEntities = getAbstractEntities(); |
| 987 | auto I = AbstractEntities.find(Node); |
| 988 | if (I != AbstractEntities.end()) |
David Blaikie | 488393f | 2017-05-12 01:13:45 +0000 | [diff] [blame] | 989 | return I->second.get(); |
| 990 | return nullptr; |
| 991 | } |
| 992 | |
Hsiangkai Wang | 2532ac8 | 2018-08-17 15:22:04 +0000 | [diff] [blame] | 993 | void DwarfCompileUnit::createAbstractEntity(const DINode *Node, |
| 994 | LexicalScope *Scope) { |
David Blaikie | 488393f | 2017-05-12 01:13:45 +0000 | [diff] [blame] | 995 | assert(Scope && Scope->isAbstractScope()); |
Hsiangkai Wang | 2532ac8 | 2018-08-17 15:22:04 +0000 | [diff] [blame] | 996 | auto &Entity = getAbstractEntities()[Node]; |
| 997 | if (isa<const DILocalVariable>(Node)) { |
| 998 | Entity = llvm::make_unique<DbgVariable>( |
| 999 | cast<const DILocalVariable>(Node), nullptr /* IA */);; |
| 1000 | DU->addScopeVariable(Scope, cast<DbgVariable>(Entity.get())); |
| 1001 | } else if (isa<const DILabel>(Node)) { |
| 1002 | Entity = llvm::make_unique<DbgLabel>( |
| 1003 | cast<const DILabel>(Node), nullptr /* IA */); |
| 1004 | DU->addScopeLabel(Scope, cast<DbgLabel>(Entity.get())); |
| 1005 | } |
David Blaikie | 488393f | 2017-05-12 01:13:45 +0000 | [diff] [blame] | 1006 | } |
| 1007 | |
Rafael Espindola | 063d725 | 2015-03-10 16:58:10 +0000 | [diff] [blame] | 1008 | void DwarfCompileUnit::emitHeader(bool UseOffsets) { |
David Blaikie | b6726a9 | 2014-11-02 02:26:24 +0000 | [diff] [blame] | 1009 | // Don't bother labeling the .dwo unit, as its offset isn't used. |
Alexey Bataev | bff3608 | 2018-03-23 13:35:54 +0000 | [diff] [blame] | 1010 | if (!Skeleton && !DD->useSectionsAsReferences()) { |
Rafael Espindola | 9ab0923 | 2015-03-17 20:07:06 +0000 | [diff] [blame] | 1011 | LabelBegin = Asm->createTempSymbol("cu_begin"); |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 1012 | Asm->OutStreamer->EmitLabel(LabelBegin); |
Rafael Espindola | 3c31114 | 2015-03-10 03:11:11 +0000 | [diff] [blame] | 1013 | } |
David Blaikie | ae57e66 | 2014-11-01 23:59:23 +0000 | [diff] [blame] | 1014 | |
Paul Robinson | cddd604 | 2017-02-28 20:24:55 +0000 | [diff] [blame] | 1015 | dwarf::UnitType UT = Skeleton ? dwarf::DW_UT_split_compile |
| 1016 | : DD->useSplitDwarf() ? dwarf::DW_UT_skeleton |
| 1017 | : dwarf::DW_UT_compile; |
| 1018 | DwarfUnit::emitCommonHeader(UseOffsets, UT); |
Paul Robinson | 543c0e1 | 2018-05-22 17:27:31 +0000 | [diff] [blame] | 1019 | if (DD->getDwarfVersion() >= 5 && UT != dwarf::DW_UT_compile) |
| 1020 | Asm->emitInt64(getDWOId()); |
David Blaikie | ae57e66 | 2014-11-01 23:59:23 +0000 | [diff] [blame] | 1021 | } |
| 1022 | |
Peter Collingbourne | b52e236 | 2017-09-12 21:50:41 +0000 | [diff] [blame] | 1023 | bool DwarfCompileUnit::hasDwarfPubSections() const { |
David Blaikie | 66cf14d | 2018-08-16 21:29:55 +0000 | [diff] [blame] | 1024 | switch (CUNode->getNameTableKind()) { |
| 1025 | case DICompileUnit::DebugNameTableKind::None: |
| 1026 | return false; |
| 1027 | // Opting in to GNU Pubnames/types overrides the default to ensure these are |
| 1028 | // generated for things like Gold's gdb_index generation. |
| 1029 | case DICompileUnit::DebugNameTableKind::GNU: |
Peter Collingbourne | b52e236 | 2017-09-12 21:50:41 +0000 | [diff] [blame] | 1030 | return true; |
David Blaikie | 66cf14d | 2018-08-16 21:29:55 +0000 | [diff] [blame] | 1031 | case DICompileUnit::DebugNameTableKind::Default: |
David Blaikie | 0e03047 | 2018-08-16 23:57:15 +0000 | [diff] [blame] | 1032 | return DD->tuneForGDB() && !includeMinimalInlineScopes() && |
David Blaikie | 2f51176 | 2019-04-23 19:00:45 +0000 | [diff] [blame] | 1033 | !CUNode->isDebugDirectivesOnly() && |
| 1034 | DD->getAccelTableKind() != AccelTableKind::Apple && |
| 1035 | DD->getDwarfVersion() < 5; |
David Blaikie | 66cf14d | 2018-08-16 21:29:55 +0000 | [diff] [blame] | 1036 | } |
Simon Pilgrim | 22d580f2 | 2018-08-17 09:47:52 +0000 | [diff] [blame] | 1037 | llvm_unreachable("Unhandled DICompileUnit::DebugNameTableKind enum"); |
Peter Collingbourne | b52e236 | 2017-09-12 21:50:41 +0000 | [diff] [blame] | 1038 | } |
| 1039 | |
David Blaikie | 192b45c | 2014-11-02 06:16:39 +0000 | [diff] [blame] | 1040 | /// addGlobalName - Add a new global name to the compile unit. |
David Blaikie | a0e3c75 | 2017-02-03 00:44:18 +0000 | [diff] [blame] | 1041 | void DwarfCompileUnit::addGlobalName(StringRef Name, const DIE &Die, |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 1042 | const DIScope *Context) { |
Peter Collingbourne | b52e236 | 2017-09-12 21:50:41 +0000 | [diff] [blame] | 1043 | if (!hasDwarfPubSections()) |
David Blaikie | 192b45c | 2014-11-02 06:16:39 +0000 | [diff] [blame] | 1044 | return; |
| 1045 | std::string FullName = getParentContextString(Context) + Name.str(); |
| 1046 | GlobalNames[FullName] = &Die; |
| 1047 | } |
| 1048 | |
David Blaikie | a0e3c75 | 2017-02-03 00:44:18 +0000 | [diff] [blame] | 1049 | void DwarfCompileUnit::addGlobalNameForTypeUnit(StringRef Name, |
| 1050 | const DIScope *Context) { |
Peter Collingbourne | b52e236 | 2017-09-12 21:50:41 +0000 | [diff] [blame] | 1051 | if (!hasDwarfPubSections()) |
David Blaikie | a0e3c75 | 2017-02-03 00:44:18 +0000 | [diff] [blame] | 1052 | return; |
| 1053 | std::string FullName = getParentContextString(Context) + Name.str(); |
| 1054 | // Insert, allowing the entry to remain as-is if it's already present |
| 1055 | // This way the CU-level type DIE is preferred over the "can't describe this |
| 1056 | // type as a unit offset because it's not really in the CU at all, it's only |
| 1057 | // in a type unit" |
| 1058 | GlobalNames.insert(std::make_pair(std::move(FullName), &getUnitDie())); |
| 1059 | } |
| 1060 | |
David Blaikie | 192b45c | 2014-11-02 06:16:39 +0000 | [diff] [blame] | 1061 | /// Add a new global type to the unit. |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 1062 | void DwarfCompileUnit::addGlobalType(const DIType *Ty, const DIE &Die, |
| 1063 | const DIScope *Context) { |
Peter Collingbourne | b52e236 | 2017-09-12 21:50:41 +0000 | [diff] [blame] | 1064 | if (!hasDwarfPubSections()) |
David Blaikie | 192b45c | 2014-11-02 06:16:39 +0000 | [diff] [blame] | 1065 | return; |
Duncan P. N. Exon Smith | b105564 | 2015-04-16 01:01:28 +0000 | [diff] [blame] | 1066 | std::string FullName = getParentContextString(Context) + Ty->getName().str(); |
David Blaikie | 192b45c | 2014-11-02 06:16:39 +0000 | [diff] [blame] | 1067 | GlobalTypes[FullName] = &Die; |
| 1068 | } |
David Blaikie | ae57e66 | 2014-11-01 23:59:23 +0000 | [diff] [blame] | 1069 | |
David Blaikie | a0e3c75 | 2017-02-03 00:44:18 +0000 | [diff] [blame] | 1070 | void DwarfCompileUnit::addGlobalTypeUnitType(const DIType *Ty, |
| 1071 | const DIScope *Context) { |
Peter Collingbourne | b52e236 | 2017-09-12 21:50:41 +0000 | [diff] [blame] | 1072 | if (!hasDwarfPubSections()) |
David Blaikie | a0e3c75 | 2017-02-03 00:44:18 +0000 | [diff] [blame] | 1073 | return; |
| 1074 | std::string FullName = getParentContextString(Context) + Ty->getName().str(); |
| 1075 | // Insert, allowing the entry to remain as-is if it's already present |
| 1076 | // This way the CU-level type DIE is preferred over the "can't describe this |
| 1077 | // type as a unit offset because it's not really in the CU at all, it's only |
| 1078 | // in a type unit" |
| 1079 | GlobalTypes.insert(std::make_pair(std::move(FullName), &getUnitDie())); |
| 1080 | } |
| 1081 | |
David Blaikie | 7d48be2 | 2014-11-02 06:37:23 +0000 | [diff] [blame] | 1082 | /// addVariableAddress - Add DW_AT_location attribute for a |
| 1083 | /// DbgVariable based on provided MachineLocation. |
| 1084 | void DwarfCompileUnit::addVariableAddress(const DbgVariable &DV, DIE &Die, |
| 1085 | MachineLocation Location) { |
Adrian Prantl | a2f9e3a | 2017-10-12 22:54:36 +0000 | [diff] [blame] | 1086 | // addBlockByrefAddress is obsolete and will be removed soon. |
| 1087 | // The clang frontend always generates block byref variables with a |
| 1088 | // complex expression that encodes exactly what addBlockByrefAddress |
| 1089 | // would do. |
| 1090 | assert((!DV.isBlockByrefVariable() || DV.hasComplexAddress()) && |
| 1091 | "block byref variable without a complex expression"); |
Duncan P. N. Exon Smith | e6cc531 | 2015-06-21 16:50:43 +0000 | [diff] [blame] | 1092 | if (DV.hasComplexAddress()) |
David Blaikie | 7d48be2 | 2014-11-02 06:37:23 +0000 | [diff] [blame] | 1093 | addComplexAddress(DV, Die, dwarf::DW_AT_location, Location); |
David Blaikie | 7d48be2 | 2014-11-02 06:37:23 +0000 | [diff] [blame] | 1094 | else |
Adrian Prantl | 5883af3 | 2015-01-19 17:57:29 +0000 | [diff] [blame] | 1095 | addAddress(Die, dwarf::DW_AT_location, Location); |
David Blaikie | 7d48be2 | 2014-11-02 06:37:23 +0000 | [diff] [blame] | 1096 | } |
David Blaikie | f7435ee | 2014-11-02 06:46:40 +0000 | [diff] [blame] | 1097 | |
| 1098 | /// Add an address attribute to a die based on the location provided. |
| 1099 | void DwarfCompileUnit::addAddress(DIE &Die, dwarf::Attribute Attribute, |
Adrian Prantl | 5883af3 | 2015-01-19 17:57:29 +0000 | [diff] [blame] | 1100 | const MachineLocation &Location) { |
Duncan P. N. Exon Smith | e7e1d0c | 2015-05-27 22:14:58 +0000 | [diff] [blame] | 1101 | DIELoc *Loc = new (DIEValueAllocator) DIELoc; |
Adrian Prantl | 956484b | 2017-03-20 21:35:09 +0000 | [diff] [blame] | 1102 | DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc); |
Adrian Prantl | c12cee3 | 2017-04-19 23:42:25 +0000 | [diff] [blame] | 1103 | if (Location.isIndirect()) |
| 1104 | DwarfExpr.setMemoryLocationKind(); |
David Blaikie | f7435ee | 2014-11-02 06:46:40 +0000 | [diff] [blame] | 1105 | |
Adrian Prantl | 2049c0d | 2017-08-02 15:22:17 +0000 | [diff] [blame] | 1106 | DIExpressionCursor Cursor({}); |
Adrian Prantl | 956484b | 2017-03-20 21:35:09 +0000 | [diff] [blame] | 1107 | const TargetRegisterInfo &TRI = *Asm->MF->getSubtarget().getRegisterInfo(); |
Adrian Prantl | c12cee3 | 2017-04-19 23:42:25 +0000 | [diff] [blame] | 1108 | if (!DwarfExpr.addMachineRegExpression(TRI, Cursor, Location.getReg())) |
Adrian Prantl | ab255fc | 2014-12-05 01:02:46 +0000 | [diff] [blame] | 1109 | return; |
Adrian Prantl | 956484b | 2017-03-20 21:35:09 +0000 | [diff] [blame] | 1110 | DwarfExpr.addExpression(std::move(Cursor)); |
Adrian Prantl | ab255fc | 2014-12-05 01:02:46 +0000 | [diff] [blame] | 1111 | |
David Blaikie | f7435ee | 2014-11-02 06:46:40 +0000 | [diff] [blame] | 1112 | // Now attach the location information to the DIE. |
Adrian Prantl | 956484b | 2017-03-20 21:35:09 +0000 | [diff] [blame] | 1113 | addBlock(Die, Attribute, DwarfExpr.finalize()); |
David Blaikie | f7435ee | 2014-11-02 06:46:40 +0000 | [diff] [blame] | 1114 | } |
David Blaikie | 77895fb | 2014-11-02 06:58:44 +0000 | [diff] [blame] | 1115 | |
| 1116 | /// Start with the address based on the location provided, and generate the |
| 1117 | /// DWARF information necessary to find the actual variable given the extra |
| 1118 | /// address information encoded in the DbgVariable, starting from the starting |
| 1119 | /// location. Add the DWARF information to the die. |
| 1120 | void DwarfCompileUnit::addComplexAddress(const DbgVariable &DV, DIE &Die, |
| 1121 | dwarf::Attribute Attribute, |
| 1122 | const MachineLocation &Location) { |
Duncan P. N. Exon Smith | e7e1d0c | 2015-05-27 22:14:58 +0000 | [diff] [blame] | 1123 | DIELoc *Loc = new (DIEValueAllocator) DIELoc; |
Adrian Prantl | 7813d9c | 2015-01-14 01:01:30 +0000 | [diff] [blame] | 1124 | DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc); |
Adrian Prantl | 956484b | 2017-03-20 21:35:09 +0000 | [diff] [blame] | 1125 | const DIExpression *DIExpr = DV.getSingleExpression(); |
| 1126 | DwarfExpr.addFragmentOffset(DIExpr); |
Adrian Prantl | c12cee3 | 2017-04-19 23:42:25 +0000 | [diff] [blame] | 1127 | if (Location.isIndirect()) |
| 1128 | DwarfExpr.setMemoryLocationKind(); |
Adrian Prantl | 956484b | 2017-03-20 21:35:09 +0000 | [diff] [blame] | 1129 | |
Adrian Prantl | 2049c0d | 2017-08-02 15:22:17 +0000 | [diff] [blame] | 1130 | DIExpressionCursor Cursor(DIExpr); |
Djordje Todorovic | 12aca5d | 2019-07-09 08:36:34 +0000 | [diff] [blame^] | 1131 | |
| 1132 | if (DIExpr->isEntryValue()) { |
| 1133 | DwarfExpr.setEntryValueFlag(); |
| 1134 | DwarfExpr.addEntryValueExpression(Cursor); |
| 1135 | } |
| 1136 | |
Peter Collingbourne | 96c9ae6 | 2016-05-20 19:35:17 +0000 | [diff] [blame] | 1137 | const TargetRegisterInfo &TRI = *Asm->MF->getSubtarget().getRegisterInfo(); |
Adrian Prantl | c12cee3 | 2017-04-19 23:42:25 +0000 | [diff] [blame] | 1138 | if (!DwarfExpr.addMachineRegExpression(TRI, Cursor, Location.getReg())) |
Adrian Prantl | 54286bd | 2016-11-02 16:12:20 +0000 | [diff] [blame] | 1139 | return; |
Adrian Prantl | 956484b | 2017-03-20 21:35:09 +0000 | [diff] [blame] | 1140 | DwarfExpr.addExpression(std::move(Cursor)); |
David Blaikie | 77895fb | 2014-11-02 06:58:44 +0000 | [diff] [blame] | 1141 | |
| 1142 | // Now attach the location information to the DIE. |
Adrian Prantl | 956484b | 2017-03-20 21:35:09 +0000 | [diff] [blame] | 1143 | addBlock(Die, Attribute, DwarfExpr.finalize()); |
David Blaikie | 77895fb | 2014-11-02 06:58:44 +0000 | [diff] [blame] | 1144 | } |
David Blaikie | 4bc0881 | 2014-11-02 07:03:19 +0000 | [diff] [blame] | 1145 | |
| 1146 | /// Add a Dwarf loclistptr attribute data and value. |
| 1147 | void DwarfCompileUnit::addLocationList(DIE &Die, dwarf::Attribute Attribute, |
| 1148 | unsigned Index) { |
David Blaikie | 4bc0881 | 2014-11-02 07:03:19 +0000 | [diff] [blame] | 1149 | dwarf::Form Form = DD->getDwarfVersion() >= 4 ? dwarf::DW_FORM_sec_offset |
| 1150 | : dwarf::DW_FORM_data4; |
Duncan P. N. Exon Smith | 4fb1f9c | 2015-06-25 23:46:41 +0000 | [diff] [blame] | 1151 | Die.addValue(DIEValueAllocator, Attribute, Form, DIELocList(Index)); |
David Blaikie | 4bc0881 | 2014-11-02 07:03:19 +0000 | [diff] [blame] | 1152 | } |
David Blaikie | 02a6333 | 2014-11-02 07:06:51 +0000 | [diff] [blame] | 1153 | |
David Blaikie | 8c485b5 | 2014-11-02 07:08:12 +0000 | [diff] [blame] | 1154 | void DwarfCompileUnit::applyVariableAttributes(const DbgVariable &Var, |
| 1155 | DIE &VariableDie) { |
David Blaikie | 02a6333 | 2014-11-02 07:06:51 +0000 | [diff] [blame] | 1156 | StringRef Name = Var.getName(); |
| 1157 | if (!Name.empty()) |
| 1158 | addString(VariableDie, dwarf::DW_AT_name, Name); |
Victor Leschuk | 3c98998 | 2016-10-26 11:59:03 +0000 | [diff] [blame] | 1159 | const auto *DIVar = Var.getVariable(); |
| 1160 | if (DIVar) |
| 1161 | if (uint32_t AlignInBytes = DIVar->getAlignInBytes()) |
| 1162 | addUInt(VariableDie, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata, |
| 1163 | AlignInBytes); |
| 1164 | |
| 1165 | addSourceLine(VariableDie, DIVar); |
David Blaikie | 02a6333 | 2014-11-02 07:06:51 +0000 | [diff] [blame] | 1166 | addType(VariableDie, Var.getType()); |
| 1167 | if (Var.isArtificial()) |
| 1168 | addFlag(VariableDie, dwarf::DW_AT_artificial); |
| 1169 | } |
David Blaikie | 9780208 | 2014-11-02 07:11:55 +0000 | [diff] [blame] | 1170 | |
Hsiangkai Wang | 2532ac8 | 2018-08-17 15:22:04 +0000 | [diff] [blame] | 1171 | void DwarfCompileUnit::applyLabelAttributes(const DbgLabel &Label, |
| 1172 | DIE &LabelDie) { |
| 1173 | StringRef Name = Label.getName(); |
| 1174 | if (!Name.empty()) |
| 1175 | addString(LabelDie, dwarf::DW_AT_name, Name); |
| 1176 | const auto *DILabel = Label.getLabel(); |
| 1177 | addSourceLine(LabelDie, DILabel); |
| 1178 | } |
| 1179 | |
David Blaikie | 9780208 | 2014-11-02 07:11:55 +0000 | [diff] [blame] | 1180 | /// Add a Dwarf expression attribute data and value. |
| 1181 | void DwarfCompileUnit::addExpr(DIELoc &Die, dwarf::Form Form, |
| 1182 | const MCExpr *Expr) { |
Duncan P. N. Exon Smith | 4fb1f9c | 2015-06-25 23:46:41 +0000 | [diff] [blame] | 1183 | Die.addValue(DIEValueAllocator, (dwarf::Attribute)0, Form, DIEExpr(Expr)); |
David Blaikie | 9780208 | 2014-11-02 07:11:55 +0000 | [diff] [blame] | 1184 | } |
David Blaikie | 3363a57 | 2014-11-02 08:09:09 +0000 | [diff] [blame] | 1185 | |
Vedant Kumar | 74533bd | 2018-10-22 21:44:21 +0000 | [diff] [blame] | 1186 | void DwarfCompileUnit::addAddressExpr(DIE &Die, dwarf::Attribute Attribute, |
| 1187 | const MCExpr *Expr) { |
| 1188 | Die.addValue(DIEValueAllocator, Attribute, dwarf::DW_FORM_addr, |
| 1189 | DIEExpr(Expr)); |
| 1190 | } |
| 1191 | |
Duncan P. N. Exon Smith | 2fbe135 | 2015-04-20 22:10:08 +0000 | [diff] [blame] | 1192 | void DwarfCompileUnit::applySubprogramAttributesToDefinition( |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 1193 | const DISubprogram *SP, DIE &SPDie) { |
Duncan P. N. Exon Smith | 537b4a8 | 2015-04-14 03:40:37 +0000 | [diff] [blame] | 1194 | auto *SPDecl = SP->getDeclaration(); |
Fangrui Song | da82ce9 | 2019-05-07 02:06:37 +0000 | [diff] [blame] | 1195 | auto *Context = SPDecl ? SPDecl->getScope() : SP->getScope(); |
David Blaikie | 3a443c2 | 2014-11-04 22:12:25 +0000 | [diff] [blame] | 1196 | applySubprogramAttributes(SP, SPDie, includeMinimalInlineScopes()); |
Duncan P. N. Exon Smith | 537b4a8 | 2015-04-14 03:40:37 +0000 | [diff] [blame] | 1197 | addGlobalName(SP->getName(), SPDie, Context); |
David Blaikie | 3363a57 | 2014-11-02 08:09:09 +0000 | [diff] [blame] | 1198 | } |
David Blaikie | cafd962 | 2014-11-02 08:51:37 +0000 | [diff] [blame] | 1199 | |
| 1200 | bool DwarfCompileUnit::isDwoUnit() const { |
| 1201 | return DD->useSplitDwarf() && Skeleton; |
| 1202 | } |
David Blaikie | 3a443c2 | 2014-11-04 22:12:25 +0000 | [diff] [blame] | 1203 | |
David Blaikie | 832c7d9 | 2019-04-24 18:09:44 +0000 | [diff] [blame] | 1204 | void DwarfCompileUnit::finishNonUnitTypeDIE(DIE& D, const DICompositeType *CTy) { |
| 1205 | constructTypeDIE(D, CTy); |
| 1206 | } |
| 1207 | |
David Blaikie | 3a443c2 | 2014-11-04 22:12:25 +0000 | [diff] [blame] | 1208 | bool DwarfCompileUnit::includeMinimalInlineScopes() const { |
Adrian Prantl | b939a25 | 2016-03-31 23:56:58 +0000 | [diff] [blame] | 1209 | return getCUNode()->getEmissionKind() == DICompileUnit::LineTablesOnly || |
David Blaikie | 3a443c2 | 2014-11-04 22:12:25 +0000 | [diff] [blame] | 1210 | (DD->useSplitDwarf() && !Skeleton); |
| 1211 | } |
David Blaikie | 61c127c | 2018-12-14 22:34:03 +0000 | [diff] [blame] | 1212 | |
| 1213 | void DwarfCompileUnit::addAddrTableBase() { |
| 1214 | const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering(); |
| 1215 | MCSymbol *Label = DD->getAddressPool().getLabel(); |
| 1216 | addSectionLabel(getUnitDie(), |
| 1217 | getDwarfVersion() >= 5 ? dwarf::DW_AT_addr_base |
| 1218 | : dwarf::DW_AT_GNU_addr_base, |
| 1219 | Label, TLOF.getDwarfAddrSection()->getBeginSymbol()); |
| 1220 | } |
Markus Lavin | b86ce21 | 2019-03-19 13:16:28 +0000 | [diff] [blame] | 1221 | |
| 1222 | void DwarfCompileUnit::addBaseTypeRef(DIEValueList &Die, int64_t Idx) { |
| 1223 | Die.addValue(DIEValueAllocator, (dwarf::Attribute)0, dwarf::DW_FORM_udata, |
| 1224 | new (DIEValueAllocator) DIEBaseTypeRef(this, Idx)); |
| 1225 | } |
| 1226 | |
| 1227 | void DwarfCompileUnit::createBaseTypeDIEs() { |
| 1228 | // Insert the base_type DIEs directly after the CU so that their offsets will |
| 1229 | // fit in the fixed size ULEB128 used inside the location expressions. |
| 1230 | // Maintain order by iterating backwards and inserting to the front of CU |
| 1231 | // child list. |
| 1232 | for (auto &Btr : reverse(ExprRefedBaseTypes)) { |
| 1233 | DIE &Die = getUnitDie().addChildFront( |
| 1234 | DIE::get(DIEValueAllocator, dwarf::DW_TAG_base_type)); |
| 1235 | SmallString<32> Str; |
| 1236 | addString(Die, dwarf::DW_AT_name, |
| 1237 | Twine(dwarf::AttributeEncodingString(Btr.Encoding) + |
| 1238 | "_" + Twine(Btr.BitSize)).toStringRef(Str)); |
| 1239 | addUInt(Die, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1, Btr.Encoding); |
| 1240 | addUInt(Die, dwarf::DW_AT_byte_size, None, Btr.BitSize / 8); |
| 1241 | |
| 1242 | Btr.Die = &Die; |
| 1243 | } |
| 1244 | } |