Re: [PATCH v2 bpf-next] bpftool: Make skeleton C++ compatible with explicit casts

From: Yonghong Song
Date: Fri Jan 02 2026 - 14:36:07 EST




On 12/31/25 2:29 AM, WanLi Niu wrote:
From: WanLi Niu <niuwl1@xxxxxxxxxxxxxxx>

Fix C++ compilation errors in generated skeleton by adding explicit
pointer casts and using integer subtraction for offset calculation.

error: invalid conversion from 'void*' to 'trace_bpf*' [-fpermissive]
| skel = skel_alloc(sizeof(*skel));
| ~~~~~~~~~~^~~~~~~~~~~~~~~
| |
| void*

error: invalid use of 'void'
| skel->ctx.sz = (void *)&skel->links - (void *)skel;

Signed-off-by: WanLi Niu <niuwl1@xxxxxxxxxxxxxxx>
Co-developed-by: Menglong Dong <dongml2@xxxxxxxxxxxxxxx>
Signed-off-by: Menglong Dong <dongml2@xxxxxxxxxxxxxxx>

For llvm22, I hacked with core_kern_overflow.lskel.h and has the following
warning/errors:

In file included from prog_tests/core_kern_overflow.cc:4:
/home/yhs/work/bpf-next/tools/testing/selftests/bpf/core_kern_overflow.lskel.h:65:9: error: assigning to
'struct core_kern_overflow_lskel *' from incompatible type 'void *'
65 | skel = skel_alloc(sizeof(*skel));
| ^~~~~~~~~~~~~~~~~~~~~~~~~
/home/yhs/work/bpf-next/tools/testing/selftests/bpf/core_kern_overflow.lskel.h:68:38: error: arithmetic on pointers to void
68 | skel->ctx.sz = (void *)&skel->links - (void *)skel;
| ~~~~~~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~~

Your patch fixed the issue.

But for llvm side, I got the following two more errors:

/home/yhs/work/bpf-next/tools/testing/selftests/bpf/core_kern_overflow.lskel.h:73:15: error: assigning to
'struct core_kern_overflow_lskel__bss *' from incompatible type 'void *'
73 | skel->bss = skel_prep_map_data((void *)data, 4096,
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
74 | sizeof(data) - 1);
| ~~~~~~~~~~~~~~~~~
/home/yhs/work/bpf-next/tools/testing/selftests/bpf/core_kern_overflow.lskel.h:223:14: error: assigning to
'struct core_kern_overflow_lskel__bss *' from incompatible type 'void *'
223 | skel->bss = skel_finalize_map_data(&skel->maps.bss.initial_value,
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
224 | 4096, PROT_READ | PROT_WRITE, skel->maps.bss.map_fd);
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

So these two issues can be fixed similar to above skel_alloc() case.

Could you fix both of them?

---
tools/bpf/bpftool/gen.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/tools/bpf/bpftool/gen.c b/tools/bpf/bpftool/gen.c
index 993c7d9484a4..71446a776130 100644
--- a/tools/bpf/bpftool/gen.c
+++ b/tools/bpf/bpftool/gen.c
@@ -731,10 +731,10 @@ static int gen_trace(struct bpf_object *obj, const char *obj_name, const char *h
{ \n\
struct %1$s *skel; \n\
\n\
- skel = skel_alloc(sizeof(*skel)); \n\
+ skel = (struct %1$s *)skel_alloc(sizeof(*skel)); \n\
if (!skel) \n\
goto cleanup; \n\
- skel->ctx.sz = (void *)&skel->links - (void *)skel; \n\
+ skel->ctx.sz = (__u64)&skel->links - (__u64)skel; \n\
",
obj_name, opts.data_sz);
bpf_object__for_each_map(map, obj) {