1use elf::endian::EndianParse;
5use elf::relocation::Rel;
6use elf::relocation::RelIterator;
7use elf::ElfBytes;
8use thiserror::Error;
9
10pub struct RelIter<'data, E: EndianParse> {
11 relocs: RelIterator<'data, E>,
12}
13
14#[derive(Error, Debug)]
15pub enum RelError {
16 #[error("elf parse error")]
17 ElfParseError(elf::parse::ParseError),
18
19 #[error("section not found")]
20 SectionNotFound,
21}
22
23impl From<elf::parse::ParseError> for RelError {
24 fn from(value: elf::parse::ParseError) -> Self {
25 RelError::ElfParseError(value)
26 }
27}
28
29pub type Result<T> = core::result::Result<T, RelError>;
30
31impl<'data, E: EndianParse> RelIter<'data, E> {
32 pub fn new(elf: &'data ElfBytes<E>, relname: &str) -> Result<Self> {
33 let sec = match elf.section_header_by_name(relname)? {
34 Some(s) => s,
35 None => return Err(RelError::SectionNotFound),
36 };
37 let relocs = elf.section_data_as_rels(&sec)?;
38 Ok(Self { relocs })
39 }
40}
41
42impl<'data, E: EndianParse> Iterator for RelIter<'data, E> {
43 type Item = Rel;
44
45 fn next(&mut self) -> Option<Self::Item> {
46 self.relocs.next()
47 }
48}