1 //===--- lib/CodeGen/DIE.cpp - DWARF Info Entries -------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // Data structures for DWARF info entries. 11 // 12 //===----------------------------------------------------------------------===// 13 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" 32 using namespace llvm; 33 34 //===----------------------------------------------------------------------===// 35 // DIEAbbrevData Implementation 36 //===----------------------------------------------------------------------===// 37 38 /// Profile - Used to gather unique data for the abbreviation folding set. 39 /// 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)); 45 } 46 47 //===----------------------------------------------------------------------===// 48 // DIEAbbrev Implementation 49 //===----------------------------------------------------------------------===// 50 51 /// Profile - Used to gather unique data for the abbreviation folding set. 52 /// 53 void DIEAbbrev::Profile(FoldingSetNodeID &ID) const { 54 ID.AddInteger(unsigned(Tag)); 55 ID.AddInteger(unsigned(Children)); 56 57 // For each attribute description. 58 for (unsigned i = 0, N = Data.size(); i < N; ++i) 59 Data[i].Profile(ID); 60 } 61 62 /// Emit - Print the abbreviation using the specified asm printer. 63 /// 64 void DIEAbbrev::Emit(const AsmPrinter *AP) const { 65 // Emit its Dwarf tag type. 66 AP->EmitULEB128(Tag, dwarf::TagString(Tag).data()); 67 68 // Emit whether it has children DIEs. 69 AP->EmitULEB128((unsigned)Children, dwarf::ChildrenString(Children).data()); 70 71 // For each attribute description. 72 for (unsigned i = 0, N = Data.size(); i < N; ++i) { 73 const DIEAbbrevData &AttrData = Data[i]; 74 75 // Emit attribute type. 76 AP->EmitULEB128(AttrData.getAttribute(), 77 dwarf::AttributeString(AttrData.getAttribute()).data()); 78 79 // Emit form type. 80 AP->EmitULEB128(AttrData.getForm(), 81 dwarf::FormEncodingString(AttrData.getForm()).data()); 82 } 83 84 // Mark end of abbreviation. 85 AP->EmitULEB128(0, "EOM(1)"); 86 AP->EmitULEB128(0, "EOM(2)"); 87 } 88 89 LLVM_DUMP_METHOD 90 void DIEAbbrev::print(raw_ostream &O) { 91 O << "Abbreviation @" 92 << format("0x%lx", (long)(intptr_t)this) 93 << " " 94 << dwarf::TagString(Tag) 95 << " " 96 << dwarf::ChildrenString(Children) 97 << '\n'; 98 99 for (unsigned i = 0, N = Data.size(); i < N; ++i) { 100 O << " " 101 << dwarf::AttributeString(Data[i].getAttribute()) 102 << " " 103 << dwarf::FormEncodingString(Data[i].getForm()) 104 << '\n'; 105 } 106 } 107 108 LLVM_DUMP_METHOD 109 void DIEAbbrev::dump() { print(dbgs()); } 110 111 //===----------------------------------------------------------------------===// 112 // DIEAbbrevSet Implementation 113 //===----------------------------------------------------------------------===// 114 115 DIEAbbrevSet::~DIEAbbrevSet() { 116 for (DIEAbbrev *Abbrev : Abbreviations) 117 Abbrev->~DIEAbbrev(); 118 } 119 120 DIEAbbrev &DIEAbbrevSet::uniqueAbbreviation(DIE &Die) { 121 122 FoldingSetNodeID ID; 123 DIEAbbrev Abbrev = Die.generateAbbrev(); 124 Abbrev.Profile(ID); 125 126 void *InsertPos; 127 if (DIEAbbrev *Existing = 128 AbbreviationsSet.FindNodeOrInsertPos(ID, InsertPos)) { 129 Die.setAbbrevNumber(Existing->getNumber()); 130 return *Existing; 131 } 132 133 // Move the abbreviation to the heap and assign a number. 134 DIEAbbrev *New = new (Alloc) DIEAbbrev(std::move(Abbrev)); 135 Abbreviations.push_back(New); 136 New->setNumber(Abbreviations.size()); 137 Die.setAbbrevNumber(Abbreviations.size()); 138 139 // Store it for lookup. 140 AbbreviationsSet.InsertNode(New, InsertPos); 141 return *New; 142 } 143 144 void DIEAbbrevSet::Emit(const AsmPrinter *AP, MCSection *Section) const { 145 if (!Abbreviations.empty()) { 146 // Start the debug abbrev section. 147 AP->OutStreamer->SwitchSection(Section); 148 AP->emitDwarfAbbrevs(Abbreviations); 149 } 150 } 151 152 //===----------------------------------------------------------------------===// 153 // DIE Implementation 154 //===----------------------------------------------------------------------===// 155 156 DIE *DIE::getParent() const { 157 return Owner.dyn_cast<DIE*>(); 158 } 159 160 DIEAbbrev DIE::generateAbbrev() const { 161 DIEAbbrev Abbrev(Tag, hasChildren()); 162 for (const DIEValue &V : values()) 163 Abbrev.AddAttribute(V.getAttribute(), V.getForm()); 164 return Abbrev; 165 } 166 167 unsigned DIE::getDebugSectionOffset() const { 168 const DIEUnit *Unit = getUnit(); 169 assert(Unit && "DIE must be owned by a DIEUnit to get its absolute offset"); 170 return Unit->getDebugSectionOffset() + getOffset(); 171 } 172 173 const DIE *DIE::getUnitDie() const { 174 const DIE *p = this; 175 while (p) { 176 if (p->getTag() == dwarf::DW_TAG_compile_unit || 177 p->getTag() == dwarf::DW_TAG_type_unit) 178 return p; 179 p = p->getParent(); 180 } 181 return nullptr; 182 } 183 184 const DIEUnit *DIE::getUnit() const { 185 const DIE *UnitDie = getUnitDie(); 186 if (UnitDie) 187 return UnitDie->Owner.dyn_cast<DIEUnit*>(); 188 return nullptr; 189 } 190 191 DIEValue DIE::findAttribute(dwarf::Attribute Attribute) const { 192 // Iterate through all the attributes until we find the one we're 193 // looking for, if we can't find it return NULL. 194 for (const auto &V : values()) 195 if (V.getAttribute() == Attribute) 196 return V; 197 return DIEValue(); 198 } 199 200 LLVM_DUMP_METHOD 201 static void printValues(raw_ostream &O, const DIEValueList &Values, 202 StringRef Type, unsigned Size, unsigned IndentCount) { 203 O << Type << ": Size: " << Size << "\n"; 204 205 unsigned I = 0; 206 const std::string Indent(IndentCount, ' '); 207 for (const auto &V : Values.values()) { 208 O << Indent; 209 O << "Blk[" << I++ << "]"; 210 O << " " << dwarf::FormEncodingString(V.getForm()) << " "; 211 V.print(O); 212 O << "\n"; 213 } 214 } 215 216 LLVM_DUMP_METHOD 217 void DIE::print(raw_ostream &O, unsigned IndentCount) const { 218 const std::string Indent(IndentCount, ' '); 219 O << Indent << "Die: " << format("0x%lx", (long)(intptr_t) this) 220 << ", Offset: " << Offset << ", Size: " << Size << "\n"; 221 222 O << Indent << dwarf::TagString(getTag()) << " " 223 << dwarf::ChildrenString(hasChildren()) << "\n"; 224 225 IndentCount += 2; 226 for (const auto &V : values()) { 227 O << Indent; 228 O << dwarf::AttributeString(V.getAttribute()); 229 O << " " << dwarf::FormEncodingString(V.getForm()) << " "; 230 V.print(O); 231 O << "\n"; 232 } 233 IndentCount -= 2; 234 235 for (const auto &Child : children()) 236 Child.print(O, IndentCount + 4); 237 238 O << "\n"; 239 } 240 241 LLVM_DUMP_METHOD 242 void DIE::dump() { 243 print(dbgs()); 244 } 245 246 unsigned DIE::computeOffsetsAndAbbrevs(const AsmPrinter *AP, 247 DIEAbbrevSet &AbbrevSet, 248 unsigned CUOffset) { 249 // Unique the abbreviation and fill in the abbreviation number so this DIE 250 // can be emitted. 251 const DIEAbbrev &Abbrev = AbbrevSet.uniqueAbbreviation(*this); 252 253 // Set compile/type unit relative offset of this DIE. 254 setOffset(CUOffset); 255 256 // Add the byte size of the abbreviation code. 257 CUOffset += getULEB128Size(getAbbrevNumber()); 258 259 // Add the byte size of all the DIE attribute values. 260 for (const auto &V : values()) 261 CUOffset += V.SizeOf(AP); 262 263 // Let the children compute their offsets and abbreviation numbers. 264 if (hasChildren()) { 265 (void)Abbrev; 266 assert(Abbrev.hasChildren() && "Children flag not set"); 267 268 for (auto &Child : children()) 269 CUOffset = Child.computeOffsetsAndAbbrevs(AP, AbbrevSet, CUOffset); 270 271 // Each child chain is terminated with a zero byte, adjust the offset. 272 CUOffset += sizeof(int8_t); 273 } 274 275 // Compute the byte size of this DIE and all of its children correctly. This 276 // is needed so that top level DIE can help the compile unit set its length 277 // correctly. 278 setSize(CUOffset - getOffset()); 279 return CUOffset; 280 } 281 282 //===----------------------------------------------------------------------===// 283 // DIEUnit Implementation 284 //===----------------------------------------------------------------------===// 285 DIEUnit::DIEUnit(uint16_t V, uint8_t A, dwarf::Tag UnitTag) 286 : Die(UnitTag), Section(nullptr), Offset(0), Length(0), Version(V), 287 AddrSize(A) 288 { 289 Die.Owner = this; 290 assert((UnitTag == dwarf::DW_TAG_compile_unit || 291 UnitTag == dwarf::DW_TAG_type_unit || 292 UnitTag == dwarf::DW_TAG_partial_unit) && "expected a unit TAG"); 293 } 294 295 void DIEValue::EmitValue(const AsmPrinter *AP) const { 296 switch (Ty) { 297 case isNone: 298 llvm_unreachable("Expected valid DIEValue"); 299 #define HANDLE_DIEVALUE(T) \ 300 case is##T: \ 301 getDIE##T().EmitValue(AP, Form); \ 302 break; 303 #include "llvm/CodeGen/DIEValue.def" 304 } 305 } 306 307 unsigned DIEValue::SizeOf(const AsmPrinter *AP) const { 308 switch (Ty) { 309 case isNone: 310 llvm_unreachable("Expected valid DIEValue"); 311 #define HANDLE_DIEVALUE(T) \ 312 case is##T: \ 313 return getDIE##T().SizeOf(AP, Form); 314 #include "llvm/CodeGen/DIEValue.def" 315 } 316 llvm_unreachable("Unknown DIE kind"); 317 } 318 319 LLVM_DUMP_METHOD 320 void DIEValue::print(raw_ostream &O) const { 321 switch (Ty) { 322 case isNone: 323 llvm_unreachable("Expected valid DIEValue"); 324 #define HANDLE_DIEVALUE(T) \ 325 case is##T: \ 326 getDIE##T().print(O); \ 327 break; 328 #include "llvm/CodeGen/DIEValue.def" 329 } 330 } 331 332 LLVM_DUMP_METHOD 333 void DIEValue::dump() const { 334 print(dbgs()); 335 } 336 337 //===----------------------------------------------------------------------===// 338 // DIEInteger Implementation 339 //===----------------------------------------------------------------------===// 340 341 /// EmitValue - Emit integer of appropriate size. 342 /// 343 void DIEInteger::EmitValue(const AsmPrinter *Asm, dwarf::Form Form) const { 344 switch (Form) { 345 case dwarf::DW_FORM_flag_present: 346 // Emit something to keep the lines and comments in sync. 347 // FIXME: Is there a better way to do this? 348 Asm->OutStreamer->AddBlankLine(); 349 return; 350 case dwarf::DW_FORM_flag: 351 LLVM_FALLTHROUGH; 352 case dwarf::DW_FORM_ref1: 353 LLVM_FALLTHROUGH; 354 case dwarf::DW_FORM_data1: 355 LLVM_FALLTHROUGH; 356 case dwarf::DW_FORM_ref2: 357 LLVM_FALLTHROUGH; 358 case dwarf::DW_FORM_data2: 359 LLVM_FALLTHROUGH; 360 case dwarf::DW_FORM_strp: 361 LLVM_FALLTHROUGH; 362 case dwarf::DW_FORM_ref4: 363 LLVM_FALLTHROUGH; 364 case dwarf::DW_FORM_data4: 365 LLVM_FALLTHROUGH; 366 case dwarf::DW_FORM_ref8: 367 LLVM_FALLTHROUGH; 368 case dwarf::DW_FORM_ref_sig8: 369 LLVM_FALLTHROUGH; 370 case dwarf::DW_FORM_data8: 371 LLVM_FALLTHROUGH; 372 case dwarf::DW_FORM_GNU_ref_alt: 373 LLVM_FALLTHROUGH; 374 case dwarf::DW_FORM_GNU_strp_alt: 375 LLVM_FALLTHROUGH; 376 case dwarf::DW_FORM_line_strp: 377 LLVM_FALLTHROUGH; 378 case dwarf::DW_FORM_sec_offset: 379 LLVM_FALLTHROUGH; 380 case dwarf::DW_FORM_strp_sup: 381 LLVM_FALLTHROUGH; 382 case dwarf::DW_FORM_ref_sup: 383 LLVM_FALLTHROUGH; 384 case dwarf::DW_FORM_addr: 385 LLVM_FALLTHROUGH; 386 case dwarf::DW_FORM_ref_addr: 387 Asm->OutStreamer->EmitIntValue(Integer, SizeOf(Asm, Form)); 388 return; 389 case dwarf::DW_FORM_GNU_str_index: 390 LLVM_FALLTHROUGH; 391 case dwarf::DW_FORM_GNU_addr_index: 392 LLVM_FALLTHROUGH; 393 case dwarf::DW_FORM_ref_udata: 394 LLVM_FALLTHROUGH; 395 case dwarf::DW_FORM_udata: 396 Asm->EmitULEB128(Integer); 397 return; 398 case dwarf::DW_FORM_sdata: 399 Asm->EmitSLEB128(Integer); 400 return; 401 default: llvm_unreachable("DIE Value form not supported yet"); 402 } 403 } 404 405 /// SizeOf - Determine size of integer value in bytes. 406 /// 407 unsigned DIEInteger::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const { 408 switch (Form) { 409 case dwarf::DW_FORM_flag_present: return 0; 410 case dwarf::DW_FORM_flag: LLVM_FALLTHROUGH; 411 case dwarf::DW_FORM_ref1: LLVM_FALLTHROUGH; 412 case dwarf::DW_FORM_data1: return sizeof(int8_t); 413 case dwarf::DW_FORM_ref2: LLVM_FALLTHROUGH; 414 case dwarf::DW_FORM_data2: return sizeof(int16_t); 415 case dwarf::DW_FORM_ref4: LLVM_FALLTHROUGH; 416 case dwarf::DW_FORM_data4: return sizeof(int32_t); 417 case dwarf::DW_FORM_ref8: LLVM_FALLTHROUGH; 418 case dwarf::DW_FORM_ref_sig8: LLVM_FALLTHROUGH; 419 case dwarf::DW_FORM_data8: return sizeof(int64_t); 420 case dwarf::DW_FORM_ref_addr: 421 if (AP->getDwarfVersion() == 2) 422 return AP->getPointerSize(); 423 LLVM_FALLTHROUGH; 424 case dwarf::DW_FORM_strp: 425 LLVM_FALLTHROUGH; 426 case dwarf::DW_FORM_GNU_ref_alt: 427 LLVM_FALLTHROUGH; 428 case dwarf::DW_FORM_GNU_strp_alt: 429 LLVM_FALLTHROUGH; 430 case dwarf::DW_FORM_line_strp: 431 LLVM_FALLTHROUGH; 432 case dwarf::DW_FORM_sec_offset: 433 LLVM_FALLTHROUGH; 434 case dwarf::DW_FORM_strp_sup: 435 LLVM_FALLTHROUGH; 436 case dwarf::DW_FORM_ref_sup: 437 switch (AP->OutStreamer->getContext().getDwarfFormat()) { 438 case dwarf::DWARF32: 439 return 4; 440 case dwarf::DWARF64: 441 return 8; 442 } 443 llvm_unreachable("Invalid DWARF format"); 444 case dwarf::DW_FORM_GNU_str_index: 445 LLVM_FALLTHROUGH; 446 case dwarf::DW_FORM_GNU_addr_index: 447 LLVM_FALLTHROUGH; 448 case dwarf::DW_FORM_ref_udata: 449 LLVM_FALLTHROUGH; 450 case dwarf::DW_FORM_udata: 451 return getULEB128Size(Integer); 452 case dwarf::DW_FORM_sdata: 453 return getSLEB128Size(Integer); 454 case dwarf::DW_FORM_addr: 455 return AP->getPointerSize(); 456 default: llvm_unreachable("DIE Value form not supported yet"); 457 } 458 } 459 460 LLVM_DUMP_METHOD 461 void DIEInteger::print(raw_ostream &O) const { 462 O << "Int: " << (int64_t)Integer << " 0x"; 463 O.write_hex(Integer); 464 } 465 466 //===----------------------------------------------------------------------===// 467 // DIEExpr Implementation 468 //===----------------------------------------------------------------------===// 469 470 /// EmitValue - Emit expression value. 471 /// 472 void DIEExpr::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const { 473 AP->OutStreamer->EmitValue(Expr, SizeOf(AP, Form)); 474 } 475 476 /// SizeOf - Determine size of expression value in bytes. 477 /// 478 unsigned DIEExpr::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const { 479 if (Form == dwarf::DW_FORM_data4) return 4; 480 if (Form == dwarf::DW_FORM_sec_offset) return 4; 481 if (Form == dwarf::DW_FORM_strp) return 4; 482 return AP->getPointerSize(); 483 } 484 485 LLVM_DUMP_METHOD 486 void DIEExpr::print(raw_ostream &O) const { O << "Expr: " << *Expr; } 487 488 //===----------------------------------------------------------------------===// 489 // DIELabel Implementation 490 //===----------------------------------------------------------------------===// 491 492 /// EmitValue - Emit label value. 493 /// 494 void DIELabel::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const { 495 AP->EmitLabelReference(Label, SizeOf(AP, Form), 496 Form == dwarf::DW_FORM_strp || 497 Form == dwarf::DW_FORM_sec_offset || 498 Form == dwarf::DW_FORM_ref_addr); 499 } 500 501 /// SizeOf - Determine size of label value in bytes. 502 /// 503 unsigned DIELabel::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const { 504 if (Form == dwarf::DW_FORM_data4) return 4; 505 if (Form == dwarf::DW_FORM_sec_offset) return 4; 506 if (Form == dwarf::DW_FORM_strp) return 4; 507 return AP->getPointerSize(); 508 } 509 510 LLVM_DUMP_METHOD 511 void DIELabel::print(raw_ostream &O) const { O << "Lbl: " << Label->getName(); } 512 513 //===----------------------------------------------------------------------===// 514 // DIEDelta Implementation 515 //===----------------------------------------------------------------------===// 516 517 /// EmitValue - Emit delta value. 518 /// 519 void DIEDelta::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const { 520 AP->EmitLabelDifference(LabelHi, LabelLo, SizeOf(AP, Form)); 521 } 522 523 /// SizeOf - Determine size of delta value in bytes. 524 /// 525 unsigned DIEDelta::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const { 526 if (Form == dwarf::DW_FORM_data4) return 4; 527 if (Form == dwarf::DW_FORM_sec_offset) return 4; 528 if (Form == dwarf::DW_FORM_strp) return 4; 529 return AP->getPointerSize(); 530 } 531 532 LLVM_DUMP_METHOD 533 void DIEDelta::print(raw_ostream &O) const { 534 O << "Del: " << LabelHi->getName() << "-" << LabelLo->getName(); 535 } 536 537 //===----------------------------------------------------------------------===// 538 // DIEString Implementation 539 //===----------------------------------------------------------------------===// 540 541 /// EmitValue - Emit string value. 542 /// 543 void DIEString::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const { 544 assert( 545 (Form == dwarf::DW_FORM_strp || Form == dwarf::DW_FORM_GNU_str_index) && 546 "Expected valid string form"); 547 548 // Index of string in symbol table. 549 if (Form == dwarf::DW_FORM_GNU_str_index) { 550 DIEInteger(S.getIndex()).EmitValue(AP, Form); 551 return; 552 } 553 554 // Relocatable symbol. 555 assert(Form == dwarf::DW_FORM_strp); 556 if (AP->MAI->doesDwarfUseRelocationsAcrossSections()) { 557 DIELabel(S.getSymbol()).EmitValue(AP, Form); 558 return; 559 } 560 561 // Offset into symbol table. 562 DIEInteger(S.getOffset()).EmitValue(AP, Form); 563 } 564 565 /// SizeOf - Determine size of delta value in bytes. 566 /// 567 unsigned DIEString::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const { 568 assert( 569 (Form == dwarf::DW_FORM_strp || Form == dwarf::DW_FORM_GNU_str_index) && 570 "Expected valid string form"); 571 572 // Index of string in symbol table. 573 if (Form == dwarf::DW_FORM_GNU_str_index) 574 return DIEInteger(S.getIndex()).SizeOf(AP, Form); 575 576 // Relocatable symbol. 577 if (AP->MAI->doesDwarfUseRelocationsAcrossSections()) 578 return DIELabel(S.getSymbol()).SizeOf(AP, Form); 579 580 // Offset into symbol table. 581 return DIEInteger(S.getOffset()).SizeOf(AP, Form); 582 } 583 584 LLVM_DUMP_METHOD 585 void DIEString::print(raw_ostream &O) const { 586 O << "String: " << S.getString(); 587 } 588 589 //===----------------------------------------------------------------------===// 590 // DIEInlineString Implementation 591 //===----------------------------------------------------------------------===// 592 void DIEInlineString::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const { 593 if (Form == dwarf::DW_FORM_string) { 594 for (char ch : S) 595 AP->EmitInt8(ch); 596 AP->EmitInt8(0); 597 return; 598 } 599 llvm_unreachable("Expected valid string form"); 600 } 601 602 unsigned DIEInlineString::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const { 603 // Emit string bytes + NULL byte. 604 return S.size() + 1; 605 } 606 607 LLVM_DUMP_METHOD 608 void DIEInlineString::print(raw_ostream &O) const { 609 O << "InlineString: " << S; 610 } 611 612 //===----------------------------------------------------------------------===// 613 // DIEEntry Implementation 614 //===----------------------------------------------------------------------===// 615 616 /// EmitValue - Emit debug information entry offset. 617 /// 618 void DIEEntry::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const { 619 620 switch (Form) { 621 case dwarf::DW_FORM_ref1: 622 case dwarf::DW_FORM_ref2: 623 case dwarf::DW_FORM_ref4: 624 case dwarf::DW_FORM_ref8: 625 AP->OutStreamer->EmitIntValue(Entry->getOffset(), SizeOf(AP, Form)); 626 return; 627 628 case dwarf::DW_FORM_ref_udata: 629 AP->EmitULEB128(Entry->getOffset()); 630 return; 631 632 case dwarf::DW_FORM_ref_addr: { 633 // Get the absolute offset for this DIE within the debug info/types section. 634 unsigned Addr = Entry->getDebugSectionOffset(); 635 if (AP->MAI->doesDwarfUseRelocationsAcrossSections()) { 636 const DwarfDebug *DD = AP->getDwarfDebug(); 637 if (DD) 638 assert(!DD->useSplitDwarf() && 639 "TODO: dwo files can't have relocations."); 640 const DIEUnit *Unit = Entry->getUnit(); 641 assert(Unit && "CUDie should belong to a CU."); 642 MCSection *Section = Unit->getSection(); 643 if (Section) { 644 const MCSymbol *SectionSym = Section->getBeginSymbol(); 645 AP->EmitLabelPlusOffset(SectionSym, Addr, SizeOf(AP, Form)); 646 return; 647 } 648 } 649 AP->OutStreamer->EmitIntValue(Addr, SizeOf(AP, Form)); 650 return; 651 } 652 default: 653 llvm_unreachable("Improper form for DIE reference"); 654 } 655 } 656 657 unsigned DIEEntry::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const { 658 switch (Form) { 659 case dwarf::DW_FORM_ref1: 660 return 1; 661 case dwarf::DW_FORM_ref2: 662 return 2; 663 case dwarf::DW_FORM_ref4: 664 return 4; 665 case dwarf::DW_FORM_ref8: 666 return 8; 667 case dwarf::DW_FORM_ref_udata: 668 return getULEB128Size(Entry->getOffset()); 669 case dwarf::DW_FORM_ref_addr: 670 if (AP->getDwarfVersion() == 2) 671 return AP->getPointerSize(); 672 switch (AP->OutStreamer->getContext().getDwarfFormat()) { 673 case dwarf::DWARF32: 674 return 4; 675 case dwarf::DWARF64: 676 return 8; 677 } 678 llvm_unreachable("Invalid DWARF format"); 679 680 default: 681 llvm_unreachable("Improper form for DIE reference"); 682 } 683 } 684 685 LLVM_DUMP_METHOD 686 void DIEEntry::print(raw_ostream &O) const { 687 O << format("Die: 0x%lx", (long)(intptr_t)&Entry); 688 } 689 690 //===----------------------------------------------------------------------===// 691 // DIELoc Implementation 692 //===----------------------------------------------------------------------===// 693 694 /// ComputeSize - calculate the size of the location expression. 695 /// 696 unsigned DIELoc::ComputeSize(const AsmPrinter *AP) const { 697 if (!Size) { 698 for (const auto &V : values()) 699 Size += V.SizeOf(AP); 700 } 701 702 return Size; 703 } 704 705 /// EmitValue - Emit location data. 706 /// 707 void DIELoc::EmitValue(const AsmPrinter *Asm, dwarf::Form Form) const { 708 switch (Form) { 709 default: llvm_unreachable("Improper form for block"); 710 case dwarf::DW_FORM_block1: Asm->EmitInt8(Size); break; 711 case dwarf::DW_FORM_block2: Asm->EmitInt16(Size); break; 712 case dwarf::DW_FORM_block4: Asm->EmitInt32(Size); break; 713 case dwarf::DW_FORM_block: 714 case dwarf::DW_FORM_exprloc: 715 Asm->EmitULEB128(Size); break; 716 } 717 718 for (const auto &V : values()) 719 V.EmitValue(Asm); 720 } 721 722 /// SizeOf - Determine size of location data in bytes. 723 /// 724 unsigned DIELoc::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const { 725 switch (Form) { 726 case dwarf::DW_FORM_block1: return Size + sizeof(int8_t); 727 case dwarf::DW_FORM_block2: return Size + sizeof(int16_t); 728 case dwarf::DW_FORM_block4: return Size + sizeof(int32_t); 729 case dwarf::DW_FORM_block: 730 case dwarf::DW_FORM_exprloc: 731 return Size + getULEB128Size(Size); 732 default: llvm_unreachable("Improper form for block"); 733 } 734 } 735 736 LLVM_DUMP_METHOD 737 void DIELoc::print(raw_ostream &O) const { 738 printValues(O, *this, "ExprLoc", Size, 5); 739 } 740 741 //===----------------------------------------------------------------------===// 742 // DIEBlock Implementation 743 //===----------------------------------------------------------------------===// 744 745 /// ComputeSize - calculate the size of the block. 746 /// 747 unsigned DIEBlock::ComputeSize(const AsmPrinter *AP) const { 748 if (!Size) { 749 for (const auto &V : values()) 750 Size += V.SizeOf(AP); 751 } 752 753 return Size; 754 } 755 756 /// EmitValue - Emit block data. 757 /// 758 void DIEBlock::EmitValue(const AsmPrinter *Asm, dwarf::Form Form) const { 759 switch (Form) { 760 default: llvm_unreachable("Improper form for block"); 761 case dwarf::DW_FORM_block1: Asm->EmitInt8(Size); break; 762 case dwarf::DW_FORM_block2: Asm->EmitInt16(Size); break; 763 case dwarf::DW_FORM_block4: Asm->EmitInt32(Size); break; 764 case dwarf::DW_FORM_block: Asm->EmitULEB128(Size); break; 765 } 766 767 for (const auto &V : values()) 768 V.EmitValue(Asm); 769 } 770 771 /// SizeOf - Determine size of block data in bytes. 772 /// 773 unsigned DIEBlock::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const { 774 switch (Form) { 775 case dwarf::DW_FORM_block1: return Size + sizeof(int8_t); 776 case dwarf::DW_FORM_block2: return Size + sizeof(int16_t); 777 case dwarf::DW_FORM_block4: return Size + sizeof(int32_t); 778 case dwarf::DW_FORM_block: return Size + getULEB128Size(Size); 779 default: llvm_unreachable("Improper form for block"); 780 } 781 } 782 783 LLVM_DUMP_METHOD 784 void DIEBlock::print(raw_ostream &O) const { 785 printValues(O, *this, "Blk", Size, 5); 786 } 787 788 //===----------------------------------------------------------------------===// 789 // DIELocList Implementation 790 //===----------------------------------------------------------------------===// 791 792 unsigned DIELocList::SizeOf(const AsmPrinter *AP, dwarf::Form Form) const { 793 if (Form == dwarf::DW_FORM_data4) 794 return 4; 795 if (Form == dwarf::DW_FORM_sec_offset) 796 return 4; 797 return AP->getPointerSize(); 798 } 799 800 /// EmitValue - Emit label value. 801 /// 802 void DIELocList::EmitValue(const AsmPrinter *AP, dwarf::Form Form) const { 803 DwarfDebug *DD = AP->getDwarfDebug(); 804 MCSymbol *Label = DD->getDebugLocs().getList(Index).Label; 805 AP->emitDwarfSymbolReference(Label, /*ForceOffset*/ DD->useSplitDwarf()); 806 } 807 808 LLVM_DUMP_METHOD 809 void DIELocList::print(raw_ostream &O) const { O << "LocList: " << Index; } 810