|
|
|
@ -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<u64, Error> {
|
|
|
|
|
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::<Phdr64>() as u16;
|
|
|
|
|
|
|
|
|
|
*offset += size;
|
|
|
|
|
*offset += page_size;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn populate_segments(&mut self) -> Result<u64, Error> {
|
|
|
|
|
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<u64, Error> {
|
|
|
|
|
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<PathBuf, Error> {
|
|
|
|
|
use std::io::Error as IOError;
|
|
|
|
|
use std::io::ErrorKind;
|
|
|
|
|
const EHS: u64 = size_of::<Ehdr64>() as u64;
|
|
|
|
|
const PHS: u16 = size_of::<Phdr64>() as u16;
|
|
|
|
|
const SHS: u16 = size_of::<Shdr64>() as u16;
|
|
|
|
|
|
|
|
|
|
self.populate_sections()?;
|
|
|
|
|
self.populate_segments()?;
|
|
|
|
|
self.file.finalize();
|
|
|
|
|
let page_size = self.populate_segments()?;
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
})?;
|
|
|
|
|
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 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())?;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)?;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ELF64Dumper::new(self.file)
|
|
|
|
|
.generate_elf_file(str_path, 0o755)
|
|
|
|
|
.map_err(|e| Error::IOError(e))?;
|
|
|
|
|
writer.flush()?;
|
|
|
|
|
|
|
|
|
|
Ok(self.destination)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn file_writer(output_filename: &str, permission: u32) -> Result<BufWriter<File>, 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 = (names.len() - 1) as u32;
|
|
|
|
|
let idx = 1 + (name_idx as u32); // add 0 at start
|
|
|
|
|
// name index
|
|
|
|
|
h.sh_name = idx;
|
|
|
|
|
// link index
|
|
|
|
|
h.sh_link = idx;
|
|
|
|
|
h.sh_link = 0;
|
|
|
|
|
h.sh_size = size as u64;
|
|
|
|
|
// TODO
|
|
|
|
|
// filled at finalize()
|
|
|
|
|
// 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<File>, offset: usize) -> Result<usize, Error> {
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|