1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Test weak ksyms.
4  *
5  * Copyright (c) 2021 Google
6  */
7 
8 #include "vmlinux.h"
9 
10 #include <bpf/bpf_helpers.h>
11 
12 int out__existing_typed = -1;
13 __u64 out__existing_typeless = -1;
14 
15 __u64 out__non_existent_typeless = -1;
16 __u64 out__non_existent_typed = -1;
17 
18 /* existing weak symbols */
19 
20 /* test existing weak symbols can be resolved. */
21 extern const struct rq runqueues __ksym __weak; /* typed */
22 extern const void bpf_prog_active __ksym __weak; /* typeless */
23 
24 
25 /* non-existent weak symbols. */
26 
27 /* typeless symbols, default to zero. */
28 extern const void bpf_link_fops1 __ksym __weak;
29 
30 /* typed symbols, default to zero. */
31 extern const int bpf_link_fops2 __ksym __weak;
32 
33 SEC("raw_tp/sys_enter")
34 int pass_handler(const void *ctx)
35 {
36 	struct rq *rq;
37 
38 	/* tests existing symbols. */
39 	rq = (struct rq *)bpf_per_cpu_ptr(&runqueues, 0);
40 	if (rq)
41 		out__existing_typed = 0;
42 	out__existing_typeless = (__u64)&bpf_prog_active;
43 
44 	/* tests non-existent symbols. */
45 	out__non_existent_typeless = (__u64)&bpf_link_fops1;
46 
47 	/* tests non-existent symbols. */
48 	out__non_existent_typed = (__u64)&bpf_link_fops2;
49 
50 	if (&bpf_link_fops2) /* can't happen */
51 		out__non_existent_typed = (__u64)bpf_per_cpu_ptr(&bpf_link_fops2, 0);
52 
53 	return 0;
54 }
55 
56 char _license[] SEC("license") = "GPL";
57