fune/third_party/rust/goblin/examples/scroll.rs
Bastien Orivel ec3b82b135 Bug 1579425 - Part 2: Revendor dependencies. r=froydnj
Depends on D45046

Differential Revision: https://phabricator.services.mozilla.com/D45047

--HG--
rename : third_party/rust/goblin/src/elf/dyn.rs => third_party/rust/goblin/src/elf/dynamic.rs
rename : third_party/rust/object/src/lib.rs => third_party/rust/object/src/read/any.rs
rename : third_party/rust/object/src/pe.rs => third_party/rust/object/src/read/pe.rs
rename : third_party/rust/object/src/wasm.rs => third_party/rust/object/src/read/wasm.rs
extra : moz-landing-system : lando
2019-09-06 17:49:58 +00:00

31 lines
1.1 KiB
Rust

/// Demonstrates the magical powers of scroll + goblin
/// Goblin implements `TryFromCtx` for the header type
/// which means downstream crates/clients can just "parse" headers out of
/// arbitrary buffers, without learning new crate specific function names
/// I.e., all you need are Types + Pread = Happiness
use goblin::{error, elf64, elf};
use scroll::{Pwrite, Pread};
fn run () -> error::Result<()> {
let crt1: Vec<u8> = include!("../etc/crt1.rs");
let header: elf64::header::Header = crt1.pread(0)?;
assert_eq!(header.e_type, elf64::header::ET_REL);
println!("header: {:?}", &header);
// now lets write the header into some bytes
let mut bytes = [0u8; elf64::header::SIZEOF_EHDR];
bytes.pwrite(header, 0)?;
// read it back out
let header2: elf64::header::Header = bytes.pread(0)?;
// they're the same
assert_eq!(header, header2);
let elf: elf::Elf = crt1.pread(0)?;
println!("elf: {:#?}", &elf);
let elf = elf::Elf::parse(&crt1)?;
println!("elf: {:#?}", &elf);
Ok(())
}
fn main() {
run().unwrap();
}