Bug Summary

File:llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp
Warning:line 891, column 38
Called C++ object pointer is null

Annotated Source Code

Press '?' to see keyboard shortcuts

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