elfviewer : detailed program headers info

This commit is contained in:
georgemoralis 2023-05-13 10:00:22 +03:00
parent a8a598aee4
commit 98068809aa
3 changed files with 111 additions and 6 deletions

View file

@ -17,7 +17,7 @@ void ElfViewer::display(bool enabled)
static int selected = -1;
ImGui::Begin("Self/Elf Viewer", &enabled);
ImGui::BeginChild("Left Tree pane", ImVec2(200, 0), false);//left tree
ImGui::BeginChild("Left Tree pane", ImVec2(300, 0), false);//left tree
if (elf->isSelfFile())
{
if (ImGui::TreeNode("Self"))
@ -57,7 +57,9 @@ void ElfViewer::display(bool enabled)
const auto* elf_header = elf->GetElfHeader();
for (u16 i = 0; i < elf_header->e_phnum; i++)
{
if (ImGui::TreeNodeEx((void*)(intptr_t)i,ImGuiTreeNodeFlags_Leaf | ImGuiTreeNodeFlags_NoTreePushOnOpen, "%d", i))
const auto* pheader = elf->GetProgramHeader();
std::string ProgramInfo = elf->ElfPheaderFlagsStr((pheader + i)->p_flags) + " " + elf->ElfPheaderTypeStr((pheader + i)->p_type);
if (ImGui::TreeNodeEx((void*)(intptr_t)i,ImGuiTreeNodeFlags_Leaf | ImGuiTreeNodeFlags_NoTreePushOnOpen, "%d - %s", i,ProgramInfo.c_str()))
{
if (ImGui::IsItemClicked())
selected = ELF_PROGRAM_HEADER_START + i;

View file

@ -436,11 +436,71 @@ std::string Elf::ElfHeaderStr()
return header;
}
std::string Elf::ElfPheaderTypeStr(u32 type) {
switch (type) {
case PT_NULL:
return "Null";
case PT_LOAD:
return "Loadable";
case PT_DYNAMIC:
return "Dynamic";
case PT_INERP:
return "Interpreter Path";
case PT_NOTE:
return "Note";
case PT_SHLIB:
return "Section Header Library";
case PT_PHDR:
return "Program Header";
case PT_TLS:
return "Thread-Local Storage";
case PT_NUM:
return "Defined Sections Number";
case PT_SCE_RELA:
return "SCE Relative";
case PT_SCE_DYNLIBDATA:
return "SCE Dynamic Library Data";
case PT_SCE_PROCPARAM:
return "SCE Processor Parameters";
case PT_SCE_MODULE_PARAM:
return "SCE Module Parameters";
case PT_SCE_RELRO:
return "SCE Read-Only After Relocation";
case PT_GNU_EH_FRAME:
return "GNU Entry Header Frame";
case PT_GNU_STACK:
return "GNU Stack (executability)";
case PT_GNU_RELRO:
return "GNU Read-Only After Relocation";
case PT_SCE_COMMENT:
return "SCE Comment";
case PT_SCE_LIBVERSION:
return "SCE Library Version";
default:
return "Unknown Section";
}
}
std::string Elf::ElfPheaderFlagsStr(u32 flags) {
std::string flg = "(";
flg += (flags & PF_READ) ? "R" : "_";
flg += (flags & PF_WRITE) ? "W" : "_";
flg += (flags & PF_EXEC) ? "X" : "_";
flg += ")";
return flg;
}
std::string Elf::ElfPHeaderStr(u16 no)
{
std::string header = fmt::format("====== PROGRAM HEADER {} ========\n", no);
header += fmt::format("p_type ....: {:#010x}\n", (m_elf_phdr + no)->p_type);
header += fmt::format("p_flags ...: {:#010x}\n", (m_elf_phdr + no)->p_flags);
header += fmt::format("p_type ....: {}\n", ElfPheaderTypeStr((m_elf_phdr + no)->p_type));
auto flags = magic_enum::enum_cast<elf_program_flags>((m_elf_phdr + no)->p_flags);
if (flags.has_value())
{
header += fmt::format("p_flags ...: {}\n", magic_enum::enum_name(flags.value()));
}
// header += fmt::format("p_flags ...: {:#010x}\n", (m_elf_phdr + no)->p_flags);
header += fmt::format("p_offset ..: {:#018x}\n", (m_elf_phdr + no)->p_offset);
header += fmt::format("p_vaddr ...: {:#018x}\n", (m_elf_phdr + no)->p_vaddr);
header += fmt::format("p_paddr ...: {:#018x}\n", (m_elf_phdr + no)->p_paddr);

View file

@ -238,10 +238,50 @@ struct elf_header
u16 e_shstrndx; /* Section name string table index */
};
typedef enum : u32 {
PT_NULL = 0x0,
PT_LOAD = 0x1,
PT_DYNAMIC = 0x2,
PT_INERP = 0x3,
PT_NOTE = 0x4,
PT_SHLIB = 0x5,
PT_PHDR = 0x6,
PT_TLS = 0x7,
PT_NUM = 0x8,
PT_SCE_RELA = 0x60000000,
PT_SCE_DYNLIBDATA = 0x61000000,
PT_SCE_PROCPARAM = 0x61000001,
PT_SCE_MODULE_PARAM = 0x61000002,
PT_SCE_RELRO = 0x61000010,
PT_GNU_EH_FRAME = 0x6474e550,
PT_GNU_STACK = 0x6474e551,
PT_GNU_RELRO = 0x6474e552,
PT_SCE_COMMENT = 0x6fffff00,
PT_SCE_LIBVERSION = 0x6fffff01,
PT_LOSUNW = 0x6ffffffa,
PT_SUNWBSS = 0x6ffffffa,
PT_SUNWSTACK = 0x6ffffffb,
PT_HISUNW = 0x6fffffff,
PT_HIOS = 0x6fffffff,
PT_LOPROC = 0x70000000,
PT_HIPROC = 0x7fffffff
} elf_program_type;
typedef enum : u32 {
PF_NONE = 0x0,
PF_EXEC = 0x1,
PF_WRITE = 0x2,
PF_WRITE_EXEC = 0x3,
PF_READ = 0x4,
PF_READ_EXEC = 0x5,
PF_READ_WRITE = 0x6,
PF_READ_WRITE_EXEC = 0x7
} elf_program_flags;
struct elf_program_header
{
u32 p_type; /* Type of segment */
u32 p_flags; /* Segment attributes */
elf_program_type p_type; /* Type of segment */
elf_program_flags p_flags; /* Segment attributes */
u64 p_offset; /* Offset in file */
u64 p_vaddr; /* Virtual address in memory */
u64 p_paddr; /* Reserved */
@ -301,6 +341,9 @@ public:
std::string SELFSegHeader(u16 no);
std::string ElfHeaderStr();
std::string ElfPHeaderStr(u16 no);
std::string ElfPheaderTypeStr(u32 type);
std::string ElfPheaderFlagsStr(u32 flags);
private:
void Reset();