You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
1.9 KiB
Rust

use crate::error::Error;
use std::{
convert::TryFrom,
fmt::{Display, Formatter},
};
pub type SectionDataIterator<'data> = Box<dyn Iterator<Item = u8> + 'data>;
pub struct SectionInfo<'data> {
pub size: u64,
pub data: Option<SectionDataIterator<'data>>,
pub offset: u64,
}
pub enum Section<'data> {
Text(SectionInfo<'data>),
Data(SectionInfo<'data>, bool), // readonly bool
Bss(SectionInfo<'data>),
}
impl Section<'_> {
pub fn size(&self) -> Result<u64, Error> {
match self {
Section::Text(s) => Ok(s.size),
Section::Data(s, _) => Ok(s.size),
Section::Bss(s) => Ok(s.size),
}
}
}
impl<'data> TryFrom<(&str, SectionInfo<'data>)> for Section<'data> {
type Error = Error;
fn try_from(value: (&str, SectionInfo<'data>)) -> Result<Self, Self::Error> {
if value.0.starts_with(".text") {
Ok(Section::Text(value.1))
} else if value.0.starts_with(".rodata") {
Ok(Section::Data(value.1, true))
} else if value.0.starts_with(".data") {
Ok(Section::Data(value.1, false))
} else if value.0.starts_with(".bss") {
Ok(Section::Bss(value.1))
} else {
Err(Error::InvalidSectionName)
}
}
}
impl Display for SectionInfo<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
f,
"size: {}, data: {}, offset: {}",
self.size,
self.data.is_some(),
self.offset,
)
}
}
impl Display for Section<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Section::Text(s) => writeln!(f, "TEXT {}", s),
Section::Data(s, false) => writeln!(f, "DATA {}", s),
Section::Data(s, true) => writeln!(f, "RO-DATA {}", s),
Section::Bss(s) => writeln!(f, "BSS {}", s),
}
}
}