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>;
}
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 output;
mod segment;
mod loadable;
use elf_utilities::file::ELF64;
pub use object::*;

@ -15,15 +15,15 @@ pub enum SegmentType {
type SegmentSections = Vec<SectionInfo<(usize, usize)>>;
#[derive(Default)]
pub struct SegmentView {
pub struct Loadable {
text: SegmentSections,
rodata: SegmentSections,
data: SegmentSections,
bss: SegmentSections,
}
impl<'data> SegmentView {
pub fn append_section(&mut self, section: Section<(usize, usize)>) -> Result<(), Error> {
impl<'data> Loadable {
pub fn process_section(&mut self, section: Section<(usize, usize)>) -> Result<(), Error> {
match section {
Section::Text(si) => self.text.push(si),
Section::Data(si, true) => self.rodata.push(si),

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

Loading…
Cancel
Save