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