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 "llvm/ADT/Twine.h" 17 #include "llvm/CodeGen/AsmPrinter.h" 18 #include "llvm/IR/DataLayout.h" 19 #include "llvm/MC/MCAsmInfo.h" 20 #include "llvm/MC/MCStreamer.h" 21 #include "llvm/MC/MCSymbol.h" 22 #include "llvm/Support/Allocator.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 compile unit DIE to which this DIE 116 /// belongs. 117 const DIE *DIE::getCompileUnit() const { 118 const DIE *Cu = getCompileUnitOrNull(); 119 assert(Cu && "We should not have orphaned DIEs."); 120 return Cu; 121 } 122 123 /// Climb up the parent chain to get the compile unit DIE this DIE belongs 124 /// to. Return NULL if DIE is not added to an owner yet. 125 const DIE *DIE::getCompileUnitOrNull() const { 126 const DIE *p = this; 127 while (p) { 128 if (p->getTag() == dwarf::DW_TAG_compile_unit) 129 return p; 130 p = p->getParent(); 131 } 132 return NULL; 133 } 134 135 DIEValue *DIE::findAttribute(uint16_t Attribute) { 136 const SmallVectorImpl<DIEValue *> &Values = getValues(); 137 const DIEAbbrev &Abbrevs = getAbbrev(); 138 139 // Iterate through all the attributes until we find the one we're 140 // looking for, if we can't find it return NULL. 141 for (size_t i = 0; i < Values.size(); ++i) 142 if (Abbrevs.getData()[i].getAttribute() == Attribute) 143 return Values[i]; 144 return NULL; 145 } 146 147 #ifndef NDEBUG 148 void DIE::print(raw_ostream &O, unsigned IndentCount) const { 149 const std::string Indent(IndentCount, ' '); 150 bool isBlock = Abbrev.getTag() == 0; 151 152 if (!isBlock) { 153 O << Indent 154 << "Die: " 155 << format("0x%lx", (long)(intptr_t)this) 156 << ", Offset: " << Offset 157 << ", Size: " << Size << "\n"; 158 159 O << Indent 160 << dwarf::TagString(Abbrev.getTag()) 161 << " " 162 << dwarf::ChildrenString(Abbrev.getChildrenFlag()) << "\n"; 163 } else { 164 O << "Size: " << Size << "\n"; 165 } 166 167 const SmallVectorImpl<DIEAbbrevData> &Data = Abbrev.getData(); 168 169 IndentCount += 2; 170 for (unsigned i = 0, N = Data.size(); i < N; ++i) { 171 O << Indent; 172 173 if (!isBlock) 174 O << dwarf::AttributeString(Data[i].getAttribute()); 175 else 176 O << "Blk[" << i << "]"; 177 178 O << " " 179 << dwarf::FormEncodingString(Data[i].getForm()) 180 << " "; 181 Values[i]->print(O); 182 O << "\n"; 183 } 184 IndentCount -= 2; 185 186 for (unsigned j = 0, M = Children.size(); j < M; ++j) { 187 Children[j]->print(O, IndentCount+4); 188 } 189 190 if (!isBlock) O << "\n"; 191 } 192 193 void DIE::dump() { 194 print(dbgs()); 195 } 196 #endif 197 198 void DIEValue::anchor() { } 199 200 #ifndef NDEBUG 201 void DIEValue::dump() const { 202 print(dbgs()); 203 } 204 #endif 205 206 //===----------------------------------------------------------------------===// 207 // DIEInteger Implementation 208 //===----------------------------------------------------------------------===// 209 210 /// EmitValue - Emit integer of appropriate size. 211 /// 212 void DIEInteger::EmitValue(AsmPrinter *Asm, dwarf::Form Form) const { 213 unsigned Size = ~0U; 214 switch (Form) { 215 case dwarf::DW_FORM_flag_present: 216 // Emit something to keep the lines and comments in sync. 217 // FIXME: Is there a better way to do this? 218 if (Asm->OutStreamer.hasRawTextSupport()) 219 Asm->OutStreamer.EmitRawText(""); 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_data8: Size = 8; break; 231 case dwarf::DW_FORM_GNU_str_index: Asm->EmitULEB128(Integer); return; 232 case dwarf::DW_FORM_GNU_addr_index: Asm->EmitULEB128(Integer); return; 233 case dwarf::DW_FORM_udata: Asm->EmitULEB128(Integer); return; 234 case dwarf::DW_FORM_sdata: Asm->EmitSLEB128(Integer); return; 235 case dwarf::DW_FORM_addr: 236 Size = Asm->getDataLayout().getPointerSize(); break; 237 default: llvm_unreachable("DIE Value form not supported yet"); 238 } 239 Asm->OutStreamer.EmitIntValue(Integer, Size); 240 } 241 242 /// SizeOf - Determine size of integer value in bytes. 243 /// 244 unsigned DIEInteger::SizeOf(AsmPrinter *AP, dwarf::Form Form) const { 245 switch (Form) { 246 case dwarf::DW_FORM_flag_present: return 0; 247 case dwarf::DW_FORM_flag: // Fall thru 248 case dwarf::DW_FORM_ref1: // Fall thru 249 case dwarf::DW_FORM_data1: return sizeof(int8_t); 250 case dwarf::DW_FORM_ref2: // Fall thru 251 case dwarf::DW_FORM_data2: return sizeof(int16_t); 252 case dwarf::DW_FORM_sec_offset: // Fall thru 253 case dwarf::DW_FORM_ref4: // Fall thru 254 case dwarf::DW_FORM_data4: return sizeof(int32_t); 255 case dwarf::DW_FORM_ref8: // Fall thru 256 case dwarf::DW_FORM_data8: return sizeof(int64_t); 257 case dwarf::DW_FORM_GNU_str_index: return MCAsmInfo::getULEB128Size(Integer); 258 case dwarf::DW_FORM_GNU_addr_index: return MCAsmInfo::getULEB128Size(Integer); 259 case dwarf::DW_FORM_udata: return MCAsmInfo::getULEB128Size(Integer); 260 case dwarf::DW_FORM_sdata: return MCAsmInfo::getSLEB128Size(Integer); 261 case dwarf::DW_FORM_addr: return AP->getDataLayout().getPointerSize(); 262 default: llvm_unreachable("DIE Value form not supported yet"); 263 } 264 } 265 266 #ifndef NDEBUG 267 void DIEInteger::print(raw_ostream &O) const { 268 O << "Int: " << (int64_t)Integer << " 0x"; 269 O.write_hex(Integer); 270 } 271 #endif 272 273 //===----------------------------------------------------------------------===// 274 // DIEExpr Implementation 275 //===----------------------------------------------------------------------===// 276 277 /// EmitValue - Emit expression value. 278 /// 279 void DIEExpr::EmitValue(AsmPrinter *AP, dwarf::Form Form) const { 280 AP->OutStreamer.EmitValue(Expr, SizeOf(AP, Form)); 281 } 282 283 /// SizeOf - Determine size of expression value in bytes. 284 /// 285 unsigned DIEExpr::SizeOf(AsmPrinter *AP, dwarf::Form Form) const { 286 if (Form == dwarf::DW_FORM_data4) return 4; 287 if (Form == dwarf::DW_FORM_sec_offset) return 4; 288 if (Form == dwarf::DW_FORM_strp) return 4; 289 return AP->getDataLayout().getPointerSize(); 290 } 291 292 #ifndef NDEBUG 293 void DIEExpr::print(raw_ostream &O) const { 294 O << "Expr: "; 295 Expr->print(O); 296 } 297 #endif 298 299 //===----------------------------------------------------------------------===// 300 // DIELabel Implementation 301 //===----------------------------------------------------------------------===// 302 303 /// EmitValue - Emit label value. 304 /// 305 void DIELabel::EmitValue(AsmPrinter *AP, dwarf::Form Form) const { 306 AP->EmitLabelReference(Label, SizeOf(AP, Form), 307 Form == dwarf::DW_FORM_strp || 308 Form == dwarf::DW_FORM_sec_offset || 309 Form == dwarf::DW_FORM_ref_addr); 310 } 311 312 /// SizeOf - Determine size of label value in bytes. 313 /// 314 unsigned DIELabel::SizeOf(AsmPrinter *AP, dwarf::Form Form) const { 315 if (Form == dwarf::DW_FORM_data4) return 4; 316 if (Form == dwarf::DW_FORM_sec_offset) return 4; 317 if (Form == dwarf::DW_FORM_strp) return 4; 318 return AP->getDataLayout().getPointerSize(); 319 } 320 321 #ifndef NDEBUG 322 void DIELabel::print(raw_ostream &O) const { 323 O << "Lbl: " << Label->getName(); 324 } 325 #endif 326 327 //===----------------------------------------------------------------------===// 328 // DIEDelta Implementation 329 //===----------------------------------------------------------------------===// 330 331 /// EmitValue - Emit delta value. 332 /// 333 void DIEDelta::EmitValue(AsmPrinter *AP, dwarf::Form Form) const { 334 AP->EmitLabelDifference(LabelHi, LabelLo, SizeOf(AP, Form)); 335 } 336 337 /// SizeOf - Determine size of delta value in bytes. 338 /// 339 unsigned DIEDelta::SizeOf(AsmPrinter *AP, dwarf::Form Form) const { 340 if (Form == dwarf::DW_FORM_data4) return 4; 341 if (Form == dwarf::DW_FORM_strp) return 4; 342 return AP->getDataLayout().getPointerSize(); 343 } 344 345 #ifndef NDEBUG 346 void DIEDelta::print(raw_ostream &O) const { 347 O << "Del: " << LabelHi->getName() << "-" << LabelLo->getName(); 348 } 349 #endif 350 351 //===----------------------------------------------------------------------===// 352 // DIEString Implementation 353 //===----------------------------------------------------------------------===// 354 355 /// EmitValue - Emit string value. 356 /// 357 void DIEString::EmitValue(AsmPrinter *AP, dwarf::Form Form) const { 358 Access->EmitValue(AP, Form); 359 } 360 361 /// SizeOf - Determine size of delta value in bytes. 362 /// 363 unsigned DIEString::SizeOf(AsmPrinter *AP, dwarf::Form Form) const { 364 return Access->SizeOf(AP, Form); 365 } 366 367 #ifndef NDEBUG 368 void DIEString::print(raw_ostream &O) const { 369 O << "String: " << Str << "\tSymbol: "; 370 Access->print(O); 371 } 372 #endif 373 374 //===----------------------------------------------------------------------===// 375 // DIEEntry Implementation 376 //===----------------------------------------------------------------------===// 377 378 /// EmitValue - Emit debug information entry offset. 379 /// 380 void DIEEntry::EmitValue(AsmPrinter *AP, dwarf::Form Form) const { 381 AP->EmitInt32(Entry->getOffset()); 382 } 383 384 unsigned DIEEntry::getRefAddrSize(AsmPrinter *AP) { 385 // DWARF4: References that use the attribute form DW_FORM_ref_addr are 386 // specified to be four bytes in the DWARF 32-bit format and eight bytes 387 // in the DWARF 64-bit format, while DWARF Version 2 specifies that such 388 // references have the same size as an address on the target system. 389 if (AP->getDwarfDebug()->getDwarfVersion() == 2) 390 return AP->getDataLayout().getPointerSize(); 391 return sizeof(int32_t); 392 } 393 394 #ifndef NDEBUG 395 void DIEEntry::print(raw_ostream &O) const { 396 O << format("Die: 0x%lx", (long)(intptr_t)Entry); 397 } 398 #endif 399 400 //===----------------------------------------------------------------------===// 401 // DIEBlock Implementation 402 //===----------------------------------------------------------------------===// 403 404 /// ComputeSize - calculate the size of the block. 405 /// 406 unsigned DIEBlock::ComputeSize(AsmPrinter *AP) { 407 if (!Size) { 408 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData(); 409 for (unsigned i = 0, N = Values.size(); i < N; ++i) 410 Size += Values[i]->SizeOf(AP, AbbrevData[i].getForm()); 411 } 412 413 return Size; 414 } 415 416 /// EmitValue - Emit block data. 417 /// 418 void DIEBlock::EmitValue(AsmPrinter *Asm, dwarf::Form Form) const { 419 switch (Form) { 420 default: llvm_unreachable("Improper form for block"); 421 case dwarf::DW_FORM_block1: Asm->EmitInt8(Size); break; 422 case dwarf::DW_FORM_block2: Asm->EmitInt16(Size); break; 423 case dwarf::DW_FORM_block4: Asm->EmitInt32(Size); break; 424 case dwarf::DW_FORM_block: Asm->EmitULEB128(Size); break; 425 } 426 427 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData(); 428 for (unsigned i = 0, N = Values.size(); i < N; ++i) 429 Values[i]->EmitValue(Asm, AbbrevData[i].getForm()); 430 } 431 432 /// SizeOf - Determine size of block data in bytes. 433 /// 434 unsigned DIEBlock::SizeOf(AsmPrinter *AP, dwarf::Form Form) const { 435 switch (Form) { 436 case dwarf::DW_FORM_block1: return Size + sizeof(int8_t); 437 case dwarf::DW_FORM_block2: return Size + sizeof(int16_t); 438 case dwarf::DW_FORM_block4: return Size + sizeof(int32_t); 439 case dwarf::DW_FORM_block: return Size + MCAsmInfo::getULEB128Size(Size); 440 default: llvm_unreachable("Improper form for block"); 441 } 442 } 443 444 #ifndef NDEBUG 445 void DIEBlock::print(raw_ostream &O) const { 446 O << "Blk: "; 447 DIE::print(O, 5); 448 } 449 #endif 450