#object-file #ebpf #object #btf #elf #low-level

no-std aya-obj

An eBPF object file parsing library with BTF and relocation support

3 unstable releases

0.2.1 Nov 1, 2024
0.2.0 Oct 9, 2024
0.1.0 Feb 28, 2024

#434 in Unix APIs

Download history 650/week @ 2024-08-24 950/week @ 2024-08-31 910/week @ 2024-09-07 1580/week @ 2024-09-14 1807/week @ 2024-09-21 1781/week @ 2024-09-28 3993/week @ 2024-10-05 5269/week @ 2024-10-12 4402/week @ 2024-10-19 3222/week @ 2024-10-26 2588/week @ 2024-11-02 2844/week @ 2024-11-09 3151/week @ 2024-11-16 3827/week @ 2024-11-23 4035/week @ 2024-11-30 4184/week @ 2024-12-07

15,682 downloads per month
Used in 19 crates (3 directly)

MIT/Apache

1MB
23K SLoC

aya-obj

Status

This crate includes code that started as internal API used by the aya crate. It has been split out so that it can be used by other projects that deal with eBPF object files. Unless you're writing low level eBPF plumbing tools, you should not need to use this crate but see the aya crate instead.

The API as it is today has a few rough edges and is generally not as polished nor stable as the main aya crate API. As always, improvements welcome!

Overview

eBPF programs written with libbpf or aya-bpf are usually compiled into an ELF object file, using various sections to store information about the eBPF programs.

aya-obj is a library for parsing such eBPF object files, with BTF and relocation support.

Example

This example loads a simple eBPF program and runs it with rbpf.

use aya_obj::{generated::bpf_insn, Object};

// Parse the object file
let bytes = std::fs::read("program.o").unwrap();
let mut object = Object::parse(&bytes).unwrap();
// Relocate the programs
object.relocate_calls().unwrap();
object.relocate_maps(std::iter::empty()).unwrap();

// Run with rbpf
let instructions = &object.programs["prog_name"].function.instructions;
let data = unsafe {
    core::slice::from_raw_parts(
        instructions.as_ptr() as *const u8,
        instructions.len() * core::mem::size_of::<bpf_insn>(),
    )
};
let vm = rbpf::EbpfVmNoData::new(Some(data)).unwrap();
let _return = vm.execute_program().unwrap();

Dependencies

~2.2–3.5MB
~61K SLoC