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"
33 //===----------------------------------------------------------------------===//
34 // DIEAbbrevData Implementation
35 //===----------------------------------------------------------------------===//
37 /// Profile - Used to gather unique data for the abbreviation folding set.
39 void DIEAbbrevData::Profile(FoldingSetNodeID &ID) const {
40 // Explicitly cast to an integer type for which FoldingSetNodeID has
41 // overloads. Otherwise MSVC 2010 thinks this call is ambiguous.
42 ID.AddInteger(unsigned(Attribute));
43 ID.AddInteger(unsigned(Form));
46 //===----------------------------------------------------------------------===//
47 // DIEAbbrev Implementation
48 //===----------------------------------------------------------------------===//
50 /// Profile - Used to gather unique data for the abbreviation folding set.
52 void DIEAbbrev::Profile(FoldingSetNodeID &ID) const {
53 ID.AddInteger(unsigned(Tag));
54 ID.AddInteger(unsigned(Children));
56 // For each attribute description.
57 for (unsigned i = 0, N = Data.size(); i < N; ++i)
61 /// Emit - Print the abbreviation using the specified asm printer.
63 void DIEAbbrev::Emit(AsmPrinter *AP) const {
64 // Emit its Dwarf tag type.
65 AP->EmitULEB128(Tag, dwarf::TagString(Tag));
67 // Emit whether it has children DIEs.
68 AP->EmitULEB128((unsigned)Children, dwarf::ChildrenString(Children));
70 // For each attribute description.
71 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
72 const DIEAbbrevData &AttrData = Data[i];
74 // Emit attribute type.
75 AP->EmitULEB128(AttrData.getAttribute(),
76 dwarf::AttributeString(AttrData.getAttribute()));
79 AP->EmitULEB128(AttrData.getForm(),
80 dwarf::FormEncodingString(AttrData.getForm()));
83 // Mark end of abbreviation.
84 AP->EmitULEB128(0, "EOM(1)");
85 AP->EmitULEB128(0, "EOM(2)");
89 void DIEAbbrev::print(raw_ostream &O) {
91 << format("0x%lx", (long)(intptr_t)this)
93 << dwarf::TagString(Tag)
95 << dwarf::ChildrenString(Children)
98 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
100 << dwarf::AttributeString(Data[i].getAttribute())
102 << dwarf::FormEncodingString(Data[i].getForm())
106 void DIEAbbrev::dump() { print(dbgs()); }
109 /// Climb up the parent chain to get the unit DIE to which this DIE
111 const DIE *DIE::getUnit() const {
112 const DIE *Cu = getUnitOrNull();
113 assert(Cu && "We should not have orphaned DIEs.");
117 /// Climb up the parent chain to get the unit DIE this DIE belongs
118 /// to. Return NULL if DIE is not added to an owner yet.
119 const DIE *DIE::getUnitOrNull() const {
122 if (p->getTag() == dwarf::DW_TAG_compile_unit ||
123 p->getTag() == dwarf::DW_TAG_type_unit)
130 DIEValue *DIE::findAttribute(dwarf::Attribute Attribute) const {
131 const SmallVectorImpl<DIEValue *> &Values = getValues();
132 const DIEAbbrev &Abbrevs = getAbbrev();
134 // Iterate through all the attributes until we find the one we're
135 // looking for, if we can't find it return NULL.
136 for (size_t i = 0; i < Values.size(); ++i)
137 if (Abbrevs.getData()[i].getAttribute() == Attribute)
143 void DIE::print(raw_ostream &O, unsigned IndentCount) const {
144 const std::string Indent(IndentCount, ' ');
145 bool isBlock = Abbrev.getTag() == 0;
150 << format("0x%lx", (long)(intptr_t)this)
151 << ", Offset: " << Offset
152 << ", Size: " << Size << "\n";
155 << dwarf::TagString(Abbrev.getTag())
157 << dwarf::ChildrenString(Abbrev.hasChildren()) << "\n";
159 O << "Size: " << Size << "\n";
162 const SmallVectorImpl<DIEAbbrevData> &Data = Abbrev.getData();
165 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
169 O << dwarf::AttributeString(Data[i].getAttribute());
171 O << "Blk[" << i << "]";
174 << dwarf::FormEncodingString(Data[i].getForm())
181 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
182 Children[j]->print(O, IndentCount+4);
185 if (!isBlock) O << "\n";
193 void DIEValue::anchor() { }
196 void DIEValue::dump() const {
201 //===----------------------------------------------------------------------===//
202 // DIEInteger Implementation
203 //===----------------------------------------------------------------------===//
205 /// EmitValue - Emit integer of appropriate size.
207 void DIEInteger::EmitValue(AsmPrinter *Asm, dwarf::Form Form) const {
210 case dwarf::DW_FORM_flag_present:
211 // Emit something to keep the lines and comments in sync.
212 // FIXME: Is there a better way to do this?
213 Asm->OutStreamer.AddBlankLine();
215 case dwarf::DW_FORM_flag: // Fall thru
216 case dwarf::DW_FORM_ref1: // Fall thru
217 case dwarf::DW_FORM_data1: Size = 1; break;
218 case dwarf::DW_FORM_ref2: // Fall thru
219 case dwarf::DW_FORM_data2: Size = 2; break;
220 case dwarf::DW_FORM_sec_offset: // Fall thru
221 case dwarf::DW_FORM_ref4: // Fall thru
222 case dwarf::DW_FORM_data4: Size = 4; break;
223 case dwarf::DW_FORM_ref8: // Fall thru
224 case dwarf::DW_FORM_ref_sig8: // Fall thru
225 case dwarf::DW_FORM_data8: Size = 8; break;
226 case dwarf::DW_FORM_GNU_str_index: Asm->EmitULEB128(Integer); return;
227 case dwarf::DW_FORM_GNU_addr_index: Asm->EmitULEB128(Integer); return;
228 case dwarf::DW_FORM_udata: Asm->EmitULEB128(Integer); return;
229 case dwarf::DW_FORM_sdata: Asm->EmitSLEB128(Integer); return;
230 case dwarf::DW_FORM_addr:
231 Size = Asm->getDataLayout().getPointerSize(); break;
232 default: llvm_unreachable("DIE Value form not supported yet");
234 Asm->OutStreamer.EmitIntValue(Integer, Size);
237 /// SizeOf - Determine size of integer value in bytes.
239 unsigned DIEInteger::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
241 case dwarf::DW_FORM_flag_present: return 0;
242 case dwarf::DW_FORM_flag: // Fall thru
243 case dwarf::DW_FORM_ref1: // Fall thru
244 case dwarf::DW_FORM_data1: return sizeof(int8_t);
245 case dwarf::DW_FORM_ref2: // Fall thru
246 case dwarf::DW_FORM_data2: return sizeof(int16_t);
247 case dwarf::DW_FORM_sec_offset: // Fall thru
248 case dwarf::DW_FORM_ref4: // Fall thru
249 case dwarf::DW_FORM_data4: return sizeof(int32_t);
250 case dwarf::DW_FORM_ref8: // Fall thru
251 case dwarf::DW_FORM_ref_sig8: // Fall thru
252 case dwarf::DW_FORM_data8: return sizeof(int64_t);
253 case dwarf::DW_FORM_GNU_str_index: return getULEB128Size(Integer);
254 case dwarf::DW_FORM_GNU_addr_index: return getULEB128Size(Integer);
255 case dwarf::DW_FORM_udata: return getULEB128Size(Integer);
256 case dwarf::DW_FORM_sdata: return getSLEB128Size(Integer);
257 case dwarf::DW_FORM_addr: return AP->getDataLayout().getPointerSize();
258 default: llvm_unreachable("DIE Value form not supported yet");
263 void DIEInteger::print(raw_ostream &O) const {
264 O << "Int: " << (int64_t)Integer << " 0x";
265 O.write_hex(Integer);
269 //===----------------------------------------------------------------------===//
270 // DIEExpr Implementation
271 //===----------------------------------------------------------------------===//
273 /// EmitValue - Emit expression value.
275 void DIEExpr::EmitValue(AsmPrinter *AP, dwarf::Form Form) const {
276 AP->OutStreamer.EmitValue(Expr, SizeOf(AP, Form));
279 /// SizeOf - Determine size of expression value in bytes.
281 unsigned DIEExpr::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
282 if (Form == dwarf::DW_FORM_data4) return 4;
283 if (Form == dwarf::DW_FORM_sec_offset) return 4;
284 if (Form == dwarf::DW_FORM_strp) return 4;
285 return AP->getDataLayout().getPointerSize();
289 void DIEExpr::print(raw_ostream &O) const {
295 //===----------------------------------------------------------------------===//
296 // DIELabel Implementation
297 //===----------------------------------------------------------------------===//
299 /// EmitValue - Emit label value.
301 void DIELabel::EmitValue(AsmPrinter *AP, dwarf::Form Form) const {
302 AP->EmitLabelReference(Label, SizeOf(AP, Form),
303 Form == dwarf::DW_FORM_strp ||
304 Form == dwarf::DW_FORM_sec_offset ||
305 Form == dwarf::DW_FORM_ref_addr);
308 /// SizeOf - Determine size of label value in bytes.
310 unsigned DIELabel::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
311 if (Form == dwarf::DW_FORM_data4) return 4;
312 if (Form == dwarf::DW_FORM_sec_offset) return 4;
313 if (Form == dwarf::DW_FORM_strp) return 4;
314 return AP->getDataLayout().getPointerSize();
318 void DIELabel::print(raw_ostream &O) const {
319 O << "Lbl: " << Label->getName();
323 //===----------------------------------------------------------------------===//
324 // DIEDelta Implementation
325 //===----------------------------------------------------------------------===//
327 /// EmitValue - Emit delta value.
329 void DIEDelta::EmitValue(AsmPrinter *AP, dwarf::Form Form) const {
330 AP->EmitLabelDifference(LabelHi, LabelLo, SizeOf(AP, Form));
333 /// SizeOf - Determine size of delta value in bytes.
335 unsigned DIEDelta::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
336 if (Form == dwarf::DW_FORM_data4) return 4;
337 if (Form == dwarf::DW_FORM_sec_offset) return 4;
338 if (Form == dwarf::DW_FORM_strp) return 4;
339 return AP->getDataLayout().getPointerSize();
343 void DIEDelta::print(raw_ostream &O) const {
344 O << "Del: " << LabelHi->getName() << "-" << LabelLo->getName();
348 //===----------------------------------------------------------------------===//
349 // DIEString Implementation
350 //===----------------------------------------------------------------------===//
352 /// EmitValue - Emit string value.
354 void DIEString::EmitValue(AsmPrinter *AP, dwarf::Form Form) const {
355 Access->EmitValue(AP, Form);
358 /// SizeOf - Determine size of delta value in bytes.
360 unsigned DIEString::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
361 return Access->SizeOf(AP, Form);
365 void DIEString::print(raw_ostream &O) const {
366 O << "String: " << Str << "\tSymbol: ";
371 //===----------------------------------------------------------------------===//
372 // DIEEntry Implementation
373 //===----------------------------------------------------------------------===//
375 /// Emit something like ".long Hi+Offset-Lo" where the size in bytes of the
376 /// directive is specified by Size and Hi/Lo specify the labels.
377 static void emitLabelOffsetDifference(MCStreamer &Streamer, const MCSymbol *Hi,
378 uint64_t Offset, const MCSymbol *Lo,
380 MCContext &Context = Streamer.getContext();
382 // Emit Hi+Offset - Lo
383 // Get the Hi+Offset expression.
385 MCBinaryExpr::CreateAdd(MCSymbolRefExpr::Create(Hi, Context),
386 MCConstantExpr::Create(Offset, Context), Context);
388 // Get the Hi+Offset-Lo expression.
389 const MCExpr *Diff = MCBinaryExpr::CreateSub(
390 Plus, MCSymbolRefExpr::Create(Lo, Context), Context);
392 // Otherwise, emit with .set (aka assignment).
393 MCSymbol *SetLabel = Context.CreateTempSymbol();
394 Streamer.EmitAssignment(SetLabel, Diff);
395 Streamer.EmitSymbolValue(SetLabel, Size);
398 /// EmitValue - Emit debug information entry offset.
400 void DIEEntry::EmitValue(AsmPrinter *AP, dwarf::Form Form) const {
402 if (Form == dwarf::DW_FORM_ref_addr) {
403 const DwarfDebug *DD = AP->getDwarfDebug();
404 unsigned Addr = Entry.getOffset();
405 assert(!DD->useSplitDwarf() && "TODO: dwo files can't have relocations.");
406 // For DW_FORM_ref_addr, output the offset from beginning of debug info
407 // section. Entry->getOffset() returns the offset from start of the
409 DwarfCompileUnit *CU = DD->lookupUnit(Entry.getUnit());
410 assert(CU && "CUDie should belong to a CU.");
411 Addr += CU->getDebugInfoOffset();
412 if (AP->MAI->doesDwarfUseRelocationsAcrossSections())
413 AP->EmitLabelPlusOffset(CU->getSectionSym(), Addr,
414 DIEEntry::getRefAddrSize(AP));
416 emitLabelOffsetDifference(AP->OutStreamer, CU->getSectionSym(), Addr,
418 DIEEntry::getRefAddrSize(AP));
420 AP->EmitInt32(Entry.getOffset());
423 unsigned DIEEntry::getRefAddrSize(AsmPrinter *AP) {
424 // DWARF4: References that use the attribute form DW_FORM_ref_addr are
425 // specified to be four bytes in the DWARF 32-bit format and eight bytes
426 // in the DWARF 64-bit format, while DWARF Version 2 specifies that such
427 // references have the same size as an address on the target system.
428 const DwarfDebug *DD = AP->getDwarfDebug();
429 assert(DD && "Expected Dwarf Debug info to be available");
430 if (DD->getDwarfVersion() == 2)
431 return AP->getDataLayout().getPointerSize();
432 return sizeof(int32_t);
436 void DIEEntry::print(raw_ostream &O) const {
437 O << format("Die: 0x%lx", (long)(intptr_t)&Entry);
441 //===----------------------------------------------------------------------===//
442 // DIETypeSignature Implementation
443 //===----------------------------------------------------------------------===//
444 void DIETypeSignature::EmitValue(AsmPrinter *Asm, dwarf::Form Form) const {
445 assert(Form == dwarf::DW_FORM_ref_sig8);
446 Asm->OutStreamer.EmitIntValue(Unit.getTypeSignature(), 8);
450 void DIETypeSignature::print(raw_ostream &O) const {
451 O << format("Type Unit: 0x%lx", Unit.getTypeSignature());
454 void DIETypeSignature::dump() const { print(dbgs()); }
457 //===----------------------------------------------------------------------===//
458 // DIELoc Implementation
459 //===----------------------------------------------------------------------===//
461 /// ComputeSize - calculate the size of the location expression.
463 unsigned DIELoc::ComputeSize(AsmPrinter *AP) const {
465 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
466 for (unsigned i = 0, N = Values.size(); i < N; ++i)
467 Size += Values[i]->SizeOf(AP, AbbrevData[i].getForm());
473 /// EmitValue - Emit location data.
475 void DIELoc::EmitValue(AsmPrinter *Asm, dwarf::Form Form) const {
477 default: llvm_unreachable("Improper form for block");
478 case dwarf::DW_FORM_block1: Asm->EmitInt8(Size); break;
479 case dwarf::DW_FORM_block2: Asm->EmitInt16(Size); break;
480 case dwarf::DW_FORM_block4: Asm->EmitInt32(Size); break;
481 case dwarf::DW_FORM_block:
482 case dwarf::DW_FORM_exprloc:
483 Asm->EmitULEB128(Size); break;
486 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
487 for (unsigned i = 0, N = Values.size(); i < N; ++i)
488 Values[i]->EmitValue(Asm, AbbrevData[i].getForm());
491 /// SizeOf - Determine size of location data in bytes.
493 unsigned DIELoc::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
495 case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
496 case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
497 case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
498 case dwarf::DW_FORM_block:
499 case dwarf::DW_FORM_exprloc:
500 return Size + getULEB128Size(Size);
501 default: llvm_unreachable("Improper form for block");
506 void DIELoc::print(raw_ostream &O) const {
512 //===----------------------------------------------------------------------===//
513 // DIEBlock Implementation
514 //===----------------------------------------------------------------------===//
516 /// ComputeSize - calculate the size of the block.
518 unsigned DIEBlock::ComputeSize(AsmPrinter *AP) const {
520 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
521 for (unsigned i = 0, N = Values.size(); i < N; ++i)
522 Size += Values[i]->SizeOf(AP, AbbrevData[i].getForm());
528 /// EmitValue - Emit block data.
530 void DIEBlock::EmitValue(AsmPrinter *Asm, dwarf::Form Form) const {
532 default: llvm_unreachable("Improper form for block");
533 case dwarf::DW_FORM_block1: Asm->EmitInt8(Size); break;
534 case dwarf::DW_FORM_block2: Asm->EmitInt16(Size); break;
535 case dwarf::DW_FORM_block4: Asm->EmitInt32(Size); break;
536 case dwarf::DW_FORM_block: Asm->EmitULEB128(Size); break;
539 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
540 for (unsigned i = 0, N = Values.size(); i < N; ++i)
541 Values[i]->EmitValue(Asm, AbbrevData[i].getForm());
544 /// SizeOf - Determine size of block data in bytes.
546 unsigned DIEBlock::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
548 case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
549 case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
550 case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
551 case dwarf::DW_FORM_block: return Size + getULEB128Size(Size);
552 default: llvm_unreachable("Improper form for block");
557 void DIEBlock::print(raw_ostream &O) const {
563 //===----------------------------------------------------------------------===//
564 // DIELocList Implementation
565 //===----------------------------------------------------------------------===//
567 unsigned DIELocList::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
568 if (Form == dwarf::DW_FORM_data4)
570 if (Form == dwarf::DW_FORM_sec_offset)
572 return AP->getDataLayout().getPointerSize();
575 /// EmitValue - Emit label value.
577 void DIELocList::EmitValue(AsmPrinter *AP, dwarf::Form Form) const {
578 DwarfDebug *DD = AP->getDwarfDebug();
579 MCSymbol *Label = DD->getDebugLocEntries()[Index].Label;
581 if (AP->MAI->doesDwarfUseRelocationsAcrossSections() && !DD->useSplitDwarf())
582 AP->EmitSectionOffset(Label, DD->getDebugLocSym());
584 AP->EmitLabelDifference(Label, DD->getDebugLocSym(), 4);
588 void DIELocList::print(raw_ostream &O) const {
589 O << "LocList: " << Index;