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