BPF List
 help / color / mirror / Atom feed
* [PATCH bpf-next] bpf: Cast helper IDs to the helper type in bpf_helper_defs.h
@ 2026-10-20 10:12 Maxim Skokov
  2026-10-21  0:42 ` Alexei Starovoitov
  0 siblings, 1 reply; 4+ messages in thread
From: Maxim Skokov @ 2026-10-20 10:12 UTC (permalink / raw)
  To: bpf; +Cc: ast, daniel, andrii, eddyz87, yonghong.song, Maxim Skokov

bpf_helper_defs.h defines each helper as a function pointer initialized
with a void pointer:

  static long (* const bpf_map_update_elem)(...) = (void *) 2;

Converting void * to a function pointer is a GNU extension in C (gcc
-pedantic: "ISO C forbids initialization between function pointer and
'void *'") and an error in C++, so BPF programs written in C++ can't
include bpf_helpers.h.

Cast to the type of the pointer being defined instead. The name is in
scope in its own initializer, so this works in C and C++:

  static long (* const bpf_map_update_elem)(...) =
	(__typeof__(bpf_map_update_elem)) 2;

The generated code doesn't change.

Assisted-by: LLM
Signed-off-by: Maxim Skokov <skokovmaksimevg@gmail.com>
---
Tested: the generated header compiles with clang for the bpf target as
C, gnu++20 and c++17, and with gcc as C; with gcc -pedantic the 211
function pointer warnings are gone. All 1082 selftests BPF programs
build to byte-identical objects with the old and the new header.

 scripts/bpf_doc.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/scripts/bpf_doc.py b/scripts/bpf_doc.py
index 15d113a1bc1d1..6c18aa09389d8 100755
--- a/scripts/bpf_doc.py
+++ b/scripts/bpf_doc.py
@@ -908,7 +908,7 @@ class PrinterHelpersHeader(Printer):
             comma = ', '
             print(one_arg, end='')
 
-        print(') = (void *) %d;' % helper.enum_val)
+        print(') = (__typeof__(%s)) %d;' % (proto['name'], helper.enum_val))
         print('')
 
 
-- 
2.47.3


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-10-21  5:18 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-10-20 10:12 [PATCH bpf-next] bpf: Cast helper IDs to the helper type in bpf_helper_defs.h Maxim Skokov
2026-10-21  0:42 ` Alexei Starovoitov
2026-10-21  1:34   ` Kumar Kartikeya Dwivedi
2026-10-21  5:17   ` Maxim Skokov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox