From 0b5f013323d2b6524a438f43af29f03491af3657 Mon Sep 17 00:00:00 2001 From: Ales Katona Date: Fri, 15 Jan 2021 16:35:54 -0800 Subject: [PATCH] refactor loadable to be generic --- src/common.rs | 2 ++ src/{formats/elf => common}/loadable.rs | 10 ++++------ src/formats/elf.rs | 1 - src/formats/elf/output.rs | 6 +----- 4 files changed, 7 insertions(+), 12 deletions(-) rename src/{formats/elf => common}/loadable.rs (94%) diff --git a/src/common.rs b/src/common.rs index 899073c..7ff3fc0 100644 --- a/src/common.rs +++ b/src/common.rs @@ -1,10 +1,12 @@ mod lazy; +mod loadable; mod output; mod relocatable; mod section; mod symbol; pub use lazy::*; +pub use loadable::*; pub use output::*; pub use relocatable::*; pub use section::*; diff --git a/src/formats/elf/loadable.rs b/src/common/loadable.rs similarity index 94% rename from src/formats/elf/loadable.rs rename to src/common/loadable.rs index a0bda9c..802a358 100644 --- a/src/formats/elf/loadable.rs +++ b/src/common/loadable.rs @@ -4,8 +4,6 @@ use crate::common::SectionInfo; use crate::common::{Relocatable, Section}; use crate::error::Error; -use super::ElfObject; - pub enum SegmentType { Text, Data, @@ -43,9 +41,9 @@ impl Loadable { text.chain(rodata).chain(data).chain(bss) } - pub fn program_bytes<'l>( + pub fn program_bytes<'l, R: Relocatable>( &'l self, - objects: &'l [ElfObject], + objects: &'l [R], ) -> impl Iterator> { let text_iter = self.text.iter(); let rodata_iter = self.rodata.iter(); @@ -62,9 +60,9 @@ impl Loadable { data1.chain(data2) } - pub fn data_bytes<'l>( + pub fn data_bytes<'l, R: Relocatable>( &'l self, - objects: &'l [ElfObject], + objects: &'l [R], ) -> impl Iterator> { let iter = self.data.iter().filter_map(move |si| match si.data_index { None => None, diff --git a/src/formats/elf.rs b/src/formats/elf.rs index 2766567..2f5e1c5 100644 --- a/src/formats/elf.rs +++ b/src/formats/elf.rs @@ -6,7 +6,6 @@ use crate::linker::Linker; mod object; mod output; -mod loadable; use elf_utilities::file::ELF64; pub use object::*; diff --git a/src/formats/elf/output.rs b/src/formats/elf/output.rs index 4b84767..e5d2504 100644 --- a/src/formats/elf/output.rs +++ b/src/formats/elf/output.rs @@ -14,12 +14,8 @@ use elf_utilities::{ segment::{Phdr64, Segment64, Type as SeType, PF_R, PF_W, PF_X}, }; -use crate::{ - common::{Output, Section}, - error::Error, -}; +use crate::{common::{Loadable, Output, Section, SegmentType}, error::Error}; -use super::loadable::*; use super::ElfObject; pub struct ElfOutput {