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 bool is_trampoline = false; 2360 if (arch.IsValid() && (arch.GetMachine() == llvm::Triple::aarch64)) { 2361 // On AArch64, trampolines are registered as code. 2362 // If we detect a trampoline (which starts with __AArch64ADRPThunk_ or 2363 // __AArch64AbsLongThunk_) we register the symbol as a trampoline. This 2364 // way we will be able to detect the trampoline when we step in a function 2365 // and step through the trampoline. 2366 if (symbol_type == eSymbolTypeCode) { 2367 llvm::StringRef trampoline_name = mangled.GetName().GetStringRef(); 2368 if (trampoline_name.starts_with("__AArch64ADRPThunk_") || 2369 trampoline_name.starts_with("__AArch64AbsLongThunk_")) { 2370 symbol_type = eSymbolTypeTrampoline; 2371 is_trampoline = true; 2372 } 2373 } 2374 } 2375 2376 Symbol dc_symbol( 2377 i + start_id, // ID is the original symbol table index. 2378 mangled, 2379 symbol_type, // Type of this symbol 2380 is_global, // Is this globally visible? 2381 false, // Is this symbol debug info? 2382 is_trampoline, // Is this symbol a trampoline? 2383 false, // Is this symbol artificial? 2384 AddressRange(symbol_section_sp, // Section in which this symbol is 2385 // defined or null. 2386 symbol_value, // Offset in section or symbol value. 2387 symbol.st_size), // Size in bytes of this symbol. 2388 symbol_size_valid, // Symbol size is valid 2389 has_suffix, // Contains linker annotations? 2390 flags); // Symbol flags. 2391 if (symbol.getBinding() == STB_WEAK) 2392 dc_symbol.SetIsWeak(true); 2393 symtab->AddSymbol(dc_symbol); 2394 } 2395 return i; 2396 } 2397 2398 unsigned ObjectFileELF::ParseSymbolTable(Symtab *symbol_table, 2399 user_id_t start_id, 2400 lldb_private::Section *symtab) { 2401 if (symtab->GetObjectFile() != this) { 2402 // If the symbol table section is owned by a different object file, have it 2403 // do the parsing. 2404 ObjectFileELF *obj_file_elf = 2405 static_cast<ObjectFileELF *>(symtab->GetObjectFile()); 2406 return obj_file_elf->ParseSymbolTable(symbol_table, start_id, symtab); 2407 } 2408 2409 // Get section list for this object file. 2410 SectionList *section_list = m_sections_up.get(); 2411 if (!section_list) 2412 return 0; 2413 2414 user_id_t symtab_id = symtab->GetID(); 2415 const ELFSectionHeaderInfo *symtab_hdr = GetSectionHeaderByIndex(symtab_id); 2416 assert(symtab_hdr->sh_type == SHT_SYMTAB || 2417 symtab_hdr->sh_type == SHT_DYNSYM); 2418 2419 // sh_link: section header index of associated string table. 2420 user_id_t strtab_id = symtab_hdr->sh_link; 2421 Section *strtab = section_list->FindSectionByID(strtab_id).get(); 2422 2423 if (symtab && strtab) { 2424 assert(symtab->GetObjectFile() == this); 2425 assert(strtab->GetObjectFile() == this); 2426 2427 DataExtractor symtab_data; 2428 DataExtractor strtab_data; 2429 if (ReadSectionData(symtab, symtab_data) && 2430 ReadSectionData(strtab, strtab_data)) { 2431 size_t num_symbols = symtab_data.GetByteSize() / symtab_hdr->sh_entsize; 2432 2433 return ParseSymbols(symbol_table, start_id, section_list, num_symbols, 2434 symtab_data, strtab_data); 2435 } 2436 } 2437 2438 return 0; 2439 } 2440 2441 size_t ObjectFileELF::ParseDynamicSymbols() { 2442 if (m_dynamic_symbols.size()) 2443 return m_dynamic_symbols.size(); 2444 2445 SectionList *section_list = GetSectionList(); 2446 if (!section_list) 2447 return 0; 2448 2449 // Find the SHT_DYNAMIC section. 2450 Section *dynsym = 2451 section_list->FindSectionByType(eSectionTypeELFDynamicLinkInfo, true) 2452 .get(); 2453 if (!dynsym) 2454 return 0; 2455 assert(dynsym->GetObjectFile() == this); 2456 2457 ELFDynamic symbol; 2458 DataExtractor dynsym_data; 2459 if (ReadSectionData(dynsym, dynsym_data)) { 2460 const lldb::offset_t section_size = dynsym_data.GetByteSize(); 2461 lldb::offset_t cursor = 0; 2462 2463 while (cursor < section_size) { 2464 if (!symbol.Parse(dynsym_data, &cursor)) 2465 break; 2466 2467 m_dynamic_symbols.push_back(symbol); 2468 } 2469 } 2470 2471 return m_dynamic_symbols.size(); 2472 } 2473 2474 const ELFDynamic *ObjectFileELF::FindDynamicSymbol(unsigned tag) { 2475 if (!ParseDynamicSymbols()) 2476 return nullptr; 2477 2478 DynamicSymbolCollIter I = m_dynamic_symbols.begin(); 2479 DynamicSymbolCollIter E = m_dynamic_symbols.end(); 2480 for (; I != E; ++I) { 2481 ELFDynamic *symbol = &*I; 2482 2483 if (symbol->d_tag == tag) 2484 return symbol; 2485 } 2486 2487 return nullptr; 2488 } 2489 2490 unsigned ObjectFileELF::PLTRelocationType() { 2491 // DT_PLTREL 2492 // This member specifies the type of relocation entry to which the 2493 // procedure linkage table refers. The d_val member holds DT_REL or 2494 // DT_RELA, as appropriate. All relocations in a procedure linkage table 2495 // must use the same relocation. 2496 const ELFDynamic *symbol = FindDynamicSymbol(DT_PLTREL); 2497 2498 if (symbol) 2499 return symbol->d_val; 2500 2501 return 0; 2502 } 2503 2504 // Returns the size of the normal plt entries and the offset of the first 2505 // normal plt entry. The 0th entry in the plt table is usually a resolution 2506 // entry which have different size in some architectures then the rest of the 2507 // plt entries. 2508 static std::pair<uint64_t, uint64_t> 2509 GetPltEntrySizeAndOffset(const ELFSectionHeader *rel_hdr, 2510 const ELFSectionHeader *plt_hdr) { 2511 const elf_xword num_relocations = rel_hdr->sh_size / rel_hdr->sh_entsize; 2512 2513 // Clang 3.3 sets entsize to 4 for 32-bit binaries, but the plt entries are 2514 // 16 bytes. So round the entsize up by the alignment if addralign is set. 2515 elf_xword plt_entsize = 2516 plt_hdr->sh_addralign 2517 ? llvm::alignTo(plt_hdr->sh_entsize, plt_hdr->sh_addralign) 2518 : plt_hdr->sh_entsize; 2519 2520 // Some linkers e.g ld for arm, fill plt_hdr->sh_entsize field incorrectly. 2521 // PLT entries relocation code in general requires multiple instruction and 2522 // should be greater than 4 bytes in most cases. Try to guess correct size 2523 // just in case. 2524 if (plt_entsize <= 4) { 2525 // The linker haven't set the plt_hdr->sh_entsize field. Try to guess the 2526 // size of the plt entries based on the number of entries and the size of 2527 // the plt section with the assumption that the size of the 0th entry is at 2528 // least as big as the size of the normal entries and it isn't much bigger 2529 // then that. 2530 if (plt_hdr->sh_addralign) 2531 plt_entsize = plt_hdr->sh_size / plt_hdr->sh_addralign / 2532 (num_relocations + 1) * plt_hdr->sh_addralign; 2533 else 2534 plt_entsize = plt_hdr->sh_size / (num_relocations + 1); 2535 } 2536 2537 elf_xword plt_offset = plt_hdr->sh_size - num_relocations * plt_entsize; 2538 2539 return std::make_pair(plt_entsize, plt_offset); 2540 } 2541 2542 static unsigned ParsePLTRelocations( 2543 Symtab *symbol_table, user_id_t start_id, unsigned rel_type, 2544 const ELFHeader *hdr, const ELFSectionHeader *rel_hdr, 2545 const ELFSectionHeader *plt_hdr, const ELFSectionHeader *sym_hdr, 2546 const lldb::SectionSP &plt_section_sp, DataExtractor &rel_data, 2547 DataExtractor &symtab_data, DataExtractor &strtab_data) { 2548 ELFRelocation rel(rel_type); 2549 ELFSymbol symbol; 2550 lldb::offset_t offset = 0; 2551 2552 uint64_t plt_offset, plt_entsize; 2553 std::tie(plt_entsize, plt_offset) = 2554 GetPltEntrySizeAndOffset(rel_hdr, plt_hdr); 2555 const elf_xword num_relocations = rel_hdr->sh_size / rel_hdr->sh_entsize; 2556 2557 typedef unsigned (*reloc_info_fn)(const ELFRelocation &rel); 2558 reloc_info_fn reloc_type; 2559 reloc_info_fn reloc_symbol; 2560 2561 if (hdr->Is32Bit()) { 2562 reloc_type = ELFRelocation::RelocType32; 2563 reloc_symbol = ELFRelocation::RelocSymbol32; 2564 } else { 2565 reloc_type = ELFRelocation::RelocType64; 2566 reloc_symbol = ELFRelocation::RelocSymbol64; 2567 } 2568 2569 unsigned slot_type = hdr->GetRelocationJumpSlotType(); 2570 unsigned i; 2571 for (i = 0; i < num_relocations; ++i) { 2572 if (!rel.Parse(rel_data, &offset)) 2573 break; 2574 2575 if (reloc_type(rel) != slot_type) 2576 continue; 2577 2578 lldb::offset_t symbol_offset = reloc_symbol(rel) * sym_hdr->sh_entsize; 2579 if (!symbol.Parse(symtab_data, &symbol_offset)) 2580 break; 2581 2582 const char *symbol_name = strtab_data.PeekCStr(symbol.st_name); 2583 uint64_t plt_index = plt_offset + i * plt_entsize; 2584 2585 Symbol jump_symbol( 2586 i + start_id, // Symbol table index 2587 symbol_name, // symbol name. 2588 eSymbolTypeTrampoline, // Type of this symbol 2589 false, // Is this globally visible? 2590 false, // Is this symbol debug info? 2591 true, // Is this symbol a trampoline? 2592 true, // Is this symbol artificial? 2593 plt_section_sp, // Section in which this symbol is defined or null. 2594 plt_index, // Offset in section or symbol value. 2595 plt_entsize, // Size in bytes of this symbol. 2596 true, // Size is valid 2597 false, // Contains linker annotations? 2598 0); // Symbol flags. 2599 2600 symbol_table->AddSymbol(jump_symbol); 2601 } 2602 2603 return i; 2604 } 2605 2606 unsigned 2607 ObjectFileELF::ParseTrampolineSymbols(Symtab *symbol_table, user_id_t start_id, 2608 const ELFSectionHeaderInfo *rel_hdr, 2609 user_id_t rel_id) { 2610 assert(rel_hdr->sh_type == SHT_RELA || rel_hdr->sh_type == SHT_REL); 2611 2612 // The link field points to the associated symbol table. 2613 user_id_t symtab_id = rel_hdr->sh_link; 2614 2615 // If the link field doesn't point to the appropriate symbol name table then 2616 // try to find it by name as some compiler don't fill in the link fields. 2617 if (!symtab_id) 2618 symtab_id = GetSectionIndexByName(".dynsym"); 2619 2620 // Get PLT section. We cannot use rel_hdr->sh_info, since current linkers 2621 // point that to the .got.plt or .got section instead of .plt. 2622 user_id_t plt_id = GetSectionIndexByName(".plt"); 2623 2624 if (!symtab_id || !plt_id) 2625 return 0; 2626 2627 const ELFSectionHeaderInfo *plt_hdr = GetSectionHeaderByIndex(plt_id); 2628 if (!plt_hdr) 2629 return 0; 2630 2631 const ELFSectionHeaderInfo *sym_hdr = GetSectionHeaderByIndex(symtab_id); 2632 if (!sym_hdr) 2633 return 0; 2634 2635 SectionList *section_list = m_sections_up.get(); 2636 if (!section_list) 2637 return 0; 2638 2639 Section *rel_section = section_list->FindSectionByID(rel_id).get(); 2640 if (!rel_section) 2641 return 0; 2642 2643 SectionSP plt_section_sp(section_list->FindSectionByID(plt_id)); 2644 if (!plt_section_sp) 2645 return 0; 2646 2647 Section *symtab = section_list->FindSectionByID(symtab_id).get(); 2648 if (!symtab) 2649 return 0; 2650 2651 // sh_link points to associated string table. 2652 Section *strtab = section_list->FindSectionByID(sym_hdr->sh_link).get(); 2653 if (!strtab) 2654 return 0; 2655 2656 DataExtractor rel_data; 2657 if (!ReadSectionData(rel_section, rel_data)) 2658 return 0; 2659 2660 DataExtractor symtab_data; 2661 if (!ReadSectionData(symtab, symtab_data)) 2662 return 0; 2663 2664 DataExtractor strtab_data; 2665 if (!ReadSectionData(strtab, strtab_data)) 2666 return 0; 2667 2668 unsigned rel_type = PLTRelocationType(); 2669 if (!rel_type) 2670 return 0; 2671 2672 return ParsePLTRelocations(symbol_table, start_id, rel_type, &m_header, 2673 rel_hdr, plt_hdr, sym_hdr, plt_section_sp, 2674 rel_data, symtab_data, strtab_data); 2675 } 2676 2677 static void ApplyELF64ABS64Relocation(Symtab *symtab, ELFRelocation &rel, 2678 DataExtractor &debug_data, 2679 Section *rel_section) { 2680 Symbol *symbol = symtab->FindSymbolByID(ELFRelocation::RelocSymbol64(rel)); 2681 if (symbol) { 2682 addr_t value = symbol->GetAddressRef().GetFileAddress(); 2683 DataBufferSP &data_buffer_sp = debug_data.GetSharedDataBuffer(); 2684 // ObjectFileELF creates a WritableDataBuffer in CreateInstance. 2685 WritableDataBuffer *data_buffer = 2686 llvm::cast<WritableDataBuffer>(data_buffer_sp.get()); 2687 uint64_t *dst = reinterpret_cast<uint64_t *>( 2688 data_buffer->GetBytes() + rel_section->GetFileOffset() + 2689 ELFRelocation::RelocOffset64(rel)); 2690 uint64_t val_offset = value + ELFRelocation::RelocAddend64(rel); 2691 memcpy(dst, &val_offset, sizeof(uint64_t)); 2692 } 2693 } 2694 2695 static void ApplyELF64ABS32Relocation(Symtab *symtab, ELFRelocation &rel, 2696 DataExtractor &debug_data, 2697 Section *rel_section, bool is_signed) { 2698 Symbol *symbol = symtab->FindSymbolByID(ELFRelocation::RelocSymbol64(rel)); 2699 if (symbol) { 2700 addr_t value = symbol->GetAddressRef().GetFileAddress(); 2701 value += ELFRelocation::RelocAddend32(rel); 2702 if ((!is_signed && (value > UINT32_MAX)) || 2703 (is_signed && 2704 ((int64_t)value > INT32_MAX || (int64_t)value < INT32_MIN))) { 2705 Log *log = GetLog(LLDBLog::Modules); 2706 LLDB_LOGF(log, "Failed to apply debug info relocations"); 2707 return; 2708 } 2709 uint32_t truncated_addr = (value & 0xFFFFFFFF); 2710 DataBufferSP &data_buffer_sp = debug_data.GetSharedDataBuffer(); 2711 // ObjectFileELF creates a WritableDataBuffer in CreateInstance. 2712 WritableDataBuffer *data_buffer = 2713 llvm::cast<WritableDataBuffer>(data_buffer_sp.get()); 2714 uint32_t *dst = reinterpret_cast<uint32_t *>( 2715 data_buffer->GetBytes() + rel_section->GetFileOffset() + 2716 ELFRelocation::RelocOffset32(rel)); 2717 memcpy(dst, &truncated_addr, sizeof(uint32_t)); 2718 } 2719 } 2720 2721 static void ApplyELF32ABS32RelRelocation(Symtab *symtab, ELFRelocation &rel, 2722 DataExtractor &debug_data, 2723 Section *rel_section) { 2724 Log *log = GetLog(LLDBLog::Modules); 2725 Symbol *symbol = symtab->FindSymbolByID(ELFRelocation::RelocSymbol32(rel)); 2726 if (symbol) { 2727 addr_t value = symbol->GetAddressRef().GetFileAddress(); 2728 if (value == LLDB_INVALID_ADDRESS) { 2729 const char *name = symbol->GetName().GetCString(); 2730 LLDB_LOGF(log, "Debug info symbol invalid: %s", name); 2731 return; 2732 } 2733 assert(llvm::isUInt<32>(value) && "Valid addresses are 32-bit"); 2734 DataBufferSP &data_buffer_sp = debug_data.GetSharedDataBuffer(); 2735 // ObjectFileELF creates a WritableDataBuffer in CreateInstance. 2736 WritableDataBuffer *data_buffer = 2737 llvm::cast<WritableDataBuffer>(data_buffer_sp.get()); 2738 uint8_t *dst = data_buffer->GetBytes() + rel_section->GetFileOffset() + 2739 ELFRelocation::RelocOffset32(rel); 2740 // Implicit addend is stored inline as a signed value. 2741 int32_t addend; 2742 memcpy(&addend, dst, sizeof(int32_t)); 2743 // The sum must be positive. This extra check prevents UB from overflow in 2744 // the actual range check below. 2745 if (addend < 0 && static_cast<uint32_t>(-addend) > value) { 2746 LLDB_LOGF(log, "Debug info relocation overflow: 0x%" PRIx64, 2747 static_cast<int64_t>(value) + addend); 2748 return; 2749 } 2750 if (!llvm::isUInt<32>(value + addend)) { 2751 LLDB_LOGF(log, "Debug info relocation out of range: 0x%" PRIx64, value); 2752 return; 2753 } 2754 uint32_t addr = value + addend; 2755 memcpy(dst, &addr, sizeof(uint32_t)); 2756 } 2757 } 2758 2759 unsigned ObjectFileELF::ApplyRelocations( 2760 Symtab *symtab, const ELFHeader *hdr, const ELFSectionHeader *rel_hdr, 2761 const ELFSectionHeader *symtab_hdr, const ELFSectionHeader *debug_hdr, 2762 DataExtractor &rel_data, DataExtractor &symtab_data, 2763 DataExtractor &debug_data, Section *rel_section) { 2764 ELFRelocation rel(rel_hdr->sh_type); 2765 lldb::addr_t offset = 0; 2766 const unsigned num_relocations = rel_hdr->sh_size / rel_hdr->sh_entsize; 2767 typedef unsigned (*reloc_info_fn)(const ELFRelocation &rel); 2768 reloc_info_fn reloc_type; 2769 reloc_info_fn reloc_symbol; 2770 2771 if (hdr->Is32Bit()) { 2772 reloc_type = ELFRelocation::RelocType32; 2773 reloc_symbol = ELFRelocation::RelocSymbol32; 2774 } else { 2775 reloc_type = ELFRelocation::RelocType64; 2776 reloc_symbol = ELFRelocation::RelocSymbol64; 2777 } 2778 2779 for (unsigned i = 0; i < num_relocations; ++i) { 2780 if (!rel.Parse(rel_data, &offset)) { 2781 GetModule()->ReportError(".rel{0}[{1:d}] failed to parse relocation", 2782 rel_section->GetName().AsCString(), i); 2783 break; 2784 } 2785 Symbol *symbol = nullptr; 2786 2787 if (hdr->Is32Bit()) { 2788 switch (hdr->e_machine) { 2789 case llvm::ELF::EM_ARM: 2790 switch (reloc_type(rel)) { 2791 case R_ARM_ABS32: 2792 ApplyELF32ABS32RelRelocation(symtab, rel, debug_data, rel_section); 2793 break; 2794 case R_ARM_REL32: 2795 GetModule()->ReportError("unsupported AArch32 relocation:" 2796 " .rel{0}[{1}], type {2}", 2797 rel_section->GetName().AsCString(), i, 2798 reloc_type(rel)); 2799 break; 2800 default: 2801 assert(false && "unexpected relocation type"); 2802 } 2803 break; 2804 case llvm::ELF::EM_386: 2805 switch (reloc_type(rel)) { 2806 case R_386_32: 2807 symbol = symtab->FindSymbolByID(reloc_symbol(rel)); 2808 if (symbol) { 2809 addr_t f_offset = 2810 rel_section->GetFileOffset() + ELFRelocation::RelocOffset32(rel); 2811 DataBufferSP &data_buffer_sp = debug_data.GetSharedDataBuffer(); 2812 // ObjectFileELF creates a WritableDataBuffer in CreateInstance. 2813 WritableDataBuffer *data_buffer = 2814 llvm::cast<WritableDataBuffer>(data_buffer_sp.get()); 2815 uint32_t *dst = reinterpret_cast<uint32_t *>( 2816 data_buffer->GetBytes() + f_offset); 2817 2818 addr_t value = symbol->GetAddressRef().GetFileAddress(); 2819 if (rel.IsRela()) { 2820 value += ELFRelocation::RelocAddend32(rel); 2821 } else { 2822 value += *dst; 2823 } 2824 *dst = value; 2825 } else { 2826 GetModule()->ReportError(".rel{0}[{1}] unknown symbol id: {2:d}", 2827 rel_section->GetName().AsCString(), i, 2828 reloc_symbol(rel)); 2829 } 2830 break; 2831 case R_386_NONE: 2832 case R_386_PC32: 2833 GetModule()->ReportError("unsupported i386 relocation:" 2834 " .rel{0}[{1}], type {2}", 2835 rel_section->GetName().AsCString(), i, 2836 reloc_type(rel)); 2837 break; 2838 default: 2839 assert(false && "unexpected relocation type"); 2840 break; 2841 } 2842 break; 2843 default: 2844 GetModule()->ReportError("unsupported 32-bit ELF machine arch: {0}", hdr->e_machine); 2845 break; 2846 } 2847 } else { 2848 switch (hdr->e_machine) { 2849 case llvm::ELF::EM_AARCH64: 2850 switch (reloc_type(rel)) { 2851 case R_AARCH64_ABS64: 2852 ApplyELF64ABS64Relocation(symtab, rel, debug_data, rel_section); 2853 break; 2854 case R_AARCH64_ABS32: 2855 ApplyELF64ABS32Relocation(symtab, rel, debug_data, rel_section, true); 2856 break; 2857 default: 2858 assert(false && "unexpected relocation type"); 2859 } 2860 break; 2861 case llvm::ELF::EM_LOONGARCH: 2862 switch (reloc_type(rel)) { 2863 case R_LARCH_64: 2864 ApplyELF64ABS64Relocation(symtab, rel, debug_data, rel_section); 2865 break; 2866 case R_LARCH_32: 2867 ApplyELF64ABS32Relocation(symtab, rel, debug_data, rel_section, true); 2868 break; 2869 default: 2870 assert(false && "unexpected relocation type"); 2871 } 2872 break; 2873 case llvm::ELF::EM_X86_64: 2874 switch (reloc_type(rel)) { 2875 case R_X86_64_64: 2876 ApplyELF64ABS64Relocation(symtab, rel, debug_data, rel_section); 2877 break; 2878 case R_X86_64_32: 2879 ApplyELF64ABS32Relocation(symtab, rel, debug_data, rel_section, 2880 false); 2881 break; 2882 case R_X86_64_32S: 2883 ApplyELF64ABS32Relocation(symtab, rel, debug_data, rel_section, true); 2884 break; 2885 case R_X86_64_PC32: 2886 default: 2887 assert(false && "unexpected relocation type"); 2888 } 2889 break; 2890 default: 2891 GetModule()->ReportError("unsupported 64-bit ELF machine arch: {0}", hdr->e_machine); 2892 break; 2893 } 2894 } 2895 } 2896 2897 return 0; 2898 } 2899 2900 unsigned ObjectFileELF::RelocateDebugSections(const ELFSectionHeader *rel_hdr, 2901 user_id_t rel_id, 2902 lldb_private::Symtab *thetab) { 2903 assert(rel_hdr->sh_type == SHT_RELA || rel_hdr->sh_type == SHT_REL); 2904 2905 // Parse in the section list if needed. 2906 SectionList *section_list = GetSectionList(); 2907 if (!section_list) 2908 return 0; 2909 2910 user_id_t symtab_id = rel_hdr->sh_link; 2911 user_id_t debug_id = rel_hdr->sh_info; 2912 2913 const ELFSectionHeader *symtab_hdr = GetSectionHeaderByIndex(symtab_id); 2914 if (!symtab_hdr) 2915 return 0; 2916 2917 const ELFSectionHeader *debug_hdr = GetSectionHeaderByIndex(debug_id); 2918 if (!debug_hdr) 2919 return 0; 2920 2921 Section *rel = section_list->FindSectionByID(rel_id).get(); 2922 if (!rel) 2923 return 0; 2924 2925 Section *symtab = section_list->FindSectionByID(symtab_id).get(); 2926 if (!symtab) 2927 return 0; 2928 2929 Section *debug = section_list->FindSectionByID(debug_id).get(); 2930 if (!debug) 2931 return 0; 2932 2933 DataExtractor rel_data; 2934 DataExtractor symtab_data; 2935 DataExtractor debug_data; 2936 2937 if (GetData(rel->GetFileOffset(), rel->GetFileSize(), rel_data) && 2938 GetData(symtab->GetFileOffset(), symtab->GetFileSize(), symtab_data) && 2939 GetData(debug->GetFileOffset(), debug->GetFileSize(), debug_data)) { 2940 ApplyRelocations(thetab, &m_header, rel_hdr, symtab_hdr, debug_hdr, 2941 rel_data, symtab_data, debug_data, debug); 2942 } 2943 2944 return 0; 2945 } 2946 2947 void ObjectFileELF::ParseSymtab(Symtab &lldb_symtab) { 2948 ModuleSP module_sp(GetModule()); 2949 if (!module_sp) 2950 return; 2951 2952 Progress progress("Parsing symbol table", 2953 m_file.GetFilename().AsCString("<Unknown>")); 2954 ElapsedTime elapsed(module_sp->GetSymtabParseTime()); 2955 2956 // We always want to use the main object file so we (hopefully) only have one 2957 // cached copy of our symtab, dynamic sections, etc. 2958 ObjectFile *module_obj_file = module_sp->GetObjectFile(); 2959 if (module_obj_file && module_obj_file != this) 2960 return module_obj_file->ParseSymtab(lldb_symtab); 2961 2962 SectionList *section_list = module_sp->GetSectionList(); 2963 if (!section_list) 2964 return; 2965 2966 uint64_t symbol_id = 0; 2967 2968 // Sharable objects and dynamic executables usually have 2 distinct symbol 2969 // tables, one named ".symtab", and the other ".dynsym". The dynsym is a 2970 // smaller version of the symtab that only contains global symbols. The 2971 // information found in the dynsym is therefore also found in the symtab, 2972 // while the reverse is not necessarily true. 2973 Section *symtab = 2974 section_list->FindSectionByType(eSectionTypeELFSymbolTable, true).get(); 2975 if (symtab) 2976 symbol_id += ParseSymbolTable(&lldb_symtab, symbol_id, symtab); 2977 2978 // The symtab section is non-allocable and can be stripped, while the 2979 // .dynsym section which should always be always be there. To support the 2980 // minidebuginfo case we parse .dynsym when there's a .gnu_debuginfo 2981 // section, nomatter if .symtab was already parsed or not. This is because 2982 // minidebuginfo normally removes the .symtab symbols which have their 2983 // matching .dynsym counterparts. 2984 if (!symtab || 2985 GetSectionList()->FindSectionByName(ConstString(".gnu_debugdata"))) { 2986 Section *dynsym = 2987 section_list->FindSectionByType(eSectionTypeELFDynamicSymbols, true) 2988 .get(); 2989 if (dynsym) 2990 symbol_id += ParseSymbolTable(&lldb_symtab, symbol_id, dynsym); 2991 } 2992 2993 // DT_JMPREL 2994 // If present, this entry's d_ptr member holds the address of 2995 // relocation 2996 // entries associated solely with the procedure linkage table. 2997 // Separating 2998 // these relocation entries lets the dynamic linker ignore them during 2999 // process initialization, if lazy binding is enabled. If this entry is 3000 // present, the related entries of types DT_PLTRELSZ and DT_PLTREL must 3001 // also be present. 3002 const ELFDynamic *symbol = FindDynamicSymbol(DT_JMPREL); 3003 if (symbol) { 3004 // Synthesize trampoline symbols to help navigate the PLT. 3005 addr_t addr = symbol->d_ptr; 3006 Section *reloc_section = 3007 section_list->FindSectionContainingFileAddress(addr).get(); 3008 if (reloc_section) { 3009 user_id_t reloc_id = reloc_section->GetID(); 3010 const ELFSectionHeaderInfo *reloc_header = 3011 GetSectionHeaderByIndex(reloc_id); 3012 if (reloc_header) 3013 ParseTrampolineSymbols(&lldb_symtab, symbol_id, reloc_header, reloc_id); 3014 } 3015 } 3016 3017 if (DWARFCallFrameInfo *eh_frame = 3018 GetModule()->GetUnwindTable().GetEHFrameInfo()) { 3019 ParseUnwindSymbols(&lldb_symtab, eh_frame); 3020 } 3021 3022 // In the event that there's no symbol entry for the entry point we'll 3023 // artificially create one. We delegate to the symtab object the figuring 3024 // out of the proper size, this will usually make it span til the next 3025 // symbol it finds in the section. This means that if there are missing 3026 // symbols the entry point might span beyond its function definition. 3027 // We're fine with this as it doesn't make it worse than not having a 3028 // symbol entry at all. 3029 if (CalculateType() == eTypeExecutable) { 3030 ArchSpec arch = GetArchitecture(); 3031 auto entry_point_addr = GetEntryPointAddress(); 3032 bool is_valid_entry_point = 3033 entry_point_addr.IsValid() && entry_point_addr.IsSectionOffset(); 3034 addr_t entry_point_file_addr = entry_point_addr.GetFileAddress(); 3035 if (is_valid_entry_point && !lldb_symtab.FindSymbolContainingFileAddress( 3036 entry_point_file_addr)) { 3037 uint64_t symbol_id = lldb_symtab.GetNumSymbols(); 3038 // Don't set the name for any synthetic symbols, the Symbol 3039 // object will generate one if needed when the name is accessed 3040 // via accessors. 3041 SectionSP section_sp = entry_point_addr.GetSection(); 3042 Symbol symbol( 3043 /*symID=*/symbol_id, 3044 /*name=*/llvm::StringRef(), // Name will be auto generated. 3045 /*type=*/eSymbolTypeCode, 3046 /*external=*/true, 3047 /*is_debug=*/false, 3048 /*is_trampoline=*/false, 3049 /*is_artificial=*/true, 3050 /*section_sp=*/section_sp, 3051 /*offset=*/0, 3052 /*size=*/0, // FDE can span multiple symbols so don't use its size. 3053 /*size_is_valid=*/false, 3054 /*contains_linker_annotations=*/false, 3055 /*flags=*/0); 3056 // When the entry point is arm thumb we need to explicitly set its 3057 // class address to reflect that. This is important because expression 3058 // evaluation relies on correctly setting a breakpoint at this 3059 // address. 3060 if (arch.GetMachine() == llvm::Triple::arm && 3061 (entry_point_file_addr & 1)) { 3062 symbol.GetAddressRef().SetOffset(entry_point_addr.GetOffset() ^ 1); 3063 m_address_class_map[entry_point_file_addr ^ 1] = 3064 AddressClass::eCodeAlternateISA; 3065 } else { 3066 m_address_class_map[entry_point_file_addr] = AddressClass::eCode; 3067 } 3068 lldb_symtab.AddSymbol(symbol); 3069 } 3070 } 3071 } 3072 3073 void ObjectFileELF::RelocateSection(lldb_private::Section *section) 3074 { 3075 static const char *debug_prefix = ".debug"; 3076 3077 // Set relocated bit so we stop getting called, regardless of whether we 3078 // actually relocate. 3079 section->SetIsRelocated(true); 3080 3081 // We only relocate in ELF relocatable files 3082 if (CalculateType() != eTypeObjectFile) 3083 return; 3084 3085 const char *section_name = section->GetName().GetCString(); 3086 // Can't relocate that which can't be named 3087 if (section_name == nullptr) 3088 return; 3089 3090 // We don't relocate non-debug sections at the moment 3091 if (strncmp(section_name, debug_prefix, strlen(debug_prefix))) 3092 return; 3093 3094 // Relocation section names to look for 3095 std::string needle = std::string(".rel") + section_name; 3096 std::string needlea = std::string(".rela") + section_name; 3097 3098 for (SectionHeaderCollIter I = m_section_headers.begin(); 3099 I != m_section_headers.end(); ++I) { 3100 if (I->sh_type == SHT_RELA || I->sh_type == SHT_REL) { 3101 const char *hay_name = I->section_name.GetCString(); 3102 if (hay_name == nullptr) 3103 continue; 3104 if (needle == hay_name || needlea == hay_name) { 3105 const ELFSectionHeader &reloc_header = *I; 3106 user_id_t reloc_id = SectionIndex(I); 3107 RelocateDebugSections(&reloc_header, reloc_id, GetSymtab()); 3108 break; 3109 } 3110 } 3111 } 3112 } 3113 3114 void ObjectFileELF::ParseUnwindSymbols(Symtab *symbol_table, 3115 DWARFCallFrameInfo *eh_frame) { 3116 SectionList *section_list = GetSectionList(); 3117 if (!section_list) 3118 return; 3119 3120 // First we save the new symbols into a separate list and add them to the 3121 // symbol table after we collected all symbols we want to add. This is 3122 // neccessary because adding a new symbol invalidates the internal index of 3123 // the symtab what causing the next lookup to be slow because it have to 3124 // recalculate the index first. 3125 std::vector<Symbol> new_symbols; 3126 3127 size_t num_symbols = symbol_table->GetNumSymbols(); 3128 uint64_t last_symbol_id = 3129 num_symbols ? symbol_table->SymbolAtIndex(num_symbols - 1)->GetID() : 0; 3130 eh_frame->ForEachFDEEntries([&](lldb::addr_t file_addr, uint32_t size, 3131 dw_offset_t) { 3132 Symbol *symbol = symbol_table->FindSymbolAtFileAddress(file_addr); 3133 if (symbol) { 3134 if (!symbol->GetByteSizeIsValid()) { 3135 symbol->SetByteSize(size); 3136 symbol->SetSizeIsSynthesized(true); 3137 } 3138 } else { 3139 SectionSP section_sp = 3140 section_list->FindSectionContainingFileAddress(file_addr); 3141 if (section_sp) { 3142 addr_t offset = file_addr - section_sp->GetFileAddress(); 3143 uint64_t symbol_id = ++last_symbol_id; 3144 // Don't set the name for any synthetic symbols, the Symbol 3145 // object will generate one if needed when the name is accessed 3146 // via accessors. 3147 Symbol eh_symbol( 3148 /*symID=*/symbol_id, 3149 /*name=*/llvm::StringRef(), // Name will be auto generated. 3150 /*type=*/eSymbolTypeCode, 3151 /*external=*/true, 3152 /*is_debug=*/false, 3153 /*is_trampoline=*/false, 3154 /*is_artificial=*/true, 3155 /*section_sp=*/section_sp, 3156 /*offset=*/offset, 3157 /*size=*/0, // FDE can span multiple symbols so don't use its size. 3158 /*size_is_valid=*/false, 3159 /*contains_linker_annotations=*/false, 3160 /*flags=*/0); 3161 new_symbols.push_back(eh_symbol); 3162 } 3163 } 3164 return true; 3165 }); 3166 3167 for (const Symbol &s : new_symbols) 3168 symbol_table->AddSymbol(s); 3169 } 3170 3171 bool ObjectFileELF::IsStripped() { 3172 // TODO: determine this for ELF 3173 return false; 3174 } 3175 3176 //===----------------------------------------------------------------------===// 3177 // Dump 3178 // 3179 // Dump the specifics of the runtime file container (such as any headers 3180 // segments, sections, etc). 3181 void ObjectFileELF::Dump(Stream *s) { 3182 ModuleSP module_sp(GetModule()); 3183 if (!module_sp) { 3184 return; 3185 } 3186 3187 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); 3188 s->Printf("%p: ", static_cast<void *>(this)); 3189 s->Indent(); 3190 s->PutCString("ObjectFileELF"); 3191 3192 ArchSpec header_arch = GetArchitecture(); 3193 3194 *s << ", file = '" << m_file 3195 << "', arch = " << header_arch.GetArchitectureName() << "\n"; 3196 3197 DumpELFHeader(s, m_header); 3198 s->EOL(); 3199 DumpELFProgramHeaders(s); 3200 s->EOL(); 3201 DumpELFSectionHeaders(s); 3202 s->EOL(); 3203 SectionList *section_list = GetSectionList(); 3204 if (section_list) 3205 section_list->Dump(s->AsRawOstream(), s->GetIndentLevel(), nullptr, true, 3206 UINT32_MAX); 3207 Symtab *symtab = GetSymtab(); 3208 if (symtab) 3209 symtab->Dump(s, nullptr, eSortOrderNone); 3210 s->EOL(); 3211 DumpDependentModules(s); 3212 s->EOL(); 3213 } 3214 3215 // DumpELFHeader 3216 // 3217 // Dump the ELF header to the specified output stream 3218 void ObjectFileELF::DumpELFHeader(Stream *s, const ELFHeader &header) { 3219 s->PutCString("ELF Header\n"); 3220 s->Printf("e_ident[EI_MAG0 ] = 0x%2.2x\n", header.e_ident[EI_MAG0]); 3221 s->Printf("e_ident[EI_MAG1 ] = 0x%2.2x '%c'\n", header.e_ident[EI_MAG1], 3222 header.e_ident[EI_MAG1]); 3223 s->Printf("e_ident[EI_MAG2 ] = 0x%2.2x '%c'\n", header.e_ident[EI_MAG2], 3224 header.e_ident[EI_MAG2]); 3225 s->Printf("e_ident[EI_MAG3 ] = 0x%2.2x '%c'\n", header.e_ident[EI_MAG3], 3226 header.e_ident[EI_MAG3]); 3227 3228 s->Printf("e_ident[EI_CLASS ] = 0x%2.2x\n", header.e_ident[EI_CLASS]); 3229 s->Printf("e_ident[EI_DATA ] = 0x%2.2x ", header.e_ident[EI_DATA]); 3230 DumpELFHeader_e_ident_EI_DATA(s, header.e_ident[EI_DATA]); 3231 s->Printf("\ne_ident[EI_VERSION] = 0x%2.2x\n", header.e_ident[EI_VERSION]); 3232 s->Printf("e_ident[EI_PAD ] = 0x%2.2x\n", header.e_ident[EI_PAD]); 3233 3234 s->Printf("e_type = 0x%4.4x ", header.e_type); 3235 DumpELFHeader_e_type(s, header.e_type); 3236 s->Printf("\ne_machine = 0x%4.4x\n", header.e_machine); 3237 s->Printf("e_version = 0x%8.8x\n", header.e_version); 3238 s->Printf("e_entry = 0x%8.8" PRIx64 "\n", header.e_entry); 3239 s->Printf("e_phoff = 0x%8.8" PRIx64 "\n", header.e_phoff); 3240 s->Printf("e_shoff = 0x%8.8" PRIx64 "\n", header.e_shoff); 3241 s->Printf("e_flags = 0x%8.8x\n", header.e_flags); 3242 s->Printf("e_ehsize = 0x%4.4x\n", header.e_ehsize); 3243 s->Printf("e_phentsize = 0x%4.4x\n", header.e_phentsize); 3244 s->Printf("e_phnum = 0x%8.8x\n", header.e_phnum); 3245 s->Printf("e_shentsize = 0x%4.4x\n", header.e_shentsize); 3246 s->Printf("e_shnum = 0x%8.8x\n", header.e_shnum); 3247 s->Printf("e_shstrndx = 0x%8.8x\n", header.e_shstrndx); 3248 } 3249 3250 // DumpELFHeader_e_type 3251 // 3252 // Dump an token value for the ELF header member e_type 3253 void ObjectFileELF::DumpELFHeader_e_type(Stream *s, elf_half e_type) { 3254 switch (e_type) { 3255 case ET_NONE: 3256 *s << "ET_NONE"; 3257 break; 3258 case ET_REL: 3259 *s << "ET_REL"; 3260 break; 3261 case ET_EXEC: 3262 *s << "ET_EXEC"; 3263 break; 3264 case ET_DYN: 3265 *s << "ET_DYN"; 3266 break; 3267 case ET_CORE: 3268 *s << "ET_CORE"; 3269 break; 3270 default: 3271 break; 3272 } 3273 } 3274 3275 // DumpELFHeader_e_ident_EI_DATA 3276 // 3277 // Dump an token value for the ELF header member e_ident[EI_DATA] 3278 void ObjectFileELF::DumpELFHeader_e_ident_EI_DATA(Stream *s, 3279 unsigned char ei_data) { 3280 switch (ei_data) { 3281 case ELFDATANONE: 3282 *s << "ELFDATANONE"; 3283 break; 3284 case ELFDATA2LSB: 3285 *s << "ELFDATA2LSB - Little Endian"; 3286 break; 3287 case ELFDATA2MSB: 3288 *s << "ELFDATA2MSB - Big Endian"; 3289 break; 3290 default: 3291 break; 3292 } 3293 } 3294 3295 // DumpELFProgramHeader 3296 // 3297 // Dump a single ELF program header to the specified output stream 3298 void ObjectFileELF::DumpELFProgramHeader(Stream *s, 3299 const ELFProgramHeader &ph) { 3300 DumpELFProgramHeader_p_type(s, ph.p_type); 3301 s->Printf(" %8.8" PRIx64 " %8.8" PRIx64 " %8.8" PRIx64, ph.p_offset, 3302 ph.p_vaddr, ph.p_paddr); 3303 s->Printf(" %8.8" PRIx64 " %8.8" PRIx64 " %8.8x (", ph.p_filesz, ph.p_memsz, 3304 ph.p_flags); 3305 3306 DumpELFProgramHeader_p_flags(s, ph.p_flags); 3307 s->Printf(") %8.8" PRIx64, ph.p_align); 3308 } 3309 3310 // DumpELFProgramHeader_p_type 3311 // 3312 // Dump an token value for the ELF program header member p_type which describes 3313 // the type of the program header 3314 void ObjectFileELF::DumpELFProgramHeader_p_type(Stream *s, elf_word p_type) { 3315 const int kStrWidth = 15; 3316 switch (p_type) { 3317 CASE_AND_STREAM(s, PT_NULL, kStrWidth); 3318 CASE_AND_STREAM(s, PT_LOAD, kStrWidth); 3319 CASE_AND_STREAM(s, PT_DYNAMIC, kStrWidth); 3320 CASE_AND_STREAM(s, PT_INTERP, kStrWidth); 3321 CASE_AND_STREAM(s, PT_NOTE, kStrWidth); 3322 CASE_AND_STREAM(s, PT_SHLIB, kStrWidth); 3323 CASE_AND_STREAM(s, PT_PHDR, kStrWidth); 3324 CASE_AND_STREAM(s, PT_TLS, kStrWidth); 3325 CASE_AND_STREAM(s, PT_GNU_EH_FRAME, kStrWidth); 3326 default: 3327 s->Printf("0x%8.8x%*s", p_type, kStrWidth - 10, ""); 3328 break; 3329 } 3330 } 3331 3332 // DumpELFProgramHeader_p_flags 3333 // 3334 // Dump an token value for the ELF program header member p_flags 3335 void ObjectFileELF::DumpELFProgramHeader_p_flags(Stream *s, elf_word p_flags) { 3336 *s << ((p_flags & PF_X) ? "PF_X" : " ") 3337 << (((p_flags & PF_X) && (p_flags & PF_W)) ? '+' : ' ') 3338 << ((p_flags & PF_W) ? "PF_W" : " ") 3339 << (((p_flags & PF_W) && (p_flags & PF_R)) ? '+' : ' ') 3340 << ((p_flags & PF_R) ? "PF_R" : " "); 3341 } 3342 3343 // DumpELFProgramHeaders 3344 // 3345 // Dump all of the ELF program header to the specified output stream 3346 void ObjectFileELF::DumpELFProgramHeaders(Stream *s) { 3347 if (!ParseProgramHeaders()) 3348 return; 3349 3350 s->PutCString("Program Headers\n"); 3351 s->PutCString("IDX p_type p_offset p_vaddr p_paddr " 3352 "p_filesz p_memsz p_flags p_align\n"); 3353 s->PutCString("==== --------------- -------- -------- -------- " 3354 "-------- -------- ------------------------- --------\n"); 3355 3356 for (const auto &H : llvm::enumerate(m_program_headers)) { 3357 s->Format("[{0,2}] ", H.index()); 3358 ObjectFileELF::DumpELFProgramHeader(s, H.value()); 3359 s->EOL(); 3360 } 3361 } 3362 3363 // DumpELFSectionHeader 3364 // 3365 // Dump a single ELF section header to the specified output stream 3366 void ObjectFileELF::DumpELFSectionHeader(Stream *s, 3367 const ELFSectionHeaderInfo &sh) { 3368 s->Printf("%8.8x ", sh.sh_name); 3369 DumpELFSectionHeader_sh_type(s, sh.sh_type); 3370 s->Printf(" %8.8" PRIx64 " (", sh.sh_flags); 3371 DumpELFSectionHeader_sh_flags(s, sh.sh_flags); 3372 s->Printf(") %8.8" PRIx64 " %8.8" PRIx64 " %8.8" PRIx64, sh.sh_addr, 3373 sh.sh_offset, sh.sh_size); 3374 s->Printf(" %8.8x %8.8x", sh.sh_link, sh.sh_info); 3375 s->Printf(" %8.8" PRIx64 " %8.8" PRIx64, sh.sh_addralign, sh.sh_entsize); 3376 } 3377 3378 // DumpELFSectionHeader_sh_type 3379 // 3380 // Dump an token value for the ELF section header member sh_type which 3381 // describes the type of the section 3382 void ObjectFileELF::DumpELFSectionHeader_sh_type(Stream *s, elf_word sh_type) { 3383 const int kStrWidth = 12; 3384 switch (sh_type) { 3385 CASE_AND_STREAM(s, SHT_NULL, kStrWidth); 3386 CASE_AND_STREAM(s, SHT_PROGBITS, kStrWidth); 3387 CASE_AND_STREAM(s, SHT_SYMTAB, kStrWidth); 3388 CASE_AND_STREAM(s, SHT_STRTAB, kStrWidth); 3389 CASE_AND_STREAM(s, SHT_RELA, kStrWidth); 3390 CASE_AND_STREAM(s, SHT_HASH, kStrWidth); 3391 CASE_AND_STREAM(s, SHT_DYNAMIC, kStrWidth); 3392 CASE_AND_STREAM(s, SHT_NOTE, kStrWidth); 3393 CASE_AND_STREAM(s, SHT_NOBITS, kStrWidth); 3394 CASE_AND_STREAM(s, SHT_REL, kStrWidth); 3395 CASE_AND_STREAM(s, SHT_SHLIB, kStrWidth); 3396 CASE_AND_STREAM(s, SHT_DYNSYM, kStrWidth); 3397 CASE_AND_STREAM(s, SHT_LOPROC, kStrWidth); 3398 CASE_AND_STREAM(s, SHT_HIPROC, kStrWidth); 3399 CASE_AND_STREAM(s, SHT_LOUSER, kStrWidth); 3400 CASE_AND_STREAM(s, SHT_HIUSER, kStrWidth); 3401 default: 3402 s->Printf("0x%8.8x%*s", sh_type, kStrWidth - 10, ""); 3403 break; 3404 } 3405 } 3406 3407 // DumpELFSectionHeader_sh_flags 3408 // 3409 // Dump an token value for the ELF section header member sh_flags 3410 void ObjectFileELF::DumpELFSectionHeader_sh_flags(Stream *s, 3411 elf_xword sh_flags) { 3412 *s << ((sh_flags & SHF_WRITE) ? "WRITE" : " ") 3413 << (((sh_flags & SHF_WRITE) && (sh_flags & SHF_ALLOC)) ? '+' : ' ') 3414 << ((sh_flags & SHF_ALLOC) ? "ALLOC" : " ") 3415 << (((sh_flags & SHF_ALLOC) && (sh_flags & SHF_EXECINSTR)) ? '+' : ' ') 3416 << ((sh_flags & SHF_EXECINSTR) ? "EXECINSTR" : " "); 3417 } 3418 3419 // DumpELFSectionHeaders 3420 // 3421 // Dump all of the ELF section header to the specified output stream 3422 void ObjectFileELF::DumpELFSectionHeaders(Stream *s) { 3423 if (!ParseSectionHeaders()) 3424 return; 3425 3426 s->PutCString("Section Headers\n"); 3427 s->PutCString("IDX name type flags " 3428 "addr offset size link info addralgn " 3429 "entsize Name\n"); 3430 s->PutCString("==== -------- ------------ -------------------------------- " 3431 "-------- -------- -------- -------- -------- -------- " 3432 "-------- ====================\n"); 3433 3434 uint32_t idx = 0; 3435 for (SectionHeaderCollConstIter I = m_section_headers.begin(); 3436 I != m_section_headers.end(); ++I, ++idx) { 3437 s->Printf("[%2u] ", idx); 3438 ObjectFileELF::DumpELFSectionHeader(s, *I); 3439 const char *section_name = I->section_name.AsCString(""); 3440 if (section_name) 3441 *s << ' ' << section_name << "\n"; 3442 } 3443 } 3444 3445 void ObjectFileELF::DumpDependentModules(lldb_private::Stream *s) { 3446 size_t num_modules = ParseDependentModules(); 3447 3448 if (num_modules > 0) { 3449 s->PutCString("Dependent Modules:\n"); 3450 for (unsigned i = 0; i < num_modules; ++i) { 3451 const FileSpec &spec = m_filespec_up->GetFileSpecAtIndex(i); 3452 s->Printf(" %s\n", spec.GetFilename().GetCString()); 3453 } 3454 } 3455 } 3456 3457 ArchSpec ObjectFileELF::GetArchitecture() { 3458 if (!ParseHeader()) 3459 return ArchSpec(); 3460 3461 if (m_section_headers.empty()) { 3462 // Allow elf notes to be parsed which may affect the detected architecture. 3463 ParseSectionHeaders(); 3464 } 3465 3466 if (CalculateType() == eTypeCoreFile && 3467 !m_arch_spec.TripleOSWasSpecified()) { 3468 // Core files don't have section headers yet they have PT_NOTE program 3469 // headers that might shed more light on the architecture 3470 for (const elf::ELFProgramHeader &H : ProgramHeaders()) { 3471 if (H.p_type != PT_NOTE || H.p_offset == 0 || H.p_filesz == 0) 3472 continue; 3473 DataExtractor data; 3474 if (data.SetData(m_data, H.p_offset, H.p_filesz) == H.p_filesz) { 3475 UUID uuid; 3476 RefineModuleDetailsFromNote(data, m_arch_spec, uuid); 3477 } 3478 } 3479 } 3480 return m_arch_spec; 3481 } 3482 3483 ObjectFile::Type ObjectFileELF::CalculateType() { 3484 switch (m_header.e_type) { 3485 case llvm::ELF::ET_NONE: 3486 // 0 - No file type 3487 return eTypeUnknown; 3488 3489 case llvm::ELF::ET_REL: 3490 // 1 - Relocatable file 3491 return eTypeObjectFile; 3492 3493 case llvm::ELF::ET_EXEC: 3494 // 2 - Executable file 3495 return eTypeExecutable; 3496 3497 case llvm::ELF::ET_DYN: 3498 // 3 - Shared object file 3499 return eTypeSharedLibrary; 3500 3501 case ET_CORE: 3502 // 4 - Core file 3503 return eTypeCoreFile; 3504 3505 default: 3506 break; 3507 } 3508 return eTypeUnknown; 3509 } 3510 3511 ObjectFile::Strata ObjectFileELF::CalculateStrata() { 3512 switch (m_header.e_type) { 3513 case llvm::ELF::ET_NONE: 3514 // 0 - No file type 3515 return eStrataUnknown; 3516 3517 case llvm::ELF::ET_REL: 3518 // 1 - Relocatable file 3519 return eStrataUnknown; 3520 3521 case llvm::ELF::ET_EXEC: 3522 // 2 - Executable file 3523 { 3524 SectionList *section_list = GetSectionList(); 3525 if (section_list) { 3526 static ConstString loader_section_name(".interp"); 3527 SectionSP loader_section = 3528 section_list->FindSectionByName(loader_section_name); 3529 if (loader_section) { 3530 char buffer[256]; 3531 size_t read_size = 3532 ReadSectionData(loader_section.get(), 0, buffer, sizeof(buffer)); 3533 3534 // We compare the content of .interp section 3535 // It will contains \0 when counting read_size, so the size needs to 3536 // decrease by one 3537 llvm::StringRef loader_name(buffer, read_size - 1); 3538 llvm::StringRef freebsd_kernel_loader_name("/red/herring"); 3539 if (loader_name.equals(freebsd_kernel_loader_name)) 3540 return eStrataKernel; 3541 } 3542 } 3543 return eStrataUser; 3544 } 3545 3546 case llvm::ELF::ET_DYN: 3547 // 3 - Shared object file 3548 // TODO: is there any way to detect that an shared library is a kernel 3549 // related executable by inspecting the program headers, section headers, 3550 // symbols, or any other flag bits??? 3551 return eStrataUnknown; 3552 3553 case ET_CORE: 3554 // 4 - Core file 3555 // TODO: is there any way to detect that an core file is a kernel 3556 // related executable by inspecting the program headers, section headers, 3557 // symbols, or any other flag bits??? 3558 return eStrataUnknown; 3559 3560 default: 3561 break; 3562 } 3563 return eStrataUnknown; 3564 } 3565 3566 size_t ObjectFileELF::ReadSectionData(Section *section, 3567 lldb::offset_t section_offset, void *dst, 3568 size_t dst_len) { 3569 // If some other objectfile owns this data, pass this to them. 3570 if (section->GetObjectFile() != this) 3571 return section->GetObjectFile()->ReadSectionData(section, section_offset, 3572 dst, dst_len); 3573 3574 if (!section->Test(SHF_COMPRESSED)) 3575 return ObjectFile::ReadSectionData(section, section_offset, dst, dst_len); 3576 3577 // For compressed sections we need to read to full data to be able to 3578 // decompress. 3579 DataExtractor data; 3580 ReadSectionData(section, data); 3581 return data.CopyData(section_offset, dst_len, dst); 3582 } 3583 3584 size_t ObjectFileELF::ReadSectionData(Section *section, 3585 DataExtractor §ion_data) { 3586 // If some other objectfile owns this data, pass this to them. 3587 if (section->GetObjectFile() != this) 3588 return section->GetObjectFile()->ReadSectionData(section, section_data); 3589 3590 size_t result = ObjectFile::ReadSectionData(section, section_data); 3591 if (result == 0 || !(section->Get() & llvm::ELF::SHF_COMPRESSED)) 3592 return result; 3593 3594 auto Decompressor = llvm::object::Decompressor::create( 3595 section->GetName().GetStringRef(), 3596 {reinterpret_cast<const char *>(section_data.GetDataStart()), 3597 size_t(section_data.GetByteSize())}, 3598 GetByteOrder() == eByteOrderLittle, GetAddressByteSize() == 8); 3599 if (!Decompressor) { 3600 GetModule()->ReportWarning( 3601 "Unable to initialize decompressor for section '{0}': {1}", 3602 section->GetName().GetCString(), 3603 llvm::toString(Decompressor.takeError()).c_str()); 3604 section_data.Clear(); 3605 return 0; 3606 } 3607 3608 auto buffer_sp = 3609 std::make_shared<DataBufferHeap>(Decompressor->getDecompressedSize(), 0); 3610 if (auto error = Decompressor->decompress( 3611 {buffer_sp->GetBytes(), size_t(buffer_sp->GetByteSize())})) { 3612 GetModule()->ReportWarning("Decompression of section '{0}' failed: {1}", 3613 section->GetName().GetCString(), 3614 llvm::toString(std::move(error)).c_str()); 3615 section_data.Clear(); 3616 return 0; 3617 } 3618 3619 section_data.SetData(buffer_sp); 3620 return buffer_sp->GetByteSize(); 3621 } 3622 3623 llvm::ArrayRef<ELFProgramHeader> ObjectFileELF::ProgramHeaders() { 3624 ParseProgramHeaders(); 3625 return m_program_headers; 3626 } 3627 3628 DataExtractor ObjectFileELF::GetSegmentData(const ELFProgramHeader &H) { 3629 return DataExtractor(m_data, H.p_offset, H.p_filesz); 3630 } 3631 3632 bool ObjectFileELF::AnySegmentHasPhysicalAddress() { 3633 for (const ELFProgramHeader &H : ProgramHeaders()) { 3634 if (H.p_paddr != 0) 3635 return true; 3636 } 3637 return false; 3638 } 3639 3640 std::vector<ObjectFile::LoadableData> 3641 ObjectFileELF::GetLoadableData(Target &target) { 3642 // Create a list of loadable data from loadable segments, using physical 3643 // addresses if they aren't all null 3644 std::vector<LoadableData> loadables; 3645 bool should_use_paddr = AnySegmentHasPhysicalAddress(); 3646 for (const ELFProgramHeader &H : ProgramHeaders()) { 3647 LoadableData loadable; 3648 if (H.p_type != llvm::ELF::PT_LOAD) 3649 continue; 3650 loadable.Dest = should_use_paddr ? H.p_paddr : H.p_vaddr; 3651 if (loadable.Dest == LLDB_INVALID_ADDRESS) 3652 continue; 3653 if (H.p_filesz == 0) 3654 continue; 3655 auto segment_data = GetSegmentData(H); 3656 loadable.Contents = llvm::ArrayRef<uint8_t>(segment_data.GetDataStart(), 3657 segment_data.GetByteSize()); 3658 loadables.push_back(loadable); 3659 } 3660 return loadables; 3661 } 3662 3663 lldb::WritableDataBufferSP 3664 ObjectFileELF::MapFileDataWritable(const FileSpec &file, uint64_t Size, 3665 uint64_t Offset) { 3666 return FileSystem::Instance().CreateWritableDataBuffer(file.GetPath(), Size, 3667 Offset); 3668 } 3669