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