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