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