From 3849b8b7742aa14644faada8427e6bb80eb282ce Mon Sep 17 00:00:00 2001 From: Philip Craig Date: Fri, 20 Mar 2026 12:31:43 +1000 Subject: [PATCH] Always support unaligned reads (#861) The "unaligned" feature now has no effect. --- Cargo.toml | 21 +- crates/examples/src/bin/elftoefi.rs | 4 +- crates/examples/src/readobj/pe.rs | 6 +- src/elf.rs | 16 +- src/endian.rs | 572 +++++----------------------- src/macho.rs | 5 +- src/pe.rs | 74 ++-- src/read/archive.rs | 42 +- src/read/coff/symbol.rs | 4 +- src/read/elf/attributes.rs | 4 +- src/read/elf/comdat.rs | 6 +- src/read/elf/section.rs | 6 +- src/read/gnu_compression.rs | 4 +- src/read/macho/file.rs | 4 +- src/read/pe/export.rs | 20 +- src/read/pe/import.rs | 6 +- src/read/pe/resource.rs | 11 +- src/read/read_ref.rs | 3 +- src/read/xcoff/symbol.rs | 4 +- src/write/macho.rs | 2 +- xtask/src/main.rs | 1 - 21 files changed, 205 insertions(+), 610 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 2eb91d5..f54ae07 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -43,8 +43,8 @@ alloc = { version = '1.0.0', optional = true, package = 'rustc-std-workspace-all # Core read support. You will need to enable some file formats too. read_core = [] -# Read support for most file formats (including unaligned files). -read = ["read_core", "archive", "coff", "elf", "macho", "pe", "xcoff", "unaligned"] +# Read support for most file formats. +read = ["read_core", "archive", "coff", "elf", "macho", "pe", "xcoff"] # Core write support. You will need to enable some file formats too. write_core = ["dep:crc32fast", "dep:indexmap", "dep:hashbrown"] # Core write support with libstd features. You will need to enable some file formats too. @@ -65,12 +65,6 @@ std = ["memchr/std"] # Enable decompression of compressed sections. # This feature is not required if you want to do your own decompression. compression = ["dep:flate2", "dep:ruzstd", "std"] -# Treat all types as unaligned. -# Normally types use the alignment required by the specifications, but -# sometimes files do not strictly follow the specifications. -# This may be useful to enable when processing files for architectures -# that have no alignment constraints. -unaligned = [] #======================================= # File format features. @@ -96,12 +90,11 @@ all = ["read", "write", "build", "std", "compression", "wasm"] cargo-all = [] #======================================= -# Documentation should be generated with everything in "all" except for "unaligned". -doc = [ - "read_core", "write_std", "build_core", - "std", "compression", - "archive", "coff", "elf", "macho", "pe", "wasm", "xcoff", -] +doc = ["all"] + +#======================================= +# Deprecated features. +unaligned = [] #======================================= # Unstable features. Breaking changes in these features will not affect versioning. diff --git a/crates/examples/src/bin/elftoefi.rs b/crates/examples/src/bin/elftoefi.rs index 2b3ed81..5bb5bda 100644 --- a/crates/examples/src/bin/elftoefi.rs +++ b/crates/examples/src/bin/elftoefi.rs @@ -236,7 +236,7 @@ fn copy_file>( // This relocation is paired with a R_RISCV_PCREL_LO12_I. let info_offset = u64::from(r_offset).wrapping_sub(info_addr); let instruction = info_data - .read_at::>(info_offset) + .read_at::>(info_offset) .unwrap() .get(endian); // auipc @@ -249,7 +249,7 @@ fn copy_file>( if let Some(mut got_address) = got_address.take() { let info_offset = u64::from(r_offset).wrapping_sub(info_addr); let instruction = info_data - .read_at::>(info_offset) + .read_at::>(info_offset) .unwrap() .get(endian); // ld diff --git a/crates/examples/src/readobj/pe.rs b/crates/examples/src/readobj/pe.rs index 5e07dff..7f145de 100644 --- a/crates/examples/src/readobj/pe.rs +++ b/crates/examples/src/readobj/pe.rs @@ -4,7 +4,7 @@ use object::read::coff::ImageSymbol as _; use object::read::coff::*; use object::read::pe::*; use object::LittleEndian as LE; -use object::{Bytes, U32Bytes, U64Bytes}; +use object::{Bytes, U32, U64}; pub(super) fn print_coff(p: &mut Printer<'_>, data: &[u8]) { let mut offset = 0; @@ -822,10 +822,10 @@ fn print_reloc_dir( let offset = (reloc.virtual_address - block_address) as usize; if let Some(addend) = match reloc.typ { IMAGE_REL_BASED_HIGHLOW => block_data - .and_then(|data| data.read_at::>(offset).ok()) + .and_then(|data| data.read_at::>(offset).ok()) .map(|addend| u64::from(addend.get(LE))), IMAGE_REL_BASED_DIR64 => block_data - .and_then(|data| data.read_at::>(offset).ok()) + .and_then(|data| data.read_at::>(offset).ok()) .map(|addend| addend.get(LE)), _ => None, } { diff --git a/src/elf.rs b/src/elf.rs index 81a8f9d..f474573 100644 --- a/src/elf.rs +++ b/src/elf.rs @@ -8,7 +8,7 @@ #![allow(missing_docs)] #![allow(clippy::identity_op)] -use crate::endian::{Endian, U32Bytes, U64Bytes, I32, I64, U16, U32, U64}; +use crate::endian::{Endian, I32, I64, U16, U32, U64}; use crate::pod::Pod; /// The header at the start of every 32-bit ELF file. @@ -804,11 +804,11 @@ pub const SHF_EXCLUDE: u32 = 0x8000_0000; #[repr(C)] pub struct CompressionHeader32 { /// Compression format. One of the `ELFCOMPRESS_*` values. - pub ch_type: U32Bytes, + pub ch_type: U32, /// Uncompressed data size. - pub ch_size: U32Bytes, + pub ch_size: U32, /// Uncompressed data alignment. - pub ch_addralign: U32Bytes, + pub ch_addralign: U32, } /// Section compression header. @@ -821,13 +821,13 @@ pub struct CompressionHeader32 { #[repr(C)] pub struct CompressionHeader64 { /// Compression format. One of the `ELFCOMPRESS_*` values. - pub ch_type: U32Bytes, + pub ch_type: U32, /// Reserved. - pub ch_reserved: U32Bytes, + pub ch_reserved: U32, /// Uncompressed data size. - pub ch_size: U64Bytes, + pub ch_size: U64, /// Uncompressed data alignment. - pub ch_addralign: U64Bytes, + pub ch_addralign: U64, } /// ZLIB/DEFLATE algorithm. diff --git a/src/endian.rs b/src/endian.rs index bba16be..8c064e9 100644 --- a/src/endian.rs +++ b/src/endian.rs @@ -32,69 +32,9 @@ pub trait Endian: Debug + Default + Clone + Copy + PartialEq + Eq + 'static { !self.is_big_endian() } - /// Converts an unsigned 16 bit integer to native endian. - #[inline] - fn read_u16(self, n: u16) -> u16 { - if self.is_big_endian() { - u16::from_be(n) - } else { - u16::from_le(n) - } - } - - /// Converts an unsigned 32 bit integer to native endian. - #[inline] - fn read_u32(self, n: u32) -> u32 { - if self.is_big_endian() { - u32::from_be(n) - } else { - u32::from_le(n) - } - } - - /// Converts an unsigned 64 bit integer to native endian. - #[inline] - fn read_u64(self, n: u64) -> u64 { - if self.is_big_endian() { - u64::from_be(n) - } else { - u64::from_le(n) - } - } - - /// Converts a signed 16 bit integer to native endian. - #[inline] - fn read_i16(self, n: i16) -> i16 { - if self.is_big_endian() { - i16::from_be(n) - } else { - i16::from_le(n) - } - } - - /// Converts a signed 32 bit integer to native endian. - #[inline] - fn read_i32(self, n: i32) -> i32 { - if self.is_big_endian() { - i32::from_be(n) - } else { - i32::from_le(n) - } - } - - /// Converts a signed 64 bit integer to native endian. - #[inline] - fn read_i64(self, n: i64) -> i64 { - if self.is_big_endian() { - i64::from_be(n) - } else { - i64::from_le(n) - } - } - /// Converts an unaligned unsigned 16 bit integer to native endian. #[inline] - fn read_u16_bytes(self, n: [u8; 2]) -> u16 { + fn read_u16(self, n: [u8; 2]) -> u16 { if self.is_big_endian() { u16::from_be_bytes(n) } else { @@ -104,7 +44,7 @@ pub trait Endian: Debug + Default + Clone + Copy + PartialEq + Eq + 'static { /// Converts an unaligned unsigned 32 bit integer to native endian. #[inline] - fn read_u32_bytes(self, n: [u8; 4]) -> u32 { + fn read_u32(self, n: [u8; 4]) -> u32 { if self.is_big_endian() { u32::from_be_bytes(n) } else { @@ -114,7 +54,7 @@ pub trait Endian: Debug + Default + Clone + Copy + PartialEq + Eq + 'static { /// Converts an unaligned unsigned 64 bit integer to native endian. #[inline] - fn read_u64_bytes(self, n: [u8; 8]) -> u64 { + fn read_u64(self, n: [u8; 8]) -> u64 { if self.is_big_endian() { u64::from_be_bytes(n) } else { @@ -124,7 +64,7 @@ pub trait Endian: Debug + Default + Clone + Copy + PartialEq + Eq + 'static { /// Converts an unaligned signed 16 bit integer to native endian. #[inline] - fn read_i16_bytes(self, n: [u8; 2]) -> i16 { + fn read_i16(self, n: [u8; 2]) -> i16 { if self.is_big_endian() { i16::from_be_bytes(n) } else { @@ -134,7 +74,7 @@ pub trait Endian: Debug + Default + Clone + Copy + PartialEq + Eq + 'static { /// Converts an unaligned signed 32 bit integer to native endian. #[inline] - fn read_i32_bytes(self, n: [u8; 4]) -> i32 { + fn read_i32(self, n: [u8; 4]) -> i32 { if self.is_big_endian() { i32::from_be_bytes(n) } else { @@ -144,7 +84,7 @@ pub trait Endian: Debug + Default + Clone + Copy + PartialEq + Eq + 'static { /// Converts an unaligned signed 64 bit integer to native endian. #[inline] - fn read_i64_bytes(self, n: [u8; 8]) -> i64 { + fn read_i64(self, n: [u8; 8]) -> i64 { if self.is_big_endian() { i64::from_be_bytes(n) } else { @@ -152,69 +92,9 @@ pub trait Endian: Debug + Default + Clone + Copy + PartialEq + Eq + 'static { } } - /// Converts an unsigned 16 bit integer from native endian. - #[inline] - fn write_u16(self, n: u16) -> u16 { - if self.is_big_endian() { - u16::to_be(n) - } else { - u16::to_le(n) - } - } - - /// Converts an unsigned 32 bit integer from native endian. - #[inline] - fn write_u32(self, n: u32) -> u32 { - if self.is_big_endian() { - u32::to_be(n) - } else { - u32::to_le(n) - } - } - - /// Converts an unsigned 64 bit integer from native endian. - #[inline] - fn write_u64(self, n: u64) -> u64 { - if self.is_big_endian() { - u64::to_be(n) - } else { - u64::to_le(n) - } - } - - /// Converts a signed 16 bit integer from native endian. - #[inline] - fn write_i16(self, n: i16) -> i16 { - if self.is_big_endian() { - i16::to_be(n) - } else { - i16::to_le(n) - } - } - - /// Converts a signed 32 bit integer from native endian. - #[inline] - fn write_i32(self, n: i32) -> i32 { - if self.is_big_endian() { - i32::to_be(n) - } else { - i32::to_le(n) - } - } - - /// Converts a signed 64 bit integer from native endian. - #[inline] - fn write_i64(self, n: i64) -> i64 { - if self.is_big_endian() { - i64::to_be(n) - } else { - i64::to_le(n) - } - } - /// Converts an unaligned unsigned 16 bit integer from native endian. #[inline] - fn write_u16_bytes(self, n: u16) -> [u8; 2] { + fn write_u16(self, n: u16) -> [u8; 2] { if self.is_big_endian() { u16::to_be_bytes(n) } else { @@ -224,7 +104,7 @@ pub trait Endian: Debug + Default + Clone + Copy + PartialEq + Eq + 'static { /// Converts an unaligned unsigned 32 bit integer from native endian. #[inline] - fn write_u32_bytes(self, n: u32) -> [u8; 4] { + fn write_u32(self, n: u32) -> [u8; 4] { if self.is_big_endian() { u32::to_be_bytes(n) } else { @@ -234,7 +114,7 @@ pub trait Endian: Debug + Default + Clone + Copy + PartialEq + Eq + 'static { /// Converts an unaligned unsigned 64 bit integer from native endian. #[inline] - fn write_u64_bytes(self, n: u64) -> [u8; 8] { + fn write_u64(self, n: u64) -> [u8; 8] { if self.is_big_endian() { u64::to_be_bytes(n) } else { @@ -244,7 +124,7 @@ pub trait Endian: Debug + Default + Clone + Copy + PartialEq + Eq + 'static { /// Converts an unaligned signed 16 bit integer from native endian. #[inline] - fn write_i16_bytes(self, n: i16) -> [u8; 2] { + fn write_i16(self, n: i16) -> [u8; 2] { if self.is_big_endian() { i16::to_be_bytes(n) } else { @@ -254,7 +134,7 @@ pub trait Endian: Debug + Default + Clone + Copy + PartialEq + Eq + 'static { /// Converts an unaligned signed 32 bit integer from native endian. #[inline] - fn write_i32_bytes(self, n: i32) -> [u8; 4] { + fn write_i32(self, n: i32) -> [u8; 4] { if self.is_big_endian() { i32::to_be_bytes(n) } else { @@ -264,7 +144,7 @@ pub trait Endian: Debug + Default + Clone + Copy + PartialEq + Eq + 'static { /// Converts an unaligned signed 64 bit integer from native endian. #[inline] - fn write_i64_bytes(self, n: i64) -> [u8; 8] { + fn write_i64(self, n: i64) -> [u8; 8] { if self.is_big_endian() { i64::to_be_bytes(n) } else { @@ -406,306 +286,36 @@ macro_rules! unsafe_impl_endian_pod { } } -#[cfg(not(feature = "unaligned"))] -mod aligned { - use super::{fmt, Endian, FixedEndian, PhantomData, Pod}; +/// An unaligned `u16` value with an externally specified endianness of type `E`. +#[deprecated] +pub type U16Bytes = U16; - /// A `u16` value with an externally specified endianness of type `E`. - #[derive(Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] - #[repr(transparent)] - pub struct U16(u16, PhantomData); +/// An unaligned `u32` value with an externally specified endianness of type `E`. +#[deprecated] +pub type U32Bytes = U32; - impl U16 { - /// Construct a new value given bytes that already have the required endianness. - pub const fn from_bytes(n: [u8; 2]) -> Self { - Self(u16::from_ne_bytes(n), PhantomData) - } +/// An unaligned `u64` value with an externally specified endianness of type `E`. +#[deprecated] +pub type U64Bytes = U64; - /// Construct a new value given a native endian value. - pub fn new(e: E, n: u16) -> Self { - Self(e.write_u16(n), PhantomData) - } +/// An unaligned `i16` value with an externally specified endianness of type `E`. +#[deprecated] +pub type I16Bytes = I16; - /// Return the value as a native endian value. - pub fn get(self, e: E) -> u16 { - e.read_u16(self.0) - } +/// An unaligned `i32` value with an externally specified endianness of type `E`. +#[deprecated] +pub type I32Bytes = I32; - /// Set the value given a native endian value. - pub fn set(&mut self, e: E, n: u16) { - self.0 = e.write_u16(n); - } - } - - /// A `u32` value with an externally specified endianness of type `E`. - #[derive(Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] - #[repr(transparent)] - pub struct U32(u32, PhantomData); - - impl U32 { - /// Construct a new value given bytes that already have the required endianness. - pub const fn from_bytes(n: [u8; 4]) -> Self { - Self(u32::from_ne_bytes(n), PhantomData) - } - - /// Construct a new value given a native endian value. - pub fn new(e: E, n: u32) -> Self { - Self(e.write_u32(n), PhantomData) - } - /// Return the value as a native endian value. - pub fn get(self, e: E) -> u32 { - e.read_u32(self.0) - } - /// Set the value given a native endian value. - pub fn set(&mut self, e: E, n: u32) { - self.0 = e.write_u32(n); - } - } - - /// A `u64` value with an externally specified endianness of type `E`. - #[derive(Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] - #[repr(transparent)] - pub struct U64(u64, PhantomData); - - impl U64 { - /// Construct a new value given bytes that already have the required endianness. - pub const fn from_bytes(n: [u8; 8]) -> Self { - Self(u64::from_ne_bytes(n), PhantomData) - } - - /// Construct a new value given a native endian value. - pub fn new(e: E, n: u64) -> Self { - Self(e.write_u64(n), PhantomData) - } - /// Return the value as a native endian value. - pub fn get(self, e: E) -> u64 { - e.read_u64(self.0) - } - /// Set the value given a native endian value. - pub fn set(&mut self, e: E, n: u64) { - self.0 = e.write_u64(n); - } - } - - /// An `i16` value with an externally specified endianness of type `E`. - #[derive(Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] - #[repr(transparent)] - pub struct I16(i16, PhantomData); - - impl I16 { - /// Construct a new value given bytes that already have the required endianness. - pub const fn from_bytes(n: [u8; 2]) -> Self { - Self(i16::from_ne_bytes(n), PhantomData) - } - - /// Construct a new value given a native endian value. - pub fn new(e: E, n: i16) -> Self { - Self(e.write_i16(n), PhantomData) - } - /// Return the value as a native endian value. - pub fn get(self, e: E) -> i16 { - e.read_i16(self.0) - } - /// Set the value given a native endian value. - pub fn set(&mut self, e: E, n: i16) { - self.0 = e.write_i16(n); - } - } - - /// An `i32` value with an externally specified endianness of type `E`. - #[derive(Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] - #[repr(transparent)] - pub struct I32(i32, PhantomData); - - impl I32 { - /// Construct a new value given bytes that already have the required endianness. - pub const fn from_bytes(n: [u8; 4]) -> Self { - Self(i32::from_ne_bytes(n), PhantomData) - } - - /// Construct a new value given a native endian value. - pub fn new(e: E, n: i32) -> Self { - Self(e.write_i32(n), PhantomData) - } - /// Return the value as a native endian value. - pub fn get(self, e: E) -> i32 { - e.read_i32(self.0) - } - /// Set the value given a native endian value. - pub fn set(&mut self, e: E, n: i32) { - self.0 = e.write_i32(n); - } - } - - /// An `i64` value with an externally specified endianness of type `E`. - #[derive(Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] - #[repr(transparent)] - pub struct I64(i64, PhantomData); - - impl I64 { - /// Construct a new value given bytes that already have the required endianness. - pub const fn from_bytes(n: [u8; 8]) -> Self { - Self(i64::from_ne_bytes(n), PhantomData) - } - - /// Construct a new value given a native endian value. - pub fn new(e: E, n: i64) -> Self { - Self(e.write_i64(n), PhantomData) - } - /// Return the value as a native endian value. - pub fn get(self, e: E) -> i64 { - e.read_i64(self.0) - } - /// Set the value given a native endian value. - pub fn set(&mut self, e: E, n: i64) { - self.0 = e.write_i64(n); - } - } - - impl fmt::Debug for U16 { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "U16({:x})", self.0) - } - } - - impl fmt::Debug for U32 { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "U32({:x})", self.0) - } - } - - impl fmt::Debug for U64 { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "U64({:x})", self.0) - } - } - - impl fmt::Debug for I16 { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "I16({:x})", self.0) - } - } - - impl fmt::Debug for I32 { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "I32({:x})", self.0) - } - } - - impl fmt::Debug for I64 { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "I64({:x})", self.0) - } - } - - impl From for U16 { - fn from(val: u16) -> Self { - Self::new(E::FIXED, val) - } - } - - impl From> for u16 { - fn from(val: U16) -> Self { - val.get(E::FIXED) - } - } - - impl From for U32 { - fn from(val: u32) -> Self { - Self::new(E::FIXED, val) - } - } - - impl From> for u32 { - fn from(val: U32) -> Self { - val.get(E::FIXED) - } - } - - impl From for U64 { - fn from(val: u64) -> Self { - Self::new(E::FIXED, val) - } - } - - impl From> for u64 { - fn from(val: U64) -> Self { - val.get(E::FIXED) - } - } - - impl From for I16 { - fn from(val: i16) -> Self { - Self::new(E::FIXED, val) - } - } - - impl From> for i16 { - fn from(val: I16) -> Self { - val.get(E::FIXED) - } - } - - impl From for I32 { - fn from(val: i32) -> Self { - Self::new(E::FIXED, val) - } - } - - impl From> for i32 { - fn from(val: I32) -> Self { - val.get(E::FIXED) - } - } - - impl From for I64 { - fn from(val: i64) -> Self { - Self::new(E::FIXED, val) - } - } - - impl From> for i64 { - fn from(val: I64) -> Self { - val.get(E::FIXED) - } - } - - unsafe_impl_endian_pod!(U16, U32, U64, I16, I32, I64); -} - -#[cfg(not(feature = "unaligned"))] -pub use aligned::*; - -/// A `u16` value with an externally specified endianness of type `E`. -#[cfg(feature = "unaligned")] -pub type U16 = U16Bytes; - -/// A `u32` value with an externally specified endianness of type `E`. -#[cfg(feature = "unaligned")] -pub type U32 = U32Bytes; - -/// A `u64` value with an externally specified endianness of type `E`. -#[cfg(feature = "unaligned")] -pub type U64 = U64Bytes; - -/// An `i16` value with an externally specified endianness of type `E`. -#[cfg(feature = "unaligned")] -pub type I16 = I16Bytes; - -/// An `i32` value with an externally specified endianness of type `E`. -#[cfg(feature = "unaligned")] -pub type I32 = I32Bytes; - -/// An `i64` value with an externally specified endianness of type `E`. -#[cfg(feature = "unaligned")] -pub type I64 = I64Bytes; +/// An unaligned `i64` value with an externally specified endianness of type `E`. +#[deprecated] +pub type I64Bytes = I64; /// An unaligned `u16` value with an externally specified endianness of type `E`. #[derive(Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] #[repr(transparent)] -pub struct U16Bytes([u8; 2], PhantomData); +pub struct U16([u8; 2], PhantomData); -impl U16Bytes { +impl U16 { /// Construct a new value given bytes that already have the required endianness. pub const fn from_bytes(n: [u8; 2]) -> Self { Self(n, PhantomData) @@ -713,26 +323,26 @@ impl U16Bytes { /// Construct a new value given a native endian value. pub fn new(e: E, n: u16) -> Self { - Self(e.write_u16_bytes(n), PhantomData) + Self(e.write_u16(n), PhantomData) } /// Return the value as a native endian value. pub fn get(self, e: E) -> u16 { - e.read_u16_bytes(self.0) + e.read_u16(self.0) } /// Set the value given a native endian value. pub fn set(&mut self, e: E, n: u16) { - self.0 = e.write_u16_bytes(n); + self.0 = e.write_u16(n); } } /// An unaligned `u32` value with an externally specified endianness of type `E`. #[derive(Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] #[repr(transparent)] -pub struct U32Bytes([u8; 4], PhantomData); +pub struct U32([u8; 4], PhantomData); -impl U32Bytes { +impl U32 { /// Construct a new value given bytes that already have the required endianness. pub const fn from_bytes(n: [u8; 4]) -> Self { Self(n, PhantomData) @@ -740,26 +350,26 @@ impl U32Bytes { /// Construct a new value given a native endian value. pub fn new(e: E, n: u32) -> Self { - Self(e.write_u32_bytes(n), PhantomData) + Self(e.write_u32(n), PhantomData) } /// Return the value as a native endian value. pub fn get(self, e: E) -> u32 { - e.read_u32_bytes(self.0) + e.read_u32(self.0) } /// Set the value given a native endian value. pub fn set(&mut self, e: E, n: u32) { - self.0 = e.write_u32_bytes(n); + self.0 = e.write_u32(n); } } /// An unaligned `u64` value with an externally specified endianness of type `E`. #[derive(Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] #[repr(transparent)] -pub struct U64Bytes([u8; 8], PhantomData); +pub struct U64([u8; 8], PhantomData); -impl U64Bytes { +impl U64 { /// Construct a new value given bytes that already have the required endianness. pub const fn from_bytes(n: [u8; 8]) -> Self { Self(n, PhantomData) @@ -767,26 +377,26 @@ impl U64Bytes { /// Construct a new value given a native endian value. pub fn new(e: E, n: u64) -> Self { - Self(e.write_u64_bytes(n), PhantomData) + Self(e.write_u64(n), PhantomData) } /// Return the value as a native endian value. pub fn get(self, e: E) -> u64 { - e.read_u64_bytes(self.0) + e.read_u64(self.0) } /// Set the value given a native endian value. pub fn set(&mut self, e: E, n: u64) { - self.0 = e.write_u64_bytes(n); + self.0 = e.write_u64(n); } } /// An unaligned `i16` value with an externally specified endianness of type `E`. #[derive(Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] #[repr(transparent)] -pub struct I16Bytes([u8; 2], PhantomData); +pub struct I16([u8; 2], PhantomData); -impl I16Bytes { +impl I16 { /// Construct a new value given bytes that already have the required endianness. pub const fn from_bytes(n: [u8; 2]) -> Self { Self(n, PhantomData) @@ -794,26 +404,26 @@ impl I16Bytes { /// Construct a new value given a native endian value. pub fn new(e: E, n: i16) -> Self { - Self(e.write_i16_bytes(n), PhantomData) + Self(e.write_i16(n), PhantomData) } /// Return the value as a native endian value. pub fn get(self, e: E) -> i16 { - e.read_i16_bytes(self.0) + e.read_i16(self.0) } /// Set the value given a native endian value. pub fn set(&mut self, e: E, n: i16) { - self.0 = e.write_i16_bytes(n); + self.0 = e.write_i16(n); } } /// An unaligned `i32` value with an externally specified endianness of type `E`. #[derive(Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] #[repr(transparent)] -pub struct I32Bytes([u8; 4], PhantomData); +pub struct I32([u8; 4], PhantomData); -impl I32Bytes { +impl I32 { /// Construct a new value given bytes that already have the required endianness. pub const fn from_bytes(n: [u8; 4]) -> Self { Self(n, PhantomData) @@ -821,26 +431,26 @@ impl I32Bytes { /// Construct a new value given a native endian value. pub fn new(e: E, n: i32) -> Self { - Self(e.write_i32_bytes(n), PhantomData) + Self(e.write_i32(n), PhantomData) } /// Return the value as a native endian value. pub fn get(self, e: E) -> i32 { - e.read_i32_bytes(self.0) + e.read_i32(self.0) } /// Set the value given a native endian value. pub fn set(&mut self, e: E, n: i32) { - self.0 = e.write_i32_bytes(n); + self.0 = e.write_i32(n); } } /// An unaligned `i64` value with an externally specified endianness of type `E`. #[derive(Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] #[repr(transparent)] -pub struct I64Bytes([u8; 8], PhantomData); +pub struct I64([u8; 8], PhantomData); -impl I64Bytes { +impl I64 { /// Construct a new value given bytes that already have the required endianness. pub const fn from_bytes(n: [u8; 8]) -> Self { Self(n, PhantomData) @@ -848,142 +458,142 @@ impl I64Bytes { /// Construct a new value given a native endian value. pub fn new(e: E, n: i64) -> Self { - Self(e.write_i64_bytes(n), PhantomData) + Self(e.write_i64(n), PhantomData) } /// Return the value as a native endian value. pub fn get(self, e: E) -> i64 { - e.read_i64_bytes(self.0) + e.read_i64(self.0) } /// Set the value given a native endian value. pub fn set(&mut self, e: E, n: i64) { - self.0 = e.write_i64_bytes(n); + self.0 = e.write_i64(n); } } -impl fmt::Debug for U16Bytes { +impl fmt::Debug for U16 { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "U16({:x}, {:x})", self.0[0], self.0[1],) + write!(f, "U16({:02x}{:02x})", self.0[0], self.0[1],) } } -impl fmt::Debug for U32Bytes { +impl fmt::Debug for U32 { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, - "U32({:x}, {:x}, {:x}, {:x})", + "U32({:02x}{:02x}{:02x}{:02x})", self.0[0], self.0[1], self.0[2], self.0[3], ) } } -impl fmt::Debug for U64Bytes { +impl fmt::Debug for U64 { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, - "U64({:x}, {:x}, {:x}, {:x}, {:x}, {:x}, {:x}, {:x})", + "U64({:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x})", self.0[0], self.0[1], self.0[2], self.0[3], self.0[4], self.0[5], self.0[6], self.0[7], ) } } -impl fmt::Debug for I16Bytes { +impl fmt::Debug for I16 { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "I16({:x}, {:x})", self.0[0], self.0[1],) + write!(f, "I16({:02x}{:02x})", self.0[0], self.0[1],) } } -impl fmt::Debug for I32Bytes { +impl fmt::Debug for I32 { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, - "I32({:x}, {:x}, {:x}, {:x})", + "I32({:02x}{:02x}{:02x}{:02x})", self.0[0], self.0[1], self.0[2], self.0[3], ) } } -impl fmt::Debug for I64Bytes { +impl fmt::Debug for I64 { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, - "I64({:x}, {:x}, {:x}, {:x}, {:x}, {:x}, {:x}, {:x})", + "I64({:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x})", self.0[0], self.0[1], self.0[2], self.0[3], self.0[4], self.0[5], self.0[6], self.0[7], ) } } -impl From for U16Bytes { +impl From for U16 { fn from(val: u16) -> Self { Self::new(E::FIXED, val) } } -impl From> for u16 { - fn from(val: U16Bytes) -> Self { +impl From> for u16 { + fn from(val: U16) -> Self { val.get(E::FIXED) } } -impl From for U32Bytes { +impl From for U32 { fn from(val: u32) -> Self { Self::new(E::FIXED, val) } } -impl From> for u32 { - fn from(val: U32Bytes) -> Self { +impl From> for u32 { + fn from(val: U32) -> Self { val.get(E::FIXED) } } -impl From for U64Bytes { +impl From for U64 { fn from(val: u64) -> Self { Self::new(E::FIXED, val) } } -impl From> for u64 { - fn from(val: U64Bytes) -> Self { +impl From> for u64 { + fn from(val: U64) -> Self { val.get(E::FIXED) } } -impl From for I16Bytes { +impl From for I16 { fn from(val: i16) -> Self { Self::new(E::FIXED, val) } } -impl From> for i16 { - fn from(val: I16Bytes) -> Self { +impl From> for i16 { + fn from(val: I16) -> Self { val.get(E::FIXED) } } -impl From for I32Bytes { +impl From for I32 { fn from(val: i32) -> Self { Self::new(E::FIXED, val) } } -impl From> for i32 { - fn from(val: I32Bytes) -> Self { +impl From> for i32 { + fn from(val: I32) -> Self { val.get(E::FIXED) } } -impl From for I64Bytes { +impl From for I64 { fn from(val: i64) -> Self { Self::new(E::FIXED, val) } } -impl From> for i64 { - fn from(val: I64Bytes) -> Self { +impl From> for i64 { + fn from(val: I64) -> Self { val.get(E::FIXED) } } -unsafe_impl_endian_pod!(U16Bytes, U32Bytes, U64Bytes, I16Bytes, I32Bytes, I64Bytes); +unsafe_impl_endian_pod!(U16, U32, U64, I16, I32, I64); diff --git a/src/macho.rs b/src/macho.rs index 611181d..86812fb 100644 --- a/src/macho.rs +++ b/src/macho.rs @@ -7,7 +7,7 @@ #![allow(missing_docs)] -use crate::endian::{BigEndian, Endian, U64Bytes, U16, U32, U64}; +use crate::endian::{BigEndian, Endian, U16, U32, U64}; use crate::pod::Pod; // Definitions from "/usr/include/mach/machine.h". @@ -2660,8 +2660,7 @@ pub struct Nlist64 { /// see pub n_desc: U16, /// value of this symbol (or stab offset) - // Note: 4 byte alignment has been observed in practice. - pub n_value: U64Bytes, + pub n_value: U64, } /* diff --git a/src/pe.rs b/src/pe.rs index 28c4b2c..d1fb7fb 100644 --- a/src/pe.rs +++ b/src/pe.rs @@ -9,7 +9,7 @@ use core::convert::TryInto; -use crate::endian::{I32Bytes, LittleEndian as LE, U16Bytes, U32Bytes, I32, U16, U32, U64}; +use crate::endian::{LittleEndian as LE, I32, U16, U32, U64}; use crate::pod::Pod; /// MZ @@ -811,15 +811,14 @@ pub const IMAGE_SCN_SCALE_INDEX: u32 = 0x0000_0001; // Symbol format. // -// This struct has alignment 1. #[derive(Debug, Clone, Copy)] #[repr(C)] pub struct ImageSymbol { /// If first 4 bytes are 0, then second 4 bytes are offset into string table. pub name: [u8; 8], - pub value: U32Bytes, - pub section_number: U16Bytes, - pub typ: U16Bytes, + pub value: U32, + pub section_number: U16, + pub typ: U16, pub storage_class: u8, pub number_of_aux_symbols: u8, } @@ -830,15 +829,14 @@ pub const IMAGE_SIZEOF_SYMBOL: usize = 18; #[repr(C)] pub struct ImageSymbolBytes(pub [u8; IMAGE_SIZEOF_SYMBOL]); -// This struct has alignment 1. #[derive(Debug, Clone, Copy)] #[repr(C)] pub struct ImageSymbolEx { /// If first 4 bytes are 0, then second 4 bytes are offset into string table. pub name: [u8; 8], - pub value: U32Bytes, - pub section_number: I32Bytes, - pub typ: U16Bytes, + pub value: U32, + pub section_number: I32, + pub typ: U16, pub storage_class: u8, pub number_of_aux_symbols: u8, } @@ -950,7 +948,6 @@ pub const IMAGE_SYM_DTYPE_SHIFT: usize = N_BTSHFT; // // Used for both ImageSymbol and ImageSymbolEx (with padding). -// This struct has alignment 1. #[derive(Debug, Clone, Copy)] #[repr(C)] pub struct ImageAuxSymbolTokenDef { @@ -958,7 +955,7 @@ pub struct ImageAuxSymbolTokenDef { pub aux_type: u8, /// Must be 0 pub reserved1: u8, - pub symbol_table_index: U32Bytes, + pub symbol_table_index: U32, /// Must be 0 pub reserved2: [u8; 12], } @@ -966,14 +963,13 @@ pub struct ImageAuxSymbolTokenDef { pub const IMAGE_AUX_SYMBOL_TYPE_TOKEN_DEF: u16 = 1; /// Auxiliary symbol format 1: function definitions. -// This struct has alignment 1. #[derive(Debug, Clone, Copy)] #[repr(C)] pub struct ImageAuxSymbolFunction { - pub tag_index: U32Bytes, - pub total_size: U32Bytes, - pub pointer_to_linenumber: U32Bytes, - pub pointer_to_next_function: U32Bytes, + pub tag_index: U32, + pub total_size: U32, + pub pointer_to_linenumber: U32, + pub pointer_to_next_function: U32, pub unused: [u8; 2], } @@ -984,9 +980,9 @@ pub struct ImageAuxSymbolFunction { pub struct ImageAuxSymbolFunctionBeginEnd { pub unused1: [u8; 4], /// declaration line number - pub linenumber: U16Bytes, + pub linenumber: U16, pub unused2: [u8; 6], - pub pointer_to_next_function: U32Bytes, + pub pointer_to_next_function: U32, pub unused3: [u8; 2], } @@ -998,8 +994,8 @@ pub struct ImageAuxSymbolFunctionBeginEnd { #[repr(C)] pub struct ImageAuxSymbolWeak { /// the weak extern default symbol index - pub weak_default_sym_index: U32Bytes, - pub weak_search_type: U32Bytes, + pub weak_default_sym_index: U32, + pub weak_search_type: U32, } /// Auxiliary symbol format 5: sections. @@ -1010,20 +1006,20 @@ pub struct ImageAuxSymbolWeak { #[repr(C)] pub struct ImageAuxSymbolSection { /// section length - pub length: U32Bytes, + pub length: U32, /// number of relocation entries - pub number_of_relocations: U16Bytes, + pub number_of_relocations: U16, /// number of line numbers - pub number_of_linenumbers: U16Bytes, + pub number_of_linenumbers: U16, /// checksum for communal - pub check_sum: U32Bytes, + pub check_sum: U32, /// section number to associate with - pub number: U16Bytes, + pub number: U16, /// communal selection type pub selection: u8, pub reserved: u8, /// high bits of the section number - pub high_number: U16Bytes, + pub high_number: U16, } // Used for both ImageSymbol and ImageSymbolEx (both with padding). @@ -1031,7 +1027,7 @@ pub struct ImageAuxSymbolSection { #[derive(Debug, Clone, Copy)] #[repr(C)] pub struct ImageAuxSymbolCrc { - pub crc: U32Bytes, + pub crc: U32, } // @@ -1060,9 +1056,9 @@ pub const IMAGE_WEAK_EXTERN_ANTI_DEPENDENCY: u32 = 4; #[repr(C)] pub struct ImageRelocation { /// Also `RelocCount` when IMAGE_SCN_LNK_NRELOC_OVFL is set - pub virtual_address: U32Bytes, - pub symbol_table_index: U32Bytes, - pub typ: U16Bytes, + pub virtual_address: U32, + pub symbol_table_index: U32, + pub typ: U16, } // @@ -1721,9 +1717,9 @@ pub const X3_EMPTY_INST_VAL_POS_X: u16 = 0; pub struct ImageLinenumber { /// Symbol table index of function name if Linenumber is 0. /// Otherwise virtual address of line number. - pub symbol_table_index_or_virtual_address: U32Bytes, + pub symbol_table_index_or_virtual_address: U32, /// Line number. - pub linenumber: U16Bytes, + pub linenumber: U16, } // @@ -1918,17 +1914,17 @@ pub struct ImageTlsDirectory32 { pub struct ImageImportDescriptor { /// RVA to original unbound IAT (`ImageThunkData32`/`ImageThunkData64`) /// 0 for terminating null import descriptor - pub original_first_thunk: U32Bytes, + pub original_first_thunk: U32, /// 0 if not bound, /// -1 if bound, and real date\time stamp /// in IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT (new BIND) /// O.W. date/time stamp of DLL bound to (Old BIND) - pub time_date_stamp: U32Bytes, + pub time_date_stamp: U32, /// -1 if no forwarders - pub forwarder_chain: U32Bytes, - pub name: U32Bytes, + pub forwarder_chain: U32, + pub name: U32, /// RVA to IAT (if bound this IAT has actual addresses) - pub first_thunk: U32Bytes, + pub first_thunk: U32, } impl ImageImportDescriptor { @@ -2237,10 +2233,10 @@ pub struct ImagePrologueDynamicRelocationHeader { #[derive(Debug, Clone, Copy)] #[repr(C)] pub struct ImageEpilogueDynamicRelocationHeader { - pub epilogue_count: U32Bytes, + pub epilogue_count: U32, pub epilogue_byte_count: u8, pub branch_descriptor_element_size: u8, - pub branch_descriptor_count: U16Bytes, + pub branch_descriptor_count: U16, // pub branch_descriptors[...], // pub branch_descriptor_bit_map[...], } diff --git a/src/read/archive.rs b/src/read/archive.rs index ff60378..bbe6096 100644 --- a/src/read/archive.rs +++ b/src/read/archive.rs @@ -23,7 +23,7 @@ use core::convert::TryInto; use core::slice; -use crate::endian::{BigEndian as BE, LittleEndian as LE, U16Bytes, U32Bytes, U64Bytes}; +use crate::endian::{BigEndian as BE, LittleEndian as LE, U16, U32, U64}; use crate::read::{self, Bytes, Error, ReadError, ReadRef}; use crate::{archive, SkipDebugList}; @@ -612,7 +612,7 @@ enum SymbolIteratorInternal<'data> { /// - the offsets of the member headers as 32-bit big-endian integers /// - the symbol names as null-terminated strings Gnu { - offsets: slice::Iter<'data, U32Bytes>, + offsets: slice::Iter<'data, U32>, names: Bytes<'data>, }, /// A GNU 64-bit symbol table @@ -622,7 +622,7 @@ enum SymbolIteratorInternal<'data> { /// - the offsets of the member headers as 64-bit big-endian integers /// - the symbol names as null-terminated strings Gnu64 { - offsets: slice::Iter<'data, U64Bytes>, + offsets: slice::Iter<'data, U64>, names: Bytes<'data>, }, /// A BSD symbol table. @@ -634,7 +634,7 @@ enum SymbolIteratorInternal<'data> { /// - the size in bytes of the symbol names as a 32-bit little-endian integer /// - the symbol names as null-terminated strings Bsd { - offsets: slice::Iter<'data, [U32Bytes; 2]>, + offsets: slice::Iter<'data, [U32; 2]>, names: Bytes<'data>, }, /// A BSD 64-bit symbol table. @@ -646,7 +646,7 @@ enum SymbolIteratorInternal<'data> { /// - the size in bytes of the symbol names as a 64-bit little-endian integer /// - the symbol names as null-terminated strings Bsd64 { - offsets: slice::Iter<'data, [U64Bytes; 2]>, + offsets: slice::Iter<'data, [U64; 2]>, names: Bytes<'data>, }, /// A Windows COFF symbol table. @@ -658,8 +658,8 @@ enum SymbolIteratorInternal<'data> { /// - the member index for each symbol as a 16-bit little-endian integer /// - the symbol names as null-terminated strings in lexical order Coff { - members: &'data [U32Bytes], - indices: slice::Iter<'data, U16Bytes>, + members: &'data [U32], + indices: slice::Iter<'data, U16>, names: Bytes<'data>, }, } @@ -675,25 +675,25 @@ impl<'data> ArchiveSymbolIterator<'data> { match kind { ArchiveKind::Unknown => Ok(ArchiveSymbolIterator(SymbolIteratorInternal::None)), ArchiveKind::Gnu => { - let offsets_count = data.read::>()?.get(BE); - let offsets = data.read_slice::>(offsets_count as usize)?; + let offsets_count = data.read::>()?.get(BE); + let offsets = data.read_slice::>(offsets_count as usize)?; Ok(ArchiveSymbolIterator(SymbolIteratorInternal::Gnu { offsets: offsets.iter(), names: data, })) } ArchiveKind::Gnu64 => { - let offsets_count = data.read::>()?.get(BE); - let offsets = data.read_slice::>(offsets_count as usize)?; + let offsets_count = data.read::>()?.get(BE); + let offsets = data.read_slice::>(offsets_count as usize)?; Ok(ArchiveSymbolIterator(SymbolIteratorInternal::Gnu64 { offsets: offsets.iter(), names: data, })) } ArchiveKind::Bsd => { - let offsets_size = data.read::>()?.get(LE); - let offsets = data.read_slice::<[U32Bytes; 2]>(offsets_size as usize / 8)?; - let names_size = data.read::>()?.get(LE); + let offsets_size = data.read::>()?.get(LE); + let offsets = data.read_slice::<[U32; 2]>(offsets_size as usize / 8)?; + let names_size = data.read::>()?.get(LE); let names = data.read_bytes(names_size as usize)?; Ok(ArchiveSymbolIterator(SymbolIteratorInternal::Bsd { offsets: offsets.iter(), @@ -701,9 +701,9 @@ impl<'data> ArchiveSymbolIterator<'data> { })) } ArchiveKind::Bsd64 => { - let offsets_size = data.read::>()?.get(LE); - let offsets = data.read_slice::<[U64Bytes; 2]>(offsets_size as usize / 16)?; - let names_size = data.read::>()?.get(LE); + let offsets_size = data.read::>()?.get(LE); + let offsets = data.read_slice::<[U64; 2]>(offsets_size as usize / 16)?; + let names_size = data.read::>()?.get(LE); let names = data.read_bytes(names_size as usize)?; Ok(ArchiveSymbolIterator(SymbolIteratorInternal::Bsd64 { offsets: offsets.iter(), @@ -711,10 +711,10 @@ impl<'data> ArchiveSymbolIterator<'data> { })) } ArchiveKind::Coff => { - let members_count = data.read::>()?.get(LE); - let members = data.read_slice::>(members_count as usize)?; - let indices_count = data.read::>()?.get(LE); - let indices = data.read_slice::>(indices_count as usize)?; + let members_count = data.read::>()?.get(LE); + let members = data.read_slice::>(members_count as usize)?; + let indices_count = data.read::>()?.get(LE); + let indices = data.read_slice::>(indices_count as usize)?; Ok(ArchiveSymbolIterator(SymbolIteratorInternal::Coff { members, indices: indices.iter(), diff --git a/src/read/coff/symbol.rs b/src/read/coff/symbol.rs index 73119e3..fedff76 100644 --- a/src/read/coff/symbol.rs +++ b/src/read/coff/symbol.rs @@ -5,7 +5,7 @@ use core::fmt::Debug; use core::str; use super::{CoffCommon, CoffHeader, SectionTable}; -use crate::endian::{LittleEndian as LE, U32Bytes}; +use crate::endian::{LittleEndian as LE, U32}; use crate::pe; use crate::pod::{bytes_of, bytes_of_slice, Pod}; use crate::read::util::StringTable; @@ -51,7 +51,7 @@ impl<'data, R: ReadRef<'data>, Coff: CoffHeader> SymbolTable<'data, R, Coff> { // Note: don't update data when reading length; the length includes itself. let length = data - .read_at::>(offset) + .read_at::>(offset) .read_error("Missing COFF string table")? .get(LE); let str_end = offset diff --git a/src/read/elf/attributes.rs b/src/read/elf/attributes.rs index 39bc221..aa3ad22 100644 --- a/src/read/elf/attributes.rs +++ b/src/read/elf/attributes.rs @@ -81,7 +81,7 @@ impl<'data, Elf: FileHeader> AttributesSubsectionIterator<'data, Elf> { // First read the subsection length. let mut data = self.data; let length = data - .read::>() + .read::>() .read_error("ELF attributes section is too short")? .get(self.endian); @@ -180,7 +180,7 @@ impl<'data, Elf: FileHeader> AttributesSubsubsectionIterator<'data, Elf> { .read::() .read_error("ELF attributes subsection is too short")?; let length = data - .read::>() + .read::>() .read_error("ELF attributes subsection is too short")? .get(self.endian); diff --git a/src/read/elf/comdat.rs b/src/read/elf/comdat.rs index fb7b25f..832b93b 100644 --- a/src/read/elf/comdat.rs +++ b/src/read/elf/comdat.rs @@ -2,7 +2,7 @@ use core::fmt::Debug; use core::{iter, slice, str}; use crate::elf; -use crate::endian::{Endianness, U32Bytes}; +use crate::endian::{Endianness, U32}; use crate::read::{self, ComdatKind, ObjectComdat, ReadError, ReadRef, SectionIndex, SymbolIndex}; use super::{ElfFile, FileHeader, SectionHeader, Sym}; @@ -74,7 +74,7 @@ where { file: &'file ElfFile<'data, Elf, R>, section: &'data Elf::SectionHeader, - sections: &'data [U32Bytes], + sections: &'data [U32], } impl<'data, 'file, Elf, R> ElfComdat<'data, 'file, Elf, R> @@ -169,7 +169,7 @@ where R: ReadRef<'data>, { file: &'file ElfFile<'data, Elf, R>, - sections: slice::Iter<'data, U32Bytes>, + sections: slice::Iter<'data, U32>, } impl<'data, 'file, Elf, R> Iterator for ElfComdatSectionIterator<'data, 'file, Elf, R> diff --git a/src/read/elf/section.rs b/src/read/elf/section.rs index 7fbc128..6aa0f6a 100644 --- a/src/read/elf/section.rs +++ b/src/read/elf/section.rs @@ -2,7 +2,7 @@ use core::fmt::Debug; use core::{iter, slice, str}; use crate::elf; -use crate::endian::{self, Endianness, U32Bytes}; +use crate::endian::{self, Endianness, U32}; use crate::pod::{self, Pod}; use crate::read::{ self, gnu_compression, CompressedData, CompressedFileRange, CompressionFormat, Error, @@ -976,13 +976,13 @@ pub trait SectionHeader: Debug + Pod { &self, endian: Self::Endian, data: R, - ) -> read::Result])>> { + ) -> read::Result])>> { if self.sh_type(endian) != elf::SHT_GROUP { return Ok(None); } let msg = "Invalid ELF group section offset or size"; let data = self.data(endian, data).read_error(msg)?; - let (flag, data) = pod::from_bytes::>(data).read_error(msg)?; + let (flag, data) = pod::from_bytes::>(data).read_error(msg)?; let sections = pod::slice_from_all_bytes(data).read_error(msg)?; Ok(Some((flag.get(endian), sections))) } diff --git a/src/read/gnu_compression.rs b/src/read/gnu_compression.rs index 7ef7d91..e1d0b78 100644 --- a/src/read/gnu_compression.rs +++ b/src/read/gnu_compression.rs @@ -1,5 +1,5 @@ use crate::read::{self, Error, ReadError as _}; -use crate::{endian, CompressedFileRange, CompressionFormat, ReadRef, U32Bytes}; +use crate::{endian, CompressedFileRange, CompressionFormat, ReadRef, U32}; // Attempt to parse the the CompressedFileRange for a section using the GNU-style // inline compression header format. This is used by the Go compiler in Mach-O files @@ -20,7 +20,7 @@ pub(super) fn compressed_file_range<'data, R: ReadRef<'data>>( return Err(Error("Invalid GNU compressed section header")); } let uncompressed_size = file_data - .read::>(&mut offset) + .read::>(&mut offset) .read_error("GNU compressed section is too short")? .get(endian::BigEndian) .into(); diff --git a/src/read/macho/file.rs b/src/read/macho/file.rs index 3915324..ce40383 100644 --- a/src/read/macho/file.rs +++ b/src/read/macho/file.rs @@ -532,13 +532,13 @@ where if let Some(pc_bytes) = thread_data.get(pc_offset..pc_offset + 8) { let mut bytes = [0u8; 8]; bytes.copy_from_slice(pc_bytes); - return self.endian.read_u64_bytes(bytes); + return self.endian.read_u64(bytes); } } else if pc_size == 4 { if let Some(pc_bytes) = thread_data.get(pc_offset..pc_offset + 4) { let mut bytes = [0u8; 4]; bytes.copy_from_slice(pc_bytes); - return u64::from(self.endian.read_u32_bytes(bytes)); + return u64::from(self.endian.read_u32(bytes)); } } } diff --git a/src/read/pe/export.rs b/src/read/pe/export.rs index d66d406..3ea0c7b 100644 --- a/src/read/pe/export.rs +++ b/src/read/pe/export.rs @@ -1,7 +1,7 @@ use alloc::vec::Vec; use core::fmt::Debug; -use crate::endian::{LittleEndian as LE, U16Bytes, U32Bytes}; +use crate::endian::{LittleEndian as LE, U16, U32}; use crate::pe; use crate::read::{ByteString, Bytes, Error, ReadError, ReadRef, Result}; @@ -88,9 +88,9 @@ pub struct ExportTable<'data> { data: Bytes<'data>, virtual_address: u32, directory: &'data pe::ImageExportDirectory, - addresses: &'data [U32Bytes], - names: &'data [U32Bytes], - name_ordinals: &'data [U16Bytes], + addresses: &'data [U32], + names: &'data [U32], + name_ordinals: &'data [U16], } impl<'data> ExportTable<'data> { @@ -103,7 +103,7 @@ impl<'data> ExportTable<'data> { let address_of_functions = directory.address_of_functions.get(LE); if address_of_functions != 0 { addresses = data - .read_slice_at::>( + .read_slice_at::>( address_of_functions.wrapping_sub(virtual_address) as usize, directory.number_of_functions.get(LE) as usize, ) @@ -121,13 +121,13 @@ impl<'data> ExportTable<'data> { let number = directory.number_of_names.get(LE) as usize; names = data - .read_slice_at::>( + .read_slice_at::>( address_of_names.wrapping_sub(virtual_address) as usize, number, ) .read_error("Invalid PE export name pointer table")?; name_ordinals = data - .read_slice_at::>( + .read_slice_at::>( address_of_name_ordinals.wrapping_sub(virtual_address) as usize, number, ) @@ -166,14 +166,14 @@ impl<'data> ExportTable<'data> { /// /// An address table entry may be a local address, or the address of a forwarded export entry. /// See [`Self::is_forward`] and [`Self::target_from_address`]. - pub fn addresses(&self) -> &'data [U32Bytes] { + pub fn addresses(&self) -> &'data [U32] { self.addresses } /// Returns the unparsed name pointer table. /// /// A name pointer table entry can be used with [`Self::name_from_pointer`]. - pub fn name_pointers(&self) -> &'data [U32Bytes] { + pub fn name_pointers(&self) -> &'data [U32] { self.names } @@ -181,7 +181,7 @@ impl<'data> ExportTable<'data> { /// /// An ordinal table entry is a 0-based index into the address table. /// See [`Self::address_by_index`] and [`Self::target_by_index`]. - pub fn name_ordinals(&self) -> &'data [U16Bytes] { + pub fn name_ordinals(&self) -> &'data [U16] { self.name_ordinals } diff --git a/src/read/pe/import.rs b/src/read/pe/import.rs index c35dc98..79a0747 100644 --- a/src/read/pe/import.rs +++ b/src/read/pe/import.rs @@ -1,7 +1,7 @@ use core::fmt::Debug; use core::mem; -use crate::endian::{LittleEndian as LE, U16Bytes}; +use crate::endian::{LittleEndian as LE, U16}; use crate::pe; use crate::pod::Pod; use crate::read::{Bytes, ReadError, Result}; @@ -87,7 +87,7 @@ impl<'data> ImportTable<'data> { data.skip(offset as usize) .read_error("Invalid PE import thunk address")?; let hint = data - .read::>() + .read::>() .read_error("Missing PE import thunk hint")? .get(LE); let name = data @@ -326,7 +326,7 @@ impl<'data> DelayLoadImportTable<'data> { data.skip(offset as usize) .read_error("Invalid PE delay load import thunk address")?; let hint = data - .read::>() + .read::>() .read_error("Missing PE delay load import thunk hint")? .get(LE); let name = data diff --git a/src/read/pe/resource.rs b/src/read/pe/resource.rs index bf1fa70..86549e4 100644 --- a/src/read/pe/resource.rs +++ b/src/read/pe/resource.rs @@ -1,7 +1,7 @@ use alloc::string::String; use core::char; -use crate::endian::{LittleEndian as LE, U16Bytes}; +use crate::endian::{LittleEndian as LE, U16}; use crate::pe; use crate::read::{ReadError, ReadRef, Result}; @@ -155,18 +155,15 @@ impl ResourceName { } /// Returns the string unicode buffer. - pub fn data<'data>( - &self, - directory: ResourceDirectory<'data>, - ) -> Result<&'data [U16Bytes]> { + pub fn data<'data>(&self, directory: ResourceDirectory<'data>) -> Result<&'data [U16]> { let mut offset = u64::from(self.offset); let len = directory .data - .read::>(&mut offset) + .read::>(&mut offset) .read_error("Invalid resource name offset")?; directory .data - .read_slice::>(&mut offset, len.get(LE).into()) + .read_slice::>(&mut offset, len.get(LE).into()) .read_error("Invalid resource name length") } diff --git a/src/read/read_ref.rs b/src/read/read_ref.rs index c348fae..fe03891 100644 --- a/src/read/read_ref.rs +++ b/src/read/read_ref.rs @@ -80,7 +80,8 @@ pub trait ReadRef<'a>: Clone + Copy { /// `read_bytes` does not return bytes with the correct alignment for `T`. /// Implementors may want to provide their own implementation that ensures /// the alignment can be satisfied. Alternatively, only use this method with - /// types that do not need alignment (see the `unaligned` feature of this crate). + /// types that do not need alignment. The types provided by this crate do not + /// need alignment. fn read(self, offset: &mut u64) -> Result<&'a T> { let size = mem::size_of::().try_into().map_err(|_| ())?; let bytes = self.read_bytes(offset, size)?; diff --git a/src/read/xcoff/symbol.rs b/src/read/xcoff/symbol.rs index 248976a..4cdf0b9 100644 --- a/src/read/xcoff/symbol.rs +++ b/src/read/xcoff/symbol.rs @@ -4,7 +4,7 @@ use core::fmt::Debug; use core::marker::PhantomData; use core::str; -use crate::endian::{BigEndian as BE, U32Bytes}; +use crate::endian::{BigEndian as BE, U32}; use crate::pod::{bytes_of, Pod}; use crate::read::{ self, Bytes, Error, ObjectSymbol, ObjectSymbolTable, ReadError, ReadRef, Result, SectionIndex, @@ -60,7 +60,7 @@ where // Parse the string table. // Note: don't update data when reading length; the length includes itself. let length = data - .read_at::>(offset) + .read_at::>(offset) .read_error("Missing XCOFF string table")? .get(BE); let str_end = offset diff --git a/src/write/macho.rs b/src/write/macho.rs index ce87fa6..00fdf18 100644 --- a/src/write/macho.rs +++ b/src/write/macho.rs @@ -1177,7 +1177,7 @@ impl MachO for MachO64 { n_type: nlist.n_type, n_sect: nlist.n_sect, n_desc: U16::new(endian, nlist.n_desc), - n_value: U64Bytes::new(endian, nlist.n_value), + n_value: U64::new(endian, nlist.n_value), }; buffer.write(&nlist); } diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 4b9526b..a185bbc 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -136,7 +136,6 @@ fn cmd_features() -> Result<(), DynError> { // Test miscellaneous features individually. "std", "compression", - "unaligned", ] { cargo(&[ "test",