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 //===----------------------------------------------------------------------===//
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"
31 //===----------------------------------------------------------------------===//
32 // DIEAbbrevData Implementation
33 //===----------------------------------------------------------------------===//
35 /// Profile - Used to gather unique data for the abbreviation folding set.
37 void 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));
44 //===----------------------------------------------------------------------===//
45 // DIEAbbrev Implementation
46 //===----------------------------------------------------------------------===//
48 /// Profile - Used to gather unique data for the abbreviation folding set.
50 void DIEAbbrev::Profile(FoldingSetNodeID &ID) const {
51 ID.AddInteger(unsigned(Tag));
52 ID.AddInteger(unsigned(Children));
54 // For each attribute description.
55 for (unsigned i = 0, N = Data.size(); i < N; ++i)
59 /// Emit - Print the abbreviation using the specified asm printer.
61 void DIEAbbrev::Emit(AsmPrinter *AP) const {
62 // Emit its Dwarf tag type.
63 AP->EmitULEB128(Tag, dwarf::TagString(Tag));
65 // Emit whether it has children DIEs.
66 AP->EmitULEB128((unsigned)Children, dwarf::ChildrenString(Children));
68 // For each attribute description.
69 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
70 const DIEAbbrevData &AttrData = Data[i];
72 // Emit attribute type.
73 AP->EmitULEB128(AttrData.getAttribute(),
74 dwarf::AttributeString(AttrData.getAttribute()));
77 AP->EmitULEB128(AttrData.getForm(),
78 dwarf::FormEncodingString(AttrData.getForm()));
81 // Mark end of abbreviation.
82 AP->EmitULEB128(0, "EOM(1)");
83 AP->EmitULEB128(0, "EOM(2)");
87 void DIEAbbrev::print(raw_ostream &O) {
89 << format("0x%lx", (long)(intptr_t)this)
91 << dwarf::TagString(Tag)
93 << dwarf::ChildrenString(Children)
96 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
98 << dwarf::AttributeString(Data[i].getAttribute())
100 << dwarf::FormEncodingString(Data[i].getForm())
104 void DIEAbbrev::dump() { print(dbgs()); }
107 /// Climb up the parent chain to get the unit DIE to which this DIE
109 const DIE *DIE::getUnit() const {
110 const DIE *Cu = getUnitOrNull();
111 assert(Cu && "We should not have orphaned DIEs.");
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.
117 const DIE *DIE::getUnitOrNull() const {
120 if (p->getTag() == dwarf::DW_TAG_compile_unit ||
121 p->getTag() == dwarf::DW_TAG_type_unit)
128 DIEValue *DIE::findAttribute(dwarf::Attribute Attribute) const {
129 const SmallVectorImpl<DIEValue *> &Values = getValues();
130 const DIEAbbrev &Abbrevs = getAbbrev();
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)
141 void DIE::print(raw_ostream &O, unsigned IndentCount) const {
142 const std::string Indent(IndentCount, ' ');
143 bool isBlock = Abbrev.getTag() == 0;
148 << format("0x%lx", (long)(intptr_t)this)
149 << ", Offset: " << Offset
150 << ", Size: " << Size << "\n";
153 << dwarf::TagString(Abbrev.getTag())
155 << dwarf::ChildrenString(Abbrev.hasChildren()) << "\n";
157 O << "Size: " << Size << "\n";
160 const SmallVectorImpl<DIEAbbrevData> &Data = Abbrev.getData();
163 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
167 O << dwarf::AttributeString(Data[i].getAttribute());
169 O << "Blk[" << i << "]";
172 << dwarf::FormEncodingString(Data[i].getForm())
179 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
180 Children[j]->print(O, IndentCount+4);
183 if (!isBlock) O << "\n";
191 void DIEValue::anchor() { }
194 void DIEValue::dump() const {
199 //===----------------------------------------------------------------------===//
200 // DIEInteger Implementation
201 //===----------------------------------------------------------------------===//
203 /// EmitValue - Emit integer of appropriate size.
205 void DIEInteger::EmitValue(AsmPrinter *Asm, dwarf::Form Form) const {
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();
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");
232 Asm->OutStreamer.EmitIntValue(Integer, Size);
235 /// SizeOf - Determine size of integer value in bytes.
237 unsigned DIEInteger::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
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");
261 void DIEInteger::print(raw_ostream &O) const {
262 O << "Int: " << (int64_t)Integer << " 0x";
263 O.write_hex(Integer);
267 //===----------------------------------------------------------------------===//
268 // DIEExpr Implementation
269 //===----------------------------------------------------------------------===//
271 /// EmitValue - Emit expression value.
273 void DIEExpr::EmitValue(AsmPrinter *AP, dwarf::Form Form) const {
274 AP->OutStreamer.EmitValue(Expr, SizeOf(AP, Form));
277 /// SizeOf - Determine size of expression value in bytes.
279 unsigned 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();
287 void DIEExpr::print(raw_ostream &O) const {
293 //===----------------------------------------------------------------------===//
294 // DIELabel Implementation
295 //===----------------------------------------------------------------------===//
297 /// EmitValue - Emit label value.
299 void 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);
306 /// SizeOf - Determine size of label value in bytes.
308 unsigned 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();
316 void DIELabel::print(raw_ostream &O) const {
317 O << "Lbl: " << Label->getName();
321 //===----------------------------------------------------------------------===//
322 // DIEDelta Implementation
323 //===----------------------------------------------------------------------===//
325 /// EmitValue - Emit delta value.
327 void DIEDelta::EmitValue(AsmPrinter *AP, dwarf::Form Form) const {
328 AP->EmitLabelDifference(LabelHi, LabelLo, SizeOf(AP, Form));
331 /// SizeOf - Determine size of delta value in bytes.
333 unsigned 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();
341 void DIEDelta::print(raw_ostream &O) const {
342 O << "Del: " << LabelHi->getName() << "-" << LabelLo->getName();
346 //===----------------------------------------------------------------------===//
347 // DIEString Implementation
348 //===----------------------------------------------------------------------===//
350 /// EmitValue - Emit string value.
352 void DIEString::EmitValue(AsmPrinter *AP, dwarf::Form Form) const {
353 Access->EmitValue(AP, Form);
356 /// SizeOf - Determine size of delta value in bytes.
358 unsigned DIEString::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
359 return Access->SizeOf(AP, Form);
363 void DIEString::print(raw_ostream &O) const {
364 O << "String: " << Str << "\tSymbol: ";
369 //===----------------------------------------------------------------------===//
370 // DIEEntry Implementation
371 //===----------------------------------------------------------------------===//
373 /// EmitValue - Emit debug information entry offset.
375 void DIEEntry::EmitValue(AsmPrinter *AP, dwarf::Form Form) const {
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
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));
391 AP->EmitLabelOffsetDifference(CU->getSectionSym(), Addr,
393 DIEEntry::getRefAddrSize(AP));
395 AP->EmitInt32(Entry.getOffset());
398 unsigned 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);
411 void DIEEntry::print(raw_ostream &O) const {
412 O << format("Die: 0x%lx", (long)(intptr_t)&Entry);
416 //===----------------------------------------------------------------------===//
417 // DIETypeSignature Implementation
418 //===----------------------------------------------------------------------===//
419 void DIETypeSignature::EmitValue(AsmPrinter *Asm, dwarf::Form Form) const {
420 assert(Form == dwarf::DW_FORM_ref_sig8);
421 Asm->OutStreamer.EmitIntValue(Unit.getTypeSignature(), 8);
425 void DIETypeSignature::print(raw_ostream &O) const {
426 O << format("Type Unit: 0x%lx", Unit.getTypeSignature());
429 void DIETypeSignature::dump() const { print(dbgs()); }
432 //===----------------------------------------------------------------------===//
433 // DIELoc Implementation
434 //===----------------------------------------------------------------------===//
436 /// ComputeSize - calculate the size of the location expression.
438 unsigned DIELoc::ComputeSize(AsmPrinter *AP) const {
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());
448 /// EmitValue - Emit location data.
450 void DIELoc::EmitValue(AsmPrinter *Asm, dwarf::Form Form) const {
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;
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());
466 /// SizeOf - Determine size of location data in bytes.
468 unsigned DIELoc::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
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");
481 void DIELoc::print(raw_ostream &O) const {
487 //===----------------------------------------------------------------------===//
488 // DIEBlock Implementation
489 //===----------------------------------------------------------------------===//
491 /// ComputeSize - calculate the size of the block.
493 unsigned DIEBlock::ComputeSize(AsmPrinter *AP) const {
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());
503 /// EmitValue - Emit block data.
505 void DIEBlock::EmitValue(AsmPrinter *Asm, dwarf::Form Form) const {
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;
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());
519 /// SizeOf - Determine size of block data in bytes.
521 unsigned DIEBlock::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
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");
532 void DIEBlock::print(raw_ostream &O) const {
538 //===----------------------------------------------------------------------===//
539 // DIELocList Implementation
540 //===----------------------------------------------------------------------===//
542 unsigned DIELocList::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
543 if (Form == dwarf::DW_FORM_data4)
545 if (Form == dwarf::DW_FORM_sec_offset)
547 return AP->getDataLayout().getPointerSize();
550 /// EmitValue - Emit label value.
552 void DIELocList::EmitValue(AsmPrinter *AP, dwarf::Form Form) const {
553 DwarfDebug *DD = AP->getDwarfDebug();
554 MCSymbol *Label = DD->getDebugLocEntries()[Index].Label;
556 if (AP->MAI->doesDwarfUseRelocationsAcrossSections() && !DD->useSplitDwarf())
557 AP->EmitSectionOffset(Label, DD->getDebugLocSym());
559 AP->EmitLabelDifference(Label, DD->getDebugLocSym(), 4);
563 void DIELocList::print(raw_ostream &O) const {
564 O << "LocList: " << Index;