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