Skip to content

Commit 8f40a9b

Browse files
AaronDotopsiff
authored andcommitted
btf: Avoid weak external references
commit fc5eb4a upstream. If the BTF code is enabled in the build configuration, the start/stop BTF markers are guaranteed to exist. Only when CONFIG_DEBUG_INFO_BTF=n, the references in btf_parse_vmlinux() will remain unsatisfied, relying on the weak linkage of the external references to avoid breaking the build. Avoid GOT based relocations to these markers in the final executable by dropping the weak attribute and instead, make btf_parse_vmlinux() return ERR_PTR(-ENOENT) directly if CONFIG_DEBUG_INFO_BTF is not enabled to begin with. The compiler will drop any subsequent references to __start_BTF and __stop_BTF in that case, allowing the link to succeed. Note that Clang will notice that taking the address of __start_BTF can no longer yield NULL, so testing for that condition becomes unnecessary. Signed-off-by: Ard Biesheuvel <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Signed-off-by: Juxin Gao <[email protected]> Acked-by: Andrii Nakryiko <[email protected]> Acked-by: Arnd Bergmann <[email protected]> Acked-by: Jiri Olsa <[email protected]> Link: https://lore.kernel.org/bpf/[email protected] Signed-off-by: Binbin Zhou <[email protected]>
1 parent a725701 commit 8f40a9b

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

kernel/bpf/btf.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5579,8 +5579,8 @@ static struct btf *btf_parse(const union bpf_attr *attr, bpfptr_t uattr, u32 uat
55795579
return ERR_PTR(err);
55805580
}
55815581

5582-
extern char __weak __start_BTF[];
5583-
extern char __weak __stop_BTF[];
5582+
extern char __start_BTF[];
5583+
extern char __stop_BTF[];
55845584
extern struct btf *btf_vmlinux;
55855585

55865586
#define BPF_MAP_TYPE(_id, _ops)
@@ -5729,6 +5729,9 @@ struct btf *btf_parse_vmlinux(void)
57295729
struct btf *btf = NULL;
57305730
int err;
57315731

5732+
if (!IS_ENABLED(CONFIG_DEBUG_INFO_BTF))
5733+
return ERR_PTR(-ENOENT);
5734+
57325735
env = kzalloc(sizeof(*env), GFP_KERNEL | __GFP_NOWARN);
57335736
if (!env)
57345737
return ERR_PTR(-ENOMEM);

kernel/bpf/sysfs_btf.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
#include <linux/sysfs.h>
1010

1111
/* See scripts/link-vmlinux.sh, gen_btf() func for details */
12-
extern char __weak __start_BTF[];
13-
extern char __weak __stop_BTF[];
12+
extern char __start_BTF[];
13+
extern char __stop_BTF[];
1414

1515
static ssize_t
1616
btf_vmlinux_read(struct file *file, struct kobject *kobj,
@@ -32,7 +32,7 @@ static int __init btf_vmlinux_init(void)
3232
{
3333
bin_attr_btf_vmlinux.size = __stop_BTF - __start_BTF;
3434

35-
if (!__start_BTF || bin_attr_btf_vmlinux.size == 0)
35+
if (bin_attr_btf_vmlinux.size == 0)
3636
return 0;
3737

3838
btf_kobj = kobject_create_and_add("btf", kernel_kobj);

0 commit comments

Comments
 (0)