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 "DwarfDebug.h"
17 #include "DwarfExpression.h"
18 #include "llvm/ADT/APFloat.h"
19 #include "llvm/ADT/APInt.h"
20 #include "llvm/ADT/None.h"
21 #include "llvm/ADT/StringExtras.h"
22 #include "llvm/ADT/iterator_range.h"
23 #include "llvm/CodeGen/MachineFunction.h"
24 #include "llvm/CodeGen/MachineOperand.h"
25 #include "llvm/CodeGen/TargetRegisterInfo.h"
26 #include "llvm/CodeGen/TargetSubtargetInfo.h"
27 #include "llvm/IR/Constants.h"
28 #include "llvm/IR/DataLayout.h"
29 #include "llvm/IR/GlobalValue.h"
30 #include "llvm/IR/Metadata.h"
31 #include "llvm/MC/MCAsmInfo.h"
32 #include "llvm/MC/MCContext.h"
33 #include "llvm/MC/MCDwarf.h"
34 #include "llvm/MC/MCSection.h"
35 #include "llvm/MC/MCStreamer.h"
36 #include "llvm/MC/MachineLocation.h"
37 #include "llvm/Support/Casting.h"
38 #include "llvm/Support/CommandLine.h"
39 #include "llvm/Target/TargetLoweringObjectFile.h"
47 #define DEBUG_TYPE "dwarfdebug"
49 DIEDwarfExpression::DIEDwarfExpression(const AsmPrinter
&AP
,
52 : DwarfExpression(AP
.getDwarfVersion(), CU
), AP(AP
),
55 void DIEDwarfExpression::emitOp(uint8_t Op
, const char* Comment
) {
56 CU
.addUInt(DIE
, dwarf::DW_FORM_data1
, Op
);
59 void DIEDwarfExpression::emitSigned(int64_t Value
) {
60 CU
.addSInt(DIE
, dwarf::DW_FORM_sdata
, Value
);
63 void DIEDwarfExpression::emitUnsigned(uint64_t Value
) {
64 CU
.addUInt(DIE
, dwarf::DW_FORM_udata
, Value
);
67 void DIEDwarfExpression::emitData1(uint8_t Value
) {
68 CU
.addUInt(DIE
, dwarf::DW_FORM_data1
, Value
);
71 void DIEDwarfExpression::emitBaseTypeRef(uint64_t Idx
) {
72 CU
.addBaseTypeRef(DIE
, Idx
);
75 bool DIEDwarfExpression::isFrameRegister(const TargetRegisterInfo
&TRI
,
76 unsigned MachineReg
) {
77 return MachineReg
== TRI
.getFrameRegister(*AP
.MF
);
80 DwarfUnit::DwarfUnit(dwarf::Tag UnitTag
, const DICompileUnit
*Node
,
81 AsmPrinter
*A
, DwarfDebug
*DW
, DwarfFile
*DWU
)
82 : DIEUnit(A
->getDwarfVersion(), A
->MAI
->getCodePointerSize(), UnitTag
),
83 CUNode(Node
), Asm(A
), DD(DW
), DU(DWU
), IndexTyDie(nullptr) {
86 DwarfTypeUnit::DwarfTypeUnit(DwarfCompileUnit
&CU
, AsmPrinter
*A
,
87 DwarfDebug
*DW
, DwarfFile
*DWU
,
88 MCDwarfDwoLineTable
*SplitLineTable
)
89 : DwarfUnit(dwarf::DW_TAG_type_unit
, CU
.getCUNode(), A
, DW
, DWU
), CU(CU
),
90 SplitLineTable(SplitLineTable
) {
93 DwarfUnit::~DwarfUnit() {
94 for (unsigned j
= 0, M
= DIEBlocks
.size(); j
< M
; ++j
)
95 DIEBlocks
[j
]->~DIEBlock();
96 for (unsigned j
= 0, M
= DIELocs
.size(); j
< M
; ++j
)
97 DIELocs
[j
]->~DIELoc();
100 int64_t DwarfUnit::getDefaultLowerBound() const {
101 switch (getLanguage()) {
105 // The languages below have valid values in all DWARF versions.
106 case dwarf::DW_LANG_C
:
107 case dwarf::DW_LANG_C89
:
108 case dwarf::DW_LANG_C_plus_plus
:
111 case dwarf::DW_LANG_Fortran77
:
112 case dwarf::DW_LANG_Fortran90
:
115 // The languages below have valid values only if the DWARF version >= 3.
116 case dwarf::DW_LANG_C99
:
117 case dwarf::DW_LANG_ObjC
:
118 case dwarf::DW_LANG_ObjC_plus_plus
:
119 if (DD
->getDwarfVersion() >= 3)
123 case dwarf::DW_LANG_Fortran95
:
124 if (DD
->getDwarfVersion() >= 3)
128 // Starting with DWARF v4, all defined languages have valid values.
129 case dwarf::DW_LANG_D
:
130 case dwarf::DW_LANG_Java
:
131 case dwarf::DW_LANG_Python
:
132 case dwarf::DW_LANG_UPC
:
133 if (DD
->getDwarfVersion() >= 4)
137 case dwarf::DW_LANG_Ada83
:
138 case dwarf::DW_LANG_Ada95
:
139 case dwarf::DW_LANG_Cobol74
:
140 case dwarf::DW_LANG_Cobol85
:
141 case dwarf::DW_LANG_Modula2
:
142 case dwarf::DW_LANG_Pascal83
:
143 case dwarf::DW_LANG_PLI
:
144 if (DD
->getDwarfVersion() >= 4)
148 // The languages below are new in DWARF v5.
149 case dwarf::DW_LANG_BLISS
:
150 case dwarf::DW_LANG_C11
:
151 case dwarf::DW_LANG_C_plus_plus_03
:
152 case dwarf::DW_LANG_C_plus_plus_11
:
153 case dwarf::DW_LANG_C_plus_plus_14
:
154 case dwarf::DW_LANG_Dylan
:
155 case dwarf::DW_LANG_Go
:
156 case dwarf::DW_LANG_Haskell
:
157 case dwarf::DW_LANG_OCaml
:
158 case dwarf::DW_LANG_OpenCL
:
159 case dwarf::DW_LANG_RenderScript
:
160 case dwarf::DW_LANG_Rust
:
161 case dwarf::DW_LANG_Swift
:
162 if (DD
->getDwarfVersion() >= 5)
166 case dwarf::DW_LANG_Fortran03
:
167 case dwarf::DW_LANG_Fortran08
:
168 case dwarf::DW_LANG_Julia
:
169 case dwarf::DW_LANG_Modula3
:
170 if (DD
->getDwarfVersion() >= 5)
178 /// Check whether the DIE for this MDNode can be shared across CUs.
179 bool DwarfUnit::isShareableAcrossCUs(const DINode
*D
) const {
180 // When the MDNode can be part of the type system, the DIE can be shared
182 // Combining type units and cross-CU DIE sharing is lower value (since
183 // cross-CU DIE sharing is used in LTO and removes type redundancy at that
184 // level already) but may be implementable for some value in projects
185 // building multiple independent libraries with LTO and then linking those
187 if (isDwoUnit() && !DD
->shareAcrossDWOCUs())
189 return (isa
<DIType
>(D
) ||
190 (isa
<DISubprogram
>(D
) && !cast
<DISubprogram
>(D
)->isDefinition())) &&
191 !DD
->generateTypeUnits();
194 DIE
*DwarfUnit::getDIE(const DINode
*D
) const {
195 if (isShareableAcrossCUs(D
))
196 return DU
->getDIE(D
);
197 return MDNodeToDieMap
.lookup(D
);
200 void DwarfUnit::insertDIE(const DINode
*Desc
, DIE
*D
) {
201 if (isShareableAcrossCUs(Desc
)) {
202 DU
->insertDIE(Desc
, D
);
205 MDNodeToDieMap
.insert(std::make_pair(Desc
, D
));
208 void DwarfUnit::insertDIE(DIE
*D
) {
209 MDNodeToDieMap
.insert(std::make_pair(nullptr, D
));
212 void DwarfUnit::addFlag(DIE
&Die
, dwarf::Attribute Attribute
) {
213 if (DD
->getDwarfVersion() >= 4)
214 Die
.addValue(DIEValueAllocator
, Attribute
, dwarf::DW_FORM_flag_present
,
217 Die
.addValue(DIEValueAllocator
, Attribute
, dwarf::DW_FORM_flag
,
221 void DwarfUnit::addUInt(DIEValueList
&Die
, dwarf::Attribute Attribute
,
222 Optional
<dwarf::Form
> Form
, uint64_t Integer
) {
224 Form
= DIEInteger::BestForm(false, Integer
);
225 assert(Form
!= dwarf::DW_FORM_implicit_const
&&
226 "DW_FORM_implicit_const is used only for signed integers");
227 Die
.addValue(DIEValueAllocator
, Attribute
, *Form
, DIEInteger(Integer
));
230 void DwarfUnit::addUInt(DIEValueList
&Block
, dwarf::Form Form
,
232 addUInt(Block
, (dwarf::Attribute
)0, Form
, Integer
);
235 void DwarfUnit::addSInt(DIEValueList
&Die
, dwarf::Attribute Attribute
,
236 Optional
<dwarf::Form
> Form
, int64_t Integer
) {
238 Form
= DIEInteger::BestForm(true, Integer
);
239 Die
.addValue(DIEValueAllocator
, Attribute
, *Form
, DIEInteger(Integer
));
242 void DwarfUnit::addSInt(DIELoc
&Die
, Optional
<dwarf::Form
> Form
,
244 addSInt(Die
, (dwarf::Attribute
)0, Form
, Integer
);
247 void DwarfUnit::addString(DIE
&Die
, dwarf::Attribute Attribute
,
249 if (CUNode
->isDebugDirectivesOnly())
252 if (DD
->useInlineStrings()) {
253 Die
.addValue(DIEValueAllocator
, Attribute
, dwarf::DW_FORM_string
,
254 new (DIEValueAllocator
)
255 DIEInlineString(String
, DIEValueAllocator
));
259 isDwoUnit() ? dwarf::DW_FORM_GNU_str_index
: dwarf::DW_FORM_strp
;
261 auto StringPoolEntry
=
262 useSegmentedStringOffsetsTable() || IxForm
== dwarf::DW_FORM_GNU_str_index
263 ? DU
->getStringPool().getIndexedEntry(*Asm
, String
)
264 : DU
->getStringPool().getEntry(*Asm
, String
);
266 // For DWARF v5 and beyond, use the smallest strx? form possible.
267 if (useSegmentedStringOffsetsTable()) {
268 IxForm
= dwarf::DW_FORM_strx1
;
269 unsigned Index
= StringPoolEntry
.getIndex();
270 if (Index
> 0xffffff)
271 IxForm
= dwarf::DW_FORM_strx4
;
272 else if (Index
> 0xffff)
273 IxForm
= dwarf::DW_FORM_strx3
;
274 else if (Index
> 0xff)
275 IxForm
= dwarf::DW_FORM_strx2
;
277 Die
.addValue(DIEValueAllocator
, Attribute
, IxForm
,
278 DIEString(StringPoolEntry
));
281 DIEValueList::value_iterator
DwarfUnit::addLabel(DIEValueList
&Die
,
282 dwarf::Attribute Attribute
,
284 const MCSymbol
*Label
) {
285 return Die
.addValue(DIEValueAllocator
, Attribute
, Form
, DIELabel(Label
));
288 void DwarfUnit::addLabel(DIELoc
&Die
, dwarf::Form Form
, const MCSymbol
*Label
) {
289 addLabel(Die
, (dwarf::Attribute
)0, Form
, Label
);
292 void DwarfUnit::addSectionOffset(DIE
&Die
, dwarf::Attribute Attribute
,
294 if (DD
->getDwarfVersion() >= 4)
295 addUInt(Die
, Attribute
, dwarf::DW_FORM_sec_offset
, Integer
);
297 addUInt(Die
, Attribute
, dwarf::DW_FORM_data4
, Integer
);
300 Optional
<MD5::MD5Result
> DwarfUnit::getMD5AsBytes(const DIFile
*File
) const {
302 if (DD
->getDwarfVersion() < 5)
304 Optional
<DIFile::ChecksumInfo
<StringRef
>> Checksum
= File
->getChecksum();
305 if (!Checksum
|| Checksum
->Kind
!= DIFile::CSK_MD5
)
308 // Convert the string checksum to an MD5Result for the streamer.
309 // The verifier validates the checksum so we assume it's okay.
310 // An MD5 checksum is 16 bytes.
311 std::string ChecksumString
= fromHex(Checksum
->Value
);
312 MD5::MD5Result CKMem
;
313 std::copy(ChecksumString
.begin(), ChecksumString
.end(), CKMem
.Bytes
.data());
317 unsigned DwarfTypeUnit::getOrCreateSourceID(const DIFile
*File
) {
319 return getCU().getOrCreateSourceID(File
);
320 if (!UsedLineTable
) {
321 UsedLineTable
= true;
322 // This is a split type unit that needs a line table.
323 addSectionOffset(getUnitDie(), dwarf::DW_AT_stmt_list
, 0);
325 return SplitLineTable
->getFile(File
->getDirectory(), File
->getFilename(),
327 Asm
->OutContext
.getDwarfVersion(),
331 void DwarfUnit::addOpAddress(DIELoc
&Die
, const MCSymbol
*Sym
) {
332 if (DD
->getDwarfVersion() >= 5) {
333 addUInt(Die
, dwarf::DW_FORM_data1
, dwarf::DW_OP_addrx
);
334 addUInt(Die
, dwarf::DW_FORM_addrx
, DD
->getAddressPool().getIndex(Sym
));
338 if (DD
->useSplitDwarf()) {
339 addUInt(Die
, dwarf::DW_FORM_data1
, dwarf::DW_OP_GNU_addr_index
);
340 addUInt(Die
, dwarf::DW_FORM_GNU_addr_index
,
341 DD
->getAddressPool().getIndex(Sym
));
345 addUInt(Die
, dwarf::DW_FORM_data1
, dwarf::DW_OP_addr
);
346 addLabel(Die
, dwarf::DW_FORM_udata
, Sym
);
349 void DwarfUnit::addLabelDelta(DIE
&Die
, dwarf::Attribute Attribute
,
350 const MCSymbol
*Hi
, const MCSymbol
*Lo
) {
351 Die
.addValue(DIEValueAllocator
, Attribute
, dwarf::DW_FORM_data4
,
352 new (DIEValueAllocator
) DIEDelta(Hi
, Lo
));
355 void DwarfUnit::addDIEEntry(DIE
&Die
, dwarf::Attribute Attribute
, DIE
&Entry
) {
356 addDIEEntry(Die
, Attribute
, DIEEntry(Entry
));
359 void DwarfUnit::addDIETypeSignature(DIE
&Die
, uint64_t Signature
) {
360 // Flag the type unit reference as a declaration so that if it contains
361 // members (implicit special members, static data member definitions, member
362 // declarations for definitions in this CU, etc) consumers don't get confused
363 // and think this is a full definition.
364 addFlag(Die
, dwarf::DW_AT_declaration
);
366 Die
.addValue(DIEValueAllocator
, dwarf::DW_AT_signature
,
367 dwarf::DW_FORM_ref_sig8
, DIEInteger(Signature
));
370 void DwarfUnit::addDIEEntry(DIE
&Die
, dwarf::Attribute Attribute
,
372 const DIEUnit
*CU
= Die
.getUnit();
373 const DIEUnit
*EntryCU
= Entry
.getEntry().getUnit();
375 // We assume that Die belongs to this CU, if it is not linked to any CU yet.
376 CU
= getUnitDie().getUnit();
378 EntryCU
= getUnitDie().getUnit();
379 Die
.addValue(DIEValueAllocator
, Attribute
,
380 EntryCU
== CU
? dwarf::DW_FORM_ref4
: dwarf::DW_FORM_ref_addr
,
384 DIE
&DwarfUnit::createAndAddDIE(unsigned Tag
, DIE
&Parent
, const DINode
*N
) {
385 DIE
&Die
= Parent
.addChild(DIE::get(DIEValueAllocator
, (dwarf::Tag
)Tag
));
391 void DwarfUnit::addBlock(DIE
&Die
, dwarf::Attribute Attribute
, DIELoc
*Loc
) {
392 Loc
->ComputeSize(Asm
);
393 DIELocs
.push_back(Loc
); // Memoize so we can call the destructor later on.
394 Die
.addValue(DIEValueAllocator
, Attribute
,
395 Loc
->BestForm(DD
->getDwarfVersion()), Loc
);
398 void DwarfUnit::addBlock(DIE
&Die
, dwarf::Attribute Attribute
,
400 Block
->ComputeSize(Asm
);
401 DIEBlocks
.push_back(Block
); // Memoize so we can call the destructor later on.
402 Die
.addValue(DIEValueAllocator
, Attribute
, Block
->BestForm(), Block
);
405 void DwarfUnit::addSourceLine(DIE
&Die
, unsigned Line
, const DIFile
*File
) {
409 unsigned FileID
= getOrCreateSourceID(File
);
410 addUInt(Die
, dwarf::DW_AT_decl_file
, None
, FileID
);
411 addUInt(Die
, dwarf::DW_AT_decl_line
, None
, Line
);
414 void DwarfUnit::addSourceLine(DIE
&Die
, const DILocalVariable
*V
) {
417 addSourceLine(Die
, V
->getLine(), V
->getFile());
420 void DwarfUnit::addSourceLine(DIE
&Die
, const DIGlobalVariable
*G
) {
423 addSourceLine(Die
, G
->getLine(), G
->getFile());
426 void DwarfUnit::addSourceLine(DIE
&Die
, const DISubprogram
*SP
) {
429 addSourceLine(Die
, SP
->getLine(), SP
->getFile());
432 void DwarfUnit::addSourceLine(DIE
&Die
, const DILabel
*L
) {
435 addSourceLine(Die
, L
->getLine(), L
->getFile());
438 void DwarfUnit::addSourceLine(DIE
&Die
, const DIType
*Ty
) {
441 addSourceLine(Die
, Ty
->getLine(), Ty
->getFile());
444 void DwarfUnit::addSourceLine(DIE
&Die
, const DIObjCProperty
*Ty
) {
447 addSourceLine(Die
, Ty
->getLine(), Ty
->getFile());
450 /// Return true if type encoding is unsigned.
451 static bool isUnsignedDIType(DwarfDebug
*DD
, const DIType
*Ty
) {
452 if (auto *CTy
= dyn_cast
<DICompositeType
>(Ty
)) {
453 // FIXME: Enums without a fixed underlying type have unknown signedness
454 // here, leading to incorrectly emitted constants.
455 if (CTy
->getTag() == dwarf::DW_TAG_enumeration_type
)
458 // (Pieces of) aggregate types that get hacked apart by SROA may be
459 // represented by a constant. Encode them as unsigned bytes.
463 if (auto *DTy
= dyn_cast
<DIDerivedType
>(Ty
)) {
464 dwarf::Tag T
= (dwarf::Tag
)Ty
->getTag();
465 // Encode pointer constants as unsigned bytes. This is used at least for
466 // null pointer constant emission.
467 // FIXME: reference and rvalue_reference /probably/ shouldn't be allowed
468 // here, but accept them for now due to a bug in SROA producing bogus
470 if (T
== dwarf::DW_TAG_pointer_type
||
471 T
== dwarf::DW_TAG_ptr_to_member_type
||
472 T
== dwarf::DW_TAG_reference_type
||
473 T
== dwarf::DW_TAG_rvalue_reference_type
)
475 assert(T
== dwarf::DW_TAG_typedef
|| T
== dwarf::DW_TAG_const_type
||
476 T
== dwarf::DW_TAG_volatile_type
||
477 T
== dwarf::DW_TAG_restrict_type
|| T
== dwarf::DW_TAG_atomic_type
);
478 assert(DTy
->getBaseType() && "Expected valid base type");
479 return isUnsignedDIType(DD
, DTy
->getBaseType());
482 auto *BTy
= cast
<DIBasicType
>(Ty
);
483 unsigned Encoding
= BTy
->getEncoding();
484 assert((Encoding
== dwarf::DW_ATE_unsigned
||
485 Encoding
== dwarf::DW_ATE_unsigned_char
||
486 Encoding
== dwarf::DW_ATE_signed
||
487 Encoding
== dwarf::DW_ATE_signed_char
||
488 Encoding
== dwarf::DW_ATE_float
|| Encoding
== dwarf::DW_ATE_UTF
||
489 Encoding
== dwarf::DW_ATE_boolean
||
490 (Ty
->getTag() == dwarf::DW_TAG_unspecified_type
&&
491 Ty
->getName() == "decltype(nullptr)")) &&
492 "Unsupported encoding");
493 return Encoding
== dwarf::DW_ATE_unsigned
||
494 Encoding
== dwarf::DW_ATE_unsigned_char
||
495 Encoding
== dwarf::DW_ATE_UTF
|| Encoding
== dwarf::DW_ATE_boolean
||
496 Ty
->getTag() == dwarf::DW_TAG_unspecified_type
;
499 void DwarfUnit::addConstantFPValue(DIE
&Die
, const MachineOperand
&MO
) {
500 assert(MO
.isFPImm() && "Invalid machine operand!");
501 DIEBlock
*Block
= new (DIEValueAllocator
) DIEBlock
;
502 APFloat FPImm
= MO
.getFPImm()->getValueAPF();
504 // Get the raw data form of the floating point.
505 const APInt FltVal
= FPImm
.bitcastToAPInt();
506 const char *FltPtr
= (const char *)FltVal
.getRawData();
508 int NumBytes
= FltVal
.getBitWidth() / 8; // 8 bits per byte.
509 bool LittleEndian
= Asm
->getDataLayout().isLittleEndian();
510 int Incr
= (LittleEndian
? 1 : -1);
511 int Start
= (LittleEndian
? 0 : NumBytes
- 1);
512 int Stop
= (LittleEndian
? NumBytes
: -1);
514 // Output the constant to DWARF one byte at a time.
515 for (; Start
!= Stop
; Start
+= Incr
)
516 addUInt(*Block
, dwarf::DW_FORM_data1
, (unsigned char)0xFF & FltPtr
[Start
]);
518 addBlock(Die
, dwarf::DW_AT_const_value
, Block
);
521 void DwarfUnit::addConstantFPValue(DIE
&Die
, const ConstantFP
*CFP
) {
522 // Pass this down to addConstantValue as an unsigned bag of bits.
523 addConstantValue(Die
, CFP
->getValueAPF().bitcastToAPInt(), true);
526 void DwarfUnit::addConstantValue(DIE
&Die
, const ConstantInt
*CI
,
528 addConstantValue(Die
, CI
->getValue(), Ty
);
531 void DwarfUnit::addConstantValue(DIE
&Die
, const MachineOperand
&MO
,
533 assert(MO
.isImm() && "Invalid machine operand!");
535 addConstantValue(Die
, isUnsignedDIType(DD
, Ty
), MO
.getImm());
538 void DwarfUnit::addConstantValue(DIE
&Die
, uint64_t Val
, const DIType
*Ty
) {
539 addConstantValue(Die
, isUnsignedDIType(DD
, Ty
), Val
);
542 void DwarfUnit::addConstantValue(DIE
&Die
, bool Unsigned
, uint64_t Val
) {
543 // FIXME: This is a bit conservative/simple - it emits negative values always
544 // sign extended to 64 bits rather than minimizing the number of bytes.
545 addUInt(Die
, dwarf::DW_AT_const_value
,
546 Unsigned
? dwarf::DW_FORM_udata
: dwarf::DW_FORM_sdata
, Val
);
549 void DwarfUnit::addConstantValue(DIE
&Die
, const APInt
&Val
, const DIType
*Ty
) {
550 addConstantValue(Die
, Val
, isUnsignedDIType(DD
, Ty
));
553 void DwarfUnit::addConstantValue(DIE
&Die
, const APInt
&Val
, bool Unsigned
) {
554 unsigned CIBitWidth
= Val
.getBitWidth();
555 if (CIBitWidth
<= 64) {
556 addConstantValue(Die
, Unsigned
,
557 Unsigned
? Val
.getZExtValue() : Val
.getSExtValue());
561 DIEBlock
*Block
= new (DIEValueAllocator
) DIEBlock
;
563 // Get the raw data form of the large APInt.
564 const uint64_t *Ptr64
= Val
.getRawData();
566 int NumBytes
= Val
.getBitWidth() / 8; // 8 bits per byte.
567 bool LittleEndian
= Asm
->getDataLayout().isLittleEndian();
569 // Output the constant to DWARF one byte at a time.
570 for (int i
= 0; i
< NumBytes
; i
++) {
573 c
= Ptr64
[i
/ 8] >> (8 * (i
& 7));
575 c
= Ptr64
[(NumBytes
- 1 - i
) / 8] >> (8 * ((NumBytes
- 1 - i
) & 7));
576 addUInt(*Block
, dwarf::DW_FORM_data1
, c
);
579 addBlock(Die
, dwarf::DW_AT_const_value
, Block
);
582 void DwarfUnit::addLinkageName(DIE
&Die
, StringRef LinkageName
) {
583 if (!LinkageName
.empty())
585 DD
->getDwarfVersion() >= 4 ? dwarf::DW_AT_linkage_name
586 : dwarf::DW_AT_MIPS_linkage_name
,
587 GlobalValue::dropLLVMManglingEscape(LinkageName
));
590 void DwarfUnit::addTemplateParams(DIE
&Buffer
, DINodeArray TParams
) {
591 // Add template parameters.
592 for (const auto *Element
: TParams
) {
593 if (auto *TTP
= dyn_cast
<DITemplateTypeParameter
>(Element
))
594 constructTemplateTypeParameterDIE(Buffer
, TTP
);
595 else if (auto *TVP
= dyn_cast
<DITemplateValueParameter
>(Element
))
596 constructTemplateValueParameterDIE(Buffer
, TVP
);
600 /// Add thrown types.
601 void DwarfUnit::addThrownTypes(DIE
&Die
, DINodeArray ThrownTypes
) {
602 for (const auto *Ty
: ThrownTypes
) {
603 DIE
&TT
= createAndAddDIE(dwarf::DW_TAG_thrown_type
, Die
);
604 addType(TT
, cast
<DIType
>(Ty
));
608 DIE
*DwarfUnit::getOrCreateContextDIE(const DIScope
*Context
) {
609 if (!Context
|| isa
<DIFile
>(Context
))
610 return &getUnitDie();
611 if (auto *T
= dyn_cast
<DIType
>(Context
))
612 return getOrCreateTypeDIE(T
);
613 if (auto *NS
= dyn_cast
<DINamespace
>(Context
))
614 return getOrCreateNameSpace(NS
);
615 if (auto *SP
= dyn_cast
<DISubprogram
>(Context
))
616 return getOrCreateSubprogramDIE(SP
);
617 if (auto *M
= dyn_cast
<DIModule
>(Context
))
618 return getOrCreateModule(M
);
619 return getDIE(Context
);
622 DIE
*DwarfUnit::createTypeDIE(const DICompositeType
*Ty
) {
623 auto *Context
= Ty
->getScope();
624 DIE
*ContextDIE
= getOrCreateContextDIE(Context
);
626 if (DIE
*TyDIE
= getDIE(Ty
))
630 DIE
&TyDIE
= createAndAddDIE(Ty
->getTag(), *ContextDIE
, Ty
);
632 constructTypeDIE(TyDIE
, cast
<DICompositeType
>(Ty
));
634 updateAcceleratorTables(Context
, Ty
, TyDIE
);
638 DIE
*DwarfUnit::createTypeDIE(const DIScope
*Context
, DIE
&ContextDIE
,
641 DIE
&TyDIE
= createAndAddDIE(Ty
->getTag(), ContextDIE
, Ty
);
643 updateAcceleratorTables(Context
, Ty
, TyDIE
);
645 if (auto *BT
= dyn_cast
<DIBasicType
>(Ty
))
646 constructTypeDIE(TyDIE
, BT
);
647 else if (auto *STy
= dyn_cast
<DISubroutineType
>(Ty
))
648 constructTypeDIE(TyDIE
, STy
);
649 else if (auto *CTy
= dyn_cast
<DICompositeType
>(Ty
)) {
650 if (DD
->generateTypeUnits() && !Ty
->isForwardDecl() &&
651 (Ty
->getRawName() || CTy
->getRawIdentifier())) {
652 // Skip updating the accelerator tables since this is not the full type.
653 if (MDString
*TypeId
= CTy
->getRawIdentifier())
654 DD
->addDwarfTypeUnitType(getCU(), TypeId
->getString(), TyDIE
, CTy
);
656 auto X
= DD
->enterNonTypeUnitContext();
657 finishNonUnitTypeDIE(TyDIE
, CTy
);
661 constructTypeDIE(TyDIE
, CTy
);
663 constructTypeDIE(TyDIE
, cast
<DIDerivedType
>(Ty
));
669 DIE
*DwarfUnit::getOrCreateTypeDIE(const MDNode
*TyNode
) {
673 auto *Ty
= cast
<DIType
>(TyNode
);
675 // DW_TAG_restrict_type is not supported in DWARF2
676 if (Ty
->getTag() == dwarf::DW_TAG_restrict_type
&& DD
->getDwarfVersion() <= 2)
677 return getOrCreateTypeDIE(cast
<DIDerivedType
>(Ty
)->getBaseType());
679 // DW_TAG_atomic_type is not supported in DWARF < 5
680 if (Ty
->getTag() == dwarf::DW_TAG_atomic_type
&& DD
->getDwarfVersion() < 5)
681 return getOrCreateTypeDIE(cast
<DIDerivedType
>(Ty
)->getBaseType());
683 // Construct the context before querying for the existence of the DIE in case
684 // such construction creates the DIE.
685 auto *Context
= Ty
->getScope();
686 DIE
*ContextDIE
= getOrCreateContextDIE(Context
);
689 if (DIE
*TyDIE
= getDIE(Ty
))
692 return static_cast<DwarfUnit
*>(ContextDIE
->getUnit())
693 ->createTypeDIE(Context
, *ContextDIE
, Ty
);
696 void DwarfUnit::updateAcceleratorTables(const DIScope
*Context
,
697 const DIType
*Ty
, const DIE
&TyDIE
) {
698 if (!Ty
->getName().empty() && !Ty
->isForwardDecl()) {
699 bool IsImplementation
= false;
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 IsImplementation
= CT
->getRuntimeLang() == 0 || CT
->isObjcClassComplete();
705 unsigned Flags
= IsImplementation
? dwarf::DW_FLAG_type_implementation
: 0;
706 DD
->addAccelType(*CUNode
, Ty
->getName(), TyDIE
, Flags
);
708 if (!Context
|| isa
<DICompileUnit
>(Context
) || isa
<DIFile
>(Context
) ||
709 isa
<DINamespace
>(Context
) || isa
<DICommonBlock
>(Context
))
710 addGlobalType(Ty
, TyDIE
, Context
);
714 void DwarfUnit::addType(DIE
&Entity
, const DIType
*Ty
,
715 dwarf::Attribute Attribute
) {
716 assert(Ty
&& "Trying to add a type that doesn't exist?");
717 addDIEEntry(Entity
, Attribute
, DIEEntry(*getOrCreateTypeDIE(Ty
)));
720 std::string
DwarfUnit::getParentContextString(const DIScope
*Context
) const {
724 // FIXME: Decide whether to implement this for non-C++ languages.
725 if (getLanguage() != dwarf::DW_LANG_C_plus_plus
)
729 SmallVector
<const DIScope
*, 1> Parents
;
730 while (!isa
<DICompileUnit
>(Context
)) {
731 Parents
.push_back(Context
);
732 if (const DIScope
*S
= Context
->getScope())
735 // Structure, etc types will have a NULL context if they're at the top
740 // Reverse iterate over our list to go from the outermost construct to the
742 for (const DIScope
*Ctx
: make_range(Parents
.rbegin(), Parents
.rend())) {
743 StringRef Name
= Ctx
->getName();
744 if (Name
.empty() && isa
<DINamespace
>(Ctx
))
745 Name
= "(anonymous namespace)";
754 void DwarfUnit::constructTypeDIE(DIE
&Buffer
, const DIBasicType
*BTy
) {
755 // Get core information.
756 StringRef Name
= BTy
->getName();
757 // Add name if not anonymous or intermediate type.
759 addString(Buffer
, dwarf::DW_AT_name
, Name
);
761 // An unspecified type only has a name attribute.
762 if (BTy
->getTag() == dwarf::DW_TAG_unspecified_type
)
765 addUInt(Buffer
, dwarf::DW_AT_encoding
, dwarf::DW_FORM_data1
,
768 uint64_t Size
= BTy
->getSizeInBits() >> 3;
769 addUInt(Buffer
, dwarf::DW_AT_byte_size
, None
, Size
);
771 if (BTy
->isBigEndian())
772 addUInt(Buffer
, dwarf::DW_AT_endianity
, None
, dwarf::DW_END_big
);
773 else if (BTy
->isLittleEndian())
774 addUInt(Buffer
, dwarf::DW_AT_endianity
, None
, dwarf::DW_END_little
);
777 void DwarfUnit::constructTypeDIE(DIE
&Buffer
, const DIDerivedType
*DTy
) {
778 // Get core information.
779 StringRef Name
= DTy
->getName();
780 uint64_t Size
= DTy
->getSizeInBits() >> 3;
781 uint16_t Tag
= Buffer
.getTag();
783 // Map to main type, void will not have a type.
784 const DIType
*FromTy
= DTy
->getBaseType();
786 addType(Buffer
, FromTy
);
788 // Add name if not anonymous or intermediate type.
790 addString(Buffer
, dwarf::DW_AT_name
, Name
);
792 // Add size if non-zero (derived types might be zero-sized.)
793 if (Size
&& Tag
!= dwarf::DW_TAG_pointer_type
794 && Tag
!= dwarf::DW_TAG_ptr_to_member_type
795 && Tag
!= dwarf::DW_TAG_reference_type
796 && Tag
!= dwarf::DW_TAG_rvalue_reference_type
)
797 addUInt(Buffer
, dwarf::DW_AT_byte_size
, None
, Size
);
799 if (Tag
== dwarf::DW_TAG_ptr_to_member_type
)
800 addDIEEntry(Buffer
, dwarf::DW_AT_containing_type
,
801 *getOrCreateTypeDIE(cast
<DIDerivedType
>(DTy
)->getClassType()));
802 // Add source line info if available and TyDesc is not a forward declaration.
803 if (!DTy
->isForwardDecl())
804 addSourceLine(Buffer
, DTy
);
806 // If DWARF address space value is other than None, add it. The IR
807 // verifier checks that DWARF address space only exists for pointer
808 // or reference types.
809 if (DTy
->getDWARFAddressSpace())
810 addUInt(Buffer
, dwarf::DW_AT_address_class
, dwarf::DW_FORM_data4
,
811 DTy
->getDWARFAddressSpace().getValue());
814 void DwarfUnit::constructSubprogramArguments(DIE
&Buffer
, DITypeRefArray Args
) {
815 for (unsigned i
= 1, N
= Args
.size(); i
< N
; ++i
) {
816 const DIType
*Ty
= Args
[i
];
818 assert(i
== N
-1 && "Unspecified parameter must be the last argument");
819 createAndAddDIE(dwarf::DW_TAG_unspecified_parameters
, Buffer
);
821 DIE
&Arg
= createAndAddDIE(dwarf::DW_TAG_formal_parameter
, Buffer
);
823 if (Ty
->isArtificial())
824 addFlag(Arg
, dwarf::DW_AT_artificial
);
829 void DwarfUnit::constructTypeDIE(DIE
&Buffer
, const DISubroutineType
*CTy
) {
830 // Add return type. A void return won't have a type.
831 auto Elements
= cast
<DISubroutineType
>(CTy
)->getTypeArray();
833 if (auto RTy
= Elements
[0])
834 addType(Buffer
, RTy
);
836 bool isPrototyped
= true;
837 if (Elements
.size() == 2 && !Elements
[1])
838 isPrototyped
= false;
840 constructSubprogramArguments(Buffer
, Elements
);
842 // Add prototype flag if we're dealing with a C language and the function has
844 uint16_t Language
= getLanguage();
846 (Language
== dwarf::DW_LANG_C89
|| Language
== dwarf::DW_LANG_C99
||
847 Language
== dwarf::DW_LANG_ObjC
))
848 addFlag(Buffer
, dwarf::DW_AT_prototyped
);
850 // Add a DW_AT_calling_convention if this has an explicit convention.
851 if (CTy
->getCC() && CTy
->getCC() != dwarf::DW_CC_normal
)
852 addUInt(Buffer
, dwarf::DW_AT_calling_convention
, dwarf::DW_FORM_data1
,
855 if (CTy
->isLValueReference())
856 addFlag(Buffer
, dwarf::DW_AT_reference
);
858 if (CTy
->isRValueReference())
859 addFlag(Buffer
, dwarf::DW_AT_rvalue_reference
);
862 void DwarfUnit::constructTypeDIE(DIE
&Buffer
, const DICompositeType
*CTy
) {
863 // Add name if not anonymous or intermediate type.
864 StringRef Name
= CTy
->getName();
866 uint64_t Size
= CTy
->getSizeInBits() >> 3;
867 uint16_t Tag
= Buffer
.getTag();
870 case dwarf::DW_TAG_array_type
:
871 constructArrayTypeDIE(Buffer
, CTy
);
873 case dwarf::DW_TAG_enumeration_type
:
874 constructEnumTypeDIE(Buffer
, CTy
);
876 case dwarf::DW_TAG_variant_part
:
877 case dwarf::DW_TAG_structure_type
:
878 case dwarf::DW_TAG_union_type
:
879 case dwarf::DW_TAG_class_type
: {
880 // Emit the discriminator for a variant part.
881 DIDerivedType
*Discriminator
= nullptr;
882 if (Tag
== dwarf::DW_TAG_variant_part
) {
883 Discriminator
= CTy
->getDiscriminator();
886 // If the variant part has a discriminant, the discriminant is
887 // represented by a separate debugging information entry which is
888 // a child of the variant part entry.
889 DIE
&DiscMember
= constructMemberDIE(Buffer
, Discriminator
);
890 addDIEEntry(Buffer
, dwarf::DW_AT_discr
, DiscMember
);
894 // Add elements to structure type.
895 DINodeArray Elements
= CTy
->getElements();
896 for (const auto *Element
: Elements
) {
899 if (auto *SP
= dyn_cast
<DISubprogram
>(Element
))
900 getOrCreateSubprogramDIE(SP
);
901 else if (auto *DDTy
= dyn_cast
<DIDerivedType
>(Element
)) {
902 if (DDTy
->getTag() == dwarf::DW_TAG_friend
) {
903 DIE
&ElemDie
= createAndAddDIE(dwarf::DW_TAG_friend
, Buffer
);
904 addType(ElemDie
, DDTy
->getBaseType(), dwarf::DW_AT_friend
);
905 } else if (DDTy
->isStaticMember()) {
906 getOrCreateStaticMemberDIE(DDTy
);
907 } else if (Tag
== dwarf::DW_TAG_variant_part
) {
908 // When emitting a variant part, wrap each member in
910 DIE
&Variant
= createAndAddDIE(dwarf::DW_TAG_variant
, Buffer
);
911 if (const ConstantInt
*CI
=
912 dyn_cast_or_null
<ConstantInt
>(DDTy
->getDiscriminantValue())) {
913 if (isUnsignedDIType(DD
, Discriminator
->getBaseType()))
914 addUInt(Variant
, dwarf::DW_AT_discr_value
, None
, CI
->getZExtValue());
916 addSInt(Variant
, dwarf::DW_AT_discr_value
, None
, CI
->getSExtValue());
918 constructMemberDIE(Variant
, DDTy
);
920 constructMemberDIE(Buffer
, DDTy
);
922 } else if (auto *Property
= dyn_cast
<DIObjCProperty
>(Element
)) {
923 DIE
&ElemDie
= createAndAddDIE(Property
->getTag(), Buffer
);
924 StringRef PropertyName
= Property
->getName();
925 addString(ElemDie
, dwarf::DW_AT_APPLE_property_name
, PropertyName
);
926 if (Property
->getType())
927 addType(ElemDie
, Property
->getType());
928 addSourceLine(ElemDie
, Property
);
929 StringRef GetterName
= Property
->getGetterName();
930 if (!GetterName
.empty())
931 addString(ElemDie
, dwarf::DW_AT_APPLE_property_getter
, GetterName
);
932 StringRef SetterName
= Property
->getSetterName();
933 if (!SetterName
.empty())
934 addString(ElemDie
, dwarf::DW_AT_APPLE_property_setter
, SetterName
);
935 if (unsigned PropertyAttributes
= Property
->getAttributes())
936 addUInt(ElemDie
, dwarf::DW_AT_APPLE_property_attribute
, None
,
938 } else if (auto *Composite
= dyn_cast
<DICompositeType
>(Element
)) {
939 if (Composite
->getTag() == dwarf::DW_TAG_variant_part
) {
940 DIE
&VariantPart
= createAndAddDIE(Composite
->getTag(), Buffer
);
941 constructTypeDIE(VariantPart
, Composite
);
946 if (CTy
->isAppleBlockExtension())
947 addFlag(Buffer
, dwarf::DW_AT_APPLE_block
);
949 if (CTy
->getExportSymbols())
950 addFlag(Buffer
, dwarf::DW_AT_export_symbols
);
952 // This is outside the DWARF spec, but GDB expects a DW_AT_containing_type
953 // inside C++ composite types to point to the base class with the vtable.
954 // Rust uses DW_AT_containing_type to link a vtable to the type
955 // for which it was created.
956 if (auto *ContainingType
= CTy
->getVTableHolder())
957 addDIEEntry(Buffer
, dwarf::DW_AT_containing_type
,
958 *getOrCreateTypeDIE(ContainingType
));
960 if (CTy
->isObjcClassComplete())
961 addFlag(Buffer
, dwarf::DW_AT_APPLE_objc_complete_type
);
963 // Add template parameters to a class, structure or union types.
964 // FIXME: The support isn't in the metadata for this yet.
965 if (Tag
== dwarf::DW_TAG_class_type
||
966 Tag
== dwarf::DW_TAG_structure_type
|| Tag
== dwarf::DW_TAG_union_type
)
967 addTemplateParams(Buffer
, CTy
->getTemplateParams());
969 // Add the type's non-standard calling convention.
971 if (CTy
->isTypePassByValue())
972 CC
= dwarf::DW_CC_pass_by_value
;
973 else if (CTy
->isTypePassByReference())
974 CC
= dwarf::DW_CC_pass_by_reference
;
976 addUInt(Buffer
, dwarf::DW_AT_calling_convention
, dwarf::DW_FORM_data1
,
984 // Add name if not anonymous or intermediate type.
986 addString(Buffer
, dwarf::DW_AT_name
, Name
);
988 if (Tag
== dwarf::DW_TAG_enumeration_type
||
989 Tag
== dwarf::DW_TAG_class_type
|| Tag
== dwarf::DW_TAG_structure_type
||
990 Tag
== dwarf::DW_TAG_union_type
) {
991 // Add size if non-zero (derived types might be zero-sized.)
992 // TODO: Do we care about size for enum forward declarations?
994 addUInt(Buffer
, dwarf::DW_AT_byte_size
, None
, Size
);
995 else if (!CTy
->isForwardDecl())
996 // Add zero size if it is not a forward declaration.
997 addUInt(Buffer
, dwarf::DW_AT_byte_size
, None
, 0);
999 // If we're a forward decl, say so.
1000 if (CTy
->isForwardDecl())
1001 addFlag(Buffer
, dwarf::DW_AT_declaration
);
1003 // Add source line info if available.
1004 if (!CTy
->isForwardDecl())
1005 addSourceLine(Buffer
, CTy
);
1007 // No harm in adding the runtime language to the declaration.
1008 unsigned RLang
= CTy
->getRuntimeLang();
1010 addUInt(Buffer
, dwarf::DW_AT_APPLE_runtime_class
, dwarf::DW_FORM_data1
,
1013 // Add align info if available.
1014 if (uint32_t AlignInBytes
= CTy
->getAlignInBytes())
1015 addUInt(Buffer
, dwarf::DW_AT_alignment
, dwarf::DW_FORM_udata
,
1020 void DwarfUnit::constructTemplateTypeParameterDIE(
1021 DIE
&Buffer
, const DITemplateTypeParameter
*TP
) {
1023 createAndAddDIE(dwarf::DW_TAG_template_type_parameter
, Buffer
);
1024 // Add the type if it exists, it could be void and therefore no type.
1026 addType(ParamDIE
, TP
->getType());
1027 if (!TP
->getName().empty())
1028 addString(ParamDIE
, dwarf::DW_AT_name
, TP
->getName());
1031 void DwarfUnit::constructTemplateValueParameterDIE(
1032 DIE
&Buffer
, const DITemplateValueParameter
*VP
) {
1033 DIE
&ParamDIE
= createAndAddDIE(VP
->getTag(), Buffer
);
1035 // Add the type if there is one, template template and template parameter
1036 // packs will not have a type.
1037 if (VP
->getTag() == dwarf::DW_TAG_template_value_parameter
)
1038 addType(ParamDIE
, VP
->getType());
1039 if (!VP
->getName().empty())
1040 addString(ParamDIE
, dwarf::DW_AT_name
, VP
->getName());
1041 if (Metadata
*Val
= VP
->getValue()) {
1042 if (ConstantInt
*CI
= mdconst::dyn_extract
<ConstantInt
>(Val
))
1043 addConstantValue(ParamDIE
, CI
, VP
->getType());
1044 else if (GlobalValue
*GV
= mdconst::dyn_extract
<GlobalValue
>(Val
)) {
1045 // We cannot describe the location of dllimport'd entities: the
1046 // computation of their address requires loads from the IAT.
1047 if (!GV
->hasDLLImportStorageClass()) {
1048 // For declaration non-type template parameters (such as global values
1050 DIELoc
*Loc
= new (DIEValueAllocator
) DIELoc
;
1051 addOpAddress(*Loc
, Asm
->getSymbol(GV
));
1052 // Emit DW_OP_stack_value to use the address as the immediate value of
1053 // the parameter, rather than a pointer to it.
1054 addUInt(*Loc
, dwarf::DW_FORM_data1
, dwarf::DW_OP_stack_value
);
1055 addBlock(ParamDIE
, dwarf::DW_AT_location
, Loc
);
1057 } else if (VP
->getTag() == dwarf::DW_TAG_GNU_template_template_param
) {
1058 assert(isa
<MDString
>(Val
));
1059 addString(ParamDIE
, dwarf::DW_AT_GNU_template_name
,
1060 cast
<MDString
>(Val
)->getString());
1061 } else if (VP
->getTag() == dwarf::DW_TAG_GNU_template_parameter_pack
) {
1062 addTemplateParams(ParamDIE
, cast
<MDTuple
>(Val
));
1067 DIE
*DwarfUnit::getOrCreateNameSpace(const DINamespace
*NS
) {
1068 // Construct the context before querying for the existence of the DIE in case
1069 // such construction creates the DIE.
1070 DIE
*ContextDIE
= getOrCreateContextDIE(NS
->getScope());
1072 if (DIE
*NDie
= getDIE(NS
))
1074 DIE
&NDie
= createAndAddDIE(dwarf::DW_TAG_namespace
, *ContextDIE
, NS
);
1076 StringRef Name
= NS
->getName();
1078 addString(NDie
, dwarf::DW_AT_name
, NS
->getName());
1080 Name
= "(anonymous namespace)";
1081 DD
->addAccelNamespace(*CUNode
, Name
, NDie
);
1082 addGlobalName(Name
, NDie
, NS
->getScope());
1083 if (NS
->getExportSymbols())
1084 addFlag(NDie
, dwarf::DW_AT_export_symbols
);
1088 DIE
*DwarfUnit::getOrCreateModule(const DIModule
*M
) {
1089 // Construct the context before querying for the existence of the DIE in case
1090 // such construction creates the DIE.
1091 DIE
*ContextDIE
= getOrCreateContextDIE(M
->getScope());
1093 if (DIE
*MDie
= getDIE(M
))
1095 DIE
&MDie
= createAndAddDIE(dwarf::DW_TAG_module
, *ContextDIE
, M
);
1097 if (!M
->getName().empty()) {
1098 addString(MDie
, dwarf::DW_AT_name
, M
->getName());
1099 addGlobalName(M
->getName(), MDie
, M
->getScope());
1101 if (!M
->getConfigurationMacros().empty())
1102 addString(MDie
, dwarf::DW_AT_LLVM_config_macros
,
1103 M
->getConfigurationMacros());
1104 if (!M
->getIncludePath().empty())
1105 addString(MDie
, dwarf::DW_AT_LLVM_include_path
, M
->getIncludePath());
1106 if (!M
->getISysRoot().empty())
1107 addString(MDie
, dwarf::DW_AT_LLVM_isysroot
, M
->getISysRoot());
1112 DIE
*DwarfUnit::getOrCreateSubprogramDIE(const DISubprogram
*SP
, bool Minimal
) {
1113 // Construct the context before querying for the existence of the DIE in case
1114 // such construction creates the DIE (as is the case for member function
1117 Minimal
? &getUnitDie() : getOrCreateContextDIE(SP
->getScope());
1119 if (DIE
*SPDie
= getDIE(SP
))
1122 if (auto *SPDecl
= SP
->getDeclaration()) {
1124 // Add subprogram definitions to the CU die directly.
1125 ContextDIE
= &getUnitDie();
1126 // Build the decl now to ensure it precedes the definition.
1127 getOrCreateSubprogramDIE(SPDecl
);
1131 // DW_TAG_inlined_subroutine may refer to this DIE.
1132 DIE
&SPDie
= createAndAddDIE(dwarf::DW_TAG_subprogram
, *ContextDIE
, SP
);
1134 // Stop here and fill this in later, depending on whether or not this
1135 // subprogram turns out to have inlined instances or not.
1136 if (SP
->isDefinition())
1139 static_cast<DwarfUnit
*>(SPDie
.getUnit())
1140 ->applySubprogramAttributes(SP
, SPDie
);
1144 bool DwarfUnit::applySubprogramDefinitionAttributes(const DISubprogram
*SP
,
1146 DIE
*DeclDie
= nullptr;
1147 StringRef DeclLinkageName
;
1148 if (auto *SPDecl
= SP
->getDeclaration()) {
1149 DeclDie
= getDIE(SPDecl
);
1150 assert(DeclDie
&& "This DIE should've already been constructed when the "
1151 "definition DIE was created in "
1152 "getOrCreateSubprogramDIE");
1153 // Look at the Decl's linkage name only if we emitted it.
1154 if (DD
->useAllLinkageNames())
1155 DeclLinkageName
= SPDecl
->getLinkageName();
1156 unsigned DeclID
= getOrCreateSourceID(SPDecl
->getFile());
1157 unsigned DefID
= getOrCreateSourceID(SP
->getFile());
1158 if (DeclID
!= DefID
)
1159 addUInt(SPDie
, dwarf::DW_AT_decl_file
, None
, DefID
);
1161 if (SP
->getLine() != SPDecl
->getLine())
1162 addUInt(SPDie
, dwarf::DW_AT_decl_line
, None
, SP
->getLine());
1165 // Add function template parameters.
1166 addTemplateParams(SPDie
, SP
->getTemplateParams());
1168 // Add the linkage name if we have one and it isn't in the Decl.
1169 StringRef LinkageName
= SP
->getLinkageName();
1170 assert(((LinkageName
.empty() || DeclLinkageName
.empty()) ||
1171 LinkageName
== DeclLinkageName
) &&
1172 "decl has a linkage name and it is different");
1173 if (DeclLinkageName
.empty() &&
1174 // Always emit it for abstract subprograms.
1175 (DD
->useAllLinkageNames() || DU
->getAbstractSPDies().lookup(SP
)))
1176 addLinkageName(SPDie
, LinkageName
);
1181 // Refer to the function declaration where all the other attributes will be
1183 addDIEEntry(SPDie
, dwarf::DW_AT_specification
, *DeclDie
);
1187 void DwarfUnit::applySubprogramAttributes(const DISubprogram
*SP
, DIE
&SPDie
,
1188 bool SkipSPAttributes
) {
1189 // If -fdebug-info-for-profiling is enabled, need to emit the subprogram
1190 // and its source location.
1191 bool SkipSPSourceLocation
= SkipSPAttributes
&&
1192 !CUNode
->getDebugInfoForProfiling();
1193 if (!SkipSPSourceLocation
)
1194 if (applySubprogramDefinitionAttributes(SP
, SPDie
))
1197 // Constructors and operators for anonymous aggregates do not have names.
1198 if (!SP
->getName().empty())
1199 addString(SPDie
, dwarf::DW_AT_name
, SP
->getName());
1201 if (!SkipSPSourceLocation
)
1202 addSourceLine(SPDie
, SP
);
1204 // Skip the rest of the attributes under -gmlt to save space.
1205 if (SkipSPAttributes
)
1208 // Add the prototype if we have a prototype and we have a C like
1210 uint16_t Language
= getLanguage();
1211 if (SP
->isPrototyped() &&
1212 (Language
== dwarf::DW_LANG_C89
|| Language
== dwarf::DW_LANG_C99
||
1213 Language
== dwarf::DW_LANG_ObjC
))
1214 addFlag(SPDie
, dwarf::DW_AT_prototyped
);
1217 DITypeRefArray Args
;
1218 if (const DISubroutineType
*SPTy
= SP
->getType()) {
1219 Args
= SPTy
->getTypeArray();
1223 // Add a DW_AT_calling_convention if this has an explicit convention.
1224 if (CC
&& CC
!= dwarf::DW_CC_normal
)
1225 addUInt(SPDie
, dwarf::DW_AT_calling_convention
, dwarf::DW_FORM_data1
, CC
);
1227 // Add a return type. If this is a type like a C/C++ void type we don't add a
1230 if (auto Ty
= Args
[0])
1233 unsigned VK
= SP
->getVirtuality();
1235 addUInt(SPDie
, dwarf::DW_AT_virtuality
, dwarf::DW_FORM_data1
, VK
);
1236 if (SP
->getVirtualIndex() != -1u) {
1237 DIELoc
*Block
= getDIELoc();
1238 addUInt(*Block
, dwarf::DW_FORM_data1
, dwarf::DW_OP_constu
);
1239 addUInt(*Block
, dwarf::DW_FORM_udata
, SP
->getVirtualIndex());
1240 addBlock(SPDie
, dwarf::DW_AT_vtable_elem_location
, Block
);
1242 ContainingTypeMap
.insert(std::make_pair(&SPDie
, SP
->getContainingType()));
1245 if (!SP
->isDefinition()) {
1246 addFlag(SPDie
, dwarf::DW_AT_declaration
);
1248 // Add arguments. Do not add arguments for subprogram definition. They will
1249 // be handled while processing variables.
1250 constructSubprogramArguments(SPDie
, Args
);
1253 addThrownTypes(SPDie
, SP
->getThrownTypes());
1255 if (SP
->isArtificial())
1256 addFlag(SPDie
, dwarf::DW_AT_artificial
);
1258 if (!SP
->isLocalToUnit())
1259 addFlag(SPDie
, dwarf::DW_AT_external
);
1261 if (DD
->useAppleExtensionAttributes()) {
1262 if (SP
->isOptimized())
1263 addFlag(SPDie
, dwarf::DW_AT_APPLE_optimized
);
1265 if (unsigned isa
= Asm
->getISAEncoding())
1266 addUInt(SPDie
, dwarf::DW_AT_APPLE_isa
, dwarf::DW_FORM_flag
, isa
);
1269 if (SP
->isLValueReference())
1270 addFlag(SPDie
, dwarf::DW_AT_reference
);
1272 if (SP
->isRValueReference())
1273 addFlag(SPDie
, dwarf::DW_AT_rvalue_reference
);
1275 if (SP
->isNoReturn())
1276 addFlag(SPDie
, dwarf::DW_AT_noreturn
);
1278 if (SP
->isProtected())
1279 addUInt(SPDie
, dwarf::DW_AT_accessibility
, dwarf::DW_FORM_data1
,
1280 dwarf::DW_ACCESS_protected
);
1281 else if (SP
->isPrivate())
1282 addUInt(SPDie
, dwarf::DW_AT_accessibility
, dwarf::DW_FORM_data1
,
1283 dwarf::DW_ACCESS_private
);
1284 else if (SP
->isPublic())
1285 addUInt(SPDie
, dwarf::DW_AT_accessibility
, dwarf::DW_FORM_data1
,
1286 dwarf::DW_ACCESS_public
);
1288 if (SP
->isExplicit())
1289 addFlag(SPDie
, dwarf::DW_AT_explicit
);
1291 if (SP
->isMainSubprogram())
1292 addFlag(SPDie
, dwarf::DW_AT_main_subprogram
);
1294 addFlag(SPDie
, dwarf::DW_AT_pure
);
1295 if (SP
->isElemental())
1296 addFlag(SPDie
, dwarf::DW_AT_elemental
);
1297 if (SP
->isRecursive())
1298 addFlag(SPDie
, dwarf::DW_AT_recursive
);
1301 void DwarfUnit::constructSubrangeDIE(DIE
&Buffer
, const DISubrange
*SR
,
1303 DIE
&DW_Subrange
= createAndAddDIE(dwarf::DW_TAG_subrange_type
, Buffer
);
1304 addDIEEntry(DW_Subrange
, dwarf::DW_AT_type
, *IndexTy
);
1306 // The LowerBound value defines the lower bounds which is typically zero for
1307 // C/C++. The Count value is the number of elements. Values are 64 bit. If
1308 // Count == -1 then the array is unbounded and we do not emit
1309 // DW_AT_lower_bound and DW_AT_count attributes.
1310 int64_t LowerBound
= SR
->getLowerBound();
1311 int64_t DefaultLowerBound
= getDefaultLowerBound();
1313 if (auto *CI
= SR
->getCount().dyn_cast
<ConstantInt
*>())
1314 Count
= CI
->getSExtValue();
1316 if (DefaultLowerBound
== -1 || LowerBound
!= DefaultLowerBound
)
1317 addUInt(DW_Subrange
, dwarf::DW_AT_lower_bound
, None
, LowerBound
);
1319 if (auto *CV
= SR
->getCount().dyn_cast
<DIVariable
*>()) {
1320 if (auto *CountVarDIE
= getDIE(CV
))
1321 addDIEEntry(DW_Subrange
, dwarf::DW_AT_count
, *CountVarDIE
);
1322 } else if (Count
!= -1)
1323 addUInt(DW_Subrange
, dwarf::DW_AT_count
, None
, Count
);
1326 DIE
*DwarfUnit::getIndexTyDie() {
1329 // Construct an integer type to use for indexes.
1330 IndexTyDie
= &createAndAddDIE(dwarf::DW_TAG_base_type
, getUnitDie());
1331 StringRef Name
= "__ARRAY_SIZE_TYPE__";
1332 addString(*IndexTyDie
, dwarf::DW_AT_name
, Name
);
1333 addUInt(*IndexTyDie
, dwarf::DW_AT_byte_size
, None
, sizeof(int64_t));
1334 addUInt(*IndexTyDie
, dwarf::DW_AT_encoding
, dwarf::DW_FORM_data1
,
1335 dwarf::DW_ATE_unsigned
);
1336 DD
->addAccelType(*CUNode
, Name
, *IndexTyDie
, /*Flags*/ 0);
1340 /// Returns true if the vector's size differs from the sum of sizes of elements
1341 /// the user specified. This can occur if the vector has been rounded up to
1342 /// fit memory alignment constraints.
1343 static bool hasVectorBeenPadded(const DICompositeType
*CTy
) {
1344 assert(CTy
&& CTy
->isVector() && "Composite type is not a vector");
1345 const uint64_t ActualSize
= CTy
->getSizeInBits();
1347 // Obtain the size of each element in the vector.
1348 DIType
*BaseTy
= CTy
->getBaseType();
1349 assert(BaseTy
&& "Unknown vector element type.");
1350 const uint64_t ElementSize
= BaseTy
->getSizeInBits();
1352 // Locate the number of elements in the vector.
1353 const DINodeArray Elements
= CTy
->getElements();
1354 assert(Elements
.size() == 1 &&
1355 Elements
[0]->getTag() == dwarf::DW_TAG_subrange_type
&&
1356 "Invalid vector element array, expected one element of type subrange");
1357 const auto Subrange
= cast
<DISubrange
>(Elements
[0]);
1358 const auto CI
= Subrange
->getCount().get
<ConstantInt
*>();
1359 const int32_t NumVecElements
= CI
->getSExtValue();
1361 // Ensure we found the element count and that the actual size is wide
1362 // enough to contain the requested size.
1363 assert(ActualSize
>= (NumVecElements
* ElementSize
) && "Invalid vector size");
1364 return ActualSize
!= (NumVecElements
* ElementSize
);
1367 void DwarfUnit::constructArrayTypeDIE(DIE
&Buffer
, const DICompositeType
*CTy
) {
1368 if (CTy
->isVector()) {
1369 addFlag(Buffer
, dwarf::DW_AT_GNU_vector
);
1370 if (hasVectorBeenPadded(CTy
))
1371 addUInt(Buffer
, dwarf::DW_AT_byte_size
, None
,
1372 CTy
->getSizeInBits() / CHAR_BIT
);
1375 // Emit the element type.
1376 addType(Buffer
, CTy
->getBaseType());
1378 // Get an anonymous type for index type.
1379 // FIXME: This type should be passed down from the front end
1380 // as different languages may have different sizes for indexes.
1381 DIE
*IdxTy
= getIndexTyDie();
1383 // Add subranges to array type.
1384 DINodeArray Elements
= CTy
->getElements();
1385 for (unsigned i
= 0, N
= Elements
.size(); i
< N
; ++i
) {
1386 // FIXME: Should this really be such a loose cast?
1387 if (auto *Element
= dyn_cast_or_null
<DINode
>(Elements
[i
]))
1388 if (Element
->getTag() == dwarf::DW_TAG_subrange_type
)
1389 constructSubrangeDIE(Buffer
, cast
<DISubrange
>(Element
), IdxTy
);
1393 void DwarfUnit::constructEnumTypeDIE(DIE
&Buffer
, const DICompositeType
*CTy
) {
1394 const DIType
*DTy
= CTy
->getBaseType();
1395 bool IsUnsigned
= DTy
&& isUnsignedDIType(DD
, DTy
);
1397 if (DD
->getDwarfVersion() >= 3)
1398 addType(Buffer
, DTy
);
1399 if (DD
->getDwarfVersion() >= 4 && (CTy
->getFlags() & DINode::FlagEnumClass
))
1400 addFlag(Buffer
, dwarf::DW_AT_enum_class
);
1403 auto *Context
= CTy
->getScope();
1404 bool IndexEnumerators
= !Context
|| isa
<DICompileUnit
>(Context
) || isa
<DIFile
>(Context
) ||
1405 isa
<DINamespace
>(Context
) || isa
<DICommonBlock
>(Context
);
1406 DINodeArray Elements
= CTy
->getElements();
1408 // Add enumerators to enumeration type.
1409 for (unsigned i
= 0, N
= Elements
.size(); i
< N
; ++i
) {
1410 auto *Enum
= dyn_cast_or_null
<DIEnumerator
>(Elements
[i
]);
1412 DIE
&Enumerator
= createAndAddDIE(dwarf::DW_TAG_enumerator
, Buffer
);
1413 StringRef Name
= Enum
->getName();
1414 addString(Enumerator
, dwarf::DW_AT_name
, Name
);
1415 auto Value
= static_cast<uint64_t>(Enum
->getValue());
1416 addConstantValue(Enumerator
, IsUnsigned
, Value
);
1417 if (IndexEnumerators
)
1418 addGlobalName(Name
, Enumerator
, Context
);
1423 void DwarfUnit::constructContainingTypeDIEs() {
1424 for (auto CI
= ContainingTypeMap
.begin(), CE
= ContainingTypeMap
.end();
1426 DIE
&SPDie
= *CI
->first
;
1427 const DINode
*D
= CI
->second
;
1430 DIE
*NDie
= getDIE(D
);
1433 addDIEEntry(SPDie
, dwarf::DW_AT_containing_type
, *NDie
);
1437 DIE
&DwarfUnit::constructMemberDIE(DIE
&Buffer
, const DIDerivedType
*DT
) {
1438 DIE
&MemberDie
= createAndAddDIE(DT
->getTag(), Buffer
);
1439 StringRef Name
= DT
->getName();
1441 addString(MemberDie
, dwarf::DW_AT_name
, Name
);
1443 if (DIType
*Resolved
= DT
->getBaseType())
1444 addType(MemberDie
, Resolved
);
1446 addSourceLine(MemberDie
, DT
);
1448 if (DT
->getTag() == dwarf::DW_TAG_inheritance
&& DT
->isVirtual()) {
1450 // For C++, virtual base classes are not at fixed offset. Use following
1451 // expression to extract appropriate offset from vtable.
1452 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1454 DIELoc
*VBaseLocationDie
= new (DIEValueAllocator
) DIELoc
;
1455 addUInt(*VBaseLocationDie
, dwarf::DW_FORM_data1
, dwarf::DW_OP_dup
);
1456 addUInt(*VBaseLocationDie
, dwarf::DW_FORM_data1
, dwarf::DW_OP_deref
);
1457 addUInt(*VBaseLocationDie
, dwarf::DW_FORM_data1
, dwarf::DW_OP_constu
);
1458 addUInt(*VBaseLocationDie
, dwarf::DW_FORM_udata
, DT
->getOffsetInBits());
1459 addUInt(*VBaseLocationDie
, dwarf::DW_FORM_data1
, dwarf::DW_OP_minus
);
1460 addUInt(*VBaseLocationDie
, dwarf::DW_FORM_data1
, dwarf::DW_OP_deref
);
1461 addUInt(*VBaseLocationDie
, dwarf::DW_FORM_data1
, dwarf::DW_OP_plus
);
1463 addBlock(MemberDie
, dwarf::DW_AT_data_member_location
, VBaseLocationDie
);
1465 uint64_t Size
= DT
->getSizeInBits();
1466 uint64_t FieldSize
= DD
->getBaseTypeSize(DT
);
1467 uint32_t AlignInBytes
= DT
->getAlignInBytes();
1468 uint64_t OffsetInBytes
;
1470 bool IsBitfield
= FieldSize
&& Size
!= FieldSize
;
1472 // Handle bitfield, assume bytes are 8 bits.
1473 if (DD
->useDWARF2Bitfields())
1474 addUInt(MemberDie
, dwarf::DW_AT_byte_size
, None
, FieldSize
/8);
1475 addUInt(MemberDie
, dwarf::DW_AT_bit_size
, None
, Size
);
1477 uint64_t Offset
= DT
->getOffsetInBits();
1478 // We can't use DT->getAlignInBits() here: AlignInBits for member type
1479 // is non-zero if and only if alignment was forced (e.g. _Alignas()),
1480 // which can't be done with bitfields. Thus we use FieldSize here.
1481 uint32_t AlignInBits
= FieldSize
;
1482 uint32_t AlignMask
= ~(AlignInBits
- 1);
1483 // The bits from the start of the storage unit to the start of the field.
1484 uint64_t StartBitOffset
= Offset
- (Offset
& AlignMask
);
1485 // The byte offset of the field's aligned storage unit inside the struct.
1486 OffsetInBytes
= (Offset
- StartBitOffset
) / 8;
1488 if (DD
->useDWARF2Bitfields()) {
1489 uint64_t HiMark
= (Offset
+ FieldSize
) & AlignMask
;
1490 uint64_t FieldOffset
= (HiMark
- FieldSize
);
1491 Offset
-= FieldOffset
;
1493 // Maybe we need to work from the other end.
1494 if (Asm
->getDataLayout().isLittleEndian())
1495 Offset
= FieldSize
- (Offset
+ Size
);
1497 addUInt(MemberDie
, dwarf::DW_AT_bit_offset
, None
, Offset
);
1498 OffsetInBytes
= FieldOffset
>> 3;
1500 addUInt(MemberDie
, dwarf::DW_AT_data_bit_offset
, None
, Offset
);
1503 // This is not a bitfield.
1504 OffsetInBytes
= DT
->getOffsetInBits() / 8;
1506 addUInt(MemberDie
, dwarf::DW_AT_alignment
, dwarf::DW_FORM_udata
,
1510 if (DD
->getDwarfVersion() <= 2) {
1511 DIELoc
*MemLocationDie
= new (DIEValueAllocator
) DIELoc
;
1512 addUInt(*MemLocationDie
, dwarf::DW_FORM_data1
, dwarf::DW_OP_plus_uconst
);
1513 addUInt(*MemLocationDie
, dwarf::DW_FORM_udata
, OffsetInBytes
);
1514 addBlock(MemberDie
, dwarf::DW_AT_data_member_location
, MemLocationDie
);
1515 } else if (!IsBitfield
|| DD
->useDWARF2Bitfields())
1516 addUInt(MemberDie
, dwarf::DW_AT_data_member_location
, None
,
1520 if (DT
->isProtected())
1521 addUInt(MemberDie
, dwarf::DW_AT_accessibility
, dwarf::DW_FORM_data1
,
1522 dwarf::DW_ACCESS_protected
);
1523 else if (DT
->isPrivate())
1524 addUInt(MemberDie
, dwarf::DW_AT_accessibility
, dwarf::DW_FORM_data1
,
1525 dwarf::DW_ACCESS_private
);
1526 // Otherwise C++ member and base classes are considered public.
1527 else if (DT
->isPublic())
1528 addUInt(MemberDie
, dwarf::DW_AT_accessibility
, dwarf::DW_FORM_data1
,
1529 dwarf::DW_ACCESS_public
);
1530 if (DT
->isVirtual())
1531 addUInt(MemberDie
, dwarf::DW_AT_virtuality
, dwarf::DW_FORM_data1
,
1532 dwarf::DW_VIRTUALITY_virtual
);
1534 // Objective-C properties.
1535 if (DINode
*PNode
= DT
->getObjCProperty())
1536 if (DIE
*PDie
= getDIE(PNode
))
1537 MemberDie
.addValue(DIEValueAllocator
, dwarf::DW_AT_APPLE_property
,
1538 dwarf::DW_FORM_ref4
, DIEEntry(*PDie
));
1540 if (DT
->isArtificial())
1541 addFlag(MemberDie
, dwarf::DW_AT_artificial
);
1546 DIE
*DwarfUnit::getOrCreateStaticMemberDIE(const DIDerivedType
*DT
) {
1550 // Construct the context before querying for the existence of the DIE in case
1551 // such construction creates the DIE.
1552 DIE
*ContextDIE
= getOrCreateContextDIE(DT
->getScope());
1553 assert(dwarf::isType(ContextDIE
->getTag()) &&
1554 "Static member should belong to a type.");
1556 if (DIE
*StaticMemberDIE
= getDIE(DT
))
1557 return StaticMemberDIE
;
1559 DIE
&StaticMemberDIE
= createAndAddDIE(DT
->getTag(), *ContextDIE
, DT
);
1561 const DIType
*Ty
= DT
->getBaseType();
1563 addString(StaticMemberDIE
, dwarf::DW_AT_name
, DT
->getName());
1564 addType(StaticMemberDIE
, Ty
);
1565 addSourceLine(StaticMemberDIE
, DT
);
1566 addFlag(StaticMemberDIE
, dwarf::DW_AT_external
);
1567 addFlag(StaticMemberDIE
, dwarf::DW_AT_declaration
);
1569 // FIXME: We could omit private if the parent is a class_type, and
1570 // public if the parent is something else.
1571 if (DT
->isProtected())
1572 addUInt(StaticMemberDIE
, dwarf::DW_AT_accessibility
, dwarf::DW_FORM_data1
,
1573 dwarf::DW_ACCESS_protected
);
1574 else if (DT
->isPrivate())
1575 addUInt(StaticMemberDIE
, dwarf::DW_AT_accessibility
, dwarf::DW_FORM_data1
,
1576 dwarf::DW_ACCESS_private
);
1577 else if (DT
->isPublic())
1578 addUInt(StaticMemberDIE
, dwarf::DW_AT_accessibility
, dwarf::DW_FORM_data1
,
1579 dwarf::DW_ACCESS_public
);
1581 if (const ConstantInt
*CI
= dyn_cast_or_null
<ConstantInt
>(DT
->getConstant()))
1582 addConstantValue(StaticMemberDIE
, CI
, Ty
);
1583 if (const ConstantFP
*CFP
= dyn_cast_or_null
<ConstantFP
>(DT
->getConstant()))
1584 addConstantFPValue(StaticMemberDIE
, CFP
);
1586 if (uint32_t AlignInBytes
= DT
->getAlignInBytes())
1587 addUInt(StaticMemberDIE
, dwarf::DW_AT_alignment
, dwarf::DW_FORM_udata
,
1590 return &StaticMemberDIE
;
1593 void DwarfUnit::emitCommonHeader(bool UseOffsets
, dwarf::UnitType UT
) {
1594 // Emit size of content not including length itself
1595 Asm
->OutStreamer
->AddComment("Length of Unit");
1596 if (!DD
->useSectionsAsReferences()) {
1597 StringRef Prefix
= isDwoUnit() ? "debug_info_dwo_" : "debug_info_";
1598 MCSymbol
*BeginLabel
= Asm
->createTempSymbol(Prefix
+ "start");
1599 EndLabel
= Asm
->createTempSymbol(Prefix
+ "end");
1600 Asm
->EmitLabelDifference(EndLabel
, BeginLabel
, 4);
1601 Asm
->OutStreamer
->EmitLabel(BeginLabel
);
1603 Asm
->emitInt32(getHeaderSize() + getUnitDie().getSize());
1605 Asm
->OutStreamer
->AddComment("DWARF version number");
1606 unsigned Version
= DD
->getDwarfVersion();
1607 Asm
->emitInt16(Version
);
1609 // DWARF v5 reorders the address size and adds a unit type.
1611 Asm
->OutStreamer
->AddComment("DWARF Unit Type");
1613 Asm
->OutStreamer
->AddComment("Address Size (in bytes)");
1614 Asm
->emitInt8(Asm
->MAI
->getCodePointerSize());
1617 // We share one abbreviations table across all units so it's always at the
1618 // start of the section. Use a relocatable offset where needed to ensure
1619 // linking doesn't invalidate that offset.
1620 Asm
->OutStreamer
->AddComment("Offset Into Abbrev. Section");
1621 const TargetLoweringObjectFile
&TLOF
= Asm
->getObjFileLowering();
1625 Asm
->emitDwarfSymbolReference(
1626 TLOF
.getDwarfAbbrevSection()->getBeginSymbol(), false);
1629 Asm
->OutStreamer
->AddComment("Address Size (in bytes)");
1630 Asm
->emitInt8(Asm
->MAI
->getCodePointerSize());
1634 void DwarfTypeUnit::emitHeader(bool UseOffsets
) {
1635 DwarfUnit::emitCommonHeader(UseOffsets
,
1636 DD
->useSplitDwarf() ? dwarf::DW_UT_split_type
1637 : dwarf::DW_UT_type
);
1638 Asm
->OutStreamer
->AddComment("Type Signature");
1639 Asm
->OutStreamer
->EmitIntValue(TypeSignature
, sizeof(TypeSignature
));
1640 Asm
->OutStreamer
->AddComment("Type DIE Offset");
1641 // In a skeleton type unit there is no type DIE so emit a zero offset.
1642 Asm
->OutStreamer
->EmitIntValue(Ty
? Ty
->getOffset() : 0,
1643 sizeof(Ty
->getOffset()));
1647 DwarfUnit::addSectionDelta(DIE
&Die
, dwarf::Attribute Attribute
,
1648 const MCSymbol
*Hi
, const MCSymbol
*Lo
) {
1649 return Die
.addValue(DIEValueAllocator
, Attribute
,
1650 DD
->getDwarfVersion() >= 4 ? dwarf::DW_FORM_sec_offset
1651 : dwarf::DW_FORM_data4
,
1652 new (DIEValueAllocator
) DIEDelta(Hi
, Lo
));
1656 DwarfUnit::addSectionLabel(DIE
&Die
, dwarf::Attribute Attribute
,
1657 const MCSymbol
*Label
, const MCSymbol
*Sec
) {
1658 if (Asm
->MAI
->doesDwarfUseRelocationsAcrossSections())
1659 return addLabel(Die
, Attribute
,
1660 DD
->getDwarfVersion() >= 4 ? dwarf::DW_FORM_sec_offset
1661 : dwarf::DW_FORM_data4
,
1663 return addSectionDelta(Die
, Attribute
, Label
, Sec
);
1666 bool DwarfTypeUnit::isDwoUnit() const {
1667 // Since there are no skeleton type units, all type units are dwo type units
1668 // when split DWARF is being used.
1669 return DD
->useSplitDwarf();
1672 void DwarfTypeUnit::addGlobalName(StringRef Name
, const DIE
&Die
,
1673 const DIScope
*Context
) {
1674 getCU().addGlobalNameForTypeUnit(Name
, Context
);
1677 void DwarfTypeUnit::addGlobalType(const DIType
*Ty
, const DIE
&Die
,
1678 const DIScope
*Context
) {
1679 getCU().addGlobalTypeUnitType(Ty
, Context
);
1682 const MCSymbol
*DwarfUnit::getCrossSectionRelativeBaseAddress() const {
1683 if (!Asm
->MAI
->doesDwarfUseRelocationsAcrossSections())
1687 return getSection()->getBeginSymbol();
1690 void DwarfUnit::addStringOffsetsStart() {
1691 const TargetLoweringObjectFile
&TLOF
= Asm
->getObjFileLowering();
1692 addSectionLabel(getUnitDie(), dwarf::DW_AT_str_offsets_base
,
1693 DU
->getStringOffsetsStartSym(),
1694 TLOF
.getDwarfStrOffSection()->getBeginSymbol());
1697 void DwarfUnit::addRnglistsBase() {
1698 assert(DD
->getDwarfVersion() >= 5 &&
1699 "DW_AT_rnglists_base requires DWARF version 5 or later");
1700 const TargetLoweringObjectFile
&TLOF
= Asm
->getObjFileLowering();
1701 addSectionLabel(getUnitDie(), dwarf::DW_AT_rnglists_base
,
1702 DU
->getRnglistsTableBaseSym(),
1703 TLOF
.getDwarfRnglistsSection()->getBeginSymbol());
1706 void DwarfUnit::addLoclistsBase() {
1707 assert(DD
->getDwarfVersion() >= 5 &&
1708 "DW_AT_loclists_base requires DWARF version 5 or later");
1709 const TargetLoweringObjectFile
&TLOF
= Asm
->getObjFileLowering();
1710 addSectionLabel(getUnitDie(), dwarf::DW_AT_loclists_base
,
1711 DU
->getLoclistsTableBaseSym(),
1712 TLOF
.getDwarfLoclistsSection()->getBeginSymbol());
1715 void DwarfTypeUnit::finishNonUnitTypeDIE(DIE
& D
, const DICompositeType
*CTy
) {
1716 addFlag(D
, dwarf::DW_AT_declaration
);
1717 StringRef Name
= CTy
->getName();
1719 addString(D
, dwarf::DW_AT_name
, Name
);
1720 getCU().createTypeDIE(CTy
);