diff mbox series

[3/3] btf: Avoid weak external references

Message ID 20240329093356.276289-8-ardb+git@google.com (mailing list archive)
State New
Headers show
Series kbuild: Avoid weak external linkage where possible | expand

Commit Message

Ard Biesheuvel March 29, 2024, 9:34 a.m. UTC
From: Ard Biesheuvel <ardb@kernel.org>

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@kernel.org>
---
 include/asm-generic/vmlinux.lds.h | 2 ++
 kernel/bpf/btf.c                  | 4 ++--
 2 files changed, 4 insertions(+), 2 deletions(-)

Comments

Andrii Nakryiko March 29, 2024, 6:24 p.m. UTC | #1
On Fri, Mar 29, 2024 at 2:35 AM Ard Biesheuvel <ardb+git@google.com> wrote:
>
> From: Ard Biesheuvel <ardb@kernel.org>
>
> 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@kernel.org>
> ---
>  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?

>         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?

Other than that looks good to me:

Acked-by: Andrii Nakryiko <andrii@kernel.org>


>  extern struct btf *btf_vmlinux;
>
>  #define BPF_MAP_TYPE(_id, _ops)
> --
> 2.44.0.478.gd926399ef9-goog
>
>
Ard Biesheuvel April 2, 2024, 7:09 a.m. UTC | #2
On Fri, 29 Mar 2024 at 20:24, Andrii Nakryiko <andrii.nakryiko@gmail.com> wrote:
>
> On Fri, Mar 29, 2024 at 2:35 AM Ard Biesheuvel <ardb+git@google.com> wrote:
> >
> > From: Ard Biesheuvel <ardb@kernel.org>
> >
> > 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@kernel.org>
> > ---
> >  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@kernel.org>
>

Thanks!

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

Patch

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 = .);					\
 	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[];
 extern struct btf *btf_vmlinux;
 
 #define BPF_MAP_TYPE(_id, _ops)