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 unsigned 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 AsmPrinter *AP,
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(AP);
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 = Child.computeOffsetsAndAbbrevs(AP, AbbrevSet, CUOffset);
301 
302     // Each child chain is terminated with a zero byte, adjust the offset.
303     CUOffset += sizeof(int8_t);
304   }
305 
306   // Compute the byte size of this DIE and all of its children correctly. This
307   // is needed so that top level DIE can help the compile unit set its length
308   // correctly.
309   setSize(CUOffset - getOffset());
310   return CUOffset;
311 }
312 
313 //===----------------------------------------------------------------------===//
314 // DIEUnit Implementation
315 //===----------------------------------------------------------------------===//
316 DIEUnit::DIEUnit(dwarf::Tag UnitTag)
317     : Die(UnitTag), Section(nullptr), Offset(0) {
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 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     return getDIE##T().SizeOf(AP, 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, SizeOf(Asm, Form));
411     return;
412   case dwarf::DW_FORM_GNU_str_index:
413   case dwarf::DW_FORM_GNU_addr_index:
414   case dwarf::DW_FORM_ref_udata:
415   case dwarf::DW_FORM_strx:
416   case dwarf::DW_FORM_addrx:
417   case dwarf::DW_FORM_rnglistx:
418   case dwarf::DW_FORM_udata:
419     Asm->emitULEB128(Integer);
420     return;
421   case dwarf::DW_FORM_sdata:
422     Asm->emitSLEB128(Integer);
423     return;
424   default: llvm_unreachable("DIE Value form not supported yet");
425   }
426 }
427 
428 /// SizeOf - Determine size of integer value in bytes.
429 ///
430 unsigned DIEInteger::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
431   dwarf::FormParams Params = {0, 0, dwarf::DWARF32};
432   if (AP)
433     Params = {AP->getDwarfVersion(), uint8_t(AP->getPointerSize()),
434               AP->OutStreamer->getContext().getDwarfFormat()};
435 
436   if (Optional<uint8_t> FixedSize = dwarf::getFixedFormByteSize(Form, Params))
437     return *FixedSize;
438 
439   switch (Form) {
440   case dwarf::DW_FORM_GNU_str_index:
441   case dwarf::DW_FORM_GNU_addr_index:
442   case dwarf::DW_FORM_ref_udata:
443   case dwarf::DW_FORM_strx:
444   case dwarf::DW_FORM_addrx:
445   case dwarf::DW_FORM_rnglistx:
446   case dwarf::DW_FORM_udata:
447     return getULEB128Size(Integer);
448   case dwarf::DW_FORM_sdata:
449     return getSLEB128Size(Integer);
450   default: llvm_unreachable("DIE Value form not supported yet");
451   }
452 }
453 
454 LLVM_DUMP_METHOD
455 void DIEInteger::print(raw_ostream &O) const {
456   O << "Int: " << (int64_t)Integer << "  0x";
457   O.write_hex(Integer);
458 }
459 
460 //===----------------------------------------------------------------------===//
461 // DIEExpr Implementation
462 //===----------------------------------------------------------------------===//
463 
464 /// EmitValue - Emit expression value.
465 ///
466 void DIEExpr::emitValue(const AsmPrinter *AP, dwarf::Form Form) const {
467   AP->emitDebugValue(Expr, SizeOf(AP, Form));
468 }
469 
470 /// SizeOf - Determine size of expression value in bytes.
471 ///
472 unsigned DIEExpr::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
473   switch (Form) {
474   case dwarf::DW_FORM_data4:
475     return 4;
476   case dwarf::DW_FORM_data8:
477     return 8;
478   case dwarf::DW_FORM_sec_offset:
479     // FIXME: add support for DWARF64
480     return 4;
481   default:
482     llvm_unreachable("DIE Value form not supported yet");
483   }
484 }
485 
486 LLVM_DUMP_METHOD
487 void DIEExpr::print(raw_ostream &O) const { O << "Expr: " << *Expr; }
488 
489 //===----------------------------------------------------------------------===//
490 // DIELabel Implementation
491 //===----------------------------------------------------------------------===//
492 
493 /// EmitValue - Emit label value.
494 ///
495 void DIELabel::emitValue(const AsmPrinter *AP, dwarf::Form Form) const {
496   bool IsSectionRelative = Form != dwarf::DW_FORM_addr;
497   AP->emitLabelReference(Label, SizeOf(AP, Form), IsSectionRelative);
498 }
499 
500 /// SizeOf - Determine size of label value in bytes.
501 ///
502 unsigned DIELabel::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
503   switch (Form) {
504   case dwarf::DW_FORM_data4:
505     return 4;
506   case dwarf::DW_FORM_sec_offset:
507   case dwarf::DW_FORM_strp:
508     // FIXME: add support for DWARF64
509     return 4;
510   case dwarf::DW_FORM_addr:
511     return AP->MAI->getCodePointerSize();
512   default:
513     llvm_unreachable("DIE Value form not supported yet");
514   }
515 }
516 
517 LLVM_DUMP_METHOD
518 void DIELabel::print(raw_ostream &O) const { O << "Lbl: " << Label->getName(); }
519 
520 //===----------------------------------------------------------------------===//
521 // DIEBaseTypeRef Implementation
522 //===----------------------------------------------------------------------===//
523 
524 void DIEBaseTypeRef::emitValue(const AsmPrinter *AP, dwarf::Form Form) const {
525   uint64_t Offset = CU->ExprRefedBaseTypes[Index].Die->getOffset();
526   assert(Offset < (1ULL << (ULEB128PadSize * 7)) && "Offset wont fit");
527   AP->emitULEB128(Offset, nullptr, ULEB128PadSize);
528 }
529 
530 unsigned DIEBaseTypeRef::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
531   return ULEB128PadSize;
532 }
533 
534 LLVM_DUMP_METHOD
535 void DIEBaseTypeRef::print(raw_ostream &O) const { O << "BaseTypeRef: " << Index; }
536 
537 //===----------------------------------------------------------------------===//
538 // DIEDelta Implementation
539 //===----------------------------------------------------------------------===//
540 
541 /// EmitValue - Emit delta value.
542 ///
543 void DIEDelta::emitValue(const AsmPrinter *AP, dwarf::Form Form) const {
544   AP->emitLabelDifference(LabelHi, LabelLo, SizeOf(AP, Form));
545 }
546 
547 /// SizeOf - Determine size of delta value in bytes.
548 ///
549 unsigned DIEDelta::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
550   switch (Form) {
551   case dwarf::DW_FORM_data4:
552     return 4;
553   case dwarf::DW_FORM_sec_offset:
554     // FIXME: add support for DWARF64
555     return 4;
556   default:
557     llvm_unreachable("DIE Value form not supported yet");
558   }
559 }
560 
561 LLVM_DUMP_METHOD
562 void DIEDelta::print(raw_ostream &O) const {
563   O << "Del: " << LabelHi->getName() << "-" << LabelLo->getName();
564 }
565 
566 //===----------------------------------------------------------------------===//
567 // DIEString Implementation
568 //===----------------------------------------------------------------------===//
569 
570 /// EmitValue - Emit string value.
571 ///
572 void DIEString::emitValue(const AsmPrinter *AP, dwarf::Form Form) const {
573   // Index of string in symbol table.
574   switch (Form) {
575   case dwarf::DW_FORM_GNU_str_index:
576   case dwarf::DW_FORM_strx:
577   case dwarf::DW_FORM_strx1:
578   case dwarf::DW_FORM_strx2:
579   case dwarf::DW_FORM_strx3:
580   case dwarf::DW_FORM_strx4:
581     DIEInteger(S.getIndex()).emitValue(AP, Form);
582     return;
583   case dwarf::DW_FORM_strp:
584     if (AP->MAI->doesDwarfUseRelocationsAcrossSections())
585       DIELabel(S.getSymbol()).emitValue(AP, Form);
586     else
587       DIEInteger(S.getOffset()).emitValue(AP, Form);
588     return;
589   default:
590     llvm_unreachable("Expected valid string form");
591   }
592 }
593 
594 /// SizeOf - Determine size of delta value in bytes.
595 ///
596 unsigned DIEString::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
597   // Index of string in symbol table.
598   switch (Form) {
599   case dwarf::DW_FORM_GNU_str_index:
600   case dwarf::DW_FORM_strx:
601   case dwarf::DW_FORM_strx1:
602   case dwarf::DW_FORM_strx2:
603   case dwarf::DW_FORM_strx3:
604   case dwarf::DW_FORM_strx4:
605     return DIEInteger(S.getIndex()).SizeOf(AP, Form);
606   case dwarf::DW_FORM_strp:
607     if (AP->MAI->doesDwarfUseRelocationsAcrossSections())
608       return DIELabel(S.getSymbol()).SizeOf(AP, Form);
609     return DIEInteger(S.getOffset()).SizeOf(AP, Form);
610   default:
611     llvm_unreachable("Expected valid string form");
612   }
613 }
614 
615 LLVM_DUMP_METHOD
616 void DIEString::print(raw_ostream &O) const {
617   O << "String: " << S.getString();
618 }
619 
620 //===----------------------------------------------------------------------===//
621 // DIEInlineString Implementation
622 //===----------------------------------------------------------------------===//
623 void DIEInlineString::emitValue(const AsmPrinter *AP, dwarf::Form Form) const {
624   if (Form == dwarf::DW_FORM_string) {
625     AP->OutStreamer->emitBytes(S);
626     AP->emitInt8(0);
627     return;
628   }
629   llvm_unreachable("Expected valid string form");
630 }
631 
632 unsigned DIEInlineString::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
633   // Emit string bytes + NULL byte.
634   return S.size() + 1;
635 }
636 
637 LLVM_DUMP_METHOD
638 void DIEInlineString::print(raw_ostream &O) const {
639   O << "InlineString: " << S;
640 }
641 
642 //===----------------------------------------------------------------------===//
643 // DIEEntry Implementation
644 //===----------------------------------------------------------------------===//
645 
646 /// EmitValue - Emit debug information entry offset.
647 ///
648 void DIEEntry::emitValue(const AsmPrinter *AP, dwarf::Form Form) const {
649 
650   switch (Form) {
651   case dwarf::DW_FORM_ref1:
652   case dwarf::DW_FORM_ref2:
653   case dwarf::DW_FORM_ref4:
654   case dwarf::DW_FORM_ref8:
655     AP->OutStreamer->emitIntValue(Entry->getOffset(), SizeOf(AP, Form));
656     return;
657 
658   case dwarf::DW_FORM_ref_udata:
659     AP->emitULEB128(Entry->getOffset());
660     return;
661 
662   case dwarf::DW_FORM_ref_addr: {
663     // Get the absolute offset for this DIE within the debug info/types section.
664     unsigned Addr = Entry->getDebugSectionOffset();
665     if (const MCSymbol *SectionSym =
666             Entry->getUnit()->getCrossSectionRelativeBaseAddress()) {
667       AP->emitLabelPlusOffset(SectionSym, Addr, SizeOf(AP, Form), true);
668       return;
669     }
670 
671     AP->OutStreamer->emitIntValue(Addr, SizeOf(AP, Form));
672     return;
673   }
674   default:
675     llvm_unreachable("Improper form for DIE reference");
676   }
677 }
678 
679 unsigned DIEEntry::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
680   switch (Form) {
681   case dwarf::DW_FORM_ref1:
682     return 1;
683   case dwarf::DW_FORM_ref2:
684     return 2;
685   case dwarf::DW_FORM_ref4:
686     return 4;
687   case dwarf::DW_FORM_ref8:
688     return 8;
689   case dwarf::DW_FORM_ref_udata:
690     return getULEB128Size(Entry->getOffset());
691   case dwarf::DW_FORM_ref_addr:
692     if (AP->getDwarfVersion() == 2)
693       return AP->MAI->getCodePointerSize();
694     switch (AP->OutStreamer->getContext().getDwarfFormat()) {
695     case dwarf::DWARF32:
696       return 4;
697     case dwarf::DWARF64:
698       return 8;
699     }
700     llvm_unreachable("Invalid DWARF format");
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 /// ComputeSize - calculate the size of the location expression.
717 ///
718 unsigned DIELoc::ComputeSize(const AsmPrinter *AP) const {
719   if (!Size) {
720     for (const auto &V : values())
721       Size += V.SizeOf(AP);
722   }
723 
724   return Size;
725 }
726 
727 /// EmitValue - Emit location data.
728 ///
729 void DIELoc::emitValue(const AsmPrinter *Asm, dwarf::Form Form) const {
730   switch (Form) {
731   default: llvm_unreachable("Improper form for block");
732   case dwarf::DW_FORM_block1: Asm->emitInt8(Size);    break;
733   case dwarf::DW_FORM_block2: Asm->emitInt16(Size);   break;
734   case dwarf::DW_FORM_block4: Asm->emitInt32(Size);   break;
735   case dwarf::DW_FORM_block:
736   case dwarf::DW_FORM_exprloc:
737     Asm->emitULEB128(Size);
738     break;
739   }
740 
741   for (const auto &V : values())
742     V.emitValue(Asm);
743 }
744 
745 /// SizeOf - Determine size of location data in bytes.
746 ///
747 unsigned DIELoc::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
748   switch (Form) {
749   case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
750   case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
751   case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
752   case dwarf::DW_FORM_block:
753   case dwarf::DW_FORM_exprloc:
754     return Size + getULEB128Size(Size);
755   default: llvm_unreachable("Improper form for block");
756   }
757 }
758 
759 LLVM_DUMP_METHOD
760 void DIELoc::print(raw_ostream &O) const {
761   printValues(O, *this, "ExprLoc", Size, 5);
762 }
763 
764 //===----------------------------------------------------------------------===//
765 // DIEBlock Implementation
766 //===----------------------------------------------------------------------===//
767 
768 /// ComputeSize - calculate the size of the block.
769 ///
770 unsigned DIEBlock::ComputeSize(const AsmPrinter *AP) const {
771   if (!Size) {
772     for (const auto &V : values())
773       Size += V.SizeOf(AP);
774   }
775 
776   return Size;
777 }
778 
779 /// EmitValue - Emit block data.
780 ///
781 void DIEBlock::emitValue(const AsmPrinter *Asm, dwarf::Form Form) const {
782   switch (Form) {
783   default: llvm_unreachable("Improper form for block");
784   case dwarf::DW_FORM_block1: Asm->emitInt8(Size);    break;
785   case dwarf::DW_FORM_block2: Asm->emitInt16(Size);   break;
786   case dwarf::DW_FORM_block4: Asm->emitInt32(Size);   break;
787   case dwarf::DW_FORM_block:
788     Asm->emitULEB128(Size);
789     break;
790   case dwarf::DW_FORM_string: break;
791   case dwarf::DW_FORM_data16: break;
792   }
793 
794   for (const auto &V : values())
795     V.emitValue(Asm);
796 }
797 
798 /// SizeOf - Determine size of block data in bytes.
799 ///
800 unsigned DIEBlock::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
801   switch (Form) {
802   case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
803   case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
804   case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
805   case dwarf::DW_FORM_block:  return Size + getULEB128Size(Size);
806   case dwarf::DW_FORM_data16: return 16;
807   default: llvm_unreachable("Improper form for block");
808   }
809 }
810 
811 LLVM_DUMP_METHOD
812 void DIEBlock::print(raw_ostream &O) const {
813   printValues(O, *this, "Blk", Size, 5);
814 }
815 
816 //===----------------------------------------------------------------------===//
817 // DIELocList Implementation
818 //===----------------------------------------------------------------------===//
819 
820 unsigned DIELocList::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
821   switch (Form) {
822   case dwarf::DW_FORM_loclistx:
823     return getULEB128Size(Index);
824   case dwarf::DW_FORM_data4:
825     return 4;
826   case dwarf::DW_FORM_sec_offset:
827     // FIXME: add support for DWARF64
828     return 4;
829   default:
830     llvm_unreachable("DIE Value form not supported yet");
831   }
832 }
833 
834 /// EmitValue - Emit label value.
835 ///
836 void DIELocList::emitValue(const AsmPrinter *AP, dwarf::Form Form) const {
837   if (Form == dwarf::DW_FORM_loclistx) {
838     AP->emitULEB128(Index);
839     return;
840   }
841   DwarfDebug *DD = AP->getDwarfDebug();
842   MCSymbol *Label = DD->getDebugLocs().getList(Index).Label;
843   AP->emitDwarfSymbolReference(Label, /*ForceOffset*/ DD->useSplitDwarf());
844 }
845 
846 LLVM_DUMP_METHOD
847 void DIELocList::print(raw_ostream &O) const { O << "LocList: " << Index; }
848