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