Fix get_address_point

This commit is contained in:
Quentin Legot 2023-03-31 19:34:45 +02:00
parent 453de4b704
commit 35c81e5269

View File

@ -160,25 +160,25 @@ impl Loader {
} }
/// return the memory address of something stored at address /// 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<u64> { fn get_address_point(&self, address: usize, is_32bits: bool) -> Option<u64> {
if is_32bits { if is_32bits {
let mut bytes: [u8; 4] = [0; 4]; let mut bytes: [u8; 4] = [0; 4];
bytes[0] = self.bytes.get(address).copied()?; bytes[0] = self.bytes.get(address).copied()?;
bytes[0] = self.bytes.get(address + 1).copied()?; bytes[1] = self.bytes.get(address + 1).copied()?;
bytes[0] = self.bytes.get(address + 2).copied()?; bytes[2] = self.bytes.get(address + 2).copied()?;
bytes[0] = self.bytes.get(address + 3).copied()?; bytes[3] = self.bytes.get(address + 3).copied()?;
Option::Some(u32::from_le_bytes(bytes) as u64) Option::Some(u32::from_le_bytes(bytes) as u64)
} else { } else {
let mut bytes: [u8; 8] = [0; 8]; let mut bytes: [u8; 8] = [0; 8];
bytes[0] = self.bytes.get(address).copied()?; bytes[0] = self.bytes.get(address).copied()?;
bytes[0] = self.bytes.get(address + 1).copied()?; bytes[1] = self.bytes.get(address + 1).copied()?;
bytes[0] = self.bytes.get(address + 2).copied()?; bytes[2] = self.bytes.get(address + 2).copied()?;
bytes[0] = self.bytes.get(address + 3).copied()?; bytes[3] = self.bytes.get(address + 3).copied()?;
bytes[0] = self.bytes.get(address + 4).copied()?; bytes[4] = self.bytes.get(address + 4).copied()?;
bytes[0] = self.bytes.get(address + 5).copied()?; bytes[5] = self.bytes.get(address + 5).copied()?;
bytes[0] = self.bytes.get(address + 6).copied()?; bytes[6] = self.bytes.get(address + 6).copied()?;
bytes[0] = self.bytes.get(address + 7).copied()?; bytes[7] = self.bytes.get(address + 7).copied()?;
Option::Some(u64::from_le_bytes(bytes)) 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_system_v_elf());
assert_eq!(true, loader.is_riscv_isa()); assert_eq!(true, loader.is_riscv_isa());
assert_eq!(Option::Some(1), loader.get_version()); 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));
} }
} }