cleanup SegmentView

master
Ales Katona 4 years ago
parent 74b05cef35
commit 970bbf65bb
Signed by: almindor
GPG Key ID: 2F773149BF38B48F

@ -25,7 +25,7 @@ use super::ElfObject;
pub struct ElfOutput { pub struct ElfOutput {
destination: PathBuf, destination: PathBuf,
file: ELF64, file: ELF64,
input_data: OutputData, input_data: SegmentView,
writer: BufWriter<File>, writer: BufWriter<File>,
} }
@ -48,7 +48,7 @@ impl ElfOutput {
let result = Self { let result = Self {
destination, destination,
file: elf, file: elf,
input_data: OutputData::new(), input_data: SegmentView::default(),
writer, writer,
}; };
@ -64,7 +64,7 @@ impl ElfOutput {
let result = Self { let result = Self {
destination, destination,
file: elf_bin_from_object(other), file: elf_bin_from_object(other),
input_data: OutputData::new(), input_data: SegmentView::default(),
writer, writer,
}; };

@ -1,35 +1,34 @@
use std::iter::once;
use crate::common::SectionInfo; use crate::common::SectionInfo;
use crate::common::{Relocatable, Section}; use crate::common::{Relocatable, Section};
use crate::error::Error; use crate::error::Error;
use super::ElfObject; use super::ElfObject;
const SI_TEXT: usize = 0;
const SI_RODATA: usize = 1;
const SI_DATA: usize = 2;
const SI_BSS: usize = 3;
pub enum SegmentType { pub enum SegmentType {
Text = SI_TEXT as isize, Text,
Data = SI_DATA as isize, Data,
Bss = SI_BSS as isize, Bss,
} }
const SEGMENT_NAMES: [&str; 4] = [".text", ".rodata", ".data", ".bss"]; type SegmentSections = Vec<SectionInfo<(usize, usize)>>;
pub struct OutputData([Vec<SectionInfo<(usize, usize)>>; 4]); #[derive(Default)]
pub struct SegmentView {
impl<'data> OutputData { text: SegmentSections,
pub fn new() -> Self { rodata: SegmentSections,
Self([Vec::new(), Vec::new(), Vec::new(), Vec::new()]) data: SegmentSections,
bss: SegmentSections,
} }
impl<'data> SegmentView {
pub fn append_section(&mut self, section: Section<(usize, usize)>) -> Result<(), Error> { pub fn append_section(&mut self, section: Section<(usize, usize)>) -> Result<(), Error> {
match section { match section {
Section::Text(si) => self.0[SI_TEXT].push(si), Section::Text(si) => self.text.push(si),
Section::Data(si, false) => self.0[SI_DATA].push(si), Section::Data(si, true) => self.rodata.push(si),
Section::Data(si, true) => self.0[SI_RODATA].push(si), Section::Data(si, false) => self.data.push(si),
Section::Bss(si) => self.0[SI_BSS].push(si), Section::Bss(si) => self.bss.push(si),
} }
Ok(()) Ok(())
@ -37,19 +36,21 @@ impl<'data> OutputData {
pub fn sections_mut( pub fn sections_mut(
&mut self, &mut self,
) -> impl Iterator<Item = (&'static str, &mut Vec<SectionInfo<(usize, usize)>>)> { ) -> impl Iterator<Item = (&'static str, &mut SegmentSections)> {
self.0 let text = once(&mut self.text).map(|s| (".text", s));
.iter_mut() let rodata = once(&mut self.rodata).map(|s| (".rodata", s));
.enumerate() let data = once(&mut self.data).map(|s| (".data", s));
.map(|(i, s)| (SEGMENT_NAMES[i], s)) let bss = once(&mut self.bss).map(|s| (".bss", s));
text.chain(rodata).chain(data).chain(bss)
} }
pub fn program_bytes<'l>( pub fn program_bytes<'l>(
&'l self, &'l self,
objects: &'l Vec<ElfObject>, objects: &'l Vec<ElfObject>,
) -> impl Iterator<Item = Result<&'l [u8], Error>> { ) -> impl Iterator<Item = Result<&'l [u8], Error>> {
let text_iter = self.0[SI_TEXT].iter(); let text_iter = self.text.iter();
let rodata_iter = self.0[SI_RODATA].iter(); let rodata_iter = self.rodata.iter();
let data1 = text_iter.filter_map(move |si| match si.data_index { let data1 = text_iter.filter_map(move |si| match si.data_index {
None => None, None => None,
@ -69,7 +70,7 @@ impl<'data> OutputData {
&'l self, &'l self,
objects: &'l Vec<ElfObject>, objects: &'l Vec<ElfObject>,
) -> impl Iterator<Item = Result<&'l [u8], Error>> { ) -> impl Iterator<Item = Result<&'l [u8], Error>> {
let iter = self.0[SI_DATA] let iter = self.data
.iter() .iter()
.filter_map(move |si| match si.data_index { .filter_map(move |si| match si.data_index {
None => None, None => None,
@ -81,8 +82,8 @@ impl<'data> OutputData {
// .text + .rodata // .text + .rodata
pub fn program_size(&self) -> u64 { pub fn program_size(&self) -> u64 {
let text_iter = self.0[SI_TEXT].iter(); let text_iter = self.text.iter();
let rodata_iter = self.0[SI_RODATA].iter(); let rodata_iter = self.rodata.iter();
let mut result = 0u64; let mut result = 0u64;
for section in text_iter.chain(rodata_iter) { for section in text_iter.chain(rodata_iter) {
@ -95,7 +96,7 @@ impl<'data> OutputData {
// data // data
pub fn data_size(&self) -> u64 { pub fn data_size(&self) -> u64 {
let mut result = 0u64; let mut result = 0u64;
for section in self.0[SI_DATA].iter() { for section in self.data.iter() {
result += section.data_size result += section.data_size
} }
@ -105,16 +106,10 @@ impl<'data> OutputData {
// bss // bss
pub fn bss_size(&self) -> u64 { pub fn bss_size(&self) -> u64 {
let mut result = 0u64; let mut result = 0u64;
for section in self.0[SI_BSS].iter() { for section in self.bss.iter() {
result += section.data_size result += section.data_size
} }
result result
} }
} }
impl Default for OutputData {
fn default() -> Self {
Self::new()
}
}

Loading…
Cancel
Save