From: Yonghong Song <yonghong.song@linux.dev>
To: bot+bpf-ci@kernel.org, bpf@vger.kernel.org
Cc: ast@kernel.org, andrii@kernel.org, daniel@iogearbox.net,
eddyz87@gmail.com, kernel-team@fb.com, martin.lau@kernel.org,
mason@kernel.org, ihor.solodrai@linux.dev
Subject: Re: [PATCH bpf-next 1/2] libbpf: Zero-extend unsigned 32-bit enum values in CO-RE relocations
Date: Wed, 18 Nov 2026 17:30:54 -0800 [thread overview]
Message-ID: <a4aec2b1-f754-48ed-a7d7-ef07bed43eb8@linux.dev> (raw)
In-Reply-To: <e5fd05a670861cd47cf6112096c1904ea418ef9335cbeb9badd54ed87c24e1f0@mail.kernel.org>
On 11/18/26 8:59 AM, bot+bpf-ci@kernel.org wrote:
>> libbpf: Zero-extend unsigned 32-bit enum values in CO-RE relocations
> TL;DR: Zero-extending unsigned 32-bit enum values in CO-RE relocations may
> make GCC-built objects, which appear to emit sign-extended values
> (unverified), fail to load with -EINVAL, in libbpf and kernel
> bpf_core_apply().
>
>> diff --git a/tools/lib/bpf/relo_core.c b/tools/lib/bpf/relo_core.c
>> --- a/tools/lib/bpf/relo_core.c
>> +++ b/tools/lib/bpf/relo_core.c
> [ ... ]
>
>> @@ -875,10 +882,7 @@ static int bpf_core_calc_enumval_relo(const struct bpf_core_relo *relo,
>> if (!spec)
>> return -EUCLEAN; /* request instruction poisoning */
>> t = btf_type_by_id(spec->btf, spec->spec[0].type_id);
>> - if (btf_is_enum(t))
>> - *val = btf_enum(t)[spec->spec[0].idx].val;
>> - else
>> - *val = btf_enum64_value(btf_enum64(t) + spec->spec[0].idx);
>> + *val = bpf_core_enumval(t, spec->spec[0].idx);
>> break;
> Can this break objects built by GCC?
>
> bpf_core_patch_insn() compares the local (orig) value against the
> instruction immediate using a full 64-bit compare. In the ALU path that is
> insn->imm != orig_val, where the int imm is sign-extended, and in the
> LDIMM64 path it is:
>
> (__u32)insn[0].imm | ((__u64)insn[1].imm << 32)
>
> Clang emits unsigned enumerators zero-extended, which is the case this
> patch fixes. From reading the GCC source (not verified by compiling), GCC
> appears to emit every 32-bit enumerator sign-extended:
>
> gcc/config/bpf/core-builtins.cc:pack_enum_value() {
> ret.reloc_data.default_value = enum_value;
> }
>
> bpf_expand_core_builtin() then does expand_normal(data->default_value) and
> wraps it in an UNSPEC. An SImode CONST_INT is stored sign-extended in RTL,
> so 0x80000000 would be printed as 0xffffffff80000000. GCC's btfout.cc also
> sets btf_kflag to BTF_KF_ENUM_UNSIGNED, which is 0, for unsigned enums.
>
> For a GCC-built object calling bpf_core_enum_value() on an enumerator of
> 0x80000000 in an unsigned enum:
>
> Before this patch, *val was the sign-extended 0xffffffff80000000, which
> matched the instruction.
>
> After this patch, btf_enum_value() returns 0x80000000 for kflag == 0, so
> bpf_core_patch_insn() would report "unexpected insn #N (ALU/ALU64|LDIMM64)
> value: got ..., exp 2147483648" and return -EINVAL. The same code is built
> into the kernel through kernel/bpf/relo_core.c, so bpf_core_apply() is
> affected as well.
>
> Would objects built by Clang now load while GCC-built objects that loaded
> before are refused? The core_reloc/enumval tests are not in
> ci/vmtest/configs/DENYLIST.test_progs-bpf_gcc, and the test added by
> 0aff1079d5e1 ("selftests/bpf: Test CO-RE relocations of unsigned 32-bit
> enum values") uses UINT_ENUM_VAL1 = 0x80000000, so it may show this in the
> test_progs-bpf_gcc lane.
Yes, this is indeed an issue for GCC. Specifically because:
- BTF type is unsigned. This actually correct for the following:
enum dentry_flags { DCACHE_PRIVATE = 0x80000000 };
- Without this patch, both clang and gcc will have enum val with sign extension.
gcc enum return value has type int so naturally it will do sign extension,
but clang return value is unsigned, but it use signed type, so this is wrong.
This patch fixed clang to use unsigned type based on BTF and this is correct.
For gcc, BTF type is unsigned (based on language), but the enum return value is signed.
This caused a problem. gcc already generates code assuming signed type and we cannot
change already generated code, so the only thing is to change gcc BTF type from
'unsigned' to 'signed'. This is doable though.
>
> Could the validation for 32-bit enums compare only the low 32 bits and
> accept upper bits that are either zero or a sign extension? Alternatively,
> the GCC encoding could be determined before changing orig_val.
So yes, we can change orig_val to be signed (instead of original unsigned type)
to fix the issue. Then enum value will be different, for clang, the value is
32bit unsigned. For gcc, the value will be 32bit sign-extension.
The divergence between clang and gcc is in return type, clang (u64, unsigned)
and gcc(int, signed). If gcc can have return value the same as clang, things
can be simplified.
The following gcc diff might be useful for gcc folks:
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;
}
>
>
> ---
> AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
> See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
>
> CI run summary: https://github.com/kernel-patches/bpf/actions/runs/44347609693
prev parent reply other threads:[~2026-11-19 1:31 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-18 16:10 [PATCH bpf-next 1/2] libbpf: Zero-extend unsigned 32-bit enum values in CO-RE relocations Yonghong Song
2026-11-18 16:10 ` [PATCH bpf-next 2/2] selftests/bpf: Test CO-RE relocations of unsigned 32-bit enum values Yonghong Song
2026-11-18 16:59 ` [PATCH bpf-next 1/2] libbpf: Zero-extend unsigned 32-bit enum values in CO-RE relocations bot+bpf-ci
2026-11-19 1:30 ` Yonghong Song [this message]
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=a4aec2b1-f754-48ed-a7d7-ef07bed43eb8@linux.dev \
--to=yonghong.song@linux.dev \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bot+bpf-ci@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=ihor.solodrai@linux.dev \
--cc=kernel-team@fb.com \
--cc=martin.lau@kernel.org \
--cc=mason@kernel.org \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox