unify DataIndex to specific type

master
Ales Katona 4 years ago
parent 0a21271372
commit 3b3f834a2f
Signed by: almindor
GPG Key ID: 2F773149BF38B48F

@ -7,7 +7,7 @@ pub trait Output<R>
where
R: Relocatable,
{
fn process_section(&mut self, section: Section<R::Index>) -> Result<(), Error>;
fn process_section(&mut self, section: Section) -> Result<(), Error>;
fn finalize(self, objects: &[R]) -> Result<PathBuf, Error>;
}

@ -5,18 +5,33 @@ use crate::error::Error;
use super::Section;
pub type BSI<'iter, I> = Box<dyn Iterator<Item = Result<Section<I>, Error>> + 'iter>;
#[derive(Clone, Copy)]
// index into section of given object stored in the linker
pub struct DataIndex {
pub object_index: usize,
pub section_index: usize,
}
impl DataIndex {
pub fn new(object_index: usize, section_index: usize) -> Self {
DataIndex { object_index, section_index }
}
pub fn for_object(object_index: usize) -> Self {
DataIndex { object_index, section_index: 0 }
}
}
pub type BSI<'iter> = Box<dyn Iterator<Item = Result<Section, Error>> + 'iter>;
/// Contains all the needed getters to construct a final
/// mushed and relocated executable from an object file
pub trait Relocatable: Display + Sized {
// + TryFrom<PathBuf, Error = Error> {
type Index; // index into the data, e.g. usize
fn new(origin: PathBuf, object_index: usize) -> Result<Self, Error>;
fn new(origin: PathBuf, index: DataIndex) -> Result<Self, Error>;
fn origin(&self) -> &Path; // not same as section's path since this one's supposed to be cannonical
fn sections(&self) -> BSI<Self::Index>;
fn sections(&self) -> BSI;
fn section_data(&self, section_index: Self::Index) -> Result<&[u8], Error>;
fn section_data(&self, section_index: DataIndex) -> Result<&[u8], Error>;
}

