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