Skip to content

Commit 1d943a2

Browse files
committed
Merge branch 'bpf-static-linker-fix-linking-duplicate-extern-functions'
Eric Long via says: ==================== BPF static linker: fix linking duplicate extern functions Currently, if `bpftool gen object` tries to link two objects that contains the same extern function prototype, libbpf will try to get their (non-existent) size by calling bpf__resolve_size like extern variables and fail with: libbpf: global 'whatever': failed to resolve size of underlying type: -22 This should not be the case, and this series adds conditions to update size only when the BTF kind is not function. Fixes: a463492 ("libbpf: Add linker extern resolution support for functions and global variables") Signed-off-by: Eric Long <i@hack3r.moe> --- Changes in v4: - Remove redundant FUNC_PROTO check. - Merge tests into linked_funcs. - Link to v3: https://lore.kernel.org/r/20241001-libbpf-dup-extern-funcs-v3-0-42f7774efbf3@hack3r.moe Changes in v3: - Simplifiy changes and shorten subjects, according to reviews. - Remove unused includes in selftests. - Link to v2: https://lore.kernel.org/r/20240929-libbpf-dup-extern-funcs-v2-0-0cc81de3f79f@hack3r.moe Changes in v2: - Fix compile errors. Oops! - Link to v1: https://lore.kernel.org/r/20240929-libbpf-dup-extern-funcs-v1-0-df15fbd6525b@hack3r.moe --- ==================== Link: https://lore.kernel.org/r/20241002-libbpf-dup-extern-funcs-v4-0-560eb460ff90@hack3r.moe Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
2 parents c50fc1c + 3c591de commit 1d943a2

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

tools/lib/bpf/linker.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2451,6 +2451,10 @@ static int linker_append_btf(struct bpf_linker *linker, struct src_obj *obj)
24512451
if (glob_sym && glob_sym->var_idx >= 0) {
24522452
__s64 sz;
24532453

2454+
/* FUNCs don't have size, nothing to update */
2455+
if (btf_is_func(t))
2456+
continue;
2457+
24542458
dst_var = &dst_sec->sec_vars[glob_sym->var_idx];
24552459
/* Because underlying BTF type might have
24562460
* changed, so might its size have changed, so

tools/testing/selftests/bpf/progs/linked_funcs1.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ extern int set_output_val2(int x);
6363
/* here we'll force set_output_ctx2() to be __hidden in the final obj file */
6464
__hidden extern void set_output_ctx2(__u64 *ctx);
6565

66+
void *bpf_cast_to_kern_ctx(void *obj) __ksym;
67+
6668
SEC("?raw_tp/sys_enter")
6769
int BPF_PROG(handler1, struct pt_regs *regs, long id)
6870
{
@@ -86,4 +88,10 @@ int BPF_PROG(handler1, struct pt_regs *regs, long id)
8688
return 0;
8789
}
8890

91+
/* Generate BTF FUNC record and test linking with duplicate extern functions */
92+
void kfunc_gen1(void)
93+
{
94+
bpf_cast_to_kern_ctx(0);
95+
}
96+
8997
char LICENSE[] SEC("license") = "GPL";

tools/testing/selftests/bpf/progs/linked_funcs2.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ extern int set_output_val1(int x);
6363
/* here we'll force set_output_ctx1() to be __hidden in the final obj file */
6464
__hidden extern void set_output_ctx1(__u64 *ctx);
6565

66+
void *bpf_cast_to_kern_ctx(void *obj) __ksym;
67+
6668
SEC("?raw_tp/sys_enter")
6769
int BPF_PROG(handler2, struct pt_regs *regs, long id)
6870
{
@@ -86,4 +88,10 @@ int BPF_PROG(handler2, struct pt_regs *regs, long id)
8688
return 0;
8789
}
8890

91+
/* Generate BTF FUNC record and test linking with duplicate extern functions */
92+
void kfunc_gen2(void)
93+
{
94+
bpf_cast_to_kern_ctx(0);
95+
}
96+
8997
char LICENSE[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)