For a BPF_CORE_ENUMVAL_VALUE relocation, bpf_core_calc_enumval_relo() reads the value of a 32-bit enumerator from btf_enum->val, which is __s32, so the value is sign-extended to 64 bits even if the enum is unsigned. A value in [0x80000000, 0xffffffff] of an unsigned enum becomes 0xffffffff80000000 or above. Clang emits such a value zero-extended in the ld_imm64 of the relocation, so the check of the original value fails, and the whole object is refused even if the target value is the same: enum dentry_flags { DCACHE_PRIVATE = 0x80000000 }; out = bpf_core_enum_value(enum dentry_flags, DCACHE_PRIVATE); libbpf: prog 'p': relo #0: unexpected insn #0 (LDIMM64) value: got 2147483648, exp 18446744071562067968 -> 18446744071562067968 libbpf: prog 'p': relo #0: failed to patch insn #0: -EINVAL And a target value in that range is patched sign-extended, which is wrong wherever the program uses it as a 64-bit value. The x86 vmlinux has 29 enums with such values, e.g., dentry_flags, init_ns_ino and e820_type. Commit 89e7a42e3030 ("libbpf: Normalize enum values according to signedness") added btf_enum_value(), which extends the value as given by the kflag of the enum, for btf_dump and map fields, but not for CO-RE relocations, whose relo_core.c is also built into the kernel. Use it in bpf_core_calc_enumval_relo() as well, and add it to the kernel's include/linux/btf.h, so that the kernel gets the same fix. Fixes: 23b2a3a8f63a ("libbpf: Add enum64 relocation support") Signed-off-by: Yonghong Song --- include/linux/btf.h | 5 +++++ tools/lib/bpf/relo_core.c | 12 ++++++++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/include/linux/btf.h b/include/linux/btf.h index 87fad4076f0e..ceb27984df14 100644 --- a/include/linux/btf.h +++ b/include/linux/btf.h @@ -525,6 +525,11 @@ static inline struct btf_enum *btf_enum(const struct btf_type *t) return (struct btf_enum *)(t + 1); } +static inline u64 btf_enum_value(const struct btf_type *t, const struct btf_enum *e) +{ + return btf_type_kflag(t) ? (s64)e->val : (u32)e->val; +} + static inline struct btf_enum64 *btf_enum64(const struct btf_type *t) { return (struct btf_enum64 *)(t + 1); diff --git a/tools/lib/bpf/relo_core.c b/tools/lib/bpf/relo_core.c index 2672623a4198..7aa7eb4e4021 100644 --- a/tools/lib/bpf/relo_core.c +++ b/tools/lib/bpf/relo_core.c @@ -861,6 +861,13 @@ static int bpf_core_calc_type_relo(const struct bpf_core_relo *relo, return 0; } +static __u64 bpf_core_enumval(const struct btf_type *t, int idx) +{ + if (btf_is_enum(t)) + return btf_enum_value(t, btf_enum(t) + idx); + return btf_enum64_value(btf_enum64(t) + idx); +} + static int bpf_core_calc_enumval_relo(const struct bpf_core_relo *relo, const struct bpf_core_spec *spec, __u64 *val) @@ -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; default: return -EOPNOTSUPP; -- 2.53.1-Meta