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 unsigned 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 /// Return true if type encoding is unsigned. 438 static bool isUnsignedDIType(DwarfDebug *DD, const DIType *Ty) { 439 if (auto *CTy = dyn_cast<DICompositeType>(Ty)) { 440 // FIXME: Enums without a fixed underlying type have unknown signedness 441 // here, leading to incorrectly emitted constants. 442 if (CTy->getTag() == dwarf::DW_TAG_enumeration_type) 443 return false; 444 445 // (Pieces of) aggregate types that get hacked apart by SROA may be 446 // represented by a constant. Encode them as unsigned bytes. 447 return true; 448 } 449 450 if (auto *DTy = dyn_cast<DIDerivedType>(Ty)) { 451 dwarf::Tag T = (dwarf::Tag)Ty->getTag(); 452 // Encode pointer constants as unsigned bytes. This is used at least for 453 // null pointer constant emission. 454 // FIXME: reference and rvalue_reference /probably/ shouldn't be allowed 455 // here, but accept them for now due to a bug in SROA producing bogus 456 // dbg.values. 457 if (T == dwarf::DW_TAG_pointer_type || 458 T == dwarf::DW_TAG_ptr_to_member_type || 459 T == dwarf::DW_TAG_reference_type || 460 T == dwarf::DW_TAG_rvalue_reference_type) 461 return true; 462 assert(T == dwarf::DW_TAG_typedef || T == dwarf::DW_TAG_const_type || 463 T == dwarf::DW_TAG_volatile_type || 464 T == dwarf::DW_TAG_restrict_type || T == dwarf::DW_TAG_atomic_type); 465 assert(DTy->getBaseType() && "Expected valid base type"); 466 return isUnsignedDIType(DD, DTy->getBaseType()); 467 } 468 469 auto *BTy = cast<DIBasicType>(Ty); 470 unsigned Encoding = BTy->getEncoding(); 471 assert((Encoding == dwarf::DW_ATE_unsigned || 472 Encoding == dwarf::DW_ATE_unsigned_char || 473 Encoding == dwarf::DW_ATE_signed || 474 Encoding == dwarf::DW_ATE_signed_char || 475 Encoding == dwarf::DW_ATE_float || Encoding == dwarf::DW_ATE_UTF || 476 Encoding == dwarf::DW_ATE_boolean || 477 (Ty->getTag() == dwarf::DW_TAG_unspecified_type && 478 Ty->getName() == "decltype(nullptr)")) && 479 "Unsupported encoding"); 480 return Encoding == dwarf::DW_ATE_unsigned || 481 Encoding == dwarf::DW_ATE_unsigned_char || 482 Encoding == dwarf::DW_ATE_UTF || Encoding == dwarf::DW_ATE_boolean || 483 Ty->getTag() == dwarf::DW_TAG_unspecified_type; 484 } 485 486 void DwarfUnit::addConstantFPValue(DIE &Die, const MachineOperand &MO) { 487 assert(MO.isFPImm() && "Invalid machine operand!"); 488 DIEBlock *Block = new (DIEValueAllocator) DIEBlock; 489 APFloat FPImm = MO.getFPImm()->getValueAPF(); 490 491 // Get the raw data form of the floating point. 492 const APInt FltVal = FPImm.bitcastToAPInt(); 493 const char *FltPtr = (const char *)FltVal.getRawData(); 494 495 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte. 496 bool LittleEndian = Asm->getDataLayout().isLittleEndian(); 497 int Incr = (LittleEndian ? 1 : -1); 498 int Start = (LittleEndian ? 0 : NumBytes - 1); 499 int Stop = (LittleEndian ? NumBytes : -1); 500 501 // Output the constant to DWARF one byte at a time. 502 for (; Start != Stop; Start += Incr) 503 addUInt(*Block, dwarf::DW_FORM_data1, (unsigned char)0xFF & FltPtr[Start]); 504 505 addBlock(Die, dwarf::DW_AT_const_value, Block); 506 } 507 508 void DwarfUnit::addConstantFPValue(DIE &Die, const ConstantFP *CFP) { 509 // Pass this down to addConstantValue as an unsigned bag of bits. 510 addConstantValue(Die, CFP->getValueAPF().bitcastToAPInt(), true); 511 } 512 513 void DwarfUnit::addConstantValue(DIE &Die, const ConstantInt *CI, 514 const DIType *Ty) { 515 addConstantValue(Die, CI->getValue(), Ty); 516 } 517 518 void DwarfUnit::addConstantValue(DIE &Die, const MachineOperand &MO, 519 const DIType *Ty) { 520 assert(MO.isImm() && "Invalid machine operand!"); 521 522 addConstantValue(Die, isUnsignedDIType(DD, Ty), MO.getImm()); 523 } 524 525 void DwarfUnit::addConstantValue(DIE &Die, uint64_t Val, const DIType *Ty) { 526 addConstantValue(Die, isUnsignedDIType(DD, Ty), Val); 527 } 528 529 void DwarfUnit::addConstantValue(DIE &Die, bool Unsigned, uint64_t Val) { 530 // FIXME: This is a bit conservative/simple - it emits negative values always 531 // sign extended to 64 bits rather than minimizing the number of bytes. 532 addUInt(Die, dwarf::DW_AT_const_value, 533 Unsigned ? dwarf::DW_FORM_udata : dwarf::DW_FORM_sdata, Val); 534 } 535 536 void DwarfUnit::addConstantValue(DIE &Die, const APInt &Val, const DIType *Ty) { 537 addConstantValue(Die, Val, isUnsignedDIType(DD, Ty)); 538 } 539 540 void DwarfUnit::addConstantValue(DIE &Die, const APInt &Val, bool Unsigned) { 541 unsigned CIBitWidth = Val.getBitWidth(); 542 if (CIBitWidth <= 64) { 543 addConstantValue(Die, Unsigned, 544 Unsigned ? Val.getZExtValue() : Val.getSExtValue()); 545 return; 546 } 547 548 DIEBlock *Block = new (DIEValueAllocator) DIEBlock; 549 550 // Get the raw data form of the large APInt. 551 const uint64_t *Ptr64 = Val.getRawData(); 552 553 int NumBytes = Val.getBitWidth() / 8; // 8 bits per byte. 554 bool LittleEndian = Asm->getDataLayout().isLittleEndian(); 555 556 // Output the constant to DWARF one byte at a time. 557 for (int i = 0; i < NumBytes; i++) { 558 uint8_t c; 559 if (LittleEndian) 560 c = Ptr64[i / 8] >> (8 * (i & 7)); 561 else 562 c = Ptr64[(NumBytes - 1 - i) / 8] >> (8 * ((NumBytes - 1 - i) & 7)); 563 addUInt(*Block, dwarf::DW_FORM_data1, c); 564 } 565 566 addBlock(Die, dwarf::DW_AT_const_value, Block); 567 } 568 569 void DwarfUnit::addLinkageName(DIE &Die, StringRef LinkageName) { 570 if (!LinkageName.empty()) 571 addString(Die, 572 DD->getDwarfVersion() >= 4 ? dwarf::DW_AT_linkage_name 573 : dwarf::DW_AT_MIPS_linkage_name, 574 GlobalValue::dropLLVMManglingEscape(LinkageName)); 575 } 576 577 void DwarfUnit::addTemplateParams(DIE &Buffer, DINodeArray TParams) { 578 // Add template parameters. 579 for (const auto *Element : TParams) { 580 if (auto *TTP = dyn_cast<DITemplateTypeParameter>(Element)) 581 constructTemplateTypeParameterDIE(Buffer, TTP); 582 else if (auto *TVP = dyn_cast<DITemplateValueParameter>(Element)) 583 constructTemplateValueParameterDIE(Buffer, TVP); 584 } 585 } 586 587 /// Add thrown types. 588 void DwarfUnit::addThrownTypes(DIE &Die, DINodeArray ThrownTypes) { 589 for (const auto *Ty : ThrownTypes) { 590 DIE &TT = createAndAddDIE(dwarf::DW_TAG_thrown_type, Die); 591 addType(TT, cast<DIType>(Ty)); 592 } 593 } 594 595 DIE *DwarfUnit::getOrCreateContextDIE(const DIScope *Context) { 596 if (!Context || isa<DIFile>(Context)) 597 return &getUnitDie(); 598 if (auto *T = dyn_cast<DIType>(Context)) 599 return getOrCreateTypeDIE(T); 600 if (auto *NS = dyn_cast<DINamespace>(Context)) 601 return getOrCreateNameSpace(NS); 602 if (auto *SP = dyn_cast<DISubprogram>(Context)) 603 return getOrCreateSubprogramDIE(SP); 604 if (auto *M = dyn_cast<DIModule>(Context)) 605 return getOrCreateModule(M); 606 return getDIE(Context); 607 } 608 609 DIE *DwarfUnit::createTypeDIE(const DICompositeType *Ty) { 610 auto *Context = Ty->getScope(); 611 DIE *ContextDIE = getOrCreateContextDIE(Context); 612 613 if (DIE *TyDIE = getDIE(Ty)) 614 return TyDIE; 615 616 // Create new type. 617 DIE &TyDIE = createAndAddDIE(Ty->getTag(), *ContextDIE, Ty); 618 619 constructTypeDIE(TyDIE, cast<DICompositeType>(Ty)); 620 621 updateAcceleratorTables(Context, Ty, TyDIE); 622 return &TyDIE; 623 } 624 625 DIE *DwarfUnit::createTypeDIE(const DIScope *Context, DIE &ContextDIE, 626 const DIType *Ty) { 627 // Create new type. 628 DIE &TyDIE = createAndAddDIE(Ty->getTag(), ContextDIE, Ty); 629 630 updateAcceleratorTables(Context, Ty, TyDIE); 631 632 if (auto *BT = dyn_cast<DIBasicType>(Ty)) 633 constructTypeDIE(TyDIE, BT); 634 else if (auto *ST = dyn_cast<DIStringType>(Ty)) 635 constructTypeDIE(TyDIE, ST); 636 else if (auto *STy = dyn_cast<DISubroutineType>(Ty)) 637 constructTypeDIE(TyDIE, STy); 638 else if (auto *CTy = dyn_cast<DICompositeType>(Ty)) { 639 if (DD->generateTypeUnits() && !Ty->isForwardDecl() && 640 (Ty->getRawName() || CTy->getRawIdentifier())) { 641 // Skip updating the accelerator tables since this is not the full type. 642 if (MDString *TypeId = CTy->getRawIdentifier()) 643 DD->addDwarfTypeUnitType(getCU(), TypeId->getString(), TyDIE, CTy); 644 else { 645 auto X = DD->enterNonTypeUnitContext(); 646 finishNonUnitTypeDIE(TyDIE, CTy); 647 } 648 return &TyDIE; 649 } 650 constructTypeDIE(TyDIE, CTy); 651 } else { 652 constructTypeDIE(TyDIE, cast<DIDerivedType>(Ty)); 653 } 654 655 return &TyDIE; 656 } 657 658 DIE *DwarfUnit::getOrCreateTypeDIE(const MDNode *TyNode) { 659 if (!TyNode) 660 return nullptr; 661 662 auto *Ty = cast<DIType>(TyNode); 663 664 // DW_TAG_restrict_type is not supported in DWARF2 665 if (Ty->getTag() == dwarf::DW_TAG_restrict_type && DD->getDwarfVersion() <= 2) 666 return getOrCreateTypeDIE(cast<DIDerivedType>(Ty)->getBaseType()); 667 668 // DW_TAG_atomic_type is not supported in DWARF < 5 669 if (Ty->getTag() == dwarf::DW_TAG_atomic_type && DD->getDwarfVersion() < 5) 670 return getOrCreateTypeDIE(cast<DIDerivedType>(Ty)->getBaseType()); 671 672 // Construct the context before querying for the existence of the DIE in case 673 // such construction creates the DIE. 674 auto *Context = Ty->getScope(); 675 DIE *ContextDIE = getOrCreateContextDIE(Context); 676 assert(ContextDIE); 677 678 if (DIE *TyDIE = getDIE(Ty)) 679 return TyDIE; 680 681 return static_cast<DwarfUnit *>(ContextDIE->getUnit()) 682 ->createTypeDIE(Context, *ContextDIE, Ty); 683 } 684 685 void DwarfUnit::updateAcceleratorTables(const DIScope *Context, 686 const DIType *Ty, const DIE &TyDIE) { 687 if (!Ty->getName().empty() && !Ty->isForwardDecl()) { 688 bool IsImplementation = false; 689 if (auto *CT = dyn_cast<DICompositeType>(Ty)) { 690 // A runtime language of 0 actually means C/C++ and that any 691 // non-negative value is some version of Objective-C/C++. 692 IsImplementation = CT->getRuntimeLang() == 0 || CT->isObjcClassComplete(); 693 } 694 unsigned Flags = IsImplementation ? dwarf::DW_FLAG_type_implementation : 0; 695 DD->addAccelType(*CUNode, Ty->getName(), TyDIE, Flags); 696 697 if (!Context || isa<DICompileUnit>(Context) || isa<DIFile>(Context) || 698 isa<DINamespace>(Context) || isa<DICommonBlock>(Context)) 699 addGlobalType(Ty, TyDIE, Context); 700 } 701 } 702 703 void DwarfUnit::addType(DIE &Entity, const DIType *Ty, 704 dwarf::Attribute Attribute) { 705 assert(Ty && "Trying to add a type that doesn't exist?"); 706 addDIEEntry(Entity, Attribute, DIEEntry(*getOrCreateTypeDIE(Ty))); 707 } 708 709 std::string DwarfUnit::getParentContextString(const DIScope *Context) const { 710 if (!Context) 711 return ""; 712 713 // FIXME: Decide whether to implement this for non-C++ languages. 714 if (!dwarf::isCPlusPlus((dwarf::SourceLanguage)getLanguage())) 715 return ""; 716 717 std::string CS; 718 SmallVector<const DIScope *, 1> Parents; 719 while (!isa<DICompileUnit>(Context)) { 720 Parents.push_back(Context); 721 if (const DIScope *S = Context->getScope()) 722 Context = S; 723 else 724 // Structure, etc types will have a NULL context if they're at the top 725 // level. 726 break; 727 } 728 729 // Reverse iterate over our list to go from the outermost construct to the 730 // innermost. 731 for (const DIScope *Ctx : make_range(Parents.rbegin(), Parents.rend())) { 732 StringRef Name = Ctx->getName(); 733 if (Name.empty() && isa<DINamespace>(Ctx)) 734 Name = "(anonymous namespace)"; 735 if (!Name.empty()) { 736 CS += Name; 737 CS += "::"; 738 } 739 } 740 return CS; 741 } 742 743 void DwarfUnit::constructTypeDIE(DIE &Buffer, const DIBasicType *BTy) { 744 // Get core information. 745 StringRef Name = BTy->getName(); 746 // Add name if not anonymous or intermediate type. 747 if (!Name.empty()) 748 addString(Buffer, dwarf::DW_AT_name, Name); 749 750 // An unspecified type only has a name attribute. 751 if (BTy->getTag() == dwarf::DW_TAG_unspecified_type) 752 return; 753 754 if (BTy->getTag() != dwarf::DW_TAG_string_type) 755 addUInt(Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1, 756 BTy->getEncoding()); 757 758 uint64_t Size = BTy->getSizeInBits() >> 3; 759 addUInt(Buffer, dwarf::DW_AT_byte_size, None, Size); 760 761 if (BTy->isBigEndian()) 762 addUInt(Buffer, dwarf::DW_AT_endianity, None, dwarf::DW_END_big); 763 else if (BTy->isLittleEndian()) 764 addUInt(Buffer, dwarf::DW_AT_endianity, None, dwarf::DW_END_little); 765 } 766 767 void DwarfUnit::constructTypeDIE(DIE &Buffer, const DIStringType *STy) { 768 // Get core information. 769 StringRef Name = STy->getName(); 770 // Add name if not anonymous or intermediate type. 771 if (!Name.empty()) 772 addString(Buffer, dwarf::DW_AT_name, Name); 773 774 if (DIVariable *Var = STy->getStringLength()) { 775 if (auto *VarDIE = getDIE(Var)) 776 addDIEEntry(Buffer, dwarf::DW_AT_string_length, *VarDIE); 777 } else { 778 uint64_t Size = STy->getSizeInBits() >> 3; 779 addUInt(Buffer, dwarf::DW_AT_byte_size, None, Size); 780 } 781 782 if (STy->getEncoding()) { 783 // For eventual Unicode support. 784 addUInt(Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1, 785 STy->getEncoding()); 786 } 787 } 788 789 void DwarfUnit::constructTypeDIE(DIE &Buffer, const DIDerivedType *DTy) { 790 // Get core information. 791 StringRef Name = DTy->getName(); 792 uint64_t Size = DTy->getSizeInBits() >> 3; 793 uint16_t Tag = Buffer.getTag(); 794 795 // Map to main type, void will not have a type. 796 const DIType *FromTy = DTy->getBaseType(); 797 if (FromTy) 798 addType(Buffer, FromTy); 799 800 // Add name if not anonymous or intermediate type. 801 if (!Name.empty()) 802 addString(Buffer, dwarf::DW_AT_name, Name); 803 804 // If alignment is specified for a typedef , create and insert DW_AT_alignment 805 // attribute in DW_TAG_typedef DIE. 806 if (Tag == dwarf::DW_TAG_typedef && DD->getDwarfVersion() >= 5) { 807 uint32_t AlignInBytes = DTy->getAlignInBytes(); 808 if (AlignInBytes > 0) 809 addUInt(Buffer, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata, 810 AlignInBytes); 811 } 812 813 // Add size if non-zero (derived types might be zero-sized.) 814 if (Size && Tag != dwarf::DW_TAG_pointer_type 815 && Tag != dwarf::DW_TAG_ptr_to_member_type 816 && Tag != dwarf::DW_TAG_reference_type 817 && Tag != dwarf::DW_TAG_rvalue_reference_type) 818 addUInt(Buffer, dwarf::DW_AT_byte_size, None, Size); 819 820 if (Tag == dwarf::DW_TAG_ptr_to_member_type) 821 addDIEEntry(Buffer, dwarf::DW_AT_containing_type, 822 *getOrCreateTypeDIE(cast<DIDerivedType>(DTy)->getClassType())); 823 // Add source line info if available and TyDesc is not a forward declaration. 824 if (!DTy->isForwardDecl()) 825 addSourceLine(Buffer, DTy); 826 827 // If DWARF address space value is other than None, add it. The IR 828 // verifier checks that DWARF address space only exists for pointer 829 // or reference types. 830 if (DTy->getDWARFAddressSpace()) 831 addUInt(Buffer, dwarf::DW_AT_address_class, dwarf::DW_FORM_data4, 832 DTy->getDWARFAddressSpace().getValue()); 833 } 834 835 void DwarfUnit::constructSubprogramArguments(DIE &Buffer, DITypeRefArray Args) { 836 for (unsigned i = 1, N = Args.size(); i < N; ++i) { 837 const DIType *Ty = Args[i]; 838 if (!Ty) { 839 assert(i == N-1 && "Unspecified parameter must be the last argument"); 840 createAndAddDIE(dwarf::DW_TAG_unspecified_parameters, Buffer); 841 } else { 842 DIE &Arg = createAndAddDIE(dwarf::DW_TAG_formal_parameter, Buffer); 843 addType(Arg, Ty); 844 if (Ty->isArtificial()) 845 addFlag(Arg, dwarf::DW_AT_artificial); 846 } 847 } 848 } 849 850 void DwarfUnit::constructTypeDIE(DIE &Buffer, const DISubroutineType *CTy) { 851 // Add return type. A void return won't have a type. 852 auto Elements = cast<DISubroutineType>(CTy)->getTypeArray(); 853 if (Elements.size()) 854 if (auto RTy = Elements[0]) 855 addType(Buffer, RTy); 856 857 bool isPrototyped = true; 858 if (Elements.size() == 2 && !Elements[1]) 859 isPrototyped = false; 860 861 constructSubprogramArguments(Buffer, Elements); 862 863 // Add prototype flag if we're dealing with a C language and the function has 864 // been prototyped. 865 uint16_t Language = getLanguage(); 866 if (isPrototyped && 867 (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 || 868 Language == dwarf::DW_LANG_ObjC)) 869 addFlag(Buffer, dwarf::DW_AT_prototyped); 870 871 // Add a DW_AT_calling_convention if this has an explicit convention. 872 if (CTy->getCC() && CTy->getCC() != dwarf::DW_CC_normal) 873 addUInt(Buffer, dwarf::DW_AT_calling_convention, dwarf::DW_FORM_data1, 874 CTy->getCC()); 875 876 if (CTy->isLValueReference()) 877 addFlag(Buffer, dwarf::DW_AT_reference); 878 879 if (CTy->isRValueReference()) 880 addFlag(Buffer, dwarf::DW_AT_rvalue_reference); 881 } 882 883 void DwarfUnit::constructTypeDIE(DIE &Buffer, const DICompositeType *CTy) { 884 // Add name if not anonymous or intermediate type. 885 StringRef Name = CTy->getName(); 886 887 uint64_t Size = CTy->getSizeInBits() >> 3; 888 uint16_t Tag = Buffer.getTag(); 889 890 switch (Tag) { 891 case dwarf::DW_TAG_array_type: 892 constructArrayTypeDIE(Buffer, CTy); 893 break; 894 case dwarf::DW_TAG_enumeration_type: 895 constructEnumTypeDIE(Buffer, CTy); 896 break; 897 case dwarf::DW_TAG_variant_part: 898 case dwarf::DW_TAG_structure_type: 899 case dwarf::DW_TAG_union_type: 900 case dwarf::DW_TAG_class_type: { 901 // Emit the discriminator for a variant part. 902 DIDerivedType *Discriminator = nullptr; 903 if (Tag == dwarf::DW_TAG_variant_part) { 904 Discriminator = CTy->getDiscriminator(); 905 if (Discriminator) { 906 // DWARF says: 907 // If the variant part has a discriminant, the discriminant is 908 // represented by a separate debugging information entry which is 909 // a child of the variant part entry. 910 DIE &DiscMember = constructMemberDIE(Buffer, Discriminator); 911 addDIEEntry(Buffer, dwarf::DW_AT_discr, DiscMember); 912 } 913 } 914 915 // Add template parameters to a class, structure or union types. 916 if (Tag == dwarf::DW_TAG_class_type || 917 Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type) 918 addTemplateParams(Buffer, CTy->getTemplateParams()); 919 920 // Add elements to structure type. 921 DINodeArray Elements = CTy->getElements(); 922 for (const auto *Element : Elements) { 923 if (!Element) 924 continue; 925 if (auto *SP = dyn_cast<DISubprogram>(Element)) 926 getOrCreateSubprogramDIE(SP); 927 else if (auto *DDTy = dyn_cast<DIDerivedType>(Element)) { 928 if (DDTy->getTag() == dwarf::DW_TAG_friend) { 929 DIE &ElemDie = createAndAddDIE(dwarf::DW_TAG_friend, Buffer); 930 addType(ElemDie, DDTy->getBaseType(), dwarf::DW_AT_friend); 931 } else if (DDTy->isStaticMember()) { 932 getOrCreateStaticMemberDIE(DDTy); 933 } else if (Tag == dwarf::DW_TAG_variant_part) { 934 // When emitting a variant part, wrap each member in 935 // DW_TAG_variant. 936 DIE &Variant = createAndAddDIE(dwarf::DW_TAG_variant, Buffer); 937 if (const ConstantInt *CI = 938 dyn_cast_or_null<ConstantInt>(DDTy->getDiscriminantValue())) { 939 if (isUnsignedDIType(DD, Discriminator->getBaseType())) 940 addUInt(Variant, dwarf::DW_AT_discr_value, None, CI->getZExtValue()); 941 else 942 addSInt(Variant, dwarf::DW_AT_discr_value, None, CI->getSExtValue()); 943 } 944 constructMemberDIE(Variant, DDTy); 945 } else { 946 constructMemberDIE(Buffer, DDTy); 947 } 948 } else if (auto *Property = dyn_cast<DIObjCProperty>(Element)) { 949 DIE &ElemDie = createAndAddDIE(Property->getTag(), Buffer); 950 StringRef PropertyName = Property->getName(); 951 addString(ElemDie, dwarf::DW_AT_APPLE_property_name, PropertyName); 952 if (Property->getType()) 953 addType(ElemDie, Property->getType()); 954 addSourceLine(ElemDie, Property); 955 StringRef GetterName = Property->getGetterName(); 956 if (!GetterName.empty()) 957 addString(ElemDie, dwarf::DW_AT_APPLE_property_getter, GetterName); 958 StringRef SetterName = Property->getSetterName(); 959 if (!SetterName.empty()) 960 addString(ElemDie, dwarf::DW_AT_APPLE_property_setter, SetterName); 961 if (unsigned PropertyAttributes = Property->getAttributes()) 962 addUInt(ElemDie, dwarf::DW_AT_APPLE_property_attribute, None, 963 PropertyAttributes); 964 } else if (auto *Composite = dyn_cast<DICompositeType>(Element)) { 965 if (Composite->getTag() == dwarf::DW_TAG_variant_part) { 966 DIE &VariantPart = createAndAddDIE(Composite->getTag(), Buffer); 967 constructTypeDIE(VariantPart, Composite); 968 } 969 } 970 } 971 972 if (CTy->isAppleBlockExtension()) 973 addFlag(Buffer, dwarf::DW_AT_APPLE_block); 974 975 if (CTy->getExportSymbols()) 976 addFlag(Buffer, dwarf::DW_AT_export_symbols); 977 978 // This is outside the DWARF spec, but GDB expects a DW_AT_containing_type 979 // inside C++ composite types to point to the base class with the vtable. 980 // Rust uses DW_AT_containing_type to link a vtable to the type 981 // for which it was created. 982 if (auto *ContainingType = CTy->getVTableHolder()) 983 addDIEEntry(Buffer, dwarf::DW_AT_containing_type, 984 *getOrCreateTypeDIE(ContainingType)); 985 986 if (CTy->isObjcClassComplete()) 987 addFlag(Buffer, dwarf::DW_AT_APPLE_objc_complete_type); 988 989 // Add the type's non-standard calling convention. 990 uint8_t CC = 0; 991 if (CTy->isTypePassByValue()) 992 CC = dwarf::DW_CC_pass_by_value; 993 else if (CTy->isTypePassByReference()) 994 CC = dwarf::DW_CC_pass_by_reference; 995 if (CC) 996 addUInt(Buffer, dwarf::DW_AT_calling_convention, dwarf::DW_FORM_data1, 997 CC); 998 break; 999 } 1000 default: 1001 break; 1002 } 1003 1004 // Add name if not anonymous or intermediate type. 1005 if (!Name.empty()) 1006 addString(Buffer, dwarf::DW_AT_name, Name); 1007 1008 if (Tag == dwarf::DW_TAG_enumeration_type || 1009 Tag == dwarf::DW_TAG_class_type || Tag == dwarf::DW_TAG_structure_type || 1010 Tag == dwarf::DW_TAG_union_type) { 1011 // Add size if non-zero (derived types might be zero-sized.) 1012 // Ignore the size if it's a non-enum forward decl. 1013 // TODO: Do we care about size for enum forward declarations? 1014 if (Size && 1015 (!CTy->isForwardDecl() || Tag == dwarf::DW_TAG_enumeration_type)) 1016 addUInt(Buffer, dwarf::DW_AT_byte_size, None, Size); 1017 else if (!CTy->isForwardDecl()) 1018 // Add zero size if it is not a forward declaration. 1019 addUInt(Buffer, dwarf::DW_AT_byte_size, None, 0); 1020 1021 // If we're a forward decl, say so. 1022 if (CTy->isForwardDecl()) 1023 addFlag(Buffer, dwarf::DW_AT_declaration); 1024 1025 // Add source line info if available. 1026 if (!CTy->isForwardDecl()) 1027 addSourceLine(Buffer, CTy); 1028 1029 // No harm in adding the runtime language to the declaration. 1030 unsigned RLang = CTy->getRuntimeLang(); 1031 if (RLang) 1032 addUInt(Buffer, dwarf::DW_AT_APPLE_runtime_class, dwarf::DW_FORM_data1, 1033 RLang); 1034 1035 // Add align info if available. 1036 if (uint32_t AlignInBytes = CTy->getAlignInBytes()) 1037 addUInt(Buffer, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata, 1038 AlignInBytes); 1039 } 1040 } 1041 1042 void DwarfUnit::constructTemplateTypeParameterDIE( 1043 DIE &Buffer, const DITemplateTypeParameter *TP) { 1044 DIE &ParamDIE = 1045 createAndAddDIE(dwarf::DW_TAG_template_type_parameter, Buffer); 1046 // Add the type if it exists, it could be void and therefore no type. 1047 if (TP->getType()) 1048 addType(ParamDIE, TP->getType()); 1049 if (!TP->getName().empty()) 1050 addString(ParamDIE, dwarf::DW_AT_name, TP->getName()); 1051 if (TP->isDefault() && (DD->getDwarfVersion() >= 5)) 1052 addFlag(ParamDIE, dwarf::DW_AT_default_value); 1053 } 1054 1055 void DwarfUnit::constructTemplateValueParameterDIE( 1056 DIE &Buffer, const DITemplateValueParameter *VP) { 1057 DIE &ParamDIE = createAndAddDIE(VP->getTag(), Buffer); 1058 1059 // Add the type if there is one, template template and template parameter 1060 // packs will not have a type. 1061 if (VP->getTag() == dwarf::DW_TAG_template_value_parameter) 1062 addType(ParamDIE, VP->getType()); 1063 if (!VP->getName().empty()) 1064 addString(ParamDIE, dwarf::DW_AT_name, VP->getName()); 1065 if (VP->isDefault() && (DD->getDwarfVersion() >= 5)) 1066 addFlag(ParamDIE, dwarf::DW_AT_default_value); 1067 if (Metadata *Val = VP->getValue()) { 1068 if (ConstantInt *CI = mdconst::dyn_extract<ConstantInt>(Val)) 1069 addConstantValue(ParamDIE, CI, VP->getType()); 1070 else if (GlobalValue *GV = mdconst::dyn_extract<GlobalValue>(Val)) { 1071 // We cannot describe the location of dllimport'd entities: the 1072 // computation of their address requires loads from the IAT. 1073 if (!GV->hasDLLImportStorageClass()) { 1074 // For declaration non-type template parameters (such as global values 1075 // and functions) 1076 DIELoc *Loc = new (DIEValueAllocator) DIELoc; 1077 addOpAddress(*Loc, Asm->getSymbol(GV)); 1078 // Emit DW_OP_stack_value to use the address as the immediate value of 1079 // the parameter, rather than a pointer to it. 1080 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_stack_value); 1081 addBlock(ParamDIE, dwarf::DW_AT_location, Loc); 1082 } 1083 } else if (VP->getTag() == dwarf::DW_TAG_GNU_template_template_param) { 1084 assert(isa<MDString>(Val)); 1085 addString(ParamDIE, dwarf::DW_AT_GNU_template_name, 1086 cast<MDString>(Val)->getString()); 1087 } else if (VP->getTag() == dwarf::DW_TAG_GNU_template_parameter_pack) { 1088 addTemplateParams(ParamDIE, cast<MDTuple>(Val)); 1089 } 1090 } 1091 } 1092 1093 DIE *DwarfUnit::getOrCreateNameSpace(const DINamespace *NS) { 1094 // Construct the context before querying for the existence of the DIE in case 1095 // such construction creates the DIE. 1096 DIE *ContextDIE = getOrCreateContextDIE(NS->getScope()); 1097 1098 if (DIE *NDie = getDIE(NS)) 1099 return NDie; 1100 DIE &NDie = createAndAddDIE(dwarf::DW_TAG_namespace, *ContextDIE, NS); 1101 1102 StringRef Name = NS->getName(); 1103 if (!Name.empty()) 1104 addString(NDie, dwarf::DW_AT_name, NS->getName()); 1105 else 1106 Name = "(anonymous namespace)"; 1107 DD->addAccelNamespace(*CUNode, Name, NDie); 1108 addGlobalName(Name, NDie, NS->getScope()); 1109 if (NS->getExportSymbols()) 1110 addFlag(NDie, dwarf::DW_AT_export_symbols); 1111 return &NDie; 1112 } 1113 1114 DIE *DwarfUnit::getOrCreateModule(const DIModule *M) { 1115 // Construct the context before querying for the existence of the DIE in case 1116 // such construction creates the DIE. 1117 DIE *ContextDIE = getOrCreateContextDIE(M->getScope()); 1118 1119 if (DIE *MDie = getDIE(M)) 1120 return MDie; 1121 DIE &MDie = createAndAddDIE(dwarf::DW_TAG_module, *ContextDIE, M); 1122 1123 if (!M->getName().empty()) { 1124 addString(MDie, dwarf::DW_AT_name, M->getName()); 1125 addGlobalName(M->getName(), MDie, M->getScope()); 1126 } 1127 if (!M->getConfigurationMacros().empty()) 1128 addString(MDie, dwarf::DW_AT_LLVM_config_macros, 1129 M->getConfigurationMacros()); 1130 if (!M->getIncludePath().empty()) 1131 addString(MDie, dwarf::DW_AT_LLVM_include_path, M->getIncludePath()); 1132 if (!M->getAPINotesFile().empty()) 1133 addString(MDie, dwarf::DW_AT_LLVM_apinotes, M->getAPINotesFile()); 1134 if (M->getFile()) 1135 addUInt(MDie, dwarf::DW_AT_decl_file, None, 1136 getOrCreateSourceID(M->getFile())); 1137 if (M->getLineNo()) 1138 addUInt(MDie, dwarf::DW_AT_decl_line, None, M->getLineNo()); 1139 1140 return &MDie; 1141 } 1142 1143 DIE *DwarfUnit::getOrCreateSubprogramDIE(const DISubprogram *SP, bool Minimal) { 1144 // Construct the context before querying for the existence of the DIE in case 1145 // such construction creates the DIE (as is the case for member function 1146 // declarations). 1147 DIE *ContextDIE = 1148 Minimal ? &getUnitDie() : getOrCreateContextDIE(SP->getScope()); 1149 1150 if (DIE *SPDie = getDIE(SP)) 1151 return SPDie; 1152 1153 if (auto *SPDecl = SP->getDeclaration()) { 1154 if (!Minimal) { 1155 // Add subprogram definitions to the CU die directly. 1156 ContextDIE = &getUnitDie(); 1157 // Build the decl now to ensure it precedes the definition. 1158 getOrCreateSubprogramDIE(SPDecl); 1159 } 1160 } 1161 1162 // DW_TAG_inlined_subroutine may refer to this DIE. 1163 DIE &SPDie = createAndAddDIE(dwarf::DW_TAG_subprogram, *ContextDIE, SP); 1164 1165 // Stop here and fill this in later, depending on whether or not this 1166 // subprogram turns out to have inlined instances or not. 1167 if (SP->isDefinition()) 1168 return &SPDie; 1169 1170 static_cast<DwarfUnit *>(SPDie.getUnit()) 1171 ->applySubprogramAttributes(SP, SPDie); 1172 return &SPDie; 1173 } 1174 1175 bool DwarfUnit::applySubprogramDefinitionAttributes(const DISubprogram *SP, 1176 DIE &SPDie) { 1177 DIE *DeclDie = nullptr; 1178 StringRef DeclLinkageName; 1179 if (auto *SPDecl = SP->getDeclaration()) { 1180 DITypeRefArray DeclArgs, DefinitionArgs; 1181 DeclArgs = SPDecl->getType()->getTypeArray(); 1182 DefinitionArgs = SP->getType()->getTypeArray(); 1183 1184 if (DeclArgs.size() && DefinitionArgs.size()) 1185 if (DefinitionArgs[0] != NULL && DeclArgs[0] != DefinitionArgs[0]) 1186 addType(SPDie, DefinitionArgs[0]); 1187 1188 DeclDie = getDIE(SPDecl); 1189 assert(DeclDie && "This DIE should've already been constructed when the " 1190 "definition DIE was created in " 1191 "getOrCreateSubprogramDIE"); 1192 // Look at the Decl's linkage name only if we emitted it. 1193 if (DD->useAllLinkageNames()) 1194 DeclLinkageName = SPDecl->getLinkageName(); 1195 unsigned DeclID = getOrCreateSourceID(SPDecl->getFile()); 1196 unsigned DefID = getOrCreateSourceID(SP->getFile()); 1197 if (DeclID != DefID) 1198 addUInt(SPDie, dwarf::DW_AT_decl_file, None, DefID); 1199 1200 if (SP->getLine() != SPDecl->getLine()) 1201 addUInt(SPDie, dwarf::DW_AT_decl_line, None, SP->getLine()); 1202 } 1203 1204 // Add function template parameters. 1205 addTemplateParams(SPDie, SP->getTemplateParams()); 1206 1207 // Add the linkage name if we have one and it isn't in the Decl. 1208 StringRef LinkageName = SP->getLinkageName(); 1209 assert(((LinkageName.empty() || DeclLinkageName.empty()) || 1210 LinkageName == DeclLinkageName) && 1211 "decl has a linkage name and it is different"); 1212 if (DeclLinkageName.empty() && 1213 // Always emit it for abstract subprograms. 1214 (DD->useAllLinkageNames() || DU->getAbstractSPDies().lookup(SP))) 1215 addLinkageName(SPDie, LinkageName); 1216 1217 if (!DeclDie) 1218 return false; 1219 1220 // Refer to the function declaration where all the other attributes will be 1221 // found. 1222 addDIEEntry(SPDie, dwarf::DW_AT_specification, *DeclDie); 1223 return true; 1224 } 1225 1226 void DwarfUnit::applySubprogramAttributes(const DISubprogram *SP, DIE &SPDie, 1227 bool SkipSPAttributes) { 1228 // If -fdebug-info-for-profiling is enabled, need to emit the subprogram 1229 // and its source location. 1230 bool SkipSPSourceLocation = SkipSPAttributes && 1231 !CUNode->getDebugInfoForProfiling(); 1232 if (!SkipSPSourceLocation) 1233 if (applySubprogramDefinitionAttributes(SP, SPDie)) 1234 return; 1235 1236 // Constructors and operators for anonymous aggregates do not have names. 1237 if (!SP->getName().empty()) 1238 addString(SPDie, dwarf::DW_AT_name, SP->getName()); 1239 1240 if (!SkipSPSourceLocation) 1241 addSourceLine(SPDie, SP); 1242 1243 // Skip the rest of the attributes under -gmlt to save space. 1244 if (SkipSPAttributes) 1245 return; 1246 1247 // Add the prototype if we have a prototype and we have a C like 1248 // language. 1249 uint16_t Language = getLanguage(); 1250 if (SP->isPrototyped() && 1251 (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 || 1252 Language == dwarf::DW_LANG_ObjC)) 1253 addFlag(SPDie, dwarf::DW_AT_prototyped); 1254 1255 if (SP->isObjCDirect()) 1256 addFlag(SPDie, dwarf::DW_AT_APPLE_objc_direct); 1257 1258 unsigned CC = 0; 1259 DITypeRefArray Args; 1260 if (const DISubroutineType *SPTy = SP->getType()) { 1261 Args = SPTy->getTypeArray(); 1262 CC = SPTy->getCC(); 1263 } 1264 1265 // Add a DW_AT_calling_convention if this has an explicit convention. 1266 if (CC && CC != dwarf::DW_CC_normal) 1267 addUInt(SPDie, dwarf::DW_AT_calling_convention, dwarf::DW_FORM_data1, CC); 1268 1269 // Add a return type. If this is a type like a C/C++ void type we don't add a 1270 // return type. 1271 if (Args.size()) 1272 if (auto Ty = Args[0]) 1273 addType(SPDie, Ty); 1274 1275 unsigned VK = SP->getVirtuality(); 1276 if (VK) { 1277 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, VK); 1278 if (SP->getVirtualIndex() != -1u) { 1279 DIELoc *Block = getDIELoc(); 1280 addUInt(*Block, dwarf::DW_FORM_data1, dwarf::DW_OP_constu); 1281 addUInt(*Block, dwarf::DW_FORM_udata, SP->getVirtualIndex()); 1282 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, Block); 1283 } 1284 ContainingTypeMap.insert(std::make_pair(&SPDie, SP->getContainingType())); 1285 } 1286 1287 if (!SP->isDefinition()) { 1288 addFlag(SPDie, dwarf::DW_AT_declaration); 1289 1290 // Add arguments. Do not add arguments for subprogram definition. They will 1291 // be handled while processing variables. 1292 constructSubprogramArguments(SPDie, Args); 1293 } 1294 1295 addThrownTypes(SPDie, SP->getThrownTypes()); 1296 1297 if (SP->isArtificial()) 1298 addFlag(SPDie, dwarf::DW_AT_artificial); 1299 1300 if (!SP->isLocalToUnit()) 1301 addFlag(SPDie, dwarf::DW_AT_external); 1302 1303 if (DD->useAppleExtensionAttributes()) { 1304 if (SP->isOptimized()) 1305 addFlag(SPDie, dwarf::DW_AT_APPLE_optimized); 1306 1307 if (unsigned isa = Asm->getISAEncoding()) 1308 addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa); 1309 } 1310 1311 if (SP->isLValueReference()) 1312 addFlag(SPDie, dwarf::DW_AT_reference); 1313 1314 if (SP->isRValueReference()) 1315 addFlag(SPDie, dwarf::DW_AT_rvalue_reference); 1316 1317 if (SP->isNoReturn()) 1318 addFlag(SPDie, dwarf::DW_AT_noreturn); 1319 1320 if (SP->isProtected()) 1321 addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, 1322 dwarf::DW_ACCESS_protected); 1323 else if (SP->isPrivate()) 1324 addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, 1325 dwarf::DW_ACCESS_private); 1326 else if (SP->isPublic()) 1327 addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, 1328 dwarf::DW_ACCESS_public); 1329 1330 if (SP->isExplicit()) 1331 addFlag(SPDie, dwarf::DW_AT_explicit); 1332 1333 if (SP->isMainSubprogram()) 1334 addFlag(SPDie, dwarf::DW_AT_main_subprogram); 1335 if (SP->isPure()) 1336 addFlag(SPDie, dwarf::DW_AT_pure); 1337 if (SP->isElemental()) 1338 addFlag(SPDie, dwarf::DW_AT_elemental); 1339 if (SP->isRecursive()) 1340 addFlag(SPDie, dwarf::DW_AT_recursive); 1341 1342 if (DD->getDwarfVersion() >= 5 && SP->isDeleted()) 1343 addFlag(SPDie, dwarf::DW_AT_deleted); 1344 } 1345 1346 void DwarfUnit::constructSubrangeDIE(DIE &Buffer, const DISubrange *SR, 1347 DIE *IndexTy) { 1348 DIE &DW_Subrange = createAndAddDIE(dwarf::DW_TAG_subrange_type, Buffer); 1349 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, *IndexTy); 1350 1351 // The LowerBound value defines the lower bounds which is typically zero for 1352 // C/C++. The Count value is the number of elements. Values are 64 bit. If 1353 // Count == -1 then the array is unbounded and we do not emit 1354 // DW_AT_lower_bound and DW_AT_count attributes. 1355 int64_t DefaultLowerBound = getDefaultLowerBound(); 1356 int64_t Count = -1; 1357 if (auto *CI = SR->getCount().dyn_cast<ConstantInt*>()) 1358 Count = CI->getSExtValue(); 1359 1360 auto addBoundTypeEntry = [&](dwarf::Attribute Attr, 1361 DISubrange::BoundType Bound) -> void { 1362 if (auto *BV = Bound.dyn_cast<DIVariable *>()) { 1363 if (auto *VarDIE = getDIE(BV)) 1364 addDIEEntry(DW_Subrange, Attr, *VarDIE); 1365 } else if (auto *BE = Bound.dyn_cast<DIExpression *>()) { 1366 DIELoc *Loc = new (DIEValueAllocator) DIELoc; 1367 DIEDwarfExpression DwarfExpr(*Asm, getCU(), *Loc); 1368 DwarfExpr.setMemoryLocationKind(); 1369 DwarfExpr.addExpression(BE); 1370 addBlock(DW_Subrange, Attr, DwarfExpr.finalize()); 1371 } else if (auto *BI = Bound.dyn_cast<ConstantInt *>()) { 1372 if (Attr != dwarf::DW_AT_lower_bound || DefaultLowerBound == -1 || 1373 BI->getSExtValue() != DefaultLowerBound) 1374 addSInt(DW_Subrange, Attr, dwarf::DW_FORM_sdata, BI->getSExtValue()); 1375 } 1376 }; 1377 1378 addBoundTypeEntry(dwarf::DW_AT_lower_bound, SR->getLowerBound()); 1379 1380 if (auto *CV = SR->getCount().dyn_cast<DIVariable*>()) { 1381 if (auto *CountVarDIE = getDIE(CV)) 1382 addDIEEntry(DW_Subrange, dwarf::DW_AT_count, *CountVarDIE); 1383 } else if (Count != -1) 1384 addUInt(DW_Subrange, dwarf::DW_AT_count, None, Count); 1385 1386 addBoundTypeEntry(dwarf::DW_AT_upper_bound, SR->getUpperBound()); 1387 1388 addBoundTypeEntry(dwarf::DW_AT_byte_stride, SR->getStride()); 1389 } 1390 1391 DIE *DwarfUnit::getIndexTyDie() { 1392 if (IndexTyDie) 1393 return IndexTyDie; 1394 // Construct an integer type to use for indexes. 1395 IndexTyDie = &createAndAddDIE(dwarf::DW_TAG_base_type, getUnitDie()); 1396 StringRef Name = "__ARRAY_SIZE_TYPE__"; 1397 addString(*IndexTyDie, dwarf::DW_AT_name, Name); 1398 addUInt(*IndexTyDie, dwarf::DW_AT_byte_size, None, sizeof(int64_t)); 1399 addUInt(*IndexTyDie, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1, 1400 dwarf::DW_ATE_unsigned); 1401 DD->addAccelType(*CUNode, Name, *IndexTyDie, /*Flags*/ 0); 1402 return IndexTyDie; 1403 } 1404 1405 /// Returns true if the vector's size differs from the sum of sizes of elements 1406 /// the user specified. This can occur if the vector has been rounded up to 1407 /// fit memory alignment constraints. 1408 static bool hasVectorBeenPadded(const DICompositeType *CTy) { 1409 assert(CTy && CTy->isVector() && "Composite type is not a vector"); 1410 const uint64_t ActualSize = CTy->getSizeInBits(); 1411 1412 // Obtain the size of each element in the vector. 1413 DIType *BaseTy = CTy->getBaseType(); 1414 assert(BaseTy && "Unknown vector element type."); 1415 const uint64_t ElementSize = BaseTy->getSizeInBits(); 1416 1417 // Locate the number of elements in the vector. 1418 const DINodeArray Elements = CTy->getElements(); 1419 assert(Elements.size() == 1 && 1420 Elements[0]->getTag() == dwarf::DW_TAG_subrange_type && 1421 "Invalid vector element array, expected one element of type subrange"); 1422 const auto Subrange = cast<DISubrange>(Elements[0]); 1423 const auto NumVecElements = 1424 Subrange->getCount() 1425 ? Subrange->getCount().get<ConstantInt *>()->getSExtValue() 1426 : 0; 1427 1428 // Ensure we found the element count and that the actual size is wide 1429 // enough to contain the requested size. 1430 assert(ActualSize >= (NumVecElements * ElementSize) && "Invalid vector size"); 1431 return ActualSize != (NumVecElements * ElementSize); 1432 } 1433 1434 void DwarfUnit::constructArrayTypeDIE(DIE &Buffer, const DICompositeType *CTy) { 1435 if (CTy->isVector()) { 1436 addFlag(Buffer, dwarf::DW_AT_GNU_vector); 1437 if (hasVectorBeenPadded(CTy)) 1438 addUInt(Buffer, dwarf::DW_AT_byte_size, None, 1439 CTy->getSizeInBits() / CHAR_BIT); 1440 } 1441 1442 if (DIVariable *Var = CTy->getDataLocation()) { 1443 if (auto *VarDIE = getDIE(Var)) 1444 addDIEEntry(Buffer, dwarf::DW_AT_data_location, *VarDIE); 1445 } else if (DIExpression *Expr = CTy->getDataLocationExp()) { 1446 DIELoc *Loc = new (DIEValueAllocator) DIELoc; 1447 DIEDwarfExpression DwarfExpr(*Asm, getCU(), *Loc); 1448 DwarfExpr.setMemoryLocationKind(); 1449 DwarfExpr.addExpression(Expr); 1450 addBlock(Buffer, dwarf::DW_AT_data_location, DwarfExpr.finalize()); 1451 } 1452 1453 if (DIVariable *Var = CTy->getAssociated()) { 1454 if (auto *VarDIE = getDIE(Var)) 1455 addDIEEntry(Buffer, dwarf::DW_AT_associated, *VarDIE); 1456 } else if (DIExpression *Expr = CTy->getAssociatedExp()) { 1457 DIELoc *Loc = new (DIEValueAllocator) DIELoc; 1458 DIEDwarfExpression DwarfExpr(*Asm, getCU(), *Loc); 1459 DwarfExpr.setMemoryLocationKind(); 1460 DwarfExpr.addExpression(Expr); 1461 addBlock(Buffer, dwarf::DW_AT_associated, DwarfExpr.finalize()); 1462 } 1463 1464 if (DIVariable *Var = CTy->getAllocated()) { 1465 if (auto *VarDIE = getDIE(Var)) 1466 addDIEEntry(Buffer, dwarf::DW_AT_allocated, *VarDIE); 1467 } else if (DIExpression *Expr = CTy->getAllocatedExp()) { 1468 DIELoc *Loc = new (DIEValueAllocator) DIELoc; 1469 DIEDwarfExpression DwarfExpr(*Asm, getCU(), *Loc); 1470 DwarfExpr.setMemoryLocationKind(); 1471 DwarfExpr.addExpression(Expr); 1472 addBlock(Buffer, dwarf::DW_AT_allocated, DwarfExpr.finalize()); 1473 } 1474 1475 // Emit the element type. 1476 addType(Buffer, CTy->getBaseType()); 1477 1478 // Get an anonymous type for index type. 1479 // FIXME: This type should be passed down from the front end 1480 // as different languages may have different sizes for indexes. 1481 DIE *IdxTy = getIndexTyDie(); 1482 1483 // Add subranges to array type. 1484 DINodeArray Elements = CTy->getElements(); 1485 for (unsigned i = 0, N = Elements.size(); i < N; ++i) { 1486 // FIXME: Should this really be such a loose cast? 1487 if (auto *Element = dyn_cast_or_null<DINode>(Elements[i])) 1488 if (Element->getTag() == dwarf::DW_TAG_subrange_type) 1489 constructSubrangeDIE(Buffer, cast<DISubrange>(Element), IdxTy); 1490 } 1491 } 1492 1493 void DwarfUnit::constructEnumTypeDIE(DIE &Buffer, const DICompositeType *CTy) { 1494 const DIType *DTy = CTy->getBaseType(); 1495 bool IsUnsigned = DTy && isUnsignedDIType(DD, DTy); 1496 if (DTy) { 1497 if (DD->getDwarfVersion() >= 3) 1498 addType(Buffer, DTy); 1499 if (DD->getDwarfVersion() >= 4 && (CTy->getFlags() & DINode::FlagEnumClass)) 1500 addFlag(Buffer, dwarf::DW_AT_enum_class); 1501 } 1502 1503 auto *Context = CTy->getScope(); 1504 bool IndexEnumerators = !Context || isa<DICompileUnit>(Context) || isa<DIFile>(Context) || 1505 isa<DINamespace>(Context) || isa<DICommonBlock>(Context); 1506 DINodeArray Elements = CTy->getElements(); 1507 1508 // Add enumerators to enumeration type. 1509 for (unsigned i = 0, N = Elements.size(); i < N; ++i) { 1510 auto *Enum = dyn_cast_or_null<DIEnumerator>(Elements[i]); 1511 if (Enum) { 1512 DIE &Enumerator = createAndAddDIE(dwarf::DW_TAG_enumerator, Buffer); 1513 StringRef Name = Enum->getName(); 1514 addString(Enumerator, dwarf::DW_AT_name, Name); 1515 addConstantValue(Enumerator, Enum->getValue(), IsUnsigned); 1516 if (IndexEnumerators) 1517 addGlobalName(Name, Enumerator, Context); 1518 } 1519 } 1520 } 1521 1522 void DwarfUnit::constructContainingTypeDIEs() { 1523 for (auto CI = ContainingTypeMap.begin(), CE = ContainingTypeMap.end(); 1524 CI != CE; ++CI) { 1525 DIE &SPDie = *CI->first; 1526 const DINode *D = CI->second; 1527 if (!D) 1528 continue; 1529 DIE *NDie = getDIE(D); 1530 if (!NDie) 1531 continue; 1532 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, *NDie); 1533 } 1534 } 1535 1536 DIE &DwarfUnit::constructMemberDIE(DIE &Buffer, const DIDerivedType *DT) { 1537 DIE &MemberDie = createAndAddDIE(DT->getTag(), Buffer); 1538 StringRef Name = DT->getName(); 1539 if (!Name.empty()) 1540 addString(MemberDie, dwarf::DW_AT_name, Name); 1541 1542 if (DIType *Resolved = DT->getBaseType()) 1543 addType(MemberDie, Resolved); 1544 1545 addSourceLine(MemberDie, DT); 1546 1547 if (DT->getTag() == dwarf::DW_TAG_inheritance && DT->isVirtual()) { 1548 1549 // For C++, virtual base classes are not at fixed offset. Use following 1550 // expression to extract appropriate offset from vtable. 1551 // BaseAddr = ObAddr + *((*ObAddr) - Offset) 1552 1553 DIELoc *VBaseLocationDie = new (DIEValueAllocator) DIELoc; 1554 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_dup); 1555 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref); 1556 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_constu); 1557 addUInt(*VBaseLocationDie, dwarf::DW_FORM_udata, DT->getOffsetInBits()); 1558 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_minus); 1559 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref); 1560 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus); 1561 1562 addBlock(MemberDie, dwarf::DW_AT_data_member_location, VBaseLocationDie); 1563 } else { 1564 uint64_t Size = DT->getSizeInBits(); 1565 uint64_t FieldSize = DD->getBaseTypeSize(DT); 1566 uint32_t AlignInBytes = DT->getAlignInBytes(); 1567 uint64_t OffsetInBytes; 1568 1569 bool IsBitfield = FieldSize && Size != FieldSize; 1570 if (IsBitfield) { 1571 // Handle bitfield, assume bytes are 8 bits. 1572 if (DD->useDWARF2Bitfields()) 1573 addUInt(MemberDie, dwarf::DW_AT_byte_size, None, FieldSize/8); 1574 addUInt(MemberDie, dwarf::DW_AT_bit_size, None, Size); 1575 1576 uint64_t Offset = DT->getOffsetInBits(); 1577 // We can't use DT->getAlignInBits() here: AlignInBits for member type 1578 // is non-zero if and only if alignment was forced (e.g. _Alignas()), 1579 // which can't be done with bitfields. Thus we use FieldSize here. 1580 uint32_t AlignInBits = FieldSize; 1581 uint32_t AlignMask = ~(AlignInBits - 1); 1582 // The bits from the start of the storage unit to the start of the field. 1583 uint64_t StartBitOffset = Offset - (Offset & AlignMask); 1584 // The byte offset of the field's aligned storage unit inside the struct. 1585 OffsetInBytes = (Offset - StartBitOffset) / 8; 1586 1587 if (DD->useDWARF2Bitfields()) { 1588 uint64_t HiMark = (Offset + FieldSize) & AlignMask; 1589 uint64_t FieldOffset = (HiMark - FieldSize); 1590 Offset -= FieldOffset; 1591 1592 // Maybe we need to work from the other end. 1593 if (Asm->getDataLayout().isLittleEndian()) 1594 Offset = FieldSize - (Offset + Size); 1595 1596 addUInt(MemberDie, dwarf::DW_AT_bit_offset, None, Offset); 1597 OffsetInBytes = FieldOffset >> 3; 1598 } else { 1599 addUInt(MemberDie, dwarf::DW_AT_data_bit_offset, None, Offset); 1600 } 1601 } else { 1602 // This is not a bitfield. 1603 OffsetInBytes = DT->getOffsetInBits() / 8; 1604 if (AlignInBytes) 1605 addUInt(MemberDie, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata, 1606 AlignInBytes); 1607 } 1608 1609 if (DD->getDwarfVersion() <= 2) { 1610 DIELoc *MemLocationDie = new (DIEValueAllocator) DIELoc; 1611 addUInt(*MemLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst); 1612 addUInt(*MemLocationDie, dwarf::DW_FORM_udata, OffsetInBytes); 1613 addBlock(MemberDie, dwarf::DW_AT_data_member_location, MemLocationDie); 1614 } else if (!IsBitfield || DD->useDWARF2Bitfields()) 1615 addUInt(MemberDie, dwarf::DW_AT_data_member_location, None, 1616 OffsetInBytes); 1617 } 1618 1619 if (DT->isProtected()) 1620 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, 1621 dwarf::DW_ACCESS_protected); 1622 else if (DT->isPrivate()) 1623 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, 1624 dwarf::DW_ACCESS_private); 1625 // Otherwise C++ member and base classes are considered public. 1626 else if (DT->isPublic()) 1627 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, 1628 dwarf::DW_ACCESS_public); 1629 if (DT->isVirtual()) 1630 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, 1631 dwarf::DW_VIRTUALITY_virtual); 1632 1633 // Objective-C properties. 1634 if (DINode *PNode = DT->getObjCProperty()) 1635 if (DIE *PDie = getDIE(PNode)) 1636 MemberDie.addValue(DIEValueAllocator, dwarf::DW_AT_APPLE_property, 1637 dwarf::DW_FORM_ref4, DIEEntry(*PDie)); 1638 1639 if (DT->isArtificial()) 1640 addFlag(MemberDie, dwarf::DW_AT_artificial); 1641 1642 return MemberDie; 1643 } 1644 1645 DIE *DwarfUnit::getOrCreateStaticMemberDIE(const DIDerivedType *DT) { 1646 if (!DT) 1647 return nullptr; 1648 1649 // Construct the context before querying for the existence of the DIE in case 1650 // such construction creates the DIE. 1651 DIE *ContextDIE = getOrCreateContextDIE(DT->getScope()); 1652 assert(dwarf::isType(ContextDIE->getTag()) && 1653 "Static member should belong to a type."); 1654 1655 if (DIE *StaticMemberDIE = getDIE(DT)) 1656 return StaticMemberDIE; 1657 1658 DIE &StaticMemberDIE = createAndAddDIE(DT->getTag(), *ContextDIE, DT); 1659 1660 const DIType *Ty = DT->getBaseType(); 1661 1662 addString(StaticMemberDIE, dwarf::DW_AT_name, DT->getName()); 1663 addType(StaticMemberDIE, Ty); 1664 addSourceLine(StaticMemberDIE, DT); 1665 addFlag(StaticMemberDIE, dwarf::DW_AT_external); 1666 addFlag(StaticMemberDIE, dwarf::DW_AT_declaration); 1667 1668 // FIXME: We could omit private if the parent is a class_type, and 1669 // public if the parent is something else. 1670 if (DT->isProtected()) 1671 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, 1672 dwarf::DW_ACCESS_protected); 1673 else if (DT->isPrivate()) 1674 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, 1675 dwarf::DW_ACCESS_private); 1676 else if (DT->isPublic()) 1677 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, 1678 dwarf::DW_ACCESS_public); 1679 1680 if (const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(DT->getConstant())) 1681 addConstantValue(StaticMemberDIE, CI, Ty); 1682 if (const ConstantFP *CFP = dyn_cast_or_null<ConstantFP>(DT->getConstant())) 1683 addConstantFPValue(StaticMemberDIE, CFP); 1684 1685 if (uint32_t AlignInBytes = DT->getAlignInBytes()) 1686 addUInt(StaticMemberDIE, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata, 1687 AlignInBytes); 1688 1689 return &StaticMemberDIE; 1690 } 1691 1692 void DwarfUnit::emitCommonHeader(bool UseOffsets, dwarf::UnitType UT) { 1693 // Emit size of content not including length itself 1694 if (!DD->useSectionsAsReferences()) { 1695 StringRef Prefix = isDwoUnit() ? "debug_info_dwo_" : "debug_info_"; 1696 MCSymbol *BeginLabel = Asm->createTempSymbol(Prefix + "start"); 1697 EndLabel = Asm->createTempSymbol(Prefix + "end"); 1698 Asm->emitDwarfUnitLength(EndLabel, BeginLabel, "Length of Unit"); 1699 Asm->OutStreamer->emitLabel(BeginLabel); 1700 } else 1701 Asm->emitDwarfUnitLength(getHeaderSize() + getUnitDie().getSize(), 1702 "Length of Unit"); 1703 1704 Asm->OutStreamer->AddComment("DWARF version number"); 1705 unsigned Version = DD->getDwarfVersion(); 1706 Asm->emitInt16(Version); 1707 1708 // DWARF v5 reorders the address size and adds a unit type. 1709 if (Version >= 5) { 1710 Asm->OutStreamer->AddComment("DWARF Unit Type"); 1711 Asm->emitInt8(UT); 1712 Asm->OutStreamer->AddComment("Address Size (in bytes)"); 1713 Asm->emitInt8(Asm->MAI->getCodePointerSize()); 1714 } 1715 1716 // We share one abbreviations table across all units so it's always at the 1717 // start of the section. Use a relocatable offset where needed to ensure 1718 // linking doesn't invalidate that offset. 1719 Asm->OutStreamer->AddComment("Offset Into Abbrev. Section"); 1720 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering(); 1721 if (UseOffsets) 1722 Asm->emitDwarfLengthOrOffset(0); 1723 else 1724 Asm->emitDwarfSymbolReference( 1725 TLOF.getDwarfAbbrevSection()->getBeginSymbol(), false); 1726 1727 if (Version <= 4) { 1728 Asm->OutStreamer->AddComment("Address Size (in bytes)"); 1729 Asm->emitInt8(Asm->MAI->getCodePointerSize()); 1730 } 1731 } 1732 1733 void DwarfTypeUnit::emitHeader(bool UseOffsets) { 1734 DwarfUnit::emitCommonHeader(UseOffsets, 1735 DD->useSplitDwarf() ? dwarf::DW_UT_split_type 1736 : dwarf::DW_UT_type); 1737 Asm->OutStreamer->AddComment("Type Signature"); 1738 Asm->OutStreamer->emitIntValue(TypeSignature, sizeof(TypeSignature)); 1739 Asm->OutStreamer->AddComment("Type DIE Offset"); 1740 // In a skeleton type unit there is no type DIE so emit a zero offset. 1741 Asm->emitDwarfLengthOrOffset(Ty ? Ty->getOffset() : 0); 1742 } 1743 1744 DIE::value_iterator 1745 DwarfUnit::addSectionDelta(DIE &Die, dwarf::Attribute Attribute, 1746 const MCSymbol *Hi, const MCSymbol *Lo) { 1747 return Die.addValue(DIEValueAllocator, Attribute, 1748 DD->getDwarfSectionOffsetForm(), 1749 new (DIEValueAllocator) DIEDelta(Hi, Lo)); 1750 } 1751 1752 DIE::value_iterator 1753 DwarfUnit::addSectionLabel(DIE &Die, dwarf::Attribute Attribute, 1754 const MCSymbol *Label, const MCSymbol *Sec) { 1755 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections()) 1756 return addLabel(Die, Attribute, DD->getDwarfSectionOffsetForm(), Label); 1757 return addSectionDelta(Die, Attribute, Label, Sec); 1758 } 1759 1760 bool DwarfTypeUnit::isDwoUnit() const { 1761 // Since there are no skeleton type units, all type units are dwo type units 1762 // when split DWARF is being used. 1763 return DD->useSplitDwarf(); 1764 } 1765 1766 void DwarfTypeUnit::addGlobalName(StringRef Name, const DIE &Die, 1767 const DIScope *Context) { 1768 getCU().addGlobalNameForTypeUnit(Name, Context); 1769 } 1770 1771 void DwarfTypeUnit::addGlobalType(const DIType *Ty, const DIE &Die, 1772 const DIScope *Context) { 1773 getCU().addGlobalTypeUnitType(Ty, Context); 1774 } 1775 1776 const MCSymbol *DwarfUnit::getCrossSectionRelativeBaseAddress() const { 1777 if (!Asm->MAI->doesDwarfUseRelocationsAcrossSections()) 1778 return nullptr; 1779 if (isDwoUnit()) 1780 return nullptr; 1781 return getSection()->getBeginSymbol(); 1782 } 1783 1784 void DwarfUnit::addStringOffsetsStart() { 1785 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering(); 1786 addSectionLabel(getUnitDie(), dwarf::DW_AT_str_offsets_base, 1787 DU->getStringOffsetsStartSym(), 1788 TLOF.getDwarfStrOffSection()->getBeginSymbol()); 1789 } 1790 1791 void DwarfUnit::addRnglistsBase() { 1792 assert(DD->getDwarfVersion() >= 5 && 1793 "DW_AT_rnglists_base requires DWARF version 5 or later"); 1794 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering(); 1795 addSectionLabel(getUnitDie(), dwarf::DW_AT_rnglists_base, 1796 DU->getRnglistsTableBaseSym(), 1797 TLOF.getDwarfRnglistsSection()->getBeginSymbol()); 1798 } 1799 1800 void DwarfTypeUnit::finishNonUnitTypeDIE(DIE& D, const DICompositeType *CTy) { 1801 addFlag(D, dwarf::DW_AT_declaration); 1802 StringRef Name = CTy->getName(); 1803 if (!Name.empty()) 1804 addString(D, dwarf::DW_AT_name, Name); 1805 getCU().createTypeDIE(CTy); 1806 } 1807