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"
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/MCContext.h"
24 #include "llvm/MC/MCStreamer.h"
25 #include "llvm/MC/MCSymbol.h"
26 #include "llvm/Support/Debug.h"
27 #include "llvm/Support/ErrorHandling.h"
28 #include "llvm/Support/Format.h"
29 #include "llvm/Support/FormattedStream.h"
30 #include "llvm/Support/LEB128.h"
31 #include "llvm/Support/MD5.h"
34 //===----------------------------------------------------------------------===//
35 // DIEAbbrevData Implementation
36 //===----------------------------------------------------------------------===//
38 /// Profile - Used to gather unique data for the abbreviation folding set.
40 void DIEAbbrevData::Profile(FoldingSetNodeID &ID) const {
41 // Explicitly cast to an integer type for which FoldingSetNodeID has
42 // overloads. Otherwise MSVC 2010 thinks this call is ambiguous.
43 ID.AddInteger(unsigned(Attribute));
44 ID.AddInteger(unsigned(Form));
47 //===----------------------------------------------------------------------===//
48 // DIEAbbrev Implementation
49 //===----------------------------------------------------------------------===//
51 /// Profile - Used to gather unique data for the abbreviation folding set.
53 void DIEAbbrev::Profile(FoldingSetNodeID &ID) const {
54 ID.AddInteger(unsigned(Tag));
55 ID.AddInteger(unsigned(Children));
57 // For each attribute description.
58 for (unsigned i = 0, N = Data.size(); i < N; ++i)
62 /// Emit - Print the abbreviation using the specified asm printer.
64 void DIEAbbrev::Emit(AsmPrinter *AP) const {
65 // Emit its Dwarf tag type.
66 AP->EmitULEB128(Tag, dwarf::TagString(Tag));
68 // Emit whether it has children DIEs.
69 AP->EmitULEB128((unsigned)Children, dwarf::ChildrenString(Children));
71 // For each attribute description.
72 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
73 const DIEAbbrevData &AttrData = Data[i];
75 // Emit attribute type.
76 AP->EmitULEB128(AttrData.getAttribute(),
77 dwarf::AttributeString(AttrData.getAttribute()));
80 AP->EmitULEB128(AttrData.getForm(),
81 dwarf::FormEncodingString(AttrData.getForm()));
84 // Mark end of abbreviation.
85 AP->EmitULEB128(0, "EOM(1)");
86 AP->EmitULEB128(0, "EOM(2)");
90 void DIEAbbrev::print(raw_ostream &O) {
92 << format("0x%lx", (long)(intptr_t)this)
94 << dwarf::TagString(Tag)
96 << dwarf::ChildrenString(Children)
99 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
101 << dwarf::AttributeString(Data[i].getAttribute())
103 << dwarf::FormEncodingString(Data[i].getForm())
107 void DIEAbbrev::dump() { print(dbgs()); }
110 /// Climb up the parent chain to get the unit DIE to which this DIE
112 const DIE *DIE::getUnit() const {
113 const DIE *Cu = getUnitOrNull();
114 assert(Cu && "We should not have orphaned DIEs.");
118 /// Climb up the parent chain to get the unit DIE this DIE belongs
119 /// to. Return NULL if DIE is not added to an owner yet.
120 const DIE *DIE::getUnitOrNull() const {
123 if (p->getTag() == dwarf::DW_TAG_compile_unit ||
124 p->getTag() == dwarf::DW_TAG_type_unit)
131 DIEValue *DIE::findAttribute(dwarf::Attribute Attribute) const {
132 const SmallVectorImpl<DIEValue *> &Values = getValues();
133 const DIEAbbrev &Abbrevs = getAbbrev();
135 // Iterate through all the attributes until we find the one we're
136 // looking for, if we can't find it return NULL.
137 for (size_t i = 0; i < Values.size(); ++i)
138 if (Abbrevs.getData()[i].getAttribute() == Attribute)
144 void DIE::print(raw_ostream &O, unsigned IndentCount) const {
145 const std::string Indent(IndentCount, ' ');
146 bool isBlock = Abbrev.getTag() == 0;
151 << format("0x%lx", (long)(intptr_t)this)
152 << ", Offset: " << Offset
153 << ", Size: " << Size << "\n";
156 << dwarf::TagString(Abbrev.getTag())
158 << dwarf::ChildrenString(Abbrev.hasChildren()) << "\n";
160 O << "Size: " << Size << "\n";
163 const SmallVectorImpl<DIEAbbrevData> &Data = Abbrev.getData();
166 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
170 O << dwarf::AttributeString(Data[i].getAttribute());
172 O << "Blk[" << i << "]";
175 << dwarf::FormEncodingString(Data[i].getForm())
182 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
183 Children[j]->print(O, IndentCount+4);
186 if (!isBlock) O << "\n";
194 void DIEValue::anchor() { }
197 void DIEValue::dump() const {
202 //===----------------------------------------------------------------------===//
203 // DIEInteger Implementation
204 //===----------------------------------------------------------------------===//
206 /// EmitValue - Emit integer of appropriate size.
208 void DIEInteger::EmitValue(AsmPrinter *Asm, dwarf::Form Form) const {
211 case dwarf::DW_FORM_flag_present:
212 // Emit something to keep the lines and comments in sync.
213 // FIXME: Is there a better way to do this?
214 Asm->OutStreamer.AddBlankLine();
216 case dwarf::DW_FORM_flag: // Fall thru
217 case dwarf::DW_FORM_ref1: // Fall thru
218 case dwarf::DW_FORM_data1: Size = 1; break;
219 case dwarf::DW_FORM_ref2: // Fall thru
220 case dwarf::DW_FORM_data2: Size = 2; break;
221 case dwarf::DW_FORM_sec_offset: // 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 default: llvm_unreachable("DIE Value form not supported yet");
235 Asm->OutStreamer.EmitIntValue(Integer, Size);
238 /// SizeOf - Determine size of integer value in bytes.
240 unsigned DIEInteger::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
242 case dwarf::DW_FORM_flag_present: return 0;
243 case dwarf::DW_FORM_flag: // Fall thru
244 case dwarf::DW_FORM_ref1: // Fall thru
245 case dwarf::DW_FORM_data1: return sizeof(int8_t);
246 case dwarf::DW_FORM_ref2: // Fall thru
247 case dwarf::DW_FORM_data2: return sizeof(int16_t);
248 case dwarf::DW_FORM_sec_offset: // Fall thru
249 case dwarf::DW_FORM_ref4: // Fall thru
250 case dwarf::DW_FORM_data4: return sizeof(int32_t);
251 case dwarf::DW_FORM_ref8: // Fall thru
252 case dwarf::DW_FORM_ref_sig8: // Fall thru
253 case dwarf::DW_FORM_data8: return sizeof(int64_t);
254 case dwarf::DW_FORM_GNU_str_index: return getULEB128Size(Integer);
255 case dwarf::DW_FORM_GNU_addr_index: return getULEB128Size(Integer);
256 case dwarf::DW_FORM_udata: return getULEB128Size(Integer);
257 case dwarf::DW_FORM_sdata: return getSLEB128Size(Integer);
258 case dwarf::DW_FORM_addr: return AP->getDataLayout().getPointerSize();
259 default: llvm_unreachable("DIE Value form not supported yet");
264 void DIEInteger::print(raw_ostream &O) const {
265 O << "Int: " << (int64_t)Integer << " 0x";
266 O.write_hex(Integer);
270 //===----------------------------------------------------------------------===//
271 // DIEExpr Implementation
272 //===----------------------------------------------------------------------===//
274 /// EmitValue - Emit expression value.
276 void DIEExpr::EmitValue(AsmPrinter *AP, dwarf::Form Form) const {
277 AP->OutStreamer.EmitValue(Expr, SizeOf(AP, Form));
280 /// SizeOf - Determine size of expression value in bytes.
282 unsigned DIEExpr::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
283 if (Form == dwarf::DW_FORM_data4) return 4;
284 if (Form == dwarf::DW_FORM_sec_offset) return 4;
285 if (Form == dwarf::DW_FORM_strp) return 4;
286 return AP->getDataLayout().getPointerSize();
290 void DIEExpr::print(raw_ostream &O) const {
296 //===----------------------------------------------------------------------===//
297 // DIELabel Implementation
298 //===----------------------------------------------------------------------===//
300 /// EmitValue - Emit label value.
302 void DIELabel::EmitValue(AsmPrinter *AP, dwarf::Form Form) const {
303 AP->EmitLabelReference(Label, SizeOf(AP, Form),
304 Form == dwarf::DW_FORM_strp ||
305 Form == dwarf::DW_FORM_sec_offset ||
306 Form == dwarf::DW_FORM_ref_addr);
309 /// SizeOf - Determine size of label value in bytes.
311 unsigned DIELabel::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
312 if (Form == dwarf::DW_FORM_data4) return 4;
313 if (Form == dwarf::DW_FORM_sec_offset) return 4;
314 if (Form == dwarf::DW_FORM_strp) return 4;
315 return AP->getDataLayout().getPointerSize();
319 void DIELabel::print(raw_ostream &O) const {
320 O << "Lbl: " << Label->getName();
324 //===----------------------------------------------------------------------===//
325 // DIEDelta Implementation
326 //===----------------------------------------------------------------------===//
328 /// EmitValue - Emit delta value.
330 void DIEDelta::EmitValue(AsmPrinter *AP, dwarf::Form Form) const {
331 AP->EmitLabelDifference(LabelHi, LabelLo, SizeOf(AP, Form));
334 /// SizeOf - Determine size of delta value in bytes.
336 unsigned DIEDelta::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
337 if (Form == dwarf::DW_FORM_data4) return 4;
338 if (Form == dwarf::DW_FORM_sec_offset) return 4;
339 if (Form == dwarf::DW_FORM_strp) return 4;
340 return AP->getDataLayout().getPointerSize();
344 void DIEDelta::print(raw_ostream &O) const {
345 O << "Del: " << LabelHi->getName() << "-" << LabelLo->getName();
349 //===----------------------------------------------------------------------===//
350 // DIEString Implementation
351 //===----------------------------------------------------------------------===//
353 /// EmitValue - Emit string value.
355 void DIEString::EmitValue(AsmPrinter *AP, dwarf::Form Form) const {
356 Access->EmitValue(AP, Form);
359 /// SizeOf - Determine size of delta value in bytes.
361 unsigned DIEString::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
362 return Access->SizeOf(AP, Form);
366 void DIEString::print(raw_ostream &O) const {
367 O << "String: " << Str << "\tSymbol: ";
372 //===----------------------------------------------------------------------===//
373 // DIEEntry Implementation
374 //===----------------------------------------------------------------------===//
376 /// Emit something like ".long Hi+Offset-Lo" where the size in bytes of the
377 /// directive is specified by Size and Hi/Lo specify the labels.
378 static void emitLabelOffsetDifference(MCStreamer &Streamer, const MCSymbol *Hi,
379 uint64_t Offset, const MCSymbol *Lo,
381 MCContext &Context = Streamer.getContext();
383 // Emit Hi+Offset - Lo
384 // Get the Hi+Offset expression.
386 MCBinaryExpr::CreateAdd(MCSymbolRefExpr::Create(Hi, Context),
387 MCConstantExpr::Create(Offset, Context), Context);
389 // Get the Hi+Offset-Lo expression.
390 const MCExpr *Diff = MCBinaryExpr::CreateSub(
391 Plus, MCSymbolRefExpr::Create(Lo, Context), Context);
393 // Otherwise, emit with .set (aka assignment).
394 MCSymbol *SetLabel = Context.CreateTempSymbol();
395 Streamer.EmitAssignment(SetLabel, Diff);
396 Streamer.EmitSymbolValue(SetLabel, Size);
399 /// EmitValue - Emit debug information entry offset.
401 void DIEEntry::EmitValue(AsmPrinter *AP, dwarf::Form Form) const {
403 if (Form == dwarf::DW_FORM_ref_addr) {
404 const DwarfDebug *DD = AP->getDwarfDebug();
405 unsigned Addr = Entry.getOffset();
406 assert(!DD->useSplitDwarf() && "TODO: dwo files can't have relocations.");
407 // For DW_FORM_ref_addr, output the offset from beginning of debug info
408 // section. Entry->getOffset() returns the offset from start of the
410 DwarfCompileUnit *CU = DD->lookupUnit(Entry.getUnit());
411 assert(CU && "CUDie should belong to a CU.");
412 Addr += CU->getDebugInfoOffset();
413 if (AP->MAI->doesDwarfUseRelocationsAcrossSections())
414 AP->EmitLabelPlusOffset(CU->getSectionSym(), Addr,
415 DIEEntry::getRefAddrSize(AP));
417 emitLabelOffsetDifference(AP->OutStreamer, CU->getSectionSym(), Addr,
419 DIEEntry::getRefAddrSize(AP));
421 AP->EmitInt32(Entry.getOffset());
424 unsigned DIEEntry::getRefAddrSize(AsmPrinter *AP) {
425 // DWARF4: References that use the attribute form DW_FORM_ref_addr are
426 // specified to be four bytes in the DWARF 32-bit format and eight bytes
427 // in the DWARF 64-bit format, while DWARF Version 2 specifies that such
428 // references have the same size as an address on the target system.
429 const DwarfDebug *DD = AP->getDwarfDebug();
430 assert(DD && "Expected Dwarf Debug info to be available");
431 if (DD->getDwarfVersion() == 2)
432 return AP->getDataLayout().getPointerSize();
433 return sizeof(int32_t);
437 void DIEEntry::print(raw_ostream &O) const {
438 O << format("Die: 0x%lx", (long)(intptr_t)&Entry);
442 //===----------------------------------------------------------------------===//
443 // DIETypeSignature Implementation
444 //===----------------------------------------------------------------------===//
445 void DIETypeSignature::EmitValue(AsmPrinter *Asm, dwarf::Form Form) const {
446 assert(Form == dwarf::DW_FORM_ref_sig8);
447 Asm->OutStreamer.EmitIntValue(Unit.getTypeSignature(), 8);
451 void DIETypeSignature::print(raw_ostream &O) const {
452 O << format("Type Unit: 0x%lx", Unit.getTypeSignature());
455 void DIETypeSignature::dump() const { print(dbgs()); }
458 //===----------------------------------------------------------------------===//
459 // DIELoc Implementation
460 //===----------------------------------------------------------------------===//
462 /// ComputeSize - calculate the size of the location expression.
464 unsigned DIELoc::ComputeSize(AsmPrinter *AP) const {
466 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
467 for (unsigned i = 0, N = Values.size(); i < N; ++i)
468 Size += Values[i]->SizeOf(AP, AbbrevData[i].getForm());
474 /// EmitValue - Emit location data.
476 void DIELoc::EmitValue(AsmPrinter *Asm, dwarf::Form Form) const {
478 default: llvm_unreachable("Improper form for block");
479 case dwarf::DW_FORM_block1: Asm->EmitInt8(Size); break;
480 case dwarf::DW_FORM_block2: Asm->EmitInt16(Size); break;
481 case dwarf::DW_FORM_block4: Asm->EmitInt32(Size); break;
482 case dwarf::DW_FORM_block:
483 case dwarf::DW_FORM_exprloc:
484 Asm->EmitULEB128(Size); break;
487 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
488 for (unsigned i = 0, N = Values.size(); i < N; ++i)
489 Values[i]->EmitValue(Asm, AbbrevData[i].getForm());
492 /// SizeOf - Determine size of location data in bytes.
494 unsigned DIELoc::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
496 case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
497 case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
498 case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
499 case dwarf::DW_FORM_block:
500 case dwarf::DW_FORM_exprloc:
501 return Size + getULEB128Size(Size);
502 default: llvm_unreachable("Improper form for block");
507 void DIELoc::print(raw_ostream &O) const {
513 //===----------------------------------------------------------------------===//
514 // DIEBlock Implementation
515 //===----------------------------------------------------------------------===//
517 /// ComputeSize - calculate the size of the block.
519 unsigned DIEBlock::ComputeSize(AsmPrinter *AP) const {
521 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
522 for (unsigned i = 0, N = Values.size(); i < N; ++i)
523 Size += Values[i]->SizeOf(AP, AbbrevData[i].getForm());
529 /// EmitValue - Emit block data.
531 void DIEBlock::EmitValue(AsmPrinter *Asm, dwarf::Form Form) const {
533 default: llvm_unreachable("Improper form for block");
534 case dwarf::DW_FORM_block1: Asm->EmitInt8(Size); break;
535 case dwarf::DW_FORM_block2: Asm->EmitInt16(Size); break;
536 case dwarf::DW_FORM_block4: Asm->EmitInt32(Size); break;
537 case dwarf::DW_FORM_block: Asm->EmitULEB128(Size); break;
540 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
541 for (unsigned i = 0, N = Values.size(); i < N; ++i)
542 Values[i]->EmitValue(Asm, AbbrevData[i].getForm());
545 /// SizeOf - Determine size of block data in bytes.
547 unsigned DIEBlock::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
549 case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
550 case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
551 case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
552 case dwarf::DW_FORM_block: return Size + getULEB128Size(Size);
553 default: llvm_unreachable("Improper form for block");
558 void DIEBlock::print(raw_ostream &O) const {
564 //===----------------------------------------------------------------------===//
565 // DIELocList Implementation
566 //===----------------------------------------------------------------------===//
568 unsigned DIELocList::SizeOf(AsmPrinter *AP, dwarf::Form Form) const {
569 if (Form == dwarf::DW_FORM_data4)
571 if (Form == dwarf::DW_FORM_sec_offset)
573 return AP->getDataLayout().getPointerSize();
576 /// EmitValue - Emit label value.
578 void DIELocList::EmitValue(AsmPrinter *AP, dwarf::Form Form) const {
579 DwarfDebug *DD = AP->getDwarfDebug();
580 MCSymbol *Label = DD->getDebugLocEntries()[Index].Label;
582 if (AP->MAI->doesDwarfUseRelocationsAcrossSections() && !DD->useSplitDwarf())
583 AP->EmitSectionOffset(Label, DD->getDebugLocSym());
585 AP->EmitLabelDifference(Label, DD->getDebugLocSym(), 4);
589 void DIELocList::print(raw_ostream &O) const {
590 O << "LocList: " << Index;