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