From dfd191367991cb157b53767dcc05824c826b5abd Mon Sep 17 00:00:00 2001 From: Iman Hosseini Date: Tue, 20 Sep 2022 00:05:00 -0400 Subject: detects the loading of isa-incompatible (i.e. 32 bit code to 64bit HART) code and emits an error message to help avoid unintentionally loading wrong elf. --- fesvr/elfloader.cc | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'fesvr/elfloader.cc') diff --git a/fesvr/elfloader.cc b/fesvr/elfloader.cc index 76cd6da..1bdccd3 100644 --- a/fesvr/elfloader.cc +++ b/fesvr/elfloader.cc @@ -16,7 +16,7 @@ #include #include -std::map load_elf(const char* fn, memif_t* memif, reg_t* entry) +std::map load_elf(const char* fn, memif_t* memif, reg_t* entry, unsigned required_xlen = 0) { int fd = open(fn, O_RDONLY); struct stat s; @@ -32,6 +32,10 @@ std::map load_elf(const char* fn, memif_t* memif, reg_t* assert(size >= sizeof(Elf64_Ehdr)); const Elf64_Ehdr* eh64 = (const Elf64_Ehdr*)buf; assert(IS_ELF32(*eh64) || IS_ELF64(*eh64)); + unsigned xlen = IS_ELF32(*eh64) ? 32 : 64; + if (required_xlen != 0 && required_xlen != xlen) { + throw incompat_xlen(required_xlen, xlen); + } assert(IS_ELFLE(*eh64) || IS_ELFBE(*eh64)); assert(IS_ELF_EXEC(*eh64)); assert(IS_ELF_RISCV(*eh64) || IS_ELF_EM_NONE(*eh64)); -- cgit v1.1