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