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