refactor into loadable

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

@ -11,19 +11,3 @@ where
fn finalize(self, objects: &[R]) -> Result<PathBuf, Error>; fn finalize(self, objects: &[R]) -> Result<PathBuf, Error>;
} }
pub struct DummyOutput;
impl<R> Output<R> for DummyOutput
where
R: Relocatable,
{
fn process_section(&mut self, section: Section<R::Index>) -> Result<(), Error> {
eprintln!("Appending section: {}", section);
Ok(())
}
fn finalize(self, _objects: &[R]) -> Result<PathBuf, Error> {
todo!();
}
}

@ -6,7 +6,7 @@ use crate::linker::Linker;
mod object; mod object;
mod output; mod output;
mod segment; mod loadable;
use elf_utilities::file::ELF64; use elf_utilities::file::ELF64;
pub use object::*; pub use object::*;

@ -15,15 +15,15 @@ pub enum SegmentType {
type SegmentSections = Vec<SectionInfo<(usize, usize)>>; type SegmentSections = Vec<SectionInfo<(usize, usize)>>;
#[derive(Default)] #[derive(Default)]
pub struct SegmentView { pub struct Loadable {
text: SegmentSections, text: SegmentSections,
rodata: SegmentSections, rodata: SegmentSections,
data: SegmentSections, data: SegmentSections,
bss: SegmentSections, bss: SegmentSections,
} }
impl<'data> SegmentView { impl<'data> Loadable {
pub fn append_section(&mut self, section: Section<(usize, usize)>) -> Result<(), Error> { pub fn process_section(&mut self, section: Section<(usize, usize)>) -> 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),

@ -19,13 +19,13 @@ use crate::{
error::Error, error::Error,
}; };
use super::segment::*; use super::loadable::*;
use super::ElfObject; use super::ElfObject;
pub struct ElfOutput { pub struct ElfOutput {
destination: PathBuf, destination: PathBuf,
file: ELF64, file: ELF64,
segment_view: SegmentView, loadable: Loadable,
writer: BufWriter<File>, writer: BufWriter<File>,
} }
@ -48,7 +48,7 @@ impl ElfOutput {
let result = Self { let result = Self {
destination, destination,
file: elf, file: elf,
segment_view: SegmentView::default(), loadable: Loadable::default(),
writer, writer,
}; };
@ -59,7 +59,7 @@ impl ElfOutput {
let mut names = Vec::new(); let mut names = Vec::new();
let mut name_idx = 0usize; let mut name_idx = 0usize;
for (name, sections) in self.segment_view.sections_mut() { for (name, sections) in self.loadable.sections_mut() {
let mut data_size = 0; let mut data_size = 0;
for t in sections.iter() { for t in sections.iter() {
@ -131,17 +131,17 @@ impl ElfOutput {
// contains .text + .rodata as one segment // contains .text + .rodata as one segment
self.populate_segment( self.populate_segment(
&mut offset, &mut offset,
self.segment_view.program_size(), self.loadable.program_size(),
SegmentType::Text, SegmentType::Text,
); );
// contains .data as one segment // contains .data as one segment
self.populate_segment( self.populate_segment(
&mut offset, &mut offset,
self.segment_view.data_size(), self.loadable.data_size(),
SegmentType::Data, SegmentType::Data,
); );
// contains .bss as one segment // contains .bss as one segment
self.populate_segment(&mut offset, self.segment_view.bss_size(), SegmentType::Bss); self.populate_segment(&mut offset, self.loadable.bss_size(), SegmentType::Bss);
Ok(offset) Ok(offset)
} }
@ -149,7 +149,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<(usize, usize)>) -> Result<(), Error> {
self.segment_view.append_section(section) self.loadable.process_section(section)
} }
fn finalize(mut self, objects: &[ElfObject]) -> Result<PathBuf, Error> { fn finalize(mut self, objects: &[ElfObject]) -> Result<PathBuf, Error> {
@ -195,13 +195,13 @@ impl Output<ElfObject> for ElfOutput {
offset += pad_to_next_page(&mut self.writer, offset)?; offset += pad_to_next_page(&mut self.writer, offset)?;
eprintln!("Prog start: {}", offset); eprintln!("Prog start: {}", offset);
// write section/segment data // write section/segment data
for bytes in self.segment_view.program_bytes(objects) { for bytes in self.loadable.program_bytes(objects) {
offset += self.writer.write(bytes?)?; offset += self.writer.write(bytes?)?;
} }
offset += pad_to_next_page(&mut self.writer, offset)?; offset += pad_to_next_page(&mut self.writer, offset)?;
eprintln!("Data start: {}", offset); eprintln!("Data start: {}", offset);
for bytes in self.segment_view.data_bytes(objects) { for bytes in self.loadable.data_bytes(objects) {
offset += self.writer.write(bytes?)?; offset += self.writer.write(bytes?)?;
} }

Loading…
Cancel
Save