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/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 //===----------------------------------------------------------------------===//
35 // DIEAbbrevData Implementation
36 //===----------------------------------------------------------------------===//
37 
38 /// Profile - Used to gather unique data for the abbreviation folding set.
39 ///
40 void DIEAbbrevData::Profile(FoldingSetNodeID &ID) const {
41   // Explicitly cast to an integer type for which FoldingSetNodeID has
42   // overloads.  Otherwise MSVC 2010 thinks this call is ambiguous.
43   ID.AddInteger(unsigned(Attribute));
44   ID.AddInteger(unsigned(Form));
45   if (Form == dwarf::DW_FORM_implicit_const)
46     ID.AddInteger(Value);
47 }
48 
49 //===----------------------------------------------------------------------===//
50 // DIEAbbrev Implementation
51 //===----------------------------------------------------------------------===//
52 
53 /// Profile - Used to gather unique data for the abbreviation folding set.
54 ///
55 void DIEAbbrev::Profile(FoldingSetNodeID &ID) const {
56   ID.AddInteger(unsigned(Tag));
57   ID.AddInteger(unsigned(Children));
58 
59   // For each attribute description.
60   for (unsigned i = 0, N = Data.size(); i < N; ++i)
61     Data[i].Profile(ID);
62 }
63 
64 /// Emit - Print the abbreviation using the specified asm printer.
65 ///
66 void DIEAbbrev::Emit(const AsmPrinter *AP) const {
67   // Emit its Dwarf tag type.
68   AP->EmitULEB128(Tag, dwarf::TagString(Tag).data());
69 
70   // Emit whether it has children DIEs.
71   AP->EmitULEB128((unsigned)Children, dwarf::ChildrenString(Children).data());
72 
73   // For each attribute description.
74   for (unsigned i = 0, N = Data.size(); i < N; ++i) {
75     const DIEAbbrevData &AttrData = Data[i];
76 
77     // Emit attribute type.
78     AP->EmitULEB128(AttrData.getAttribute(),
79                     dwarf::AttributeString(AttrData.getAttribute()).data());
80 
81     // Emit form type.
82     AP->EmitULEB128(AttrData.getForm(),
83                     dwarf::FormEncodingString(AttrData.getForm()).data());
84 
85     // Emit value for DW_FORM_implicit_const.
86     if (AttrData.getForm() == dwarf::DW_FORM_implicit_const) {
87       assert(AP->getDwarfVersion() >= 5 &&
88             "DW_FORM_implicit_const is supported starting from DWARFv5");
89       AP->EmitSLEB128(AttrData.getValue());
90     }
91   }
92 
93   // Mark end of abbreviation.
94   AP->EmitULEB128(0, "EOM(1)");
95   AP->EmitULEB128(0, "EOM(2)");
96 }
97 
98 LLVM_DUMP_METHOD
99 void DIEAbbrev::print(raw_ostream &O) {
100   O << "Abbreviation @"
101     << format("0x%lx", (long)(intptr_t)this)
102     << "  "
103     << dwarf::TagString(Tag)
104     << " "
105     << dwarf::ChildrenString(Children)
106     << '\n';
107 
108   for (unsigned i = 0, N = Data.size(); i < N; ++i) {
109     O << "  "
110       << dwarf::AttributeString(Data[i].getAttribute())
111       << "  "
112       << dwarf::FormEncodingString(Data[i].getForm());
113 
114     if (Data[i].getForm() == dwarf::DW_FORM_implicit_const)
115       O << " " << Data[i].getValue();
116 
117     O << '\n';
118   }
119 }
120 
121 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
122 LLVM_DUMP_METHOD void DIEAbbrev::dump() {
123   print(dbgs());
124 }
125 #endif
126 
127 //===----------------------------------------------------------------------===//
128 // DIEAbbrevSet Implementation
129 //===----------------------------------------------------------------------===//
130 
131 DIEAbbrevSet::~DIEAbbrevSet() {
132   for (DIEAbbrev *Abbrev : Abbreviations)
133     Abbrev->~DIEAbbrev();
134 }
135 
136 DIEAbbrev &DIEAbbrevSet::uniqueAbbreviation(DIE &Die) {
137 
138   FoldingSetNodeID ID;
139   DIEAbbrev Abbrev = Die.generateAbbrev();
140   Abbrev.Profile(ID);
141 
142   void *InsertPos;
143   if (DIEAbbrev *Existing =
144           AbbreviationsSet.FindNodeOrInsertPos(ID, InsertPos)) {
145     Die.setAbbrevNumber(Existing->getNumber());
146     return *Existing;
147   }
148 
149   // Move the abbreviation to the heap and assign a number.
150   DIEAbbrev *New = new (Alloc) DIEAbbrev(std::move(Abbrev));
151   Abbreviations.push_back(New);
152   New->setNumber(Abbreviations.size());
153   Die.setAbbrevNumber(Abbreviations.size());
154 
155   // Store it for lookup.
156   AbbreviationsSet.InsertNode(New, InsertPos);
157   return *New;
158 }
159 
160 void DIEAbbrevSet::Emit(const AsmPrinter *AP, MCSection *Section) const {
161   if (!Abbreviations.empty()) {
162     // Start the debug abbrev section.
163     AP->OutStreamer->SwitchSection(Section);
164     AP->emitDwarfAbbrevs(Abbreviations);
165   }
166 }
167 
168 //===----------------------------------------------------------------------===//
169 // DIE Implementation
170 //===----------------------------------------------------------------------===//
171 
172 DIE *DIE::getParent() const {
173   return Owner.dyn_cast<DIE*>();
174 }
175 
176 DIEAbbrev DIE::generateAbbrev() const {
177   DIEAbbrev Abbrev(Tag, hasChildren());
178   for (const DIEValue &V : values())
179     if (V.getForm() == dwarf::DW_FORM_implicit_const)
180       Abbrev.AddImplicitConstAttribute(V.getAttribute(),
181                                        V.getDIEInteger().getValue());
182     else
183       Abbrev.AddAttribute(V.getAttribute(), V.getForm());
184   return Abbrev;
185 }
186 
187 unsigned DIE::getDebugSectionOffset() const {
188   const DIEUnit *Unit = getUnit();
189   assert(Unit && "DIE must be owned by a DIEUnit to get its absolute offset");
190   return Unit->getDebugSectionOffset() + getOffset();
191 }
192 
193 const DIE *DIE::getUnitDie() const {
194   const DIE *p = this;
195   while (p) {
196     if (p->getTag() == dwarf::DW_TAG_compile_unit ||
197         p->getTag() == dwarf::DW_TAG_type_unit)
198       return p;
199     p = p->getParent();
200   }
201   return nullptr;
202 }
203 
204 const DIEUnit *DIE::getUnit() const {
205   const DIE *UnitDie = getUnitDie();
206   if (UnitDie)
207     return UnitDie->Owner.dyn_cast<DIEUnit*>();
208   return nullptr;
209 }
210 
211 DIEValue DIE::findAttribute(dwarf::Attribute Attribute) const {
212   // Iterate through all the attributes until we find the one we're
213   // looking for, if we can't find it return NULL.
214   for (const auto &V : values())
215     if (V.getAttribute() == Attribute)
216       return V;
217   return DIEValue();
218 }
219 
220 LLVM_DUMP_METHOD
221 static void printValues(raw_ostream &O, const DIEValueList &Values,
222                         StringRef Type, unsigned Size, unsigned IndentCount) {
223   O << Type << ": Size: " << Size << "\n";
224 
225   unsigned I = 0;
226   const std::string Indent(IndentCount, ' ');
227   for (const auto &V : Values.values()) {
228     O << Indent;
229     O << "Blk[" << I++ << "]";
230     O << "  " << dwarf::FormEncodingString(V.getForm()) << " ";
231     V.print(O);
232     O << "\n";
233   }
234 }
235 
236 LLVM_DUMP_METHOD
237 void DIE::print(raw_ostream &O, unsigned IndentCount) const {
238   const std::string Indent(IndentCount, ' ');
239   O << Indent << "Die: " << format("0x%lx", (long)(intptr_t) this)
240     << ", Offset: " << Offset << ", Size: " << Size << "\n";
241 
242   O << Indent << dwarf::TagString(getTag()) << " "
243     << dwarf::ChildrenString(hasChildren()) << "\n";
244 
245   IndentCount += 2;
246   for (const auto &V : values()) {
247     O << Indent;
248     O << dwarf::AttributeString(V.getAttribute());
249     O << "  " << dwarf::FormEncodingString(V.getForm()) << " ";
250     V.print(O);
251     O << "\n";
252   }
253   IndentCount -= 2;
254 
255   for (const auto &Child : children())
256     Child.print(O, IndentCount + 4);
257 
258   O << "\n";
259 }
260 
261 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
262 LLVM_DUMP_METHOD void DIE::dump() {
263   print(dbgs());
264 }
265 #endif
266 
267 unsigned DIE::computeOffsetsAndAbbrevs(const AsmPrinter *AP,
268                                        DIEAbbrevSet &AbbrevSet,
269                                        unsigned CUOffset) {
270   // Unique the abbreviation and fill in the abbreviation number so this DIE
271   // can be emitted.
272   const DIEAbbrev &Abbrev = AbbrevSet.uniqueAbbreviation(*this);
273 
274   // Set compile/type unit relative offset of this DIE.
275   setOffset(CUOffset);
276 
277   // Add the byte size of the abbreviation code.
278   CUOffset += getULEB128Size(getAbbrevNumber());
279 
280   // Add the byte size of all the DIE attribute values.
281   for (const auto &V : values())
282     CUOffset += V.SizeOf(AP);
283 
284   // Let the children compute their offsets and abbreviation numbers.
285   if (hasChildren()) {
286     (void)Abbrev;
287     assert(Abbrev.hasChildren() && "Children flag not set");
288 
289     for (auto &Child : children())
290       CUOffset = Child.computeOffsetsAndAbbrevs(AP, AbbrevSet, CUOffset);
291 
292     // Each child chain is terminated with a zero byte, adjust the offset.
293     CUOffset += sizeof(int8_t);
294   }
295 
296   // Compute the byte size of this DIE and all of its children correctly. This
297   // is needed so that top level DIE can help the compile unit set its length
298   // correctly.
299   setSize(CUOffset - getOffset());
300   return CUOffset;
301 }
302 
303 //===----------------------------------------------------------------------===//
304 // DIEUnit Implementation
305 //===----------------------------------------------------------------------===//
306 DIEUnit::DIEUnit(uint16_t V, uint8_t A, dwarf::Tag UnitTag)
307     : Die(UnitTag), Section(nullptr), Offset(0), Length(0), Version(V),
308       AddrSize(A)
309 {
310   Die.Owner = this;
311   assert((UnitTag == dwarf::DW_TAG_compile_unit ||
312           UnitTag == dwarf::DW_TAG_type_unit ||
313           UnitTag == dwarf::DW_TAG_partial_unit) && "expected a unit TAG");
314 }
315 
316 void DIEValue::EmitValue(const AsmPrinter *AP) const {
317   switch (Ty) {
318   case isNone:
319     llvm_unreachable("Expected valid DIEValue");
320 #define HANDLE_DIEVALUE(T)                                                     \
321   case is##T:                                                                  \
322     getDIE##T().EmitValue(AP, Form);                                           \
323     break;
324 #include "llvm/CodeGen/DIEValue.def"
325   }
326 }
327 
328 unsigned DIEValue::SizeOf(const AsmPrinter *AP) const {
329   switch (Ty) {
330   case isNone:
331     llvm_unreachable("Expected valid DIEValue");
332 #define HANDLE_DIEVALUE(T)                                                     \
333   case is##T:                                                                  \
334     return getDIE##T().SizeOf(AP, Form);
335 #include "llvm/CodeGen/DIEValue.def"
336   }
337   llvm_unreachable("Unknown DIE kind");
338 }
339 
340 LLVM_DUMP_METHOD
341 void DIEValue::print(raw_ostream &O) const {
342   switch (Ty) {
343   case isNone:
344     llvm_unreachable("Expected valid DIEValue");
345 #define HANDLE_DIEVALUE(T)                                                     \
346   case is##T:                                                                  \
347     getDIE##T().print(O);                                                      \
348     break;
349 #include "llvm/CodeGen/DIEValue.def"
350   }
351 }
352 
353 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
354 LLVM_DUMP_METHOD void DIEValue::dump() const {
355   print(dbgs());
356 }
357 #endif
358 
359 //===----------------------------------------------------------------------===//
360 // DIEInteger Implementation
361 //===----------------------------------------------------------------------===//
362 
363 /// EmitValue - Emit integer of appropriate size.
364 ///
365 void DIEInteger::EmitValue(const AsmPrinter *Asm, dwarf::Form Form) const {
366   switch (Form) {
367   case dwarf::DW_FORM_implicit_const:
368   case dwarf::DW_FORM_flag_present:
369     // Emit something to keep the lines and comments in sync.
370     // FIXME: Is there a better way to do this?
371     Asm->OutStreamer->AddBlankLine();
372     return;
373   case dwarf::DW_FORM_flag:
374   case dwarf::DW_FORM_ref1:
375   case dwarf::DW_FORM_data1:
376   case dwarf::DW_FORM_ref2:
377   case dwarf::DW_FORM_data2:
378   case dwarf::DW_FORM_strp:
379   case dwarf::DW_FORM_ref4:
380   case dwarf::DW_FORM_data4:
381   case dwarf::DW_FORM_ref8:
382   case dwarf::DW_FORM_ref_sig8:
383   case dwarf::DW_FORM_data8:
384   case dwarf::DW_FORM_GNU_ref_alt:
385   case dwarf::DW_FORM_GNU_strp_alt:
386   case dwarf::DW_FORM_line_strp:
387   case dwarf::DW_FORM_sec_offset:
388   case dwarf::DW_FORM_strp_sup:
389   case dwarf::DW_FORM_ref_sup:
390   case dwarf::DW_FORM_addr:
391   case dwarf::DW_FORM_ref_addr:
392     Asm->OutStreamer->EmitIntValue(Integer, SizeOf(Asm, Form));
393     return;
394   case dwarf::DW_FORM_GNU_str_index:
395   case dwarf::DW_FORM_GNU_addr_index:
396   case dwarf::DW_FORM_ref_udata:
397   case dwarf::DW_FORM_udata:
398     Asm->EmitULEB128(Integer);
399     return;
400   case dwarf::DW_FORM_sdata:
401     Asm->EmitSLEB128(Integer);
402     return;
403   default: llvm_unreachable("DIE Value form not supported yet");
404   }
405 }
406 
407 /// SizeOf - Determine size of integer value in bytes.
408 ///
409 unsigned DIEInteger::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
410   switch (Form) {
411   case dwarf::DW_FORM_implicit_const:
412   case dwarf::DW_FORM_flag_present:
413     return 0;
414   case dwarf::DW_FORM_flag:
415   case dwarf::DW_FORM_ref1:
416   case dwarf::DW_FORM_data1:
417     return sizeof(int8_t);
418   case dwarf::DW_FORM_ref2:
419   case dwarf::DW_FORM_data2:
420     return sizeof(int16_t);
421   case dwarf::DW_FORM_ref4:
422   case dwarf::DW_FORM_data4:
423     return sizeof(int32_t);
424   case dwarf::DW_FORM_ref8:
425   case dwarf::DW_FORM_ref_sig8:
426   case dwarf::DW_FORM_data8:
427     return sizeof(int64_t);
428   case dwarf::DW_FORM_ref_addr:
429     if (AP->getDwarfVersion() == 2)
430       return AP->getPointerSize();
431     LLVM_FALLTHROUGH;
432   case dwarf::DW_FORM_strp:
433   case dwarf::DW_FORM_GNU_ref_alt:
434   case dwarf::DW_FORM_GNU_strp_alt:
435   case dwarf::DW_FORM_line_strp:
436   case dwarf::DW_FORM_sec_offset:
437   case dwarf::DW_FORM_strp_sup:
438   case dwarf::DW_FORM_ref_sup:
439     switch (AP->OutStreamer->getContext().getDwarfFormat()) {
440     case dwarf::DWARF32:
441       return 4;
442     case dwarf::DWARF64:
443       return 8;
444     }
445     llvm_unreachable("Invalid DWARF format");
446   case dwarf::DW_FORM_GNU_str_index:
447   case dwarf::DW_FORM_GNU_addr_index:
448   case dwarf::DW_FORM_ref_udata:
449   case dwarf::DW_FORM_udata:
450     return getULEB128Size(Integer);
451   case dwarf::DW_FORM_sdata:
452     return getSLEB128Size(Integer);
453   case dwarf::DW_FORM_addr:
454     return AP->getPointerSize();
455   default: llvm_unreachable("DIE Value form not supported yet");
456   }
457 }
458 
459 LLVM_DUMP_METHOD
460 void DIEInteger::print(raw_ostream &O) const {
461   O << "Int: " << (int64_t)Integer << "  0x";
462   O.write_hex(Integer);
463 }
464 
465 //===----------------------------------------------------------------------===//
466 // DIEExpr Implementation
467 //===----------------------------------------------------------------------===//
468 
469 /// EmitValue - Emit expression value.
470 ///
471 void DIEExpr::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
472   AP->EmitDebugThreadLocal(Expr, SizeOf(AP, Form));
473 }
474 
475 /// SizeOf - Determine size of expression value in bytes.
476 ///
477 unsigned DIEExpr::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
478   if (Form == dwarf::DW_FORM_data4) return 4;
479   if (Form == dwarf::DW_FORM_sec_offset) return 4;
480   if (Form == dwarf::DW_FORM_strp) return 4;
481   return AP->getPointerSize();
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   AP->EmitLabelReference(Label, SizeOf(AP, Form),
495                          Form == dwarf::DW_FORM_strp ||
496                              Form == dwarf::DW_FORM_sec_offset ||
497                              Form == dwarf::DW_FORM_ref_addr ||
498                              Form == dwarf::DW_FORM_data4);
499 }
500 
501 /// SizeOf - Determine size of label value in bytes.
502 ///
503 unsigned DIELabel::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
504   if (Form == dwarf::DW_FORM_data4) return 4;
505   if (Form == dwarf::DW_FORM_sec_offset) return 4;
506   if (Form == dwarf::DW_FORM_strp) return 4;
507   return AP->getPointerSize();
508 }
509 
510 LLVM_DUMP_METHOD
511 void DIELabel::print(raw_ostream &O) const { O << "Lbl: " << Label->getName(); }
512 
513 //===----------------------------------------------------------------------===//
514 // DIEDelta Implementation
515 //===----------------------------------------------------------------------===//
516 
517 /// EmitValue - Emit delta value.
518 ///
519 void DIEDelta::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
520   AP->EmitLabelDifference(LabelHi, LabelLo, SizeOf(AP, Form));
521 }
522 
523 /// SizeOf - Determine size of delta value in bytes.
524 ///
525 unsigned DIEDelta::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
526   if (Form == dwarf::DW_FORM_data4) return 4;
527   if (Form == dwarf::DW_FORM_sec_offset) return 4;
528   if (Form == dwarf::DW_FORM_strp) return 4;
529   return AP->getPointerSize();
530 }
531 
532 LLVM_DUMP_METHOD
533 void DIEDelta::print(raw_ostream &O) const {
534   O << "Del: " << LabelHi->getName() << "-" << LabelLo->getName();
535 }
536 
537 //===----------------------------------------------------------------------===//
538 // DIEString Implementation
539 //===----------------------------------------------------------------------===//
540 
541 /// EmitValue - Emit string value.
542 ///
543 void DIEString::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
544   assert(
545       (Form == dwarf::DW_FORM_strp || Form == dwarf::DW_FORM_GNU_str_index) &&
546       "Expected valid string form");
547 
548   // Index of string in symbol table.
549   if (Form == dwarf::DW_FORM_GNU_str_index) {
550     DIEInteger(S.getIndex()).EmitValue(AP, Form);
551     return;
552   }
553 
554   // Relocatable symbol.
555   assert(Form == dwarf::DW_FORM_strp);
556   if (AP->MAI->doesDwarfUseRelocationsAcrossSections()) {
557     DIELabel(S.getSymbol()).EmitValue(AP, Form);
558     return;
559   }
560 
561   // Offset into symbol table.
562   DIEInteger(S.getOffset()).EmitValue(AP, Form);
563 }
564 
565 /// SizeOf - Determine size of delta value in bytes.
566 ///
567 unsigned DIEString::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
568   assert(
569       (Form == dwarf::DW_FORM_strp || Form == dwarf::DW_FORM_GNU_str_index) &&
570       "Expected valid string form");
571 
572   // Index of string in symbol table.
573   if (Form == dwarf::DW_FORM_GNU_str_index)
574     return DIEInteger(S.getIndex()).SizeOf(AP, Form);
575 
576   // Relocatable symbol.
577   if (AP->MAI->doesDwarfUseRelocationsAcrossSections())
578     return DIELabel(S.getSymbol()).SizeOf(AP, Form);
579 
580   // Offset into symbol table.
581   return DIEInteger(S.getOffset()).SizeOf(AP, Form);
582 }
583 
584 LLVM_DUMP_METHOD
585 void DIEString::print(raw_ostream &O) const {
586   O << "String: " << S.getString();
587 }
588 
589 //===----------------------------------------------------------------------===//
590 // DIEInlineString Implementation
591 //===----------------------------------------------------------------------===//
592 void DIEInlineString::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
593   if (Form == dwarf::DW_FORM_string) {
594     for (char ch : S)
595       AP->EmitInt8(ch);
596     AP->EmitInt8(0);
597     return;
598   }
599   llvm_unreachable("Expected valid string form");
600 }
601 
602 unsigned DIEInlineString::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
603   // Emit string bytes + NULL byte.
604   return S.size() + 1;
605 }
606 
607 LLVM_DUMP_METHOD
608 void DIEInlineString::print(raw_ostream &O) const {
609   O << "InlineString: " << S;
610 }
611 
612 //===----------------------------------------------------------------------===//
613 // DIEEntry Implementation
614 //===----------------------------------------------------------------------===//
615 
616 /// EmitValue - Emit debug information entry offset.
617 ///
618 void DIEEntry::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
619 
620   switch (Form) {
621   case dwarf::DW_FORM_ref1:
622   case dwarf::DW_FORM_ref2:
623   case dwarf::DW_FORM_ref4:
624   case dwarf::DW_FORM_ref8:
625     AP->OutStreamer->EmitIntValue(Entry->getOffset(), SizeOf(AP, Form));
626     return;
627 
628   case dwarf::DW_FORM_ref_udata:
629     AP->EmitULEB128(Entry->getOffset());
630     return;
631 
632   case dwarf::DW_FORM_ref_addr: {
633     // Get the absolute offset for this DIE within the debug info/types section.
634     unsigned Addr = Entry->getDebugSectionOffset();
635     if (AP->MAI->doesDwarfUseRelocationsAcrossSections()) {
636       const DwarfDebug *DD = AP->getDwarfDebug();
637       if (DD)
638         assert(!DD->useSplitDwarf() &&
639                "TODO: dwo files can't have relocations.");
640       const DIEUnit *Unit = Entry->getUnit();
641       assert(Unit && "CUDie should belong to a CU.");
642       MCSection *Section = Unit->getSection();
643       if (Section) {
644         const MCSymbol *SectionSym = Section->getBeginSymbol();
645         AP->EmitLabelPlusOffset(SectionSym, Addr, SizeOf(AP, Form), true);
646         return;
647       }
648     }
649     AP->OutStreamer->EmitIntValue(Addr, SizeOf(AP, Form));
650     return;
651   }
652   default:
653     llvm_unreachable("Improper form for DIE reference");
654   }
655 }
656 
657 unsigned DIEEntry::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
658   switch (Form) {
659   case dwarf::DW_FORM_ref1:
660     return 1;
661   case dwarf::DW_FORM_ref2:
662     return 2;
663   case dwarf::DW_FORM_ref4:
664     return 4;
665   case dwarf::DW_FORM_ref8:
666     return 8;
667   case dwarf::DW_FORM_ref_udata:
668     return getULEB128Size(Entry->getOffset());
669   case dwarf::DW_FORM_ref_addr:
670     if (AP->getDwarfVersion() == 2)
671       return AP->getPointerSize();
672     switch (AP->OutStreamer->getContext().getDwarfFormat()) {
673     case dwarf::DWARF32:
674       return 4;
675     case dwarf::DWARF64:
676       return 8;
677     }
678     llvm_unreachable("Invalid DWARF format");
679 
680   default:
681     llvm_unreachable("Improper form for DIE reference");
682   }
683 }
684 
685 LLVM_DUMP_METHOD
686 void DIEEntry::print(raw_ostream &O) const {
687   O << format("Die: 0x%lx", (long)(intptr_t)&Entry);
688 }
689 
690 //===----------------------------------------------------------------------===//
691 // DIELoc Implementation
692 //===----------------------------------------------------------------------===//
693 
694 /// ComputeSize - calculate the size of the location expression.
695 ///
696 unsigned DIELoc::ComputeSize(const AsmPrinter *AP) const {
697   if (!Size) {
698     for (const auto &V : values())
699       Size += V.SizeOf(AP);
700   }
701 
702   return Size;
703 }
704 
705 /// EmitValue - Emit location data.
706 ///
707 void DIELoc::EmitValue(const AsmPrinter *Asm, dwarf::Form Form) const {
708   switch (Form) {
709   default: llvm_unreachable("Improper form for block");
710   case dwarf::DW_FORM_block1: Asm->EmitInt8(Size);    break;
711   case dwarf::DW_FORM_block2: Asm->EmitInt16(Size);   break;
712   case dwarf::DW_FORM_block4: Asm->EmitInt32(Size);   break;
713   case dwarf::DW_FORM_block:
714   case dwarf::DW_FORM_exprloc:
715     Asm->EmitULEB128(Size); break;
716   }
717 
718   for (const auto &V : values())
719     V.EmitValue(Asm);
720 }
721 
722 /// SizeOf - Determine size of location data in bytes.
723 ///
724 unsigned DIELoc::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
725   switch (Form) {
726   case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
727   case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
728   case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
729   case dwarf::DW_FORM_block:
730   case dwarf::DW_FORM_exprloc:
731     return Size + getULEB128Size(Size);
732   default: llvm_unreachable("Improper form for block");
733   }
734 }
735 
736 LLVM_DUMP_METHOD
737 void DIELoc::print(raw_ostream &O) const {
738   printValues(O, *this, "ExprLoc", Size, 5);
739 }
740 
741 //===----------------------------------------------------------------------===//
742 // DIEBlock Implementation
743 //===----------------------------------------------------------------------===//
744 
745 /// ComputeSize - calculate the size of the block.
746 ///
747 unsigned DIEBlock::ComputeSize(const AsmPrinter *AP) const {
748   if (!Size) {
749     for (const auto &V : values())
750       Size += V.SizeOf(AP);
751   }
752 
753   return Size;
754 }
755 
756 /// EmitValue - Emit block data.
757 ///
758 void DIEBlock::EmitValue(const AsmPrinter *Asm, dwarf::Form Form) const {
759   switch (Form) {
760   default: llvm_unreachable("Improper form for block");
761   case dwarf::DW_FORM_block1: Asm->EmitInt8(Size);    break;
762   case dwarf::DW_FORM_block2: Asm->EmitInt16(Size);   break;
763   case dwarf::DW_FORM_block4: Asm->EmitInt32(Size);   break;
764   case dwarf::DW_FORM_block:  Asm->EmitULEB128(Size); break;
765   }
766 
767   for (const auto &V : values())
768     V.EmitValue(Asm);
769 }
770 
771 /// SizeOf - Determine size of block data in bytes.
772 ///
773 unsigned DIEBlock::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
774   switch (Form) {
775   case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
776   case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
777   case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
778   case dwarf::DW_FORM_block:  return Size + getULEB128Size(Size);
779   default: llvm_unreachable("Improper form for block");
780   }
781 }
782 
783 LLVM_DUMP_METHOD
784 void DIEBlock::print(raw_ostream &O) const {
785   printValues(O, *this, "Blk", Size, 5);
786 }
787 
788 //===----------------------------------------------------------------------===//
789 // DIELocList Implementation
790 //===----------------------------------------------------------------------===//
791 
792 unsigned DIELocList::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
793   if (Form == dwarf::DW_FORM_data4)
794     return 4;
795   if (Form == dwarf::DW_FORM_sec_offset)
796     return 4;
797   return AP->getPointerSize();
798 }
799 
800 /// EmitValue - Emit label value.
801 ///
802 void DIELocList::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
803   DwarfDebug *DD = AP->getDwarfDebug();
804   MCSymbol *Label = DD->getDebugLocs().getList(Index).Label;
805   AP->emitDwarfSymbolReference(Label, /*ForceOffset*/ DD->useSplitDwarf());
806 }
807 
808 LLVM_DUMP_METHOD
809 void DIELocList::print(raw_ostream &O) const { O << "LocList: " << Index; }
810