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"
31 #include "llvm/Support/raw_ostream.h"
34 //===----------------------------------------------------------------------===//
35 // EmittingAsmStreamer Implementation
36 //===----------------------------------------------------------------------===//
37 unsigned EmittingAsmStreamer::emitULEB128(uint64_t Value, const char *Desc,
39 AP->EmitULEB128(Value, Desc, PadTo);
43 unsigned EmittingAsmStreamer::emitInt8(unsigned char Value) {
48 unsigned EmittingAsmStreamer::emitBytes(StringRef Data) {
49 AP->OutStreamer->EmitBytes(Data);
53 //===----------------------------------------------------------------------===//
54 // SizeReporterAsmStreamer Implementation
55 //===----------------------------------------------------------------------===//
56 unsigned SizeReporterAsmStreamer::emitULEB128(uint64_t Value, const char *Desc,
58 return getULEB128Size(Value);
61 unsigned SizeReporterAsmStreamer::emitInt8(unsigned char Value) { return 1; }
63 unsigned SizeReporterAsmStreamer::emitBytes(StringRef Data) {
67 //===----------------------------------------------------------------------===//
68 // DIEAbbrevData Implementation
69 //===----------------------------------------------------------------------===//
71 /// Profile - Used to gather unique data for the abbreviation folding set.
73 void DIEAbbrevData::Profile(FoldingSetNodeID &ID) const {
74 // Explicitly cast to an integer type for which FoldingSetNodeID has
75 // overloads. Otherwise MSVC 2010 thinks this call is ambiguous.
76 ID.AddInteger(unsigned(Attribute));
77 ID.AddInteger(unsigned(Form));
80 //===----------------------------------------------------------------------===//
81 // DIEAbbrev Implementation
82 //===----------------------------------------------------------------------===//
84 /// Profile - Used to gather unique data for the abbreviation folding set.
86 void DIEAbbrev::Profile(FoldingSetNodeID &ID) const {
87 ID.AddInteger(unsigned(Tag));
88 ID.AddInteger(unsigned(Children));
90 // For each attribute description.
91 for (unsigned i = 0, N = Data.size(); i < N; ++i)
95 /// Emit - Print the abbreviation using the specified asm printer.
97 void DIEAbbrev::Emit(const AsmPrinter *AP) const {
98 // Emit its Dwarf tag type.
99 AP->EmitULEB128(Tag, dwarf::TagString(Tag));
101 // Emit whether it has children DIEs.
102 AP->EmitULEB128((unsigned)Children, dwarf::ChildrenString(Children));
104 // For each attribute description.
105 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
106 const DIEAbbrevData &AttrData = Data[i];
108 // Emit attribute type.
109 AP->EmitULEB128(AttrData.getAttribute(),
110 dwarf::AttributeString(AttrData.getAttribute()));
113 AP->EmitULEB128(AttrData.getForm(),
114 dwarf::FormEncodingString(AttrData.getForm()));
117 // Mark end of abbreviation.
118 AP->EmitULEB128(0, "EOM(1)");
119 AP->EmitULEB128(0, "EOM(2)");
123 void DIEAbbrev::print(raw_ostream &O) {
124 O << "Abbreviation @"
125 << format("0x%lx", (long)(intptr_t)this)
127 << dwarf::TagString(Tag)
129 << dwarf::ChildrenString(Children)
132 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
134 << dwarf::AttributeString(Data[i].getAttribute())
136 << dwarf::FormEncodingString(Data[i].getForm())
142 void DIEAbbrev::dump() { print(dbgs()); }
144 DIEAbbrev DIE::generateAbbrev() const {
145 DIEAbbrev Abbrev(Tag, hasChildren());
146 for (const DIEValue &V : values())
147 Abbrev.AddAttribute(V.getAttribute(), V.getForm());
151 /// Climb up the parent chain to get the unit DIE to which this DIE
153 const DIE *DIE::getUnit() const {
154 const DIE *Cu = getUnitOrNull();
155 assert(Cu && "We should not have orphaned DIEs.");
159 /// Climb up the parent chain to get the unit DIE this DIE belongs
160 /// to. Return NULL if DIE is not added to an owner yet.
161 const DIE *DIE::getUnitOrNull() const {
164 if (p->getTag() == dwarf::DW_TAG_compile_unit ||
165 p->getTag() == dwarf::DW_TAG_type_unit)
172 DIEValue DIE::findAttribute(dwarf::Attribute Attribute) const {
173 // Iterate through all the attributes until we find the one we're
174 // looking for, if we can't find it return NULL.
175 for (const auto &V : values())
176 if (V.getAttribute() == Attribute)
182 static void printValues(raw_ostream &O, const DIEValueList &Values,
183 StringRef Type, unsigned Size, unsigned IndentCount) {
184 O << Type << ": Size: " << Size << "\n";
187 const std::string Indent(IndentCount, ' ');
188 for (const auto &V : Values.values()) {
190 O << "Blk[" << I++ << "]";
191 O << " " << dwarf::FormEncodingString(V.getForm()) << " ";
198 void DIE::print(raw_ostream &O, unsigned IndentCount) const {
199 const std::string Indent(IndentCount, ' ');
200 O << Indent << "Die: " << format("0x%lx", (long)(intptr_t) this)
201 << ", Offset: " << Offset << ", Size: " << Size << "\n";
203 O << Indent << dwarf::TagString(getTag()) << " "
204 << dwarf::ChildrenString(hasChildren()) << "\n";
207 for (const auto &V : values()) {
209 O << dwarf::AttributeString(V.getAttribute());
210 O << " " << dwarf::FormEncodingString(V.getForm()) << " ";
216 for (const auto &Child : children())
217 Child.print(O, IndentCount + 4);
227 void DIEValue::EmitValue(const AsmPrinter *AP) const {
230 llvm_unreachable("Expected valid DIEValue");
231 #define HANDLE_DIEVALUE(T) \
233 getDIE##T().EmitValue(AP, Form); \
235 #include "llvm/CodeGen/DIEValue.def"
239 unsigned DIEValue::SizeOf(const AsmPrinter *AP) const {
242 llvm_unreachable("Expected valid DIEValue");
243 #define HANDLE_DIEVALUE(T) \
245 return getDIE##T().SizeOf(AP, Form);
246 #include "llvm/CodeGen/DIEValue.def"
248 llvm_unreachable("Unknown DIE kind");
252 void DIEValue::print(raw_ostream &O) const {
255 llvm_unreachable("Expected valid DIEValue");
256 #define HANDLE_DIEVALUE(T) \
258 getDIE##T().print(O); \
260 #include "llvm/CodeGen/DIEValue.def"
265 void DIEValue::dump() const {
269 //===----------------------------------------------------------------------===//
270 // DIEInteger Implementation
271 //===----------------------------------------------------------------------===//
273 /// EmitValue - Emit integer of appropriate size.
275 void DIEInteger::EmitValue(const AsmPrinter *Asm, dwarf::Form Form) const {
278 case dwarf::DW_FORM_flag_present:
279 // Emit something to keep the lines and comments in sync.
280 // FIXME: Is there a better way to do this?
281 Asm->OutStreamer->AddBlankLine();
283 case dwarf::DW_FORM_flag: // Fall thru
284 case dwarf::DW_FORM_ref1: // Fall thru
285 case dwarf::DW_FORM_data1: Size = 1; break;
286 case dwarf::DW_FORM_ref2: // Fall thru
287 case dwarf::DW_FORM_data2: Size = 2; break;
288 case dwarf::DW_FORM_sec_offset: // Fall thru
289 case dwarf::DW_FORM_strp: // Fall thru
290 case dwarf::DW_FORM_ref4: // Fall thru
291 case dwarf::DW_FORM_data4: Size = 4; break;
292 case dwarf::DW_FORM_ref8: // Fall thru
293 case dwarf::DW_FORM_ref_sig8: // Fall thru
294 case dwarf::DW_FORM_data8: Size = 8; break;
295 case dwarf::DW_FORM_GNU_str_index: Asm->EmitULEB128(Integer); return;
296 case dwarf::DW_FORM_GNU_addr_index: Asm->EmitULEB128(Integer); return;
297 case dwarf::DW_FORM_udata: Asm->EmitULEB128(Integer); return;
298 case dwarf::DW_FORM_sdata: Asm->EmitSLEB128(Integer); return;
299 case dwarf::DW_FORM_addr:
300 Size = Asm->getPointerSize();
302 case dwarf::DW_FORM_ref_addr:
303 Size = SizeOf(Asm, dwarf::DW_FORM_ref_addr);
305 default: llvm_unreachable("DIE Value form not supported yet");
307 Asm->OutStreamer->EmitIntValue(Integer, Size);
310 /// SizeOf - Determine size of integer value in bytes.
312 unsigned DIEInteger::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
314 case dwarf::DW_FORM_flag_present: return 0;
315 case dwarf::DW_FORM_flag: // Fall thru
316 case dwarf::DW_FORM_ref1: // Fall thru
317 case dwarf::DW_FORM_data1: return sizeof(int8_t);
318 case dwarf::DW_FORM_ref2: // Fall thru
319 case dwarf::DW_FORM_data2: return sizeof(int16_t);
320 case dwarf::DW_FORM_sec_offset: // Fall thru
321 case dwarf::DW_FORM_strp: // Fall thru
322 case dwarf::DW_FORM_ref4: // Fall thru
323 case dwarf::DW_FORM_data4: return sizeof(int32_t);
324 case dwarf::DW_FORM_ref8: // Fall thru
325 case dwarf::DW_FORM_ref_sig8: // Fall thru
326 case dwarf::DW_FORM_data8: return sizeof(int64_t);
327 case dwarf::DW_FORM_GNU_str_index: return getULEB128Size(Integer);
328 case dwarf::DW_FORM_GNU_addr_index: return getULEB128Size(Integer);
329 case dwarf::DW_FORM_udata: return getULEB128Size(Integer);
330 case dwarf::DW_FORM_sdata: return getSLEB128Size(Integer);
331 case dwarf::DW_FORM_addr:
332 return AP->getPointerSize();
333 case dwarf::DW_FORM_ref_addr:
334 if (AP->OutStreamer->getContext().getDwarfVersion() == 2)
335 return AP->getPointerSize();
336 return sizeof(int32_t);
337 default: llvm_unreachable("DIE Value form not supported yet");
342 void DIEInteger::print(raw_ostream &O) const {
343 O << "Int: " << (int64_t)Integer << " 0x";
344 O.write_hex(Integer);
347 //===----------------------------------------------------------------------===//
348 // DIEExpr Implementation
349 //===----------------------------------------------------------------------===//
351 /// EmitValue - Emit expression value.
353 void DIEExpr::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
354 AP->OutStreamer->EmitValue(Expr, SizeOf(AP, Form));
357 /// SizeOf - Determine size of expression value in bytes.
359 unsigned DIEExpr::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
360 if (Form == dwarf::DW_FORM_data4) return 4;
361 if (Form == dwarf::DW_FORM_sec_offset) return 4;
362 if (Form == dwarf::DW_FORM_strp) return 4;
363 return AP->getPointerSize();
367 void DIEExpr::print(raw_ostream &O) const { O << "Expr: " << *Expr; }
369 //===----------------------------------------------------------------------===//
370 // DIELabel Implementation
371 //===----------------------------------------------------------------------===//
373 /// EmitValue - Emit label value.
375 void DIELabel::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
376 AP->EmitLabelReference(Label, SizeOf(AP, Form),
377 Form == dwarf::DW_FORM_strp ||
378 Form == dwarf::DW_FORM_sec_offset ||
379 Form == dwarf::DW_FORM_ref_addr);
382 /// SizeOf - Determine size of label value in bytes.
384 unsigned DIELabel::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
385 if (Form == dwarf::DW_FORM_data4) return 4;
386 if (Form == dwarf::DW_FORM_sec_offset) return 4;
387 if (Form == dwarf::DW_FORM_strp) return 4;
388 return AP->getPointerSize();
392 void DIELabel::print(raw_ostream &O) const { O << "Lbl: " << Label->getName(); }
394 //===----------------------------------------------------------------------===//
395 // DIEDelta Implementation
396 //===----------------------------------------------------------------------===//
398 /// EmitValue - Emit delta value.
400 void DIEDelta::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
401 AP->EmitLabelDifference(LabelHi, LabelLo, SizeOf(AP, Form));
404 /// SizeOf - Determine size of delta value in bytes.
406 unsigned DIEDelta::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
407 if (Form == dwarf::DW_FORM_data4) return 4;
408 if (Form == dwarf::DW_FORM_sec_offset) return 4;
409 if (Form == dwarf::DW_FORM_strp) return 4;
410 return AP->getPointerSize();
414 void DIEDelta::print(raw_ostream &O) const {
415 O << "Del: " << LabelHi->getName() << "-" << LabelLo->getName();
418 //===----------------------------------------------------------------------===//
419 // DIEString Implementation
420 //===----------------------------------------------------------------------===//
422 /// EmitValue - Emit string value.
424 void DIEString::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
426 (Form == dwarf::DW_FORM_strp || Form == dwarf::DW_FORM_GNU_str_index) &&
427 "Expected valid string form");
429 // Index of string in symbol table.
430 if (Form == dwarf::DW_FORM_GNU_str_index) {
431 DIEInteger(S.getIndex()).EmitValue(AP, Form);
435 // Relocatable symbol.
436 assert(Form == dwarf::DW_FORM_strp);
437 if (AP->MAI->doesDwarfUseRelocationsAcrossSections()) {
438 DIELabel(S.getSymbol()).EmitValue(AP, Form);
442 // Offset into symbol table.
443 DIEInteger(S.getOffset()).EmitValue(AP, Form);
446 /// SizeOf - Determine size of delta value in bytes.
448 unsigned DIEString::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
450 (Form == dwarf::DW_FORM_strp || Form == dwarf::DW_FORM_GNU_str_index) &&
451 "Expected valid string form");
453 // Index of string in symbol table.
454 if (Form == dwarf::DW_FORM_GNU_str_index)
455 return DIEInteger(S.getIndex()).SizeOf(AP, Form);
457 // Relocatable symbol.
458 if (AP->MAI->doesDwarfUseRelocationsAcrossSections())
459 return DIELabel(S.getSymbol()).SizeOf(AP, Form);
461 // Offset into symbol table.
462 return DIEInteger(S.getOffset()).SizeOf(AP, Form);
466 void DIEString::print(raw_ostream &O) const {
467 O << "String: " << S.getString();
470 //===----------------------------------------------------------------------===//
471 // DIEEntry Implementation
472 //===----------------------------------------------------------------------===//
474 /// EmitValue - Emit debug information entry offset.
476 void DIEEntry::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
478 if (Form == dwarf::DW_FORM_ref_addr) {
479 const DwarfDebug *DD = AP->getDwarfDebug();
480 unsigned Addr = Entry->getOffset();
481 assert(!DD->useSplitDwarf() && "TODO: dwo files can't have relocations.");
482 // For DW_FORM_ref_addr, output the offset from beginning of debug info
483 // section. Entry->getOffset() returns the offset from start of the
485 DwarfCompileUnit *CU = DD->lookupUnit(Entry->getUnit());
486 assert(CU && "CUDie should belong to a CU.");
487 Addr += CU->getDebugInfoOffset();
488 if (AP->MAI->doesDwarfUseRelocationsAcrossSections())
489 AP->EmitLabelPlusOffset(CU->getSectionSym(), Addr,
490 DIEEntry::getRefAddrSize(AP));
492 AP->OutStreamer->EmitIntValue(Addr, DIEEntry::getRefAddrSize(AP));
494 AP->EmitInt32(Entry->getOffset());
497 unsigned DIEEntry::getRefAddrSize(const AsmPrinter *AP) {
498 // DWARF4: References that use the attribute form DW_FORM_ref_addr are
499 // specified to be four bytes in the DWARF 32-bit format and eight bytes
500 // in the DWARF 64-bit format, while DWARF Version 2 specifies that such
501 // references have the same size as an address on the target system.
502 const DwarfDebug *DD = AP->getDwarfDebug();
503 assert(DD && "Expected Dwarf Debug info to be available");
504 if (DD->getDwarfVersion() == 2)
505 return AP->getPointerSize();
506 return sizeof(int32_t);
510 void DIEEntry::print(raw_ostream &O) const {
511 O << format("Die: 0x%lx", (long)(intptr_t)&Entry);
514 //===----------------------------------------------------------------------===//
515 // DIETypeSignature Implementation
516 //===----------------------------------------------------------------------===//
517 void DIETypeSignature::EmitValue(const AsmPrinter *Asm,
518 dwarf::Form Form) const {
519 assert(Form == dwarf::DW_FORM_ref_sig8);
520 Asm->OutStreamer->EmitIntValue(Unit->getTypeSignature(), 8);
524 void DIETypeSignature::print(raw_ostream &O) const {
525 O << format("Type Unit: 0x%lx", Unit->getTypeSignature());
528 //===----------------------------------------------------------------------===//
529 // DIELoc Implementation
530 //===----------------------------------------------------------------------===//
532 /// ComputeSize - calculate the size of the location expression.
534 unsigned DIELoc::ComputeSize(const AsmPrinter *AP) const {
536 for (const auto &V : values())
537 Size += V.SizeOf(AP);
543 /// EmitValue - Emit location data.
545 void DIELoc::EmitValue(const AsmPrinter *Asm, dwarf::Form Form) const {
547 default: llvm_unreachable("Improper form for block");
548 case dwarf::DW_FORM_block1: Asm->EmitInt8(Size); break;
549 case dwarf::DW_FORM_block2: Asm->EmitInt16(Size); break;
550 case dwarf::DW_FORM_block4: Asm->EmitInt32(Size); break;
551 case dwarf::DW_FORM_block:
552 case dwarf::DW_FORM_exprloc:
553 Asm->EmitULEB128(Size); break;
556 for (const auto &V : values())
560 /// SizeOf - Determine size of location data in bytes.
562 unsigned DIELoc::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
564 case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
565 case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
566 case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
567 case dwarf::DW_FORM_block:
568 case dwarf::DW_FORM_exprloc:
569 return Size + getULEB128Size(Size);
570 default: llvm_unreachable("Improper form for block");
575 void DIELoc::print(raw_ostream &O) const {
576 printValues(O, *this, "ExprLoc", Size, 5);
579 //===----------------------------------------------------------------------===//
580 // DIEBlock Implementation
581 //===----------------------------------------------------------------------===//
583 /// ComputeSize - calculate the size of the block.
585 unsigned DIEBlock::ComputeSize(const AsmPrinter *AP) const {
587 for (const auto &V : values())
588 Size += V.SizeOf(AP);
594 /// EmitValue - Emit block data.
596 void DIEBlock::EmitValue(const AsmPrinter *Asm, dwarf::Form Form) const {
598 default: llvm_unreachable("Improper form for block");
599 case dwarf::DW_FORM_block1: Asm->EmitInt8(Size); break;
600 case dwarf::DW_FORM_block2: Asm->EmitInt16(Size); break;
601 case dwarf::DW_FORM_block4: Asm->EmitInt32(Size); break;
602 case dwarf::DW_FORM_block: Asm->EmitULEB128(Size); break;
605 for (const auto &V : values())
609 /// SizeOf - Determine size of block data in bytes.
611 unsigned DIEBlock::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
613 case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
614 case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
615 case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
616 case dwarf::DW_FORM_block: return Size + getULEB128Size(Size);
617 default: llvm_unreachable("Improper form for block");
622 void DIEBlock::print(raw_ostream &O) const {
623 printValues(O, *this, "Blk", Size, 5);
626 //===----------------------------------------------------------------------===//
627 // DIELocList Implementation
628 //===----------------------------------------------------------------------===//
630 unsigned DIELocList::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
631 if (Form == dwarf::DW_FORM_data4)
633 if (Form == dwarf::DW_FORM_sec_offset)
635 return AP->getPointerSize();
638 /// EmitValue - Emit label value.
640 void DIELocList::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
641 DwarfDebug *DD = AP->getDwarfDebug();
642 MCSymbol *Label = DD->getDebugLocs().getList(Index).Label;
643 AP->emitDwarfSymbolReference(Label, /*ForceOffset*/ DD->useSplitDwarf());
647 void DIELocList::print(raw_ostream &O) const { O << "LocList: " << Index; }