1 //===-- ObjectFileELF.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 "ObjectFileELF.h" 10 11 #include <algorithm> 12 #include <cassert> 13 #include <optional> 14 #include <unordered_map> 15 16 #include "lldb/Core/Module.h" 17 #include "lldb/Core/ModuleSpec.h" 18 #include "lldb/Core/PluginManager.h" 19 #include "lldb/Core/Progress.h" 20 #include "lldb/Core/Section.h" 21 #include "lldb/Host/FileSystem.h" 22 #include "lldb/Host/LZMA.h" 23 #include "lldb/Symbol/DWARFCallFrameInfo.h" 24 #include "lldb/Symbol/SymbolContext.h" 25 #include "lldb/Target/SectionLoadList.h" 26 #include "lldb/Target/Target.h" 27 #include "lldb/Utility/ArchSpec.h" 28 #include "lldb/Utility/DataBufferHeap.h" 29 #include "lldb/Utility/FileSpecList.h" 30 #include "lldb/Utility/LLDBLog.h" 31 #include "lldb/Utility/Log.h" 32 #include "lldb/Utility/RangeMap.h" 33 #include "lldb/Utility/Status.h" 34 #include "lldb/Utility/Stream.h" 35 #include "lldb/Utility/Timer.h" 36 #include "llvm/ADT/IntervalMap.h" 37 #include "llvm/ADT/PointerUnion.h" 38 #include "llvm/ADT/StringRef.h" 39 #include "llvm/BinaryFormat/ELF.h" 40 #include "llvm/Object/Decompressor.h" 41 #include "llvm/Support/ARMBuildAttributes.h" 42 #include "llvm/Support/CRC.h" 43 #include "llvm/Support/FormatVariadic.h" 44 #include "llvm/Support/MathExtras.h" 45 #include "llvm/Support/MemoryBuffer.h" 46 #include "llvm/Support/MipsABIFlags.h" 47 48 #define CASE_AND_STREAM(s, def, width) \ 49 case def: \ 50 s->Printf("%-*s", width, #def); \ 51 break; 52 53 using namespace lldb; 54 using namespace lldb_private; 55 using namespace elf; 56 using namespace llvm::ELF; 57 58 LLDB_PLUGIN_DEFINE(ObjectFileELF) 59 60 // ELF note owner definitions 61 static const char *const LLDB_NT_OWNER_FREEBSD = "FreeBSD"; 62 static const char *const LLDB_NT_OWNER_GNU = "GNU"; 63 static const char *const LLDB_NT_OWNER_NETBSD = "NetBSD"; 64 static const char *const LLDB_NT_OWNER_NETBSDCORE = "NetBSD-CORE"; 65 static const char *const LLDB_NT_OWNER_OPENBSD = "OpenBSD"; 66 static const char *const LLDB_NT_OWNER_ANDROID = "Android"; 67 static const char *const LLDB_NT_OWNER_CORE = "CORE"; 68 static const char *const LLDB_NT_OWNER_LINUX = "LINUX"; 69 70 // ELF note type definitions 71 static const elf_word LLDB_NT_FREEBSD_ABI_TAG = 0x01; 72 static const elf_word LLDB_NT_FREEBSD_ABI_SIZE = 4; 73 74 static const elf_word LLDB_NT_GNU_ABI_TAG = 0x01; 75 static const elf_word LLDB_NT_GNU_ABI_SIZE = 16; 76 77 static const elf_word LLDB_NT_GNU_BUILD_ID_TAG = 0x03; 78 79 static const elf_word LLDB_NT_NETBSD_IDENT_TAG = 1; 80 static const elf_word LLDB_NT_NETBSD_IDENT_DESCSZ = 4; 81 static const elf_word LLDB_NT_NETBSD_IDENT_NAMESZ = 7; 82 static const elf_word LLDB_NT_NETBSD_PROCINFO = 1; 83 84 // GNU ABI note OS constants 85 static const elf_word LLDB_NT_GNU_ABI_OS_LINUX = 0x00; 86 static const elf_word LLDB_NT_GNU_ABI_OS_HURD = 0x01; 87 static const elf_word LLDB_NT_GNU_ABI_OS_SOLARIS = 0x02; 88 89 namespace { 90 91 //===----------------------------------------------------------------------===// 92 /// \class ELFRelocation 93 /// Generic wrapper for ELFRel and ELFRela. 94 /// 95 /// This helper class allows us to parse both ELFRel and ELFRela relocation 96 /// entries in a generic manner. 97 class ELFRelocation { 98 public: 99 /// Constructs an ELFRelocation entry with a personality as given by @p 100 /// type. 101 /// 102 /// \param type Either DT_REL or DT_RELA. Any other value is invalid. 103 ELFRelocation(unsigned type); 104 105 ~ELFRelocation(); 106 107 bool Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset); 108 109 static unsigned RelocType32(const ELFRelocation &rel); 110 111 static unsigned RelocType64(const ELFRelocation &rel); 112 113 static unsigned RelocSymbol32(const ELFRelocation &rel); 114 115 static unsigned RelocSymbol64(const ELFRelocation &rel); 116 117 static elf_addr RelocOffset32(const ELFRelocation &rel); 118 119 static elf_addr RelocOffset64(const ELFRelocation &rel); 120 121 static elf_sxword RelocAddend32(const ELFRelocation &rel); 122 123 static elf_sxword RelocAddend64(const ELFRelocation &rel); 124 125 bool IsRela() { return (reloc.is<ELFRela *>()); } 126 127 private: 128 typedef llvm::PointerUnion<ELFRel *, ELFRela *> RelocUnion; 129 130 RelocUnion reloc; 131 }; 132 } // end anonymous namespace 133 134 ELFRelocation::ELFRelocation(unsigned type) { 135 if (type == DT_REL || type == SHT_REL) 136 reloc = new ELFRel(); 137 else if (type == DT_RELA || type == SHT_RELA) 138 reloc = new ELFRela(); 139 else { 140 assert(false && "unexpected relocation type"); 141 reloc = static_cast<ELFRel *>(nullptr); 142 } 143 } 144 145 ELFRelocation::~ELFRelocation() { 146 if (reloc.is<ELFRel *>()) 147 delete reloc.get<ELFRel *>(); 148 else 149 delete reloc.get<ELFRela *>(); 150 } 151 152 bool ELFRelocation::Parse(const lldb_private::DataExtractor &data, 153 lldb::offset_t *offset) { 154 if (reloc.is<ELFRel *>()) 155 return reloc.get<ELFRel *>()->Parse(data, offset); 156 else 157 return reloc.get<ELFRela *>()->Parse(data, offset); 158 } 159 160 unsigned ELFRelocation::RelocType32(const ELFRelocation &rel) { 161 if (rel.reloc.is<ELFRel *>()) 162 return ELFRel::RelocType32(*rel.reloc.get<ELFRel *>()); 163 else 164 return ELFRela::RelocType32(*rel.reloc.get<ELFRela *>()); 165 } 166 167 unsigned ELFRelocation::RelocType64(const ELFRelocation &rel) { 168 if (rel.reloc.is<ELFRel *>()) 169 return ELFRel::RelocType64(*rel.reloc.get<ELFRel *>()); 170 else 171 return ELFRela::RelocType64(*rel.reloc.get<ELFRela *>()); 172 } 173 174 unsigned ELFRelocation::RelocSymbol32(const ELFRelocation &rel) { 175 if (rel.reloc.is<ELFRel *>()) 176 return ELFRel::RelocSymbol32(*rel.reloc.get<ELFRel *>()); 177 else 178 return ELFRela::RelocSymbol32(*rel.reloc.get<ELFRela *>()); 179 } 180 181 unsigned ELFRelocation::RelocSymbol64(const ELFRelocation &rel) { 182 if (rel.reloc.is<ELFRel *>()) 183 return ELFRel::RelocSymbol64(*rel.reloc.get<ELFRel *>()); 184 else 185 return ELFRela::RelocSymbol64(*rel.reloc.get<ELFRela *>()); 186 } 187 188 elf_addr ELFRelocation::RelocOffset32(const ELFRelocation &rel) { 189 if (rel.reloc.is<ELFRel *>()) 190 return rel.reloc.get<ELFRel *>()->r_offset; 191 else 192 return rel.reloc.get<ELFRela *>()->r_offset; 193 } 194 195 elf_addr ELFRelocation::RelocOffset64(const ELFRelocation &rel) { 196 if (rel.reloc.is<ELFRel *>()) 197 return rel.reloc.get<ELFRel *>()->r_offset; 198 else 199 return rel.reloc.get<ELFRela *>()->r_offset; 200 } 201 202 elf_sxword ELFRelocation::RelocAddend32(const ELFRelocation &rel) { 203 if (rel.reloc.is<ELFRel *>()) 204 return 0; 205 else 206 return rel.reloc.get<ELFRela *>()->r_addend; 207 } 208 209 elf_sxword ELFRelocation::RelocAddend64(const ELFRelocation &rel) { 210 if (rel.reloc.is<ELFRel *>()) 211 return 0; 212 else 213 return rel.reloc.get<ELFRela *>()->r_addend; 214 } 215 216 static user_id_t SegmentID(size_t PHdrIndex) { 217 return ~user_id_t(PHdrIndex); 218 } 219 220 bool ELFNote::Parse(const DataExtractor &data, lldb::offset_t *offset) { 221 // Read all fields. 222 if (data.GetU32(offset, &n_namesz, 3) == nullptr) 223 return false; 224 225 // The name field is required to be nul-terminated, and n_namesz includes the 226 // terminating nul in observed implementations (contrary to the ELF-64 spec). 227 // A special case is needed for cores generated by some older Linux versions, 228 // which write a note named "CORE" without a nul terminator and n_namesz = 4. 229 if (n_namesz == 4) { 230 char buf[4]; 231 if (data.ExtractBytes(*offset, 4, data.GetByteOrder(), buf) != 4) 232 return false; 233 if (strncmp(buf, "CORE", 4) == 0) { 234 n_name = "CORE"; 235 *offset += 4; 236 return true; 237 } 238 } 239 240 const char *cstr = data.GetCStr(offset, llvm::alignTo(n_namesz, 4)); 241 if (cstr == nullptr) { 242 Log *log = GetLog(LLDBLog::Symbols); 243 LLDB_LOGF(log, "Failed to parse note name lacking nul terminator"); 244 245 return false; 246 } 247 n_name = cstr; 248 return true; 249 } 250 251 static uint32_t mipsVariantFromElfFlags (const elf::ELFHeader &header) { 252 const uint32_t mips_arch = header.e_flags & llvm::ELF::EF_MIPS_ARCH; 253 uint32_t endian = header.e_ident[EI_DATA]; 254 uint32_t arch_variant = ArchSpec::eMIPSSubType_unknown; 255 uint32_t fileclass = header.e_ident[EI_CLASS]; 256 257 // If there aren't any elf flags available (e.g core elf file) then return 258 // default 259 // 32 or 64 bit arch (without any architecture revision) based on object file's class. 260 if (header.e_type == ET_CORE) { 261 switch (fileclass) { 262 case llvm::ELF::ELFCLASS32: 263 return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips32el 264 : ArchSpec::eMIPSSubType_mips32; 265 case llvm::ELF::ELFCLASS64: 266 return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips64el 267 : ArchSpec::eMIPSSubType_mips64; 268 default: 269 return arch_variant; 270 } 271 } 272 273 switch (mips_arch) { 274 case llvm::ELF::EF_MIPS_ARCH_1: 275 case llvm::ELF::EF_MIPS_ARCH_2: 276 case llvm::ELF::EF_MIPS_ARCH_32: 277 return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips32el 278 : ArchSpec::eMIPSSubType_mips32; 279 case llvm::ELF::EF_MIPS_ARCH_32R2: 280 return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips32r2el 281 : ArchSpec::eMIPSSubType_mips32r2; 282 case llvm::ELF::EF_MIPS_ARCH_32R6: 283 return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips32r6el 284 : ArchSpec::eMIPSSubType_mips32r6; 285 case llvm::ELF::EF_MIPS_ARCH_3: 286 case llvm::ELF::EF_MIPS_ARCH_4: 287 case llvm::ELF::EF_MIPS_ARCH_5: 288 case llvm::ELF::EF_MIPS_ARCH_64: 289 return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips64el 290 : ArchSpec::eMIPSSubType_mips64; 291 case llvm::ELF::EF_MIPS_ARCH_64R2: 292 return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips64r2el 293 : ArchSpec::eMIPSSubType_mips64r2; 294 case llvm::ELF::EF_MIPS_ARCH_64R6: 295 return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips64r6el 296 : ArchSpec::eMIPSSubType_mips64r6; 297 default: 298 break; 299 } 300 301 return arch_variant; 302 } 303 304 static uint32_t riscvVariantFromElfFlags(const elf::ELFHeader &header) { 305 uint32_t fileclass = header.e_ident[EI_CLASS]; 306 switch (fileclass) { 307 case llvm::ELF::ELFCLASS32: 308 return ArchSpec::eRISCVSubType_riscv32; 309 case llvm::ELF::ELFCLASS64: 310 return ArchSpec::eRISCVSubType_riscv64; 311 default: 312 return ArchSpec::eRISCVSubType_unknown; 313 } 314 } 315 316 static uint32_t ppc64VariantFromElfFlags(const elf::ELFHeader &header) { 317 uint32_t endian = header.e_ident[EI_DATA]; 318 if (endian == ELFDATA2LSB) 319 return ArchSpec::eCore_ppc64le_generic; 320 else 321 return ArchSpec::eCore_ppc64_generic; 322 } 323 324 static uint32_t loongarchVariantFromElfFlags(const elf::ELFHeader &header) { 325 uint32_t fileclass = header.e_ident[EI_CLASS]; 326 switch (fileclass) { 327 case llvm::ELF::ELFCLASS32: 328 return ArchSpec::eLoongArchSubType_loongarch32; 329 case llvm::ELF::ELFCLASS64: 330 return ArchSpec::eLoongArchSubType_loongarch64; 331 default: 332 return ArchSpec::eLoongArchSubType_unknown; 333 } 334 } 335 336 static uint32_t subTypeFromElfHeader(const elf::ELFHeader &header) { 337 if (header.e_machine == llvm::ELF::EM_MIPS) 338 return mipsVariantFromElfFlags(header); 339 else if (header.e_machine == llvm::ELF::EM_PPC64) 340 return ppc64VariantFromElfFlags(header); 341 else if (header.e_machine == llvm::ELF::EM_RISCV) 342 return riscvVariantFromElfFlags(header); 343 else if (header.e_machine == llvm::ELF::EM_LOONGARCH) 344 return loongarchVariantFromElfFlags(header); 345 346 return LLDB_INVALID_CPUTYPE; 347 } 348 349 char ObjectFileELF::ID; 350 351 // Arbitrary constant used as UUID prefix for core files. 352 const uint32_t ObjectFileELF::g_core_uuid_magic(0xE210C); 353 354 // Static methods. 355 void ObjectFileELF::Initialize() { 356 PluginManager::RegisterPlugin(GetPluginNameStatic(), 357 GetPluginDescriptionStatic(), CreateInstance, 358 CreateMemoryInstance, GetModuleSpecifications); 359 } 360 361 void ObjectFileELF::Terminate() { 362 PluginManager::UnregisterPlugin(CreateInstance); 363 } 364 365 ObjectFile *ObjectFileELF::CreateInstance(const lldb::ModuleSP &module_sp, 366 DataBufferSP data_sp, 367 lldb::offset_t data_offset, 368 const lldb_private::FileSpec *file, 369 lldb::offset_t file_offset, 370 lldb::offset_t length) { 371 bool mapped_writable = false; 372 if (!data_sp) { 373 data_sp = MapFileDataWritable(*file, length, file_offset); 374 if (!data_sp) 375 return nullptr; 376 data_offset = 0; 377 mapped_writable = true; 378 } 379 380 assert(data_sp); 381 382 if (data_sp->GetByteSize() <= (llvm::ELF::EI_NIDENT + data_offset)) 383 return nullptr; 384 385 const uint8_t *magic = data_sp->GetBytes() + data_offset; 386 if (!ELFHeader::MagicBytesMatch(magic)) 387 return nullptr; 388 389 // Update the data to contain the entire file if it doesn't already 390 if (data_sp->GetByteSize() < length) { 391 data_sp = MapFileDataWritable(*file, length, file_offset); 392 if (!data_sp) 393 return nullptr; 394 data_offset = 0; 395 mapped_writable = true; 396 magic = data_sp->GetBytes(); 397 } 398 399 // If we didn't map the data as writable take ownership of the buffer. 400 if (!mapped_writable) { 401 data_sp = std::make_shared<DataBufferHeap>(data_sp->GetBytes(), 402 data_sp->GetByteSize()); 403 data_offset = 0; 404 magic = data_sp->GetBytes(); 405 } 406 407 unsigned address_size = ELFHeader::AddressSizeInBytes(magic); 408 if (address_size == 4 || address_size == 8) { 409 std::unique_ptr<ObjectFileELF> objfile_up(new ObjectFileELF( 410 module_sp, data_sp, data_offset, file, file_offset, length)); 411 ArchSpec spec = objfile_up->GetArchitecture(); 412 if (spec && objfile_up->SetModulesArchitecture(spec)) 413 return objfile_up.release(); 414 } 415 416 return nullptr; 417 } 418 419 ObjectFile *ObjectFileELF::CreateMemoryInstance( 420 const lldb::ModuleSP &module_sp, WritableDataBufferSP data_sp, 421 const lldb::ProcessSP &process_sp, lldb::addr_t header_addr) { 422 if (!data_sp || data_sp->GetByteSize() < (llvm::ELF::EI_NIDENT)) 423 return nullptr; 424 const uint8_t *magic = data_sp->GetBytes(); 425 if (!ELFHeader::MagicBytesMatch(magic)) 426 return nullptr; 427 // Read the ELF header first so we can figure out how many bytes we need 428 // to read to get as least the ELF header + program headers. 429 DataExtractor data; 430 data.SetData(data_sp); 431 elf::ELFHeader hdr; 432 lldb::offset_t offset = 0; 433 if (!hdr.Parse(data, &offset)) 434 return nullptr; 435 436 // Make sure the address size is set correctly in the ELF header. 437 if (!hdr.Is32Bit() && !hdr.Is64Bit()) 438 return nullptr; 439 // Figure out where the program headers end and read enough bytes to get the 440 // program headers in their entirety. 441 lldb::offset_t end_phdrs = hdr.e_phoff + (hdr.e_phentsize * hdr.e_phnum); 442 if (end_phdrs > data_sp->GetByteSize()) 443 data_sp = ReadMemory(process_sp, header_addr, end_phdrs); 444 445 std::unique_ptr<ObjectFileELF> objfile_up( 446 new ObjectFileELF(module_sp, data_sp, process_sp, header_addr)); 447 ArchSpec spec = objfile_up->GetArchitecture(); 448 if (spec && objfile_up->SetModulesArchitecture(spec)) 449 return objfile_up.release(); 450 451 return nullptr; 452 } 453 454 bool ObjectFileELF::MagicBytesMatch(DataBufferSP &data_sp, 455 lldb::addr_t data_offset, 456 lldb::addr_t data_length) { 457 if (data_sp && 458 data_sp->GetByteSize() > (llvm::ELF::EI_NIDENT + data_offset)) { 459 const uint8_t *magic = data_sp->GetBytes() + data_offset; 460 return ELFHeader::MagicBytesMatch(magic); 461 } 462 return false; 463 } 464 465 static uint32_t calc_crc32(uint32_t init, const DataExtractor &data) { 466 return llvm::crc32(init, 467 llvm::ArrayRef(data.GetDataStart(), data.GetByteSize())); 468 } 469 470 uint32_t ObjectFileELF::CalculateELFNotesSegmentsCRC32( 471 const ProgramHeaderColl &program_headers, DataExtractor &object_data) { 472 473 uint32_t core_notes_crc = 0; 474 475 for (const ELFProgramHeader &H : program_headers) { 476 if (H.p_type == llvm::ELF::PT_NOTE) { 477 const elf_off ph_offset = H.p_offset; 478 const size_t ph_size = H.p_filesz; 479 480 DataExtractor segment_data; 481 if (segment_data.SetData(object_data, ph_offset, ph_size) != ph_size) { 482 // The ELF program header contained incorrect data, probably corefile 483 // is incomplete or corrupted. 484 break; 485 } 486 487 core_notes_crc = calc_crc32(core_notes_crc, segment_data); 488 } 489 } 490 491 return core_notes_crc; 492 } 493 494 static const char *OSABIAsCString(unsigned char osabi_byte) { 495 #define _MAKE_OSABI_CASE(x) \ 496 case x: \ 497 return #x 498 switch (osabi_byte) { 499 _MAKE_OSABI_CASE(ELFOSABI_NONE); 500 _MAKE_OSABI_CASE(ELFOSABI_HPUX); 501 _MAKE_OSABI_CASE(ELFOSABI_NETBSD); 502 _MAKE_OSABI_CASE(ELFOSABI_GNU); 503 _MAKE_OSABI_CASE(ELFOSABI_HURD); 504 _MAKE_OSABI_CASE(ELFOSABI_SOLARIS); 505 _MAKE_OSABI_CASE(ELFOSABI_AIX); 506 _MAKE_OSABI_CASE(ELFOSABI_IRIX); 507 _MAKE_OSABI_CASE(ELFOSABI_FREEBSD); 508 _MAKE_OSABI_CASE(ELFOSABI_TRU64); 509 _MAKE_OSABI_CASE(ELFOSABI_MODESTO); 510 _MAKE_OSABI_CASE(ELFOSABI_OPENBSD); 511 _MAKE_OSABI_CASE(ELFOSABI_OPENVMS); 512 _MAKE_OSABI_CASE(ELFOSABI_NSK); 513 _MAKE_OSABI_CASE(ELFOSABI_AROS); 514 _MAKE_OSABI_CASE(ELFOSABI_FENIXOS); 515 _MAKE_OSABI_CASE(ELFOSABI_C6000_ELFABI); 516 _MAKE_OSABI_CASE(ELFOSABI_C6000_LINUX); 517 _MAKE_OSABI_CASE(ELFOSABI_ARM); 518 _MAKE_OSABI_CASE(ELFOSABI_STANDALONE); 519 default: 520 return "<unknown-osabi>"; 521 } 522 #undef _MAKE_OSABI_CASE 523 } 524 525 // 526 // WARNING : This function is being deprecated 527 // It's functionality has moved to ArchSpec::SetArchitecture This function is 528 // only being kept to validate the move. 529 // 530 // TODO : Remove this function 531 static bool GetOsFromOSABI(unsigned char osabi_byte, 532 llvm::Triple::OSType &ostype) { 533 switch (osabi_byte) { 534 case ELFOSABI_AIX: 535 ostype = llvm::Triple::OSType::AIX; 536 break; 537 case ELFOSABI_FREEBSD: 538 ostype = llvm::Triple::OSType::FreeBSD; 539 break; 540 case ELFOSABI_GNU: 541 ostype = llvm::Triple::OSType::Linux; 542 break; 543 case ELFOSABI_NETBSD: 544 ostype = llvm::Triple::OSType::NetBSD; 545 break; 546 case ELFOSABI_OPENBSD: 547 ostype = llvm::Triple::OSType::OpenBSD; 548 break; 549 case ELFOSABI_SOLARIS: 550 ostype = llvm::Triple::OSType::Solaris; 551 break; 552 default: 553 ostype = llvm::Triple::OSType::UnknownOS; 554 } 555 return ostype != llvm::Triple::OSType::UnknownOS; 556 } 557 558 size_t ObjectFileELF::GetModuleSpecifications( 559 const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp, 560 lldb::offset_t data_offset, lldb::offset_t file_offset, 561 lldb::offset_t length, lldb_private::ModuleSpecList &specs) { 562 Log *log = GetLog(LLDBLog::Modules); 563 564 const size_t initial_count = specs.GetSize(); 565 566 if (ObjectFileELF::MagicBytesMatch(data_sp, 0, data_sp->GetByteSize())) { 567 DataExtractor data; 568 data.SetData(data_sp); 569 elf::ELFHeader header; 570 lldb::offset_t header_offset = data_offset; 571 if (header.Parse(data, &header_offset)) { 572 if (data_sp) { 573 ModuleSpec spec(file); 574 // In Android API level 23 and above, bionic dynamic linker is able to 575 // load .so file directly from zip file. In that case, .so file is 576 // page aligned and uncompressed, and this module spec should retain the 577 // .so file offset and file size to pass through the information from 578 // lldb-server to LLDB. For normal file, file_offset should be 0, 579 // length should be the size of the file. 580 spec.SetObjectOffset(file_offset); 581 spec.SetObjectSize(length); 582 583 const uint32_t sub_type = subTypeFromElfHeader(header); 584 spec.GetArchitecture().SetArchitecture( 585 eArchTypeELF, header.e_machine, sub_type, header.e_ident[EI_OSABI]); 586 587 if (spec.GetArchitecture().IsValid()) { 588 llvm::Triple::OSType ostype; 589 llvm::Triple::VendorType vendor; 590 llvm::Triple::OSType spec_ostype = 591 spec.GetArchitecture().GetTriple().getOS(); 592 593 LLDB_LOGF(log, "ObjectFileELF::%s file '%s' module OSABI: %s", 594 __FUNCTION__, file.GetPath().c_str(), 595 OSABIAsCString(header.e_ident[EI_OSABI])); 596 597 // SetArchitecture should have set the vendor to unknown 598 vendor = spec.GetArchitecture().GetTriple().getVendor(); 599 assert(vendor == llvm::Triple::UnknownVendor); 600 UNUSED_IF_ASSERT_DISABLED(vendor); 601 602 // 603 // Validate it is ok to remove GetOsFromOSABI 604 GetOsFromOSABI(header.e_ident[EI_OSABI], ostype); 605 assert(spec_ostype == ostype); 606 if (spec_ostype != llvm::Triple::OSType::UnknownOS) { 607 LLDB_LOGF(log, 608 "ObjectFileELF::%s file '%s' set ELF module OS type " 609 "from ELF header OSABI.", 610 __FUNCTION__, file.GetPath().c_str()); 611 } 612 613 // When ELF file does not contain GNU build ID, the later code will 614 // calculate CRC32 with this data_sp file_offset and length. It is 615 // important for Android zip .so file, which is a slice of a file, 616 // to not access the outside of the file slice range. 617 if (data_sp->GetByteSize() < length) 618 data_sp = MapFileData(file, length, file_offset); 619 if (data_sp) 620 data.SetData(data_sp); 621 // In case there is header extension in the section #0, the header we 622 // parsed above could have sentinel values for e_phnum, e_shnum, and 623 // e_shstrndx. In this case we need to reparse the header with a 624 // bigger data source to get the actual values. 625 if (header.HasHeaderExtension()) { 626 lldb::offset_t header_offset = data_offset; 627 header.Parse(data, &header_offset); 628 } 629 630 uint32_t gnu_debuglink_crc = 0; 631 std::string gnu_debuglink_file; 632 SectionHeaderColl section_headers; 633 lldb_private::UUID &uuid = spec.GetUUID(); 634 635 GetSectionHeaderInfo(section_headers, data, header, uuid, 636 gnu_debuglink_file, gnu_debuglink_crc, 637 spec.GetArchitecture()); 638 639 llvm::Triple &spec_triple = spec.GetArchitecture().GetTriple(); 640 641 LLDB_LOGF(log, 642 "ObjectFileELF::%s file '%s' module set to triple: %s " 643 "(architecture %s)", 644 __FUNCTION__, file.GetPath().c_str(), 645 spec_triple.getTriple().c_str(), 646 spec.GetArchitecture().GetArchitectureName()); 647 648 if (!uuid.IsValid()) { 649 uint32_t core_notes_crc = 0; 650 651 if (!gnu_debuglink_crc) { 652 LLDB_SCOPED_TIMERF( 653 "Calculating module crc32 %s with size %" PRIu64 " KiB", 654 file.GetFilename().AsCString(), 655 (length - file_offset) / 1024); 656 657 // For core files - which usually don't happen to have a 658 // gnu_debuglink, and are pretty bulky - calculating whole 659 // contents crc32 would be too much of luxury. Thus we will need 660 // to fallback to something simpler. 661 if (header.e_type == llvm::ELF::ET_CORE) { 662 ProgramHeaderColl program_headers; 663 GetProgramHeaderInfo(program_headers, data, header); 664 665 core_notes_crc = 666 CalculateELFNotesSegmentsCRC32(program_headers, data); 667 } else { 668 gnu_debuglink_crc = calc_crc32(0, data); 669 } 670 } 671 using u32le = llvm::support::ulittle32_t; 672 if (gnu_debuglink_crc) { 673 // Use 4 bytes of crc from the .gnu_debuglink section. 674 u32le data(gnu_debuglink_crc); 675 uuid = UUID(&data, sizeof(data)); 676 } else if (core_notes_crc) { 677 // Use 8 bytes - first 4 bytes for *magic* prefix, mainly to make 678 // it look different form .gnu_debuglink crc followed by 4 bytes 679 // of note segments crc. 680 u32le data[] = {u32le(g_core_uuid_magic), u32le(core_notes_crc)}; 681 uuid = UUID(data, sizeof(data)); 682 } 683 } 684 685 specs.Append(spec); 686 } 687 } 688 } 689 } 690 691 return specs.GetSize() - initial_count; 692 } 693 694 // ObjectFile protocol 695 696 ObjectFileELF::ObjectFileELF(const lldb::ModuleSP &module_sp, 697 DataBufferSP data_sp, lldb::offset_t data_offset, 698 const FileSpec *file, lldb::offset_t file_offset, 699 lldb::offset_t length) 700 : ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset) { 701 if (file) 702 m_file = *file; 703 } 704 705 ObjectFileELF::ObjectFileELF(const lldb::ModuleSP &module_sp, 706 DataBufferSP header_data_sp, 707 const lldb::ProcessSP &process_sp, 708 addr_t header_addr) 709 : ObjectFile(module_sp, process_sp, header_addr, header_data_sp) {} 710 711 bool ObjectFileELF::IsExecutable() const { 712 return ((m_header.e_type & ET_EXEC) != 0) || (m_header.e_entry != 0); 713 } 714 715 bool ObjectFileELF::SetLoadAddress(Target &target, lldb::addr_t value, 716 bool value_is_offset) { 717 ModuleSP module_sp = GetModule(); 718 if (module_sp) { 719 size_t num_loaded_sections = 0; 720 SectionList *section_list = GetSectionList(); 721 if (section_list) { 722 if (!value_is_offset) { 723 addr_t base = GetBaseAddress().GetFileAddress(); 724 if (base == LLDB_INVALID_ADDRESS) 725 return false; 726 value -= base; 727 } 728 729 const size_t num_sections = section_list->GetSize(); 730 size_t sect_idx = 0; 731 732 for (sect_idx = 0; sect_idx < num_sections; ++sect_idx) { 733 // Iterate through the object file sections to find all of the sections 734 // that have SHF_ALLOC in their flag bits. 735 SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx)); 736 737 // PT_TLS segments can have the same p_vaddr and p_paddr as other 738 // PT_LOAD segments so we shouldn't load them. If we do load them, then 739 // the SectionLoadList will incorrectly fill in the instance variable 740 // SectionLoadList::m_addr_to_sect with the same address as a PT_LOAD 741 // segment and we won't be able to resolve addresses in the PT_LOAD 742 // segment whose p_vaddr entry matches that of the PT_TLS. Any variables 743 // that appear in the PT_TLS segments get resolved by the DWARF 744 // expressions. If this ever changes we will need to fix all object 745 // file plug-ins, but until then, we don't want PT_TLS segments to 746 // remove the entry from SectionLoadList::m_addr_to_sect when we call 747 // SetSectionLoadAddress() below. 748 if (section_sp->IsThreadSpecific()) 749 continue; 750 if (section_sp->Test(SHF_ALLOC) || 751 section_sp->GetType() == eSectionTypeContainer) { 752 lldb::addr_t load_addr = section_sp->GetFileAddress(); 753 // We don't want to update the load address of a section with type 754 // eSectionTypeAbsoluteAddress as they already have the absolute load 755 // address already specified 756 if (section_sp->GetType() != eSectionTypeAbsoluteAddress) 757 load_addr += value; 758 759 // On 32-bit systems the load address have to fit into 4 bytes. The 760 // rest of the bytes are the overflow from the addition. 761 if (GetAddressByteSize() == 4) 762 load_addr &= 0xFFFFFFFF; 763 764 if (target.GetSectionLoadList().SetSectionLoadAddress(section_sp, 765 load_addr)) 766 ++num_loaded_sections; 767 } 768 } 769 return num_loaded_sections > 0; 770 } 771 } 772 return false; 773 } 774 775 ByteOrder ObjectFileELF::GetByteOrder() const { 776 if (m_header.e_ident[EI_DATA] == ELFDATA2MSB) 777 return eByteOrderBig; 778 if (m_header.e_ident[EI_DATA] == ELFDATA2LSB) 779 return eByteOrderLittle; 780 return eByteOrderInvalid; 781 } 782 783 uint32_t ObjectFileELF::GetAddressByteSize() const { 784 return m_data.GetAddressByteSize(); 785 } 786 787 AddressClass ObjectFileELF::GetAddressClass(addr_t file_addr) { 788 Symtab *symtab = GetSymtab(); 789 if (!symtab) 790 return AddressClass::eUnknown; 791 792 // The address class is determined based on the symtab. Ask it from the 793 // object file what contains the symtab information. 794 ObjectFile *symtab_objfile = symtab->GetObjectFile(); 795 if (symtab_objfile != nullptr && symtab_objfile != this) 796 return symtab_objfile->GetAddressClass(file_addr); 797 798 auto res = ObjectFile::GetAddressClass(file_addr); 799 if (res != AddressClass::eCode) 800 return res; 801 802 auto ub = m_address_class_map.upper_bound(file_addr); 803 if (ub == m_address_class_map.begin()) { 804 // No entry in the address class map before the address. Return default 805 // address class for an address in a code section. 806 return AddressClass::eCode; 807 } 808 809 // Move iterator to the address class entry preceding address 810 --ub; 811 812 return ub->second; 813 } 814 815 size_t ObjectFileELF::SectionIndex(const SectionHeaderCollIter &I) { 816 return std::distance(m_section_headers.begin(), I); 817 } 818 819 size_t ObjectFileELF::SectionIndex(const SectionHeaderCollConstIter &I) const { 820 return std::distance(m_section_headers.begin(), I); 821 } 822 823 bool ObjectFileELF::ParseHeader() { 824 lldb::offset_t offset = 0; 825 return m_header.Parse(m_data, &offset); 826 } 827 828 UUID ObjectFileELF::GetUUID() { 829 // Need to parse the section list to get the UUIDs, so make sure that's been 830 // done. 831 if (!ParseSectionHeaders() && GetType() != ObjectFile::eTypeCoreFile) 832 return UUID(); 833 834 if (!m_uuid) { 835 using u32le = llvm::support::ulittle32_t; 836 if (GetType() == ObjectFile::eTypeCoreFile) { 837 uint32_t core_notes_crc = 0; 838 839 if (!ParseProgramHeaders()) 840 return UUID(); 841 842 core_notes_crc = 843 CalculateELFNotesSegmentsCRC32(m_program_headers, m_data); 844 845 if (core_notes_crc) { 846 // Use 8 bytes - first 4 bytes for *magic* prefix, mainly to make it 847 // look different form .gnu_debuglink crc - followed by 4 bytes of note 848 // segments crc. 849 u32le data[] = {u32le(g_core_uuid_magic), u32le(core_notes_crc)}; 850 m_uuid = UUID(data, sizeof(data)); 851 } 852 } else { 853 if (!m_gnu_debuglink_crc) 854 m_gnu_debuglink_crc = calc_crc32(0, m_data); 855 if (m_gnu_debuglink_crc) { 856 // Use 4 bytes of crc from the .gnu_debuglink section. 857 u32le data(m_gnu_debuglink_crc); 858 m_uuid = UUID(&data, sizeof(data)); 859 } 860 } 861 } 862 863 return m_uuid; 864 } 865 866 std::optional<FileSpec> ObjectFileELF::GetDebugLink() { 867 if (m_gnu_debuglink_file.empty()) 868 return std::nullopt; 869 return FileSpec(m_gnu_debuglink_file); 870 } 871 872 uint32_t ObjectFileELF::GetDependentModules(FileSpecList &files) { 873 size_t num_modules = ParseDependentModules(); 874 uint32_t num_specs = 0; 875 876 for (unsigned i = 0; i < num_modules; ++i) { 877 if (files.AppendIfUnique(m_filespec_up->GetFileSpecAtIndex(i))) 878 num_specs++; 879 } 880 881 return num_specs; 882 } 883 884 Address ObjectFileELF::GetImageInfoAddress(Target *target) { 885 if (!ParseDynamicSymbols()) 886 return Address(); 887 888 SectionList *section_list = GetSectionList(); 889 if (!section_list) 890 return Address(); 891 892 for (size_t i = 0; i < m_dynamic_symbols.size(); ++i) { 893 const ELFDynamic &symbol = m_dynamic_symbols[i].symbol; 894 895 if (symbol.d_tag != DT_DEBUG && symbol.d_tag != DT_MIPS_RLD_MAP && 896 symbol.d_tag != DT_MIPS_RLD_MAP_REL) 897 continue; 898 899 // Compute the offset as the number of previous entries plus the size of 900 // d_tag. 901 const addr_t offset = (i * 2 + 1) * GetAddressByteSize(); 902 const addr_t d_file_addr = m_dynamic_base_addr + offset; 903 Address d_addr; 904 if (!d_addr.ResolveAddressUsingFileSections(d_file_addr, GetSectionList())) 905 return Address(); 906 if (symbol.d_tag == DT_DEBUG) 907 return d_addr; 908 909 // MIPS executables uses DT_MIPS_RLD_MAP_REL to support PIE. DT_MIPS_RLD_MAP 910 // exists in non-PIE. 911 if ((symbol.d_tag == DT_MIPS_RLD_MAP || 912 symbol.d_tag == DT_MIPS_RLD_MAP_REL) && 913 target) { 914 const addr_t d_load_addr = d_addr.GetLoadAddress(target); 915 if (d_load_addr == LLDB_INVALID_ADDRESS) 916 return Address(); 917 918 Status error; 919 if (symbol.d_tag == DT_MIPS_RLD_MAP) { 920 // DT_MIPS_RLD_MAP tag stores an absolute address of the debug pointer. 921 Address addr; 922 if (target->ReadPointerFromMemory(d_load_addr, error, addr, true)) 923 return addr; 924 } 925 if (symbol.d_tag == DT_MIPS_RLD_MAP_REL) { 926 // DT_MIPS_RLD_MAP_REL tag stores the offset to the debug pointer, 927 // relative to the address of the tag. 928 uint64_t rel_offset; 929 rel_offset = target->ReadUnsignedIntegerFromMemory( 930 d_load_addr, GetAddressByteSize(), UINT64_MAX, error, true); 931 if (error.Success() && rel_offset != UINT64_MAX) { 932 Address addr; 933 addr_t debug_ptr_address = 934 d_load_addr - GetAddressByteSize() + rel_offset; 935 addr.SetOffset(debug_ptr_address); 936 return addr; 937 } 938 } 939 } 940 } 941 return Address(); 942 } 943 944 lldb_private::Address ObjectFileELF::GetEntryPointAddress() { 945 if (m_entry_point_address.IsValid()) 946 return m_entry_point_address; 947 948 if (!ParseHeader() || !IsExecutable()) 949 return m_entry_point_address; 950 951 SectionList *section_list = GetSectionList(); 952 addr_t offset = m_header.e_entry; 953 954 if (!section_list) 955 m_entry_point_address.SetOffset(offset); 956 else 957 m_entry_point_address.ResolveAddressUsingFileSections(offset, section_list); 958 return m_entry_point_address; 959 } 960 961 Address ObjectFileELF::GetBaseAddress() { 962 if (GetType() == ObjectFile::eTypeObjectFile) { 963 for (SectionHeaderCollIter I = std::next(m_section_headers.begin()); 964 I != m_section_headers.end(); ++I) { 965 const ELFSectionHeaderInfo &header = *I; 966 if (header.sh_flags & SHF_ALLOC) 967 return Address(GetSectionList()->FindSectionByID(SectionIndex(I)), 0); 968 } 969 return LLDB_INVALID_ADDRESS; 970 } 971 972 for (const auto &EnumPHdr : llvm::enumerate(ProgramHeaders())) { 973 const ELFProgramHeader &H = EnumPHdr.value(); 974 if (H.p_type != PT_LOAD) 975 continue; 976 977 return Address( 978 GetSectionList()->FindSectionByID(SegmentID(EnumPHdr.index())), 0); 979 } 980 return LLDB_INVALID_ADDRESS; 981 } 982 983 size_t ObjectFileELF::ParseDependentModules() { 984 if (m_filespec_up) 985 return m_filespec_up->GetSize(); 986 987 m_filespec_up = std::make_unique<FileSpecList>(); 988 989 if (ParseDynamicSymbols()) { 990 for (const auto &entry : m_dynamic_symbols) { 991 if (entry.symbol.d_tag != DT_NEEDED) 992 continue; 993 if (!entry.name.empty()) { 994 FileSpec file_spec(entry.name); 995 FileSystem::Instance().Resolve(file_spec); 996 m_filespec_up->Append(file_spec); 997 } 998 } 999 } 1000 return m_filespec_up->GetSize(); 1001 } 1002 1003 // GetProgramHeaderInfo 1004 size_t ObjectFileELF::GetProgramHeaderInfo(ProgramHeaderColl &program_headers, 1005 DataExtractor &object_data, 1006 const ELFHeader &header) { 1007 // We have already parsed the program headers 1008 if (!program_headers.empty()) 1009 return program_headers.size(); 1010 1011 // If there are no program headers to read we are done. 1012 if (header.e_phnum == 0) 1013 return 0; 1014 1015 program_headers.resize(header.e_phnum); 1016 if (program_headers.size() != header.e_phnum) 1017 return 0; 1018 1019 const size_t ph_size = header.e_phnum * header.e_phentsize; 1020 const elf_off ph_offset = header.e_phoff; 1021 DataExtractor data; 1022 if (data.SetData(object_data, ph_offset, ph_size) != ph_size) 1023 return 0; 1024 1025 uint32_t idx; 1026 lldb::offset_t offset; 1027 for (idx = 0, offset = 0; idx < header.e_phnum; ++idx) { 1028 if (!program_headers[idx].Parse(data, &offset)) 1029 break; 1030 } 1031 1032 if (idx < program_headers.size()) 1033 program_headers.resize(idx); 1034 1035 return program_headers.size(); 1036 } 1037 1038 // ParseProgramHeaders 1039 bool ObjectFileELF::ParseProgramHeaders() { 1040 return GetProgramHeaderInfo(m_program_headers, m_data, m_header) != 0; 1041 } 1042 1043 lldb_private::Status 1044 ObjectFileELF::RefineModuleDetailsFromNote(lldb_private::DataExtractor &data, 1045 lldb_private::ArchSpec &arch_spec, 1046 lldb_private::UUID &uuid) { 1047 Log *log = GetLog(LLDBLog::Modules); 1048 Status error; 1049 1050 lldb::offset_t offset = 0; 1051 1052 while (true) { 1053 // Parse the note header. If this fails, bail out. 1054 const lldb::offset_t note_offset = offset; 1055 ELFNote note = ELFNote(); 1056 if (!note.Parse(data, &offset)) { 1057 // We're done. 1058 return error; 1059 } 1060 1061 LLDB_LOGF(log, "ObjectFileELF::%s parsing note name='%s', type=%" PRIu32, 1062 __FUNCTION__, note.n_name.c_str(), note.n_type); 1063 1064 // Process FreeBSD ELF notes. 1065 if ((note.n_name == LLDB_NT_OWNER_FREEBSD) && 1066 (note.n_type == LLDB_NT_FREEBSD_ABI_TAG) && 1067 (note.n_descsz == LLDB_NT_FREEBSD_ABI_SIZE)) { 1068 // Pull out the min version info. 1069 uint32_t version_info; 1070 if (data.GetU32(&offset, &version_info, 1) == nullptr) { 1071 error = 1072 Status::FromErrorString("failed to read FreeBSD ABI note payload"); 1073 return error; 1074 } 1075 1076 // Convert the version info into a major/minor number. 1077 const uint32_t version_major = version_info / 100000; 1078 const uint32_t version_minor = (version_info / 1000) % 100; 1079 1080 char os_name[32]; 1081 snprintf(os_name, sizeof(os_name), "freebsd%" PRIu32 ".%" PRIu32, 1082 version_major, version_minor); 1083 1084 // Set the elf OS version to FreeBSD. Also clear the vendor. 1085 arch_spec.GetTriple().setOSName(os_name); 1086 arch_spec.GetTriple().setVendor(llvm::Triple::VendorType::UnknownVendor); 1087 1088 LLDB_LOGF(log, 1089 "ObjectFileELF::%s detected FreeBSD %" PRIu32 ".%" PRIu32 1090 ".%" PRIu32, 1091 __FUNCTION__, version_major, version_minor, 1092 static_cast<uint32_t>(version_info % 1000)); 1093 } 1094 // Process GNU ELF notes. 1095 else if (note.n_name == LLDB_NT_OWNER_GNU) { 1096 switch (note.n_type) { 1097 case LLDB_NT_GNU_ABI_TAG: 1098 if (note.n_descsz == LLDB_NT_GNU_ABI_SIZE) { 1099 // Pull out the min OS version supporting the ABI. 1100 uint32_t version_info[4]; 1101 if (data.GetU32(&offset, &version_info[0], note.n_descsz / 4) == 1102 nullptr) { 1103 error = 1104 Status::FromErrorString("failed to read GNU ABI note payload"); 1105 return error; 1106 } 1107 1108 // Set the OS per the OS field. 1109 switch (version_info[0]) { 1110 case LLDB_NT_GNU_ABI_OS_LINUX: 1111 arch_spec.GetTriple().setOS(llvm::Triple::OSType::Linux); 1112 arch_spec.GetTriple().setVendor( 1113 llvm::Triple::VendorType::UnknownVendor); 1114 LLDB_LOGF(log, 1115 "ObjectFileELF::%s detected Linux, min version %" PRIu32 1116 ".%" PRIu32 ".%" PRIu32, 1117 __FUNCTION__, version_info[1], version_info[2], 1118 version_info[3]); 1119 // FIXME we have the minimal version number, we could be propagating 1120 // that. version_info[1] = OS Major, version_info[2] = OS Minor, 1121 // version_info[3] = Revision. 1122 break; 1123 case LLDB_NT_GNU_ABI_OS_HURD: 1124 arch_spec.GetTriple().setOS(llvm::Triple::OSType::UnknownOS); 1125 arch_spec.GetTriple().setVendor( 1126 llvm::Triple::VendorType::UnknownVendor); 1127 LLDB_LOGF(log, 1128 "ObjectFileELF::%s detected Hurd (unsupported), min " 1129 "version %" PRIu32 ".%" PRIu32 ".%" PRIu32, 1130 __FUNCTION__, version_info[1], version_info[2], 1131 version_info[3]); 1132 break; 1133 case LLDB_NT_GNU_ABI_OS_SOLARIS: 1134 arch_spec.GetTriple().setOS(llvm::Triple::OSType::Solaris); 1135 arch_spec.GetTriple().setVendor( 1136 llvm::Triple::VendorType::UnknownVendor); 1137 LLDB_LOGF(log, 1138 "ObjectFileELF::%s detected Solaris, min version %" PRIu32 1139 ".%" PRIu32 ".%" PRIu32, 1140 __FUNCTION__, version_info[1], version_info[2], 1141 version_info[3]); 1142 break; 1143 default: 1144 LLDB_LOGF(log, 1145 "ObjectFileELF::%s unrecognized OS in note, id %" PRIu32 1146 ", min version %" PRIu32 ".%" PRIu32 ".%" PRIu32, 1147 __FUNCTION__, version_info[0], version_info[1], 1148 version_info[2], version_info[3]); 1149 break; 1150 } 1151 } 1152 break; 1153 1154 case LLDB_NT_GNU_BUILD_ID_TAG: 1155 // Only bother processing this if we don't already have the uuid set. 1156 if (!uuid.IsValid()) { 1157 // 16 bytes is UUID|MD5, 20 bytes is SHA1. Other linkers may produce a 1158 // build-id of a different length. Accept it as long as it's at least 1159 // 4 bytes as it will be better than our own crc32. 1160 if (note.n_descsz >= 4) { 1161 if (const uint8_t *buf = data.PeekData(offset, note.n_descsz)) { 1162 // Save the build id as the UUID for the module. 1163 uuid = UUID(buf, note.n_descsz); 1164 } else { 1165 error = Status::FromErrorString( 1166 "failed to read GNU_BUILD_ID note payload"); 1167 return error; 1168 } 1169 } 1170 } 1171 break; 1172 } 1173 if (arch_spec.IsMIPS() && 1174 arch_spec.GetTriple().getOS() == llvm::Triple::OSType::UnknownOS) 1175 // The note.n_name == LLDB_NT_OWNER_GNU is valid for Linux platform 1176 arch_spec.GetTriple().setOS(llvm::Triple::OSType::Linux); 1177 } 1178 // Process NetBSD ELF executables and shared libraries 1179 else if ((note.n_name == LLDB_NT_OWNER_NETBSD) && 1180 (note.n_type == LLDB_NT_NETBSD_IDENT_TAG) && 1181 (note.n_descsz == LLDB_NT_NETBSD_IDENT_DESCSZ) && 1182 (note.n_namesz == LLDB_NT_NETBSD_IDENT_NAMESZ)) { 1183 // Pull out the version info. 1184 uint32_t version_info; 1185 if (data.GetU32(&offset, &version_info, 1) == nullptr) { 1186 error = 1187 Status::FromErrorString("failed to read NetBSD ABI note payload"); 1188 return error; 1189 } 1190 // Convert the version info into a major/minor/patch number. 1191 // #define __NetBSD_Version__ MMmmrrpp00 1192 // 1193 // M = major version 1194 // m = minor version; a minor number of 99 indicates current. 1195 // r = 0 (since NetBSD 3.0 not used) 1196 // p = patchlevel 1197 const uint32_t version_major = version_info / 100000000; 1198 const uint32_t version_minor = (version_info % 100000000) / 1000000; 1199 const uint32_t version_patch = (version_info % 10000) / 100; 1200 // Set the elf OS version to NetBSD. Also clear the vendor. 1201 arch_spec.GetTriple().setOSName( 1202 llvm::formatv("netbsd{0}.{1}.{2}", version_major, version_minor, 1203 version_patch).str()); 1204 arch_spec.GetTriple().setVendor(llvm::Triple::VendorType::UnknownVendor); 1205 } 1206 // Process NetBSD ELF core(5) notes 1207 else if ((note.n_name == LLDB_NT_OWNER_NETBSDCORE) && 1208 (note.n_type == LLDB_NT_NETBSD_PROCINFO)) { 1209 // Set the elf OS version to NetBSD. Also clear the vendor. 1210 arch_spec.GetTriple().setOS(llvm::Triple::OSType::NetBSD); 1211 arch_spec.GetTriple().setVendor(llvm::Triple::VendorType::UnknownVendor); 1212 } 1213 // Process OpenBSD ELF notes. 1214 else if (note.n_name == LLDB_NT_OWNER_OPENBSD) { 1215 // Set the elf OS version to OpenBSD. Also clear the vendor. 1216 arch_spec.GetTriple().setOS(llvm::Triple::OSType::OpenBSD); 1217 arch_spec.GetTriple().setVendor(llvm::Triple::VendorType::UnknownVendor); 1218 } else if (note.n_name == LLDB_NT_OWNER_ANDROID) { 1219 arch_spec.GetTriple().setOS(llvm::Triple::OSType::Linux); 1220 arch_spec.GetTriple().setEnvironment( 1221 llvm::Triple::EnvironmentType::Android); 1222 } else if (note.n_name == LLDB_NT_OWNER_LINUX) { 1223 // This is sometimes found in core files and usually contains extended 1224 // register info 1225 arch_spec.GetTriple().setOS(llvm::Triple::OSType::Linux); 1226 } else if (note.n_name == LLDB_NT_OWNER_CORE) { 1227 // Parse the NT_FILE to look for stuff in paths to shared libraries 1228 // The contents look like this in a 64 bit ELF core file: 1229 // 1230 // count = 0x000000000000000a (10) 1231 // page_size = 0x0000000000001000 (4096) 1232 // Index start end file_ofs path 1233 // ===== ------------------ ------------------ ------------------ ------------------------------------- 1234 // [ 0] 0x0000000000401000 0x0000000000000000 /tmp/a.out 1235 // [ 1] 0x0000000000600000 0x0000000000601000 0x0000000000000000 /tmp/a.out 1236 // [ 2] 0x0000000000601000 0x0000000000602000 0x0000000000000001 /tmp/a.out 1237 // [ 3] 0x00007fa79c9ed000 0x00007fa79cba8000 0x0000000000000000 /lib/x86_64-linux-gnu/libc-2.19.so 1238 // [ 4] 0x00007fa79cba8000 0x00007fa79cda7000 0x00000000000001bb /lib/x86_64-linux-gnu/libc-2.19.so 1239 // [ 5] 0x00007fa79cda7000 0x00007fa79cdab000 0x00000000000001ba /lib/x86_64-linux-gnu/libc-2.19.so 1240 // [ 6] 0x00007fa79cdab000 0x00007fa79cdad000 0x00000000000001be /lib/x86_64-linux-gnu/libc-2.19.so 1241 // [ 7] 0x00007fa79cdb2000 0x00007fa79cdd5000 0x0000000000000000 /lib/x86_64-linux-gnu/ld-2.19.so 1242 // [ 8] 0x00007fa79cfd4000 0x00007fa79cfd5000 0x0000000000000022 /lib/x86_64-linux-gnu/ld-2.19.so 1243 // [ 9] 0x00007fa79cfd5000 0x00007fa79cfd6000 0x0000000000000023 /lib/x86_64-linux-gnu/ld-2.19.so 1244 // 1245 // In the 32 bit ELFs the count, page_size, start, end, file_ofs are 1246 // uint32_t. 1247 // 1248 // For reference: see readelf source code (in binutils). 1249 if (note.n_type == NT_FILE) { 1250 uint64_t count = data.GetAddress(&offset); 1251 const char *cstr; 1252 data.GetAddress(&offset); // Skip page size 1253 offset += count * 3 * 1254 data.GetAddressByteSize(); // Skip all start/end/file_ofs 1255 for (size_t i = 0; i < count; ++i) { 1256 cstr = data.GetCStr(&offset); 1257 if (cstr == nullptr) { 1258 error = Status::FromErrorStringWithFormat( 1259 "ObjectFileELF::%s trying to read " 1260 "at an offset after the end " 1261 "(GetCStr returned nullptr)", 1262 __FUNCTION__); 1263 return error; 1264 } 1265 llvm::StringRef path(cstr); 1266 if (path.contains("/lib/x86_64-linux-gnu") || path.contains("/lib/i386-linux-gnu")) { 1267 arch_spec.GetTriple().setOS(llvm::Triple::OSType::Linux); 1268 break; 1269 } 1270 } 1271 if (arch_spec.IsMIPS() && 1272 arch_spec.GetTriple().getOS() == llvm::Triple::OSType::UnknownOS) 1273 // In case of MIPSR6, the LLDB_NT_OWNER_GNU note is missing for some 1274 // cases (e.g. compile with -nostdlib) Hence set OS to Linux 1275 arch_spec.GetTriple().setOS(llvm::Triple::OSType::Linux); 1276 } 1277 } 1278 1279 // Calculate the offset of the next note just in case "offset" has been 1280 // used to poke at the contents of the note data 1281 offset = note_offset + note.GetByteSize(); 1282 } 1283 1284 return error; 1285 } 1286 1287 void ObjectFileELF::ParseARMAttributes(DataExtractor &data, uint64_t length, 1288 ArchSpec &arch_spec) { 1289 lldb::offset_t Offset = 0; 1290 1291 uint8_t FormatVersion = data.GetU8(&Offset); 1292 if (FormatVersion != llvm::ELFAttrs::Format_Version) 1293 return; 1294 1295 Offset = Offset + sizeof(uint32_t); // Section Length 1296 llvm::StringRef VendorName = data.GetCStr(&Offset); 1297 1298 if (VendorName != "aeabi") 1299 return; 1300 1301 if (arch_spec.GetTriple().getEnvironment() == 1302 llvm::Triple::UnknownEnvironment) 1303 arch_spec.GetTriple().setEnvironment(llvm::Triple::EABI); 1304 1305 while (Offset < length) { 1306 uint8_t Tag = data.GetU8(&Offset); 1307 uint32_t Size = data.GetU32(&Offset); 1308 1309 if (Tag != llvm::ARMBuildAttrs::File || Size == 0) 1310 continue; 1311 1312 while (Offset < length) { 1313 uint64_t Tag = data.GetULEB128(&Offset); 1314 switch (Tag) { 1315 default: 1316 if (Tag < 32) 1317 data.GetULEB128(&Offset); 1318 else if (Tag % 2 == 0) 1319 data.GetULEB128(&Offset); 1320 else 1321 data.GetCStr(&Offset); 1322 1323 break; 1324 1325 case llvm::ARMBuildAttrs::CPU_raw_name: 1326 case llvm::ARMBuildAttrs::CPU_name: 1327 data.GetCStr(&Offset); 1328 1329 break; 1330 1331 case llvm::ARMBuildAttrs::ABI_VFP_args: { 1332 uint64_t VFPArgs = data.GetULEB128(&Offset); 1333 1334 if (VFPArgs == llvm::ARMBuildAttrs::BaseAAPCS) { 1335 if (arch_spec.GetTriple().getEnvironment() == 1336 llvm::Triple::UnknownEnvironment || 1337 arch_spec.GetTriple().getEnvironment() == llvm::Triple::EABIHF) 1338 arch_spec.GetTriple().setEnvironment(llvm::Triple::EABI); 1339 1340 arch_spec.SetFlags(ArchSpec::eARM_abi_soft_float); 1341 } else if (VFPArgs == llvm::ARMBuildAttrs::HardFPAAPCS) { 1342 if (arch_spec.GetTriple().getEnvironment() == 1343 llvm::Triple::UnknownEnvironment || 1344 arch_spec.GetTriple().getEnvironment() == llvm::Triple::EABI) 1345 arch_spec.GetTriple().setEnvironment(llvm::Triple::EABIHF); 1346 1347 arch_spec.SetFlags(ArchSpec::eARM_abi_hard_float); 1348 } 1349 1350 break; 1351 } 1352 } 1353 } 1354 } 1355 } 1356 1357 // GetSectionHeaderInfo 1358 size_t ObjectFileELF::GetSectionHeaderInfo(SectionHeaderColl §ion_headers, 1359 DataExtractor &object_data, 1360 const elf::ELFHeader &header, 1361 lldb_private::UUID &uuid, 1362 std::string &gnu_debuglink_file, 1363 uint32_t &gnu_debuglink_crc, 1364 ArchSpec &arch_spec) { 1365 // Don't reparse the section headers if we already did that. 1366 if (!section_headers.empty()) 1367 return section_headers.size(); 1368 1369 // Only initialize the arch_spec to okay defaults if they're not already set. 1370 // We'll refine this with note data as we parse the notes. 1371 if (arch_spec.GetTriple().getOS() == llvm::Triple::OSType::UnknownOS) { 1372 llvm::Triple::OSType ostype; 1373 llvm::Triple::OSType spec_ostype; 1374 const uint32_t sub_type = subTypeFromElfHeader(header); 1375 arch_spec.SetArchitecture(eArchTypeELF, header.e_machine, sub_type, 1376 header.e_ident[EI_OSABI]); 1377 1378 // Validate if it is ok to remove GetOsFromOSABI. Note, that now the OS is 1379 // determined based on EI_OSABI flag and the info extracted from ELF notes 1380 // (see RefineModuleDetailsFromNote). However in some cases that still 1381 // might be not enough: for example a shared library might not have any 1382 // notes at all and have EI_OSABI flag set to System V, as result the OS 1383 // will be set to UnknownOS. 1384 GetOsFromOSABI(header.e_ident[EI_OSABI], ostype); 1385 spec_ostype = arch_spec.GetTriple().getOS(); 1386 assert(spec_ostype == ostype); 1387 UNUSED_IF_ASSERT_DISABLED(spec_ostype); 1388 } 1389 1390 if (arch_spec.GetMachine() == llvm::Triple::mips || 1391 arch_spec.GetMachine() == llvm::Triple::mipsel || 1392 arch_spec.GetMachine() == llvm::Triple::mips64 || 1393 arch_spec.GetMachine() == llvm::Triple::mips64el) { 1394 switch (header.e_flags & llvm::ELF::EF_MIPS_ARCH_ASE) { 1395 case llvm::ELF::EF_MIPS_MICROMIPS: 1396 arch_spec.SetFlags(ArchSpec::eMIPSAse_micromips); 1397 break; 1398 case llvm::ELF::EF_MIPS_ARCH_ASE_M16: 1399 arch_spec.SetFlags(ArchSpec::eMIPSAse_mips16); 1400 break; 1401 case llvm::ELF::EF_MIPS_ARCH_ASE_MDMX: 1402 arch_spec.SetFlags(ArchSpec::eMIPSAse_mdmx); 1403 break; 1404 default: 1405 break; 1406 } 1407 } 1408 1409 if (arch_spec.GetMachine() == llvm::Triple::arm || 1410 arch_spec.GetMachine() == llvm::Triple::thumb) { 1411 if (header.e_flags & llvm::ELF::EF_ARM_SOFT_FLOAT) 1412 arch_spec.SetFlags(ArchSpec::eARM_abi_soft_float); 1413 else if (header.e_flags & llvm::ELF::EF_ARM_VFP_FLOAT) 1414 arch_spec.SetFlags(ArchSpec::eARM_abi_hard_float); 1415 } 1416 1417 if (arch_spec.GetMachine() == llvm::Triple::riscv32 || 1418 arch_spec.GetMachine() == llvm::Triple::riscv64) { 1419 uint32_t flags = arch_spec.GetFlags(); 1420 1421 if (header.e_flags & llvm::ELF::EF_RISCV_RVC) 1422 flags |= ArchSpec::eRISCV_rvc; 1423 if (header.e_flags & llvm::ELF::EF_RISCV_RVE) 1424 flags |= ArchSpec::eRISCV_rve; 1425 1426 if ((header.e_flags & llvm::ELF::EF_RISCV_FLOAT_ABI_SINGLE) == 1427 llvm::ELF::EF_RISCV_FLOAT_ABI_SINGLE) 1428 flags |= ArchSpec::eRISCV_float_abi_single; 1429 else if ((header.e_flags & llvm::ELF::EF_RISCV_FLOAT_ABI_DOUBLE) == 1430 llvm::ELF::EF_RISCV_FLOAT_ABI_DOUBLE) 1431 flags |= ArchSpec::eRISCV_float_abi_double; 1432 else if ((header.e_flags & llvm::ELF::EF_RISCV_FLOAT_ABI_QUAD) == 1433 llvm::ELF::EF_RISCV_FLOAT_ABI_QUAD) 1434 flags |= ArchSpec::eRISCV_float_abi_quad; 1435 1436 arch_spec.SetFlags(flags); 1437 } 1438 1439 if (arch_spec.GetMachine() == llvm::Triple::loongarch32 || 1440 arch_spec.GetMachine() == llvm::Triple::loongarch64) { 1441 uint32_t flags = arch_spec.GetFlags(); 1442 switch (header.e_flags & llvm::ELF::EF_LOONGARCH_ABI_MODIFIER_MASK) { 1443 case llvm::ELF::EF_LOONGARCH_ABI_SINGLE_FLOAT: 1444 flags |= ArchSpec::eLoongArch_abi_single_float; 1445 break; 1446 case llvm::ELF::EF_LOONGARCH_ABI_DOUBLE_FLOAT: 1447 flags |= ArchSpec::eLoongArch_abi_double_float; 1448 break; 1449 case llvm::ELF::EF_LOONGARCH_ABI_SOFT_FLOAT: 1450 break; 1451 } 1452 1453 arch_spec.SetFlags(flags); 1454 } 1455 1456 // If there are no section headers we are done. 1457 if (header.e_shnum == 0) 1458 return 0; 1459 1460 Log *log = GetLog(LLDBLog::Modules); 1461 1462 section_headers.resize(header.e_shnum); 1463 if (section_headers.size() != header.e_shnum) 1464 return 0; 1465 1466 const size_t sh_size = header.e_shnum * header.e_shentsize; 1467 const elf_off sh_offset = header.e_shoff; 1468 DataExtractor sh_data; 1469 if (sh_data.SetData(object_data, sh_offset, sh_size) != sh_size) 1470 return 0; 1471 1472 uint32_t idx; 1473 lldb::offset_t offset; 1474 for (idx = 0, offset = 0; idx < header.e_shnum; ++idx) { 1475 if (!section_headers[idx].Parse(sh_data, &offset)) 1476 break; 1477 } 1478 if (idx < section_headers.size()) 1479 section_headers.resize(idx); 1480 1481 const unsigned strtab_idx = header.e_shstrndx; 1482 if (strtab_idx && strtab_idx < section_headers.size()) { 1483 const ELFSectionHeaderInfo &sheader = section_headers[strtab_idx]; 1484 const size_t byte_size = sheader.sh_size; 1485 const Elf64_Off offset = sheader.sh_offset; 1486 lldb_private::DataExtractor shstr_data; 1487 1488 if (shstr_data.SetData(object_data, offset, byte_size) == byte_size) { 1489 for (SectionHeaderCollIter I = section_headers.begin(); 1490 I != section_headers.end(); ++I) { 1491 static ConstString g_sect_name_gnu_debuglink(".gnu_debuglink"); 1492 const ELFSectionHeaderInfo &sheader = *I; 1493 const uint64_t section_size = 1494 sheader.sh_type == SHT_NOBITS ? 0 : sheader.sh_size; 1495 ConstString name(shstr_data.PeekCStr(I->sh_name)); 1496 1497 I->section_name = name; 1498 1499 if (arch_spec.IsMIPS()) { 1500 uint32_t arch_flags = arch_spec.GetFlags(); 1501 DataExtractor data; 1502 if (sheader.sh_type == SHT_MIPS_ABIFLAGS) { 1503 1504 if (section_size && (data.SetData(object_data, sheader.sh_offset, 1505 section_size) == section_size)) { 1506 // MIPS ASE Mask is at offset 12 in MIPS.abiflags section 1507 lldb::offset_t offset = 12; // MIPS ABI Flags Version: 0 1508 arch_flags |= data.GetU32(&offset); 1509 1510 // The floating point ABI is at offset 7 1511 offset = 7; 1512 switch (data.GetU8(&offset)) { 1513 case llvm::Mips::Val_GNU_MIPS_ABI_FP_ANY: 1514 arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_ANY; 1515 break; 1516 case llvm::Mips::Val_GNU_MIPS_ABI_FP_DOUBLE: 1517 arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_DOUBLE; 1518 break; 1519 case llvm::Mips::Val_GNU_MIPS_ABI_FP_SINGLE: 1520 arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_SINGLE; 1521 break; 1522 case llvm::Mips::Val_GNU_MIPS_ABI_FP_SOFT: 1523 arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_SOFT; 1524 break; 1525 case llvm::Mips::Val_GNU_MIPS_ABI_FP_OLD_64: 1526 arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_OLD_64; 1527 break; 1528 case llvm::Mips::Val_GNU_MIPS_ABI_FP_XX: 1529 arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_XX; 1530 break; 1531 case llvm::Mips::Val_GNU_MIPS_ABI_FP_64: 1532 arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_64; 1533 break; 1534 case llvm::Mips::Val_GNU_MIPS_ABI_FP_64A: 1535 arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_64A; 1536 break; 1537 } 1538 } 1539 } 1540 // Settings appropriate ArchSpec ABI Flags 1541 switch (header.e_flags & llvm::ELF::EF_MIPS_ABI) { 1542 case llvm::ELF::EF_MIPS_ABI_O32: 1543 arch_flags |= lldb_private::ArchSpec::eMIPSABI_O32; 1544 break; 1545 case EF_MIPS_ABI_O64: 1546 arch_flags |= lldb_private::ArchSpec::eMIPSABI_O64; 1547 break; 1548 case EF_MIPS_ABI_EABI32: 1549 arch_flags |= lldb_private::ArchSpec::eMIPSABI_EABI32; 1550 break; 1551 case EF_MIPS_ABI_EABI64: 1552 arch_flags |= lldb_private::ArchSpec::eMIPSABI_EABI64; 1553 break; 1554 default: 1555 // ABI Mask doesn't cover N32 and N64 ABI. 1556 if (header.e_ident[EI_CLASS] == llvm::ELF::ELFCLASS64) 1557 arch_flags |= lldb_private::ArchSpec::eMIPSABI_N64; 1558 else if (header.e_flags & llvm::ELF::EF_MIPS_ABI2) 1559 arch_flags |= lldb_private::ArchSpec::eMIPSABI_N32; 1560 break; 1561 } 1562 arch_spec.SetFlags(arch_flags); 1563 } 1564 1565 if (arch_spec.GetMachine() == llvm::Triple::arm || 1566 arch_spec.GetMachine() == llvm::Triple::thumb) { 1567 DataExtractor data; 1568 1569 if (sheader.sh_type == SHT_ARM_ATTRIBUTES && section_size != 0 && 1570 data.SetData(object_data, sheader.sh_offset, section_size) == section_size) 1571 ParseARMAttributes(data, section_size, arch_spec); 1572 } 1573 1574 if (name == g_sect_name_gnu_debuglink) { 1575 DataExtractor data; 1576 if (section_size && (data.SetData(object_data, sheader.sh_offset, 1577 section_size) == section_size)) { 1578 lldb::offset_t gnu_debuglink_offset = 0; 1579 gnu_debuglink_file = data.GetCStr(&gnu_debuglink_offset); 1580 gnu_debuglink_offset = llvm::alignTo(gnu_debuglink_offset, 4); 1581 data.GetU32(&gnu_debuglink_offset, &gnu_debuglink_crc, 1); 1582 } 1583 } 1584 1585 // Process ELF note section entries. 1586 bool is_note_header = (sheader.sh_type == SHT_NOTE); 1587 1588 // The section header ".note.android.ident" is stored as a 1589 // PROGBITS type header but it is actually a note header. 1590 static ConstString g_sect_name_android_ident(".note.android.ident"); 1591 if (!is_note_header && name == g_sect_name_android_ident) 1592 is_note_header = true; 1593 1594 if (is_note_header) { 1595 // Allow notes to refine module info. 1596 DataExtractor data; 1597 if (section_size && (data.SetData(object_data, sheader.sh_offset, 1598 section_size) == section_size)) { 1599 Status error = RefineModuleDetailsFromNote(data, arch_spec, uuid); 1600 if (error.Fail()) { 1601 LLDB_LOGF(log, "ObjectFileELF::%s ELF note processing failed: %s", 1602 __FUNCTION__, error.AsCString()); 1603 } 1604 } 1605 } 1606 } 1607 1608 // Make any unknown triple components to be unspecified unknowns. 1609 if (arch_spec.GetTriple().getVendor() == llvm::Triple::UnknownVendor) 1610 arch_spec.GetTriple().setVendorName(llvm::StringRef()); 1611 if (arch_spec.GetTriple().getOS() == llvm::Triple::UnknownOS) 1612 arch_spec.GetTriple().setOSName(llvm::StringRef()); 1613 1614 return section_headers.size(); 1615 } 1616 } 1617 1618 section_headers.clear(); 1619 return 0; 1620 } 1621 1622 llvm::StringRef 1623 ObjectFileELF::StripLinkerSymbolAnnotations(llvm::StringRef symbol_name) const { 1624 size_t pos = symbol_name.find('@'); 1625 return symbol_name.substr(0, pos); 1626 } 1627 1628 // ParseSectionHeaders 1629 size_t ObjectFileELF::ParseSectionHeaders() { 1630 return GetSectionHeaderInfo(m_section_headers, m_data, m_header, m_uuid, 1631 m_gnu_debuglink_file, m_gnu_debuglink_crc, 1632 m_arch_spec); 1633 } 1634 1635 const ObjectFileELF::ELFSectionHeaderInfo * 1636 ObjectFileELF::GetSectionHeaderByIndex(lldb::user_id_t id) { 1637 if (!ParseSectionHeaders()) 1638 return nullptr; 1639 1640 if (id < m_section_headers.size()) 1641 return &m_section_headers[id]; 1642 1643 return nullptr; 1644 } 1645 1646 lldb::user_id_t ObjectFileELF::GetSectionIndexByName(const char *name) { 1647 if (!name || !name[0] || !ParseSectionHeaders()) 1648 return 0; 1649 for (size_t i = 1; i < m_section_headers.size(); ++i) 1650 if (m_section_headers[i].section_name == ConstString(name)) 1651 return i; 1652 return 0; 1653 } 1654 1655 static SectionType GetSectionTypeFromName(llvm::StringRef Name) { 1656 if (Name.consume_front(".debug_")) { 1657 return llvm::StringSwitch<SectionType>(Name) 1658 .Case("abbrev", eSectionTypeDWARFDebugAbbrev) 1659 .Case("abbrev.dwo", eSectionTypeDWARFDebugAbbrevDwo) 1660 .Case("addr", eSectionTypeDWARFDebugAddr) 1661 .Case("aranges", eSectionTypeDWARFDebugAranges) 1662 .Case("cu_index", eSectionTypeDWARFDebugCuIndex) 1663 .Case("frame", eSectionTypeDWARFDebugFrame) 1664 .Case("info", eSectionTypeDWARFDebugInfo) 1665 .Case("info.dwo", eSectionTypeDWARFDebugInfoDwo) 1666 .Cases("line", "line.dwo", eSectionTypeDWARFDebugLine) 1667 .Cases("line_str", "line_str.dwo", eSectionTypeDWARFDebugLineStr) 1668 .Case("loc", eSectionTypeDWARFDebugLoc) 1669 .Case("loc.dwo", eSectionTypeDWARFDebugLocDwo) 1670 .Case("loclists", eSectionTypeDWARFDebugLocLists) 1671 .Case("loclists.dwo", eSectionTypeDWARFDebugLocListsDwo) 1672 .Case("macinfo", eSectionTypeDWARFDebugMacInfo) 1673 .Cases("macro", "macro.dwo", eSectionTypeDWARFDebugMacro) 1674 .Case("names", eSectionTypeDWARFDebugNames) 1675 .Case("pubnames", eSectionTypeDWARFDebugPubNames) 1676 .Case("pubtypes", eSectionTypeDWARFDebugPubTypes) 1677 .Case("ranges", eSectionTypeDWARFDebugRanges) 1678 .Case("rnglists", eSectionTypeDWARFDebugRngLists) 1679 .Case("rnglists.dwo", eSectionTypeDWARFDebugRngListsDwo) 1680 .Case("str", eSectionTypeDWARFDebugStr) 1681 .Case("str.dwo", eSectionTypeDWARFDebugStrDwo) 1682 .Case("str_offsets", eSectionTypeDWARFDebugStrOffsets) 1683 .Case("str_offsets.dwo", eSectionTypeDWARFDebugStrOffsetsDwo) 1684 .Case("tu_index", eSectionTypeDWARFDebugTuIndex) 1685 .Case("types", eSectionTypeDWARFDebugTypes) 1686 .Case("types.dwo", eSectionTypeDWARFDebugTypesDwo) 1687 .Default(eSectionTypeOther); 1688 } 1689 return llvm::StringSwitch<SectionType>(Name) 1690 .Case(".ARM.exidx", eSectionTypeARMexidx) 1691 .Case(".ARM.extab", eSectionTypeARMextab) 1692 .Case(".ctf", eSectionTypeDebug) 1693 .Cases(".data", ".tdata", eSectionTypeData) 1694 .Case(".eh_frame", eSectionTypeEHFrame) 1695 .Case(".gnu_debugaltlink", eSectionTypeDWARFGNUDebugAltLink) 1696 .Case(".gosymtab", eSectionTypeGoSymtab) 1697 .Case(".text", eSectionTypeCode) 1698 .Case(".swift_ast", eSectionTypeSwiftModules) 1699 .Default(eSectionTypeOther); 1700 } 1701 1702 SectionType ObjectFileELF::GetSectionType(const ELFSectionHeaderInfo &H) const { 1703 switch (H.sh_type) { 1704 case SHT_PROGBITS: 1705 if (H.sh_flags & SHF_EXECINSTR) 1706 return eSectionTypeCode; 1707 break; 1708 case SHT_NOBITS: 1709 if (H.sh_flags & SHF_ALLOC) 1710 return eSectionTypeZeroFill; 1711 break; 1712 case SHT_SYMTAB: 1713 return eSectionTypeELFSymbolTable; 1714 case SHT_DYNSYM: 1715 return eSectionTypeELFDynamicSymbols; 1716 case SHT_RELA: 1717 case SHT_REL: 1718 return eSectionTypeELFRelocationEntries; 1719 case SHT_DYNAMIC: 1720 return eSectionTypeELFDynamicLinkInfo; 1721 } 1722 return GetSectionTypeFromName(H.section_name.GetStringRef()); 1723 } 1724 1725 static uint32_t GetTargetByteSize(SectionType Type, const ArchSpec &arch) { 1726 switch (Type) { 1727 case eSectionTypeData: 1728 case eSectionTypeZeroFill: 1729 return arch.GetDataByteSize(); 1730 case eSectionTypeCode: 1731 return arch.GetCodeByteSize(); 1732 default: 1733 return 1; 1734 } 1735 } 1736 1737 static Permissions GetPermissions(const ELFSectionHeader &H) { 1738 Permissions Perm = Permissions(0); 1739 if (H.sh_flags & SHF_ALLOC) 1740 Perm |= ePermissionsReadable; 1741 if (H.sh_flags & SHF_WRITE) 1742 Perm |= ePermissionsWritable; 1743 if (H.sh_flags & SHF_EXECINSTR) 1744 Perm |= ePermissionsExecutable; 1745 return Perm; 1746 } 1747 1748 static Permissions GetPermissions(const ELFProgramHeader &H) { 1749 Permissions Perm = Permissions(0); 1750 if (H.p_flags & PF_R) 1751 Perm |= ePermissionsReadable; 1752 if (H.p_flags & PF_W) 1753 Perm |= ePermissionsWritable; 1754 if (H.p_flags & PF_X) 1755 Perm |= ePermissionsExecutable; 1756 return Perm; 1757 } 1758 1759 namespace { 1760 1761 using VMRange = lldb_private::Range<addr_t, addr_t>; 1762 1763 struct SectionAddressInfo { 1764 SectionSP Segment; 1765 VMRange Range; 1766 }; 1767 1768 // (Unlinked) ELF object files usually have 0 for every section address, meaning 1769 // we need to compute synthetic addresses in order for "file addresses" from 1770 // different sections to not overlap. This class handles that logic. 1771 class VMAddressProvider { 1772 using VMMap = llvm::IntervalMap<addr_t, SectionSP, 4, 1773 llvm::IntervalMapHalfOpenInfo<addr_t>>; 1774 1775 ObjectFile::Type ObjectType; 1776 addr_t NextVMAddress = 0; 1777 VMMap::Allocator Alloc; 1778 VMMap Segments{Alloc}; 1779 VMMap Sections{Alloc}; 1780 lldb_private::Log *Log = GetLog(LLDBLog::Modules); 1781 size_t SegmentCount = 0; 1782 std::string SegmentName; 1783 1784 VMRange GetVMRange(const ELFSectionHeader &H) { 1785 addr_t Address = H.sh_addr; 1786 addr_t Size = H.sh_flags & SHF_ALLOC ? H.sh_size : 0; 1787 1788 // When this is a debug file for relocatable file, the address is all zero 1789 // and thus needs to use accumulate method 1790 if ((ObjectType == ObjectFile::Type::eTypeObjectFile || 1791 (ObjectType == ObjectFile::Type::eTypeDebugInfo && H.sh_addr == 0)) && 1792 Segments.empty() && (H.sh_flags & SHF_ALLOC)) { 1793 NextVMAddress = 1794 llvm::alignTo(NextVMAddress, std::max<addr_t>(H.sh_addralign, 1)); 1795 Address = NextVMAddress; 1796 NextVMAddress += Size; 1797 } 1798 return VMRange(Address, Size); 1799 } 1800 1801 public: 1802 VMAddressProvider(ObjectFile::Type Type, llvm::StringRef SegmentName) 1803 : ObjectType(Type), SegmentName(std::string(SegmentName)) {} 1804 1805 std::string GetNextSegmentName() const { 1806 return llvm::formatv("{0}[{1}]", SegmentName, SegmentCount).str(); 1807 } 1808 1809 std::optional<VMRange> GetAddressInfo(const ELFProgramHeader &H) { 1810 if (H.p_memsz == 0) { 1811 LLDB_LOG(Log, "Ignoring zero-sized {0} segment. Corrupt object file?", 1812 SegmentName); 1813 return std::nullopt; 1814 } 1815 1816 if (Segments.overlaps(H.p_vaddr, H.p_vaddr + H.p_memsz)) { 1817 LLDB_LOG(Log, "Ignoring overlapping {0} segment. Corrupt object file?", 1818 SegmentName); 1819 return std::nullopt; 1820 } 1821 return VMRange(H.p_vaddr, H.p_memsz); 1822 } 1823 1824 std::optional<SectionAddressInfo> GetAddressInfo(const ELFSectionHeader &H) { 1825 VMRange Range = GetVMRange(H); 1826 SectionSP Segment; 1827 auto It = Segments.find(Range.GetRangeBase()); 1828 if ((H.sh_flags & SHF_ALLOC) && It.valid()) { 1829 addr_t MaxSize; 1830 if (It.start() <= Range.GetRangeBase()) { 1831 MaxSize = It.stop() - Range.GetRangeBase(); 1832 Segment = *It; 1833 } else 1834 MaxSize = It.start() - Range.GetRangeBase(); 1835 if (Range.GetByteSize() > MaxSize) { 1836 LLDB_LOG(Log, "Shortening section crossing segment boundaries. " 1837 "Corrupt object file?"); 1838 Range.SetByteSize(MaxSize); 1839 } 1840 } 1841 if (Range.GetByteSize() > 0 && 1842 Sections.overlaps(Range.GetRangeBase(), Range.GetRangeEnd())) { 1843 LLDB_LOG(Log, "Ignoring overlapping section. Corrupt object file?"); 1844 return std::nullopt; 1845 } 1846 if (Segment) 1847 Range.Slide(-Segment->GetFileAddress()); 1848 return SectionAddressInfo{Segment, Range}; 1849 } 1850 1851 void AddSegment(const VMRange &Range, SectionSP Seg) { 1852 Segments.insert(Range.GetRangeBase(), Range.GetRangeEnd(), std::move(Seg)); 1853 ++SegmentCount; 1854 } 1855 1856 void AddSection(SectionAddressInfo Info, SectionSP Sect) { 1857 if (Info.Range.GetByteSize() == 0) 1858 return; 1859 if (Info.Segment) 1860 Info.Range.Slide(Info.Segment->GetFileAddress()); 1861 Sections.insert(Info.Range.GetRangeBase(), Info.Range.GetRangeEnd(), 1862 std::move(Sect)); 1863 } 1864 }; 1865 } 1866 1867 // We have to do this because ELF doesn't have section IDs, and also 1868 // doesn't require section names to be unique. (We use the section index 1869 // for section IDs, but that isn't guaranteed to be the same in separate 1870 // debug images.) 1871 static SectionSP FindMatchingSection(const SectionList §ion_list, 1872 SectionSP section) { 1873 SectionSP sect_sp; 1874 1875 addr_t vm_addr = section->GetFileAddress(); 1876 ConstString name = section->GetName(); 1877 offset_t byte_size = section->GetByteSize(); 1878 bool thread_specific = section->IsThreadSpecific(); 1879 uint32_t permissions = section->GetPermissions(); 1880 uint32_t alignment = section->GetLog2Align(); 1881 1882 for (auto sect : section_list) { 1883 if (sect->GetName() == name && 1884 sect->IsThreadSpecific() == thread_specific && 1885 sect->GetPermissions() == permissions && 1886 sect->GetByteSize() == byte_size && sect->GetFileAddress() == vm_addr && 1887 sect->GetLog2Align() == alignment) { 1888 sect_sp = sect; 1889 break; 1890 } else { 1891 sect_sp = FindMatchingSection(sect->GetChildren(), section); 1892 if (sect_sp) 1893 break; 1894 } 1895 } 1896 1897 return sect_sp; 1898 } 1899 1900 void ObjectFileELF::CreateSections(SectionList &unified_section_list) { 1901 if (m_sections_up) 1902 return; 1903 1904 m_sections_up = std::make_unique<SectionList>(); 1905 VMAddressProvider regular_provider(GetType(), "PT_LOAD"); 1906 VMAddressProvider tls_provider(GetType(), "PT_TLS"); 1907 1908 for (const auto &EnumPHdr : llvm::enumerate(ProgramHeaders())) { 1909 const ELFProgramHeader &PHdr = EnumPHdr.value(); 1910 if (PHdr.p_type != PT_LOAD && PHdr.p_type != PT_TLS) 1911 continue; 1912 1913 VMAddressProvider &provider = 1914 PHdr.p_type == PT_TLS ? tls_provider : regular_provider; 1915 auto InfoOr = provider.GetAddressInfo(PHdr); 1916 if (!InfoOr) 1917 continue; 1918 1919 uint32_t Log2Align = llvm::Log2_64(std::max<elf_xword>(PHdr.p_align, 1)); 1920 SectionSP Segment = std::make_shared<Section>( 1921 GetModule(), this, SegmentID(EnumPHdr.index()), 1922 ConstString(provider.GetNextSegmentName()), eSectionTypeContainer, 1923 InfoOr->GetRangeBase(), InfoOr->GetByteSize(), PHdr.p_offset, 1924 PHdr.p_filesz, Log2Align, /*flags*/ 0); 1925 Segment->SetPermissions(GetPermissions(PHdr)); 1926 Segment->SetIsThreadSpecific(PHdr.p_type == PT_TLS); 1927 m_sections_up->AddSection(Segment); 1928 1929 provider.AddSegment(*InfoOr, std::move(Segment)); 1930 } 1931 1932 ParseSectionHeaders(); 1933 if (m_section_headers.empty()) 1934 return; 1935 1936 for (SectionHeaderCollIter I = std::next(m_section_headers.begin()); 1937 I != m_section_headers.end(); ++I) { 1938 const ELFSectionHeaderInfo &header = *I; 1939 1940 ConstString &name = I->section_name; 1941 const uint64_t file_size = 1942 header.sh_type == SHT_NOBITS ? 0 : header.sh_size; 1943 1944 VMAddressProvider &provider = 1945 header.sh_flags & SHF_TLS ? tls_provider : regular_provider; 1946 auto InfoOr = provider.GetAddressInfo(header); 1947 if (!InfoOr) 1948 continue; 1949 1950 SectionType sect_type = GetSectionType(header); 1951 1952 const uint32_t target_bytes_size = 1953 GetTargetByteSize(sect_type, m_arch_spec); 1954 1955 elf::elf_xword log2align = 1956 (header.sh_addralign == 0) ? 0 : llvm::Log2_64(header.sh_addralign); 1957 1958 SectionSP section_sp(new Section( 1959 InfoOr->Segment, GetModule(), // Module to which this section belongs. 1960 this, // ObjectFile to which this section belongs and should 1961 // read section data from. 1962 SectionIndex(I), // Section ID. 1963 name, // Section name. 1964 sect_type, // Section type. 1965 InfoOr->Range.GetRangeBase(), // VM address. 1966 InfoOr->Range.GetByteSize(), // VM size in bytes of this section. 1967 header.sh_offset, // Offset of this section in the file. 1968 file_size, // Size of the section as found in the file. 1969 log2align, // Alignment of the section 1970 header.sh_flags, // Flags for this section. 1971 target_bytes_size)); // Number of host bytes per target byte 1972 1973 section_sp->SetPermissions(GetPermissions(header)); 1974 section_sp->SetIsThreadSpecific(header.sh_flags & SHF_TLS); 1975 (InfoOr->Segment ? InfoOr->Segment->GetChildren() : *m_sections_up) 1976 .AddSection(section_sp); 1977 provider.AddSection(std::move(*InfoOr), std::move(section_sp)); 1978 } 1979 1980 // For eTypeDebugInfo files, the Symbol Vendor will take care of updating the 1981 // unified section list. 1982 if (GetType() != eTypeDebugInfo) 1983 unified_section_list = *m_sections_up; 1984 1985 // If there's a .gnu_debugdata section, we'll try to read the .symtab that's 1986 // embedded in there and replace the one in the original object file (if any). 1987 // If there's none in the orignal object file, we add it to it. 1988 if (auto gdd_obj_file = GetGnuDebugDataObjectFile()) { 1989 if (auto gdd_objfile_section_list = gdd_obj_file->GetSectionList()) { 1990 if (SectionSP symtab_section_sp = 1991 gdd_objfile_section_list->FindSectionByType( 1992 eSectionTypeELFSymbolTable, true)) { 1993 SectionSP module_section_sp = unified_section_list.FindSectionByType( 1994 eSectionTypeELFSymbolTable, true); 1995 if (module_section_sp) 1996 unified_section_list.ReplaceSection(module_section_sp->GetID(), 1997 symtab_section_sp); 1998 else 1999 unified_section_list.AddSection(symtab_section_sp); 2000 } 2001 } 2002 } 2003 } 2004 2005 std::shared_ptr<ObjectFileELF> ObjectFileELF::GetGnuDebugDataObjectFile() { 2006 if (m_gnu_debug_data_object_file != nullptr) 2007 return m_gnu_debug_data_object_file; 2008 2009 SectionSP section = 2010 GetSectionList()->FindSectionByName(ConstString(".gnu_debugdata")); 2011 if (!section) 2012 return nullptr; 2013 2014 if (!lldb_private::lzma::isAvailable()) { 2015 GetModule()->ReportWarning( 2016 "No LZMA support found for reading .gnu_debugdata section"); 2017 return nullptr; 2018 } 2019 2020 // Uncompress the data 2021 DataExtractor data; 2022 section->GetSectionData(data); 2023 llvm::SmallVector<uint8_t, 0> uncompressedData; 2024 auto err = lldb_private::lzma::uncompress(data.GetData(), uncompressedData); 2025 if (err) { 2026 GetModule()->ReportWarning( 2027 "An error occurred while decompression the section {0}: {1}", 2028 section->GetName().AsCString(), llvm::toString(std::move(err)).c_str()); 2029 return nullptr; 2030 } 2031 2032 // Construct ObjectFileELF object from decompressed buffer 2033 DataBufferSP gdd_data_buf( 2034 new DataBufferHeap(uncompressedData.data(), uncompressedData.size())); 2035 auto fspec = GetFileSpec().CopyByAppendingPathComponent( 2036 llvm::StringRef("gnu_debugdata")); 2037 m_gnu_debug_data_object_file.reset(new ObjectFileELF( 2038 GetModule(), gdd_data_buf, 0, &fspec, 0, gdd_data_buf->GetByteSize())); 2039 2040 // This line is essential; otherwise a breakpoint can be set but not hit. 2041 m_gnu_debug_data_object_file->SetType(ObjectFile::eTypeDebugInfo); 2042 2043 ArchSpec spec = m_gnu_debug_data_object_file->GetArchitecture(); 2044 if (spec && m_gnu_debug_data_object_file->SetModulesArchitecture(spec)) 2045 return m_gnu_debug_data_object_file; 2046 2047 return nullptr; 2048 } 2049 2050 // Find the arm/aarch64 mapping symbol character in the given symbol name. 2051 // Mapping symbols have the form of "$<char>[.<any>]*". Additionally we 2052 // recognize cases when the mapping symbol prefixed by an arbitrary string 2053 // because if a symbol prefix added to each symbol in the object file with 2054 // objcopy then the mapping symbols are also prefixed. 2055 static char FindArmAarch64MappingSymbol(const char *symbol_name) { 2056 if (!symbol_name) 2057 return '\0'; 2058 2059 const char *dollar_pos = ::strchr(symbol_name, '$'); 2060 if (!dollar_pos || dollar_pos[1] == '\0') 2061 return '\0'; 2062 2063 if (dollar_pos[2] == '\0' || dollar_pos[2] == '.') 2064 return dollar_pos[1]; 2065 return '\0'; 2066 } 2067 2068 #define STO_MIPS_ISA (3 << 6) 2069 #define STO_MICROMIPS (2 << 6) 2070 #define IS_MICROMIPS(ST_OTHER) (((ST_OTHER)&STO_MIPS_ISA) == STO_MICROMIPS) 2071 2072 // private 2073 std::pair<unsigned, ObjectFileELF::FileAddressToAddressClassMap> 2074 ObjectFileELF::ParseSymbols(Symtab *symtab, user_id_t start_id, 2075 SectionList *section_list, const size_t num_symbols, 2076 const DataExtractor &symtab_data, 2077 const DataExtractor &strtab_data) { 2078 ELFSymbol symbol; 2079 lldb::offset_t offset = 0; 2080 // The changes these symbols would make to the class map. We will also update 2081 // m_address_class_map but need to tell the caller what changed because the 2082 // caller may be another object file. 2083 FileAddressToAddressClassMap address_class_map; 2084 2085 static ConstString text_section_name(".text"); 2086 static ConstString init_section_name(".init"); 2087 static ConstString fini_section_name(".fini"); 2088 static ConstString ctors_section_name(".ctors"); 2089 static ConstString dtors_section_name(".dtors"); 2090 2091 static ConstString data_section_name(".data"); 2092 static ConstString rodata_section_name(".rodata"); 2093 static ConstString rodata1_section_name(".rodata1"); 2094 static ConstString data2_section_name(".data1"); 2095 static ConstString bss_section_name(".bss"); 2096 static ConstString opd_section_name(".opd"); // For ppc64 2097 2098 // On Android the oatdata and the oatexec symbols in the oat and odex files 2099 // covers the full .text section what causes issues with displaying unusable 2100 // symbol name to the user and very slow unwinding speed because the 2101 // instruction emulation based unwind plans try to emulate all instructions 2102 // in these symbols. Don't add these symbols to the symbol list as they have 2103 // no use for the debugger and they are causing a lot of trouble. Filtering 2104 // can't be restricted to Android because this special object file don't 2105 // contain the note section specifying the environment to Android but the 2106 // custom extension and file name makes it highly unlikely that this will 2107 // collide with anything else. 2108 llvm::StringRef file_extension = m_file.GetFileNameExtension(); 2109 bool skip_oatdata_oatexec = 2110 file_extension == ".oat" || file_extension == ".odex"; 2111 2112 ArchSpec arch = GetArchitecture(); 2113 ModuleSP module_sp(GetModule()); 2114 SectionList *module_section_list = 2115 module_sp ? module_sp->GetSectionList() : nullptr; 2116 2117 // We might have debug information in a separate object, in which case 2118 // we need to map the sections from that object to the sections in the 2119 // main object during symbol lookup. If we had to compare the sections 2120 // for every single symbol, that would be expensive, so this map is 2121 // used to accelerate the process. 2122 std::unordered_map<lldb::SectionSP, lldb::SectionSP> section_map; 2123 2124 unsigned i; 2125 for (i = 0; i < num_symbols; ++i) { 2126 if (!symbol.Parse(symtab_data, &offset)) 2127 break; 2128 2129 const char *symbol_name = strtab_data.PeekCStr(symbol.st_name); 2130 if (!symbol_name) 2131 symbol_name = ""; 2132 2133 // No need to add non-section symbols that have no names 2134 if (symbol.getType() != STT_SECTION && 2135 (symbol_name == nullptr || symbol_name[0] == '\0')) 2136 continue; 2137 2138 // Skipping oatdata and oatexec sections if it is requested. See details 2139 // above the definition of skip_oatdata_oatexec for the reasons. 2140 if (skip_oatdata_oatexec && (::strcmp(symbol_name, "oatdata") == 0 || 2141 ::strcmp(symbol_name, "oatexec") == 0)) 2142 continue; 2143 2144 SectionSP symbol_section_sp; 2145 SymbolType symbol_type = eSymbolTypeInvalid; 2146 Elf64_Half shndx = symbol.st_shndx; 2147 2148 switch (shndx) { 2149 case SHN_ABS: 2150 symbol_type = eSymbolTypeAbsolute; 2151 break; 2152 case SHN_UNDEF: 2153 symbol_type = eSymbolTypeUndefined; 2154 break; 2155 default: 2156 symbol_section_sp = section_list->FindSectionByID(shndx); 2157 break; 2158 } 2159 2160 // If a symbol is undefined do not process it further even if it has a STT 2161 // type 2162 if (symbol_type != eSymbolTypeUndefined) { 2163 switch (symbol.getType()) { 2164 default: 2165 case STT_NOTYPE: 2166 // The symbol's type is not specified. 2167 break; 2168 2169 case STT_OBJECT: 2170 // The symbol is associated with a data object, such as a variable, an 2171 // array, etc. 2172 symbol_type = eSymbolTypeData; 2173 break; 2174 2175 case STT_FUNC: 2176 // The symbol is associated with a function or other executable code. 2177 symbol_type = eSymbolTypeCode; 2178 break; 2179 2180 case STT_SECTION: 2181 // The symbol is associated with a section. Symbol table entries of 2182 // this type exist primarily for relocation and normally have STB_LOCAL 2183 // binding. 2184 break; 2185 2186 case STT_FILE: 2187 // Conventionally, the symbol's name gives the name of the source file 2188 // associated with the object file. A file symbol has STB_LOCAL 2189 // binding, its section index is SHN_ABS, and it precedes the other 2190 // STB_LOCAL symbols for the file, if it is present. 2191 symbol_type = eSymbolTypeSourceFile; 2192 break; 2193 2194 case STT_GNU_IFUNC: 2195 // The symbol is associated with an indirect function. The actual 2196 // function will be resolved if it is referenced. 2197 symbol_type = eSymbolTypeResolver; 2198 break; 2199 } 2200 } 2201 2202 if (symbol_type == eSymbolTypeInvalid && symbol.getType() != STT_SECTION) { 2203 if (symbol_section_sp) { 2204 ConstString sect_name = symbol_section_sp->GetName(); 2205 if (sect_name == text_section_name || sect_name == init_section_name || 2206 sect_name == fini_section_name || sect_name == ctors_section_name || 2207 sect_name == dtors_section_name) { 2208 symbol_type = eSymbolTypeCode; 2209 } else if (sect_name == data_section_name || 2210 sect_name == data2_section_name || 2211 sect_name == rodata_section_name || 2212 sect_name == rodata1_section_name || 2213 sect_name == bss_section_name) { 2214 symbol_type = eSymbolTypeData; 2215 } 2216 } 2217 } 2218 2219 int64_t symbol_value_offset = 0; 2220 uint32_t additional_flags = 0; 2221 2222 if (arch.IsValid()) { 2223 if (arch.GetMachine() == llvm::Triple::arm) { 2224 if (symbol.getBinding() == STB_LOCAL) { 2225 char mapping_symbol = FindArmAarch64MappingSymbol(symbol_name); 2226 if (symbol_type == eSymbolTypeCode) { 2227 switch (mapping_symbol) { 2228 case 'a': 2229 // $a[.<any>]* - marks an ARM instruction sequence 2230 address_class_map[symbol.st_value] = AddressClass::eCode; 2231 break; 2232 case 'b': 2233 case 't': 2234 // $b[.<any>]* - marks a THUMB BL instruction sequence 2235 // $t[.<any>]* - marks a THUMB instruction sequence 2236 address_class_map[symbol.st_value] = 2237 AddressClass::eCodeAlternateISA; 2238 break; 2239 case 'd': 2240 // $d[.<any>]* - marks a data item sequence (e.g. lit pool) 2241 address_class_map[symbol.st_value] = AddressClass::eData; 2242 break; 2243 } 2244 } 2245 if (mapping_symbol) 2246 continue; 2247 } 2248 } else if (arch.GetMachine() == llvm::Triple::aarch64) { 2249 if (symbol.getBinding() == STB_LOCAL) { 2250 char mapping_symbol = FindArmAarch64MappingSymbol(symbol_name); 2251 if (symbol_type == eSymbolTypeCode) { 2252 switch (mapping_symbol) { 2253 case 'x': 2254 // $x[.<any>]* - marks an A64 instruction sequence 2255 address_class_map[symbol.st_value] = AddressClass::eCode; 2256 break; 2257 case 'd': 2258 // $d[.<any>]* - marks a data item sequence (e.g. lit pool) 2259 address_class_map[symbol.st_value] = AddressClass::eData; 2260 break; 2261 } 2262 } 2263 if (mapping_symbol) 2264 continue; 2265 } 2266 } 2267 2268 if (arch.GetMachine() == llvm::Triple::arm) { 2269 if (symbol_type == eSymbolTypeCode) { 2270 if (symbol.st_value & 1) { 2271 // Subtracting 1 from the address effectively unsets the low order 2272 // bit, which results in the address actually pointing to the 2273 // beginning of the symbol. This delta will be used below in 2274 // conjunction with symbol.st_value to produce the final 2275 // symbol_value that we store in the symtab. 2276 symbol_value_offset = -1; 2277 address_class_map[symbol.st_value ^ 1] = 2278 AddressClass::eCodeAlternateISA; 2279 } else { 2280 // This address is ARM 2281 address_class_map[symbol.st_value] = AddressClass::eCode; 2282 } 2283 } 2284 } 2285 2286 /* 2287 * MIPS: 2288 * The bit #0 of an address is used for ISA mode (1 for microMIPS, 0 for 2289 * MIPS). 2290 * This allows processor to switch between microMIPS and MIPS without any 2291 * need 2292 * for special mode-control register. However, apart from .debug_line, 2293 * none of 2294 * the ELF/DWARF sections set the ISA bit (for symbol or section). Use 2295 * st_other 2296 * flag to check whether the symbol is microMIPS and then set the address 2297 * class 2298 * accordingly. 2299 */ 2300 if (arch.IsMIPS()) { 2301 if (IS_MICROMIPS(symbol.st_other)) 2302 address_class_map[symbol.st_value] = AddressClass::eCodeAlternateISA; 2303 else if ((symbol.st_value & 1) && (symbol_type == eSymbolTypeCode)) { 2304 symbol.st_value = symbol.st_value & (~1ull); 2305 address_class_map[symbol.st_value] = AddressClass::eCodeAlternateISA; 2306 } else { 2307 if (symbol_type == eSymbolTypeCode) 2308 address_class_map[symbol.st_value] = AddressClass::eCode; 2309 else if (symbol_type == eSymbolTypeData) 2310 address_class_map[symbol.st_value] = AddressClass::eData; 2311 else 2312 address_class_map[symbol.st_value] = AddressClass::eUnknown; 2313 } 2314 } 2315 } 2316 2317 // symbol_value_offset may contain 0 for ARM symbols or -1 for THUMB 2318 // symbols. See above for more details. 2319 uint64_t symbol_value = symbol.st_value + symbol_value_offset; 2320 2321 if (symbol_section_sp && 2322 CalculateType() != ObjectFile::Type::eTypeObjectFile) 2323 symbol_value -= symbol_section_sp->GetFileAddress(); 2324 2325 if (symbol_section_sp && module_section_list && 2326 module_section_list != section_list) { 2327 auto section_it = section_map.find(symbol_section_sp); 2328 if (section_it == section_map.end()) { 2329 section_it = section_map 2330 .emplace(symbol_section_sp, 2331 FindMatchingSection(*module_section_list, 2332 symbol_section_sp)) 2333 .first; 2334 } 2335 if (section_it->second) 2336 symbol_section_sp = section_it->second; 2337 } 2338 2339 bool is_global = symbol.getBinding() == STB_GLOBAL; 2340 uint32_t flags = symbol.st_other << 8 | symbol.st_info | additional_flags; 2341 llvm::StringRef symbol_ref(symbol_name); 2342 2343 // Symbol names may contain @VERSION suffixes. Find those and strip them 2344 // temporarily. 2345 size_t version_pos = symbol_ref.find('@'); 2346 bool has_suffix = version_pos != llvm::StringRef::npos; 2347 llvm::StringRef symbol_bare = symbol_ref.substr(0, version_pos); 2348 Mangled mangled(symbol_bare); 2349 2350 // Now append the suffix back to mangled and unmangled names. Only do it if 2351 // the demangling was successful (string is not empty). 2352 if (has_suffix) { 2353 llvm::StringRef suffix = symbol_ref.substr(version_pos); 2354 2355 llvm::StringRef mangled_name = mangled.GetMangledName().GetStringRef(); 2356 if (!mangled_name.empty()) 2357 mangled.SetMangledName(ConstString((mangled_name + suffix).str())); 2358 2359 ConstString demangled = mangled.GetDemangledName(); 2360 llvm::StringRef demangled_name = demangled.GetStringRef(); 2361 if (!demangled_name.empty()) 2362 mangled.SetDemangledName(ConstString((demangled_name + suffix).str())); 2363 } 2364 2365 // In ELF all symbol should have a valid size but it is not true for some 2366 // function symbols coming from hand written assembly. As none of the 2367 // function symbol should have 0 size we try to calculate the size for 2368 // these symbols in the symtab with saying that their original size is not 2369 // valid. 2370 bool symbol_size_valid = 2371 symbol.st_size != 0 || symbol.getType() != STT_FUNC; 2372 2373 bool is_trampoline = false; 2374 if (arch.IsValid() && (arch.GetMachine() == llvm::Triple::aarch64)) { 2375 // On AArch64, trampolines are registered as code. 2376 // If we detect a trampoline (which starts with __AArch64ADRPThunk_ or 2377 // __AArch64AbsLongThunk_) we register the symbol as a trampoline. This 2378 // way we will be able to detect the trampoline when we step in a function 2379 // and step through the trampoline. 2380 if (symbol_type == eSymbolTypeCode) { 2381 llvm::StringRef trampoline_name = mangled.GetName().GetStringRef(); 2382 if (trampoline_name.starts_with("__AArch64ADRPThunk_") || 2383 trampoline_name.starts_with("__AArch64AbsLongThunk_")) { 2384 symbol_type = eSymbolTypeTrampoline; 2385 is_trampoline = true; 2386 } 2387 } 2388 } 2389 2390 Symbol dc_symbol( 2391 i + start_id, // ID is the original symbol table index. 2392 mangled, 2393 symbol_type, // Type of this symbol 2394 is_global, // Is this globally visible? 2395 false, // Is this symbol debug info? 2396 is_trampoline, // Is this symbol a trampoline? 2397 false, // Is this symbol artificial? 2398 AddressRange(symbol_section_sp, // Section in which this symbol is 2399 // defined or null. 2400 symbol_value, // Offset in section or symbol value. 2401 symbol.st_size), // Size in bytes of this symbol. 2402 symbol_size_valid, // Symbol size is valid 2403 has_suffix, // Contains linker annotations? 2404 flags); // Symbol flags. 2405 if (symbol.getBinding() == STB_WEAK) 2406 dc_symbol.SetIsWeak(true); 2407 symtab->AddSymbol(dc_symbol); 2408 } 2409 2410 m_address_class_map.merge(address_class_map); 2411 return {i, address_class_map}; 2412 } 2413 2414 std::pair<unsigned, ObjectFileELF::FileAddressToAddressClassMap> 2415 ObjectFileELF::ParseSymbolTable(Symtab *symbol_table, user_id_t start_id, 2416 lldb_private::Section *symtab) { 2417 if (symtab->GetObjectFile() != this) { 2418 // If the symbol table section is owned by a different object file, have it 2419 // do the parsing. 2420 ObjectFileELF *obj_file_elf = 2421 static_cast<ObjectFileELF *>(symtab->GetObjectFile()); 2422 auto [num_symbols, address_class_map] = 2423 obj_file_elf->ParseSymbolTable(symbol_table, start_id, symtab); 2424 2425 // The other object file returned the changes it made to its address 2426 // class map, make the same changes to ours. 2427 m_address_class_map.merge(address_class_map); 2428 2429 return {num_symbols, address_class_map}; 2430 } 2431 2432 // Get section list for this object file. 2433 SectionList *section_list = m_sections_up.get(); 2434 if (!section_list) 2435 return {}; 2436 2437 user_id_t symtab_id = symtab->GetID(); 2438 const ELFSectionHeaderInfo *symtab_hdr = GetSectionHeaderByIndex(symtab_id); 2439 assert(symtab_hdr->sh_type == SHT_SYMTAB || 2440 symtab_hdr->sh_type == SHT_DYNSYM); 2441 2442 // sh_link: section header index of associated string table. 2443 user_id_t strtab_id = symtab_hdr->sh_link; 2444 Section *strtab = section_list->FindSectionByID(strtab_id).get(); 2445 2446 if (symtab && strtab) { 2447 assert(symtab->GetObjectFile() == this); 2448 assert(strtab->GetObjectFile() == this); 2449 2450 DataExtractor symtab_data; 2451 DataExtractor strtab_data; 2452 if (ReadSectionData(symtab, symtab_data) && 2453 ReadSectionData(strtab, strtab_data)) { 2454 size_t num_symbols = symtab_data.GetByteSize() / symtab_hdr->sh_entsize; 2455 2456 return ParseSymbols(symbol_table, start_id, section_list, num_symbols, 2457 symtab_data, strtab_data); 2458 } 2459 } 2460 2461 return {0, {}}; 2462 } 2463 2464 size_t ObjectFileELF::ParseDynamicSymbols() { 2465 if (m_dynamic_symbols.size()) 2466 return m_dynamic_symbols.size(); 2467 2468 std::optional<DataExtractor> dynamic_data = GetDynamicData(); 2469 if (!dynamic_data) 2470 return 0; 2471 2472 ELFDynamicWithName e; 2473 lldb::offset_t cursor = 0; 2474 while (e.symbol.Parse(*dynamic_data, &cursor)) { 2475 m_dynamic_symbols.push_back(e); 2476 if (e.symbol.d_tag == DT_NULL) 2477 break; 2478 } 2479 if (std::optional<DataExtractor> dynstr_data = GetDynstrData()) { 2480 for (ELFDynamicWithName &entry : m_dynamic_symbols) { 2481 switch (entry.symbol.d_tag) { 2482 case DT_NEEDED: 2483 case DT_SONAME: 2484 case DT_RPATH: 2485 case DT_RUNPATH: 2486 case DT_AUXILIARY: 2487 case DT_FILTER: { 2488 lldb::offset_t cursor = entry.symbol.d_val; 2489 const char *name = dynstr_data->GetCStr(&cursor); 2490 if (name) 2491 entry.name = std::string(name); 2492 break; 2493 } 2494 default: 2495 break; 2496 } 2497 } 2498 } 2499 return m_dynamic_symbols.size(); 2500 } 2501 2502 const ELFDynamic *ObjectFileELF::FindDynamicSymbol(unsigned tag) { 2503 if (!ParseDynamicSymbols()) 2504 return nullptr; 2505 for (const auto &entry : m_dynamic_symbols) { 2506 if (entry.symbol.d_tag == tag) 2507 return &entry.symbol; 2508 } 2509 return nullptr; 2510 } 2511 2512 unsigned ObjectFileELF::PLTRelocationType() { 2513 // DT_PLTREL 2514 // This member specifies the type of relocation entry to which the 2515 // procedure linkage table refers. The d_val member holds DT_REL or 2516 // DT_RELA, as appropriate. All relocations in a procedure linkage table 2517 // must use the same relocation. 2518 const ELFDynamic *symbol = FindDynamicSymbol(DT_PLTREL); 2519 2520 if (symbol) 2521 return symbol->d_val; 2522 2523 return 0; 2524 } 2525 2526 // Returns the size of the normal plt entries and the offset of the first 2527 // normal plt entry. The 0th entry in the plt table is usually a resolution 2528 // entry which have different size in some architectures then the rest of the 2529 // plt entries. 2530 static std::pair<uint64_t, uint64_t> 2531 GetPltEntrySizeAndOffset(const ELFSectionHeader *rel_hdr, 2532 const ELFSectionHeader *plt_hdr) { 2533 const elf_xword num_relocations = rel_hdr->sh_size / rel_hdr->sh_entsize; 2534 2535 // Clang 3.3 sets entsize to 4 for 32-bit binaries, but the plt entries are 2536 // 16 bytes. So round the entsize up by the alignment if addralign is set. 2537 elf_xword plt_entsize = 2538 plt_hdr->sh_addralign 2539 ? llvm::alignTo(plt_hdr->sh_entsize, plt_hdr->sh_addralign) 2540 : plt_hdr->sh_entsize; 2541 2542 // Some linkers e.g ld for arm, fill plt_hdr->sh_entsize field incorrectly. 2543 // PLT entries relocation code in general requires multiple instruction and 2544 // should be greater than 4 bytes in most cases. Try to guess correct size 2545 // just in case. 2546 if (plt_entsize <= 4) { 2547 // The linker haven't set the plt_hdr->sh_entsize field. Try to guess the 2548 // size of the plt entries based on the number of entries and the size of 2549 // the plt section with the assumption that the size of the 0th entry is at 2550 // least as big as the size of the normal entries and it isn't much bigger 2551 // then that. 2552 if (plt_hdr->sh_addralign) 2553 plt_entsize = plt_hdr->sh_size / plt_hdr->sh_addralign / 2554 (num_relocations + 1) * plt_hdr->sh_addralign; 2555 else 2556 plt_entsize = plt_hdr->sh_size / (num_relocations + 1); 2557 } 2558 2559 elf_xword plt_offset = plt_hdr->sh_size - num_relocations * plt_entsize; 2560 2561 return std::make_pair(plt_entsize, plt_offset); 2562 } 2563 2564 static unsigned ParsePLTRelocations( 2565 Symtab *symbol_table, user_id_t start_id, unsigned rel_type, 2566 const ELFHeader *hdr, const ELFSectionHeader *rel_hdr, 2567 const ELFSectionHeader *plt_hdr, const ELFSectionHeader *sym_hdr, 2568 const lldb::SectionSP &plt_section_sp, DataExtractor &rel_data, 2569 DataExtractor &symtab_data, DataExtractor &strtab_data) { 2570 ELFRelocation rel(rel_type); 2571 ELFSymbol symbol; 2572 lldb::offset_t offset = 0; 2573 2574 uint64_t plt_offset, plt_entsize; 2575 std::tie(plt_entsize, plt_offset) = 2576 GetPltEntrySizeAndOffset(rel_hdr, plt_hdr); 2577 const elf_xword num_relocations = rel_hdr->sh_size / rel_hdr->sh_entsize; 2578 2579 typedef unsigned (*reloc_info_fn)(const ELFRelocation &rel); 2580 reloc_info_fn reloc_type; 2581 reloc_info_fn reloc_symbol; 2582 2583 if (hdr->Is32Bit()) { 2584 reloc_type = ELFRelocation::RelocType32; 2585 reloc_symbol = ELFRelocation::RelocSymbol32; 2586 } else { 2587 reloc_type = ELFRelocation::RelocType64; 2588 reloc_symbol = ELFRelocation::RelocSymbol64; 2589 } 2590 2591 unsigned slot_type = hdr->GetRelocationJumpSlotType(); 2592 unsigned i; 2593 for (i = 0; i < num_relocations; ++i) { 2594 if (!rel.Parse(rel_data, &offset)) 2595 break; 2596 2597 if (reloc_type(rel) != slot_type) 2598 continue; 2599 2600 lldb::offset_t symbol_offset = reloc_symbol(rel) * sym_hdr->sh_entsize; 2601 if (!symbol.Parse(symtab_data, &symbol_offset)) 2602 break; 2603 2604 const char *symbol_name = strtab_data.PeekCStr(symbol.st_name); 2605 uint64_t plt_index = plt_offset + i * plt_entsize; 2606 2607 Symbol jump_symbol( 2608 i + start_id, // Symbol table index 2609 symbol_name, // symbol name. 2610 eSymbolTypeTrampoline, // Type of this symbol 2611 false, // Is this globally visible? 2612 false, // Is this symbol debug info? 2613 true, // Is this symbol a trampoline? 2614 true, // Is this symbol artificial? 2615 plt_section_sp, // Section in which this symbol is defined or null. 2616 plt_index, // Offset in section or symbol value. 2617 plt_entsize, // Size in bytes of this symbol. 2618 true, // Size is valid 2619 false, // Contains linker annotations? 2620 0); // Symbol flags. 2621 2622 symbol_table->AddSymbol(jump_symbol); 2623 } 2624 2625 return i; 2626 } 2627 2628 unsigned 2629 ObjectFileELF::ParseTrampolineSymbols(Symtab *symbol_table, user_id_t start_id, 2630 const ELFSectionHeaderInfo *rel_hdr, 2631 user_id_t rel_id) { 2632 assert(rel_hdr->sh_type == SHT_RELA || rel_hdr->sh_type == SHT_REL); 2633 2634 // The link field points to the associated symbol table. 2635 user_id_t symtab_id = rel_hdr->sh_link; 2636 2637 // If the link field doesn't point to the appropriate symbol name table then 2638 // try to find it by name as some compiler don't fill in the link fields. 2639 if (!symtab_id) 2640 symtab_id = GetSectionIndexByName(".dynsym"); 2641 2642 // Get PLT section. We cannot use rel_hdr->sh_info, since current linkers 2643 // point that to the .got.plt or .got section instead of .plt. 2644 user_id_t plt_id = GetSectionIndexByName(".plt"); 2645 2646 if (!symtab_id || !plt_id) 2647 return 0; 2648 2649 const ELFSectionHeaderInfo *plt_hdr = GetSectionHeaderByIndex(plt_id); 2650 if (!plt_hdr) 2651 return 0; 2652 2653 const ELFSectionHeaderInfo *sym_hdr = GetSectionHeaderByIndex(symtab_id); 2654 if (!sym_hdr) 2655 return 0; 2656 2657 SectionList *section_list = m_sections_up.get(); 2658 if (!section_list) 2659 return 0; 2660 2661 Section *rel_section = section_list->FindSectionByID(rel_id).get(); 2662 if (!rel_section) 2663 return 0; 2664 2665 SectionSP plt_section_sp(section_list->FindSectionByID(plt_id)); 2666 if (!plt_section_sp) 2667 return 0; 2668 2669 Section *symtab = section_list->FindSectionByID(symtab_id).get(); 2670 if (!symtab) 2671 return 0; 2672 2673 // sh_link points to associated string table. 2674 Section *strtab = section_list->FindSectionByID(sym_hdr->sh_link).get(); 2675 if (!strtab) 2676 return 0; 2677 2678 DataExtractor rel_data; 2679 if (!ReadSectionData(rel_section, rel_data)) 2680 return 0; 2681 2682 DataExtractor symtab_data; 2683 if (!ReadSectionData(symtab, symtab_data)) 2684 return 0; 2685 2686 DataExtractor strtab_data; 2687 if (!ReadSectionData(strtab, strtab_data)) 2688 return 0; 2689 2690 unsigned rel_type = PLTRelocationType(); 2691 if (!rel_type) 2692 return 0; 2693 2694 return ParsePLTRelocations(symbol_table, start_id, rel_type, &m_header, 2695 rel_hdr, plt_hdr, sym_hdr, plt_section_sp, 2696 rel_data, symtab_data, strtab_data); 2697 } 2698 2699 static void ApplyELF64ABS64Relocation(Symtab *symtab, ELFRelocation &rel, 2700 DataExtractor &debug_data, 2701 Section *rel_section) { 2702 Symbol *symbol = symtab->FindSymbolByID(ELFRelocation::RelocSymbol64(rel)); 2703 if (symbol) { 2704 addr_t value = symbol->GetAddressRef().GetFileAddress(); 2705 DataBufferSP &data_buffer_sp = debug_data.GetSharedDataBuffer(); 2706 // ObjectFileELF creates a WritableDataBuffer in CreateInstance. 2707 WritableDataBuffer *data_buffer = 2708 llvm::cast<WritableDataBuffer>(data_buffer_sp.get()); 2709 uint64_t *dst = reinterpret_cast<uint64_t *>( 2710 data_buffer->GetBytes() + rel_section->GetFileOffset() + 2711 ELFRelocation::RelocOffset64(rel)); 2712 uint64_t val_offset = value + ELFRelocation::RelocAddend64(rel); 2713 memcpy(dst, &val_offset, sizeof(uint64_t)); 2714 } 2715 } 2716 2717 static void ApplyELF64ABS32Relocation(Symtab *symtab, ELFRelocation &rel, 2718 DataExtractor &debug_data, 2719 Section *rel_section, bool is_signed) { 2720 Symbol *symbol = symtab->FindSymbolByID(ELFRelocation::RelocSymbol64(rel)); 2721 if (symbol) { 2722 addr_t value = symbol->GetAddressRef().GetFileAddress(); 2723 value += ELFRelocation::RelocAddend32(rel); 2724 if ((!is_signed && (value > UINT32_MAX)) || 2725 (is_signed && 2726 ((int64_t)value > INT32_MAX || (int64_t)value < INT32_MIN))) { 2727 Log *log = GetLog(LLDBLog::Modules); 2728 LLDB_LOGF(log, "Failed to apply debug info relocations"); 2729 return; 2730 } 2731 uint32_t truncated_addr = (value & 0xFFFFFFFF); 2732 DataBufferSP &data_buffer_sp = debug_data.GetSharedDataBuffer(); 2733 // ObjectFileELF creates a WritableDataBuffer in CreateInstance. 2734 WritableDataBuffer *data_buffer = 2735 llvm::cast<WritableDataBuffer>(data_buffer_sp.get()); 2736 uint32_t *dst = reinterpret_cast<uint32_t *>( 2737 data_buffer->GetBytes() + rel_section->GetFileOffset() + 2738 ELFRelocation::RelocOffset32(rel)); 2739 memcpy(dst, &truncated_addr, sizeof(uint32_t)); 2740 } 2741 } 2742 2743 static void ApplyELF32ABS32RelRelocation(Symtab *symtab, ELFRelocation &rel, 2744 DataExtractor &debug_data, 2745 Section *rel_section) { 2746 Log *log = GetLog(LLDBLog::Modules); 2747 Symbol *symbol = symtab->FindSymbolByID(ELFRelocation::RelocSymbol32(rel)); 2748 if (symbol) { 2749 addr_t value = symbol->GetAddressRef().GetFileAddress(); 2750 if (value == LLDB_INVALID_ADDRESS) { 2751 const char *name = symbol->GetName().GetCString(); 2752 LLDB_LOGF(log, "Debug info symbol invalid: %s", name); 2753 return; 2754 } 2755 assert(llvm::isUInt<32>(value) && "Valid addresses are 32-bit"); 2756 DataBufferSP &data_buffer_sp = debug_data.GetSharedDataBuffer(); 2757 // ObjectFileELF creates a WritableDataBuffer in CreateInstance. 2758 WritableDataBuffer *data_buffer = 2759 llvm::cast<WritableDataBuffer>(data_buffer_sp.get()); 2760 uint8_t *dst = data_buffer->GetBytes() + rel_section->GetFileOffset() + 2761 ELFRelocation::RelocOffset32(rel); 2762 // Implicit addend is stored inline as a signed value. 2763 int32_t addend; 2764 memcpy(&addend, dst, sizeof(int32_t)); 2765 // The sum must be positive. This extra check prevents UB from overflow in 2766 // the actual range check below. 2767 if (addend < 0 && static_cast<uint32_t>(-addend) > value) { 2768 LLDB_LOGF(log, "Debug info relocation overflow: 0x%" PRIx64, 2769 static_cast<int64_t>(value) + addend); 2770 return; 2771 } 2772 if (!llvm::isUInt<32>(value + addend)) { 2773 LLDB_LOGF(log, "Debug info relocation out of range: 0x%" PRIx64, value); 2774 return; 2775 } 2776 uint32_t addr = value + addend; 2777 memcpy(dst, &addr, sizeof(uint32_t)); 2778 } 2779 } 2780 2781 unsigned ObjectFileELF::ApplyRelocations( 2782 Symtab *symtab, const ELFHeader *hdr, const ELFSectionHeader *rel_hdr, 2783 const ELFSectionHeader *symtab_hdr, const ELFSectionHeader *debug_hdr, 2784 DataExtractor &rel_data, DataExtractor &symtab_data, 2785 DataExtractor &debug_data, Section *rel_section) { 2786 ELFRelocation rel(rel_hdr->sh_type); 2787 lldb::addr_t offset = 0; 2788 const unsigned num_relocations = rel_hdr->sh_size / rel_hdr->sh_entsize; 2789 typedef unsigned (*reloc_info_fn)(const ELFRelocation &rel); 2790 reloc_info_fn reloc_type; 2791 reloc_info_fn reloc_symbol; 2792 2793 if (hdr->Is32Bit()) { 2794 reloc_type = ELFRelocation::RelocType32; 2795 reloc_symbol = ELFRelocation::RelocSymbol32; 2796 } else { 2797 reloc_type = ELFRelocation::RelocType64; 2798 reloc_symbol = ELFRelocation::RelocSymbol64; 2799 } 2800 2801 for (unsigned i = 0; i < num_relocations; ++i) { 2802 if (!rel.Parse(rel_data, &offset)) { 2803 GetModule()->ReportError(".rel{0}[{1:d}] failed to parse relocation", 2804 rel_section->GetName().AsCString(), i); 2805 break; 2806 } 2807 Symbol *symbol = nullptr; 2808 2809 if (hdr->Is32Bit()) { 2810 switch (hdr->e_machine) { 2811 case llvm::ELF::EM_ARM: 2812 switch (reloc_type(rel)) { 2813 case R_ARM_ABS32: 2814 ApplyELF32ABS32RelRelocation(symtab, rel, debug_data, rel_section); 2815 break; 2816 case R_ARM_REL32: 2817 GetModule()->ReportError("unsupported AArch32 relocation:" 2818 " .rel{0}[{1}], type {2}", 2819 rel_section->GetName().AsCString(), i, 2820 reloc_type(rel)); 2821 break; 2822 default: 2823 assert(false && "unexpected relocation type"); 2824 } 2825 break; 2826 case llvm::ELF::EM_386: 2827 switch (reloc_type(rel)) { 2828 case R_386_32: 2829 symbol = symtab->FindSymbolByID(reloc_symbol(rel)); 2830 if (symbol) { 2831 addr_t f_offset = 2832 rel_section->GetFileOffset() + ELFRelocation::RelocOffset32(rel); 2833 DataBufferSP &data_buffer_sp = debug_data.GetSharedDataBuffer(); 2834 // ObjectFileELF creates a WritableDataBuffer in CreateInstance. 2835 WritableDataBuffer *data_buffer = 2836 llvm::cast<WritableDataBuffer>(data_buffer_sp.get()); 2837 uint32_t *dst = reinterpret_cast<uint32_t *>( 2838 data_buffer->GetBytes() + f_offset); 2839 2840 addr_t value = symbol->GetAddressRef().GetFileAddress(); 2841 if (rel.IsRela()) { 2842 value += ELFRelocation::RelocAddend32(rel); 2843 } else { 2844 value += *dst; 2845 } 2846 *dst = value; 2847 } else { 2848 GetModule()->ReportError(".rel{0}[{1}] unknown symbol id: {2:d}", 2849 rel_section->GetName().AsCString(), i, 2850 reloc_symbol(rel)); 2851 } 2852 break; 2853 case R_386_NONE: 2854 case R_386_PC32: 2855 GetModule()->ReportError("unsupported i386 relocation:" 2856 " .rel{0}[{1}], type {2}", 2857 rel_section->GetName().AsCString(), i, 2858 reloc_type(rel)); 2859 break; 2860 default: 2861 assert(false && "unexpected relocation type"); 2862 break; 2863 } 2864 break; 2865 default: 2866 GetModule()->ReportError("unsupported 32-bit ELF machine arch: {0}", hdr->e_machine); 2867 break; 2868 } 2869 } else { 2870 switch (hdr->e_machine) { 2871 case llvm::ELF::EM_AARCH64: 2872 switch (reloc_type(rel)) { 2873 case R_AARCH64_ABS64: 2874 ApplyELF64ABS64Relocation(symtab, rel, debug_data, rel_section); 2875 break; 2876 case R_AARCH64_ABS32: 2877 ApplyELF64ABS32Relocation(symtab, rel, debug_data, rel_section, true); 2878 break; 2879 default: 2880 assert(false && "unexpected relocation type"); 2881 } 2882 break; 2883 case llvm::ELF::EM_LOONGARCH: 2884 switch (reloc_type(rel)) { 2885 case R_LARCH_64: 2886 ApplyELF64ABS64Relocation(symtab, rel, debug_data, rel_section); 2887 break; 2888 case R_LARCH_32: 2889 ApplyELF64ABS32Relocation(symtab, rel, debug_data, rel_section, true); 2890 break; 2891 default: 2892 assert(false && "unexpected relocation type"); 2893 } 2894 break; 2895 case llvm::ELF::EM_X86_64: 2896 switch (reloc_type(rel)) { 2897 case R_X86_64_64: 2898 ApplyELF64ABS64Relocation(symtab, rel, debug_data, rel_section); 2899 break; 2900 case R_X86_64_32: 2901 ApplyELF64ABS32Relocation(symtab, rel, debug_data, rel_section, 2902 false); 2903 break; 2904 case R_X86_64_32S: 2905 ApplyELF64ABS32Relocation(symtab, rel, debug_data, rel_section, true); 2906 break; 2907 case R_X86_64_PC32: 2908 default: 2909 assert(false && "unexpected relocation type"); 2910 } 2911 break; 2912 default: 2913 GetModule()->ReportError("unsupported 64-bit ELF machine arch: {0}", hdr->e_machine); 2914 break; 2915 } 2916 } 2917 } 2918 2919 return 0; 2920 } 2921 2922 unsigned ObjectFileELF::RelocateDebugSections(const ELFSectionHeader *rel_hdr, 2923 user_id_t rel_id, 2924 lldb_private::Symtab *thetab) { 2925 assert(rel_hdr->sh_type == SHT_RELA || rel_hdr->sh_type == SHT_REL); 2926 2927 // Parse in the section list if needed. 2928 SectionList *section_list = GetSectionList(); 2929 if (!section_list) 2930 return 0; 2931 2932 user_id_t symtab_id = rel_hdr->sh_link; 2933 user_id_t debug_id = rel_hdr->sh_info; 2934 2935 const ELFSectionHeader *symtab_hdr = GetSectionHeaderByIndex(symtab_id); 2936 if (!symtab_hdr) 2937 return 0; 2938 2939 const ELFSectionHeader *debug_hdr = GetSectionHeaderByIndex(debug_id); 2940 if (!debug_hdr) 2941 return 0; 2942 2943 Section *rel = section_list->FindSectionByID(rel_id).get(); 2944 if (!rel) 2945 return 0; 2946 2947 Section *symtab = section_list->FindSectionByID(symtab_id).get(); 2948 if (!symtab) 2949 return 0; 2950 2951 Section *debug = section_list->FindSectionByID(debug_id).get(); 2952 if (!debug) 2953 return 0; 2954 2955 DataExtractor rel_data; 2956 DataExtractor symtab_data; 2957 DataExtractor debug_data; 2958 2959 if (GetData(rel->GetFileOffset(), rel->GetFileSize(), rel_data) && 2960 GetData(symtab->GetFileOffset(), symtab->GetFileSize(), symtab_data) && 2961 GetData(debug->GetFileOffset(), debug->GetFileSize(), debug_data)) { 2962 ApplyRelocations(thetab, &m_header, rel_hdr, symtab_hdr, debug_hdr, 2963 rel_data, symtab_data, debug_data, debug); 2964 } 2965 2966 return 0; 2967 } 2968 2969 void ObjectFileELF::ParseSymtab(Symtab &lldb_symtab) { 2970 ModuleSP module_sp(GetModule()); 2971 if (!module_sp) 2972 return; 2973 2974 Progress progress("Parsing symbol table", 2975 m_file.GetFilename().AsCString("<Unknown>")); 2976 ElapsedTime elapsed(module_sp->GetSymtabParseTime()); 2977 2978 // We always want to use the main object file so we (hopefully) only have one 2979 // cached copy of our symtab, dynamic sections, etc. 2980 ObjectFile *module_obj_file = module_sp->GetObjectFile(); 2981 if (module_obj_file && module_obj_file != this) 2982 return module_obj_file->ParseSymtab(lldb_symtab); 2983 2984 SectionList *section_list = module_sp->GetSectionList(); 2985 if (!section_list) 2986 return; 2987 2988 uint64_t symbol_id = 0; 2989 2990 // Sharable objects and dynamic executables usually have 2 distinct symbol 2991 // tables, one named ".symtab", and the other ".dynsym". The dynsym is a 2992 // smaller version of the symtab that only contains global symbols. The 2993 // information found in the dynsym is therefore also found in the symtab, 2994 // while the reverse is not necessarily true. 2995 Section *symtab = 2996 section_list->FindSectionByType(eSectionTypeELFSymbolTable, true).get(); 2997 if (symtab) { 2998 auto [num_symbols, address_class_map] = 2999 ParseSymbolTable(&lldb_symtab, symbol_id, symtab); 3000 m_address_class_map.merge(address_class_map); 3001 symbol_id += num_symbols; 3002 } 3003 3004 // The symtab section is non-allocable and can be stripped, while the 3005 // .dynsym section which should always be always be there. To support the 3006 // minidebuginfo case we parse .dynsym when there's a .gnu_debuginfo 3007 // section, nomatter if .symtab was already parsed or not. This is because 3008 // minidebuginfo normally removes the .symtab symbols which have their 3009 // matching .dynsym counterparts. 3010 if (!symtab || 3011 GetSectionList()->FindSectionByName(ConstString(".gnu_debugdata"))) { 3012 Section *dynsym = 3013 section_list->FindSectionByType(eSectionTypeELFDynamicSymbols, true) 3014 .get(); 3015 if (dynsym) { 3016 auto [num_symbols, address_class_map] = 3017 ParseSymbolTable(&lldb_symtab, symbol_id, dynsym); 3018 symbol_id += num_symbols; 3019 m_address_class_map.merge(address_class_map); 3020 } 3021 } 3022 3023 // DT_JMPREL 3024 // If present, this entry's d_ptr member holds the address of 3025 // relocation 3026 // entries associated solely with the procedure linkage table. 3027 // Separating 3028 // these relocation entries lets the dynamic linker ignore them during 3029 // process initialization, if lazy binding is enabled. If this entry is 3030 // present, the related entries of types DT_PLTRELSZ and DT_PLTREL must 3031 // also be present. 3032 const ELFDynamic *symbol = FindDynamicSymbol(DT_JMPREL); 3033 if (symbol) { 3034 // Synthesize trampoline symbols to help navigate the PLT. 3035 addr_t addr = symbol->d_ptr; 3036 Section *reloc_section = 3037 section_list->FindSectionContainingFileAddress(addr).get(); 3038 if (reloc_section) { 3039 user_id_t reloc_id = reloc_section->GetID(); 3040 const ELFSectionHeaderInfo *reloc_header = 3041 GetSectionHeaderByIndex(reloc_id); 3042 if (reloc_header) 3043 ParseTrampolineSymbols(&lldb_symtab, symbol_id, reloc_header, reloc_id); 3044 } 3045 } 3046 3047 if (DWARFCallFrameInfo *eh_frame = 3048 GetModule()->GetUnwindTable().GetEHFrameInfo()) { 3049 ParseUnwindSymbols(&lldb_symtab, eh_frame); 3050 } 3051 3052 // In the event that there's no symbol entry for the entry point we'll 3053 // artificially create one. We delegate to the symtab object the figuring 3054 // out of the proper size, this will usually make it span til the next 3055 // symbol it finds in the section. This means that if there are missing 3056 // symbols the entry point might span beyond its function definition. 3057 // We're fine with this as it doesn't make it worse than not having a 3058 // symbol entry at all. 3059 if (CalculateType() == eTypeExecutable) { 3060 ArchSpec arch = GetArchitecture(); 3061 auto entry_point_addr = GetEntryPointAddress(); 3062 bool is_valid_entry_point = 3063 entry_point_addr.IsValid() && entry_point_addr.IsSectionOffset(); 3064 addr_t entry_point_file_addr = entry_point_addr.GetFileAddress(); 3065 if (is_valid_entry_point && !lldb_symtab.FindSymbolContainingFileAddress( 3066 entry_point_file_addr)) { 3067 uint64_t symbol_id = lldb_symtab.GetNumSymbols(); 3068 // Don't set the name for any synthetic symbols, the Symbol 3069 // object will generate one if needed when the name is accessed 3070 // via accessors. 3071 SectionSP section_sp = entry_point_addr.GetSection(); 3072 Symbol symbol( 3073 /*symID=*/symbol_id, 3074 /*name=*/llvm::StringRef(), // Name will be auto generated. 3075 /*type=*/eSymbolTypeCode, 3076 /*external=*/true, 3077 /*is_debug=*/false, 3078 /*is_trampoline=*/false, 3079 /*is_artificial=*/true, 3080 /*section_sp=*/section_sp, 3081 /*offset=*/0, 3082 /*size=*/0, // FDE can span multiple symbols so don't use its size. 3083 /*size_is_valid=*/false, 3084 /*contains_linker_annotations=*/false, 3085 /*flags=*/0); 3086 // When the entry point is arm thumb we need to explicitly set its 3087 // class address to reflect that. This is important because expression 3088 // evaluation relies on correctly setting a breakpoint at this 3089 // address. 3090 if (arch.GetMachine() == llvm::Triple::arm && 3091 (entry_point_file_addr & 1)) { 3092 symbol.GetAddressRef().SetOffset(entry_point_addr.GetOffset() ^ 1); 3093 m_address_class_map[entry_point_file_addr ^ 1] = 3094 AddressClass::eCodeAlternateISA; 3095 } else { 3096 m_address_class_map[entry_point_file_addr] = AddressClass::eCode; 3097 } 3098 lldb_symtab.AddSymbol(symbol); 3099 } 3100 } 3101 } 3102 3103 void ObjectFileELF::RelocateSection(lldb_private::Section *section) 3104 { 3105 static const char *debug_prefix = ".debug"; 3106 3107 // Set relocated bit so we stop getting called, regardless of whether we 3108 // actually relocate. 3109 section->SetIsRelocated(true); 3110 3111 // We only relocate in ELF relocatable files 3112 if (CalculateType() != eTypeObjectFile) 3113 return; 3114 3115 const char *section_name = section->GetName().GetCString(); 3116 // Can't relocate that which can't be named 3117 if (section_name == nullptr) 3118 return; 3119 3120 // We don't relocate non-debug sections at the moment 3121 if (strncmp(section_name, debug_prefix, strlen(debug_prefix))) 3122 return; 3123 3124 // Relocation section names to look for 3125 std::string needle = std::string(".rel") + section_name; 3126 std::string needlea = std::string(".rela") + section_name; 3127 3128 for (SectionHeaderCollIter I = m_section_headers.begin(); 3129 I != m_section_headers.end(); ++I) { 3130 if (I->sh_type == SHT_RELA || I->sh_type == SHT_REL) { 3131 const char *hay_name = I->section_name.GetCString(); 3132 if (hay_name == nullptr) 3133 continue; 3134 if (needle == hay_name || needlea == hay_name) { 3135 const ELFSectionHeader &reloc_header = *I; 3136 user_id_t reloc_id = SectionIndex(I); 3137 RelocateDebugSections(&reloc_header, reloc_id, GetSymtab()); 3138 break; 3139 } 3140 } 3141 } 3142 } 3143 3144 void ObjectFileELF::ParseUnwindSymbols(Symtab *symbol_table, 3145 DWARFCallFrameInfo *eh_frame) { 3146 SectionList *section_list = GetSectionList(); 3147 if (!section_list) 3148 return; 3149 3150 // First we save the new symbols into a separate list and add them to the 3151 // symbol table after we collected all symbols we want to add. This is 3152 // neccessary because adding a new symbol invalidates the internal index of 3153 // the symtab what causing the next lookup to be slow because it have to 3154 // recalculate the index first. 3155 std::vector<Symbol> new_symbols; 3156 3157 size_t num_symbols = symbol_table->GetNumSymbols(); 3158 uint64_t last_symbol_id = 3159 num_symbols ? symbol_table->SymbolAtIndex(num_symbols - 1)->GetID() : 0; 3160 eh_frame->ForEachFDEEntries([&](lldb::addr_t file_addr, uint32_t size, 3161 dw_offset_t) { 3162 Symbol *symbol = symbol_table->FindSymbolAtFileAddress(file_addr); 3163 if (symbol) { 3164 if (!symbol->GetByteSizeIsValid()) { 3165 symbol->SetByteSize(size); 3166 symbol->SetSizeIsSynthesized(true); 3167 } 3168 } else { 3169 SectionSP section_sp = 3170 section_list->FindSectionContainingFileAddress(file_addr); 3171 if (section_sp) { 3172 addr_t offset = file_addr - section_sp->GetFileAddress(); 3173 uint64_t symbol_id = ++last_symbol_id; 3174 // Don't set the name for any synthetic symbols, the Symbol 3175 // object will generate one if needed when the name is accessed 3176 // via accessors. 3177 Symbol eh_symbol( 3178 /*symID=*/symbol_id, 3179 /*name=*/llvm::StringRef(), // Name will be auto generated. 3180 /*type=*/eSymbolTypeCode, 3181 /*external=*/true, 3182 /*is_debug=*/false, 3183 /*is_trampoline=*/false, 3184 /*is_artificial=*/true, 3185 /*section_sp=*/section_sp, 3186 /*offset=*/offset, 3187 /*size=*/0, // FDE can span multiple symbols so don't use its size. 3188 /*size_is_valid=*/false, 3189 /*contains_linker_annotations=*/false, 3190 /*flags=*/0); 3191 new_symbols.push_back(eh_symbol); 3192 } 3193 } 3194 return true; 3195 }); 3196 3197 for (const Symbol &s : new_symbols) 3198 symbol_table->AddSymbol(s); 3199 } 3200 3201 bool ObjectFileELF::IsStripped() { 3202 // TODO: determine this for ELF 3203 return false; 3204 } 3205 3206 //===----------------------------------------------------------------------===// 3207 // Dump 3208 // 3209 // Dump the specifics of the runtime file container (such as any headers 3210 // segments, sections, etc). 3211 void ObjectFileELF::Dump(Stream *s) { 3212 ModuleSP module_sp(GetModule()); 3213 if (!module_sp) { 3214 return; 3215 } 3216 3217 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); 3218 s->Printf("%p: ", static_cast<void *>(this)); 3219 s->Indent(); 3220 s->PutCString("ObjectFileELF"); 3221 3222 ArchSpec header_arch = GetArchitecture(); 3223 3224 *s << ", file = '" << m_file 3225 << "', arch = " << header_arch.GetArchitectureName(); 3226 if (m_memory_addr != LLDB_INVALID_ADDRESS) 3227 s->Printf(", addr = %#16.16" PRIx64, m_memory_addr); 3228 s->EOL(); 3229 3230 DumpELFHeader(s, m_header); 3231 s->EOL(); 3232 DumpELFProgramHeaders(s); 3233 s->EOL(); 3234 DumpELFSectionHeaders(s); 3235 s->EOL(); 3236 SectionList *section_list = GetSectionList(); 3237 if (section_list) 3238 section_list->Dump(s->AsRawOstream(), s->GetIndentLevel(), nullptr, true, 3239 UINT32_MAX); 3240 Symtab *symtab = GetSymtab(); 3241 if (symtab) 3242 symtab->Dump(s, nullptr, eSortOrderNone); 3243 s->EOL(); 3244 DumpDependentModules(s); 3245 s->EOL(); 3246 DumpELFDynamic(s); 3247 s->EOL(); 3248 Address image_info_addr = GetImageInfoAddress(nullptr); 3249 if (image_info_addr.IsValid()) 3250 s->Printf("image_info_address = %#16.16" PRIx64 "\n", 3251 image_info_addr.GetFileAddress()); 3252 } 3253 3254 // DumpELFHeader 3255 // 3256 // Dump the ELF header to the specified output stream 3257 void ObjectFileELF::DumpELFHeader(Stream *s, const ELFHeader &header) { 3258 s->PutCString("ELF Header\n"); 3259 s->Printf("e_ident[EI_MAG0 ] = 0x%2.2x\n", header.e_ident[EI_MAG0]); 3260 s->Printf("e_ident[EI_MAG1 ] = 0x%2.2x '%c'\n", header.e_ident[EI_MAG1], 3261 header.e_ident[EI_MAG1]); 3262 s->Printf("e_ident[EI_MAG2 ] = 0x%2.2x '%c'\n", header.e_ident[EI_MAG2], 3263 header.e_ident[EI_MAG2]); 3264 s->Printf("e_ident[EI_MAG3 ] = 0x%2.2x '%c'\n", header.e_ident[EI_MAG3], 3265 header.e_ident[EI_MAG3]); 3266 3267 s->Printf("e_ident[EI_CLASS ] = 0x%2.2x\n", header.e_ident[EI_CLASS]); 3268 s->Printf("e_ident[EI_DATA ] = 0x%2.2x ", header.e_ident[EI_DATA]); 3269 DumpELFHeader_e_ident_EI_DATA(s, header.e_ident[EI_DATA]); 3270 s->Printf("\ne_ident[EI_VERSION] = 0x%2.2x\n", header.e_ident[EI_VERSION]); 3271 s->Printf("e_ident[EI_PAD ] = 0x%2.2x\n", header.e_ident[EI_PAD]); 3272 3273 s->Printf("e_type = 0x%4.4x ", header.e_type); 3274 DumpELFHeader_e_type(s, header.e_type); 3275 s->Printf("\ne_machine = 0x%4.4x\n", header.e_machine); 3276 s->Printf("e_version = 0x%8.8x\n", header.e_version); 3277 s->Printf("e_entry = 0x%8.8" PRIx64 "\n", header.e_entry); 3278 s->Printf("e_phoff = 0x%8.8" PRIx64 "\n", header.e_phoff); 3279 s->Printf("e_shoff = 0x%8.8" PRIx64 "\n", header.e_shoff); 3280 s->Printf("e_flags = 0x%8.8x\n", header.e_flags); 3281 s->Printf("e_ehsize = 0x%4.4x\n", header.e_ehsize); 3282 s->Printf("e_phentsize = 0x%4.4x\n", header.e_phentsize); 3283 s->Printf("e_phnum = 0x%8.8x\n", header.e_phnum); 3284 s->Printf("e_shentsize = 0x%4.4x\n", header.e_shentsize); 3285 s->Printf("e_shnum = 0x%8.8x\n", header.e_shnum); 3286 s->Printf("e_shstrndx = 0x%8.8x\n", header.e_shstrndx); 3287 } 3288 3289 // DumpELFHeader_e_type 3290 // 3291 // Dump an token value for the ELF header member e_type 3292 void ObjectFileELF::DumpELFHeader_e_type(Stream *s, elf_half e_type) { 3293 switch (e_type) { 3294 case ET_NONE: 3295 *s << "ET_NONE"; 3296 break; 3297 case ET_REL: 3298 *s << "ET_REL"; 3299 break; 3300 case ET_EXEC: 3301 *s << "ET_EXEC"; 3302 break; 3303 case ET_DYN: 3304 *s << "ET_DYN"; 3305 break; 3306 case ET_CORE: 3307 *s << "ET_CORE"; 3308 break; 3309 default: 3310 break; 3311 } 3312 } 3313 3314 // DumpELFHeader_e_ident_EI_DATA 3315 // 3316 // Dump an token value for the ELF header member e_ident[EI_DATA] 3317 void ObjectFileELF::DumpELFHeader_e_ident_EI_DATA(Stream *s, 3318 unsigned char ei_data) { 3319 switch (ei_data) { 3320 case ELFDATANONE: 3321 *s << "ELFDATANONE"; 3322 break; 3323 case ELFDATA2LSB: 3324 *s << "ELFDATA2LSB - Little Endian"; 3325 break; 3326 case ELFDATA2MSB: 3327 *s << "ELFDATA2MSB - Big Endian"; 3328 break; 3329 default: 3330 break; 3331 } 3332 } 3333 3334 // DumpELFProgramHeader 3335 // 3336 // Dump a single ELF program header to the specified output stream 3337 void ObjectFileELF::DumpELFProgramHeader(Stream *s, 3338 const ELFProgramHeader &ph) { 3339 DumpELFProgramHeader_p_type(s, ph.p_type); 3340 s->Printf(" %8.8" PRIx64 " %8.8" PRIx64 " %8.8" PRIx64, ph.p_offset, 3341 ph.p_vaddr, ph.p_paddr); 3342 s->Printf(" %8.8" PRIx64 " %8.8" PRIx64 " %8.8x (", ph.p_filesz, ph.p_memsz, 3343 ph.p_flags); 3344 3345 DumpELFProgramHeader_p_flags(s, ph.p_flags); 3346 s->Printf(") %8.8" PRIx64, ph.p_align); 3347 } 3348 3349 // DumpELFProgramHeader_p_type 3350 // 3351 // Dump an token value for the ELF program header member p_type which describes 3352 // the type of the program header 3353 void ObjectFileELF::DumpELFProgramHeader_p_type(Stream *s, elf_word p_type) { 3354 const int kStrWidth = 15; 3355 switch (p_type) { 3356 CASE_AND_STREAM(s, PT_NULL, kStrWidth); 3357 CASE_AND_STREAM(s, PT_LOAD, kStrWidth); 3358 CASE_AND_STREAM(s, PT_DYNAMIC, kStrWidth); 3359 CASE_AND_STREAM(s, PT_INTERP, kStrWidth); 3360 CASE_AND_STREAM(s, PT_NOTE, kStrWidth); 3361 CASE_AND_STREAM(s, PT_SHLIB, kStrWidth); 3362 CASE_AND_STREAM(s, PT_PHDR, kStrWidth); 3363 CASE_AND_STREAM(s, PT_TLS, kStrWidth); 3364 CASE_AND_STREAM(s, PT_GNU_EH_FRAME, kStrWidth); 3365 default: 3366 s->Printf("0x%8.8x%*s", p_type, kStrWidth - 10, ""); 3367 break; 3368 } 3369 } 3370 3371 // DumpELFProgramHeader_p_flags 3372 // 3373 // Dump an token value for the ELF program header member p_flags 3374 void ObjectFileELF::DumpELFProgramHeader_p_flags(Stream *s, elf_word p_flags) { 3375 *s << ((p_flags & PF_X) ? "PF_X" : " ") 3376 << (((p_flags & PF_X) && (p_flags & PF_W)) ? '+' : ' ') 3377 << ((p_flags & PF_W) ? "PF_W" : " ") 3378 << (((p_flags & PF_W) && (p_flags & PF_R)) ? '+' : ' ') 3379 << ((p_flags & PF_R) ? "PF_R" : " "); 3380 } 3381 3382 // DumpELFProgramHeaders 3383 // 3384 // Dump all of the ELF program header to the specified output stream 3385 void ObjectFileELF::DumpELFProgramHeaders(Stream *s) { 3386 if (!ParseProgramHeaders()) 3387 return; 3388 3389 s->PutCString("Program Headers\n"); 3390 s->PutCString("IDX p_type p_offset p_vaddr p_paddr " 3391 "p_filesz p_memsz p_flags p_align\n"); 3392 s->PutCString("==== --------------- -------- -------- -------- " 3393 "-------- -------- ------------------------- --------\n"); 3394 3395 for (const auto &H : llvm::enumerate(m_program_headers)) { 3396 s->Format("[{0,2}] ", H.index()); 3397 ObjectFileELF::DumpELFProgramHeader(s, H.value()); 3398 s->EOL(); 3399 } 3400 } 3401 3402 // DumpELFSectionHeader 3403 // 3404 // Dump a single ELF section header to the specified output stream 3405 void ObjectFileELF::DumpELFSectionHeader(Stream *s, 3406 const ELFSectionHeaderInfo &sh) { 3407 s->Printf("%8.8x ", sh.sh_name); 3408 DumpELFSectionHeader_sh_type(s, sh.sh_type); 3409 s->Printf(" %8.8" PRIx64 " (", sh.sh_flags); 3410 DumpELFSectionHeader_sh_flags(s, sh.sh_flags); 3411 s->Printf(") %8.8" PRIx64 " %8.8" PRIx64 " %8.8" PRIx64, sh.sh_addr, 3412 sh.sh_offset, sh.sh_size); 3413 s->Printf(" %8.8x %8.8x", sh.sh_link, sh.sh_info); 3414 s->Printf(" %8.8" PRIx64 " %8.8" PRIx64, sh.sh_addralign, sh.sh_entsize); 3415 } 3416 3417 // DumpELFSectionHeader_sh_type 3418 // 3419 // Dump an token value for the ELF section header member sh_type which 3420 // describes the type of the section 3421 void ObjectFileELF::DumpELFSectionHeader_sh_type(Stream *s, elf_word sh_type) { 3422 const int kStrWidth = 12; 3423 switch (sh_type) { 3424 CASE_AND_STREAM(s, SHT_NULL, kStrWidth); 3425 CASE_AND_STREAM(s, SHT_PROGBITS, kStrWidth); 3426 CASE_AND_STREAM(s, SHT_SYMTAB, kStrWidth); 3427 CASE_AND_STREAM(s, SHT_STRTAB, kStrWidth); 3428 CASE_AND_STREAM(s, SHT_RELA, kStrWidth); 3429 CASE_AND_STREAM(s, SHT_HASH, kStrWidth); 3430 CASE_AND_STREAM(s, SHT_DYNAMIC, kStrWidth); 3431 CASE_AND_STREAM(s, SHT_NOTE, kStrWidth); 3432 CASE_AND_STREAM(s, SHT_NOBITS, kStrWidth); 3433 CASE_AND_STREAM(s, SHT_REL, kStrWidth); 3434 CASE_AND_STREAM(s, SHT_SHLIB, kStrWidth); 3435 CASE_AND_STREAM(s, SHT_DYNSYM, kStrWidth); 3436 CASE_AND_STREAM(s, SHT_LOPROC, kStrWidth); 3437 CASE_AND_STREAM(s, SHT_HIPROC, kStrWidth); 3438 CASE_AND_STREAM(s, SHT_LOUSER, kStrWidth); 3439 CASE_AND_STREAM(s, SHT_HIUSER, kStrWidth); 3440 default: 3441 s->Printf("0x%8.8x%*s", sh_type, kStrWidth - 10, ""); 3442 break; 3443 } 3444 } 3445 3446 // DumpELFSectionHeader_sh_flags 3447 // 3448 // Dump an token value for the ELF section header member sh_flags 3449 void ObjectFileELF::DumpELFSectionHeader_sh_flags(Stream *s, 3450 elf_xword sh_flags) { 3451 *s << ((sh_flags & SHF_WRITE) ? "WRITE" : " ") 3452 << (((sh_flags & SHF_WRITE) && (sh_flags & SHF_ALLOC)) ? '+' : ' ') 3453 << ((sh_flags & SHF_ALLOC) ? "ALLOC" : " ") 3454 << (((sh_flags & SHF_ALLOC) && (sh_flags & SHF_EXECINSTR)) ? '+' : ' ') 3455 << ((sh_flags & SHF_EXECINSTR) ? "EXECINSTR" : " "); 3456 } 3457 3458 // DumpELFSectionHeaders 3459 // 3460 // Dump all of the ELF section header to the specified output stream 3461 void ObjectFileELF::DumpELFSectionHeaders(Stream *s) { 3462 if (!ParseSectionHeaders()) 3463 return; 3464 3465 s->PutCString("Section Headers\n"); 3466 s->PutCString("IDX name type flags " 3467 "addr offset size link info addralgn " 3468 "entsize Name\n"); 3469 s->PutCString("==== -------- ------------ -------------------------------- " 3470 "-------- -------- -------- -------- -------- -------- " 3471 "-------- ====================\n"); 3472 3473 uint32_t idx = 0; 3474 for (SectionHeaderCollConstIter I = m_section_headers.begin(); 3475 I != m_section_headers.end(); ++I, ++idx) { 3476 s->Printf("[%2u] ", idx); 3477 ObjectFileELF::DumpELFSectionHeader(s, *I); 3478 const char *section_name = I->section_name.AsCString(""); 3479 if (section_name) 3480 *s << ' ' << section_name << "\n"; 3481 } 3482 } 3483 3484 void ObjectFileELF::DumpDependentModules(lldb_private::Stream *s) { 3485 size_t num_modules = ParseDependentModules(); 3486 3487 if (num_modules > 0) { 3488 s->PutCString("Dependent Modules:\n"); 3489 for (unsigned i = 0; i < num_modules; ++i) { 3490 const FileSpec &spec = m_filespec_up->GetFileSpecAtIndex(i); 3491 s->Printf(" %s\n", spec.GetFilename().GetCString()); 3492 } 3493 } 3494 } 3495 3496 std::string static getDynamicTagAsString(uint16_t Arch, uint64_t Type) { 3497 #define DYNAMIC_STRINGIFY_ENUM(tag, value) \ 3498 case value: \ 3499 return #tag; 3500 3501 #define DYNAMIC_TAG(n, v) 3502 switch (Arch) { 3503 case llvm::ELF::EM_AARCH64: 3504 switch (Type) { 3505 #define AARCH64_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value) 3506 #include "llvm/BinaryFormat/DynamicTags.def" 3507 #undef AARCH64_DYNAMIC_TAG 3508 } 3509 break; 3510 3511 case llvm::ELF::EM_HEXAGON: 3512 switch (Type) { 3513 #define HEXAGON_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value) 3514 #include "llvm/BinaryFormat/DynamicTags.def" 3515 #undef HEXAGON_DYNAMIC_TAG 3516 } 3517 break; 3518 3519 case llvm::ELF::EM_MIPS: 3520 switch (Type) { 3521 #define MIPS_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value) 3522 #include "llvm/BinaryFormat/DynamicTags.def" 3523 #undef MIPS_DYNAMIC_TAG 3524 } 3525 break; 3526 3527 case llvm::ELF::EM_PPC: 3528 switch (Type) { 3529 #define PPC_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value) 3530 #include "llvm/BinaryFormat/DynamicTags.def" 3531 #undef PPC_DYNAMIC_TAG 3532 } 3533 break; 3534 3535 case llvm::ELF::EM_PPC64: 3536 switch (Type) { 3537 #define PPC64_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value) 3538 #include "llvm/BinaryFormat/DynamicTags.def" 3539 #undef PPC64_DYNAMIC_TAG 3540 } 3541 break; 3542 3543 case llvm::ELF::EM_RISCV: 3544 switch (Type) { 3545 #define RISCV_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value) 3546 #include "llvm/BinaryFormat/DynamicTags.def" 3547 #undef RISCV_DYNAMIC_TAG 3548 } 3549 break; 3550 } 3551 #undef DYNAMIC_TAG 3552 switch (Type) { 3553 // Now handle all dynamic tags except the architecture specific ones 3554 #define AARCH64_DYNAMIC_TAG(name, value) 3555 #define MIPS_DYNAMIC_TAG(name, value) 3556 #define HEXAGON_DYNAMIC_TAG(name, value) 3557 #define PPC_DYNAMIC_TAG(name, value) 3558 #define PPC64_DYNAMIC_TAG(name, value) 3559 #define RISCV_DYNAMIC_TAG(name, value) 3560 // Also ignore marker tags such as DT_HIOS (maps to DT_VERNEEDNUM), etc. 3561 #define DYNAMIC_TAG_MARKER(name, value) 3562 #define DYNAMIC_TAG(name, value) \ 3563 case value: \ 3564 return #name; 3565 #include "llvm/BinaryFormat/DynamicTags.def" 3566 #undef DYNAMIC_TAG 3567 #undef AARCH64_DYNAMIC_TAG 3568 #undef MIPS_DYNAMIC_TAG 3569 #undef HEXAGON_DYNAMIC_TAG 3570 #undef PPC_DYNAMIC_TAG 3571 #undef PPC64_DYNAMIC_TAG 3572 #undef RISCV_DYNAMIC_TAG 3573 #undef DYNAMIC_TAG_MARKER 3574 #undef DYNAMIC_STRINGIFY_ENUM 3575 default: 3576 return "<unknown:>0x" + llvm::utohexstr(Type, true); 3577 } 3578 } 3579 3580 void ObjectFileELF::DumpELFDynamic(lldb_private::Stream *s) { 3581 ParseDynamicSymbols(); 3582 if (m_dynamic_symbols.empty()) 3583 return; 3584 3585 s->PutCString(".dynamic:\n"); 3586 s->PutCString("IDX d_tag d_val/d_ptr\n"); 3587 s->PutCString("==== ---------------- ------------------\n"); 3588 uint32_t idx = 0; 3589 for (const auto &entry : m_dynamic_symbols) { 3590 s->Printf("[%2u] ", idx++); 3591 s->Printf( 3592 "%-16s 0x%16.16" PRIx64, 3593 getDynamicTagAsString(m_header.e_machine, entry.symbol.d_tag).c_str(), 3594 entry.symbol.d_ptr); 3595 if (!entry.name.empty()) 3596 s->Printf(" \"%s\"", entry.name.c_str()); 3597 s->EOL(); 3598 } 3599 } 3600 3601 ArchSpec ObjectFileELF::GetArchitecture() { 3602 if (!ParseHeader()) 3603 return ArchSpec(); 3604 3605 if (m_section_headers.empty()) { 3606 // Allow elf notes to be parsed which may affect the detected architecture. 3607 ParseSectionHeaders(); 3608 } 3609 3610 if (CalculateType() == eTypeCoreFile && 3611 !m_arch_spec.TripleOSWasSpecified()) { 3612 // Core files don't have section headers yet they have PT_NOTE program 3613 // headers that might shed more light on the architecture 3614 for (const elf::ELFProgramHeader &H : ProgramHeaders()) { 3615 if (H.p_type != PT_NOTE || H.p_offset == 0 || H.p_filesz == 0) 3616 continue; 3617 DataExtractor data; 3618 if (data.SetData(m_data, H.p_offset, H.p_filesz) == H.p_filesz) { 3619 UUID uuid; 3620 RefineModuleDetailsFromNote(data, m_arch_spec, uuid); 3621 } 3622 } 3623 } 3624 return m_arch_spec; 3625 } 3626 3627 ObjectFile::Type ObjectFileELF::CalculateType() { 3628 switch (m_header.e_type) { 3629 case llvm::ELF::ET_NONE: 3630 // 0 - No file type 3631 return eTypeUnknown; 3632 3633 case llvm::ELF::ET_REL: 3634 // 1 - Relocatable file 3635 return eTypeObjectFile; 3636 3637 case llvm::ELF::ET_EXEC: 3638 // 2 - Executable file 3639 return eTypeExecutable; 3640 3641 case llvm::ELF::ET_DYN: 3642 // 3 - Shared object file 3643 return eTypeSharedLibrary; 3644 3645 case ET_CORE: 3646 // 4 - Core file 3647 return eTypeCoreFile; 3648 3649 default: 3650 break; 3651 } 3652 return eTypeUnknown; 3653 } 3654 3655 ObjectFile::Strata ObjectFileELF::CalculateStrata() { 3656 switch (m_header.e_type) { 3657 case llvm::ELF::ET_NONE: 3658 // 0 - No file type 3659 return eStrataUnknown; 3660 3661 case llvm::ELF::ET_REL: 3662 // 1 - Relocatable file 3663 return eStrataUnknown; 3664 3665 case llvm::ELF::ET_EXEC: 3666 // 2 - Executable file 3667 { 3668 SectionList *section_list = GetSectionList(); 3669 if (section_list) { 3670 static ConstString loader_section_name(".interp"); 3671 SectionSP loader_section = 3672 section_list->FindSectionByName(loader_section_name); 3673 if (loader_section) { 3674 char buffer[256]; 3675 size_t read_size = 3676 ReadSectionData(loader_section.get(), 0, buffer, sizeof(buffer)); 3677 3678 // We compare the content of .interp section 3679 // It will contains \0 when counting read_size, so the size needs to 3680 // decrease by one 3681 llvm::StringRef loader_name(buffer, read_size - 1); 3682 llvm::StringRef freebsd_kernel_loader_name("/red/herring"); 3683 if (loader_name == freebsd_kernel_loader_name) 3684 return eStrataKernel; 3685 } 3686 } 3687 return eStrataUser; 3688 } 3689 3690 case llvm::ELF::ET_DYN: 3691 // 3 - Shared object file 3692 // TODO: is there any way to detect that an shared library is a kernel 3693 // related executable by inspecting the program headers, section headers, 3694 // symbols, or any other flag bits??? 3695 return eStrataUnknown; 3696 3697 case ET_CORE: 3698 // 4 - Core file 3699 // TODO: is there any way to detect that an core file is a kernel 3700 // related executable by inspecting the program headers, section headers, 3701 // symbols, or any other flag bits??? 3702 return eStrataUnknown; 3703 3704 default: 3705 break; 3706 } 3707 return eStrataUnknown; 3708 } 3709 3710 size_t ObjectFileELF::ReadSectionData(Section *section, 3711 lldb::offset_t section_offset, void *dst, 3712 size_t dst_len) { 3713 // If some other objectfile owns this data, pass this to them. 3714 if (section->GetObjectFile() != this) 3715 return section->GetObjectFile()->ReadSectionData(section, section_offset, 3716 dst, dst_len); 3717 3718 if (!section->Test(SHF_COMPRESSED)) 3719 return ObjectFile::ReadSectionData(section, section_offset, dst, dst_len); 3720 3721 // For compressed sections we need to read to full data to be able to 3722 // decompress. 3723 DataExtractor data; 3724 ReadSectionData(section, data); 3725 return data.CopyData(section_offset, dst_len, dst); 3726 } 3727 3728 size_t ObjectFileELF::ReadSectionData(Section *section, 3729 DataExtractor §ion_data) { 3730 // If some other objectfile owns this data, pass this to them. 3731 if (section->GetObjectFile() != this) 3732 return section->GetObjectFile()->ReadSectionData(section, section_data); 3733 3734 size_t result = ObjectFile::ReadSectionData(section, section_data); 3735 if (result == 0 || !(section->Get() & llvm::ELF::SHF_COMPRESSED)) 3736 return result; 3737 3738 auto Decompressor = llvm::object::Decompressor::create( 3739 section->GetName().GetStringRef(), 3740 {reinterpret_cast<const char *>(section_data.GetDataStart()), 3741 size_t(section_data.GetByteSize())}, 3742 GetByteOrder() == eByteOrderLittle, GetAddressByteSize() == 8); 3743 if (!Decompressor) { 3744 GetModule()->ReportWarning( 3745 "Unable to initialize decompressor for section '{0}': {1}", 3746 section->GetName().GetCString(), 3747 llvm::toString(Decompressor.takeError()).c_str()); 3748 section_data.Clear(); 3749 return 0; 3750 } 3751 3752 auto buffer_sp = 3753 std::make_shared<DataBufferHeap>(Decompressor->getDecompressedSize(), 0); 3754 if (auto error = Decompressor->decompress( 3755 {buffer_sp->GetBytes(), size_t(buffer_sp->GetByteSize())})) { 3756 GetModule()->ReportWarning("Decompression of section '{0}' failed: {1}", 3757 section->GetName().GetCString(), 3758 llvm::toString(std::move(error)).c_str()); 3759 section_data.Clear(); 3760 return 0; 3761 } 3762 3763 section_data.SetData(buffer_sp); 3764 return buffer_sp->GetByteSize(); 3765 } 3766 3767 llvm::ArrayRef<ELFProgramHeader> ObjectFileELF::ProgramHeaders() { 3768 ParseProgramHeaders(); 3769 return m_program_headers; 3770 } 3771 3772 DataExtractor ObjectFileELF::GetSegmentData(const ELFProgramHeader &H) { 3773 // Try and read the program header from our cached m_data which can come from 3774 // the file on disk being mmap'ed or from the initial part of the ELF file we 3775 // read from memory and cached. 3776 DataExtractor data = DataExtractor(m_data, H.p_offset, H.p_filesz); 3777 if (data.GetByteSize() == H.p_filesz) 3778 return data; 3779 if (IsInMemory()) { 3780 // We have a ELF file in process memory, read the program header data from 3781 // the process. 3782 if (ProcessSP process_sp = m_process_wp.lock()) { 3783 const lldb::offset_t base_file_addr = GetBaseAddress().GetFileAddress(); 3784 const addr_t load_bias = m_memory_addr - base_file_addr; 3785 const addr_t data_addr = H.p_vaddr + load_bias; 3786 if (DataBufferSP data_sp = ReadMemory(process_sp, data_addr, H.p_memsz)) 3787 return DataExtractor(data_sp, GetByteOrder(), GetAddressByteSize()); 3788 } 3789 } 3790 return DataExtractor(); 3791 } 3792 3793 bool ObjectFileELF::AnySegmentHasPhysicalAddress() { 3794 for (const ELFProgramHeader &H : ProgramHeaders()) { 3795 if (H.p_paddr != 0) 3796 return true; 3797 } 3798 return false; 3799 } 3800 3801 std::vector<ObjectFile::LoadableData> 3802 ObjectFileELF::GetLoadableData(Target &target) { 3803 // Create a list of loadable data from loadable segments, using physical 3804 // addresses if they aren't all null 3805 std::vector<LoadableData> loadables; 3806 bool should_use_paddr = AnySegmentHasPhysicalAddress(); 3807 for (const ELFProgramHeader &H : ProgramHeaders()) { 3808 LoadableData loadable; 3809 if (H.p_type != llvm::ELF::PT_LOAD) 3810 continue; 3811 loadable.Dest = should_use_paddr ? H.p_paddr : H.p_vaddr; 3812 if (loadable.Dest == LLDB_INVALID_ADDRESS) 3813 continue; 3814 if (H.p_filesz == 0) 3815 continue; 3816 auto segment_data = GetSegmentData(H); 3817 loadable.Contents = llvm::ArrayRef<uint8_t>(segment_data.GetDataStart(), 3818 segment_data.GetByteSize()); 3819 loadables.push_back(loadable); 3820 } 3821 return loadables; 3822 } 3823 3824 lldb::WritableDataBufferSP 3825 ObjectFileELF::MapFileDataWritable(const FileSpec &file, uint64_t Size, 3826 uint64_t Offset) { 3827 return FileSystem::Instance().CreateWritableDataBuffer(file.GetPath(), Size, 3828 Offset); 3829 } 3830 3831 std::optional<DataExtractor> ObjectFileELF::GetDynstrData() { 3832 if (SectionList *section_list = GetSectionList()) { 3833 // Find the SHT_DYNAMIC section. 3834 if (Section *dynamic = 3835 section_list 3836 ->FindSectionByType(eSectionTypeELFDynamicLinkInfo, true) 3837 .get()) { 3838 assert(dynamic->GetObjectFile() == this); 3839 if (const ELFSectionHeaderInfo *header = 3840 GetSectionHeaderByIndex(dynamic->GetID())) { 3841 // sh_link: section header index of string table used by entries in 3842 // the section. 3843 if (Section *dynstr = 3844 section_list->FindSectionByID(header->sh_link).get()) { 3845 DataExtractor data; 3846 if (ReadSectionData(dynstr, data)) 3847 return data; 3848 } 3849 } 3850 } 3851 } 3852 3853 // Every ELF file which represents an executable or shared library has 3854 // mandatory .dynamic entries. Two of these values are DT_STRTAB and DT_STRSZ 3855 // and represent the dynamic symbol tables's string table. These are needed 3856 // by the dynamic loader and we can read them from a process' address space. 3857 // 3858 // When loading and ELF file from memory, only the program headers end up 3859 // being mapped into memory, and we can find these values in the PT_DYNAMIC 3860 // segment. 3861 const ELFDynamic *strtab = FindDynamicSymbol(DT_STRTAB); 3862 const ELFDynamic *strsz = FindDynamicSymbol(DT_STRSZ); 3863 if (strtab == nullptr || strsz == nullptr) 3864 return std::nullopt; 3865 3866 if (ProcessSP process_sp = m_process_wp.lock()) { 3867 if (DataBufferSP data_sp = 3868 ReadMemory(process_sp, strtab->d_ptr, strsz->d_val)) 3869 return DataExtractor(data_sp, GetByteOrder(), GetAddressByteSize()); 3870 } else { 3871 // We have an ELF file with no section headers or we didn't find the 3872 // .dynamic section. Try and find the .dynstr section. 3873 Address addr; 3874 if (addr.ResolveAddressUsingFileSections(strtab->d_ptr, GetSectionList())) { 3875 DataExtractor data; 3876 addr.GetSection()->GetSectionData(data); 3877 return DataExtractor(data, 3878 strtab->d_ptr - addr.GetSection()->GetFileAddress(), 3879 strsz->d_val); 3880 } 3881 } 3882 return std::nullopt; 3883 } 3884 3885 std::optional<lldb_private::DataExtractor> ObjectFileELF::GetDynamicData() { 3886 DataExtractor data; 3887 // The PT_DYNAMIC program header describes where the .dynamic section is and 3888 // doesn't require parsing section headers. The PT_DYNAMIC is required by 3889 // executables and shared libraries so it will always be available. 3890 for (const ELFProgramHeader &H : ProgramHeaders()) { 3891 if (H.p_type == llvm::ELF::PT_DYNAMIC) { 3892 data = GetSegmentData(H); 3893 if (data.GetByteSize() > 0) { 3894 m_dynamic_base_addr = H.p_vaddr; 3895 return data; 3896 } 3897 } 3898 } 3899 // Fall back to using section headers. 3900 if (SectionList *section_list = GetSectionList()) { 3901 // Find the SHT_DYNAMIC section. 3902 if (Section *dynamic = 3903 section_list 3904 ->FindSectionByType(eSectionTypeELFDynamicLinkInfo, true) 3905 .get()) { 3906 assert(dynamic->GetObjectFile() == this); 3907 if (ReadSectionData(dynamic, data)) { 3908 m_dynamic_base_addr = dynamic->GetFileAddress(); 3909 return data; 3910 } 3911 } 3912 } 3913 return std::nullopt; 3914 } 3915