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