File tree Expand file tree Collapse file tree 4 files changed +44
-2
lines changed
Expand file tree Collapse file tree 4 files changed +44
-2
lines changed Original file line number Diff line number Diff line change @@ -7,7 +7,7 @@ license = "MIT OR Apache-2.0"
77name = " ostool"
88readme = " ../README.md"
99repository = " https://github.com/ZR233/ostool"
10- version = " 0.8.1 "
10+ version = " 0.8.2 "
1111
1212[[bin ]]
1313name = " ostool"
Original file line number Diff line number Diff 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 {
Original file line number Diff line number Diff 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) ;
You can’t perform that action at this time.
0 commit comments