From 35c81e5269afa4f3203d1fe9f9dcf6f5c193a528 Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Fri, 31 Mar 2023 19:34:45 +0200 Subject: [PATCH] Fix get_address_point --- src/simulator/loader.rs | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/simulator/loader.rs b/src/simulator/loader.rs index 904bc87..2eac03c 100644 --- a/src/simulator/loader.rs +++ b/src/simulator/loader.rs @@ -160,25 +160,25 @@ impl Loader { } /// return the memory address of something stored at address - /// Can return None if the file is smaller than adress + 4 (or 7 if 64 bits), in this case, the elf header is incorrect + /// Can return None if the file is smaller than adress + 3 (or 7 if 64 bits), in this case, the elf header is incorrect fn get_address_point(&self, address: usize, is_32bits: bool) -> Option { if is_32bits { let mut bytes: [u8; 4] = [0; 4]; bytes[0] = self.bytes.get(address).copied()?; - bytes[0] = self.bytes.get(address + 1).copied()?; - bytes[0] = self.bytes.get(address + 2).copied()?; - bytes[0] = self.bytes.get(address + 3).copied()?; + bytes[1] = self.bytes.get(address + 1).copied()?; + bytes[2] = self.bytes.get(address + 2).copied()?; + bytes[3] = self.bytes.get(address + 3).copied()?; Option::Some(u32::from_le_bytes(bytes) as u64) } else { let mut bytes: [u8; 8] = [0; 8]; bytes[0] = self.bytes.get(address).copied()?; - bytes[0] = self.bytes.get(address + 1).copied()?; - bytes[0] = self.bytes.get(address + 2).copied()?; - bytes[0] = self.bytes.get(address + 3).copied()?; - bytes[0] = self.bytes.get(address + 4).copied()?; - bytes[0] = self.bytes.get(address + 5).copied()?; - bytes[0] = self.bytes.get(address + 6).copied()?; - bytes[0] = self.bytes.get(address + 7).copied()?; + bytes[1] = self.bytes.get(address + 1).copied()?; + bytes[2] = self.bytes.get(address + 2).copied()?; + bytes[3] = self.bytes.get(address + 3).copied()?; + bytes[4] = self.bytes.get(address + 4).copied()?; + bytes[5] = self.bytes.get(address + 5).copied()?; + bytes[6] = self.bytes.get(address + 6).copied()?; + bytes[7] = self.bytes.get(address + 7).copied()?; Option::Some(u64::from_le_bytes(bytes)) } } @@ -201,6 +201,9 @@ mod test { assert_eq!(true, loader.is_system_v_elf()); assert_eq!(true, loader.is_riscv_isa()); assert_eq!(Option::Some(1), loader.get_version()); + assert_eq!(Option::Some(0x4000), loader.get_entrypoint(false)); + assert_eq!(Option::Some(64), loader.get_program_header_table_location(false)); + assert_eq!(Option::Some(18984), loader.get_section_header_table_location(false)); } } \ No newline at end of file