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