This commit is contained in:
nora 2023-02-16 20:40:05 +01:00
parent 4b41166d5f
commit c38aa61857
5 changed files with 60 additions and 50 deletions

View file

@ -1,6 +1,45 @@
#![allow(clippy::must_use_candidate, clippy::missing_errors_doc)]
use std::fmt::{Debug, Display};
use bytemuck::{Pod, Zeroable};
use idx::ToIdxUsize;
pub mod consts;
mod idx;
pub mod read;
pub mod write;
/// A _run time_ address inside an object file.
#[derive(Clone, Copy, PartialEq, Eq, Hash, Zeroable, Pod)]
#[repr(transparent)]
pub struct Addr(pub u64);
impl Debug for Addr {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "0x{:x}", self.0)
}
}
impl Display for Addr {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "0x{:x}", self.0)
}
}
/// An offset into an object file. Either absolut or relative to a particular section.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Zeroable, Pod)]
#[repr(transparent)]
pub struct Offset(pub u64);
impl ToIdxUsize for Offset {
fn to_idx_usize(self) -> usize {
self.0.to_idx_usize()
}
}
impl Display for Offset {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "0x{:x}", self.0)
}
}

View file

@ -5,6 +5,7 @@
use crate::{
consts::{self as c, DynamicTag, ShType},
idx::{define_idx, ElfIndexExt, ToIdxUsize},
Addr, Offset,
};
use bstr::BStr;
@ -44,32 +45,6 @@ pub enum ElfReadError {
pub type Result<T> = std::result::Result<T, ElfReadError>;
#[derive(Clone, Copy, PartialEq, Eq, Hash, Zeroable, Pod)]
#[repr(transparent)]
pub struct Addr(pub u64);
impl Debug for Addr {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "0x{:x}", self.0)
}
}
impl Display for Addr {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "0x{:x}", self.0)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Zeroable, Pod)]
#[repr(transparent)]
pub struct Offset(pub u64);
impl ToIdxUsize for Offset {
fn to_idx_usize(self) -> usize {
self.0.to_idx_usize()
}
}
define_idx! {
pub struct ShStringIdx(u32);
}
@ -107,16 +82,6 @@ pub struct ElfHeader {
pub shstrndex: c::SectionIdx,
}
pub const HEADER_ENTRY_OFFSET: usize = 24;
#[test]
fn elf_header_entry_offset() {
let mut header = ElfHeader::zeroed();
let buf = bytemuck::cast_mut::<ElfHeader, [u64; mem::size_of::<ElfHeader>() / 8]>(&mut header);
buf[HEADER_ENTRY_OFFSET / 8] = 0x001122334455667788;
assert_eq!(header.entry, Addr(0x001122334455667788));
}
#[derive(Debug, Clone, Copy, Zeroable, Pod)]
#[repr(C)]
pub struct ElfIdent {

View file

@ -3,7 +3,8 @@ use bytemuck::Pod;
use crate::consts::{
Machine, PhFlags, PhType, SectionIdx, ShFlags, ShType, Type, SHT_NULL, SHT_STRTAB,
};
use crate::read::{self, Addr, ElfHeader, ElfIdent, Offset, Phdr, ShStringIdx, Shdr};
use crate::read::{self, ElfHeader, ElfIdent, Phdr, ShStringIdx, Shdr};
use crate::{Addr, Offset};
use std::io::Write;
use std::mem::size_of;
use std::num::NonZeroU64;
@ -355,7 +356,7 @@ fn align_up(n: u64, align: u64) -> u64 {
let next_down = n - masked; // 0b0100
let ret = next_down + align; // 0b0110
debug_assert!(ret >= n);
debug_assert!(ret & align == 0);
debug_assert!(ret & required_mask == 0);
ret
}