From b1fe77e995886bbf7b6363654eed7a85d7b4be31 Mon Sep 17 00:00:00 2001 From: goeiecool9999 <7033575+goeiecool9999@users.noreply.github.com> Date: Tue, 22 Sep 2026 20:01:24 +0200 Subject: [PATCH] Refactor ELF reader class also properly interpet the mmap failure condition --- .../ExceptionHandler/ELFSymbolTable.cpp | 71 +++++++++---------- src/Common/ExceptionHandler/ELFSymbolTable.h | 15 ++-- 2 files changed, 40 insertions(+), 46 deletions(-) diff --git a/src/Common/ExceptionHandler/ELFSymbolTable.cpp b/src/Common/ExceptionHandler/ELFSymbolTable.cpp index 7258338e..6339f7a6 100644 --- a/src/Common/ExceptionHandler/ELFSymbolTable.cpp +++ b/src/Common/ExceptionHandler/ELFSymbolTable.cpp @@ -5,31 +5,19 @@ #include #include -uint16 ELFSymbolTable::FindSection(int type, const std::string_view& name) +template +std::span ELFSymbolTable::SectionAsArray(const Elf64_Shdr* section) { - if (!shTable || !shStrTable) - return 0; + if (section == nullptr) + return {}; + if (section->sh_size < sizeof(T)) + return {}; + if (section->sh_entsize != 0 && section->sh_entsize != sizeof(T)) + return {}; - for (uint16 i = 0; i < header->e_shnum; ++i) - { - auto& entry = shTable[i]; - if(entry.sh_type == type && std::string_view{&shStrTable[entry.sh_name]} == name) - { - return i; - } - } - return 0; + return {(T*)(mappedExecutable + section->sh_offset), section->sh_size / sizeof(T)}; } -void* ELFSymbolTable::SectionPointer(uint16 index) -{ - return SectionPointer(shTable[index]); -} - -void* ELFSymbolTable::SectionPointer(const Elf64_Shdr& section) -{ - return (void*)(mappedExecutable + section.sh_offset); -} ELFSymbolTable::ELFSymbolTable() { @@ -40,7 +28,7 @@ ELFSymbolTable::ELFSymbolTable() // retrieve file size. struct stat filestats; - if (fstat(fd, &filestats)) + if (fstat(fd, &filestats) != 0) { close(fd); return; @@ -48,9 +36,9 @@ ELFSymbolTable::ELFSymbolTable() mappedExecutableSize = filestats.st_size; // attempt to map the file - mappedExecutable = (uint8*)(mmap(nullptr, mappedExecutableSize, PROT_READ, MAP_PRIVATE, fd, 0)); + mappedExecutable = static_cast(mmap(nullptr, mappedExecutableSize, PROT_READ, MAP_PRIVATE, fd, 0)); close(fd); - if (!mappedExecutable) + if (mappedExecutable == MAP_FAILED) return; // verify signature @@ -64,19 +52,11 @@ ELFSymbolTable::ELFSymbolTable() } } - shTable = (Elf64_Shdr*)(mappedExecutable + header->e_shoff); + shTable = {(Elf64_Shdr*)(mappedExecutable + header->e_shoff), header->e_shnum}; - Elf64_Shdr& shStrn = shTable[header->e_shstrndx]; - shStrTable = (char*)(mappedExecutable + shStrn.sh_offset); - - strTable = (char*)SectionPointer(FindSection(SHT_STRTAB, ".strtab")); - - Elf64_Shdr& symTabShdr = shTable[FindSection(SHT_SYMTAB, ".symtab")]; - if (symTabShdr.sh_entsize == 0) - return; - - symTableLen = symTabShdr.sh_size / symTabShdr.sh_entsize; - symTable = (Elf64_Sym*)(SectionPointer(symTabShdr)); + shStrTable = SectionAsArray(&shTable[header->e_shstrndx]); + strTable = SectionAsArray(FindSection(SHT_STRTAB, ".strtab")); + symTable = SectionAsArray(FindSection(SHT_SYMTAB, ".symtab")); } ELFSymbolTable::~ELFSymbolTable() @@ -85,15 +65,30 @@ ELFSymbolTable::~ELFSymbolTable() munmap(mappedExecutable, mappedExecutableSize); } +Elf64_Shdr* ELFSymbolTable::FindSection(Elf64_Word type, const std::string_view& name) +{ + if (shTable.empty() || shStrTable.empty()) + return nullptr; + + for (auto& entry : shTable) + { + if(entry.sh_type == type && std::string_view{&shStrTable[entry.sh_name]} == name) + { + return &entry; + } + } + return nullptr; +} + std::string_view ELFSymbolTable::OffsetToSymbol(uint64 ptr, uint64& fromStart) const { - if(!symTable || !strTable) + if(symTable.empty() || strTable.empty()) { fromStart = -1; return {}; } - for (auto entry = symTable+1; entry < symTable+symTableLen; ++entry) + for (auto entry = symTable.begin()+1; entry != symTable.end(); ++entry) { if (ELF64_ST_TYPE(entry->st_info) != STT_FUNC) continue; diff --git a/src/Common/ExceptionHandler/ELFSymbolTable.h b/src/Common/ExceptionHandler/ELFSymbolTable.h index 89248bbb..9640ae36 100644 --- a/src/Common/ExceptionHandler/ELFSymbolTable.h +++ b/src/Common/ExceptionHandler/ELFSymbolTable.h @@ -15,17 +15,16 @@ private: Elf64_Ehdr* header = nullptr; - Elf64_Shdr* shTable = nullptr; - char* shStrTable = nullptr; + std::span shTable{}; + std::span shStrTable{}; - Elf64_Sym* symTable = nullptr; - uint64 symTableLen = 0; - char* strTable = nullptr; + std::span symTable{}; + std::span strTable{}; - uint16 FindSection(int type, const std::string_view& name); + Elf64_Shdr* FindSection(Elf64_Word type, const std::string_view& name); - void* SectionPointer (uint16 index); - void* SectionPointer(const Elf64_Shdr& section); + template + std::span SectionAsArray(const Elf64_Shdr* section); // ownership of mapped memory, cannot copy. ELFSymbolTable(const ELFSymbolTable&) = delete;