I have been down a rabbit hole of cleaning up the aya error types 😅 Most of the important changes are in `errors.rs`. TL;DR Current exposed types are: - `EbpfError` - `ProgramError` - `MapError` - `LinkError` - `PerfBufferError` - `SysError` Honestly I'm still thinking about how we could collapse those types. Either into a single type, or at least fewer than we expose today. Within each of those types, I've tried to remove any invariants that don't have any business being public (e.g if a syscall fails with -EINVAL, there is nothing at runtime you can do about it other than bailing). 👆 (and the spaghetti of errors depending on other errors) are replaced by an `Other` invariant that's a Box<dyn std::error::Error>. There are still some `pub(crate) XInternalError` types, but these are used only to make nice error messages. This could plausibly be replaced with anyhow/context etc.. But I've left it as-is for now. Signed-off-by: Dave Tucker <dave@dtucker.co.uk> |
8 months ago | |
---|---|---|
.. | ||
include | 8 months ago | |
src | 8 months ago | |
CHANGELOG.md | 8 months ago | |
Cargo.toml | 8 months ago | |
README.md | 2 years ago |
README.md
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();