Skip to content

Commit

Permalink
Fix codestyle inconsistencies (#5)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniil Gentili <[email protected]>
  • Loading branch information
zanieb and danog authored May 16, 2023
1 parent ed82129 commit f3f3247
Show file tree
Hide file tree
Showing 25 changed files with 2,734 additions and 1,288 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ jobs:
- name: Install Dependencies
run: sudo apt install libunwind-dev
if: runner.os == 'Linux'
- name: Format
run: cargo fmt --check
- name: Build
run: cargo build --release --verbose --examples
- name: Test
Expand Down
2 changes: 1 addition & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ fn main() {
match env::var("CARGO_CFG_TARGET_OS").unwrap().as_ref() {
"windows" => println!("cargo:rustc-cfg=unwind"),
"linux" => println!("cargo:rustc-cfg=unwind"),
_ => { }
_ => {}
}
}
98 changes: 68 additions & 30 deletions src/binary_parser.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

use std::collections::HashMap;
use std::fs::File;
use std::path::Path;
Expand All @@ -15,7 +14,7 @@ pub struct BinaryInfo {
pub bss_size: u64,
pub offset: u64,
pub addr: u64,
pub size: u64
pub size: u64,
}

impl BinaryInfo {
Expand All @@ -42,12 +41,18 @@ pub fn parse_binary(filename: &Path, addr: u64, size: u64) -> Result<BinaryInfo,
let mach = match mach {
goblin::mach::Mach::Binary(mach) => mach,
goblin::mach::Mach::Fat(fat) => {
let arch = fat.iter_arches().find(|arch|
match arch {
let arch = fat
.iter_arches()
.find(|arch| match arch {
Ok(arch) => arch.is_64(),
Err(_) => false
}
).ok_or_else(|| format_err!("Failed to find 64 bit arch in FAT archive in {}", filename.display()))??;
Err(_) => false,
})
.ok_or_else(|| {
format_err!(
"Failed to find 64 bit arch in FAT archive in {}",
filename.display()
)
})??;
let bytes = &buffer[arch.offset as usize..][..arch.size as usize];
goblin::mach::MachO::parse(bytes, 0)?
}
Expand All @@ -72,24 +77,44 @@ pub fn parse_binary(filename: &Path, addr: u64, size: u64) -> Result<BinaryInfo,
if name.starts_with('_') {
symbols.insert(name[1..].to_string(), value.n_value + offset);
}

}
}
Ok(BinaryInfo{filename: filename.to_owned(), symbols, bss_addr, bss_size, offset, addr, size})
Ok(BinaryInfo {
filename: filename.to_owned(),
symbols,
bss_addr,
bss_size,
offset,
addr,
size,
})
}

Object::Elf(elf) => {
let bss_header = elf.section_headers
let bss_header = elf
.section_headers
.iter()
.find(|ref header| header.sh_type == goblin::elf::section_header::SHT_NOBITS)
.ok_or_else(|| format_err!("Failed to find BSS section header in {}", filename.display()))?;

let program_header = elf.program_headers
.ok_or_else(|| {
format_err!(
"Failed to find BSS section header in {}",
filename.display()
)
})?;

let program_header = elf
.program_headers
.iter()
.find(|ref header|
header.p_type == goblin::elf::program_header::PT_LOAD &&
header.p_flags & goblin::elf::program_header::PF_X != 0)
.ok_or_else(|| format_err!("Failed to find executable PT_LOAD program header in {}", filename.display()))?;
.find(|ref header| {
header.p_type == goblin::elf::program_header::PT_LOAD
&& header.p_flags & goblin::elf::program_header::PF_X != 0
})
.ok_or_else(|| {
format_err!(
"Failed to find executable PT_LOAD program header in {}",
filename.display()
)
})?;

// 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.
Expand All @@ -103,14 +128,16 @@ pub fn parse_binary(filename: &Path, addr: u64, size: u64) -> Result<BinaryInfo,
let name = elf.dynstrtab[dynsym.st_name].to_string();
symbols.insert(name, dynsym.st_value + offset);
}
Ok(BinaryInfo{filename: filename.to_owned(),
symbols,
bss_addr: bss_header.sh_addr + offset,
bss_size: bss_header.sh_size,
offset,
addr,
size})
},
Ok(BinaryInfo {
filename: filename.to_owned(),
symbols,
bss_addr: bss_header.sh_addr + offset,
bss_size: bss_header.sh_size,
offset,
addr,
size,
})
}
Object::PE(pe) => {
for export in pe.exports {
if let Some(name) = export.name {
Expand All @@ -123,16 +150,27 @@ pub fn parse_binary(filename: &Path, addr: u64, size: u64) -> Result<BinaryInfo,
pe.sections
.iter()
.find(|ref section| section.name.starts_with(b".data"))
.ok_or_else(|| format_err!("Failed to find .data section in PE binary of {}", filename.display()))
.ok_or_else(|| {
format_err!(
"Failed to find .data section in PE binary of {}",
filename.display()
)
})
.map(|data_section| {
let bss_addr = u64::from(data_section.virtual_address) + offset;
let bss_size = u64::from(data_section.virtual_size);

BinaryInfo{filename: filename.to_owned(), symbols, bss_addr, bss_size, offset, addr, size}
BinaryInfo {
filename: filename.to_owned(),
symbols,
bss_addr,
bss_size,
offset,
addr,
size,
}
})
},
_ => {
Err(format_err!("Unhandled binary type"))
}
_ => Err(format_err!("Unhandled binary type")),
}
}
Loading

0 comments on commit f3f3247

Please sign in to comment.