From 060f7cc6009fcc02a01356c74bb0bdaf24730ab2 Mon Sep 17 00:00:00 2001 From: Jan Bujak Date: Mon, 24 Jul 2023 09:09:16 +0000 Subject: [PATCH] Implement `Sym` for `ElfSymbol32` and `ElfSymbol64` --- src/read/elf/file.rs | 2 +- src/read/elf/symbol.rs | 111 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 110 insertions(+), 3 deletions(-) diff --git a/src/read/elf/file.rs b/src/read/elf/file.rs index 67be37e2..64bd076e 100644 --- a/src/read/elf/file.rs +++ b/src/read/elf/file.rs @@ -448,7 +448,7 @@ pub trait FileHeader: Debug + Pod { type CompressionHeader: CompressionHeader; type NoteHeader: NoteHeader; type Dyn: Dyn; - type Sym: Sym; + type Sym: Sym + Pod + Debug; type Rel: Rel; type Rela: Rela + From; diff --git a/src/read/elf/symbol.rs b/src/read/elf/symbol.rs index ac109570..f3cad208 100644 --- a/src/read/elf/symbol.rs +++ b/src/read/elf/symbol.rs @@ -5,7 +5,6 @@ use core::slice; use core::str; use crate::endian::{self, Endianness}; -use crate::pod::Pod; use crate::read::util::StringTable; use crate::read::{ self, ObjectSymbol, ObjectSymbolTable, ReadError, ReadRef, SectionIndex, SymbolFlags, @@ -437,7 +436,7 @@ impl<'data, 'file, Elf: FileHeader, R: ReadRef<'data>> ObjectSymbol<'data> /// A trait for generic access to `Sym32` and `Sym64`. #[allow(missing_docs)] -pub trait Sym: Debug + Pod { +pub trait Sym { type Word: Into; type Endian: endian::Endian; @@ -575,3 +574,111 @@ impl Sym for elf::Sym64 { self.st_size.get(endian) } } + +impl<'data, 'file, Endian, R> Sym for ElfSymbol32<'data, 'file, Endian, R> +where + Endian: endian::Endian, + R: ReadRef<'data>, +{ + type Word = u32; + type Endian = Endian; + + #[inline] + fn st_name(&self, endian: Self::Endian) -> u32 { + self.symbol.st_name(endian) + } + + #[inline] + fn st_info(&self) -> u8 { + self.symbol.st_info() + } + + #[inline] + fn st_bind(&self) -> u8 { + self.symbol.st_bind() + } + + #[inline] + fn st_type(&self) -> u8 { + self.symbol.st_type() + } + + #[inline] + fn st_other(&self) -> u8 { + self.symbol.st_other() + } + + #[inline] + fn st_visibility(&self) -> u8 { + self.symbol.st_visibility() + } + + #[inline] + fn st_shndx(&self, endian: Self::Endian) -> u16 { + self.symbol.st_shndx(endian) + } + + #[inline] + fn st_value(&self, endian: Self::Endian) -> Self::Word { + self.symbol.st_value(endian) + } + + #[inline] + fn st_size(&self, endian: Self::Endian) -> Self::Word { + self.symbol.st_size(endian) + } +} + +impl<'data, 'file, Endian, R> Sym for ElfSymbol64<'data, 'file, Endian, R> +where + Endian: endian::Endian, + R: ReadRef<'data>, +{ + type Word = u64; + type Endian = Endian; + + #[inline] + fn st_name(&self, endian: Self::Endian) -> u32 { + self.symbol.st_name(endian) + } + + #[inline] + fn st_info(&self) -> u8 { + self.symbol.st_info() + } + + #[inline] + fn st_bind(&self) -> u8 { + self.symbol.st_bind() + } + + #[inline] + fn st_type(&self) -> u8 { + self.symbol.st_type() + } + + #[inline] + fn st_other(&self) -> u8 { + self.symbol.st_other() + } + + #[inline] + fn st_visibility(&self) -> u8 { + self.symbol.st_visibility() + } + + #[inline] + fn st_shndx(&self, endian: Self::Endian) -> u16 { + self.symbol.st_shndx(endian) + } + + #[inline] + fn st_value(&self, endian: Self::Endian) -> Self::Word { + self.symbol.st_value(endian) + } + + #[inline] + fn st_size(&self, endian: Self::Endian) -> Self::Word { + self.symbol.st_size(endian) + } +}