On Tue, Feb 15, 2022 at 2:59 PM Mauricio Vásquez <mauricio@xxxxxxxxxx> wrote: > > The last part of the BTFGen algorithm is to create a new BTF object with > all the types that were recorded in the previous steps. > > This function performs two different steps: > 1. Add the types to the new BTF object by using btf__add_type(). Some > special logic around struct and unions is implemented to only add the > members that are really used in the field-based relocations. The type > ID on the new and old BTF objects is stored on a map. > 2. Fix all the type IDs on the new BTF object by using the IDs saved in > the previous step. > > Signed-off-by: Mauricio Vásquez <mauricio@xxxxxxxxxx> > Signed-off-by: Rafael David Tinoco <rafael.tinoco@xxxxxxxxxxx> > Signed-off-by: Lorenzo Fontana <lorenzo.fontana@xxxxxxxxxx> > Signed-off-by: Leonardo Di Donato <leonardo.didonato@xxxxxxxxxx> > --- > tools/bpf/bpftool/gen.c | 100 +++++++++++++++++++++++++++++++++++++++- > 1 file changed, 99 insertions(+), 1 deletion(-) > [...] > + cloned_m = btf_members(cloned_type); > + m = btf_members(type); > + vlen = btf_vlen(cloned_type); > + for (idx_src = 0; idx_src < vlen; idx_src++, cloned_m++, m++) { > + /* add only members that are marked as used */ > + if (cloned_m->name_off != MARKED) > + continue; > + > + name = btf__str_by_offset(info->src_btf, m->name_off); > + err = btf__add_field(btf_new, name, m->type, > + BTF_MEMBER_BIT_OFFSET(m->offset), > + BTF_MEMBER_BITFIELD_SIZE(m->offset)); BTF_MEMBER_BIT_OFFSET() and BTF_MEMBER_BIT_OFFSET() shouldn't be used unconditionally, only if kflag is set. It's better to use btf_member_bit_offset() and btf_member_bitfield_size() helpers here, they handle this transparently. > + if (err < 0) > + goto err_out; > + } > + } else { > + err = btf__add_type(btf_new, info->src_btf, type); > + if (err < 0) > + goto err_out; > + new_id = err; > + } > + > + /* add ID mapping */ > + ids[i] = new_id; > + } > + [...]