Skip to content

Commit 0fec7a3

Browse files
anakryikoAlexei Starovoitov
authored andcommitted
libbpf: Suppress compiler warning when using SEC() macro with externs
When used on externs SEC() macro will trigger compilation warning about inapplicable `__attribute__((used))`. That's expected for extern declarations, so suppress it with the corresponding _Pragma. Signed-off-by: Andrii Nakryiko <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Acked-by: Yonghong Song <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 5b438f0 commit 0fec7a3

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

tools/lib/bpf/bpf_helpers.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,16 @@
2525
/*
2626
* Helper macro to place programs, maps, license in
2727
* different sections in elf_bpf file. Section names
28-
* are interpreted by elf_bpf loader
28+
* are interpreted by libbpf depending on the context (BPF programs, BPF maps,
29+
* extern variables, etc).
30+
* To allow use of SEC() with externs (e.g., for extern .maps declarations),
31+
* make sure __attribute__((unused)) doesn't trigger compilation warning.
2932
*/
30-
#define SEC(NAME) __attribute__((section(NAME), used))
33+
#define SEC(name) \
34+
_Pragma("GCC diagnostic push") \
35+
_Pragma("GCC diagnostic ignored \"-Wignored-attributes\"") \
36+
__attribute__((section(name), used)) \
37+
_Pragma("GCC diagnostic pop") \
3138

3239
/* Avoid 'linux/stddef.h' definition of '__always_inline'. */
3340
#undef __always_inline

0 commit comments

Comments
 (0)