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