• Stars
    star
    108
  • Rank 321,259 (Top 7 %)
  • Language
    Rust
  • License
    Apache License 2.0
  • Created over 9 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Library to load and relocate ELF files.

Build cargo-badge docs-badge

elfloader

A library to load and relocate ELF files in memory. This library depends only on libcore so it can be used in kernel level code, for example to load user-space programs.

How-to use

Clients will have to implement the ElfLoader trait:

use elfloader::*;
use log::info;

/// A simple ExampleLoader, that implements ElfLoader
/// but does nothing but logging
struct ExampleLoader {
    vbase: u64,
}

impl ElfLoader for ExampleLoader {
    fn allocate(&mut self, load_headers: LoadableHeaders) -> Result<(), ElfLoaderErr> {
        for header in load_headers {
            info!(
                "allocate base = {:#x} size = {:#x} flags = {}",
                header.virtual_addr(),
                header.mem_size(),
                header.flags()
            );
        }
        Ok(())
    }

    fn relocate(&mut self, entry: RelocationEntry) -> Result<(), ElfLoaderErr> {
        use RelocationType::x86_64;
        use crate::arch::x86_64::RelocationTypes::*;

        let addr: *mut u64 = (self.vbase + entry.offset) as *mut u64;

        match entry.rtype {
            x86_64(R_AMD64_RELATIVE) => {

                // This type requires addend to be present
                let addend = entry
                    .addend
                    .ok_or(ElfLoaderErr::UnsupportedRelocationEntry)?;

                // This is a relative relocation, add the offset (where we put our
                // binary in the vspace) to the addend and we're done.
                info!(
                    "R_RELATIVE *{:p} = {:#x}",
                    addr,
                    self.vbase + addend
                );
                Ok(())
            }
            _ => Ok((/* not implemented */)),
        }
    }

    fn load(&mut self, flags: Flags, base: VAddr, region: &[u8]) -> Result<(), ElfLoaderErr> {
        let start = self.vbase + base;
        let end = self.vbase + base + region.len() as u64;
        info!("load region into = {:#x} -- {:#x}", start, end);
        Ok(())
    }

    fn tls(
        &mut self,
        tdata_start: VAddr,
        _tdata_length: u64,
        total_size: u64,
        _align: u64
    ) -> Result<(), ElfLoaderErr> {
        let tls_end = tdata_start +  total_size;
        info!("Initial TLS region is at = {:#x} -- {:#x}", tdata_start, tls_end);
        Ok(())
    }

}

// Then, with ElfBinary, a ELF file is loaded using `load`:
fn main() {
    use std::fs;

    let binary_blob = fs::read("test/test.x86_64").expect("Can't read binary");
    let binary = ElfBinary::new(binary_blob.as_slice()).expect("Got proper ELF file");
    let mut loader = ExampleLoader { vbase: 0x1000_0000 };
    binary.load(&mut loader).expect("Can't load the binary?");
}

More Repositories

1

rust-x86

Rust library to use x86 (amd64) specific functionality and registers.
Rust
303
star
2

rust-cpuid

cpuid library in rust.
Rust
139
star
3

autoperf

Simplify the use of performance counters.
Rust
63
star
4

rust-perfcnt

Rust library to program hardware performance counter.
Rust
49
star
5

rust-slabmalloc

Simple malloc implementation.
Rust
30
star
6

rust-multiboot

Multiboot library written in rust.
Rust
20
star
7

rust-uio

Linux UIO Library in Rust
Rust
17
star
8

contextswitch

Quantifying the cost of context switch
C
15
star
9

phdplot

Make nice plots with matplotlib.
Python
11
star
10

rust-raytracer

Raytracers in Rust.
Rust
9
star
11

rust-lkl

Wrapper to use the Linux LKL library in Rust
Rust
6
star
12

node-replication

Rust
5
star
13

rust-ext2

Rust
4
star
14

urcu-sys

Rust bindings for user-space RCU (https://github.com/urcu/userspace-rcu)
Rust
4
star
15

rust-driverkit

driverkit
Rust
4
star
16

backtracer

Simple backtrace support for no-std rust
Rust
4
star
17

rust-ahci

Rust
3
star
18

sv6

C
3
star
19

netlat

Rust
3
star
20

rust-topology

x86 hardware topology abstraction
Rust
3
star
21

rust-processortrace

Rust driver for Intel processor trace.
Rust
3
star
22

cslab

Code for Computer Systems Lab - Assignments
C
3
star
23

vmopsbench

C
2
star
24

rust-klogger

Kernel logging library in rust.
Rust
2
star
25

website

Personal website
TeX
2
star
26

acpica-sys

Rust bindings for acpica C library.
C
1
star
27

corealloc

CLI utility tool to allocate cores.
Rust
1
star
28

Index

Practice implementation of a hash table. Documentation: https://harmedchronogram.github.io/Index/docs/index/
Rust
1
star
29

rust-rumpkernel

Rumpkernel for use in rust projects.
Rust
1
star
30

aos10

Advanced Operating System 2010
C
1
star
31

termcodes

ANSI style escape codes
Rust
1
star
32

scfs

A state centric file-system
Rust
1
star
33

rust-workshop

Rust workshop material for DevPulseCon 2020/2021
Rust
1
star