Skip to content

Commit eb28f2c

Browse files
retragerbradford
authored andcommitted
pe: Change to load at ImageBase if specified
Signed-off-by: Akira Moroo <retrage01@gmail.com>
1 parent 61d3e72 commit eb28f2c

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ fn boot_from_device(device: &mut block::VirtioBlockDevice, info: &dyn boot::Info
128128

129129
let mut l = pe::Loader::new(&mut file);
130130
let load_addr = 0x20_0000;
131-
let (entry_addr, size) = match l.load(load_addr) {
131+
let (entry_addr, load_addr, size) = match l.load(load_addr) {
132132
Ok(load_info) => load_info,
133133
Err(err) => {
134134
log!("Error loading executable: {:?}", err);
@@ -137,7 +137,7 @@ fn boot_from_device(device: &mut block::VirtioBlockDevice, info: &dyn boot::Info
137137
};
138138

139139
log!("Executable loaded");
140-
efi::efi_exec(entry_addr, 0x20_0000, size, info, &f, device);
140+
efi::efi_exec(entry_addr, load_addr, size, info, &f, device);
141141
true
142142
}
143143

src/pe.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ impl<'a> Loader<'a> {
4747
}
4848
}
4949

50-
pub fn load(&mut self, address: u64) -> Result<(u64, u64), Error> {
50+
pub fn load(&mut self, load_addr: u64) -> Result<(u64, u64, u64), Error> {
5151
let mut data: [u8; 1024] = [0; 1024];
5252

5353
match self.file.read(&mut data[0..512]) {
@@ -102,6 +102,12 @@ impl<'a> Loader<'a> {
102102
let entry_point = optional_region.read_u32(16);
103103

104104
self.image_base = optional_region.read_u64(24);
105+
let address = if self.image_base != 0 {
106+
// The image has desired load address
107+
self.image_base
108+
} else {
109+
load_addr
110+
};
105111
self.image_size = optional_region.read_u32(56);
106112
let size_of_headers = optional_region.read_u32(60);
107113

@@ -210,7 +216,11 @@ impl<'a> Loader<'a> {
210216
}
211217
}
212218

213-
Ok((address + u64::from(entry_point), u64::from(self.image_size)))
219+
Ok((
220+
address + u64::from(entry_point),
221+
address,
222+
u64::from(self.image_size),
223+
))
214224
}
215225
}
216226

@@ -235,8 +245,9 @@ mod tests {
235245
alloc::alloc(layout)
236246
};
237247

238-
let (a, size) = l.load(fake_mem as u64).expect("expect loading success");
239-
assert_eq!(a, fake_mem as u64 + 0x4000);
248+
let (entry, addr, size) = l.load(fake_mem as u64).expect("expect loading success");
249+
assert_eq!(entry, fake_mem as u64 + 0x4000);
250+
assert_eq!(addr, fake_mem as u64);
240251
assert_eq!(size, 110_592);
241252
}
242253
}

0 commit comments

Comments
 (0)