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(const 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(const 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_strp: // Fall thru
222 case dwarf::DW_FORM_ref4: // Fall thru
223 case dwarf::DW_FORM_data4: Size = 4; break;
224 case dwarf::DW_FORM_ref8: // Fall thru
225 case dwarf::DW_FORM_ref_sig8: // Fall thru
226 case dwarf::DW_FORM_data8: Size = 8; break;
227 case dwarf::DW_FORM_GNU_str_index: Asm->EmitULEB128(Integer); return;
228 case dwarf::DW_FORM_GNU_addr_index: Asm->EmitULEB128(Integer); return;
229 case dwarf::DW_FORM_udata: Asm->EmitULEB128(Integer); return;
230 case dwarf::DW_FORM_sdata: Asm->EmitSLEB128(Integer); return;
231 case dwarf::DW_FORM_addr:
232 Size = Asm->getDataLayout().getPointerSize(); break;
233 case dwarf::DW_FORM_ref_addr:
234 Size = SizeOf(Asm, dwarf::DW_FORM_ref_addr);
236 default: llvm_unreachable("DIE Value form not supported yet");
238 Asm->OutStreamer.EmitIntValue(Integer, Size);
241 /// SizeOf - Determine size of integer value in bytes.
243 unsigned DIEInteger::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
245 case dwarf::DW_FORM_flag_present: return 0;
246 case dwarf::DW_FORM_flag: // Fall thru
247 case dwarf::DW_FORM_ref1: // Fall thru
248 case dwarf::DW_FORM_data1: return sizeof(int8_t);
249 case dwarf::DW_FORM_ref2: // Fall thru
250 case dwarf::DW_FORM_data2: return sizeof(int16_t);
251 case dwarf::DW_FORM_sec_offset: // Fall thru
252 case dwarf::DW_FORM_strp: // Fall thru
253 case dwarf::DW_FORM_ref4: // Fall thru
254 case dwarf::DW_FORM_data4: return sizeof(int32_t);
255 case dwarf::DW_FORM_ref8: // Fall thru
256 case dwarf::DW_FORM_ref_sig8: // Fall thru
257 case dwarf::DW_FORM_data8: return sizeof(int64_t);
258 case dwarf::DW_FORM_GNU_str_index: return getULEB128Size(Integer);
259 case dwarf::DW_FORM_GNU_addr_index: return getULEB128Size(Integer);
260 case dwarf::DW_FORM_udata: return getULEB128Size(Integer);
261 case dwarf::DW_FORM_sdata: return getSLEB128Size(Integer);
262 case dwarf::DW_FORM_addr: return AP->getDataLayout().getPointerSize();
263 case dwarf::DW_FORM_ref_addr:
264 if (AP->OutStreamer.getContext().getDwarfVersion() == 2)
265 return AP->getDataLayout().getPointerSize();
266 return sizeof(int32_t);
268 default: llvm_unreachable("DIE Value form not supported yet");
273 void DIEInteger::print(raw_ostream &O) const {
274 O << "Int: " << (int64_t)Integer << " 0x";
275 O.write_hex(Integer);
279 //===----------------------------------------------------------------------===//
280 // DIEExpr Implementation
281 //===----------------------------------------------------------------------===//
283 /// EmitValue - Emit expression value.
285 void DIEExpr::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
286 AP->OutStreamer.EmitValue(Expr, SizeOf(AP, Form));
289 /// SizeOf - Determine size of expression value in bytes.
291 unsigned DIEExpr::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
292 if (Form == dwarf::DW_FORM_data4) return 4;
293 if (Form == dwarf::DW_FORM_sec_offset) return 4;
294 if (Form == dwarf::DW_FORM_strp) return 4;
295 return AP->getDataLayout().getPointerSize();
299 void DIEExpr::print(raw_ostream &O) const {
305 //===----------------------------------------------------------------------===//
306 // DIELabel Implementation
307 //===----------------------------------------------------------------------===//
309 /// EmitValue - Emit label value.
311 void DIELabel::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
312 AP->EmitLabelReference(Label, SizeOf(AP, Form),
313 Form == dwarf::DW_FORM_strp ||
314 Form == dwarf::DW_FORM_sec_offset ||
315 Form == dwarf::DW_FORM_ref_addr);
318 /// SizeOf - Determine size of label value in bytes.
320 unsigned DIELabel::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
321 if (Form == dwarf::DW_FORM_data4) return 4;
322 if (Form == dwarf::DW_FORM_sec_offset) return 4;
323 if (Form == dwarf::DW_FORM_strp) return 4;
324 return AP->getDataLayout().getPointerSize();
328 void DIELabel::print(raw_ostream &O) const {
329 O << "Lbl: " << Label->getName();
333 //===----------------------------------------------------------------------===//
334 // DIEDelta Implementation
335 //===----------------------------------------------------------------------===//
337 /// EmitValue - Emit delta value.
339 void DIEDelta::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
340 AP->EmitLabelDifference(LabelHi, LabelLo, SizeOf(AP, Form));
343 /// SizeOf - Determine size of delta value in bytes.
345 unsigned DIEDelta::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
346 if (Form == dwarf::DW_FORM_data4) return 4;
347 if (Form == dwarf::DW_FORM_sec_offset) return 4;
348 if (Form == dwarf::DW_FORM_strp) return 4;
349 return AP->getDataLayout().getPointerSize();
353 void DIEDelta::print(raw_ostream &O) const {
354 O << "Del: " << LabelHi->getName() << "-" << LabelLo->getName();
358 //===----------------------------------------------------------------------===//
359 // DIEString Implementation
360 //===----------------------------------------------------------------------===//
362 /// EmitValue - Emit string value.
364 void DIEString::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
365 Access->EmitValue(AP, Form);
368 /// SizeOf - Determine size of delta value in bytes.
370 unsigned DIEString::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
371 return Access->SizeOf(AP, Form);
375 void DIEString::print(raw_ostream &O) const {
376 O << "String: " << Str << "\tSymbol: ";
381 //===----------------------------------------------------------------------===//
382 // DIEEntry Implementation
383 //===----------------------------------------------------------------------===//
385 /// Emit something like ".long Hi+Offset-Lo" where the size in bytes of the
386 /// directive is specified by Size and Hi/Lo specify the labels.
387 static void emitLabelOffsetDifference(MCStreamer &Streamer, const MCSymbol *Hi,
388 uint64_t Offset, const MCSymbol *Lo,
390 MCContext &Context = Streamer.getContext();
392 // Emit Hi+Offset - Lo
393 // Get the Hi+Offset expression.
395 MCBinaryExpr::CreateAdd(MCSymbolRefExpr::Create(Hi, Context),
396 MCConstantExpr::Create(Offset, Context), Context);
398 // Get the Hi+Offset-Lo expression.
399 const MCExpr *Diff = MCBinaryExpr::CreateSub(
400 Plus, MCSymbolRefExpr::Create(Lo, Context), Context);
402 // Otherwise, emit with .set (aka assignment).
403 MCSymbol *SetLabel = Context.CreateTempSymbol();
404 Streamer.EmitAssignment(SetLabel, Diff);
405 Streamer.EmitSymbolValue(SetLabel, Size);
408 /// EmitValue - Emit debug information entry offset.
410 void DIEEntry::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
412 if (Form == dwarf::DW_FORM_ref_addr) {
413 const DwarfDebug *DD = AP->getDwarfDebug();
414 unsigned Addr = Entry.getOffset();
415 assert(!DD->useSplitDwarf() && "TODO: dwo files can't have relocations.");
416 // For DW_FORM_ref_addr, output the offset from beginning of debug info
417 // section. Entry->getOffset() returns the offset from start of the
419 DwarfCompileUnit *CU = DD->lookupUnit(Entry.getUnit());
420 assert(CU && "CUDie should belong to a CU.");
421 Addr += CU->getDebugInfoOffset();
422 if (AP->MAI->doesDwarfUseRelocationsAcrossSections())
423 AP->EmitLabelPlusOffset(CU->getSectionSym(), Addr,
424 DIEEntry::getRefAddrSize(AP));
426 emitLabelOffsetDifference(AP->OutStreamer, CU->getSectionSym(), Addr,
428 DIEEntry::getRefAddrSize(AP));
430 AP->EmitInt32(Entry.getOffset());
433 unsigned DIEEntry::getRefAddrSize(const AsmPrinter *AP) {
434 // DWARF4: References that use the attribute form DW_FORM_ref_addr are
435 // specified to be four bytes in the DWARF 32-bit format and eight bytes
436 // in the DWARF 64-bit format, while DWARF Version 2 specifies that such
437 // references have the same size as an address on the target system.
438 const DwarfDebug *DD = AP->getDwarfDebug();
439 assert(DD && "Expected Dwarf Debug info to be available");
440 if (DD->getDwarfVersion() == 2)
441 return AP->getDataLayout().getPointerSize();
442 return sizeof(int32_t);
446 void DIEEntry::print(raw_ostream &O) const {
447 O << format("Die: 0x%lx", (long)(intptr_t)&Entry);
451 //===----------------------------------------------------------------------===//
452 // DIETypeSignature Implementation
453 //===----------------------------------------------------------------------===//
454 void DIETypeSignature::EmitValue(const AsmPrinter *Asm, dwarf::Form Form) const {
455 assert(Form == dwarf::DW_FORM_ref_sig8);
456 Asm->OutStreamer.EmitIntValue(Unit.getTypeSignature(), 8);
460 void DIETypeSignature::print(raw_ostream &O) const {
461 O << format("Type Unit: 0x%lx", Unit.getTypeSignature());
464 void DIETypeSignature::dump() const { print(dbgs()); }
467 //===----------------------------------------------------------------------===//
468 // DIELoc Implementation
469 //===----------------------------------------------------------------------===//
471 /// ComputeSize - calculate the size of the location expression.
473 unsigned DIELoc::ComputeSize(const AsmPrinter *AP) const {
475 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
476 for (unsigned i = 0, N = Values.size(); i < N; ++i)
477 Size += Values[i]->SizeOf(AP, AbbrevData[i].getForm());
483 /// EmitValue - Emit location data.
485 void DIELoc::EmitValue(const AsmPrinter *Asm, dwarf::Form Form) const {
487 default: llvm_unreachable("Improper form for block");
488 case dwarf::DW_FORM_block1: Asm->EmitInt8(Size); break;
489 case dwarf::DW_FORM_block2: Asm->EmitInt16(Size); break;
490 case dwarf::DW_FORM_block4: Asm->EmitInt32(Size); break;
491 case dwarf::DW_FORM_block:
492 case dwarf::DW_FORM_exprloc:
493 Asm->EmitULEB128(Size); break;
496 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
497 for (unsigned i = 0, N = Values.size(); i < N; ++i)
498 Values[i]->EmitValue(Asm, AbbrevData[i].getForm());
501 /// SizeOf - Determine size of location data in bytes.
503 unsigned DIELoc::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
505 case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
506 case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
507 case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
508 case dwarf::DW_FORM_block:
509 case dwarf::DW_FORM_exprloc:
510 return Size + getULEB128Size(Size);
511 default: llvm_unreachable("Improper form for block");
516 void DIELoc::print(raw_ostream &O) const {
522 //===----------------------------------------------------------------------===//
523 // DIEBlock Implementation
524 //===----------------------------------------------------------------------===//
526 /// ComputeSize - calculate the size of the block.
528 unsigned DIEBlock::ComputeSize(const AsmPrinter *AP) const {
530 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
531 for (unsigned i = 0, N = Values.size(); i < N; ++i)
532 Size += Values[i]->SizeOf(AP, AbbrevData[i].getForm());
538 /// EmitValue - Emit block data.
540 void DIEBlock::EmitValue(const AsmPrinter *Asm, dwarf::Form Form) const {
542 default: llvm_unreachable("Improper form for block");
543 case dwarf::DW_FORM_block1: Asm->EmitInt8(Size); break;
544 case dwarf::DW_FORM_block2: Asm->EmitInt16(Size); break;
545 case dwarf::DW_FORM_block4: Asm->EmitInt32(Size); break;
546 case dwarf::DW_FORM_block: Asm->EmitULEB128(Size); break;
549 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
550 for (unsigned i = 0, N = Values.size(); i < N; ++i)
551 Values[i]->EmitValue(Asm, AbbrevData[i].getForm());
554 /// SizeOf - Determine size of block data in bytes.
556 unsigned DIEBlock::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
558 case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
559 case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
560 case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
561 case dwarf::DW_FORM_block: return Size + getULEB128Size(Size);
562 default: llvm_unreachable("Improper form for block");
567 void DIEBlock::print(raw_ostream &O) const {
573 //===----------------------------------------------------------------------===//
574 // DIELocList Implementation
575 //===----------------------------------------------------------------------===//
577 unsigned DIELocList::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
578 if (Form == dwarf::DW_FORM_data4)
580 if (Form == dwarf::DW_FORM_sec_offset)
582 return AP->getDataLayout().getPointerSize();
585 /// EmitValue - Emit label value.
587 void DIELocList::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
588 DwarfDebug *DD = AP->getDwarfDebug();
589 MCSymbol *Label = DD->getDebugLocEntries()[Index].Label;
591 if (AP->MAI->doesDwarfUseRelocationsAcrossSections() && !DD->useSplitDwarf())
592 AP->EmitSectionOffset(Label, DD->getDebugLocSym());
594 AP->EmitLabelDifference(Label, DD->getDebugLocSym(), 4);
598 void DIELocList::print(raw_ostream &O) const {
599 O << "LocList: " << Index;