#elf #ld #linker #pure #parser #link #read

bin+lib elfkit

an elf parser and manipulation library in pure rust

7 releases

Uses old Rust 2015

0.0.7 May 25, 2019
0.0.6 Jul 21, 2018
0.0.4 Oct 13, 2017
0.0.3 Sep 27, 2017

#6 in #ld

Download history 52/week @ 2024-03-11 30/week @ 2024-03-18 4/week @ 2024-03-25 78/week @ 2024-04-01 26/week @ 2024-04-08 105/week @ 2024-04-15 35/week @ 2024-04-22 21/week @ 2024-04-29 8/week @ 2024-05-06 24/week @ 2024-05-13 32/week @ 2024-05-20 15/week @ 2024-05-27 22/week @ 2024-06-03 16/week @ 2024-06-10 10/week @ 2024-06-17 11/week @ 2024-06-24

61 downloads per month
Used in 12 crates (2 directly)

MIT/Apache and GPL-2.0 licenses

6MB
5.5K SLoC

Contains (static library, 2.5MB) 999-libmusl.a, (static library, 2.5MB) tests/linker/c-common/zzz-libmusl.a, (static library, 2.5MB) tests/linker/c-init-array/999-libmusl.a, (static library, 2.5MB) tests/linker/c-simple/999-libmusl.a, (static library, 2.5MB) 998-libmusl.a, (static library, 2.5MB) 999-libmusl.a and 51 more.

Build Status MIT licensed docs

Elfkit

An elf read and manipulation library in pure Rust (written from scratch, no bfd, no gnu code, no license infections), intended to be used in binary manipulation utils such as strip, chrpath, objcopy and ld. The end goal is to build a well designed library that facilitates all sorts of binary manipulation magic.

elfkit can now link elfkit, so it's reasonably complete for x86_64. But it's definitely not stable yet and might produce incorrect code.

Using the linker

The quickest way to use elfkit with Rust is with korhal/stasis.

You can also either build from source or download binaries. GCC does not have an option to use a foreign linker, so we need to pretend we're ld.gold, like so:

curl -L https://github.com/aep/elfkit/releases/download/0.0.4/elfkit-0.0.4.tar.xz | tar xvjf -
export PATH="$PWD/elfkit-0.0.4/:$PATH"
musl-gcc -fuse-ld=gold main.c

For using elfkit for compiling Rust code, add the following to ~/.cargo/config:

[target.x86_64-unknown-linux-musl]
rustflags = [
    "-C", "link-arg=-fuse-ld=gold",
    "-C", "link-arg=-Wl,-dynamic-linker,/usr/local/musl/lib/libc.so",
]

When compiling from source, create the ld.gold symlink manually:

cargo build --release --bin ld
ln -s "$PWD/target/release/ld" /usr/local/bin/ld.gold

Other binutils

readelf: screenshot

implementation status

binutils

type status gnu compatible
ldd done no
readelf done no
ld wip wip
objdump - -
ar - -
as - -
nm - -
strip - -

section parsers

type read write
symbols done done
strtab done done
relocations done done
dynamic done done
note - -
gnu_hash - -
hash - mvp
versym - -
verneed - -

architectures

abi parser linker
x86_64 done wip
mips32r2 o32 done
arm eabi done

modular linker toolkit

  • Loader: loads elf objects from disk
  • Linker: produces a link graph of sections from a loader
  • Collector: bakes multiple sections into a single object
  • Relocator: applies relocations to a combined object

alternatives

  • goblin mach-o and archive support, no-std support, very low level
  • elf most popular, most generic use case, no writing, no section parsing
  • xmas-elf zero alloc (good for writing an OS), read only

references

Dependencies

~11–21MB
~300K SLoC