1 //===--- lib/CodeGen/DIE.cpp - DWARF Info Entries -------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Data structures for DWARF info entries.
12 //===----------------------------------------------------------------------===//
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"
34 //===----------------------------------------------------------------------===//
35 // DIEAbbrevData Implementation
36 //===----------------------------------------------------------------------===//
38 /// Profile - Used to gather unique data for the abbreviation folding set.
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));
47 //===----------------------------------------------------------------------===//
48 // DIEAbbrev Implementation
49 //===----------------------------------------------------------------------===//
51 /// Profile - Used to gather unique data for the abbreviation folding set.
53 void DIEAbbrev::Profile(FoldingSetNodeID &ID) const {
54 ID.AddInteger(unsigned(Tag));
55 ID.AddInteger(unsigned(Children));
57 // For each attribute description.
58 for (unsigned i = 0, N = Data.size(); i < N; ++i)
62 /// Emit - Print the abbreviation using the specified asm printer.
64 void DIEAbbrev::Emit(const AsmPrinter *AP) const {
65 // Emit its Dwarf tag type.
66 AP->EmitULEB128(Tag, dwarf::TagString(Tag));
68 // Emit whether it has children DIEs.
69 AP->EmitULEB128((unsigned)Children, dwarf::ChildrenString(Children));
71 // For each attribute description.
72 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
73 const DIEAbbrevData &AttrData = Data[i];
75 // Emit attribute type.
76 AP->EmitULEB128(AttrData.getAttribute(),
77 dwarf::AttributeString(AttrData.getAttribute()));
80 AP->EmitULEB128(AttrData.getForm(),
81 dwarf::FormEncodingString(AttrData.getForm()));
84 // Mark end of abbreviation.
85 AP->EmitULEB128(0, "EOM(1)");
86 AP->EmitULEB128(0, "EOM(2)");
90 void DIEAbbrev::print(raw_ostream &O) {
92 << format("0x%lx", (long)(intptr_t)this)
94 << dwarf::TagString(Tag)
96 << dwarf::ChildrenString(Children)
99 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
101 << dwarf::AttributeString(Data[i].getAttribute())
103 << dwarf::FormEncodingString(Data[i].getForm())
107 void DIEAbbrev::dump() { print(dbgs()); }
110 /// Climb up the parent chain to get the unit DIE to which this DIE
112 const DIE *DIE::getUnit() const {
113 const DIE *Cu = getUnitOrNull();
114 assert(Cu && "We should not have orphaned DIEs.");
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.
120 const DIE *DIE::getUnitOrNull() const {
123 if (p->getTag() == dwarf::DW_TAG_compile_unit ||
124 p->getTag() == dwarf::DW_TAG_type_unit)
131 DIEValue *DIE::findAttribute(dwarf::Attribute Attribute) const {
132 const SmallVectorImpl<DIEValue *> &Values = getValues();
133 const DIEAbbrev &Abbrevs = getAbbrev();
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)
144 void DIE::print(raw_ostream &O, unsigned IndentCount) const {
145 const std::string Indent(IndentCount, ' ');
146 bool isBlock = Abbrev.getTag() == 0;
151 << format("0x%lx", (long)(intptr_t)this)
152 << ", Offset: " << Offset
153 << ", Size: " << Size << "\n";
156 << dwarf::TagString(Abbrev.getTag())
158 << dwarf::ChildrenString(Abbrev.hasChildren()) << "\n";
160 O << "Size: " << Size << "\n";
163 const SmallVectorImpl<DIEAbbrevData> &Data = Abbrev.getData();
166 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
170 O << dwarf::AttributeString(Data[i].getAttribute());
172 O << "Blk[" << i << "]";
175 << dwarf::FormEncodingString(Data[i].getForm())
182 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
183 Children[j]->print(O, IndentCount+4);
186 if (!isBlock) O << "\n";
194 void DIEValue::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
196 #define EMIT_VALUE_IMPL(Kind) \
198 cast<DIE##Kind>(this)->EmitValueImpl(AP, Form); \
200 EMIT_VALUE_IMPL(Integer)
201 EMIT_VALUE_IMPL(String)
202 EMIT_VALUE_IMPL(Expr)
203 EMIT_VALUE_IMPL(Label)
204 EMIT_VALUE_IMPL(Delta)
205 EMIT_VALUE_IMPL(Entry)
206 EMIT_VALUE_IMPL(TypeSignature)
207 EMIT_VALUE_IMPL(Block)
209 EMIT_VALUE_IMPL(LocList)
210 #undef EMIT_VALUE_IMPL
214 unsigned DIEValue::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
216 #define SIZE_OF_IMPL(Kind) \
218 return cast<DIE##Kind>(this)->SizeOfImpl(AP, Form);
219 SIZE_OF_IMPL(Integer)
225 SIZE_OF_IMPL(TypeSignature)
228 SIZE_OF_IMPL(LocList)
231 llvm_unreachable("Unknown DIE kind");
235 void DIEValue::print(raw_ostream &O) const {
237 #define PRINT_IMPL(Kind) \
239 cast<DIE##Kind>(this)->printImpl(O); \
247 PRINT_IMPL(TypeSignature)
255 void DIEValue::dump() const {
260 //===----------------------------------------------------------------------===//
261 // DIEInteger Implementation
262 //===----------------------------------------------------------------------===//
264 /// EmitValue - Emit integer of appropriate size.
266 void DIEInteger::EmitValueImpl(const AsmPrinter *Asm, dwarf::Form Form) const {
269 case dwarf::DW_FORM_flag_present:
270 // Emit something to keep the lines and comments in sync.
271 // FIXME: Is there a better way to do this?
272 Asm->OutStreamer->AddBlankLine();
274 case dwarf::DW_FORM_flag: // Fall thru
275 case dwarf::DW_FORM_ref1: // Fall thru
276 case dwarf::DW_FORM_data1: Size = 1; break;
277 case dwarf::DW_FORM_ref2: // Fall thru
278 case dwarf::DW_FORM_data2: Size = 2; break;
279 case dwarf::DW_FORM_sec_offset: // Fall thru
280 case dwarf::DW_FORM_strp: // Fall thru
281 case dwarf::DW_FORM_ref4: // Fall thru
282 case dwarf::DW_FORM_data4: Size = 4; break;
283 case dwarf::DW_FORM_ref8: // Fall thru
284 case dwarf::DW_FORM_ref_sig8: // Fall thru
285 case dwarf::DW_FORM_data8: Size = 8; break;
286 case dwarf::DW_FORM_GNU_str_index: Asm->EmitULEB128(Integer); return;
287 case dwarf::DW_FORM_GNU_addr_index: Asm->EmitULEB128(Integer); return;
288 case dwarf::DW_FORM_udata: Asm->EmitULEB128(Integer); return;
289 case dwarf::DW_FORM_sdata: Asm->EmitSLEB128(Integer); return;
290 case dwarf::DW_FORM_addr:
291 Size = Asm->getDataLayout().getPointerSize(); break;
292 case dwarf::DW_FORM_ref_addr:
293 Size = SizeOf(Asm, dwarf::DW_FORM_ref_addr);
295 default: llvm_unreachable("DIE Value form not supported yet");
297 Asm->OutStreamer->EmitIntValue(Integer, Size);
300 /// SizeOf - Determine size of integer value in bytes.
302 unsigned DIEInteger::SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const {
304 case dwarf::DW_FORM_flag_present: return 0;
305 case dwarf::DW_FORM_flag: // Fall thru
306 case dwarf::DW_FORM_ref1: // Fall thru
307 case dwarf::DW_FORM_data1: return sizeof(int8_t);
308 case dwarf::DW_FORM_ref2: // Fall thru
309 case dwarf::DW_FORM_data2: return sizeof(int16_t);
310 case dwarf::DW_FORM_sec_offset: // Fall thru
311 case dwarf::DW_FORM_strp: // Fall thru
312 case dwarf::DW_FORM_ref4: // Fall thru
313 case dwarf::DW_FORM_data4: return sizeof(int32_t);
314 case dwarf::DW_FORM_ref8: // Fall thru
315 case dwarf::DW_FORM_ref_sig8: // Fall thru
316 case dwarf::DW_FORM_data8: return sizeof(int64_t);
317 case dwarf::DW_FORM_GNU_str_index: return getULEB128Size(Integer);
318 case dwarf::DW_FORM_GNU_addr_index: return getULEB128Size(Integer);
319 case dwarf::DW_FORM_udata: return getULEB128Size(Integer);
320 case dwarf::DW_FORM_sdata: return getSLEB128Size(Integer);
321 case dwarf::DW_FORM_addr: return AP->getDataLayout().getPointerSize();
322 case dwarf::DW_FORM_ref_addr:
323 if (AP->OutStreamer->getContext().getDwarfVersion() == 2)
324 return AP->getDataLayout().getPointerSize();
325 return sizeof(int32_t);
326 default: llvm_unreachable("DIE Value form not supported yet");
331 void DIEInteger::printImpl(raw_ostream &O) const {
332 O << "Int: " << (int64_t)Integer << " 0x";
333 O.write_hex(Integer);
337 //===----------------------------------------------------------------------===//
338 // DIEExpr Implementation
339 //===----------------------------------------------------------------------===//
341 /// EmitValue - Emit expression value.
343 void DIEExpr::EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const {
344 AP->OutStreamer->EmitValue(Expr, SizeOf(AP, Form));
347 /// SizeOf - Determine size of expression value in bytes.
349 unsigned DIEExpr::SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const {
350 if (Form == dwarf::DW_FORM_data4) return 4;
351 if (Form == dwarf::DW_FORM_sec_offset) return 4;
352 if (Form == dwarf::DW_FORM_strp) return 4;
353 return AP->getDataLayout().getPointerSize();
357 void DIEExpr::printImpl(raw_ostream &O) const {
363 //===----------------------------------------------------------------------===//
364 // DIELabel Implementation
365 //===----------------------------------------------------------------------===//
367 /// EmitValue - Emit label value.
369 void DIELabel::EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const {
370 AP->EmitLabelReference(Label, SizeOf(AP, Form),
371 Form == dwarf::DW_FORM_strp ||
372 Form == dwarf::DW_FORM_sec_offset ||
373 Form == dwarf::DW_FORM_ref_addr);
376 /// SizeOf - Determine size of label value in bytes.
378 unsigned DIELabel::SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const {
379 if (Form == dwarf::DW_FORM_data4) return 4;
380 if (Form == dwarf::DW_FORM_sec_offset) return 4;
381 if (Form == dwarf::DW_FORM_strp) return 4;
382 return AP->getDataLayout().getPointerSize();
386 void DIELabel::printImpl(raw_ostream &O) const {
387 O << "Lbl: " << Label->getName();
391 //===----------------------------------------------------------------------===//
392 // DIEDelta Implementation
393 //===----------------------------------------------------------------------===//
395 /// EmitValue - Emit delta value.
397 void DIEDelta::EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const {
398 AP->EmitLabelDifference(LabelHi, LabelLo, SizeOf(AP, Form));
401 /// SizeOf - Determine size of delta value in bytes.
403 unsigned DIEDelta::SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const {
404 if (Form == dwarf::DW_FORM_data4) return 4;
405 if (Form == dwarf::DW_FORM_sec_offset) return 4;
406 if (Form == dwarf::DW_FORM_strp) return 4;
407 return AP->getDataLayout().getPointerSize();
411 void DIEDelta::printImpl(raw_ostream &O) const {
412 O << "Del: " << LabelHi->getName() << "-" << LabelLo->getName();
416 //===----------------------------------------------------------------------===//
417 // DIEString Implementation
418 //===----------------------------------------------------------------------===//
420 /// EmitValue - Emit string value.
422 void DIEString::EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const {
424 (Form == dwarf::DW_FORM_strp || Form == dwarf::DW_FORM_GNU_str_index) &&
425 "Expected valid string form");
427 // Index of string in symbol table.
428 if (Form == dwarf::DW_FORM_GNU_str_index) {
429 DIEInteger(S.getIndex()).EmitValue(AP, Form);
433 // Relocatable symbol.
434 assert(Form == dwarf::DW_FORM_strp);
435 if (AP->MAI->doesDwarfUseRelocationsAcrossSections()) {
436 DIELabel(S.getSymbol()).EmitValue(AP, Form);
440 // Offset into symbol table.
441 DIEInteger(S.getOffset()).EmitValue(AP, Form);
444 /// SizeOf - Determine size of delta value in bytes.
446 unsigned DIEString::SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const {
448 (Form == dwarf::DW_FORM_strp || Form == dwarf::DW_FORM_GNU_str_index) &&
449 "Expected valid string form");
451 // Index of string in symbol table.
452 if (Form == dwarf::DW_FORM_GNU_str_index)
453 return DIEInteger(S.getIndex()).SizeOf(AP, Form);
455 // Relocatable symbol.
456 if (AP->MAI->doesDwarfUseRelocationsAcrossSections())
457 return DIELabel(S.getSymbol()).SizeOf(AP, Form);
459 // Offset into symbol table.
460 return DIEInteger(S.getOffset()).SizeOf(AP, Form);
464 void DIEString::printImpl(raw_ostream &O) const {
465 O << "String: " << S.getString();
469 //===----------------------------------------------------------------------===//
470 // DIEEntry Implementation
471 //===----------------------------------------------------------------------===//
473 /// EmitValue - Emit debug information entry offset.
475 void DIEEntry::EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const {
477 if (Form == dwarf::DW_FORM_ref_addr) {
478 const DwarfDebug *DD = AP->getDwarfDebug();
479 unsigned Addr = Entry.getOffset();
480 assert(!DD->useSplitDwarf() && "TODO: dwo files can't have relocations.");
481 // For DW_FORM_ref_addr, output the offset from beginning of debug info
482 // section. Entry->getOffset() returns the offset from start of the
484 DwarfCompileUnit *CU = DD->lookupUnit(Entry.getUnit());
485 assert(CU && "CUDie should belong to a CU.");
486 Addr += CU->getDebugInfoOffset();
487 if (AP->MAI->doesDwarfUseRelocationsAcrossSections())
488 AP->EmitLabelPlusOffset(CU->getSectionSym(), Addr,
489 DIEEntry::getRefAddrSize(AP));
491 AP->OutStreamer->EmitIntValue(Addr, DIEEntry::getRefAddrSize(AP));
493 AP->EmitInt32(Entry.getOffset());
496 unsigned DIEEntry::getRefAddrSize(const AsmPrinter *AP) {
497 // DWARF4: References that use the attribute form DW_FORM_ref_addr are
498 // specified to be four bytes in the DWARF 32-bit format and eight bytes
499 // in the DWARF 64-bit format, while DWARF Version 2 specifies that such
500 // references have the same size as an address on the target system.
501 const DwarfDebug *DD = AP->getDwarfDebug();
502 assert(DD && "Expected Dwarf Debug info to be available");
503 if (DD->getDwarfVersion() == 2)
504 return AP->getDataLayout().getPointerSize();
505 return sizeof(int32_t);
509 void DIEEntry::printImpl(raw_ostream &O) const {
510 O << format("Die: 0x%lx", (long)(intptr_t)&Entry);
514 //===----------------------------------------------------------------------===//
515 // DIETypeSignature Implementation
516 //===----------------------------------------------------------------------===//
517 void DIETypeSignature::EmitValueImpl(const AsmPrinter *Asm, dwarf::Form Form) const {
518 assert(Form == dwarf::DW_FORM_ref_sig8);
519 Asm->OutStreamer->EmitIntValue(Unit.getTypeSignature(), 8);
523 void DIETypeSignature::printImpl(raw_ostream &O) const {
524 O << format("Type Unit: 0x%lx", Unit.getTypeSignature());
528 //===----------------------------------------------------------------------===//
529 // DIELoc Implementation
530 //===----------------------------------------------------------------------===//
532 /// ComputeSize - calculate the size of the location expression.
534 unsigned DIELoc::ComputeSize(const AsmPrinter *AP) const {
536 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
537 for (unsigned i = 0, N = Values.size(); i < N; ++i)
538 Size += Values[i]->SizeOf(AP, AbbrevData[i].getForm());
544 /// EmitValue - Emit location data.
546 void DIELoc::EmitValueImpl(const AsmPrinter *Asm, dwarf::Form Form) const {
548 default: llvm_unreachable("Improper form for block");
549 case dwarf::DW_FORM_block1: Asm->EmitInt8(Size); break;
550 case dwarf::DW_FORM_block2: Asm->EmitInt16(Size); break;
551 case dwarf::DW_FORM_block4: Asm->EmitInt32(Size); break;
552 case dwarf::DW_FORM_block:
553 case dwarf::DW_FORM_exprloc:
554 Asm->EmitULEB128(Size); break;
557 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
558 for (unsigned i = 0, N = Values.size(); i < N; ++i)
559 Values[i]->EmitValue(Asm, AbbrevData[i].getForm());
562 /// SizeOf - Determine size of location data in bytes.
564 unsigned DIELoc::SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const {
566 case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
567 case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
568 case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
569 case dwarf::DW_FORM_block:
570 case dwarf::DW_FORM_exprloc:
571 return Size + getULEB128Size(Size);
572 default: llvm_unreachable("Improper form for block");
577 void DIELoc::printImpl(raw_ostream &O) const {
583 //===----------------------------------------------------------------------===//
584 // DIEBlock Implementation
585 //===----------------------------------------------------------------------===//
587 /// ComputeSize - calculate the size of the block.
589 unsigned DIEBlock::ComputeSize(const AsmPrinter *AP) const {
591 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
592 for (unsigned i = 0, N = Values.size(); i < N; ++i)
593 Size += Values[i]->SizeOf(AP, AbbrevData[i].getForm());
599 /// EmitValue - Emit block data.
601 void DIEBlock::EmitValueImpl(const AsmPrinter *Asm, dwarf::Form Form) const {
603 default: llvm_unreachable("Improper form for block");
604 case dwarf::DW_FORM_block1: Asm->EmitInt8(Size); break;
605 case dwarf::DW_FORM_block2: Asm->EmitInt16(Size); break;
606 case dwarf::DW_FORM_block4: Asm->EmitInt32(Size); break;
607 case dwarf::DW_FORM_block: Asm->EmitULEB128(Size); break;
610 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
611 for (unsigned i = 0, N = Values.size(); i < N; ++i)
612 Values[i]->EmitValue(Asm, AbbrevData[i].getForm());
615 /// SizeOf - Determine size of block data in bytes.
617 unsigned DIEBlock::SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const {
619 case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
620 case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
621 case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
622 case dwarf::DW_FORM_block: return Size + getULEB128Size(Size);
623 default: llvm_unreachable("Improper form for block");
628 void DIEBlock::printImpl(raw_ostream &O) const {
634 //===----------------------------------------------------------------------===//
635 // DIELocList Implementation
636 //===----------------------------------------------------------------------===//
638 unsigned DIELocList::SizeOfImpl(const AsmPrinter *AP, dwarf::Form Form) const {
639 if (Form == dwarf::DW_FORM_data4)
641 if (Form == dwarf::DW_FORM_sec_offset)
643 return AP->getDataLayout().getPointerSize();
646 /// EmitValue - Emit label value.
648 void DIELocList::EmitValueImpl(const AsmPrinter *AP, dwarf::Form Form) const {
649 DwarfDebug *DD = AP->getDwarfDebug();
650 MCSymbol *Label = DD->getDebugLocs().getList(Index).Label;
652 if (AP->MAI->doesDwarfUseRelocationsAcrossSections() && !DD->useSplitDwarf())
653 AP->emitSectionOffset(Label);
655 AP->EmitLabelDifference(Label, Label->getSection().getBeginSymbol(), 4);
659 void DIELocList::printImpl(raw_ostream &O) const {
660 O << "LocList: " << Index;