Liking cljdoc? Tell your friends :D

clj-ebpf.relocate

CO-RE (Compile Once - Run Everywhere) relocation support.

This namespace provides functionality for BPF CO-RE relocations, enabling BPF programs to be portable across different kernel versions by using BTF information to relocate field accesses and type information at load time.

CO-RE relocations allow BPF programs to:

  • Access struct fields regardless of their offset in different kernel versions
  • Check for field/type/enum existence at load time
  • Get accurate field sizes and types
  • Handle bitfield operations correctly
  • Support both local and target BTF type IDs

Key concepts:

  • Field relocations: Adjust field offsets, sizes, existence
  • Type relocations: Check type existence, size, matches
  • Enum relocations: Get enum values, check existence
  • BTF-based resolution: Use kernel BTF to resolve relocations
CO-RE (Compile Once - Run Everywhere) relocation support.

This namespace provides functionality for BPF CO-RE relocations, enabling
BPF programs to be portable across different kernel versions by using BTF
information to relocate field accesses and type information at load time.

CO-RE relocations allow BPF programs to:
- Access struct fields regardless of their offset in different kernel versions
- Check for field/type/enum existence at load time
- Get accurate field sizes and types
- Handle bitfield operations correctly
- Support both local and target BTF type IDs

Key concepts:
- Field relocations: Adjust field offsets, sizes, existence
- Type relocations: Check type existence, size, matches
- Enum relocations: Get enum values, check existence
- BTF-based resolution: Use kernel BTF to resolve relocations
raw docstring

apply-relocationclj

(apply-relocation insns relo local-btf target-btf)

Apply a single CO-RE relocation to BPF instruction.

Parameters:

  • insns: BPF instruction bytecode (byte array)
  • relo: Relocation record (from create-relocation)
  • local-btf: Local program BTF data
  • target-btf: Target kernel BTF data

Returns updated instruction bytecode, or original if relocation fails.

The relocation updates the instruction's immediate or offset field based on the relocation kind and resolved value from target kernel BTF.

Apply a single CO-RE relocation to BPF instruction.

Parameters:
- insns: BPF instruction bytecode (byte array)
- relo: Relocation record (from create-relocation)
- local-btf: Local program BTF data
- target-btf: Target kernel BTF data

Returns updated instruction bytecode, or original if relocation fails.

The relocation updates the instruction's immediate or offset field based on
the relocation kind and resolved value from target kernel BTF.
sourceraw docstring

apply-relocationsclj

(apply-relocations insns relocations local-btf target-btf)

Apply all CO-RE relocations to BPF program.

Parameters:

  • insns: BPF instruction bytecode (byte array)
  • relocations: Sequence of relocation records
  • local-btf: Local program BTF data (from compiled object)
  • target-btf: Target kernel BTF data (from /sys/kernel/btf/vmlinux)

Returns updated instruction bytecode with all relocations applied.

Example: (apply-relocations bytecode relocs local-btf kernel-btf)

Apply all CO-RE relocations to BPF program.

Parameters:
- insns: BPF instruction bytecode (byte array)
- relocations: Sequence of relocation records
- local-btf: Local program BTF data (from compiled object)
- target-btf: Target kernel BTF data (from /sys/kernel/btf/vmlinux)

Returns updated instruction bytecode with all relocations applied.

Example:
  (apply-relocations bytecode relocs local-btf kernel-btf)
sourceraw docstring

core-read-supported?clj

(core-read-supported?)

Check if CO-RE relocations are supported on this system.

Returns true if kernel BTF is available and CO-RE can be used.

Check if CO-RE relocations are supported on this system.

Returns true if kernel BTF is available and CO-RE can be used.
sourceraw docstring

create-relocationclj

(create-relocation insn-off type-id access-str-off kind)

Create a CO-RE relocation record.

Parameters:

  • insn-off: Instruction offset in bytes within code section
  • type-id: BTF type ID of the root entity (struct, enum, etc.)
  • access-str-off: Offset into BTF string section (or access string)
  • kind: Relocation kind (keyword from relocation-kind map)

Returns map with relocation information.

Example: (create-relocation 24 42 "0:1" :field-byte-offset)

Create a CO-RE relocation record.

Parameters:
- insn-off: Instruction offset in bytes within code section
- type-id: BTF type ID of the root entity (struct, enum, etc.)
- access-str-off: Offset into BTF string section (or access string)
- kind: Relocation kind (keyword from relocation-kind map)

Returns map with relocation information.

Example:
  (create-relocation 24 42 "0:1" :field-byte-offset)
sourceraw docstring

generate-field-access-reloclj

(generate-field-access-relo insn-offset struct-name field-path btf-data)

Generate CO-RE relocation for field access.

This is used when generating BPF code dynamically to create relocatable field accesses.

Parameters:

  • insn-offset: Instruction offset where field access occurs
  • struct-name: Structure name (e.g., "task_struct")
  • field-path: Field access path (e.g., "pid" or "parent.pid")
  • btf-data: BTF data to lookup type ID

Returns relocation record for field offset.

