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