diff --git a/src/common/output.rs b/src/common/output.rs index 9e279b9..46532f7 100644 --- a/src/common/output.rs +++ b/src/common/output.rs @@ -4,8 +4,6 @@ use super::Section; use crate::error::Error; pub trait Output<'data> { - fn allocate(&mut self, size: u64) -> Result; - fn append_section(&mut self, section: Section<'data>) -> Result<(), Error>; // fn prepare_symbol(&mut self, symbol: impl Lazy) -> Result<&mut Self, Error>; @@ -16,11 +14,6 @@ pub trait Output<'data> { pub struct DummyOutput; impl<'data> Output<'data> for DummyOutput { - fn allocate(&mut self, size: u64) -> Result { - eprintln!("Allocating: {}", size); - Ok(size) - } - fn append_section(&mut self, section: Section<'data>) -> Result<(), Error> { eprintln!("Appending section: {}", section); Ok(()) diff --git a/src/common/section.rs b/src/common/section.rs index 23e2a37..5cab17d 100644 --- a/src/common/section.rs +++ b/src/common/section.rs @@ -4,14 +4,26 @@ use std::{ fmt::{Display, Formatter}, }; -pub type SectionDataIterator<'data> = Box + 'data>; +// pub trait CBI<'data, T>: Iterator { +// fn clone_to_box(&self) -> Box + 'data>; +// } +// impl<'data, T, X: 'data + Iterator + Clone> CBI<'data, T> for X { +// fn clone_to_box(&self) -> Box + 'data> { +// Box::new(self.clone()) +// } +// } + +// pub type SectionDataIterator<'data> = Box + 'data>; + +#[derive(Clone)] pub struct SectionInfo<'data> { pub size: u64, - pub data: Option>, + pub data: Option<&'data [u8]>, pub offset: u64, } +#[derive(Clone)] pub enum Section<'data> { Text(SectionInfo<'data>), Data(SectionInfo<'data>, bool), // readonly bool diff --git a/src/error.rs b/src/error.rs index 73a185f..bb96a56 100644 --- a/src/error.rs +++ b/src/error.rs @@ -10,6 +10,7 @@ pub enum Error { InvalidSectionName, InvalidSectionData, ParseError(Box), + TryFromIntError, // LinkingError(Trace), } @@ -33,6 +34,12 @@ impl From for Error { } } +impl From for Error { + fn from(err: std::num::TryFromIntError) -> Self { + Self::TryFromIntError + } +} + impl Display for Error { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { write!(f, "TODO!") diff --git a/src/formats/elf.rs b/src/formats/elf.rs index b262d5e..ddd8ade 100644 --- a/src/formats/elf.rs +++ b/src/formats/elf.rs @@ -18,7 +18,7 @@ impl Linker<'_> { } impl<'data> Lazy<&'data str, ELF64> for Symbol<'data> { - fn value(&self, src: &ELF64) -> Result<&'data str, Error> { + fn value(&self, _src: &ELF64) -> Result<&'data str, Error> { Err(Error::InvalidSectionData) // TODO } diff --git a/src/formats/elf/object.rs b/src/formats/elf/object.rs index 62b1ba4..1389f58 100644 --- a/src/formats/elf/object.rs +++ b/src/formats/elf/object.rs @@ -52,12 +52,12 @@ impl Relocatable for ElfObject { Type::ProgBits => { if s.header.sh_size > 0 { if let Some(di) = match &s.contents { - Contents64::Raw(v) => Some(v.iter()), + Contents64::Raw(v) => Some(v), _ => None, } { let si = SectionInfo { size: s.header.sh_size, - data: Some(Box::new(di.copied())), + data: Some(di), offset: s.header.sh_offset, }; let s_name: &str = &s.name; diff --git a/src/formats/elf/output.rs b/src/formats/elf/output.rs index 0eeef36..beb95c0 100644 --- a/src/formats/elf/output.rs +++ b/src/formats/elf/output.rs @@ -1,6 +1,18 @@ -use std::path::PathBuf; +use std::{ + convert::TryInto, + fs::File, + io::BufWriter, + io::{Seek, SeekFrom, Write}, + mem::size_of, + path::{Path, PathBuf}, +}; -use elf_utilities::{file::{ELF64Dumper, ELF64}, section::{Contents64, Section64, Shdr64, build_string_table}}; +use elf_utilities::{ + file::ELF64, + header::Ehdr64, + section::{build_string_table, Contents64, Section64, Shdr64, Type as ShType}, + segment::{Phdr64, Segment64, Type as SeType, PF_R, PF_W, PF_X}, +}; use crate::{ common::{Output, Section}, @@ -13,7 +25,7 @@ use super::ElfObject; pub struct ElfOutput<'data> { destination: PathBuf, file: ELF64, - segment_data: SegmentData<'data>, + input_data: InputData<'data>, } impl<'data> ElfOutput<'data> { @@ -32,7 +44,7 @@ impl<'data> ElfOutput<'data> { Self { destination, file: elf, - segment_data: SegmentData::new(), + input_data: InputData::new(), } } @@ -42,110 +54,183 @@ impl<'data> ElfOutput<'data> { Self { destination, file: elf_bin_from_object(other), - segment_data: SegmentData::new(), + input_data: InputData::new(), } } - fn populate_sections(&mut self) -> Result { + fn populate_sections(&mut self) -> Result<(), Error> { let mut names = Vec::new(); + let mut name_idx = 0usize; - for (name, sections) in self.segment_data.iter_mut() { - let mut data = Vec::new(); + for (name, sections) in self.input_data.sections_mut() { + let mut data_size = 0; - for t in sections.iter_mut() { - if let Some(iter) = &mut t.data { - data.extend(iter.as_mut()); + for t in sections.iter() { + if let Some(bytes) = t.data { + data_size += bytes.len(); } } + if data_size == 0{ + continue; + } + names.push(name); let section = Section64 { name: name.into(), - header: make_section_header(&names, data.len()), - contents: Contents64::Raw(data), + header: make_section_header(name_idx, ShType::ProgBits, data_size), + contents: Contents64::Raw(Vec::new()), // placeholder only }; + name_idx += name.len() + 1; self.file.add_section(section); - } let name = ".shstrtab"; names.push(name); - let string_table = build_string_table(&names, true); + let string_table = build_string_table(names, true); let section = Section64 { name: name.into(), - header: make_section_header(&names, string_table.len()), + header: make_section_header(name_idx, ShType::StrTab, string_table.len()), contents: Contents64::Raw(string_table), }; self.file.add_section(section); - Ok(0) + Ok(()) } - fn populate_segment(&mut self, offset: &mut u64, size: u64) { - use elf_utilities::segment::{Phdr64, Segment64, Type}; - use std::mem::size_of; + fn populate_segment(&mut self, offset: &mut u64, size: u64, which: SegmentType) { + if size == 0 { + return; + } let mut segment = Segment64 { header: Phdr64::default(), }; - segment.header.set_type(Type::Load); + segment.header.set_type(SeType::Load); segment.header.p_filesz = size; segment.header.p_memsz = size; segment.header.p_offset = *offset; - // TODO: elfix - add to elf-utilities as add_segment + let mut page_size = next_page(size); + + match which { + SegmentType::Text => segment.header.p_flags = PF_R | PF_X, + SegmentType::Data => segment.header.p_flags = PF_R | PF_W, + SegmentType::Bss => { + segment.header.p_flags = PF_R; + segment.header.p_memsz = 0; + page_size = 0; + } + } + self.file.segments.push(segment); self.file.ehdr.e_phnum += 1; self.file.ehdr.e_phentsize = size_of::() as u16; - *offset += size; + *offset += page_size; } fn populate_segments(&mut self) -> Result { let mut offset = 0u64; // program header/segments // contains .text + .rodata as one segment - self.populate_segment(&mut offset, self.segment_data.program_size()); + self.populate_segment( + &mut offset, + self.input_data.program_size(), + SegmentType::Text, + ); // contains .data as one segment - self.populate_segment(&mut offset, self.segment_data.data_size()); + self.populate_segment( + &mut offset, + self.input_data.data_size(), + SegmentType::Data, + ); // contains .bss as one segment - self.populate_segment(&mut offset, self.segment_data.bss_size()); + self.populate_segment(&mut offset, self.input_data.bss_size(), SegmentType::Bss); - Ok(0) + Ok(offset) } } impl<'data> Output<'data> for ElfOutput<'data> { - fn allocate(&mut self, size: u64) -> Result { - Ok(size) - } - fn append_section(&mut self, section: Section<'data>) -> Result<(), Error> { - self.segment_data.append_section(section) + self.input_data.append_section(section) } fn finalize(mut self) -> Result { - use std::io::Error as IOError; - use std::io::ErrorKind; + const EHS: u64 = size_of::() as u64; + const PHS: u16 = size_of::() as u16; + const SHS: u16 = size_of::() as u16; self.populate_sections()?; - self.populate_segments()?; - self.file.finalize(); + let page_size = self.populate_segments()?; + + self.file.ehdr.e_shnum = self.file.sections.len() as u16; + self.file.ehdr.e_entry = 4096; // TODO + self.file.ehdr.e_phentsize = PHS; + self.file.ehdr.e_shentsize = SHS; + self.file.ehdr.e_phoff = EHS; + self.file.ehdr.e_shoff = self.file.ehdr.e_phoff + (usize::from(self.file.ehdr.e_phentsize) * self.file.segments.len()) as u64; + + let str_path = expand_path(&self.destination)?; - let str_path = self.destination.to_str().ok_or_else(|| { - let ioe = IOError::new(ErrorKind::Other, "Path expansion fail"); - let boxed = Box::new(ioe); - Error::IOError(boxed) - })?; + let mut writer = file_writer(str_path, 0o755)?; + let mut offset = 0; + + // write ELF header + offset += writer.write(&self.file.ehdr.to_le_bytes())?; + + // write program header table + for seg in self.file.segments.iter_mut() { + seg.header.p_offset = offset as u64; + offset += writer.write(&seg.header.to_le_bytes())?; + } - ELF64Dumper::new(self.file) - .generate_elf_file(str_path, 0o755) - .map_err(|e| Error::IOError(e))?; + eprintln!("SH start: {}", offset); + // write section header table + for sec in self.file.sections.iter_mut() { + if sec.header.get_type() == ShType::StrTab { + sec.header.sh_offset = offset as u64 + u64::from(SHS); + } + offset += writer.write(&sec.header.to_le_bytes())?; + if sec.header.get_type() == ShType::StrTab { + offset += writer.write(&sec.to_le_bytes())?; + } + } + + // program data (segments) + offset += pad_to_next_page(&mut writer, offset)?; + eprintln!("Prog start: {}", offset); + // write section/segment data + for bytes in self.input_data.program_bytes() { + offset += writer.write(bytes)?; + } + offset += pad_to_next_page(&mut writer, offset)?; + + eprintln!("Data start: {}", offset); + for bytes in self.input_data.data_bytes() { + offset += writer.write(bytes)?; + } + + writer.flush()?; Ok(self.destination) } } +fn file_writer(output_filename: &str, permission: u32) -> Result, Error> { + use std::os::unix::fs::OpenOptionsExt; + + let file = std::fs::OpenOptions::new() + .create(true) + .truncate(true) + .write(true) + .mode(permission) + .open(output_filename)?; + + Ok(BufWriter::new(file)) +} + // init new ELF64 from an object file meant as executable output fn elf_bin_from_object(other: &ELF64) -> ELF64 { use elf_utilities::header::Type; @@ -160,29 +245,49 @@ fn elf_bin_from_object(other: &ELF64) -> ELF64 { elf } -fn make_section_header(names: &Vec<&str>, size: usize) -> Shdr64 { - use elf_utilities::section::Type; - +fn make_section_header(name_idx: usize, stype: ShType, size: usize) -> Shdr64 { let mut h = Shdr64::default(); - if let Some(name) = names.last() { - if *name == ".shstrtab" { - h.set_type(Type::StrTab); - } else { - h.set_type(Type::ProgBits); - } + h.set_type(stype); + + // section index used for name and section indexing (currently same) + let idx = 1 + (name_idx as u32); // add 0 at start + // name index + h.sh_name = idx; + // link index + h.sh_link = 0; + h.sh_size = size as u64; + // filled at finalize() + // h.sh_offset = offset; + + h +} - // section index used for name and section indexing (currently same) - let idx = (names.len() - 1) as u32; - // name index - h.sh_name = idx; - // link index - h.sh_link = idx; - h.sh_size = size as u64; - // TODO - // h.sh_offset = offset; - - h - } else { - panic!("Unexpected nameless section"); - } +fn expand_path(path: &Path) -> Result<&str, Error> { + use std::io::Error as IOError; + use std::io::ErrorKind; + + path.to_str().ok_or_else(|| { + let ioe = IOError::new(ErrorKind::Other, "Path expansion fail"); + let boxed = Box::new(ioe); + Error::IOError(boxed) + }) +} + +fn next_page(size: u64) -> u64 { + let page_size: u64 = page_size::get() as u64; + + let pages_needed = (size / page_size) + 1; + + page_size * pages_needed +} + +fn pad_to_next_page(writer: &mut BufWriter, offset: usize) -> Result { + let page_size = page_size::get(); + + let padding = page_size - (offset % page_size); + + eprintln!("Padding from: {} with: {}", offset, padding); + writer.seek(SeekFrom::Current(padding.try_into()?))?; + + Ok(padding) } diff --git a/src/formats/elf/segment.rs b/src/formats/elf/segment.rs new file mode 100644 index 0000000..56a3ff6 --- /dev/null +++ b/src/formats/elf/segment.rs @@ -0,0 +1,97 @@ +use crate::common::SectionInfo; +use crate::common::{Section}; +use crate::error::Error; + +const SI_TEXT: usize = 0; +const SI_RODATA: usize = 1; +const SI_DATA: usize = 2; +const SI_BSS: usize = 3; + +pub enum SegmentType { + Text = SI_TEXT as isize, + Data = SI_DATA as isize, + Bss = SI_BSS as isize, +} + +const SEGMENT_NAMES: [&str; 4] = [".text", ".rodata", ".data", ".bss"]; + +pub struct InputData<'data>([Vec>; 4]); + +impl<'data> InputData<'data> { + pub fn new() -> Self { + Self([Vec::new(), Vec::new(), Vec::new(), Vec::new()]) + } + + pub fn append_section(&mut self, section: Section<'data>) -> Result<(), Error> { + match section { + Section::Text(si) => self.0[SI_TEXT].push(si), + Section::Data(si, false) => self.0[SI_DATA].push(si), + Section::Data(si, true) => self.0[SI_RODATA].push(si), + Section::Bss(si) => self.0[SI_BSS].push(si), + } + + Ok(()) + } + + pub fn sections_mut( + &mut self, + ) -> impl Iterator>)> { + self.0 + .iter_mut() + .enumerate() + .map(|(i, s)| (SEGMENT_NAMES[i], s)) + } + + pub fn program_bytes<'local>(&'local self) -> impl Iterator { + let text_iter = self.0[SI_TEXT].iter(); + let rodata_iter = self.0[SI_RODATA].iter(); + + let data1 = text_iter.filter_map(|si| si.data); + let data2 = rodata_iter.filter_map(|si| si.data); + + data1.chain(data2) + } + + pub fn data_bytes<'local>(&'local self) -> impl Iterator { + self.0[SI_DATA].iter().filter_map(|si| si.data) + } + + // .text + .rodata + pub fn program_size(&self) -> u64 { + let text_iter = self.0[SI_TEXT].iter(); + let rodata_iter = self.0[SI_RODATA].iter(); + + let mut result = 0u64; + for section in text_iter.chain(rodata_iter) { + result += section.size + } + + result + } + + // data + pub fn data_size(&self) -> u64 { + let mut result = 0u64; + for section in self.0[SI_DATA].iter() { + result += section.size + } + + result + } + + // bss + pub fn bss_size(&self) -> u64 { + let mut result = 0u64; + for section in self.0[SI_BSS].iter() { + result += section.size + } + + result + } +} + +impl Default for InputData<'_> { + fn default() -> Self { + Self::new() + } +} diff --git a/src/linker.rs b/src/linker.rs index 39e8453..3a9cd94 100644 --- a/src/linker.rs +++ b/src/linker.rs @@ -18,28 +18,14 @@ impl<'data> Linker<'data> { self.relocatables.push(relocatable); } - pub fn link<'l: 'data>(&'l self, output: &mut dyn Output<'data>) -> Result { - let allocated = output.allocate(self.total_size()?)?; - + pub fn link(&'data self, output: &mut dyn Output<'data>) -> Result { for r in self.relocatables.iter() { for s in r.sections() { output.append_section(s?)?; } } - Ok(allocated) - } - - fn total_size(&self) -> Result { - let mut result = 0u64; - - for o in self.relocatables.iter() { - for s in o.sections() { - result += s?.size()?; - } - } - - Ok(result) + Ok(0) } }