1 //===- DWARFFormValue.cpp -------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "llvm/DebugInfo/DWARF/DWARFFormValue.h" 10 #include "llvm/ADT/ArrayRef.h" 11 #include "llvm/ADT/None.h" 12 #include "llvm/ADT/Optional.h" 13 #include "llvm/ADT/StringRef.h" 14 #include "llvm/BinaryFormat/Dwarf.h" 15 #include "llvm/DebugInfo/DWARF/DWARFContext.h" 16 #include "llvm/DebugInfo/DWARF/DWARFRelocMap.h" 17 #include "llvm/DebugInfo/DWARF/DWARFUnit.h" 18 #include "llvm/Support/ErrorHandling.h" 19 #include "llvm/Support/Format.h" 20 #include "llvm/Support/WithColor.h" 21 #include "llvm/Support/raw_ostream.h" 22 #include <cinttypes> 23 #include <cstdint> 24 #include <limits> 25 26 using namespace llvm; 27 using namespace dwarf; 28 29 static const DWARFFormValue::FormClass DWARF5FormClasses[] = { 30 DWARFFormValue::FC_Unknown, // 0x0 31 DWARFFormValue::FC_Address, // 0x01 DW_FORM_addr 32 DWARFFormValue::FC_Unknown, // 0x02 unused 33 DWARFFormValue::FC_Block, // 0x03 DW_FORM_block2 34 DWARFFormValue::FC_Block, // 0x04 DW_FORM_block4 35 DWARFFormValue::FC_Constant, // 0x05 DW_FORM_data2 36 // --- These can be FC_SectionOffset in DWARF3 and below: 37 DWARFFormValue::FC_Constant, // 0x06 DW_FORM_data4 38 DWARFFormValue::FC_Constant, // 0x07 DW_FORM_data8 39 // --- 40 DWARFFormValue::FC_String, // 0x08 DW_FORM_string 41 DWARFFormValue::FC_Block, // 0x09 DW_FORM_block 42 DWARFFormValue::FC_Block, // 0x0a DW_FORM_block1 43 DWARFFormValue::FC_Constant, // 0x0b DW_FORM_data1 44 DWARFFormValue::FC_Flag, // 0x0c DW_FORM_flag 45 DWARFFormValue::FC_Constant, // 0x0d DW_FORM_sdata 46 DWARFFormValue::FC_String, // 0x0e DW_FORM_strp 47 DWARFFormValue::FC_Constant, // 0x0f DW_FORM_udata 48 DWARFFormValue::FC_Reference, // 0x10 DW_FORM_ref_addr 49 DWARFFormValue::FC_Reference, // 0x11 DW_FORM_ref1 50 DWARFFormValue::FC_Reference, // 0x12 DW_FORM_ref2 51 DWARFFormValue::FC_Reference, // 0x13 DW_FORM_ref4 52 DWARFFormValue::FC_Reference, // 0x14 DW_FORM_ref8 53 DWARFFormValue::FC_Reference, // 0x15 DW_FORM_ref_udata 54 DWARFFormValue::FC_Indirect, // 0x16 DW_FORM_indirect 55 DWARFFormValue::FC_SectionOffset, // 0x17 DW_FORM_sec_offset 56 DWARFFormValue::FC_Exprloc, // 0x18 DW_FORM_exprloc 57 DWARFFormValue::FC_Flag, // 0x19 DW_FORM_flag_present 58 DWARFFormValue::FC_String, // 0x1a DW_FORM_strx 59 DWARFFormValue::FC_Address, // 0x1b DW_FORM_addrx 60 DWARFFormValue::FC_Reference, // 0x1c DW_FORM_ref_sup4 61 DWARFFormValue::FC_String, // 0x1d DW_FORM_strp_sup 62 DWARFFormValue::FC_Constant, // 0x1e DW_FORM_data16 63 DWARFFormValue::FC_String, // 0x1f DW_FORM_line_strp 64 DWARFFormValue::FC_Reference, // 0x20 DW_FORM_ref_sig8 65 DWARFFormValue::FC_Constant, // 0x21 DW_FORM_implicit_const 66 DWARFFormValue::FC_SectionOffset, // 0x22 DW_FORM_loclistx 67 DWARFFormValue::FC_SectionOffset, // 0x23 DW_FORM_rnglistx 68 DWARFFormValue::FC_Reference, // 0x24 DW_FORM_ref_sup8 69 DWARFFormValue::FC_String, // 0x25 DW_FORM_strx1 70 DWARFFormValue::FC_String, // 0x26 DW_FORM_strx2 71 DWARFFormValue::FC_String, // 0x27 DW_FORM_strx3 72 DWARFFormValue::FC_String, // 0x28 DW_FORM_strx4 73 DWARFFormValue::FC_Address, // 0x29 DW_FORM_addrx1 74 DWARFFormValue::FC_Address, // 0x2a DW_FORM_addrx2 75 DWARFFormValue::FC_Address, // 0x2b DW_FORM_addrx3 76 DWARFFormValue::FC_Address, // 0x2c DW_FORM_addrx4 77 78 }; 79 80 DWARFFormValue DWARFFormValue::createFromSValue(dwarf::Form F, int64_t V) { 81 return DWARFFormValue(F, ValueType(V)); 82 } 83 84 DWARFFormValue DWARFFormValue::createFromUValue(dwarf::Form F, uint64_t V) { 85 return DWARFFormValue(F, ValueType(V)); 86 } 87 88 DWARFFormValue DWARFFormValue::createFromPValue(dwarf::Form F, const char *V) { 89 return DWARFFormValue(F, ValueType(V)); 90 } 91 92 DWARFFormValue DWARFFormValue::createFromBlockValue(dwarf::Form F, 93 ArrayRef<uint8_t> D) { 94 ValueType V; 95 V.uval = D.size(); 96 V.data = D.data(); 97 return DWARFFormValue(F, V); 98 } 99 100 DWARFFormValue DWARFFormValue::createFromUnit(dwarf::Form F, const DWARFUnit *U, 101 uint32_t *OffsetPtr) { 102 DWARFFormValue FormValue(F); 103 FormValue.extractValue(U->getDebugInfoExtractor(), OffsetPtr, 104 U->getFormParams(), U); 105 return FormValue; 106 } 107 108 bool DWARFFormValue::skipValue(dwarf::Form Form, DataExtractor DebugInfoData, 109 uint32_t *OffsetPtr, 110 const dwarf::FormParams Params) { 111 bool Indirect = false; 112 do { 113 switch (Form) { 114 // Blocks of inlined data that have a length field and the data bytes 115 // inlined in the .debug_info. 116 case DW_FORM_exprloc: 117 case DW_FORM_block: { 118 uint64_t size = DebugInfoData.getULEB128(OffsetPtr); 119 *OffsetPtr += size; 120 return true; 121 } 122 case DW_FORM_block1: { 123 uint8_t size = DebugInfoData.getU8(OffsetPtr); 124 *OffsetPtr += size; 125 return true; 126 } 127 case DW_FORM_block2: { 128 uint16_t size = DebugInfoData.getU16(OffsetPtr); 129 *OffsetPtr += size; 130 return true; 131 } 132 case DW_FORM_block4: { 133 uint32_t size = DebugInfoData.getU32(OffsetPtr); 134 *OffsetPtr += size; 135 return true; 136 } 137 138 // Inlined NULL terminated C-strings. 139 case DW_FORM_string: 140 DebugInfoData.getCStr(OffsetPtr); 141 return true; 142 143 case DW_FORM_addr: 144 case DW_FORM_ref_addr: 145 case DW_FORM_flag_present: 146 case DW_FORM_data1: 147 case DW_FORM_data2: 148 case DW_FORM_data4: 149 case DW_FORM_data8: 150 case DW_FORM_data16: 151 case DW_FORM_flag: 152 case DW_FORM_ref1: 153 case DW_FORM_ref2: 154 case DW_FORM_ref4: 155 case DW_FORM_ref8: 156 case DW_FORM_ref_sig8: 157 case DW_FORM_ref_sup4: 158 case DW_FORM_ref_sup8: 159 case DW_FORM_strx1: 160 case DW_FORM_strx2: 161 case DW_FORM_strx4: 162 case DW_FORM_addrx1: 163 case DW_FORM_addrx2: 164 case DW_FORM_addrx4: 165 case DW_FORM_sec_offset: 166 case DW_FORM_strp: 167 case DW_FORM_strp_sup: 168 case DW_FORM_line_strp: 169 case DW_FORM_GNU_ref_alt: 170 case DW_FORM_GNU_strp_alt: 171 if (Optional<uint8_t> FixedSize = 172 dwarf::getFixedFormByteSize(Form, Params)) { 173 *OffsetPtr += *FixedSize; 174 return true; 175 } 176 return false; 177 178 // signed or unsigned LEB 128 values. 179 case DW_FORM_sdata: 180 DebugInfoData.getSLEB128(OffsetPtr); 181 return true; 182 183 case DW_FORM_udata: 184 case DW_FORM_ref_udata: 185 case DW_FORM_strx: 186 case DW_FORM_addrx: 187 case DW_FORM_loclistx: 188 case DW_FORM_rnglistx: 189 case DW_FORM_GNU_addr_index: 190 case DW_FORM_GNU_str_index: 191 DebugInfoData.getULEB128(OffsetPtr); 192 return true; 193 194 case DW_FORM_indirect: 195 Indirect = true; 196 Form = static_cast<dwarf::Form>(DebugInfoData.getULEB128(OffsetPtr)); 197 break; 198 199 default: 200 return false; 201 } 202 } while (Indirect); 203 return true; 204 } 205 206 bool DWARFFormValue::isFormClass(DWARFFormValue::FormClass FC) const { 207 // First, check DWARF5 form classes. 208 if (Form < makeArrayRef(DWARF5FormClasses).size() && 209 DWARF5FormClasses[Form] == FC) 210 return true; 211 // Check more forms from extensions and proposals. 212 switch (Form) { 213 case DW_FORM_GNU_ref_alt: 214 return (FC == FC_Reference); 215 case DW_FORM_GNU_addr_index: 216 return (FC == FC_Address); 217 case DW_FORM_GNU_str_index: 218 case DW_FORM_GNU_strp_alt: 219 return (FC == FC_String); 220 default: 221 break; 222 } 223 224 if (FC == FC_SectionOffset) { 225 if (Form == DW_FORM_strp || Form == DW_FORM_line_strp) 226 return true; 227 // In DWARF3 DW_FORM_data4 and DW_FORM_data8 served also as a section 228 // offset. If we don't have a DWARFUnit, default to the old behavior. 229 if (Form == DW_FORM_data4 || Form == DW_FORM_data8) 230 return !U || U->getVersion() <= 3; 231 } 232 233 return false; 234 } 235 236 bool DWARFFormValue::extractValue(const DWARFDataExtractor &Data, 237 uint32_t *OffsetPtr, dwarf::FormParams FP, 238 const DWARFContext *Ctx, 239 const DWARFUnit *CU) { 240 if (!Ctx && CU) 241 Ctx = &CU->getContext(); 242 C = Ctx; 243 U = CU; 244 bool Indirect = false; 245 bool IsBlock = false; 246 Value.data = nullptr; 247 // Read the value for the form into value and follow and DW_FORM_indirect 248 // instances we run into 249 do { 250 Indirect = false; 251 switch (Form) { 252 case DW_FORM_addr: 253 case DW_FORM_ref_addr: { 254 uint16_t Size = 255 (Form == DW_FORM_addr) ? FP.AddrSize : FP.getRefAddrByteSize(); 256 Value.uval = Data.getRelocatedValue(Size, OffsetPtr, &Value.SectionIndex); 257 break; 258 } 259 case DW_FORM_exprloc: 260 case DW_FORM_block: 261 Value.uval = Data.getULEB128(OffsetPtr); 262 IsBlock = true; 263 break; 264 case DW_FORM_block1: 265 Value.uval = Data.getU8(OffsetPtr); 266 IsBlock = true; 267 break; 268 case DW_FORM_block2: 269 Value.uval = Data.getU16(OffsetPtr); 270 IsBlock = true; 271 break; 272 case DW_FORM_block4: 273 Value.uval = Data.getU32(OffsetPtr); 274 IsBlock = true; 275 break; 276 case DW_FORM_data1: 277 case DW_FORM_ref1: 278 case DW_FORM_flag: 279 case DW_FORM_strx1: 280 case DW_FORM_addrx1: 281 Value.uval = Data.getU8(OffsetPtr); 282 break; 283 case DW_FORM_data2: 284 case DW_FORM_ref2: 285 case DW_FORM_strx2: 286 case DW_FORM_addrx2: 287 Value.uval = Data.getU16(OffsetPtr); 288 break; 289 case DW_FORM_strx3: 290 Value.uval = Data.getU24(OffsetPtr); 291 break; 292 case DW_FORM_data4: 293 case DW_FORM_ref4: 294 case DW_FORM_ref_sup4: 295 case DW_FORM_strx4: 296 case DW_FORM_addrx4: 297 Value.uval = Data.getRelocatedValue(4, OffsetPtr); 298 break; 299 case DW_FORM_data8: 300 case DW_FORM_ref8: 301 case DW_FORM_ref_sup8: 302 Value.uval = Data.getU64(OffsetPtr); 303 break; 304 case DW_FORM_data16: 305 // Treat this like a 16-byte block. 306 Value.uval = 16; 307 IsBlock = true; 308 break; 309 case DW_FORM_sdata: 310 Value.sval = Data.getSLEB128(OffsetPtr); 311 break; 312 case DW_FORM_udata: 313 case DW_FORM_ref_udata: 314 case DW_FORM_rnglistx: 315 Value.uval = Data.getULEB128(OffsetPtr); 316 break; 317 case DW_FORM_string: 318 Value.cstr = Data.getCStr(OffsetPtr); 319 break; 320 case DW_FORM_indirect: 321 Form = static_cast<dwarf::Form>(Data.getULEB128(OffsetPtr)); 322 Indirect = true; 323 break; 324 case DW_FORM_strp: 325 case DW_FORM_sec_offset: 326 case DW_FORM_GNU_ref_alt: 327 case DW_FORM_GNU_strp_alt: 328 case DW_FORM_line_strp: 329 case DW_FORM_strp_sup: { 330 Value.uval = 331 Data.getRelocatedValue(FP.getDwarfOffsetByteSize(), OffsetPtr); 332 break; 333 } 334 case DW_FORM_flag_present: 335 Value.uval = 1; 336 break; 337 case DW_FORM_ref_sig8: 338 Value.uval = Data.getU64(OffsetPtr); 339 break; 340 case DW_FORM_GNU_addr_index: 341 case DW_FORM_GNU_str_index: 342 case DW_FORM_addrx: 343 case DW_FORM_strx: 344 Value.uval = Data.getULEB128(OffsetPtr); 345 break; 346 default: 347 // DWARFFormValue::skipValue() will have caught this and caused all 348 // DWARF DIEs to fail to be parsed, so this code is not be reachable. 349 llvm_unreachable("unsupported form"); 350 } 351 } while (Indirect); 352 353 if (IsBlock) { 354 StringRef Str = Data.getData().substr(*OffsetPtr, Value.uval); 355 Value.data = nullptr; 356 if (!Str.empty()) { 357 Value.data = Str.bytes_begin(); 358 *OffsetPtr += Value.uval; 359 } 360 } 361 362 return true; 363 } 364 365 void DWARFFormValue::dumpSectionedAddress(raw_ostream &OS, 366 DIDumpOptions DumpOpts, 367 object::SectionedAddress SA) const { 368 OS << format("0x%016" PRIx64, SA.Address); 369 dumpAddressSection(U->getContext().getDWARFObj(), OS, DumpOpts, 370 SA.SectionIndex); 371 } 372 373 void DWARFFormValue::dumpAddressSection(const DWARFObject &Obj, raw_ostream &OS, 374 DIDumpOptions DumpOpts, 375 uint64_t SectionIndex) { 376 if (!DumpOpts.Verbose || SectionIndex == -1ULL) 377 return; 378 ArrayRef<SectionName> SectionNames = Obj.getSectionNames(); 379 const auto &SecRef = SectionNames[SectionIndex]; 380 381 OS << " \"" << SecRef.Name << '\"'; 382 383 // Print section index if name is not unique. 384 if (!SecRef.IsNameUnique) 385 OS << format(" [%" PRIu64 "]", SectionIndex); 386 } 387 388 void DWARFFormValue::dump(raw_ostream &OS, DIDumpOptions DumpOpts) const { 389 uint64_t UValue = Value.uval; 390 bool CURelativeOffset = false; 391 raw_ostream &AddrOS = DumpOpts.ShowAddresses 392 ? WithColor(OS, HighlightColor::Address).get() 393 : nulls(); 394 switch (Form) { 395 case DW_FORM_addr: 396 dumpSectionedAddress(AddrOS, DumpOpts, {Value.uval, Value.SectionIndex}); 397 break; 398 case DW_FORM_addrx: 399 case DW_FORM_addrx1: 400 case DW_FORM_addrx2: 401 case DW_FORM_addrx3: 402 case DW_FORM_addrx4: 403 case DW_FORM_GNU_addr_index: { 404 Optional<object::SectionedAddress> A = U->getAddrOffsetSectionItem(UValue); 405 if (!A || DumpOpts.Verbose) 406 AddrOS << format("indexed (%8.8x) address = ", (uint32_t)UValue); 407 if (U == nullptr) 408 OS << "<invalid dwarf unit>"; 409 else if (A) 410 dumpSectionedAddress(AddrOS, DumpOpts, *A); 411 else 412 OS << "<no .debug_addr section>"; 413 break; 414 } 415 case DW_FORM_flag_present: 416 OS << "true"; 417 break; 418 case DW_FORM_flag: 419 case DW_FORM_data1: 420 OS << format("0x%02x", (uint8_t)UValue); 421 break; 422 case DW_FORM_data2: 423 OS << format("0x%04x", (uint16_t)UValue); 424 break; 425 case DW_FORM_data4: 426 OS << format("0x%08x", (uint32_t)UValue); 427 break; 428 case DW_FORM_ref_sig8: 429 AddrOS << format("0x%016" PRIx64, UValue); 430 break; 431 case DW_FORM_data8: 432 OS << format("0x%016" PRIx64, UValue); 433 break; 434 case DW_FORM_data16: 435 OS << format_bytes(ArrayRef<uint8_t>(Value.data, 16), None, 16, 16); 436 break; 437 case DW_FORM_string: 438 OS << '"'; 439 OS.write_escaped(Value.cstr); 440 OS << '"'; 441 break; 442 case DW_FORM_exprloc: 443 case DW_FORM_block: 444 case DW_FORM_block1: 445 case DW_FORM_block2: 446 case DW_FORM_block4: 447 if (UValue > 0) { 448 switch (Form) { 449 case DW_FORM_exprloc: 450 case DW_FORM_block: 451 AddrOS << format("<0x%" PRIx64 "> ", UValue); 452 break; 453 case DW_FORM_block1: 454 AddrOS << format("<0x%2.2x> ", (uint8_t)UValue); 455 break; 456 case DW_FORM_block2: 457 AddrOS << format("<0x%4.4x> ", (uint16_t)UValue); 458 break; 459 case DW_FORM_block4: 460 AddrOS << format("<0x%8.8x> ", (uint32_t)UValue); 461 break; 462 default: 463 break; 464 } 465 466 const uint8_t *DataPtr = Value.data; 467 if (DataPtr) { 468 // UValue contains size of block 469 const uint8_t *EndDataPtr = DataPtr + UValue; 470 while (DataPtr < EndDataPtr) { 471 AddrOS << format("%2.2x ", *DataPtr); 472 ++DataPtr; 473 } 474 } else 475 OS << "NULL"; 476 } 477 break; 478 479 case DW_FORM_sdata: 480 OS << Value.sval; 481 break; 482 case DW_FORM_udata: 483 OS << Value.uval; 484 break; 485 case DW_FORM_strp: 486 if (DumpOpts.Verbose) 487 OS << format(" .debug_str[0x%8.8x] = ", (uint32_t)UValue); 488 dumpString(OS); 489 break; 490 case DW_FORM_line_strp: 491 if (DumpOpts.Verbose) 492 OS << format(" .debug_line_str[0x%8.8x] = ", (uint32_t)UValue); 493 dumpString(OS); 494 break; 495 case DW_FORM_strx: 496 case DW_FORM_strx1: 497 case DW_FORM_strx2: 498 case DW_FORM_strx3: 499 case DW_FORM_strx4: 500 case DW_FORM_GNU_str_index: 501 if (DumpOpts.Verbose) 502 OS << format("indexed (%8.8x) string = ", (uint32_t)UValue); 503 dumpString(OS); 504 break; 505 case DW_FORM_GNU_strp_alt: 506 if (DumpOpts.Verbose) 507 OS << format("alt indirect string, offset: 0x%" PRIx64 "", UValue); 508 dumpString(OS); 509 break; 510 case DW_FORM_ref_addr: 511 AddrOS << format("0x%016" PRIx64, UValue); 512 break; 513 case DW_FORM_ref1: 514 CURelativeOffset = true; 515 if (DumpOpts.Verbose) 516 AddrOS << format("cu + 0x%2.2x", (uint8_t)UValue); 517 break; 518 case DW_FORM_ref2: 519 CURelativeOffset = true; 520 if (DumpOpts.Verbose) 521 AddrOS << format("cu + 0x%4.4x", (uint16_t)UValue); 522 break; 523 case DW_FORM_ref4: 524 CURelativeOffset = true; 525 if (DumpOpts.Verbose) 526 AddrOS << format("cu + 0x%4.4x", (uint32_t)UValue); 527 break; 528 case DW_FORM_ref8: 529 CURelativeOffset = true; 530 if (DumpOpts.Verbose) 531 AddrOS << format("cu + 0x%8.8" PRIx64, UValue); 532 break; 533 case DW_FORM_ref_udata: 534 CURelativeOffset = true; 535 if (DumpOpts.Verbose) 536 AddrOS << format("cu + 0x%" PRIx64, UValue); 537 break; 538 case DW_FORM_GNU_ref_alt: 539 AddrOS << format("<alt 0x%" PRIx64 ">", UValue); 540 break; 541 542 // All DW_FORM_indirect attributes should be resolved prior to calling 543 // this function 544 case DW_FORM_indirect: 545 OS << "DW_FORM_indirect"; 546 break; 547 548 case DW_FORM_rnglistx: 549 OS << format("indexed (0x%x) rangelist = ", (uint32_t)UValue); 550 break; 551 552 // Should be formatted to 64-bit for DWARF64. 553 case DW_FORM_sec_offset: 554 AddrOS << format("0x%08x", (uint32_t)UValue); 555 break; 556 557 default: 558 OS << format("DW_FORM(0x%4.4x)", Form); 559 break; 560 } 561 562 if (CURelativeOffset) { 563 if (DumpOpts.Verbose) 564 OS << " => {"; 565 if (DumpOpts.ShowAddresses) 566 WithColor(OS, HighlightColor::Address).get() 567 << format("0x%8.8" PRIx64, UValue + (U ? U->getOffset() : 0)); 568 if (DumpOpts.Verbose) 569 OS << "}"; 570 } 571 } 572 573 void DWARFFormValue::dumpString(raw_ostream &OS) const { 574 Optional<const char *> DbgStr = getAsCString(); 575 if (DbgStr.hasValue()) { 576 auto COS = WithColor(OS, HighlightColor::String); 577 COS.get() << '"'; 578 COS.get().write_escaped(DbgStr.getValue()); 579 COS.get() << '"'; 580 } 581 } 582 583 Optional<const char *> DWARFFormValue::getAsCString() const { 584 if (!isFormClass(FC_String)) 585 return None; 586 if (Form == DW_FORM_string) 587 return Value.cstr; 588 // FIXME: Add support for DW_FORM_GNU_strp_alt 589 if (Form == DW_FORM_GNU_strp_alt || C == nullptr) 590 return None; 591 uint32_t Offset = Value.uval; 592 if (Form == DW_FORM_line_strp) { 593 // .debug_line_str is tracked in the Context. 594 if (const char *Str = C->getLineStringExtractor().getCStr(&Offset)) 595 return Str; 596 return None; 597 } 598 if (Form == DW_FORM_GNU_str_index || Form == DW_FORM_strx || 599 Form == DW_FORM_strx1 || Form == DW_FORM_strx2 || Form == DW_FORM_strx3 || 600 Form == DW_FORM_strx4) { 601 if (!U) 602 return None; 603 Optional<uint64_t> StrOffset = U->getStringOffsetSectionItem(Offset); 604 if (!StrOffset) 605 return None; 606 Offset = *StrOffset; 607 } 608 // Prefer the Unit's string extractor, because for .dwo it will point to 609 // .debug_str.dwo, while the Context's extractor always uses .debug_str. 610 if (U) { 611 if (const char *Str = U->getStringExtractor().getCStr(&Offset)) 612 return Str; 613 return None; 614 } 615 if (const char *Str = C->getStringExtractor().getCStr(&Offset)) 616 return Str; 617 return None; 618 } 619 620 Optional<uint64_t> DWARFFormValue::getAsAddress() const { 621 if (auto SA = getAsSectionedAddress()) 622 return SA->Address; 623 return None; 624 } 625 Optional<object::SectionedAddress> 626 DWARFFormValue::getAsSectionedAddress() const { 627 if (!isFormClass(FC_Address)) 628 return None; 629 if (Form == DW_FORM_GNU_addr_index || Form == DW_FORM_addrx) { 630 uint32_t Index = Value.uval; 631 if (!U) 632 return None; 633 Optional<object::SectionedAddress> SA = U->getAddrOffsetSectionItem(Index); 634 if (!SA) 635 return None; 636 return SA; 637 } 638 return {{Value.uval, Value.SectionIndex}}; 639 } 640 641 Optional<uint64_t> DWARFFormValue::getAsReference() const { 642 if (!isFormClass(FC_Reference)) 643 return None; 644 switch (Form) { 645 case DW_FORM_ref1: 646 case DW_FORM_ref2: 647 case DW_FORM_ref4: 648 case DW_FORM_ref8: 649 case DW_FORM_ref_udata: 650 if (!U) 651 return None; 652 return Value.uval + U->getOffset(); 653 case DW_FORM_ref_addr: 654 case DW_FORM_ref_sig8: 655 case DW_FORM_GNU_ref_alt: 656 return Value.uval; 657 default: 658 return None; 659 } 660 } 661 662 Optional<uint64_t> DWARFFormValue::getAsSectionOffset() const { 663 if (!isFormClass(FC_SectionOffset)) 664 return None; 665 return Value.uval; 666 } 667 668 Optional<uint64_t> DWARFFormValue::getAsUnsignedConstant() const { 669 if ((!isFormClass(FC_Constant) && !isFormClass(FC_Flag)) || 670 Form == DW_FORM_sdata) 671 return None; 672 return Value.uval; 673 } 674 675 Optional<int64_t> DWARFFormValue::getAsSignedConstant() const { 676 if ((!isFormClass(FC_Constant) && !isFormClass(FC_Flag)) || 677 (Form == DW_FORM_udata && 678 uint64_t(std::numeric_limits<int64_t>::max()) < Value.uval)) 679 return None; 680 switch (Form) { 681 case DW_FORM_data4: 682 return int32_t(Value.uval); 683 case DW_FORM_data2: 684 return int16_t(Value.uval); 685 case DW_FORM_data1: 686 return int8_t(Value.uval); 687 case DW_FORM_sdata: 688 case DW_FORM_data8: 689 default: 690 return Value.sval; 691 } 692 } 693 694 Optional<ArrayRef<uint8_t>> DWARFFormValue::getAsBlock() const { 695 if (!isFormClass(FC_Block) && !isFormClass(FC_Exprloc) && 696 Form != DW_FORM_data16) 697 return None; 698 return makeArrayRef(Value.data, Value.uval); 699 } 700 701 Optional<uint64_t> DWARFFormValue::getAsCStringOffset() const { 702 if (!isFormClass(FC_String) && Form == DW_FORM_string) 703 return None; 704 return Value.uval; 705 } 706 707 Optional<uint64_t> DWARFFormValue::getAsReferenceUVal() const { 708 if (!isFormClass(FC_Reference)) 709 return None; 710 return Value.uval; 711 } 712