btf_dump prints decl tags, so bpftool can annotate types in the C dump by adding decl tags to the BTF it dumps. Adding them to the input BTF would make it modifiable, which libbpf refuses for BTF with unknown header-extension bytes, such as BTF from a newer kernel. Dump C through an empty split BTF over the input instead, so that the annotations can go there. btf__new_empty_split() does not inherit the base pointer size, so set it explicitly. No functional change. Signed-off-by: Ihor Solodrai --- Later in this series bpftool adds decl tags to the split, for record attributes and type tag spellings. --- tools/bpf/bpftool/btf.c | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/tools/bpf/bpftool/btf.c b/tools/bpf/bpftool/btf.c index 385521e6b028..a438b5a03f5a 100644 --- a/tools/bpf/bpftool/btf.c +++ b/tools/bpf/bpftool/btf.c @@ -1092,17 +1092,30 @@ static struct sort_datum *sort_btf_c(const struct btf *btf) return datums; } -static int dump_btf_c(const struct btf *btf, +static int dump_btf_c(struct btf *btf, __u32 *root_type_ids, int root_type_cnt, bool sort_dump) { struct sort_datum *datums = NULL; - struct btf_dump *d; + struct btf_dump *d = NULL; + struct btf *split; + size_t ptr_sz; int err = 0, i; - d = btf_dump__new(btf, btf_dump_printf, NULL, NULL); - if (!d) + split = btf__new_empty_split(btf); + if (!split) return -errno; + /* btf__new_empty_split() does not inherit the base pointer size */ + ptr_sz = btf__pointer_size(btf); + if (ptr_sz) + btf__set_pointer_size(split, ptr_sz); + + d = btf_dump__new(split, btf_dump_printf, NULL, NULL); + if (!d) { + err = -errno; + goto done; + } + printf("#ifndef __VMLINUX_H__\n"); printf("#define __VMLINUX_H__\n"); printf("\n"); @@ -1130,10 +1143,10 @@ static int dump_btf_c(const struct btf *btf, goto done; } } else { - int cnt = btf__type_cnt(btf); + int cnt = btf__type_cnt(split); if (sort_dump) - datums = sort_btf_c(btf); + datums = sort_btf_c(split); for (i = 0; i < cnt; i++) { int idx = datums ? datums[i].index : i; @@ -1146,7 +1159,7 @@ static int dump_btf_c(const struct btf *btf, goto done; } - err = dump_btf_kfuncs(d, btf); + err = dump_btf_kfuncs(d, split); if (err) goto done; } @@ -1160,6 +1173,7 @@ static int dump_btf_c(const struct btf *btf, done: free(datums); btf_dump__free(d); + btf__free(split); return err; } -- 2.56.0