Skip to content

Commit

Permalink
libbpf: Skip forward declaration when counting duplicated type names
Browse files Browse the repository at this point in the history
Currently if a declaration appears in the BTF before the definition, the
definition is dumped as a conflicting name, eg:

    $ bpftool btf dump file vmlinux format raw | grep "'unix_sock'"
    [81287] FWD 'unix_sock' fwd_kind=struct
    [89336] STRUCT 'unix_sock' size=1024 vlen=14

    $ bpftool btf dump file vmlinux format c | grep "struct unix_sock"
    struct unix_sock;
    struct unix_sock___2 {	<--- conflict, the "___2" is unexpected
		    struct unix_sock___2 *unix_sk;

This causes a compilation error if the dump output is used as a header
file.

Fix it by skipping declaration when counting duplicated type names.

Fixes: 351131b ("libbpf: add btf_dump API for BTF-to-C conversion")
Signed-off-by: Xu Kuohai <[email protected]>
Acked-by: Song Liu <[email protected]>
  • Loading branch information
Xu Kuohai authored and Nobody committed Feb 28, 2022
1 parent 6a3b4d7 commit f02e713
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions tools/lib/bpf/btf_dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -1505,6 +1505,11 @@ static const char *btf_dump_resolve_name(struct btf_dump *d, __u32 id,
if (s->name_resolved)
return *cached_name ? *cached_name : orig_name;

if (btf_is_fwd(t) || (btf_is_enum(t) && btf_vlen(t) == 0)) {
s->name_resolved = 1;
return orig_name;
}

dup_cnt = btf_dump_name_dups(d, name_map, orig_name);
if (dup_cnt > 1) {
const size_t max_len = 256;
Expand Down

0 comments on commit f02e713

Please sign in to comment.