Skip to content

Commit 1589cb0

Browse files
author
ligma
committed
add more printing capabilities to load commands
1 parent f9f2b97 commit 1589cb0

File tree

2 files changed

+48
-5
lines changed

2 files changed

+48
-5
lines changed

src/constants.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,6 @@ pub const LC_SUB_LIBRARY: u32 = 0x15; // sub library
214214
pub const LC_TWOLEVEL_HINTS: u32 = 0x16; // two-level namespace lookup hints
215215
pub const LC_PREBIND_CKSUM: u32 = 0x17; // prebind checksum
216216

217-
// load a dynamically linked shared library that is allowed to be missing (all symbols are weak imported)
218217
pub const LC_LOAD_WEAK_DYLIB: u32 = 0x18 | LC_REQ_DYLD;
219218

220219
pub const LC_SEGMENT_64: u32 = 0x19; // 64-bit segment of this file to be mapped

src/printer.rs

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use prettytable::{row, Table};
22

33
use crate::constants::*;
44
use 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

77
pub 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+
271297
fn 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

Comments
 (0)