Skip to content

Commit

Permalink
feat(binfmt): Holey Bytes support (initial)
Browse files Browse the repository at this point in the history
  • Loading branch information
ondra05 committed Oct 1, 2023
1 parent f309679 commit 2c2c0f9
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 2 deletions.
4 changes: 4 additions & 0 deletions arch-ops/src/holeybytes/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,22 @@ pub struct HbEncoder<W> {
}

impl<W> HbEncoder<W> {
#[inline]
pub const fn new(inner: W) -> Self {
Self { inner }
}

#[inline]
pub fn into_inner(self) -> W {
self.inner
}

#[inline]
pub fn inner(&self) -> &W {
&self.inner
}

#[inline]
pub fn inner_mut(&mut self) -> &mut W {
&mut self.inner
}
Expand Down
1 change: 1 addition & 0 deletions arch-ops/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ pub enum RelocCode {
W65RelaxJmp,
CleverShort,
CleverShortPcrel,
HbRelaxedRel,
}

#[derive(Clone, Debug, Hash, PartialEq, Eq)]
Expand Down
3 changes: 2 additions & 1 deletion binfmt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,6 @@ m68k = []
clever = []
z80 = []
m6502 = []
default-archs = ["w65", "x86", "arm","riscv","m68k", "clever"]
holeybytes = []
default-archs = ["w65", "x86", "arm","riscv","m68k", "clever", "holeybytes"]
all-archs = ["default-archs", "z80", "m6502"]
3 changes: 2 additions & 1 deletion binfmt/src/elf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,8 @@ pub mod consts {
EM_CSKY = 252, // C-SKY 32-bit processor
EM_WC65C816 = 257, // 65816/65c816

EM_CLEVER = 0x434C, // Clever-ISA
EM_CLEVER = 0x434C, // Clever-ISA
EM_HOLEYBYTES = 0xAB1E, // Holey Bytes
}
}

Expand Down
3 changes: 3 additions & 0 deletions binfmt/src/elf64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ pub mod x86_64;
#[cfg(feature = "clever")]
pub mod clever;

#[cfg(feature = "holeybytes")]
pub mod holeybytes;

pub mod genericle {
pub fn create_format() -> super::Elf64Format<crate::elf::ElfHowToUnknown> {
super::Elf64Format::new(
Expand Down
133 changes: 133 additions & 0 deletions binfmt/src/elf64/holeybytes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
use super::Elf64Format;

macro_rules! gen {
(count: $count:expr; $($variant:ident),* $(,)?) => {
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[repr(u8)]
#[non_exhaustive]
pub enum Elf64HoleyBytesHowTo
{ $($variant),* }

static TABLE: [Elf64HoleyBytesHowTo; $count] =
[$(Elf64HoleyBytesHowTo::$variant),*];
};
}

gen! {
count: 13;
None,
Abs64,
Rel16,
Rel32,
RelaxRel,
GotPcrel,
Plt,
GotPcrelRelax,
PltRelax,
Dynent,
JumpSlot,
GlobData,
TpOff,
DynTpOff,
GotDynTpOff,
}

impl HowTo for Elf64HoleyBytesHowTo {
#[inline]
fn from_relnum<'a>(num: u32) -> Option<&'a Self>
where
Self: Sized + 'a,
{
TABLE.get(num as usize)
}

fn from_reloc_code<'a>(code: RelocCode) -> Option<&'a Self>
where
Self: Sized + 'a,
{
Some(match code {
RelocCode::None => &TABLE[Self::None as usize],
RelocCode::Abs { addr_width: 64 } => &TABLE[Self::Abs64 as usize],
RelocCode::BaseRel { addr_width } => todo!(),
RelocCode::Rel { addr_width: 16 } => &TABLE[Self::Rel16 as usize],
RelocCode::Rel { addr_width: 32 } => &TABLE[Self::Rel32 as usize],
RelocCode::AbsShifted { addr_width, shift } => todo!(),
RelocCode::RelShifted { addr_width, shift } => todo!(),
RelocCode::Got { addr_width } => todo!(),
RelocCode::RelGot { addr_wdith } => todo!(),
RelocCode::Plt { addr_width } => todo!(),
RelocCode::RelPlt { addr_width } => todo!(),
RelocCode::DynSymEntry { width } => todo!(),
_ => return None,
})
}

fn reloc_num(&self) -> u32 {
*self as u32
}

fn name(&self) -> &'static str {
use Elf64HoleyBytesHowTo::*;
match self {
None => "R_HOLEYBYTES_NONE",
Abs64 => "R_HOLEYBYTES_ABS64",
Rel16 => "R_HOLEYBYTES_REL16",
Rel32 => "R_HOLEYBYTES_REL32",
RelaxRel => "R_HOLEYBYTES_RELAXREL",
GotPcrel => "R_HOLEYBYTES_GOTPCREL",
Plt => "R_HOLEYBYTES_PLT",
GotPcrelRelax => "R_HOLEYBYTES_GOTPCRELRELAX",
PltRelax => "R_HOLEYBYTES_PLTRELAX",
Dynent => "R_HOLEYBYTES_DYNENT",
JumpSlot => "R_HOLEYBYTES_JUMPSLOT",
GlobData => "R_HOLEYBYTES_GLOBDATA",
TpOff => "R_HOLEYBYTES_TPOFF",
DynTpOff => "R_HOLEYBYTES_DYNTPOFF",
GotDynTpOff => "R_HOLEYBYTES_GOTDYNTPOFF",
}
}

fn reloc_size(&self) -> usize {
use Elf64HoleyBytesHowTo::*;
match self {
None => 0,
Rel16 => 2,
Rel32 | GotPcrel | Plt | GotDynTpOff => 4,
RelaxRel | GotPcrelRelax | PltRelax => 5,
Abs64 | Dynent | JumpSlot | GlobData | TpOff | DynTpOff => 8,
}
}

fn pcrel(&self) -> bool {
use Elf64HoleyBytesHowTo::*;
match self {
None | Abs64 | Dynent | JumpSlot | GlobData | TpOff | DynTpOff => false,
Rel16 | Rel32 | RelaxRel | GotPcrel | Plt | GotPcrelRelax | PltRelax | GotDynTpOff => {
true
}
}
}

fn is_relax(&self) -> bool {
use Elf64HoleyBytesHowTo::*;
matches!(self, RelaxRel | GotPcrelRelax | PltRelax)
}

fn relax_size(&self, addr: u128, at_addr: u128) -> Option<usize> {
unimplemented!("Will be removed soon™?")
}

fn apply(&self, addr: u128, at_addr: u128, region: &mut [u8]) -> Result<bool, HowToError> {
todo!()
}
}

pub fn create_format() -> Elf64Format<Elf64HoleyBytesHowTo> {
Elf64Format::new(
super::consts::EM_HOLEYBYTES,
super::consts::ELFDATA2LSB,
"elf64-holeybytes",
None,
None,
)
}
5 changes: 5 additions & 0 deletions binfmt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ define_formats![
elf64-x86_64,
#[cfg(all(feature = "elf64", feature = "clever"))]
elf64-clever,
#[cfg(all(feature = "elf64", feature = "holeybytes"))]
elf64-holeybytes,
#[cfg(feature = "elf32")]
elf32-genericle,
#[cfg(feature = "elf32")]
Expand Down Expand Up @@ -145,6 +147,9 @@ pub fn def_vec_for(targ: &Target) -> &'static (dyn crate::fmt::Binfmt + Sync + S
clever-*-elf => &*BINARY_FORMATS_BY_NAME["elf64-clever"],
clever-*-cleveros => &*BINARY_FORMATS_BY_NAME["elf64-clever"],
clever-*-*-elf => &*BINARY_FORMATS_BY_NAME["elf64-clever"],
holeybytes-*-elf => &*BINARY_FORMATS_BY_NAME["elf64-holeybytes"],
holeybytes-*-*-elf => &*BINARY_FORMATS_BY_NAME["elf64-holeybytes"],
holeybytes-*-ableos => &*BINARY_FORMATS_BY_NAME["elf64-holeybytes"],
* => panic!("Unknown Target")
}
}
Expand Down

0 comments on commit 2c2c0f9

Please sign in to comment.