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