Comments (1)
// xmas_elf wants a slice, not a pointer, so we have to try to figure out how
// big the ELF data is from just the pointer. We do this by first creating a
// slice for just the Elf64 header size. From that we can see where the
// section headers end and extend the slice to cover those. Since ELF doesn't
// guaranetee any ordering of section header vs sections, we scan the section
// headers to see where the last section ends, and if that's past the section
// headers, we further extend the slice and recreate the ELF one last time.
unsafe fn elf_from_ptr<'a>(image: *const c_void) -> Result<xmas_elf::ElfFile<'a>, &'static str> {
let image = image.cast::<u8>();
const ELF64_HEADER_SIZE: usize = 64;
let slice = unsafe { std::slice::from_raw_parts(image, ELF64_HEADER_SIZE) };
let elf = xmas_elf::ElfFile::new(slice)?;
let section_header_end = elf.header.pt2.sh_offset() as usize
+ elf.header.pt2.sh_count() as usize * elf.header.pt2.sh_entry_size() as usize;
let slice = unsafe { std::slice::from_raw_parts(image, section_header_end) };
let elf = xmas_elf::ElfFile::new(slice)?;
let last_section_end = elf
.section_iter()
.map(|s| s.offset() + s.size())
.max()
.ok_or(Err("no sections in ELF"))? as usize;
if sh_end > end {
let slice = unsafe { std::slice::from_raw_parts(image, std::cmp::max(end, last_section_end)) };
xmas_elf::ElfFile::new(slice)
} else {
Ok(elf)
}
}
from xmas-elf.
Related Issues (20)
- Nominate `nrc` for most amazing human being of the week. HOT 1
- Implement Eq on enums
- Implement find_program_header_by_type
- Add a license HOT 1
- Publish on crates.io HOT 6
- Load data using the correct byte order. HOT 1
- Constructing wrapper objects does not respect type alignment
- Please publish latest version
- Return Error type instead of String in Results
- Fix note section handling
- Don't use enums in Pod types HOT 1
- Program header flags are private and cannot be read HOT 6
- Panic on array index out of bounds
- Return Rela types HOT 1
- New release for fixing docs.rs issue HOT 1
- Please pubblish latest version HOT 1
- Weird panic when looking up section in my boot loader HOT 1
- Weird index out of bound while parsing RISC-V 32-bit binary HOT 3
- `parse_header` constructs an unaligned reference
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from xmas-elf.