Skip to content
This repository was archived by the owner on Nov 23, 2025. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,13 @@ pub trait ElfLoader {
fn allocate(&mut self, load_headers: LoadableHeaders) -> Result<(), ElfLoaderErr>;

/// Copies `region` into memory starting at `base`.
/// The caller makes sure that there was an `allocate` call previously
///
/// # Safety
///
/// The caller must ensure that there was an `allocate` call previously
/// to initialize the region.
fn load(&mut self, flags: Flags, base: VAddr, region: &[u8]) -> Result<(), ElfLoaderErr>;
unsafe fn load(&mut self, flags: Flags, base: VAddr, region: &[u8])
-> Result<(), ElfLoaderErr>;

/// Request for the client to relocate the given `entry`
/// within the loaded ELF file.
Expand Down Expand Up @@ -465,11 +469,14 @@ impl<'s> ElfBinary<'s> {
if let Ph64(header) = p {
let typ = header.get_type()?;
if typ == Type::Load {
loader.load(
header.flags,
header.virtual_addr,
header.raw_data(&self.file),
)?;
// SAFETY: Yes, `loader.allocate(load_iter)?;` allocates memory.
unsafe {
loader.load(
header.flags,
header.virtual_addr,
header.raw_data(&self.file),
)?;
}
} else if typ == Type::Tls {
loader.tls(
header.virtual_addr,
Expand Down Expand Up @@ -585,7 +592,12 @@ mod test {
}
}

fn load(&mut self, _flags: Flags, base: VAddr, region: &[u8]) -> Result<(), ElfLoaderErr> {
unsafe fn load(
&mut self,
_flags: Flags,
base: VAddr,
region: &[u8],
) -> Result<(), ElfLoaderErr> {
info!("load base = {:#x} size = {:#x} region", base, region.len());
self.actions.push(LoaderAction::Load(base, region.len()));
Ok(())
Expand Down