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