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