1 //===--- lib/CodeGen/DIE.cpp - DWARF Info Entries -------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // Data structures for DWARF info entries.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/CodeGen/DIE.h"
14 #include "DwarfCompileUnit.h"
15 #include "DwarfDebug.h"
16 #include "DwarfUnit.h"
17 #include "llvm/ADT/Twine.h"
18 #include "llvm/CodeGen/AsmPrinter.h"
19 #include "llvm/Config/llvm-config.h"
20 #include "llvm/IR/DataLayout.h"
21 #include "llvm/MC/MCAsmInfo.h"
22 #include "llvm/MC/MCContext.h"
23 #include "llvm/MC/MCStreamer.h"
24 #include "llvm/MC/MCSymbol.h"
25 #include "llvm/Support/Debug.h"
26 #include "llvm/Support/ErrorHandling.h"
27 #include "llvm/Support/Format.h"
28 #include "llvm/Support/FormattedStream.h"
29 #include "llvm/Support/LEB128.h"
30 #include "llvm/Support/MD5.h"
31 #include "llvm/Support/raw_ostream.h"
34 #define DEBUG_TYPE "dwarfdebug"
36 //===----------------------------------------------------------------------===//
37 // DIEAbbrevData Implementation
38 //===----------------------------------------------------------------------===//
40 /// Profile - Used to gather unique data for the abbreviation folding set.
42 void DIEAbbrevData::Profile(FoldingSetNodeID
&ID
) const {
43 // Explicitly cast to an integer type for which FoldingSetNodeID has
44 // overloads. Otherwise MSVC 2010 thinks this call is ambiguous.
45 ID
.AddInteger(unsigned(Attribute
));
46 ID
.AddInteger(unsigned(Form
));
47 if (Form
== dwarf::DW_FORM_implicit_const
)
51 //===----------------------------------------------------------------------===//
52 // DIEAbbrev Implementation
53 //===----------------------------------------------------------------------===//
55 /// Profile - Used to gather unique data for the abbreviation folding set.
57 void DIEAbbrev::Profile(FoldingSetNodeID
&ID
) const {
58 ID
.AddInteger(unsigned(Tag
));
59 ID
.AddInteger(unsigned(Children
));
61 // For each attribute description.
62 for (unsigned i
= 0, N
= Data
.size(); i
< N
; ++i
)
66 /// Emit - Print the abbreviation using the specified asm printer.
68 void DIEAbbrev::Emit(const AsmPrinter
*AP
) const {
69 // Emit its Dwarf tag type.
70 AP
->emitULEB128(Tag
, dwarf::TagString(Tag
).data());
72 // Emit whether it has children DIEs.
73 AP
->emitULEB128((unsigned)Children
, dwarf::ChildrenString(Children
).data());
75 // For each attribute description.
76 for (unsigned i
= 0, N
= Data
.size(); i
< N
; ++i
) {
77 const DIEAbbrevData
&AttrData
= Data
[i
];
79 // Emit attribute type.
80 AP
->emitULEB128(AttrData
.getAttribute(),
81 dwarf::AttributeString(AttrData
.getAttribute()).data());
85 // Could be an assertion, but this way we can see the failing form code
86 // easily, which helps track down where it came from.
87 if (!dwarf::isValidFormForVersion(AttrData
.getForm(),
88 AP
->getDwarfVersion())) {
89 LLVM_DEBUG(dbgs() << "Invalid form " << format("0x%x", AttrData
.getForm())
90 << " for DWARF version " << AP
->getDwarfVersion()
92 llvm_unreachable("Invalid form for specified DWARF version");
95 AP
->emitULEB128(AttrData
.getForm(),
96 dwarf::FormEncodingString(AttrData
.getForm()).data());
98 // Emit value for DW_FORM_implicit_const.
99 if (AttrData
.getForm() == dwarf::DW_FORM_implicit_const
)
100 AP
->emitSLEB128(AttrData
.getValue());
103 // Mark end of abbreviation.
104 AP
->emitULEB128(0, "EOM(1)");
105 AP
->emitULEB128(0, "EOM(2)");
109 void DIEAbbrev::print(raw_ostream
&O
) const {
110 O
<< "Abbreviation @"
111 << format("0x%lx", (long)(intptr_t)this)
113 << dwarf::TagString(Tag
)
115 << dwarf::ChildrenString(Children
)
118 for (unsigned i
= 0, N
= Data
.size(); i
< N
; ++i
) {
120 << dwarf::AttributeString(Data
[i
].getAttribute())
122 << dwarf::FormEncodingString(Data
[i
].getForm());
124 if (Data
[i
].getForm() == dwarf::DW_FORM_implicit_const
)
125 O
<< " " << Data
[i
].getValue();
131 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
132 LLVM_DUMP_METHOD
void DIEAbbrev::dump() const {
137 //===----------------------------------------------------------------------===//
138 // DIEAbbrevSet Implementation
139 //===----------------------------------------------------------------------===//
141 DIEAbbrevSet::~DIEAbbrevSet() {
142 for (DIEAbbrev
*Abbrev
: Abbreviations
)
143 Abbrev
->~DIEAbbrev();
146 DIEAbbrev
&DIEAbbrevSet::uniqueAbbreviation(DIE
&Die
) {
149 DIEAbbrev Abbrev
= Die
.generateAbbrev();
153 if (DIEAbbrev
*Existing
=
154 AbbreviationsSet
.FindNodeOrInsertPos(ID
, InsertPos
)) {
155 Die
.setAbbrevNumber(Existing
->getNumber());
159 // Move the abbreviation to the heap and assign a number.
160 DIEAbbrev
*New
= new (Alloc
) DIEAbbrev(std::move(Abbrev
));
161 Abbreviations
.push_back(New
);
162 New
->setNumber(Abbreviations
.size());
163 Die
.setAbbrevNumber(Abbreviations
.size());
165 // Store it for lookup.
166 AbbreviationsSet
.InsertNode(New
, InsertPos
);
170 void DIEAbbrevSet::Emit(const AsmPrinter
*AP
, MCSection
*Section
) const {
171 if (!Abbreviations
.empty()) {
172 // Start the debug abbrev section.
173 AP
->OutStreamer
->SwitchSection(Section
);
174 AP
->emitDwarfAbbrevs(Abbreviations
);
178 //===----------------------------------------------------------------------===//
179 // DIE Implementation
180 //===----------------------------------------------------------------------===//
182 DIE
*DIE::getParent() const {
183 return Owner
.dyn_cast
<DIE
*>();
186 DIEAbbrev
DIE::generateAbbrev() const {
187 DIEAbbrev
Abbrev(Tag
, hasChildren());
188 for (const DIEValue
&V
: values())
189 if (V
.getForm() == dwarf::DW_FORM_implicit_const
)
190 Abbrev
.AddImplicitConstAttribute(V
.getAttribute(),
191 V
.getDIEInteger().getValue());
193 Abbrev
.AddAttribute(V
.getAttribute(), V
.getForm());
197 uint64_t DIE::getDebugSectionOffset() const {
198 const DIEUnit
*Unit
= getUnit();
199 assert(Unit
&& "DIE must be owned by a DIEUnit to get its absolute offset");
200 return Unit
->getDebugSectionOffset() + getOffset();
203 const DIE
*DIE::getUnitDie() const {
206 if (p
->getTag() == dwarf::DW_TAG_compile_unit
||
207 p
->getTag() == dwarf::DW_TAG_type_unit
)
214 DIEUnit
*DIE::getUnit() const {
215 const DIE
*UnitDie
= getUnitDie();
217 return UnitDie
->Owner
.dyn_cast
<DIEUnit
*>();
221 DIEValue
DIE::findAttribute(dwarf::Attribute Attribute
) const {
222 // Iterate through all the attributes until we find the one we're
223 // looking for, if we can't find it return NULL.
224 for (const auto &V
: values())
225 if (V
.getAttribute() == Attribute
)
231 static void printValues(raw_ostream
&O
, const DIEValueList
&Values
,
232 StringRef Type
, unsigned Size
, unsigned IndentCount
) {
233 O
<< Type
<< ": Size: " << Size
<< "\n";
236 const std::string
Indent(IndentCount
, ' ');
237 for (const auto &V
: Values
.values()) {
239 O
<< "Blk[" << I
++ << "]";
240 O
<< " " << dwarf::FormEncodingString(V
.getForm()) << " ";
247 void DIE::print(raw_ostream
&O
, unsigned IndentCount
) const {
248 const std::string
Indent(IndentCount
, ' ');
249 O
<< Indent
<< "Die: " << format("0x%lx", (long)(intptr_t) this)
250 << ", Offset: " << Offset
<< ", Size: " << Size
<< "\n";
252 O
<< Indent
<< dwarf::TagString(getTag()) << " "
253 << dwarf::ChildrenString(hasChildren()) << "\n";
256 for (const auto &V
: values()) {
258 O
<< dwarf::AttributeString(V
.getAttribute());
259 O
<< " " << dwarf::FormEncodingString(V
.getForm()) << " ";
265 for (const auto &Child
: children())
266 Child
.print(O
, IndentCount
+ 4);
271 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
272 LLVM_DUMP_METHOD
void DIE::dump() const {
277 unsigned DIE::computeOffsetsAndAbbrevs(const AsmPrinter
*AP
,
278 DIEAbbrevSet
&AbbrevSet
,
280 // Unique the abbreviation and fill in the abbreviation number so this DIE
282 const DIEAbbrev
&Abbrev
= AbbrevSet
.uniqueAbbreviation(*this);
284 // Set compile/type unit relative offset of this DIE.
287 // Add the byte size of the abbreviation code.
288 CUOffset
+= getULEB128Size(getAbbrevNumber());
290 // Add the byte size of all the DIE attribute values.
291 for (const auto &V
: values())
292 CUOffset
+= V
.SizeOf(AP
);
294 // Let the children compute their offsets and abbreviation numbers.
297 assert(Abbrev
.hasChildren() && "Children flag not set");
299 for (auto &Child
: children())
300 CUOffset
= Child
.computeOffsetsAndAbbrevs(AP
, AbbrevSet
, CUOffset
);
302 // Each child chain is terminated with a zero byte, adjust the offset.
303 CUOffset
+= sizeof(int8_t);
306 // Compute the byte size of this DIE and all of its children correctly. This
307 // is needed so that top level DIE can help the compile unit set its length
309 setSize(CUOffset
- getOffset());
313 //===----------------------------------------------------------------------===//
314 // DIEUnit Implementation
315 //===----------------------------------------------------------------------===//
316 DIEUnit::DIEUnit(dwarf::Tag UnitTag
)
317 : Die(UnitTag
), Section(nullptr), Offset(0) {
319 assert((UnitTag
== dwarf::DW_TAG_compile_unit
||
320 UnitTag
== dwarf::DW_TAG_skeleton_unit
||
321 UnitTag
== dwarf::DW_TAG_type_unit
||
322 UnitTag
== dwarf::DW_TAG_partial_unit
) &&
323 "expected a unit TAG");
326 void DIEValue::emitValue(const AsmPrinter
*AP
) const {
329 llvm_unreachable("Expected valid DIEValue");
330 #define HANDLE_DIEVALUE(T) \
332 getDIE##T().emitValue(AP, Form); \
334 #include "llvm/CodeGen/DIEValue.def"
338 unsigned DIEValue::SizeOf(const AsmPrinter
*AP
) const {
341 llvm_unreachable("Expected valid DIEValue");
342 #define HANDLE_DIEVALUE(T) \
344 return getDIE##T().SizeOf(AP, Form);
345 #include "llvm/CodeGen/DIEValue.def"
347 llvm_unreachable("Unknown DIE kind");
351 void DIEValue::print(raw_ostream
&O
) const {
354 llvm_unreachable("Expected valid DIEValue");
355 #define HANDLE_DIEVALUE(T) \
357 getDIE##T().print(O); \
359 #include "llvm/CodeGen/DIEValue.def"
363 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
364 LLVM_DUMP_METHOD
void DIEValue::dump() const {
369 //===----------------------------------------------------------------------===//
370 // DIEInteger Implementation
371 //===----------------------------------------------------------------------===//
373 /// EmitValue - Emit integer of appropriate size.
375 void DIEInteger::emitValue(const AsmPrinter
*Asm
, dwarf::Form Form
) const {
377 case dwarf::DW_FORM_implicit_const
:
378 case dwarf::DW_FORM_flag_present
:
379 // Emit something to keep the lines and comments in sync.
380 // FIXME: Is there a better way to do this?
381 Asm
->OutStreamer
->AddBlankLine();
383 case dwarf::DW_FORM_flag
:
384 case dwarf::DW_FORM_ref1
:
385 case dwarf::DW_FORM_data1
:
386 case dwarf::DW_FORM_strx1
:
387 case dwarf::DW_FORM_addrx1
:
388 case dwarf::DW_FORM_ref2
:
389 case dwarf::DW_FORM_data2
:
390 case dwarf::DW_FORM_strx2
:
391 case dwarf::DW_FORM_addrx2
:
392 case dwarf::DW_FORM_strx3
:
393 case dwarf::DW_FORM_strp
:
394 case dwarf::DW_FORM_ref4
:
395 case dwarf::DW_FORM_data4
:
396 case dwarf::DW_FORM_ref_sup4
:
397 case dwarf::DW_FORM_strx4
:
398 case dwarf::DW_FORM_addrx4
:
399 case dwarf::DW_FORM_ref8
:
400 case dwarf::DW_FORM_ref_sig8
:
401 case dwarf::DW_FORM_data8
:
402 case dwarf::DW_FORM_ref_sup8
:
403 case dwarf::DW_FORM_GNU_ref_alt
:
404 case dwarf::DW_FORM_GNU_strp_alt
:
405 case dwarf::DW_FORM_line_strp
:
406 case dwarf::DW_FORM_sec_offset
:
407 case dwarf::DW_FORM_strp_sup
:
408 case dwarf::DW_FORM_addr
:
409 case dwarf::DW_FORM_ref_addr
:
410 Asm
->OutStreamer
->emitIntValue(Integer
, SizeOf(Asm
, Form
));
412 case dwarf::DW_FORM_GNU_str_index
:
413 case dwarf::DW_FORM_GNU_addr_index
:
414 case dwarf::DW_FORM_ref_udata
:
415 case dwarf::DW_FORM_strx
:
416 case dwarf::DW_FORM_addrx
:
417 case dwarf::DW_FORM_rnglistx
:
418 case dwarf::DW_FORM_udata
:
419 Asm
->emitULEB128(Integer
);
421 case dwarf::DW_FORM_sdata
:
422 Asm
->emitSLEB128(Integer
);
424 default: llvm_unreachable("DIE Value form not supported yet");
428 /// SizeOf - Determine size of integer value in bytes.
430 unsigned DIEInteger::SizeOf(const AsmPrinter
*AP
, dwarf::Form Form
) const {
431 assert(AP
&& "AsmPrinter is required to set FormParams");
432 dwarf::FormParams Params
= {AP
->getDwarfVersion(),
433 uint8_t(AP
->getPointerSize()),
434 AP
->OutStreamer
->getContext().getDwarfFormat()};
436 if (Optional
<uint8_t> FixedSize
= dwarf::getFixedFormByteSize(Form
, Params
))
440 case dwarf::DW_FORM_GNU_str_index
:
441 case dwarf::DW_FORM_GNU_addr_index
:
442 case dwarf::DW_FORM_ref_udata
:
443 case dwarf::DW_FORM_strx
:
444 case dwarf::DW_FORM_addrx
:
445 case dwarf::DW_FORM_rnglistx
:
446 case dwarf::DW_FORM_udata
:
447 return getULEB128Size(Integer
);
448 case dwarf::DW_FORM_sdata
:
449 return getSLEB128Size(Integer
);
450 default: llvm_unreachable("DIE Value form not supported yet");
455 void DIEInteger::print(raw_ostream
&O
) const {
456 O
<< "Int: " << (int64_t)Integer
<< " 0x";
457 O
.write_hex(Integer
);
460 //===----------------------------------------------------------------------===//
461 // DIEExpr Implementation
462 //===----------------------------------------------------------------------===//
464 /// EmitValue - Emit expression value.
466 void DIEExpr::emitValue(const AsmPrinter
*AP
, dwarf::Form Form
) const {
467 AP
->emitDebugValue(Expr
, SizeOf(AP
, Form
));
470 /// SizeOf - Determine size of expression value in bytes.
472 unsigned DIEExpr::SizeOf(const AsmPrinter
*AP
, dwarf::Form Form
) const {
474 case dwarf::DW_FORM_data4
:
476 case dwarf::DW_FORM_data8
:
478 case dwarf::DW_FORM_sec_offset
:
479 return AP
->getDwarfOffsetByteSize();
481 llvm_unreachable("DIE Value form not supported yet");
486 void DIEExpr::print(raw_ostream
&O
) const { O
<< "Expr: " << *Expr
; }
488 //===----------------------------------------------------------------------===//
489 // DIELabel Implementation
490 //===----------------------------------------------------------------------===//
492 /// EmitValue - Emit label value.
494 void DIELabel::emitValue(const AsmPrinter
*AP
, dwarf::Form Form
) const {
495 bool IsSectionRelative
= Form
!= dwarf::DW_FORM_addr
;
496 AP
->emitLabelReference(Label
, SizeOf(AP
, Form
), IsSectionRelative
);
499 /// SizeOf - Determine size of label value in bytes.
501 unsigned DIELabel::SizeOf(const AsmPrinter
*AP
, dwarf::Form Form
) const {
503 case dwarf::DW_FORM_data4
:
505 case dwarf::DW_FORM_data8
:
507 case dwarf::DW_FORM_sec_offset
:
508 case dwarf::DW_FORM_strp
:
509 return AP
->getDwarfOffsetByteSize();
510 case dwarf::DW_FORM_addr
:
511 return AP
->MAI
->getCodePointerSize();
513 llvm_unreachable("DIE Value form not supported yet");
518 void DIELabel::print(raw_ostream
&O
) const { O
<< "Lbl: " << Label
->getName(); }
520 //===----------------------------------------------------------------------===//
521 // DIEBaseTypeRef Implementation
522 //===----------------------------------------------------------------------===//
524 void DIEBaseTypeRef::emitValue(const AsmPrinter
*AP
, dwarf::Form Form
) const {
525 uint64_t Offset
= CU
->ExprRefedBaseTypes
[Index
].Die
->getOffset();
526 assert(Offset
< (1ULL << (ULEB128PadSize
* 7)) && "Offset wont fit");
527 AP
->emitULEB128(Offset
, nullptr, ULEB128PadSize
);
530 unsigned DIEBaseTypeRef::SizeOf(const AsmPrinter
*AP
, dwarf::Form Form
) const {
531 return ULEB128PadSize
;
535 void DIEBaseTypeRef::print(raw_ostream
&O
) const { O
<< "BaseTypeRef: " << Index
; }
537 //===----------------------------------------------------------------------===//
538 // DIEDelta Implementation
539 //===----------------------------------------------------------------------===//
541 /// EmitValue - Emit delta value.
543 void DIEDelta::emitValue(const AsmPrinter
*AP
, dwarf::Form Form
) const {
544 AP
->emitLabelDifference(LabelHi
, LabelLo
, SizeOf(AP
, Form
));
547 /// SizeOf - Determine size of delta value in bytes.
549 unsigned DIEDelta::SizeOf(const AsmPrinter
*AP
, dwarf::Form Form
) const {
551 case dwarf::DW_FORM_data4
:
553 case dwarf::DW_FORM_data8
:
555 case dwarf::DW_FORM_sec_offset
:
556 return AP
->getDwarfOffsetByteSize();
558 llvm_unreachable("DIE Value form not supported yet");
563 void DIEDelta::print(raw_ostream
&O
) const {
564 O
<< "Del: " << LabelHi
->getName() << "-" << LabelLo
->getName();
567 //===----------------------------------------------------------------------===//
568 // DIEString Implementation
569 //===----------------------------------------------------------------------===//
571 /// EmitValue - Emit string value.
573 void DIEString::emitValue(const AsmPrinter
*AP
, dwarf::Form Form
) const {
574 // Index of string in symbol table.
576 case dwarf::DW_FORM_GNU_str_index
:
577 case dwarf::DW_FORM_strx
:
578 case dwarf::DW_FORM_strx1
:
579 case dwarf::DW_FORM_strx2
:
580 case dwarf::DW_FORM_strx3
:
581 case dwarf::DW_FORM_strx4
:
582 DIEInteger(S
.getIndex()).emitValue(AP
, Form
);
584 case dwarf::DW_FORM_strp
:
585 if (AP
->MAI
->doesDwarfUseRelocationsAcrossSections())
586 DIELabel(S
.getSymbol()).emitValue(AP
, Form
);
588 DIEInteger(S
.getOffset()).emitValue(AP
, Form
);
591 llvm_unreachable("Expected valid string form");
595 /// SizeOf - Determine size of delta value in bytes.
597 unsigned DIEString::SizeOf(const AsmPrinter
*AP
, dwarf::Form Form
) const {
598 // Index of string in symbol table.
600 case dwarf::DW_FORM_GNU_str_index
:
601 case dwarf::DW_FORM_strx
:
602 case dwarf::DW_FORM_strx1
:
603 case dwarf::DW_FORM_strx2
:
604 case dwarf::DW_FORM_strx3
:
605 case dwarf::DW_FORM_strx4
:
606 return DIEInteger(S
.getIndex()).SizeOf(AP
, Form
);
607 case dwarf::DW_FORM_strp
:
608 if (AP
->MAI
->doesDwarfUseRelocationsAcrossSections())
609 return DIELabel(S
.getSymbol()).SizeOf(AP
, Form
);
610 return DIEInteger(S
.getOffset()).SizeOf(AP
, Form
);
612 llvm_unreachable("Expected valid string form");
617 void DIEString::print(raw_ostream
&O
) const {
618 O
<< "String: " << S
.getString();
621 //===----------------------------------------------------------------------===//
622 // DIEInlineString Implementation
623 //===----------------------------------------------------------------------===//
624 void DIEInlineString::emitValue(const AsmPrinter
*AP
, dwarf::Form Form
) const {
625 if (Form
== dwarf::DW_FORM_string
) {
626 AP
->OutStreamer
->emitBytes(S
);
630 llvm_unreachable("Expected valid string form");
633 unsigned DIEInlineString::SizeOf(const AsmPrinter
*AP
, dwarf::Form Form
) const {
634 // Emit string bytes + NULL byte.
639 void DIEInlineString::print(raw_ostream
&O
) const {
640 O
<< "InlineString: " << S
;
643 //===----------------------------------------------------------------------===//
644 // DIEEntry Implementation
645 //===----------------------------------------------------------------------===//
647 /// EmitValue - Emit debug information entry offset.
649 void DIEEntry::emitValue(const AsmPrinter
*AP
, dwarf::Form Form
) const {
652 case dwarf::DW_FORM_ref1
:
653 case dwarf::DW_FORM_ref2
:
654 case dwarf::DW_FORM_ref4
:
655 case dwarf::DW_FORM_ref8
:
656 AP
->OutStreamer
->emitIntValue(Entry
->getOffset(), SizeOf(AP
, Form
));
659 case dwarf::DW_FORM_ref_udata
:
660 AP
->emitULEB128(Entry
->getOffset());
663 case dwarf::DW_FORM_ref_addr
: {
664 // Get the absolute offset for this DIE within the debug info/types section.
665 uint64_t Addr
= Entry
->getDebugSectionOffset();
666 if (const MCSymbol
*SectionSym
=
667 Entry
->getUnit()->getCrossSectionRelativeBaseAddress()) {
668 AP
->emitLabelPlusOffset(SectionSym
, Addr
, SizeOf(AP
, Form
), true);
672 AP
->OutStreamer
->emitIntValue(Addr
, SizeOf(AP
, Form
));
676 llvm_unreachable("Improper form for DIE reference");
680 unsigned DIEEntry::SizeOf(const AsmPrinter
*AP
, dwarf::Form Form
) const {
682 case dwarf::DW_FORM_ref1
:
684 case dwarf::DW_FORM_ref2
:
686 case dwarf::DW_FORM_ref4
:
688 case dwarf::DW_FORM_ref8
:
690 case dwarf::DW_FORM_ref_udata
:
691 return getULEB128Size(Entry
->getOffset());
692 case dwarf::DW_FORM_ref_addr
:
693 if (AP
->getDwarfVersion() == 2)
694 return AP
->MAI
->getCodePointerSize();
695 switch (AP
->OutStreamer
->getContext().getDwarfFormat()) {
701 llvm_unreachable("Invalid DWARF format");
704 llvm_unreachable("Improper form for DIE reference");
709 void DIEEntry::print(raw_ostream
&O
) const {
710 O
<< format("Die: 0x%lx", (long)(intptr_t)&Entry
);
713 //===----------------------------------------------------------------------===//
714 // DIELoc Implementation
715 //===----------------------------------------------------------------------===//
717 /// ComputeSize - calculate the size of the location expression.
719 unsigned DIELoc::ComputeSize(const AsmPrinter
*AP
) const {
721 for (const auto &V
: values())
722 Size
+= V
.SizeOf(AP
);
728 /// EmitValue - Emit location data.
730 void DIELoc::emitValue(const AsmPrinter
*Asm
, dwarf::Form Form
) const {
732 default: llvm_unreachable("Improper form for block");
733 case dwarf::DW_FORM_block1
: Asm
->emitInt8(Size
); break;
734 case dwarf::DW_FORM_block2
: Asm
->emitInt16(Size
); break;
735 case dwarf::DW_FORM_block4
: Asm
->emitInt32(Size
); break;
736 case dwarf::DW_FORM_block
:
737 case dwarf::DW_FORM_exprloc
:
738 Asm
->emitULEB128(Size
);
742 for (const auto &V
: values())
746 /// SizeOf - Determine size of location data in bytes.
748 unsigned DIELoc::SizeOf(const AsmPrinter
*AP
, dwarf::Form Form
) const {
750 case dwarf::DW_FORM_block1
: return Size
+ sizeof(int8_t);
751 case dwarf::DW_FORM_block2
: return Size
+ sizeof(int16_t);
752 case dwarf::DW_FORM_block4
: return Size
+ sizeof(int32_t);
753 case dwarf::DW_FORM_block
:
754 case dwarf::DW_FORM_exprloc
:
755 return Size
+ getULEB128Size(Size
);
756 default: llvm_unreachable("Improper form for block");
761 void DIELoc::print(raw_ostream
&O
) const {
762 printValues(O
, *this, "ExprLoc", Size
, 5);
765 //===----------------------------------------------------------------------===//
766 // DIEBlock Implementation
767 //===----------------------------------------------------------------------===//
769 /// ComputeSize - calculate the size of the block.
771 unsigned DIEBlock::ComputeSize(const AsmPrinter
*AP
) const {
773 for (const auto &V
: values())
774 Size
+= V
.SizeOf(AP
);
780 /// EmitValue - Emit block data.
782 void DIEBlock::emitValue(const AsmPrinter
*Asm
, dwarf::Form Form
) const {
784 default: llvm_unreachable("Improper form for block");
785 case dwarf::DW_FORM_block1
: Asm
->emitInt8(Size
); break;
786 case dwarf::DW_FORM_block2
: Asm
->emitInt16(Size
); break;
787 case dwarf::DW_FORM_block4
: Asm
->emitInt32(Size
); break;
788 case dwarf::DW_FORM_exprloc
:
789 case dwarf::DW_FORM_block
:
790 Asm
->emitULEB128(Size
);
792 case dwarf::DW_FORM_string
: break;
793 case dwarf::DW_FORM_data16
: break;
796 for (const auto &V
: values())
800 /// SizeOf - Determine size of block data in bytes.
802 unsigned DIEBlock::SizeOf(const AsmPrinter
*AP
, dwarf::Form Form
) const {
804 case dwarf::DW_FORM_block1
: return Size
+ sizeof(int8_t);
805 case dwarf::DW_FORM_block2
: return Size
+ sizeof(int16_t);
806 case dwarf::DW_FORM_block4
: return Size
+ sizeof(int32_t);
807 case dwarf::DW_FORM_exprloc
:
808 case dwarf::DW_FORM_block
: return Size
+ getULEB128Size(Size
);
809 case dwarf::DW_FORM_data16
: return 16;
810 default: llvm_unreachable("Improper form for block");
815 void DIEBlock::print(raw_ostream
&O
) const {
816 printValues(O
, *this, "Blk", Size
, 5);
819 //===----------------------------------------------------------------------===//
820 // DIELocList Implementation
821 //===----------------------------------------------------------------------===//
823 unsigned DIELocList::SizeOf(const AsmPrinter
*AP
, dwarf::Form Form
) const {
825 case dwarf::DW_FORM_loclistx
:
826 return getULEB128Size(Index
);
827 case dwarf::DW_FORM_data4
:
828 assert(!AP
->isDwarf64() &&
829 "DW_FORM_data4 is not suitable to emit a pointer to a location list "
830 "in the 64-bit DWARF format");
832 case dwarf::DW_FORM_data8
:
833 assert(AP
->isDwarf64() &&
834 "DW_FORM_data8 is not suitable to emit a pointer to a location list "
835 "in the 32-bit DWARF format");
837 case dwarf::DW_FORM_sec_offset
:
838 return AP
->getDwarfOffsetByteSize();
840 llvm_unreachable("DIE Value form not supported yet");
844 /// EmitValue - Emit label value.
846 void DIELocList::emitValue(const AsmPrinter
*AP
, dwarf::Form Form
) const {
847 if (Form
== dwarf::DW_FORM_loclistx
) {
848 AP
->emitULEB128(Index
);
851 DwarfDebug
*DD
= AP
->getDwarfDebug();
852 MCSymbol
*Label
= DD
->getDebugLocs().getList(Index
).Label
;
853 AP
->emitDwarfSymbolReference(Label
, /*ForceOffset*/ DD
->useSplitDwarf());
857 void DIELocList::print(raw_ostream
&O
) const { O
<< "LocList: " << Index
; }
859 //===----------------------------------------------------------------------===//
860 // DIEAddrOffset Implementation
861 //===----------------------------------------------------------------------===//
863 unsigned DIEAddrOffset::SizeOf(const AsmPrinter
*AP
, dwarf::Form Form
) const {
864 return Addr
.SizeOf(AP
, dwarf::DW_FORM_addrx
) +
865 Offset
.SizeOf(AP
, dwarf::DW_FORM_data4
);
868 /// EmitValue - Emit label value.
870 void DIEAddrOffset::emitValue(const AsmPrinter
*AP
, dwarf::Form Form
) const {
871 Addr
.emitValue(AP
, dwarf::DW_FORM_addrx
);
872 Offset
.emitValue(AP
, dwarf::DW_FORM_data4
);
876 void DIEAddrOffset::print(raw_ostream
&O
) const {