refactoring
parent
8d7f01c86e
commit
21096384d8
@ -0,0 +1,24 @@
|
||||
use crate::error::Error;
|
||||
use super::Section;
|
||||
|
||||
pub trait Output {
|
||||
fn allocate(&mut self, size: u64) -> Result<u64, Error>;
|
||||
|
||||
fn append_section(&mut self, section: &Section) -> Result<(), Error>;
|
||||
|
||||
// fn prepare_symbol<V, S>(&mut self, symbol: impl Lazy<V, S>) -> Result<&mut Self, Error>;
|
||||
}
|
||||
|
||||
pub struct DummyOutput;
|
||||
|
||||
impl Output for DummyOutput {
|
||||
fn allocate(&mut self, size: u64) -> Result<u64, Error> {
|
||||
eprintln!("Allocating: {}", size);
|
||||
Ok(size)
|
||||
}
|
||||
|
||||
fn append_section(&mut self, section: &Section) -> Result<(), Error> {
|
||||
eprintln!("Appending section: {}", section);
|
||||
Ok(())
|
||||
}
|
||||
}
|
@ -0,0 +1,69 @@
|
||||
use std::{
|
||||
fmt::Display,
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
|
||||
use crate::common::{Relocatable, Storage};
|
||||
use crate::{common::BSI, error::Error};
|
||||
use xmas_elf::header::Type as ElfType;
|
||||
use xmas_elf::ElfFile;
|
||||
|
||||
use super::SectionIter;
|
||||
|
||||
pub struct ElfObject<'data> {
|
||||
origin: PathBuf,
|
||||
elf: ElfFile<'data>,
|
||||
}
|
||||
|
||||
impl<'data> ElfObject<'data> {
|
||||
pub fn new(storage: &'data mut Storage) -> Result<Self, Error> {
|
||||
let origin = storage.origin()?;
|
||||
let elf = ElfFile::new(storage.bytes()?).map_err(|_| Error::InvalidObjectType(0))?;
|
||||
|
||||
is_relocatable(&elf)?;
|
||||
let result = ElfObject { origin, elf };
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
}
|
||||
|
||||
impl Relocatable for ElfObject<'_> {
|
||||
fn origin(&self) -> &Path {
|
||||
&self.origin
|
||||
}
|
||||
|
||||
fn sections(&self) -> BSI<'_> {
|
||||
Box::new(SectionIter {
|
||||
elf: &self.elf,
|
||||
iter: self.elf.section_iter(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for ElfObject<'_> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(
|
||||
f,
|
||||
"==={:?}===\n>Symbols:\n{}\n>Sections:\n",
|
||||
self.origin().file_name().unwrap(),
|
||||
"TODO"
|
||||
)?;
|
||||
|
||||
for section in self.sections() {
|
||||
let u = section.unwrap();
|
||||
write!(f, "{}", u)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn is_relocatable(elf: &ElfFile) -> Result<(), Error> {
|
||||
let raw_type = elf.header.pt2.type_();
|
||||
let elf_type: ElfType = raw_type.as_type();
|
||||
if elf_type != ElfType::Relocatable {
|
||||
return Err(Error::InvalidObjectType(raw_type.0.into()));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
Loading…
Reference in New Issue