Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Architecture Specific Relocations + 32bit Support #30

Merged
merged 18 commits into from
Mar 19, 2022
Merged

Conversation

landhb
Copy link
Contributor

@landhb landhb commented Mar 2, 2022

@gz Here is the full PR for #29. It ended up being larger than I originally planned so if you prefer I try to split it up into multiple just let me know.

Adds RelocationEntry struct to abstract 32bit and 64bit Rela<T>/Rel<T> entries.

pub struct RelocationEntry {
    pub rtype: RelocationType,
    pub offset: u64,
    pub index: u32,
    pub addend: Option<u64>,
}

Which is then passed to the user via the relocate() function in the exposed trait interface:

pub trait ElfLoader {
    ...
    /// Request for the client to relocate the given `entry`
    /// within the loaded ELF file.
    fn relocate(&mut self, entry: RelocationEntry) -> Result<(), ElfLoaderErr>;
    ...
}   

Architecture specific relocation types are added into ./arch/[ARCH].rs and can be handled in a single match statement by the user by adding a use for the architectures they'd like to handle:

use elfloader::arch::x86_64::RelocationTypes::*;
use elfloader::arch::x86::RelocationTypes::*;
use elfloader::RelocationType::{x86, x86_64};

// Handle relocations for multiple architectures
fn relocate(&mut self, entry: RelocationEntry) -> Result<(), ElfLoaderErr> {
    match entry.rtype {

        // Example handling x86 relocation type
        x86(R_386_RELATIVE) => {},

        // Example handling x86_64 relocation type
        x86_64(R_AMD64_RELATIVE) => {},

        // Unsupported Skip
        x => {trace!("Skipping relocation type: {:?}", x)}
    }
    Ok(())
}

Change summaries:

  • Added ElfBinary::get_arch()
  • Added ElfBinary::interpreter()
  • Added tests load_pie_elf_32(), check_nopie_32(), and check_tls_32()
  • Edited ElBinary::maybe_relocate() to handle SectionData::Rela32, SectionData::Rel32, and SectionData::Rel64 and pass the abstracted RelocationEntry object to loader.relocate()
  • Edited ElfBinary::parse_dynamic() to handle 32bit SegmentData::Dynamic32
  • Edited ElfBinary::load() to handle 32bit ProgramHeader::Ph32
  • Edited ElfBinary::iter_loadable_headers() to handle 32bit ProgramHeader::Ph32
  • Edited ElfBinary::new() to handle 32bit ProgramHeader::Ph32
  • Edited README to reflect usage

Repo structure changes:

  • Moved test code to src/test.rs
  • Moved ElfBinary code to src/binary.rs
  • Added src/arch/* for architecture specific Relocation definitions

@gz
Copy link
Owner

gz commented Mar 3, 2022

This looks awesome, thanks for the contribution! I will review this over the next few days when I have some time.

@landhb
Copy link
Contributor Author

landhb commented Mar 3, 2022

No problem!

@gz gz merged commit e2fdc74 into gz:master Mar 19, 2022
@gz
Copy link
Owner

gz commented Mar 19, 2022

release as 0.15, many thanks for the great contribution!

@landhb
Copy link
Contributor Author

landhb commented Mar 19, 2022

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants