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);
267 default: llvm_unreachable("DIE Value form not supported yet");
272 void DIEInteger::print(raw_ostream &O) const {
273 O << "Int: " << (int64_t)Integer << " 0x";
274 O.write_hex(Integer);
278 //===----------------------------------------------------------------------===//
279 // DIEExpr Implementation
280 //===----------------------------------------------------------------------===//
282 /// EmitValue - Emit expression value.
284 void DIEExpr::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
285 AP->OutStreamer.EmitValue(Expr, SizeOf(AP, Form));
288 /// SizeOf - Determine size of expression value in bytes.
290 unsigned DIEExpr::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
291 if (Form == dwarf::DW_FORM_data4) return 4;
292 if (Form == dwarf::DW_FORM_sec_offset) return 4;
293 if (Form == dwarf::DW_FORM_strp) return 4;
294 return AP->getDataLayout().getPointerSize();
298 void DIEExpr::print(raw_ostream &O) const {
304 //===----------------------------------------------------------------------===//
305 // DIELabel Implementation
306 //===----------------------------------------------------------------------===//
308 /// EmitValue - Emit label value.
310 void DIELabel::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
311 AP->EmitLabelReference(Label, SizeOf(AP, Form),
312 Form == dwarf::DW_FORM_strp ||
313 Form == dwarf::DW_FORM_sec_offset ||
314 Form == dwarf::DW_FORM_ref_addr);
317 /// SizeOf - Determine size of label value in bytes.
319 unsigned DIELabel::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
320 if (Form == dwarf::DW_FORM_data4) return 4;
321 if (Form == dwarf::DW_FORM_sec_offset) return 4;
322 if (Form == dwarf::DW_FORM_strp) return 4;
323 return AP->getDataLayout().getPointerSize();
327 void DIELabel::print(raw_ostream &O) const {
328 O << "Lbl: " << Label->getName();
332 //===----------------------------------------------------------------------===//
333 // DIEDelta Implementation
334 //===----------------------------------------------------------------------===//
336 /// EmitValue - Emit delta value.
338 void DIEDelta::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
339 AP->EmitLabelDifference(LabelHi, LabelLo, SizeOf(AP, Form));
342 /// SizeOf - Determine size of delta value in bytes.
344 unsigned DIEDelta::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
345 if (Form == dwarf::DW_FORM_data4) return 4;
346 if (Form == dwarf::DW_FORM_sec_offset) return 4;
347 if (Form == dwarf::DW_FORM_strp) return 4;
348 return AP->getDataLayout().getPointerSize();
352 void DIEDelta::print(raw_ostream &O) const {
353 O << "Del: " << LabelHi->getName() << "-" << LabelLo->getName();
357 //===----------------------------------------------------------------------===//
358 // DIEString Implementation
359 //===----------------------------------------------------------------------===//
361 /// EmitValue - Emit string value.
363 void DIEString::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
364 Access->EmitValue(AP, Form);
367 /// SizeOf - Determine size of delta value in bytes.
369 unsigned DIEString::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
370 return Access->SizeOf(AP, Form);
374 void DIEString::print(raw_ostream &O) const {
375 O << "String: " << Str << "\tSymbol: ";
380 //===----------------------------------------------------------------------===//
381 // DIEEntry Implementation
382 //===----------------------------------------------------------------------===//
384 /// Emit something like ".long Hi+Offset-Lo" where the size in bytes of the
385 /// directive is specified by Size and Hi/Lo specify the labels.
386 static void emitLabelOffsetDifference(MCStreamer &Streamer, const MCSymbol *Hi,
387 uint64_t Offset, const MCSymbol *Lo,
389 MCContext &Context = Streamer.getContext();
391 // Emit Hi+Offset - Lo
392 // Get the Hi+Offset expression.
394 MCBinaryExpr::CreateAdd(MCSymbolRefExpr::Create(Hi, Context),
395 MCConstantExpr::Create(Offset, Context), Context);
397 // Get the Hi+Offset-Lo expression.
398 const MCExpr *Diff = MCBinaryExpr::CreateSub(
399 Plus, MCSymbolRefExpr::Create(Lo, Context), Context);
401 // Otherwise, emit with .set (aka assignment).
402 MCSymbol *SetLabel = Context.CreateTempSymbol();
403 Streamer.EmitAssignment(SetLabel, Diff);
404 Streamer.EmitSymbolValue(SetLabel, Size);
407 /// EmitValue - Emit debug information entry offset.
409 void DIEEntry::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
411 if (Form == dwarf::DW_FORM_ref_addr) {
412 const DwarfDebug *DD = AP->getDwarfDebug();
413 unsigned Addr = Entry.getOffset();
414 assert(!DD->useSplitDwarf() && "TODO: dwo files can't have relocations.");
415 // For DW_FORM_ref_addr, output the offset from beginning of debug info
416 // section. Entry->getOffset() returns the offset from start of the
418 DwarfCompileUnit *CU = DD->lookupUnit(Entry.getUnit());
419 assert(CU && "CUDie should belong to a CU.");
420 Addr += CU->getDebugInfoOffset();
421 if (AP->MAI->doesDwarfUseRelocationsAcrossSections())
422 AP->EmitLabelPlusOffset(CU->getSectionSym(), Addr,
423 DIEEntry::getRefAddrSize(AP));
425 emitLabelOffsetDifference(AP->OutStreamer, CU->getSectionSym(), Addr,
427 DIEEntry::getRefAddrSize(AP));
429 AP->EmitInt32(Entry.getOffset());
432 unsigned DIEEntry::getRefAddrSize(const AsmPrinter *AP) {
433 // DWARF4: References that use the attribute form DW_FORM_ref_addr are
434 // specified to be four bytes in the DWARF 32-bit format and eight bytes
435 // in the DWARF 64-bit format, while DWARF Version 2 specifies that such
436 // references have the same size as an address on the target system.
437 const DwarfDebug *DD = AP->getDwarfDebug();
438 assert(DD && "Expected Dwarf Debug info to be available");
439 if (DD->getDwarfVersion() == 2)
440 return AP->getDataLayout().getPointerSize();
441 return sizeof(int32_t);
445 void DIEEntry::print(raw_ostream &O) const {
446 O << format("Die: 0x%lx", (long)(intptr_t)&Entry);
450 //===----------------------------------------------------------------------===//
451 // DIETypeSignature Implementation
452 //===----------------------------------------------------------------------===//
453 void DIETypeSignature::EmitValue(const AsmPrinter *Asm, dwarf::Form Form) const {
454 assert(Form == dwarf::DW_FORM_ref_sig8);
455 Asm->OutStreamer.EmitIntValue(Unit.getTypeSignature(), 8);
459 void DIETypeSignature::print(raw_ostream &O) const {
460 O << format("Type Unit: 0x%lx", Unit.getTypeSignature());
463 void DIETypeSignature::dump() const { print(dbgs()); }
466 //===----------------------------------------------------------------------===//
467 // DIELoc Implementation
468 //===----------------------------------------------------------------------===//
470 /// ComputeSize - calculate the size of the location expression.
472 unsigned DIELoc::ComputeSize(const AsmPrinter *AP) const {
474 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
475 for (unsigned i = 0, N = Values.size(); i < N; ++i)
476 Size += Values[i]->SizeOf(AP, AbbrevData[i].getForm());
482 /// EmitValue - Emit location data.
484 void DIELoc::EmitValue(const AsmPrinter *Asm, dwarf::Form Form) const {
486 default: llvm_unreachable("Improper form for block");
487 case dwarf::DW_FORM_block1: Asm->EmitInt8(Size); break;
488 case dwarf::DW_FORM_block2: Asm->EmitInt16(Size); break;
489 case dwarf::DW_FORM_block4: Asm->EmitInt32(Size); break;
490 case dwarf::DW_FORM_block:
491 case dwarf::DW_FORM_exprloc:
492 Asm->EmitULEB128(Size); break;
495 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
496 for (unsigned i = 0, N = Values.size(); i < N; ++i)
497 Values[i]->EmitValue(Asm, AbbrevData[i].getForm());
500 /// SizeOf - Determine size of location data in bytes.
502 unsigned DIELoc::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
504 case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
505 case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
506 case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
507 case dwarf::DW_FORM_block:
508 case dwarf::DW_FORM_exprloc:
509 return Size + getULEB128Size(Size);
510 default: llvm_unreachable("Improper form for block");
515 void DIELoc::print(raw_ostream &O) const {
521 //===----------------------------------------------------------------------===//
522 // DIEBlock Implementation
523 //===----------------------------------------------------------------------===//
525 /// ComputeSize - calculate the size of the block.
527 unsigned DIEBlock::ComputeSize(const AsmPrinter *AP) const {
529 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
530 for (unsigned i = 0, N = Values.size(); i < N; ++i)
531 Size += Values[i]->SizeOf(AP, AbbrevData[i].getForm());
537 /// EmitValue - Emit block data.
539 void DIEBlock::EmitValue(const AsmPrinter *Asm, dwarf::Form Form) const {
541 default: llvm_unreachable("Improper form for block");
542 case dwarf::DW_FORM_block1: Asm->EmitInt8(Size); break;
543 case dwarf::DW_FORM_block2: Asm->EmitInt16(Size); break;
544 case dwarf::DW_FORM_block4: Asm->EmitInt32(Size); break;
545 case dwarf::DW_FORM_block: Asm->EmitULEB128(Size); break;
548 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
549 for (unsigned i = 0, N = Values.size(); i < N; ++i)
550 Values[i]->EmitValue(Asm, AbbrevData[i].getForm());
553 /// SizeOf - Determine size of block data in bytes.
555 unsigned DIEBlock::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
557 case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
558 case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
559 case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
560 case dwarf::DW_FORM_block: return Size + getULEB128Size(Size);
561 default: llvm_unreachable("Improper form for block");
566 void DIEBlock::print(raw_ostream &O) const {
572 //===----------------------------------------------------------------------===//
573 // DIELocList Implementation
574 //===----------------------------------------------------------------------===//
576 unsigned DIELocList::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
577 if (Form == dwarf::DW_FORM_data4)
579 if (Form == dwarf::DW_FORM_sec_offset)
581 return AP->getDataLayout().getPointerSize();
584 /// EmitValue - Emit label value.
586 void DIELocList::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
587 DwarfDebug *DD = AP->getDwarfDebug();
588 MCSymbol *Label = DD->getDebugLocEntries()[Index].Label;
590 if (AP->MAI->doesDwarfUseRelocationsAcrossSections() && !DD->useSplitDwarf())
591 AP->EmitSectionOffset(Label, DD->getDebugLocSym());
593 AP->EmitLabelDifference(Label, DD->getDebugLocSym(), 4);
597 void DIELocList::print(raw_ostream &O) const {
598 O << "LocList: " << Index;