On Sun, Sep 29, 2024 at 9:32 AM Alexei Starovoitov <alexei.starovoitov@xxxxxxxxx> wrote: > > On Sun, Sep 29, 2024 at 2:31 AM Eric Long via B4 Relay > <devnull+i.hack3r.moe@xxxxxxxxxx> wrote: > > > > From: Eric Long <i@xxxxxxxxxx> > > > > FUNCs do not have sizes, thus currently btf__resolve_size will fail > > with -EINVAL. Add conditions so that we only update size when the BTF > > object is not function or function prototype. > > > > Signed-off-by: Eric Long <i@xxxxxxxxxx> > > --- > > tools/lib/bpf/linker.c | 23 +++++++++++++---------- > > 1 file changed, 13 insertions(+), 10 deletions(-) > > > > diff --git a/tools/lib/bpf/linker.c b/tools/lib/bpf/linker.c > > index 81dbbdd79a7c65a4b048b85e1dba99cb5f7cb56b..cffb388fa40ef054c2661b8363120f8a4d3c3784 100644 > > --- a/tools/lib/bpf/linker.c > > +++ b/tools/lib/bpf/linker.c > > @@ -2452,17 +2452,20 @@ static int linker_append_btf(struct bpf_linker *linker, struct src_obj *obj) > > __s64 sz; > > > > dst_var = &dst_sec->sec_vars[glob_sym->var_idx]; > > - /* Because underlying BTF type might have > > - * changed, so might its size have changed, so > > - * re-calculate and update it in sec_var. > > - */ > > - sz = btf__resolve_size(linker->btf, glob_sym->underlying_btf_id); > > - if (sz < 0) { > > - pr_warn("global '%s': failed to resolve size of underlying type: %d\n", > > - name, (int)sz); > > - return -EINVAL; > > + t = btf__type_by_id(linker->btf, glob_sym->underlying_btf_id); > > + if (btf_kind(t) != BTF_KIND_FUNC && btf_kind(t) != BTF_KIND_FUNC_PROTO) { > > + /* Because underlying BTF type might have > > + * changed, so might its size have changed, so > > + * re-calculate and update it in sec_var. > > + */ > > + sz = btf__resolve_size(linker->btf, glob_sym->underlying_btf_id); > > + if (sz < 0) { > > + pr_warn("global '%s': failed to resolve size of underlying type: %d\n", > > + name, (int)sz); > > + return -EINVAL; > > + } > > + dst_var->size = sz; > > Looks like a hack to me. > > In the test you're using: > void *bpf_cast_to_kern_ctx(void *obj) __ksym; > > but __weak is missing. > Is that the reason you're hitting this issue? This is the real issue, unfortunately. And the reason we have this size update is due to weak *variables*. When the weak variable is overridden by a "strong" variable or extern is resolved to a concrete variable, its underlying type might change (e.g., FWD to STRUCT, but probably there are other cases). Details are a bit hazy by now. But in any case, this size updating logic should only apply to variables, so filtering out FUNCs is the straightforward solution. It can just be coded a bit more nicely, which I suggested in a separate reply.