12211c825SHao Luo // SPDX-License-Identifier: GPL-2.0
22211c825SHao Luo /*
32211c825SHao Luo  * Test weak ksyms.
42211c825SHao Luo  *
52211c825SHao Luo  * Copyright (c) 2021 Google
62211c825SHao Luo  */
72211c825SHao Luo 
82211c825SHao Luo #include "vmlinux.h"
92211c825SHao Luo 
102211c825SHao Luo #include <bpf/bpf_helpers.h>
112211c825SHao Luo 
122211c825SHao Luo int out__existing_typed = -1;
132211c825SHao Luo __u64 out__existing_typeless = -1;
142211c825SHao Luo 
152211c825SHao Luo __u64 out__non_existent_typeless = -1;
162211c825SHao Luo __u64 out__non_existent_typed = -1;
172211c825SHao Luo 
182211c825SHao Luo /* existing weak symbols */
192211c825SHao Luo 
202211c825SHao Luo /* test existing weak symbols can be resolved. */
212211c825SHao Luo extern const struct rq runqueues __ksym __weak; /* typed */
222211c825SHao Luo extern const void bpf_prog_active __ksym __weak; /* typeless */
23*3b2ec214SAlexei Starovoitov struct task_struct *bpf_task_acquire(struct task_struct *p) __ksym __weak;
24*3b2ec214SAlexei Starovoitov void bpf_testmod_test_mod_kfunc(int i) __ksym __weak;
252211c825SHao Luo 
262211c825SHao Luo 
272211c825SHao Luo /* non-existent weak symbols. */
282211c825SHao Luo 
292211c825SHao Luo /* typeless symbols, default to zero. */
302211c825SHao Luo extern const void bpf_link_fops1 __ksym __weak;
312211c825SHao Luo 
322211c825SHao Luo /* typed symbols, default to zero. */
332211c825SHao Luo extern const int bpf_link_fops2 __ksym __weak;
34*3b2ec214SAlexei Starovoitov void invalid_kfunc(void) __ksym __weak;
352211c825SHao Luo 
362211c825SHao Luo SEC("raw_tp/sys_enter")
pass_handler(const void * ctx)372211c825SHao Luo int pass_handler(const void *ctx)
382211c825SHao Luo {
392211c825SHao Luo 	struct rq *rq;
402211c825SHao Luo 
412211c825SHao Luo 	/* tests existing symbols. */
422211c825SHao Luo 	rq = (struct rq *)bpf_per_cpu_ptr(&runqueues, 0);
43bb4a6a92SAlexei Starovoitov 	if (rq && bpf_ksym_exists(&runqueues))
443268f031SAlexei Starovoitov 		out__existing_typed = rq->cpu;
452211c825SHao Luo 	out__existing_typeless = (__u64)&bpf_prog_active;
462211c825SHao Luo 
472211c825SHao Luo 	/* tests non-existent symbols. */
482211c825SHao Luo 	out__non_existent_typeless = (__u64)&bpf_link_fops1;
492211c825SHao Luo 
502211c825SHao Luo 	/* tests non-existent symbols. */
512211c825SHao Luo 	out__non_existent_typed = (__u64)&bpf_link_fops2;
522211c825SHao Luo 
532211c825SHao Luo 	if (&bpf_link_fops2) /* can't happen */
542211c825SHao Luo 		out__non_existent_typed = (__u64)bpf_per_cpu_ptr(&bpf_link_fops2, 0);
552211c825SHao Luo 
56*3b2ec214SAlexei Starovoitov 	if (!bpf_ksym_exists(bpf_task_acquire))
57*3b2ec214SAlexei Starovoitov 		/* dead code won't be seen by the verifier */
58*3b2ec214SAlexei Starovoitov 		bpf_task_acquire(0);
59*3b2ec214SAlexei Starovoitov 
60*3b2ec214SAlexei Starovoitov 	if (!bpf_ksym_exists(bpf_testmod_test_mod_kfunc))
61*3b2ec214SAlexei Starovoitov 		/* dead code won't be seen by the verifier */
62*3b2ec214SAlexei Starovoitov 		bpf_testmod_test_mod_kfunc(0);
63*3b2ec214SAlexei Starovoitov 
64*3b2ec214SAlexei Starovoitov 	if (bpf_ksym_exists(invalid_kfunc))
65*3b2ec214SAlexei Starovoitov 		/* dead code won't be seen by the verifier */
66*3b2ec214SAlexei Starovoitov 		invalid_kfunc();
67*3b2ec214SAlexei Starovoitov 
682211c825SHao Luo 	return 0;
692211c825SHao Luo }
702211c825SHao Luo 
712211c825SHao Luo char _license[] SEC("license") = "GPL";
72