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 //===----------------------------------------------------------------------===//
16 #include "DwarfCompileUnit.h"
17 #include "DwarfDebug.h"
18 #include "DwarfUnit.h"
19 #include "llvm/ADT/Twine.h"
20 #include "llvm/CodeGen/AsmPrinter.h"
21 #include "llvm/IR/DataLayout.h"
22 #include "llvm/MC/MCAsmInfo.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 /// EmitValue - Emit debug information entry offset.
377 void DIEEntry::EmitValue(AsmPrinter *AP, dwarf::Form Form) const {
379 if (Form == dwarf::DW_FORM_ref_addr) {
380 const DwarfDebug *DD = AP->getDwarfDebug();
381 unsigned Addr = Entry.getOffset();
382 assert(!DD->useSplitDwarf() && "TODO: dwo files can't have relocations.");
383 // For DW_FORM_ref_addr, output the offset from beginning of debug info
384 // section. Entry->getOffset() returns the offset from start of the
386 DwarfCompileUnit *CU = DD->lookupUnit(Entry.getUnit());
387 assert(CU && "CUDie should belong to a CU.");
388 Addr += CU->getDebugInfoOffset();
389 if (AP->MAI->doesDwarfUseRelocationsAcrossSections())
390 AP->EmitLabelPlusOffset(CU->getSectionSym(), Addr,
391 DIEEntry::getRefAddrSize(AP));
393 AP->EmitLabelOffsetDifference(CU->getSectionSym(), Addr,
395 DIEEntry::getRefAddrSize(AP));
397 AP->EmitInt32(Entry.getOffset());
400 unsigned DIEEntry::getRefAddrSize(AsmPrinter *AP) {
401 // DWARF4: References that use the attribute form DW_FORM_ref_addr are
402 // specified to be four bytes in the DWARF 32-bit format and eight bytes
403 // in the DWARF 64-bit format, while DWARF Version 2 specifies that such
404 // references have the same size as an address on the target system.
405 const DwarfDebug *DD = AP->getDwarfDebug();
406 assert(DD && "Expected Dwarf Debug info to be available");
407 if (DD->getDwarfVersion() == 2)
408 return AP->getDataLayout().getPointerSize();
409 return sizeof(int32_t);
413 void DIEEntry::print(raw_ostream &O) const {
414 O << format("Die: 0x%lx", (long)(intptr_t)&Entry);
418 //===----------------------------------------------------------------------===//
419 // DIETypeSignature Implementation
420 //===----------------------------------------------------------------------===//
421 void DIETypeSignature::EmitValue(AsmPrinter *Asm, dwarf::Form Form) const {
422 assert(Form == dwarf::DW_FORM_ref_sig8);
423 Asm->OutStreamer.EmitIntValue(Unit.getTypeSignature(), 8);
427 void DIETypeSignature::print(raw_ostream &O) const {
428 O << format("Type Unit: 0x%lx", Unit.getTypeSignature());
431 void DIETypeSignature::dump() const { print(dbgs()); }
434 //===----------------------------------------------------------------------===//
435 // DIELoc Implementation
436 //===----------------------------------------------------------------------===//
438 /// ComputeSize - calculate the size of the location expression.
440 unsigned DIELoc::ComputeSize(AsmPrinter *AP) const {
442 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
443 for (unsigned i = 0, N = Values.size(); i < N; ++i)
444 Size += Values[i]->SizeOf(AP, AbbrevData[i].getForm());
450 /// EmitValue - Emit location data.
452 void DIELoc::EmitValue(AsmPrinter *Asm, dwarf::Form Form) const {
454 default: llvm_unreachable("Improper form for block");
455 case dwarf::DW_FORM_block1: Asm->EmitInt8(Size); break;
456 case dwarf::DW_FORM_block2: Asm->EmitInt16(Size); break;
457 case dwarf::DW_FORM_block4: Asm->EmitInt32(Size); break;
458 case dwarf::DW_FORM_block:
459 case dwarf::DW_FORM_exprloc:
460 Asm->EmitULEB128(Size); break;
463 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
464 for (unsigned i = 0, N = Values.size(); i < N; ++i)
465 Values[i]->EmitValue(Asm, AbbrevData[i].getForm());
468 /// SizeOf - Determine size of location data in bytes.
470 unsigned DIELoc::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
472 case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
473 case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
474 case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
475 case dwarf::DW_FORM_block:
476 case dwarf::DW_FORM_exprloc:
477 return Size + getULEB128Size(Size);
478 default: llvm_unreachable("Improper form for block");
483 void DIELoc::print(raw_ostream &O) const {
489 //===----------------------------------------------------------------------===//
490 // DIEBlock Implementation
491 //===----------------------------------------------------------------------===//
493 /// ComputeSize - calculate the size of the block.
495 unsigned DIEBlock::ComputeSize(AsmPrinter *AP) const {
497 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
498 for (unsigned i = 0, N = Values.size(); i < N; ++i)
499 Size += Values[i]->SizeOf(AP, AbbrevData[i].getForm());
505 /// EmitValue - Emit block data.
507 void DIEBlock::EmitValue(AsmPrinter *Asm, dwarf::Form Form) const {
509 default: llvm_unreachable("Improper form for block");
510 case dwarf::DW_FORM_block1: Asm->EmitInt8(Size); break;
511 case dwarf::DW_FORM_block2: Asm->EmitInt16(Size); break;
512 case dwarf::DW_FORM_block4: Asm->EmitInt32(Size); break;
513 case dwarf::DW_FORM_block: Asm->EmitULEB128(Size); break;
516 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
517 for (unsigned i = 0, N = Values.size(); i < N; ++i)
518 Values[i]->EmitValue(Asm, AbbrevData[i].getForm());
521 /// SizeOf - Determine size of block data in bytes.
523 unsigned DIEBlock::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
525 case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
526 case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
527 case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
528 case dwarf::DW_FORM_block: return Size + getULEB128Size(Size);
529 default: llvm_unreachable("Improper form for block");
534 void DIEBlock::print(raw_ostream &O) const {
540 //===----------------------------------------------------------------------===//
541 // DIELocList Implementation
542 //===----------------------------------------------------------------------===//
544 unsigned DIELocList::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
545 if (Form == dwarf::DW_FORM_data4)
547 if (Form == dwarf::DW_FORM_sec_offset)
549 return AP->getDataLayout().getPointerSize();
552 /// EmitValue - Emit label value.
554 void DIELocList::EmitValue(AsmPrinter *AP, dwarf::Form Form) const {
555 DwarfDebug *DD = AP->getDwarfDebug();
556 MCSymbol *Label = DD->getDebugLocEntries()[Index].Label;
558 if (AP->MAI->doesDwarfUseRelocationsAcrossSections() && !DD->useSplitDwarf())
559 AP->EmitSectionOffset(Label, DD->getDebugLocSym());
561 AP->EmitLabelDifference(Label, DD->getDebugLocSym(), 4);
565 void DIELocList::print(raw_ostream &O) const {
566 O << "LocList: " << Index;