Example: (generate-field-access-relo 16 "task_struct" "pid" btf)

Generate CO-RE relocation for field access.

This is used when generating BPF code dynamically to create relocatable
field accesses.

Parameters:
- insn-offset: Instruction offset where field access occurs
- struct-name: Structure name (e.g., "task_struct")
- field-path: Field access path (e.g., "pid" or "parent.pid")
- btf-data: BTF data to lookup type ID

Returns relocation record for field offset.

Example:
  (generate-field-access-relo 16 "task_struct" "pid" btf)
sourceraw docstring

get-kernel-btfclj

(get-kernel-btf)

Load kernel BTF for CO-RE relocations.

Returns BTF data from /sys/kernel/btf/vmlinux, or nil if not available.

Load kernel BTF for CO-RE relocations.

Returns BTF data from /sys/kernel/btf/vmlinux, or nil if not available.
sourceraw docstring

parse-access-stringclj

(parse-access-string access-str)

Parse CO-RE access string into field path indices.

Access string format: "idx1:idx2:idx3..."

  • Each index represents a field position in a nested structure
  • "0:1" means first field (0) of that struct, then second field (1) of that struct

Parameters:

  • access-str: Access string (e.g., "0:1:2")

Returns vector of field indices.

Examples: (parse-access-string "0") ; => [0] (parse-access-string "0:1") ; => [0 1] (parse-access-string "0:1:2") ; => [0 1 2]

Parse CO-RE access string into field path indices.

Access string format: "idx1:idx2:idx3..."
- Each index represents a field position in a nested structure
- "0:1" means first field (0) of that struct, then second field (1) of that struct

Parameters:
- access-str: Access string (e.g., "0:1:2")

Returns vector of field indices.

Examples:
  (parse-access-string "0")     ; => [0]
  (parse-access-string "0:1")   ; => [0 1]
  (parse-access-string "0:1:2") ; => [0 1 2]
sourceraw docstring

relocation-kindclj

CO-RE relocation kinds as defined in linux/bpf.h.

Field-based relocations (0-5):

  • :field-byte-offset - Field offset in bytes from struct start
  • :field-byte-size - Field size in bytes
  • :field-exists - Field existence (0 or 1)
  • :field-signed - Field signedness (0 unsigned, 1 signed)
  • :field-lshift-u64 - Left bit shift for bitfield extraction
  • :field-rshift-u64 - Right bit shift for bitfield extraction

Type-based relocations (6-9, 12):

  • :type-id-local - BTF type ID in local program BTF
  • :type-id-target - BTF type ID in target kernel BTF
  • :type-exists - Type existence in target kernel (0 or 1)
  • :type-size - Type size in bytes
  • :type-matches - Type layout match between local and target

Enum-based relocations (10-11):

  • :enumval-exists - Enum value existence (0 or 1)
  • :enumval-value - Enum value integer value
CO-RE relocation kinds as defined in linux/bpf.h.

Field-based relocations (0-5):
- :field-byte-offset  - Field offset in bytes from struct start
- :field-byte-size    - Field size in bytes
- :field-exists       - Field existence (0 or 1)
- :field-signed       - Field signedness (0 unsigned, 1 signed)
- :field-lshift-u64   - Left bit shift for bitfield extraction
- :field-rshift-u64   - Right bit shift for bitfield extraction

Type-based relocations (6-9, 12):
- :type-id-local      - BTF type ID in local program BTF
- :type-id-target     - BTF type ID in target kernel BTF
- :type-exists        - Type existence in target kernel (0 or 1)
- :type-size          - Type size in bytes
- :type-matches       - Type layout match between local and target

Enum-based relocations (10-11):
- :enumval-exists     - Enum value existence (0 or 1)
- :enumval-value      - Enum value integer value
sourceraw docstring

relocation-kind-namesclj

Reverse mapping from relocation kind value to keyword name.

Reverse mapping from relocation kind value to keyword name.
sourceraw docstring

resolve-bitfield-shiftsclj

(resolve-bitfield-shifts btf-data type-id field-path)

Calculate bitfield left/right shift values for extraction.

For bitfields, we need shift operations to extract the value:

  1. Left shift to align MSB with register MSB
  2. Right shift (arithmetic for signed, logical for unsigned) to align LSB

Parameters:

  • btf-data: BTF data
  • type-id: Starting BTF type ID
  • field-path: Vector of field indices

Returns map with :lshift and :rshift values, or nil if not a bitfield.

Calculate bitfield left/right shift values for extraction.

For bitfields, we need shift operations to extract the value:
1. Left shift to align MSB with register MSB
2. Right shift (arithmetic for signed, logical for unsigned) to align LSB

Parameters:
- btf-data: BTF data
- type-id: Starting BTF type ID
- field-path: Vector of field indices

Returns map with :lshift and :rshift values, or nil if not a bitfield.
sourceraw docstring

resolve-enum-valueclj

(resolve-enum-value btf-data enum-type-id value-name)

Get enum value from target BTF.

Parameters:

  • btf-data: Target kernel BTF data
  • enum-type-id: Enum BTF type ID
  • value-name: Enum value name

