1 //===--- lib/CodeGen/DIE.cpp - DWARF Info Entries -------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Data structures for DWARF info entries.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/CodeGen/DIE.h"
15 #include "DwarfCompileUnit.h"
16 #include "DwarfDebug.h"
17 #include "DwarfUnit.h"
18 #include "llvm/ADT/Twine.h"
19 #include "llvm/CodeGen/AsmPrinter.h"
20 #include "llvm/Config/llvm-config.h"
21 #include "llvm/IR/DataLayout.h"
22 #include "llvm/MC/MCAsmInfo.h"
23 #include "llvm/MC/MCContext.h"
24 #include "llvm/MC/MCStreamer.h"
25 #include "llvm/MC/MCSymbol.h"
26 #include "llvm/Support/Debug.h"
27 #include "llvm/Support/ErrorHandling.h"
28 #include "llvm/Support/Format.h"
29 #include "llvm/Support/FormattedStream.h"
30 #include "llvm/Support/LEB128.h"
31 #include "llvm/Support/MD5.h"
32 #include "llvm/Support/raw_ostream.h"
33 using namespace llvm;
34 
35 #define DEBUG_TYPE "dwarfdebug"
36 
37 //===----------------------------------------------------------------------===//
38 // DIEAbbrevData Implementation
39 //===----------------------------------------------------------------------===//
40 
41 /// Profile - Used to gather unique data for the abbreviation folding set.
42 ///
43 void DIEAbbrevData::Profile(FoldingSetNodeID &ID) const {
44   // Explicitly cast to an integer type for which FoldingSetNodeID has
45   // overloads.  Otherwise MSVC 2010 thinks this call is ambiguous.
46   ID.AddInteger(unsigned(Attribute));
47   ID.AddInteger(unsigned(Form));
48   if (Form == dwarf::DW_FORM_implicit_const)
49     ID.AddInteger(Value);
50 }
51 
52 //===----------------------------------------------------------------------===//
53 // DIEAbbrev Implementation
54 //===----------------------------------------------------------------------===//
55 
56 /// Profile - Used to gather unique data for the abbreviation folding set.
57 ///
58 void DIEAbbrev::Profile(FoldingSetNodeID &ID) const {
59   ID.AddInteger(unsigned(Tag));
60   ID.AddInteger(unsigned(Children));
61 
62   // For each attribute description.
63   for (unsigned i = 0, N = Data.size(); i < N; ++i)
64     Data[i].Profile(ID);
65 }
66 
67 /// Emit - Print the abbreviation using the specified asm printer.
68 ///
69 void DIEAbbrev::Emit(const AsmPrinter *AP) const {
70   // Emit its Dwarf tag type.
71   AP->EmitULEB128(Tag, dwarf::TagString(Tag).data());
72 
73   // Emit whether it has children DIEs.
74   AP->EmitULEB128((unsigned)Children, dwarf::ChildrenString(Children).data());
75 
76   // For each attribute description.
77   for (unsigned i = 0, N = Data.size(); i < N; ++i) {
78     const DIEAbbrevData &AttrData = Data[i];
79 
80     // Emit attribute type.
81     AP->EmitULEB128(AttrData.getAttribute(),
82                     dwarf::AttributeString(AttrData.getAttribute()).data());
83 
84     // Emit form type.
85 #ifndef NDEBUG
86     // Could be an assertion, but this way we can see the failing form code
87     // easily, which helps track down where it came from.
88     if (!dwarf::isValidFormForVersion(AttrData.getForm(),
89                                       AP->getDwarfVersion())) {
90       DEBUG(dbgs() << "Invalid form " << format("0x%x", AttrData.getForm())
91                    << " for DWARF version " << AP->getDwarfVersion() << "\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 const 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(uint16_t V, uint8_t A, dwarf::Tag UnitTag)
317     : Die(UnitTag), Section(nullptr), Offset(0), Length(0), Version(V),
318       AddrSize(A)
319 {
320   Die.Owner = this;
321   assert((UnitTag == dwarf::DW_TAG_compile_unit ||
322           UnitTag == dwarf::DW_TAG_type_unit ||
323           UnitTag == dwarf::DW_TAG_partial_unit) && "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_udata:
417     Asm->EmitULEB128(Integer);
418     return;
419   case dwarf::DW_FORM_sdata:
420     Asm->EmitSLEB128(Integer);
421     return;
422   default: llvm_unreachable("DIE Value form not supported yet");
423   }
424 }
425 
426 /// SizeOf - Determine size of integer value in bytes.
427 ///
428 unsigned DIEInteger::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
429   dwarf::FormParams Params = {0, 0, dwarf::DWARF32};
430   if (AP)
431     Params = {AP->getDwarfVersion(), uint8_t(AP->getPointerSize()),
432               AP->OutStreamer->getContext().getDwarfFormat()};
433 
434   if (Optional<uint8_t> FixedSize = dwarf::getFixedFormByteSize(Form, Params))
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_udata:
443     return getULEB128Size(Integer);
444   case dwarf::DW_FORM_sdata:
445     return getSLEB128Size(Integer);
446   default: llvm_unreachable("DIE Value form not supported yet");
447   }
448 }
449 
450 LLVM_DUMP_METHOD
451 void DIEInteger::print(raw_ostream &O) const {
452   O << "Int: " << (int64_t)Integer << "  0x";
453   O.write_hex(Integer);
454 }
455 
456 //===----------------------------------------------------------------------===//
457 // DIEExpr Implementation
458 //===----------------------------------------------------------------------===//
459 
460 /// EmitValue - Emit expression value.
461 ///
462 void DIEExpr::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
463   AP->EmitDebugThreadLocal(Expr, SizeOf(AP, Form));
464 }
465 
466 /// SizeOf - Determine size of expression value in bytes.
467 ///
468 unsigned DIEExpr::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
469   if (Form == dwarf::DW_FORM_data4) return 4;
470   if (Form == dwarf::DW_FORM_sec_offset) return 4;
471   if (Form == dwarf::DW_FORM_strp) return 4;
472   return AP->getPointerSize();
473 }
474 
475 LLVM_DUMP_METHOD
476 void DIEExpr::print(raw_ostream &O) const { O << "Expr: " << *Expr; }
477 
478 //===----------------------------------------------------------------------===//
479 // DIELabel Implementation
480 //===----------------------------------------------------------------------===//
481 
482 /// EmitValue - Emit label value.
483 ///
484 void DIELabel::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
485   AP->EmitLabelReference(Label, SizeOf(AP, Form),
486                          Form == dwarf::DW_FORM_strp ||
487                              Form == dwarf::DW_FORM_sec_offset ||
488                              Form == dwarf::DW_FORM_ref_addr ||
489                              Form == dwarf::DW_FORM_data4);
490 }
491 
492 /// SizeOf - Determine size of label value in bytes.
493 ///
494 unsigned DIELabel::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
495   if (Form == dwarf::DW_FORM_data4) return 4;
496   if (Form == dwarf::DW_FORM_sec_offset) return 4;
497   if (Form == dwarf::DW_FORM_strp) return 4;
498   return AP->MAI->getCodePointerSize();
499 }
500 
501 LLVM_DUMP_METHOD
502 void DIELabel::print(raw_ostream &O) const { O << "Lbl: " << Label->getName(); }
503 
504 //===----------------------------------------------------------------------===//
505 // DIEDelta Implementation
506 //===----------------------------------------------------------------------===//
507 
508 /// EmitValue - Emit delta value.
509 ///
510 void DIEDelta::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
511   AP->EmitLabelDifference(LabelHi, LabelLo, SizeOf(AP, Form));
512 }
513 
514 /// SizeOf - Determine size of delta value in bytes.
515 ///
516 unsigned DIEDelta::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
517   if (Form == dwarf::DW_FORM_data4) return 4;
518   if (Form == dwarf::DW_FORM_sec_offset) return 4;
519   if (Form == dwarf::DW_FORM_strp) return 4;
520   return AP->MAI->getCodePointerSize();
521 }
522 
523 LLVM_DUMP_METHOD
524 void DIEDelta::print(raw_ostream &O) const {
525   O << "Del: " << LabelHi->getName() << "-" << LabelLo->getName();
526 }
527 
528 //===----------------------------------------------------------------------===//
529 // DIEString Implementation
530 //===----------------------------------------------------------------------===//
531 
532 /// EmitValue - Emit string value.
533 ///
534 void DIEString::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
535   // Index of string in symbol table.
536   switch (Form) {
537   case dwarf::DW_FORM_GNU_str_index:
538   case dwarf::DW_FORM_strx:
539   case dwarf::DW_FORM_strx1:
540   case dwarf::DW_FORM_strx2:
541   case dwarf::DW_FORM_strx3:
542   case dwarf::DW_FORM_strx4:
543     DIEInteger(S.getIndex()).EmitValue(AP, Form);
544     return;
545   case dwarf::DW_FORM_strp:
546     if (AP->MAI->doesDwarfUseRelocationsAcrossSections())
547       DIELabel(S.getSymbol()).EmitValue(AP, Form);
548     else
549       DIEInteger(S.getOffset()).EmitValue(AP, Form);
550     return;
551   default:
552     llvm_unreachable("Expected valid string form");
553   }
554 }
555 
556 /// SizeOf - Determine size of delta value in bytes.
557 ///
558 unsigned DIEString::SizeOf(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     return DIEInteger(S.getIndex()).SizeOf(AP, Form);
568   case dwarf::DW_FORM_strp:
569     if (AP->MAI->doesDwarfUseRelocationsAcrossSections())
570       return DIELabel(S.getSymbol()).SizeOf(AP, Form);
571     return DIEInteger(S.getOffset()).SizeOf(AP, Form);
572   default:
573     llvm_unreachable("Expected valid string form");
574   }
575 }
576 
577 LLVM_DUMP_METHOD
578 void DIEString::print(raw_ostream &O) const {
579   O << "String: " << S.getString();
580 }
581 
582 //===----------------------------------------------------------------------===//
583 // DIEInlineString Implementation
584 //===----------------------------------------------------------------------===//
585 void DIEInlineString::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
586   if (Form == dwarf::DW_FORM_string) {
587     for (char ch : S)
588       AP->emitInt8(ch);
589     AP->emitInt8(0);
590     return;
591   }
592   llvm_unreachable("Expected valid string form");
593 }
594 
595 unsigned DIEInlineString::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
596   // Emit string bytes + NULL byte.
597   return S.size() + 1;
598 }
599 
600 LLVM_DUMP_METHOD
601 void DIEInlineString::print(raw_ostream &O) const {
602   O << "InlineString: " << S;
603 }
604 
605 //===----------------------------------------------------------------------===//
606 // DIEEntry Implementation
607 //===----------------------------------------------------------------------===//
608 
609 /// EmitValue - Emit debug information entry offset.
610 ///
611 void DIEEntry::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
612 
613   switch (Form) {
614   case dwarf::DW_FORM_ref1:
615   case dwarf::DW_FORM_ref2:
616   case dwarf::DW_FORM_ref4:
617   case dwarf::DW_FORM_ref8:
618     AP->OutStreamer->EmitIntValue(Entry->getOffset(), SizeOf(AP, Form));
619     return;
620 
621   case dwarf::DW_FORM_ref_udata:
622     AP->EmitULEB128(Entry->getOffset());
623     return;
624 
625   case dwarf::DW_FORM_ref_addr: {
626     // Get the absolute offset for this DIE within the debug info/types section.
627     unsigned Addr = Entry->getDebugSectionOffset();
628     if (const MCSymbol *SectionSym =
629             Entry->getUnit()->getCrossSectionRelativeBaseAddress()) {
630       AP->EmitLabelPlusOffset(SectionSym, Addr, SizeOf(AP, Form), true);
631       return;
632     }
633 
634     AP->OutStreamer->EmitIntValue(Addr, SizeOf(AP, Form));
635     return;
636   }
637   default:
638     llvm_unreachable("Improper form for DIE reference");
639   }
640 }
641 
642 unsigned DIEEntry::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
643   switch (Form) {
644   case dwarf::DW_FORM_ref1:
645     return 1;
646   case dwarf::DW_FORM_ref2:
647     return 2;
648   case dwarf::DW_FORM_ref4:
649     return 4;
650   case dwarf::DW_FORM_ref8:
651     return 8;
652   case dwarf::DW_FORM_ref_udata:
653     return getULEB128Size(Entry->getOffset());
654   case dwarf::DW_FORM_ref_addr:
655     if (AP->getDwarfVersion() == 2)
656       return AP->MAI->getCodePointerSize();
657     switch (AP->OutStreamer->getContext().getDwarfFormat()) {
658     case dwarf::DWARF32:
659       return 4;
660     case dwarf::DWARF64:
661       return 8;
662     }
663     llvm_unreachable("Invalid DWARF format");
664 
665   default:
666     llvm_unreachable("Improper form for DIE reference");
667   }
668 }
669 
670 LLVM_DUMP_METHOD
671 void DIEEntry::print(raw_ostream &O) const {
672   O << format("Die: 0x%lx", (long)(intptr_t)&Entry);
673 }
674 
675 //===----------------------------------------------------------------------===//
676 // DIELoc Implementation
677 //===----------------------------------------------------------------------===//
678 
679 /// ComputeSize - calculate the size of the location expression.
680 ///
681 unsigned DIELoc::ComputeSize(const AsmPrinter *AP) const {
682   if (!Size) {
683     for (const auto &V : values())
684       Size += V.SizeOf(AP);
685   }
686 
687   return Size;
688 }
689 
690 /// EmitValue - Emit location data.
691 ///
692 void DIELoc::EmitValue(const AsmPrinter *Asm, dwarf::Form Form) const {
693   switch (Form) {
694   default: llvm_unreachable("Improper form for block");
695   case dwarf::DW_FORM_block1: Asm->emitInt8(Size);    break;
696   case dwarf::DW_FORM_block2: Asm->emitInt16(Size);   break;
697   case dwarf::DW_FORM_block4: Asm->emitInt32(Size);   break;
698   case dwarf::DW_FORM_block:
699   case dwarf::DW_FORM_exprloc:
700     Asm->EmitULEB128(Size); break;
701   }
702 
703   for (const auto &V : values())
704     V.EmitValue(Asm);
705 }
706 
707 /// SizeOf - Determine size of location data in bytes.
708 ///
709 unsigned DIELoc::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
710   switch (Form) {
711   case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
712   case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
713   case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
714   case dwarf::DW_FORM_block:
715   case dwarf::DW_FORM_exprloc:
716     return Size + getULEB128Size(Size);
717   default: llvm_unreachable("Improper form for block");
718   }
719 }
720 
721 LLVM_DUMP_METHOD
722 void DIELoc::print(raw_ostream &O) const {
723   printValues(O, *this, "ExprLoc", Size, 5);
724 }
725 
726 //===----------------------------------------------------------------------===//
727 // DIEBlock Implementation
728 //===----------------------------------------------------------------------===//
729 
730 /// ComputeSize - calculate the size of the block.
731 ///
732 unsigned DIEBlock::ComputeSize(const AsmPrinter *AP) const {
733   if (!Size) {
734     for (const auto &V : values())
735       Size += V.SizeOf(AP);
736   }
737 
738   return Size;
739 }
740 
741 /// EmitValue - Emit block data.
742 ///
743 void DIEBlock::EmitValue(const AsmPrinter *Asm, dwarf::Form Form) const {
744   switch (Form) {
745   default: llvm_unreachable("Improper form for block");
746   case dwarf::DW_FORM_block1: Asm->emitInt8(Size);    break;
747   case dwarf::DW_FORM_block2: Asm->emitInt16(Size);   break;
748   case dwarf::DW_FORM_block4: Asm->emitInt32(Size);   break;
749   case dwarf::DW_FORM_block:  Asm->EmitULEB128(Size); break;
750   case dwarf::DW_FORM_string: break;
751   case dwarf::DW_FORM_data16: break;
752   }
753 
754   for (const auto &V : values())
755     V.EmitValue(Asm);
756 }
757 
758 /// SizeOf - Determine size of block data in bytes.
759 ///
760 unsigned DIEBlock::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
761   switch (Form) {
762   case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
763   case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
764   case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
765   case dwarf::DW_FORM_block:  return Size + getULEB128Size(Size);
766   case dwarf::DW_FORM_data16: return 16;
767   default: llvm_unreachable("Improper form for block");
768   }
769 }
770 
771 LLVM_DUMP_METHOD
772 void DIEBlock::print(raw_ostream &O) const {
773   printValues(O, *this, "Blk", Size, 5);
774 }
775 
776 //===----------------------------------------------------------------------===//
777 // DIELocList Implementation
778 //===----------------------------------------------------------------------===//
779 
780 unsigned DIELocList::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
781   if (Form == dwarf::DW_FORM_data4)
782     return 4;
783   if (Form == dwarf::DW_FORM_sec_offset)
784     return 4;
785   return AP->MAI->getCodePointerSize();
786 }
787 
788 /// EmitValue - Emit label value.
789 ///
790 void DIELocList::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
791   DwarfDebug *DD = AP->getDwarfDebug();
792   MCSymbol *Label = DD->getDebugLocs().getList(Index).Label;
793   AP->emitDwarfSymbolReference(Label, /*ForceOffset*/ DD->useSplitDwarf());
794 }
795 
796 LLVM_DUMP_METHOD
797 void DIELocList::print(raw_ostream &O) const { O << "LocList: " << Index; }
798