Skip to content

Commit 34f2db3

Browse files
committed
feat: 添加 objcopy_elf 方法以处理 ELF 文件的剥离
1 parent 1361c0d commit 34f2db3

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ostool/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ license = "MIT OR Apache-2.0"
77
name = "ostool"
88
readme = "../README.md"
99
repository = "https://github.com/ZR233/ostool"
10-
version = "0.8.1"
10+
version = "0.8.2"
1111

1212
[[bin]]
1313
name = "ostool"

ostool/src/bin/cargo-osrun.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ async fn main() -> anyhow::Result<()> {
9797
};
9898

9999
app.set_elf_path(args.elf).await;
100+
app.objcopy_elf()?;
100101

101102
app.debug = args.no_run;
102103
if args.to_bin {

ostool/src/ctx.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,47 @@ impl AppContext {
7676
self.arch = Some(file.architecture())
7777
}
7878

79+
pub fn objcopy_elf(&mut self) -> anyhow::Result<PathBuf> {
80+
let elf_path = self
81+
.elf_path
82+
.as_ref()
83+
.ok_or(anyhow!("elf not exist"))?
84+
.canonicalize()?;
85+
86+
let stripped_elf_path = elf_path.with_file_name(
87+
elf_path
88+
.file_stem()
89+
.ok_or(anyhow!("Invalid file path"))?
90+
.to_string_lossy()
91+
.to_string()
92+
+ ".elf",
93+
);
94+
println!(
95+
"{}",
96+
format!(
97+
"Stripping ELF file...\r\n original elf: {}\r\n stripped elf: {}",
98+
elf_path.display(),
99+
stripped_elf_path.display()
100+
)
101+
.bold()
102+
.purple()
103+
);
104+
105+
let mut objcopy = self.command("rust-objcopy");
106+
107+
objcopy.arg(format!(
108+
"--binary-architecture={}",
109+
format!("{:?}", self.arch.unwrap()).to_lowercase()
110+
));
111+
objcopy.arg(&elf_path);
112+
objcopy.arg(&stripped_elf_path);
113+
114+
objcopy.run()?;
115+
self.elf_path = Some(stripped_elf_path.clone());
116+
117+
Ok(stripped_elf_path)
118+
}
119+
79120
pub fn objcopy_output_bin(&mut self) -> anyhow::Result<PathBuf> {
80121
if self.bin_path.is_some() {
81122
debug!("BIN file already exists: {:?}", self.bin_path);

0 commit comments

Comments
 (0)