1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright (c) 2021 Facebook */ 3 4 #include "vmlinux.h" 5 #include <bpf/bpf_helpers.h> 6 #include <bpf/bpf_tracing.h> 7 #include <bpf/bpf_core_read.h> 8 #include "bpf_misc.h" 9 10 /* weak and shared between both files */ 11 const volatile int my_tid __weak; 12 long syscall_id __weak; 13 14 int output_val2; 15 int output_ctx2; 16 int output_weak2; /* should stay zero */ 17 18 /* same "subprog" name in all files, but it's ok because they all are static */ 19 static __noinline int subprog(int x) 20 { 21 /* but different formula */ 22 return x * 2; 23 } 24 25 /* Global functions can't be void */ 26 int set_output_val2(int x) 27 { 28 output_val2 = 2 * x + 2 * subprog(x); 29 return 2 * x; 30 } 31 32 /* This function can't be verified as global, as it assumes raw_tp/sys_enter 33 * context and accesses syscall id (second argument). So we mark it as 34 * __hidden, so that libbpf will mark it as static in the final object file, 35 * right before verifying it in the kernel. 36 * 37 * But we don't mark it as __hidden here, rather at extern site. __hidden is 38 * "contaminating" visibility, so it will get propagated from either extern or 39 * actual definition (including from the losing __weak definition). 40 */ 41 void set_output_ctx2(__u64 *ctx) 42 { 43 output_ctx2 = ctx[1]; /* long id, same as in BPF_PROG below */ 44 } 45 46 /* this weak instance should lose, because it will be processed second */ 47 __weak int set_output_weak(int x) 48 { 49 static volatile int whatever; 50 51 /* make sure we use CO-RE relocations in a weak function, this used to 52 * cause problems for BPF static linker 53 */ 54 whatever = 2 * bpf_core_type_size(struct task_struct); 55 __sink(whatever); 56 57 output_weak2 = x; 58 return 2 * x; 59 } 60 61 extern int set_output_val1(int x); 62 63 /* here we'll force set_output_ctx1() to be __hidden in the final obj file */ 64 __hidden extern void set_output_ctx1(__u64 *ctx); 65 66 void *bpf_cast_to_kern_ctx(void *obj) __ksym; 67 68 SEC("?raw_tp/sys_enter") 69 int BPF_PROG(handler2, struct pt_regs *regs, long id) 70 { 71 static volatile int whatever; 72 73 if (my_tid != (s32)bpf_get_current_pid_tgid() || id != syscall_id) 74 return 0; 75 76 /* make sure we have CO-RE relocations in main program */ 77 whatever = bpf_core_type_size(struct task_struct); 78 __sink(whatever); 79 80 set_output_val1(2000); 81 set_output_ctx1(ctx); /* ctx definition is hidden in BPF_PROG macro */ 82 83 /* keep input value the same across both files to avoid dependency on 84 * handler call order; differentiate by output_weak1 vs output_weak2. 85 */ 86 set_output_weak(42); 87 88 return 0; 89 } 90 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 97 char LICENSE[] SEC("license") = "GPL"; 98