Skip to content

BTF relocations#3966

Open
vadorovsky wants to merge 1 commit into
rust-lang:masterfrom
vadorovsky:btf-relocations
Open

BTF relocations#3966
vadorovsky wants to merge 1 commit into
rust-lang:masterfrom
vadorovsky:btf-relocations

Conversation

@vadorovsky
Copy link
Copy Markdown

@vadorovsky vadorovsky commented May 31, 2026

The RFC proposes the btf_relocatable_types feature gate, that provides a #[repr(Btf)] representation and low-level field-info intrinsics that emit BTF (BPF TypeFormat) CO-RE (Compile Once, Run Everywhere) relocations.

It also documents the relationship to offset_of!, ordinary field projection and LLVM BPF lowering.

This feature was originally proposed as pre-RFC.

Rendered

vadorovsky added a commit to vadorovsky/rust that referenced this pull request May 31, 2026
BTF, the BPF Type Format, encodes type information for both the running
Linux kernel and compiled eBPF programs. An eBPF object can carry
relocation records that describe field and aggregate accesses in terms
of BTF types instead of fixed offsets; at load time, the loader compares
the program's BTF with the kernel's BTF and rewrites those accesses to
the correct offsets for the target kernel. This mechanism is often
referred to as "CO-RE relocations" or "BTF relocations".

`offset_of` always folds to a plain layout constant and does not
preserve enough information for BTF CO-RE relocation emission. As a
result, it is not suitable for relocatable field queries on BPF targets.

Add three explicit intrinsics for BTF field metadata queries:

* `btf_field_byte_offset`
* `btf_field_byte_size`
* `btf_field_exists`

The user-facing BTF relocatable type support remains behind the
`btf_relocations` feature gate, so use of this experimental CO-RE
surface requires an explicit nightly opt-in.

These intrinsics provide a frontend surface for CO-RE relocations.
Unlike `offset_of`, they remain visible to backend codegen and can lower
to relocatable field-info queries instead of immediate layout constants.

For LLVM, lower these intrinsics through `@llvm.bpf.preserve.field.info`
with the corresponding query kind. The necessary
`@llvm.preserve.{struct,array,union}.access.index` chain is constructed
internally during lowering, but it is not exposed as part of the
user-facing API.

On targets or backends without BTF relocation support, fall back to the
ordinary layout-computed result: the field offset for
`btf_field_byte_offset`, the field size for `btf_field_byte_size`, and
`true` for `btf_field_exists`.

The language-level design for this feature is proposed in
rust-lang/rfcs#3966.
vadorovsky added a commit to vadorovsky/rust that referenced this pull request May 31, 2026
BTF, the BPF Type Format, encodes type information for both the running
Linux kernel and compiled eBPF programs. An eBPF object can carry
relocation records that describe field and aggregate accesses in terms
of BTF types instead of fixed offsets; at load time, the loader compares
the program's BTF with the kernel's BTF and rewrites those accesses to
the correct offsets for the target kernel. This mechanism is often
referred to as "CO-RE relocations" or "BTF relocations".

`offset_of` always folds to a plain layout constant and does not
preserve enough information for BTF CO-RE relocation emission. As a
result, it is not suitable for relocatable field queries on BPF targets.

Add three intrinsics for BTF field metadata queries:

* `btf_field_byte_offset`
* `btf_field_byte_size`
* `btf_field_exists`

Their availability is hidden behind the `btf_relocations` feature gate.

Unlike `offset_of`, they remain visible to backend codegen and can lower
to relocatable field-info queries instead of immediate layout constants.

For LLVM, lower these intrinsics through `@llvm.bpf.preserve.field.info`
with the corresponding query kind. The necessary
`@llvm.preserve.{struct,array,union}.access.index` chain is constructed
internally during lowering, but it is not exposed as part of the
user-facing API.

On targets or backends without BTF relocation support, fall back to:

* The field offset for `btf_field_byte_offset`.
* The field size for `btf_field_byte_size`.
* `true` for `btf_field_exists`.

The language-level design for this feature is proposed in
rust-lang/rfcs#3966.
@Noratrieb Noratrieb added T-lang Relevant to the language team, which will review and decide on the RFC. T-opsem Relevant to the operational semantics team, which will review and decide on the RFC. labels May 31, 2026
@vadorovsky vadorovsky force-pushed the btf-relocations branch 2 times, most recently from dcd88f8 to 0a822d7 Compare June 1, 2026 11:30
The RFC proposes the `btf_relocations` feature gate, which provides a
`#[repr(Btf)]` representation and `core::btf` field-info macros that
emit BTF (BPF Type Format)[0] CO-RE (Compile Once, Run Everywhere)[1]
relocations.

It also documents the relationship to `offset_of!`, ordinary field
projection, and LLVM BPF lowering.

This feature was originally proposed as a pre-RFC[2].

[0] https://docs.kernel.org/bpf/btf.html
[1] https://nakryiko.com/posts/bpf-portability-and-co-re/
[2] https://internals.rust-lang.org/t/pre-rfc-btf-relocations/24161/25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

T-lang Relevant to the language team, which will review and decide on the RFC. T-opsem Relevant to the operational semantics team, which will review and decide on the RFC.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants