Thread (20 messages) 20 messages, 5 authors, 2022-02-18

Re: [PATCH bpf-next v7 5/7] bpftool: Implement btfgen_get_btf()

flat view

From: Andrii Nakryiko <hidden>
Date: 2022-02-16 18:20:52
Also in: bpf

On Tue, Feb 15, 2022 at 2:59 PM Mauricio Vásquez [off-list ref] 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 <redacted>
Signed-off-by: Rafael David Tinoco <redacted>
Signed-off-by: Lorenzo Fontana <redacted>
Signed-off-by: Leonardo Di Donato <redacted>
---
 tools/bpf/bpftool/gen.c | 100 +++++++++++++++++++++++++++++++++++++++-
 1 file changed, 99 insertions(+), 1 deletion(-)
[...]
quoted hunk ↗ jump to hunk
+                       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.
quoted hunk ↗ jump to hunk
+                               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;
+       }
+
[...]
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help