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