From 2de6345f963916fa97ee9e0df95de3d2a32a91ac Mon Sep 17 00:00:00 2001 From: Keenan Tims Date: Mon, 6 Nov 2023 00:35:21 -0800 Subject: [PATCH] Minor cleanup, commented-out beginnings of elf support --- src/file.rs | 59 +++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 53 insertions(+), 6 deletions(-) diff --git a/src/file.rs b/src/file.rs index 38b87ac..e05d912 100644 --- a/src/file.rs +++ b/src/file.rs @@ -1,14 +1,10 @@ -extern crate object; - use std::fs::File; use std::io::prelude::*; use std::iter::repeat; -use std::path::Path; use std::path::PathBuf; use clap::ValueEnum; -use log::debug; -use log::warn; +use log::{debug, warn}; use object::elf; use object::Endianness; @@ -34,7 +30,9 @@ struct ElfWriter; struct IhexWriter; struct BinWriter; -struct ElfReader; +struct ElfReader { + file_contents: Vec, +} struct IhexReader { file_contents: String, } @@ -350,3 +348,52 @@ impl ObjectFileReader for IhexReader { }) } } + +//TODO: This is getting a bit hairy to figure out which sections to include, so forget about it for now. + +// impl ObjectFileReader for ElfReader { +// fn new(file: &PathBuf) -> Result, std::io::Error> +// where +// Self: Sized, +// { +// let file_contents = std::fs::read(file)?; +// Ok(Box::new(ElfReader { file_contents })) +// } +// fn read_all(&mut self, size: Option) -> Result> { +// let object = object::File::parse(&*self.file_contents).unwrap(); +// let data = Vec::new(); +// let file_base = None; + +// debug!( +// "Opened object file, found sections: {:?}", +// object +// .sections() +// .map(|x| x.name().unwrap().to_owned()) +// .collect::>() +// ); + +// for sec in object.sections() { +// if let SectionFlags::Elf{ sh_flags: flags } = sec.flags() { +// if flags & elf::SHT_PROGBITS { +// if file_base.is_none() { +// file_base = Some(sec.address()); +// } else if sec.address() < file_base.unwrap() { +// // Make space at the beginning of data +// data. +// file_base = Some(sec.address()); + +// } + +// } +// } +// } + +// Ok(MemorySection { +// data: section +// .data_range(section.address(), size)? +// .ok_or("section is empty or smaller than given size")? +// .to_vec(), +// address: section.address() as u32, +// }) +// } +// }