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