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