1
Fork 0

Iterate through a fat mach-o file to find the first single-arch mach-o part

This commit is contained in:
Jan-Erik Rediger 2023-12-11 15:54:30 +01:00
parent 1297ce2668
commit 53708c2bb4
2 changed files with 25 additions and 2 deletions

View file

@ -3,7 +3,7 @@ use std::{
io::Read,
};
use goblin::mach::Mach;
use goblin::mach::{Mach, SingleArch};
use pyo3::{exceptions::PyTypeError, prelude::*};
mod exports;
@ -57,6 +57,24 @@ impl Object {
let macho = match object {
goblin::Object::Mach(Mach::Binary(macho)) => macho,
goblin::Object::Mach(Mach::Fat(march)) => {
let mut macho = None;
for arch in &march {
let arch = arch.map_err(|_| {
PyErr::new::<PyTypeError, _>("cannot parse single arch from Mach-O file")
})?;
if let SingleArch::MachO(m) = arch {
macho = Some(m);
break;
}
}
match macho {
Some(macho) => macho,
None => return Err(PyErr::new::<PyTypeError, _>("not a macho file")),
}
}
_ => return Err(PyErr::new::<PyTypeError, _>("not a macho file")),
};

View file

@ -1,6 +1,11 @@
import sys
import oelf
g = oelf.Object("target/debug/liboelf.dylib")
path = "target/debug/liboelf.dylib"
if len(sys.argv) > 1:
path = sys.argv[1]
g = oelf.Object(path)
print(f"{g.header=}")
print(f"{g.name=}")