@@ -2,7 +2,7 @@ use prettytable::{row, Table};
22
33use crate :: constants:: * ;
44use crate :: header:: { MachHeader , MachHeader32 , MachHeader64 } ;
5- use crate :: load_commands:: { EncryptionInfoCommand , LoadCommand , RoutinesCommand , SegmentCommand } ;
5+ use crate :: load_commands:: { EncryptionInfoCommand , LoadCommand , RoutinesCommand , SegmentCommand , SegmentCommand32 , SegmentCommand64 } ;
66
77pub fn print_header ( header : & MachHeader ) {
88 let mut table = Table :: new ( ) ;
@@ -163,10 +163,10 @@ pub fn print_load_commands(load_commands: &Vec<LoadCommand>) {
163163 LoadCommand :: SegmentCommand ( command) => {
164164 match command {
165165 SegmentCommand :: SEG32 ( command) => {
166- print_lc_cmd_and_cmdsize ( command. cmd , command . cmdsize , & mut table) ;
166+ print_segment_command32 ( command, & mut table) ;
167167 }
168168 SegmentCommand :: SEG64 ( command) => {
169- print_lc_cmd_and_cmdsize ( command. cmd , command . cmdsize , & mut table) ;
169+ print_segment_command64 ( command, & mut table) ;
170170 }
171171 }
172172 }
@@ -268,6 +268,32 @@ pub fn print_load_commands(load_commands: &Vec<LoadCommand>) {
268268 table. printstd ( ) ;
269269}
270270
271+ fn print_segment_command32 ( command : & SegmentCommand32 , table : & mut Table ) {
272+ print_lc_cmd_and_cmdsize ( command. cmd , command. cmdsize , table) ;
273+ print_bytes_array ( & command. segname , table) ;
274+ table. add_row ( row ! [ Fcc ->"vmaddr" , Fyc ->format!( "0x{:x}" , command. vmaddr) , c->"-" ] ) ;
275+ table. add_row ( row ! [ Fcc ->"vmsize" , Fyc ->format!( "0x{:x}" , command. vmsize) , c->"-" ] ) ;
276+ table. add_row ( row ! [ Fcc ->"fileoff" , Fyc ->format!( "0x{:x}" , command. fileoff) , c->"-" ] ) ;
277+ table. add_row ( row ! [ Fcc ->"filesize" , Fyc ->format!( "0x{:x}" , command. filesize) , c->"-" ] ) ;
278+ table. add_row ( row ! [ Fcc ->"maxprot" , Fyc ->format!( "{}" , command. maxprot) , c->"-" ] ) ;
279+ table. add_row ( row ! [ Fcc ->"initprot" , Fyc ->format!( "{}" , command. initprot) , c->"-" ] ) ;
280+ table. add_row ( row ! [ Fcc ->"nsects" , Fyc ->format!( "0x{:x}" , command. nsects) , c->"-" ] ) ;
281+ table. add_row ( row ! [ Fcc ->"flags" , Fyc ->format!( "0x{:x}" , command. flags) , c->"-" ] ) ;
282+ }
283+
284+ fn print_segment_command64 ( command : & SegmentCommand64 , table : & mut Table ) {
285+ print_lc_cmd_and_cmdsize ( command. cmd , command. cmdsize , table) ;
286+ print_bytes_array ( & command. segname , table) ;
287+ table. add_row ( row ! [ Fcc ->"vmaddr" , Fyc ->format!( "0x{:x}" , command. vmaddr) , c->"-" ] ) ;
288+ table. add_row ( row ! [ Fcc ->"vmsize" , Fyc ->format!( "0x{:x}" , command. vmsize) , c->"-" ] ) ;
289+ table. add_row ( row ! [ Fcc ->"fileoff" , Fyc ->format!( "0x{:x}" , command. fileoff) , c->"-" ] ) ;
290+ table. add_row ( row ! [ Fcc ->"filesize" , Fyc ->format!( "0x{:x}" , command. filesize) , c->"-" ] ) ;
291+ table. add_row ( row ! [ Fcc ->"maxprot" , Fyc ->format!( "{}" , command. maxprot) , c->"-" ] ) ;
292+ table. add_row ( row ! [ Fcc ->"initprot" , Fyc ->format!( "{}" , command. initprot) , c->"-" ] ) ;
293+ table. add_row ( row ! [ Fcc ->"nsects" , Fyc ->format!( "0x{:x}" , command. nsects) , c->"-" ] ) ;
294+ table. add_row ( row ! [ Fcc ->"flags" , Fyc ->format!( "0x{:x}" , command. flags) , c->"-" ] ) ;
295+ }
296+
271297fn print_lc_cmd_and_cmdsize ( cmd : u32 , cmdsize : u32 , table : & mut Table ) {
272298 let cmd_string = match cmd {
273299 LC_SEGMENT => "LC_SEGMENT" ,
@@ -325,5 +351,23 @@ fn print_lc_cmd_and_cmdsize(cmd: u32, cmdsize: u32, table: &mut Table) {
325351 } ;
326352
327353 table. add_row ( row ! [ Fcc ->"cmd" , Fyc ->format!( "0x{:x}\n ({})" , cmd, cmd_string) , c->"-" ] ) ;
328- table. add_row ( row ! [ Fcc ->"cmdsize" , Fyc ->format!( "0x{:x}" , cmdsize) , c->"size of the load command in bytes " ] ) ;
354+ table. add_row ( row ! [ Fcc ->"cmdsize" , Fyc ->format!( "0x{:x}" , cmdsize) , c->"- " ] ) ;
329355}
356+
357+ fn print_bytes_array ( bytes : & [ u8 ] , table : & mut Table ) {
358+ let mut result = String :: from ( "[" ) ;
359+ for ( index, & byte) in bytes. iter ( ) . enumerate ( ) {
360+ if index % 4 == 0 && index != 0 {
361+ result. push_str ( "\n " ) ;
362+ }
363+ result. push_str ( & format ! ( "0x{:02X}" , byte) ) ;
364+ if index < bytes. len ( ) - 1 {
365+ result. push_str ( ", " ) ;
366+ }
367+ }
368+ result. push ( ']' ) ;
369+
370+ let as_string = String :: from_utf8 ( bytes. to_vec ( ) ) . unwrap ( ) ;
371+
372+ table. add_row ( row ! [ Fcc ->"segname" , Fyc ->format!( "{}" , result) , c->as_string] ) ;
373+ }
0 commit comments