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