|
|
@ -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?)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|