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 where
R: Relocatable, 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>; fn finalize(self, objects: &[R]) -> Result<PathBuf, Error>;
} }

@ -5,18 +5,33 @@ use crate::error::Error;
use super::Section; 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 /// Contains all the needed getters to construct a final
/// mushed and relocated executable from an object file /// mushed and relocated executable from an object file
pub trait Relocatable: Display + Sized { pub trait Relocatable: Display + Sized {
// + TryFrom<PathBuf, Error = Error> { // + TryFrom<PathBuf, Error = Error> {
type Index; // index into the data, e.g. usize fn new(origin: PathBuf, index: DataIndex) -> Result<Self, Error>;
fn new(origin: PathBuf, object_index: usize) -> Result<Self, Error>;
fn origin(&self) -> &Path; // not same as section's path since this one's supposed to be cannonical 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}, fmt::{Display, Formatter},
}; };
use super::DataIndex;
#[derive(Clone)] #[derive(Clone)]
pub struct SectionInfo<I> { pub struct SectionInfo {
pub file_size: u64, pub file_size: u64,
pub data_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, pub offset: u64,
} }
#[derive(Clone)] #[derive(Clone)]
pub enum Section<I> { pub enum Section {
Text(SectionInfo<I>), Text(SectionInfo),
Data(SectionInfo<I>, bool), // readonly bool Data(SectionInfo, bool), // readonly bool
Bss(SectionInfo<I>), Bss(SectionInfo),
} }
impl<'data, I> TryFrom<(&str, SectionInfo<I>)> for Section<I> { impl<'data> TryFrom<(&str, SectionInfo)> for Section {
type Error = Error; 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") { if value.0.starts_with(".text") {
Ok(Section::Text(value.1)) Ok(Section::Text(value.1))
} else if value.0.starts_with(".rodata") { } 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 { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!( write!(
f, 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 { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self { match self {
Section::Text(s) => writeln!(f, "TEXT {}", s), Section::Text(s) => writeln!(f, "TEXT {}", s),

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

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

@ -148,7 +148,7 @@ impl ElfOutput {
} }
impl Output<ElfObject> for 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) self.loadable.process_section(section)
} }

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

Loading…
Cancel
Save