Path: blob/main/contrib/llvm-project/lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
39642 views
//===-- ObjectFileELF.cpp -------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "ObjectFileELF.h"910#include <algorithm>11#include <cassert>12#include <optional>13#include <unordered_map>1415#include "lldb/Core/Module.h"16#include "lldb/Core/ModuleSpec.h"17#include "lldb/Core/PluginManager.h"18#include "lldb/Core/Progress.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/FileSpecList.h"29#include "lldb/Utility/LLDBLog.h"30#include "lldb/Utility/Log.h"31#include "lldb/Utility/RangeMap.h"32#include "lldb/Utility/Status.h"33#include "lldb/Utility/Stream.h"34#include "lldb/Utility/Timer.h"35#include "llvm/ADT/IntervalMap.h"36#include "llvm/ADT/PointerUnion.h"37#include "llvm/ADT/StringRef.h"38#include "llvm/BinaryFormat/ELF.h"39#include "llvm/Object/Decompressor.h"40#include "llvm/Support/ARMBuildAttributes.h"41#include "llvm/Support/CRC.h"42#include "llvm/Support/FormatVariadic.h"43#include "llvm/Support/MathExtras.h"44#include "llvm/Support/MemoryBuffer.h"45#include "llvm/Support/MipsABIFlags.h"4647#define CASE_AND_STREAM(s, def, width) \48case def: \49s->Printf("%-*s", width, #def); \50break;5152using namespace lldb;53using namespace lldb_private;54using namespace elf;55using namespace llvm::ELF;5657LLDB_PLUGIN_DEFINE(ObjectFileELF)5859// ELF note owner definitions60static const char *const LLDB_NT_OWNER_FREEBSD = "FreeBSD";61static const char *const LLDB_NT_OWNER_GNU = "GNU";62static const char *const LLDB_NT_OWNER_NETBSD = "NetBSD";63static const char *const LLDB_NT_OWNER_NETBSDCORE = "NetBSD-CORE";64static const char *const LLDB_NT_OWNER_OPENBSD = "OpenBSD";65static const char *const LLDB_NT_OWNER_ANDROID = "Android";66static const char *const LLDB_NT_OWNER_CORE = "CORE";67static const char *const LLDB_NT_OWNER_LINUX = "LINUX";6869// ELF note type definitions70static const elf_word LLDB_NT_FREEBSD_ABI_TAG = 0x01;71static const elf_word LLDB_NT_FREEBSD_ABI_SIZE = 4;7273static const elf_word LLDB_NT_GNU_ABI_TAG = 0x01;74static const elf_word LLDB_NT_GNU_ABI_SIZE = 16;7576static const elf_word LLDB_NT_GNU_BUILD_ID_TAG = 0x03;7778static const elf_word LLDB_NT_NETBSD_IDENT_TAG = 1;79static const elf_word LLDB_NT_NETBSD_IDENT_DESCSZ = 4;80static const elf_word LLDB_NT_NETBSD_IDENT_NAMESZ = 7;81static const elf_word LLDB_NT_NETBSD_PROCINFO = 1;8283// GNU ABI note OS constants84static const elf_word LLDB_NT_GNU_ABI_OS_LINUX = 0x00;85static const elf_word LLDB_NT_GNU_ABI_OS_HURD = 0x01;86static const elf_word LLDB_NT_GNU_ABI_OS_SOLARIS = 0x02;8788namespace {8990//===----------------------------------------------------------------------===//91/// \class ELFRelocation92/// Generic wrapper for ELFRel and ELFRela.93///94/// This helper class allows us to parse both ELFRel and ELFRela relocation95/// entries in a generic manner.96class ELFRelocation {97public:98/// Constructs an ELFRelocation entry with a personality as given by @p99/// type.100///101/// \param type Either DT_REL or DT_RELA. Any other value is invalid.102ELFRelocation(unsigned type);103104~ELFRelocation();105106bool Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset);107108static unsigned RelocType32(const ELFRelocation &rel);109110static unsigned RelocType64(const ELFRelocation &rel);111112static unsigned RelocSymbol32(const ELFRelocation &rel);113114static unsigned RelocSymbol64(const ELFRelocation &rel);115116static elf_addr RelocOffset32(const ELFRelocation &rel);117118static elf_addr RelocOffset64(const ELFRelocation &rel);119120static elf_sxword RelocAddend32(const ELFRelocation &rel);121122static elf_sxword RelocAddend64(const ELFRelocation &rel);123124bool IsRela() { return (reloc.is<ELFRela *>()); }125126private:127typedef llvm::PointerUnion<ELFRel *, ELFRela *> RelocUnion;128129RelocUnion reloc;130};131} // end anonymous namespace132133ELFRelocation::ELFRelocation(unsigned type) {134if (type == DT_REL || type == SHT_REL)135reloc = new ELFRel();136else if (type == DT_RELA || type == SHT_RELA)137reloc = new ELFRela();138else {139assert(false && "unexpected relocation type");140reloc = static_cast<ELFRel *>(nullptr);141}142}143144ELFRelocation::~ELFRelocation() {145if (reloc.is<ELFRel *>())146delete reloc.get<ELFRel *>();147else148delete reloc.get<ELFRela *>();149}150151bool ELFRelocation::Parse(const lldb_private::DataExtractor &data,152lldb::offset_t *offset) {153if (reloc.is<ELFRel *>())154return reloc.get<ELFRel *>()->Parse(data, offset);155else156return reloc.get<ELFRela *>()->Parse(data, offset);157}158159unsigned ELFRelocation::RelocType32(const ELFRelocation &rel) {160if (rel.reloc.is<ELFRel *>())161return ELFRel::RelocType32(*rel.reloc.get<ELFRel *>());162else163return ELFRela::RelocType32(*rel.reloc.get<ELFRela *>());164}165166unsigned ELFRelocation::RelocType64(const ELFRelocation &rel) {167if (rel.reloc.is<ELFRel *>())168return ELFRel::RelocType64(*rel.reloc.get<ELFRel *>());169else170return ELFRela::RelocType64(*rel.reloc.get<ELFRela *>());171}172173unsigned ELFRelocation::RelocSymbol32(const ELFRelocation &rel) {174if (rel.reloc.is<ELFRel *>())175return ELFRel::RelocSymbol32(*rel.reloc.get<ELFRel *>());176else177return ELFRela::RelocSymbol32(*rel.reloc.get<ELFRela *>());178}179180unsigned ELFRelocation::RelocSymbol64(const ELFRelocation &rel) {181if (rel.reloc.is<ELFRel *>())182return ELFRel::RelocSymbol64(*rel.reloc.get<ELFRel *>());183else184return ELFRela::RelocSymbol64(*rel.reloc.get<ELFRela *>());185}186187elf_addr ELFRelocation::RelocOffset32(const ELFRelocation &rel) {188if (rel.reloc.is<ELFRel *>())189return rel.reloc.get<ELFRel *>()->r_offset;190else191return rel.reloc.get<ELFRela *>()->r_offset;192}193194elf_addr ELFRelocation::RelocOffset64(const ELFRelocation &rel) {195if (rel.reloc.is<ELFRel *>())196return rel.reloc.get<ELFRel *>()->r_offset;197else198return rel.reloc.get<ELFRela *>()->r_offset;199}200201elf_sxword ELFRelocation::RelocAddend32(const ELFRelocation &rel) {202if (rel.reloc.is<ELFRel *>())203return 0;204else205return rel.reloc.get<ELFRela *>()->r_addend;206}207208elf_sxword ELFRelocation::RelocAddend64(const ELFRelocation &rel) {209if (rel.reloc.is<ELFRel *>())210return 0;211else212return rel.reloc.get<ELFRela *>()->r_addend;213}214215static user_id_t SegmentID(size_t PHdrIndex) {216return ~user_id_t(PHdrIndex);217}218219bool ELFNote::Parse(const DataExtractor &data, lldb::offset_t *offset) {220// Read all fields.221if (data.GetU32(offset, &n_namesz, 3) == nullptr)222return false;223224// The name field is required to be nul-terminated, and n_namesz includes the225// terminating nul in observed implementations (contrary to the ELF-64 spec).226// A special case is needed for cores generated by some older Linux versions,227// which write a note named "CORE" without a nul terminator and n_namesz = 4.228if (n_namesz == 4) {229char buf[4];230if (data.ExtractBytes(*offset, 4, data.GetByteOrder(), buf) != 4)231return false;232if (strncmp(buf, "CORE", 4) == 0) {233n_name = "CORE";234*offset += 4;235return true;236}237}238239const char *cstr = data.GetCStr(offset, llvm::alignTo(n_namesz, 4));240if (cstr == nullptr) {241Log *log = GetLog(LLDBLog::Symbols);242LLDB_LOGF(log, "Failed to parse note name lacking nul terminator");243244return false;245}246n_name = cstr;247return true;248}249250static uint32_t mipsVariantFromElfFlags (const elf::ELFHeader &header) {251const uint32_t mips_arch = header.e_flags & llvm::ELF::EF_MIPS_ARCH;252uint32_t endian = header.e_ident[EI_DATA];253uint32_t arch_variant = ArchSpec::eMIPSSubType_unknown;254uint32_t fileclass = header.e_ident[EI_CLASS];255256// If there aren't any elf flags available (e.g core elf file) then return257// default258// 32 or 64 bit arch (without any architecture revision) based on object file's class.259if (header.e_type == ET_CORE) {260switch (fileclass) {261case llvm::ELF::ELFCLASS32:262return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips32el263: ArchSpec::eMIPSSubType_mips32;264case llvm::ELF::ELFCLASS64:265return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips64el266: ArchSpec::eMIPSSubType_mips64;267default:268return arch_variant;269}270}271272switch (mips_arch) {273case llvm::ELF::EF_MIPS_ARCH_1:274case llvm::ELF::EF_MIPS_ARCH_2:275case llvm::ELF::EF_MIPS_ARCH_32:276return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips32el277: ArchSpec::eMIPSSubType_mips32;278case llvm::ELF::EF_MIPS_ARCH_32R2:279return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips32r2el280: ArchSpec::eMIPSSubType_mips32r2;281case llvm::ELF::EF_MIPS_ARCH_32R6:282return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips32r6el283: ArchSpec::eMIPSSubType_mips32r6;284case llvm::ELF::EF_MIPS_ARCH_3:285case llvm::ELF::EF_MIPS_ARCH_4:286case llvm::ELF::EF_MIPS_ARCH_5:287case llvm::ELF::EF_MIPS_ARCH_64:288return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips64el289: ArchSpec::eMIPSSubType_mips64;290case llvm::ELF::EF_MIPS_ARCH_64R2:291return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips64r2el292: ArchSpec::eMIPSSubType_mips64r2;293case llvm::ELF::EF_MIPS_ARCH_64R6:294return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips64r6el295: ArchSpec::eMIPSSubType_mips64r6;296default:297break;298}299300return arch_variant;301}302303static uint32_t riscvVariantFromElfFlags(const elf::ELFHeader &header) {304uint32_t fileclass = header.e_ident[EI_CLASS];305switch (fileclass) {306case llvm::ELF::ELFCLASS32:307return ArchSpec::eRISCVSubType_riscv32;308case llvm::ELF::ELFCLASS64:309return ArchSpec::eRISCVSubType_riscv64;310default:311return ArchSpec::eRISCVSubType_unknown;312}313}314315static uint32_t ppc64VariantFromElfFlags(const elf::ELFHeader &header) {316uint32_t endian = header.e_ident[EI_DATA];317if (endian == ELFDATA2LSB)318return ArchSpec::eCore_ppc64le_generic;319else320return ArchSpec::eCore_ppc64_generic;321}322323static uint32_t loongarchVariantFromElfFlags(const elf::ELFHeader &header) {324uint32_t fileclass = header.e_ident[EI_CLASS];325switch (fileclass) {326case llvm::ELF::ELFCLASS32:327return ArchSpec::eLoongArchSubType_loongarch32;328case llvm::ELF::ELFCLASS64:329return ArchSpec::eLoongArchSubType_loongarch64;330default:331return ArchSpec::eLoongArchSubType_unknown;332}333}334335static uint32_t subTypeFromElfHeader(const elf::ELFHeader &header) {336if (header.e_machine == llvm::ELF::EM_MIPS)337return mipsVariantFromElfFlags(header);338else if (header.e_machine == llvm::ELF::EM_PPC64)339return ppc64VariantFromElfFlags(header);340else if (header.e_machine == llvm::ELF::EM_RISCV)341return riscvVariantFromElfFlags(header);342else if (header.e_machine == llvm::ELF::EM_LOONGARCH)343return loongarchVariantFromElfFlags(header);344345return LLDB_INVALID_CPUTYPE;346}347348char ObjectFileELF::ID;349350// Arbitrary constant used as UUID prefix for core files.351const uint32_t ObjectFileELF::g_core_uuid_magic(0xE210C);352353// Static methods.354void ObjectFileELF::Initialize() {355PluginManager::RegisterPlugin(GetPluginNameStatic(),356GetPluginDescriptionStatic(), CreateInstance,357CreateMemoryInstance, GetModuleSpecifications);358}359360void ObjectFileELF::Terminate() {361PluginManager::UnregisterPlugin(CreateInstance);362}363364ObjectFile *ObjectFileELF::CreateInstance(const lldb::ModuleSP &module_sp,365DataBufferSP data_sp,366lldb::offset_t data_offset,367const lldb_private::FileSpec *file,368lldb::offset_t file_offset,369lldb::offset_t length) {370bool mapped_writable = false;371if (!data_sp) {372data_sp = MapFileDataWritable(*file, length, file_offset);373if (!data_sp)374return nullptr;375data_offset = 0;376mapped_writable = true;377}378379assert(data_sp);380381if (data_sp->GetByteSize() <= (llvm::ELF::EI_NIDENT + data_offset))382return nullptr;383384const uint8_t *magic = data_sp->GetBytes() + data_offset;385if (!ELFHeader::MagicBytesMatch(magic))386return nullptr;387388// Update the data to contain the entire file if it doesn't already389if (data_sp->GetByteSize() < length) {390data_sp = MapFileDataWritable(*file, length, file_offset);391if (!data_sp)392return nullptr;393data_offset = 0;394mapped_writable = true;395magic = data_sp->GetBytes();396}397398// If we didn't map the data as writable take ownership of the buffer.399if (!mapped_writable) {400data_sp = std::make_shared<DataBufferHeap>(data_sp->GetBytes(),401data_sp->GetByteSize());402data_offset = 0;403magic = data_sp->GetBytes();404}405406unsigned address_size = ELFHeader::AddressSizeInBytes(magic);407if (address_size == 4 || address_size == 8) {408std::unique_ptr<ObjectFileELF> objfile_up(new ObjectFileELF(409module_sp, data_sp, data_offset, file, file_offset, length));410ArchSpec spec = objfile_up->GetArchitecture();411if (spec && objfile_up->SetModulesArchitecture(spec))412return objfile_up.release();413}414415return nullptr;416}417418ObjectFile *ObjectFileELF::CreateMemoryInstance(419const lldb::ModuleSP &module_sp, WritableDataBufferSP data_sp,420const lldb::ProcessSP &process_sp, lldb::addr_t header_addr) {421if (data_sp && data_sp->GetByteSize() > (llvm::ELF::EI_NIDENT)) {422const uint8_t *magic = data_sp->GetBytes();423if (ELFHeader::MagicBytesMatch(magic)) {424unsigned address_size = ELFHeader::AddressSizeInBytes(magic);425if (address_size == 4 || address_size == 8) {426std::unique_ptr<ObjectFileELF> objfile_up(427new ObjectFileELF(module_sp, data_sp, process_sp, header_addr));428ArchSpec spec = objfile_up->GetArchitecture();429if (spec && objfile_up->SetModulesArchitecture(spec))430return objfile_up.release();431}432}433}434return nullptr;435}436437bool ObjectFileELF::MagicBytesMatch(DataBufferSP &data_sp,438lldb::addr_t data_offset,439lldb::addr_t data_length) {440if (data_sp &&441data_sp->GetByteSize() > (llvm::ELF::EI_NIDENT + data_offset)) {442const uint8_t *magic = data_sp->GetBytes() + data_offset;443return ELFHeader::MagicBytesMatch(magic);444}445return false;446}447448static uint32_t calc_crc32(uint32_t init, const DataExtractor &data) {449return llvm::crc32(init,450llvm::ArrayRef(data.GetDataStart(), data.GetByteSize()));451}452453uint32_t ObjectFileELF::CalculateELFNotesSegmentsCRC32(454const ProgramHeaderColl &program_headers, DataExtractor &object_data) {455456uint32_t core_notes_crc = 0;457458for (const ELFProgramHeader &H : program_headers) {459if (H.p_type == llvm::ELF::PT_NOTE) {460const elf_off ph_offset = H.p_offset;461const size_t ph_size = H.p_filesz;462463DataExtractor segment_data;464if (segment_data.SetData(object_data, ph_offset, ph_size) != ph_size) {465// The ELF program header contained incorrect data, probably corefile466// is incomplete or corrupted.467break;468}469470core_notes_crc = calc_crc32(core_notes_crc, segment_data);471}472}473474return core_notes_crc;475}476477static const char *OSABIAsCString(unsigned char osabi_byte) {478#define _MAKE_OSABI_CASE(x) \479case x: \480return #x481switch (osabi_byte) {482_MAKE_OSABI_CASE(ELFOSABI_NONE);483_MAKE_OSABI_CASE(ELFOSABI_HPUX);484_MAKE_OSABI_CASE(ELFOSABI_NETBSD);485_MAKE_OSABI_CASE(ELFOSABI_GNU);486_MAKE_OSABI_CASE(ELFOSABI_HURD);487_MAKE_OSABI_CASE(ELFOSABI_SOLARIS);488_MAKE_OSABI_CASE(ELFOSABI_AIX);489_MAKE_OSABI_CASE(ELFOSABI_IRIX);490_MAKE_OSABI_CASE(ELFOSABI_FREEBSD);491_MAKE_OSABI_CASE(ELFOSABI_TRU64);492_MAKE_OSABI_CASE(ELFOSABI_MODESTO);493_MAKE_OSABI_CASE(ELFOSABI_OPENBSD);494_MAKE_OSABI_CASE(ELFOSABI_OPENVMS);495_MAKE_OSABI_CASE(ELFOSABI_NSK);496_MAKE_OSABI_CASE(ELFOSABI_AROS);497_MAKE_OSABI_CASE(ELFOSABI_FENIXOS);498_MAKE_OSABI_CASE(ELFOSABI_C6000_ELFABI);499_MAKE_OSABI_CASE(ELFOSABI_C6000_LINUX);500_MAKE_OSABI_CASE(ELFOSABI_ARM);501_MAKE_OSABI_CASE(ELFOSABI_STANDALONE);502default:503return "<unknown-osabi>";504}505#undef _MAKE_OSABI_CASE506}507508//509// WARNING : This function is being deprecated510// It's functionality has moved to ArchSpec::SetArchitecture This function is511// only being kept to validate the move.512//513// TODO : Remove this function514static bool GetOsFromOSABI(unsigned char osabi_byte,515llvm::Triple::OSType &ostype) {516switch (osabi_byte) {517case ELFOSABI_AIX:518ostype = llvm::Triple::OSType::AIX;519break;520case ELFOSABI_FREEBSD:521ostype = llvm::Triple::OSType::FreeBSD;522break;523case ELFOSABI_GNU:524ostype = llvm::Triple::OSType::Linux;525break;526case ELFOSABI_NETBSD:527ostype = llvm::Triple::OSType::NetBSD;528break;529case ELFOSABI_OPENBSD:530ostype = llvm::Triple::OSType::OpenBSD;531break;532case ELFOSABI_SOLARIS:533ostype = llvm::Triple::OSType::Solaris;534break;535default:536ostype = llvm::Triple::OSType::UnknownOS;537}538return ostype != llvm::Triple::OSType::UnknownOS;539}540541size_t ObjectFileELF::GetModuleSpecifications(542const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp,543lldb::offset_t data_offset, lldb::offset_t file_offset,544lldb::offset_t length, lldb_private::ModuleSpecList &specs) {545Log *log = GetLog(LLDBLog::Modules);546547const size_t initial_count = specs.GetSize();548549if (ObjectFileELF::MagicBytesMatch(data_sp, 0, data_sp->GetByteSize())) {550DataExtractor data;551data.SetData(data_sp);552elf::ELFHeader header;553lldb::offset_t header_offset = data_offset;554if (header.Parse(data, &header_offset)) {555if (data_sp) {556ModuleSpec spec(file);557// In Android API level 23 and above, bionic dynamic linker is able to558// load .so file directly from zip file. In that case, .so file is559// page aligned and uncompressed, and this module spec should retain the560// .so file offset and file size to pass through the information from561// lldb-server to LLDB. For normal file, file_offset should be 0,562// length should be the size of the file.563spec.SetObjectOffset(file_offset);564spec.SetObjectSize(length);565566const uint32_t sub_type = subTypeFromElfHeader(header);567spec.GetArchitecture().SetArchitecture(568eArchTypeELF, header.e_machine, sub_type, header.e_ident[EI_OSABI]);569570if (spec.GetArchitecture().IsValid()) {571llvm::Triple::OSType ostype;572llvm::Triple::VendorType vendor;573llvm::Triple::OSType spec_ostype =574spec.GetArchitecture().GetTriple().getOS();575576LLDB_LOGF(log, "ObjectFileELF::%s file '%s' module OSABI: %s",577__FUNCTION__, file.GetPath().c_str(),578OSABIAsCString(header.e_ident[EI_OSABI]));579580// SetArchitecture should have set the vendor to unknown581vendor = spec.GetArchitecture().GetTriple().getVendor();582assert(vendor == llvm::Triple::UnknownVendor);583UNUSED_IF_ASSERT_DISABLED(vendor);584585//586// Validate it is ok to remove GetOsFromOSABI587GetOsFromOSABI(header.e_ident[EI_OSABI], ostype);588assert(spec_ostype == ostype);589if (spec_ostype != llvm::Triple::OSType::UnknownOS) {590LLDB_LOGF(log,591"ObjectFileELF::%s file '%s' set ELF module OS type "592"from ELF header OSABI.",593__FUNCTION__, file.GetPath().c_str());594}595596// When ELF file does not contain GNU build ID, the later code will597// calculate CRC32 with this data_sp file_offset and length. It is598// important for Android zip .so file, which is a slice of a file,599// to not access the outside of the file slice range.600if (data_sp->GetByteSize() < length)601data_sp = MapFileData(file, length, file_offset);602if (data_sp)603data.SetData(data_sp);604// In case there is header extension in the section #0, the header we605// parsed above could have sentinel values for e_phnum, e_shnum, and606// e_shstrndx. In this case we need to reparse the header with a607// bigger data source to get the actual values.608if (header.HasHeaderExtension()) {609lldb::offset_t header_offset = data_offset;610header.Parse(data, &header_offset);611}612613uint32_t gnu_debuglink_crc = 0;614std::string gnu_debuglink_file;615SectionHeaderColl section_headers;616lldb_private::UUID &uuid = spec.GetUUID();617618GetSectionHeaderInfo(section_headers, data, header, uuid,619gnu_debuglink_file, gnu_debuglink_crc,620spec.GetArchitecture());621622llvm::Triple &spec_triple = spec.GetArchitecture().GetTriple();623624LLDB_LOGF(log,625"ObjectFileELF::%s file '%s' module set to triple: %s "626"(architecture %s)",627__FUNCTION__, file.GetPath().c_str(),628spec_triple.getTriple().c_str(),629spec.GetArchitecture().GetArchitectureName());630631if (!uuid.IsValid()) {632uint32_t core_notes_crc = 0;633634if (!gnu_debuglink_crc) {635LLDB_SCOPED_TIMERF(636"Calculating module crc32 %s with size %" PRIu64 " KiB",637file.GetFilename().AsCString(),638(length - file_offset) / 1024);639640// For core files - which usually don't happen to have a641// gnu_debuglink, and are pretty bulky - calculating whole642// contents crc32 would be too much of luxury. Thus we will need643// to fallback to something simpler.644if (header.e_type == llvm::ELF::ET_CORE) {645ProgramHeaderColl program_headers;646GetProgramHeaderInfo(program_headers, data, header);647648core_notes_crc =649CalculateELFNotesSegmentsCRC32(program_headers, data);650} else {651gnu_debuglink_crc = calc_crc32(0, data);652}653}654using u32le = llvm::support::ulittle32_t;655if (gnu_debuglink_crc) {656// Use 4 bytes of crc from the .gnu_debuglink section.657u32le data(gnu_debuglink_crc);658uuid = UUID(&data, sizeof(data));659} else if (core_notes_crc) {660// Use 8 bytes - first 4 bytes for *magic* prefix, mainly to make661// it look different form .gnu_debuglink crc followed by 4 bytes662// of note segments crc.663u32le data[] = {u32le(g_core_uuid_magic), u32le(core_notes_crc)};664uuid = UUID(data, sizeof(data));665}666}667668specs.Append(spec);669}670}671}672}673674return specs.GetSize() - initial_count;675}676677// ObjectFile protocol678679ObjectFileELF::ObjectFileELF(const lldb::ModuleSP &module_sp,680DataBufferSP data_sp, lldb::offset_t data_offset,681const FileSpec *file, lldb::offset_t file_offset,682lldb::offset_t length)683: ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset) {684if (file)685m_file = *file;686}687688ObjectFileELF::ObjectFileELF(const lldb::ModuleSP &module_sp,689DataBufferSP header_data_sp,690const lldb::ProcessSP &process_sp,691addr_t header_addr)692: ObjectFile(module_sp, process_sp, header_addr, header_data_sp) {}693694bool ObjectFileELF::IsExecutable() const {695return ((m_header.e_type & ET_EXEC) != 0) || (m_header.e_entry != 0);696}697698bool ObjectFileELF::SetLoadAddress(Target &target, lldb::addr_t value,699bool value_is_offset) {700ModuleSP module_sp = GetModule();701if (module_sp) {702size_t num_loaded_sections = 0;703SectionList *section_list = GetSectionList();704if (section_list) {705if (!value_is_offset) {706addr_t base = GetBaseAddress().GetFileAddress();707if (base == LLDB_INVALID_ADDRESS)708return false;709value -= base;710}711712const size_t num_sections = section_list->GetSize();713size_t sect_idx = 0;714715for (sect_idx = 0; sect_idx < num_sections; ++sect_idx) {716// Iterate through the object file sections to find all of the sections717// that have SHF_ALLOC in their flag bits.718SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx));719720// PT_TLS segments can have the same p_vaddr and p_paddr as other721// PT_LOAD segments so we shouldn't load them. If we do load them, then722// the SectionLoadList will incorrectly fill in the instance variable723// SectionLoadList::m_addr_to_sect with the same address as a PT_LOAD724// segment and we won't be able to resolve addresses in the PT_LOAD725// segment whose p_vaddr entry matches that of the PT_TLS. Any variables726// that appear in the PT_TLS segments get resolved by the DWARF727// expressions. If this ever changes we will need to fix all object728// file plug-ins, but until then, we don't want PT_TLS segments to729// remove the entry from SectionLoadList::m_addr_to_sect when we call730// SetSectionLoadAddress() below.731if (section_sp->IsThreadSpecific())732continue;733if (section_sp->Test(SHF_ALLOC) ||734section_sp->GetType() == eSectionTypeContainer) {735lldb::addr_t load_addr = section_sp->GetFileAddress();736// We don't want to update the load address of a section with type737// eSectionTypeAbsoluteAddress as they already have the absolute load738// address already specified739if (section_sp->GetType() != eSectionTypeAbsoluteAddress)740load_addr += value;741742// On 32-bit systems the load address have to fit into 4 bytes. The743// rest of the bytes are the overflow from the addition.744if (GetAddressByteSize() == 4)745load_addr &= 0xFFFFFFFF;746747if (target.GetSectionLoadList().SetSectionLoadAddress(section_sp,748load_addr))749++num_loaded_sections;750}751}752return num_loaded_sections > 0;753}754}755return false;756}757758ByteOrder ObjectFileELF::GetByteOrder() const {759if (m_header.e_ident[EI_DATA] == ELFDATA2MSB)760return eByteOrderBig;761if (m_header.e_ident[EI_DATA] == ELFDATA2LSB)762return eByteOrderLittle;763return eByteOrderInvalid;764}765766uint32_t ObjectFileELF::GetAddressByteSize() const {767return m_data.GetAddressByteSize();768}769770AddressClass ObjectFileELF::GetAddressClass(addr_t file_addr) {771Symtab *symtab = GetSymtab();772if (!symtab)773return AddressClass::eUnknown;774775// The address class is determined based on the symtab. Ask it from the776// object file what contains the symtab information.777ObjectFile *symtab_objfile = symtab->GetObjectFile();778if (symtab_objfile != nullptr && symtab_objfile != this)779return symtab_objfile->GetAddressClass(file_addr);780781auto res = ObjectFile::GetAddressClass(file_addr);782if (res != AddressClass::eCode)783return res;784785auto ub = m_address_class_map.upper_bound(file_addr);786if (ub == m_address_class_map.begin()) {787// No entry in the address class map before the address. Return default788// address class for an address in a code section.789return AddressClass::eCode;790}791792// Move iterator to the address class entry preceding address793--ub;794795return ub->second;796}797798size_t ObjectFileELF::SectionIndex(const SectionHeaderCollIter &I) {799return std::distance(m_section_headers.begin(), I);800}801802size_t ObjectFileELF::SectionIndex(const SectionHeaderCollConstIter &I) const {803return std::distance(m_section_headers.begin(), I);804}805806bool ObjectFileELF::ParseHeader() {807lldb::offset_t offset = 0;808return m_header.Parse(m_data, &offset);809}810811UUID ObjectFileELF::GetUUID() {812// Need to parse the section list to get the UUIDs, so make sure that's been813// done.814if (!ParseSectionHeaders() && GetType() != ObjectFile::eTypeCoreFile)815return UUID();816817if (!m_uuid) {818using u32le = llvm::support::ulittle32_t;819if (GetType() == ObjectFile::eTypeCoreFile) {820uint32_t core_notes_crc = 0;821822if (!ParseProgramHeaders())823return UUID();824825core_notes_crc =826CalculateELFNotesSegmentsCRC32(m_program_headers, m_data);827828if (core_notes_crc) {829// Use 8 bytes - first 4 bytes for *magic* prefix, mainly to make it830// look different form .gnu_debuglink crc - followed by 4 bytes of note831// segments crc.832u32le data[] = {u32le(g_core_uuid_magic), u32le(core_notes_crc)};833m_uuid = UUID(data, sizeof(data));834}835} else {836if (!m_gnu_debuglink_crc)837m_gnu_debuglink_crc = calc_crc32(0, m_data);838if (m_gnu_debuglink_crc) {839// Use 4 bytes of crc from the .gnu_debuglink section.840u32le data(m_gnu_debuglink_crc);841m_uuid = UUID(&data, sizeof(data));842}843}844}845846return m_uuid;847}848849std::optional<FileSpec> ObjectFileELF::GetDebugLink() {850if (m_gnu_debuglink_file.empty())851return std::nullopt;852return FileSpec(m_gnu_debuglink_file);853}854855uint32_t ObjectFileELF::GetDependentModules(FileSpecList &files) {856size_t num_modules = ParseDependentModules();857uint32_t num_specs = 0;858859for (unsigned i = 0; i < num_modules; ++i) {860if (files.AppendIfUnique(m_filespec_up->GetFileSpecAtIndex(i)))861num_specs++;862}863864return num_specs;865}866867Address ObjectFileELF::GetImageInfoAddress(Target *target) {868if (!ParseDynamicSymbols())869return Address();870871SectionList *section_list = GetSectionList();872if (!section_list)873return Address();874875// Find the SHT_DYNAMIC (.dynamic) section.876SectionSP dynsym_section_sp(877section_list->FindSectionByType(eSectionTypeELFDynamicLinkInfo, true));878if (!dynsym_section_sp)879return Address();880assert(dynsym_section_sp->GetObjectFile() == this);881882user_id_t dynsym_id = dynsym_section_sp->GetID();883const ELFSectionHeaderInfo *dynsym_hdr = GetSectionHeaderByIndex(dynsym_id);884if (!dynsym_hdr)885return Address();886887for (size_t i = 0; i < m_dynamic_symbols.size(); ++i) {888ELFDynamic &symbol = m_dynamic_symbols[i];889890if (symbol.d_tag == DT_DEBUG) {891// Compute the offset as the number of previous entries plus the size of892// d_tag.893addr_t offset = i * dynsym_hdr->sh_entsize + GetAddressByteSize();894return Address(dynsym_section_sp, offset);895}896// MIPS executables uses DT_MIPS_RLD_MAP_REL to support PIE. DT_MIPS_RLD_MAP897// exists in non-PIE.898else if ((symbol.d_tag == DT_MIPS_RLD_MAP ||899symbol.d_tag == DT_MIPS_RLD_MAP_REL) &&900target) {901addr_t offset = i * dynsym_hdr->sh_entsize + GetAddressByteSize();902addr_t dyn_base = dynsym_section_sp->GetLoadBaseAddress(target);903if (dyn_base == LLDB_INVALID_ADDRESS)904return Address();905906Status error;907if (symbol.d_tag == DT_MIPS_RLD_MAP) {908// DT_MIPS_RLD_MAP tag stores an absolute address of the debug pointer.909Address addr;910if (target->ReadPointerFromMemory(dyn_base + offset, error, addr, true))911return addr;912}913if (symbol.d_tag == DT_MIPS_RLD_MAP_REL) {914// DT_MIPS_RLD_MAP_REL tag stores the offset to the debug pointer,915// relative to the address of the tag.916uint64_t rel_offset;917rel_offset = target->ReadUnsignedIntegerFromMemory(918dyn_base + offset, GetAddressByteSize(), UINT64_MAX, error, true);919if (error.Success() && rel_offset != UINT64_MAX) {920Address addr;921addr_t debug_ptr_address =922dyn_base + (offset - GetAddressByteSize()) + rel_offset;923addr.SetOffset(debug_ptr_address);924return addr;925}926}927}928}929930return Address();931}932933lldb_private::Address ObjectFileELF::GetEntryPointAddress() {934if (m_entry_point_address.IsValid())935return m_entry_point_address;936937if (!ParseHeader() || !IsExecutable())938return m_entry_point_address;939940SectionList *section_list = GetSectionList();941addr_t offset = m_header.e_entry;942943if (!section_list)944m_entry_point_address.SetOffset(offset);945else946m_entry_point_address.ResolveAddressUsingFileSections(offset, section_list);947return m_entry_point_address;948}949950Address ObjectFileELF::GetBaseAddress() {951if (GetType() == ObjectFile::eTypeObjectFile) {952for (SectionHeaderCollIter I = std::next(m_section_headers.begin());953I != m_section_headers.end(); ++I) {954const ELFSectionHeaderInfo &header = *I;955if (header.sh_flags & SHF_ALLOC)956return Address(GetSectionList()->FindSectionByID(SectionIndex(I)), 0);957}958return LLDB_INVALID_ADDRESS;959}960961for (const auto &EnumPHdr : llvm::enumerate(ProgramHeaders())) {962const ELFProgramHeader &H = EnumPHdr.value();963if (H.p_type != PT_LOAD)964continue;965966return Address(967GetSectionList()->FindSectionByID(SegmentID(EnumPHdr.index())), 0);968}969return LLDB_INVALID_ADDRESS;970}971972// ParseDependentModules973size_t ObjectFileELF::ParseDependentModules() {974if (m_filespec_up)975return m_filespec_up->GetSize();976977m_filespec_up = std::make_unique<FileSpecList>();978979if (!ParseSectionHeaders())980return 0;981982SectionList *section_list = GetSectionList();983if (!section_list)984return 0;985986// Find the SHT_DYNAMIC section.987Section *dynsym =988section_list->FindSectionByType(eSectionTypeELFDynamicLinkInfo, true)989.get();990if (!dynsym)991return 0;992assert(dynsym->GetObjectFile() == this);993994const ELFSectionHeaderInfo *header = GetSectionHeaderByIndex(dynsym->GetID());995if (!header)996return 0;997// sh_link: section header index of string table used by entries in the998// section.999Section *dynstr = section_list->FindSectionByID(header->sh_link).get();1000if (!dynstr)1001return 0;10021003DataExtractor dynsym_data;1004DataExtractor dynstr_data;1005if (ReadSectionData(dynsym, dynsym_data) &&1006ReadSectionData(dynstr, dynstr_data)) {1007ELFDynamic symbol;1008const lldb::offset_t section_size = dynsym_data.GetByteSize();1009lldb::offset_t offset = 0;10101011// The only type of entries we are concerned with are tagged DT_NEEDED,1012// yielding the name of a required library.1013while (offset < section_size) {1014if (!symbol.Parse(dynsym_data, &offset))1015break;10161017if (symbol.d_tag != DT_NEEDED)1018continue;10191020uint32_t str_index = static_cast<uint32_t>(symbol.d_val);1021const char *lib_name = dynstr_data.PeekCStr(str_index);1022FileSpec file_spec(lib_name);1023FileSystem::Instance().Resolve(file_spec);1024m_filespec_up->Append(file_spec);1025}1026}10271028return m_filespec_up->GetSize();1029}10301031// GetProgramHeaderInfo1032size_t ObjectFileELF::GetProgramHeaderInfo(ProgramHeaderColl &program_headers,1033DataExtractor &object_data,1034const ELFHeader &header) {1035// We have already parsed the program headers1036if (!program_headers.empty())1037return program_headers.size();10381039// If there are no program headers to read we are done.1040if (header.e_phnum == 0)1041return 0;10421043program_headers.resize(header.e_phnum);1044if (program_headers.size() != header.e_phnum)1045return 0;10461047const size_t ph_size = header.e_phnum * header.e_phentsize;1048const elf_off ph_offset = header.e_phoff;1049DataExtractor data;1050if (data.SetData(object_data, ph_offset, ph_size) != ph_size)1051return 0;10521053uint32_t idx;1054lldb::offset_t offset;1055for (idx = 0, offset = 0; idx < header.e_phnum; ++idx) {1056if (!program_headers[idx].Parse(data, &offset))1057break;1058}10591060if (idx < program_headers.size())1061program_headers.resize(idx);10621063return program_headers.size();1064}10651066// ParseProgramHeaders1067bool ObjectFileELF::ParseProgramHeaders() {1068return GetProgramHeaderInfo(m_program_headers, m_data, m_header) != 0;1069}10701071lldb_private::Status1072ObjectFileELF::RefineModuleDetailsFromNote(lldb_private::DataExtractor &data,1073lldb_private::ArchSpec &arch_spec,1074lldb_private::UUID &uuid) {1075Log *log = GetLog(LLDBLog::Modules);1076Status error;10771078lldb::offset_t offset = 0;10791080while (true) {1081// Parse the note header. If this fails, bail out.1082const lldb::offset_t note_offset = offset;1083ELFNote note = ELFNote();1084if (!note.Parse(data, &offset)) {1085// We're done.1086return error;1087}10881089LLDB_LOGF(log, "ObjectFileELF::%s parsing note name='%s', type=%" PRIu32,1090__FUNCTION__, note.n_name.c_str(), note.n_type);10911092// Process FreeBSD ELF notes.1093if ((note.n_name == LLDB_NT_OWNER_FREEBSD) &&1094(note.n_type == LLDB_NT_FREEBSD_ABI_TAG) &&1095(note.n_descsz == LLDB_NT_FREEBSD_ABI_SIZE)) {1096// Pull out the min version info.1097uint32_t version_info;1098if (data.GetU32(&offset, &version_info, 1) == nullptr) {1099error.SetErrorString("failed to read FreeBSD ABI note payload");1100return error;1101}11021103// Convert the version info into a major/minor number.1104const uint32_t version_major = version_info / 100000;1105const uint32_t version_minor = (version_info / 1000) % 100;11061107char os_name[32];1108snprintf(os_name, sizeof(os_name), "freebsd%" PRIu32 ".%" PRIu32,1109version_major, version_minor);11101111// Set the elf OS version to FreeBSD. Also clear the vendor.1112arch_spec.GetTriple().setOSName(os_name);1113arch_spec.GetTriple().setVendor(llvm::Triple::VendorType::UnknownVendor);11141115LLDB_LOGF(log,1116"ObjectFileELF::%s detected FreeBSD %" PRIu32 ".%" PRIu321117".%" PRIu32,1118__FUNCTION__, version_major, version_minor,1119static_cast<uint32_t>(version_info % 1000));1120}1121// Process GNU ELF notes.1122else if (note.n_name == LLDB_NT_OWNER_GNU) {1123switch (note.n_type) {1124case LLDB_NT_GNU_ABI_TAG:1125if (note.n_descsz == LLDB_NT_GNU_ABI_SIZE) {1126// Pull out the min OS version supporting the ABI.1127uint32_t version_info[4];1128if (data.GetU32(&offset, &version_info[0], note.n_descsz / 4) ==1129nullptr) {1130error.SetErrorString("failed to read GNU ABI note payload");1131return error;1132}11331134// Set the OS per the OS field.1135switch (version_info[0]) {1136case LLDB_NT_GNU_ABI_OS_LINUX:1137arch_spec.GetTriple().setOS(llvm::Triple::OSType::Linux);1138arch_spec.GetTriple().setVendor(1139llvm::Triple::VendorType::UnknownVendor);1140LLDB_LOGF(log,1141"ObjectFileELF::%s detected Linux, min version %" PRIu321142".%" PRIu32 ".%" PRIu32,1143__FUNCTION__, version_info[1], version_info[2],1144version_info[3]);1145// FIXME we have the minimal version number, we could be propagating1146// that. version_info[1] = OS Major, version_info[2] = OS Minor,1147// version_info[3] = Revision.1148break;1149case LLDB_NT_GNU_ABI_OS_HURD:1150arch_spec.GetTriple().setOS(llvm::Triple::OSType::UnknownOS);1151arch_spec.GetTriple().setVendor(1152llvm::Triple::VendorType::UnknownVendor);1153LLDB_LOGF(log,1154"ObjectFileELF::%s detected Hurd (unsupported), min "1155"version %" PRIu32 ".%" PRIu32 ".%" PRIu32,1156__FUNCTION__, version_info[1], version_info[2],1157version_info[3]);1158break;1159case LLDB_NT_GNU_ABI_OS_SOLARIS:1160arch_spec.GetTriple().setOS(llvm::Triple::OSType::Solaris);1161arch_spec.GetTriple().setVendor(1162llvm::Triple::VendorType::UnknownVendor);1163LLDB_LOGF(log,1164"ObjectFileELF::%s detected Solaris, min version %" PRIu321165".%" PRIu32 ".%" PRIu32,1166__FUNCTION__, version_info[1], version_info[2],1167version_info[3]);1168break;1169default:1170LLDB_LOGF(log,1171"ObjectFileELF::%s unrecognized OS in note, id %" PRIu321172", min version %" PRIu32 ".%" PRIu32 ".%" PRIu32,1173__FUNCTION__, version_info[0], version_info[1],1174version_info[2], version_info[3]);1175break;1176}1177}1178break;11791180case LLDB_NT_GNU_BUILD_ID_TAG:1181// Only bother processing this if we don't already have the uuid set.1182if (!uuid.IsValid()) {1183// 16 bytes is UUID|MD5, 20 bytes is SHA1. Other linkers may produce a1184// build-id of a different length. Accept it as long as it's at least1185// 4 bytes as it will be better than our own crc32.1186if (note.n_descsz >= 4) {1187if (const uint8_t *buf = data.PeekData(offset, note.n_descsz)) {1188// Save the build id as the UUID for the module.1189uuid = UUID(buf, note.n_descsz);1190} else {1191error.SetErrorString("failed to read GNU_BUILD_ID note payload");1192return error;1193}1194}1195}1196break;1197}1198if (arch_spec.IsMIPS() &&1199arch_spec.GetTriple().getOS() == llvm::Triple::OSType::UnknownOS)1200// The note.n_name == LLDB_NT_OWNER_GNU is valid for Linux platform1201arch_spec.GetTriple().setOS(llvm::Triple::OSType::Linux);1202}1203// Process NetBSD ELF executables and shared libraries1204else if ((note.n_name == LLDB_NT_OWNER_NETBSD) &&1205(note.n_type == LLDB_NT_NETBSD_IDENT_TAG) &&1206(note.n_descsz == LLDB_NT_NETBSD_IDENT_DESCSZ) &&1207(note.n_namesz == LLDB_NT_NETBSD_IDENT_NAMESZ)) {1208// Pull out the version info.1209uint32_t version_info;1210if (data.GetU32(&offset, &version_info, 1) == nullptr) {1211error.SetErrorString("failed to read NetBSD ABI note payload");1212return error;1213}1214// Convert the version info into a major/minor/patch number.1215// #define __NetBSD_Version__ MMmmrrpp001216//1217// M = major version1218// m = minor version; a minor number of 99 indicates current.1219// r = 0 (since NetBSD 3.0 not used)1220// p = patchlevel1221const uint32_t version_major = version_info / 100000000;1222const uint32_t version_minor = (version_info % 100000000) / 1000000;1223const uint32_t version_patch = (version_info % 10000) / 100;1224// Set the elf OS version to NetBSD. Also clear the vendor.1225arch_spec.GetTriple().setOSName(1226llvm::formatv("netbsd{0}.{1}.{2}", version_major, version_minor,1227version_patch).str());1228arch_spec.GetTriple().setVendor(llvm::Triple::VendorType::UnknownVendor);1229}1230// Process NetBSD ELF core(5) notes1231else if ((note.n_name == LLDB_NT_OWNER_NETBSDCORE) &&1232(note.n_type == LLDB_NT_NETBSD_PROCINFO)) {1233// Set the elf OS version to NetBSD. Also clear the vendor.1234arch_spec.GetTriple().setOS(llvm::Triple::OSType::NetBSD);1235arch_spec.GetTriple().setVendor(llvm::Triple::VendorType::UnknownVendor);1236}1237// Process OpenBSD ELF notes.1238else if (note.n_name == LLDB_NT_OWNER_OPENBSD) {1239// Set the elf OS version to OpenBSD. Also clear the vendor.1240arch_spec.GetTriple().setOS(llvm::Triple::OSType::OpenBSD);1241arch_spec.GetTriple().setVendor(llvm::Triple::VendorType::UnknownVendor);1242} else if (note.n_name == LLDB_NT_OWNER_ANDROID) {1243arch_spec.GetTriple().setOS(llvm::Triple::OSType::Linux);1244arch_spec.GetTriple().setEnvironment(1245llvm::Triple::EnvironmentType::Android);1246} else if (note.n_name == LLDB_NT_OWNER_LINUX) {1247// This is sometimes found in core files and usually contains extended1248// register info1249arch_spec.GetTriple().setOS(llvm::Triple::OSType::Linux);1250} else if (note.n_name == LLDB_NT_OWNER_CORE) {1251// Parse the NT_FILE to look for stuff in paths to shared libraries1252// The contents look like this in a 64 bit ELF core file:1253//1254// count = 0x000000000000000a (10)1255// page_size = 0x0000000000001000 (4096)1256// Index start end file_ofs path1257// ===== ------------------ ------------------ ------------------ -------------------------------------1258// [ 0] 0x0000000000401000 0x0000000000000000 /tmp/a.out1259// [ 1] 0x0000000000600000 0x0000000000601000 0x0000000000000000 /tmp/a.out1260// [ 2] 0x0000000000601000 0x0000000000602000 0x0000000000000001 /tmp/a.out1261// [ 3] 0x00007fa79c9ed000 0x00007fa79cba8000 0x0000000000000000 /lib/x86_64-linux-gnu/libc-2.19.so1262// [ 4] 0x00007fa79cba8000 0x00007fa79cda7000 0x00000000000001bb /lib/x86_64-linux-gnu/libc-2.19.so1263// [ 5] 0x00007fa79cda7000 0x00007fa79cdab000 0x00000000000001ba /lib/x86_64-linux-gnu/libc-2.19.so1264// [ 6] 0x00007fa79cdab000 0x00007fa79cdad000 0x00000000000001be /lib/x86_64-linux-gnu/libc-2.19.so1265// [ 7] 0x00007fa79cdb2000 0x00007fa79cdd5000 0x0000000000000000 /lib/x86_64-linux-gnu/ld-2.19.so1266// [ 8] 0x00007fa79cfd4000 0x00007fa79cfd5000 0x0000000000000022 /lib/x86_64-linux-gnu/ld-2.19.so1267// [ 9] 0x00007fa79cfd5000 0x00007fa79cfd6000 0x0000000000000023 /lib/x86_64-linux-gnu/ld-2.19.so1268//1269// In the 32 bit ELFs the count, page_size, start, end, file_ofs are1270// uint32_t.1271//1272// For reference: see readelf source code (in binutils).1273if (note.n_type == NT_FILE) {1274uint64_t count = data.GetAddress(&offset);1275const char *cstr;1276data.GetAddress(&offset); // Skip page size1277offset += count * 3 *1278data.GetAddressByteSize(); // Skip all start/end/file_ofs1279for (size_t i = 0; i < count; ++i) {1280cstr = data.GetCStr(&offset);1281if (cstr == nullptr) {1282error.SetErrorStringWithFormat("ObjectFileELF::%s trying to read "1283"at an offset after the end "1284"(GetCStr returned nullptr)",1285__FUNCTION__);1286return error;1287}1288llvm::StringRef path(cstr);1289if (path.contains("/lib/x86_64-linux-gnu") || path.contains("/lib/i386-linux-gnu")) {1290arch_spec.GetTriple().setOS(llvm::Triple::OSType::Linux);1291break;1292}1293}1294if (arch_spec.IsMIPS() &&1295arch_spec.GetTriple().getOS() == llvm::Triple::OSType::UnknownOS)1296// In case of MIPSR6, the LLDB_NT_OWNER_GNU note is missing for some1297// cases (e.g. compile with -nostdlib) Hence set OS to Linux1298arch_spec.GetTriple().setOS(llvm::Triple::OSType::Linux);1299}1300}13011302// Calculate the offset of the next note just in case "offset" has been1303// used to poke at the contents of the note data1304offset = note_offset + note.GetByteSize();1305}13061307return error;1308}13091310void ObjectFileELF::ParseARMAttributes(DataExtractor &data, uint64_t length,1311ArchSpec &arch_spec) {1312lldb::offset_t Offset = 0;13131314uint8_t FormatVersion = data.GetU8(&Offset);1315if (FormatVersion != llvm::ELFAttrs::Format_Version)1316return;13171318Offset = Offset + sizeof(uint32_t); // Section Length1319llvm::StringRef VendorName = data.GetCStr(&Offset);13201321if (VendorName != "aeabi")1322return;13231324if (arch_spec.GetTriple().getEnvironment() ==1325llvm::Triple::UnknownEnvironment)1326arch_spec.GetTriple().setEnvironment(llvm::Triple::EABI);13271328while (Offset < length) {1329uint8_t Tag = data.GetU8(&Offset);1330uint32_t Size = data.GetU32(&Offset);13311332if (Tag != llvm::ARMBuildAttrs::File || Size == 0)1333continue;13341335while (Offset < length) {1336uint64_t Tag = data.GetULEB128(&Offset);1337switch (Tag) {1338default:1339if (Tag < 32)1340data.GetULEB128(&Offset);1341else if (Tag % 2 == 0)1342data.GetULEB128(&Offset);1343else1344data.GetCStr(&Offset);13451346break;13471348case llvm::ARMBuildAttrs::CPU_raw_name:1349case llvm::ARMBuildAttrs::CPU_name:1350data.GetCStr(&Offset);13511352break;13531354case llvm::ARMBuildAttrs::ABI_VFP_args: {1355uint64_t VFPArgs = data.GetULEB128(&Offset);13561357if (VFPArgs == llvm::ARMBuildAttrs::BaseAAPCS) {1358if (arch_spec.GetTriple().getEnvironment() ==1359llvm::Triple::UnknownEnvironment ||1360arch_spec.GetTriple().getEnvironment() == llvm::Triple::EABIHF)1361arch_spec.GetTriple().setEnvironment(llvm::Triple::EABI);13621363arch_spec.SetFlags(ArchSpec::eARM_abi_soft_float);1364} else if (VFPArgs == llvm::ARMBuildAttrs::HardFPAAPCS) {1365if (arch_spec.GetTriple().getEnvironment() ==1366llvm::Triple::UnknownEnvironment ||1367arch_spec.GetTriple().getEnvironment() == llvm::Triple::EABI)1368arch_spec.GetTriple().setEnvironment(llvm::Triple::EABIHF);13691370arch_spec.SetFlags(ArchSpec::eARM_abi_hard_float);1371}13721373break;1374}1375}1376}1377}1378}13791380// GetSectionHeaderInfo1381size_t ObjectFileELF::GetSectionHeaderInfo(SectionHeaderColl §ion_headers,1382DataExtractor &object_data,1383const elf::ELFHeader &header,1384lldb_private::UUID &uuid,1385std::string &gnu_debuglink_file,1386uint32_t &gnu_debuglink_crc,1387ArchSpec &arch_spec) {1388// Don't reparse the section headers if we already did that.1389if (!section_headers.empty())1390return section_headers.size();13911392// Only initialize the arch_spec to okay defaults if they're not already set.1393// We'll refine this with note data as we parse the notes.1394if (arch_spec.GetTriple().getOS() == llvm::Triple::OSType::UnknownOS) {1395llvm::Triple::OSType ostype;1396llvm::Triple::OSType spec_ostype;1397const uint32_t sub_type = subTypeFromElfHeader(header);1398arch_spec.SetArchitecture(eArchTypeELF, header.e_machine, sub_type,1399header.e_ident[EI_OSABI]);14001401// Validate if it is ok to remove GetOsFromOSABI. Note, that now the OS is1402// determined based on EI_OSABI flag and the info extracted from ELF notes1403// (see RefineModuleDetailsFromNote). However in some cases that still1404// might be not enough: for example a shared library might not have any1405// notes at all and have EI_OSABI flag set to System V, as result the OS1406// will be set to UnknownOS.1407GetOsFromOSABI(header.e_ident[EI_OSABI], ostype);1408spec_ostype = arch_spec.GetTriple().getOS();1409assert(spec_ostype == ostype);1410UNUSED_IF_ASSERT_DISABLED(spec_ostype);1411}14121413if (arch_spec.GetMachine() == llvm::Triple::mips ||1414arch_spec.GetMachine() == llvm::Triple::mipsel ||1415arch_spec.GetMachine() == llvm::Triple::mips64 ||1416arch_spec.GetMachine() == llvm::Triple::mips64el) {1417switch (header.e_flags & llvm::ELF::EF_MIPS_ARCH_ASE) {1418case llvm::ELF::EF_MIPS_MICROMIPS:1419arch_spec.SetFlags(ArchSpec::eMIPSAse_micromips);1420break;1421case llvm::ELF::EF_MIPS_ARCH_ASE_M16:1422arch_spec.SetFlags(ArchSpec::eMIPSAse_mips16);1423break;1424case llvm::ELF::EF_MIPS_ARCH_ASE_MDMX:1425arch_spec.SetFlags(ArchSpec::eMIPSAse_mdmx);1426break;1427default:1428break;1429}1430}14311432if (arch_spec.GetMachine() == llvm::Triple::arm ||1433arch_spec.GetMachine() == llvm::Triple::thumb) {1434if (header.e_flags & llvm::ELF::EF_ARM_SOFT_FLOAT)1435arch_spec.SetFlags(ArchSpec::eARM_abi_soft_float);1436else if (header.e_flags & llvm::ELF::EF_ARM_VFP_FLOAT)1437arch_spec.SetFlags(ArchSpec::eARM_abi_hard_float);1438}14391440if (arch_spec.GetMachine() == llvm::Triple::riscv32 ||1441arch_spec.GetMachine() == llvm::Triple::riscv64) {1442uint32_t flags = arch_spec.GetFlags();14431444if (header.e_flags & llvm::ELF::EF_RISCV_RVC)1445flags |= ArchSpec::eRISCV_rvc;1446if (header.e_flags & llvm::ELF::EF_RISCV_RVE)1447flags |= ArchSpec::eRISCV_rve;14481449if ((header.e_flags & llvm::ELF::EF_RISCV_FLOAT_ABI_SINGLE) ==1450llvm::ELF::EF_RISCV_FLOAT_ABI_SINGLE)1451flags |= ArchSpec::eRISCV_float_abi_single;1452else if ((header.e_flags & llvm::ELF::EF_RISCV_FLOAT_ABI_DOUBLE) ==1453llvm::ELF::EF_RISCV_FLOAT_ABI_DOUBLE)1454flags |= ArchSpec::eRISCV_float_abi_double;1455else if ((header.e_flags & llvm::ELF::EF_RISCV_FLOAT_ABI_QUAD) ==1456llvm::ELF::EF_RISCV_FLOAT_ABI_QUAD)1457flags |= ArchSpec::eRISCV_float_abi_quad;14581459arch_spec.SetFlags(flags);1460}14611462// If there are no section headers we are done.1463if (header.e_shnum == 0)1464return 0;14651466Log *log = GetLog(LLDBLog::Modules);14671468section_headers.resize(header.e_shnum);1469if (section_headers.size() != header.e_shnum)1470return 0;14711472const size_t sh_size = header.e_shnum * header.e_shentsize;1473const elf_off sh_offset = header.e_shoff;1474DataExtractor sh_data;1475if (sh_data.SetData(object_data, sh_offset, sh_size) != sh_size)1476return 0;14771478uint32_t idx;1479lldb::offset_t offset;1480for (idx = 0, offset = 0; idx < header.e_shnum; ++idx) {1481if (!section_headers[idx].Parse(sh_data, &offset))1482break;1483}1484if (idx < section_headers.size())1485section_headers.resize(idx);14861487const unsigned strtab_idx = header.e_shstrndx;1488if (strtab_idx && strtab_idx < section_headers.size()) {1489const ELFSectionHeaderInfo &sheader = section_headers[strtab_idx];1490const size_t byte_size = sheader.sh_size;1491const Elf64_Off offset = sheader.sh_offset;1492lldb_private::DataExtractor shstr_data;14931494if (shstr_data.SetData(object_data, offset, byte_size) == byte_size) {1495for (SectionHeaderCollIter I = section_headers.begin();1496I != section_headers.end(); ++I) {1497static ConstString g_sect_name_gnu_debuglink(".gnu_debuglink");1498const ELFSectionHeaderInfo &sheader = *I;1499const uint64_t section_size =1500sheader.sh_type == SHT_NOBITS ? 0 : sheader.sh_size;1501ConstString name(shstr_data.PeekCStr(I->sh_name));15021503I->section_name = name;15041505if (arch_spec.IsMIPS()) {1506uint32_t arch_flags = arch_spec.GetFlags();1507DataExtractor data;1508if (sheader.sh_type == SHT_MIPS_ABIFLAGS) {15091510if (section_size && (data.SetData(object_data, sheader.sh_offset,1511section_size) == section_size)) {1512// MIPS ASE Mask is at offset 12 in MIPS.abiflags section1513lldb::offset_t offset = 12; // MIPS ABI Flags Version: 01514arch_flags |= data.GetU32(&offset);15151516// The floating point ABI is at offset 71517offset = 7;1518switch (data.GetU8(&offset)) {1519case llvm::Mips::Val_GNU_MIPS_ABI_FP_ANY:1520arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_ANY;1521break;1522case llvm::Mips::Val_GNU_MIPS_ABI_FP_DOUBLE:1523arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_DOUBLE;1524break;1525case llvm::Mips::Val_GNU_MIPS_ABI_FP_SINGLE:1526arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_SINGLE;1527break;1528case llvm::Mips::Val_GNU_MIPS_ABI_FP_SOFT:1529arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_SOFT;1530break;1531case llvm::Mips::Val_GNU_MIPS_ABI_FP_OLD_64:1532arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_OLD_64;1533break;1534case llvm::Mips::Val_GNU_MIPS_ABI_FP_XX:1535arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_XX;1536break;1537case llvm::Mips::Val_GNU_MIPS_ABI_FP_64:1538arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_64;1539break;1540case llvm::Mips::Val_GNU_MIPS_ABI_FP_64A:1541arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_64A;1542break;1543}1544}1545}1546// Settings appropriate ArchSpec ABI Flags1547switch (header.e_flags & llvm::ELF::EF_MIPS_ABI) {1548case llvm::ELF::EF_MIPS_ABI_O32:1549arch_flags |= lldb_private::ArchSpec::eMIPSABI_O32;1550break;1551case EF_MIPS_ABI_O64:1552arch_flags |= lldb_private::ArchSpec::eMIPSABI_O64;1553break;1554case EF_MIPS_ABI_EABI32:1555arch_flags |= lldb_private::ArchSpec::eMIPSABI_EABI32;1556break;1557case EF_MIPS_ABI_EABI64:1558arch_flags |= lldb_private::ArchSpec::eMIPSABI_EABI64;1559break;1560default:1561// ABI Mask doesn't cover N32 and N64 ABI.1562if (header.e_ident[EI_CLASS] == llvm::ELF::ELFCLASS64)1563arch_flags |= lldb_private::ArchSpec::eMIPSABI_N64;1564else if (header.e_flags & llvm::ELF::EF_MIPS_ABI2)1565arch_flags |= lldb_private::ArchSpec::eMIPSABI_N32;1566break;1567}1568arch_spec.SetFlags(arch_flags);1569}15701571if (arch_spec.GetMachine() == llvm::Triple::arm ||1572arch_spec.GetMachine() == llvm::Triple::thumb) {1573DataExtractor data;15741575if (sheader.sh_type == SHT_ARM_ATTRIBUTES && section_size != 0 &&1576data.SetData(object_data, sheader.sh_offset, section_size) == section_size)1577ParseARMAttributes(data, section_size, arch_spec);1578}15791580if (name == g_sect_name_gnu_debuglink) {1581DataExtractor data;1582if (section_size && (data.SetData(object_data, sheader.sh_offset,1583section_size) == section_size)) {1584lldb::offset_t gnu_debuglink_offset = 0;1585gnu_debuglink_file = data.GetCStr(&gnu_debuglink_offset);1586gnu_debuglink_offset = llvm::alignTo(gnu_debuglink_offset, 4);1587data.GetU32(&gnu_debuglink_offset, &gnu_debuglink_crc, 1);1588}1589}15901591// Process ELF note section entries.1592bool is_note_header = (sheader.sh_type == SHT_NOTE);15931594// The section header ".note.android.ident" is stored as a1595// PROGBITS type header but it is actually a note header.1596static ConstString g_sect_name_android_ident(".note.android.ident");1597if (!is_note_header && name == g_sect_name_android_ident)1598is_note_header = true;15991600if (is_note_header) {1601// Allow notes to refine module info.1602DataExtractor data;1603if (section_size && (data.SetData(object_data, sheader.sh_offset,1604section_size) == section_size)) {1605Status error = RefineModuleDetailsFromNote(data, arch_spec, uuid);1606if (error.Fail()) {1607LLDB_LOGF(log, "ObjectFileELF::%s ELF note processing failed: %s",1608__FUNCTION__, error.AsCString());1609}1610}1611}1612}16131614// Make any unknown triple components to be unspecified unknowns.1615if (arch_spec.GetTriple().getVendor() == llvm::Triple::UnknownVendor)1616arch_spec.GetTriple().setVendorName(llvm::StringRef());1617if (arch_spec.GetTriple().getOS() == llvm::Triple::UnknownOS)1618arch_spec.GetTriple().setOSName(llvm::StringRef());16191620return section_headers.size();1621}1622}16231624section_headers.clear();1625return 0;1626}16271628llvm::StringRef1629ObjectFileELF::StripLinkerSymbolAnnotations(llvm::StringRef symbol_name) const {1630size_t pos = symbol_name.find('@');1631return symbol_name.substr(0, pos);1632}16331634// ParseSectionHeaders1635size_t ObjectFileELF::ParseSectionHeaders() {1636return GetSectionHeaderInfo(m_section_headers, m_data, m_header, m_uuid,1637m_gnu_debuglink_file, m_gnu_debuglink_crc,1638m_arch_spec);1639}16401641const ObjectFileELF::ELFSectionHeaderInfo *1642ObjectFileELF::GetSectionHeaderByIndex(lldb::user_id_t id) {1643if (!ParseSectionHeaders())1644return nullptr;16451646if (id < m_section_headers.size())1647return &m_section_headers[id];16481649return nullptr;1650}16511652lldb::user_id_t ObjectFileELF::GetSectionIndexByName(const char *name) {1653if (!name || !name[0] || !ParseSectionHeaders())1654return 0;1655for (size_t i = 1; i < m_section_headers.size(); ++i)1656if (m_section_headers[i].section_name == ConstString(name))1657return i;1658return 0;1659}16601661static SectionType GetSectionTypeFromName(llvm::StringRef Name) {1662if (Name.consume_front(".debug_")) {1663return llvm::StringSwitch<SectionType>(Name)1664.Case("abbrev", eSectionTypeDWARFDebugAbbrev)1665.Case("abbrev.dwo", eSectionTypeDWARFDebugAbbrevDwo)1666.Case("addr", eSectionTypeDWARFDebugAddr)1667.Case("aranges", eSectionTypeDWARFDebugAranges)1668.Case("cu_index", eSectionTypeDWARFDebugCuIndex)1669.Case("frame", eSectionTypeDWARFDebugFrame)1670.Case("info", eSectionTypeDWARFDebugInfo)1671.Case("info.dwo", eSectionTypeDWARFDebugInfoDwo)1672.Cases("line", "line.dwo", eSectionTypeDWARFDebugLine)1673.Cases("line_str", "line_str.dwo", eSectionTypeDWARFDebugLineStr)1674.Case("loc", eSectionTypeDWARFDebugLoc)1675.Case("loc.dwo", eSectionTypeDWARFDebugLocDwo)1676.Case("loclists", eSectionTypeDWARFDebugLocLists)1677.Case("loclists.dwo", eSectionTypeDWARFDebugLocListsDwo)1678.Case("macinfo", eSectionTypeDWARFDebugMacInfo)1679.Cases("macro", "macro.dwo", eSectionTypeDWARFDebugMacro)1680.Case("names", eSectionTypeDWARFDebugNames)1681.Case("pubnames", eSectionTypeDWARFDebugPubNames)1682.Case("pubtypes", eSectionTypeDWARFDebugPubTypes)1683.Case("ranges", eSectionTypeDWARFDebugRanges)1684.Case("rnglists", eSectionTypeDWARFDebugRngLists)1685.Case("rnglists.dwo", eSectionTypeDWARFDebugRngListsDwo)1686.Case("str", eSectionTypeDWARFDebugStr)1687.Case("str.dwo", eSectionTypeDWARFDebugStrDwo)1688.Case("str_offsets", eSectionTypeDWARFDebugStrOffsets)1689.Case("str_offsets.dwo", eSectionTypeDWARFDebugStrOffsetsDwo)1690.Case("tu_index", eSectionTypeDWARFDebugTuIndex)1691.Case("types", eSectionTypeDWARFDebugTypes)1692.Case("types.dwo", eSectionTypeDWARFDebugTypesDwo)1693.Default(eSectionTypeOther);1694}1695return llvm::StringSwitch<SectionType>(Name)1696.Case(".ARM.exidx", eSectionTypeARMexidx)1697.Case(".ARM.extab", eSectionTypeARMextab)1698.Case(".ctf", eSectionTypeDebug)1699.Cases(".data", ".tdata", eSectionTypeData)1700.Case(".eh_frame", eSectionTypeEHFrame)1701.Case(".gnu_debugaltlink", eSectionTypeDWARFGNUDebugAltLink)1702.Case(".gosymtab", eSectionTypeGoSymtab)1703.Case(".text", eSectionTypeCode)1704.Case(".swift_ast", eSectionTypeSwiftModules)1705.Default(eSectionTypeOther);1706}17071708SectionType ObjectFileELF::GetSectionType(const ELFSectionHeaderInfo &H) const {1709switch (H.sh_type) {1710case SHT_PROGBITS:1711if (H.sh_flags & SHF_EXECINSTR)1712return eSectionTypeCode;1713break;1714case SHT_NOBITS:1715if (H.sh_flags & SHF_ALLOC)1716return eSectionTypeZeroFill;1717break;1718case SHT_SYMTAB:1719return eSectionTypeELFSymbolTable;1720case SHT_DYNSYM:1721return eSectionTypeELFDynamicSymbols;1722case SHT_RELA:1723case SHT_REL:1724return eSectionTypeELFRelocationEntries;1725case SHT_DYNAMIC:1726return eSectionTypeELFDynamicLinkInfo;1727}1728return GetSectionTypeFromName(H.section_name.GetStringRef());1729}17301731static uint32_t GetTargetByteSize(SectionType Type, const ArchSpec &arch) {1732switch (Type) {1733case eSectionTypeData:1734case eSectionTypeZeroFill:1735return arch.GetDataByteSize();1736case eSectionTypeCode:1737return arch.GetCodeByteSize();1738default:1739return 1;1740}1741}17421743static Permissions GetPermissions(const ELFSectionHeader &H) {1744Permissions Perm = Permissions(0);1745if (H.sh_flags & SHF_ALLOC)1746Perm |= ePermissionsReadable;1747if (H.sh_flags & SHF_WRITE)1748Perm |= ePermissionsWritable;1749if (H.sh_flags & SHF_EXECINSTR)1750Perm |= ePermissionsExecutable;1751return Perm;1752}17531754static Permissions GetPermissions(const ELFProgramHeader &H) {1755Permissions Perm = Permissions(0);1756if (H.p_flags & PF_R)1757Perm |= ePermissionsReadable;1758if (H.p_flags & PF_W)1759Perm |= ePermissionsWritable;1760if (H.p_flags & PF_X)1761Perm |= ePermissionsExecutable;1762return Perm;1763}17641765namespace {17661767using VMRange = lldb_private::Range<addr_t, addr_t>;17681769struct SectionAddressInfo {1770SectionSP Segment;1771VMRange Range;1772};17731774// (Unlinked) ELF object files usually have 0 for every section address, meaning1775// we need to compute synthetic addresses in order for "file addresses" from1776// different sections to not overlap. This class handles that logic.1777class VMAddressProvider {1778using VMMap = llvm::IntervalMap<addr_t, SectionSP, 4,1779llvm::IntervalMapHalfOpenInfo<addr_t>>;17801781ObjectFile::Type ObjectType;1782addr_t NextVMAddress = 0;1783VMMap::Allocator Alloc;1784VMMap Segments{Alloc};1785VMMap Sections{Alloc};1786lldb_private::Log *Log = GetLog(LLDBLog::Modules);1787size_t SegmentCount = 0;1788std::string SegmentName;17891790VMRange GetVMRange(const ELFSectionHeader &H) {1791addr_t Address = H.sh_addr;1792addr_t Size = H.sh_flags & SHF_ALLOC ? H.sh_size : 0;17931794// When this is a debug file for relocatable file, the address is all zero1795// and thus needs to use accumulate method1796if ((ObjectType == ObjectFile::Type::eTypeObjectFile ||1797(ObjectType == ObjectFile::Type::eTypeDebugInfo && H.sh_addr == 0)) &&1798Segments.empty() && (H.sh_flags & SHF_ALLOC)) {1799NextVMAddress =1800llvm::alignTo(NextVMAddress, std::max<addr_t>(H.sh_addralign, 1));1801Address = NextVMAddress;1802NextVMAddress += Size;1803}1804return VMRange(Address, Size);1805}18061807public:1808VMAddressProvider(ObjectFile::Type Type, llvm::StringRef SegmentName)1809: ObjectType(Type), SegmentName(std::string(SegmentName)) {}18101811std::string GetNextSegmentName() const {1812return llvm::formatv("{0}[{1}]", SegmentName, SegmentCount).str();1813}18141815std::optional<VMRange> GetAddressInfo(const ELFProgramHeader &H) {1816if (H.p_memsz == 0) {1817LLDB_LOG(Log, "Ignoring zero-sized {0} segment. Corrupt object file?",1818SegmentName);1819return std::nullopt;1820}18211822if (Segments.overlaps(H.p_vaddr, H.p_vaddr + H.p_memsz)) {1823LLDB_LOG(Log, "Ignoring overlapping {0} segment. Corrupt object file?",1824SegmentName);1825return std::nullopt;1826}1827return VMRange(H.p_vaddr, H.p_memsz);1828}18291830std::optional<SectionAddressInfo> GetAddressInfo(const ELFSectionHeader &H) {1831VMRange Range = GetVMRange(H);1832SectionSP Segment;1833auto It = Segments.find(Range.GetRangeBase());1834if ((H.sh_flags & SHF_ALLOC) && It.valid()) {1835addr_t MaxSize;1836if (It.start() <= Range.GetRangeBase()) {1837MaxSize = It.stop() - Range.GetRangeBase();1838Segment = *It;1839} else1840MaxSize = It.start() - Range.GetRangeBase();1841if (Range.GetByteSize() > MaxSize) {1842LLDB_LOG(Log, "Shortening section crossing segment boundaries. "1843"Corrupt object file?");1844Range.SetByteSize(MaxSize);1845}1846}1847if (Range.GetByteSize() > 0 &&1848Sections.overlaps(Range.GetRangeBase(), Range.GetRangeEnd())) {1849LLDB_LOG(Log, "Ignoring overlapping section. Corrupt object file?");1850return std::nullopt;1851}1852if (Segment)1853Range.Slide(-Segment->GetFileAddress());1854return SectionAddressInfo{Segment, Range};1855}18561857void AddSegment(const VMRange &Range, SectionSP Seg) {1858Segments.insert(Range.GetRangeBase(), Range.GetRangeEnd(), std::move(Seg));1859++SegmentCount;1860}18611862void AddSection(SectionAddressInfo Info, SectionSP Sect) {1863if (Info.Range.GetByteSize() == 0)1864return;1865if (Info.Segment)1866Info.Range.Slide(Info.Segment->GetFileAddress());1867Sections.insert(Info.Range.GetRangeBase(), Info.Range.GetRangeEnd(),1868std::move(Sect));1869}1870};1871}18721873// We have to do this because ELF doesn't have section IDs, and also1874// doesn't require section names to be unique. (We use the section index1875// for section IDs, but that isn't guaranteed to be the same in separate1876// debug images.)1877static SectionSP FindMatchingSection(const SectionList §ion_list,1878SectionSP section) {1879SectionSP sect_sp;18801881addr_t vm_addr = section->GetFileAddress();1882ConstString name = section->GetName();1883offset_t byte_size = section->GetByteSize();1884bool thread_specific = section->IsThreadSpecific();1885uint32_t permissions = section->GetPermissions();1886uint32_t alignment = section->GetLog2Align();18871888for (auto sect : section_list) {1889if (sect->GetName() == name &&1890sect->IsThreadSpecific() == thread_specific &&1891sect->GetPermissions() == permissions &&1892sect->GetByteSize() == byte_size && sect->GetFileAddress() == vm_addr &&1893sect->GetLog2Align() == alignment) {1894sect_sp = sect;1895break;1896} else {1897sect_sp = FindMatchingSection(sect->GetChildren(), section);1898if (sect_sp)1899break;1900}1901}19021903return sect_sp;1904}19051906void ObjectFileELF::CreateSections(SectionList &unified_section_list) {1907if (m_sections_up)1908return;19091910m_sections_up = std::make_unique<SectionList>();1911VMAddressProvider regular_provider(GetType(), "PT_LOAD");1912VMAddressProvider tls_provider(GetType(), "PT_TLS");19131914for (const auto &EnumPHdr : llvm::enumerate(ProgramHeaders())) {1915const ELFProgramHeader &PHdr = EnumPHdr.value();1916if (PHdr.p_type != PT_LOAD && PHdr.p_type != PT_TLS)1917continue;19181919VMAddressProvider &provider =1920PHdr.p_type == PT_TLS ? tls_provider : regular_provider;1921auto InfoOr = provider.GetAddressInfo(PHdr);1922if (!InfoOr)1923continue;19241925uint32_t Log2Align = llvm::Log2_64(std::max<elf_xword>(PHdr.p_align, 1));1926SectionSP Segment = std::make_shared<Section>(1927GetModule(), this, SegmentID(EnumPHdr.index()),1928ConstString(provider.GetNextSegmentName()), eSectionTypeContainer,1929InfoOr->GetRangeBase(), InfoOr->GetByteSize(), PHdr.p_offset,1930PHdr.p_filesz, Log2Align, /*flags*/ 0);1931Segment->SetPermissions(GetPermissions(PHdr));1932Segment->SetIsThreadSpecific(PHdr.p_type == PT_TLS);1933m_sections_up->AddSection(Segment);19341935provider.AddSegment(*InfoOr, std::move(Segment));1936}19371938ParseSectionHeaders();1939if (m_section_headers.empty())1940return;19411942for (SectionHeaderCollIter I = std::next(m_section_headers.begin());1943I != m_section_headers.end(); ++I) {1944const ELFSectionHeaderInfo &header = *I;19451946ConstString &name = I->section_name;1947const uint64_t file_size =1948header.sh_type == SHT_NOBITS ? 0 : header.sh_size;19491950VMAddressProvider &provider =1951header.sh_flags & SHF_TLS ? tls_provider : regular_provider;1952auto InfoOr = provider.GetAddressInfo(header);1953if (!InfoOr)1954continue;19551956SectionType sect_type = GetSectionType(header);19571958const uint32_t target_bytes_size =1959GetTargetByteSize(sect_type, m_arch_spec);19601961elf::elf_xword log2align =1962(header.sh_addralign == 0) ? 0 : llvm::Log2_64(header.sh_addralign);19631964SectionSP section_sp(new Section(1965InfoOr->Segment, GetModule(), // Module to which this section belongs.1966this, // ObjectFile to which this section belongs and should1967// read section data from.1968SectionIndex(I), // Section ID.1969name, // Section name.1970sect_type, // Section type.1971InfoOr->Range.GetRangeBase(), // VM address.1972InfoOr->Range.GetByteSize(), // VM size in bytes of this section.1973header.sh_offset, // Offset of this section in the file.1974file_size, // Size of the section as found in the file.1975log2align, // Alignment of the section1976header.sh_flags, // Flags for this section.1977target_bytes_size)); // Number of host bytes per target byte19781979section_sp->SetPermissions(GetPermissions(header));1980section_sp->SetIsThreadSpecific(header.sh_flags & SHF_TLS);1981(InfoOr->Segment ? InfoOr->Segment->GetChildren() : *m_sections_up)1982.AddSection(section_sp);1983provider.AddSection(std::move(*InfoOr), std::move(section_sp));1984}19851986// For eTypeDebugInfo files, the Symbol Vendor will take care of updating the1987// unified section list.1988if (GetType() != eTypeDebugInfo)1989unified_section_list = *m_sections_up;19901991// If there's a .gnu_debugdata section, we'll try to read the .symtab that's1992// embedded in there and replace the one in the original object file (if any).1993// If there's none in the orignal object file, we add it to it.1994if (auto gdd_obj_file = GetGnuDebugDataObjectFile()) {1995if (auto gdd_objfile_section_list = gdd_obj_file->GetSectionList()) {1996if (SectionSP symtab_section_sp =1997gdd_objfile_section_list->FindSectionByType(1998eSectionTypeELFSymbolTable, true)) {1999SectionSP module_section_sp = unified_section_list.FindSectionByType(2000eSectionTypeELFSymbolTable, true);2001if (module_section_sp)2002unified_section_list.ReplaceSection(module_section_sp->GetID(),2003symtab_section_sp);2004else2005unified_section_list.AddSection(symtab_section_sp);2006}2007}2008}2009}20102011std::shared_ptr<ObjectFileELF> ObjectFileELF::GetGnuDebugDataObjectFile() {2012if (m_gnu_debug_data_object_file != nullptr)2013return m_gnu_debug_data_object_file;20142015SectionSP section =2016GetSectionList()->FindSectionByName(ConstString(".gnu_debugdata"));2017if (!section)2018return nullptr;20192020if (!lldb_private::lzma::isAvailable()) {2021GetModule()->ReportWarning(2022"No LZMA support found for reading .gnu_debugdata section");2023return nullptr;2024}20252026// Uncompress the data2027DataExtractor data;2028section->GetSectionData(data);2029llvm::SmallVector<uint8_t, 0> uncompressedData;2030auto err = lldb_private::lzma::uncompress(data.GetData(), uncompressedData);2031if (err) {2032GetModule()->ReportWarning(2033"An error occurred while decompression the section {0}: {1}",2034section->GetName().AsCString(), llvm::toString(std::move(err)).c_str());2035return nullptr;2036}20372038// Construct ObjectFileELF object from decompressed buffer2039DataBufferSP gdd_data_buf(2040new DataBufferHeap(uncompressedData.data(), uncompressedData.size()));2041auto fspec = GetFileSpec().CopyByAppendingPathComponent(2042llvm::StringRef("gnu_debugdata"));2043m_gnu_debug_data_object_file.reset(new ObjectFileELF(2044GetModule(), gdd_data_buf, 0, &fspec, 0, gdd_data_buf->GetByteSize()));20452046// This line is essential; otherwise a breakpoint can be set but not hit.2047m_gnu_debug_data_object_file->SetType(ObjectFile::eTypeDebugInfo);20482049ArchSpec spec = m_gnu_debug_data_object_file->GetArchitecture();2050if (spec && m_gnu_debug_data_object_file->SetModulesArchitecture(spec))2051return m_gnu_debug_data_object_file;20522053return nullptr;2054}20552056// Find the arm/aarch64 mapping symbol character in the given symbol name.2057// Mapping symbols have the form of "$<char>[.<any>]*". Additionally we2058// recognize cases when the mapping symbol prefixed by an arbitrary string2059// because if a symbol prefix added to each symbol in the object file with2060// objcopy then the mapping symbols are also prefixed.2061static char FindArmAarch64MappingSymbol(const char *symbol_name) {2062if (!symbol_name)2063return '\0';20642065const char *dollar_pos = ::strchr(symbol_name, '$');2066if (!dollar_pos || dollar_pos[1] == '\0')2067return '\0';20682069if (dollar_pos[2] == '\0' || dollar_pos[2] == '.')2070return dollar_pos[1];2071return '\0';2072}20732074#define STO_MIPS_ISA (3 << 6)2075#define STO_MICROMIPS (2 << 6)2076#define IS_MICROMIPS(ST_OTHER) (((ST_OTHER)&STO_MIPS_ISA) == STO_MICROMIPS)20772078// private2079std::pair<unsigned, ObjectFileELF::FileAddressToAddressClassMap>2080ObjectFileELF::ParseSymbols(Symtab *symtab, user_id_t start_id,2081SectionList *section_list, const size_t num_symbols,2082const DataExtractor &symtab_data,2083const DataExtractor &strtab_data) {2084ELFSymbol symbol;2085lldb::offset_t offset = 0;2086// The changes these symbols would make to the class map. We will also update2087// m_address_class_map but need to tell the caller what changed because the2088// caller may be another object file.2089FileAddressToAddressClassMap address_class_map;20902091static ConstString text_section_name(".text");2092static ConstString init_section_name(".init");2093static ConstString fini_section_name(".fini");2094static ConstString ctors_section_name(".ctors");2095static ConstString dtors_section_name(".dtors");20962097static ConstString data_section_name(".data");2098static ConstString rodata_section_name(".rodata");2099static ConstString rodata1_section_name(".rodata1");2100static ConstString data2_section_name(".data1");2101static ConstString bss_section_name(".bss");2102static ConstString opd_section_name(".opd"); // For ppc6421032104// On Android the oatdata and the oatexec symbols in the oat and odex files2105// covers the full .text section what causes issues with displaying unusable2106// symbol name to the user and very slow unwinding speed because the2107// instruction emulation based unwind plans try to emulate all instructions2108// in these symbols. Don't add these symbols to the symbol list as they have2109// no use for the debugger and they are causing a lot of trouble. Filtering2110// can't be restricted to Android because this special object file don't2111// contain the note section specifying the environment to Android but the2112// custom extension and file name makes it highly unlikely that this will2113// collide with anything else.2114llvm::StringRef file_extension = m_file.GetFileNameExtension();2115bool skip_oatdata_oatexec =2116file_extension == ".oat" || file_extension == ".odex";21172118ArchSpec arch = GetArchitecture();2119ModuleSP module_sp(GetModule());2120SectionList *module_section_list =2121module_sp ? module_sp->GetSectionList() : nullptr;21222123// We might have debug information in a separate object, in which case2124// we need to map the sections from that object to the sections in the2125// main object during symbol lookup. If we had to compare the sections2126// for every single symbol, that would be expensive, so this map is2127// used to accelerate the process.2128std::unordered_map<lldb::SectionSP, lldb::SectionSP> section_map;21292130unsigned i;2131for (i = 0; i < num_symbols; ++i) {2132if (!symbol.Parse(symtab_data, &offset))2133break;21342135const char *symbol_name = strtab_data.PeekCStr(symbol.st_name);2136if (!symbol_name)2137symbol_name = "";21382139// No need to add non-section symbols that have no names2140if (symbol.getType() != STT_SECTION &&2141(symbol_name == nullptr || symbol_name[0] == '\0'))2142continue;21432144// Skipping oatdata and oatexec sections if it is requested. See details2145// above the definition of skip_oatdata_oatexec for the reasons.2146if (skip_oatdata_oatexec && (::strcmp(symbol_name, "oatdata") == 0 ||2147::strcmp(symbol_name, "oatexec") == 0))2148continue;21492150SectionSP symbol_section_sp;2151SymbolType symbol_type = eSymbolTypeInvalid;2152Elf64_Half shndx = symbol.st_shndx;21532154switch (shndx) {2155case SHN_ABS:2156symbol_type = eSymbolTypeAbsolute;2157break;2158case SHN_UNDEF:2159symbol_type = eSymbolTypeUndefined;2160break;2161default:2162symbol_section_sp = section_list->FindSectionByID(shndx);2163break;2164}21652166// If a symbol is undefined do not process it further even if it has a STT2167// type2168if (symbol_type != eSymbolTypeUndefined) {2169switch (symbol.getType()) {2170default:2171case STT_NOTYPE:2172// The symbol's type is not specified.2173break;21742175case STT_OBJECT:2176// The symbol is associated with a data object, such as a variable, an2177// array, etc.2178symbol_type = eSymbolTypeData;2179break;21802181case STT_FUNC:2182// The symbol is associated with a function or other executable code.2183symbol_type = eSymbolTypeCode;2184break;21852186case STT_SECTION:2187// The symbol is associated with a section. Symbol table entries of2188// this type exist primarily for relocation and normally have STB_LOCAL2189// binding.2190break;21912192case STT_FILE:2193// Conventionally, the symbol's name gives the name of the source file2194// associated with the object file. A file symbol has STB_LOCAL2195// binding, its section index is SHN_ABS, and it precedes the other2196// STB_LOCAL symbols for the file, if it is present.2197symbol_type = eSymbolTypeSourceFile;2198break;21992200case STT_GNU_IFUNC:2201// The symbol is associated with an indirect function. The actual2202// function will be resolved if it is referenced.2203symbol_type = eSymbolTypeResolver;2204break;2205}2206}22072208if (symbol_type == eSymbolTypeInvalid && symbol.getType() != STT_SECTION) {2209if (symbol_section_sp) {2210ConstString sect_name = symbol_section_sp->GetName();2211if (sect_name == text_section_name || sect_name == init_section_name ||2212sect_name == fini_section_name || sect_name == ctors_section_name ||2213sect_name == dtors_section_name) {2214symbol_type = eSymbolTypeCode;2215} else if (sect_name == data_section_name ||2216sect_name == data2_section_name ||2217sect_name == rodata_section_name ||2218sect_name == rodata1_section_name ||2219sect_name == bss_section_name) {2220symbol_type = eSymbolTypeData;2221}2222}2223}22242225int64_t symbol_value_offset = 0;2226uint32_t additional_flags = 0;22272228if (arch.IsValid()) {2229if (arch.GetMachine() == llvm::Triple::arm) {2230if (symbol.getBinding() == STB_LOCAL) {2231char mapping_symbol = FindArmAarch64MappingSymbol(symbol_name);2232if (symbol_type == eSymbolTypeCode) {2233switch (mapping_symbol) {2234case 'a':2235// $a[.<any>]* - marks an ARM instruction sequence2236address_class_map[symbol.st_value] = AddressClass::eCode;2237break;2238case 'b':2239case 't':2240// $b[.<any>]* - marks a THUMB BL instruction sequence2241// $t[.<any>]* - marks a THUMB instruction sequence2242address_class_map[symbol.st_value] =2243AddressClass::eCodeAlternateISA;2244break;2245case 'd':2246// $d[.<any>]* - marks a data item sequence (e.g. lit pool)2247address_class_map[symbol.st_value] = AddressClass::eData;2248break;2249}2250}2251if (mapping_symbol)2252continue;2253}2254} else if (arch.GetMachine() == llvm::Triple::aarch64) {2255if (symbol.getBinding() == STB_LOCAL) {2256char mapping_symbol = FindArmAarch64MappingSymbol(symbol_name);2257if (symbol_type == eSymbolTypeCode) {2258switch (mapping_symbol) {2259case 'x':2260// $x[.<any>]* - marks an A64 instruction sequence2261address_class_map[symbol.st_value] = AddressClass::eCode;2262break;2263case 'd':2264// $d[.<any>]* - marks a data item sequence (e.g. lit pool)2265address_class_map[symbol.st_value] = AddressClass::eData;2266break;2267}2268}2269if (mapping_symbol)2270continue;2271}2272}22732274if (arch.GetMachine() == llvm::Triple::arm) {2275if (symbol_type == eSymbolTypeCode) {2276if (symbol.st_value & 1) {2277// Subtracting 1 from the address effectively unsets the low order2278// bit, which results in the address actually pointing to the2279// beginning of the symbol. This delta will be used below in2280// conjunction with symbol.st_value to produce the final2281// symbol_value that we store in the symtab.2282symbol_value_offset = -1;2283address_class_map[symbol.st_value ^ 1] =2284AddressClass::eCodeAlternateISA;2285} else {2286// This address is ARM2287address_class_map[symbol.st_value] = AddressClass::eCode;2288}2289}2290}22912292/*2293* MIPS:2294* The bit #0 of an address is used for ISA mode (1 for microMIPS, 0 for2295* MIPS).2296* This allows processor to switch between microMIPS and MIPS without any2297* need2298* for special mode-control register. However, apart from .debug_line,2299* none of2300* the ELF/DWARF sections set the ISA bit (for symbol or section). Use2301* st_other2302* flag to check whether the symbol is microMIPS and then set the address2303* class2304* accordingly.2305*/2306if (arch.IsMIPS()) {2307if (IS_MICROMIPS(symbol.st_other))2308address_class_map[symbol.st_value] = AddressClass::eCodeAlternateISA;2309else if ((symbol.st_value & 1) && (symbol_type == eSymbolTypeCode)) {2310symbol.st_value = symbol.st_value & (~1ull);2311address_class_map[symbol.st_value] = AddressClass::eCodeAlternateISA;2312} else {2313if (symbol_type == eSymbolTypeCode)2314address_class_map[symbol.st_value] = AddressClass::eCode;2315else if (symbol_type == eSymbolTypeData)2316address_class_map[symbol.st_value] = AddressClass::eData;2317else2318address_class_map[symbol.st_value] = AddressClass::eUnknown;2319}2320}2321}23222323// symbol_value_offset may contain 0 for ARM symbols or -1 for THUMB2324// symbols. See above for more details.2325uint64_t symbol_value = symbol.st_value + symbol_value_offset;23262327if (symbol_section_sp &&2328CalculateType() != ObjectFile::Type::eTypeObjectFile)2329symbol_value -= symbol_section_sp->GetFileAddress();23302331if (symbol_section_sp && module_section_list &&2332module_section_list != section_list) {2333auto section_it = section_map.find(symbol_section_sp);2334if (section_it == section_map.end()) {2335section_it = section_map2336.emplace(symbol_section_sp,2337FindMatchingSection(*module_section_list,2338symbol_section_sp))2339.first;2340}2341if (section_it->second)2342symbol_section_sp = section_it->second;2343}23442345bool is_global = symbol.getBinding() == STB_GLOBAL;2346uint32_t flags = symbol.st_other << 8 | symbol.st_info | additional_flags;2347llvm::StringRef symbol_ref(symbol_name);23482349// Symbol names may contain @VERSION suffixes. Find those and strip them2350// temporarily.2351size_t version_pos = symbol_ref.find('@');2352bool has_suffix = version_pos != llvm::StringRef::npos;2353llvm::StringRef symbol_bare = symbol_ref.substr(0, version_pos);2354Mangled mangled(symbol_bare);23552356// Now append the suffix back to mangled and unmangled names. Only do it if2357// the demangling was successful (string is not empty).2358if (has_suffix) {2359llvm::StringRef suffix = symbol_ref.substr(version_pos);23602361llvm::StringRef mangled_name = mangled.GetMangledName().GetStringRef();2362if (!mangled_name.empty())2363mangled.SetMangledName(ConstString((mangled_name + suffix).str()));23642365ConstString demangled = mangled.GetDemangledName();2366llvm::StringRef demangled_name = demangled.GetStringRef();2367if (!demangled_name.empty())2368mangled.SetDemangledName(ConstString((demangled_name + suffix).str()));2369}23702371// In ELF all symbol should have a valid size but it is not true for some2372// function symbols coming from hand written assembly. As none of the2373// function symbol should have 0 size we try to calculate the size for2374// these symbols in the symtab with saying that their original size is not2375// valid.2376bool symbol_size_valid =2377symbol.st_size != 0 || symbol.getType() != STT_FUNC;23782379bool is_trampoline = false;2380if (arch.IsValid() && (arch.GetMachine() == llvm::Triple::aarch64)) {2381// On AArch64, trampolines are registered as code.2382// If we detect a trampoline (which starts with __AArch64ADRPThunk_ or2383// __AArch64AbsLongThunk_) we register the symbol as a trampoline. This2384// way we will be able to detect the trampoline when we step in a function2385// and step through the trampoline.2386if (symbol_type == eSymbolTypeCode) {2387llvm::StringRef trampoline_name = mangled.GetName().GetStringRef();2388if (trampoline_name.starts_with("__AArch64ADRPThunk_") ||2389trampoline_name.starts_with("__AArch64AbsLongThunk_")) {2390symbol_type = eSymbolTypeTrampoline;2391is_trampoline = true;2392}2393}2394}23952396Symbol dc_symbol(2397i + start_id, // ID is the original symbol table index.2398mangled,2399symbol_type, // Type of this symbol2400is_global, // Is this globally visible?2401false, // Is this symbol debug info?2402is_trampoline, // Is this symbol a trampoline?2403false, // Is this symbol artificial?2404AddressRange(symbol_section_sp, // Section in which this symbol is2405// defined or null.2406symbol_value, // Offset in section or symbol value.2407symbol.st_size), // Size in bytes of this symbol.2408symbol_size_valid, // Symbol size is valid2409has_suffix, // Contains linker annotations?2410flags); // Symbol flags.2411if (symbol.getBinding() == STB_WEAK)2412dc_symbol.SetIsWeak(true);2413symtab->AddSymbol(dc_symbol);2414}24152416m_address_class_map.merge(address_class_map);2417return {i, address_class_map};2418}24192420std::pair<unsigned, ObjectFileELF::FileAddressToAddressClassMap>2421ObjectFileELF::ParseSymbolTable(Symtab *symbol_table, user_id_t start_id,2422lldb_private::Section *symtab) {2423if (symtab->GetObjectFile() != this) {2424// If the symbol table section is owned by a different object file, have it2425// do the parsing.2426ObjectFileELF *obj_file_elf =2427static_cast<ObjectFileELF *>(symtab->GetObjectFile());2428auto [num_symbols, address_class_map] =2429obj_file_elf->ParseSymbolTable(symbol_table, start_id, symtab);24302431// The other object file returned the changes it made to its address2432// class map, make the same changes to ours.2433m_address_class_map.merge(address_class_map);24342435return {num_symbols, address_class_map};2436}24372438// Get section list for this object file.2439SectionList *section_list = m_sections_up.get();2440if (!section_list)2441return {};24422443user_id_t symtab_id = symtab->GetID();2444const ELFSectionHeaderInfo *symtab_hdr = GetSectionHeaderByIndex(symtab_id);2445assert(symtab_hdr->sh_type == SHT_SYMTAB ||2446symtab_hdr->sh_type == SHT_DYNSYM);24472448// sh_link: section header index of associated string table.2449user_id_t strtab_id = symtab_hdr->sh_link;2450Section *strtab = section_list->FindSectionByID(strtab_id).get();24512452if (symtab && strtab) {2453assert(symtab->GetObjectFile() == this);2454assert(strtab->GetObjectFile() == this);24552456DataExtractor symtab_data;2457DataExtractor strtab_data;2458if (ReadSectionData(symtab, symtab_data) &&2459ReadSectionData(strtab, strtab_data)) {2460size_t num_symbols = symtab_data.GetByteSize() / symtab_hdr->sh_entsize;24612462return ParseSymbols(symbol_table, start_id, section_list, num_symbols,2463symtab_data, strtab_data);2464}2465}24662467return {0, {}};2468}24692470size_t ObjectFileELF::ParseDynamicSymbols() {2471if (m_dynamic_symbols.size())2472return m_dynamic_symbols.size();24732474SectionList *section_list = GetSectionList();2475if (!section_list)2476return 0;24772478// Find the SHT_DYNAMIC section.2479Section *dynsym =2480section_list->FindSectionByType(eSectionTypeELFDynamicLinkInfo, true)2481.get();2482if (!dynsym)2483return 0;2484assert(dynsym->GetObjectFile() == this);24852486ELFDynamic symbol;2487DataExtractor dynsym_data;2488if (ReadSectionData(dynsym, dynsym_data)) {2489const lldb::offset_t section_size = dynsym_data.GetByteSize();2490lldb::offset_t cursor = 0;24912492while (cursor < section_size) {2493if (!symbol.Parse(dynsym_data, &cursor))2494break;24952496m_dynamic_symbols.push_back(symbol);2497}2498}24992500return m_dynamic_symbols.size();2501}25022503const ELFDynamic *ObjectFileELF::FindDynamicSymbol(unsigned tag) {2504if (!ParseDynamicSymbols())2505return nullptr;25062507DynamicSymbolCollIter I = m_dynamic_symbols.begin();2508DynamicSymbolCollIter E = m_dynamic_symbols.end();2509for (; I != E; ++I) {2510ELFDynamic *symbol = &*I;25112512if (symbol->d_tag == tag)2513return symbol;2514}25152516return nullptr;2517}25182519unsigned ObjectFileELF::PLTRelocationType() {2520// DT_PLTREL2521// This member specifies the type of relocation entry to which the2522// procedure linkage table refers. The d_val member holds DT_REL or2523// DT_RELA, as appropriate. All relocations in a procedure linkage table2524// must use the same relocation.2525const ELFDynamic *symbol = FindDynamicSymbol(DT_PLTREL);25262527if (symbol)2528return symbol->d_val;25292530return 0;2531}25322533// Returns the size of the normal plt entries and the offset of the first2534// normal plt entry. The 0th entry in the plt table is usually a resolution2535// entry which have different size in some architectures then the rest of the2536// plt entries.2537static std::pair<uint64_t, uint64_t>2538GetPltEntrySizeAndOffset(const ELFSectionHeader *rel_hdr,2539const ELFSectionHeader *plt_hdr) {2540const elf_xword num_relocations = rel_hdr->sh_size / rel_hdr->sh_entsize;25412542// Clang 3.3 sets entsize to 4 for 32-bit binaries, but the plt entries are2543// 16 bytes. So round the entsize up by the alignment if addralign is set.2544elf_xword plt_entsize =2545plt_hdr->sh_addralign2546? llvm::alignTo(plt_hdr->sh_entsize, plt_hdr->sh_addralign)2547: plt_hdr->sh_entsize;25482549// Some linkers e.g ld for arm, fill plt_hdr->sh_entsize field incorrectly.2550// PLT entries relocation code in general requires multiple instruction and2551// should be greater than 4 bytes in most cases. Try to guess correct size2552// just in case.2553if (plt_entsize <= 4) {2554// The linker haven't set the plt_hdr->sh_entsize field. Try to guess the2555// size of the plt entries based on the number of entries and the size of2556// the plt section with the assumption that the size of the 0th entry is at2557// least as big as the size of the normal entries and it isn't much bigger2558// then that.2559if (plt_hdr->sh_addralign)2560plt_entsize = plt_hdr->sh_size / plt_hdr->sh_addralign /2561(num_relocations + 1) * plt_hdr->sh_addralign;2562else2563plt_entsize = plt_hdr->sh_size / (num_relocations + 1);2564}25652566elf_xword plt_offset = plt_hdr->sh_size - num_relocations * plt_entsize;25672568return std::make_pair(plt_entsize, plt_offset);2569}25702571static unsigned ParsePLTRelocations(2572Symtab *symbol_table, user_id_t start_id, unsigned rel_type,2573const ELFHeader *hdr, const ELFSectionHeader *rel_hdr,2574const ELFSectionHeader *plt_hdr, const ELFSectionHeader *sym_hdr,2575const lldb::SectionSP &plt_section_sp, DataExtractor &rel_data,2576DataExtractor &symtab_data, DataExtractor &strtab_data) {2577ELFRelocation rel(rel_type);2578ELFSymbol symbol;2579lldb::offset_t offset = 0;25802581uint64_t plt_offset, plt_entsize;2582std::tie(plt_entsize, plt_offset) =2583GetPltEntrySizeAndOffset(rel_hdr, plt_hdr);2584const elf_xword num_relocations = rel_hdr->sh_size / rel_hdr->sh_entsize;25852586typedef unsigned (*reloc_info_fn)(const ELFRelocation &rel);2587reloc_info_fn reloc_type;2588reloc_info_fn reloc_symbol;25892590if (hdr->Is32Bit()) {2591reloc_type = ELFRelocation::RelocType32;2592reloc_symbol = ELFRelocation::RelocSymbol32;2593} else {2594reloc_type = ELFRelocation::RelocType64;2595reloc_symbol = ELFRelocation::RelocSymbol64;2596}25972598unsigned slot_type = hdr->GetRelocationJumpSlotType();2599unsigned i;2600for (i = 0; i < num_relocations; ++i) {2601if (!rel.Parse(rel_data, &offset))2602break;26032604if (reloc_type(rel) != slot_type)2605continue;26062607lldb::offset_t symbol_offset = reloc_symbol(rel) * sym_hdr->sh_entsize;2608if (!symbol.Parse(symtab_data, &symbol_offset))2609break;26102611const char *symbol_name = strtab_data.PeekCStr(symbol.st_name);2612uint64_t plt_index = plt_offset + i * plt_entsize;26132614Symbol jump_symbol(2615i + start_id, // Symbol table index2616symbol_name, // symbol name.2617eSymbolTypeTrampoline, // Type of this symbol2618false, // Is this globally visible?2619false, // Is this symbol debug info?2620true, // Is this symbol a trampoline?2621true, // Is this symbol artificial?2622plt_section_sp, // Section in which this symbol is defined or null.2623plt_index, // Offset in section or symbol value.2624plt_entsize, // Size in bytes of this symbol.2625true, // Size is valid2626false, // Contains linker annotations?26270); // Symbol flags.26282629symbol_table->AddSymbol(jump_symbol);2630}26312632return i;2633}26342635unsigned2636ObjectFileELF::ParseTrampolineSymbols(Symtab *symbol_table, user_id_t start_id,2637const ELFSectionHeaderInfo *rel_hdr,2638user_id_t rel_id) {2639assert(rel_hdr->sh_type == SHT_RELA || rel_hdr->sh_type == SHT_REL);26402641// The link field points to the associated symbol table.2642user_id_t symtab_id = rel_hdr->sh_link;26432644// If the link field doesn't point to the appropriate symbol name table then2645// try to find it by name as some compiler don't fill in the link fields.2646if (!symtab_id)2647symtab_id = GetSectionIndexByName(".dynsym");26482649// Get PLT section. We cannot use rel_hdr->sh_info, since current linkers2650// point that to the .got.plt or .got section instead of .plt.2651user_id_t plt_id = GetSectionIndexByName(".plt");26522653if (!symtab_id || !plt_id)2654return 0;26552656const ELFSectionHeaderInfo *plt_hdr = GetSectionHeaderByIndex(plt_id);2657if (!plt_hdr)2658return 0;26592660const ELFSectionHeaderInfo *sym_hdr = GetSectionHeaderByIndex(symtab_id);2661if (!sym_hdr)2662return 0;26632664SectionList *section_list = m_sections_up.get();2665if (!section_list)2666return 0;26672668Section *rel_section = section_list->FindSectionByID(rel_id).get();2669if (!rel_section)2670return 0;26712672SectionSP plt_section_sp(section_list->FindSectionByID(plt_id));2673if (!plt_section_sp)2674return 0;26752676Section *symtab = section_list->FindSectionByID(symtab_id).get();2677if (!symtab)2678return 0;26792680// sh_link points to associated string table.2681Section *strtab = section_list->FindSectionByID(sym_hdr->sh_link).get();2682if (!strtab)2683return 0;26842685DataExtractor rel_data;2686if (!ReadSectionData(rel_section, rel_data))2687return 0;26882689DataExtractor symtab_data;2690if (!ReadSectionData(symtab, symtab_data))2691return 0;26922693DataExtractor strtab_data;2694if (!ReadSectionData(strtab, strtab_data))2695return 0;26962697unsigned rel_type = PLTRelocationType();2698if (!rel_type)2699return 0;27002701return ParsePLTRelocations(symbol_table, start_id, rel_type, &m_header,2702rel_hdr, plt_hdr, sym_hdr, plt_section_sp,2703rel_data, symtab_data, strtab_data);2704}27052706static void ApplyELF64ABS64Relocation(Symtab *symtab, ELFRelocation &rel,2707DataExtractor &debug_data,2708Section *rel_section) {2709Symbol *symbol = symtab->FindSymbolByID(ELFRelocation::RelocSymbol64(rel));2710if (symbol) {2711addr_t value = symbol->GetAddressRef().GetFileAddress();2712DataBufferSP &data_buffer_sp = debug_data.GetSharedDataBuffer();2713// ObjectFileELF creates a WritableDataBuffer in CreateInstance.2714WritableDataBuffer *data_buffer =2715llvm::cast<WritableDataBuffer>(data_buffer_sp.get());2716uint64_t *dst = reinterpret_cast<uint64_t *>(2717data_buffer->GetBytes() + rel_section->GetFileOffset() +2718ELFRelocation::RelocOffset64(rel));2719uint64_t val_offset = value + ELFRelocation::RelocAddend64(rel);2720memcpy(dst, &val_offset, sizeof(uint64_t));2721}2722}27232724static void ApplyELF64ABS32Relocation(Symtab *symtab, ELFRelocation &rel,2725DataExtractor &debug_data,2726Section *rel_section, bool is_signed) {2727Symbol *symbol = symtab->FindSymbolByID(ELFRelocation::RelocSymbol64(rel));2728if (symbol) {2729addr_t value = symbol->GetAddressRef().GetFileAddress();2730value += ELFRelocation::RelocAddend32(rel);2731if ((!is_signed && (value > UINT32_MAX)) ||2732(is_signed &&2733((int64_t)value > INT32_MAX || (int64_t)value < INT32_MIN))) {2734Log *log = GetLog(LLDBLog::Modules);2735LLDB_LOGF(log, "Failed to apply debug info relocations");2736return;2737}2738uint32_t truncated_addr = (value & 0xFFFFFFFF);2739DataBufferSP &data_buffer_sp = debug_data.GetSharedDataBuffer();2740// ObjectFileELF creates a WritableDataBuffer in CreateInstance.2741WritableDataBuffer *data_buffer =2742llvm::cast<WritableDataBuffer>(data_buffer_sp.get());2743uint32_t *dst = reinterpret_cast<uint32_t *>(2744data_buffer->GetBytes() + rel_section->GetFileOffset() +2745ELFRelocation::RelocOffset32(rel));2746memcpy(dst, &truncated_addr, sizeof(uint32_t));2747}2748}27492750static void ApplyELF32ABS32RelRelocation(Symtab *symtab, ELFRelocation &rel,2751DataExtractor &debug_data,2752Section *rel_section) {2753Log *log = GetLog(LLDBLog::Modules);2754Symbol *symbol = symtab->FindSymbolByID(ELFRelocation::RelocSymbol32(rel));2755if (symbol) {2756addr_t value = symbol->GetAddressRef().GetFileAddress();2757if (value == LLDB_INVALID_ADDRESS) {2758const char *name = symbol->GetName().GetCString();2759LLDB_LOGF(log, "Debug info symbol invalid: %s", name);2760return;2761}2762assert(llvm::isUInt<32>(value) && "Valid addresses are 32-bit");2763DataBufferSP &data_buffer_sp = debug_data.GetSharedDataBuffer();2764// ObjectFileELF creates a WritableDataBuffer in CreateInstance.2765WritableDataBuffer *data_buffer =2766llvm::cast<WritableDataBuffer>(data_buffer_sp.get());2767uint8_t *dst = data_buffer->GetBytes() + rel_section->GetFileOffset() +2768ELFRelocation::RelocOffset32(rel);2769// Implicit addend is stored inline as a signed value.2770int32_t addend;2771memcpy(&addend, dst, sizeof(int32_t));2772// The sum must be positive. This extra check prevents UB from overflow in2773// the actual range check below.2774if (addend < 0 && static_cast<uint32_t>(-addend) > value) {2775LLDB_LOGF(log, "Debug info relocation overflow: 0x%" PRIx64,2776static_cast<int64_t>(value) + addend);2777return;2778}2779if (!llvm::isUInt<32>(value + addend)) {2780LLDB_LOGF(log, "Debug info relocation out of range: 0x%" PRIx64, value);2781return;2782}2783uint32_t addr = value + addend;2784memcpy(dst, &addr, sizeof(uint32_t));2785}2786}27872788unsigned ObjectFileELF::ApplyRelocations(2789Symtab *symtab, const ELFHeader *hdr, const ELFSectionHeader *rel_hdr,2790const ELFSectionHeader *symtab_hdr, const ELFSectionHeader *debug_hdr,2791DataExtractor &rel_data, DataExtractor &symtab_data,2792DataExtractor &debug_data, Section *rel_section) {2793ELFRelocation rel(rel_hdr->sh_type);2794lldb::addr_t offset = 0;2795const unsigned num_relocations = rel_hdr->sh_size / rel_hdr->sh_entsize;2796typedef unsigned (*reloc_info_fn)(const ELFRelocation &rel);2797reloc_info_fn reloc_type;2798reloc_info_fn reloc_symbol;27992800if (hdr->Is32Bit()) {2801reloc_type = ELFRelocation::RelocType32;2802reloc_symbol = ELFRelocation::RelocSymbol32;2803} else {2804reloc_type = ELFRelocation::RelocType64;2805reloc_symbol = ELFRelocation::RelocSymbol64;2806}28072808for (unsigned i = 0; i < num_relocations; ++i) {2809if (!rel.Parse(rel_data, &offset)) {2810GetModule()->ReportError(".rel{0}[{1:d}] failed to parse relocation",2811rel_section->GetName().AsCString(), i);2812break;2813}2814Symbol *symbol = nullptr;28152816if (hdr->Is32Bit()) {2817switch (hdr->e_machine) {2818case llvm::ELF::EM_ARM:2819switch (reloc_type(rel)) {2820case R_ARM_ABS32:2821ApplyELF32ABS32RelRelocation(symtab, rel, debug_data, rel_section);2822break;2823case R_ARM_REL32:2824GetModule()->ReportError("unsupported AArch32 relocation:"2825" .rel{0}[{1}], type {2}",2826rel_section->GetName().AsCString(), i,2827reloc_type(rel));2828break;2829default:2830assert(false && "unexpected relocation type");2831}2832break;2833case llvm::ELF::EM_386:2834switch (reloc_type(rel)) {2835case R_386_32:2836symbol = symtab->FindSymbolByID(reloc_symbol(rel));2837if (symbol) {2838addr_t f_offset =2839rel_section->GetFileOffset() + ELFRelocation::RelocOffset32(rel);2840DataBufferSP &data_buffer_sp = debug_data.GetSharedDataBuffer();2841// ObjectFileELF creates a WritableDataBuffer in CreateInstance.2842WritableDataBuffer *data_buffer =2843llvm::cast<WritableDataBuffer>(data_buffer_sp.get());2844uint32_t *dst = reinterpret_cast<uint32_t *>(2845data_buffer->GetBytes() + f_offset);28462847addr_t value = symbol->GetAddressRef().GetFileAddress();2848if (rel.IsRela()) {2849value += ELFRelocation::RelocAddend32(rel);2850} else {2851value += *dst;2852}2853*dst = value;2854} else {2855GetModule()->ReportError(".rel{0}[{1}] unknown symbol id: {2:d}",2856rel_section->GetName().AsCString(), i,2857reloc_symbol(rel));2858}2859break;2860case R_386_NONE:2861case R_386_PC32:2862GetModule()->ReportError("unsupported i386 relocation:"2863" .rel{0}[{1}], type {2}",2864rel_section->GetName().AsCString(), i,2865reloc_type(rel));2866break;2867default:2868assert(false && "unexpected relocation type");2869break;2870}2871break;2872default:2873GetModule()->ReportError("unsupported 32-bit ELF machine arch: {0}", hdr->e_machine);2874break;2875}2876} else {2877switch (hdr->e_machine) {2878case llvm::ELF::EM_AARCH64:2879switch (reloc_type(rel)) {2880case R_AARCH64_ABS64:2881ApplyELF64ABS64Relocation(symtab, rel, debug_data, rel_section);2882break;2883case R_AARCH64_ABS32:2884ApplyELF64ABS32Relocation(symtab, rel, debug_data, rel_section, true);2885break;2886default:2887assert(false && "unexpected relocation type");2888}2889break;2890case llvm::ELF::EM_LOONGARCH:2891switch (reloc_type(rel)) {2892case R_LARCH_64:2893ApplyELF64ABS64Relocation(symtab, rel, debug_data, rel_section);2894break;2895case R_LARCH_32:2896ApplyELF64ABS32Relocation(symtab, rel, debug_data, rel_section, true);2897break;2898default:2899assert(false && "unexpected relocation type");2900}2901break;2902case llvm::ELF::EM_X86_64:2903switch (reloc_type(rel)) {2904case R_X86_64_64:2905ApplyELF64ABS64Relocation(symtab, rel, debug_data, rel_section);2906break;2907case R_X86_64_32:2908ApplyELF64ABS32Relocation(symtab, rel, debug_data, rel_section,2909false);2910break;2911case R_X86_64_32S:2912ApplyELF64ABS32Relocation(symtab, rel, debug_data, rel_section, true);2913break;2914case R_X86_64_PC32:2915default:2916assert(false && "unexpected relocation type");2917}2918break;2919default:2920GetModule()->ReportError("unsupported 64-bit ELF machine arch: {0}", hdr->e_machine);2921break;2922}2923}2924}29252926return 0;2927}29282929unsigned ObjectFileELF::RelocateDebugSections(const ELFSectionHeader *rel_hdr,2930user_id_t rel_id,2931lldb_private::Symtab *thetab) {2932assert(rel_hdr->sh_type == SHT_RELA || rel_hdr->sh_type == SHT_REL);29332934// Parse in the section list if needed.2935SectionList *section_list = GetSectionList();2936if (!section_list)2937return 0;29382939user_id_t symtab_id = rel_hdr->sh_link;2940user_id_t debug_id = rel_hdr->sh_info;29412942const ELFSectionHeader *symtab_hdr = GetSectionHeaderByIndex(symtab_id);2943if (!symtab_hdr)2944return 0;29452946const ELFSectionHeader *debug_hdr = GetSectionHeaderByIndex(debug_id);2947if (!debug_hdr)2948return 0;29492950Section *rel = section_list->FindSectionByID(rel_id).get();2951if (!rel)2952return 0;29532954Section *symtab = section_list->FindSectionByID(symtab_id).get();2955if (!symtab)2956return 0;29572958Section *debug = section_list->FindSectionByID(debug_id).get();2959if (!debug)2960return 0;29612962DataExtractor rel_data;2963DataExtractor symtab_data;2964DataExtractor debug_data;29652966if (GetData(rel->GetFileOffset(), rel->GetFileSize(), rel_data) &&2967GetData(symtab->GetFileOffset(), symtab->GetFileSize(), symtab_data) &&2968GetData(debug->GetFileOffset(), debug->GetFileSize(), debug_data)) {2969ApplyRelocations(thetab, &m_header, rel_hdr, symtab_hdr, debug_hdr,2970rel_data, symtab_data, debug_data, debug);2971}29722973return 0;2974}29752976void ObjectFileELF::ParseSymtab(Symtab &lldb_symtab) {2977ModuleSP module_sp(GetModule());2978if (!module_sp)2979return;29802981Progress progress("Parsing symbol table",2982m_file.GetFilename().AsCString("<Unknown>"));2983ElapsedTime elapsed(module_sp->GetSymtabParseTime());29842985// We always want to use the main object file so we (hopefully) only have one2986// cached copy of our symtab, dynamic sections, etc.2987ObjectFile *module_obj_file = module_sp->GetObjectFile();2988if (module_obj_file && module_obj_file != this)2989return module_obj_file->ParseSymtab(lldb_symtab);29902991SectionList *section_list = module_sp->GetSectionList();2992if (!section_list)2993return;29942995uint64_t symbol_id = 0;29962997// Sharable objects and dynamic executables usually have 2 distinct symbol2998// tables, one named ".symtab", and the other ".dynsym". The dynsym is a2999// smaller version of the symtab that only contains global symbols. The3000// information found in the dynsym is therefore also found in the symtab,3001// while the reverse is not necessarily true.3002Section *symtab =3003section_list->FindSectionByType(eSectionTypeELFSymbolTable, true).get();3004if (symtab) {3005auto [num_symbols, address_class_map] =3006ParseSymbolTable(&lldb_symtab, symbol_id, symtab);3007m_address_class_map.merge(address_class_map);3008symbol_id += num_symbols;3009}30103011// The symtab section is non-allocable and can be stripped, while the3012// .dynsym section which should always be always be there. To support the3013// minidebuginfo case we parse .dynsym when there's a .gnu_debuginfo3014// section, nomatter if .symtab was already parsed or not. This is because3015// minidebuginfo normally removes the .symtab symbols which have their3016// matching .dynsym counterparts.3017if (!symtab ||3018GetSectionList()->FindSectionByName(ConstString(".gnu_debugdata"))) {3019Section *dynsym =3020section_list->FindSectionByType(eSectionTypeELFDynamicSymbols, true)3021.get();3022if (dynsym) {3023auto [num_symbols, address_class_map] =3024ParseSymbolTable(&lldb_symtab, symbol_id, dynsym);3025symbol_id += num_symbols;3026m_address_class_map.merge(address_class_map);3027}3028}30293030// DT_JMPREL3031// If present, this entry's d_ptr member holds the address of3032// relocation3033// entries associated solely with the procedure linkage table.3034// Separating3035// these relocation entries lets the dynamic linker ignore them during3036// process initialization, if lazy binding is enabled. If this entry is3037// present, the related entries of types DT_PLTRELSZ and DT_PLTREL must3038// also be present.3039const ELFDynamic *symbol = FindDynamicSymbol(DT_JMPREL);3040if (symbol) {3041// Synthesize trampoline symbols to help navigate the PLT.3042addr_t addr = symbol->d_ptr;3043Section *reloc_section =3044section_list->FindSectionContainingFileAddress(addr).get();3045if (reloc_section) {3046user_id_t reloc_id = reloc_section->GetID();3047const ELFSectionHeaderInfo *reloc_header =3048GetSectionHeaderByIndex(reloc_id);3049if (reloc_header)3050ParseTrampolineSymbols(&lldb_symtab, symbol_id, reloc_header, reloc_id);3051}3052}30533054if (DWARFCallFrameInfo *eh_frame =3055GetModule()->GetUnwindTable().GetEHFrameInfo()) {3056ParseUnwindSymbols(&lldb_symtab, eh_frame);3057}30583059// In the event that there's no symbol entry for the entry point we'll3060// artificially create one. We delegate to the symtab object the figuring3061// out of the proper size, this will usually make it span til the next3062// symbol it finds in the section. This means that if there are missing3063// symbols the entry point might span beyond its function definition.3064// We're fine with this as it doesn't make it worse than not having a3065// symbol entry at all.3066if (CalculateType() == eTypeExecutable) {3067ArchSpec arch = GetArchitecture();3068auto entry_point_addr = GetEntryPointAddress();3069bool is_valid_entry_point =3070entry_point_addr.IsValid() && entry_point_addr.IsSectionOffset();3071addr_t entry_point_file_addr = entry_point_addr.GetFileAddress();3072if (is_valid_entry_point && !lldb_symtab.FindSymbolContainingFileAddress(3073entry_point_file_addr)) {3074uint64_t symbol_id = lldb_symtab.GetNumSymbols();3075// Don't set the name for any synthetic symbols, the Symbol3076// object will generate one if needed when the name is accessed3077// via accessors.3078SectionSP section_sp = entry_point_addr.GetSection();3079Symbol symbol(3080/*symID=*/symbol_id,3081/*name=*/llvm::StringRef(), // Name will be auto generated.3082/*type=*/eSymbolTypeCode,3083/*external=*/true,3084/*is_debug=*/false,3085/*is_trampoline=*/false,3086/*is_artificial=*/true,3087/*section_sp=*/section_sp,3088/*offset=*/0,3089/*size=*/0, // FDE can span multiple symbols so don't use its size.3090/*size_is_valid=*/false,3091/*contains_linker_annotations=*/false,3092/*flags=*/0);3093// When the entry point is arm thumb we need to explicitly set its3094// class address to reflect that. This is important because expression3095// evaluation relies on correctly setting a breakpoint at this3096// address.3097if (arch.GetMachine() == llvm::Triple::arm &&3098(entry_point_file_addr & 1)) {3099symbol.GetAddressRef().SetOffset(entry_point_addr.GetOffset() ^ 1);3100m_address_class_map[entry_point_file_addr ^ 1] =3101AddressClass::eCodeAlternateISA;3102} else {3103m_address_class_map[entry_point_file_addr] = AddressClass::eCode;3104}3105lldb_symtab.AddSymbol(symbol);3106}3107}3108}31093110void ObjectFileELF::RelocateSection(lldb_private::Section *section)3111{3112static const char *debug_prefix = ".debug";31133114// Set relocated bit so we stop getting called, regardless of whether we3115// actually relocate.3116section->SetIsRelocated(true);31173118// We only relocate in ELF relocatable files3119if (CalculateType() != eTypeObjectFile)3120return;31213122const char *section_name = section->GetName().GetCString();3123// Can't relocate that which can't be named3124if (section_name == nullptr)3125return;31263127// We don't relocate non-debug sections at the moment3128if (strncmp(section_name, debug_prefix, strlen(debug_prefix)))3129return;31303131// Relocation section names to look for3132std::string needle = std::string(".rel") + section_name;3133std::string needlea = std::string(".rela") + section_name;31343135for (SectionHeaderCollIter I = m_section_headers.begin();3136I != m_section_headers.end(); ++I) {3137if (I->sh_type == SHT_RELA || I->sh_type == SHT_REL) {3138const char *hay_name = I->section_name.GetCString();3139if (hay_name == nullptr)3140continue;3141if (needle == hay_name || needlea == hay_name) {3142const ELFSectionHeader &reloc_header = *I;3143user_id_t reloc_id = SectionIndex(I);3144RelocateDebugSections(&reloc_header, reloc_id, GetSymtab());3145break;3146}3147}3148}3149}31503151void ObjectFileELF::ParseUnwindSymbols(Symtab *symbol_table,3152DWARFCallFrameInfo *eh_frame) {3153SectionList *section_list = GetSectionList();3154if (!section_list)3155return;31563157// First we save the new symbols into a separate list and add them to the3158// symbol table after we collected all symbols we want to add. This is3159// neccessary because adding a new symbol invalidates the internal index of3160// the symtab what causing the next lookup to be slow because it have to3161// recalculate the index first.3162std::vector<Symbol> new_symbols;31633164size_t num_symbols = symbol_table->GetNumSymbols();3165uint64_t last_symbol_id =3166num_symbols ? symbol_table->SymbolAtIndex(num_symbols - 1)->GetID() : 0;3167eh_frame->ForEachFDEEntries([&](lldb::addr_t file_addr, uint32_t size,3168dw_offset_t) {3169Symbol *symbol = symbol_table->FindSymbolAtFileAddress(file_addr);3170if (symbol) {3171if (!symbol->GetByteSizeIsValid()) {3172symbol->SetByteSize(size);3173symbol->SetSizeIsSynthesized(true);3174}3175} else {3176SectionSP section_sp =3177section_list->FindSectionContainingFileAddress(file_addr);3178if (section_sp) {3179addr_t offset = file_addr - section_sp->GetFileAddress();3180uint64_t symbol_id = ++last_symbol_id;3181// Don't set the name for any synthetic symbols, the Symbol3182// object will generate one if needed when the name is accessed3183// via accessors.3184Symbol eh_symbol(3185/*symID=*/symbol_id,3186/*name=*/llvm::StringRef(), // Name will be auto generated.3187/*type=*/eSymbolTypeCode,3188/*external=*/true,3189/*is_debug=*/false,3190/*is_trampoline=*/false,3191/*is_artificial=*/true,3192/*section_sp=*/section_sp,3193/*offset=*/offset,3194/*size=*/0, // FDE can span multiple symbols so don't use its size.3195/*size_is_valid=*/false,3196/*contains_linker_annotations=*/false,3197/*flags=*/0);3198new_symbols.push_back(eh_symbol);3199}3200}3201return true;3202});32033204for (const Symbol &s : new_symbols)3205symbol_table->AddSymbol(s);3206}32073208bool ObjectFileELF::IsStripped() {3209// TODO: determine this for ELF3210return false;3211}32123213//===----------------------------------------------------------------------===//3214// Dump3215//3216// Dump the specifics of the runtime file container (such as any headers3217// segments, sections, etc).3218void ObjectFileELF::Dump(Stream *s) {3219ModuleSP module_sp(GetModule());3220if (!module_sp) {3221return;3222}32233224std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());3225s->Printf("%p: ", static_cast<void *>(this));3226s->Indent();3227s->PutCString("ObjectFileELF");32283229ArchSpec header_arch = GetArchitecture();32303231*s << ", file = '" << m_file3232<< "', arch = " << header_arch.GetArchitectureName() << "\n";32333234DumpELFHeader(s, m_header);3235s->EOL();3236DumpELFProgramHeaders(s);3237s->EOL();3238DumpELFSectionHeaders(s);3239s->EOL();3240SectionList *section_list = GetSectionList();3241if (section_list)3242section_list->Dump(s->AsRawOstream(), s->GetIndentLevel(), nullptr, true,3243UINT32_MAX);3244Symtab *symtab = GetSymtab();3245if (symtab)3246symtab->Dump(s, nullptr, eSortOrderNone);3247s->EOL();3248DumpDependentModules(s);3249s->EOL();3250}32513252// DumpELFHeader3253//3254// Dump the ELF header to the specified output stream3255void ObjectFileELF::DumpELFHeader(Stream *s, const ELFHeader &header) {3256s->PutCString("ELF Header\n");3257s->Printf("e_ident[EI_MAG0 ] = 0x%2.2x\n", header.e_ident[EI_MAG0]);3258s->Printf("e_ident[EI_MAG1 ] = 0x%2.2x '%c'\n", header.e_ident[EI_MAG1],3259header.e_ident[EI_MAG1]);3260s->Printf("e_ident[EI_MAG2 ] = 0x%2.2x '%c'\n", header.e_ident[EI_MAG2],3261header.e_ident[EI_MAG2]);3262s->Printf("e_ident[EI_MAG3 ] = 0x%2.2x '%c'\n", header.e_ident[EI_MAG3],3263header.e_ident[EI_MAG3]);32643265s->Printf("e_ident[EI_CLASS ] = 0x%2.2x\n", header.e_ident[EI_CLASS]);3266s->Printf("e_ident[EI_DATA ] = 0x%2.2x ", header.e_ident[EI_DATA]);3267DumpELFHeader_e_ident_EI_DATA(s, header.e_ident[EI_DATA]);3268s->Printf("\ne_ident[EI_VERSION] = 0x%2.2x\n", header.e_ident[EI_VERSION]);3269s->Printf("e_ident[EI_PAD ] = 0x%2.2x\n", header.e_ident[EI_PAD]);32703271s->Printf("e_type = 0x%4.4x ", header.e_type);3272DumpELFHeader_e_type(s, header.e_type);3273s->Printf("\ne_machine = 0x%4.4x\n", header.e_machine);3274s->Printf("e_version = 0x%8.8x\n", header.e_version);3275s->Printf("e_entry = 0x%8.8" PRIx64 "\n", header.e_entry);3276s->Printf("e_phoff = 0x%8.8" PRIx64 "\n", header.e_phoff);3277s->Printf("e_shoff = 0x%8.8" PRIx64 "\n", header.e_shoff);3278s->Printf("e_flags = 0x%8.8x\n", header.e_flags);3279s->Printf("e_ehsize = 0x%4.4x\n", header.e_ehsize);3280s->Printf("e_phentsize = 0x%4.4x\n", header.e_phentsize);3281s->Printf("e_phnum = 0x%8.8x\n", header.e_phnum);3282s->Printf("e_shentsize = 0x%4.4x\n", header.e_shentsize);3283s->Printf("e_shnum = 0x%8.8x\n", header.e_shnum);3284s->Printf("e_shstrndx = 0x%8.8x\n", header.e_shstrndx);3285}32863287// DumpELFHeader_e_type3288//3289// Dump an token value for the ELF header member e_type3290void ObjectFileELF::DumpELFHeader_e_type(Stream *s, elf_half e_type) {3291switch (e_type) {3292case ET_NONE:3293*s << "ET_NONE";3294break;3295case ET_REL:3296*s << "ET_REL";3297break;3298case ET_EXEC:3299*s << "ET_EXEC";3300break;3301case ET_DYN:3302*s << "ET_DYN";3303break;3304case ET_CORE:3305*s << "ET_CORE";3306break;3307default:3308break;3309}3310}33113312// DumpELFHeader_e_ident_EI_DATA3313//3314// Dump an token value for the ELF header member e_ident[EI_DATA]3315void ObjectFileELF::DumpELFHeader_e_ident_EI_DATA(Stream *s,3316unsigned char ei_data) {3317switch (ei_data) {3318case ELFDATANONE:3319*s << "ELFDATANONE";3320break;3321case ELFDATA2LSB:3322*s << "ELFDATA2LSB - Little Endian";3323break;3324case ELFDATA2MSB:3325*s << "ELFDATA2MSB - Big Endian";3326break;3327default:3328break;3329}3330}33313332// DumpELFProgramHeader3333//3334// Dump a single ELF program header to the specified output stream3335void ObjectFileELF::DumpELFProgramHeader(Stream *s,3336const ELFProgramHeader &ph) {3337DumpELFProgramHeader_p_type(s, ph.p_type);3338s->Printf(" %8.8" PRIx64 " %8.8" PRIx64 " %8.8" PRIx64, ph.p_offset,3339ph.p_vaddr, ph.p_paddr);3340s->Printf(" %8.8" PRIx64 " %8.8" PRIx64 " %8.8x (", ph.p_filesz, ph.p_memsz,3341ph.p_flags);33423343DumpELFProgramHeader_p_flags(s, ph.p_flags);3344s->Printf(") %8.8" PRIx64, ph.p_align);3345}33463347// DumpELFProgramHeader_p_type3348//3349// Dump an token value for the ELF program header member p_type which describes3350// the type of the program header3351void ObjectFileELF::DumpELFProgramHeader_p_type(Stream *s, elf_word p_type) {3352const int kStrWidth = 15;3353switch (p_type) {3354CASE_AND_STREAM(s, PT_NULL, kStrWidth);3355CASE_AND_STREAM(s, PT_LOAD, kStrWidth);3356CASE_AND_STREAM(s, PT_DYNAMIC, kStrWidth);3357CASE_AND_STREAM(s, PT_INTERP, kStrWidth);3358CASE_AND_STREAM(s, PT_NOTE, kStrWidth);3359CASE_AND_STREAM(s, PT_SHLIB, kStrWidth);3360CASE_AND_STREAM(s, PT_PHDR, kStrWidth);3361CASE_AND_STREAM(s, PT_TLS, kStrWidth);3362CASE_AND_STREAM(s, PT_GNU_EH_FRAME, kStrWidth);3363default:3364s->Printf("0x%8.8x%*s", p_type, kStrWidth - 10, "");3365break;3366}3367}33683369// DumpELFProgramHeader_p_flags3370//3371// Dump an token value for the ELF program header member p_flags3372void ObjectFileELF::DumpELFProgramHeader_p_flags(Stream *s, elf_word p_flags) {3373*s << ((p_flags & PF_X) ? "PF_X" : " ")3374<< (((p_flags & PF_X) && (p_flags & PF_W)) ? '+' : ' ')3375<< ((p_flags & PF_W) ? "PF_W" : " ")3376<< (((p_flags & PF_W) && (p_flags & PF_R)) ? '+' : ' ')3377<< ((p_flags & PF_R) ? "PF_R" : " ");3378}33793380// DumpELFProgramHeaders3381//3382// Dump all of the ELF program header to the specified output stream3383void ObjectFileELF::DumpELFProgramHeaders(Stream *s) {3384if (!ParseProgramHeaders())3385return;33863387s->PutCString("Program Headers\n");3388s->PutCString("IDX p_type p_offset p_vaddr p_paddr "3389"p_filesz p_memsz p_flags p_align\n");3390s->PutCString("==== --------------- -------- -------- -------- "3391"-------- -------- ------------------------- --------\n");33923393for (const auto &H : llvm::enumerate(m_program_headers)) {3394s->Format("[{0,2}] ", H.index());3395ObjectFileELF::DumpELFProgramHeader(s, H.value());3396s->EOL();3397}3398}33993400// DumpELFSectionHeader3401//3402// Dump a single ELF section header to the specified output stream3403void ObjectFileELF::DumpELFSectionHeader(Stream *s,3404const ELFSectionHeaderInfo &sh) {3405s->Printf("%8.8x ", sh.sh_name);3406DumpELFSectionHeader_sh_type(s, sh.sh_type);3407s->Printf(" %8.8" PRIx64 " (", sh.sh_flags);3408DumpELFSectionHeader_sh_flags(s, sh.sh_flags);3409s->Printf(") %8.8" PRIx64 " %8.8" PRIx64 " %8.8" PRIx64, sh.sh_addr,3410sh.sh_offset, sh.sh_size);3411s->Printf(" %8.8x %8.8x", sh.sh_link, sh.sh_info);3412s->Printf(" %8.8" PRIx64 " %8.8" PRIx64, sh.sh_addralign, sh.sh_entsize);3413}34143415// DumpELFSectionHeader_sh_type3416//3417// Dump an token value for the ELF section header member sh_type which3418// describes the type of the section3419void ObjectFileELF::DumpELFSectionHeader_sh_type(Stream *s, elf_word sh_type) {3420const int kStrWidth = 12;3421switch (sh_type) {3422CASE_AND_STREAM(s, SHT_NULL, kStrWidth);3423CASE_AND_STREAM(s, SHT_PROGBITS, kStrWidth);3424CASE_AND_STREAM(s, SHT_SYMTAB, kStrWidth);3425CASE_AND_STREAM(s, SHT_STRTAB, kStrWidth);3426CASE_AND_STREAM(s, SHT_RELA, kStrWidth);3427CASE_AND_STREAM(s, SHT_HASH, kStrWidth);3428CASE_AND_STREAM(s, SHT_DYNAMIC, kStrWidth);3429CASE_AND_STREAM(s, SHT_NOTE, kStrWidth);3430CASE_AND_STREAM(s, SHT_NOBITS, kStrWidth);3431CASE_AND_STREAM(s, SHT_REL, kStrWidth);3432CASE_AND_STREAM(s, SHT_SHLIB, kStrWidth);3433CASE_AND_STREAM(s, SHT_DYNSYM, kStrWidth);3434CASE_AND_STREAM(s, SHT_LOPROC, kStrWidth);3435CASE_AND_STREAM(s, SHT_HIPROC, kStrWidth);3436CASE_AND_STREAM(s, SHT_LOUSER, kStrWidth);3437CASE_AND_STREAM(s, SHT_HIUSER, kStrWidth);3438default:3439s->Printf("0x%8.8x%*s", sh_type, kStrWidth - 10, "");3440break;3441}3442}34433444// DumpELFSectionHeader_sh_flags3445//3446// Dump an token value for the ELF section header member sh_flags3447void ObjectFileELF::DumpELFSectionHeader_sh_flags(Stream *s,3448elf_xword sh_flags) {3449*s << ((sh_flags & SHF_WRITE) ? "WRITE" : " ")3450<< (((sh_flags & SHF_WRITE) && (sh_flags & SHF_ALLOC)) ? '+' : ' ')3451<< ((sh_flags & SHF_ALLOC) ? "ALLOC" : " ")3452<< (((sh_flags & SHF_ALLOC) && (sh_flags & SHF_EXECINSTR)) ? '+' : ' ')3453<< ((sh_flags & SHF_EXECINSTR) ? "EXECINSTR" : " ");3454}34553456// DumpELFSectionHeaders3457//3458// Dump all of the ELF section header to the specified output stream3459void ObjectFileELF::DumpELFSectionHeaders(Stream *s) {3460if (!ParseSectionHeaders())3461return;34623463s->PutCString("Section Headers\n");3464s->PutCString("IDX name type flags "3465"addr offset size link info addralgn "3466"entsize Name\n");3467s->PutCString("==== -------- ------------ -------------------------------- "3468"-------- -------- -------- -------- -------- -------- "3469"-------- ====================\n");34703471uint32_t idx = 0;3472for (SectionHeaderCollConstIter I = m_section_headers.begin();3473I != m_section_headers.end(); ++I, ++idx) {3474s->Printf("[%2u] ", idx);3475ObjectFileELF::DumpELFSectionHeader(s, *I);3476const char *section_name = I->section_name.AsCString("");3477if (section_name)3478*s << ' ' << section_name << "\n";3479}3480}34813482void ObjectFileELF::DumpDependentModules(lldb_private::Stream *s) {3483size_t num_modules = ParseDependentModules();34843485if (num_modules > 0) {3486s->PutCString("Dependent Modules:\n");3487for (unsigned i = 0; i < num_modules; ++i) {3488const FileSpec &spec = m_filespec_up->GetFileSpecAtIndex(i);3489s->Printf(" %s\n", spec.GetFilename().GetCString());3490}3491}3492}34933494ArchSpec ObjectFileELF::GetArchitecture() {3495if (!ParseHeader())3496return ArchSpec();34973498if (m_section_headers.empty()) {3499// Allow elf notes to be parsed which may affect the detected architecture.3500ParseSectionHeaders();3501}35023503if (CalculateType() == eTypeCoreFile &&3504!m_arch_spec.TripleOSWasSpecified()) {3505// Core files don't have section headers yet they have PT_NOTE program3506// headers that might shed more light on the architecture3507for (const elf::ELFProgramHeader &H : ProgramHeaders()) {3508if (H.p_type != PT_NOTE || H.p_offset == 0 || H.p_filesz == 0)3509continue;3510DataExtractor data;3511if (data.SetData(m_data, H.p_offset, H.p_filesz) == H.p_filesz) {3512UUID uuid;3513RefineModuleDetailsFromNote(data, m_arch_spec, uuid);3514}3515}3516}3517return m_arch_spec;3518}35193520ObjectFile::Type ObjectFileELF::CalculateType() {3521switch (m_header.e_type) {3522case llvm::ELF::ET_NONE:3523// 0 - No file type3524return eTypeUnknown;35253526case llvm::ELF::ET_REL:3527// 1 - Relocatable file3528return eTypeObjectFile;35293530case llvm::ELF::ET_EXEC:3531// 2 - Executable file3532return eTypeExecutable;35333534case llvm::ELF::ET_DYN:3535// 3 - Shared object file3536return eTypeSharedLibrary;35373538case ET_CORE:3539// 4 - Core file3540return eTypeCoreFile;35413542default:3543break;3544}3545return eTypeUnknown;3546}35473548ObjectFile::Strata ObjectFileELF::CalculateStrata() {3549switch (m_header.e_type) {3550case llvm::ELF::ET_NONE:3551// 0 - No file type3552return eStrataUnknown;35533554case llvm::ELF::ET_REL:3555// 1 - Relocatable file3556return eStrataUnknown;35573558case llvm::ELF::ET_EXEC:3559// 2 - Executable file3560{3561SectionList *section_list = GetSectionList();3562if (section_list) {3563static ConstString loader_section_name(".interp");3564SectionSP loader_section =3565section_list->FindSectionByName(loader_section_name);3566if (loader_section) {3567char buffer[256];3568size_t read_size =3569ReadSectionData(loader_section.get(), 0, buffer, sizeof(buffer));35703571// We compare the content of .interp section3572// It will contains \0 when counting read_size, so the size needs to3573// decrease by one3574llvm::StringRef loader_name(buffer, read_size - 1);3575llvm::StringRef freebsd_kernel_loader_name("/red/herring");3576if (loader_name == freebsd_kernel_loader_name)3577return eStrataKernel;3578}3579}3580return eStrataUser;3581}35823583case llvm::ELF::ET_DYN:3584// 3 - Shared object file3585// TODO: is there any way to detect that an shared library is a kernel3586// related executable by inspecting the program headers, section headers,3587// symbols, or any other flag bits???3588return eStrataUnknown;35893590case ET_CORE:3591// 4 - Core file3592// TODO: is there any way to detect that an core file is a kernel3593// related executable by inspecting the program headers, section headers,3594// symbols, or any other flag bits???3595return eStrataUnknown;35963597default:3598break;3599}3600return eStrataUnknown;3601}36023603size_t ObjectFileELF::ReadSectionData(Section *section,3604lldb::offset_t section_offset, void *dst,3605size_t dst_len) {3606// If some other objectfile owns this data, pass this to them.3607if (section->GetObjectFile() != this)3608return section->GetObjectFile()->ReadSectionData(section, section_offset,3609dst, dst_len);36103611if (!section->Test(SHF_COMPRESSED))3612return ObjectFile::ReadSectionData(section, section_offset, dst, dst_len);36133614// For compressed sections we need to read to full data to be able to3615// decompress.3616DataExtractor data;3617ReadSectionData(section, data);3618return data.CopyData(section_offset, dst_len, dst);3619}36203621size_t ObjectFileELF::ReadSectionData(Section *section,3622DataExtractor §ion_data) {3623// If some other objectfile owns this data, pass this to them.3624if (section->GetObjectFile() != this)3625return section->GetObjectFile()->ReadSectionData(section, section_data);36263627size_t result = ObjectFile::ReadSectionData(section, section_data);3628if (result == 0 || !(section->Get() & llvm::ELF::SHF_COMPRESSED))3629return result;36303631auto Decompressor = llvm::object::Decompressor::create(3632section->GetName().GetStringRef(),3633{reinterpret_cast<const char *>(section_data.GetDataStart()),3634size_t(section_data.GetByteSize())},3635GetByteOrder() == eByteOrderLittle, GetAddressByteSize() == 8);3636if (!Decompressor) {3637GetModule()->ReportWarning(3638"Unable to initialize decompressor for section '{0}': {1}",3639section->GetName().GetCString(),3640llvm::toString(Decompressor.takeError()).c_str());3641section_data.Clear();3642return 0;3643}36443645auto buffer_sp =3646std::make_shared<DataBufferHeap>(Decompressor->getDecompressedSize(), 0);3647if (auto error = Decompressor->decompress(3648{buffer_sp->GetBytes(), size_t(buffer_sp->GetByteSize())})) {3649GetModule()->ReportWarning("Decompression of section '{0}' failed: {1}",3650section->GetName().GetCString(),3651llvm::toString(std::move(error)).c_str());3652section_data.Clear();3653return 0;3654}36553656section_data.SetData(buffer_sp);3657return buffer_sp->GetByteSize();3658}36593660llvm::ArrayRef<ELFProgramHeader> ObjectFileELF::ProgramHeaders() {3661ParseProgramHeaders();3662return m_program_headers;3663}36643665DataExtractor ObjectFileELF::GetSegmentData(const ELFProgramHeader &H) {3666return DataExtractor(m_data, H.p_offset, H.p_filesz);3667}36683669bool ObjectFileELF::AnySegmentHasPhysicalAddress() {3670for (const ELFProgramHeader &H : ProgramHeaders()) {3671if (H.p_paddr != 0)3672return true;3673}3674return false;3675}36763677std::vector<ObjectFile::LoadableData>3678ObjectFileELF::GetLoadableData(Target &target) {3679// Create a list of loadable data from loadable segments, using physical3680// addresses if they aren't all null3681std::vector<LoadableData> loadables;3682bool should_use_paddr = AnySegmentHasPhysicalAddress();3683for (const ELFProgramHeader &H : ProgramHeaders()) {3684LoadableData loadable;3685if (H.p_type != llvm::ELF::PT_LOAD)3686continue;3687loadable.Dest = should_use_paddr ? H.p_paddr : H.p_vaddr;3688if (loadable.Dest == LLDB_INVALID_ADDRESS)3689continue;3690if (H.p_filesz == 0)3691continue;3692auto segment_data = GetSegmentData(H);3693loadable.Contents = llvm::ArrayRef<uint8_t>(segment_data.GetDataStart(),3694segment_data.GetByteSize());3695loadables.push_back(loadable);3696}3697return loadables;3698}36993700lldb::WritableDataBufferSP3701ObjectFileELF::MapFileDataWritable(const FileSpec &file, uint64_t Size,3702uint64_t Offset) {3703return FileSystem::Instance().CreateWritableDataBuffer(file.GetPath(), Size,3704Offset);3705}370637073708