From: Yonghong Song <yonghong.song@linux.dev>
To: Eduard Zingerman <eddyz87@gmail.com>, bpf@vger.kernel.org
Cc: Alexei Starovoitov <ast@kernel.org>,
Andrii Nakryiko <andrii@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
kernel-team@fb.com, vineet.gupta@linux.dev
Subject: Re: [PATCH bpf-next v5 0/3] libbpf: Fix CO-RE relocations of unsigned 32-bit enum values
Date: Mon, 23 Nov 2026 10:04:22 -0800 [thread overview]
Message-ID: <cc4f6f63-d21a-4e78-a9fc-3397e041955e@linux.dev> (raw)
In-Reply-To: <8fdf1b51-7368-46e0-80f9-744b15845ad0@linux.dev>
On 11/23/26 10:01 AM, Yonghong Song wrote:
>
>
> On 11/23/26 9:45 AM, Eduard Zingerman wrote:
>> On Fri, 2026-11-20 at 13:49 -0800, Yonghong Song wrote:
>>> For a BPF_CORE_ENUMVAL_VALUE relocation, libbpf sign-extends the value
>>> of a 32-bit enumerator even if the enum is unsigned. A value in
>>> [0x80000000, 0xffffffff], e.g., DCACHE_PRIVATE of enum dentry_flags,
>>> then doesn't match the zero-extended value clang emits, and the whole
>>> object is refused. And a target value in that range is patched
>>> sign-extended.
>>>
>>> Patch 1 zero-extends the 32-bit values of unsigned enums, i.e., with
>>> the kflag 0, which is the form clang emits. The kernel builds the same
>>> relo_core.c, so it gets the fix too.
>>>
>>> GCC emits every 32-bit enum value sign-extended, since its
>>> __builtin_preserve_enum_value() returns int. So with the change above
>>> alone, GCC objects that load today would be refused. Patch 1 therefore
>>> also accepts the sign-extended form of an unsigned value, and patches
>>> the target value in the same form.
>> Hi Yonghong,
>>
>> I checked LLVM and GCC definitions for __builtin_preserve_enum_value().
>> LLVM:
>>
>> clang/include/clang/Basic/BuiltinsBPF.td:
>>
>> def PreserveEnumValue : TargetBuiltin {
>> let Spellings = ["__builtin_preserve_enum_value"];
>> let Attributes = [CustomTypeChecking];
>> let Prototype = "long int(...)";
>> }
>>
>> ---
>>
>> gcc/config/bpf/bpf.cc:
>>
>> def_builtin ("__builtin_preserve_enum_value",
>> BPF_BUILTIN_PRESERVE_ENUM_VALUE,
>> build_function_type_list (integer_type_node,
>> ptr_type_node,
>> integer_type_node,
>> integer_type_node,
>> 0));
>> gcc/tree.cc:
>>
>> integer_type_node = make_signed_type (INT_TYPE_SIZE);
>>
>>
>> Meaning that the two indeed disagree. Given that GCC version can't
>> express 64-bit values, it appears that GCC side definition needs an
>> update. Would this allow to avoid GCC-specific workaround in libbpf?
>
> Yes, I am aware of this issue. See
> https://lore.kernel.org/bpf/a4aec2b1-f754-48ed-a7d7-ef07bed43eb8@linux.dev/
>
> I even suggested a solution:
>
> diff --git a/gcc/config/bpf/bpf.cc b/gcc/config/bpf/bpf.cc index
> 3ef93938222..45d5337bab6 100644 --- a/gcc/config/bpf/bpf.cc +++
> b/gcc/config/bpf/bpf.cc @@ -1048,7 +1048,7 @@ bpf_init_builtins (void)
> integer_type_node, 0));
> def_builtin ("__builtin_preserve_enum_value",
> BPF_BUILTIN_PRESERVE_ENUM_VALUE,
> - build_function_type_list (integer_type_node, ptr_type_node, +
> build_function_type_list (ullt, ptr_type_node, integer_type_node,
> integer_type_node,
> 0));
>
> diff --git a/gcc/config/bpf/core-builtins.cc
> b/gcc/config/bpf/core-builtins.cc index a5c973cd385..289d3e67d78
> 100644 --- a/gcc/config/bpf/core-builtins.cc +++
> b/gcc/config/bpf/core-builtins.cc @@ -877,8 +877,12 @@
> pack_enum_value_fail: else
> {
> ret.reloc_data.expr = tmp;
> + /* Extend the enumerator to 64 bits according to its own signedness,
> + as clang does, instead of expanding it as a sign-extended SImode +
> CONST_INT. */ if (kind == BPF_RELO_ENUMVAL_VALUE)
> - ret.reloc_data.default_value = enum_value; +
> ret.reloc_data.default_value + = fold_convert
> (long_long_unsigned_type_node, enum_value); else
> ret.reloc_data.default_value = integer_one_node;
> }
>
> Vineet Gupta will take a look.
>
> Discussed with Alexei earlier, the suggestion is to proceed as is for
> now.
> It is not clear when gcc will implement this.
Sorry, messed up the above diff. The below is a better one:
diff --git a/gcc/config/bpf/bpf.cc b/gcc/config/bpf/bpf.cc index
3ef93938222..45d5337bab6 100644 --- a/gcc/config/bpf/bpf.cc +++
b/gcc/config/bpf/bpf.cc @@ -1048,7 +1048,7 @@ bpf_init_builtins (void) integer_type_node, 0));
def_builtin ("__builtin_preserve_enum_value",
BPF_BUILTIN_PRESERVE_ENUM_VALUE,
- build_function_type_list (integer_type_node, ptr_type_node, +
build_function_type_list (ullt, ptr_type_node, integer_type_node, integer_type_node,
0));
diff --git a/gcc/config/bpf/core-builtins.cc
b/gcc/config/bpf/core-builtins.cc index a5c973cd385..289d3e67d78 100644
--- a/gcc/config/bpf/core-builtins.cc +++
b/gcc/config/bpf/core-builtins.cc @@ -877,8 +877,12 @@
pack_enum_value_fail: else
{
ret.reloc_data.expr = tmp;
+ /* Extend the enumerator to 64 bits according to its own signedness, +
as clang does, instead of expanding it as a sign-extended SImode +
CONST_INT. */ if (kind == BPF_RELO_ENUMVAL_VALUE)
- ret.reloc_data.default_value = enum_value; +
ret.reloc_data.default_value + = fold_convert
(long_long_unsigned_type_node, enum_value); else
ret.reloc_data.default_value = integer_one_node;
}
next prev parent reply other threads:[~2026-11-23 18:04 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-20 21:49 [PATCH bpf-next v5 0/3] libbpf: Fix CO-RE relocations of unsigned 32-bit enum values Yonghong Song
2026-11-20 21:50 ` [PATCH bpf-next v5 1/3] " Yonghong Song
2026-11-20 21:50 ` [PATCH bpf-next v5 2/3] selftests/bpf: Test " Yonghong Song
2026-11-20 21:50 ` [PATCH bpf-next v5 3/3] selftests/bpf: Test CO-RE relocations of signed enum values without kflag Yonghong Song
2026-11-20 22:39 ` bot+bpf-ci
2026-11-21 0:21 ` Yonghong Song
2026-11-23 17:45 ` [PATCH bpf-next v5 0/3] libbpf: Fix CO-RE relocations of unsigned 32-bit enum values Eduard Zingerman
2026-11-23 18:01 ` Yonghong Song
2026-11-23 18:04 ` Yonghong Song [this message]
2026-11-23 19:46 ` Eduard Zingerman
2026-11-23 22:08 ` Yonghong Song
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=cc4f6f63-d21a-4e78-a9fc-3397e041955e@linux.dev \
--to=yonghong.song@linux.dev \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=kernel-team@fb.com \
--cc=vineet.gupta@linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.