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 864 void DwarfUnit::constructTypeDIE(DIE &Buffer, const DIDerivedType *DTy) { 865 // Get core information. 866 StringRef Name = DTy->getName(); 867 uint64_t Size = DTy->getSizeInBits() >> 3; 868 uint16_t Tag = Buffer.getTag(); 869 870 // Map to main type, void will not have a type. 871 const DIType *FromTy = resolve(DTy->getBaseType()); 872 if (FromTy) 873 addType(Buffer, FromTy); 874 875 // Add name if not anonymous or intermediate type. 876 if (!Name.empty()) 877 addString(Buffer, dwarf::DW_AT_name, Name); 878 879 // Add size if non-zero (derived types might be zero-sized.) 880 if (Size && Tag != dwarf::DW_TAG_pointer_type 881 && Tag != dwarf::DW_TAG_ptr_to_member_type 882 && Tag != dwarf::DW_TAG_reference_type 883 && Tag != dwarf::DW_TAG_rvalue_reference_type) 884 addUInt(Buffer, dwarf::DW_AT_byte_size, None, Size); 885 886 if (Tag == dwarf::DW_TAG_ptr_to_member_type) 887 addDIEEntry( 888 Buffer, dwarf::DW_AT_containing_type, 889 *getOrCreateTypeDIE(resolve(cast<DIDerivedType>(DTy)->getClassType()))); 890 // Add source line info if available and TyDesc is not a forward declaration. 891 if (!DTy->isForwardDecl()) 892 addSourceLine(Buffer, DTy); 893 894 // If DWARF address space value is other than None, add it for pointer and 895 // reference types as DW_AT_address_class. 896 if (DTy->getDWARFAddressSpace() && (Tag == dwarf::DW_TAG_pointer_type || 897 Tag == dwarf::DW_TAG_reference_type)) 898 addUInt(Buffer, dwarf::DW_AT_address_class, dwarf::DW_FORM_data4, 899 DTy->getDWARFAddressSpace().getValue()); 900 } 901 902 void DwarfUnit::constructSubprogramArguments(DIE &Buffer, DITypeRefArray Args) { 903 for (unsigned i = 1, N = Args.size(); i < N; ++i) { 904 const DIType *Ty = resolve(Args[i]); 905 if (!Ty) { 906 assert(i == N-1 && "Unspecified parameter must be the last argument"); 907 createAndAddDIE(dwarf::DW_TAG_unspecified_parameters, Buffer); 908 } else { 909 DIE &Arg = createAndAddDIE(dwarf::DW_TAG_formal_parameter, Buffer); 910 addType(Arg, Ty); 911 if (Ty->isArtificial()) 912 addFlag(Arg, dwarf::DW_AT_artificial); 913 } 914 } 915 } 916 917 void DwarfUnit::constructTypeDIE(DIE &Buffer, const DISubroutineType *CTy) { 918 // Add return type. A void return won't have a type. 919 auto Elements = cast<DISubroutineType>(CTy)->getTypeArray(); 920 if (Elements.size()) 921 if (auto RTy = resolve(Elements[0])) 922 addType(Buffer, RTy); 923 924 bool isPrototyped = true; 925 if (Elements.size() == 2 && !Elements[1]) 926 isPrototyped = false; 927 928 constructSubprogramArguments(Buffer, Elements); 929 930 // Add prototype flag if we're dealing with a C language and the function has 931 // been prototyped. 932 uint16_t Language = getLanguage(); 933 if (isPrototyped && 934 (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 || 935 Language == dwarf::DW_LANG_ObjC)) 936 addFlag(Buffer, dwarf::DW_AT_prototyped); 937 938 // Add a DW_AT_calling_convention if this has an explicit convention. 939 if (CTy->getCC() && CTy->getCC() != dwarf::DW_CC_normal) 940 addUInt(Buffer, dwarf::DW_AT_calling_convention, dwarf::DW_FORM_data1, 941 CTy->getCC()); 942 943 if (CTy->isLValueReference()) 944 addFlag(Buffer, dwarf::DW_AT_reference); 945 946 if (CTy->isRValueReference()) 947 addFlag(Buffer, dwarf::DW_AT_rvalue_reference); 948 } 949 950 void DwarfUnit::constructTypeDIE(DIE &Buffer, const DICompositeType *CTy) { 951 // Add name if not anonymous or intermediate type. 952 StringRef Name = CTy->getName(); 953 954 uint64_t Size = CTy->getSizeInBits() >> 3; 955 uint16_t Tag = Buffer.getTag(); 956 957 switch (Tag) { 958 case dwarf::DW_TAG_array_type: 959 constructArrayTypeDIE(Buffer, CTy); 960 break; 961 case dwarf::DW_TAG_enumeration_type: 962 constructEnumTypeDIE(Buffer, CTy); 963 break; 964 case dwarf::DW_TAG_variant_part: 965 case dwarf::DW_TAG_structure_type: 966 case dwarf::DW_TAG_union_type: 967 case dwarf::DW_TAG_class_type: { 968 // Emit the discriminator for a variant part. 969 DIDerivedType *Discriminator = nullptr; 970 if (Tag == dwarf::DW_TAG_variant_part) { 971 Discriminator = CTy->getDiscriminator(); 972 if (Discriminator) { 973 // DWARF says: 974 // If the variant part has a discriminant, the discriminant is 975 // represented by a separate debugging information entry which is 976 // a child of the variant part entry. 977 DIE &DiscMember = constructMemberDIE(Buffer, Discriminator); 978 addDIEEntry(Buffer, dwarf::DW_AT_discr, DiscMember); 979 } 980 } 981 982 // Add elements to structure type. 983 DINodeArray Elements = CTy->getElements(); 984 for (const auto *Element : Elements) { 985 if (!Element) 986 continue; 987 if (auto *SP = dyn_cast<DISubprogram>(Element)) 988 getOrCreateSubprogramDIE(SP); 989 else if (auto *DDTy = dyn_cast<DIDerivedType>(Element)) { 990 if (DDTy->getTag() == dwarf::DW_TAG_friend) { 991 DIE &ElemDie = createAndAddDIE(dwarf::DW_TAG_friend, Buffer); 992 addType(ElemDie, resolve(DDTy->getBaseType()), dwarf::DW_AT_friend); 993 } else if (DDTy->isStaticMember()) { 994 getOrCreateStaticMemberDIE(DDTy); 995 } else if (Tag == dwarf::DW_TAG_variant_part) { 996 // When emitting a variant part, wrap each member in 997 // DW_TAG_variant. 998 DIE &Variant = createAndAddDIE(dwarf::DW_TAG_variant, Buffer); 999 if (const ConstantInt *CI = 1000 dyn_cast_or_null<ConstantInt>(DDTy->getDiscriminantValue())) { 1001 if (isUnsignedDIType(DD, resolve(Discriminator->getBaseType()))) 1002 addUInt(Variant, dwarf::DW_AT_discr_value, None, CI->getZExtValue()); 1003 else 1004 addSInt(Variant, dwarf::DW_AT_discr_value, None, CI->getSExtValue()); 1005 } 1006 constructMemberDIE(Variant, DDTy); 1007 } else { 1008 constructMemberDIE(Buffer, DDTy); 1009 } 1010 } else if (auto *Property = dyn_cast<DIObjCProperty>(Element)) { 1011 DIE &ElemDie = createAndAddDIE(Property->getTag(), Buffer); 1012 StringRef PropertyName = Property->getName(); 1013 addString(ElemDie, dwarf::DW_AT_APPLE_property_name, PropertyName); 1014 if (Property->getType()) 1015 addType(ElemDie, resolve(Property->getType())); 1016 addSourceLine(ElemDie, Property); 1017 StringRef GetterName = Property->getGetterName(); 1018 if (!GetterName.empty()) 1019 addString(ElemDie, dwarf::DW_AT_APPLE_property_getter, GetterName); 1020 StringRef SetterName = Property->getSetterName(); 1021 if (!SetterName.empty()) 1022 addString(ElemDie, dwarf::DW_AT_APPLE_property_setter, SetterName); 1023 if (unsigned PropertyAttributes = Property->getAttributes()) 1024 addUInt(ElemDie, dwarf::DW_AT_APPLE_property_attribute, None, 1025 PropertyAttributes); 1026 } else if (auto *Composite = dyn_cast<DICompositeType>(Element)) { 1027 if (Composite->getTag() == dwarf::DW_TAG_variant_part) { 1028 DIE &VariantPart = createAndAddDIE(Composite->getTag(), Buffer); 1029 constructTypeDIE(VariantPart, Composite); 1030 } 1031 } 1032 } 1033 1034 if (CTy->isAppleBlockExtension()) 1035 addFlag(Buffer, dwarf::DW_AT_APPLE_block); 1036 1037 // This is outside the DWARF spec, but GDB expects a DW_AT_containing_type 1038 // inside C++ composite types to point to the base class with the vtable. 1039 // Rust uses DW_AT_containing_type to link a vtable to the type 1040 // for which it was created. 1041 if (auto *ContainingType = resolve(CTy->getVTableHolder())) 1042 addDIEEntry(Buffer, dwarf::DW_AT_containing_type, 1043 *getOrCreateTypeDIE(ContainingType)); 1044 1045 if (CTy->isObjcClassComplete()) 1046 addFlag(Buffer, dwarf::DW_AT_APPLE_objc_complete_type); 1047 1048 // Add template parameters to a class, structure or union types. 1049 // FIXME: The support isn't in the metadata for this yet. 1050 if (Tag == dwarf::DW_TAG_class_type || 1051 Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type) 1052 addTemplateParams(Buffer, CTy->getTemplateParams()); 1053 1054 // Add the type's non-standard calling convention. 1055 uint8_t CC = 0; 1056 if (CTy->isTypePassByValue()) 1057 CC = dwarf::DW_CC_pass_by_value; 1058 else if (CTy->isTypePassByReference()) 1059 CC = dwarf::DW_CC_pass_by_reference; 1060 if (CC) 1061 addUInt(Buffer, dwarf::DW_AT_calling_convention, dwarf::DW_FORM_data1, 1062 CC); 1063 break; 1064 } 1065 default: 1066 break; 1067 } 1068 1069 // Add name if not anonymous or intermediate type. 1070 if (!Name.empty()) 1071 addString(Buffer, dwarf::DW_AT_name, Name); 1072 1073 if (Tag == dwarf::DW_TAG_enumeration_type || 1074 Tag == dwarf::DW_TAG_class_type || Tag == dwarf::DW_TAG_structure_type || 1075 Tag == dwarf::DW_TAG_union_type) { 1076 // Add size if non-zero (derived types might be zero-sized.) 1077 // TODO: Do we care about size for enum forward declarations? 1078 if (Size) 1079 addUInt(Buffer, dwarf::DW_AT_byte_size, None, Size); 1080 else if (!CTy->isForwardDecl()) 1081 // Add zero size if it is not a forward declaration. 1082 addUInt(Buffer, dwarf::DW_AT_byte_size, None, 0); 1083 1084 // If we're a forward decl, say so. 1085 if (CTy->isForwardDecl()) 1086 addFlag(Buffer, dwarf::DW_AT_declaration); 1087 1088 // Add source line info if available. 1089 if (!CTy->isForwardDecl()) 1090 addSourceLine(Buffer, CTy); 1091 1092 // No harm in adding the runtime language to the declaration. 1093 unsigned RLang = CTy->getRuntimeLang(); 1094 if (RLang) 1095 addUInt(Buffer, dwarf::DW_AT_APPLE_runtime_class, dwarf::DW_FORM_data1, 1096 RLang); 1097 1098 // Add align info if available. 1099 if (uint32_t AlignInBytes = CTy->getAlignInBytes()) 1100 addUInt(Buffer, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata, 1101 AlignInBytes); 1102 } 1103 } 1104 1105 void DwarfUnit::constructTemplateTypeParameterDIE( 1106 DIE &Buffer, const DITemplateTypeParameter *TP) { 1107 DIE &ParamDIE = 1108 createAndAddDIE(dwarf::DW_TAG_template_type_parameter, Buffer); 1109 // Add the type if it exists, it could be void and therefore no type. 1110 if (TP->getType()) 1111 addType(ParamDIE, resolve(TP->getType())); 1112 if (!TP->getName().empty()) 1113 addString(ParamDIE, dwarf::DW_AT_name, TP->getName()); 1114 } 1115 1116 void DwarfUnit::constructTemplateValueParameterDIE( 1117 DIE &Buffer, const DITemplateValueParameter *VP) { 1118 DIE &ParamDIE = createAndAddDIE(VP->getTag(), Buffer); 1119 1120 // Add the type if there is one, template template and template parameter 1121 // packs will not have a type. 1122 if (VP->getTag() == dwarf::DW_TAG_template_value_parameter) 1123 addType(ParamDIE, resolve(VP->getType())); 1124 if (!VP->getName().empty()) 1125 addString(ParamDIE, dwarf::DW_AT_name, VP->getName()); 1126 if (Metadata *Val = VP->getValue()) { 1127 if (ConstantInt *CI = mdconst::dyn_extract<ConstantInt>(Val)) 1128 addConstantValue(ParamDIE, CI, resolve(VP->getType())); 1129 else if (GlobalValue *GV = mdconst::dyn_extract<GlobalValue>(Val)) { 1130 // We cannot describe the location of dllimport'd entities: the 1131 // computation of their address requires loads from the IAT. 1132 if (!GV->hasDLLImportStorageClass()) { 1133 // For declaration non-type template parameters (such as global values 1134 // and functions) 1135 DIELoc *Loc = new (DIEValueAllocator) DIELoc; 1136 addOpAddress(*Loc, Asm->getSymbol(GV)); 1137 // Emit DW_OP_stack_value to use the address as the immediate value of 1138 // the parameter, rather than a pointer to it. 1139 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_stack_value); 1140 addBlock(ParamDIE, dwarf::DW_AT_location, Loc); 1141 } 1142 } else if (VP->getTag() == dwarf::DW_TAG_GNU_template_template_param) { 1143 assert(isa<MDString>(Val)); 1144 addString(ParamDIE, dwarf::DW_AT_GNU_template_name, 1145 cast<MDString>(Val)->getString()); 1146 } else if (VP->getTag() == dwarf::DW_TAG_GNU_template_parameter_pack) { 1147 addTemplateParams(ParamDIE, cast<MDTuple>(Val)); 1148 } 1149 } 1150 } 1151 1152 DIE *DwarfUnit::getOrCreateNameSpace(const DINamespace *NS) { 1153 // Construct the context before querying for the existence of the DIE in case 1154 // such construction creates the DIE. 1155 DIE *ContextDIE = getOrCreateContextDIE(NS->getScope()); 1156 1157 if (DIE *NDie = getDIE(NS)) 1158 return NDie; 1159 DIE &NDie = createAndAddDIE(dwarf::DW_TAG_namespace, *ContextDIE, NS); 1160 1161 StringRef Name = NS->getName(); 1162 if (!Name.empty()) 1163 addString(NDie, dwarf::DW_AT_name, NS->getName()); 1164 else 1165 Name = "(anonymous namespace)"; 1166 DD->addAccelNamespace(Name, NDie); 1167 addGlobalName(Name, NDie, NS->getScope()); 1168 if (NS->getExportSymbols()) 1169 addFlag(NDie, dwarf::DW_AT_export_symbols); 1170 return &NDie; 1171 } 1172 1173 DIE *DwarfUnit::getOrCreateModule(const DIModule *M) { 1174 // Construct the context before querying for the existence of the DIE in case 1175 // such construction creates the DIE. 1176 DIE *ContextDIE = getOrCreateContextDIE(M->getScope()); 1177 1178 if (DIE *MDie = getDIE(M)) 1179 return MDie; 1180 DIE &MDie = createAndAddDIE(dwarf::DW_TAG_module, *ContextDIE, M); 1181 1182 if (!M->getName().empty()) { 1183 addString(MDie, dwarf::DW_AT_name, M->getName()); 1184 addGlobalName(M->getName(), MDie, M->getScope()); 1185 } 1186 if (!M->getConfigurationMacros().empty()) 1187 addString(MDie, dwarf::DW_AT_LLVM_config_macros, 1188 M->getConfigurationMacros()); 1189 if (!M->getIncludePath().empty()) 1190 addString(MDie, dwarf::DW_AT_LLVM_include_path, M->getIncludePath()); 1191 if (!M->getISysRoot().empty()) 1192 addString(MDie, dwarf::DW_AT_LLVM_isysroot, M->getISysRoot()); 1193 1194 return &MDie; 1195 } 1196 1197 DIE *DwarfUnit::getOrCreateSubprogramDIE(const DISubprogram *SP, bool Minimal) { 1198 // Construct the context before querying for the existence of the DIE in case 1199 // such construction creates the DIE (as is the case for member function 1200 // declarations). 1201 DIE *ContextDIE = 1202 Minimal ? &getUnitDie() : getOrCreateContextDIE(resolve(SP->getScope())); 1203 1204 if (DIE *SPDie = getDIE(SP)) 1205 return SPDie; 1206 1207 if (auto *SPDecl = SP->getDeclaration()) { 1208 if (!Minimal) { 1209 // Add subprogram definitions to the CU die directly. 1210 ContextDIE = &getUnitDie(); 1211 // Build the decl now to ensure it precedes the definition. 1212 getOrCreateSubprogramDIE(SPDecl); 1213 } 1214 } 1215 1216 // DW_TAG_inlined_subroutine may refer to this DIE. 1217 DIE &SPDie = createAndAddDIE(dwarf::DW_TAG_subprogram, *ContextDIE, SP); 1218 1219 // Stop here and fill this in later, depending on whether or not this 1220 // subprogram turns out to have inlined instances or not. 1221 if (SP->isDefinition()) 1222 return &SPDie; 1223 1224 applySubprogramAttributes(SP, SPDie); 1225 return &SPDie; 1226 } 1227 1228 bool DwarfUnit::applySubprogramDefinitionAttributes(const DISubprogram *SP, 1229 DIE &SPDie) { 1230 DIE *DeclDie = nullptr; 1231 StringRef DeclLinkageName; 1232 if (auto *SPDecl = SP->getDeclaration()) { 1233 DeclDie = getDIE(SPDecl); 1234 assert(DeclDie && "This DIE should've already been constructed when the " 1235 "definition DIE was created in " 1236 "getOrCreateSubprogramDIE"); 1237 // Look at the Decl's linkage name only if we emitted it. 1238 if (DD->useAllLinkageNames()) 1239 DeclLinkageName = SPDecl->getLinkageName(); 1240 unsigned DeclID = getOrCreateSourceID(SPDecl->getFile()); 1241 unsigned DefID = getOrCreateSourceID(SP->getFile()); 1242 if (DeclID != DefID) 1243 addUInt(SPDie, dwarf::DW_AT_decl_file, None, DefID); 1244 1245 if (SP->getLine() != SPDecl->getLine()) 1246 addUInt(SPDie, dwarf::DW_AT_decl_line, None, SP->getLine()); 1247 } 1248 1249 // Add function template parameters. 1250 addTemplateParams(SPDie, SP->getTemplateParams()); 1251 1252 // Add the linkage name if we have one and it isn't in the Decl. 1253 StringRef LinkageName = SP->getLinkageName(); 1254 assert(((LinkageName.empty() || DeclLinkageName.empty()) || 1255 LinkageName == DeclLinkageName) && 1256 "decl has a linkage name and it is different"); 1257 if (DeclLinkageName.empty() && 1258 // Always emit it for abstract subprograms. 1259 (DD->useAllLinkageNames() || DU->getAbstractSPDies().lookup(SP))) 1260 addLinkageName(SPDie, LinkageName); 1261 1262 if (!DeclDie) 1263 return false; 1264 1265 // Refer to the function declaration where all the other attributes will be 1266 // found. 1267 addDIEEntry(SPDie, dwarf::DW_AT_specification, *DeclDie); 1268 return true; 1269 } 1270 1271 void DwarfUnit::applySubprogramAttributes(const DISubprogram *SP, DIE &SPDie, 1272 bool SkipSPAttributes) { 1273 // If -fdebug-info-for-profiling is enabled, need to emit the subprogram 1274 // and its source location. 1275 bool SkipSPSourceLocation = SkipSPAttributes && 1276 !CUNode->getDebugInfoForProfiling(); 1277 if (!SkipSPSourceLocation) 1278 if (applySubprogramDefinitionAttributes(SP, SPDie)) 1279 return; 1280 1281 // Constructors and operators for anonymous aggregates do not have names. 1282 if (!SP->getName().empty()) 1283 addString(SPDie, dwarf::DW_AT_name, SP->getName()); 1284 1285 if (!SkipSPSourceLocation) 1286 addSourceLine(SPDie, SP); 1287 1288 // Skip the rest of the attributes under -gmlt to save space. 1289 if (SkipSPAttributes) 1290 return; 1291 1292 // Add the prototype if we have a prototype and we have a C like 1293 // language. 1294 uint16_t Language = getLanguage(); 1295 if (SP->isPrototyped() && 1296 (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 || 1297 Language == dwarf::DW_LANG_ObjC)) 1298 addFlag(SPDie, dwarf::DW_AT_prototyped); 1299 1300 unsigned CC = 0; 1301 DITypeRefArray Args; 1302 if (const DISubroutineType *SPTy = SP->getType()) { 1303 Args = SPTy->getTypeArray(); 1304 CC = SPTy->getCC(); 1305 } 1306 1307 // Add a DW_AT_calling_convention if this has an explicit convention. 1308 if (CC && CC != dwarf::DW_CC_normal) 1309 addUInt(SPDie, dwarf::DW_AT_calling_convention, dwarf::DW_FORM_data1, CC); 1310 1311 // Add a return type. If this is a type like a C/C++ void type we don't add a 1312 // return type. 1313 if (Args.size()) 1314 if (auto Ty = resolve(Args[0])) 1315 addType(SPDie, Ty); 1316 1317 unsigned VK = SP->getVirtuality(); 1318 if (VK) { 1319 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, VK); 1320 if (SP->getVirtualIndex() != -1u) { 1321 DIELoc *Block = getDIELoc(); 1322 addUInt(*Block, dwarf::DW_FORM_data1, dwarf::DW_OP_constu); 1323 addUInt(*Block, dwarf::DW_FORM_udata, SP->getVirtualIndex()); 1324 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, Block); 1325 } 1326 ContainingTypeMap.insert( 1327 std::make_pair(&SPDie, resolve(SP->getContainingType()))); 1328 } 1329 1330 if (!SP->isDefinition()) { 1331 addFlag(SPDie, dwarf::DW_AT_declaration); 1332 1333 // Add arguments. Do not add arguments for subprogram definition. They will 1334 // be handled while processing variables. 1335 constructSubprogramArguments(SPDie, Args); 1336 } 1337 1338 addThrownTypes(SPDie, SP->getThrownTypes()); 1339 1340 if (SP->isArtificial()) 1341 addFlag(SPDie, dwarf::DW_AT_artificial); 1342 1343 if (!SP->isLocalToUnit()) 1344 addFlag(SPDie, dwarf::DW_AT_external); 1345 1346 if (DD->useAppleExtensionAttributes()) { 1347 if (SP->isOptimized()) 1348 addFlag(SPDie, dwarf::DW_AT_APPLE_optimized); 1349 1350 if (unsigned isa = Asm->getISAEncoding()) 1351 addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa); 1352 } 1353 1354 if (SP->isLValueReference()) 1355 addFlag(SPDie, dwarf::DW_AT_reference); 1356 1357 if (SP->isRValueReference()) 1358 addFlag(SPDie, dwarf::DW_AT_rvalue_reference); 1359 1360 if (SP->isNoReturn()) 1361 addFlag(SPDie, dwarf::DW_AT_noreturn); 1362 1363 if (SP->isProtected()) 1364 addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, 1365 dwarf::DW_ACCESS_protected); 1366 else if (SP->isPrivate()) 1367 addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, 1368 dwarf::DW_ACCESS_private); 1369 else if (SP->isPublic()) 1370 addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, 1371 dwarf::DW_ACCESS_public); 1372 1373 if (SP->isExplicit()) 1374 addFlag(SPDie, dwarf::DW_AT_explicit); 1375 1376 if (SP->isMainSubprogram()) 1377 addFlag(SPDie, dwarf::DW_AT_main_subprogram); 1378 } 1379 1380 void DwarfUnit::constructSubrangeDIE(DIE &Buffer, const DISubrange *SR, 1381 DIE *IndexTy) { 1382 DIE &DW_Subrange = createAndAddDIE(dwarf::DW_TAG_subrange_type, Buffer); 1383 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, *IndexTy); 1384 1385 // The LowerBound value defines the lower bounds which is typically zero for 1386 // C/C++. The Count value is the number of elements. Values are 64 bit. If 1387 // Count == -1 then the array is unbounded and we do not emit 1388 // DW_AT_lower_bound and DW_AT_count attributes. 1389 int64_t LowerBound = SR->getLowerBound(); 1390 int64_t DefaultLowerBound = getDefaultLowerBound(); 1391 int64_t Count = -1; 1392 if (auto *CI = SR->getCount().dyn_cast<ConstantInt*>()) 1393 Count = CI->getSExtValue(); 1394 1395 if (DefaultLowerBound == -1 || LowerBound != DefaultLowerBound) 1396 addUInt(DW_Subrange, dwarf::DW_AT_lower_bound, None, LowerBound); 1397 1398 if (auto *CV = SR->getCount().dyn_cast<DIVariable*>()) { 1399 if (auto *CountVarDIE = getDIE(CV)) 1400 addDIEEntry(DW_Subrange, dwarf::DW_AT_count, *CountVarDIE); 1401 } else if (Count != -1) 1402 addUInt(DW_Subrange, dwarf::DW_AT_count, None, Count); 1403 } 1404 1405 DIE *DwarfUnit::getIndexTyDie() { 1406 if (IndexTyDie) 1407 return IndexTyDie; 1408 // Construct an integer type to use for indexes. 1409 IndexTyDie = &createAndAddDIE(dwarf::DW_TAG_base_type, getUnitDie()); 1410 StringRef Name = "__ARRAY_SIZE_TYPE__"; 1411 addString(*IndexTyDie, dwarf::DW_AT_name, Name); 1412 addUInt(*IndexTyDie, dwarf::DW_AT_byte_size, None, sizeof(int64_t)); 1413 addUInt(*IndexTyDie, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1, 1414 dwarf::DW_ATE_unsigned); 1415 DD->addAccelType(Name, *IndexTyDie, /*Flags*/ 0); 1416 return IndexTyDie; 1417 } 1418 1419 /// Returns true if the vector's size differs from the sum of sizes of elements 1420 /// the user specified. This can occur if the vector has been rounded up to 1421 /// fit memory alignment constraints. 1422 static bool hasVectorBeenPadded(const DICompositeType *CTy) { 1423 assert(CTy && CTy->isVector() && "Composite type is not a vector"); 1424 const uint64_t ActualSize = CTy->getSizeInBits(); 1425 1426 // Obtain the size of each element in the vector. 1427 DIType *BaseTy = CTy->getBaseType().resolve(); 1428 assert(BaseTy && "Unknown vector element type."); 1429 const uint64_t ElementSize = BaseTy->getSizeInBits(); 1430 1431 // Locate the number of elements in the vector. 1432 const DINodeArray Elements = CTy->getElements(); 1433 assert(Elements.size() == 1 && 1434 Elements[0]->getTag() == dwarf::DW_TAG_subrange_type && 1435 "Invalid vector element array, expected one element of type subrange"); 1436 const auto Subrange = cast<DISubrange>(Elements[0]); 1437 const auto CI = Subrange->getCount().get<ConstantInt *>(); 1438 const int32_t NumVecElements = CI->getSExtValue(); 1439 1440 // Ensure we found the element count and that the actual size is wide 1441 // enough to contain the requested size. 1442 assert(ActualSize >= (NumVecElements * ElementSize) && "Invalid vector size"); 1443 return ActualSize != (NumVecElements * ElementSize); 1444 } 1445 1446 void DwarfUnit::constructArrayTypeDIE(DIE &Buffer, const DICompositeType *CTy) { 1447 if (CTy->isVector()) { 1448 addFlag(Buffer, dwarf::DW_AT_GNU_vector); 1449 if (hasVectorBeenPadded(CTy)) 1450 addUInt(Buffer, dwarf::DW_AT_byte_size, None, 1451 CTy->getSizeInBits() / CHAR_BIT); 1452 } 1453 1454 // Emit the element type. 1455 addType(Buffer, resolve(CTy->getBaseType())); 1456 1457 // Get an anonymous type for index type. 1458 // FIXME: This type should be passed down from the front end 1459 // as different languages may have different sizes for indexes. 1460 DIE *IdxTy = getIndexTyDie(); 1461 1462 // Add subranges to array type. 1463 DINodeArray Elements = CTy->getElements(); 1464 for (unsigned i = 0, N = Elements.size(); i < N; ++i) { 1465 // FIXME: Should this really be such a loose cast? 1466 if (auto *Element = dyn_cast_or_null<DINode>(Elements[i])) 1467 if (Element->getTag() == dwarf::DW_TAG_subrange_type) 1468 constructSubrangeDIE(Buffer, cast<DISubrange>(Element), IdxTy); 1469 } 1470 } 1471 1472 void DwarfUnit::constructEnumTypeDIE(DIE &Buffer, const DICompositeType *CTy) { 1473 const DIType *DTy = resolve(CTy->getBaseType()); 1474 bool IsUnsigned = DTy && isUnsignedDIType(DD, DTy); 1475 if (DTy) { 1476 if (DD->getDwarfVersion() >= 3) 1477 addType(Buffer, DTy); 1478 if (DD->getDwarfVersion() >= 4 && (CTy->getFlags() & DINode::FlagFixedEnum)) 1479 addFlag(Buffer, dwarf::DW_AT_enum_class); 1480 } 1481 1482 DINodeArray Elements = CTy->getElements(); 1483 1484 // Add enumerators to enumeration type. 1485 for (unsigned i = 0, N = Elements.size(); i < N; ++i) { 1486 auto *Enum = dyn_cast_or_null<DIEnumerator>(Elements[i]); 1487 if (Enum) { 1488 DIE &Enumerator = createAndAddDIE(dwarf::DW_TAG_enumerator, Buffer); 1489 StringRef Name = Enum->getName(); 1490 addString(Enumerator, dwarf::DW_AT_name, Name); 1491 auto Value = static_cast<uint64_t>(Enum->getValue()); 1492 addConstantValue(Enumerator, IsUnsigned, Value); 1493 } 1494 } 1495 } 1496 1497 void DwarfUnit::constructContainingTypeDIEs() { 1498 for (auto CI = ContainingTypeMap.begin(), CE = ContainingTypeMap.end(); 1499 CI != CE; ++CI) { 1500 DIE &SPDie = *CI->first; 1501 const DINode *D = CI->second; 1502 if (!D) 1503 continue; 1504 DIE *NDie = getDIE(D); 1505 if (!NDie) 1506 continue; 1507 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, *NDie); 1508 } 1509 } 1510 1511 DIE &DwarfUnit::constructMemberDIE(DIE &Buffer, const DIDerivedType *DT) { 1512 DIE &MemberDie = createAndAddDIE(DT->getTag(), Buffer); 1513 StringRef Name = DT->getName(); 1514 if (!Name.empty()) 1515 addString(MemberDie, dwarf::DW_AT_name, Name); 1516 1517 if (DIType *Resolved = resolve(DT->getBaseType())) 1518 addType(MemberDie, Resolved); 1519 1520 addSourceLine(MemberDie, DT); 1521 1522 if (DT->getTag() == dwarf::DW_TAG_inheritance && DT->isVirtual()) { 1523 1524 // For C++, virtual base classes are not at fixed offset. Use following 1525 // expression to extract appropriate offset from vtable. 1526 // BaseAddr = ObAddr + *((*ObAddr) - Offset) 1527 1528 DIELoc *VBaseLocationDie = new (DIEValueAllocator) DIELoc; 1529 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_dup); 1530 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref); 1531 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_constu); 1532 addUInt(*VBaseLocationDie, dwarf::DW_FORM_udata, DT->getOffsetInBits()); 1533 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_minus); 1534 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref); 1535 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus); 1536 1537 addBlock(MemberDie, dwarf::DW_AT_data_member_location, VBaseLocationDie); 1538 } else { 1539 uint64_t Size = DT->getSizeInBits(); 1540 uint64_t FieldSize = DD->getBaseTypeSize(DT); 1541 uint32_t AlignInBytes = DT->getAlignInBytes(); 1542 uint64_t OffsetInBytes; 1543 1544 bool IsBitfield = FieldSize && Size != FieldSize; 1545 if (IsBitfield) { 1546 // Handle bitfield, assume bytes are 8 bits. 1547 if (DD->useDWARF2Bitfields()) 1548 addUInt(MemberDie, dwarf::DW_AT_byte_size, None, FieldSize/8); 1549 addUInt(MemberDie, dwarf::DW_AT_bit_size, None, Size); 1550 1551 uint64_t Offset = DT->getOffsetInBits(); 1552 // We can't use DT->getAlignInBits() here: AlignInBits for member type 1553 // is non-zero if and only if alignment was forced (e.g. _Alignas()), 1554 // which can't be done with bitfields. Thus we use FieldSize here. 1555 uint32_t AlignInBits = FieldSize; 1556 uint32_t AlignMask = ~(AlignInBits - 1); 1557 // The bits from the start of the storage unit to the start of the field. 1558 uint64_t StartBitOffset = Offset - (Offset & AlignMask); 1559 // The byte offset of the field's aligned storage unit inside the struct. 1560 OffsetInBytes = (Offset - StartBitOffset) / 8; 1561 1562 if (DD->useDWARF2Bitfields()) { 1563 uint64_t HiMark = (Offset + FieldSize) & AlignMask; 1564 uint64_t FieldOffset = (HiMark - FieldSize); 1565 Offset -= FieldOffset; 1566 1567 // Maybe we need to work from the other end. 1568 if (Asm->getDataLayout().isLittleEndian()) 1569 Offset = FieldSize - (Offset + Size); 1570 1571 addUInt(MemberDie, dwarf::DW_AT_bit_offset, None, Offset); 1572 OffsetInBytes = FieldOffset >> 3; 1573 } else { 1574 addUInt(MemberDie, dwarf::DW_AT_data_bit_offset, None, Offset); 1575 } 1576 } else { 1577 // This is not a bitfield. 1578 OffsetInBytes = DT->getOffsetInBits() / 8; 1579 if (AlignInBytes) 1580 addUInt(MemberDie, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata, 1581 AlignInBytes); 1582 } 1583 1584 if (DD->getDwarfVersion() <= 2) { 1585 DIELoc *MemLocationDie = new (DIEValueAllocator) DIELoc; 1586 addUInt(*MemLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst); 1587 addUInt(*MemLocationDie, dwarf::DW_FORM_udata, OffsetInBytes); 1588 addBlock(MemberDie, dwarf::DW_AT_data_member_location, MemLocationDie); 1589 } else if (!IsBitfield || DD->useDWARF2Bitfields()) 1590 addUInt(MemberDie, dwarf::DW_AT_data_member_location, None, 1591 OffsetInBytes); 1592 } 1593 1594 if (DT->isProtected()) 1595 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, 1596 dwarf::DW_ACCESS_protected); 1597 else if (DT->isPrivate()) 1598 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, 1599 dwarf::DW_ACCESS_private); 1600 // Otherwise C++ member and base classes are considered public. 1601 else if (DT->isPublic()) 1602 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, 1603 dwarf::DW_ACCESS_public); 1604 if (DT->isVirtual()) 1605 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, 1606 dwarf::DW_VIRTUALITY_virtual); 1607 1608 // Objective-C properties. 1609 if (DINode *PNode = DT->getObjCProperty()) 1610 if (DIE *PDie = getDIE(PNode)) 1611 MemberDie.addValue(DIEValueAllocator, dwarf::DW_AT_APPLE_property, 1612 dwarf::DW_FORM_ref4, DIEEntry(*PDie)); 1613 1614 if (DT->isArtificial()) 1615 addFlag(MemberDie, dwarf::DW_AT_artificial); 1616 1617 return MemberDie; 1618 } 1619 1620 DIE *DwarfUnit::getOrCreateStaticMemberDIE(const DIDerivedType *DT) { 1621 if (!DT) 1622 return nullptr; 1623 1624 // Construct the context before querying for the existence of the DIE in case 1625 // such construction creates the DIE. 1626 DIE *ContextDIE = getOrCreateContextDIE(resolve(DT->getScope())); 1627 assert(dwarf::isType(ContextDIE->getTag()) && 1628 "Static member should belong to a type."); 1629 1630 if (DIE *StaticMemberDIE = getDIE(DT)) 1631 return StaticMemberDIE; 1632 1633 DIE &StaticMemberDIE = createAndAddDIE(DT->getTag(), *ContextDIE, DT); 1634 1635 const DIType *Ty = resolve(DT->getBaseType()); 1636 1637 addString(StaticMemberDIE, dwarf::DW_AT_name, DT->getName()); 1638 addType(StaticMemberDIE, Ty); 1639 addSourceLine(StaticMemberDIE, DT); 1640 addFlag(StaticMemberDIE, dwarf::DW_AT_external); 1641 addFlag(StaticMemberDIE, dwarf::DW_AT_declaration); 1642 1643 // FIXME: We could omit private if the parent is a class_type, and 1644 // public if the parent is something else. 1645 if (DT->isProtected()) 1646 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, 1647 dwarf::DW_ACCESS_protected); 1648 else if (DT->isPrivate()) 1649 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, 1650 dwarf::DW_ACCESS_private); 1651 else if (DT->isPublic()) 1652 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, 1653 dwarf::DW_ACCESS_public); 1654 1655 if (const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(DT->getConstant())) 1656 addConstantValue(StaticMemberDIE, CI, Ty); 1657 if (const ConstantFP *CFP = dyn_cast_or_null<ConstantFP>(DT->getConstant())) 1658 addConstantFPValue(StaticMemberDIE, CFP); 1659 1660 if (uint32_t AlignInBytes = DT->getAlignInBytes()) 1661 addUInt(StaticMemberDIE, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata, 1662 AlignInBytes); 1663 1664 return &StaticMemberDIE; 1665 } 1666 1667 void DwarfUnit::emitCommonHeader(bool UseOffsets, dwarf::UnitType UT) { 1668 // Emit size of content not including length itself 1669 Asm->OutStreamer->AddComment("Length of Unit"); 1670 Asm->emitInt32(getHeaderSize() + getUnitDie().getSize()); 1671 1672 Asm->OutStreamer->AddComment("DWARF version number"); 1673 unsigned Version = DD->getDwarfVersion(); 1674 Asm->emitInt16(Version); 1675 1676 // DWARF v5 reorders the address size and adds a unit type. 1677 if (Version >= 5) { 1678 Asm->OutStreamer->AddComment("DWARF Unit Type"); 1679 Asm->emitInt8(UT); 1680 Asm->OutStreamer->AddComment("Address Size (in bytes)"); 1681 Asm->emitInt8(Asm->MAI->getCodePointerSize()); 1682 } 1683 1684 // We share one abbreviations table across all units so it's always at the 1685 // start of the section. Use a relocatable offset where needed to ensure 1686 // linking doesn't invalidate that offset. 1687 Asm->OutStreamer->AddComment("Offset Into Abbrev. Section"); 1688 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering(); 1689 if (UseOffsets) 1690 Asm->emitInt32(0); 1691 else 1692 Asm->emitDwarfSymbolReference( 1693 TLOF.getDwarfAbbrevSection()->getBeginSymbol(), false); 1694 1695 if (Version <= 4) { 1696 Asm->OutStreamer->AddComment("Address Size (in bytes)"); 1697 Asm->emitInt8(Asm->MAI->getCodePointerSize()); 1698 } 1699 } 1700 1701 void DwarfTypeUnit::emitHeader(bool UseOffsets) { 1702 DwarfUnit::emitCommonHeader(UseOffsets, 1703 DD->useSplitDwarf() ? dwarf::DW_UT_split_type 1704 : dwarf::DW_UT_type); 1705 Asm->OutStreamer->AddComment("Type Signature"); 1706 Asm->OutStreamer->EmitIntValue(TypeSignature, sizeof(TypeSignature)); 1707 Asm->OutStreamer->AddComment("Type DIE Offset"); 1708 // In a skeleton type unit there is no type DIE so emit a zero offset. 1709 Asm->OutStreamer->EmitIntValue(Ty ? Ty->getOffset() : 0, 1710 sizeof(Ty->getOffset())); 1711 } 1712 1713 DIE::value_iterator 1714 DwarfUnit::addSectionDelta(DIE &Die, dwarf::Attribute Attribute, 1715 const MCSymbol *Hi, const MCSymbol *Lo) { 1716 return Die.addValue(DIEValueAllocator, Attribute, 1717 DD->getDwarfVersion() >= 4 ? dwarf::DW_FORM_sec_offset 1718 : dwarf::DW_FORM_data4, 1719 new (DIEValueAllocator) DIEDelta(Hi, Lo)); 1720 } 1721 1722 DIE::value_iterator 1723 DwarfUnit::addSectionLabel(DIE &Die, dwarf::Attribute Attribute, 1724 const MCSymbol *Label, const MCSymbol *Sec) { 1725 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections()) 1726 return addLabel(Die, Attribute, 1727 DD->getDwarfVersion() >= 4 ? dwarf::DW_FORM_sec_offset 1728 : dwarf::DW_FORM_data4, 1729 Label); 1730 return addSectionDelta(Die, Attribute, Label, Sec); 1731 } 1732 1733 bool DwarfTypeUnit::isDwoUnit() const { 1734 // Since there are no skeleton type units, all type units are dwo type units 1735 // when split DWARF is being used. 1736 return DD->useSplitDwarf(); 1737 } 1738 1739 void DwarfTypeUnit::addGlobalName(StringRef Name, const DIE &Die, 1740 const DIScope *Context) { 1741 getCU().addGlobalNameForTypeUnit(Name, Context); 1742 } 1743 1744 void DwarfTypeUnit::addGlobalType(const DIType *Ty, const DIE &Die, 1745 const DIScope *Context) { 1746 getCU().addGlobalTypeUnitType(Ty, Context); 1747 } 1748 1749 const MCSymbol *DwarfUnit::getCrossSectionRelativeBaseAddress() const { 1750 if (!Asm->MAI->doesDwarfUseRelocationsAcrossSections()) 1751 return nullptr; 1752 if (isDwoUnit()) 1753 return nullptr; 1754 return getSection()->getBeginSymbol(); 1755 } 1756 1757 void DwarfUnit::addStringOffsetsStart() { 1758 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering(); 1759 addSectionLabel(getUnitDie(), dwarf::DW_AT_str_offsets_base, 1760 DU->getStringOffsetsStartSym(), 1761 TLOF.getDwarfStrOffSection()->getBeginSymbol()); 1762 } 1763 1764 void DwarfUnit::addRnglistsBase() { 1765 assert(DD->getDwarfVersion() >= 5 && 1766 "DW_AT_rnglists_base requires DWARF version 5 or later"); 1767 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering(); 1768 addSectionLabel(getUnitDie(), dwarf::DW_AT_rnglists_base, 1769 DU->getRnglistsTableBaseSym(), 1770 TLOF.getDwarfRnglistsSection()->getBeginSymbol()); 1771 } 1772