Skip to content

Commit

Permalink
Fixed FELF emit bug with custom base, updated README
Browse files Browse the repository at this point in the history
  • Loading branch information
gamozolabs committed Nov 8, 2021
1 parent f2aa249 commit a05aada
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ Usage: elfloader [--binary] [--base=<addr>] <input ELF> <output>
base to the start of the first LOAD segment if needed.
<addr> is default hex, can be overrided with `0d`, `0b`,
`0x`, or `0o` prefixes.
Warning: This does not _relocate_ to base, it simply starts
the output at `<addr>` (adding zero bytes such that the
output image can be loaded at `<addr>` instead of the
original ELF base)
<input ELF> - Path to input ELF
<output> - Path to output file
```
Expand Down
15 changes: 9 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,17 +282,16 @@ pub fn load_file(path: impl AsRef<Path>, base: Option<u64>)
// Sort load sections by virtual address
load.sort_by_key(|x| x.0);

// Get the lowest address loaded
let lowest_addr = load.get(0).ok_or(Error::NoLoadSegments)?.0;
// Start load at the specified `base`, otherwise use the lowest address of
// all the LOAD sections
let lowest_addr = base.unwrap_or(
load.get(0).ok_or(Error::NoLoadSegments)?.0);

// Flat in-memory loaded representation
let mut loaded = Vec::new();

// Start load at the specified `base`, otherwise use the lowest address of
// all the LOAD sections
let mut cur_addr = base.unwrap_or(lowest_addr);

// Load everything!
let mut cur_addr = lowest_addr;
for (vaddr, bytes) in load {
// Get the offset from where we are
let offset: usize = vaddr.checked_sub(cur_addr)
Expand Down Expand Up @@ -369,6 +368,10 @@ r#"Usage: elfloader [--binary] [--base=<addr>] <input ELF> <output>
base to the start of the first LOAD segment if needed.
<addr> is default hex, can be overrided with `0d`, `0b`,
`0x`, or `0o` prefixes.
Warning: This does not _relocate_ to base, it simply starts
the output at `<addr>` (adding zero bytes such that the
output image can be loaded at `<addr>` instead of the
original ELF base)
<input ELF> - Path to input ELF
<output> - Path to output file"#);
return Ok(());
Expand Down

0 comments on commit a05aada

Please sign in to comment.