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