Commit 14f1aae1 authored by Andrii Nakryiko's avatar Andrii Nakryiko Committed by Alexei Starovoitov

selftests/bpf: Add global variables linking selftest

Add selftest validating various aspects of statically linking global
variables:
  - correct resolution of extern variables across .bss, .data, and .rodata
    sections;
  - correct handling of weak definitions;
  - correct de-duplication of repeating special externs (.kconfig, .ksyms).
Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Acked-by: default avatarYonghong Song <yhs@fb.com>
Link: https://lore.kernel.org/bpf/20210423181348.1801389-17-andrii@kernel.org
parent f2644fb4
......@@ -309,10 +309,11 @@ endef
SKEL_BLACKLIST := btf__% test_pinning_invalid.c test_sk_assign.c
LINKED_SKELS := test_static_linked.skel.h linked_funcs.skel.h
LINKED_SKELS := test_static_linked.skel.h linked_funcs.skel.h linked_vars.skel.h
test_static_linked.skel.h-deps := test_static_linked1.o test_static_linked2.o
linked_funcs.skel.h-deps := linked_funcs1.o linked_funcs2.o
linked_vars.skel.h-deps := linked_vars1.o linked_vars2.o
LINKED_BPF_SRCS := $(patsubst %.o,%.c,$(foreach skel,$(LINKED_SKELS),$($(skel)-deps)))
......
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2021 Facebook */
#include <test_progs.h>
#include <sys/syscall.h>
#include "linked_vars.skel.h"
void test_linked_vars(void)
{
int err;
struct linked_vars *skel;
skel = linked_vars__open();
if (!ASSERT_OK_PTR(skel, "skel_open"))
return;
skel->bss->input_bss1 = 1000;
skel->bss->input_bss2 = 2000;
skel->bss->input_bss_weak = 3000;
err = linked_vars__load(skel);
if (!ASSERT_OK(err, "skel_load"))
goto cleanup;
err = linked_vars__attach(skel);
if (!ASSERT_OK(err, "skel_attach"))
goto cleanup;
/* trigger */
syscall(SYS_getpgid);
ASSERT_EQ(skel->bss->output_bss1, 1000 + 2000 + 3000, "output_bss1");
ASSERT_EQ(skel->bss->output_bss2, 1000 + 2000 + 3000, "output_bss2");
/* 10 comes from "winner" input_data_weak in first obj file */
ASSERT_EQ(skel->bss->output_data1, 1 + 2 + 10, "output_bss1");
ASSERT_EQ(skel->bss->output_data2, 1 + 2 + 10, "output_bss2");
/* 100 comes from "winner" input_rodata_weak in first obj file */
ASSERT_EQ(skel->bss->output_rodata1, 11 + 22 + 100, "output_weak1");
ASSERT_EQ(skel->bss->output_rodata2, 11 + 22 + 100, "output_weak2");
cleanup:
linked_vars__destroy(skel);
}
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2021 Facebook */
#include "vmlinux.h"
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
extern int LINUX_KERNEL_VERSION __kconfig;
/* this weak extern will be strict due to the other file's strong extern */
extern bool CONFIG_BPF_SYSCALL __kconfig __weak;
extern const void bpf_link_fops __ksym __weak;
int input_bss1;
int input_data1 = 1;
const volatile int input_rodata1 = 11;
int input_bss_weak __weak;
/* these two definitions should win */
int input_data_weak __weak = 10;
const volatile int input_rodata_weak __weak = 100;
extern int input_bss2;
extern int input_data2;
extern const int input_rodata2;
int output_bss1;
int output_data1;
int output_rodata1;
long output_sink1;
static __noinline int get_bss_res(void)
{
/* just make sure all the relocations work against .text as well */
return input_bss1 + input_bss2 + input_bss_weak;
}
SEC("raw_tp/sys_enter")
int BPF_PROG(handler1)
{
output_bss1 = get_bss_res();
output_data1 = input_data1 + input_data2 + input_data_weak;
output_rodata1 = input_rodata1 + input_rodata2 + input_rodata_weak;
/* make sure we actually use above special externs, otherwise compiler
* will optimize them out
*/
output_sink1 = LINUX_KERNEL_VERSION
+ CONFIG_BPF_SYSCALL
+ (long)&bpf_link_fops;
return 0;
}
char LICENSE[] SEC("license") = "GPL";
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2021 Facebook */
#include "vmlinux.h"
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
extern int LINUX_KERNEL_VERSION __kconfig;
/* when an extern is defined as both strong and weak, resulting symbol will be strong */
extern bool CONFIG_BPF_SYSCALL __kconfig;
extern const void __start_BTF __ksym;
int input_bss2;
int input_data2 = 2;
const volatile int input_rodata2 = 22;
int input_bss_weak __weak;
/* these two weak variables should lose */
int input_data_weak __weak = 20;
const volatile int input_rodata_weak __weak = 200;
extern int input_bss1;
extern int input_data1;
extern const int input_rodata1;
int output_bss2;
int output_data2;
int output_rodata2;
int output_sink2;
static __noinline int get_data_res(void)
{
/* just make sure all the relocations work against .text as well */
return input_data1 + input_data2 + input_data_weak;
}
SEC("raw_tp/sys_enter")
int BPF_PROG(handler2)
{
output_bss2 = input_bss1 + input_bss2 + input_bss_weak;
output_data2 = get_data_res();
output_rodata2 = input_rodata1 + input_rodata2 + input_rodata_weak;
/* make sure we actually use above special externs, otherwise compiler
* will optimize them out
*/
output_sink2 = LINUX_KERNEL_VERSION
+ CONFIG_BPF_SYSCALL
+ (long)&__start_BTF;
return 0;
}
char LICENSE[] SEC("license") = "GPL";
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment