m4b / m4b/goblin

#[repr(C)] structs missing Cread/Cwrite implementations

Open
#421 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
1.5k
Forks
202
PR merge metrics
No merged PRs in 30d

Description

All the `#[repr(C)]` structs implement Pread/Pwrite but do not implement Cread/Cwrite which is useful in no_std environments.

I'd be happy to implement Cread/Cwrite for the ELF structs (with tests), but wanted to know if this is something that is wanted for the project and if the implementation below is the recommend way to do this.

Here is my implementation for the ELF header:
```rust
use core::mem;
use scroll::{Cread, Cwrite, Endian};

impl ctx::FromCtx for Header {
#[inline]
fn from_ctx(src: &[u8], le: Endian) -> Self {
assert!(src.len() >= SIZEOF_EHDR);
let mut elf_header = Header::default();
for i in 0..SIZEOF_IDENT {
elf_header.e_ident[i] = src.cread(i);
}
let endianness = match elf_header.e_ident[EI_DATA] {
ELFDATA2LSB => scroll::LE,
ELFDATA2MSB => scroll::BE,
// fallback to provided endian
_ => le,
};
elf_header.e_type = src.cread_with(SIZEOF_IDENT, endianness);
elf_header.e_machine = src.cread_with(SIZEOF_IDENT + 2, endianness);
elf_header.e_version = src.cread_with(SIZEOF_IDENT + 4, endianness);
elf_header.e_entry = src.cread_with(SIZEOF_IDENT + 8, endianness);
elf_header.e_phoff =
src.cread_with(SIZEOF_IDENT + 8 + mem::size_of::<$size>(), endianness);
elf_header.e_shoff =
src.cread_with(SIZEOF_IDENT + 8 + 2 * mem::size_of::<$size>(), endianness);
...
elf_header.e_shstrndx =
src.cread_with(SIZEOF_IDENT + 22 + 3 * mem::size_of::<$size>(), endianness);
elf_header
}
}
impl ctx::IntoCtx for Header {
fn into_ctx(self, bytes: &mut [u8], le: Endian) {
assert!(bytes.len() >= SIZEOF_EHDR);
let endianness = match self.e_ident[EI_DATA] {
ELFDATA2LSB => scroll::LE,
ELFDATA2MSB => scroll::BE,
// fallback to provided endian
_ => le,
};
for i in 0..self.e_ident.len() {
bytes.cwrite(self.e_ident[i], i);
}
bytes.cwrite_with(self.e_type, SIZEOF_IDENT, endianness);
bytes.cwrite_with(self.e_machine, SIZEOF_IDENT + 2, endianness);
...
bytes.cwrite_with(
self.e_shstrndx,
SIZEOF_IDENT + 22 + 3 * mem::size_of::<$size>(),
endianness,
);
}
}
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by locating the ELF structs marked #[repr(C)] and their existing Pread/Pwrite implementations. Review the proposed Header Cread/Cwrite approach and the project's current tests before extending it to the relevant structs. Done means the intended ELF structs support Cread/Cwrite and have coverage for the new behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
reverse-engineering
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.