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 for (const auto &EnumPHdr : llvm::enumerate(ProgramHeaders())) { 939 const ELFProgramHeader &H = EnumPHdr.value(); 940 if (H.p_type != PT_LOAD) 941 continue; 942 943 return Address( 944 GetSectionList()->FindSectionByID(SegmentID(EnumPHdr.index())), 0); 945 } 946 return LLDB_INVALID_ADDRESS; 947 } 948 949 // ParseDependentModules 950 size_t ObjectFileELF::ParseDependentModules() { 951 if (m_filespec_up) 952 return m_filespec_up->GetSize(); 953 954 m_filespec_up = std::make_unique<FileSpecList>(); 955 956 if (!ParseSectionHeaders()) 957 return 0; 958 959 SectionList *section_list = GetSectionList(); 960 if (!section_list) 961 return 0; 962 963 // Find the SHT_DYNAMIC section. 964 Section *dynsym = 965 section_list->FindSectionByType(eSectionTypeELFDynamicLinkInfo, true) 966 .get(); 967 if (!dynsym) 968 return 0; 969 assert(dynsym->GetObjectFile() == this); 970 971 const ELFSectionHeaderInfo *header = GetSectionHeaderByIndex(dynsym->GetID()); 972 if (!header) 973 return 0; 974 // sh_link: section header index of string table used by entries in the 975 // section. 976 Section *dynstr = section_list->FindSectionByID(header->sh_link).get(); 977 if (!dynstr) 978 return 0; 979 980 DataExtractor dynsym_data; 981 DataExtractor dynstr_data; 982 if (ReadSectionData(dynsym, dynsym_data) && 983 ReadSectionData(dynstr, dynstr_data)) { 984 ELFDynamic symbol; 985 const lldb::offset_t section_size = dynsym_data.GetByteSize(); 986 lldb::offset_t offset = 0; 987 988 // The only type of entries we are concerned with are tagged DT_NEEDED, 989 // yielding the name of a required library. 990 while (offset < section_size) { 991 if (!symbol.Parse(dynsym_data, &offset)) 992 break; 993 994 if (symbol.d_tag != DT_NEEDED) 995 continue; 996 997 uint32_t str_index = static_cast<uint32_t>(symbol.d_val); 998 const char *lib_name = dynstr_data.PeekCStr(str_index); 999 FileSpec file_spec(lib_name); 1000 FileSystem::Instance().Resolve(file_spec); 1001 m_filespec_up->Append(file_spec); 1002 } 1003 } 1004 1005 return m_filespec_up->GetSize(); 1006 } 1007 1008 // GetProgramHeaderInfo 1009 size_t ObjectFileELF::GetProgramHeaderInfo(ProgramHeaderColl &program_headers, 1010 DataExtractor &object_data, 1011 const ELFHeader &header) { 1012 // We have already parsed the program headers 1013 if (!program_headers.empty()) 1014 return program_headers.size(); 1015 1016 // If there are no program headers to read we are done. 1017 if (header.e_phnum == 0) 1018 return 0; 1019 1020 program_headers.resize(header.e_phnum); 1021 if (program_headers.size() != header.e_phnum) 1022 return 0; 1023 1024 const size_t ph_size = header.e_phnum * header.e_phentsize; 1025 const elf_off ph_offset = header.e_phoff; 1026 DataExtractor data; 1027 if (data.SetData(object_data, ph_offset, ph_size) != ph_size) 1028 return 0; 1029 1030 uint32_t idx; 1031 lldb::offset_t offset; 1032 for (idx = 0, offset = 0; idx < header.e_phnum; ++idx) { 1033 if (!program_headers[idx].Parse(data, &offset)) 1034 break; 1035 } 1036 1037 if (idx < program_headers.size()) 1038 program_headers.resize(idx); 1039 1040 return program_headers.size(); 1041 } 1042 1043 // ParseProgramHeaders 1044 bool ObjectFileELF::ParseProgramHeaders() { 1045 return GetProgramHeaderInfo(m_program_headers, m_data, m_header) != 0; 1046 } 1047 1048 lldb_private::Status 1049 ObjectFileELF::RefineModuleDetailsFromNote(lldb_private::DataExtractor &data, 1050 lldb_private::ArchSpec &arch_spec, 1051 lldb_private::UUID &uuid) { 1052 Log *log = GetLog(LLDBLog::Modules); 1053 Status error; 1054 1055 lldb::offset_t offset = 0; 1056 1057 while (true) { 1058 // Parse the note header. If this fails, bail out. 1059 const lldb::offset_t note_offset = offset; 1060 ELFNote note = ELFNote(); 1061 if (!note.Parse(data, &offset)) { 1062 // We're done. 1063 return error; 1064 } 1065 1066 LLDB_LOGF(log, "ObjectFileELF::%s parsing note name='%s', type=%" PRIu32, 1067 __FUNCTION__, note.n_name.c_str(), note.n_type); 1068 1069 // Process FreeBSD ELF notes. 1070 if ((note.n_name == LLDB_NT_OWNER_FREEBSD) && 1071 (note.n_type == LLDB_NT_FREEBSD_ABI_TAG) && 1072 (note.n_descsz == LLDB_NT_FREEBSD_ABI_SIZE)) { 1073 // Pull out the min version info. 1074 uint32_t version_info; 1075 if (data.GetU32(&offset, &version_info, 1) == nullptr) { 1076 error.SetErrorString("failed to read FreeBSD ABI note payload"); 1077 return error; 1078 } 1079 1080 // Convert the version info into a major/minor number. 1081 const uint32_t version_major = version_info / 100000; 1082 const uint32_t version_minor = (version_info / 1000) % 100; 1083 1084 char os_name[32]; 1085 snprintf(os_name, sizeof(os_name), "freebsd%" PRIu32 ".%" PRIu32, 1086 version_major, version_minor); 1087 1088 // Set the elf OS version to FreeBSD. Also clear the vendor. 1089 arch_spec.GetTriple().setOSName(os_name); 1090 arch_spec.GetTriple().setVendor(llvm::Triple::VendorType::UnknownVendor); 1091 1092 LLDB_LOGF(log, 1093 "ObjectFileELF::%s detected FreeBSD %" PRIu32 ".%" PRIu32 1094 ".%" PRIu32, 1095 __FUNCTION__, version_major, version_minor, 1096 static_cast<uint32_t>(version_info % 1000)); 1097 } 1098 // Process GNU ELF notes. 1099 else if (note.n_name == LLDB_NT_OWNER_GNU) { 1100 switch (note.n_type) { 1101 case LLDB_NT_GNU_ABI_TAG: 1102 if (note.n_descsz == LLDB_NT_GNU_ABI_SIZE) { 1103 // Pull out the min OS version supporting the ABI. 1104 uint32_t version_info[4]; 1105 if (data.GetU32(&offset, &version_info[0], note.n_descsz / 4) == 1106 nullptr) { 1107 error.SetErrorString("failed to read GNU ABI note payload"); 1108 return error; 1109 } 1110 1111 // Set the OS per the OS field. 1112 switch (version_info[0]) { 1113 case LLDB_NT_GNU_ABI_OS_LINUX: 1114 arch_spec.GetTriple().setOS(llvm::Triple::OSType::Linux); 1115 arch_spec.GetTriple().setVendor( 1116 llvm::Triple::VendorType::UnknownVendor); 1117 LLDB_LOGF(log, 1118 "ObjectFileELF::%s detected Linux, min version %" PRIu32 1119 ".%" PRIu32 ".%" PRIu32, 1120 __FUNCTION__, version_info[1], version_info[2], 1121 version_info[3]); 1122 // FIXME we have the minimal version number, we could be propagating 1123 // that. version_info[1] = OS Major, version_info[2] = OS Minor, 1124 // version_info[3] = Revision. 1125 break; 1126 case LLDB_NT_GNU_ABI_OS_HURD: 1127 arch_spec.GetTriple().setOS(llvm::Triple::OSType::UnknownOS); 1128 arch_spec.GetTriple().setVendor( 1129 llvm::Triple::VendorType::UnknownVendor); 1130 LLDB_LOGF(log, 1131 "ObjectFileELF::%s detected Hurd (unsupported), min " 1132 "version %" PRIu32 ".%" PRIu32 ".%" PRIu32, 1133 __FUNCTION__, version_info[1], version_info[2], 1134 version_info[3]); 1135 break; 1136 case LLDB_NT_GNU_ABI_OS_SOLARIS: 1137 arch_spec.GetTriple().setOS(llvm::Triple::OSType::Solaris); 1138 arch_spec.GetTriple().setVendor( 1139 llvm::Triple::VendorType::UnknownVendor); 1140 LLDB_LOGF(log, 1141 "ObjectFileELF::%s detected Solaris, min version %" PRIu32 1142 ".%" PRIu32 ".%" PRIu32, 1143 __FUNCTION__, version_info[1], version_info[2], 1144 version_info[3]); 1145 break; 1146 default: 1147 LLDB_LOGF(log, 1148 "ObjectFileELF::%s unrecognized OS in note, id %" PRIu32 1149 ", min version %" PRIu32 ".%" PRIu32 ".%" PRIu32, 1150 __FUNCTION__, version_info[0], version_info[1], 1151 version_info[2], version_info[3]); 1152 break; 1153 } 1154 } 1155 break; 1156 1157 case LLDB_NT_GNU_BUILD_ID_TAG: 1158 // Only bother processing this if we don't already have the uuid set. 1159 if (!uuid.IsValid()) { 1160 // 16 bytes is UUID|MD5, 20 bytes is SHA1. Other linkers may produce a 1161 // build-id of a different length. Accept it as long as it's at least 1162 // 4 bytes as it will be better than our own crc32. 1163 if (note.n_descsz >= 4) { 1164 if (const uint8_t *buf = data.PeekData(offset, note.n_descsz)) { 1165 // Save the build id as the UUID for the module. 1166 uuid = UUID(buf, note.n_descsz); 1167 } else { 1168 error.SetErrorString("failed to read GNU_BUILD_ID note payload"); 1169 return error; 1170 } 1171 } 1172 } 1173 break; 1174 } 1175 if (arch_spec.IsMIPS() && 1176 arch_spec.GetTriple().getOS() == llvm::Triple::OSType::UnknownOS) 1177 // The note.n_name == LLDB_NT_OWNER_GNU is valid for Linux platform 1178 arch_spec.GetTriple().setOS(llvm::Triple::OSType::Linux); 1179 } 1180 // Process NetBSD ELF executables and shared libraries 1181 else if ((note.n_name == LLDB_NT_OWNER_NETBSD) && 1182 (note.n_type == LLDB_NT_NETBSD_IDENT_TAG) && 1183 (note.n_descsz == LLDB_NT_NETBSD_IDENT_DESCSZ) && 1184 (note.n_namesz == LLDB_NT_NETBSD_IDENT_NAMESZ)) { 1185 // Pull out the version info. 1186 uint32_t version_info; 1187 if (data.GetU32(&offset, &version_info, 1) == nullptr) { 1188 error.SetErrorString("failed to read NetBSD ABI note payload"); 1189 return error; 1190 } 1191 // Convert the version info into a major/minor/patch number. 1192 // #define __NetBSD_Version__ MMmmrrpp00 1193 // 1194 // M = major version 1195 // m = minor version; a minor number of 99 indicates current. 1196 // r = 0 (since NetBSD 3.0 not used) 1197 // p = patchlevel 1198 const uint32_t version_major = version_info / 100000000; 1199 const uint32_t version_minor = (version_info % 100000000) / 1000000; 1200 const uint32_t version_patch = (version_info % 10000) / 100; 1201 // Set the elf OS version to NetBSD. Also clear the vendor. 1202 arch_spec.GetTriple().setOSName( 1203 llvm::formatv("netbsd{0}.{1}.{2}", version_major, version_minor, 1204 version_patch).str()); 1205 arch_spec.GetTriple().setVendor(llvm::Triple::VendorType::UnknownVendor); 1206 } 1207 // Process NetBSD ELF core(5) notes 1208 else if ((note.n_name == LLDB_NT_OWNER_NETBSDCORE) && 1209 (note.n_type == LLDB_NT_NETBSD_PROCINFO)) { 1210 // Set the elf OS version to NetBSD. Also clear the vendor. 1211 arch_spec.GetTriple().setOS(llvm::Triple::OSType::NetBSD); 1212 arch_spec.GetTriple().setVendor(llvm::Triple::VendorType::UnknownVendor); 1213 } 1214 // Process OpenBSD ELF notes. 1215 else if (note.n_name == LLDB_NT_OWNER_OPENBSD) { 1216 // Set the elf OS version to OpenBSD. Also clear the vendor. 1217 arch_spec.GetTriple().setOS(llvm::Triple::OSType::OpenBSD); 1218 arch_spec.GetTriple().setVendor(llvm::Triple::VendorType::UnknownVendor); 1219 } else if (note.n_name == LLDB_NT_OWNER_ANDROID) { 1220 arch_spec.GetTriple().setOS(llvm::Triple::OSType::Linux); 1221 arch_spec.GetTriple().setEnvironment( 1222 llvm::Triple::EnvironmentType::Android); 1223 } else if (note.n_name == LLDB_NT_OWNER_LINUX) { 1224 // This is sometimes found in core files and usually contains extended 1225 // register info 1226 arch_spec.GetTriple().setOS(llvm::Triple::OSType::Linux); 1227 } else if (note.n_name == LLDB_NT_OWNER_CORE) { 1228 // Parse the NT_FILE to look for stuff in paths to shared libraries 1229 // The contents look like this in a 64 bit ELF core file: 1230 // 1231 // count = 0x000000000000000a (10) 1232 // page_size = 0x0000000000001000 (4096) 1233 // Index start end file_ofs path 1234 // ===== ------------------ ------------------ ------------------ ------------------------------------- 1235 // [ 0] 0x0000000000401000 0x0000000000000000 /tmp/a.out 1236 // [ 1] 0x0000000000600000 0x0000000000601000 0x0000000000000000 /tmp/a.out 1237 // [ 2] 0x0000000000601000 0x0000000000602000 0x0000000000000001 /tmp/a.out 1238 // [ 3] 0x00007fa79c9ed000 0x00007fa79cba8000 0x0000000000000000 /lib/x86_64-linux-gnu/libc-2.19.so 1239 // [ 4] 0x00007fa79cba8000 0x00007fa79cda7000 0x00000000000001bb /lib/x86_64-linux-gnu/libc-2.19.so 1240 // [ 5] 0x00007fa79cda7000 0x00007fa79cdab000 0x00000000000001ba /lib/x86_64-linux-gnu/libc-2.19.so 1241 // [ 6] 0x00007fa79cdab000 0x00007fa79cdad000 0x00000000000001be /lib/x86_64-linux-gnu/libc-2.19.so 1242 // [ 7] 0x00007fa79cdb2000 0x00007fa79cdd5000 0x0000000000000000 /lib/x86_64-linux-gnu/ld-2.19.so 1243 // [ 8] 0x00007fa79cfd4000 0x00007fa79cfd5000 0x0000000000000022 /lib/x86_64-linux-gnu/ld-2.19.so 1244 // [ 9] 0x00007fa79cfd5000 0x00007fa79cfd6000 0x0000000000000023 /lib/x86_64-linux-gnu/ld-2.19.so 1245 // 1246 // In the 32 bit ELFs the count, page_size, start, end, file_ofs are 1247 // uint32_t. 1248 // 1249 // For reference: see readelf source code (in binutils). 1250 if (note.n_type == NT_FILE) { 1251 uint64_t count = data.GetAddress(&offset); 1252 const char *cstr; 1253 data.GetAddress(&offset); // Skip page size 1254 offset += count * 3 * 1255 data.GetAddressByteSize(); // Skip all start/end/file_ofs 1256 for (size_t i = 0; i < count; ++i) { 1257 cstr = data.GetCStr(&offset); 1258 if (cstr == nullptr) { 1259 error.SetErrorStringWithFormat("ObjectFileELF::%s trying to read " 1260 "at an offset after the end " 1261 "(GetCStr returned nullptr)", 1262 __FUNCTION__); 1263 return error; 1264 } 1265 llvm::StringRef path(cstr); 1266 if (path.contains("/lib/x86_64-linux-gnu") || path.contains("/lib/i386-linux-gnu")) { 1267 arch_spec.GetTriple().setOS(llvm::Triple::OSType::Linux); 1268 break; 1269 } 1270 } 1271 if (arch_spec.IsMIPS() && 1272 arch_spec.GetTriple().getOS() == llvm::Triple::OSType::UnknownOS) 1273 // In case of MIPSR6, the LLDB_NT_OWNER_GNU note is missing for some 1274 // cases (e.g. compile with -nostdlib) Hence set OS to Linux 1275 arch_spec.GetTriple().setOS(llvm::Triple::OSType::Linux); 1276 } 1277 } 1278 1279 // Calculate the offset of the next note just in case "offset" has been 1280 // used to poke at the contents of the note data 1281 offset = note_offset + note.GetByteSize(); 1282 } 1283 1284 return error; 1285 } 1286 1287 void ObjectFileELF::ParseARMAttributes(DataExtractor &data, uint64_t length, 1288 ArchSpec &arch_spec) { 1289 lldb::offset_t Offset = 0; 1290 1291 uint8_t FormatVersion = data.GetU8(&Offset); 1292 if (FormatVersion != llvm::ELFAttrs::Format_Version) 1293 return; 1294 1295 Offset = Offset + sizeof(uint32_t); // Section Length 1296 llvm::StringRef VendorName = data.GetCStr(&Offset); 1297 1298 if (VendorName != "aeabi") 1299 return; 1300 1301 if (arch_spec.GetTriple().getEnvironment() == 1302 llvm::Triple::UnknownEnvironment) 1303 arch_spec.GetTriple().setEnvironment(llvm::Triple::EABI); 1304 1305 while (Offset < length) { 1306 uint8_t Tag = data.GetU8(&Offset); 1307 uint32_t Size = data.GetU32(&Offset); 1308 1309 if (Tag != llvm::ARMBuildAttrs::File || Size == 0) 1310 continue; 1311 1312 while (Offset < length) { 1313 uint64_t Tag = data.GetULEB128(&Offset); 1314 switch (Tag) { 1315 default: 1316 if (Tag < 32) 1317 data.GetULEB128(&Offset); 1318 else if (Tag % 2 == 0) 1319 data.GetULEB128(&Offset); 1320 else 1321 data.GetCStr(&Offset); 1322 1323 break; 1324 1325 case llvm::ARMBuildAttrs::CPU_raw_name: 1326 case llvm::ARMBuildAttrs::CPU_name: 1327 data.GetCStr(&Offset); 1328 1329 break; 1330 1331 case llvm::ARMBuildAttrs::ABI_VFP_args: { 1332 uint64_t VFPArgs = data.GetULEB128(&Offset); 1333 1334 if (VFPArgs == llvm::ARMBuildAttrs::BaseAAPCS) { 1335 if (arch_spec.GetTriple().getEnvironment() == 1336 llvm::Triple::UnknownEnvironment || 1337 arch_spec.GetTriple().getEnvironment() == llvm::Triple::EABIHF) 1338 arch_spec.GetTriple().setEnvironment(llvm::Triple::EABI); 1339 1340 arch_spec.SetFlags(ArchSpec::eARM_abi_soft_float); 1341 } else if (VFPArgs == llvm::ARMBuildAttrs::HardFPAAPCS) { 1342 if (arch_spec.GetTriple().getEnvironment() == 1343 llvm::Triple::UnknownEnvironment || 1344 arch_spec.GetTriple().getEnvironment() == llvm::Triple::EABI) 1345 arch_spec.GetTriple().setEnvironment(llvm::Triple::EABIHF); 1346 1347 arch_spec.SetFlags(ArchSpec::eARM_abi_hard_float); 1348 } 1349 1350 break; 1351 } 1352 } 1353 } 1354 } 1355 } 1356 1357 // GetSectionHeaderInfo 1358 size_t ObjectFileELF::GetSectionHeaderInfo(SectionHeaderColl §ion_headers, 1359 DataExtractor &object_data, 1360 const elf::ELFHeader &header, 1361 lldb_private::UUID &uuid, 1362 std::string &gnu_debuglink_file, 1363 uint32_t &gnu_debuglink_crc, 1364 ArchSpec &arch_spec) { 1365 // Don't reparse the section headers if we already did that. 1366 if (!section_headers.empty()) 1367 return section_headers.size(); 1368 1369 // Only initialize the arch_spec to okay defaults if they're not already set. 1370 // We'll refine this with note data as we parse the notes. 1371 if (arch_spec.GetTriple().getOS() == llvm::Triple::OSType::UnknownOS) { 1372 llvm::Triple::OSType ostype; 1373 llvm::Triple::OSType spec_ostype; 1374 const uint32_t sub_type = subTypeFromElfHeader(header); 1375 arch_spec.SetArchitecture(eArchTypeELF, header.e_machine, sub_type, 1376 header.e_ident[EI_OSABI]); 1377 1378 // Validate if it is ok to remove GetOsFromOSABI. Note, that now the OS is 1379 // determined based on EI_OSABI flag and the info extracted from ELF notes 1380 // (see RefineModuleDetailsFromNote). However in some cases that still 1381 // might be not enough: for example a shared library might not have any 1382 // notes at all and have EI_OSABI flag set to System V, as result the OS 1383 // will be set to UnknownOS. 1384 GetOsFromOSABI(header.e_ident[EI_OSABI], ostype); 1385 spec_ostype = arch_spec.GetTriple().getOS(); 1386 assert(spec_ostype == ostype); 1387 UNUSED_IF_ASSERT_DISABLED(spec_ostype); 1388 } 1389 1390 if (arch_spec.GetMachine() == llvm::Triple::mips || 1391 arch_spec.GetMachine() == llvm::Triple::mipsel || 1392 arch_spec.GetMachine() == llvm::Triple::mips64 || 1393 arch_spec.GetMachine() == llvm::Triple::mips64el) { 1394 switch (header.e_flags & llvm::ELF::EF_MIPS_ARCH_ASE) { 1395 case llvm::ELF::EF_MIPS_MICROMIPS: 1396 arch_spec.SetFlags(ArchSpec::eMIPSAse_micromips); 1397 break; 1398 case llvm::ELF::EF_MIPS_ARCH_ASE_M16: 1399 arch_spec.SetFlags(ArchSpec::eMIPSAse_mips16); 1400 break; 1401 case llvm::ELF::EF_MIPS_ARCH_ASE_MDMX: 1402 arch_spec.SetFlags(ArchSpec::eMIPSAse_mdmx); 1403 break; 1404 default: 1405 break; 1406 } 1407 } 1408 1409 if (arch_spec.GetMachine() == llvm::Triple::arm || 1410 arch_spec.GetMachine() == llvm::Triple::thumb) { 1411 if (header.e_flags & llvm::ELF::EF_ARM_SOFT_FLOAT) 1412 arch_spec.SetFlags(ArchSpec::eARM_abi_soft_float); 1413 else if (header.e_flags & llvm::ELF::EF_ARM_VFP_FLOAT) 1414 arch_spec.SetFlags(ArchSpec::eARM_abi_hard_float); 1415 } 1416 1417 if (arch_spec.GetMachine() == llvm::Triple::riscv32 || 1418 arch_spec.GetMachine() == llvm::Triple::riscv64) { 1419 uint32_t flags = arch_spec.GetFlags(); 1420 1421 if (header.e_flags & llvm::ELF::EF_RISCV_RVC) 1422 flags |= ArchSpec::eRISCV_rvc; 1423 if (header.e_flags & llvm::ELF::EF_RISCV_RVE) 1424 flags |= ArchSpec::eRISCV_rve; 1425 1426 if ((header.e_flags & llvm::ELF::EF_RISCV_FLOAT_ABI_SINGLE) == 1427 llvm::ELF::EF_RISCV_FLOAT_ABI_SINGLE) 1428 flags |= ArchSpec::eRISCV_float_abi_single; 1429 else if ((header.e_flags & llvm::ELF::EF_RISCV_FLOAT_ABI_DOUBLE) == 1430 llvm::ELF::EF_RISCV_FLOAT_ABI_DOUBLE) 1431 flags |= ArchSpec::eRISCV_float_abi_double; 1432 else if ((header.e_flags & llvm::ELF::EF_RISCV_FLOAT_ABI_QUAD) == 1433 llvm::ELF::EF_RISCV_FLOAT_ABI_QUAD) 1434 flags |= ArchSpec::eRISCV_float_abi_quad; 1435 1436 arch_spec.SetFlags(flags); 1437 } 1438 1439 // If there are no section headers we are done. 1440 if (header.e_shnum == 0) 1441 return 0; 1442 1443 Log *log = GetLog(LLDBLog::Modules); 1444 1445 section_headers.resize(header.e_shnum); 1446 if (section_headers.size() != header.e_shnum) 1447 return 0; 1448 1449 const size_t sh_size = header.e_shnum * header.e_shentsize; 1450 const elf_off sh_offset = header.e_shoff; 1451 DataExtractor sh_data; 1452 if (sh_data.SetData(object_data, sh_offset, sh_size) != sh_size) 1453 return 0; 1454 1455 uint32_t idx; 1456 lldb::offset_t offset; 1457 for (idx = 0, offset = 0; idx < header.e_shnum; ++idx) { 1458 if (!section_headers[idx].Parse(sh_data, &offset)) 1459 break; 1460 } 1461 if (idx < section_headers.size()) 1462 section_headers.resize(idx); 1463 1464 const unsigned strtab_idx = header.e_shstrndx; 1465 if (strtab_idx && strtab_idx < section_headers.size()) { 1466 const ELFSectionHeaderInfo &sheader = section_headers[strtab_idx]; 1467 const size_t byte_size = sheader.sh_size; 1468 const Elf64_Off offset = sheader.sh_offset; 1469 lldb_private::DataExtractor shstr_data; 1470 1471 if (shstr_data.SetData(object_data, offset, byte_size) == byte_size) { 1472 for (SectionHeaderCollIter I = section_headers.begin(); 1473 I != section_headers.end(); ++I) { 1474 static ConstString g_sect_name_gnu_debuglink(".gnu_debuglink"); 1475 const ELFSectionHeaderInfo &sheader = *I; 1476 const uint64_t section_size = 1477 sheader.sh_type == SHT_NOBITS ? 0 : sheader.sh_size; 1478 ConstString name(shstr_data.PeekCStr(I->sh_name)); 1479 1480 I->section_name = name; 1481 1482 if (arch_spec.IsMIPS()) { 1483 uint32_t arch_flags = arch_spec.GetFlags(); 1484 DataExtractor data; 1485 if (sheader.sh_type == SHT_MIPS_ABIFLAGS) { 1486 1487 if (section_size && (data.SetData(object_data, sheader.sh_offset, 1488 section_size) == section_size)) { 1489 // MIPS ASE Mask is at offset 12 in MIPS.abiflags section 1490 lldb::offset_t offset = 12; // MIPS ABI Flags Version: 0 1491 arch_flags |= data.GetU32(&offset); 1492 1493 // The floating point ABI is at offset 7 1494 offset = 7; 1495 switch (data.GetU8(&offset)) { 1496 case llvm::Mips::Val_GNU_MIPS_ABI_FP_ANY: 1497 arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_ANY; 1498 break; 1499 case llvm::Mips::Val_GNU_MIPS_ABI_FP_DOUBLE: 1500 arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_DOUBLE; 1501 break; 1502 case llvm::Mips::Val_GNU_MIPS_ABI_FP_SINGLE: 1503 arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_SINGLE; 1504 break; 1505 case llvm::Mips::Val_GNU_MIPS_ABI_FP_SOFT: 1506 arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_SOFT; 1507 break; 1508 case llvm::Mips::Val_GNU_MIPS_ABI_FP_OLD_64: 1509 arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_OLD_64; 1510 break; 1511 case llvm::Mips::Val_GNU_MIPS_ABI_FP_XX: 1512 arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_XX; 1513 break; 1514 case llvm::Mips::Val_GNU_MIPS_ABI_FP_64: 1515 arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_64; 1516 break; 1517 case llvm::Mips::Val_GNU_MIPS_ABI_FP_64A: 1518 arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_64A; 1519 break; 1520 } 1521 } 1522 } 1523 // Settings appropriate ArchSpec ABI Flags 1524 switch (header.e_flags & llvm::ELF::EF_MIPS_ABI) { 1525 case llvm::ELF::EF_MIPS_ABI_O32: 1526 arch_flags |= lldb_private::ArchSpec::eMIPSABI_O32; 1527 break; 1528 case EF_MIPS_ABI_O64: 1529 arch_flags |= lldb_private::ArchSpec::eMIPSABI_O64; 1530 break; 1531 case EF_MIPS_ABI_EABI32: 1532 arch_flags |= lldb_private::ArchSpec::eMIPSABI_EABI32; 1533 break; 1534 case EF_MIPS_ABI_EABI64: 1535 arch_flags |= lldb_private::ArchSpec::eMIPSABI_EABI64; 1536 break; 1537 default: 1538 // ABI Mask doesn't cover N32 and N64 ABI. 1539 if (header.e_ident[EI_CLASS] == llvm::ELF::ELFCLASS64) 1540 arch_flags |= lldb_private::ArchSpec::eMIPSABI_N64; 1541 else if (header.e_flags & llvm::ELF::EF_MIPS_ABI2) 1542 arch_flags |= lldb_private::ArchSpec::eMIPSABI_N32; 1543 break; 1544 } 1545 arch_spec.SetFlags(arch_flags); 1546 } 1547 1548 if (arch_spec.GetMachine() == llvm::Triple::arm || 1549 arch_spec.GetMachine() == llvm::Triple::thumb) { 1550 DataExtractor data; 1551 1552 if (sheader.sh_type == SHT_ARM_ATTRIBUTES && section_size != 0 && 1553 data.SetData(object_data, sheader.sh_offset, section_size) == section_size) 1554 ParseARMAttributes(data, section_size, arch_spec); 1555 } 1556 1557 if (name == g_sect_name_gnu_debuglink) { 1558 DataExtractor data; 1559 if (section_size && (data.SetData(object_data, sheader.sh_offset, 1560 section_size) == section_size)) { 1561 lldb::offset_t gnu_debuglink_offset = 0; 1562 gnu_debuglink_file = data.GetCStr(&gnu_debuglink_offset); 1563 gnu_debuglink_offset = llvm::alignTo(gnu_debuglink_offset, 4); 1564 data.GetU32(&gnu_debuglink_offset, &gnu_debuglink_crc, 1); 1565 } 1566 } 1567 1568 // Process ELF note section entries. 1569 bool is_note_header = (sheader.sh_type == SHT_NOTE); 1570 1571 // The section header ".note.android.ident" is stored as a 1572 // PROGBITS type header but it is actually a note header. 1573 static ConstString g_sect_name_android_ident(".note.android.ident"); 1574 if (!is_note_header && name == g_sect_name_android_ident) 1575 is_note_header = true; 1576 1577 if (is_note_header) { 1578 // Allow notes to refine module info. 1579 DataExtractor data; 1580 if (section_size && (data.SetData(object_data, sheader.sh_offset, 1581 section_size) == section_size)) { 1582 Status error = RefineModuleDetailsFromNote(data, arch_spec, uuid); 1583 if (error.Fail()) { 1584 LLDB_LOGF(log, "ObjectFileELF::%s ELF note processing failed: %s", 1585 __FUNCTION__, error.AsCString()); 1586 } 1587 } 1588 } 1589 } 1590 1591 // Make any unknown triple components to be unspecified unknowns. 1592 if (arch_spec.GetTriple().getVendor() == llvm::Triple::UnknownVendor) 1593 arch_spec.GetTriple().setVendorName(llvm::StringRef()); 1594 if (arch_spec.GetTriple().getOS() == llvm::Triple::UnknownOS) 1595 arch_spec.GetTriple().setOSName(llvm::StringRef()); 1596 1597 return section_headers.size(); 1598 } 1599 } 1600 1601 section_headers.clear(); 1602 return 0; 1603 } 1604 1605 llvm::StringRef 1606 ObjectFileELF::StripLinkerSymbolAnnotations(llvm::StringRef symbol_name) const { 1607 size_t pos = symbol_name.find('@'); 1608 return symbol_name.substr(0, pos); 1609 } 1610 1611 // ParseSectionHeaders 1612 size_t ObjectFileELF::ParseSectionHeaders() { 1613 return GetSectionHeaderInfo(m_section_headers, m_data, m_header, m_uuid, 1614 m_gnu_debuglink_file, m_gnu_debuglink_crc, 1615 m_arch_spec); 1616 } 1617 1618 const ObjectFileELF::ELFSectionHeaderInfo * 1619 ObjectFileELF::GetSectionHeaderByIndex(lldb::user_id_t id) { 1620 if (!ParseSectionHeaders()) 1621 return nullptr; 1622 1623 if (id < m_section_headers.size()) 1624 return &m_section_headers[id]; 1625 1626 return nullptr; 1627 } 1628 1629 lldb::user_id_t ObjectFileELF::GetSectionIndexByName(const char *name) { 1630 if (!name || !name[0] || !ParseSectionHeaders()) 1631 return 0; 1632 for (size_t i = 1; i < m_section_headers.size(); ++i) 1633 if (m_section_headers[i].section_name == ConstString(name)) 1634 return i; 1635 return 0; 1636 } 1637 1638 static SectionType GetSectionTypeFromName(llvm::StringRef Name) { 1639 if (Name.consume_front(".debug_")) { 1640 return llvm::StringSwitch<SectionType>(Name) 1641 .Case("abbrev", eSectionTypeDWARFDebugAbbrev) 1642 .Case("abbrev.dwo", eSectionTypeDWARFDebugAbbrevDwo) 1643 .Case("addr", eSectionTypeDWARFDebugAddr) 1644 .Case("aranges", eSectionTypeDWARFDebugAranges) 1645 .Case("cu_index", eSectionTypeDWARFDebugCuIndex) 1646 .Case("frame", eSectionTypeDWARFDebugFrame) 1647 .Case("info", eSectionTypeDWARFDebugInfo) 1648 .Case("info.dwo", eSectionTypeDWARFDebugInfoDwo) 1649 .Cases("line", "line.dwo", eSectionTypeDWARFDebugLine) 1650 .Cases("line_str", "line_str.dwo", eSectionTypeDWARFDebugLineStr) 1651 .Case("loc", eSectionTypeDWARFDebugLoc) 1652 .Case("loc.dwo", eSectionTypeDWARFDebugLocDwo) 1653 .Case("loclists", eSectionTypeDWARFDebugLocLists) 1654 .Case("loclists.dwo", eSectionTypeDWARFDebugLocListsDwo) 1655 .Case("macinfo", eSectionTypeDWARFDebugMacInfo) 1656 .Cases("macro", "macro.dwo", eSectionTypeDWARFDebugMacro) 1657 .Case("names", eSectionTypeDWARFDebugNames) 1658 .Case("pubnames", eSectionTypeDWARFDebugPubNames) 1659 .Case("pubtypes", eSectionTypeDWARFDebugPubTypes) 1660 .Case("ranges", eSectionTypeDWARFDebugRanges) 1661 .Case("rnglists", eSectionTypeDWARFDebugRngLists) 1662 .Case("rnglists.dwo", eSectionTypeDWARFDebugRngListsDwo) 1663 .Case("str", eSectionTypeDWARFDebugStr) 1664 .Case("str.dwo", eSectionTypeDWARFDebugStrDwo) 1665 .Case("str_offsets", eSectionTypeDWARFDebugStrOffsets) 1666 .Case("str_offsets.dwo", eSectionTypeDWARFDebugStrOffsetsDwo) 1667 .Case("tu_index", eSectionTypeDWARFDebugTuIndex) 1668 .Case("types", eSectionTypeDWARFDebugTypes) 1669 .Case("types.dwo", eSectionTypeDWARFDebugTypesDwo) 1670 .Default(eSectionTypeOther); 1671 } 1672 return llvm::StringSwitch<SectionType>(Name) 1673 .Case(".ARM.exidx", eSectionTypeARMexidx) 1674 .Case(".ARM.extab", eSectionTypeARMextab) 1675 .Cases(".bss", ".tbss", eSectionTypeZeroFill) 1676 .Case(".ctf", eSectionTypeDebug) 1677 .Cases(".data", ".tdata", eSectionTypeData) 1678 .Case(".eh_frame", eSectionTypeEHFrame) 1679 .Case(".gnu_debugaltlink", eSectionTypeDWARFGNUDebugAltLink) 1680 .Case(".gosymtab", eSectionTypeGoSymtab) 1681 .Case(".text", eSectionTypeCode) 1682 .Default(eSectionTypeOther); 1683 } 1684 1685 SectionType ObjectFileELF::GetSectionType(const ELFSectionHeaderInfo &H) const { 1686 switch (H.sh_type) { 1687 case SHT_PROGBITS: 1688 if (H.sh_flags & SHF_EXECINSTR) 1689 return eSectionTypeCode; 1690 break; 1691 case SHT_SYMTAB: 1692 return eSectionTypeELFSymbolTable; 1693 case SHT_DYNSYM: 1694 return eSectionTypeELFDynamicSymbols; 1695 case SHT_RELA: 1696 case SHT_REL: 1697 return eSectionTypeELFRelocationEntries; 1698 case SHT_DYNAMIC: 1699 return eSectionTypeELFDynamicLinkInfo; 1700 } 1701 return GetSectionTypeFromName(H.section_name.GetStringRef()); 1702 } 1703 1704 static uint32_t GetTargetByteSize(SectionType Type, const ArchSpec &arch) { 1705 switch (Type) { 1706 case eSectionTypeData: 1707 case eSectionTypeZeroFill: 1708 return arch.GetDataByteSize(); 1709 case eSectionTypeCode: 1710 return arch.GetCodeByteSize(); 1711 default: 1712 return 1; 1713 } 1714 } 1715 1716 static Permissions GetPermissions(const ELFSectionHeader &H) { 1717 Permissions Perm = Permissions(0); 1718 if (H.sh_flags & SHF_ALLOC) 1719 Perm |= ePermissionsReadable; 1720 if (H.sh_flags & SHF_WRITE) 1721 Perm |= ePermissionsWritable; 1722 if (H.sh_flags & SHF_EXECINSTR) 1723 Perm |= ePermissionsExecutable; 1724 return Perm; 1725 } 1726 1727 static Permissions GetPermissions(const ELFProgramHeader &H) { 1728 Permissions Perm = Permissions(0); 1729 if (H.p_flags & PF_R) 1730 Perm |= ePermissionsReadable; 1731 if (H.p_flags & PF_W) 1732 Perm |= ePermissionsWritable; 1733 if (H.p_flags & PF_X) 1734 Perm |= ePermissionsExecutable; 1735 return Perm; 1736 } 1737 1738 namespace { 1739 1740 using VMRange = lldb_private::Range<addr_t, addr_t>; 1741 1742 struct SectionAddressInfo { 1743 SectionSP Segment; 1744 VMRange Range; 1745 }; 1746 1747 // (Unlinked) ELF object files usually have 0 for every section address, meaning 1748 // we need to compute synthetic addresses in order for "file addresses" from 1749 // different sections to not overlap. This class handles that logic. 1750 class VMAddressProvider { 1751 using VMMap = llvm::IntervalMap<addr_t, SectionSP, 4, 1752 llvm::IntervalMapHalfOpenInfo<addr_t>>; 1753 1754 ObjectFile::Type ObjectType; 1755 addr_t NextVMAddress = 0; 1756 VMMap::Allocator Alloc; 1757 VMMap Segments{Alloc}; 1758 VMMap Sections{Alloc}; 1759 lldb_private::Log *Log = GetLog(LLDBLog::Modules); 1760 size_t SegmentCount = 0; 1761 std::string SegmentName; 1762 1763 VMRange GetVMRange(const ELFSectionHeader &H) { 1764 addr_t Address = H.sh_addr; 1765 addr_t Size = H.sh_flags & SHF_ALLOC ? H.sh_size : 0; 1766 if (ObjectType == ObjectFile::Type::eTypeObjectFile && Segments.empty() && (H.sh_flags & SHF_ALLOC)) { 1767 NextVMAddress = 1768 llvm::alignTo(NextVMAddress, std::max<addr_t>(H.sh_addralign, 1)); 1769 Address = NextVMAddress; 1770 NextVMAddress += Size; 1771 } 1772 return VMRange(Address, Size); 1773 } 1774 1775 public: 1776 VMAddressProvider(ObjectFile::Type Type, llvm::StringRef SegmentName) 1777 : ObjectType(Type), SegmentName(std::string(SegmentName)) {} 1778 1779 std::string GetNextSegmentName() const { 1780 return llvm::formatv("{0}[{1}]", SegmentName, SegmentCount).str(); 1781 } 1782 1783 std::optional<VMRange> GetAddressInfo(const ELFProgramHeader &H) { 1784 if (H.p_memsz == 0) { 1785 LLDB_LOG(Log, "Ignoring zero-sized {0} segment. Corrupt object file?", 1786 SegmentName); 1787 return std::nullopt; 1788 } 1789 1790 if (Segments.overlaps(H.p_vaddr, H.p_vaddr + H.p_memsz)) { 1791 LLDB_LOG(Log, "Ignoring overlapping {0} segment. Corrupt object file?", 1792 SegmentName); 1793 return std::nullopt; 1794 } 1795 return VMRange(H.p_vaddr, H.p_memsz); 1796 } 1797 1798 std::optional<SectionAddressInfo> GetAddressInfo(const ELFSectionHeader &H) { 1799 VMRange Range = GetVMRange(H); 1800 SectionSP Segment; 1801 auto It = Segments.find(Range.GetRangeBase()); 1802 if ((H.sh_flags & SHF_ALLOC) && It.valid()) { 1803 addr_t MaxSize; 1804 if (It.start() <= Range.GetRangeBase()) { 1805 MaxSize = It.stop() - Range.GetRangeBase(); 1806 Segment = *It; 1807 } else 1808 MaxSize = It.start() - Range.GetRangeBase(); 1809 if (Range.GetByteSize() > MaxSize) { 1810 LLDB_LOG(Log, "Shortening section crossing segment boundaries. " 1811 "Corrupt object file?"); 1812 Range.SetByteSize(MaxSize); 1813 } 1814 } 1815 if (Range.GetByteSize() > 0 && 1816 Sections.overlaps(Range.GetRangeBase(), Range.GetRangeEnd())) { 1817 LLDB_LOG(Log, "Ignoring overlapping section. Corrupt object file?"); 1818 return std::nullopt; 1819 } 1820 if (Segment) 1821 Range.Slide(-Segment->GetFileAddress()); 1822 return SectionAddressInfo{Segment, Range}; 1823 } 1824 1825 void AddSegment(const VMRange &Range, SectionSP Seg) { 1826 Segments.insert(Range.GetRangeBase(), Range.GetRangeEnd(), std::move(Seg)); 1827 ++SegmentCount; 1828 } 1829 1830 void AddSection(SectionAddressInfo Info, SectionSP Sect) { 1831 if (Info.Range.GetByteSize() == 0) 1832 return; 1833 if (Info.Segment) 1834 Info.Range.Slide(Info.Segment->GetFileAddress()); 1835 Sections.insert(Info.Range.GetRangeBase(), Info.Range.GetRangeEnd(), 1836 std::move(Sect)); 1837 } 1838 }; 1839 } 1840 1841 void ObjectFileELF::CreateSections(SectionList &unified_section_list) { 1842 if (m_sections_up) 1843 return; 1844 1845 m_sections_up = std::make_unique<SectionList>(); 1846 VMAddressProvider regular_provider(GetType(), "PT_LOAD"); 1847 VMAddressProvider tls_provider(GetType(), "PT_TLS"); 1848 1849 for (const auto &EnumPHdr : llvm::enumerate(ProgramHeaders())) { 1850 const ELFProgramHeader &PHdr = EnumPHdr.value(); 1851 if (PHdr.p_type != PT_LOAD && PHdr.p_type != PT_TLS) 1852 continue; 1853 1854 VMAddressProvider &provider = 1855 PHdr.p_type == PT_TLS ? tls_provider : regular_provider; 1856 auto InfoOr = provider.GetAddressInfo(PHdr); 1857 if (!InfoOr) 1858 continue; 1859 1860 uint32_t Log2Align = llvm::Log2_64(std::max<elf_xword>(PHdr.p_align, 1)); 1861 SectionSP Segment = std::make_shared<Section>( 1862 GetModule(), this, SegmentID(EnumPHdr.index()), 1863 ConstString(provider.GetNextSegmentName()), eSectionTypeContainer, 1864 InfoOr->GetRangeBase(), InfoOr->GetByteSize(), PHdr.p_offset, 1865 PHdr.p_filesz, Log2Align, /*flags*/ 0); 1866 Segment->SetPermissions(GetPermissions(PHdr)); 1867 Segment->SetIsThreadSpecific(PHdr.p_type == PT_TLS); 1868 m_sections_up->AddSection(Segment); 1869 1870 provider.AddSegment(*InfoOr, std::move(Segment)); 1871 } 1872 1873 ParseSectionHeaders(); 1874 if (m_section_headers.empty()) 1875 return; 1876 1877 for (SectionHeaderCollIter I = std::next(m_section_headers.begin()); 1878 I != m_section_headers.end(); ++I) { 1879 const ELFSectionHeaderInfo &header = *I; 1880 1881 ConstString &name = I->section_name; 1882 const uint64_t file_size = 1883 header.sh_type == SHT_NOBITS ? 0 : header.sh_size; 1884 1885 VMAddressProvider &provider = 1886 header.sh_flags & SHF_TLS ? tls_provider : regular_provider; 1887 auto InfoOr = provider.GetAddressInfo(header); 1888 if (!InfoOr) 1889 continue; 1890 1891 SectionType sect_type = GetSectionType(header); 1892 1893 const uint32_t target_bytes_size = 1894 GetTargetByteSize(sect_type, m_arch_spec); 1895 1896 elf::elf_xword log2align = 1897 (header.sh_addralign == 0) ? 0 : llvm::Log2_64(header.sh_addralign); 1898 1899 SectionSP section_sp(new Section( 1900 InfoOr->Segment, GetModule(), // Module to which this section belongs. 1901 this, // ObjectFile to which this section belongs and should 1902 // read section data from. 1903 SectionIndex(I), // Section ID. 1904 name, // Section name. 1905 sect_type, // Section type. 1906 InfoOr->Range.GetRangeBase(), // VM address. 1907 InfoOr->Range.GetByteSize(), // VM size in bytes of this section. 1908 header.sh_offset, // Offset of this section in the file. 1909 file_size, // Size of the section as found in the file. 1910 log2align, // Alignment of the section 1911 header.sh_flags, // Flags for this section. 1912 target_bytes_size)); // Number of host bytes per target byte 1913 1914 section_sp->SetPermissions(GetPermissions(header)); 1915 section_sp->SetIsThreadSpecific(header.sh_flags & SHF_TLS); 1916 (InfoOr->Segment ? InfoOr->Segment->GetChildren() : *m_sections_up) 1917 .AddSection(section_sp); 1918 provider.AddSection(std::move(*InfoOr), std::move(section_sp)); 1919 } 1920 1921 // For eTypeDebugInfo files, the Symbol Vendor will take care of updating the 1922 // unified section list. 1923 if (GetType() != eTypeDebugInfo) 1924 unified_section_list = *m_sections_up; 1925 1926 // If there's a .gnu_debugdata section, we'll try to read the .symtab that's 1927 // embedded in there and replace the one in the original object file (if any). 1928 // If there's none in the orignal object file, we add it to it. 1929 if (auto gdd_obj_file = GetGnuDebugDataObjectFile()) { 1930 if (auto gdd_objfile_section_list = gdd_obj_file->GetSectionList()) { 1931 if (SectionSP symtab_section_sp = 1932 gdd_objfile_section_list->FindSectionByType( 1933 eSectionTypeELFSymbolTable, true)) { 1934 SectionSP module_section_sp = unified_section_list.FindSectionByType( 1935 eSectionTypeELFSymbolTable, true); 1936 if (module_section_sp) 1937 unified_section_list.ReplaceSection(module_section_sp->GetID(), 1938 symtab_section_sp); 1939 else 1940 unified_section_list.AddSection(symtab_section_sp); 1941 } 1942 } 1943 } 1944 } 1945 1946 std::shared_ptr<ObjectFileELF> ObjectFileELF::GetGnuDebugDataObjectFile() { 1947 if (m_gnu_debug_data_object_file != nullptr) 1948 return m_gnu_debug_data_object_file; 1949 1950 SectionSP section = 1951 GetSectionList()->FindSectionByName(ConstString(".gnu_debugdata")); 1952 if (!section) 1953 return nullptr; 1954 1955 if (!lldb_private::lzma::isAvailable()) { 1956 GetModule()->ReportWarning( 1957 "No LZMA support found for reading .gnu_debugdata section"); 1958 return nullptr; 1959 } 1960 1961 // Uncompress the data 1962 DataExtractor data; 1963 section->GetSectionData(data); 1964 llvm::SmallVector<uint8_t, 0> uncompressedData; 1965 auto err = lldb_private::lzma::uncompress(data.GetData(), uncompressedData); 1966 if (err) { 1967 GetModule()->ReportWarning( 1968 "An error occurred while decompression the section {0}: {1}", 1969 section->GetName().AsCString(), llvm::toString(std::move(err)).c_str()); 1970 return nullptr; 1971 } 1972 1973 // Construct ObjectFileELF object from decompressed buffer 1974 DataBufferSP gdd_data_buf( 1975 new DataBufferHeap(uncompressedData.data(), uncompressedData.size())); 1976 auto fspec = GetFileSpec().CopyByAppendingPathComponent( 1977 llvm::StringRef("gnu_debugdata")); 1978 m_gnu_debug_data_object_file.reset(new ObjectFileELF( 1979 GetModule(), gdd_data_buf, 0, &fspec, 0, gdd_data_buf->GetByteSize())); 1980 1981 // This line is essential; otherwise a breakpoint can be set but not hit. 1982 m_gnu_debug_data_object_file->SetType(ObjectFile::eTypeDebugInfo); 1983 1984 ArchSpec spec = m_gnu_debug_data_object_file->GetArchitecture(); 1985 if (spec && m_gnu_debug_data_object_file->SetModulesArchitecture(spec)) 1986 return m_gnu_debug_data_object_file; 1987 1988 return nullptr; 1989 } 1990 1991 // Find the arm/aarch64 mapping symbol character in the given symbol name. 1992 // Mapping symbols have the form of "$<char>[.<any>]*". Additionally we 1993 // recognize cases when the mapping symbol prefixed by an arbitrary string 1994 // because if a symbol prefix added to each symbol in the object file with 1995 // objcopy then the mapping symbols are also prefixed. 1996 static char FindArmAarch64MappingSymbol(const char *symbol_name) { 1997 if (!symbol_name) 1998 return '\0'; 1999 2000 const char *dollar_pos = ::strchr(symbol_name, '$'); 2001 if (!dollar_pos || dollar_pos[1] == '\0') 2002 return '\0'; 2003 2004 if (dollar_pos[2] == '\0' || dollar_pos[2] == '.') 2005 return dollar_pos[1]; 2006 return '\0'; 2007 } 2008 2009 #define STO_MIPS_ISA (3 << 6) 2010 #define STO_MICROMIPS (2 << 6) 2011 #define IS_MICROMIPS(ST_OTHER) (((ST_OTHER)&STO_MIPS_ISA) == STO_MICROMIPS) 2012 2013 // private 2014 unsigned ObjectFileELF::ParseSymbols(Symtab *symtab, user_id_t start_id, 2015 SectionList *section_list, 2016 const size_t num_symbols, 2017 const DataExtractor &symtab_data, 2018 const DataExtractor &strtab_data) { 2019 ELFSymbol symbol; 2020 lldb::offset_t offset = 0; 2021 2022 static ConstString text_section_name(".text"); 2023 static ConstString init_section_name(".init"); 2024 static ConstString fini_section_name(".fini"); 2025 static ConstString ctors_section_name(".ctors"); 2026 static ConstString dtors_section_name(".dtors"); 2027 2028 static ConstString data_section_name(".data"); 2029 static ConstString rodata_section_name(".rodata"); 2030 static ConstString rodata1_section_name(".rodata1"); 2031 static ConstString data2_section_name(".data1"); 2032 static ConstString bss_section_name(".bss"); 2033 static ConstString opd_section_name(".opd"); // For ppc64 2034 2035 // On Android the oatdata and the oatexec symbols in the oat and odex files 2036 // covers the full .text section what causes issues with displaying unusable 2037 // symbol name to the user and very slow unwinding speed because the 2038 // instruction emulation based unwind plans try to emulate all instructions 2039 // in these symbols. Don't add these symbols to the symbol list as they have 2040 // no use for the debugger and they are causing a lot of trouble. Filtering 2041 // can't be restricted to Android because this special object file don't 2042 // contain the note section specifying the environment to Android but the 2043 // custom extension and file name makes it highly unlikely that this will 2044 // collide with anything else. 2045 llvm::StringRef file_extension = m_file.GetFileNameExtension(); 2046 bool skip_oatdata_oatexec = 2047 file_extension == ".oat" || file_extension == ".odex"; 2048 2049 ArchSpec arch = GetArchitecture(); 2050 ModuleSP module_sp(GetModule()); 2051 SectionList *module_section_list = 2052 module_sp ? module_sp->GetSectionList() : nullptr; 2053 2054 // Local cache to avoid doing a FindSectionByName for each symbol. The "const 2055 // char*" key must came from a ConstString object so they can be compared by 2056 // pointer 2057 std::unordered_map<const char *, lldb::SectionSP> section_name_to_section; 2058 2059 unsigned i; 2060 for (i = 0; i < num_symbols; ++i) { 2061 if (!symbol.Parse(symtab_data, &offset)) 2062 break; 2063 2064 const char *symbol_name = strtab_data.PeekCStr(symbol.st_name); 2065 if (!symbol_name) 2066 symbol_name = ""; 2067 2068 // No need to add non-section symbols that have no names 2069 if (symbol.getType() != STT_SECTION && 2070 (symbol_name == nullptr || symbol_name[0] == '\0')) 2071 continue; 2072 2073 // Skipping oatdata and oatexec sections if it is requested. See details 2074 // above the definition of skip_oatdata_oatexec for the reasons. 2075 if (skip_oatdata_oatexec && (::strcmp(symbol_name, "oatdata") == 0 || 2076 ::strcmp(symbol_name, "oatexec") == 0)) 2077 continue; 2078 2079 SectionSP symbol_section_sp; 2080 SymbolType symbol_type = eSymbolTypeInvalid; 2081 Elf64_Half shndx = symbol.st_shndx; 2082 2083 switch (shndx) { 2084 case SHN_ABS: 2085 symbol_type = eSymbolTypeAbsolute; 2086 break; 2087 case SHN_UNDEF: 2088 symbol_type = eSymbolTypeUndefined; 2089 break; 2090 default: 2091 symbol_section_sp = section_list->FindSectionByID(shndx); 2092 break; 2093 } 2094 2095 // If a symbol is undefined do not process it further even if it has a STT 2096 // type 2097 if (symbol_type != eSymbolTypeUndefined) { 2098 switch (symbol.getType()) { 2099 default: 2100 case STT_NOTYPE: 2101 // The symbol's type is not specified. 2102 break; 2103 2104 case STT_OBJECT: 2105 // The symbol is associated with a data object, such as a variable, an 2106 // array, etc. 2107 symbol_type = eSymbolTypeData; 2108 break; 2109 2110 case STT_FUNC: 2111 // The symbol is associated with a function or other executable code. 2112 symbol_type = eSymbolTypeCode; 2113 break; 2114 2115 case STT_SECTION: 2116 // The symbol is associated with a section. Symbol table entries of 2117 // this type exist primarily for relocation and normally have STB_LOCAL 2118 // binding. 2119 break; 2120 2121 case STT_FILE: 2122 // Conventionally, the symbol's name gives the name of the source file 2123 // associated with the object file. A file symbol has STB_LOCAL 2124 // binding, its section index is SHN_ABS, and it precedes the other 2125 // STB_LOCAL symbols for the file, if it is present. 2126 symbol_type = eSymbolTypeSourceFile; 2127 break; 2128 2129 case STT_GNU_IFUNC: 2130 // The symbol is associated with an indirect function. The actual 2131 // function will be resolved if it is referenced. 2132 symbol_type = eSymbolTypeResolver; 2133 break; 2134 } 2135 } 2136 2137 if (symbol_type == eSymbolTypeInvalid && symbol.getType() != STT_SECTION) { 2138 if (symbol_section_sp) { 2139 ConstString sect_name = symbol_section_sp->GetName(); 2140 if (sect_name == text_section_name || sect_name == init_section_name || 2141 sect_name == fini_section_name || sect_name == ctors_section_name || 2142 sect_name == dtors_section_name) { 2143 symbol_type = eSymbolTypeCode; 2144 } else if (sect_name == data_section_name || 2145 sect_name == data2_section_name || 2146 sect_name == rodata_section_name || 2147 sect_name == rodata1_section_name || 2148 sect_name == bss_section_name) { 2149 symbol_type = eSymbolTypeData; 2150 } 2151 } 2152 } 2153 2154 int64_t symbol_value_offset = 0; 2155 uint32_t additional_flags = 0; 2156 2157 if (arch.IsValid()) { 2158 if (arch.GetMachine() == llvm::Triple::arm) { 2159 if (symbol.getBinding() == STB_LOCAL) { 2160 char mapping_symbol = FindArmAarch64MappingSymbol(symbol_name); 2161 if (symbol_type == eSymbolTypeCode) { 2162 switch (mapping_symbol) { 2163 case 'a': 2164 // $a[.<any>]* - marks an ARM instruction sequence 2165 m_address_class_map[symbol.st_value] = AddressClass::eCode; 2166 break; 2167 case 'b': 2168 case 't': 2169 // $b[.<any>]* - marks a THUMB BL instruction sequence 2170 // $t[.<any>]* - marks a THUMB instruction sequence 2171 m_address_class_map[symbol.st_value] = 2172 AddressClass::eCodeAlternateISA; 2173 break; 2174 case 'd': 2175 // $d[.<any>]* - marks a data item sequence (e.g. lit pool) 2176 m_address_class_map[symbol.st_value] = AddressClass::eData; 2177 break; 2178 } 2179 } 2180 if (mapping_symbol) 2181 continue; 2182 } 2183 } else if (arch.GetMachine() == llvm::Triple::aarch64) { 2184 if (symbol.getBinding() == STB_LOCAL) { 2185 char mapping_symbol = FindArmAarch64MappingSymbol(symbol_name); 2186 if (symbol_type == eSymbolTypeCode) { 2187 switch (mapping_symbol) { 2188 case 'x': 2189 // $x[.<any>]* - marks an A64 instruction sequence 2190 m_address_class_map[symbol.st_value] = AddressClass::eCode; 2191 break; 2192 case 'd': 2193 // $d[.<any>]* - marks a data item sequence (e.g. lit pool) 2194 m_address_class_map[symbol.st_value] = AddressClass::eData; 2195 break; 2196 } 2197 } 2198 if (mapping_symbol) 2199 continue; 2200 } 2201 } 2202 2203 if (arch.GetMachine() == llvm::Triple::arm) { 2204 if (symbol_type == eSymbolTypeCode) { 2205 if (symbol.st_value & 1) { 2206 // Subtracting 1 from the address effectively unsets the low order 2207 // bit, which results in the address actually pointing to the 2208 // beginning of the symbol. This delta will be used below in 2209 // conjunction with symbol.st_value to produce the final 2210 // symbol_value that we store in the symtab. 2211 symbol_value_offset = -1; 2212 m_address_class_map[symbol.st_value ^ 1] = 2213 AddressClass::eCodeAlternateISA; 2214 } else { 2215 // This address is ARM 2216 m_address_class_map[symbol.st_value] = AddressClass::eCode; 2217 } 2218 } 2219 } 2220 2221 /* 2222 * MIPS: 2223 * The bit #0 of an address is used for ISA mode (1 for microMIPS, 0 for 2224 * MIPS). 2225 * This allows processor to switch between microMIPS and MIPS without any 2226 * need 2227 * for special mode-control register. However, apart from .debug_line, 2228 * none of 2229 * the ELF/DWARF sections set the ISA bit (for symbol or section). Use 2230 * st_other 2231 * flag to check whether the symbol is microMIPS and then set the address 2232 * class 2233 * accordingly. 2234 */ 2235 if (arch.IsMIPS()) { 2236 if (IS_MICROMIPS(symbol.st_other)) 2237 m_address_class_map[symbol.st_value] = AddressClass::eCodeAlternateISA; 2238 else if ((symbol.st_value & 1) && (symbol_type == eSymbolTypeCode)) { 2239 symbol.st_value = symbol.st_value & (~1ull); 2240 m_address_class_map[symbol.st_value] = AddressClass::eCodeAlternateISA; 2241 } else { 2242 if (symbol_type == eSymbolTypeCode) 2243 m_address_class_map[symbol.st_value] = AddressClass::eCode; 2244 else if (symbol_type == eSymbolTypeData) 2245 m_address_class_map[symbol.st_value] = AddressClass::eData; 2246 else 2247 m_address_class_map[symbol.st_value] = AddressClass::eUnknown; 2248 } 2249 } 2250 } 2251 2252 // symbol_value_offset may contain 0 for ARM symbols or -1 for THUMB 2253 // symbols. See above for more details. 2254 uint64_t symbol_value = symbol.st_value + symbol_value_offset; 2255 2256 if (symbol_section_sp && 2257 CalculateType() != ObjectFile::Type::eTypeObjectFile) 2258 symbol_value -= symbol_section_sp->GetFileAddress(); 2259 2260 if (symbol_section_sp && module_section_list && 2261 module_section_list != section_list) { 2262 ConstString sect_name = symbol_section_sp->GetName(); 2263 auto section_it = section_name_to_section.find(sect_name.GetCString()); 2264 if (section_it == section_name_to_section.end()) 2265 section_it = 2266 section_name_to_section 2267 .emplace(sect_name.GetCString(), 2268 module_section_list->FindSectionByName(sect_name)) 2269 .first; 2270 if (section_it->second) 2271 symbol_section_sp = section_it->second; 2272 } 2273 2274 bool is_global = symbol.getBinding() == STB_GLOBAL; 2275 uint32_t flags = symbol.st_other << 8 | symbol.st_info | additional_flags; 2276 llvm::StringRef symbol_ref(symbol_name); 2277 2278 // Symbol names may contain @VERSION suffixes. Find those and strip them 2279 // temporarily. 2280 size_t version_pos = symbol_ref.find('@'); 2281 bool has_suffix = version_pos != llvm::StringRef::npos; 2282 llvm::StringRef symbol_bare = symbol_ref.substr(0, version_pos); 2283 Mangled mangled(symbol_bare); 2284 2285 // Now append the suffix back to mangled and unmangled names. Only do it if 2286 // the demangling was successful (string is not empty). 2287 if (has_suffix) { 2288 llvm::StringRef suffix = symbol_ref.substr(version_pos); 2289 2290 llvm::StringRef mangled_name = mangled.GetMangledName().GetStringRef(); 2291 if (!mangled_name.empty()) 2292 mangled.SetMangledName(ConstString((mangled_name + suffix).str())); 2293 2294 ConstString demangled = mangled.GetDemangledName(); 2295 llvm::StringRef demangled_name = demangled.GetStringRef(); 2296 if (!demangled_name.empty()) 2297 mangled.SetDemangledName(ConstString((demangled_name + suffix).str())); 2298 } 2299 2300 // In ELF all symbol should have a valid size but it is not true for some 2301 // function symbols coming from hand written assembly. As none of the 2302 // function symbol should have 0 size we try to calculate the size for 2303 // these symbols in the symtab with saying that their original size is not 2304 // valid. 2305 bool symbol_size_valid = 2306 symbol.st_size != 0 || symbol.getType() != STT_FUNC; 2307 2308 Symbol dc_symbol( 2309 i + start_id, // ID is the original symbol table index. 2310 mangled, 2311 symbol_type, // Type of this symbol 2312 is_global, // Is this globally visible? 2313 false, // Is this symbol debug info? 2314 false, // Is this symbol a trampoline? 2315 false, // Is this symbol artificial? 2316 AddressRange(symbol_section_sp, // Section in which this symbol is 2317 // defined or null. 2318 symbol_value, // Offset in section or symbol value. 2319 symbol.st_size), // Size in bytes of this symbol. 2320 symbol_size_valid, // Symbol size is valid 2321 has_suffix, // Contains linker annotations? 2322 flags); // Symbol flags. 2323 if (symbol.getBinding() == STB_WEAK) 2324 dc_symbol.SetIsWeak(true); 2325 symtab->AddSymbol(dc_symbol); 2326 } 2327 return i; 2328 } 2329 2330 unsigned ObjectFileELF::ParseSymbolTable(Symtab *symbol_table, 2331 user_id_t start_id, 2332 lldb_private::Section *symtab) { 2333 if (symtab->GetObjectFile() != this) { 2334 // If the symbol table section is owned by a different object file, have it 2335 // do the parsing. 2336 ObjectFileELF *obj_file_elf = 2337 static_cast<ObjectFileELF *>(symtab->GetObjectFile()); 2338 return obj_file_elf->ParseSymbolTable(symbol_table, start_id, symtab); 2339 } 2340 2341 // Get section list for this object file. 2342 SectionList *section_list = m_sections_up.get(); 2343 if (!section_list) 2344 return 0; 2345 2346 user_id_t symtab_id = symtab->GetID(); 2347 const ELFSectionHeaderInfo *symtab_hdr = GetSectionHeaderByIndex(symtab_id); 2348 assert(symtab_hdr->sh_type == SHT_SYMTAB || 2349 symtab_hdr->sh_type == SHT_DYNSYM); 2350 2351 // sh_link: section header index of associated string table. 2352 user_id_t strtab_id = symtab_hdr->sh_link; 2353 Section *strtab = section_list->FindSectionByID(strtab_id).get(); 2354 2355 if (symtab && strtab) { 2356 assert(symtab->GetObjectFile() == this); 2357 assert(strtab->GetObjectFile() == this); 2358 2359 DataExtractor symtab_data; 2360 DataExtractor strtab_data; 2361 if (ReadSectionData(symtab, symtab_data) && 2362 ReadSectionData(strtab, strtab_data)) { 2363 size_t num_symbols = symtab_data.GetByteSize() / symtab_hdr->sh_entsize; 2364 2365 return ParseSymbols(symbol_table, start_id, section_list, num_symbols, 2366 symtab_data, strtab_data); 2367 } 2368 } 2369 2370 return 0; 2371 } 2372 2373 size_t ObjectFileELF::ParseDynamicSymbols() { 2374 if (m_dynamic_symbols.size()) 2375 return m_dynamic_symbols.size(); 2376 2377 SectionList *section_list = GetSectionList(); 2378 if (!section_list) 2379 return 0; 2380 2381 // Find the SHT_DYNAMIC section. 2382 Section *dynsym = 2383 section_list->FindSectionByType(eSectionTypeELFDynamicLinkInfo, true) 2384 .get(); 2385 if (!dynsym) 2386 return 0; 2387 assert(dynsym->GetObjectFile() == this); 2388 2389 ELFDynamic symbol; 2390 DataExtractor dynsym_data; 2391 if (ReadSectionData(dynsym, dynsym_data)) { 2392 const lldb::offset_t section_size = dynsym_data.GetByteSize(); 2393 lldb::offset_t cursor = 0; 2394 2395 while (cursor < section_size) { 2396 if (!symbol.Parse(dynsym_data, &cursor)) 2397 break; 2398 2399 m_dynamic_symbols.push_back(symbol); 2400 } 2401 } 2402 2403 return m_dynamic_symbols.size(); 2404 } 2405 2406 const ELFDynamic *ObjectFileELF::FindDynamicSymbol(unsigned tag) { 2407 if (!ParseDynamicSymbols()) 2408 return nullptr; 2409 2410 DynamicSymbolCollIter I = m_dynamic_symbols.begin(); 2411 DynamicSymbolCollIter E = m_dynamic_symbols.end(); 2412 for (; I != E; ++I) { 2413 ELFDynamic *symbol = &*I; 2414 2415 if (symbol->d_tag == tag) 2416 return symbol; 2417 } 2418 2419 return nullptr; 2420 } 2421 2422 unsigned ObjectFileELF::PLTRelocationType() { 2423 // DT_PLTREL 2424 // This member specifies the type of relocation entry to which the 2425 // procedure linkage table refers. The d_val member holds DT_REL or 2426 // DT_RELA, as appropriate. All relocations in a procedure linkage table 2427 // must use the same relocation. 2428 const ELFDynamic *symbol = FindDynamicSymbol(DT_PLTREL); 2429 2430 if (symbol) 2431 return symbol->d_val; 2432 2433 return 0; 2434 } 2435 2436 // Returns the size of the normal plt entries and the offset of the first 2437 // normal plt entry. The 0th entry in the plt table is usually a resolution 2438 // entry which have different size in some architectures then the rest of the 2439 // plt entries. 2440 static std::pair<uint64_t, uint64_t> 2441 GetPltEntrySizeAndOffset(const ELFSectionHeader *rel_hdr, 2442 const ELFSectionHeader *plt_hdr) { 2443 const elf_xword num_relocations = rel_hdr->sh_size / rel_hdr->sh_entsize; 2444 2445 // Clang 3.3 sets entsize to 4 for 32-bit binaries, but the plt entries are 2446 // 16 bytes. So round the entsize up by the alignment if addralign is set. 2447 elf_xword plt_entsize = 2448 plt_hdr->sh_addralign 2449 ? llvm::alignTo(plt_hdr->sh_entsize, plt_hdr->sh_addralign) 2450 : plt_hdr->sh_entsize; 2451 2452 // Some linkers e.g ld for arm, fill plt_hdr->sh_entsize field incorrectly. 2453 // PLT entries relocation code in general requires multiple instruction and 2454 // should be greater than 4 bytes in most cases. Try to guess correct size 2455 // just in case. 2456 if (plt_entsize <= 4) { 2457 // The linker haven't set the plt_hdr->sh_entsize field. Try to guess the 2458 // size of the plt entries based on the number of entries and the size of 2459 // the plt section with the assumption that the size of the 0th entry is at 2460 // least as big as the size of the normal entries and it isn't much bigger 2461 // then that. 2462 if (plt_hdr->sh_addralign) 2463 plt_entsize = plt_hdr->sh_size / plt_hdr->sh_addralign / 2464 (num_relocations + 1) * plt_hdr->sh_addralign; 2465 else 2466 plt_entsize = plt_hdr->sh_size / (num_relocations + 1); 2467 } 2468 2469 elf_xword plt_offset = plt_hdr->sh_size - num_relocations * plt_entsize; 2470 2471 return std::make_pair(plt_entsize, plt_offset); 2472 } 2473 2474 static unsigned ParsePLTRelocations( 2475 Symtab *symbol_table, user_id_t start_id, unsigned rel_type, 2476 const ELFHeader *hdr, const ELFSectionHeader *rel_hdr, 2477 const ELFSectionHeader *plt_hdr, const ELFSectionHeader *sym_hdr, 2478 const lldb::SectionSP &plt_section_sp, DataExtractor &rel_data, 2479 DataExtractor &symtab_data, DataExtractor &strtab_data) { 2480 ELFRelocation rel(rel_type); 2481 ELFSymbol symbol; 2482 lldb::offset_t offset = 0; 2483 2484 uint64_t plt_offset, plt_entsize; 2485 std::tie(plt_entsize, plt_offset) = 2486 GetPltEntrySizeAndOffset(rel_hdr, plt_hdr); 2487 const elf_xword num_relocations = rel_hdr->sh_size / rel_hdr->sh_entsize; 2488 2489 typedef unsigned (*reloc_info_fn)(const ELFRelocation &rel); 2490 reloc_info_fn reloc_type; 2491 reloc_info_fn reloc_symbol; 2492 2493 if (hdr->Is32Bit()) { 2494 reloc_type = ELFRelocation::RelocType32; 2495 reloc_symbol = ELFRelocation::RelocSymbol32; 2496 } else { 2497 reloc_type = ELFRelocation::RelocType64; 2498 reloc_symbol = ELFRelocation::RelocSymbol64; 2499 } 2500 2501 unsigned slot_type = hdr->GetRelocationJumpSlotType(); 2502 unsigned i; 2503 for (i = 0; i < num_relocations; ++i) { 2504 if (!rel.Parse(rel_data, &offset)) 2505 break; 2506 2507 if (reloc_type(rel) != slot_type) 2508 continue; 2509 2510 lldb::offset_t symbol_offset = reloc_symbol(rel) * sym_hdr->sh_entsize; 2511 if (!symbol.Parse(symtab_data, &symbol_offset)) 2512 break; 2513 2514 const char *symbol_name = strtab_data.PeekCStr(symbol.st_name); 2515 uint64_t plt_index = plt_offset + i * plt_entsize; 2516 2517 Symbol jump_symbol( 2518 i + start_id, // Symbol table index 2519 symbol_name, // symbol name. 2520 eSymbolTypeTrampoline, // Type of this symbol 2521 false, // Is this globally visible? 2522 false, // Is this symbol debug info? 2523 true, // Is this symbol a trampoline? 2524 true, // Is this symbol artificial? 2525 plt_section_sp, // Section in which this symbol is defined or null. 2526 plt_index, // Offset in section or symbol value. 2527 plt_entsize, // Size in bytes of this symbol. 2528 true, // Size is valid 2529 false, // Contains linker annotations? 2530 0); // Symbol flags. 2531 2532 symbol_table->AddSymbol(jump_symbol); 2533 } 2534 2535 return i; 2536 } 2537 2538 unsigned 2539 ObjectFileELF::ParseTrampolineSymbols(Symtab *symbol_table, user_id_t start_id, 2540 const ELFSectionHeaderInfo *rel_hdr, 2541 user_id_t rel_id) { 2542 assert(rel_hdr->sh_type == SHT_RELA || rel_hdr->sh_type == SHT_REL); 2543 2544 // The link field points to the associated symbol table. 2545 user_id_t symtab_id = rel_hdr->sh_link; 2546 2547 // If the link field doesn't point to the appropriate symbol name table then 2548 // try to find it by name as some compiler don't fill in the link fields. 2549 if (!symtab_id) 2550 symtab_id = GetSectionIndexByName(".dynsym"); 2551 2552 // Get PLT section. We cannot use rel_hdr->sh_info, since current linkers 2553 // point that to the .got.plt or .got section instead of .plt. 2554 user_id_t plt_id = GetSectionIndexByName(".plt"); 2555 2556 if (!symtab_id || !plt_id) 2557 return 0; 2558 2559 const ELFSectionHeaderInfo *plt_hdr = GetSectionHeaderByIndex(plt_id); 2560 if (!plt_hdr) 2561 return 0; 2562 2563 const ELFSectionHeaderInfo *sym_hdr = GetSectionHeaderByIndex(symtab_id); 2564 if (!sym_hdr) 2565 return 0; 2566 2567 SectionList *section_list = m_sections_up.get(); 2568 if (!section_list) 2569 return 0; 2570 2571 Section *rel_section = section_list->FindSectionByID(rel_id).get(); 2572 if (!rel_section) 2573 return 0; 2574 2575 SectionSP plt_section_sp(section_list->FindSectionByID(plt_id)); 2576 if (!plt_section_sp) 2577 return 0; 2578 2579 Section *symtab = section_list->FindSectionByID(symtab_id).get(); 2580 if (!symtab) 2581 return 0; 2582 2583 // sh_link points to associated string table. 2584 Section *strtab = section_list->FindSectionByID(sym_hdr->sh_link).get(); 2585 if (!strtab) 2586 return 0; 2587 2588 DataExtractor rel_data; 2589 if (!ReadSectionData(rel_section, rel_data)) 2590 return 0; 2591 2592 DataExtractor symtab_data; 2593 if (!ReadSectionData(symtab, symtab_data)) 2594 return 0; 2595 2596 DataExtractor strtab_data; 2597 if (!ReadSectionData(strtab, strtab_data)) 2598 return 0; 2599 2600 unsigned rel_type = PLTRelocationType(); 2601 if (!rel_type) 2602 return 0; 2603 2604 return ParsePLTRelocations(symbol_table, start_id, rel_type, &m_header, 2605 rel_hdr, plt_hdr, sym_hdr, plt_section_sp, 2606 rel_data, symtab_data, strtab_data); 2607 } 2608 2609 static void ApplyELF64ABS64Relocation(Symtab *symtab, ELFRelocation &rel, 2610 DataExtractor &debug_data, 2611 Section *rel_section) { 2612 Symbol *symbol = symtab->FindSymbolByID(ELFRelocation::RelocSymbol64(rel)); 2613 if (symbol) { 2614 addr_t value = symbol->GetAddressRef().GetFileAddress(); 2615 DataBufferSP &data_buffer_sp = debug_data.GetSharedDataBuffer(); 2616 // ObjectFileELF creates a WritableDataBuffer in CreateInstance. 2617 WritableDataBuffer *data_buffer = 2618 llvm::cast<WritableDataBuffer>(data_buffer_sp.get()); 2619 uint64_t *dst = reinterpret_cast<uint64_t *>( 2620 data_buffer->GetBytes() + rel_section->GetFileOffset() + 2621 ELFRelocation::RelocOffset64(rel)); 2622 uint64_t val_offset = value + ELFRelocation::RelocAddend64(rel); 2623 memcpy(dst, &val_offset, sizeof(uint64_t)); 2624 } 2625 } 2626 2627 static void ApplyELF64ABS32Relocation(Symtab *symtab, ELFRelocation &rel, 2628 DataExtractor &debug_data, 2629 Section *rel_section, bool is_signed) { 2630 Symbol *symbol = symtab->FindSymbolByID(ELFRelocation::RelocSymbol64(rel)); 2631 if (symbol) { 2632 addr_t value = symbol->GetAddressRef().GetFileAddress(); 2633 value += ELFRelocation::RelocAddend32(rel); 2634 if ((!is_signed && (value > UINT32_MAX)) || 2635 (is_signed && 2636 ((int64_t)value > INT32_MAX || (int64_t)value < INT32_MIN))) { 2637 Log *log = GetLog(LLDBLog::Modules); 2638 LLDB_LOGF(log, "Failed to apply debug info relocations"); 2639 return; 2640 } 2641 uint32_t truncated_addr = (value & 0xFFFFFFFF); 2642 DataBufferSP &data_buffer_sp = debug_data.GetSharedDataBuffer(); 2643 // ObjectFileELF creates a WritableDataBuffer in CreateInstance. 2644 WritableDataBuffer *data_buffer = 2645 llvm::cast<WritableDataBuffer>(data_buffer_sp.get()); 2646 uint32_t *dst = reinterpret_cast<uint32_t *>( 2647 data_buffer->GetBytes() + rel_section->GetFileOffset() + 2648 ELFRelocation::RelocOffset32(rel)); 2649 memcpy(dst, &truncated_addr, sizeof(uint32_t)); 2650 } 2651 } 2652 2653 static void ApplyELF32ABS32RelRelocation(Symtab *symtab, ELFRelocation &rel, 2654 DataExtractor &debug_data, 2655 Section *rel_section) { 2656 Log *log = GetLog(LLDBLog::Modules); 2657 Symbol *symbol = symtab->FindSymbolByID(ELFRelocation::RelocSymbol32(rel)); 2658 if (symbol) { 2659 addr_t value = symbol->GetAddressRef().GetFileAddress(); 2660 if (value == LLDB_INVALID_ADDRESS) { 2661 const char *name = symbol->GetName().GetCString(); 2662 LLDB_LOGF(log, "Debug info symbol invalid: %s", name); 2663 return; 2664 } 2665 assert(llvm::isUInt<32>(value) && "Valid addresses are 32-bit"); 2666 DataBufferSP &data_buffer_sp = debug_data.GetSharedDataBuffer(); 2667 // ObjectFileELF creates a WritableDataBuffer in CreateInstance. 2668 WritableDataBuffer *data_buffer = 2669 llvm::cast<WritableDataBuffer>(data_buffer_sp.get()); 2670 uint8_t *dst = data_buffer->GetBytes() + rel_section->GetFileOffset() + 2671 ELFRelocation::RelocOffset32(rel); 2672 // Implicit addend is stored inline as a signed value. 2673 int32_t addend; 2674 memcpy(&addend, dst, sizeof(int32_t)); 2675 // The sum must be positive. This extra check prevents UB from overflow in 2676 // the actual range check below. 2677 if (addend < 0 && static_cast<uint32_t>(-addend) > value) { 2678 LLDB_LOGF(log, "Debug info relocation overflow: 0x%" PRIx64, 2679 static_cast<int64_t>(value) + addend); 2680 return; 2681 } 2682 if (!llvm::isUInt<32>(value + addend)) { 2683 LLDB_LOGF(log, "Debug info relocation out of range: 0x%" PRIx64, value); 2684 return; 2685 } 2686 uint32_t addr = value + addend; 2687 memcpy(dst, &addr, sizeof(uint32_t)); 2688 } 2689 } 2690 2691 unsigned ObjectFileELF::ApplyRelocations( 2692 Symtab *symtab, const ELFHeader *hdr, const ELFSectionHeader *rel_hdr, 2693 const ELFSectionHeader *symtab_hdr, const ELFSectionHeader *debug_hdr, 2694 DataExtractor &rel_data, DataExtractor &symtab_data, 2695 DataExtractor &debug_data, Section *rel_section) { 2696 ELFRelocation rel(rel_hdr->sh_type); 2697 lldb::addr_t offset = 0; 2698 const unsigned num_relocations = rel_hdr->sh_size / rel_hdr->sh_entsize; 2699 typedef unsigned (*reloc_info_fn)(const ELFRelocation &rel); 2700 reloc_info_fn reloc_type; 2701 reloc_info_fn reloc_symbol; 2702 2703 if (hdr->Is32Bit()) { 2704 reloc_type = ELFRelocation::RelocType32; 2705 reloc_symbol = ELFRelocation::RelocSymbol32; 2706 } else { 2707 reloc_type = ELFRelocation::RelocType64; 2708 reloc_symbol = ELFRelocation::RelocSymbol64; 2709 } 2710 2711 for (unsigned i = 0; i < num_relocations; ++i) { 2712 if (!rel.Parse(rel_data, &offset)) { 2713 GetModule()->ReportError(".rel{0}[{1:d}] failed to parse relocation", 2714 rel_section->GetName().AsCString(), i); 2715 break; 2716 } 2717 Symbol *symbol = nullptr; 2718 2719 if (hdr->Is32Bit()) { 2720 switch (hdr->e_machine) { 2721 case llvm::ELF::EM_ARM: 2722 switch (reloc_type(rel)) { 2723 case R_ARM_ABS32: 2724 ApplyELF32ABS32RelRelocation(symtab, rel, debug_data, rel_section); 2725 break; 2726 case R_ARM_REL32: 2727 GetModule()->ReportError("unsupported AArch32 relocation:" 2728 " .rel{0}[{1}], type {2}", 2729 rel_section->GetName().AsCString(), i, 2730 reloc_type(rel)); 2731 break; 2732 default: 2733 assert(false && "unexpected relocation type"); 2734 } 2735 break; 2736 case llvm::ELF::EM_386: 2737 switch (reloc_type(rel)) { 2738 case R_386_32: 2739 symbol = symtab->FindSymbolByID(reloc_symbol(rel)); 2740 if (symbol) { 2741 addr_t f_offset = 2742 rel_section->GetFileOffset() + ELFRelocation::RelocOffset32(rel); 2743 DataBufferSP &data_buffer_sp = debug_data.GetSharedDataBuffer(); 2744 // ObjectFileELF creates a WritableDataBuffer in CreateInstance. 2745 WritableDataBuffer *data_buffer = 2746 llvm::cast<WritableDataBuffer>(data_buffer_sp.get()); 2747 uint32_t *dst = reinterpret_cast<uint32_t *>( 2748 data_buffer->GetBytes() + f_offset); 2749 2750 addr_t value = symbol->GetAddressRef().GetFileAddress(); 2751 if (rel.IsRela()) { 2752 value += ELFRelocation::RelocAddend32(rel); 2753 } else { 2754 value += *dst; 2755 } 2756 *dst = value; 2757 } else { 2758 GetModule()->ReportError(".rel{0}[{1}] unknown symbol id: {2:d}", 2759 rel_section->GetName().AsCString(), i, 2760 reloc_symbol(rel)); 2761 } 2762 break; 2763 case R_386_NONE: 2764 case R_386_PC32: 2765 GetModule()->ReportError("unsupported i386 relocation:" 2766 " .rel{0}[{1}], type {2}", 2767 rel_section->GetName().AsCString(), i, 2768 reloc_type(rel)); 2769 break; 2770 default: 2771 assert(false && "unexpected relocation type"); 2772 break; 2773 } 2774 break; 2775 default: 2776 GetModule()->ReportError("unsupported 32-bit ELF machine arch: {0}", hdr->e_machine); 2777 break; 2778 } 2779 } else { 2780 switch (hdr->e_machine) { 2781 case llvm::ELF::EM_AARCH64: 2782 switch (reloc_type(rel)) { 2783 case R_AARCH64_ABS64: 2784 ApplyELF64ABS64Relocation(symtab, rel, debug_data, rel_section); 2785 break; 2786 case R_AARCH64_ABS32: 2787 ApplyELF64ABS32Relocation(symtab, rel, debug_data, rel_section, true); 2788 break; 2789 default: 2790 assert(false && "unexpected relocation type"); 2791 } 2792 break; 2793 case llvm::ELF::EM_LOONGARCH: 2794 switch (reloc_type(rel)) { 2795 case R_LARCH_64: 2796 ApplyELF64ABS64Relocation(symtab, rel, debug_data, rel_section); 2797 break; 2798 case R_LARCH_32: 2799 ApplyELF64ABS32Relocation(symtab, rel, debug_data, rel_section, true); 2800 break; 2801 default: 2802 assert(false && "unexpected relocation type"); 2803 } 2804 break; 2805 case llvm::ELF::EM_X86_64: 2806 switch (reloc_type(rel)) { 2807 case R_X86_64_64: 2808 ApplyELF64ABS64Relocation(symtab, rel, debug_data, rel_section); 2809 break; 2810 case R_X86_64_32: 2811 ApplyELF64ABS32Relocation(symtab, rel, debug_data, rel_section, 2812 false); 2813 break; 2814 case R_X86_64_32S: 2815 ApplyELF64ABS32Relocation(symtab, rel, debug_data, rel_section, true); 2816 break; 2817 case R_X86_64_PC32: 2818 default: 2819 assert(false && "unexpected relocation type"); 2820 } 2821 break; 2822 default: 2823 GetModule()->ReportError("unsupported 64-bit ELF machine arch: {0}", hdr->e_machine); 2824 break; 2825 } 2826 } 2827 } 2828 2829 return 0; 2830 } 2831 2832 unsigned ObjectFileELF::RelocateDebugSections(const ELFSectionHeader *rel_hdr, 2833 user_id_t rel_id, 2834 lldb_private::Symtab *thetab) { 2835 assert(rel_hdr->sh_type == SHT_RELA || rel_hdr->sh_type == SHT_REL); 2836 2837 // Parse in the section list if needed. 2838 SectionList *section_list = GetSectionList(); 2839 if (!section_list) 2840 return 0; 2841 2842 user_id_t symtab_id = rel_hdr->sh_link; 2843 user_id_t debug_id = rel_hdr->sh_info; 2844 2845 const ELFSectionHeader *symtab_hdr = GetSectionHeaderByIndex(symtab_id); 2846 if (!symtab_hdr) 2847 return 0; 2848 2849 const ELFSectionHeader *debug_hdr = GetSectionHeaderByIndex(debug_id); 2850 if (!debug_hdr) 2851 return 0; 2852 2853 Section *rel = section_list->FindSectionByID(rel_id).get(); 2854 if (!rel) 2855 return 0; 2856 2857 Section *symtab = section_list->FindSectionByID(symtab_id).get(); 2858 if (!symtab) 2859 return 0; 2860 2861 Section *debug = section_list->FindSectionByID(debug_id).get(); 2862 if (!debug) 2863 return 0; 2864 2865 DataExtractor rel_data; 2866 DataExtractor symtab_data; 2867 DataExtractor debug_data; 2868 2869 if (GetData(rel->GetFileOffset(), rel->GetFileSize(), rel_data) && 2870 GetData(symtab->GetFileOffset(), symtab->GetFileSize(), symtab_data) && 2871 GetData(debug->GetFileOffset(), debug->GetFileSize(), debug_data)) { 2872 ApplyRelocations(thetab, &m_header, rel_hdr, symtab_hdr, debug_hdr, 2873 rel_data, symtab_data, debug_data, debug); 2874 } 2875 2876 return 0; 2877 } 2878 2879 void ObjectFileELF::ParseSymtab(Symtab &lldb_symtab) { 2880 ModuleSP module_sp(GetModule()); 2881 if (!module_sp) 2882 return; 2883 2884 Progress progress( 2885 llvm::formatv("Parsing symbol table for {0}", 2886 m_file.GetFilename().AsCString("<Unknown>"))); 2887 ElapsedTime elapsed(module_sp->GetSymtabParseTime()); 2888 2889 // We always want to use the main object file so we (hopefully) only have one 2890 // cached copy of our symtab, dynamic sections, etc. 2891 ObjectFile *module_obj_file = module_sp->GetObjectFile(); 2892 if (module_obj_file && module_obj_file != this) 2893 return module_obj_file->ParseSymtab(lldb_symtab); 2894 2895 SectionList *section_list = module_sp->GetSectionList(); 2896 if (!section_list) 2897 return; 2898 2899 uint64_t symbol_id = 0; 2900 2901 // Sharable objects and dynamic executables usually have 2 distinct symbol 2902 // tables, one named ".symtab", and the other ".dynsym". The dynsym is a 2903 // smaller version of the symtab that only contains global symbols. The 2904 // information found in the dynsym is therefore also found in the symtab, 2905 // while the reverse is not necessarily true. 2906 Section *symtab = 2907 section_list->FindSectionByType(eSectionTypeELFSymbolTable, true).get(); 2908 if (symtab) 2909 symbol_id += ParseSymbolTable(&lldb_symtab, symbol_id, symtab); 2910 2911 // The symtab section is non-allocable and can be stripped, while the 2912 // .dynsym section which should always be always be there. To support the 2913 // minidebuginfo case we parse .dynsym when there's a .gnu_debuginfo 2914 // section, nomatter if .symtab was already parsed or not. This is because 2915 // minidebuginfo normally removes the .symtab symbols which have their 2916 // matching .dynsym counterparts. 2917 if (!symtab || 2918 GetSectionList()->FindSectionByName(ConstString(".gnu_debugdata"))) { 2919 Section *dynsym = 2920 section_list->FindSectionByType(eSectionTypeELFDynamicSymbols, true) 2921 .get(); 2922 if (dynsym) 2923 symbol_id += ParseSymbolTable(&lldb_symtab, symbol_id, dynsym); 2924 } 2925 2926 // DT_JMPREL 2927 // If present, this entry's d_ptr member holds the address of 2928 // relocation 2929 // entries associated solely with the procedure linkage table. 2930 // Separating 2931 // these relocation entries lets the dynamic linker ignore them during 2932 // process initialization, if lazy binding is enabled. If this entry is 2933 // present, the related entries of types DT_PLTRELSZ and DT_PLTREL must 2934 // also be present. 2935 const ELFDynamic *symbol = FindDynamicSymbol(DT_JMPREL); 2936 if (symbol) { 2937 // Synthesize trampoline symbols to help navigate the PLT. 2938 addr_t addr = symbol->d_ptr; 2939 Section *reloc_section = 2940 section_list->FindSectionContainingFileAddress(addr).get(); 2941 if (reloc_section) { 2942 user_id_t reloc_id = reloc_section->GetID(); 2943 const ELFSectionHeaderInfo *reloc_header = 2944 GetSectionHeaderByIndex(reloc_id); 2945 if (reloc_header) 2946 ParseTrampolineSymbols(&lldb_symtab, symbol_id, reloc_header, reloc_id); 2947 } 2948 } 2949 2950 if (DWARFCallFrameInfo *eh_frame = 2951 GetModule()->GetUnwindTable().GetEHFrameInfo()) { 2952 ParseUnwindSymbols(&lldb_symtab, eh_frame); 2953 } 2954 2955 // In the event that there's no symbol entry for the entry point we'll 2956 // artificially create one. We delegate to the symtab object the figuring 2957 // out of the proper size, this will usually make it span til the next 2958 // symbol it finds in the section. This means that if there are missing 2959 // symbols the entry point might span beyond its function definition. 2960 // We're fine with this as it doesn't make it worse than not having a 2961 // symbol entry at all. 2962 if (CalculateType() == eTypeExecutable) { 2963 ArchSpec arch = GetArchitecture(); 2964 auto entry_point_addr = GetEntryPointAddress(); 2965 bool is_valid_entry_point = 2966 entry_point_addr.IsValid() && entry_point_addr.IsSectionOffset(); 2967 addr_t entry_point_file_addr = entry_point_addr.GetFileAddress(); 2968 if (is_valid_entry_point && !lldb_symtab.FindSymbolContainingFileAddress( 2969 entry_point_file_addr)) { 2970 uint64_t symbol_id = lldb_symtab.GetNumSymbols(); 2971 // Don't set the name for any synthetic symbols, the Symbol 2972 // object will generate one if needed when the name is accessed 2973 // via accessors. 2974 SectionSP section_sp = entry_point_addr.GetSection(); 2975 Symbol symbol( 2976 /*symID=*/symbol_id, 2977 /*name=*/llvm::StringRef(), // Name will be auto generated. 2978 /*type=*/eSymbolTypeCode, 2979 /*external=*/true, 2980 /*is_debug=*/false, 2981 /*is_trampoline=*/false, 2982 /*is_artificial=*/true, 2983 /*section_sp=*/section_sp, 2984 /*offset=*/0, 2985 /*size=*/0, // FDE can span multiple symbols so don't use its size. 2986 /*size_is_valid=*/false, 2987 /*contains_linker_annotations=*/false, 2988 /*flags=*/0); 2989 // When the entry point is arm thumb we need to explicitly set its 2990 // class address to reflect that. This is important because expression 2991 // evaluation relies on correctly setting a breakpoint at this 2992 // address. 2993 if (arch.GetMachine() == llvm::Triple::arm && 2994 (entry_point_file_addr & 1)) { 2995 symbol.GetAddressRef().SetOffset(entry_point_addr.GetOffset() ^ 1); 2996 m_address_class_map[entry_point_file_addr ^ 1] = 2997 AddressClass::eCodeAlternateISA; 2998 } else { 2999 m_address_class_map[entry_point_file_addr] = AddressClass::eCode; 3000 } 3001 lldb_symtab.AddSymbol(symbol); 3002 } 3003 } 3004 } 3005 3006 void ObjectFileELF::RelocateSection(lldb_private::Section *section) 3007 { 3008 static const char *debug_prefix = ".debug"; 3009 3010 // Set relocated bit so we stop getting called, regardless of whether we 3011 // actually relocate. 3012 section->SetIsRelocated(true); 3013 3014 // We only relocate in ELF relocatable files 3015 if (CalculateType() != eTypeObjectFile) 3016 return; 3017 3018 const char *section_name = section->GetName().GetCString(); 3019 // Can't relocate that which can't be named 3020 if (section_name == nullptr) 3021 return; 3022 3023 // We don't relocate non-debug sections at the moment 3024 if (strncmp(section_name, debug_prefix, strlen(debug_prefix))) 3025 return; 3026 3027 // Relocation section names to look for 3028 std::string needle = std::string(".rel") + section_name; 3029 std::string needlea = std::string(".rela") + section_name; 3030 3031 for (SectionHeaderCollIter I = m_section_headers.begin(); 3032 I != m_section_headers.end(); ++I) { 3033 if (I->sh_type == SHT_RELA || I->sh_type == SHT_REL) { 3034 const char *hay_name = I->section_name.GetCString(); 3035 if (hay_name == nullptr) 3036 continue; 3037 if (needle == hay_name || needlea == hay_name) { 3038 const ELFSectionHeader &reloc_header = *I; 3039 user_id_t reloc_id = SectionIndex(I); 3040 RelocateDebugSections(&reloc_header, reloc_id, GetSymtab()); 3041 break; 3042 } 3043 } 3044 } 3045 } 3046 3047 void ObjectFileELF::ParseUnwindSymbols(Symtab *symbol_table, 3048 DWARFCallFrameInfo *eh_frame) { 3049 SectionList *section_list = GetSectionList(); 3050 if (!section_list) 3051 return; 3052 3053 // First we save the new symbols into a separate list and add them to the 3054 // symbol table after we collected all symbols we want to add. This is 3055 // neccessary because adding a new symbol invalidates the internal index of 3056 // the symtab what causing the next lookup to be slow because it have to 3057 // recalculate the index first. 3058 std::vector<Symbol> new_symbols; 3059 3060 size_t num_symbols = symbol_table->GetNumSymbols(); 3061 uint64_t last_symbol_id = 3062 num_symbols ? symbol_table->SymbolAtIndex(num_symbols - 1)->GetID() : 0; 3063 eh_frame->ForEachFDEEntries([&](lldb::addr_t file_addr, uint32_t size, 3064 dw_offset_t) { 3065 Symbol *symbol = symbol_table->FindSymbolAtFileAddress(file_addr); 3066 if (symbol) { 3067 if (!symbol->GetByteSizeIsValid()) { 3068 symbol->SetByteSize(size); 3069 symbol->SetSizeIsSynthesized(true); 3070 } 3071 } else { 3072 SectionSP section_sp = 3073 section_list->FindSectionContainingFileAddress(file_addr); 3074 if (section_sp) { 3075 addr_t offset = file_addr - section_sp->GetFileAddress(); 3076 uint64_t symbol_id = ++last_symbol_id; 3077 // Don't set the name for any synthetic symbols, the Symbol 3078 // object will generate one if needed when the name is accessed 3079 // via accessors. 3080 Symbol eh_symbol( 3081 /*symID=*/symbol_id, 3082 /*name=*/llvm::StringRef(), // Name will be auto generated. 3083 /*type=*/eSymbolTypeCode, 3084 /*external=*/true, 3085 /*is_debug=*/false, 3086 /*is_trampoline=*/false, 3087 /*is_artificial=*/true, 3088 /*section_sp=*/section_sp, 3089 /*offset=*/offset, 3090 /*size=*/0, // FDE can span multiple symbols so don't use its size. 3091 /*size_is_valid=*/false, 3092 /*contains_linker_annotations=*/false, 3093 /*flags=*/0); 3094 new_symbols.push_back(eh_symbol); 3095 } 3096 } 3097 return true; 3098 }); 3099 3100 for (const Symbol &s : new_symbols) 3101 symbol_table->AddSymbol(s); 3102 } 3103 3104 bool ObjectFileELF::IsStripped() { 3105 // TODO: determine this for ELF 3106 return false; 3107 } 3108 3109 //===----------------------------------------------------------------------===// 3110 // Dump 3111 // 3112 // Dump the specifics of the runtime file container (such as any headers 3113 // segments, sections, etc). 3114 void ObjectFileELF::Dump(Stream *s) { 3115 ModuleSP module_sp(GetModule()); 3116 if (!module_sp) { 3117 return; 3118 } 3119 3120 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); 3121 s->Printf("%p: ", static_cast<void *>(this)); 3122 s->Indent(); 3123 s->PutCString("ObjectFileELF"); 3124 3125 ArchSpec header_arch = GetArchitecture(); 3126 3127 *s << ", file = '" << m_file 3128 << "', arch = " << header_arch.GetArchitectureName() << "\n"; 3129 3130 DumpELFHeader(s, m_header); 3131 s->EOL(); 3132 DumpELFProgramHeaders(s); 3133 s->EOL(); 3134 DumpELFSectionHeaders(s); 3135 s->EOL(); 3136 SectionList *section_list = GetSectionList(); 3137 if (section_list) 3138 section_list->Dump(s->AsRawOstream(), s->GetIndentLevel(), nullptr, true, 3139 UINT32_MAX); 3140 Symtab *symtab = GetSymtab(); 3141 if (symtab) 3142 symtab->Dump(s, nullptr, eSortOrderNone); 3143 s->EOL(); 3144 DumpDependentModules(s); 3145 s->EOL(); 3146 } 3147 3148 // DumpELFHeader 3149 // 3150 // Dump the ELF header to the specified output stream 3151 void ObjectFileELF::DumpELFHeader(Stream *s, const ELFHeader &header) { 3152 s->PutCString("ELF Header\n"); 3153 s->Printf("e_ident[EI_MAG0 ] = 0x%2.2x\n", header.e_ident[EI_MAG0]); 3154 s->Printf("e_ident[EI_MAG1 ] = 0x%2.2x '%c'\n", header.e_ident[EI_MAG1], 3155 header.e_ident[EI_MAG1]); 3156 s->Printf("e_ident[EI_MAG2 ] = 0x%2.2x '%c'\n", header.e_ident[EI_MAG2], 3157 header.e_ident[EI_MAG2]); 3158 s->Printf("e_ident[EI_MAG3 ] = 0x%2.2x '%c'\n", header.e_ident[EI_MAG3], 3159 header.e_ident[EI_MAG3]); 3160 3161 s->Printf("e_ident[EI_CLASS ] = 0x%2.2x\n", header.e_ident[EI_CLASS]); 3162 s->Printf("e_ident[EI_DATA ] = 0x%2.2x ", header.e_ident[EI_DATA]); 3163 DumpELFHeader_e_ident_EI_DATA(s, header.e_ident[EI_DATA]); 3164 s->Printf("\ne_ident[EI_VERSION] = 0x%2.2x\n", header.e_ident[EI_VERSION]); 3165 s->Printf("e_ident[EI_PAD ] = 0x%2.2x\n", header.e_ident[EI_PAD]); 3166 3167 s->Printf("e_type = 0x%4.4x ", header.e_type); 3168 DumpELFHeader_e_type(s, header.e_type); 3169 s->Printf("\ne_machine = 0x%4.4x\n", header.e_machine); 3170 s->Printf("e_version = 0x%8.8x\n", header.e_version); 3171 s->Printf("e_entry = 0x%8.8" PRIx64 "\n", header.e_entry); 3172 s->Printf("e_phoff = 0x%8.8" PRIx64 "\n", header.e_phoff); 3173 s->Printf("e_shoff = 0x%8.8" PRIx64 "\n", header.e_shoff); 3174 s->Printf("e_flags = 0x%8.8x\n", header.e_flags); 3175 s->Printf("e_ehsize = 0x%4.4x\n", header.e_ehsize); 3176 s->Printf("e_phentsize = 0x%4.4x\n", header.e_phentsize); 3177 s->Printf("e_phnum = 0x%8.8x\n", header.e_phnum); 3178 s->Printf("e_shentsize = 0x%4.4x\n", header.e_shentsize); 3179 s->Printf("e_shnum = 0x%8.8x\n", header.e_shnum); 3180 s->Printf("e_shstrndx = 0x%8.8x\n", header.e_shstrndx); 3181 } 3182 3183 // DumpELFHeader_e_type 3184 // 3185 // Dump an token value for the ELF header member e_type 3186 void ObjectFileELF::DumpELFHeader_e_type(Stream *s, elf_half e_type) { 3187 switch (e_type) { 3188 case ET_NONE: 3189 *s << "ET_NONE"; 3190 break; 3191 case ET_REL: 3192 *s << "ET_REL"; 3193 break; 3194 case ET_EXEC: 3195 *s << "ET_EXEC"; 3196 break; 3197 case ET_DYN: 3198 *s << "ET_DYN"; 3199 break; 3200 case ET_CORE: 3201 *s << "ET_CORE"; 3202 break; 3203 default: 3204 break; 3205 } 3206 } 3207 3208 // DumpELFHeader_e_ident_EI_DATA 3209 // 3210 // Dump an token value for the ELF header member e_ident[EI_DATA] 3211 void ObjectFileELF::DumpELFHeader_e_ident_EI_DATA(Stream *s, 3212 unsigned char ei_data) { 3213 switch (ei_data) { 3214 case ELFDATANONE: 3215 *s << "ELFDATANONE"; 3216 break; 3217 case ELFDATA2LSB: 3218 *s << "ELFDATA2LSB - Little Endian"; 3219 break; 3220 case ELFDATA2MSB: 3221 *s << "ELFDATA2MSB - Big Endian"; 3222 break; 3223 default: 3224 break; 3225 } 3226 } 3227 3228 // DumpELFProgramHeader 3229 // 3230 // Dump a single ELF program header to the specified output stream 3231 void ObjectFileELF::DumpELFProgramHeader(Stream *s, 3232 const ELFProgramHeader &ph) { 3233 DumpELFProgramHeader_p_type(s, ph.p_type); 3234 s->Printf(" %8.8" PRIx64 " %8.8" PRIx64 " %8.8" PRIx64, ph.p_offset, 3235 ph.p_vaddr, ph.p_paddr); 3236 s->Printf(" %8.8" PRIx64 " %8.8" PRIx64 " %8.8x (", ph.p_filesz, ph.p_memsz, 3237 ph.p_flags); 3238 3239 DumpELFProgramHeader_p_flags(s, ph.p_flags); 3240 s->Printf(") %8.8" PRIx64, ph.p_align); 3241 } 3242 3243 // DumpELFProgramHeader_p_type 3244 // 3245 // Dump an token value for the ELF program header member p_type which describes 3246 // the type of the program header 3247 void ObjectFileELF::DumpELFProgramHeader_p_type(Stream *s, elf_word p_type) { 3248 const int kStrWidth = 15; 3249 switch (p_type) { 3250 CASE_AND_STREAM(s, PT_NULL, kStrWidth); 3251 CASE_AND_STREAM(s, PT_LOAD, kStrWidth); 3252 CASE_AND_STREAM(s, PT_DYNAMIC, kStrWidth); 3253 CASE_AND_STREAM(s, PT_INTERP, kStrWidth); 3254 CASE_AND_STREAM(s, PT_NOTE, kStrWidth); 3255 CASE_AND_STREAM(s, PT_SHLIB, kStrWidth); 3256 CASE_AND_STREAM(s, PT_PHDR, kStrWidth); 3257 CASE_AND_STREAM(s, PT_TLS, kStrWidth); 3258 CASE_AND_STREAM(s, PT_GNU_EH_FRAME, kStrWidth); 3259 default: 3260 s->Printf("0x%8.8x%*s", p_type, kStrWidth - 10, ""); 3261 break; 3262 } 3263 } 3264 3265 // DumpELFProgramHeader_p_flags 3266 // 3267 // Dump an token value for the ELF program header member p_flags 3268 void ObjectFileELF::DumpELFProgramHeader_p_flags(Stream *s, elf_word p_flags) { 3269 *s << ((p_flags & PF_X) ? "PF_X" : " ") 3270 << (((p_flags & PF_X) && (p_flags & PF_W)) ? '+' : ' ') 3271 << ((p_flags & PF_W) ? "PF_W" : " ") 3272 << (((p_flags & PF_W) && (p_flags & PF_R)) ? '+' : ' ') 3273 << ((p_flags & PF_R) ? "PF_R" : " "); 3274 } 3275 3276 // DumpELFProgramHeaders 3277 // 3278 // Dump all of the ELF program header to the specified output stream 3279 void ObjectFileELF::DumpELFProgramHeaders(Stream *s) { 3280 if (!ParseProgramHeaders()) 3281 return; 3282 3283 s->PutCString("Program Headers\n"); 3284 s->PutCString("IDX p_type p_offset p_vaddr p_paddr " 3285 "p_filesz p_memsz p_flags p_align\n"); 3286 s->PutCString("==== --------------- -------- -------- -------- " 3287 "-------- -------- ------------------------- --------\n"); 3288 3289 for (const auto &H : llvm::enumerate(m_program_headers)) { 3290 s->Format("[{0,2}] ", H.index()); 3291 ObjectFileELF::DumpELFProgramHeader(s, H.value()); 3292 s->EOL(); 3293 } 3294 } 3295 3296 // DumpELFSectionHeader 3297 // 3298 // Dump a single ELF section header to the specified output stream 3299 void ObjectFileELF::DumpELFSectionHeader(Stream *s, 3300 const ELFSectionHeaderInfo &sh) { 3301 s->Printf("%8.8x ", sh.sh_name); 3302 DumpELFSectionHeader_sh_type(s, sh.sh_type); 3303 s->Printf(" %8.8" PRIx64 " (", sh.sh_flags); 3304 DumpELFSectionHeader_sh_flags(s, sh.sh_flags); 3305 s->Printf(") %8.8" PRIx64 " %8.8" PRIx64 " %8.8" PRIx64, sh.sh_addr, 3306 sh.sh_offset, sh.sh_size); 3307 s->Printf(" %8.8x %8.8x", sh.sh_link, sh.sh_info); 3308 s->Printf(" %8.8" PRIx64 " %8.8" PRIx64, sh.sh_addralign, sh.sh_entsize); 3309 } 3310 3311 // DumpELFSectionHeader_sh_type 3312 // 3313 // Dump an token value for the ELF section header member sh_type which 3314 // describes the type of the section 3315 void ObjectFileELF::DumpELFSectionHeader_sh_type(Stream *s, elf_word sh_type) { 3316 const int kStrWidth = 12; 3317 switch (sh_type) { 3318 CASE_AND_STREAM(s, SHT_NULL, kStrWidth); 3319 CASE_AND_STREAM(s, SHT_PROGBITS, kStrWidth); 3320 CASE_AND_STREAM(s, SHT_SYMTAB, kStrWidth); 3321 CASE_AND_STREAM(s, SHT_STRTAB, kStrWidth); 3322 CASE_AND_STREAM(s, SHT_RELA, kStrWidth); 3323 CASE_AND_STREAM(s, SHT_HASH, kStrWidth); 3324 CASE_AND_STREAM(s, SHT_DYNAMIC, kStrWidth); 3325 CASE_AND_STREAM(s, SHT_NOTE, kStrWidth); 3326 CASE_AND_STREAM(s, SHT_NOBITS, kStrWidth); 3327 CASE_AND_STREAM(s, SHT_REL, kStrWidth); 3328 CASE_AND_STREAM(s, SHT_SHLIB, kStrWidth); 3329 CASE_AND_STREAM(s, SHT_DYNSYM, kStrWidth); 3330 CASE_AND_STREAM(s, SHT_LOPROC, kStrWidth); 3331 CASE_AND_STREAM(s, SHT_HIPROC, kStrWidth); 3332 CASE_AND_STREAM(s, SHT_LOUSER, kStrWidth); 3333 CASE_AND_STREAM(s, SHT_HIUSER, kStrWidth); 3334 default: 3335 s->Printf("0x%8.8x%*s", sh_type, kStrWidth - 10, ""); 3336 break; 3337 } 3338 } 3339 3340 // DumpELFSectionHeader_sh_flags 3341 // 3342 // Dump an token value for the ELF section header member sh_flags 3343 void ObjectFileELF::DumpELFSectionHeader_sh_flags(Stream *s, 3344 elf_xword sh_flags) { 3345 *s << ((sh_flags & SHF_WRITE) ? "WRITE" : " ") 3346 << (((sh_flags & SHF_WRITE) && (sh_flags & SHF_ALLOC)) ? '+' : ' ') 3347 << ((sh_flags & SHF_ALLOC) ? "ALLOC" : " ") 3348 << (((sh_flags & SHF_ALLOC) && (sh_flags & SHF_EXECINSTR)) ? '+' : ' ') 3349 << ((sh_flags & SHF_EXECINSTR) ? "EXECINSTR" : " "); 3350 } 3351 3352 // DumpELFSectionHeaders 3353 // 3354 // Dump all of the ELF section header to the specified output stream 3355 void ObjectFileELF::DumpELFSectionHeaders(Stream *s) { 3356 if (!ParseSectionHeaders()) 3357 return; 3358 3359 s->PutCString("Section Headers\n"); 3360 s->PutCString("IDX name type flags " 3361 "addr offset size link info addralgn " 3362 "entsize Name\n"); 3363 s->PutCString("==== -------- ------------ -------------------------------- " 3364 "-------- -------- -------- -------- -------- -------- " 3365 "-------- ====================\n"); 3366 3367 uint32_t idx = 0; 3368 for (SectionHeaderCollConstIter I = m_section_headers.begin(); 3369 I != m_section_headers.end(); ++I, ++idx) { 3370 s->Printf("[%2u] ", idx); 3371 ObjectFileELF::DumpELFSectionHeader(s, *I); 3372 const char *section_name = I->section_name.AsCString(""); 3373 if (section_name) 3374 *s << ' ' << section_name << "\n"; 3375 } 3376 } 3377 3378 void ObjectFileELF::DumpDependentModules(lldb_private::Stream *s) { 3379 size_t num_modules = ParseDependentModules(); 3380 3381 if (num_modules > 0) { 3382 s->PutCString("Dependent Modules:\n"); 3383 for (unsigned i = 0; i < num_modules; ++i) { 3384 const FileSpec &spec = m_filespec_up->GetFileSpecAtIndex(i); 3385 s->Printf(" %s\n", spec.GetFilename().GetCString()); 3386 } 3387 } 3388 } 3389 3390 ArchSpec ObjectFileELF::GetArchitecture() { 3391 if (!ParseHeader()) 3392 return ArchSpec(); 3393 3394 if (m_section_headers.empty()) { 3395 // Allow elf notes to be parsed which may affect the detected architecture. 3396 ParseSectionHeaders(); 3397 } 3398 3399 if (CalculateType() == eTypeCoreFile && 3400 !m_arch_spec.TripleOSWasSpecified()) { 3401 // Core files don't have section headers yet they have PT_NOTE program 3402 // headers that might shed more light on the architecture 3403 for (const elf::ELFProgramHeader &H : ProgramHeaders()) { 3404 if (H.p_type != PT_NOTE || H.p_offset == 0 || H.p_filesz == 0) 3405 continue; 3406 DataExtractor data; 3407 if (data.SetData(m_data, H.p_offset, H.p_filesz) == H.p_filesz) { 3408 UUID uuid; 3409 RefineModuleDetailsFromNote(data, m_arch_spec, uuid); 3410 } 3411 } 3412 } 3413 return m_arch_spec; 3414 } 3415 3416 ObjectFile::Type ObjectFileELF::CalculateType() { 3417 switch (m_header.e_type) { 3418 case llvm::ELF::ET_NONE: 3419 // 0 - No file type 3420 return eTypeUnknown; 3421 3422 case llvm::ELF::ET_REL: 3423 // 1 - Relocatable file 3424 return eTypeObjectFile; 3425 3426 case llvm::ELF::ET_EXEC: 3427 // 2 - Executable file 3428 return eTypeExecutable; 3429 3430 case llvm::ELF::ET_DYN: 3431 // 3 - Shared object file 3432 return eTypeSharedLibrary; 3433 3434 case ET_CORE: 3435 // 4 - Core file 3436 return eTypeCoreFile; 3437 3438 default: 3439 break; 3440 } 3441 return eTypeUnknown; 3442 } 3443 3444 ObjectFile::Strata ObjectFileELF::CalculateStrata() { 3445 switch (m_header.e_type) { 3446 case llvm::ELF::ET_NONE: 3447 // 0 - No file type 3448 return eStrataUnknown; 3449 3450 case llvm::ELF::ET_REL: 3451 // 1 - Relocatable file 3452 return eStrataUnknown; 3453 3454 case llvm::ELF::ET_EXEC: 3455 // 2 - Executable file 3456 // TODO: is there any way to detect that an executable is a kernel 3457 // related executable by inspecting the program headers, section headers, 3458 // symbols, or any other flag bits??? 3459 return eStrataUser; 3460 3461 case llvm::ELF::ET_DYN: 3462 // 3 - Shared object file 3463 // TODO: is there any way to detect that an shared library is a kernel 3464 // related executable by inspecting the program headers, section headers, 3465 // symbols, or any other flag bits??? 3466 return eStrataUnknown; 3467 3468 case ET_CORE: 3469 // 4 - Core file 3470 // TODO: is there any way to detect that an core file is a kernel 3471 // related executable by inspecting the program headers, section headers, 3472 // symbols, or any other flag bits??? 3473 return eStrataUnknown; 3474 3475 default: 3476 break; 3477 } 3478 return eStrataUnknown; 3479 } 3480 3481 size_t ObjectFileELF::ReadSectionData(Section *section, 3482 lldb::offset_t section_offset, void *dst, 3483 size_t dst_len) { 3484 // If some other objectfile owns this data, pass this to them. 3485 if (section->GetObjectFile() != this) 3486 return section->GetObjectFile()->ReadSectionData(section, section_offset, 3487 dst, dst_len); 3488 3489 if (!section->Test(SHF_COMPRESSED)) 3490 return ObjectFile::ReadSectionData(section, section_offset, dst, dst_len); 3491 3492 // For compressed sections we need to read to full data to be able to 3493 // decompress. 3494 DataExtractor data; 3495 ReadSectionData(section, data); 3496 return data.CopyData(section_offset, dst_len, dst); 3497 } 3498 3499 size_t ObjectFileELF::ReadSectionData(Section *section, 3500 DataExtractor §ion_data) { 3501 // If some other objectfile owns this data, pass this to them. 3502 if (section->GetObjectFile() != this) 3503 return section->GetObjectFile()->ReadSectionData(section, section_data); 3504 3505 size_t result = ObjectFile::ReadSectionData(section, section_data); 3506 if (result == 0 || !(section->Get() & llvm::ELF::SHF_COMPRESSED)) 3507 return result; 3508 3509 auto Decompressor = llvm::object::Decompressor::create( 3510 section->GetName().GetStringRef(), 3511 {reinterpret_cast<const char *>(section_data.GetDataStart()), 3512 size_t(section_data.GetByteSize())}, 3513 GetByteOrder() == eByteOrderLittle, GetAddressByteSize() == 8); 3514 if (!Decompressor) { 3515 GetModule()->ReportWarning( 3516 "Unable to initialize decompressor for section '{0}': {1}", 3517 section->GetName().GetCString(), 3518 llvm::toString(Decompressor.takeError()).c_str()); 3519 section_data.Clear(); 3520 return 0; 3521 } 3522 3523 auto buffer_sp = 3524 std::make_shared<DataBufferHeap>(Decompressor->getDecompressedSize(), 0); 3525 if (auto error = Decompressor->decompress( 3526 {buffer_sp->GetBytes(), size_t(buffer_sp->GetByteSize())})) { 3527 GetModule()->ReportWarning("Decompression of section '{0}' failed: {1}", 3528 section->GetName().GetCString(), 3529 llvm::toString(std::move(error)).c_str()); 3530 section_data.Clear(); 3531 return 0; 3532 } 3533 3534 section_data.SetData(buffer_sp); 3535 return buffer_sp->GetByteSize(); 3536 } 3537 3538 llvm::ArrayRef<ELFProgramHeader> ObjectFileELF::ProgramHeaders() { 3539 ParseProgramHeaders(); 3540 return m_program_headers; 3541 } 3542 3543 DataExtractor ObjectFileELF::GetSegmentData(const ELFProgramHeader &H) { 3544 return DataExtractor(m_data, H.p_offset, H.p_filesz); 3545 } 3546 3547 bool ObjectFileELF::AnySegmentHasPhysicalAddress() { 3548 for (const ELFProgramHeader &H : ProgramHeaders()) { 3549 if (H.p_paddr != 0) 3550 return true; 3551 } 3552 return false; 3553 } 3554 3555 std::vector<ObjectFile::LoadableData> 3556 ObjectFileELF::GetLoadableData(Target &target) { 3557 // Create a list of loadable data from loadable segments, using physical 3558 // addresses if they aren't all null 3559 std::vector<LoadableData> loadables; 3560 bool should_use_paddr = AnySegmentHasPhysicalAddress(); 3561 for (const ELFProgramHeader &H : ProgramHeaders()) { 3562 LoadableData loadable; 3563 if (H.p_type != llvm::ELF::PT_LOAD) 3564 continue; 3565 loadable.Dest = should_use_paddr ? H.p_paddr : H.p_vaddr; 3566 if (loadable.Dest == LLDB_INVALID_ADDRESS) 3567 continue; 3568 if (H.p_filesz == 0) 3569 continue; 3570 auto segment_data = GetSegmentData(H); 3571 loadable.Contents = llvm::ArrayRef<uint8_t>(segment_data.GetDataStart(), 3572 segment_data.GetByteSize()); 3573 loadables.push_back(loadable); 3574 } 3575 return loadables; 3576 } 3577 3578 lldb::WritableDataBufferSP 3579 ObjectFileELF::MapFileDataWritable(const FileSpec &file, uint64_t Size, 3580 uint64_t Offset) { 3581 return FileSystem::Instance().CreateWritableDataBuffer(file.GetPath(), Size, 3582 Offset); 3583 } 3584