mirror of
https://github.com/Noratrieb/elven-forest.git
synced 2026-01-14 10:45:03 +01:00
addr
This commit is contained in:
parent
c38aa61857
commit
0df129d612
4 changed files with 71 additions and 44 deletions
|
|
@ -188,7 +188,7 @@ fn print_file(path: &str) -> anyhow::Result<()> {
|
||||||
|
|
||||||
let symbol = sym_display_name(elf, sym)?;
|
let symbol = sym_display_name(elf, sym)?;
|
||||||
|
|
||||||
let offset = Addr(rela.offset.0);
|
let offset = rela.offset;
|
||||||
let r#type = c::RX86_64(rela.info.r#type());
|
let r#type = c::RX86_64(rela.info.r#type());
|
||||||
let addend = rela.addend;
|
let addend = rela.addend;
|
||||||
|
|
||||||
|
|
|
||||||
64
elven-parser/src/addrs.rs
Normal file
64
elven-parser/src/addrs.rs
Normal file
|
|
@ -0,0 +1,64 @@
|
||||||
|
use std::{
|
||||||
|
fmt::{Debug, Display},
|
||||||
|
ops::Add,
|
||||||
|
};
|
||||||
|
|
||||||
|
use crate::idx::ToIdxUsize;
|
||||||
|
use bytemuck::{Pod, Zeroable};
|
||||||
|
|
||||||
|
/// A _run time_ address inside an object file.
|
||||||
|
#[derive(Clone, Copy, PartialEq, Eq, Hash, Zeroable, Pod)]
|
||||||
|
#[repr(transparent)]
|
||||||
|
pub struct Addr {
|
||||||
|
value: u64,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(non_snake_case)]
|
||||||
|
pub const fn Addr(value: u64) -> Addr {
|
||||||
|
Addr { value }
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Debug for Addr {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
write!(f, "0x{:x}", self.value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Display for Addr {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
write!(f, "0x{:x}", self.value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Add<Self> for Addr {
|
||||||
|
type Output = Self;
|
||||||
|
|
||||||
|
fn add(self, rhs: Self) -> Self::Output {
|
||||||
|
self + rhs.value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Add<u64> for Addr {
|
||||||
|
type Output = Self;
|
||||||
|
|
||||||
|
fn add(self, rhs: u64) -> Self::Output {
|
||||||
|
Addr(self.value + rhs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,45 +1,9 @@
|
||||||
#![allow(clippy::must_use_candidate, clippy::missing_errors_doc)]
|
#![allow(clippy::must_use_candidate, clippy::missing_errors_doc)]
|
||||||
|
|
||||||
use std::fmt::{Debug, Display};
|
mod addrs;
|
||||||
|
|
||||||
use bytemuck::{Pod, Zeroable};
|
|
||||||
use idx::ToIdxUsize;
|
|
||||||
|
|
||||||
pub mod consts;
|
pub mod consts;
|
||||||
mod idx;
|
mod idx;
|
||||||
pub mod read;
|
pub mod read;
|
||||||
pub mod write;
|
pub mod write;
|
||||||
|
|
||||||
/// A _run time_ address inside an object file.
|
pub use crate::addrs::{Addr, Offset};
|
||||||
#[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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -59,7 +59,7 @@ pub fn run(opts: Opts) -> Result<()> {
|
||||||
|
|
||||||
let _start_sym = elf.symbol_by_name(b"_start")?;
|
let _start_sym = elf.symbol_by_name(b"_start")?;
|
||||||
|
|
||||||
write_output(text_content, _start_sym.value.0)?;
|
write_output(text_content, _start_sym.value)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
@ -67,7 +67,7 @@ pub fn run(opts: Opts) -> Result<()> {
|
||||||
pub const BASE_EXEC_ADDR: Addr = Addr(0x400000); // whatever ld does
|
pub const BASE_EXEC_ADDR: Addr = Addr(0x400000); // whatever ld does
|
||||||
pub const DEFAULT_PROGRAM_HEADER_ALIGN_THAT_LD_USES_HERE: u64 = 0x1000;
|
pub const DEFAULT_PROGRAM_HEADER_ALIGN_THAT_LD_USES_HERE: u64 = 0x1000;
|
||||||
|
|
||||||
fn write_output(text: &[u8], entry_offset_from_text: u64) -> Result<()> {
|
fn write_output(text: &[u8], entry_offset_from_text: Addr) -> Result<()> {
|
||||||
let ident = ElfIdent {
|
let ident = ElfIdent {
|
||||||
magic: *c::ELFMAG,
|
magic: *c::ELFMAG,
|
||||||
class: c::Class(c::ELFCLASS64),
|
class: c::Class(c::ELFCLASS64),
|
||||||
|
|
@ -112,9 +112,8 @@ fn write_output(text: &[u8], entry_offset_from_text: u64) -> Result<()> {
|
||||||
|
|
||||||
write.add_program_header(elf_header_and_program_headers);
|
write.add_program_header(elf_header_and_program_headers);
|
||||||
|
|
||||||
let entry_addr = Addr(
|
let entry_addr =
|
||||||
BASE_EXEC_ADDR.0 + DEFAULT_PROGRAM_HEADER_ALIGN_THAT_LD_USES_HERE + entry_offset_from_text,
|
BASE_EXEC_ADDR + DEFAULT_PROGRAM_HEADER_ALIGN_THAT_LD_USES_HERE + entry_offset_from_text;
|
||||||
);
|
|
||||||
|
|
||||||
let text_program_header = ProgramHeader {
|
let text_program_header = ProgramHeader {
|
||||||
r#type: PT_LOAD.into(),
|
r#type: PT_LOAD.into(),
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue