Revert r354244 "[DAGCombiner] Eliminate dead stores to stack."
[llvm-complete.git] / lib / CodeGen / AsmPrinter / DwarfUnit.cpp
blob8534bd12ba22171ec7640c31b01e25fb6447fa51
1 //===-- llvm/CodeGen/DwarfUnit.cpp - Dwarf Type and Compile Units ---------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains support for constructing a dwarf compile unit.
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"
40 #include <cassert>
41 #include <cstdint>
42 #include <string>
43 #include <utility>
45 using namespace llvm;
47 #define DEBUG_TYPE "dwarfdebug"
49 DIEDwarfExpression::DIEDwarfExpression(const AsmPrinter &AP, DwarfUnit &DU,
50 DIELoc &DIE)
51 : DwarfExpression(AP.getDwarfVersion()), AP(AP), DU(DU),
52 DIE(DIE) {}
54 void DIEDwarfExpression::emitOp(uint8_t Op, const char* Comment) {
55 DU.addUInt(DIE, dwarf::DW_FORM_data1, Op);
58 void DIEDwarfExpression::emitSigned(int64_t Value) {
59 DU.addSInt(DIE, dwarf::DW_FORM_sdata, Value);
62 void DIEDwarfExpression::emitUnsigned(uint64_t Value) {
63 DU.addUInt(DIE, dwarf::DW_FORM_udata, Value);
66 bool DIEDwarfExpression::isFrameRegister(const TargetRegisterInfo &TRI,
67 unsigned MachineReg) {
68 return MachineReg == TRI.getFrameRegister(*AP.MF);
71 DwarfUnit::DwarfUnit(dwarf::Tag UnitTag, const DICompileUnit *Node,
72 AsmPrinter *A, DwarfDebug *DW, DwarfFile *DWU)
73 : DIEUnit(A->getDwarfVersion(), A->MAI->getCodePointerSize(), UnitTag),
74 CUNode(Node), Asm(A), DD(DW), DU(DWU), IndexTyDie(nullptr) {
77 DwarfTypeUnit::DwarfTypeUnit(DwarfCompileUnit &CU, AsmPrinter *A,
78 DwarfDebug *DW, DwarfFile *DWU,
79 MCDwarfDwoLineTable *SplitLineTable)
80 : DwarfUnit(dwarf::DW_TAG_type_unit, CU.getCUNode(), A, DW, DWU), CU(CU),
81 SplitLineTable(SplitLineTable) {
84 DwarfUnit::~DwarfUnit() {
85 for (unsigned j = 0, M = DIEBlocks.size(); j < M; ++j)
86 DIEBlocks[j]->~DIEBlock();
87 for (unsigned j = 0, M = DIELocs.size(); j < M; ++j)
88 DIELocs[j]->~DIELoc();
91 int64_t DwarfUnit::getDefaultLowerBound() const {
92 switch (getLanguage()) {
93 default:
94 break;
96 // The languages below have valid values in all DWARF versions.
97 case dwarf::DW_LANG_C:
98 case dwarf::DW_LANG_C89:
99 case dwarf::DW_LANG_C_plus_plus:
100 return 0;
102 case dwarf::DW_LANG_Fortran77:
103 case dwarf::DW_LANG_Fortran90:
104 return 1;
106 // The languages below have valid values only if the DWARF version >= 3.
107 case dwarf::DW_LANG_C99:
108 case dwarf::DW_LANG_ObjC:
109 case dwarf::DW_LANG_ObjC_plus_plus:
110 if (DD->getDwarfVersion() >= 3)
111 return 0;
112 break;
114 case dwarf::DW_LANG_Fortran95:
115 if (DD->getDwarfVersion() >= 3)
116 return 1;
117 break;
119 // Starting with DWARF v4, all defined languages have valid values.
120 case dwarf::DW_LANG_D:
121 case dwarf::DW_LANG_Java:
122 case dwarf::DW_LANG_Python:
123 case dwarf::DW_LANG_UPC:
124 if (DD->getDwarfVersion() >= 4)
125 return 0;
126 break;
128 case dwarf::DW_LANG_Ada83:
129 case dwarf::DW_LANG_Ada95:
130 case dwarf::DW_LANG_Cobol74:
131 case dwarf::DW_LANG_Cobol85:
132 case dwarf::DW_LANG_Modula2:
133 case dwarf::DW_LANG_Pascal83:
134 case dwarf::DW_LANG_PLI:
135 if (DD->getDwarfVersion() >= 4)
136 return 1;
137 break;
139 // The languages below are new in DWARF v5.
140 case dwarf::DW_LANG_BLISS:
141 case dwarf::DW_LANG_C11:
142 case dwarf::DW_LANG_C_plus_plus_03:
143 case dwarf::DW_LANG_C_plus_plus_11:
144 case dwarf::DW_LANG_C_plus_plus_14:
145 case dwarf::DW_LANG_Dylan:
146 case dwarf::DW_LANG_Go:
147 case dwarf::DW_LANG_Haskell:
148 case dwarf::DW_LANG_OCaml:
149 case dwarf::DW_LANG_OpenCL:
150 case dwarf::DW_LANG_RenderScript:
151 case dwarf::DW_LANG_Rust:
152 case dwarf::DW_LANG_Swift:
153 if (DD->getDwarfVersion() >= 5)
154 return 0;
155 break;
157 case dwarf::DW_LANG_Fortran03:
158 case dwarf::DW_LANG_Fortran08:
159 case dwarf::DW_LANG_Julia:
160 case dwarf::DW_LANG_Modula3:
161 if (DD->getDwarfVersion() >= 5)
162 return 1;
163 break;
166 return -1;
169 /// Check whether the DIE for this MDNode can be shared across CUs.
170 bool DwarfUnit::isShareableAcrossCUs(const DINode *D) const {
171 // When the MDNode can be part of the type system, the DIE can be shared
172 // across CUs.
173 // Combining type units and cross-CU DIE sharing is lower value (since
174 // cross-CU DIE sharing is used in LTO and removes type redundancy at that
175 // level already) but may be implementable for some value in projects
176 // building multiple independent libraries with LTO and then linking those
177 // together.
178 if (isDwoUnit() && !DD->shareAcrossDWOCUs())
179 return false;
180 return (isa<DIType>(D) ||
181 (isa<DISubprogram>(D) && !cast<DISubprogram>(D)->isDefinition())) &&
182 !DD->generateTypeUnits();
185 DIE *DwarfUnit::getDIE(const DINode *D) const {
186 if (isShareableAcrossCUs(D))
187 return DU->getDIE(D);
188 return MDNodeToDieMap.lookup(D);
191 void DwarfUnit::insertDIE(const DINode *Desc, DIE *D) {
192 if (isShareableAcrossCUs(Desc)) {
193 DU->insertDIE(Desc, D);
194 return;
196 MDNodeToDieMap.insert(std::make_pair(Desc, D));
199 void DwarfUnit::addFlag(DIE &Die, dwarf::Attribute Attribute) {
200 if (DD->getDwarfVersion() >= 4)
201 Die.addValue(DIEValueAllocator, Attribute, dwarf::DW_FORM_flag_present,
202 DIEInteger(1));
203 else
204 Die.addValue(DIEValueAllocator, Attribute, dwarf::DW_FORM_flag,
205 DIEInteger(1));
208 void DwarfUnit::addUInt(DIEValueList &Die, dwarf::Attribute Attribute,
209 Optional<dwarf::Form> Form, uint64_t Integer) {
210 if (!Form)
211 Form = DIEInteger::BestForm(false, Integer);
212 assert(Form != dwarf::DW_FORM_implicit_const &&
213 "DW_FORM_implicit_const is used only for signed integers");
214 Die.addValue(DIEValueAllocator, Attribute, *Form, DIEInteger(Integer));
217 void DwarfUnit::addUInt(DIEValueList &Block, dwarf::Form Form,
218 uint64_t Integer) {
219 addUInt(Block, (dwarf::Attribute)0, Form, Integer);
222 void DwarfUnit::addSInt(DIEValueList &Die, dwarf::Attribute Attribute,
223 Optional<dwarf::Form> Form, int64_t Integer) {
224 if (!Form)
225 Form = DIEInteger::BestForm(true, Integer);
226 Die.addValue(DIEValueAllocator, Attribute, *Form, DIEInteger(Integer));
229 void DwarfUnit::addSInt(DIELoc &Die, Optional<dwarf::Form> Form,
230 int64_t Integer) {
231 addSInt(Die, (dwarf::Attribute)0, Form, Integer);
234 void DwarfUnit::addString(DIE &Die, dwarf::Attribute Attribute,
235 StringRef String) {
236 if (CUNode->isDebugDirectivesOnly())
237 return;
239 if (DD->useInlineStrings()) {
240 Die.addValue(DIEValueAllocator, Attribute, dwarf::DW_FORM_string,
241 new (DIEValueAllocator)
242 DIEInlineString(String, DIEValueAllocator));
243 return;
245 dwarf::Form IxForm =
246 isDwoUnit() ? dwarf::DW_FORM_GNU_str_index : dwarf::DW_FORM_strp;
248 auto StringPoolEntry =
249 useSegmentedStringOffsetsTable() || IxForm == dwarf::DW_FORM_GNU_str_index
250 ? DU->getStringPool().getIndexedEntry(*Asm, String)
251 : DU->getStringPool().getEntry(*Asm, String);
253 // For DWARF v5 and beyond, use the smallest strx? form possible.
254 if (useSegmentedStringOffsetsTable()) {
255 IxForm = dwarf::DW_FORM_strx1;
256 unsigned Index = StringPoolEntry.getIndex();
257 if (Index > 0xffffff)
258 IxForm = dwarf::DW_FORM_strx4;
259 else if (Index > 0xffff)
260 IxForm = dwarf::DW_FORM_strx3;
261 else if (Index > 0xff)
262 IxForm = dwarf::DW_FORM_strx2;
264 Die.addValue(DIEValueAllocator, Attribute, IxForm,
265 DIEString(StringPoolEntry));
268 DIEValueList::value_iterator DwarfUnit::addLabel(DIEValueList &Die,
269 dwarf::Attribute Attribute,
270 dwarf::Form Form,
271 const MCSymbol *Label) {
272 return Die.addValue(DIEValueAllocator, Attribute, Form, DIELabel(Label));
275 void DwarfUnit::addLabel(DIELoc &Die, dwarf::Form Form, const MCSymbol *Label) {
276 addLabel(Die, (dwarf::Attribute)0, Form, Label);
279 void DwarfUnit::addSectionOffset(DIE &Die, dwarf::Attribute Attribute,
280 uint64_t Integer) {
281 if (DD->getDwarfVersion() >= 4)
282 addUInt(Die, Attribute, dwarf::DW_FORM_sec_offset, Integer);
283 else
284 addUInt(Die, Attribute, dwarf::DW_FORM_data4, Integer);
287 MD5::MD5Result *DwarfUnit::getMD5AsBytes(const DIFile *File) const {
288 assert(File);
289 if (DD->getDwarfVersion() < 5)
290 return nullptr;
291 Optional<DIFile::ChecksumInfo<StringRef>> Checksum = File->getChecksum();
292 if (!Checksum || Checksum->Kind != DIFile::CSK_MD5)
293 return nullptr;
295 // Convert the string checksum to an MD5Result for the streamer.
296 // The verifier validates the checksum so we assume it's okay.
297 // An MD5 checksum is 16 bytes.
298 std::string ChecksumString = fromHex(Checksum->Value);
299 void *CKMem = Asm->OutStreamer->getContext().allocate(16, 1);
300 memcpy(CKMem, ChecksumString.data(), 16);
301 return reinterpret_cast<MD5::MD5Result *>(CKMem);
304 unsigned DwarfTypeUnit::getOrCreateSourceID(const DIFile *File) {
305 if (!SplitLineTable)
306 return getCU().getOrCreateSourceID(File);
307 if (!UsedLineTable) {
308 UsedLineTable = true;
309 // This is a split type unit that needs a line table.
310 addSectionOffset(getUnitDie(), dwarf::DW_AT_stmt_list, 0);
312 return SplitLineTable->getFile(File->getDirectory(), File->getFilename(),
313 getMD5AsBytes(File), File->getSource());
316 void DwarfUnit::addOpAddress(DIELoc &Die, const MCSymbol *Sym) {
317 if (DD->getDwarfVersion() >= 5) {
318 addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_addrx);
319 addUInt(Die, dwarf::DW_FORM_addrx, DD->getAddressPool().getIndex(Sym));
320 return;
323 if (DD->useSplitDwarf()) {
324 addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_addr_index);
325 addUInt(Die, dwarf::DW_FORM_GNU_addr_index,
326 DD->getAddressPool().getIndex(Sym));
327 return;
330 addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
331 addLabel(Die, dwarf::DW_FORM_udata, Sym);
334 void DwarfUnit::addLabelDelta(DIE &Die, dwarf::Attribute Attribute,
335 const MCSymbol *Hi, const MCSymbol *Lo) {
336 Die.addValue(DIEValueAllocator, Attribute, dwarf::DW_FORM_data4,
337 new (DIEValueAllocator) DIEDelta(Hi, Lo));
340 void DwarfUnit::addDIEEntry(DIE &Die, dwarf::Attribute Attribute, DIE &Entry) {
341 addDIEEntry(Die, Attribute, DIEEntry(Entry));
344 void DwarfUnit::addDIETypeSignature(DIE &Die, uint64_t Signature) {
345 // Flag the type unit reference as a declaration so that if it contains
346 // members (implicit special members, static data member definitions, member
347 // declarations for definitions in this CU, etc) consumers don't get confused
348 // and think this is a full definition.
349 addFlag(Die, dwarf::DW_AT_declaration);
351 Die.addValue(DIEValueAllocator, dwarf::DW_AT_signature,
352 dwarf::DW_FORM_ref_sig8, DIEInteger(Signature));
355 void DwarfUnit::addDIEEntry(DIE &Die, dwarf::Attribute Attribute,
356 DIEEntry Entry) {
357 const DIEUnit *CU = Die.getUnit();
358 const DIEUnit *EntryCU = Entry.getEntry().getUnit();
359 if (!CU)
360 // We assume that Die belongs to this CU, if it is not linked to any CU yet.
361 CU = getUnitDie().getUnit();
362 if (!EntryCU)
363 EntryCU = getUnitDie().getUnit();
364 Die.addValue(DIEValueAllocator, Attribute,
365 EntryCU == CU ? dwarf::DW_FORM_ref4 : dwarf::DW_FORM_ref_addr,
366 Entry);
369 DIE &DwarfUnit::createAndAddDIE(unsigned Tag, DIE &Parent, const DINode *N) {
370 DIE &Die = Parent.addChild(DIE::get(DIEValueAllocator, (dwarf::Tag)Tag));
371 if (N)
372 insertDIE(N, &Die);
373 return Die;
376 void DwarfUnit::addBlock(DIE &Die, dwarf::Attribute Attribute, DIELoc *Loc) {
377 Loc->ComputeSize(Asm);
378 DIELocs.push_back(Loc); // Memoize so we can call the destructor later on.
379 Die.addValue(DIEValueAllocator, Attribute,
380 Loc->BestForm(DD->getDwarfVersion()), Loc);
383 void DwarfUnit::addBlock(DIE &Die, dwarf::Attribute Attribute,
384 DIEBlock *Block) {
385 Block->ComputeSize(Asm);
386 DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on.
387 Die.addValue(DIEValueAllocator, Attribute, Block->BestForm(), Block);
390 void DwarfUnit::addSourceLine(DIE &Die, unsigned Line, const DIFile *File) {
391 if (Line == 0)
392 return;
394 unsigned FileID = getOrCreateSourceID(File);
395 assert(FileID && "Invalid file id");
396 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
397 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
400 void DwarfUnit::addSourceLine(DIE &Die, const DILocalVariable *V) {
401 assert(V);
403 addSourceLine(Die, V->getLine(), V->getFile());
406 void DwarfUnit::addSourceLine(DIE &Die, const DIGlobalVariable *G) {
407 assert(G);
409 addSourceLine(Die, G->getLine(), G->getFile());
412 void DwarfUnit::addSourceLine(DIE &Die, const DISubprogram *SP) {
413 assert(SP);
415 addSourceLine(Die, SP->getLine(), SP->getFile());
418 void DwarfUnit::addSourceLine(DIE &Die, const DILabel *L) {
419 assert(L);
421 addSourceLine(Die, L->getLine(), L->getFile());
424 void DwarfUnit::addSourceLine(DIE &Die, const DIType *Ty) {
425 assert(Ty);
427 addSourceLine(Die, Ty->getLine(), Ty->getFile());
430 void DwarfUnit::addSourceLine(DIE &Die, const DIObjCProperty *Ty) {
431 assert(Ty);
433 addSourceLine(Die, Ty->getLine(), Ty->getFile());
436 /// Return true if type encoding is unsigned.
437 static bool isUnsignedDIType(DwarfDebug *DD, const DIType *Ty) {
438 if (auto *CTy = dyn_cast<DICompositeType>(Ty)) {
439 // FIXME: Enums without a fixed underlying type have unknown signedness
440 // here, leading to incorrectly emitted constants.
441 if (CTy->getTag() == dwarf::DW_TAG_enumeration_type)
442 return false;
444 // (Pieces of) aggregate types that get hacked apart by SROA may be
445 // represented by a constant. Encode them as unsigned bytes.
446 return true;
449 if (auto *DTy = dyn_cast<DIDerivedType>(Ty)) {
450 dwarf::Tag T = (dwarf::Tag)Ty->getTag();
451 // Encode pointer constants as unsigned bytes. This is used at least for
452 // null pointer constant emission.
453 // FIXME: reference and rvalue_reference /probably/ shouldn't be allowed
454 // here, but accept them for now due to a bug in SROA producing bogus
455 // dbg.values.
456 if (T == dwarf::DW_TAG_pointer_type ||
457 T == dwarf::DW_TAG_ptr_to_member_type ||
458 T == dwarf::DW_TAG_reference_type ||
459 T == dwarf::DW_TAG_rvalue_reference_type)
460 return true;
461 assert(T == dwarf::DW_TAG_typedef || T == dwarf::DW_TAG_const_type ||
462 T == dwarf::DW_TAG_volatile_type ||
463 T == dwarf::DW_TAG_restrict_type || T == dwarf::DW_TAG_atomic_type);
464 DITypeRef Deriv = DTy->getBaseType();
465 assert(Deriv && "Expected valid base type");
466 return isUnsignedDIType(DD, DD->resolve(Deriv));
469 auto *BTy = cast<DIBasicType>(Ty);
470 unsigned Encoding = BTy->getEncoding();
471 assert((Encoding == dwarf::DW_ATE_unsigned ||
472 Encoding == dwarf::DW_ATE_unsigned_char ||
473 Encoding == dwarf::DW_ATE_signed ||
474 Encoding == dwarf::DW_ATE_signed_char ||
475 Encoding == dwarf::DW_ATE_float || Encoding == dwarf::DW_ATE_UTF ||
476 Encoding == dwarf::DW_ATE_boolean ||
477 (Ty->getTag() == dwarf::DW_TAG_unspecified_type &&
478 Ty->getName() == "decltype(nullptr)")) &&
479 "Unsupported encoding");
480 return Encoding == dwarf::DW_ATE_unsigned ||
481 Encoding == dwarf::DW_ATE_unsigned_char ||
482 Encoding == dwarf::DW_ATE_UTF || Encoding == dwarf::DW_ATE_boolean ||
483 Ty->getTag() == dwarf::DW_TAG_unspecified_type;
486 void DwarfUnit::addConstantFPValue(DIE &Die, const MachineOperand &MO) {
487 assert(MO.isFPImm() && "Invalid machine operand!");
488 DIEBlock *Block = new (DIEValueAllocator) DIEBlock;
489 APFloat FPImm = MO.getFPImm()->getValueAPF();
491 // Get the raw data form of the floating point.
492 const APInt FltVal = FPImm.bitcastToAPInt();
493 const char *FltPtr = (const char *)FltVal.getRawData();
495 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte.
496 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
497 int Incr = (LittleEndian ? 1 : -1);
498 int Start = (LittleEndian ? 0 : NumBytes - 1);
499 int Stop = (LittleEndian ? NumBytes : -1);
501 // Output the constant to DWARF one byte at a time.
502 for (; Start != Stop; Start += Incr)
503 addUInt(*Block, dwarf::DW_FORM_data1, (unsigned char)0xFF & FltPtr[Start]);
505 addBlock(Die, dwarf::DW_AT_const_value, Block);
508 void DwarfUnit::addConstantFPValue(DIE &Die, const ConstantFP *CFP) {
509 // Pass this down to addConstantValue as an unsigned bag of bits.
510 addConstantValue(Die, CFP->getValueAPF().bitcastToAPInt(), true);
513 void DwarfUnit::addConstantValue(DIE &Die, const ConstantInt *CI,
514 const DIType *Ty) {
515 addConstantValue(Die, CI->getValue(), Ty);
518 void DwarfUnit::addConstantValue(DIE &Die, const MachineOperand &MO,
519 const DIType *Ty) {
520 assert(MO.isImm() && "Invalid machine operand!");
522 addConstantValue(Die, isUnsignedDIType(DD, Ty), MO.getImm());
525 void DwarfUnit::addConstantValue(DIE &Die, bool Unsigned, uint64_t Val) {
526 // FIXME: This is a bit conservative/simple - it emits negative values always
527 // sign extended to 64 bits rather than minimizing the number of bytes.
528 addUInt(Die, dwarf::DW_AT_const_value,
529 Unsigned ? dwarf::DW_FORM_udata : dwarf::DW_FORM_sdata, Val);
532 void DwarfUnit::addConstantValue(DIE &Die, const APInt &Val, const DIType *Ty) {
533 addConstantValue(Die, Val, isUnsignedDIType(DD, Ty));
536 void DwarfUnit::addConstantValue(DIE &Die, const APInt &Val, bool Unsigned) {
537 unsigned CIBitWidth = Val.getBitWidth();
538 if (CIBitWidth <= 64) {
539 addConstantValue(Die, Unsigned,
540 Unsigned ? Val.getZExtValue() : Val.getSExtValue());
541 return;
544 DIEBlock *Block = new (DIEValueAllocator) DIEBlock;
546 // Get the raw data form of the large APInt.
547 const uint64_t *Ptr64 = Val.getRawData();
549 int NumBytes = Val.getBitWidth() / 8; // 8 bits per byte.
550 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
552 // Output the constant to DWARF one byte at a time.
553 for (int i = 0; i < NumBytes; i++) {
554 uint8_t c;
555 if (LittleEndian)
556 c = Ptr64[i / 8] >> (8 * (i & 7));
557 else
558 c = Ptr64[(NumBytes - 1 - i) / 8] >> (8 * ((NumBytes - 1 - i) & 7));
559 addUInt(*Block, dwarf::DW_FORM_data1, c);
562 addBlock(Die, dwarf::DW_AT_const_value, Block);
565 void DwarfUnit::addLinkageName(DIE &Die, StringRef LinkageName) {
566 if (!LinkageName.empty())
567 addString(Die,
568 DD->getDwarfVersion() >= 4 ? dwarf::DW_AT_linkage_name
569 : dwarf::DW_AT_MIPS_linkage_name,
570 GlobalValue::dropLLVMManglingEscape(LinkageName));
573 void DwarfUnit::addTemplateParams(DIE &Buffer, DINodeArray TParams) {
574 // Add template parameters.
575 for (const auto *Element : TParams) {
576 if (auto *TTP = dyn_cast<DITemplateTypeParameter>(Element))
577 constructTemplateTypeParameterDIE(Buffer, TTP);
578 else if (auto *TVP = dyn_cast<DITemplateValueParameter>(Element))
579 constructTemplateValueParameterDIE(Buffer, TVP);
583 /// Add thrown types.
584 void DwarfUnit::addThrownTypes(DIE &Die, DINodeArray ThrownTypes) {
585 for (const auto *Ty : ThrownTypes) {
586 DIE &TT = createAndAddDIE(dwarf::DW_TAG_thrown_type, Die);
587 addType(TT, cast<DIType>(Ty));
591 DIE *DwarfUnit::getOrCreateContextDIE(const DIScope *Context) {
592 if (!Context || isa<DIFile>(Context))
593 return &getUnitDie();
594 if (auto *T = dyn_cast<DIType>(Context))
595 return getOrCreateTypeDIE(T);
596 if (auto *NS = dyn_cast<DINamespace>(Context))
597 return getOrCreateNameSpace(NS);
598 if (auto *SP = dyn_cast<DISubprogram>(Context))
599 return getOrCreateSubprogramDIE(SP);
600 if (auto *M = dyn_cast<DIModule>(Context))
601 return getOrCreateModule(M);
602 return getDIE(Context);
605 DIE *DwarfTypeUnit::createTypeDIE(const DICompositeType *Ty) {
606 auto *Context = resolve(Ty->getScope());
607 DIE *ContextDIE = getOrCreateContextDIE(Context);
609 if (DIE *TyDIE = getDIE(Ty))
610 return TyDIE;
612 // Create new type.
613 DIE &TyDIE = createAndAddDIE(Ty->getTag(), *ContextDIE, Ty);
615 constructTypeDIE(TyDIE, cast<DICompositeType>(Ty));
617 updateAcceleratorTables(Context, Ty, TyDIE);
618 return &TyDIE;
621 DIE *DwarfUnit::getOrCreateTypeDIE(const MDNode *TyNode) {
622 if (!TyNode)
623 return nullptr;
625 auto *Ty = cast<DIType>(TyNode);
627 // DW_TAG_restrict_type is not supported in DWARF2
628 if (Ty->getTag() == dwarf::DW_TAG_restrict_type && DD->getDwarfVersion() <= 2)
629 return getOrCreateTypeDIE(resolve(cast<DIDerivedType>(Ty)->getBaseType()));
631 // DW_TAG_atomic_type is not supported in DWARF < 5
632 if (Ty->getTag() == dwarf::DW_TAG_atomic_type && DD->getDwarfVersion() < 5)
633 return getOrCreateTypeDIE(resolve(cast<DIDerivedType>(Ty)->getBaseType()));
635 // Construct the context before querying for the existence of the DIE in case
636 // such construction creates the DIE.
637 auto *Context = resolve(Ty->getScope());
638 DIE *ContextDIE = getOrCreateContextDIE(Context);
639 assert(ContextDIE);
641 if (DIE *TyDIE = getDIE(Ty))
642 return TyDIE;
644 // Create new type.
645 DIE &TyDIE = createAndAddDIE(Ty->getTag(), *ContextDIE, Ty);
647 updateAcceleratorTables(Context, Ty, TyDIE);
649 if (auto *BT = dyn_cast<DIBasicType>(Ty))
650 constructTypeDIE(TyDIE, BT);
651 else if (auto *STy = dyn_cast<DISubroutineType>(Ty))
652 constructTypeDIE(TyDIE, STy);
653 else if (auto *CTy = dyn_cast<DICompositeType>(Ty)) {
654 if (DD->generateTypeUnits() && !Ty->isForwardDecl())
655 if (MDString *TypeId = CTy->getRawIdentifier()) {
656 DD->addDwarfTypeUnitType(getCU(), TypeId->getString(), TyDIE, CTy);
657 // Skip updating the accelerator tables since this is not the full type.
658 return &TyDIE;
660 constructTypeDIE(TyDIE, CTy);
661 } else {
662 constructTypeDIE(TyDIE, cast<DIDerivedType>(Ty));
665 return &TyDIE;
668 void DwarfUnit::updateAcceleratorTables(const DIScope *Context,
669 const DIType *Ty, const DIE &TyDIE) {
670 if (!Ty->getName().empty() && !Ty->isForwardDecl()) {
671 bool IsImplementation = false;
672 if (auto *CT = dyn_cast<DICompositeType>(Ty)) {
673 // A runtime language of 0 actually means C/C++ and that any
674 // non-negative value is some version of Objective-C/C++.
675 IsImplementation = CT->getRuntimeLang() == 0 || CT->isObjcClassComplete();
677 unsigned Flags = IsImplementation ? dwarf::DW_FLAG_type_implementation : 0;
678 DD->addAccelType(*CUNode, Ty->getName(), TyDIE, Flags);
680 if (!Context || isa<DICompileUnit>(Context) || isa<DIFile>(Context) ||
681 isa<DINamespace>(Context))
682 addGlobalType(Ty, TyDIE, Context);
686 void DwarfUnit::addType(DIE &Entity, const DIType *Ty,
687 dwarf::Attribute Attribute) {
688 assert(Ty && "Trying to add a type that doesn't exist?");
689 addDIEEntry(Entity, Attribute, DIEEntry(*getOrCreateTypeDIE(Ty)));
692 std::string DwarfUnit::getParentContextString(const DIScope *Context) const {
693 if (!Context)
694 return "";
696 // FIXME: Decide whether to implement this for non-C++ languages.
697 if (getLanguage() != dwarf::DW_LANG_C_plus_plus)
698 return "";
700 std::string CS;
701 SmallVector<const DIScope *, 1> Parents;
702 while (!isa<DICompileUnit>(Context)) {
703 Parents.push_back(Context);
704 if (Context->getScope())
705 Context = resolve(Context->getScope());
706 else
707 // Structure, etc types will have a NULL context if they're at the top
708 // level.
709 break;
712 // Reverse iterate over our list to go from the outermost construct to the
713 // innermost.
714 for (const DIScope *Ctx : make_range(Parents.rbegin(), Parents.rend())) {
715 StringRef Name = Ctx->getName();
716 if (Name.empty() && isa<DINamespace>(Ctx))
717 Name = "(anonymous namespace)";
718 if (!Name.empty()) {
719 CS += Name;
720 CS += "::";
723 return CS;
726 void DwarfUnit::constructTypeDIE(DIE &Buffer, const DIBasicType *BTy) {
727 // Get core information.
728 StringRef Name = BTy->getName();
729 // Add name if not anonymous or intermediate type.
730 if (!Name.empty())
731 addString(Buffer, dwarf::DW_AT_name, Name);
733 // An unspecified type only has a name attribute.
734 if (BTy->getTag() == dwarf::DW_TAG_unspecified_type)
735 return;
737 addUInt(Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
738 BTy->getEncoding());
740 uint64_t Size = BTy->getSizeInBits() >> 3;
741 addUInt(Buffer, dwarf::DW_AT_byte_size, None, Size);
743 if (BTy->isBigEndian())
744 addUInt(Buffer, dwarf::DW_AT_endianity, None, dwarf::DW_END_big);
745 else if (BTy->isLittleEndian())
746 addUInt(Buffer, dwarf::DW_AT_endianity, None, dwarf::DW_END_little);
749 void DwarfUnit::constructTypeDIE(DIE &Buffer, const DIDerivedType *DTy) {
750 // Get core information.
751 StringRef Name = DTy->getName();
752 uint64_t Size = DTy->getSizeInBits() >> 3;
753 uint16_t Tag = Buffer.getTag();
755 // Map to main type, void will not have a type.
756 const DIType *FromTy = resolve(DTy->getBaseType());
757 if (FromTy)
758 addType(Buffer, FromTy);
760 // Add name if not anonymous or intermediate type.
761 if (!Name.empty())
762 addString(Buffer, dwarf::DW_AT_name, Name);
764 // Add size if non-zero (derived types might be zero-sized.)
765 if (Size && Tag != dwarf::DW_TAG_pointer_type
766 && Tag != dwarf::DW_TAG_ptr_to_member_type
767 && Tag != dwarf::DW_TAG_reference_type
768 && Tag != dwarf::DW_TAG_rvalue_reference_type)
769 addUInt(Buffer, dwarf::DW_AT_byte_size, None, Size);
771 if (Tag == dwarf::DW_TAG_ptr_to_member_type)
772 addDIEEntry(
773 Buffer, dwarf::DW_AT_containing_type,
774 *getOrCreateTypeDIE(resolve(cast<DIDerivedType>(DTy)->getClassType())));
775 // Add source line info if available and TyDesc is not a forward declaration.
776 if (!DTy->isForwardDecl())
777 addSourceLine(Buffer, DTy);
779 // If DWARF address space value is other than None, add it for pointer and
780 // reference types as DW_AT_address_class.
781 if (DTy->getDWARFAddressSpace() && (Tag == dwarf::DW_TAG_pointer_type ||
782 Tag == dwarf::DW_TAG_reference_type))
783 addUInt(Buffer, dwarf::DW_AT_address_class, dwarf::DW_FORM_data4,
784 DTy->getDWARFAddressSpace().getValue());
787 void DwarfUnit::constructSubprogramArguments(DIE &Buffer, DITypeRefArray Args) {
788 for (unsigned i = 1, N = Args.size(); i < N; ++i) {
789 const DIType *Ty = resolve(Args[i]);
790 if (!Ty) {
791 assert(i == N-1 && "Unspecified parameter must be the last argument");
792 createAndAddDIE(dwarf::DW_TAG_unspecified_parameters, Buffer);
793 } else {
794 DIE &Arg = createAndAddDIE(dwarf::DW_TAG_formal_parameter, Buffer);
795 addType(Arg, Ty);
796 if (Ty->isArtificial())
797 addFlag(Arg, dwarf::DW_AT_artificial);
802 void DwarfUnit::constructTypeDIE(DIE &Buffer, const DISubroutineType *CTy) {
803 // Add return type. A void return won't have a type.
804 auto Elements = cast<DISubroutineType>(CTy)->getTypeArray();
805 if (Elements.size())
806 if (auto RTy = resolve(Elements[0]))
807 addType(Buffer, RTy);
809 bool isPrototyped = true;
810 if (Elements.size() == 2 && !Elements[1])
811 isPrototyped = false;
813 constructSubprogramArguments(Buffer, Elements);
815 // Add prototype flag if we're dealing with a C language and the function has
816 // been prototyped.
817 uint16_t Language = getLanguage();
818 if (isPrototyped &&
819 (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 ||
820 Language == dwarf::DW_LANG_ObjC))
821 addFlag(Buffer, dwarf::DW_AT_prototyped);
823 // Add a DW_AT_calling_convention if this has an explicit convention.
824 if (CTy->getCC() && CTy->getCC() != dwarf::DW_CC_normal)
825 addUInt(Buffer, dwarf::DW_AT_calling_convention, dwarf::DW_FORM_data1,
826 CTy->getCC());
828 if (CTy->isLValueReference())
829 addFlag(Buffer, dwarf::DW_AT_reference);
831 if (CTy->isRValueReference())
832 addFlag(Buffer, dwarf::DW_AT_rvalue_reference);
835 void DwarfUnit::constructTypeDIE(DIE &Buffer, const DICompositeType *CTy) {
836 // Add name if not anonymous or intermediate type.
837 StringRef Name = CTy->getName();
839 uint64_t Size = CTy->getSizeInBits() >> 3;
840 uint16_t Tag = Buffer.getTag();
842 switch (Tag) {
843 case dwarf::DW_TAG_array_type:
844 constructArrayTypeDIE(Buffer, CTy);
845 break;
846 case dwarf::DW_TAG_enumeration_type:
847 constructEnumTypeDIE(Buffer, CTy);
848 break;
849 case dwarf::DW_TAG_variant_part:
850 case dwarf::DW_TAG_structure_type:
851 case dwarf::DW_TAG_union_type:
852 case dwarf::DW_TAG_class_type: {
853 // Emit the discriminator for a variant part.
854 DIDerivedType *Discriminator = nullptr;
855 if (Tag == dwarf::DW_TAG_variant_part) {
856 Discriminator = CTy->getDiscriminator();
857 if (Discriminator) {
858 // DWARF says:
859 // If the variant part has a discriminant, the discriminant is
860 // represented by a separate debugging information entry which is
861 // a child of the variant part entry.
862 DIE &DiscMember = constructMemberDIE(Buffer, Discriminator);
863 addDIEEntry(Buffer, dwarf::DW_AT_discr, DiscMember);
867 // Add elements to structure type.
868 DINodeArray Elements = CTy->getElements();
869 for (const auto *Element : Elements) {
870 if (!Element)
871 continue;
872 if (auto *SP = dyn_cast<DISubprogram>(Element))
873 getOrCreateSubprogramDIE(SP);
874 else if (auto *DDTy = dyn_cast<DIDerivedType>(Element)) {
875 if (DDTy->getTag() == dwarf::DW_TAG_friend) {
876 DIE &ElemDie = createAndAddDIE(dwarf::DW_TAG_friend, Buffer);
877 addType(ElemDie, resolve(DDTy->getBaseType()), dwarf::DW_AT_friend);
878 } else if (DDTy->isStaticMember()) {
879 getOrCreateStaticMemberDIE(DDTy);
880 } else if (Tag == dwarf::DW_TAG_variant_part) {
881 // When emitting a variant part, wrap each member in
882 // DW_TAG_variant.
883 DIE &Variant = createAndAddDIE(dwarf::DW_TAG_variant, Buffer);
884 if (const ConstantInt *CI =
885 dyn_cast_or_null<ConstantInt>(DDTy->getDiscriminantValue())) {
886 if (isUnsignedDIType(DD, resolve(Discriminator->getBaseType())))
887 addUInt(Variant, dwarf::DW_AT_discr_value, None, CI->getZExtValue());
888 else
889 addSInt(Variant, dwarf::DW_AT_discr_value, None, CI->getSExtValue());
891 constructMemberDIE(Variant, DDTy);
892 } else {
893 constructMemberDIE(Buffer, DDTy);
895 } else if (auto *Property = dyn_cast<DIObjCProperty>(Element)) {
896 DIE &ElemDie = createAndAddDIE(Property->getTag(), Buffer);
897 StringRef PropertyName = Property->getName();
898 addString(ElemDie, dwarf::DW_AT_APPLE_property_name, PropertyName);
899 if (Property->getType())
900 addType(ElemDie, resolve(Property->getType()));
901 addSourceLine(ElemDie, Property);
902 StringRef GetterName = Property->getGetterName();
903 if (!GetterName.empty())
904 addString(ElemDie, dwarf::DW_AT_APPLE_property_getter, GetterName);
905 StringRef SetterName = Property->getSetterName();
906 if (!SetterName.empty())
907 addString(ElemDie, dwarf::DW_AT_APPLE_property_setter, SetterName);
908 if (unsigned PropertyAttributes = Property->getAttributes())
909 addUInt(ElemDie, dwarf::DW_AT_APPLE_property_attribute, None,
910 PropertyAttributes);
911 } else if (auto *Composite = dyn_cast<DICompositeType>(Element)) {
912 if (Composite->getTag() == dwarf::DW_TAG_variant_part) {
913 DIE &VariantPart = createAndAddDIE(Composite->getTag(), Buffer);
914 constructTypeDIE(VariantPart, Composite);
919 if (CTy->isAppleBlockExtension())
920 addFlag(Buffer, dwarf::DW_AT_APPLE_block);
922 // This is outside the DWARF spec, but GDB expects a DW_AT_containing_type
923 // inside C++ composite types to point to the base class with the vtable.
924 // Rust uses DW_AT_containing_type to link a vtable to the type
925 // for which it was created.
926 if (auto *ContainingType = resolve(CTy->getVTableHolder()))
927 addDIEEntry(Buffer, dwarf::DW_AT_containing_type,
928 *getOrCreateTypeDIE(ContainingType));
930 if (CTy->isObjcClassComplete())
931 addFlag(Buffer, dwarf::DW_AT_APPLE_objc_complete_type);
933 // Add template parameters to a class, structure or union types.
934 // FIXME: The support isn't in the metadata for this yet.
935 if (Tag == dwarf::DW_TAG_class_type ||
936 Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type)
937 addTemplateParams(Buffer, CTy->getTemplateParams());
939 // Add the type's non-standard calling convention.
940 uint8_t CC = 0;
941 if (CTy->isTypePassByValue())
942 CC = dwarf::DW_CC_pass_by_value;
943 else if (CTy->isTypePassByReference())
944 CC = dwarf::DW_CC_pass_by_reference;
945 if (CC)
946 addUInt(Buffer, dwarf::DW_AT_calling_convention, dwarf::DW_FORM_data1,
947 CC);
948 break;
950 default:
951 break;
954 // Add name if not anonymous or intermediate type.
955 if (!Name.empty())
956 addString(Buffer, dwarf::DW_AT_name, Name);
958 if (Tag == dwarf::DW_TAG_enumeration_type ||
959 Tag == dwarf::DW_TAG_class_type || Tag == dwarf::DW_TAG_structure_type ||
960 Tag == dwarf::DW_TAG_union_type) {
961 // Add size if non-zero (derived types might be zero-sized.)
962 // TODO: Do we care about size for enum forward declarations?
963 if (Size)
964 addUInt(Buffer, dwarf::DW_AT_byte_size, None, Size);
965 else if (!CTy->isForwardDecl())
966 // Add zero size if it is not a forward declaration.
967 addUInt(Buffer, dwarf::DW_AT_byte_size, None, 0);
969 // If we're a forward decl, say so.
970 if (CTy->isForwardDecl())
971 addFlag(Buffer, dwarf::DW_AT_declaration);
973 // Add source line info if available.
974 if (!CTy->isForwardDecl())
975 addSourceLine(Buffer, CTy);
977 // No harm in adding the runtime language to the declaration.
978 unsigned RLang = CTy->getRuntimeLang();
979 if (RLang)
980 addUInt(Buffer, dwarf::DW_AT_APPLE_runtime_class, dwarf::DW_FORM_data1,
981 RLang);
983 // Add align info if available.
984 if (uint32_t AlignInBytes = CTy->getAlignInBytes())
985 addUInt(Buffer, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata,
986 AlignInBytes);
990 void DwarfUnit::constructTemplateTypeParameterDIE(
991 DIE &Buffer, const DITemplateTypeParameter *TP) {
992 DIE &ParamDIE =
993 createAndAddDIE(dwarf::DW_TAG_template_type_parameter, Buffer);
994 // Add the type if it exists, it could be void and therefore no type.
995 if (TP->getType())
996 addType(ParamDIE, resolve(TP->getType()));
997 if (!TP->getName().empty())
998 addString(ParamDIE, dwarf::DW_AT_name, TP->getName());
1001 void DwarfUnit::constructTemplateValueParameterDIE(
1002 DIE &Buffer, const DITemplateValueParameter *VP) {
1003 DIE &ParamDIE = createAndAddDIE(VP->getTag(), Buffer);
1005 // Add the type if there is one, template template and template parameter
1006 // packs will not have a type.
1007 if (VP->getTag() == dwarf::DW_TAG_template_value_parameter)
1008 addType(ParamDIE, resolve(VP->getType()));
1009 if (!VP->getName().empty())
1010 addString(ParamDIE, dwarf::DW_AT_name, VP->getName());
1011 if (Metadata *Val = VP->getValue()) {
1012 if (ConstantInt *CI = mdconst::dyn_extract<ConstantInt>(Val))
1013 addConstantValue(ParamDIE, CI, resolve(VP->getType()));
1014 else if (GlobalValue *GV = mdconst::dyn_extract<GlobalValue>(Val)) {
1015 // We cannot describe the location of dllimport'd entities: the
1016 // computation of their address requires loads from the IAT.
1017 if (!GV->hasDLLImportStorageClass()) {
1018 // For declaration non-type template parameters (such as global values
1019 // and functions)
1020 DIELoc *Loc = new (DIEValueAllocator) DIELoc;
1021 addOpAddress(*Loc, Asm->getSymbol(GV));
1022 // Emit DW_OP_stack_value to use the address as the immediate value of
1023 // the parameter, rather than a pointer to it.
1024 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_stack_value);
1025 addBlock(ParamDIE, dwarf::DW_AT_location, Loc);
1027 } else if (VP->getTag() == dwarf::DW_TAG_GNU_template_template_param) {
1028 assert(isa<MDString>(Val));
1029 addString(ParamDIE, dwarf::DW_AT_GNU_template_name,
1030 cast<MDString>(Val)->getString());
1031 } else if (VP->getTag() == dwarf::DW_TAG_GNU_template_parameter_pack) {
1032 addTemplateParams(ParamDIE, cast<MDTuple>(Val));
1037 DIE *DwarfUnit::getOrCreateNameSpace(const DINamespace *NS) {
1038 // Construct the context before querying for the existence of the DIE in case
1039 // such construction creates the DIE.
1040 DIE *ContextDIE = getOrCreateContextDIE(NS->getScope());
1042 if (DIE *NDie = getDIE(NS))
1043 return NDie;
1044 DIE &NDie = createAndAddDIE(dwarf::DW_TAG_namespace, *ContextDIE, NS);
1046 StringRef Name = NS->getName();
1047 if (!Name.empty())
1048 addString(NDie, dwarf::DW_AT_name, NS->getName());
1049 else
1050 Name = "(anonymous namespace)";
1051 DD->addAccelNamespace(*CUNode, Name, NDie);
1052 addGlobalName(Name, NDie, NS->getScope());
1053 if (NS->getExportSymbols())
1054 addFlag(NDie, dwarf::DW_AT_export_symbols);
1055 return &NDie;
1058 DIE *DwarfUnit::getOrCreateModule(const DIModule *M) {
1059 // Construct the context before querying for the existence of the DIE in case
1060 // such construction creates the DIE.
1061 DIE *ContextDIE = getOrCreateContextDIE(M->getScope());
1063 if (DIE *MDie = getDIE(M))
1064 return MDie;
1065 DIE &MDie = createAndAddDIE(dwarf::DW_TAG_module, *ContextDIE, M);
1067 if (!M->getName().empty()) {
1068 addString(MDie, dwarf::DW_AT_name, M->getName());
1069 addGlobalName(M->getName(), MDie, M->getScope());
1071 if (!M->getConfigurationMacros().empty())
1072 addString(MDie, dwarf::DW_AT_LLVM_config_macros,
1073 M->getConfigurationMacros());
1074 if (!M->getIncludePath().empty())
1075 addString(MDie, dwarf::DW_AT_LLVM_include_path, M->getIncludePath());
1076 if (!M->getISysRoot().empty())
1077 addString(MDie, dwarf::DW_AT_LLVM_isysroot, M->getISysRoot());
1079 return &MDie;
1082 DIE *DwarfUnit::getOrCreateSubprogramDIE(const DISubprogram *SP, bool Minimal) {
1083 // Construct the context before querying for the existence of the DIE in case
1084 // such construction creates the DIE (as is the case for member function
1085 // declarations).
1086 DIE *ContextDIE =
1087 Minimal ? &getUnitDie() : getOrCreateContextDIE(resolve(SP->getScope()));
1089 if (DIE *SPDie = getDIE(SP))
1090 return SPDie;
1092 if (auto *SPDecl = SP->getDeclaration()) {
1093 if (!Minimal) {
1094 // Add subprogram definitions to the CU die directly.
1095 ContextDIE = &getUnitDie();
1096 // Build the decl now to ensure it precedes the definition.
1097 getOrCreateSubprogramDIE(SPDecl);
1101 // DW_TAG_inlined_subroutine may refer to this DIE.
1102 DIE &SPDie = createAndAddDIE(dwarf::DW_TAG_subprogram, *ContextDIE, SP);
1104 // Stop here and fill this in later, depending on whether or not this
1105 // subprogram turns out to have inlined instances or not.
1106 if (SP->isDefinition())
1107 return &SPDie;
1109 applySubprogramAttributes(SP, SPDie);
1110 return &SPDie;
1113 bool DwarfUnit::applySubprogramDefinitionAttributes(const DISubprogram *SP,
1114 DIE &SPDie) {
1115 DIE *DeclDie = nullptr;
1116 StringRef DeclLinkageName;
1117 if (auto *SPDecl = SP->getDeclaration()) {
1118 DeclDie = getDIE(SPDecl);
1119 assert(DeclDie && "This DIE should've already been constructed when the "
1120 "definition DIE was created in "
1121 "getOrCreateSubprogramDIE");
1122 // Look at the Decl's linkage name only if we emitted it.
1123 if (DD->useAllLinkageNames())
1124 DeclLinkageName = SPDecl->getLinkageName();
1125 unsigned DeclID = getOrCreateSourceID(SPDecl->getFile());
1126 unsigned DefID = getOrCreateSourceID(SP->getFile());
1127 if (DeclID != DefID)
1128 addUInt(SPDie, dwarf::DW_AT_decl_file, None, DefID);
1130 if (SP->getLine() != SPDecl->getLine())
1131 addUInt(SPDie, dwarf::DW_AT_decl_line, None, SP->getLine());
1134 // Add function template parameters.
1135 addTemplateParams(SPDie, SP->getTemplateParams());
1137 // Add the linkage name if we have one and it isn't in the Decl.
1138 StringRef LinkageName = SP->getLinkageName();
1139 assert(((LinkageName.empty() || DeclLinkageName.empty()) ||
1140 LinkageName == DeclLinkageName) &&
1141 "decl has a linkage name and it is different");
1142 if (DeclLinkageName.empty() &&
1143 // Always emit it for abstract subprograms.
1144 (DD->useAllLinkageNames() || DU->getAbstractSPDies().lookup(SP)))
1145 addLinkageName(SPDie, LinkageName);
1147 if (!DeclDie)
1148 return false;
1150 // Refer to the function declaration where all the other attributes will be
1151 // found.
1152 addDIEEntry(SPDie, dwarf::DW_AT_specification, *DeclDie);
1153 return true;
1156 void DwarfUnit::applySubprogramAttributes(const DISubprogram *SP, DIE &SPDie,
1157 bool SkipSPAttributes) {
1158 // If -fdebug-info-for-profiling is enabled, need to emit the subprogram
1159 // and its source location.
1160 bool SkipSPSourceLocation = SkipSPAttributes &&
1161 !CUNode->getDebugInfoForProfiling();
1162 if (!SkipSPSourceLocation)
1163 if (applySubprogramDefinitionAttributes(SP, SPDie))
1164 return;
1166 // Constructors and operators for anonymous aggregates do not have names.
1167 if (!SP->getName().empty())
1168 addString(SPDie, dwarf::DW_AT_name, SP->getName());
1170 if (!SkipSPSourceLocation)
1171 addSourceLine(SPDie, SP);
1173 // Skip the rest of the attributes under -gmlt to save space.
1174 if (SkipSPAttributes)
1175 return;
1177 // Add the prototype if we have a prototype and we have a C like
1178 // language.
1179 uint16_t Language = getLanguage();
1180 if (SP->isPrototyped() &&
1181 (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 ||
1182 Language == dwarf::DW_LANG_ObjC))
1183 addFlag(SPDie, dwarf::DW_AT_prototyped);
1185 unsigned CC = 0;
1186 DITypeRefArray Args;
1187 if (const DISubroutineType *SPTy = SP->getType()) {
1188 Args = SPTy->getTypeArray();
1189 CC = SPTy->getCC();
1192 // Add a DW_AT_calling_convention if this has an explicit convention.
1193 if (CC && CC != dwarf::DW_CC_normal)
1194 addUInt(SPDie, dwarf::DW_AT_calling_convention, dwarf::DW_FORM_data1, CC);
1196 // Add a return type. If this is a type like a C/C++ void type we don't add a
1197 // return type.
1198 if (Args.size())
1199 if (auto Ty = resolve(Args[0]))
1200 addType(SPDie, Ty);
1202 unsigned VK = SP->getVirtuality();
1203 if (VK) {
1204 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, VK);
1205 if (SP->getVirtualIndex() != -1u) {
1206 DIELoc *Block = getDIELoc();
1207 addUInt(*Block, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1208 addUInt(*Block, dwarf::DW_FORM_udata, SP->getVirtualIndex());
1209 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, Block);
1211 ContainingTypeMap.insert(
1212 std::make_pair(&SPDie, resolve(SP->getContainingType())));
1215 if (!SP->isDefinition()) {
1216 addFlag(SPDie, dwarf::DW_AT_declaration);
1218 // Add arguments. Do not add arguments for subprogram definition. They will
1219 // be handled while processing variables.
1220 constructSubprogramArguments(SPDie, Args);
1223 addThrownTypes(SPDie, SP->getThrownTypes());
1225 if (SP->isArtificial())
1226 addFlag(SPDie, dwarf::DW_AT_artificial);
1228 if (!SP->isLocalToUnit())
1229 addFlag(SPDie, dwarf::DW_AT_external);
1231 if (DD->useAppleExtensionAttributes()) {
1232 if (SP->isOptimized())
1233 addFlag(SPDie, dwarf::DW_AT_APPLE_optimized);
1235 if (unsigned isa = Asm->getISAEncoding())
1236 addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa);
1239 if (SP->isLValueReference())
1240 addFlag(SPDie, dwarf::DW_AT_reference);
1242 if (SP->isRValueReference())
1243 addFlag(SPDie, dwarf::DW_AT_rvalue_reference);
1245 if (SP->isNoReturn())
1246 addFlag(SPDie, dwarf::DW_AT_noreturn);
1248 if (SP->isProtected())
1249 addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1250 dwarf::DW_ACCESS_protected);
1251 else if (SP->isPrivate())
1252 addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1253 dwarf::DW_ACCESS_private);
1254 else if (SP->isPublic())
1255 addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1256 dwarf::DW_ACCESS_public);
1258 if (SP->isExplicit())
1259 addFlag(SPDie, dwarf::DW_AT_explicit);
1261 if (SP->isMainSubprogram())
1262 addFlag(SPDie, dwarf::DW_AT_main_subprogram);
1265 void DwarfUnit::constructSubrangeDIE(DIE &Buffer, const DISubrange *SR,
1266 DIE *IndexTy) {
1267 DIE &DW_Subrange = createAndAddDIE(dwarf::DW_TAG_subrange_type, Buffer);
1268 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, *IndexTy);
1270 // The LowerBound value defines the lower bounds which is typically zero for
1271 // C/C++. The Count value is the number of elements. Values are 64 bit. If
1272 // Count == -1 then the array is unbounded and we do not emit
1273 // DW_AT_lower_bound and DW_AT_count attributes.
1274 int64_t LowerBound = SR->getLowerBound();
1275 int64_t DefaultLowerBound = getDefaultLowerBound();
1276 int64_t Count = -1;
1277 if (auto *CI = SR->getCount().dyn_cast<ConstantInt*>())
1278 Count = CI->getSExtValue();
1280 if (DefaultLowerBound == -1 || LowerBound != DefaultLowerBound)
1281 addUInt(DW_Subrange, dwarf::DW_AT_lower_bound, None, LowerBound);
1283 if (auto *CV = SR->getCount().dyn_cast<DIVariable*>()) {
1284 if (auto *CountVarDIE = getDIE(CV))
1285 addDIEEntry(DW_Subrange, dwarf::DW_AT_count, *CountVarDIE);
1286 } else if (Count != -1)
1287 addUInt(DW_Subrange, dwarf::DW_AT_count, None, Count);
1290 DIE *DwarfUnit::getIndexTyDie() {
1291 if (IndexTyDie)
1292 return IndexTyDie;
1293 // Construct an integer type to use for indexes.
1294 IndexTyDie = &createAndAddDIE(dwarf::DW_TAG_base_type, getUnitDie());
1295 StringRef Name = "__ARRAY_SIZE_TYPE__";
1296 addString(*IndexTyDie, dwarf::DW_AT_name, Name);
1297 addUInt(*IndexTyDie, dwarf::DW_AT_byte_size, None, sizeof(int64_t));
1298 addUInt(*IndexTyDie, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
1299 dwarf::DW_ATE_unsigned);
1300 DD->addAccelType(*CUNode, Name, *IndexTyDie, /*Flags*/ 0);
1301 return IndexTyDie;
1304 /// Returns true if the vector's size differs from the sum of sizes of elements
1305 /// the user specified. This can occur if the vector has been rounded up to
1306 /// fit memory alignment constraints.
1307 static bool hasVectorBeenPadded(const DICompositeType *CTy) {
1308 assert(CTy && CTy->isVector() && "Composite type is not a vector");
1309 const uint64_t ActualSize = CTy->getSizeInBits();
1311 // Obtain the size of each element in the vector.
1312 DIType *BaseTy = CTy->getBaseType().resolve();
1313 assert(BaseTy && "Unknown vector element type.");
1314 const uint64_t ElementSize = BaseTy->getSizeInBits();
1316 // Locate the number of elements in the vector.
1317 const DINodeArray Elements = CTy->getElements();
1318 assert(Elements.size() == 1 &&
1319 Elements[0]->getTag() == dwarf::DW_TAG_subrange_type &&
1320 "Invalid vector element array, expected one element of type subrange");
1321 const auto Subrange = cast<DISubrange>(Elements[0]);
1322 const auto CI = Subrange->getCount().get<ConstantInt *>();
1323 const int32_t NumVecElements = CI->getSExtValue();
1325 // Ensure we found the element count and that the actual size is wide
1326 // enough to contain the requested size.
1327 assert(ActualSize >= (NumVecElements * ElementSize) && "Invalid vector size");
1328 return ActualSize != (NumVecElements * ElementSize);
1331 void DwarfUnit::constructArrayTypeDIE(DIE &Buffer, const DICompositeType *CTy) {
1332 if (CTy->isVector()) {
1333 addFlag(Buffer, dwarf::DW_AT_GNU_vector);
1334 if (hasVectorBeenPadded(CTy))
1335 addUInt(Buffer, dwarf::DW_AT_byte_size, None,
1336 CTy->getSizeInBits() / CHAR_BIT);
1339 // Emit the element type.
1340 addType(Buffer, resolve(CTy->getBaseType()));
1342 // Get an anonymous type for index type.
1343 // FIXME: This type should be passed down from the front end
1344 // as different languages may have different sizes for indexes.
1345 DIE *IdxTy = getIndexTyDie();
1347 // Add subranges to array type.
1348 DINodeArray Elements = CTy->getElements();
1349 for (unsigned i = 0, N = Elements.size(); i < N; ++i) {
1350 // FIXME: Should this really be such a loose cast?
1351 if (auto *Element = dyn_cast_or_null<DINode>(Elements[i]))
1352 if (Element->getTag() == dwarf::DW_TAG_subrange_type)
1353 constructSubrangeDIE(Buffer, cast<DISubrange>(Element), IdxTy);
1357 void DwarfUnit::constructEnumTypeDIE(DIE &Buffer, const DICompositeType *CTy) {
1358 const DIType *DTy = resolve(CTy->getBaseType());
1359 bool IsUnsigned = DTy && isUnsignedDIType(DD, DTy);
1360 if (DTy) {
1361 if (DD->getDwarfVersion() >= 3)
1362 addType(Buffer, DTy);
1363 if (DD->getDwarfVersion() >= 4 && (CTy->getFlags() & DINode::FlagEnumClass))
1364 addFlag(Buffer, dwarf::DW_AT_enum_class);
1367 DINodeArray Elements = CTy->getElements();
1369 // Add enumerators to enumeration type.
1370 for (unsigned i = 0, N = Elements.size(); i < N; ++i) {
1371 auto *Enum = dyn_cast_or_null<DIEnumerator>(Elements[i]);
1372 if (Enum) {
1373 DIE &Enumerator = createAndAddDIE(dwarf::DW_TAG_enumerator, Buffer);
1374 StringRef Name = Enum->getName();
1375 addString(Enumerator, dwarf::DW_AT_name, Name);
1376 auto Value = static_cast<uint64_t>(Enum->getValue());
1377 addConstantValue(Enumerator, IsUnsigned, Value);
1382 void DwarfUnit::constructContainingTypeDIEs() {
1383 for (auto CI = ContainingTypeMap.begin(), CE = ContainingTypeMap.end();
1384 CI != CE; ++CI) {
1385 DIE &SPDie = *CI->first;
1386 const DINode *D = CI->second;
1387 if (!D)
1388 continue;
1389 DIE *NDie = getDIE(D);
1390 if (!NDie)
1391 continue;
1392 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, *NDie);
1396 DIE &DwarfUnit::constructMemberDIE(DIE &Buffer, const DIDerivedType *DT) {
1397 DIE &MemberDie = createAndAddDIE(DT->getTag(), Buffer);
1398 StringRef Name = DT->getName();
1399 if (!Name.empty())
1400 addString(MemberDie, dwarf::DW_AT_name, Name);
1402 if (DIType *Resolved = resolve(DT->getBaseType()))
1403 addType(MemberDie, Resolved);
1405 addSourceLine(MemberDie, DT);
1407 if (DT->getTag() == dwarf::DW_TAG_inheritance && DT->isVirtual()) {
1409 // For C++, virtual base classes are not at fixed offset. Use following
1410 // expression to extract appropriate offset from vtable.
1411 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1413 DIELoc *VBaseLocationDie = new (DIEValueAllocator) DIELoc;
1414 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1415 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1416 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1417 addUInt(*VBaseLocationDie, dwarf::DW_FORM_udata, DT->getOffsetInBits());
1418 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1419 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1420 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1422 addBlock(MemberDie, dwarf::DW_AT_data_member_location, VBaseLocationDie);
1423 } else {
1424 uint64_t Size = DT->getSizeInBits();
1425 uint64_t FieldSize = DD->getBaseTypeSize(DT);
1426 uint32_t AlignInBytes = DT->getAlignInBytes();
1427 uint64_t OffsetInBytes;
1429 bool IsBitfield = FieldSize && Size != FieldSize;
1430 if (IsBitfield) {
1431 // Handle bitfield, assume bytes are 8 bits.
1432 if (DD->useDWARF2Bitfields())
1433 addUInt(MemberDie, dwarf::DW_AT_byte_size, None, FieldSize/8);
1434 addUInt(MemberDie, dwarf::DW_AT_bit_size, None, Size);
1436 uint64_t Offset = DT->getOffsetInBits();
1437 // We can't use DT->getAlignInBits() here: AlignInBits for member type
1438 // is non-zero if and only if alignment was forced (e.g. _Alignas()),
1439 // which can't be done with bitfields. Thus we use FieldSize here.
1440 uint32_t AlignInBits = FieldSize;
1441 uint32_t AlignMask = ~(AlignInBits - 1);
1442 // The bits from the start of the storage unit to the start of the field.
1443 uint64_t StartBitOffset = Offset - (Offset & AlignMask);
1444 // The byte offset of the field's aligned storage unit inside the struct.
1445 OffsetInBytes = (Offset - StartBitOffset) / 8;
1447 if (DD->useDWARF2Bitfields()) {
1448 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1449 uint64_t FieldOffset = (HiMark - FieldSize);
1450 Offset -= FieldOffset;
1452 // Maybe we need to work from the other end.
1453 if (Asm->getDataLayout().isLittleEndian())
1454 Offset = FieldSize - (Offset + Size);
1456 addUInt(MemberDie, dwarf::DW_AT_bit_offset, None, Offset);
1457 OffsetInBytes = FieldOffset >> 3;
1458 } else {
1459 addUInt(MemberDie, dwarf::DW_AT_data_bit_offset, None, Offset);
1461 } else {
1462 // This is not a bitfield.
1463 OffsetInBytes = DT->getOffsetInBits() / 8;
1464 if (AlignInBytes)
1465 addUInt(MemberDie, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata,
1466 AlignInBytes);
1469 if (DD->getDwarfVersion() <= 2) {
1470 DIELoc *MemLocationDie = new (DIEValueAllocator) DIELoc;
1471 addUInt(*MemLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
1472 addUInt(*MemLocationDie, dwarf::DW_FORM_udata, OffsetInBytes);
1473 addBlock(MemberDie, dwarf::DW_AT_data_member_location, MemLocationDie);
1474 } else if (!IsBitfield || DD->useDWARF2Bitfields())
1475 addUInt(MemberDie, dwarf::DW_AT_data_member_location, None,
1476 OffsetInBytes);
1479 if (DT->isProtected())
1480 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1481 dwarf::DW_ACCESS_protected);
1482 else if (DT->isPrivate())
1483 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1484 dwarf::DW_ACCESS_private);
1485 // Otherwise C++ member and base classes are considered public.
1486 else if (DT->isPublic())
1487 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1488 dwarf::DW_ACCESS_public);
1489 if (DT->isVirtual())
1490 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1,
1491 dwarf::DW_VIRTUALITY_virtual);
1493 // Objective-C properties.
1494 if (DINode *PNode = DT->getObjCProperty())
1495 if (DIE *PDie = getDIE(PNode))
1496 MemberDie.addValue(DIEValueAllocator, dwarf::DW_AT_APPLE_property,
1497 dwarf::DW_FORM_ref4, DIEEntry(*PDie));
1499 if (DT->isArtificial())
1500 addFlag(MemberDie, dwarf::DW_AT_artificial);
1502 return MemberDie;
1505 DIE *DwarfUnit::getOrCreateStaticMemberDIE(const DIDerivedType *DT) {
1506 if (!DT)
1507 return nullptr;
1509 // Construct the context before querying for the existence of the DIE in case
1510 // such construction creates the DIE.
1511 DIE *ContextDIE = getOrCreateContextDIE(resolve(DT->getScope()));
1512 assert(dwarf::isType(ContextDIE->getTag()) &&
1513 "Static member should belong to a type.");
1515 if (DIE *StaticMemberDIE = getDIE(DT))
1516 return StaticMemberDIE;
1518 DIE &StaticMemberDIE = createAndAddDIE(DT->getTag(), *ContextDIE, DT);
1520 const DIType *Ty = resolve(DT->getBaseType());
1522 addString(StaticMemberDIE, dwarf::DW_AT_name, DT->getName());
1523 addType(StaticMemberDIE, Ty);
1524 addSourceLine(StaticMemberDIE, DT);
1525 addFlag(StaticMemberDIE, dwarf::DW_AT_external);
1526 addFlag(StaticMemberDIE, dwarf::DW_AT_declaration);
1528 // FIXME: We could omit private if the parent is a class_type, and
1529 // public if the parent is something else.
1530 if (DT->isProtected())
1531 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1532 dwarf::DW_ACCESS_protected);
1533 else if (DT->isPrivate())
1534 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1535 dwarf::DW_ACCESS_private);
1536 else if (DT->isPublic())
1537 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1538 dwarf::DW_ACCESS_public);
1540 if (const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(DT->getConstant()))
1541 addConstantValue(StaticMemberDIE, CI, Ty);
1542 if (const ConstantFP *CFP = dyn_cast_or_null<ConstantFP>(DT->getConstant()))
1543 addConstantFPValue(StaticMemberDIE, CFP);
1545 if (uint32_t AlignInBytes = DT->getAlignInBytes())
1546 addUInt(StaticMemberDIE, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata,
1547 AlignInBytes);
1549 return &StaticMemberDIE;
1552 void DwarfUnit::emitCommonHeader(bool UseOffsets, dwarf::UnitType UT) {
1553 // Emit size of content not including length itself
1554 Asm->OutStreamer->AddComment("Length of Unit");
1555 if (!DD->useSectionsAsReferences()) {
1556 StringRef Prefix = isDwoUnit() ? "debug_info_dwo_" : "debug_info_";
1557 MCSymbol *BeginLabel = Asm->createTempSymbol(Prefix + "start");
1558 EndLabel = Asm->createTempSymbol(Prefix + "end");
1559 Asm->EmitLabelDifference(EndLabel, BeginLabel, 4);
1560 Asm->OutStreamer->EmitLabel(BeginLabel);
1561 } else
1562 Asm->emitInt32(getHeaderSize() + getUnitDie().getSize());
1564 Asm->OutStreamer->AddComment("DWARF version number");
1565 unsigned Version = DD->getDwarfVersion();
1566 Asm->emitInt16(Version);
1568 // DWARF v5 reorders the address size and adds a unit type.
1569 if (Version >= 5) {
1570 Asm->OutStreamer->AddComment("DWARF Unit Type");
1571 Asm->emitInt8(UT);
1572 Asm->OutStreamer->AddComment("Address Size (in bytes)");
1573 Asm->emitInt8(Asm->MAI->getCodePointerSize());
1576 // We share one abbreviations table across all units so it's always at the
1577 // start of the section. Use a relocatable offset where needed to ensure
1578 // linking doesn't invalidate that offset.
1579 Asm->OutStreamer->AddComment("Offset Into Abbrev. Section");
1580 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
1581 if (UseOffsets)
1582 Asm->emitInt32(0);
1583 else
1584 Asm->emitDwarfSymbolReference(
1585 TLOF.getDwarfAbbrevSection()->getBeginSymbol(), false);
1587 if (Version <= 4) {
1588 Asm->OutStreamer->AddComment("Address Size (in bytes)");
1589 Asm->emitInt8(Asm->MAI->getCodePointerSize());
1593 void DwarfTypeUnit::emitHeader(bool UseOffsets) {
1594 DwarfUnit::emitCommonHeader(UseOffsets,
1595 DD->useSplitDwarf() ? dwarf::DW_UT_split_type
1596 : dwarf::DW_UT_type);
1597 Asm->OutStreamer->AddComment("Type Signature");
1598 Asm->OutStreamer->EmitIntValue(TypeSignature, sizeof(TypeSignature));
1599 Asm->OutStreamer->AddComment("Type DIE Offset");
1600 // In a skeleton type unit there is no type DIE so emit a zero offset.
1601 Asm->OutStreamer->EmitIntValue(Ty ? Ty->getOffset() : 0,
1602 sizeof(Ty->getOffset()));
1605 DIE::value_iterator
1606 DwarfUnit::addSectionDelta(DIE &Die, dwarf::Attribute Attribute,
1607 const MCSymbol *Hi, const MCSymbol *Lo) {
1608 return Die.addValue(DIEValueAllocator, Attribute,
1609 DD->getDwarfVersion() >= 4 ? dwarf::DW_FORM_sec_offset
1610 : dwarf::DW_FORM_data4,
1611 new (DIEValueAllocator) DIEDelta(Hi, Lo));
1614 DIE::value_iterator
1615 DwarfUnit::addSectionLabel(DIE &Die, dwarf::Attribute Attribute,
1616 const MCSymbol *Label, const MCSymbol *Sec) {
1617 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
1618 return addLabel(Die, Attribute,
1619 DD->getDwarfVersion() >= 4 ? dwarf::DW_FORM_sec_offset
1620 : dwarf::DW_FORM_data4,
1621 Label);
1622 return addSectionDelta(Die, Attribute, Label, Sec);
1625 bool DwarfTypeUnit::isDwoUnit() const {
1626 // Since there are no skeleton type units, all type units are dwo type units
1627 // when split DWARF is being used.
1628 return DD->useSplitDwarf();
1631 void DwarfTypeUnit::addGlobalName(StringRef Name, const DIE &Die,
1632 const DIScope *Context) {
1633 getCU().addGlobalNameForTypeUnit(Name, Context);
1636 void DwarfTypeUnit::addGlobalType(const DIType *Ty, const DIE &Die,
1637 const DIScope *Context) {
1638 getCU().addGlobalTypeUnitType(Ty, Context);
1641 const MCSymbol *DwarfUnit::getCrossSectionRelativeBaseAddress() const {
1642 if (!Asm->MAI->doesDwarfUseRelocationsAcrossSections())
1643 return nullptr;
1644 if (isDwoUnit())
1645 return nullptr;
1646 return getSection()->getBeginSymbol();
1649 void DwarfUnit::addStringOffsetsStart() {
1650 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
1651 addSectionLabel(getUnitDie(), dwarf::DW_AT_str_offsets_base,
1652 DU->getStringOffsetsStartSym(),
1653 TLOF.getDwarfStrOffSection()->getBeginSymbol());
1656 void DwarfUnit::addRnglistsBase() {
1657 assert(DD->getDwarfVersion() >= 5 &&
1658 "DW_AT_rnglists_base requires DWARF version 5 or later");
1659 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
1660 addSectionLabel(getUnitDie(), dwarf::DW_AT_rnglists_base,
1661 DU->getRnglistsTableBaseSym(),
1662 TLOF.getDwarfRnglistsSection()->getBeginSymbol());
1665 void DwarfUnit::addLoclistsBase() {
1666 assert(DD->getDwarfVersion() >= 5 &&
1667 "DW_AT_loclists_base requires DWARF version 5 or later");
1668 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
1669 addSectionLabel(getUnitDie(), dwarf::DW_AT_loclists_base,
1670 DU->getLoclistsTableBaseSym(),
1671 TLOF.getDwarfLoclistsSection()->getBeginSymbol());