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