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