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 "DwarfDebug.h" 17 #include "llvm/ADT/APFloat.h" 18 #include "llvm/IR/Constants.h" 19 #include "llvm/IR/DIBuilder.h" 20 #include "llvm/IR/DataLayout.h" 21 #include "llvm/IR/GlobalVariable.h" 22 #include "llvm/IR/Instructions.h" 23 #include "llvm/IR/Mangler.h" 24 #include "llvm/MC/MCAsmInfo.h" 25 #include "llvm/MC/MCContext.h" 26 #include "llvm/MC/MCSection.h" 27 #include "llvm/MC/MCStreamer.h" 28 #include "llvm/Support/CommandLine.h" 29 #include "llvm/Target/TargetFrameLowering.h" 30 #include "llvm/Target/TargetLoweringObjectFile.h" 31 #include "llvm/Target/TargetMachine.h" 32 #include "llvm/Target/TargetRegisterInfo.h" 33 34 using namespace llvm; 35 36 #define DEBUG_TYPE "dwarfdebug" 37 38 static cl::opt<bool> 39 GenerateDwarfTypeUnits("generate-type-units", cl::Hidden, 40 cl::desc("Generate DWARF4 type units."), 41 cl::init(false)); 42 43 /// Unit - Unit constructor. 44 DwarfUnit::DwarfUnit(unsigned UID, dwarf::Tag UnitTag, DICompileUnit Node, 45 AsmPrinter *A, DwarfDebug *DW, DwarfFile *DWU) 46 : UniqueID(UID), CUNode(Node), UnitDie(UnitTag), DebugInfoOffset(0), Asm(A), 47 DD(DW), DU(DWU), IndexTyDie(nullptr), Section(nullptr), 48 Skeleton(nullptr) { 49 assert(UnitTag == dwarf::DW_TAG_compile_unit || 50 UnitTag == dwarf::DW_TAG_type_unit); 51 DIEIntegerOne = new (DIEValueAllocator) DIEInteger(1); 52 } 53 54 DwarfCompileUnit::DwarfCompileUnit(unsigned UID, DICompileUnit Node, 55 AsmPrinter *A, DwarfDebug *DW, 56 DwarfFile *DWU) 57 : DwarfUnit(UID, dwarf::DW_TAG_compile_unit, Node, A, DW, DWU) { 58 insertDIE(Node, &getUnitDie()); 59 } 60 61 DwarfTypeUnit::DwarfTypeUnit(unsigned UID, DwarfCompileUnit &CU, AsmPrinter *A, 62 DwarfDebug *DW, DwarfFile *DWU, 63 MCDwarfDwoLineTable *SplitLineTable) 64 : DwarfUnit(UID, dwarf::DW_TAG_type_unit, CU.getCUNode(), A, DW, DWU), 65 CU(CU), SplitLineTable(SplitLineTable) { 66 if (SplitLineTable) 67 addSectionOffset(UnitDie, dwarf::DW_AT_stmt_list, 0); 68 } 69 70 /// ~Unit - Destructor for compile unit. 71 DwarfUnit::~DwarfUnit() { 72 for (unsigned j = 0, M = DIEBlocks.size(); j < M; ++j) 73 DIEBlocks[j]->~DIEBlock(); 74 for (unsigned j = 0, M = DIELocs.size(); j < M; ++j) 75 DIELocs[j]->~DIELoc(); 76 } 77 78 /// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug 79 /// information entry. 80 DIEEntry *DwarfUnit::createDIEEntry(DIE &Entry) { 81 DIEEntry *Value = new (DIEValueAllocator) DIEEntry(Entry); 82 return Value; 83 } 84 85 /// getDefaultLowerBound - Return the default lower bound for an array. If the 86 /// DWARF version doesn't handle the language, return -1. 87 int64_t DwarfUnit::getDefaultLowerBound() const { 88 switch (getLanguage()) { 89 default: 90 break; 91 92 case dwarf::DW_LANG_C89: 93 case dwarf::DW_LANG_C99: 94 case dwarf::DW_LANG_C: 95 case dwarf::DW_LANG_C_plus_plus: 96 case dwarf::DW_LANG_ObjC: 97 case dwarf::DW_LANG_ObjC_plus_plus: 98 return 0; 99 100 case dwarf::DW_LANG_Fortran77: 101 case dwarf::DW_LANG_Fortran90: 102 case dwarf::DW_LANG_Fortran95: 103 return 1; 104 105 // The languages below have valid values only if the DWARF version >= 4. 106 case dwarf::DW_LANG_Java: 107 case dwarf::DW_LANG_Python: 108 case dwarf::DW_LANG_UPC: 109 case dwarf::DW_LANG_D: 110 if (dwarf::DWARF_VERSION >= 4) 111 return 0; 112 break; 113 114 case dwarf::DW_LANG_Ada83: 115 case dwarf::DW_LANG_Ada95: 116 case dwarf::DW_LANG_Cobol74: 117 case dwarf::DW_LANG_Cobol85: 118 case dwarf::DW_LANG_Modula2: 119 case dwarf::DW_LANG_Pascal83: 120 case dwarf::DW_LANG_PLI: 121 if (dwarf::DWARF_VERSION >= 4) 122 return 1; 123 break; 124 } 125 126 return -1; 127 } 128 129 /// Check whether the DIE for this MDNode can be shared across CUs. 130 static bool isShareableAcrossCUs(DIDescriptor D) { 131 // When the MDNode can be part of the type system, the DIE can be shared 132 // across CUs. 133 // Combining type units and cross-CU DIE sharing is lower value (since 134 // cross-CU DIE sharing is used in LTO and removes type redundancy at that 135 // level already) but may be implementable for some value in projects 136 // building multiple independent libraries with LTO and then linking those 137 // together. 138 return (D.isType() || 139 (D.isSubprogram() && !DISubprogram(D).isDefinition())) && 140 !GenerateDwarfTypeUnits; 141 } 142 143 /// getDIE - Returns the debug information entry map slot for the 144 /// specified debug variable. We delegate the request to DwarfDebug 145 /// when the DIE for this MDNode can be shared across CUs. The mappings 146 /// will be kept in DwarfDebug for shareable DIEs. 147 DIE *DwarfUnit::getDIE(DIDescriptor D) const { 148 if (isShareableAcrossCUs(D)) 149 return DD->getDIE(D); 150 return MDNodeToDieMap.lookup(D); 151 } 152 153 /// insertDIE - Insert DIE into the map. We delegate the request to DwarfDebug 154 /// when the DIE for this MDNode can be shared across CUs. The mappings 155 /// will be kept in DwarfDebug for shareable DIEs. 156 void DwarfUnit::insertDIE(DIDescriptor Desc, DIE *D) { 157 if (isShareableAcrossCUs(Desc)) { 158 DD->insertDIE(Desc, D); 159 return; 160 } 161 MDNodeToDieMap.insert(std::make_pair(Desc, D)); 162 } 163 164 /// addFlag - Add a flag that is true. 165 void DwarfUnit::addFlag(DIE &Die, dwarf::Attribute Attribute) { 166 if (DD->getDwarfVersion() >= 4) 167 Die.addValue(Attribute, dwarf::DW_FORM_flag_present, DIEIntegerOne); 168 else 169 Die.addValue(Attribute, dwarf::DW_FORM_flag, DIEIntegerOne); 170 } 171 172 /// addUInt - Add an unsigned integer attribute data and value. 173 /// 174 void DwarfUnit::addUInt(DIE &Die, dwarf::Attribute Attribute, 175 Optional<dwarf::Form> Form, uint64_t Integer) { 176 if (!Form) 177 Form = DIEInteger::BestForm(false, Integer); 178 DIEValue *Value = Integer == 1 ? DIEIntegerOne : new (DIEValueAllocator) 179 DIEInteger(Integer); 180 Die.addValue(Attribute, *Form, Value); 181 } 182 183 void DwarfUnit::addUInt(DIE &Block, dwarf::Form Form, uint64_t Integer) { 184 addUInt(Block, (dwarf::Attribute)0, Form, Integer); 185 } 186 187 /// addSInt - Add an signed integer attribute data and value. 188 /// 189 void DwarfUnit::addSInt(DIE &Die, dwarf::Attribute Attribute, 190 Optional<dwarf::Form> Form, int64_t Integer) { 191 if (!Form) 192 Form = DIEInteger::BestForm(true, Integer); 193 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer); 194 Die.addValue(Attribute, *Form, Value); 195 } 196 197 void DwarfUnit::addSInt(DIELoc &Die, Optional<dwarf::Form> Form, 198 int64_t Integer) { 199 addSInt(Die, (dwarf::Attribute)0, Form, Integer); 200 } 201 202 /// addString - Add a string attribute data and value. We always emit a 203 /// reference to the string pool instead of immediate strings so that DIEs have 204 /// more predictable sizes. In the case of split dwarf we emit an index 205 /// into another table which gets us the static offset into the string 206 /// table. 207 void DwarfUnit::addString(DIE &Die, dwarf::Attribute Attribute, 208 StringRef String) { 209 210 if (!DD->useSplitDwarf()) 211 return addLocalString(Die, Attribute, String); 212 213 unsigned idx = DU->getStringPool().getIndex(*Asm, String); 214 DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx); 215 DIEValue *Str = new (DIEValueAllocator) DIEString(Value, String); 216 Die.addValue(Attribute, dwarf::DW_FORM_GNU_str_index, Str); 217 } 218 219 /// addLocalString - Add a string attribute data and value. This is guaranteed 220 /// to be in the local string pool instead of indirected. 221 void DwarfUnit::addLocalString(DIE &Die, dwarf::Attribute Attribute, 222 StringRef String) { 223 MCSymbol *Symb = DU->getStringPool().getSymbol(*Asm, String); 224 DIEValue *Value; 225 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections()) 226 Value = new (DIEValueAllocator) DIELabel(Symb); 227 else { 228 MCSymbol *StringPool = DU->getStringPool().getSectionSymbol(); 229 Value = new (DIEValueAllocator) DIEDelta(Symb, StringPool); 230 } 231 DIEValue *Str = new (DIEValueAllocator) DIEString(Value, String); 232 Die.addValue(Attribute, dwarf::DW_FORM_strp, Str); 233 } 234 235 /// addExpr - Add a Dwarf expression attribute data and value. 236 /// 237 void DwarfUnit::addExpr(DIELoc &Die, dwarf::Form Form, const MCExpr *Expr) { 238 DIEValue *Value = new (DIEValueAllocator) DIEExpr(Expr); 239 Die.addValue((dwarf::Attribute)0, Form, Value); 240 } 241 242 /// addLocationList - Add a Dwarf loclistptr attribute data and value. 243 /// 244 void DwarfUnit::addLocationList(DIE &Die, dwarf::Attribute Attribute, 245 unsigned Index) { 246 DIEValue *Value = new (DIEValueAllocator) DIELocList(Index); 247 dwarf::Form Form = DD->getDwarfVersion() >= 4 ? dwarf::DW_FORM_sec_offset 248 : dwarf::DW_FORM_data4; 249 Die.addValue(Attribute, Form, Value); 250 } 251 252 /// addLabel - Add a Dwarf label attribute data and value. 253 /// 254 void DwarfUnit::addLabel(DIE &Die, dwarf::Attribute Attribute, dwarf::Form Form, 255 const MCSymbol *Label) { 256 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label); 257 Die.addValue(Attribute, Form, Value); 258 } 259 260 void DwarfUnit::addLabel(DIELoc &Die, dwarf::Form Form, const MCSymbol *Label) { 261 addLabel(Die, (dwarf::Attribute)0, Form, Label); 262 } 263 264 /// addSectionLabel - Add a Dwarf section label attribute data and value. 265 /// 266 void DwarfUnit::addSectionLabel(DIE &Die, dwarf::Attribute Attribute, 267 const MCSymbol *Label) { 268 if (DD->getDwarfVersion() >= 4) 269 addLabel(Die, Attribute, dwarf::DW_FORM_sec_offset, Label); 270 else 271 addLabel(Die, Attribute, dwarf::DW_FORM_data4, Label); 272 } 273 274 /// addSectionOffset - Add an offset into a section attribute data and value. 275 /// 276 void DwarfUnit::addSectionOffset(DIE &Die, dwarf::Attribute Attribute, 277 uint64_t Integer) { 278 if (DD->getDwarfVersion() >= 4) 279 addUInt(Die, Attribute, dwarf::DW_FORM_sec_offset, Integer); 280 else 281 addUInt(Die, Attribute, dwarf::DW_FORM_data4, Integer); 282 } 283 284 /// addLabelAddress - Add a dwarf label attribute data and value using 285 /// DW_FORM_addr or DW_FORM_GNU_addr_index. 286 /// 287 void DwarfCompileUnit::addLabelAddress(DIE &Die, dwarf::Attribute Attribute, 288 const MCSymbol *Label) { 289 290 if (!DD->useSplitDwarf()) 291 return addLocalLabelAddress(Die, Attribute, Label); 292 293 if (Label) 294 DD->addArangeLabel(SymbolCU(this, Label)); 295 296 unsigned idx = DD->getAddressPool().getIndex(Label); 297 DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx); 298 Die.addValue(Attribute, dwarf::DW_FORM_GNU_addr_index, Value); 299 } 300 301 void DwarfCompileUnit::addLocalLabelAddress(DIE &Die, 302 dwarf::Attribute Attribute, 303 const MCSymbol *Label) { 304 if (Label) 305 DD->addArangeLabel(SymbolCU(this, Label)); 306 307 Die.addValue(Attribute, dwarf::DW_FORM_addr, 308 Label ? (DIEValue *)new (DIEValueAllocator) DIELabel(Label) 309 : new (DIEValueAllocator) DIEInteger(0)); 310 } 311 312 unsigned DwarfCompileUnit::getOrCreateSourceID(StringRef FileName, StringRef DirName) { 313 // If we print assembly, we can't separate .file entries according to 314 // compile units. Thus all files will belong to the default compile unit. 315 316 // FIXME: add a better feature test than hasRawTextSupport. Even better, 317 // extend .file to support this. 318 return Asm->OutStreamer.EmitDwarfFileDirective( 319 0, DirName, FileName, 320 Asm->OutStreamer.hasRawTextSupport() ? 0 : getUniqueID()); 321 } 322 323 unsigned DwarfTypeUnit::getOrCreateSourceID(StringRef FileName, StringRef DirName) { 324 return SplitLineTable ? SplitLineTable->getFile(DirName, FileName) 325 : getCU().getOrCreateSourceID(FileName, DirName); 326 } 327 328 /// addOpAddress - Add a dwarf op address data and value using the 329 /// form given and an op of either DW_FORM_addr or DW_FORM_GNU_addr_index. 330 /// 331 void DwarfUnit::addOpAddress(DIELoc &Die, const MCSymbol *Sym) { 332 if (!DD->useSplitDwarf()) { 333 addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_addr); 334 addLabel(Die, dwarf::DW_FORM_udata, Sym); 335 } else { 336 addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_addr_index); 337 addUInt(Die, dwarf::DW_FORM_GNU_addr_index, 338 DD->getAddressPool().getIndex(Sym)); 339 } 340 } 341 342 /// addSectionDelta - Add a section label delta attribute data and value. 343 /// 344 void DwarfUnit::addSectionDelta(DIE &Die, dwarf::Attribute Attribute, 345 const MCSymbol *Hi, const MCSymbol *Lo) { 346 DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo); 347 Die.addValue(Attribute, DD->getDwarfVersion() >= 4 ? dwarf::DW_FORM_sec_offset 348 : dwarf::DW_FORM_data4, 349 Value); 350 } 351 352 void DwarfUnit::addLabelDelta(DIE &Die, dwarf::Attribute Attribute, 353 const MCSymbol *Hi, const MCSymbol *Lo) { 354 DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo); 355 Die.addValue(Attribute, dwarf::DW_FORM_data4, Value); 356 } 357 358 /// addDIEEntry - Add a DIE attribute data and value. 359 /// 360 void DwarfUnit::addDIEEntry(DIE &Die, dwarf::Attribute Attribute, DIE &Entry) { 361 addDIEEntry(Die, Attribute, createDIEEntry(Entry)); 362 } 363 364 void DwarfUnit::addDIETypeSignature(DIE &Die, const DwarfTypeUnit &Type) { 365 // Flag the type unit reference as a declaration so that if it contains 366 // members (implicit special members, static data member definitions, member 367 // declarations for definitions in this CU, etc) consumers don't get confused 368 // and think this is a full definition. 369 addFlag(Die, dwarf::DW_AT_declaration); 370 371 Die.addValue(dwarf::DW_AT_signature, dwarf::DW_FORM_ref_sig8, 372 new (DIEValueAllocator) DIETypeSignature(Type)); 373 } 374 375 void DwarfUnit::addDIEEntry(DIE &Die, dwarf::Attribute Attribute, 376 DIEEntry *Entry) { 377 const DIE *DieCU = Die.getUnitOrNull(); 378 const DIE *EntryCU = Entry->getEntry().getUnitOrNull(); 379 if (!DieCU) 380 // We assume that Die belongs to this CU, if it is not linked to any CU yet. 381 DieCU = &getUnitDie(); 382 if (!EntryCU) 383 EntryCU = &getUnitDie(); 384 Die.addValue(Attribute, 385 EntryCU == DieCU ? dwarf::DW_FORM_ref4 : dwarf::DW_FORM_ref_addr, 386 Entry); 387 } 388 389 /// Create a DIE with the given Tag, add the DIE to its parent, and 390 /// call insertDIE if MD is not null. 391 DIE &DwarfUnit::createAndAddDIE(unsigned Tag, DIE &Parent, DIDescriptor N) { 392 assert(Tag != dwarf::DW_TAG_auto_variable && 393 Tag != dwarf::DW_TAG_arg_variable); 394 Parent.addChild(make_unique<DIE>((dwarf::Tag)Tag)); 395 DIE &Die = *Parent.getChildren().back(); 396 if (N) 397 insertDIE(N, &Die); 398 return Die; 399 } 400 401 /// addBlock - Add block data. 402 /// 403 void DwarfUnit::addBlock(DIE &Die, dwarf::Attribute Attribute, DIELoc *Loc) { 404 Loc->ComputeSize(Asm); 405 DIELocs.push_back(Loc); // Memoize so we can call the destructor later on. 406 Die.addValue(Attribute, Loc->BestForm(DD->getDwarfVersion()), Loc); 407 } 408 409 void DwarfUnit::addBlock(DIE &Die, dwarf::Attribute Attribute, 410 DIEBlock *Block) { 411 Block->ComputeSize(Asm); 412 DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on. 413 Die.addValue(Attribute, Block->BestForm(), Block); 414 } 415 416 /// addSourceLine - Add location information to specified debug information 417 /// entry. 418 void DwarfUnit::addSourceLine(DIE &Die, unsigned Line, StringRef File, 419 StringRef Directory) { 420 if (Line == 0) 421 return; 422 423 unsigned FileID = getOrCreateSourceID(File, Directory); 424 assert(FileID && "Invalid file id"); 425 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID); 426 addUInt(Die, dwarf::DW_AT_decl_line, None, Line); 427 } 428 429 /// addSourceLine - Add location information to specified debug information 430 /// entry. 431 void DwarfUnit::addSourceLine(DIE &Die, DIVariable V) { 432 assert(V.isVariable()); 433 434 addSourceLine(Die, V.getLineNumber(), V.getContext().getFilename(), 435 V.getContext().getDirectory()); 436 } 437 438 /// addSourceLine - Add location information to specified debug information 439 /// entry. 440 void DwarfUnit::addSourceLine(DIE &Die, DIGlobalVariable G) { 441 assert(G.isGlobalVariable()); 442 443 addSourceLine(Die, G.getLineNumber(), G.getFilename(), G.getDirectory()); 444 } 445 446 /// addSourceLine - Add location information to specified debug information 447 /// entry. 448 void DwarfUnit::addSourceLine(DIE &Die, DISubprogram SP) { 449 assert(SP.isSubprogram()); 450 451 addSourceLine(Die, SP.getLineNumber(), SP.getFilename(), SP.getDirectory()); 452 } 453 454 /// addSourceLine - Add location information to specified debug information 455 /// entry. 456 void DwarfUnit::addSourceLine(DIE &Die, DIType Ty) { 457 assert(Ty.isType()); 458 459 addSourceLine(Die, Ty.getLineNumber(), Ty.getFilename(), Ty.getDirectory()); 460 } 461 462 /// addSourceLine - Add location information to specified debug information 463 /// entry. 464 void DwarfUnit::addSourceLine(DIE &Die, DIObjCProperty Ty) { 465 assert(Ty.isObjCProperty()); 466 467 DIFile File = Ty.getFile(); 468 addSourceLine(Die, Ty.getLineNumber(), File.getFilename(), 469 File.getDirectory()); 470 } 471 472 /// addSourceLine - Add location information to specified debug information 473 /// entry. 474 void DwarfUnit::addSourceLine(DIE &Die, DINameSpace NS) { 475 assert(NS.Verify()); 476 477 addSourceLine(Die, NS.getLineNumber(), NS.getFilename(), NS.getDirectory()); 478 } 479 480 /// addVariableAddress - Add DW_AT_location attribute for a 481 /// DbgVariable based on provided MachineLocation. 482 void DwarfUnit::addVariableAddress(const DbgVariable &DV, DIE &Die, 483 MachineLocation Location) { 484 if (DV.variableHasComplexAddress()) 485 addComplexAddress(DV, Die, dwarf::DW_AT_location, Location); 486 else if (DV.isBlockByrefVariable()) 487 addBlockByrefAddress(DV, Die, dwarf::DW_AT_location, Location); 488 else 489 addAddress(Die, dwarf::DW_AT_location, Location, 490 DV.getVariable().isIndirect()); 491 } 492 493 /// addRegisterOp - Add register operand. 494 void DwarfUnit::addRegisterOp(DIELoc &TheDie, unsigned Reg) { 495 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo(); 496 int DWReg = RI->getDwarfRegNum(Reg, false); 497 bool isSubRegister = DWReg < 0; 498 499 unsigned Idx = 0; 500 501 // Go up the super-register chain until we hit a valid dwarf register number. 502 for (MCSuperRegIterator SR(Reg, RI); SR.isValid() && DWReg < 0; ++SR) { 503 DWReg = RI->getDwarfRegNum(*SR, false); 504 if (DWReg >= 0) 505 Idx = RI->getSubRegIndex(*SR, Reg); 506 } 507 508 if (DWReg < 0) { 509 DEBUG(dbgs() << "Invalid Dwarf register number.\n"); 510 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_nop); 511 return; 512 } 513 514 // Emit register 515 if (DWReg < 32) 516 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + DWReg); 517 else { 518 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_regx); 519 addUInt(TheDie, dwarf::DW_FORM_udata, DWReg); 520 } 521 522 // Emit Mask 523 if (isSubRegister) { 524 unsigned Size = RI->getSubRegIdxSize(Idx); 525 unsigned Offset = RI->getSubRegIdxOffset(Idx); 526 if (Offset > 0) { 527 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_bit_piece); 528 addUInt(TheDie, dwarf::DW_FORM_data1, Size); 529 addUInt(TheDie, dwarf::DW_FORM_data1, Offset); 530 } else { 531 unsigned ByteSize = Size / 8; // Assuming 8 bits per byte. 532 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_piece); 533 addUInt(TheDie, dwarf::DW_FORM_data1, ByteSize); 534 } 535 } 536 } 537 538 /// addRegisterOffset - Add register offset. 539 void DwarfUnit::addRegisterOffset(DIELoc &TheDie, unsigned Reg, 540 int64_t Offset) { 541 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo(); 542 unsigned DWReg = RI->getDwarfRegNum(Reg, false); 543 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo(); 544 if (Reg == TRI->getFrameRegister(*Asm->MF)) 545 // If variable offset is based in frame register then use fbreg. 546 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_fbreg); 547 else if (DWReg < 32) 548 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + DWReg); 549 else { 550 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx); 551 addUInt(TheDie, dwarf::DW_FORM_udata, DWReg); 552 } 553 addSInt(TheDie, dwarf::DW_FORM_sdata, Offset); 554 } 555 556 /// addAddress - Add an address attribute to a die based on the location 557 /// provided. 558 void DwarfUnit::addAddress(DIE &Die, dwarf::Attribute Attribute, 559 const MachineLocation &Location, bool Indirect) { 560 DIELoc *Loc = new (DIEValueAllocator) DIELoc(); 561 562 if (Location.isReg() && !Indirect) 563 addRegisterOp(*Loc, Location.getReg()); 564 else { 565 addRegisterOffset(*Loc, Location.getReg(), Location.getOffset()); 566 if (Indirect && !Location.isReg()) { 567 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_deref); 568 } 569 } 570 571 // Now attach the location information to the DIE. 572 addBlock(Die, Attribute, Loc); 573 } 574 575 /// addComplexAddress - Start with the address based on the location provided, 576 /// and generate the DWARF information necessary to find the actual variable 577 /// given the extra address information encoded in the DbgVariable, starting 578 /// from the starting location. Add the DWARF information to the die. 579 /// 580 void DwarfUnit::addComplexAddress(const DbgVariable &DV, DIE &Die, 581 dwarf::Attribute Attribute, 582 const MachineLocation &Location) { 583 DIELoc *Loc = new (DIEValueAllocator) DIELoc(); 584 unsigned N = DV.getNumAddrElements(); 585 unsigned i = 0; 586 if (Location.isReg()) { 587 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) { 588 // If first address element is OpPlus then emit 589 // DW_OP_breg + Offset instead of DW_OP_reg + Offset. 590 addRegisterOffset(*Loc, Location.getReg(), DV.getAddrElement(1)); 591 i = 2; 592 } else 593 addRegisterOp(*Loc, Location.getReg()); 594 } else 595 addRegisterOffset(*Loc, Location.getReg(), Location.getOffset()); 596 597 for (; i < N; ++i) { 598 uint64_t Element = DV.getAddrElement(i); 599 if (Element == DIBuilder::OpPlus) { 600 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst); 601 addUInt(*Loc, dwarf::DW_FORM_udata, DV.getAddrElement(++i)); 602 } else if (Element == DIBuilder::OpDeref) { 603 if (!Location.isReg()) 604 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_deref); 605 } else 606 llvm_unreachable("unknown DIBuilder Opcode"); 607 } 608 609 // Now attach the location information to the DIE. 610 addBlock(Die, Attribute, Loc); 611 } 612 613 /* Byref variables, in Blocks, are declared by the programmer as "SomeType 614 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and 615 gives the variable VarName either the struct, or a pointer to the struct, as 616 its type. This is necessary for various behind-the-scenes things the 617 compiler needs to do with by-reference variables in Blocks. 618 619 However, as far as the original *programmer* is concerned, the variable 620 should still have type 'SomeType', as originally declared. 621 622 The function getBlockByrefType dives into the __Block_byref_x_VarName 623 struct to find the original type of the variable, which is then assigned to 624 the variable's Debug Information Entry as its real type. So far, so good. 625 However now the debugger will expect the variable VarName to have the type 626 SomeType. So we need the location attribute for the variable to be an 627 expression that explains to the debugger how to navigate through the 628 pointers and struct to find the actual variable of type SomeType. 629 630 The following function does just that. We start by getting 631 the "normal" location for the variable. This will be the location 632 of either the struct __Block_byref_x_VarName or the pointer to the 633 struct __Block_byref_x_VarName. 634 635 The struct will look something like: 636 637 struct __Block_byref_x_VarName { 638 ... <various fields> 639 struct __Block_byref_x_VarName *forwarding; 640 ... <various other fields> 641 SomeType VarName; 642 ... <maybe more fields> 643 }; 644 645 If we are given the struct directly (as our starting point) we 646 need to tell the debugger to: 647 648 1). Add the offset of the forwarding field. 649 650 2). Follow that pointer to get the real __Block_byref_x_VarName 651 struct to use (the real one may have been copied onto the heap). 652 653 3). Add the offset for the field VarName, to find the actual variable. 654 655 If we started with a pointer to the struct, then we need to 656 dereference that pointer first, before the other steps. 657 Translating this into DWARF ops, we will need to append the following 658 to the current location description for the variable: 659 660 DW_OP_deref -- optional, if we start with a pointer 661 DW_OP_plus_uconst <forward_fld_offset> 662 DW_OP_deref 663 DW_OP_plus_uconst <varName_fld_offset> 664 665 That is what this function does. */ 666 667 /// addBlockByrefAddress - Start with the address based on the location 668 /// provided, and generate the DWARF information necessary to find the 669 /// actual Block variable (navigating the Block struct) based on the 670 /// starting location. Add the DWARF information to the die. For 671 /// more information, read large comment just above here. 672 /// 673 void DwarfUnit::addBlockByrefAddress(const DbgVariable &DV, DIE &Die, 674 dwarf::Attribute Attribute, 675 const MachineLocation &Location) { 676 DIType Ty = DV.getType(); 677 DIType TmpTy = Ty; 678 uint16_t Tag = Ty.getTag(); 679 bool isPointer = false; 680 681 StringRef varName = DV.getName(); 682 683 if (Tag == dwarf::DW_TAG_pointer_type) { 684 DIDerivedType DTy(Ty); 685 TmpTy = resolve(DTy.getTypeDerivedFrom()); 686 isPointer = true; 687 } 688 689 DICompositeType blockStruct(TmpTy); 690 691 // Find the __forwarding field and the variable field in the __Block_byref 692 // struct. 693 DIArray Fields = blockStruct.getTypeArray(); 694 DIDerivedType varField; 695 DIDerivedType forwardingField; 696 697 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) { 698 DIDerivedType DT(Fields.getElement(i)); 699 StringRef fieldName = DT.getName(); 700 if (fieldName == "__forwarding") 701 forwardingField = DT; 702 else if (fieldName == varName) 703 varField = DT; 704 } 705 706 // Get the offsets for the forwarding field and the variable field. 707 unsigned forwardingFieldOffset = forwardingField.getOffsetInBits() >> 3; 708 unsigned varFieldOffset = varField.getOffsetInBits() >> 2; 709 710 // Decode the original location, and use that as the start of the byref 711 // variable's location. 712 DIELoc *Loc = new (DIEValueAllocator) DIELoc(); 713 714 if (Location.isReg()) 715 addRegisterOp(*Loc, Location.getReg()); 716 else 717 addRegisterOffset(*Loc, Location.getReg(), Location.getOffset()); 718 719 // If we started with a pointer to the __Block_byref... struct, then 720 // the first thing we need to do is dereference the pointer (DW_OP_deref). 721 if (isPointer) 722 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_deref); 723 724 // Next add the offset for the '__forwarding' field: 725 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in 726 // adding the offset if it's 0. 727 if (forwardingFieldOffset > 0) { 728 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst); 729 addUInt(*Loc, dwarf::DW_FORM_udata, forwardingFieldOffset); 730 } 731 732 // Now dereference the __forwarding field to get to the real __Block_byref 733 // struct: DW_OP_deref. 734 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_deref); 735 736 // Now that we've got the real __Block_byref... struct, add the offset 737 // for the variable's field to get to the location of the actual variable: 738 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0. 739 if (varFieldOffset > 0) { 740 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst); 741 addUInt(*Loc, dwarf::DW_FORM_udata, varFieldOffset); 742 } 743 744 // Now attach the location information to the DIE. 745 addBlock(Die, Attribute, Loc); 746 } 747 748 /// isTypeSigned - Return true if the type is signed. 749 static bool isTypeSigned(DwarfDebug *DD, DIType Ty, int *SizeInBits) { 750 if (Ty.isDerivedType()) 751 return isTypeSigned(DD, DD->resolve(DIDerivedType(Ty).getTypeDerivedFrom()), 752 SizeInBits); 753 if (Ty.isBasicType()) 754 if (DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed || 755 DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed_char) { 756 *SizeInBits = Ty.getSizeInBits(); 757 return true; 758 } 759 return false; 760 } 761 762 /// Return true if type encoding is unsigned. 763 static bool isUnsignedDIType(DwarfDebug *DD, DIType Ty) { 764 DIDerivedType DTy(Ty); 765 if (DTy.isDerivedType()) 766 return isUnsignedDIType(DD, DD->resolve(DTy.getTypeDerivedFrom())); 767 768 DIBasicType BTy(Ty); 769 if (BTy.isBasicType()) { 770 unsigned Encoding = BTy.getEncoding(); 771 if (Encoding == dwarf::DW_ATE_unsigned || 772 Encoding == dwarf::DW_ATE_unsigned_char || 773 Encoding == dwarf::DW_ATE_boolean) 774 return true; 775 } 776 return false; 777 } 778 779 /// If this type is derived from a base type then return base type size. 780 static uint64_t getBaseTypeSize(DwarfDebug *DD, DIDerivedType Ty) { 781 unsigned Tag = Ty.getTag(); 782 783 if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef && 784 Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type && 785 Tag != dwarf::DW_TAG_restrict_type) 786 return Ty.getSizeInBits(); 787 788 DIType BaseType = DD->resolve(Ty.getTypeDerivedFrom()); 789 790 // If this type is not derived from any type or the type is a declaration then 791 // take conservative approach. 792 if (!BaseType.isValid() || BaseType.isForwardDecl()) 793 return Ty.getSizeInBits(); 794 795 // If this is a derived type, go ahead and get the base type, unless it's a 796 // reference then it's just the size of the field. Pointer types have no need 797 // of this since they're a different type of qualification on the type. 798 if (BaseType.getTag() == dwarf::DW_TAG_reference_type || 799 BaseType.getTag() == dwarf::DW_TAG_rvalue_reference_type) 800 return Ty.getSizeInBits(); 801 802 if (BaseType.isDerivedType()) 803 return getBaseTypeSize(DD, DIDerivedType(BaseType)); 804 805 return BaseType.getSizeInBits(); 806 } 807 808 /// addConstantValue - Add constant value entry in variable DIE. 809 void DwarfUnit::addConstantValue(DIE &Die, const MachineOperand &MO, 810 DIType Ty) { 811 // FIXME: This is a bit conservative/simple - it emits negative values at 812 // their maximum bit width which is a bit unfortunate (& doesn't prefer 813 // udata/sdata over dataN as suggested by the DWARF spec) 814 assert(MO.isImm() && "Invalid machine operand!"); 815 int SizeInBits = -1; 816 bool SignedConstant = isTypeSigned(DD, Ty, &SizeInBits); 817 dwarf::Form Form; 818 819 // If we're a signed constant definitely use sdata. 820 if (SignedConstant) { 821 addSInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, MO.getImm()); 822 return; 823 } 824 825 // Else use data for now unless it's larger than we can deal with. 826 switch (SizeInBits) { 827 case 8: 828 Form = dwarf::DW_FORM_data1; 829 break; 830 case 16: 831 Form = dwarf::DW_FORM_data2; 832 break; 833 case 32: 834 Form = dwarf::DW_FORM_data4; 835 break; 836 case 64: 837 Form = dwarf::DW_FORM_data8; 838 break; 839 default: 840 Form = dwarf::DW_FORM_udata; 841 addUInt(Die, dwarf::DW_AT_const_value, Form, MO.getImm()); 842 return; 843 } 844 addUInt(Die, dwarf::DW_AT_const_value, Form, MO.getImm()); 845 } 846 847 /// addConstantFPValue - Add constant value entry in variable DIE. 848 void DwarfUnit::addConstantFPValue(DIE &Die, const MachineOperand &MO) { 849 assert(MO.isFPImm() && "Invalid machine operand!"); 850 DIEBlock *Block = new (DIEValueAllocator) DIEBlock(); 851 APFloat FPImm = MO.getFPImm()->getValueAPF(); 852 853 // Get the raw data form of the floating point. 854 const APInt FltVal = FPImm.bitcastToAPInt(); 855 const char *FltPtr = (const char *)FltVal.getRawData(); 856 857 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte. 858 bool LittleEndian = Asm->getDataLayout().isLittleEndian(); 859 int Incr = (LittleEndian ? 1 : -1); 860 int Start = (LittleEndian ? 0 : NumBytes - 1); 861 int Stop = (LittleEndian ? NumBytes : -1); 862 863 // Output the constant to DWARF one byte at a time. 864 for (; Start != Stop; Start += Incr) 865 addUInt(*Block, dwarf::DW_FORM_data1, (unsigned char)0xFF & FltPtr[Start]); 866 867 addBlock(Die, dwarf::DW_AT_const_value, Block); 868 } 869 870 /// addConstantFPValue - Add constant value entry in variable DIE. 871 void DwarfUnit::addConstantFPValue(DIE &Die, const ConstantFP *CFP) { 872 // Pass this down to addConstantValue as an unsigned bag of bits. 873 addConstantValue(Die, CFP->getValueAPF().bitcastToAPInt(), true); 874 } 875 876 /// addConstantValue - Add constant value entry in variable DIE. 877 void DwarfUnit::addConstantValue(DIE &Die, const ConstantInt *CI, 878 bool Unsigned) { 879 addConstantValue(Die, CI->getValue(), Unsigned); 880 } 881 882 // addConstantValue - Add constant value entry in variable DIE. 883 void DwarfUnit::addConstantValue(DIE &Die, const APInt &Val, bool Unsigned) { 884 unsigned CIBitWidth = Val.getBitWidth(); 885 if (CIBitWidth <= 64) { 886 // If we're a signed constant definitely use sdata. 887 if (!Unsigned) { 888 addSInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, 889 Val.getSExtValue()); 890 return; 891 } 892 893 // Else use data for now unless it's larger than we can deal with. 894 dwarf::Form Form; 895 switch (CIBitWidth) { 896 case 8: 897 Form = dwarf::DW_FORM_data1; 898 break; 899 case 16: 900 Form = dwarf::DW_FORM_data2; 901 break; 902 case 32: 903 Form = dwarf::DW_FORM_data4; 904 break; 905 case 64: 906 Form = dwarf::DW_FORM_data8; 907 break; 908 default: 909 addUInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_udata, 910 Val.getZExtValue()); 911 return; 912 } 913 addUInt(Die, dwarf::DW_AT_const_value, Form, Val.getZExtValue()); 914 return; 915 } 916 917 DIEBlock *Block = new (DIEValueAllocator) DIEBlock(); 918 919 // Get the raw data form of the large APInt. 920 const uint64_t *Ptr64 = Val.getRawData(); 921 922 int NumBytes = Val.getBitWidth() / 8; // 8 bits per byte. 923 bool LittleEndian = Asm->getDataLayout().isLittleEndian(); 924 925 // Output the constant to DWARF one byte at a time. 926 for (int i = 0; i < NumBytes; i++) { 927 uint8_t c; 928 if (LittleEndian) 929 c = Ptr64[i / 8] >> (8 * (i & 7)); 930 else 931 c = Ptr64[(NumBytes - 1 - i) / 8] >> (8 * ((NumBytes - 1 - i) & 7)); 932 addUInt(*Block, dwarf::DW_FORM_data1, c); 933 } 934 935 addBlock(Die, dwarf::DW_AT_const_value, Block); 936 } 937 938 /// addTemplateParams - Add template parameters into buffer. 939 void DwarfUnit::addTemplateParams(DIE &Buffer, DIArray TParams) { 940 // Add template parameters. 941 for (unsigned i = 0, e = TParams.getNumElements(); i != e; ++i) { 942 DIDescriptor Element = TParams.getElement(i); 943 if (Element.isTemplateTypeParameter()) 944 constructTemplateTypeParameterDIE(Buffer, 945 DITemplateTypeParameter(Element)); 946 else if (Element.isTemplateValueParameter()) 947 constructTemplateValueParameterDIE(Buffer, 948 DITemplateValueParameter(Element)); 949 } 950 } 951 952 /// getOrCreateContextDIE - Get context owner's DIE. 953 DIE *DwarfUnit::getOrCreateContextDIE(DIScope Context) { 954 if (!Context || Context.isFile()) 955 return &getUnitDie(); 956 if (Context.isType()) 957 return getOrCreateTypeDIE(DIType(Context)); 958 if (Context.isNameSpace()) 959 return getOrCreateNameSpace(DINameSpace(Context)); 960 if (Context.isSubprogram()) 961 return getOrCreateSubprogramDIE(DISubprogram(Context)); 962 return getDIE(Context); 963 } 964 965 DIE *DwarfUnit::createTypeDIE(DICompositeType Ty) { 966 DIScope Context = resolve(Ty.getContext()); 967 DIE *ContextDIE = getOrCreateContextDIE(Context); 968 969 if (DIE *TyDIE = getDIE(Ty)) 970 return TyDIE; 971 972 // Create new type. 973 DIE &TyDIE = createAndAddDIE(Ty.getTag(), *ContextDIE, Ty); 974 975 constructTypeDIE(TyDIE, Ty); 976 977 updateAcceleratorTables(Context, Ty, TyDIE); 978 return &TyDIE; 979 } 980 981 /// getOrCreateTypeDIE - Find existing DIE or create new DIE for the 982 /// given DIType. 983 DIE *DwarfUnit::getOrCreateTypeDIE(const MDNode *TyNode) { 984 if (!TyNode) 985 return nullptr; 986 987 DIType Ty(TyNode); 988 assert(Ty.isType()); 989 assert(Ty == resolve(Ty.getRef()) && 990 "type was not uniqued, possible ODR violation."); 991 992 // DW_TAG_restrict_type is not supported in DWARF2 993 if (Ty.getTag() == dwarf::DW_TAG_restrict_type && DD->getDwarfVersion() <= 2) 994 return getOrCreateTypeDIE(resolve(DIDerivedType(Ty).getTypeDerivedFrom())); 995 996 // Construct the context before querying for the existence of the DIE in case 997 // such construction creates the DIE. 998 DIScope Context = resolve(Ty.getContext()); 999 DIE *ContextDIE = getOrCreateContextDIE(Context); 1000 assert(ContextDIE); 1001 1002 if (DIE *TyDIE = getDIE(Ty)) 1003 return TyDIE; 1004 1005 // Create new type. 1006 DIE &TyDIE = createAndAddDIE(Ty.getTag(), *ContextDIE, Ty); 1007 1008 updateAcceleratorTables(Context, Ty, TyDIE); 1009 1010 if (Ty.isBasicType()) 1011 constructTypeDIE(TyDIE, DIBasicType(Ty)); 1012 else if (Ty.isCompositeType()) { 1013 DICompositeType CTy(Ty); 1014 if (GenerateDwarfTypeUnits && !Ty.isForwardDecl()) 1015 if (MDString *TypeId = CTy.getIdentifier()) { 1016 DD->addDwarfTypeUnitType(getCU(), TypeId->getString(), TyDIE, CTy); 1017 // Skip updating the accelerator tables since this is not the full type. 1018 return &TyDIE; 1019 } 1020 constructTypeDIE(TyDIE, CTy); 1021 } else { 1022 assert(Ty.isDerivedType() && "Unknown kind of DIType"); 1023 constructTypeDIE(TyDIE, DIDerivedType(Ty)); 1024 } 1025 1026 return &TyDIE; 1027 } 1028 1029 void DwarfUnit::updateAcceleratorTables(DIScope Context, DIType Ty, 1030 const DIE &TyDIE) { 1031 if (!Ty.getName().empty() && !Ty.isForwardDecl()) { 1032 bool IsImplementation = 0; 1033 if (Ty.isCompositeType()) { 1034 DICompositeType CT(Ty); 1035 // A runtime language of 0 actually means C/C++ and that any 1036 // non-negative value is some version of Objective-C/C++. 1037 IsImplementation = (CT.getRunTimeLang() == 0) || CT.isObjcClassComplete(); 1038 } 1039 unsigned Flags = IsImplementation ? dwarf::DW_FLAG_type_implementation : 0; 1040 DD->addAccelType(Ty.getName(), TyDIE, Flags); 1041 1042 if ((!Context || Context.isCompileUnit() || Context.isFile() || 1043 Context.isNameSpace()) && 1044 getCUNode().getEmissionKind() != DIBuilder::LineTablesOnly) 1045 GlobalTypes[getParentContextString(Context) + Ty.getName().str()] = 1046 &TyDIE; 1047 } 1048 } 1049 1050 /// addType - Add a new type attribute to the specified entity. 1051 void DwarfUnit::addType(DIE &Entity, DIType Ty, dwarf::Attribute Attribute) { 1052 assert(Ty && "Trying to add a type that doesn't exist?"); 1053 1054 // Check for pre-existence. 1055 DIEEntry *Entry = getDIEEntry(Ty); 1056 // If it exists then use the existing value. 1057 if (Entry) { 1058 addDIEEntry(Entity, Attribute, Entry); 1059 return; 1060 } 1061 1062 // Construct type. 1063 DIE *Buffer = getOrCreateTypeDIE(Ty); 1064 1065 // Set up proxy. 1066 Entry = createDIEEntry(*Buffer); 1067 insertDIEEntry(Ty, Entry); 1068 addDIEEntry(Entity, Attribute, Entry); 1069 } 1070 1071 /// addGlobalName - Add a new global name to the compile unit. 1072 void DwarfUnit::addGlobalName(StringRef Name, DIE &Die, DIScope Context) { 1073 if (getCUNode().getEmissionKind() == DIBuilder::LineTablesOnly) 1074 return; 1075 std::string FullName = getParentContextString(Context) + Name.str(); 1076 GlobalNames[FullName] = &Die; 1077 } 1078 1079 /// getParentContextString - Walks the metadata parent chain in a language 1080 /// specific manner (using the compile unit language) and returns 1081 /// it as a string. This is done at the metadata level because DIEs may 1082 /// not currently have been added to the parent context and walking the 1083 /// DIEs looking for names is more expensive than walking the metadata. 1084 std::string DwarfUnit::getParentContextString(DIScope Context) const { 1085 if (!Context) 1086 return ""; 1087 1088 // FIXME: Decide whether to implement this for non-C++ languages. 1089 if (getLanguage() != dwarf::DW_LANG_C_plus_plus) 1090 return ""; 1091 1092 std::string CS; 1093 SmallVector<DIScope, 1> Parents; 1094 while (!Context.isCompileUnit()) { 1095 Parents.push_back(Context); 1096 if (Context.getContext()) 1097 Context = resolve(Context.getContext()); 1098 else 1099 // Structure, etc types will have a NULL context if they're at the top 1100 // level. 1101 break; 1102 } 1103 1104 // Reverse iterate over our list to go from the outermost construct to the 1105 // innermost. 1106 for (SmallVectorImpl<DIScope>::reverse_iterator I = Parents.rbegin(), 1107 E = Parents.rend(); 1108 I != E; ++I) { 1109 DIScope Ctx = *I; 1110 StringRef Name = Ctx.getName(); 1111 if (!Name.empty()) { 1112 CS += Name; 1113 CS += "::"; 1114 } 1115 } 1116 return CS; 1117 } 1118 1119 /// constructTypeDIE - Construct basic type die from DIBasicType. 1120 void DwarfUnit::constructTypeDIE(DIE &Buffer, DIBasicType BTy) { 1121 // Get core information. 1122 StringRef Name = BTy.getName(); 1123 // Add name if not anonymous or intermediate type. 1124 if (!Name.empty()) 1125 addString(Buffer, dwarf::DW_AT_name, Name); 1126 1127 // An unspecified type only has a name attribute. 1128 if (BTy.getTag() == dwarf::DW_TAG_unspecified_type) 1129 return; 1130 1131 addUInt(Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1, 1132 BTy.getEncoding()); 1133 1134 uint64_t Size = BTy.getSizeInBits() >> 3; 1135 addUInt(Buffer, dwarf::DW_AT_byte_size, None, Size); 1136 } 1137 1138 /// constructTypeDIE - Construct derived type die from DIDerivedType. 1139 void DwarfUnit::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) { 1140 // Get core information. 1141 StringRef Name = DTy.getName(); 1142 uint64_t Size = DTy.getSizeInBits() >> 3; 1143 uint16_t Tag = Buffer.getTag(); 1144 1145 // Map to main type, void will not have a type. 1146 DIType FromTy = resolve(DTy.getTypeDerivedFrom()); 1147 if (FromTy) 1148 addType(Buffer, FromTy); 1149 1150 // Add name if not anonymous or intermediate type. 1151 if (!Name.empty()) 1152 addString(Buffer, dwarf::DW_AT_name, Name); 1153 1154 // Add size if non-zero (derived types might be zero-sized.) 1155 if (Size && Tag != dwarf::DW_TAG_pointer_type) 1156 addUInt(Buffer, dwarf::DW_AT_byte_size, None, Size); 1157 1158 if (Tag == dwarf::DW_TAG_ptr_to_member_type) 1159 addDIEEntry(Buffer, dwarf::DW_AT_containing_type, 1160 *getOrCreateTypeDIE(resolve(DTy.getClassType()))); 1161 // Add source line info if available and TyDesc is not a forward declaration. 1162 if (!DTy.isForwardDecl()) 1163 addSourceLine(Buffer, DTy); 1164 } 1165 1166 /// constructSubprogramArguments - Construct function argument DIEs. 1167 void DwarfUnit::constructSubprogramArguments(DIE &Buffer, DIArray Args) { 1168 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) { 1169 DIDescriptor Ty = Args.getElement(i); 1170 if (Ty.isUnspecifiedParameter()) { 1171 assert(i == N-1 && "Unspecified parameter must be the last argument"); 1172 createAndAddDIE(dwarf::DW_TAG_unspecified_parameters, Buffer); 1173 } else { 1174 DIE &Arg = createAndAddDIE(dwarf::DW_TAG_formal_parameter, Buffer); 1175 addType(Arg, DIType(Ty)); 1176 if (DIType(Ty).isArtificial()) 1177 addFlag(Arg, dwarf::DW_AT_artificial); 1178 } 1179 } 1180 } 1181 1182 /// constructTypeDIE - Construct type DIE from DICompositeType. 1183 void DwarfUnit::constructTypeDIE(DIE &Buffer, DICompositeType CTy) { 1184 // Add name if not anonymous or intermediate type. 1185 StringRef Name = CTy.getName(); 1186 1187 uint64_t Size = CTy.getSizeInBits() >> 3; 1188 uint16_t Tag = Buffer.getTag(); 1189 1190 switch (Tag) { 1191 case dwarf::DW_TAG_array_type: 1192 constructArrayTypeDIE(Buffer, CTy); 1193 break; 1194 case dwarf::DW_TAG_enumeration_type: 1195 constructEnumTypeDIE(Buffer, CTy); 1196 break; 1197 case dwarf::DW_TAG_subroutine_type: { 1198 // Add return type. A void return won't have a type. 1199 DIArray Elements = CTy.getTypeArray(); 1200 DIType RTy(Elements.getElement(0)); 1201 if (RTy) 1202 addType(Buffer, RTy); 1203 1204 bool isPrototyped = true; 1205 if (Elements.getNumElements() == 2 && 1206 Elements.getElement(1).isUnspecifiedParameter()) 1207 isPrototyped = false; 1208 1209 constructSubprogramArguments(Buffer, Elements); 1210 1211 // Add prototype flag if we're dealing with a C language and the 1212 // function has been prototyped. 1213 uint16_t Language = getLanguage(); 1214 if (isPrototyped && 1215 (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 || 1216 Language == dwarf::DW_LANG_ObjC)) 1217 addFlag(Buffer, dwarf::DW_AT_prototyped); 1218 1219 if (CTy.isLValueReference()) 1220 addFlag(Buffer, dwarf::DW_AT_reference); 1221 1222 if (CTy.isRValueReference()) 1223 addFlag(Buffer, dwarf::DW_AT_rvalue_reference); 1224 } break; 1225 case dwarf::DW_TAG_structure_type: 1226 case dwarf::DW_TAG_union_type: 1227 case dwarf::DW_TAG_class_type: { 1228 // Add elements to structure type. 1229 DIArray Elements = CTy.getTypeArray(); 1230 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) { 1231 DIDescriptor Element = Elements.getElement(i); 1232 if (Element.isSubprogram()) 1233 getOrCreateSubprogramDIE(DISubprogram(Element)); 1234 else if (Element.isDerivedType()) { 1235 DIDerivedType DDTy(Element); 1236 if (DDTy.getTag() == dwarf::DW_TAG_friend) { 1237 DIE &ElemDie = createAndAddDIE(dwarf::DW_TAG_friend, Buffer); 1238 addType(ElemDie, resolve(DDTy.getTypeDerivedFrom()), 1239 dwarf::DW_AT_friend); 1240 } else if (DDTy.isStaticMember()) { 1241 getOrCreateStaticMemberDIE(DDTy); 1242 } else { 1243 constructMemberDIE(Buffer, DDTy); 1244 } 1245 } else if (Element.isObjCProperty()) { 1246 DIObjCProperty Property(Element); 1247 DIE &ElemDie = createAndAddDIE(Property.getTag(), Buffer); 1248 StringRef PropertyName = Property.getObjCPropertyName(); 1249 addString(ElemDie, dwarf::DW_AT_APPLE_property_name, PropertyName); 1250 if (Property.getType()) 1251 addType(ElemDie, Property.getType()); 1252 addSourceLine(ElemDie, Property); 1253 StringRef GetterName = Property.getObjCPropertyGetterName(); 1254 if (!GetterName.empty()) 1255 addString(ElemDie, dwarf::DW_AT_APPLE_property_getter, GetterName); 1256 StringRef SetterName = Property.getObjCPropertySetterName(); 1257 if (!SetterName.empty()) 1258 addString(ElemDie, dwarf::DW_AT_APPLE_property_setter, SetterName); 1259 unsigned PropertyAttributes = 0; 1260 if (Property.isReadOnlyObjCProperty()) 1261 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readonly; 1262 if (Property.isReadWriteObjCProperty()) 1263 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readwrite; 1264 if (Property.isAssignObjCProperty()) 1265 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_assign; 1266 if (Property.isRetainObjCProperty()) 1267 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_retain; 1268 if (Property.isCopyObjCProperty()) 1269 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_copy; 1270 if (Property.isNonAtomicObjCProperty()) 1271 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_nonatomic; 1272 if (PropertyAttributes) 1273 addUInt(ElemDie, dwarf::DW_AT_APPLE_property_attribute, None, 1274 PropertyAttributes); 1275 1276 DIEEntry *Entry = getDIEEntry(Element); 1277 if (!Entry) { 1278 Entry = createDIEEntry(ElemDie); 1279 insertDIEEntry(Element, Entry); 1280 } 1281 } else 1282 continue; 1283 } 1284 1285 if (CTy.isAppleBlockExtension()) 1286 addFlag(Buffer, dwarf::DW_AT_APPLE_block); 1287 1288 DICompositeType ContainingType(resolve(CTy.getContainingType())); 1289 if (ContainingType) 1290 addDIEEntry(Buffer, dwarf::DW_AT_containing_type, 1291 *getOrCreateTypeDIE(ContainingType)); 1292 1293 if (CTy.isObjcClassComplete()) 1294 addFlag(Buffer, dwarf::DW_AT_APPLE_objc_complete_type); 1295 1296 // Add template parameters to a class, structure or union types. 1297 // FIXME: The support isn't in the metadata for this yet. 1298 if (Tag == dwarf::DW_TAG_class_type || 1299 Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type) 1300 addTemplateParams(Buffer, CTy.getTemplateParams()); 1301 1302 break; 1303 } 1304 default: 1305 break; 1306 } 1307 1308 // Add name if not anonymous or intermediate type. 1309 if (!Name.empty()) 1310 addString(Buffer, dwarf::DW_AT_name, Name); 1311 1312 if (Tag == dwarf::DW_TAG_enumeration_type || 1313 Tag == dwarf::DW_TAG_class_type || Tag == dwarf::DW_TAG_structure_type || 1314 Tag == dwarf::DW_TAG_union_type) { 1315 // Add size if non-zero (derived types might be zero-sized.) 1316 // TODO: Do we care about size for enum forward declarations? 1317 if (Size) 1318 addUInt(Buffer, dwarf::DW_AT_byte_size, None, Size); 1319 else if (!CTy.isForwardDecl()) 1320 // Add zero size if it is not a forward declaration. 1321 addUInt(Buffer, dwarf::DW_AT_byte_size, None, 0); 1322 1323 // If we're a forward decl, say so. 1324 if (CTy.isForwardDecl()) 1325 addFlag(Buffer, dwarf::DW_AT_declaration); 1326 1327 // Add source line info if available. 1328 if (!CTy.isForwardDecl()) 1329 addSourceLine(Buffer, CTy); 1330 1331 // No harm in adding the runtime language to the declaration. 1332 unsigned RLang = CTy.getRunTimeLang(); 1333 if (RLang) 1334 addUInt(Buffer, dwarf::DW_AT_APPLE_runtime_class, dwarf::DW_FORM_data1, 1335 RLang); 1336 } 1337 } 1338 1339 /// constructTemplateTypeParameterDIE - Construct new DIE for the given 1340 /// DITemplateTypeParameter. 1341 void DwarfUnit::constructTemplateTypeParameterDIE(DIE &Buffer, 1342 DITemplateTypeParameter TP) { 1343 DIE &ParamDIE = 1344 createAndAddDIE(dwarf::DW_TAG_template_type_parameter, Buffer); 1345 // Add the type if it exists, it could be void and therefore no type. 1346 if (TP.getType()) 1347 addType(ParamDIE, resolve(TP.getType())); 1348 if (!TP.getName().empty()) 1349 addString(ParamDIE, dwarf::DW_AT_name, TP.getName()); 1350 } 1351 1352 /// constructTemplateValueParameterDIE - Construct new DIE for the given 1353 /// DITemplateValueParameter. 1354 void 1355 DwarfUnit::constructTemplateValueParameterDIE(DIE &Buffer, 1356 DITemplateValueParameter VP) { 1357 DIE &ParamDIE = createAndAddDIE(VP.getTag(), Buffer); 1358 1359 // Add the type if there is one, template template and template parameter 1360 // packs will not have a type. 1361 if (VP.getTag() == dwarf::DW_TAG_template_value_parameter) 1362 addType(ParamDIE, resolve(VP.getType())); 1363 if (!VP.getName().empty()) 1364 addString(ParamDIE, dwarf::DW_AT_name, VP.getName()); 1365 if (Value *Val = VP.getValue()) { 1366 if (ConstantInt *CI = dyn_cast<ConstantInt>(Val)) 1367 addConstantValue(ParamDIE, CI, 1368 isUnsignedDIType(DD, resolve(VP.getType()))); 1369 else if (GlobalValue *GV = dyn_cast<GlobalValue>(Val)) { 1370 // For declaration non-type template parameters (such as global values and 1371 // functions) 1372 DIELoc *Loc = new (DIEValueAllocator) DIELoc(); 1373 addOpAddress(*Loc, Asm->getSymbol(GV)); 1374 // Emit DW_OP_stack_value to use the address as the immediate value of the 1375 // parameter, rather than a pointer to it. 1376 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_stack_value); 1377 addBlock(ParamDIE, dwarf::DW_AT_location, Loc); 1378 } else if (VP.getTag() == dwarf::DW_TAG_GNU_template_template_param) { 1379 assert(isa<MDString>(Val)); 1380 addString(ParamDIE, dwarf::DW_AT_GNU_template_name, 1381 cast<MDString>(Val)->getString()); 1382 } else if (VP.getTag() == dwarf::DW_TAG_GNU_template_parameter_pack) { 1383 assert(isa<MDNode>(Val)); 1384 DIArray A(cast<MDNode>(Val)); 1385 addTemplateParams(ParamDIE, A); 1386 } 1387 } 1388 } 1389 1390 /// getOrCreateNameSpace - Create a DIE for DINameSpace. 1391 DIE *DwarfUnit::getOrCreateNameSpace(DINameSpace NS) { 1392 // Construct the context before querying for the existence of the DIE in case 1393 // such construction creates the DIE. 1394 DIE *ContextDIE = getOrCreateContextDIE(NS.getContext()); 1395 1396 if (DIE *NDie = getDIE(NS)) 1397 return NDie; 1398 DIE &NDie = createAndAddDIE(dwarf::DW_TAG_namespace, *ContextDIE, NS); 1399 1400 if (!NS.getName().empty()) { 1401 addString(NDie, dwarf::DW_AT_name, NS.getName()); 1402 DD->addAccelNamespace(NS.getName(), NDie); 1403 addGlobalName(NS.getName(), NDie, NS.getContext()); 1404 } else 1405 DD->addAccelNamespace("(anonymous namespace)", NDie); 1406 addSourceLine(NDie, NS); 1407 return &NDie; 1408 } 1409 1410 /// getOrCreateSubprogramDIE - Create new DIE using SP. 1411 DIE *DwarfUnit::getOrCreateSubprogramDIE(DISubprogram SP) { 1412 // Construct the context before querying for the existence of the DIE in case 1413 // such construction creates the DIE (as is the case for member function 1414 // declarations). 1415 DIScope Context = resolve(SP.getContext()); 1416 DIE *ContextDIE = getOrCreateContextDIE(Context); 1417 1418 // Unique declarations based on the ODR, where applicable. 1419 SP = DISubprogram(DD->resolve(SP.getRef())); 1420 assert(SP.Verify()); 1421 1422 if (DIE *SPDie = getDIE(SP)) 1423 return SPDie; 1424 1425 DISubprogram SPDecl = SP.getFunctionDeclaration(); 1426 if (SPDecl.isSubprogram()) 1427 // Add subprogram definitions to the CU die directly. 1428 ContextDIE = &getUnitDie(); 1429 1430 // DW_TAG_inlined_subroutine may refer to this DIE. 1431 DIE &SPDie = createAndAddDIE(dwarf::DW_TAG_subprogram, *ContextDIE, SP); 1432 1433 DIE *DeclDie = nullptr; 1434 if (SPDecl.isSubprogram()) 1435 DeclDie = getOrCreateSubprogramDIE(SPDecl); 1436 1437 // Add function template parameters. 1438 addTemplateParams(SPDie, SP.getTemplateParams()); 1439 1440 if (DeclDie) 1441 // Refer function declaration directly. 1442 addDIEEntry(SPDie, dwarf::DW_AT_specification, *DeclDie); 1443 1444 // Add the linkage name if we have one and it isn't in the Decl. 1445 StringRef LinkageName = SP.getLinkageName(); 1446 if (!LinkageName.empty()) { 1447 if (SPDecl.isSubprogram() && !SPDecl.getLinkageName().empty()) 1448 assert(SPDecl.getLinkageName() == SP.getLinkageName() && 1449 "decl has a linkage name and it is different"); 1450 else 1451 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name, 1452 GlobalValue::getRealLinkageName(LinkageName)); 1453 } 1454 1455 // If this DIE is going to refer declaration info using AT_specification 1456 // then there is no need to add other attributes. 1457 if (DeclDie) 1458 return &SPDie; 1459 1460 // Constructors and operators for anonymous aggregates do not have names. 1461 if (!SP.getName().empty()) 1462 addString(SPDie, dwarf::DW_AT_name, SP.getName()); 1463 1464 addSourceLine(SPDie, SP); 1465 1466 // Add the prototype if we have a prototype and we have a C like 1467 // language. 1468 uint16_t Language = getLanguage(); 1469 if (SP.isPrototyped() && 1470 (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 || 1471 Language == dwarf::DW_LANG_ObjC)) 1472 addFlag(SPDie, dwarf::DW_AT_prototyped); 1473 1474 DICompositeType SPTy = SP.getType(); 1475 assert(SPTy.getTag() == dwarf::DW_TAG_subroutine_type && 1476 "the type of a subprogram should be a subroutine"); 1477 1478 DIArray Args = SPTy.getTypeArray(); 1479 // Add a return type. If this is a type like a C/C++ void type we don't add a 1480 // return type. 1481 if (Args.getElement(0)) 1482 addType(SPDie, DIType(Args.getElement(0))); 1483 1484 unsigned VK = SP.getVirtuality(); 1485 if (VK) { 1486 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, VK); 1487 DIELoc *Block = getDIELoc(); 1488 addUInt(*Block, dwarf::DW_FORM_data1, dwarf::DW_OP_constu); 1489 addUInt(*Block, dwarf::DW_FORM_udata, SP.getVirtualIndex()); 1490 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, Block); 1491 ContainingTypeMap.insert( 1492 std::make_pair(&SPDie, resolve(SP.getContainingType()))); 1493 } 1494 1495 if (!SP.isDefinition()) { 1496 addFlag(SPDie, dwarf::DW_AT_declaration); 1497 1498 // Add arguments. Do not add arguments for subprogram definition. They will 1499 // be handled while processing variables. 1500 constructSubprogramArguments(SPDie, Args); 1501 } 1502 1503 if (SP.isArtificial()) 1504 addFlag(SPDie, dwarf::DW_AT_artificial); 1505 1506 if (!SP.isLocalToUnit()) 1507 addFlag(SPDie, dwarf::DW_AT_external); 1508 1509 if (SP.isOptimized()) 1510 addFlag(SPDie, dwarf::DW_AT_APPLE_optimized); 1511 1512 if (unsigned isa = Asm->getISAEncoding()) { 1513 addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa); 1514 } 1515 1516 if (SP.isLValueReference()) 1517 addFlag(SPDie, dwarf::DW_AT_reference); 1518 1519 if (SP.isRValueReference()) 1520 addFlag(SPDie, dwarf::DW_AT_rvalue_reference); 1521 1522 if (SP.isProtected()) 1523 addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, 1524 dwarf::DW_ACCESS_protected); 1525 else if (SP.isPrivate()) 1526 addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, 1527 dwarf::DW_ACCESS_private); 1528 else 1529 addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, 1530 dwarf::DW_ACCESS_public); 1531 1532 if (SP.isExplicit()) 1533 addFlag(SPDie, dwarf::DW_AT_explicit); 1534 1535 return &SPDie; 1536 } 1537 1538 // Return const expression if value is a GEP to access merged global 1539 // constant. e.g. 1540 // i8* getelementptr ({ i8, i8, i8, i8 }* @_MergedGlobals, i32 0, i32 0) 1541 static const ConstantExpr *getMergedGlobalExpr(const Value *V) { 1542 const ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(V); 1543 if (!CE || CE->getNumOperands() != 3 || 1544 CE->getOpcode() != Instruction::GetElementPtr) 1545 return nullptr; 1546 1547 // First operand points to a global struct. 1548 Value *Ptr = CE->getOperand(0); 1549 if (!isa<GlobalValue>(Ptr) || 1550 !isa<StructType>(cast<PointerType>(Ptr->getType())->getElementType())) 1551 return nullptr; 1552 1553 // Second operand is zero. 1554 const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(CE->getOperand(1)); 1555 if (!CI || !CI->isZero()) 1556 return nullptr; 1557 1558 // Third operand is offset. 1559 if (!isa<ConstantInt>(CE->getOperand(2))) 1560 return nullptr; 1561 1562 return CE; 1563 } 1564 1565 /// createGlobalVariableDIE - create global variable DIE. 1566 void DwarfCompileUnit::createGlobalVariableDIE(DIGlobalVariable GV) { 1567 // Check for pre-existence. 1568 if (getDIE(GV)) 1569 return; 1570 1571 assert(GV.isGlobalVariable()); 1572 1573 DIScope GVContext = GV.getContext(); 1574 DIType GTy = DD->resolve(GV.getType()); 1575 1576 // If this is a static data member definition, some attributes belong 1577 // to the declaration DIE. 1578 DIE *VariableDIE = nullptr; 1579 bool IsStaticMember = false; 1580 DIDerivedType SDMDecl = GV.getStaticDataMemberDeclaration(); 1581 if (SDMDecl.Verify()) { 1582 assert(SDMDecl.isStaticMember() && "Expected static member decl"); 1583 // We need the declaration DIE that is in the static member's class. 1584 VariableDIE = getOrCreateStaticMemberDIE(SDMDecl); 1585 IsStaticMember = true; 1586 } 1587 1588 // If this is not a static data member definition, create the variable 1589 // DIE and add the initial set of attributes to it. 1590 if (!VariableDIE) { 1591 // Construct the context before querying for the existence of the DIE in 1592 // case such construction creates the DIE. 1593 DIE *ContextDIE = getOrCreateContextDIE(GVContext); 1594 1595 // Add to map. 1596 VariableDIE = &createAndAddDIE(GV.getTag(), *ContextDIE, GV); 1597 1598 // Add name and type. 1599 addString(*VariableDIE, dwarf::DW_AT_name, GV.getDisplayName()); 1600 addType(*VariableDIE, GTy); 1601 1602 // Add scoping info. 1603 if (!GV.isLocalToUnit()) 1604 addFlag(*VariableDIE, dwarf::DW_AT_external); 1605 1606 // Add line number info. 1607 addSourceLine(*VariableDIE, GV); 1608 } 1609 1610 // Add location. 1611 bool addToAccelTable = false; 1612 DIE *VariableSpecDIE = nullptr; 1613 bool isGlobalVariable = GV.getGlobal() != nullptr; 1614 if (isGlobalVariable) { 1615 addToAccelTable = true; 1616 DIELoc *Loc = new (DIEValueAllocator) DIELoc(); 1617 const MCSymbol *Sym = Asm->getSymbol(GV.getGlobal()); 1618 if (GV.getGlobal()->isThreadLocal()) { 1619 // FIXME: Make this work with -gsplit-dwarf. 1620 unsigned PointerSize = Asm->getDataLayout().getPointerSize(); 1621 assert((PointerSize == 4 || PointerSize == 8) && 1622 "Add support for other sizes if necessary"); 1623 // Based on GCC's support for TLS: 1624 if (!DD->useSplitDwarf()) { 1625 // 1) Start with a constNu of the appropriate pointer size 1626 addUInt(*Loc, dwarf::DW_FORM_data1, 1627 PointerSize == 4 ? dwarf::DW_OP_const4u : dwarf::DW_OP_const8u); 1628 // 2) containing the (relocated) offset of the TLS variable 1629 // within the module's TLS block. 1630 addExpr(*Loc, dwarf::DW_FORM_udata, 1631 Asm->getObjFileLowering().getDebugThreadLocalSymbol(Sym)); 1632 } else { 1633 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_const_index); 1634 addUInt(*Loc, dwarf::DW_FORM_udata, 1635 DD->getAddressPool().getIndex(Sym, /* TLS */ true)); 1636 } 1637 // 3) followed by a custom OP to make the debugger do a TLS lookup. 1638 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_push_tls_address); 1639 } else { 1640 DD->addArangeLabel(SymbolCU(this, Sym)); 1641 addOpAddress(*Loc, Sym); 1642 } 1643 // Do not create specification DIE if context is either compile unit 1644 // or a subprogram. 1645 if (GVContext && GV.isDefinition() && !GVContext.isCompileUnit() && 1646 !GVContext.isFile() && !DD->isSubprogramContext(GVContext)) { 1647 // Create specification DIE. 1648 VariableSpecDIE = &createAndAddDIE(dwarf::DW_TAG_variable, UnitDie); 1649 addDIEEntry(*VariableSpecDIE, dwarf::DW_AT_specification, *VariableDIE); 1650 addBlock(*VariableSpecDIE, dwarf::DW_AT_location, Loc); 1651 // A static member's declaration is already flagged as such. 1652 if (!SDMDecl.Verify()) 1653 addFlag(*VariableDIE, dwarf::DW_AT_declaration); 1654 } else { 1655 addBlock(*VariableDIE, dwarf::DW_AT_location, Loc); 1656 } 1657 // Add the linkage name. 1658 StringRef LinkageName = GV.getLinkageName(); 1659 if (!LinkageName.empty()) 1660 // From DWARF4: DIEs to which DW_AT_linkage_name may apply include: 1661 // TAG_common_block, TAG_constant, TAG_entry_point, TAG_subprogram and 1662 // TAG_variable. 1663 addString(IsStaticMember && VariableSpecDIE ? *VariableSpecDIE 1664 : *VariableDIE, 1665 DD->getDwarfVersion() >= 4 ? dwarf::DW_AT_linkage_name 1666 : dwarf::DW_AT_MIPS_linkage_name, 1667 GlobalValue::getRealLinkageName(LinkageName)); 1668 } else if (const ConstantInt *CI = 1669 dyn_cast_or_null<ConstantInt>(GV.getConstant())) { 1670 // AT_const_value was added when the static member was created. To avoid 1671 // emitting AT_const_value multiple times, we only add AT_const_value when 1672 // it is not a static member. 1673 if (!IsStaticMember) 1674 addConstantValue(*VariableDIE, CI, isUnsignedDIType(DD, GTy)); 1675 } else if (const ConstantExpr *CE = getMergedGlobalExpr(GV->getOperand(11))) { 1676 addToAccelTable = true; 1677 // GV is a merged global. 1678 DIELoc *Loc = new (DIEValueAllocator) DIELoc(); 1679 Value *Ptr = CE->getOperand(0); 1680 MCSymbol *Sym = Asm->getSymbol(cast<GlobalValue>(Ptr)); 1681 DD->addArangeLabel(SymbolCU(this, Sym)); 1682 addOpAddress(*Loc, Sym); 1683 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_constu); 1684 SmallVector<Value *, 3> Idx(CE->op_begin() + 1, CE->op_end()); 1685 addUInt(*Loc, dwarf::DW_FORM_udata, 1686 Asm->getDataLayout().getIndexedOffset(Ptr->getType(), Idx)); 1687 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_plus); 1688 addBlock(*VariableDIE, dwarf::DW_AT_location, Loc); 1689 } 1690 1691 if (addToAccelTable) { 1692 DIE &AddrDIE = VariableSpecDIE ? *VariableSpecDIE : *VariableDIE; 1693 DD->addAccelName(GV.getName(), AddrDIE); 1694 1695 // If the linkage name is different than the name, go ahead and output 1696 // that as well into the name table. 1697 if (GV.getLinkageName() != "" && GV.getName() != GV.getLinkageName()) 1698 DD->addAccelName(GV.getLinkageName(), AddrDIE); 1699 } 1700 1701 if (!GV.isLocalToUnit()) 1702 addGlobalName(GV.getName(), 1703 VariableSpecDIE ? *VariableSpecDIE : *VariableDIE, 1704 GV.getContext()); 1705 } 1706 1707 /// constructSubrangeDIE - Construct subrange DIE from DISubrange. 1708 void DwarfUnit::constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy) { 1709 DIE &DW_Subrange = createAndAddDIE(dwarf::DW_TAG_subrange_type, Buffer); 1710 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, *IndexTy); 1711 1712 // The LowerBound value defines the lower bounds which is typically zero for 1713 // C/C++. The Count value is the number of elements. Values are 64 bit. If 1714 // Count == -1 then the array is unbounded and we do not emit 1715 // DW_AT_lower_bound and DW_AT_upper_bound attributes. If LowerBound == 0 and 1716 // Count == 0, then the array has zero elements in which case we do not emit 1717 // an upper bound. 1718 int64_t LowerBound = SR.getLo(); 1719 int64_t DefaultLowerBound = getDefaultLowerBound(); 1720 int64_t Count = SR.getCount(); 1721 1722 if (DefaultLowerBound == -1 || LowerBound != DefaultLowerBound) 1723 addUInt(DW_Subrange, dwarf::DW_AT_lower_bound, None, LowerBound); 1724 1725 if (Count != -1 && Count != 0) 1726 // FIXME: An unbounded array should reference the expression that defines 1727 // the array. 1728 addUInt(DW_Subrange, dwarf::DW_AT_upper_bound, None, 1729 LowerBound + Count - 1); 1730 } 1731 1732 /// constructArrayTypeDIE - Construct array type DIE from DICompositeType. 1733 void DwarfUnit::constructArrayTypeDIE(DIE &Buffer, DICompositeType CTy) { 1734 if (CTy.isVector()) 1735 addFlag(Buffer, dwarf::DW_AT_GNU_vector); 1736 1737 // Emit the element type. 1738 addType(Buffer, resolve(CTy.getTypeDerivedFrom())); 1739 1740 // Get an anonymous type for index type. 1741 // FIXME: This type should be passed down from the front end 1742 // as different languages may have different sizes for indexes. 1743 DIE *IdxTy = getIndexTyDie(); 1744 if (!IdxTy) { 1745 // Construct an integer type to use for indexes. 1746 IdxTy = &createAndAddDIE(dwarf::DW_TAG_base_type, UnitDie); 1747 addString(*IdxTy, dwarf::DW_AT_name, "sizetype"); 1748 addUInt(*IdxTy, dwarf::DW_AT_byte_size, None, sizeof(int64_t)); 1749 addUInt(*IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1, 1750 dwarf::DW_ATE_unsigned); 1751 setIndexTyDie(IdxTy); 1752 } 1753 1754 // Add subranges to array type. 1755 DIArray Elements = CTy.getTypeArray(); 1756 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) { 1757 DIDescriptor Element = Elements.getElement(i); 1758 if (Element.getTag() == dwarf::DW_TAG_subrange_type) 1759 constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy); 1760 } 1761 } 1762 1763 /// constructEnumTypeDIE - Construct an enum type DIE from DICompositeType. 1764 void DwarfUnit::constructEnumTypeDIE(DIE &Buffer, DICompositeType CTy) { 1765 DIArray Elements = CTy.getTypeArray(); 1766 1767 // Add enumerators to enumeration type. 1768 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) { 1769 DIEnumerator Enum(Elements.getElement(i)); 1770 if (Enum.isEnumerator()) { 1771 DIE &Enumerator = createAndAddDIE(dwarf::DW_TAG_enumerator, Buffer); 1772 StringRef Name = Enum.getName(); 1773 addString(Enumerator, dwarf::DW_AT_name, Name); 1774 int64_t Value = Enum.getEnumValue(); 1775 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, 1776 Value); 1777 } 1778 } 1779 DIType DTy = resolve(CTy.getTypeDerivedFrom()); 1780 if (DTy) { 1781 addType(Buffer, DTy); 1782 addFlag(Buffer, dwarf::DW_AT_enum_class); 1783 } 1784 } 1785 1786 /// constructContainingTypeDIEs - Construct DIEs for types that contain 1787 /// vtables. 1788 void DwarfUnit::constructContainingTypeDIEs() { 1789 for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(), 1790 CE = ContainingTypeMap.end(); 1791 CI != CE; ++CI) { 1792 DIE &SPDie = *CI->first; 1793 DIDescriptor D(CI->second); 1794 if (!D) 1795 continue; 1796 DIE *NDie = getDIE(D); 1797 if (!NDie) 1798 continue; 1799 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, *NDie); 1800 } 1801 } 1802 1803 /// constructVariableDIE - Construct a DIE for the given DbgVariable. 1804 std::unique_ptr<DIE> DwarfUnit::constructVariableDIE(DbgVariable &DV, 1805 AbstractOrInlined AbsIn) { 1806 auto D = constructVariableDIEImpl(DV, AbsIn); 1807 DV.setDIE(*D); 1808 return D; 1809 } 1810 1811 std::unique_ptr<DIE> 1812 DwarfUnit::constructVariableDIEImpl(const DbgVariable &DV, 1813 AbstractOrInlined AbsIn) { 1814 StringRef Name = DV.getName(); 1815 1816 // Define variable debug information entry. 1817 auto VariableDie = make_unique<DIE>(DV.getTag()); 1818 DbgVariable *AbsVar = DV.getAbstractVariable(); 1819 DIE *AbsDIE = AbsVar ? AbsVar->getDIE() : nullptr; 1820 if (AbsDIE) 1821 addDIEEntry(*VariableDie, dwarf::DW_AT_abstract_origin, *AbsDIE); 1822 else { 1823 if (!Name.empty()) 1824 addString(*VariableDie, dwarf::DW_AT_name, Name); 1825 addSourceLine(*VariableDie, DV.getVariable()); 1826 addType(*VariableDie, DV.getType()); 1827 } 1828 1829 if (AbsIn != AOI_Inlined && DV.isArtificial()) 1830 addFlag(*VariableDie, dwarf::DW_AT_artificial); 1831 1832 if (AbsIn == AOI_Abstract) 1833 return VariableDie; 1834 1835 // Add variable address. 1836 1837 unsigned Offset = DV.getDotDebugLocOffset(); 1838 if (Offset != ~0U) { 1839 addLocationList(*VariableDie, dwarf::DW_AT_location, Offset); 1840 return VariableDie; 1841 } 1842 1843 // Check if variable is described by a DBG_VALUE instruction. 1844 if (const MachineInstr *DVInsn = DV.getMInsn()) { 1845 assert(DVInsn->getNumOperands() == 3); 1846 if (DVInsn->getOperand(0).isReg()) { 1847 const MachineOperand RegOp = DVInsn->getOperand(0); 1848 // If the second operand is an immediate, this is an indirect value. 1849 if (DVInsn->getOperand(1).isImm()) { 1850 MachineLocation Location(RegOp.getReg(), 1851 DVInsn->getOperand(1).getImm()); 1852 addVariableAddress(DV, *VariableDie, Location); 1853 } else if (RegOp.getReg()) 1854 addVariableAddress(DV, *VariableDie, MachineLocation(RegOp.getReg())); 1855 } else if (DVInsn->getOperand(0).isImm()) 1856 addConstantValue(*VariableDie, DVInsn->getOperand(0), DV.getType()); 1857 else if (DVInsn->getOperand(0).isFPImm()) 1858 addConstantFPValue(*VariableDie, DVInsn->getOperand(0)); 1859 else if (DVInsn->getOperand(0).isCImm()) 1860 addConstantValue(*VariableDie, DVInsn->getOperand(0).getCImm(), 1861 isUnsignedDIType(DD, DV.getType())); 1862 1863 return VariableDie; 1864 } 1865 1866 // .. else use frame index. 1867 int FI = DV.getFrameIndex(); 1868 if (FI != ~0) { 1869 unsigned FrameReg = 0; 1870 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering(); 1871 int Offset = TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg); 1872 MachineLocation Location(FrameReg, Offset); 1873 addVariableAddress(DV, *VariableDie, Location); 1874 } 1875 1876 return VariableDie; 1877 } 1878 1879 /// constructMemberDIE - Construct member DIE from DIDerivedType. 1880 void DwarfUnit::constructMemberDIE(DIE &Buffer, DIDerivedType DT) { 1881 DIE &MemberDie = createAndAddDIE(DT.getTag(), Buffer); 1882 StringRef Name = DT.getName(); 1883 if (!Name.empty()) 1884 addString(MemberDie, dwarf::DW_AT_name, Name); 1885 1886 addType(MemberDie, resolve(DT.getTypeDerivedFrom())); 1887 1888 addSourceLine(MemberDie, DT); 1889 1890 if (DT.getTag() == dwarf::DW_TAG_inheritance && DT.isVirtual()) { 1891 1892 // For C++, virtual base classes are not at fixed offset. Use following 1893 // expression to extract appropriate offset from vtable. 1894 // BaseAddr = ObAddr + *((*ObAddr) - Offset) 1895 1896 DIELoc *VBaseLocationDie = new (DIEValueAllocator) DIELoc(); 1897 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_dup); 1898 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref); 1899 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_constu); 1900 addUInt(*VBaseLocationDie, dwarf::DW_FORM_udata, DT.getOffsetInBits()); 1901 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_minus); 1902 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref); 1903 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus); 1904 1905 addBlock(MemberDie, dwarf::DW_AT_data_member_location, VBaseLocationDie); 1906 } else { 1907 uint64_t Size = DT.getSizeInBits(); 1908 uint64_t FieldSize = getBaseTypeSize(DD, DT); 1909 uint64_t OffsetInBytes; 1910 1911 if (Size != FieldSize) { 1912 // Handle bitfield, assume bytes are 8 bits. 1913 addUInt(MemberDie, dwarf::DW_AT_byte_size, None, FieldSize/8); 1914 addUInt(MemberDie, dwarf::DW_AT_bit_size, None, Size); 1915 1916 uint64_t Offset = DT.getOffsetInBits(); 1917 uint64_t AlignMask = ~(DT.getAlignInBits() - 1); 1918 uint64_t HiMark = (Offset + FieldSize) & AlignMask; 1919 uint64_t FieldOffset = (HiMark - FieldSize); 1920 Offset -= FieldOffset; 1921 1922 // Maybe we need to work from the other end. 1923 if (Asm->getDataLayout().isLittleEndian()) 1924 Offset = FieldSize - (Offset + Size); 1925 addUInt(MemberDie, dwarf::DW_AT_bit_offset, None, Offset); 1926 1927 // Here DW_AT_data_member_location points to the anonymous 1928 // field that includes this bit field. 1929 OffsetInBytes = FieldOffset >> 3; 1930 } else 1931 // This is not a bitfield. 1932 OffsetInBytes = DT.getOffsetInBits() >> 3; 1933 1934 if (DD->getDwarfVersion() <= 2) { 1935 DIELoc *MemLocationDie = new (DIEValueAllocator) DIELoc(); 1936 addUInt(*MemLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst); 1937 addUInt(*MemLocationDie, dwarf::DW_FORM_udata, OffsetInBytes); 1938 addBlock(MemberDie, dwarf::DW_AT_data_member_location, MemLocationDie); 1939 } else 1940 addUInt(MemberDie, dwarf::DW_AT_data_member_location, None, 1941 OffsetInBytes); 1942 } 1943 1944 if (DT.isProtected()) 1945 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, 1946 dwarf::DW_ACCESS_protected); 1947 else if (DT.isPrivate()) 1948 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, 1949 dwarf::DW_ACCESS_private); 1950 // Otherwise C++ member and base classes are considered public. 1951 else 1952 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, 1953 dwarf::DW_ACCESS_public); 1954 if (DT.isVirtual()) 1955 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, 1956 dwarf::DW_VIRTUALITY_virtual); 1957 1958 // Objective-C properties. 1959 if (MDNode *PNode = DT.getObjCProperty()) 1960 if (DIEEntry *PropertyDie = getDIEEntry(PNode)) 1961 MemberDie.addValue(dwarf::DW_AT_APPLE_property, dwarf::DW_FORM_ref4, 1962 PropertyDie); 1963 1964 if (DT.isArtificial()) 1965 addFlag(MemberDie, dwarf::DW_AT_artificial); 1966 } 1967 1968 /// getOrCreateStaticMemberDIE - Create new DIE for C++ static member. 1969 DIE *DwarfUnit::getOrCreateStaticMemberDIE(DIDerivedType DT) { 1970 if (!DT.Verify()) 1971 return nullptr; 1972 1973 // Construct the context before querying for the existence of the DIE in case 1974 // such construction creates the DIE. 1975 DIE *ContextDIE = getOrCreateContextDIE(resolve(DT.getContext())); 1976 assert(dwarf::isType(ContextDIE->getTag()) && 1977 "Static member should belong to a type."); 1978 1979 if (DIE *StaticMemberDIE = getDIE(DT)) 1980 return StaticMemberDIE; 1981 1982 DIE &StaticMemberDIE = createAndAddDIE(DT.getTag(), *ContextDIE, DT); 1983 1984 DIType Ty = resolve(DT.getTypeDerivedFrom()); 1985 1986 addString(StaticMemberDIE, dwarf::DW_AT_name, DT.getName()); 1987 addType(StaticMemberDIE, Ty); 1988 addSourceLine(StaticMemberDIE, DT); 1989 addFlag(StaticMemberDIE, dwarf::DW_AT_external); 1990 addFlag(StaticMemberDIE, dwarf::DW_AT_declaration); 1991 1992 // FIXME: We could omit private if the parent is a class_type, and 1993 // public if the parent is something else. 1994 if (DT.isProtected()) 1995 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, 1996 dwarf::DW_ACCESS_protected); 1997 else if (DT.isPrivate()) 1998 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, 1999 dwarf::DW_ACCESS_private); 2000 else 2001 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, 2002 dwarf::DW_ACCESS_public); 2003 2004 if (const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(DT.getConstant())) 2005 addConstantValue(StaticMemberDIE, CI, isUnsignedDIType(DD, Ty)); 2006 if (const ConstantFP *CFP = dyn_cast_or_null<ConstantFP>(DT.getConstant())) 2007 addConstantFPValue(StaticMemberDIE, CFP); 2008 2009 return &StaticMemberDIE; 2010 } 2011 2012 void DwarfUnit::emitHeader(const MCSymbol *ASectionSym) const { 2013 Asm->OutStreamer.AddComment("DWARF version number"); 2014 Asm->EmitInt16(DD->getDwarfVersion()); 2015 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section"); 2016 // We share one abbreviations table across all units so it's always at the 2017 // start of the section. Use a relocatable offset where needed to ensure 2018 // linking doesn't invalidate that offset. 2019 if (ASectionSym) 2020 Asm->EmitSectionOffset(ASectionSym, ASectionSym); 2021 else 2022 // Use a constant value when no symbol is provided. 2023 Asm->EmitInt32(0); 2024 Asm->OutStreamer.AddComment("Address Size (in bytes)"); 2025 Asm->EmitInt8(Asm->getDataLayout().getPointerSize()); 2026 } 2027 2028 void DwarfUnit::addRange(RangeSpan Range) { 2029 // Only add a range for this unit if we're emitting full debug. 2030 if (getCUNode().getEmissionKind() == DIBuilder::FullDebug) { 2031 // If we have no current ranges just add the range and return, otherwise, 2032 // check the current section and CU against the previous section and CU we 2033 // emitted into and the subprogram was contained within. If these are the 2034 // same then extend our current range, otherwise add this as a new range. 2035 if (CURanges.size() == 0 || 2036 this != DD->getPrevCU() || 2037 Asm->getCurrentSection() != DD->getPrevSection()) { 2038 CURanges.push_back(Range); 2039 return; 2040 } 2041 2042 assert(&(CURanges.back().getEnd()->getSection()) == 2043 &(Range.getEnd()->getSection()) && 2044 "We can only append to a range in the same section!"); 2045 CURanges.back().setEnd(Range.getEnd()); 2046 } 2047 } 2048 2049 void DwarfCompileUnit::initStmtList(MCSymbol *DwarfLineSectionSym) { 2050 // Define start line table label for each Compile Unit. 2051 MCSymbol *LineTableStartSym = 2052 Asm->OutStreamer.getDwarfLineTableSymbol(getUniqueID()); 2053 2054 stmtListIndex = UnitDie.getValues().size(); 2055 2056 // DW_AT_stmt_list is a offset of line number information for this 2057 // compile unit in debug_line section. For split dwarf this is 2058 // left in the skeleton CU and so not included. 2059 // The line table entries are not always emitted in assembly, so it 2060 // is not okay to use line_table_start here. 2061 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections()) 2062 addSectionLabel(UnitDie, dwarf::DW_AT_stmt_list, LineTableStartSym); 2063 else 2064 addSectionDelta(UnitDie, dwarf::DW_AT_stmt_list, LineTableStartSym, 2065 DwarfLineSectionSym); 2066 } 2067 2068 void DwarfCompileUnit::applyStmtList(DIE &D) { 2069 D.addValue(dwarf::DW_AT_stmt_list, 2070 UnitDie.getAbbrev().getData()[stmtListIndex].getForm(), 2071 UnitDie.getValues()[stmtListIndex]); 2072 } 2073 2074 void DwarfTypeUnit::emitHeader(const MCSymbol *ASectionSym) const { 2075 DwarfUnit::emitHeader(ASectionSym); 2076 Asm->OutStreamer.AddComment("Type Signature"); 2077 Asm->OutStreamer.EmitIntValue(TypeSignature, sizeof(TypeSignature)); 2078 Asm->OutStreamer.AddComment("Type DIE Offset"); 2079 // In a skeleton type unit there is no type DIE so emit a zero offset. 2080 Asm->OutStreamer.EmitIntValue(Ty ? Ty->getOffset() : 0, 2081 sizeof(Ty->getOffset())); 2082 } 2083 2084 void DwarfTypeUnit::initSection(const MCSection *Section) { 2085 assert(!this->Section); 2086 this->Section = Section; 2087 // Since each type unit is contained in its own COMDAT section, the begin 2088 // label and the section label are the same. Using the begin label emission in 2089 // DwarfDebug to emit the section label as well is slightly subtle/sneaky, but 2090 // the only other alternative of lazily constructing start-of-section labels 2091 // and storing a mapping in DwarfDebug (or AsmPrinter). 2092 this->SectionSym = this->LabelBegin = 2093 Asm->GetTempSymbol(Section->getLabelBeginName(), getUniqueID()); 2094 this->LabelEnd = 2095 Asm->GetTempSymbol(Section->getLabelEndName(), getUniqueID()); 2096 } 2097