BPF assembly utilities including label resolution.
Provides symbolic label support for BPF programs, eliminating the need for manual jump offset calculations.
Example: (require '[clj-ebpf.asm :as asm] '[clj-ebpf.dsl :as dsl])
(asm/assemble-with-labels [(dsl/mov :r0 0) (asm/jmp-imm :jeq :r1 0 :success) (dsl/mov :r0 -1) (asm/jmp :done) (asm/label :success) (dsl/mov :r0 1) (asm/label :done) (dsl/exit-insn)])
BPF assembly utilities including label resolution.
Provides symbolic label support for BPF programs, eliminating
the need for manual jump offset calculations.
Example:
(require '[clj-ebpf.asm :as asm]
'[clj-ebpf.dsl :as dsl])
(asm/assemble-with-labels
[(dsl/mov :r0 0)
(asm/jmp-imm :jeq :r1 0 :success)
(dsl/mov :r0 -1)
(asm/jmp :done)
(asm/label :success)
(dsl/mov :r0 1)
(asm/label :done)
(dsl/exit-insn)])(assemble-with-labels instructions)Assemble instructions with automatic label resolution.
This is the main entry point for programs using symbolic labels. Handles mixed sequences of:
label)jmp-imm, jmp-reg, jmp)Parameters:
Returns: Assembled bytecode (byte array)
Example: (assemble-with-labels [(dsl/mov :r0 0) (jmp-imm :jeq :r1 0 :success) (dsl/mov :r0 -1) (jmp :done) (label :success) (dsl/mov :r0 1) (label :done) (dsl/exit-insn)])
Assemble instructions with automatic label resolution.
This is the main entry point for programs using symbolic labels.
Handles mixed sequences of:
- Label pseudo-instructions (created with `label`)
- Symbolic jump instructions (created with `jmp-imm`, `jmp-reg`, `jmp`)
- Regular DSL instructions (byte arrays)
Parameters:
- instructions: Sequence of instructions
Returns: Assembled bytecode (byte array)
Example:
(assemble-with-labels
[(dsl/mov :r0 0)
(jmp-imm :jeq :r1 0 :success)
(dsl/mov :r0 -1)
(jmp :done)
(label :success)
(dsl/mov :r0 1)
(label :done)
(dsl/exit-insn)])(check-bounds data-reg data-end-reg offset fail-label scratch-reg)Generate bounds check with symbolic label for failure.
Like net/check-bounds but accepts keyword labels.
Parameters:
Example: (check-bounds :r7 :r8 14 :pass :r9)
Generate bounds check with symbolic label for failure. Like net/check-bounds but accepts keyword labels. Parameters: - data-reg: Register containing data pointer - data-end-reg: Register containing data_end pointer - offset: Number of bytes needed - fail-label: Label (keyword) or offset (number) to jump on failure - scratch-reg: Scratch register for calculation Example: (check-bounds :r7 :r8 14 :pass :r9)
(check-bounds-dynamic data-reg data-end-reg offset-reg fail-label scratch-reg)Generate bounds check with dynamic offset.
Parameters:
Generate bounds check with dynamic offset. Parameters: - data-reg: Register containing data pointer - data-end-reg: Register containing data_end pointer - offset-reg: Register containing offset value - fail-label: Label (keyword) or offset (number) to jump on failure - scratch-reg: Scratch register for calculation
(instruction-size insn)Return the size of an instruction in 8-byte units. Most instructions are 1, but lddw (64-bit immediate load) is 2.
Return the size of an instruction in 8-byte units. Most instructions are 1, but lddw (64-bit immediate load) is 2.
(jmp target)Unconditional jump to symbolic target.
Parameters:
Example: (jmp :done) ; symbolic (jmp 5) ; numeric
Unconditional jump to symbolic target. Parameters: - target: Jump target - keyword label or numeric offset Example: (jmp :done) ; symbolic (jmp 5) ; numeric
(jmp-imm op dst imm target)Conditional jump with immediate operand and symbolic target.
Parameters:
If target is a keyword, it will be resolved during assembly. If target is a number, works like dsl/jmp-imm.
Example: (jmp-imm :jeq :r0 0 :is-zero) ; symbolic (jmp-imm :jeq :r0 0 5) ; numeric (backwards compat)
Conditional jump with immediate operand and symbolic target. Parameters: - op: Jump operation (:jeq, :jne, :jgt, :jge, :jlt, :jle, :jset, :jsgt, :jsge, :jslt, :jsle) - dst: Destination register - imm: Immediate value to compare - target: Jump target - keyword label or numeric offset If target is a keyword, it will be resolved during assembly. If target is a number, works like dsl/jmp-imm. Example: (jmp-imm :jeq :r0 0 :is-zero) ; symbolic (jmp-imm :jeq :r0 0 5) ; numeric (backwards compat)
(jmp-reg op dst src target)Conditional jump with register operand and symbolic target.
Parameters:
Example: (jmp-reg :jgt :r0 :r1 :greater) ; symbolic (jmp-reg :jgt :r0 :r1 3) ; numeric
Conditional jump with register operand and symbolic target. Parameters: - op: Jump operation - dst: First register to compare - src: Second register to compare - target: Jump target - keyword label or numeric offset Example: (jmp-reg :jgt :r0 :r1 :greater) ; symbolic (jmp-reg :jgt :r0 :r1 3) ; numeric
(label name)Create a label pseudo-instruction. Labels mark positions in the instruction stream for jump targets. They don't generate bytecode - they're resolved during assembly.
Parameters:
Example: (label :my-target)
Create a label pseudo-instruction. Labels mark positions in the instruction stream for jump targets. They don't generate bytecode - they're resolved during assembly. Parameters: - name: Keyword naming the label (e.g., :loop, :error, :done) Example: (label :my-target)
(label? insn)Check if instruction is a label pseudo-instruction.
Check if instruction is a label pseudo-instruction.
(real-instruction? insn)Check if this counts as a real instruction (not a label). Used for position counting.
Check if this counts as a real instruction (not a label). Used for position counting.
(resolve-labels instructions)Resolve symbolic labels to numeric offsets.
Two-pass algorithm:
Parameters:
Returns: Sequence of resolved instructions (byte arrays only, no labels)
Resolve symbolic labels to numeric offsets. Two-pass algorithm: 1. Collect all label positions 2. Resolve jump targets and filter out labels Parameters: - instructions: Sequence of instructions (may include labels and symbolic jumps) Returns: Sequence of resolved instructions (byte arrays only, no labels)
(symbolic-jump? insn)Check if instruction is a symbolic jump (with keyword target).
Check if instruction is a symbolic jump (with keyword target).
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |