#ifndef LLDB_SOURCE_PLUGINS_OBJECTFILE_ELF_OBJECTFILEELF_H
#define LLDB_SOURCE_PLUGINS_OBJECTFILE_ELF_OBJECTFILEELF_H
#include <cstdint>
#include <optional>
#include <vector>
#ifdef MS_DEBUGGER
#include "lldb/Core/ModuleSpec.h"
#endif
#include "lldb/Symbol/ObjectFile.h"
#include "lldb/Utility/ArchSpec.h"
#include "lldb/Utility/FileSpec.h"
#include "lldb/Utility/UUID.h"
#include "lldb/lldb-private.h"
#include "ELFHeader.h"
struct ELFNote {
elf::elf_word n_namesz = 0;
elf::elf_word n_descsz = 0;
elf::elf_word n_type = 0;
std::string n_name;
ELFNote() = default;
bool Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset);
size_t GetByteSize() const {
return 12 + llvm::alignTo(n_namesz, 4) + llvm::alignTo(n_descsz, 4);
}
};
class ObjectFileELF : public lldb_private::ObjectFile {
public:
static void Initialize();
static void Terminate();
static llvm::StringRef GetPluginNameStatic() { return "elf"; }
static llvm::StringRef GetPluginDescriptionStatic() {
return "ELF object file reader.";
}
static lldb_private::ObjectFile *
CreateInstance(const lldb::ModuleSP &module_sp, lldb::DataBufferSP data_sp,
lldb::offset_t data_offset, const lldb_private::FileSpec *file,
lldb::offset_t file_offset, lldb::offset_t length);
static lldb_private::ObjectFile *CreateMemoryInstance(
const lldb::ModuleSP &module_sp, lldb::WritableDataBufferSP data_sp,
const lldb::ProcessSP &process_sp, lldb::addr_t header_addr);
static size_t GetModuleSpecifications(const lldb_private::FileSpec &file,
lldb::DataBufferSP &data_sp,
lldb::offset_t data_offset,
lldb::offset_t file_offset,
lldb::offset_t length,
lldb_private::ModuleSpecList &specs);
static bool MagicBytesMatch(lldb::DataBufferSP &data_sp, lldb::addr_t offset,
lldb::addr_t length);
llvm::StringRef GetPluginName() override { return GetPluginNameStatic(); }
static char ID;
bool isA(const void *ClassID) const override {
return ClassID == &ID || ObjectFile::isA(ClassID);
}
static bool classof(const ObjectFile *obj) { return obj->isA(&ID); }
bool ParseHeader() override;
bool SetLoadAddress(lldb_private::Target &target, lldb::addr_t value,
bool value_is_offset) override;
lldb::ByteOrder GetByteOrder() const override;
bool IsExecutable() const override;
uint32_t GetAddressByteSize() const override;
lldb_private::AddressClass GetAddressClass(lldb::addr_t file_addr) override;
void ParseSymtab(lldb_private::Symtab &symtab) override;
bool IsStripped() override;
void CreateSections(lldb_private::SectionList &unified_section_list) override;
void Dump(lldb_private::Stream *s) override;
lldb_private::ArchSpec GetArchitecture() override;
lldb_private::UUID GetUUID() override;
std::optional<lldb_private::FileSpec> GetDebugLink();
uint32_t GetDependentModules(lldb_private::FileSpecList &files) override;
lldb_private::Address
GetImageInfoAddress(lldb_private::Target *target) override;
lldb_private::Address GetEntryPointAddress() override;
lldb_private::Address GetBaseAddress() override;
ObjectFile::Type CalculateType() override;
ObjectFile::Strata CalculateStrata() override;
size_t ReadSectionData(lldb_private::Section *section,
lldb::offset_t section_offset, void *dst,
size_t dst_len) override;
size_t ReadSectionData(lldb_private::Section *section,
lldb_private::DataExtractor §ion_data) override;
llvm::ArrayRef<elf::ELFProgramHeader> ProgramHeaders();
lldb_private::DataExtractor GetSegmentData(const elf::ELFProgramHeader &H);
llvm::StringRef
StripLinkerSymbolAnnotations(llvm::StringRef symbol_name) const override;
void RelocateSection(lldb_private::Section *section) override;
protected:
std::vector<LoadableData>
GetLoadableData(lldb_private::Target &target) override;
static lldb::WritableDataBufferSP
MapFileDataWritable(const lldb_private::FileSpec &file, uint64_t Size,
uint64_t Offset);
private:
ObjectFileELF(const lldb::ModuleSP &module_sp, lldb::DataBufferSP data_sp,
lldb::offset_t data_offset, const lldb_private::FileSpec *file,
lldb::offset_t offset, lldb::offset_t length);
ObjectFileELF(const lldb::ModuleSP &module_sp,
lldb::DataBufferSP header_data_sp,
const lldb::ProcessSP &process_sp, lldb::addr_t header_addr);
typedef std::vector<elf::ELFProgramHeader> ProgramHeaderColl;
struct ELFSectionHeaderInfo : public elf::ELFSectionHeader {
lldb_private::ConstString section_name;
};
typedef std::vector<ELFSectionHeaderInfo> SectionHeaderColl;
typedef SectionHeaderColl::iterator SectionHeaderCollIter;
typedef SectionHeaderColl::const_iterator SectionHeaderCollConstIter;
typedef std::vector<elf::ELFDynamic> DynamicSymbolColl;
typedef DynamicSymbolColl::iterator DynamicSymbolCollIter;
typedef DynamicSymbolColl::const_iterator DynamicSymbolCollConstIter;
typedef std::map<lldb::addr_t, lldb_private::AddressClass>
FileAddressToAddressClassMap;
static const uint32_t m_plugin_version = 1;
static const uint32_t g_core_uuid_magic;
elf::ELFHeader m_header;
lldb_private::UUID m_uuid;
std::string m_gnu_debuglink_file;
uint32_t m_gnu_debuglink_crc = 0;
ProgramHeaderColl m_program_headers;
SectionHeaderColl m_section_headers;
DynamicSymbolColl m_dynamic_symbols;
std::shared_ptr<ObjectFileELF> m_gnu_debug_data_object_file;
mutable std::unique_ptr<lldb_private::FileSpecList> m_filespec_up;
lldb_private::Address m_entry_point_address;
lldb_private::ArchSpec m_arch_spec;
FileAddressToAddressClassMap m_address_class_map;
size_t SectionIndex(const SectionHeaderCollIter &I);
size_t SectionIndex(const SectionHeaderCollConstIter &I) const;
static size_t GetProgramHeaderInfo(ProgramHeaderColl &program_headers,
lldb_private::DataExtractor &object_data,
const elf::ELFHeader &header);
static uint32_t
CalculateELFNotesSegmentsCRC32(const ProgramHeaderColl &program_headers,
lldb_private::DataExtractor &data);
bool ParseProgramHeaders();
size_t ParseSectionHeaders();
lldb::SectionType GetSectionType(const ELFSectionHeaderInfo &H) const;
static void ParseARMAttributes(lldb_private::DataExtractor &data,
uint64_t length,
lldb_private::ArchSpec &arch_spec);
static size_t GetSectionHeaderInfo(SectionHeaderColl §ion_headers,
lldb_private::DataExtractor &object_data,
const elf::ELFHeader &header,
lldb_private::UUID &uuid,
std::string &gnu_debuglink_file,
uint32_t &gnu_debuglink_crc,
lldb_private::ArchSpec &arch_spec);
size_t ParseDependentModules();
size_t ParseDynamicSymbols();
std::pair<unsigned, FileAddressToAddressClassMap>
ParseSymbolTable(lldb_private::Symtab *symbol_table, lldb::user_id_t start_id,
lldb_private::Section *symtab);
std::pair<unsigned, FileAddressToAddressClassMap>
ParseSymbols(lldb_private::Symtab *symbol_table, lldb::user_id_t start_id,
lldb_private::SectionList *section_list,
const size_t num_symbols,
const lldb_private::DataExtractor &symtab_data,
const lldb_private::DataExtractor &strtab_data);
unsigned ParseTrampolineSymbols(lldb_private::Symtab *symbol_table,
lldb::user_id_t start_id,
const ELFSectionHeaderInfo *rela_hdr,
lldb::user_id_t section_id);
void ParseUnwindSymbols(lldb_private::Symtab *symbol_table,
lldb_private::DWARFCallFrameInfo *eh_frame);
unsigned RelocateDebugSections(const elf::ELFSectionHeader *rel_hdr,
lldb::user_id_t rel_id,
lldb_private::Symtab *thetab);
unsigned ApplyRelocations(lldb_private::Symtab *symtab,
const elf::ELFHeader *hdr,
const elf::ELFSectionHeader *rel_hdr,
const elf::ELFSectionHeader *symtab_hdr,
const elf::ELFSectionHeader *debug_hdr,
lldb_private::DataExtractor &rel_data,
lldb_private::DataExtractor &symtab_data,
lldb_private::DataExtractor &debug_data,
lldb_private::Section *rel_section);
size_t GetSectionHeaderStringTable();
lldb::user_id_t GetSectionIndexByName(const char *name);
const ELFSectionHeaderInfo *GetSectionHeaderByIndex(lldb::user_id_t id);
static void DumpELFHeader(lldb_private::Stream *s,
const elf::ELFHeader &header);
static void DumpELFHeader_e_ident_EI_DATA(lldb_private::Stream *s,
unsigned char ei_data);
static void DumpELFHeader_e_type(lldb_private::Stream *s,
elf::elf_half e_type);
void DumpELFProgramHeaders(lldb_private::Stream *s);
static void DumpELFProgramHeader(lldb_private::Stream *s,
const elf::ELFProgramHeader &ph);
static void DumpELFProgramHeader_p_type(lldb_private::Stream *s,
elf::elf_word p_type);
static void DumpELFProgramHeader_p_flags(lldb_private::Stream *s,
elf::elf_word p_flags);
void DumpELFSectionHeaders(lldb_private::Stream *s);
static void DumpELFSectionHeader(lldb_private::Stream *s,
const ELFSectionHeaderInfo &sh);
static void DumpELFSectionHeader_sh_type(lldb_private::Stream *s,
elf::elf_word sh_type);
static void DumpELFSectionHeader_sh_flags(lldb_private::Stream *s,
elf::elf_xword sh_flags);
void DumpDependentModules(lldb_private::Stream *s);
const elf::ELFDynamic *FindDynamicSymbol(unsigned tag);
unsigned PLTRelocationType();
static lldb_private::Status
RefineModuleDetailsFromNote(lldb_private::DataExtractor &data,
lldb_private::ArchSpec &arch_spec,
lldb_private::UUID &uuid);
bool AnySegmentHasPhysicalAddress();
std::shared_ptr<ObjectFileELF> GetGnuDebugDataObjectFile();
#ifdef MS_DEBUGGER
std::vector<std::shared_ptr<lldb_private::ModuleSpec>> GetChildModuleSpecs(const std::string &kernel_name = "") override;
std::shared_ptr<lldb_private::ModuleSpec> GetTargetModuleSpec(llvm::StringRef kernel_name) override;
#endif
};
#endif