From d9c234510ed2d53f3e4e53a014dc5052c63f44bb Mon Sep 17 00:00:00 2001 From: Michael House Date: Mon, 4 Sep 2023 17:49:25 -0500 Subject: [PATCH 1/2] Updated version number in README getting started --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5db0561..750aeef 100644 --- a/README.md +++ b/README.md @@ -65,7 +65,7 @@ cargo add deaf Or by manually updating your cargo toml: ```bash -deaf = "0.1.0" +deaf = "0.1.2" ``` Then use it in your project like so: From a4613048b875a15aab6d915cf1e804f2e8de95cf Mon Sep 17 00:00:00 2001 From: Michael House Date: Mon, 4 Sep 2023 20:50:20 -0500 Subject: [PATCH 2/2] Modified RelocationInfo to handle 32 and 64 bit relocations --- src/errors.rs | 4 ++ src/tables/info/relocation.rs | 116 +++++++++++++++++++++------------ src/tables/items/relocation.rs | 2 +- 3 files changed, 78 insertions(+), 44 deletions(-) diff --git a/src/errors.rs b/src/errors.rs index 5c6e124..41bf1c4 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -33,6 +33,10 @@ pub enum Error { #[error("Given section is of the wrong type")] WrongSectionError, + /// Could not parse complex type from primitive + #[error("Could not convert from complex value")] + FromComplexError, + /// Could not parse complex type from primitive #[error("Could not convert from primitive value")] FromPrimitiveError(String), diff --git a/src/tables/info/relocation.rs b/src/tables/info/relocation.rs index 1066818..8edcbae 100644 --- a/src/tables/info/relocation.rs +++ b/src/tables/info/relocation.rs @@ -1,8 +1,8 @@ -use crate::errors::Result; +use crate::errors::{Result, Error}; use crate::common::Convert; /// Representation of the info field in a Relocation record -#[derive(Default,Clone,Copy,PartialEq)] +#[derive(Default,Debug,Clone,Copy,PartialEq)] pub struct RelocationInfo { symbol: u64, kind: u8, @@ -10,25 +10,9 @@ pub struct RelocationInfo { impl RelocationInfo { - /// Initialize an empty relocation info instance - pub fn empty() -> Self { - Self { - symbol: 0, - kind: 0 - } - } - /// Parse a combined value as an info struct - pub fn new(v: u64) -> Result { - Ok(Self { - symbol: v >> 8, - kind: v as u8, - }) - } - - /// Get the combined value of the info struct - pub fn value(&self) -> u64 { - (self.symbol << 8) + (self.kind as u64) + fn new(symbol: u64, kind: u8) -> Self { + Self { symbol, kind } } /// Get the 'symbol' component of the info struct @@ -36,35 +20,50 @@ impl RelocationInfo { self.symbol } + /// Set the 'symbol' component of the info struct + pub fn set_symbol(&mut self, value: u64) { + self.symbol = value; + } + /// Get the 'kind' component of the info struct pub fn kind(&self) -> u8 { self.kind } + /// Set the 'kind' component of the info struct + pub fn set_kind(&mut self, value: u8) { + self.kind = value; + } + } impl Convert for RelocationInfo { - fn convert(self) -> Result { Ok(self.value()) } + fn convert(self) -> Result { + Ok((self.symbol as u64) << 32 | self.kind as u64) + } } impl Convert for RelocationInfo { - fn convert(self) -> Result { Ok(self.value().try_into()?) } + fn convert(self) -> Result { + Ok((self.symbol as u32) << 8 | self.kind as u32) + } } impl Convert for u64 { - fn convert(self) -> Result { RelocationInfo::new(self) } + fn convert(self) -> Result { + Ok(RelocationInfo::new( + self >> 32, + self as u8) + ) + } } impl Convert for u32 { - fn convert(self) -> Result { RelocationInfo::new(self.into()) } -} - -impl std::fmt::Debug for RelocationInfo { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("RelocationInfo") - .field("symbol", &self.symbol()) - .field("kind", &self.kind()) - .finish() + fn convert(self) -> Result { + Ok(RelocationInfo::new( + (self as u64) >> 8, + self as u8) + ) } } @@ -73,29 +72,46 @@ mod tests { use super::*; #[test] - fn test_relocation_info_parse_pair() { + fn test_relocation_info_parse_pair_32() { + // original value (0xfe000000 + 0x06) + let value: u32 = 0xfe000006; + + // parse the relocation info from value + let result: Result = value.convert(); + + // unwrap the relocation result + assert!(result.is_ok()); + let info = result.unwrap(); + + // verify that fields have expected value + assert_eq!(info.symbol,0xfe0000); + assert_eq!(info.kind,0x06); + } + + #[test] + fn test_relocation_info_parse_pair_64() { // original value (0xfe000000 + 0x06) - let value = 0xfe00000006; + let value: u64 = 0xfe00000006; // parse the relocation info from value - let result = RelocationInfo::new(value); + let result: Result = value.convert(); // unwrap the relocation result assert!(result.is_ok()); let info = result.unwrap(); // verify that fields have expected value - assert_eq!(info.symbol,0xfe000000); + assert_eq!(info.symbol,0xfe); assert_eq!(info.kind,0x06); } #[test] fn test_relocation_info_parse_zeroes() { // original value - let value = 0x0000000000; + let value: u64 = 0x0000000000; // parse the relocation info from value - let result = RelocationInfo::new(value); + let result: Result = value.convert(); // unwrap the relocation result assert!(result.is_ok()); @@ -109,10 +125,10 @@ mod tests { #[test] fn test_relocation_info_back_to_zeroes() { // original value - let value = 0x0000000000; + let value: u64 = 0x0000000000; // parse the relocation info and then convert back - let info = RelocationInfo::new(value).unwrap(); + let info: RelocationInfo = value.convert().unwrap(); let result: Result = info.convert(); // verify that the result matches the original @@ -121,12 +137,26 @@ mod tests { } #[test] - fn test_relocation_info_back_to_value() { + fn test_relocation_info_back_to_value_32() { + // original value + let value: u32 = 0xfe000006; + + // parse the relocation info and then convert back + let info: RelocationInfo = value.convert().unwrap(); + let result: Result = info.convert(); + + // verify that the result matches the original + assert!(result.is_ok()); + assert_eq!(result.unwrap(),value); + } + + #[test] + fn test_relocation_info_back_to_value_64() { // original value - let value = 0xfe00000006; + let value: u64 = 0xfe00000006; // parse the relocation info and then convert back - let info = RelocationInfo::new(value).unwrap(); + let info: RelocationInfo = value.convert().unwrap(); let result: Result = info.convert(); // verify that the result matches the original diff --git a/src/tables/items/relocation.rs b/src/tables/items/relocation.rs index 0b8c163..7df4c30 100644 --- a/src/tables/items/relocation.rs +++ b/src/tables/items/relocation.rs @@ -1,5 +1,5 @@ use crate::common::{Width,Layout,Item,ranges::*}; -use crate::tables::info::RelocationInfo; +use crate::tables::info::{RelocationInfo}; use crate::tables::TableItem; use crate::errors::Result;