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