Re: [PATCH 3/3] btf: Avoid weak external references

From: Ard Biesheuvel
Date: Tue Apr 02 2024 - 03:09:59 EST


On Fri, 29 Mar 2024 at 20:24, Andrii Nakryiko <andrii.nakryiko@xxxxxxxxx> wrote:
>
> On Fri, Mar 29, 2024 at 2:35 AM Ard Biesheuvel <ardb+git@xxxxxxxxxx> wrote:
> >
> > From: Ard Biesheuvel <ardb@xxxxxxxxxx>
> >
> > If the BTF code is enabled in the build configuration, the start/stop
> > BTF markers are guaranteed to exist in the final link but not during the
> > first linker pass.
> >
> > Avoid GOT based relocations to these markers in the final executable by
> > providing preliminary definitions that will be used by the first linker
> > pass, and superseded by the actual definitions in the subsequent ones.
> >
> > Signed-off-by: Ard Biesheuvel <ardb@xxxxxxxxxx>
> > ---
> > include/asm-generic/vmlinux.lds.h | 2 ++
> > kernel/bpf/btf.c | 4 ++--
> > 2 files changed, 4 insertions(+), 2 deletions(-)
> >
> > diff --git a/include/asm-generic/vmlinux.lds.h b/include/asm-generic/vmlinux.lds.h
> > index e8449be62058..141bddb511ee 100644
> > --- a/include/asm-generic/vmlinux.lds.h
> > +++ b/include/asm-generic/vmlinux.lds.h
> > @@ -456,6 +456,8 @@
> > * independent code.
> > */
> > #define PRELIMINARY_SYMBOL_DEFINITIONS \
> > + PROVIDE(__start_BTF = .); \
> > + PROVIDE(__stop_BTF = .); \
>
> should this be guarded by CONFIG_DEBUG_INFO_BTF condition?
>

That shouldn't matter - the linker will not create the symbol unless
an unsatisfied reference to it exists anywhere in the input.

> > PROVIDE(kallsyms_addresses = .); \
> > PROVIDE(kallsyms_offsets = .); \
> > PROVIDE(kallsyms_names = .); \
> > diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
> > index 90c4a32d89ff..46a56bf067a8 100644
> > --- a/kernel/bpf/btf.c
> > +++ b/kernel/bpf/btf.c
> > @@ -5642,8 +5642,8 @@ static struct btf *btf_parse(const union bpf_attr *attr, bpfptr_t uattr, u32 uat
> > return ERR_PTR(err);
> > }
> >
> > -extern char __weak __start_BTF[];
> > -extern char __weak __stop_BTF[];
> > +extern char __start_BTF[];
> > +extern char __stop_BTF[];
>
> kernel/bpf/sysfs_btf.c also defines __weak externs for these symbols,
> you probably need to adjust that as well?
>

Yes, thanks for pointing that out.

> Other than that looks good to me:
>
> Acked-by: Andrii Nakryiko <andrii@xxxxxxxxxx>
>

Thanks!

>
> > extern struct btf *btf_vmlinux;
> >
> > #define BPF_MAP_TYPE(_id, _ops)
> > --
> > 2.44.0.478.gd926399ef9-goog
> >
> >