@ -4,25 +4,27 @@ use std::{
fmt::{Display, Formatter},
};
use super::DataIndex;
#[derive(Clone)]
pub struct SectionInfo<I> {
pub struct SectionInfo {
pub file_size: u64,
pub data_size: u64,
pub data_index: Option<I>, // some form of indexing to the source data (Relocatable's data)
pub data_index: Option<DataIndex>, // some form of indexing to the source data (Relocatable's data)
pub offset: u64,
}
#[derive(Clone)]
pub enum Section<I> {
Text(SectionInfo<I>),
Data(SectionInfo<I>, bool), // readonly bool
Bss(SectionInfo<I>),
pub enum Section {
Text(SectionInfo),
Data(SectionInfo, bool), // readonly bool
Bss(SectionInfo),
}
impl<'data, I> TryFrom<(&str, SectionInfo<I>)> for Section<I> {
impl<'data> TryFrom<(&str, SectionInfo)> for Section {
type Error = Error;
fn try_from(value: (&str, SectionInfo<I>)) -> Result<Self, Self::Error> {
fn try_from(value: (&str, SectionInfo)) -> Result<Self, Self::Error> {
if value.0.starts_with(".text") {
Ok(Section::Text(value.1))
} else if value.0.starts_with(".rodata") {
@ -37,7 +39,7 @@ impl<'data, I> TryFrom<(&str, SectionInfo<I>)> for Section<I> {
}
}
impl<I> Display for SectionInfo<I> {
impl Display for SectionInfo {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
f,
@ -50,7 +52,7 @@ impl<I> Display for SectionInfo<I> {
}
}
impl<I> Display for Section<I> {
impl Display for Section {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Section::Text(s) => writeln!(f, "TEXT {}", s),

@ -12,7 +12,7 @@ pub enum SegmentType {
Bss,
}
type SegmentSections = Vec<SectionInfo<(usize, usize)>>;
type SegmentSections = Vec<SectionInfo>;
#[derive(Default)]
pub struct Loadable {
@ -22,8 +22,8 @@ pub struct Loadable {
bss: SegmentSections,
}
impl<'data> Loadable {
pub fn process_section(&mut self, section: Section<(usize, usize)>) -> Result<(), Error> {
impl Loadable {
pub fn process_section(&mut self, section: Section) -> Result<(), Error> {
match section {
Section::Text(si) => self.text.push(si),
Section::Data(si, true) => self.rodata.push(si),
@ -52,11 +52,11 @@ impl<'data> Loadable {
let data1 = text_iter.filter_map(move |si| match si.data_index {
None => None,
Some(di) => Some(objects[di.0].section_data(di)),
Some(di) => Some(objects[di.object_index].section_data(di)),
});
let data2 = rodata_iter.filter_map(move |si| match si.data_index {
None => None,
Some(di) => Some(objects[di.0].section_data(di)),
Some(di) => Some(objects[di.object_index].section_data(di)),
});
data1.chain(data2)
@ -68,7 +68,7 @@ impl<'data> Loadable {
) -> impl Iterator<Item = Result<&'l [u8], Error>> {
let iter = self.data.iter().filter_map(move |si| match si.data_index {
None => None,
Some(di) => Some(objects[di.0].section_data(di)),
Some(di) => Some(objects[di.object_index].section_data(di)),
});
iter

@ -4,7 +4,7 @@ use std::{
path::{Path, PathBuf},
};
use crate::common::{Relocatable, Section, SectionInfo};
use crate::common::{DataIndex, Relocatable, Section, SectionInfo};
use crate::{common::BSI, error::Error};
use elf_utilities::{file::ELF64, parser::read_elf64};
@ -41,17 +41,15 @@ impl ElfObject {
}
impl Relocatable for ElfObject {
type Index = (usize, usize); // object index, section index
fn new(origin: PathBuf, object_index: usize) -> Result<Self, Error> {
ElfObject::new(origin, object_index)
fn new(origin: PathBuf, di: DataIndex) -> Result<Self, Error> {
ElfObject::new(origin, di.object_index)
}
fn origin(&self) -> &Path {
&self.origin
}
fn sections(&self) -> BSI<(usize, usize)> {
fn sections(&self) -> BSI {
use elf_utilities::section::{Contents64, Type};
let iter = self
@ -69,7 +67,7 @@ impl Relocatable for ElfObject {
let si = SectionInfo {
file_size: s.header.sh_size,
data_size: s.header.sh_size,
data_index: Some((self.object_index, i)),
data_index: Some(DataIndex::new(self.object_index, i)),
offset: s.header.sh_offset,
};
let s_name: &str = &s.name;
@ -98,9 +96,9 @@ impl Relocatable for ElfObject {
Box::new(iter)
}
fn section_data(&self, index: (usize, usize)) -> Result<&[u8], Error> {
fn section_data(&self, index: DataIndex) -> Result<&[u8], Error> {
use elf_utilities::section::Contents64;
let section = &self.elf.sections[index.1];
let section = &self.elf.sections[index.section_index];
match &section.contents {
Contents64::Raw(v) => Ok(&v),

@ -148,7 +148,7 @@ impl ElfOutput {
}
impl Output<ElfObject> for ElfOutput {
fn process_section(&mut self, section: Section<(usize, usize)>) -> Result<(), Error> {
fn process_section(&mut self, section: Section) -> Result<(), Error> {
self.loadable.process_section(section)
}

@ -4,10 +4,7 @@ use std::{
path::{Path, PathBuf},
};
use crate::{
common::{Output, Relocatable},
error::Error,
};
use crate::{common::{DataIndex, Output, Relocatable}, error::Error};
pub struct Linker<R, O>
where
@ -32,7 +29,7 @@ where
pub fn add_relocatable(&mut self, origin: PathBuf) -> Result<(), Error> {
self.relocatables
.push(R::new(origin, self.relocatables.len())?);
.push(R::new(origin, DataIndex::for_object(self.relocatables.len()))?);
Ok(())
}

Loading…
Cancel
Save