Skip to content

Commit

Permalink
Remove Clone + Copy bounds from ElfN type
Browse files Browse the repository at this point in the history
Having the ElfN type implement Copy is a convenience, but not a
necessity. With upcoming work it may not be feasible to keep this type
copyable. Hence, remove the Copy implementation. Go even one step
further and also remove Clone, as it does not really seem necessary
either.

Signed-off-by: Daniel Müller <deso@posteo.net>
  • Loading branch information
d-e-s-o committed Dec 17, 2024
1 parent 1387ab7 commit 3844896
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 29 deletions.
18 changes: 9 additions & 9 deletions src/elf/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,12 @@ impl<'mmap> SymbolTableCache<'mmap> {

fn create_str2sym<F>(&self, mut filter: F) -> Result<Vec<(&'mmap str, usize)>>
where
F: FnMut(ElfN_Sym<'_>) -> bool,
F: FnMut(&ElfN_Sym<'_>) -> bool,
{
let mut str2sym = self
.syms
.iter(0)
.filter(|sym| filter(*sym))
.filter(|sym| filter(sym))
.enumerate()
.map(|(i, sym)| {
let name = self
Expand All @@ -229,7 +229,7 @@ impl<'mmap> SymbolTableCache<'mmap> {

fn ensure_str2sym<F>(&self, filter: F) -> Result<&[(&'mmap str, usize)]>
where
F: FnMut(ElfN_Sym<'_>) -> bool,
F: FnMut(&ElfN_Sym<'_>) -> bool,
{
let str2sym = self
.str2sym
Expand Down Expand Up @@ -377,7 +377,7 @@ impl<'mmap> Cache<'mmap> {
/// of certain member variables to reference data from this header,
/// which otherwise is zeroed out.
#[inline]
fn read_first_shdr(&self, ehdr: ElfN_Ehdr<'_>) -> Result<ElfN_Shdr<'mmap>> {
fn read_first_shdr(&self, ehdr: &ElfN_Ehdr<'_>) -> Result<ElfN_Shdr<'mmap>> {
let mut data = self
.elf_data
.get(ehdr.shoff() as usize..)
Expand Down Expand Up @@ -430,7 +430,7 @@ impl<'mmap> Cache<'mmap> {
// number of entries in the section header table is held in the sh_size
// member of the initial entry in section header table."
let shnum = if e_shnum == 0 {
let shdr = self.read_first_shdr(ehdr)?.to_64bit();
let shdr = self.read_first_shdr(&ehdr)?.to_64bit();
usize::try_from(shdr.sh_size).ok().ok_or_invalid_data(|| {
format!(
"ELF file contains unsupported number of sections ({})",
Expand All @@ -447,7 +447,7 @@ impl<'mmap> Cache<'mmap> {
// program header table is held in the sh_info member of the
// initial entry in section header table."
let phnum = if e_phnum == PN_XNUM {
let shdr = self.read_first_shdr(ehdr)?.to_64bit();
let shdr = self.read_first_shdr(&ehdr)?.to_64bit();
usize::try_from(shdr.sh_info).ok().ok_or_invalid_data(|| {
format!(
"ELF file contains unsupported number of program headers ({})",
Expand Down Expand Up @@ -514,7 +514,7 @@ impl<'mmap> Cache<'mmap> {
self.phdrs.get_or_try_init(|| self.parse_phdrs()).copied()
}

fn shstrndx(&self, ehdr: ElfN_Ehdr<'_>) -> Result<usize> {
fn shstrndx(&self, ehdr: &ElfN_Ehdr<'_>) -> Result<usize> {
let e_shstrndx = ehdr.shstrndx();
// "If the index of section name string table section is larger
// than or equal to SHN_LORESERVE (0xff00), this member holds
Expand All @@ -535,7 +535,7 @@ impl<'mmap> Cache<'mmap> {

fn parse_shstrtab(&self) -> Result<&'mmap [u8]> {
let ehdr = self.ensure_ehdr()?;
let shstrndx = self.shstrndx(ehdr.ehdr)?;
let shstrndx = self.shstrndx(&ehdr.ehdr)?;
let shstrtab = self.section_data_raw(shstrndx)?;
Ok(shstrtab)
}
Expand Down Expand Up @@ -1233,7 +1233,7 @@ mod tests {

let parser = ElfParser::open_file(file.as_file(), file.path()).unwrap();
let ehdr = parser.cache.ensure_ehdr().unwrap();
let shstrndx = parser.cache.shstrndx(ehdr.ehdr).unwrap();
let shstrndx = parser.cache.shstrndx(&ehdr.ehdr).unwrap();
assert_eq!(shstrndx, usize::from(SHSTRNDX));
}

Expand Down
24 changes: 4 additions & 20 deletions src/elf/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,17 @@ where
matches!(self, Self::B32(..))
}

pub fn to_64bit(self) -> T
pub fn to_64bit(&self) -> T
where
T: Copy + for<'ty> From<&'ty T::Ty32Bit>,
{
match self {
Self::B32(ty) => T::from(ty),
Self::B64(ty) => *ty,
Self::B64(ty) => **ty,
}
}
}

impl<T> Clone for ElfN<'_, T>
where
T: Has32BitTy,
{
fn clone(&self) -> Self {
*self
}
}

impl<T> Copy for ElfN<'_, T> where T: Has32BitTy {}


#[derive(Copy, Clone, Debug)]
pub(crate) enum ElfNSlice<'elf, T>
Expand Down Expand Up @@ -520,10 +509,10 @@ impl TryFrom<&Elf64_Sym> for SymType {
}
}

impl TryFrom<ElfN<'_, Elf64_Sym>> for SymType {
impl TryFrom<&ElfN<'_, Elf64_Sym>> for SymType {
type Error = ();

fn try_from(other: ElfN<'_, Elf64_Sym>) -> Result<Self, Self::Error> {
fn try_from(other: &ElfN<'_, Elf64_Sym>) -> Result<Self, Self::Error> {
SymType::try_from_elf_type(other.type_())
}
}
Expand Down Expand Up @@ -698,11 +687,6 @@ mod tests {
/// Exercise some trivial type conversion functions.
#[test]
fn conversions() {
let ehdr64 = Elf64_Ehdr::default();
let ehdr = ElfN::B64(&ehdr64);
#[allow(clippy::clone_on_copy)]
let _ehdr2 = ehdr.clone();

let shdr = Elf32_Shdr::default();
let _shdr64 = Elf64_Shdr::from(&shdr);

Expand Down

0 comments on commit 3844896

Please sign in to comment.