From abebd9cc9ebdd46fc1f1eb3d705372dd8a1457d8 Mon Sep 17 00:00:00 2001 From: tzlil Date: Wed, 22 Feb 2023 00:10:29 +0200 Subject: add readelf.c style padding --- src/structs.rs | 43 +++++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/src/structs.rs b/src/structs.rs index f5eb7ea..850a47c 100644 --- a/src/structs.rs +++ b/src/structs.rs @@ -25,21 +25,23 @@ pub struct ElfIdent { impl std::fmt::Display for ElfIdent { fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { - formatter.write_str("Magic:\t"); + formatter.write_str(" Magic: "); - for b in unsafe {*(std::ptr::addr_of!(self) as *const [u8; 16])} { + for b in unsafe {*(std::ptr::addr_of!(self.magic) as *const [u8; 16])} { formatter.pad(&format!("{:02x?} ", b)); } - formatter.write_fmt(format_args!("\nClass:\t{:?}", self.class)); - formatter.write_fmt(format_args!("\nData:\t{}", self.data)); - formatter.write_fmt(format_args!("\nVersion:\t{} ({})", self.version, match self.version { + formatter.write_str("\n"); + + formatter.write_fmt(format_args!(" Class: {:?}\n", self.class)); + formatter.write_fmt(format_args!(" Data: {}\n", self.data)); + formatter.write_fmt(format_args!(" Version: {} ({})\n", self.version, match self.version { 0 => "invalid", 1 => "current", 2_u8..=u8::MAX => todo!(), // need to cover all cases })); - formatter.write_fmt(format_args!("\nOS/ABI:\t{}", self.OSABI)); - formatter.write_fmt(format_args!("\nABI Verscion:\t{}", self.ABIVersion)) + formatter.write_fmt(format_args!(" OS/ABI: {}\n", self.OSABI)); + formatter.write_fmt(format_args!(" ABI Version: {}\n", self.ABIVersion)) } } @@ -94,20 +96,21 @@ pub struct ElfHeader { impl std::fmt::Display for ElfHeader { fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { + // yes, this is really what they do in readelf.c formatter.write_fmt(format_args!("{}", self.ident)); - formatter.write_fmt(format_args!("\nType:\t{}", self.r#type)); - formatter.write_fmt(format_args!("\nMachine:\t{}", self.machine)); - formatter.write_fmt(format_args!("\nVersion:\t{:#02x?}", self.version)); - formatter.write_fmt(format_args!("\nEntry point address:\t{:#x?}", self.entry)); - formatter.write_fmt(format_args!("\nStart of program headers:\t{} (bytes into file)", self.phoff)); - formatter.write_fmt(format_args!("\nStart of section headers:\t{} (bytes into file)", self.shoff)); - formatter.write_fmt(format_args!("\nFlags:\t{:#x?}", self.flags)); - formatter.write_fmt(format_args!("\nSize of this header:\t{} (bytes)", self.ehsize)); - formatter.write_fmt(format_args!("\nSize of program headers:\t{} (bytes)", self.phentsize)); - formatter.write_fmt(format_args!("\nNumber of program headers:\t{}", self.phnum)); - formatter.write_fmt(format_args!("\nSize of section headers:\t{} (bytes)", self.shentsize)); - formatter.write_fmt(format_args!("\nNumber of section headers:\t{}", self.shnum)); - formatter.write_fmt(format_args!("\nSection header string table index:\t{}", self.shstrndx)) + formatter.write_fmt(format_args!(" Type: {}\n", self.r#type)); + formatter.write_fmt(format_args!(" Machine: {}\n", self.machine)); + formatter.write_fmt(format_args!(" Version: {:#02x?}\n", self.version)); + formatter.write_fmt(format_args!(" Entry point address: {:#x?}\n", self.entry)); + formatter.write_fmt(format_args!(" Start of program headers: {} (bytes into file)\n", self.phoff)); + formatter.write_fmt(format_args!(" Start of section headers: {} (bytes into file)\n", self.shoff)); + formatter.write_fmt(format_args!(" Flags: {:#x?}\n", self.flags)); + formatter.write_fmt(format_args!(" Size of this header: {} (bytes)\n", self.ehsize)); + formatter.write_fmt(format_args!(" Size of program headers: {} (bytes)\n", self.phentsize)); + formatter.write_fmt(format_args!(" Number of program headers: {}\n", self.phnum)); + formatter.write_fmt(format_args!(" Size of section headers: {} (bytes)\n", self.shentsize)); + formatter.write_fmt(format_args!(" Number of section headers: {}\n", self.shnum)); + formatter.write_fmt(format_args!(" Section header string table index: {}\n", self.shstrndx)) } } -- cgit 1.4.1