Skip to content

Commit

Permalink
fix(elfloader): Sort phdr entries by paddr before return to `readFrom…
Browse files Browse the repository at this point in the history
…Elf()` (#566)

The `readFromElf()` relies on all phdr entries are sorted by paddr, which is
not mandatory for the ELF format. We must sort the parsed `sections` before
return back to `readFromElf()`.

Signed-off-by: Jiuyue Ma <[email protected]>
  • Loading branch information
forever043 authored Feb 10, 2025
1 parent 560e044 commit 92e75e0
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/test/csrc/common/elfloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
***************************************************************************************/

#include "elfloader.h"
#include <algorithm>

void ElfBinary::load() {
assert(size >= sizeof(Elf64_Ehdr));
Expand Down Expand Up @@ -46,6 +47,8 @@ void ElfBinary::parse(ElfBinaryData<ehdr_t, phdr_t, shdr_t, sym_t> &data) {
}
}
}
std::sort(sections.begin(), sections.end(),
[](const ElfSection &a, const ElfSection &b) { return a.data_dst < b.data_dst; });
}

ElfBinaryFile::ElfBinaryFile(const char *filename) : filename(filename) {
Expand Down Expand Up @@ -102,8 +105,8 @@ long readFromElf(void *ptr, const char *file_name, long buf_size) {

if (base_addr != PMEM_BASE) {
printf(
"The first address in the elf does not match the base of the physical memory. It is likely that execution "
"leads to unexpected behaviour.");
"The first address in the elf does not match the base of the physical memory.\n"
"It is likely that execution leads to unexpected behaviour.\n");
}

for (auto section: elf_file.sections) {
Expand Down

0 comments on commit 92e75e0

Please sign in to comment.