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