Skip to content

Commit

Permalink
Page-align offset before calculating virtual addresses for ELF
Browse files Browse the repository at this point in the history
This mimics what `ld.so` does by aligning the virtual addresses to
the current page size, and fixes:
- Existing issues with DSOs produced by LLD which, unlike gold or BFD,
  doesn't align p_vaddr to p_align (the previous overflow prevention
  wasn't sufficient here).
- Systems w/ configurable page sizes, where just using p_align isn't
  enough.
  • Loading branch information
andrewjcg committed Oct 26, 2023
1 parent 75adc45 commit d1c5469
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ log = "0.4"
lru = "0.10"
regex = ">=1.6.0"
tempfile = "3.6.0"
page_size = "0.6.0"
proc-maps = "0.3.2"
memmap = "0.7.0"
cpp_demangle = "0.4"
Expand Down
7 changes: 4 additions & 3 deletions src/binary_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,10 @@ pub fn parse_binary(filename: &Path, addr: u64, size: u64) -> Result<BinaryInfo,
)
})?;

// p_vaddr may be larger than the map address in case when the header has an offset and
// the map address is relatively small. In this case we can default to 0.
let offset = offset.saturating_sub(program_header.p_vaddr);
// Align the virtual address offset, then subtract it from the offset
// to get real offset for symbol addresses in the file.
let aligned_vaddr = program_header.p_vaddr - (program_header.p_vaddr % page_size::get() as u64);
let offset = offset - aligned_vaddr;

for sym in elf.syms.iter() {
let name = elf.strtab[sym.st_name].to_string();
Expand Down

0 comments on commit d1c5469

Please sign in to comment.