1 //===-- llvm/CodeGen/DwarfUnit.cpp - Dwarf Type and Compile Units ---------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains support for constructing a dwarf compile unit.
12 //===----------------------------------------------------------------------===//
14 #include "DwarfUnit.h"
15 #include "AddressPool.h"
16 #include "DwarfCompileUnit.h"
17 #include "DwarfDebug.h"
18 #include "DwarfExpression.h"
19 #include "llvm/ADT/APFloat.h"
20 #include "llvm/ADT/APInt.h"
21 #include "llvm/ADT/None.h"
22 #include "llvm/ADT/iterator_range.h"
23 #include "llvm/CodeGen/MachineFunction.h"
24 #include "llvm/CodeGen/MachineOperand.h"
25 #include "llvm/IR/Constants.h"
26 #include "llvm/IR/DataLayout.h"
27 #include "llvm/IR/GlobalValue.h"
28 #include "llvm/IR/Metadata.h"
29 #include "llvm/MC/MCAsmInfo.h"
30 #include "llvm/MC/MCDwarf.h"
31 #include "llvm/MC/MCSection.h"
32 #include "llvm/MC/MCStreamer.h"
33 #include "llvm/MC/MachineLocation.h"
34 #include "llvm/Support/Casting.h"
35 #include "llvm/Support/CommandLine.h"
36 #include "llvm/Target/TargetLoweringObjectFile.h"
37 #include "llvm/Target/TargetRegisterInfo.h"
38 #include "llvm/Target/TargetSubtargetInfo.h"
46 #define DEBUG_TYPE "dwarfdebug"
49 GenerateDwarfTypeUnits("generate-type-units", cl::Hidden,
50 cl::desc("Generate DWARF4 type units."),
53 DIEDwarfExpression::DIEDwarfExpression(const AsmPrinter &AP, DwarfUnit &DU,
55 : DwarfExpression(AP.getDwarfVersion()), AP(AP), DU(DU),
58 void DIEDwarfExpression::emitOp(uint8_t Op, const char* Comment) {
59 DU.addUInt(DIE, dwarf::DW_FORM_data1, Op);
62 void DIEDwarfExpression::emitSigned(int64_t Value) {
63 DU.addSInt(DIE, dwarf::DW_FORM_sdata, Value);
66 void DIEDwarfExpression::emitUnsigned(uint64_t Value) {
67 DU.addUInt(DIE, dwarf::DW_FORM_udata, Value);
70 bool DIEDwarfExpression::isFrameRegister(const TargetRegisterInfo &TRI,
71 unsigned MachineReg) {
72 return MachineReg == TRI.getFrameRegister(*AP.MF);
75 DwarfUnit::DwarfUnit(dwarf::Tag UnitTag, const DICompileUnit *Node,
76 AsmPrinter *A, DwarfDebug *DW, DwarfFile *DWU)
77 : DIEUnit(A->getDwarfVersion(), A->MAI->getCodePointerSize(), UnitTag),
78 CUNode(Node), Asm(A), DD(DW), DU(DWU), IndexTyDie(nullptr) {
81 DwarfTypeUnit::DwarfTypeUnit(DwarfCompileUnit &CU, AsmPrinter *A,
82 DwarfDebug *DW, DwarfFile *DWU,
83 MCDwarfDwoLineTable *SplitLineTable)
84 : DwarfUnit(dwarf::DW_TAG_type_unit, CU.getCUNode(), A, DW, DWU), CU(CU),
85 SplitLineTable(SplitLineTable) {
87 addSectionOffset(getUnitDie(), dwarf::DW_AT_stmt_list, 0);
90 DwarfUnit::~DwarfUnit() {
91 for (unsigned j = 0, M = DIEBlocks.size(); j < M; ++j)
92 DIEBlocks[j]->~DIEBlock();
93 for (unsigned j = 0, M = DIELocs.size(); j < M; ++j)
94 DIELocs[j]->~DIELoc();
97 int64_t DwarfUnit::getDefaultLowerBound() const {
98 switch (getLanguage()) {
102 // The languages below have valid values in all DWARF versions.
103 case dwarf::DW_LANG_C:
104 case dwarf::DW_LANG_C89:
105 case dwarf::DW_LANG_C_plus_plus:
108 case dwarf::DW_LANG_Fortran77:
109 case dwarf::DW_LANG_Fortran90:
112 // The languages below have valid values only if the DWARF version >= 3.
113 case dwarf::DW_LANG_C99:
114 case dwarf::DW_LANG_ObjC:
115 case dwarf::DW_LANG_ObjC_plus_plus:
116 if (DD->getDwarfVersion() >= 3)
120 case dwarf::DW_LANG_Fortran95:
121 if (DD->getDwarfVersion() >= 3)
125 // Starting with DWARF v4, all defined languages have valid values.
126 case dwarf::DW_LANG_D:
127 case dwarf::DW_LANG_Java:
128 case dwarf::DW_LANG_Python:
129 case dwarf::DW_LANG_UPC:
130 if (DD->getDwarfVersion() >= 4)
134 case dwarf::DW_LANG_Ada83:
135 case dwarf::DW_LANG_Ada95:
136 case dwarf::DW_LANG_Cobol74:
137 case dwarf::DW_LANG_Cobol85:
138 case dwarf::DW_LANG_Modula2:
139 case dwarf::DW_LANG_Pascal83:
140 case dwarf::DW_LANG_PLI:
141 if (DD->getDwarfVersion() >= 4)
145 // The languages below are new in DWARF v5.
146 case dwarf::DW_LANG_BLISS:
147 case dwarf::DW_LANG_C11:
148 case dwarf::DW_LANG_C_plus_plus_03:
149 case dwarf::DW_LANG_C_plus_plus_11:
150 case dwarf::DW_LANG_C_plus_plus_14:
151 case dwarf::DW_LANG_Dylan:
152 case dwarf::DW_LANG_Go:
153 case dwarf::DW_LANG_Haskell:
154 case dwarf::DW_LANG_OCaml:
155 case dwarf::DW_LANG_OpenCL:
156 case dwarf::DW_LANG_RenderScript:
157 case dwarf::DW_LANG_Rust:
158 case dwarf::DW_LANG_Swift:
159 if (DD->getDwarfVersion() >= 5)
163 case dwarf::DW_LANG_Fortran03:
164 case dwarf::DW_LANG_Fortran08:
165 case dwarf::DW_LANG_Julia:
166 case dwarf::DW_LANG_Modula3:
167 if (DD->getDwarfVersion() >= 5)
175 /// Check whether the DIE for this MDNode can be shared across CUs.
176 bool DwarfUnit::isShareableAcrossCUs(const DINode *D) const {
177 // When the MDNode can be part of the type system, the DIE can be shared
179 // Combining type units and cross-CU DIE sharing is lower value (since
180 // cross-CU DIE sharing is used in LTO and removes type redundancy at that
181 // level already) but may be implementable for some value in projects
182 // building multiple independent libraries with LTO and then linking those
184 if (isDwoUnit() && !DD->shareAcrossDWOCUs())
186 return (isa<DIType>(D) ||
187 (isa<DISubprogram>(D) && !cast<DISubprogram>(D)->isDefinition())) &&
188 !GenerateDwarfTypeUnits;
191 DIE *DwarfUnit::getDIE(const DINode *D) const {
192 if (isShareableAcrossCUs(D))
193 return DU->getDIE(D);
194 return MDNodeToDieMap.lookup(D);
197 void DwarfUnit::insertDIE(const DINode *Desc, DIE *D) {
198 if (isShareableAcrossCUs(Desc)) {
199 DU->insertDIE(Desc, D);
202 MDNodeToDieMap.insert(std::make_pair(Desc, D));
205 void DwarfUnit::addFlag(DIE &Die, dwarf::Attribute Attribute) {
206 if (DD->getDwarfVersion() >= 4)
207 Die.addValue(DIEValueAllocator, Attribute, dwarf::DW_FORM_flag_present,
210 Die.addValue(DIEValueAllocator, Attribute, dwarf::DW_FORM_flag,
214 void DwarfUnit::addUInt(DIEValueList &Die, dwarf::Attribute Attribute,
215 Optional<dwarf::Form> Form, uint64_t Integer) {
217 Form = DIEInteger::BestForm(false, Integer);
218 assert(Form != dwarf::DW_FORM_implicit_const &&
219 "DW_FORM_implicit_const is used only for signed integers");
220 Die.addValue(DIEValueAllocator, Attribute, *Form, DIEInteger(Integer));
223 void DwarfUnit::addUInt(DIEValueList &Block, dwarf::Form Form,
225 addUInt(Block, (dwarf::Attribute)0, Form, Integer);
228 void DwarfUnit::addSInt(DIEValueList &Die, dwarf::Attribute Attribute,
229 Optional<dwarf::Form> Form, int64_t Integer) {
231 Form = DIEInteger::BestForm(true, Integer);
232 Die.addValue(DIEValueAllocator, Attribute, *Form, DIEInteger(Integer));
235 void DwarfUnit::addSInt(DIELoc &Die, Optional<dwarf::Form> Form,
237 addSInt(Die, (dwarf::Attribute)0, Form, Integer);
240 void DwarfUnit::addString(DIE &Die, dwarf::Attribute Attribute,
242 Die.addValue(DIEValueAllocator, Attribute,
243 isDwoUnit() ? dwarf::DW_FORM_GNU_str_index : dwarf::DW_FORM_strp,
244 DIEString(DU->getStringPool().getEntry(*Asm, String)));
247 DIEValueList::value_iterator DwarfUnit::addLabel(DIEValueList &Die,
248 dwarf::Attribute Attribute,
250 const MCSymbol *Label) {
251 return Die.addValue(DIEValueAllocator, Attribute, Form, DIELabel(Label));
254 void DwarfUnit::addLabel(DIELoc &Die, dwarf::Form Form, const MCSymbol *Label) {
255 addLabel(Die, (dwarf::Attribute)0, Form, Label);
258 void DwarfUnit::addSectionOffset(DIE &Die, dwarf::Attribute Attribute,
260 if (DD->getDwarfVersion() >= 4)
261 addUInt(Die, Attribute, dwarf::DW_FORM_sec_offset, Integer);
263 addUInt(Die, Attribute, dwarf::DW_FORM_data4, Integer);
266 unsigned DwarfTypeUnit::getOrCreateSourceID(StringRef FileName, StringRef DirName) {
267 return SplitLineTable ? SplitLineTable->getFile(DirName, FileName)
268 : getCU().getOrCreateSourceID(FileName, DirName);
271 void DwarfUnit::addOpAddress(DIELoc &Die, const MCSymbol *Sym) {
272 if (!DD->useSplitDwarf()) {
273 addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
274 addLabel(Die, dwarf::DW_FORM_udata, Sym);
276 addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_addr_index);
277 addUInt(Die, dwarf::DW_FORM_GNU_addr_index,
278 DD->getAddressPool().getIndex(Sym));
282 void DwarfUnit::addLabelDelta(DIE &Die, dwarf::Attribute Attribute,
283 const MCSymbol *Hi, const MCSymbol *Lo) {
284 Die.addValue(DIEValueAllocator, Attribute, dwarf::DW_FORM_data4,
285 new (DIEValueAllocator) DIEDelta(Hi, Lo));
288 void DwarfUnit::addDIEEntry(DIE &Die, dwarf::Attribute Attribute, DIE &Entry) {
289 addDIEEntry(Die, Attribute, DIEEntry(Entry));
292 void DwarfUnit::addDIETypeSignature(DIE &Die, uint64_t Signature) {
293 // Flag the type unit reference as a declaration so that if it contains
294 // members (implicit special members, static data member definitions, member
295 // declarations for definitions in this CU, etc) consumers don't get confused
296 // and think this is a full definition.
297 addFlag(Die, dwarf::DW_AT_declaration);
299 Die.addValue(DIEValueAllocator, dwarf::DW_AT_signature,
300 dwarf::DW_FORM_ref_sig8, DIEInteger(Signature));
303 void DwarfUnit::addDIEEntry(DIE &Die, dwarf::Attribute Attribute,
305 const DIEUnit *CU = Die.getUnit();
306 const DIEUnit *EntryCU = Entry.getEntry().getUnit();
308 // We assume that Die belongs to this CU, if it is not linked to any CU yet.
309 CU = getUnitDie().getUnit();
311 EntryCU = getUnitDie().getUnit();
312 Die.addValue(DIEValueAllocator, Attribute,
313 EntryCU == CU ? dwarf::DW_FORM_ref4 : dwarf::DW_FORM_ref_addr,
317 DIE &DwarfUnit::createAndAddDIE(unsigned Tag, DIE &Parent, const DINode *N) {
318 DIE &Die = Parent.addChild(DIE::get(DIEValueAllocator, (dwarf::Tag)Tag));
324 void DwarfUnit::addBlock(DIE &Die, dwarf::Attribute Attribute, DIELoc *Loc) {
325 Loc->ComputeSize(Asm);
326 DIELocs.push_back(Loc); // Memoize so we can call the destructor later on.
327 Die.addValue(DIEValueAllocator, Attribute,
328 Loc->BestForm(DD->getDwarfVersion()), Loc);
331 void DwarfUnit::addBlock(DIE &Die, dwarf::Attribute Attribute,
333 Block->ComputeSize(Asm);
334 DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on.
335 Die.addValue(DIEValueAllocator, Attribute, Block->BestForm(), Block);
338 void DwarfUnit::addSourceLine(DIE &Die, unsigned Line, StringRef File,
339 StringRef Directory) {
343 unsigned FileID = getOrCreateSourceID(File, Directory);
344 assert(FileID && "Invalid file id");
345 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
346 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
349 void DwarfUnit::addSourceLine(DIE &Die, const DILocalVariable *V) {
352 addSourceLine(Die, V->getLine(), V->getScope()->getFilename(),
353 V->getScope()->getDirectory());
356 void DwarfUnit::addSourceLine(DIE &Die, const DIGlobalVariable *G) {
359 addSourceLine(Die, G->getLine(), G->getFilename(), G->getDirectory());
362 void DwarfUnit::addSourceLine(DIE &Die, const DISubprogram *SP) {
365 addSourceLine(Die, SP->getLine(), SP->getFilename(), SP->getDirectory());
368 void DwarfUnit::addSourceLine(DIE &Die, const DIType *Ty) {
371 addSourceLine(Die, Ty->getLine(), Ty->getFilename(), Ty->getDirectory());
374 void DwarfUnit::addSourceLine(DIE &Die, const DIObjCProperty *Ty) {
377 addSourceLine(Die, Ty->getLine(), Ty->getFilename(), Ty->getDirectory());
380 /* Byref variables, in Blocks, are declared by the programmer as "SomeType
381 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
382 gives the variable VarName either the struct, or a pointer to the struct, as
383 its type. This is necessary for various behind-the-scenes things the
384 compiler needs to do with by-reference variables in Blocks.
386 However, as far as the original *programmer* is concerned, the variable
387 should still have type 'SomeType', as originally declared.
389 The function getBlockByrefType dives into the __Block_byref_x_VarName
390 struct to find the original type of the variable, which is then assigned to
391 the variable's Debug Information Entry as its real type. So far, so good.
392 However now the debugger will expect the variable VarName to have the type
393 SomeType. So we need the location attribute for the variable to be an
394 expression that explains to the debugger how to navigate through the
395 pointers and struct to find the actual variable of type SomeType.
397 The following function does just that. We start by getting
398 the "normal" location for the variable. This will be the location
399 of either the struct __Block_byref_x_VarName or the pointer to the
400 struct __Block_byref_x_VarName.
402 The struct will look something like:
404 struct __Block_byref_x_VarName {
406 struct __Block_byref_x_VarName *forwarding;
407 ... <various other fields>
409 ... <maybe more fields>
412 If we are given the struct directly (as our starting point) we
413 need to tell the debugger to:
415 1). Add the offset of the forwarding field.
417 2). Follow that pointer to get the real __Block_byref_x_VarName
418 struct to use (the real one may have been copied onto the heap).
420 3). Add the offset for the field VarName, to find the actual variable.
422 If we started with a pointer to the struct, then we need to
423 dereference that pointer first, before the other steps.
424 Translating this into DWARF ops, we will need to append the following
425 to the current location description for the variable:
427 DW_OP_deref -- optional, if we start with a pointer
428 DW_OP_plus_uconst <forward_fld_offset>
430 DW_OP_plus_uconst <varName_fld_offset>
432 That is what this function does. */
434 void DwarfUnit::addBlockByrefAddress(const DbgVariable &DV, DIE &Die,
435 dwarf::Attribute Attribute,
436 const MachineLocation &Location) {
437 const DIType *Ty = DV.getType();
438 const DIType *TmpTy = Ty;
439 uint16_t Tag = Ty->getTag();
440 bool isPointer = false;
442 StringRef varName = DV.getName();
444 if (Tag == dwarf::DW_TAG_pointer_type) {
445 auto *DTy = cast<DIDerivedType>(Ty);
446 TmpTy = resolve(DTy->getBaseType());
450 // Find the __forwarding field and the variable field in the __Block_byref
452 DINodeArray Fields = cast<DICompositeType>(TmpTy)->getElements();
453 const DIDerivedType *varField = nullptr;
454 const DIDerivedType *forwardingField = nullptr;
456 for (unsigned i = 0, N = Fields.size(); i < N; ++i) {
457 auto *DT = cast<DIDerivedType>(Fields[i]);
458 StringRef fieldName = DT->getName();
459 if (fieldName == "__forwarding")
460 forwardingField = DT;
461 else if (fieldName == varName)
465 // Get the offsets for the forwarding field and the variable field.
466 unsigned forwardingFieldOffset = forwardingField->getOffsetInBits() >> 3;
467 unsigned varFieldOffset = varField->getOffsetInBits() >> 2;
469 // Decode the original location, and use that as the start of the byref
470 // variable's location.
471 DIELoc *Loc = new (DIEValueAllocator) DIELoc;
472 DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc);
473 if (Location.isIndirect())
474 DwarfExpr.setMemoryLocationKind();
476 SmallVector<uint64_t, 9> Ops;
477 if (Location.isIndirect() && Location.getOffset()) {
478 Ops.push_back(dwarf::DW_OP_plus_uconst);
479 Ops.push_back(Location.getOffset());
481 // If we started with a pointer to the __Block_byref... struct, then
482 // the first thing we need to do is dereference the pointer (DW_OP_deref).
484 Ops.push_back(dwarf::DW_OP_deref);
486 // Next add the offset for the '__forwarding' field:
487 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
488 // adding the offset if it's 0.
489 if (forwardingFieldOffset > 0) {
490 Ops.push_back(dwarf::DW_OP_plus_uconst);
491 Ops.push_back(forwardingFieldOffset);
494 // Now dereference the __forwarding field to get to the real __Block_byref
495 // struct: DW_OP_deref.
496 Ops.push_back(dwarf::DW_OP_deref);
498 // Now that we've got the real __Block_byref... struct, add the offset
499 // for the variable's field to get to the location of the actual variable:
500 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
501 if (varFieldOffset > 0) {
502 Ops.push_back(dwarf::DW_OP_plus_uconst);
503 Ops.push_back(varFieldOffset);
506 DIExpressionCursor Cursor(Ops);
507 const TargetRegisterInfo &TRI = *Asm->MF->getSubtarget().getRegisterInfo();
508 if (!DwarfExpr.addMachineRegExpression(TRI, Cursor, Location.getReg()))
510 DwarfExpr.addExpression(std::move(Cursor));
512 // Now attach the location information to the DIE.
513 addBlock(Die, Attribute, DwarfExpr.finalize());
516 /// Return true if type encoding is unsigned.
517 static bool isUnsignedDIType(DwarfDebug *DD, const DIType *Ty) {
518 if (auto *CTy = dyn_cast<DICompositeType>(Ty)) {
519 // FIXME: Enums without a fixed underlying type have unknown signedness
520 // here, leading to incorrectly emitted constants.
521 if (CTy->getTag() == dwarf::DW_TAG_enumeration_type)
524 // (Pieces of) aggregate types that get hacked apart by SROA may be
525 // represented by a constant. Encode them as unsigned bytes.
529 if (auto *DTy = dyn_cast<DIDerivedType>(Ty)) {
530 dwarf::Tag T = (dwarf::Tag)Ty->getTag();
531 // Encode pointer constants as unsigned bytes. This is used at least for
532 // null pointer constant emission.
533 // FIXME: reference and rvalue_reference /probably/ shouldn't be allowed
534 // here, but accept them for now due to a bug in SROA producing bogus
536 if (T == dwarf::DW_TAG_pointer_type ||
537 T == dwarf::DW_TAG_ptr_to_member_type ||
538 T == dwarf::DW_TAG_reference_type ||
539 T == dwarf::DW_TAG_rvalue_reference_type)
541 assert(T == dwarf::DW_TAG_typedef || T == dwarf::DW_TAG_const_type ||
542 T == dwarf::DW_TAG_volatile_type ||
543 T == dwarf::DW_TAG_restrict_type || T == dwarf::DW_TAG_atomic_type);
544 DITypeRef Deriv = DTy->getBaseType();
545 assert(Deriv && "Expected valid base type");
546 return isUnsignedDIType(DD, DD->resolve(Deriv));
549 auto *BTy = cast<DIBasicType>(Ty);
550 unsigned Encoding = BTy->getEncoding();
551 assert((Encoding == dwarf::DW_ATE_unsigned ||
552 Encoding == dwarf::DW_ATE_unsigned_char ||
553 Encoding == dwarf::DW_ATE_signed ||
554 Encoding == dwarf::DW_ATE_signed_char ||
555 Encoding == dwarf::DW_ATE_float || Encoding == dwarf::DW_ATE_UTF ||
556 Encoding == dwarf::DW_ATE_boolean ||
557 (Ty->getTag() == dwarf::DW_TAG_unspecified_type &&
558 Ty->getName() == "decltype(nullptr)")) &&
559 "Unsupported encoding");
560 return Encoding == dwarf::DW_ATE_unsigned ||
561 Encoding == dwarf::DW_ATE_unsigned_char ||
562 Encoding == dwarf::DW_ATE_UTF || Encoding == dwarf::DW_ATE_boolean ||
563 Ty->getTag() == dwarf::DW_TAG_unspecified_type;
566 void DwarfUnit::addConstantFPValue(DIE &Die, const MachineOperand &MO) {
567 assert(MO.isFPImm() && "Invalid machine operand!");
568 DIEBlock *Block = new (DIEValueAllocator) DIEBlock;
569 APFloat FPImm = MO.getFPImm()->getValueAPF();
571 // Get the raw data form of the floating point.
572 const APInt FltVal = FPImm.bitcastToAPInt();
573 const char *FltPtr = (const char *)FltVal.getRawData();
575 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte.
576 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
577 int Incr = (LittleEndian ? 1 : -1);
578 int Start = (LittleEndian ? 0 : NumBytes - 1);
579 int Stop = (LittleEndian ? NumBytes : -1);
581 // Output the constant to DWARF one byte at a time.
582 for (; Start != Stop; Start += Incr)
583 addUInt(*Block, dwarf::DW_FORM_data1, (unsigned char)0xFF & FltPtr[Start]);
585 addBlock(Die, dwarf::DW_AT_const_value, Block);
588 void DwarfUnit::addConstantFPValue(DIE &Die, const ConstantFP *CFP) {
589 // Pass this down to addConstantValue as an unsigned bag of bits.
590 addConstantValue(Die, CFP->getValueAPF().bitcastToAPInt(), true);
593 void DwarfUnit::addConstantValue(DIE &Die, const ConstantInt *CI,
595 addConstantValue(Die, CI->getValue(), Ty);
598 void DwarfUnit::addConstantValue(DIE &Die, const MachineOperand &MO,
600 assert(MO.isImm() && "Invalid machine operand!");
602 addConstantValue(Die, isUnsignedDIType(DD, Ty), MO.getImm());
605 void DwarfUnit::addConstantValue(DIE &Die, bool Unsigned, uint64_t Val) {
606 // FIXME: This is a bit conservative/simple - it emits negative values always
607 // sign extended to 64 bits rather than minimizing the number of bytes.
608 addUInt(Die, dwarf::DW_AT_const_value,
609 Unsigned ? dwarf::DW_FORM_udata : dwarf::DW_FORM_sdata, Val);
612 void DwarfUnit::addConstantValue(DIE &Die, const APInt &Val, const DIType *Ty) {
613 addConstantValue(Die, Val, isUnsignedDIType(DD, Ty));
616 void DwarfUnit::addConstantValue(DIE &Die, const APInt &Val, bool Unsigned) {
617 unsigned CIBitWidth = Val.getBitWidth();
618 if (CIBitWidth <= 64) {
619 addConstantValue(Die, Unsigned,
620 Unsigned ? Val.getZExtValue() : Val.getSExtValue());
624 DIEBlock *Block = new (DIEValueAllocator) DIEBlock;
626 // Get the raw data form of the large APInt.
627 const uint64_t *Ptr64 = Val.getRawData();
629 int NumBytes = Val.getBitWidth() / 8; // 8 bits per byte.
630 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
632 // Output the constant to DWARF one byte at a time.
633 for (int i = 0; i < NumBytes; i++) {
636 c = Ptr64[i / 8] >> (8 * (i & 7));
638 c = Ptr64[(NumBytes - 1 - i) / 8] >> (8 * ((NumBytes - 1 - i) & 7));
639 addUInt(*Block, dwarf::DW_FORM_data1, c);
642 addBlock(Die, dwarf::DW_AT_const_value, Block);
645 void DwarfUnit::addLinkageName(DIE &Die, StringRef LinkageName) {
646 if (!LinkageName.empty())
648 DD->getDwarfVersion() >= 4 ? dwarf::DW_AT_linkage_name
649 : dwarf::DW_AT_MIPS_linkage_name,
650 GlobalValue::dropLLVMManglingEscape(LinkageName));
653 void DwarfUnit::addTemplateParams(DIE &Buffer, DINodeArray TParams) {
654 // Add template parameters.
655 for (const auto *Element : TParams) {
656 if (auto *TTP = dyn_cast<DITemplateTypeParameter>(Element))
657 constructTemplateTypeParameterDIE(Buffer, TTP);
658 else if (auto *TVP = dyn_cast<DITemplateValueParameter>(Element))
659 constructTemplateValueParameterDIE(Buffer, TVP);
663 /// Add thrown types.
664 void DwarfUnit::addThrownTypes(DIE &Die, DINodeArray ThrownTypes) {
665 for (const auto *Ty : ThrownTypes) {
666 DIE &TT = createAndAddDIE(dwarf::DW_TAG_thrown_type, Die);
667 addType(TT, cast<DIType>(Ty));
671 DIE *DwarfUnit::getOrCreateContextDIE(const DIScope *Context) {
672 if (!Context || isa<DIFile>(Context))
673 return &getUnitDie();
674 if (auto *T = dyn_cast<DIType>(Context))
675 return getOrCreateTypeDIE(T);
676 if (auto *NS = dyn_cast<DINamespace>(Context))
677 return getOrCreateNameSpace(NS);
678 if (auto *SP = dyn_cast<DISubprogram>(Context))
679 return getOrCreateSubprogramDIE(SP);
680 if (auto *M = dyn_cast<DIModule>(Context))
681 return getOrCreateModule(M);
682 return getDIE(Context);
685 DIE *DwarfTypeUnit::createTypeDIE(const DICompositeType *Ty) {
686 auto *Context = resolve(Ty->getScope());
687 DIE *ContextDIE = getOrCreateContextDIE(Context);
689 if (DIE *TyDIE = getDIE(Ty))
693 DIE &TyDIE = createAndAddDIE(Ty->getTag(), *ContextDIE, Ty);
695 constructTypeDIE(TyDIE, cast<DICompositeType>(Ty));
697 updateAcceleratorTables(Context, Ty, TyDIE);
701 DIE *DwarfUnit::getOrCreateTypeDIE(const MDNode *TyNode) {
705 auto *Ty = cast<DIType>(TyNode);
707 // DW_TAG_restrict_type is not supported in DWARF2
708 if (Ty->getTag() == dwarf::DW_TAG_restrict_type && DD->getDwarfVersion() <= 2)
709 return getOrCreateTypeDIE(resolve(cast<DIDerivedType>(Ty)->getBaseType()));
711 // DW_TAG_atomic_type is not supported in DWARF < 5
712 if (Ty->getTag() == dwarf::DW_TAG_atomic_type && DD->getDwarfVersion() < 5)
713 return getOrCreateTypeDIE(resolve(cast<DIDerivedType>(Ty)->getBaseType()));
715 // Construct the context before querying for the existence of the DIE in case
716 // such construction creates the DIE.
717 auto *Context = resolve(Ty->getScope());
718 DIE *ContextDIE = getOrCreateContextDIE(Context);
721 if (DIE *TyDIE = getDIE(Ty))
725 DIE &TyDIE = createAndAddDIE(Ty->getTag(), *ContextDIE, Ty);
727 updateAcceleratorTables(Context, Ty, TyDIE);
729 if (auto *BT = dyn_cast<DIBasicType>(Ty))
730 constructTypeDIE(TyDIE, BT);
731 else if (auto *STy = dyn_cast<DISubroutineType>(Ty))
732 constructTypeDIE(TyDIE, STy);
733 else if (auto *CTy = dyn_cast<DICompositeType>(Ty)) {
734 if (GenerateDwarfTypeUnits && !Ty->isForwardDecl())
735 if (MDString *TypeId = CTy->getRawIdentifier()) {
736 DD->addDwarfTypeUnitType(getCU(), TypeId->getString(), TyDIE, CTy);
737 // Skip updating the accelerator tables since this is not the full type.
740 constructTypeDIE(TyDIE, CTy);
742 constructTypeDIE(TyDIE, cast<DIDerivedType>(Ty));
748 void DwarfUnit::updateAcceleratorTables(const DIScope *Context,
749 const DIType *Ty, const DIE &TyDIE) {
750 if (!Ty->getName().empty() && !Ty->isForwardDecl()) {
751 bool IsImplementation = false;
752 if (auto *CT = dyn_cast<DICompositeType>(Ty)) {
753 // A runtime language of 0 actually means C/C++ and that any
754 // non-negative value is some version of Objective-C/C++.
755 IsImplementation = CT->getRuntimeLang() == 0 || CT->isObjcClassComplete();
757 unsigned Flags = IsImplementation ? dwarf::DW_FLAG_type_implementation : 0;
758 DD->addAccelType(Ty->getName(), TyDIE, Flags);
760 if (!Context || isa<DICompileUnit>(Context) || isa<DIFile>(Context) ||
761 isa<DINamespace>(Context))
762 addGlobalType(Ty, TyDIE, Context);
766 void DwarfUnit::addType(DIE &Entity, const DIType *Ty,
767 dwarf::Attribute Attribute) {
768 assert(Ty && "Trying to add a type that doesn't exist?");
769 addDIEEntry(Entity, Attribute, DIEEntry(*getOrCreateTypeDIE(Ty)));
772 std::string DwarfUnit::getParentContextString(const DIScope *Context) const {
776 // FIXME: Decide whether to implement this for non-C++ languages.
777 if (getLanguage() != dwarf::DW_LANG_C_plus_plus)
781 SmallVector<const DIScope *, 1> Parents;
782 while (!isa<DICompileUnit>(Context)) {
783 Parents.push_back(Context);
784 if (Context->getScope())
785 Context = resolve(Context->getScope());
787 // Structure, etc types will have a NULL context if they're at the top
792 // Reverse iterate over our list to go from the outermost construct to the
794 for (const DIScope *Ctx : make_range(Parents.rbegin(), Parents.rend())) {
795 StringRef Name = Ctx->getName();
796 if (Name.empty() && isa<DINamespace>(Ctx))
797 Name = "(anonymous namespace)";
806 void DwarfUnit::constructTypeDIE(DIE &Buffer, const DIBasicType *BTy) {
807 // Get core information.
808 StringRef Name = BTy->getName();
809 // Add name if not anonymous or intermediate type.
811 addString(Buffer, dwarf::DW_AT_name, Name);
813 // An unspecified type only has a name attribute.
814 if (BTy->getTag() == dwarf::DW_TAG_unspecified_type)
817 addUInt(Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
820 uint64_t Size = BTy->getSizeInBits() >> 3;
821 addUInt(Buffer, dwarf::DW_AT_byte_size, None, Size);
824 void DwarfUnit::constructTypeDIE(DIE &Buffer, const DIDerivedType *DTy) {
825 // Get core information.
826 StringRef Name = DTy->getName();
827 uint64_t Size = DTy->getSizeInBits() >> 3;
828 uint16_t Tag = Buffer.getTag();
830 // Map to main type, void will not have a type.
831 const DIType *FromTy = resolve(DTy->getBaseType());
833 addType(Buffer, FromTy);
835 // Add name if not anonymous or intermediate type.
837 addString(Buffer, dwarf::DW_AT_name, Name);
839 // Add size if non-zero (derived types might be zero-sized.)
840 if (Size && Tag != dwarf::DW_TAG_pointer_type
841 && Tag != dwarf::DW_TAG_ptr_to_member_type
842 && Tag != dwarf::DW_TAG_reference_type
843 && Tag != dwarf::DW_TAG_rvalue_reference_type)
844 addUInt(Buffer, dwarf::DW_AT_byte_size, None, Size);
846 if (Tag == dwarf::DW_TAG_ptr_to_member_type)
848 Buffer, dwarf::DW_AT_containing_type,
849 *getOrCreateTypeDIE(resolve(cast<DIDerivedType>(DTy)->getClassType())));
850 // Add source line info if available and TyDesc is not a forward declaration.
851 if (!DTy->isForwardDecl())
852 addSourceLine(Buffer, DTy);
854 // If DWARF address space value is other than None, add it for pointer and
855 // reference types as DW_AT_address_class.
856 if (DTy->getDWARFAddressSpace() && (Tag == dwarf::DW_TAG_pointer_type ||
857 Tag == dwarf::DW_TAG_reference_type))
858 addUInt(Buffer, dwarf::DW_AT_address_class, dwarf::DW_FORM_data4,
859 DTy->getDWARFAddressSpace().getValue());
862 void DwarfUnit::constructSubprogramArguments(DIE &Buffer, DITypeRefArray Args) {
863 for (unsigned i = 1, N = Args.size(); i < N; ++i) {
864 const DIType *Ty = resolve(Args[i]);
866 assert(i == N-1 && "Unspecified parameter must be the last argument");
867 createAndAddDIE(dwarf::DW_TAG_unspecified_parameters, Buffer);
869 DIE &Arg = createAndAddDIE(dwarf::DW_TAG_formal_parameter, Buffer);
871 if (Ty->isArtificial())
872 addFlag(Arg, dwarf::DW_AT_artificial);
877 void DwarfUnit::constructTypeDIE(DIE &Buffer, const DISubroutineType *CTy) {
878 // Add return type. A void return won't have a type.
879 auto Elements = cast<DISubroutineType>(CTy)->getTypeArray();
881 if (auto RTy = resolve(Elements[0]))
882 addType(Buffer, RTy);
884 bool isPrototyped = true;
885 if (Elements.size() == 2 && !Elements[1])
886 isPrototyped = false;
888 constructSubprogramArguments(Buffer, Elements);
890 // Add prototype flag if we're dealing with a C language and the function has
892 uint16_t Language = getLanguage();
894 (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 ||
895 Language == dwarf::DW_LANG_ObjC))
896 addFlag(Buffer, dwarf::DW_AT_prototyped);
898 // Add a DW_AT_calling_convention if this has an explicit convention.
899 if (CTy->getCC() && CTy->getCC() != dwarf::DW_CC_normal)
900 addUInt(Buffer, dwarf::DW_AT_calling_convention, dwarf::DW_FORM_data1,
903 if (CTy->isLValueReference())
904 addFlag(Buffer, dwarf::DW_AT_reference);
906 if (CTy->isRValueReference())
907 addFlag(Buffer, dwarf::DW_AT_rvalue_reference);
910 void DwarfUnit::constructTypeDIE(DIE &Buffer, const DICompositeType *CTy) {
911 // Add name if not anonymous or intermediate type.
912 StringRef Name = CTy->getName();
914 uint64_t Size = CTy->getSizeInBits() >> 3;
915 uint16_t Tag = Buffer.getTag();
918 case dwarf::DW_TAG_array_type:
919 constructArrayTypeDIE(Buffer, CTy);
921 case dwarf::DW_TAG_enumeration_type:
922 constructEnumTypeDIE(Buffer, CTy);
924 case dwarf::DW_TAG_structure_type:
925 case dwarf::DW_TAG_union_type:
926 case dwarf::DW_TAG_class_type: {
927 // Add elements to structure type.
928 DINodeArray Elements = CTy->getElements();
929 for (const auto *Element : Elements) {
932 if (auto *SP = dyn_cast<DISubprogram>(Element))
933 getOrCreateSubprogramDIE(SP);
934 else if (auto *DDTy = dyn_cast<DIDerivedType>(Element)) {
935 if (DDTy->getTag() == dwarf::DW_TAG_friend) {
936 DIE &ElemDie = createAndAddDIE(dwarf::DW_TAG_friend, Buffer);
937 addType(ElemDie, resolve(DDTy->getBaseType()), dwarf::DW_AT_friend);
938 } else if (DDTy->isStaticMember()) {
939 getOrCreateStaticMemberDIE(DDTy);
941 constructMemberDIE(Buffer, DDTy);
943 } else if (auto *Property = dyn_cast<DIObjCProperty>(Element)) {
944 DIE &ElemDie = createAndAddDIE(Property->getTag(), Buffer);
945 StringRef PropertyName = Property->getName();
946 addString(ElemDie, dwarf::DW_AT_APPLE_property_name, PropertyName);
947 if (Property->getType())
948 addType(ElemDie, resolve(Property->getType()));
949 addSourceLine(ElemDie, Property);
950 StringRef GetterName = Property->getGetterName();
951 if (!GetterName.empty())
952 addString(ElemDie, dwarf::DW_AT_APPLE_property_getter, GetterName);
953 StringRef SetterName = Property->getSetterName();
954 if (!SetterName.empty())
955 addString(ElemDie, dwarf::DW_AT_APPLE_property_setter, SetterName);
956 if (unsigned PropertyAttributes = Property->getAttributes())
957 addUInt(ElemDie, dwarf::DW_AT_APPLE_property_attribute, None,
962 if (CTy->isAppleBlockExtension())
963 addFlag(Buffer, dwarf::DW_AT_APPLE_block);
965 // This is outside the DWARF spec, but GDB expects a DW_AT_containing_type
966 // inside C++ composite types to point to the base class with the vtable.
967 if (auto *ContainingType =
968 dyn_cast_or_null<DICompositeType>(resolve(CTy->getVTableHolder())))
969 addDIEEntry(Buffer, dwarf::DW_AT_containing_type,
970 *getOrCreateTypeDIE(ContainingType));
972 if (CTy->isObjcClassComplete())
973 addFlag(Buffer, dwarf::DW_AT_APPLE_objc_complete_type);
975 // Add template parameters to a class, structure or union types.
976 // FIXME: The support isn't in the metadata for this yet.
977 if (Tag == dwarf::DW_TAG_class_type ||
978 Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type)
979 addTemplateParams(Buffer, CTy->getTemplateParams());
987 // Add name if not anonymous or intermediate type.
989 addString(Buffer, dwarf::DW_AT_name, Name);
991 if (Tag == dwarf::DW_TAG_enumeration_type ||
992 Tag == dwarf::DW_TAG_class_type || Tag == dwarf::DW_TAG_structure_type ||
993 Tag == dwarf::DW_TAG_union_type) {
994 // Add size if non-zero (derived types might be zero-sized.)
995 // TODO: Do we care about size for enum forward declarations?
997 addUInt(Buffer, dwarf::DW_AT_byte_size, None, Size);
998 else if (!CTy->isForwardDecl())
999 // Add zero size if it is not a forward declaration.
1000 addUInt(Buffer, dwarf::DW_AT_byte_size, None, 0);
1002 // If we're a forward decl, say so.
1003 if (CTy->isForwardDecl())
1004 addFlag(Buffer, dwarf::DW_AT_declaration);
1006 // Add source line info if available.
1007 if (!CTy->isForwardDecl())
1008 addSourceLine(Buffer, CTy);
1010 // No harm in adding the runtime language to the declaration.
1011 unsigned RLang = CTy->getRuntimeLang();
1013 addUInt(Buffer, dwarf::DW_AT_APPLE_runtime_class, dwarf::DW_FORM_data1,
1016 // Add align info if available.
1017 if (uint32_t AlignInBytes = CTy->getAlignInBytes())
1018 addUInt(Buffer, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata,
1023 void DwarfUnit::constructTemplateTypeParameterDIE(
1024 DIE &Buffer, const DITemplateTypeParameter *TP) {
1026 createAndAddDIE(dwarf::DW_TAG_template_type_parameter, Buffer);
1027 // Add the type if it exists, it could be void and therefore no type.
1029 addType(ParamDIE, resolve(TP->getType()));
1030 if (!TP->getName().empty())
1031 addString(ParamDIE, dwarf::DW_AT_name, TP->getName());
1034 void DwarfUnit::constructTemplateValueParameterDIE(
1035 DIE &Buffer, const DITemplateValueParameter *VP) {
1036 DIE &ParamDIE = createAndAddDIE(VP->getTag(), Buffer);
1038 // Add the type if there is one, template template and template parameter
1039 // packs will not have a type.
1040 if (VP->getTag() == dwarf::DW_TAG_template_value_parameter)
1041 addType(ParamDIE, resolve(VP->getType()));
1042 if (!VP->getName().empty())
1043 addString(ParamDIE, dwarf::DW_AT_name, VP->getName());
1044 if (Metadata *Val = VP->getValue()) {
1045 if (ConstantInt *CI = mdconst::dyn_extract<ConstantInt>(Val))
1046 addConstantValue(ParamDIE, CI, resolve(VP->getType()));
1047 else if (GlobalValue *GV = mdconst::dyn_extract<GlobalValue>(Val)) {
1048 // We cannot describe the location of dllimport'd entities: the
1049 // computation of their address requires loads from the IAT.
1050 if (!GV->hasDLLImportStorageClass()) {
1051 // For declaration non-type template parameters (such as global values
1053 DIELoc *Loc = new (DIEValueAllocator) DIELoc;
1054 addOpAddress(*Loc, Asm->getSymbol(GV));
1055 // Emit DW_OP_stack_value to use the address as the immediate value of
1056 // the parameter, rather than a pointer to it.
1057 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_stack_value);
1058 addBlock(ParamDIE, dwarf::DW_AT_location, Loc);
1060 } else if (VP->getTag() == dwarf::DW_TAG_GNU_template_template_param) {
1061 assert(isa<MDString>(Val));
1062 addString(ParamDIE, dwarf::DW_AT_GNU_template_name,
1063 cast<MDString>(Val)->getString());
1064 } else if (VP->getTag() == dwarf::DW_TAG_GNU_template_parameter_pack) {
1065 addTemplateParams(ParamDIE, cast<MDTuple>(Val));
1070 DIE *DwarfUnit::getOrCreateNameSpace(const DINamespace *NS) {
1071 // Construct the context before querying for the existence of the DIE in case
1072 // such construction creates the DIE.
1073 DIE *ContextDIE = getOrCreateContextDIE(NS->getScope());
1075 if (DIE *NDie = getDIE(NS))
1077 DIE &NDie = createAndAddDIE(dwarf::DW_TAG_namespace, *ContextDIE, NS);
1079 StringRef Name = NS->getName();
1081 addString(NDie, dwarf::DW_AT_name, NS->getName());
1083 Name = "(anonymous namespace)";
1084 DD->addAccelNamespace(Name, NDie);
1085 addGlobalName(Name, NDie, NS->getScope());
1086 if (NS->getExportSymbols())
1087 addFlag(NDie, dwarf::DW_AT_export_symbols);
1091 DIE *DwarfUnit::getOrCreateModule(const DIModule *M) {
1092 // Construct the context before querying for the existence of the DIE in case
1093 // such construction creates the DIE.
1094 DIE *ContextDIE = getOrCreateContextDIE(M->getScope());
1096 if (DIE *MDie = getDIE(M))
1098 DIE &MDie = createAndAddDIE(dwarf::DW_TAG_module, *ContextDIE, M);
1100 if (!M->getName().empty()) {
1101 addString(MDie, dwarf::DW_AT_name, M->getName());
1102 addGlobalName(M->getName(), MDie, M->getScope());
1104 if (!M->getConfigurationMacros().empty())
1105 addString(MDie, dwarf::DW_AT_LLVM_config_macros,
1106 M->getConfigurationMacros());
1107 if (!M->getIncludePath().empty())
1108 addString(MDie, dwarf::DW_AT_LLVM_include_path, M->getIncludePath());
1109 if (!M->getISysRoot().empty())
1110 addString(MDie, dwarf::DW_AT_LLVM_isysroot, M->getISysRoot());
1115 DIE *DwarfUnit::getOrCreateSubprogramDIE(const DISubprogram *SP, bool Minimal) {
1116 // Construct the context before querying for the existence of the DIE in case
1117 // such construction creates the DIE (as is the case for member function
1120 Minimal ? &getUnitDie() : getOrCreateContextDIE(resolve(SP->getScope()));
1122 if (DIE *SPDie = getDIE(SP))
1125 if (auto *SPDecl = SP->getDeclaration()) {
1127 // Add subprogram definitions to the CU die directly.
1128 ContextDIE = &getUnitDie();
1129 // Build the decl now to ensure it precedes the definition.
1130 getOrCreateSubprogramDIE(SPDecl);
1134 // DW_TAG_inlined_subroutine may refer to this DIE.
1135 DIE &SPDie = createAndAddDIE(dwarf::DW_TAG_subprogram, *ContextDIE, SP);
1137 // Stop here and fill this in later, depending on whether or not this
1138 // subprogram turns out to have inlined instances or not.
1139 if (SP->isDefinition())
1142 applySubprogramAttributes(SP, SPDie);
1146 bool DwarfUnit::applySubprogramDefinitionAttributes(const DISubprogram *SP,
1148 DIE *DeclDie = nullptr;
1149 StringRef DeclLinkageName;
1150 if (auto *SPDecl = SP->getDeclaration()) {
1151 DeclDie = getDIE(SPDecl);
1152 assert(DeclDie && "This DIE should've already been constructed when the "
1153 "definition DIE was created in "
1154 "getOrCreateSubprogramDIE");
1155 // Look at the Decl's linkage name only if we emitted it.
1156 if (DD->useAllLinkageNames())
1157 DeclLinkageName = SPDecl->getLinkageName();
1159 getOrCreateSourceID(SPDecl->getFilename(), SPDecl->getDirectory());
1160 unsigned DefID = getOrCreateSourceID(SP->getFilename(), SP->getDirectory());
1161 if (DeclID != DefID)
1162 addUInt(SPDie, dwarf::DW_AT_decl_file, None, DefID);
1164 if (SP->getLine() != SPDecl->getLine())
1165 addUInt(SPDie, dwarf::DW_AT_decl_line, None, SP->getLine());
1168 // Add function template parameters.
1169 addTemplateParams(SPDie, SP->getTemplateParams());
1171 // Add the linkage name if we have one and it isn't in the Decl.
1172 StringRef LinkageName = SP->getLinkageName();
1173 assert(((LinkageName.empty() || DeclLinkageName.empty()) ||
1174 LinkageName == DeclLinkageName) &&
1175 "decl has a linkage name and it is different");
1176 if (DeclLinkageName.empty() &&
1177 // Always emit it for abstract subprograms.
1178 (DD->useAllLinkageNames() || DU->getAbstractSPDies().lookup(SP)))
1179 addLinkageName(SPDie, LinkageName);
1184 // Refer to the function declaration where all the other attributes will be
1186 addDIEEntry(SPDie, dwarf::DW_AT_specification, *DeclDie);
1190 void DwarfUnit::applySubprogramAttributes(const DISubprogram *SP, DIE &SPDie,
1191 bool SkipSPAttributes) {
1192 // If -fdebug-info-for-profiling is enabled, need to emit the subprogram
1193 // and its source location.
1194 bool SkipSPSourceLocation = SkipSPAttributes &&
1195 !CUNode->getDebugInfoForProfiling();
1196 if (!SkipSPSourceLocation)
1197 if (applySubprogramDefinitionAttributes(SP, SPDie))
1200 // Constructors and operators for anonymous aggregates do not have names.
1201 if (!SP->getName().empty())
1202 addString(SPDie, dwarf::DW_AT_name, SP->getName());
1204 if (!SkipSPSourceLocation)
1205 addSourceLine(SPDie, SP);
1207 // Skip the rest of the attributes under -gmlt to save space.
1208 if (SkipSPAttributes)
1211 // Add the prototype if we have a prototype and we have a C like
1213 uint16_t Language = getLanguage();
1214 if (SP->isPrototyped() &&
1215 (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 ||
1216 Language == dwarf::DW_LANG_ObjC))
1217 addFlag(SPDie, dwarf::DW_AT_prototyped);
1220 DITypeRefArray Args;
1221 if (const DISubroutineType *SPTy = SP->getType()) {
1222 Args = SPTy->getTypeArray();
1226 // Add a DW_AT_calling_convention if this has an explicit convention.
1227 if (CC && CC != dwarf::DW_CC_normal)
1228 addUInt(SPDie, dwarf::DW_AT_calling_convention, dwarf::DW_FORM_data1, CC);
1230 // Add a return type. If this is a type like a C/C++ void type we don't add a
1233 if (auto Ty = resolve(Args[0]))
1236 unsigned VK = SP->getVirtuality();
1238 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, VK);
1239 if (SP->getVirtualIndex() != -1u) {
1240 DIELoc *Block = getDIELoc();
1241 addUInt(*Block, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1242 addUInt(*Block, dwarf::DW_FORM_udata, SP->getVirtualIndex());
1243 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, Block);
1245 ContainingTypeMap.insert(
1246 std::make_pair(&SPDie, resolve(SP->getContainingType())));
1249 if (!SP->isDefinition()) {
1250 addFlag(SPDie, dwarf::DW_AT_declaration);
1252 // Add arguments. Do not add arguments for subprogram definition. They will
1253 // be handled while processing variables.
1254 constructSubprogramArguments(SPDie, Args);
1257 addThrownTypes(SPDie, SP->getThrownTypes());
1259 if (SP->isArtificial())
1260 addFlag(SPDie, dwarf::DW_AT_artificial);
1262 if (!SP->isLocalToUnit())
1263 addFlag(SPDie, dwarf::DW_AT_external);
1265 if (DD->useAppleExtensionAttributes()) {
1266 if (SP->isOptimized())
1267 addFlag(SPDie, dwarf::DW_AT_APPLE_optimized);
1269 if (unsigned isa = Asm->getISAEncoding())
1270 addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa);
1273 if (SP->isLValueReference())
1274 addFlag(SPDie, dwarf::DW_AT_reference);
1276 if (SP->isRValueReference())
1277 addFlag(SPDie, dwarf::DW_AT_rvalue_reference);
1279 if (SP->isNoReturn())
1280 addFlag(SPDie, dwarf::DW_AT_noreturn);
1282 if (SP->isProtected())
1283 addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1284 dwarf::DW_ACCESS_protected);
1285 else if (SP->isPrivate())
1286 addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1287 dwarf::DW_ACCESS_private);
1288 else if (SP->isPublic())
1289 addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1290 dwarf::DW_ACCESS_public);
1292 if (SP->isExplicit())
1293 addFlag(SPDie, dwarf::DW_AT_explicit);
1295 if (SP->isMainSubprogram())
1296 addFlag(SPDie, dwarf::DW_AT_main_subprogram);
1299 void DwarfUnit::constructSubrangeDIE(DIE &Buffer, const DISubrange *SR,
1301 DIE &DW_Subrange = createAndAddDIE(dwarf::DW_TAG_subrange_type, Buffer);
1302 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, *IndexTy);
1304 // The LowerBound value defines the lower bounds which is typically zero for
1305 // C/C++. The Count value is the number of elements. Values are 64 bit. If
1306 // Count == -1 then the array is unbounded and we do not emit
1307 // DW_AT_lower_bound and DW_AT_count attributes.
1308 int64_t LowerBound = SR->getLowerBound();
1309 int64_t DefaultLowerBound = getDefaultLowerBound();
1310 int64_t Count = SR->getCount();
1312 if (DefaultLowerBound == -1 || LowerBound != DefaultLowerBound)
1313 addUInt(DW_Subrange, dwarf::DW_AT_lower_bound, None, LowerBound);
1316 // FIXME: An unbounded array should reference the expression that defines
1318 addUInt(DW_Subrange, dwarf::DW_AT_count, None, Count);
1321 DIE *DwarfUnit::getIndexTyDie() {
1324 // Construct an integer type to use for indexes.
1325 IndexTyDie = &createAndAddDIE(dwarf::DW_TAG_base_type, getUnitDie());
1326 addString(*IndexTyDie, dwarf::DW_AT_name, "sizetype");
1327 addUInt(*IndexTyDie, dwarf::DW_AT_byte_size, None, sizeof(int64_t));
1328 addUInt(*IndexTyDie, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
1329 dwarf::DW_ATE_unsigned);
1333 void DwarfUnit::constructArrayTypeDIE(DIE &Buffer, const DICompositeType *CTy) {
1334 if (CTy->isVector())
1335 addFlag(Buffer, dwarf::DW_AT_GNU_vector);
1337 // Emit the element type.
1338 addType(Buffer, resolve(CTy->getBaseType()));
1340 // Get an anonymous type for index type.
1341 // FIXME: This type should be passed down from the front end
1342 // as different languages may have different sizes for indexes.
1343 DIE *IdxTy = getIndexTyDie();
1345 // Add subranges to array type.
1346 DINodeArray Elements = CTy->getElements();
1347 for (unsigned i = 0, N = Elements.size(); i < N; ++i) {
1348 // FIXME: Should this really be such a loose cast?
1349 if (auto *Element = dyn_cast_or_null<DINode>(Elements[i]))
1350 if (Element->getTag() == dwarf::DW_TAG_subrange_type)
1351 constructSubrangeDIE(Buffer, cast<DISubrange>(Element), IdxTy);
1355 void DwarfUnit::constructEnumTypeDIE(DIE &Buffer, const DICompositeType *CTy) {
1356 DINodeArray Elements = CTy->getElements();
1358 // Add enumerators to enumeration type.
1359 for (unsigned i = 0, N = Elements.size(); i < N; ++i) {
1360 auto *Enum = dyn_cast_or_null<DIEnumerator>(Elements[i]);
1362 DIE &Enumerator = createAndAddDIE(dwarf::DW_TAG_enumerator, Buffer);
1363 StringRef Name = Enum->getName();
1364 addString(Enumerator, dwarf::DW_AT_name, Name);
1365 int64_t Value = Enum->getValue();
1366 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata,
1370 const DIType *DTy = resolve(CTy->getBaseType());
1372 addType(Buffer, DTy);
1373 addFlag(Buffer, dwarf::DW_AT_enum_class);
1377 void DwarfUnit::constructContainingTypeDIEs() {
1378 for (auto CI = ContainingTypeMap.begin(), CE = ContainingTypeMap.end();
1380 DIE &SPDie = *CI->first;
1381 const DINode *D = CI->second;
1384 DIE *NDie = getDIE(D);
1387 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, *NDie);
1391 void DwarfUnit::constructMemberDIE(DIE &Buffer, const DIDerivedType *DT) {
1392 DIE &MemberDie = createAndAddDIE(DT->getTag(), Buffer);
1393 StringRef Name = DT->getName();
1395 addString(MemberDie, dwarf::DW_AT_name, Name);
1397 addType(MemberDie, resolve(DT->getBaseType()));
1399 addSourceLine(MemberDie, DT);
1401 if (DT->getTag() == dwarf::DW_TAG_inheritance && DT->isVirtual()) {
1403 // For C++, virtual base classes are not at fixed offset. Use following
1404 // expression to extract appropriate offset from vtable.
1405 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1407 DIELoc *VBaseLocationDie = new (DIEValueAllocator) DIELoc;
1408 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1409 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1410 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1411 addUInt(*VBaseLocationDie, dwarf::DW_FORM_udata, DT->getOffsetInBits());
1412 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1413 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1414 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1416 addBlock(MemberDie, dwarf::DW_AT_data_member_location, VBaseLocationDie);
1418 uint64_t Size = DT->getSizeInBits();
1419 uint64_t FieldSize = DD->getBaseTypeSize(DT);
1420 uint32_t AlignInBytes = DT->getAlignInBytes();
1421 uint64_t OffsetInBytes;
1423 bool IsBitfield = FieldSize && Size != FieldSize;
1425 // Handle bitfield, assume bytes are 8 bits.
1426 if (DD->useDWARF2Bitfields())
1427 addUInt(MemberDie, dwarf::DW_AT_byte_size, None, FieldSize/8);
1428 addUInt(MemberDie, dwarf::DW_AT_bit_size, None, Size);
1430 uint64_t Offset = DT->getOffsetInBits();
1431 // We can't use DT->getAlignInBits() here: AlignInBits for member type
1432 // is non-zero if and only if alignment was forced (e.g. _Alignas()),
1433 // which can't be done with bitfields. Thus we use FieldSize here.
1434 uint32_t AlignInBits = FieldSize;
1435 uint32_t AlignMask = ~(AlignInBits - 1);
1436 // The bits from the start of the storage unit to the start of the field.
1437 uint64_t StartBitOffset = Offset - (Offset & AlignMask);
1438 // The byte offset of the field's aligned storage unit inside the struct.
1439 OffsetInBytes = (Offset - StartBitOffset) / 8;
1441 if (DD->useDWARF2Bitfields()) {
1442 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1443 uint64_t FieldOffset = (HiMark - FieldSize);
1444 Offset -= FieldOffset;
1446 // Maybe we need to work from the other end.
1447 if (Asm->getDataLayout().isLittleEndian())
1448 Offset = FieldSize - (Offset + Size);
1450 addUInt(MemberDie, dwarf::DW_AT_bit_offset, None, Offset);
1451 OffsetInBytes = FieldOffset >> 3;
1453 addUInt(MemberDie, dwarf::DW_AT_data_bit_offset, None, Offset);
1456 // This is not a bitfield.
1457 OffsetInBytes = DT->getOffsetInBits() / 8;
1459 addUInt(MemberDie, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata,
1463 if (DD->getDwarfVersion() <= 2) {
1464 DIELoc *MemLocationDie = new (DIEValueAllocator) DIELoc;
1465 addUInt(*MemLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
1466 addUInt(*MemLocationDie, dwarf::DW_FORM_udata, OffsetInBytes);
1467 addBlock(MemberDie, dwarf::DW_AT_data_member_location, MemLocationDie);
1468 } else if (!IsBitfield || DD->useDWARF2Bitfields())
1469 addUInt(MemberDie, dwarf::DW_AT_data_member_location, None,
1473 if (DT->isProtected())
1474 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1475 dwarf::DW_ACCESS_protected);
1476 else if (DT->isPrivate())
1477 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1478 dwarf::DW_ACCESS_private);
1479 // Otherwise C++ member and base classes are considered public.
1480 else if (DT->isPublic())
1481 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1482 dwarf::DW_ACCESS_public);
1483 if (DT->isVirtual())
1484 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1,
1485 dwarf::DW_VIRTUALITY_virtual);
1487 // Objective-C properties.
1488 if (DINode *PNode = DT->getObjCProperty())
1489 if (DIE *PDie = getDIE(PNode))
1490 MemberDie.addValue(DIEValueAllocator, dwarf::DW_AT_APPLE_property,
1491 dwarf::DW_FORM_ref4, DIEEntry(*PDie));
1493 if (DT->isArtificial())
1494 addFlag(MemberDie, dwarf::DW_AT_artificial);
1497 DIE *DwarfUnit::getOrCreateStaticMemberDIE(const DIDerivedType *DT) {
1501 // Construct the context before querying for the existence of the DIE in case
1502 // such construction creates the DIE.
1503 DIE *ContextDIE = getOrCreateContextDIE(resolve(DT->getScope()));
1504 assert(dwarf::isType(ContextDIE->getTag()) &&
1505 "Static member should belong to a type.");
1507 if (DIE *StaticMemberDIE = getDIE(DT))
1508 return StaticMemberDIE;
1510 DIE &StaticMemberDIE = createAndAddDIE(DT->getTag(), *ContextDIE, DT);
1512 const DIType *Ty = resolve(DT->getBaseType());
1514 addString(StaticMemberDIE, dwarf::DW_AT_name, DT->getName());
1515 addType(StaticMemberDIE, Ty);
1516 addSourceLine(StaticMemberDIE, DT);
1517 addFlag(StaticMemberDIE, dwarf::DW_AT_external);
1518 addFlag(StaticMemberDIE, dwarf::DW_AT_declaration);
1520 // FIXME: We could omit private if the parent is a class_type, and
1521 // public if the parent is something else.
1522 if (DT->isProtected())
1523 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1524 dwarf::DW_ACCESS_protected);
1525 else if (DT->isPrivate())
1526 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1527 dwarf::DW_ACCESS_private);
1528 else if (DT->isPublic())
1529 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1530 dwarf::DW_ACCESS_public);
1532 if (const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(DT->getConstant()))
1533 addConstantValue(StaticMemberDIE, CI, Ty);
1534 if (const ConstantFP *CFP = dyn_cast_or_null<ConstantFP>(DT->getConstant()))
1535 addConstantFPValue(StaticMemberDIE, CFP);
1537 if (uint32_t AlignInBytes = DT->getAlignInBytes())
1538 addUInt(StaticMemberDIE, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata,
1541 return &StaticMemberDIE;
1544 void DwarfUnit::emitCommonHeader(bool UseOffsets, dwarf::UnitType UT) {
1545 // Emit size of content not including length itself
1546 Asm->OutStreamer->AddComment("Length of Unit");
1547 Asm->EmitInt32(getHeaderSize() + getUnitDie().getSize());
1549 Asm->OutStreamer->AddComment("DWARF version number");
1550 unsigned Version = DD->getDwarfVersion();
1551 Asm->EmitInt16(Version);
1553 // DWARF v5 reorders the address size and adds a unit type.
1555 Asm->OutStreamer->AddComment("DWARF Unit Type");
1557 Asm->OutStreamer->AddComment("Address Size (in bytes)");
1558 Asm->EmitInt8(Asm->MAI->getCodePointerSize());
1561 // We share one abbreviations table across all units so it's always at the
1562 // start of the section. Use a relocatable offset where needed to ensure
1563 // linking doesn't invalidate that offset.
1564 Asm->OutStreamer->AddComment("Offset Into Abbrev. Section");
1565 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
1569 Asm->emitDwarfSymbolReference(
1570 TLOF.getDwarfAbbrevSection()->getBeginSymbol(), false);
1573 Asm->OutStreamer->AddComment("Address Size (in bytes)");
1574 Asm->EmitInt8(Asm->MAI->getCodePointerSize());
1578 void DwarfTypeUnit::emitHeader(bool UseOffsets) {
1579 DwarfUnit::emitCommonHeader(UseOffsets,
1580 DD->useSplitDwarf() ? dwarf::DW_UT_split_type
1581 : dwarf::DW_UT_type);
1582 Asm->OutStreamer->AddComment("Type Signature");
1583 Asm->OutStreamer->EmitIntValue(TypeSignature, sizeof(TypeSignature));
1584 Asm->OutStreamer->AddComment("Type DIE Offset");
1585 // In a skeleton type unit there is no type DIE so emit a zero offset.
1586 Asm->OutStreamer->EmitIntValue(Ty ? Ty->getOffset() : 0,
1587 sizeof(Ty->getOffset()));
1591 DwarfUnit::addSectionDelta(DIE &Die, dwarf::Attribute Attribute,
1592 const MCSymbol *Hi, const MCSymbol *Lo) {
1593 return Die.addValue(DIEValueAllocator, Attribute,
1594 DD->getDwarfVersion() >= 4 ? dwarf::DW_FORM_sec_offset
1595 : dwarf::DW_FORM_data4,
1596 new (DIEValueAllocator) DIEDelta(Hi, Lo));
1600 DwarfUnit::addSectionLabel(DIE &Die, dwarf::Attribute Attribute,
1601 const MCSymbol *Label, const MCSymbol *Sec) {
1602 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
1603 return addLabel(Die, Attribute,
1604 DD->getDwarfVersion() >= 4 ? dwarf::DW_FORM_sec_offset
1605 : dwarf::DW_FORM_data4,
1607 return addSectionDelta(Die, Attribute, Label, Sec);
1610 bool DwarfTypeUnit::isDwoUnit() const {
1611 // Since there are no skeleton type units, all type units are dwo type units
1612 // when split DWARF is being used.
1613 return DD->useSplitDwarf();
1616 void DwarfTypeUnit::addGlobalName(StringRef Name, const DIE &Die,
1617 const DIScope *Context) {
1618 getCU().addGlobalNameForTypeUnit(Name, Context);
1621 void DwarfTypeUnit::addGlobalType(const DIType *Ty, const DIE &Die,
1622 const DIScope *Context) {
1623 getCU().addGlobalTypeUnitType(Ty, Context);
1626 const MCSymbol *DwarfUnit::getCrossSectionRelativeBaseAddress() const {
1627 if (!Asm->MAI->doesDwarfUseRelocationsAcrossSections())
1631 return getSection()->getBeginSymbol();