diff --git a/src/lib.rs b/src/lib.rs index 182bb43..f58f4d6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,12 +10,14 @@ mod exports; mod header; mod imports; mod sections; +mod segments; mod symbols; use exports::Export; use header::Header; use imports::Import; use sections::{Section, Sections}; +use segments::Segment; use symbols::Symbols; #[pyclass] @@ -100,13 +102,23 @@ impl Object { Symbols::from(self.macho().symbols()) } + fn segments(&self) -> Vec { + self.macho() + .segments + .into_iter() + .map(|seg| seg.into()) + .collect() + } + fn sections(&self) -> Sections { let macho = self.macho(); let mut sections = vec![]; + let mut idx = 0; for sect_iter in macho.segments.sections() { sections.extend(sect_iter.map(|section| { + idx += 1; let (sect, _data) = section.unwrap(); - Section::from(sect) + Section::from((idx, sect)) })); } Sections { sections } diff --git a/src/sections.rs b/src/sections.rs index 9cf4878..77c1822 100644 --- a/src/sections.rs +++ b/src/sections.rs @@ -3,6 +3,8 @@ use pyo3::prelude::*; #[derive(Debug, Clone)] #[pyclass] pub struct Section { + #[pyo3(get)] + index: usize, #[pyo3(get)] name: Option, #[pyo3(get)] @@ -30,9 +32,10 @@ impl Section { } } -impl From for Section { - fn from(section: goblin::mach::segment::Section) -> Self { +impl From<(usize, goblin::mach::segment::Section)> for Section { + fn from((index, section): (usize, goblin::mach::segment::Section)) -> Self { Section { + index, name: section.name().ok().map(|s| s.to_string()), segment: section.segname().ok().map(|s| s.to_string()), addr: section.addr, diff --git a/src/segments.rs b/src/segments.rs new file mode 100644 index 0000000..afb3898 --- /dev/null +++ b/src/segments.rs @@ -0,0 +1,54 @@ +use pyo3::prelude::*; + +#[derive(Debug, Clone)] +#[pyclass] +pub struct Segment { + #[pyo3(get)] + cmd: u32, + #[pyo3(get)] + cmdsize: u32, + #[pyo3(get)] + name: Option, + #[pyo3(get)] + vmaddr: u64, + #[pyo3(get)] + vmsize: u64, + #[pyo3(get)] + fileoff: u64, + #[pyo3(get)] + filesize: u64, + #[pyo3(get)] + maxprot: u32, + #[pyo3(get)] + initprot: u32, + #[pyo3(get)] + nsects: u32, + #[pyo3(get)] + flags: u32, +} + +#[pymethods] +impl Segment { + fn __repr__(&self) -> String { + format!("{:?}", self) + } +} + +impl From<&goblin::mach::segment::Segment<'_>> for Segment { + fn from(segm: &goblin::mach::segment::Segment) -> Self { + let segname = segm.name().ok().map(|s| s.to_string()); + Segment { + cmd: segm.cmd, + cmdsize: segm.cmdsize, + name: segname, + vmaddr: segm.vmaddr, + vmsize: segm.vmsize, + fileoff: segm.fileoff, + filesize: segm.filesize, + maxprot: segm.maxprot, + initprot: segm.initprot, + nsects: segm.nsects, + flags: segm.flags, + } + } +} diff --git a/test.py b/test.py index 30620ab..caaff38 100644 --- a/test.py +++ b/test.py @@ -27,6 +27,10 @@ print("imports") for imp in g.imports(): print(imp) +print("segments") +for segment in g.segments(): + print(f"{segment}") + print("sections") for section in g.sections(): print(f"{section}")