LLVM 20.0.0git
DIE.cpp
Go to the documentation of this file.
1//===--- lib/CodeGen/DIE.cpp - DWARF Info Entries -------------------------===//
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// Data structures for DWARF info entries.
10//
11//===----------------------------------------------------------------------===//
12
13#include "llvm/CodeGen/DIE.h"
14#include "DwarfCompileUnit.h"
15#include "DwarfDebug.h"
17#include "llvm/Config/llvm-config.h"
18#include "llvm/MC/MCAsmInfo.h"
19#include "llvm/MC/MCStreamer.h"
20#include "llvm/MC/MCSymbol.h"
21#include "llvm/Support/Debug.h"
23#include "llvm/Support/Format.h"
24#include "llvm/Support/LEB128.h"
26using namespace llvm;
27
28#define DEBUG_TYPE "dwarfdebug"
29
30//===----------------------------------------------------------------------===//
31// DIEAbbrevData Implementation
32//===----------------------------------------------------------------------===//
33
34/// Profile - Used to gather unique data for the abbreviation folding set.
35///
37 // Explicitly cast to an integer type for which FoldingSetNodeID has
38 // overloads. Otherwise MSVC 2010 thinks this call is ambiguous.
39 ID.AddInteger(unsigned(Attribute));
40 ID.AddInteger(unsigned(Form));
41 if (Form == dwarf::DW_FORM_implicit_const)
42 ID.AddInteger(Value);
43}
44
45//===----------------------------------------------------------------------===//
46// DIEAbbrev Implementation
47//===----------------------------------------------------------------------===//
48
49/// Profile - Used to gather unique data for the abbreviation folding set.
50///
52 ID.AddInteger(unsigned(Tag));
53 ID.AddInteger(unsigned(Children));
54
55 // For each attribute description.
56 for (const DIEAbbrevData &D : Data)
57 D.Profile(ID);
58}
59
60/// Emit - Print the abbreviation using the specified asm printer.
61///
62void DIEAbbrev::Emit(const AsmPrinter *AP) const {
63 // Emit its Dwarf tag type.
64 AP->emitULEB128(Tag, dwarf::TagString(Tag).data());
65
66 // Emit whether it has children DIEs.
67 AP->emitULEB128((unsigned)Children, dwarf::ChildrenString(Children).data());
68
69 // For each attribute description.
70 for (const DIEAbbrevData &AttrData : Data) {
71 // Emit attribute type.
72 AP->emitULEB128(AttrData.getAttribute(),
73 dwarf::AttributeString(AttrData.getAttribute()).data());
74
75 // Emit form type.
76#ifndef NDEBUG
77 // Could be an assertion, but this way we can see the failing form code
78 // easily, which helps track down where it came from.
79 if (!dwarf::isValidFormForVersion(AttrData.getForm(),
80 AP->getDwarfVersion())) {
81 LLVM_DEBUG(dbgs() << "Invalid form " << format("0x%x", AttrData.getForm())
82 << " for DWARF version " << AP->getDwarfVersion()
83 << "\n");
84 llvm_unreachable("Invalid form for specified DWARF version");
85 }
86#endif
87 AP->emitULEB128(AttrData.getForm(),
88 dwarf::FormEncodingString(AttrData.getForm()).data());
89
90 // Emit value for DW_FORM_implicit_const.
91 if (AttrData.getForm() == dwarf::DW_FORM_implicit_const)
92 AP->emitSLEB128(AttrData.getValue());
93 }
94
95 // Mark end of abbreviation.
96 AP->emitULEB128(0, "EOM(1)");
97 AP->emitULEB128(0, "EOM(2)");
98}
99
102 O << "Abbreviation @"
103 << format("0x%lx", (long)(intptr_t)this)
104 << " "
105 << dwarf::TagString(Tag)
106 << " "
107 << dwarf::ChildrenString(Children)
108 << '\n';
109
110 for (const DIEAbbrevData &D : Data) {
111 O << " " << dwarf::AttributeString(D.getAttribute()) << " "
112 << dwarf::FormEncodingString(D.getForm());
113
114 if (D.getForm() == dwarf::DW_FORM_implicit_const)
115 O << " " << D.getValue();
116
117 O << '\n';
118 }
119}
120
121#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
123 print(dbgs());
124}
125#endif
126
127//===----------------------------------------------------------------------===//
128// DIEAbbrevSet Implementation
129//===----------------------------------------------------------------------===//
130
132 for (DIEAbbrev *Abbrev : Abbreviations)
133 Abbrev->~DIEAbbrev();
134}
135
137
139 DIEAbbrev Abbrev = Die.generateAbbrev();
140 Abbrev.Profile(ID);
141
142 void *InsertPos;
143 if (DIEAbbrev *Existing =
144 AbbreviationsSet.FindNodeOrInsertPos(ID, InsertPos)) {
145 Die.setAbbrevNumber(Existing->getNumber());
146 return *Existing;
147 }
148
149 // Move the abbreviation to the heap and assign a number.
150 DIEAbbrev *New = new (Alloc) DIEAbbrev(std::move(Abbrev));
151 Abbreviations.push_back(New);
152 New->setNumber(Abbreviations.size());
153 Die.setAbbrevNumber(Abbreviations.size());
154
155 // Store it for lookup.
156 AbbreviationsSet.InsertNode(New, InsertPos);
157 return *New;
158}
159
160void DIEAbbrevSet::Emit(const AsmPrinter *AP, MCSection *Section) const {
161 if (!Abbreviations.empty()) {
162 // Start the debug abbrev section.
163 AP->OutStreamer->switchSection(Section);
164 AP->emitDwarfAbbrevs(Abbreviations);
165 }
166}
167
168//===----------------------------------------------------------------------===//
169// DIE Implementation
170//===----------------------------------------------------------------------===//
171
172DIE *DIE::getParent() const { return dyn_cast_if_present<DIE *>(Owner); }
173
175 DIEAbbrev Abbrev(Tag, hasChildren());
176 for (const DIEValue &V : values())
177 if (V.getForm() == dwarf::DW_FORM_implicit_const)
178 Abbrev.AddImplicitConstAttribute(V.getAttribute(),
179 V.getDIEInteger().getValue());
180 else
181 Abbrev.AddAttribute(V.getAttribute(), V.getForm());
182 return Abbrev;
183}
184
186 const DIEUnit *Unit = getUnit();
187 assert(Unit && "DIE must be owned by a DIEUnit to get its absolute offset");
188 return Unit->getDebugSectionOffset() + getOffset();
189}
190
191const DIE *DIE::getUnitDie() const {
192 const DIE *p = this;
193 while (p) {
194 if (p->getTag() == dwarf::DW_TAG_compile_unit ||
195 p->getTag() == dwarf::DW_TAG_skeleton_unit ||
196 p->getTag() == dwarf::DW_TAG_type_unit)
197 return p;
198 p = p->getParent();
199 }
200 return nullptr;
201}
202
204 const DIE *UnitDie = getUnitDie();
205 if (UnitDie)
206 return dyn_cast_if_present<DIEUnit *>(UnitDie->Owner);
207 return nullptr;
208}
209
211 // Iterate through all the attributes until we find the one we're
212 // looking for, if we can't find it return NULL.
213 for (const auto &V : values())
214 if (V.getAttribute() == Attribute)
215 return V;
216 return DIEValue();
217}
218
220static void printValues(raw_ostream &O, const DIEValueList &Values,
221 StringRef Type, unsigned Size, unsigned IndentCount) {
222 O << Type << ": Size: " << Size << "\n";
223
224 unsigned I = 0;
225 const std::string Indent(IndentCount, ' ');
226 for (const auto &V : Values.values()) {
227 O << Indent;
228 O << "Blk[" << I++ << "]";
229 O << " " << dwarf::FormEncodingString(V.getForm()) << " ";
230 V.print(O);
231 O << "\n";
232 }
233}
234
236void DIE::print(raw_ostream &O, unsigned IndentCount) const {
237 const std::string Indent(IndentCount, ' ');
238 O << Indent << "Die: " << format("0x%lx", (long)(intptr_t) this)
239 << ", Offset: " << Offset << ", Size: " << Size << "\n";
240
241 O << Indent << dwarf::TagString(getTag()) << " "
243
244 IndentCount += 2;
245 for (const auto &V : values()) {
246 O << Indent;
247 O << dwarf::AttributeString(V.getAttribute());
248 O << " " << dwarf::FormEncodingString(V.getForm()) << " ";
249 V.print(O);
250 O << "\n";
251 }
252 IndentCount -= 2;
253
254 for (const auto &Child : children())
255 Child.print(O, IndentCount + 4);
256
257 O << "\n";
258}
259
260#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
262 print(dbgs());
263}
264#endif
265
267 DIEAbbrevSet &AbbrevSet,
268 unsigned CUOffset) {
269 // Unique the abbreviation and fill in the abbreviation number so this DIE
270 // can be emitted.
271 const DIEAbbrev &Abbrev = AbbrevSet.uniqueAbbreviation(*this);
272
273 // Set compile/type unit relative offset of this DIE.
274 setOffset(CUOffset);
275
276 // Add the byte size of the abbreviation code.
277 CUOffset += getULEB128Size(getAbbrevNumber());
278
279 // Add the byte size of all the DIE attribute values.
280 for (const auto &V : values())
281 CUOffset += V.sizeOf(FormParams);
282
283 // Let the children compute their offsets and abbreviation numbers.
284 if (hasChildren()) {
285 (void)Abbrev;
286 assert(Abbrev.hasChildren() && "Children flag not set");
287
288 for (auto &Child : children())
289 CUOffset =
290 Child.computeOffsetsAndAbbrevs(FormParams, AbbrevSet, CUOffset);
291
292 // Each child chain is terminated with a zero byte, adjust the offset.
293 CUOffset += sizeof(int8_t);
294 }
295
296 // Compute the byte size of this DIE and all of its children correctly. This
297 // is needed so that top level DIE can help the compile unit set its length
298 // correctly.
299 setSize(CUOffset - getOffset());
300 return CUOffset;
301}
302
303//===----------------------------------------------------------------------===//
304// DIEUnit Implementation
305//===----------------------------------------------------------------------===//
306DIEUnit::DIEUnit(dwarf::Tag UnitTag) : Die(UnitTag) {
307 Die.Owner = this;
308 assert((UnitTag == dwarf::DW_TAG_compile_unit ||
309 UnitTag == dwarf::DW_TAG_skeleton_unit ||
310 UnitTag == dwarf::DW_TAG_type_unit ||
311 UnitTag == dwarf::DW_TAG_partial_unit) &&
312 "expected a unit TAG");
313}
314
315void DIEValue::emitValue(const AsmPrinter *AP) const {
316 switch (Ty) {
317 case isNone:
318 llvm_unreachable("Expected valid DIEValue");
319#define HANDLE_DIEVALUE(T) \
320 case is##T: \
321 getDIE##T().emitValue(AP, Form); \
322 break;
323#include "llvm/CodeGen/DIEValue.def"
324 }
325}
326
327unsigned DIEValue::sizeOf(const dwarf::FormParams &FormParams) const {
328 switch (Ty) {
329 case isNone:
330 llvm_unreachable("Expected valid DIEValue");
331#define HANDLE_DIEVALUE(T) \
332 case is##T: \
333 return getDIE##T().sizeOf(FormParams, Form);
334#include "llvm/CodeGen/DIEValue.def"
335 }
336 llvm_unreachable("Unknown DIE kind");
337}
338
341 switch (Ty) {
342 case isNone:
343 llvm_unreachable("Expected valid DIEValue");
344#define HANDLE_DIEVALUE(T) \
345 case is##T: \
346 getDIE##T().print(O); \
347 break;
348#include "llvm/CodeGen/DIEValue.def"
349 }
350}
351
352#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
354 print(dbgs());
355}
356#endif
357
358//===----------------------------------------------------------------------===//
359// DIEInteger Implementation
360//===----------------------------------------------------------------------===//
361
362/// EmitValue - Emit integer of appropriate size.
363///
364void DIEInteger::emitValue(const AsmPrinter *Asm, dwarf::Form Form) const {
365 switch (Form) {
366 case dwarf::DW_FORM_implicit_const:
367 case dwarf::DW_FORM_flag_present:
368 // Emit something to keep the lines and comments in sync.
369 // FIXME: Is there a better way to do this?
370 Asm->OutStreamer->addBlankLine();
371 return;
372 case dwarf::DW_FORM_flag:
373 case dwarf::DW_FORM_ref1:
374 case dwarf::DW_FORM_data1:
375 case dwarf::DW_FORM_strx1:
376 case dwarf::DW_FORM_addrx1:
377 case dwarf::DW_FORM_ref2:
378 case dwarf::DW_FORM_data2:
379 case dwarf::DW_FORM_strx2:
380 case dwarf::DW_FORM_addrx2:
381 case dwarf::DW_FORM_strx3:
382 case dwarf::DW_FORM_addrx3:
383 case dwarf::DW_FORM_strp:
384 case dwarf::DW_FORM_ref4:
385 case dwarf::DW_FORM_data4:
386 case dwarf::DW_FORM_ref_sup4:
387 case dwarf::DW_FORM_strx4:
388 case dwarf::DW_FORM_addrx4:
389 case dwarf::DW_FORM_ref8:
390 case dwarf::DW_FORM_ref_sig8:
391 case dwarf::DW_FORM_data8:
392 case dwarf::DW_FORM_ref_sup8:
393 case dwarf::DW_FORM_GNU_ref_alt:
394 case dwarf::DW_FORM_GNU_strp_alt:
395 case dwarf::DW_FORM_line_strp:
396 case dwarf::DW_FORM_sec_offset:
397 case dwarf::DW_FORM_strp_sup:
398 case dwarf::DW_FORM_addr:
399 case dwarf::DW_FORM_ref_addr:
400 Asm->OutStreamer->emitIntValue(Integer,
401 sizeOf(Asm->getDwarfFormParams(), Form));
402 return;
403 case dwarf::DW_FORM_GNU_str_index:
404 case dwarf::DW_FORM_GNU_addr_index:
405 case dwarf::DW_FORM_ref_udata:
406 case dwarf::DW_FORM_strx:
407 case dwarf::DW_FORM_addrx:
408 case dwarf::DW_FORM_rnglistx:
409 case dwarf::DW_FORM_udata:
410 Asm->emitULEB128(Integer);
411 return;
412 case dwarf::DW_FORM_sdata:
413 Asm->emitSLEB128(Integer);
414 return;
415 default: llvm_unreachable("DIE Value form not supported yet");
416 }
417}
418
419/// sizeOf - Determine size of integer value in bytes.
420///
421unsigned DIEInteger::sizeOf(const dwarf::FormParams &FormParams,
422 dwarf::Form Form) const {
423 if (std::optional<uint8_t> FixedSize =
424 dwarf::getFixedFormByteSize(Form, FormParams))
425 return *FixedSize;
426
427 switch (Form) {
428 case dwarf::DW_FORM_GNU_str_index:
429 case dwarf::DW_FORM_GNU_addr_index:
430 case dwarf::DW_FORM_ref_udata:
431 case dwarf::DW_FORM_strx:
432 case dwarf::DW_FORM_addrx:
433 case dwarf::DW_FORM_rnglistx:
434 case dwarf::DW_FORM_udata:
435 return getULEB128Size(Integer);
436 case dwarf::DW_FORM_sdata:
437 return getSLEB128Size(Integer);
438 default: llvm_unreachable("DIE Value form not supported yet");
439 }
440}
441
444 O << "Int: " << (int64_t)Integer << " 0x";
445 O.write_hex(Integer);
446}
447
448//===----------------------------------------------------------------------===//
449// DIEExpr Implementation
450//===----------------------------------------------------------------------===//
451
452/// EmitValue - Emit expression value.
453///
454void DIEExpr::emitValue(const AsmPrinter *AP, dwarf::Form Form) const {
455 AP->emitDebugValue(Expr, sizeOf(AP->getDwarfFormParams(), Form));
456}
457
458/// SizeOf - Determine size of expression value in bytes.
459///
460unsigned DIEExpr::sizeOf(const dwarf::FormParams &FormParams,
461 dwarf::Form Form) const {
462 switch (Form) {
463 case dwarf::DW_FORM_data4:
464 return 4;
465 case dwarf::DW_FORM_data8:
466 return 8;
467 case dwarf::DW_FORM_sec_offset:
468 return FormParams.getDwarfOffsetByteSize();
469 default:
470 llvm_unreachable("DIE Value form not supported yet");
471 }
472}
473
475void DIEExpr::print(raw_ostream &O) const { O << "Expr: " << *Expr; }
476
477//===----------------------------------------------------------------------===//
478// DIELabel Implementation
479//===----------------------------------------------------------------------===//
480
481/// EmitValue - Emit label value.
482///
483void DIELabel::emitValue(const AsmPrinter *AP, dwarf::Form Form) const {
484 bool IsSectionRelative = Form != dwarf::DW_FORM_addr;
485 AP->emitLabelReference(Label, sizeOf(AP->getDwarfFormParams(), Form),
486 IsSectionRelative);
487}
488
489/// sizeOf - Determine size of label value in bytes.
490///
491unsigned DIELabel::sizeOf(const dwarf::FormParams &FormParams,
492 dwarf::Form Form) const {
493 switch (Form) {
494 case dwarf::DW_FORM_data4:
495 return 4;
496 case dwarf::DW_FORM_data8:
497 return 8;
498 case dwarf::DW_FORM_sec_offset:
499 case dwarf::DW_FORM_strp:
500 return FormParams.getDwarfOffsetByteSize();
501 case dwarf::DW_FORM_addr:
502 return FormParams.AddrSize;
503 default:
504 llvm_unreachable("DIE Value form not supported yet");
505 }
506}
507
509void DIELabel::print(raw_ostream &O) const { O << "Lbl: " << Label->getName(); }
510
511//===----------------------------------------------------------------------===//
512// DIEBaseTypeRef Implementation
513//===----------------------------------------------------------------------===//
514
516 uint64_t Offset = CU->ExprRefedBaseTypes[Index].Die->getOffset();
517 assert(Offset < (1ULL << (ULEB128PadSize * 7)) && "Offset wont fit");
518 AP->emitULEB128(Offset, nullptr, ULEB128PadSize);
519}
520
522 return ULEB128PadSize;
523}
524
526void DIEBaseTypeRef::print(raw_ostream &O) const { O << "BaseTypeRef: " << Index; }
527
528//===----------------------------------------------------------------------===//
529// DIEDelta Implementation
530//===----------------------------------------------------------------------===//
531
532/// EmitValue - Emit delta value.
533///
534void DIEDelta::emitValue(const AsmPrinter *AP, dwarf::Form Form) const {
535 AP->emitLabelDifference(LabelHi, LabelLo,
536 sizeOf(AP->getDwarfFormParams(), Form));
537}
538
539/// SizeOf - Determine size of delta value in bytes.
540///
541unsigned DIEDelta::sizeOf(const dwarf::FormParams &FormParams,
542 dwarf::Form Form) const {
543 switch (Form) {
544 case dwarf::DW_FORM_data4:
545 return 4;
546 case dwarf::DW_FORM_data8:
547 return 8;
548 case dwarf::DW_FORM_sec_offset:
549 return FormParams.getDwarfOffsetByteSize();
550 default:
551 llvm_unreachable("DIE Value form not supported yet");
552 }
553}
554
557 O << "Del: " << LabelHi->getName() << "-" << LabelLo->getName();
558}
559
560//===----------------------------------------------------------------------===//
561// DIEString Implementation
562//===----------------------------------------------------------------------===//
563
564/// EmitValue - Emit string value.
565///
566void DIEString::emitValue(const AsmPrinter *AP, dwarf::Form Form) const {
567 // Index of string in symbol table.
568 switch (Form) {
569 case dwarf::DW_FORM_GNU_str_index:
570 case dwarf::DW_FORM_strx:
571 case dwarf::DW_FORM_strx1:
572 case dwarf::DW_FORM_strx2:
573 case dwarf::DW_FORM_strx3:
574 case dwarf::DW_FORM_strx4:
575 DIEInteger(S.getIndex()).emitValue(AP, Form);
576 return;
577 case dwarf::DW_FORM_strp:
579 DIELabel(S.getSymbol()).emitValue(AP, Form);
580 else
581 DIEInteger(S.getOffset()).emitValue(AP, Form);
582 return;
583 default:
584 llvm_unreachable("Expected valid string form");
585 }
586}
587
588/// sizeOf - Determine size of delta value in bytes.
589///
590unsigned DIEString::sizeOf(const dwarf::FormParams &FormParams,
591 dwarf::Form Form) const {
592 // Index of string in symbol table.
593 switch (Form) {
594 case dwarf::DW_FORM_GNU_str_index:
595 case dwarf::DW_FORM_strx:
596 case dwarf::DW_FORM_strx1:
597 case dwarf::DW_FORM_strx2:
598 case dwarf::DW_FORM_strx3:
599 case dwarf::DW_FORM_strx4:
600 return DIEInteger(S.getIndex()).sizeOf(FormParams, Form);
601 case dwarf::DW_FORM_strp:
603 return DIELabel(S.getSymbol()).sizeOf(FormParams, Form);
604 return DIEInteger(S.getOffset()).sizeOf(FormParams, Form);
605 default:
606 llvm_unreachable("Expected valid string form");
607 }
608}
609
612 O << "String: " << S.getString();
613}
614
615//===----------------------------------------------------------------------===//
616// DIEInlineString Implementation
617//===----------------------------------------------------------------------===//
619 if (Form == dwarf::DW_FORM_string) {
620 AP->OutStreamer->emitBytes(S);
621 AP->emitInt8(0);
622 return;
623 }
624 llvm_unreachable("Expected valid string form");
625}
626
628 // Emit string bytes + NULL byte.
629 return S.size() + 1;
630}
631
634 O << "InlineString: " << S;
635}
636
637//===----------------------------------------------------------------------===//
638// DIEEntry Implementation
639//===----------------------------------------------------------------------===//
640
641/// EmitValue - Emit debug information entry offset.
642///
643void DIEEntry::emitValue(const AsmPrinter *AP, dwarf::Form Form) const {
644
645 switch (Form) {
646 case dwarf::DW_FORM_ref1:
647 case dwarf::DW_FORM_ref2:
648 case dwarf::DW_FORM_ref4:
649 case dwarf::DW_FORM_ref8:
650 AP->OutStreamer->emitIntValue(Entry->getOffset(),
651 sizeOf(AP->getDwarfFormParams(), Form));
652 return;
653
654 case dwarf::DW_FORM_ref_udata:
655 AP->emitULEB128(Entry->getOffset());
656 return;
657
658 case dwarf::DW_FORM_ref_addr: {
659 // Get the absolute offset for this DIE within the debug info/types section.
661 if (const MCSymbol *SectionSym =
663 AP->emitLabelPlusOffset(SectionSym, Addr,
664 sizeOf(AP->getDwarfFormParams(), Form), true);
665 return;
666 }
667
668 AP->OutStreamer->emitIntValue(Addr, sizeOf(AP->getDwarfFormParams(), Form));
669 return;
670 }
671 default:
672 llvm_unreachable("Improper form for DIE reference");
673 }
674}
675
676unsigned DIEEntry::sizeOf(const dwarf::FormParams &FormParams,
677 dwarf::Form Form) const {
678 switch (Form) {
679 case dwarf::DW_FORM_ref1:
680 return 1;
681 case dwarf::DW_FORM_ref2:
682 return 2;
683 case dwarf::DW_FORM_ref4:
684 return 4;
685 case dwarf::DW_FORM_ref8:
686 return 8;
687 case dwarf::DW_FORM_ref_udata:
688 return getULEB128Size(Entry->getOffset());
689 case dwarf::DW_FORM_ref_addr:
690 return FormParams.getRefAddrByteSize();
691
692 default:
693 llvm_unreachable("Improper form for DIE reference");
694 }
695}
696
699 O << format("Die: 0x%lx", (long)(intptr_t)&Entry);
700}
701
702//===----------------------------------------------------------------------===//
703// DIELoc Implementation
704//===----------------------------------------------------------------------===//
705
706unsigned DIELoc::computeSize(const dwarf::FormParams &FormParams) const {
707 if (!Size) {
708 for (const auto &V : values())
709 Size += V.sizeOf(FormParams);
710 }
711
712 return Size;
713}
714
715/// EmitValue - Emit location data.
716///
717void DIELoc::emitValue(const AsmPrinter *Asm, dwarf::Form Form) const {
718 switch (Form) {
719 default: llvm_unreachable("Improper form for block");
720 case dwarf::DW_FORM_block1: Asm->emitInt8(Size); break;
721 case dwarf::DW_FORM_block2: Asm->emitInt16(Size); break;
722 case dwarf::DW_FORM_block4: Asm->emitInt32(Size); break;
723 case dwarf::DW_FORM_block:
724 case dwarf::DW_FORM_exprloc:
725 Asm->emitULEB128(Size);
726 break;
727 }
728
729 for (const auto &V : values())
730 V.emitValue(Asm);
731}
732
733/// sizeOf - Determine size of location data in bytes.
734///
735unsigned DIELoc::sizeOf(const dwarf::FormParams &, dwarf::Form Form) const {
736 switch (Form) {
737 case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
738 case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
739 case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
740 case dwarf::DW_FORM_block:
741 case dwarf::DW_FORM_exprloc:
742 return Size + getULEB128Size(Size);
743 default: llvm_unreachable("Improper form for block");
744 }
745}
746
749 printValues(O, *this, "ExprLoc", Size, 5);
750}
751
752//===----------------------------------------------------------------------===//
753// DIEBlock Implementation
754//===----------------------------------------------------------------------===//
755
756unsigned DIEBlock::computeSize(const dwarf::FormParams &FormParams) const {
757 if (!Size) {
758 for (const auto &V : values())
759 Size += V.sizeOf(FormParams);
760 }
761
762 return Size;
763}
764
765/// EmitValue - Emit block data.
766///
767void DIEBlock::emitValue(const AsmPrinter *Asm, dwarf::Form Form) const {
768 switch (Form) {
769 default: llvm_unreachable("Improper form for block");
770 case dwarf::DW_FORM_block1: Asm->emitInt8(Size); break;
771 case dwarf::DW_FORM_block2: Asm->emitInt16(Size); break;
772 case dwarf::DW_FORM_block4: Asm->emitInt32(Size); break;
773 case dwarf::DW_FORM_exprloc:
774 case dwarf::DW_FORM_block:
775 Asm->emitULEB128(Size);
776 break;
777 case dwarf::DW_FORM_string: break;
778 case dwarf::DW_FORM_data16: break;
779 }
780
781 for (const auto &V : values())
782 V.emitValue(Asm);
783}
784
785/// sizeOf - Determine size of block data in bytes.
786///
787unsigned DIEBlock::sizeOf(const dwarf::FormParams &, dwarf::Form Form) const {
788 switch (Form) {
789 case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
790 case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
791 case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
792 case dwarf::DW_FORM_exprloc:
793 case dwarf::DW_FORM_block: return Size + getULEB128Size(Size);
794 case dwarf::DW_FORM_data16: return 16;
795 default: llvm_unreachable("Improper form for block");
796 }
797}
798
801 printValues(O, *this, "Blk", Size, 5);
802}
803
804//===----------------------------------------------------------------------===//
805// DIELocList Implementation
806//===----------------------------------------------------------------------===//
807
808unsigned DIELocList::sizeOf(const dwarf::FormParams &FormParams,
809 dwarf::Form Form) const {
810 switch (Form) {
811 case dwarf::DW_FORM_loclistx:
812 return getULEB128Size(Index);
813 case dwarf::DW_FORM_data4:
814 assert(FormParams.Format != dwarf::DWARF64 &&
815 "DW_FORM_data4 is not suitable to emit a pointer to a location list "
816 "in the 64-bit DWARF format");
817 return 4;
818 case dwarf::DW_FORM_data8:
819 assert(FormParams.Format == dwarf::DWARF64 &&
820 "DW_FORM_data8 is not suitable to emit a pointer to a location list "
821 "in the 32-bit DWARF format");
822 return 8;
823 case dwarf::DW_FORM_sec_offset:
824 return FormParams.getDwarfOffsetByteSize();
825 default:
826 llvm_unreachable("DIE Value form not supported yet");
827 }
828}
829
830/// EmitValue - Emit label value.
831///
832void DIELocList::emitValue(const AsmPrinter *AP, dwarf::Form Form) const {
833 if (Form == dwarf::DW_FORM_loclistx) {
834 AP->emitULEB128(Index);
835 return;
836 }
837 DwarfDebug *DD = AP->getDwarfDebug();
838 MCSymbol *Label = DD->getDebugLocs().getList(Index).Label;
839 AP->emitDwarfSymbolReference(Label, /*ForceOffset*/ DD->useSplitDwarf());
840}
841
843void DIELocList::print(raw_ostream &O) const { O << "LocList: " << Index; }
844
845//===----------------------------------------------------------------------===//
846// DIEAddrOffset Implementation
847//===----------------------------------------------------------------------===//
848
849unsigned DIEAddrOffset::sizeOf(const dwarf::FormParams &FormParams,
850 dwarf::Form) const {
851 return Addr.sizeOf(FormParams, dwarf::DW_FORM_addrx) +
852 Offset.sizeOf(FormParams, dwarf::DW_FORM_data4);
853}
854
855/// EmitValue - Emit label value.
856///
858 Addr.emitValue(AP, dwarf::DW_FORM_addrx);
859 Offset.emitValue(AP, dwarf::DW_FORM_data4);
860}
861
864 O << "AddrOffset: ";
865 Addr.print(O);
866 O << " + ";
867 Offset.print(O);
868}
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition: Compiler.h:622
static LLVM_DUMP_METHOD void printValues(raw_ostream &O, const DIEValueList &Values, StringRef Type, unsigned Size, unsigned IndentCount)
Definition: DIE.cpp:220
#define LLVM_DEBUG(...)
Definition: Debug.h:106
uint64_t Addr
uint64_t Size
#define I(x, y, z)
Definition: MD5.cpp:58
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:86
void emitULEB128(uint64_t Value, const char *Desc=nullptr, unsigned PadTo=0) const
Emit the specified unsigned leb128 value.
void emitDwarfSymbolReference(const MCSymbol *Label, bool ForceOffset=false) const
Emit a reference to a symbol for use in dwarf.
virtual void emitDebugValue(const MCExpr *Value, unsigned Size) const
Emit the directive and value for debug thread local expression.
Definition: AsmPrinter.cpp:920
DwarfDebug * getDwarfDebug()
Definition: AsmPrinter.h:237
void emitLabelPlusOffset(const MCSymbol *Label, uint64_t Offset, unsigned Size, bool IsSectionRelative=false) const
Emit something like ".long Label+Offset" where the size in bytes of the directive is specified by Siz...
void emitLabelDifference(const MCSymbol *Hi, const MCSymbol *Lo, unsigned Size) const
Emit something like ".long Hi-Lo" where the size in bytes of the directive is specified by Size and H...
void emitInt8(int Value) const
Emit a byte directive and value.
void emitDwarfAbbrevs(const T &Abbrevs) const
Emit Dwarf abbreviation table.
Definition: AsmPrinter.h:772
void emitSLEB128(int64_t Value, const char *Desc=nullptr) const
Emit the specified signed leb128 value.
std::unique_ptr< MCStreamer > OutStreamer
This is the MCStreamer object for the file we are generating.
Definition: AsmPrinter.h:101
void emitLabelReference(const MCSymbol *Label, unsigned Size, bool IsSectionRelative=false) const
Emit something like ".long Label" where the size in bytes of the directive is specified by Size and L...
Definition: AsmPrinter.h:692
uint16_t getDwarfVersion() const
dwarf::FormParams getDwarfFormParams() const
Returns information about the byte size of DW_FORM values.
bool doesDwarfUseRelocationsAcrossSections() const
Definition: AsmPrinter.h:322
Dwarf abbreviation data, describes one attribute of a Dwarf abbreviation.
Definition: DIE.h:49
void Profile(FoldingSetNodeID &ID) const
Used to gather unique data for the abbreviation folding set.
Definition: DIE.cpp:36
Helps unique DIEAbbrev objects and assigns abbreviation numbers.
Definition: DIE.h:140
void Emit(const AsmPrinter *AP, MCSection *Section) const
Print all abbreviations using the specified asm printer.
Definition: DIE.cpp:160
DIEAbbrev & uniqueAbbreviation(DIE &Die)
Generate the abbreviation declaration for a DIE and return a pointer to the generated abbreviation.
Definition: DIE.cpp:136
Dwarf abbreviation, describes the organization of a debug information object.
Definition: DIE.h:79
void print(raw_ostream &O) const
Definition: DIE.cpp:101
void AddImplicitConstAttribute(dwarf::Attribute Attribute, int64_t Value)
Adds attribute with DW_FORM_implicit_const value.
Definition: DIE.h:114
void Emit(const AsmPrinter *AP) const
Print the abbreviation using the specified asm printer.
Definition: DIE.cpp:62
void AddAttribute(dwarf::Attribute Attribute, dwarf::Form Form)
Adds another set of attribute information to the abbreviation.
Definition: DIE.h:109
void dump() const
Definition: DIE.cpp:122
void Profile(FoldingSetNodeID &ID) const
Used to gather unique data for the abbreviation folding set.
Definition: DIE.cpp:51
bool hasChildren() const
Definition: DIE.h:102
void print(raw_ostream &O) const
Definition: DIE.cpp:863
unsigned sizeOf(const dwarf::FormParams &FormParams, dwarf::Form Form) const
Definition: DIE.cpp:849
void emitValue(const AsmPrinter *AP, dwarf::Form Form) const
EmitValue - Emit label value.
Definition: DIE.cpp:857
void emitValue(const AsmPrinter *AP, dwarf::Form Form) const
EmitValue - Emit base type reference.
Definition: DIE.cpp:515
void print(raw_ostream &O) const
Definition: DIE.cpp:526
unsigned sizeOf(const dwarf::FormParams &, dwarf::Form) const
sizeOf - Determine size of the base type reference in bytes.
Definition: DIE.cpp:521
unsigned sizeOf(const dwarf::FormParams &, dwarf::Form Form) const
sizeOf - Determine size of block data in bytes.
Definition: DIE.cpp:787
void print(raw_ostream &O) const
Definition: DIE.cpp:800
void emitValue(const AsmPrinter *Asm, dwarf::Form Form) const
EmitValue - Emit block data.
Definition: DIE.cpp:767
unsigned computeSize(const dwarf::FormParams &FormParams) const
Calculate the size of the location expression.
Definition: DIE.cpp:756
void print(raw_ostream &O) const
Definition: DIE.cpp:556
unsigned sizeOf(const dwarf::FormParams &FormParams, dwarf::Form Form) const
SizeOf - Determine size of delta value in bytes.
Definition: DIE.cpp:541
void emitValue(const AsmPrinter *AP, dwarf::Form Form) const
EmitValue - Emit delta value.
Definition: DIE.cpp:534
void emitValue(const AsmPrinter *AP, dwarf::Form Form) const
EmitValue - Emit debug information entry offset.
Definition: DIE.cpp:643
void print(raw_ostream &O) const
Definition: DIE.cpp:698
unsigned sizeOf(const dwarf::FormParams &FormParams, dwarf::Form Form) const
Definition: DIE.cpp:676
void print(raw_ostream &O) const
Definition: DIE.cpp:475
void emitValue(const AsmPrinter *AP, dwarf::Form Form) const
EmitValue - Emit expression value.
Definition: DIE.cpp:454
unsigned sizeOf(const dwarf::FormParams &FormParams, dwarf::Form Form) const
SizeOf - Determine size of expression value in bytes.
Definition: DIE.cpp:460
void emitValue(const AsmPrinter *AP, dwarf::Form Form) const
Definition: DIE.cpp:618
void print(raw_ostream &O) const
Definition: DIE.cpp:633
unsigned sizeOf(const dwarf::FormParams &, dwarf::Form) const
Definition: DIE.cpp:627
An integer value DIE.
Definition: DIE.h:168
unsigned sizeOf(const dwarf::FormParams &FormParams, dwarf::Form Form) const
sizeOf - Determine size of integer value in bytes.
Definition: DIE.cpp:421
void print(raw_ostream &O) const
Definition: DIE.cpp:443
void emitValue(const AsmPrinter *Asm, dwarf::Form Form) const
EmitValue - Emit integer of appropriate size.
Definition: DIE.cpp:364
A label DIE.
Definition: DIE.h:223
void emitValue(const AsmPrinter *AP, dwarf::Form Form) const
EmitValue - Emit label value.
Definition: DIE.cpp:483
void print(raw_ostream &O) const
Definition: DIE.cpp:509
unsigned sizeOf(const dwarf::FormParams &FormParams, dwarf::Form Form) const
sizeOf - Determine size of label value in bytes.
Definition: DIE.cpp:491
void print(raw_ostream &O) const
Definition: DIE.cpp:843
unsigned sizeOf(const dwarf::FormParams &FormParams, dwarf::Form Form) const
Definition: DIE.cpp:808
void emitValue(const AsmPrinter *AP, dwarf::Form Form) const
EmitValue - Emit label value.
Definition: DIE.cpp:832
void print(raw_ostream &O) const
Definition: DIE.cpp:748
unsigned sizeOf(const dwarf::FormParams &, dwarf::Form Form) const
sizeOf - Determine size of location data in bytes.
Definition: DIE.cpp:735
void emitValue(const AsmPrinter *Asm, dwarf::Form Form) const
EmitValue - Emit location data.
Definition: DIE.cpp:717
unsigned computeSize(const dwarf::FormParams &FormParams) const
Calculate the size of the location expression.
Definition: DIE.cpp:706
void emitValue(const AsmPrinter *AP, dwarf::Form Form) const
EmitValue - Emit string value.
Definition: DIE.cpp:566
void print(raw_ostream &O) const
Definition: DIE.cpp:611
unsigned sizeOf(const dwarf::FormParams &FormParams, dwarf::Form Form) const
sizeOf - Determine size of delta value in bytes.
Definition: DIE.cpp:590
Represents a compile or type unit.
Definition: DIE.h:960
virtual const MCSymbol * getCrossSectionRelativeBaseAddress() const
Definition: DIE.h:989
DIEUnit(dwarf::Tag UnitTag)
Definition: DIE.cpp:306
A list of DIE values.
Definition: DIE.h:689
value_range values()
Definition: DIE.h:807
void print(raw_ostream &O) const
Definition: DIE.cpp:340
void emitValue(const AsmPrinter *AP) const
Emit value via the Dwarf writer.
Definition: DIE.cpp:315
unsigned sizeOf(const dwarf::FormParams &FormParams) const
Return the size of a value in bytes.
Definition: DIE.cpp:327
void dump() const
Definition: DIE.cpp:353
A structured debug information entry.
Definition: DIE.h:819
DIEValue findAttribute(dwarf::Attribute Attribute) const
Find a value in the DIE with the attribute given.
Definition: DIE.cpp:210
void print(raw_ostream &O, unsigned IndentCount=0) const
Definition: DIE.cpp:236
unsigned getAbbrevNumber() const
Definition: DIE.h:854
DIEAbbrev generateAbbrev() const
Generate the abbreviation for this DIE.
Definition: DIE.cpp:174
unsigned computeOffsetsAndAbbrevs(const dwarf::FormParams &FormParams, DIEAbbrevSet &AbbrevSet, unsigned CUOffset)
Compute the offset of this DIE and all its children.
Definition: DIE.cpp:266
void setSize(unsigned S)
Definition: DIE.h:931
DIEUnit * getUnit() const
Climb up the parent chain to get the compile unit or type unit that this DIE belongs to.
Definition: DIE.cpp:203
child_range children()
Definition: DIE.h:875
const DIE * getUnitDie() const
Climb up the parent chain to get the compile unit or type unit DIE that this DIE belongs to.
Definition: DIE.cpp:191
void setAbbrevNumber(unsigned I)
Set the abbreviation number for this DIE.
Definition: DIE.h:891
unsigned getOffset() const
Get the compile/type unit relative offset of this DIE.
Definition: DIE.h:857
void setOffset(unsigned O)
Definition: DIE.h:930
bool hasChildren() const
Definition: DIE.h:867
uint64_t getDebugSectionOffset() const
Get the absolute offset within the .debug_info or .debug_types section for this DIE.
Definition: DIE.cpp:185
dwarf::Tag getTag() const
Definition: DIE.h:855
void dump() const
Definition: DIE.cpp:261
DIE * getParent() const
Definition: DIE.cpp:172
const List & getList(size_t LI) const
Collects and handles dwarf debug information.
Definition: DwarfDebug.h:351
const DebugLocStream & getDebugLocs() const
Returns the entries for the .debug_loc section.
Definition: DwarfDebug.h:854
bool useSplitDwarf() const
Returns whether or not to change the current debug info for the split dwarf proposal support.
Definition: DwarfDebug.h:815
FoldingSetNodeID - This class is used to gather all the unique data bits of a node.
Definition: FoldingSet.h:327
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition: MCSection.h:36
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
StringRef getName() const
getName - Get the symbol name.
Definition: MCSymbol.h:205
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
constexpr size_t size() const
size - Get the string size.
Definition: StringRef.h:150
constexpr const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:144
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
LLVM Value Representation.
Definition: Value.h:74
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
StringRef AttributeString(unsigned Attribute)
Definition: Dwarf.cpp:72
StringRef FormEncodingString(unsigned Encoding)
Definition: Dwarf.cpp:105
StringRef ChildrenString(unsigned Children)
Definition: Dwarf.cpp:62
StringRef TagString(unsigned Tag)
Definition: Dwarf.cpp:21
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
Attribute
Attributes.
Definition: Dwarf.h:123
@ DWARF64
Definition: Dwarf.h:91
bool isValidFormForVersion(Form F, unsigned Version, bool ExtensionsOk=true)
Tells whether the specified form is defined in the specified version, or is an extension if extension...
Definition: Dwarf.cpp:859
std::optional< uint8_t > getFixedFormByteSize(dwarf::Form Form, FormParams Params)
Get the fixed byte size for a given form.
Definition: Dwarf.cpp:771
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:480
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition: Format.h:125
unsigned getULEB128Size(uint64_t Value)
Utility function to get the size of the ULEB128-encoded value.
Definition: LEB128.cpp:19
unsigned getSLEB128Size(int64_t Value)
Utility function to get the size of the SLEB128-encoded value.
Definition: LEB128.cpp:29
A helper struct providing information about the byte size of DW_FORM values that vary in size dependi...
Definition: Dwarf.h:1084
bool DwarfUsesRelocationsAcrossSections
True if DWARF v2 output generally uses relocations for references to other .debug_* sections.
Definition: Dwarf.h:1090
DwarfFormat Format
Definition: Dwarf.h:1087
uint8_t getDwarfOffsetByteSize() const
The size of a reference is determined by the DWARF 32/64-bit format.
Definition: Dwarf.h:1102
uint8_t getRefAddrByteSize() const
The definition of the size of form DW_FORM_ref_addr depends on the version.
Definition: Dwarf.h:1095