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