Move Section into section.rs

This commit is contained in:
Christopher Cole 2022-10-10 19:28:09 -07:00
parent 9530771c45
commit 2693cbf659
No known key found for this signature in database
GPG Key ID: 0AC856975983E9DB
3 changed files with 19 additions and 19 deletions

View File

@ -18,7 +18,7 @@ mod string_table;
pub struct File {
pub ehdr: file::FileHeader,
pub phdrs: Vec<segment::ProgramHeader>,
pub sections: Vec<Section>,
pub sections: Vec<section::Section>,
}
impl std::fmt::Debug for File {
@ -92,14 +92,14 @@ impl File {
phdrs.push(phdr);
}
let mut sections = Vec::<Section>::default();
let mut sections = Vec::<section::Section>::default();
// Parse the section headers
reader.seek(io::SeekFrom::Start(ehdr.e_shoff))?;
for _ in 0..ehdr.e_shnum {
let shdr = section::SectionHeader::parse(ehdr.class, &mut reader)?;
sections.push(
Section {
section::Section {
name: String::new(),
shdr: shdr,
data: Vec::new(),
@ -130,7 +130,7 @@ impl File {
})
}
pub fn get_symbols(&self, section: &Section) -> Result<Vec<symbol::Symbol>, ParseError> {
pub fn get_symbols(&self, section: &section::Section) -> Result<Vec<symbol::Symbol>, ParseError> {
let mut symbols = Vec::new();
if section.shdr.sh_type == section::SectionType(gabi::SHT_SYMTAB) || section.shdr.sh_type == section::SectionType(gabi::SHT_DYNSYM) {
let link = &self.sections[section.shdr.sh_link as usize].data;
@ -178,26 +178,13 @@ impl File {
Ok(())
}
pub fn get_section<T: AsRef<str>>(&self, name: T) -> Option<&Section> {
pub fn get_section<T: AsRef<str>>(&self, name: T) -> Option<&section::Section> {
self.sections
.iter()
.find(|section| section.name == name.as_ref() )
}
}
#[derive(Debug)]
pub struct Section {
pub name: String,
pub shdr: section::SectionHeader,
pub data: Vec<u8>,
}
impl std::fmt::Display for Section {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}", self.shdr)
}
}
#[cfg(test)]
mod tests {
use std::path::PathBuf;

View File

@ -108,7 +108,7 @@ pub fn read_u64<T: std::io::Read>(endian: Endian, io: &mut T) -> Result<u64, Par
}
}
pub trait Parse<R>: Copy {
pub trait Parse<R>: Sized {
fn parse(class: Class, reader: &mut R) -> Result<Self, crate::ParseError>
where
R: ReadExt;

View File

@ -2,6 +2,19 @@ use crate::file::Class;
use crate::gabi;
use crate::parse::{Parse, ReadExt};
#[derive(Debug)]
pub struct Section {
pub name: String,
pub shdr: SectionHeader,
pub data: Vec<u8>,
}
impl std::fmt::Display for Section {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}", self.shdr)
}
}
/// Encapsulates the contents of an ELF Section Header
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct SectionHeader {