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 {
197 llvm_unreachable("Expected valid DIEValue");
198 #define HANDLE_DIEVALUE(T) \
200 getDIE##T().EmitValue(AP, Form); \
202 #include "llvm/CodeGen/DIEValue.def"
206 unsigned DIEValue::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
209 llvm_unreachable("Expected valid DIEValue");
210 #define HANDLE_DIEVALUE(T) \
212 return getDIE##T().SizeOf(AP, Form);
213 #include "llvm/CodeGen/DIEValue.def"
215 llvm_unreachable("Unknown DIE kind");
219 void DIEValue::print(raw_ostream &O) const {
222 llvm_unreachable("Expected valid DIEValue");
223 #define HANDLE_DIEVALUE(T) \
225 getDIE##T().print(O); \
227 #include "llvm/CodeGen/DIEValue.def"
231 void DIEValue::dump() const {
236 //===----------------------------------------------------------------------===//
237 // DIEInteger Implementation
238 //===----------------------------------------------------------------------===//
240 /// EmitValue - Emit integer of appropriate size.
242 void DIEInteger::EmitValue(const AsmPrinter *Asm, dwarf::Form Form) const {
245 case dwarf::DW_FORM_flag_present:
246 // Emit something to keep the lines and comments in sync.
247 // FIXME: Is there a better way to do this?
248 Asm->OutStreamer->AddBlankLine();
250 case dwarf::DW_FORM_flag: // Fall thru
251 case dwarf::DW_FORM_ref1: // Fall thru
252 case dwarf::DW_FORM_data1: Size = 1; break;
253 case dwarf::DW_FORM_ref2: // Fall thru
254 case dwarf::DW_FORM_data2: Size = 2; break;
255 case dwarf::DW_FORM_sec_offset: // Fall thru
256 case dwarf::DW_FORM_strp: // Fall thru
257 case dwarf::DW_FORM_ref4: // Fall thru
258 case dwarf::DW_FORM_data4: Size = 4; break;
259 case dwarf::DW_FORM_ref8: // Fall thru
260 case dwarf::DW_FORM_ref_sig8: // Fall thru
261 case dwarf::DW_FORM_data8: Size = 8; break;
262 case dwarf::DW_FORM_GNU_str_index: Asm->EmitULEB128(Integer); return;
263 case dwarf::DW_FORM_GNU_addr_index: Asm->EmitULEB128(Integer); return;
264 case dwarf::DW_FORM_udata: Asm->EmitULEB128(Integer); return;
265 case dwarf::DW_FORM_sdata: Asm->EmitSLEB128(Integer); return;
266 case dwarf::DW_FORM_addr:
267 Size = Asm->getDataLayout().getPointerSize(); break;
268 case dwarf::DW_FORM_ref_addr:
269 Size = SizeOf(Asm, dwarf::DW_FORM_ref_addr);
271 default: llvm_unreachable("DIE Value form not supported yet");
273 Asm->OutStreamer->EmitIntValue(Integer, Size);
276 /// SizeOf - Determine size of integer value in bytes.
278 unsigned DIEInteger::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
280 case dwarf::DW_FORM_flag_present: return 0;
281 case dwarf::DW_FORM_flag: // Fall thru
282 case dwarf::DW_FORM_ref1: // Fall thru
283 case dwarf::DW_FORM_data1: return sizeof(int8_t);
284 case dwarf::DW_FORM_ref2: // Fall thru
285 case dwarf::DW_FORM_data2: return sizeof(int16_t);
286 case dwarf::DW_FORM_sec_offset: // Fall thru
287 case dwarf::DW_FORM_strp: // Fall thru
288 case dwarf::DW_FORM_ref4: // Fall thru
289 case dwarf::DW_FORM_data4: return sizeof(int32_t);
290 case dwarf::DW_FORM_ref8: // Fall thru
291 case dwarf::DW_FORM_ref_sig8: // Fall thru
292 case dwarf::DW_FORM_data8: return sizeof(int64_t);
293 case dwarf::DW_FORM_GNU_str_index: return getULEB128Size(Integer);
294 case dwarf::DW_FORM_GNU_addr_index: return getULEB128Size(Integer);
295 case dwarf::DW_FORM_udata: return getULEB128Size(Integer);
296 case dwarf::DW_FORM_sdata: return getSLEB128Size(Integer);
297 case dwarf::DW_FORM_addr: return AP->getDataLayout().getPointerSize();
298 case dwarf::DW_FORM_ref_addr:
299 if (AP->OutStreamer->getContext().getDwarfVersion() == 2)
300 return AP->getDataLayout().getPointerSize();
301 return sizeof(int32_t);
302 default: llvm_unreachable("DIE Value form not supported yet");
307 void DIEInteger::print(raw_ostream &O) const {
308 O << "Int: " << (int64_t)Integer << " 0x";
309 O.write_hex(Integer);
313 //===----------------------------------------------------------------------===//
314 // DIEExpr Implementation
315 //===----------------------------------------------------------------------===//
317 /// EmitValue - Emit expression value.
319 void DIEExpr::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
320 AP->OutStreamer->EmitValue(Expr, SizeOf(AP, Form));
323 /// SizeOf - Determine size of expression value in bytes.
325 unsigned DIEExpr::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
326 if (Form == dwarf::DW_FORM_data4) return 4;
327 if (Form == dwarf::DW_FORM_sec_offset) return 4;
328 if (Form == dwarf::DW_FORM_strp) return 4;
329 return AP->getDataLayout().getPointerSize();
333 void DIEExpr::print(raw_ostream &O) const { O << "Expr: " << *Expr; }
336 //===----------------------------------------------------------------------===//
337 // DIELabel Implementation
338 //===----------------------------------------------------------------------===//
340 /// EmitValue - Emit label value.
342 void DIELabel::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
343 AP->EmitLabelReference(Label, SizeOf(AP, Form),
344 Form == dwarf::DW_FORM_strp ||
345 Form == dwarf::DW_FORM_sec_offset ||
346 Form == dwarf::DW_FORM_ref_addr);
349 /// SizeOf - Determine size of label value in bytes.
351 unsigned DIELabel::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
352 if (Form == dwarf::DW_FORM_data4) return 4;
353 if (Form == dwarf::DW_FORM_sec_offset) return 4;
354 if (Form == dwarf::DW_FORM_strp) return 4;
355 return AP->getDataLayout().getPointerSize();
359 void DIELabel::print(raw_ostream &O) const { O << "Lbl: " << Label->getName(); }
362 //===----------------------------------------------------------------------===//
363 // DIEDelta Implementation
364 //===----------------------------------------------------------------------===//
366 /// EmitValue - Emit delta value.
368 void DIEDelta::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
369 AP->EmitLabelDifference(LabelHi, LabelLo, SizeOf(AP, Form));
372 /// SizeOf - Determine size of delta value in bytes.
374 unsigned DIEDelta::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
375 if (Form == dwarf::DW_FORM_data4) return 4;
376 if (Form == dwarf::DW_FORM_sec_offset) return 4;
377 if (Form == dwarf::DW_FORM_strp) return 4;
378 return AP->getDataLayout().getPointerSize();
382 void DIEDelta::print(raw_ostream &O) const {
383 O << "Del: " << LabelHi->getName() << "-" << LabelLo->getName();
387 //===----------------------------------------------------------------------===//
388 // DIEString Implementation
389 //===----------------------------------------------------------------------===//
391 /// EmitValue - Emit string value.
393 void DIEString::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
395 (Form == dwarf::DW_FORM_strp || Form == dwarf::DW_FORM_GNU_str_index) &&
396 "Expected valid string form");
398 // Index of string in symbol table.
399 if (Form == dwarf::DW_FORM_GNU_str_index) {
400 DIEInteger(S.getIndex()).EmitValue(AP, Form);
404 // Relocatable symbol.
405 assert(Form == dwarf::DW_FORM_strp);
406 if (AP->MAI->doesDwarfUseRelocationsAcrossSections()) {
407 DIELabel(S.getSymbol()).EmitValue(AP, Form);
411 // Offset into symbol table.
412 DIEInteger(S.getOffset()).EmitValue(AP, Form);
415 /// SizeOf - Determine size of delta value in bytes.
417 unsigned DIEString::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
419 (Form == dwarf::DW_FORM_strp || Form == dwarf::DW_FORM_GNU_str_index) &&
420 "Expected valid string form");
422 // Index of string in symbol table.
423 if (Form == dwarf::DW_FORM_GNU_str_index)
424 return DIEInteger(S.getIndex()).SizeOf(AP, Form);
426 // Relocatable symbol.
427 if (AP->MAI->doesDwarfUseRelocationsAcrossSections())
428 return DIELabel(S.getSymbol()).SizeOf(AP, Form);
430 // Offset into symbol table.
431 return DIEInteger(S.getOffset()).SizeOf(AP, Form);
435 void DIEString::print(raw_ostream &O) const {
436 O << "String: " << S.getString();
440 //===----------------------------------------------------------------------===//
441 // DIEEntry Implementation
442 //===----------------------------------------------------------------------===//
444 /// EmitValue - Emit debug information entry offset.
446 void DIEEntry::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
448 if (Form == dwarf::DW_FORM_ref_addr) {
449 const DwarfDebug *DD = AP->getDwarfDebug();
450 unsigned Addr = Entry->getOffset();
451 assert(!DD->useSplitDwarf() && "TODO: dwo files can't have relocations.");
452 // For DW_FORM_ref_addr, output the offset from beginning of debug info
453 // section. Entry->getOffset() returns the offset from start of the
455 DwarfCompileUnit *CU = DD->lookupUnit(Entry->getUnit());
456 assert(CU && "CUDie should belong to a CU.");
457 Addr += CU->getDebugInfoOffset();
458 if (AP->MAI->doesDwarfUseRelocationsAcrossSections())
459 AP->EmitLabelPlusOffset(CU->getSectionSym(), Addr,
460 DIEEntry::getRefAddrSize(AP));
462 AP->OutStreamer->EmitIntValue(Addr, DIEEntry::getRefAddrSize(AP));
464 AP->EmitInt32(Entry->getOffset());
467 unsigned DIEEntry::getRefAddrSize(const AsmPrinter *AP) {
468 // DWARF4: References that use the attribute form DW_FORM_ref_addr are
469 // specified to be four bytes in the DWARF 32-bit format and eight bytes
470 // in the DWARF 64-bit format, while DWARF Version 2 specifies that such
471 // references have the same size as an address on the target system.
472 const DwarfDebug *DD = AP->getDwarfDebug();
473 assert(DD && "Expected Dwarf Debug info to be available");
474 if (DD->getDwarfVersion() == 2)
475 return AP->getDataLayout().getPointerSize();
476 return sizeof(int32_t);
480 void DIEEntry::print(raw_ostream &O) const {
481 O << format("Die: 0x%lx", (long)(intptr_t)&Entry);
485 //===----------------------------------------------------------------------===//
486 // DIETypeSignature Implementation
487 //===----------------------------------------------------------------------===//
488 void DIETypeSignature::EmitValue(const AsmPrinter *Asm,
489 dwarf::Form Form) const {
490 assert(Form == dwarf::DW_FORM_ref_sig8);
491 Asm->OutStreamer->EmitIntValue(Unit->getTypeSignature(), 8);
495 void DIETypeSignature::print(raw_ostream &O) const {
496 O << format("Type Unit: 0x%lx", Unit->getTypeSignature());
500 //===----------------------------------------------------------------------===//
501 // DIELoc Implementation
502 //===----------------------------------------------------------------------===//
504 /// ComputeSize - calculate the size of the location expression.
506 unsigned DIELoc::ComputeSize(const AsmPrinter *AP) const {
508 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
509 for (unsigned i = 0, N = Values.size(); i < N; ++i)
510 Size += Values[i].SizeOf(AP, AbbrevData[i].getForm());
516 /// EmitValue - Emit location data.
518 void DIELoc::EmitValue(const AsmPrinter *Asm, dwarf::Form Form) const {
520 default: llvm_unreachable("Improper form for block");
521 case dwarf::DW_FORM_block1: Asm->EmitInt8(Size); break;
522 case dwarf::DW_FORM_block2: Asm->EmitInt16(Size); break;
523 case dwarf::DW_FORM_block4: Asm->EmitInt32(Size); break;
524 case dwarf::DW_FORM_block:
525 case dwarf::DW_FORM_exprloc:
526 Asm->EmitULEB128(Size); break;
529 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
530 for (unsigned i = 0, N = Values.size(); i < N; ++i)
531 Values[i].EmitValue(Asm, AbbrevData[i].getForm());
534 /// SizeOf - Determine size of location data in bytes.
536 unsigned DIELoc::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
538 case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
539 case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
540 case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
541 case dwarf::DW_FORM_block:
542 case dwarf::DW_FORM_exprloc:
543 return Size + getULEB128Size(Size);
544 default: llvm_unreachable("Improper form for block");
549 void DIELoc::print(raw_ostream &O) const {
555 //===----------------------------------------------------------------------===//
556 // DIEBlock Implementation
557 //===----------------------------------------------------------------------===//
559 /// ComputeSize - calculate the size of the block.
561 unsigned DIEBlock::ComputeSize(const AsmPrinter *AP) const {
563 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
564 for (unsigned i = 0, N = Values.size(); i < N; ++i)
565 Size += Values[i].SizeOf(AP, AbbrevData[i].getForm());
571 /// EmitValue - Emit block data.
573 void DIEBlock::EmitValue(const AsmPrinter *Asm, dwarf::Form Form) const {
575 default: llvm_unreachable("Improper form for block");
576 case dwarf::DW_FORM_block1: Asm->EmitInt8(Size); break;
577 case dwarf::DW_FORM_block2: Asm->EmitInt16(Size); break;
578 case dwarf::DW_FORM_block4: Asm->EmitInt32(Size); break;
579 case dwarf::DW_FORM_block: Asm->EmitULEB128(Size); break;
582 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
583 for (unsigned i = 0, N = Values.size(); i < N; ++i)
584 Values[i].EmitValue(Asm, AbbrevData[i].getForm());
587 /// SizeOf - Determine size of block data in bytes.
589 unsigned DIEBlock::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
591 case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
592 case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
593 case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
594 case dwarf::DW_FORM_block: return Size + getULEB128Size(Size);
595 default: llvm_unreachable("Improper form for block");
600 void DIEBlock::print(raw_ostream &O) const {
606 //===----------------------------------------------------------------------===//
607 // DIELocList Implementation
608 //===----------------------------------------------------------------------===//
610 unsigned DIELocList::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
611 if (Form == dwarf::DW_FORM_data4)
613 if (Form == dwarf::DW_FORM_sec_offset)
615 return AP->getDataLayout().getPointerSize();
618 /// EmitValue - Emit label value.
620 void DIELocList::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
621 DwarfDebug *DD = AP->getDwarfDebug();
622 MCSymbol *Label = DD->getDebugLocs().getList(Index).Label;
624 if (AP->MAI->doesDwarfUseRelocationsAcrossSections() && !DD->useSplitDwarf())
625 AP->emitSectionOffset(Label);
627 AP->EmitLabelDifference(Label, Label->getSection().getBeginSymbol(), 4);
631 void DIELocList::print(raw_ostream &O) const { O << "LocList: " << Index; }