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