initial commit
This commit is contained in:
143
src/ints.rs
Normal file
143
src/ints.rs
Normal file
@@ -0,0 +1,143 @@
|
||||
use bytemuck::{Pod, Zeroable};
|
||||
use core::fmt::Debug;
|
||||
use core::marker::PhantomData;
|
||||
use serde::Serialize;
|
||||
|
||||
#[derive(Pod, Zeroable, Clone, Copy, Debug, Serialize)]
|
||||
#[repr(C, packed)]
|
||||
pub struct BigEndian;
|
||||
|
||||
#[derive(Pod, Zeroable, Clone, Copy, Debug, Serialize)]
|
||||
#[repr(C, packed)]
|
||||
pub struct LittleEndian;
|
||||
|
||||
pub trait Decode: Copy {
|
||||
type Native: Debug + Copy;
|
||||
fn to_native(self) -> Self::Native;
|
||||
}
|
||||
|
||||
#[derive(Pod, Zeroable, Clone, Copy)]
|
||||
#[repr(C, packed)]
|
||||
pub struct U64<E> {
|
||||
raw: [u8; 8],
|
||||
_endian: PhantomData<E>,
|
||||
}
|
||||
|
||||
#[derive(Pod, Zeroable, Clone, Copy)]
|
||||
#[repr(C, packed)]
|
||||
pub struct U32<E> {
|
||||
raw: [u8; 4],
|
||||
_endian: PhantomData<E>,
|
||||
}
|
||||
|
||||
#[derive(Pod, Zeroable, Clone, Copy)]
|
||||
#[repr(C, packed)]
|
||||
pub struct U16<E> {
|
||||
raw: [u8; 2],
|
||||
_endian: PhantomData<E>,
|
||||
}
|
||||
|
||||
impl Decode for U64<BigEndian> {
|
||||
type Native = u64;
|
||||
fn to_native(self) -> Self::Native {
|
||||
u64::from_be_bytes(self.raw)
|
||||
}
|
||||
}
|
||||
|
||||
impl Decode for U64<LittleEndian> {
|
||||
type Native = u64;
|
||||
fn to_native(self) -> Self::Native {
|
||||
u64::from_le_bytes(self.raw)
|
||||
}
|
||||
}
|
||||
|
||||
impl Decode for U32<BigEndian> {
|
||||
type Native = u32;
|
||||
fn to_native(self) -> Self::Native {
|
||||
u32::from_be_bytes(self.raw)
|
||||
}
|
||||
}
|
||||
|
||||
impl Decode for U32<LittleEndian> {
|
||||
type Native = u32;
|
||||
fn to_native(self) -> Self::Native {
|
||||
u32::from_le_bytes(self.raw)
|
||||
}
|
||||
}
|
||||
|
||||
impl Decode for U16<BigEndian> {
|
||||
type Native = u16;
|
||||
fn to_native(self) -> Self::Native {
|
||||
u16::from_be_bytes(self.raw)
|
||||
}
|
||||
}
|
||||
|
||||
impl Decode for U16<LittleEndian> {
|
||||
type Native = u16;
|
||||
fn to_native(self) -> Self::Native {
|
||||
u16::from_le_bytes(self.raw)
|
||||
}
|
||||
}
|
||||
|
||||
impl<E> Debug for U64<E>
|
||||
where
|
||||
Self: Decode,
|
||||
{
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
Debug::fmt(&self.to_native(), f)
|
||||
}
|
||||
}
|
||||
|
||||
impl<E> Debug for U32<E>
|
||||
where
|
||||
Self: Decode,
|
||||
{
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
Debug::fmt(&self.to_native(), f)
|
||||
}
|
||||
}
|
||||
|
||||
impl<E> Debug for U16<E>
|
||||
where
|
||||
Self: Decode,
|
||||
{
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
Debug::fmt(&self.to_native(), f)
|
||||
}
|
||||
}
|
||||
|
||||
impl<E> Serialize for U64<E>
|
||||
where
|
||||
Self: Decode,
|
||||
{
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: serde::Serializer,
|
||||
{
|
||||
format!("{:?}", self.to_native()).serialize(serializer)
|
||||
}
|
||||
}
|
||||
|
||||
impl<E> Serialize for U32<E>
|
||||
where
|
||||
Self: Decode,
|
||||
{
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: serde::Serializer,
|
||||
{
|
||||
format!("{:?}", self.to_native()).serialize(serializer)
|
||||
}
|
||||
}
|
||||
|
||||
impl<E> Serialize for U16<E>
|
||||
where
|
||||
Self: Decode,
|
||||
{
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: serde::Serializer,
|
||||
{
|
||||
format!("{:?}", self.to_native()).serialize(serializer)
|
||||
}
|
||||
}
|
||||
208
src/main.rs
Normal file
208
src/main.rs
Normal file
@@ -0,0 +1,208 @@
|
||||
mod ints;
|
||||
mod output;
|
||||
mod structs;
|
||||
|
||||
use crate::{
|
||||
ints::BigEndian,
|
||||
output::Output,
|
||||
structs::{ElfHeader1, ElfHeader2, SectionHeaderEntry, SymbolTableEntry},
|
||||
};
|
||||
use bytemuck::from_bytes;
|
||||
use clap::{Parser, Subcommand};
|
||||
use core::str::from_utf8;
|
||||
use eyre::{bail, eyre, Context};
|
||||
use ints::{Decode, LittleEndian, U32, U64};
|
||||
use serde_json::Value;
|
||||
use std::{fs, mem::size_of, path::PathBuf};
|
||||
|
||||
#[derive(Parser)]
|
||||
struct Opt {
|
||||
elf: PathBuf,
|
||||
|
||||
#[clap(subcommand)]
|
||||
command: Command,
|
||||
}
|
||||
|
||||
#[derive(Subcommand)]
|
||||
enum Command {
|
||||
/// List sections
|
||||
Sections {},
|
||||
|
||||
/// List symbols
|
||||
Symbols {
|
||||
#[clap(short, long)]
|
||||
section: String,
|
||||
},
|
||||
}
|
||||
const HEADER1_LEN: usize = size_of::<ElfHeader1>();
|
||||
|
||||
macro_rules! parse_elf {
|
||||
($endian:ty, $word:ty, $out:expr, $header1:expr, $elf:expr) => {{
|
||||
//let header1 = $header1;
|
||||
let out = $out;
|
||||
const HEADER2_LEN: usize = size_of::<ElfHeader2<$endian, $word>>();
|
||||
let elf = $elf;
|
||||
if elf.len() < HEADER1_LEN + HEADER2_LEN {
|
||||
bail!("not a valid elf file: file too small");
|
||||
}
|
||||
|
||||
let header2: &[u8; HEADER2_LEN] = elf[HEADER1_LEN..][..HEADER2_LEN].try_into()?;
|
||||
let header2: ElfHeader2<$endian, $word> = *from_bytes(header2);
|
||||
out.write("header2", &header2)?;
|
||||
|
||||
let e_shentsize = usize::from(header2.e_shentsize.to_native());
|
||||
if e_shentsize != size_of::<SectionHeaderEntry<$endian, $word>>() {
|
||||
bail!("wrong e_shentsize (0x{e_shentsize:x})");
|
||||
}
|
||||
|
||||
let e_shnum = usize::from(header2.e_shnum.to_native());
|
||||
let section_header_table_size = e_shentsize * e_shnum;
|
||||
let section_header_table_start =
|
||||
usize::try_from(header2.e_shoff.to_native()).wrap_err("e_shoff bigger than usize")?;
|
||||
let section_header_table_end = section_header_table_start + section_header_table_size;
|
||||
|
||||
if elf.len() < section_header_table_end {
|
||||
bail!("not a valid elf file: section table goes past eof");
|
||||
} else if elf.len() > section_header_table_end {
|
||||
bail!("not a valid elf file: section table doesn't end at eof");
|
||||
}
|
||||
|
||||
let mut section_header_entries = vec![];
|
||||
for entry_start in
|
||||
(section_header_table_start..section_header_table_end).step_by(e_shentsize)
|
||||
{
|
||||
let entry = &elf[entry_start..][..e_shentsize];
|
||||
let entry: SectionHeaderEntry<$endian, $word> = *from_bytes(entry);
|
||||
section_header_entries.push(entry);
|
||||
}
|
||||
out.write("section_table", §ion_header_entries)?;
|
||||
|
||||
let names_section_i = usize::from(header2.e_shstrndx.to_native());
|
||||
let names_section_entry = §ion_header_entries
|
||||
.get(names_section_i)
|
||||
.ok_or(eyre!("invalid e_shstrndx"))?;
|
||||
|
||||
let names_section_start: usize = (names_section_entry.sh_offset.to_native().try_into())
|
||||
.wrap_err("sh_offset bigger than usize")?;
|
||||
let names_section_size: usize = (names_section_entry.sh_size.to_native().try_into())
|
||||
.wrap_err("sh_size bigger than usize")?;
|
||||
let names_section_end = names_section_start + names_section_size;
|
||||
let names_section = &elf[names_section_start..names_section_end];
|
||||
|
||||
let mut symtab_i = None;
|
||||
let mut strtab = None;
|
||||
|
||||
let mut section_header_entries_with_names = vec![];
|
||||
for (i, entry) in section_header_entries.iter().enumerate() {
|
||||
let name_start: usize = (entry.sh_name.to_native())
|
||||
.try_into()
|
||||
.wrap_err("sh_name bigger than usize")?;
|
||||
|
||||
if name_start > names_section.len() {
|
||||
bail!("sh_name out of bounds for .names section");
|
||||
}
|
||||
|
||||
let name_end = (name_start..names_section.len())
|
||||
.find(|&i| names_section[i] == b'\0')
|
||||
.ok_or(eyre!(".names entry missing null byte"))?;
|
||||
let name = &names_section[name_start..name_end];
|
||||
let name = from_utf8(name).wrap_err(eyre!(".names entry not valid utf-8"))?;
|
||||
let section_start = entry.start().wrap_err_with(|| name.to_string())?;
|
||||
let section_end = entry.end().wrap_err_with(|| name.to_string())?;
|
||||
if section_end > elf.len() {
|
||||
bail!("section {name:?} end is past eof");
|
||||
}
|
||||
|
||||
match name {
|
||||
".symtab" => symtab_i = Some(i),
|
||||
".strtab" => {
|
||||
strtab = Some(&elf[section_start..section_end]);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
let mut entry_json =
|
||||
serde_json::to_value(entry).wrap_err("failed to serialize section header")?;
|
||||
|
||||
let Value::Object(fields) = &mut entry_json else {
|
||||
unreachable!()
|
||||
};
|
||||
|
||||
fields.insert("name".to_string(), Value::String(name.to_string()));
|
||||
section_header_entries_with_names.push(entry_json);
|
||||
}
|
||||
let _ = out.write("section_table", §ion_header_entries_with_names);
|
||||
|
||||
if let Some(symtab_i) = symtab_i {
|
||||
let symtab = §ion_header_entries[symtab_i];
|
||||
const ENTRY_SIZE: usize = size_of::<SymbolTableEntry<$word>>();
|
||||
if symtab.sh_size.to_native() as usize != ENTRY_SIZE {
|
||||
bail!(".symtab size not a multiple of symtab entry size");
|
||||
}
|
||||
|
||||
let section_start: usize = symtab.start().wrap_err(".symtab")?;
|
||||
let section_end: usize = symtab.end().wrap_err(".symtab")?;
|
||||
|
||||
for entry in elf[section_start..section_end].chunks_exact(ENTRY_SIZE) {
|
||||
let entry: &[u8; ENTRY_SIZE] = entry.try_into().unwrap();
|
||||
let entry: &SymbolTableEntry<$word> = from_bytes(entry);
|
||||
let name_start: usize = (entry.st_name.to_native().try_into())
|
||||
.wrap_err("symtab entry st_name too big to fit in usize")?;
|
||||
|
||||
let name = (name_start != 0)
|
||||
.then(|| {
|
||||
strtab.map(|strtab| {
|
||||
eprintln!("{name_start} {}", strtab.len());
|
||||
let name_end = (name_start..strtab.len())
|
||||
.find(|&i| strtab[i] == b'\0')
|
||||
.ok_or(eyre!(".strtab entry missing null byte"))?;
|
||||
let name = &strtab[name_start..name_end];
|
||||
from_utf8(name).wrap_err(eyre!(
|
||||
".strtab entry at 0x{name_start:x} is not valid utf-8"
|
||||
))
|
||||
})
|
||||
})
|
||||
// Option<Option<Result>> to Result<Option>
|
||||
.flatten()
|
||||
.transpose()?;
|
||||
|
||||
eprintln!("[.symtab] {name:?}: {entry:?}");
|
||||
}
|
||||
}
|
||||
}};
|
||||
}
|
||||
|
||||
fn main() -> eyre::Result<()> {
|
||||
let opt = Opt::parse();
|
||||
color_eyre::install()?;
|
||||
|
||||
let elf = fs::read(opt.elf).wrap_err("failed to read elf file")?;
|
||||
|
||||
if elf.len() < HEADER1_LEN {
|
||||
bail!("not a valid elf file: file too small");
|
||||
}
|
||||
let header1: &[u8; HEADER1_LEN] = elf[..HEADER1_LEN].try_into()?;
|
||||
let header1: ElfHeader1 = *from_bytes(header1);
|
||||
|
||||
let mut out = Output::default();
|
||||
out.write("header1", &header1)?;
|
||||
|
||||
if &header1.ei_magic != b"\x7FELF" {
|
||||
bail!("not a valid elf file: invalid magic");
|
||||
}
|
||||
|
||||
if header1.ei_version != 1 {
|
||||
bail!("unknown elf version: 0x{:x}", header1.ei_version);
|
||||
}
|
||||
|
||||
match (header1.ei_class, header1.ei_data) {
|
||||
(1, 1) => parse_elf!(LittleEndian, U32<LittleEndian>, &mut out, header1, elf),
|
||||
(2, 1) => parse_elf!(LittleEndian, U64<LittleEndian>, &mut out, header1, elf),
|
||||
(1, 2) => parse_elf!(BigEndian, U32<BigEndian>, &mut out, header1, elf),
|
||||
(2, 2) => parse_elf!(BigEndian, U64<BigEndian>, &mut out, header1, elf),
|
||||
(1 | 2, ei_data) => bail!("unknown e_ident[EI_DATA]: 0x{ei_data:x}"),
|
||||
(ei_class, ..) => bail!("unknown e_ident[EI_CLASS]: 0x{ei_class:x}"),
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
28
src/output.rs
Normal file
28
src/output.rs
Normal file
@@ -0,0 +1,28 @@
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
use eyre::{bail, Context};
|
||||
use serde::Serialize;
|
||||
use serde_json::Value;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Output {
|
||||
elements: BTreeMap<&'static str, Value>,
|
||||
}
|
||||
|
||||
impl Output {
|
||||
pub fn write(&mut self, key: &'static str, v: &impl Serialize) -> eyre::Result<()> {
|
||||
let v = serde_json::to_value(v).wrap_err("failed to serialize output")?;
|
||||
if self.elements.insert(key, v).is_some() {
|
||||
bail!("duplicated output key {key:?}");
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Output {
|
||||
fn drop(&mut self) {
|
||||
let out =
|
||||
serde_json::to_string_pretty(&self.elements).expect("can serialize output as json");
|
||||
println!("{out}",);
|
||||
}
|
||||
}
|
||||
96
src/structs.rs
Normal file
96
src/structs.rs
Normal file
@@ -0,0 +1,96 @@
|
||||
use crate::ints::{Decode, U16, U32};
|
||||
use bytemuck::{Pod, Zeroable};
|
||||
use eyre::eyre;
|
||||
use serde::Serialize;
|
||||
|
||||
#[derive(Pod, Zeroable, Clone, Copy, Debug, Serialize)]
|
||||
#[repr(C, packed)]
|
||||
pub struct ElfHeader1 {
|
||||
pub ei_magic: [u8; 4],
|
||||
pub ei_class: u8,
|
||||
pub ei_data: u8,
|
||||
pub ei_version: u8,
|
||||
pub ei_osabi: u8,
|
||||
pub ei_abiversion: u8,
|
||||
#[serde(skip)]
|
||||
_padding: [u8; 7],
|
||||
}
|
||||
|
||||
#[derive(Pod, Zeroable, Clone, Copy, Debug, Serialize)]
|
||||
#[repr(C, packed)]
|
||||
pub struct ElfHeader2<Endian, Word>
|
||||
where
|
||||
U16<Endian>: Decode,
|
||||
U32<Endian>: Decode,
|
||||
Word: Decode,
|
||||
{
|
||||
pub e_type: U16<Endian>,
|
||||
pub e_machine: U16<Endian>,
|
||||
pub e_version: U32<Endian>,
|
||||
pub e_entry: Word,
|
||||
pub e_phoff: Word,
|
||||
pub e_shoff: Word,
|
||||
pub e_flags: U32<Endian>,
|
||||
pub e_ehsize: U16<Endian>,
|
||||
pub e_phentsize: U16<Endian>,
|
||||
pub e_phnum: U16<Endian>,
|
||||
pub e_shentsize: U16<Endian>,
|
||||
pub e_shnum: U16<Endian>,
|
||||
pub e_shstrndx: U16<Endian>,
|
||||
}
|
||||
|
||||
#[derive(Pod, Zeroable, Clone, Copy, Debug, Serialize)]
|
||||
#[repr(C, packed)]
|
||||
pub struct SectionHeaderEntry<Endian, Word>
|
||||
where
|
||||
U16<Endian>: Decode,
|
||||
U32<Endian>: Decode,
|
||||
Word: Decode,
|
||||
{
|
||||
pub sh_name: U32<Endian>,
|
||||
pub sh_type: U32<Endian>,
|
||||
pub sh_flags: Word,
|
||||
pub sh_addr: Word,
|
||||
pub sh_offset: Word,
|
||||
pub sh_size: Word,
|
||||
pub sh_link: U32<Endian>,
|
||||
pub sh_info: U32<Endian>,
|
||||
pub sh_addralign: Word,
|
||||
pub sh_entsize: Word,
|
||||
}
|
||||
|
||||
#[derive(Pod, Zeroable, Clone, Copy, Debug, Serialize)]
|
||||
#[repr(C, packed)]
|
||||
pub struct SymbolTableEntry<Word: Decode> {
|
||||
pub st_name: Word,
|
||||
pub st_value: Word,
|
||||
pub st_size: Word,
|
||||
pub st_info: u8,
|
||||
pub st_other: u8,
|
||||
pub st_shndx: Word,
|
||||
}
|
||||
|
||||
impl<Endian, Word> SectionHeaderEntry<Endian, Word>
|
||||
where
|
||||
U16<Endian>: Decode,
|
||||
U32<Endian>: Decode,
|
||||
Word: Decode,
|
||||
<Word as Decode>::Native: TryInto<usize>,
|
||||
{
|
||||
pub fn start(&self) -> eyre::Result<usize> {
|
||||
self.sh_offset
|
||||
.to_native()
|
||||
.try_into()
|
||||
.map_err(|_| eyre!("sh_offset bigger than usize"))
|
||||
}
|
||||
|
||||
pub fn end(&self) -> eyre::Result<usize> {
|
||||
let size: usize = self
|
||||
.sh_size
|
||||
.to_native()
|
||||
.try_into()
|
||||
.map_err(|_| eyre!("sh_size bigger than usize"))?;
|
||||
|
||||
Ok(self.start()? + size)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user