LLVM 24.0.0git
DwarfUnit.cpp
Go to the documentation of this file.
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"
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"
31#include <cassert>
32#include <cstdint>
33#include <limits>
34#include <string>
35
36using namespace llvm;
37
38#define DEBUG_TYPE "dwarfdebug"
39
42 : DwarfExpression(AP.getDwarfVersion(), CU), AP(AP), OutDIE(DIE) {}
43
44void DIEDwarfExpression::emitOp(uint8_t Op, const char* Comment) {
45 CU.addUInt(getActiveDIE(), dwarf::DW_FORM_data1, Op);
46}
47
48void DIEDwarfExpression::emitSigned(int64_t Value) {
49 CU.addSInt(getActiveDIE(), dwarf::DW_FORM_sdata, Value);
50}
51
52void DIEDwarfExpression::emitUnsigned(uint64_t Value) {
53 CU.addUInt(getActiveDIE(), dwarf::DW_FORM_udata, Value);
54}
55
56void DIEDwarfExpression::emitData1(uint8_t Value) {
57 CU.addUInt(getActiveDIE(), dwarf::DW_FORM_data1, Value);
58}
59
60void DIEDwarfExpression::emitBaseTypeRef(uint64_t Idx) {
61 CU.addBaseTypeRef(getActiveDIE(), Idx);
62}
63
65 assert(!IsBuffering && "Already buffering?");
66 IsBuffering = true;
67}
68
69void DIEDwarfExpression::disableTemporaryBuffer() { IsBuffering = false; }
70
72 return TmpDIE.computeSize(AP.getDwarfFormParams());
73}
74
75void DIEDwarfExpression::commitTemporaryBuffer() { OutDIE.takeValues(TmpDIE); }
76
77bool DIEDwarfExpression::isFrameRegister(const TargetRegisterInfo &TRI,
78 llvm::Register MachineReg) {
79 return MachineReg == TRI.getFrameRegister(*AP.MF);
80}
81
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
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
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 (getSourceLanguage()) {
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.
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
189 // Prevent generation of cross-CU references for DWARF v2 due to conflicts
190 // resulting from the FAQ recommendation: "If you are producing DWARF V2,
191 // please use the DWARF V3 definition of DW_FORM_ref_addr."
192 // (https://dwarfstd.org/faq.html)
193 if (DD->getDwarfVersion() == 2)
194 return false;
195 if (isDwoUnit() && !DD->shareAcrossDWOCUs())
196 return false;
197 return (isa<DIType>(D) ||
198 (isa<DISubprogram>(D) && !cast<DISubprogram>(D)->isDefinition())) &&
199 !DD->generateTypeUnits();
200}
201
204 return DU->getDIE(D);
205 return MDNodeToDieMap.lookup(D);
206}
207
210 DU->insertDIE(Desc, D);
211 return;
212 }
213 MDNodeToDieMap.insert(std::make_pair(Desc, D));
214}
215
217 MDNodeToDieMap.insert(std::make_pair(nullptr, D));
218}
219
221 if (DD->getDwarfVersion() >= 4)
222 addAttribute(Die, Attribute, dwarf::DW_FORM_flag_present, DIEInteger(1));
223 else
224 addAttribute(Die, Attribute, dwarf::DW_FORM_flag, DIEInteger(1));
225}
226
228 std::optional<dwarf::Form> Form, uint64_t Integer) {
229 if (!Form)
230 Form = DIEInteger::BestForm(false, Integer);
231 assert(Form != dwarf::DW_FORM_implicit_const &&
232 "DW_FORM_implicit_const is used only for signed integers");
234}
235
240
241void DwarfUnit::addIntToBlock(DIEBlock &Block, const APInt &Val) {
242 // Get the raw data form of the large APInt.
243 const uint64_t *Ptr64 = Val.getRawData();
244
245 int NumBytes = Val.getBitWidth() / 8; // 8 bits per byte.
246 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
247
248 // Output the constant to DWARF one byte at a time.
249 for (int i = 0; i < NumBytes; i++) {
250 uint8_t c;
251 if (LittleEndian)
252 c = Ptr64[i / 8] >> (8 * (i & 7));
253 else
254 c = Ptr64[(NumBytes - 1 - i) / 8] >> (8 * ((NumBytes - 1 - i) & 7));
255 addUInt(Block, dwarf::DW_FORM_data1, c);
256 }
257}
258
259void DwarfUnit::addIntAsBlock(DIE &Die, dwarf::Attribute Attribute,
260 const APInt &Val) {
261 DIEBlock *Block = new (DIEValueAllocator) DIEBlock;
262 addIntToBlock(*Block, Val);
263 Block->computeSize(Asm->getDwarfFormParams());
264 addBlock(Die, Attribute, Block->BestForm(), Block);
265}
266
268 const APInt &Val, bool Unsigned) {
269 unsigned CIBitWidth = Val.getBitWidth();
270 if (CIBitWidth <= 64) {
271 if (Unsigned)
272 addUInt(Die, Attribute, std::nullopt, Val.getZExtValue());
273 else
274 addSInt(Die, Attribute, std::nullopt, Val.getSExtValue());
275 return;
276 }
277
278 addIntAsBlock(Die, Attribute, Val);
279}
280
282 std::optional<dwarf::Form> Form, int64_t Integer) {
283 if (!Form)
284 Form = DIEInteger::BestForm(true, Integer);
286}
287
288void DwarfUnit::addSInt(DIEValueList &Die, std::optional<dwarf::Form> Form,
289 int64_t Integer) {
290 addSInt(Die, (dwarf::Attribute)0, Form, Integer);
291}
292
295 if (CUNode->isDebugDirectivesOnly())
296 return;
297
298 if (DD->useInlineStrings()) {
299 addAttribute(Die, Attribute, dwarf::DW_FORM_string,
302 return;
303 }
304 dwarf::Form IxForm =
305 isDwoUnit() ? dwarf::DW_FORM_GNU_str_index : dwarf::DW_FORM_strp;
306
307 auto StringPoolEntry =
308 useSegmentedStringOffsetsTable() || IxForm == dwarf::DW_FORM_GNU_str_index
309 ? DU->getStringPool().getIndexedEntry(*Asm, String)
310 : DU->getStringPool().getEntry(*Asm, String);
311
312 // For DWARF v5 and beyond, use the smallest strx? form possible.
314 IxForm = dwarf::DW_FORM_strx1;
315 unsigned Index = StringPoolEntry.getIndex();
316 if (Index > 0xffffff)
317 IxForm = dwarf::DW_FORM_strx4;
318 else if (Index > 0xffff)
319 IxForm = dwarf::DW_FORM_strx3;
320 else if (Index > 0xff)
321 IxForm = dwarf::DW_FORM_strx2;
322 }
323 addAttribute(Die, Attribute, IxForm, DIEString(StringPoolEntry));
324}
325
327 dwarf::Form Form, const MCSymbol *Label) {
328 addAttribute(Die, Attribute, Form, DIELabel(Label));
329}
330
331void DwarfUnit::addLabel(DIELoc &Die, dwarf::Form Form, const MCSymbol *Label) {
332 addLabel(Die, (dwarf::Attribute)0, Form, Label);
333}
334
336 uint64_t Integer) {
337 addUInt(Die, Attribute, DD->getDwarfSectionOffsetForm(), Integer);
338}
339
340unsigned DwarfTypeUnit::getOrCreateSourceID(const DIFile *File) {
341 if (!SplitLineTable)
342 return getCU().getOrCreateSourceID(File);
343 if (!UsedLineTable) {
344 UsedLineTable = true;
345 // This is a split type unit that needs a line table.
346 addSectionOffset(getUnitDie(), dwarf::DW_AT_stmt_list, 0);
347 }
348 return SplitLineTable->getFile(
349 File->getDirectory(), File->getFilename(), DD->getMD5AsBytes(File),
350 Asm->OutContext.getDwarfVersion(), File->getSource());
351}
352
354 bool UseAddrOffsetFormOrExpressions =
355 DD->useAddrOffsetForm() || DD->useAddrOffsetExpressions();
356
357 const MCSymbol *Base = nullptr;
358 if (Label->isInSection() && UseAddrOffsetFormOrExpressions)
359 Base = DD->getSectionLabel(&Label->getSection());
360
361 uint32_t Index = DD->getAddressPool().getIndex(Base ? Base : Label);
362
363 if (DD->getDwarfVersion() >= 5) {
364 addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_addrx);
365 addUInt(Die, dwarf::DW_FORM_addrx, Index);
366 } else {
367 addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_addr_index);
368 addUInt(Die, dwarf::DW_FORM_GNU_addr_index, Index);
369 }
370
371 if (Base && Base != Label) {
372 addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_const4u);
373 addLabelDelta(Die, (dwarf::Attribute)0, Label, Base);
374 addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
375 }
376}
377
379 if (DD->getDwarfVersion() >= 5) {
380 addPoolOpAddress(Die, Sym);
381 return;
382 }
383
384 if (DD->useSplitDwarf()) {
385 addPoolOpAddress(Die, Sym);
386 return;
387 }
388
389 addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
390 addLabel(Die, dwarf::DW_FORM_addr, Sym);
391}
392
394 const MCSymbol *Hi, const MCSymbol *Lo) {
395 addAttribute(Die, Attribute, dwarf::DW_FORM_data4,
397}
398
402
403void DwarfUnit::addDIETypeSignature(DIE &Die, uint64_t Signature) {
404 // Flag the type unit reference as a declaration so that if it contains
405 // members (implicit special members, static data member definitions, member
406 // declarations for definitions in this CU, etc) consumers don't get confused
407 // and think this is a full definition.
408 addFlag(Die, dwarf::DW_AT_declaration);
409
410 addAttribute(Die, dwarf::DW_AT_signature, dwarf::DW_FORM_ref_sig8,
411 DIEInteger(Signature));
412}
413
415 DIEEntry Entry) {
416 const DIEUnit *CU = Die.getUnit();
417 const DIEUnit *EntryCU = Entry.getEntry().getUnit();
418 if (!CU)
419 // We assume that Die belongs to this CU, if it is not linked to any CU yet.
420 CU = getUnitDie().getUnit();
421 if (!EntryCU)
422 EntryCU = getUnitDie().getUnit();
423 assert(EntryCU == CU || !DD->useSplitDwarf() || DD->shareAcrossDWOCUs() ||
424 !static_cast<const DwarfUnit*>(CU)->isDwoUnit());
426 EntryCU == CU ? dwarf::DW_FORM_ref4 : dwarf::DW_FORM_ref_addr,
427 Entry);
428}
429
431 DIE &Die = Parent.addChild(DIE::get(DIEValueAllocator, Tag));
432 if (N)
433 insertDIE(N, &Die);
434 return Die;
435}
436
438 Loc->computeSize(Asm->getDwarfFormParams());
439 DIELocs.push_back(Loc); // Memoize so we can call the destructor later on.
440 addAttribute(Die, Attribute, Loc->BestForm(DD->getDwarfVersion()), Loc);
441}
442
444 DIEBlock *Block) {
445 Block->computeSize(Asm->getDwarfFormParams());
446 DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on.
447 addAttribute(Die, Attribute, Form, Block);
448}
449
454
456 const DIExpression *Expr) {
458 DIEDwarfExpression DwarfExpr(*Asm, getCU(), *Loc);
459 DwarfExpr.setMemoryLocationKind();
460 DwarfExpr.addExpression(Expr);
461 addBlock(Die, Attribute, DwarfExpr.finalize());
462}
463
464void DwarfUnit::addSourceLine(DIE &Die, unsigned Line, unsigned Column,
465 const DIFile *File) {
466 if (Line == 0)
467 return;
468
469 unsigned FileID = getOrCreateSourceID(File);
470 addUInt(Die, dwarf::DW_AT_decl_file, std::nullopt, FileID);
471 addUInt(Die, dwarf::DW_AT_decl_line, std::nullopt, Line);
472
473 if (Column != 0)
474 addUInt(Die, dwarf::DW_AT_decl_column, std::nullopt, Column);
475}
476
478 assert(V);
479
480 addSourceLine(Die, V->getLine(), /*Column*/ 0, V->getFile());
481}
482
484 assert(G);
485
486 addSourceLine(Die, G->getLine(), /*Column*/ 0, G->getFile());
487}
488
490 assert(SP);
491
492 addSourceLine(Die, SP->getLine(), /*Column*/ 0, SP->getFile());
493}
494
496 assert(L);
497
498 addSourceLine(Die, L->getLine(), L->getColumn(), L->getFile());
499}
500
501void DwarfUnit::addSourceLine(DIE &Die, const DIType *Ty) {
502 assert(Ty);
503
504 addSourceLine(Die, Ty->getLine(), /*Column*/ 0, Ty->getFile());
505}
506
508 assert(Ty);
509
510 addSourceLine(Die, Ty->getLine(), /*Column*/ 0, Ty->getFile());
511}
512
514 assert(P);
515
516 addSourceLine(Die, P->getLine(), /*Column*/ 0, P->getFile());
517}
518
520 // Pass this down to addConstantValue as an unsigned bag of bits.
521 addConstantValue(Die, CFP->getValueAPF().bitcastToAPInt(), true);
522}
523
525 const DIType *Ty) {
526 addConstantValue(Die, CI->getValue(), Ty);
527}
528
529void DwarfUnit::addConstantValue(DIE &Die, uint64_t Val, const DIType *Ty) {
530 addConstantValue(Die, DD->isUnsignedDIType(Ty), Val);
531}
532
533void DwarfUnit::addConstantValue(DIE &Die, bool Unsigned, uint64_t Val) {
534 // FIXME: This is a bit conservative/simple - it emits negative values always
535 // sign extended to 64 bits rather than minimizing the number of bytes.
536 addUInt(Die, dwarf::DW_AT_const_value,
537 Unsigned ? dwarf::DW_FORM_udata : dwarf::DW_FORM_sdata, Val);
538}
539
540void DwarfUnit::addConstantValue(DIE &Die, const APInt &Val, const DIType *Ty) {
541 addConstantValue(Die, Val, DD->isUnsignedDIType(Ty));
542}
543
544void DwarfUnit::addConstantValue(DIE &Die, const APInt &Val, bool Unsigned) {
545 unsigned CIBitWidth = Val.getBitWidth();
546 if (CIBitWidth <= 64) {
548 Unsigned ? Val.getZExtValue() : Val.getSExtValue());
549 return;
550 }
551
552 addIntAsBlock(Die, dwarf::DW_AT_const_value, Val);
553}
554
556 if (!LinkageName.empty())
557 addString(Die,
558 DD->getDwarfVersion() >= 4 ? dwarf::DW_AT_linkage_name
559 : dwarf::DW_AT_MIPS_linkage_name,
561}
562
563void DwarfUnit::addTemplateParams(DIE &Buffer, DINodeArray TParams) {
564 // Add template parameters.
565 for (const auto *Element : TParams) {
566 if (auto *TTP = dyn_cast<DITemplateTypeParameter>(Element))
567 constructTemplateTypeParameterDIE(Buffer, TTP);
568 else if (auto *TVP = dyn_cast<DITemplateValueParameter>(Element))
569 constructTemplateValueParameterDIE(Buffer, TVP);
570 }
571}
572
573/// Add thrown types.
574void DwarfUnit::addThrownTypes(DIE &Die, DINodeArray ThrownTypes) {
575 for (const auto *Ty : ThrownTypes) {
576 DIE &TT = createAndAddDIE(dwarf::DW_TAG_thrown_type, Die);
577 addType(TT, cast<DIType>(Ty));
578 }
579}
580
582 if ((Flags & DINode::FlagAccessibility) == DINode::FlagProtected)
583 addUInt(Die, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
585 else if ((Flags & DINode::FlagAccessibility) == DINode::FlagPrivate)
586 addUInt(Die, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
588 else if ((Flags & DINode::FlagAccessibility) == DINode::FlagPublic)
589 addUInt(Die, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
591}
592
594 if (!Context || isa<DIFile>(Context) || isa<DICompileUnit>(Context))
595 return &getUnitDie();
596 if (auto *T = dyn_cast<DIType>(Context))
597 return getOrCreateTypeDIE(T);
598 if (auto *NS = dyn_cast<DINamespace>(Context))
599 return getOrCreateNameSpace(NS);
600 if (auto *SP = dyn_cast<DISubprogram>(Context))
601 return getOrCreateSubprogramDIE(SP, nullptr);
602 if (auto *M = dyn_cast<DIModule>(Context))
603 return getOrCreateModule(M);
604 return getDIE(Context);
605}
606
608 auto *Context = Ty->getScope();
609 DIE *ContextDIE = getOrCreateContextDIE(Context);
610
611 if (DIE *TyDIE = getDIE(Ty))
612 return TyDIE;
613
614 // Create new type.
615 DIE &TyDIE = createAndAddDIE(Ty->getTag(), *ContextDIE, Ty);
616
618
619 updateAcceleratorTables(Context, Ty, TyDIE);
620 return &TyDIE;
621}
622
623DIE *DwarfUnit::createTypeDIE(const DIScope *Context, DIE &ContextDIE,
624 const DIType *Ty) {
625 // Create new type.
626 DIE &TyDIE = createAndAddDIE(Ty->getTag(), ContextDIE, Ty);
627
628 auto construct = [&](const auto *Ty) {
629 updateAcceleratorTables(Context, Ty, TyDIE);
630 constructTypeDIE(TyDIE, Ty);
631 };
632
633 if (auto *CTy = dyn_cast<DICompositeType>(Ty)) {
634 if (DD->generateTypeUnits() && !Ty->isForwardDecl() &&
635 (Ty->getRawName() || CTy->getRawIdentifier())) {
636 // Skip updating the accelerator tables since this is not the full type.
637 if (MDString *TypeId = CTy->getRawIdentifier()) {
638 addGlobalType(Ty, TyDIE, Context);
639 DD->addDwarfTypeUnitType(getCU(), TypeId->getString(), TyDIE, CTy);
640 } else {
641 updateAcceleratorTables(Context, Ty, TyDIE);
642 finishNonUnitTypeDIE(TyDIE, CTy);
643 }
644 return &TyDIE;
645 }
646 construct(CTy);
647 } else if (auto *FPT = dyn_cast<DIFixedPointType>(Ty))
648 construct(FPT);
649 else if (auto *BT = dyn_cast<DIBasicType>(Ty))
650 construct(BT);
651 else if (auto *ST = dyn_cast<DIStringType>(Ty))
652 construct(ST);
653 else if (auto *STy = dyn_cast<DISubroutineType>(Ty))
654 construct(STy);
655 else if (auto *SRTy = dyn_cast<DISubrangeType>(Ty))
656 constructSubrangeDIE(TyDIE, SRTy);
657 else
658 construct(cast<DIDerivedType>(Ty));
659
660 return &TyDIE;
661}
662
664 if (!TyNode)
665 return nullptr;
666
667 auto *Ty = cast<DIType>(TyNode);
668
669 // DW_TAG_restrict_type is not supported in DWARF2
670 if (Ty->getTag() == dwarf::DW_TAG_restrict_type && DD->getDwarfVersion() <= 2)
672
673 // DW_TAG_atomic_type is not supported in DWARF < 5
674 if (Ty->getTag() == dwarf::DW_TAG_atomic_type && DD->getDwarfVersion() < 5)
676
677 // Construct the context before querying for the existence of the DIE in case
678 // such construction creates the DIE.
679 auto *Context = Ty->getScope();
680 DIE *ContextDIE = getOrCreateContextDIE(Context);
681 assert(ContextDIE);
682
683 if (DIE *TyDIE = getDIE(Ty))
684 return TyDIE;
685
686 return static_cast<DwarfUnit *>(ContextDIE->getUnit())
687 ->createTypeDIE(Context, *ContextDIE, Ty);
688}
689
691 const DIType *Ty, const DIE &TyDIE) {
692 if (Ty->getName().empty())
693 return;
694 if (Ty->isForwardDecl())
695 return;
696
697 // add temporary record for this type to be added later
698
699 unsigned Flags = 0;
700 if (auto *CT = dyn_cast<DICompositeType>(Ty)) {
701 // A runtime language of 0 actually means C/C++ and that any
702 // non-negative value is some version of Objective-C/C++.
703 if (CT->getRuntimeLang() == 0 || CT->isObjcClassComplete())
705 }
706
707 DD->addAccelType(*this, CUNode->getNameTableKind(), Ty->getName(), TyDIE,
708 Flags);
709
710 if (auto *CT = dyn_cast<DICompositeType>(Ty))
711 if (Ty->getName() != CT->getIdentifier() &&
712 CT->getRuntimeLang() == dwarf::DW_LANG_Swift)
713 DD->addAccelType(*this, CUNode->getNameTableKind(), CT->getIdentifier(),
714 TyDIE, Flags);
715
716 addGlobalType(Ty, TyDIE, Context);
717}
718
719void DwarfUnit::addGlobalType(const DIType *Ty, const DIE &TyDIE,
720 const DIScope *Context) {
721 if (!Context || isa<DICompileUnit>(Context) || isa<DIFile>(Context) ||
722 isa<DINamespace>(Context) || isa<DICommonBlock>(Context))
723 addGlobalTypeImpl(Ty, TyDIE, Context);
724}
725
726void DwarfUnit::addType(DIE &Entity, const DIType *Ty,
728 assert(Ty && "Trying to add a type that doesn't exist?");
730}
731
732// FIXME: change callsites to use the new DW_LNAME_ language codes.
734 const auto &Lang = getLanguage();
735
736 if (!Lang.hasVersionedName())
737 return static_cast<llvm::dwarf::SourceLanguage>(Lang.getName());
738
740 static_cast<llvm::dwarf::SourceLanguageName>(Lang.getName()),
741 Lang.getVersion())
743}
744
745std::string DwarfUnit::getParentContextString(const DIScope *Context) const {
746 if (!Context)
747 return "";
748
749 // FIXME: Decide whether to implement this for non-C++ languages.
751 return "";
752
753 std::string CS;
755 while (!isa<DICompileUnit>(Context)) {
756 Parents.push_back(Context);
757 if (const DIScope *S = Context->getScope())
758 Context = S;
759 else
760 // Structure, etc types will have a NULL context if they're at the top
761 // level.
762 break;
763 }
764
765 // Reverse iterate over our list to go from the outermost construct to the
766 // innermost.
767 for (const DIScope *Ctx : llvm::reverse(Parents)) {
768 StringRef Name = Ctx->getName();
769 if (Name.empty() && isa<DINamespace>(Ctx))
770 Name = "(anonymous namespace)";
771 if (!Name.empty()) {
772 CS += Name;
773 CS += "::";
774 }
775 }
776 return CS;
777}
778
779void DwarfUnit::constructTypeDIE(DIE &Buffer, const DIBasicType *BTy) {
780 // Get core information.
781 StringRef Name = BTy->getName();
782 // Add name if not anonymous or intermediate type.
783 if (!Name.empty())
784 addString(Buffer, dwarf::DW_AT_name, Name);
785
786 // An unspecified type only has a name attribute.
787 if (BTy->getTag() == dwarf::DW_TAG_unspecified_type)
788 return;
789
790 if (BTy->getTag() != dwarf::DW_TAG_string_type)
791 addUInt(Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
792 BTy->getEncoding());
793
794 uint64_t SizeInBytes = divideCeil(BTy->getSizeInBits(), 8);
795 addUInt(Buffer, dwarf::DW_AT_byte_size, std::nullopt, SizeInBytes);
796 if (BTy->getTag() == dwarf::Tag::DW_TAG_base_type) {
797 // DW_TAG_base_type:
798 // If the value of an object of the given type does not fully occupy the
799 // storage described by a byte size attribute, the base type entry may also
800 // have a DW_AT_bit_size [...] attribute.
801 // TODO: Do big endian targets need DW_AT_data_bit_offset? See discussion in
802 // pull request #164372.
803 if (uint64_t DataSizeInBits = BTy->getDataSizeInBits();
804 DataSizeInBits && DataSizeInBits != SizeInBytes * 8)
805 addUInt(Buffer, dwarf::DW_AT_bit_size, std::nullopt, DataSizeInBits);
806 }
807
808 if (BTy->isBigEndian())
809 addUInt(Buffer, dwarf::DW_AT_endianity, std::nullopt, dwarf::DW_END_big);
810 else if (BTy->isLittleEndian())
811 addUInt(Buffer, dwarf::DW_AT_endianity, std::nullopt, dwarf::DW_END_little);
812
813 if (uint32_t NumExtraInhabitants = BTy->getNumExtraInhabitants())
814 addUInt(Buffer, dwarf::DW_AT_LLVM_num_extra_inhabitants, std::nullopt,
815 NumExtraInhabitants);
816}
817
818void DwarfUnit::constructTypeDIE(DIE &Buffer, const DIFixedPointType *BTy) {
819 // Base type handling.
820 constructTypeDIE(Buffer, static_cast<const DIBasicType *>(BTy));
821
822 if (BTy->isBinary())
823 addSInt(Buffer, dwarf::DW_AT_binary_scale, dwarf::DW_FORM_sdata,
824 BTy->getFactor());
825 else if (BTy->isDecimal())
826 addSInt(Buffer, dwarf::DW_AT_decimal_scale, dwarf::DW_FORM_sdata,
827 BTy->getFactor());
828 else {
829 assert(BTy->isRational());
830 DIE *ContextDIE = getOrCreateContextDIE(BTy->getScope());
831 DIE &Constant = createAndAddDIE(dwarf::DW_TAG_constant, *ContextDIE);
832
833 addInt(Constant, dwarf::DW_AT_GNU_numerator, BTy->getNumerator(),
834 !BTy->isSigned());
835 addInt(Constant, dwarf::DW_AT_GNU_denominator, BTy->getDenominator(),
836 !BTy->isSigned());
837
838 addDIEEntry(Buffer, dwarf::DW_AT_small, Constant);
839 }
840}
841
842void DwarfUnit::constructTypeDIE(DIE &Buffer, const DIStringType *STy) {
843 // Get core information.
844 StringRef Name = STy->getName();
845 // Add name if not anonymous or intermediate type.
846 if (!Name.empty())
847 addString(Buffer, dwarf::DW_AT_name, Name);
848
849 if (DIVariable *Var = STy->getStringLength()) {
850 if (auto *VarDIE = getDIE(Var))
851 addDIEEntry(Buffer, dwarf::DW_AT_string_length, *VarDIE);
852 } else if (DIExpression *Expr = STy->getStringLengthExp()) {
853 addBlock(Buffer, dwarf::DW_AT_string_length, Expr);
854 } else {
855 uint64_t Size = STy->getSizeInBits() >> 3;
856 addUInt(Buffer, dwarf::DW_AT_byte_size, std::nullopt, Size);
857 }
858
859 if (DIExpression *Expr = STy->getStringLocationExp()) {
860 addBlock(Buffer, dwarf::DW_AT_data_location, Expr);
861 }
862
863 if (STy->getEncoding()) {
864 // For eventual Unicode support.
865 addUInt(Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
866 STy->getEncoding());
867 }
868}
869
870void DwarfUnit::constructTypeDIE(DIE &Buffer, const DIDerivedType *DTy) {
871 // Get core information.
872 StringRef Name = DTy->getName();
873 uint64_t Size = DTy->getSizeInBits() >> 3;
874 uint16_t Tag = Buffer.getTag();
875
876 // Map to main type, void will not have a type.
877 const DIType *FromTy = DTy->getBaseType();
878 if (FromTy)
879 addType(Buffer, FromTy);
880
881 // Add name if not anonymous or intermediate type.
882 if (!Name.empty())
883 addString(Buffer, dwarf::DW_AT_name, Name);
884
885 addAnnotation(Buffer, DTy->getAnnotations());
886
887 // If alignment is specified for a typedef , create and insert DW_AT_alignment
888 // attribute in DW_TAG_typedef DIE.
889 if (Tag == dwarf::DW_TAG_typedef && DD->getDwarfVersion() >= 5) {
890 uint32_t AlignInBytes = DTy->getAlignInBytes();
891 if (AlignInBytes > 0)
892 addUInt(Buffer, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata,
893 AlignInBytes);
894 }
895
896 // Add size if non-zero (derived types might be zero-sized.)
897 if (Size && Tag != dwarf::DW_TAG_pointer_type
898 && Tag != dwarf::DW_TAG_ptr_to_member_type
899 && Tag != dwarf::DW_TAG_reference_type
900 && Tag != dwarf::DW_TAG_rvalue_reference_type)
901 addUInt(Buffer, dwarf::DW_AT_byte_size, std::nullopt, Size);
902
903 if (Tag == dwarf::DW_TAG_ptr_to_member_type)
904 addDIEEntry(Buffer, dwarf::DW_AT_containing_type,
905 *getOrCreateTypeDIE(cast<DIDerivedType>(DTy)->getClassType()));
906
907 addAccess(Buffer, DTy->getFlags());
908
909 // Add source line info if available and TyDesc is not a forward declaration.
910 if (!DTy->isForwardDecl())
911 addSourceLine(Buffer, DTy);
912
913 // If DWARF address space value is other than None, add it. The IR
914 // verifier checks that DWARF address space only exists for pointer
915 // or reference types.
916 if (DTy->getDWARFAddressSpace())
917 addUInt(Buffer, dwarf::DW_AT_address_class, dwarf::DW_FORM_data4,
918 *DTy->getDWARFAddressSpace());
919
920 // Add template alias template parameters.
921 if (Tag == dwarf::DW_TAG_template_alias)
922 addTemplateParams(Buffer, DTy->getTemplateParams());
923
924 if (auto PtrAuthData = DTy->getPtrAuthData()) {
925 addUInt(Buffer, dwarf::DW_AT_LLVM_ptrauth_key, dwarf::DW_FORM_data1,
926 PtrAuthData->key());
927 if (PtrAuthData->isAddressDiscriminated())
928 addFlag(Buffer, dwarf::DW_AT_LLVM_ptrauth_address_discriminated);
929 addUInt(Buffer, dwarf::DW_AT_LLVM_ptrauth_extra_discriminator,
930 dwarf::DW_FORM_data2, PtrAuthData->extraDiscriminator());
931 if (PtrAuthData->isaPointer())
932 addFlag(Buffer, dwarf::DW_AT_LLVM_ptrauth_isa_pointer);
933 if (PtrAuthData->authenticatesNullValues())
934 addFlag(Buffer, dwarf::DW_AT_LLVM_ptrauth_authenticates_null_values);
935 }
936}
937
938std::optional<unsigned>
940 // Args[0] is the return type.
941 std::optional<unsigned> ObjectPointerIndex;
942 for (unsigned i = 1, N = Args.size(); i < N; ++i) {
943 const DIType *Ty = Args[i];
944 if (!Ty) {
945 assert(i == N-1 && "Unspecified parameter must be the last argument");
946 createAndAddDIE(dwarf::DW_TAG_unspecified_parameters, Buffer);
947 } else {
948 DIE &Arg = createAndAddDIE(dwarf::DW_TAG_formal_parameter, Buffer);
949 addType(Arg, Ty);
950 if (Ty->isArtificial())
951 addFlag(Arg, dwarf::DW_AT_artificial);
952
953 if (Ty->isObjectPointer()) {
954 assert(!ObjectPointerIndex &&
955 "Can't have more than one object pointer");
956 ObjectPointerIndex = i;
957 }
958 }
959 }
960
961 return ObjectPointerIndex;
962}
963
964void DwarfUnit::constructTypeDIE(DIE &Buffer, const DISubroutineType *CTy) {
965 // Add return type. A void return won't have a type.
966 auto Elements = cast<DISubroutineType>(CTy)->getTypeArray();
967 if (Elements.size())
968 if (auto RTy = Elements[0])
969 addType(Buffer, RTy);
970
971 bool isPrototyped = true;
972 if (Elements.size() == 2 && !Elements[1])
973 isPrototyped = false;
974
975 constructSubprogramArguments(Buffer, Elements);
976
977 // Add prototype flag if we're dealing with a C language and the function has
978 // been prototyped.
979 if (isPrototyped && dwarf::isC(getSourceLanguage()))
980 addFlag(Buffer, dwarf::DW_AT_prototyped);
981
982 // Add a DW_AT_calling_convention if this has an explicit convention.
983 if (CTy->getCC() && CTy->getCC() != dwarf::DW_CC_normal)
984 addUInt(Buffer, dwarf::DW_AT_calling_convention, dwarf::DW_FORM_data1,
985 CTy->getCC());
986
987 if (CTy->isLValueReference())
988 addFlag(Buffer, dwarf::DW_AT_reference);
989
990 if (CTy->isRValueReference())
991 addFlag(Buffer, dwarf::DW_AT_rvalue_reference);
992}
993
994void DwarfUnit::addAnnotation(DIE &Buffer, DINodeArray Annotations) {
995 if (!Annotations)
996 return;
997
998 for (const Metadata *Annotation : Annotations->operands()) {
999 const MDNode *MD = cast<MDNode>(Annotation);
1000 const MDString *Name = cast<MDString>(MD->getOperand(0));
1001 const auto &Value = MD->getOperand(1);
1002
1003 DIE &AnnotationDie = createAndAddDIE(dwarf::DW_TAG_LLVM_annotation, Buffer);
1004 addString(AnnotationDie, dwarf::DW_AT_name, Name->getString());
1005 if (const auto *Data = dyn_cast<MDString>(Value))
1006 addString(AnnotationDie, dwarf::DW_AT_const_value, Data->getString());
1007 else if (const auto *Data = dyn_cast<ConstantAsMetadata>(Value))
1008 addConstantValue(AnnotationDie, Data->getValue()->getUniqueInteger(),
1009 /*Unsigned=*/true);
1010 else
1011 assert(false && "Unsupported annotation value type");
1012 }
1013}
1014
1015void DwarfUnit::addDiscriminant(DIE &Variant, Constant *Discriminant,
1016 bool IsUnsigned) {
1017 if (const auto *CI = dyn_cast_or_null<ConstantInt>(Discriminant)) {
1018 addInt(Variant, dwarf::DW_AT_discr_value, CI->getValue(), IsUnsigned);
1019 } else if (const auto *CA =
1021 // Must have an even number of operands.
1022 unsigned NElems = CA->getNumElements();
1023 if (NElems % 2 != 0) {
1024 return;
1025 }
1026
1027 DIEBlock *Block = new (DIEValueAllocator) DIEBlock;
1028
1029 auto AddInt = [&](const APInt &Val) {
1030 if (IsUnsigned)
1031 addUInt(*Block, dwarf::DW_FORM_udata, Val.getZExtValue());
1032 else
1033 addSInt(*Block, dwarf::DW_FORM_sdata, Val.getSExtValue());
1034 };
1035
1036 for (unsigned I = 0; I < NElems; I += 2) {
1037 APInt LV = CA->getElementAsAPInt(I);
1038 APInt HV = CA->getElementAsAPInt(I + 1);
1039 if (LV == HV) {
1040 addUInt(*Block, dwarf::DW_FORM_data1, dwarf::DW_DSC_label);
1041 AddInt(LV);
1042 } else {
1043 addUInt(*Block, dwarf::DW_FORM_data1, dwarf::DW_DSC_range);
1044 AddInt(LV);
1045 AddInt(HV);
1046 }
1047 }
1048 addBlock(Variant, dwarf::DW_AT_discr_list, Block);
1049 }
1050}
1051
1053 // Add name if not anonymous or intermediate type.
1054 StringRef Name = CTy->getName();
1055
1056 uint16_t Tag = Buffer.getTag();
1057
1058 switch (Tag) {
1059 case dwarf::DW_TAG_array_type:
1060 constructArrayTypeDIE(Buffer, CTy);
1061 break;
1062 case dwarf::DW_TAG_enumeration_type:
1063 constructEnumTypeDIE(Buffer, CTy);
1064 break;
1065 case dwarf::DW_TAG_variant_part:
1066 case dwarf::DW_TAG_variant:
1067 case dwarf::DW_TAG_structure_type:
1068 case dwarf::DW_TAG_union_type:
1069 case dwarf::DW_TAG_class_type:
1070 case dwarf::DW_TAG_namelist: {
1071 // Emit the discriminator for a variant part.
1072 DIDerivedType *Discriminator = nullptr;
1073 if (Tag == dwarf::DW_TAG_variant_part) {
1074 Discriminator = CTy->getDiscriminator();
1075 if (Discriminator) {
1076 // DWARF says:
1077 // If the variant part has a discriminant, the discriminant is
1078 // represented by a separate debugging information entry which is
1079 // a child of the variant part entry.
1080 // However, for a language like Ada, this yields a weird
1081 // result: a discriminant field would have to be emitted
1082 // multiple times, once per variant part. Instead, this DWARF
1083 // restriction was lifted for DWARF 6 (see
1084 // https://dwarfstd.org/issues/180123.1.html) and so we allow
1085 // this here.
1086 DIE *DiscDIE = getDIE(Discriminator);
1087 if (DiscDIE == nullptr) {
1088 DiscDIE = &constructMemberDIE(Buffer, Discriminator);
1089 }
1090 addDIEEntry(Buffer, dwarf::DW_AT_discr, *DiscDIE);
1091 }
1092 }
1093
1094 // Add template parameters to a class, structure or union types.
1095 if (Tag == dwarf::DW_TAG_class_type ||
1096 Tag == dwarf::DW_TAG_structure_type ||
1097 Tag == dwarf::DW_TAG_union_type) {
1098 if (!(DD->useSplitDwarf() && !getCU().getSkeleton()) ||
1099 CTy->isNameSimplified())
1100 addTemplateParams(Buffer, CTy->getTemplateParams());
1101 }
1102
1103 // Add elements to structure type.
1104 DINodeArray Elements = CTy->getElements();
1105 for (const auto *Element : Elements) {
1106 if (!Element)
1107 continue;
1108 if (auto *SP = dyn_cast<DISubprogram>(Element))
1109 getOrCreateSubprogramDIE(SP, nullptr);
1110 else if (auto *DDTy = dyn_cast<DIDerivedType>(Element)) {
1111 if (DDTy->getTag() == dwarf::DW_TAG_friend) {
1112 DIE &ElemDie = createAndAddDIE(dwarf::DW_TAG_friend, Buffer);
1113 addType(ElemDie, DDTy->getBaseType(), dwarf::DW_AT_friend);
1114 } else if (DDTy->isStaticMember()) {
1116 } else if (Tag == dwarf::DW_TAG_variant_part) {
1117 // When emitting a variant part, wrap each member in
1118 // DW_TAG_variant.
1119 DIE &Variant = createAndAddDIE(dwarf::DW_TAG_variant, Buffer);
1120 if (Constant *CI = DDTy->getDiscriminantValue()) {
1121 addDiscriminant(Variant, CI,
1122 DD->isUnsignedDIType(Discriminator->getBaseType()));
1123 }
1124 // If the variant holds a composite type with tag
1125 // DW_TAG_variant, inline those members into the variant
1126 // DIE.
1127 if (auto *Composite =
1128 dyn_cast_or_null<DICompositeType>(DDTy->getBaseType());
1129 Composite != nullptr &&
1130 Composite->getTag() == dwarf::DW_TAG_variant) {
1131 constructTypeDIE(Variant, Composite);
1132 } else {
1133 constructMemberDIE(Variant, DDTy);
1134 }
1135 } else {
1136 constructMemberDIE(Buffer, DDTy);
1137 }
1138 } else if (auto *Property = dyn_cast<DIObjCProperty>(Element)) {
1139 DIE &ElemDie = createAndAddDIE(Property->getTag(), Buffer, Property);
1140 StringRef PropertyName = Property->getName();
1141 addString(ElemDie, dwarf::DW_AT_APPLE_property_name, PropertyName);
1142 if (Property->getType())
1143 addType(ElemDie, Property->getType());
1144 addSourceLine(ElemDie, Property);
1145 StringRef GetterName = Property->getGetterName();
1146 if (!GetterName.empty())
1147 addString(ElemDie, dwarf::DW_AT_APPLE_property_getter, GetterName);
1148 StringRef SetterName = Property->getSetterName();
1149 if (!SetterName.empty())
1150 addString(ElemDie, dwarf::DW_AT_APPLE_property_setter, SetterName);
1151 if (unsigned PropertyAttributes = Property->getAttributes())
1152 addUInt(ElemDie, dwarf::DW_AT_APPLE_property_attribute, std::nullopt,
1153 PropertyAttributes);
1154 } else if (auto *Property = dyn_cast<DIProperty>(Element)) {
1155 constructPropertyDIE(Buffer, Property);
1156 } else if (auto *Composite = dyn_cast<DICompositeType>(Element)) {
1157 if (Composite->getTag() == dwarf::DW_TAG_variant_part) {
1158 DIE &VariantPart = createAndAddDIE(Composite->getTag(), Buffer);
1159 constructTypeDIE(VariantPart, Composite);
1160 }
1161 } else if (Tag == dwarf::DW_TAG_namelist) {
1162 auto *VarDIE = getDIE(Element);
1163 if (VarDIE) {
1164 DIE &ItemDie = createAndAddDIE(dwarf::DW_TAG_namelist_item, Buffer);
1165 addDIEEntry(ItemDie, dwarf::DW_AT_namelist_item, *VarDIE);
1166 }
1167 }
1168 }
1169
1170 if (CTy->isAppleBlockExtension())
1171 addFlag(Buffer, dwarf::DW_AT_APPLE_block);
1172
1173 if (CTy->getExportSymbols())
1174 addFlag(Buffer, dwarf::DW_AT_export_symbols);
1175
1176 // This is outside the DWARF spec, but GDB expects a DW_AT_containing_type
1177 // inside C++ composite types to point to the base class with the vtable.
1178 // Rust uses DW_AT_containing_type to link a vtable to the type
1179 // for which it was created.
1180 if (auto *ContainingType = CTy->getVTableHolder())
1181 addDIEEntry(Buffer, dwarf::DW_AT_containing_type,
1182 *getOrCreateTypeDIE(ContainingType));
1183
1184 if (CTy->isObjcClassComplete())
1185 addFlag(Buffer, dwarf::DW_AT_APPLE_objc_complete_type);
1186
1187 // Add the type's non-standard calling convention.
1188 // DW_CC_pass_by_value/DW_CC_pass_by_reference are introduced in DWARF 5.
1189 if (!Asm->TM.Options.DebugStrictDwarf || DD->getDwarfVersion() >= 5) {
1190 uint8_t CC = 0;
1191 if (CTy->isTypePassByValue())
1192 CC = dwarf::DW_CC_pass_by_value;
1193 else if (CTy->isTypePassByReference())
1194 CC = dwarf::DW_CC_pass_by_reference;
1195 if (CC)
1196 addUInt(Buffer, dwarf::DW_AT_calling_convention, dwarf::DW_FORM_data1,
1197 CC);
1198 }
1199
1200 if (auto *SpecifiedFrom = CTy->getSpecification())
1201 addDIEEntry(Buffer, dwarf::DW_AT_specification,
1202 *getOrCreateContextDIE(SpecifiedFrom));
1203
1204 break;
1205 }
1206 default:
1207 break;
1208 }
1209
1210 // Add name if not anonymous or intermediate type.
1211 if (!Name.empty())
1212 addString(Buffer, dwarf::DW_AT_name, Name);
1213
1214 // For Swift, mangled names are put into DW_AT_linkage_name.
1215 if (CTy->getRuntimeLang() == dwarf::DW_LANG_Swift && CTy->getRawIdentifier())
1216 addString(Buffer, dwarf::DW_AT_linkage_name, CTy->getIdentifier());
1217
1218 addAnnotation(Buffer, CTy->getAnnotations());
1219
1220 if (Tag == dwarf::DW_TAG_enumeration_type ||
1221 Tag == dwarf::DW_TAG_class_type || Tag == dwarf::DW_TAG_structure_type ||
1222 Tag == dwarf::DW_TAG_union_type) {
1223 if (auto *Var = dyn_cast_or_null<DIVariable>(CTy->getRawSizeInBits())) {
1224 if (auto *VarDIE = getDIE(Var))
1225 addDIEEntry(Buffer, dwarf::DW_AT_bit_size, *VarDIE);
1226 } else if (auto *Exp =
1228 addBlock(Buffer, dwarf::DW_AT_bit_size, Exp);
1229 } else {
1230 uint64_t Size = CTy->getSizeInBits() >> 3;
1231 // Add size if non-zero (derived types might be zero-sized.)
1232 // Ignore the size if it's a non-enum forward decl.
1233 // TODO: Do we care about size for enum forward declarations?
1234 if (Size &&
1235 (!CTy->isForwardDecl() || Tag == dwarf::DW_TAG_enumeration_type))
1236 addUInt(Buffer, dwarf::DW_AT_byte_size, std::nullopt, Size);
1237 else if (!CTy->isForwardDecl())
1238 // Add zero size if it is not a forward declaration.
1239 addUInt(Buffer, dwarf::DW_AT_byte_size, std::nullopt, 0);
1240 }
1241
1242 // If we're a forward decl, say so.
1243 if (CTy->isForwardDecl())
1244 addFlag(Buffer, dwarf::DW_AT_declaration);
1245
1246 // Add accessibility info if available.
1247 addAccess(Buffer, CTy->getFlags());
1248
1249 // Add source line info if available.
1250 if (!CTy->isForwardDecl())
1251 addSourceLine(Buffer, CTy);
1252
1253 // No harm in adding the runtime language to the declaration.
1254 unsigned RLang = CTy->getRuntimeLang();
1255 if (RLang)
1256 addUInt(Buffer, dwarf::DW_AT_APPLE_runtime_class, dwarf::DW_FORM_data1,
1257 RLang);
1258
1259 // Add align info if available.
1260 if (uint32_t AlignInBytes = CTy->getAlignInBytes())
1261 addUInt(Buffer, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata,
1262 AlignInBytes);
1263
1264 if (uint32_t NumExtraInhabitants = CTy->getNumExtraInhabitants())
1265 addUInt(Buffer, dwarf::DW_AT_LLVM_num_extra_inhabitants, std::nullopt,
1266 NumExtraInhabitants);
1267 }
1268}
1269
1270void DwarfUnit::constructTemplateTypeParameterDIE(
1271 DIE &Buffer, const DITemplateTypeParameter *TP) {
1272 DIE &ParamDIE =
1273 createAndAddDIE(dwarf::DW_TAG_template_type_parameter, Buffer);
1274 // Add the type if it exists, it could be void and therefore no type.
1275 if (TP->getType())
1276 addType(ParamDIE, TP->getType());
1277 if (!TP->getName().empty())
1278 addString(ParamDIE, dwarf::DW_AT_name, TP->getName());
1279 if (TP->isDefault() && isCompatibleWithVersion(5))
1280 addFlag(ParamDIE, dwarf::DW_AT_default_value);
1281}
1282
1283void DwarfUnit::constructTemplateValueParameterDIE(
1284 DIE &Buffer, const DITemplateValueParameter *VP) {
1285 DIE &ParamDIE = createAndAddDIE(VP->getTag(), Buffer);
1286
1287 // Add the type if there is one, template template and template parameter
1288 // packs will not have a type.
1289 if (VP->getTag() == dwarf::DW_TAG_template_value_parameter)
1290 addType(ParamDIE, VP->getType());
1291 if (!VP->getName().empty())
1292 addString(ParamDIE, dwarf::DW_AT_name, VP->getName());
1293 if (VP->isDefault() && isCompatibleWithVersion(5))
1294 addFlag(ParamDIE, dwarf::DW_AT_default_value);
1295 if (Metadata *Val = VP->getValue()) {
1297 addConstantValue(ParamDIE, CI, VP->getType());
1298 else if (ConstantFP *CF = mdconst::dyn_extract<ConstantFP>(Val))
1299 addConstantFPValue(ParamDIE, CF);
1300 else if (GlobalValue *GV = mdconst::dyn_extract<GlobalValue>(Val)) {
1301 // We cannot describe the location of dllimport'd entities: the
1302 // computation of their address requires loads from the IAT.
1303 if (!GV->hasDLLImportStorageClass()) {
1304 // For declaration non-type template parameters (such as global values
1305 // and functions)
1307 addOpAddress(*Loc, Asm->getSymbol(GV));
1308 // Emit DW_OP_stack_value to use the address as the immediate value of
1309 // the parameter, rather than a pointer to it.
1310 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_stack_value);
1311 addBlock(ParamDIE, dwarf::DW_AT_location, Loc);
1312 }
1313 } else if (VP->getTag() == dwarf::DW_TAG_GNU_template_template_param) {
1314 assert(isa<MDString>(Val));
1315 addString(ParamDIE, dwarf::DW_AT_GNU_template_name,
1316 cast<MDString>(Val)->getString());
1317 } else if (VP->getTag() == dwarf::DW_TAG_GNU_template_parameter_pack) {
1318 addTemplateParams(ParamDIE, cast<MDTuple>(Val));
1319 }
1320 }
1321}
1322
1324 // Construct the context before querying for the existence of the DIE in case
1325 // such construction creates the DIE.
1326 DIE *ContextDIE = getOrCreateContextDIE(NS->getScope());
1327
1328 if (DIE *NDie = getDIE(NS))
1329 return NDie;
1330 DIE &NDie = createAndAddDIE(dwarf::DW_TAG_namespace, *ContextDIE, NS);
1331
1332 StringRef Name = NS->getName();
1333 if (!Name.empty())
1334 addString(NDie, dwarf::DW_AT_name, NS->getName());
1335 else
1336 Name = "(anonymous namespace)";
1337 DD->addAccelNamespace(*this, CUNode->getNameTableKind(), Name, NDie);
1338 addGlobalName(Name, NDie, NS->getScope());
1339 if (NS->getExportSymbols())
1340 addFlag(NDie, dwarf::DW_AT_export_symbols);
1341 return &NDie;
1342}
1343
1345 // Construct the context before querying for the existence of the DIE in case
1346 // such construction creates the DIE.
1347 DIE *ContextDIE = getOrCreateContextDIE(M->getScope());
1348
1349 if (DIE *MDie = getDIE(M))
1350 return MDie;
1351 DIE &MDie = createAndAddDIE(dwarf::DW_TAG_module, *ContextDIE, M);
1352
1353 if (!M->getName().empty()) {
1354 addString(MDie, dwarf::DW_AT_name, M->getName());
1355 addGlobalName(M->getName(), MDie, M->getScope());
1356 }
1357 if (!M->getConfigurationMacros().empty())
1358 addString(MDie, dwarf::DW_AT_LLVM_config_macros,
1359 M->getConfigurationMacros());
1360 if (!M->getIncludePath().empty())
1361 addString(MDie, dwarf::DW_AT_LLVM_include_path, M->getIncludePath());
1362 if (!M->getAPINotesFile().empty())
1363 addString(MDie, dwarf::DW_AT_LLVM_apinotes, M->getAPINotesFile());
1364 if (M->getFile())
1365 addUInt(MDie, dwarf::DW_AT_decl_file, std::nullopt,
1366 getOrCreateSourceID(M->getFile()));
1367 if (M->getLineNo())
1368 addUInt(MDie, dwarf::DW_AT_decl_line, std::nullopt, M->getLineNo());
1369 if (M->getIsDecl())
1370 addFlag(MDie, dwarf::DW_AT_declaration);
1371
1372 return &MDie;
1373}
1374
1376 const Function *FnHint, bool Minimal) {
1377 // Construct the context before querying for the existence of the DIE in case
1378 // such construction creates the DIE (as is the case for member function
1379 // declarations).
1380 DIE *ContextDIE =
1382
1383 if (DIE *SPDie = getDIE(SP))
1384 return SPDie;
1385
1386 if (auto *SPDecl = SP->getDeclaration()) {
1387 if (!Minimal) {
1388 // Build the decl now to ensure it precedes the definition.
1389 getOrCreateSubprogramDIE(SPDecl, nullptr);
1390 // Check whether the DIE for SP has already been created after the call
1391 // above.
1392 // FIXME: Should the creation of definition subprogram DIE during
1393 // the creation of declaration subprogram DIE be allowed?
1394 // See https://github.com/llvm/llvm-project/pull/154636.
1395 if (DIE *SPDie = getDIE(SP))
1396 return SPDie;
1397 }
1398 }
1399
1400 // DW_TAG_inlined_subroutine may refer to this DIE.
1401 DIE &SPDie = createAndAddDIE(dwarf::DW_TAG_subprogram, *ContextDIE, SP);
1402
1403 // Stop here and fill this in later, depending on whether or not this
1404 // subprogram turns out to have inlined instances or not.
1405 if (SP->isDefinition())
1406 return &SPDie;
1407
1408 static_cast<DwarfUnit *>(SPDie.getUnit())
1409 ->applySubprogramAttributes(SP, SPDie);
1410 return &SPDie;
1411}
1412
1414 DIE &SPDie, bool Minimal) {
1415 DIE *DeclDie = nullptr;
1416 StringRef DeclLinkageName;
1417 if (auto *SPDecl = SP->getDeclaration()) {
1418 if (!Minimal) {
1419 DITypeArray DeclArgs, DefinitionArgs;
1420 DeclArgs = SPDecl->getType()->getTypeArray();
1421 DefinitionArgs = SP->getType()->getTypeArray();
1422
1423 if (DeclArgs.size() && DefinitionArgs.size())
1424 if (DefinitionArgs[0] != nullptr && DeclArgs[0] != DefinitionArgs[0])
1425 addType(SPDie, DefinitionArgs[0]);
1426
1427 DeclDie = getDIE(SPDecl);
1428 assert(DeclDie && "This DIE should've already been constructed when the "
1429 "definition DIE was created in "
1430 "getOrCreateSubprogramDIE");
1431 // Look at the Decl's linkage name only if we emitted it.
1432 if (DD->useAllLinkageNames())
1433 DeclLinkageName = SPDecl->getLinkageName();
1434 unsigned DeclID = getOrCreateSourceID(SPDecl->getFile());
1435 unsigned DefID = getOrCreateSourceID(SP->getFile());
1436 if (DeclID != DefID)
1437 addUInt(SPDie, dwarf::DW_AT_decl_file, std::nullopt, DefID);
1438
1439 if (SP->getLine() != SPDecl->getLine())
1440 addUInt(SPDie, dwarf::DW_AT_decl_line, std::nullopt, SP->getLine());
1441 }
1442 }
1443
1444 // Add function template parameters.
1445 if (!Minimal || SP->isNameSimplified())
1446 addTemplateParams(SPDie, SP->getTemplateParams());
1447
1448 // Add the linkage name if we have one and it isn't in the Decl.
1449 StringRef LinkageName = SP->getLinkageName();
1450 // Always emit linkage name for abstract subprograms.
1451 if (DeclLinkageName != LinkageName &&
1452 (DD->useAllLinkageNames() || DU->getAbstractScopeDIEs().lookup(SP)))
1454
1455 if (!DeclDie)
1456 return false;
1457
1458 // Refer to the function declaration where all the other attributes will be
1459 // found.
1460 addDIEEntry(SPDie, dwarf::DW_AT_specification, *DeclDie);
1461 return true;
1462}
1463
1465 bool SkipSPAttributes) {
1466 // If -fdebug-info-for-profiling is enabled, need to emit the subprogram
1467 // and its source location.
1468 bool SkipSPSourceLocation = SkipSPAttributes &&
1469 !CUNode->getDebugInfoForProfiling();
1470 if (!SkipSPSourceLocation)
1471 if (applySubprogramDefinitionAttributes(SP, SPDie, SkipSPAttributes))
1472 return;
1473
1474 // Constructors and operators for anonymous aggregates do not have names.
1475 if (!SP->getName().empty())
1476 addString(SPDie, dwarf::DW_AT_name, SP->getName());
1477
1478 addAnnotation(SPDie, SP->getAnnotations());
1479
1480 if (!SkipSPSourceLocation)
1481 addSourceLine(SPDie, SP);
1482
1483 // Skip the rest of the attributes under -gmlt to save space.
1484 if (SkipSPAttributes)
1485 return;
1486
1487 // Add the prototype if we have a prototype and we have a C like
1488 // language.
1489 if (SP->isPrototyped() && dwarf::isC(getSourceLanguage()))
1490 addFlag(SPDie, dwarf::DW_AT_prototyped);
1491
1492 if (SP->isObjCDirect())
1493 addFlag(SPDie, dwarf::DW_AT_APPLE_objc_direct);
1494
1495 unsigned CC = 0;
1496 DITypeArray Args;
1497 if (const DISubroutineType *SPTy = SP->getType()) {
1498 Args = SPTy->getTypeArray();
1499 CC = SPTy->getCC();
1500 }
1501
1502 // Add a DW_AT_calling_convention if this has an explicit convention.
1503 if (CC && CC != dwarf::DW_CC_normal)
1504 addUInt(SPDie, dwarf::DW_AT_calling_convention, dwarf::DW_FORM_data1, CC);
1505
1506 // Add a return type. If this is a type like a C/C++ void type we don't add a
1507 // return type.
1508 if (Args.size())
1509 if (auto Ty = Args[0])
1510 addType(SPDie, Ty);
1511
1512 unsigned VK = SP->getVirtuality();
1513 if (VK) {
1514 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, VK);
1515 if (SP->getVirtualIndex() != -1u) {
1516 DIELoc *Block = getDIELoc();
1517 addUInt(*Block, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1518 addUInt(*Block, dwarf::DW_FORM_udata, SP->getVirtualIndex());
1519 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, Block);
1520 }
1521 ContainingTypeMap.insert(std::make_pair(&SPDie, SP->getContainingType()));
1522 }
1523
1524 if (!SP->isDefinition()) {
1525 addFlag(SPDie, dwarf::DW_AT_declaration);
1526
1527 // Add arguments. Do not add arguments for subprogram definition. They will
1528 // be handled while processing variables.
1529 //
1530 // Encode the object pointer as an index instead of a DIE reference in order
1531 // to minimize the affect on the .debug_info size.
1532 if (std::optional<unsigned> ObjectPointerIndex =
1533 constructSubprogramArguments(SPDie, Args)) {
1534 if (getDwarfDebug().tuneForLLDB() &&
1535 getDwarfDebug().getDwarfVersion() >= 5) {
1536 // 0th index in Args is the return type, hence adjust by 1. In DWARF
1537 // we want the first parameter to be at index 0.
1538 assert(*ObjectPointerIndex > 0);
1539 addSInt(SPDie, dwarf::DW_AT_object_pointer,
1540 dwarf::DW_FORM_implicit_const, *ObjectPointerIndex - 1);
1541 }
1542 }
1543 }
1544
1545 addThrownTypes(SPDie, SP->getThrownTypes());
1546
1547 if (SP->isArtificial())
1548 addFlag(SPDie, dwarf::DW_AT_artificial);
1549
1550 if (!SP->isLocalToUnit())
1551 addFlag(SPDie, dwarf::DW_AT_external);
1552
1553 if (SP->isOptimized())
1554 addFlag(SPDie, dwarf::DW_AT_APPLE_optimized);
1555
1556 if (DD->useAppleExtensionAttributes())
1557 if (unsigned isa = Asm->getISAEncoding())
1558 addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa);
1559
1560 if (SP->isLValueReference())
1561 addFlag(SPDie, dwarf::DW_AT_reference);
1562
1563 if (SP->isRValueReference())
1564 addFlag(SPDie, dwarf::DW_AT_rvalue_reference);
1565
1566 if (SP->isNoReturn())
1567 addFlag(SPDie, dwarf::DW_AT_noreturn);
1568
1569 addAccess(SPDie, SP->getFlags());
1570
1571 if (SP->isExplicit())
1572 addFlag(SPDie, dwarf::DW_AT_explicit);
1573
1574 if (SP->isMainSubprogram())
1575 addFlag(SPDie, dwarf::DW_AT_main_subprogram);
1576 if (SP->isPure())
1577 addFlag(SPDie, dwarf::DW_AT_pure);
1578 if (SP->isElemental())
1579 addFlag(SPDie, dwarf::DW_AT_elemental);
1580 if (SP->isRecursive())
1581 addFlag(SPDie, dwarf::DW_AT_recursive);
1582
1583 if (!SP->getTargetFuncName().empty())
1584 addString(SPDie, dwarf::DW_AT_trampoline, SP->getTargetFuncName());
1585
1586 if (DD->getDwarfVersion() >= 5 && SP->isDeleted())
1587 addFlag(SPDie, dwarf::DW_AT_deleted);
1588}
1589
1590void DwarfUnit::constructSubrangeDIE(DIE &DW_Subrange, const DISubrangeType *SR,
1591 bool ForArray) {
1592 StringRef Name = SR->getName();
1593 if (!Name.empty())
1594 addString(DW_Subrange, dwarf::DW_AT_name, Name);
1595
1596 if (SR->getBaseType())
1597 addType(DW_Subrange, SR->getBaseType());
1598
1599 addSourceLine(DW_Subrange, SR);
1600
1601 if (uint64_t Size = SR->getSizeInBits())
1602 addUInt(DW_Subrange, dwarf::DW_AT_byte_size, std::nullopt, Size >> 3);
1603 if (uint32_t AlignInBytes = SR->getAlignInBytes())
1604 addUInt(DW_Subrange, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata,
1605 AlignInBytes);
1606
1607 if (SR->isBigEndian())
1608 addUInt(DW_Subrange, dwarf::DW_AT_endianity, std::nullopt,
1609 dwarf::DW_END_big);
1610 else if (SR->isLittleEndian())
1611 addUInt(DW_Subrange, dwarf::DW_AT_endianity, std::nullopt,
1612 dwarf::DW_END_little);
1613
1614 // The LowerBound value defines the lower bounds which is typically
1615 // zero for C/C++. Values are 64 bit.
1616 int64_t DefaultLowerBound = getDefaultLowerBound();
1617
1618 auto AddBoundTypeEntry = [&](dwarf::Attribute Attr,
1619 DISubrangeType::BoundType Bound) -> void {
1620 if (auto *BV = dyn_cast_if_present<DIVariable *>(Bound)) {
1621 if (auto *VarDIE = getDIE(BV))
1622 addDIEEntry(DW_Subrange, Attr, *VarDIE);
1623 } else if (auto *DT = dyn_cast_if_present<DIDerivedType *>(Bound)) {
1624 if (auto *DTDIE = getDIE(DT))
1625 addDIEEntry(DW_Subrange, Attr, *DTDIE);
1626 } else if (auto *BE = dyn_cast_if_present<DIExpression *>(Bound)) {
1627 addBlock(DW_Subrange, Attr, BE);
1628 } else if (auto *BI = dyn_cast_if_present<ConstantInt *>(Bound)) {
1629 if (Attr == dwarf::DW_AT_GNU_bias) {
1630 if (BI->getSExtValue() != 0)
1631 addUInt(DW_Subrange, Attr, dwarf::DW_FORM_sdata, BI->getSExtValue());
1632 } else if (Attr != dwarf::DW_AT_lower_bound || DefaultLowerBound == -1 ||
1633 BI->getSExtValue() != DefaultLowerBound || !ForArray)
1634 addSInt(DW_Subrange, Attr, dwarf::DW_FORM_sdata, BI->getSExtValue());
1635 }
1636 };
1637
1638 AddBoundTypeEntry(dwarf::DW_AT_lower_bound, SR->getLowerBound());
1639
1640 AddBoundTypeEntry(dwarf::DW_AT_upper_bound, SR->getUpperBound());
1641
1642 AddBoundTypeEntry(dwarf::DW_AT_bit_stride, SR->getStride());
1643
1644 AddBoundTypeEntry(dwarf::DW_AT_GNU_bias, SR->getBias());
1645}
1646
1647void DwarfUnit::constructSubrangeDIE(DIE &Buffer, const DISubrange *SR) {
1648 DIE &DW_Subrange = createAndAddDIE(dwarf::DW_TAG_subrange_type, Buffer);
1649
1650 DIE *IdxTy = getIndexTyDie();
1651 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, *IdxTy);
1652
1653 // The LowerBound value defines the lower bounds which is typically zero for
1654 // C/C++. The Count value is the number of elements. Values are 64 bit. If
1655 // Count == -1 then the array is unbounded and we do not emit
1656 // DW_AT_lower_bound and DW_AT_count attributes.
1657 int64_t DefaultLowerBound = getDefaultLowerBound();
1658
1659 auto AddBoundTypeEntry = [&](dwarf::Attribute Attr,
1660 DISubrange::BoundType Bound) -> void {
1661 if (auto *BV = dyn_cast_if_present<DIVariable *>(Bound)) {
1662 if (auto *VarDIE = getDIE(BV))
1663 addDIEEntry(DW_Subrange, Attr, *VarDIE);
1664 } else if (auto *BE = dyn_cast_if_present<DIExpression *>(Bound)) {
1665 addBlock(DW_Subrange, Attr, BE);
1666 } else if (auto *BI = dyn_cast_if_present<ConstantInt *>(Bound)) {
1667 if (Attr == dwarf::DW_AT_count) {
1668 if (BI->getSExtValue() != -1)
1669 addUInt(DW_Subrange, Attr, std::nullopt, BI->getSExtValue());
1670 } else if (Attr != dwarf::DW_AT_lower_bound || DefaultLowerBound == -1 ||
1671 BI->getSExtValue() != DefaultLowerBound)
1672 addSInt(DW_Subrange, Attr, dwarf::DW_FORM_sdata, BI->getSExtValue());
1673 }
1674 };
1675
1676 AddBoundTypeEntry(dwarf::DW_AT_lower_bound, SR->getLowerBound());
1677
1678 AddBoundTypeEntry(dwarf::DW_AT_count, SR->getCount());
1679
1680 AddBoundTypeEntry(dwarf::DW_AT_upper_bound, SR->getUpperBound());
1681
1682 AddBoundTypeEntry(dwarf::DW_AT_byte_stride, SR->getStride());
1683}
1684
1685void DwarfUnit::constructGenericSubrangeDIE(DIE &Buffer,
1686 const DIGenericSubrange *GSR) {
1687 DIE &DwGenericSubrange =
1688 createAndAddDIE(dwarf::DW_TAG_generic_subrange, Buffer);
1689 // Get an anonymous type for index type.
1690 // FIXME: This type should be passed down from the front end
1691 // as different languages may have different sizes for indexes.
1692 DIE *IdxTy = getIndexTyDie();
1693 addDIEEntry(DwGenericSubrange, dwarf::DW_AT_type, *IdxTy);
1694
1695 int64_t DefaultLowerBound = getDefaultLowerBound();
1696
1697 auto AddBoundTypeEntry = [&](dwarf::Attribute Attr,
1698 DIGenericSubrange::BoundType Bound) -> void {
1699 if (auto *BV = dyn_cast_if_present<DIVariable *>(Bound)) {
1700 if (auto *VarDIE = getDIE(BV))
1701 addDIEEntry(DwGenericSubrange, Attr, *VarDIE);
1702 } else if (auto *BE = dyn_cast_if_present<DIExpression *>(Bound)) {
1703 if (BE->isConstant() &&
1705 *BE->isConstant()) {
1706 if (Attr != dwarf::DW_AT_lower_bound || DefaultLowerBound == -1 ||
1707 static_cast<int64_t>(BE->getElement(1)) != DefaultLowerBound)
1708 addSInt(DwGenericSubrange, Attr, dwarf::DW_FORM_sdata,
1709 BE->getElement(1));
1710 } else {
1711 addBlock(DwGenericSubrange, Attr, BE);
1712 }
1713 }
1714 };
1715
1716 AddBoundTypeEntry(dwarf::DW_AT_lower_bound, GSR->getLowerBound());
1717 AddBoundTypeEntry(dwarf::DW_AT_count, GSR->getCount());
1718 AddBoundTypeEntry(dwarf::DW_AT_upper_bound, GSR->getUpperBound());
1719 AddBoundTypeEntry(dwarf::DW_AT_byte_stride, GSR->getStride());
1720}
1721
1722DIE *DwarfUnit::getIndexTyDie() {
1723 if (IndexTyDie)
1724 return IndexTyDie;
1725 // Construct an integer type to use for indexes.
1726 IndexTyDie = &createAndAddDIE(dwarf::DW_TAG_base_type, getUnitDie());
1727 StringRef Name = "__ARRAY_SIZE_TYPE__";
1728 addString(*IndexTyDie, dwarf::DW_AT_name, Name);
1729 addUInt(*IndexTyDie, dwarf::DW_AT_byte_size, std::nullopt, sizeof(int64_t));
1730 addUInt(*IndexTyDie, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
1732 DD->addAccelType(*this, CUNode->getNameTableKind(), Name, *IndexTyDie,
1733 /*Flags*/ 0);
1734 return IndexTyDie;
1735}
1736
1737/// Returns true if the vector's size differs from the sum of sizes of elements
1738/// the user specified. This can occur if the vector has been rounded up to
1739/// fit memory alignment constraints.
1740static bool hasVectorBeenPadded(const DICompositeType *CTy) {
1741 assert(CTy && CTy->isVector() && "Composite type is not a vector");
1742 const uint64_t ActualSize = CTy->getSizeInBits();
1743
1744 // Obtain the size of each element in the vector.
1745 DIType *BaseTy = CTy->getBaseType();
1746 assert(BaseTy && "Unknown vector element type.");
1747 const uint64_t ElementSize = BaseTy->getSizeInBits();
1748
1749 // Locate the number of elements in the vector.
1750 const DINodeArray Elements = CTy->getElements();
1751 assert(Elements.size() == 1 &&
1752 Elements[0]->getTag() == dwarf::DW_TAG_subrange_type &&
1753 "Invalid vector element array, expected one element of type subrange");
1754 const auto Subrange = cast<DISubrange>(Elements[0]);
1755 const auto NumVecElements =
1756 Subrange->getCount()
1757 ? cast<ConstantInt *>(Subrange->getCount())->getSExtValue()
1758 : 0;
1759
1760 // Ensure we found the element count and that the actual size is wide
1761 // enough to contain the requested size.
1762 assert(ActualSize >= (NumVecElements * ElementSize) && "Invalid vector size");
1763 return ActualSize != (NumVecElements * ElementSize);
1764}
1765
1766void DwarfUnit::constructArrayTypeDIE(DIE &Buffer, const DICompositeType *CTy) {
1767 if (CTy->isVector()) {
1768 addFlag(Buffer, dwarf::DW_AT_GNU_vector);
1769 if (hasVectorBeenPadded(CTy))
1770 addUInt(Buffer, dwarf::DW_AT_byte_size, std::nullopt,
1771 CTy->getSizeInBits() / CHAR_BIT);
1772 }
1773
1774 if (DIVariable *Var = CTy->getDataLocation()) {
1775 if (auto *VarDIE = getDIE(Var))
1776 addDIEEntry(Buffer, dwarf::DW_AT_data_location, *VarDIE);
1777 } else if (DIExpression *Expr = CTy->getDataLocationExp()) {
1778 addBlock(Buffer, dwarf::DW_AT_data_location, Expr);
1779 }
1780
1781 if (DIVariable *Var = CTy->getAssociated()) {
1782 if (auto *VarDIE = getDIE(Var))
1783 addDIEEntry(Buffer, dwarf::DW_AT_associated, *VarDIE);
1784 } else if (DIExpression *Expr = CTy->getAssociatedExp()) {
1785 addBlock(Buffer, dwarf::DW_AT_associated, Expr);
1786 }
1787
1788 if (DIVariable *Var = CTy->getAllocated()) {
1789 if (auto *VarDIE = getDIE(Var))
1790 addDIEEntry(Buffer, dwarf::DW_AT_allocated, *VarDIE);
1791 } else if (DIExpression *Expr = CTy->getAllocatedExp()) {
1792 addBlock(Buffer, dwarf::DW_AT_allocated, Expr);
1793 }
1794
1795 if (auto *RankConst = CTy->getRankConst()) {
1796 addSInt(Buffer, dwarf::DW_AT_rank, dwarf::DW_FORM_sdata,
1797 RankConst->getSExtValue());
1798 } else if (auto *RankExpr = CTy->getRankExp()) {
1799 addBlock(Buffer, dwarf::DW_AT_rank, RankExpr);
1800 }
1801
1802 if (auto *BitStride = CTy->getBitStrideConst()) {
1803 addUInt(Buffer, dwarf::DW_AT_bit_stride, {}, BitStride->getZExtValue());
1804 }
1805
1806 // Emit the element type.
1807 addType(Buffer, CTy->getBaseType());
1808
1809 // Add subranges to array type.
1810 DINodeArray Elements = CTy->getElements();
1811 for (DINode *E : Elements) {
1812 if (auto *Element = dyn_cast_or_null<DISubrangeType>(E)) {
1813 DIE &TyDIE = createAndAddDIE(Element->getTag(), Buffer, CTy);
1814 constructSubrangeDIE(TyDIE, Element, true);
1815 } else if (auto *Element = dyn_cast_or_null<DISubrange>(E))
1816 constructSubrangeDIE(Buffer, Element);
1817 else if (auto *Element = dyn_cast_or_null<DIGenericSubrange>(E))
1818 constructGenericSubrangeDIE(Buffer, Element);
1819 }
1820}
1821
1822void DwarfUnit::constructEnumTypeDIE(DIE &Buffer, const DICompositeType *CTy) {
1823 const DIType *DTy = CTy->getBaseType();
1824 bool IsUnsigned = DTy && DD->isUnsignedDIType(DTy);
1825 if (DTy) {
1826 if (!Asm->TM.Options.DebugStrictDwarf || DD->getDwarfVersion() >= 3)
1827 addType(Buffer, DTy);
1828 if (DD->getDwarfVersion() >= 4 && (CTy->getFlags() & DINode::FlagEnumClass))
1829 addFlag(Buffer, dwarf::DW_AT_enum_class);
1830 }
1831
1832 if (auto Kind = CTy->getEnumKind())
1833 addUInt(Buffer, dwarf::DW_AT_APPLE_enum_kind, dwarf::DW_FORM_data1, *Kind);
1834
1835 auto *Context = CTy->getScope();
1836 bool IndexEnumerators = !Context || isa<DICompileUnit>(Context) || isa<DIFile>(Context) ||
1838 DINodeArray Elements = CTy->getElements();
1839
1840 // Add enumerators to enumeration type.
1841 for (const DINode *E : Elements) {
1843 if (Enum) {
1844 DIE &Enumerator = createAndAddDIE(dwarf::DW_TAG_enumerator, Buffer);
1845 StringRef Name = Enum->getName();
1846 addString(Enumerator, dwarf::DW_AT_name, Name);
1847 addConstantValue(Enumerator, Enum->getValue(), IsUnsigned);
1848 if (IndexEnumerators)
1850 }
1851 }
1852}
1853
1855 for (auto &P : ContainingTypeMap) {
1856 DIE &SPDie = *P.first;
1857 const DINode *D = P.second;
1858 if (!D)
1859 continue;
1860 DIE *NDie = getDIE(D);
1861 if (!NDie)
1862 continue;
1863 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, *NDie);
1864 }
1865}
1866
1867DIE &DwarfUnit::constructMemberDIE(DIE &Buffer, const DIDerivedType *DT) {
1868 DIE &MemberDie = createAndAddDIE(DT->getTag(), Buffer, DT);
1869 StringRef Name = DT->getName();
1870 if (!Name.empty())
1871 addString(MemberDie, dwarf::DW_AT_name, Name);
1872
1873 addAnnotation(MemberDie, DT->getAnnotations());
1874
1875 if (DIType *Resolved = DT->getBaseType())
1876 addType(MemberDie, Resolved);
1877
1878 addSourceLine(MemberDie, DT);
1879
1880 if (DT->getTag() == dwarf::DW_TAG_inheritance && DT->isVirtual()) {
1881
1882 // For C++, virtual base classes are not at fixed offset. Use following
1883 // expression to extract appropriate offset from vtable.
1884 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1885
1886 DIELoc *VBaseLocationDie = new (DIEValueAllocator) DIELoc;
1887 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1888 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1889 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1890 addUInt(*VBaseLocationDie, dwarf::DW_FORM_udata, DT->getOffsetInBits());
1891 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1892 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1893 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1894
1895 addBlock(MemberDie, dwarf::DW_AT_data_member_location, VBaseLocationDie);
1896 } else {
1897 uint64_t Size = 0;
1898 uint64_t FieldSize = 0;
1899
1900 bool IsBitfield = DT->isBitField();
1901
1902 // Handle the size.
1903 if (DT->getRawSizeInBits() == nullptr) {
1904 // No size, just ignore.
1905 } else if (auto *Var = dyn_cast<DIVariable>(DT->getRawSizeInBits())) {
1906 if (auto *VarDIE = getDIE(Var))
1907 addDIEEntry(MemberDie, dwarf::DW_AT_bit_size, *VarDIE);
1908 } else if (auto *Exp = dyn_cast<DIExpression>(DT->getRawSizeInBits())) {
1909 addBlock(MemberDie, dwarf::DW_AT_bit_size, Exp);
1910 } else {
1911 Size = DT->getSizeInBits();
1912 FieldSize = DD->getBaseTypeSize(DT);
1913 if (IsBitfield) {
1914 // Handle bitfield, assume bytes are 8 bits.
1915 if (DD->useDWARF2Bitfields())
1916 addUInt(MemberDie, dwarf::DW_AT_byte_size, std::nullopt,
1917 FieldSize / 8);
1918 addUInt(MemberDie, dwarf::DW_AT_bit_size, std::nullopt, Size);
1919 }
1920 }
1921
1922 // Handle the location. DW_AT_data_bit_offset won't allow an
1923 // expression until DWARF 6, but it can be used as an extension.
1924 // See https://dwarfstd.org/issues/250501.1.html
1925 if (auto *Var = dyn_cast_or_null<DIVariable>(DT->getRawOffsetInBits())) {
1926 if (!Asm->TM.Options.DebugStrictDwarf || DD->getDwarfVersion() >= 6) {
1927 if (auto *VarDIE = getDIE(Var))
1928 addDIEEntry(MemberDie, dwarf::DW_AT_data_bit_offset, *VarDIE);
1929 }
1930 } else if (auto *Expr =
1932 if (!Asm->TM.Options.DebugStrictDwarf || DD->getDwarfVersion() >= 6) {
1933 addBlock(MemberDie, dwarf::DW_AT_data_bit_offset, Expr);
1934 }
1935 } else {
1936 uint32_t AlignInBytes = DT->getAlignInBytes();
1937 uint64_t OffsetInBytes;
1938
1939 if (IsBitfield) {
1940 assert(DT->getOffsetInBits() <=
1941 (uint64_t)std::numeric_limits<int64_t>::max());
1942 int64_t Offset = DT->getOffsetInBits();
1943 // We can't use DT->getAlignInBits() here: AlignInBits for member type
1944 // is non-zero if and only if alignment was forced (e.g. _Alignas()),
1945 // which can't be done with bitfields. Thus we use FieldSize here.
1946 uint32_t AlignInBits = FieldSize;
1947 uint32_t AlignMask = ~(AlignInBits - 1);
1948 // The bits from the start of the storage unit to the start of the
1949 // field.
1950 uint64_t StartBitOffset = Offset - (Offset & AlignMask);
1951 // The byte offset of the field's aligned storage unit inside the
1952 // struct.
1953 OffsetInBytes = (Offset - StartBitOffset) / 8;
1954
1955 if (DD->useDWARF2Bitfields()) {
1956 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1957 uint64_t FieldOffset = (HiMark - FieldSize);
1958 Offset -= FieldOffset;
1959
1960 // Maybe we need to work from the other end.
1961 if (Asm->getDataLayout().isLittleEndian())
1962 Offset = FieldSize - (Offset + Size);
1963
1964 if (Offset < 0)
1965 addSInt(MemberDie, dwarf::DW_AT_bit_offset, dwarf::DW_FORM_sdata,
1966 Offset);
1967 else
1968 addUInt(MemberDie, dwarf::DW_AT_bit_offset, std::nullopt,
1969 (uint64_t)Offset);
1970 OffsetInBytes = FieldOffset >> 3;
1971 } else {
1972 addUInt(MemberDie, dwarf::DW_AT_data_bit_offset, std::nullopt,
1973 Offset);
1974 }
1975 } else {
1976 // This is not a bitfield.
1977 OffsetInBytes = DT->getOffsetInBits() / 8;
1978 if (AlignInBytes)
1979 addUInt(MemberDie, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata,
1980 AlignInBytes);
1981 }
1982
1983 if (DD->getDwarfVersion() <= 2) {
1984 DIELoc *MemLocationDie = new (DIEValueAllocator) DIELoc;
1985 addUInt(*MemLocationDie, dwarf::DW_FORM_data1,
1986 dwarf::DW_OP_plus_uconst);
1987 addUInt(*MemLocationDie, dwarf::DW_FORM_udata, OffsetInBytes);
1988 addBlock(MemberDie, dwarf::DW_AT_data_member_location, MemLocationDie);
1989 } else if (!IsBitfield || DD->useDWARF2Bitfields()) {
1990 // In DWARF v3, DW_FORM_data4/8 in DW_AT_data_member_location are
1991 // interpreted as location-list pointers. Interpreting constants as
1992 // pointers is not expected, so we use DW_FORM_udata to encode the
1993 // constants here.
1994 if (DD->getDwarfVersion() == 3)
1995 addUInt(MemberDie, dwarf::DW_AT_data_member_location,
1996 dwarf::DW_FORM_udata, OffsetInBytes);
1997 else
1998 addUInt(MemberDie, dwarf::DW_AT_data_member_location, std::nullopt,
1999 OffsetInBytes);
2000 }
2001 }
2002 }
2003
2004 addAccess(MemberDie, DT->getFlags());
2005
2006 if (DT->isVirtual())
2007 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1,
2008 dwarf::DW_VIRTUALITY_virtual);
2009
2010 // Objective-C properties.
2011 if (DINode *PNode = DT->getObjCProperty())
2012 if (DIE *PDie = getDIE(PNode))
2013 addAttribute(MemberDie, dwarf::DW_AT_APPLE_property,
2014 dwarf::DW_FORM_ref4, DIEEntry(*PDie));
2015
2016 if (DT->isArtificial())
2017 addFlag(MemberDie, dwarf::DW_AT_artificial);
2018
2019 return MemberDie;
2020}
2021
2022void DwarfUnit::constructPropertyDIE(DIE &Buffer, const DIProperty *P) {
2023 DIE &PropertyDie = createAndAddDIE(dwarf::DW_TAG_property, Buffer, P);
2024 addString(PropertyDie, dwarf::DW_AT_name, P->getName());
2025 if (DIType *Ty = P->getType())
2026 addType(PropertyDie, Ty);
2027 addSourceLine(PropertyDie, P);
2028
2029 if (DINode *BackingStorage = P->getBackingStorage()) {
2030 DIE &GetterDie =
2031 createAndAddDIE(dwarf::DW_TAG_property_getter, PropertyDie);
2032 PropertyForwardMap.insert(std::make_pair(&GetterDie, BackingStorage));
2033 }
2034}
2035
2037 for (auto &P : PropertyForwardMap) {
2038 DIE &GetterDie = *P.first;
2039 const DINode *Target = P.second;
2040 if (!Target)
2041 continue;
2042 DIE *TargetDie = getDIE(Target);
2043 if (!TargetDie)
2044 continue;
2045 addDIEEntry(GetterDie, dwarf::DW_AT_property_forward, *TargetDie);
2046 }
2047}
2048
2050 if (!DT)
2051 return nullptr;
2052
2053 // Construct the context before querying for the existence of the DIE in case
2054 // such construction creates the DIE.
2055 DIE *ContextDIE = getOrCreateContextDIE(DT->getScope());
2056 assert(dwarf::isType(ContextDIE->getTag()) &&
2057 "Static member should belong to a type.");
2058
2059 if (DIE *StaticMemberDIE = getDIE(DT))
2060 return StaticMemberDIE;
2061
2062 DwarfUnit *ContextUnit = static_cast<DwarfUnit *>(ContextDIE->getUnit());
2063 DIE &StaticMemberDIE = createAndAddDIE(DT->getTag(), *ContextDIE, DT);
2064
2065 const DIType *Ty = DT->getBaseType();
2066
2067 addString(StaticMemberDIE, dwarf::DW_AT_name, DT->getName());
2068 addType(StaticMemberDIE, Ty);
2069 ContextUnit->addSourceLine(StaticMemberDIE, DT);
2070 addFlag(StaticMemberDIE, dwarf::DW_AT_external);
2071 addFlag(StaticMemberDIE, dwarf::DW_AT_declaration);
2072
2073 // Consider the case when the static member was created by the compiler.
2074 if (DT->isArtificial())
2075 addFlag(StaticMemberDIE, dwarf::DW_AT_artificial);
2076
2077 // FIXME: We could omit private if the parent is a class_type, and
2078 // public if the parent is something else.
2079 addAccess(StaticMemberDIE, DT->getFlags());
2080
2082 addConstantValue(StaticMemberDIE, CI, Ty);
2083 else if (const ConstantFP *CFP =
2085 addConstantFPValue(StaticMemberDIE, CFP);
2086 else if (auto *CDS =
2088 assert(CDS->getElementType()->isIntegerTy() &&
2089 "Non-integer arrays not supported.");
2091 for (unsigned I = 0; I != CDS->getNumElements(); ++I)
2092 addIntToBlock(*Block, CDS->getElementAsAPInt(I));
2093 Block->computeSize(Asm->getDwarfFormParams());
2094 addBlock(StaticMemberDIE, dwarf::DW_AT_const_value, Block->BestForm(),
2095 Block);
2096 }
2097
2098 if (uint32_t AlignInBytes = DT->getAlignInBytes())
2099 addUInt(StaticMemberDIE, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata,
2100 AlignInBytes);
2101
2102 return &StaticMemberDIE;
2103}
2104
2106 // Emit size of content not including length itself
2107 if (!DD->useSectionsAsReferences())
2108 EndLabel = Asm->emitDwarfUnitLength(
2109 isDwoUnit() ? "debug_info_dwo" : "debug_info", "Length of Unit");
2110 else
2111 Asm->emitDwarfUnitLength(getHeaderSize() + getUnitDie().getSize(),
2112 "Length of Unit");
2113
2114 Asm->OutStreamer->AddComment("DWARF version number");
2115 unsigned Version = DD->getDwarfVersion();
2116 Asm->emitInt16(Version);
2117
2118 // DWARF v5 reorders the address size and adds a unit type.
2119 if (Version >= 5) {
2120 Asm->OutStreamer->AddComment("DWARF Unit Type");
2121 Asm->emitInt8(UT);
2122 Asm->OutStreamer->AddComment("Address Size (in bytes)");
2123 Asm->emitInt8(Asm->MAI.getCodePointerSize());
2124 }
2125
2126 // We share one abbreviations table across all units so it's always at the
2127 // start of the section. Use a relocatable offset where needed to ensure
2128 // linking doesn't invalidate that offset.
2129 Asm->OutStreamer->AddComment("Offset Into Abbrev. Section");
2130 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
2131 if (UseOffsets)
2132 Asm->emitDwarfLengthOrOffset(0);
2133 else
2134 Asm->emitDwarfSymbolReference(
2135 TLOF.getDwarfAbbrevSection()->getBeginSymbol(), false);
2136
2137 if (Version <= 4) {
2138 Asm->OutStreamer->AddComment("Address Size (in bytes)");
2139 Asm->emitInt8(Asm->MAI.getCodePointerSize());
2140 }
2141}
2142
2143void DwarfTypeUnit::emitHeader(bool UseOffsets) {
2144 if (!DD->useSplitDwarf()) {
2145 LabelBegin = Asm->createTempSymbol("tu_begin");
2146 Asm->OutStreamer->emitLabel(LabelBegin);
2147 }
2148 DwarfUnit::emitCommonHeader(UseOffsets,
2149 DD->useSplitDwarf() ? dwarf::DW_UT_split_type
2150 : dwarf::DW_UT_type);
2151 Asm->OutStreamer->AddComment("Type Signature");
2152 Asm->OutStreamer->emitIntValue(TypeSignature, sizeof(TypeSignature));
2153 Asm->OutStreamer->AddComment("Type DIE Offset");
2154 // In a skeleton type unit there is no type DIE so emit a zero offset.
2155 Asm->emitDwarfLengthOrOffset(Ty ? Ty->getOffset() : 0);
2156}
2157
2158void DwarfUnit::addSectionDelta(DIE &Die, dwarf::Attribute Attribute,
2159 const MCSymbol *Hi, const MCSymbol *Lo) {
2162}
2163
2165 const MCSymbol *Label, const MCSymbol *Sec) {
2166 // If we are in an object file and not a DWO, emit a label. Otherwise we are
2167 // in a DWO and we cannot emit relocations, so emit a section delta.
2168 if (Asm->doesDwarfUseRelocationsAcrossSections() && !isDwoUnit())
2169 addLabel(Die, Attribute, DD->getDwarfSectionOffsetForm(), Label);
2170 else
2171 addSectionDelta(Die, Attribute, Label, Sec);
2172}
2173
2174bool DwarfTypeUnit::isDwoUnit() const {
2175 // Since there are no skeleton type units, all type units are dwo type units
2176 // when split DWARF is being used.
2177 return DD->useSplitDwarf();
2178}
2179
2181 const DIScope *Context) {
2182 getCU().addGlobalNameForTypeUnit(Name, Context);
2183}
2184
2186 const DIScope *Context) {
2187 getCU().addGlobalTypeUnitType(Ty, Context);
2188}
2189
2192 return nullptr;
2193 if (isDwoUnit())
2194 return nullptr;
2195 return getSection()->getBeginSymbol();
2196}
2197
2199 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
2200 addSectionLabel(getUnitDie(), dwarf::DW_AT_str_offsets_base,
2201 DU->getStringOffsetsStartSym(),
2203}
2204
2206 assert(DD->getDwarfVersion() >= 5 &&
2207 "DW_AT_rnglists_base requires DWARF version 5 or later");
2208 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
2209 addSectionLabel(getUnitDie(), dwarf::DW_AT_rnglists_base,
2210 DU->getRnglistsTableBaseSym(),
2212}
2213
2214void DwarfTypeUnit::finishNonUnitTypeDIE(DIE& D, const DICompositeType *CTy) {
2216}
2217
2218bool DwarfUnit::isCompatibleWithVersion(uint16_t Version) const {
2219 return !Asm->TM.Options.DebugStrictDwarf || DD->getDwarfVersion() >= Version;
2220}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
unsigned uint64_t
This file declares a class to represent arbitrary precision floating point values and provide a varie...
This file implements a class to represent arbitrary precision integral constant values and operations...
BitTracker BT
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
static bool hasVectorBeenPadded(const DICompositeType *CTy)
Returns true if the vector's size differs from the sum of sizes of elements the user specified.
#define I(x, y, z)
Definition MD5.cpp:57
#define G(x, y, z)
Definition MD5.cpp:55
Register const TargetRegisterInfo * TRI
This file contains the declarations for metadata subclasses.
#define T
#define P(N)
static enum BaseType getBaseType(const Value *Val)
Return the baseType for Val which states whether Val is exclusively derived from constant/null,...
static unsigned getSize(unsigned Kind)
APInt bitcastToAPInt() const
Definition APFloat.h:1467
Class for arbitrary precision integers.
Definition APInt.h:78
uint64_t getZExtValue() const
Get zero extended value.
Definition APInt.h:1561
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition APInt.h:1509
const uint64_t * getRawData() const
This function returns a pointer to the internal storage of the APInt.
Definition APInt.h:572
int64_t getSExtValue() const
Get sign extended value.
Definition APInt.h:1583
void resetUsedFlag(bool HasBeenUsed=false)
Definition AddressPool.h:51
Annotations lets you mark points and ranges inside source code, for tests:
Definition Annotations.h:67
This class is intended to be used as a driving class for all asm writers.
Definition AsmPrinter.h:91
MCSymbol * getSymbol(const GlobalValue *GV) const
TargetMachine & TM
Target machine description.
Definition AsmPrinter.h:94
MCContext & OutContext
This is the context for the output file that we are streaming.
Definition AsmPrinter.h:101
const DataLayout & getDataLayout() const
Return information about data layout.
bool doesDwarfUseRelocationsAcrossSections() const
Definition AsmPrinter.h:376
Functions, function parameters, and return types can have attributes to indicate how they should be t...
Definition Attributes.h:105
ConstantFP - Floating Point Values [float, double].
Definition Constants.h:420
const APFloat & getValueAPF() const
Definition Constants.h:463
This is the shared class of boolean and integer constants.
Definition Constants.h:87
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition Constants.h:159
This is an important base class in LLVM.
Definition Constant.h:43
Basic type, like 'int' or 'float'.
uint32_t getDataSizeInBits() const
unsigned getEncoding() const
DIExpression * getRankExp() const
DIExpression * getAssociatedExp() const
DIVariable * getAllocated() const
DIExpression * getDataLocationExp() const
DIVariable * getAssociated() const
DIDerivedType * getDiscriminator() const
DIVariable * getDataLocation() const
unsigned getRuntimeLang() const
DIType * getSpecification() const
StringRef getIdentifier() const
DINodeArray getElements() const
DITemplateParameterArray getTemplateParams() const
DIExpression * getAllocatedExp() const
ConstantInt * getBitStrideConst() const
std::optional< uint32_t > getEnumKind() const
DIType * getVTableHolder() const
DINodeArray getAnnotations() const
ConstantInt * getRankConst() const
MDString * getRawIdentifier() const
DIType * getBaseType() const
DINodeArray getAnnotations() const
Get annotations associated with this derived type.
DITemplateParameterArray getTemplateParams() const
Get the template parameters from a template alias.
DIObjCProperty * getObjCProperty() const
LLVM_ABI Constant * getConstant() const
DIEBlock - Represents a block of values.
Definition DIE.h:1070
A simple label difference DIE.
Definition DIE.h:265
DwarfExpression implementation for singular DW_AT_location.
DIEDwarfExpression(const AsmPrinter &AP, DwarfCompileUnit &CU, DIELoc &DIE)
Definition DwarfUnit.cpp:40
A pointer to another debug information entry.
Definition DIE.h:325
A container for inline string values.
Definition DIE.h:303
An integer value DIE.
Definition DIE.h:169
static dwarf::Form BestForm(bool IsSigned, uint64_t Int)
Choose the best form for integer.
Definition DIE.h:176
A label DIE.
Definition DIE.h:226
DIELoc - Represents an expression location.
Definition DIE.h:1034
A container for string pool string values.
Definition DIE.h:283
virtual const MCSymbol * getCrossSectionRelativeBaseAddress() const
Definition DIE.h:1013
LLVM_ABI DIEUnit(dwarf::Tag UnitTag)
Definition DIE.cpp:306
MCSection * getSection() const
Return the section that this DIEUnit will be emitted into.
Definition DIE.h:1020
DIE & getUnitDie()
Definition DIE.h:1023
A list of DIE values.
Definition DIE.h:712
A structured debug information entry.
Definition DIE.h:842
DIE & addChild(DIE *Child)
Add a child to the DIE.
Definition DIE.h:958
static DIE * get(BumpPtrAllocator &Alloc, dwarf::Tag Tag)
Definition DIE.h:872
LLVM_ABI DIEUnit * getUnit() const
Climb up the parent chain to get the compile unit or type unit that this DIE belongs to.
Definition DIE.cpp:203
dwarf::Tag getTag() const
Definition DIE.h:878
DWARF expression.
const APInt & getDenominator() const
LLVM_ABI bool isSigned() const
const APInt & getNumerator() const
LLVM_ABI BoundType getLowerBound() const
LLVM_ABI BoundType getCount() const
LLVM_ABI BoundType getUpperBound() const
PointerUnion< DIVariable *, DIExpression * > BoundType
LLVM_ABI BoundType getStride() const
Represents a module in the programming language, for example, a Clang module, or a Fortran module.
Debug lexical block.
DIScope * getScope() const
StringRef getName() const
bool getExportSymbols() const
Tagged DWARF-like metadata node.
LLVM_ABI dwarf::Tag getTag() const
DIFlags
Debug info flags.
A property of a class or structure.
Base class for scope-like contexts.
LLVM_ABI DIScope * getScope() const
String type, Fortran CHARACTER(n)
unsigned getEncoding() const
DIExpression * getStringLengthExp() const
DIVariable * getStringLength() const
DIExpression * getStringLocationExp() const
Subprogram description. Uses SubclassData1.
BoundType getLowerBound() const
BoundType getBias() const
PointerUnion< ConstantInt *, DIVariable *, DIExpression *, DIDerivedType * > BoundType
BoundType getUpperBound() const
DIType * getBaseType() const
Get the base type this is derived from.
BoundType getStride() const
Array subrange.
LLVM_ABI BoundType getUpperBound() const
LLVM_ABI BoundType getStride() const
LLVM_ABI BoundType getLowerBound() const
LLVM_ABI BoundType getCount() const
Type array for a subprogram.
Base class for types.
bool isLittleEndian() const
uint32_t getNumExtraInhabitants() const
bool isBigEndian() const
bool isLValueReference() const
bool isBitField() const
bool isVirtual() const
bool isObjcClassComplete() const
bool isAppleBlockExtension() const
uint64_t getOffsetInBits() const
bool isVector() const
DIFlags getFlags() const
StringRef getName() const
bool isForwardDecl() const
bool isTypePassByValue() const
uint64_t getSizeInBits() const
uint32_t getAlignInBytes() const
Metadata * getRawSizeInBits() const
bool isRValueReference() const
bool isArtificial() const
bool getExportSymbols() const
DIScope * getScope() const
bool isTypePassByReference() const
Metadata * getRawOffsetInBits() const
bool isLittleEndian() const
Layout endianness...
Definition DataLayout.h:217
void addGlobalNameForTypeUnit(StringRef Name, const DIScope *Context)
Add a new global name present in a type unit to this compile unit.
unsigned getOrCreateSourceID(const DIFile *File) override
Look up the source ID for the given file.
void addGlobalTypeUnitType(const DIType *Ty, const DIScope *Context)
Add a new global type present in a type unit to this compile unit.
Collects and handles dwarf debug information.
Definition DwarfDebug.h:352
std::optional< MD5::MD5Result > getMD5AsBytes(const DIFile *File) const
If the File has an MD5 checksum, return it as an MD5Result allocated in the MCContext.
uint16_t getDwarfVersion() const
Returns the Dwarf Version.
dwarf::Form getDwarfSectionOffsetForm() const
Returns a suitable DWARF form to represent a section offset, i.e.
AddressPool & getAddressPool()
Definition DwarfDebug.h:930
bool useSplitDwarf() const
Returns whether or not to change the current debug info for split DWARF.
Definition DwarfDebug.h:875
virtual void disableTemporaryBuffer()=0
Disable emission to the temporary buffer.
virtual unsigned getTemporaryBufferSize()=0
Return the emitted size, in number of bytes, for the data stored in the temporary buffer.
void setMemoryLocationKind()
Lock this down to become a memory location description.
DwarfCompileUnit & CU
void addExpression(DIExpressionCursor &&Expr)
Emit all remaining operations in the DIExpressionCursor.
virtual void commitTemporaryBuffer()=0
Commit the data stored in the temporary buffer to the main output.
DwarfExpression(unsigned DwarfVersion, DwarfCompileUnit &CU)
virtual void enableTemporaryBuffer()=0
Start emitting data to the temporary buffer.
DwarfTypeUnit(DwarfCompileUnit &CU, AsmPrinter *A, DwarfDebug *DW, DwarfFile *DWU, unsigned UniqueID, MCDwarfDwoLineTable *SplitLineTable=nullptr)
Definition DwarfUnit.cpp:88
void addGlobalTypeImpl(const DIType *Ty, const DIE &Die, const DIScope *Context) override
Add a new global type to the compile unit.
DwarfCompileUnit & getCU() override
Definition DwarfUnit.h:455
void emitHeader(bool UseOffsets) override
Emit the header for this unit, not including the initial length field.
void addGlobalName(StringRef Name, const DIE &Die, const DIScope *Context) override
Add a new global name to the compile unit.
virtual DIE * getOrCreateTypeDIE(const MDNode *TyNode)
Find existing DIE or create new DIE for the given type.
void addInt(DIE &Die, dwarf::Attribute Attribute, const APInt &Integer, bool Unsigned)
Add an integer attribute data and value; value may be any width.
DwarfDebug & getDwarfDebug() const
Definition DwarfUnit.h:114
void addThrownTypes(DIE &Die, DINodeArray ThrownTypes)
Add thrown types.
void addStringOffsetsStart()
Add the DW_AT_str_offsets_base attribute to the unit DIE.
void addAnnotation(DIE &Buffer, DINodeArray Annotations)
Add DW_TAG_LLVM_annotation.
std::vector< DIEBlock * > DIEBlocks
A list of all the DIEBlocks in use.
Definition DwarfUnit.h:67
std::vector< DIELoc * > DIELocs
A list of all the DIELocs in use.
Definition DwarfUnit.h:70
void addBlock(DIE &Die, dwarf::Attribute Attribute, DIELoc *Loc)
Add block data.
void addTemplateParams(DIE &Buffer, DINodeArray TParams)
Add template parameters in buffer.
virtual DIE * getOrCreateContextDIE(const DIScope *Context)
Get context owner's DIE.
bool useSegmentedStringOffsetsTable() const
Definition DwarfUnit.h:297
bool applySubprogramDefinitionAttributes(const DISubprogram *SP, DIE &SPDie, bool Minimal)
DIELoc * getDIELoc()
Returns a fresh newly allocated DIELoc.
Definition DwarfUnit.h:146
void updateAcceleratorTables(const DIScope *Context, const DIType *Ty, const DIE &TyDIE)
If this is a named finished type then include it in the list of types for the accelerator tables.
void addAttribute(DIEValueList &Die, dwarf::Attribute Attribute, dwarf::Form Form, T &&Value)
Definition DwarfUnit.h:86
void addOpAddress(DIELoc &Die, const MCSymbol *Sym)
Add a dwarf op address data and value using the form given and an op of either DW_FORM_addr or DW_FOR...
void addUInt(DIEValueList &Die, dwarf::Attribute Attribute, std::optional< dwarf::Form > Form, uint64_t Integer)
Add an unsigned integer attribute data and value.
void addString(DIE &Die, dwarf::Attribute Attribute, StringRef Str)
Add a string attribute data and value.
void addConstantValue(DIE &Die, const ConstantInt *CI, const DIType *Ty)
Add constant value entry in variable DIE.
DIE * getOrCreateNameSpace(const DINamespace *NS)
void insertDIE(const DINode *Desc, DIE *D)
Insert DIE into the map.
void addAccess(DIE &Die, DINode::DIFlags Flags)
Add the accessibility attribute.
void addGlobalType(const DIType *Ty, const DIE &Die, const DIScope *Context)
DIE * createTypeDIE(const DIScope *Context, DIE &ContextDIE, const DIType *Ty)
Creates type DIE with specific context.
bool shouldPlaceInUnitDIE(const DISubprogram *SP, bool Minimal)
Definition DwarfUnit.h:353
DwarfDebug * DD
Definition DwarfUnit.h:56
DenseMap< DIE *, const DINode * > ContainingTypeMap
This map is used to keep track of subprogram DIEs that need DW_AT_containing_type attribute.
Definition DwarfUnit.h:75
const DICompileUnit * CUNode
MDNode for the compile unit.
Definition DwarfUnit.h:41
virtual unsigned getOrCreateSourceID(const DIFile *File)=0
Look up the source ID for the given file.
virtual DIE * getOrCreateSubprogramDIE(const DISubprogram *SP, const Function *FnHint, bool Minimal=false)
DIE * getOrCreateSubprogramContextDIE(const DISubprogram *SP, bool IgnoreScope)
Definition DwarfUnit.h:358
virtual void addGlobalTypeImpl(const DIType *Ty, const DIE &Die, const DIScope *Context)=0
Add a new global type to the compile unit.
void addDIETypeSignature(DIE &Die, uint64_t Signature)
Add a type's DW_AT_signature and set the declaration flag.
virtual DwarfCompileUnit & getCU()=0
DIE * getDIE(const DINode *D) const
Returns the DIE map slot for the specified debug variable.
void constructPropertyForwardDIEs()
virtual unsigned getHeaderSize() const
Compute the size of a header for this unit, not including the initial length field.
Definition DwarfUnit.h:303
MCSymbol * LabelBegin
The start of the unit within its section.
Definition DwarfUnit.h:50
void addSInt(DIEValueList &Die, dwarf::Attribute Attribute, std::optional< dwarf::Form > Form, int64_t Integer)
Add an signed integer attribute data and value.
DwarfUnit(dwarf::Tag, const DICompileUnit *Node, AsmPrinter *A, DwarfDebug *DW, DwarfFile *DWU, unsigned UniqueID=0)
Definition DwarfUnit.cpp:82
void addLabelDelta(DIEValueList &Die, dwarf::Attribute Attribute, const MCSymbol *Hi, const MCSymbol *Lo)
Add a label delta attribute data and value.
void addLinkageName(DIE &Die, StringRef LinkageName)
Add a linkage name, if it isn't empty.
std::string getParentContextString(const DIScope *Context) const
Get string containing language specific context for a global name.
void addSourceLine(DIE &Die, unsigned Line, unsigned Column, const DIFile *File)
Add location information to specified debug information entry.
void emitCommonHeader(bool UseOffsets, dwarf::UnitType UT)
Emit the common part of the header for this unit.
BumpPtrAllocator DIEValueAllocator
Definition DwarfUnit.h:44
DIE * IndexTyDie
An anonymous type for index type. Owned by DIEUnit.
Definition DwarfUnit.h:60
void addRnglistsBase()
Add the DW_AT_rnglists_base attribute to the unit DIE.
DIE * getOrCreateModule(const DIModule *M)
const DICompileUnit * getCUNode() const
Definition DwarfUnit.h:113
DIE & createAndAddDIE(dwarf::Tag Tag, DIE &Parent, const DINode *N=nullptr)
Create a DIE with the given Tag, add the DIE to its parent, and call insertDIE if MD is not null.
DwarfFile * DU
Definition DwarfUnit.h:57
std::optional< unsigned > constructSubprogramArguments(DIE &Buffer, DITypeArray Args)
Construct function argument DIEs.
void addSectionOffset(DIE &Die, dwarf::Attribute Attribute, uint64_t Integer)
Add an offset into a section attribute data and value.
DIE * getOrCreateStaticMemberDIE(const DIDerivedType *DT)
Create new static data member DIE.
void addLabel(DIEValueList &Die, dwarf::Attribute Attribute, dwarf::Form Form, const MCSymbol *Label)
Add a Dwarf label attribute data and value.
void addConstantFPValue(DIE &Die, const ConstantFP *CFP)
Add constant value entry in variable DIE.
void constructContainingTypeDIEs()
Construct DIEs for types that contain vtables.
unsigned UniqueID
A numeric ID unique among all CUs in the module.
Definition DwarfUnit.h:39
void addSectionLabel(DIE &Die, dwarf::Attribute Attribute, const MCSymbol *Label, const MCSymbol *Sec)
Add a Dwarf section label attribute data and value.
bool isShareableAcrossCUs(const DINode *D) const
Check whether the DIE for this MDNode can be shared across CUs.
DenseMap< DIE *, const DINode * > PropertyForwardMap
Definition DwarfUnit.h:76
llvm::dwarf::SourceLanguage getSourceLanguage() const
void addPoolOpAddress(DIEValueList &Die, const MCSymbol *Label)
~DwarfUnit() override
Definition DwarfUnit.cpp:94
DenseMap< const MDNode *, DIE * > MDNodeToDieMap
Tracks the mapping of unit level debug information variables to debug information entries.
Definition DwarfUnit.h:64
void constructTypeDIE(DIE &Buffer, const DICompositeType *CTy)
virtual void addGlobalName(StringRef Name, const DIE &Die, const DIScope *Context)=0
Add a new global name to the compile unit.
MCSymbol * EndLabel
Emitted at the end of the CU and used to compute the CU Length field.
Definition DwarfUnit.h:53
void addFlag(DIE &Die, dwarf::Attribute Attribute)
Add a flag that is true to the DIE.
AsmPrinter * Asm
Target of Dwarf emission.
Definition DwarfUnit.h:47
void addType(DIE &Entity, const DIType *Ty, dwarf::Attribute Attribute=dwarf::DW_AT_type)
Add a new type attribute to the specified entity.
void applySubprogramAttributes(const DISubprogram *SP, DIE &SPDie, bool SkipSPAttributes=false)
void addDIEEntry(DIE &Die, dwarf::Attribute Attribute, DIE &Entry)
Add a DIE attribute data and value.
static StringRef dropLLVMManglingEscape(StringRef Name)
If the given string begins with the GlobalValue name mangling escape character '\1',...
uint16_t getDwarfVersion() const
Definition MCContext.h:814
unsigned getFile(StringRef Directory, StringRef FileName, std::optional< MD5::MD5Result > Checksum, uint16_t DwarfVersion, std::optional< StringRef > Source)
Definition MCDwarf.h:358
MCSection * getDwarfStrOffSection() const
MCSection * getDwarfRnglistsSection() const
MCSection * getDwarfAbbrevSection() const
MCSymbol * getBeginSymbol()
Definition MCSection.h:653
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition MCSymbol.h:42
Metadata node.
Definition Metadata.h:1069
const MDOperand & getOperand(unsigned I) const
Definition Metadata.h:1426
A single uniqued string.
Definition Metadata.h:722
Root of the metadata hierarchy.
Definition Metadata.h:64
Wrapper class representing virtual and physical registers.
Definition Register.h:20
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
constexpr bool empty() const
Check if the string is empty.
Definition StringRef.h:141
TargetOptions Options
unsigned DebugStrictDwarf
When set to true, don't use DWARF extensions in later DWARF versions.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
Target - Wrapper for Target specific information.
LLVM Value Representation.
Definition Value.h:75
Calculates the starting offsets for various sections within the .debug_names section.
Definition Dwarf.h:35
@ DW_ACCESS_private
Definition Dwarf.h:187
@ DW_ACCESS_protected
Definition Dwarf.h:186
@ DW_ACCESS_public
Definition Dwarf.h:185
Attribute
Attributes.
Definition Dwarf.h:125
SourceLanguageName
Definition Dwarf.h:229
UnitType
Constants for unit types in DWARF v5.
Definition Dwarf.h:979
bool isType(Tag T)
Definition Dwarf.h:113
@ DW_LANG_hi_user
Definition Dwarf.h:226
std::optional< SourceLanguage > toDW_LANG(SourceLanguageName name, uint32_t version)
Convert a DWARF 6 pair of language name and version to a DWARF 5 DW_LANG.
Definition Dwarf.h:237
bool isCPlusPlus(SourceLanguage S)
Definition Dwarf.h:562
TypeKind getArrayIndexTypeEncoding(SourceLanguage S)
Definition Dwarf.h:831
@ DW_DSC_range
Definition Dwarf.h:874
@ DW_DSC_label
Definition Dwarf.h:873
@ DW_FLAG_type_implementation
Definition Dwarf.h:1036
bool isC(SourceLanguage S)
Definition Dwarf.h:744
std::enable_if_t< detail::IsValidPointer< X, Y >::value, X * > dyn_extract(Y &&MD)
Extract a Value from Metadata, if any.
Definition Metadata.h:696
This is an optimization pass for GlobalISel generic memory operations.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
auto dyn_cast_if_present(const Y &Val)
dyn_cast_if_present<X> - Functionally identical to dyn_cast, except that a null (or none in the case ...
Definition Casting.h:732
Op::Description Desc
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
Definition InstrProf.h:143
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
auto reverse(ContainerTy &&C)
Definition STLExtras.h:407
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
constexpr T divideCeil(U Numerator, V Denominator)
Returns the integer ceil(Numerator / Denominator).
Definition MathExtras.h:389
DWARFExpression::Operation Op
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
#define N