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

Zero-clang pure-Elixir eBPF ISA v3 + ELF64 + `.BTF` + `.BTF.ext` compiler.

Emits standards-compliant little-endian ELF64 (`EM_BPF`, `ELFDATA2LSB`)
relocatable objects containing:

  * One or more executable program sections (`xdp`, `tcx/ingress`,
    `tcx/egress`, `tracepoint/<category>/<name>`)
  * `license` section (without a leading dot, matching libbpf requirements)
  * Optional `.maps` section and `.rel<sec>` `R_BPF_64_64` relocations
  * Valid `.BTF` type graph (BTF-defined maps including `LPM_TRIE` with
    `BPF_F_NO_PREALLOC` and `RINGBUF` without `key`/`value`,
    `DATASEC(".maps")`, per-program `FUNC` + `FUNC_PROTO`) and `.BTF.ext`
    `func_info` records so `libbpf 1.5.0` and the Linux kernel load all
    programs and shared maps cleanly.

Supports both low-level instruction specifications (`compile/1`,
`compile_timed/1`, `xdp_packet_counter/1`, `xdp_percpu_packet_counter/1`,
`xdp_return/1`) and high-level fused `%MaeBPF.IR.Module{}` compilation
(`compile_ir/1`, `compile_ir/2`, `compile_ir!/1`, `compile_ir!/2`).

# `compile`

Compiles a low-level program specification map into a valid ELF64 eBPF binary.

Specification keys:
  * `:section` - ELF section name (default `"xdp"`)
  * `:name` - program function name (default `"xdp_prog"`)
  * `:license` - license string (default `"GPL"`)
  * `:maps` - list of map specs
  * `:instructions` - list of eBPF instructions and `{:label, atom}` markers

# `compile_ir`

Compiles a `%MaeBPF.IR.Module{}` (multi-program, multi-map, fused packet
parser, static safety checked) into a little-endian `bpfel` ELF64 object.

Options:
  * `:debug_trace` - when `true`, injects `"__maebpf_debug_rb"` and
    `"__maebpf_rb_drops___maebpf_debug_rb"` and emits per-plug trace events
    (default `false`)
  * `:sample` - debug trace sampling interval (`>= 1`, default `1`)
  * `:ringbuf_kb` - debug RingBuf size in KiB (default `256`)
  * `:max_stack_bytes` - stack limit override (default `512`)
  * `:max_instructions` - instruction budget override (default `4096`)
  * `:max_branches` - branch budget override (default `512`)

Returns `{:ok, %{elf: bin, ir: ir, source_map: map, insn_counts: map, stack_bytes: map, elapsed_us: int}}`.

# `compile_ir!`

Like `compile_ir/2`, raising `ArgumentError` on validation or compilation
failure.

# `compile_timed`

Compiles a low-level program specification map and returns
`{:ok, elf_bin, elapsed_us}`.

# `xdp_packet_counter`

Builds a ready-to-load XDP program in pure Elixir that counts packets in a
BTF-defined `BPF_MAP_TYPE_ARRAY` or `BPF_MAP_TYPE_PERCPU_ARRAY` map
(`key: u32`, `value: u64`) and returns `XDP_PASS` (2) by default.

# `xdp_percpu_packet_counter`

Convenience wrapper for `xdp_packet_counter/1` using a lock-free
`BPF_MAP_TYPE_PERCPU_ARRAY` counter map.

# `xdp_return`

Builds a minimal pure-Elixir XDP program returning `action` (default `XDP_PASS = 2`).

---

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