[PATCH 2/2] bpf: CO-RE consumer of deferred constants
David Faust
david.faust@oss.qualcomm.com
Tue Nov 3 02:09:09 GMT 2026
On 10/31/26 7:54 PM, York Jasper Niebuhr wrote:
> Use deferred constants to preserve semantic information required for BPF
> CO-RE relocations.
>
> Mark enum types for deferred enum values and record types carrying
> "preserve_access_index" for deferred field offsets. Deferred constants
> encountered during BPF lowering are either turned into CO-RE relocations
> or resolved to their ordinary compile-time values when no relocation is
> requested.
>
> Add a "preserve_enum_values" attribute for enum types. Enumerators of a
> type carrying this attribute automatically generate
> BPF_RELO_ENUMVAL_VALUE relocations.
>
> Remove the bpf_enum_mappings workaround in favor of direct enum identity
> recovery from deferred cst arguments to __builtin_preserve_enum_value.
>
> For record types carrying "preserve_access_index", deferred field
> offsets originating from offsetof expressions are converted into
> BPF_RELO_FIELD_BYTE_OFFSET relocations.
>
> BPF CO-RE relocations can not currently be emitted for static data. Add
> an IPA pass which resolves deferred constants in static initializers
> before the generic deferred-constant barrier. Initializers which would
> require CO-RE relocations are diagnosed. Support for such initializers
> could be added in the future by generating relocated runtime
> initialization code, for cases such as:
>
> struct task_struct {
> ...
> int x;
> ...
> } __attribute__((preserve_access_index));
>
> static size_t OFF_X = offsetof (struct task_struct, x);
>
So I gave this a try to see if the above static initializer thing
is an issue for the kernel selftests.
Instead I encountered another problem when using an enumerator to index
into an array, e.g.:
enum bpf_map_type {
BPF_MAP_TYPE_START,
BPF_MAP_TYPE_RINGBUF,
__BPF_MAP_TYPE_MAX
};
int a[BPF_MAP_TYPE_RINGBUF];
$ ./xgcc -B. ~/playpen/bpf/defer.c -c
/home/dfaust/playpen/bpf/defer.c:8:5: error: variably modified ‘a’ at file scope
8 | int a[BPF_MAP_TYPE_RINGBUF];
| ^
It's coming from this spot in c-decl.cc grokdeclarator:7444
else if ((decl_context == NORMAL || decl_context == FIELD)
&& current_scope == file_scope)
{
error_at (loc, "variably modified %qE at file scope", name);
size = integer_one_node;
}
Not sure whether this indicates a bug in the C/middle-end portion,
or due to how it is being used from the BPF backend, e.g. if the
plugin_finish_type hook needs to take more care rather than just
marking any enumeral type.
More information about the bpf
mailing list