From 132d3236bb884433bf94e961f6d32264e0334aec Mon Sep 17 00:00:00 2001 From: ornitorrincos Date: Tue, 18 Aug 2026 12:15:05 +0200 Subject: --- misc-host/ELF/ELF-loader-test/elfinfoprint.cpp | 74 ++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 misc-host/ELF/ELF-loader-test/elfinfoprint.cpp (limited to 'misc-host/ELF/ELF-loader-test/elfinfoprint.cpp') diff --git a/misc-host/ELF/ELF-loader-test/elfinfoprint.cpp b/misc-host/ELF/ELF-loader-test/elfinfoprint.cpp new file mode 100644 index 0000000..d5dcbfd --- /dev/null +++ b/misc-host/ELF/ELF-loader-test/elfinfoprint.cpp @@ -0,0 +1,74 @@ +#include "elfinfoprint.h" + +#include "bootloader_compat.h" + + +bool IsElf(Elf64_Ehdr * header) +{ + if(!header) + { + return false; + } + + if(header->e_ident[EI_MAG0] != ELFMAG0) { + return false; + } + if(header->e_ident[EI_MAG1] != ELFMAG1) { + return false; + } + if(header->e_ident[EI_MAG2] != ELFMAG2) { + return false; + } + if(header->e_ident[EI_MAG3] != ELFMAG3) { + return false; + } + + return true; +} + +void ElfInfoPrint(Elf64_Ehdr * header) +{ + // check if actual elf file + if(!IsElf(header)) + { + Print(L"Not an ELF File\n"); + return; + } + + if(header->e_type == ET_EXEC) + { + Print(L"Executable file\n"); + } + + if(header->e_machine == EM_X86_64) + { + Print(L"AMD64 Machine\n"); + } else + { + Print(L"Unrecorginzed Machine\n"); + } + + Print(L"Entry point: hx%x\n", header->e_entry); + + + char * strtable = elf_str_table(header); + Print(L"%s\n", strtable); +} + +// functions to access section headers +Elf64_Shdr *elf_sheader(Elf64_Ehdr *hdr) +{ + return (Elf64_Shdr *)((uintptr_t)hdr + hdr->e_shoff); +} + +Elf64_Shdr *elf_section(Elf64_Ehdr *hdr, int idx) +{ + return &elf_sheader(hdr)[idx]; +} + +char *elf_str_table(Elf64_Ehdr *hdr) +{ + if(hdr->e_shstrndx == SHN_UNDEF) return NULL; + return (char *)hdr + elf_section(hdr, hdr->e_shstrndx)->sh_offset; +} + -- cgit v1.2.3-70-g09d2