btf_loader: handle union forward declaration properly
Andrii Nakryiko
maintainerPATCH v2 1/1 btf_loader: handle union forward declaration properly
Differentiate between struct and union forwards in BTF. BTF type representing forward declaration (BTF_KIND_FWD) encodes whether the forward declaration is for struct or union type by using kflag. So use that kflag to create a proper internal representation of forward declaration.
Tested with btfdiff on vmlinux:
$ PAHOLE=build/pahole ./btfdiff ~/linux-build/default/vmlinux | wc -l 0
Also tested manually:
$ cat test.c struct struct_fwd;
union union_fwd;
struct s {
struct struct_fwd *f1;
union union_fwd *f2;
};int func(struct s *s) {
return !!s->f1 + !!s->f2;
}
$ clang -g -target bpf -c test.c -o test.o
$ bpftool btf dump file test.o | grep fwd_kind
[6] FWD 'struct_fwd' fwd_kind=struct
[8] FWD 'union_fwd' fwd_kind=union
$ build/pahole -F btf test.o
struct s {
struct struct_fwd * f1; /* 0 8 */
union union_fwd * f2; /* 8 8 */ /* size: 16, cachelines: 1, members: 2 */
/* last cacheline: 16 bytes */
};Signed-off-by Andrii Nakryiko <[email protected]>
--- btf_loader.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
btf_loader.c +5 -4
@@ -134,12 +134,13 @@ static struct type *type__new(uint16_t tag, strings_t name, size_t size)return type; } -static struct class *class__new (strings_t name,size_t size) +static struct class *class__new (strings_t name,size_t size,bool is_union) {struct class *class =tag__alloc (sizeof (*class)); +uint32_t tag = is_union ? DW_TAG_union_type : DW_TAG_structure_type;if (class !=NULL ) { -type__init (&class->type, DW_TAG_structure_type, name, size); +type__init (&class->type, tag, name, size);INIT_LIST_HEAD (&class->vtable); }
@@ -228,7 +229,7 @@ static int create_members(struct btf_elf *btfe, const struct btf_type *tp,static int create_new_class (struct btf_elf *btfe,const struct btf_type *tp,uint32_t id) { -struct class *class =class__new (tp->name_off, tp->size); +struct class *class =class__new (tp->name_off, tp->size, false);int member_size =create_members (btfe, tp, &class->type);if (member_size <0 )
@@ -313,7 +314,7 @@ static int create_new_subroutine_type(struct btf_elf *btfe, const struct btf_typstatic int create_new_forward_decl (struct btf_elf *btfe,const struct btf_type *tp,uint32_t id) { -struct class *fwd =class__new (tp->name_off,0 ); +struct class *fwd =class__new (tp->name_off,0 ,btf_kflag (tp));if (fwd ==NULL )return -ENOMEM ;
Signature · 1 line
2.24.1