From 7908f8255e1cc76d15ce6186432561a9e2b2242c Mon Sep 17 00:00:00 2001 From: Philip Craig Date: Thu, 4 May 2023 16:20:10 +1000 Subject: [PATCH] read/elf: GNU properties do not use section alignment --- src/read/elf/file.rs | 25 +++++++++++++++++++++++++ src/read/elf/note.rs | 13 +++++-------- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/src/read/elf/file.rs b/src/read/elf/file.rs index 259da790..aac66e7c 100644 --- a/src/read/elf/file.rs +++ b/src/read/elf/file.rs @@ -456,6 +456,15 @@ pub trait FileHeader: Debug + Pod { /// This is a property of the type, not a value in the header data. fn is_type_64(&self) -> bool; + /// Return true if this type is a 64-bit header. + /// + /// This is a property of the type, not a value in the header data. + /// + /// This is the same as `is_type_64`, but is non-dispatchable. + fn is_type_64_sized() -> bool + where + Self: Sized; + fn e_ident(&self) -> &elf::Ident; fn e_type(&self, endian: Self::Endian) -> u16; fn e_machine(&self, endian: Self::Endian) -> u16; @@ -724,6 +733,14 @@ impl FileHeader for elf::FileHeader32 { false } + #[inline] + fn is_type_64_sized() -> bool + where + Self: Sized, + { + false + } + #[inline] fn e_ident(&self) -> &elf::Ident { &self.e_ident @@ -813,6 +830,14 @@ impl FileHeader for elf::FileHeader64 { true } + #[inline] + fn is_type_64_sized() -> bool + where + Self: Sized, + { + true + } + #[inline] fn e_ident(&self) -> &elf::Ident { &self.e_ident diff --git a/src/read/elf/note.rs b/src/read/elf/note.rs index f1a80b24..fc5aa775 100644 --- a/src/read/elf/note.rs +++ b/src/read/elf/note.rs @@ -80,12 +80,7 @@ where } self.data = data; - Ok(Some(Note { - align: self.align, - header, - name, - desc, - })) + Ok(Some(Note { header, name, desc })) } } @@ -95,7 +90,6 @@ pub struct Note<'data, Elf> where Elf: FileHeader, { - align: usize, header: &'data Elf::NoteHeader, name: &'data [u8], desc: &'data [u8], @@ -152,9 +146,12 @@ impl<'data, Elf: FileHeader> Note<'data, Elf> { if self.name() != elf::ELF_NOTE_GNU || self.n_type(endian) != elf::NT_GNU_PROPERTY_TYPE_0 { return None; } + // Use the ELF class instead of the section alignment. + // This matches what other parsers do. + let align = if Elf::is_type_64_sized() { 8 } else { 4 }; Some(GnuPropertyIterator { endian, - align: self.align, + align, data: Bytes(self.desc), }) }