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