1 //===-- llvm/CodeGen/DwarfUnit.cpp - Dwarf Type and Compile Units ---------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file contains support for constructing a dwarf compile unit.
11 //===----------------------------------------------------------------------===//
13 #include "DwarfUnit.h"
14 #include "AddressPool.h"
15 #include "DwarfCompileUnit.h"
16 #include "DwarfExpression.h"
17 #include "llvm/ADT/APFloat.h"
18 #include "llvm/ADT/APInt.h"
19 #include "llvm/ADT/None.h"
20 #include "llvm/ADT/StringExtras.h"
21 #include "llvm/ADT/iterator_range.h"
22 #include "llvm/CodeGen/MachineFunction.h"
23 #include "llvm/CodeGen/MachineOperand.h"
24 #include "llvm/CodeGen/TargetRegisterInfo.h"
25 #include "llvm/CodeGen/TargetSubtargetInfo.h"
26 #include "llvm/IR/Constants.h"
27 #include "llvm/IR/DataLayout.h"
28 #include "llvm/IR/GlobalValue.h"
29 #include "llvm/IR/Metadata.h"
30 #include "llvm/MC/MCAsmInfo.h"
31 #include "llvm/MC/MCContext.h"
32 #include "llvm/MC/MCDwarf.h"
33 #include "llvm/MC/MCSection.h"
34 #include "llvm/MC/MCStreamer.h"
35 #include "llvm/MC/MachineLocation.h"
36 #include "llvm/Support/Casting.h"
37 #include "llvm/Support/CommandLine.h"
38 #include "llvm/Target/TargetLoweringObjectFile.h"
46 #define DEBUG_TYPE "dwarfdebug"
48 DIEDwarfExpression::DIEDwarfExpression(const AsmPrinter
&AP
,
49 DwarfCompileUnit
&CU
, DIELoc
&DIE
)
50 : DwarfExpression(AP
.getDwarfVersion(), CU
), AP(AP
), OutDIE(DIE
) {}
52 void DIEDwarfExpression::emitOp(uint8_t Op
, const char* Comment
) {
53 CU
.addUInt(getActiveDIE(), dwarf::DW_FORM_data1
, Op
);
56 void DIEDwarfExpression::emitSigned(int64_t Value
) {
57 CU
.addSInt(getActiveDIE(), dwarf::DW_FORM_sdata
, Value
);
60 void DIEDwarfExpression::emitUnsigned(uint64_t Value
) {
61 CU
.addUInt(getActiveDIE(), dwarf::DW_FORM_udata
, Value
);
64 void DIEDwarfExpression::emitData1(uint8_t Value
) {
65 CU
.addUInt(getActiveDIE(), dwarf::DW_FORM_data1
, Value
);
68 void DIEDwarfExpression::emitBaseTypeRef(uint64_t Idx
) {
69 CU
.addBaseTypeRef(getActiveDIE(), Idx
);
72 void DIEDwarfExpression::enableTemporaryBuffer() {
73 assert(!IsBuffering
&& "Already buffering?");
77 void DIEDwarfExpression::disableTemporaryBuffer() { IsBuffering
= false; }
79 unsigned DIEDwarfExpression::getTemporaryBufferSize() {
80 return TmpDIE
.computeSize(AP
.getDwarfFormParams());
83 void DIEDwarfExpression::commitTemporaryBuffer() { OutDIE
.takeValues(TmpDIE
); }
85 bool DIEDwarfExpression::isFrameRegister(const TargetRegisterInfo
&TRI
,
86 llvm::Register MachineReg
) {
87 return MachineReg
== TRI
.getFrameRegister(*AP
.MF
);
90 DwarfUnit::DwarfUnit(dwarf::Tag UnitTag
, const DICompileUnit
*Node
,
91 AsmPrinter
*A
, DwarfDebug
*DW
, DwarfFile
*DWU
)
92 : DIEUnit(UnitTag
), CUNode(Node
), Asm(A
), DD(DW
), DU(DWU
),
93 IndexTyDie(nullptr) {}
95 DwarfTypeUnit::DwarfTypeUnit(DwarfCompileUnit
&CU
, AsmPrinter
*A
,
96 DwarfDebug
*DW
, DwarfFile
*DWU
,
97 MCDwarfDwoLineTable
*SplitLineTable
)
98 : DwarfUnit(dwarf::DW_TAG_type_unit
, CU
.getCUNode(), A
, DW
, DWU
), CU(CU
),
99 SplitLineTable(SplitLineTable
) {
102 DwarfUnit::~DwarfUnit() {
103 for (DIEBlock
*B
: DIEBlocks
)
105 for (DIELoc
*L
: DIELocs
)
109 int64_t DwarfUnit::getDefaultLowerBound() const {
110 switch (getLanguage()) {
114 // The languages below have valid values in all DWARF versions.
115 case dwarf::DW_LANG_C
:
116 case dwarf::DW_LANG_C89
:
117 case dwarf::DW_LANG_C_plus_plus
:
120 case dwarf::DW_LANG_Fortran77
:
121 case dwarf::DW_LANG_Fortran90
:
124 // The languages below have valid values only if the DWARF version >= 3.
125 case dwarf::DW_LANG_C99
:
126 case dwarf::DW_LANG_ObjC
:
127 case dwarf::DW_LANG_ObjC_plus_plus
:
128 if (DD
->getDwarfVersion() >= 3)
132 case dwarf::DW_LANG_Fortran95
:
133 if (DD
->getDwarfVersion() >= 3)
137 // Starting with DWARF v4, all defined languages have valid values.
138 case dwarf::DW_LANG_D
:
139 case dwarf::DW_LANG_Java
:
140 case dwarf::DW_LANG_Python
:
141 case dwarf::DW_LANG_UPC
:
142 if (DD
->getDwarfVersion() >= 4)
146 case dwarf::DW_LANG_Ada83
:
147 case dwarf::DW_LANG_Ada95
:
148 case dwarf::DW_LANG_Cobol74
:
149 case dwarf::DW_LANG_Cobol85
:
150 case dwarf::DW_LANG_Modula2
:
151 case dwarf::DW_LANG_Pascal83
:
152 case dwarf::DW_LANG_PLI
:
153 if (DD
->getDwarfVersion() >= 4)
157 // The languages below are new in DWARF v5.
158 case dwarf::DW_LANG_BLISS
:
159 case dwarf::DW_LANG_C11
:
160 case dwarf::DW_LANG_C_plus_plus_03
:
161 case dwarf::DW_LANG_C_plus_plus_11
:
162 case dwarf::DW_LANG_C_plus_plus_14
:
163 case dwarf::DW_LANG_Dylan
:
164 case dwarf::DW_LANG_Go
:
165 case dwarf::DW_LANG_Haskell
:
166 case dwarf::DW_LANG_OCaml
:
167 case dwarf::DW_LANG_OpenCL
:
168 case dwarf::DW_LANG_RenderScript
:
169 case dwarf::DW_LANG_Rust
:
170 case dwarf::DW_LANG_Swift
:
171 if (DD
->getDwarfVersion() >= 5)
175 case dwarf::DW_LANG_Fortran03
:
176 case dwarf::DW_LANG_Fortran08
:
177 case dwarf::DW_LANG_Julia
:
178 case dwarf::DW_LANG_Modula3
:
179 if (DD
->getDwarfVersion() >= 5)
187 /// Check whether the DIE for this MDNode can be shared across CUs.
188 bool DwarfUnit::isShareableAcrossCUs(const DINode
*D
) const {
189 // When the MDNode can be part of the type system, the DIE can be shared
191 // Combining type units and cross-CU DIE sharing is lower value (since
192 // cross-CU DIE sharing is used in LTO and removes type redundancy at that
193 // level already) but may be implementable for some value in projects
194 // building multiple independent libraries with LTO and then linking those
196 if (isDwoUnit() && !DD
->shareAcrossDWOCUs())
198 return (isa
<DIType
>(D
) ||
199 (isa
<DISubprogram
>(D
) && !cast
<DISubprogram
>(D
)->isDefinition())) &&
200 !DD
->generateTypeUnits();
203 DIE
*DwarfUnit::getDIE(const DINode
*D
) const {
204 if (isShareableAcrossCUs(D
))
205 return DU
->getDIE(D
);
206 return MDNodeToDieMap
.lookup(D
);
209 void DwarfUnit::insertDIE(const DINode
*Desc
, DIE
*D
) {
210 if (isShareableAcrossCUs(Desc
)) {
211 DU
->insertDIE(Desc
, D
);
214 MDNodeToDieMap
.insert(std::make_pair(Desc
, D
));
217 void DwarfUnit::insertDIE(DIE
*D
) {
218 MDNodeToDieMap
.insert(std::make_pair(nullptr, D
));
221 void DwarfUnit::addFlag(DIE
&Die
, dwarf::Attribute Attribute
) {
222 if (DD
->getDwarfVersion() >= 4)
223 addAttribute(Die
, Attribute
, dwarf::DW_FORM_flag_present
, DIEInteger(1));
225 addAttribute(Die
, Attribute
, dwarf::DW_FORM_flag
, DIEInteger(1));
228 void DwarfUnit::addUInt(DIEValueList
&Die
, dwarf::Attribute Attribute
,
229 Optional
<dwarf::Form
> Form
, uint64_t Integer
) {
231 Form
= DIEInteger::BestForm(false, Integer
);
232 assert(Form
!= dwarf::DW_FORM_implicit_const
&&
233 "DW_FORM_implicit_const is used only for signed integers");
234 addAttribute(Die
, Attribute
, *Form
, DIEInteger(Integer
));
237 void DwarfUnit::addUInt(DIEValueList
&Block
, dwarf::Form Form
,
239 addUInt(Block
, (dwarf::Attribute
)0, Form
, Integer
);
242 void DwarfUnit::addSInt(DIEValueList
&Die
, dwarf::Attribute Attribute
,
243 Optional
<dwarf::Form
> Form
, int64_t Integer
) {
245 Form
= DIEInteger::BestForm(true, Integer
);
246 addAttribute(Die
, Attribute
, *Form
, DIEInteger(Integer
));
249 void DwarfUnit::addSInt(DIELoc
&Die
, Optional
<dwarf::Form
> Form
,
251 addSInt(Die
, (dwarf::Attribute
)0, Form
, Integer
);
254 void DwarfUnit::addString(DIE
&Die
, dwarf::Attribute Attribute
,
256 if (CUNode
->isDebugDirectivesOnly())
259 if (DD
->useInlineStrings()) {
260 addAttribute(Die
, Attribute
, dwarf::DW_FORM_string
,
261 new (DIEValueAllocator
)
262 DIEInlineString(String
, DIEValueAllocator
));
266 isDwoUnit() ? dwarf::DW_FORM_GNU_str_index
: dwarf::DW_FORM_strp
;
268 auto StringPoolEntry
=
269 useSegmentedStringOffsetsTable() || IxForm
== dwarf::DW_FORM_GNU_str_index
270 ? DU
->getStringPool().getIndexedEntry(*Asm
, String
)
271 : DU
->getStringPool().getEntry(*Asm
, String
);
273 // For DWARF v5 and beyond, use the smallest strx? form possible.
274 if (useSegmentedStringOffsetsTable()) {
275 IxForm
= dwarf::DW_FORM_strx1
;
276 unsigned Index
= StringPoolEntry
.getIndex();
277 if (Index
> 0xffffff)
278 IxForm
= dwarf::DW_FORM_strx4
;
279 else if (Index
> 0xffff)
280 IxForm
= dwarf::DW_FORM_strx3
;
281 else if (Index
> 0xff)
282 IxForm
= dwarf::DW_FORM_strx2
;
284 addAttribute(Die
, Attribute
, IxForm
, DIEString(StringPoolEntry
));
287 void DwarfUnit::addLabel(DIEValueList
&Die
, dwarf::Attribute Attribute
,
288 dwarf::Form Form
, const MCSymbol
*Label
) {
289 addAttribute(Die
, Attribute
, Form
, DIELabel(Label
));
292 void DwarfUnit::addLabel(DIELoc
&Die
, dwarf::Form Form
, const MCSymbol
*Label
) {
293 addLabel(Die
, (dwarf::Attribute
)0, Form
, Label
);
296 void DwarfUnit::addSectionOffset(DIE
&Die
, dwarf::Attribute Attribute
,
298 addUInt(Die
, Attribute
, DD
->getDwarfSectionOffsetForm(), Integer
);
301 unsigned DwarfTypeUnit::getOrCreateSourceID(const DIFile
*File
) {
303 return getCU().getOrCreateSourceID(File
);
304 if (!UsedLineTable
) {
305 UsedLineTable
= true;
306 // This is a split type unit that needs a line table.
307 addSectionOffset(getUnitDie(), dwarf::DW_AT_stmt_list
, 0);
309 return SplitLineTable
->getFile(
310 File
->getDirectory(), File
->getFilename(), DD
->getMD5AsBytes(File
),
311 Asm
->OutContext
.getDwarfVersion(), File
->getSource());
314 void DwarfUnit::addPoolOpAddress(DIEValueList
&Die
, const MCSymbol
*Label
) {
315 bool UseAddrOffsetFormOrExpressions
=
316 DD
->useAddrOffsetForm() || DD
->useAddrOffsetExpressions();
318 const MCSymbol
*Base
= nullptr;
319 if (Label
->isInSection() && UseAddrOffsetFormOrExpressions
)
320 Base
= DD
->getSectionLabel(&Label
->getSection());
322 uint32_t Index
= DD
->getAddressPool().getIndex(Base
? Base
: Label
);
324 if (DD
->getDwarfVersion() >= 5) {
325 addUInt(Die
, dwarf::DW_FORM_data1
, dwarf::DW_OP_addrx
);
326 addUInt(Die
, dwarf::DW_FORM_addrx
, Index
);
328 addUInt(Die
, dwarf::DW_FORM_data1
, dwarf::DW_OP_GNU_addr_index
);
329 addUInt(Die
, dwarf::DW_FORM_GNU_addr_index
, Index
);
332 if (Base
&& Base
!= Label
) {
333 addUInt(Die
, dwarf::DW_FORM_data1
, dwarf::DW_OP_const4u
);
334 addLabelDelta(Die
, (dwarf::Attribute
)0, Label
, Base
);
335 addUInt(Die
, dwarf::DW_FORM_data1
, dwarf::DW_OP_plus
);
339 void DwarfUnit::addOpAddress(DIELoc
&Die
, const MCSymbol
*Sym
) {
340 if (DD
->getDwarfVersion() >= 5) {
341 addPoolOpAddress(Die
, Sym
);
345 if (DD
->useSplitDwarf()) {
346 addPoolOpAddress(Die
, Sym
);
350 addUInt(Die
, dwarf::DW_FORM_data1
, dwarf::DW_OP_addr
);
351 addLabel(Die
, dwarf::DW_FORM_addr
, Sym
);
354 void DwarfUnit::addLabelDelta(DIEValueList
&Die
, dwarf::Attribute Attribute
,
355 const MCSymbol
*Hi
, const MCSymbol
*Lo
) {
356 addAttribute(Die
, Attribute
, dwarf::DW_FORM_data4
,
357 new (DIEValueAllocator
) DIEDelta(Hi
, Lo
));
360 void DwarfUnit::addDIEEntry(DIE
&Die
, dwarf::Attribute Attribute
, DIE
&Entry
) {
361 addDIEEntry(Die
, Attribute
, DIEEntry(Entry
));
364 void DwarfUnit::addDIETypeSignature(DIE
&Die
, uint64_t Signature
) {
365 // Flag the type unit reference as a declaration so that if it contains
366 // members (implicit special members, static data member definitions, member
367 // declarations for definitions in this CU, etc) consumers don't get confused
368 // and think this is a full definition.
369 addFlag(Die
, dwarf::DW_AT_declaration
);
371 addAttribute(Die
, dwarf::DW_AT_signature
, dwarf::DW_FORM_ref_sig8
,
372 DIEInteger(Signature
));
375 void DwarfUnit::addDIEEntry(DIE
&Die
, dwarf::Attribute Attribute
,
377 const DIEUnit
*CU
= Die
.getUnit();
378 const DIEUnit
*EntryCU
= Entry
.getEntry().getUnit();
380 // We assume that Die belongs to this CU, if it is not linked to any CU yet.
381 CU
= getUnitDie().getUnit();
383 EntryCU
= getUnitDie().getUnit();
384 addAttribute(Die
, Attribute
,
385 EntryCU
== CU
? dwarf::DW_FORM_ref4
: dwarf::DW_FORM_ref_addr
,
389 DIE
&DwarfUnit::createAndAddDIE(dwarf::Tag Tag
, DIE
&Parent
, const DINode
*N
) {
390 DIE
&Die
= Parent
.addChild(DIE::get(DIEValueAllocator
, Tag
));
396 void DwarfUnit::addBlock(DIE
&Die
, dwarf::Attribute Attribute
, DIELoc
*Loc
) {
397 Loc
->computeSize(Asm
->getDwarfFormParams());
398 DIELocs
.push_back(Loc
); // Memoize so we can call the destructor later on.
399 addAttribute(Die
, Attribute
, Loc
->BestForm(DD
->getDwarfVersion()), Loc
);
402 void DwarfUnit::addBlock(DIE
&Die
, dwarf::Attribute Attribute
, dwarf::Form Form
,
404 Block
->computeSize(Asm
->getDwarfFormParams());
405 DIEBlocks
.push_back(Block
); // Memoize so we can call the destructor later on.
406 addAttribute(Die
, Attribute
, Form
, Block
);
409 void DwarfUnit::addBlock(DIE
&Die
, dwarf::Attribute Attribute
,
411 addBlock(Die
, Attribute
, Block
->BestForm(), Block
);
414 void DwarfUnit::addSourceLine(DIE
&Die
, unsigned Line
, const DIFile
*File
) {
418 unsigned FileID
= getOrCreateSourceID(File
);
419 addUInt(Die
, dwarf::DW_AT_decl_file
, None
, FileID
);
420 addUInt(Die
, dwarf::DW_AT_decl_line
, None
, Line
);
423 void DwarfUnit::addSourceLine(DIE
&Die
, const DILocalVariable
*V
) {
426 addSourceLine(Die
, V
->getLine(), V
->getFile());
429 void DwarfUnit::addSourceLine(DIE
&Die
, const DIGlobalVariable
*G
) {
432 addSourceLine(Die
, G
->getLine(), G
->getFile());
435 void DwarfUnit::addSourceLine(DIE
&Die
, const DISubprogram
*SP
) {
438 addSourceLine(Die
, SP
->getLine(), SP
->getFile());
441 void DwarfUnit::addSourceLine(DIE
&Die
, const DILabel
*L
) {
444 addSourceLine(Die
, L
->getLine(), L
->getFile());
447 void DwarfUnit::addSourceLine(DIE
&Die
, const DIType
*Ty
) {
450 addSourceLine(Die
, Ty
->getLine(), Ty
->getFile());
453 void DwarfUnit::addSourceLine(DIE
&Die
, const DIObjCProperty
*Ty
) {
456 addSourceLine(Die
, Ty
->getLine(), Ty
->getFile());
459 void DwarfUnit::addConstantFPValue(DIE
&Die
, const ConstantFP
*CFP
) {
460 // Pass this down to addConstantValue as an unsigned bag of bits.
461 addConstantValue(Die
, CFP
->getValueAPF().bitcastToAPInt(), true);
464 void DwarfUnit::addConstantValue(DIE
&Die
, const ConstantInt
*CI
,
466 addConstantValue(Die
, CI
->getValue(), Ty
);
469 void DwarfUnit::addConstantValue(DIE
&Die
, uint64_t Val
, const DIType
*Ty
) {
470 addConstantValue(Die
, DD
->isUnsignedDIType(Ty
), Val
);
473 void DwarfUnit::addConstantValue(DIE
&Die
, bool Unsigned
, uint64_t Val
) {
474 // FIXME: This is a bit conservative/simple - it emits negative values always
475 // sign extended to 64 bits rather than minimizing the number of bytes.
476 addUInt(Die
, dwarf::DW_AT_const_value
,
477 Unsigned
? dwarf::DW_FORM_udata
: dwarf::DW_FORM_sdata
, Val
);
480 void DwarfUnit::addConstantValue(DIE
&Die
, const APInt
&Val
, const DIType
*Ty
) {
481 addConstantValue(Die
, Val
, DD
->isUnsignedDIType(Ty
));
484 void DwarfUnit::addConstantValue(DIE
&Die
, const APInt
&Val
, bool Unsigned
) {
485 unsigned CIBitWidth
= Val
.getBitWidth();
486 if (CIBitWidth
<= 64) {
487 addConstantValue(Die
, Unsigned
,
488 Unsigned
? Val
.getZExtValue() : Val
.getSExtValue());
492 DIEBlock
*Block
= new (DIEValueAllocator
) DIEBlock
;
494 // Get the raw data form of the large APInt.
495 const uint64_t *Ptr64
= Val
.getRawData();
497 int NumBytes
= Val
.getBitWidth() / 8; // 8 bits per byte.
498 bool LittleEndian
= Asm
->getDataLayout().isLittleEndian();
500 // Output the constant to DWARF one byte at a time.
501 for (int i
= 0; i
< NumBytes
; i
++) {
504 c
= Ptr64
[i
/ 8] >> (8 * (i
& 7));
506 c
= Ptr64
[(NumBytes
- 1 - i
) / 8] >> (8 * ((NumBytes
- 1 - i
) & 7));
507 addUInt(*Block
, dwarf::DW_FORM_data1
, c
);
510 addBlock(Die
, dwarf::DW_AT_const_value
, Block
);
513 void DwarfUnit::addLinkageName(DIE
&Die
, StringRef LinkageName
) {
514 if (!LinkageName
.empty())
516 DD
->getDwarfVersion() >= 4 ? dwarf::DW_AT_linkage_name
517 : dwarf::DW_AT_MIPS_linkage_name
,
518 GlobalValue::dropLLVMManglingEscape(LinkageName
));
521 void DwarfUnit::addTemplateParams(DIE
&Buffer
, DINodeArray TParams
) {
522 // Add template parameters.
523 for (const auto *Element
: TParams
) {
524 if (auto *TTP
= dyn_cast
<DITemplateTypeParameter
>(Element
))
525 constructTemplateTypeParameterDIE(Buffer
, TTP
);
526 else if (auto *TVP
= dyn_cast
<DITemplateValueParameter
>(Element
))
527 constructTemplateValueParameterDIE(Buffer
, TVP
);
531 /// Add thrown types.
532 void DwarfUnit::addThrownTypes(DIE
&Die
, DINodeArray ThrownTypes
) {
533 for (const auto *Ty
: ThrownTypes
) {
534 DIE
&TT
= createAndAddDIE(dwarf::DW_TAG_thrown_type
, Die
);
535 addType(TT
, cast
<DIType
>(Ty
));
539 void DwarfUnit::addAccess(DIE
&Die
, DINode::DIFlags Flags
) {
540 if ((Flags
& DINode::FlagAccessibility
) == DINode::FlagProtected
)
541 addUInt(Die
, dwarf::DW_AT_accessibility
, dwarf::DW_FORM_data1
,
542 dwarf::DW_ACCESS_protected
);
543 else if ((Flags
& DINode::FlagAccessibility
) == DINode::FlagPrivate
)
544 addUInt(Die
, dwarf::DW_AT_accessibility
, dwarf::DW_FORM_data1
,
545 dwarf::DW_ACCESS_private
);
546 else if ((Flags
& DINode::FlagAccessibility
) == DINode::FlagPublic
)
547 addUInt(Die
, dwarf::DW_AT_accessibility
, dwarf::DW_FORM_data1
,
548 dwarf::DW_ACCESS_public
);
551 DIE
*DwarfUnit::getOrCreateContextDIE(const DIScope
*Context
) {
552 if (!Context
|| isa
<DIFile
>(Context
))
553 return &getUnitDie();
554 if (auto *T
= dyn_cast
<DIType
>(Context
))
555 return getOrCreateTypeDIE(T
);
556 if (auto *NS
= dyn_cast
<DINamespace
>(Context
))
557 return getOrCreateNameSpace(NS
);
558 if (auto *SP
= dyn_cast
<DISubprogram
>(Context
))
559 return getOrCreateSubprogramDIE(SP
);
560 if (auto *M
= dyn_cast
<DIModule
>(Context
))
561 return getOrCreateModule(M
);
562 return getDIE(Context
);
565 DIE
*DwarfUnit::createTypeDIE(const DICompositeType
*Ty
) {
566 auto *Context
= Ty
->getScope();
567 DIE
*ContextDIE
= getOrCreateContextDIE(Context
);
569 if (DIE
*TyDIE
= getDIE(Ty
))
573 DIE
&TyDIE
= createAndAddDIE(Ty
->getTag(), *ContextDIE
, Ty
);
575 constructTypeDIE(TyDIE
, cast
<DICompositeType
>(Ty
));
577 updateAcceleratorTables(Context
, Ty
, TyDIE
);
581 DIE
*DwarfUnit::createTypeDIE(const DIScope
*Context
, DIE
&ContextDIE
,
584 DIE
&TyDIE
= createAndAddDIE(Ty
->getTag(), ContextDIE
, Ty
);
586 updateAcceleratorTables(Context
, Ty
, TyDIE
);
588 if (auto *BT
= dyn_cast
<DIBasicType
>(Ty
))
589 constructTypeDIE(TyDIE
, BT
);
590 else if (auto *ST
= dyn_cast
<DIStringType
>(Ty
))
591 constructTypeDIE(TyDIE
, ST
);
592 else if (auto *STy
= dyn_cast
<DISubroutineType
>(Ty
))
593 constructTypeDIE(TyDIE
, STy
);
594 else if (auto *CTy
= dyn_cast
<DICompositeType
>(Ty
)) {
595 if (DD
->generateTypeUnits() && !Ty
->isForwardDecl() &&
596 (Ty
->getRawName() || CTy
->getRawIdentifier())) {
597 // Skip updating the accelerator tables since this is not the full type.
598 if (MDString
*TypeId
= CTy
->getRawIdentifier())
599 DD
->addDwarfTypeUnitType(getCU(), TypeId
->getString(), TyDIE
, CTy
);
601 auto X
= DD
->enterNonTypeUnitContext();
602 finishNonUnitTypeDIE(TyDIE
, CTy
);
606 constructTypeDIE(TyDIE
, CTy
);
608 constructTypeDIE(TyDIE
, cast
<DIDerivedType
>(Ty
));
614 DIE
*DwarfUnit::getOrCreateTypeDIE(const MDNode
*TyNode
) {
618 auto *Ty
= cast
<DIType
>(TyNode
);
620 // DW_TAG_restrict_type is not supported in DWARF2
621 if (Ty
->getTag() == dwarf::DW_TAG_restrict_type
&& DD
->getDwarfVersion() <= 2)
622 return getOrCreateTypeDIE(cast
<DIDerivedType
>(Ty
)->getBaseType());
624 // DW_TAG_atomic_type is not supported in DWARF < 5
625 if (Ty
->getTag() == dwarf::DW_TAG_atomic_type
&& DD
->getDwarfVersion() < 5)
626 return getOrCreateTypeDIE(cast
<DIDerivedType
>(Ty
)->getBaseType());
628 // Construct the context before querying for the existence of the DIE in case
629 // such construction creates the DIE.
630 auto *Context
= Ty
->getScope();
631 DIE
*ContextDIE
= getOrCreateContextDIE(Context
);
634 if (DIE
*TyDIE
= getDIE(Ty
))
637 return static_cast<DwarfUnit
*>(ContextDIE
->getUnit())
638 ->createTypeDIE(Context
, *ContextDIE
, Ty
);
641 void DwarfUnit::updateAcceleratorTables(const DIScope
*Context
,
642 const DIType
*Ty
, const DIE
&TyDIE
) {
643 if (!Ty
->getName().empty() && !Ty
->isForwardDecl()) {
644 bool IsImplementation
= false;
645 if (auto *CT
= dyn_cast
<DICompositeType
>(Ty
)) {
646 // A runtime language of 0 actually means C/C++ and that any
647 // non-negative value is some version of Objective-C/C++.
648 IsImplementation
= CT
->getRuntimeLang() == 0 || CT
->isObjcClassComplete();
650 unsigned Flags
= IsImplementation
? dwarf::DW_FLAG_type_implementation
: 0;
651 DD
->addAccelType(*CUNode
, Ty
->getName(), TyDIE
, Flags
);
653 if (!Context
|| isa
<DICompileUnit
>(Context
) || isa
<DIFile
>(Context
) ||
654 isa
<DINamespace
>(Context
) || isa
<DICommonBlock
>(Context
))
655 addGlobalType(Ty
, TyDIE
, Context
);
659 void DwarfUnit::addType(DIE
&Entity
, const DIType
*Ty
,
660 dwarf::Attribute Attribute
) {
661 assert(Ty
&& "Trying to add a type that doesn't exist?");
662 addDIEEntry(Entity
, Attribute
, DIEEntry(*getOrCreateTypeDIE(Ty
)));
665 std::string
DwarfUnit::getParentContextString(const DIScope
*Context
) const {
669 // FIXME: Decide whether to implement this for non-C++ languages.
670 if (!dwarf::isCPlusPlus((dwarf::SourceLanguage
)getLanguage()))
674 SmallVector
<const DIScope
*, 1> Parents
;
675 while (!isa
<DICompileUnit
>(Context
)) {
676 Parents
.push_back(Context
);
677 if (const DIScope
*S
= Context
->getScope())
680 // Structure, etc types will have a NULL context if they're at the top
685 // Reverse iterate over our list to go from the outermost construct to the
687 for (const DIScope
*Ctx
: llvm::reverse(Parents
)) {
688 StringRef Name
= Ctx
->getName();
689 if (Name
.empty() && isa
<DINamespace
>(Ctx
))
690 Name
= "(anonymous namespace)";
699 void DwarfUnit::constructTypeDIE(DIE
&Buffer
, const DIBasicType
*BTy
) {
700 // Get core information.
701 StringRef Name
= BTy
->getName();
702 // Add name if not anonymous or intermediate type.
704 addString(Buffer
, dwarf::DW_AT_name
, Name
);
706 // An unspecified type only has a name attribute.
707 if (BTy
->getTag() == dwarf::DW_TAG_unspecified_type
)
710 if (BTy
->getTag() != dwarf::DW_TAG_string_type
)
711 addUInt(Buffer
, dwarf::DW_AT_encoding
, dwarf::DW_FORM_data1
,
714 uint64_t Size
= BTy
->getSizeInBits() >> 3;
715 addUInt(Buffer
, dwarf::DW_AT_byte_size
, None
, Size
);
717 if (BTy
->isBigEndian())
718 addUInt(Buffer
, dwarf::DW_AT_endianity
, None
, dwarf::DW_END_big
);
719 else if (BTy
->isLittleEndian())
720 addUInt(Buffer
, dwarf::DW_AT_endianity
, None
, dwarf::DW_END_little
);
723 void DwarfUnit::constructTypeDIE(DIE
&Buffer
, const DIStringType
*STy
) {
724 // Get core information.
725 StringRef Name
= STy
->getName();
726 // Add name if not anonymous or intermediate type.
728 addString(Buffer
, dwarf::DW_AT_name
, Name
);
730 if (DIVariable
*Var
= STy
->getStringLength()) {
731 if (auto *VarDIE
= getDIE(Var
))
732 addDIEEntry(Buffer
, dwarf::DW_AT_string_length
, *VarDIE
);
733 } else if (DIExpression
*Expr
= STy
->getStringLengthExp()) {
734 DIELoc
*Loc
= new (DIEValueAllocator
) DIELoc
;
735 DIEDwarfExpression
DwarfExpr(*Asm
, getCU(), *Loc
);
736 // This is to describe the memory location of the
737 // length of a Fortran deferred length string, so
738 // lock it down as such.
739 DwarfExpr
.setMemoryLocationKind();
740 DwarfExpr
.addExpression(Expr
);
741 addBlock(Buffer
, dwarf::DW_AT_string_length
, DwarfExpr
.finalize());
743 uint64_t Size
= STy
->getSizeInBits() >> 3;
744 addUInt(Buffer
, dwarf::DW_AT_byte_size
, None
, Size
);
747 if (STy
->getEncoding()) {
748 // For eventual Unicode support.
749 addUInt(Buffer
, dwarf::DW_AT_encoding
, dwarf::DW_FORM_data1
,
754 void DwarfUnit::constructTypeDIE(DIE
&Buffer
, const DIDerivedType
*DTy
) {
755 // Get core information.
756 StringRef Name
= DTy
->getName();
757 uint64_t Size
= DTy
->getSizeInBits() >> 3;
758 uint16_t Tag
= Buffer
.getTag();
760 // Map to main type, void will not have a type.
761 const DIType
*FromTy
= DTy
->getBaseType();
763 addType(Buffer
, FromTy
);
765 // Add name if not anonymous or intermediate type.
767 addString(Buffer
, dwarf::DW_AT_name
, Name
);
769 addAnnotation(Buffer
, DTy
->getAnnotations());
771 // If alignment is specified for a typedef , create and insert DW_AT_alignment
772 // attribute in DW_TAG_typedef DIE.
773 if (Tag
== dwarf::DW_TAG_typedef
&& DD
->getDwarfVersion() >= 5) {
774 uint32_t AlignInBytes
= DTy
->getAlignInBytes();
775 if (AlignInBytes
> 0)
776 addUInt(Buffer
, dwarf::DW_AT_alignment
, dwarf::DW_FORM_udata
,
780 // Add size if non-zero (derived types might be zero-sized.)
781 if (Size
&& Tag
!= dwarf::DW_TAG_pointer_type
782 && Tag
!= dwarf::DW_TAG_ptr_to_member_type
783 && Tag
!= dwarf::DW_TAG_reference_type
784 && Tag
!= dwarf::DW_TAG_rvalue_reference_type
)
785 addUInt(Buffer
, dwarf::DW_AT_byte_size
, None
, Size
);
787 if (Tag
== dwarf::DW_TAG_ptr_to_member_type
)
788 addDIEEntry(Buffer
, dwarf::DW_AT_containing_type
,
789 *getOrCreateTypeDIE(cast
<DIDerivedType
>(DTy
)->getClassType()));
790 // Add source line info if available and TyDesc is not a forward declaration.
791 if (!DTy
->isForwardDecl())
792 addSourceLine(Buffer
, DTy
);
794 // If DWARF address space value is other than None, add it. The IR
795 // verifier checks that DWARF address space only exists for pointer
796 // or reference types.
797 if (DTy
->getDWARFAddressSpace())
798 addUInt(Buffer
, dwarf::DW_AT_address_class
, dwarf::DW_FORM_data4
,
799 DTy
->getDWARFAddressSpace().getValue());
802 void DwarfUnit::constructSubprogramArguments(DIE
&Buffer
, DITypeRefArray Args
) {
803 for (unsigned i
= 1, N
= Args
.size(); i
< N
; ++i
) {
804 const DIType
*Ty
= Args
[i
];
806 assert(i
== N
-1 && "Unspecified parameter must be the last argument");
807 createAndAddDIE(dwarf::DW_TAG_unspecified_parameters
, Buffer
);
809 DIE
&Arg
= createAndAddDIE(dwarf::DW_TAG_formal_parameter
, Buffer
);
811 if (Ty
->isArtificial())
812 addFlag(Arg
, dwarf::DW_AT_artificial
);
817 void DwarfUnit::constructTypeDIE(DIE
&Buffer
, const DISubroutineType
*CTy
) {
818 // Add return type. A void return won't have a type.
819 auto Elements
= cast
<DISubroutineType
>(CTy
)->getTypeArray();
821 if (auto RTy
= Elements
[0])
822 addType(Buffer
, RTy
);
824 bool isPrototyped
= true;
825 if (Elements
.size() == 2 && !Elements
[1])
826 isPrototyped
= false;
828 constructSubprogramArguments(Buffer
, Elements
);
830 // Add prototype flag if we're dealing with a C language and the function has
832 uint16_t Language
= getLanguage();
834 (Language
== dwarf::DW_LANG_C89
|| Language
== dwarf::DW_LANG_C99
||
835 Language
== dwarf::DW_LANG_ObjC
))
836 addFlag(Buffer
, dwarf::DW_AT_prototyped
);
838 // Add a DW_AT_calling_convention if this has an explicit convention.
839 if (CTy
->getCC() && CTy
->getCC() != dwarf::DW_CC_normal
)
840 addUInt(Buffer
, dwarf::DW_AT_calling_convention
, dwarf::DW_FORM_data1
,
843 if (CTy
->isLValueReference())
844 addFlag(Buffer
, dwarf::DW_AT_reference
);
846 if (CTy
->isRValueReference())
847 addFlag(Buffer
, dwarf::DW_AT_rvalue_reference
);
850 void DwarfUnit::addAnnotation(DIE
&Buffer
, DINodeArray Annotations
) {
854 for (const Metadata
*Annotation
: Annotations
->operands()) {
855 const MDNode
*MD
= cast
<MDNode
>(Annotation
);
856 const MDString
*Name
= cast
<MDString
>(MD
->getOperand(0));
857 const auto &Value
= MD
->getOperand(1);
859 DIE
&AnnotationDie
= createAndAddDIE(dwarf::DW_TAG_LLVM_annotation
, Buffer
);
860 addString(AnnotationDie
, dwarf::DW_AT_name
, Name
->getString());
861 if (const auto *Data
= dyn_cast
<MDString
>(Value
))
862 addString(AnnotationDie
, dwarf::DW_AT_const_value
, Data
->getString());
863 else if (const auto *Data
= dyn_cast
<ConstantAsMetadata
>(Value
))
864 addConstantValue(AnnotationDie
, Data
->getValue()->getUniqueInteger(),
867 assert(false && "Unsupported annotation value type");
871 void DwarfUnit::constructTypeDIE(DIE
&Buffer
, const DICompositeType
*CTy
) {
872 // Add name if not anonymous or intermediate type.
873 StringRef Name
= CTy
->getName();
875 uint64_t Size
= CTy
->getSizeInBits() >> 3;
876 uint16_t Tag
= Buffer
.getTag();
879 case dwarf::DW_TAG_array_type
:
880 constructArrayTypeDIE(Buffer
, CTy
);
882 case dwarf::DW_TAG_enumeration_type
:
883 constructEnumTypeDIE(Buffer
, CTy
);
885 case dwarf::DW_TAG_variant_part
:
886 case dwarf::DW_TAG_structure_type
:
887 case dwarf::DW_TAG_union_type
:
888 case dwarf::DW_TAG_class_type
:
889 case dwarf::DW_TAG_namelist
: {
890 // Emit the discriminator for a variant part.
891 DIDerivedType
*Discriminator
= nullptr;
892 if (Tag
== dwarf::DW_TAG_variant_part
) {
893 Discriminator
= CTy
->getDiscriminator();
896 // If the variant part has a discriminant, the discriminant is
897 // represented by a separate debugging information entry which is
898 // a child of the variant part entry.
899 DIE
&DiscMember
= constructMemberDIE(Buffer
, Discriminator
);
900 addDIEEntry(Buffer
, dwarf::DW_AT_discr
, DiscMember
);
904 // Add template parameters to a class, structure or union types.
905 if (Tag
== dwarf::DW_TAG_class_type
||
906 Tag
== dwarf::DW_TAG_structure_type
|| Tag
== dwarf::DW_TAG_union_type
)
907 addTemplateParams(Buffer
, CTy
->getTemplateParams());
909 // Add elements to structure type.
910 DINodeArray Elements
= CTy
->getElements();
911 for (const auto *Element
: Elements
) {
914 if (auto *SP
= dyn_cast
<DISubprogram
>(Element
))
915 getOrCreateSubprogramDIE(SP
);
916 else if (auto *DDTy
= dyn_cast
<DIDerivedType
>(Element
)) {
917 if (DDTy
->getTag() == dwarf::DW_TAG_friend
) {
918 DIE
&ElemDie
= createAndAddDIE(dwarf::DW_TAG_friend
, Buffer
);
919 addType(ElemDie
, DDTy
->getBaseType(), dwarf::DW_AT_friend
);
920 } else if (DDTy
->isStaticMember()) {
921 getOrCreateStaticMemberDIE(DDTy
);
922 } else if (Tag
== dwarf::DW_TAG_variant_part
) {
923 // When emitting a variant part, wrap each member in
925 DIE
&Variant
= createAndAddDIE(dwarf::DW_TAG_variant
, Buffer
);
926 if (const ConstantInt
*CI
=
927 dyn_cast_or_null
<ConstantInt
>(DDTy
->getDiscriminantValue())) {
928 if (DD
->isUnsignedDIType(Discriminator
->getBaseType()))
929 addUInt(Variant
, dwarf::DW_AT_discr_value
, None
, CI
->getZExtValue());
931 addSInt(Variant
, dwarf::DW_AT_discr_value
, None
, CI
->getSExtValue());
933 constructMemberDIE(Variant
, DDTy
);
935 constructMemberDIE(Buffer
, DDTy
);
937 } else if (auto *Property
= dyn_cast
<DIObjCProperty
>(Element
)) {
938 DIE
&ElemDie
= createAndAddDIE(Property
->getTag(), Buffer
);
939 StringRef PropertyName
= Property
->getName();
940 addString(ElemDie
, dwarf::DW_AT_APPLE_property_name
, PropertyName
);
941 if (Property
->getType())
942 addType(ElemDie
, Property
->getType());
943 addSourceLine(ElemDie
, Property
);
944 StringRef GetterName
= Property
->getGetterName();
945 if (!GetterName
.empty())
946 addString(ElemDie
, dwarf::DW_AT_APPLE_property_getter
, GetterName
);
947 StringRef SetterName
= Property
->getSetterName();
948 if (!SetterName
.empty())
949 addString(ElemDie
, dwarf::DW_AT_APPLE_property_setter
, SetterName
);
950 if (unsigned PropertyAttributes
= Property
->getAttributes())
951 addUInt(ElemDie
, dwarf::DW_AT_APPLE_property_attribute
, None
,
953 } else if (auto *Composite
= dyn_cast
<DICompositeType
>(Element
)) {
954 if (Composite
->getTag() == dwarf::DW_TAG_variant_part
) {
955 DIE
&VariantPart
= createAndAddDIE(Composite
->getTag(), Buffer
);
956 constructTypeDIE(VariantPart
, Composite
);
958 } else if (Tag
== dwarf::DW_TAG_namelist
) {
959 auto *Var
= dyn_cast
<DINode
>(Element
);
960 auto *VarDIE
= getDIE(Var
);
962 DIE
&ItemDie
= createAndAddDIE(dwarf::DW_TAG_namelist_item
, Buffer
);
963 addDIEEntry(ItemDie
, dwarf::DW_AT_namelist_item
, *VarDIE
);
968 if (CTy
->isAppleBlockExtension())
969 addFlag(Buffer
, dwarf::DW_AT_APPLE_block
);
971 if (CTy
->getExportSymbols())
972 addFlag(Buffer
, dwarf::DW_AT_export_symbols
);
974 // This is outside the DWARF spec, but GDB expects a DW_AT_containing_type
975 // inside C++ composite types to point to the base class with the vtable.
976 // Rust uses DW_AT_containing_type to link a vtable to the type
977 // for which it was created.
978 if (auto *ContainingType
= CTy
->getVTableHolder())
979 addDIEEntry(Buffer
, dwarf::DW_AT_containing_type
,
980 *getOrCreateTypeDIE(ContainingType
));
982 if (CTy
->isObjcClassComplete())
983 addFlag(Buffer
, dwarf::DW_AT_APPLE_objc_complete_type
);
985 // Add the type's non-standard calling convention.
986 // DW_CC_pass_by_value/DW_CC_pass_by_reference are introduced in DWARF 5.
987 if (!Asm
->TM
.Options
.DebugStrictDwarf
|| DD
->getDwarfVersion() >= 5) {
989 if (CTy
->isTypePassByValue())
990 CC
= dwarf::DW_CC_pass_by_value
;
991 else if (CTy
->isTypePassByReference())
992 CC
= dwarf::DW_CC_pass_by_reference
;
994 addUInt(Buffer
, dwarf::DW_AT_calling_convention
, dwarf::DW_FORM_data1
,
1003 // Add name if not anonymous or intermediate type.
1005 addString(Buffer
, dwarf::DW_AT_name
, Name
);
1007 addAnnotation(Buffer
, CTy
->getAnnotations());
1009 if (Tag
== dwarf::DW_TAG_enumeration_type
||
1010 Tag
== dwarf::DW_TAG_class_type
|| Tag
== dwarf::DW_TAG_structure_type
||
1011 Tag
== dwarf::DW_TAG_union_type
) {
1012 // Add size if non-zero (derived types might be zero-sized.)
1013 // Ignore the size if it's a non-enum forward decl.
1014 // TODO: Do we care about size for enum forward declarations?
1016 (!CTy
->isForwardDecl() || Tag
== dwarf::DW_TAG_enumeration_type
))
1017 addUInt(Buffer
, dwarf::DW_AT_byte_size
, None
, Size
);
1018 else if (!CTy
->isForwardDecl())
1019 // Add zero size if it is not a forward declaration.
1020 addUInt(Buffer
, dwarf::DW_AT_byte_size
, None
, 0);
1022 // If we're a forward decl, say so.
1023 if (CTy
->isForwardDecl())
1024 addFlag(Buffer
, dwarf::DW_AT_declaration
);
1026 // Add accessibility info if available.
1027 addAccess(Buffer
, CTy
->getFlags());
1029 // Add source line info if available.
1030 if (!CTy
->isForwardDecl())
1031 addSourceLine(Buffer
, CTy
);
1033 // No harm in adding the runtime language to the declaration.
1034 unsigned RLang
= CTy
->getRuntimeLang();
1036 addUInt(Buffer
, dwarf::DW_AT_APPLE_runtime_class
, dwarf::DW_FORM_data1
,
1039 // Add align info if available.
1040 if (uint32_t AlignInBytes
= CTy
->getAlignInBytes())
1041 addUInt(Buffer
, dwarf::DW_AT_alignment
, dwarf::DW_FORM_udata
,
1046 void DwarfUnit::constructTemplateTypeParameterDIE(
1047 DIE
&Buffer
, const DITemplateTypeParameter
*TP
) {
1049 createAndAddDIE(dwarf::DW_TAG_template_type_parameter
, Buffer
);
1050 // Add the type if it exists, it could be void and therefore no type.
1052 addType(ParamDIE
, TP
->getType());
1053 if (!TP
->getName().empty())
1054 addString(ParamDIE
, dwarf::DW_AT_name
, TP
->getName());
1055 if (TP
->isDefault() && (DD
->getDwarfVersion() >= 5))
1056 addFlag(ParamDIE
, dwarf::DW_AT_default_value
);
1059 void DwarfUnit::constructTemplateValueParameterDIE(
1060 DIE
&Buffer
, const DITemplateValueParameter
*VP
) {
1061 DIE
&ParamDIE
= createAndAddDIE(VP
->getTag(), Buffer
);
1063 // Add the type if there is one, template template and template parameter
1064 // packs will not have a type.
1065 if (VP
->getTag() == dwarf::DW_TAG_template_value_parameter
)
1066 addType(ParamDIE
, VP
->getType());
1067 if (!VP
->getName().empty())
1068 addString(ParamDIE
, dwarf::DW_AT_name
, VP
->getName());
1069 if (VP
->isDefault() && (DD
->getDwarfVersion() >= 5))
1070 addFlag(ParamDIE
, dwarf::DW_AT_default_value
);
1071 if (Metadata
*Val
= VP
->getValue()) {
1072 if (ConstantInt
*CI
= mdconst::dyn_extract
<ConstantInt
>(Val
))
1073 addConstantValue(ParamDIE
, CI
, VP
->getType());
1074 else if (GlobalValue
*GV
= mdconst::dyn_extract
<GlobalValue
>(Val
)) {
1075 // We cannot describe the location of dllimport'd entities: the
1076 // computation of their address requires loads from the IAT.
1077 if (!GV
->hasDLLImportStorageClass()) {
1078 // For declaration non-type template parameters (such as global values
1080 DIELoc
*Loc
= new (DIEValueAllocator
) DIELoc
;
1081 addOpAddress(*Loc
, Asm
->getSymbol(GV
));
1082 // Emit DW_OP_stack_value to use the address as the immediate value of
1083 // the parameter, rather than a pointer to it.
1084 addUInt(*Loc
, dwarf::DW_FORM_data1
, dwarf::DW_OP_stack_value
);
1085 addBlock(ParamDIE
, dwarf::DW_AT_location
, Loc
);
1087 } else if (VP
->getTag() == dwarf::DW_TAG_GNU_template_template_param
) {
1088 assert(isa
<MDString
>(Val
));
1089 addString(ParamDIE
, dwarf::DW_AT_GNU_template_name
,
1090 cast
<MDString
>(Val
)->getString());
1091 } else if (VP
->getTag() == dwarf::DW_TAG_GNU_template_parameter_pack
) {
1092 addTemplateParams(ParamDIE
, cast
<MDTuple
>(Val
));
1097 DIE
*DwarfUnit::getOrCreateNameSpace(const DINamespace
*NS
) {
1098 // Construct the context before querying for the existence of the DIE in case
1099 // such construction creates the DIE.
1100 DIE
*ContextDIE
= getOrCreateContextDIE(NS
->getScope());
1102 if (DIE
*NDie
= getDIE(NS
))
1104 DIE
&NDie
= createAndAddDIE(dwarf::DW_TAG_namespace
, *ContextDIE
, NS
);
1106 StringRef Name
= NS
->getName();
1108 addString(NDie
, dwarf::DW_AT_name
, NS
->getName());
1110 Name
= "(anonymous namespace)";
1111 DD
->addAccelNamespace(*CUNode
, Name
, NDie
);
1112 addGlobalName(Name
, NDie
, NS
->getScope());
1113 if (NS
->getExportSymbols())
1114 addFlag(NDie
, dwarf::DW_AT_export_symbols
);
1118 DIE
*DwarfUnit::getOrCreateModule(const DIModule
*M
) {
1119 // Construct the context before querying for the existence of the DIE in case
1120 // such construction creates the DIE.
1121 DIE
*ContextDIE
= getOrCreateContextDIE(M
->getScope());
1123 if (DIE
*MDie
= getDIE(M
))
1125 DIE
&MDie
= createAndAddDIE(dwarf::DW_TAG_module
, *ContextDIE
, M
);
1127 if (!M
->getName().empty()) {
1128 addString(MDie
, dwarf::DW_AT_name
, M
->getName());
1129 addGlobalName(M
->getName(), MDie
, M
->getScope());
1131 if (!M
->getConfigurationMacros().empty())
1132 addString(MDie
, dwarf::DW_AT_LLVM_config_macros
,
1133 M
->getConfigurationMacros());
1134 if (!M
->getIncludePath().empty())
1135 addString(MDie
, dwarf::DW_AT_LLVM_include_path
, M
->getIncludePath());
1136 if (!M
->getAPINotesFile().empty())
1137 addString(MDie
, dwarf::DW_AT_LLVM_apinotes
, M
->getAPINotesFile());
1139 addUInt(MDie
, dwarf::DW_AT_decl_file
, None
,
1140 getOrCreateSourceID(M
->getFile()));
1142 addUInt(MDie
, dwarf::DW_AT_decl_line
, None
, M
->getLineNo());
1144 addFlag(MDie
, dwarf::DW_AT_declaration
);
1149 DIE
*DwarfUnit::getOrCreateSubprogramDIE(const DISubprogram
*SP
, bool Minimal
) {
1150 // Construct the context before querying for the existence of the DIE in case
1151 // such construction creates the DIE (as is the case for member function
1154 Minimal
? &getUnitDie() : getOrCreateContextDIE(SP
->getScope());
1156 if (DIE
*SPDie
= getDIE(SP
))
1159 if (auto *SPDecl
= SP
->getDeclaration()) {
1161 // Add subprogram definitions to the CU die directly.
1162 ContextDIE
= &getUnitDie();
1163 // Build the decl now to ensure it precedes the definition.
1164 getOrCreateSubprogramDIE(SPDecl
);
1168 // DW_TAG_inlined_subroutine may refer to this DIE.
1169 DIE
&SPDie
= createAndAddDIE(dwarf::DW_TAG_subprogram
, *ContextDIE
, SP
);
1171 // Stop here and fill this in later, depending on whether or not this
1172 // subprogram turns out to have inlined instances or not.
1173 if (SP
->isDefinition())
1176 static_cast<DwarfUnit
*>(SPDie
.getUnit())
1177 ->applySubprogramAttributes(SP
, SPDie
);
1181 bool DwarfUnit::applySubprogramDefinitionAttributes(const DISubprogram
*SP
,
1182 DIE
&SPDie
, bool Minimal
) {
1183 DIE
*DeclDie
= nullptr;
1184 StringRef DeclLinkageName
;
1185 if (auto *SPDecl
= SP
->getDeclaration()) {
1187 DITypeRefArray DeclArgs
, DefinitionArgs
;
1188 DeclArgs
= SPDecl
->getType()->getTypeArray();
1189 DefinitionArgs
= SP
->getType()->getTypeArray();
1191 if (DeclArgs
.size() && DefinitionArgs
.size())
1192 if (DefinitionArgs
[0] != nullptr && DeclArgs
[0] != DefinitionArgs
[0])
1193 addType(SPDie
, DefinitionArgs
[0]);
1195 DeclDie
= getDIE(SPDecl
);
1196 assert(DeclDie
&& "This DIE should've already been constructed when the "
1197 "definition DIE was created in "
1198 "getOrCreateSubprogramDIE");
1199 // Look at the Decl's linkage name only if we emitted it.
1200 if (DD
->useAllLinkageNames())
1201 DeclLinkageName
= SPDecl
->getLinkageName();
1202 unsigned DeclID
= getOrCreateSourceID(SPDecl
->getFile());
1203 unsigned DefID
= getOrCreateSourceID(SP
->getFile());
1204 if (DeclID
!= DefID
)
1205 addUInt(SPDie
, dwarf::DW_AT_decl_file
, None
, DefID
);
1207 if (SP
->getLine() != SPDecl
->getLine())
1208 addUInt(SPDie
, dwarf::DW_AT_decl_line
, None
, SP
->getLine());
1212 // Add function template parameters.
1213 addTemplateParams(SPDie
, SP
->getTemplateParams());
1215 // Add the linkage name if we have one and it isn't in the Decl.
1216 StringRef LinkageName
= SP
->getLinkageName();
1217 assert(((LinkageName
.empty() || DeclLinkageName
.empty()) ||
1218 LinkageName
== DeclLinkageName
) &&
1219 "decl has a linkage name and it is different");
1220 if (DeclLinkageName
.empty() &&
1221 // Always emit it for abstract subprograms.
1222 (DD
->useAllLinkageNames() || DU
->getAbstractSPDies().lookup(SP
)))
1223 addLinkageName(SPDie
, LinkageName
);
1228 // Refer to the function declaration where all the other attributes will be
1230 addDIEEntry(SPDie
, dwarf::DW_AT_specification
, *DeclDie
);
1234 void DwarfUnit::applySubprogramAttributes(const DISubprogram
*SP
, DIE
&SPDie
,
1235 bool SkipSPAttributes
) {
1236 // If -fdebug-info-for-profiling is enabled, need to emit the subprogram
1237 // and its source location.
1238 bool SkipSPSourceLocation
= SkipSPAttributes
&&
1239 !CUNode
->getDebugInfoForProfiling();
1240 if (!SkipSPSourceLocation
)
1241 if (applySubprogramDefinitionAttributes(SP
, SPDie
, SkipSPAttributes
))
1244 // Constructors and operators for anonymous aggregates do not have names.
1245 if (!SP
->getName().empty())
1246 addString(SPDie
, dwarf::DW_AT_name
, SP
->getName());
1248 addAnnotation(SPDie
, SP
->getAnnotations());
1250 if (!SkipSPSourceLocation
)
1251 addSourceLine(SPDie
, SP
);
1253 // Skip the rest of the attributes under -gmlt to save space.
1254 if (SkipSPAttributes
)
1257 // Add the prototype if we have a prototype and we have a C like
1259 uint16_t Language
= getLanguage();
1260 if (SP
->isPrototyped() &&
1261 (Language
== dwarf::DW_LANG_C89
|| Language
== dwarf::DW_LANG_C99
||
1262 Language
== dwarf::DW_LANG_ObjC
))
1263 addFlag(SPDie
, dwarf::DW_AT_prototyped
);
1265 if (SP
->isObjCDirect())
1266 addFlag(SPDie
, dwarf::DW_AT_APPLE_objc_direct
);
1269 DITypeRefArray Args
;
1270 if (const DISubroutineType
*SPTy
= SP
->getType()) {
1271 Args
= SPTy
->getTypeArray();
1275 // Add a DW_AT_calling_convention if this has an explicit convention.
1276 if (CC
&& CC
!= dwarf::DW_CC_normal
)
1277 addUInt(SPDie
, dwarf::DW_AT_calling_convention
, dwarf::DW_FORM_data1
, CC
);
1279 // Add a return type. If this is a type like a C/C++ void type we don't add a
1282 if (auto Ty
= Args
[0])
1285 unsigned VK
= SP
->getVirtuality();
1287 addUInt(SPDie
, dwarf::DW_AT_virtuality
, dwarf::DW_FORM_data1
, VK
);
1288 if (SP
->getVirtualIndex() != -1u) {
1289 DIELoc
*Block
= getDIELoc();
1290 addUInt(*Block
, dwarf::DW_FORM_data1
, dwarf::DW_OP_constu
);
1291 addUInt(*Block
, dwarf::DW_FORM_udata
, SP
->getVirtualIndex());
1292 addBlock(SPDie
, dwarf::DW_AT_vtable_elem_location
, Block
);
1294 ContainingTypeMap
.insert(std::make_pair(&SPDie
, SP
->getContainingType()));
1297 if (!SP
->isDefinition()) {
1298 addFlag(SPDie
, dwarf::DW_AT_declaration
);
1300 // Add arguments. Do not add arguments for subprogram definition. They will
1301 // be handled while processing variables.
1302 constructSubprogramArguments(SPDie
, Args
);
1305 addThrownTypes(SPDie
, SP
->getThrownTypes());
1307 if (SP
->isArtificial())
1308 addFlag(SPDie
, dwarf::DW_AT_artificial
);
1310 if (!SP
->isLocalToUnit())
1311 addFlag(SPDie
, dwarf::DW_AT_external
);
1313 if (DD
->useAppleExtensionAttributes()) {
1314 if (SP
->isOptimized())
1315 addFlag(SPDie
, dwarf::DW_AT_APPLE_optimized
);
1317 if (unsigned isa
= Asm
->getISAEncoding())
1318 addUInt(SPDie
, dwarf::DW_AT_APPLE_isa
, dwarf::DW_FORM_flag
, isa
);
1321 if (SP
->isLValueReference())
1322 addFlag(SPDie
, dwarf::DW_AT_reference
);
1324 if (SP
->isRValueReference())
1325 addFlag(SPDie
, dwarf::DW_AT_rvalue_reference
);
1327 if (SP
->isNoReturn())
1328 addFlag(SPDie
, dwarf::DW_AT_noreturn
);
1330 addAccess(SPDie
, SP
->getFlags());
1332 if (SP
->isExplicit())
1333 addFlag(SPDie
, dwarf::DW_AT_explicit
);
1335 if (SP
->isMainSubprogram())
1336 addFlag(SPDie
, dwarf::DW_AT_main_subprogram
);
1338 addFlag(SPDie
, dwarf::DW_AT_pure
);
1339 if (SP
->isElemental())
1340 addFlag(SPDie
, dwarf::DW_AT_elemental
);
1341 if (SP
->isRecursive())
1342 addFlag(SPDie
, dwarf::DW_AT_recursive
);
1344 if (DD
->getDwarfVersion() >= 5 && SP
->isDeleted())
1345 addFlag(SPDie
, dwarf::DW_AT_deleted
);
1348 void DwarfUnit::constructSubrangeDIE(DIE
&Buffer
, const DISubrange
*SR
,
1350 DIE
&DW_Subrange
= createAndAddDIE(dwarf::DW_TAG_subrange_type
, Buffer
);
1351 addDIEEntry(DW_Subrange
, dwarf::DW_AT_type
, *IndexTy
);
1353 // The LowerBound value defines the lower bounds which is typically zero for
1354 // C/C++. The Count value is the number of elements. Values are 64 bit. If
1355 // Count == -1 then the array is unbounded and we do not emit
1356 // DW_AT_lower_bound and DW_AT_count attributes.
1357 int64_t DefaultLowerBound
= getDefaultLowerBound();
1359 auto AddBoundTypeEntry
= [&](dwarf::Attribute Attr
,
1360 DISubrange::BoundType Bound
) -> void {
1361 if (auto *BV
= Bound
.dyn_cast
<DIVariable
*>()) {
1362 if (auto *VarDIE
= getDIE(BV
))
1363 addDIEEntry(DW_Subrange
, Attr
, *VarDIE
);
1364 } else if (auto *BE
= Bound
.dyn_cast
<DIExpression
*>()) {
1365 DIELoc
*Loc
= new (DIEValueAllocator
) DIELoc
;
1366 DIEDwarfExpression
DwarfExpr(*Asm
, getCU(), *Loc
);
1367 DwarfExpr
.setMemoryLocationKind();
1368 DwarfExpr
.addExpression(BE
);
1369 addBlock(DW_Subrange
, Attr
, DwarfExpr
.finalize());
1370 } else if (auto *BI
= Bound
.dyn_cast
<ConstantInt
*>()) {
1371 if (Attr
== dwarf::DW_AT_count
) {
1372 if (BI
->getSExtValue() != -1)
1373 addUInt(DW_Subrange
, Attr
, None
, BI
->getSExtValue());
1374 } else if (Attr
!= dwarf::DW_AT_lower_bound
|| DefaultLowerBound
== -1 ||
1375 BI
->getSExtValue() != DefaultLowerBound
)
1376 addSInt(DW_Subrange
, Attr
, dwarf::DW_FORM_sdata
, BI
->getSExtValue());
1380 AddBoundTypeEntry(dwarf::DW_AT_lower_bound
, SR
->getLowerBound());
1382 AddBoundTypeEntry(dwarf::DW_AT_count
, SR
->getCount());
1384 AddBoundTypeEntry(dwarf::DW_AT_upper_bound
, SR
->getUpperBound());
1386 AddBoundTypeEntry(dwarf::DW_AT_byte_stride
, SR
->getStride());
1389 void DwarfUnit::constructGenericSubrangeDIE(DIE
&Buffer
,
1390 const DIGenericSubrange
*GSR
,
1392 DIE
&DwGenericSubrange
=
1393 createAndAddDIE(dwarf::DW_TAG_generic_subrange
, Buffer
);
1394 addDIEEntry(DwGenericSubrange
, dwarf::DW_AT_type
, *IndexTy
);
1396 int64_t DefaultLowerBound
= getDefaultLowerBound();
1398 auto AddBoundTypeEntry
= [&](dwarf::Attribute Attr
,
1399 DIGenericSubrange::BoundType Bound
) -> void {
1400 if (auto *BV
= Bound
.dyn_cast
<DIVariable
*>()) {
1401 if (auto *VarDIE
= getDIE(BV
))
1402 addDIEEntry(DwGenericSubrange
, Attr
, *VarDIE
);
1403 } else if (auto *BE
= Bound
.dyn_cast
<DIExpression
*>()) {
1404 if (BE
->isConstant() &&
1405 DIExpression::SignedOrUnsignedConstant::SignedConstant
==
1406 *BE
->isConstant()) {
1407 if (Attr
!= dwarf::DW_AT_lower_bound
|| DefaultLowerBound
== -1 ||
1408 static_cast<int64_t>(BE
->getElement(1)) != DefaultLowerBound
)
1409 addSInt(DwGenericSubrange
, Attr
, dwarf::DW_FORM_sdata
,
1412 DIELoc
*Loc
= new (DIEValueAllocator
) DIELoc
;
1413 DIEDwarfExpression
DwarfExpr(*Asm
, getCU(), *Loc
);
1414 DwarfExpr
.setMemoryLocationKind();
1415 DwarfExpr
.addExpression(BE
);
1416 addBlock(DwGenericSubrange
, Attr
, DwarfExpr
.finalize());
1421 AddBoundTypeEntry(dwarf::DW_AT_lower_bound
, GSR
->getLowerBound());
1422 AddBoundTypeEntry(dwarf::DW_AT_count
, GSR
->getCount());
1423 AddBoundTypeEntry(dwarf::DW_AT_upper_bound
, GSR
->getUpperBound());
1424 AddBoundTypeEntry(dwarf::DW_AT_byte_stride
, GSR
->getStride());
1427 DIE
*DwarfUnit::getIndexTyDie() {
1430 // Construct an integer type to use for indexes.
1431 IndexTyDie
= &createAndAddDIE(dwarf::DW_TAG_base_type
, getUnitDie());
1432 StringRef Name
= "__ARRAY_SIZE_TYPE__";
1433 addString(*IndexTyDie
, dwarf::DW_AT_name
, Name
);
1434 addUInt(*IndexTyDie
, dwarf::DW_AT_byte_size
, None
, sizeof(int64_t));
1435 addUInt(*IndexTyDie
, dwarf::DW_AT_encoding
, dwarf::DW_FORM_data1
,
1436 dwarf::DW_ATE_unsigned
);
1437 DD
->addAccelType(*CUNode
, Name
, *IndexTyDie
, /*Flags*/ 0);
1441 /// Returns true if the vector's size differs from the sum of sizes of elements
1442 /// the user specified. This can occur if the vector has been rounded up to
1443 /// fit memory alignment constraints.
1444 static bool hasVectorBeenPadded(const DICompositeType
*CTy
) {
1445 assert(CTy
&& CTy
->isVector() && "Composite type is not a vector");
1446 const uint64_t ActualSize
= CTy
->getSizeInBits();
1448 // Obtain the size of each element in the vector.
1449 DIType
*BaseTy
= CTy
->getBaseType();
1450 assert(BaseTy
&& "Unknown vector element type.");
1451 const uint64_t ElementSize
= BaseTy
->getSizeInBits();
1453 // Locate the number of elements in the vector.
1454 const DINodeArray Elements
= CTy
->getElements();
1455 assert(Elements
.size() == 1 &&
1456 Elements
[0]->getTag() == dwarf::DW_TAG_subrange_type
&&
1457 "Invalid vector element array, expected one element of type subrange");
1458 const auto Subrange
= cast
<DISubrange
>(Elements
[0]);
1459 const auto NumVecElements
=
1460 Subrange
->getCount()
1461 ? Subrange
->getCount().get
<ConstantInt
*>()->getSExtValue()
1464 // Ensure we found the element count and that the actual size is wide
1465 // enough to contain the requested size.
1466 assert(ActualSize
>= (NumVecElements
* ElementSize
) && "Invalid vector size");
1467 return ActualSize
!= (NumVecElements
* ElementSize
);
1470 void DwarfUnit::constructArrayTypeDIE(DIE
&Buffer
, const DICompositeType
*CTy
) {
1471 if (CTy
->isVector()) {
1472 addFlag(Buffer
, dwarf::DW_AT_GNU_vector
);
1473 if (hasVectorBeenPadded(CTy
))
1474 addUInt(Buffer
, dwarf::DW_AT_byte_size
, None
,
1475 CTy
->getSizeInBits() / CHAR_BIT
);
1478 if (DIVariable
*Var
= CTy
->getDataLocation()) {
1479 if (auto *VarDIE
= getDIE(Var
))
1480 addDIEEntry(Buffer
, dwarf::DW_AT_data_location
, *VarDIE
);
1481 } else if (DIExpression
*Expr
= CTy
->getDataLocationExp()) {
1482 DIELoc
*Loc
= new (DIEValueAllocator
) DIELoc
;
1483 DIEDwarfExpression
DwarfExpr(*Asm
, getCU(), *Loc
);
1484 DwarfExpr
.setMemoryLocationKind();
1485 DwarfExpr
.addExpression(Expr
);
1486 addBlock(Buffer
, dwarf::DW_AT_data_location
, DwarfExpr
.finalize());
1489 if (DIVariable
*Var
= CTy
->getAssociated()) {
1490 if (auto *VarDIE
= getDIE(Var
))
1491 addDIEEntry(Buffer
, dwarf::DW_AT_associated
, *VarDIE
);
1492 } else if (DIExpression
*Expr
= CTy
->getAssociatedExp()) {
1493 DIELoc
*Loc
= new (DIEValueAllocator
) DIELoc
;
1494 DIEDwarfExpression
DwarfExpr(*Asm
, getCU(), *Loc
);
1495 DwarfExpr
.setMemoryLocationKind();
1496 DwarfExpr
.addExpression(Expr
);
1497 addBlock(Buffer
, dwarf::DW_AT_associated
, DwarfExpr
.finalize());
1500 if (DIVariable
*Var
= CTy
->getAllocated()) {
1501 if (auto *VarDIE
= getDIE(Var
))
1502 addDIEEntry(Buffer
, dwarf::DW_AT_allocated
, *VarDIE
);
1503 } else if (DIExpression
*Expr
= CTy
->getAllocatedExp()) {
1504 DIELoc
*Loc
= new (DIEValueAllocator
) DIELoc
;
1505 DIEDwarfExpression
DwarfExpr(*Asm
, getCU(), *Loc
);
1506 DwarfExpr
.setMemoryLocationKind();
1507 DwarfExpr
.addExpression(Expr
);
1508 addBlock(Buffer
, dwarf::DW_AT_allocated
, DwarfExpr
.finalize());
1511 if (auto *RankConst
= CTy
->getRankConst()) {
1512 addSInt(Buffer
, dwarf::DW_AT_rank
, dwarf::DW_FORM_sdata
,
1513 RankConst
->getSExtValue());
1514 } else if (auto *RankExpr
= CTy
->getRankExp()) {
1515 DIELoc
*Loc
= new (DIEValueAllocator
) DIELoc
;
1516 DIEDwarfExpression
DwarfExpr(*Asm
, getCU(), *Loc
);
1517 DwarfExpr
.setMemoryLocationKind();
1518 DwarfExpr
.addExpression(RankExpr
);
1519 addBlock(Buffer
, dwarf::DW_AT_rank
, DwarfExpr
.finalize());
1522 // Emit the element type.
1523 addType(Buffer
, CTy
->getBaseType());
1525 // Get an anonymous type for index type.
1526 // FIXME: This type should be passed down from the front end
1527 // as different languages may have different sizes for indexes.
1528 DIE
*IdxTy
= getIndexTyDie();
1530 // Add subranges to array type.
1531 DINodeArray Elements
= CTy
->getElements();
1532 for (DINode
*E
: Elements
) {
1533 // FIXME: Should this really be such a loose cast?
1534 if (auto *Element
= dyn_cast_or_null
<DINode
>(E
)) {
1535 if (Element
->getTag() == dwarf::DW_TAG_subrange_type
)
1536 constructSubrangeDIE(Buffer
, cast
<DISubrange
>(Element
), IdxTy
);
1537 else if (Element
->getTag() == dwarf::DW_TAG_generic_subrange
)
1538 constructGenericSubrangeDIE(Buffer
, cast
<DIGenericSubrange
>(Element
),
1544 void DwarfUnit::constructEnumTypeDIE(DIE
&Buffer
, const DICompositeType
*CTy
) {
1545 const DIType
*DTy
= CTy
->getBaseType();
1546 bool IsUnsigned
= DTy
&& DD
->isUnsignedDIType(DTy
);
1548 if (DD
->getDwarfVersion() >= 3)
1549 addType(Buffer
, DTy
);
1550 if (DD
->getDwarfVersion() >= 4 && (CTy
->getFlags() & DINode::FlagEnumClass
))
1551 addFlag(Buffer
, dwarf::DW_AT_enum_class
);
1554 auto *Context
= CTy
->getScope();
1555 bool IndexEnumerators
= !Context
|| isa
<DICompileUnit
>(Context
) || isa
<DIFile
>(Context
) ||
1556 isa
<DINamespace
>(Context
) || isa
<DICommonBlock
>(Context
);
1557 DINodeArray Elements
= CTy
->getElements();
1559 // Add enumerators to enumeration type.
1560 for (const DINode
*E
: Elements
) {
1561 auto *Enum
= dyn_cast_or_null
<DIEnumerator
>(E
);
1563 DIE
&Enumerator
= createAndAddDIE(dwarf::DW_TAG_enumerator
, Buffer
);
1564 StringRef Name
= Enum
->getName();
1565 addString(Enumerator
, dwarf::DW_AT_name
, Name
);
1566 addConstantValue(Enumerator
, Enum
->getValue(), IsUnsigned
);
1567 if (IndexEnumerators
)
1568 addGlobalName(Name
, Enumerator
, Context
);
1573 void DwarfUnit::constructContainingTypeDIEs() {
1574 for (auto &P
: ContainingTypeMap
) {
1575 DIE
&SPDie
= *P
.first
;
1576 const DINode
*D
= P
.second
;
1579 DIE
*NDie
= getDIE(D
);
1582 addDIEEntry(SPDie
, dwarf::DW_AT_containing_type
, *NDie
);
1586 DIE
&DwarfUnit::constructMemberDIE(DIE
&Buffer
, const DIDerivedType
*DT
) {
1587 DIE
&MemberDie
= createAndAddDIE(DT
->getTag(), Buffer
);
1588 StringRef Name
= DT
->getName();
1590 addString(MemberDie
, dwarf::DW_AT_name
, Name
);
1592 addAnnotation(MemberDie
, DT
->getAnnotations());
1594 if (DIType
*Resolved
= DT
->getBaseType())
1595 addType(MemberDie
, Resolved
);
1597 addSourceLine(MemberDie
, DT
);
1599 if (DT
->getTag() == dwarf::DW_TAG_inheritance
&& DT
->isVirtual()) {
1601 // For C++, virtual base classes are not at fixed offset. Use following
1602 // expression to extract appropriate offset from vtable.
1603 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1605 DIELoc
*VBaseLocationDie
= new (DIEValueAllocator
) DIELoc
;
1606 addUInt(*VBaseLocationDie
, dwarf::DW_FORM_data1
, dwarf::DW_OP_dup
);
1607 addUInt(*VBaseLocationDie
, dwarf::DW_FORM_data1
, dwarf::DW_OP_deref
);
1608 addUInt(*VBaseLocationDie
, dwarf::DW_FORM_data1
, dwarf::DW_OP_constu
);
1609 addUInt(*VBaseLocationDie
, dwarf::DW_FORM_udata
, DT
->getOffsetInBits());
1610 addUInt(*VBaseLocationDie
, dwarf::DW_FORM_data1
, dwarf::DW_OP_minus
);
1611 addUInt(*VBaseLocationDie
, dwarf::DW_FORM_data1
, dwarf::DW_OP_deref
);
1612 addUInt(*VBaseLocationDie
, dwarf::DW_FORM_data1
, dwarf::DW_OP_plus
);
1614 addBlock(MemberDie
, dwarf::DW_AT_data_member_location
, VBaseLocationDie
);
1616 uint64_t Size
= DT
->getSizeInBits();
1617 uint64_t FieldSize
= DD
->getBaseTypeSize(DT
);
1618 uint32_t AlignInBytes
= DT
->getAlignInBytes();
1619 uint64_t OffsetInBytes
;
1621 bool IsBitfield
= FieldSize
&& Size
!= FieldSize
;
1623 // Handle bitfield, assume bytes are 8 bits.
1624 if (DD
->useDWARF2Bitfields())
1625 addUInt(MemberDie
, dwarf::DW_AT_byte_size
, None
, FieldSize
/8);
1626 addUInt(MemberDie
, dwarf::DW_AT_bit_size
, None
, Size
);
1628 uint64_t Offset
= DT
->getOffsetInBits();
1629 // We can't use DT->getAlignInBits() here: AlignInBits for member type
1630 // is non-zero if and only if alignment was forced (e.g. _Alignas()),
1631 // which can't be done with bitfields. Thus we use FieldSize here.
1632 uint32_t AlignInBits
= FieldSize
;
1633 uint32_t AlignMask
= ~(AlignInBits
- 1);
1634 // The bits from the start of the storage unit to the start of the field.
1635 uint64_t StartBitOffset
= Offset
- (Offset
& AlignMask
);
1636 // The byte offset of the field's aligned storage unit inside the struct.
1637 OffsetInBytes
= (Offset
- StartBitOffset
) / 8;
1639 if (DD
->useDWARF2Bitfields()) {
1640 uint64_t HiMark
= (Offset
+ FieldSize
) & AlignMask
;
1641 uint64_t FieldOffset
= (HiMark
- FieldSize
);
1642 Offset
-= FieldOffset
;
1644 // Maybe we need to work from the other end.
1645 if (Asm
->getDataLayout().isLittleEndian())
1646 Offset
= FieldSize
- (Offset
+ Size
);
1648 addUInt(MemberDie
, dwarf::DW_AT_bit_offset
, None
, Offset
);
1649 OffsetInBytes
= FieldOffset
>> 3;
1651 addUInt(MemberDie
, dwarf::DW_AT_data_bit_offset
, None
, Offset
);
1654 // This is not a bitfield.
1655 OffsetInBytes
= DT
->getOffsetInBits() / 8;
1657 addUInt(MemberDie
, dwarf::DW_AT_alignment
, dwarf::DW_FORM_udata
,
1661 if (DD
->getDwarfVersion() <= 2) {
1662 DIELoc
*MemLocationDie
= new (DIEValueAllocator
) DIELoc
;
1663 addUInt(*MemLocationDie
, dwarf::DW_FORM_data1
, dwarf::DW_OP_plus_uconst
);
1664 addUInt(*MemLocationDie
, dwarf::DW_FORM_udata
, OffsetInBytes
);
1665 addBlock(MemberDie
, dwarf::DW_AT_data_member_location
, MemLocationDie
);
1666 } else if (!IsBitfield
|| DD
->useDWARF2Bitfields()) {
1667 // In DWARF v3, DW_FORM_data4/8 in DW_AT_data_member_location are
1668 // interpreted as location-list pointers. Interpreting constants as
1669 // pointers is not expected, so we use DW_FORM_udata to encode the
1671 if (DD
->getDwarfVersion() == 3)
1672 addUInt(MemberDie
, dwarf::DW_AT_data_member_location
,
1673 dwarf::DW_FORM_udata
, OffsetInBytes
);
1675 addUInt(MemberDie
, dwarf::DW_AT_data_member_location
, None
,
1680 addAccess(MemberDie
, DT
->getFlags());
1682 if (DT
->isVirtual())
1683 addUInt(MemberDie
, dwarf::DW_AT_virtuality
, dwarf::DW_FORM_data1
,
1684 dwarf::DW_VIRTUALITY_virtual
);
1686 // Objective-C properties.
1687 if (DINode
*PNode
= DT
->getObjCProperty())
1688 if (DIE
*PDie
= getDIE(PNode
))
1689 addAttribute(MemberDie
, dwarf::DW_AT_APPLE_property
,
1690 dwarf::DW_FORM_ref4
, DIEEntry(*PDie
));
1692 if (DT
->isArtificial())
1693 addFlag(MemberDie
, dwarf::DW_AT_artificial
);
1698 DIE
*DwarfUnit::getOrCreateStaticMemberDIE(const DIDerivedType
*DT
) {
1702 // Construct the context before querying for the existence of the DIE in case
1703 // such construction creates the DIE.
1704 DIE
*ContextDIE
= getOrCreateContextDIE(DT
->getScope());
1705 assert(dwarf::isType(ContextDIE
->getTag()) &&
1706 "Static member should belong to a type.");
1708 if (DIE
*StaticMemberDIE
= getDIE(DT
))
1709 return StaticMemberDIE
;
1711 DIE
&StaticMemberDIE
= createAndAddDIE(DT
->getTag(), *ContextDIE
, DT
);
1713 const DIType
*Ty
= DT
->getBaseType();
1715 addString(StaticMemberDIE
, dwarf::DW_AT_name
, DT
->getName());
1716 addType(StaticMemberDIE
, Ty
);
1717 addSourceLine(StaticMemberDIE
, DT
);
1718 addFlag(StaticMemberDIE
, dwarf::DW_AT_external
);
1719 addFlag(StaticMemberDIE
, dwarf::DW_AT_declaration
);
1721 // FIXME: We could omit private if the parent is a class_type, and
1722 // public if the parent is something else.
1723 addAccess(StaticMemberDIE
, DT
->getFlags());
1725 if (const ConstantInt
*CI
= dyn_cast_or_null
<ConstantInt
>(DT
->getConstant()))
1726 addConstantValue(StaticMemberDIE
, CI
, Ty
);
1727 if (const ConstantFP
*CFP
= dyn_cast_or_null
<ConstantFP
>(DT
->getConstant()))
1728 addConstantFPValue(StaticMemberDIE
, CFP
);
1730 if (uint32_t AlignInBytes
= DT
->getAlignInBytes())
1731 addUInt(StaticMemberDIE
, dwarf::DW_AT_alignment
, dwarf::DW_FORM_udata
,
1734 return &StaticMemberDIE
;
1737 void DwarfUnit::emitCommonHeader(bool UseOffsets
, dwarf::UnitType UT
) {
1738 // Emit size of content not including length itself
1739 if (!DD
->useSectionsAsReferences())
1740 EndLabel
= Asm
->emitDwarfUnitLength(
1741 isDwoUnit() ? "debug_info_dwo" : "debug_info", "Length of Unit");
1743 Asm
->emitDwarfUnitLength(getHeaderSize() + getUnitDie().getSize(),
1746 Asm
->OutStreamer
->AddComment("DWARF version number");
1747 unsigned Version
= DD
->getDwarfVersion();
1748 Asm
->emitInt16(Version
);
1750 // DWARF v5 reorders the address size and adds a unit type.
1752 Asm
->OutStreamer
->AddComment("DWARF Unit Type");
1754 Asm
->OutStreamer
->AddComment("Address Size (in bytes)");
1755 Asm
->emitInt8(Asm
->MAI
->getCodePointerSize());
1758 // We share one abbreviations table across all units so it's always at the
1759 // start of the section. Use a relocatable offset where needed to ensure
1760 // linking doesn't invalidate that offset.
1761 Asm
->OutStreamer
->AddComment("Offset Into Abbrev. Section");
1762 const TargetLoweringObjectFile
&TLOF
= Asm
->getObjFileLowering();
1764 Asm
->emitDwarfLengthOrOffset(0);
1766 Asm
->emitDwarfSymbolReference(
1767 TLOF
.getDwarfAbbrevSection()->getBeginSymbol(), false);
1770 Asm
->OutStreamer
->AddComment("Address Size (in bytes)");
1771 Asm
->emitInt8(Asm
->MAI
->getCodePointerSize());
1775 void DwarfTypeUnit::emitHeader(bool UseOffsets
) {
1776 DwarfUnit::emitCommonHeader(UseOffsets
,
1777 DD
->useSplitDwarf() ? dwarf::DW_UT_split_type
1778 : dwarf::DW_UT_type
);
1779 Asm
->OutStreamer
->AddComment("Type Signature");
1780 Asm
->OutStreamer
->emitIntValue(TypeSignature
, sizeof(TypeSignature
));
1781 Asm
->OutStreamer
->AddComment("Type DIE Offset");
1782 // In a skeleton type unit there is no type DIE so emit a zero offset.
1783 Asm
->emitDwarfLengthOrOffset(Ty
? Ty
->getOffset() : 0);
1786 void DwarfUnit::addSectionDelta(DIE
&Die
, dwarf::Attribute Attribute
,
1787 const MCSymbol
*Hi
, const MCSymbol
*Lo
) {
1788 addAttribute(Die
, Attribute
, DD
->getDwarfSectionOffsetForm(),
1789 new (DIEValueAllocator
) DIEDelta(Hi
, Lo
));
1792 void DwarfUnit::addSectionLabel(DIE
&Die
, dwarf::Attribute Attribute
,
1793 const MCSymbol
*Label
, const MCSymbol
*Sec
) {
1794 if (Asm
->MAI
->doesDwarfUseRelocationsAcrossSections())
1795 addLabel(Die
, Attribute
, DD
->getDwarfSectionOffsetForm(), Label
);
1797 addSectionDelta(Die
, Attribute
, Label
, Sec
);
1800 bool DwarfTypeUnit::isDwoUnit() const {
1801 // Since there are no skeleton type units, all type units are dwo type units
1802 // when split DWARF is being used.
1803 return DD
->useSplitDwarf();
1806 void DwarfTypeUnit::addGlobalName(StringRef Name
, const DIE
&Die
,
1807 const DIScope
*Context
) {
1808 getCU().addGlobalNameForTypeUnit(Name
, Context
);
1811 void DwarfTypeUnit::addGlobalType(const DIType
*Ty
, const DIE
&Die
,
1812 const DIScope
*Context
) {
1813 getCU().addGlobalTypeUnitType(Ty
, Context
);
1816 const MCSymbol
*DwarfUnit::getCrossSectionRelativeBaseAddress() const {
1817 if (!Asm
->MAI
->doesDwarfUseRelocationsAcrossSections())
1821 return getSection()->getBeginSymbol();
1824 void DwarfUnit::addStringOffsetsStart() {
1825 const TargetLoweringObjectFile
&TLOF
= Asm
->getObjFileLowering();
1826 addSectionLabel(getUnitDie(), dwarf::DW_AT_str_offsets_base
,
1827 DU
->getStringOffsetsStartSym(),
1828 TLOF
.getDwarfStrOffSection()->getBeginSymbol());
1831 void DwarfUnit::addRnglistsBase() {
1832 assert(DD
->getDwarfVersion() >= 5 &&
1833 "DW_AT_rnglists_base requires DWARF version 5 or later");
1834 const TargetLoweringObjectFile
&TLOF
= Asm
->getObjFileLowering();
1835 addSectionLabel(getUnitDie(), dwarf::DW_AT_rnglists_base
,
1836 DU
->getRnglistsTableBaseSym(),
1837 TLOF
.getDwarfRnglistsSection()->getBeginSymbol());
1840 void DwarfTypeUnit::finishNonUnitTypeDIE(DIE
& D
, const DICompositeType
*CTy
) {
1841 addFlag(D
, dwarf::DW_AT_declaration
);
1842 StringRef Name
= CTy
->getName();
1844 addString(D
, dwarf::DW_AT_name
, Name
);
1845 getCU().createTypeDIE(CTy
);