summaryrefslogtreecommitdiffstats
path: root/third_party/rust/goblin/examples/lipo.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 14:29:10 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 14:29:10 +0000
commit2aa4a82499d4becd2284cdb482213d541b8804dd (patch)
treeb80bf8bf13c3766139fbacc530efd0dd9d54394c /third_party/rust/goblin/examples/lipo.rs
parentInitial commit. (diff)
downloadfirefox-upstream.tar.xz
firefox-upstream.zip
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/goblin/examples/lipo.rs')
-rw-r--r--third_party/rust/goblin/examples/lipo.rs68
1 files changed, 68 insertions, 0 deletions
diff --git a/third_party/rust/goblin/examples/lipo.rs b/third_party/rust/goblin/examples/lipo.rs
new file mode 100644
index 0000000000..72cf7a5107
--- /dev/null
+++ b/third_party/rust/goblin/examples/lipo.rs
@@ -0,0 +1,68 @@
+use goblin::mach::{self, Mach};
+use std::env;
+use std::process;
+use std::path::Path;
+use std::fs::File;
+use std::io::{Read, Write};
+
+fn usage() -> ! {
+ println!("usage: lipo <options> <mach-o fat file>");
+ println!(" -m64 Extracts and writes the 64-bit binary in this fat container, if any");
+ process::exit(1);
+}
+
+fn main () {
+ let len = env::args().len();
+
+ if len <= 1 {
+ usage();
+ } else {
+ let mut m64 = false;
+ {
+ let mut flags = env::args().collect::<Vec<_>>();
+ flags.pop();
+ flags.remove(0);
+ for option in flags {
+ match option.as_str() {
+ "-m64" => { m64 = true }
+ other => {
+ println!("unknown flag: {}", other);
+ println!();
+ usage();
+ }
+ }
+ }
+ }
+
+ let path_name = env::args_os().last().unwrap();
+ let path = Path::new(&path_name);
+ let buffer = { let mut v = Vec::new(); let mut f = File::open(&path).unwrap(); f.read_to_end(&mut v).unwrap(); v};
+ match mach::Mach::parse(&buffer) {
+ Ok(Mach::Binary(_macho)) => {
+ println!("Already a single arch binary");
+ process::exit(2);
+ },
+ Ok(Mach::Fat(fat)) => {
+ for (i, arch) in fat.iter_arches().enumerate() {
+ let arch = arch.unwrap();
+ let name = format!("{}.{}", &path_name.to_string_lossy(), i);
+ let path = Path::new(&name);
+ if arch.is_64() && m64 {
+ let bytes = &buffer[arch.offset as usize..][..arch.size as usize];
+ let mut file = File::create(path).unwrap();
+ file.write_all(bytes).unwrap();
+ break;
+ } else if !m64 {
+ let bytes = &buffer[arch.offset as usize..][..arch.size as usize];
+ let mut file = File::create(path).unwrap();
+ file.write_all(bytes).unwrap();
+ }
+ }
+ },
+ Err(err) => {
+ println!("err: {:?}", err);
+ process::exit(2);
+ }
+ }
+ }
+}