Returns enum integer value, or 0 if not found.

Get enum value from target BTF.

Parameters:
- btf-data: Target kernel BTF data
- enum-type-id: Enum BTF type ID
- value-name: Enum value name

Returns enum integer value, or 0 if not found.
sourceraw docstring

resolve-enum-value-existsclj

(resolve-enum-value-exists btf-data enum-type-id value-name)

Check if enum value exists in target BTF.

Parameters:

  • btf-data: Target kernel BTF data
  • enum-type-id: Enum BTF type ID
  • value-name: Enum value name

Returns 1 if enum value exists, 0 if not.

Check if enum value exists in target BTF.

Parameters:
- btf-data: Target kernel BTF data
- enum-type-id: Enum BTF type ID
- value-name: Enum value name

Returns 1 if enum value exists, 0 if not.
sourceraw docstring

resolve-field-existsclj

(resolve-field-exists btf-data type-id field-path)

Check if field exists using BTF information.

Parameters:

  • btf-data: BTF data
  • type-id: Starting BTF type ID
  • field-path: Vector of field indices

Returns 1 if field exists, 0 if not.

Check if field exists using BTF information.

Parameters:
- btf-data: BTF data
- type-id: Starting BTF type ID
- field-path: Vector of field indices

Returns 1 if field exists, 0 if not.
sourceraw docstring

resolve-field-offsetclj

(resolve-field-offset btf-data type-id field-path)

Resolve field offset in bytes using BTF information.

Walks through the field path (access string) to find the final field offset.

Parameters:

  • btf-data: BTF data (from btf/load-btf-file)
  • type-id: Starting BTF type ID (struct/union)
  • field-path: Vector of field indices (from parse-access-string)

Returns field offset in bytes, or nil if field not found.

Example: (resolve-field-offset btf 42 [0 1]) ; Get offset of second field of first field

Resolve field offset in bytes using BTF information.

Walks through the field path (access string) to find the final field offset.

Parameters:
- btf-data: BTF data (from btf/load-btf-file)
- type-id: Starting BTF type ID (struct/union)
- field-path: Vector of field indices (from parse-access-string)

Returns field offset in bytes, or nil if field not found.

Example:
  (resolve-field-offset btf 42 [0 1])  ; Get offset of second field of first field
sourceraw docstring

resolve-field-signedclj

(resolve-field-signed btf-data type-id field-path)

Check if field is signed using BTF information.

Parameters:

  • btf-data: BTF data
  • type-id: Starting BTF type ID
  • field-path: Vector of field indices

Returns 1 if signed, 0 if unsigned, nil if unknown.

Check if field is signed using BTF information.

Parameters:
- btf-data: BTF data
- type-id: Starting BTF type ID
- field-path: Vector of field indices

Returns 1 if signed, 0 if unsigned, nil if unknown.
sourceraw docstring

resolve-field-sizeclj

(resolve-field-size btf-data type-id field-path)

Resolve field size in bytes using BTF information.

Parameters:

  • btf-data: BTF data
  • type-id: Starting BTF type ID
  • field-path: Vector of field indices

Returns field size in bytes, or nil if field not found.

Resolve field size in bytes using BTF information.

Parameters:
- btf-data: BTF data
- type-id: Starting BTF type ID
- field-path: Vector of field indices

Returns field size in bytes, or nil if field not found.
sourceraw docstring

resolve-type-existsclj

(resolve-type-exists btf-data type-name)

Check if type exists in target BTF.

Parameters:

  • btf-data: Target kernel BTF data
  • type-name: Type name to search for

Returns 1 if type exists, 0 if not.

Check if type exists in target BTF.

Parameters:
- btf-data: Target kernel BTF data
- type-name: Type name to search for

Returns 1 if type exists, 0 if not.
sourceraw docstring

resolve-type-matchesclj

(resolve-type-matches local-btf target-btf local-type-id target-type-id)

Check if local type layout matches target type layout.

This is a simplified check that compares type sizes. A full implementation would recursively compare all struct members.

Parameters:

  • local-btf: Local program BTF data
  • target-btf: Target kernel BTF data
  • local-type-id: Local BTF type ID
  • target-type-id: Target BTF type ID

Returns 1 if types match, 0 if not.

Check if local type layout matches target type layout.

This is a simplified check that compares type sizes. A full implementation
would recursively compare all struct members.

Parameters:
- local-btf: Local program BTF data
- target-btf: Target kernel BTF data
- local-type-id: Local BTF type ID
- target-type-id: Target BTF type ID

Returns 1 if types match, 0 if not.
sourceraw docstring

resolve-type-sizeclj

(resolve-type-size btf-data type-id)

Get type size from target BTF.

Parameters:

  • btf-data: Target kernel BTF data
  • type-id: BTF type ID

Returns size in bytes, or 0 if type not found.

Get type size from target BTF.

Parameters:
- btf-data: Target kernel BTF data
- type-id: BTF type ID

Returns size in bytes, or 0 if type not found.
sourceraw docstring

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
Move to previous article
Move to next article
Ctrl+/Jump to the search field
× close