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"
16#include "llvm/CodeGen/AsmPrinter.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"
22#include "llvm/Support/ErrorHandling.h"
23#include "llvm/Support/Format.h"
24#include "llvm/Support/LEB128.h"
25#include "llvm/Support/raw_ostream.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///
36void DIEAbbrevData::Profile(FoldingSetNodeID &ID) const {
37 // Explicitly cast to an integer type for which FoldingSetNodeID has
38 // overloads. Otherwise MSVC 2010 thinks this call is ambiguous.
39 ID.AddInteger(I: unsigned(Attribute));
40 ID.AddInteger(I: unsigned(Form));
41 if (Form == dwarf::DW_FORM_implicit_const)
42 ID.AddInteger(I: Value);
43}
44
45//===----------------------------------------------------------------------===//
46// DIEAbbrev Implementation
47//===----------------------------------------------------------------------===//
48
49/// Profile - Used to gather unique data for the abbreviation folding set.
50///
51void DIEAbbrev::Profile(FoldingSetNodeID &ID) const {
52 ID.AddInteger(I: unsigned(Tag));
53 ID.AddInteger(I: 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 assert((Tag != DIE::PlaceholderTag) &&
64 "Abbreviation with placeholder tag should not be emitted");
65
66 // Emit its Dwarf tag type.
67 AP->emitULEB128(Value: Tag, Desc: dwarf::TagString(Tag).data());
68
69 // Emit whether it has children DIEs.
70 AP->emitULEB128(Value: (unsigned)Children, Desc: dwarf::ChildrenString(Children).data());
71
72 // For each attribute description.
73 for (const DIEAbbrevData &AttrData : Data) {
74 // Emit attribute type.
75 AP->emitULEB128(Value: AttrData.getAttribute(),
76 Desc: dwarf::AttributeString(Attribute: AttrData.getAttribute()).data());
77
78 // Emit form type.
79#ifndef NDEBUG
80 // Could be an assertion, but this way we can see the failing form code
81 // easily, which helps track down where it came from.
82 if (!dwarf::isValidFormForVersion(AttrData.getForm(),
83 AP->getDwarfVersion())) {
84 LLVM_DEBUG(dbgs() << "Invalid form " << format("0x%x", AttrData.getForm())
85 << " for DWARF version " << AP->getDwarfVersion()
86 << "\n");
87 llvm_unreachable("Invalid form for specified DWARF version");
88 }
89#endif
90 AP->emitULEB128(Value: AttrData.getForm(),
91 Desc: dwarf::FormEncodingString(Encoding: AttrData.getForm()).data());
92
93 // Emit value for DW_FORM_implicit_const.
94 if (AttrData.getForm() == dwarf::DW_FORM_implicit_const)
95 AP->emitSLEB128(Value: AttrData.getValue());
96 }
97
98 // Mark end of abbreviation.
99 AP->emitULEB128(Value: 0, Desc: "EOM(1)");
100 AP->emitULEB128(Value: 0, Desc: "EOM(2)");
101}
102
103LLVM_DUMP_METHOD
104void DIEAbbrev::print(raw_ostream &O) const {
105 O << "Abbreviation @"
106 << format(Fmt: "0x%lx", Vals: (long)(intptr_t)this)
107 << " "
108 << dwarf::TagString(Tag)
109 << " "
110 << dwarf::ChildrenString(Children)
111 << '\n';
112
113 for (const DIEAbbrevData &D : Data) {
114 O << " " << dwarf::AttributeString(Attribute: D.getAttribute()) << " "
115 << dwarf::FormEncodingString(Encoding: D.getForm());
116
117 if (D.getForm() == dwarf::DW_FORM_implicit_const)
118 O << " " << D.getValue();
119
120 O << '\n';
121 }
122}
123
124#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
125LLVM_DUMP_METHOD void DIEAbbrev::dump() const {
126 print(dbgs());
127}
128#endif
129
130//===----------------------------------------------------------------------===//
131// DIEAbbrevSet Implementation
132//===----------------------------------------------------------------------===//
133
134DIEAbbrevSet::~DIEAbbrevSet() {
135 for (DIEAbbrev *Abbrev : Abbreviations)
136 Abbrev->~DIEAbbrev();
137}
138
139DIEAbbrev &DIEAbbrevSet::uniqueAbbreviation(DIE &Die) {
140
141 FoldingSetNodeID ID;
142 DIEAbbrev Abbrev = Die.generateAbbrev();
143 Abbrev.Profile(ID);
144
145 FoldingSetInsertToken Token;
146 if (DIEAbbrev *Existing = AbbreviationsSet.lookup(ID, Token)) {
147 Die.setAbbrevNumber(Existing->getNumber());
148 return *Existing;
149 }
150
151 // Move the abbreviation to the heap and assign a number.
152 DIEAbbrev *New = new (Alloc) DIEAbbrev(std::move(Abbrev));
153 Abbreviations.push_back(x: New);
154 New->setNumber(Abbreviations.size());
155 Die.setAbbrevNumber(Abbreviations.size());
156
157 // Store it for lookup.
158 AbbreviationsSet.insert(N: New, Token);
159 return *New;
160}
161
162void DIEAbbrevSet::Emit(const AsmPrinter *AP, MCSection *Section) const {
163 if (!Abbreviations.empty()) {
164 // Start the debug abbrev section.
165 AP->OutStreamer->switchSection(Section);
166 AP->emitDwarfAbbrevs(Abbrevs: Abbreviations);
167 }
168}
169
170//===----------------------------------------------------------------------===//
171// DIE Implementation
172//===----------------------------------------------------------------------===//
173
174DIE *DIE::getRawParent() const { return dyn_cast_if_present<DIE *>(Val: Owner); }
175
176DIE *DIE::getParent() const {
177 DIE *Parent = getRawParent();
178 assert((!Parent || !Parent->isPlaceholder()) &&
179 "Parent must not be requested when DIE is in a temporary placeholder");
180 return Parent;
181}
182
183void DIE::removeChild(DIE &Child) {
184 assert(Child.getRawParent() == this && "Not a child of this DIE");
185 [[maybe_unused]] bool Removed = Children.deleteNode(N&: Child);
186 assert(Removed && "Child is missing from the list of children");
187 Child.Owner = nullptr;
188}
189
190void DIE::takeChildren(DIE &Other, bool AtFront) {
191 for (DIE &Child : Other.children())
192 Child.Owner = this;
193 if (!AtFront) {
194 Children.takeNodes(Other&: Other.Children);
195 return;
196 }
197 IntrusiveBackList<DIE> Moved;
198 Moved.takeNodes(Other&: Other.Children);
199 Moved.takeNodes(Other&: Children);
200 Children.takeNodes(Other&: Moved);
201}
202
203DIEAbbrev DIE::generateAbbrev() const {
204 assert(!isPlaceholder() &&
205 "Abbrevs should not be generated for placeholder tags");
206
207 DIEAbbrev Abbrev(Tag, hasChildren());
208 for (const DIEValue &V : values())
209 if (V.getForm() == dwarf::DW_FORM_implicit_const)
210 Abbrev.AddImplicitConstAttribute(Attribute: V.getAttribute(),
211 Value: V.getDIEInteger().getValue());
212 else
213 Abbrev.AddAttribute(Attribute: V.getAttribute(), Form: V.getForm());
214 return Abbrev;
215}
216
217uint64_t DIE::getDebugSectionOffset() const {
218 const DIEUnit *Unit = getUnit();
219 assert(Unit && "DIE must be owned by a DIEUnit to get its absolute offset");
220 return Unit->getDebugSectionOffset() + getOffset();
221}
222
223DIEUnit *DIE::getUnit() const {
224 for (const DIE *P = this; P; P = P->getRawParent())
225 if (auto *Unit = dyn_cast_if_present<DIEUnit *>(Val: P->Owner))
226 return Unit;
227 return nullptr;
228}
229
230const DIE *DIE::getUnitDie() const {
231 const DIEUnit *Unit = getUnit();
232 return Unit ? &Unit->getUnitDie() : nullptr;
233}
234
235DIEValue DIE::findAttribute(dwarf::Attribute Attribute) const {
236 // Iterate through all the attributes until we find the one we're
237 // looking for, if we can't find it return NULL.
238 for (const auto &V : values())
239 if (V.getAttribute() == Attribute)
240 return V;
241 return DIEValue();
242}
243
244LLVM_DUMP_METHOD
245static void printValues(raw_ostream &O, const DIEValueList &Values,
246 StringRef Type, unsigned Size, unsigned IndentCount) {
247 O << Type << ": Size: " << Size << "\n";
248
249 unsigned I = 0;
250 const std::string Indent(IndentCount, ' ');
251 for (const auto &V : Values.values()) {
252 O << Indent;
253 O << "Blk[" << I++ << "]";
254 O << " " << dwarf::FormEncodingString(Encoding: V.getForm()) << " ";
255 V.print(O);
256 O << "\n";
257 }
258}
259
260LLVM_DUMP_METHOD
261void DIE::print(raw_ostream &O, unsigned IndentCount) const {
262 const std::string Indent(IndentCount, ' ');
263 O << Indent << "Die: " << format(Fmt: "0x%lx", Vals: (long)(intptr_t) this)
264 << ", Offset: " << Offset << ", Size: " << Size << "\n";
265
266 O << Indent << dwarf::TagString(Tag: getTag()) << " "
267 << dwarf::ChildrenString(Children: hasChildren()) << "\n";
268
269 IndentCount += 2;
270 for (const auto &V : values()) {
271 O << Indent;
272 O << dwarf::AttributeString(Attribute: V.getAttribute());
273 O << " " << dwarf::FormEncodingString(Encoding: V.getForm()) << " ";
274 V.print(O);
275 O << "\n";
276 }
277 IndentCount -= 2;
278
279 for (const auto &Child : children())
280 Child.print(O, IndentCount: IndentCount + 4);
281
282 O << "\n";
283}
284
285#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
286LLVM_DUMP_METHOD void DIE::dump() const {
287 print(dbgs());
288}
289#endif
290
291unsigned DIE::computeOffsetsAndAbbrevs(const dwarf::FormParams &FormParams,
292 DIEAbbrevSet &AbbrevSet,
293 unsigned CUOffset) {
294 assert(!isPlaceholder() && "Cannot compute offsets for a placeholder DIE");
295
296 // Unique the abbreviation and fill in the abbreviation number so this DIE
297 // can be emitted.
298 const DIEAbbrev &Abbrev = AbbrevSet.uniqueAbbreviation(Die&: *this);
299
300 // Set compile/type unit relative offset of this DIE.
301 setOffset(CUOffset);
302
303 // Add the byte size of the abbreviation code.
304 CUOffset += getULEB128Size(Value: getAbbrevNumber());
305
306 // Add the byte size of all the DIE attribute values.
307 for (const auto &V : values())
308 CUOffset += V.sizeOf(FormParams);
309
310 // Let the children compute their offsets and abbreviation numbers.
311 if (hasChildren()) {
312 (void)Abbrev;
313 assert(Abbrev.hasChildren() && "Children flag not set");
314
315 for (auto &Child : children())
316 CUOffset =
317 Child.computeOffsetsAndAbbrevs(FormParams, AbbrevSet, CUOffset);
318
319 // Each child chain is terminated with a zero byte, adjust the offset.
320 CUOffset += sizeof(int8_t);
321 }
322
323 // Compute the byte size of this DIE and all of its children correctly. This
324 // is needed so that top level DIE can help the compile unit set its length
325 // correctly.
326 setSize(CUOffset - getOffset());
327 return CUOffset;
328}
329
330//===----------------------------------------------------------------------===//
331// DIEUnit Implementation
332//===----------------------------------------------------------------------===//
333DIEUnit::DIEUnit(dwarf::Tag UnitTag) : Die(UnitTag) {
334 Die.Owner = this;
335 assert(dwarf::isUnitType(UnitTag) && "expected a unit TAG");
336}
337
338void DIEValue::emitValue(const AsmPrinter *AP) const {
339 switch (Ty) {
340 case isNone:
341 llvm_unreachable("Expected valid DIEValue");
342#define HANDLE_DIEVALUE(T) \
343 case is##T: \
344 getDIE##T().emitValue(AP, Form); \
345 break;
346#include "llvm/CodeGen/DIEValue.def"
347 }
348}
349
350unsigned DIEValue::sizeOf(const dwarf::FormParams &FormParams) const {
351 switch (Ty) {
352 case isNone:
353 llvm_unreachable("Expected valid DIEValue");
354#define HANDLE_DIEVALUE(T) \
355 case is##T: \
356 return getDIE##T().sizeOf(FormParams, Form);
357#include "llvm/CodeGen/DIEValue.def"
358 }
359 llvm_unreachable("Unknown DIE kind");
360}
361
362LLVM_DUMP_METHOD
363void DIEValue::print(raw_ostream &O) const {
364 switch (Ty) {
365 case isNone:
366 llvm_unreachable("Expected valid DIEValue");
367#define HANDLE_DIEVALUE(T) \
368 case is##T: \
369 getDIE##T().print(O); \
370 break;
371#include "llvm/CodeGen/DIEValue.def"
372 }
373}
374
375#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
376LLVM_DUMP_METHOD void DIEValue::dump() const {
377 print(dbgs());
378}
379#endif
380
381//===----------------------------------------------------------------------===//
382// DIEInteger Implementation
383//===----------------------------------------------------------------------===//
384
385/// EmitValue - Emit integer of appropriate size.
386///
387void DIEInteger::emitValue(const AsmPrinter *Asm, dwarf::Form Form) const {
388 switch (Form) {
389 case dwarf::DW_FORM_implicit_const:
390 case dwarf::DW_FORM_flag_present:
391 // Emit something to keep the lines and comments in sync.
392 // FIXME: Is there a better way to do this?
393 Asm->OutStreamer->addBlankLine();
394 return;
395 case dwarf::DW_FORM_flag:
396 case dwarf::DW_FORM_ref1:
397 case dwarf::DW_FORM_data1:
398 case dwarf::DW_FORM_strx1:
399 case dwarf::DW_FORM_addrx1:
400 case dwarf::DW_FORM_ref2:
401 case dwarf::DW_FORM_data2:
402 case dwarf::DW_FORM_strx2:
403 case dwarf::DW_FORM_addrx2:
404 case dwarf::DW_FORM_strx3:
405 case dwarf::DW_FORM_addrx3:
406 case dwarf::DW_FORM_strp:
407 case dwarf::DW_FORM_ref4:
408 case dwarf::DW_FORM_data4:
409 case dwarf::DW_FORM_ref_sup4:
410 case dwarf::DW_FORM_strx4:
411 case dwarf::DW_FORM_addrx4:
412 case dwarf::DW_FORM_ref8:
413 case dwarf::DW_FORM_ref_sig8:
414 case dwarf::DW_FORM_data8:
415 case dwarf::DW_FORM_ref_sup8:
416 case dwarf::DW_FORM_GNU_ref_alt:
417 case dwarf::DW_FORM_GNU_strp_alt:
418 case dwarf::DW_FORM_line_strp:
419 case dwarf::DW_FORM_sec_offset:
420 case dwarf::DW_FORM_strp_sup:
421 case dwarf::DW_FORM_addr:
422 case dwarf::DW_FORM_ref_addr:
423 Asm->OutStreamer->emitIntValue(Value: Integer,
424 Size: sizeOf(FormParams: Asm->getDwarfFormParams(), Form));
425 return;
426 case dwarf::DW_FORM_GNU_str_index:
427 case dwarf::DW_FORM_GNU_addr_index:
428 case dwarf::DW_FORM_ref_udata:
429 case dwarf::DW_FORM_strx:
430 case dwarf::DW_FORM_addrx:
431 case dwarf::DW_FORM_rnglistx:
432 case dwarf::DW_FORM_udata:
433 Asm->emitULEB128(Value: Integer);
434 return;
435 case dwarf::DW_FORM_sdata:
436 Asm->emitSLEB128(Value: Integer);
437 return;
438 default: llvm_unreachable("DIE Value form not supported yet");
439 }
440}
441
442/// sizeOf - Determine size of integer value in bytes.
443///
444unsigned DIEInteger::sizeOf(const dwarf::FormParams &FormParams,
445 dwarf::Form Form) const {
446 if (std::optional<uint8_t> FixedSize =
447 dwarf::getFixedFormByteSize(Form, Params: FormParams))
448 return *FixedSize;
449
450 switch (Form) {
451 case dwarf::DW_FORM_GNU_str_index:
452 case dwarf::DW_FORM_GNU_addr_index:
453 case dwarf::DW_FORM_ref_udata:
454 case dwarf::DW_FORM_strx:
455 case dwarf::DW_FORM_addrx:
456 case dwarf::DW_FORM_rnglistx:
457 case dwarf::DW_FORM_udata:
458 return getULEB128Size(Value: Integer);
459 case dwarf::DW_FORM_sdata:
460 return getSLEB128Size(Value: Integer);
461 default: llvm_unreachable("DIE Value form not supported yet");
462 }
463}
464
465LLVM_DUMP_METHOD
466void DIEInteger::print(raw_ostream &O) const {
467 O << "Int: " << (int64_t)Integer << " 0x";
468 O.write_hex(N: Integer);
469}
470
471//===----------------------------------------------------------------------===//
472// DIEExpr Implementation
473//===----------------------------------------------------------------------===//
474
475/// EmitValue - Emit expression value.
476///
477void DIEExpr::emitValue(const AsmPrinter *AP, dwarf::Form Form) const {
478 AP->emitDebugValue(Value: Expr, Size: sizeOf(FormParams: AP->getDwarfFormParams(), Form));
479}
480
481/// SizeOf - Determine size of expression value in bytes.
482///
483unsigned DIEExpr::sizeOf(const dwarf::FormParams &FormParams,
484 dwarf::Form Form) const {
485 switch (Form) {
486 case dwarf::DW_FORM_data4:
487 return 4;
488 case dwarf::DW_FORM_data8:
489 return 8;
490 case dwarf::DW_FORM_sec_offset:
491 return FormParams.getDwarfOffsetByteSize();
492 default:
493 llvm_unreachable("DIE Value form not supported yet");
494 }
495}
496
497LLVM_DUMP_METHOD
498void DIEExpr::print(raw_ostream &O) const {
499 MCTargetOptions Opts;
500 O << "Expr: ";
501 MCAsmInfo(Opts).printExpr(O, *Expr);
502}
503
504//===----------------------------------------------------------------------===//
505// DIELabel Implementation
506//===----------------------------------------------------------------------===//
507
508/// EmitValue - Emit label value.
509///
510void DIELabel::emitValue(const AsmPrinter *AP, dwarf::Form Form) const {
511 bool IsSectionRelative = Form != dwarf::DW_FORM_addr;
512 AP->emitLabelReference(Label, Size: sizeOf(FormParams: AP->getDwarfFormParams(), Form),
513 IsSectionRelative);
514}
515
516/// sizeOf - Determine size of label value in bytes.
517///
518unsigned DIELabel::sizeOf(const dwarf::FormParams &FormParams,
519 dwarf::Form Form) const {
520 switch (Form) {
521 case dwarf::DW_FORM_data4:
522 return 4;
523 case dwarf::DW_FORM_data8:
524 return 8;
525 case dwarf::DW_FORM_sec_offset:
526 case dwarf::DW_FORM_strp:
527 return FormParams.getDwarfOffsetByteSize();
528 case dwarf::DW_FORM_addr:
529 return FormParams.AddrSize;
530 default:
531 llvm_unreachable("DIE Value form not supported yet");
532 }
533}
534
535LLVM_DUMP_METHOD
536void DIELabel::print(raw_ostream &O) const { O << "Lbl: " << Label->getName(); }
537
538//===----------------------------------------------------------------------===//
539// DIEBaseTypeRef Implementation
540//===----------------------------------------------------------------------===//
541
542void DIEBaseTypeRef::emitValue(const AsmPrinter *AP, dwarf::Form Form) const {
543 uint64_t Offset = CU->ExprRefedBaseTypes[Index].Die->getOffset();
544 assert(Offset < (1ULL << (ULEB128PadSize * 7)) && "Offset wont fit");
545 AP->emitULEB128(Value: Offset, Desc: nullptr, PadTo: ULEB128PadSize);
546}
547
548unsigned DIEBaseTypeRef::sizeOf(const dwarf::FormParams &, dwarf::Form) const {
549 return ULEB128PadSize;
550}
551
552LLVM_DUMP_METHOD
553void DIEBaseTypeRef::print(raw_ostream &O) const { O << "BaseTypeRef: " << Index; }
554
555//===----------------------------------------------------------------------===//
556// DIEDelta Implementation
557//===----------------------------------------------------------------------===//
558
559/// EmitValue - Emit delta value.
560///
561void DIEDelta::emitValue(const AsmPrinter *AP, dwarf::Form Form) const {
562 AP->emitLabelDifference(Hi: LabelHi, Lo: LabelLo,
563 Size: sizeOf(FormParams: AP->getDwarfFormParams(), Form));
564}
565
566/// SizeOf - Determine size of delta value in bytes.
567///
568unsigned DIEDelta::sizeOf(const dwarf::FormParams &FormParams,
569 dwarf::Form Form) const {
570 switch (Form) {
571 case dwarf::DW_FORM_data4:
572 return 4;
573 case dwarf::DW_FORM_data8:
574 return 8;
575 case dwarf::DW_FORM_sec_offset:
576 return FormParams.getDwarfOffsetByteSize();
577 default:
578 llvm_unreachable("DIE Value form not supported yet");
579 }
580}
581
582LLVM_DUMP_METHOD
583void DIEDelta::print(raw_ostream &O) const {
584 O << "Del: " << LabelHi->getName() << "-" << LabelLo->getName();
585}
586
587//===----------------------------------------------------------------------===//
588// DIEString Implementation
589//===----------------------------------------------------------------------===//
590
591/// EmitValue - Emit string value.
592///
593void DIEString::emitValue(const AsmPrinter *AP, dwarf::Form Form) const {
594 // Index of string in symbol table.
595 switch (Form) {
596 case dwarf::DW_FORM_GNU_str_index:
597 case dwarf::DW_FORM_strx:
598 case dwarf::DW_FORM_strx1:
599 case dwarf::DW_FORM_strx2:
600 case dwarf::DW_FORM_strx3:
601 case dwarf::DW_FORM_strx4:
602 DIEInteger(S.getIndex()).emitValue(Asm: AP, Form);
603 return;
604 case dwarf::DW_FORM_strp:
605 if (AP->doesDwarfUseRelocationsAcrossSections())
606 DIELabel(S.getSymbol()).emitValue(AP, Form);
607 else
608 DIEInteger(S.getOffset()).emitValue(Asm: AP, Form);
609 return;
610 default:
611 llvm_unreachable("Expected valid string form");
612 }
613}
614
615/// sizeOf - Determine size of delta value in bytes.
616///
617unsigned DIEString::sizeOf(const dwarf::FormParams &FormParams,
618 dwarf::Form Form) const {
619 // Index of string in symbol table.
620 switch (Form) {
621 case dwarf::DW_FORM_GNU_str_index:
622 case dwarf::DW_FORM_strx:
623 case dwarf::DW_FORM_strx1:
624 case dwarf::DW_FORM_strx2:
625 case dwarf::DW_FORM_strx3:
626 case dwarf::DW_FORM_strx4:
627 return DIEInteger(S.getIndex()).sizeOf(FormParams, Form);
628 case dwarf::DW_FORM_strp:
629 if (FormParams.DwarfUsesRelocationsAcrossSections)
630 return DIELabel(S.getSymbol()).sizeOf(FormParams, Form);
631 return DIEInteger(S.getOffset()).sizeOf(FormParams, Form);
632 default:
633 llvm_unreachable("Expected valid string form");
634 }
635}
636
637LLVM_DUMP_METHOD
638void DIEString::print(raw_ostream &O) const {
639 O << "String: " << S.getString();
640}
641
642//===----------------------------------------------------------------------===//
643// DIEInlineString Implementation
644//===----------------------------------------------------------------------===//
645void DIEInlineString::emitValue(const AsmPrinter *AP, dwarf::Form Form) const {
646 if (Form == dwarf::DW_FORM_string) {
647 AP->OutStreamer->emitBytes(Data: S);
648 AP->emitInt8(Value: 0);
649 return;
650 }
651 llvm_unreachable("Expected valid string form");
652}
653
654unsigned DIEInlineString::sizeOf(const dwarf::FormParams &, dwarf::Form) const {
655 // Emit string bytes + NULL byte.
656 return S.size() + 1;
657}
658
659LLVM_DUMP_METHOD
660void DIEInlineString::print(raw_ostream &O) const {
661 O << "InlineString: " << S;
662}
663
664//===----------------------------------------------------------------------===//
665// DIEEntry Implementation
666//===----------------------------------------------------------------------===//
667
668/// EmitValue - Emit debug information entry offset.
669///
670void DIEEntry::emitValue(const AsmPrinter *AP, dwarf::Form Form) const {
671
672 switch (Form) {
673 case dwarf::DW_FORM_ref1:
674 case dwarf::DW_FORM_ref2:
675 case dwarf::DW_FORM_ref4:
676 case dwarf::DW_FORM_ref8:
677 AP->OutStreamer->emitIntValue(Value: Entry->getOffset(),
678 Size: sizeOf(FormParams: AP->getDwarfFormParams(), Form));
679 return;
680
681 case dwarf::DW_FORM_ref_udata:
682 AP->emitULEB128(Value: Entry->getOffset());
683 return;
684
685 case dwarf::DW_FORM_ref_addr: {
686 // Get the absolute offset for this DIE within the debug info/types section.
687 uint64_t Addr = Entry->getDebugSectionOffset();
688 if (const MCSymbol *SectionSym =
689 Entry->getUnit()->getCrossSectionRelativeBaseAddress()) {
690 AP->emitLabelPlusOffset(Label: SectionSym, Offset: Addr,
691 Size: sizeOf(FormParams: AP->getDwarfFormParams(), Form), IsSectionRelative: true);
692 return;
693 }
694
695 AP->OutStreamer->emitIntValue(Value: Addr, Size: sizeOf(FormParams: AP->getDwarfFormParams(), Form));
696 return;
697 }
698 default:
699 llvm_unreachable("Improper form for DIE reference");
700 }
701}
702
703unsigned DIEEntry::sizeOf(const dwarf::FormParams &FormParams,
704 dwarf::Form Form) const {
705 switch (Form) {
706 case dwarf::DW_FORM_ref1:
707 return 1;
708 case dwarf::DW_FORM_ref2:
709 return 2;
710 case dwarf::DW_FORM_ref4:
711 return 4;
712 case dwarf::DW_FORM_ref8:
713 return 8;
714 case dwarf::DW_FORM_ref_udata:
715 return getULEB128Size(Value: Entry->getOffset());
716 case dwarf::DW_FORM_ref_addr:
717 return FormParams.getRefAddrByteSize();
718
719 default:
720 llvm_unreachable("Improper form for DIE reference");
721 }
722}
723
724LLVM_DUMP_METHOD
725void DIEEntry::print(raw_ostream &O) const {
726 O << format(Fmt: "Die: 0x%lx", Vals: (long)(intptr_t)&Entry);
727}
728
729//===----------------------------------------------------------------------===//
730// DIELoc Implementation
731//===----------------------------------------------------------------------===//
732
733unsigned DIELoc::computeSize(const dwarf::FormParams &FormParams) const {
734 if (!Size) {
735 for (const auto &V : values())
736 Size += V.sizeOf(FormParams);
737 }
738
739 return Size;
740}
741
742/// EmitValue - Emit location data.
743///
744void DIELoc::emitValue(const AsmPrinter *Asm, dwarf::Form Form) const {
745 switch (Form) {
746 default: llvm_unreachable("Improper form for block");
747 case dwarf::DW_FORM_block1: Asm->emitInt8(Value: Size); break;
748 case dwarf::DW_FORM_block2: Asm->emitInt16(Value: Size); break;
749 case dwarf::DW_FORM_block4: Asm->emitInt32(Value: Size); break;
750 case dwarf::DW_FORM_block:
751 case dwarf::DW_FORM_exprloc:
752 Asm->emitULEB128(Value: Size);
753 break;
754 }
755
756 for (const auto &V : values())
757 V.emitValue(AP: Asm);
758}
759
760/// sizeOf - Determine size of location data in bytes.
761///
762unsigned DIELoc::sizeOf(const dwarf::FormParams &, dwarf::Form Form) const {
763 switch (Form) {
764 case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
765 case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
766 case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
767 case dwarf::DW_FORM_block:
768 case dwarf::DW_FORM_exprloc:
769 return Size + getULEB128Size(Value: Size);
770 default: llvm_unreachable("Improper form for block");
771 }
772}
773
774LLVM_DUMP_METHOD
775void DIELoc::print(raw_ostream &O) const {
776 printValues(O, Values: *this, Type: "ExprLoc", Size, IndentCount: 5);
777}
778
779//===----------------------------------------------------------------------===//
780// DIEBlock Implementation
781//===----------------------------------------------------------------------===//
782
783unsigned DIEBlock::computeSize(const dwarf::FormParams &FormParams) const {
784 if (!Size) {
785 for (const auto &V : values())
786 Size += V.sizeOf(FormParams);
787 }
788
789 return Size;
790}
791
792/// EmitValue - Emit block data.
793///
794void DIEBlock::emitValue(const AsmPrinter *Asm, dwarf::Form Form) const {
795 switch (Form) {
796 default: llvm_unreachable("Improper form for block");
797 case dwarf::DW_FORM_block1: Asm->emitInt8(Value: Size); break;
798 case dwarf::DW_FORM_block2: Asm->emitInt16(Value: Size); break;
799 case dwarf::DW_FORM_block4: Asm->emitInt32(Value: Size); break;
800 case dwarf::DW_FORM_exprloc:
801 case dwarf::DW_FORM_block:
802 Asm->emitULEB128(Value: Size);
803 break;
804 case dwarf::DW_FORM_string: break;
805 case dwarf::DW_FORM_data16: break;
806 }
807
808 for (const auto &V : values())
809 V.emitValue(AP: Asm);
810}
811
812/// sizeOf - Determine size of block data in bytes.
813///
814unsigned DIEBlock::sizeOf(const dwarf::FormParams &, dwarf::Form Form) const {
815 switch (Form) {
816 case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
817 case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
818 case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
819 case dwarf::DW_FORM_exprloc:
820 case dwarf::DW_FORM_block: return Size + getULEB128Size(Value: Size);
821 case dwarf::DW_FORM_data16: return 16;
822 default: llvm_unreachable("Improper form for block");
823 }
824}
825
826LLVM_DUMP_METHOD
827void DIEBlock::print(raw_ostream &O) const {
828 printValues(O, Values: *this, Type: "Blk", Size, IndentCount: 5);
829}
830
831//===----------------------------------------------------------------------===//
832// DIELocList Implementation
833//===----------------------------------------------------------------------===//
834
835unsigned DIELocList::sizeOf(const dwarf::FormParams &FormParams,
836 dwarf::Form Form) const {
837 switch (Form) {
838 case dwarf::DW_FORM_loclistx:
839 return getULEB128Size(Value: Index);
840 case dwarf::DW_FORM_data4:
841 assert(FormParams.Format != dwarf::DWARF64 &&
842 "DW_FORM_data4 is not suitable to emit a pointer to a location list "
843 "in the 64-bit DWARF format");
844 return 4;
845 case dwarf::DW_FORM_data8:
846 assert(FormParams.Format == dwarf::DWARF64 &&
847 "DW_FORM_data8 is not suitable to emit a pointer to a location list "
848 "in the 32-bit DWARF format");
849 return 8;
850 case dwarf::DW_FORM_sec_offset:
851 return FormParams.getDwarfOffsetByteSize();
852 default:
853 llvm_unreachable("DIE Value form not supported yet");
854 }
855}
856
857/// EmitValue - Emit label value.
858///
859void DIELocList::emitValue(const AsmPrinter *AP, dwarf::Form Form) const {
860 if (Form == dwarf::DW_FORM_loclistx) {
861 AP->emitULEB128(Value: Index);
862 return;
863 }
864 DwarfDebug *DD = AP->getDwarfDebug();
865 MCSymbol *Label = DD->getDebugLocs().getList(LI: Index).Label;
866 AP->emitDwarfSymbolReference(Label, /*ForceOffset*/ DD->useSplitDwarf());
867}
868
869LLVM_DUMP_METHOD
870void DIELocList::print(raw_ostream &O) const { O << "LocList: " << Index; }
871
872//===----------------------------------------------------------------------===//
873// DIEAddrOffset Implementation
874//===----------------------------------------------------------------------===//
875
876unsigned DIEAddrOffset::sizeOf(const dwarf::FormParams &FormParams,
877 dwarf::Form) const {
878 return Addr.sizeOf(FormParams, Form: dwarf::DW_FORM_addrx) +
879 Offset.sizeOf(FormParams, Form: dwarf::DW_FORM_data4);
880}
881
882/// EmitValue - Emit label value.
883///
884void DIEAddrOffset::emitValue(const AsmPrinter *AP, dwarf::Form Form) const {
885 Addr.emitValue(Asm: AP, Form: dwarf::DW_FORM_addrx);
886 Offset.emitValue(AP, Form: dwarf::DW_FORM_data4);
887}
888
889LLVM_DUMP_METHOD
890void DIEAddrOffset::print(raw_ostream &O) const {
891 O << "AddrOffset: ";
892 Addr.print(O);
893 O << " + ";
894 Offset.print(O);
895}
896