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