MaeBPF.Compiler.C (maebpf_c v0.1.0)

Copy Markdown View Source

C CO-RE eBPF compiler implementing @behaviour MaeBPF.Compiler for use MaeBPFC modules and direct {:c, source} compilation.

Compiles generated or raw C source in memory via Linux memfd_create (with temporary file fallback on non-Linux hosts) and clang -O2 -g -target bpf -mcpu=v3 -c -o -, with optional static BPF .bpf.o object linking via bpftool gen object.

Summary

Functions

Compiles C source to an eBPF ELF64 object binary in memory.

Compiles C source to an eBPF ELF64 binary and returns elapsed microseconds.

Ensures vmlinux.h exists in cache_dir, dumping it once from kernel BTF.

Statically links one or more precompiled BPF-target .bpf.o objects (EM_BPF, ET_REL) into primary_elf via bpftool gen object.

Functions

compile(c_source, opts \\ [])

Compiles C source to an eBPF ELF64 object binary in memory.

Returns {:ok, elf_binary} or {:error, reason}.

compile_ir(ir_module, opts \\ [])

Implements MaeBPF.Compiler.compile_ir/2 for use MaeBPFC modules.

Lowers %MaeBPF.IR.Module{} via MaeBPFC.Codegen.generate/2, compiles the C CO-RE source with clang -target bpf, and statically links any link_objects: (EM_BPF .bpf.o) via bpftool gen object.

Returns {:ok, %{elf: bin, ir: valid_ir, c_source: c_src, source_map: %{}, elapsed_us: us}}.

compile_timed(c_source, opts \\ [])

Compiles C source to an eBPF ELF64 binary and returns elapsed microseconds.

ensure_vmlinux_h(opts \\ [])

Ensures vmlinux.h exists in cache_dir, dumping it once from kernel BTF.