From aac8c9b47bc15f4e93cae038cecd13a3818b50e8 Mon Sep 17 00:00:00 2001 From: _xeroxz <_xeroxz@back.engineer> Date: Wed, 28 Dec 2022 23:58:55 -0800 Subject: [PATCH 1/6] added a function to the symbol trait called "native_symbol" that allows you to get a copy of the underlying native symbol structure. --- src/read/any.rs | 7 +++++++ src/read/coff/symbol.rs | 10 +++++++++- src/read/elf/symbol.rs | 8 ++++++++ src/read/macho/symbol.rs | 8 ++++++++ src/read/traits.rs | 7 +++++++ 5 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/read/any.rs b/src/read/any.rs index c390b21b..6de97d4a 100644 --- a/src/read/any.rs +++ b/src/read/any.rs @@ -1196,6 +1196,13 @@ impl<'data, 'file, R: ReadRef<'data>> fmt::Debug for Symbol<'data, 'file, R> { impl<'data, 'file, R: ReadRef<'data>> read::private::Sealed for Symbol<'data, 'file, R> {} impl<'data, 'file, R: ReadRef<'data>> ObjectSymbol<'data> for Symbol<'data, 'file, R> { + + type NativeSymbolType = Option<()>; + + fn native_symbol(&self) -> Option<()> { + None + } + fn index(&self) -> SymbolIndex { with_inner!(self.inner, SymbolInternal, |x| x.0.index()) } diff --git a/src/read/coff/symbol.rs b/src/read/coff/symbol.rs index c954c8a2..11f622a2 100644 --- a/src/read/coff/symbol.rs +++ b/src/read/coff/symbol.rs @@ -5,7 +5,7 @@ use core::str; use super::{CoffCommon, SectionTable}; use crate::endian::{LittleEndian as LE, U32Bytes}; -use crate::pe; +use crate::pe::{self, ImageSymbol}; use crate::pod::{bytes_of_slice, Pod}; use crate::read::util::StringTable; use crate::read::{ @@ -328,6 +328,14 @@ where impl<'data, 'file, R: ReadRef<'data>> read::private::Sealed for CoffSymbol<'data, 'file, R> {} impl<'data, 'file, R: ReadRef<'data>> ObjectSymbol<'data> for CoffSymbol<'data, 'file, R> { + + type NativeSymbolType = ImageSymbol; + + #[inline] + fn native_symbol(&self) -> Self::NativeSymbolType { + *self.symbol + } + #[inline] fn index(&self) -> SymbolIndex { self.index diff --git a/src/read/elf/symbol.rs b/src/read/elf/symbol.rs index 390aa466..ce334882 100644 --- a/src/read/elf/symbol.rs +++ b/src/read/elf/symbol.rs @@ -320,6 +320,14 @@ impl<'data, 'file, Elf: FileHeader, R: ReadRef<'data>> read::private::Sealed impl<'data, 'file, Elf: FileHeader, R: ReadRef<'data>> ObjectSymbol<'data> for ElfSymbol<'data, 'file, Elf, R> { + + type NativeSymbolType = Elf::Sym; + + #[inline] + fn native_symbol(&self) -> Self::NativeSymbolType { + *self.symbol + } + #[inline] fn index(&self) -> SymbolIndex { self.index diff --git a/src/read/macho/symbol.rs b/src/read/macho/symbol.rs index e102c5d0..aa09feb3 100644 --- a/src/read/macho/symbol.rs +++ b/src/read/macho/symbol.rs @@ -282,6 +282,14 @@ where Mach: MachHeader, R: ReadRef<'data>, { + + type NativeSymbolType = Mach::Nlist; + + #[inline] + fn native_symbol(&self) -> Self::NativeSymbolType { + *self.nlist + } + #[inline] fn index(&self) -> SymbolIndex { self.index diff --git a/src/read/traits.rs b/src/read/traits.rs index f1a473e0..2d136222 100644 --- a/src/read/traits.rs +++ b/src/read/traits.rs @@ -395,6 +395,13 @@ pub trait ObjectSymbolTable<'data>: read::private::Sealed { /// A symbol table entry. pub trait ObjectSymbol<'data>: read::private::Sealed { + + /// native symbol type for object symbol + type NativeSymbolType; + + /// gets the underlying native symbol by value. + fn native_symbol(&self) -> Self::NativeSymbolType; + /// The index of the symbol. fn index(&self) -> SymbolIndex; From c3896f28949c6de236b07923a7fffd0edee1d1cb Mon Sep 17 00:00:00 2001 From: _xeroxz <_xeroxz@back.engineer> Date: Thu, 29 Dec 2022 00:07:29 -0800 Subject: [PATCH 2/6] updated symbols to optionally return a value... --- src/read/coff/symbol.rs | 4 ++-- src/read/elf/symbol.rs | 4 ++-- src/read/macho/symbol.rs | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/read/coff/symbol.rs b/src/read/coff/symbol.rs index 11f622a2..ea3348f5 100644 --- a/src/read/coff/symbol.rs +++ b/src/read/coff/symbol.rs @@ -329,11 +329,11 @@ impl<'data, 'file, R: ReadRef<'data>> read::private::Sealed for CoffSymbol<'data impl<'data, 'file, R: ReadRef<'data>> ObjectSymbol<'data> for CoffSymbol<'data, 'file, R> { - type NativeSymbolType = ImageSymbol; + type NativeSymbolType = Option; #[inline] fn native_symbol(&self) -> Self::NativeSymbolType { - *self.symbol + Some(*self.symbol) } #[inline] diff --git a/src/read/elf/symbol.rs b/src/read/elf/symbol.rs index ce334882..e2581d62 100644 --- a/src/read/elf/symbol.rs +++ b/src/read/elf/symbol.rs @@ -321,11 +321,11 @@ impl<'data, 'file, Elf: FileHeader, R: ReadRef<'data>> ObjectSymbol<'data> for ElfSymbol<'data, 'file, Elf, R> { - type NativeSymbolType = Elf::Sym; + type NativeSymbolType = Option; #[inline] fn native_symbol(&self) -> Self::NativeSymbolType { - *self.symbol + Some(*self.symbol) } #[inline] diff --git a/src/read/macho/symbol.rs b/src/read/macho/symbol.rs index aa09feb3..a6ab131d 100644 --- a/src/read/macho/symbol.rs +++ b/src/read/macho/symbol.rs @@ -283,11 +283,11 @@ where R: ReadRef<'data>, { - type NativeSymbolType = Mach::Nlist; + type NativeSymbolType = Option; #[inline] fn native_symbol(&self) -> Self::NativeSymbolType { - *self.nlist + Some(*self.nlist) } #[inline] From 076bb3ac2c8ea24614068bb4c82710600089f605 Mon Sep 17 00:00:00 2001 From: _xeroxz <_xeroxz@back.engineer> Date: Thu, 29 Dec 2022 00:33:31 -0800 Subject: [PATCH 3/6] removed native_symbol function and added raw_symbol. I still need to add test code before merging... --- src/read/any.rs | 7 ------- src/read/coff/symbol.rs | 17 ++++++++--------- src/read/elf/symbol.rs | 8 -------- src/read/macho/symbol.rs | 8 -------- src/read/traits.rs | 7 ------- 5 files changed, 8 insertions(+), 39 deletions(-) diff --git a/src/read/any.rs b/src/read/any.rs index 6de97d4a..c390b21b 100644 --- a/src/read/any.rs +++ b/src/read/any.rs @@ -1196,13 +1196,6 @@ impl<'data, 'file, R: ReadRef<'data>> fmt::Debug for Symbol<'data, 'file, R> { impl<'data, 'file, R: ReadRef<'data>> read::private::Sealed for Symbol<'data, 'file, R> {} impl<'data, 'file, R: ReadRef<'data>> ObjectSymbol<'data> for Symbol<'data, 'file, R> { - - type NativeSymbolType = Option<()>; - - fn native_symbol(&self) -> Option<()> { - None - } - fn index(&self) -> SymbolIndex { with_inner!(self.inner, SymbolInternal, |x| x.0.index()) } diff --git a/src/read/coff/symbol.rs b/src/read/coff/symbol.rs index ea3348f5..c990e40c 100644 --- a/src/read/coff/symbol.rs +++ b/src/read/coff/symbol.rs @@ -5,7 +5,7 @@ use core::str; use super::{CoffCommon, SectionTable}; use crate::endian::{LittleEndian as LE, U32Bytes}; -use crate::pe::{self, ImageSymbol}; +use crate::pe; use crate::pod::{bytes_of_slice, Pod}; use crate::read::util::StringTable; use crate::read::{ @@ -325,17 +325,16 @@ where pub(crate) symbol: &'data pe::ImageSymbol, } -impl<'data, 'file, R: ReadRef<'data>> read::private::Sealed for CoffSymbol<'data, 'file, R> {} - -impl<'data, 'file, R: ReadRef<'data>> ObjectSymbol<'data> for CoffSymbol<'data, 'file, R> { - - type NativeSymbolType = Option; - +impl<'data, 'file, R: ReadRef<'data>> CoffSymbol<'data, 'file, R> { #[inline] - fn native_symbol(&self) -> Self::NativeSymbolType { - Some(*self.symbol) + fn raw_symbol(&self) -> pe::ImageSymbol { + *self.symbol } +} + +impl<'data, 'file, R: ReadRef<'data>> read::private::Sealed for CoffSymbol<'data, 'file, R> {} +impl<'data, 'file, R: ReadRef<'data>> ObjectSymbol<'data> for CoffSymbol<'data, 'file, R> { #[inline] fn index(&self) -> SymbolIndex { self.index diff --git a/src/read/elf/symbol.rs b/src/read/elf/symbol.rs index e2581d62..390aa466 100644 --- a/src/read/elf/symbol.rs +++ b/src/read/elf/symbol.rs @@ -320,14 +320,6 @@ impl<'data, 'file, Elf: FileHeader, R: ReadRef<'data>> read::private::Sealed impl<'data, 'file, Elf: FileHeader, R: ReadRef<'data>> ObjectSymbol<'data> for ElfSymbol<'data, 'file, Elf, R> { - - type NativeSymbolType = Option; - - #[inline] - fn native_symbol(&self) -> Self::NativeSymbolType { - Some(*self.symbol) - } - #[inline] fn index(&self) -> SymbolIndex { self.index diff --git a/src/read/macho/symbol.rs b/src/read/macho/symbol.rs index a6ab131d..e102c5d0 100644 --- a/src/read/macho/symbol.rs +++ b/src/read/macho/symbol.rs @@ -282,14 +282,6 @@ where Mach: MachHeader, R: ReadRef<'data>, { - - type NativeSymbolType = Option; - - #[inline] - fn native_symbol(&self) -> Self::NativeSymbolType { - Some(*self.nlist) - } - #[inline] fn index(&self) -> SymbolIndex { self.index diff --git a/src/read/traits.rs b/src/read/traits.rs index 2d136222..f1a473e0 100644 --- a/src/read/traits.rs +++ b/src/read/traits.rs @@ -395,13 +395,6 @@ pub trait ObjectSymbolTable<'data>: read::private::Sealed { /// A symbol table entry. pub trait ObjectSymbol<'data>: read::private::Sealed { - - /// native symbol type for object symbol - type NativeSymbolType; - - /// gets the underlying native symbol by value. - fn native_symbol(&self) -> Self::NativeSymbolType; - /// The index of the symbol. fn index(&self) -> SymbolIndex; From 28123a44b54a90cbad8dd8c43b53c4e43caf229c Mon Sep 17 00:00:00 2001 From: _xeroxz <_xeroxz@back.engineer> Date: Thu, 29 Dec 2022 00:46:00 -0800 Subject: [PATCH 4/6] added test code, works, ready to merge --- src/read/coff/symbol.rs | 3 ++- tests/read/coff.rs | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/read/coff/symbol.rs b/src/read/coff/symbol.rs index c990e40c..3b675a56 100644 --- a/src/read/coff/symbol.rs +++ b/src/read/coff/symbol.rs @@ -327,7 +327,8 @@ where impl<'data, 'file, R: ReadRef<'data>> CoffSymbol<'data, 'file, R> { #[inline] - fn raw_symbol(&self) -> pe::ImageSymbol { + /// get the raw ImageSymbol struct. + pub fn raw_symbol(&self) -> pe::ImageSymbol { *self.symbol } } diff --git a/tests/read/coff.rs b/tests/read/coff.rs index 3e61ec28..40863da3 100644 --- a/tests/read/coff.rs +++ b/tests/read/coff.rs @@ -21,3 +21,20 @@ fn coff_extended_relocations() { let relocations = code_section.relocations().collect::>(); assert_eq!(relocations.len(), 65536); } + + +#[cfg(feature = "coff")] +#[test] +fn coff_symbol_raw_test() { + let path_to_obj: PathBuf = ["testfiles", "coff", "relocs_overflow.o"].iter().collect(); + let contents = fs::read(&path_to_obj).expect("Could not read relocs_overflow.o"); + let file = + read::coff::CoffFile::parse(&contents[..]).expect("Could not parse relocs_overflow.o"); + + for sym in file.symbols() { + let raw_sym = sym.raw_symbol(); + raw_sym.derived_type(); + raw_sym.storage_class; + // cool it works :) + } +} \ No newline at end of file From 2b97362cae191b3ebb62896400cb4e760271cca3 Mon Sep 17 00:00:00 2001 From: _xeroxz <_xeroxz@back.engineer> Date: Mon, 2 Jan 2023 19:52:52 -0800 Subject: [PATCH 5/6] applied requested changes --- src/read/coff/symbol.rs | 6 +++--- tests/read/coff.rs | 17 ----------------- 2 files changed, 3 insertions(+), 20 deletions(-) diff --git a/src/read/coff/symbol.rs b/src/read/coff/symbol.rs index 3b675a56..217e38fc 100644 --- a/src/read/coff/symbol.rs +++ b/src/read/coff/symbol.rs @@ -327,9 +327,9 @@ where impl<'data, 'file, R: ReadRef<'data>> CoffSymbol<'data, 'file, R> { #[inline] - /// get the raw ImageSymbol struct. - pub fn raw_symbol(&self) -> pe::ImageSymbol { - *self.symbol + /// Get the raw `ImageSymbol` struct. + pub fn raw_symbol(&self) -> &'data pe::ImageSymbol { + self.symbol } } diff --git a/tests/read/coff.rs b/tests/read/coff.rs index 40863da3..7ef987ff 100644 --- a/tests/read/coff.rs +++ b/tests/read/coff.rs @@ -20,21 +20,4 @@ fn coff_extended_relocations() { }; let relocations = code_section.relocations().collect::>(); assert_eq!(relocations.len(), 65536); -} - - -#[cfg(feature = "coff")] -#[test] -fn coff_symbol_raw_test() { - let path_to_obj: PathBuf = ["testfiles", "coff", "relocs_overflow.o"].iter().collect(); - let contents = fs::read(&path_to_obj).expect("Could not read relocs_overflow.o"); - let file = - read::coff::CoffFile::parse(&contents[..]).expect("Could not parse relocs_overflow.o"); - - for sym in file.symbols() { - let raw_sym = sym.raw_symbol(); - raw_sym.derived_type(); - raw_sym.storage_class; - // cool it works :) - } } \ No newline at end of file From 63ba849b09690336d6df1b7e413d945ad25fbd14 Mon Sep 17 00:00:00 2001 From: _xeroxz <_xeroxz@back.engineer> Date: Mon, 2 Jan 2023 23:46:27 -0800 Subject: [PATCH 6/6] ran cargo fmt on the project --- tests/read/coff.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/read/coff.rs b/tests/read/coff.rs index 7ef987ff..3e61ec28 100644 --- a/tests/read/coff.rs +++ b/tests/read/coff.rs @@ -20,4 +20,4 @@ fn coff_extended_relocations() { }; let relocations = code_section.relocations().collect::>(); assert_eq!(relocations.len(), 65536); -} \ No newline at end of file +}