1
Fork 0

Expose segments

This commit is contained in:
Jan-Erik Rediger 2023-12-11 16:30:07 +01:00
parent 53708c2bb4
commit 6dba4064f2
4 changed files with 76 additions and 3 deletions

View file

@ -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<Segment> {
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 }

View file

@ -3,6 +3,8 @@ use pyo3::prelude::*;
#[derive(Debug, Clone)]
#[pyclass]
pub struct Section {
#[pyo3(get)]
index: usize,
#[pyo3(get)]
name: Option<String>,
#[pyo3(get)]
@ -30,9 +32,10 @@ impl Section {
}
}
impl From<goblin::mach::segment::Section> 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,

54
src/segments.rs Normal file
View file

@ -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<String>,
#[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,
}
}
}

View file

@ -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}")