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 using namespace llvm; 32 33 //===----------------------------------------------------------------------===// 34 // DIEAbbrevData Implementation 35 //===----------------------------------------------------------------------===// 36 37 /// Profile - Used to gather unique data for the abbreviation folding set. 38 /// 39 void DIEAbbrevData::Profile(FoldingSetNodeID &ID) const { 40 // Explicitly cast to an integer type for which FoldingSetNodeID has 41 // overloads. Otherwise MSVC 2010 thinks this call is ambiguous. 42 ID.AddInteger(unsigned(Attribute)); 43 ID.AddInteger(unsigned(Form)); 44 } 45 46 //===----------------------------------------------------------------------===// 47 // DIEAbbrev Implementation 48 //===----------------------------------------------------------------------===// 49 50 /// Profile - Used to gather unique data for the abbreviation folding set. 51 /// 52 void DIEAbbrev::Profile(FoldingSetNodeID &ID) const { 53 ID.AddInteger(unsigned(Tag)); 54 ID.AddInteger(unsigned(Children)); 55 56 // For each attribute description. 57 for (unsigned i = 0, N = Data.size(); i < N; ++i) 58 Data[i].Profile(ID); 59 } 60 61 /// Emit - Print the abbreviation using the specified asm printer. 62 /// 63 void DIEAbbrev::Emit(AsmPrinter *AP) const { 64 // Emit its Dwarf tag type. 65 AP->EmitULEB128(Tag, dwarf::TagString(Tag)); 66 67 // Emit whether it has children DIEs. 68 AP->EmitULEB128((unsigned)Children, dwarf::ChildrenString(Children)); 69 70 // For each attribute description. 71 for (unsigned i = 0, N = Data.size(); i < N; ++i) { 72 const DIEAbbrevData &AttrData = Data[i]; 73 74 // Emit attribute type. 75 AP->EmitULEB128(AttrData.getAttribute(), 76 dwarf::AttributeString(AttrData.getAttribute())); 77 78 // Emit form type. 79 AP->EmitULEB128(AttrData.getForm(), 80 dwarf::FormEncodingString(AttrData.getForm())); 81 } 82 83 // Mark end of abbreviation. 84 AP->EmitULEB128(0, "EOM(1)"); 85 AP->EmitULEB128(0, "EOM(2)"); 86 } 87 88 #ifndef NDEBUG 89 void DIEAbbrev::print(raw_ostream &O) { 90 O << "Abbreviation @" 91 << format("0x%lx", (long)(intptr_t)this) 92 << " " 93 << dwarf::TagString(Tag) 94 << " " 95 << dwarf::ChildrenString(Children) 96 << '\n'; 97 98 for (unsigned i = 0, N = Data.size(); i < N; ++i) { 99 O << " " 100 << dwarf::AttributeString(Data[i].getAttribute()) 101 << " " 102 << dwarf::FormEncodingString(Data[i].getForm()) 103 << '\n'; 104 } 105 } 106 void DIEAbbrev::dump() { print(dbgs()); } 107 #endif 108 109 /// Climb up the parent chain to get the unit DIE to which this DIE 110 /// belongs. 111 const DIE *DIE::getUnit() const { 112 const DIE *Cu = getUnitOrNull(); 113 assert(Cu && "We should not have orphaned DIEs."); 114 return Cu; 115 } 116 117 /// Climb up the parent chain to get the unit DIE this DIE belongs 118 /// to. Return NULL if DIE is not added to an owner yet. 119 const DIE *DIE::getUnitOrNull() const { 120 const DIE *p = this; 121 while (p) { 122 if (p->getTag() == dwarf::DW_TAG_compile_unit || 123 p->getTag() == dwarf::DW_TAG_type_unit) 124 return p; 125 p = p->getParent(); 126 } 127 return nullptr; 128 } 129 130 DIEValue *DIE::findAttribute(dwarf::Attribute Attribute) const { 131 const SmallVectorImpl<DIEValue *> &Values = getValues(); 132 const DIEAbbrev &Abbrevs = getAbbrev(); 133 134 // Iterate through all the attributes until we find the one we're 135 // looking for, if we can't find it return NULL. 136 for (size_t i = 0; i < Values.size(); ++i) 137 if (Abbrevs.getData()[i].getAttribute() == Attribute) 138 return Values[i]; 139 return nullptr; 140 } 141 142 #ifndef NDEBUG 143 void DIE::print(raw_ostream &O, unsigned IndentCount) const { 144 const std::string Indent(IndentCount, ' '); 145 bool isBlock = Abbrev.getTag() == 0; 146 147 if (!isBlock) { 148 O << Indent 149 << "Die: " 150 << format("0x%lx", (long)(intptr_t)this) 151 << ", Offset: " << Offset 152 << ", Size: " << Size << "\n"; 153 154 O << Indent 155 << dwarf::TagString(Abbrev.getTag()) 156 << " " 157 << dwarf::ChildrenString(Abbrev.hasChildren()) << "\n"; 158 } else { 159 O << "Size: " << Size << "\n"; 160 } 161 162 const SmallVectorImpl<DIEAbbrevData> &Data = Abbrev.getData(); 163 164 IndentCount += 2; 165 for (unsigned i = 0, N = Data.size(); i < N; ++i) { 166 O << Indent; 167 168 if (!isBlock) 169 O << dwarf::AttributeString(Data[i].getAttribute()); 170 else 171 O << "Blk[" << i << "]"; 172 173 O << " " 174 << dwarf::FormEncodingString(Data[i].getForm()) 175 << " "; 176 Values[i]->print(O); 177 O << "\n"; 178 } 179 IndentCount -= 2; 180 181 for (unsigned j = 0, M = Children.size(); j < M; ++j) { 182 Children[j]->print(O, IndentCount+4); 183 } 184 185 if (!isBlock) O << "\n"; 186 } 187 188 void DIE::dump() { 189 print(dbgs()); 190 } 191 #endif 192 193 void DIEValue::anchor() { } 194 195 #ifndef NDEBUG 196 void DIEValue::dump() const { 197 print(dbgs()); 198 } 199 #endif 200 201 //===----------------------------------------------------------------------===// 202 // DIEInteger Implementation 203 //===----------------------------------------------------------------------===// 204 205 /// EmitValue - Emit integer of appropriate size. 206 /// 207 void DIEInteger::EmitValue(AsmPrinter *Asm, dwarf::Form Form) const { 208 unsigned Size = ~0U; 209 switch (Form) { 210 case dwarf::DW_FORM_flag_present: 211 // Emit something to keep the lines and comments in sync. 212 // FIXME: Is there a better way to do this? 213 Asm->OutStreamer.AddBlankLine(); 214 return; 215 case dwarf::DW_FORM_flag: // Fall thru 216 case dwarf::DW_FORM_ref1: // Fall thru 217 case dwarf::DW_FORM_data1: Size = 1; break; 218 case dwarf::DW_FORM_ref2: // Fall thru 219 case dwarf::DW_FORM_data2: Size = 2; break; 220 case dwarf::DW_FORM_sec_offset: // Fall thru 221 case dwarf::DW_FORM_ref4: // Fall thru 222 case dwarf::DW_FORM_data4: Size = 4; break; 223 case dwarf::DW_FORM_ref8: // Fall thru 224 case dwarf::DW_FORM_ref_sig8: // Fall thru 225 case dwarf::DW_FORM_data8: Size = 8; break; 226 case dwarf::DW_FORM_GNU_str_index: Asm->EmitULEB128(Integer); return; 227 case dwarf::DW_FORM_GNU_addr_index: Asm->EmitULEB128(Integer); return; 228 case dwarf::DW_FORM_udata: Asm->EmitULEB128(Integer); return; 229 case dwarf::DW_FORM_sdata: Asm->EmitSLEB128(Integer); return; 230 case dwarf::DW_FORM_addr: 231 Size = Asm->getDataLayout().getPointerSize(); break; 232 default: llvm_unreachable("DIE Value form not supported yet"); 233 } 234 Asm->OutStreamer.EmitIntValue(Integer, Size); 235 } 236 237 /// SizeOf - Determine size of integer value in bytes. 238 /// 239 unsigned DIEInteger::SizeOf(AsmPrinter *AP, dwarf::Form Form) const { 240 switch (Form) { 241 case dwarf::DW_FORM_flag_present: return 0; 242 case dwarf::DW_FORM_flag: // Fall thru 243 case dwarf::DW_FORM_ref1: // Fall thru 244 case dwarf::DW_FORM_data1: return sizeof(int8_t); 245 case dwarf::DW_FORM_ref2: // Fall thru 246 case dwarf::DW_FORM_data2: return sizeof(int16_t); 247 case dwarf::DW_FORM_sec_offset: // Fall thru 248 case dwarf::DW_FORM_ref4: // Fall thru 249 case dwarf::DW_FORM_data4: return sizeof(int32_t); 250 case dwarf::DW_FORM_ref8: // Fall thru 251 case dwarf::DW_FORM_ref_sig8: // Fall thru 252 case dwarf::DW_FORM_data8: return sizeof(int64_t); 253 case dwarf::DW_FORM_GNU_str_index: return getULEB128Size(Integer); 254 case dwarf::DW_FORM_GNU_addr_index: return getULEB128Size(Integer); 255 case dwarf::DW_FORM_udata: return getULEB128Size(Integer); 256 case dwarf::DW_FORM_sdata: return getSLEB128Size(Integer); 257 case dwarf::DW_FORM_addr: return AP->getDataLayout().getPointerSize(); 258 default: llvm_unreachable("DIE Value form not supported yet"); 259 } 260 } 261 262 #ifndef NDEBUG 263 void DIEInteger::print(raw_ostream &O) const { 264 O << "Int: " << (int64_t)Integer << " 0x"; 265 O.write_hex(Integer); 266 } 267 #endif 268 269 //===----------------------------------------------------------------------===// 270 // DIEExpr Implementation 271 //===----------------------------------------------------------------------===// 272 273 /// EmitValue - Emit expression value. 274 /// 275 void DIEExpr::EmitValue(AsmPrinter *AP, dwarf::Form Form) const { 276 AP->OutStreamer.EmitValue(Expr, SizeOf(AP, Form)); 277 } 278 279 /// SizeOf - Determine size of expression value in bytes. 280 /// 281 unsigned DIEExpr::SizeOf(AsmPrinter *AP, dwarf::Form Form) const { 282 if (Form == dwarf::DW_FORM_data4) return 4; 283 if (Form == dwarf::DW_FORM_sec_offset) return 4; 284 if (Form == dwarf::DW_FORM_strp) return 4; 285 return AP->getDataLayout().getPointerSize(); 286 } 287 288 #ifndef NDEBUG 289 void DIEExpr::print(raw_ostream &O) const { 290 O << "Expr: "; 291 Expr->print(O); 292 } 293 #endif 294 295 //===----------------------------------------------------------------------===// 296 // DIELabel Implementation 297 //===----------------------------------------------------------------------===// 298 299 /// EmitValue - Emit label value. 300 /// 301 void DIELabel::EmitValue(AsmPrinter *AP, dwarf::Form Form) const { 302 AP->EmitLabelReference(Label, SizeOf(AP, Form), 303 Form == dwarf::DW_FORM_strp || 304 Form == dwarf::DW_FORM_sec_offset || 305 Form == dwarf::DW_FORM_ref_addr); 306 } 307 308 /// SizeOf - Determine size of label value in bytes. 309 /// 310 unsigned DIELabel::SizeOf(AsmPrinter *AP, dwarf::Form Form) const { 311 if (Form == dwarf::DW_FORM_data4) return 4; 312 if (Form == dwarf::DW_FORM_sec_offset) return 4; 313 if (Form == dwarf::DW_FORM_strp) return 4; 314 return AP->getDataLayout().getPointerSize(); 315 } 316 317 #ifndef NDEBUG 318 void DIELabel::print(raw_ostream &O) const { 319 O << "Lbl: " << Label->getName(); 320 } 321 #endif 322 323 //===----------------------------------------------------------------------===// 324 // DIEDelta Implementation 325 //===----------------------------------------------------------------------===// 326 327 /// EmitValue - Emit delta value. 328 /// 329 void DIEDelta::EmitValue(AsmPrinter *AP, dwarf::Form Form) const { 330 AP->EmitLabelDifference(LabelHi, LabelLo, SizeOf(AP, Form)); 331 } 332 333 /// SizeOf - Determine size of delta value in bytes. 334 /// 335 unsigned DIEDelta::SizeOf(AsmPrinter *AP, dwarf::Form Form) const { 336 if (Form == dwarf::DW_FORM_data4) return 4; 337 if (Form == dwarf::DW_FORM_sec_offset) return 4; 338 if (Form == dwarf::DW_FORM_strp) return 4; 339 return AP->getDataLayout().getPointerSize(); 340 } 341 342 #ifndef NDEBUG 343 void DIEDelta::print(raw_ostream &O) const { 344 O << "Del: " << LabelHi->getName() << "-" << LabelLo->getName(); 345 } 346 #endif 347 348 //===----------------------------------------------------------------------===// 349 // DIEString Implementation 350 //===----------------------------------------------------------------------===// 351 352 /// EmitValue - Emit string value. 353 /// 354 void DIEString::EmitValue(AsmPrinter *AP, dwarf::Form Form) const { 355 Access->EmitValue(AP, Form); 356 } 357 358 /// SizeOf - Determine size of delta value in bytes. 359 /// 360 unsigned DIEString::SizeOf(AsmPrinter *AP, dwarf::Form Form) const { 361 return Access->SizeOf(AP, Form); 362 } 363 364 #ifndef NDEBUG 365 void DIEString::print(raw_ostream &O) const { 366 O << "String: " << Str << "\tSymbol: "; 367 Access->print(O); 368 } 369 #endif 370 371 //===----------------------------------------------------------------------===// 372 // DIEEntry Implementation 373 //===----------------------------------------------------------------------===// 374 375 /// Emit something like ".long Hi+Offset-Lo" where the size in bytes of the 376 /// directive is specified by Size and Hi/Lo specify the labels. 377 static void emitLabelOffsetDifference(MCStreamer &Streamer, const MCSymbol *Hi, 378 uint64_t Offset, const MCSymbol *Lo, 379 unsigned Size) { 380 MCContext &Context = Streamer.getContext(); 381 382 // Emit Hi+Offset - Lo 383 // Get the Hi+Offset expression. 384 const MCExpr *Plus = 385 MCBinaryExpr::CreateAdd(MCSymbolRefExpr::Create(Hi, Context), 386 MCConstantExpr::Create(Offset, Context), Context); 387 388 // Get the Hi+Offset-Lo expression. 389 const MCExpr *Diff = MCBinaryExpr::CreateSub( 390 Plus, MCSymbolRefExpr::Create(Lo, Context), Context); 391 392 // Otherwise, emit with .set (aka assignment). 393 MCSymbol *SetLabel = Context.CreateTempSymbol(); 394 Streamer.EmitAssignment(SetLabel, Diff); 395 Streamer.EmitSymbolValue(SetLabel, Size); 396 } 397 398 /// EmitValue - Emit debug information entry offset. 399 /// 400 void DIEEntry::EmitValue(AsmPrinter *AP, dwarf::Form Form) const { 401 402 if (Form == dwarf::DW_FORM_ref_addr) { 403 const DwarfDebug *DD = AP->getDwarfDebug(); 404 unsigned Addr = Entry.getOffset(); 405 assert(!DD->useSplitDwarf() && "TODO: dwo files can't have relocations."); 406 // For DW_FORM_ref_addr, output the offset from beginning of debug info 407 // section. Entry->getOffset() returns the offset from start of the 408 // compile unit. 409 DwarfCompileUnit *CU = DD->lookupUnit(Entry.getUnit()); 410 assert(CU && "CUDie should belong to a CU."); 411 Addr += CU->getDebugInfoOffset(); 412 if (AP->MAI->doesDwarfUseRelocationsAcrossSections()) 413 AP->EmitLabelPlusOffset(CU->getSectionSym(), Addr, 414 DIEEntry::getRefAddrSize(AP)); 415 else 416 emitLabelOffsetDifference(AP->OutStreamer, CU->getSectionSym(), Addr, 417 CU->getSectionSym(), 418 DIEEntry::getRefAddrSize(AP)); 419 } else 420 AP->EmitInt32(Entry.getOffset()); 421 } 422 423 unsigned DIEEntry::getRefAddrSize(AsmPrinter *AP) { 424 // DWARF4: References that use the attribute form DW_FORM_ref_addr are 425 // specified to be four bytes in the DWARF 32-bit format and eight bytes 426 // in the DWARF 64-bit format, while DWARF Version 2 specifies that such 427 // references have the same size as an address on the target system. 428 const DwarfDebug *DD = AP->getDwarfDebug(); 429 assert(DD && "Expected Dwarf Debug info to be available"); 430 if (DD->getDwarfVersion() == 2) 431 return AP->getDataLayout().getPointerSize(); 432 return sizeof(int32_t); 433 } 434 435 #ifndef NDEBUG 436 void DIEEntry::print(raw_ostream &O) const { 437 O << format("Die: 0x%lx", (long)(intptr_t)&Entry); 438 } 439 #endif 440 441 //===----------------------------------------------------------------------===// 442 // DIETypeSignature Implementation 443 //===----------------------------------------------------------------------===// 444 void DIETypeSignature::EmitValue(AsmPrinter *Asm, dwarf::Form Form) const { 445 assert(Form == dwarf::DW_FORM_ref_sig8); 446 Asm->OutStreamer.EmitIntValue(Unit.getTypeSignature(), 8); 447 } 448 449 #ifndef NDEBUG 450 void DIETypeSignature::print(raw_ostream &O) const { 451 O << format("Type Unit: 0x%lx", Unit.getTypeSignature()); 452 } 453 454 void DIETypeSignature::dump() const { print(dbgs()); } 455 #endif 456 457 //===----------------------------------------------------------------------===// 458 // DIELoc Implementation 459 //===----------------------------------------------------------------------===// 460 461 /// ComputeSize - calculate the size of the location expression. 462 /// 463 unsigned DIELoc::ComputeSize(AsmPrinter *AP) const { 464 if (!Size) { 465 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData(); 466 for (unsigned i = 0, N = Values.size(); i < N; ++i) 467 Size += Values[i]->SizeOf(AP, AbbrevData[i].getForm()); 468 } 469 470 return Size; 471 } 472 473 /// EmitValue - Emit location data. 474 /// 475 void DIELoc::EmitValue(AsmPrinter *Asm, dwarf::Form Form) const { 476 switch (Form) { 477 default: llvm_unreachable("Improper form for block"); 478 case dwarf::DW_FORM_block1: Asm->EmitInt8(Size); break; 479 case dwarf::DW_FORM_block2: Asm->EmitInt16(Size); break; 480 case dwarf::DW_FORM_block4: Asm->EmitInt32(Size); break; 481 case dwarf::DW_FORM_block: 482 case dwarf::DW_FORM_exprloc: 483 Asm->EmitULEB128(Size); break; 484 } 485 486 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData(); 487 for (unsigned i = 0, N = Values.size(); i < N; ++i) 488 Values[i]->EmitValue(Asm, AbbrevData[i].getForm()); 489 } 490 491 /// SizeOf - Determine size of location data in bytes. 492 /// 493 unsigned DIELoc::SizeOf(AsmPrinter *AP, dwarf::Form Form) const { 494 switch (Form) { 495 case dwarf::DW_FORM_block1: return Size + sizeof(int8_t); 496 case dwarf::DW_FORM_block2: return Size + sizeof(int16_t); 497 case dwarf::DW_FORM_block4: return Size + sizeof(int32_t); 498 case dwarf::DW_FORM_block: 499 case dwarf::DW_FORM_exprloc: 500 return Size + getULEB128Size(Size); 501 default: llvm_unreachable("Improper form for block"); 502 } 503 } 504 505 #ifndef NDEBUG 506 void DIELoc::print(raw_ostream &O) const { 507 O << "ExprLoc: "; 508 DIE::print(O, 5); 509 } 510 #endif 511 512 //===----------------------------------------------------------------------===// 513 // DIEBlock Implementation 514 //===----------------------------------------------------------------------===// 515 516 /// ComputeSize - calculate the size of the block. 517 /// 518 unsigned DIEBlock::ComputeSize(AsmPrinter *AP) const { 519 if (!Size) { 520 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData(); 521 for (unsigned i = 0, N = Values.size(); i < N; ++i) 522 Size += Values[i]->SizeOf(AP, AbbrevData[i].getForm()); 523 } 524 525 return Size; 526 } 527 528 /// EmitValue - Emit block data. 529 /// 530 void DIEBlock::EmitValue(AsmPrinter *Asm, dwarf::Form Form) const { 531 switch (Form) { 532 default: llvm_unreachable("Improper form for block"); 533 case dwarf::DW_FORM_block1: Asm->EmitInt8(Size); break; 534 case dwarf::DW_FORM_block2: Asm->EmitInt16(Size); break; 535 case dwarf::DW_FORM_block4: Asm->EmitInt32(Size); break; 536 case dwarf::DW_FORM_block: Asm->EmitULEB128(Size); break; 537 } 538 539 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData(); 540 for (unsigned i = 0, N = Values.size(); i < N; ++i) 541 Values[i]->EmitValue(Asm, AbbrevData[i].getForm()); 542 } 543 544 /// SizeOf - Determine size of block data in bytes. 545 /// 546 unsigned DIEBlock::SizeOf(AsmPrinter *AP, dwarf::Form Form) const { 547 switch (Form) { 548 case dwarf::DW_FORM_block1: return Size + sizeof(int8_t); 549 case dwarf::DW_FORM_block2: return Size + sizeof(int16_t); 550 case dwarf::DW_FORM_block4: return Size + sizeof(int32_t); 551 case dwarf::DW_FORM_block: return Size + getULEB128Size(Size); 552 default: llvm_unreachable("Improper form for block"); 553 } 554 } 555 556 #ifndef NDEBUG 557 void DIEBlock::print(raw_ostream &O) const { 558 O << "Blk: "; 559 DIE::print(O, 5); 560 } 561 #endif 562 563 //===----------------------------------------------------------------------===// 564 // DIELocList Implementation 565 //===----------------------------------------------------------------------===// 566 567 unsigned DIELocList::SizeOf(AsmPrinter *AP, dwarf::Form Form) const { 568 if (Form == dwarf::DW_FORM_data4) 569 return 4; 570 if (Form == dwarf::DW_FORM_sec_offset) 571 return 4; 572 return AP->getDataLayout().getPointerSize(); 573 } 574 575 /// EmitValue - Emit label value. 576 /// 577 void DIELocList::EmitValue(AsmPrinter *AP, dwarf::Form Form) const { 578 DwarfDebug *DD = AP->getDwarfDebug(); 579 MCSymbol *Label = DD->getDebugLocEntries()[Index].Label; 580 581 if (AP->MAI->doesDwarfUseRelocationsAcrossSections() && !DD->useSplitDwarf()) 582 AP->EmitSectionOffset(Label, DD->getDebugLocSym()); 583 else 584 AP->EmitLabelDifference(Label, DD->getDebugLocSym(), 4); 585 } 586 587 #ifndef NDEBUG 588 void DIELocList::print(raw_ostream &O) const { 589 O << "LocList: " << Index; 590 591 } 592 #endif 593