From: Yonghong Song <yonghong.song@linux.dev>
To: bpf@vger.kernel.org
Cc: Alexei Starovoitov <ast@kernel.org>,
Andrii Nakryiko <andrii@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Eduard Zingerman <eddyz87@gmail.com>,
kernel-team@fb.com
Subject: [PATCH bpf-next v2 3/4] libbpf: Accept sign-extended 32-bit enum values in CO-RE relocations
Date: Wed, 18 Nov 2026 21:14:59 -0800 [thread overview]
Message-ID: <20261119051459.1902876-1-yonghong.song@linux.dev> (raw)
In-Reply-To: <20261119051443.1902163-1-yonghong.song@linux.dev>
The value of a 32-bit enum in a BPF_CORE_ENUMVAL_VALUE relocation is in
the low 32 bits of the ld_imm64, and the compiler chooses how it is
extended to 64 bits. Clang zero-extends the value of an unsigned enum
and sign-extends that of a signed one. GCC sign-extends every value,
since its __builtin_preserve_enum_value() returns int:
enum E { V = 0x80000000 };
out = bpf_core_enum_value(enum E, V);
clang: r1 = 0x80000000 ll
gcc: lddw %r1,-2147483648
The previous patch extends the original value as given by the kflag of
the enum, which is the form clang emits. So an object built by GCC,
which was accepted when the value was sign-extended for all 32-bit
enums, is now refused:
libbpf: prog 'p': relo #0: unexpected insn #0 (LDIMM64) value: got
18446744071562067968, exp 2147483648 -> 2147483648
The two forms differ only for an unsigned value in [0x80000000,
0xffffffff]. For such a value, accept the sign-extended form as well.
In that case, also sign-extend the target value, since GCC code uses
the ld_imm64 as the int value widened to 64 bits.
An original value below 0x80000000 is the same in both forms, so the
instruction doesn't show which compiler emitted it. The target value
then gets the extension given by the kflag of the target enum, which is
the form clang emits, as its builtin returns long. A target value that
doesn't fit in 32 bits, e.g., from an enum64 in the target kernel, is
patched unchanged.
Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
---
tools/lib/bpf/relo_core.c | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)
diff --git a/tools/lib/bpf/relo_core.c b/tools/lib/bpf/relo_core.c
index 7aa7eb4e4021..76efee8e876f 100644
--- a/tools/lib/bpf/relo_core.c
+++ b/tools/lib/bpf/relo_core.c
@@ -1032,6 +1032,19 @@ static int insn_bytes_to_bpf_size(__u32 sz)
}
}
+/* A 32-bit enum value is in the low 32 bits of the ld_imm64, extended as
+ * the compiler chooses: clang zero-extends an unsigned value, GCC
+ * sign-extends every value, as its __builtin_preserve_enum_value() returns
+ * int. Check whether the instruction has the sign-extended form of an
+ * unsigned value, so that the target value gets that form too.
+ */
+static bool bpf_core_enumval_is_sext(const struct bpf_core_relo *relo,
+ __u64 imm, __u64 orig_val)
+{
+ return relo->kind == BPF_CORE_ENUMVAL_VALUE && orig_val <= UINT_MAX &&
+ imm != orig_val && imm == (__u64)(__s32)orig_val;
+}
+
/*
* Patch relocatable BPF instruction.
*
@@ -1148,6 +1161,11 @@ int bpf_core_patch_insn(const char *prog_name, struct bpf_insn *insn,
return bpf_core_poison_insn(prog_name, relo_idx, insn, insn_idx);
imm = (__u32)insn[0].imm | ((__u64)insn[1].imm << 32);
+ if (res->validate && bpf_core_enumval_is_sext(relo, imm, orig_val)) {
+ orig_val = imm;
+ if (new_val <= UINT_MAX)
+ new_val = (__s32)new_val;
+ }
if (res->validate && imm != orig_val) {
pr_warn("prog '%s': relo #%d: unexpected insn #%d (LDIMM64) value: got %llu, exp %llu -> %llu\n",
prog_name, relo_idx,
--
2.53.1-Meta
next prev parent reply other threads:[~2026-11-19 5:15 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-19 5:14 [PATCH bpf-next v2 0/4] libbpf: Fix CO-RE relocations of unsigned 32-bit enum values Yonghong Song
2026-11-19 5:14 ` [PATCH bpf-next v2 1/4] libbpf: Zero-extend unsigned 32-bit enum values in CO-RE relocations Yonghong Song
2026-11-19 5:54 ` bot+bpf-ci
2026-11-19 15:55 ` Yonghong Song
2026-11-19 5:14 ` [PATCH bpf-next v2 2/4] selftests/bpf: Test CO-RE relocations of unsigned 32-bit enum values Yonghong Song
2026-11-19 5:54 ` bot+bpf-ci
2026-11-19 15:57 ` Yonghong Song
2026-11-19 5:14 ` Yonghong Song [this message]
2026-11-19 5:21 ` [PATCH bpf-next v2 3/4] libbpf: Accept sign-extended 32-bit enum values in CO-RE relocations sashiko-bot
2026-11-19 5:15 ` [PATCH bpf-next v2 4/4] selftests/bpf: Take unsigned enum values as unsigned int for GCC 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=20261119051459.1902876-1-yonghong.song@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 \
/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.