Skip to content

Commit 32022fd

Browse files
anakryikoborkmann
authored andcommitted
libbpf: Handle GCC noreturn-turned-volatile quirk
Handle a GCC quirk of emitting extra volatile modifier in DWARF (and subsequently preserved in BTF by pahole) for function pointers marked as __attribute__((noreturn)). This was the way to mark such functions before GCC 2.5 added noreturn attribute. Drop such func_proto modifiers, similarly to how it's done for array (also to handle GCC quirk/bug). Such volatile attribute is emitted by GCC only, so existing selftests can't express such test. Simple repro is like this (compiled with GCC + BTF generated by pahole): struct my_struct { void __attribute__((noreturn)) (*fn)(int); }; struct my_struct a; Without this fix, output will be: struct my_struct { voidvolatile (*fn)(int); }; With the fix: struct my_struct { void (*fn)(int); }; Fixes: 351131b ("libbpf: add btf_dump API for BTF-to-C conversion") Reported-by: Jean-Philippe Brucker <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Tested-by: Jean-Philippe Brucker <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 8ca8d4a commit 32022fd

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

tools/lib/bpf/btf_dump.c

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,6 +1137,20 @@ static void btf_dump_emit_mods(struct btf_dump *d, struct id_stack *decl_stack)
11371137
}
11381138
}
11391139

1140+
static void btf_dump_drop_mods(struct btf_dump *d, struct id_stack *decl_stack)
1141+
{
1142+
const struct btf_type *t;
1143+
__u32 id;
1144+
1145+
while (decl_stack->cnt) {
1146+
id = decl_stack->ids[decl_stack->cnt - 1];
1147+
t = btf__type_by_id(d->btf, id);
1148+
if (!btf_is_mod(t))
1149+
return;
1150+
decl_stack->cnt--;
1151+
}
1152+
}
1153+
11401154
static void btf_dump_emit_name(const struct btf_dump *d,
11411155
const char *name, bool last_was_ptr)
11421156
{
@@ -1235,14 +1249,7 @@ static void btf_dump_emit_type_chain(struct btf_dump *d,
12351249
* a const/volatile modifier for array, so we are
12361250
* going to silently skip them here.
12371251
*/
1238-
while (decls->cnt) {
1239-
next_id = decls->ids[decls->cnt - 1];
1240-
next_t = btf__type_by_id(d->btf, next_id);
1241-
if (btf_is_mod(next_t))
1242-
decls->cnt--;
1243-
else
1244-
break;
1245-
}
1252+
btf_dump_drop_mods(d, decls);
12461253

12471254
if (decls->cnt == 0) {
12481255
btf_dump_emit_name(d, fname, last_was_ptr);
@@ -1270,7 +1277,15 @@ static void btf_dump_emit_type_chain(struct btf_dump *d,
12701277
__u16 vlen = btf_vlen(t);
12711278
int i;
12721279

1273-
btf_dump_emit_mods(d, decls);
1280+
/*
1281+
* GCC emits extra volatile qualifier for
1282+
* __attribute__((noreturn)) function pointers. Clang
1283+
* doesn't do it. It's a GCC quirk for backwards
1284+
* compatibility with code written for GCC <2.5. So,
1285+
* similarly to extra qualifiers for array, just drop
1286+
* them, instead of handling them.
1287+
*/
1288+
btf_dump_drop_mods(d, decls);
12741289
if (decls->cnt) {
12751290
btf_dump_printf(d, " (");
12761291
btf_dump_emit_type_chain(d, decls, fname, lvl);

0 commit comments

Comments
 (0)