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 // 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(const 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 DIEAbbrev DIE::generateAbbrev() const {
111 DIEAbbrev Abbrev(Tag, hasChildren());
112 for (const DIEValue &V : Values)
113 Abbrev.AddAttribute(V.getAttribute(), V.getForm());
117 /// Climb up the parent chain to get the unit DIE to which this DIE
119 const DIE *DIE::getUnit() const {
120 const DIE *Cu = getUnitOrNull();
121 assert(Cu && "We should not have orphaned DIEs.");
125 /// Climb up the parent chain to get the unit DIE this DIE belongs
126 /// to. Return NULL if DIE is not added to an owner yet.
127 const DIE *DIE::getUnitOrNull() const {
130 if (p->getTag() == dwarf::DW_TAG_compile_unit ||
131 p->getTag() == dwarf::DW_TAG_type_unit)
138 DIEValue DIE::findAttribute(dwarf::Attribute Attribute) const {
139 // Iterate through all the attributes until we find the one we're
140 // looking for, if we can't find it return NULL.
141 for (const auto &V : values())
142 if (V.getAttribute() == Attribute)
148 void DIE::print(raw_ostream &O, unsigned IndentCount) const {
149 const std::string Indent(IndentCount, ' ');
150 bool isBlock = getTag() == 0;
155 << format("0x%lx", (long)(intptr_t)this)
156 << ", Offset: " << Offset
157 << ", Size: " << Size << "\n";
160 << dwarf::TagString(getTag())
162 << dwarf::ChildrenString(hasChildren()) << "\n";
164 O << "Size: " << Size << "\n";
168 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
172 O << dwarf::AttributeString(Values[i].getAttribute());
174 O << "Blk[" << i << "]";
177 << dwarf::FormEncodingString(Values[i].getForm())
184 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
185 Children[j]->print(O, IndentCount+4);
188 if (!isBlock) O << "\n";
196 void DIEValue::EmitValue(const AsmPrinter *AP) const {
199 llvm_unreachable("Expected valid DIEValue");
200 #define HANDLE_DIEVALUE(T) \
202 getDIE##T().EmitValue(AP, Form); \
204 #include "llvm/CodeGen/DIEValue.def"
208 unsigned DIEValue::SizeOf(const AsmPrinter *AP) const {
211 llvm_unreachable("Expected valid DIEValue");
212 #define HANDLE_DIEVALUE(T) \
214 return getDIE##T().SizeOf(AP, Form);
215 #include "llvm/CodeGen/DIEValue.def"
217 llvm_unreachable("Unknown DIE kind");
221 void DIEValue::print(raw_ostream &O) const {
224 llvm_unreachable("Expected valid DIEValue");
225 #define HANDLE_DIEVALUE(T) \
227 getDIE##T().print(O); \
229 #include "llvm/CodeGen/DIEValue.def"
233 void DIEValue::dump() const {
238 //===----------------------------------------------------------------------===//
239 // DIEInteger Implementation
240 //===----------------------------------------------------------------------===//
242 /// EmitValue - Emit integer of appropriate size.
244 void DIEInteger::EmitValue(const AsmPrinter *Asm, dwarf::Form Form) const {
247 case dwarf::DW_FORM_flag_present:
248 // Emit something to keep the lines and comments in sync.
249 // FIXME: Is there a better way to do this?
250 Asm->OutStreamer->AddBlankLine();
252 case dwarf::DW_FORM_flag: // Fall thru
253 case dwarf::DW_FORM_ref1: // Fall thru
254 case dwarf::DW_FORM_data1: Size = 1; break;
255 case dwarf::DW_FORM_ref2: // Fall thru
256 case dwarf::DW_FORM_data2: Size = 2; break;
257 case dwarf::DW_FORM_sec_offset: // Fall thru
258 case dwarf::DW_FORM_strp: // Fall thru
259 case dwarf::DW_FORM_ref4: // Fall thru
260 case dwarf::DW_FORM_data4: Size = 4; break;
261 case dwarf::DW_FORM_ref8: // Fall thru
262 case dwarf::DW_FORM_ref_sig8: // Fall thru
263 case dwarf::DW_FORM_data8: Size = 8; break;
264 case dwarf::DW_FORM_GNU_str_index: Asm->EmitULEB128(Integer); return;
265 case dwarf::DW_FORM_GNU_addr_index: Asm->EmitULEB128(Integer); return;
266 case dwarf::DW_FORM_udata: Asm->EmitULEB128(Integer); return;
267 case dwarf::DW_FORM_sdata: Asm->EmitSLEB128(Integer); return;
268 case dwarf::DW_FORM_addr:
269 Size = Asm->getDataLayout().getPointerSize(); break;
270 case dwarf::DW_FORM_ref_addr:
271 Size = SizeOf(Asm, dwarf::DW_FORM_ref_addr);
273 default: llvm_unreachable("DIE Value form not supported yet");
275 Asm->OutStreamer->EmitIntValue(Integer, Size);
278 /// SizeOf - Determine size of integer value in bytes.
280 unsigned DIEInteger::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
282 case dwarf::DW_FORM_flag_present: return 0;
283 case dwarf::DW_FORM_flag: // Fall thru
284 case dwarf::DW_FORM_ref1: // Fall thru
285 case dwarf::DW_FORM_data1: return sizeof(int8_t);
286 case dwarf::DW_FORM_ref2: // Fall thru
287 case dwarf::DW_FORM_data2: return sizeof(int16_t);
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: return sizeof(int32_t);
292 case dwarf::DW_FORM_ref8: // Fall thru
293 case dwarf::DW_FORM_ref_sig8: // Fall thru
294 case dwarf::DW_FORM_data8: return sizeof(int64_t);
295 case dwarf::DW_FORM_GNU_str_index: return getULEB128Size(Integer);
296 case dwarf::DW_FORM_GNU_addr_index: return getULEB128Size(Integer);
297 case dwarf::DW_FORM_udata: return getULEB128Size(Integer);
298 case dwarf::DW_FORM_sdata: return getSLEB128Size(Integer);
299 case dwarf::DW_FORM_addr: return AP->getDataLayout().getPointerSize();
300 case dwarf::DW_FORM_ref_addr:
301 if (AP->OutStreamer->getContext().getDwarfVersion() == 2)
302 return AP->getDataLayout().getPointerSize();
303 return sizeof(int32_t);
304 default: llvm_unreachable("DIE Value form not supported yet");
309 void DIEInteger::print(raw_ostream &O) const {
310 O << "Int: " << (int64_t)Integer << " 0x";
311 O.write_hex(Integer);
315 //===----------------------------------------------------------------------===//
316 // DIEExpr Implementation
317 //===----------------------------------------------------------------------===//
319 /// EmitValue - Emit expression value.
321 void DIEExpr::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
322 AP->OutStreamer->EmitValue(Expr, SizeOf(AP, Form));
325 /// SizeOf - Determine size of expression value in bytes.
327 unsigned DIEExpr::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
328 if (Form == dwarf::DW_FORM_data4) return 4;
329 if (Form == dwarf::DW_FORM_sec_offset) return 4;
330 if (Form == dwarf::DW_FORM_strp) return 4;
331 return AP->getDataLayout().getPointerSize();
335 void DIEExpr::print(raw_ostream &O) const { O << "Expr: " << *Expr; }
338 //===----------------------------------------------------------------------===//
339 // DIELabel Implementation
340 //===----------------------------------------------------------------------===//
342 /// EmitValue - Emit label value.
344 void DIELabel::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
345 AP->EmitLabelReference(Label, SizeOf(AP, Form),
346 Form == dwarf::DW_FORM_strp ||
347 Form == dwarf::DW_FORM_sec_offset ||
348 Form == dwarf::DW_FORM_ref_addr);
351 /// SizeOf - Determine size of label value in bytes.
353 unsigned DIELabel::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
354 if (Form == dwarf::DW_FORM_data4) return 4;
355 if (Form == dwarf::DW_FORM_sec_offset) return 4;
356 if (Form == dwarf::DW_FORM_strp) return 4;
357 return AP->getDataLayout().getPointerSize();
361 void DIELabel::print(raw_ostream &O) const { O << "Lbl: " << Label->getName(); }
364 //===----------------------------------------------------------------------===//
365 // DIEDelta Implementation
366 //===----------------------------------------------------------------------===//
368 /// EmitValue - Emit delta value.
370 void DIEDelta::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
371 AP->EmitLabelDifference(LabelHi, LabelLo, SizeOf(AP, Form));
374 /// SizeOf - Determine size of delta value in bytes.
376 unsigned DIEDelta::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
377 if (Form == dwarf::DW_FORM_data4) return 4;
378 if (Form == dwarf::DW_FORM_sec_offset) return 4;
379 if (Form == dwarf::DW_FORM_strp) return 4;
380 return AP->getDataLayout().getPointerSize();
384 void DIEDelta::print(raw_ostream &O) const {
385 O << "Del: " << LabelHi->getName() << "-" << LabelLo->getName();
389 //===----------------------------------------------------------------------===//
390 // DIEString Implementation
391 //===----------------------------------------------------------------------===//
393 /// EmitValue - Emit string value.
395 void DIEString::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
397 (Form == dwarf::DW_FORM_strp || Form == dwarf::DW_FORM_GNU_str_index) &&
398 "Expected valid string form");
400 // Index of string in symbol table.
401 if (Form == dwarf::DW_FORM_GNU_str_index) {
402 DIEInteger(S.getIndex()).EmitValue(AP, Form);
406 // Relocatable symbol.
407 assert(Form == dwarf::DW_FORM_strp);
408 if (AP->MAI->doesDwarfUseRelocationsAcrossSections()) {
409 DIELabel(S.getSymbol()).EmitValue(AP, Form);
413 // Offset into symbol table.
414 DIEInteger(S.getOffset()).EmitValue(AP, Form);
417 /// SizeOf - Determine size of delta value in bytes.
419 unsigned DIEString::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
421 (Form == dwarf::DW_FORM_strp || Form == dwarf::DW_FORM_GNU_str_index) &&
422 "Expected valid string form");
424 // Index of string in symbol table.
425 if (Form == dwarf::DW_FORM_GNU_str_index)
426 return DIEInteger(S.getIndex()).SizeOf(AP, Form);
428 // Relocatable symbol.
429 if (AP->MAI->doesDwarfUseRelocationsAcrossSections())
430 return DIELabel(S.getSymbol()).SizeOf(AP, Form);
432 // Offset into symbol table.
433 return DIEInteger(S.getOffset()).SizeOf(AP, Form);
437 void DIEString::print(raw_ostream &O) const {
438 O << "String: " << S.getString();
442 //===----------------------------------------------------------------------===//
443 // DIEEntry Implementation
444 //===----------------------------------------------------------------------===//
446 /// EmitValue - Emit debug information entry offset.
448 void DIEEntry::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
450 if (Form == dwarf::DW_FORM_ref_addr) {
451 const DwarfDebug *DD = AP->getDwarfDebug();
452 unsigned Addr = Entry->getOffset();
453 assert(!DD->useSplitDwarf() && "TODO: dwo files can't have relocations.");
454 // For DW_FORM_ref_addr, output the offset from beginning of debug info
455 // section. Entry->getOffset() returns the offset from start of the
457 DwarfCompileUnit *CU = DD->lookupUnit(Entry->getUnit());
458 assert(CU && "CUDie should belong to a CU.");
459 Addr += CU->getDebugInfoOffset();
460 if (AP->MAI->doesDwarfUseRelocationsAcrossSections())
461 AP->EmitLabelPlusOffset(CU->getSectionSym(), Addr,
462 DIEEntry::getRefAddrSize(AP));
464 AP->OutStreamer->EmitIntValue(Addr, DIEEntry::getRefAddrSize(AP));
466 AP->EmitInt32(Entry->getOffset());
469 unsigned DIEEntry::getRefAddrSize(const AsmPrinter *AP) {
470 // DWARF4: References that use the attribute form DW_FORM_ref_addr are
471 // specified to be four bytes in the DWARF 32-bit format and eight bytes
472 // in the DWARF 64-bit format, while DWARF Version 2 specifies that such
473 // references have the same size as an address on the target system.
474 const DwarfDebug *DD = AP->getDwarfDebug();
475 assert(DD && "Expected Dwarf Debug info to be available");
476 if (DD->getDwarfVersion() == 2)
477 return AP->getDataLayout().getPointerSize();
478 return sizeof(int32_t);
482 void DIEEntry::print(raw_ostream &O) const {
483 O << format("Die: 0x%lx", (long)(intptr_t)&Entry);
487 //===----------------------------------------------------------------------===//
488 // DIETypeSignature Implementation
489 //===----------------------------------------------------------------------===//
490 void DIETypeSignature::EmitValue(const AsmPrinter *Asm,
491 dwarf::Form Form) const {
492 assert(Form == dwarf::DW_FORM_ref_sig8);
493 Asm->OutStreamer->EmitIntValue(Unit->getTypeSignature(), 8);
497 void DIETypeSignature::print(raw_ostream &O) const {
498 O << format("Type Unit: 0x%lx", Unit->getTypeSignature());
502 //===----------------------------------------------------------------------===//
503 // DIELoc Implementation
504 //===----------------------------------------------------------------------===//
506 /// ComputeSize - calculate the size of the location expression.
508 unsigned DIELoc::ComputeSize(const AsmPrinter *AP) const {
510 for (const auto &V : Values)
511 Size += V.SizeOf(AP);
517 /// EmitValue - Emit location data.
519 void DIELoc::EmitValue(const AsmPrinter *Asm, dwarf::Form Form) const {
521 default: llvm_unreachable("Improper form for block");
522 case dwarf::DW_FORM_block1: Asm->EmitInt8(Size); break;
523 case dwarf::DW_FORM_block2: Asm->EmitInt16(Size); break;
524 case dwarf::DW_FORM_block4: Asm->EmitInt32(Size); break;
525 case dwarf::DW_FORM_block:
526 case dwarf::DW_FORM_exprloc:
527 Asm->EmitULEB128(Size); break;
530 for (const auto &V : Values)
534 /// SizeOf - Determine size of location data in bytes.
536 unsigned DIELoc::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
538 case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
539 case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
540 case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
541 case dwarf::DW_FORM_block:
542 case dwarf::DW_FORM_exprloc:
543 return Size + getULEB128Size(Size);
544 default: llvm_unreachable("Improper form for block");
549 void DIELoc::print(raw_ostream &O) const {
555 //===----------------------------------------------------------------------===//
556 // DIEBlock Implementation
557 //===----------------------------------------------------------------------===//
559 /// ComputeSize - calculate the size of the block.
561 unsigned DIEBlock::ComputeSize(const AsmPrinter *AP) const {
563 for (const auto &V : Values)
564 Size += V.SizeOf(AP);
570 /// EmitValue - Emit block data.
572 void DIEBlock::EmitValue(const AsmPrinter *Asm, dwarf::Form Form) const {
574 default: llvm_unreachable("Improper form for block");
575 case dwarf::DW_FORM_block1: Asm->EmitInt8(Size); break;
576 case dwarf::DW_FORM_block2: Asm->EmitInt16(Size); break;
577 case dwarf::DW_FORM_block4: Asm->EmitInt32(Size); break;
578 case dwarf::DW_FORM_block: Asm->EmitULEB128(Size); break;
581 for (const auto &V : Values)
585 /// SizeOf - Determine size of block data in bytes.
587 unsigned DIEBlock::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
589 case dwarf::DW_FORM_block1: return Size + sizeof(int8_t);
590 case dwarf::DW_FORM_block2: return Size + sizeof(int16_t);
591 case dwarf::DW_FORM_block4: return Size + sizeof(int32_t);
592 case dwarf::DW_FORM_block: return Size + getULEB128Size(Size);
593 default: llvm_unreachable("Improper form for block");
598 void DIEBlock::print(raw_ostream &O) const {
604 //===----------------------------------------------------------------------===//
605 // DIELocList Implementation
606 //===----------------------------------------------------------------------===//
608 unsigned DIELocList::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const {
609 if (Form == dwarf::DW_FORM_data4)
611 if (Form == dwarf::DW_FORM_sec_offset)
613 return AP->getDataLayout().getPointerSize();
616 /// EmitValue - Emit label value.
618 void DIELocList::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const {
619 DwarfDebug *DD = AP->getDwarfDebug();
620 MCSymbol *Label = DD->getDebugLocs().getList(Index).Label;
621 AP->emitDwarfSymbolReference(Label, /*ForceOffset*/ DD->useSplitDwarf());
625 void DIELocList::print(raw_ostream &O) const { O << "LocList: " << Index; }