Skip to content

Commit 8b1712f

Browse files
WanLi Niuqmonnet
authored andcommitted
bpftool: Make skeleton C++ compatible with explicit casts
Fix C++ compilation errors in generated skeleton by adding explicit pointer casts and use char * subtraction for offset calculation error: invalid conversion from 'void*' to '<obj_name>*' [-fpermissive] | skel = skel_alloc(sizeof(*skel)); | ~~~~~~~~~~^~~~~~~~~~~~~~~ | | | void* error: arithmetic on pointers to void | skel->ctx.sz = (void *)&skel->links - (void *)skel; | ~~~~~~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~~ error: assigning to 'struct <obj_name>__<ident> *' from incompatible type 'void *' | skel-><ident> = skel_prep_map_data((void *)data, 4096, | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | sizeof(data) - 1); | ~~~~~~~~~~~~~~~~~ error: assigning to 'struct <obj_name>__<ident> *' from incompatible type 'void *' | skel-><ident> = skel_finalize_map_data(&skel->maps.<ident>.initial_value, | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | 4096, PROT_READ | PROT_WRITE, skel->maps.<ident>.map_fd); | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Minimum reproducer: $ cat test.bpf.c int val; // placed in .bss section #include "vmlinux.h" #include <bpf/bpf_helpers.h> SEC("raw_tracepoint/sched_wakeup_new") int handle(void *ctx) { return 0; } $ cat test.cpp #include <cerrno> extern "C" { #include "test.bpf.skel.h" } $ bpftool btf dump file /sys/kernel/btf/vmlinux format c > vmlinux.h $ clang -g -O2 -target bpf -c test.bpf.c -o test.bpf.o $ bpftool gen skeleton test.bpf.o -L > test.bpf.skel.h $ g++ -c test.cpp -I. Co-developed-by: Menglong Dong <dongml2@chinatelecom.cn> Signed-off-by: WanLi Niu <niuwl1@chinatelecom.cn> Signed-off-by: Menglong Dong <dongml2@chinatelecom.cn> Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Link: https://lore.kernel.org/bpf/20260106023123.2928-1-kiraskyler@163.com
1 parent 2839fdc commit 8b1712f

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

src/gen.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -731,10 +731,10 @@ static int gen_trace(struct bpf_object *obj, const char *obj_name, const char *h
731731
{ \n\
732732
struct %1$s *skel; \n\
733733
\n\
734-
skel = skel_alloc(sizeof(*skel)); \n\
734+
skel = (struct %1$s *)skel_alloc(sizeof(*skel)); \n\
735735
if (!skel) \n\
736736
goto cleanup; \n\
737-
skel->ctx.sz = (void *)&skel->links - (void *)skel; \n\
737+
skel->ctx.sz = (char *)&skel->links - (char *)skel; \n\
738738
",
739739
obj_name, opts.data_sz);
740740
bpf_object__for_each_map(map, obj) {
@@ -755,7 +755,7 @@ static int gen_trace(struct bpf_object *obj, const char *obj_name, const char *h
755755
\n\
756756
\"; \n\
757757
\n\
758-
skel->%1$s = skel_prep_map_data((void *)data, %2$zd,\n\
758+
skel->%1$s = (__typeof__(skel->%1$s))skel_prep_map_data((void *)data, %2$zd,\n\
759759
sizeof(data) - 1);\n\
760760
if (!skel->%1$s) \n\
761761
goto cleanup; \n\
@@ -857,7 +857,7 @@ static int gen_trace(struct bpf_object *obj, const char *obj_name, const char *h
857857

858858
codegen("\
859859
\n\
860-
skel->%1$s = skel_finalize_map_data(&skel->maps.%1$s.initial_value, \n\
860+
skel->%1$s = (__typeof__(skel->%1$s))skel_finalize_map_data(&skel->maps.%1$s.initial_value,\n\
861861
%2$zd, %3$s, skel->maps.%1$s.map_fd);\n\
862862
if (!skel->%1$s) \n\
863863
return -ENOMEM; \n\

0 commit comments

Comments
 (0)