1//===-- llvm/CodeGen/DwarfUnit.cpp - Dwarf Type and Compile Units ---------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9// This file contains support for constructing a dwarf compile unit.
10//
11//===----------------------------------------------------------------------===//
12
13#include "DwarfUnit.h"
14#include "AddressPool.h"
15#include "DwarfCompileUnit.h"
16#include "DwarfExpression.h"
17#include "llvm/ADT/APFloat.h"
18#include "llvm/ADT/APInt.h"
19#include "llvm/CodeGen/TargetRegisterInfo.h"
20#include "llvm/IR/Constants.h"
21#include "llvm/IR/DataLayout.h"
22#include "llvm/IR/GlobalValue.h"
23#include "llvm/IR/Metadata.h"
24#include "llvm/MC/MCAsmInfo.h"
25#include "llvm/MC/MCContext.h"
26#include "llvm/MC/MCDwarf.h"
27#include "llvm/MC/MCSection.h"
28#include "llvm/MC/MCStreamer.h"
29#include "llvm/Support/Casting.h"
30#include "llvm/Target/TargetLoweringObjectFile.h"
31#include <cassert>
32#include <cstdint>
33#include <string>
34#include <utility>
35
36using namespace llvm;
37
38#define DEBUG_TYPE "dwarfdebug"
39
40DIEDwarfExpression::DIEDwarfExpression(const AsmPrinter &AP,
41 DwarfCompileUnit &CU, DIELoc &DIE)
42 : DwarfExpression(AP.getDwarfVersion(), CU), AP(AP), OutDIE(DIE) {}
43
44void DIEDwarfExpression::emitOp(uint8_t Op, const char* Comment) {
45 CU.addUInt(Block&: getActiveDIE(), Form: dwarf::DW_FORM_data1, Integer: Op);
46}
47
48void DIEDwarfExpression::emitSigned(int64_t Value) {
49 CU.addSInt(Die&: getActiveDIE(), Form: dwarf::DW_FORM_sdata, Integer: Value);
50}
51
52void DIEDwarfExpression::emitUnsigned(uint64_t Value) {
53 CU.addUInt(Block&: getActiveDIE(), Form: dwarf::DW_FORM_udata, Integer: Value);
54}
55
56void DIEDwarfExpression::emitData1(uint8_t Value) {
57 CU.addUInt(Block&: getActiveDIE(), Form: dwarf::DW_FORM_data1, Integer: Value);
58}
59
60void DIEDwarfExpression::emitBaseTypeRef(uint64_t Idx) {
61 CU.addBaseTypeRef(Die&: getActiveDIE(), Idx);
62}
63
64void DIEDwarfExpression::enableTemporaryBuffer() {
65 assert(!IsBuffering && "Already buffering?");
66 IsBuffering = true;
67}
68
69void DIEDwarfExpression::disableTemporaryBuffer() { IsBuffering = false; }
70
71unsigned DIEDwarfExpression::getTemporaryBufferSize() {
72 return TmpDIE.computeSize(FormParams: AP.getDwarfFormParams());
73}
74
75void DIEDwarfExpression::commitTemporaryBuffer() { OutDIE.takeValues(Other&: TmpDIE); }
76
77bool DIEDwarfExpression::isFrameRegister(const TargetRegisterInfo &TRI,
78 llvm::Register MachineReg) {
79 return MachineReg == TRI.getFrameRegister(MF: *AP.MF);
80}
81
82DwarfUnit::DwarfUnit(dwarf::Tag UnitTag, const DICompileUnit *Node,
83 AsmPrinter *A, DwarfDebug *DW, DwarfFile *DWU,
84 unsigned UniqueID)
85 : DIEUnit(UnitTag), UniqueID(UniqueID), CUNode(Node), Asm(A), DD(DW),
86 DU(DWU) {}
87
88DwarfTypeUnit::DwarfTypeUnit(DwarfCompileUnit &CU, AsmPrinter *A,
89 DwarfDebug *DW, DwarfFile *DWU, unsigned UniqueID,
90 MCDwarfDwoLineTable *SplitLineTable)
91 : DwarfUnit(dwarf::DW_TAG_type_unit, CU.getCUNode(), A, DW, DWU, UniqueID),
92 CU(CU), SplitLineTable(SplitLineTable) {}
93
94DwarfUnit::~DwarfUnit() {
95 for (DIEBlock *B : DIEBlocks)
96 B->~DIEBlock();
97 for (DIELoc *L : DIELocs)
98 L->~DIELoc();
99}
100
101int64_t DwarfUnit::getDefaultLowerBound() const {
102 switch (getLanguage()) {
103 default:
104 break;
105
106 // The languages below have valid values in all DWARF versions.
107 case dwarf::DW_LANG_C:
108 case dwarf::DW_LANG_C89:
109 case dwarf::DW_LANG_C_plus_plus:
110 return 0;
111
112 case dwarf::DW_LANG_Fortran77:
113 case dwarf::DW_LANG_Fortran90:
114 return 1;
115
116 // The languages below have valid values only if the DWARF version >= 3.
117 case dwarf::DW_LANG_C99:
118 case dwarf::DW_LANG_ObjC:
119 case dwarf::DW_LANG_ObjC_plus_plus:
120 if (DD->getDwarfVersion() >= 3)
121 return 0;
122 break;
123
124 case dwarf::DW_LANG_Fortran95:
125 if (DD->getDwarfVersion() >= 3)
126 return 1;
127 break;
128
129 // Starting with DWARF v4, all defined languages have valid values.
130 case dwarf::DW_LANG_D:
131 case dwarf::DW_LANG_Java:
132 case dwarf::DW_LANG_Python:
133 case dwarf::DW_LANG_UPC:
134 if (DD->getDwarfVersion() >= 4)
135 return 0;
136 break;
137
138 case dwarf::DW_LANG_Ada83:
139 case dwarf::DW_LANG_Ada95:
140 case dwarf::DW_LANG_Cobol74:
141 case dwarf::DW_LANG_Cobol85:
142 case dwarf::DW_LANG_Modula2:
143 case dwarf::DW_LANG_Pascal83:
144 case dwarf::DW_LANG_PLI:
145 if (DD->getDwarfVersion() >= 4)
146 return 1;
147 break;
148
149 // The languages below are new in DWARF v5.
150 case dwarf::DW_LANG_BLISS:
151 case dwarf::DW_LANG_C11:
152 case dwarf::DW_LANG_C_plus_plus_03:
153 case dwarf::DW_LANG_C_plus_plus_11:
154 case dwarf::DW_LANG_C_plus_plus_14:
155 case dwarf::DW_LANG_Dylan:
156 case dwarf::DW_LANG_Go:
157 case dwarf::DW_LANG_Haskell:
158 case dwarf::DW_LANG_OCaml:
159 case dwarf::DW_LANG_OpenCL:
160 case dwarf::DW_LANG_RenderScript:
161 case dwarf::DW_LANG_Rust:
162 case dwarf::DW_LANG_Swift:
163 if (DD->getDwarfVersion() >= 5)
164 return 0;
165 break;
166
167 case dwarf::DW_LANG_Fortran03:
168 case dwarf::DW_LANG_Fortran08:
169 case dwarf::DW_LANG_Julia:
170 case dwarf::DW_LANG_Modula3:
171 if (DD->getDwarfVersion() >= 5)
172 return 1;
173 break;
174 }
175
176 return -1;
177}
178
179/// Check whether the DIE for this MDNode can be shared across CUs.
180bool DwarfUnit::isShareableAcrossCUs(const DINode *D) const {
181 // When the MDNode can be part of the type system, the DIE can be shared
182 // across CUs.
183 // Combining type units and cross-CU DIE sharing is lower value (since
184 // cross-CU DIE sharing is used in LTO and removes type redundancy at that
185 // level already) but may be implementable for some value in projects
186 // building multiple independent libraries with LTO and then linking those
187 // together.
188 if (isDwoUnit() && !DD->shareAcrossDWOCUs())
189 return false;
190 return (isa<DIType>(Val: D) ||
191 (isa<DISubprogram>(Val: D) && !cast<DISubprogram>(Val: D)->isDefinition())) &&
192 !DD->generateTypeUnits();
193}
194
195DIE *DwarfUnit::getDIE(const DINode *D) const {
196 if (isShareableAcrossCUs(D))
197 return DU->getDIE(TypeMD: D);
198 return MDNodeToDieMap.lookup(Val: D);
199}
200
201void DwarfUnit::insertDIE(const DINode *Desc, DIE *D) {
202 if (isShareableAcrossCUs(D: Desc)) {
203 DU->insertDIE(TypeMD: Desc, Die: D);
204 return;
205 }
206 MDNodeToDieMap.insert(KV: std::make_pair(x&: Desc, y&: D));
207}
208
209void DwarfUnit::insertDIE(DIE *D) {
210 MDNodeToDieMap.insert(KV: std::make_pair(x: nullptr, y&: D));
211}
212
213void DwarfUnit::addFlag(DIE &Die, dwarf::Attribute Attribute) {
214 if (DD->getDwarfVersion() >= 4)
215 addAttribute(Die, Attribute, Form: dwarf::DW_FORM_flag_present, Value: DIEInteger(1));
216 else
217 addAttribute(Die, Attribute, Form: dwarf::DW_FORM_flag, Value: DIEInteger(1));
218}
219
220void DwarfUnit::addUInt(DIEValueList &Die, dwarf::Attribute Attribute,
221 std::optional<dwarf::Form> Form, uint64_t Integer) {
222 if (!Form)
223 Form = DIEInteger::BestForm(IsSigned: false, Int: Integer);
224 assert(Form != dwarf::DW_FORM_implicit_const &&
225 "DW_FORM_implicit_const is used only for signed integers");
226 addAttribute(Die, Attribute, Form: *Form, Value: DIEInteger(Integer));
227}
228
229void DwarfUnit::addUInt(DIEValueList &Block, dwarf::Form Form,
230 uint64_t Integer) {
231 addUInt(Die&: Block, Attribute: (dwarf::Attribute)0, Form, Integer);
232}
233
234void DwarfUnit::addSInt(DIEValueList &Die, dwarf::Attribute Attribute,
235 std::optional<dwarf::Form> Form, int64_t Integer) {
236 if (!Form)
237 Form = DIEInteger::BestForm(IsSigned: true, Int: Integer);
238 addAttribute(Die, Attribute, Form: *Form, Value: DIEInteger(Integer));
239}
240
241void DwarfUnit::addSInt(DIELoc &Die, std::optional<dwarf::Form> Form,
242 int64_t Integer) {
243 addSInt(Die, Attribute: (dwarf::Attribute)0, Form, Integer);
244}
245
246void DwarfUnit::addString(DIE &Die, dwarf::Attribute Attribute,
247 StringRef String) {
248 if (CUNode->isDebugDirectivesOnly())
249 return;
250
251 if (DD->useInlineStrings()) {
252 addAttribute(Die, Attribute, Form: dwarf::DW_FORM_string,
253 Value: new (DIEValueAllocator)
254 DIEInlineString(String, DIEValueAllocator));
255 return;
256 }
257 dwarf::Form IxForm =
258 isDwoUnit() ? dwarf::DW_FORM_GNU_str_index : dwarf::DW_FORM_strp;
259
260 auto StringPoolEntry =
261 useSegmentedStringOffsetsTable() || IxForm == dwarf::DW_FORM_GNU_str_index
262 ? DU->getStringPool().getIndexedEntry(Asm&: *Asm, Str: String)
263 : DU->getStringPool().getEntry(Asm&: *Asm, Str: String);
264
265 // For DWARF v5 and beyond, use the smallest strx? form possible.
266 if (useSegmentedStringOffsetsTable()) {
267 IxForm = dwarf::DW_FORM_strx1;
268 unsigned Index = StringPoolEntry.getIndex();
269 if (Index > 0xffffff)
270 IxForm = dwarf::DW_FORM_strx4;
271 else if (Index > 0xffff)
272 IxForm = dwarf::DW_FORM_strx3;
273 else if (Index > 0xff)
274 IxForm = dwarf::DW_FORM_strx2;
275 }
276 addAttribute(Die, Attribute, Form: IxForm, Value: DIEString(StringPoolEntry));
277}
278
279void DwarfUnit::addLabel(DIEValueList &Die, dwarf::Attribute Attribute,
280 dwarf::Form Form, const MCSymbol *Label) {
281 addAttribute(Die, Attribute, Form, Value: DIELabel(Label));
282}
283
284void DwarfUnit::addLabel(DIELoc &Die, dwarf::Form Form, const MCSymbol *Label) {
285 addLabel(Die, Attribute: (dwarf::Attribute)0, Form, Label);
286}
287
288void DwarfUnit::addSectionOffset(DIE &Die, dwarf::Attribute Attribute,
289 uint64_t Integer) {
290 addUInt(Die, Attribute, Form: DD->getDwarfSectionOffsetForm(), Integer);
291}
292
293unsigned DwarfTypeUnit::getOrCreateSourceID(const DIFile *File) {
294 if (!SplitLineTable)
295 return getCU().getOrCreateSourceID(File);
296 if (!UsedLineTable) {
297 UsedLineTable = true;
298 // This is a split type unit that needs a line table.
299 addSectionOffset(Die&: getUnitDie(), Attribute: dwarf::DW_AT_stmt_list, Integer: 0);
300 }
301 return SplitLineTable->getFile(
302 Directory: File->getDirectory(), FileName: File->getFilename(), Checksum: DD->getMD5AsBytes(File),
303 DwarfVersion: Asm->OutContext.getDwarfVersion(), Source: File->getSource());
304}
305
306void DwarfUnit::addPoolOpAddress(DIEValueList &Die, const MCSymbol *Label) {
307 bool UseAddrOffsetFormOrExpressions =
308 DD->useAddrOffsetForm() || DD->useAddrOffsetExpressions();
309
310 const MCSymbol *Base = nullptr;
311 if (Label->isInSection() && UseAddrOffsetFormOrExpressions)
312 Base = DD->getSectionLabel(S: &Label->getSection());
313
314 uint32_t Index = DD->getAddressPool().getIndex(Sym: Base ? Base : Label);
315
316 if (DD->getDwarfVersion() >= 5) {
317 addUInt(Block&: Die, Form: dwarf::DW_FORM_data1, Integer: dwarf::DW_OP_addrx);
318 addUInt(Block&: Die, Form: dwarf::DW_FORM_addrx, Integer: Index);
319 } else {
320 addUInt(Block&: Die, Form: dwarf::DW_FORM_data1, Integer: dwarf::DW_OP_GNU_addr_index);
321 addUInt(Block&: Die, Form: dwarf::DW_FORM_GNU_addr_index, Integer: Index);
322 }
323
324 if (Base && Base != Label) {
325 addUInt(Block&: Die, Form: dwarf::DW_FORM_data1, Integer: dwarf::DW_OP_const4u);
326 addLabelDelta(Die, Attribute: (dwarf::Attribute)0, Hi: Label, Lo: Base);
327 addUInt(Block&: Die, Form: dwarf::DW_FORM_data1, Integer: dwarf::DW_OP_plus);
328 }
329}
330
331void DwarfUnit::addOpAddress(DIELoc &Die, const MCSymbol *Sym) {
332 if (DD->getDwarfVersion() >= 5) {
333 addPoolOpAddress(Die, Label: Sym);
334 return;
335 }
336
337 if (DD->useSplitDwarf()) {
338 addPoolOpAddress(Die, Label: Sym);
339 return;
340 }
341
342 addUInt(Block&: Die, Form: dwarf::DW_FORM_data1, Integer: dwarf::DW_OP_addr);
343 addLabel(Die, Form: dwarf::DW_FORM_addr, Label: Sym);
344}
345
346void DwarfUnit::addLabelDelta(DIEValueList &Die, dwarf::Attribute Attribute,
347 const MCSymbol *Hi, const MCSymbol *Lo) {
348 addAttribute(Die, Attribute, Form: dwarf::DW_FORM_data4,
349 Value: new (DIEValueAllocator) DIEDelta(Hi, Lo));
350}
351
352void DwarfUnit::addDIEEntry(DIE &Die, dwarf::Attribute Attribute, DIE &Entry) {
353 addDIEEntry(Die, Attribute, Entry: DIEEntry(Entry));
354}
355
356void DwarfUnit::addDIETypeSignature(DIE &Die, uint64_t Signature) {
357 // Flag the type unit reference as a declaration so that if it contains
358 // members (implicit special members, static data member definitions, member
359 // declarations for definitions in this CU, etc) consumers don't get confused
360 // and think this is a full definition.
361 addFlag(Die, Attribute: dwarf::DW_AT_declaration);
362
363 addAttribute(Die, Attribute: dwarf::DW_AT_signature, Form: dwarf::DW_FORM_ref_sig8,
364 Value: DIEInteger(Signature));
365}
366
367void DwarfUnit::addDIEEntry(DIE &Die, dwarf::Attribute Attribute,
368 DIEEntry Entry) {
369 const DIEUnit *CU = Die.getUnit();
370 const DIEUnit *EntryCU = Entry.getEntry().getUnit();
371 if (!CU)
372 // We assume that Die belongs to this CU, if it is not linked to any CU yet.
373 CU = getUnitDie().getUnit();
374 if (!EntryCU)
375 EntryCU = getUnitDie().getUnit();
376 assert(EntryCU == CU || !DD->useSplitDwarf() || DD->shareAcrossDWOCUs() ||
377 !static_cast<const DwarfUnit*>(CU)->isDwoUnit());
378 addAttribute(Die, Attribute,
379 Form: EntryCU == CU ? dwarf::DW_FORM_ref4 : dwarf::DW_FORM_ref_addr,
380 Value&: Entry);
381}
382
383DIE &DwarfUnit::createAndAddDIE(dwarf::Tag Tag, DIE &Parent, const DINode *N) {
384 DIE &Die = Parent.addChild(Child: DIE::get(Alloc&: DIEValueAllocator, Tag));
385 if (N)
386 insertDIE(Desc: N, D: &Die);
387 return Die;
388}
389
390void DwarfUnit::addBlock(DIE &Die, dwarf::Attribute Attribute, DIELoc *Loc) {
391 Loc->computeSize(FormParams: Asm->getDwarfFormParams());
392 DIELocs.push_back(x: Loc); // Memoize so we can call the destructor later on.
393 addAttribute(Die, Attribute, Form: Loc->BestForm(DwarfVersion: DD->getDwarfVersion()), Value&: Loc);
394}
395
396void DwarfUnit::addBlock(DIE &Die, dwarf::Attribute Attribute, dwarf::Form Form,
397 DIEBlock *Block) {
398 Block->computeSize(FormParams: Asm->getDwarfFormParams());
399 DIEBlocks.push_back(x: Block); // Memoize so we can call the destructor later on.
400 addAttribute(Die, Attribute, Form, Value&: Block);
401}
402
403void DwarfUnit::addBlock(DIE &Die, dwarf::Attribute Attribute,
404 DIEBlock *Block) {
405 addBlock(Die, Attribute, Form: Block->BestForm(), Block);
406}
407
408void DwarfUnit::addSourceLine(DIE &Die, unsigned Line, const DIFile *File) {
409 if (Line == 0)
410 return;
411
412 unsigned FileID = getOrCreateSourceID(File);
413 addUInt(Die, Attribute: dwarf::DW_AT_decl_file, Form: std::nullopt, Integer: FileID);
414 addUInt(Die, Attribute: dwarf::DW_AT_decl_line, Form: std::nullopt, Integer: Line);
415}
416
417void DwarfUnit::addSourceLine(DIE &Die, const DILocalVariable *V) {
418 assert(V);
419
420 addSourceLine(Die, Line: V->getLine(), File: V->getFile());
421}
422
423void DwarfUnit::addSourceLine(DIE &Die, const DIGlobalVariable *G) {
424 assert(G);
425
426 addSourceLine(Die, Line: G->getLine(), File: G->getFile());
427}
428
429void DwarfUnit::addSourceLine(DIE &Die, const DISubprogram *SP) {
430 assert(SP);
431
432 addSourceLine(Die, Line: SP->getLine(), File: SP->getFile());
433}
434
435void DwarfUnit::addSourceLine(DIE &Die, const DILabel *L) {
436 assert(L);
437
438 addSourceLine(Die, Line: L->getLine(), File: L->getFile());
439}
440
441void DwarfUnit::addSourceLine(DIE &Die, const DIType *Ty) {
442 assert(Ty);
443
444 addSourceLine(Die, Line: Ty->getLine(), File: Ty->getFile());
445}
446
447void DwarfUnit::addSourceLine(DIE &Die, const DIObjCProperty *Ty) {
448 assert(Ty);
449
450 addSourceLine(Die, Line: Ty->getLine(), File: Ty->getFile());
451}
452
453void DwarfUnit::addConstantFPValue(DIE &Die, const ConstantFP *CFP) {
454 // Pass this down to addConstantValue as an unsigned bag of bits.
455 addConstantValue(Die, Val: CFP->getValueAPF().bitcastToAPInt(), Unsigned: true);
456}
457
458void DwarfUnit::addConstantValue(DIE &Die, const ConstantInt *CI,
459 const DIType *Ty) {
460 addConstantValue(Die, Val: CI->getValue(), Ty);
461}
462
463void DwarfUnit::addConstantValue(DIE &Die, uint64_t Val, const DIType *Ty) {
464 addConstantValue(Die, Unsigned: DD->isUnsignedDIType(Ty), Val);
465}
466
467void DwarfUnit::addConstantValue(DIE &Die, bool Unsigned, uint64_t Val) {
468 // FIXME: This is a bit conservative/simple - it emits negative values always
469 // sign extended to 64 bits rather than minimizing the number of bytes.
470 addUInt(Die, Attribute: dwarf::DW_AT_const_value,
471 Form: Unsigned ? dwarf::DW_FORM_udata : dwarf::DW_FORM_sdata, Integer: Val);
472}
473
474void DwarfUnit::addConstantValue(DIE &Die, const APInt &Val, const DIType *Ty) {
475 addConstantValue(Die, Val, Unsigned: DD->isUnsignedDIType(Ty));
476}
477
478void DwarfUnit::addConstantValue(DIE &Die, const APInt &Val, bool Unsigned) {
479 unsigned CIBitWidth = Val.getBitWidth();
480 if (CIBitWidth <= 64) {
481 addConstantValue(Die, Unsigned,
482 Val: Unsigned ? Val.getZExtValue() : Val.getSExtValue());
483 return;
484 }
485
486 DIEBlock *Block = new (DIEValueAllocator) DIEBlock;
487
488 // Get the raw data form of the large APInt.
489 const uint64_t *Ptr64 = Val.getRawData();
490
491 int NumBytes = Val.getBitWidth() / 8; // 8 bits per byte.
492 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
493
494 // Output the constant to DWARF one byte at a time.
495 for (int i = 0; i < NumBytes; i++) {
496 uint8_t c;
497 if (LittleEndian)
498 c = Ptr64[i / 8] >> (8 * (i & 7));
499 else
500 c = Ptr64[(NumBytes - 1 - i) / 8] >> (8 * ((NumBytes - 1 - i) & 7));
501 addUInt(Block&: *Block, Form: dwarf::DW_FORM_data1, Integer: c);
502 }
503
504 addBlock(Die, Attribute: dwarf::DW_AT_const_value, Block);
505}
506
507void DwarfUnit::addLinkageName(DIE &Die, StringRef LinkageName) {
508 if (!LinkageName.empty())
509 addString(Die,
510 Attribute: DD->getDwarfVersion() >= 4 ? dwarf::DW_AT_linkage_name
511 : dwarf::DW_AT_MIPS_linkage_name,
512 String: GlobalValue::dropLLVMManglingEscape(Name: LinkageName));
513}
514
515void DwarfUnit::addTemplateParams(DIE &Buffer, DINodeArray TParams) {
516 // Add template parameters.
517 for (const auto *Element : TParams) {
518 if (auto *TTP = dyn_cast<DITemplateTypeParameter>(Val: Element))
519 constructTemplateTypeParameterDIE(Buffer, TP: TTP);
520 else if (auto *TVP = dyn_cast<DITemplateValueParameter>(Val: Element))
521 constructTemplateValueParameterDIE(Buffer, TVP);
522 }
523}
524
525/// Add thrown types.
526void DwarfUnit::addThrownTypes(DIE &Die, DINodeArray ThrownTypes) {
527 for (const auto *Ty : ThrownTypes) {
528 DIE &TT = createAndAddDIE(Tag: dwarf::DW_TAG_thrown_type, Parent&: Die);
529 addType(Entity&: TT, Ty: cast<DIType>(Val: Ty));
530 }
531}
532
533void DwarfUnit::addAccess(DIE &Die, DINode::DIFlags Flags) {
534 if ((Flags & DINode::FlagAccessibility) == DINode::FlagProtected)
535 addUInt(Die, Attribute: dwarf::DW_AT_accessibility, Form: dwarf::DW_FORM_data1,
536 Integer: dwarf::DW_ACCESS_protected);
537 else if ((Flags & DINode::FlagAccessibility) == DINode::FlagPrivate)
538 addUInt(Die, Attribute: dwarf::DW_AT_accessibility, Form: dwarf::DW_FORM_data1,
539 Integer: dwarf::DW_ACCESS_private);
540 else if ((Flags & DINode::FlagAccessibility) == DINode::FlagPublic)
541 addUInt(Die, Attribute: dwarf::DW_AT_accessibility, Form: dwarf::DW_FORM_data1,
542 Integer: dwarf::DW_ACCESS_public);
543}
544
545DIE *DwarfUnit::getOrCreateContextDIE(const DIScope *Context) {
546 if (!Context || isa<DIFile>(Val: Context) || isa<DICompileUnit>(Val: Context))
547 return &getUnitDie();
548 if (auto *T = dyn_cast<DIType>(Val: Context))
549 return getOrCreateTypeDIE(TyNode: T);
550 if (auto *NS = dyn_cast<DINamespace>(Val: Context))
551 return getOrCreateNameSpace(NS);
552 if (auto *SP = dyn_cast<DISubprogram>(Val: Context))
553 return getOrCreateSubprogramDIE(SP);
554 if (auto *M = dyn_cast<DIModule>(Val: Context))
555 return getOrCreateModule(M);
556 return getDIE(D: Context);
557}
558
559DIE *DwarfUnit::createTypeDIE(const DICompositeType *Ty) {
560 auto *Context = Ty->getScope();
561 DIE *ContextDIE = getOrCreateContextDIE(Context);
562
563 if (DIE *TyDIE = getDIE(D: Ty))
564 return TyDIE;
565
566 // Create new type.
567 DIE &TyDIE = createAndAddDIE(Tag: Ty->getTag(), Parent&: *ContextDIE, N: Ty);
568
569 constructTypeDIE(Buffer&: TyDIE, CTy: cast<DICompositeType>(Val: Ty));
570
571 updateAcceleratorTables(Context, Ty, TyDIE);
572 return &TyDIE;
573}
574
575DIE *DwarfUnit::createTypeDIE(const DIScope *Context, DIE &ContextDIE,
576 const DIType *Ty) {
577 // Create new type.
578 DIE &TyDIE = createAndAddDIE(Tag: Ty->getTag(), Parent&: ContextDIE, N: Ty);
579
580 updateAcceleratorTables(Context, Ty, TyDIE);
581
582 if (auto *BT = dyn_cast<DIBasicType>(Val: Ty))
583 constructTypeDIE(Buffer&: TyDIE, BTy: BT);
584 else if (auto *ST = dyn_cast<DIStringType>(Val: Ty))
585 constructTypeDIE(Buffer&: TyDIE, BTy: ST);
586 else if (auto *STy = dyn_cast<DISubroutineType>(Val: Ty))
587 constructTypeDIE(Buffer&: TyDIE, CTy: STy);
588 else if (auto *CTy = dyn_cast<DICompositeType>(Val: Ty)) {
589 if (DD->generateTypeUnits() && !Ty->isForwardDecl() &&
590 (Ty->getRawName() || CTy->getRawIdentifier())) {
591 // Skip updating the accelerator tables since this is not the full type.
592 if (MDString *TypeId = CTy->getRawIdentifier())
593 DD->addDwarfTypeUnitType(CU&: getCU(), Identifier: TypeId->getString(), Die&: TyDIE, CTy);
594 else
595 finishNonUnitTypeDIE(D&: TyDIE, CTy);
596 return &TyDIE;
597 }
598 constructTypeDIE(Buffer&: TyDIE, CTy);
599 } else {
600 constructTypeDIE(Buffer&: TyDIE, DTy: cast<DIDerivedType>(Val: Ty));
601 }
602
603 return &TyDIE;
604}
605
606DIE *DwarfUnit::getOrCreateTypeDIE(const MDNode *TyNode) {
607 if (!TyNode)
608 return nullptr;
609
610 auto *Ty = cast<DIType>(Val: TyNode);
611
612 // DW_TAG_restrict_type is not supported in DWARF2
613 if (Ty->getTag() == dwarf::DW_TAG_restrict_type && DD->getDwarfVersion() <= 2)
614 return getOrCreateTypeDIE(TyNode: cast<DIDerivedType>(Val: Ty)->getBaseType());
615
616 // DW_TAG_atomic_type is not supported in DWARF < 5
617 if (Ty->getTag() == dwarf::DW_TAG_atomic_type && DD->getDwarfVersion() < 5)
618 return getOrCreateTypeDIE(TyNode: cast<DIDerivedType>(Val: Ty)->getBaseType());
619
620 // Construct the context before querying for the existence of the DIE in case
621 // such construction creates the DIE.
622 auto *Context = Ty->getScope();
623 DIE *ContextDIE = getOrCreateContextDIE(Context);
624 assert(ContextDIE);
625
626 if (DIE *TyDIE = getDIE(D: Ty))
627 return TyDIE;
628
629 return static_cast<DwarfUnit *>(ContextDIE->getUnit())
630 ->createTypeDIE(Context, ContextDIE&: *ContextDIE, Ty);
631}
632
633void DwarfUnit::updateAcceleratorTables(const DIScope *Context,
634 const DIType *Ty, const DIE &TyDIE) {
635 if (!Ty->getName().empty() && !Ty->isForwardDecl()) {
636 bool IsImplementation = false;
637 if (auto *CT = dyn_cast<DICompositeType>(Val: Ty)) {
638 // A runtime language of 0 actually means C/C++ and that any
639 // non-negative value is some version of Objective-C/C++.
640 IsImplementation = CT->getRuntimeLang() == 0 || CT->isObjcClassComplete();
641 }
642 unsigned Flags = IsImplementation ? dwarf::DW_FLAG_type_implementation : 0;
643 DD->addAccelType(Unit: *this, NameTableKind: CUNode->getNameTableKind(), Name: Ty->getName(), Die: TyDIE,
644 Flags);
645
646 if (!Context || isa<DICompileUnit>(Val: Context) || isa<DIFile>(Val: Context) ||
647 isa<DINamespace>(Val: Context) || isa<DICommonBlock>(Val: Context))
648 addGlobalType(Ty, Die: TyDIE, Context);
649 }
650}
651
652void DwarfUnit::addType(DIE &Entity, const DIType *Ty,
653 dwarf::Attribute Attribute) {
654 assert(Ty && "Trying to add a type that doesn't exist?");
655 addDIEEntry(Die&: Entity, Attribute, Entry: DIEEntry(*getOrCreateTypeDIE(TyNode: Ty)));
656}
657
658std::string DwarfUnit::getParentContextString(const DIScope *Context) const {
659 if (!Context)
660 return "";
661
662 // FIXME: Decide whether to implement this for non-C++ languages.
663 if (!dwarf::isCPlusPlus(S: (dwarf::SourceLanguage)getLanguage()))
664 return "";
665
666 std::string CS;
667 SmallVector<const DIScope *, 1> Parents;
668 while (!isa<DICompileUnit>(Val: Context)) {
669 Parents.push_back(Elt: Context);
670 if (const DIScope *S = Context->getScope())
671 Context = S;
672 else
673 // Structure, etc types will have a NULL context if they're at the top
674 // level.
675 break;
676 }
677
678 // Reverse iterate over our list to go from the outermost construct to the
679 // innermost.
680 for (const DIScope *Ctx : llvm::reverse(C&: Parents)) {
681 StringRef Name = Ctx->getName();
682 if (Name.empty() && isa<DINamespace>(Val: Ctx))
683 Name = "(anonymous namespace)";
684 if (!Name.empty()) {
685 CS += Name;
686 CS += "::";
687 }
688 }
689 return CS;
690}
691
692void DwarfUnit::constructTypeDIE(DIE &Buffer, const DIBasicType *BTy) {
693 // Get core information.
694 StringRef Name = BTy->getName();
695 // Add name if not anonymous or intermediate type.
696 if (!Name.empty())
697 addString(Die&: Buffer, Attribute: dwarf::DW_AT_name, String: Name);
698
699 // An unspecified type only has a name attribute.
700 if (BTy->getTag() == dwarf::DW_TAG_unspecified_type)
701 return;
702
703 if (BTy->getTag() != dwarf::DW_TAG_string_type)
704 addUInt(Die&: Buffer, Attribute: dwarf::DW_AT_encoding, Form: dwarf::DW_FORM_data1,
705 Integer: BTy->getEncoding());
706
707 uint64_t Size = BTy->getSizeInBits() >> 3;
708 addUInt(Die&: Buffer, Attribute: dwarf::DW_AT_byte_size, Form: std::nullopt, Integer: Size);
709
710 if (BTy->isBigEndian())
711 addUInt(Die&: Buffer, Attribute: dwarf::DW_AT_endianity, Form: std::nullopt, Integer: dwarf::DW_END_big);
712 else if (BTy->isLittleEndian())
713 addUInt(Die&: Buffer, Attribute: dwarf::DW_AT_endianity, Form: std::nullopt, Integer: dwarf::DW_END_little);
714}
715
716void DwarfUnit::constructTypeDIE(DIE &Buffer, const DIStringType *STy) {
717 // Get core information.
718 StringRef Name = STy->getName();
719 // Add name if not anonymous or intermediate type.
720 if (!Name.empty())
721 addString(Die&: Buffer, Attribute: dwarf::DW_AT_name, String: Name);
722
723 if (DIVariable *Var = STy->getStringLength()) {
724 if (auto *VarDIE = getDIE(D: Var))
725 addDIEEntry(Die&: Buffer, Attribute: dwarf::DW_AT_string_length, Entry&: *VarDIE);
726 } else if (DIExpression *Expr = STy->getStringLengthExp()) {
727 DIELoc *Loc = new (DIEValueAllocator) DIELoc;
728 DIEDwarfExpression DwarfExpr(*Asm, getCU(), *Loc);
729 // This is to describe the memory location of the
730 // length of a Fortran deferred length string, so
731 // lock it down as such.
732 DwarfExpr.setMemoryLocationKind();
733 DwarfExpr.addExpression(Expr);
734 addBlock(Die&: Buffer, Attribute: dwarf::DW_AT_string_length, Loc: DwarfExpr.finalize());
735 } else {
736 uint64_t Size = STy->getSizeInBits() >> 3;
737 addUInt(Die&: Buffer, Attribute: dwarf::DW_AT_byte_size, Form: std::nullopt, Integer: Size);
738 }
739
740 if (DIExpression *Expr = STy->getStringLocationExp()) {
741 DIELoc *Loc = new (DIEValueAllocator) DIELoc;
742 DIEDwarfExpression DwarfExpr(*Asm, getCU(), *Loc);
743 // This is to describe the memory location of the
744 // string, so lock it down as such.
745 DwarfExpr.setMemoryLocationKind();
746 DwarfExpr.addExpression(Expr);
747 addBlock(Die&: Buffer, Attribute: dwarf::DW_AT_data_location, Loc: DwarfExpr.finalize());
748 }
749
750 if (STy->getEncoding()) {
751 // For eventual Unicode support.
752 addUInt(Die&: Buffer, Attribute: dwarf::DW_AT_encoding, Form: dwarf::DW_FORM_data1,
753 Integer: STy->getEncoding());
754 }
755}
756
757void DwarfUnit::constructTypeDIE(DIE &Buffer, const DIDerivedType *DTy) {
758 // Get core information.
759 StringRef Name = DTy->getName();
760 uint64_t Size = DTy->getSizeInBits() >> 3;
761 uint16_t Tag = Buffer.getTag();
762
763 // Map to main type, void will not have a type.
764 const DIType *FromTy = DTy->getBaseType();
765 if (FromTy)
766 addType(Entity&: Buffer, Ty: FromTy);
767
768 // Add name if not anonymous or intermediate type.
769 if (!Name.empty())
770 addString(Die&: Buffer, Attribute: dwarf::DW_AT_name, String: Name);
771
772 addAnnotation(Buffer, Annotations: DTy->getAnnotations());
773
774 // If alignment is specified for a typedef , create and insert DW_AT_alignment
775 // attribute in DW_TAG_typedef DIE.
776 if (Tag == dwarf::DW_TAG_typedef && DD->getDwarfVersion() >= 5) {
777 uint32_t AlignInBytes = DTy->getAlignInBytes();
778 if (AlignInBytes > 0)
779 addUInt(Die&: Buffer, Attribute: dwarf::DW_AT_alignment, Form: dwarf::DW_FORM_udata,
780 Integer: AlignInBytes);
781 }
782
783 // Add size if non-zero (derived types might be zero-sized.)
784 if (Size && Tag != dwarf::DW_TAG_pointer_type
785 && Tag != dwarf::DW_TAG_ptr_to_member_type
786 && Tag != dwarf::DW_TAG_reference_type
787 && Tag != dwarf::DW_TAG_rvalue_reference_type)
788 addUInt(Die&: Buffer, Attribute: dwarf::DW_AT_byte_size, Form: std::nullopt, Integer: Size);
789
790 if (Tag == dwarf::DW_TAG_ptr_to_member_type)
791 addDIEEntry(Die&: Buffer, Attribute: dwarf::DW_AT_containing_type,
792 Entry&: *getOrCreateTypeDIE(TyNode: cast<DIDerivedType>(Val: DTy)->getClassType()));
793
794 addAccess(Die&: Buffer, Flags: DTy->getFlags());
795
796 // Add source line info if available and TyDesc is not a forward declaration.
797 if (!DTy->isForwardDecl())
798 addSourceLine(Die&: Buffer, Ty: DTy);
799
800 // If DWARF address space value is other than None, add it. The IR
801 // verifier checks that DWARF address space only exists for pointer
802 // or reference types.
803 if (DTy->getDWARFAddressSpace())
804 addUInt(Die&: Buffer, Attribute: dwarf::DW_AT_address_class, Form: dwarf::DW_FORM_data4,
805 Integer: *DTy->getDWARFAddressSpace());
806
807 // Add template alias template parameters.
808 if (Tag == dwarf::DW_TAG_template_alias)
809 addTemplateParams(Buffer, TParams: DTy->getTemplateParams());
810
811 if (auto PtrAuthData = DTy->getPtrAuthData()) {
812 addUInt(Die&: Buffer, Attribute: dwarf::DW_AT_LLVM_ptrauth_key, Form: dwarf::DW_FORM_data1,
813 Integer: PtrAuthData->key());
814 if (PtrAuthData->isAddressDiscriminated())
815 addFlag(Die&: Buffer, Attribute: dwarf::DW_AT_LLVM_ptrauth_address_discriminated);
816 addUInt(Die&: Buffer, Attribute: dwarf::DW_AT_LLVM_ptrauth_extra_discriminator,
817 Form: dwarf::DW_FORM_data2, Integer: PtrAuthData->extraDiscriminator());
818 if (PtrAuthData->isaPointer())
819 addFlag(Die&: Buffer, Attribute: dwarf::DW_AT_LLVM_ptrauth_isa_pointer);
820 if (PtrAuthData->authenticatesNullValues())
821 addFlag(Die&: Buffer, Attribute: dwarf::DW_AT_LLVM_ptrauth_authenticates_null_values);
822 }
823}
824
825void DwarfUnit::constructSubprogramArguments(DIE &Buffer, DITypeRefArray Args) {
826 for (unsigned i = 1, N = Args.size(); i < N; ++i) {
827 const DIType *Ty = Args[i];
828 if (!Ty) {
829 assert(i == N-1 && "Unspecified parameter must be the last argument");
830 createAndAddDIE(Tag: dwarf::DW_TAG_unspecified_parameters, Parent&: Buffer);
831 } else {
832 DIE &Arg = createAndAddDIE(Tag: dwarf::DW_TAG_formal_parameter, Parent&: Buffer);
833 addType(Entity&: Arg, Ty);
834 if (Ty->isArtificial())
835 addFlag(Die&: Arg, Attribute: dwarf::DW_AT_artificial);
836 }
837 }
838}
839
840void DwarfUnit::constructTypeDIE(DIE &Buffer, const DISubroutineType *CTy) {
841 // Add return type. A void return won't have a type.
842 auto Elements = cast<DISubroutineType>(Val: CTy)->getTypeArray();
843 if (Elements.size())
844 if (auto RTy = Elements[0])
845 addType(Entity&: Buffer, Ty: RTy);
846
847 bool isPrototyped = true;
848 if (Elements.size() == 2 && !Elements[1])
849 isPrototyped = false;
850
851 constructSubprogramArguments(Buffer, Args: Elements);
852
853 // Add prototype flag if we're dealing with a C language and the function has
854 // been prototyped.
855 if (isPrototyped && dwarf::isC(S: (dwarf::SourceLanguage)getLanguage()))
856 addFlag(Die&: Buffer, Attribute: dwarf::DW_AT_prototyped);
857
858 // Add a DW_AT_calling_convention if this has an explicit convention.
859 if (CTy->getCC() && CTy->getCC() != dwarf::DW_CC_normal)
860 addUInt(Die&: Buffer, Attribute: dwarf::DW_AT_calling_convention, Form: dwarf::DW_FORM_data1,
861 Integer: CTy->getCC());
862
863 if (CTy->isLValueReference())
864 addFlag(Die&: Buffer, Attribute: dwarf::DW_AT_reference);
865
866 if (CTy->isRValueReference())
867 addFlag(Die&: Buffer, Attribute: dwarf::DW_AT_rvalue_reference);
868}
869
870void DwarfUnit::addAnnotation(DIE &Buffer, DINodeArray Annotations) {
871 if (!Annotations)
872 return;
873
874 for (const Metadata *Annotation : Annotations->operands()) {
875 const MDNode *MD = cast<MDNode>(Val: Annotation);
876 const MDString *Name = cast<MDString>(Val: MD->getOperand(I: 0));
877 const auto &Value = MD->getOperand(I: 1);
878
879 DIE &AnnotationDie = createAndAddDIE(Tag: dwarf::DW_TAG_LLVM_annotation, Parent&: Buffer);
880 addString(Die&: AnnotationDie, Attribute: dwarf::DW_AT_name, String: Name->getString());
881 if (const auto *Data = dyn_cast<MDString>(Val: Value))
882 addString(Die&: AnnotationDie, Attribute: dwarf::DW_AT_const_value, String: Data->getString());
883 else if (const auto *Data = dyn_cast<ConstantAsMetadata>(Val: Value))
884 addConstantValue(Die&: AnnotationDie, Val: Data->getValue()->getUniqueInteger(),
885 /*Unsigned=*/true);
886 else
887 assert(false && "Unsupported annotation value type");
888 }
889}
890
891void DwarfUnit::constructTypeDIE(DIE &Buffer, const DICompositeType *CTy) {
892 // Add name if not anonymous or intermediate type.
893 StringRef Name = CTy->getName();
894
895 uint64_t Size = CTy->getSizeInBits() >> 3;
896 uint16_t Tag = Buffer.getTag();
897
898 switch (Tag) {
899 case dwarf::DW_TAG_array_type:
900 constructArrayTypeDIE(Buffer, CTy);
901 break;
902 case dwarf::DW_TAG_enumeration_type:
903 constructEnumTypeDIE(Buffer, CTy);
904 break;
905 case dwarf::DW_TAG_variant_part:
906 case dwarf::DW_TAG_structure_type:
907 case dwarf::DW_TAG_union_type:
908 case dwarf::DW_TAG_class_type:
909 case dwarf::DW_TAG_namelist: {
910 // Emit the discriminator for a variant part.
911 DIDerivedType *Discriminator = nullptr;
912 if (Tag == dwarf::DW_TAG_variant_part) {
913 Discriminator = CTy->getDiscriminator();
914 if (Discriminator) {
915 // DWARF says:
916 // If the variant part has a discriminant, the discriminant is
917 // represented by a separate debugging information entry which is
918 // a child of the variant part entry.
919 DIE &DiscMember = constructMemberDIE(Buffer, DT: Discriminator);
920 addDIEEntry(Die&: Buffer, Attribute: dwarf::DW_AT_discr, Entry&: DiscMember);
921 }
922 }
923
924 // Add template parameters to a class, structure or union types.
925 if (Tag == dwarf::DW_TAG_class_type ||
926 Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type)
927 addTemplateParams(Buffer, TParams: CTy->getTemplateParams());
928
929 // Add elements to structure type.
930 DINodeArray Elements = CTy->getElements();
931 for (const auto *Element : Elements) {
932 if (!Element)
933 continue;
934 if (auto *SP = dyn_cast<DISubprogram>(Val: Element))
935 getOrCreateSubprogramDIE(SP);
936 else if (auto *DDTy = dyn_cast<DIDerivedType>(Val: Element)) {
937 if (DDTy->getTag() == dwarf::DW_TAG_friend) {
938 DIE &ElemDie = createAndAddDIE(Tag: dwarf::DW_TAG_friend, Parent&: Buffer);
939 addType(Entity&: ElemDie, Ty: DDTy->getBaseType(), Attribute: dwarf::DW_AT_friend);
940 } else if (DDTy->isStaticMember()) {
941 getOrCreateStaticMemberDIE(DT: DDTy);
942 } else if (Tag == dwarf::DW_TAG_variant_part) {
943 // When emitting a variant part, wrap each member in
944 // DW_TAG_variant.
945 DIE &Variant = createAndAddDIE(Tag: dwarf::DW_TAG_variant, Parent&: Buffer);
946 if (const ConstantInt *CI =
947 dyn_cast_or_null<ConstantInt>(Val: DDTy->getDiscriminantValue())) {
948 if (DD->isUnsignedDIType(Ty: Discriminator->getBaseType()))
949 addUInt(Die&: Variant, Attribute: dwarf::DW_AT_discr_value, Form: std::nullopt,
950 Integer: CI->getZExtValue());
951 else
952 addSInt(Die&: Variant, Attribute: dwarf::DW_AT_discr_value, Form: std::nullopt,
953 Integer: CI->getSExtValue());
954 }
955 constructMemberDIE(Buffer&: Variant, DT: DDTy);
956 } else {
957 constructMemberDIE(Buffer, DT: DDTy);
958 }
959 } else if (auto *Property = dyn_cast<DIObjCProperty>(Val: Element)) {
960 DIE &ElemDie = createAndAddDIE(Tag: Property->getTag(), Parent&: Buffer);
961 StringRef PropertyName = Property->getName();
962 addString(Die&: ElemDie, Attribute: dwarf::DW_AT_APPLE_property_name, String: PropertyName);
963 if (Property->getType())
964 addType(Entity&: ElemDie, Ty: Property->getType());
965 addSourceLine(Die&: ElemDie, Ty: Property);
966 StringRef GetterName = Property->getGetterName();
967 if (!GetterName.empty())
968 addString(Die&: ElemDie, Attribute: dwarf::DW_AT_APPLE_property_getter, String: GetterName);
969 StringRef SetterName = Property->getSetterName();
970 if (!SetterName.empty())
971 addString(Die&: ElemDie, Attribute: dwarf::DW_AT_APPLE_property_setter, String: SetterName);
972 if (unsigned PropertyAttributes = Property->getAttributes())
973 addUInt(Die&: ElemDie, Attribute: dwarf::DW_AT_APPLE_property_attribute, Form: std::nullopt,
974 Integer: PropertyAttributes);
975 } else if (auto *Composite = dyn_cast<DICompositeType>(Val: Element)) {
976 if (Composite->getTag() == dwarf::DW_TAG_variant_part) {
977 DIE &VariantPart = createAndAddDIE(Tag: Composite->getTag(), Parent&: Buffer);
978 constructTypeDIE(Buffer&: VariantPart, CTy: Composite);
979 }
980 } else if (Tag == dwarf::DW_TAG_namelist) {
981 auto *Var = dyn_cast<DINode>(Val: Element);
982 auto *VarDIE = getDIE(D: Var);
983 if (VarDIE) {
984 DIE &ItemDie = createAndAddDIE(Tag: dwarf::DW_TAG_namelist_item, Parent&: Buffer);
985 addDIEEntry(Die&: ItemDie, Attribute: dwarf::DW_AT_namelist_item, Entry&: *VarDIE);
986 }
987 }
988 }
989
990 if (CTy->isAppleBlockExtension())
991 addFlag(Die&: Buffer, Attribute: dwarf::DW_AT_APPLE_block);
992
993 if (CTy->getExportSymbols())
994 addFlag(Die&: Buffer, Attribute: dwarf::DW_AT_export_symbols);
995
996 // This is outside the DWARF spec, but GDB expects a DW_AT_containing_type
997 // inside C++ composite types to point to the base class with the vtable.
998 // Rust uses DW_AT_containing_type to link a vtable to the type
999 // for which it was created.
1000 if (auto *ContainingType = CTy->getVTableHolder())
1001 addDIEEntry(Die&: Buffer, Attribute: dwarf::DW_AT_containing_type,
1002 Entry&: *getOrCreateTypeDIE(TyNode: ContainingType));
1003
1004 if (CTy->isObjcClassComplete())
1005 addFlag(Die&: Buffer, Attribute: dwarf::DW_AT_APPLE_objc_complete_type);
1006
1007 // Add the type's non-standard calling convention.
1008 // DW_CC_pass_by_value/DW_CC_pass_by_reference are introduced in DWARF 5.
1009 if (!Asm->TM.Options.DebugStrictDwarf || DD->getDwarfVersion() >= 5) {
1010 uint8_t CC = 0;
1011 if (CTy->isTypePassByValue())
1012 CC = dwarf::DW_CC_pass_by_value;
1013 else if (CTy->isTypePassByReference())
1014 CC = dwarf::DW_CC_pass_by_reference;
1015 if (CC)
1016 addUInt(Die&: Buffer, Attribute: dwarf::DW_AT_calling_convention, Form: dwarf::DW_FORM_data1,
1017 Integer: CC);
1018 }
1019 break;
1020 }
1021 default:
1022 break;
1023 }
1024
1025 // Add name if not anonymous or intermediate type.
1026 if (!Name.empty())
1027 addString(Die&: Buffer, Attribute: dwarf::DW_AT_name, String: Name);
1028
1029 addAnnotation(Buffer, Annotations: CTy->getAnnotations());
1030
1031 if (Tag == dwarf::DW_TAG_enumeration_type ||
1032 Tag == dwarf::DW_TAG_class_type || Tag == dwarf::DW_TAG_structure_type ||
1033 Tag == dwarf::DW_TAG_union_type) {
1034 // Add size if non-zero (derived types might be zero-sized.)
1035 // Ignore the size if it's a non-enum forward decl.
1036 // TODO: Do we care about size for enum forward declarations?
1037 if (Size &&
1038 (!CTy->isForwardDecl() || Tag == dwarf::DW_TAG_enumeration_type))
1039 addUInt(Die&: Buffer, Attribute: dwarf::DW_AT_byte_size, Form: std::nullopt, Integer: Size);
1040 else if (!CTy->isForwardDecl())
1041 // Add zero size if it is not a forward declaration.
1042 addUInt(Die&: Buffer, Attribute: dwarf::DW_AT_byte_size, Form: std::nullopt, Integer: 0);
1043
1044 // If we're a forward decl, say so.
1045 if (CTy->isForwardDecl())
1046 addFlag(Die&: Buffer, Attribute: dwarf::DW_AT_declaration);
1047
1048 // Add accessibility info if available.
1049 addAccess(Die&: Buffer, Flags: CTy->getFlags());
1050
1051 // Add source line info if available.
1052 if (!CTy->isForwardDecl())
1053 addSourceLine(Die&: Buffer, Ty: CTy);
1054
1055 // No harm in adding the runtime language to the declaration.
1056 unsigned RLang = CTy->getRuntimeLang();
1057 if (RLang)
1058 addUInt(Die&: Buffer, Attribute: dwarf::DW_AT_APPLE_runtime_class, Form: dwarf::DW_FORM_data1,
1059 Integer: RLang);
1060
1061 // Add align info if available.
1062 if (uint32_t AlignInBytes = CTy->getAlignInBytes())
1063 addUInt(Die&: Buffer, Attribute: dwarf::DW_AT_alignment, Form: dwarf::DW_FORM_udata,
1064 Integer: AlignInBytes);
1065 }
1066}
1067
1068void DwarfUnit::constructTemplateTypeParameterDIE(
1069 DIE &Buffer, const DITemplateTypeParameter *TP) {
1070 DIE &ParamDIE =
1071 createAndAddDIE(Tag: dwarf::DW_TAG_template_type_parameter, Parent&: Buffer);
1072 // Add the type if it exists, it could be void and therefore no type.
1073 if (TP->getType())
1074 addType(Entity&: ParamDIE, Ty: TP->getType());
1075 if (!TP->getName().empty())
1076 addString(Die&: ParamDIE, Attribute: dwarf::DW_AT_name, String: TP->getName());
1077 if (TP->isDefault() && isCompatibleWithVersion(Version: 5))
1078 addFlag(Die&: ParamDIE, Attribute: dwarf::DW_AT_default_value);
1079}
1080
1081void DwarfUnit::constructTemplateValueParameterDIE(
1082 DIE &Buffer, const DITemplateValueParameter *VP) {
1083 DIE &ParamDIE = createAndAddDIE(Tag: VP->getTag(), Parent&: Buffer);
1084
1085 // Add the type if there is one, template template and template parameter
1086 // packs will not have a type.
1087 if (VP->getTag() == dwarf::DW_TAG_template_value_parameter)
1088 addType(Entity&: ParamDIE, Ty: VP->getType());
1089 if (!VP->getName().empty())
1090 addString(Die&: ParamDIE, Attribute: dwarf::DW_AT_name, String: VP->getName());
1091 if (VP->isDefault() && isCompatibleWithVersion(Version: 5))
1092 addFlag(Die&: ParamDIE, Attribute: dwarf::DW_AT_default_value);
1093 if (Metadata *Val = VP->getValue()) {
1094 if (ConstantInt *CI = mdconst::dyn_extract<ConstantInt>(MD&: Val))
1095 addConstantValue(Die&: ParamDIE, CI, Ty: VP->getType());
1096 else if (GlobalValue *GV = mdconst::dyn_extract<GlobalValue>(MD&: Val)) {
1097 // We cannot describe the location of dllimport'd entities: the
1098 // computation of their address requires loads from the IAT.
1099 if (!GV->hasDLLImportStorageClass()) {
1100 // For declaration non-type template parameters (such as global values
1101 // and functions)
1102 DIELoc *Loc = new (DIEValueAllocator) DIELoc;
1103 addOpAddress(Die&: *Loc, Sym: Asm->getSymbol(GV));
1104 // Emit DW_OP_stack_value to use the address as the immediate value of
1105 // the parameter, rather than a pointer to it.
1106 addUInt(Block&: *Loc, Form: dwarf::DW_FORM_data1, Integer: dwarf::DW_OP_stack_value);
1107 addBlock(Die&: ParamDIE, Attribute: dwarf::DW_AT_location, Loc);
1108 }
1109 } else if (VP->getTag() == dwarf::DW_TAG_GNU_template_template_param) {
1110 assert(isa<MDString>(Val));
1111 addString(Die&: ParamDIE, Attribute: dwarf::DW_AT_GNU_template_name,
1112 String: cast<MDString>(Val)->getString());
1113 } else if (VP->getTag() == dwarf::DW_TAG_GNU_template_parameter_pack) {
1114 addTemplateParams(Buffer&: ParamDIE, TParams: cast<MDTuple>(Val));
1115 }
1116 }
1117}
1118
1119DIE *DwarfUnit::getOrCreateNameSpace(const DINamespace *NS) {
1120 // Construct the context before querying for the existence of the DIE in case
1121 // such construction creates the DIE.
1122 DIE *ContextDIE = getOrCreateContextDIE(Context: NS->getScope());
1123
1124 if (DIE *NDie = getDIE(D: NS))
1125 return NDie;
1126 DIE &NDie = createAndAddDIE(Tag: dwarf::DW_TAG_namespace, Parent&: *ContextDIE, N: NS);
1127
1128 StringRef Name = NS->getName();
1129 if (!Name.empty())
1130 addString(Die&: NDie, Attribute: dwarf::DW_AT_name, String: NS->getName());
1131 else
1132 Name = "(anonymous namespace)";
1133 DD->addAccelNamespace(Unit: *this, NameTableKind: CUNode->getNameTableKind(), Name, Die: NDie);
1134 addGlobalName(Name, Die: NDie, Context: NS->getScope());
1135 if (NS->getExportSymbols())
1136 addFlag(Die&: NDie, Attribute: dwarf::DW_AT_export_symbols);
1137 return &NDie;
1138}
1139
1140DIE *DwarfUnit::getOrCreateModule(const DIModule *M) {
1141 // Construct the context before querying for the existence of the DIE in case
1142 // such construction creates the DIE.
1143 DIE *ContextDIE = getOrCreateContextDIE(Context: M->getScope());
1144
1145 if (DIE *MDie = getDIE(D: M))
1146 return MDie;
1147 DIE &MDie = createAndAddDIE(Tag: dwarf::DW_TAG_module, Parent&: *ContextDIE, N: M);
1148
1149 if (!M->getName().empty()) {
1150 addString(Die&: MDie, Attribute: dwarf::DW_AT_name, String: M->getName());
1151 addGlobalName(Name: M->getName(), Die: MDie, Context: M->getScope());
1152 }
1153 if (!M->getConfigurationMacros().empty())
1154 addString(Die&: MDie, Attribute: dwarf::DW_AT_LLVM_config_macros,
1155 String: M->getConfigurationMacros());
1156 if (!M->getIncludePath().empty())
1157 addString(Die&: MDie, Attribute: dwarf::DW_AT_LLVM_include_path, String: M->getIncludePath());
1158 if (!M->getAPINotesFile().empty())
1159 addString(Die&: MDie, Attribute: dwarf::DW_AT_LLVM_apinotes, String: M->getAPINotesFile());
1160 if (M->getFile())
1161 addUInt(Die&: MDie, Attribute: dwarf::DW_AT_decl_file, Form: std::nullopt,
1162 Integer: getOrCreateSourceID(File: M->getFile()));
1163 if (M->getLineNo())
1164 addUInt(Die&: MDie, Attribute: dwarf::DW_AT_decl_line, Form: std::nullopt, Integer: M->getLineNo());
1165 if (M->getIsDecl())
1166 addFlag(Die&: MDie, Attribute: dwarf::DW_AT_declaration);
1167
1168 return &MDie;
1169}
1170
1171DIE *DwarfUnit::getOrCreateSubprogramDIE(const DISubprogram *SP, bool Minimal) {
1172 // Construct the context before querying for the existence of the DIE in case
1173 // such construction creates the DIE (as is the case for member function
1174 // declarations).
1175 DIE *ContextDIE =
1176 Minimal ? &getUnitDie() : getOrCreateContextDIE(Context: SP->getScope());
1177
1178 if (DIE *SPDie = getDIE(D: SP))
1179 return SPDie;
1180
1181 if (auto *SPDecl = SP->getDeclaration()) {
1182 if (!Minimal) {
1183 // Add subprogram definitions to the CU die directly.
1184 ContextDIE = &getUnitDie();
1185 // Build the decl now to ensure it precedes the definition.
1186 getOrCreateSubprogramDIE(SP: SPDecl);
1187 }
1188 }
1189
1190 // DW_TAG_inlined_subroutine may refer to this DIE.
1191 DIE &SPDie = createAndAddDIE(Tag: dwarf::DW_TAG_subprogram, Parent&: *ContextDIE, N: SP);
1192
1193 // Stop here and fill this in later, depending on whether or not this
1194 // subprogram turns out to have inlined instances or not.
1195 if (SP->isDefinition())
1196 return &SPDie;
1197
1198 static_cast<DwarfUnit *>(SPDie.getUnit())
1199 ->applySubprogramAttributes(SP, SPDie);
1200 return &SPDie;
1201}
1202
1203bool DwarfUnit::applySubprogramDefinitionAttributes(const DISubprogram *SP,
1204 DIE &SPDie, bool Minimal) {
1205 DIE *DeclDie = nullptr;
1206 StringRef DeclLinkageName;
1207 if (auto *SPDecl = SP->getDeclaration()) {
1208 if (!Minimal) {
1209 DITypeRefArray DeclArgs, DefinitionArgs;
1210 DeclArgs = SPDecl->getType()->getTypeArray();
1211 DefinitionArgs = SP->getType()->getTypeArray();
1212
1213 if (DeclArgs.size() && DefinitionArgs.size())
1214 if (DefinitionArgs[0] != nullptr && DeclArgs[0] != DefinitionArgs[0])
1215 addType(Entity&: SPDie, Ty: DefinitionArgs[0]);
1216
1217 DeclDie = getDIE(D: SPDecl);
1218 assert(DeclDie && "This DIE should've already been constructed when the "
1219 "definition DIE was created in "
1220 "getOrCreateSubprogramDIE");
1221 // Look at the Decl's linkage name only if we emitted it.
1222 if (DD->useAllLinkageNames())
1223 DeclLinkageName = SPDecl->getLinkageName();
1224 unsigned DeclID = getOrCreateSourceID(File: SPDecl->getFile());
1225 unsigned DefID = getOrCreateSourceID(File: SP->getFile());
1226 if (DeclID != DefID)
1227 addUInt(Die&: SPDie, Attribute: dwarf::DW_AT_decl_file, Form: std::nullopt, Integer: DefID);
1228
1229 if (SP->getLine() != SPDecl->getLine())
1230 addUInt(Die&: SPDie, Attribute: dwarf::DW_AT_decl_line, Form: std::nullopt, Integer: SP->getLine());
1231 }
1232 }
1233
1234 // Add function template parameters.
1235 addTemplateParams(Buffer&: SPDie, TParams: SP->getTemplateParams());
1236
1237 // Add the linkage name if we have one and it isn't in the Decl.
1238 StringRef LinkageName = SP->getLinkageName();
1239 assert(((LinkageName.empty() || DeclLinkageName.empty()) ||
1240 LinkageName == DeclLinkageName) &&
1241 "decl has a linkage name and it is different");
1242 if (DeclLinkageName.empty() &&
1243 // Always emit it for abstract subprograms.
1244 (DD->useAllLinkageNames() || DU->getAbstractScopeDIEs().lookup(Val: SP)))
1245 addLinkageName(Die&: SPDie, LinkageName);
1246
1247 if (!DeclDie)
1248 return false;
1249
1250 // Refer to the function declaration where all the other attributes will be
1251 // found.
1252 addDIEEntry(Die&: SPDie, Attribute: dwarf::DW_AT_specification, Entry&: *DeclDie);
1253 return true;
1254}
1255
1256void DwarfUnit::applySubprogramAttributes(const DISubprogram *SP, DIE &SPDie,
1257 bool SkipSPAttributes) {
1258 // If -fdebug-info-for-profiling is enabled, need to emit the subprogram
1259 // and its source location.
1260 bool SkipSPSourceLocation = SkipSPAttributes &&
1261 !CUNode->getDebugInfoForProfiling();
1262 if (!SkipSPSourceLocation)
1263 if (applySubprogramDefinitionAttributes(SP, SPDie, Minimal: SkipSPAttributes))
1264 return;
1265
1266 // Constructors and operators for anonymous aggregates do not have names.
1267 if (!SP->getName().empty())
1268 addString(Die&: SPDie, Attribute: dwarf::DW_AT_name, String: SP->getName());
1269
1270 addAnnotation(Buffer&: SPDie, Annotations: SP->getAnnotations());
1271
1272 if (!SkipSPSourceLocation)
1273 addSourceLine(Die&: SPDie, SP);
1274
1275 // Skip the rest of the attributes under -gmlt to save space.
1276 if (SkipSPAttributes)
1277 return;
1278
1279 // Add the prototype if we have a prototype and we have a C like
1280 // language.
1281 if (SP->isPrototyped() && dwarf::isC(S: (dwarf::SourceLanguage)getLanguage()))
1282 addFlag(Die&: SPDie, Attribute: dwarf::DW_AT_prototyped);
1283
1284 if (SP->isObjCDirect())
1285 addFlag(Die&: SPDie, Attribute: dwarf::DW_AT_APPLE_objc_direct);
1286
1287 unsigned CC = 0;
1288 DITypeRefArray Args;
1289 if (const DISubroutineType *SPTy = SP->getType()) {
1290 Args = SPTy->getTypeArray();
1291 CC = SPTy->getCC();
1292 }
1293
1294 // Add a DW_AT_calling_convention if this has an explicit convention.
1295 if (CC && CC != dwarf::DW_CC_normal)
1296 addUInt(Die&: SPDie, Attribute: dwarf::DW_AT_calling_convention, Form: dwarf::DW_FORM_data1, Integer: CC);
1297
1298 // Add a return type. If this is a type like a C/C++ void type we don't add a
1299 // return type.
1300 if (Args.size())
1301 if (auto Ty = Args[0])
1302 addType(Entity&: SPDie, Ty);
1303
1304 unsigned VK = SP->getVirtuality();
1305 if (VK) {
1306 addUInt(Die&: SPDie, Attribute: dwarf::DW_AT_virtuality, Form: dwarf::DW_FORM_data1, Integer: VK);
1307 if (SP->getVirtualIndex() != -1u) {
1308 DIELoc *Block = getDIELoc();
1309 addUInt(Block&: *Block, Form: dwarf::DW_FORM_data1, Integer: dwarf::DW_OP_constu);
1310 addUInt(Block&: *Block, Form: dwarf::DW_FORM_udata, Integer: SP->getVirtualIndex());
1311 addBlock(Die&: SPDie, Attribute: dwarf::DW_AT_vtable_elem_location, Loc: Block);
1312 }
1313 ContainingTypeMap.insert(KV: std::make_pair(x: &SPDie, y: SP->getContainingType()));
1314 }
1315
1316 if (!SP->isDefinition()) {
1317 addFlag(Die&: SPDie, Attribute: dwarf::DW_AT_declaration);
1318
1319 // Add arguments. Do not add arguments for subprogram definition. They will
1320 // be handled while processing variables.
1321 constructSubprogramArguments(Buffer&: SPDie, Args);
1322 }
1323
1324 addThrownTypes(Die&: SPDie, ThrownTypes: SP->getThrownTypes());
1325
1326 if (SP->isArtificial())
1327 addFlag(Die&: SPDie, Attribute: dwarf::DW_AT_artificial);
1328
1329 if (!SP->isLocalToUnit())
1330 addFlag(Die&: SPDie, Attribute: dwarf::DW_AT_external);
1331
1332 if (DD->useAppleExtensionAttributes()) {
1333 if (SP->isOptimized())
1334 addFlag(Die&: SPDie, Attribute: dwarf::DW_AT_APPLE_optimized);
1335
1336 if (unsigned isa = Asm->getISAEncoding())
1337 addUInt(Die&: SPDie, Attribute: dwarf::DW_AT_APPLE_isa, Form: dwarf::DW_FORM_flag, Integer: isa);
1338 }
1339
1340 if (SP->isLValueReference())
1341 addFlag(Die&: SPDie, Attribute: dwarf::DW_AT_reference);
1342
1343 if (SP->isRValueReference())
1344 addFlag(Die&: SPDie, Attribute: dwarf::DW_AT_rvalue_reference);
1345
1346 if (SP->isNoReturn())
1347 addFlag(Die&: SPDie, Attribute: dwarf::DW_AT_noreturn);
1348
1349 addAccess(Die&: SPDie, Flags: SP->getFlags());
1350
1351 if (SP->isExplicit())
1352 addFlag(Die&: SPDie, Attribute: dwarf::DW_AT_explicit);
1353
1354 if (SP->isMainSubprogram())
1355 addFlag(Die&: SPDie, Attribute: dwarf::DW_AT_main_subprogram);
1356 if (SP->isPure())
1357 addFlag(Die&: SPDie, Attribute: dwarf::DW_AT_pure);
1358 if (SP->isElemental())
1359 addFlag(Die&: SPDie, Attribute: dwarf::DW_AT_elemental);
1360 if (SP->isRecursive())
1361 addFlag(Die&: SPDie, Attribute: dwarf::DW_AT_recursive);
1362
1363 if (!SP->getTargetFuncName().empty())
1364 addString(Die&: SPDie, Attribute: dwarf::DW_AT_trampoline, String: SP->getTargetFuncName());
1365
1366 if (DD->getDwarfVersion() >= 5 && SP->isDeleted())
1367 addFlag(Die&: SPDie, Attribute: dwarf::DW_AT_deleted);
1368}
1369
1370void DwarfUnit::constructSubrangeDIE(DIE &Buffer, const DISubrange *SR,
1371 DIE *IndexTy) {
1372 DIE &DW_Subrange = createAndAddDIE(Tag: dwarf::DW_TAG_subrange_type, Parent&: Buffer);
1373 addDIEEntry(Die&: DW_Subrange, Attribute: dwarf::DW_AT_type, Entry&: *IndexTy);
1374
1375 // The LowerBound value defines the lower bounds which is typically zero for
1376 // C/C++. The Count value is the number of elements. Values are 64 bit. If
1377 // Count == -1 then the array is unbounded and we do not emit
1378 // DW_AT_lower_bound and DW_AT_count attributes.
1379 int64_t DefaultLowerBound = getDefaultLowerBound();
1380
1381 auto AddBoundTypeEntry = [&](dwarf::Attribute Attr,
1382 DISubrange::BoundType Bound) -> void {
1383 if (auto *BV = dyn_cast_if_present<DIVariable *>(Val&: Bound)) {
1384 if (auto *VarDIE = getDIE(D: BV))
1385 addDIEEntry(Die&: DW_Subrange, Attribute: Attr, Entry&: *VarDIE);
1386 } else if (auto *BE = dyn_cast_if_present<DIExpression *>(Val&: Bound)) {
1387 DIELoc *Loc = new (DIEValueAllocator) DIELoc;
1388 DIEDwarfExpression DwarfExpr(*Asm, getCU(), *Loc);
1389 DwarfExpr.setMemoryLocationKind();
1390 DwarfExpr.addExpression(Expr: BE);
1391 addBlock(Die&: DW_Subrange, Attribute: Attr, Loc: DwarfExpr.finalize());
1392 } else if (auto *BI = dyn_cast_if_present<ConstantInt *>(Val&: Bound)) {
1393 if (Attr == dwarf::DW_AT_count) {
1394 if (BI->getSExtValue() != -1)
1395 addUInt(Die&: DW_Subrange, Attribute: Attr, Form: std::nullopt, Integer: BI->getSExtValue());
1396 } else if (Attr != dwarf::DW_AT_lower_bound || DefaultLowerBound == -1 ||
1397 BI->getSExtValue() != DefaultLowerBound)
1398 addSInt(Die&: DW_Subrange, Attribute: Attr, Form: dwarf::DW_FORM_sdata, Integer: BI->getSExtValue());
1399 }
1400 };
1401
1402 AddBoundTypeEntry(dwarf::DW_AT_lower_bound, SR->getLowerBound());
1403
1404 AddBoundTypeEntry(dwarf::DW_AT_count, SR->getCount());
1405
1406 AddBoundTypeEntry(dwarf::DW_AT_upper_bound, SR->getUpperBound());
1407
1408 AddBoundTypeEntry(dwarf::DW_AT_byte_stride, SR->getStride());
1409}
1410
1411void DwarfUnit::constructGenericSubrangeDIE(DIE &Buffer,
1412 const DIGenericSubrange *GSR,
1413 DIE *IndexTy) {
1414 DIE &DwGenericSubrange =
1415 createAndAddDIE(Tag: dwarf::DW_TAG_generic_subrange, Parent&: Buffer);
1416 addDIEEntry(Die&: DwGenericSubrange, Attribute: dwarf::DW_AT_type, Entry&: *IndexTy);
1417
1418 int64_t DefaultLowerBound = getDefaultLowerBound();
1419
1420 auto AddBoundTypeEntry = [&](dwarf::Attribute Attr,
1421 DIGenericSubrange::BoundType Bound) -> void {
1422 if (auto *BV = dyn_cast_if_present<DIVariable *>(Val&: Bound)) {
1423 if (auto *VarDIE = getDIE(D: BV))
1424 addDIEEntry(Die&: DwGenericSubrange, Attribute: Attr, Entry&: *VarDIE);
1425 } else if (auto *BE = dyn_cast_if_present<DIExpression *>(Val&: Bound)) {
1426 if (BE->isConstant() &&
1427 DIExpression::SignedOrUnsignedConstant::SignedConstant ==
1428 *BE->isConstant()) {
1429 if (Attr != dwarf::DW_AT_lower_bound || DefaultLowerBound == -1 ||
1430 static_cast<int64_t>(BE->getElement(I: 1)) != DefaultLowerBound)
1431 addSInt(Die&: DwGenericSubrange, Attribute: Attr, Form: dwarf::DW_FORM_sdata,
1432 Integer: BE->getElement(I: 1));
1433 } else {
1434 DIELoc *Loc = new (DIEValueAllocator) DIELoc;
1435 DIEDwarfExpression DwarfExpr(*Asm, getCU(), *Loc);
1436 DwarfExpr.setMemoryLocationKind();
1437 DwarfExpr.addExpression(Expr: BE);
1438 addBlock(Die&: DwGenericSubrange, Attribute: Attr, Loc: DwarfExpr.finalize());
1439 }
1440 }
1441 };
1442
1443 AddBoundTypeEntry(dwarf::DW_AT_lower_bound, GSR->getLowerBound());
1444 AddBoundTypeEntry(dwarf::DW_AT_count, GSR->getCount());
1445 AddBoundTypeEntry(dwarf::DW_AT_upper_bound, GSR->getUpperBound());
1446 AddBoundTypeEntry(dwarf::DW_AT_byte_stride, GSR->getStride());
1447}
1448
1449DIE *DwarfUnit::getIndexTyDie() {
1450 if (IndexTyDie)
1451 return IndexTyDie;
1452 // Construct an integer type to use for indexes.
1453 IndexTyDie = &createAndAddDIE(Tag: dwarf::DW_TAG_base_type, Parent&: getUnitDie());
1454 StringRef Name = "__ARRAY_SIZE_TYPE__";
1455 addString(Die&: *IndexTyDie, Attribute: dwarf::DW_AT_name, String: Name);
1456 addUInt(Die&: *IndexTyDie, Attribute: dwarf::DW_AT_byte_size, Form: std::nullopt, Integer: sizeof(int64_t));
1457 addUInt(Die&: *IndexTyDie, Attribute: dwarf::DW_AT_encoding, Form: dwarf::DW_FORM_data1,
1458 Integer: dwarf::getArrayIndexTypeEncoding(
1459 S: (dwarf::SourceLanguage)getLanguage()));
1460 DD->addAccelType(Unit: *this, NameTableKind: CUNode->getNameTableKind(), Name, Die: *IndexTyDie,
1461 /*Flags*/ 0);
1462 return IndexTyDie;
1463}
1464
1465/// Returns true if the vector's size differs from the sum of sizes of elements
1466/// the user specified. This can occur if the vector has been rounded up to
1467/// fit memory alignment constraints.
1468static bool hasVectorBeenPadded(const DICompositeType *CTy) {
1469 assert(CTy && CTy->isVector() && "Composite type is not a vector");
1470 const uint64_t ActualSize = CTy->getSizeInBits();
1471
1472 // Obtain the size of each element in the vector.
1473 DIType *BaseTy = CTy->getBaseType();
1474 assert(BaseTy && "Unknown vector element type.");
1475 const uint64_t ElementSize = BaseTy->getSizeInBits();
1476
1477 // Locate the number of elements in the vector.
1478 const DINodeArray Elements = CTy->getElements();
1479 assert(Elements.size() == 1 &&
1480 Elements[0]->getTag() == dwarf::DW_TAG_subrange_type &&
1481 "Invalid vector element array, expected one element of type subrange");
1482 const auto Subrange = cast<DISubrange>(Val: Elements[0]);
1483 const auto NumVecElements =
1484 Subrange->getCount()
1485 ? cast<ConstantInt *>(Val: Subrange->getCount())->getSExtValue()
1486 : 0;
1487
1488 // Ensure we found the element count and that the actual size is wide
1489 // enough to contain the requested size.
1490 assert(ActualSize >= (NumVecElements * ElementSize) && "Invalid vector size");
1491 return ActualSize != (NumVecElements * ElementSize);
1492}
1493
1494void DwarfUnit::constructArrayTypeDIE(DIE &Buffer, const DICompositeType *CTy) {
1495 if (CTy->isVector()) {
1496 addFlag(Die&: Buffer, Attribute: dwarf::DW_AT_GNU_vector);
1497 if (hasVectorBeenPadded(CTy))
1498 addUInt(Die&: Buffer, Attribute: dwarf::DW_AT_byte_size, Form: std::nullopt,
1499 Integer: CTy->getSizeInBits() / CHAR_BIT);
1500 }
1501
1502 if (DIVariable *Var = CTy->getDataLocation()) {
1503 if (auto *VarDIE = getDIE(D: Var))
1504 addDIEEntry(Die&: Buffer, Attribute: dwarf::DW_AT_data_location, Entry&: *VarDIE);
1505 } else if (DIExpression *Expr = CTy->getDataLocationExp()) {
1506 DIELoc *Loc = new (DIEValueAllocator) DIELoc;
1507 DIEDwarfExpression DwarfExpr(*Asm, getCU(), *Loc);
1508 DwarfExpr.setMemoryLocationKind();
1509 DwarfExpr.addExpression(Expr);
1510 addBlock(Die&: Buffer, Attribute: dwarf::DW_AT_data_location, Loc: DwarfExpr.finalize());
1511 }
1512
1513 if (DIVariable *Var = CTy->getAssociated()) {
1514 if (auto *VarDIE = getDIE(D: Var))
1515 addDIEEntry(Die&: Buffer, Attribute: dwarf::DW_AT_associated, Entry&: *VarDIE);
1516 } else if (DIExpression *Expr = CTy->getAssociatedExp()) {
1517 DIELoc *Loc = new (DIEValueAllocator) DIELoc;
1518 DIEDwarfExpression DwarfExpr(*Asm, getCU(), *Loc);
1519 DwarfExpr.setMemoryLocationKind();
1520 DwarfExpr.addExpression(Expr);
1521 addBlock(Die&: Buffer, Attribute: dwarf::DW_AT_associated, Loc: DwarfExpr.finalize());
1522 }
1523
1524 if (DIVariable *Var = CTy->getAllocated()) {
1525 if (auto *VarDIE = getDIE(D: Var))
1526 addDIEEntry(Die&: Buffer, Attribute: dwarf::DW_AT_allocated, Entry&: *VarDIE);
1527 } else if (DIExpression *Expr = CTy->getAllocatedExp()) {
1528 DIELoc *Loc = new (DIEValueAllocator) DIELoc;
1529 DIEDwarfExpression DwarfExpr(*Asm, getCU(), *Loc);
1530 DwarfExpr.setMemoryLocationKind();
1531 DwarfExpr.addExpression(Expr);
1532 addBlock(Die&: Buffer, Attribute: dwarf::DW_AT_allocated, Loc: DwarfExpr.finalize());
1533 }
1534
1535 if (auto *RankConst = CTy->getRankConst()) {
1536 addSInt(Die&: Buffer, Attribute: dwarf::DW_AT_rank, Form: dwarf::DW_FORM_sdata,
1537 Integer: RankConst->getSExtValue());
1538 } else if (auto *RankExpr = CTy->getRankExp()) {
1539 DIELoc *Loc = new (DIEValueAllocator) DIELoc;
1540 DIEDwarfExpression DwarfExpr(*Asm, getCU(), *Loc);
1541 DwarfExpr.setMemoryLocationKind();
1542 DwarfExpr.addExpression(Expr: RankExpr);
1543 addBlock(Die&: Buffer, Attribute: dwarf::DW_AT_rank, Loc: DwarfExpr.finalize());
1544 }
1545
1546 // Emit the element type.
1547 addType(Entity&: Buffer, Ty: CTy->getBaseType());
1548
1549 // Get an anonymous type for index type.
1550 // FIXME: This type should be passed down from the front end
1551 // as different languages may have different sizes for indexes.
1552 DIE *IdxTy = getIndexTyDie();
1553
1554 // Add subranges to array type.
1555 DINodeArray Elements = CTy->getElements();
1556 for (DINode *E : Elements) {
1557 // FIXME: Should this really be such a loose cast?
1558 if (auto *Element = dyn_cast_or_null<DINode>(Val: E)) {
1559 if (Element->getTag() == dwarf::DW_TAG_subrange_type)
1560 constructSubrangeDIE(Buffer, SR: cast<DISubrange>(Val: Element), IndexTy: IdxTy);
1561 else if (Element->getTag() == dwarf::DW_TAG_generic_subrange)
1562 constructGenericSubrangeDIE(Buffer, GSR: cast<DIGenericSubrange>(Val: Element),
1563 IndexTy: IdxTy);
1564 }
1565 }
1566}
1567
1568void DwarfUnit::constructEnumTypeDIE(DIE &Buffer, const DICompositeType *CTy) {
1569 const DIType *DTy = CTy->getBaseType();
1570 bool IsUnsigned = DTy && DD->isUnsignedDIType(Ty: DTy);
1571 if (DTy) {
1572 if (DD->getDwarfVersion() >= 3)
1573 addType(Entity&: Buffer, Ty: DTy);
1574 if (DD->getDwarfVersion() >= 4 && (CTy->getFlags() & DINode::FlagEnumClass))
1575 addFlag(Die&: Buffer, Attribute: dwarf::DW_AT_enum_class);
1576 }
1577
1578 auto *Context = CTy->getScope();
1579 bool IndexEnumerators = !Context || isa<DICompileUnit>(Val: Context) || isa<DIFile>(Val: Context) ||
1580 isa<DINamespace>(Val: Context) || isa<DICommonBlock>(Val: Context);
1581 DINodeArray Elements = CTy->getElements();
1582
1583 // Add enumerators to enumeration type.
1584 for (const DINode *E : Elements) {
1585 auto *Enum = dyn_cast_or_null<DIEnumerator>(Val: E);
1586 if (Enum) {
1587 DIE &Enumerator = createAndAddDIE(Tag: dwarf::DW_TAG_enumerator, Parent&: Buffer);
1588 StringRef Name = Enum->getName();
1589 addString(Die&: Enumerator, Attribute: dwarf::DW_AT_name, String: Name);
1590 addConstantValue(Die&: Enumerator, Val: Enum->getValue(), Unsigned: IsUnsigned);
1591 if (IndexEnumerators)
1592 addGlobalName(Name, Die: Enumerator, Context);
1593 }
1594 }
1595}
1596
1597void DwarfUnit::constructContainingTypeDIEs() {
1598 for (auto &P : ContainingTypeMap) {
1599 DIE &SPDie = *P.first;
1600 const DINode *D = P.second;
1601 if (!D)
1602 continue;
1603 DIE *NDie = getDIE(D);
1604 if (!NDie)
1605 continue;
1606 addDIEEntry(Die&: SPDie, Attribute: dwarf::DW_AT_containing_type, Entry&: *NDie);
1607 }
1608}
1609
1610DIE &DwarfUnit::constructMemberDIE(DIE &Buffer, const DIDerivedType *DT) {
1611 DIE &MemberDie = createAndAddDIE(Tag: DT->getTag(), Parent&: Buffer);
1612 StringRef Name = DT->getName();
1613 if (!Name.empty())
1614 addString(Die&: MemberDie, Attribute: dwarf::DW_AT_name, String: Name);
1615
1616 addAnnotation(Buffer&: MemberDie, Annotations: DT->getAnnotations());
1617
1618 if (DIType *Resolved = DT->getBaseType())
1619 addType(Entity&: MemberDie, Ty: Resolved);
1620
1621 addSourceLine(Die&: MemberDie, Ty: DT);
1622
1623 if (DT->getTag() == dwarf::DW_TAG_inheritance && DT->isVirtual()) {
1624
1625 // For C++, virtual base classes are not at fixed offset. Use following
1626 // expression to extract appropriate offset from vtable.
1627 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1628
1629 DIELoc *VBaseLocationDie = new (DIEValueAllocator) DIELoc;
1630 addUInt(Block&: *VBaseLocationDie, Form: dwarf::DW_FORM_data1, Integer: dwarf::DW_OP_dup);
1631 addUInt(Block&: *VBaseLocationDie, Form: dwarf::DW_FORM_data1, Integer: dwarf::DW_OP_deref);
1632 addUInt(Block&: *VBaseLocationDie, Form: dwarf::DW_FORM_data1, Integer: dwarf::DW_OP_constu);
1633 addUInt(Block&: *VBaseLocationDie, Form: dwarf::DW_FORM_udata, Integer: DT->getOffsetInBits());
1634 addUInt(Block&: *VBaseLocationDie, Form: dwarf::DW_FORM_data1, Integer: dwarf::DW_OP_minus);
1635 addUInt(Block&: *VBaseLocationDie, Form: dwarf::DW_FORM_data1, Integer: dwarf::DW_OP_deref);
1636 addUInt(Block&: *VBaseLocationDie, Form: dwarf::DW_FORM_data1, Integer: dwarf::DW_OP_plus);
1637
1638 addBlock(Die&: MemberDie, Attribute: dwarf::DW_AT_data_member_location, Loc: VBaseLocationDie);
1639 } else {
1640 uint64_t Size = DT->getSizeInBits();
1641 uint64_t FieldSize = DD->getBaseTypeSize(Ty: DT);
1642 uint32_t AlignInBytes = DT->getAlignInBytes();
1643 uint64_t OffsetInBytes;
1644
1645 bool IsBitfield = DT->isBitField();
1646 if (IsBitfield) {
1647 // Handle bitfield, assume bytes are 8 bits.
1648 if (DD->useDWARF2Bitfields())
1649 addUInt(Die&: MemberDie, Attribute: dwarf::DW_AT_byte_size, Form: std::nullopt, Integer: FieldSize / 8);
1650 addUInt(Die&: MemberDie, Attribute: dwarf::DW_AT_bit_size, Form: std::nullopt, Integer: Size);
1651
1652 uint64_t Offset = DT->getOffsetInBits();
1653 // We can't use DT->getAlignInBits() here: AlignInBits for member type
1654 // is non-zero if and only if alignment was forced (e.g. _Alignas()),
1655 // which can't be done with bitfields. Thus we use FieldSize here.
1656 uint32_t AlignInBits = FieldSize;
1657 uint32_t AlignMask = ~(AlignInBits - 1);
1658 // The bits from the start of the storage unit to the start of the field.
1659 uint64_t StartBitOffset = Offset - (Offset & AlignMask);
1660 // The byte offset of the field's aligned storage unit inside the struct.
1661 OffsetInBytes = (Offset - StartBitOffset) / 8;
1662
1663 if (DD->useDWARF2Bitfields()) {
1664 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1665 uint64_t FieldOffset = (HiMark - FieldSize);
1666 Offset -= FieldOffset;
1667
1668 // Maybe we need to work from the other end.
1669 if (Asm->getDataLayout().isLittleEndian())
1670 Offset = FieldSize - (Offset + Size);
1671
1672 addUInt(Die&: MemberDie, Attribute: dwarf::DW_AT_bit_offset, Form: std::nullopt, Integer: Offset);
1673 OffsetInBytes = FieldOffset >> 3;
1674 } else {
1675 addUInt(Die&: MemberDie, Attribute: dwarf::DW_AT_data_bit_offset, Form: std::nullopt, Integer: Offset);
1676 }
1677 } else {
1678 // This is not a bitfield.
1679 OffsetInBytes = DT->getOffsetInBits() / 8;
1680 if (AlignInBytes)
1681 addUInt(Die&: MemberDie, Attribute: dwarf::DW_AT_alignment, Form: dwarf::DW_FORM_udata,
1682 Integer: AlignInBytes);
1683 }
1684
1685 if (DD->getDwarfVersion() <= 2) {
1686 DIELoc *MemLocationDie = new (DIEValueAllocator) DIELoc;
1687 addUInt(Block&: *MemLocationDie, Form: dwarf::DW_FORM_data1, Integer: dwarf::DW_OP_plus_uconst);
1688 addUInt(Block&: *MemLocationDie, Form: dwarf::DW_FORM_udata, Integer: OffsetInBytes);
1689 addBlock(Die&: MemberDie, Attribute: dwarf::DW_AT_data_member_location, Loc: MemLocationDie);
1690 } else if (!IsBitfield || DD->useDWARF2Bitfields()) {
1691 // In DWARF v3, DW_FORM_data4/8 in DW_AT_data_member_location are
1692 // interpreted as location-list pointers. Interpreting constants as
1693 // pointers is not expected, so we use DW_FORM_udata to encode the
1694 // constants here.
1695 if (DD->getDwarfVersion() == 3)
1696 addUInt(Die&: MemberDie, Attribute: dwarf::DW_AT_data_member_location,
1697 Form: dwarf::DW_FORM_udata, Integer: OffsetInBytes);
1698 else
1699 addUInt(Die&: MemberDie, Attribute: dwarf::DW_AT_data_member_location, Form: std::nullopt,
1700 Integer: OffsetInBytes);
1701 }
1702 }
1703
1704 addAccess(Die&: MemberDie, Flags: DT->getFlags());
1705
1706 if (DT->isVirtual())
1707 addUInt(Die&: MemberDie, Attribute: dwarf::DW_AT_virtuality, Form: dwarf::DW_FORM_data1,
1708 Integer: dwarf::DW_VIRTUALITY_virtual);
1709
1710 // Objective-C properties.
1711 if (DINode *PNode = DT->getObjCProperty())
1712 if (DIE *PDie = getDIE(D: PNode))
1713 addAttribute(Die&: MemberDie, Attribute: dwarf::DW_AT_APPLE_property,
1714 Form: dwarf::DW_FORM_ref4, Value: DIEEntry(*PDie));
1715
1716 if (DT->isArtificial())
1717 addFlag(Die&: MemberDie, Attribute: dwarf::DW_AT_artificial);
1718
1719 return MemberDie;
1720}
1721
1722DIE *DwarfUnit::getOrCreateStaticMemberDIE(const DIDerivedType *DT) {
1723 if (!DT)
1724 return nullptr;
1725
1726 // Construct the context before querying for the existence of the DIE in case
1727 // such construction creates the DIE.
1728 DIE *ContextDIE = getOrCreateContextDIE(Context: DT->getScope());
1729 assert(dwarf::isType(ContextDIE->getTag()) &&
1730 "Static member should belong to a type.");
1731
1732 if (DIE *StaticMemberDIE = getDIE(D: DT))
1733 return StaticMemberDIE;
1734
1735 DIE &StaticMemberDIE = createAndAddDIE(Tag: DT->getTag(), Parent&: *ContextDIE, N: DT);
1736
1737 const DIType *Ty = DT->getBaseType();
1738
1739 addString(Die&: StaticMemberDIE, Attribute: dwarf::DW_AT_name, String: DT->getName());
1740 addType(Entity&: StaticMemberDIE, Ty);
1741 addSourceLine(Die&: StaticMemberDIE, Ty: DT);
1742 addFlag(Die&: StaticMemberDIE, Attribute: dwarf::DW_AT_external);
1743 addFlag(Die&: StaticMemberDIE, Attribute: dwarf::DW_AT_declaration);
1744
1745 // FIXME: We could omit private if the parent is a class_type, and
1746 // public if the parent is something else.
1747 addAccess(Die&: StaticMemberDIE, Flags: DT->getFlags());
1748
1749 if (const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(Val: DT->getConstant()))
1750 addConstantValue(Die&: StaticMemberDIE, CI, Ty);
1751 if (const ConstantFP *CFP = dyn_cast_or_null<ConstantFP>(Val: DT->getConstant()))
1752 addConstantFPValue(Die&: StaticMemberDIE, CFP);
1753
1754 if (uint32_t AlignInBytes = DT->getAlignInBytes())
1755 addUInt(Die&: StaticMemberDIE, Attribute: dwarf::DW_AT_alignment, Form: dwarf::DW_FORM_udata,
1756 Integer: AlignInBytes);
1757
1758 return &StaticMemberDIE;
1759}
1760
1761void DwarfUnit::emitCommonHeader(bool UseOffsets, dwarf::UnitType UT) {
1762 // Emit size of content not including length itself
1763 if (!DD->useSectionsAsReferences())
1764 EndLabel = Asm->emitDwarfUnitLength(
1765 Prefix: isDwoUnit() ? "debug_info_dwo" : "debug_info", Comment: "Length of Unit");
1766 else
1767 Asm->emitDwarfUnitLength(Length: getHeaderSize() + getUnitDie().getSize(),
1768 Comment: "Length of Unit");
1769
1770 Asm->OutStreamer->AddComment(T: "DWARF version number");
1771 unsigned Version = DD->getDwarfVersion();
1772 Asm->emitInt16(Value: Version);
1773
1774 // DWARF v5 reorders the address size and adds a unit type.
1775 if (Version >= 5) {
1776 Asm->OutStreamer->AddComment(T: "DWARF Unit Type");
1777 Asm->emitInt8(Value: UT);
1778 Asm->OutStreamer->AddComment(T: "Address Size (in bytes)");
1779 Asm->emitInt8(Value: Asm->MAI->getCodePointerSize());
1780 }
1781
1782 // We share one abbreviations table across all units so it's always at the
1783 // start of the section. Use a relocatable offset where needed to ensure
1784 // linking doesn't invalidate that offset.
1785 Asm->OutStreamer->AddComment(T: "Offset Into Abbrev. Section");
1786 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
1787 if (UseOffsets)
1788 Asm->emitDwarfLengthOrOffset(Value: 0);
1789 else
1790 Asm->emitDwarfSymbolReference(
1791 Label: TLOF.getDwarfAbbrevSection()->getBeginSymbol(), ForceOffset: false);
1792
1793 if (Version <= 4) {
1794 Asm->OutStreamer->AddComment(T: "Address Size (in bytes)");
1795 Asm->emitInt8(Value: Asm->MAI->getCodePointerSize());
1796 }
1797}
1798
1799void DwarfTypeUnit::emitHeader(bool UseOffsets) {
1800 if (!DD->useSplitDwarf()) {
1801 LabelBegin = Asm->createTempSymbol(Name: "tu_begin");
1802 Asm->OutStreamer->emitLabel(Symbol: LabelBegin);
1803 }
1804 DwarfUnit::emitCommonHeader(UseOffsets,
1805 UT: DD->useSplitDwarf() ? dwarf::DW_UT_split_type
1806 : dwarf::DW_UT_type);
1807 Asm->OutStreamer->AddComment(T: "Type Signature");
1808 Asm->OutStreamer->emitIntValue(Value: TypeSignature, Size: sizeof(TypeSignature));
1809 Asm->OutStreamer->AddComment(T: "Type DIE Offset");
1810 // In a skeleton type unit there is no type DIE so emit a zero offset.
1811 Asm->emitDwarfLengthOrOffset(Value: Ty ? Ty->getOffset() : 0);
1812}
1813
1814void DwarfUnit::addSectionDelta(DIE &Die, dwarf::Attribute Attribute,
1815 const MCSymbol *Hi, const MCSymbol *Lo) {
1816 addAttribute(Die, Attribute, Form: DD->getDwarfSectionOffsetForm(),
1817 Value: new (DIEValueAllocator) DIEDelta(Hi, Lo));
1818}
1819
1820void DwarfUnit::addSectionLabel(DIE &Die, dwarf::Attribute Attribute,
1821 const MCSymbol *Label, const MCSymbol *Sec) {
1822 if (Asm->doesDwarfUseRelocationsAcrossSections())
1823 addLabel(Die, Attribute, Form: DD->getDwarfSectionOffsetForm(), Label);
1824 else
1825 addSectionDelta(Die, Attribute, Hi: Label, Lo: Sec);
1826}
1827
1828bool DwarfTypeUnit::isDwoUnit() const {
1829 // Since there are no skeleton type units, all type units are dwo type units
1830 // when split DWARF is being used.
1831 return DD->useSplitDwarf();
1832}
1833
1834void DwarfTypeUnit::addGlobalName(StringRef Name, const DIE &Die,
1835 const DIScope *Context) {
1836 getCU().addGlobalNameForTypeUnit(Name, Context);
1837}
1838
1839void DwarfTypeUnit::addGlobalType(const DIType *Ty, const DIE &Die,
1840 const DIScope *Context) {
1841 getCU().addGlobalTypeUnitType(Ty, Context);
1842}
1843
1844const MCSymbol *DwarfUnit::getCrossSectionRelativeBaseAddress() const {
1845 if (!Asm->doesDwarfUseRelocationsAcrossSections())
1846 return nullptr;
1847 if (isDwoUnit())
1848 return nullptr;
1849 return getSection()->getBeginSymbol();
1850}
1851
1852void DwarfUnit::addStringOffsetsStart() {
1853 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
1854 addSectionLabel(Die&: getUnitDie(), Attribute: dwarf::DW_AT_str_offsets_base,
1855 Label: DU->getStringOffsetsStartSym(),
1856 Sec: TLOF.getDwarfStrOffSection()->getBeginSymbol());
1857}
1858
1859void DwarfUnit::addRnglistsBase() {
1860 assert(DD->getDwarfVersion() >= 5 &&
1861 "DW_AT_rnglists_base requires DWARF version 5 or later");
1862 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
1863 addSectionLabel(Die&: getUnitDie(), Attribute: dwarf::DW_AT_rnglists_base,
1864 Label: DU->getRnglistsTableBaseSym(),
1865 Sec: TLOF.getDwarfRnglistsSection()->getBeginSymbol());
1866}
1867
1868void DwarfTypeUnit::finishNonUnitTypeDIE(DIE& D, const DICompositeType *CTy) {
1869 DD->getAddressPool().resetUsedFlag(HasBeenUsed: true);
1870}
1871
1872bool DwarfUnit::isCompatibleWithVersion(uint16_t Version) const {
1873 return !Asm->TM.Options.DebugStrictDwarf || DD->getDwarfVersion() >= Version;
1874}
1875

source code of llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp