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"
32using namespace llvm;
33
34//===----------------------------------------------------------------------===//
35// DIEAbbrevData Implementation
36//===----------------------------------------------------------------------===//
37
38/// Profile - Used to gather unique data for the abbreviation folding set.
39///
40void 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}
46
47//===----------------------------------------------------------------------===//
48// DIEAbbrev Implementation
49//===----------------------------------------------------------------------===//
50
51/// Profile - Used to gather unique data for the abbreviation folding set.
52///
53void DIEAbbrev::Profile(FoldingSetNodeID &ID) const {
54  ID.AddInteger(unsigned(Tag));
55  ID.AddInteger(unsigned(Children));
56
57  // For each attribute description.
58  for (unsigned i = 0, N = Data.size(); i < N; ++i)
59    Data[i].Profile(ID);
60}
61
62/// Emit - Print the abbreviation using the specified asm printer.
63///
64void DIEAbbrev::Emit(const AsmPrinter *AP) const {
65  // Emit its Dwarf tag type.
66  AP->EmitULEB128(Tag, dwarf::TagString(Tag));
67
68  // Emit whether it has children DIEs.
69  AP->EmitULEB128((unsigned)Children, dwarf::ChildrenString(Children));
70
71  // For each attribute description.
72  for (unsigned i = 0, N = Data.size(); i < N; ++i) {
73    const DIEAbbrevData &AttrData = Data[i];
74
75    // Emit attribute type.
76    AP->EmitULEB128(AttrData.getAttribute(),
77                    dwarf::AttributeString(AttrData.getAttribute()));
78
79    // Emit form type.
80    AP->EmitULEB128(AttrData.getForm(),
81                    dwarf::FormEncodingString(AttrData.getForm()));
82  }
83
84  // Mark end of abbreviation.
85  AP->EmitULEB128(0, "EOM(1)");
86  AP->EmitULEB128(0, "EOM(2)");
87}
88
89#ifndef NDEBUG
90void DIEAbbrev::print(raw_ostream &O) {
91  O << "Abbreviation @"
92    << format("0x%lx", (long)(intptr_t)this)
93    << "  "
94    << dwarf::TagString(Tag)
95    << " "
96    << dwarf::ChildrenString(Children)
97    << '\n';
98
99  for (unsigned i = 0, N = Data.size(); i < N; ++i) {
100    O << "  "
101      << dwarf::AttributeString(Data[i].getAttribute())
102      << "  "
103      << dwarf::FormEncodingString(Data[i].getForm())
104      << '\n';
105  }
106}
107void DIEAbbrev::dump() { print(dbgs()); }
108#endif
109
110/// Climb up the parent chain to get the unit DIE to which this DIE
111/// belongs.
112const DIE *DIE::getUnit() const {
113  const DIE *Cu = getUnitOrNull();
114  assert(Cu && "We should not have orphaned DIEs.");
115  return Cu;
116}
117
118/// Climb up the parent chain to get the unit DIE this DIE belongs
119/// to. Return NULL if DIE is not added to an owner yet.
120const DIE *DIE::getUnitOrNull() const {
121  const DIE *p = this;
122  while (p) {
123    if (p->getTag() == dwarf::DW_TAG_compile_unit ||
124        p->getTag() == dwarf::DW_TAG_type_unit)
125      return p;
126    p = p->getParent();
127  }
128  return nullptr;
129}
130
131DIEValue *DIE::findAttribute(dwarf::Attribute Attribute) const {
132  const SmallVectorImpl<DIEValue *> &Values = getValues();
133  const DIEAbbrev &Abbrevs = getAbbrev();
134
135  // Iterate through all the attributes until we find the one we're
136  // looking for, if we can't find it return NULL.
137  for (size_t i = 0; i < Values.size(); ++i)
138    if (Abbrevs.getData()[i].getAttribute() == Attribute)
139      return Values[i];
140  return nullptr;
141}
142
143#ifndef NDEBUG
144void DIE::print(raw_ostream &O, unsigned IndentCount) const {
145  const std::string Indent(IndentCount, ' ');
146  bool isBlock = Abbrev.getTag() == 0;
147
148  if (!isBlock) {
149    O << Indent
150      << "Die: "
151      << format("0x%lx", (long)(intptr_t)this)
152      << ", Offset: " << Offset
153      << ", Size: " << Size << "\n";
154
155    O << Indent
156      << dwarf::TagString(Abbrev.getTag())
157      << " "
158      << dwarf::ChildrenString(Abbrev.hasChildren()) << "\n";
159  } else {
160    O << "Size: " << Size << "\n";
161  }
162
163  const SmallVectorImpl<DIEAbbrevData> &Data = Abbrev.getData();
164
165  IndentCount += 2;
166  for (unsigned i = 0, N = Data.size(); i < N; ++i) {
167    O << Indent;
168
169    if (!isBlock)
170      O << dwarf::AttributeString(Data[i].getAttribute());
171    else
172      O << "Blk[" << i << "]";
173
174    O <<  "  "
175      << dwarf::FormEncodingString(Data[i].getForm())
176      << " ";
177    Values[i]->print(O);
178    O << "\n";
179  }
180  IndentCount -= 2;
181
182  for (unsigned j = 0, M = Children.size(); j < M; ++j) {
183    Children[j]->print(O, IndentCount+4);
184  }
185
186  if (!isBlock) O << "\n";
187}
188
189void DIE::dump() {
190  print(dbgs());
191}
192#endif
193
194void DIEValue::anchor() { }
195
196#ifndef NDEBUG
197void DIEValue::dump() const {
198  print(dbgs());
199}
200#endif
201
202//===----------------------------------------------------------------------===//
203// DIEInteger Implementation
204//===----------------------------------------------------------------------===//
205
206/// EmitValue - Emit integer of appropriate size.
207///
208void DIEInteger::EmitValue(const AsmPrinter *Asm, dwarf::Form Form) const {
209  unsigned Size = ~0U;
210  switch (Form) {
211  case dwarf::DW_FORM_flag_present:
212    // Emit something to keep the lines and comments in sync.
213    // FIXME: Is there a better way to do this?
214    Asm->OutStreamer.AddBlankLine();
215    return;
216  case dwarf::DW_FORM_flag:  // Fall thru
217  case dwarf::DW_FORM_ref1:  // Fall thru
218  case dwarf::DW_FORM_data1: Size = 1; break;
219  case dwarf::DW_FORM_ref2:  // Fall thru
220  case dwarf::DW_FORM_data2: Size = 2; break;
221  case dwarf::DW_FORM_sec_offset: // Fall thru
222  case dwarf::DW_FORM_strp: // Fall thru
223  case dwarf::DW_FORM_ref4:  // Fall thru
224  case dwarf::DW_FORM_data4: Size = 4; break;
225  case dwarf::DW_FORM_ref8:  // Fall thru
226  case dwarf::DW_FORM_ref_sig8:  // Fall thru
227  case dwarf::DW_FORM_data8: Size = 8; break;
228  case dwarf::DW_FORM_GNU_str_index: Asm->EmitULEB128(Integer); return;
229  case dwarf::DW_FORM_GNU_addr_index: Asm->EmitULEB128(Integer); return;
230  case dwarf::DW_FORM_udata: Asm->EmitULEB128(Integer); return;
231  case dwarf::DW_FORM_sdata: Asm->EmitSLEB128(Integer); return;
232  case dwarf::DW_FORM_addr:
233    Size = Asm->getDataLayout().getPointerSize(); break;
234  case dwarf::DW_FORM_ref_addr:
235    Size = SizeOf(Asm, dwarf::DW_FORM_ref_addr);
236    break;
237  default: llvm_unreachable("DIE Value form not supported yet");
238  }
239  Asm->OutStreamer.EmitIntValue(Integer, Size);
240}
241
242/// SizeOf - Determine size of integer value in bytes.
243///
244unsigned DIEInteger::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
245  switch (Form) {
246  case dwarf::DW_FORM_flag_present: return 0;
247  case dwarf::DW_FORM_flag:  // Fall thru
248  case dwarf::DW_FORM_ref1:  // Fall thru
249  case dwarf::DW_FORM_data1: return sizeof(int8_t);
250  case dwarf::DW_FORM_ref2:  // Fall thru
251  case dwarf::DW_FORM_data2: return sizeof(int16_t);
252  case dwarf::DW_FORM_sec_offset: // Fall thru
253  case dwarf::DW_FORM_strp: // Fall thru
254  case dwarf::DW_FORM_ref4:  // Fall thru
255  case dwarf::DW_FORM_data4: return sizeof(int32_t);
256  case dwarf::DW_FORM_ref8:  // Fall thru
257  case dwarf::DW_FORM_ref_sig8:  // Fall thru
258  case dwarf::DW_FORM_data8: return sizeof(int64_t);
259  case dwarf::DW_FORM_GNU_str_index: return getULEB128Size(Integer);
260  case dwarf::DW_FORM_GNU_addr_index: return getULEB128Size(Integer);
261  case dwarf::DW_FORM_udata: return getULEB128Size(Integer);
262  case dwarf::DW_FORM_sdata: return getSLEB128Size(Integer);
263  case dwarf::DW_FORM_addr:  return AP->getDataLayout().getPointerSize();
264  case dwarf::DW_FORM_ref_addr:
265    if (AP->OutStreamer.getContext().getDwarfVersion() == 2)
266      return AP->getDataLayout().getPointerSize();
267    return sizeof(int32_t);
268  default: llvm_unreachable("DIE Value form not supported yet");
269  }
270}
271
272#ifndef NDEBUG
273void DIEInteger::print(raw_ostream &O) const {
274  O << "Int: " << (int64_t)Integer << "  0x";
275  O.write_hex(Integer);
276}
277#endif
278
279//===----------------------------------------------------------------------===//
280// DIEExpr Implementation
281//===----------------------------------------------------------------------===//
282
283/// EmitValue - Emit expression value.
284///
285void DIEExpr::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
286  AP->OutStreamer.EmitValue(Expr, SizeOf(AP, Form));
287}
288
289/// SizeOf - Determine size of expression value in bytes.
290///
291unsigned DIEExpr::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
292  if (Form == dwarf::DW_FORM_data4) return 4;
293  if (Form == dwarf::DW_FORM_sec_offset) return 4;
294  if (Form == dwarf::DW_FORM_strp) return 4;
295  return AP->getDataLayout().getPointerSize();
296}
297
298#ifndef NDEBUG
299void DIEExpr::print(raw_ostream &O) const {
300  O << "Expr: ";
301  Expr->print(O);
302}
303#endif
304
305//===----------------------------------------------------------------------===//
306// DIELabel Implementation
307//===----------------------------------------------------------------------===//
308
309/// EmitValue - Emit label value.
310///
311void DIELabel::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
312  AP->EmitLabelReference(Label, SizeOf(AP, Form),
313                         Form == dwarf::DW_FORM_strp ||
314                             Form == dwarf::DW_FORM_sec_offset ||
315                             Form == dwarf::DW_FORM_ref_addr);
316}
317
318/// SizeOf - Determine size of label value in bytes.
319///
320unsigned DIELabel::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
321  if (Form == dwarf::DW_FORM_data4) return 4;
322  if (Form == dwarf::DW_FORM_sec_offset) return 4;
323  if (Form == dwarf::DW_FORM_strp) return 4;
324  return AP->getDataLayout().getPointerSize();
325}
326
327#ifndef NDEBUG
328void DIELabel::print(raw_ostream &O) const {
329  O << "Lbl: " << Label->getName();
330}
331#endif
332
333//===----------------------------------------------------------------------===//
334// DIEDelta Implementation
335//===----------------------------------------------------------------------===//
336
337/// EmitValue - Emit delta value.
338///
339void DIEDelta::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
340  AP->EmitLabelDifference(LabelHi, LabelLo, SizeOf(AP, Form));
341}
342
343/// SizeOf - Determine size of delta value in bytes.
344///
345unsigned DIEDelta::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
346  if (Form == dwarf::DW_FORM_data4) return 4;
347  if (Form == dwarf::DW_FORM_sec_offset) return 4;
348  if (Form == dwarf::DW_FORM_strp) return 4;
349  return AP->getDataLayout().getPointerSize();
350}
351
352#ifndef NDEBUG
353void DIEDelta::print(raw_ostream &O) const {
354  O << "Del: " << LabelHi->getName() << "-" << LabelLo->getName();
355}
356#endif
357
358//===----------------------------------------------------------------------===//
359// DIEString Implementation
360//===----------------------------------------------------------------------===//
361
362/// EmitValue - Emit string value.
363///
364void DIEString::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
365  Access->EmitValue(AP, Form);
366}
367
368/// SizeOf - Determine size of delta value in bytes.
369///
370unsigned DIEString::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
371  return Access->SizeOf(AP, Form);
372}
373
374#ifndef NDEBUG
375void DIEString::print(raw_ostream &O) const {
376  O << "String: " << Str << "\tSymbol: ";
377  Access->print(O);
378}
379#endif
380
381//===----------------------------------------------------------------------===//
382// DIEEntry Implementation
383//===----------------------------------------------------------------------===//
384
385/// EmitValue - Emit debug information entry offset.
386///
387void DIEEntry::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
388
389  if (Form == dwarf::DW_FORM_ref_addr) {
390    const DwarfDebug *DD = AP->getDwarfDebug();
391    unsigned Addr = Entry.getOffset();
392    assert(!DD->useSplitDwarf() && "TODO: dwo files can't have relocations.");
393    // For DW_FORM_ref_addr, output the offset from beginning of debug info
394    // section. Entry->getOffset() returns the offset from start of the
395    // compile unit.
396    DwarfCompileUnit *CU = DD->lookupUnit(Entry.getUnit());
397    assert(CU && "CUDie should belong to a CU.");
398    Addr += CU->getDebugInfoOffset();
399    if (AP->MAI->doesDwarfUseRelocationsAcrossSections())
400      AP->EmitLabelPlusOffset(CU->getSectionSym(), Addr,
401                              DIEEntry::getRefAddrSize(AP));
402    else
403      AP->OutStreamer.EmitIntValue(Addr, DIEEntry::getRefAddrSize(AP));
404  } else
405    AP->EmitInt32(Entry.getOffset());
406}
407
408unsigned DIEEntry::getRefAddrSize(const AsmPrinter *AP) {
409  // DWARF4: References that use the attribute form DW_FORM_ref_addr are
410  // specified to be four bytes in the DWARF 32-bit format and eight bytes
411  // in the DWARF 64-bit format, while DWARF Version 2 specifies that such
412  // references have the same size as an address on the target system.
413  const DwarfDebug *DD = AP->getDwarfDebug();
414  assert(DD && "Expected Dwarf Debug info to be available");
415  if (DD->getDwarfVersion() == 2)
416    return AP->getDataLayout().getPointerSize();
417  return sizeof(int32_t);
418}
419
420#ifndef NDEBUG
421void DIEEntry::print(raw_ostream &O) const {
422  O << format("Die: 0x%lx", (long)(intptr_t)&Entry);
423}
424#endif
425
426//===----------------------------------------------------------------------===//
427// DIETypeSignature Implementation
428//===----------------------------------------------------------------------===//
429void DIETypeSignature::EmitValue(const AsmPrinter *Asm, dwarf::Form Form) const {
430  assert(Form == dwarf::DW_FORM_ref_sig8);
431  Asm->OutStreamer.EmitIntValue(Unit.getTypeSignature(), 8);
432}
433
434#ifndef NDEBUG
435void DIETypeSignature::print(raw_ostream &O) const {
436  O << format("Type Unit: 0x%lx", Unit.getTypeSignature());
437}
438
439void DIETypeSignature::dump() const { print(dbgs()); }
440#endif
441
442//===----------------------------------------------------------------------===//
443// DIELoc Implementation
444//===----------------------------------------------------------------------===//
445
446/// ComputeSize - calculate the size of the location expression.
447///
448unsigned DIELoc::ComputeSize(const AsmPrinter *AP) const {
449  if (!Size) {
450    const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
451    for (unsigned i = 0, N = Values.size(); i < N; ++i)
452      Size += Values[i]->SizeOf(AP, AbbrevData[i].getForm());
453  }
454
455  return Size;
456}
457
458/// EmitValue - Emit location data.
459///
460void DIELoc::EmitValue(const AsmPrinter *Asm, dwarf::Form Form) const {
461  switch (Form) {
462  default: llvm_unreachable("Improper form for block");
463  case dwarf::DW_FORM_block1: Asm->EmitInt8(Size);    break;
464  case dwarf::DW_FORM_block2: Asm->EmitInt16(Size);   break;
465  case dwarf::DW_FORM_block4: Asm->EmitInt32(Size);   break;
466  case dwarf::DW_FORM_block:
467  case dwarf::DW_FORM_exprloc:
468    Asm->EmitULEB128(Size); break;
469  }
470
471  const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
472  for (unsigned i = 0, N = Values.size(); i < N; ++i)
473    Values[i]->EmitValue(Asm, AbbrevData[i].getForm());
474}
475
476/// SizeOf - Determine size of location data in bytes.
477///
478unsigned DIELoc::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
479  switch (Form) {
480  case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
481  case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
482  case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
483  case dwarf::DW_FORM_block:
484  case dwarf::DW_FORM_exprloc:
485    return Size + getULEB128Size(Size);
486  default: llvm_unreachable("Improper form for block");
487  }
488}
489
490#ifndef NDEBUG
491void DIELoc::print(raw_ostream &O) const {
492  O << "ExprLoc: ";
493  DIE::print(O, 5);
494}
495#endif
496
497//===----------------------------------------------------------------------===//
498// DIEBlock Implementation
499//===----------------------------------------------------------------------===//
500
501/// ComputeSize - calculate the size of the block.
502///
503unsigned DIEBlock::ComputeSize(const AsmPrinter *AP) const {
504  if (!Size) {
505    const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
506    for (unsigned i = 0, N = Values.size(); i < N; ++i)
507      Size += Values[i]->SizeOf(AP, AbbrevData[i].getForm());
508  }
509
510  return Size;
511}
512
513/// EmitValue - Emit block data.
514///
515void DIEBlock::EmitValue(const AsmPrinter *Asm, dwarf::Form Form) const {
516  switch (Form) {
517  default: llvm_unreachable("Improper form for block");
518  case dwarf::DW_FORM_block1: Asm->EmitInt8(Size);    break;
519  case dwarf::DW_FORM_block2: Asm->EmitInt16(Size);   break;
520  case dwarf::DW_FORM_block4: Asm->EmitInt32(Size);   break;
521  case dwarf::DW_FORM_block:  Asm->EmitULEB128(Size); break;
522  }
523
524  const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
525  for (unsigned i = 0, N = Values.size(); i < N; ++i)
526    Values[i]->EmitValue(Asm, AbbrevData[i].getForm());
527}
528
529/// SizeOf - Determine size of block data in bytes.
530///
531unsigned DIEBlock::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
532  switch (Form) {
533  case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
534  case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
535  case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
536  case dwarf::DW_FORM_block:  return Size + getULEB128Size(Size);
537  default: llvm_unreachable("Improper form for block");
538  }
539}
540
541#ifndef NDEBUG
542void DIEBlock::print(raw_ostream &O) const {
543  O << "Blk: ";
544  DIE::print(O, 5);
545}
546#endif
547
548//===----------------------------------------------------------------------===//
549// DIELocList Implementation
550//===----------------------------------------------------------------------===//
551
552unsigned DIELocList::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
553  if (Form == dwarf::DW_FORM_data4)
554    return 4;
555  if (Form == dwarf::DW_FORM_sec_offset)
556    return 4;
557  return AP->getDataLayout().getPointerSize();
558}
559
560/// EmitValue - Emit label value.
561///
562void DIELocList::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
563  DwarfDebug *DD = AP->getDwarfDebug();
564  MCSymbol *Label = DD->getDebugLocEntries()[Index].Label;
565
566  if (AP->MAI->doesDwarfUseRelocationsAcrossSections() && !DD->useSplitDwarf())
567    AP->emitSectionOffset(Label);
568  else
569    AP->EmitLabelDifference(Label, Label->getSection().getBeginSymbol(), 4);
570}
571
572#ifndef NDEBUG
573void DIELocList::print(raw_ostream &O) const {
574  O << "LocList: " << Index;
575
576}
577#endif
578