|
|
@ -13,22 +13,52 @@ pub enum SegmentType {
|
|
|
|
Bss,
|
|
|
|
Bss,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub struct SegmentSection {
|
|
|
|
|
|
|
|
si: SectionInfo,
|
|
|
|
|
|
|
|
output_offset: usize,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl SegmentSection {
|
|
|
|
|
|
|
|
// TODO: refactor this into newtype swap with compile-time checking
|
|
|
|
|
|
|
|
pub fn section_info(&mut self, output_offset: usize) -> Result<&SectionInfo, Error> {
|
|
|
|
|
|
|
|
if self.output_offset > 0 {
|
|
|
|
|
|
|
|
Err(Error::InvalidSectionIndex) // duplicate access??
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
self.output_offset = output_offset;
|
|
|
|
|
|
|
|
Ok(&mut self.si)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl From<SectionInfo> for SegmentSection {
|
|
|
|
|
|
|
|
fn from(si: SectionInfo) -> Self {
|
|
|
|
|
|
|
|
Self {
|
|
|
|
|
|
|
|
si,
|
|
|
|
|
|
|
|
output_offset: 0,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct SegmentSections {
|
|
|
|
pub struct SegmentSections {
|
|
|
|
sections_info: Vec<SectionInfo>,
|
|
|
|
sections_info: Vec<SegmentSection>,
|
|
|
|
data_size: u64,
|
|
|
|
data_size: u64,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl SegmentSections {
|
|
|
|
impl SegmentSections {
|
|
|
|
pub fn push(&mut self, si: SectionInfo) {
|
|
|
|
pub fn push(&mut self, si: SectionInfo) {
|
|
|
|
self.data_size += si.data_size;
|
|
|
|
self.data_size += si.data_size;
|
|
|
|
self.sections_info.push(si);
|
|
|
|
self.sections_info.push(si.into());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn iter(&self) -> impl Iterator<Item = &SectionInfo> {
|
|
|
|
pub fn iter(&self) -> impl Iterator<Item = &SegmentSection> {
|
|
|
|
self.sections_info.iter()
|
|
|
|
self.sections_info.iter()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut SegmentSection> {
|
|
|
|
|
|
|
|
self.sections_info.iter_mut()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn data_size(&self) -> u64 {
|
|
|
|
pub fn data_size(&self) -> u64 {
|
|
|
|
self.data_size
|
|
|
|
self.data_size
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -97,32 +127,29 @@ impl Loadable {
|
|
|
|
text.chain(rodata).chain(data).chain(bss)
|
|
|
|
text.chain(rodata).chain(data).chain(bss)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn program_bytes<'l, R: Relocatable>(
|
|
|
|
pub fn program_sections(&mut self) -> impl Iterator<Item = &mut SegmentSection> {
|
|
|
|
&'l self,
|
|
|
|
let text_iter = self.text.iter_mut();
|
|
|
|
objects: &'l [R],
|
|
|
|
let rodata_iter = self.rodata.iter_mut();
|
|
|
|
) -> impl Iterator<Item = Result<&'l [u8], Error>> {
|
|
|
|
|
|
|
|
let text_iter = self.text.iter();
|
|
|
|
|
|
|
|
let rodata_iter = self.rodata.iter();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let data1 = text_iter.filter_map(move |si| match si.file_size {
|
|
|
|
let data1 = text_iter.filter_map(move |ss| match ss.si.file_size {
|
|
|
|
0 => None,
|
|
|
|
0 => None,
|
|
|
|
_ => Some(objects[si.object_index].bytes(si.offset, si.file_size)),
|
|
|
|
_ => Some(ss),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
let data2 = rodata_iter.filter_map(move |si| match si.data_size {
|
|
|
|
let data2 = rodata_iter.filter_map(move |ss| match ss.si.data_size {
|
|
|
|
0 => None,
|
|
|
|
0 => None,
|
|
|
|
_ => Some(objects[si.object_index].bytes(si.offset, si.file_size)),
|
|
|
|
_ => Some(ss),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
data1.chain(data2)
|
|
|
|
data1.chain(data2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn data_bytes<'l, R: Relocatable>(
|
|
|
|
pub fn data_sections(&mut self) -> impl Iterator<Item = &mut SegmentSection> {
|
|
|
|
&'l self,
|
|
|
|
let iter = self
|
|
|
|
objects: &'l [R],
|
|
|
|
.data
|
|
|
|
) -> impl Iterator<Item = Result<&'l [u8], Error>> {
|
|
|
|
.iter_mut()
|
|
|
|
let iter = self.data.iter().filter_map(move |si| match si.file_size {
|
|
|
|
.filter_map(move |ss| match ss.si.file_size {
|
|
|
|
0 => None,
|
|
|
|
0 => None,
|
|
|
|
_ => Some(objects[si.object_index].bytes(si.offset, si.file_size)),
|
|
|
|
_ => Some(ss),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
iter
|
|
|
|
iter
|
|
|
@ -134,8 +161,8 @@ impl Loadable {
|
|
|
|
let rodata_iter = self.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 ss in text_iter.chain(rodata_iter) {
|
|
|
|
result += section.data_size
|
|
|
|
result += ss.si.data_size
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
result
|
|
|
|
result
|
|
|
@ -144,8 +171,8 @@ impl Loadable {
|
|
|
|
// 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.data.iter() {
|
|
|
|
for ss in self.data.iter() {
|
|
|
|
result += section.data_size
|
|
|
|
result += ss.si.data_size
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
result
|
|
|
|
result
|
|
|
@ -154,8 +181,8 @@ impl Loadable {
|
|
|
|
// 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.bss.iter() {
|
|
|
|
for ss in self.bss.iter() {
|
|
|
|
result += section.data_size
|
|
|
|
result += ss.si.data_size
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
result
|
|
|
|
result
|
|
|
|