# `MaeBPF.Compiler.C`
[🔗](https://github.com/cocoa-xu/MaeBPF/blob/v0.1.0/maebpf_c/lib/maebpf/compiler/c.ex#L1)

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`.

# `compile`

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

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

# `compile_ir`

Implements `c: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`

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

# `ensure_vmlinux_h`

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

# `link_objects`

```elixir
@spec link_objects(binary(), [String.t()], keyword()) ::
  {:ok, binary()} | {:error, term()}
```

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

Only kernel BPF relocatable objects (`e_type=ET_REL`, `e_machine=EM_BPF=247`)
are accepted; userspace native `.o`/`.so`/`libc` binaries cannot be linked
into Linux kernel eBPF.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
