1 //===-- llvm/CodeGen/DwarfUnit.cpp - Dwarf Type and Compile Units ---------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file contains support for constructing a dwarf compile unit. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "DwarfUnit.h" 14 #include "AddressPool.h" 15 #include "DwarfCompileUnit.h" 16 #include "DwarfDebug.h" 17 #include "DwarfExpression.h" 18 #include "llvm/ADT/APFloat.h" 19 #include "llvm/ADT/APInt.h" 20 #include "llvm/ADT/None.h" 21 #include "llvm/ADT/StringExtras.h" 22 #include "llvm/ADT/iterator_range.h" 23 #include "llvm/CodeGen/MachineFunction.h" 24 #include "llvm/CodeGen/MachineOperand.h" 25 #include "llvm/CodeGen/TargetRegisterInfo.h" 26 #include "llvm/CodeGen/TargetSubtargetInfo.h" 27 #include "llvm/IR/Constants.h" 28 #include "llvm/IR/DataLayout.h" 29 #include "llvm/IR/GlobalValue.h" 30 #include "llvm/IR/Metadata.h" 31 #include "llvm/MC/MCAsmInfo.h" 32 #include "llvm/MC/MCContext.h" 33 #include "llvm/MC/MCDwarf.h" 34 #include "llvm/MC/MCSection.h" 35 #include "llvm/MC/MCStreamer.h" 36 #include "llvm/MC/MachineLocation.h" 37 #include "llvm/Support/Casting.h" 38 #include "llvm/Support/CommandLine.h" 39 #include "llvm/Target/TargetLoweringObjectFile.h" 40 #include <cassert> 41 #include <cstdint> 42 #include <string> 43 #include <utility> 44 45 using namespace llvm; 46 47 #define DEBUG_TYPE "dwarfdebug" 48 49 DIEDwarfExpression::DIEDwarfExpression(const AsmPrinter &AP, 50 DwarfCompileUnit &CU, DIELoc &DIE) 51 : DwarfExpression(AP.getDwarfVersion(), CU), AP(AP), OutDIE(DIE) {} 52 53 void DIEDwarfExpression::emitOp(uint8_t Op, const char* Comment) { 54 CU.addUInt(getActiveDIE(), dwarf::DW_FORM_data1, Op); 55 } 56 57 void DIEDwarfExpression::emitSigned(int64_t Value) { 58 CU.addSInt(getActiveDIE(), dwarf::DW_FORM_sdata, Value); 59 } 60 61 void DIEDwarfExpression::emitUnsigned(uint64_t Value) { 62 CU.addUInt(getActiveDIE(), dwarf::DW_FORM_udata, Value); 63 } 64 65 void DIEDwarfExpression::emitData1(uint8_t Value) { 66 CU.addUInt(getActiveDIE(), dwarf::DW_FORM_data1, Value); 67 } 68 69 void DIEDwarfExpression::emitBaseTypeRef(uint64_t Idx) { 70 CU.addBaseTypeRef(getActiveDIE(), Idx); 71 } 72 73 void DIEDwarfExpression::enableTemporaryBuffer() { 74 assert(!IsBuffering && "Already buffering?"); 75 IsBuffering = true; 76 } 77 78 void DIEDwarfExpression::disableTemporaryBuffer() { IsBuffering = false; } 79 80 unsigned DIEDwarfExpression::getTemporaryBufferSize() { 81 return TmpDIE.ComputeSize(&AP); 82 } 83 84 void DIEDwarfExpression::commitTemporaryBuffer() { OutDIE.takeValues(TmpDIE); } 85 86 bool DIEDwarfExpression::isFrameRegister(const TargetRegisterInfo &TRI, 87 unsigned MachineReg) { 88 return MachineReg == TRI.getFrameRegister(*AP.MF); 89 } 90 91 DwarfUnit::DwarfUnit(dwarf::Tag UnitTag, const DICompileUnit *Node, 92 AsmPrinter *A, DwarfDebug *DW, DwarfFile *DWU) 93 : DIEUnit(UnitTag), CUNode(Node), Asm(A), DD(DW), DU(DWU), 94 IndexTyDie(nullptr) {} 95 96 DwarfTypeUnit::DwarfTypeUnit(DwarfCompileUnit &CU, AsmPrinter *A, 97 DwarfDebug *DW, DwarfFile *DWU, 98 MCDwarfDwoLineTable *SplitLineTable) 99 : DwarfUnit(dwarf::DW_TAG_type_unit, CU.getCUNode(), A, DW, DWU), CU(CU), 100 SplitLineTable(SplitLineTable) { 101 } 102 103 DwarfUnit::~DwarfUnit() { 104 for (unsigned j = 0, M = DIEBlocks.size(); j < M; ++j) 105 DIEBlocks[j]->~DIEBlock(); 106 for (unsigned j = 0, M = DIELocs.size(); j < M; ++j) 107 DIELocs[j]->~DIELoc(); 108 } 109 110 int64_t DwarfUnit::getDefaultLowerBound() const { 111 switch (getLanguage()) { 112 default: 113 break; 114 115 // The languages below have valid values in all DWARF versions. 116 case dwarf::DW_LANG_C: 117 case dwarf::DW_LANG_C89: 118 case dwarf::DW_LANG_C_plus_plus: 119 return 0; 120 121 case dwarf::DW_LANG_Fortran77: 122 case dwarf::DW_LANG_Fortran90: 123 return 1; 124 125 // The languages below have valid values only if the DWARF version >= 3. 126 case dwarf::DW_LANG_C99: 127 case dwarf::DW_LANG_ObjC: 128 case dwarf::DW_LANG_ObjC_plus_plus: 129 if (DD->getDwarfVersion() >= 3) 130 return 0; 131 break; 132 133 case dwarf::DW_LANG_Fortran95: 134 if (DD->getDwarfVersion() >= 3) 135 return 1; 136 break; 137 138 // Starting with DWARF v4, all defined languages have valid values. 139 case dwarf::DW_LANG_D: 140 case dwarf::DW_LANG_Java: 141 case dwarf::DW_LANG_Python: 142 case dwarf::DW_LANG_UPC: 143 if (DD->getDwarfVersion() >= 4) 144 return 0; 145 break; 146 147 case dwarf::DW_LANG_Ada83: 148 case dwarf::DW_LANG_Ada95: 149 case dwarf::DW_LANG_Cobol74: 150 case dwarf::DW_LANG_Cobol85: 151 case dwarf::DW_LANG_Modula2: 152 case dwarf::DW_LANG_Pascal83: 153 case dwarf::DW_LANG_PLI: 154 if (DD->getDwarfVersion() >= 4) 155 return 1; 156 break; 157 158 // The languages below are new in DWARF v5. 159 case dwarf::DW_LANG_BLISS: 160 case dwarf::DW_LANG_C11: 161 case dwarf::DW_LANG_C_plus_plus_03: 162 case dwarf::DW_LANG_C_plus_plus_11: 163 case dwarf::DW_LANG_C_plus_plus_14: 164 case dwarf::DW_LANG_Dylan: 165 case dwarf::DW_LANG_Go: 166 case dwarf::DW_LANG_Haskell: 167 case dwarf::DW_LANG_OCaml: 168 case dwarf::DW_LANG_OpenCL: 169 case dwarf::DW_LANG_RenderScript: 170 case dwarf::DW_LANG_Rust: 171 case dwarf::DW_LANG_Swift: 172 if (DD->getDwarfVersion() >= 5) 173 return 0; 174 break; 175 176 case dwarf::DW_LANG_Fortran03: 177 case dwarf::DW_LANG_Fortran08: 178 case dwarf::DW_LANG_Julia: 179 case dwarf::DW_LANG_Modula3: 180 if (DD->getDwarfVersion() >= 5) 181 return 1; 182 break; 183 } 184 185 return -1; 186 } 187 188 /// Check whether the DIE for this MDNode can be shared across CUs. 189 bool DwarfUnit::isShareableAcrossCUs(const DINode *D) const { 190 // When the MDNode can be part of the type system (this includes subprogram 191 // declarations *and* subprogram definitions, even local definitions), the 192 // DIE must be shared across CUs. 193 // Combining type units and cross-CU DIE sharing is lower value (since 194 // cross-CU DIE sharing is used in LTO and removes type redundancy at that 195 // level already) but may be implementable for some value in projects 196 // building multiple independent libraries with LTO and then linking those 197 // together. 198 if (isDwoUnit() && !DD->shareAcrossDWOCUs()) 199 return false; 200 return (isa<DIType>(D) || isa<DISubprogram>(D)) && !DD->generateTypeUnits(); 201 } 202 203 DIE *DwarfUnit::getDIE(const DINode *D) const { 204 if (isShareableAcrossCUs(D)) 205 return DU->getDIE(D); 206 return MDNodeToDieMap.lookup(D); 207 } 208 209 void DwarfUnit::insertDIE(const DINode *Desc, DIE *D) { 210 if (isShareableAcrossCUs(Desc)) { 211 DU->insertDIE(Desc, D); 212 return; 213 } 214 MDNodeToDieMap.insert(std::make_pair(Desc, D)); 215 } 216 217 void DwarfUnit::insertDIE(DIE *D) { 218 MDNodeToDieMap.insert(std::make_pair(nullptr, D)); 219 } 220 221 void DwarfUnit::addFlag(DIE &Die, dwarf::Attribute Attribute) { 222 if (DD->getDwarfVersion() >= 4) 223 Die.addValue(DIEValueAllocator, Attribute, dwarf::DW_FORM_flag_present, 224 DIEInteger(1)); 225 else 226 Die.addValue(DIEValueAllocator, Attribute, dwarf::DW_FORM_flag, 227 DIEInteger(1)); 228 } 229 230 void DwarfUnit::addUInt(DIEValueList &Die, dwarf::Attribute Attribute, 231 Optional<dwarf::Form> Form, uint64_t Integer) { 232 if (!Form) 233 Form = DIEInteger::BestForm(false, Integer); 234 assert(Form != dwarf::DW_FORM_implicit_const && 235 "DW_FORM_implicit_const is used only for signed integers"); 236 Die.addValue(DIEValueAllocator, Attribute, *Form, DIEInteger(Integer)); 237 } 238 239 void DwarfUnit::addUInt(DIEValueList &Block, dwarf::Form Form, 240 uint64_t Integer) { 241 addUInt(Block, (dwarf::Attribute)0, Form, Integer); 242 } 243 244 void DwarfUnit::addSInt(DIEValueList &Die, dwarf::Attribute Attribute, 245 Optional<dwarf::Form> Form, int64_t Integer) { 246 if (!Form) 247 Form = DIEInteger::BestForm(true, Integer); 248 Die.addValue(DIEValueAllocator, Attribute, *Form, DIEInteger(Integer)); 249 } 250 251 void DwarfUnit::addSInt(DIELoc &Die, Optional<dwarf::Form> Form, 252 int64_t Integer) { 253 addSInt(Die, (dwarf::Attribute)0, Form, Integer); 254 } 255 256 void DwarfUnit::addString(DIE &Die, dwarf::Attribute Attribute, 257 StringRef String) { 258 if (CUNode->isDebugDirectivesOnly()) 259 return; 260 261 if (DD->useInlineStrings()) { 262 Die.addValue(DIEValueAllocator, Attribute, dwarf::DW_FORM_string, 263 new (DIEValueAllocator) 264 DIEInlineString(String, DIEValueAllocator)); 265 return; 266 } 267 dwarf::Form IxForm = 268 isDwoUnit() ? dwarf::DW_FORM_GNU_str_index : dwarf::DW_FORM_strp; 269 270 auto StringPoolEntry = 271 useSegmentedStringOffsetsTable() || IxForm == dwarf::DW_FORM_GNU_str_index 272 ? DU->getStringPool().getIndexedEntry(*Asm, String) 273 : DU->getStringPool().getEntry(*Asm, String); 274 275 // For DWARF v5 and beyond, use the smallest strx? form possible. 276 if (useSegmentedStringOffsetsTable()) { 277 IxForm = dwarf::DW_FORM_strx1; 278 unsigned Index = StringPoolEntry.getIndex(); 279 if (Index > 0xffffff) 280 IxForm = dwarf::DW_FORM_strx4; 281 else if (Index > 0xffff) 282 IxForm = dwarf::DW_FORM_strx3; 283 else if (Index > 0xff) 284 IxForm = dwarf::DW_FORM_strx2; 285 } 286 Die.addValue(DIEValueAllocator, Attribute, IxForm, 287 DIEString(StringPoolEntry)); 288 } 289 290 DIEValueList::value_iterator DwarfUnit::addLabel(DIEValueList &Die, 291 dwarf::Attribute Attribute, 292 dwarf::Form Form, 293 const MCSymbol *Label) { 294 return Die.addValue(DIEValueAllocator, Attribute, Form, DIELabel(Label)); 295 } 296 297 void DwarfUnit::addLabel(DIELoc &Die, dwarf::Form Form, const MCSymbol *Label) { 298 addLabel(Die, (dwarf::Attribute)0, Form, Label); 299 } 300 301 void DwarfUnit::addSectionOffset(DIE &Die, dwarf::Attribute Attribute, 302 uint64_t Integer) { 303 if (DD->getDwarfVersion() >= 4) 304 addUInt(Die, Attribute, dwarf::DW_FORM_sec_offset, Integer); 305 else 306 addUInt(Die, Attribute, dwarf::DW_FORM_data4, Integer); 307 } 308 309 unsigned DwarfTypeUnit::getOrCreateSourceID(const DIFile *File) { 310 if (!SplitLineTable) 311 return getCU().getOrCreateSourceID(File); 312 if (!UsedLineTable) { 313 UsedLineTable = true; 314 // This is a split type unit that needs a line table. 315 addSectionOffset(getUnitDie(), dwarf::DW_AT_stmt_list, 0); 316 } 317 return SplitLineTable->getFile( 318 File->getDirectory(), File->getFilename(), DD->getMD5AsBytes(File), 319 Asm->OutContext.getDwarfVersion(), File->getSource()); 320 } 321 322 void DwarfUnit::addOpAddress(DIELoc &Die, const MCSymbol *Sym) { 323 if (DD->getDwarfVersion() >= 5) { 324 addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_addrx); 325 addUInt(Die, dwarf::DW_FORM_addrx, DD->getAddressPool().getIndex(Sym)); 326 return; 327 } 328 329 if (DD->useSplitDwarf()) { 330 addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_addr_index); 331 addUInt(Die, dwarf::DW_FORM_GNU_addr_index, 332 DD->getAddressPool().getIndex(Sym)); 333 return; 334 } 335 336 addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_addr); 337 addLabel(Die, dwarf::DW_FORM_addr, Sym); 338 } 339 340 void DwarfUnit::addLabelDelta(DIE &Die, dwarf::Attribute Attribute, 341 const MCSymbol *Hi, const MCSymbol *Lo) { 342 Die.addValue(DIEValueAllocator, Attribute, dwarf::DW_FORM_data4, 343 new (DIEValueAllocator) DIEDelta(Hi, Lo)); 344 } 345 346 void DwarfUnit::addDIEEntry(DIE &Die, dwarf::Attribute Attribute, DIE &Entry) { 347 addDIEEntry(Die, Attribute, DIEEntry(Entry)); 348 } 349 350 void DwarfUnit::addDIETypeSignature(DIE &Die, uint64_t Signature) { 351 // Flag the type unit reference as a declaration so that if it contains 352 // members (implicit special members, static data member definitions, member 353 // declarations for definitions in this CU, etc) consumers don't get confused 354 // and think this is a full definition. 355 addFlag(Die, dwarf::DW_AT_declaration); 356 357 Die.addValue(DIEValueAllocator, dwarf::DW_AT_signature, 358 dwarf::DW_FORM_ref_sig8, DIEInteger(Signature)); 359 } 360 361 void DwarfUnit::addDIEEntry(DIE &Die, dwarf::Attribute Attribute, 362 DIEEntry Entry) { 363 const DIEUnit *CU = Die.getUnit(); 364 const DIEUnit *EntryCU = Entry.getEntry().getUnit(); 365 if (!CU) 366 // We assume that Die belongs to this CU, if it is not linked to any CU yet. 367 CU = getUnitDie().getUnit(); 368 if (!EntryCU) 369 EntryCU = getUnitDie().getUnit(); 370 Die.addValue(DIEValueAllocator, Attribute, 371 EntryCU == CU ? dwarf::DW_FORM_ref4 : dwarf::DW_FORM_ref_addr, 372 Entry); 373 } 374 375 DIE &DwarfUnit::createAndAddDIE(unsigned Tag, DIE &Parent, const DINode *N) { 376 DIE &Die = Parent.addChild(DIE::get(DIEValueAllocator, (dwarf::Tag)Tag)); 377 if (N) 378 insertDIE(N, &Die); 379 return Die; 380 } 381 382 void DwarfUnit::addBlock(DIE &Die, dwarf::Attribute Attribute, DIELoc *Loc) { 383 Loc->ComputeSize(Asm); 384 DIELocs.push_back(Loc); // Memoize so we can call the destructor later on. 385 Die.addValue(DIEValueAllocator, Attribute, 386 Loc->BestForm(DD->getDwarfVersion()), Loc); 387 } 388 389 void DwarfUnit::addBlock(DIE &Die, dwarf::Attribute Attribute, 390 DIEBlock *Block) { 391 Block->ComputeSize(Asm); 392 DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on. 393 Die.addValue(DIEValueAllocator, Attribute, Block->BestForm(), Block); 394 } 395 396 void DwarfUnit::addSourceLine(DIE &Die, unsigned Line, const DIFile *File) { 397 if (Line == 0) 398 return; 399 400 unsigned FileID = getOrCreateSourceID(File); 401 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID); 402 addUInt(Die, dwarf::DW_AT_decl_line, None, Line); 403 } 404 405 void DwarfUnit::addSourceLine(DIE &Die, const DILocalVariable *V) { 406 assert(V); 407 408 addSourceLine(Die, V->getLine(), V->getFile()); 409 } 410 411 void DwarfUnit::addSourceLine(DIE &Die, const DIGlobalVariable *G) { 412 assert(G); 413 414 addSourceLine(Die, G->getLine(), G->getFile()); 415 } 416 417 void DwarfUnit::addSourceLine(DIE &Die, const DISubprogram *SP) { 418 assert(SP); 419 420 addSourceLine(Die, SP->getLine(), SP->getFile()); 421 } 422 423 void DwarfUnit::addSourceLine(DIE &Die, const DILabel *L) { 424 assert(L); 425 426 addSourceLine(Die, L->getLine(), L->getFile()); 427 } 428 429 void DwarfUnit::addSourceLine(DIE &Die, const DIType *Ty) { 430 assert(Ty); 431 432 addSourceLine(Die, Ty->getLine(), Ty->getFile()); 433 } 434 435 void DwarfUnit::addSourceLine(DIE &Die, const DIObjCProperty *Ty) { 436 assert(Ty); 437 438 addSourceLine(Die, Ty->getLine(), Ty->getFile()); 439 } 440 441 /// Return true if type encoding is unsigned. 442 static bool isUnsignedDIType(DwarfDebug *DD, const DIType *Ty) { 443 if (auto *CTy = dyn_cast<DICompositeType>(Ty)) { 444 // FIXME: Enums without a fixed underlying type have unknown signedness 445 // here, leading to incorrectly emitted constants. 446 if (CTy->getTag() == dwarf::DW_TAG_enumeration_type) 447 return false; 448 449 // (Pieces of) aggregate types that get hacked apart by SROA may be 450 // represented by a constant. Encode them as unsigned bytes. 451 return true; 452 } 453 454 if (auto *DTy = dyn_cast<DIDerivedType>(Ty)) { 455 dwarf::Tag T = (dwarf::Tag)Ty->getTag(); 456 // Encode pointer constants as unsigned bytes. This is used at least for 457 // null pointer constant emission. 458 // FIXME: reference and rvalue_reference /probably/ shouldn't be allowed 459 // here, but accept them for now due to a bug in SROA producing bogus 460 // dbg.values. 461 if (T == dwarf::DW_TAG_pointer_type || 462 T == dwarf::DW_TAG_ptr_to_member_type || 463 T == dwarf::DW_TAG_reference_type || 464 T == dwarf::DW_TAG_rvalue_reference_type) 465 return true; 466 assert(T == dwarf::DW_TAG_typedef || T == dwarf::DW_TAG_const_type || 467 T == dwarf::DW_TAG_volatile_type || 468 T == dwarf::DW_TAG_restrict_type || T == dwarf::DW_TAG_atomic_type); 469 assert(DTy->getBaseType() && "Expected valid base type"); 470 return isUnsignedDIType(DD, DTy->getBaseType()); 471 } 472 473 auto *BTy = cast<DIBasicType>(Ty); 474 unsigned Encoding = BTy->getEncoding(); 475 assert((Encoding == dwarf::DW_ATE_unsigned || 476 Encoding == dwarf::DW_ATE_unsigned_char || 477 Encoding == dwarf::DW_ATE_signed || 478 Encoding == dwarf::DW_ATE_signed_char || 479 Encoding == dwarf::DW_ATE_float || Encoding == dwarf::DW_ATE_UTF || 480 Encoding == dwarf::DW_ATE_boolean || 481 (Ty->getTag() == dwarf::DW_TAG_unspecified_type && 482 Ty->getName() == "decltype(nullptr)")) && 483 "Unsupported encoding"); 484 return Encoding == dwarf::DW_ATE_unsigned || 485 Encoding == dwarf::DW_ATE_unsigned_char || 486 Encoding == dwarf::DW_ATE_UTF || Encoding == dwarf::DW_ATE_boolean || 487 Ty->getTag() == dwarf::DW_TAG_unspecified_type; 488 } 489 490 void DwarfUnit::addConstantFPValue(DIE &Die, const MachineOperand &MO) { 491 assert(MO.isFPImm() && "Invalid machine operand!"); 492 DIEBlock *Block = new (DIEValueAllocator) DIEBlock; 493 APFloat FPImm = MO.getFPImm()->getValueAPF(); 494 495 // Get the raw data form of the floating point. 496 const APInt FltVal = FPImm.bitcastToAPInt(); 497 const char *FltPtr = (const char *)FltVal.getRawData(); 498 499 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte. 500 bool LittleEndian = Asm->getDataLayout().isLittleEndian(); 501 int Incr = (LittleEndian ? 1 : -1); 502 int Start = (LittleEndian ? 0 : NumBytes - 1); 503 int Stop = (LittleEndian ? NumBytes : -1); 504 505 // Output the constant to DWARF one byte at a time. 506 for (; Start != Stop; Start += Incr) 507 addUInt(*Block, dwarf::DW_FORM_data1, (unsigned char)0xFF & FltPtr[Start]); 508 509 addBlock(Die, dwarf::DW_AT_const_value, Block); 510 } 511 512 void DwarfUnit::addConstantFPValue(DIE &Die, const ConstantFP *CFP) { 513 // Pass this down to addConstantValue as an unsigned bag of bits. 514 addConstantValue(Die, CFP->getValueAPF().bitcastToAPInt(), true); 515 } 516 517 void DwarfUnit::addConstantValue(DIE &Die, const ConstantInt *CI, 518 const DIType *Ty) { 519 addConstantValue(Die, CI->getValue(), Ty); 520 } 521 522 void DwarfUnit::addConstantValue(DIE &Die, const MachineOperand &MO, 523 const DIType *Ty) { 524 assert(MO.isImm() && "Invalid machine operand!"); 525 526 addConstantValue(Die, isUnsignedDIType(DD, Ty), MO.getImm()); 527 } 528 529 void DwarfUnit::addConstantValue(DIE &Die, uint64_t Val, const DIType *Ty) { 530 addConstantValue(Die, isUnsignedDIType(DD, Ty), Val); 531 } 532 533 void DwarfUnit::addConstantValue(DIE &Die, bool Unsigned, uint64_t Val) { 534 // FIXME: This is a bit conservative/simple - it emits negative values always 535 // sign extended to 64 bits rather than minimizing the number of bytes. 536 addUInt(Die, dwarf::DW_AT_const_value, 537 Unsigned ? dwarf::DW_FORM_udata : dwarf::DW_FORM_sdata, Val); 538 } 539 540 void DwarfUnit::addConstantValue(DIE &Die, const APInt &Val, const DIType *Ty) { 541 addConstantValue(Die, Val, isUnsignedDIType(DD, Ty)); 542 } 543 544 void DwarfUnit::addConstantValue(DIE &Die, const APInt &Val, bool Unsigned) { 545 unsigned CIBitWidth = Val.getBitWidth(); 546 if (CIBitWidth <= 64) { 547 addConstantValue(Die, Unsigned, 548 Unsigned ? Val.getZExtValue() : Val.getSExtValue()); 549 return; 550 } 551 552 DIEBlock *Block = new (DIEValueAllocator) DIEBlock; 553 554 // Get the raw data form of the large APInt. 555 const uint64_t *Ptr64 = Val.getRawData(); 556 557 int NumBytes = Val.getBitWidth() / 8; // 8 bits per byte. 558 bool LittleEndian = Asm->getDataLayout().isLittleEndian(); 559 560 // Output the constant to DWARF one byte at a time. 561 for (int i = 0; i < NumBytes; i++) { 562 uint8_t c; 563 if (LittleEndian) 564 c = Ptr64[i / 8] >> (8 * (i & 7)); 565 else 566 c = Ptr64[(NumBytes - 1 - i) / 8] >> (8 * ((NumBytes - 1 - i) & 7)); 567 addUInt(*Block, dwarf::DW_FORM_data1, c); 568 } 569 570 addBlock(Die, dwarf::DW_AT_const_value, Block); 571 } 572 573 void DwarfUnit::addLinkageName(DIE &Die, StringRef LinkageName) { 574 if (!LinkageName.empty()) 575 addString(Die, 576 DD->getDwarfVersion() >= 4 ? dwarf::DW_AT_linkage_name 577 : dwarf::DW_AT_MIPS_linkage_name, 578 GlobalValue::dropLLVMManglingEscape(LinkageName)); 579 } 580 581 void DwarfUnit::addTemplateParams(DIE &Buffer, DINodeArray TParams) { 582 // Add template parameters. 583 for (const auto *Element : TParams) { 584 if (auto *TTP = dyn_cast<DITemplateTypeParameter>(Element)) 585 constructTemplateTypeParameterDIE(Buffer, TTP); 586 else if (auto *TVP = dyn_cast<DITemplateValueParameter>(Element)) 587 constructTemplateValueParameterDIE(Buffer, TVP); 588 } 589 } 590 591 /// Add thrown types. 592 void DwarfUnit::addThrownTypes(DIE &Die, DINodeArray ThrownTypes) { 593 for (const auto *Ty : ThrownTypes) { 594 DIE &TT = createAndAddDIE(dwarf::DW_TAG_thrown_type, Die); 595 addType(TT, cast<DIType>(Ty)); 596 } 597 } 598 599 DIE *DwarfUnit::getOrCreateContextDIE(const DIScope *Context) { 600 if (!Context || isa<DIFile>(Context)) 601 return &getUnitDie(); 602 if (auto *T = dyn_cast<DIType>(Context)) 603 return getOrCreateTypeDIE(T); 604 if (auto *NS = dyn_cast<DINamespace>(Context)) 605 return getOrCreateNameSpace(NS); 606 if (auto *SP = dyn_cast<DISubprogram>(Context)) 607 return getOrCreateSubprogramDIE(SP); 608 if (auto *M = dyn_cast<DIModule>(Context)) 609 return getOrCreateModule(M); 610 return getDIE(Context); 611 } 612 613 DIE *DwarfUnit::createTypeDIE(const DICompositeType *Ty) { 614 auto *Context = Ty->getScope(); 615 DIE *ContextDIE = getOrCreateContextDIE(Context); 616 617 if (DIE *TyDIE = getDIE(Ty)) 618 return TyDIE; 619 620 // Create new type. 621 DIE &TyDIE = createAndAddDIE(Ty->getTag(), *ContextDIE, Ty); 622 623 constructTypeDIE(TyDIE, cast<DICompositeType>(Ty)); 624 625 updateAcceleratorTables(Context, Ty, TyDIE); 626 return &TyDIE; 627 } 628 629 DIE *DwarfUnit::createTypeDIE(const DIScope *Context, DIE &ContextDIE, 630 const DIType *Ty) { 631 // Create new type. 632 DIE &TyDIE = createAndAddDIE(Ty->getTag(), ContextDIE, Ty); 633 634 updateAcceleratorTables(Context, Ty, TyDIE); 635 636 if (auto *BT = dyn_cast<DIBasicType>(Ty)) 637 constructTypeDIE(TyDIE, BT); 638 else if (auto *ST = dyn_cast<DIStringType>(Ty)) 639 constructTypeDIE(TyDIE, ST); 640 else if (auto *STy = dyn_cast<DISubroutineType>(Ty)) 641 constructTypeDIE(TyDIE, STy); 642 else if (auto *CTy = dyn_cast<DICompositeType>(Ty)) { 643 if (DD->generateTypeUnits() && !Ty->isForwardDecl() && 644 (Ty->getRawName() || CTy->getRawIdentifier())) { 645 // Skip updating the accelerator tables since this is not the full type. 646 if (MDString *TypeId = CTy->getRawIdentifier()) 647 DD->addDwarfTypeUnitType(getCU(), TypeId->getString(), TyDIE, CTy); 648 else { 649 auto X = DD->enterNonTypeUnitContext(); 650 finishNonUnitTypeDIE(TyDIE, CTy); 651 } 652 return &TyDIE; 653 } 654 constructTypeDIE(TyDIE, CTy); 655 } else { 656 constructTypeDIE(TyDIE, cast<DIDerivedType>(Ty)); 657 } 658 659 return &TyDIE; 660 } 661 662 DIE *DwarfUnit::getOrCreateTypeDIE(const MDNode *TyNode) { 663 if (!TyNode) 664 return nullptr; 665 666 auto *Ty = cast<DIType>(TyNode); 667 668 // DW_TAG_restrict_type is not supported in DWARF2 669 if (Ty->getTag() == dwarf::DW_TAG_restrict_type && DD->getDwarfVersion() <= 2) 670 return getOrCreateTypeDIE(cast<DIDerivedType>(Ty)->getBaseType()); 671 672 // DW_TAG_atomic_type is not supported in DWARF < 5 673 if (Ty->getTag() == dwarf::DW_TAG_atomic_type && DD->getDwarfVersion() < 5) 674 return getOrCreateTypeDIE(cast<DIDerivedType>(Ty)->getBaseType()); 675 676 // Construct the context before querying for the existence of the DIE in case 677 // such construction creates the DIE. 678 auto *Context = Ty->getScope(); 679 DIE *ContextDIE = getOrCreateContextDIE(Context); 680 assert(ContextDIE); 681 682 if (DIE *TyDIE = getDIE(Ty)) 683 return TyDIE; 684 685 return static_cast<DwarfUnit *>(ContextDIE->getUnit()) 686 ->createTypeDIE(Context, *ContextDIE, Ty); 687 } 688 689 void DwarfUnit::updateAcceleratorTables(const DIScope *Context, 690 const DIType *Ty, const DIE &TyDIE) { 691 if (!Ty->getName().empty() && !Ty->isForwardDecl()) { 692 bool IsImplementation = false; 693 if (auto *CT = dyn_cast<DICompositeType>(Ty)) { 694 // A runtime language of 0 actually means C/C++ and that any 695 // non-negative value is some version of Objective-C/C++. 696 IsImplementation = CT->getRuntimeLang() == 0 || CT->isObjcClassComplete(); 697 } 698 unsigned Flags = IsImplementation ? dwarf::DW_FLAG_type_implementation : 0; 699 DD->addAccelType(*CUNode, Ty->getName(), TyDIE, Flags); 700 701 if (!Context || isa<DICompileUnit>(Context) || isa<DIFile>(Context) || 702 isa<DINamespace>(Context) || isa<DICommonBlock>(Context)) 703 addGlobalType(Ty, TyDIE, Context); 704 } 705 } 706 707 void DwarfUnit::addType(DIE &Entity, const DIType *Ty, 708 dwarf::Attribute Attribute) { 709 assert(Ty && "Trying to add a type that doesn't exist?"); 710 addDIEEntry(Entity, Attribute, DIEEntry(*getOrCreateTypeDIE(Ty))); 711 } 712 713 std::string DwarfUnit::getParentContextString(const DIScope *Context) const { 714 if (!Context) 715 return ""; 716 717 // FIXME: Decide whether to implement this for non-C++ languages. 718 if (!dwarf::isCPlusPlus((dwarf::SourceLanguage)getLanguage())) 719 return ""; 720 721 std::string CS; 722 SmallVector<const DIScope *, 1> Parents; 723 while (!isa<DICompileUnit>(Context)) { 724 Parents.push_back(Context); 725 if (const DIScope *S = Context->getScope()) 726 Context = S; 727 else 728 // Structure, etc types will have a NULL context if they're at the top 729 // level. 730 break; 731 } 732 733 // Reverse iterate over our list to go from the outermost construct to the 734 // innermost. 735 for (const DIScope *Ctx : make_range(Parents.rbegin(), Parents.rend())) { 736 StringRef Name = Ctx->getName(); 737 if (Name.empty() && isa<DINamespace>(Ctx)) 738 Name = "(anonymous namespace)"; 739 if (!Name.empty()) { 740 CS += Name; 741 CS += "::"; 742 } 743 } 744 return CS; 745 } 746 747 void DwarfUnit::constructTypeDIE(DIE &Buffer, const DIBasicType *BTy) { 748 // Get core information. 749 StringRef Name = BTy->getName(); 750 // Add name if not anonymous or intermediate type. 751 if (!Name.empty()) 752 addString(Buffer, dwarf::DW_AT_name, Name); 753 754 // An unspecified type only has a name attribute. 755 if (BTy->getTag() == dwarf::DW_TAG_unspecified_type) 756 return; 757 758 if (BTy->getTag() != dwarf::DW_TAG_string_type) 759 addUInt(Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1, 760 BTy->getEncoding()); 761 762 uint64_t Size = BTy->getSizeInBits() >> 3; 763 addUInt(Buffer, dwarf::DW_AT_byte_size, None, Size); 764 765 if (BTy->isBigEndian()) 766 addUInt(Buffer, dwarf::DW_AT_endianity, None, dwarf::DW_END_big); 767 else if (BTy->isLittleEndian()) 768 addUInt(Buffer, dwarf::DW_AT_endianity, None, dwarf::DW_END_little); 769 } 770 771 void DwarfUnit::constructTypeDIE(DIE &Buffer, const DIStringType *STy) { 772 // Get core information. 773 StringRef Name = STy->getName(); 774 // Add name if not anonymous or intermediate type. 775 if (!Name.empty()) 776 addString(Buffer, dwarf::DW_AT_name, Name); 777 778 if (DIVariable *Var = STy->getStringLength()) { 779 if (auto *VarDIE = getDIE(Var)) 780 addDIEEntry(Buffer, dwarf::DW_AT_string_length, *VarDIE); 781 } else { 782 uint64_t Size = STy->getSizeInBits() >> 3; 783 addUInt(Buffer, dwarf::DW_AT_byte_size, None, Size); 784 } 785 786 if (STy->getEncoding()) { 787 // For eventual Unicode support. 788 addUInt(Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1, 789 STy->getEncoding()); 790 } 791 } 792 793 void DwarfUnit::constructTypeDIE(DIE &Buffer, const DIDerivedType *DTy) { 794 // Get core information. 795 StringRef Name = DTy->getName(); 796 uint64_t Size = DTy->getSizeInBits() >> 3; 797 uint16_t Tag = Buffer.getTag(); 798 799 // Map to main type, void will not have a type. 800 const DIType *FromTy = DTy->getBaseType(); 801 if (FromTy) 802 addType(Buffer, FromTy); 803 804 // Add name if not anonymous or intermediate type. 805 if (!Name.empty()) 806 addString(Buffer, dwarf::DW_AT_name, Name); 807 808 // If alignment is specified for a typedef , create and insert DW_AT_alignment 809 // attribute in DW_TAG_typedef DIE. 810 if (Tag == dwarf::DW_TAG_typedef && DD->getDwarfVersion() >= 5) { 811 uint32_t AlignInBytes = DTy->getAlignInBytes(); 812 if (AlignInBytes > 0) 813 addUInt(Buffer, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata, 814 AlignInBytes); 815 } 816 817 // Add size if non-zero (derived types might be zero-sized.) 818 if (Size && Tag != dwarf::DW_TAG_pointer_type 819 && Tag != dwarf::DW_TAG_ptr_to_member_type 820 && Tag != dwarf::DW_TAG_reference_type 821 && Tag != dwarf::DW_TAG_rvalue_reference_type) 822 addUInt(Buffer, dwarf::DW_AT_byte_size, None, Size); 823 824 if (Tag == dwarf::DW_TAG_ptr_to_member_type) 825 addDIEEntry(Buffer, dwarf::DW_AT_containing_type, 826 *getOrCreateTypeDIE(cast<DIDerivedType>(DTy)->getClassType())); 827 // Add source line info if available and TyDesc is not a forward declaration. 828 if (!DTy->isForwardDecl()) 829 addSourceLine(Buffer, DTy); 830 831 // If DWARF address space value is other than None, add it. The IR 832 // verifier checks that DWARF address space only exists for pointer 833 // or reference types. 834 if (DTy->getDWARFAddressSpace()) 835 addUInt(Buffer, dwarf::DW_AT_address_class, dwarf::DW_FORM_data4, 836 DTy->getDWARFAddressSpace().getValue()); 837 } 838 839 void DwarfUnit::constructSubprogramArguments(DIE &Buffer, DITypeRefArray Args) { 840 for (unsigned i = 1, N = Args.size(); i < N; ++i) { 841 const DIType *Ty = Args[i]; 842 if (!Ty) { 843 assert(i == N-1 && "Unspecified parameter must be the last argument"); 844 createAndAddDIE(dwarf::DW_TAG_unspecified_parameters, Buffer); 845 } else { 846 DIE &Arg = createAndAddDIE(dwarf::DW_TAG_formal_parameter, Buffer); 847 addType(Arg, Ty); 848 if (Ty->isArtificial()) 849 addFlag(Arg, dwarf::DW_AT_artificial); 850 } 851 } 852 } 853 854 void DwarfUnit::constructTypeDIE(DIE &Buffer, const DISubroutineType *CTy) { 855 // Add return type. A void return won't have a type. 856 auto Elements = cast<DISubroutineType>(CTy)->getTypeArray(); 857 if (Elements.size()) 858 if (auto RTy = Elements[0]) 859 addType(Buffer, RTy); 860 861 bool isPrototyped = true; 862 if (Elements.size() == 2 && !Elements[1]) 863 isPrototyped = false; 864 865 constructSubprogramArguments(Buffer, Elements); 866 867 // Add prototype flag if we're dealing with a C language and the function has 868 // been prototyped. 869 uint16_t Language = getLanguage(); 870 if (isPrototyped && 871 (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 || 872 Language == dwarf::DW_LANG_ObjC)) 873 addFlag(Buffer, dwarf::DW_AT_prototyped); 874 875 // Add a DW_AT_calling_convention if this has an explicit convention. 876 if (CTy->getCC() && CTy->getCC() != dwarf::DW_CC_normal) 877 addUInt(Buffer, dwarf::DW_AT_calling_convention, dwarf::DW_FORM_data1, 878 CTy->getCC()); 879 880 if (CTy->isLValueReference()) 881 addFlag(Buffer, dwarf::DW_AT_reference); 882 883 if (CTy->isRValueReference()) 884 addFlag(Buffer, dwarf::DW_AT_rvalue_reference); 885 } 886 887 void DwarfUnit::constructTypeDIE(DIE &Buffer, const DICompositeType *CTy) { 888 // Add name if not anonymous or intermediate type. 889 StringRef Name = CTy->getName(); 890 891 uint64_t Size = CTy->getSizeInBits() >> 3; 892 uint16_t Tag = Buffer.getTag(); 893 894 switch (Tag) { 895 case dwarf::DW_TAG_array_type: 896 constructArrayTypeDIE(Buffer, CTy); 897 break; 898 case dwarf::DW_TAG_enumeration_type: 899 constructEnumTypeDIE(Buffer, CTy); 900 break; 901 case dwarf::DW_TAG_variant_part: 902 case dwarf::DW_TAG_structure_type: 903 case dwarf::DW_TAG_union_type: 904 case dwarf::DW_TAG_class_type: { 905 // Emit the discriminator for a variant part. 906 DIDerivedType *Discriminator = nullptr; 907 if (Tag == dwarf::DW_TAG_variant_part) { 908 Discriminator = CTy->getDiscriminator(); 909 if (Discriminator) { 910 // DWARF says: 911 // If the variant part has a discriminant, the discriminant is 912 // represented by a separate debugging information entry which is 913 // a child of the variant part entry. 914 DIE &DiscMember = constructMemberDIE(Buffer, Discriminator); 915 addDIEEntry(Buffer, dwarf::DW_AT_discr, DiscMember); 916 } 917 } 918 919 // Add template parameters to a class, structure or union types. 920 if (Tag == dwarf::DW_TAG_class_type || 921 Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type) 922 addTemplateParams(Buffer, CTy->getTemplateParams()); 923 924 // Add elements to structure type. 925 DINodeArray Elements = CTy->getElements(); 926 for (const auto *Element : Elements) { 927 if (!Element) 928 continue; 929 if (auto *SP = dyn_cast<DISubprogram>(Element)) 930 getOrCreateSubprogramDIE(SP); 931 else if (auto *DDTy = dyn_cast<DIDerivedType>(Element)) { 932 if (DDTy->getTag() == dwarf::DW_TAG_friend) { 933 DIE &ElemDie = createAndAddDIE(dwarf::DW_TAG_friend, Buffer); 934 addType(ElemDie, DDTy->getBaseType(), dwarf::DW_AT_friend); 935 } else if (DDTy->isStaticMember()) { 936 getOrCreateStaticMemberDIE(DDTy); 937 } else if (Tag == dwarf::DW_TAG_variant_part) { 938 // When emitting a variant part, wrap each member in 939 // DW_TAG_variant. 940 DIE &Variant = createAndAddDIE(dwarf::DW_TAG_variant, Buffer); 941 if (const ConstantInt *CI = 942 dyn_cast_or_null<ConstantInt>(DDTy->getDiscriminantValue())) { 943 if (isUnsignedDIType(DD, Discriminator->getBaseType())) 944 addUInt(Variant, dwarf::DW_AT_discr_value, None, CI->getZExtValue()); 945 else 946 addSInt(Variant, dwarf::DW_AT_discr_value, None, CI->getSExtValue()); 947 } 948 constructMemberDIE(Variant, DDTy); 949 } else { 950 constructMemberDIE(Buffer, DDTy); 951 } 952 } else if (auto *Property = dyn_cast<DIObjCProperty>(Element)) { 953 DIE &ElemDie = createAndAddDIE(Property->getTag(), Buffer); 954 StringRef PropertyName = Property->getName(); 955 addString(ElemDie, dwarf::DW_AT_APPLE_property_name, PropertyName); 956 if (Property->getType()) 957 addType(ElemDie, Property->getType()); 958 addSourceLine(ElemDie, Property); 959 StringRef GetterName = Property->getGetterName(); 960 if (!GetterName.empty()) 961 addString(ElemDie, dwarf::DW_AT_APPLE_property_getter, GetterName); 962 StringRef SetterName = Property->getSetterName(); 963 if (!SetterName.empty()) 964 addString(ElemDie, dwarf::DW_AT_APPLE_property_setter, SetterName); 965 if (unsigned PropertyAttributes = Property->getAttributes()) 966 addUInt(ElemDie, dwarf::DW_AT_APPLE_property_attribute, None, 967 PropertyAttributes); 968 } else if (auto *Composite = dyn_cast<DICompositeType>(Element)) { 969 if (Composite->getTag() == dwarf::DW_TAG_variant_part) { 970 DIE &VariantPart = createAndAddDIE(Composite->getTag(), Buffer); 971 constructTypeDIE(VariantPart, Composite); 972 } 973 } 974 } 975 976 if (CTy->isAppleBlockExtension()) 977 addFlag(Buffer, dwarf::DW_AT_APPLE_block); 978 979 if (CTy->getExportSymbols()) 980 addFlag(Buffer, dwarf::DW_AT_export_symbols); 981 982 // This is outside the DWARF spec, but GDB expects a DW_AT_containing_type 983 // inside C++ composite types to point to the base class with the vtable. 984 // Rust uses DW_AT_containing_type to link a vtable to the type 985 // for which it was created. 986 if (auto *ContainingType = CTy->getVTableHolder()) 987 addDIEEntry(Buffer, dwarf::DW_AT_containing_type, 988 *getOrCreateTypeDIE(ContainingType)); 989 990 if (CTy->isObjcClassComplete()) 991 addFlag(Buffer, dwarf::DW_AT_APPLE_objc_complete_type); 992 993 // Add the type's non-standard calling convention. 994 uint8_t CC = 0; 995 if (CTy->isTypePassByValue()) 996 CC = dwarf::DW_CC_pass_by_value; 997 else if (CTy->isTypePassByReference()) 998 CC = dwarf::DW_CC_pass_by_reference; 999 if (CC) 1000 addUInt(Buffer, dwarf::DW_AT_calling_convention, dwarf::DW_FORM_data1, 1001 CC); 1002 break; 1003 } 1004 default: 1005 break; 1006 } 1007 1008 // Add name if not anonymous or intermediate type. 1009 if (!Name.empty()) 1010 addString(Buffer, dwarf::DW_AT_name, Name); 1011 1012 if (Tag == dwarf::DW_TAG_enumeration_type || 1013 Tag == dwarf::DW_TAG_class_type || Tag == dwarf::DW_TAG_structure_type || 1014 Tag == dwarf::DW_TAG_union_type) { 1015 // Add size if non-zero (derived types might be zero-sized.) 1016 // TODO: Do we care about size for enum forward declarations? 1017 if (Size) 1018 addUInt(Buffer, dwarf::DW_AT_byte_size, None, Size); 1019 else if (!CTy->isForwardDecl()) 1020 // Add zero size if it is not a forward declaration. 1021 addUInt(Buffer, dwarf::DW_AT_byte_size, None, 0); 1022 1023 // If we're a forward decl, say so. 1024 if (CTy->isForwardDecl()) 1025 addFlag(Buffer, dwarf::DW_AT_declaration); 1026 1027 // Add source line info if available. 1028 if (!CTy->isForwardDecl()) 1029 addSourceLine(Buffer, CTy); 1030 1031 // No harm in adding the runtime language to the declaration. 1032 unsigned RLang = CTy->getRuntimeLang(); 1033 if (RLang) 1034 addUInt(Buffer, dwarf::DW_AT_APPLE_runtime_class, dwarf::DW_FORM_data1, 1035 RLang); 1036 1037 // Add align info if available. 1038 if (uint32_t AlignInBytes = CTy->getAlignInBytes()) 1039 addUInt(Buffer, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata, 1040 AlignInBytes); 1041 } 1042 } 1043 1044 void DwarfUnit::constructTemplateTypeParameterDIE( 1045 DIE &Buffer, const DITemplateTypeParameter *TP) { 1046 DIE &ParamDIE = 1047 createAndAddDIE(dwarf::DW_TAG_template_type_parameter, Buffer); 1048 // Add the type if it exists, it could be void and therefore no type. 1049 if (TP->getType()) 1050 addType(ParamDIE, TP->getType()); 1051 if (!TP->getName().empty()) 1052 addString(ParamDIE, dwarf::DW_AT_name, TP->getName()); 1053 if (TP->isDefault() && (DD->getDwarfVersion() >= 5)) 1054 addFlag(ParamDIE, dwarf::DW_AT_default_value); 1055 } 1056 1057 void DwarfUnit::constructTemplateValueParameterDIE( 1058 DIE &Buffer, const DITemplateValueParameter *VP) { 1059 DIE &ParamDIE = createAndAddDIE(VP->getTag(), Buffer); 1060 1061 // Add the type if there is one, template template and template parameter 1062 // packs will not have a type. 1063 if (VP->getTag() == dwarf::DW_TAG_template_value_parameter) 1064 addType(ParamDIE, VP->getType()); 1065 if (!VP->getName().empty()) 1066 addString(ParamDIE, dwarf::DW_AT_name, VP->getName()); 1067 if (VP->isDefault() && (DD->getDwarfVersion() >= 5)) 1068 addFlag(ParamDIE, dwarf::DW_AT_default_value); 1069 if (Metadata *Val = VP->getValue()) { 1070 if (ConstantInt *CI = mdconst::dyn_extract<ConstantInt>(Val)) 1071 addConstantValue(ParamDIE, CI, VP->getType()); 1072 else if (GlobalValue *GV = mdconst::dyn_extract<GlobalValue>(Val)) { 1073 // We cannot describe the location of dllimport'd entities: the 1074 // computation of their address requires loads from the IAT. 1075 if (!GV->hasDLLImportStorageClass()) { 1076 // For declaration non-type template parameters (such as global values 1077 // and functions) 1078 DIELoc *Loc = new (DIEValueAllocator) DIELoc; 1079 addOpAddress(*Loc, Asm->getSymbol(GV)); 1080 // Emit DW_OP_stack_value to use the address as the immediate value of 1081 // the parameter, rather than a pointer to it. 1082 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_stack_value); 1083 addBlock(ParamDIE, dwarf::DW_AT_location, Loc); 1084 } 1085 } else if (VP->getTag() == dwarf::DW_TAG_GNU_template_template_param) { 1086 assert(isa<MDString>(Val)); 1087 addString(ParamDIE, dwarf::DW_AT_GNU_template_name, 1088 cast<MDString>(Val)->getString()); 1089 } else if (VP->getTag() == dwarf::DW_TAG_GNU_template_parameter_pack) { 1090 addTemplateParams(ParamDIE, cast<MDTuple>(Val)); 1091 } 1092 } 1093 } 1094 1095 DIE *DwarfUnit::getOrCreateNameSpace(const DINamespace *NS) { 1096 // Construct the context before querying for the existence of the DIE in case 1097 // such construction creates the DIE. 1098 DIE *ContextDIE = getOrCreateContextDIE(NS->getScope()); 1099 1100 if (DIE *NDie = getDIE(NS)) 1101 return NDie; 1102 DIE &NDie = createAndAddDIE(dwarf::DW_TAG_namespace, *ContextDIE, NS); 1103 1104 StringRef Name = NS->getName(); 1105 if (!Name.empty()) 1106 addString(NDie, dwarf::DW_AT_name, NS->getName()); 1107 else 1108 Name = "(anonymous namespace)"; 1109 DD->addAccelNamespace(*CUNode, Name, NDie); 1110 addGlobalName(Name, NDie, NS->getScope()); 1111 if (NS->getExportSymbols()) 1112 addFlag(NDie, dwarf::DW_AT_export_symbols); 1113 return &NDie; 1114 } 1115 1116 DIE *DwarfUnit::getOrCreateModule(const DIModule *M) { 1117 // Construct the context before querying for the existence of the DIE in case 1118 // such construction creates the DIE. 1119 DIE *ContextDIE = getOrCreateContextDIE(M->getScope()); 1120 1121 if (DIE *MDie = getDIE(M)) 1122 return MDie; 1123 DIE &MDie = createAndAddDIE(dwarf::DW_TAG_module, *ContextDIE, M); 1124 1125 if (!M->getName().empty()) { 1126 addString(MDie, dwarf::DW_AT_name, M->getName()); 1127 addGlobalName(M->getName(), MDie, M->getScope()); 1128 } 1129 if (!M->getConfigurationMacros().empty()) 1130 addString(MDie, dwarf::DW_AT_LLVM_config_macros, 1131 M->getConfigurationMacros()); 1132 if (!M->getIncludePath().empty()) 1133 addString(MDie, dwarf::DW_AT_LLVM_include_path, M->getIncludePath()); 1134 if (!M->getAPINotesFile().empty()) 1135 addString(MDie, dwarf::DW_AT_LLVM_apinotes, M->getAPINotesFile()); 1136 if (M->getFile()) 1137 addUInt(MDie, dwarf::DW_AT_decl_file, None, 1138 getOrCreateSourceID(M->getFile())); 1139 if (M->getLineNo()) 1140 addUInt(MDie, dwarf::DW_AT_decl_line, None, M->getLineNo()); 1141 1142 return &MDie; 1143 } 1144 1145 DIE *DwarfUnit::getOrCreateSubprogramDIE(const DISubprogram *SP, bool Minimal) { 1146 // Construct the context before querying for the existence of the DIE in case 1147 // such construction creates the DIE (as is the case for member function 1148 // declarations). 1149 DIE *ContextDIE = 1150 Minimal ? &getUnitDie() : getOrCreateContextDIE(SP->getScope()); 1151 1152 if (DIE *SPDie = getDIE(SP)) 1153 return SPDie; 1154 1155 if (auto *SPDecl = SP->getDeclaration()) { 1156 if (!Minimal) { 1157 // Add subprogram definitions to the CU die directly. 1158 ContextDIE = &getUnitDie(); 1159 // Build the decl now to ensure it precedes the definition. 1160 getOrCreateSubprogramDIE(SPDecl); 1161 } 1162 } 1163 1164 // DW_TAG_inlined_subroutine may refer to this DIE. 1165 DIE &SPDie = createAndAddDIE(dwarf::DW_TAG_subprogram, *ContextDIE, SP); 1166 1167 // Stop here and fill this in later, depending on whether or not this 1168 // subprogram turns out to have inlined instances or not. 1169 if (SP->isDefinition()) 1170 return &SPDie; 1171 1172 static_cast<DwarfUnit *>(SPDie.getUnit()) 1173 ->applySubprogramAttributes(SP, SPDie); 1174 return &SPDie; 1175 } 1176 1177 bool DwarfUnit::applySubprogramDefinitionAttributes(const DISubprogram *SP, 1178 DIE &SPDie) { 1179 DIE *DeclDie = nullptr; 1180 StringRef DeclLinkageName; 1181 if (auto *SPDecl = SP->getDeclaration()) { 1182 DITypeRefArray DeclArgs, DefinitionArgs; 1183 DeclArgs = SPDecl->getType()->getTypeArray(); 1184 DefinitionArgs = SP->getType()->getTypeArray(); 1185 1186 if (DeclArgs.size() && DefinitionArgs.size()) 1187 if (DefinitionArgs[0] != NULL && DeclArgs[0] != DefinitionArgs[0]) 1188 addType(SPDie, DefinitionArgs[0]); 1189 1190 DeclDie = getDIE(SPDecl); 1191 assert(DeclDie && "This DIE should've already been constructed when the " 1192 "definition DIE was created in " 1193 "getOrCreateSubprogramDIE"); 1194 // Look at the Decl's linkage name only if we emitted it. 1195 if (DD->useAllLinkageNames()) 1196 DeclLinkageName = SPDecl->getLinkageName(); 1197 unsigned DeclID = getOrCreateSourceID(SPDecl->getFile()); 1198 unsigned DefID = getOrCreateSourceID(SP->getFile()); 1199 if (DeclID != DefID) 1200 addUInt(SPDie, dwarf::DW_AT_decl_file, None, DefID); 1201 1202 if (SP->getLine() != SPDecl->getLine()) 1203 addUInt(SPDie, dwarf::DW_AT_decl_line, None, SP->getLine()); 1204 } 1205 1206 // Add function template parameters. 1207 addTemplateParams(SPDie, SP->getTemplateParams()); 1208 1209 // Add the linkage name if we have one and it isn't in the Decl. 1210 StringRef LinkageName = SP->getLinkageName(); 1211 assert(((LinkageName.empty() || DeclLinkageName.empty()) || 1212 LinkageName == DeclLinkageName) && 1213 "decl has a linkage name and it is different"); 1214 if (DeclLinkageName.empty() && 1215 // Always emit it for abstract subprograms. 1216 (DD->useAllLinkageNames() || DU->getAbstractSPDies().lookup(SP))) 1217 addLinkageName(SPDie, LinkageName); 1218 1219 if (!DeclDie) 1220 return false; 1221 1222 // Refer to the function declaration where all the other attributes will be 1223 // found. 1224 addDIEEntry(SPDie, dwarf::DW_AT_specification, *DeclDie); 1225 return true; 1226 } 1227 1228 void DwarfUnit::applySubprogramAttributes(const DISubprogram *SP, DIE &SPDie, 1229 bool SkipSPAttributes) { 1230 // If -fdebug-info-for-profiling is enabled, need to emit the subprogram 1231 // and its source location. 1232 bool SkipSPSourceLocation = SkipSPAttributes && 1233 !CUNode->getDebugInfoForProfiling(); 1234 if (!SkipSPSourceLocation) 1235 if (applySubprogramDefinitionAttributes(SP, SPDie)) 1236 return; 1237 1238 // Constructors and operators for anonymous aggregates do not have names. 1239 if (!SP->getName().empty()) 1240 addString(SPDie, dwarf::DW_AT_name, SP->getName()); 1241 1242 if (!SkipSPSourceLocation) 1243 addSourceLine(SPDie, SP); 1244 1245 // Skip the rest of the attributes under -gmlt to save space. 1246 if (SkipSPAttributes) 1247 return; 1248 1249 // Add the prototype if we have a prototype and we have a C like 1250 // language. 1251 uint16_t Language = getLanguage(); 1252 if (SP->isPrototyped() && 1253 (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 || 1254 Language == dwarf::DW_LANG_ObjC)) 1255 addFlag(SPDie, dwarf::DW_AT_prototyped); 1256 1257 if (SP->isObjCDirect()) 1258 addFlag(SPDie, dwarf::DW_AT_APPLE_objc_direct); 1259 1260 unsigned CC = 0; 1261 DITypeRefArray Args; 1262 if (const DISubroutineType *SPTy = SP->getType()) { 1263 Args = SPTy->getTypeArray(); 1264 CC = SPTy->getCC(); 1265 } 1266 1267 // Add a DW_AT_calling_convention if this has an explicit convention. 1268 if (CC && CC != dwarf::DW_CC_normal) 1269 addUInt(SPDie, dwarf::DW_AT_calling_convention, dwarf::DW_FORM_data1, CC); 1270 1271 // Add a return type. If this is a type like a C/C++ void type we don't add a 1272 // return type. 1273 if (Args.size()) 1274 if (auto Ty = Args[0]) 1275 addType(SPDie, Ty); 1276 1277 unsigned VK = SP->getVirtuality(); 1278 if (VK) { 1279 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, VK); 1280 if (SP->getVirtualIndex() != -1u) { 1281 DIELoc *Block = getDIELoc(); 1282 addUInt(*Block, dwarf::DW_FORM_data1, dwarf::DW_OP_constu); 1283 addUInt(*Block, dwarf::DW_FORM_udata, SP->getVirtualIndex()); 1284 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, Block); 1285 } 1286 ContainingTypeMap.insert(std::make_pair(&SPDie, SP->getContainingType())); 1287 } 1288 1289 if (!SP->isDefinition()) { 1290 addFlag(SPDie, dwarf::DW_AT_declaration); 1291 1292 // Add arguments. Do not add arguments for subprogram definition. They will 1293 // be handled while processing variables. 1294 constructSubprogramArguments(SPDie, Args); 1295 } 1296 1297 addThrownTypes(SPDie, SP->getThrownTypes()); 1298 1299 if (SP->isArtificial()) 1300 addFlag(SPDie, dwarf::DW_AT_artificial); 1301 1302 if (!SP->isLocalToUnit()) 1303 addFlag(SPDie, dwarf::DW_AT_external); 1304 1305 if (DD->useAppleExtensionAttributes()) { 1306 if (SP->isOptimized()) 1307 addFlag(SPDie, dwarf::DW_AT_APPLE_optimized); 1308 1309 if (unsigned isa = Asm->getISAEncoding()) 1310 addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa); 1311 } 1312 1313 if (SP->isLValueReference()) 1314 addFlag(SPDie, dwarf::DW_AT_reference); 1315 1316 if (SP->isRValueReference()) 1317 addFlag(SPDie, dwarf::DW_AT_rvalue_reference); 1318 1319 if (SP->isNoReturn()) 1320 addFlag(SPDie, dwarf::DW_AT_noreturn); 1321 1322 if (SP->isProtected()) 1323 addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, 1324 dwarf::DW_ACCESS_protected); 1325 else if (SP->isPrivate()) 1326 addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, 1327 dwarf::DW_ACCESS_private); 1328 else if (SP->isPublic()) 1329 addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, 1330 dwarf::DW_ACCESS_public); 1331 1332 if (SP->isExplicit()) 1333 addFlag(SPDie, dwarf::DW_AT_explicit); 1334 1335 if (SP->isMainSubprogram()) 1336 addFlag(SPDie, dwarf::DW_AT_main_subprogram); 1337 if (SP->isPure()) 1338 addFlag(SPDie, dwarf::DW_AT_pure); 1339 if (SP->isElemental()) 1340 addFlag(SPDie, dwarf::DW_AT_elemental); 1341 if (SP->isRecursive()) 1342 addFlag(SPDie, dwarf::DW_AT_recursive); 1343 1344 if (DD->getDwarfVersion() >= 5 && SP->isDeleted()) 1345 addFlag(SPDie, dwarf::DW_AT_deleted); 1346 } 1347 1348 void DwarfUnit::constructSubrangeDIE(DIE &Buffer, const DISubrange *SR, 1349 DIE *IndexTy) { 1350 DIE &DW_Subrange = createAndAddDIE(dwarf::DW_TAG_subrange_type, Buffer); 1351 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, *IndexTy); 1352 1353 // The LowerBound value defines the lower bounds which is typically zero for 1354 // C/C++. The Count value is the number of elements. Values are 64 bit. If 1355 // Count == -1 then the array is unbounded and we do not emit 1356 // DW_AT_lower_bound and DW_AT_count attributes. 1357 int64_t DefaultLowerBound = getDefaultLowerBound(); 1358 int64_t Count = -1; 1359 if (auto *CI = SR->getCount().dyn_cast<ConstantInt*>()) 1360 Count = CI->getSExtValue(); 1361 1362 auto addBoundTypeEntry = [&](dwarf::Attribute Attr, 1363 DISubrange::BoundType Bound) -> void { 1364 if (auto *BV = Bound.dyn_cast<DIVariable *>()) { 1365 if (auto *VarDIE = getDIE(BV)) 1366 addDIEEntry(DW_Subrange, Attr, *VarDIE); 1367 } else if (auto *BE = Bound.dyn_cast<DIExpression *>()) { 1368 DIELoc *Loc = new (DIEValueAllocator) DIELoc; 1369 DIEDwarfExpression DwarfExpr(*Asm, getCU(), *Loc); 1370 DwarfExpr.setMemoryLocationKind(); 1371 DwarfExpr.addExpression(BE); 1372 addBlock(DW_Subrange, Attr, DwarfExpr.finalize()); 1373 } else if (auto *BI = Bound.dyn_cast<ConstantInt *>()) { 1374 if (Attr != dwarf::DW_AT_lower_bound || DefaultLowerBound == -1 || 1375 BI->getSExtValue() != DefaultLowerBound) 1376 addSInt(DW_Subrange, Attr, dwarf::DW_FORM_sdata, BI->getSExtValue()); 1377 } 1378 }; 1379 1380 addBoundTypeEntry(dwarf::DW_AT_lower_bound, SR->getLowerBound()); 1381 1382 if (auto *CV = SR->getCount().dyn_cast<DIVariable*>()) { 1383 if (auto *CountVarDIE = getDIE(CV)) 1384 addDIEEntry(DW_Subrange, dwarf::DW_AT_count, *CountVarDIE); 1385 } else if (Count != -1) 1386 addUInt(DW_Subrange, dwarf::DW_AT_count, None, Count); 1387 1388 addBoundTypeEntry(dwarf::DW_AT_upper_bound, SR->getUpperBound()); 1389 1390 addBoundTypeEntry(dwarf::DW_AT_byte_stride, SR->getStride()); 1391 } 1392 1393 DIE *DwarfUnit::getIndexTyDie() { 1394 if (IndexTyDie) 1395 return IndexTyDie; 1396 // Construct an integer type to use for indexes. 1397 IndexTyDie = &createAndAddDIE(dwarf::DW_TAG_base_type, getUnitDie()); 1398 StringRef Name = "__ARRAY_SIZE_TYPE__"; 1399 addString(*IndexTyDie, dwarf::DW_AT_name, Name); 1400 addUInt(*IndexTyDie, dwarf::DW_AT_byte_size, None, sizeof(int64_t)); 1401 addUInt(*IndexTyDie, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1, 1402 dwarf::DW_ATE_unsigned); 1403 DD->addAccelType(*CUNode, Name, *IndexTyDie, /*Flags*/ 0); 1404 return IndexTyDie; 1405 } 1406 1407 /// Returns true if the vector's size differs from the sum of sizes of elements 1408 /// the user specified. This can occur if the vector has been rounded up to 1409 /// fit memory alignment constraints. 1410 static bool hasVectorBeenPadded(const DICompositeType *CTy) { 1411 assert(CTy && CTy->isVector() && "Composite type is not a vector"); 1412 const uint64_t ActualSize = CTy->getSizeInBits(); 1413 1414 // Obtain the size of each element in the vector. 1415 DIType *BaseTy = CTy->getBaseType(); 1416 assert(BaseTy && "Unknown vector element type."); 1417 const uint64_t ElementSize = BaseTy->getSizeInBits(); 1418 1419 // Locate the number of elements in the vector. 1420 const DINodeArray Elements = CTy->getElements(); 1421 assert(Elements.size() == 1 && 1422 Elements[0]->getTag() == dwarf::DW_TAG_subrange_type && 1423 "Invalid vector element array, expected one element of type subrange"); 1424 const auto Subrange = cast<DISubrange>(Elements[0]); 1425 const auto NumVecElements = 1426 Subrange->getCount() 1427 ? Subrange->getCount().get<ConstantInt *>()->getSExtValue() 1428 : 0; 1429 1430 // Ensure we found the element count and that the actual size is wide 1431 // enough to contain the requested size. 1432 assert(ActualSize >= (NumVecElements * ElementSize) && "Invalid vector size"); 1433 return ActualSize != (NumVecElements * ElementSize); 1434 } 1435 1436 void DwarfUnit::constructArrayTypeDIE(DIE &Buffer, const DICompositeType *CTy) { 1437 if (CTy->isVector()) { 1438 addFlag(Buffer, dwarf::DW_AT_GNU_vector); 1439 if (hasVectorBeenPadded(CTy)) 1440 addUInt(Buffer, dwarf::DW_AT_byte_size, None, 1441 CTy->getSizeInBits() / CHAR_BIT); 1442 } 1443 1444 if (DIVariable *Var = CTy->getDataLocation()) { 1445 if (auto *VarDIE = getDIE(Var)) 1446 addDIEEntry(Buffer, dwarf::DW_AT_data_location, *VarDIE); 1447 } else if (DIExpression *Expr = CTy->getDataLocationExp()) { 1448 DIELoc *Loc = new (DIEValueAllocator) DIELoc; 1449 DIEDwarfExpression DwarfExpr(*Asm, getCU(), *Loc); 1450 DwarfExpr.setMemoryLocationKind(); 1451 DwarfExpr.addExpression(Expr); 1452 addBlock(Buffer, dwarf::DW_AT_data_location, DwarfExpr.finalize()); 1453 } 1454 1455 if (DIVariable *Var = CTy->getAssociated()) { 1456 if (auto *VarDIE = getDIE(Var)) 1457 addDIEEntry(Buffer, dwarf::DW_AT_associated, *VarDIE); 1458 } else if (DIExpression *Expr = CTy->getAssociatedExp()) { 1459 DIELoc *Loc = new (DIEValueAllocator) DIELoc; 1460 DIEDwarfExpression DwarfExpr(*Asm, getCU(), *Loc); 1461 DwarfExpr.setMemoryLocationKind(); 1462 DwarfExpr.addExpression(Expr); 1463 addBlock(Buffer, dwarf::DW_AT_associated, DwarfExpr.finalize()); 1464 } 1465 1466 if (DIVariable *Var = CTy->getAllocated()) { 1467 if (auto *VarDIE = getDIE(Var)) 1468 addDIEEntry(Buffer, dwarf::DW_AT_allocated, *VarDIE); 1469 } else if (DIExpression *Expr = CTy->getAllocatedExp()) { 1470 DIELoc *Loc = new (DIEValueAllocator) DIELoc; 1471 DIEDwarfExpression DwarfExpr(*Asm, getCU(), *Loc); 1472 DwarfExpr.setMemoryLocationKind(); 1473 DwarfExpr.addExpression(Expr); 1474 addBlock(Buffer, dwarf::DW_AT_allocated, DwarfExpr.finalize()); 1475 } 1476 1477 // Emit the element type. 1478 addType(Buffer, CTy->getBaseType()); 1479 1480 // Get an anonymous type for index type. 1481 // FIXME: This type should be passed down from the front end 1482 // as different languages may have different sizes for indexes. 1483 DIE *IdxTy = getIndexTyDie(); 1484 1485 // Add subranges to array type. 1486 DINodeArray Elements = CTy->getElements(); 1487 for (unsigned i = 0, N = Elements.size(); i < N; ++i) { 1488 // FIXME: Should this really be such a loose cast? 1489 if (auto *Element = dyn_cast_or_null<DINode>(Elements[i])) 1490 if (Element->getTag() == dwarf::DW_TAG_subrange_type) 1491 constructSubrangeDIE(Buffer, cast<DISubrange>(Element), IdxTy); 1492 } 1493 } 1494 1495 void DwarfUnit::constructEnumTypeDIE(DIE &Buffer, const DICompositeType *CTy) { 1496 const DIType *DTy = CTy->getBaseType(); 1497 bool IsUnsigned = DTy && isUnsignedDIType(DD, DTy); 1498 if (DTy) { 1499 if (DD->getDwarfVersion() >= 3) 1500 addType(Buffer, DTy); 1501 if (DD->getDwarfVersion() >= 4 && (CTy->getFlags() & DINode::FlagEnumClass)) 1502 addFlag(Buffer, dwarf::DW_AT_enum_class); 1503 } 1504 1505 auto *Context = CTy->getScope(); 1506 bool IndexEnumerators = !Context || isa<DICompileUnit>(Context) || isa<DIFile>(Context) || 1507 isa<DINamespace>(Context) || isa<DICommonBlock>(Context); 1508 DINodeArray Elements = CTy->getElements(); 1509 1510 // Add enumerators to enumeration type. 1511 for (unsigned i = 0, N = Elements.size(); i < N; ++i) { 1512 auto *Enum = dyn_cast_or_null<DIEnumerator>(Elements[i]); 1513 if (Enum) { 1514 DIE &Enumerator = createAndAddDIE(dwarf::DW_TAG_enumerator, Buffer); 1515 StringRef Name = Enum->getName(); 1516 addString(Enumerator, dwarf::DW_AT_name, Name); 1517 addConstantValue(Enumerator, Enum->getValue(), IsUnsigned); 1518 if (IndexEnumerators) 1519 addGlobalName(Name, Enumerator, Context); 1520 } 1521 } 1522 } 1523 1524 void DwarfUnit::constructContainingTypeDIEs() { 1525 for (auto CI = ContainingTypeMap.begin(), CE = ContainingTypeMap.end(); 1526 CI != CE; ++CI) { 1527 DIE &SPDie = *CI->first; 1528 const DINode *D = CI->second; 1529 if (!D) 1530 continue; 1531 DIE *NDie = getDIE(D); 1532 if (!NDie) 1533 continue; 1534 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, *NDie); 1535 } 1536 } 1537 1538 DIE &DwarfUnit::constructMemberDIE(DIE &Buffer, const DIDerivedType *DT) { 1539 DIE &MemberDie = createAndAddDIE(DT->getTag(), Buffer); 1540 StringRef Name = DT->getName(); 1541 if (!Name.empty()) 1542 addString(MemberDie, dwarf::DW_AT_name, Name); 1543 1544 if (DIType *Resolved = DT->getBaseType()) 1545 addType(MemberDie, Resolved); 1546 1547 addSourceLine(MemberDie, DT); 1548 1549 if (DT->getTag() == dwarf::DW_TAG_inheritance && DT->isVirtual()) { 1550 1551 // For C++, virtual base classes are not at fixed offset. Use following 1552 // expression to extract appropriate offset from vtable. 1553 // BaseAddr = ObAddr + *((*ObAddr) - Offset) 1554 1555 DIELoc *VBaseLocationDie = new (DIEValueAllocator) DIELoc; 1556 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_dup); 1557 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref); 1558 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_constu); 1559 addUInt(*VBaseLocationDie, dwarf::DW_FORM_udata, DT->getOffsetInBits()); 1560 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_minus); 1561 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref); 1562 addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus); 1563 1564 addBlock(MemberDie, dwarf::DW_AT_data_member_location, VBaseLocationDie); 1565 } else { 1566 uint64_t Size = DT->getSizeInBits(); 1567 uint64_t FieldSize = DD->getBaseTypeSize(DT); 1568 uint32_t AlignInBytes = DT->getAlignInBytes(); 1569 uint64_t OffsetInBytes; 1570 1571 bool IsBitfield = FieldSize && Size != FieldSize; 1572 if (IsBitfield) { 1573 // Handle bitfield, assume bytes are 8 bits. 1574 if (DD->useDWARF2Bitfields()) 1575 addUInt(MemberDie, dwarf::DW_AT_byte_size, None, FieldSize/8); 1576 addUInt(MemberDie, dwarf::DW_AT_bit_size, None, Size); 1577 1578 uint64_t Offset = DT->getOffsetInBits(); 1579 // We can't use DT->getAlignInBits() here: AlignInBits for member type 1580 // is non-zero if and only if alignment was forced (e.g. _Alignas()), 1581 // which can't be done with bitfields. Thus we use FieldSize here. 1582 uint32_t AlignInBits = FieldSize; 1583 uint32_t AlignMask = ~(AlignInBits - 1); 1584 // The bits from the start of the storage unit to the start of the field. 1585 uint64_t StartBitOffset = Offset - (Offset & AlignMask); 1586 // The byte offset of the field's aligned storage unit inside the struct. 1587 OffsetInBytes = (Offset - StartBitOffset) / 8; 1588 1589 if (DD->useDWARF2Bitfields()) { 1590 uint64_t HiMark = (Offset + FieldSize) & AlignMask; 1591 uint64_t FieldOffset = (HiMark - FieldSize); 1592 Offset -= FieldOffset; 1593 1594 // Maybe we need to work from the other end. 1595 if (Asm->getDataLayout().isLittleEndian()) 1596 Offset = FieldSize - (Offset + Size); 1597 1598 addUInt(MemberDie, dwarf::DW_AT_bit_offset, None, Offset); 1599 OffsetInBytes = FieldOffset >> 3; 1600 } else { 1601 addUInt(MemberDie, dwarf::DW_AT_data_bit_offset, None, Offset); 1602 } 1603 } else { 1604 // This is not a bitfield. 1605 OffsetInBytes = DT->getOffsetInBits() / 8; 1606 if (AlignInBytes) 1607 addUInt(MemberDie, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata, 1608 AlignInBytes); 1609 } 1610 1611 if (DD->getDwarfVersion() <= 2) { 1612 DIELoc *MemLocationDie = new (DIEValueAllocator) DIELoc; 1613 addUInt(*MemLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst); 1614 addUInt(*MemLocationDie, dwarf::DW_FORM_udata, OffsetInBytes); 1615 addBlock(MemberDie, dwarf::DW_AT_data_member_location, MemLocationDie); 1616 } else if (!IsBitfield || DD->useDWARF2Bitfields()) 1617 addUInt(MemberDie, dwarf::DW_AT_data_member_location, None, 1618 OffsetInBytes); 1619 } 1620 1621 if (DT->isProtected()) 1622 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, 1623 dwarf::DW_ACCESS_protected); 1624 else if (DT->isPrivate()) 1625 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, 1626 dwarf::DW_ACCESS_private); 1627 // Otherwise C++ member and base classes are considered public. 1628 else if (DT->isPublic()) 1629 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, 1630 dwarf::DW_ACCESS_public); 1631 if (DT->isVirtual()) 1632 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, 1633 dwarf::DW_VIRTUALITY_virtual); 1634 1635 // Objective-C properties. 1636 if (DINode *PNode = DT->getObjCProperty()) 1637 if (DIE *PDie = getDIE(PNode)) 1638 MemberDie.addValue(DIEValueAllocator, dwarf::DW_AT_APPLE_property, 1639 dwarf::DW_FORM_ref4, DIEEntry(*PDie)); 1640 1641 if (DT->isArtificial()) 1642 addFlag(MemberDie, dwarf::DW_AT_artificial); 1643 1644 return MemberDie; 1645 } 1646 1647 DIE *DwarfUnit::getOrCreateStaticMemberDIE(const DIDerivedType *DT) { 1648 if (!DT) 1649 return nullptr; 1650 1651 // Construct the context before querying for the existence of the DIE in case 1652 // such construction creates the DIE. 1653 DIE *ContextDIE = getOrCreateContextDIE(DT->getScope()); 1654 assert(dwarf::isType(ContextDIE->getTag()) && 1655 "Static member should belong to a type."); 1656 1657 if (DIE *StaticMemberDIE = getDIE(DT)) 1658 return StaticMemberDIE; 1659 1660 DIE &StaticMemberDIE = createAndAddDIE(DT->getTag(), *ContextDIE, DT); 1661 1662 const DIType *Ty = DT->getBaseType(); 1663 1664 addString(StaticMemberDIE, dwarf::DW_AT_name, DT->getName()); 1665 addType(StaticMemberDIE, Ty); 1666 addSourceLine(StaticMemberDIE, DT); 1667 addFlag(StaticMemberDIE, dwarf::DW_AT_external); 1668 addFlag(StaticMemberDIE, dwarf::DW_AT_declaration); 1669 1670 // FIXME: We could omit private if the parent is a class_type, and 1671 // public if the parent is something else. 1672 if (DT->isProtected()) 1673 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, 1674 dwarf::DW_ACCESS_protected); 1675 else if (DT->isPrivate()) 1676 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, 1677 dwarf::DW_ACCESS_private); 1678 else if (DT->isPublic()) 1679 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, 1680 dwarf::DW_ACCESS_public); 1681 1682 if (const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(DT->getConstant())) 1683 addConstantValue(StaticMemberDIE, CI, Ty); 1684 if (const ConstantFP *CFP = dyn_cast_or_null<ConstantFP>(DT->getConstant())) 1685 addConstantFPValue(StaticMemberDIE, CFP); 1686 1687 if (uint32_t AlignInBytes = DT->getAlignInBytes()) 1688 addUInt(StaticMemberDIE, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata, 1689 AlignInBytes); 1690 1691 return &StaticMemberDIE; 1692 } 1693 1694 void DwarfUnit::emitCommonHeader(bool UseOffsets, dwarf::UnitType UT) { 1695 // Emit size of content not including length itself 1696 Asm->OutStreamer->AddComment("Length of Unit"); 1697 if (!DD->useSectionsAsReferences()) { 1698 StringRef Prefix = isDwoUnit() ? "debug_info_dwo_" : "debug_info_"; 1699 MCSymbol *BeginLabel = Asm->createTempSymbol(Prefix + "start"); 1700 EndLabel = Asm->createTempSymbol(Prefix + "end"); 1701 Asm->emitLabelDifference(EndLabel, BeginLabel, 4); 1702 Asm->OutStreamer->emitLabel(BeginLabel); 1703 } else 1704 Asm->emitInt32(getHeaderSize() + getUnitDie().getSize()); 1705 1706 Asm->OutStreamer->AddComment("DWARF version number"); 1707 unsigned Version = DD->getDwarfVersion(); 1708 Asm->emitInt16(Version); 1709 1710 // DWARF v5 reorders the address size and adds a unit type. 1711 if (Version >= 5) { 1712 Asm->OutStreamer->AddComment("DWARF Unit Type"); 1713 Asm->emitInt8(UT); 1714 Asm->OutStreamer->AddComment("Address Size (in bytes)"); 1715 Asm->emitInt8(Asm->MAI->getCodePointerSize()); 1716 } 1717 1718 // We share one abbreviations table across all units so it's always at the 1719 // start of the section. Use a relocatable offset where needed to ensure 1720 // linking doesn't invalidate that offset. 1721 Asm->OutStreamer->AddComment("Offset Into Abbrev. Section"); 1722 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering(); 1723 if (UseOffsets) 1724 Asm->emitInt32(0); 1725 else 1726 Asm->emitDwarfSymbolReference( 1727 TLOF.getDwarfAbbrevSection()->getBeginSymbol(), false); 1728 1729 if (Version <= 4) { 1730 Asm->OutStreamer->AddComment("Address Size (in bytes)"); 1731 Asm->emitInt8(Asm->MAI->getCodePointerSize()); 1732 } 1733 } 1734 1735 void DwarfTypeUnit::emitHeader(bool UseOffsets) { 1736 DwarfUnit::emitCommonHeader(UseOffsets, 1737 DD->useSplitDwarf() ? dwarf::DW_UT_split_type 1738 : dwarf::DW_UT_type); 1739 Asm->OutStreamer->AddComment("Type Signature"); 1740 Asm->OutStreamer->emitIntValue(TypeSignature, sizeof(TypeSignature)); 1741 Asm->OutStreamer->AddComment("Type DIE Offset"); 1742 // In a skeleton type unit there is no type DIE so emit a zero offset. 1743 Asm->OutStreamer->emitIntValue(Ty ? Ty->getOffset() : 0, 1744 sizeof(Ty->getOffset())); 1745 } 1746 1747 DIE::value_iterator 1748 DwarfUnit::addSectionDelta(DIE &Die, dwarf::Attribute Attribute, 1749 const MCSymbol *Hi, const MCSymbol *Lo) { 1750 return Die.addValue(DIEValueAllocator, Attribute, 1751 DD->getDwarfVersion() >= 4 ? dwarf::DW_FORM_sec_offset 1752 : dwarf::DW_FORM_data4, 1753 new (DIEValueAllocator) DIEDelta(Hi, Lo)); 1754 } 1755 1756 DIE::value_iterator 1757 DwarfUnit::addSectionLabel(DIE &Die, dwarf::Attribute Attribute, 1758 const MCSymbol *Label, const MCSymbol *Sec) { 1759 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections()) 1760 return addLabel(Die, Attribute, 1761 DD->getDwarfVersion() >= 4 ? dwarf::DW_FORM_sec_offset 1762 : dwarf::DW_FORM_data4, 1763 Label); 1764 return addSectionDelta(Die, Attribute, Label, Sec); 1765 } 1766 1767 bool DwarfTypeUnit::isDwoUnit() const { 1768 // Since there are no skeleton type units, all type units are dwo type units 1769 // when split DWARF is being used. 1770 return DD->useSplitDwarf(); 1771 } 1772 1773 void DwarfTypeUnit::addGlobalName(StringRef Name, const DIE &Die, 1774 const DIScope *Context) { 1775 getCU().addGlobalNameForTypeUnit(Name, Context); 1776 } 1777 1778 void DwarfTypeUnit::addGlobalType(const DIType *Ty, const DIE &Die, 1779 const DIScope *Context) { 1780 getCU().addGlobalTypeUnitType(Ty, Context); 1781 } 1782 1783 const MCSymbol *DwarfUnit::getCrossSectionRelativeBaseAddress() const { 1784 if (!Asm->MAI->doesDwarfUseRelocationsAcrossSections()) 1785 return nullptr; 1786 if (isDwoUnit()) 1787 return nullptr; 1788 return getSection()->getBeginSymbol(); 1789 } 1790 1791 void DwarfUnit::addStringOffsetsStart() { 1792 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering(); 1793 addSectionLabel(getUnitDie(), dwarf::DW_AT_str_offsets_base, 1794 DU->getStringOffsetsStartSym(), 1795 TLOF.getDwarfStrOffSection()->getBeginSymbol()); 1796 } 1797 1798 void DwarfUnit::addRnglistsBase() { 1799 assert(DD->getDwarfVersion() >= 5 && 1800 "DW_AT_rnglists_base requires DWARF version 5 or later"); 1801 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering(); 1802 addSectionLabel(getUnitDie(), dwarf::DW_AT_rnglists_base, 1803 DU->getRnglistsTableBaseSym(), 1804 TLOF.getDwarfRnglistsSection()->getBeginSymbol()); 1805 } 1806 1807 void DwarfTypeUnit::finishNonUnitTypeDIE(DIE& D, const DICompositeType *CTy) { 1808 addFlag(D, dwarf::DW_AT_declaration); 1809 StringRef Name = CTy->getName(); 1810 if (!Name.empty()) 1811 addString(D, dwarf::DW_AT_name, Name); 1812 getCU().createTypeDIE(CTy); 1813 } 1814