1*f7c14bbaSAndroid Build Coastguard Worker // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2*f7c14bbaSAndroid Build Coastguard Worker
3*f7c14bbaSAndroid Build Coastguard Worker /*
4*f7c14bbaSAndroid Build Coastguard Worker * Common eBPF ELF object loading operations.
5*f7c14bbaSAndroid Build Coastguard Worker *
6*f7c14bbaSAndroid Build Coastguard Worker * Copyright (C) 2013-2015 Alexei Starovoitov <[email protected]>
7*f7c14bbaSAndroid Build Coastguard Worker * Copyright (C) 2015 Wang Nan <[email protected]>
8*f7c14bbaSAndroid Build Coastguard Worker * Copyright (C) 2015 Huawei Inc.
9*f7c14bbaSAndroid Build Coastguard Worker * Copyright (C) 2017 Nicira, Inc.
10*f7c14bbaSAndroid Build Coastguard Worker * Copyright (C) 2019 Isovalent, Inc.
11*f7c14bbaSAndroid Build Coastguard Worker */
12*f7c14bbaSAndroid Build Coastguard Worker
13*f7c14bbaSAndroid Build Coastguard Worker #ifndef _GNU_SOURCE
14*f7c14bbaSAndroid Build Coastguard Worker #define _GNU_SOURCE
15*f7c14bbaSAndroid Build Coastguard Worker #endif
16*f7c14bbaSAndroid Build Coastguard Worker #include <stdlib.h>
17*f7c14bbaSAndroid Build Coastguard Worker #include <stdio.h>
18*f7c14bbaSAndroid Build Coastguard Worker #include <stdarg.h>
19*f7c14bbaSAndroid Build Coastguard Worker #include <libgen.h>
20*f7c14bbaSAndroid Build Coastguard Worker #include <inttypes.h>
21*f7c14bbaSAndroid Build Coastguard Worker #include <limits.h>
22*f7c14bbaSAndroid Build Coastguard Worker #include <string.h>
23*f7c14bbaSAndroid Build Coastguard Worker #include <unistd.h>
24*f7c14bbaSAndroid Build Coastguard Worker #include <endian.h>
25*f7c14bbaSAndroid Build Coastguard Worker #include <fcntl.h>
26*f7c14bbaSAndroid Build Coastguard Worker #include <errno.h>
27*f7c14bbaSAndroid Build Coastguard Worker #include <ctype.h>
28*f7c14bbaSAndroid Build Coastguard Worker #include <asm/unistd.h>
29*f7c14bbaSAndroid Build Coastguard Worker #include <linux/err.h>
30*f7c14bbaSAndroid Build Coastguard Worker #include <linux/kernel.h>
31*f7c14bbaSAndroid Build Coastguard Worker #include <linux/bpf.h>
32*f7c14bbaSAndroid Build Coastguard Worker #include <linux/btf.h>
33*f7c14bbaSAndroid Build Coastguard Worker #include <linux/filter.h>
34*f7c14bbaSAndroid Build Coastguard Worker #include <linux/limits.h>
35*f7c14bbaSAndroid Build Coastguard Worker #include <linux/perf_event.h>
36*f7c14bbaSAndroid Build Coastguard Worker #include <linux/bpf_perf_event.h>
37*f7c14bbaSAndroid Build Coastguard Worker #include <linux/ring_buffer.h>
38*f7c14bbaSAndroid Build Coastguard Worker #include <sys/epoll.h>
39*f7c14bbaSAndroid Build Coastguard Worker #include <sys/ioctl.h>
40*f7c14bbaSAndroid Build Coastguard Worker #include <sys/mman.h>
41*f7c14bbaSAndroid Build Coastguard Worker #include <sys/stat.h>
42*f7c14bbaSAndroid Build Coastguard Worker #include <sys/types.h>
43*f7c14bbaSAndroid Build Coastguard Worker #include <sys/vfs.h>
44*f7c14bbaSAndroid Build Coastguard Worker #include <sys/utsname.h>
45*f7c14bbaSAndroid Build Coastguard Worker #include <sys/resource.h>
46*f7c14bbaSAndroid Build Coastguard Worker #include <libelf.h>
47*f7c14bbaSAndroid Build Coastguard Worker #include <gelf.h>
48*f7c14bbaSAndroid Build Coastguard Worker #include <zlib.h>
49*f7c14bbaSAndroid Build Coastguard Worker
50*f7c14bbaSAndroid Build Coastguard Worker #include "libbpf.h"
51*f7c14bbaSAndroid Build Coastguard Worker #include "bpf.h"
52*f7c14bbaSAndroid Build Coastguard Worker #include "btf.h"
53*f7c14bbaSAndroid Build Coastguard Worker #include "str_error.h"
54*f7c14bbaSAndroid Build Coastguard Worker #include "libbpf_internal.h"
55*f7c14bbaSAndroid Build Coastguard Worker #include "hashmap.h"
56*f7c14bbaSAndroid Build Coastguard Worker #include "bpf_gen_internal.h"
57*f7c14bbaSAndroid Build Coastguard Worker #include "zip.h"
58*f7c14bbaSAndroid Build Coastguard Worker
59*f7c14bbaSAndroid Build Coastguard Worker #ifndef BPF_FS_MAGIC
60*f7c14bbaSAndroid Build Coastguard Worker #define BPF_FS_MAGIC 0xcafe4a11
61*f7c14bbaSAndroid Build Coastguard Worker #endif
62*f7c14bbaSAndroid Build Coastguard Worker
63*f7c14bbaSAndroid Build Coastguard Worker #define BPF_FS_DEFAULT_PATH "/sys/fs/bpf"
64*f7c14bbaSAndroid Build Coastguard Worker
65*f7c14bbaSAndroid Build Coastguard Worker #define BPF_INSN_SZ (sizeof(struct bpf_insn))
66*f7c14bbaSAndroid Build Coastguard Worker
67*f7c14bbaSAndroid Build Coastguard Worker /* vsprintf() in __base_pr() uses nonliteral format string. It may break
68*f7c14bbaSAndroid Build Coastguard Worker * compilation if user enables corresponding warning. Disable it explicitly.
69*f7c14bbaSAndroid Build Coastguard Worker */
70*f7c14bbaSAndroid Build Coastguard Worker #pragma GCC diagnostic ignored "-Wformat-nonliteral"
71*f7c14bbaSAndroid Build Coastguard Worker
72*f7c14bbaSAndroid Build Coastguard Worker #define __printf(a, b) __attribute__((format(printf, a, b)))
73*f7c14bbaSAndroid Build Coastguard Worker
74*f7c14bbaSAndroid Build Coastguard Worker static struct bpf_map *bpf_object__add_map(struct bpf_object *obj);
75*f7c14bbaSAndroid Build Coastguard Worker static bool prog_is_subprog(const struct bpf_object *obj, const struct bpf_program *prog);
76*f7c14bbaSAndroid Build Coastguard Worker static int map_set_def_max_entries(struct bpf_map *map);
77*f7c14bbaSAndroid Build Coastguard Worker
78*f7c14bbaSAndroid Build Coastguard Worker static const char * const attach_type_name[] = {
79*f7c14bbaSAndroid Build Coastguard Worker [BPF_CGROUP_INET_INGRESS] = "cgroup_inet_ingress",
80*f7c14bbaSAndroid Build Coastguard Worker [BPF_CGROUP_INET_EGRESS] = "cgroup_inet_egress",
81*f7c14bbaSAndroid Build Coastguard Worker [BPF_CGROUP_INET_SOCK_CREATE] = "cgroup_inet_sock_create",
82*f7c14bbaSAndroid Build Coastguard Worker [BPF_CGROUP_INET_SOCK_RELEASE] = "cgroup_inet_sock_release",
83*f7c14bbaSAndroid Build Coastguard Worker [BPF_CGROUP_SOCK_OPS] = "cgroup_sock_ops",
84*f7c14bbaSAndroid Build Coastguard Worker [BPF_CGROUP_DEVICE] = "cgroup_device",
85*f7c14bbaSAndroid Build Coastguard Worker [BPF_CGROUP_INET4_BIND] = "cgroup_inet4_bind",
86*f7c14bbaSAndroid Build Coastguard Worker [BPF_CGROUP_INET6_BIND] = "cgroup_inet6_bind",
87*f7c14bbaSAndroid Build Coastguard Worker [BPF_CGROUP_INET4_CONNECT] = "cgroup_inet4_connect",
88*f7c14bbaSAndroid Build Coastguard Worker [BPF_CGROUP_INET6_CONNECT] = "cgroup_inet6_connect",
89*f7c14bbaSAndroid Build Coastguard Worker [BPF_CGROUP_UNIX_CONNECT] = "cgroup_unix_connect",
90*f7c14bbaSAndroid Build Coastguard Worker [BPF_CGROUP_INET4_POST_BIND] = "cgroup_inet4_post_bind",
91*f7c14bbaSAndroid Build Coastguard Worker [BPF_CGROUP_INET6_POST_BIND] = "cgroup_inet6_post_bind",
92*f7c14bbaSAndroid Build Coastguard Worker [BPF_CGROUP_INET4_GETPEERNAME] = "cgroup_inet4_getpeername",
93*f7c14bbaSAndroid Build Coastguard Worker [BPF_CGROUP_INET6_GETPEERNAME] = "cgroup_inet6_getpeername",
94*f7c14bbaSAndroid Build Coastguard Worker [BPF_CGROUP_UNIX_GETPEERNAME] = "cgroup_unix_getpeername",
95*f7c14bbaSAndroid Build Coastguard Worker [BPF_CGROUP_INET4_GETSOCKNAME] = "cgroup_inet4_getsockname",
96*f7c14bbaSAndroid Build Coastguard Worker [BPF_CGROUP_INET6_GETSOCKNAME] = "cgroup_inet6_getsockname",
97*f7c14bbaSAndroid Build Coastguard Worker [BPF_CGROUP_UNIX_GETSOCKNAME] = "cgroup_unix_getsockname",
98*f7c14bbaSAndroid Build Coastguard Worker [BPF_CGROUP_UDP4_SENDMSG] = "cgroup_udp4_sendmsg",
99*f7c14bbaSAndroid Build Coastguard Worker [BPF_CGROUP_UDP6_SENDMSG] = "cgroup_udp6_sendmsg",
100*f7c14bbaSAndroid Build Coastguard Worker [BPF_CGROUP_UNIX_SENDMSG] = "cgroup_unix_sendmsg",
101*f7c14bbaSAndroid Build Coastguard Worker [BPF_CGROUP_SYSCTL] = "cgroup_sysctl",
102*f7c14bbaSAndroid Build Coastguard Worker [BPF_CGROUP_UDP4_RECVMSG] = "cgroup_udp4_recvmsg",
103*f7c14bbaSAndroid Build Coastguard Worker [BPF_CGROUP_UDP6_RECVMSG] = "cgroup_udp6_recvmsg",
104*f7c14bbaSAndroid Build Coastguard Worker [BPF_CGROUP_UNIX_RECVMSG] = "cgroup_unix_recvmsg",
105*f7c14bbaSAndroid Build Coastguard Worker [BPF_CGROUP_GETSOCKOPT] = "cgroup_getsockopt",
106*f7c14bbaSAndroid Build Coastguard Worker [BPF_CGROUP_SETSOCKOPT] = "cgroup_setsockopt",
107*f7c14bbaSAndroid Build Coastguard Worker [BPF_SK_SKB_STREAM_PARSER] = "sk_skb_stream_parser",
108*f7c14bbaSAndroid Build Coastguard Worker [BPF_SK_SKB_STREAM_VERDICT] = "sk_skb_stream_verdict",
109*f7c14bbaSAndroid Build Coastguard Worker [BPF_SK_SKB_VERDICT] = "sk_skb_verdict",
110*f7c14bbaSAndroid Build Coastguard Worker [BPF_SK_MSG_VERDICT] = "sk_msg_verdict",
111*f7c14bbaSAndroid Build Coastguard Worker [BPF_LIRC_MODE2] = "lirc_mode2",
112*f7c14bbaSAndroid Build Coastguard Worker [BPF_FLOW_DISSECTOR] = "flow_dissector",
113*f7c14bbaSAndroid Build Coastguard Worker [BPF_TRACE_RAW_TP] = "trace_raw_tp",
114*f7c14bbaSAndroid Build Coastguard Worker [BPF_TRACE_FENTRY] = "trace_fentry",
115*f7c14bbaSAndroid Build Coastguard Worker [BPF_TRACE_FEXIT] = "trace_fexit",
116*f7c14bbaSAndroid Build Coastguard Worker [BPF_MODIFY_RETURN] = "modify_return",
117*f7c14bbaSAndroid Build Coastguard Worker [BPF_LSM_MAC] = "lsm_mac",
118*f7c14bbaSAndroid Build Coastguard Worker [BPF_LSM_CGROUP] = "lsm_cgroup",
119*f7c14bbaSAndroid Build Coastguard Worker [BPF_SK_LOOKUP] = "sk_lookup",
120*f7c14bbaSAndroid Build Coastguard Worker [BPF_TRACE_ITER] = "trace_iter",
121*f7c14bbaSAndroid Build Coastguard Worker [BPF_XDP_DEVMAP] = "xdp_devmap",
122*f7c14bbaSAndroid Build Coastguard Worker [BPF_XDP_CPUMAP] = "xdp_cpumap",
123*f7c14bbaSAndroid Build Coastguard Worker [BPF_XDP] = "xdp",
124*f7c14bbaSAndroid Build Coastguard Worker [BPF_SK_REUSEPORT_SELECT] = "sk_reuseport_select",
125*f7c14bbaSAndroid Build Coastguard Worker [BPF_SK_REUSEPORT_SELECT_OR_MIGRATE] = "sk_reuseport_select_or_migrate",
126*f7c14bbaSAndroid Build Coastguard Worker [BPF_PERF_EVENT] = "perf_event",
127*f7c14bbaSAndroid Build Coastguard Worker [BPF_TRACE_KPROBE_MULTI] = "trace_kprobe_multi",
128*f7c14bbaSAndroid Build Coastguard Worker [BPF_STRUCT_OPS] = "struct_ops",
129*f7c14bbaSAndroid Build Coastguard Worker [BPF_NETFILTER] = "netfilter",
130*f7c14bbaSAndroid Build Coastguard Worker [BPF_TCX_INGRESS] = "tcx_ingress",
131*f7c14bbaSAndroid Build Coastguard Worker [BPF_TCX_EGRESS] = "tcx_egress",
132*f7c14bbaSAndroid Build Coastguard Worker [BPF_TRACE_UPROBE_MULTI] = "trace_uprobe_multi",
133*f7c14bbaSAndroid Build Coastguard Worker [BPF_NETKIT_PRIMARY] = "netkit_primary",
134*f7c14bbaSAndroid Build Coastguard Worker [BPF_NETKIT_PEER] = "netkit_peer",
135*f7c14bbaSAndroid Build Coastguard Worker };
136*f7c14bbaSAndroid Build Coastguard Worker
137*f7c14bbaSAndroid Build Coastguard Worker static const char * const link_type_name[] = {
138*f7c14bbaSAndroid Build Coastguard Worker [BPF_LINK_TYPE_UNSPEC] = "unspec",
139*f7c14bbaSAndroid Build Coastguard Worker [BPF_LINK_TYPE_RAW_TRACEPOINT] = "raw_tracepoint",
140*f7c14bbaSAndroid Build Coastguard Worker [BPF_LINK_TYPE_TRACING] = "tracing",
141*f7c14bbaSAndroid Build Coastguard Worker [BPF_LINK_TYPE_CGROUP] = "cgroup",
142*f7c14bbaSAndroid Build Coastguard Worker [BPF_LINK_TYPE_ITER] = "iter",
143*f7c14bbaSAndroid Build Coastguard Worker [BPF_LINK_TYPE_NETNS] = "netns",
144*f7c14bbaSAndroid Build Coastguard Worker [BPF_LINK_TYPE_XDP] = "xdp",
145*f7c14bbaSAndroid Build Coastguard Worker [BPF_LINK_TYPE_PERF_EVENT] = "perf_event",
146*f7c14bbaSAndroid Build Coastguard Worker [BPF_LINK_TYPE_KPROBE_MULTI] = "kprobe_multi",
147*f7c14bbaSAndroid Build Coastguard Worker [BPF_LINK_TYPE_STRUCT_OPS] = "struct_ops",
148*f7c14bbaSAndroid Build Coastguard Worker [BPF_LINK_TYPE_NETFILTER] = "netfilter",
149*f7c14bbaSAndroid Build Coastguard Worker [BPF_LINK_TYPE_TCX] = "tcx",
150*f7c14bbaSAndroid Build Coastguard Worker [BPF_LINK_TYPE_UPROBE_MULTI] = "uprobe_multi",
151*f7c14bbaSAndroid Build Coastguard Worker [BPF_LINK_TYPE_NETKIT] = "netkit",
152*f7c14bbaSAndroid Build Coastguard Worker };
153*f7c14bbaSAndroid Build Coastguard Worker
154*f7c14bbaSAndroid Build Coastguard Worker static const char * const map_type_name[] = {
155*f7c14bbaSAndroid Build Coastguard Worker [BPF_MAP_TYPE_UNSPEC] = "unspec",
156*f7c14bbaSAndroid Build Coastguard Worker [BPF_MAP_TYPE_HASH] = "hash",
157*f7c14bbaSAndroid Build Coastguard Worker [BPF_MAP_TYPE_ARRAY] = "array",
158*f7c14bbaSAndroid Build Coastguard Worker [BPF_MAP_TYPE_PROG_ARRAY] = "prog_array",
159*f7c14bbaSAndroid Build Coastguard Worker [BPF_MAP_TYPE_PERF_EVENT_ARRAY] = "perf_event_array",
160*f7c14bbaSAndroid Build Coastguard Worker [BPF_MAP_TYPE_PERCPU_HASH] = "percpu_hash",
161*f7c14bbaSAndroid Build Coastguard Worker [BPF_MAP_TYPE_PERCPU_ARRAY] = "percpu_array",
162*f7c14bbaSAndroid Build Coastguard Worker [BPF_MAP_TYPE_STACK_TRACE] = "stack_trace",
163*f7c14bbaSAndroid Build Coastguard Worker [BPF_MAP_TYPE_CGROUP_ARRAY] = "cgroup_array",
164*f7c14bbaSAndroid Build Coastguard Worker [BPF_MAP_TYPE_LRU_HASH] = "lru_hash",
165*f7c14bbaSAndroid Build Coastguard Worker [BPF_MAP_TYPE_LRU_PERCPU_HASH] = "lru_percpu_hash",
166*f7c14bbaSAndroid Build Coastguard Worker [BPF_MAP_TYPE_LPM_TRIE] = "lpm_trie",
167*f7c14bbaSAndroid Build Coastguard Worker [BPF_MAP_TYPE_ARRAY_OF_MAPS] = "array_of_maps",
168*f7c14bbaSAndroid Build Coastguard Worker [BPF_MAP_TYPE_HASH_OF_MAPS] = "hash_of_maps",
169*f7c14bbaSAndroid Build Coastguard Worker [BPF_MAP_TYPE_DEVMAP] = "devmap",
170*f7c14bbaSAndroid Build Coastguard Worker [BPF_MAP_TYPE_DEVMAP_HASH] = "devmap_hash",
171*f7c14bbaSAndroid Build Coastguard Worker [BPF_MAP_TYPE_SOCKMAP] = "sockmap",
172*f7c14bbaSAndroid Build Coastguard Worker [BPF_MAP_TYPE_CPUMAP] = "cpumap",
173*f7c14bbaSAndroid Build Coastguard Worker [BPF_MAP_TYPE_XSKMAP] = "xskmap",
174*f7c14bbaSAndroid Build Coastguard Worker [BPF_MAP_TYPE_SOCKHASH] = "sockhash",
175*f7c14bbaSAndroid Build Coastguard Worker [BPF_MAP_TYPE_CGROUP_STORAGE] = "cgroup_storage",
176*f7c14bbaSAndroid Build Coastguard Worker [BPF_MAP_TYPE_REUSEPORT_SOCKARRAY] = "reuseport_sockarray",
177*f7c14bbaSAndroid Build Coastguard Worker [BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE] = "percpu_cgroup_storage",
178*f7c14bbaSAndroid Build Coastguard Worker [BPF_MAP_TYPE_QUEUE] = "queue",
179*f7c14bbaSAndroid Build Coastguard Worker [BPF_MAP_TYPE_STACK] = "stack",
180*f7c14bbaSAndroid Build Coastguard Worker [BPF_MAP_TYPE_SK_STORAGE] = "sk_storage",
181*f7c14bbaSAndroid Build Coastguard Worker [BPF_MAP_TYPE_STRUCT_OPS] = "struct_ops",
182*f7c14bbaSAndroid Build Coastguard Worker [BPF_MAP_TYPE_RINGBUF] = "ringbuf",
183*f7c14bbaSAndroid Build Coastguard Worker [BPF_MAP_TYPE_INODE_STORAGE] = "inode_storage",
184*f7c14bbaSAndroid Build Coastguard Worker [BPF_MAP_TYPE_TASK_STORAGE] = "task_storage",
185*f7c14bbaSAndroid Build Coastguard Worker [BPF_MAP_TYPE_BLOOM_FILTER] = "bloom_filter",
186*f7c14bbaSAndroid Build Coastguard Worker [BPF_MAP_TYPE_USER_RINGBUF] = "user_ringbuf",
187*f7c14bbaSAndroid Build Coastguard Worker [BPF_MAP_TYPE_CGRP_STORAGE] = "cgrp_storage",
188*f7c14bbaSAndroid Build Coastguard Worker [BPF_MAP_TYPE_ARENA] = "arena",
189*f7c14bbaSAndroid Build Coastguard Worker };
190*f7c14bbaSAndroid Build Coastguard Worker
191*f7c14bbaSAndroid Build Coastguard Worker static const char * const prog_type_name[] = {
192*f7c14bbaSAndroid Build Coastguard Worker [BPF_PROG_TYPE_UNSPEC] = "unspec",
193*f7c14bbaSAndroid Build Coastguard Worker [BPF_PROG_TYPE_SOCKET_FILTER] = "socket_filter",
194*f7c14bbaSAndroid Build Coastguard Worker [BPF_PROG_TYPE_KPROBE] = "kprobe",
195*f7c14bbaSAndroid Build Coastguard Worker [BPF_PROG_TYPE_SCHED_CLS] = "sched_cls",
196*f7c14bbaSAndroid Build Coastguard Worker [BPF_PROG_TYPE_SCHED_ACT] = "sched_act",
197*f7c14bbaSAndroid Build Coastguard Worker [BPF_PROG_TYPE_TRACEPOINT] = "tracepoint",
198*f7c14bbaSAndroid Build Coastguard Worker [BPF_PROG_TYPE_XDP] = "xdp",
199*f7c14bbaSAndroid Build Coastguard Worker [BPF_PROG_TYPE_PERF_EVENT] = "perf_event",
200*f7c14bbaSAndroid Build Coastguard Worker [BPF_PROG_TYPE_CGROUP_SKB] = "cgroup_skb",
201*f7c14bbaSAndroid Build Coastguard Worker [BPF_PROG_TYPE_CGROUP_SOCK] = "cgroup_sock",
202*f7c14bbaSAndroid Build Coastguard Worker [BPF_PROG_TYPE_LWT_IN] = "lwt_in",
203*f7c14bbaSAndroid Build Coastguard Worker [BPF_PROG_TYPE_LWT_OUT] = "lwt_out",
204*f7c14bbaSAndroid Build Coastguard Worker [BPF_PROG_TYPE_LWT_XMIT] = "lwt_xmit",
205*f7c14bbaSAndroid Build Coastguard Worker [BPF_PROG_TYPE_SOCK_OPS] = "sock_ops",
206*f7c14bbaSAndroid Build Coastguard Worker [BPF_PROG_TYPE_SK_SKB] = "sk_skb",
207*f7c14bbaSAndroid Build Coastguard Worker [BPF_PROG_TYPE_CGROUP_DEVICE] = "cgroup_device",
208*f7c14bbaSAndroid Build Coastguard Worker [BPF_PROG_TYPE_SK_MSG] = "sk_msg",
209*f7c14bbaSAndroid Build Coastguard Worker [BPF_PROG_TYPE_RAW_TRACEPOINT] = "raw_tracepoint",
210*f7c14bbaSAndroid Build Coastguard Worker [BPF_PROG_TYPE_CGROUP_SOCK_ADDR] = "cgroup_sock_addr",
211*f7c14bbaSAndroid Build Coastguard Worker [BPF_PROG_TYPE_LWT_SEG6LOCAL] = "lwt_seg6local",
212*f7c14bbaSAndroid Build Coastguard Worker [BPF_PROG_TYPE_LIRC_MODE2] = "lirc_mode2",
213*f7c14bbaSAndroid Build Coastguard Worker [BPF_PROG_TYPE_SK_REUSEPORT] = "sk_reuseport",
214*f7c14bbaSAndroid Build Coastguard Worker [BPF_PROG_TYPE_FLOW_DISSECTOR] = "flow_dissector",
215*f7c14bbaSAndroid Build Coastguard Worker [BPF_PROG_TYPE_CGROUP_SYSCTL] = "cgroup_sysctl",
216*f7c14bbaSAndroid Build Coastguard Worker [BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE] = "raw_tracepoint_writable",
217*f7c14bbaSAndroid Build Coastguard Worker [BPF_PROG_TYPE_CGROUP_SOCKOPT] = "cgroup_sockopt",
218*f7c14bbaSAndroid Build Coastguard Worker [BPF_PROG_TYPE_TRACING] = "tracing",
219*f7c14bbaSAndroid Build Coastguard Worker [BPF_PROG_TYPE_STRUCT_OPS] = "struct_ops",
220*f7c14bbaSAndroid Build Coastguard Worker [BPF_PROG_TYPE_EXT] = "ext",
221*f7c14bbaSAndroid Build Coastguard Worker [BPF_PROG_TYPE_LSM] = "lsm",
222*f7c14bbaSAndroid Build Coastguard Worker [BPF_PROG_TYPE_SK_LOOKUP] = "sk_lookup",
223*f7c14bbaSAndroid Build Coastguard Worker [BPF_PROG_TYPE_SYSCALL] = "syscall",
224*f7c14bbaSAndroid Build Coastguard Worker [BPF_PROG_TYPE_NETFILTER] = "netfilter",
225*f7c14bbaSAndroid Build Coastguard Worker };
226*f7c14bbaSAndroid Build Coastguard Worker
__base_pr(enum libbpf_print_level level,const char * format,va_list args)227*f7c14bbaSAndroid Build Coastguard Worker static int __base_pr(enum libbpf_print_level level, const char *format,
228*f7c14bbaSAndroid Build Coastguard Worker va_list args)
229*f7c14bbaSAndroid Build Coastguard Worker {
230*f7c14bbaSAndroid Build Coastguard Worker if (level == LIBBPF_DEBUG)
231*f7c14bbaSAndroid Build Coastguard Worker return 0;
232*f7c14bbaSAndroid Build Coastguard Worker
233*f7c14bbaSAndroid Build Coastguard Worker return vfprintf(stderr, format, args);
234*f7c14bbaSAndroid Build Coastguard Worker }
235*f7c14bbaSAndroid Build Coastguard Worker
236*f7c14bbaSAndroid Build Coastguard Worker static libbpf_print_fn_t __libbpf_pr = __base_pr;
237*f7c14bbaSAndroid Build Coastguard Worker
libbpf_set_print(libbpf_print_fn_t fn)238*f7c14bbaSAndroid Build Coastguard Worker libbpf_print_fn_t libbpf_set_print(libbpf_print_fn_t fn)
239*f7c14bbaSAndroid Build Coastguard Worker {
240*f7c14bbaSAndroid Build Coastguard Worker libbpf_print_fn_t old_print_fn;
241*f7c14bbaSAndroid Build Coastguard Worker
242*f7c14bbaSAndroid Build Coastguard Worker old_print_fn = __atomic_exchange_n(&__libbpf_pr, fn, __ATOMIC_RELAXED);
243*f7c14bbaSAndroid Build Coastguard Worker
244*f7c14bbaSAndroid Build Coastguard Worker return old_print_fn;
245*f7c14bbaSAndroid Build Coastguard Worker }
246*f7c14bbaSAndroid Build Coastguard Worker
247*f7c14bbaSAndroid Build Coastguard Worker __printf(2, 3)
libbpf_print(enum libbpf_print_level level,const char * format,...)248*f7c14bbaSAndroid Build Coastguard Worker void libbpf_print(enum libbpf_print_level level, const char *format, ...)
249*f7c14bbaSAndroid Build Coastguard Worker {
250*f7c14bbaSAndroid Build Coastguard Worker va_list args;
251*f7c14bbaSAndroid Build Coastguard Worker int old_errno;
252*f7c14bbaSAndroid Build Coastguard Worker libbpf_print_fn_t print_fn;
253*f7c14bbaSAndroid Build Coastguard Worker
254*f7c14bbaSAndroid Build Coastguard Worker print_fn = __atomic_load_n(&__libbpf_pr, __ATOMIC_RELAXED);
255*f7c14bbaSAndroid Build Coastguard Worker if (!print_fn)
256*f7c14bbaSAndroid Build Coastguard Worker return;
257*f7c14bbaSAndroid Build Coastguard Worker
258*f7c14bbaSAndroid Build Coastguard Worker old_errno = errno;
259*f7c14bbaSAndroid Build Coastguard Worker
260*f7c14bbaSAndroid Build Coastguard Worker va_start(args, format);
261*f7c14bbaSAndroid Build Coastguard Worker __libbpf_pr(level, format, args);
262*f7c14bbaSAndroid Build Coastguard Worker va_end(args);
263*f7c14bbaSAndroid Build Coastguard Worker
264*f7c14bbaSAndroid Build Coastguard Worker errno = old_errno;
265*f7c14bbaSAndroid Build Coastguard Worker }
266*f7c14bbaSAndroid Build Coastguard Worker
pr_perm_msg(int err)267*f7c14bbaSAndroid Build Coastguard Worker static void pr_perm_msg(int err)
268*f7c14bbaSAndroid Build Coastguard Worker {
269*f7c14bbaSAndroid Build Coastguard Worker struct rlimit limit;
270*f7c14bbaSAndroid Build Coastguard Worker char buf[100];
271*f7c14bbaSAndroid Build Coastguard Worker
272*f7c14bbaSAndroid Build Coastguard Worker if (err != -EPERM || geteuid() != 0)
273*f7c14bbaSAndroid Build Coastguard Worker return;
274*f7c14bbaSAndroid Build Coastguard Worker
275*f7c14bbaSAndroid Build Coastguard Worker err = getrlimit(RLIMIT_MEMLOCK, &limit);
276*f7c14bbaSAndroid Build Coastguard Worker if (err)
277*f7c14bbaSAndroid Build Coastguard Worker return;
278*f7c14bbaSAndroid Build Coastguard Worker
279*f7c14bbaSAndroid Build Coastguard Worker if (limit.rlim_cur == RLIM_INFINITY)
280*f7c14bbaSAndroid Build Coastguard Worker return;
281*f7c14bbaSAndroid Build Coastguard Worker
282*f7c14bbaSAndroid Build Coastguard Worker if (limit.rlim_cur < 1024)
283*f7c14bbaSAndroid Build Coastguard Worker snprintf(buf, sizeof(buf), "%zu bytes", (size_t)limit.rlim_cur);
284*f7c14bbaSAndroid Build Coastguard Worker else if (limit.rlim_cur < 1024*1024)
285*f7c14bbaSAndroid Build Coastguard Worker snprintf(buf, sizeof(buf), "%.1f KiB", (double)limit.rlim_cur / 1024);
286*f7c14bbaSAndroid Build Coastguard Worker else
287*f7c14bbaSAndroid Build Coastguard Worker snprintf(buf, sizeof(buf), "%.1f MiB", (double)limit.rlim_cur / (1024*1024));
288*f7c14bbaSAndroid Build Coastguard Worker
289*f7c14bbaSAndroid Build Coastguard Worker pr_warn("permission error while running as root; try raising 'ulimit -l'? current value: %s\n",
290*f7c14bbaSAndroid Build Coastguard Worker buf);
291*f7c14bbaSAndroid Build Coastguard Worker }
292*f7c14bbaSAndroid Build Coastguard Worker
293*f7c14bbaSAndroid Build Coastguard Worker #define STRERR_BUFSIZE 128
294*f7c14bbaSAndroid Build Coastguard Worker
295*f7c14bbaSAndroid Build Coastguard Worker /* Copied from tools/perf/util/util.h */
296*f7c14bbaSAndroid Build Coastguard Worker #ifndef zfree
297*f7c14bbaSAndroid Build Coastguard Worker # define zfree(ptr) ({ free(*ptr); *ptr = NULL; })
298*f7c14bbaSAndroid Build Coastguard Worker #endif
299*f7c14bbaSAndroid Build Coastguard Worker
300*f7c14bbaSAndroid Build Coastguard Worker #ifndef zclose
301*f7c14bbaSAndroid Build Coastguard Worker # define zclose(fd) ({ \
302*f7c14bbaSAndroid Build Coastguard Worker int ___err = 0; \
303*f7c14bbaSAndroid Build Coastguard Worker if ((fd) >= 0) \
304*f7c14bbaSAndroid Build Coastguard Worker ___err = close((fd)); \
305*f7c14bbaSAndroid Build Coastguard Worker fd = -1; \
306*f7c14bbaSAndroid Build Coastguard Worker ___err; })
307*f7c14bbaSAndroid Build Coastguard Worker #endif
308*f7c14bbaSAndroid Build Coastguard Worker
ptr_to_u64(const void * ptr)309*f7c14bbaSAndroid Build Coastguard Worker static inline __u64 ptr_to_u64(const void *ptr)
310*f7c14bbaSAndroid Build Coastguard Worker {
311*f7c14bbaSAndroid Build Coastguard Worker return (__u64) (unsigned long) ptr;
312*f7c14bbaSAndroid Build Coastguard Worker }
313*f7c14bbaSAndroid Build Coastguard Worker
libbpf_set_strict_mode(enum libbpf_strict_mode mode)314*f7c14bbaSAndroid Build Coastguard Worker int libbpf_set_strict_mode(enum libbpf_strict_mode mode)
315*f7c14bbaSAndroid Build Coastguard Worker {
316*f7c14bbaSAndroid Build Coastguard Worker /* as of v1.0 libbpf_set_strict_mode() is a no-op */
317*f7c14bbaSAndroid Build Coastguard Worker return 0;
318*f7c14bbaSAndroid Build Coastguard Worker }
319*f7c14bbaSAndroid Build Coastguard Worker
libbpf_major_version(void)320*f7c14bbaSAndroid Build Coastguard Worker __u32 libbpf_major_version(void)
321*f7c14bbaSAndroid Build Coastguard Worker {
322*f7c14bbaSAndroid Build Coastguard Worker return LIBBPF_MAJOR_VERSION;
323*f7c14bbaSAndroid Build Coastguard Worker }
324*f7c14bbaSAndroid Build Coastguard Worker
libbpf_minor_version(void)325*f7c14bbaSAndroid Build Coastguard Worker __u32 libbpf_minor_version(void)
326*f7c14bbaSAndroid Build Coastguard Worker {
327*f7c14bbaSAndroid Build Coastguard Worker return LIBBPF_MINOR_VERSION;
328*f7c14bbaSAndroid Build Coastguard Worker }
329*f7c14bbaSAndroid Build Coastguard Worker
libbpf_version_string(void)330*f7c14bbaSAndroid Build Coastguard Worker const char *libbpf_version_string(void)
331*f7c14bbaSAndroid Build Coastguard Worker {
332*f7c14bbaSAndroid Build Coastguard Worker #define __S(X) #X
333*f7c14bbaSAndroid Build Coastguard Worker #define _S(X) __S(X)
334*f7c14bbaSAndroid Build Coastguard Worker return "v" _S(LIBBPF_MAJOR_VERSION) "." _S(LIBBPF_MINOR_VERSION);
335*f7c14bbaSAndroid Build Coastguard Worker #undef _S
336*f7c14bbaSAndroid Build Coastguard Worker #undef __S
337*f7c14bbaSAndroid Build Coastguard Worker }
338*f7c14bbaSAndroid Build Coastguard Worker
339*f7c14bbaSAndroid Build Coastguard Worker enum reloc_type {
340*f7c14bbaSAndroid Build Coastguard Worker RELO_LD64,
341*f7c14bbaSAndroid Build Coastguard Worker RELO_CALL,
342*f7c14bbaSAndroid Build Coastguard Worker RELO_DATA,
343*f7c14bbaSAndroid Build Coastguard Worker RELO_EXTERN_LD64,
344*f7c14bbaSAndroid Build Coastguard Worker RELO_EXTERN_CALL,
345*f7c14bbaSAndroid Build Coastguard Worker RELO_SUBPROG_ADDR,
346*f7c14bbaSAndroid Build Coastguard Worker RELO_CORE,
347*f7c14bbaSAndroid Build Coastguard Worker };
348*f7c14bbaSAndroid Build Coastguard Worker
349*f7c14bbaSAndroid Build Coastguard Worker struct reloc_desc {
350*f7c14bbaSAndroid Build Coastguard Worker enum reloc_type type;
351*f7c14bbaSAndroid Build Coastguard Worker int insn_idx;
352*f7c14bbaSAndroid Build Coastguard Worker union {
353*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_core_relo *core_relo; /* used when type == RELO_CORE */
354*f7c14bbaSAndroid Build Coastguard Worker struct {
355*f7c14bbaSAndroid Build Coastguard Worker int map_idx;
356*f7c14bbaSAndroid Build Coastguard Worker int sym_off;
357*f7c14bbaSAndroid Build Coastguard Worker int ext_idx;
358*f7c14bbaSAndroid Build Coastguard Worker };
359*f7c14bbaSAndroid Build Coastguard Worker };
360*f7c14bbaSAndroid Build Coastguard Worker };
361*f7c14bbaSAndroid Build Coastguard Worker
362*f7c14bbaSAndroid Build Coastguard Worker /* stored as sec_def->cookie for all libbpf-supported SEC()s */
363*f7c14bbaSAndroid Build Coastguard Worker enum sec_def_flags {
364*f7c14bbaSAndroid Build Coastguard Worker SEC_NONE = 0,
365*f7c14bbaSAndroid Build Coastguard Worker /* expected_attach_type is optional, if kernel doesn't support that */
366*f7c14bbaSAndroid Build Coastguard Worker SEC_EXP_ATTACH_OPT = 1,
367*f7c14bbaSAndroid Build Coastguard Worker /* legacy, only used by libbpf_get_type_names() and
368*f7c14bbaSAndroid Build Coastguard Worker * libbpf_attach_type_by_name(), not used by libbpf itself at all.
369*f7c14bbaSAndroid Build Coastguard Worker * This used to be associated with cgroup (and few other) BPF programs
370*f7c14bbaSAndroid Build Coastguard Worker * that were attachable through BPF_PROG_ATTACH command. Pretty
371*f7c14bbaSAndroid Build Coastguard Worker * meaningless nowadays, though.
372*f7c14bbaSAndroid Build Coastguard Worker */
373*f7c14bbaSAndroid Build Coastguard Worker SEC_ATTACHABLE = 2,
374*f7c14bbaSAndroid Build Coastguard Worker SEC_ATTACHABLE_OPT = SEC_ATTACHABLE | SEC_EXP_ATTACH_OPT,
375*f7c14bbaSAndroid Build Coastguard Worker /* attachment target is specified through BTF ID in either kernel or
376*f7c14bbaSAndroid Build Coastguard Worker * other BPF program's BTF object
377*f7c14bbaSAndroid Build Coastguard Worker */
378*f7c14bbaSAndroid Build Coastguard Worker SEC_ATTACH_BTF = 4,
379*f7c14bbaSAndroid Build Coastguard Worker /* BPF program type allows sleeping/blocking in kernel */
380*f7c14bbaSAndroid Build Coastguard Worker SEC_SLEEPABLE = 8,
381*f7c14bbaSAndroid Build Coastguard Worker /* BPF program support non-linear XDP buffer */
382*f7c14bbaSAndroid Build Coastguard Worker SEC_XDP_FRAGS = 16,
383*f7c14bbaSAndroid Build Coastguard Worker /* Setup proper attach type for usdt probes. */
384*f7c14bbaSAndroid Build Coastguard Worker SEC_USDT = 32,
385*f7c14bbaSAndroid Build Coastguard Worker };
386*f7c14bbaSAndroid Build Coastguard Worker
387*f7c14bbaSAndroid Build Coastguard Worker struct bpf_sec_def {
388*f7c14bbaSAndroid Build Coastguard Worker char *sec;
389*f7c14bbaSAndroid Build Coastguard Worker enum bpf_prog_type prog_type;
390*f7c14bbaSAndroid Build Coastguard Worker enum bpf_attach_type expected_attach_type;
391*f7c14bbaSAndroid Build Coastguard Worker long cookie;
392*f7c14bbaSAndroid Build Coastguard Worker int handler_id;
393*f7c14bbaSAndroid Build Coastguard Worker
394*f7c14bbaSAndroid Build Coastguard Worker libbpf_prog_setup_fn_t prog_setup_fn;
395*f7c14bbaSAndroid Build Coastguard Worker libbpf_prog_prepare_load_fn_t prog_prepare_load_fn;
396*f7c14bbaSAndroid Build Coastguard Worker libbpf_prog_attach_fn_t prog_attach_fn;
397*f7c14bbaSAndroid Build Coastguard Worker };
398*f7c14bbaSAndroid Build Coastguard Worker
399*f7c14bbaSAndroid Build Coastguard Worker /*
400*f7c14bbaSAndroid Build Coastguard Worker * bpf_prog should be a better name but it has been used in
401*f7c14bbaSAndroid Build Coastguard Worker * linux/filter.h.
402*f7c14bbaSAndroid Build Coastguard Worker */
403*f7c14bbaSAndroid Build Coastguard Worker struct bpf_program {
404*f7c14bbaSAndroid Build Coastguard Worker char *name;
405*f7c14bbaSAndroid Build Coastguard Worker char *sec_name;
406*f7c14bbaSAndroid Build Coastguard Worker size_t sec_idx;
407*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_sec_def *sec_def;
408*f7c14bbaSAndroid Build Coastguard Worker /* this program's instruction offset (in number of instructions)
409*f7c14bbaSAndroid Build Coastguard Worker * within its containing ELF section
410*f7c14bbaSAndroid Build Coastguard Worker */
411*f7c14bbaSAndroid Build Coastguard Worker size_t sec_insn_off;
412*f7c14bbaSAndroid Build Coastguard Worker /* number of original instructions in ELF section belonging to this
413*f7c14bbaSAndroid Build Coastguard Worker * program, not taking into account subprogram instructions possible
414*f7c14bbaSAndroid Build Coastguard Worker * appended later during relocation
415*f7c14bbaSAndroid Build Coastguard Worker */
416*f7c14bbaSAndroid Build Coastguard Worker size_t sec_insn_cnt;
417*f7c14bbaSAndroid Build Coastguard Worker /* Offset (in number of instructions) of the start of instruction
418*f7c14bbaSAndroid Build Coastguard Worker * belonging to this BPF program within its containing main BPF
419*f7c14bbaSAndroid Build Coastguard Worker * program. For the entry-point (main) BPF program, this is always
420*f7c14bbaSAndroid Build Coastguard Worker * zero. For a sub-program, this gets reset before each of main BPF
421*f7c14bbaSAndroid Build Coastguard Worker * programs are processed and relocated and is used to determined
422*f7c14bbaSAndroid Build Coastguard Worker * whether sub-program was already appended to the main program, and
423*f7c14bbaSAndroid Build Coastguard Worker * if yes, at which instruction offset.
424*f7c14bbaSAndroid Build Coastguard Worker */
425*f7c14bbaSAndroid Build Coastguard Worker size_t sub_insn_off;
426*f7c14bbaSAndroid Build Coastguard Worker
427*f7c14bbaSAndroid Build Coastguard Worker /* instructions that belong to BPF program; insns[0] is located at
428*f7c14bbaSAndroid Build Coastguard Worker * sec_insn_off instruction within its ELF section in ELF file, so
429*f7c14bbaSAndroid Build Coastguard Worker * when mapping ELF file instruction index to the local instruction,
430*f7c14bbaSAndroid Build Coastguard Worker * one needs to subtract sec_insn_off; and vice versa.
431*f7c14bbaSAndroid Build Coastguard Worker */
432*f7c14bbaSAndroid Build Coastguard Worker struct bpf_insn *insns;
433*f7c14bbaSAndroid Build Coastguard Worker /* actual number of instruction in this BPF program's image; for
434*f7c14bbaSAndroid Build Coastguard Worker * entry-point BPF programs this includes the size of main program
435*f7c14bbaSAndroid Build Coastguard Worker * itself plus all the used sub-programs, appended at the end
436*f7c14bbaSAndroid Build Coastguard Worker */
437*f7c14bbaSAndroid Build Coastguard Worker size_t insns_cnt;
438*f7c14bbaSAndroid Build Coastguard Worker
439*f7c14bbaSAndroid Build Coastguard Worker struct reloc_desc *reloc_desc;
440*f7c14bbaSAndroid Build Coastguard Worker int nr_reloc;
441*f7c14bbaSAndroid Build Coastguard Worker
442*f7c14bbaSAndroid Build Coastguard Worker /* BPF verifier log settings */
443*f7c14bbaSAndroid Build Coastguard Worker char *log_buf;
444*f7c14bbaSAndroid Build Coastguard Worker size_t log_size;
445*f7c14bbaSAndroid Build Coastguard Worker __u32 log_level;
446*f7c14bbaSAndroid Build Coastguard Worker
447*f7c14bbaSAndroid Build Coastguard Worker struct bpf_object *obj;
448*f7c14bbaSAndroid Build Coastguard Worker
449*f7c14bbaSAndroid Build Coastguard Worker int fd;
450*f7c14bbaSAndroid Build Coastguard Worker bool autoload;
451*f7c14bbaSAndroid Build Coastguard Worker bool autoattach;
452*f7c14bbaSAndroid Build Coastguard Worker bool sym_global;
453*f7c14bbaSAndroid Build Coastguard Worker bool mark_btf_static;
454*f7c14bbaSAndroid Build Coastguard Worker enum bpf_prog_type type;
455*f7c14bbaSAndroid Build Coastguard Worker enum bpf_attach_type expected_attach_type;
456*f7c14bbaSAndroid Build Coastguard Worker int exception_cb_idx;
457*f7c14bbaSAndroid Build Coastguard Worker
458*f7c14bbaSAndroid Build Coastguard Worker int prog_ifindex;
459*f7c14bbaSAndroid Build Coastguard Worker __u32 attach_btf_obj_fd;
460*f7c14bbaSAndroid Build Coastguard Worker __u32 attach_btf_id;
461*f7c14bbaSAndroid Build Coastguard Worker __u32 attach_prog_fd;
462*f7c14bbaSAndroid Build Coastguard Worker
463*f7c14bbaSAndroid Build Coastguard Worker void *func_info;
464*f7c14bbaSAndroid Build Coastguard Worker __u32 func_info_rec_size;
465*f7c14bbaSAndroid Build Coastguard Worker __u32 func_info_cnt;
466*f7c14bbaSAndroid Build Coastguard Worker
467*f7c14bbaSAndroid Build Coastguard Worker void *line_info;
468*f7c14bbaSAndroid Build Coastguard Worker __u32 line_info_rec_size;
469*f7c14bbaSAndroid Build Coastguard Worker __u32 line_info_cnt;
470*f7c14bbaSAndroid Build Coastguard Worker __u32 prog_flags;
471*f7c14bbaSAndroid Build Coastguard Worker };
472*f7c14bbaSAndroid Build Coastguard Worker
473*f7c14bbaSAndroid Build Coastguard Worker struct bpf_struct_ops {
474*f7c14bbaSAndroid Build Coastguard Worker const char *tname;
475*f7c14bbaSAndroid Build Coastguard Worker const struct btf_type *type;
476*f7c14bbaSAndroid Build Coastguard Worker struct bpf_program **progs;
477*f7c14bbaSAndroid Build Coastguard Worker __u32 *kern_func_off;
478*f7c14bbaSAndroid Build Coastguard Worker /* e.g. struct tcp_congestion_ops in bpf_prog's btf format */
479*f7c14bbaSAndroid Build Coastguard Worker void *data;
480*f7c14bbaSAndroid Build Coastguard Worker /* e.g. struct bpf_struct_ops_tcp_congestion_ops in
481*f7c14bbaSAndroid Build Coastguard Worker * btf_vmlinux's format.
482*f7c14bbaSAndroid Build Coastguard Worker * struct bpf_struct_ops_tcp_congestion_ops {
483*f7c14bbaSAndroid Build Coastguard Worker * [... some other kernel fields ...]
484*f7c14bbaSAndroid Build Coastguard Worker * struct tcp_congestion_ops data;
485*f7c14bbaSAndroid Build Coastguard Worker * }
486*f7c14bbaSAndroid Build Coastguard Worker * kern_vdata-size == sizeof(struct bpf_struct_ops_tcp_congestion_ops)
487*f7c14bbaSAndroid Build Coastguard Worker * bpf_map__init_kern_struct_ops() will populate the "kern_vdata"
488*f7c14bbaSAndroid Build Coastguard Worker * from "data".
489*f7c14bbaSAndroid Build Coastguard Worker */
490*f7c14bbaSAndroid Build Coastguard Worker void *kern_vdata;
491*f7c14bbaSAndroid Build Coastguard Worker __u32 type_id;
492*f7c14bbaSAndroid Build Coastguard Worker };
493*f7c14bbaSAndroid Build Coastguard Worker
494*f7c14bbaSAndroid Build Coastguard Worker #define DATA_SEC ".data"
495*f7c14bbaSAndroid Build Coastguard Worker #define BSS_SEC ".bss"
496*f7c14bbaSAndroid Build Coastguard Worker #define RODATA_SEC ".rodata"
497*f7c14bbaSAndroid Build Coastguard Worker #define KCONFIG_SEC ".kconfig"
498*f7c14bbaSAndroid Build Coastguard Worker #define KSYMS_SEC ".ksyms"
499*f7c14bbaSAndroid Build Coastguard Worker #define STRUCT_OPS_SEC ".struct_ops"
500*f7c14bbaSAndroid Build Coastguard Worker #define STRUCT_OPS_LINK_SEC ".struct_ops.link"
501*f7c14bbaSAndroid Build Coastguard Worker #define ARENA_SEC ".addr_space.1"
502*f7c14bbaSAndroid Build Coastguard Worker
503*f7c14bbaSAndroid Build Coastguard Worker enum libbpf_map_type {
504*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_MAP_UNSPEC,
505*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_MAP_DATA,
506*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_MAP_BSS,
507*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_MAP_RODATA,
508*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_MAP_KCONFIG,
509*f7c14bbaSAndroid Build Coastguard Worker };
510*f7c14bbaSAndroid Build Coastguard Worker
511*f7c14bbaSAndroid Build Coastguard Worker struct bpf_map_def {
512*f7c14bbaSAndroid Build Coastguard Worker unsigned int type;
513*f7c14bbaSAndroid Build Coastguard Worker unsigned int key_size;
514*f7c14bbaSAndroid Build Coastguard Worker unsigned int value_size;
515*f7c14bbaSAndroid Build Coastguard Worker unsigned int max_entries;
516*f7c14bbaSAndroid Build Coastguard Worker unsigned int map_flags;
517*f7c14bbaSAndroid Build Coastguard Worker };
518*f7c14bbaSAndroid Build Coastguard Worker
519*f7c14bbaSAndroid Build Coastguard Worker struct bpf_map {
520*f7c14bbaSAndroid Build Coastguard Worker struct bpf_object *obj;
521*f7c14bbaSAndroid Build Coastguard Worker char *name;
522*f7c14bbaSAndroid Build Coastguard Worker /* real_name is defined for special internal maps (.rodata*,
523*f7c14bbaSAndroid Build Coastguard Worker * .data*, .bss, .kconfig) and preserves their original ELF section
524*f7c14bbaSAndroid Build Coastguard Worker * name. This is important to be able to find corresponding BTF
525*f7c14bbaSAndroid Build Coastguard Worker * DATASEC information.
526*f7c14bbaSAndroid Build Coastguard Worker */
527*f7c14bbaSAndroid Build Coastguard Worker char *real_name;
528*f7c14bbaSAndroid Build Coastguard Worker int fd;
529*f7c14bbaSAndroid Build Coastguard Worker int sec_idx;
530*f7c14bbaSAndroid Build Coastguard Worker size_t sec_offset;
531*f7c14bbaSAndroid Build Coastguard Worker int map_ifindex;
532*f7c14bbaSAndroid Build Coastguard Worker int inner_map_fd;
533*f7c14bbaSAndroid Build Coastguard Worker struct bpf_map_def def;
534*f7c14bbaSAndroid Build Coastguard Worker __u32 numa_node;
535*f7c14bbaSAndroid Build Coastguard Worker __u32 btf_var_idx;
536*f7c14bbaSAndroid Build Coastguard Worker int mod_btf_fd;
537*f7c14bbaSAndroid Build Coastguard Worker __u32 btf_key_type_id;
538*f7c14bbaSAndroid Build Coastguard Worker __u32 btf_value_type_id;
539*f7c14bbaSAndroid Build Coastguard Worker __u32 btf_vmlinux_value_type_id;
540*f7c14bbaSAndroid Build Coastguard Worker enum libbpf_map_type libbpf_type;
541*f7c14bbaSAndroid Build Coastguard Worker void *mmaped;
542*f7c14bbaSAndroid Build Coastguard Worker struct bpf_struct_ops *st_ops;
543*f7c14bbaSAndroid Build Coastguard Worker struct bpf_map *inner_map;
544*f7c14bbaSAndroid Build Coastguard Worker void **init_slots;
545*f7c14bbaSAndroid Build Coastguard Worker int init_slots_sz;
546*f7c14bbaSAndroid Build Coastguard Worker char *pin_path;
547*f7c14bbaSAndroid Build Coastguard Worker bool pinned;
548*f7c14bbaSAndroid Build Coastguard Worker bool reused;
549*f7c14bbaSAndroid Build Coastguard Worker bool autocreate;
550*f7c14bbaSAndroid Build Coastguard Worker __u64 map_extra;
551*f7c14bbaSAndroid Build Coastguard Worker };
552*f7c14bbaSAndroid Build Coastguard Worker
553*f7c14bbaSAndroid Build Coastguard Worker enum extern_type {
554*f7c14bbaSAndroid Build Coastguard Worker EXT_UNKNOWN,
555*f7c14bbaSAndroid Build Coastguard Worker EXT_KCFG,
556*f7c14bbaSAndroid Build Coastguard Worker EXT_KSYM,
557*f7c14bbaSAndroid Build Coastguard Worker };
558*f7c14bbaSAndroid Build Coastguard Worker
559*f7c14bbaSAndroid Build Coastguard Worker enum kcfg_type {
560*f7c14bbaSAndroid Build Coastguard Worker KCFG_UNKNOWN,
561*f7c14bbaSAndroid Build Coastguard Worker KCFG_CHAR,
562*f7c14bbaSAndroid Build Coastguard Worker KCFG_BOOL,
563*f7c14bbaSAndroid Build Coastguard Worker KCFG_INT,
564*f7c14bbaSAndroid Build Coastguard Worker KCFG_TRISTATE,
565*f7c14bbaSAndroid Build Coastguard Worker KCFG_CHAR_ARR,
566*f7c14bbaSAndroid Build Coastguard Worker };
567*f7c14bbaSAndroid Build Coastguard Worker
568*f7c14bbaSAndroid Build Coastguard Worker struct extern_desc {
569*f7c14bbaSAndroid Build Coastguard Worker enum extern_type type;
570*f7c14bbaSAndroid Build Coastguard Worker int sym_idx;
571*f7c14bbaSAndroid Build Coastguard Worker int btf_id;
572*f7c14bbaSAndroid Build Coastguard Worker int sec_btf_id;
573*f7c14bbaSAndroid Build Coastguard Worker const char *name;
574*f7c14bbaSAndroid Build Coastguard Worker char *essent_name;
575*f7c14bbaSAndroid Build Coastguard Worker bool is_set;
576*f7c14bbaSAndroid Build Coastguard Worker bool is_weak;
577*f7c14bbaSAndroid Build Coastguard Worker union {
578*f7c14bbaSAndroid Build Coastguard Worker struct {
579*f7c14bbaSAndroid Build Coastguard Worker enum kcfg_type type;
580*f7c14bbaSAndroid Build Coastguard Worker int sz;
581*f7c14bbaSAndroid Build Coastguard Worker int align;
582*f7c14bbaSAndroid Build Coastguard Worker int data_off;
583*f7c14bbaSAndroid Build Coastguard Worker bool is_signed;
584*f7c14bbaSAndroid Build Coastguard Worker } kcfg;
585*f7c14bbaSAndroid Build Coastguard Worker struct {
586*f7c14bbaSAndroid Build Coastguard Worker unsigned long long addr;
587*f7c14bbaSAndroid Build Coastguard Worker
588*f7c14bbaSAndroid Build Coastguard Worker /* target btf_id of the corresponding kernel var. */
589*f7c14bbaSAndroid Build Coastguard Worker int kernel_btf_obj_fd;
590*f7c14bbaSAndroid Build Coastguard Worker int kernel_btf_id;
591*f7c14bbaSAndroid Build Coastguard Worker
592*f7c14bbaSAndroid Build Coastguard Worker /* local btf_id of the ksym extern's type. */
593*f7c14bbaSAndroid Build Coastguard Worker __u32 type_id;
594*f7c14bbaSAndroid Build Coastguard Worker /* BTF fd index to be patched in for insn->off, this is
595*f7c14bbaSAndroid Build Coastguard Worker * 0 for vmlinux BTF, index in obj->fd_array for module
596*f7c14bbaSAndroid Build Coastguard Worker * BTF
597*f7c14bbaSAndroid Build Coastguard Worker */
598*f7c14bbaSAndroid Build Coastguard Worker __s16 btf_fd_idx;
599*f7c14bbaSAndroid Build Coastguard Worker } ksym;
600*f7c14bbaSAndroid Build Coastguard Worker };
601*f7c14bbaSAndroid Build Coastguard Worker };
602*f7c14bbaSAndroid Build Coastguard Worker
603*f7c14bbaSAndroid Build Coastguard Worker struct module_btf {
604*f7c14bbaSAndroid Build Coastguard Worker struct btf *btf;
605*f7c14bbaSAndroid Build Coastguard Worker char *name;
606*f7c14bbaSAndroid Build Coastguard Worker __u32 id;
607*f7c14bbaSAndroid Build Coastguard Worker int fd;
608*f7c14bbaSAndroid Build Coastguard Worker int fd_array_idx;
609*f7c14bbaSAndroid Build Coastguard Worker };
610*f7c14bbaSAndroid Build Coastguard Worker
611*f7c14bbaSAndroid Build Coastguard Worker enum sec_type {
612*f7c14bbaSAndroid Build Coastguard Worker SEC_UNUSED = 0,
613*f7c14bbaSAndroid Build Coastguard Worker SEC_RELO,
614*f7c14bbaSAndroid Build Coastguard Worker SEC_BSS,
615*f7c14bbaSAndroid Build Coastguard Worker SEC_DATA,
616*f7c14bbaSAndroid Build Coastguard Worker SEC_RODATA,
617*f7c14bbaSAndroid Build Coastguard Worker SEC_ST_OPS,
618*f7c14bbaSAndroid Build Coastguard Worker };
619*f7c14bbaSAndroid Build Coastguard Worker
620*f7c14bbaSAndroid Build Coastguard Worker struct elf_sec_desc {
621*f7c14bbaSAndroid Build Coastguard Worker enum sec_type sec_type;
622*f7c14bbaSAndroid Build Coastguard Worker Elf64_Shdr *shdr;
623*f7c14bbaSAndroid Build Coastguard Worker Elf_Data *data;
624*f7c14bbaSAndroid Build Coastguard Worker };
625*f7c14bbaSAndroid Build Coastguard Worker
626*f7c14bbaSAndroid Build Coastguard Worker struct elf_state {
627*f7c14bbaSAndroid Build Coastguard Worker int fd;
628*f7c14bbaSAndroid Build Coastguard Worker const void *obj_buf;
629*f7c14bbaSAndroid Build Coastguard Worker size_t obj_buf_sz;
630*f7c14bbaSAndroid Build Coastguard Worker Elf *elf;
631*f7c14bbaSAndroid Build Coastguard Worker Elf64_Ehdr *ehdr;
632*f7c14bbaSAndroid Build Coastguard Worker Elf_Data *symbols;
633*f7c14bbaSAndroid Build Coastguard Worker Elf_Data *arena_data;
634*f7c14bbaSAndroid Build Coastguard Worker size_t shstrndx; /* section index for section name strings */
635*f7c14bbaSAndroid Build Coastguard Worker size_t strtabidx;
636*f7c14bbaSAndroid Build Coastguard Worker struct elf_sec_desc *secs;
637*f7c14bbaSAndroid Build Coastguard Worker size_t sec_cnt;
638*f7c14bbaSAndroid Build Coastguard Worker int btf_maps_shndx;
639*f7c14bbaSAndroid Build Coastguard Worker __u32 btf_maps_sec_btf_id;
640*f7c14bbaSAndroid Build Coastguard Worker int text_shndx;
641*f7c14bbaSAndroid Build Coastguard Worker int symbols_shndx;
642*f7c14bbaSAndroid Build Coastguard Worker bool has_st_ops;
643*f7c14bbaSAndroid Build Coastguard Worker int arena_data_shndx;
644*f7c14bbaSAndroid Build Coastguard Worker };
645*f7c14bbaSAndroid Build Coastguard Worker
646*f7c14bbaSAndroid Build Coastguard Worker struct usdt_manager;
647*f7c14bbaSAndroid Build Coastguard Worker
648*f7c14bbaSAndroid Build Coastguard Worker struct bpf_object {
649*f7c14bbaSAndroid Build Coastguard Worker char name[BPF_OBJ_NAME_LEN];
650*f7c14bbaSAndroid Build Coastguard Worker char license[64];
651*f7c14bbaSAndroid Build Coastguard Worker __u32 kern_version;
652*f7c14bbaSAndroid Build Coastguard Worker
653*f7c14bbaSAndroid Build Coastguard Worker struct bpf_program *programs;
654*f7c14bbaSAndroid Build Coastguard Worker size_t nr_programs;
655*f7c14bbaSAndroid Build Coastguard Worker struct bpf_map *maps;
656*f7c14bbaSAndroid Build Coastguard Worker size_t nr_maps;
657*f7c14bbaSAndroid Build Coastguard Worker size_t maps_cap;
658*f7c14bbaSAndroid Build Coastguard Worker
659*f7c14bbaSAndroid Build Coastguard Worker char *kconfig;
660*f7c14bbaSAndroid Build Coastguard Worker struct extern_desc *externs;
661*f7c14bbaSAndroid Build Coastguard Worker int nr_extern;
662*f7c14bbaSAndroid Build Coastguard Worker int kconfig_map_idx;
663*f7c14bbaSAndroid Build Coastguard Worker
664*f7c14bbaSAndroid Build Coastguard Worker bool loaded;
665*f7c14bbaSAndroid Build Coastguard Worker bool has_subcalls;
666*f7c14bbaSAndroid Build Coastguard Worker bool has_rodata;
667*f7c14bbaSAndroid Build Coastguard Worker
668*f7c14bbaSAndroid Build Coastguard Worker struct bpf_gen *gen_loader;
669*f7c14bbaSAndroid Build Coastguard Worker
670*f7c14bbaSAndroid Build Coastguard Worker /* Information when doing ELF related work. Only valid if efile.elf is not NULL */
671*f7c14bbaSAndroid Build Coastguard Worker struct elf_state efile;
672*f7c14bbaSAndroid Build Coastguard Worker
673*f7c14bbaSAndroid Build Coastguard Worker struct btf *btf;
674*f7c14bbaSAndroid Build Coastguard Worker struct btf_ext *btf_ext;
675*f7c14bbaSAndroid Build Coastguard Worker
676*f7c14bbaSAndroid Build Coastguard Worker /* Parse and load BTF vmlinux if any of the programs in the object need
677*f7c14bbaSAndroid Build Coastguard Worker * it at load time.
678*f7c14bbaSAndroid Build Coastguard Worker */
679*f7c14bbaSAndroid Build Coastguard Worker struct btf *btf_vmlinux;
680*f7c14bbaSAndroid Build Coastguard Worker /* Path to the custom BTF to be used for BPF CO-RE relocations as an
681*f7c14bbaSAndroid Build Coastguard Worker * override for vmlinux BTF.
682*f7c14bbaSAndroid Build Coastguard Worker */
683*f7c14bbaSAndroid Build Coastguard Worker char *btf_custom_path;
684*f7c14bbaSAndroid Build Coastguard Worker /* vmlinux BTF override for CO-RE relocations */
685*f7c14bbaSAndroid Build Coastguard Worker struct btf *btf_vmlinux_override;
686*f7c14bbaSAndroid Build Coastguard Worker /* Lazily initialized kernel module BTFs */
687*f7c14bbaSAndroid Build Coastguard Worker struct module_btf *btf_modules;
688*f7c14bbaSAndroid Build Coastguard Worker bool btf_modules_loaded;
689*f7c14bbaSAndroid Build Coastguard Worker size_t btf_module_cnt;
690*f7c14bbaSAndroid Build Coastguard Worker size_t btf_module_cap;
691*f7c14bbaSAndroid Build Coastguard Worker
692*f7c14bbaSAndroid Build Coastguard Worker /* optional log settings passed to BPF_BTF_LOAD and BPF_PROG_LOAD commands */
693*f7c14bbaSAndroid Build Coastguard Worker char *log_buf;
694*f7c14bbaSAndroid Build Coastguard Worker size_t log_size;
695*f7c14bbaSAndroid Build Coastguard Worker __u32 log_level;
696*f7c14bbaSAndroid Build Coastguard Worker
697*f7c14bbaSAndroid Build Coastguard Worker int *fd_array;
698*f7c14bbaSAndroid Build Coastguard Worker size_t fd_array_cap;
699*f7c14bbaSAndroid Build Coastguard Worker size_t fd_array_cnt;
700*f7c14bbaSAndroid Build Coastguard Worker
701*f7c14bbaSAndroid Build Coastguard Worker struct usdt_manager *usdt_man;
702*f7c14bbaSAndroid Build Coastguard Worker
703*f7c14bbaSAndroid Build Coastguard Worker struct bpf_map *arena_map;
704*f7c14bbaSAndroid Build Coastguard Worker void *arena_data;
705*f7c14bbaSAndroid Build Coastguard Worker size_t arena_data_sz;
706*f7c14bbaSAndroid Build Coastguard Worker
707*f7c14bbaSAndroid Build Coastguard Worker struct kern_feature_cache *feat_cache;
708*f7c14bbaSAndroid Build Coastguard Worker char *token_path;
709*f7c14bbaSAndroid Build Coastguard Worker int token_fd;
710*f7c14bbaSAndroid Build Coastguard Worker
711*f7c14bbaSAndroid Build Coastguard Worker char path[];
712*f7c14bbaSAndroid Build Coastguard Worker };
713*f7c14bbaSAndroid Build Coastguard Worker
714*f7c14bbaSAndroid Build Coastguard Worker static const char *elf_sym_str(const struct bpf_object *obj, size_t off);
715*f7c14bbaSAndroid Build Coastguard Worker static const char *elf_sec_str(const struct bpf_object *obj, size_t off);
716*f7c14bbaSAndroid Build Coastguard Worker static Elf_Scn *elf_sec_by_idx(const struct bpf_object *obj, size_t idx);
717*f7c14bbaSAndroid Build Coastguard Worker static Elf_Scn *elf_sec_by_name(const struct bpf_object *obj, const char *name);
718*f7c14bbaSAndroid Build Coastguard Worker static Elf64_Shdr *elf_sec_hdr(const struct bpf_object *obj, Elf_Scn *scn);
719*f7c14bbaSAndroid Build Coastguard Worker static const char *elf_sec_name(const struct bpf_object *obj, Elf_Scn *scn);
720*f7c14bbaSAndroid Build Coastguard Worker static Elf_Data *elf_sec_data(const struct bpf_object *obj, Elf_Scn *scn);
721*f7c14bbaSAndroid Build Coastguard Worker static Elf64_Sym *elf_sym_by_idx(const struct bpf_object *obj, size_t idx);
722*f7c14bbaSAndroid Build Coastguard Worker static Elf64_Rel *elf_rel_by_idx(Elf_Data *data, size_t idx);
723*f7c14bbaSAndroid Build Coastguard Worker
bpf_program__unload(struct bpf_program * prog)724*f7c14bbaSAndroid Build Coastguard Worker void bpf_program__unload(struct bpf_program *prog)
725*f7c14bbaSAndroid Build Coastguard Worker {
726*f7c14bbaSAndroid Build Coastguard Worker if (!prog)
727*f7c14bbaSAndroid Build Coastguard Worker return;
728*f7c14bbaSAndroid Build Coastguard Worker
729*f7c14bbaSAndroid Build Coastguard Worker zclose(prog->fd);
730*f7c14bbaSAndroid Build Coastguard Worker
731*f7c14bbaSAndroid Build Coastguard Worker zfree(&prog->func_info);
732*f7c14bbaSAndroid Build Coastguard Worker zfree(&prog->line_info);
733*f7c14bbaSAndroid Build Coastguard Worker }
734*f7c14bbaSAndroid Build Coastguard Worker
bpf_program__exit(struct bpf_program * prog)735*f7c14bbaSAndroid Build Coastguard Worker static void bpf_program__exit(struct bpf_program *prog)
736*f7c14bbaSAndroid Build Coastguard Worker {
737*f7c14bbaSAndroid Build Coastguard Worker if (!prog)
738*f7c14bbaSAndroid Build Coastguard Worker return;
739*f7c14bbaSAndroid Build Coastguard Worker
740*f7c14bbaSAndroid Build Coastguard Worker bpf_program__unload(prog);
741*f7c14bbaSAndroid Build Coastguard Worker zfree(&prog->name);
742*f7c14bbaSAndroid Build Coastguard Worker zfree(&prog->sec_name);
743*f7c14bbaSAndroid Build Coastguard Worker zfree(&prog->insns);
744*f7c14bbaSAndroid Build Coastguard Worker zfree(&prog->reloc_desc);
745*f7c14bbaSAndroid Build Coastguard Worker
746*f7c14bbaSAndroid Build Coastguard Worker prog->nr_reloc = 0;
747*f7c14bbaSAndroid Build Coastguard Worker prog->insns_cnt = 0;
748*f7c14bbaSAndroid Build Coastguard Worker prog->sec_idx = -1;
749*f7c14bbaSAndroid Build Coastguard Worker }
750*f7c14bbaSAndroid Build Coastguard Worker
insn_is_subprog_call(const struct bpf_insn * insn)751*f7c14bbaSAndroid Build Coastguard Worker static bool insn_is_subprog_call(const struct bpf_insn *insn)
752*f7c14bbaSAndroid Build Coastguard Worker {
753*f7c14bbaSAndroid Build Coastguard Worker return BPF_CLASS(insn->code) == BPF_JMP &&
754*f7c14bbaSAndroid Build Coastguard Worker BPF_OP(insn->code) == BPF_CALL &&
755*f7c14bbaSAndroid Build Coastguard Worker BPF_SRC(insn->code) == BPF_K &&
756*f7c14bbaSAndroid Build Coastguard Worker insn->src_reg == BPF_PSEUDO_CALL &&
757*f7c14bbaSAndroid Build Coastguard Worker insn->dst_reg == 0 &&
758*f7c14bbaSAndroid Build Coastguard Worker insn->off == 0;
759*f7c14bbaSAndroid Build Coastguard Worker }
760*f7c14bbaSAndroid Build Coastguard Worker
is_call_insn(const struct bpf_insn * insn)761*f7c14bbaSAndroid Build Coastguard Worker static bool is_call_insn(const struct bpf_insn *insn)
762*f7c14bbaSAndroid Build Coastguard Worker {
763*f7c14bbaSAndroid Build Coastguard Worker return insn->code == (BPF_JMP | BPF_CALL);
764*f7c14bbaSAndroid Build Coastguard Worker }
765*f7c14bbaSAndroid Build Coastguard Worker
insn_is_pseudo_func(struct bpf_insn * insn)766*f7c14bbaSAndroid Build Coastguard Worker static bool insn_is_pseudo_func(struct bpf_insn *insn)
767*f7c14bbaSAndroid Build Coastguard Worker {
768*f7c14bbaSAndroid Build Coastguard Worker return is_ldimm64_insn(insn) && insn->src_reg == BPF_PSEUDO_FUNC;
769*f7c14bbaSAndroid Build Coastguard Worker }
770*f7c14bbaSAndroid Build Coastguard Worker
771*f7c14bbaSAndroid Build Coastguard Worker static int
bpf_object__init_prog(struct bpf_object * obj,struct bpf_program * prog,const char * name,size_t sec_idx,const char * sec_name,size_t sec_off,void * insn_data,size_t insn_data_sz)772*f7c14bbaSAndroid Build Coastguard Worker bpf_object__init_prog(struct bpf_object *obj, struct bpf_program *prog,
773*f7c14bbaSAndroid Build Coastguard Worker const char *name, size_t sec_idx, const char *sec_name,
774*f7c14bbaSAndroid Build Coastguard Worker size_t sec_off, void *insn_data, size_t insn_data_sz)
775*f7c14bbaSAndroid Build Coastguard Worker {
776*f7c14bbaSAndroid Build Coastguard Worker if (insn_data_sz == 0 || insn_data_sz % BPF_INSN_SZ || sec_off % BPF_INSN_SZ) {
777*f7c14bbaSAndroid Build Coastguard Worker pr_warn("sec '%s': corrupted program '%s', offset %zu, size %zu\n",
778*f7c14bbaSAndroid Build Coastguard Worker sec_name, name, sec_off, insn_data_sz);
779*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
780*f7c14bbaSAndroid Build Coastguard Worker }
781*f7c14bbaSAndroid Build Coastguard Worker
782*f7c14bbaSAndroid Build Coastguard Worker memset(prog, 0, sizeof(*prog));
783*f7c14bbaSAndroid Build Coastguard Worker prog->obj = obj;
784*f7c14bbaSAndroid Build Coastguard Worker
785*f7c14bbaSAndroid Build Coastguard Worker prog->sec_idx = sec_idx;
786*f7c14bbaSAndroid Build Coastguard Worker prog->sec_insn_off = sec_off / BPF_INSN_SZ;
787*f7c14bbaSAndroid Build Coastguard Worker prog->sec_insn_cnt = insn_data_sz / BPF_INSN_SZ;
788*f7c14bbaSAndroid Build Coastguard Worker /* insns_cnt can later be increased by appending used subprograms */
789*f7c14bbaSAndroid Build Coastguard Worker prog->insns_cnt = prog->sec_insn_cnt;
790*f7c14bbaSAndroid Build Coastguard Worker
791*f7c14bbaSAndroid Build Coastguard Worker prog->type = BPF_PROG_TYPE_UNSPEC;
792*f7c14bbaSAndroid Build Coastguard Worker prog->fd = -1;
793*f7c14bbaSAndroid Build Coastguard Worker prog->exception_cb_idx = -1;
794*f7c14bbaSAndroid Build Coastguard Worker
795*f7c14bbaSAndroid Build Coastguard Worker /* libbpf's convention for SEC("?abc...") is that it's just like
796*f7c14bbaSAndroid Build Coastguard Worker * SEC("abc...") but the corresponding bpf_program starts out with
797*f7c14bbaSAndroid Build Coastguard Worker * autoload set to false.
798*f7c14bbaSAndroid Build Coastguard Worker */
799*f7c14bbaSAndroid Build Coastguard Worker if (sec_name[0] == '?') {
800*f7c14bbaSAndroid Build Coastguard Worker prog->autoload = false;
801*f7c14bbaSAndroid Build Coastguard Worker /* from now on forget there was ? in section name */
802*f7c14bbaSAndroid Build Coastguard Worker sec_name++;
803*f7c14bbaSAndroid Build Coastguard Worker } else {
804*f7c14bbaSAndroid Build Coastguard Worker prog->autoload = true;
805*f7c14bbaSAndroid Build Coastguard Worker }
806*f7c14bbaSAndroid Build Coastguard Worker
807*f7c14bbaSAndroid Build Coastguard Worker prog->autoattach = true;
808*f7c14bbaSAndroid Build Coastguard Worker
809*f7c14bbaSAndroid Build Coastguard Worker /* inherit object's log_level */
810*f7c14bbaSAndroid Build Coastguard Worker prog->log_level = obj->log_level;
811*f7c14bbaSAndroid Build Coastguard Worker
812*f7c14bbaSAndroid Build Coastguard Worker prog->sec_name = strdup(sec_name);
813*f7c14bbaSAndroid Build Coastguard Worker if (!prog->sec_name)
814*f7c14bbaSAndroid Build Coastguard Worker goto errout;
815*f7c14bbaSAndroid Build Coastguard Worker
816*f7c14bbaSAndroid Build Coastguard Worker prog->name = strdup(name);
817*f7c14bbaSAndroid Build Coastguard Worker if (!prog->name)
818*f7c14bbaSAndroid Build Coastguard Worker goto errout;
819*f7c14bbaSAndroid Build Coastguard Worker
820*f7c14bbaSAndroid Build Coastguard Worker prog->insns = malloc(insn_data_sz);
821*f7c14bbaSAndroid Build Coastguard Worker if (!prog->insns)
822*f7c14bbaSAndroid Build Coastguard Worker goto errout;
823*f7c14bbaSAndroid Build Coastguard Worker memcpy(prog->insns, insn_data, insn_data_sz);
824*f7c14bbaSAndroid Build Coastguard Worker
825*f7c14bbaSAndroid Build Coastguard Worker return 0;
826*f7c14bbaSAndroid Build Coastguard Worker errout:
827*f7c14bbaSAndroid Build Coastguard Worker pr_warn("sec '%s': failed to allocate memory for prog '%s'\n", sec_name, name);
828*f7c14bbaSAndroid Build Coastguard Worker bpf_program__exit(prog);
829*f7c14bbaSAndroid Build Coastguard Worker return -ENOMEM;
830*f7c14bbaSAndroid Build Coastguard Worker }
831*f7c14bbaSAndroid Build Coastguard Worker
832*f7c14bbaSAndroid Build Coastguard Worker static int
bpf_object__add_programs(struct bpf_object * obj,Elf_Data * sec_data,const char * sec_name,int sec_idx)833*f7c14bbaSAndroid Build Coastguard Worker bpf_object__add_programs(struct bpf_object *obj, Elf_Data *sec_data,
834*f7c14bbaSAndroid Build Coastguard Worker const char *sec_name, int sec_idx)
835*f7c14bbaSAndroid Build Coastguard Worker {
836*f7c14bbaSAndroid Build Coastguard Worker Elf_Data *symbols = obj->efile.symbols;
837*f7c14bbaSAndroid Build Coastguard Worker struct bpf_program *prog, *progs;
838*f7c14bbaSAndroid Build Coastguard Worker void *data = sec_data->d_buf;
839*f7c14bbaSAndroid Build Coastguard Worker size_t sec_sz = sec_data->d_size, sec_off, prog_sz, nr_syms;
840*f7c14bbaSAndroid Build Coastguard Worker int nr_progs, err, i;
841*f7c14bbaSAndroid Build Coastguard Worker const char *name;
842*f7c14bbaSAndroid Build Coastguard Worker Elf64_Sym *sym;
843*f7c14bbaSAndroid Build Coastguard Worker
844*f7c14bbaSAndroid Build Coastguard Worker progs = obj->programs;
845*f7c14bbaSAndroid Build Coastguard Worker nr_progs = obj->nr_programs;
846*f7c14bbaSAndroid Build Coastguard Worker nr_syms = symbols->d_size / sizeof(Elf64_Sym);
847*f7c14bbaSAndroid Build Coastguard Worker
848*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < nr_syms; i++) {
849*f7c14bbaSAndroid Build Coastguard Worker sym = elf_sym_by_idx(obj, i);
850*f7c14bbaSAndroid Build Coastguard Worker
851*f7c14bbaSAndroid Build Coastguard Worker if (sym->st_shndx != sec_idx)
852*f7c14bbaSAndroid Build Coastguard Worker continue;
853*f7c14bbaSAndroid Build Coastguard Worker if (ELF64_ST_TYPE(sym->st_info) != STT_FUNC)
854*f7c14bbaSAndroid Build Coastguard Worker continue;
855*f7c14bbaSAndroid Build Coastguard Worker
856*f7c14bbaSAndroid Build Coastguard Worker prog_sz = sym->st_size;
857*f7c14bbaSAndroid Build Coastguard Worker sec_off = sym->st_value;
858*f7c14bbaSAndroid Build Coastguard Worker
859*f7c14bbaSAndroid Build Coastguard Worker name = elf_sym_str(obj, sym->st_name);
860*f7c14bbaSAndroid Build Coastguard Worker if (!name) {
861*f7c14bbaSAndroid Build Coastguard Worker pr_warn("sec '%s': failed to get symbol name for offset %zu\n",
862*f7c14bbaSAndroid Build Coastguard Worker sec_name, sec_off);
863*f7c14bbaSAndroid Build Coastguard Worker return -LIBBPF_ERRNO__FORMAT;
864*f7c14bbaSAndroid Build Coastguard Worker }
865*f7c14bbaSAndroid Build Coastguard Worker
866*f7c14bbaSAndroid Build Coastguard Worker if (sec_off + prog_sz > sec_sz) {
867*f7c14bbaSAndroid Build Coastguard Worker pr_warn("sec '%s': program at offset %zu crosses section boundary\n",
868*f7c14bbaSAndroid Build Coastguard Worker sec_name, sec_off);
869*f7c14bbaSAndroid Build Coastguard Worker return -LIBBPF_ERRNO__FORMAT;
870*f7c14bbaSAndroid Build Coastguard Worker }
871*f7c14bbaSAndroid Build Coastguard Worker
872*f7c14bbaSAndroid Build Coastguard Worker if (sec_idx != obj->efile.text_shndx && ELF64_ST_BIND(sym->st_info) == STB_LOCAL) {
873*f7c14bbaSAndroid Build Coastguard Worker pr_warn("sec '%s': program '%s' is static and not supported\n", sec_name, name);
874*f7c14bbaSAndroid Build Coastguard Worker return -ENOTSUP;
875*f7c14bbaSAndroid Build Coastguard Worker }
876*f7c14bbaSAndroid Build Coastguard Worker
877*f7c14bbaSAndroid Build Coastguard Worker pr_debug("sec '%s': found program '%s' at insn offset %zu (%zu bytes), code size %zu insns (%zu bytes)\n",
878*f7c14bbaSAndroid Build Coastguard Worker sec_name, name, sec_off / BPF_INSN_SZ, sec_off, prog_sz / BPF_INSN_SZ, prog_sz);
879*f7c14bbaSAndroid Build Coastguard Worker
880*f7c14bbaSAndroid Build Coastguard Worker progs = libbpf_reallocarray(progs, nr_progs + 1, sizeof(*progs));
881*f7c14bbaSAndroid Build Coastguard Worker if (!progs) {
882*f7c14bbaSAndroid Build Coastguard Worker /*
883*f7c14bbaSAndroid Build Coastguard Worker * In this case the original obj->programs
884*f7c14bbaSAndroid Build Coastguard Worker * is still valid, so don't need special treat for
885*f7c14bbaSAndroid Build Coastguard Worker * bpf_close_object().
886*f7c14bbaSAndroid Build Coastguard Worker */
887*f7c14bbaSAndroid Build Coastguard Worker pr_warn("sec '%s': failed to alloc memory for new program '%s'\n",
888*f7c14bbaSAndroid Build Coastguard Worker sec_name, name);
889*f7c14bbaSAndroid Build Coastguard Worker return -ENOMEM;
890*f7c14bbaSAndroid Build Coastguard Worker }
891*f7c14bbaSAndroid Build Coastguard Worker obj->programs = progs;
892*f7c14bbaSAndroid Build Coastguard Worker
893*f7c14bbaSAndroid Build Coastguard Worker prog = &progs[nr_progs];
894*f7c14bbaSAndroid Build Coastguard Worker
895*f7c14bbaSAndroid Build Coastguard Worker err = bpf_object__init_prog(obj, prog, name, sec_idx, sec_name,
896*f7c14bbaSAndroid Build Coastguard Worker sec_off, data + sec_off, prog_sz);
897*f7c14bbaSAndroid Build Coastguard Worker if (err)
898*f7c14bbaSAndroid Build Coastguard Worker return err;
899*f7c14bbaSAndroid Build Coastguard Worker
900*f7c14bbaSAndroid Build Coastguard Worker if (ELF64_ST_BIND(sym->st_info) != STB_LOCAL)
901*f7c14bbaSAndroid Build Coastguard Worker prog->sym_global = true;
902*f7c14bbaSAndroid Build Coastguard Worker
903*f7c14bbaSAndroid Build Coastguard Worker /* if function is a global/weak symbol, but has restricted
904*f7c14bbaSAndroid Build Coastguard Worker * (STV_HIDDEN or STV_INTERNAL) visibility, mark its BTF FUNC
905*f7c14bbaSAndroid Build Coastguard Worker * as static to enable more permissive BPF verification mode
906*f7c14bbaSAndroid Build Coastguard Worker * with more outside context available to BPF verifier
907*f7c14bbaSAndroid Build Coastguard Worker */
908*f7c14bbaSAndroid Build Coastguard Worker if (prog->sym_global && (ELF64_ST_VISIBILITY(sym->st_other) == STV_HIDDEN
909*f7c14bbaSAndroid Build Coastguard Worker || ELF64_ST_VISIBILITY(sym->st_other) == STV_INTERNAL))
910*f7c14bbaSAndroid Build Coastguard Worker prog->mark_btf_static = true;
911*f7c14bbaSAndroid Build Coastguard Worker
912*f7c14bbaSAndroid Build Coastguard Worker nr_progs++;
913*f7c14bbaSAndroid Build Coastguard Worker obj->nr_programs = nr_progs;
914*f7c14bbaSAndroid Build Coastguard Worker }
915*f7c14bbaSAndroid Build Coastguard Worker
916*f7c14bbaSAndroid Build Coastguard Worker return 0;
917*f7c14bbaSAndroid Build Coastguard Worker }
918*f7c14bbaSAndroid Build Coastguard Worker
919*f7c14bbaSAndroid Build Coastguard Worker static const struct btf_member *
find_member_by_offset(const struct btf_type * t,__u32 bit_offset)920*f7c14bbaSAndroid Build Coastguard Worker find_member_by_offset(const struct btf_type *t, __u32 bit_offset)
921*f7c14bbaSAndroid Build Coastguard Worker {
922*f7c14bbaSAndroid Build Coastguard Worker struct btf_member *m;
923*f7c14bbaSAndroid Build Coastguard Worker int i;
924*f7c14bbaSAndroid Build Coastguard Worker
925*f7c14bbaSAndroid Build Coastguard Worker for (i = 0, m = btf_members(t); i < btf_vlen(t); i++, m++) {
926*f7c14bbaSAndroid Build Coastguard Worker if (btf_member_bit_offset(t, i) == bit_offset)
927*f7c14bbaSAndroid Build Coastguard Worker return m;
928*f7c14bbaSAndroid Build Coastguard Worker }
929*f7c14bbaSAndroid Build Coastguard Worker
930*f7c14bbaSAndroid Build Coastguard Worker return NULL;
931*f7c14bbaSAndroid Build Coastguard Worker }
932*f7c14bbaSAndroid Build Coastguard Worker
933*f7c14bbaSAndroid Build Coastguard Worker static const struct btf_member *
find_member_by_name(const struct btf * btf,const struct btf_type * t,const char * name)934*f7c14bbaSAndroid Build Coastguard Worker find_member_by_name(const struct btf *btf, const struct btf_type *t,
935*f7c14bbaSAndroid Build Coastguard Worker const char *name)
936*f7c14bbaSAndroid Build Coastguard Worker {
937*f7c14bbaSAndroid Build Coastguard Worker struct btf_member *m;
938*f7c14bbaSAndroid Build Coastguard Worker int i;
939*f7c14bbaSAndroid Build Coastguard Worker
940*f7c14bbaSAndroid Build Coastguard Worker for (i = 0, m = btf_members(t); i < btf_vlen(t); i++, m++) {
941*f7c14bbaSAndroid Build Coastguard Worker if (!strcmp(btf__name_by_offset(btf, m->name_off), name))
942*f7c14bbaSAndroid Build Coastguard Worker return m;
943*f7c14bbaSAndroid Build Coastguard Worker }
944*f7c14bbaSAndroid Build Coastguard Worker
945*f7c14bbaSAndroid Build Coastguard Worker return NULL;
946*f7c14bbaSAndroid Build Coastguard Worker }
947*f7c14bbaSAndroid Build Coastguard Worker
948*f7c14bbaSAndroid Build Coastguard Worker static int find_ksym_btf_id(struct bpf_object *obj, const char *ksym_name,
949*f7c14bbaSAndroid Build Coastguard Worker __u16 kind, struct btf **res_btf,
950*f7c14bbaSAndroid Build Coastguard Worker struct module_btf **res_mod_btf);
951*f7c14bbaSAndroid Build Coastguard Worker
952*f7c14bbaSAndroid Build Coastguard Worker #define STRUCT_OPS_VALUE_PREFIX "bpf_struct_ops_"
953*f7c14bbaSAndroid Build Coastguard Worker static int find_btf_by_prefix_kind(const struct btf *btf, const char *prefix,
954*f7c14bbaSAndroid Build Coastguard Worker const char *name, __u32 kind);
955*f7c14bbaSAndroid Build Coastguard Worker
956*f7c14bbaSAndroid Build Coastguard Worker static int
find_struct_ops_kern_types(struct bpf_object * obj,const char * tname_raw,struct module_btf ** mod_btf,const struct btf_type ** type,__u32 * type_id,const struct btf_type ** vtype,__u32 * vtype_id,const struct btf_member ** data_member)957*f7c14bbaSAndroid Build Coastguard Worker find_struct_ops_kern_types(struct bpf_object *obj, const char *tname_raw,
958*f7c14bbaSAndroid Build Coastguard Worker struct module_btf **mod_btf,
959*f7c14bbaSAndroid Build Coastguard Worker const struct btf_type **type, __u32 *type_id,
960*f7c14bbaSAndroid Build Coastguard Worker const struct btf_type **vtype, __u32 *vtype_id,
961*f7c14bbaSAndroid Build Coastguard Worker const struct btf_member **data_member)
962*f7c14bbaSAndroid Build Coastguard Worker {
963*f7c14bbaSAndroid Build Coastguard Worker const struct btf_type *kern_type, *kern_vtype;
964*f7c14bbaSAndroid Build Coastguard Worker const struct btf_member *kern_data_member;
965*f7c14bbaSAndroid Build Coastguard Worker struct btf *btf;
966*f7c14bbaSAndroid Build Coastguard Worker __s32 kern_vtype_id, kern_type_id;
967*f7c14bbaSAndroid Build Coastguard Worker char tname[256];
968*f7c14bbaSAndroid Build Coastguard Worker __u32 i;
969*f7c14bbaSAndroid Build Coastguard Worker
970*f7c14bbaSAndroid Build Coastguard Worker snprintf(tname, sizeof(tname), "%.*s",
971*f7c14bbaSAndroid Build Coastguard Worker (int)bpf_core_essential_name_len(tname_raw), tname_raw);
972*f7c14bbaSAndroid Build Coastguard Worker
973*f7c14bbaSAndroid Build Coastguard Worker kern_type_id = find_ksym_btf_id(obj, tname, BTF_KIND_STRUCT,
974*f7c14bbaSAndroid Build Coastguard Worker &btf, mod_btf);
975*f7c14bbaSAndroid Build Coastguard Worker if (kern_type_id < 0) {
976*f7c14bbaSAndroid Build Coastguard Worker pr_warn("struct_ops init_kern: struct %s is not found in kernel BTF\n",
977*f7c14bbaSAndroid Build Coastguard Worker tname);
978*f7c14bbaSAndroid Build Coastguard Worker return kern_type_id;
979*f7c14bbaSAndroid Build Coastguard Worker }
980*f7c14bbaSAndroid Build Coastguard Worker kern_type = btf__type_by_id(btf, kern_type_id);
981*f7c14bbaSAndroid Build Coastguard Worker
982*f7c14bbaSAndroid Build Coastguard Worker /* Find the corresponding "map_value" type that will be used
983*f7c14bbaSAndroid Build Coastguard Worker * in map_update(BPF_MAP_TYPE_STRUCT_OPS). For example,
984*f7c14bbaSAndroid Build Coastguard Worker * find "struct bpf_struct_ops_tcp_congestion_ops" from the
985*f7c14bbaSAndroid Build Coastguard Worker * btf_vmlinux.
986*f7c14bbaSAndroid Build Coastguard Worker */
987*f7c14bbaSAndroid Build Coastguard Worker kern_vtype_id = find_btf_by_prefix_kind(btf, STRUCT_OPS_VALUE_PREFIX,
988*f7c14bbaSAndroid Build Coastguard Worker tname, BTF_KIND_STRUCT);
989*f7c14bbaSAndroid Build Coastguard Worker if (kern_vtype_id < 0) {
990*f7c14bbaSAndroid Build Coastguard Worker pr_warn("struct_ops init_kern: struct %s%s is not found in kernel BTF\n",
991*f7c14bbaSAndroid Build Coastguard Worker STRUCT_OPS_VALUE_PREFIX, tname);
992*f7c14bbaSAndroid Build Coastguard Worker return kern_vtype_id;
993*f7c14bbaSAndroid Build Coastguard Worker }
994*f7c14bbaSAndroid Build Coastguard Worker kern_vtype = btf__type_by_id(btf, kern_vtype_id);
995*f7c14bbaSAndroid Build Coastguard Worker
996*f7c14bbaSAndroid Build Coastguard Worker /* Find "struct tcp_congestion_ops" from
997*f7c14bbaSAndroid Build Coastguard Worker * struct bpf_struct_ops_tcp_congestion_ops {
998*f7c14bbaSAndroid Build Coastguard Worker * [ ... ]
999*f7c14bbaSAndroid Build Coastguard Worker * struct tcp_congestion_ops data;
1000*f7c14bbaSAndroid Build Coastguard Worker * }
1001*f7c14bbaSAndroid Build Coastguard Worker */
1002*f7c14bbaSAndroid Build Coastguard Worker kern_data_member = btf_members(kern_vtype);
1003*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < btf_vlen(kern_vtype); i++, kern_data_member++) {
1004*f7c14bbaSAndroid Build Coastguard Worker if (kern_data_member->type == kern_type_id)
1005*f7c14bbaSAndroid Build Coastguard Worker break;
1006*f7c14bbaSAndroid Build Coastguard Worker }
1007*f7c14bbaSAndroid Build Coastguard Worker if (i == btf_vlen(kern_vtype)) {
1008*f7c14bbaSAndroid Build Coastguard Worker pr_warn("struct_ops init_kern: struct %s data is not found in struct %s%s\n",
1009*f7c14bbaSAndroid Build Coastguard Worker tname, STRUCT_OPS_VALUE_PREFIX, tname);
1010*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
1011*f7c14bbaSAndroid Build Coastguard Worker }
1012*f7c14bbaSAndroid Build Coastguard Worker
1013*f7c14bbaSAndroid Build Coastguard Worker *type = kern_type;
1014*f7c14bbaSAndroid Build Coastguard Worker *type_id = kern_type_id;
1015*f7c14bbaSAndroid Build Coastguard Worker *vtype = kern_vtype;
1016*f7c14bbaSAndroid Build Coastguard Worker *vtype_id = kern_vtype_id;
1017*f7c14bbaSAndroid Build Coastguard Worker *data_member = kern_data_member;
1018*f7c14bbaSAndroid Build Coastguard Worker
1019*f7c14bbaSAndroid Build Coastguard Worker return 0;
1020*f7c14bbaSAndroid Build Coastguard Worker }
1021*f7c14bbaSAndroid Build Coastguard Worker
bpf_map__is_struct_ops(const struct bpf_map * map)1022*f7c14bbaSAndroid Build Coastguard Worker static bool bpf_map__is_struct_ops(const struct bpf_map *map)
1023*f7c14bbaSAndroid Build Coastguard Worker {
1024*f7c14bbaSAndroid Build Coastguard Worker return map->def.type == BPF_MAP_TYPE_STRUCT_OPS;
1025*f7c14bbaSAndroid Build Coastguard Worker }
1026*f7c14bbaSAndroid Build Coastguard Worker
is_valid_st_ops_program(struct bpf_object * obj,const struct bpf_program * prog)1027*f7c14bbaSAndroid Build Coastguard Worker static bool is_valid_st_ops_program(struct bpf_object *obj,
1028*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_program *prog)
1029*f7c14bbaSAndroid Build Coastguard Worker {
1030*f7c14bbaSAndroid Build Coastguard Worker int i;
1031*f7c14bbaSAndroid Build Coastguard Worker
1032*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < obj->nr_programs; i++) {
1033*f7c14bbaSAndroid Build Coastguard Worker if (&obj->programs[i] == prog)
1034*f7c14bbaSAndroid Build Coastguard Worker return prog->type == BPF_PROG_TYPE_STRUCT_OPS;
1035*f7c14bbaSAndroid Build Coastguard Worker }
1036*f7c14bbaSAndroid Build Coastguard Worker
1037*f7c14bbaSAndroid Build Coastguard Worker return false;
1038*f7c14bbaSAndroid Build Coastguard Worker }
1039*f7c14bbaSAndroid Build Coastguard Worker
1040*f7c14bbaSAndroid Build Coastguard Worker /* For each struct_ops program P, referenced from some struct_ops map M,
1041*f7c14bbaSAndroid Build Coastguard Worker * enable P.autoload if there are Ms for which M.autocreate is true,
1042*f7c14bbaSAndroid Build Coastguard Worker * disable P.autoload if for all Ms M.autocreate is false.
1043*f7c14bbaSAndroid Build Coastguard Worker * Don't change P.autoload for programs that are not referenced from any maps.
1044*f7c14bbaSAndroid Build Coastguard Worker */
bpf_object_adjust_struct_ops_autoload(struct bpf_object * obj)1045*f7c14bbaSAndroid Build Coastguard Worker static int bpf_object_adjust_struct_ops_autoload(struct bpf_object *obj)
1046*f7c14bbaSAndroid Build Coastguard Worker {
1047*f7c14bbaSAndroid Build Coastguard Worker struct bpf_program *prog, *slot_prog;
1048*f7c14bbaSAndroid Build Coastguard Worker struct bpf_map *map;
1049*f7c14bbaSAndroid Build Coastguard Worker int i, j, k, vlen;
1050*f7c14bbaSAndroid Build Coastguard Worker
1051*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < obj->nr_programs; ++i) {
1052*f7c14bbaSAndroid Build Coastguard Worker int should_load = false;
1053*f7c14bbaSAndroid Build Coastguard Worker int use_cnt = 0;
1054*f7c14bbaSAndroid Build Coastguard Worker
1055*f7c14bbaSAndroid Build Coastguard Worker prog = &obj->programs[i];
1056*f7c14bbaSAndroid Build Coastguard Worker if (prog->type != BPF_PROG_TYPE_STRUCT_OPS)
1057*f7c14bbaSAndroid Build Coastguard Worker continue;
1058*f7c14bbaSAndroid Build Coastguard Worker
1059*f7c14bbaSAndroid Build Coastguard Worker for (j = 0; j < obj->nr_maps; ++j) {
1060*f7c14bbaSAndroid Build Coastguard Worker map = &obj->maps[j];
1061*f7c14bbaSAndroid Build Coastguard Worker if (!bpf_map__is_struct_ops(map))
1062*f7c14bbaSAndroid Build Coastguard Worker continue;
1063*f7c14bbaSAndroid Build Coastguard Worker
1064*f7c14bbaSAndroid Build Coastguard Worker vlen = btf_vlen(map->st_ops->type);
1065*f7c14bbaSAndroid Build Coastguard Worker for (k = 0; k < vlen; ++k) {
1066*f7c14bbaSAndroid Build Coastguard Worker slot_prog = map->st_ops->progs[k];
1067*f7c14bbaSAndroid Build Coastguard Worker if (prog != slot_prog)
1068*f7c14bbaSAndroid Build Coastguard Worker continue;
1069*f7c14bbaSAndroid Build Coastguard Worker
1070*f7c14bbaSAndroid Build Coastguard Worker use_cnt++;
1071*f7c14bbaSAndroid Build Coastguard Worker if (map->autocreate)
1072*f7c14bbaSAndroid Build Coastguard Worker should_load = true;
1073*f7c14bbaSAndroid Build Coastguard Worker }
1074*f7c14bbaSAndroid Build Coastguard Worker }
1075*f7c14bbaSAndroid Build Coastguard Worker if (use_cnt)
1076*f7c14bbaSAndroid Build Coastguard Worker prog->autoload = should_load;
1077*f7c14bbaSAndroid Build Coastguard Worker }
1078*f7c14bbaSAndroid Build Coastguard Worker
1079*f7c14bbaSAndroid Build Coastguard Worker return 0;
1080*f7c14bbaSAndroid Build Coastguard Worker }
1081*f7c14bbaSAndroid Build Coastguard Worker
1082*f7c14bbaSAndroid Build Coastguard Worker /* Init the map's fields that depend on kern_btf */
bpf_map__init_kern_struct_ops(struct bpf_map * map)1083*f7c14bbaSAndroid Build Coastguard Worker static int bpf_map__init_kern_struct_ops(struct bpf_map *map)
1084*f7c14bbaSAndroid Build Coastguard Worker {
1085*f7c14bbaSAndroid Build Coastguard Worker const struct btf_member *member, *kern_member, *kern_data_member;
1086*f7c14bbaSAndroid Build Coastguard Worker const struct btf_type *type, *kern_type, *kern_vtype;
1087*f7c14bbaSAndroid Build Coastguard Worker __u32 i, kern_type_id, kern_vtype_id, kern_data_off;
1088*f7c14bbaSAndroid Build Coastguard Worker struct bpf_object *obj = map->obj;
1089*f7c14bbaSAndroid Build Coastguard Worker const struct btf *btf = obj->btf;
1090*f7c14bbaSAndroid Build Coastguard Worker struct bpf_struct_ops *st_ops;
1091*f7c14bbaSAndroid Build Coastguard Worker const struct btf *kern_btf;
1092*f7c14bbaSAndroid Build Coastguard Worker struct module_btf *mod_btf;
1093*f7c14bbaSAndroid Build Coastguard Worker void *data, *kern_data;
1094*f7c14bbaSAndroid Build Coastguard Worker const char *tname;
1095*f7c14bbaSAndroid Build Coastguard Worker int err;
1096*f7c14bbaSAndroid Build Coastguard Worker
1097*f7c14bbaSAndroid Build Coastguard Worker st_ops = map->st_ops;
1098*f7c14bbaSAndroid Build Coastguard Worker type = st_ops->type;
1099*f7c14bbaSAndroid Build Coastguard Worker tname = st_ops->tname;
1100*f7c14bbaSAndroid Build Coastguard Worker err = find_struct_ops_kern_types(obj, tname, &mod_btf,
1101*f7c14bbaSAndroid Build Coastguard Worker &kern_type, &kern_type_id,
1102*f7c14bbaSAndroid Build Coastguard Worker &kern_vtype, &kern_vtype_id,
1103*f7c14bbaSAndroid Build Coastguard Worker &kern_data_member);
1104*f7c14bbaSAndroid Build Coastguard Worker if (err)
1105*f7c14bbaSAndroid Build Coastguard Worker return err;
1106*f7c14bbaSAndroid Build Coastguard Worker
1107*f7c14bbaSAndroid Build Coastguard Worker kern_btf = mod_btf ? mod_btf->btf : obj->btf_vmlinux;
1108*f7c14bbaSAndroid Build Coastguard Worker
1109*f7c14bbaSAndroid Build Coastguard Worker pr_debug("struct_ops init_kern %s: type_id:%u kern_type_id:%u kern_vtype_id:%u\n",
1110*f7c14bbaSAndroid Build Coastguard Worker map->name, st_ops->type_id, kern_type_id, kern_vtype_id);
1111*f7c14bbaSAndroid Build Coastguard Worker
1112*f7c14bbaSAndroid Build Coastguard Worker map->mod_btf_fd = mod_btf ? mod_btf->fd : -1;
1113*f7c14bbaSAndroid Build Coastguard Worker map->def.value_size = kern_vtype->size;
1114*f7c14bbaSAndroid Build Coastguard Worker map->btf_vmlinux_value_type_id = kern_vtype_id;
1115*f7c14bbaSAndroid Build Coastguard Worker
1116*f7c14bbaSAndroid Build Coastguard Worker st_ops->kern_vdata = calloc(1, kern_vtype->size);
1117*f7c14bbaSAndroid Build Coastguard Worker if (!st_ops->kern_vdata)
1118*f7c14bbaSAndroid Build Coastguard Worker return -ENOMEM;
1119*f7c14bbaSAndroid Build Coastguard Worker
1120*f7c14bbaSAndroid Build Coastguard Worker data = st_ops->data;
1121*f7c14bbaSAndroid Build Coastguard Worker kern_data_off = kern_data_member->offset / 8;
1122*f7c14bbaSAndroid Build Coastguard Worker kern_data = st_ops->kern_vdata + kern_data_off;
1123*f7c14bbaSAndroid Build Coastguard Worker
1124*f7c14bbaSAndroid Build Coastguard Worker member = btf_members(type);
1125*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < btf_vlen(type); i++, member++) {
1126*f7c14bbaSAndroid Build Coastguard Worker const struct btf_type *mtype, *kern_mtype;
1127*f7c14bbaSAndroid Build Coastguard Worker __u32 mtype_id, kern_mtype_id;
1128*f7c14bbaSAndroid Build Coastguard Worker void *mdata, *kern_mdata;
1129*f7c14bbaSAndroid Build Coastguard Worker struct bpf_program *prog;
1130*f7c14bbaSAndroid Build Coastguard Worker __s64 msize, kern_msize;
1131*f7c14bbaSAndroid Build Coastguard Worker __u32 moff, kern_moff;
1132*f7c14bbaSAndroid Build Coastguard Worker __u32 kern_member_idx;
1133*f7c14bbaSAndroid Build Coastguard Worker const char *mname;
1134*f7c14bbaSAndroid Build Coastguard Worker
1135*f7c14bbaSAndroid Build Coastguard Worker mname = btf__name_by_offset(btf, member->name_off);
1136*f7c14bbaSAndroid Build Coastguard Worker moff = member->offset / 8;
1137*f7c14bbaSAndroid Build Coastguard Worker mdata = data + moff;
1138*f7c14bbaSAndroid Build Coastguard Worker msize = btf__resolve_size(btf, member->type);
1139*f7c14bbaSAndroid Build Coastguard Worker if (msize < 0) {
1140*f7c14bbaSAndroid Build Coastguard Worker pr_warn("struct_ops init_kern %s: failed to resolve the size of member %s\n",
1141*f7c14bbaSAndroid Build Coastguard Worker map->name, mname);
1142*f7c14bbaSAndroid Build Coastguard Worker return msize;
1143*f7c14bbaSAndroid Build Coastguard Worker }
1144*f7c14bbaSAndroid Build Coastguard Worker
1145*f7c14bbaSAndroid Build Coastguard Worker kern_member = find_member_by_name(kern_btf, kern_type, mname);
1146*f7c14bbaSAndroid Build Coastguard Worker if (!kern_member) {
1147*f7c14bbaSAndroid Build Coastguard Worker if (!libbpf_is_mem_zeroed(mdata, msize)) {
1148*f7c14bbaSAndroid Build Coastguard Worker pr_warn("struct_ops init_kern %s: Cannot find member %s in kernel BTF\n",
1149*f7c14bbaSAndroid Build Coastguard Worker map->name, mname);
1150*f7c14bbaSAndroid Build Coastguard Worker return -ENOTSUP;
1151*f7c14bbaSAndroid Build Coastguard Worker }
1152*f7c14bbaSAndroid Build Coastguard Worker
1153*f7c14bbaSAndroid Build Coastguard Worker if (st_ops->progs[i]) {
1154*f7c14bbaSAndroid Build Coastguard Worker /* If we had declaratively set struct_ops callback, we need to
1155*f7c14bbaSAndroid Build Coastguard Worker * force its autoload to false, because it doesn't have
1156*f7c14bbaSAndroid Build Coastguard Worker * a chance of succeeding from POV of the current struct_ops map.
1157*f7c14bbaSAndroid Build Coastguard Worker * If this program is still referenced somewhere else, though,
1158*f7c14bbaSAndroid Build Coastguard Worker * then bpf_object_adjust_struct_ops_autoload() will update its
1159*f7c14bbaSAndroid Build Coastguard Worker * autoload accordingly.
1160*f7c14bbaSAndroid Build Coastguard Worker */
1161*f7c14bbaSAndroid Build Coastguard Worker st_ops->progs[i]->autoload = false;
1162*f7c14bbaSAndroid Build Coastguard Worker st_ops->progs[i] = NULL;
1163*f7c14bbaSAndroid Build Coastguard Worker }
1164*f7c14bbaSAndroid Build Coastguard Worker
1165*f7c14bbaSAndroid Build Coastguard Worker /* Skip all-zero/NULL fields if they are not present in the kernel BTF */
1166*f7c14bbaSAndroid Build Coastguard Worker pr_info("struct_ops %s: member %s not found in kernel, skipping it as it's set to zero\n",
1167*f7c14bbaSAndroid Build Coastguard Worker map->name, mname);
1168*f7c14bbaSAndroid Build Coastguard Worker continue;
1169*f7c14bbaSAndroid Build Coastguard Worker }
1170*f7c14bbaSAndroid Build Coastguard Worker
1171*f7c14bbaSAndroid Build Coastguard Worker kern_member_idx = kern_member - btf_members(kern_type);
1172*f7c14bbaSAndroid Build Coastguard Worker if (btf_member_bitfield_size(type, i) ||
1173*f7c14bbaSAndroid Build Coastguard Worker btf_member_bitfield_size(kern_type, kern_member_idx)) {
1174*f7c14bbaSAndroid Build Coastguard Worker pr_warn("struct_ops init_kern %s: bitfield %s is not supported\n",
1175*f7c14bbaSAndroid Build Coastguard Worker map->name, mname);
1176*f7c14bbaSAndroid Build Coastguard Worker return -ENOTSUP;
1177*f7c14bbaSAndroid Build Coastguard Worker }
1178*f7c14bbaSAndroid Build Coastguard Worker
1179*f7c14bbaSAndroid Build Coastguard Worker kern_moff = kern_member->offset / 8;
1180*f7c14bbaSAndroid Build Coastguard Worker kern_mdata = kern_data + kern_moff;
1181*f7c14bbaSAndroid Build Coastguard Worker
1182*f7c14bbaSAndroid Build Coastguard Worker mtype = skip_mods_and_typedefs(btf, member->type, &mtype_id);
1183*f7c14bbaSAndroid Build Coastguard Worker kern_mtype = skip_mods_and_typedefs(kern_btf, kern_member->type,
1184*f7c14bbaSAndroid Build Coastguard Worker &kern_mtype_id);
1185*f7c14bbaSAndroid Build Coastguard Worker if (BTF_INFO_KIND(mtype->info) !=
1186*f7c14bbaSAndroid Build Coastguard Worker BTF_INFO_KIND(kern_mtype->info)) {
1187*f7c14bbaSAndroid Build Coastguard Worker pr_warn("struct_ops init_kern %s: Unmatched member type %s %u != %u(kernel)\n",
1188*f7c14bbaSAndroid Build Coastguard Worker map->name, mname, BTF_INFO_KIND(mtype->info),
1189*f7c14bbaSAndroid Build Coastguard Worker BTF_INFO_KIND(kern_mtype->info));
1190*f7c14bbaSAndroid Build Coastguard Worker return -ENOTSUP;
1191*f7c14bbaSAndroid Build Coastguard Worker }
1192*f7c14bbaSAndroid Build Coastguard Worker
1193*f7c14bbaSAndroid Build Coastguard Worker if (btf_is_ptr(mtype)) {
1194*f7c14bbaSAndroid Build Coastguard Worker prog = *(void **)mdata;
1195*f7c14bbaSAndroid Build Coastguard Worker /* just like for !kern_member case above, reset declaratively
1196*f7c14bbaSAndroid Build Coastguard Worker * set (at compile time) program's autload to false,
1197*f7c14bbaSAndroid Build Coastguard Worker * if user replaced it with another program or NULL
1198*f7c14bbaSAndroid Build Coastguard Worker */
1199*f7c14bbaSAndroid Build Coastguard Worker if (st_ops->progs[i] && st_ops->progs[i] != prog)
1200*f7c14bbaSAndroid Build Coastguard Worker st_ops->progs[i]->autoload = false;
1201*f7c14bbaSAndroid Build Coastguard Worker
1202*f7c14bbaSAndroid Build Coastguard Worker /* Update the value from the shadow type */
1203*f7c14bbaSAndroid Build Coastguard Worker st_ops->progs[i] = prog;
1204*f7c14bbaSAndroid Build Coastguard Worker if (!prog)
1205*f7c14bbaSAndroid Build Coastguard Worker continue;
1206*f7c14bbaSAndroid Build Coastguard Worker
1207*f7c14bbaSAndroid Build Coastguard Worker if (!is_valid_st_ops_program(obj, prog)) {
1208*f7c14bbaSAndroid Build Coastguard Worker pr_warn("struct_ops init_kern %s: member %s is not a struct_ops program\n",
1209*f7c14bbaSAndroid Build Coastguard Worker map->name, mname);
1210*f7c14bbaSAndroid Build Coastguard Worker return -ENOTSUP;
1211*f7c14bbaSAndroid Build Coastguard Worker }
1212*f7c14bbaSAndroid Build Coastguard Worker
1213*f7c14bbaSAndroid Build Coastguard Worker kern_mtype = skip_mods_and_typedefs(kern_btf,
1214*f7c14bbaSAndroid Build Coastguard Worker kern_mtype->type,
1215*f7c14bbaSAndroid Build Coastguard Worker &kern_mtype_id);
1216*f7c14bbaSAndroid Build Coastguard Worker
1217*f7c14bbaSAndroid Build Coastguard Worker /* mtype->type must be a func_proto which was
1218*f7c14bbaSAndroid Build Coastguard Worker * guaranteed in bpf_object__collect_st_ops_relos(),
1219*f7c14bbaSAndroid Build Coastguard Worker * so only check kern_mtype for func_proto here.
1220*f7c14bbaSAndroid Build Coastguard Worker */
1221*f7c14bbaSAndroid Build Coastguard Worker if (!btf_is_func_proto(kern_mtype)) {
1222*f7c14bbaSAndroid Build Coastguard Worker pr_warn("struct_ops init_kern %s: kernel member %s is not a func ptr\n",
1223*f7c14bbaSAndroid Build Coastguard Worker map->name, mname);
1224*f7c14bbaSAndroid Build Coastguard Worker return -ENOTSUP;
1225*f7c14bbaSAndroid Build Coastguard Worker }
1226*f7c14bbaSAndroid Build Coastguard Worker
1227*f7c14bbaSAndroid Build Coastguard Worker if (mod_btf)
1228*f7c14bbaSAndroid Build Coastguard Worker prog->attach_btf_obj_fd = mod_btf->fd;
1229*f7c14bbaSAndroid Build Coastguard Worker
1230*f7c14bbaSAndroid Build Coastguard Worker /* if we haven't yet processed this BPF program, record proper
1231*f7c14bbaSAndroid Build Coastguard Worker * attach_btf_id and member_idx
1232*f7c14bbaSAndroid Build Coastguard Worker */
1233*f7c14bbaSAndroid Build Coastguard Worker if (!prog->attach_btf_id) {
1234*f7c14bbaSAndroid Build Coastguard Worker prog->attach_btf_id = kern_type_id;
1235*f7c14bbaSAndroid Build Coastguard Worker prog->expected_attach_type = kern_member_idx;
1236*f7c14bbaSAndroid Build Coastguard Worker }
1237*f7c14bbaSAndroid Build Coastguard Worker
1238*f7c14bbaSAndroid Build Coastguard Worker /* struct_ops BPF prog can be re-used between multiple
1239*f7c14bbaSAndroid Build Coastguard Worker * .struct_ops & .struct_ops.link as long as it's the
1240*f7c14bbaSAndroid Build Coastguard Worker * same struct_ops struct definition and the same
1241*f7c14bbaSAndroid Build Coastguard Worker * function pointer field
1242*f7c14bbaSAndroid Build Coastguard Worker */
1243*f7c14bbaSAndroid Build Coastguard Worker if (prog->attach_btf_id != kern_type_id) {
1244*f7c14bbaSAndroid Build Coastguard Worker pr_warn("struct_ops init_kern %s func ptr %s: invalid reuse of prog %s in sec %s with type %u: attach_btf_id %u != kern_type_id %u\n",
1245*f7c14bbaSAndroid Build Coastguard Worker map->name, mname, prog->name, prog->sec_name, prog->type,
1246*f7c14bbaSAndroid Build Coastguard Worker prog->attach_btf_id, kern_type_id);
1247*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
1248*f7c14bbaSAndroid Build Coastguard Worker }
1249*f7c14bbaSAndroid Build Coastguard Worker if (prog->expected_attach_type != kern_member_idx) {
1250*f7c14bbaSAndroid Build Coastguard Worker pr_warn("struct_ops init_kern %s func ptr %s: invalid reuse of prog %s in sec %s with type %u: expected_attach_type %u != kern_member_idx %u\n",
1251*f7c14bbaSAndroid Build Coastguard Worker map->name, mname, prog->name, prog->sec_name, prog->type,
1252*f7c14bbaSAndroid Build Coastguard Worker prog->expected_attach_type, kern_member_idx);
1253*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
1254*f7c14bbaSAndroid Build Coastguard Worker }
1255*f7c14bbaSAndroid Build Coastguard Worker
1256*f7c14bbaSAndroid Build Coastguard Worker st_ops->kern_func_off[i] = kern_data_off + kern_moff;
1257*f7c14bbaSAndroid Build Coastguard Worker
1258*f7c14bbaSAndroid Build Coastguard Worker pr_debug("struct_ops init_kern %s: func ptr %s is set to prog %s from data(+%u) to kern_data(+%u)\n",
1259*f7c14bbaSAndroid Build Coastguard Worker map->name, mname, prog->name, moff,
1260*f7c14bbaSAndroid Build Coastguard Worker kern_moff);
1261*f7c14bbaSAndroid Build Coastguard Worker
1262*f7c14bbaSAndroid Build Coastguard Worker continue;
1263*f7c14bbaSAndroid Build Coastguard Worker }
1264*f7c14bbaSAndroid Build Coastguard Worker
1265*f7c14bbaSAndroid Build Coastguard Worker kern_msize = btf__resolve_size(kern_btf, kern_mtype_id);
1266*f7c14bbaSAndroid Build Coastguard Worker if (kern_msize < 0 || msize != kern_msize) {
1267*f7c14bbaSAndroid Build Coastguard Worker pr_warn("struct_ops init_kern %s: Error in size of member %s: %zd != %zd(kernel)\n",
1268*f7c14bbaSAndroid Build Coastguard Worker map->name, mname, (ssize_t)msize,
1269*f7c14bbaSAndroid Build Coastguard Worker (ssize_t)kern_msize);
1270*f7c14bbaSAndroid Build Coastguard Worker return -ENOTSUP;
1271*f7c14bbaSAndroid Build Coastguard Worker }
1272*f7c14bbaSAndroid Build Coastguard Worker
1273*f7c14bbaSAndroid Build Coastguard Worker pr_debug("struct_ops init_kern %s: copy %s %u bytes from data(+%u) to kern_data(+%u)\n",
1274*f7c14bbaSAndroid Build Coastguard Worker map->name, mname, (unsigned int)msize,
1275*f7c14bbaSAndroid Build Coastguard Worker moff, kern_moff);
1276*f7c14bbaSAndroid Build Coastguard Worker memcpy(kern_mdata, mdata, msize);
1277*f7c14bbaSAndroid Build Coastguard Worker }
1278*f7c14bbaSAndroid Build Coastguard Worker
1279*f7c14bbaSAndroid Build Coastguard Worker return 0;
1280*f7c14bbaSAndroid Build Coastguard Worker }
1281*f7c14bbaSAndroid Build Coastguard Worker
bpf_object__init_kern_struct_ops_maps(struct bpf_object * obj)1282*f7c14bbaSAndroid Build Coastguard Worker static int bpf_object__init_kern_struct_ops_maps(struct bpf_object *obj)
1283*f7c14bbaSAndroid Build Coastguard Worker {
1284*f7c14bbaSAndroid Build Coastguard Worker struct bpf_map *map;
1285*f7c14bbaSAndroid Build Coastguard Worker size_t i;
1286*f7c14bbaSAndroid Build Coastguard Worker int err;
1287*f7c14bbaSAndroid Build Coastguard Worker
1288*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < obj->nr_maps; i++) {
1289*f7c14bbaSAndroid Build Coastguard Worker map = &obj->maps[i];
1290*f7c14bbaSAndroid Build Coastguard Worker
1291*f7c14bbaSAndroid Build Coastguard Worker if (!bpf_map__is_struct_ops(map))
1292*f7c14bbaSAndroid Build Coastguard Worker continue;
1293*f7c14bbaSAndroid Build Coastguard Worker
1294*f7c14bbaSAndroid Build Coastguard Worker if (!map->autocreate)
1295*f7c14bbaSAndroid Build Coastguard Worker continue;
1296*f7c14bbaSAndroid Build Coastguard Worker
1297*f7c14bbaSAndroid Build Coastguard Worker err = bpf_map__init_kern_struct_ops(map);
1298*f7c14bbaSAndroid Build Coastguard Worker if (err)
1299*f7c14bbaSAndroid Build Coastguard Worker return err;
1300*f7c14bbaSAndroid Build Coastguard Worker }
1301*f7c14bbaSAndroid Build Coastguard Worker
1302*f7c14bbaSAndroid Build Coastguard Worker return 0;
1303*f7c14bbaSAndroid Build Coastguard Worker }
1304*f7c14bbaSAndroid Build Coastguard Worker
init_struct_ops_maps(struct bpf_object * obj,const char * sec_name,int shndx,Elf_Data * data)1305*f7c14bbaSAndroid Build Coastguard Worker static int init_struct_ops_maps(struct bpf_object *obj, const char *sec_name,
1306*f7c14bbaSAndroid Build Coastguard Worker int shndx, Elf_Data *data)
1307*f7c14bbaSAndroid Build Coastguard Worker {
1308*f7c14bbaSAndroid Build Coastguard Worker const struct btf_type *type, *datasec;
1309*f7c14bbaSAndroid Build Coastguard Worker const struct btf_var_secinfo *vsi;
1310*f7c14bbaSAndroid Build Coastguard Worker struct bpf_struct_ops *st_ops;
1311*f7c14bbaSAndroid Build Coastguard Worker const char *tname, *var_name;
1312*f7c14bbaSAndroid Build Coastguard Worker __s32 type_id, datasec_id;
1313*f7c14bbaSAndroid Build Coastguard Worker const struct btf *btf;
1314*f7c14bbaSAndroid Build Coastguard Worker struct bpf_map *map;
1315*f7c14bbaSAndroid Build Coastguard Worker __u32 i;
1316*f7c14bbaSAndroid Build Coastguard Worker
1317*f7c14bbaSAndroid Build Coastguard Worker if (shndx == -1)
1318*f7c14bbaSAndroid Build Coastguard Worker return 0;
1319*f7c14bbaSAndroid Build Coastguard Worker
1320*f7c14bbaSAndroid Build Coastguard Worker btf = obj->btf;
1321*f7c14bbaSAndroid Build Coastguard Worker datasec_id = btf__find_by_name_kind(btf, sec_name,
1322*f7c14bbaSAndroid Build Coastguard Worker BTF_KIND_DATASEC);
1323*f7c14bbaSAndroid Build Coastguard Worker if (datasec_id < 0) {
1324*f7c14bbaSAndroid Build Coastguard Worker pr_warn("struct_ops init: DATASEC %s not found\n",
1325*f7c14bbaSAndroid Build Coastguard Worker sec_name);
1326*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
1327*f7c14bbaSAndroid Build Coastguard Worker }
1328*f7c14bbaSAndroid Build Coastguard Worker
1329*f7c14bbaSAndroid Build Coastguard Worker datasec = btf__type_by_id(btf, datasec_id);
1330*f7c14bbaSAndroid Build Coastguard Worker vsi = btf_var_secinfos(datasec);
1331*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < btf_vlen(datasec); i++, vsi++) {
1332*f7c14bbaSAndroid Build Coastguard Worker type = btf__type_by_id(obj->btf, vsi->type);
1333*f7c14bbaSAndroid Build Coastguard Worker var_name = btf__name_by_offset(obj->btf, type->name_off);
1334*f7c14bbaSAndroid Build Coastguard Worker
1335*f7c14bbaSAndroid Build Coastguard Worker type_id = btf__resolve_type(obj->btf, vsi->type);
1336*f7c14bbaSAndroid Build Coastguard Worker if (type_id < 0) {
1337*f7c14bbaSAndroid Build Coastguard Worker pr_warn("struct_ops init: Cannot resolve var type_id %u in DATASEC %s\n",
1338*f7c14bbaSAndroid Build Coastguard Worker vsi->type, sec_name);
1339*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
1340*f7c14bbaSAndroid Build Coastguard Worker }
1341*f7c14bbaSAndroid Build Coastguard Worker
1342*f7c14bbaSAndroid Build Coastguard Worker type = btf__type_by_id(obj->btf, type_id);
1343*f7c14bbaSAndroid Build Coastguard Worker tname = btf__name_by_offset(obj->btf, type->name_off);
1344*f7c14bbaSAndroid Build Coastguard Worker if (!tname[0]) {
1345*f7c14bbaSAndroid Build Coastguard Worker pr_warn("struct_ops init: anonymous type is not supported\n");
1346*f7c14bbaSAndroid Build Coastguard Worker return -ENOTSUP;
1347*f7c14bbaSAndroid Build Coastguard Worker }
1348*f7c14bbaSAndroid Build Coastguard Worker if (!btf_is_struct(type)) {
1349*f7c14bbaSAndroid Build Coastguard Worker pr_warn("struct_ops init: %s is not a struct\n", tname);
1350*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
1351*f7c14bbaSAndroid Build Coastguard Worker }
1352*f7c14bbaSAndroid Build Coastguard Worker
1353*f7c14bbaSAndroid Build Coastguard Worker map = bpf_object__add_map(obj);
1354*f7c14bbaSAndroid Build Coastguard Worker if (IS_ERR(map))
1355*f7c14bbaSAndroid Build Coastguard Worker return PTR_ERR(map);
1356*f7c14bbaSAndroid Build Coastguard Worker
1357*f7c14bbaSAndroid Build Coastguard Worker map->sec_idx = shndx;
1358*f7c14bbaSAndroid Build Coastguard Worker map->sec_offset = vsi->offset;
1359*f7c14bbaSAndroid Build Coastguard Worker map->name = strdup(var_name);
1360*f7c14bbaSAndroid Build Coastguard Worker if (!map->name)
1361*f7c14bbaSAndroid Build Coastguard Worker return -ENOMEM;
1362*f7c14bbaSAndroid Build Coastguard Worker map->btf_value_type_id = type_id;
1363*f7c14bbaSAndroid Build Coastguard Worker
1364*f7c14bbaSAndroid Build Coastguard Worker /* Follow same convention as for programs autoload:
1365*f7c14bbaSAndroid Build Coastguard Worker * SEC("?.struct_ops") means map is not created by default.
1366*f7c14bbaSAndroid Build Coastguard Worker */
1367*f7c14bbaSAndroid Build Coastguard Worker if (sec_name[0] == '?') {
1368*f7c14bbaSAndroid Build Coastguard Worker map->autocreate = false;
1369*f7c14bbaSAndroid Build Coastguard Worker /* from now on forget there was ? in section name */
1370*f7c14bbaSAndroid Build Coastguard Worker sec_name++;
1371*f7c14bbaSAndroid Build Coastguard Worker }
1372*f7c14bbaSAndroid Build Coastguard Worker
1373*f7c14bbaSAndroid Build Coastguard Worker map->def.type = BPF_MAP_TYPE_STRUCT_OPS;
1374*f7c14bbaSAndroid Build Coastguard Worker map->def.key_size = sizeof(int);
1375*f7c14bbaSAndroid Build Coastguard Worker map->def.value_size = type->size;
1376*f7c14bbaSAndroid Build Coastguard Worker map->def.max_entries = 1;
1377*f7c14bbaSAndroid Build Coastguard Worker map->def.map_flags = strcmp(sec_name, STRUCT_OPS_LINK_SEC) == 0 ? BPF_F_LINK : 0;
1378*f7c14bbaSAndroid Build Coastguard Worker
1379*f7c14bbaSAndroid Build Coastguard Worker map->st_ops = calloc(1, sizeof(*map->st_ops));
1380*f7c14bbaSAndroid Build Coastguard Worker if (!map->st_ops)
1381*f7c14bbaSAndroid Build Coastguard Worker return -ENOMEM;
1382*f7c14bbaSAndroid Build Coastguard Worker st_ops = map->st_ops;
1383*f7c14bbaSAndroid Build Coastguard Worker st_ops->data = malloc(type->size);
1384*f7c14bbaSAndroid Build Coastguard Worker st_ops->progs = calloc(btf_vlen(type), sizeof(*st_ops->progs));
1385*f7c14bbaSAndroid Build Coastguard Worker st_ops->kern_func_off = malloc(btf_vlen(type) *
1386*f7c14bbaSAndroid Build Coastguard Worker sizeof(*st_ops->kern_func_off));
1387*f7c14bbaSAndroid Build Coastguard Worker if (!st_ops->data || !st_ops->progs || !st_ops->kern_func_off)
1388*f7c14bbaSAndroid Build Coastguard Worker return -ENOMEM;
1389*f7c14bbaSAndroid Build Coastguard Worker
1390*f7c14bbaSAndroid Build Coastguard Worker if (vsi->offset + type->size > data->d_size) {
1391*f7c14bbaSAndroid Build Coastguard Worker pr_warn("struct_ops init: var %s is beyond the end of DATASEC %s\n",
1392*f7c14bbaSAndroid Build Coastguard Worker var_name, sec_name);
1393*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
1394*f7c14bbaSAndroid Build Coastguard Worker }
1395*f7c14bbaSAndroid Build Coastguard Worker
1396*f7c14bbaSAndroid Build Coastguard Worker memcpy(st_ops->data,
1397*f7c14bbaSAndroid Build Coastguard Worker data->d_buf + vsi->offset,
1398*f7c14bbaSAndroid Build Coastguard Worker type->size);
1399*f7c14bbaSAndroid Build Coastguard Worker st_ops->tname = tname;
1400*f7c14bbaSAndroid Build Coastguard Worker st_ops->type = type;
1401*f7c14bbaSAndroid Build Coastguard Worker st_ops->type_id = type_id;
1402*f7c14bbaSAndroid Build Coastguard Worker
1403*f7c14bbaSAndroid Build Coastguard Worker pr_debug("struct_ops init: struct %s(type_id=%u) %s found at offset %u\n",
1404*f7c14bbaSAndroid Build Coastguard Worker tname, type_id, var_name, vsi->offset);
1405*f7c14bbaSAndroid Build Coastguard Worker }
1406*f7c14bbaSAndroid Build Coastguard Worker
1407*f7c14bbaSAndroid Build Coastguard Worker return 0;
1408*f7c14bbaSAndroid Build Coastguard Worker }
1409*f7c14bbaSAndroid Build Coastguard Worker
bpf_object_init_struct_ops(struct bpf_object * obj)1410*f7c14bbaSAndroid Build Coastguard Worker static int bpf_object_init_struct_ops(struct bpf_object *obj)
1411*f7c14bbaSAndroid Build Coastguard Worker {
1412*f7c14bbaSAndroid Build Coastguard Worker const char *sec_name;
1413*f7c14bbaSAndroid Build Coastguard Worker int sec_idx, err;
1414*f7c14bbaSAndroid Build Coastguard Worker
1415*f7c14bbaSAndroid Build Coastguard Worker for (sec_idx = 0; sec_idx < obj->efile.sec_cnt; ++sec_idx) {
1416*f7c14bbaSAndroid Build Coastguard Worker struct elf_sec_desc *desc = &obj->efile.secs[sec_idx];
1417*f7c14bbaSAndroid Build Coastguard Worker
1418*f7c14bbaSAndroid Build Coastguard Worker if (desc->sec_type != SEC_ST_OPS)
1419*f7c14bbaSAndroid Build Coastguard Worker continue;
1420*f7c14bbaSAndroid Build Coastguard Worker
1421*f7c14bbaSAndroid Build Coastguard Worker sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, sec_idx));
1422*f7c14bbaSAndroid Build Coastguard Worker if (!sec_name)
1423*f7c14bbaSAndroid Build Coastguard Worker return -LIBBPF_ERRNO__FORMAT;
1424*f7c14bbaSAndroid Build Coastguard Worker
1425*f7c14bbaSAndroid Build Coastguard Worker err = init_struct_ops_maps(obj, sec_name, sec_idx, desc->data);
1426*f7c14bbaSAndroid Build Coastguard Worker if (err)
1427*f7c14bbaSAndroid Build Coastguard Worker return err;
1428*f7c14bbaSAndroid Build Coastguard Worker }
1429*f7c14bbaSAndroid Build Coastguard Worker
1430*f7c14bbaSAndroid Build Coastguard Worker return 0;
1431*f7c14bbaSAndroid Build Coastguard Worker }
1432*f7c14bbaSAndroid Build Coastguard Worker
bpf_object__new(const char * path,const void * obj_buf,size_t obj_buf_sz,const char * obj_name)1433*f7c14bbaSAndroid Build Coastguard Worker static struct bpf_object *bpf_object__new(const char *path,
1434*f7c14bbaSAndroid Build Coastguard Worker const void *obj_buf,
1435*f7c14bbaSAndroid Build Coastguard Worker size_t obj_buf_sz,
1436*f7c14bbaSAndroid Build Coastguard Worker const char *obj_name)
1437*f7c14bbaSAndroid Build Coastguard Worker {
1438*f7c14bbaSAndroid Build Coastguard Worker struct bpf_object *obj;
1439*f7c14bbaSAndroid Build Coastguard Worker char *end;
1440*f7c14bbaSAndroid Build Coastguard Worker
1441*f7c14bbaSAndroid Build Coastguard Worker obj = calloc(1, sizeof(struct bpf_object) + strlen(path) + 1);
1442*f7c14bbaSAndroid Build Coastguard Worker if (!obj) {
1443*f7c14bbaSAndroid Build Coastguard Worker pr_warn("alloc memory failed for %s\n", path);
1444*f7c14bbaSAndroid Build Coastguard Worker return ERR_PTR(-ENOMEM);
1445*f7c14bbaSAndroid Build Coastguard Worker }
1446*f7c14bbaSAndroid Build Coastguard Worker
1447*f7c14bbaSAndroid Build Coastguard Worker strcpy(obj->path, path);
1448*f7c14bbaSAndroid Build Coastguard Worker if (obj_name) {
1449*f7c14bbaSAndroid Build Coastguard Worker libbpf_strlcpy(obj->name, obj_name, sizeof(obj->name));
1450*f7c14bbaSAndroid Build Coastguard Worker } else {
1451*f7c14bbaSAndroid Build Coastguard Worker /* Using basename() GNU version which doesn't modify arg. */
1452*f7c14bbaSAndroid Build Coastguard Worker libbpf_strlcpy(obj->name, basename((void *)path), sizeof(obj->name));
1453*f7c14bbaSAndroid Build Coastguard Worker end = strchr(obj->name, '.');
1454*f7c14bbaSAndroid Build Coastguard Worker if (end)
1455*f7c14bbaSAndroid Build Coastguard Worker *end = 0;
1456*f7c14bbaSAndroid Build Coastguard Worker }
1457*f7c14bbaSAndroid Build Coastguard Worker
1458*f7c14bbaSAndroid Build Coastguard Worker obj->efile.fd = -1;
1459*f7c14bbaSAndroid Build Coastguard Worker /*
1460*f7c14bbaSAndroid Build Coastguard Worker * Caller of this function should also call
1461*f7c14bbaSAndroid Build Coastguard Worker * bpf_object__elf_finish() after data collection to return
1462*f7c14bbaSAndroid Build Coastguard Worker * obj_buf to user. If not, we should duplicate the buffer to
1463*f7c14bbaSAndroid Build Coastguard Worker * avoid user freeing them before elf finish.
1464*f7c14bbaSAndroid Build Coastguard Worker */
1465*f7c14bbaSAndroid Build Coastguard Worker obj->efile.obj_buf = obj_buf;
1466*f7c14bbaSAndroid Build Coastguard Worker obj->efile.obj_buf_sz = obj_buf_sz;
1467*f7c14bbaSAndroid Build Coastguard Worker obj->efile.btf_maps_shndx = -1;
1468*f7c14bbaSAndroid Build Coastguard Worker obj->kconfig_map_idx = -1;
1469*f7c14bbaSAndroid Build Coastguard Worker
1470*f7c14bbaSAndroid Build Coastguard Worker obj->kern_version = get_kernel_version();
1471*f7c14bbaSAndroid Build Coastguard Worker obj->loaded = false;
1472*f7c14bbaSAndroid Build Coastguard Worker
1473*f7c14bbaSAndroid Build Coastguard Worker return obj;
1474*f7c14bbaSAndroid Build Coastguard Worker }
1475*f7c14bbaSAndroid Build Coastguard Worker
bpf_object__elf_finish(struct bpf_object * obj)1476*f7c14bbaSAndroid Build Coastguard Worker static void bpf_object__elf_finish(struct bpf_object *obj)
1477*f7c14bbaSAndroid Build Coastguard Worker {
1478*f7c14bbaSAndroid Build Coastguard Worker if (!obj->efile.elf)
1479*f7c14bbaSAndroid Build Coastguard Worker return;
1480*f7c14bbaSAndroid Build Coastguard Worker
1481*f7c14bbaSAndroid Build Coastguard Worker elf_end(obj->efile.elf);
1482*f7c14bbaSAndroid Build Coastguard Worker obj->efile.elf = NULL;
1483*f7c14bbaSAndroid Build Coastguard Worker obj->efile.symbols = NULL;
1484*f7c14bbaSAndroid Build Coastguard Worker obj->efile.arena_data = NULL;
1485*f7c14bbaSAndroid Build Coastguard Worker
1486*f7c14bbaSAndroid Build Coastguard Worker zfree(&obj->efile.secs);
1487*f7c14bbaSAndroid Build Coastguard Worker obj->efile.sec_cnt = 0;
1488*f7c14bbaSAndroid Build Coastguard Worker zclose(obj->efile.fd);
1489*f7c14bbaSAndroid Build Coastguard Worker obj->efile.obj_buf = NULL;
1490*f7c14bbaSAndroid Build Coastguard Worker obj->efile.obj_buf_sz = 0;
1491*f7c14bbaSAndroid Build Coastguard Worker }
1492*f7c14bbaSAndroid Build Coastguard Worker
bpf_object__elf_init(struct bpf_object * obj)1493*f7c14bbaSAndroid Build Coastguard Worker static int bpf_object__elf_init(struct bpf_object *obj)
1494*f7c14bbaSAndroid Build Coastguard Worker {
1495*f7c14bbaSAndroid Build Coastguard Worker Elf64_Ehdr *ehdr;
1496*f7c14bbaSAndroid Build Coastguard Worker int err = 0;
1497*f7c14bbaSAndroid Build Coastguard Worker Elf *elf;
1498*f7c14bbaSAndroid Build Coastguard Worker
1499*f7c14bbaSAndroid Build Coastguard Worker if (obj->efile.elf) {
1500*f7c14bbaSAndroid Build Coastguard Worker pr_warn("elf: init internal error\n");
1501*f7c14bbaSAndroid Build Coastguard Worker return -LIBBPF_ERRNO__LIBELF;
1502*f7c14bbaSAndroid Build Coastguard Worker }
1503*f7c14bbaSAndroid Build Coastguard Worker
1504*f7c14bbaSAndroid Build Coastguard Worker if (obj->efile.obj_buf_sz > 0) {
1505*f7c14bbaSAndroid Build Coastguard Worker /* obj_buf should have been validated by bpf_object__open_mem(). */
1506*f7c14bbaSAndroid Build Coastguard Worker elf = elf_memory((char *)obj->efile.obj_buf, obj->efile.obj_buf_sz);
1507*f7c14bbaSAndroid Build Coastguard Worker } else {
1508*f7c14bbaSAndroid Build Coastguard Worker obj->efile.fd = open(obj->path, O_RDONLY | O_CLOEXEC);
1509*f7c14bbaSAndroid Build Coastguard Worker if (obj->efile.fd < 0) {
1510*f7c14bbaSAndroid Build Coastguard Worker char errmsg[STRERR_BUFSIZE], *cp;
1511*f7c14bbaSAndroid Build Coastguard Worker
1512*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
1513*f7c14bbaSAndroid Build Coastguard Worker cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg));
1514*f7c14bbaSAndroid Build Coastguard Worker pr_warn("elf: failed to open %s: %s\n", obj->path, cp);
1515*f7c14bbaSAndroid Build Coastguard Worker return err;
1516*f7c14bbaSAndroid Build Coastguard Worker }
1517*f7c14bbaSAndroid Build Coastguard Worker
1518*f7c14bbaSAndroid Build Coastguard Worker elf = elf_begin(obj->efile.fd, ELF_C_READ_MMAP, NULL);
1519*f7c14bbaSAndroid Build Coastguard Worker }
1520*f7c14bbaSAndroid Build Coastguard Worker
1521*f7c14bbaSAndroid Build Coastguard Worker if (!elf) {
1522*f7c14bbaSAndroid Build Coastguard Worker pr_warn("elf: failed to open %s as ELF file: %s\n", obj->path, elf_errmsg(-1));
1523*f7c14bbaSAndroid Build Coastguard Worker err = -LIBBPF_ERRNO__LIBELF;
1524*f7c14bbaSAndroid Build Coastguard Worker goto errout;
1525*f7c14bbaSAndroid Build Coastguard Worker }
1526*f7c14bbaSAndroid Build Coastguard Worker
1527*f7c14bbaSAndroid Build Coastguard Worker obj->efile.elf = elf;
1528*f7c14bbaSAndroid Build Coastguard Worker
1529*f7c14bbaSAndroid Build Coastguard Worker if (elf_kind(elf) != ELF_K_ELF) {
1530*f7c14bbaSAndroid Build Coastguard Worker err = -LIBBPF_ERRNO__FORMAT;
1531*f7c14bbaSAndroid Build Coastguard Worker pr_warn("elf: '%s' is not a proper ELF object\n", obj->path);
1532*f7c14bbaSAndroid Build Coastguard Worker goto errout;
1533*f7c14bbaSAndroid Build Coastguard Worker }
1534*f7c14bbaSAndroid Build Coastguard Worker
1535*f7c14bbaSAndroid Build Coastguard Worker if (gelf_getclass(elf) != ELFCLASS64) {
1536*f7c14bbaSAndroid Build Coastguard Worker err = -LIBBPF_ERRNO__FORMAT;
1537*f7c14bbaSAndroid Build Coastguard Worker pr_warn("elf: '%s' is not a 64-bit ELF object\n", obj->path);
1538*f7c14bbaSAndroid Build Coastguard Worker goto errout;
1539*f7c14bbaSAndroid Build Coastguard Worker }
1540*f7c14bbaSAndroid Build Coastguard Worker
1541*f7c14bbaSAndroid Build Coastguard Worker obj->efile.ehdr = ehdr = elf64_getehdr(elf);
1542*f7c14bbaSAndroid Build Coastguard Worker if (!obj->efile.ehdr) {
1543*f7c14bbaSAndroid Build Coastguard Worker pr_warn("elf: failed to get ELF header from %s: %s\n", obj->path, elf_errmsg(-1));
1544*f7c14bbaSAndroid Build Coastguard Worker err = -LIBBPF_ERRNO__FORMAT;
1545*f7c14bbaSAndroid Build Coastguard Worker goto errout;
1546*f7c14bbaSAndroid Build Coastguard Worker }
1547*f7c14bbaSAndroid Build Coastguard Worker
1548*f7c14bbaSAndroid Build Coastguard Worker if (elf_getshdrstrndx(elf, &obj->efile.shstrndx)) {
1549*f7c14bbaSAndroid Build Coastguard Worker pr_warn("elf: failed to get section names section index for %s: %s\n",
1550*f7c14bbaSAndroid Build Coastguard Worker obj->path, elf_errmsg(-1));
1551*f7c14bbaSAndroid Build Coastguard Worker err = -LIBBPF_ERRNO__FORMAT;
1552*f7c14bbaSAndroid Build Coastguard Worker goto errout;
1553*f7c14bbaSAndroid Build Coastguard Worker }
1554*f7c14bbaSAndroid Build Coastguard Worker
1555*f7c14bbaSAndroid Build Coastguard Worker /* ELF is corrupted/truncated, avoid calling elf_strptr. */
1556*f7c14bbaSAndroid Build Coastguard Worker if (!elf_rawdata(elf_getscn(elf, obj->efile.shstrndx), NULL)) {
1557*f7c14bbaSAndroid Build Coastguard Worker pr_warn("elf: failed to get section names strings from %s: %s\n",
1558*f7c14bbaSAndroid Build Coastguard Worker obj->path, elf_errmsg(-1));
1559*f7c14bbaSAndroid Build Coastguard Worker err = -LIBBPF_ERRNO__FORMAT;
1560*f7c14bbaSAndroid Build Coastguard Worker goto errout;
1561*f7c14bbaSAndroid Build Coastguard Worker }
1562*f7c14bbaSAndroid Build Coastguard Worker
1563*f7c14bbaSAndroid Build Coastguard Worker /* Old LLVM set e_machine to EM_NONE */
1564*f7c14bbaSAndroid Build Coastguard Worker if (ehdr->e_type != ET_REL || (ehdr->e_machine && ehdr->e_machine != EM_BPF)) {
1565*f7c14bbaSAndroid Build Coastguard Worker pr_warn("elf: %s is not a valid eBPF object file\n", obj->path);
1566*f7c14bbaSAndroid Build Coastguard Worker err = -LIBBPF_ERRNO__FORMAT;
1567*f7c14bbaSAndroid Build Coastguard Worker goto errout;
1568*f7c14bbaSAndroid Build Coastguard Worker }
1569*f7c14bbaSAndroid Build Coastguard Worker
1570*f7c14bbaSAndroid Build Coastguard Worker return 0;
1571*f7c14bbaSAndroid Build Coastguard Worker errout:
1572*f7c14bbaSAndroid Build Coastguard Worker bpf_object__elf_finish(obj);
1573*f7c14bbaSAndroid Build Coastguard Worker return err;
1574*f7c14bbaSAndroid Build Coastguard Worker }
1575*f7c14bbaSAndroid Build Coastguard Worker
bpf_object__check_endianness(struct bpf_object * obj)1576*f7c14bbaSAndroid Build Coastguard Worker static int bpf_object__check_endianness(struct bpf_object *obj)
1577*f7c14bbaSAndroid Build Coastguard Worker {
1578*f7c14bbaSAndroid Build Coastguard Worker #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
1579*f7c14bbaSAndroid Build Coastguard Worker if (obj->efile.ehdr->e_ident[EI_DATA] == ELFDATA2LSB)
1580*f7c14bbaSAndroid Build Coastguard Worker return 0;
1581*f7c14bbaSAndroid Build Coastguard Worker #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
1582*f7c14bbaSAndroid Build Coastguard Worker if (obj->efile.ehdr->e_ident[EI_DATA] == ELFDATA2MSB)
1583*f7c14bbaSAndroid Build Coastguard Worker return 0;
1584*f7c14bbaSAndroid Build Coastguard Worker #else
1585*f7c14bbaSAndroid Build Coastguard Worker # error "Unrecognized __BYTE_ORDER__"
1586*f7c14bbaSAndroid Build Coastguard Worker #endif
1587*f7c14bbaSAndroid Build Coastguard Worker pr_warn("elf: endianness mismatch in %s.\n", obj->path);
1588*f7c14bbaSAndroid Build Coastguard Worker return -LIBBPF_ERRNO__ENDIAN;
1589*f7c14bbaSAndroid Build Coastguard Worker }
1590*f7c14bbaSAndroid Build Coastguard Worker
1591*f7c14bbaSAndroid Build Coastguard Worker static int
bpf_object__init_license(struct bpf_object * obj,void * data,size_t size)1592*f7c14bbaSAndroid Build Coastguard Worker bpf_object__init_license(struct bpf_object *obj, void *data, size_t size)
1593*f7c14bbaSAndroid Build Coastguard Worker {
1594*f7c14bbaSAndroid Build Coastguard Worker if (!data) {
1595*f7c14bbaSAndroid Build Coastguard Worker pr_warn("invalid license section in %s\n", obj->path);
1596*f7c14bbaSAndroid Build Coastguard Worker return -LIBBPF_ERRNO__FORMAT;
1597*f7c14bbaSAndroid Build Coastguard Worker }
1598*f7c14bbaSAndroid Build Coastguard Worker /* libbpf_strlcpy() only copies first N - 1 bytes, so size + 1 won't
1599*f7c14bbaSAndroid Build Coastguard Worker * go over allowed ELF data section buffer
1600*f7c14bbaSAndroid Build Coastguard Worker */
1601*f7c14bbaSAndroid Build Coastguard Worker libbpf_strlcpy(obj->license, data, min(size + 1, sizeof(obj->license)));
1602*f7c14bbaSAndroid Build Coastguard Worker pr_debug("license of %s is %s\n", obj->path, obj->license);
1603*f7c14bbaSAndroid Build Coastguard Worker return 0;
1604*f7c14bbaSAndroid Build Coastguard Worker }
1605*f7c14bbaSAndroid Build Coastguard Worker
1606*f7c14bbaSAndroid Build Coastguard Worker static int
bpf_object__init_kversion(struct bpf_object * obj,void * data,size_t size)1607*f7c14bbaSAndroid Build Coastguard Worker bpf_object__init_kversion(struct bpf_object *obj, void *data, size_t size)
1608*f7c14bbaSAndroid Build Coastguard Worker {
1609*f7c14bbaSAndroid Build Coastguard Worker __u32 kver;
1610*f7c14bbaSAndroid Build Coastguard Worker
1611*f7c14bbaSAndroid Build Coastguard Worker if (!data || size != sizeof(kver)) {
1612*f7c14bbaSAndroid Build Coastguard Worker pr_warn("invalid kver section in %s\n", obj->path);
1613*f7c14bbaSAndroid Build Coastguard Worker return -LIBBPF_ERRNO__FORMAT;
1614*f7c14bbaSAndroid Build Coastguard Worker }
1615*f7c14bbaSAndroid Build Coastguard Worker memcpy(&kver, data, sizeof(kver));
1616*f7c14bbaSAndroid Build Coastguard Worker obj->kern_version = kver;
1617*f7c14bbaSAndroid Build Coastguard Worker pr_debug("kernel version of %s is %x\n", obj->path, obj->kern_version);
1618*f7c14bbaSAndroid Build Coastguard Worker return 0;
1619*f7c14bbaSAndroid Build Coastguard Worker }
1620*f7c14bbaSAndroid Build Coastguard Worker
bpf_map_type__is_map_in_map(enum bpf_map_type type)1621*f7c14bbaSAndroid Build Coastguard Worker static bool bpf_map_type__is_map_in_map(enum bpf_map_type type)
1622*f7c14bbaSAndroid Build Coastguard Worker {
1623*f7c14bbaSAndroid Build Coastguard Worker if (type == BPF_MAP_TYPE_ARRAY_OF_MAPS ||
1624*f7c14bbaSAndroid Build Coastguard Worker type == BPF_MAP_TYPE_HASH_OF_MAPS)
1625*f7c14bbaSAndroid Build Coastguard Worker return true;
1626*f7c14bbaSAndroid Build Coastguard Worker return false;
1627*f7c14bbaSAndroid Build Coastguard Worker }
1628*f7c14bbaSAndroid Build Coastguard Worker
find_elf_sec_sz(const struct bpf_object * obj,const char * name,__u32 * size)1629*f7c14bbaSAndroid Build Coastguard Worker static int find_elf_sec_sz(const struct bpf_object *obj, const char *name, __u32 *size)
1630*f7c14bbaSAndroid Build Coastguard Worker {
1631*f7c14bbaSAndroid Build Coastguard Worker Elf_Data *data;
1632*f7c14bbaSAndroid Build Coastguard Worker Elf_Scn *scn;
1633*f7c14bbaSAndroid Build Coastguard Worker
1634*f7c14bbaSAndroid Build Coastguard Worker if (!name)
1635*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
1636*f7c14bbaSAndroid Build Coastguard Worker
1637*f7c14bbaSAndroid Build Coastguard Worker scn = elf_sec_by_name(obj, name);
1638*f7c14bbaSAndroid Build Coastguard Worker data = elf_sec_data(obj, scn);
1639*f7c14bbaSAndroid Build Coastguard Worker if (data) {
1640*f7c14bbaSAndroid Build Coastguard Worker *size = data->d_size;
1641*f7c14bbaSAndroid Build Coastguard Worker return 0; /* found it */
1642*f7c14bbaSAndroid Build Coastguard Worker }
1643*f7c14bbaSAndroid Build Coastguard Worker
1644*f7c14bbaSAndroid Build Coastguard Worker return -ENOENT;
1645*f7c14bbaSAndroid Build Coastguard Worker }
1646*f7c14bbaSAndroid Build Coastguard Worker
find_elf_var_sym(const struct bpf_object * obj,const char * name)1647*f7c14bbaSAndroid Build Coastguard Worker static Elf64_Sym *find_elf_var_sym(const struct bpf_object *obj, const char *name)
1648*f7c14bbaSAndroid Build Coastguard Worker {
1649*f7c14bbaSAndroid Build Coastguard Worker Elf_Data *symbols = obj->efile.symbols;
1650*f7c14bbaSAndroid Build Coastguard Worker const char *sname;
1651*f7c14bbaSAndroid Build Coastguard Worker size_t si;
1652*f7c14bbaSAndroid Build Coastguard Worker
1653*f7c14bbaSAndroid Build Coastguard Worker for (si = 0; si < symbols->d_size / sizeof(Elf64_Sym); si++) {
1654*f7c14bbaSAndroid Build Coastguard Worker Elf64_Sym *sym = elf_sym_by_idx(obj, si);
1655*f7c14bbaSAndroid Build Coastguard Worker
1656*f7c14bbaSAndroid Build Coastguard Worker if (ELF64_ST_TYPE(sym->st_info) != STT_OBJECT)
1657*f7c14bbaSAndroid Build Coastguard Worker continue;
1658*f7c14bbaSAndroid Build Coastguard Worker
1659*f7c14bbaSAndroid Build Coastguard Worker if (ELF64_ST_BIND(sym->st_info) != STB_GLOBAL &&
1660*f7c14bbaSAndroid Build Coastguard Worker ELF64_ST_BIND(sym->st_info) != STB_WEAK)
1661*f7c14bbaSAndroid Build Coastguard Worker continue;
1662*f7c14bbaSAndroid Build Coastguard Worker
1663*f7c14bbaSAndroid Build Coastguard Worker sname = elf_sym_str(obj, sym->st_name);
1664*f7c14bbaSAndroid Build Coastguard Worker if (!sname) {
1665*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to get sym name string for var %s\n", name);
1666*f7c14bbaSAndroid Build Coastguard Worker return ERR_PTR(-EIO);
1667*f7c14bbaSAndroid Build Coastguard Worker }
1668*f7c14bbaSAndroid Build Coastguard Worker if (strcmp(name, sname) == 0)
1669*f7c14bbaSAndroid Build Coastguard Worker return sym;
1670*f7c14bbaSAndroid Build Coastguard Worker }
1671*f7c14bbaSAndroid Build Coastguard Worker
1672*f7c14bbaSAndroid Build Coastguard Worker return ERR_PTR(-ENOENT);
1673*f7c14bbaSAndroid Build Coastguard Worker }
1674*f7c14bbaSAndroid Build Coastguard Worker
1675*f7c14bbaSAndroid Build Coastguard Worker /* Some versions of Android don't provide memfd_create() in their libc
1676*f7c14bbaSAndroid Build Coastguard Worker * implementation, so avoid complications and just go straight to Linux
1677*f7c14bbaSAndroid Build Coastguard Worker * syscall.
1678*f7c14bbaSAndroid Build Coastguard Worker */
sys_memfd_create(const char * name,unsigned flags)1679*f7c14bbaSAndroid Build Coastguard Worker static int sys_memfd_create(const char *name, unsigned flags)
1680*f7c14bbaSAndroid Build Coastguard Worker {
1681*f7c14bbaSAndroid Build Coastguard Worker return syscall(__NR_memfd_create, name, flags);
1682*f7c14bbaSAndroid Build Coastguard Worker }
1683*f7c14bbaSAndroid Build Coastguard Worker
1684*f7c14bbaSAndroid Build Coastguard Worker #ifndef MFD_CLOEXEC
1685*f7c14bbaSAndroid Build Coastguard Worker #define MFD_CLOEXEC 0x0001U
1686*f7c14bbaSAndroid Build Coastguard Worker #endif
1687*f7c14bbaSAndroid Build Coastguard Worker
create_placeholder_fd(void)1688*f7c14bbaSAndroid Build Coastguard Worker static int create_placeholder_fd(void)
1689*f7c14bbaSAndroid Build Coastguard Worker {
1690*f7c14bbaSAndroid Build Coastguard Worker int fd;
1691*f7c14bbaSAndroid Build Coastguard Worker
1692*f7c14bbaSAndroid Build Coastguard Worker fd = ensure_good_fd(sys_memfd_create("libbpf-placeholder-fd", MFD_CLOEXEC));
1693*f7c14bbaSAndroid Build Coastguard Worker if (fd < 0)
1694*f7c14bbaSAndroid Build Coastguard Worker return -errno;
1695*f7c14bbaSAndroid Build Coastguard Worker return fd;
1696*f7c14bbaSAndroid Build Coastguard Worker }
1697*f7c14bbaSAndroid Build Coastguard Worker
bpf_object__add_map(struct bpf_object * obj)1698*f7c14bbaSAndroid Build Coastguard Worker static struct bpf_map *bpf_object__add_map(struct bpf_object *obj)
1699*f7c14bbaSAndroid Build Coastguard Worker {
1700*f7c14bbaSAndroid Build Coastguard Worker struct bpf_map *map;
1701*f7c14bbaSAndroid Build Coastguard Worker int err;
1702*f7c14bbaSAndroid Build Coastguard Worker
1703*f7c14bbaSAndroid Build Coastguard Worker err = libbpf_ensure_mem((void **)&obj->maps, &obj->maps_cap,
1704*f7c14bbaSAndroid Build Coastguard Worker sizeof(*obj->maps), obj->nr_maps + 1);
1705*f7c14bbaSAndroid Build Coastguard Worker if (err)
1706*f7c14bbaSAndroid Build Coastguard Worker return ERR_PTR(err);
1707*f7c14bbaSAndroid Build Coastguard Worker
1708*f7c14bbaSAndroid Build Coastguard Worker map = &obj->maps[obj->nr_maps++];
1709*f7c14bbaSAndroid Build Coastguard Worker map->obj = obj;
1710*f7c14bbaSAndroid Build Coastguard Worker /* Preallocate map FD without actually creating BPF map just yet.
1711*f7c14bbaSAndroid Build Coastguard Worker * These map FD "placeholders" will be reused later without changing
1712*f7c14bbaSAndroid Build Coastguard Worker * FD value when map is actually created in the kernel.
1713*f7c14bbaSAndroid Build Coastguard Worker *
1714*f7c14bbaSAndroid Build Coastguard Worker * This is useful to be able to perform BPF program relocations
1715*f7c14bbaSAndroid Build Coastguard Worker * without having to create BPF maps before that step. This allows us
1716*f7c14bbaSAndroid Build Coastguard Worker * to finalize and load BTF very late in BPF object's loading phase,
1717*f7c14bbaSAndroid Build Coastguard Worker * right before BPF maps have to be created and BPF programs have to
1718*f7c14bbaSAndroid Build Coastguard Worker * be loaded. By having these map FD placeholders we can perform all
1719*f7c14bbaSAndroid Build Coastguard Worker * the sanitizations, relocations, and any other adjustments before we
1720*f7c14bbaSAndroid Build Coastguard Worker * start creating actual BPF kernel objects (BTF, maps, progs).
1721*f7c14bbaSAndroid Build Coastguard Worker */
1722*f7c14bbaSAndroid Build Coastguard Worker map->fd = create_placeholder_fd();
1723*f7c14bbaSAndroid Build Coastguard Worker if (map->fd < 0)
1724*f7c14bbaSAndroid Build Coastguard Worker return ERR_PTR(map->fd);
1725*f7c14bbaSAndroid Build Coastguard Worker map->inner_map_fd = -1;
1726*f7c14bbaSAndroid Build Coastguard Worker map->autocreate = true;
1727*f7c14bbaSAndroid Build Coastguard Worker
1728*f7c14bbaSAndroid Build Coastguard Worker return map;
1729*f7c14bbaSAndroid Build Coastguard Worker }
1730*f7c14bbaSAndroid Build Coastguard Worker
array_map_mmap_sz(unsigned int value_sz,unsigned int max_entries)1731*f7c14bbaSAndroid Build Coastguard Worker static size_t array_map_mmap_sz(unsigned int value_sz, unsigned int max_entries)
1732*f7c14bbaSAndroid Build Coastguard Worker {
1733*f7c14bbaSAndroid Build Coastguard Worker const long page_sz = sysconf(_SC_PAGE_SIZE);
1734*f7c14bbaSAndroid Build Coastguard Worker size_t map_sz;
1735*f7c14bbaSAndroid Build Coastguard Worker
1736*f7c14bbaSAndroid Build Coastguard Worker map_sz = (size_t)roundup(value_sz, 8) * max_entries;
1737*f7c14bbaSAndroid Build Coastguard Worker map_sz = roundup(map_sz, page_sz);
1738*f7c14bbaSAndroid Build Coastguard Worker return map_sz;
1739*f7c14bbaSAndroid Build Coastguard Worker }
1740*f7c14bbaSAndroid Build Coastguard Worker
bpf_map_mmap_sz(const struct bpf_map * map)1741*f7c14bbaSAndroid Build Coastguard Worker static size_t bpf_map_mmap_sz(const struct bpf_map *map)
1742*f7c14bbaSAndroid Build Coastguard Worker {
1743*f7c14bbaSAndroid Build Coastguard Worker const long page_sz = sysconf(_SC_PAGE_SIZE);
1744*f7c14bbaSAndroid Build Coastguard Worker
1745*f7c14bbaSAndroid Build Coastguard Worker switch (map->def.type) {
1746*f7c14bbaSAndroid Build Coastguard Worker case BPF_MAP_TYPE_ARRAY:
1747*f7c14bbaSAndroid Build Coastguard Worker return array_map_mmap_sz(map->def.value_size, map->def.max_entries);
1748*f7c14bbaSAndroid Build Coastguard Worker case BPF_MAP_TYPE_ARENA:
1749*f7c14bbaSAndroid Build Coastguard Worker return page_sz * map->def.max_entries;
1750*f7c14bbaSAndroid Build Coastguard Worker default:
1751*f7c14bbaSAndroid Build Coastguard Worker return 0; /* not supported */
1752*f7c14bbaSAndroid Build Coastguard Worker }
1753*f7c14bbaSAndroid Build Coastguard Worker }
1754*f7c14bbaSAndroid Build Coastguard Worker
bpf_map_mmap_resize(struct bpf_map * map,size_t old_sz,size_t new_sz)1755*f7c14bbaSAndroid Build Coastguard Worker static int bpf_map_mmap_resize(struct bpf_map *map, size_t old_sz, size_t new_sz)
1756*f7c14bbaSAndroid Build Coastguard Worker {
1757*f7c14bbaSAndroid Build Coastguard Worker void *mmaped;
1758*f7c14bbaSAndroid Build Coastguard Worker
1759*f7c14bbaSAndroid Build Coastguard Worker if (!map->mmaped)
1760*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
1761*f7c14bbaSAndroid Build Coastguard Worker
1762*f7c14bbaSAndroid Build Coastguard Worker if (old_sz == new_sz)
1763*f7c14bbaSAndroid Build Coastguard Worker return 0;
1764*f7c14bbaSAndroid Build Coastguard Worker
1765*f7c14bbaSAndroid Build Coastguard Worker mmaped = mmap(NULL, new_sz, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
1766*f7c14bbaSAndroid Build Coastguard Worker if (mmaped == MAP_FAILED)
1767*f7c14bbaSAndroid Build Coastguard Worker return -errno;
1768*f7c14bbaSAndroid Build Coastguard Worker
1769*f7c14bbaSAndroid Build Coastguard Worker memcpy(mmaped, map->mmaped, min(old_sz, new_sz));
1770*f7c14bbaSAndroid Build Coastguard Worker munmap(map->mmaped, old_sz);
1771*f7c14bbaSAndroid Build Coastguard Worker map->mmaped = mmaped;
1772*f7c14bbaSAndroid Build Coastguard Worker return 0;
1773*f7c14bbaSAndroid Build Coastguard Worker }
1774*f7c14bbaSAndroid Build Coastguard Worker
internal_map_name(struct bpf_object * obj,const char * real_name)1775*f7c14bbaSAndroid Build Coastguard Worker static char *internal_map_name(struct bpf_object *obj, const char *real_name)
1776*f7c14bbaSAndroid Build Coastguard Worker {
1777*f7c14bbaSAndroid Build Coastguard Worker char map_name[BPF_OBJ_NAME_LEN], *p;
1778*f7c14bbaSAndroid Build Coastguard Worker int pfx_len, sfx_len = max((size_t)7, strlen(real_name));
1779*f7c14bbaSAndroid Build Coastguard Worker
1780*f7c14bbaSAndroid Build Coastguard Worker /* This is one of the more confusing parts of libbpf for various
1781*f7c14bbaSAndroid Build Coastguard Worker * reasons, some of which are historical. The original idea for naming
1782*f7c14bbaSAndroid Build Coastguard Worker * internal names was to include as much of BPF object name prefix as
1783*f7c14bbaSAndroid Build Coastguard Worker * possible, so that it can be distinguished from similar internal
1784*f7c14bbaSAndroid Build Coastguard Worker * maps of a different BPF object.
1785*f7c14bbaSAndroid Build Coastguard Worker * As an example, let's say we have bpf_object named 'my_object_name'
1786*f7c14bbaSAndroid Build Coastguard Worker * and internal map corresponding to '.rodata' ELF section. The final
1787*f7c14bbaSAndroid Build Coastguard Worker * map name advertised to user and to the kernel will be
1788*f7c14bbaSAndroid Build Coastguard Worker * 'my_objec.rodata', taking first 8 characters of object name and
1789*f7c14bbaSAndroid Build Coastguard Worker * entire 7 characters of '.rodata'.
1790*f7c14bbaSAndroid Build Coastguard Worker * Somewhat confusingly, if internal map ELF section name is shorter
1791*f7c14bbaSAndroid Build Coastguard Worker * than 7 characters, e.g., '.bss', we still reserve 7 characters
1792*f7c14bbaSAndroid Build Coastguard Worker * for the suffix, even though we only have 4 actual characters, and
1793*f7c14bbaSAndroid Build Coastguard Worker * resulting map will be called 'my_objec.bss', not even using all 15
1794*f7c14bbaSAndroid Build Coastguard Worker * characters allowed by the kernel. Oh well, at least the truncated
1795*f7c14bbaSAndroid Build Coastguard Worker * object name is somewhat consistent in this case. But if the map
1796*f7c14bbaSAndroid Build Coastguard Worker * name is '.kconfig', we'll still have entirety of '.kconfig' added
1797*f7c14bbaSAndroid Build Coastguard Worker * (8 chars) and thus will be left with only first 7 characters of the
1798*f7c14bbaSAndroid Build Coastguard Worker * object name ('my_obje'). Happy guessing, user, that the final map
1799*f7c14bbaSAndroid Build Coastguard Worker * name will be "my_obje.kconfig".
1800*f7c14bbaSAndroid Build Coastguard Worker * Now, with libbpf starting to support arbitrarily named .rodata.*
1801*f7c14bbaSAndroid Build Coastguard Worker * and .data.* data sections, it's possible that ELF section name is
1802*f7c14bbaSAndroid Build Coastguard Worker * longer than allowed 15 chars, so we now need to be careful to take
1803*f7c14bbaSAndroid Build Coastguard Worker * only up to 15 first characters of ELF name, taking no BPF object
1804*f7c14bbaSAndroid Build Coastguard Worker * name characters at all. So '.rodata.abracadabra' will result in
1805*f7c14bbaSAndroid Build Coastguard Worker * '.rodata.abracad' kernel and user-visible name.
1806*f7c14bbaSAndroid Build Coastguard Worker * We need to keep this convoluted logic intact for .data, .bss and
1807*f7c14bbaSAndroid Build Coastguard Worker * .rodata maps, but for new custom .data.custom and .rodata.custom
1808*f7c14bbaSAndroid Build Coastguard Worker * maps we use their ELF names as is, not prepending bpf_object name
1809*f7c14bbaSAndroid Build Coastguard Worker * in front. We still need to truncate them to 15 characters for the
1810*f7c14bbaSAndroid Build Coastguard Worker * kernel. Full name can be recovered for such maps by using DATASEC
1811*f7c14bbaSAndroid Build Coastguard Worker * BTF type associated with such map's value type, though.
1812*f7c14bbaSAndroid Build Coastguard Worker */
1813*f7c14bbaSAndroid Build Coastguard Worker if (sfx_len >= BPF_OBJ_NAME_LEN)
1814*f7c14bbaSAndroid Build Coastguard Worker sfx_len = BPF_OBJ_NAME_LEN - 1;
1815*f7c14bbaSAndroid Build Coastguard Worker
1816*f7c14bbaSAndroid Build Coastguard Worker /* if there are two or more dots in map name, it's a custom dot map */
1817*f7c14bbaSAndroid Build Coastguard Worker if (strchr(real_name + 1, '.') != NULL)
1818*f7c14bbaSAndroid Build Coastguard Worker pfx_len = 0;
1819*f7c14bbaSAndroid Build Coastguard Worker else
1820*f7c14bbaSAndroid Build Coastguard Worker pfx_len = min((size_t)BPF_OBJ_NAME_LEN - sfx_len - 1, strlen(obj->name));
1821*f7c14bbaSAndroid Build Coastguard Worker
1822*f7c14bbaSAndroid Build Coastguard Worker snprintf(map_name, sizeof(map_name), "%.*s%.*s", pfx_len, obj->name,
1823*f7c14bbaSAndroid Build Coastguard Worker sfx_len, real_name);
1824*f7c14bbaSAndroid Build Coastguard Worker
1825*f7c14bbaSAndroid Build Coastguard Worker /* sanitise map name to characters allowed by kernel */
1826*f7c14bbaSAndroid Build Coastguard Worker for (p = map_name; *p && p < map_name + sizeof(map_name); p++)
1827*f7c14bbaSAndroid Build Coastguard Worker if (!isalnum(*p) && *p != '_' && *p != '.')
1828*f7c14bbaSAndroid Build Coastguard Worker *p = '_';
1829*f7c14bbaSAndroid Build Coastguard Worker
1830*f7c14bbaSAndroid Build Coastguard Worker return strdup(map_name);
1831*f7c14bbaSAndroid Build Coastguard Worker }
1832*f7c14bbaSAndroid Build Coastguard Worker
1833*f7c14bbaSAndroid Build Coastguard Worker static int
1834*f7c14bbaSAndroid Build Coastguard Worker map_fill_btf_type_info(struct bpf_object *obj, struct bpf_map *map);
1835*f7c14bbaSAndroid Build Coastguard Worker
1836*f7c14bbaSAndroid Build Coastguard Worker /* Internal BPF map is mmap()'able only if at least one of corresponding
1837*f7c14bbaSAndroid Build Coastguard Worker * DATASEC's VARs are to be exposed through BPF skeleton. I.e., it's a GLOBAL
1838*f7c14bbaSAndroid Build Coastguard Worker * variable and it's not marked as __hidden (which turns it into, effectively,
1839*f7c14bbaSAndroid Build Coastguard Worker * a STATIC variable).
1840*f7c14bbaSAndroid Build Coastguard Worker */
map_is_mmapable(struct bpf_object * obj,struct bpf_map * map)1841*f7c14bbaSAndroid Build Coastguard Worker static bool map_is_mmapable(struct bpf_object *obj, struct bpf_map *map)
1842*f7c14bbaSAndroid Build Coastguard Worker {
1843*f7c14bbaSAndroid Build Coastguard Worker const struct btf_type *t, *vt;
1844*f7c14bbaSAndroid Build Coastguard Worker struct btf_var_secinfo *vsi;
1845*f7c14bbaSAndroid Build Coastguard Worker int i, n;
1846*f7c14bbaSAndroid Build Coastguard Worker
1847*f7c14bbaSAndroid Build Coastguard Worker if (!map->btf_value_type_id)
1848*f7c14bbaSAndroid Build Coastguard Worker return false;
1849*f7c14bbaSAndroid Build Coastguard Worker
1850*f7c14bbaSAndroid Build Coastguard Worker t = btf__type_by_id(obj->btf, map->btf_value_type_id);
1851*f7c14bbaSAndroid Build Coastguard Worker if (!btf_is_datasec(t))
1852*f7c14bbaSAndroid Build Coastguard Worker return false;
1853*f7c14bbaSAndroid Build Coastguard Worker
1854*f7c14bbaSAndroid Build Coastguard Worker vsi = btf_var_secinfos(t);
1855*f7c14bbaSAndroid Build Coastguard Worker for (i = 0, n = btf_vlen(t); i < n; i++, vsi++) {
1856*f7c14bbaSAndroid Build Coastguard Worker vt = btf__type_by_id(obj->btf, vsi->type);
1857*f7c14bbaSAndroid Build Coastguard Worker if (!btf_is_var(vt))
1858*f7c14bbaSAndroid Build Coastguard Worker continue;
1859*f7c14bbaSAndroid Build Coastguard Worker
1860*f7c14bbaSAndroid Build Coastguard Worker if (btf_var(vt)->linkage != BTF_VAR_STATIC)
1861*f7c14bbaSAndroid Build Coastguard Worker return true;
1862*f7c14bbaSAndroid Build Coastguard Worker }
1863*f7c14bbaSAndroid Build Coastguard Worker
1864*f7c14bbaSAndroid Build Coastguard Worker return false;
1865*f7c14bbaSAndroid Build Coastguard Worker }
1866*f7c14bbaSAndroid Build Coastguard Worker
1867*f7c14bbaSAndroid Build Coastguard Worker static int
bpf_object__init_internal_map(struct bpf_object * obj,enum libbpf_map_type type,const char * real_name,int sec_idx,void * data,size_t data_sz)1868*f7c14bbaSAndroid Build Coastguard Worker bpf_object__init_internal_map(struct bpf_object *obj, enum libbpf_map_type type,
1869*f7c14bbaSAndroid Build Coastguard Worker const char *real_name, int sec_idx, void *data, size_t data_sz)
1870*f7c14bbaSAndroid Build Coastguard Worker {
1871*f7c14bbaSAndroid Build Coastguard Worker struct bpf_map_def *def;
1872*f7c14bbaSAndroid Build Coastguard Worker struct bpf_map *map;
1873*f7c14bbaSAndroid Build Coastguard Worker size_t mmap_sz;
1874*f7c14bbaSAndroid Build Coastguard Worker int err;
1875*f7c14bbaSAndroid Build Coastguard Worker
1876*f7c14bbaSAndroid Build Coastguard Worker map = bpf_object__add_map(obj);
1877*f7c14bbaSAndroid Build Coastguard Worker if (IS_ERR(map))
1878*f7c14bbaSAndroid Build Coastguard Worker return PTR_ERR(map);
1879*f7c14bbaSAndroid Build Coastguard Worker
1880*f7c14bbaSAndroid Build Coastguard Worker map->libbpf_type = type;
1881*f7c14bbaSAndroid Build Coastguard Worker map->sec_idx = sec_idx;
1882*f7c14bbaSAndroid Build Coastguard Worker map->sec_offset = 0;
1883*f7c14bbaSAndroid Build Coastguard Worker map->real_name = strdup(real_name);
1884*f7c14bbaSAndroid Build Coastguard Worker map->name = internal_map_name(obj, real_name);
1885*f7c14bbaSAndroid Build Coastguard Worker if (!map->real_name || !map->name) {
1886*f7c14bbaSAndroid Build Coastguard Worker zfree(&map->real_name);
1887*f7c14bbaSAndroid Build Coastguard Worker zfree(&map->name);
1888*f7c14bbaSAndroid Build Coastguard Worker return -ENOMEM;
1889*f7c14bbaSAndroid Build Coastguard Worker }
1890*f7c14bbaSAndroid Build Coastguard Worker
1891*f7c14bbaSAndroid Build Coastguard Worker def = &map->def;
1892*f7c14bbaSAndroid Build Coastguard Worker def->type = BPF_MAP_TYPE_ARRAY;
1893*f7c14bbaSAndroid Build Coastguard Worker def->key_size = sizeof(int);
1894*f7c14bbaSAndroid Build Coastguard Worker def->value_size = data_sz;
1895*f7c14bbaSAndroid Build Coastguard Worker def->max_entries = 1;
1896*f7c14bbaSAndroid Build Coastguard Worker def->map_flags = type == LIBBPF_MAP_RODATA || type == LIBBPF_MAP_KCONFIG
1897*f7c14bbaSAndroid Build Coastguard Worker ? BPF_F_RDONLY_PROG : 0;
1898*f7c14bbaSAndroid Build Coastguard Worker
1899*f7c14bbaSAndroid Build Coastguard Worker /* failures are fine because of maps like .rodata.str1.1 */
1900*f7c14bbaSAndroid Build Coastguard Worker (void) map_fill_btf_type_info(obj, map);
1901*f7c14bbaSAndroid Build Coastguard Worker
1902*f7c14bbaSAndroid Build Coastguard Worker if (map_is_mmapable(obj, map))
1903*f7c14bbaSAndroid Build Coastguard Worker def->map_flags |= BPF_F_MMAPABLE;
1904*f7c14bbaSAndroid Build Coastguard Worker
1905*f7c14bbaSAndroid Build Coastguard Worker pr_debug("map '%s' (global data): at sec_idx %d, offset %zu, flags %x.\n",
1906*f7c14bbaSAndroid Build Coastguard Worker map->name, map->sec_idx, map->sec_offset, def->map_flags);
1907*f7c14bbaSAndroid Build Coastguard Worker
1908*f7c14bbaSAndroid Build Coastguard Worker mmap_sz = bpf_map_mmap_sz(map);
1909*f7c14bbaSAndroid Build Coastguard Worker map->mmaped = mmap(NULL, mmap_sz, PROT_READ | PROT_WRITE,
1910*f7c14bbaSAndroid Build Coastguard Worker MAP_SHARED | MAP_ANONYMOUS, -1, 0);
1911*f7c14bbaSAndroid Build Coastguard Worker if (map->mmaped == MAP_FAILED) {
1912*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
1913*f7c14bbaSAndroid Build Coastguard Worker map->mmaped = NULL;
1914*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to alloc map '%s' content buffer: %d\n",
1915*f7c14bbaSAndroid Build Coastguard Worker map->name, err);
1916*f7c14bbaSAndroid Build Coastguard Worker zfree(&map->real_name);
1917*f7c14bbaSAndroid Build Coastguard Worker zfree(&map->name);
1918*f7c14bbaSAndroid Build Coastguard Worker return err;
1919*f7c14bbaSAndroid Build Coastguard Worker }
1920*f7c14bbaSAndroid Build Coastguard Worker
1921*f7c14bbaSAndroid Build Coastguard Worker if (data)
1922*f7c14bbaSAndroid Build Coastguard Worker memcpy(map->mmaped, data, data_sz);
1923*f7c14bbaSAndroid Build Coastguard Worker
1924*f7c14bbaSAndroid Build Coastguard Worker pr_debug("map %td is \"%s\"\n", map - obj->maps, map->name);
1925*f7c14bbaSAndroid Build Coastguard Worker return 0;
1926*f7c14bbaSAndroid Build Coastguard Worker }
1927*f7c14bbaSAndroid Build Coastguard Worker
bpf_object__init_global_data_maps(struct bpf_object * obj)1928*f7c14bbaSAndroid Build Coastguard Worker static int bpf_object__init_global_data_maps(struct bpf_object *obj)
1929*f7c14bbaSAndroid Build Coastguard Worker {
1930*f7c14bbaSAndroid Build Coastguard Worker struct elf_sec_desc *sec_desc;
1931*f7c14bbaSAndroid Build Coastguard Worker const char *sec_name;
1932*f7c14bbaSAndroid Build Coastguard Worker int err = 0, sec_idx;
1933*f7c14bbaSAndroid Build Coastguard Worker
1934*f7c14bbaSAndroid Build Coastguard Worker /*
1935*f7c14bbaSAndroid Build Coastguard Worker * Populate obj->maps with libbpf internal maps.
1936*f7c14bbaSAndroid Build Coastguard Worker */
1937*f7c14bbaSAndroid Build Coastguard Worker for (sec_idx = 1; sec_idx < obj->efile.sec_cnt; sec_idx++) {
1938*f7c14bbaSAndroid Build Coastguard Worker sec_desc = &obj->efile.secs[sec_idx];
1939*f7c14bbaSAndroid Build Coastguard Worker
1940*f7c14bbaSAndroid Build Coastguard Worker /* Skip recognized sections with size 0. */
1941*f7c14bbaSAndroid Build Coastguard Worker if (!sec_desc->data || sec_desc->data->d_size == 0)
1942*f7c14bbaSAndroid Build Coastguard Worker continue;
1943*f7c14bbaSAndroid Build Coastguard Worker
1944*f7c14bbaSAndroid Build Coastguard Worker switch (sec_desc->sec_type) {
1945*f7c14bbaSAndroid Build Coastguard Worker case SEC_DATA:
1946*f7c14bbaSAndroid Build Coastguard Worker sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, sec_idx));
1947*f7c14bbaSAndroid Build Coastguard Worker err = bpf_object__init_internal_map(obj, LIBBPF_MAP_DATA,
1948*f7c14bbaSAndroid Build Coastguard Worker sec_name, sec_idx,
1949*f7c14bbaSAndroid Build Coastguard Worker sec_desc->data->d_buf,
1950*f7c14bbaSAndroid Build Coastguard Worker sec_desc->data->d_size);
1951*f7c14bbaSAndroid Build Coastguard Worker break;
1952*f7c14bbaSAndroid Build Coastguard Worker case SEC_RODATA:
1953*f7c14bbaSAndroid Build Coastguard Worker obj->has_rodata = true;
1954*f7c14bbaSAndroid Build Coastguard Worker sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, sec_idx));
1955*f7c14bbaSAndroid Build Coastguard Worker err = bpf_object__init_internal_map(obj, LIBBPF_MAP_RODATA,
1956*f7c14bbaSAndroid Build Coastguard Worker sec_name, sec_idx,
1957*f7c14bbaSAndroid Build Coastguard Worker sec_desc->data->d_buf,
1958*f7c14bbaSAndroid Build Coastguard Worker sec_desc->data->d_size);
1959*f7c14bbaSAndroid Build Coastguard Worker break;
1960*f7c14bbaSAndroid Build Coastguard Worker case SEC_BSS:
1961*f7c14bbaSAndroid Build Coastguard Worker sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, sec_idx));
1962*f7c14bbaSAndroid Build Coastguard Worker err = bpf_object__init_internal_map(obj, LIBBPF_MAP_BSS,
1963*f7c14bbaSAndroid Build Coastguard Worker sec_name, sec_idx,
1964*f7c14bbaSAndroid Build Coastguard Worker NULL,
1965*f7c14bbaSAndroid Build Coastguard Worker sec_desc->data->d_size);
1966*f7c14bbaSAndroid Build Coastguard Worker break;
1967*f7c14bbaSAndroid Build Coastguard Worker default:
1968*f7c14bbaSAndroid Build Coastguard Worker /* skip */
1969*f7c14bbaSAndroid Build Coastguard Worker break;
1970*f7c14bbaSAndroid Build Coastguard Worker }
1971*f7c14bbaSAndroid Build Coastguard Worker if (err)
1972*f7c14bbaSAndroid Build Coastguard Worker return err;
1973*f7c14bbaSAndroid Build Coastguard Worker }
1974*f7c14bbaSAndroid Build Coastguard Worker return 0;
1975*f7c14bbaSAndroid Build Coastguard Worker }
1976*f7c14bbaSAndroid Build Coastguard Worker
1977*f7c14bbaSAndroid Build Coastguard Worker
find_extern_by_name(const struct bpf_object * obj,const void * name)1978*f7c14bbaSAndroid Build Coastguard Worker static struct extern_desc *find_extern_by_name(const struct bpf_object *obj,
1979*f7c14bbaSAndroid Build Coastguard Worker const void *name)
1980*f7c14bbaSAndroid Build Coastguard Worker {
1981*f7c14bbaSAndroid Build Coastguard Worker int i;
1982*f7c14bbaSAndroid Build Coastguard Worker
1983*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < obj->nr_extern; i++) {
1984*f7c14bbaSAndroid Build Coastguard Worker if (strcmp(obj->externs[i].name, name) == 0)
1985*f7c14bbaSAndroid Build Coastguard Worker return &obj->externs[i];
1986*f7c14bbaSAndroid Build Coastguard Worker }
1987*f7c14bbaSAndroid Build Coastguard Worker return NULL;
1988*f7c14bbaSAndroid Build Coastguard Worker }
1989*f7c14bbaSAndroid Build Coastguard Worker
set_kcfg_value_tri(struct extern_desc * ext,void * ext_val,char value)1990*f7c14bbaSAndroid Build Coastguard Worker static int set_kcfg_value_tri(struct extern_desc *ext, void *ext_val,
1991*f7c14bbaSAndroid Build Coastguard Worker char value)
1992*f7c14bbaSAndroid Build Coastguard Worker {
1993*f7c14bbaSAndroid Build Coastguard Worker switch (ext->kcfg.type) {
1994*f7c14bbaSAndroid Build Coastguard Worker case KCFG_BOOL:
1995*f7c14bbaSAndroid Build Coastguard Worker if (value == 'm') {
1996*f7c14bbaSAndroid Build Coastguard Worker pr_warn("extern (kcfg) '%s': value '%c' implies tristate or char type\n",
1997*f7c14bbaSAndroid Build Coastguard Worker ext->name, value);
1998*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
1999*f7c14bbaSAndroid Build Coastguard Worker }
2000*f7c14bbaSAndroid Build Coastguard Worker *(bool *)ext_val = value == 'y' ? true : false;
2001*f7c14bbaSAndroid Build Coastguard Worker break;
2002*f7c14bbaSAndroid Build Coastguard Worker case KCFG_TRISTATE:
2003*f7c14bbaSAndroid Build Coastguard Worker if (value == 'y')
2004*f7c14bbaSAndroid Build Coastguard Worker *(enum libbpf_tristate *)ext_val = TRI_YES;
2005*f7c14bbaSAndroid Build Coastguard Worker else if (value == 'm')
2006*f7c14bbaSAndroid Build Coastguard Worker *(enum libbpf_tristate *)ext_val = TRI_MODULE;
2007*f7c14bbaSAndroid Build Coastguard Worker else /* value == 'n' */
2008*f7c14bbaSAndroid Build Coastguard Worker *(enum libbpf_tristate *)ext_val = TRI_NO;
2009*f7c14bbaSAndroid Build Coastguard Worker break;
2010*f7c14bbaSAndroid Build Coastguard Worker case KCFG_CHAR:
2011*f7c14bbaSAndroid Build Coastguard Worker *(char *)ext_val = value;
2012*f7c14bbaSAndroid Build Coastguard Worker break;
2013*f7c14bbaSAndroid Build Coastguard Worker case KCFG_UNKNOWN:
2014*f7c14bbaSAndroid Build Coastguard Worker case KCFG_INT:
2015*f7c14bbaSAndroid Build Coastguard Worker case KCFG_CHAR_ARR:
2016*f7c14bbaSAndroid Build Coastguard Worker default:
2017*f7c14bbaSAndroid Build Coastguard Worker pr_warn("extern (kcfg) '%s': value '%c' implies bool, tristate, or char type\n",
2018*f7c14bbaSAndroid Build Coastguard Worker ext->name, value);
2019*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
2020*f7c14bbaSAndroid Build Coastguard Worker }
2021*f7c14bbaSAndroid Build Coastguard Worker ext->is_set = true;
2022*f7c14bbaSAndroid Build Coastguard Worker return 0;
2023*f7c14bbaSAndroid Build Coastguard Worker }
2024*f7c14bbaSAndroid Build Coastguard Worker
set_kcfg_value_str(struct extern_desc * ext,char * ext_val,const char * value)2025*f7c14bbaSAndroid Build Coastguard Worker static int set_kcfg_value_str(struct extern_desc *ext, char *ext_val,
2026*f7c14bbaSAndroid Build Coastguard Worker const char *value)
2027*f7c14bbaSAndroid Build Coastguard Worker {
2028*f7c14bbaSAndroid Build Coastguard Worker size_t len;
2029*f7c14bbaSAndroid Build Coastguard Worker
2030*f7c14bbaSAndroid Build Coastguard Worker if (ext->kcfg.type != KCFG_CHAR_ARR) {
2031*f7c14bbaSAndroid Build Coastguard Worker pr_warn("extern (kcfg) '%s': value '%s' implies char array type\n",
2032*f7c14bbaSAndroid Build Coastguard Worker ext->name, value);
2033*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
2034*f7c14bbaSAndroid Build Coastguard Worker }
2035*f7c14bbaSAndroid Build Coastguard Worker
2036*f7c14bbaSAndroid Build Coastguard Worker len = strlen(value);
2037*f7c14bbaSAndroid Build Coastguard Worker if (value[len - 1] != '"') {
2038*f7c14bbaSAndroid Build Coastguard Worker pr_warn("extern (kcfg) '%s': invalid string config '%s'\n",
2039*f7c14bbaSAndroid Build Coastguard Worker ext->name, value);
2040*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
2041*f7c14bbaSAndroid Build Coastguard Worker }
2042*f7c14bbaSAndroid Build Coastguard Worker
2043*f7c14bbaSAndroid Build Coastguard Worker /* strip quotes */
2044*f7c14bbaSAndroid Build Coastguard Worker len -= 2;
2045*f7c14bbaSAndroid Build Coastguard Worker if (len >= ext->kcfg.sz) {
2046*f7c14bbaSAndroid Build Coastguard Worker pr_warn("extern (kcfg) '%s': long string '%s' of (%zu bytes) truncated to %d bytes\n",
2047*f7c14bbaSAndroid Build Coastguard Worker ext->name, value, len, ext->kcfg.sz - 1);
2048*f7c14bbaSAndroid Build Coastguard Worker len = ext->kcfg.sz - 1;
2049*f7c14bbaSAndroid Build Coastguard Worker }
2050*f7c14bbaSAndroid Build Coastguard Worker memcpy(ext_val, value + 1, len);
2051*f7c14bbaSAndroid Build Coastguard Worker ext_val[len] = '\0';
2052*f7c14bbaSAndroid Build Coastguard Worker ext->is_set = true;
2053*f7c14bbaSAndroid Build Coastguard Worker return 0;
2054*f7c14bbaSAndroid Build Coastguard Worker }
2055*f7c14bbaSAndroid Build Coastguard Worker
parse_u64(const char * value,__u64 * res)2056*f7c14bbaSAndroid Build Coastguard Worker static int parse_u64(const char *value, __u64 *res)
2057*f7c14bbaSAndroid Build Coastguard Worker {
2058*f7c14bbaSAndroid Build Coastguard Worker char *value_end;
2059*f7c14bbaSAndroid Build Coastguard Worker int err;
2060*f7c14bbaSAndroid Build Coastguard Worker
2061*f7c14bbaSAndroid Build Coastguard Worker errno = 0;
2062*f7c14bbaSAndroid Build Coastguard Worker *res = strtoull(value, &value_end, 0);
2063*f7c14bbaSAndroid Build Coastguard Worker if (errno) {
2064*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
2065*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to parse '%s' as integer: %d\n", value, err);
2066*f7c14bbaSAndroid Build Coastguard Worker return err;
2067*f7c14bbaSAndroid Build Coastguard Worker }
2068*f7c14bbaSAndroid Build Coastguard Worker if (*value_end) {
2069*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to parse '%s' as integer completely\n", value);
2070*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
2071*f7c14bbaSAndroid Build Coastguard Worker }
2072*f7c14bbaSAndroid Build Coastguard Worker return 0;
2073*f7c14bbaSAndroid Build Coastguard Worker }
2074*f7c14bbaSAndroid Build Coastguard Worker
is_kcfg_value_in_range(const struct extern_desc * ext,__u64 v)2075*f7c14bbaSAndroid Build Coastguard Worker static bool is_kcfg_value_in_range(const struct extern_desc *ext, __u64 v)
2076*f7c14bbaSAndroid Build Coastguard Worker {
2077*f7c14bbaSAndroid Build Coastguard Worker int bit_sz = ext->kcfg.sz * 8;
2078*f7c14bbaSAndroid Build Coastguard Worker
2079*f7c14bbaSAndroid Build Coastguard Worker if (ext->kcfg.sz == 8)
2080*f7c14bbaSAndroid Build Coastguard Worker return true;
2081*f7c14bbaSAndroid Build Coastguard Worker
2082*f7c14bbaSAndroid Build Coastguard Worker /* Validate that value stored in u64 fits in integer of `ext->sz`
2083*f7c14bbaSAndroid Build Coastguard Worker * bytes size without any loss of information. If the target integer
2084*f7c14bbaSAndroid Build Coastguard Worker * is signed, we rely on the following limits of integer type of
2085*f7c14bbaSAndroid Build Coastguard Worker * Y bits and subsequent transformation:
2086*f7c14bbaSAndroid Build Coastguard Worker *
2087*f7c14bbaSAndroid Build Coastguard Worker * -2^(Y-1) <= X <= 2^(Y-1) - 1
2088*f7c14bbaSAndroid Build Coastguard Worker * 0 <= X + 2^(Y-1) <= 2^Y - 1
2089*f7c14bbaSAndroid Build Coastguard Worker * 0 <= X + 2^(Y-1) < 2^Y
2090*f7c14bbaSAndroid Build Coastguard Worker *
2091*f7c14bbaSAndroid Build Coastguard Worker * For unsigned target integer, check that all the (64 - Y) bits are
2092*f7c14bbaSAndroid Build Coastguard Worker * zero.
2093*f7c14bbaSAndroid Build Coastguard Worker */
2094*f7c14bbaSAndroid Build Coastguard Worker if (ext->kcfg.is_signed)
2095*f7c14bbaSAndroid Build Coastguard Worker return v + (1ULL << (bit_sz - 1)) < (1ULL << bit_sz);
2096*f7c14bbaSAndroid Build Coastguard Worker else
2097*f7c14bbaSAndroid Build Coastguard Worker return (v >> bit_sz) == 0;
2098*f7c14bbaSAndroid Build Coastguard Worker }
2099*f7c14bbaSAndroid Build Coastguard Worker
set_kcfg_value_num(struct extern_desc * ext,void * ext_val,__u64 value)2100*f7c14bbaSAndroid Build Coastguard Worker static int set_kcfg_value_num(struct extern_desc *ext, void *ext_val,
2101*f7c14bbaSAndroid Build Coastguard Worker __u64 value)
2102*f7c14bbaSAndroid Build Coastguard Worker {
2103*f7c14bbaSAndroid Build Coastguard Worker if (ext->kcfg.type != KCFG_INT && ext->kcfg.type != KCFG_CHAR &&
2104*f7c14bbaSAndroid Build Coastguard Worker ext->kcfg.type != KCFG_BOOL) {
2105*f7c14bbaSAndroid Build Coastguard Worker pr_warn("extern (kcfg) '%s': value '%llu' implies integer, char, or boolean type\n",
2106*f7c14bbaSAndroid Build Coastguard Worker ext->name, (unsigned long long)value);
2107*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
2108*f7c14bbaSAndroid Build Coastguard Worker }
2109*f7c14bbaSAndroid Build Coastguard Worker if (ext->kcfg.type == KCFG_BOOL && value > 1) {
2110*f7c14bbaSAndroid Build Coastguard Worker pr_warn("extern (kcfg) '%s': value '%llu' isn't boolean compatible\n",
2111*f7c14bbaSAndroid Build Coastguard Worker ext->name, (unsigned long long)value);
2112*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
2113*f7c14bbaSAndroid Build Coastguard Worker
2114*f7c14bbaSAndroid Build Coastguard Worker }
2115*f7c14bbaSAndroid Build Coastguard Worker if (!is_kcfg_value_in_range(ext, value)) {
2116*f7c14bbaSAndroid Build Coastguard Worker pr_warn("extern (kcfg) '%s': value '%llu' doesn't fit in %d bytes\n",
2117*f7c14bbaSAndroid Build Coastguard Worker ext->name, (unsigned long long)value, ext->kcfg.sz);
2118*f7c14bbaSAndroid Build Coastguard Worker return -ERANGE;
2119*f7c14bbaSAndroid Build Coastguard Worker }
2120*f7c14bbaSAndroid Build Coastguard Worker switch (ext->kcfg.sz) {
2121*f7c14bbaSAndroid Build Coastguard Worker case 1:
2122*f7c14bbaSAndroid Build Coastguard Worker *(__u8 *)ext_val = value;
2123*f7c14bbaSAndroid Build Coastguard Worker break;
2124*f7c14bbaSAndroid Build Coastguard Worker case 2:
2125*f7c14bbaSAndroid Build Coastguard Worker *(__u16 *)ext_val = value;
2126*f7c14bbaSAndroid Build Coastguard Worker break;
2127*f7c14bbaSAndroid Build Coastguard Worker case 4:
2128*f7c14bbaSAndroid Build Coastguard Worker *(__u32 *)ext_val = value;
2129*f7c14bbaSAndroid Build Coastguard Worker break;
2130*f7c14bbaSAndroid Build Coastguard Worker case 8:
2131*f7c14bbaSAndroid Build Coastguard Worker *(__u64 *)ext_val = value;
2132*f7c14bbaSAndroid Build Coastguard Worker break;
2133*f7c14bbaSAndroid Build Coastguard Worker default:
2134*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
2135*f7c14bbaSAndroid Build Coastguard Worker }
2136*f7c14bbaSAndroid Build Coastguard Worker ext->is_set = true;
2137*f7c14bbaSAndroid Build Coastguard Worker return 0;
2138*f7c14bbaSAndroid Build Coastguard Worker }
2139*f7c14bbaSAndroid Build Coastguard Worker
bpf_object__process_kconfig_line(struct bpf_object * obj,char * buf,void * data)2140*f7c14bbaSAndroid Build Coastguard Worker static int bpf_object__process_kconfig_line(struct bpf_object *obj,
2141*f7c14bbaSAndroid Build Coastguard Worker char *buf, void *data)
2142*f7c14bbaSAndroid Build Coastguard Worker {
2143*f7c14bbaSAndroid Build Coastguard Worker struct extern_desc *ext;
2144*f7c14bbaSAndroid Build Coastguard Worker char *sep, *value;
2145*f7c14bbaSAndroid Build Coastguard Worker int len, err = 0;
2146*f7c14bbaSAndroid Build Coastguard Worker void *ext_val;
2147*f7c14bbaSAndroid Build Coastguard Worker __u64 num;
2148*f7c14bbaSAndroid Build Coastguard Worker
2149*f7c14bbaSAndroid Build Coastguard Worker if (!str_has_pfx(buf, "CONFIG_"))
2150*f7c14bbaSAndroid Build Coastguard Worker return 0;
2151*f7c14bbaSAndroid Build Coastguard Worker
2152*f7c14bbaSAndroid Build Coastguard Worker sep = strchr(buf, '=');
2153*f7c14bbaSAndroid Build Coastguard Worker if (!sep) {
2154*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to parse '%s': no separator\n", buf);
2155*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
2156*f7c14bbaSAndroid Build Coastguard Worker }
2157*f7c14bbaSAndroid Build Coastguard Worker
2158*f7c14bbaSAndroid Build Coastguard Worker /* Trim ending '\n' */
2159*f7c14bbaSAndroid Build Coastguard Worker len = strlen(buf);
2160*f7c14bbaSAndroid Build Coastguard Worker if (buf[len - 1] == '\n')
2161*f7c14bbaSAndroid Build Coastguard Worker buf[len - 1] = '\0';
2162*f7c14bbaSAndroid Build Coastguard Worker /* Split on '=' and ensure that a value is present. */
2163*f7c14bbaSAndroid Build Coastguard Worker *sep = '\0';
2164*f7c14bbaSAndroid Build Coastguard Worker if (!sep[1]) {
2165*f7c14bbaSAndroid Build Coastguard Worker *sep = '=';
2166*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to parse '%s': no value\n", buf);
2167*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
2168*f7c14bbaSAndroid Build Coastguard Worker }
2169*f7c14bbaSAndroid Build Coastguard Worker
2170*f7c14bbaSAndroid Build Coastguard Worker ext = find_extern_by_name(obj, buf);
2171*f7c14bbaSAndroid Build Coastguard Worker if (!ext || ext->is_set)
2172*f7c14bbaSAndroid Build Coastguard Worker return 0;
2173*f7c14bbaSAndroid Build Coastguard Worker
2174*f7c14bbaSAndroid Build Coastguard Worker ext_val = data + ext->kcfg.data_off;
2175*f7c14bbaSAndroid Build Coastguard Worker value = sep + 1;
2176*f7c14bbaSAndroid Build Coastguard Worker
2177*f7c14bbaSAndroid Build Coastguard Worker switch (*value) {
2178*f7c14bbaSAndroid Build Coastguard Worker case 'y': case 'n': case 'm':
2179*f7c14bbaSAndroid Build Coastguard Worker err = set_kcfg_value_tri(ext, ext_val, *value);
2180*f7c14bbaSAndroid Build Coastguard Worker break;
2181*f7c14bbaSAndroid Build Coastguard Worker case '"':
2182*f7c14bbaSAndroid Build Coastguard Worker err = set_kcfg_value_str(ext, ext_val, value);
2183*f7c14bbaSAndroid Build Coastguard Worker break;
2184*f7c14bbaSAndroid Build Coastguard Worker default:
2185*f7c14bbaSAndroid Build Coastguard Worker /* assume integer */
2186*f7c14bbaSAndroid Build Coastguard Worker err = parse_u64(value, &num);
2187*f7c14bbaSAndroid Build Coastguard Worker if (err) {
2188*f7c14bbaSAndroid Build Coastguard Worker pr_warn("extern (kcfg) '%s': value '%s' isn't a valid integer\n", ext->name, value);
2189*f7c14bbaSAndroid Build Coastguard Worker return err;
2190*f7c14bbaSAndroid Build Coastguard Worker }
2191*f7c14bbaSAndroid Build Coastguard Worker if (ext->kcfg.type != KCFG_INT && ext->kcfg.type != KCFG_CHAR) {
2192*f7c14bbaSAndroid Build Coastguard Worker pr_warn("extern (kcfg) '%s': value '%s' implies integer type\n", ext->name, value);
2193*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
2194*f7c14bbaSAndroid Build Coastguard Worker }
2195*f7c14bbaSAndroid Build Coastguard Worker err = set_kcfg_value_num(ext, ext_val, num);
2196*f7c14bbaSAndroid Build Coastguard Worker break;
2197*f7c14bbaSAndroid Build Coastguard Worker }
2198*f7c14bbaSAndroid Build Coastguard Worker if (err)
2199*f7c14bbaSAndroid Build Coastguard Worker return err;
2200*f7c14bbaSAndroid Build Coastguard Worker pr_debug("extern (kcfg) '%s': set to %s\n", ext->name, value);
2201*f7c14bbaSAndroid Build Coastguard Worker return 0;
2202*f7c14bbaSAndroid Build Coastguard Worker }
2203*f7c14bbaSAndroid Build Coastguard Worker
bpf_object__read_kconfig_file(struct bpf_object * obj,void * data)2204*f7c14bbaSAndroid Build Coastguard Worker static int bpf_object__read_kconfig_file(struct bpf_object *obj, void *data)
2205*f7c14bbaSAndroid Build Coastguard Worker {
2206*f7c14bbaSAndroid Build Coastguard Worker char buf[PATH_MAX];
2207*f7c14bbaSAndroid Build Coastguard Worker struct utsname uts;
2208*f7c14bbaSAndroid Build Coastguard Worker int len, err = 0;
2209*f7c14bbaSAndroid Build Coastguard Worker gzFile file;
2210*f7c14bbaSAndroid Build Coastguard Worker
2211*f7c14bbaSAndroid Build Coastguard Worker uname(&uts);
2212*f7c14bbaSAndroid Build Coastguard Worker len = snprintf(buf, PATH_MAX, "/boot/config-%s", uts.release);
2213*f7c14bbaSAndroid Build Coastguard Worker if (len < 0)
2214*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
2215*f7c14bbaSAndroid Build Coastguard Worker else if (len >= PATH_MAX)
2216*f7c14bbaSAndroid Build Coastguard Worker return -ENAMETOOLONG;
2217*f7c14bbaSAndroid Build Coastguard Worker
2218*f7c14bbaSAndroid Build Coastguard Worker /* gzopen also accepts uncompressed files. */
2219*f7c14bbaSAndroid Build Coastguard Worker file = gzopen(buf, "re");
2220*f7c14bbaSAndroid Build Coastguard Worker if (!file)
2221*f7c14bbaSAndroid Build Coastguard Worker file = gzopen("/proc/config.gz", "re");
2222*f7c14bbaSAndroid Build Coastguard Worker
2223*f7c14bbaSAndroid Build Coastguard Worker if (!file) {
2224*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to open system Kconfig\n");
2225*f7c14bbaSAndroid Build Coastguard Worker return -ENOENT;
2226*f7c14bbaSAndroid Build Coastguard Worker }
2227*f7c14bbaSAndroid Build Coastguard Worker
2228*f7c14bbaSAndroid Build Coastguard Worker while (gzgets(file, buf, sizeof(buf))) {
2229*f7c14bbaSAndroid Build Coastguard Worker err = bpf_object__process_kconfig_line(obj, buf, data);
2230*f7c14bbaSAndroid Build Coastguard Worker if (err) {
2231*f7c14bbaSAndroid Build Coastguard Worker pr_warn("error parsing system Kconfig line '%s': %d\n",
2232*f7c14bbaSAndroid Build Coastguard Worker buf, err);
2233*f7c14bbaSAndroid Build Coastguard Worker goto out;
2234*f7c14bbaSAndroid Build Coastguard Worker }
2235*f7c14bbaSAndroid Build Coastguard Worker }
2236*f7c14bbaSAndroid Build Coastguard Worker
2237*f7c14bbaSAndroid Build Coastguard Worker out:
2238*f7c14bbaSAndroid Build Coastguard Worker gzclose(file);
2239*f7c14bbaSAndroid Build Coastguard Worker return err;
2240*f7c14bbaSAndroid Build Coastguard Worker }
2241*f7c14bbaSAndroid Build Coastguard Worker
bpf_object__read_kconfig_mem(struct bpf_object * obj,const char * config,void * data)2242*f7c14bbaSAndroid Build Coastguard Worker static int bpf_object__read_kconfig_mem(struct bpf_object *obj,
2243*f7c14bbaSAndroid Build Coastguard Worker const char *config, void *data)
2244*f7c14bbaSAndroid Build Coastguard Worker {
2245*f7c14bbaSAndroid Build Coastguard Worker char buf[PATH_MAX];
2246*f7c14bbaSAndroid Build Coastguard Worker int err = 0;
2247*f7c14bbaSAndroid Build Coastguard Worker FILE *file;
2248*f7c14bbaSAndroid Build Coastguard Worker
2249*f7c14bbaSAndroid Build Coastguard Worker file = fmemopen((void *)config, strlen(config), "r");
2250*f7c14bbaSAndroid Build Coastguard Worker if (!file) {
2251*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
2252*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to open in-memory Kconfig: %d\n", err);
2253*f7c14bbaSAndroid Build Coastguard Worker return err;
2254*f7c14bbaSAndroid Build Coastguard Worker }
2255*f7c14bbaSAndroid Build Coastguard Worker
2256*f7c14bbaSAndroid Build Coastguard Worker while (fgets(buf, sizeof(buf), file)) {
2257*f7c14bbaSAndroid Build Coastguard Worker err = bpf_object__process_kconfig_line(obj, buf, data);
2258*f7c14bbaSAndroid Build Coastguard Worker if (err) {
2259*f7c14bbaSAndroid Build Coastguard Worker pr_warn("error parsing in-memory Kconfig line '%s': %d\n",
2260*f7c14bbaSAndroid Build Coastguard Worker buf, err);
2261*f7c14bbaSAndroid Build Coastguard Worker break;
2262*f7c14bbaSAndroid Build Coastguard Worker }
2263*f7c14bbaSAndroid Build Coastguard Worker }
2264*f7c14bbaSAndroid Build Coastguard Worker
2265*f7c14bbaSAndroid Build Coastguard Worker fclose(file);
2266*f7c14bbaSAndroid Build Coastguard Worker return err;
2267*f7c14bbaSAndroid Build Coastguard Worker }
2268*f7c14bbaSAndroid Build Coastguard Worker
bpf_object__init_kconfig_map(struct bpf_object * obj)2269*f7c14bbaSAndroid Build Coastguard Worker static int bpf_object__init_kconfig_map(struct bpf_object *obj)
2270*f7c14bbaSAndroid Build Coastguard Worker {
2271*f7c14bbaSAndroid Build Coastguard Worker struct extern_desc *last_ext = NULL, *ext;
2272*f7c14bbaSAndroid Build Coastguard Worker size_t map_sz;
2273*f7c14bbaSAndroid Build Coastguard Worker int i, err;
2274*f7c14bbaSAndroid Build Coastguard Worker
2275*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < obj->nr_extern; i++) {
2276*f7c14bbaSAndroid Build Coastguard Worker ext = &obj->externs[i];
2277*f7c14bbaSAndroid Build Coastguard Worker if (ext->type == EXT_KCFG)
2278*f7c14bbaSAndroid Build Coastguard Worker last_ext = ext;
2279*f7c14bbaSAndroid Build Coastguard Worker }
2280*f7c14bbaSAndroid Build Coastguard Worker
2281*f7c14bbaSAndroid Build Coastguard Worker if (!last_ext)
2282*f7c14bbaSAndroid Build Coastguard Worker return 0;
2283*f7c14bbaSAndroid Build Coastguard Worker
2284*f7c14bbaSAndroid Build Coastguard Worker map_sz = last_ext->kcfg.data_off + last_ext->kcfg.sz;
2285*f7c14bbaSAndroid Build Coastguard Worker err = bpf_object__init_internal_map(obj, LIBBPF_MAP_KCONFIG,
2286*f7c14bbaSAndroid Build Coastguard Worker ".kconfig", obj->efile.symbols_shndx,
2287*f7c14bbaSAndroid Build Coastguard Worker NULL, map_sz);
2288*f7c14bbaSAndroid Build Coastguard Worker if (err)
2289*f7c14bbaSAndroid Build Coastguard Worker return err;
2290*f7c14bbaSAndroid Build Coastguard Worker
2291*f7c14bbaSAndroid Build Coastguard Worker obj->kconfig_map_idx = obj->nr_maps - 1;
2292*f7c14bbaSAndroid Build Coastguard Worker
2293*f7c14bbaSAndroid Build Coastguard Worker return 0;
2294*f7c14bbaSAndroid Build Coastguard Worker }
2295*f7c14bbaSAndroid Build Coastguard Worker
2296*f7c14bbaSAndroid Build Coastguard Worker const struct btf_type *
skip_mods_and_typedefs(const struct btf * btf,__u32 id,__u32 * res_id)2297*f7c14bbaSAndroid Build Coastguard Worker skip_mods_and_typedefs(const struct btf *btf, __u32 id, __u32 *res_id)
2298*f7c14bbaSAndroid Build Coastguard Worker {
2299*f7c14bbaSAndroid Build Coastguard Worker const struct btf_type *t = btf__type_by_id(btf, id);
2300*f7c14bbaSAndroid Build Coastguard Worker
2301*f7c14bbaSAndroid Build Coastguard Worker if (res_id)
2302*f7c14bbaSAndroid Build Coastguard Worker *res_id = id;
2303*f7c14bbaSAndroid Build Coastguard Worker
2304*f7c14bbaSAndroid Build Coastguard Worker while (btf_is_mod(t) || btf_is_typedef(t)) {
2305*f7c14bbaSAndroid Build Coastguard Worker if (res_id)
2306*f7c14bbaSAndroid Build Coastguard Worker *res_id = t->type;
2307*f7c14bbaSAndroid Build Coastguard Worker t = btf__type_by_id(btf, t->type);
2308*f7c14bbaSAndroid Build Coastguard Worker }
2309*f7c14bbaSAndroid Build Coastguard Worker
2310*f7c14bbaSAndroid Build Coastguard Worker return t;
2311*f7c14bbaSAndroid Build Coastguard Worker }
2312*f7c14bbaSAndroid Build Coastguard Worker
2313*f7c14bbaSAndroid Build Coastguard Worker static const struct btf_type *
resolve_func_ptr(const struct btf * btf,__u32 id,__u32 * res_id)2314*f7c14bbaSAndroid Build Coastguard Worker resolve_func_ptr(const struct btf *btf, __u32 id, __u32 *res_id)
2315*f7c14bbaSAndroid Build Coastguard Worker {
2316*f7c14bbaSAndroid Build Coastguard Worker const struct btf_type *t;
2317*f7c14bbaSAndroid Build Coastguard Worker
2318*f7c14bbaSAndroid Build Coastguard Worker t = skip_mods_and_typedefs(btf, id, NULL);
2319*f7c14bbaSAndroid Build Coastguard Worker if (!btf_is_ptr(t))
2320*f7c14bbaSAndroid Build Coastguard Worker return NULL;
2321*f7c14bbaSAndroid Build Coastguard Worker
2322*f7c14bbaSAndroid Build Coastguard Worker t = skip_mods_and_typedefs(btf, t->type, res_id);
2323*f7c14bbaSAndroid Build Coastguard Worker
2324*f7c14bbaSAndroid Build Coastguard Worker return btf_is_func_proto(t) ? t : NULL;
2325*f7c14bbaSAndroid Build Coastguard Worker }
2326*f7c14bbaSAndroid Build Coastguard Worker
__btf_kind_str(__u16 kind)2327*f7c14bbaSAndroid Build Coastguard Worker static const char *__btf_kind_str(__u16 kind)
2328*f7c14bbaSAndroid Build Coastguard Worker {
2329*f7c14bbaSAndroid Build Coastguard Worker switch (kind) {
2330*f7c14bbaSAndroid Build Coastguard Worker case BTF_KIND_UNKN: return "void";
2331*f7c14bbaSAndroid Build Coastguard Worker case BTF_KIND_INT: return "int";
2332*f7c14bbaSAndroid Build Coastguard Worker case BTF_KIND_PTR: return "ptr";
2333*f7c14bbaSAndroid Build Coastguard Worker case BTF_KIND_ARRAY: return "array";
2334*f7c14bbaSAndroid Build Coastguard Worker case BTF_KIND_STRUCT: return "struct";
2335*f7c14bbaSAndroid Build Coastguard Worker case BTF_KIND_UNION: return "union";
2336*f7c14bbaSAndroid Build Coastguard Worker case BTF_KIND_ENUM: return "enum";
2337*f7c14bbaSAndroid Build Coastguard Worker case BTF_KIND_FWD: return "fwd";
2338*f7c14bbaSAndroid Build Coastguard Worker case BTF_KIND_TYPEDEF: return "typedef";
2339*f7c14bbaSAndroid Build Coastguard Worker case BTF_KIND_VOLATILE: return "volatile";
2340*f7c14bbaSAndroid Build Coastguard Worker case BTF_KIND_CONST: return "const";
2341*f7c14bbaSAndroid Build Coastguard Worker case BTF_KIND_RESTRICT: return "restrict";
2342*f7c14bbaSAndroid Build Coastguard Worker case BTF_KIND_FUNC: return "func";
2343*f7c14bbaSAndroid Build Coastguard Worker case BTF_KIND_FUNC_PROTO: return "func_proto";
2344*f7c14bbaSAndroid Build Coastguard Worker case BTF_KIND_VAR: return "var";
2345*f7c14bbaSAndroid Build Coastguard Worker case BTF_KIND_DATASEC: return "datasec";
2346*f7c14bbaSAndroid Build Coastguard Worker case BTF_KIND_FLOAT: return "float";
2347*f7c14bbaSAndroid Build Coastguard Worker case BTF_KIND_DECL_TAG: return "decl_tag";
2348*f7c14bbaSAndroid Build Coastguard Worker case BTF_KIND_TYPE_TAG: return "type_tag";
2349*f7c14bbaSAndroid Build Coastguard Worker case BTF_KIND_ENUM64: return "enum64";
2350*f7c14bbaSAndroid Build Coastguard Worker default: return "unknown";
2351*f7c14bbaSAndroid Build Coastguard Worker }
2352*f7c14bbaSAndroid Build Coastguard Worker }
2353*f7c14bbaSAndroid Build Coastguard Worker
btf_kind_str(const struct btf_type * t)2354*f7c14bbaSAndroid Build Coastguard Worker const char *btf_kind_str(const struct btf_type *t)
2355*f7c14bbaSAndroid Build Coastguard Worker {
2356*f7c14bbaSAndroid Build Coastguard Worker return __btf_kind_str(btf_kind(t));
2357*f7c14bbaSAndroid Build Coastguard Worker }
2358*f7c14bbaSAndroid Build Coastguard Worker
2359*f7c14bbaSAndroid Build Coastguard Worker /*
2360*f7c14bbaSAndroid Build Coastguard Worker * Fetch integer attribute of BTF map definition. Such attributes are
2361*f7c14bbaSAndroid Build Coastguard Worker * represented using a pointer to an array, in which dimensionality of array
2362*f7c14bbaSAndroid Build Coastguard Worker * encodes specified integer value. E.g., int (*type)[BPF_MAP_TYPE_ARRAY];
2363*f7c14bbaSAndroid Build Coastguard Worker * encodes `type => BPF_MAP_TYPE_ARRAY` key/value pair completely using BTF
2364*f7c14bbaSAndroid Build Coastguard Worker * type definition, while using only sizeof(void *) space in ELF data section.
2365*f7c14bbaSAndroid Build Coastguard Worker */
get_map_field_int(const char * map_name,const struct btf * btf,const struct btf_member * m,__u32 * res)2366*f7c14bbaSAndroid Build Coastguard Worker static bool get_map_field_int(const char *map_name, const struct btf *btf,
2367*f7c14bbaSAndroid Build Coastguard Worker const struct btf_member *m, __u32 *res)
2368*f7c14bbaSAndroid Build Coastguard Worker {
2369*f7c14bbaSAndroid Build Coastguard Worker const struct btf_type *t = skip_mods_and_typedefs(btf, m->type, NULL);
2370*f7c14bbaSAndroid Build Coastguard Worker const char *name = btf__name_by_offset(btf, m->name_off);
2371*f7c14bbaSAndroid Build Coastguard Worker const struct btf_array *arr_info;
2372*f7c14bbaSAndroid Build Coastguard Worker const struct btf_type *arr_t;
2373*f7c14bbaSAndroid Build Coastguard Worker
2374*f7c14bbaSAndroid Build Coastguard Worker if (!btf_is_ptr(t)) {
2375*f7c14bbaSAndroid Build Coastguard Worker pr_warn("map '%s': attr '%s': expected PTR, got %s.\n",
2376*f7c14bbaSAndroid Build Coastguard Worker map_name, name, btf_kind_str(t));
2377*f7c14bbaSAndroid Build Coastguard Worker return false;
2378*f7c14bbaSAndroid Build Coastguard Worker }
2379*f7c14bbaSAndroid Build Coastguard Worker
2380*f7c14bbaSAndroid Build Coastguard Worker arr_t = btf__type_by_id(btf, t->type);
2381*f7c14bbaSAndroid Build Coastguard Worker if (!arr_t) {
2382*f7c14bbaSAndroid Build Coastguard Worker pr_warn("map '%s': attr '%s': type [%u] not found.\n",
2383*f7c14bbaSAndroid Build Coastguard Worker map_name, name, t->type);
2384*f7c14bbaSAndroid Build Coastguard Worker return false;
2385*f7c14bbaSAndroid Build Coastguard Worker }
2386*f7c14bbaSAndroid Build Coastguard Worker if (!btf_is_array(arr_t)) {
2387*f7c14bbaSAndroid Build Coastguard Worker pr_warn("map '%s': attr '%s': expected ARRAY, got %s.\n",
2388*f7c14bbaSAndroid Build Coastguard Worker map_name, name, btf_kind_str(arr_t));
2389*f7c14bbaSAndroid Build Coastguard Worker return false;
2390*f7c14bbaSAndroid Build Coastguard Worker }
2391*f7c14bbaSAndroid Build Coastguard Worker arr_info = btf_array(arr_t);
2392*f7c14bbaSAndroid Build Coastguard Worker *res = arr_info->nelems;
2393*f7c14bbaSAndroid Build Coastguard Worker return true;
2394*f7c14bbaSAndroid Build Coastguard Worker }
2395*f7c14bbaSAndroid Build Coastguard Worker
get_map_field_long(const char * map_name,const struct btf * btf,const struct btf_member * m,__u64 * res)2396*f7c14bbaSAndroid Build Coastguard Worker static bool get_map_field_long(const char *map_name, const struct btf *btf,
2397*f7c14bbaSAndroid Build Coastguard Worker const struct btf_member *m, __u64 *res)
2398*f7c14bbaSAndroid Build Coastguard Worker {
2399*f7c14bbaSAndroid Build Coastguard Worker const struct btf_type *t = skip_mods_and_typedefs(btf, m->type, NULL);
2400*f7c14bbaSAndroid Build Coastguard Worker const char *name = btf__name_by_offset(btf, m->name_off);
2401*f7c14bbaSAndroid Build Coastguard Worker
2402*f7c14bbaSAndroid Build Coastguard Worker if (btf_is_ptr(t)) {
2403*f7c14bbaSAndroid Build Coastguard Worker __u32 res32;
2404*f7c14bbaSAndroid Build Coastguard Worker bool ret;
2405*f7c14bbaSAndroid Build Coastguard Worker
2406*f7c14bbaSAndroid Build Coastguard Worker ret = get_map_field_int(map_name, btf, m, &res32);
2407*f7c14bbaSAndroid Build Coastguard Worker if (ret)
2408*f7c14bbaSAndroid Build Coastguard Worker *res = (__u64)res32;
2409*f7c14bbaSAndroid Build Coastguard Worker return ret;
2410*f7c14bbaSAndroid Build Coastguard Worker }
2411*f7c14bbaSAndroid Build Coastguard Worker
2412*f7c14bbaSAndroid Build Coastguard Worker if (!btf_is_enum(t) && !btf_is_enum64(t)) {
2413*f7c14bbaSAndroid Build Coastguard Worker pr_warn("map '%s': attr '%s': expected ENUM or ENUM64, got %s.\n",
2414*f7c14bbaSAndroid Build Coastguard Worker map_name, name, btf_kind_str(t));
2415*f7c14bbaSAndroid Build Coastguard Worker return false;
2416*f7c14bbaSAndroid Build Coastguard Worker }
2417*f7c14bbaSAndroid Build Coastguard Worker
2418*f7c14bbaSAndroid Build Coastguard Worker if (btf_vlen(t) != 1) {
2419*f7c14bbaSAndroid Build Coastguard Worker pr_warn("map '%s': attr '%s': invalid __ulong\n",
2420*f7c14bbaSAndroid Build Coastguard Worker map_name, name);
2421*f7c14bbaSAndroid Build Coastguard Worker return false;
2422*f7c14bbaSAndroid Build Coastguard Worker }
2423*f7c14bbaSAndroid Build Coastguard Worker
2424*f7c14bbaSAndroid Build Coastguard Worker if (btf_is_enum(t)) {
2425*f7c14bbaSAndroid Build Coastguard Worker const struct btf_enum *e = btf_enum(t);
2426*f7c14bbaSAndroid Build Coastguard Worker
2427*f7c14bbaSAndroid Build Coastguard Worker *res = e->val;
2428*f7c14bbaSAndroid Build Coastguard Worker } else {
2429*f7c14bbaSAndroid Build Coastguard Worker const struct btf_enum64 *e = btf_enum64(t);
2430*f7c14bbaSAndroid Build Coastguard Worker
2431*f7c14bbaSAndroid Build Coastguard Worker *res = btf_enum64_value(e);
2432*f7c14bbaSAndroid Build Coastguard Worker }
2433*f7c14bbaSAndroid Build Coastguard Worker return true;
2434*f7c14bbaSAndroid Build Coastguard Worker }
2435*f7c14bbaSAndroid Build Coastguard Worker
pathname_concat(char * buf,size_t buf_sz,const char * path,const char * name)2436*f7c14bbaSAndroid Build Coastguard Worker static int pathname_concat(char *buf, size_t buf_sz, const char *path, const char *name)
2437*f7c14bbaSAndroid Build Coastguard Worker {
2438*f7c14bbaSAndroid Build Coastguard Worker int len;
2439*f7c14bbaSAndroid Build Coastguard Worker
2440*f7c14bbaSAndroid Build Coastguard Worker len = snprintf(buf, buf_sz, "%s/%s", path, name);
2441*f7c14bbaSAndroid Build Coastguard Worker if (len < 0)
2442*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
2443*f7c14bbaSAndroid Build Coastguard Worker if (len >= buf_sz)
2444*f7c14bbaSAndroid Build Coastguard Worker return -ENAMETOOLONG;
2445*f7c14bbaSAndroid Build Coastguard Worker
2446*f7c14bbaSAndroid Build Coastguard Worker return 0;
2447*f7c14bbaSAndroid Build Coastguard Worker }
2448*f7c14bbaSAndroid Build Coastguard Worker
build_map_pin_path(struct bpf_map * map,const char * path)2449*f7c14bbaSAndroid Build Coastguard Worker static int build_map_pin_path(struct bpf_map *map, const char *path)
2450*f7c14bbaSAndroid Build Coastguard Worker {
2451*f7c14bbaSAndroid Build Coastguard Worker char buf[PATH_MAX];
2452*f7c14bbaSAndroid Build Coastguard Worker int err;
2453*f7c14bbaSAndroid Build Coastguard Worker
2454*f7c14bbaSAndroid Build Coastguard Worker if (!path)
2455*f7c14bbaSAndroid Build Coastguard Worker path = BPF_FS_DEFAULT_PATH;
2456*f7c14bbaSAndroid Build Coastguard Worker
2457*f7c14bbaSAndroid Build Coastguard Worker err = pathname_concat(buf, sizeof(buf), path, bpf_map__name(map));
2458*f7c14bbaSAndroid Build Coastguard Worker if (err)
2459*f7c14bbaSAndroid Build Coastguard Worker return err;
2460*f7c14bbaSAndroid Build Coastguard Worker
2461*f7c14bbaSAndroid Build Coastguard Worker return bpf_map__set_pin_path(map, buf);
2462*f7c14bbaSAndroid Build Coastguard Worker }
2463*f7c14bbaSAndroid Build Coastguard Worker
2464*f7c14bbaSAndroid Build Coastguard Worker /* should match definition in bpf_helpers.h */
2465*f7c14bbaSAndroid Build Coastguard Worker enum libbpf_pin_type {
2466*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_PIN_NONE,
2467*f7c14bbaSAndroid Build Coastguard Worker /* PIN_BY_NAME: pin maps by name (in /sys/fs/bpf by default) */
2468*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_PIN_BY_NAME,
2469*f7c14bbaSAndroid Build Coastguard Worker };
2470*f7c14bbaSAndroid Build Coastguard Worker
parse_btf_map_def(const char * map_name,struct btf * btf,const struct btf_type * def_t,bool strict,struct btf_map_def * map_def,struct btf_map_def * inner_def)2471*f7c14bbaSAndroid Build Coastguard Worker int parse_btf_map_def(const char *map_name, struct btf *btf,
2472*f7c14bbaSAndroid Build Coastguard Worker const struct btf_type *def_t, bool strict,
2473*f7c14bbaSAndroid Build Coastguard Worker struct btf_map_def *map_def, struct btf_map_def *inner_def)
2474*f7c14bbaSAndroid Build Coastguard Worker {
2475*f7c14bbaSAndroid Build Coastguard Worker const struct btf_type *t;
2476*f7c14bbaSAndroid Build Coastguard Worker const struct btf_member *m;
2477*f7c14bbaSAndroid Build Coastguard Worker bool is_inner = inner_def == NULL;
2478*f7c14bbaSAndroid Build Coastguard Worker int vlen, i;
2479*f7c14bbaSAndroid Build Coastguard Worker
2480*f7c14bbaSAndroid Build Coastguard Worker vlen = btf_vlen(def_t);
2481*f7c14bbaSAndroid Build Coastguard Worker m = btf_members(def_t);
2482*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < vlen; i++, m++) {
2483*f7c14bbaSAndroid Build Coastguard Worker const char *name = btf__name_by_offset(btf, m->name_off);
2484*f7c14bbaSAndroid Build Coastguard Worker
2485*f7c14bbaSAndroid Build Coastguard Worker if (!name) {
2486*f7c14bbaSAndroid Build Coastguard Worker pr_warn("map '%s': invalid field #%d.\n", map_name, i);
2487*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
2488*f7c14bbaSAndroid Build Coastguard Worker }
2489*f7c14bbaSAndroid Build Coastguard Worker if (strcmp(name, "type") == 0) {
2490*f7c14bbaSAndroid Build Coastguard Worker if (!get_map_field_int(map_name, btf, m, &map_def->map_type))
2491*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
2492*f7c14bbaSAndroid Build Coastguard Worker map_def->parts |= MAP_DEF_MAP_TYPE;
2493*f7c14bbaSAndroid Build Coastguard Worker } else if (strcmp(name, "max_entries") == 0) {
2494*f7c14bbaSAndroid Build Coastguard Worker if (!get_map_field_int(map_name, btf, m, &map_def->max_entries))
2495*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
2496*f7c14bbaSAndroid Build Coastguard Worker map_def->parts |= MAP_DEF_MAX_ENTRIES;
2497*f7c14bbaSAndroid Build Coastguard Worker } else if (strcmp(name, "map_flags") == 0) {
2498*f7c14bbaSAndroid Build Coastguard Worker if (!get_map_field_int(map_name, btf, m, &map_def->map_flags))
2499*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
2500*f7c14bbaSAndroid Build Coastguard Worker map_def->parts |= MAP_DEF_MAP_FLAGS;
2501*f7c14bbaSAndroid Build Coastguard Worker } else if (strcmp(name, "numa_node") == 0) {
2502*f7c14bbaSAndroid Build Coastguard Worker if (!get_map_field_int(map_name, btf, m, &map_def->numa_node))
2503*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
2504*f7c14bbaSAndroid Build Coastguard Worker map_def->parts |= MAP_DEF_NUMA_NODE;
2505*f7c14bbaSAndroid Build Coastguard Worker } else if (strcmp(name, "key_size") == 0) {
2506*f7c14bbaSAndroid Build Coastguard Worker __u32 sz;
2507*f7c14bbaSAndroid Build Coastguard Worker
2508*f7c14bbaSAndroid Build Coastguard Worker if (!get_map_field_int(map_name, btf, m, &sz))
2509*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
2510*f7c14bbaSAndroid Build Coastguard Worker if (map_def->key_size && map_def->key_size != sz) {
2511*f7c14bbaSAndroid Build Coastguard Worker pr_warn("map '%s': conflicting key size %u != %u.\n",
2512*f7c14bbaSAndroid Build Coastguard Worker map_name, map_def->key_size, sz);
2513*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
2514*f7c14bbaSAndroid Build Coastguard Worker }
2515*f7c14bbaSAndroid Build Coastguard Worker map_def->key_size = sz;
2516*f7c14bbaSAndroid Build Coastguard Worker map_def->parts |= MAP_DEF_KEY_SIZE;
2517*f7c14bbaSAndroid Build Coastguard Worker } else if (strcmp(name, "key") == 0) {
2518*f7c14bbaSAndroid Build Coastguard Worker __s64 sz;
2519*f7c14bbaSAndroid Build Coastguard Worker
2520*f7c14bbaSAndroid Build Coastguard Worker t = btf__type_by_id(btf, m->type);
2521*f7c14bbaSAndroid Build Coastguard Worker if (!t) {
2522*f7c14bbaSAndroid Build Coastguard Worker pr_warn("map '%s': key type [%d] not found.\n",
2523*f7c14bbaSAndroid Build Coastguard Worker map_name, m->type);
2524*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
2525*f7c14bbaSAndroid Build Coastguard Worker }
2526*f7c14bbaSAndroid Build Coastguard Worker if (!btf_is_ptr(t)) {
2527*f7c14bbaSAndroid Build Coastguard Worker pr_warn("map '%s': key spec is not PTR: %s.\n",
2528*f7c14bbaSAndroid Build Coastguard Worker map_name, btf_kind_str(t));
2529*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
2530*f7c14bbaSAndroid Build Coastguard Worker }
2531*f7c14bbaSAndroid Build Coastguard Worker sz = btf__resolve_size(btf, t->type);
2532*f7c14bbaSAndroid Build Coastguard Worker if (sz < 0) {
2533*f7c14bbaSAndroid Build Coastguard Worker pr_warn("map '%s': can't determine key size for type [%u]: %zd.\n",
2534*f7c14bbaSAndroid Build Coastguard Worker map_name, t->type, (ssize_t)sz);
2535*f7c14bbaSAndroid Build Coastguard Worker return sz;
2536*f7c14bbaSAndroid Build Coastguard Worker }
2537*f7c14bbaSAndroid Build Coastguard Worker if (map_def->key_size && map_def->key_size != sz) {
2538*f7c14bbaSAndroid Build Coastguard Worker pr_warn("map '%s': conflicting key size %u != %zd.\n",
2539*f7c14bbaSAndroid Build Coastguard Worker map_name, map_def->key_size, (ssize_t)sz);
2540*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
2541*f7c14bbaSAndroid Build Coastguard Worker }
2542*f7c14bbaSAndroid Build Coastguard Worker map_def->key_size = sz;
2543*f7c14bbaSAndroid Build Coastguard Worker map_def->key_type_id = t->type;
2544*f7c14bbaSAndroid Build Coastguard Worker map_def->parts |= MAP_DEF_KEY_SIZE | MAP_DEF_KEY_TYPE;
2545*f7c14bbaSAndroid Build Coastguard Worker } else if (strcmp(name, "value_size") == 0) {
2546*f7c14bbaSAndroid Build Coastguard Worker __u32 sz;
2547*f7c14bbaSAndroid Build Coastguard Worker
2548*f7c14bbaSAndroid Build Coastguard Worker if (!get_map_field_int(map_name, btf, m, &sz))
2549*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
2550*f7c14bbaSAndroid Build Coastguard Worker if (map_def->value_size && map_def->value_size != sz) {
2551*f7c14bbaSAndroid Build Coastguard Worker pr_warn("map '%s': conflicting value size %u != %u.\n",
2552*f7c14bbaSAndroid Build Coastguard Worker map_name, map_def->value_size, sz);
2553*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
2554*f7c14bbaSAndroid Build Coastguard Worker }
2555*f7c14bbaSAndroid Build Coastguard Worker map_def->value_size = sz;
2556*f7c14bbaSAndroid Build Coastguard Worker map_def->parts |= MAP_DEF_VALUE_SIZE;
2557*f7c14bbaSAndroid Build Coastguard Worker } else if (strcmp(name, "value") == 0) {
2558*f7c14bbaSAndroid Build Coastguard Worker __s64 sz;
2559*f7c14bbaSAndroid Build Coastguard Worker
2560*f7c14bbaSAndroid Build Coastguard Worker t = btf__type_by_id(btf, m->type);
2561*f7c14bbaSAndroid Build Coastguard Worker if (!t) {
2562*f7c14bbaSAndroid Build Coastguard Worker pr_warn("map '%s': value type [%d] not found.\n",
2563*f7c14bbaSAndroid Build Coastguard Worker map_name, m->type);
2564*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
2565*f7c14bbaSAndroid Build Coastguard Worker }
2566*f7c14bbaSAndroid Build Coastguard Worker if (!btf_is_ptr(t)) {
2567*f7c14bbaSAndroid Build Coastguard Worker pr_warn("map '%s': value spec is not PTR: %s.\n",
2568*f7c14bbaSAndroid Build Coastguard Worker map_name, btf_kind_str(t));
2569*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
2570*f7c14bbaSAndroid Build Coastguard Worker }
2571*f7c14bbaSAndroid Build Coastguard Worker sz = btf__resolve_size(btf, t->type);
2572*f7c14bbaSAndroid Build Coastguard Worker if (sz < 0) {
2573*f7c14bbaSAndroid Build Coastguard Worker pr_warn("map '%s': can't determine value size for type [%u]: %zd.\n",
2574*f7c14bbaSAndroid Build Coastguard Worker map_name, t->type, (ssize_t)sz);
2575*f7c14bbaSAndroid Build Coastguard Worker return sz;
2576*f7c14bbaSAndroid Build Coastguard Worker }
2577*f7c14bbaSAndroid Build Coastguard Worker if (map_def->value_size && map_def->value_size != sz) {
2578*f7c14bbaSAndroid Build Coastguard Worker pr_warn("map '%s': conflicting value size %u != %zd.\n",
2579*f7c14bbaSAndroid Build Coastguard Worker map_name, map_def->value_size, (ssize_t)sz);
2580*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
2581*f7c14bbaSAndroid Build Coastguard Worker }
2582*f7c14bbaSAndroid Build Coastguard Worker map_def->value_size = sz;
2583*f7c14bbaSAndroid Build Coastguard Worker map_def->value_type_id = t->type;
2584*f7c14bbaSAndroid Build Coastguard Worker map_def->parts |= MAP_DEF_VALUE_SIZE | MAP_DEF_VALUE_TYPE;
2585*f7c14bbaSAndroid Build Coastguard Worker }
2586*f7c14bbaSAndroid Build Coastguard Worker else if (strcmp(name, "values") == 0) {
2587*f7c14bbaSAndroid Build Coastguard Worker bool is_map_in_map = bpf_map_type__is_map_in_map(map_def->map_type);
2588*f7c14bbaSAndroid Build Coastguard Worker bool is_prog_array = map_def->map_type == BPF_MAP_TYPE_PROG_ARRAY;
2589*f7c14bbaSAndroid Build Coastguard Worker const char *desc = is_map_in_map ? "map-in-map inner" : "prog-array value";
2590*f7c14bbaSAndroid Build Coastguard Worker char inner_map_name[128];
2591*f7c14bbaSAndroid Build Coastguard Worker int err;
2592*f7c14bbaSAndroid Build Coastguard Worker
2593*f7c14bbaSAndroid Build Coastguard Worker if (is_inner) {
2594*f7c14bbaSAndroid Build Coastguard Worker pr_warn("map '%s': multi-level inner maps not supported.\n",
2595*f7c14bbaSAndroid Build Coastguard Worker map_name);
2596*f7c14bbaSAndroid Build Coastguard Worker return -ENOTSUP;
2597*f7c14bbaSAndroid Build Coastguard Worker }
2598*f7c14bbaSAndroid Build Coastguard Worker if (i != vlen - 1) {
2599*f7c14bbaSAndroid Build Coastguard Worker pr_warn("map '%s': '%s' member should be last.\n",
2600*f7c14bbaSAndroid Build Coastguard Worker map_name, name);
2601*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
2602*f7c14bbaSAndroid Build Coastguard Worker }
2603*f7c14bbaSAndroid Build Coastguard Worker if (!is_map_in_map && !is_prog_array) {
2604*f7c14bbaSAndroid Build Coastguard Worker pr_warn("map '%s': should be map-in-map or prog-array.\n",
2605*f7c14bbaSAndroid Build Coastguard Worker map_name);
2606*f7c14bbaSAndroid Build Coastguard Worker return -ENOTSUP;
2607*f7c14bbaSAndroid Build Coastguard Worker }
2608*f7c14bbaSAndroid Build Coastguard Worker if (map_def->value_size && map_def->value_size != 4) {
2609*f7c14bbaSAndroid Build Coastguard Worker pr_warn("map '%s': conflicting value size %u != 4.\n",
2610*f7c14bbaSAndroid Build Coastguard Worker map_name, map_def->value_size);
2611*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
2612*f7c14bbaSAndroid Build Coastguard Worker }
2613*f7c14bbaSAndroid Build Coastguard Worker map_def->value_size = 4;
2614*f7c14bbaSAndroid Build Coastguard Worker t = btf__type_by_id(btf, m->type);
2615*f7c14bbaSAndroid Build Coastguard Worker if (!t) {
2616*f7c14bbaSAndroid Build Coastguard Worker pr_warn("map '%s': %s type [%d] not found.\n",
2617*f7c14bbaSAndroid Build Coastguard Worker map_name, desc, m->type);
2618*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
2619*f7c14bbaSAndroid Build Coastguard Worker }
2620*f7c14bbaSAndroid Build Coastguard Worker if (!btf_is_array(t) || btf_array(t)->nelems) {
2621*f7c14bbaSAndroid Build Coastguard Worker pr_warn("map '%s': %s spec is not a zero-sized array.\n",
2622*f7c14bbaSAndroid Build Coastguard Worker map_name, desc);
2623*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
2624*f7c14bbaSAndroid Build Coastguard Worker }
2625*f7c14bbaSAndroid Build Coastguard Worker t = skip_mods_and_typedefs(btf, btf_array(t)->type, NULL);
2626*f7c14bbaSAndroid Build Coastguard Worker if (!btf_is_ptr(t)) {
2627*f7c14bbaSAndroid Build Coastguard Worker pr_warn("map '%s': %s def is of unexpected kind %s.\n",
2628*f7c14bbaSAndroid Build Coastguard Worker map_name, desc, btf_kind_str(t));
2629*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
2630*f7c14bbaSAndroid Build Coastguard Worker }
2631*f7c14bbaSAndroid Build Coastguard Worker t = skip_mods_and_typedefs(btf, t->type, NULL);
2632*f7c14bbaSAndroid Build Coastguard Worker if (is_prog_array) {
2633*f7c14bbaSAndroid Build Coastguard Worker if (!btf_is_func_proto(t)) {
2634*f7c14bbaSAndroid Build Coastguard Worker pr_warn("map '%s': prog-array value def is of unexpected kind %s.\n",
2635*f7c14bbaSAndroid Build Coastguard Worker map_name, btf_kind_str(t));
2636*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
2637*f7c14bbaSAndroid Build Coastguard Worker }
2638*f7c14bbaSAndroid Build Coastguard Worker continue;
2639*f7c14bbaSAndroid Build Coastguard Worker }
2640*f7c14bbaSAndroid Build Coastguard Worker if (!btf_is_struct(t)) {
2641*f7c14bbaSAndroid Build Coastguard Worker pr_warn("map '%s': map-in-map inner def is of unexpected kind %s.\n",
2642*f7c14bbaSAndroid Build Coastguard Worker map_name, btf_kind_str(t));
2643*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
2644*f7c14bbaSAndroid Build Coastguard Worker }
2645*f7c14bbaSAndroid Build Coastguard Worker
2646*f7c14bbaSAndroid Build Coastguard Worker snprintf(inner_map_name, sizeof(inner_map_name), "%s.inner", map_name);
2647*f7c14bbaSAndroid Build Coastguard Worker err = parse_btf_map_def(inner_map_name, btf, t, strict, inner_def, NULL);
2648*f7c14bbaSAndroid Build Coastguard Worker if (err)
2649*f7c14bbaSAndroid Build Coastguard Worker return err;
2650*f7c14bbaSAndroid Build Coastguard Worker
2651*f7c14bbaSAndroid Build Coastguard Worker map_def->parts |= MAP_DEF_INNER_MAP;
2652*f7c14bbaSAndroid Build Coastguard Worker } else if (strcmp(name, "pinning") == 0) {
2653*f7c14bbaSAndroid Build Coastguard Worker __u32 val;
2654*f7c14bbaSAndroid Build Coastguard Worker
2655*f7c14bbaSAndroid Build Coastguard Worker if (is_inner) {
2656*f7c14bbaSAndroid Build Coastguard Worker pr_warn("map '%s': inner def can't be pinned.\n", map_name);
2657*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
2658*f7c14bbaSAndroid Build Coastguard Worker }
2659*f7c14bbaSAndroid Build Coastguard Worker if (!get_map_field_int(map_name, btf, m, &val))
2660*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
2661*f7c14bbaSAndroid Build Coastguard Worker if (val != LIBBPF_PIN_NONE && val != LIBBPF_PIN_BY_NAME) {
2662*f7c14bbaSAndroid Build Coastguard Worker pr_warn("map '%s': invalid pinning value %u.\n",
2663*f7c14bbaSAndroid Build Coastguard Worker map_name, val);
2664*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
2665*f7c14bbaSAndroid Build Coastguard Worker }
2666*f7c14bbaSAndroid Build Coastguard Worker map_def->pinning = val;
2667*f7c14bbaSAndroid Build Coastguard Worker map_def->parts |= MAP_DEF_PINNING;
2668*f7c14bbaSAndroid Build Coastguard Worker } else if (strcmp(name, "map_extra") == 0) {
2669*f7c14bbaSAndroid Build Coastguard Worker __u64 map_extra;
2670*f7c14bbaSAndroid Build Coastguard Worker
2671*f7c14bbaSAndroid Build Coastguard Worker if (!get_map_field_long(map_name, btf, m, &map_extra))
2672*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
2673*f7c14bbaSAndroid Build Coastguard Worker map_def->map_extra = map_extra;
2674*f7c14bbaSAndroid Build Coastguard Worker map_def->parts |= MAP_DEF_MAP_EXTRA;
2675*f7c14bbaSAndroid Build Coastguard Worker } else {
2676*f7c14bbaSAndroid Build Coastguard Worker if (strict) {
2677*f7c14bbaSAndroid Build Coastguard Worker pr_warn("map '%s': unknown field '%s'.\n", map_name, name);
2678*f7c14bbaSAndroid Build Coastguard Worker return -ENOTSUP;
2679*f7c14bbaSAndroid Build Coastguard Worker }
2680*f7c14bbaSAndroid Build Coastguard Worker pr_debug("map '%s': ignoring unknown field '%s'.\n", map_name, name);
2681*f7c14bbaSAndroid Build Coastguard Worker }
2682*f7c14bbaSAndroid Build Coastguard Worker }
2683*f7c14bbaSAndroid Build Coastguard Worker
2684*f7c14bbaSAndroid Build Coastguard Worker if (map_def->map_type == BPF_MAP_TYPE_UNSPEC) {
2685*f7c14bbaSAndroid Build Coastguard Worker pr_warn("map '%s': map type isn't specified.\n", map_name);
2686*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
2687*f7c14bbaSAndroid Build Coastguard Worker }
2688*f7c14bbaSAndroid Build Coastguard Worker
2689*f7c14bbaSAndroid Build Coastguard Worker return 0;
2690*f7c14bbaSAndroid Build Coastguard Worker }
2691*f7c14bbaSAndroid Build Coastguard Worker
adjust_ringbuf_sz(size_t sz)2692*f7c14bbaSAndroid Build Coastguard Worker static size_t adjust_ringbuf_sz(size_t sz)
2693*f7c14bbaSAndroid Build Coastguard Worker {
2694*f7c14bbaSAndroid Build Coastguard Worker __u32 page_sz = sysconf(_SC_PAGE_SIZE);
2695*f7c14bbaSAndroid Build Coastguard Worker __u32 mul;
2696*f7c14bbaSAndroid Build Coastguard Worker
2697*f7c14bbaSAndroid Build Coastguard Worker /* if user forgot to set any size, make sure they see error */
2698*f7c14bbaSAndroid Build Coastguard Worker if (sz == 0)
2699*f7c14bbaSAndroid Build Coastguard Worker return 0;
2700*f7c14bbaSAndroid Build Coastguard Worker /* Kernel expects BPF_MAP_TYPE_RINGBUF's max_entries to be
2701*f7c14bbaSAndroid Build Coastguard Worker * a power-of-2 multiple of kernel's page size. If user diligently
2702*f7c14bbaSAndroid Build Coastguard Worker * satisified these conditions, pass the size through.
2703*f7c14bbaSAndroid Build Coastguard Worker */
2704*f7c14bbaSAndroid Build Coastguard Worker if ((sz % page_sz) == 0 && is_pow_of_2(sz / page_sz))
2705*f7c14bbaSAndroid Build Coastguard Worker return sz;
2706*f7c14bbaSAndroid Build Coastguard Worker
2707*f7c14bbaSAndroid Build Coastguard Worker /* Otherwise find closest (page_sz * power_of_2) product bigger than
2708*f7c14bbaSAndroid Build Coastguard Worker * user-set size to satisfy both user size request and kernel
2709*f7c14bbaSAndroid Build Coastguard Worker * requirements and substitute correct max_entries for map creation.
2710*f7c14bbaSAndroid Build Coastguard Worker */
2711*f7c14bbaSAndroid Build Coastguard Worker for (mul = 1; mul <= UINT_MAX / page_sz; mul <<= 1) {
2712*f7c14bbaSAndroid Build Coastguard Worker if (mul * page_sz > sz)
2713*f7c14bbaSAndroid Build Coastguard Worker return mul * page_sz;
2714*f7c14bbaSAndroid Build Coastguard Worker }
2715*f7c14bbaSAndroid Build Coastguard Worker
2716*f7c14bbaSAndroid Build Coastguard Worker /* if it's impossible to satisfy the conditions (i.e., user size is
2717*f7c14bbaSAndroid Build Coastguard Worker * very close to UINT_MAX but is not a power-of-2 multiple of
2718*f7c14bbaSAndroid Build Coastguard Worker * page_size) then just return original size and let kernel reject it
2719*f7c14bbaSAndroid Build Coastguard Worker */
2720*f7c14bbaSAndroid Build Coastguard Worker return sz;
2721*f7c14bbaSAndroid Build Coastguard Worker }
2722*f7c14bbaSAndroid Build Coastguard Worker
map_is_ringbuf(const struct bpf_map * map)2723*f7c14bbaSAndroid Build Coastguard Worker static bool map_is_ringbuf(const struct bpf_map *map)
2724*f7c14bbaSAndroid Build Coastguard Worker {
2725*f7c14bbaSAndroid Build Coastguard Worker return map->def.type == BPF_MAP_TYPE_RINGBUF ||
2726*f7c14bbaSAndroid Build Coastguard Worker map->def.type == BPF_MAP_TYPE_USER_RINGBUF;
2727*f7c14bbaSAndroid Build Coastguard Worker }
2728*f7c14bbaSAndroid Build Coastguard Worker
fill_map_from_def(struct bpf_map * map,const struct btf_map_def * def)2729*f7c14bbaSAndroid Build Coastguard Worker static void fill_map_from_def(struct bpf_map *map, const struct btf_map_def *def)
2730*f7c14bbaSAndroid Build Coastguard Worker {
2731*f7c14bbaSAndroid Build Coastguard Worker map->def.type = def->map_type;
2732*f7c14bbaSAndroid Build Coastguard Worker map->def.key_size = def->key_size;
2733*f7c14bbaSAndroid Build Coastguard Worker map->def.value_size = def->value_size;
2734*f7c14bbaSAndroid Build Coastguard Worker map->def.max_entries = def->max_entries;
2735*f7c14bbaSAndroid Build Coastguard Worker map->def.map_flags = def->map_flags;
2736*f7c14bbaSAndroid Build Coastguard Worker map->map_extra = def->map_extra;
2737*f7c14bbaSAndroid Build Coastguard Worker
2738*f7c14bbaSAndroid Build Coastguard Worker map->numa_node = def->numa_node;
2739*f7c14bbaSAndroid Build Coastguard Worker map->btf_key_type_id = def->key_type_id;
2740*f7c14bbaSAndroid Build Coastguard Worker map->btf_value_type_id = def->value_type_id;
2741*f7c14bbaSAndroid Build Coastguard Worker
2742*f7c14bbaSAndroid Build Coastguard Worker /* auto-adjust BPF ringbuf map max_entries to be a multiple of page size */
2743*f7c14bbaSAndroid Build Coastguard Worker if (map_is_ringbuf(map))
2744*f7c14bbaSAndroid Build Coastguard Worker map->def.max_entries = adjust_ringbuf_sz(map->def.max_entries);
2745*f7c14bbaSAndroid Build Coastguard Worker
2746*f7c14bbaSAndroid Build Coastguard Worker if (def->parts & MAP_DEF_MAP_TYPE)
2747*f7c14bbaSAndroid Build Coastguard Worker pr_debug("map '%s': found type = %u.\n", map->name, def->map_type);
2748*f7c14bbaSAndroid Build Coastguard Worker
2749*f7c14bbaSAndroid Build Coastguard Worker if (def->parts & MAP_DEF_KEY_TYPE)
2750*f7c14bbaSAndroid Build Coastguard Worker pr_debug("map '%s': found key [%u], sz = %u.\n",
2751*f7c14bbaSAndroid Build Coastguard Worker map->name, def->key_type_id, def->key_size);
2752*f7c14bbaSAndroid Build Coastguard Worker else if (def->parts & MAP_DEF_KEY_SIZE)
2753*f7c14bbaSAndroid Build Coastguard Worker pr_debug("map '%s': found key_size = %u.\n", map->name, def->key_size);
2754*f7c14bbaSAndroid Build Coastguard Worker
2755*f7c14bbaSAndroid Build Coastguard Worker if (def->parts & MAP_DEF_VALUE_TYPE)
2756*f7c14bbaSAndroid Build Coastguard Worker pr_debug("map '%s': found value [%u], sz = %u.\n",
2757*f7c14bbaSAndroid Build Coastguard Worker map->name, def->value_type_id, def->value_size);
2758*f7c14bbaSAndroid Build Coastguard Worker else if (def->parts & MAP_DEF_VALUE_SIZE)
2759*f7c14bbaSAndroid Build Coastguard Worker pr_debug("map '%s': found value_size = %u.\n", map->name, def->value_size);
2760*f7c14bbaSAndroid Build Coastguard Worker
2761*f7c14bbaSAndroid Build Coastguard Worker if (def->parts & MAP_DEF_MAX_ENTRIES)
2762*f7c14bbaSAndroid Build Coastguard Worker pr_debug("map '%s': found max_entries = %u.\n", map->name, def->max_entries);
2763*f7c14bbaSAndroid Build Coastguard Worker if (def->parts & MAP_DEF_MAP_FLAGS)
2764*f7c14bbaSAndroid Build Coastguard Worker pr_debug("map '%s': found map_flags = 0x%x.\n", map->name, def->map_flags);
2765*f7c14bbaSAndroid Build Coastguard Worker if (def->parts & MAP_DEF_MAP_EXTRA)
2766*f7c14bbaSAndroid Build Coastguard Worker pr_debug("map '%s': found map_extra = 0x%llx.\n", map->name,
2767*f7c14bbaSAndroid Build Coastguard Worker (unsigned long long)def->map_extra);
2768*f7c14bbaSAndroid Build Coastguard Worker if (def->parts & MAP_DEF_PINNING)
2769*f7c14bbaSAndroid Build Coastguard Worker pr_debug("map '%s': found pinning = %u.\n", map->name, def->pinning);
2770*f7c14bbaSAndroid Build Coastguard Worker if (def->parts & MAP_DEF_NUMA_NODE)
2771*f7c14bbaSAndroid Build Coastguard Worker pr_debug("map '%s': found numa_node = %u.\n", map->name, def->numa_node);
2772*f7c14bbaSAndroid Build Coastguard Worker
2773*f7c14bbaSAndroid Build Coastguard Worker if (def->parts & MAP_DEF_INNER_MAP)
2774*f7c14bbaSAndroid Build Coastguard Worker pr_debug("map '%s': found inner map definition.\n", map->name);
2775*f7c14bbaSAndroid Build Coastguard Worker }
2776*f7c14bbaSAndroid Build Coastguard Worker
btf_var_linkage_str(__u32 linkage)2777*f7c14bbaSAndroid Build Coastguard Worker static const char *btf_var_linkage_str(__u32 linkage)
2778*f7c14bbaSAndroid Build Coastguard Worker {
2779*f7c14bbaSAndroid Build Coastguard Worker switch (linkage) {
2780*f7c14bbaSAndroid Build Coastguard Worker case BTF_VAR_STATIC: return "static";
2781*f7c14bbaSAndroid Build Coastguard Worker case BTF_VAR_GLOBAL_ALLOCATED: return "global";
2782*f7c14bbaSAndroid Build Coastguard Worker case BTF_VAR_GLOBAL_EXTERN: return "extern";
2783*f7c14bbaSAndroid Build Coastguard Worker default: return "unknown";
2784*f7c14bbaSAndroid Build Coastguard Worker }
2785*f7c14bbaSAndroid Build Coastguard Worker }
2786*f7c14bbaSAndroid Build Coastguard Worker
bpf_object__init_user_btf_map(struct bpf_object * obj,const struct btf_type * sec,int var_idx,int sec_idx,const Elf_Data * data,bool strict,const char * pin_root_path)2787*f7c14bbaSAndroid Build Coastguard Worker static int bpf_object__init_user_btf_map(struct bpf_object *obj,
2788*f7c14bbaSAndroid Build Coastguard Worker const struct btf_type *sec,
2789*f7c14bbaSAndroid Build Coastguard Worker int var_idx, int sec_idx,
2790*f7c14bbaSAndroid Build Coastguard Worker const Elf_Data *data, bool strict,
2791*f7c14bbaSAndroid Build Coastguard Worker const char *pin_root_path)
2792*f7c14bbaSAndroid Build Coastguard Worker {
2793*f7c14bbaSAndroid Build Coastguard Worker struct btf_map_def map_def = {}, inner_def = {};
2794*f7c14bbaSAndroid Build Coastguard Worker const struct btf_type *var, *def;
2795*f7c14bbaSAndroid Build Coastguard Worker const struct btf_var_secinfo *vi;
2796*f7c14bbaSAndroid Build Coastguard Worker const struct btf_var *var_extra;
2797*f7c14bbaSAndroid Build Coastguard Worker const char *map_name;
2798*f7c14bbaSAndroid Build Coastguard Worker struct bpf_map *map;
2799*f7c14bbaSAndroid Build Coastguard Worker int err;
2800*f7c14bbaSAndroid Build Coastguard Worker
2801*f7c14bbaSAndroid Build Coastguard Worker vi = btf_var_secinfos(sec) + var_idx;
2802*f7c14bbaSAndroid Build Coastguard Worker var = btf__type_by_id(obj->btf, vi->type);
2803*f7c14bbaSAndroid Build Coastguard Worker var_extra = btf_var(var);
2804*f7c14bbaSAndroid Build Coastguard Worker map_name = btf__name_by_offset(obj->btf, var->name_off);
2805*f7c14bbaSAndroid Build Coastguard Worker
2806*f7c14bbaSAndroid Build Coastguard Worker if (map_name == NULL || map_name[0] == '\0') {
2807*f7c14bbaSAndroid Build Coastguard Worker pr_warn("map #%d: empty name.\n", var_idx);
2808*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
2809*f7c14bbaSAndroid Build Coastguard Worker }
2810*f7c14bbaSAndroid Build Coastguard Worker if ((__u64)vi->offset + vi->size > data->d_size) {
2811*f7c14bbaSAndroid Build Coastguard Worker pr_warn("map '%s' BTF data is corrupted.\n", map_name);
2812*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
2813*f7c14bbaSAndroid Build Coastguard Worker }
2814*f7c14bbaSAndroid Build Coastguard Worker if (!btf_is_var(var)) {
2815*f7c14bbaSAndroid Build Coastguard Worker pr_warn("map '%s': unexpected var kind %s.\n",
2816*f7c14bbaSAndroid Build Coastguard Worker map_name, btf_kind_str(var));
2817*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
2818*f7c14bbaSAndroid Build Coastguard Worker }
2819*f7c14bbaSAndroid Build Coastguard Worker if (var_extra->linkage != BTF_VAR_GLOBAL_ALLOCATED) {
2820*f7c14bbaSAndroid Build Coastguard Worker pr_warn("map '%s': unsupported map linkage %s.\n",
2821*f7c14bbaSAndroid Build Coastguard Worker map_name, btf_var_linkage_str(var_extra->linkage));
2822*f7c14bbaSAndroid Build Coastguard Worker return -EOPNOTSUPP;
2823*f7c14bbaSAndroid Build Coastguard Worker }
2824*f7c14bbaSAndroid Build Coastguard Worker
2825*f7c14bbaSAndroid Build Coastguard Worker def = skip_mods_and_typedefs(obj->btf, var->type, NULL);
2826*f7c14bbaSAndroid Build Coastguard Worker if (!btf_is_struct(def)) {
2827*f7c14bbaSAndroid Build Coastguard Worker pr_warn("map '%s': unexpected def kind %s.\n",
2828*f7c14bbaSAndroid Build Coastguard Worker map_name, btf_kind_str(var));
2829*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
2830*f7c14bbaSAndroid Build Coastguard Worker }
2831*f7c14bbaSAndroid Build Coastguard Worker if (def->size > vi->size) {
2832*f7c14bbaSAndroid Build Coastguard Worker pr_warn("map '%s': invalid def size.\n", map_name);
2833*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
2834*f7c14bbaSAndroid Build Coastguard Worker }
2835*f7c14bbaSAndroid Build Coastguard Worker
2836*f7c14bbaSAndroid Build Coastguard Worker map = bpf_object__add_map(obj);
2837*f7c14bbaSAndroid Build Coastguard Worker if (IS_ERR(map))
2838*f7c14bbaSAndroid Build Coastguard Worker return PTR_ERR(map);
2839*f7c14bbaSAndroid Build Coastguard Worker map->name = strdup(map_name);
2840*f7c14bbaSAndroid Build Coastguard Worker if (!map->name) {
2841*f7c14bbaSAndroid Build Coastguard Worker pr_warn("map '%s': failed to alloc map name.\n", map_name);
2842*f7c14bbaSAndroid Build Coastguard Worker return -ENOMEM;
2843*f7c14bbaSAndroid Build Coastguard Worker }
2844*f7c14bbaSAndroid Build Coastguard Worker map->libbpf_type = LIBBPF_MAP_UNSPEC;
2845*f7c14bbaSAndroid Build Coastguard Worker map->def.type = BPF_MAP_TYPE_UNSPEC;
2846*f7c14bbaSAndroid Build Coastguard Worker map->sec_idx = sec_idx;
2847*f7c14bbaSAndroid Build Coastguard Worker map->sec_offset = vi->offset;
2848*f7c14bbaSAndroid Build Coastguard Worker map->btf_var_idx = var_idx;
2849*f7c14bbaSAndroid Build Coastguard Worker pr_debug("map '%s': at sec_idx %d, offset %zu.\n",
2850*f7c14bbaSAndroid Build Coastguard Worker map_name, map->sec_idx, map->sec_offset);
2851*f7c14bbaSAndroid Build Coastguard Worker
2852*f7c14bbaSAndroid Build Coastguard Worker err = parse_btf_map_def(map->name, obj->btf, def, strict, &map_def, &inner_def);
2853*f7c14bbaSAndroid Build Coastguard Worker if (err)
2854*f7c14bbaSAndroid Build Coastguard Worker return err;
2855*f7c14bbaSAndroid Build Coastguard Worker
2856*f7c14bbaSAndroid Build Coastguard Worker fill_map_from_def(map, &map_def);
2857*f7c14bbaSAndroid Build Coastguard Worker
2858*f7c14bbaSAndroid Build Coastguard Worker if (map_def.pinning == LIBBPF_PIN_BY_NAME) {
2859*f7c14bbaSAndroid Build Coastguard Worker err = build_map_pin_path(map, pin_root_path);
2860*f7c14bbaSAndroid Build Coastguard Worker if (err) {
2861*f7c14bbaSAndroid Build Coastguard Worker pr_warn("map '%s': couldn't build pin path.\n", map->name);
2862*f7c14bbaSAndroid Build Coastguard Worker return err;
2863*f7c14bbaSAndroid Build Coastguard Worker }
2864*f7c14bbaSAndroid Build Coastguard Worker }
2865*f7c14bbaSAndroid Build Coastguard Worker
2866*f7c14bbaSAndroid Build Coastguard Worker if (map_def.parts & MAP_DEF_INNER_MAP) {
2867*f7c14bbaSAndroid Build Coastguard Worker map->inner_map = calloc(1, sizeof(*map->inner_map));
2868*f7c14bbaSAndroid Build Coastguard Worker if (!map->inner_map)
2869*f7c14bbaSAndroid Build Coastguard Worker return -ENOMEM;
2870*f7c14bbaSAndroid Build Coastguard Worker map->inner_map->fd = create_placeholder_fd();
2871*f7c14bbaSAndroid Build Coastguard Worker if (map->inner_map->fd < 0)
2872*f7c14bbaSAndroid Build Coastguard Worker return map->inner_map->fd;
2873*f7c14bbaSAndroid Build Coastguard Worker map->inner_map->sec_idx = sec_idx;
2874*f7c14bbaSAndroid Build Coastguard Worker map->inner_map->name = malloc(strlen(map_name) + sizeof(".inner") + 1);
2875*f7c14bbaSAndroid Build Coastguard Worker if (!map->inner_map->name)
2876*f7c14bbaSAndroid Build Coastguard Worker return -ENOMEM;
2877*f7c14bbaSAndroid Build Coastguard Worker sprintf(map->inner_map->name, "%s.inner", map_name);
2878*f7c14bbaSAndroid Build Coastguard Worker
2879*f7c14bbaSAndroid Build Coastguard Worker fill_map_from_def(map->inner_map, &inner_def);
2880*f7c14bbaSAndroid Build Coastguard Worker }
2881*f7c14bbaSAndroid Build Coastguard Worker
2882*f7c14bbaSAndroid Build Coastguard Worker err = map_fill_btf_type_info(obj, map);
2883*f7c14bbaSAndroid Build Coastguard Worker if (err)
2884*f7c14bbaSAndroid Build Coastguard Worker return err;
2885*f7c14bbaSAndroid Build Coastguard Worker
2886*f7c14bbaSAndroid Build Coastguard Worker return 0;
2887*f7c14bbaSAndroid Build Coastguard Worker }
2888*f7c14bbaSAndroid Build Coastguard Worker
init_arena_map_data(struct bpf_object * obj,struct bpf_map * map,const char * sec_name,int sec_idx,void * data,size_t data_sz)2889*f7c14bbaSAndroid Build Coastguard Worker static int init_arena_map_data(struct bpf_object *obj, struct bpf_map *map,
2890*f7c14bbaSAndroid Build Coastguard Worker const char *sec_name, int sec_idx,
2891*f7c14bbaSAndroid Build Coastguard Worker void *data, size_t data_sz)
2892*f7c14bbaSAndroid Build Coastguard Worker {
2893*f7c14bbaSAndroid Build Coastguard Worker const long page_sz = sysconf(_SC_PAGE_SIZE);
2894*f7c14bbaSAndroid Build Coastguard Worker size_t mmap_sz;
2895*f7c14bbaSAndroid Build Coastguard Worker
2896*f7c14bbaSAndroid Build Coastguard Worker mmap_sz = bpf_map_mmap_sz(obj->arena_map);
2897*f7c14bbaSAndroid Build Coastguard Worker if (roundup(data_sz, page_sz) > mmap_sz) {
2898*f7c14bbaSAndroid Build Coastguard Worker pr_warn("elf: sec '%s': declared ARENA map size (%zu) is too small to hold global __arena variables of size %zu\n",
2899*f7c14bbaSAndroid Build Coastguard Worker sec_name, mmap_sz, data_sz);
2900*f7c14bbaSAndroid Build Coastguard Worker return -E2BIG;
2901*f7c14bbaSAndroid Build Coastguard Worker }
2902*f7c14bbaSAndroid Build Coastguard Worker
2903*f7c14bbaSAndroid Build Coastguard Worker obj->arena_data = malloc(data_sz);
2904*f7c14bbaSAndroid Build Coastguard Worker if (!obj->arena_data)
2905*f7c14bbaSAndroid Build Coastguard Worker return -ENOMEM;
2906*f7c14bbaSAndroid Build Coastguard Worker memcpy(obj->arena_data, data, data_sz);
2907*f7c14bbaSAndroid Build Coastguard Worker obj->arena_data_sz = data_sz;
2908*f7c14bbaSAndroid Build Coastguard Worker
2909*f7c14bbaSAndroid Build Coastguard Worker /* make bpf_map__init_value() work for ARENA maps */
2910*f7c14bbaSAndroid Build Coastguard Worker map->mmaped = obj->arena_data;
2911*f7c14bbaSAndroid Build Coastguard Worker
2912*f7c14bbaSAndroid Build Coastguard Worker return 0;
2913*f7c14bbaSAndroid Build Coastguard Worker }
2914*f7c14bbaSAndroid Build Coastguard Worker
bpf_object__init_user_btf_maps(struct bpf_object * obj,bool strict,const char * pin_root_path)2915*f7c14bbaSAndroid Build Coastguard Worker static int bpf_object__init_user_btf_maps(struct bpf_object *obj, bool strict,
2916*f7c14bbaSAndroid Build Coastguard Worker const char *pin_root_path)
2917*f7c14bbaSAndroid Build Coastguard Worker {
2918*f7c14bbaSAndroid Build Coastguard Worker const struct btf_type *sec = NULL;
2919*f7c14bbaSAndroid Build Coastguard Worker int nr_types, i, vlen, err;
2920*f7c14bbaSAndroid Build Coastguard Worker const struct btf_type *t;
2921*f7c14bbaSAndroid Build Coastguard Worker const char *name;
2922*f7c14bbaSAndroid Build Coastguard Worker Elf_Data *data;
2923*f7c14bbaSAndroid Build Coastguard Worker Elf_Scn *scn;
2924*f7c14bbaSAndroid Build Coastguard Worker
2925*f7c14bbaSAndroid Build Coastguard Worker if (obj->efile.btf_maps_shndx < 0)
2926*f7c14bbaSAndroid Build Coastguard Worker return 0;
2927*f7c14bbaSAndroid Build Coastguard Worker
2928*f7c14bbaSAndroid Build Coastguard Worker scn = elf_sec_by_idx(obj, obj->efile.btf_maps_shndx);
2929*f7c14bbaSAndroid Build Coastguard Worker data = elf_sec_data(obj, scn);
2930*f7c14bbaSAndroid Build Coastguard Worker if (!scn || !data) {
2931*f7c14bbaSAndroid Build Coastguard Worker pr_warn("elf: failed to get %s map definitions for %s\n",
2932*f7c14bbaSAndroid Build Coastguard Worker MAPS_ELF_SEC, obj->path);
2933*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
2934*f7c14bbaSAndroid Build Coastguard Worker }
2935*f7c14bbaSAndroid Build Coastguard Worker
2936*f7c14bbaSAndroid Build Coastguard Worker nr_types = btf__type_cnt(obj->btf);
2937*f7c14bbaSAndroid Build Coastguard Worker for (i = 1; i < nr_types; i++) {
2938*f7c14bbaSAndroid Build Coastguard Worker t = btf__type_by_id(obj->btf, i);
2939*f7c14bbaSAndroid Build Coastguard Worker if (!btf_is_datasec(t))
2940*f7c14bbaSAndroid Build Coastguard Worker continue;
2941*f7c14bbaSAndroid Build Coastguard Worker name = btf__name_by_offset(obj->btf, t->name_off);
2942*f7c14bbaSAndroid Build Coastguard Worker if (strcmp(name, MAPS_ELF_SEC) == 0) {
2943*f7c14bbaSAndroid Build Coastguard Worker sec = t;
2944*f7c14bbaSAndroid Build Coastguard Worker obj->efile.btf_maps_sec_btf_id = i;
2945*f7c14bbaSAndroid Build Coastguard Worker break;
2946*f7c14bbaSAndroid Build Coastguard Worker }
2947*f7c14bbaSAndroid Build Coastguard Worker }
2948*f7c14bbaSAndroid Build Coastguard Worker
2949*f7c14bbaSAndroid Build Coastguard Worker if (!sec) {
2950*f7c14bbaSAndroid Build Coastguard Worker pr_warn("DATASEC '%s' not found.\n", MAPS_ELF_SEC);
2951*f7c14bbaSAndroid Build Coastguard Worker return -ENOENT;
2952*f7c14bbaSAndroid Build Coastguard Worker }
2953*f7c14bbaSAndroid Build Coastguard Worker
2954*f7c14bbaSAndroid Build Coastguard Worker vlen = btf_vlen(sec);
2955*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < vlen; i++) {
2956*f7c14bbaSAndroid Build Coastguard Worker err = bpf_object__init_user_btf_map(obj, sec, i,
2957*f7c14bbaSAndroid Build Coastguard Worker obj->efile.btf_maps_shndx,
2958*f7c14bbaSAndroid Build Coastguard Worker data, strict,
2959*f7c14bbaSAndroid Build Coastguard Worker pin_root_path);
2960*f7c14bbaSAndroid Build Coastguard Worker if (err)
2961*f7c14bbaSAndroid Build Coastguard Worker return err;
2962*f7c14bbaSAndroid Build Coastguard Worker }
2963*f7c14bbaSAndroid Build Coastguard Worker
2964*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < obj->nr_maps; i++) {
2965*f7c14bbaSAndroid Build Coastguard Worker struct bpf_map *map = &obj->maps[i];
2966*f7c14bbaSAndroid Build Coastguard Worker
2967*f7c14bbaSAndroid Build Coastguard Worker if (map->def.type != BPF_MAP_TYPE_ARENA)
2968*f7c14bbaSAndroid Build Coastguard Worker continue;
2969*f7c14bbaSAndroid Build Coastguard Worker
2970*f7c14bbaSAndroid Build Coastguard Worker if (obj->arena_map) {
2971*f7c14bbaSAndroid Build Coastguard Worker pr_warn("map '%s': only single ARENA map is supported (map '%s' is also ARENA)\n",
2972*f7c14bbaSAndroid Build Coastguard Worker map->name, obj->arena_map->name);
2973*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
2974*f7c14bbaSAndroid Build Coastguard Worker }
2975*f7c14bbaSAndroid Build Coastguard Worker obj->arena_map = map;
2976*f7c14bbaSAndroid Build Coastguard Worker
2977*f7c14bbaSAndroid Build Coastguard Worker if (obj->efile.arena_data) {
2978*f7c14bbaSAndroid Build Coastguard Worker err = init_arena_map_data(obj, map, ARENA_SEC, obj->efile.arena_data_shndx,
2979*f7c14bbaSAndroid Build Coastguard Worker obj->efile.arena_data->d_buf,
2980*f7c14bbaSAndroid Build Coastguard Worker obj->efile.arena_data->d_size);
2981*f7c14bbaSAndroid Build Coastguard Worker if (err)
2982*f7c14bbaSAndroid Build Coastguard Worker return err;
2983*f7c14bbaSAndroid Build Coastguard Worker }
2984*f7c14bbaSAndroid Build Coastguard Worker }
2985*f7c14bbaSAndroid Build Coastguard Worker if (obj->efile.arena_data && !obj->arena_map) {
2986*f7c14bbaSAndroid Build Coastguard Worker pr_warn("elf: sec '%s': to use global __arena variables the ARENA map should be explicitly declared in SEC(\".maps\")\n",
2987*f7c14bbaSAndroid Build Coastguard Worker ARENA_SEC);
2988*f7c14bbaSAndroid Build Coastguard Worker return -ENOENT;
2989*f7c14bbaSAndroid Build Coastguard Worker }
2990*f7c14bbaSAndroid Build Coastguard Worker
2991*f7c14bbaSAndroid Build Coastguard Worker return 0;
2992*f7c14bbaSAndroid Build Coastguard Worker }
2993*f7c14bbaSAndroid Build Coastguard Worker
bpf_object__init_maps(struct bpf_object * obj,const struct bpf_object_open_opts * opts)2994*f7c14bbaSAndroid Build Coastguard Worker static int bpf_object__init_maps(struct bpf_object *obj,
2995*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_object_open_opts *opts)
2996*f7c14bbaSAndroid Build Coastguard Worker {
2997*f7c14bbaSAndroid Build Coastguard Worker const char *pin_root_path;
2998*f7c14bbaSAndroid Build Coastguard Worker bool strict;
2999*f7c14bbaSAndroid Build Coastguard Worker int err = 0;
3000*f7c14bbaSAndroid Build Coastguard Worker
3001*f7c14bbaSAndroid Build Coastguard Worker strict = !OPTS_GET(opts, relaxed_maps, false);
3002*f7c14bbaSAndroid Build Coastguard Worker pin_root_path = OPTS_GET(opts, pin_root_path, NULL);
3003*f7c14bbaSAndroid Build Coastguard Worker
3004*f7c14bbaSAndroid Build Coastguard Worker err = bpf_object__init_user_btf_maps(obj, strict, pin_root_path);
3005*f7c14bbaSAndroid Build Coastguard Worker err = err ?: bpf_object__init_global_data_maps(obj);
3006*f7c14bbaSAndroid Build Coastguard Worker err = err ?: bpf_object__init_kconfig_map(obj);
3007*f7c14bbaSAndroid Build Coastguard Worker err = err ?: bpf_object_init_struct_ops(obj);
3008*f7c14bbaSAndroid Build Coastguard Worker
3009*f7c14bbaSAndroid Build Coastguard Worker return err;
3010*f7c14bbaSAndroid Build Coastguard Worker }
3011*f7c14bbaSAndroid Build Coastguard Worker
section_have_execinstr(struct bpf_object * obj,int idx)3012*f7c14bbaSAndroid Build Coastguard Worker static bool section_have_execinstr(struct bpf_object *obj, int idx)
3013*f7c14bbaSAndroid Build Coastguard Worker {
3014*f7c14bbaSAndroid Build Coastguard Worker Elf64_Shdr *sh;
3015*f7c14bbaSAndroid Build Coastguard Worker
3016*f7c14bbaSAndroid Build Coastguard Worker sh = elf_sec_hdr(obj, elf_sec_by_idx(obj, idx));
3017*f7c14bbaSAndroid Build Coastguard Worker if (!sh)
3018*f7c14bbaSAndroid Build Coastguard Worker return false;
3019*f7c14bbaSAndroid Build Coastguard Worker
3020*f7c14bbaSAndroid Build Coastguard Worker return sh->sh_flags & SHF_EXECINSTR;
3021*f7c14bbaSAndroid Build Coastguard Worker }
3022*f7c14bbaSAndroid Build Coastguard Worker
starts_with_qmark(const char * s)3023*f7c14bbaSAndroid Build Coastguard Worker static bool starts_with_qmark(const char *s)
3024*f7c14bbaSAndroid Build Coastguard Worker {
3025*f7c14bbaSAndroid Build Coastguard Worker return s && s[0] == '?';
3026*f7c14bbaSAndroid Build Coastguard Worker }
3027*f7c14bbaSAndroid Build Coastguard Worker
btf_needs_sanitization(struct bpf_object * obj)3028*f7c14bbaSAndroid Build Coastguard Worker static bool btf_needs_sanitization(struct bpf_object *obj)
3029*f7c14bbaSAndroid Build Coastguard Worker {
3030*f7c14bbaSAndroid Build Coastguard Worker bool has_func_global = kernel_supports(obj, FEAT_BTF_GLOBAL_FUNC);
3031*f7c14bbaSAndroid Build Coastguard Worker bool has_datasec = kernel_supports(obj, FEAT_BTF_DATASEC);
3032*f7c14bbaSAndroid Build Coastguard Worker bool has_float = kernel_supports(obj, FEAT_BTF_FLOAT);
3033*f7c14bbaSAndroid Build Coastguard Worker bool has_func = kernel_supports(obj, FEAT_BTF_FUNC);
3034*f7c14bbaSAndroid Build Coastguard Worker bool has_decl_tag = kernel_supports(obj, FEAT_BTF_DECL_TAG);
3035*f7c14bbaSAndroid Build Coastguard Worker bool has_type_tag = kernel_supports(obj, FEAT_BTF_TYPE_TAG);
3036*f7c14bbaSAndroid Build Coastguard Worker bool has_enum64 = kernel_supports(obj, FEAT_BTF_ENUM64);
3037*f7c14bbaSAndroid Build Coastguard Worker bool has_qmark_datasec = kernel_supports(obj, FEAT_BTF_QMARK_DATASEC);
3038*f7c14bbaSAndroid Build Coastguard Worker
3039*f7c14bbaSAndroid Build Coastguard Worker return !has_func || !has_datasec || !has_func_global || !has_float ||
3040*f7c14bbaSAndroid Build Coastguard Worker !has_decl_tag || !has_type_tag || !has_enum64 || !has_qmark_datasec;
3041*f7c14bbaSAndroid Build Coastguard Worker }
3042*f7c14bbaSAndroid Build Coastguard Worker
bpf_object__sanitize_btf(struct bpf_object * obj,struct btf * btf)3043*f7c14bbaSAndroid Build Coastguard Worker static int bpf_object__sanitize_btf(struct bpf_object *obj, struct btf *btf)
3044*f7c14bbaSAndroid Build Coastguard Worker {
3045*f7c14bbaSAndroid Build Coastguard Worker bool has_func_global = kernel_supports(obj, FEAT_BTF_GLOBAL_FUNC);
3046*f7c14bbaSAndroid Build Coastguard Worker bool has_datasec = kernel_supports(obj, FEAT_BTF_DATASEC);
3047*f7c14bbaSAndroid Build Coastguard Worker bool has_float = kernel_supports(obj, FEAT_BTF_FLOAT);
3048*f7c14bbaSAndroid Build Coastguard Worker bool has_func = kernel_supports(obj, FEAT_BTF_FUNC);
3049*f7c14bbaSAndroid Build Coastguard Worker bool has_decl_tag = kernel_supports(obj, FEAT_BTF_DECL_TAG);
3050*f7c14bbaSAndroid Build Coastguard Worker bool has_type_tag = kernel_supports(obj, FEAT_BTF_TYPE_TAG);
3051*f7c14bbaSAndroid Build Coastguard Worker bool has_enum64 = kernel_supports(obj, FEAT_BTF_ENUM64);
3052*f7c14bbaSAndroid Build Coastguard Worker bool has_qmark_datasec = kernel_supports(obj, FEAT_BTF_QMARK_DATASEC);
3053*f7c14bbaSAndroid Build Coastguard Worker int enum64_placeholder_id = 0;
3054*f7c14bbaSAndroid Build Coastguard Worker struct btf_type *t;
3055*f7c14bbaSAndroid Build Coastguard Worker int i, j, vlen;
3056*f7c14bbaSAndroid Build Coastguard Worker
3057*f7c14bbaSAndroid Build Coastguard Worker for (i = 1; i < btf__type_cnt(btf); i++) {
3058*f7c14bbaSAndroid Build Coastguard Worker t = (struct btf_type *)btf__type_by_id(btf, i);
3059*f7c14bbaSAndroid Build Coastguard Worker
3060*f7c14bbaSAndroid Build Coastguard Worker if ((!has_datasec && btf_is_var(t)) || (!has_decl_tag && btf_is_decl_tag(t))) {
3061*f7c14bbaSAndroid Build Coastguard Worker /* replace VAR/DECL_TAG with INT */
3062*f7c14bbaSAndroid Build Coastguard Worker t->info = BTF_INFO_ENC(BTF_KIND_INT, 0, 0);
3063*f7c14bbaSAndroid Build Coastguard Worker /*
3064*f7c14bbaSAndroid Build Coastguard Worker * using size = 1 is the safest choice, 4 will be too
3065*f7c14bbaSAndroid Build Coastguard Worker * big and cause kernel BTF validation failure if
3066*f7c14bbaSAndroid Build Coastguard Worker * original variable took less than 4 bytes
3067*f7c14bbaSAndroid Build Coastguard Worker */
3068*f7c14bbaSAndroid Build Coastguard Worker t->size = 1;
3069*f7c14bbaSAndroid Build Coastguard Worker *(int *)(t + 1) = BTF_INT_ENC(0, 0, 8);
3070*f7c14bbaSAndroid Build Coastguard Worker } else if (!has_datasec && btf_is_datasec(t)) {
3071*f7c14bbaSAndroid Build Coastguard Worker /* replace DATASEC with STRUCT */
3072*f7c14bbaSAndroid Build Coastguard Worker const struct btf_var_secinfo *v = btf_var_secinfos(t);
3073*f7c14bbaSAndroid Build Coastguard Worker struct btf_member *m = btf_members(t);
3074*f7c14bbaSAndroid Build Coastguard Worker struct btf_type *vt;
3075*f7c14bbaSAndroid Build Coastguard Worker char *name;
3076*f7c14bbaSAndroid Build Coastguard Worker
3077*f7c14bbaSAndroid Build Coastguard Worker name = (char *)btf__name_by_offset(btf, t->name_off);
3078*f7c14bbaSAndroid Build Coastguard Worker while (*name) {
3079*f7c14bbaSAndroid Build Coastguard Worker if (*name == '.' || *name == '?')
3080*f7c14bbaSAndroid Build Coastguard Worker *name = '_';
3081*f7c14bbaSAndroid Build Coastguard Worker name++;
3082*f7c14bbaSAndroid Build Coastguard Worker }
3083*f7c14bbaSAndroid Build Coastguard Worker
3084*f7c14bbaSAndroid Build Coastguard Worker vlen = btf_vlen(t);
3085*f7c14bbaSAndroid Build Coastguard Worker t->info = BTF_INFO_ENC(BTF_KIND_STRUCT, 0, vlen);
3086*f7c14bbaSAndroid Build Coastguard Worker for (j = 0; j < vlen; j++, v++, m++) {
3087*f7c14bbaSAndroid Build Coastguard Worker /* order of field assignments is important */
3088*f7c14bbaSAndroid Build Coastguard Worker m->offset = v->offset * 8;
3089*f7c14bbaSAndroid Build Coastguard Worker m->type = v->type;
3090*f7c14bbaSAndroid Build Coastguard Worker /* preserve variable name as member name */
3091*f7c14bbaSAndroid Build Coastguard Worker vt = (void *)btf__type_by_id(btf, v->type);
3092*f7c14bbaSAndroid Build Coastguard Worker m->name_off = vt->name_off;
3093*f7c14bbaSAndroid Build Coastguard Worker }
3094*f7c14bbaSAndroid Build Coastguard Worker } else if (!has_qmark_datasec && btf_is_datasec(t) &&
3095*f7c14bbaSAndroid Build Coastguard Worker starts_with_qmark(btf__name_by_offset(btf, t->name_off))) {
3096*f7c14bbaSAndroid Build Coastguard Worker /* replace '?' prefix with '_' for DATASEC names */
3097*f7c14bbaSAndroid Build Coastguard Worker char *name;
3098*f7c14bbaSAndroid Build Coastguard Worker
3099*f7c14bbaSAndroid Build Coastguard Worker name = (char *)btf__name_by_offset(btf, t->name_off);
3100*f7c14bbaSAndroid Build Coastguard Worker if (name[0] == '?')
3101*f7c14bbaSAndroid Build Coastguard Worker name[0] = '_';
3102*f7c14bbaSAndroid Build Coastguard Worker } else if (!has_func && btf_is_func_proto(t)) {
3103*f7c14bbaSAndroid Build Coastguard Worker /* replace FUNC_PROTO with ENUM */
3104*f7c14bbaSAndroid Build Coastguard Worker vlen = btf_vlen(t);
3105*f7c14bbaSAndroid Build Coastguard Worker t->info = BTF_INFO_ENC(BTF_KIND_ENUM, 0, vlen);
3106*f7c14bbaSAndroid Build Coastguard Worker t->size = sizeof(__u32); /* kernel enforced */
3107*f7c14bbaSAndroid Build Coastguard Worker } else if (!has_func && btf_is_func(t)) {
3108*f7c14bbaSAndroid Build Coastguard Worker /* replace FUNC with TYPEDEF */
3109*f7c14bbaSAndroid Build Coastguard Worker t->info = BTF_INFO_ENC(BTF_KIND_TYPEDEF, 0, 0);
3110*f7c14bbaSAndroid Build Coastguard Worker } else if (!has_func_global && btf_is_func(t)) {
3111*f7c14bbaSAndroid Build Coastguard Worker /* replace BTF_FUNC_GLOBAL with BTF_FUNC_STATIC */
3112*f7c14bbaSAndroid Build Coastguard Worker t->info = BTF_INFO_ENC(BTF_KIND_FUNC, 0, 0);
3113*f7c14bbaSAndroid Build Coastguard Worker } else if (!has_float && btf_is_float(t)) {
3114*f7c14bbaSAndroid Build Coastguard Worker /* replace FLOAT with an equally-sized empty STRUCT;
3115*f7c14bbaSAndroid Build Coastguard Worker * since C compilers do not accept e.g. "float" as a
3116*f7c14bbaSAndroid Build Coastguard Worker * valid struct name, make it anonymous
3117*f7c14bbaSAndroid Build Coastguard Worker */
3118*f7c14bbaSAndroid Build Coastguard Worker t->name_off = 0;
3119*f7c14bbaSAndroid Build Coastguard Worker t->info = BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 0);
3120*f7c14bbaSAndroid Build Coastguard Worker } else if (!has_type_tag && btf_is_type_tag(t)) {
3121*f7c14bbaSAndroid Build Coastguard Worker /* replace TYPE_TAG with a CONST */
3122*f7c14bbaSAndroid Build Coastguard Worker t->name_off = 0;
3123*f7c14bbaSAndroid Build Coastguard Worker t->info = BTF_INFO_ENC(BTF_KIND_CONST, 0, 0);
3124*f7c14bbaSAndroid Build Coastguard Worker } else if (!has_enum64 && btf_is_enum(t)) {
3125*f7c14bbaSAndroid Build Coastguard Worker /* clear the kflag */
3126*f7c14bbaSAndroid Build Coastguard Worker t->info = btf_type_info(btf_kind(t), btf_vlen(t), false);
3127*f7c14bbaSAndroid Build Coastguard Worker } else if (!has_enum64 && btf_is_enum64(t)) {
3128*f7c14bbaSAndroid Build Coastguard Worker /* replace ENUM64 with a union */
3129*f7c14bbaSAndroid Build Coastguard Worker struct btf_member *m;
3130*f7c14bbaSAndroid Build Coastguard Worker
3131*f7c14bbaSAndroid Build Coastguard Worker if (enum64_placeholder_id == 0) {
3132*f7c14bbaSAndroid Build Coastguard Worker enum64_placeholder_id = btf__add_int(btf, "enum64_placeholder", 1, 0);
3133*f7c14bbaSAndroid Build Coastguard Worker if (enum64_placeholder_id < 0)
3134*f7c14bbaSAndroid Build Coastguard Worker return enum64_placeholder_id;
3135*f7c14bbaSAndroid Build Coastguard Worker
3136*f7c14bbaSAndroid Build Coastguard Worker t = (struct btf_type *)btf__type_by_id(btf, i);
3137*f7c14bbaSAndroid Build Coastguard Worker }
3138*f7c14bbaSAndroid Build Coastguard Worker
3139*f7c14bbaSAndroid Build Coastguard Worker m = btf_members(t);
3140*f7c14bbaSAndroid Build Coastguard Worker vlen = btf_vlen(t);
3141*f7c14bbaSAndroid Build Coastguard Worker t->info = BTF_INFO_ENC(BTF_KIND_UNION, 0, vlen);
3142*f7c14bbaSAndroid Build Coastguard Worker for (j = 0; j < vlen; j++, m++) {
3143*f7c14bbaSAndroid Build Coastguard Worker m->type = enum64_placeholder_id;
3144*f7c14bbaSAndroid Build Coastguard Worker m->offset = 0;
3145*f7c14bbaSAndroid Build Coastguard Worker }
3146*f7c14bbaSAndroid Build Coastguard Worker }
3147*f7c14bbaSAndroid Build Coastguard Worker }
3148*f7c14bbaSAndroid Build Coastguard Worker
3149*f7c14bbaSAndroid Build Coastguard Worker return 0;
3150*f7c14bbaSAndroid Build Coastguard Worker }
3151*f7c14bbaSAndroid Build Coastguard Worker
libbpf_needs_btf(const struct bpf_object * obj)3152*f7c14bbaSAndroid Build Coastguard Worker static bool libbpf_needs_btf(const struct bpf_object *obj)
3153*f7c14bbaSAndroid Build Coastguard Worker {
3154*f7c14bbaSAndroid Build Coastguard Worker return obj->efile.btf_maps_shndx >= 0 ||
3155*f7c14bbaSAndroid Build Coastguard Worker obj->efile.has_st_ops ||
3156*f7c14bbaSAndroid Build Coastguard Worker obj->nr_extern > 0;
3157*f7c14bbaSAndroid Build Coastguard Worker }
3158*f7c14bbaSAndroid Build Coastguard Worker
kernel_needs_btf(const struct bpf_object * obj)3159*f7c14bbaSAndroid Build Coastguard Worker static bool kernel_needs_btf(const struct bpf_object *obj)
3160*f7c14bbaSAndroid Build Coastguard Worker {
3161*f7c14bbaSAndroid Build Coastguard Worker return obj->efile.has_st_ops;
3162*f7c14bbaSAndroid Build Coastguard Worker }
3163*f7c14bbaSAndroid Build Coastguard Worker
bpf_object__init_btf(struct bpf_object * obj,Elf_Data * btf_data,Elf_Data * btf_ext_data)3164*f7c14bbaSAndroid Build Coastguard Worker static int bpf_object__init_btf(struct bpf_object *obj,
3165*f7c14bbaSAndroid Build Coastguard Worker Elf_Data *btf_data,
3166*f7c14bbaSAndroid Build Coastguard Worker Elf_Data *btf_ext_data)
3167*f7c14bbaSAndroid Build Coastguard Worker {
3168*f7c14bbaSAndroid Build Coastguard Worker int err = -ENOENT;
3169*f7c14bbaSAndroid Build Coastguard Worker
3170*f7c14bbaSAndroid Build Coastguard Worker if (btf_data) {
3171*f7c14bbaSAndroid Build Coastguard Worker obj->btf = btf__new(btf_data->d_buf, btf_data->d_size);
3172*f7c14bbaSAndroid Build Coastguard Worker err = libbpf_get_error(obj->btf);
3173*f7c14bbaSAndroid Build Coastguard Worker if (err) {
3174*f7c14bbaSAndroid Build Coastguard Worker obj->btf = NULL;
3175*f7c14bbaSAndroid Build Coastguard Worker pr_warn("Error loading ELF section %s: %d.\n", BTF_ELF_SEC, err);
3176*f7c14bbaSAndroid Build Coastguard Worker goto out;
3177*f7c14bbaSAndroid Build Coastguard Worker }
3178*f7c14bbaSAndroid Build Coastguard Worker /* enforce 8-byte pointers for BPF-targeted BTFs */
3179*f7c14bbaSAndroid Build Coastguard Worker btf__set_pointer_size(obj->btf, 8);
3180*f7c14bbaSAndroid Build Coastguard Worker }
3181*f7c14bbaSAndroid Build Coastguard Worker if (btf_ext_data) {
3182*f7c14bbaSAndroid Build Coastguard Worker struct btf_ext_info *ext_segs[3];
3183*f7c14bbaSAndroid Build Coastguard Worker int seg_num, sec_num;
3184*f7c14bbaSAndroid Build Coastguard Worker
3185*f7c14bbaSAndroid Build Coastguard Worker if (!obj->btf) {
3186*f7c14bbaSAndroid Build Coastguard Worker pr_debug("Ignore ELF section %s because its depending ELF section %s is not found.\n",
3187*f7c14bbaSAndroid Build Coastguard Worker BTF_EXT_ELF_SEC, BTF_ELF_SEC);
3188*f7c14bbaSAndroid Build Coastguard Worker goto out;
3189*f7c14bbaSAndroid Build Coastguard Worker }
3190*f7c14bbaSAndroid Build Coastguard Worker obj->btf_ext = btf_ext__new(btf_ext_data->d_buf, btf_ext_data->d_size);
3191*f7c14bbaSAndroid Build Coastguard Worker err = libbpf_get_error(obj->btf_ext);
3192*f7c14bbaSAndroid Build Coastguard Worker if (err) {
3193*f7c14bbaSAndroid Build Coastguard Worker pr_warn("Error loading ELF section %s: %d. Ignored and continue.\n",
3194*f7c14bbaSAndroid Build Coastguard Worker BTF_EXT_ELF_SEC, err);
3195*f7c14bbaSAndroid Build Coastguard Worker obj->btf_ext = NULL;
3196*f7c14bbaSAndroid Build Coastguard Worker goto out;
3197*f7c14bbaSAndroid Build Coastguard Worker }
3198*f7c14bbaSAndroid Build Coastguard Worker
3199*f7c14bbaSAndroid Build Coastguard Worker /* setup .BTF.ext to ELF section mapping */
3200*f7c14bbaSAndroid Build Coastguard Worker ext_segs[0] = &obj->btf_ext->func_info;
3201*f7c14bbaSAndroid Build Coastguard Worker ext_segs[1] = &obj->btf_ext->line_info;
3202*f7c14bbaSAndroid Build Coastguard Worker ext_segs[2] = &obj->btf_ext->core_relo_info;
3203*f7c14bbaSAndroid Build Coastguard Worker for (seg_num = 0; seg_num < ARRAY_SIZE(ext_segs); seg_num++) {
3204*f7c14bbaSAndroid Build Coastguard Worker struct btf_ext_info *seg = ext_segs[seg_num];
3205*f7c14bbaSAndroid Build Coastguard Worker const struct btf_ext_info_sec *sec;
3206*f7c14bbaSAndroid Build Coastguard Worker const char *sec_name;
3207*f7c14bbaSAndroid Build Coastguard Worker Elf_Scn *scn;
3208*f7c14bbaSAndroid Build Coastguard Worker
3209*f7c14bbaSAndroid Build Coastguard Worker if (seg->sec_cnt == 0)
3210*f7c14bbaSAndroid Build Coastguard Worker continue;
3211*f7c14bbaSAndroid Build Coastguard Worker
3212*f7c14bbaSAndroid Build Coastguard Worker seg->sec_idxs = calloc(seg->sec_cnt, sizeof(*seg->sec_idxs));
3213*f7c14bbaSAndroid Build Coastguard Worker if (!seg->sec_idxs) {
3214*f7c14bbaSAndroid Build Coastguard Worker err = -ENOMEM;
3215*f7c14bbaSAndroid Build Coastguard Worker goto out;
3216*f7c14bbaSAndroid Build Coastguard Worker }
3217*f7c14bbaSAndroid Build Coastguard Worker
3218*f7c14bbaSAndroid Build Coastguard Worker sec_num = 0;
3219*f7c14bbaSAndroid Build Coastguard Worker for_each_btf_ext_sec(seg, sec) {
3220*f7c14bbaSAndroid Build Coastguard Worker /* preventively increment index to avoid doing
3221*f7c14bbaSAndroid Build Coastguard Worker * this before every continue below
3222*f7c14bbaSAndroid Build Coastguard Worker */
3223*f7c14bbaSAndroid Build Coastguard Worker sec_num++;
3224*f7c14bbaSAndroid Build Coastguard Worker
3225*f7c14bbaSAndroid Build Coastguard Worker sec_name = btf__name_by_offset(obj->btf, sec->sec_name_off);
3226*f7c14bbaSAndroid Build Coastguard Worker if (str_is_empty(sec_name))
3227*f7c14bbaSAndroid Build Coastguard Worker continue;
3228*f7c14bbaSAndroid Build Coastguard Worker scn = elf_sec_by_name(obj, sec_name);
3229*f7c14bbaSAndroid Build Coastguard Worker if (!scn)
3230*f7c14bbaSAndroid Build Coastguard Worker continue;
3231*f7c14bbaSAndroid Build Coastguard Worker
3232*f7c14bbaSAndroid Build Coastguard Worker seg->sec_idxs[sec_num - 1] = elf_ndxscn(scn);
3233*f7c14bbaSAndroid Build Coastguard Worker }
3234*f7c14bbaSAndroid Build Coastguard Worker }
3235*f7c14bbaSAndroid Build Coastguard Worker }
3236*f7c14bbaSAndroid Build Coastguard Worker out:
3237*f7c14bbaSAndroid Build Coastguard Worker if (err && libbpf_needs_btf(obj)) {
3238*f7c14bbaSAndroid Build Coastguard Worker pr_warn("BTF is required, but is missing or corrupted.\n");
3239*f7c14bbaSAndroid Build Coastguard Worker return err;
3240*f7c14bbaSAndroid Build Coastguard Worker }
3241*f7c14bbaSAndroid Build Coastguard Worker return 0;
3242*f7c14bbaSAndroid Build Coastguard Worker }
3243*f7c14bbaSAndroid Build Coastguard Worker
compare_vsi_off(const void * _a,const void * _b)3244*f7c14bbaSAndroid Build Coastguard Worker static int compare_vsi_off(const void *_a, const void *_b)
3245*f7c14bbaSAndroid Build Coastguard Worker {
3246*f7c14bbaSAndroid Build Coastguard Worker const struct btf_var_secinfo *a = _a;
3247*f7c14bbaSAndroid Build Coastguard Worker const struct btf_var_secinfo *b = _b;
3248*f7c14bbaSAndroid Build Coastguard Worker
3249*f7c14bbaSAndroid Build Coastguard Worker return a->offset - b->offset;
3250*f7c14bbaSAndroid Build Coastguard Worker }
3251*f7c14bbaSAndroid Build Coastguard Worker
btf_fixup_datasec(struct bpf_object * obj,struct btf * btf,struct btf_type * t)3252*f7c14bbaSAndroid Build Coastguard Worker static int btf_fixup_datasec(struct bpf_object *obj, struct btf *btf,
3253*f7c14bbaSAndroid Build Coastguard Worker struct btf_type *t)
3254*f7c14bbaSAndroid Build Coastguard Worker {
3255*f7c14bbaSAndroid Build Coastguard Worker __u32 size = 0, i, vars = btf_vlen(t);
3256*f7c14bbaSAndroid Build Coastguard Worker const char *sec_name = btf__name_by_offset(btf, t->name_off);
3257*f7c14bbaSAndroid Build Coastguard Worker struct btf_var_secinfo *vsi;
3258*f7c14bbaSAndroid Build Coastguard Worker bool fixup_offsets = false;
3259*f7c14bbaSAndroid Build Coastguard Worker int err;
3260*f7c14bbaSAndroid Build Coastguard Worker
3261*f7c14bbaSAndroid Build Coastguard Worker if (!sec_name) {
3262*f7c14bbaSAndroid Build Coastguard Worker pr_debug("No name found in string section for DATASEC kind.\n");
3263*f7c14bbaSAndroid Build Coastguard Worker return -ENOENT;
3264*f7c14bbaSAndroid Build Coastguard Worker }
3265*f7c14bbaSAndroid Build Coastguard Worker
3266*f7c14bbaSAndroid Build Coastguard Worker /* Extern-backing datasecs (.ksyms, .kconfig) have their size and
3267*f7c14bbaSAndroid Build Coastguard Worker * variable offsets set at the previous step. Further, not every
3268*f7c14bbaSAndroid Build Coastguard Worker * extern BTF VAR has corresponding ELF symbol preserved, so we skip
3269*f7c14bbaSAndroid Build Coastguard Worker * all fixups altogether for such sections and go straight to sorting
3270*f7c14bbaSAndroid Build Coastguard Worker * VARs within their DATASEC.
3271*f7c14bbaSAndroid Build Coastguard Worker */
3272*f7c14bbaSAndroid Build Coastguard Worker if (strcmp(sec_name, KCONFIG_SEC) == 0 || strcmp(sec_name, KSYMS_SEC) == 0)
3273*f7c14bbaSAndroid Build Coastguard Worker goto sort_vars;
3274*f7c14bbaSAndroid Build Coastguard Worker
3275*f7c14bbaSAndroid Build Coastguard Worker /* Clang leaves DATASEC size and VAR offsets as zeroes, so we need to
3276*f7c14bbaSAndroid Build Coastguard Worker * fix this up. But BPF static linker already fixes this up and fills
3277*f7c14bbaSAndroid Build Coastguard Worker * all the sizes and offsets during static linking. So this step has
3278*f7c14bbaSAndroid Build Coastguard Worker * to be optional. But the STV_HIDDEN handling is non-optional for any
3279*f7c14bbaSAndroid Build Coastguard Worker * non-extern DATASEC, so the variable fixup loop below handles both
3280*f7c14bbaSAndroid Build Coastguard Worker * functions at the same time, paying the cost of BTF VAR <-> ELF
3281*f7c14bbaSAndroid Build Coastguard Worker * symbol matching just once.
3282*f7c14bbaSAndroid Build Coastguard Worker */
3283*f7c14bbaSAndroid Build Coastguard Worker if (t->size == 0) {
3284*f7c14bbaSAndroid Build Coastguard Worker err = find_elf_sec_sz(obj, sec_name, &size);
3285*f7c14bbaSAndroid Build Coastguard Worker if (err || !size) {
3286*f7c14bbaSAndroid Build Coastguard Worker pr_debug("sec '%s': failed to determine size from ELF: size %u, err %d\n",
3287*f7c14bbaSAndroid Build Coastguard Worker sec_name, size, err);
3288*f7c14bbaSAndroid Build Coastguard Worker return -ENOENT;
3289*f7c14bbaSAndroid Build Coastguard Worker }
3290*f7c14bbaSAndroid Build Coastguard Worker
3291*f7c14bbaSAndroid Build Coastguard Worker t->size = size;
3292*f7c14bbaSAndroid Build Coastguard Worker fixup_offsets = true;
3293*f7c14bbaSAndroid Build Coastguard Worker }
3294*f7c14bbaSAndroid Build Coastguard Worker
3295*f7c14bbaSAndroid Build Coastguard Worker for (i = 0, vsi = btf_var_secinfos(t); i < vars; i++, vsi++) {
3296*f7c14bbaSAndroid Build Coastguard Worker const struct btf_type *t_var;
3297*f7c14bbaSAndroid Build Coastguard Worker struct btf_var *var;
3298*f7c14bbaSAndroid Build Coastguard Worker const char *var_name;
3299*f7c14bbaSAndroid Build Coastguard Worker Elf64_Sym *sym;
3300*f7c14bbaSAndroid Build Coastguard Worker
3301*f7c14bbaSAndroid Build Coastguard Worker t_var = btf__type_by_id(btf, vsi->type);
3302*f7c14bbaSAndroid Build Coastguard Worker if (!t_var || !btf_is_var(t_var)) {
3303*f7c14bbaSAndroid Build Coastguard Worker pr_debug("sec '%s': unexpected non-VAR type found\n", sec_name);
3304*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
3305*f7c14bbaSAndroid Build Coastguard Worker }
3306*f7c14bbaSAndroid Build Coastguard Worker
3307*f7c14bbaSAndroid Build Coastguard Worker var = btf_var(t_var);
3308*f7c14bbaSAndroid Build Coastguard Worker if (var->linkage == BTF_VAR_STATIC || var->linkage == BTF_VAR_GLOBAL_EXTERN)
3309*f7c14bbaSAndroid Build Coastguard Worker continue;
3310*f7c14bbaSAndroid Build Coastguard Worker
3311*f7c14bbaSAndroid Build Coastguard Worker var_name = btf__name_by_offset(btf, t_var->name_off);
3312*f7c14bbaSAndroid Build Coastguard Worker if (!var_name) {
3313*f7c14bbaSAndroid Build Coastguard Worker pr_debug("sec '%s': failed to find name of DATASEC's member #%d\n",
3314*f7c14bbaSAndroid Build Coastguard Worker sec_name, i);
3315*f7c14bbaSAndroid Build Coastguard Worker return -ENOENT;
3316*f7c14bbaSAndroid Build Coastguard Worker }
3317*f7c14bbaSAndroid Build Coastguard Worker
3318*f7c14bbaSAndroid Build Coastguard Worker sym = find_elf_var_sym(obj, var_name);
3319*f7c14bbaSAndroid Build Coastguard Worker if (IS_ERR(sym)) {
3320*f7c14bbaSAndroid Build Coastguard Worker pr_debug("sec '%s': failed to find ELF symbol for VAR '%s'\n",
3321*f7c14bbaSAndroid Build Coastguard Worker sec_name, var_name);
3322*f7c14bbaSAndroid Build Coastguard Worker return -ENOENT;
3323*f7c14bbaSAndroid Build Coastguard Worker }
3324*f7c14bbaSAndroid Build Coastguard Worker
3325*f7c14bbaSAndroid Build Coastguard Worker if (fixup_offsets)
3326*f7c14bbaSAndroid Build Coastguard Worker vsi->offset = sym->st_value;
3327*f7c14bbaSAndroid Build Coastguard Worker
3328*f7c14bbaSAndroid Build Coastguard Worker /* if variable is a global/weak symbol, but has restricted
3329*f7c14bbaSAndroid Build Coastguard Worker * (STV_HIDDEN or STV_INTERNAL) visibility, mark its BTF VAR
3330*f7c14bbaSAndroid Build Coastguard Worker * as static. This follows similar logic for functions (BPF
3331*f7c14bbaSAndroid Build Coastguard Worker * subprogs) and influences libbpf's further decisions about
3332*f7c14bbaSAndroid Build Coastguard Worker * whether to make global data BPF array maps as
3333*f7c14bbaSAndroid Build Coastguard Worker * BPF_F_MMAPABLE.
3334*f7c14bbaSAndroid Build Coastguard Worker */
3335*f7c14bbaSAndroid Build Coastguard Worker if (ELF64_ST_VISIBILITY(sym->st_other) == STV_HIDDEN
3336*f7c14bbaSAndroid Build Coastguard Worker || ELF64_ST_VISIBILITY(sym->st_other) == STV_INTERNAL)
3337*f7c14bbaSAndroid Build Coastguard Worker var->linkage = BTF_VAR_STATIC;
3338*f7c14bbaSAndroid Build Coastguard Worker }
3339*f7c14bbaSAndroid Build Coastguard Worker
3340*f7c14bbaSAndroid Build Coastguard Worker sort_vars:
3341*f7c14bbaSAndroid Build Coastguard Worker qsort(btf_var_secinfos(t), vars, sizeof(*vsi), compare_vsi_off);
3342*f7c14bbaSAndroid Build Coastguard Worker return 0;
3343*f7c14bbaSAndroid Build Coastguard Worker }
3344*f7c14bbaSAndroid Build Coastguard Worker
bpf_object_fixup_btf(struct bpf_object * obj)3345*f7c14bbaSAndroid Build Coastguard Worker static int bpf_object_fixup_btf(struct bpf_object *obj)
3346*f7c14bbaSAndroid Build Coastguard Worker {
3347*f7c14bbaSAndroid Build Coastguard Worker int i, n, err = 0;
3348*f7c14bbaSAndroid Build Coastguard Worker
3349*f7c14bbaSAndroid Build Coastguard Worker if (!obj->btf)
3350*f7c14bbaSAndroid Build Coastguard Worker return 0;
3351*f7c14bbaSAndroid Build Coastguard Worker
3352*f7c14bbaSAndroid Build Coastguard Worker n = btf__type_cnt(obj->btf);
3353*f7c14bbaSAndroid Build Coastguard Worker for (i = 1; i < n; i++) {
3354*f7c14bbaSAndroid Build Coastguard Worker struct btf_type *t = btf_type_by_id(obj->btf, i);
3355*f7c14bbaSAndroid Build Coastguard Worker
3356*f7c14bbaSAndroid Build Coastguard Worker /* Loader needs to fix up some of the things compiler
3357*f7c14bbaSAndroid Build Coastguard Worker * couldn't get its hands on while emitting BTF. This
3358*f7c14bbaSAndroid Build Coastguard Worker * is section size and global variable offset. We use
3359*f7c14bbaSAndroid Build Coastguard Worker * the info from the ELF itself for this purpose.
3360*f7c14bbaSAndroid Build Coastguard Worker */
3361*f7c14bbaSAndroid Build Coastguard Worker if (btf_is_datasec(t)) {
3362*f7c14bbaSAndroid Build Coastguard Worker err = btf_fixup_datasec(obj, obj->btf, t);
3363*f7c14bbaSAndroid Build Coastguard Worker if (err)
3364*f7c14bbaSAndroid Build Coastguard Worker return err;
3365*f7c14bbaSAndroid Build Coastguard Worker }
3366*f7c14bbaSAndroid Build Coastguard Worker }
3367*f7c14bbaSAndroid Build Coastguard Worker
3368*f7c14bbaSAndroid Build Coastguard Worker return 0;
3369*f7c14bbaSAndroid Build Coastguard Worker }
3370*f7c14bbaSAndroid Build Coastguard Worker
prog_needs_vmlinux_btf(struct bpf_program * prog)3371*f7c14bbaSAndroid Build Coastguard Worker static bool prog_needs_vmlinux_btf(struct bpf_program *prog)
3372*f7c14bbaSAndroid Build Coastguard Worker {
3373*f7c14bbaSAndroid Build Coastguard Worker if (prog->type == BPF_PROG_TYPE_STRUCT_OPS ||
3374*f7c14bbaSAndroid Build Coastguard Worker prog->type == BPF_PROG_TYPE_LSM)
3375*f7c14bbaSAndroid Build Coastguard Worker return true;
3376*f7c14bbaSAndroid Build Coastguard Worker
3377*f7c14bbaSAndroid Build Coastguard Worker /* BPF_PROG_TYPE_TRACING programs which do not attach to other programs
3378*f7c14bbaSAndroid Build Coastguard Worker * also need vmlinux BTF
3379*f7c14bbaSAndroid Build Coastguard Worker */
3380*f7c14bbaSAndroid Build Coastguard Worker if (prog->type == BPF_PROG_TYPE_TRACING && !prog->attach_prog_fd)
3381*f7c14bbaSAndroid Build Coastguard Worker return true;
3382*f7c14bbaSAndroid Build Coastguard Worker
3383*f7c14bbaSAndroid Build Coastguard Worker return false;
3384*f7c14bbaSAndroid Build Coastguard Worker }
3385*f7c14bbaSAndroid Build Coastguard Worker
map_needs_vmlinux_btf(struct bpf_map * map)3386*f7c14bbaSAndroid Build Coastguard Worker static bool map_needs_vmlinux_btf(struct bpf_map *map)
3387*f7c14bbaSAndroid Build Coastguard Worker {
3388*f7c14bbaSAndroid Build Coastguard Worker return bpf_map__is_struct_ops(map);
3389*f7c14bbaSAndroid Build Coastguard Worker }
3390*f7c14bbaSAndroid Build Coastguard Worker
obj_needs_vmlinux_btf(const struct bpf_object * obj)3391*f7c14bbaSAndroid Build Coastguard Worker static bool obj_needs_vmlinux_btf(const struct bpf_object *obj)
3392*f7c14bbaSAndroid Build Coastguard Worker {
3393*f7c14bbaSAndroid Build Coastguard Worker struct bpf_program *prog;
3394*f7c14bbaSAndroid Build Coastguard Worker struct bpf_map *map;
3395*f7c14bbaSAndroid Build Coastguard Worker int i;
3396*f7c14bbaSAndroid Build Coastguard Worker
3397*f7c14bbaSAndroid Build Coastguard Worker /* CO-RE relocations need kernel BTF, only when btf_custom_path
3398*f7c14bbaSAndroid Build Coastguard Worker * is not specified
3399*f7c14bbaSAndroid Build Coastguard Worker */
3400*f7c14bbaSAndroid Build Coastguard Worker if (obj->btf_ext && obj->btf_ext->core_relo_info.len && !obj->btf_custom_path)
3401*f7c14bbaSAndroid Build Coastguard Worker return true;
3402*f7c14bbaSAndroid Build Coastguard Worker
3403*f7c14bbaSAndroid Build Coastguard Worker /* Support for typed ksyms needs kernel BTF */
3404*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < obj->nr_extern; i++) {
3405*f7c14bbaSAndroid Build Coastguard Worker const struct extern_desc *ext;
3406*f7c14bbaSAndroid Build Coastguard Worker
3407*f7c14bbaSAndroid Build Coastguard Worker ext = &obj->externs[i];
3408*f7c14bbaSAndroid Build Coastguard Worker if (ext->type == EXT_KSYM && ext->ksym.type_id)
3409*f7c14bbaSAndroid Build Coastguard Worker return true;
3410*f7c14bbaSAndroid Build Coastguard Worker }
3411*f7c14bbaSAndroid Build Coastguard Worker
3412*f7c14bbaSAndroid Build Coastguard Worker bpf_object__for_each_program(prog, obj) {
3413*f7c14bbaSAndroid Build Coastguard Worker if (!prog->autoload)
3414*f7c14bbaSAndroid Build Coastguard Worker continue;
3415*f7c14bbaSAndroid Build Coastguard Worker if (prog_needs_vmlinux_btf(prog))
3416*f7c14bbaSAndroid Build Coastguard Worker return true;
3417*f7c14bbaSAndroid Build Coastguard Worker }
3418*f7c14bbaSAndroid Build Coastguard Worker
3419*f7c14bbaSAndroid Build Coastguard Worker bpf_object__for_each_map(map, obj) {
3420*f7c14bbaSAndroid Build Coastguard Worker if (map_needs_vmlinux_btf(map))
3421*f7c14bbaSAndroid Build Coastguard Worker return true;
3422*f7c14bbaSAndroid Build Coastguard Worker }
3423*f7c14bbaSAndroid Build Coastguard Worker
3424*f7c14bbaSAndroid Build Coastguard Worker return false;
3425*f7c14bbaSAndroid Build Coastguard Worker }
3426*f7c14bbaSAndroid Build Coastguard Worker
bpf_object__load_vmlinux_btf(struct bpf_object * obj,bool force)3427*f7c14bbaSAndroid Build Coastguard Worker static int bpf_object__load_vmlinux_btf(struct bpf_object *obj, bool force)
3428*f7c14bbaSAndroid Build Coastguard Worker {
3429*f7c14bbaSAndroid Build Coastguard Worker int err;
3430*f7c14bbaSAndroid Build Coastguard Worker
3431*f7c14bbaSAndroid Build Coastguard Worker /* btf_vmlinux could be loaded earlier */
3432*f7c14bbaSAndroid Build Coastguard Worker if (obj->btf_vmlinux || obj->gen_loader)
3433*f7c14bbaSAndroid Build Coastguard Worker return 0;
3434*f7c14bbaSAndroid Build Coastguard Worker
3435*f7c14bbaSAndroid Build Coastguard Worker if (!force && !obj_needs_vmlinux_btf(obj))
3436*f7c14bbaSAndroid Build Coastguard Worker return 0;
3437*f7c14bbaSAndroid Build Coastguard Worker
3438*f7c14bbaSAndroid Build Coastguard Worker obj->btf_vmlinux = btf__load_vmlinux_btf();
3439*f7c14bbaSAndroid Build Coastguard Worker err = libbpf_get_error(obj->btf_vmlinux);
3440*f7c14bbaSAndroid Build Coastguard Worker if (err) {
3441*f7c14bbaSAndroid Build Coastguard Worker pr_warn("Error loading vmlinux BTF: %d\n", err);
3442*f7c14bbaSAndroid Build Coastguard Worker obj->btf_vmlinux = NULL;
3443*f7c14bbaSAndroid Build Coastguard Worker return err;
3444*f7c14bbaSAndroid Build Coastguard Worker }
3445*f7c14bbaSAndroid Build Coastguard Worker return 0;
3446*f7c14bbaSAndroid Build Coastguard Worker }
3447*f7c14bbaSAndroid Build Coastguard Worker
bpf_object__sanitize_and_load_btf(struct bpf_object * obj)3448*f7c14bbaSAndroid Build Coastguard Worker static int bpf_object__sanitize_and_load_btf(struct bpf_object *obj)
3449*f7c14bbaSAndroid Build Coastguard Worker {
3450*f7c14bbaSAndroid Build Coastguard Worker struct btf *kern_btf = obj->btf;
3451*f7c14bbaSAndroid Build Coastguard Worker bool btf_mandatory, sanitize;
3452*f7c14bbaSAndroid Build Coastguard Worker int i, err = 0;
3453*f7c14bbaSAndroid Build Coastguard Worker
3454*f7c14bbaSAndroid Build Coastguard Worker if (!obj->btf)
3455*f7c14bbaSAndroid Build Coastguard Worker return 0;
3456*f7c14bbaSAndroid Build Coastguard Worker
3457*f7c14bbaSAndroid Build Coastguard Worker if (!kernel_supports(obj, FEAT_BTF)) {
3458*f7c14bbaSAndroid Build Coastguard Worker if (kernel_needs_btf(obj)) {
3459*f7c14bbaSAndroid Build Coastguard Worker err = -EOPNOTSUPP;
3460*f7c14bbaSAndroid Build Coastguard Worker goto report;
3461*f7c14bbaSAndroid Build Coastguard Worker }
3462*f7c14bbaSAndroid Build Coastguard Worker pr_debug("Kernel doesn't support BTF, skipping uploading it.\n");
3463*f7c14bbaSAndroid Build Coastguard Worker return 0;
3464*f7c14bbaSAndroid Build Coastguard Worker }
3465*f7c14bbaSAndroid Build Coastguard Worker
3466*f7c14bbaSAndroid Build Coastguard Worker /* Even though some subprogs are global/weak, user might prefer more
3467*f7c14bbaSAndroid Build Coastguard Worker * permissive BPF verification process that BPF verifier performs for
3468*f7c14bbaSAndroid Build Coastguard Worker * static functions, taking into account more context from the caller
3469*f7c14bbaSAndroid Build Coastguard Worker * functions. In such case, they need to mark such subprogs with
3470*f7c14bbaSAndroid Build Coastguard Worker * __attribute__((visibility("hidden"))) and libbpf will adjust
3471*f7c14bbaSAndroid Build Coastguard Worker * corresponding FUNC BTF type to be marked as static and trigger more
3472*f7c14bbaSAndroid Build Coastguard Worker * involved BPF verification process.
3473*f7c14bbaSAndroid Build Coastguard Worker */
3474*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < obj->nr_programs; i++) {
3475*f7c14bbaSAndroid Build Coastguard Worker struct bpf_program *prog = &obj->programs[i];
3476*f7c14bbaSAndroid Build Coastguard Worker struct btf_type *t;
3477*f7c14bbaSAndroid Build Coastguard Worker const char *name;
3478*f7c14bbaSAndroid Build Coastguard Worker int j, n;
3479*f7c14bbaSAndroid Build Coastguard Worker
3480*f7c14bbaSAndroid Build Coastguard Worker if (!prog->mark_btf_static || !prog_is_subprog(obj, prog))
3481*f7c14bbaSAndroid Build Coastguard Worker continue;
3482*f7c14bbaSAndroid Build Coastguard Worker
3483*f7c14bbaSAndroid Build Coastguard Worker n = btf__type_cnt(obj->btf);
3484*f7c14bbaSAndroid Build Coastguard Worker for (j = 1; j < n; j++) {
3485*f7c14bbaSAndroid Build Coastguard Worker t = btf_type_by_id(obj->btf, j);
3486*f7c14bbaSAndroid Build Coastguard Worker if (!btf_is_func(t) || btf_func_linkage(t) != BTF_FUNC_GLOBAL)
3487*f7c14bbaSAndroid Build Coastguard Worker continue;
3488*f7c14bbaSAndroid Build Coastguard Worker
3489*f7c14bbaSAndroid Build Coastguard Worker name = btf__str_by_offset(obj->btf, t->name_off);
3490*f7c14bbaSAndroid Build Coastguard Worker if (strcmp(name, prog->name) != 0)
3491*f7c14bbaSAndroid Build Coastguard Worker continue;
3492*f7c14bbaSAndroid Build Coastguard Worker
3493*f7c14bbaSAndroid Build Coastguard Worker t->info = btf_type_info(BTF_KIND_FUNC, BTF_FUNC_STATIC, 0);
3494*f7c14bbaSAndroid Build Coastguard Worker break;
3495*f7c14bbaSAndroid Build Coastguard Worker }
3496*f7c14bbaSAndroid Build Coastguard Worker }
3497*f7c14bbaSAndroid Build Coastguard Worker
3498*f7c14bbaSAndroid Build Coastguard Worker sanitize = btf_needs_sanitization(obj);
3499*f7c14bbaSAndroid Build Coastguard Worker if (sanitize) {
3500*f7c14bbaSAndroid Build Coastguard Worker const void *raw_data;
3501*f7c14bbaSAndroid Build Coastguard Worker __u32 sz;
3502*f7c14bbaSAndroid Build Coastguard Worker
3503*f7c14bbaSAndroid Build Coastguard Worker /* clone BTF to sanitize a copy and leave the original intact */
3504*f7c14bbaSAndroid Build Coastguard Worker raw_data = btf__raw_data(obj->btf, &sz);
3505*f7c14bbaSAndroid Build Coastguard Worker kern_btf = btf__new(raw_data, sz);
3506*f7c14bbaSAndroid Build Coastguard Worker err = libbpf_get_error(kern_btf);
3507*f7c14bbaSAndroid Build Coastguard Worker if (err)
3508*f7c14bbaSAndroid Build Coastguard Worker return err;
3509*f7c14bbaSAndroid Build Coastguard Worker
3510*f7c14bbaSAndroid Build Coastguard Worker /* enforce 8-byte pointers for BPF-targeted BTFs */
3511*f7c14bbaSAndroid Build Coastguard Worker btf__set_pointer_size(obj->btf, 8);
3512*f7c14bbaSAndroid Build Coastguard Worker err = bpf_object__sanitize_btf(obj, kern_btf);
3513*f7c14bbaSAndroid Build Coastguard Worker if (err)
3514*f7c14bbaSAndroid Build Coastguard Worker return err;
3515*f7c14bbaSAndroid Build Coastguard Worker }
3516*f7c14bbaSAndroid Build Coastguard Worker
3517*f7c14bbaSAndroid Build Coastguard Worker if (obj->gen_loader) {
3518*f7c14bbaSAndroid Build Coastguard Worker __u32 raw_size = 0;
3519*f7c14bbaSAndroid Build Coastguard Worker const void *raw_data = btf__raw_data(kern_btf, &raw_size);
3520*f7c14bbaSAndroid Build Coastguard Worker
3521*f7c14bbaSAndroid Build Coastguard Worker if (!raw_data)
3522*f7c14bbaSAndroid Build Coastguard Worker return -ENOMEM;
3523*f7c14bbaSAndroid Build Coastguard Worker bpf_gen__load_btf(obj->gen_loader, raw_data, raw_size);
3524*f7c14bbaSAndroid Build Coastguard Worker /* Pretend to have valid FD to pass various fd >= 0 checks.
3525*f7c14bbaSAndroid Build Coastguard Worker * This fd == 0 will not be used with any syscall and will be reset to -1 eventually.
3526*f7c14bbaSAndroid Build Coastguard Worker */
3527*f7c14bbaSAndroid Build Coastguard Worker btf__set_fd(kern_btf, 0);
3528*f7c14bbaSAndroid Build Coastguard Worker } else {
3529*f7c14bbaSAndroid Build Coastguard Worker /* currently BPF_BTF_LOAD only supports log_level 1 */
3530*f7c14bbaSAndroid Build Coastguard Worker err = btf_load_into_kernel(kern_btf, obj->log_buf, obj->log_size,
3531*f7c14bbaSAndroid Build Coastguard Worker obj->log_level ? 1 : 0, obj->token_fd);
3532*f7c14bbaSAndroid Build Coastguard Worker }
3533*f7c14bbaSAndroid Build Coastguard Worker if (sanitize) {
3534*f7c14bbaSAndroid Build Coastguard Worker if (!err) {
3535*f7c14bbaSAndroid Build Coastguard Worker /* move fd to libbpf's BTF */
3536*f7c14bbaSAndroid Build Coastguard Worker btf__set_fd(obj->btf, btf__fd(kern_btf));
3537*f7c14bbaSAndroid Build Coastguard Worker btf__set_fd(kern_btf, -1);
3538*f7c14bbaSAndroid Build Coastguard Worker }
3539*f7c14bbaSAndroid Build Coastguard Worker btf__free(kern_btf);
3540*f7c14bbaSAndroid Build Coastguard Worker }
3541*f7c14bbaSAndroid Build Coastguard Worker report:
3542*f7c14bbaSAndroid Build Coastguard Worker if (err) {
3543*f7c14bbaSAndroid Build Coastguard Worker btf_mandatory = kernel_needs_btf(obj);
3544*f7c14bbaSAndroid Build Coastguard Worker pr_warn("Error loading .BTF into kernel: %d. %s\n", err,
3545*f7c14bbaSAndroid Build Coastguard Worker btf_mandatory ? "BTF is mandatory, can't proceed."
3546*f7c14bbaSAndroid Build Coastguard Worker : "BTF is optional, ignoring.");
3547*f7c14bbaSAndroid Build Coastguard Worker if (!btf_mandatory)
3548*f7c14bbaSAndroid Build Coastguard Worker err = 0;
3549*f7c14bbaSAndroid Build Coastguard Worker }
3550*f7c14bbaSAndroid Build Coastguard Worker return err;
3551*f7c14bbaSAndroid Build Coastguard Worker }
3552*f7c14bbaSAndroid Build Coastguard Worker
elf_sym_str(const struct bpf_object * obj,size_t off)3553*f7c14bbaSAndroid Build Coastguard Worker static const char *elf_sym_str(const struct bpf_object *obj, size_t off)
3554*f7c14bbaSAndroid Build Coastguard Worker {
3555*f7c14bbaSAndroid Build Coastguard Worker const char *name;
3556*f7c14bbaSAndroid Build Coastguard Worker
3557*f7c14bbaSAndroid Build Coastguard Worker name = elf_strptr(obj->efile.elf, obj->efile.strtabidx, off);
3558*f7c14bbaSAndroid Build Coastguard Worker if (!name) {
3559*f7c14bbaSAndroid Build Coastguard Worker pr_warn("elf: failed to get section name string at offset %zu from %s: %s\n",
3560*f7c14bbaSAndroid Build Coastguard Worker off, obj->path, elf_errmsg(-1));
3561*f7c14bbaSAndroid Build Coastguard Worker return NULL;
3562*f7c14bbaSAndroid Build Coastguard Worker }
3563*f7c14bbaSAndroid Build Coastguard Worker
3564*f7c14bbaSAndroid Build Coastguard Worker return name;
3565*f7c14bbaSAndroid Build Coastguard Worker }
3566*f7c14bbaSAndroid Build Coastguard Worker
elf_sec_str(const struct bpf_object * obj,size_t off)3567*f7c14bbaSAndroid Build Coastguard Worker static const char *elf_sec_str(const struct bpf_object *obj, size_t off)
3568*f7c14bbaSAndroid Build Coastguard Worker {
3569*f7c14bbaSAndroid Build Coastguard Worker const char *name;
3570*f7c14bbaSAndroid Build Coastguard Worker
3571*f7c14bbaSAndroid Build Coastguard Worker name = elf_strptr(obj->efile.elf, obj->efile.shstrndx, off);
3572*f7c14bbaSAndroid Build Coastguard Worker if (!name) {
3573*f7c14bbaSAndroid Build Coastguard Worker pr_warn("elf: failed to get section name string at offset %zu from %s: %s\n",
3574*f7c14bbaSAndroid Build Coastguard Worker off, obj->path, elf_errmsg(-1));
3575*f7c14bbaSAndroid Build Coastguard Worker return NULL;
3576*f7c14bbaSAndroid Build Coastguard Worker }
3577*f7c14bbaSAndroid Build Coastguard Worker
3578*f7c14bbaSAndroid Build Coastguard Worker return name;
3579*f7c14bbaSAndroid Build Coastguard Worker }
3580*f7c14bbaSAndroid Build Coastguard Worker
elf_sec_by_idx(const struct bpf_object * obj,size_t idx)3581*f7c14bbaSAndroid Build Coastguard Worker static Elf_Scn *elf_sec_by_idx(const struct bpf_object *obj, size_t idx)
3582*f7c14bbaSAndroid Build Coastguard Worker {
3583*f7c14bbaSAndroid Build Coastguard Worker Elf_Scn *scn;
3584*f7c14bbaSAndroid Build Coastguard Worker
3585*f7c14bbaSAndroid Build Coastguard Worker scn = elf_getscn(obj->efile.elf, idx);
3586*f7c14bbaSAndroid Build Coastguard Worker if (!scn) {
3587*f7c14bbaSAndroid Build Coastguard Worker pr_warn("elf: failed to get section(%zu) from %s: %s\n",
3588*f7c14bbaSAndroid Build Coastguard Worker idx, obj->path, elf_errmsg(-1));
3589*f7c14bbaSAndroid Build Coastguard Worker return NULL;
3590*f7c14bbaSAndroid Build Coastguard Worker }
3591*f7c14bbaSAndroid Build Coastguard Worker return scn;
3592*f7c14bbaSAndroid Build Coastguard Worker }
3593*f7c14bbaSAndroid Build Coastguard Worker
elf_sec_by_name(const struct bpf_object * obj,const char * name)3594*f7c14bbaSAndroid Build Coastguard Worker static Elf_Scn *elf_sec_by_name(const struct bpf_object *obj, const char *name)
3595*f7c14bbaSAndroid Build Coastguard Worker {
3596*f7c14bbaSAndroid Build Coastguard Worker Elf_Scn *scn = NULL;
3597*f7c14bbaSAndroid Build Coastguard Worker Elf *elf = obj->efile.elf;
3598*f7c14bbaSAndroid Build Coastguard Worker const char *sec_name;
3599*f7c14bbaSAndroid Build Coastguard Worker
3600*f7c14bbaSAndroid Build Coastguard Worker while ((scn = elf_nextscn(elf, scn)) != NULL) {
3601*f7c14bbaSAndroid Build Coastguard Worker sec_name = elf_sec_name(obj, scn);
3602*f7c14bbaSAndroid Build Coastguard Worker if (!sec_name)
3603*f7c14bbaSAndroid Build Coastguard Worker return NULL;
3604*f7c14bbaSAndroid Build Coastguard Worker
3605*f7c14bbaSAndroid Build Coastguard Worker if (strcmp(sec_name, name) != 0)
3606*f7c14bbaSAndroid Build Coastguard Worker continue;
3607*f7c14bbaSAndroid Build Coastguard Worker
3608*f7c14bbaSAndroid Build Coastguard Worker return scn;
3609*f7c14bbaSAndroid Build Coastguard Worker }
3610*f7c14bbaSAndroid Build Coastguard Worker return NULL;
3611*f7c14bbaSAndroid Build Coastguard Worker }
3612*f7c14bbaSAndroid Build Coastguard Worker
elf_sec_hdr(const struct bpf_object * obj,Elf_Scn * scn)3613*f7c14bbaSAndroid Build Coastguard Worker static Elf64_Shdr *elf_sec_hdr(const struct bpf_object *obj, Elf_Scn *scn)
3614*f7c14bbaSAndroid Build Coastguard Worker {
3615*f7c14bbaSAndroid Build Coastguard Worker Elf64_Shdr *shdr;
3616*f7c14bbaSAndroid Build Coastguard Worker
3617*f7c14bbaSAndroid Build Coastguard Worker if (!scn)
3618*f7c14bbaSAndroid Build Coastguard Worker return NULL;
3619*f7c14bbaSAndroid Build Coastguard Worker
3620*f7c14bbaSAndroid Build Coastguard Worker shdr = elf64_getshdr(scn);
3621*f7c14bbaSAndroid Build Coastguard Worker if (!shdr) {
3622*f7c14bbaSAndroid Build Coastguard Worker pr_warn("elf: failed to get section(%zu) header from %s: %s\n",
3623*f7c14bbaSAndroid Build Coastguard Worker elf_ndxscn(scn), obj->path, elf_errmsg(-1));
3624*f7c14bbaSAndroid Build Coastguard Worker return NULL;
3625*f7c14bbaSAndroid Build Coastguard Worker }
3626*f7c14bbaSAndroid Build Coastguard Worker
3627*f7c14bbaSAndroid Build Coastguard Worker return shdr;
3628*f7c14bbaSAndroid Build Coastguard Worker }
3629*f7c14bbaSAndroid Build Coastguard Worker
elf_sec_name(const struct bpf_object * obj,Elf_Scn * scn)3630*f7c14bbaSAndroid Build Coastguard Worker static const char *elf_sec_name(const struct bpf_object *obj, Elf_Scn *scn)
3631*f7c14bbaSAndroid Build Coastguard Worker {
3632*f7c14bbaSAndroid Build Coastguard Worker const char *name;
3633*f7c14bbaSAndroid Build Coastguard Worker Elf64_Shdr *sh;
3634*f7c14bbaSAndroid Build Coastguard Worker
3635*f7c14bbaSAndroid Build Coastguard Worker if (!scn)
3636*f7c14bbaSAndroid Build Coastguard Worker return NULL;
3637*f7c14bbaSAndroid Build Coastguard Worker
3638*f7c14bbaSAndroid Build Coastguard Worker sh = elf_sec_hdr(obj, scn);
3639*f7c14bbaSAndroid Build Coastguard Worker if (!sh)
3640*f7c14bbaSAndroid Build Coastguard Worker return NULL;
3641*f7c14bbaSAndroid Build Coastguard Worker
3642*f7c14bbaSAndroid Build Coastguard Worker name = elf_sec_str(obj, sh->sh_name);
3643*f7c14bbaSAndroid Build Coastguard Worker if (!name) {
3644*f7c14bbaSAndroid Build Coastguard Worker pr_warn("elf: failed to get section(%zu) name from %s: %s\n",
3645*f7c14bbaSAndroid Build Coastguard Worker elf_ndxscn(scn), obj->path, elf_errmsg(-1));
3646*f7c14bbaSAndroid Build Coastguard Worker return NULL;
3647*f7c14bbaSAndroid Build Coastguard Worker }
3648*f7c14bbaSAndroid Build Coastguard Worker
3649*f7c14bbaSAndroid Build Coastguard Worker return name;
3650*f7c14bbaSAndroid Build Coastguard Worker }
3651*f7c14bbaSAndroid Build Coastguard Worker
elf_sec_data(const struct bpf_object * obj,Elf_Scn * scn)3652*f7c14bbaSAndroid Build Coastguard Worker static Elf_Data *elf_sec_data(const struct bpf_object *obj, Elf_Scn *scn)
3653*f7c14bbaSAndroid Build Coastguard Worker {
3654*f7c14bbaSAndroid Build Coastguard Worker Elf_Data *data;
3655*f7c14bbaSAndroid Build Coastguard Worker
3656*f7c14bbaSAndroid Build Coastguard Worker if (!scn)
3657*f7c14bbaSAndroid Build Coastguard Worker return NULL;
3658*f7c14bbaSAndroid Build Coastguard Worker
3659*f7c14bbaSAndroid Build Coastguard Worker data = elf_getdata(scn, 0);
3660*f7c14bbaSAndroid Build Coastguard Worker if (!data) {
3661*f7c14bbaSAndroid Build Coastguard Worker pr_warn("elf: failed to get section(%zu) %s data from %s: %s\n",
3662*f7c14bbaSAndroid Build Coastguard Worker elf_ndxscn(scn), elf_sec_name(obj, scn) ?: "<?>",
3663*f7c14bbaSAndroid Build Coastguard Worker obj->path, elf_errmsg(-1));
3664*f7c14bbaSAndroid Build Coastguard Worker return NULL;
3665*f7c14bbaSAndroid Build Coastguard Worker }
3666*f7c14bbaSAndroid Build Coastguard Worker
3667*f7c14bbaSAndroid Build Coastguard Worker return data;
3668*f7c14bbaSAndroid Build Coastguard Worker }
3669*f7c14bbaSAndroid Build Coastguard Worker
elf_sym_by_idx(const struct bpf_object * obj,size_t idx)3670*f7c14bbaSAndroid Build Coastguard Worker static Elf64_Sym *elf_sym_by_idx(const struct bpf_object *obj, size_t idx)
3671*f7c14bbaSAndroid Build Coastguard Worker {
3672*f7c14bbaSAndroid Build Coastguard Worker if (idx >= obj->efile.symbols->d_size / sizeof(Elf64_Sym))
3673*f7c14bbaSAndroid Build Coastguard Worker return NULL;
3674*f7c14bbaSAndroid Build Coastguard Worker
3675*f7c14bbaSAndroid Build Coastguard Worker return (Elf64_Sym *)obj->efile.symbols->d_buf + idx;
3676*f7c14bbaSAndroid Build Coastguard Worker }
3677*f7c14bbaSAndroid Build Coastguard Worker
elf_rel_by_idx(Elf_Data * data,size_t idx)3678*f7c14bbaSAndroid Build Coastguard Worker static Elf64_Rel *elf_rel_by_idx(Elf_Data *data, size_t idx)
3679*f7c14bbaSAndroid Build Coastguard Worker {
3680*f7c14bbaSAndroid Build Coastguard Worker if (idx >= data->d_size / sizeof(Elf64_Rel))
3681*f7c14bbaSAndroid Build Coastguard Worker return NULL;
3682*f7c14bbaSAndroid Build Coastguard Worker
3683*f7c14bbaSAndroid Build Coastguard Worker return (Elf64_Rel *)data->d_buf + idx;
3684*f7c14bbaSAndroid Build Coastguard Worker }
3685*f7c14bbaSAndroid Build Coastguard Worker
is_sec_name_dwarf(const char * name)3686*f7c14bbaSAndroid Build Coastguard Worker static bool is_sec_name_dwarf(const char *name)
3687*f7c14bbaSAndroid Build Coastguard Worker {
3688*f7c14bbaSAndroid Build Coastguard Worker /* approximation, but the actual list is too long */
3689*f7c14bbaSAndroid Build Coastguard Worker return str_has_pfx(name, ".debug_");
3690*f7c14bbaSAndroid Build Coastguard Worker }
3691*f7c14bbaSAndroid Build Coastguard Worker
ignore_elf_section(Elf64_Shdr * hdr,const char * name)3692*f7c14bbaSAndroid Build Coastguard Worker static bool ignore_elf_section(Elf64_Shdr *hdr, const char *name)
3693*f7c14bbaSAndroid Build Coastguard Worker {
3694*f7c14bbaSAndroid Build Coastguard Worker /* no special handling of .strtab */
3695*f7c14bbaSAndroid Build Coastguard Worker if (hdr->sh_type == SHT_STRTAB)
3696*f7c14bbaSAndroid Build Coastguard Worker return true;
3697*f7c14bbaSAndroid Build Coastguard Worker
3698*f7c14bbaSAndroid Build Coastguard Worker /* ignore .llvm_addrsig section as well */
3699*f7c14bbaSAndroid Build Coastguard Worker if (hdr->sh_type == SHT_LLVM_ADDRSIG)
3700*f7c14bbaSAndroid Build Coastguard Worker return true;
3701*f7c14bbaSAndroid Build Coastguard Worker
3702*f7c14bbaSAndroid Build Coastguard Worker /* no subprograms will lead to an empty .text section, ignore it */
3703*f7c14bbaSAndroid Build Coastguard Worker if (hdr->sh_type == SHT_PROGBITS && hdr->sh_size == 0 &&
3704*f7c14bbaSAndroid Build Coastguard Worker strcmp(name, ".text") == 0)
3705*f7c14bbaSAndroid Build Coastguard Worker return true;
3706*f7c14bbaSAndroid Build Coastguard Worker
3707*f7c14bbaSAndroid Build Coastguard Worker /* DWARF sections */
3708*f7c14bbaSAndroid Build Coastguard Worker if (is_sec_name_dwarf(name))
3709*f7c14bbaSAndroid Build Coastguard Worker return true;
3710*f7c14bbaSAndroid Build Coastguard Worker
3711*f7c14bbaSAndroid Build Coastguard Worker if (str_has_pfx(name, ".rel")) {
3712*f7c14bbaSAndroid Build Coastguard Worker name += sizeof(".rel") - 1;
3713*f7c14bbaSAndroid Build Coastguard Worker /* DWARF section relocations */
3714*f7c14bbaSAndroid Build Coastguard Worker if (is_sec_name_dwarf(name))
3715*f7c14bbaSAndroid Build Coastguard Worker return true;
3716*f7c14bbaSAndroid Build Coastguard Worker
3717*f7c14bbaSAndroid Build Coastguard Worker /* .BTF and .BTF.ext don't need relocations */
3718*f7c14bbaSAndroid Build Coastguard Worker if (strcmp(name, BTF_ELF_SEC) == 0 ||
3719*f7c14bbaSAndroid Build Coastguard Worker strcmp(name, BTF_EXT_ELF_SEC) == 0)
3720*f7c14bbaSAndroid Build Coastguard Worker return true;
3721*f7c14bbaSAndroid Build Coastguard Worker }
3722*f7c14bbaSAndroid Build Coastguard Worker
3723*f7c14bbaSAndroid Build Coastguard Worker return false;
3724*f7c14bbaSAndroid Build Coastguard Worker }
3725*f7c14bbaSAndroid Build Coastguard Worker
cmp_progs(const void * _a,const void * _b)3726*f7c14bbaSAndroid Build Coastguard Worker static int cmp_progs(const void *_a, const void *_b)
3727*f7c14bbaSAndroid Build Coastguard Worker {
3728*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_program *a = _a;
3729*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_program *b = _b;
3730*f7c14bbaSAndroid Build Coastguard Worker
3731*f7c14bbaSAndroid Build Coastguard Worker if (a->sec_idx != b->sec_idx)
3732*f7c14bbaSAndroid Build Coastguard Worker return a->sec_idx < b->sec_idx ? -1 : 1;
3733*f7c14bbaSAndroid Build Coastguard Worker
3734*f7c14bbaSAndroid Build Coastguard Worker /* sec_insn_off can't be the same within the section */
3735*f7c14bbaSAndroid Build Coastguard Worker return a->sec_insn_off < b->sec_insn_off ? -1 : 1;
3736*f7c14bbaSAndroid Build Coastguard Worker }
3737*f7c14bbaSAndroid Build Coastguard Worker
bpf_object__elf_collect(struct bpf_object * obj)3738*f7c14bbaSAndroid Build Coastguard Worker static int bpf_object__elf_collect(struct bpf_object *obj)
3739*f7c14bbaSAndroid Build Coastguard Worker {
3740*f7c14bbaSAndroid Build Coastguard Worker struct elf_sec_desc *sec_desc;
3741*f7c14bbaSAndroid Build Coastguard Worker Elf *elf = obj->efile.elf;
3742*f7c14bbaSAndroid Build Coastguard Worker Elf_Data *btf_ext_data = NULL;
3743*f7c14bbaSAndroid Build Coastguard Worker Elf_Data *btf_data = NULL;
3744*f7c14bbaSAndroid Build Coastguard Worker int idx = 0, err = 0;
3745*f7c14bbaSAndroid Build Coastguard Worker const char *name;
3746*f7c14bbaSAndroid Build Coastguard Worker Elf_Data *data;
3747*f7c14bbaSAndroid Build Coastguard Worker Elf_Scn *scn;
3748*f7c14bbaSAndroid Build Coastguard Worker Elf64_Shdr *sh;
3749*f7c14bbaSAndroid Build Coastguard Worker
3750*f7c14bbaSAndroid Build Coastguard Worker /* ELF section indices are 0-based, but sec #0 is special "invalid"
3751*f7c14bbaSAndroid Build Coastguard Worker * section. Since section count retrieved by elf_getshdrnum() does
3752*f7c14bbaSAndroid Build Coastguard Worker * include sec #0, it is already the necessary size of an array to keep
3753*f7c14bbaSAndroid Build Coastguard Worker * all the sections.
3754*f7c14bbaSAndroid Build Coastguard Worker */
3755*f7c14bbaSAndroid Build Coastguard Worker if (elf_getshdrnum(obj->efile.elf, &obj->efile.sec_cnt)) {
3756*f7c14bbaSAndroid Build Coastguard Worker pr_warn("elf: failed to get the number of sections for %s: %s\n",
3757*f7c14bbaSAndroid Build Coastguard Worker obj->path, elf_errmsg(-1));
3758*f7c14bbaSAndroid Build Coastguard Worker return -LIBBPF_ERRNO__FORMAT;
3759*f7c14bbaSAndroid Build Coastguard Worker }
3760*f7c14bbaSAndroid Build Coastguard Worker obj->efile.secs = calloc(obj->efile.sec_cnt, sizeof(*obj->efile.secs));
3761*f7c14bbaSAndroid Build Coastguard Worker if (!obj->efile.secs)
3762*f7c14bbaSAndroid Build Coastguard Worker return -ENOMEM;
3763*f7c14bbaSAndroid Build Coastguard Worker
3764*f7c14bbaSAndroid Build Coastguard Worker /* a bunch of ELF parsing functionality depends on processing symbols,
3765*f7c14bbaSAndroid Build Coastguard Worker * so do the first pass and find the symbol table
3766*f7c14bbaSAndroid Build Coastguard Worker */
3767*f7c14bbaSAndroid Build Coastguard Worker scn = NULL;
3768*f7c14bbaSAndroid Build Coastguard Worker while ((scn = elf_nextscn(elf, scn)) != NULL) {
3769*f7c14bbaSAndroid Build Coastguard Worker sh = elf_sec_hdr(obj, scn);
3770*f7c14bbaSAndroid Build Coastguard Worker if (!sh)
3771*f7c14bbaSAndroid Build Coastguard Worker return -LIBBPF_ERRNO__FORMAT;
3772*f7c14bbaSAndroid Build Coastguard Worker
3773*f7c14bbaSAndroid Build Coastguard Worker if (sh->sh_type == SHT_SYMTAB) {
3774*f7c14bbaSAndroid Build Coastguard Worker if (obj->efile.symbols) {
3775*f7c14bbaSAndroid Build Coastguard Worker pr_warn("elf: multiple symbol tables in %s\n", obj->path);
3776*f7c14bbaSAndroid Build Coastguard Worker return -LIBBPF_ERRNO__FORMAT;
3777*f7c14bbaSAndroid Build Coastguard Worker }
3778*f7c14bbaSAndroid Build Coastguard Worker
3779*f7c14bbaSAndroid Build Coastguard Worker data = elf_sec_data(obj, scn);
3780*f7c14bbaSAndroid Build Coastguard Worker if (!data)
3781*f7c14bbaSAndroid Build Coastguard Worker return -LIBBPF_ERRNO__FORMAT;
3782*f7c14bbaSAndroid Build Coastguard Worker
3783*f7c14bbaSAndroid Build Coastguard Worker idx = elf_ndxscn(scn);
3784*f7c14bbaSAndroid Build Coastguard Worker
3785*f7c14bbaSAndroid Build Coastguard Worker obj->efile.symbols = data;
3786*f7c14bbaSAndroid Build Coastguard Worker obj->efile.symbols_shndx = idx;
3787*f7c14bbaSAndroid Build Coastguard Worker obj->efile.strtabidx = sh->sh_link;
3788*f7c14bbaSAndroid Build Coastguard Worker }
3789*f7c14bbaSAndroid Build Coastguard Worker }
3790*f7c14bbaSAndroid Build Coastguard Worker
3791*f7c14bbaSAndroid Build Coastguard Worker if (!obj->efile.symbols) {
3792*f7c14bbaSAndroid Build Coastguard Worker pr_warn("elf: couldn't find symbol table in %s, stripped object file?\n",
3793*f7c14bbaSAndroid Build Coastguard Worker obj->path);
3794*f7c14bbaSAndroid Build Coastguard Worker return -ENOENT;
3795*f7c14bbaSAndroid Build Coastguard Worker }
3796*f7c14bbaSAndroid Build Coastguard Worker
3797*f7c14bbaSAndroid Build Coastguard Worker scn = NULL;
3798*f7c14bbaSAndroid Build Coastguard Worker while ((scn = elf_nextscn(elf, scn)) != NULL) {
3799*f7c14bbaSAndroid Build Coastguard Worker idx = elf_ndxscn(scn);
3800*f7c14bbaSAndroid Build Coastguard Worker sec_desc = &obj->efile.secs[idx];
3801*f7c14bbaSAndroid Build Coastguard Worker
3802*f7c14bbaSAndroid Build Coastguard Worker sh = elf_sec_hdr(obj, scn);
3803*f7c14bbaSAndroid Build Coastguard Worker if (!sh)
3804*f7c14bbaSAndroid Build Coastguard Worker return -LIBBPF_ERRNO__FORMAT;
3805*f7c14bbaSAndroid Build Coastguard Worker
3806*f7c14bbaSAndroid Build Coastguard Worker name = elf_sec_str(obj, sh->sh_name);
3807*f7c14bbaSAndroid Build Coastguard Worker if (!name)
3808*f7c14bbaSAndroid Build Coastguard Worker return -LIBBPF_ERRNO__FORMAT;
3809*f7c14bbaSAndroid Build Coastguard Worker
3810*f7c14bbaSAndroid Build Coastguard Worker if (ignore_elf_section(sh, name))
3811*f7c14bbaSAndroid Build Coastguard Worker continue;
3812*f7c14bbaSAndroid Build Coastguard Worker
3813*f7c14bbaSAndroid Build Coastguard Worker data = elf_sec_data(obj, scn);
3814*f7c14bbaSAndroid Build Coastguard Worker if (!data)
3815*f7c14bbaSAndroid Build Coastguard Worker return -LIBBPF_ERRNO__FORMAT;
3816*f7c14bbaSAndroid Build Coastguard Worker
3817*f7c14bbaSAndroid Build Coastguard Worker pr_debug("elf: section(%d) %s, size %ld, link %d, flags %lx, type=%d\n",
3818*f7c14bbaSAndroid Build Coastguard Worker idx, name, (unsigned long)data->d_size,
3819*f7c14bbaSAndroid Build Coastguard Worker (int)sh->sh_link, (unsigned long)sh->sh_flags,
3820*f7c14bbaSAndroid Build Coastguard Worker (int)sh->sh_type);
3821*f7c14bbaSAndroid Build Coastguard Worker
3822*f7c14bbaSAndroid Build Coastguard Worker if (strcmp(name, "license") == 0) {
3823*f7c14bbaSAndroid Build Coastguard Worker err = bpf_object__init_license(obj, data->d_buf, data->d_size);
3824*f7c14bbaSAndroid Build Coastguard Worker if (err)
3825*f7c14bbaSAndroid Build Coastguard Worker return err;
3826*f7c14bbaSAndroid Build Coastguard Worker } else if (strcmp(name, "version") == 0) {
3827*f7c14bbaSAndroid Build Coastguard Worker err = bpf_object__init_kversion(obj, data->d_buf, data->d_size);
3828*f7c14bbaSAndroid Build Coastguard Worker if (err)
3829*f7c14bbaSAndroid Build Coastguard Worker return err;
3830*f7c14bbaSAndroid Build Coastguard Worker } else if (strcmp(name, "maps") == 0) {
3831*f7c14bbaSAndroid Build Coastguard Worker pr_warn("elf: legacy map definitions in 'maps' section are not supported by libbpf v1.0+\n");
3832*f7c14bbaSAndroid Build Coastguard Worker return -ENOTSUP;
3833*f7c14bbaSAndroid Build Coastguard Worker } else if (strcmp(name, MAPS_ELF_SEC) == 0) {
3834*f7c14bbaSAndroid Build Coastguard Worker obj->efile.btf_maps_shndx = idx;
3835*f7c14bbaSAndroid Build Coastguard Worker } else if (strcmp(name, BTF_ELF_SEC) == 0) {
3836*f7c14bbaSAndroid Build Coastguard Worker if (sh->sh_type != SHT_PROGBITS)
3837*f7c14bbaSAndroid Build Coastguard Worker return -LIBBPF_ERRNO__FORMAT;
3838*f7c14bbaSAndroid Build Coastguard Worker btf_data = data;
3839*f7c14bbaSAndroid Build Coastguard Worker } else if (strcmp(name, BTF_EXT_ELF_SEC) == 0) {
3840*f7c14bbaSAndroid Build Coastguard Worker if (sh->sh_type != SHT_PROGBITS)
3841*f7c14bbaSAndroid Build Coastguard Worker return -LIBBPF_ERRNO__FORMAT;
3842*f7c14bbaSAndroid Build Coastguard Worker btf_ext_data = data;
3843*f7c14bbaSAndroid Build Coastguard Worker } else if (sh->sh_type == SHT_SYMTAB) {
3844*f7c14bbaSAndroid Build Coastguard Worker /* already processed during the first pass above */
3845*f7c14bbaSAndroid Build Coastguard Worker } else if (sh->sh_type == SHT_PROGBITS && data->d_size > 0) {
3846*f7c14bbaSAndroid Build Coastguard Worker if (sh->sh_flags & SHF_EXECINSTR) {
3847*f7c14bbaSAndroid Build Coastguard Worker if (strcmp(name, ".text") == 0)
3848*f7c14bbaSAndroid Build Coastguard Worker obj->efile.text_shndx = idx;
3849*f7c14bbaSAndroid Build Coastguard Worker err = bpf_object__add_programs(obj, data, name, idx);
3850*f7c14bbaSAndroid Build Coastguard Worker if (err)
3851*f7c14bbaSAndroid Build Coastguard Worker return err;
3852*f7c14bbaSAndroid Build Coastguard Worker } else if (strcmp(name, DATA_SEC) == 0 ||
3853*f7c14bbaSAndroid Build Coastguard Worker str_has_pfx(name, DATA_SEC ".")) {
3854*f7c14bbaSAndroid Build Coastguard Worker sec_desc->sec_type = SEC_DATA;
3855*f7c14bbaSAndroid Build Coastguard Worker sec_desc->shdr = sh;
3856*f7c14bbaSAndroid Build Coastguard Worker sec_desc->data = data;
3857*f7c14bbaSAndroid Build Coastguard Worker } else if (strcmp(name, RODATA_SEC) == 0 ||
3858*f7c14bbaSAndroid Build Coastguard Worker str_has_pfx(name, RODATA_SEC ".")) {
3859*f7c14bbaSAndroid Build Coastguard Worker sec_desc->sec_type = SEC_RODATA;
3860*f7c14bbaSAndroid Build Coastguard Worker sec_desc->shdr = sh;
3861*f7c14bbaSAndroid Build Coastguard Worker sec_desc->data = data;
3862*f7c14bbaSAndroid Build Coastguard Worker } else if (strcmp(name, STRUCT_OPS_SEC) == 0 ||
3863*f7c14bbaSAndroid Build Coastguard Worker strcmp(name, STRUCT_OPS_LINK_SEC) == 0 ||
3864*f7c14bbaSAndroid Build Coastguard Worker strcmp(name, "?" STRUCT_OPS_SEC) == 0 ||
3865*f7c14bbaSAndroid Build Coastguard Worker strcmp(name, "?" STRUCT_OPS_LINK_SEC) == 0) {
3866*f7c14bbaSAndroid Build Coastguard Worker sec_desc->sec_type = SEC_ST_OPS;
3867*f7c14bbaSAndroid Build Coastguard Worker sec_desc->shdr = sh;
3868*f7c14bbaSAndroid Build Coastguard Worker sec_desc->data = data;
3869*f7c14bbaSAndroid Build Coastguard Worker obj->efile.has_st_ops = true;
3870*f7c14bbaSAndroid Build Coastguard Worker } else if (strcmp(name, ARENA_SEC) == 0) {
3871*f7c14bbaSAndroid Build Coastguard Worker obj->efile.arena_data = data;
3872*f7c14bbaSAndroid Build Coastguard Worker obj->efile.arena_data_shndx = idx;
3873*f7c14bbaSAndroid Build Coastguard Worker } else {
3874*f7c14bbaSAndroid Build Coastguard Worker pr_info("elf: skipping unrecognized data section(%d) %s\n",
3875*f7c14bbaSAndroid Build Coastguard Worker idx, name);
3876*f7c14bbaSAndroid Build Coastguard Worker }
3877*f7c14bbaSAndroid Build Coastguard Worker } else if (sh->sh_type == SHT_REL) {
3878*f7c14bbaSAndroid Build Coastguard Worker int targ_sec_idx = sh->sh_info; /* points to other section */
3879*f7c14bbaSAndroid Build Coastguard Worker
3880*f7c14bbaSAndroid Build Coastguard Worker if (sh->sh_entsize != sizeof(Elf64_Rel) ||
3881*f7c14bbaSAndroid Build Coastguard Worker targ_sec_idx >= obj->efile.sec_cnt)
3882*f7c14bbaSAndroid Build Coastguard Worker return -LIBBPF_ERRNO__FORMAT;
3883*f7c14bbaSAndroid Build Coastguard Worker
3884*f7c14bbaSAndroid Build Coastguard Worker /* Only do relo for section with exec instructions */
3885*f7c14bbaSAndroid Build Coastguard Worker if (!section_have_execinstr(obj, targ_sec_idx) &&
3886*f7c14bbaSAndroid Build Coastguard Worker strcmp(name, ".rel" STRUCT_OPS_SEC) &&
3887*f7c14bbaSAndroid Build Coastguard Worker strcmp(name, ".rel" STRUCT_OPS_LINK_SEC) &&
3888*f7c14bbaSAndroid Build Coastguard Worker strcmp(name, ".rel?" STRUCT_OPS_SEC) &&
3889*f7c14bbaSAndroid Build Coastguard Worker strcmp(name, ".rel?" STRUCT_OPS_LINK_SEC) &&
3890*f7c14bbaSAndroid Build Coastguard Worker strcmp(name, ".rel" MAPS_ELF_SEC)) {
3891*f7c14bbaSAndroid Build Coastguard Worker pr_info("elf: skipping relo section(%d) %s for section(%d) %s\n",
3892*f7c14bbaSAndroid Build Coastguard Worker idx, name, targ_sec_idx,
3893*f7c14bbaSAndroid Build Coastguard Worker elf_sec_name(obj, elf_sec_by_idx(obj, targ_sec_idx)) ?: "<?>");
3894*f7c14bbaSAndroid Build Coastguard Worker continue;
3895*f7c14bbaSAndroid Build Coastguard Worker }
3896*f7c14bbaSAndroid Build Coastguard Worker
3897*f7c14bbaSAndroid Build Coastguard Worker sec_desc->sec_type = SEC_RELO;
3898*f7c14bbaSAndroid Build Coastguard Worker sec_desc->shdr = sh;
3899*f7c14bbaSAndroid Build Coastguard Worker sec_desc->data = data;
3900*f7c14bbaSAndroid Build Coastguard Worker } else if (sh->sh_type == SHT_NOBITS && (strcmp(name, BSS_SEC) == 0 ||
3901*f7c14bbaSAndroid Build Coastguard Worker str_has_pfx(name, BSS_SEC "."))) {
3902*f7c14bbaSAndroid Build Coastguard Worker sec_desc->sec_type = SEC_BSS;
3903*f7c14bbaSAndroid Build Coastguard Worker sec_desc->shdr = sh;
3904*f7c14bbaSAndroid Build Coastguard Worker sec_desc->data = data;
3905*f7c14bbaSAndroid Build Coastguard Worker } else {
3906*f7c14bbaSAndroid Build Coastguard Worker pr_info("elf: skipping section(%d) %s (size %zu)\n", idx, name,
3907*f7c14bbaSAndroid Build Coastguard Worker (size_t)sh->sh_size);
3908*f7c14bbaSAndroid Build Coastguard Worker }
3909*f7c14bbaSAndroid Build Coastguard Worker }
3910*f7c14bbaSAndroid Build Coastguard Worker
3911*f7c14bbaSAndroid Build Coastguard Worker if (!obj->efile.strtabidx || obj->efile.strtabidx > idx) {
3912*f7c14bbaSAndroid Build Coastguard Worker pr_warn("elf: symbol strings section missing or invalid in %s\n", obj->path);
3913*f7c14bbaSAndroid Build Coastguard Worker return -LIBBPF_ERRNO__FORMAT;
3914*f7c14bbaSAndroid Build Coastguard Worker }
3915*f7c14bbaSAndroid Build Coastguard Worker
3916*f7c14bbaSAndroid Build Coastguard Worker /* sort BPF programs by section name and in-section instruction offset
3917*f7c14bbaSAndroid Build Coastguard Worker * for faster search
3918*f7c14bbaSAndroid Build Coastguard Worker */
3919*f7c14bbaSAndroid Build Coastguard Worker if (obj->nr_programs)
3920*f7c14bbaSAndroid Build Coastguard Worker qsort(obj->programs, obj->nr_programs, sizeof(*obj->programs), cmp_progs);
3921*f7c14bbaSAndroid Build Coastguard Worker
3922*f7c14bbaSAndroid Build Coastguard Worker return bpf_object__init_btf(obj, btf_data, btf_ext_data);
3923*f7c14bbaSAndroid Build Coastguard Worker }
3924*f7c14bbaSAndroid Build Coastguard Worker
sym_is_extern(const Elf64_Sym * sym)3925*f7c14bbaSAndroid Build Coastguard Worker static bool sym_is_extern(const Elf64_Sym *sym)
3926*f7c14bbaSAndroid Build Coastguard Worker {
3927*f7c14bbaSAndroid Build Coastguard Worker int bind = ELF64_ST_BIND(sym->st_info);
3928*f7c14bbaSAndroid Build Coastguard Worker /* externs are symbols w/ type=NOTYPE, bind=GLOBAL|WEAK, section=UND */
3929*f7c14bbaSAndroid Build Coastguard Worker return sym->st_shndx == SHN_UNDEF &&
3930*f7c14bbaSAndroid Build Coastguard Worker (bind == STB_GLOBAL || bind == STB_WEAK) &&
3931*f7c14bbaSAndroid Build Coastguard Worker ELF64_ST_TYPE(sym->st_info) == STT_NOTYPE;
3932*f7c14bbaSAndroid Build Coastguard Worker }
3933*f7c14bbaSAndroid Build Coastguard Worker
sym_is_subprog(const Elf64_Sym * sym,int text_shndx)3934*f7c14bbaSAndroid Build Coastguard Worker static bool sym_is_subprog(const Elf64_Sym *sym, int text_shndx)
3935*f7c14bbaSAndroid Build Coastguard Worker {
3936*f7c14bbaSAndroid Build Coastguard Worker int bind = ELF64_ST_BIND(sym->st_info);
3937*f7c14bbaSAndroid Build Coastguard Worker int type = ELF64_ST_TYPE(sym->st_info);
3938*f7c14bbaSAndroid Build Coastguard Worker
3939*f7c14bbaSAndroid Build Coastguard Worker /* in .text section */
3940*f7c14bbaSAndroid Build Coastguard Worker if (sym->st_shndx != text_shndx)
3941*f7c14bbaSAndroid Build Coastguard Worker return false;
3942*f7c14bbaSAndroid Build Coastguard Worker
3943*f7c14bbaSAndroid Build Coastguard Worker /* local function */
3944*f7c14bbaSAndroid Build Coastguard Worker if (bind == STB_LOCAL && type == STT_SECTION)
3945*f7c14bbaSAndroid Build Coastguard Worker return true;
3946*f7c14bbaSAndroid Build Coastguard Worker
3947*f7c14bbaSAndroid Build Coastguard Worker /* global function */
3948*f7c14bbaSAndroid Build Coastguard Worker return bind == STB_GLOBAL && type == STT_FUNC;
3949*f7c14bbaSAndroid Build Coastguard Worker }
3950*f7c14bbaSAndroid Build Coastguard Worker
find_extern_btf_id(const struct btf * btf,const char * ext_name)3951*f7c14bbaSAndroid Build Coastguard Worker static int find_extern_btf_id(const struct btf *btf, const char *ext_name)
3952*f7c14bbaSAndroid Build Coastguard Worker {
3953*f7c14bbaSAndroid Build Coastguard Worker const struct btf_type *t;
3954*f7c14bbaSAndroid Build Coastguard Worker const char *tname;
3955*f7c14bbaSAndroid Build Coastguard Worker int i, n;
3956*f7c14bbaSAndroid Build Coastguard Worker
3957*f7c14bbaSAndroid Build Coastguard Worker if (!btf)
3958*f7c14bbaSAndroid Build Coastguard Worker return -ESRCH;
3959*f7c14bbaSAndroid Build Coastguard Worker
3960*f7c14bbaSAndroid Build Coastguard Worker n = btf__type_cnt(btf);
3961*f7c14bbaSAndroid Build Coastguard Worker for (i = 1; i < n; i++) {
3962*f7c14bbaSAndroid Build Coastguard Worker t = btf__type_by_id(btf, i);
3963*f7c14bbaSAndroid Build Coastguard Worker
3964*f7c14bbaSAndroid Build Coastguard Worker if (!btf_is_var(t) && !btf_is_func(t))
3965*f7c14bbaSAndroid Build Coastguard Worker continue;
3966*f7c14bbaSAndroid Build Coastguard Worker
3967*f7c14bbaSAndroid Build Coastguard Worker tname = btf__name_by_offset(btf, t->name_off);
3968*f7c14bbaSAndroid Build Coastguard Worker if (strcmp(tname, ext_name))
3969*f7c14bbaSAndroid Build Coastguard Worker continue;
3970*f7c14bbaSAndroid Build Coastguard Worker
3971*f7c14bbaSAndroid Build Coastguard Worker if (btf_is_var(t) &&
3972*f7c14bbaSAndroid Build Coastguard Worker btf_var(t)->linkage != BTF_VAR_GLOBAL_EXTERN)
3973*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
3974*f7c14bbaSAndroid Build Coastguard Worker
3975*f7c14bbaSAndroid Build Coastguard Worker if (btf_is_func(t) && btf_func_linkage(t) != BTF_FUNC_EXTERN)
3976*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
3977*f7c14bbaSAndroid Build Coastguard Worker
3978*f7c14bbaSAndroid Build Coastguard Worker return i;
3979*f7c14bbaSAndroid Build Coastguard Worker }
3980*f7c14bbaSAndroid Build Coastguard Worker
3981*f7c14bbaSAndroid Build Coastguard Worker return -ENOENT;
3982*f7c14bbaSAndroid Build Coastguard Worker }
3983*f7c14bbaSAndroid Build Coastguard Worker
find_extern_sec_btf_id(struct btf * btf,int ext_btf_id)3984*f7c14bbaSAndroid Build Coastguard Worker static int find_extern_sec_btf_id(struct btf *btf, int ext_btf_id) {
3985*f7c14bbaSAndroid Build Coastguard Worker const struct btf_var_secinfo *vs;
3986*f7c14bbaSAndroid Build Coastguard Worker const struct btf_type *t;
3987*f7c14bbaSAndroid Build Coastguard Worker int i, j, n;
3988*f7c14bbaSAndroid Build Coastguard Worker
3989*f7c14bbaSAndroid Build Coastguard Worker if (!btf)
3990*f7c14bbaSAndroid Build Coastguard Worker return -ESRCH;
3991*f7c14bbaSAndroid Build Coastguard Worker
3992*f7c14bbaSAndroid Build Coastguard Worker n = btf__type_cnt(btf);
3993*f7c14bbaSAndroid Build Coastguard Worker for (i = 1; i < n; i++) {
3994*f7c14bbaSAndroid Build Coastguard Worker t = btf__type_by_id(btf, i);
3995*f7c14bbaSAndroid Build Coastguard Worker
3996*f7c14bbaSAndroid Build Coastguard Worker if (!btf_is_datasec(t))
3997*f7c14bbaSAndroid Build Coastguard Worker continue;
3998*f7c14bbaSAndroid Build Coastguard Worker
3999*f7c14bbaSAndroid Build Coastguard Worker vs = btf_var_secinfos(t);
4000*f7c14bbaSAndroid Build Coastguard Worker for (j = 0; j < btf_vlen(t); j++, vs++) {
4001*f7c14bbaSAndroid Build Coastguard Worker if (vs->type == ext_btf_id)
4002*f7c14bbaSAndroid Build Coastguard Worker return i;
4003*f7c14bbaSAndroid Build Coastguard Worker }
4004*f7c14bbaSAndroid Build Coastguard Worker }
4005*f7c14bbaSAndroid Build Coastguard Worker
4006*f7c14bbaSAndroid Build Coastguard Worker return -ENOENT;
4007*f7c14bbaSAndroid Build Coastguard Worker }
4008*f7c14bbaSAndroid Build Coastguard Worker
find_kcfg_type(const struct btf * btf,int id,bool * is_signed)4009*f7c14bbaSAndroid Build Coastguard Worker static enum kcfg_type find_kcfg_type(const struct btf *btf, int id,
4010*f7c14bbaSAndroid Build Coastguard Worker bool *is_signed)
4011*f7c14bbaSAndroid Build Coastguard Worker {
4012*f7c14bbaSAndroid Build Coastguard Worker const struct btf_type *t;
4013*f7c14bbaSAndroid Build Coastguard Worker const char *name;
4014*f7c14bbaSAndroid Build Coastguard Worker
4015*f7c14bbaSAndroid Build Coastguard Worker t = skip_mods_and_typedefs(btf, id, NULL);
4016*f7c14bbaSAndroid Build Coastguard Worker name = btf__name_by_offset(btf, t->name_off);
4017*f7c14bbaSAndroid Build Coastguard Worker
4018*f7c14bbaSAndroid Build Coastguard Worker if (is_signed)
4019*f7c14bbaSAndroid Build Coastguard Worker *is_signed = false;
4020*f7c14bbaSAndroid Build Coastguard Worker switch (btf_kind(t)) {
4021*f7c14bbaSAndroid Build Coastguard Worker case BTF_KIND_INT: {
4022*f7c14bbaSAndroid Build Coastguard Worker int enc = btf_int_encoding(t);
4023*f7c14bbaSAndroid Build Coastguard Worker
4024*f7c14bbaSAndroid Build Coastguard Worker if (enc & BTF_INT_BOOL)
4025*f7c14bbaSAndroid Build Coastguard Worker return t->size == 1 ? KCFG_BOOL : KCFG_UNKNOWN;
4026*f7c14bbaSAndroid Build Coastguard Worker if (is_signed)
4027*f7c14bbaSAndroid Build Coastguard Worker *is_signed = enc & BTF_INT_SIGNED;
4028*f7c14bbaSAndroid Build Coastguard Worker if (t->size == 1)
4029*f7c14bbaSAndroid Build Coastguard Worker return KCFG_CHAR;
4030*f7c14bbaSAndroid Build Coastguard Worker if (t->size < 1 || t->size > 8 || (t->size & (t->size - 1)))
4031*f7c14bbaSAndroid Build Coastguard Worker return KCFG_UNKNOWN;
4032*f7c14bbaSAndroid Build Coastguard Worker return KCFG_INT;
4033*f7c14bbaSAndroid Build Coastguard Worker }
4034*f7c14bbaSAndroid Build Coastguard Worker case BTF_KIND_ENUM:
4035*f7c14bbaSAndroid Build Coastguard Worker if (t->size != 4)
4036*f7c14bbaSAndroid Build Coastguard Worker return KCFG_UNKNOWN;
4037*f7c14bbaSAndroid Build Coastguard Worker if (strcmp(name, "libbpf_tristate"))
4038*f7c14bbaSAndroid Build Coastguard Worker return KCFG_UNKNOWN;
4039*f7c14bbaSAndroid Build Coastguard Worker return KCFG_TRISTATE;
4040*f7c14bbaSAndroid Build Coastguard Worker case BTF_KIND_ENUM64:
4041*f7c14bbaSAndroid Build Coastguard Worker if (strcmp(name, "libbpf_tristate"))
4042*f7c14bbaSAndroid Build Coastguard Worker return KCFG_UNKNOWN;
4043*f7c14bbaSAndroid Build Coastguard Worker return KCFG_TRISTATE;
4044*f7c14bbaSAndroid Build Coastguard Worker case BTF_KIND_ARRAY:
4045*f7c14bbaSAndroid Build Coastguard Worker if (btf_array(t)->nelems == 0)
4046*f7c14bbaSAndroid Build Coastguard Worker return KCFG_UNKNOWN;
4047*f7c14bbaSAndroid Build Coastguard Worker if (find_kcfg_type(btf, btf_array(t)->type, NULL) != KCFG_CHAR)
4048*f7c14bbaSAndroid Build Coastguard Worker return KCFG_UNKNOWN;
4049*f7c14bbaSAndroid Build Coastguard Worker return KCFG_CHAR_ARR;
4050*f7c14bbaSAndroid Build Coastguard Worker default:
4051*f7c14bbaSAndroid Build Coastguard Worker return KCFG_UNKNOWN;
4052*f7c14bbaSAndroid Build Coastguard Worker }
4053*f7c14bbaSAndroid Build Coastguard Worker }
4054*f7c14bbaSAndroid Build Coastguard Worker
cmp_externs(const void * _a,const void * _b)4055*f7c14bbaSAndroid Build Coastguard Worker static int cmp_externs(const void *_a, const void *_b)
4056*f7c14bbaSAndroid Build Coastguard Worker {
4057*f7c14bbaSAndroid Build Coastguard Worker const struct extern_desc *a = _a;
4058*f7c14bbaSAndroid Build Coastguard Worker const struct extern_desc *b = _b;
4059*f7c14bbaSAndroid Build Coastguard Worker
4060*f7c14bbaSAndroid Build Coastguard Worker if (a->type != b->type)
4061*f7c14bbaSAndroid Build Coastguard Worker return a->type < b->type ? -1 : 1;
4062*f7c14bbaSAndroid Build Coastguard Worker
4063*f7c14bbaSAndroid Build Coastguard Worker if (a->type == EXT_KCFG) {
4064*f7c14bbaSAndroid Build Coastguard Worker /* descending order by alignment requirements */
4065*f7c14bbaSAndroid Build Coastguard Worker if (a->kcfg.align != b->kcfg.align)
4066*f7c14bbaSAndroid Build Coastguard Worker return a->kcfg.align > b->kcfg.align ? -1 : 1;
4067*f7c14bbaSAndroid Build Coastguard Worker /* ascending order by size, within same alignment class */
4068*f7c14bbaSAndroid Build Coastguard Worker if (a->kcfg.sz != b->kcfg.sz)
4069*f7c14bbaSAndroid Build Coastguard Worker return a->kcfg.sz < b->kcfg.sz ? -1 : 1;
4070*f7c14bbaSAndroid Build Coastguard Worker }
4071*f7c14bbaSAndroid Build Coastguard Worker
4072*f7c14bbaSAndroid Build Coastguard Worker /* resolve ties by name */
4073*f7c14bbaSAndroid Build Coastguard Worker return strcmp(a->name, b->name);
4074*f7c14bbaSAndroid Build Coastguard Worker }
4075*f7c14bbaSAndroid Build Coastguard Worker
find_int_btf_id(const struct btf * btf)4076*f7c14bbaSAndroid Build Coastguard Worker static int find_int_btf_id(const struct btf *btf)
4077*f7c14bbaSAndroid Build Coastguard Worker {
4078*f7c14bbaSAndroid Build Coastguard Worker const struct btf_type *t;
4079*f7c14bbaSAndroid Build Coastguard Worker int i, n;
4080*f7c14bbaSAndroid Build Coastguard Worker
4081*f7c14bbaSAndroid Build Coastguard Worker n = btf__type_cnt(btf);
4082*f7c14bbaSAndroid Build Coastguard Worker for (i = 1; i < n; i++) {
4083*f7c14bbaSAndroid Build Coastguard Worker t = btf__type_by_id(btf, i);
4084*f7c14bbaSAndroid Build Coastguard Worker
4085*f7c14bbaSAndroid Build Coastguard Worker if (btf_is_int(t) && btf_int_bits(t) == 32)
4086*f7c14bbaSAndroid Build Coastguard Worker return i;
4087*f7c14bbaSAndroid Build Coastguard Worker }
4088*f7c14bbaSAndroid Build Coastguard Worker
4089*f7c14bbaSAndroid Build Coastguard Worker return 0;
4090*f7c14bbaSAndroid Build Coastguard Worker }
4091*f7c14bbaSAndroid Build Coastguard Worker
add_dummy_ksym_var(struct btf * btf)4092*f7c14bbaSAndroid Build Coastguard Worker static int add_dummy_ksym_var(struct btf *btf)
4093*f7c14bbaSAndroid Build Coastguard Worker {
4094*f7c14bbaSAndroid Build Coastguard Worker int i, int_btf_id, sec_btf_id, dummy_var_btf_id;
4095*f7c14bbaSAndroid Build Coastguard Worker const struct btf_var_secinfo *vs;
4096*f7c14bbaSAndroid Build Coastguard Worker const struct btf_type *sec;
4097*f7c14bbaSAndroid Build Coastguard Worker
4098*f7c14bbaSAndroid Build Coastguard Worker if (!btf)
4099*f7c14bbaSAndroid Build Coastguard Worker return 0;
4100*f7c14bbaSAndroid Build Coastguard Worker
4101*f7c14bbaSAndroid Build Coastguard Worker sec_btf_id = btf__find_by_name_kind(btf, KSYMS_SEC,
4102*f7c14bbaSAndroid Build Coastguard Worker BTF_KIND_DATASEC);
4103*f7c14bbaSAndroid Build Coastguard Worker if (sec_btf_id < 0)
4104*f7c14bbaSAndroid Build Coastguard Worker return 0;
4105*f7c14bbaSAndroid Build Coastguard Worker
4106*f7c14bbaSAndroid Build Coastguard Worker sec = btf__type_by_id(btf, sec_btf_id);
4107*f7c14bbaSAndroid Build Coastguard Worker vs = btf_var_secinfos(sec);
4108*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < btf_vlen(sec); i++, vs++) {
4109*f7c14bbaSAndroid Build Coastguard Worker const struct btf_type *vt;
4110*f7c14bbaSAndroid Build Coastguard Worker
4111*f7c14bbaSAndroid Build Coastguard Worker vt = btf__type_by_id(btf, vs->type);
4112*f7c14bbaSAndroid Build Coastguard Worker if (btf_is_func(vt))
4113*f7c14bbaSAndroid Build Coastguard Worker break;
4114*f7c14bbaSAndroid Build Coastguard Worker }
4115*f7c14bbaSAndroid Build Coastguard Worker
4116*f7c14bbaSAndroid Build Coastguard Worker /* No func in ksyms sec. No need to add dummy var. */
4117*f7c14bbaSAndroid Build Coastguard Worker if (i == btf_vlen(sec))
4118*f7c14bbaSAndroid Build Coastguard Worker return 0;
4119*f7c14bbaSAndroid Build Coastguard Worker
4120*f7c14bbaSAndroid Build Coastguard Worker int_btf_id = find_int_btf_id(btf);
4121*f7c14bbaSAndroid Build Coastguard Worker dummy_var_btf_id = btf__add_var(btf,
4122*f7c14bbaSAndroid Build Coastguard Worker "dummy_ksym",
4123*f7c14bbaSAndroid Build Coastguard Worker BTF_VAR_GLOBAL_ALLOCATED,
4124*f7c14bbaSAndroid Build Coastguard Worker int_btf_id);
4125*f7c14bbaSAndroid Build Coastguard Worker if (dummy_var_btf_id < 0)
4126*f7c14bbaSAndroid Build Coastguard Worker pr_warn("cannot create a dummy_ksym var\n");
4127*f7c14bbaSAndroid Build Coastguard Worker
4128*f7c14bbaSAndroid Build Coastguard Worker return dummy_var_btf_id;
4129*f7c14bbaSAndroid Build Coastguard Worker }
4130*f7c14bbaSAndroid Build Coastguard Worker
bpf_object__collect_externs(struct bpf_object * obj)4131*f7c14bbaSAndroid Build Coastguard Worker static int bpf_object__collect_externs(struct bpf_object *obj)
4132*f7c14bbaSAndroid Build Coastguard Worker {
4133*f7c14bbaSAndroid Build Coastguard Worker struct btf_type *sec, *kcfg_sec = NULL, *ksym_sec = NULL;
4134*f7c14bbaSAndroid Build Coastguard Worker const struct btf_type *t;
4135*f7c14bbaSAndroid Build Coastguard Worker struct extern_desc *ext;
4136*f7c14bbaSAndroid Build Coastguard Worker int i, n, off, dummy_var_btf_id;
4137*f7c14bbaSAndroid Build Coastguard Worker const char *ext_name, *sec_name;
4138*f7c14bbaSAndroid Build Coastguard Worker size_t ext_essent_len;
4139*f7c14bbaSAndroid Build Coastguard Worker Elf_Scn *scn;
4140*f7c14bbaSAndroid Build Coastguard Worker Elf64_Shdr *sh;
4141*f7c14bbaSAndroid Build Coastguard Worker
4142*f7c14bbaSAndroid Build Coastguard Worker if (!obj->efile.symbols)
4143*f7c14bbaSAndroid Build Coastguard Worker return 0;
4144*f7c14bbaSAndroid Build Coastguard Worker
4145*f7c14bbaSAndroid Build Coastguard Worker scn = elf_sec_by_idx(obj, obj->efile.symbols_shndx);
4146*f7c14bbaSAndroid Build Coastguard Worker sh = elf_sec_hdr(obj, scn);
4147*f7c14bbaSAndroid Build Coastguard Worker if (!sh || sh->sh_entsize != sizeof(Elf64_Sym))
4148*f7c14bbaSAndroid Build Coastguard Worker return -LIBBPF_ERRNO__FORMAT;
4149*f7c14bbaSAndroid Build Coastguard Worker
4150*f7c14bbaSAndroid Build Coastguard Worker dummy_var_btf_id = add_dummy_ksym_var(obj->btf);
4151*f7c14bbaSAndroid Build Coastguard Worker if (dummy_var_btf_id < 0)
4152*f7c14bbaSAndroid Build Coastguard Worker return dummy_var_btf_id;
4153*f7c14bbaSAndroid Build Coastguard Worker
4154*f7c14bbaSAndroid Build Coastguard Worker n = sh->sh_size / sh->sh_entsize;
4155*f7c14bbaSAndroid Build Coastguard Worker pr_debug("looking for externs among %d symbols...\n", n);
4156*f7c14bbaSAndroid Build Coastguard Worker
4157*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < n; i++) {
4158*f7c14bbaSAndroid Build Coastguard Worker Elf64_Sym *sym = elf_sym_by_idx(obj, i);
4159*f7c14bbaSAndroid Build Coastguard Worker
4160*f7c14bbaSAndroid Build Coastguard Worker if (!sym)
4161*f7c14bbaSAndroid Build Coastguard Worker return -LIBBPF_ERRNO__FORMAT;
4162*f7c14bbaSAndroid Build Coastguard Worker if (!sym_is_extern(sym))
4163*f7c14bbaSAndroid Build Coastguard Worker continue;
4164*f7c14bbaSAndroid Build Coastguard Worker ext_name = elf_sym_str(obj, sym->st_name);
4165*f7c14bbaSAndroid Build Coastguard Worker if (!ext_name || !ext_name[0])
4166*f7c14bbaSAndroid Build Coastguard Worker continue;
4167*f7c14bbaSAndroid Build Coastguard Worker
4168*f7c14bbaSAndroid Build Coastguard Worker ext = obj->externs;
4169*f7c14bbaSAndroid Build Coastguard Worker ext = libbpf_reallocarray(ext, obj->nr_extern + 1, sizeof(*ext));
4170*f7c14bbaSAndroid Build Coastguard Worker if (!ext)
4171*f7c14bbaSAndroid Build Coastguard Worker return -ENOMEM;
4172*f7c14bbaSAndroid Build Coastguard Worker obj->externs = ext;
4173*f7c14bbaSAndroid Build Coastguard Worker ext = &ext[obj->nr_extern];
4174*f7c14bbaSAndroid Build Coastguard Worker memset(ext, 0, sizeof(*ext));
4175*f7c14bbaSAndroid Build Coastguard Worker obj->nr_extern++;
4176*f7c14bbaSAndroid Build Coastguard Worker
4177*f7c14bbaSAndroid Build Coastguard Worker ext->btf_id = find_extern_btf_id(obj->btf, ext_name);
4178*f7c14bbaSAndroid Build Coastguard Worker if (ext->btf_id <= 0) {
4179*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to find BTF for extern '%s': %d\n",
4180*f7c14bbaSAndroid Build Coastguard Worker ext_name, ext->btf_id);
4181*f7c14bbaSAndroid Build Coastguard Worker return ext->btf_id;
4182*f7c14bbaSAndroid Build Coastguard Worker }
4183*f7c14bbaSAndroid Build Coastguard Worker t = btf__type_by_id(obj->btf, ext->btf_id);
4184*f7c14bbaSAndroid Build Coastguard Worker ext->name = btf__name_by_offset(obj->btf, t->name_off);
4185*f7c14bbaSAndroid Build Coastguard Worker ext->sym_idx = i;
4186*f7c14bbaSAndroid Build Coastguard Worker ext->is_weak = ELF64_ST_BIND(sym->st_info) == STB_WEAK;
4187*f7c14bbaSAndroid Build Coastguard Worker
4188*f7c14bbaSAndroid Build Coastguard Worker ext_essent_len = bpf_core_essential_name_len(ext->name);
4189*f7c14bbaSAndroid Build Coastguard Worker ext->essent_name = NULL;
4190*f7c14bbaSAndroid Build Coastguard Worker if (ext_essent_len != strlen(ext->name)) {
4191*f7c14bbaSAndroid Build Coastguard Worker ext->essent_name = strndup(ext->name, ext_essent_len);
4192*f7c14bbaSAndroid Build Coastguard Worker if (!ext->essent_name)
4193*f7c14bbaSAndroid Build Coastguard Worker return -ENOMEM;
4194*f7c14bbaSAndroid Build Coastguard Worker }
4195*f7c14bbaSAndroid Build Coastguard Worker
4196*f7c14bbaSAndroid Build Coastguard Worker ext->sec_btf_id = find_extern_sec_btf_id(obj->btf, ext->btf_id);
4197*f7c14bbaSAndroid Build Coastguard Worker if (ext->sec_btf_id <= 0) {
4198*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to find BTF for extern '%s' [%d] section: %d\n",
4199*f7c14bbaSAndroid Build Coastguard Worker ext_name, ext->btf_id, ext->sec_btf_id);
4200*f7c14bbaSAndroid Build Coastguard Worker return ext->sec_btf_id;
4201*f7c14bbaSAndroid Build Coastguard Worker }
4202*f7c14bbaSAndroid Build Coastguard Worker sec = (void *)btf__type_by_id(obj->btf, ext->sec_btf_id);
4203*f7c14bbaSAndroid Build Coastguard Worker sec_name = btf__name_by_offset(obj->btf, sec->name_off);
4204*f7c14bbaSAndroid Build Coastguard Worker
4205*f7c14bbaSAndroid Build Coastguard Worker if (strcmp(sec_name, KCONFIG_SEC) == 0) {
4206*f7c14bbaSAndroid Build Coastguard Worker if (btf_is_func(t)) {
4207*f7c14bbaSAndroid Build Coastguard Worker pr_warn("extern function %s is unsupported under %s section\n",
4208*f7c14bbaSAndroid Build Coastguard Worker ext->name, KCONFIG_SEC);
4209*f7c14bbaSAndroid Build Coastguard Worker return -ENOTSUP;
4210*f7c14bbaSAndroid Build Coastguard Worker }
4211*f7c14bbaSAndroid Build Coastguard Worker kcfg_sec = sec;
4212*f7c14bbaSAndroid Build Coastguard Worker ext->type = EXT_KCFG;
4213*f7c14bbaSAndroid Build Coastguard Worker ext->kcfg.sz = btf__resolve_size(obj->btf, t->type);
4214*f7c14bbaSAndroid Build Coastguard Worker if (ext->kcfg.sz <= 0) {
4215*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to resolve size of extern (kcfg) '%s': %d\n",
4216*f7c14bbaSAndroid Build Coastguard Worker ext_name, ext->kcfg.sz);
4217*f7c14bbaSAndroid Build Coastguard Worker return ext->kcfg.sz;
4218*f7c14bbaSAndroid Build Coastguard Worker }
4219*f7c14bbaSAndroid Build Coastguard Worker ext->kcfg.align = btf__align_of(obj->btf, t->type);
4220*f7c14bbaSAndroid Build Coastguard Worker if (ext->kcfg.align <= 0) {
4221*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to determine alignment of extern (kcfg) '%s': %d\n",
4222*f7c14bbaSAndroid Build Coastguard Worker ext_name, ext->kcfg.align);
4223*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
4224*f7c14bbaSAndroid Build Coastguard Worker }
4225*f7c14bbaSAndroid Build Coastguard Worker ext->kcfg.type = find_kcfg_type(obj->btf, t->type,
4226*f7c14bbaSAndroid Build Coastguard Worker &ext->kcfg.is_signed);
4227*f7c14bbaSAndroid Build Coastguard Worker if (ext->kcfg.type == KCFG_UNKNOWN) {
4228*f7c14bbaSAndroid Build Coastguard Worker pr_warn("extern (kcfg) '%s': type is unsupported\n", ext_name);
4229*f7c14bbaSAndroid Build Coastguard Worker return -ENOTSUP;
4230*f7c14bbaSAndroid Build Coastguard Worker }
4231*f7c14bbaSAndroid Build Coastguard Worker } else if (strcmp(sec_name, KSYMS_SEC) == 0) {
4232*f7c14bbaSAndroid Build Coastguard Worker ksym_sec = sec;
4233*f7c14bbaSAndroid Build Coastguard Worker ext->type = EXT_KSYM;
4234*f7c14bbaSAndroid Build Coastguard Worker skip_mods_and_typedefs(obj->btf, t->type,
4235*f7c14bbaSAndroid Build Coastguard Worker &ext->ksym.type_id);
4236*f7c14bbaSAndroid Build Coastguard Worker } else {
4237*f7c14bbaSAndroid Build Coastguard Worker pr_warn("unrecognized extern section '%s'\n", sec_name);
4238*f7c14bbaSAndroid Build Coastguard Worker return -ENOTSUP;
4239*f7c14bbaSAndroid Build Coastguard Worker }
4240*f7c14bbaSAndroid Build Coastguard Worker }
4241*f7c14bbaSAndroid Build Coastguard Worker pr_debug("collected %d externs total\n", obj->nr_extern);
4242*f7c14bbaSAndroid Build Coastguard Worker
4243*f7c14bbaSAndroid Build Coastguard Worker if (!obj->nr_extern)
4244*f7c14bbaSAndroid Build Coastguard Worker return 0;
4245*f7c14bbaSAndroid Build Coastguard Worker
4246*f7c14bbaSAndroid Build Coastguard Worker /* sort externs by type, for kcfg ones also by (align, size, name) */
4247*f7c14bbaSAndroid Build Coastguard Worker qsort(obj->externs, obj->nr_extern, sizeof(*ext), cmp_externs);
4248*f7c14bbaSAndroid Build Coastguard Worker
4249*f7c14bbaSAndroid Build Coastguard Worker /* for .ksyms section, we need to turn all externs into allocated
4250*f7c14bbaSAndroid Build Coastguard Worker * variables in BTF to pass kernel verification; we do this by
4251*f7c14bbaSAndroid Build Coastguard Worker * pretending that each extern is a 8-byte variable
4252*f7c14bbaSAndroid Build Coastguard Worker */
4253*f7c14bbaSAndroid Build Coastguard Worker if (ksym_sec) {
4254*f7c14bbaSAndroid Build Coastguard Worker /* find existing 4-byte integer type in BTF to use for fake
4255*f7c14bbaSAndroid Build Coastguard Worker * extern variables in DATASEC
4256*f7c14bbaSAndroid Build Coastguard Worker */
4257*f7c14bbaSAndroid Build Coastguard Worker int int_btf_id = find_int_btf_id(obj->btf);
4258*f7c14bbaSAndroid Build Coastguard Worker /* For extern function, a dummy_var added earlier
4259*f7c14bbaSAndroid Build Coastguard Worker * will be used to replace the vs->type and
4260*f7c14bbaSAndroid Build Coastguard Worker * its name string will be used to refill
4261*f7c14bbaSAndroid Build Coastguard Worker * the missing param's name.
4262*f7c14bbaSAndroid Build Coastguard Worker */
4263*f7c14bbaSAndroid Build Coastguard Worker const struct btf_type *dummy_var;
4264*f7c14bbaSAndroid Build Coastguard Worker
4265*f7c14bbaSAndroid Build Coastguard Worker dummy_var = btf__type_by_id(obj->btf, dummy_var_btf_id);
4266*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < obj->nr_extern; i++) {
4267*f7c14bbaSAndroid Build Coastguard Worker ext = &obj->externs[i];
4268*f7c14bbaSAndroid Build Coastguard Worker if (ext->type != EXT_KSYM)
4269*f7c14bbaSAndroid Build Coastguard Worker continue;
4270*f7c14bbaSAndroid Build Coastguard Worker pr_debug("extern (ksym) #%d: symbol %d, name %s\n",
4271*f7c14bbaSAndroid Build Coastguard Worker i, ext->sym_idx, ext->name);
4272*f7c14bbaSAndroid Build Coastguard Worker }
4273*f7c14bbaSAndroid Build Coastguard Worker
4274*f7c14bbaSAndroid Build Coastguard Worker sec = ksym_sec;
4275*f7c14bbaSAndroid Build Coastguard Worker n = btf_vlen(sec);
4276*f7c14bbaSAndroid Build Coastguard Worker for (i = 0, off = 0; i < n; i++, off += sizeof(int)) {
4277*f7c14bbaSAndroid Build Coastguard Worker struct btf_var_secinfo *vs = btf_var_secinfos(sec) + i;
4278*f7c14bbaSAndroid Build Coastguard Worker struct btf_type *vt;
4279*f7c14bbaSAndroid Build Coastguard Worker
4280*f7c14bbaSAndroid Build Coastguard Worker vt = (void *)btf__type_by_id(obj->btf, vs->type);
4281*f7c14bbaSAndroid Build Coastguard Worker ext_name = btf__name_by_offset(obj->btf, vt->name_off);
4282*f7c14bbaSAndroid Build Coastguard Worker ext = find_extern_by_name(obj, ext_name);
4283*f7c14bbaSAndroid Build Coastguard Worker if (!ext) {
4284*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to find extern definition for BTF %s '%s'\n",
4285*f7c14bbaSAndroid Build Coastguard Worker btf_kind_str(vt), ext_name);
4286*f7c14bbaSAndroid Build Coastguard Worker return -ESRCH;
4287*f7c14bbaSAndroid Build Coastguard Worker }
4288*f7c14bbaSAndroid Build Coastguard Worker if (btf_is_func(vt)) {
4289*f7c14bbaSAndroid Build Coastguard Worker const struct btf_type *func_proto;
4290*f7c14bbaSAndroid Build Coastguard Worker struct btf_param *param;
4291*f7c14bbaSAndroid Build Coastguard Worker int j;
4292*f7c14bbaSAndroid Build Coastguard Worker
4293*f7c14bbaSAndroid Build Coastguard Worker func_proto = btf__type_by_id(obj->btf,
4294*f7c14bbaSAndroid Build Coastguard Worker vt->type);
4295*f7c14bbaSAndroid Build Coastguard Worker param = btf_params(func_proto);
4296*f7c14bbaSAndroid Build Coastguard Worker /* Reuse the dummy_var string if the
4297*f7c14bbaSAndroid Build Coastguard Worker * func proto does not have param name.
4298*f7c14bbaSAndroid Build Coastguard Worker */
4299*f7c14bbaSAndroid Build Coastguard Worker for (j = 0; j < btf_vlen(func_proto); j++)
4300*f7c14bbaSAndroid Build Coastguard Worker if (param[j].type && !param[j].name_off)
4301*f7c14bbaSAndroid Build Coastguard Worker param[j].name_off =
4302*f7c14bbaSAndroid Build Coastguard Worker dummy_var->name_off;
4303*f7c14bbaSAndroid Build Coastguard Worker vs->type = dummy_var_btf_id;
4304*f7c14bbaSAndroid Build Coastguard Worker vt->info &= ~0xffff;
4305*f7c14bbaSAndroid Build Coastguard Worker vt->info |= BTF_FUNC_GLOBAL;
4306*f7c14bbaSAndroid Build Coastguard Worker } else {
4307*f7c14bbaSAndroid Build Coastguard Worker btf_var(vt)->linkage = BTF_VAR_GLOBAL_ALLOCATED;
4308*f7c14bbaSAndroid Build Coastguard Worker vt->type = int_btf_id;
4309*f7c14bbaSAndroid Build Coastguard Worker }
4310*f7c14bbaSAndroid Build Coastguard Worker vs->offset = off;
4311*f7c14bbaSAndroid Build Coastguard Worker vs->size = sizeof(int);
4312*f7c14bbaSAndroid Build Coastguard Worker }
4313*f7c14bbaSAndroid Build Coastguard Worker sec->size = off;
4314*f7c14bbaSAndroid Build Coastguard Worker }
4315*f7c14bbaSAndroid Build Coastguard Worker
4316*f7c14bbaSAndroid Build Coastguard Worker if (kcfg_sec) {
4317*f7c14bbaSAndroid Build Coastguard Worker sec = kcfg_sec;
4318*f7c14bbaSAndroid Build Coastguard Worker /* for kcfg externs calculate their offsets within a .kconfig map */
4319*f7c14bbaSAndroid Build Coastguard Worker off = 0;
4320*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < obj->nr_extern; i++) {
4321*f7c14bbaSAndroid Build Coastguard Worker ext = &obj->externs[i];
4322*f7c14bbaSAndroid Build Coastguard Worker if (ext->type != EXT_KCFG)
4323*f7c14bbaSAndroid Build Coastguard Worker continue;
4324*f7c14bbaSAndroid Build Coastguard Worker
4325*f7c14bbaSAndroid Build Coastguard Worker ext->kcfg.data_off = roundup(off, ext->kcfg.align);
4326*f7c14bbaSAndroid Build Coastguard Worker off = ext->kcfg.data_off + ext->kcfg.sz;
4327*f7c14bbaSAndroid Build Coastguard Worker pr_debug("extern (kcfg) #%d: symbol %d, off %u, name %s\n",
4328*f7c14bbaSAndroid Build Coastguard Worker i, ext->sym_idx, ext->kcfg.data_off, ext->name);
4329*f7c14bbaSAndroid Build Coastguard Worker }
4330*f7c14bbaSAndroid Build Coastguard Worker sec->size = off;
4331*f7c14bbaSAndroid Build Coastguard Worker n = btf_vlen(sec);
4332*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < n; i++) {
4333*f7c14bbaSAndroid Build Coastguard Worker struct btf_var_secinfo *vs = btf_var_secinfos(sec) + i;
4334*f7c14bbaSAndroid Build Coastguard Worker
4335*f7c14bbaSAndroid Build Coastguard Worker t = btf__type_by_id(obj->btf, vs->type);
4336*f7c14bbaSAndroid Build Coastguard Worker ext_name = btf__name_by_offset(obj->btf, t->name_off);
4337*f7c14bbaSAndroid Build Coastguard Worker ext = find_extern_by_name(obj, ext_name);
4338*f7c14bbaSAndroid Build Coastguard Worker if (!ext) {
4339*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to find extern definition for BTF var '%s'\n",
4340*f7c14bbaSAndroid Build Coastguard Worker ext_name);
4341*f7c14bbaSAndroid Build Coastguard Worker return -ESRCH;
4342*f7c14bbaSAndroid Build Coastguard Worker }
4343*f7c14bbaSAndroid Build Coastguard Worker btf_var(t)->linkage = BTF_VAR_GLOBAL_ALLOCATED;
4344*f7c14bbaSAndroid Build Coastguard Worker vs->offset = ext->kcfg.data_off;
4345*f7c14bbaSAndroid Build Coastguard Worker }
4346*f7c14bbaSAndroid Build Coastguard Worker }
4347*f7c14bbaSAndroid Build Coastguard Worker return 0;
4348*f7c14bbaSAndroid Build Coastguard Worker }
4349*f7c14bbaSAndroid Build Coastguard Worker
prog_is_subprog(const struct bpf_object * obj,const struct bpf_program * prog)4350*f7c14bbaSAndroid Build Coastguard Worker static bool prog_is_subprog(const struct bpf_object *obj, const struct bpf_program *prog)
4351*f7c14bbaSAndroid Build Coastguard Worker {
4352*f7c14bbaSAndroid Build Coastguard Worker return prog->sec_idx == obj->efile.text_shndx && obj->nr_programs > 1;
4353*f7c14bbaSAndroid Build Coastguard Worker }
4354*f7c14bbaSAndroid Build Coastguard Worker
4355*f7c14bbaSAndroid Build Coastguard Worker struct bpf_program *
bpf_object__find_program_by_name(const struct bpf_object * obj,const char * name)4356*f7c14bbaSAndroid Build Coastguard Worker bpf_object__find_program_by_name(const struct bpf_object *obj,
4357*f7c14bbaSAndroid Build Coastguard Worker const char *name)
4358*f7c14bbaSAndroid Build Coastguard Worker {
4359*f7c14bbaSAndroid Build Coastguard Worker struct bpf_program *prog;
4360*f7c14bbaSAndroid Build Coastguard Worker
4361*f7c14bbaSAndroid Build Coastguard Worker bpf_object__for_each_program(prog, obj) {
4362*f7c14bbaSAndroid Build Coastguard Worker if (prog_is_subprog(obj, prog))
4363*f7c14bbaSAndroid Build Coastguard Worker continue;
4364*f7c14bbaSAndroid Build Coastguard Worker if (!strcmp(prog->name, name))
4365*f7c14bbaSAndroid Build Coastguard Worker return prog;
4366*f7c14bbaSAndroid Build Coastguard Worker }
4367*f7c14bbaSAndroid Build Coastguard Worker return errno = ENOENT, NULL;
4368*f7c14bbaSAndroid Build Coastguard Worker }
4369*f7c14bbaSAndroid Build Coastguard Worker
bpf_object__shndx_is_data(const struct bpf_object * obj,int shndx)4370*f7c14bbaSAndroid Build Coastguard Worker static bool bpf_object__shndx_is_data(const struct bpf_object *obj,
4371*f7c14bbaSAndroid Build Coastguard Worker int shndx)
4372*f7c14bbaSAndroid Build Coastguard Worker {
4373*f7c14bbaSAndroid Build Coastguard Worker switch (obj->efile.secs[shndx].sec_type) {
4374*f7c14bbaSAndroid Build Coastguard Worker case SEC_BSS:
4375*f7c14bbaSAndroid Build Coastguard Worker case SEC_DATA:
4376*f7c14bbaSAndroid Build Coastguard Worker case SEC_RODATA:
4377*f7c14bbaSAndroid Build Coastguard Worker return true;
4378*f7c14bbaSAndroid Build Coastguard Worker default:
4379*f7c14bbaSAndroid Build Coastguard Worker return false;
4380*f7c14bbaSAndroid Build Coastguard Worker }
4381*f7c14bbaSAndroid Build Coastguard Worker }
4382*f7c14bbaSAndroid Build Coastguard Worker
bpf_object__shndx_is_maps(const struct bpf_object * obj,int shndx)4383*f7c14bbaSAndroid Build Coastguard Worker static bool bpf_object__shndx_is_maps(const struct bpf_object *obj,
4384*f7c14bbaSAndroid Build Coastguard Worker int shndx)
4385*f7c14bbaSAndroid Build Coastguard Worker {
4386*f7c14bbaSAndroid Build Coastguard Worker return shndx == obj->efile.btf_maps_shndx;
4387*f7c14bbaSAndroid Build Coastguard Worker }
4388*f7c14bbaSAndroid Build Coastguard Worker
4389*f7c14bbaSAndroid Build Coastguard Worker static enum libbpf_map_type
bpf_object__section_to_libbpf_map_type(const struct bpf_object * obj,int shndx)4390*f7c14bbaSAndroid Build Coastguard Worker bpf_object__section_to_libbpf_map_type(const struct bpf_object *obj, int shndx)
4391*f7c14bbaSAndroid Build Coastguard Worker {
4392*f7c14bbaSAndroid Build Coastguard Worker if (shndx == obj->efile.symbols_shndx)
4393*f7c14bbaSAndroid Build Coastguard Worker return LIBBPF_MAP_KCONFIG;
4394*f7c14bbaSAndroid Build Coastguard Worker
4395*f7c14bbaSAndroid Build Coastguard Worker switch (obj->efile.secs[shndx].sec_type) {
4396*f7c14bbaSAndroid Build Coastguard Worker case SEC_BSS:
4397*f7c14bbaSAndroid Build Coastguard Worker return LIBBPF_MAP_BSS;
4398*f7c14bbaSAndroid Build Coastguard Worker case SEC_DATA:
4399*f7c14bbaSAndroid Build Coastguard Worker return LIBBPF_MAP_DATA;
4400*f7c14bbaSAndroid Build Coastguard Worker case SEC_RODATA:
4401*f7c14bbaSAndroid Build Coastguard Worker return LIBBPF_MAP_RODATA;
4402*f7c14bbaSAndroid Build Coastguard Worker default:
4403*f7c14bbaSAndroid Build Coastguard Worker return LIBBPF_MAP_UNSPEC;
4404*f7c14bbaSAndroid Build Coastguard Worker }
4405*f7c14bbaSAndroid Build Coastguard Worker }
4406*f7c14bbaSAndroid Build Coastguard Worker
bpf_program__record_reloc(struct bpf_program * prog,struct reloc_desc * reloc_desc,__u32 insn_idx,const char * sym_name,const Elf64_Sym * sym,const Elf64_Rel * rel)4407*f7c14bbaSAndroid Build Coastguard Worker static int bpf_program__record_reloc(struct bpf_program *prog,
4408*f7c14bbaSAndroid Build Coastguard Worker struct reloc_desc *reloc_desc,
4409*f7c14bbaSAndroid Build Coastguard Worker __u32 insn_idx, const char *sym_name,
4410*f7c14bbaSAndroid Build Coastguard Worker const Elf64_Sym *sym, const Elf64_Rel *rel)
4411*f7c14bbaSAndroid Build Coastguard Worker {
4412*f7c14bbaSAndroid Build Coastguard Worker struct bpf_insn *insn = &prog->insns[insn_idx];
4413*f7c14bbaSAndroid Build Coastguard Worker size_t map_idx, nr_maps = prog->obj->nr_maps;
4414*f7c14bbaSAndroid Build Coastguard Worker struct bpf_object *obj = prog->obj;
4415*f7c14bbaSAndroid Build Coastguard Worker __u32 shdr_idx = sym->st_shndx;
4416*f7c14bbaSAndroid Build Coastguard Worker enum libbpf_map_type type;
4417*f7c14bbaSAndroid Build Coastguard Worker const char *sym_sec_name;
4418*f7c14bbaSAndroid Build Coastguard Worker struct bpf_map *map;
4419*f7c14bbaSAndroid Build Coastguard Worker
4420*f7c14bbaSAndroid Build Coastguard Worker if (!is_call_insn(insn) && !is_ldimm64_insn(insn)) {
4421*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': invalid relo against '%s' for insns[%d].code 0x%x\n",
4422*f7c14bbaSAndroid Build Coastguard Worker prog->name, sym_name, insn_idx, insn->code);
4423*f7c14bbaSAndroid Build Coastguard Worker return -LIBBPF_ERRNO__RELOC;
4424*f7c14bbaSAndroid Build Coastguard Worker }
4425*f7c14bbaSAndroid Build Coastguard Worker
4426*f7c14bbaSAndroid Build Coastguard Worker if (sym_is_extern(sym)) {
4427*f7c14bbaSAndroid Build Coastguard Worker int sym_idx = ELF64_R_SYM(rel->r_info);
4428*f7c14bbaSAndroid Build Coastguard Worker int i, n = obj->nr_extern;
4429*f7c14bbaSAndroid Build Coastguard Worker struct extern_desc *ext;
4430*f7c14bbaSAndroid Build Coastguard Worker
4431*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < n; i++) {
4432*f7c14bbaSAndroid Build Coastguard Worker ext = &obj->externs[i];
4433*f7c14bbaSAndroid Build Coastguard Worker if (ext->sym_idx == sym_idx)
4434*f7c14bbaSAndroid Build Coastguard Worker break;
4435*f7c14bbaSAndroid Build Coastguard Worker }
4436*f7c14bbaSAndroid Build Coastguard Worker if (i >= n) {
4437*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': extern relo failed to find extern for '%s' (%d)\n",
4438*f7c14bbaSAndroid Build Coastguard Worker prog->name, sym_name, sym_idx);
4439*f7c14bbaSAndroid Build Coastguard Worker return -LIBBPF_ERRNO__RELOC;
4440*f7c14bbaSAndroid Build Coastguard Worker }
4441*f7c14bbaSAndroid Build Coastguard Worker pr_debug("prog '%s': found extern #%d '%s' (sym %d) for insn #%u\n",
4442*f7c14bbaSAndroid Build Coastguard Worker prog->name, i, ext->name, ext->sym_idx, insn_idx);
4443*f7c14bbaSAndroid Build Coastguard Worker if (insn->code == (BPF_JMP | BPF_CALL))
4444*f7c14bbaSAndroid Build Coastguard Worker reloc_desc->type = RELO_EXTERN_CALL;
4445*f7c14bbaSAndroid Build Coastguard Worker else
4446*f7c14bbaSAndroid Build Coastguard Worker reloc_desc->type = RELO_EXTERN_LD64;
4447*f7c14bbaSAndroid Build Coastguard Worker reloc_desc->insn_idx = insn_idx;
4448*f7c14bbaSAndroid Build Coastguard Worker reloc_desc->ext_idx = i;
4449*f7c14bbaSAndroid Build Coastguard Worker return 0;
4450*f7c14bbaSAndroid Build Coastguard Worker }
4451*f7c14bbaSAndroid Build Coastguard Worker
4452*f7c14bbaSAndroid Build Coastguard Worker /* sub-program call relocation */
4453*f7c14bbaSAndroid Build Coastguard Worker if (is_call_insn(insn)) {
4454*f7c14bbaSAndroid Build Coastguard Worker if (insn->src_reg != BPF_PSEUDO_CALL) {
4455*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': incorrect bpf_call opcode\n", prog->name);
4456*f7c14bbaSAndroid Build Coastguard Worker return -LIBBPF_ERRNO__RELOC;
4457*f7c14bbaSAndroid Build Coastguard Worker }
4458*f7c14bbaSAndroid Build Coastguard Worker /* text_shndx can be 0, if no default "main" program exists */
4459*f7c14bbaSAndroid Build Coastguard Worker if (!shdr_idx || shdr_idx != obj->efile.text_shndx) {
4460*f7c14bbaSAndroid Build Coastguard Worker sym_sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, shdr_idx));
4461*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': bad call relo against '%s' in section '%s'\n",
4462*f7c14bbaSAndroid Build Coastguard Worker prog->name, sym_name, sym_sec_name);
4463*f7c14bbaSAndroid Build Coastguard Worker return -LIBBPF_ERRNO__RELOC;
4464*f7c14bbaSAndroid Build Coastguard Worker }
4465*f7c14bbaSAndroid Build Coastguard Worker if (sym->st_value % BPF_INSN_SZ) {
4466*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': bad call relo against '%s' at offset %zu\n",
4467*f7c14bbaSAndroid Build Coastguard Worker prog->name, sym_name, (size_t)sym->st_value);
4468*f7c14bbaSAndroid Build Coastguard Worker return -LIBBPF_ERRNO__RELOC;
4469*f7c14bbaSAndroid Build Coastguard Worker }
4470*f7c14bbaSAndroid Build Coastguard Worker reloc_desc->type = RELO_CALL;
4471*f7c14bbaSAndroid Build Coastguard Worker reloc_desc->insn_idx = insn_idx;
4472*f7c14bbaSAndroid Build Coastguard Worker reloc_desc->sym_off = sym->st_value;
4473*f7c14bbaSAndroid Build Coastguard Worker return 0;
4474*f7c14bbaSAndroid Build Coastguard Worker }
4475*f7c14bbaSAndroid Build Coastguard Worker
4476*f7c14bbaSAndroid Build Coastguard Worker if (!shdr_idx || shdr_idx >= SHN_LORESERVE) {
4477*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': invalid relo against '%s' in special section 0x%x; forgot to initialize global var?..\n",
4478*f7c14bbaSAndroid Build Coastguard Worker prog->name, sym_name, shdr_idx);
4479*f7c14bbaSAndroid Build Coastguard Worker return -LIBBPF_ERRNO__RELOC;
4480*f7c14bbaSAndroid Build Coastguard Worker }
4481*f7c14bbaSAndroid Build Coastguard Worker
4482*f7c14bbaSAndroid Build Coastguard Worker /* loading subprog addresses */
4483*f7c14bbaSAndroid Build Coastguard Worker if (sym_is_subprog(sym, obj->efile.text_shndx)) {
4484*f7c14bbaSAndroid Build Coastguard Worker /* global_func: sym->st_value = offset in the section, insn->imm = 0.
4485*f7c14bbaSAndroid Build Coastguard Worker * local_func: sym->st_value = 0, insn->imm = offset in the section.
4486*f7c14bbaSAndroid Build Coastguard Worker */
4487*f7c14bbaSAndroid Build Coastguard Worker if ((sym->st_value % BPF_INSN_SZ) || (insn->imm % BPF_INSN_SZ)) {
4488*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': bad subprog addr relo against '%s' at offset %zu+%d\n",
4489*f7c14bbaSAndroid Build Coastguard Worker prog->name, sym_name, (size_t)sym->st_value, insn->imm);
4490*f7c14bbaSAndroid Build Coastguard Worker return -LIBBPF_ERRNO__RELOC;
4491*f7c14bbaSAndroid Build Coastguard Worker }
4492*f7c14bbaSAndroid Build Coastguard Worker
4493*f7c14bbaSAndroid Build Coastguard Worker reloc_desc->type = RELO_SUBPROG_ADDR;
4494*f7c14bbaSAndroid Build Coastguard Worker reloc_desc->insn_idx = insn_idx;
4495*f7c14bbaSAndroid Build Coastguard Worker reloc_desc->sym_off = sym->st_value;
4496*f7c14bbaSAndroid Build Coastguard Worker return 0;
4497*f7c14bbaSAndroid Build Coastguard Worker }
4498*f7c14bbaSAndroid Build Coastguard Worker
4499*f7c14bbaSAndroid Build Coastguard Worker type = bpf_object__section_to_libbpf_map_type(obj, shdr_idx);
4500*f7c14bbaSAndroid Build Coastguard Worker sym_sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, shdr_idx));
4501*f7c14bbaSAndroid Build Coastguard Worker
4502*f7c14bbaSAndroid Build Coastguard Worker /* arena data relocation */
4503*f7c14bbaSAndroid Build Coastguard Worker if (shdr_idx == obj->efile.arena_data_shndx) {
4504*f7c14bbaSAndroid Build Coastguard Worker reloc_desc->type = RELO_DATA;
4505*f7c14bbaSAndroid Build Coastguard Worker reloc_desc->insn_idx = insn_idx;
4506*f7c14bbaSAndroid Build Coastguard Worker reloc_desc->map_idx = obj->arena_map - obj->maps;
4507*f7c14bbaSAndroid Build Coastguard Worker reloc_desc->sym_off = sym->st_value;
4508*f7c14bbaSAndroid Build Coastguard Worker return 0;
4509*f7c14bbaSAndroid Build Coastguard Worker }
4510*f7c14bbaSAndroid Build Coastguard Worker
4511*f7c14bbaSAndroid Build Coastguard Worker /* generic map reference relocation */
4512*f7c14bbaSAndroid Build Coastguard Worker if (type == LIBBPF_MAP_UNSPEC) {
4513*f7c14bbaSAndroid Build Coastguard Worker if (!bpf_object__shndx_is_maps(obj, shdr_idx)) {
4514*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': bad map relo against '%s' in section '%s'\n",
4515*f7c14bbaSAndroid Build Coastguard Worker prog->name, sym_name, sym_sec_name);
4516*f7c14bbaSAndroid Build Coastguard Worker return -LIBBPF_ERRNO__RELOC;
4517*f7c14bbaSAndroid Build Coastguard Worker }
4518*f7c14bbaSAndroid Build Coastguard Worker for (map_idx = 0; map_idx < nr_maps; map_idx++) {
4519*f7c14bbaSAndroid Build Coastguard Worker map = &obj->maps[map_idx];
4520*f7c14bbaSAndroid Build Coastguard Worker if (map->libbpf_type != type ||
4521*f7c14bbaSAndroid Build Coastguard Worker map->sec_idx != sym->st_shndx ||
4522*f7c14bbaSAndroid Build Coastguard Worker map->sec_offset != sym->st_value)
4523*f7c14bbaSAndroid Build Coastguard Worker continue;
4524*f7c14bbaSAndroid Build Coastguard Worker pr_debug("prog '%s': found map %zd (%s, sec %d, off %zu) for insn #%u\n",
4525*f7c14bbaSAndroid Build Coastguard Worker prog->name, map_idx, map->name, map->sec_idx,
4526*f7c14bbaSAndroid Build Coastguard Worker map->sec_offset, insn_idx);
4527*f7c14bbaSAndroid Build Coastguard Worker break;
4528*f7c14bbaSAndroid Build Coastguard Worker }
4529*f7c14bbaSAndroid Build Coastguard Worker if (map_idx >= nr_maps) {
4530*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': map relo failed to find map for section '%s', off %zu\n",
4531*f7c14bbaSAndroid Build Coastguard Worker prog->name, sym_sec_name, (size_t)sym->st_value);
4532*f7c14bbaSAndroid Build Coastguard Worker return -LIBBPF_ERRNO__RELOC;
4533*f7c14bbaSAndroid Build Coastguard Worker }
4534*f7c14bbaSAndroid Build Coastguard Worker reloc_desc->type = RELO_LD64;
4535*f7c14bbaSAndroid Build Coastguard Worker reloc_desc->insn_idx = insn_idx;
4536*f7c14bbaSAndroid Build Coastguard Worker reloc_desc->map_idx = map_idx;
4537*f7c14bbaSAndroid Build Coastguard Worker reloc_desc->sym_off = 0; /* sym->st_value determines map_idx */
4538*f7c14bbaSAndroid Build Coastguard Worker return 0;
4539*f7c14bbaSAndroid Build Coastguard Worker }
4540*f7c14bbaSAndroid Build Coastguard Worker
4541*f7c14bbaSAndroid Build Coastguard Worker /* global data map relocation */
4542*f7c14bbaSAndroid Build Coastguard Worker if (!bpf_object__shndx_is_data(obj, shdr_idx)) {
4543*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': bad data relo against section '%s'\n",
4544*f7c14bbaSAndroid Build Coastguard Worker prog->name, sym_sec_name);
4545*f7c14bbaSAndroid Build Coastguard Worker return -LIBBPF_ERRNO__RELOC;
4546*f7c14bbaSAndroid Build Coastguard Worker }
4547*f7c14bbaSAndroid Build Coastguard Worker for (map_idx = 0; map_idx < nr_maps; map_idx++) {
4548*f7c14bbaSAndroid Build Coastguard Worker map = &obj->maps[map_idx];
4549*f7c14bbaSAndroid Build Coastguard Worker if (map->libbpf_type != type || map->sec_idx != sym->st_shndx)
4550*f7c14bbaSAndroid Build Coastguard Worker continue;
4551*f7c14bbaSAndroid Build Coastguard Worker pr_debug("prog '%s': found data map %zd (%s, sec %d, off %zu) for insn %u\n",
4552*f7c14bbaSAndroid Build Coastguard Worker prog->name, map_idx, map->name, map->sec_idx,
4553*f7c14bbaSAndroid Build Coastguard Worker map->sec_offset, insn_idx);
4554*f7c14bbaSAndroid Build Coastguard Worker break;
4555*f7c14bbaSAndroid Build Coastguard Worker }
4556*f7c14bbaSAndroid Build Coastguard Worker if (map_idx >= nr_maps) {
4557*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': data relo failed to find map for section '%s'\n",
4558*f7c14bbaSAndroid Build Coastguard Worker prog->name, sym_sec_name);
4559*f7c14bbaSAndroid Build Coastguard Worker return -LIBBPF_ERRNO__RELOC;
4560*f7c14bbaSAndroid Build Coastguard Worker }
4561*f7c14bbaSAndroid Build Coastguard Worker
4562*f7c14bbaSAndroid Build Coastguard Worker reloc_desc->type = RELO_DATA;
4563*f7c14bbaSAndroid Build Coastguard Worker reloc_desc->insn_idx = insn_idx;
4564*f7c14bbaSAndroid Build Coastguard Worker reloc_desc->map_idx = map_idx;
4565*f7c14bbaSAndroid Build Coastguard Worker reloc_desc->sym_off = sym->st_value;
4566*f7c14bbaSAndroid Build Coastguard Worker return 0;
4567*f7c14bbaSAndroid Build Coastguard Worker }
4568*f7c14bbaSAndroid Build Coastguard Worker
prog_contains_insn(const struct bpf_program * prog,size_t insn_idx)4569*f7c14bbaSAndroid Build Coastguard Worker static bool prog_contains_insn(const struct bpf_program *prog, size_t insn_idx)
4570*f7c14bbaSAndroid Build Coastguard Worker {
4571*f7c14bbaSAndroid Build Coastguard Worker return insn_idx >= prog->sec_insn_off &&
4572*f7c14bbaSAndroid Build Coastguard Worker insn_idx < prog->sec_insn_off + prog->sec_insn_cnt;
4573*f7c14bbaSAndroid Build Coastguard Worker }
4574*f7c14bbaSAndroid Build Coastguard Worker
find_prog_by_sec_insn(const struct bpf_object * obj,size_t sec_idx,size_t insn_idx)4575*f7c14bbaSAndroid Build Coastguard Worker static struct bpf_program *find_prog_by_sec_insn(const struct bpf_object *obj,
4576*f7c14bbaSAndroid Build Coastguard Worker size_t sec_idx, size_t insn_idx)
4577*f7c14bbaSAndroid Build Coastguard Worker {
4578*f7c14bbaSAndroid Build Coastguard Worker int l = 0, r = obj->nr_programs - 1, m;
4579*f7c14bbaSAndroid Build Coastguard Worker struct bpf_program *prog;
4580*f7c14bbaSAndroid Build Coastguard Worker
4581*f7c14bbaSAndroid Build Coastguard Worker if (!obj->nr_programs)
4582*f7c14bbaSAndroid Build Coastguard Worker return NULL;
4583*f7c14bbaSAndroid Build Coastguard Worker
4584*f7c14bbaSAndroid Build Coastguard Worker while (l < r) {
4585*f7c14bbaSAndroid Build Coastguard Worker m = l + (r - l + 1) / 2;
4586*f7c14bbaSAndroid Build Coastguard Worker prog = &obj->programs[m];
4587*f7c14bbaSAndroid Build Coastguard Worker
4588*f7c14bbaSAndroid Build Coastguard Worker if (prog->sec_idx < sec_idx ||
4589*f7c14bbaSAndroid Build Coastguard Worker (prog->sec_idx == sec_idx && prog->sec_insn_off <= insn_idx))
4590*f7c14bbaSAndroid Build Coastguard Worker l = m;
4591*f7c14bbaSAndroid Build Coastguard Worker else
4592*f7c14bbaSAndroid Build Coastguard Worker r = m - 1;
4593*f7c14bbaSAndroid Build Coastguard Worker }
4594*f7c14bbaSAndroid Build Coastguard Worker /* matching program could be at index l, but it still might be the
4595*f7c14bbaSAndroid Build Coastguard Worker * wrong one, so we need to double check conditions for the last time
4596*f7c14bbaSAndroid Build Coastguard Worker */
4597*f7c14bbaSAndroid Build Coastguard Worker prog = &obj->programs[l];
4598*f7c14bbaSAndroid Build Coastguard Worker if (prog->sec_idx == sec_idx && prog_contains_insn(prog, insn_idx))
4599*f7c14bbaSAndroid Build Coastguard Worker return prog;
4600*f7c14bbaSAndroid Build Coastguard Worker return NULL;
4601*f7c14bbaSAndroid Build Coastguard Worker }
4602*f7c14bbaSAndroid Build Coastguard Worker
4603*f7c14bbaSAndroid Build Coastguard Worker static int
bpf_object__collect_prog_relos(struct bpf_object * obj,Elf64_Shdr * shdr,Elf_Data * data)4604*f7c14bbaSAndroid Build Coastguard Worker bpf_object__collect_prog_relos(struct bpf_object *obj, Elf64_Shdr *shdr, Elf_Data *data)
4605*f7c14bbaSAndroid Build Coastguard Worker {
4606*f7c14bbaSAndroid Build Coastguard Worker const char *relo_sec_name, *sec_name;
4607*f7c14bbaSAndroid Build Coastguard Worker size_t sec_idx = shdr->sh_info, sym_idx;
4608*f7c14bbaSAndroid Build Coastguard Worker struct bpf_program *prog;
4609*f7c14bbaSAndroid Build Coastguard Worker struct reloc_desc *relos;
4610*f7c14bbaSAndroid Build Coastguard Worker int err, i, nrels;
4611*f7c14bbaSAndroid Build Coastguard Worker const char *sym_name;
4612*f7c14bbaSAndroid Build Coastguard Worker __u32 insn_idx;
4613*f7c14bbaSAndroid Build Coastguard Worker Elf_Scn *scn;
4614*f7c14bbaSAndroid Build Coastguard Worker Elf_Data *scn_data;
4615*f7c14bbaSAndroid Build Coastguard Worker Elf64_Sym *sym;
4616*f7c14bbaSAndroid Build Coastguard Worker Elf64_Rel *rel;
4617*f7c14bbaSAndroid Build Coastguard Worker
4618*f7c14bbaSAndroid Build Coastguard Worker if (sec_idx >= obj->efile.sec_cnt)
4619*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
4620*f7c14bbaSAndroid Build Coastguard Worker
4621*f7c14bbaSAndroid Build Coastguard Worker scn = elf_sec_by_idx(obj, sec_idx);
4622*f7c14bbaSAndroid Build Coastguard Worker scn_data = elf_sec_data(obj, scn);
4623*f7c14bbaSAndroid Build Coastguard Worker if (!scn_data)
4624*f7c14bbaSAndroid Build Coastguard Worker return -LIBBPF_ERRNO__FORMAT;
4625*f7c14bbaSAndroid Build Coastguard Worker
4626*f7c14bbaSAndroid Build Coastguard Worker relo_sec_name = elf_sec_str(obj, shdr->sh_name);
4627*f7c14bbaSAndroid Build Coastguard Worker sec_name = elf_sec_name(obj, scn);
4628*f7c14bbaSAndroid Build Coastguard Worker if (!relo_sec_name || !sec_name)
4629*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
4630*f7c14bbaSAndroid Build Coastguard Worker
4631*f7c14bbaSAndroid Build Coastguard Worker pr_debug("sec '%s': collecting relocation for section(%zu) '%s'\n",
4632*f7c14bbaSAndroid Build Coastguard Worker relo_sec_name, sec_idx, sec_name);
4633*f7c14bbaSAndroid Build Coastguard Worker nrels = shdr->sh_size / shdr->sh_entsize;
4634*f7c14bbaSAndroid Build Coastguard Worker
4635*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < nrels; i++) {
4636*f7c14bbaSAndroid Build Coastguard Worker rel = elf_rel_by_idx(data, i);
4637*f7c14bbaSAndroid Build Coastguard Worker if (!rel) {
4638*f7c14bbaSAndroid Build Coastguard Worker pr_warn("sec '%s': failed to get relo #%d\n", relo_sec_name, i);
4639*f7c14bbaSAndroid Build Coastguard Worker return -LIBBPF_ERRNO__FORMAT;
4640*f7c14bbaSAndroid Build Coastguard Worker }
4641*f7c14bbaSAndroid Build Coastguard Worker
4642*f7c14bbaSAndroid Build Coastguard Worker sym_idx = ELF64_R_SYM(rel->r_info);
4643*f7c14bbaSAndroid Build Coastguard Worker sym = elf_sym_by_idx(obj, sym_idx);
4644*f7c14bbaSAndroid Build Coastguard Worker if (!sym) {
4645*f7c14bbaSAndroid Build Coastguard Worker pr_warn("sec '%s': symbol #%zu not found for relo #%d\n",
4646*f7c14bbaSAndroid Build Coastguard Worker relo_sec_name, sym_idx, i);
4647*f7c14bbaSAndroid Build Coastguard Worker return -LIBBPF_ERRNO__FORMAT;
4648*f7c14bbaSAndroid Build Coastguard Worker }
4649*f7c14bbaSAndroid Build Coastguard Worker
4650*f7c14bbaSAndroid Build Coastguard Worker if (sym->st_shndx >= obj->efile.sec_cnt) {
4651*f7c14bbaSAndroid Build Coastguard Worker pr_warn("sec '%s': corrupted symbol #%zu pointing to invalid section #%zu for relo #%d\n",
4652*f7c14bbaSAndroid Build Coastguard Worker relo_sec_name, sym_idx, (size_t)sym->st_shndx, i);
4653*f7c14bbaSAndroid Build Coastguard Worker return -LIBBPF_ERRNO__FORMAT;
4654*f7c14bbaSAndroid Build Coastguard Worker }
4655*f7c14bbaSAndroid Build Coastguard Worker
4656*f7c14bbaSAndroid Build Coastguard Worker if (rel->r_offset % BPF_INSN_SZ || rel->r_offset >= scn_data->d_size) {
4657*f7c14bbaSAndroid Build Coastguard Worker pr_warn("sec '%s': invalid offset 0x%zx for relo #%d\n",
4658*f7c14bbaSAndroid Build Coastguard Worker relo_sec_name, (size_t)rel->r_offset, i);
4659*f7c14bbaSAndroid Build Coastguard Worker return -LIBBPF_ERRNO__FORMAT;
4660*f7c14bbaSAndroid Build Coastguard Worker }
4661*f7c14bbaSAndroid Build Coastguard Worker
4662*f7c14bbaSAndroid Build Coastguard Worker insn_idx = rel->r_offset / BPF_INSN_SZ;
4663*f7c14bbaSAndroid Build Coastguard Worker /* relocations against static functions are recorded as
4664*f7c14bbaSAndroid Build Coastguard Worker * relocations against the section that contains a function;
4665*f7c14bbaSAndroid Build Coastguard Worker * in such case, symbol will be STT_SECTION and sym.st_name
4666*f7c14bbaSAndroid Build Coastguard Worker * will point to empty string (0), so fetch section name
4667*f7c14bbaSAndroid Build Coastguard Worker * instead
4668*f7c14bbaSAndroid Build Coastguard Worker */
4669*f7c14bbaSAndroid Build Coastguard Worker if (ELF64_ST_TYPE(sym->st_info) == STT_SECTION && sym->st_name == 0)
4670*f7c14bbaSAndroid Build Coastguard Worker sym_name = elf_sec_name(obj, elf_sec_by_idx(obj, sym->st_shndx));
4671*f7c14bbaSAndroid Build Coastguard Worker else
4672*f7c14bbaSAndroid Build Coastguard Worker sym_name = elf_sym_str(obj, sym->st_name);
4673*f7c14bbaSAndroid Build Coastguard Worker sym_name = sym_name ?: "<?";
4674*f7c14bbaSAndroid Build Coastguard Worker
4675*f7c14bbaSAndroid Build Coastguard Worker pr_debug("sec '%s': relo #%d: insn #%u against '%s'\n",
4676*f7c14bbaSAndroid Build Coastguard Worker relo_sec_name, i, insn_idx, sym_name);
4677*f7c14bbaSAndroid Build Coastguard Worker
4678*f7c14bbaSAndroid Build Coastguard Worker prog = find_prog_by_sec_insn(obj, sec_idx, insn_idx);
4679*f7c14bbaSAndroid Build Coastguard Worker if (!prog) {
4680*f7c14bbaSAndroid Build Coastguard Worker pr_debug("sec '%s': relo #%d: couldn't find program in section '%s' for insn #%u, probably overridden weak function, skipping...\n",
4681*f7c14bbaSAndroid Build Coastguard Worker relo_sec_name, i, sec_name, insn_idx);
4682*f7c14bbaSAndroid Build Coastguard Worker continue;
4683*f7c14bbaSAndroid Build Coastguard Worker }
4684*f7c14bbaSAndroid Build Coastguard Worker
4685*f7c14bbaSAndroid Build Coastguard Worker relos = libbpf_reallocarray(prog->reloc_desc,
4686*f7c14bbaSAndroid Build Coastguard Worker prog->nr_reloc + 1, sizeof(*relos));
4687*f7c14bbaSAndroid Build Coastguard Worker if (!relos)
4688*f7c14bbaSAndroid Build Coastguard Worker return -ENOMEM;
4689*f7c14bbaSAndroid Build Coastguard Worker prog->reloc_desc = relos;
4690*f7c14bbaSAndroid Build Coastguard Worker
4691*f7c14bbaSAndroid Build Coastguard Worker /* adjust insn_idx to local BPF program frame of reference */
4692*f7c14bbaSAndroid Build Coastguard Worker insn_idx -= prog->sec_insn_off;
4693*f7c14bbaSAndroid Build Coastguard Worker err = bpf_program__record_reloc(prog, &relos[prog->nr_reloc],
4694*f7c14bbaSAndroid Build Coastguard Worker insn_idx, sym_name, sym, rel);
4695*f7c14bbaSAndroid Build Coastguard Worker if (err)
4696*f7c14bbaSAndroid Build Coastguard Worker return err;
4697*f7c14bbaSAndroid Build Coastguard Worker
4698*f7c14bbaSAndroid Build Coastguard Worker prog->nr_reloc++;
4699*f7c14bbaSAndroid Build Coastguard Worker }
4700*f7c14bbaSAndroid Build Coastguard Worker return 0;
4701*f7c14bbaSAndroid Build Coastguard Worker }
4702*f7c14bbaSAndroid Build Coastguard Worker
map_fill_btf_type_info(struct bpf_object * obj,struct bpf_map * map)4703*f7c14bbaSAndroid Build Coastguard Worker static int map_fill_btf_type_info(struct bpf_object *obj, struct bpf_map *map)
4704*f7c14bbaSAndroid Build Coastguard Worker {
4705*f7c14bbaSAndroid Build Coastguard Worker int id;
4706*f7c14bbaSAndroid Build Coastguard Worker
4707*f7c14bbaSAndroid Build Coastguard Worker if (!obj->btf)
4708*f7c14bbaSAndroid Build Coastguard Worker return -ENOENT;
4709*f7c14bbaSAndroid Build Coastguard Worker
4710*f7c14bbaSAndroid Build Coastguard Worker /* if it's BTF-defined map, we don't need to search for type IDs.
4711*f7c14bbaSAndroid Build Coastguard Worker * For struct_ops map, it does not need btf_key_type_id and
4712*f7c14bbaSAndroid Build Coastguard Worker * btf_value_type_id.
4713*f7c14bbaSAndroid Build Coastguard Worker */
4714*f7c14bbaSAndroid Build Coastguard Worker if (map->sec_idx == obj->efile.btf_maps_shndx || bpf_map__is_struct_ops(map))
4715*f7c14bbaSAndroid Build Coastguard Worker return 0;
4716*f7c14bbaSAndroid Build Coastguard Worker
4717*f7c14bbaSAndroid Build Coastguard Worker /*
4718*f7c14bbaSAndroid Build Coastguard Worker * LLVM annotates global data differently in BTF, that is,
4719*f7c14bbaSAndroid Build Coastguard Worker * only as '.data', '.bss' or '.rodata'.
4720*f7c14bbaSAndroid Build Coastguard Worker */
4721*f7c14bbaSAndroid Build Coastguard Worker if (!bpf_map__is_internal(map))
4722*f7c14bbaSAndroid Build Coastguard Worker return -ENOENT;
4723*f7c14bbaSAndroid Build Coastguard Worker
4724*f7c14bbaSAndroid Build Coastguard Worker id = btf__find_by_name(obj->btf, map->real_name);
4725*f7c14bbaSAndroid Build Coastguard Worker if (id < 0)
4726*f7c14bbaSAndroid Build Coastguard Worker return id;
4727*f7c14bbaSAndroid Build Coastguard Worker
4728*f7c14bbaSAndroid Build Coastguard Worker map->btf_key_type_id = 0;
4729*f7c14bbaSAndroid Build Coastguard Worker map->btf_value_type_id = id;
4730*f7c14bbaSAndroid Build Coastguard Worker return 0;
4731*f7c14bbaSAndroid Build Coastguard Worker }
4732*f7c14bbaSAndroid Build Coastguard Worker
bpf_get_map_info_from_fdinfo(int fd,struct bpf_map_info * info)4733*f7c14bbaSAndroid Build Coastguard Worker static int bpf_get_map_info_from_fdinfo(int fd, struct bpf_map_info *info)
4734*f7c14bbaSAndroid Build Coastguard Worker {
4735*f7c14bbaSAndroid Build Coastguard Worker char file[PATH_MAX], buff[4096];
4736*f7c14bbaSAndroid Build Coastguard Worker FILE *fp;
4737*f7c14bbaSAndroid Build Coastguard Worker __u32 val;
4738*f7c14bbaSAndroid Build Coastguard Worker int err;
4739*f7c14bbaSAndroid Build Coastguard Worker
4740*f7c14bbaSAndroid Build Coastguard Worker snprintf(file, sizeof(file), "/proc/%d/fdinfo/%d", getpid(), fd);
4741*f7c14bbaSAndroid Build Coastguard Worker memset(info, 0, sizeof(*info));
4742*f7c14bbaSAndroid Build Coastguard Worker
4743*f7c14bbaSAndroid Build Coastguard Worker fp = fopen(file, "re");
4744*f7c14bbaSAndroid Build Coastguard Worker if (!fp) {
4745*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
4746*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to open %s: %d. No procfs support?\n", file,
4747*f7c14bbaSAndroid Build Coastguard Worker err);
4748*f7c14bbaSAndroid Build Coastguard Worker return err;
4749*f7c14bbaSAndroid Build Coastguard Worker }
4750*f7c14bbaSAndroid Build Coastguard Worker
4751*f7c14bbaSAndroid Build Coastguard Worker while (fgets(buff, sizeof(buff), fp)) {
4752*f7c14bbaSAndroid Build Coastguard Worker if (sscanf(buff, "map_type:\t%u", &val) == 1)
4753*f7c14bbaSAndroid Build Coastguard Worker info->type = val;
4754*f7c14bbaSAndroid Build Coastguard Worker else if (sscanf(buff, "key_size:\t%u", &val) == 1)
4755*f7c14bbaSAndroid Build Coastguard Worker info->key_size = val;
4756*f7c14bbaSAndroid Build Coastguard Worker else if (sscanf(buff, "value_size:\t%u", &val) == 1)
4757*f7c14bbaSAndroid Build Coastguard Worker info->value_size = val;
4758*f7c14bbaSAndroid Build Coastguard Worker else if (sscanf(buff, "max_entries:\t%u", &val) == 1)
4759*f7c14bbaSAndroid Build Coastguard Worker info->max_entries = val;
4760*f7c14bbaSAndroid Build Coastguard Worker else if (sscanf(buff, "map_flags:\t%i", &val) == 1)
4761*f7c14bbaSAndroid Build Coastguard Worker info->map_flags = val;
4762*f7c14bbaSAndroid Build Coastguard Worker }
4763*f7c14bbaSAndroid Build Coastguard Worker
4764*f7c14bbaSAndroid Build Coastguard Worker fclose(fp);
4765*f7c14bbaSAndroid Build Coastguard Worker
4766*f7c14bbaSAndroid Build Coastguard Worker return 0;
4767*f7c14bbaSAndroid Build Coastguard Worker }
4768*f7c14bbaSAndroid Build Coastguard Worker
bpf_map__autocreate(const struct bpf_map * map)4769*f7c14bbaSAndroid Build Coastguard Worker bool bpf_map__autocreate(const struct bpf_map *map)
4770*f7c14bbaSAndroid Build Coastguard Worker {
4771*f7c14bbaSAndroid Build Coastguard Worker return map->autocreate;
4772*f7c14bbaSAndroid Build Coastguard Worker }
4773*f7c14bbaSAndroid Build Coastguard Worker
bpf_map__set_autocreate(struct bpf_map * map,bool autocreate)4774*f7c14bbaSAndroid Build Coastguard Worker int bpf_map__set_autocreate(struct bpf_map *map, bool autocreate)
4775*f7c14bbaSAndroid Build Coastguard Worker {
4776*f7c14bbaSAndroid Build Coastguard Worker if (map->obj->loaded)
4777*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-EBUSY);
4778*f7c14bbaSAndroid Build Coastguard Worker
4779*f7c14bbaSAndroid Build Coastguard Worker map->autocreate = autocreate;
4780*f7c14bbaSAndroid Build Coastguard Worker return 0;
4781*f7c14bbaSAndroid Build Coastguard Worker }
4782*f7c14bbaSAndroid Build Coastguard Worker
bpf_map__reuse_fd(struct bpf_map * map,int fd)4783*f7c14bbaSAndroid Build Coastguard Worker int bpf_map__reuse_fd(struct bpf_map *map, int fd)
4784*f7c14bbaSAndroid Build Coastguard Worker {
4785*f7c14bbaSAndroid Build Coastguard Worker struct bpf_map_info info;
4786*f7c14bbaSAndroid Build Coastguard Worker __u32 len = sizeof(info), name_len;
4787*f7c14bbaSAndroid Build Coastguard Worker int new_fd, err;
4788*f7c14bbaSAndroid Build Coastguard Worker char *new_name;
4789*f7c14bbaSAndroid Build Coastguard Worker
4790*f7c14bbaSAndroid Build Coastguard Worker memset(&info, 0, len);
4791*f7c14bbaSAndroid Build Coastguard Worker err = bpf_map_get_info_by_fd(fd, &info, &len);
4792*f7c14bbaSAndroid Build Coastguard Worker if (err && errno == EINVAL)
4793*f7c14bbaSAndroid Build Coastguard Worker err = bpf_get_map_info_from_fdinfo(fd, &info);
4794*f7c14bbaSAndroid Build Coastguard Worker if (err)
4795*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(err);
4796*f7c14bbaSAndroid Build Coastguard Worker
4797*f7c14bbaSAndroid Build Coastguard Worker name_len = strlen(info.name);
4798*f7c14bbaSAndroid Build Coastguard Worker if (name_len == BPF_OBJ_NAME_LEN - 1 && strncmp(map->name, info.name, name_len) == 0)
4799*f7c14bbaSAndroid Build Coastguard Worker new_name = strdup(map->name);
4800*f7c14bbaSAndroid Build Coastguard Worker else
4801*f7c14bbaSAndroid Build Coastguard Worker new_name = strdup(info.name);
4802*f7c14bbaSAndroid Build Coastguard Worker
4803*f7c14bbaSAndroid Build Coastguard Worker if (!new_name)
4804*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-errno);
4805*f7c14bbaSAndroid Build Coastguard Worker
4806*f7c14bbaSAndroid Build Coastguard Worker /*
4807*f7c14bbaSAndroid Build Coastguard Worker * Like dup(), but make sure new FD is >= 3 and has O_CLOEXEC set.
4808*f7c14bbaSAndroid Build Coastguard Worker * This is similar to what we do in ensure_good_fd(), but without
4809*f7c14bbaSAndroid Build Coastguard Worker * closing original FD.
4810*f7c14bbaSAndroid Build Coastguard Worker */
4811*f7c14bbaSAndroid Build Coastguard Worker new_fd = fcntl(fd, F_DUPFD_CLOEXEC, 3);
4812*f7c14bbaSAndroid Build Coastguard Worker if (new_fd < 0) {
4813*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
4814*f7c14bbaSAndroid Build Coastguard Worker goto err_free_new_name;
4815*f7c14bbaSAndroid Build Coastguard Worker }
4816*f7c14bbaSAndroid Build Coastguard Worker
4817*f7c14bbaSAndroid Build Coastguard Worker err = reuse_fd(map->fd, new_fd);
4818*f7c14bbaSAndroid Build Coastguard Worker if (err)
4819*f7c14bbaSAndroid Build Coastguard Worker goto err_free_new_name;
4820*f7c14bbaSAndroid Build Coastguard Worker
4821*f7c14bbaSAndroid Build Coastguard Worker free(map->name);
4822*f7c14bbaSAndroid Build Coastguard Worker
4823*f7c14bbaSAndroid Build Coastguard Worker map->name = new_name;
4824*f7c14bbaSAndroid Build Coastguard Worker map->def.type = info.type;
4825*f7c14bbaSAndroid Build Coastguard Worker map->def.key_size = info.key_size;
4826*f7c14bbaSAndroid Build Coastguard Worker map->def.value_size = info.value_size;
4827*f7c14bbaSAndroid Build Coastguard Worker map->def.max_entries = info.max_entries;
4828*f7c14bbaSAndroid Build Coastguard Worker map->def.map_flags = info.map_flags;
4829*f7c14bbaSAndroid Build Coastguard Worker map->btf_key_type_id = info.btf_key_type_id;
4830*f7c14bbaSAndroid Build Coastguard Worker map->btf_value_type_id = info.btf_value_type_id;
4831*f7c14bbaSAndroid Build Coastguard Worker map->reused = true;
4832*f7c14bbaSAndroid Build Coastguard Worker map->map_extra = info.map_extra;
4833*f7c14bbaSAndroid Build Coastguard Worker
4834*f7c14bbaSAndroid Build Coastguard Worker return 0;
4835*f7c14bbaSAndroid Build Coastguard Worker
4836*f7c14bbaSAndroid Build Coastguard Worker err_free_new_name:
4837*f7c14bbaSAndroid Build Coastguard Worker free(new_name);
4838*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(err);
4839*f7c14bbaSAndroid Build Coastguard Worker }
4840*f7c14bbaSAndroid Build Coastguard Worker
bpf_map__max_entries(const struct bpf_map * map)4841*f7c14bbaSAndroid Build Coastguard Worker __u32 bpf_map__max_entries(const struct bpf_map *map)
4842*f7c14bbaSAndroid Build Coastguard Worker {
4843*f7c14bbaSAndroid Build Coastguard Worker return map->def.max_entries;
4844*f7c14bbaSAndroid Build Coastguard Worker }
4845*f7c14bbaSAndroid Build Coastguard Worker
bpf_map__inner_map(struct bpf_map * map)4846*f7c14bbaSAndroid Build Coastguard Worker struct bpf_map *bpf_map__inner_map(struct bpf_map *map)
4847*f7c14bbaSAndroid Build Coastguard Worker {
4848*f7c14bbaSAndroid Build Coastguard Worker if (!bpf_map_type__is_map_in_map(map->def.type))
4849*f7c14bbaSAndroid Build Coastguard Worker return errno = EINVAL, NULL;
4850*f7c14bbaSAndroid Build Coastguard Worker
4851*f7c14bbaSAndroid Build Coastguard Worker return map->inner_map;
4852*f7c14bbaSAndroid Build Coastguard Worker }
4853*f7c14bbaSAndroid Build Coastguard Worker
bpf_map__set_max_entries(struct bpf_map * map,__u32 max_entries)4854*f7c14bbaSAndroid Build Coastguard Worker int bpf_map__set_max_entries(struct bpf_map *map, __u32 max_entries)
4855*f7c14bbaSAndroid Build Coastguard Worker {
4856*f7c14bbaSAndroid Build Coastguard Worker if (map->obj->loaded)
4857*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-EBUSY);
4858*f7c14bbaSAndroid Build Coastguard Worker
4859*f7c14bbaSAndroid Build Coastguard Worker map->def.max_entries = max_entries;
4860*f7c14bbaSAndroid Build Coastguard Worker
4861*f7c14bbaSAndroid Build Coastguard Worker /* auto-adjust BPF ringbuf map max_entries to be a multiple of page size */
4862*f7c14bbaSAndroid Build Coastguard Worker if (map_is_ringbuf(map))
4863*f7c14bbaSAndroid Build Coastguard Worker map->def.max_entries = adjust_ringbuf_sz(map->def.max_entries);
4864*f7c14bbaSAndroid Build Coastguard Worker
4865*f7c14bbaSAndroid Build Coastguard Worker return 0;
4866*f7c14bbaSAndroid Build Coastguard Worker }
4867*f7c14bbaSAndroid Build Coastguard Worker
bpf_object_prepare_token(struct bpf_object * obj)4868*f7c14bbaSAndroid Build Coastguard Worker static int bpf_object_prepare_token(struct bpf_object *obj)
4869*f7c14bbaSAndroid Build Coastguard Worker {
4870*f7c14bbaSAndroid Build Coastguard Worker const char *bpffs_path;
4871*f7c14bbaSAndroid Build Coastguard Worker int bpffs_fd = -1, token_fd, err;
4872*f7c14bbaSAndroid Build Coastguard Worker bool mandatory;
4873*f7c14bbaSAndroid Build Coastguard Worker enum libbpf_print_level level;
4874*f7c14bbaSAndroid Build Coastguard Worker
4875*f7c14bbaSAndroid Build Coastguard Worker /* token is explicitly prevented */
4876*f7c14bbaSAndroid Build Coastguard Worker if (obj->token_path && obj->token_path[0] == '\0') {
4877*f7c14bbaSAndroid Build Coastguard Worker pr_debug("object '%s': token is prevented, skipping...\n", obj->name);
4878*f7c14bbaSAndroid Build Coastguard Worker return 0;
4879*f7c14bbaSAndroid Build Coastguard Worker }
4880*f7c14bbaSAndroid Build Coastguard Worker
4881*f7c14bbaSAndroid Build Coastguard Worker mandatory = obj->token_path != NULL;
4882*f7c14bbaSAndroid Build Coastguard Worker level = mandatory ? LIBBPF_WARN : LIBBPF_DEBUG;
4883*f7c14bbaSAndroid Build Coastguard Worker
4884*f7c14bbaSAndroid Build Coastguard Worker bpffs_path = obj->token_path ?: BPF_FS_DEFAULT_PATH;
4885*f7c14bbaSAndroid Build Coastguard Worker bpffs_fd = open(bpffs_path, O_DIRECTORY, O_RDWR);
4886*f7c14bbaSAndroid Build Coastguard Worker if (bpffs_fd < 0) {
4887*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
4888*f7c14bbaSAndroid Build Coastguard Worker __pr(level, "object '%s': failed (%d) to open BPF FS mount at '%s'%s\n",
4889*f7c14bbaSAndroid Build Coastguard Worker obj->name, err, bpffs_path,
4890*f7c14bbaSAndroid Build Coastguard Worker mandatory ? "" : ", skipping optional step...");
4891*f7c14bbaSAndroid Build Coastguard Worker return mandatory ? err : 0;
4892*f7c14bbaSAndroid Build Coastguard Worker }
4893*f7c14bbaSAndroid Build Coastguard Worker
4894*f7c14bbaSAndroid Build Coastguard Worker token_fd = bpf_token_create(bpffs_fd, 0);
4895*f7c14bbaSAndroid Build Coastguard Worker close(bpffs_fd);
4896*f7c14bbaSAndroid Build Coastguard Worker if (token_fd < 0) {
4897*f7c14bbaSAndroid Build Coastguard Worker if (!mandatory && token_fd == -ENOENT) {
4898*f7c14bbaSAndroid Build Coastguard Worker pr_debug("object '%s': BPF FS at '%s' doesn't have BPF token delegation set up, skipping...\n",
4899*f7c14bbaSAndroid Build Coastguard Worker obj->name, bpffs_path);
4900*f7c14bbaSAndroid Build Coastguard Worker return 0;
4901*f7c14bbaSAndroid Build Coastguard Worker }
4902*f7c14bbaSAndroid Build Coastguard Worker __pr(level, "object '%s': failed (%d) to create BPF token from '%s'%s\n",
4903*f7c14bbaSAndroid Build Coastguard Worker obj->name, token_fd, bpffs_path,
4904*f7c14bbaSAndroid Build Coastguard Worker mandatory ? "" : ", skipping optional step...");
4905*f7c14bbaSAndroid Build Coastguard Worker return mandatory ? token_fd : 0;
4906*f7c14bbaSAndroid Build Coastguard Worker }
4907*f7c14bbaSAndroid Build Coastguard Worker
4908*f7c14bbaSAndroid Build Coastguard Worker obj->feat_cache = calloc(1, sizeof(*obj->feat_cache));
4909*f7c14bbaSAndroid Build Coastguard Worker if (!obj->feat_cache) {
4910*f7c14bbaSAndroid Build Coastguard Worker close(token_fd);
4911*f7c14bbaSAndroid Build Coastguard Worker return -ENOMEM;
4912*f7c14bbaSAndroid Build Coastguard Worker }
4913*f7c14bbaSAndroid Build Coastguard Worker
4914*f7c14bbaSAndroid Build Coastguard Worker obj->token_fd = token_fd;
4915*f7c14bbaSAndroid Build Coastguard Worker obj->feat_cache->token_fd = token_fd;
4916*f7c14bbaSAndroid Build Coastguard Worker
4917*f7c14bbaSAndroid Build Coastguard Worker return 0;
4918*f7c14bbaSAndroid Build Coastguard Worker }
4919*f7c14bbaSAndroid Build Coastguard Worker
4920*f7c14bbaSAndroid Build Coastguard Worker static int
bpf_object__probe_loading(struct bpf_object * obj)4921*f7c14bbaSAndroid Build Coastguard Worker bpf_object__probe_loading(struct bpf_object *obj)
4922*f7c14bbaSAndroid Build Coastguard Worker {
4923*f7c14bbaSAndroid Build Coastguard Worker char *cp, errmsg[STRERR_BUFSIZE];
4924*f7c14bbaSAndroid Build Coastguard Worker struct bpf_insn insns[] = {
4925*f7c14bbaSAndroid Build Coastguard Worker BPF_MOV64_IMM(BPF_REG_0, 0),
4926*f7c14bbaSAndroid Build Coastguard Worker BPF_EXIT_INSN(),
4927*f7c14bbaSAndroid Build Coastguard Worker };
4928*f7c14bbaSAndroid Build Coastguard Worker int ret, insn_cnt = ARRAY_SIZE(insns);
4929*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_OPTS(bpf_prog_load_opts, opts,
4930*f7c14bbaSAndroid Build Coastguard Worker .token_fd = obj->token_fd,
4931*f7c14bbaSAndroid Build Coastguard Worker .prog_flags = obj->token_fd ? BPF_F_TOKEN_FD : 0,
4932*f7c14bbaSAndroid Build Coastguard Worker );
4933*f7c14bbaSAndroid Build Coastguard Worker
4934*f7c14bbaSAndroid Build Coastguard Worker if (obj->gen_loader)
4935*f7c14bbaSAndroid Build Coastguard Worker return 0;
4936*f7c14bbaSAndroid Build Coastguard Worker
4937*f7c14bbaSAndroid Build Coastguard Worker ret = bump_rlimit_memlock();
4938*f7c14bbaSAndroid Build Coastguard Worker if (ret)
4939*f7c14bbaSAndroid Build Coastguard Worker pr_warn("Failed to bump RLIMIT_MEMLOCK (err = %d), you might need to do it explicitly!\n", ret);
4940*f7c14bbaSAndroid Build Coastguard Worker
4941*f7c14bbaSAndroid Build Coastguard Worker /* make sure basic loading works */
4942*f7c14bbaSAndroid Build Coastguard Worker ret = bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, NULL, "GPL", insns, insn_cnt, &opts);
4943*f7c14bbaSAndroid Build Coastguard Worker if (ret < 0)
4944*f7c14bbaSAndroid Build Coastguard Worker ret = bpf_prog_load(BPF_PROG_TYPE_TRACEPOINT, NULL, "GPL", insns, insn_cnt, &opts);
4945*f7c14bbaSAndroid Build Coastguard Worker if (ret < 0) {
4946*f7c14bbaSAndroid Build Coastguard Worker ret = errno;
4947*f7c14bbaSAndroid Build Coastguard Worker cp = libbpf_strerror_r(ret, errmsg, sizeof(errmsg));
4948*f7c14bbaSAndroid Build Coastguard Worker pr_warn("Error in %s():%s(%d). Couldn't load trivial BPF "
4949*f7c14bbaSAndroid Build Coastguard Worker "program. Make sure your kernel supports BPF "
4950*f7c14bbaSAndroid Build Coastguard Worker "(CONFIG_BPF_SYSCALL=y) and/or that RLIMIT_MEMLOCK is "
4951*f7c14bbaSAndroid Build Coastguard Worker "set to big enough value.\n", __func__, cp, ret);
4952*f7c14bbaSAndroid Build Coastguard Worker return -ret;
4953*f7c14bbaSAndroid Build Coastguard Worker }
4954*f7c14bbaSAndroid Build Coastguard Worker close(ret);
4955*f7c14bbaSAndroid Build Coastguard Worker
4956*f7c14bbaSAndroid Build Coastguard Worker return 0;
4957*f7c14bbaSAndroid Build Coastguard Worker }
4958*f7c14bbaSAndroid Build Coastguard Worker
kernel_supports(const struct bpf_object * obj,enum kern_feature_id feat_id)4959*f7c14bbaSAndroid Build Coastguard Worker bool kernel_supports(const struct bpf_object *obj, enum kern_feature_id feat_id)
4960*f7c14bbaSAndroid Build Coastguard Worker {
4961*f7c14bbaSAndroid Build Coastguard Worker if (obj->gen_loader)
4962*f7c14bbaSAndroid Build Coastguard Worker /* To generate loader program assume the latest kernel
4963*f7c14bbaSAndroid Build Coastguard Worker * to avoid doing extra prog_load, map_create syscalls.
4964*f7c14bbaSAndroid Build Coastguard Worker */
4965*f7c14bbaSAndroid Build Coastguard Worker return true;
4966*f7c14bbaSAndroid Build Coastguard Worker
4967*f7c14bbaSAndroid Build Coastguard Worker if (obj->token_fd)
4968*f7c14bbaSAndroid Build Coastguard Worker return feat_supported(obj->feat_cache, feat_id);
4969*f7c14bbaSAndroid Build Coastguard Worker
4970*f7c14bbaSAndroid Build Coastguard Worker return feat_supported(NULL, feat_id);
4971*f7c14bbaSAndroid Build Coastguard Worker }
4972*f7c14bbaSAndroid Build Coastguard Worker
map_is_reuse_compat(const struct bpf_map * map,int map_fd)4973*f7c14bbaSAndroid Build Coastguard Worker static bool map_is_reuse_compat(const struct bpf_map *map, int map_fd)
4974*f7c14bbaSAndroid Build Coastguard Worker {
4975*f7c14bbaSAndroid Build Coastguard Worker struct bpf_map_info map_info;
4976*f7c14bbaSAndroid Build Coastguard Worker char msg[STRERR_BUFSIZE];
4977*f7c14bbaSAndroid Build Coastguard Worker __u32 map_info_len = sizeof(map_info);
4978*f7c14bbaSAndroid Build Coastguard Worker int err;
4979*f7c14bbaSAndroid Build Coastguard Worker
4980*f7c14bbaSAndroid Build Coastguard Worker memset(&map_info, 0, map_info_len);
4981*f7c14bbaSAndroid Build Coastguard Worker err = bpf_map_get_info_by_fd(map_fd, &map_info, &map_info_len);
4982*f7c14bbaSAndroid Build Coastguard Worker if (err && errno == EINVAL)
4983*f7c14bbaSAndroid Build Coastguard Worker err = bpf_get_map_info_from_fdinfo(map_fd, &map_info);
4984*f7c14bbaSAndroid Build Coastguard Worker if (err) {
4985*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to get map info for map FD %d: %s\n", map_fd,
4986*f7c14bbaSAndroid Build Coastguard Worker libbpf_strerror_r(errno, msg, sizeof(msg)));
4987*f7c14bbaSAndroid Build Coastguard Worker return false;
4988*f7c14bbaSAndroid Build Coastguard Worker }
4989*f7c14bbaSAndroid Build Coastguard Worker
4990*f7c14bbaSAndroid Build Coastguard Worker return (map_info.type == map->def.type &&
4991*f7c14bbaSAndroid Build Coastguard Worker map_info.key_size == map->def.key_size &&
4992*f7c14bbaSAndroid Build Coastguard Worker map_info.value_size == map->def.value_size &&
4993*f7c14bbaSAndroid Build Coastguard Worker map_info.max_entries == map->def.max_entries &&
4994*f7c14bbaSAndroid Build Coastguard Worker map_info.map_flags == map->def.map_flags &&
4995*f7c14bbaSAndroid Build Coastguard Worker map_info.map_extra == map->map_extra);
4996*f7c14bbaSAndroid Build Coastguard Worker }
4997*f7c14bbaSAndroid Build Coastguard Worker
4998*f7c14bbaSAndroid Build Coastguard Worker static int
bpf_object__reuse_map(struct bpf_map * map)4999*f7c14bbaSAndroid Build Coastguard Worker bpf_object__reuse_map(struct bpf_map *map)
5000*f7c14bbaSAndroid Build Coastguard Worker {
5001*f7c14bbaSAndroid Build Coastguard Worker char *cp, errmsg[STRERR_BUFSIZE];
5002*f7c14bbaSAndroid Build Coastguard Worker int err, pin_fd;
5003*f7c14bbaSAndroid Build Coastguard Worker
5004*f7c14bbaSAndroid Build Coastguard Worker pin_fd = bpf_obj_get(map->pin_path);
5005*f7c14bbaSAndroid Build Coastguard Worker if (pin_fd < 0) {
5006*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
5007*f7c14bbaSAndroid Build Coastguard Worker if (err == -ENOENT) {
5008*f7c14bbaSAndroid Build Coastguard Worker pr_debug("found no pinned map to reuse at '%s'\n",
5009*f7c14bbaSAndroid Build Coastguard Worker map->pin_path);
5010*f7c14bbaSAndroid Build Coastguard Worker return 0;
5011*f7c14bbaSAndroid Build Coastguard Worker }
5012*f7c14bbaSAndroid Build Coastguard Worker
5013*f7c14bbaSAndroid Build Coastguard Worker cp = libbpf_strerror_r(-err, errmsg, sizeof(errmsg));
5014*f7c14bbaSAndroid Build Coastguard Worker pr_warn("couldn't retrieve pinned map '%s': %s\n",
5015*f7c14bbaSAndroid Build Coastguard Worker map->pin_path, cp);
5016*f7c14bbaSAndroid Build Coastguard Worker return err;
5017*f7c14bbaSAndroid Build Coastguard Worker }
5018*f7c14bbaSAndroid Build Coastguard Worker
5019*f7c14bbaSAndroid Build Coastguard Worker if (!map_is_reuse_compat(map, pin_fd)) {
5020*f7c14bbaSAndroid Build Coastguard Worker pr_warn("couldn't reuse pinned map at '%s': parameter mismatch\n",
5021*f7c14bbaSAndroid Build Coastguard Worker map->pin_path);
5022*f7c14bbaSAndroid Build Coastguard Worker close(pin_fd);
5023*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
5024*f7c14bbaSAndroid Build Coastguard Worker }
5025*f7c14bbaSAndroid Build Coastguard Worker
5026*f7c14bbaSAndroid Build Coastguard Worker err = bpf_map__reuse_fd(map, pin_fd);
5027*f7c14bbaSAndroid Build Coastguard Worker close(pin_fd);
5028*f7c14bbaSAndroid Build Coastguard Worker if (err)
5029*f7c14bbaSAndroid Build Coastguard Worker return err;
5030*f7c14bbaSAndroid Build Coastguard Worker
5031*f7c14bbaSAndroid Build Coastguard Worker map->pinned = true;
5032*f7c14bbaSAndroid Build Coastguard Worker pr_debug("reused pinned map at '%s'\n", map->pin_path);
5033*f7c14bbaSAndroid Build Coastguard Worker
5034*f7c14bbaSAndroid Build Coastguard Worker return 0;
5035*f7c14bbaSAndroid Build Coastguard Worker }
5036*f7c14bbaSAndroid Build Coastguard Worker
5037*f7c14bbaSAndroid Build Coastguard Worker static int
bpf_object__populate_internal_map(struct bpf_object * obj,struct bpf_map * map)5038*f7c14bbaSAndroid Build Coastguard Worker bpf_object__populate_internal_map(struct bpf_object *obj, struct bpf_map *map)
5039*f7c14bbaSAndroid Build Coastguard Worker {
5040*f7c14bbaSAndroid Build Coastguard Worker enum libbpf_map_type map_type = map->libbpf_type;
5041*f7c14bbaSAndroid Build Coastguard Worker char *cp, errmsg[STRERR_BUFSIZE];
5042*f7c14bbaSAndroid Build Coastguard Worker int err, zero = 0;
5043*f7c14bbaSAndroid Build Coastguard Worker
5044*f7c14bbaSAndroid Build Coastguard Worker if (obj->gen_loader) {
5045*f7c14bbaSAndroid Build Coastguard Worker bpf_gen__map_update_elem(obj->gen_loader, map - obj->maps,
5046*f7c14bbaSAndroid Build Coastguard Worker map->mmaped, map->def.value_size);
5047*f7c14bbaSAndroid Build Coastguard Worker if (map_type == LIBBPF_MAP_RODATA || map_type == LIBBPF_MAP_KCONFIG)
5048*f7c14bbaSAndroid Build Coastguard Worker bpf_gen__map_freeze(obj->gen_loader, map - obj->maps);
5049*f7c14bbaSAndroid Build Coastguard Worker return 0;
5050*f7c14bbaSAndroid Build Coastguard Worker }
5051*f7c14bbaSAndroid Build Coastguard Worker
5052*f7c14bbaSAndroid Build Coastguard Worker err = bpf_map_update_elem(map->fd, &zero, map->mmaped, 0);
5053*f7c14bbaSAndroid Build Coastguard Worker if (err) {
5054*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
5055*f7c14bbaSAndroid Build Coastguard Worker cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg));
5056*f7c14bbaSAndroid Build Coastguard Worker pr_warn("Error setting initial map(%s) contents: %s\n",
5057*f7c14bbaSAndroid Build Coastguard Worker map->name, cp);
5058*f7c14bbaSAndroid Build Coastguard Worker return err;
5059*f7c14bbaSAndroid Build Coastguard Worker }
5060*f7c14bbaSAndroid Build Coastguard Worker
5061*f7c14bbaSAndroid Build Coastguard Worker /* Freeze .rodata and .kconfig map as read-only from syscall side. */
5062*f7c14bbaSAndroid Build Coastguard Worker if (map_type == LIBBPF_MAP_RODATA || map_type == LIBBPF_MAP_KCONFIG) {
5063*f7c14bbaSAndroid Build Coastguard Worker err = bpf_map_freeze(map->fd);
5064*f7c14bbaSAndroid Build Coastguard Worker if (err) {
5065*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
5066*f7c14bbaSAndroid Build Coastguard Worker cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg));
5067*f7c14bbaSAndroid Build Coastguard Worker pr_warn("Error freezing map(%s) as read-only: %s\n",
5068*f7c14bbaSAndroid Build Coastguard Worker map->name, cp);
5069*f7c14bbaSAndroid Build Coastguard Worker return err;
5070*f7c14bbaSAndroid Build Coastguard Worker }
5071*f7c14bbaSAndroid Build Coastguard Worker }
5072*f7c14bbaSAndroid Build Coastguard Worker return 0;
5073*f7c14bbaSAndroid Build Coastguard Worker }
5074*f7c14bbaSAndroid Build Coastguard Worker
5075*f7c14bbaSAndroid Build Coastguard Worker static void bpf_map__destroy(struct bpf_map *map);
5076*f7c14bbaSAndroid Build Coastguard Worker
map_is_created(const struct bpf_map * map)5077*f7c14bbaSAndroid Build Coastguard Worker static bool map_is_created(const struct bpf_map *map)
5078*f7c14bbaSAndroid Build Coastguard Worker {
5079*f7c14bbaSAndroid Build Coastguard Worker return map->obj->loaded || map->reused;
5080*f7c14bbaSAndroid Build Coastguard Worker }
5081*f7c14bbaSAndroid Build Coastguard Worker
bpf_object__create_map(struct bpf_object * obj,struct bpf_map * map,bool is_inner)5082*f7c14bbaSAndroid Build Coastguard Worker static int bpf_object__create_map(struct bpf_object *obj, struct bpf_map *map, bool is_inner)
5083*f7c14bbaSAndroid Build Coastguard Worker {
5084*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_OPTS(bpf_map_create_opts, create_attr);
5085*f7c14bbaSAndroid Build Coastguard Worker struct bpf_map_def *def = &map->def;
5086*f7c14bbaSAndroid Build Coastguard Worker const char *map_name = NULL;
5087*f7c14bbaSAndroid Build Coastguard Worker int err = 0, map_fd;
5088*f7c14bbaSAndroid Build Coastguard Worker
5089*f7c14bbaSAndroid Build Coastguard Worker if (kernel_supports(obj, FEAT_PROG_NAME))
5090*f7c14bbaSAndroid Build Coastguard Worker map_name = map->name;
5091*f7c14bbaSAndroid Build Coastguard Worker create_attr.map_ifindex = map->map_ifindex;
5092*f7c14bbaSAndroid Build Coastguard Worker create_attr.map_flags = def->map_flags;
5093*f7c14bbaSAndroid Build Coastguard Worker create_attr.numa_node = map->numa_node;
5094*f7c14bbaSAndroid Build Coastguard Worker create_attr.map_extra = map->map_extra;
5095*f7c14bbaSAndroid Build Coastguard Worker create_attr.token_fd = obj->token_fd;
5096*f7c14bbaSAndroid Build Coastguard Worker if (obj->token_fd)
5097*f7c14bbaSAndroid Build Coastguard Worker create_attr.map_flags |= BPF_F_TOKEN_FD;
5098*f7c14bbaSAndroid Build Coastguard Worker
5099*f7c14bbaSAndroid Build Coastguard Worker if (bpf_map__is_struct_ops(map)) {
5100*f7c14bbaSAndroid Build Coastguard Worker create_attr.btf_vmlinux_value_type_id = map->btf_vmlinux_value_type_id;
5101*f7c14bbaSAndroid Build Coastguard Worker if (map->mod_btf_fd >= 0) {
5102*f7c14bbaSAndroid Build Coastguard Worker create_attr.value_type_btf_obj_fd = map->mod_btf_fd;
5103*f7c14bbaSAndroid Build Coastguard Worker create_attr.map_flags |= BPF_F_VTYPE_BTF_OBJ_FD;
5104*f7c14bbaSAndroid Build Coastguard Worker }
5105*f7c14bbaSAndroid Build Coastguard Worker }
5106*f7c14bbaSAndroid Build Coastguard Worker
5107*f7c14bbaSAndroid Build Coastguard Worker if (obj->btf && btf__fd(obj->btf) >= 0) {
5108*f7c14bbaSAndroid Build Coastguard Worker create_attr.btf_fd = btf__fd(obj->btf);
5109*f7c14bbaSAndroid Build Coastguard Worker create_attr.btf_key_type_id = map->btf_key_type_id;
5110*f7c14bbaSAndroid Build Coastguard Worker create_attr.btf_value_type_id = map->btf_value_type_id;
5111*f7c14bbaSAndroid Build Coastguard Worker }
5112*f7c14bbaSAndroid Build Coastguard Worker
5113*f7c14bbaSAndroid Build Coastguard Worker if (bpf_map_type__is_map_in_map(def->type)) {
5114*f7c14bbaSAndroid Build Coastguard Worker if (map->inner_map) {
5115*f7c14bbaSAndroid Build Coastguard Worker err = map_set_def_max_entries(map->inner_map);
5116*f7c14bbaSAndroid Build Coastguard Worker if (err)
5117*f7c14bbaSAndroid Build Coastguard Worker return err;
5118*f7c14bbaSAndroid Build Coastguard Worker err = bpf_object__create_map(obj, map->inner_map, true);
5119*f7c14bbaSAndroid Build Coastguard Worker if (err) {
5120*f7c14bbaSAndroid Build Coastguard Worker pr_warn("map '%s': failed to create inner map: %d\n",
5121*f7c14bbaSAndroid Build Coastguard Worker map->name, err);
5122*f7c14bbaSAndroid Build Coastguard Worker return err;
5123*f7c14bbaSAndroid Build Coastguard Worker }
5124*f7c14bbaSAndroid Build Coastguard Worker map->inner_map_fd = map->inner_map->fd;
5125*f7c14bbaSAndroid Build Coastguard Worker }
5126*f7c14bbaSAndroid Build Coastguard Worker if (map->inner_map_fd >= 0)
5127*f7c14bbaSAndroid Build Coastguard Worker create_attr.inner_map_fd = map->inner_map_fd;
5128*f7c14bbaSAndroid Build Coastguard Worker }
5129*f7c14bbaSAndroid Build Coastguard Worker
5130*f7c14bbaSAndroid Build Coastguard Worker switch (def->type) {
5131*f7c14bbaSAndroid Build Coastguard Worker case BPF_MAP_TYPE_PERF_EVENT_ARRAY:
5132*f7c14bbaSAndroid Build Coastguard Worker case BPF_MAP_TYPE_CGROUP_ARRAY:
5133*f7c14bbaSAndroid Build Coastguard Worker case BPF_MAP_TYPE_STACK_TRACE:
5134*f7c14bbaSAndroid Build Coastguard Worker case BPF_MAP_TYPE_ARRAY_OF_MAPS:
5135*f7c14bbaSAndroid Build Coastguard Worker case BPF_MAP_TYPE_HASH_OF_MAPS:
5136*f7c14bbaSAndroid Build Coastguard Worker case BPF_MAP_TYPE_DEVMAP:
5137*f7c14bbaSAndroid Build Coastguard Worker case BPF_MAP_TYPE_DEVMAP_HASH:
5138*f7c14bbaSAndroid Build Coastguard Worker case BPF_MAP_TYPE_CPUMAP:
5139*f7c14bbaSAndroid Build Coastguard Worker case BPF_MAP_TYPE_XSKMAP:
5140*f7c14bbaSAndroid Build Coastguard Worker case BPF_MAP_TYPE_SOCKMAP:
5141*f7c14bbaSAndroid Build Coastguard Worker case BPF_MAP_TYPE_SOCKHASH:
5142*f7c14bbaSAndroid Build Coastguard Worker case BPF_MAP_TYPE_QUEUE:
5143*f7c14bbaSAndroid Build Coastguard Worker case BPF_MAP_TYPE_STACK:
5144*f7c14bbaSAndroid Build Coastguard Worker case BPF_MAP_TYPE_ARENA:
5145*f7c14bbaSAndroid Build Coastguard Worker create_attr.btf_fd = 0;
5146*f7c14bbaSAndroid Build Coastguard Worker create_attr.btf_key_type_id = 0;
5147*f7c14bbaSAndroid Build Coastguard Worker create_attr.btf_value_type_id = 0;
5148*f7c14bbaSAndroid Build Coastguard Worker map->btf_key_type_id = 0;
5149*f7c14bbaSAndroid Build Coastguard Worker map->btf_value_type_id = 0;
5150*f7c14bbaSAndroid Build Coastguard Worker break;
5151*f7c14bbaSAndroid Build Coastguard Worker case BPF_MAP_TYPE_STRUCT_OPS:
5152*f7c14bbaSAndroid Build Coastguard Worker create_attr.btf_value_type_id = 0;
5153*f7c14bbaSAndroid Build Coastguard Worker break;
5154*f7c14bbaSAndroid Build Coastguard Worker default:
5155*f7c14bbaSAndroid Build Coastguard Worker break;
5156*f7c14bbaSAndroid Build Coastguard Worker }
5157*f7c14bbaSAndroid Build Coastguard Worker
5158*f7c14bbaSAndroid Build Coastguard Worker if (obj->gen_loader) {
5159*f7c14bbaSAndroid Build Coastguard Worker bpf_gen__map_create(obj->gen_loader, def->type, map_name,
5160*f7c14bbaSAndroid Build Coastguard Worker def->key_size, def->value_size, def->max_entries,
5161*f7c14bbaSAndroid Build Coastguard Worker &create_attr, is_inner ? -1 : map - obj->maps);
5162*f7c14bbaSAndroid Build Coastguard Worker /* We keep pretenting we have valid FD to pass various fd >= 0
5163*f7c14bbaSAndroid Build Coastguard Worker * checks by just keeping original placeholder FDs in place.
5164*f7c14bbaSAndroid Build Coastguard Worker * See bpf_object__add_map() comment.
5165*f7c14bbaSAndroid Build Coastguard Worker * This placeholder fd will not be used with any syscall and
5166*f7c14bbaSAndroid Build Coastguard Worker * will be reset to -1 eventually.
5167*f7c14bbaSAndroid Build Coastguard Worker */
5168*f7c14bbaSAndroid Build Coastguard Worker map_fd = map->fd;
5169*f7c14bbaSAndroid Build Coastguard Worker } else {
5170*f7c14bbaSAndroid Build Coastguard Worker map_fd = bpf_map_create(def->type, map_name,
5171*f7c14bbaSAndroid Build Coastguard Worker def->key_size, def->value_size,
5172*f7c14bbaSAndroid Build Coastguard Worker def->max_entries, &create_attr);
5173*f7c14bbaSAndroid Build Coastguard Worker }
5174*f7c14bbaSAndroid Build Coastguard Worker if (map_fd < 0 && (create_attr.btf_key_type_id || create_attr.btf_value_type_id)) {
5175*f7c14bbaSAndroid Build Coastguard Worker char *cp, errmsg[STRERR_BUFSIZE];
5176*f7c14bbaSAndroid Build Coastguard Worker
5177*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
5178*f7c14bbaSAndroid Build Coastguard Worker cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg));
5179*f7c14bbaSAndroid Build Coastguard Worker pr_warn("Error in bpf_create_map_xattr(%s):%s(%d). Retrying without BTF.\n",
5180*f7c14bbaSAndroid Build Coastguard Worker map->name, cp, err);
5181*f7c14bbaSAndroid Build Coastguard Worker create_attr.btf_fd = 0;
5182*f7c14bbaSAndroid Build Coastguard Worker create_attr.btf_key_type_id = 0;
5183*f7c14bbaSAndroid Build Coastguard Worker create_attr.btf_value_type_id = 0;
5184*f7c14bbaSAndroid Build Coastguard Worker map->btf_key_type_id = 0;
5185*f7c14bbaSAndroid Build Coastguard Worker map->btf_value_type_id = 0;
5186*f7c14bbaSAndroid Build Coastguard Worker map_fd = bpf_map_create(def->type, map_name,
5187*f7c14bbaSAndroid Build Coastguard Worker def->key_size, def->value_size,
5188*f7c14bbaSAndroid Build Coastguard Worker def->max_entries, &create_attr);
5189*f7c14bbaSAndroid Build Coastguard Worker }
5190*f7c14bbaSAndroid Build Coastguard Worker
5191*f7c14bbaSAndroid Build Coastguard Worker if (bpf_map_type__is_map_in_map(def->type) && map->inner_map) {
5192*f7c14bbaSAndroid Build Coastguard Worker if (obj->gen_loader)
5193*f7c14bbaSAndroid Build Coastguard Worker map->inner_map->fd = -1;
5194*f7c14bbaSAndroid Build Coastguard Worker bpf_map__destroy(map->inner_map);
5195*f7c14bbaSAndroid Build Coastguard Worker zfree(&map->inner_map);
5196*f7c14bbaSAndroid Build Coastguard Worker }
5197*f7c14bbaSAndroid Build Coastguard Worker
5198*f7c14bbaSAndroid Build Coastguard Worker if (map_fd < 0)
5199*f7c14bbaSAndroid Build Coastguard Worker return map_fd;
5200*f7c14bbaSAndroid Build Coastguard Worker
5201*f7c14bbaSAndroid Build Coastguard Worker /* obj->gen_loader case, prevent reuse_fd() from closing map_fd */
5202*f7c14bbaSAndroid Build Coastguard Worker if (map->fd == map_fd)
5203*f7c14bbaSAndroid Build Coastguard Worker return 0;
5204*f7c14bbaSAndroid Build Coastguard Worker
5205*f7c14bbaSAndroid Build Coastguard Worker /* Keep placeholder FD value but now point it to the BPF map object.
5206*f7c14bbaSAndroid Build Coastguard Worker * This way everything that relied on this map's FD (e.g., relocated
5207*f7c14bbaSAndroid Build Coastguard Worker * ldimm64 instructions) will stay valid and won't need adjustments.
5208*f7c14bbaSAndroid Build Coastguard Worker * map->fd stays valid but now point to what map_fd points to.
5209*f7c14bbaSAndroid Build Coastguard Worker */
5210*f7c14bbaSAndroid Build Coastguard Worker return reuse_fd(map->fd, map_fd);
5211*f7c14bbaSAndroid Build Coastguard Worker }
5212*f7c14bbaSAndroid Build Coastguard Worker
init_map_in_map_slots(struct bpf_object * obj,struct bpf_map * map)5213*f7c14bbaSAndroid Build Coastguard Worker static int init_map_in_map_slots(struct bpf_object *obj, struct bpf_map *map)
5214*f7c14bbaSAndroid Build Coastguard Worker {
5215*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_map *targ_map;
5216*f7c14bbaSAndroid Build Coastguard Worker unsigned int i;
5217*f7c14bbaSAndroid Build Coastguard Worker int fd, err = 0;
5218*f7c14bbaSAndroid Build Coastguard Worker
5219*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < map->init_slots_sz; i++) {
5220*f7c14bbaSAndroid Build Coastguard Worker if (!map->init_slots[i])
5221*f7c14bbaSAndroid Build Coastguard Worker continue;
5222*f7c14bbaSAndroid Build Coastguard Worker
5223*f7c14bbaSAndroid Build Coastguard Worker targ_map = map->init_slots[i];
5224*f7c14bbaSAndroid Build Coastguard Worker fd = targ_map->fd;
5225*f7c14bbaSAndroid Build Coastguard Worker
5226*f7c14bbaSAndroid Build Coastguard Worker if (obj->gen_loader) {
5227*f7c14bbaSAndroid Build Coastguard Worker bpf_gen__populate_outer_map(obj->gen_loader,
5228*f7c14bbaSAndroid Build Coastguard Worker map - obj->maps, i,
5229*f7c14bbaSAndroid Build Coastguard Worker targ_map - obj->maps);
5230*f7c14bbaSAndroid Build Coastguard Worker } else {
5231*f7c14bbaSAndroid Build Coastguard Worker err = bpf_map_update_elem(map->fd, &i, &fd, 0);
5232*f7c14bbaSAndroid Build Coastguard Worker }
5233*f7c14bbaSAndroid Build Coastguard Worker if (err) {
5234*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
5235*f7c14bbaSAndroid Build Coastguard Worker pr_warn("map '%s': failed to initialize slot [%d] to map '%s' fd=%d: %d\n",
5236*f7c14bbaSAndroid Build Coastguard Worker map->name, i, targ_map->name, fd, err);
5237*f7c14bbaSAndroid Build Coastguard Worker return err;
5238*f7c14bbaSAndroid Build Coastguard Worker }
5239*f7c14bbaSAndroid Build Coastguard Worker pr_debug("map '%s': slot [%d] set to map '%s' fd=%d\n",
5240*f7c14bbaSAndroid Build Coastguard Worker map->name, i, targ_map->name, fd);
5241*f7c14bbaSAndroid Build Coastguard Worker }
5242*f7c14bbaSAndroid Build Coastguard Worker
5243*f7c14bbaSAndroid Build Coastguard Worker zfree(&map->init_slots);
5244*f7c14bbaSAndroid Build Coastguard Worker map->init_slots_sz = 0;
5245*f7c14bbaSAndroid Build Coastguard Worker
5246*f7c14bbaSAndroid Build Coastguard Worker return 0;
5247*f7c14bbaSAndroid Build Coastguard Worker }
5248*f7c14bbaSAndroid Build Coastguard Worker
init_prog_array_slots(struct bpf_object * obj,struct bpf_map * map)5249*f7c14bbaSAndroid Build Coastguard Worker static int init_prog_array_slots(struct bpf_object *obj, struct bpf_map *map)
5250*f7c14bbaSAndroid Build Coastguard Worker {
5251*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_program *targ_prog;
5252*f7c14bbaSAndroid Build Coastguard Worker unsigned int i;
5253*f7c14bbaSAndroid Build Coastguard Worker int fd, err;
5254*f7c14bbaSAndroid Build Coastguard Worker
5255*f7c14bbaSAndroid Build Coastguard Worker if (obj->gen_loader)
5256*f7c14bbaSAndroid Build Coastguard Worker return -ENOTSUP;
5257*f7c14bbaSAndroid Build Coastguard Worker
5258*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < map->init_slots_sz; i++) {
5259*f7c14bbaSAndroid Build Coastguard Worker if (!map->init_slots[i])
5260*f7c14bbaSAndroid Build Coastguard Worker continue;
5261*f7c14bbaSAndroid Build Coastguard Worker
5262*f7c14bbaSAndroid Build Coastguard Worker targ_prog = map->init_slots[i];
5263*f7c14bbaSAndroid Build Coastguard Worker fd = bpf_program__fd(targ_prog);
5264*f7c14bbaSAndroid Build Coastguard Worker
5265*f7c14bbaSAndroid Build Coastguard Worker err = bpf_map_update_elem(map->fd, &i, &fd, 0);
5266*f7c14bbaSAndroid Build Coastguard Worker if (err) {
5267*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
5268*f7c14bbaSAndroid Build Coastguard Worker pr_warn("map '%s': failed to initialize slot [%d] to prog '%s' fd=%d: %d\n",
5269*f7c14bbaSAndroid Build Coastguard Worker map->name, i, targ_prog->name, fd, err);
5270*f7c14bbaSAndroid Build Coastguard Worker return err;
5271*f7c14bbaSAndroid Build Coastguard Worker }
5272*f7c14bbaSAndroid Build Coastguard Worker pr_debug("map '%s': slot [%d] set to prog '%s' fd=%d\n",
5273*f7c14bbaSAndroid Build Coastguard Worker map->name, i, targ_prog->name, fd);
5274*f7c14bbaSAndroid Build Coastguard Worker }
5275*f7c14bbaSAndroid Build Coastguard Worker
5276*f7c14bbaSAndroid Build Coastguard Worker zfree(&map->init_slots);
5277*f7c14bbaSAndroid Build Coastguard Worker map->init_slots_sz = 0;
5278*f7c14bbaSAndroid Build Coastguard Worker
5279*f7c14bbaSAndroid Build Coastguard Worker return 0;
5280*f7c14bbaSAndroid Build Coastguard Worker }
5281*f7c14bbaSAndroid Build Coastguard Worker
bpf_object_init_prog_arrays(struct bpf_object * obj)5282*f7c14bbaSAndroid Build Coastguard Worker static int bpf_object_init_prog_arrays(struct bpf_object *obj)
5283*f7c14bbaSAndroid Build Coastguard Worker {
5284*f7c14bbaSAndroid Build Coastguard Worker struct bpf_map *map;
5285*f7c14bbaSAndroid Build Coastguard Worker int i, err;
5286*f7c14bbaSAndroid Build Coastguard Worker
5287*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < obj->nr_maps; i++) {
5288*f7c14bbaSAndroid Build Coastguard Worker map = &obj->maps[i];
5289*f7c14bbaSAndroid Build Coastguard Worker
5290*f7c14bbaSAndroid Build Coastguard Worker if (!map->init_slots_sz || map->def.type != BPF_MAP_TYPE_PROG_ARRAY)
5291*f7c14bbaSAndroid Build Coastguard Worker continue;
5292*f7c14bbaSAndroid Build Coastguard Worker
5293*f7c14bbaSAndroid Build Coastguard Worker err = init_prog_array_slots(obj, map);
5294*f7c14bbaSAndroid Build Coastguard Worker if (err < 0)
5295*f7c14bbaSAndroid Build Coastguard Worker return err;
5296*f7c14bbaSAndroid Build Coastguard Worker }
5297*f7c14bbaSAndroid Build Coastguard Worker return 0;
5298*f7c14bbaSAndroid Build Coastguard Worker }
5299*f7c14bbaSAndroid Build Coastguard Worker
map_set_def_max_entries(struct bpf_map * map)5300*f7c14bbaSAndroid Build Coastguard Worker static int map_set_def_max_entries(struct bpf_map *map)
5301*f7c14bbaSAndroid Build Coastguard Worker {
5302*f7c14bbaSAndroid Build Coastguard Worker if (map->def.type == BPF_MAP_TYPE_PERF_EVENT_ARRAY && !map->def.max_entries) {
5303*f7c14bbaSAndroid Build Coastguard Worker int nr_cpus;
5304*f7c14bbaSAndroid Build Coastguard Worker
5305*f7c14bbaSAndroid Build Coastguard Worker nr_cpus = libbpf_num_possible_cpus();
5306*f7c14bbaSAndroid Build Coastguard Worker if (nr_cpus < 0) {
5307*f7c14bbaSAndroid Build Coastguard Worker pr_warn("map '%s': failed to determine number of system CPUs: %d\n",
5308*f7c14bbaSAndroid Build Coastguard Worker map->name, nr_cpus);
5309*f7c14bbaSAndroid Build Coastguard Worker return nr_cpus;
5310*f7c14bbaSAndroid Build Coastguard Worker }
5311*f7c14bbaSAndroid Build Coastguard Worker pr_debug("map '%s': setting size to %d\n", map->name, nr_cpus);
5312*f7c14bbaSAndroid Build Coastguard Worker map->def.max_entries = nr_cpus;
5313*f7c14bbaSAndroid Build Coastguard Worker }
5314*f7c14bbaSAndroid Build Coastguard Worker
5315*f7c14bbaSAndroid Build Coastguard Worker return 0;
5316*f7c14bbaSAndroid Build Coastguard Worker }
5317*f7c14bbaSAndroid Build Coastguard Worker
5318*f7c14bbaSAndroid Build Coastguard Worker static int
bpf_object__create_maps(struct bpf_object * obj)5319*f7c14bbaSAndroid Build Coastguard Worker bpf_object__create_maps(struct bpf_object *obj)
5320*f7c14bbaSAndroid Build Coastguard Worker {
5321*f7c14bbaSAndroid Build Coastguard Worker struct bpf_map *map;
5322*f7c14bbaSAndroid Build Coastguard Worker char *cp, errmsg[STRERR_BUFSIZE];
5323*f7c14bbaSAndroid Build Coastguard Worker unsigned int i, j;
5324*f7c14bbaSAndroid Build Coastguard Worker int err;
5325*f7c14bbaSAndroid Build Coastguard Worker bool retried;
5326*f7c14bbaSAndroid Build Coastguard Worker
5327*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < obj->nr_maps; i++) {
5328*f7c14bbaSAndroid Build Coastguard Worker map = &obj->maps[i];
5329*f7c14bbaSAndroid Build Coastguard Worker
5330*f7c14bbaSAndroid Build Coastguard Worker /* To support old kernels, we skip creating global data maps
5331*f7c14bbaSAndroid Build Coastguard Worker * (.rodata, .data, .kconfig, etc); later on, during program
5332*f7c14bbaSAndroid Build Coastguard Worker * loading, if we detect that at least one of the to-be-loaded
5333*f7c14bbaSAndroid Build Coastguard Worker * programs is referencing any global data map, we'll error
5334*f7c14bbaSAndroid Build Coastguard Worker * out with program name and relocation index logged.
5335*f7c14bbaSAndroid Build Coastguard Worker * This approach allows to accommodate Clang emitting
5336*f7c14bbaSAndroid Build Coastguard Worker * unnecessary .rodata.str1.1 sections for string literals,
5337*f7c14bbaSAndroid Build Coastguard Worker * but also it allows to have CO-RE applications that use
5338*f7c14bbaSAndroid Build Coastguard Worker * global variables in some of BPF programs, but not others.
5339*f7c14bbaSAndroid Build Coastguard Worker * If those global variable-using programs are not loaded at
5340*f7c14bbaSAndroid Build Coastguard Worker * runtime due to bpf_program__set_autoload(prog, false),
5341*f7c14bbaSAndroid Build Coastguard Worker * bpf_object loading will succeed just fine even on old
5342*f7c14bbaSAndroid Build Coastguard Worker * kernels.
5343*f7c14bbaSAndroid Build Coastguard Worker */
5344*f7c14bbaSAndroid Build Coastguard Worker if (bpf_map__is_internal(map) && !kernel_supports(obj, FEAT_GLOBAL_DATA))
5345*f7c14bbaSAndroid Build Coastguard Worker map->autocreate = false;
5346*f7c14bbaSAndroid Build Coastguard Worker
5347*f7c14bbaSAndroid Build Coastguard Worker if (!map->autocreate) {
5348*f7c14bbaSAndroid Build Coastguard Worker pr_debug("map '%s': skipped auto-creating...\n", map->name);
5349*f7c14bbaSAndroid Build Coastguard Worker continue;
5350*f7c14bbaSAndroid Build Coastguard Worker }
5351*f7c14bbaSAndroid Build Coastguard Worker
5352*f7c14bbaSAndroid Build Coastguard Worker err = map_set_def_max_entries(map);
5353*f7c14bbaSAndroid Build Coastguard Worker if (err)
5354*f7c14bbaSAndroid Build Coastguard Worker goto err_out;
5355*f7c14bbaSAndroid Build Coastguard Worker
5356*f7c14bbaSAndroid Build Coastguard Worker retried = false;
5357*f7c14bbaSAndroid Build Coastguard Worker retry:
5358*f7c14bbaSAndroid Build Coastguard Worker if (map->pin_path) {
5359*f7c14bbaSAndroid Build Coastguard Worker err = bpf_object__reuse_map(map);
5360*f7c14bbaSAndroid Build Coastguard Worker if (err) {
5361*f7c14bbaSAndroid Build Coastguard Worker pr_warn("map '%s': error reusing pinned map\n",
5362*f7c14bbaSAndroid Build Coastguard Worker map->name);
5363*f7c14bbaSAndroid Build Coastguard Worker goto err_out;
5364*f7c14bbaSAndroid Build Coastguard Worker }
5365*f7c14bbaSAndroid Build Coastguard Worker if (retried && map->fd < 0) {
5366*f7c14bbaSAndroid Build Coastguard Worker pr_warn("map '%s': cannot find pinned map\n",
5367*f7c14bbaSAndroid Build Coastguard Worker map->name);
5368*f7c14bbaSAndroid Build Coastguard Worker err = -ENOENT;
5369*f7c14bbaSAndroid Build Coastguard Worker goto err_out;
5370*f7c14bbaSAndroid Build Coastguard Worker }
5371*f7c14bbaSAndroid Build Coastguard Worker }
5372*f7c14bbaSAndroid Build Coastguard Worker
5373*f7c14bbaSAndroid Build Coastguard Worker if (map->reused) {
5374*f7c14bbaSAndroid Build Coastguard Worker pr_debug("map '%s': skipping creation (preset fd=%d)\n",
5375*f7c14bbaSAndroid Build Coastguard Worker map->name, map->fd);
5376*f7c14bbaSAndroid Build Coastguard Worker } else {
5377*f7c14bbaSAndroid Build Coastguard Worker err = bpf_object__create_map(obj, map, false);
5378*f7c14bbaSAndroid Build Coastguard Worker if (err)
5379*f7c14bbaSAndroid Build Coastguard Worker goto err_out;
5380*f7c14bbaSAndroid Build Coastguard Worker
5381*f7c14bbaSAndroid Build Coastguard Worker pr_debug("map '%s': created successfully, fd=%d\n",
5382*f7c14bbaSAndroid Build Coastguard Worker map->name, map->fd);
5383*f7c14bbaSAndroid Build Coastguard Worker
5384*f7c14bbaSAndroid Build Coastguard Worker if (bpf_map__is_internal(map)) {
5385*f7c14bbaSAndroid Build Coastguard Worker err = bpf_object__populate_internal_map(obj, map);
5386*f7c14bbaSAndroid Build Coastguard Worker if (err < 0)
5387*f7c14bbaSAndroid Build Coastguard Worker goto err_out;
5388*f7c14bbaSAndroid Build Coastguard Worker }
5389*f7c14bbaSAndroid Build Coastguard Worker if (map->def.type == BPF_MAP_TYPE_ARENA) {
5390*f7c14bbaSAndroid Build Coastguard Worker map->mmaped = mmap((void *)(long)map->map_extra,
5391*f7c14bbaSAndroid Build Coastguard Worker bpf_map_mmap_sz(map), PROT_READ | PROT_WRITE,
5392*f7c14bbaSAndroid Build Coastguard Worker map->map_extra ? MAP_SHARED | MAP_FIXED : MAP_SHARED,
5393*f7c14bbaSAndroid Build Coastguard Worker map->fd, 0);
5394*f7c14bbaSAndroid Build Coastguard Worker if (map->mmaped == MAP_FAILED) {
5395*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
5396*f7c14bbaSAndroid Build Coastguard Worker map->mmaped = NULL;
5397*f7c14bbaSAndroid Build Coastguard Worker pr_warn("map '%s': failed to mmap arena: %d\n",
5398*f7c14bbaSAndroid Build Coastguard Worker map->name, err);
5399*f7c14bbaSAndroid Build Coastguard Worker return err;
5400*f7c14bbaSAndroid Build Coastguard Worker }
5401*f7c14bbaSAndroid Build Coastguard Worker if (obj->arena_data) {
5402*f7c14bbaSAndroid Build Coastguard Worker memcpy(map->mmaped, obj->arena_data, obj->arena_data_sz);
5403*f7c14bbaSAndroid Build Coastguard Worker zfree(&obj->arena_data);
5404*f7c14bbaSAndroid Build Coastguard Worker }
5405*f7c14bbaSAndroid Build Coastguard Worker }
5406*f7c14bbaSAndroid Build Coastguard Worker if (map->init_slots_sz && map->def.type != BPF_MAP_TYPE_PROG_ARRAY) {
5407*f7c14bbaSAndroid Build Coastguard Worker err = init_map_in_map_slots(obj, map);
5408*f7c14bbaSAndroid Build Coastguard Worker if (err < 0)
5409*f7c14bbaSAndroid Build Coastguard Worker goto err_out;
5410*f7c14bbaSAndroid Build Coastguard Worker }
5411*f7c14bbaSAndroid Build Coastguard Worker }
5412*f7c14bbaSAndroid Build Coastguard Worker
5413*f7c14bbaSAndroid Build Coastguard Worker if (map->pin_path && !map->pinned) {
5414*f7c14bbaSAndroid Build Coastguard Worker err = bpf_map__pin(map, NULL);
5415*f7c14bbaSAndroid Build Coastguard Worker if (err) {
5416*f7c14bbaSAndroid Build Coastguard Worker if (!retried && err == -EEXIST) {
5417*f7c14bbaSAndroid Build Coastguard Worker retried = true;
5418*f7c14bbaSAndroid Build Coastguard Worker goto retry;
5419*f7c14bbaSAndroid Build Coastguard Worker }
5420*f7c14bbaSAndroid Build Coastguard Worker pr_warn("map '%s': failed to auto-pin at '%s': %d\n",
5421*f7c14bbaSAndroid Build Coastguard Worker map->name, map->pin_path, err);
5422*f7c14bbaSAndroid Build Coastguard Worker goto err_out;
5423*f7c14bbaSAndroid Build Coastguard Worker }
5424*f7c14bbaSAndroid Build Coastguard Worker }
5425*f7c14bbaSAndroid Build Coastguard Worker }
5426*f7c14bbaSAndroid Build Coastguard Worker
5427*f7c14bbaSAndroid Build Coastguard Worker return 0;
5428*f7c14bbaSAndroid Build Coastguard Worker
5429*f7c14bbaSAndroid Build Coastguard Worker err_out:
5430*f7c14bbaSAndroid Build Coastguard Worker cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg));
5431*f7c14bbaSAndroid Build Coastguard Worker pr_warn("map '%s': failed to create: %s(%d)\n", map->name, cp, err);
5432*f7c14bbaSAndroid Build Coastguard Worker pr_perm_msg(err);
5433*f7c14bbaSAndroid Build Coastguard Worker for (j = 0; j < i; j++)
5434*f7c14bbaSAndroid Build Coastguard Worker zclose(obj->maps[j].fd);
5435*f7c14bbaSAndroid Build Coastguard Worker return err;
5436*f7c14bbaSAndroid Build Coastguard Worker }
5437*f7c14bbaSAndroid Build Coastguard Worker
bpf_core_is_flavor_sep(const char * s)5438*f7c14bbaSAndroid Build Coastguard Worker static bool bpf_core_is_flavor_sep(const char *s)
5439*f7c14bbaSAndroid Build Coastguard Worker {
5440*f7c14bbaSAndroid Build Coastguard Worker /* check X___Y name pattern, where X and Y are not underscores */
5441*f7c14bbaSAndroid Build Coastguard Worker return s[0] != '_' && /* X */
5442*f7c14bbaSAndroid Build Coastguard Worker s[1] == '_' && s[2] == '_' && s[3] == '_' && /* ___ */
5443*f7c14bbaSAndroid Build Coastguard Worker s[4] != '_'; /* Y */
5444*f7c14bbaSAndroid Build Coastguard Worker }
5445*f7c14bbaSAndroid Build Coastguard Worker
5446*f7c14bbaSAndroid Build Coastguard Worker /* Given 'some_struct_name___with_flavor' return the length of a name prefix
5447*f7c14bbaSAndroid Build Coastguard Worker * before last triple underscore. Struct name part after last triple
5448*f7c14bbaSAndroid Build Coastguard Worker * underscore is ignored by BPF CO-RE relocation during relocation matching.
5449*f7c14bbaSAndroid Build Coastguard Worker */
bpf_core_essential_name_len(const char * name)5450*f7c14bbaSAndroid Build Coastguard Worker size_t bpf_core_essential_name_len(const char *name)
5451*f7c14bbaSAndroid Build Coastguard Worker {
5452*f7c14bbaSAndroid Build Coastguard Worker size_t n = strlen(name);
5453*f7c14bbaSAndroid Build Coastguard Worker int i;
5454*f7c14bbaSAndroid Build Coastguard Worker
5455*f7c14bbaSAndroid Build Coastguard Worker for (i = n - 5; i >= 0; i--) {
5456*f7c14bbaSAndroid Build Coastguard Worker if (bpf_core_is_flavor_sep(name + i))
5457*f7c14bbaSAndroid Build Coastguard Worker return i + 1;
5458*f7c14bbaSAndroid Build Coastguard Worker }
5459*f7c14bbaSAndroid Build Coastguard Worker return n;
5460*f7c14bbaSAndroid Build Coastguard Worker }
5461*f7c14bbaSAndroid Build Coastguard Worker
bpf_core_free_cands(struct bpf_core_cand_list * cands)5462*f7c14bbaSAndroid Build Coastguard Worker void bpf_core_free_cands(struct bpf_core_cand_list *cands)
5463*f7c14bbaSAndroid Build Coastguard Worker {
5464*f7c14bbaSAndroid Build Coastguard Worker if (!cands)
5465*f7c14bbaSAndroid Build Coastguard Worker return;
5466*f7c14bbaSAndroid Build Coastguard Worker
5467*f7c14bbaSAndroid Build Coastguard Worker free(cands->cands);
5468*f7c14bbaSAndroid Build Coastguard Worker free(cands);
5469*f7c14bbaSAndroid Build Coastguard Worker }
5470*f7c14bbaSAndroid Build Coastguard Worker
bpf_core_add_cands(struct bpf_core_cand * local_cand,size_t local_essent_len,const struct btf * targ_btf,const char * targ_btf_name,int targ_start_id,struct bpf_core_cand_list * cands)5471*f7c14bbaSAndroid Build Coastguard Worker int bpf_core_add_cands(struct bpf_core_cand *local_cand,
5472*f7c14bbaSAndroid Build Coastguard Worker size_t local_essent_len,
5473*f7c14bbaSAndroid Build Coastguard Worker const struct btf *targ_btf,
5474*f7c14bbaSAndroid Build Coastguard Worker const char *targ_btf_name,
5475*f7c14bbaSAndroid Build Coastguard Worker int targ_start_id,
5476*f7c14bbaSAndroid Build Coastguard Worker struct bpf_core_cand_list *cands)
5477*f7c14bbaSAndroid Build Coastguard Worker {
5478*f7c14bbaSAndroid Build Coastguard Worker struct bpf_core_cand *new_cands, *cand;
5479*f7c14bbaSAndroid Build Coastguard Worker const struct btf_type *t, *local_t;
5480*f7c14bbaSAndroid Build Coastguard Worker const char *targ_name, *local_name;
5481*f7c14bbaSAndroid Build Coastguard Worker size_t targ_essent_len;
5482*f7c14bbaSAndroid Build Coastguard Worker int n, i;
5483*f7c14bbaSAndroid Build Coastguard Worker
5484*f7c14bbaSAndroid Build Coastguard Worker local_t = btf__type_by_id(local_cand->btf, local_cand->id);
5485*f7c14bbaSAndroid Build Coastguard Worker local_name = btf__str_by_offset(local_cand->btf, local_t->name_off);
5486*f7c14bbaSAndroid Build Coastguard Worker
5487*f7c14bbaSAndroid Build Coastguard Worker n = btf__type_cnt(targ_btf);
5488*f7c14bbaSAndroid Build Coastguard Worker for (i = targ_start_id; i < n; i++) {
5489*f7c14bbaSAndroid Build Coastguard Worker t = btf__type_by_id(targ_btf, i);
5490*f7c14bbaSAndroid Build Coastguard Worker if (!btf_kind_core_compat(t, local_t))
5491*f7c14bbaSAndroid Build Coastguard Worker continue;
5492*f7c14bbaSAndroid Build Coastguard Worker
5493*f7c14bbaSAndroid Build Coastguard Worker targ_name = btf__name_by_offset(targ_btf, t->name_off);
5494*f7c14bbaSAndroid Build Coastguard Worker if (str_is_empty(targ_name))
5495*f7c14bbaSAndroid Build Coastguard Worker continue;
5496*f7c14bbaSAndroid Build Coastguard Worker
5497*f7c14bbaSAndroid Build Coastguard Worker targ_essent_len = bpf_core_essential_name_len(targ_name);
5498*f7c14bbaSAndroid Build Coastguard Worker if (targ_essent_len != local_essent_len)
5499*f7c14bbaSAndroid Build Coastguard Worker continue;
5500*f7c14bbaSAndroid Build Coastguard Worker
5501*f7c14bbaSAndroid Build Coastguard Worker if (strncmp(local_name, targ_name, local_essent_len) != 0)
5502*f7c14bbaSAndroid Build Coastguard Worker continue;
5503*f7c14bbaSAndroid Build Coastguard Worker
5504*f7c14bbaSAndroid Build Coastguard Worker pr_debug("CO-RE relocating [%d] %s %s: found target candidate [%d] %s %s in [%s]\n",
5505*f7c14bbaSAndroid Build Coastguard Worker local_cand->id, btf_kind_str(local_t),
5506*f7c14bbaSAndroid Build Coastguard Worker local_name, i, btf_kind_str(t), targ_name,
5507*f7c14bbaSAndroid Build Coastguard Worker targ_btf_name);
5508*f7c14bbaSAndroid Build Coastguard Worker new_cands = libbpf_reallocarray(cands->cands, cands->len + 1,
5509*f7c14bbaSAndroid Build Coastguard Worker sizeof(*cands->cands));
5510*f7c14bbaSAndroid Build Coastguard Worker if (!new_cands)
5511*f7c14bbaSAndroid Build Coastguard Worker return -ENOMEM;
5512*f7c14bbaSAndroid Build Coastguard Worker
5513*f7c14bbaSAndroid Build Coastguard Worker cand = &new_cands[cands->len];
5514*f7c14bbaSAndroid Build Coastguard Worker cand->btf = targ_btf;
5515*f7c14bbaSAndroid Build Coastguard Worker cand->id = i;
5516*f7c14bbaSAndroid Build Coastguard Worker
5517*f7c14bbaSAndroid Build Coastguard Worker cands->cands = new_cands;
5518*f7c14bbaSAndroid Build Coastguard Worker cands->len++;
5519*f7c14bbaSAndroid Build Coastguard Worker }
5520*f7c14bbaSAndroid Build Coastguard Worker return 0;
5521*f7c14bbaSAndroid Build Coastguard Worker }
5522*f7c14bbaSAndroid Build Coastguard Worker
load_module_btfs(struct bpf_object * obj)5523*f7c14bbaSAndroid Build Coastguard Worker static int load_module_btfs(struct bpf_object *obj)
5524*f7c14bbaSAndroid Build Coastguard Worker {
5525*f7c14bbaSAndroid Build Coastguard Worker struct bpf_btf_info info;
5526*f7c14bbaSAndroid Build Coastguard Worker struct module_btf *mod_btf;
5527*f7c14bbaSAndroid Build Coastguard Worker struct btf *btf;
5528*f7c14bbaSAndroid Build Coastguard Worker char name[64];
5529*f7c14bbaSAndroid Build Coastguard Worker __u32 id = 0, len;
5530*f7c14bbaSAndroid Build Coastguard Worker int err, fd;
5531*f7c14bbaSAndroid Build Coastguard Worker
5532*f7c14bbaSAndroid Build Coastguard Worker if (obj->btf_modules_loaded)
5533*f7c14bbaSAndroid Build Coastguard Worker return 0;
5534*f7c14bbaSAndroid Build Coastguard Worker
5535*f7c14bbaSAndroid Build Coastguard Worker if (obj->gen_loader)
5536*f7c14bbaSAndroid Build Coastguard Worker return 0;
5537*f7c14bbaSAndroid Build Coastguard Worker
5538*f7c14bbaSAndroid Build Coastguard Worker /* don't do this again, even if we find no module BTFs */
5539*f7c14bbaSAndroid Build Coastguard Worker obj->btf_modules_loaded = true;
5540*f7c14bbaSAndroid Build Coastguard Worker
5541*f7c14bbaSAndroid Build Coastguard Worker /* kernel too old to support module BTFs */
5542*f7c14bbaSAndroid Build Coastguard Worker if (!kernel_supports(obj, FEAT_MODULE_BTF))
5543*f7c14bbaSAndroid Build Coastguard Worker return 0;
5544*f7c14bbaSAndroid Build Coastguard Worker
5545*f7c14bbaSAndroid Build Coastguard Worker while (true) {
5546*f7c14bbaSAndroid Build Coastguard Worker err = bpf_btf_get_next_id(id, &id);
5547*f7c14bbaSAndroid Build Coastguard Worker if (err && errno == ENOENT)
5548*f7c14bbaSAndroid Build Coastguard Worker return 0;
5549*f7c14bbaSAndroid Build Coastguard Worker if (err && errno == EPERM) {
5550*f7c14bbaSAndroid Build Coastguard Worker pr_debug("skipping module BTFs loading, missing privileges\n");
5551*f7c14bbaSAndroid Build Coastguard Worker return 0;
5552*f7c14bbaSAndroid Build Coastguard Worker }
5553*f7c14bbaSAndroid Build Coastguard Worker if (err) {
5554*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
5555*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to iterate BTF objects: %d\n", err);
5556*f7c14bbaSAndroid Build Coastguard Worker return err;
5557*f7c14bbaSAndroid Build Coastguard Worker }
5558*f7c14bbaSAndroid Build Coastguard Worker
5559*f7c14bbaSAndroid Build Coastguard Worker fd = bpf_btf_get_fd_by_id(id);
5560*f7c14bbaSAndroid Build Coastguard Worker if (fd < 0) {
5561*f7c14bbaSAndroid Build Coastguard Worker if (errno == ENOENT)
5562*f7c14bbaSAndroid Build Coastguard Worker continue; /* expected race: BTF was unloaded */
5563*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
5564*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to get BTF object #%d FD: %d\n", id, err);
5565*f7c14bbaSAndroid Build Coastguard Worker return err;
5566*f7c14bbaSAndroid Build Coastguard Worker }
5567*f7c14bbaSAndroid Build Coastguard Worker
5568*f7c14bbaSAndroid Build Coastguard Worker len = sizeof(info);
5569*f7c14bbaSAndroid Build Coastguard Worker memset(&info, 0, sizeof(info));
5570*f7c14bbaSAndroid Build Coastguard Worker info.name = ptr_to_u64(name);
5571*f7c14bbaSAndroid Build Coastguard Worker info.name_len = sizeof(name);
5572*f7c14bbaSAndroid Build Coastguard Worker
5573*f7c14bbaSAndroid Build Coastguard Worker err = bpf_btf_get_info_by_fd(fd, &info, &len);
5574*f7c14bbaSAndroid Build Coastguard Worker if (err) {
5575*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
5576*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to get BTF object #%d info: %d\n", id, err);
5577*f7c14bbaSAndroid Build Coastguard Worker goto err_out;
5578*f7c14bbaSAndroid Build Coastguard Worker }
5579*f7c14bbaSAndroid Build Coastguard Worker
5580*f7c14bbaSAndroid Build Coastguard Worker /* ignore non-module BTFs */
5581*f7c14bbaSAndroid Build Coastguard Worker if (!info.kernel_btf || strcmp(name, "vmlinux") == 0) {
5582*f7c14bbaSAndroid Build Coastguard Worker close(fd);
5583*f7c14bbaSAndroid Build Coastguard Worker continue;
5584*f7c14bbaSAndroid Build Coastguard Worker }
5585*f7c14bbaSAndroid Build Coastguard Worker
5586*f7c14bbaSAndroid Build Coastguard Worker btf = btf_get_from_fd(fd, obj->btf_vmlinux);
5587*f7c14bbaSAndroid Build Coastguard Worker err = libbpf_get_error(btf);
5588*f7c14bbaSAndroid Build Coastguard Worker if (err) {
5589*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to load module [%s]'s BTF object #%d: %d\n",
5590*f7c14bbaSAndroid Build Coastguard Worker name, id, err);
5591*f7c14bbaSAndroid Build Coastguard Worker goto err_out;
5592*f7c14bbaSAndroid Build Coastguard Worker }
5593*f7c14bbaSAndroid Build Coastguard Worker
5594*f7c14bbaSAndroid Build Coastguard Worker err = libbpf_ensure_mem((void **)&obj->btf_modules, &obj->btf_module_cap,
5595*f7c14bbaSAndroid Build Coastguard Worker sizeof(*obj->btf_modules), obj->btf_module_cnt + 1);
5596*f7c14bbaSAndroid Build Coastguard Worker if (err)
5597*f7c14bbaSAndroid Build Coastguard Worker goto err_out;
5598*f7c14bbaSAndroid Build Coastguard Worker
5599*f7c14bbaSAndroid Build Coastguard Worker mod_btf = &obj->btf_modules[obj->btf_module_cnt++];
5600*f7c14bbaSAndroid Build Coastguard Worker
5601*f7c14bbaSAndroid Build Coastguard Worker mod_btf->btf = btf;
5602*f7c14bbaSAndroid Build Coastguard Worker mod_btf->id = id;
5603*f7c14bbaSAndroid Build Coastguard Worker mod_btf->fd = fd;
5604*f7c14bbaSAndroid Build Coastguard Worker mod_btf->name = strdup(name);
5605*f7c14bbaSAndroid Build Coastguard Worker if (!mod_btf->name) {
5606*f7c14bbaSAndroid Build Coastguard Worker err = -ENOMEM;
5607*f7c14bbaSAndroid Build Coastguard Worker goto err_out;
5608*f7c14bbaSAndroid Build Coastguard Worker }
5609*f7c14bbaSAndroid Build Coastguard Worker continue;
5610*f7c14bbaSAndroid Build Coastguard Worker
5611*f7c14bbaSAndroid Build Coastguard Worker err_out:
5612*f7c14bbaSAndroid Build Coastguard Worker close(fd);
5613*f7c14bbaSAndroid Build Coastguard Worker return err;
5614*f7c14bbaSAndroid Build Coastguard Worker }
5615*f7c14bbaSAndroid Build Coastguard Worker
5616*f7c14bbaSAndroid Build Coastguard Worker return 0;
5617*f7c14bbaSAndroid Build Coastguard Worker }
5618*f7c14bbaSAndroid Build Coastguard Worker
5619*f7c14bbaSAndroid Build Coastguard Worker static struct bpf_core_cand_list *
bpf_core_find_cands(struct bpf_object * obj,const struct btf * local_btf,__u32 local_type_id)5620*f7c14bbaSAndroid Build Coastguard Worker bpf_core_find_cands(struct bpf_object *obj, const struct btf *local_btf, __u32 local_type_id)
5621*f7c14bbaSAndroid Build Coastguard Worker {
5622*f7c14bbaSAndroid Build Coastguard Worker struct bpf_core_cand local_cand = {};
5623*f7c14bbaSAndroid Build Coastguard Worker struct bpf_core_cand_list *cands;
5624*f7c14bbaSAndroid Build Coastguard Worker const struct btf *main_btf;
5625*f7c14bbaSAndroid Build Coastguard Worker const struct btf_type *local_t;
5626*f7c14bbaSAndroid Build Coastguard Worker const char *local_name;
5627*f7c14bbaSAndroid Build Coastguard Worker size_t local_essent_len;
5628*f7c14bbaSAndroid Build Coastguard Worker int err, i;
5629*f7c14bbaSAndroid Build Coastguard Worker
5630*f7c14bbaSAndroid Build Coastguard Worker local_cand.btf = local_btf;
5631*f7c14bbaSAndroid Build Coastguard Worker local_cand.id = local_type_id;
5632*f7c14bbaSAndroid Build Coastguard Worker local_t = btf__type_by_id(local_btf, local_type_id);
5633*f7c14bbaSAndroid Build Coastguard Worker if (!local_t)
5634*f7c14bbaSAndroid Build Coastguard Worker return ERR_PTR(-EINVAL);
5635*f7c14bbaSAndroid Build Coastguard Worker
5636*f7c14bbaSAndroid Build Coastguard Worker local_name = btf__name_by_offset(local_btf, local_t->name_off);
5637*f7c14bbaSAndroid Build Coastguard Worker if (str_is_empty(local_name))
5638*f7c14bbaSAndroid Build Coastguard Worker return ERR_PTR(-EINVAL);
5639*f7c14bbaSAndroid Build Coastguard Worker local_essent_len = bpf_core_essential_name_len(local_name);
5640*f7c14bbaSAndroid Build Coastguard Worker
5641*f7c14bbaSAndroid Build Coastguard Worker cands = calloc(1, sizeof(*cands));
5642*f7c14bbaSAndroid Build Coastguard Worker if (!cands)
5643*f7c14bbaSAndroid Build Coastguard Worker return ERR_PTR(-ENOMEM);
5644*f7c14bbaSAndroid Build Coastguard Worker
5645*f7c14bbaSAndroid Build Coastguard Worker /* Attempt to find target candidates in vmlinux BTF first */
5646*f7c14bbaSAndroid Build Coastguard Worker main_btf = obj->btf_vmlinux_override ?: obj->btf_vmlinux;
5647*f7c14bbaSAndroid Build Coastguard Worker err = bpf_core_add_cands(&local_cand, local_essent_len, main_btf, "vmlinux", 1, cands);
5648*f7c14bbaSAndroid Build Coastguard Worker if (err)
5649*f7c14bbaSAndroid Build Coastguard Worker goto err_out;
5650*f7c14bbaSAndroid Build Coastguard Worker
5651*f7c14bbaSAndroid Build Coastguard Worker /* if vmlinux BTF has any candidate, don't got for module BTFs */
5652*f7c14bbaSAndroid Build Coastguard Worker if (cands->len)
5653*f7c14bbaSAndroid Build Coastguard Worker return cands;
5654*f7c14bbaSAndroid Build Coastguard Worker
5655*f7c14bbaSAndroid Build Coastguard Worker /* if vmlinux BTF was overridden, don't attempt to load module BTFs */
5656*f7c14bbaSAndroid Build Coastguard Worker if (obj->btf_vmlinux_override)
5657*f7c14bbaSAndroid Build Coastguard Worker return cands;
5658*f7c14bbaSAndroid Build Coastguard Worker
5659*f7c14bbaSAndroid Build Coastguard Worker /* now look through module BTFs, trying to still find candidates */
5660*f7c14bbaSAndroid Build Coastguard Worker err = load_module_btfs(obj);
5661*f7c14bbaSAndroid Build Coastguard Worker if (err)
5662*f7c14bbaSAndroid Build Coastguard Worker goto err_out;
5663*f7c14bbaSAndroid Build Coastguard Worker
5664*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < obj->btf_module_cnt; i++) {
5665*f7c14bbaSAndroid Build Coastguard Worker err = bpf_core_add_cands(&local_cand, local_essent_len,
5666*f7c14bbaSAndroid Build Coastguard Worker obj->btf_modules[i].btf,
5667*f7c14bbaSAndroid Build Coastguard Worker obj->btf_modules[i].name,
5668*f7c14bbaSAndroid Build Coastguard Worker btf__type_cnt(obj->btf_vmlinux),
5669*f7c14bbaSAndroid Build Coastguard Worker cands);
5670*f7c14bbaSAndroid Build Coastguard Worker if (err)
5671*f7c14bbaSAndroid Build Coastguard Worker goto err_out;
5672*f7c14bbaSAndroid Build Coastguard Worker }
5673*f7c14bbaSAndroid Build Coastguard Worker
5674*f7c14bbaSAndroid Build Coastguard Worker return cands;
5675*f7c14bbaSAndroid Build Coastguard Worker err_out:
5676*f7c14bbaSAndroid Build Coastguard Worker bpf_core_free_cands(cands);
5677*f7c14bbaSAndroid Build Coastguard Worker return ERR_PTR(err);
5678*f7c14bbaSAndroid Build Coastguard Worker }
5679*f7c14bbaSAndroid Build Coastguard Worker
5680*f7c14bbaSAndroid Build Coastguard Worker /* Check local and target types for compatibility. This check is used for
5681*f7c14bbaSAndroid Build Coastguard Worker * type-based CO-RE relocations and follow slightly different rules than
5682*f7c14bbaSAndroid Build Coastguard Worker * field-based relocations. This function assumes that root types were already
5683*f7c14bbaSAndroid Build Coastguard Worker * checked for name match. Beyond that initial root-level name check, names
5684*f7c14bbaSAndroid Build Coastguard Worker * are completely ignored. Compatibility rules are as follows:
5685*f7c14bbaSAndroid Build Coastguard Worker * - any two STRUCTs/UNIONs/FWDs/ENUMs/INTs are considered compatible, but
5686*f7c14bbaSAndroid Build Coastguard Worker * kind should match for local and target types (i.e., STRUCT is not
5687*f7c14bbaSAndroid Build Coastguard Worker * compatible with UNION);
5688*f7c14bbaSAndroid Build Coastguard Worker * - for ENUMs, the size is ignored;
5689*f7c14bbaSAndroid Build Coastguard Worker * - for INT, size and signedness are ignored;
5690*f7c14bbaSAndroid Build Coastguard Worker * - for ARRAY, dimensionality is ignored, element types are checked for
5691*f7c14bbaSAndroid Build Coastguard Worker * compatibility recursively;
5692*f7c14bbaSAndroid Build Coastguard Worker * - CONST/VOLATILE/RESTRICT modifiers are ignored;
5693*f7c14bbaSAndroid Build Coastguard Worker * - TYPEDEFs/PTRs are compatible if types they pointing to are compatible;
5694*f7c14bbaSAndroid Build Coastguard Worker * - FUNC_PROTOs are compatible if they have compatible signature: same
5695*f7c14bbaSAndroid Build Coastguard Worker * number of input args and compatible return and argument types.
5696*f7c14bbaSAndroid Build Coastguard Worker * These rules are not set in stone and probably will be adjusted as we get
5697*f7c14bbaSAndroid Build Coastguard Worker * more experience with using BPF CO-RE relocations.
5698*f7c14bbaSAndroid Build Coastguard Worker */
bpf_core_types_are_compat(const struct btf * local_btf,__u32 local_id,const struct btf * targ_btf,__u32 targ_id)5699*f7c14bbaSAndroid Build Coastguard Worker int bpf_core_types_are_compat(const struct btf *local_btf, __u32 local_id,
5700*f7c14bbaSAndroid Build Coastguard Worker const struct btf *targ_btf, __u32 targ_id)
5701*f7c14bbaSAndroid Build Coastguard Worker {
5702*f7c14bbaSAndroid Build Coastguard Worker return __bpf_core_types_are_compat(local_btf, local_id, targ_btf, targ_id, 32);
5703*f7c14bbaSAndroid Build Coastguard Worker }
5704*f7c14bbaSAndroid Build Coastguard Worker
bpf_core_types_match(const struct btf * local_btf,__u32 local_id,const struct btf * targ_btf,__u32 targ_id)5705*f7c14bbaSAndroid Build Coastguard Worker int bpf_core_types_match(const struct btf *local_btf, __u32 local_id,
5706*f7c14bbaSAndroid Build Coastguard Worker const struct btf *targ_btf, __u32 targ_id)
5707*f7c14bbaSAndroid Build Coastguard Worker {
5708*f7c14bbaSAndroid Build Coastguard Worker return __bpf_core_types_match(local_btf, local_id, targ_btf, targ_id, false, 32);
5709*f7c14bbaSAndroid Build Coastguard Worker }
5710*f7c14bbaSAndroid Build Coastguard Worker
bpf_core_hash_fn(const long key,void * ctx)5711*f7c14bbaSAndroid Build Coastguard Worker static size_t bpf_core_hash_fn(const long key, void *ctx)
5712*f7c14bbaSAndroid Build Coastguard Worker {
5713*f7c14bbaSAndroid Build Coastguard Worker return key;
5714*f7c14bbaSAndroid Build Coastguard Worker }
5715*f7c14bbaSAndroid Build Coastguard Worker
bpf_core_equal_fn(const long k1,const long k2,void * ctx)5716*f7c14bbaSAndroid Build Coastguard Worker static bool bpf_core_equal_fn(const long k1, const long k2, void *ctx)
5717*f7c14bbaSAndroid Build Coastguard Worker {
5718*f7c14bbaSAndroid Build Coastguard Worker return k1 == k2;
5719*f7c14bbaSAndroid Build Coastguard Worker }
5720*f7c14bbaSAndroid Build Coastguard Worker
record_relo_core(struct bpf_program * prog,const struct bpf_core_relo * core_relo,int insn_idx)5721*f7c14bbaSAndroid Build Coastguard Worker static int record_relo_core(struct bpf_program *prog,
5722*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_core_relo *core_relo, int insn_idx)
5723*f7c14bbaSAndroid Build Coastguard Worker {
5724*f7c14bbaSAndroid Build Coastguard Worker struct reloc_desc *relos, *relo;
5725*f7c14bbaSAndroid Build Coastguard Worker
5726*f7c14bbaSAndroid Build Coastguard Worker relos = libbpf_reallocarray(prog->reloc_desc,
5727*f7c14bbaSAndroid Build Coastguard Worker prog->nr_reloc + 1, sizeof(*relos));
5728*f7c14bbaSAndroid Build Coastguard Worker if (!relos)
5729*f7c14bbaSAndroid Build Coastguard Worker return -ENOMEM;
5730*f7c14bbaSAndroid Build Coastguard Worker relo = &relos[prog->nr_reloc];
5731*f7c14bbaSAndroid Build Coastguard Worker relo->type = RELO_CORE;
5732*f7c14bbaSAndroid Build Coastguard Worker relo->insn_idx = insn_idx;
5733*f7c14bbaSAndroid Build Coastguard Worker relo->core_relo = core_relo;
5734*f7c14bbaSAndroid Build Coastguard Worker prog->reloc_desc = relos;
5735*f7c14bbaSAndroid Build Coastguard Worker prog->nr_reloc++;
5736*f7c14bbaSAndroid Build Coastguard Worker return 0;
5737*f7c14bbaSAndroid Build Coastguard Worker }
5738*f7c14bbaSAndroid Build Coastguard Worker
find_relo_core(struct bpf_program * prog,int insn_idx)5739*f7c14bbaSAndroid Build Coastguard Worker static const struct bpf_core_relo *find_relo_core(struct bpf_program *prog, int insn_idx)
5740*f7c14bbaSAndroid Build Coastguard Worker {
5741*f7c14bbaSAndroid Build Coastguard Worker struct reloc_desc *relo;
5742*f7c14bbaSAndroid Build Coastguard Worker int i;
5743*f7c14bbaSAndroid Build Coastguard Worker
5744*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < prog->nr_reloc; i++) {
5745*f7c14bbaSAndroid Build Coastguard Worker relo = &prog->reloc_desc[i];
5746*f7c14bbaSAndroid Build Coastguard Worker if (relo->type != RELO_CORE || relo->insn_idx != insn_idx)
5747*f7c14bbaSAndroid Build Coastguard Worker continue;
5748*f7c14bbaSAndroid Build Coastguard Worker
5749*f7c14bbaSAndroid Build Coastguard Worker return relo->core_relo;
5750*f7c14bbaSAndroid Build Coastguard Worker }
5751*f7c14bbaSAndroid Build Coastguard Worker
5752*f7c14bbaSAndroid Build Coastguard Worker return NULL;
5753*f7c14bbaSAndroid Build Coastguard Worker }
5754*f7c14bbaSAndroid Build Coastguard Worker
bpf_core_resolve_relo(struct bpf_program * prog,const struct bpf_core_relo * relo,int relo_idx,const struct btf * local_btf,struct hashmap * cand_cache,struct bpf_core_relo_res * targ_res)5755*f7c14bbaSAndroid Build Coastguard Worker static int bpf_core_resolve_relo(struct bpf_program *prog,
5756*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_core_relo *relo,
5757*f7c14bbaSAndroid Build Coastguard Worker int relo_idx,
5758*f7c14bbaSAndroid Build Coastguard Worker const struct btf *local_btf,
5759*f7c14bbaSAndroid Build Coastguard Worker struct hashmap *cand_cache,
5760*f7c14bbaSAndroid Build Coastguard Worker struct bpf_core_relo_res *targ_res)
5761*f7c14bbaSAndroid Build Coastguard Worker {
5762*f7c14bbaSAndroid Build Coastguard Worker struct bpf_core_spec specs_scratch[3] = {};
5763*f7c14bbaSAndroid Build Coastguard Worker struct bpf_core_cand_list *cands = NULL;
5764*f7c14bbaSAndroid Build Coastguard Worker const char *prog_name = prog->name;
5765*f7c14bbaSAndroid Build Coastguard Worker const struct btf_type *local_type;
5766*f7c14bbaSAndroid Build Coastguard Worker const char *local_name;
5767*f7c14bbaSAndroid Build Coastguard Worker __u32 local_id = relo->type_id;
5768*f7c14bbaSAndroid Build Coastguard Worker int err;
5769*f7c14bbaSAndroid Build Coastguard Worker
5770*f7c14bbaSAndroid Build Coastguard Worker local_type = btf__type_by_id(local_btf, local_id);
5771*f7c14bbaSAndroid Build Coastguard Worker if (!local_type)
5772*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
5773*f7c14bbaSAndroid Build Coastguard Worker
5774*f7c14bbaSAndroid Build Coastguard Worker local_name = btf__name_by_offset(local_btf, local_type->name_off);
5775*f7c14bbaSAndroid Build Coastguard Worker if (!local_name)
5776*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
5777*f7c14bbaSAndroid Build Coastguard Worker
5778*f7c14bbaSAndroid Build Coastguard Worker if (relo->kind != BPF_CORE_TYPE_ID_LOCAL &&
5779*f7c14bbaSAndroid Build Coastguard Worker !hashmap__find(cand_cache, local_id, &cands)) {
5780*f7c14bbaSAndroid Build Coastguard Worker cands = bpf_core_find_cands(prog->obj, local_btf, local_id);
5781*f7c14bbaSAndroid Build Coastguard Worker if (IS_ERR(cands)) {
5782*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': relo #%d: target candidate search failed for [%d] %s %s: %ld\n",
5783*f7c14bbaSAndroid Build Coastguard Worker prog_name, relo_idx, local_id, btf_kind_str(local_type),
5784*f7c14bbaSAndroid Build Coastguard Worker local_name, PTR_ERR(cands));
5785*f7c14bbaSAndroid Build Coastguard Worker return PTR_ERR(cands);
5786*f7c14bbaSAndroid Build Coastguard Worker }
5787*f7c14bbaSAndroid Build Coastguard Worker err = hashmap__set(cand_cache, local_id, cands, NULL, NULL);
5788*f7c14bbaSAndroid Build Coastguard Worker if (err) {
5789*f7c14bbaSAndroid Build Coastguard Worker bpf_core_free_cands(cands);
5790*f7c14bbaSAndroid Build Coastguard Worker return err;
5791*f7c14bbaSAndroid Build Coastguard Worker }
5792*f7c14bbaSAndroid Build Coastguard Worker }
5793*f7c14bbaSAndroid Build Coastguard Worker
5794*f7c14bbaSAndroid Build Coastguard Worker return bpf_core_calc_relo_insn(prog_name, relo, relo_idx, local_btf, cands, specs_scratch,
5795*f7c14bbaSAndroid Build Coastguard Worker targ_res);
5796*f7c14bbaSAndroid Build Coastguard Worker }
5797*f7c14bbaSAndroid Build Coastguard Worker
5798*f7c14bbaSAndroid Build Coastguard Worker static int
bpf_object__relocate_core(struct bpf_object * obj,const char * targ_btf_path)5799*f7c14bbaSAndroid Build Coastguard Worker bpf_object__relocate_core(struct bpf_object *obj, const char *targ_btf_path)
5800*f7c14bbaSAndroid Build Coastguard Worker {
5801*f7c14bbaSAndroid Build Coastguard Worker const struct btf_ext_info_sec *sec;
5802*f7c14bbaSAndroid Build Coastguard Worker struct bpf_core_relo_res targ_res;
5803*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_core_relo *rec;
5804*f7c14bbaSAndroid Build Coastguard Worker const struct btf_ext_info *seg;
5805*f7c14bbaSAndroid Build Coastguard Worker struct hashmap_entry *entry;
5806*f7c14bbaSAndroid Build Coastguard Worker struct hashmap *cand_cache = NULL;
5807*f7c14bbaSAndroid Build Coastguard Worker struct bpf_program *prog;
5808*f7c14bbaSAndroid Build Coastguard Worker struct bpf_insn *insn;
5809*f7c14bbaSAndroid Build Coastguard Worker const char *sec_name;
5810*f7c14bbaSAndroid Build Coastguard Worker int i, err = 0, insn_idx, sec_idx, sec_num;
5811*f7c14bbaSAndroid Build Coastguard Worker
5812*f7c14bbaSAndroid Build Coastguard Worker if (obj->btf_ext->core_relo_info.len == 0)
5813*f7c14bbaSAndroid Build Coastguard Worker return 0;
5814*f7c14bbaSAndroid Build Coastguard Worker
5815*f7c14bbaSAndroid Build Coastguard Worker if (targ_btf_path) {
5816*f7c14bbaSAndroid Build Coastguard Worker obj->btf_vmlinux_override = btf__parse(targ_btf_path, NULL);
5817*f7c14bbaSAndroid Build Coastguard Worker err = libbpf_get_error(obj->btf_vmlinux_override);
5818*f7c14bbaSAndroid Build Coastguard Worker if (err) {
5819*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to parse target BTF: %d\n", err);
5820*f7c14bbaSAndroid Build Coastguard Worker return err;
5821*f7c14bbaSAndroid Build Coastguard Worker }
5822*f7c14bbaSAndroid Build Coastguard Worker }
5823*f7c14bbaSAndroid Build Coastguard Worker
5824*f7c14bbaSAndroid Build Coastguard Worker cand_cache = hashmap__new(bpf_core_hash_fn, bpf_core_equal_fn, NULL);
5825*f7c14bbaSAndroid Build Coastguard Worker if (IS_ERR(cand_cache)) {
5826*f7c14bbaSAndroid Build Coastguard Worker err = PTR_ERR(cand_cache);
5827*f7c14bbaSAndroid Build Coastguard Worker goto out;
5828*f7c14bbaSAndroid Build Coastguard Worker }
5829*f7c14bbaSAndroid Build Coastguard Worker
5830*f7c14bbaSAndroid Build Coastguard Worker seg = &obj->btf_ext->core_relo_info;
5831*f7c14bbaSAndroid Build Coastguard Worker sec_num = 0;
5832*f7c14bbaSAndroid Build Coastguard Worker for_each_btf_ext_sec(seg, sec) {
5833*f7c14bbaSAndroid Build Coastguard Worker sec_idx = seg->sec_idxs[sec_num];
5834*f7c14bbaSAndroid Build Coastguard Worker sec_num++;
5835*f7c14bbaSAndroid Build Coastguard Worker
5836*f7c14bbaSAndroid Build Coastguard Worker sec_name = btf__name_by_offset(obj->btf, sec->sec_name_off);
5837*f7c14bbaSAndroid Build Coastguard Worker if (str_is_empty(sec_name)) {
5838*f7c14bbaSAndroid Build Coastguard Worker err = -EINVAL;
5839*f7c14bbaSAndroid Build Coastguard Worker goto out;
5840*f7c14bbaSAndroid Build Coastguard Worker }
5841*f7c14bbaSAndroid Build Coastguard Worker
5842*f7c14bbaSAndroid Build Coastguard Worker pr_debug("sec '%s': found %d CO-RE relocations\n", sec_name, sec->num_info);
5843*f7c14bbaSAndroid Build Coastguard Worker
5844*f7c14bbaSAndroid Build Coastguard Worker for_each_btf_ext_rec(seg, sec, i, rec) {
5845*f7c14bbaSAndroid Build Coastguard Worker if (rec->insn_off % BPF_INSN_SZ)
5846*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
5847*f7c14bbaSAndroid Build Coastguard Worker insn_idx = rec->insn_off / BPF_INSN_SZ;
5848*f7c14bbaSAndroid Build Coastguard Worker prog = find_prog_by_sec_insn(obj, sec_idx, insn_idx);
5849*f7c14bbaSAndroid Build Coastguard Worker if (!prog) {
5850*f7c14bbaSAndroid Build Coastguard Worker /* When __weak subprog is "overridden" by another instance
5851*f7c14bbaSAndroid Build Coastguard Worker * of the subprog from a different object file, linker still
5852*f7c14bbaSAndroid Build Coastguard Worker * appends all the .BTF.ext info that used to belong to that
5853*f7c14bbaSAndroid Build Coastguard Worker * eliminated subprogram.
5854*f7c14bbaSAndroid Build Coastguard Worker * This is similar to what x86-64 linker does for relocations.
5855*f7c14bbaSAndroid Build Coastguard Worker * So just ignore such relocations just like we ignore
5856*f7c14bbaSAndroid Build Coastguard Worker * subprog instructions when discovering subprograms.
5857*f7c14bbaSAndroid Build Coastguard Worker */
5858*f7c14bbaSAndroid Build Coastguard Worker pr_debug("sec '%s': skipping CO-RE relocation #%d for insn #%d belonging to eliminated weak subprogram\n",
5859*f7c14bbaSAndroid Build Coastguard Worker sec_name, i, insn_idx);
5860*f7c14bbaSAndroid Build Coastguard Worker continue;
5861*f7c14bbaSAndroid Build Coastguard Worker }
5862*f7c14bbaSAndroid Build Coastguard Worker /* no need to apply CO-RE relocation if the program is
5863*f7c14bbaSAndroid Build Coastguard Worker * not going to be loaded
5864*f7c14bbaSAndroid Build Coastguard Worker */
5865*f7c14bbaSAndroid Build Coastguard Worker if (!prog->autoload)
5866*f7c14bbaSAndroid Build Coastguard Worker continue;
5867*f7c14bbaSAndroid Build Coastguard Worker
5868*f7c14bbaSAndroid Build Coastguard Worker /* adjust insn_idx from section frame of reference to the local
5869*f7c14bbaSAndroid Build Coastguard Worker * program's frame of reference; (sub-)program code is not yet
5870*f7c14bbaSAndroid Build Coastguard Worker * relocated, so it's enough to just subtract in-section offset
5871*f7c14bbaSAndroid Build Coastguard Worker */
5872*f7c14bbaSAndroid Build Coastguard Worker insn_idx = insn_idx - prog->sec_insn_off;
5873*f7c14bbaSAndroid Build Coastguard Worker if (insn_idx >= prog->insns_cnt)
5874*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
5875*f7c14bbaSAndroid Build Coastguard Worker insn = &prog->insns[insn_idx];
5876*f7c14bbaSAndroid Build Coastguard Worker
5877*f7c14bbaSAndroid Build Coastguard Worker err = record_relo_core(prog, rec, insn_idx);
5878*f7c14bbaSAndroid Build Coastguard Worker if (err) {
5879*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': relo #%d: failed to record relocation: %d\n",
5880*f7c14bbaSAndroid Build Coastguard Worker prog->name, i, err);
5881*f7c14bbaSAndroid Build Coastguard Worker goto out;
5882*f7c14bbaSAndroid Build Coastguard Worker }
5883*f7c14bbaSAndroid Build Coastguard Worker
5884*f7c14bbaSAndroid Build Coastguard Worker if (prog->obj->gen_loader)
5885*f7c14bbaSAndroid Build Coastguard Worker continue;
5886*f7c14bbaSAndroid Build Coastguard Worker
5887*f7c14bbaSAndroid Build Coastguard Worker err = bpf_core_resolve_relo(prog, rec, i, obj->btf, cand_cache, &targ_res);
5888*f7c14bbaSAndroid Build Coastguard Worker if (err) {
5889*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': relo #%d: failed to relocate: %d\n",
5890*f7c14bbaSAndroid Build Coastguard Worker prog->name, i, err);
5891*f7c14bbaSAndroid Build Coastguard Worker goto out;
5892*f7c14bbaSAndroid Build Coastguard Worker }
5893*f7c14bbaSAndroid Build Coastguard Worker
5894*f7c14bbaSAndroid Build Coastguard Worker err = bpf_core_patch_insn(prog->name, insn, insn_idx, rec, i, &targ_res);
5895*f7c14bbaSAndroid Build Coastguard Worker if (err) {
5896*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': relo #%d: failed to patch insn #%u: %d\n",
5897*f7c14bbaSAndroid Build Coastguard Worker prog->name, i, insn_idx, err);
5898*f7c14bbaSAndroid Build Coastguard Worker goto out;
5899*f7c14bbaSAndroid Build Coastguard Worker }
5900*f7c14bbaSAndroid Build Coastguard Worker }
5901*f7c14bbaSAndroid Build Coastguard Worker }
5902*f7c14bbaSAndroid Build Coastguard Worker
5903*f7c14bbaSAndroid Build Coastguard Worker out:
5904*f7c14bbaSAndroid Build Coastguard Worker /* obj->btf_vmlinux and module BTFs are freed after object load */
5905*f7c14bbaSAndroid Build Coastguard Worker btf__free(obj->btf_vmlinux_override);
5906*f7c14bbaSAndroid Build Coastguard Worker obj->btf_vmlinux_override = NULL;
5907*f7c14bbaSAndroid Build Coastguard Worker
5908*f7c14bbaSAndroid Build Coastguard Worker if (!IS_ERR_OR_NULL(cand_cache)) {
5909*f7c14bbaSAndroid Build Coastguard Worker hashmap__for_each_entry(cand_cache, entry, i) {
5910*f7c14bbaSAndroid Build Coastguard Worker bpf_core_free_cands(entry->pvalue);
5911*f7c14bbaSAndroid Build Coastguard Worker }
5912*f7c14bbaSAndroid Build Coastguard Worker hashmap__free(cand_cache);
5913*f7c14bbaSAndroid Build Coastguard Worker }
5914*f7c14bbaSAndroid Build Coastguard Worker return err;
5915*f7c14bbaSAndroid Build Coastguard Worker }
5916*f7c14bbaSAndroid Build Coastguard Worker
5917*f7c14bbaSAndroid Build Coastguard Worker /* base map load ldimm64 special constant, used also for log fixup logic */
5918*f7c14bbaSAndroid Build Coastguard Worker #define POISON_LDIMM64_MAP_BASE 2001000000
5919*f7c14bbaSAndroid Build Coastguard Worker #define POISON_LDIMM64_MAP_PFX "200100"
5920*f7c14bbaSAndroid Build Coastguard Worker
poison_map_ldimm64(struct bpf_program * prog,int relo_idx,int insn_idx,struct bpf_insn * insn,int map_idx,const struct bpf_map * map)5921*f7c14bbaSAndroid Build Coastguard Worker static void poison_map_ldimm64(struct bpf_program *prog, int relo_idx,
5922*f7c14bbaSAndroid Build Coastguard Worker int insn_idx, struct bpf_insn *insn,
5923*f7c14bbaSAndroid Build Coastguard Worker int map_idx, const struct bpf_map *map)
5924*f7c14bbaSAndroid Build Coastguard Worker {
5925*f7c14bbaSAndroid Build Coastguard Worker int i;
5926*f7c14bbaSAndroid Build Coastguard Worker
5927*f7c14bbaSAndroid Build Coastguard Worker pr_debug("prog '%s': relo #%d: poisoning insn #%d that loads map #%d '%s'\n",
5928*f7c14bbaSAndroid Build Coastguard Worker prog->name, relo_idx, insn_idx, map_idx, map->name);
5929*f7c14bbaSAndroid Build Coastguard Worker
5930*f7c14bbaSAndroid Build Coastguard Worker /* we turn single ldimm64 into two identical invalid calls */
5931*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < 2; i++) {
5932*f7c14bbaSAndroid Build Coastguard Worker insn->code = BPF_JMP | BPF_CALL;
5933*f7c14bbaSAndroid Build Coastguard Worker insn->dst_reg = 0;
5934*f7c14bbaSAndroid Build Coastguard Worker insn->src_reg = 0;
5935*f7c14bbaSAndroid Build Coastguard Worker insn->off = 0;
5936*f7c14bbaSAndroid Build Coastguard Worker /* if this instruction is reachable (not a dead code),
5937*f7c14bbaSAndroid Build Coastguard Worker * verifier will complain with something like:
5938*f7c14bbaSAndroid Build Coastguard Worker * invalid func unknown#2001000123
5939*f7c14bbaSAndroid Build Coastguard Worker * where lower 123 is map index into obj->maps[] array
5940*f7c14bbaSAndroid Build Coastguard Worker */
5941*f7c14bbaSAndroid Build Coastguard Worker insn->imm = POISON_LDIMM64_MAP_BASE + map_idx;
5942*f7c14bbaSAndroid Build Coastguard Worker
5943*f7c14bbaSAndroid Build Coastguard Worker insn++;
5944*f7c14bbaSAndroid Build Coastguard Worker }
5945*f7c14bbaSAndroid Build Coastguard Worker }
5946*f7c14bbaSAndroid Build Coastguard Worker
5947*f7c14bbaSAndroid Build Coastguard Worker /* unresolved kfunc call special constant, used also for log fixup logic */
5948*f7c14bbaSAndroid Build Coastguard Worker #define POISON_CALL_KFUNC_BASE 2002000000
5949*f7c14bbaSAndroid Build Coastguard Worker #define POISON_CALL_KFUNC_PFX "2002"
5950*f7c14bbaSAndroid Build Coastguard Worker
poison_kfunc_call(struct bpf_program * prog,int relo_idx,int insn_idx,struct bpf_insn * insn,int ext_idx,const struct extern_desc * ext)5951*f7c14bbaSAndroid Build Coastguard Worker static void poison_kfunc_call(struct bpf_program *prog, int relo_idx,
5952*f7c14bbaSAndroid Build Coastguard Worker int insn_idx, struct bpf_insn *insn,
5953*f7c14bbaSAndroid Build Coastguard Worker int ext_idx, const struct extern_desc *ext)
5954*f7c14bbaSAndroid Build Coastguard Worker {
5955*f7c14bbaSAndroid Build Coastguard Worker pr_debug("prog '%s': relo #%d: poisoning insn #%d that calls kfunc '%s'\n",
5956*f7c14bbaSAndroid Build Coastguard Worker prog->name, relo_idx, insn_idx, ext->name);
5957*f7c14bbaSAndroid Build Coastguard Worker
5958*f7c14bbaSAndroid Build Coastguard Worker /* we turn kfunc call into invalid helper call with identifiable constant */
5959*f7c14bbaSAndroid Build Coastguard Worker insn->code = BPF_JMP | BPF_CALL;
5960*f7c14bbaSAndroid Build Coastguard Worker insn->dst_reg = 0;
5961*f7c14bbaSAndroid Build Coastguard Worker insn->src_reg = 0;
5962*f7c14bbaSAndroid Build Coastguard Worker insn->off = 0;
5963*f7c14bbaSAndroid Build Coastguard Worker /* if this instruction is reachable (not a dead code),
5964*f7c14bbaSAndroid Build Coastguard Worker * verifier will complain with something like:
5965*f7c14bbaSAndroid Build Coastguard Worker * invalid func unknown#2001000123
5966*f7c14bbaSAndroid Build Coastguard Worker * where lower 123 is extern index into obj->externs[] array
5967*f7c14bbaSAndroid Build Coastguard Worker */
5968*f7c14bbaSAndroid Build Coastguard Worker insn->imm = POISON_CALL_KFUNC_BASE + ext_idx;
5969*f7c14bbaSAndroid Build Coastguard Worker }
5970*f7c14bbaSAndroid Build Coastguard Worker
5971*f7c14bbaSAndroid Build Coastguard Worker /* Relocate data references within program code:
5972*f7c14bbaSAndroid Build Coastguard Worker * - map references;
5973*f7c14bbaSAndroid Build Coastguard Worker * - global variable references;
5974*f7c14bbaSAndroid Build Coastguard Worker * - extern references.
5975*f7c14bbaSAndroid Build Coastguard Worker */
5976*f7c14bbaSAndroid Build Coastguard Worker static int
bpf_object__relocate_data(struct bpf_object * obj,struct bpf_program * prog)5977*f7c14bbaSAndroid Build Coastguard Worker bpf_object__relocate_data(struct bpf_object *obj, struct bpf_program *prog)
5978*f7c14bbaSAndroid Build Coastguard Worker {
5979*f7c14bbaSAndroid Build Coastguard Worker int i;
5980*f7c14bbaSAndroid Build Coastguard Worker
5981*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < prog->nr_reloc; i++) {
5982*f7c14bbaSAndroid Build Coastguard Worker struct reloc_desc *relo = &prog->reloc_desc[i];
5983*f7c14bbaSAndroid Build Coastguard Worker struct bpf_insn *insn = &prog->insns[relo->insn_idx];
5984*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_map *map;
5985*f7c14bbaSAndroid Build Coastguard Worker struct extern_desc *ext;
5986*f7c14bbaSAndroid Build Coastguard Worker
5987*f7c14bbaSAndroid Build Coastguard Worker switch (relo->type) {
5988*f7c14bbaSAndroid Build Coastguard Worker case RELO_LD64:
5989*f7c14bbaSAndroid Build Coastguard Worker map = &obj->maps[relo->map_idx];
5990*f7c14bbaSAndroid Build Coastguard Worker if (obj->gen_loader) {
5991*f7c14bbaSAndroid Build Coastguard Worker insn[0].src_reg = BPF_PSEUDO_MAP_IDX;
5992*f7c14bbaSAndroid Build Coastguard Worker insn[0].imm = relo->map_idx;
5993*f7c14bbaSAndroid Build Coastguard Worker } else if (map->autocreate) {
5994*f7c14bbaSAndroid Build Coastguard Worker insn[0].src_reg = BPF_PSEUDO_MAP_FD;
5995*f7c14bbaSAndroid Build Coastguard Worker insn[0].imm = map->fd;
5996*f7c14bbaSAndroid Build Coastguard Worker } else {
5997*f7c14bbaSAndroid Build Coastguard Worker poison_map_ldimm64(prog, i, relo->insn_idx, insn,
5998*f7c14bbaSAndroid Build Coastguard Worker relo->map_idx, map);
5999*f7c14bbaSAndroid Build Coastguard Worker }
6000*f7c14bbaSAndroid Build Coastguard Worker break;
6001*f7c14bbaSAndroid Build Coastguard Worker case RELO_DATA:
6002*f7c14bbaSAndroid Build Coastguard Worker map = &obj->maps[relo->map_idx];
6003*f7c14bbaSAndroid Build Coastguard Worker insn[1].imm = insn[0].imm + relo->sym_off;
6004*f7c14bbaSAndroid Build Coastguard Worker if (obj->gen_loader) {
6005*f7c14bbaSAndroid Build Coastguard Worker insn[0].src_reg = BPF_PSEUDO_MAP_IDX_VALUE;
6006*f7c14bbaSAndroid Build Coastguard Worker insn[0].imm = relo->map_idx;
6007*f7c14bbaSAndroid Build Coastguard Worker } else if (map->autocreate) {
6008*f7c14bbaSAndroid Build Coastguard Worker insn[0].src_reg = BPF_PSEUDO_MAP_VALUE;
6009*f7c14bbaSAndroid Build Coastguard Worker insn[0].imm = map->fd;
6010*f7c14bbaSAndroid Build Coastguard Worker } else {
6011*f7c14bbaSAndroid Build Coastguard Worker poison_map_ldimm64(prog, i, relo->insn_idx, insn,
6012*f7c14bbaSAndroid Build Coastguard Worker relo->map_idx, map);
6013*f7c14bbaSAndroid Build Coastguard Worker }
6014*f7c14bbaSAndroid Build Coastguard Worker break;
6015*f7c14bbaSAndroid Build Coastguard Worker case RELO_EXTERN_LD64:
6016*f7c14bbaSAndroid Build Coastguard Worker ext = &obj->externs[relo->ext_idx];
6017*f7c14bbaSAndroid Build Coastguard Worker if (ext->type == EXT_KCFG) {
6018*f7c14bbaSAndroid Build Coastguard Worker if (obj->gen_loader) {
6019*f7c14bbaSAndroid Build Coastguard Worker insn[0].src_reg = BPF_PSEUDO_MAP_IDX_VALUE;
6020*f7c14bbaSAndroid Build Coastguard Worker insn[0].imm = obj->kconfig_map_idx;
6021*f7c14bbaSAndroid Build Coastguard Worker } else {
6022*f7c14bbaSAndroid Build Coastguard Worker insn[0].src_reg = BPF_PSEUDO_MAP_VALUE;
6023*f7c14bbaSAndroid Build Coastguard Worker insn[0].imm = obj->maps[obj->kconfig_map_idx].fd;
6024*f7c14bbaSAndroid Build Coastguard Worker }
6025*f7c14bbaSAndroid Build Coastguard Worker insn[1].imm = ext->kcfg.data_off;
6026*f7c14bbaSAndroid Build Coastguard Worker } else /* EXT_KSYM */ {
6027*f7c14bbaSAndroid Build Coastguard Worker if (ext->ksym.type_id && ext->is_set) { /* typed ksyms */
6028*f7c14bbaSAndroid Build Coastguard Worker insn[0].src_reg = BPF_PSEUDO_BTF_ID;
6029*f7c14bbaSAndroid Build Coastguard Worker insn[0].imm = ext->ksym.kernel_btf_id;
6030*f7c14bbaSAndroid Build Coastguard Worker insn[1].imm = ext->ksym.kernel_btf_obj_fd;
6031*f7c14bbaSAndroid Build Coastguard Worker } else { /* typeless ksyms or unresolved typed ksyms */
6032*f7c14bbaSAndroid Build Coastguard Worker insn[0].imm = (__u32)ext->ksym.addr;
6033*f7c14bbaSAndroid Build Coastguard Worker insn[1].imm = ext->ksym.addr >> 32;
6034*f7c14bbaSAndroid Build Coastguard Worker }
6035*f7c14bbaSAndroid Build Coastguard Worker }
6036*f7c14bbaSAndroid Build Coastguard Worker break;
6037*f7c14bbaSAndroid Build Coastguard Worker case RELO_EXTERN_CALL:
6038*f7c14bbaSAndroid Build Coastguard Worker ext = &obj->externs[relo->ext_idx];
6039*f7c14bbaSAndroid Build Coastguard Worker insn[0].src_reg = BPF_PSEUDO_KFUNC_CALL;
6040*f7c14bbaSAndroid Build Coastguard Worker if (ext->is_set) {
6041*f7c14bbaSAndroid Build Coastguard Worker insn[0].imm = ext->ksym.kernel_btf_id;
6042*f7c14bbaSAndroid Build Coastguard Worker insn[0].off = ext->ksym.btf_fd_idx;
6043*f7c14bbaSAndroid Build Coastguard Worker } else { /* unresolved weak kfunc call */
6044*f7c14bbaSAndroid Build Coastguard Worker poison_kfunc_call(prog, i, relo->insn_idx, insn,
6045*f7c14bbaSAndroid Build Coastguard Worker relo->ext_idx, ext);
6046*f7c14bbaSAndroid Build Coastguard Worker }
6047*f7c14bbaSAndroid Build Coastguard Worker break;
6048*f7c14bbaSAndroid Build Coastguard Worker case RELO_SUBPROG_ADDR:
6049*f7c14bbaSAndroid Build Coastguard Worker if (insn[0].src_reg != BPF_PSEUDO_FUNC) {
6050*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': relo #%d: bad insn\n",
6051*f7c14bbaSAndroid Build Coastguard Worker prog->name, i);
6052*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
6053*f7c14bbaSAndroid Build Coastguard Worker }
6054*f7c14bbaSAndroid Build Coastguard Worker /* handled already */
6055*f7c14bbaSAndroid Build Coastguard Worker break;
6056*f7c14bbaSAndroid Build Coastguard Worker case RELO_CALL:
6057*f7c14bbaSAndroid Build Coastguard Worker /* handled already */
6058*f7c14bbaSAndroid Build Coastguard Worker break;
6059*f7c14bbaSAndroid Build Coastguard Worker case RELO_CORE:
6060*f7c14bbaSAndroid Build Coastguard Worker /* will be handled by bpf_program_record_relos() */
6061*f7c14bbaSAndroid Build Coastguard Worker break;
6062*f7c14bbaSAndroid Build Coastguard Worker default:
6063*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': relo #%d: bad relo type %d\n",
6064*f7c14bbaSAndroid Build Coastguard Worker prog->name, i, relo->type);
6065*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
6066*f7c14bbaSAndroid Build Coastguard Worker }
6067*f7c14bbaSAndroid Build Coastguard Worker }
6068*f7c14bbaSAndroid Build Coastguard Worker
6069*f7c14bbaSAndroid Build Coastguard Worker return 0;
6070*f7c14bbaSAndroid Build Coastguard Worker }
6071*f7c14bbaSAndroid Build Coastguard Worker
adjust_prog_btf_ext_info(const struct bpf_object * obj,const struct bpf_program * prog,const struct btf_ext_info * ext_info,void ** prog_info,__u32 * prog_rec_cnt,__u32 * prog_rec_sz)6072*f7c14bbaSAndroid Build Coastguard Worker static int adjust_prog_btf_ext_info(const struct bpf_object *obj,
6073*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_program *prog,
6074*f7c14bbaSAndroid Build Coastguard Worker const struct btf_ext_info *ext_info,
6075*f7c14bbaSAndroid Build Coastguard Worker void **prog_info, __u32 *prog_rec_cnt,
6076*f7c14bbaSAndroid Build Coastguard Worker __u32 *prog_rec_sz)
6077*f7c14bbaSAndroid Build Coastguard Worker {
6078*f7c14bbaSAndroid Build Coastguard Worker void *copy_start = NULL, *copy_end = NULL;
6079*f7c14bbaSAndroid Build Coastguard Worker void *rec, *rec_end, *new_prog_info;
6080*f7c14bbaSAndroid Build Coastguard Worker const struct btf_ext_info_sec *sec;
6081*f7c14bbaSAndroid Build Coastguard Worker size_t old_sz, new_sz;
6082*f7c14bbaSAndroid Build Coastguard Worker int i, sec_num, sec_idx, off_adj;
6083*f7c14bbaSAndroid Build Coastguard Worker
6084*f7c14bbaSAndroid Build Coastguard Worker sec_num = 0;
6085*f7c14bbaSAndroid Build Coastguard Worker for_each_btf_ext_sec(ext_info, sec) {
6086*f7c14bbaSAndroid Build Coastguard Worker sec_idx = ext_info->sec_idxs[sec_num];
6087*f7c14bbaSAndroid Build Coastguard Worker sec_num++;
6088*f7c14bbaSAndroid Build Coastguard Worker if (prog->sec_idx != sec_idx)
6089*f7c14bbaSAndroid Build Coastguard Worker continue;
6090*f7c14bbaSAndroid Build Coastguard Worker
6091*f7c14bbaSAndroid Build Coastguard Worker for_each_btf_ext_rec(ext_info, sec, i, rec) {
6092*f7c14bbaSAndroid Build Coastguard Worker __u32 insn_off = *(__u32 *)rec / BPF_INSN_SZ;
6093*f7c14bbaSAndroid Build Coastguard Worker
6094*f7c14bbaSAndroid Build Coastguard Worker if (insn_off < prog->sec_insn_off)
6095*f7c14bbaSAndroid Build Coastguard Worker continue;
6096*f7c14bbaSAndroid Build Coastguard Worker if (insn_off >= prog->sec_insn_off + prog->sec_insn_cnt)
6097*f7c14bbaSAndroid Build Coastguard Worker break;
6098*f7c14bbaSAndroid Build Coastguard Worker
6099*f7c14bbaSAndroid Build Coastguard Worker if (!copy_start)
6100*f7c14bbaSAndroid Build Coastguard Worker copy_start = rec;
6101*f7c14bbaSAndroid Build Coastguard Worker copy_end = rec + ext_info->rec_size;
6102*f7c14bbaSAndroid Build Coastguard Worker }
6103*f7c14bbaSAndroid Build Coastguard Worker
6104*f7c14bbaSAndroid Build Coastguard Worker if (!copy_start)
6105*f7c14bbaSAndroid Build Coastguard Worker return -ENOENT;
6106*f7c14bbaSAndroid Build Coastguard Worker
6107*f7c14bbaSAndroid Build Coastguard Worker /* append func/line info of a given (sub-)program to the main
6108*f7c14bbaSAndroid Build Coastguard Worker * program func/line info
6109*f7c14bbaSAndroid Build Coastguard Worker */
6110*f7c14bbaSAndroid Build Coastguard Worker old_sz = (size_t)(*prog_rec_cnt) * ext_info->rec_size;
6111*f7c14bbaSAndroid Build Coastguard Worker new_sz = old_sz + (copy_end - copy_start);
6112*f7c14bbaSAndroid Build Coastguard Worker new_prog_info = realloc(*prog_info, new_sz);
6113*f7c14bbaSAndroid Build Coastguard Worker if (!new_prog_info)
6114*f7c14bbaSAndroid Build Coastguard Worker return -ENOMEM;
6115*f7c14bbaSAndroid Build Coastguard Worker *prog_info = new_prog_info;
6116*f7c14bbaSAndroid Build Coastguard Worker *prog_rec_cnt = new_sz / ext_info->rec_size;
6117*f7c14bbaSAndroid Build Coastguard Worker memcpy(new_prog_info + old_sz, copy_start, copy_end - copy_start);
6118*f7c14bbaSAndroid Build Coastguard Worker
6119*f7c14bbaSAndroid Build Coastguard Worker /* Kernel instruction offsets are in units of 8-byte
6120*f7c14bbaSAndroid Build Coastguard Worker * instructions, while .BTF.ext instruction offsets generated
6121*f7c14bbaSAndroid Build Coastguard Worker * by Clang are in units of bytes. So convert Clang offsets
6122*f7c14bbaSAndroid Build Coastguard Worker * into kernel offsets and adjust offset according to program
6123*f7c14bbaSAndroid Build Coastguard Worker * relocated position.
6124*f7c14bbaSAndroid Build Coastguard Worker */
6125*f7c14bbaSAndroid Build Coastguard Worker off_adj = prog->sub_insn_off - prog->sec_insn_off;
6126*f7c14bbaSAndroid Build Coastguard Worker rec = new_prog_info + old_sz;
6127*f7c14bbaSAndroid Build Coastguard Worker rec_end = new_prog_info + new_sz;
6128*f7c14bbaSAndroid Build Coastguard Worker for (; rec < rec_end; rec += ext_info->rec_size) {
6129*f7c14bbaSAndroid Build Coastguard Worker __u32 *insn_off = rec;
6130*f7c14bbaSAndroid Build Coastguard Worker
6131*f7c14bbaSAndroid Build Coastguard Worker *insn_off = *insn_off / BPF_INSN_SZ + off_adj;
6132*f7c14bbaSAndroid Build Coastguard Worker }
6133*f7c14bbaSAndroid Build Coastguard Worker *prog_rec_sz = ext_info->rec_size;
6134*f7c14bbaSAndroid Build Coastguard Worker return 0;
6135*f7c14bbaSAndroid Build Coastguard Worker }
6136*f7c14bbaSAndroid Build Coastguard Worker
6137*f7c14bbaSAndroid Build Coastguard Worker return -ENOENT;
6138*f7c14bbaSAndroid Build Coastguard Worker }
6139*f7c14bbaSAndroid Build Coastguard Worker
6140*f7c14bbaSAndroid Build Coastguard Worker static int
reloc_prog_func_and_line_info(const struct bpf_object * obj,struct bpf_program * main_prog,const struct bpf_program * prog)6141*f7c14bbaSAndroid Build Coastguard Worker reloc_prog_func_and_line_info(const struct bpf_object *obj,
6142*f7c14bbaSAndroid Build Coastguard Worker struct bpf_program *main_prog,
6143*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_program *prog)
6144*f7c14bbaSAndroid Build Coastguard Worker {
6145*f7c14bbaSAndroid Build Coastguard Worker int err;
6146*f7c14bbaSAndroid Build Coastguard Worker
6147*f7c14bbaSAndroid Build Coastguard Worker /* no .BTF.ext relocation if .BTF.ext is missing or kernel doesn't
6148*f7c14bbaSAndroid Build Coastguard Worker * support func/line info
6149*f7c14bbaSAndroid Build Coastguard Worker */
6150*f7c14bbaSAndroid Build Coastguard Worker if (!obj->btf_ext || !kernel_supports(obj, FEAT_BTF_FUNC))
6151*f7c14bbaSAndroid Build Coastguard Worker return 0;
6152*f7c14bbaSAndroid Build Coastguard Worker
6153*f7c14bbaSAndroid Build Coastguard Worker /* only attempt func info relocation if main program's func_info
6154*f7c14bbaSAndroid Build Coastguard Worker * relocation was successful
6155*f7c14bbaSAndroid Build Coastguard Worker */
6156*f7c14bbaSAndroid Build Coastguard Worker if (main_prog != prog && !main_prog->func_info)
6157*f7c14bbaSAndroid Build Coastguard Worker goto line_info;
6158*f7c14bbaSAndroid Build Coastguard Worker
6159*f7c14bbaSAndroid Build Coastguard Worker err = adjust_prog_btf_ext_info(obj, prog, &obj->btf_ext->func_info,
6160*f7c14bbaSAndroid Build Coastguard Worker &main_prog->func_info,
6161*f7c14bbaSAndroid Build Coastguard Worker &main_prog->func_info_cnt,
6162*f7c14bbaSAndroid Build Coastguard Worker &main_prog->func_info_rec_size);
6163*f7c14bbaSAndroid Build Coastguard Worker if (err) {
6164*f7c14bbaSAndroid Build Coastguard Worker if (err != -ENOENT) {
6165*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': error relocating .BTF.ext function info: %d\n",
6166*f7c14bbaSAndroid Build Coastguard Worker prog->name, err);
6167*f7c14bbaSAndroid Build Coastguard Worker return err;
6168*f7c14bbaSAndroid Build Coastguard Worker }
6169*f7c14bbaSAndroid Build Coastguard Worker if (main_prog->func_info) {
6170*f7c14bbaSAndroid Build Coastguard Worker /*
6171*f7c14bbaSAndroid Build Coastguard Worker * Some info has already been found but has problem
6172*f7c14bbaSAndroid Build Coastguard Worker * in the last btf_ext reloc. Must have to error out.
6173*f7c14bbaSAndroid Build Coastguard Worker */
6174*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': missing .BTF.ext function info.\n", prog->name);
6175*f7c14bbaSAndroid Build Coastguard Worker return err;
6176*f7c14bbaSAndroid Build Coastguard Worker }
6177*f7c14bbaSAndroid Build Coastguard Worker /* Have problem loading the very first info. Ignore the rest. */
6178*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': missing .BTF.ext function info for the main program, skipping all of .BTF.ext func info.\n",
6179*f7c14bbaSAndroid Build Coastguard Worker prog->name);
6180*f7c14bbaSAndroid Build Coastguard Worker }
6181*f7c14bbaSAndroid Build Coastguard Worker
6182*f7c14bbaSAndroid Build Coastguard Worker line_info:
6183*f7c14bbaSAndroid Build Coastguard Worker /* don't relocate line info if main program's relocation failed */
6184*f7c14bbaSAndroid Build Coastguard Worker if (main_prog != prog && !main_prog->line_info)
6185*f7c14bbaSAndroid Build Coastguard Worker return 0;
6186*f7c14bbaSAndroid Build Coastguard Worker
6187*f7c14bbaSAndroid Build Coastguard Worker err = adjust_prog_btf_ext_info(obj, prog, &obj->btf_ext->line_info,
6188*f7c14bbaSAndroid Build Coastguard Worker &main_prog->line_info,
6189*f7c14bbaSAndroid Build Coastguard Worker &main_prog->line_info_cnt,
6190*f7c14bbaSAndroid Build Coastguard Worker &main_prog->line_info_rec_size);
6191*f7c14bbaSAndroid Build Coastguard Worker if (err) {
6192*f7c14bbaSAndroid Build Coastguard Worker if (err != -ENOENT) {
6193*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': error relocating .BTF.ext line info: %d\n",
6194*f7c14bbaSAndroid Build Coastguard Worker prog->name, err);
6195*f7c14bbaSAndroid Build Coastguard Worker return err;
6196*f7c14bbaSAndroid Build Coastguard Worker }
6197*f7c14bbaSAndroid Build Coastguard Worker if (main_prog->line_info) {
6198*f7c14bbaSAndroid Build Coastguard Worker /*
6199*f7c14bbaSAndroid Build Coastguard Worker * Some info has already been found but has problem
6200*f7c14bbaSAndroid Build Coastguard Worker * in the last btf_ext reloc. Must have to error out.
6201*f7c14bbaSAndroid Build Coastguard Worker */
6202*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': missing .BTF.ext line info.\n", prog->name);
6203*f7c14bbaSAndroid Build Coastguard Worker return err;
6204*f7c14bbaSAndroid Build Coastguard Worker }
6205*f7c14bbaSAndroid Build Coastguard Worker /* Have problem loading the very first info. Ignore the rest. */
6206*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': missing .BTF.ext line info for the main program, skipping all of .BTF.ext line info.\n",
6207*f7c14bbaSAndroid Build Coastguard Worker prog->name);
6208*f7c14bbaSAndroid Build Coastguard Worker }
6209*f7c14bbaSAndroid Build Coastguard Worker return 0;
6210*f7c14bbaSAndroid Build Coastguard Worker }
6211*f7c14bbaSAndroid Build Coastguard Worker
cmp_relo_by_insn_idx(const void * key,const void * elem)6212*f7c14bbaSAndroid Build Coastguard Worker static int cmp_relo_by_insn_idx(const void *key, const void *elem)
6213*f7c14bbaSAndroid Build Coastguard Worker {
6214*f7c14bbaSAndroid Build Coastguard Worker size_t insn_idx = *(const size_t *)key;
6215*f7c14bbaSAndroid Build Coastguard Worker const struct reloc_desc *relo = elem;
6216*f7c14bbaSAndroid Build Coastguard Worker
6217*f7c14bbaSAndroid Build Coastguard Worker if (insn_idx == relo->insn_idx)
6218*f7c14bbaSAndroid Build Coastguard Worker return 0;
6219*f7c14bbaSAndroid Build Coastguard Worker return insn_idx < relo->insn_idx ? -1 : 1;
6220*f7c14bbaSAndroid Build Coastguard Worker }
6221*f7c14bbaSAndroid Build Coastguard Worker
find_prog_insn_relo(const struct bpf_program * prog,size_t insn_idx)6222*f7c14bbaSAndroid Build Coastguard Worker static struct reloc_desc *find_prog_insn_relo(const struct bpf_program *prog, size_t insn_idx)
6223*f7c14bbaSAndroid Build Coastguard Worker {
6224*f7c14bbaSAndroid Build Coastguard Worker if (!prog->nr_reloc)
6225*f7c14bbaSAndroid Build Coastguard Worker return NULL;
6226*f7c14bbaSAndroid Build Coastguard Worker return bsearch(&insn_idx, prog->reloc_desc, prog->nr_reloc,
6227*f7c14bbaSAndroid Build Coastguard Worker sizeof(*prog->reloc_desc), cmp_relo_by_insn_idx);
6228*f7c14bbaSAndroid Build Coastguard Worker }
6229*f7c14bbaSAndroid Build Coastguard Worker
append_subprog_relos(struct bpf_program * main_prog,struct bpf_program * subprog)6230*f7c14bbaSAndroid Build Coastguard Worker static int append_subprog_relos(struct bpf_program *main_prog, struct bpf_program *subprog)
6231*f7c14bbaSAndroid Build Coastguard Worker {
6232*f7c14bbaSAndroid Build Coastguard Worker int new_cnt = main_prog->nr_reloc + subprog->nr_reloc;
6233*f7c14bbaSAndroid Build Coastguard Worker struct reloc_desc *relos;
6234*f7c14bbaSAndroid Build Coastguard Worker int i;
6235*f7c14bbaSAndroid Build Coastguard Worker
6236*f7c14bbaSAndroid Build Coastguard Worker if (main_prog == subprog)
6237*f7c14bbaSAndroid Build Coastguard Worker return 0;
6238*f7c14bbaSAndroid Build Coastguard Worker relos = libbpf_reallocarray(main_prog->reloc_desc, new_cnt, sizeof(*relos));
6239*f7c14bbaSAndroid Build Coastguard Worker /* if new count is zero, reallocarray can return a valid NULL result;
6240*f7c14bbaSAndroid Build Coastguard Worker * in this case the previous pointer will be freed, so we *have to*
6241*f7c14bbaSAndroid Build Coastguard Worker * reassign old pointer to the new value (even if it's NULL)
6242*f7c14bbaSAndroid Build Coastguard Worker */
6243*f7c14bbaSAndroid Build Coastguard Worker if (!relos && new_cnt)
6244*f7c14bbaSAndroid Build Coastguard Worker return -ENOMEM;
6245*f7c14bbaSAndroid Build Coastguard Worker if (subprog->nr_reloc)
6246*f7c14bbaSAndroid Build Coastguard Worker memcpy(relos + main_prog->nr_reloc, subprog->reloc_desc,
6247*f7c14bbaSAndroid Build Coastguard Worker sizeof(*relos) * subprog->nr_reloc);
6248*f7c14bbaSAndroid Build Coastguard Worker
6249*f7c14bbaSAndroid Build Coastguard Worker for (i = main_prog->nr_reloc; i < new_cnt; i++)
6250*f7c14bbaSAndroid Build Coastguard Worker relos[i].insn_idx += subprog->sub_insn_off;
6251*f7c14bbaSAndroid Build Coastguard Worker /* After insn_idx adjustment the 'relos' array is still sorted
6252*f7c14bbaSAndroid Build Coastguard Worker * by insn_idx and doesn't break bsearch.
6253*f7c14bbaSAndroid Build Coastguard Worker */
6254*f7c14bbaSAndroid Build Coastguard Worker main_prog->reloc_desc = relos;
6255*f7c14bbaSAndroid Build Coastguard Worker main_prog->nr_reloc = new_cnt;
6256*f7c14bbaSAndroid Build Coastguard Worker return 0;
6257*f7c14bbaSAndroid Build Coastguard Worker }
6258*f7c14bbaSAndroid Build Coastguard Worker
6259*f7c14bbaSAndroid Build Coastguard Worker static int
bpf_object__append_subprog_code(struct bpf_object * obj,struct bpf_program * main_prog,struct bpf_program * subprog)6260*f7c14bbaSAndroid Build Coastguard Worker bpf_object__append_subprog_code(struct bpf_object *obj, struct bpf_program *main_prog,
6261*f7c14bbaSAndroid Build Coastguard Worker struct bpf_program *subprog)
6262*f7c14bbaSAndroid Build Coastguard Worker {
6263*f7c14bbaSAndroid Build Coastguard Worker struct bpf_insn *insns;
6264*f7c14bbaSAndroid Build Coastguard Worker size_t new_cnt;
6265*f7c14bbaSAndroid Build Coastguard Worker int err;
6266*f7c14bbaSAndroid Build Coastguard Worker
6267*f7c14bbaSAndroid Build Coastguard Worker subprog->sub_insn_off = main_prog->insns_cnt;
6268*f7c14bbaSAndroid Build Coastguard Worker
6269*f7c14bbaSAndroid Build Coastguard Worker new_cnt = main_prog->insns_cnt + subprog->insns_cnt;
6270*f7c14bbaSAndroid Build Coastguard Worker insns = libbpf_reallocarray(main_prog->insns, new_cnt, sizeof(*insns));
6271*f7c14bbaSAndroid Build Coastguard Worker if (!insns) {
6272*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': failed to realloc prog code\n", main_prog->name);
6273*f7c14bbaSAndroid Build Coastguard Worker return -ENOMEM;
6274*f7c14bbaSAndroid Build Coastguard Worker }
6275*f7c14bbaSAndroid Build Coastguard Worker main_prog->insns = insns;
6276*f7c14bbaSAndroid Build Coastguard Worker main_prog->insns_cnt = new_cnt;
6277*f7c14bbaSAndroid Build Coastguard Worker
6278*f7c14bbaSAndroid Build Coastguard Worker memcpy(main_prog->insns + subprog->sub_insn_off, subprog->insns,
6279*f7c14bbaSAndroid Build Coastguard Worker subprog->insns_cnt * sizeof(*insns));
6280*f7c14bbaSAndroid Build Coastguard Worker
6281*f7c14bbaSAndroid Build Coastguard Worker pr_debug("prog '%s': added %zu insns from sub-prog '%s'\n",
6282*f7c14bbaSAndroid Build Coastguard Worker main_prog->name, subprog->insns_cnt, subprog->name);
6283*f7c14bbaSAndroid Build Coastguard Worker
6284*f7c14bbaSAndroid Build Coastguard Worker /* The subprog insns are now appended. Append its relos too. */
6285*f7c14bbaSAndroid Build Coastguard Worker err = append_subprog_relos(main_prog, subprog);
6286*f7c14bbaSAndroid Build Coastguard Worker if (err)
6287*f7c14bbaSAndroid Build Coastguard Worker return err;
6288*f7c14bbaSAndroid Build Coastguard Worker return 0;
6289*f7c14bbaSAndroid Build Coastguard Worker }
6290*f7c14bbaSAndroid Build Coastguard Worker
6291*f7c14bbaSAndroid Build Coastguard Worker static int
bpf_object__reloc_code(struct bpf_object * obj,struct bpf_program * main_prog,struct bpf_program * prog)6292*f7c14bbaSAndroid Build Coastguard Worker bpf_object__reloc_code(struct bpf_object *obj, struct bpf_program *main_prog,
6293*f7c14bbaSAndroid Build Coastguard Worker struct bpf_program *prog)
6294*f7c14bbaSAndroid Build Coastguard Worker {
6295*f7c14bbaSAndroid Build Coastguard Worker size_t sub_insn_idx, insn_idx;
6296*f7c14bbaSAndroid Build Coastguard Worker struct bpf_program *subprog;
6297*f7c14bbaSAndroid Build Coastguard Worker struct reloc_desc *relo;
6298*f7c14bbaSAndroid Build Coastguard Worker struct bpf_insn *insn;
6299*f7c14bbaSAndroid Build Coastguard Worker int err;
6300*f7c14bbaSAndroid Build Coastguard Worker
6301*f7c14bbaSAndroid Build Coastguard Worker err = reloc_prog_func_and_line_info(obj, main_prog, prog);
6302*f7c14bbaSAndroid Build Coastguard Worker if (err)
6303*f7c14bbaSAndroid Build Coastguard Worker return err;
6304*f7c14bbaSAndroid Build Coastguard Worker
6305*f7c14bbaSAndroid Build Coastguard Worker for (insn_idx = 0; insn_idx < prog->sec_insn_cnt; insn_idx++) {
6306*f7c14bbaSAndroid Build Coastguard Worker insn = &main_prog->insns[prog->sub_insn_off + insn_idx];
6307*f7c14bbaSAndroid Build Coastguard Worker if (!insn_is_subprog_call(insn) && !insn_is_pseudo_func(insn))
6308*f7c14bbaSAndroid Build Coastguard Worker continue;
6309*f7c14bbaSAndroid Build Coastguard Worker
6310*f7c14bbaSAndroid Build Coastguard Worker relo = find_prog_insn_relo(prog, insn_idx);
6311*f7c14bbaSAndroid Build Coastguard Worker if (relo && relo->type == RELO_EXTERN_CALL)
6312*f7c14bbaSAndroid Build Coastguard Worker /* kfunc relocations will be handled later
6313*f7c14bbaSAndroid Build Coastguard Worker * in bpf_object__relocate_data()
6314*f7c14bbaSAndroid Build Coastguard Worker */
6315*f7c14bbaSAndroid Build Coastguard Worker continue;
6316*f7c14bbaSAndroid Build Coastguard Worker if (relo && relo->type != RELO_CALL && relo->type != RELO_SUBPROG_ADDR) {
6317*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': unexpected relo for insn #%zu, type %d\n",
6318*f7c14bbaSAndroid Build Coastguard Worker prog->name, insn_idx, relo->type);
6319*f7c14bbaSAndroid Build Coastguard Worker return -LIBBPF_ERRNO__RELOC;
6320*f7c14bbaSAndroid Build Coastguard Worker }
6321*f7c14bbaSAndroid Build Coastguard Worker if (relo) {
6322*f7c14bbaSAndroid Build Coastguard Worker /* sub-program instruction index is a combination of
6323*f7c14bbaSAndroid Build Coastguard Worker * an offset of a symbol pointed to by relocation and
6324*f7c14bbaSAndroid Build Coastguard Worker * call instruction's imm field; for global functions,
6325*f7c14bbaSAndroid Build Coastguard Worker * call always has imm = -1, but for static functions
6326*f7c14bbaSAndroid Build Coastguard Worker * relocation is against STT_SECTION and insn->imm
6327*f7c14bbaSAndroid Build Coastguard Worker * points to a start of a static function
6328*f7c14bbaSAndroid Build Coastguard Worker *
6329*f7c14bbaSAndroid Build Coastguard Worker * for subprog addr relocation, the relo->sym_off + insn->imm is
6330*f7c14bbaSAndroid Build Coastguard Worker * the byte offset in the corresponding section.
6331*f7c14bbaSAndroid Build Coastguard Worker */
6332*f7c14bbaSAndroid Build Coastguard Worker if (relo->type == RELO_CALL)
6333*f7c14bbaSAndroid Build Coastguard Worker sub_insn_idx = relo->sym_off / BPF_INSN_SZ + insn->imm + 1;
6334*f7c14bbaSAndroid Build Coastguard Worker else
6335*f7c14bbaSAndroid Build Coastguard Worker sub_insn_idx = (relo->sym_off + insn->imm) / BPF_INSN_SZ;
6336*f7c14bbaSAndroid Build Coastguard Worker } else if (insn_is_pseudo_func(insn)) {
6337*f7c14bbaSAndroid Build Coastguard Worker /*
6338*f7c14bbaSAndroid Build Coastguard Worker * RELO_SUBPROG_ADDR relo is always emitted even if both
6339*f7c14bbaSAndroid Build Coastguard Worker * functions are in the same section, so it shouldn't reach here.
6340*f7c14bbaSAndroid Build Coastguard Worker */
6341*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': missing subprog addr relo for insn #%zu\n",
6342*f7c14bbaSAndroid Build Coastguard Worker prog->name, insn_idx);
6343*f7c14bbaSAndroid Build Coastguard Worker return -LIBBPF_ERRNO__RELOC;
6344*f7c14bbaSAndroid Build Coastguard Worker } else {
6345*f7c14bbaSAndroid Build Coastguard Worker /* if subprogram call is to a static function within
6346*f7c14bbaSAndroid Build Coastguard Worker * the same ELF section, there won't be any relocation
6347*f7c14bbaSAndroid Build Coastguard Worker * emitted, but it also means there is no additional
6348*f7c14bbaSAndroid Build Coastguard Worker * offset necessary, insns->imm is relative to
6349*f7c14bbaSAndroid Build Coastguard Worker * instruction's original position within the section
6350*f7c14bbaSAndroid Build Coastguard Worker */
6351*f7c14bbaSAndroid Build Coastguard Worker sub_insn_idx = prog->sec_insn_off + insn_idx + insn->imm + 1;
6352*f7c14bbaSAndroid Build Coastguard Worker }
6353*f7c14bbaSAndroid Build Coastguard Worker
6354*f7c14bbaSAndroid Build Coastguard Worker /* we enforce that sub-programs should be in .text section */
6355*f7c14bbaSAndroid Build Coastguard Worker subprog = find_prog_by_sec_insn(obj, obj->efile.text_shndx, sub_insn_idx);
6356*f7c14bbaSAndroid Build Coastguard Worker if (!subprog) {
6357*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': no .text section found yet sub-program call exists\n",
6358*f7c14bbaSAndroid Build Coastguard Worker prog->name);
6359*f7c14bbaSAndroid Build Coastguard Worker return -LIBBPF_ERRNO__RELOC;
6360*f7c14bbaSAndroid Build Coastguard Worker }
6361*f7c14bbaSAndroid Build Coastguard Worker
6362*f7c14bbaSAndroid Build Coastguard Worker /* if it's the first call instruction calling into this
6363*f7c14bbaSAndroid Build Coastguard Worker * subprogram (meaning this subprog hasn't been processed
6364*f7c14bbaSAndroid Build Coastguard Worker * yet) within the context of current main program:
6365*f7c14bbaSAndroid Build Coastguard Worker * - append it at the end of main program's instructions blog;
6366*f7c14bbaSAndroid Build Coastguard Worker * - process is recursively, while current program is put on hold;
6367*f7c14bbaSAndroid Build Coastguard Worker * - if that subprogram calls some other not yet processes
6368*f7c14bbaSAndroid Build Coastguard Worker * subprogram, same thing will happen recursively until
6369*f7c14bbaSAndroid Build Coastguard Worker * there are no more unprocesses subprograms left to append
6370*f7c14bbaSAndroid Build Coastguard Worker * and relocate.
6371*f7c14bbaSAndroid Build Coastguard Worker */
6372*f7c14bbaSAndroid Build Coastguard Worker if (subprog->sub_insn_off == 0) {
6373*f7c14bbaSAndroid Build Coastguard Worker err = bpf_object__append_subprog_code(obj, main_prog, subprog);
6374*f7c14bbaSAndroid Build Coastguard Worker if (err)
6375*f7c14bbaSAndroid Build Coastguard Worker return err;
6376*f7c14bbaSAndroid Build Coastguard Worker err = bpf_object__reloc_code(obj, main_prog, subprog);
6377*f7c14bbaSAndroid Build Coastguard Worker if (err)
6378*f7c14bbaSAndroid Build Coastguard Worker return err;
6379*f7c14bbaSAndroid Build Coastguard Worker }
6380*f7c14bbaSAndroid Build Coastguard Worker
6381*f7c14bbaSAndroid Build Coastguard Worker /* main_prog->insns memory could have been re-allocated, so
6382*f7c14bbaSAndroid Build Coastguard Worker * calculate pointer again
6383*f7c14bbaSAndroid Build Coastguard Worker */
6384*f7c14bbaSAndroid Build Coastguard Worker insn = &main_prog->insns[prog->sub_insn_off + insn_idx];
6385*f7c14bbaSAndroid Build Coastguard Worker /* calculate correct instruction position within current main
6386*f7c14bbaSAndroid Build Coastguard Worker * prog; each main prog can have a different set of
6387*f7c14bbaSAndroid Build Coastguard Worker * subprograms appended (potentially in different order as
6388*f7c14bbaSAndroid Build Coastguard Worker * well), so position of any subprog can be different for
6389*f7c14bbaSAndroid Build Coastguard Worker * different main programs
6390*f7c14bbaSAndroid Build Coastguard Worker */
6391*f7c14bbaSAndroid Build Coastguard Worker insn->imm = subprog->sub_insn_off - (prog->sub_insn_off + insn_idx) - 1;
6392*f7c14bbaSAndroid Build Coastguard Worker
6393*f7c14bbaSAndroid Build Coastguard Worker pr_debug("prog '%s': insn #%zu relocated, imm %d points to subprog '%s' (now at %zu offset)\n",
6394*f7c14bbaSAndroid Build Coastguard Worker prog->name, insn_idx, insn->imm, subprog->name, subprog->sub_insn_off);
6395*f7c14bbaSAndroid Build Coastguard Worker }
6396*f7c14bbaSAndroid Build Coastguard Worker
6397*f7c14bbaSAndroid Build Coastguard Worker return 0;
6398*f7c14bbaSAndroid Build Coastguard Worker }
6399*f7c14bbaSAndroid Build Coastguard Worker
6400*f7c14bbaSAndroid Build Coastguard Worker /*
6401*f7c14bbaSAndroid Build Coastguard Worker * Relocate sub-program calls.
6402*f7c14bbaSAndroid Build Coastguard Worker *
6403*f7c14bbaSAndroid Build Coastguard Worker * Algorithm operates as follows. Each entry-point BPF program (referred to as
6404*f7c14bbaSAndroid Build Coastguard Worker * main prog) is processed separately. For each subprog (non-entry functions,
6405*f7c14bbaSAndroid Build Coastguard Worker * that can be called from either entry progs or other subprogs) gets their
6406*f7c14bbaSAndroid Build Coastguard Worker * sub_insn_off reset to zero. This serves as indicator that this subprogram
6407*f7c14bbaSAndroid Build Coastguard Worker * hasn't been yet appended and relocated within current main prog. Once its
6408*f7c14bbaSAndroid Build Coastguard Worker * relocated, sub_insn_off will point at the position within current main prog
6409*f7c14bbaSAndroid Build Coastguard Worker * where given subprog was appended. This will further be used to relocate all
6410*f7c14bbaSAndroid Build Coastguard Worker * the call instructions jumping into this subprog.
6411*f7c14bbaSAndroid Build Coastguard Worker *
6412*f7c14bbaSAndroid Build Coastguard Worker * We start with main program and process all call instructions. If the call
6413*f7c14bbaSAndroid Build Coastguard Worker * is into a subprog that hasn't been processed (i.e., subprog->sub_insn_off
6414*f7c14bbaSAndroid Build Coastguard Worker * is zero), subprog instructions are appended at the end of main program's
6415*f7c14bbaSAndroid Build Coastguard Worker * instruction array. Then main program is "put on hold" while we recursively
6416*f7c14bbaSAndroid Build Coastguard Worker * process newly appended subprogram. If that subprogram calls into another
6417*f7c14bbaSAndroid Build Coastguard Worker * subprogram that hasn't been appended, new subprogram is appended again to
6418*f7c14bbaSAndroid Build Coastguard Worker * the *main* prog's instructions (subprog's instructions are always left
6419*f7c14bbaSAndroid Build Coastguard Worker * untouched, as they need to be in unmodified state for subsequent main progs
6420*f7c14bbaSAndroid Build Coastguard Worker * and subprog instructions are always sent only as part of a main prog) and
6421*f7c14bbaSAndroid Build Coastguard Worker * the process continues recursively. Once all the subprogs called from a main
6422*f7c14bbaSAndroid Build Coastguard Worker * prog or any of its subprogs are appended (and relocated), all their
6423*f7c14bbaSAndroid Build Coastguard Worker * positions within finalized instructions array are known, so it's easy to
6424*f7c14bbaSAndroid Build Coastguard Worker * rewrite call instructions with correct relative offsets, corresponding to
6425*f7c14bbaSAndroid Build Coastguard Worker * desired target subprog.
6426*f7c14bbaSAndroid Build Coastguard Worker *
6427*f7c14bbaSAndroid Build Coastguard Worker * Its important to realize that some subprogs might not be called from some
6428*f7c14bbaSAndroid Build Coastguard Worker * main prog and any of its called/used subprogs. Those will keep their
6429*f7c14bbaSAndroid Build Coastguard Worker * subprog->sub_insn_off as zero at all times and won't be appended to current
6430*f7c14bbaSAndroid Build Coastguard Worker * main prog and won't be relocated within the context of current main prog.
6431*f7c14bbaSAndroid Build Coastguard Worker * They might still be used from other main progs later.
6432*f7c14bbaSAndroid Build Coastguard Worker *
6433*f7c14bbaSAndroid Build Coastguard Worker * Visually this process can be shown as below. Suppose we have two main
6434*f7c14bbaSAndroid Build Coastguard Worker * programs mainA and mainB and BPF object contains three subprogs: subA,
6435*f7c14bbaSAndroid Build Coastguard Worker * subB, and subC. mainA calls only subA, mainB calls only subC, but subA and
6436*f7c14bbaSAndroid Build Coastguard Worker * subC both call subB:
6437*f7c14bbaSAndroid Build Coastguard Worker *
6438*f7c14bbaSAndroid Build Coastguard Worker * +--------+ +-------+
6439*f7c14bbaSAndroid Build Coastguard Worker * | v v |
6440*f7c14bbaSAndroid Build Coastguard Worker * +--+---+ +--+-+-+ +---+--+
6441*f7c14bbaSAndroid Build Coastguard Worker * | subA | | subB | | subC |
6442*f7c14bbaSAndroid Build Coastguard Worker * +--+---+ +------+ +---+--+
6443*f7c14bbaSAndroid Build Coastguard Worker * ^ ^
6444*f7c14bbaSAndroid Build Coastguard Worker * | |
6445*f7c14bbaSAndroid Build Coastguard Worker * +---+-------+ +------+----+
6446*f7c14bbaSAndroid Build Coastguard Worker * | mainA | | mainB |
6447*f7c14bbaSAndroid Build Coastguard Worker * +-----------+ +-----------+
6448*f7c14bbaSAndroid Build Coastguard Worker *
6449*f7c14bbaSAndroid Build Coastguard Worker * We'll start relocating mainA, will find subA, append it and start
6450*f7c14bbaSAndroid Build Coastguard Worker * processing sub A recursively:
6451*f7c14bbaSAndroid Build Coastguard Worker *
6452*f7c14bbaSAndroid Build Coastguard Worker * +-----------+------+
6453*f7c14bbaSAndroid Build Coastguard Worker * | mainA | subA |
6454*f7c14bbaSAndroid Build Coastguard Worker * +-----------+------+
6455*f7c14bbaSAndroid Build Coastguard Worker *
6456*f7c14bbaSAndroid Build Coastguard Worker * At this point we notice that subB is used from subA, so we append it and
6457*f7c14bbaSAndroid Build Coastguard Worker * relocate (there are no further subcalls from subB):
6458*f7c14bbaSAndroid Build Coastguard Worker *
6459*f7c14bbaSAndroid Build Coastguard Worker * +-----------+------+------+
6460*f7c14bbaSAndroid Build Coastguard Worker * | mainA | subA | subB |
6461*f7c14bbaSAndroid Build Coastguard Worker * +-----------+------+------+
6462*f7c14bbaSAndroid Build Coastguard Worker *
6463*f7c14bbaSAndroid Build Coastguard Worker * At this point, we relocate subA calls, then go one level up and finish with
6464*f7c14bbaSAndroid Build Coastguard Worker * relocatin mainA calls. mainA is done.
6465*f7c14bbaSAndroid Build Coastguard Worker *
6466*f7c14bbaSAndroid Build Coastguard Worker * For mainB process is similar but results in different order. We start with
6467*f7c14bbaSAndroid Build Coastguard Worker * mainB and skip subA and subB, as mainB never calls them (at least
6468*f7c14bbaSAndroid Build Coastguard Worker * directly), but we see subC is needed, so we append and start processing it:
6469*f7c14bbaSAndroid Build Coastguard Worker *
6470*f7c14bbaSAndroid Build Coastguard Worker * +-----------+------+
6471*f7c14bbaSAndroid Build Coastguard Worker * | mainB | subC |
6472*f7c14bbaSAndroid Build Coastguard Worker * +-----------+------+
6473*f7c14bbaSAndroid Build Coastguard Worker * Now we see subC needs subB, so we go back to it, append and relocate it:
6474*f7c14bbaSAndroid Build Coastguard Worker *
6475*f7c14bbaSAndroid Build Coastguard Worker * +-----------+------+------+
6476*f7c14bbaSAndroid Build Coastguard Worker * | mainB | subC | subB |
6477*f7c14bbaSAndroid Build Coastguard Worker * +-----------+------+------+
6478*f7c14bbaSAndroid Build Coastguard Worker *
6479*f7c14bbaSAndroid Build Coastguard Worker * At this point we unwind recursion, relocate calls in subC, then in mainB.
6480*f7c14bbaSAndroid Build Coastguard Worker */
6481*f7c14bbaSAndroid Build Coastguard Worker static int
bpf_object__relocate_calls(struct bpf_object * obj,struct bpf_program * prog)6482*f7c14bbaSAndroid Build Coastguard Worker bpf_object__relocate_calls(struct bpf_object *obj, struct bpf_program *prog)
6483*f7c14bbaSAndroid Build Coastguard Worker {
6484*f7c14bbaSAndroid Build Coastguard Worker struct bpf_program *subprog;
6485*f7c14bbaSAndroid Build Coastguard Worker int i, err;
6486*f7c14bbaSAndroid Build Coastguard Worker
6487*f7c14bbaSAndroid Build Coastguard Worker /* mark all subprogs as not relocated (yet) within the context of
6488*f7c14bbaSAndroid Build Coastguard Worker * current main program
6489*f7c14bbaSAndroid Build Coastguard Worker */
6490*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < obj->nr_programs; i++) {
6491*f7c14bbaSAndroid Build Coastguard Worker subprog = &obj->programs[i];
6492*f7c14bbaSAndroid Build Coastguard Worker if (!prog_is_subprog(obj, subprog))
6493*f7c14bbaSAndroid Build Coastguard Worker continue;
6494*f7c14bbaSAndroid Build Coastguard Worker
6495*f7c14bbaSAndroid Build Coastguard Worker subprog->sub_insn_off = 0;
6496*f7c14bbaSAndroid Build Coastguard Worker }
6497*f7c14bbaSAndroid Build Coastguard Worker
6498*f7c14bbaSAndroid Build Coastguard Worker err = bpf_object__reloc_code(obj, prog, prog);
6499*f7c14bbaSAndroid Build Coastguard Worker if (err)
6500*f7c14bbaSAndroid Build Coastguard Worker return err;
6501*f7c14bbaSAndroid Build Coastguard Worker
6502*f7c14bbaSAndroid Build Coastguard Worker return 0;
6503*f7c14bbaSAndroid Build Coastguard Worker }
6504*f7c14bbaSAndroid Build Coastguard Worker
6505*f7c14bbaSAndroid Build Coastguard Worker static void
bpf_object__free_relocs(struct bpf_object * obj)6506*f7c14bbaSAndroid Build Coastguard Worker bpf_object__free_relocs(struct bpf_object *obj)
6507*f7c14bbaSAndroid Build Coastguard Worker {
6508*f7c14bbaSAndroid Build Coastguard Worker struct bpf_program *prog;
6509*f7c14bbaSAndroid Build Coastguard Worker int i;
6510*f7c14bbaSAndroid Build Coastguard Worker
6511*f7c14bbaSAndroid Build Coastguard Worker /* free up relocation descriptors */
6512*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < obj->nr_programs; i++) {
6513*f7c14bbaSAndroid Build Coastguard Worker prog = &obj->programs[i];
6514*f7c14bbaSAndroid Build Coastguard Worker zfree(&prog->reloc_desc);
6515*f7c14bbaSAndroid Build Coastguard Worker prog->nr_reloc = 0;
6516*f7c14bbaSAndroid Build Coastguard Worker }
6517*f7c14bbaSAndroid Build Coastguard Worker }
6518*f7c14bbaSAndroid Build Coastguard Worker
cmp_relocs(const void * _a,const void * _b)6519*f7c14bbaSAndroid Build Coastguard Worker static int cmp_relocs(const void *_a, const void *_b)
6520*f7c14bbaSAndroid Build Coastguard Worker {
6521*f7c14bbaSAndroid Build Coastguard Worker const struct reloc_desc *a = _a;
6522*f7c14bbaSAndroid Build Coastguard Worker const struct reloc_desc *b = _b;
6523*f7c14bbaSAndroid Build Coastguard Worker
6524*f7c14bbaSAndroid Build Coastguard Worker if (a->insn_idx != b->insn_idx)
6525*f7c14bbaSAndroid Build Coastguard Worker return a->insn_idx < b->insn_idx ? -1 : 1;
6526*f7c14bbaSAndroid Build Coastguard Worker
6527*f7c14bbaSAndroid Build Coastguard Worker /* no two relocations should have the same insn_idx, but ... */
6528*f7c14bbaSAndroid Build Coastguard Worker if (a->type != b->type)
6529*f7c14bbaSAndroid Build Coastguard Worker return a->type < b->type ? -1 : 1;
6530*f7c14bbaSAndroid Build Coastguard Worker
6531*f7c14bbaSAndroid Build Coastguard Worker return 0;
6532*f7c14bbaSAndroid Build Coastguard Worker }
6533*f7c14bbaSAndroid Build Coastguard Worker
bpf_object__sort_relos(struct bpf_object * obj)6534*f7c14bbaSAndroid Build Coastguard Worker static void bpf_object__sort_relos(struct bpf_object *obj)
6535*f7c14bbaSAndroid Build Coastguard Worker {
6536*f7c14bbaSAndroid Build Coastguard Worker int i;
6537*f7c14bbaSAndroid Build Coastguard Worker
6538*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < obj->nr_programs; i++) {
6539*f7c14bbaSAndroid Build Coastguard Worker struct bpf_program *p = &obj->programs[i];
6540*f7c14bbaSAndroid Build Coastguard Worker
6541*f7c14bbaSAndroid Build Coastguard Worker if (!p->nr_reloc)
6542*f7c14bbaSAndroid Build Coastguard Worker continue;
6543*f7c14bbaSAndroid Build Coastguard Worker
6544*f7c14bbaSAndroid Build Coastguard Worker qsort(p->reloc_desc, p->nr_reloc, sizeof(*p->reloc_desc), cmp_relocs);
6545*f7c14bbaSAndroid Build Coastguard Worker }
6546*f7c14bbaSAndroid Build Coastguard Worker }
6547*f7c14bbaSAndroid Build Coastguard Worker
bpf_prog_assign_exc_cb(struct bpf_object * obj,struct bpf_program * prog)6548*f7c14bbaSAndroid Build Coastguard Worker static int bpf_prog_assign_exc_cb(struct bpf_object *obj, struct bpf_program *prog)
6549*f7c14bbaSAndroid Build Coastguard Worker {
6550*f7c14bbaSAndroid Build Coastguard Worker const char *str = "exception_callback:";
6551*f7c14bbaSAndroid Build Coastguard Worker size_t pfx_len = strlen(str);
6552*f7c14bbaSAndroid Build Coastguard Worker int i, j, n;
6553*f7c14bbaSAndroid Build Coastguard Worker
6554*f7c14bbaSAndroid Build Coastguard Worker if (!obj->btf || !kernel_supports(obj, FEAT_BTF_DECL_TAG))
6555*f7c14bbaSAndroid Build Coastguard Worker return 0;
6556*f7c14bbaSAndroid Build Coastguard Worker
6557*f7c14bbaSAndroid Build Coastguard Worker n = btf__type_cnt(obj->btf);
6558*f7c14bbaSAndroid Build Coastguard Worker for (i = 1; i < n; i++) {
6559*f7c14bbaSAndroid Build Coastguard Worker const char *name;
6560*f7c14bbaSAndroid Build Coastguard Worker struct btf_type *t;
6561*f7c14bbaSAndroid Build Coastguard Worker
6562*f7c14bbaSAndroid Build Coastguard Worker t = btf_type_by_id(obj->btf, i);
6563*f7c14bbaSAndroid Build Coastguard Worker if (!btf_is_decl_tag(t) || btf_decl_tag(t)->component_idx != -1)
6564*f7c14bbaSAndroid Build Coastguard Worker continue;
6565*f7c14bbaSAndroid Build Coastguard Worker
6566*f7c14bbaSAndroid Build Coastguard Worker name = btf__str_by_offset(obj->btf, t->name_off);
6567*f7c14bbaSAndroid Build Coastguard Worker if (strncmp(name, str, pfx_len) != 0)
6568*f7c14bbaSAndroid Build Coastguard Worker continue;
6569*f7c14bbaSAndroid Build Coastguard Worker
6570*f7c14bbaSAndroid Build Coastguard Worker t = btf_type_by_id(obj->btf, t->type);
6571*f7c14bbaSAndroid Build Coastguard Worker if (!btf_is_func(t) || btf_func_linkage(t) != BTF_FUNC_GLOBAL) {
6572*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': exception_callback:<value> decl tag not applied to the main program\n",
6573*f7c14bbaSAndroid Build Coastguard Worker prog->name);
6574*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
6575*f7c14bbaSAndroid Build Coastguard Worker }
6576*f7c14bbaSAndroid Build Coastguard Worker if (strcmp(prog->name, btf__str_by_offset(obj->btf, t->name_off)) != 0)
6577*f7c14bbaSAndroid Build Coastguard Worker continue;
6578*f7c14bbaSAndroid Build Coastguard Worker /* Multiple callbacks are specified for the same prog,
6579*f7c14bbaSAndroid Build Coastguard Worker * the verifier will eventually return an error for this
6580*f7c14bbaSAndroid Build Coastguard Worker * case, hence simply skip appending a subprog.
6581*f7c14bbaSAndroid Build Coastguard Worker */
6582*f7c14bbaSAndroid Build Coastguard Worker if (prog->exception_cb_idx >= 0) {
6583*f7c14bbaSAndroid Build Coastguard Worker prog->exception_cb_idx = -1;
6584*f7c14bbaSAndroid Build Coastguard Worker break;
6585*f7c14bbaSAndroid Build Coastguard Worker }
6586*f7c14bbaSAndroid Build Coastguard Worker
6587*f7c14bbaSAndroid Build Coastguard Worker name += pfx_len;
6588*f7c14bbaSAndroid Build Coastguard Worker if (str_is_empty(name)) {
6589*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': exception_callback:<value> decl tag contains empty value\n",
6590*f7c14bbaSAndroid Build Coastguard Worker prog->name);
6591*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
6592*f7c14bbaSAndroid Build Coastguard Worker }
6593*f7c14bbaSAndroid Build Coastguard Worker
6594*f7c14bbaSAndroid Build Coastguard Worker for (j = 0; j < obj->nr_programs; j++) {
6595*f7c14bbaSAndroid Build Coastguard Worker struct bpf_program *subprog = &obj->programs[j];
6596*f7c14bbaSAndroid Build Coastguard Worker
6597*f7c14bbaSAndroid Build Coastguard Worker if (!prog_is_subprog(obj, subprog))
6598*f7c14bbaSAndroid Build Coastguard Worker continue;
6599*f7c14bbaSAndroid Build Coastguard Worker if (strcmp(name, subprog->name) != 0)
6600*f7c14bbaSAndroid Build Coastguard Worker continue;
6601*f7c14bbaSAndroid Build Coastguard Worker /* Enforce non-hidden, as from verifier point of
6602*f7c14bbaSAndroid Build Coastguard Worker * view it expects global functions, whereas the
6603*f7c14bbaSAndroid Build Coastguard Worker * mark_btf_static fixes up linkage as static.
6604*f7c14bbaSAndroid Build Coastguard Worker */
6605*f7c14bbaSAndroid Build Coastguard Worker if (!subprog->sym_global || subprog->mark_btf_static) {
6606*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': exception callback %s must be a global non-hidden function\n",
6607*f7c14bbaSAndroid Build Coastguard Worker prog->name, subprog->name);
6608*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
6609*f7c14bbaSAndroid Build Coastguard Worker }
6610*f7c14bbaSAndroid Build Coastguard Worker /* Let's see if we already saw a static exception callback with the same name */
6611*f7c14bbaSAndroid Build Coastguard Worker if (prog->exception_cb_idx >= 0) {
6612*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': multiple subprogs with same name as exception callback '%s'\n",
6613*f7c14bbaSAndroid Build Coastguard Worker prog->name, subprog->name);
6614*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
6615*f7c14bbaSAndroid Build Coastguard Worker }
6616*f7c14bbaSAndroid Build Coastguard Worker prog->exception_cb_idx = j;
6617*f7c14bbaSAndroid Build Coastguard Worker break;
6618*f7c14bbaSAndroid Build Coastguard Worker }
6619*f7c14bbaSAndroid Build Coastguard Worker
6620*f7c14bbaSAndroid Build Coastguard Worker if (prog->exception_cb_idx >= 0)
6621*f7c14bbaSAndroid Build Coastguard Worker continue;
6622*f7c14bbaSAndroid Build Coastguard Worker
6623*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': cannot find exception callback '%s'\n", prog->name, name);
6624*f7c14bbaSAndroid Build Coastguard Worker return -ENOENT;
6625*f7c14bbaSAndroid Build Coastguard Worker }
6626*f7c14bbaSAndroid Build Coastguard Worker
6627*f7c14bbaSAndroid Build Coastguard Worker return 0;
6628*f7c14bbaSAndroid Build Coastguard Worker }
6629*f7c14bbaSAndroid Build Coastguard Worker
6630*f7c14bbaSAndroid Build Coastguard Worker static struct {
6631*f7c14bbaSAndroid Build Coastguard Worker enum bpf_prog_type prog_type;
6632*f7c14bbaSAndroid Build Coastguard Worker const char *ctx_name;
6633*f7c14bbaSAndroid Build Coastguard Worker } global_ctx_map[] = {
6634*f7c14bbaSAndroid Build Coastguard Worker { BPF_PROG_TYPE_CGROUP_DEVICE, "bpf_cgroup_dev_ctx" },
6635*f7c14bbaSAndroid Build Coastguard Worker { BPF_PROG_TYPE_CGROUP_SKB, "__sk_buff" },
6636*f7c14bbaSAndroid Build Coastguard Worker { BPF_PROG_TYPE_CGROUP_SOCK, "bpf_sock" },
6637*f7c14bbaSAndroid Build Coastguard Worker { BPF_PROG_TYPE_CGROUP_SOCK_ADDR, "bpf_sock_addr" },
6638*f7c14bbaSAndroid Build Coastguard Worker { BPF_PROG_TYPE_CGROUP_SOCKOPT, "bpf_sockopt" },
6639*f7c14bbaSAndroid Build Coastguard Worker { BPF_PROG_TYPE_CGROUP_SYSCTL, "bpf_sysctl" },
6640*f7c14bbaSAndroid Build Coastguard Worker { BPF_PROG_TYPE_FLOW_DISSECTOR, "__sk_buff" },
6641*f7c14bbaSAndroid Build Coastguard Worker { BPF_PROG_TYPE_KPROBE, "bpf_user_pt_regs_t" },
6642*f7c14bbaSAndroid Build Coastguard Worker { BPF_PROG_TYPE_LWT_IN, "__sk_buff" },
6643*f7c14bbaSAndroid Build Coastguard Worker { BPF_PROG_TYPE_LWT_OUT, "__sk_buff" },
6644*f7c14bbaSAndroid Build Coastguard Worker { BPF_PROG_TYPE_LWT_SEG6LOCAL, "__sk_buff" },
6645*f7c14bbaSAndroid Build Coastguard Worker { BPF_PROG_TYPE_LWT_XMIT, "__sk_buff" },
6646*f7c14bbaSAndroid Build Coastguard Worker { BPF_PROG_TYPE_NETFILTER, "bpf_nf_ctx" },
6647*f7c14bbaSAndroid Build Coastguard Worker { BPF_PROG_TYPE_PERF_EVENT, "bpf_perf_event_data" },
6648*f7c14bbaSAndroid Build Coastguard Worker { BPF_PROG_TYPE_RAW_TRACEPOINT, "bpf_raw_tracepoint_args" },
6649*f7c14bbaSAndroid Build Coastguard Worker { BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE, "bpf_raw_tracepoint_args" },
6650*f7c14bbaSAndroid Build Coastguard Worker { BPF_PROG_TYPE_SCHED_ACT, "__sk_buff" },
6651*f7c14bbaSAndroid Build Coastguard Worker { BPF_PROG_TYPE_SCHED_CLS, "__sk_buff" },
6652*f7c14bbaSAndroid Build Coastguard Worker { BPF_PROG_TYPE_SK_LOOKUP, "bpf_sk_lookup" },
6653*f7c14bbaSAndroid Build Coastguard Worker { BPF_PROG_TYPE_SK_MSG, "sk_msg_md" },
6654*f7c14bbaSAndroid Build Coastguard Worker { BPF_PROG_TYPE_SK_REUSEPORT, "sk_reuseport_md" },
6655*f7c14bbaSAndroid Build Coastguard Worker { BPF_PROG_TYPE_SK_SKB, "__sk_buff" },
6656*f7c14bbaSAndroid Build Coastguard Worker { BPF_PROG_TYPE_SOCK_OPS, "bpf_sock_ops" },
6657*f7c14bbaSAndroid Build Coastguard Worker { BPF_PROG_TYPE_SOCKET_FILTER, "__sk_buff" },
6658*f7c14bbaSAndroid Build Coastguard Worker { BPF_PROG_TYPE_XDP, "xdp_md" },
6659*f7c14bbaSAndroid Build Coastguard Worker /* all other program types don't have "named" context structs */
6660*f7c14bbaSAndroid Build Coastguard Worker };
6661*f7c14bbaSAndroid Build Coastguard Worker
6662*f7c14bbaSAndroid Build Coastguard Worker /* forward declarations for arch-specific underlying types of bpf_user_pt_regs_t typedef,
6663*f7c14bbaSAndroid Build Coastguard Worker * for below __builtin_types_compatible_p() checks;
6664*f7c14bbaSAndroid Build Coastguard Worker * with this approach we don't need any extra arch-specific #ifdef guards
6665*f7c14bbaSAndroid Build Coastguard Worker */
6666*f7c14bbaSAndroid Build Coastguard Worker struct pt_regs;
6667*f7c14bbaSAndroid Build Coastguard Worker struct user_pt_regs;
6668*f7c14bbaSAndroid Build Coastguard Worker struct user_regs_struct;
6669*f7c14bbaSAndroid Build Coastguard Worker
need_func_arg_type_fixup(const struct btf * btf,const struct bpf_program * prog,const char * subprog_name,int arg_idx,int arg_type_id,const char * ctx_name)6670*f7c14bbaSAndroid Build Coastguard Worker static bool need_func_arg_type_fixup(const struct btf *btf, const struct bpf_program *prog,
6671*f7c14bbaSAndroid Build Coastguard Worker const char *subprog_name, int arg_idx,
6672*f7c14bbaSAndroid Build Coastguard Worker int arg_type_id, const char *ctx_name)
6673*f7c14bbaSAndroid Build Coastguard Worker {
6674*f7c14bbaSAndroid Build Coastguard Worker const struct btf_type *t;
6675*f7c14bbaSAndroid Build Coastguard Worker const char *tname;
6676*f7c14bbaSAndroid Build Coastguard Worker
6677*f7c14bbaSAndroid Build Coastguard Worker /* check if existing parameter already matches verifier expectations */
6678*f7c14bbaSAndroid Build Coastguard Worker t = skip_mods_and_typedefs(btf, arg_type_id, NULL);
6679*f7c14bbaSAndroid Build Coastguard Worker if (!btf_is_ptr(t))
6680*f7c14bbaSAndroid Build Coastguard Worker goto out_warn;
6681*f7c14bbaSAndroid Build Coastguard Worker
6682*f7c14bbaSAndroid Build Coastguard Worker /* typedef bpf_user_pt_regs_t is a special PITA case, valid for kprobe
6683*f7c14bbaSAndroid Build Coastguard Worker * and perf_event programs, so check this case early on and forget
6684*f7c14bbaSAndroid Build Coastguard Worker * about it for subsequent checks
6685*f7c14bbaSAndroid Build Coastguard Worker */
6686*f7c14bbaSAndroid Build Coastguard Worker while (btf_is_mod(t))
6687*f7c14bbaSAndroid Build Coastguard Worker t = btf__type_by_id(btf, t->type);
6688*f7c14bbaSAndroid Build Coastguard Worker if (btf_is_typedef(t) &&
6689*f7c14bbaSAndroid Build Coastguard Worker (prog->type == BPF_PROG_TYPE_KPROBE || prog->type == BPF_PROG_TYPE_PERF_EVENT)) {
6690*f7c14bbaSAndroid Build Coastguard Worker tname = btf__str_by_offset(btf, t->name_off) ?: "<anon>";
6691*f7c14bbaSAndroid Build Coastguard Worker if (strcmp(tname, "bpf_user_pt_regs_t") == 0)
6692*f7c14bbaSAndroid Build Coastguard Worker return false; /* canonical type for kprobe/perf_event */
6693*f7c14bbaSAndroid Build Coastguard Worker }
6694*f7c14bbaSAndroid Build Coastguard Worker
6695*f7c14bbaSAndroid Build Coastguard Worker /* now we can ignore typedefs moving forward */
6696*f7c14bbaSAndroid Build Coastguard Worker t = skip_mods_and_typedefs(btf, t->type, NULL);
6697*f7c14bbaSAndroid Build Coastguard Worker
6698*f7c14bbaSAndroid Build Coastguard Worker /* if it's `void *`, definitely fix up BTF info */
6699*f7c14bbaSAndroid Build Coastguard Worker if (btf_is_void(t))
6700*f7c14bbaSAndroid Build Coastguard Worker return true;
6701*f7c14bbaSAndroid Build Coastguard Worker
6702*f7c14bbaSAndroid Build Coastguard Worker /* if it's already proper canonical type, no need to fix up */
6703*f7c14bbaSAndroid Build Coastguard Worker tname = btf__str_by_offset(btf, t->name_off) ?: "<anon>";
6704*f7c14bbaSAndroid Build Coastguard Worker if (btf_is_struct(t) && strcmp(tname, ctx_name) == 0)
6705*f7c14bbaSAndroid Build Coastguard Worker return false;
6706*f7c14bbaSAndroid Build Coastguard Worker
6707*f7c14bbaSAndroid Build Coastguard Worker /* special cases */
6708*f7c14bbaSAndroid Build Coastguard Worker switch (prog->type) {
6709*f7c14bbaSAndroid Build Coastguard Worker case BPF_PROG_TYPE_KPROBE:
6710*f7c14bbaSAndroid Build Coastguard Worker /* `struct pt_regs *` is expected, but we need to fix up */
6711*f7c14bbaSAndroid Build Coastguard Worker if (btf_is_struct(t) && strcmp(tname, "pt_regs") == 0)
6712*f7c14bbaSAndroid Build Coastguard Worker return true;
6713*f7c14bbaSAndroid Build Coastguard Worker break;
6714*f7c14bbaSAndroid Build Coastguard Worker case BPF_PROG_TYPE_PERF_EVENT:
6715*f7c14bbaSAndroid Build Coastguard Worker if (__builtin_types_compatible_p(bpf_user_pt_regs_t, struct pt_regs) &&
6716*f7c14bbaSAndroid Build Coastguard Worker btf_is_struct(t) && strcmp(tname, "pt_regs") == 0)
6717*f7c14bbaSAndroid Build Coastguard Worker return true;
6718*f7c14bbaSAndroid Build Coastguard Worker if (__builtin_types_compatible_p(bpf_user_pt_regs_t, struct user_pt_regs) &&
6719*f7c14bbaSAndroid Build Coastguard Worker btf_is_struct(t) && strcmp(tname, "user_pt_regs") == 0)
6720*f7c14bbaSAndroid Build Coastguard Worker return true;
6721*f7c14bbaSAndroid Build Coastguard Worker if (__builtin_types_compatible_p(bpf_user_pt_regs_t, struct user_regs_struct) &&
6722*f7c14bbaSAndroid Build Coastguard Worker btf_is_struct(t) && strcmp(tname, "user_regs_struct") == 0)
6723*f7c14bbaSAndroid Build Coastguard Worker return true;
6724*f7c14bbaSAndroid Build Coastguard Worker break;
6725*f7c14bbaSAndroid Build Coastguard Worker case BPF_PROG_TYPE_RAW_TRACEPOINT:
6726*f7c14bbaSAndroid Build Coastguard Worker case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
6727*f7c14bbaSAndroid Build Coastguard Worker /* allow u64* as ctx */
6728*f7c14bbaSAndroid Build Coastguard Worker if (btf_is_int(t) && t->size == 8)
6729*f7c14bbaSAndroid Build Coastguard Worker return true;
6730*f7c14bbaSAndroid Build Coastguard Worker break;
6731*f7c14bbaSAndroid Build Coastguard Worker default:
6732*f7c14bbaSAndroid Build Coastguard Worker break;
6733*f7c14bbaSAndroid Build Coastguard Worker }
6734*f7c14bbaSAndroid Build Coastguard Worker
6735*f7c14bbaSAndroid Build Coastguard Worker out_warn:
6736*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': subprog '%s' arg#%d is expected to be of `struct %s *` type\n",
6737*f7c14bbaSAndroid Build Coastguard Worker prog->name, subprog_name, arg_idx, ctx_name);
6738*f7c14bbaSAndroid Build Coastguard Worker return false;
6739*f7c14bbaSAndroid Build Coastguard Worker }
6740*f7c14bbaSAndroid Build Coastguard Worker
clone_func_btf_info(struct btf * btf,int orig_fn_id,struct bpf_program * prog)6741*f7c14bbaSAndroid Build Coastguard Worker static int clone_func_btf_info(struct btf *btf, int orig_fn_id, struct bpf_program *prog)
6742*f7c14bbaSAndroid Build Coastguard Worker {
6743*f7c14bbaSAndroid Build Coastguard Worker int fn_id, fn_proto_id, ret_type_id, orig_proto_id;
6744*f7c14bbaSAndroid Build Coastguard Worker int i, err, arg_cnt, fn_name_off, linkage;
6745*f7c14bbaSAndroid Build Coastguard Worker struct btf_type *fn_t, *fn_proto_t, *t;
6746*f7c14bbaSAndroid Build Coastguard Worker struct btf_param *p;
6747*f7c14bbaSAndroid Build Coastguard Worker
6748*f7c14bbaSAndroid Build Coastguard Worker /* caller already validated FUNC -> FUNC_PROTO validity */
6749*f7c14bbaSAndroid Build Coastguard Worker fn_t = btf_type_by_id(btf, orig_fn_id);
6750*f7c14bbaSAndroid Build Coastguard Worker fn_proto_t = btf_type_by_id(btf, fn_t->type);
6751*f7c14bbaSAndroid Build Coastguard Worker
6752*f7c14bbaSAndroid Build Coastguard Worker /* Note that each btf__add_xxx() operation invalidates
6753*f7c14bbaSAndroid Build Coastguard Worker * all btf_type and string pointers, so we need to be
6754*f7c14bbaSAndroid Build Coastguard Worker * very careful when cloning BTF types. BTF type
6755*f7c14bbaSAndroid Build Coastguard Worker * pointers have to be always refetched. And to avoid
6756*f7c14bbaSAndroid Build Coastguard Worker * problems with invalidated string pointers, we
6757*f7c14bbaSAndroid Build Coastguard Worker * add empty strings initially, then just fix up
6758*f7c14bbaSAndroid Build Coastguard Worker * name_off offsets in place. Offsets are stable for
6759*f7c14bbaSAndroid Build Coastguard Worker * existing strings, so that works out.
6760*f7c14bbaSAndroid Build Coastguard Worker */
6761*f7c14bbaSAndroid Build Coastguard Worker fn_name_off = fn_t->name_off; /* we are about to invalidate fn_t */
6762*f7c14bbaSAndroid Build Coastguard Worker linkage = btf_func_linkage(fn_t);
6763*f7c14bbaSAndroid Build Coastguard Worker orig_proto_id = fn_t->type; /* original FUNC_PROTO ID */
6764*f7c14bbaSAndroid Build Coastguard Worker ret_type_id = fn_proto_t->type; /* fn_proto_t will be invalidated */
6765*f7c14bbaSAndroid Build Coastguard Worker arg_cnt = btf_vlen(fn_proto_t);
6766*f7c14bbaSAndroid Build Coastguard Worker
6767*f7c14bbaSAndroid Build Coastguard Worker /* clone FUNC_PROTO and its params */
6768*f7c14bbaSAndroid Build Coastguard Worker fn_proto_id = btf__add_func_proto(btf, ret_type_id);
6769*f7c14bbaSAndroid Build Coastguard Worker if (fn_proto_id < 0)
6770*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
6771*f7c14bbaSAndroid Build Coastguard Worker
6772*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < arg_cnt; i++) {
6773*f7c14bbaSAndroid Build Coastguard Worker int name_off;
6774*f7c14bbaSAndroid Build Coastguard Worker
6775*f7c14bbaSAndroid Build Coastguard Worker /* copy original parameter data */
6776*f7c14bbaSAndroid Build Coastguard Worker t = btf_type_by_id(btf, orig_proto_id);
6777*f7c14bbaSAndroid Build Coastguard Worker p = &btf_params(t)[i];
6778*f7c14bbaSAndroid Build Coastguard Worker name_off = p->name_off;
6779*f7c14bbaSAndroid Build Coastguard Worker
6780*f7c14bbaSAndroid Build Coastguard Worker err = btf__add_func_param(btf, "", p->type);
6781*f7c14bbaSAndroid Build Coastguard Worker if (err)
6782*f7c14bbaSAndroid Build Coastguard Worker return err;
6783*f7c14bbaSAndroid Build Coastguard Worker
6784*f7c14bbaSAndroid Build Coastguard Worker fn_proto_t = btf_type_by_id(btf, fn_proto_id);
6785*f7c14bbaSAndroid Build Coastguard Worker p = &btf_params(fn_proto_t)[i];
6786*f7c14bbaSAndroid Build Coastguard Worker p->name_off = name_off; /* use remembered str offset */
6787*f7c14bbaSAndroid Build Coastguard Worker }
6788*f7c14bbaSAndroid Build Coastguard Worker
6789*f7c14bbaSAndroid Build Coastguard Worker /* clone FUNC now, btf__add_func() enforces non-empty name, so use
6790*f7c14bbaSAndroid Build Coastguard Worker * entry program's name as a placeholder, which we replace immediately
6791*f7c14bbaSAndroid Build Coastguard Worker * with original name_off
6792*f7c14bbaSAndroid Build Coastguard Worker */
6793*f7c14bbaSAndroid Build Coastguard Worker fn_id = btf__add_func(btf, prog->name, linkage, fn_proto_id);
6794*f7c14bbaSAndroid Build Coastguard Worker if (fn_id < 0)
6795*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
6796*f7c14bbaSAndroid Build Coastguard Worker
6797*f7c14bbaSAndroid Build Coastguard Worker fn_t = btf_type_by_id(btf, fn_id);
6798*f7c14bbaSAndroid Build Coastguard Worker fn_t->name_off = fn_name_off; /* reuse original string */
6799*f7c14bbaSAndroid Build Coastguard Worker
6800*f7c14bbaSAndroid Build Coastguard Worker return fn_id;
6801*f7c14bbaSAndroid Build Coastguard Worker }
6802*f7c14bbaSAndroid Build Coastguard Worker
6803*f7c14bbaSAndroid Build Coastguard Worker /* Check if main program or global subprog's function prototype has `arg:ctx`
6804*f7c14bbaSAndroid Build Coastguard Worker * argument tags, and, if necessary, substitute correct type to match what BPF
6805*f7c14bbaSAndroid Build Coastguard Worker * verifier would expect, taking into account specific program type. This
6806*f7c14bbaSAndroid Build Coastguard Worker * allows to support __arg_ctx tag transparently on old kernels that don't yet
6807*f7c14bbaSAndroid Build Coastguard Worker * have a native support for it in the verifier, making user's life much
6808*f7c14bbaSAndroid Build Coastguard Worker * easier.
6809*f7c14bbaSAndroid Build Coastguard Worker */
bpf_program_fixup_func_info(struct bpf_object * obj,struct bpf_program * prog)6810*f7c14bbaSAndroid Build Coastguard Worker static int bpf_program_fixup_func_info(struct bpf_object *obj, struct bpf_program *prog)
6811*f7c14bbaSAndroid Build Coastguard Worker {
6812*f7c14bbaSAndroid Build Coastguard Worker const char *ctx_name = NULL, *ctx_tag = "arg:ctx", *fn_name;
6813*f7c14bbaSAndroid Build Coastguard Worker struct bpf_func_info_min *func_rec;
6814*f7c14bbaSAndroid Build Coastguard Worker struct btf_type *fn_t, *fn_proto_t;
6815*f7c14bbaSAndroid Build Coastguard Worker struct btf *btf = obj->btf;
6816*f7c14bbaSAndroid Build Coastguard Worker const struct btf_type *t;
6817*f7c14bbaSAndroid Build Coastguard Worker struct btf_param *p;
6818*f7c14bbaSAndroid Build Coastguard Worker int ptr_id = 0, struct_id, tag_id, orig_fn_id;
6819*f7c14bbaSAndroid Build Coastguard Worker int i, n, arg_idx, arg_cnt, err, rec_idx;
6820*f7c14bbaSAndroid Build Coastguard Worker int *orig_ids;
6821*f7c14bbaSAndroid Build Coastguard Worker
6822*f7c14bbaSAndroid Build Coastguard Worker /* no .BTF.ext, no problem */
6823*f7c14bbaSAndroid Build Coastguard Worker if (!obj->btf_ext || !prog->func_info)
6824*f7c14bbaSAndroid Build Coastguard Worker return 0;
6825*f7c14bbaSAndroid Build Coastguard Worker
6826*f7c14bbaSAndroid Build Coastguard Worker /* don't do any fix ups if kernel natively supports __arg_ctx */
6827*f7c14bbaSAndroid Build Coastguard Worker if (kernel_supports(obj, FEAT_ARG_CTX_TAG))
6828*f7c14bbaSAndroid Build Coastguard Worker return 0;
6829*f7c14bbaSAndroid Build Coastguard Worker
6830*f7c14bbaSAndroid Build Coastguard Worker /* some BPF program types just don't have named context structs, so
6831*f7c14bbaSAndroid Build Coastguard Worker * this fallback mechanism doesn't work for them
6832*f7c14bbaSAndroid Build Coastguard Worker */
6833*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < ARRAY_SIZE(global_ctx_map); i++) {
6834*f7c14bbaSAndroid Build Coastguard Worker if (global_ctx_map[i].prog_type != prog->type)
6835*f7c14bbaSAndroid Build Coastguard Worker continue;
6836*f7c14bbaSAndroid Build Coastguard Worker ctx_name = global_ctx_map[i].ctx_name;
6837*f7c14bbaSAndroid Build Coastguard Worker break;
6838*f7c14bbaSAndroid Build Coastguard Worker }
6839*f7c14bbaSAndroid Build Coastguard Worker if (!ctx_name)
6840*f7c14bbaSAndroid Build Coastguard Worker return 0;
6841*f7c14bbaSAndroid Build Coastguard Worker
6842*f7c14bbaSAndroid Build Coastguard Worker /* remember original func BTF IDs to detect if we already cloned them */
6843*f7c14bbaSAndroid Build Coastguard Worker orig_ids = calloc(prog->func_info_cnt, sizeof(*orig_ids));
6844*f7c14bbaSAndroid Build Coastguard Worker if (!orig_ids)
6845*f7c14bbaSAndroid Build Coastguard Worker return -ENOMEM;
6846*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < prog->func_info_cnt; i++) {
6847*f7c14bbaSAndroid Build Coastguard Worker func_rec = prog->func_info + prog->func_info_rec_size * i;
6848*f7c14bbaSAndroid Build Coastguard Worker orig_ids[i] = func_rec->type_id;
6849*f7c14bbaSAndroid Build Coastguard Worker }
6850*f7c14bbaSAndroid Build Coastguard Worker
6851*f7c14bbaSAndroid Build Coastguard Worker /* go through each DECL_TAG with "arg:ctx" and see if it points to one
6852*f7c14bbaSAndroid Build Coastguard Worker * of our subprogs; if yes and subprog is global and needs adjustment,
6853*f7c14bbaSAndroid Build Coastguard Worker * clone and adjust FUNC -> FUNC_PROTO combo
6854*f7c14bbaSAndroid Build Coastguard Worker */
6855*f7c14bbaSAndroid Build Coastguard Worker for (i = 1, n = btf__type_cnt(btf); i < n; i++) {
6856*f7c14bbaSAndroid Build Coastguard Worker /* only DECL_TAG with "arg:ctx" value are interesting */
6857*f7c14bbaSAndroid Build Coastguard Worker t = btf__type_by_id(btf, i);
6858*f7c14bbaSAndroid Build Coastguard Worker if (!btf_is_decl_tag(t))
6859*f7c14bbaSAndroid Build Coastguard Worker continue;
6860*f7c14bbaSAndroid Build Coastguard Worker if (strcmp(btf__str_by_offset(btf, t->name_off), ctx_tag) != 0)
6861*f7c14bbaSAndroid Build Coastguard Worker continue;
6862*f7c14bbaSAndroid Build Coastguard Worker
6863*f7c14bbaSAndroid Build Coastguard Worker /* only global funcs need adjustment, if at all */
6864*f7c14bbaSAndroid Build Coastguard Worker orig_fn_id = t->type;
6865*f7c14bbaSAndroid Build Coastguard Worker fn_t = btf_type_by_id(btf, orig_fn_id);
6866*f7c14bbaSAndroid Build Coastguard Worker if (!btf_is_func(fn_t) || btf_func_linkage(fn_t) != BTF_FUNC_GLOBAL)
6867*f7c14bbaSAndroid Build Coastguard Worker continue;
6868*f7c14bbaSAndroid Build Coastguard Worker
6869*f7c14bbaSAndroid Build Coastguard Worker /* sanity check FUNC -> FUNC_PROTO chain, just in case */
6870*f7c14bbaSAndroid Build Coastguard Worker fn_proto_t = btf_type_by_id(btf, fn_t->type);
6871*f7c14bbaSAndroid Build Coastguard Worker if (!fn_proto_t || !btf_is_func_proto(fn_proto_t))
6872*f7c14bbaSAndroid Build Coastguard Worker continue;
6873*f7c14bbaSAndroid Build Coastguard Worker
6874*f7c14bbaSAndroid Build Coastguard Worker /* find corresponding func_info record */
6875*f7c14bbaSAndroid Build Coastguard Worker func_rec = NULL;
6876*f7c14bbaSAndroid Build Coastguard Worker for (rec_idx = 0; rec_idx < prog->func_info_cnt; rec_idx++) {
6877*f7c14bbaSAndroid Build Coastguard Worker if (orig_ids[rec_idx] == t->type) {
6878*f7c14bbaSAndroid Build Coastguard Worker func_rec = prog->func_info + prog->func_info_rec_size * rec_idx;
6879*f7c14bbaSAndroid Build Coastguard Worker break;
6880*f7c14bbaSAndroid Build Coastguard Worker }
6881*f7c14bbaSAndroid Build Coastguard Worker }
6882*f7c14bbaSAndroid Build Coastguard Worker /* current main program doesn't call into this subprog */
6883*f7c14bbaSAndroid Build Coastguard Worker if (!func_rec)
6884*f7c14bbaSAndroid Build Coastguard Worker continue;
6885*f7c14bbaSAndroid Build Coastguard Worker
6886*f7c14bbaSAndroid Build Coastguard Worker /* some more sanity checking of DECL_TAG */
6887*f7c14bbaSAndroid Build Coastguard Worker arg_cnt = btf_vlen(fn_proto_t);
6888*f7c14bbaSAndroid Build Coastguard Worker arg_idx = btf_decl_tag(t)->component_idx;
6889*f7c14bbaSAndroid Build Coastguard Worker if (arg_idx < 0 || arg_idx >= arg_cnt)
6890*f7c14bbaSAndroid Build Coastguard Worker continue;
6891*f7c14bbaSAndroid Build Coastguard Worker
6892*f7c14bbaSAndroid Build Coastguard Worker /* check if we should fix up argument type */
6893*f7c14bbaSAndroid Build Coastguard Worker p = &btf_params(fn_proto_t)[arg_idx];
6894*f7c14bbaSAndroid Build Coastguard Worker fn_name = btf__str_by_offset(btf, fn_t->name_off) ?: "<anon>";
6895*f7c14bbaSAndroid Build Coastguard Worker if (!need_func_arg_type_fixup(btf, prog, fn_name, arg_idx, p->type, ctx_name))
6896*f7c14bbaSAndroid Build Coastguard Worker continue;
6897*f7c14bbaSAndroid Build Coastguard Worker
6898*f7c14bbaSAndroid Build Coastguard Worker /* clone fn/fn_proto, unless we already did it for another arg */
6899*f7c14bbaSAndroid Build Coastguard Worker if (func_rec->type_id == orig_fn_id) {
6900*f7c14bbaSAndroid Build Coastguard Worker int fn_id;
6901*f7c14bbaSAndroid Build Coastguard Worker
6902*f7c14bbaSAndroid Build Coastguard Worker fn_id = clone_func_btf_info(btf, orig_fn_id, prog);
6903*f7c14bbaSAndroid Build Coastguard Worker if (fn_id < 0) {
6904*f7c14bbaSAndroid Build Coastguard Worker err = fn_id;
6905*f7c14bbaSAndroid Build Coastguard Worker goto err_out;
6906*f7c14bbaSAndroid Build Coastguard Worker }
6907*f7c14bbaSAndroid Build Coastguard Worker
6908*f7c14bbaSAndroid Build Coastguard Worker /* point func_info record to a cloned FUNC type */
6909*f7c14bbaSAndroid Build Coastguard Worker func_rec->type_id = fn_id;
6910*f7c14bbaSAndroid Build Coastguard Worker }
6911*f7c14bbaSAndroid Build Coastguard Worker
6912*f7c14bbaSAndroid Build Coastguard Worker /* create PTR -> STRUCT type chain to mark PTR_TO_CTX argument;
6913*f7c14bbaSAndroid Build Coastguard Worker * we do it just once per main BPF program, as all global
6914*f7c14bbaSAndroid Build Coastguard Worker * funcs share the same program type, so need only PTR ->
6915*f7c14bbaSAndroid Build Coastguard Worker * STRUCT type chain
6916*f7c14bbaSAndroid Build Coastguard Worker */
6917*f7c14bbaSAndroid Build Coastguard Worker if (ptr_id == 0) {
6918*f7c14bbaSAndroid Build Coastguard Worker struct_id = btf__add_struct(btf, ctx_name, 0);
6919*f7c14bbaSAndroid Build Coastguard Worker ptr_id = btf__add_ptr(btf, struct_id);
6920*f7c14bbaSAndroid Build Coastguard Worker if (ptr_id < 0 || struct_id < 0) {
6921*f7c14bbaSAndroid Build Coastguard Worker err = -EINVAL;
6922*f7c14bbaSAndroid Build Coastguard Worker goto err_out;
6923*f7c14bbaSAndroid Build Coastguard Worker }
6924*f7c14bbaSAndroid Build Coastguard Worker }
6925*f7c14bbaSAndroid Build Coastguard Worker
6926*f7c14bbaSAndroid Build Coastguard Worker /* for completeness, clone DECL_TAG and point it to cloned param */
6927*f7c14bbaSAndroid Build Coastguard Worker tag_id = btf__add_decl_tag(btf, ctx_tag, func_rec->type_id, arg_idx);
6928*f7c14bbaSAndroid Build Coastguard Worker if (tag_id < 0) {
6929*f7c14bbaSAndroid Build Coastguard Worker err = -EINVAL;
6930*f7c14bbaSAndroid Build Coastguard Worker goto err_out;
6931*f7c14bbaSAndroid Build Coastguard Worker }
6932*f7c14bbaSAndroid Build Coastguard Worker
6933*f7c14bbaSAndroid Build Coastguard Worker /* all the BTF manipulations invalidated pointers, refetch them */
6934*f7c14bbaSAndroid Build Coastguard Worker fn_t = btf_type_by_id(btf, func_rec->type_id);
6935*f7c14bbaSAndroid Build Coastguard Worker fn_proto_t = btf_type_by_id(btf, fn_t->type);
6936*f7c14bbaSAndroid Build Coastguard Worker
6937*f7c14bbaSAndroid Build Coastguard Worker /* fix up type ID pointed to by param */
6938*f7c14bbaSAndroid Build Coastguard Worker p = &btf_params(fn_proto_t)[arg_idx];
6939*f7c14bbaSAndroid Build Coastguard Worker p->type = ptr_id;
6940*f7c14bbaSAndroid Build Coastguard Worker }
6941*f7c14bbaSAndroid Build Coastguard Worker
6942*f7c14bbaSAndroid Build Coastguard Worker free(orig_ids);
6943*f7c14bbaSAndroid Build Coastguard Worker return 0;
6944*f7c14bbaSAndroid Build Coastguard Worker err_out:
6945*f7c14bbaSAndroid Build Coastguard Worker free(orig_ids);
6946*f7c14bbaSAndroid Build Coastguard Worker return err;
6947*f7c14bbaSAndroid Build Coastguard Worker }
6948*f7c14bbaSAndroid Build Coastguard Worker
bpf_object__relocate(struct bpf_object * obj,const char * targ_btf_path)6949*f7c14bbaSAndroid Build Coastguard Worker static int bpf_object__relocate(struct bpf_object *obj, const char *targ_btf_path)
6950*f7c14bbaSAndroid Build Coastguard Worker {
6951*f7c14bbaSAndroid Build Coastguard Worker struct bpf_program *prog;
6952*f7c14bbaSAndroid Build Coastguard Worker size_t i, j;
6953*f7c14bbaSAndroid Build Coastguard Worker int err;
6954*f7c14bbaSAndroid Build Coastguard Worker
6955*f7c14bbaSAndroid Build Coastguard Worker if (obj->btf_ext) {
6956*f7c14bbaSAndroid Build Coastguard Worker err = bpf_object__relocate_core(obj, targ_btf_path);
6957*f7c14bbaSAndroid Build Coastguard Worker if (err) {
6958*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to perform CO-RE relocations: %d\n",
6959*f7c14bbaSAndroid Build Coastguard Worker err);
6960*f7c14bbaSAndroid Build Coastguard Worker return err;
6961*f7c14bbaSAndroid Build Coastguard Worker }
6962*f7c14bbaSAndroid Build Coastguard Worker bpf_object__sort_relos(obj);
6963*f7c14bbaSAndroid Build Coastguard Worker }
6964*f7c14bbaSAndroid Build Coastguard Worker
6965*f7c14bbaSAndroid Build Coastguard Worker /* Before relocating calls pre-process relocations and mark
6966*f7c14bbaSAndroid Build Coastguard Worker * few ld_imm64 instructions that points to subprogs.
6967*f7c14bbaSAndroid Build Coastguard Worker * Otherwise bpf_object__reloc_code() later would have to consider
6968*f7c14bbaSAndroid Build Coastguard Worker * all ld_imm64 insns as relocation candidates. That would
6969*f7c14bbaSAndroid Build Coastguard Worker * reduce relocation speed, since amount of find_prog_insn_relo()
6970*f7c14bbaSAndroid Build Coastguard Worker * would increase and most of them will fail to find a relo.
6971*f7c14bbaSAndroid Build Coastguard Worker */
6972*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < obj->nr_programs; i++) {
6973*f7c14bbaSAndroid Build Coastguard Worker prog = &obj->programs[i];
6974*f7c14bbaSAndroid Build Coastguard Worker for (j = 0; j < prog->nr_reloc; j++) {
6975*f7c14bbaSAndroid Build Coastguard Worker struct reloc_desc *relo = &prog->reloc_desc[j];
6976*f7c14bbaSAndroid Build Coastguard Worker struct bpf_insn *insn = &prog->insns[relo->insn_idx];
6977*f7c14bbaSAndroid Build Coastguard Worker
6978*f7c14bbaSAndroid Build Coastguard Worker /* mark the insn, so it's recognized by insn_is_pseudo_func() */
6979*f7c14bbaSAndroid Build Coastguard Worker if (relo->type == RELO_SUBPROG_ADDR)
6980*f7c14bbaSAndroid Build Coastguard Worker insn[0].src_reg = BPF_PSEUDO_FUNC;
6981*f7c14bbaSAndroid Build Coastguard Worker }
6982*f7c14bbaSAndroid Build Coastguard Worker }
6983*f7c14bbaSAndroid Build Coastguard Worker
6984*f7c14bbaSAndroid Build Coastguard Worker /* relocate subprogram calls and append used subprograms to main
6985*f7c14bbaSAndroid Build Coastguard Worker * programs; each copy of subprogram code needs to be relocated
6986*f7c14bbaSAndroid Build Coastguard Worker * differently for each main program, because its code location might
6987*f7c14bbaSAndroid Build Coastguard Worker * have changed.
6988*f7c14bbaSAndroid Build Coastguard Worker * Append subprog relos to main programs to allow data relos to be
6989*f7c14bbaSAndroid Build Coastguard Worker * processed after text is completely relocated.
6990*f7c14bbaSAndroid Build Coastguard Worker */
6991*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < obj->nr_programs; i++) {
6992*f7c14bbaSAndroid Build Coastguard Worker prog = &obj->programs[i];
6993*f7c14bbaSAndroid Build Coastguard Worker /* sub-program's sub-calls are relocated within the context of
6994*f7c14bbaSAndroid Build Coastguard Worker * its main program only
6995*f7c14bbaSAndroid Build Coastguard Worker */
6996*f7c14bbaSAndroid Build Coastguard Worker if (prog_is_subprog(obj, prog))
6997*f7c14bbaSAndroid Build Coastguard Worker continue;
6998*f7c14bbaSAndroid Build Coastguard Worker if (!prog->autoload)
6999*f7c14bbaSAndroid Build Coastguard Worker continue;
7000*f7c14bbaSAndroid Build Coastguard Worker
7001*f7c14bbaSAndroid Build Coastguard Worker err = bpf_object__relocate_calls(obj, prog);
7002*f7c14bbaSAndroid Build Coastguard Worker if (err) {
7003*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': failed to relocate calls: %d\n",
7004*f7c14bbaSAndroid Build Coastguard Worker prog->name, err);
7005*f7c14bbaSAndroid Build Coastguard Worker return err;
7006*f7c14bbaSAndroid Build Coastguard Worker }
7007*f7c14bbaSAndroid Build Coastguard Worker
7008*f7c14bbaSAndroid Build Coastguard Worker err = bpf_prog_assign_exc_cb(obj, prog);
7009*f7c14bbaSAndroid Build Coastguard Worker if (err)
7010*f7c14bbaSAndroid Build Coastguard Worker return err;
7011*f7c14bbaSAndroid Build Coastguard Worker /* Now, also append exception callback if it has not been done already. */
7012*f7c14bbaSAndroid Build Coastguard Worker if (prog->exception_cb_idx >= 0) {
7013*f7c14bbaSAndroid Build Coastguard Worker struct bpf_program *subprog = &obj->programs[prog->exception_cb_idx];
7014*f7c14bbaSAndroid Build Coastguard Worker
7015*f7c14bbaSAndroid Build Coastguard Worker /* Calling exception callback directly is disallowed, which the
7016*f7c14bbaSAndroid Build Coastguard Worker * verifier will reject later. In case it was processed already,
7017*f7c14bbaSAndroid Build Coastguard Worker * we can skip this step, otherwise for all other valid cases we
7018*f7c14bbaSAndroid Build Coastguard Worker * have to append exception callback now.
7019*f7c14bbaSAndroid Build Coastguard Worker */
7020*f7c14bbaSAndroid Build Coastguard Worker if (subprog->sub_insn_off == 0) {
7021*f7c14bbaSAndroid Build Coastguard Worker err = bpf_object__append_subprog_code(obj, prog, subprog);
7022*f7c14bbaSAndroid Build Coastguard Worker if (err)
7023*f7c14bbaSAndroid Build Coastguard Worker return err;
7024*f7c14bbaSAndroid Build Coastguard Worker err = bpf_object__reloc_code(obj, prog, subprog);
7025*f7c14bbaSAndroid Build Coastguard Worker if (err)
7026*f7c14bbaSAndroid Build Coastguard Worker return err;
7027*f7c14bbaSAndroid Build Coastguard Worker }
7028*f7c14bbaSAndroid Build Coastguard Worker }
7029*f7c14bbaSAndroid Build Coastguard Worker }
7030*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < obj->nr_programs; i++) {
7031*f7c14bbaSAndroid Build Coastguard Worker prog = &obj->programs[i];
7032*f7c14bbaSAndroid Build Coastguard Worker if (prog_is_subprog(obj, prog))
7033*f7c14bbaSAndroid Build Coastguard Worker continue;
7034*f7c14bbaSAndroid Build Coastguard Worker if (!prog->autoload)
7035*f7c14bbaSAndroid Build Coastguard Worker continue;
7036*f7c14bbaSAndroid Build Coastguard Worker
7037*f7c14bbaSAndroid Build Coastguard Worker /* Process data relos for main programs */
7038*f7c14bbaSAndroid Build Coastguard Worker err = bpf_object__relocate_data(obj, prog);
7039*f7c14bbaSAndroid Build Coastguard Worker if (err) {
7040*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': failed to relocate data references: %d\n",
7041*f7c14bbaSAndroid Build Coastguard Worker prog->name, err);
7042*f7c14bbaSAndroid Build Coastguard Worker return err;
7043*f7c14bbaSAndroid Build Coastguard Worker }
7044*f7c14bbaSAndroid Build Coastguard Worker
7045*f7c14bbaSAndroid Build Coastguard Worker /* Fix up .BTF.ext information, if necessary */
7046*f7c14bbaSAndroid Build Coastguard Worker err = bpf_program_fixup_func_info(obj, prog);
7047*f7c14bbaSAndroid Build Coastguard Worker if (err) {
7048*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': failed to perform .BTF.ext fix ups: %d\n",
7049*f7c14bbaSAndroid Build Coastguard Worker prog->name, err);
7050*f7c14bbaSAndroid Build Coastguard Worker return err;
7051*f7c14bbaSAndroid Build Coastguard Worker }
7052*f7c14bbaSAndroid Build Coastguard Worker }
7053*f7c14bbaSAndroid Build Coastguard Worker
7054*f7c14bbaSAndroid Build Coastguard Worker return 0;
7055*f7c14bbaSAndroid Build Coastguard Worker }
7056*f7c14bbaSAndroid Build Coastguard Worker
7057*f7c14bbaSAndroid Build Coastguard Worker static int bpf_object__collect_st_ops_relos(struct bpf_object *obj,
7058*f7c14bbaSAndroid Build Coastguard Worker Elf64_Shdr *shdr, Elf_Data *data);
7059*f7c14bbaSAndroid Build Coastguard Worker
bpf_object__collect_map_relos(struct bpf_object * obj,Elf64_Shdr * shdr,Elf_Data * data)7060*f7c14bbaSAndroid Build Coastguard Worker static int bpf_object__collect_map_relos(struct bpf_object *obj,
7061*f7c14bbaSAndroid Build Coastguard Worker Elf64_Shdr *shdr, Elf_Data *data)
7062*f7c14bbaSAndroid Build Coastguard Worker {
7063*f7c14bbaSAndroid Build Coastguard Worker const int bpf_ptr_sz = 8, host_ptr_sz = sizeof(void *);
7064*f7c14bbaSAndroid Build Coastguard Worker int i, j, nrels, new_sz;
7065*f7c14bbaSAndroid Build Coastguard Worker const struct btf_var_secinfo *vi = NULL;
7066*f7c14bbaSAndroid Build Coastguard Worker const struct btf_type *sec, *var, *def;
7067*f7c14bbaSAndroid Build Coastguard Worker struct bpf_map *map = NULL, *targ_map = NULL;
7068*f7c14bbaSAndroid Build Coastguard Worker struct bpf_program *targ_prog = NULL;
7069*f7c14bbaSAndroid Build Coastguard Worker bool is_prog_array, is_map_in_map;
7070*f7c14bbaSAndroid Build Coastguard Worker const struct btf_member *member;
7071*f7c14bbaSAndroid Build Coastguard Worker const char *name, *mname, *type;
7072*f7c14bbaSAndroid Build Coastguard Worker unsigned int moff;
7073*f7c14bbaSAndroid Build Coastguard Worker Elf64_Sym *sym;
7074*f7c14bbaSAndroid Build Coastguard Worker Elf64_Rel *rel;
7075*f7c14bbaSAndroid Build Coastguard Worker void *tmp;
7076*f7c14bbaSAndroid Build Coastguard Worker
7077*f7c14bbaSAndroid Build Coastguard Worker if (!obj->efile.btf_maps_sec_btf_id || !obj->btf)
7078*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
7079*f7c14bbaSAndroid Build Coastguard Worker sec = btf__type_by_id(obj->btf, obj->efile.btf_maps_sec_btf_id);
7080*f7c14bbaSAndroid Build Coastguard Worker if (!sec)
7081*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
7082*f7c14bbaSAndroid Build Coastguard Worker
7083*f7c14bbaSAndroid Build Coastguard Worker nrels = shdr->sh_size / shdr->sh_entsize;
7084*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < nrels; i++) {
7085*f7c14bbaSAndroid Build Coastguard Worker rel = elf_rel_by_idx(data, i);
7086*f7c14bbaSAndroid Build Coastguard Worker if (!rel) {
7087*f7c14bbaSAndroid Build Coastguard Worker pr_warn(".maps relo #%d: failed to get ELF relo\n", i);
7088*f7c14bbaSAndroid Build Coastguard Worker return -LIBBPF_ERRNO__FORMAT;
7089*f7c14bbaSAndroid Build Coastguard Worker }
7090*f7c14bbaSAndroid Build Coastguard Worker
7091*f7c14bbaSAndroid Build Coastguard Worker sym = elf_sym_by_idx(obj, ELF64_R_SYM(rel->r_info));
7092*f7c14bbaSAndroid Build Coastguard Worker if (!sym) {
7093*f7c14bbaSAndroid Build Coastguard Worker pr_warn(".maps relo #%d: symbol %zx not found\n",
7094*f7c14bbaSAndroid Build Coastguard Worker i, (size_t)ELF64_R_SYM(rel->r_info));
7095*f7c14bbaSAndroid Build Coastguard Worker return -LIBBPF_ERRNO__FORMAT;
7096*f7c14bbaSAndroid Build Coastguard Worker }
7097*f7c14bbaSAndroid Build Coastguard Worker name = elf_sym_str(obj, sym->st_name) ?: "<?>";
7098*f7c14bbaSAndroid Build Coastguard Worker
7099*f7c14bbaSAndroid Build Coastguard Worker pr_debug(".maps relo #%d: for %zd value %zd rel->r_offset %zu name %d ('%s')\n",
7100*f7c14bbaSAndroid Build Coastguard Worker i, (ssize_t)(rel->r_info >> 32), (size_t)sym->st_value,
7101*f7c14bbaSAndroid Build Coastguard Worker (size_t)rel->r_offset, sym->st_name, name);
7102*f7c14bbaSAndroid Build Coastguard Worker
7103*f7c14bbaSAndroid Build Coastguard Worker for (j = 0; j < obj->nr_maps; j++) {
7104*f7c14bbaSAndroid Build Coastguard Worker map = &obj->maps[j];
7105*f7c14bbaSAndroid Build Coastguard Worker if (map->sec_idx != obj->efile.btf_maps_shndx)
7106*f7c14bbaSAndroid Build Coastguard Worker continue;
7107*f7c14bbaSAndroid Build Coastguard Worker
7108*f7c14bbaSAndroid Build Coastguard Worker vi = btf_var_secinfos(sec) + map->btf_var_idx;
7109*f7c14bbaSAndroid Build Coastguard Worker if (vi->offset <= rel->r_offset &&
7110*f7c14bbaSAndroid Build Coastguard Worker rel->r_offset + bpf_ptr_sz <= vi->offset + vi->size)
7111*f7c14bbaSAndroid Build Coastguard Worker break;
7112*f7c14bbaSAndroid Build Coastguard Worker }
7113*f7c14bbaSAndroid Build Coastguard Worker if (j == obj->nr_maps) {
7114*f7c14bbaSAndroid Build Coastguard Worker pr_warn(".maps relo #%d: cannot find map '%s' at rel->r_offset %zu\n",
7115*f7c14bbaSAndroid Build Coastguard Worker i, name, (size_t)rel->r_offset);
7116*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
7117*f7c14bbaSAndroid Build Coastguard Worker }
7118*f7c14bbaSAndroid Build Coastguard Worker
7119*f7c14bbaSAndroid Build Coastguard Worker is_map_in_map = bpf_map_type__is_map_in_map(map->def.type);
7120*f7c14bbaSAndroid Build Coastguard Worker is_prog_array = map->def.type == BPF_MAP_TYPE_PROG_ARRAY;
7121*f7c14bbaSAndroid Build Coastguard Worker type = is_map_in_map ? "map" : "prog";
7122*f7c14bbaSAndroid Build Coastguard Worker if (is_map_in_map) {
7123*f7c14bbaSAndroid Build Coastguard Worker if (sym->st_shndx != obj->efile.btf_maps_shndx) {
7124*f7c14bbaSAndroid Build Coastguard Worker pr_warn(".maps relo #%d: '%s' isn't a BTF-defined map\n",
7125*f7c14bbaSAndroid Build Coastguard Worker i, name);
7126*f7c14bbaSAndroid Build Coastguard Worker return -LIBBPF_ERRNO__RELOC;
7127*f7c14bbaSAndroid Build Coastguard Worker }
7128*f7c14bbaSAndroid Build Coastguard Worker if (map->def.type == BPF_MAP_TYPE_HASH_OF_MAPS &&
7129*f7c14bbaSAndroid Build Coastguard Worker map->def.key_size != sizeof(int)) {
7130*f7c14bbaSAndroid Build Coastguard Worker pr_warn(".maps relo #%d: hash-of-maps '%s' should have key size %zu.\n",
7131*f7c14bbaSAndroid Build Coastguard Worker i, map->name, sizeof(int));
7132*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
7133*f7c14bbaSAndroid Build Coastguard Worker }
7134*f7c14bbaSAndroid Build Coastguard Worker targ_map = bpf_object__find_map_by_name(obj, name);
7135*f7c14bbaSAndroid Build Coastguard Worker if (!targ_map) {
7136*f7c14bbaSAndroid Build Coastguard Worker pr_warn(".maps relo #%d: '%s' isn't a valid map reference\n",
7137*f7c14bbaSAndroid Build Coastguard Worker i, name);
7138*f7c14bbaSAndroid Build Coastguard Worker return -ESRCH;
7139*f7c14bbaSAndroid Build Coastguard Worker }
7140*f7c14bbaSAndroid Build Coastguard Worker } else if (is_prog_array) {
7141*f7c14bbaSAndroid Build Coastguard Worker targ_prog = bpf_object__find_program_by_name(obj, name);
7142*f7c14bbaSAndroid Build Coastguard Worker if (!targ_prog) {
7143*f7c14bbaSAndroid Build Coastguard Worker pr_warn(".maps relo #%d: '%s' isn't a valid program reference\n",
7144*f7c14bbaSAndroid Build Coastguard Worker i, name);
7145*f7c14bbaSAndroid Build Coastguard Worker return -ESRCH;
7146*f7c14bbaSAndroid Build Coastguard Worker }
7147*f7c14bbaSAndroid Build Coastguard Worker if (targ_prog->sec_idx != sym->st_shndx ||
7148*f7c14bbaSAndroid Build Coastguard Worker targ_prog->sec_insn_off * 8 != sym->st_value ||
7149*f7c14bbaSAndroid Build Coastguard Worker prog_is_subprog(obj, targ_prog)) {
7150*f7c14bbaSAndroid Build Coastguard Worker pr_warn(".maps relo #%d: '%s' isn't an entry-point program\n",
7151*f7c14bbaSAndroid Build Coastguard Worker i, name);
7152*f7c14bbaSAndroid Build Coastguard Worker return -LIBBPF_ERRNO__RELOC;
7153*f7c14bbaSAndroid Build Coastguard Worker }
7154*f7c14bbaSAndroid Build Coastguard Worker } else {
7155*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
7156*f7c14bbaSAndroid Build Coastguard Worker }
7157*f7c14bbaSAndroid Build Coastguard Worker
7158*f7c14bbaSAndroid Build Coastguard Worker var = btf__type_by_id(obj->btf, vi->type);
7159*f7c14bbaSAndroid Build Coastguard Worker def = skip_mods_and_typedefs(obj->btf, var->type, NULL);
7160*f7c14bbaSAndroid Build Coastguard Worker if (btf_vlen(def) == 0)
7161*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
7162*f7c14bbaSAndroid Build Coastguard Worker member = btf_members(def) + btf_vlen(def) - 1;
7163*f7c14bbaSAndroid Build Coastguard Worker mname = btf__name_by_offset(obj->btf, member->name_off);
7164*f7c14bbaSAndroid Build Coastguard Worker if (strcmp(mname, "values"))
7165*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
7166*f7c14bbaSAndroid Build Coastguard Worker
7167*f7c14bbaSAndroid Build Coastguard Worker moff = btf_member_bit_offset(def, btf_vlen(def) - 1) / 8;
7168*f7c14bbaSAndroid Build Coastguard Worker if (rel->r_offset - vi->offset < moff)
7169*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
7170*f7c14bbaSAndroid Build Coastguard Worker
7171*f7c14bbaSAndroid Build Coastguard Worker moff = rel->r_offset - vi->offset - moff;
7172*f7c14bbaSAndroid Build Coastguard Worker /* here we use BPF pointer size, which is always 64 bit, as we
7173*f7c14bbaSAndroid Build Coastguard Worker * are parsing ELF that was built for BPF target
7174*f7c14bbaSAndroid Build Coastguard Worker */
7175*f7c14bbaSAndroid Build Coastguard Worker if (moff % bpf_ptr_sz)
7176*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
7177*f7c14bbaSAndroid Build Coastguard Worker moff /= bpf_ptr_sz;
7178*f7c14bbaSAndroid Build Coastguard Worker if (moff >= map->init_slots_sz) {
7179*f7c14bbaSAndroid Build Coastguard Worker new_sz = moff + 1;
7180*f7c14bbaSAndroid Build Coastguard Worker tmp = libbpf_reallocarray(map->init_slots, new_sz, host_ptr_sz);
7181*f7c14bbaSAndroid Build Coastguard Worker if (!tmp)
7182*f7c14bbaSAndroid Build Coastguard Worker return -ENOMEM;
7183*f7c14bbaSAndroid Build Coastguard Worker map->init_slots = tmp;
7184*f7c14bbaSAndroid Build Coastguard Worker memset(map->init_slots + map->init_slots_sz, 0,
7185*f7c14bbaSAndroid Build Coastguard Worker (new_sz - map->init_slots_sz) * host_ptr_sz);
7186*f7c14bbaSAndroid Build Coastguard Worker map->init_slots_sz = new_sz;
7187*f7c14bbaSAndroid Build Coastguard Worker }
7188*f7c14bbaSAndroid Build Coastguard Worker map->init_slots[moff] = is_map_in_map ? (void *)targ_map : (void *)targ_prog;
7189*f7c14bbaSAndroid Build Coastguard Worker
7190*f7c14bbaSAndroid Build Coastguard Worker pr_debug(".maps relo #%d: map '%s' slot [%d] points to %s '%s'\n",
7191*f7c14bbaSAndroid Build Coastguard Worker i, map->name, moff, type, name);
7192*f7c14bbaSAndroid Build Coastguard Worker }
7193*f7c14bbaSAndroid Build Coastguard Worker
7194*f7c14bbaSAndroid Build Coastguard Worker return 0;
7195*f7c14bbaSAndroid Build Coastguard Worker }
7196*f7c14bbaSAndroid Build Coastguard Worker
bpf_object__collect_relos(struct bpf_object * obj)7197*f7c14bbaSAndroid Build Coastguard Worker static int bpf_object__collect_relos(struct bpf_object *obj)
7198*f7c14bbaSAndroid Build Coastguard Worker {
7199*f7c14bbaSAndroid Build Coastguard Worker int i, err;
7200*f7c14bbaSAndroid Build Coastguard Worker
7201*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < obj->efile.sec_cnt; i++) {
7202*f7c14bbaSAndroid Build Coastguard Worker struct elf_sec_desc *sec_desc = &obj->efile.secs[i];
7203*f7c14bbaSAndroid Build Coastguard Worker Elf64_Shdr *shdr;
7204*f7c14bbaSAndroid Build Coastguard Worker Elf_Data *data;
7205*f7c14bbaSAndroid Build Coastguard Worker int idx;
7206*f7c14bbaSAndroid Build Coastguard Worker
7207*f7c14bbaSAndroid Build Coastguard Worker if (sec_desc->sec_type != SEC_RELO)
7208*f7c14bbaSAndroid Build Coastguard Worker continue;
7209*f7c14bbaSAndroid Build Coastguard Worker
7210*f7c14bbaSAndroid Build Coastguard Worker shdr = sec_desc->shdr;
7211*f7c14bbaSAndroid Build Coastguard Worker data = sec_desc->data;
7212*f7c14bbaSAndroid Build Coastguard Worker idx = shdr->sh_info;
7213*f7c14bbaSAndroid Build Coastguard Worker
7214*f7c14bbaSAndroid Build Coastguard Worker if (shdr->sh_type != SHT_REL || idx < 0 || idx >= obj->efile.sec_cnt) {
7215*f7c14bbaSAndroid Build Coastguard Worker pr_warn("internal error at %d\n", __LINE__);
7216*f7c14bbaSAndroid Build Coastguard Worker return -LIBBPF_ERRNO__INTERNAL;
7217*f7c14bbaSAndroid Build Coastguard Worker }
7218*f7c14bbaSAndroid Build Coastguard Worker
7219*f7c14bbaSAndroid Build Coastguard Worker if (obj->efile.secs[idx].sec_type == SEC_ST_OPS)
7220*f7c14bbaSAndroid Build Coastguard Worker err = bpf_object__collect_st_ops_relos(obj, shdr, data);
7221*f7c14bbaSAndroid Build Coastguard Worker else if (idx == obj->efile.btf_maps_shndx)
7222*f7c14bbaSAndroid Build Coastguard Worker err = bpf_object__collect_map_relos(obj, shdr, data);
7223*f7c14bbaSAndroid Build Coastguard Worker else
7224*f7c14bbaSAndroid Build Coastguard Worker err = bpf_object__collect_prog_relos(obj, shdr, data);
7225*f7c14bbaSAndroid Build Coastguard Worker if (err)
7226*f7c14bbaSAndroid Build Coastguard Worker return err;
7227*f7c14bbaSAndroid Build Coastguard Worker }
7228*f7c14bbaSAndroid Build Coastguard Worker
7229*f7c14bbaSAndroid Build Coastguard Worker bpf_object__sort_relos(obj);
7230*f7c14bbaSAndroid Build Coastguard Worker return 0;
7231*f7c14bbaSAndroid Build Coastguard Worker }
7232*f7c14bbaSAndroid Build Coastguard Worker
insn_is_helper_call(struct bpf_insn * insn,enum bpf_func_id * func_id)7233*f7c14bbaSAndroid Build Coastguard Worker static bool insn_is_helper_call(struct bpf_insn *insn, enum bpf_func_id *func_id)
7234*f7c14bbaSAndroid Build Coastguard Worker {
7235*f7c14bbaSAndroid Build Coastguard Worker if (BPF_CLASS(insn->code) == BPF_JMP &&
7236*f7c14bbaSAndroid Build Coastguard Worker BPF_OP(insn->code) == BPF_CALL &&
7237*f7c14bbaSAndroid Build Coastguard Worker BPF_SRC(insn->code) == BPF_K &&
7238*f7c14bbaSAndroid Build Coastguard Worker insn->src_reg == 0 &&
7239*f7c14bbaSAndroid Build Coastguard Worker insn->dst_reg == 0) {
7240*f7c14bbaSAndroid Build Coastguard Worker *func_id = insn->imm;
7241*f7c14bbaSAndroid Build Coastguard Worker return true;
7242*f7c14bbaSAndroid Build Coastguard Worker }
7243*f7c14bbaSAndroid Build Coastguard Worker return false;
7244*f7c14bbaSAndroid Build Coastguard Worker }
7245*f7c14bbaSAndroid Build Coastguard Worker
bpf_object__sanitize_prog(struct bpf_object * obj,struct bpf_program * prog)7246*f7c14bbaSAndroid Build Coastguard Worker static int bpf_object__sanitize_prog(struct bpf_object *obj, struct bpf_program *prog)
7247*f7c14bbaSAndroid Build Coastguard Worker {
7248*f7c14bbaSAndroid Build Coastguard Worker struct bpf_insn *insn = prog->insns;
7249*f7c14bbaSAndroid Build Coastguard Worker enum bpf_func_id func_id;
7250*f7c14bbaSAndroid Build Coastguard Worker int i;
7251*f7c14bbaSAndroid Build Coastguard Worker
7252*f7c14bbaSAndroid Build Coastguard Worker if (obj->gen_loader)
7253*f7c14bbaSAndroid Build Coastguard Worker return 0;
7254*f7c14bbaSAndroid Build Coastguard Worker
7255*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < prog->insns_cnt; i++, insn++) {
7256*f7c14bbaSAndroid Build Coastguard Worker if (!insn_is_helper_call(insn, &func_id))
7257*f7c14bbaSAndroid Build Coastguard Worker continue;
7258*f7c14bbaSAndroid Build Coastguard Worker
7259*f7c14bbaSAndroid Build Coastguard Worker /* on kernels that don't yet support
7260*f7c14bbaSAndroid Build Coastguard Worker * bpf_probe_read_{kernel,user}[_str] helpers, fall back
7261*f7c14bbaSAndroid Build Coastguard Worker * to bpf_probe_read() which works well for old kernels
7262*f7c14bbaSAndroid Build Coastguard Worker */
7263*f7c14bbaSAndroid Build Coastguard Worker switch (func_id) {
7264*f7c14bbaSAndroid Build Coastguard Worker case BPF_FUNC_probe_read_kernel:
7265*f7c14bbaSAndroid Build Coastguard Worker case BPF_FUNC_probe_read_user:
7266*f7c14bbaSAndroid Build Coastguard Worker if (!kernel_supports(obj, FEAT_PROBE_READ_KERN))
7267*f7c14bbaSAndroid Build Coastguard Worker insn->imm = BPF_FUNC_probe_read;
7268*f7c14bbaSAndroid Build Coastguard Worker break;
7269*f7c14bbaSAndroid Build Coastguard Worker case BPF_FUNC_probe_read_kernel_str:
7270*f7c14bbaSAndroid Build Coastguard Worker case BPF_FUNC_probe_read_user_str:
7271*f7c14bbaSAndroid Build Coastguard Worker if (!kernel_supports(obj, FEAT_PROBE_READ_KERN))
7272*f7c14bbaSAndroid Build Coastguard Worker insn->imm = BPF_FUNC_probe_read_str;
7273*f7c14bbaSAndroid Build Coastguard Worker break;
7274*f7c14bbaSAndroid Build Coastguard Worker default:
7275*f7c14bbaSAndroid Build Coastguard Worker break;
7276*f7c14bbaSAndroid Build Coastguard Worker }
7277*f7c14bbaSAndroid Build Coastguard Worker }
7278*f7c14bbaSAndroid Build Coastguard Worker return 0;
7279*f7c14bbaSAndroid Build Coastguard Worker }
7280*f7c14bbaSAndroid Build Coastguard Worker
7281*f7c14bbaSAndroid Build Coastguard Worker static int libbpf_find_attach_btf_id(struct bpf_program *prog, const char *attach_name,
7282*f7c14bbaSAndroid Build Coastguard Worker int *btf_obj_fd, int *btf_type_id);
7283*f7c14bbaSAndroid Build Coastguard Worker
7284*f7c14bbaSAndroid Build Coastguard Worker /* this is called as prog->sec_def->prog_prepare_load_fn for libbpf-supported sec_defs */
libbpf_prepare_prog_load(struct bpf_program * prog,struct bpf_prog_load_opts * opts,long cookie)7285*f7c14bbaSAndroid Build Coastguard Worker static int libbpf_prepare_prog_load(struct bpf_program *prog,
7286*f7c14bbaSAndroid Build Coastguard Worker struct bpf_prog_load_opts *opts, long cookie)
7287*f7c14bbaSAndroid Build Coastguard Worker {
7288*f7c14bbaSAndroid Build Coastguard Worker enum sec_def_flags def = cookie;
7289*f7c14bbaSAndroid Build Coastguard Worker
7290*f7c14bbaSAndroid Build Coastguard Worker /* old kernels might not support specifying expected_attach_type */
7291*f7c14bbaSAndroid Build Coastguard Worker if ((def & SEC_EXP_ATTACH_OPT) && !kernel_supports(prog->obj, FEAT_EXP_ATTACH_TYPE))
7292*f7c14bbaSAndroid Build Coastguard Worker opts->expected_attach_type = 0;
7293*f7c14bbaSAndroid Build Coastguard Worker
7294*f7c14bbaSAndroid Build Coastguard Worker if (def & SEC_SLEEPABLE)
7295*f7c14bbaSAndroid Build Coastguard Worker opts->prog_flags |= BPF_F_SLEEPABLE;
7296*f7c14bbaSAndroid Build Coastguard Worker
7297*f7c14bbaSAndroid Build Coastguard Worker if (prog->type == BPF_PROG_TYPE_XDP && (def & SEC_XDP_FRAGS))
7298*f7c14bbaSAndroid Build Coastguard Worker opts->prog_flags |= BPF_F_XDP_HAS_FRAGS;
7299*f7c14bbaSAndroid Build Coastguard Worker
7300*f7c14bbaSAndroid Build Coastguard Worker /* special check for usdt to use uprobe_multi link */
7301*f7c14bbaSAndroid Build Coastguard Worker if ((def & SEC_USDT) && kernel_supports(prog->obj, FEAT_UPROBE_MULTI_LINK))
7302*f7c14bbaSAndroid Build Coastguard Worker prog->expected_attach_type = BPF_TRACE_UPROBE_MULTI;
7303*f7c14bbaSAndroid Build Coastguard Worker
7304*f7c14bbaSAndroid Build Coastguard Worker if ((def & SEC_ATTACH_BTF) && !prog->attach_btf_id) {
7305*f7c14bbaSAndroid Build Coastguard Worker int btf_obj_fd = 0, btf_type_id = 0, err;
7306*f7c14bbaSAndroid Build Coastguard Worker const char *attach_name;
7307*f7c14bbaSAndroid Build Coastguard Worker
7308*f7c14bbaSAndroid Build Coastguard Worker attach_name = strchr(prog->sec_name, '/');
7309*f7c14bbaSAndroid Build Coastguard Worker if (!attach_name) {
7310*f7c14bbaSAndroid Build Coastguard Worker /* if BPF program is annotated with just SEC("fentry")
7311*f7c14bbaSAndroid Build Coastguard Worker * (or similar) without declaratively specifying
7312*f7c14bbaSAndroid Build Coastguard Worker * target, then it is expected that target will be
7313*f7c14bbaSAndroid Build Coastguard Worker * specified with bpf_program__set_attach_target() at
7314*f7c14bbaSAndroid Build Coastguard Worker * runtime before BPF object load step. If not, then
7315*f7c14bbaSAndroid Build Coastguard Worker * there is nothing to load into the kernel as BPF
7316*f7c14bbaSAndroid Build Coastguard Worker * verifier won't be able to validate BPF program
7317*f7c14bbaSAndroid Build Coastguard Worker * correctness anyways.
7318*f7c14bbaSAndroid Build Coastguard Worker */
7319*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': no BTF-based attach target is specified, use bpf_program__set_attach_target()\n",
7320*f7c14bbaSAndroid Build Coastguard Worker prog->name);
7321*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
7322*f7c14bbaSAndroid Build Coastguard Worker }
7323*f7c14bbaSAndroid Build Coastguard Worker attach_name++; /* skip over / */
7324*f7c14bbaSAndroid Build Coastguard Worker
7325*f7c14bbaSAndroid Build Coastguard Worker err = libbpf_find_attach_btf_id(prog, attach_name, &btf_obj_fd, &btf_type_id);
7326*f7c14bbaSAndroid Build Coastguard Worker if (err)
7327*f7c14bbaSAndroid Build Coastguard Worker return err;
7328*f7c14bbaSAndroid Build Coastguard Worker
7329*f7c14bbaSAndroid Build Coastguard Worker /* cache resolved BTF FD and BTF type ID in the prog */
7330*f7c14bbaSAndroid Build Coastguard Worker prog->attach_btf_obj_fd = btf_obj_fd;
7331*f7c14bbaSAndroid Build Coastguard Worker prog->attach_btf_id = btf_type_id;
7332*f7c14bbaSAndroid Build Coastguard Worker
7333*f7c14bbaSAndroid Build Coastguard Worker /* but by now libbpf common logic is not utilizing
7334*f7c14bbaSAndroid Build Coastguard Worker * prog->atach_btf_obj_fd/prog->attach_btf_id anymore because
7335*f7c14bbaSAndroid Build Coastguard Worker * this callback is called after opts were populated by
7336*f7c14bbaSAndroid Build Coastguard Worker * libbpf, so this callback has to update opts explicitly here
7337*f7c14bbaSAndroid Build Coastguard Worker */
7338*f7c14bbaSAndroid Build Coastguard Worker opts->attach_btf_obj_fd = btf_obj_fd;
7339*f7c14bbaSAndroid Build Coastguard Worker opts->attach_btf_id = btf_type_id;
7340*f7c14bbaSAndroid Build Coastguard Worker }
7341*f7c14bbaSAndroid Build Coastguard Worker return 0;
7342*f7c14bbaSAndroid Build Coastguard Worker }
7343*f7c14bbaSAndroid Build Coastguard Worker
7344*f7c14bbaSAndroid Build Coastguard Worker static void fixup_verifier_log(struct bpf_program *prog, char *buf, size_t buf_sz);
7345*f7c14bbaSAndroid Build Coastguard Worker
bpf_object_load_prog(struct bpf_object * obj,struct bpf_program * prog,struct bpf_insn * insns,int insns_cnt,const char * license,__u32 kern_version,int * prog_fd)7346*f7c14bbaSAndroid Build Coastguard Worker static int bpf_object_load_prog(struct bpf_object *obj, struct bpf_program *prog,
7347*f7c14bbaSAndroid Build Coastguard Worker struct bpf_insn *insns, int insns_cnt,
7348*f7c14bbaSAndroid Build Coastguard Worker const char *license, __u32 kern_version, int *prog_fd)
7349*f7c14bbaSAndroid Build Coastguard Worker {
7350*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_OPTS(bpf_prog_load_opts, load_attr);
7351*f7c14bbaSAndroid Build Coastguard Worker const char *prog_name = NULL;
7352*f7c14bbaSAndroid Build Coastguard Worker char *cp, errmsg[STRERR_BUFSIZE];
7353*f7c14bbaSAndroid Build Coastguard Worker size_t log_buf_size = 0;
7354*f7c14bbaSAndroid Build Coastguard Worker char *log_buf = NULL, *tmp;
7355*f7c14bbaSAndroid Build Coastguard Worker bool own_log_buf = true;
7356*f7c14bbaSAndroid Build Coastguard Worker __u32 log_level = prog->log_level;
7357*f7c14bbaSAndroid Build Coastguard Worker int ret, err;
7358*f7c14bbaSAndroid Build Coastguard Worker
7359*f7c14bbaSAndroid Build Coastguard Worker /* Be more helpful by rejecting programs that can't be validated early
7360*f7c14bbaSAndroid Build Coastguard Worker * with more meaningful and actionable error message.
7361*f7c14bbaSAndroid Build Coastguard Worker */
7362*f7c14bbaSAndroid Build Coastguard Worker switch (prog->type) {
7363*f7c14bbaSAndroid Build Coastguard Worker case BPF_PROG_TYPE_UNSPEC:
7364*f7c14bbaSAndroid Build Coastguard Worker /*
7365*f7c14bbaSAndroid Build Coastguard Worker * The program type must be set. Most likely we couldn't find a proper
7366*f7c14bbaSAndroid Build Coastguard Worker * section definition at load time, and thus we didn't infer the type.
7367*f7c14bbaSAndroid Build Coastguard Worker */
7368*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': missing BPF prog type, check ELF section name '%s'\n",
7369*f7c14bbaSAndroid Build Coastguard Worker prog->name, prog->sec_name);
7370*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
7371*f7c14bbaSAndroid Build Coastguard Worker case BPF_PROG_TYPE_STRUCT_OPS:
7372*f7c14bbaSAndroid Build Coastguard Worker if (prog->attach_btf_id == 0) {
7373*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': SEC(\"struct_ops\") program isn't referenced anywhere, did you forget to use it?\n",
7374*f7c14bbaSAndroid Build Coastguard Worker prog->name);
7375*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
7376*f7c14bbaSAndroid Build Coastguard Worker }
7377*f7c14bbaSAndroid Build Coastguard Worker break;
7378*f7c14bbaSAndroid Build Coastguard Worker default:
7379*f7c14bbaSAndroid Build Coastguard Worker break;
7380*f7c14bbaSAndroid Build Coastguard Worker }
7381*f7c14bbaSAndroid Build Coastguard Worker
7382*f7c14bbaSAndroid Build Coastguard Worker if (!insns || !insns_cnt)
7383*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
7384*f7c14bbaSAndroid Build Coastguard Worker
7385*f7c14bbaSAndroid Build Coastguard Worker if (kernel_supports(obj, FEAT_PROG_NAME))
7386*f7c14bbaSAndroid Build Coastguard Worker prog_name = prog->name;
7387*f7c14bbaSAndroid Build Coastguard Worker load_attr.attach_prog_fd = prog->attach_prog_fd;
7388*f7c14bbaSAndroid Build Coastguard Worker load_attr.attach_btf_obj_fd = prog->attach_btf_obj_fd;
7389*f7c14bbaSAndroid Build Coastguard Worker load_attr.attach_btf_id = prog->attach_btf_id;
7390*f7c14bbaSAndroid Build Coastguard Worker load_attr.kern_version = kern_version;
7391*f7c14bbaSAndroid Build Coastguard Worker load_attr.prog_ifindex = prog->prog_ifindex;
7392*f7c14bbaSAndroid Build Coastguard Worker
7393*f7c14bbaSAndroid Build Coastguard Worker /* specify func_info/line_info only if kernel supports them */
7394*f7c14bbaSAndroid Build Coastguard Worker if (obj->btf && btf__fd(obj->btf) >= 0 && kernel_supports(obj, FEAT_BTF_FUNC)) {
7395*f7c14bbaSAndroid Build Coastguard Worker load_attr.prog_btf_fd = btf__fd(obj->btf);
7396*f7c14bbaSAndroid Build Coastguard Worker load_attr.func_info = prog->func_info;
7397*f7c14bbaSAndroid Build Coastguard Worker load_attr.func_info_rec_size = prog->func_info_rec_size;
7398*f7c14bbaSAndroid Build Coastguard Worker load_attr.func_info_cnt = prog->func_info_cnt;
7399*f7c14bbaSAndroid Build Coastguard Worker load_attr.line_info = prog->line_info;
7400*f7c14bbaSAndroid Build Coastguard Worker load_attr.line_info_rec_size = prog->line_info_rec_size;
7401*f7c14bbaSAndroid Build Coastguard Worker load_attr.line_info_cnt = prog->line_info_cnt;
7402*f7c14bbaSAndroid Build Coastguard Worker }
7403*f7c14bbaSAndroid Build Coastguard Worker load_attr.log_level = log_level;
7404*f7c14bbaSAndroid Build Coastguard Worker load_attr.prog_flags = prog->prog_flags;
7405*f7c14bbaSAndroid Build Coastguard Worker load_attr.fd_array = obj->fd_array;
7406*f7c14bbaSAndroid Build Coastguard Worker
7407*f7c14bbaSAndroid Build Coastguard Worker load_attr.token_fd = obj->token_fd;
7408*f7c14bbaSAndroid Build Coastguard Worker if (obj->token_fd)
7409*f7c14bbaSAndroid Build Coastguard Worker load_attr.prog_flags |= BPF_F_TOKEN_FD;
7410*f7c14bbaSAndroid Build Coastguard Worker
7411*f7c14bbaSAndroid Build Coastguard Worker /* adjust load_attr if sec_def provides custom preload callback */
7412*f7c14bbaSAndroid Build Coastguard Worker if (prog->sec_def && prog->sec_def->prog_prepare_load_fn) {
7413*f7c14bbaSAndroid Build Coastguard Worker err = prog->sec_def->prog_prepare_load_fn(prog, &load_attr, prog->sec_def->cookie);
7414*f7c14bbaSAndroid Build Coastguard Worker if (err < 0) {
7415*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': failed to prepare load attributes: %d\n",
7416*f7c14bbaSAndroid Build Coastguard Worker prog->name, err);
7417*f7c14bbaSAndroid Build Coastguard Worker return err;
7418*f7c14bbaSAndroid Build Coastguard Worker }
7419*f7c14bbaSAndroid Build Coastguard Worker insns = prog->insns;
7420*f7c14bbaSAndroid Build Coastguard Worker insns_cnt = prog->insns_cnt;
7421*f7c14bbaSAndroid Build Coastguard Worker }
7422*f7c14bbaSAndroid Build Coastguard Worker
7423*f7c14bbaSAndroid Build Coastguard Worker /* allow prog_prepare_load_fn to change expected_attach_type */
7424*f7c14bbaSAndroid Build Coastguard Worker load_attr.expected_attach_type = prog->expected_attach_type;
7425*f7c14bbaSAndroid Build Coastguard Worker
7426*f7c14bbaSAndroid Build Coastguard Worker if (obj->gen_loader) {
7427*f7c14bbaSAndroid Build Coastguard Worker bpf_gen__prog_load(obj->gen_loader, prog->type, prog->name,
7428*f7c14bbaSAndroid Build Coastguard Worker license, insns, insns_cnt, &load_attr,
7429*f7c14bbaSAndroid Build Coastguard Worker prog - obj->programs);
7430*f7c14bbaSAndroid Build Coastguard Worker *prog_fd = -1;
7431*f7c14bbaSAndroid Build Coastguard Worker return 0;
7432*f7c14bbaSAndroid Build Coastguard Worker }
7433*f7c14bbaSAndroid Build Coastguard Worker
7434*f7c14bbaSAndroid Build Coastguard Worker retry_load:
7435*f7c14bbaSAndroid Build Coastguard Worker /* if log_level is zero, we don't request logs initially even if
7436*f7c14bbaSAndroid Build Coastguard Worker * custom log_buf is specified; if the program load fails, then we'll
7437*f7c14bbaSAndroid Build Coastguard Worker * bump log_level to 1 and use either custom log_buf or we'll allocate
7438*f7c14bbaSAndroid Build Coastguard Worker * our own and retry the load to get details on what failed
7439*f7c14bbaSAndroid Build Coastguard Worker */
7440*f7c14bbaSAndroid Build Coastguard Worker if (log_level) {
7441*f7c14bbaSAndroid Build Coastguard Worker if (prog->log_buf) {
7442*f7c14bbaSAndroid Build Coastguard Worker log_buf = prog->log_buf;
7443*f7c14bbaSAndroid Build Coastguard Worker log_buf_size = prog->log_size;
7444*f7c14bbaSAndroid Build Coastguard Worker own_log_buf = false;
7445*f7c14bbaSAndroid Build Coastguard Worker } else if (obj->log_buf) {
7446*f7c14bbaSAndroid Build Coastguard Worker log_buf = obj->log_buf;
7447*f7c14bbaSAndroid Build Coastguard Worker log_buf_size = obj->log_size;
7448*f7c14bbaSAndroid Build Coastguard Worker own_log_buf = false;
7449*f7c14bbaSAndroid Build Coastguard Worker } else {
7450*f7c14bbaSAndroid Build Coastguard Worker log_buf_size = max((size_t)BPF_LOG_BUF_SIZE, log_buf_size * 2);
7451*f7c14bbaSAndroid Build Coastguard Worker tmp = realloc(log_buf, log_buf_size);
7452*f7c14bbaSAndroid Build Coastguard Worker if (!tmp) {
7453*f7c14bbaSAndroid Build Coastguard Worker ret = -ENOMEM;
7454*f7c14bbaSAndroid Build Coastguard Worker goto out;
7455*f7c14bbaSAndroid Build Coastguard Worker }
7456*f7c14bbaSAndroid Build Coastguard Worker log_buf = tmp;
7457*f7c14bbaSAndroid Build Coastguard Worker log_buf[0] = '\0';
7458*f7c14bbaSAndroid Build Coastguard Worker own_log_buf = true;
7459*f7c14bbaSAndroid Build Coastguard Worker }
7460*f7c14bbaSAndroid Build Coastguard Worker }
7461*f7c14bbaSAndroid Build Coastguard Worker
7462*f7c14bbaSAndroid Build Coastguard Worker load_attr.log_buf = log_buf;
7463*f7c14bbaSAndroid Build Coastguard Worker load_attr.log_size = log_buf_size;
7464*f7c14bbaSAndroid Build Coastguard Worker load_attr.log_level = log_level;
7465*f7c14bbaSAndroid Build Coastguard Worker
7466*f7c14bbaSAndroid Build Coastguard Worker ret = bpf_prog_load(prog->type, prog_name, license, insns, insns_cnt, &load_attr);
7467*f7c14bbaSAndroid Build Coastguard Worker if (ret >= 0) {
7468*f7c14bbaSAndroid Build Coastguard Worker if (log_level && own_log_buf) {
7469*f7c14bbaSAndroid Build Coastguard Worker pr_debug("prog '%s': -- BEGIN PROG LOAD LOG --\n%s-- END PROG LOAD LOG --\n",
7470*f7c14bbaSAndroid Build Coastguard Worker prog->name, log_buf);
7471*f7c14bbaSAndroid Build Coastguard Worker }
7472*f7c14bbaSAndroid Build Coastguard Worker
7473*f7c14bbaSAndroid Build Coastguard Worker if (obj->has_rodata && kernel_supports(obj, FEAT_PROG_BIND_MAP)) {
7474*f7c14bbaSAndroid Build Coastguard Worker struct bpf_map *map;
7475*f7c14bbaSAndroid Build Coastguard Worker int i;
7476*f7c14bbaSAndroid Build Coastguard Worker
7477*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < obj->nr_maps; i++) {
7478*f7c14bbaSAndroid Build Coastguard Worker map = &prog->obj->maps[i];
7479*f7c14bbaSAndroid Build Coastguard Worker if (map->libbpf_type != LIBBPF_MAP_RODATA)
7480*f7c14bbaSAndroid Build Coastguard Worker continue;
7481*f7c14bbaSAndroid Build Coastguard Worker
7482*f7c14bbaSAndroid Build Coastguard Worker if (bpf_prog_bind_map(ret, map->fd, NULL)) {
7483*f7c14bbaSAndroid Build Coastguard Worker cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
7484*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': failed to bind map '%s': %s\n",
7485*f7c14bbaSAndroid Build Coastguard Worker prog->name, map->real_name, cp);
7486*f7c14bbaSAndroid Build Coastguard Worker /* Don't fail hard if can't bind rodata. */
7487*f7c14bbaSAndroid Build Coastguard Worker }
7488*f7c14bbaSAndroid Build Coastguard Worker }
7489*f7c14bbaSAndroid Build Coastguard Worker }
7490*f7c14bbaSAndroid Build Coastguard Worker
7491*f7c14bbaSAndroid Build Coastguard Worker *prog_fd = ret;
7492*f7c14bbaSAndroid Build Coastguard Worker ret = 0;
7493*f7c14bbaSAndroid Build Coastguard Worker goto out;
7494*f7c14bbaSAndroid Build Coastguard Worker }
7495*f7c14bbaSAndroid Build Coastguard Worker
7496*f7c14bbaSAndroid Build Coastguard Worker if (log_level == 0) {
7497*f7c14bbaSAndroid Build Coastguard Worker log_level = 1;
7498*f7c14bbaSAndroid Build Coastguard Worker goto retry_load;
7499*f7c14bbaSAndroid Build Coastguard Worker }
7500*f7c14bbaSAndroid Build Coastguard Worker /* On ENOSPC, increase log buffer size and retry, unless custom
7501*f7c14bbaSAndroid Build Coastguard Worker * log_buf is specified.
7502*f7c14bbaSAndroid Build Coastguard Worker * Be careful to not overflow u32, though. Kernel's log buf size limit
7503*f7c14bbaSAndroid Build Coastguard Worker * isn't part of UAPI so it can always be bumped to full 4GB. So don't
7504*f7c14bbaSAndroid Build Coastguard Worker * multiply by 2 unless we are sure we'll fit within 32 bits.
7505*f7c14bbaSAndroid Build Coastguard Worker * Currently, we'll get -EINVAL when we reach (UINT_MAX >> 2).
7506*f7c14bbaSAndroid Build Coastguard Worker */
7507*f7c14bbaSAndroid Build Coastguard Worker if (own_log_buf && errno == ENOSPC && log_buf_size <= UINT_MAX / 2)
7508*f7c14bbaSAndroid Build Coastguard Worker goto retry_load;
7509*f7c14bbaSAndroid Build Coastguard Worker
7510*f7c14bbaSAndroid Build Coastguard Worker ret = -errno;
7511*f7c14bbaSAndroid Build Coastguard Worker
7512*f7c14bbaSAndroid Build Coastguard Worker /* post-process verifier log to improve error descriptions */
7513*f7c14bbaSAndroid Build Coastguard Worker fixup_verifier_log(prog, log_buf, log_buf_size);
7514*f7c14bbaSAndroid Build Coastguard Worker
7515*f7c14bbaSAndroid Build Coastguard Worker cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
7516*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': BPF program load failed: %s\n", prog->name, cp);
7517*f7c14bbaSAndroid Build Coastguard Worker pr_perm_msg(ret);
7518*f7c14bbaSAndroid Build Coastguard Worker
7519*f7c14bbaSAndroid Build Coastguard Worker if (own_log_buf && log_buf && log_buf[0] != '\0') {
7520*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': -- BEGIN PROG LOAD LOG --\n%s-- END PROG LOAD LOG --\n",
7521*f7c14bbaSAndroid Build Coastguard Worker prog->name, log_buf);
7522*f7c14bbaSAndroid Build Coastguard Worker }
7523*f7c14bbaSAndroid Build Coastguard Worker
7524*f7c14bbaSAndroid Build Coastguard Worker out:
7525*f7c14bbaSAndroid Build Coastguard Worker if (own_log_buf)
7526*f7c14bbaSAndroid Build Coastguard Worker free(log_buf);
7527*f7c14bbaSAndroid Build Coastguard Worker return ret;
7528*f7c14bbaSAndroid Build Coastguard Worker }
7529*f7c14bbaSAndroid Build Coastguard Worker
find_prev_line(char * buf,char * cur)7530*f7c14bbaSAndroid Build Coastguard Worker static char *find_prev_line(char *buf, char *cur)
7531*f7c14bbaSAndroid Build Coastguard Worker {
7532*f7c14bbaSAndroid Build Coastguard Worker char *p;
7533*f7c14bbaSAndroid Build Coastguard Worker
7534*f7c14bbaSAndroid Build Coastguard Worker if (cur == buf) /* end of a log buf */
7535*f7c14bbaSAndroid Build Coastguard Worker return NULL;
7536*f7c14bbaSAndroid Build Coastguard Worker
7537*f7c14bbaSAndroid Build Coastguard Worker p = cur - 1;
7538*f7c14bbaSAndroid Build Coastguard Worker while (p - 1 >= buf && *(p - 1) != '\n')
7539*f7c14bbaSAndroid Build Coastguard Worker p--;
7540*f7c14bbaSAndroid Build Coastguard Worker
7541*f7c14bbaSAndroid Build Coastguard Worker return p;
7542*f7c14bbaSAndroid Build Coastguard Worker }
7543*f7c14bbaSAndroid Build Coastguard Worker
patch_log(char * buf,size_t buf_sz,size_t log_sz,char * orig,size_t orig_sz,const char * patch)7544*f7c14bbaSAndroid Build Coastguard Worker static void patch_log(char *buf, size_t buf_sz, size_t log_sz,
7545*f7c14bbaSAndroid Build Coastguard Worker char *orig, size_t orig_sz, const char *patch)
7546*f7c14bbaSAndroid Build Coastguard Worker {
7547*f7c14bbaSAndroid Build Coastguard Worker /* size of the remaining log content to the right from the to-be-replaced part */
7548*f7c14bbaSAndroid Build Coastguard Worker size_t rem_sz = (buf + log_sz) - (orig + orig_sz);
7549*f7c14bbaSAndroid Build Coastguard Worker size_t patch_sz = strlen(patch);
7550*f7c14bbaSAndroid Build Coastguard Worker
7551*f7c14bbaSAndroid Build Coastguard Worker if (patch_sz != orig_sz) {
7552*f7c14bbaSAndroid Build Coastguard Worker /* If patch line(s) are longer than original piece of verifier log,
7553*f7c14bbaSAndroid Build Coastguard Worker * shift log contents by (patch_sz - orig_sz) bytes to the right
7554*f7c14bbaSAndroid Build Coastguard Worker * starting from after to-be-replaced part of the log.
7555*f7c14bbaSAndroid Build Coastguard Worker *
7556*f7c14bbaSAndroid Build Coastguard Worker * If patch line(s) are shorter than original piece of verifier log,
7557*f7c14bbaSAndroid Build Coastguard Worker * shift log contents by (orig_sz - patch_sz) bytes to the left
7558*f7c14bbaSAndroid Build Coastguard Worker * starting from after to-be-replaced part of the log
7559*f7c14bbaSAndroid Build Coastguard Worker *
7560*f7c14bbaSAndroid Build Coastguard Worker * We need to be careful about not overflowing available
7561*f7c14bbaSAndroid Build Coastguard Worker * buf_sz capacity. If that's the case, we'll truncate the end
7562*f7c14bbaSAndroid Build Coastguard Worker * of the original log, as necessary.
7563*f7c14bbaSAndroid Build Coastguard Worker */
7564*f7c14bbaSAndroid Build Coastguard Worker if (patch_sz > orig_sz) {
7565*f7c14bbaSAndroid Build Coastguard Worker if (orig + patch_sz >= buf + buf_sz) {
7566*f7c14bbaSAndroid Build Coastguard Worker /* patch is big enough to cover remaining space completely */
7567*f7c14bbaSAndroid Build Coastguard Worker patch_sz -= (orig + patch_sz) - (buf + buf_sz) + 1;
7568*f7c14bbaSAndroid Build Coastguard Worker rem_sz = 0;
7569*f7c14bbaSAndroid Build Coastguard Worker } else if (patch_sz - orig_sz > buf_sz - log_sz) {
7570*f7c14bbaSAndroid Build Coastguard Worker /* patch causes part of remaining log to be truncated */
7571*f7c14bbaSAndroid Build Coastguard Worker rem_sz -= (patch_sz - orig_sz) - (buf_sz - log_sz);
7572*f7c14bbaSAndroid Build Coastguard Worker }
7573*f7c14bbaSAndroid Build Coastguard Worker }
7574*f7c14bbaSAndroid Build Coastguard Worker /* shift remaining log to the right by calculated amount */
7575*f7c14bbaSAndroid Build Coastguard Worker memmove(orig + patch_sz, orig + orig_sz, rem_sz);
7576*f7c14bbaSAndroid Build Coastguard Worker }
7577*f7c14bbaSAndroid Build Coastguard Worker
7578*f7c14bbaSAndroid Build Coastguard Worker memcpy(orig, patch, patch_sz);
7579*f7c14bbaSAndroid Build Coastguard Worker }
7580*f7c14bbaSAndroid Build Coastguard Worker
fixup_log_failed_core_relo(struct bpf_program * prog,char * buf,size_t buf_sz,size_t log_sz,char * line1,char * line2,char * line3)7581*f7c14bbaSAndroid Build Coastguard Worker static void fixup_log_failed_core_relo(struct bpf_program *prog,
7582*f7c14bbaSAndroid Build Coastguard Worker char *buf, size_t buf_sz, size_t log_sz,
7583*f7c14bbaSAndroid Build Coastguard Worker char *line1, char *line2, char *line3)
7584*f7c14bbaSAndroid Build Coastguard Worker {
7585*f7c14bbaSAndroid Build Coastguard Worker /* Expected log for failed and not properly guarded CO-RE relocation:
7586*f7c14bbaSAndroid Build Coastguard Worker * line1 -> 123: (85) call unknown#195896080
7587*f7c14bbaSAndroid Build Coastguard Worker * line2 -> invalid func unknown#195896080
7588*f7c14bbaSAndroid Build Coastguard Worker * line3 -> <anything else or end of buffer>
7589*f7c14bbaSAndroid Build Coastguard Worker *
7590*f7c14bbaSAndroid Build Coastguard Worker * "123" is the index of the instruction that was poisoned. We extract
7591*f7c14bbaSAndroid Build Coastguard Worker * instruction index to find corresponding CO-RE relocation and
7592*f7c14bbaSAndroid Build Coastguard Worker * replace this part of the log with more relevant information about
7593*f7c14bbaSAndroid Build Coastguard Worker * failed CO-RE relocation.
7594*f7c14bbaSAndroid Build Coastguard Worker */
7595*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_core_relo *relo;
7596*f7c14bbaSAndroid Build Coastguard Worker struct bpf_core_spec spec;
7597*f7c14bbaSAndroid Build Coastguard Worker char patch[512], spec_buf[256];
7598*f7c14bbaSAndroid Build Coastguard Worker int insn_idx, err, spec_len;
7599*f7c14bbaSAndroid Build Coastguard Worker
7600*f7c14bbaSAndroid Build Coastguard Worker if (sscanf(line1, "%d: (%*d) call unknown#195896080\n", &insn_idx) != 1)
7601*f7c14bbaSAndroid Build Coastguard Worker return;
7602*f7c14bbaSAndroid Build Coastguard Worker
7603*f7c14bbaSAndroid Build Coastguard Worker relo = find_relo_core(prog, insn_idx);
7604*f7c14bbaSAndroid Build Coastguard Worker if (!relo)
7605*f7c14bbaSAndroid Build Coastguard Worker return;
7606*f7c14bbaSAndroid Build Coastguard Worker
7607*f7c14bbaSAndroid Build Coastguard Worker err = bpf_core_parse_spec(prog->name, prog->obj->btf, relo, &spec);
7608*f7c14bbaSAndroid Build Coastguard Worker if (err)
7609*f7c14bbaSAndroid Build Coastguard Worker return;
7610*f7c14bbaSAndroid Build Coastguard Worker
7611*f7c14bbaSAndroid Build Coastguard Worker spec_len = bpf_core_format_spec(spec_buf, sizeof(spec_buf), &spec);
7612*f7c14bbaSAndroid Build Coastguard Worker snprintf(patch, sizeof(patch),
7613*f7c14bbaSAndroid Build Coastguard Worker "%d: <invalid CO-RE relocation>\n"
7614*f7c14bbaSAndroid Build Coastguard Worker "failed to resolve CO-RE relocation %s%s\n",
7615*f7c14bbaSAndroid Build Coastguard Worker insn_idx, spec_buf, spec_len >= sizeof(spec_buf) ? "..." : "");
7616*f7c14bbaSAndroid Build Coastguard Worker
7617*f7c14bbaSAndroid Build Coastguard Worker patch_log(buf, buf_sz, log_sz, line1, line3 - line1, patch);
7618*f7c14bbaSAndroid Build Coastguard Worker }
7619*f7c14bbaSAndroid Build Coastguard Worker
fixup_log_missing_map_load(struct bpf_program * prog,char * buf,size_t buf_sz,size_t log_sz,char * line1,char * line2,char * line3)7620*f7c14bbaSAndroid Build Coastguard Worker static void fixup_log_missing_map_load(struct bpf_program *prog,
7621*f7c14bbaSAndroid Build Coastguard Worker char *buf, size_t buf_sz, size_t log_sz,
7622*f7c14bbaSAndroid Build Coastguard Worker char *line1, char *line2, char *line3)
7623*f7c14bbaSAndroid Build Coastguard Worker {
7624*f7c14bbaSAndroid Build Coastguard Worker /* Expected log for failed and not properly guarded map reference:
7625*f7c14bbaSAndroid Build Coastguard Worker * line1 -> 123: (85) call unknown#2001000345
7626*f7c14bbaSAndroid Build Coastguard Worker * line2 -> invalid func unknown#2001000345
7627*f7c14bbaSAndroid Build Coastguard Worker * line3 -> <anything else or end of buffer>
7628*f7c14bbaSAndroid Build Coastguard Worker *
7629*f7c14bbaSAndroid Build Coastguard Worker * "123" is the index of the instruction that was poisoned.
7630*f7c14bbaSAndroid Build Coastguard Worker * "345" in "2001000345" is a map index in obj->maps to fetch map name.
7631*f7c14bbaSAndroid Build Coastguard Worker */
7632*f7c14bbaSAndroid Build Coastguard Worker struct bpf_object *obj = prog->obj;
7633*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_map *map;
7634*f7c14bbaSAndroid Build Coastguard Worker int insn_idx, map_idx;
7635*f7c14bbaSAndroid Build Coastguard Worker char patch[128];
7636*f7c14bbaSAndroid Build Coastguard Worker
7637*f7c14bbaSAndroid Build Coastguard Worker if (sscanf(line1, "%d: (%*d) call unknown#%d\n", &insn_idx, &map_idx) != 2)
7638*f7c14bbaSAndroid Build Coastguard Worker return;
7639*f7c14bbaSAndroid Build Coastguard Worker
7640*f7c14bbaSAndroid Build Coastguard Worker map_idx -= POISON_LDIMM64_MAP_BASE;
7641*f7c14bbaSAndroid Build Coastguard Worker if (map_idx < 0 || map_idx >= obj->nr_maps)
7642*f7c14bbaSAndroid Build Coastguard Worker return;
7643*f7c14bbaSAndroid Build Coastguard Worker map = &obj->maps[map_idx];
7644*f7c14bbaSAndroid Build Coastguard Worker
7645*f7c14bbaSAndroid Build Coastguard Worker snprintf(patch, sizeof(patch),
7646*f7c14bbaSAndroid Build Coastguard Worker "%d: <invalid BPF map reference>\n"
7647*f7c14bbaSAndroid Build Coastguard Worker "BPF map '%s' is referenced but wasn't created\n",
7648*f7c14bbaSAndroid Build Coastguard Worker insn_idx, map->name);
7649*f7c14bbaSAndroid Build Coastguard Worker
7650*f7c14bbaSAndroid Build Coastguard Worker patch_log(buf, buf_sz, log_sz, line1, line3 - line1, patch);
7651*f7c14bbaSAndroid Build Coastguard Worker }
7652*f7c14bbaSAndroid Build Coastguard Worker
fixup_log_missing_kfunc_call(struct bpf_program * prog,char * buf,size_t buf_sz,size_t log_sz,char * line1,char * line2,char * line3)7653*f7c14bbaSAndroid Build Coastguard Worker static void fixup_log_missing_kfunc_call(struct bpf_program *prog,
7654*f7c14bbaSAndroid Build Coastguard Worker char *buf, size_t buf_sz, size_t log_sz,
7655*f7c14bbaSAndroid Build Coastguard Worker char *line1, char *line2, char *line3)
7656*f7c14bbaSAndroid Build Coastguard Worker {
7657*f7c14bbaSAndroid Build Coastguard Worker /* Expected log for failed and not properly guarded kfunc call:
7658*f7c14bbaSAndroid Build Coastguard Worker * line1 -> 123: (85) call unknown#2002000345
7659*f7c14bbaSAndroid Build Coastguard Worker * line2 -> invalid func unknown#2002000345
7660*f7c14bbaSAndroid Build Coastguard Worker * line3 -> <anything else or end of buffer>
7661*f7c14bbaSAndroid Build Coastguard Worker *
7662*f7c14bbaSAndroid Build Coastguard Worker * "123" is the index of the instruction that was poisoned.
7663*f7c14bbaSAndroid Build Coastguard Worker * "345" in "2002000345" is an extern index in obj->externs to fetch kfunc name.
7664*f7c14bbaSAndroid Build Coastguard Worker */
7665*f7c14bbaSAndroid Build Coastguard Worker struct bpf_object *obj = prog->obj;
7666*f7c14bbaSAndroid Build Coastguard Worker const struct extern_desc *ext;
7667*f7c14bbaSAndroid Build Coastguard Worker int insn_idx, ext_idx;
7668*f7c14bbaSAndroid Build Coastguard Worker char patch[128];
7669*f7c14bbaSAndroid Build Coastguard Worker
7670*f7c14bbaSAndroid Build Coastguard Worker if (sscanf(line1, "%d: (%*d) call unknown#%d\n", &insn_idx, &ext_idx) != 2)
7671*f7c14bbaSAndroid Build Coastguard Worker return;
7672*f7c14bbaSAndroid Build Coastguard Worker
7673*f7c14bbaSAndroid Build Coastguard Worker ext_idx -= POISON_CALL_KFUNC_BASE;
7674*f7c14bbaSAndroid Build Coastguard Worker if (ext_idx < 0 || ext_idx >= obj->nr_extern)
7675*f7c14bbaSAndroid Build Coastguard Worker return;
7676*f7c14bbaSAndroid Build Coastguard Worker ext = &obj->externs[ext_idx];
7677*f7c14bbaSAndroid Build Coastguard Worker
7678*f7c14bbaSAndroid Build Coastguard Worker snprintf(patch, sizeof(patch),
7679*f7c14bbaSAndroid Build Coastguard Worker "%d: <invalid kfunc call>\n"
7680*f7c14bbaSAndroid Build Coastguard Worker "kfunc '%s' is referenced but wasn't resolved\n",
7681*f7c14bbaSAndroid Build Coastguard Worker insn_idx, ext->name);
7682*f7c14bbaSAndroid Build Coastguard Worker
7683*f7c14bbaSAndroid Build Coastguard Worker patch_log(buf, buf_sz, log_sz, line1, line3 - line1, patch);
7684*f7c14bbaSAndroid Build Coastguard Worker }
7685*f7c14bbaSAndroid Build Coastguard Worker
fixup_verifier_log(struct bpf_program * prog,char * buf,size_t buf_sz)7686*f7c14bbaSAndroid Build Coastguard Worker static void fixup_verifier_log(struct bpf_program *prog, char *buf, size_t buf_sz)
7687*f7c14bbaSAndroid Build Coastguard Worker {
7688*f7c14bbaSAndroid Build Coastguard Worker /* look for familiar error patterns in last N lines of the log */
7689*f7c14bbaSAndroid Build Coastguard Worker const size_t max_last_line_cnt = 10;
7690*f7c14bbaSAndroid Build Coastguard Worker char *prev_line, *cur_line, *next_line;
7691*f7c14bbaSAndroid Build Coastguard Worker size_t log_sz;
7692*f7c14bbaSAndroid Build Coastguard Worker int i;
7693*f7c14bbaSAndroid Build Coastguard Worker
7694*f7c14bbaSAndroid Build Coastguard Worker if (!buf)
7695*f7c14bbaSAndroid Build Coastguard Worker return;
7696*f7c14bbaSAndroid Build Coastguard Worker
7697*f7c14bbaSAndroid Build Coastguard Worker log_sz = strlen(buf) + 1;
7698*f7c14bbaSAndroid Build Coastguard Worker next_line = buf + log_sz - 1;
7699*f7c14bbaSAndroid Build Coastguard Worker
7700*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < max_last_line_cnt; i++, next_line = cur_line) {
7701*f7c14bbaSAndroid Build Coastguard Worker cur_line = find_prev_line(buf, next_line);
7702*f7c14bbaSAndroid Build Coastguard Worker if (!cur_line)
7703*f7c14bbaSAndroid Build Coastguard Worker return;
7704*f7c14bbaSAndroid Build Coastguard Worker
7705*f7c14bbaSAndroid Build Coastguard Worker if (str_has_pfx(cur_line, "invalid func unknown#195896080\n")) {
7706*f7c14bbaSAndroid Build Coastguard Worker prev_line = find_prev_line(buf, cur_line);
7707*f7c14bbaSAndroid Build Coastguard Worker if (!prev_line)
7708*f7c14bbaSAndroid Build Coastguard Worker continue;
7709*f7c14bbaSAndroid Build Coastguard Worker
7710*f7c14bbaSAndroid Build Coastguard Worker /* failed CO-RE relocation case */
7711*f7c14bbaSAndroid Build Coastguard Worker fixup_log_failed_core_relo(prog, buf, buf_sz, log_sz,
7712*f7c14bbaSAndroid Build Coastguard Worker prev_line, cur_line, next_line);
7713*f7c14bbaSAndroid Build Coastguard Worker return;
7714*f7c14bbaSAndroid Build Coastguard Worker } else if (str_has_pfx(cur_line, "invalid func unknown#"POISON_LDIMM64_MAP_PFX)) {
7715*f7c14bbaSAndroid Build Coastguard Worker prev_line = find_prev_line(buf, cur_line);
7716*f7c14bbaSAndroid Build Coastguard Worker if (!prev_line)
7717*f7c14bbaSAndroid Build Coastguard Worker continue;
7718*f7c14bbaSAndroid Build Coastguard Worker
7719*f7c14bbaSAndroid Build Coastguard Worker /* reference to uncreated BPF map */
7720*f7c14bbaSAndroid Build Coastguard Worker fixup_log_missing_map_load(prog, buf, buf_sz, log_sz,
7721*f7c14bbaSAndroid Build Coastguard Worker prev_line, cur_line, next_line);
7722*f7c14bbaSAndroid Build Coastguard Worker return;
7723*f7c14bbaSAndroid Build Coastguard Worker } else if (str_has_pfx(cur_line, "invalid func unknown#"POISON_CALL_KFUNC_PFX)) {
7724*f7c14bbaSAndroid Build Coastguard Worker prev_line = find_prev_line(buf, cur_line);
7725*f7c14bbaSAndroid Build Coastguard Worker if (!prev_line)
7726*f7c14bbaSAndroid Build Coastguard Worker continue;
7727*f7c14bbaSAndroid Build Coastguard Worker
7728*f7c14bbaSAndroid Build Coastguard Worker /* reference to unresolved kfunc */
7729*f7c14bbaSAndroid Build Coastguard Worker fixup_log_missing_kfunc_call(prog, buf, buf_sz, log_sz,
7730*f7c14bbaSAndroid Build Coastguard Worker prev_line, cur_line, next_line);
7731*f7c14bbaSAndroid Build Coastguard Worker return;
7732*f7c14bbaSAndroid Build Coastguard Worker }
7733*f7c14bbaSAndroid Build Coastguard Worker }
7734*f7c14bbaSAndroid Build Coastguard Worker }
7735*f7c14bbaSAndroid Build Coastguard Worker
bpf_program_record_relos(struct bpf_program * prog)7736*f7c14bbaSAndroid Build Coastguard Worker static int bpf_program_record_relos(struct bpf_program *prog)
7737*f7c14bbaSAndroid Build Coastguard Worker {
7738*f7c14bbaSAndroid Build Coastguard Worker struct bpf_object *obj = prog->obj;
7739*f7c14bbaSAndroid Build Coastguard Worker int i;
7740*f7c14bbaSAndroid Build Coastguard Worker
7741*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < prog->nr_reloc; i++) {
7742*f7c14bbaSAndroid Build Coastguard Worker struct reloc_desc *relo = &prog->reloc_desc[i];
7743*f7c14bbaSAndroid Build Coastguard Worker struct extern_desc *ext = &obj->externs[relo->ext_idx];
7744*f7c14bbaSAndroid Build Coastguard Worker int kind;
7745*f7c14bbaSAndroid Build Coastguard Worker
7746*f7c14bbaSAndroid Build Coastguard Worker switch (relo->type) {
7747*f7c14bbaSAndroid Build Coastguard Worker case RELO_EXTERN_LD64:
7748*f7c14bbaSAndroid Build Coastguard Worker if (ext->type != EXT_KSYM)
7749*f7c14bbaSAndroid Build Coastguard Worker continue;
7750*f7c14bbaSAndroid Build Coastguard Worker kind = btf_is_var(btf__type_by_id(obj->btf, ext->btf_id)) ?
7751*f7c14bbaSAndroid Build Coastguard Worker BTF_KIND_VAR : BTF_KIND_FUNC;
7752*f7c14bbaSAndroid Build Coastguard Worker bpf_gen__record_extern(obj->gen_loader, ext->name,
7753*f7c14bbaSAndroid Build Coastguard Worker ext->is_weak, !ext->ksym.type_id,
7754*f7c14bbaSAndroid Build Coastguard Worker true, kind, relo->insn_idx);
7755*f7c14bbaSAndroid Build Coastguard Worker break;
7756*f7c14bbaSAndroid Build Coastguard Worker case RELO_EXTERN_CALL:
7757*f7c14bbaSAndroid Build Coastguard Worker bpf_gen__record_extern(obj->gen_loader, ext->name,
7758*f7c14bbaSAndroid Build Coastguard Worker ext->is_weak, false, false, BTF_KIND_FUNC,
7759*f7c14bbaSAndroid Build Coastguard Worker relo->insn_idx);
7760*f7c14bbaSAndroid Build Coastguard Worker break;
7761*f7c14bbaSAndroid Build Coastguard Worker case RELO_CORE: {
7762*f7c14bbaSAndroid Build Coastguard Worker struct bpf_core_relo cr = {
7763*f7c14bbaSAndroid Build Coastguard Worker .insn_off = relo->insn_idx * 8,
7764*f7c14bbaSAndroid Build Coastguard Worker .type_id = relo->core_relo->type_id,
7765*f7c14bbaSAndroid Build Coastguard Worker .access_str_off = relo->core_relo->access_str_off,
7766*f7c14bbaSAndroid Build Coastguard Worker .kind = relo->core_relo->kind,
7767*f7c14bbaSAndroid Build Coastguard Worker };
7768*f7c14bbaSAndroid Build Coastguard Worker
7769*f7c14bbaSAndroid Build Coastguard Worker bpf_gen__record_relo_core(obj->gen_loader, &cr);
7770*f7c14bbaSAndroid Build Coastguard Worker break;
7771*f7c14bbaSAndroid Build Coastguard Worker }
7772*f7c14bbaSAndroid Build Coastguard Worker default:
7773*f7c14bbaSAndroid Build Coastguard Worker continue;
7774*f7c14bbaSAndroid Build Coastguard Worker }
7775*f7c14bbaSAndroid Build Coastguard Worker }
7776*f7c14bbaSAndroid Build Coastguard Worker return 0;
7777*f7c14bbaSAndroid Build Coastguard Worker }
7778*f7c14bbaSAndroid Build Coastguard Worker
7779*f7c14bbaSAndroid Build Coastguard Worker static int
bpf_object__load_progs(struct bpf_object * obj,int log_level)7780*f7c14bbaSAndroid Build Coastguard Worker bpf_object__load_progs(struct bpf_object *obj, int log_level)
7781*f7c14bbaSAndroid Build Coastguard Worker {
7782*f7c14bbaSAndroid Build Coastguard Worker struct bpf_program *prog;
7783*f7c14bbaSAndroid Build Coastguard Worker size_t i;
7784*f7c14bbaSAndroid Build Coastguard Worker int err;
7785*f7c14bbaSAndroid Build Coastguard Worker
7786*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < obj->nr_programs; i++) {
7787*f7c14bbaSAndroid Build Coastguard Worker prog = &obj->programs[i];
7788*f7c14bbaSAndroid Build Coastguard Worker err = bpf_object__sanitize_prog(obj, prog);
7789*f7c14bbaSAndroid Build Coastguard Worker if (err)
7790*f7c14bbaSAndroid Build Coastguard Worker return err;
7791*f7c14bbaSAndroid Build Coastguard Worker }
7792*f7c14bbaSAndroid Build Coastguard Worker
7793*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < obj->nr_programs; i++) {
7794*f7c14bbaSAndroid Build Coastguard Worker prog = &obj->programs[i];
7795*f7c14bbaSAndroid Build Coastguard Worker if (prog_is_subprog(obj, prog))
7796*f7c14bbaSAndroid Build Coastguard Worker continue;
7797*f7c14bbaSAndroid Build Coastguard Worker if (!prog->autoload) {
7798*f7c14bbaSAndroid Build Coastguard Worker pr_debug("prog '%s': skipped loading\n", prog->name);
7799*f7c14bbaSAndroid Build Coastguard Worker continue;
7800*f7c14bbaSAndroid Build Coastguard Worker }
7801*f7c14bbaSAndroid Build Coastguard Worker prog->log_level |= log_level;
7802*f7c14bbaSAndroid Build Coastguard Worker
7803*f7c14bbaSAndroid Build Coastguard Worker if (obj->gen_loader)
7804*f7c14bbaSAndroid Build Coastguard Worker bpf_program_record_relos(prog);
7805*f7c14bbaSAndroid Build Coastguard Worker
7806*f7c14bbaSAndroid Build Coastguard Worker err = bpf_object_load_prog(obj, prog, prog->insns, prog->insns_cnt,
7807*f7c14bbaSAndroid Build Coastguard Worker obj->license, obj->kern_version, &prog->fd);
7808*f7c14bbaSAndroid Build Coastguard Worker if (err) {
7809*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': failed to load: %d\n", prog->name, err);
7810*f7c14bbaSAndroid Build Coastguard Worker return err;
7811*f7c14bbaSAndroid Build Coastguard Worker }
7812*f7c14bbaSAndroid Build Coastguard Worker }
7813*f7c14bbaSAndroid Build Coastguard Worker
7814*f7c14bbaSAndroid Build Coastguard Worker bpf_object__free_relocs(obj);
7815*f7c14bbaSAndroid Build Coastguard Worker return 0;
7816*f7c14bbaSAndroid Build Coastguard Worker }
7817*f7c14bbaSAndroid Build Coastguard Worker
7818*f7c14bbaSAndroid Build Coastguard Worker static const struct bpf_sec_def *find_sec_def(const char *sec_name);
7819*f7c14bbaSAndroid Build Coastguard Worker
bpf_object_init_progs(struct bpf_object * obj,const struct bpf_object_open_opts * opts)7820*f7c14bbaSAndroid Build Coastguard Worker static int bpf_object_init_progs(struct bpf_object *obj, const struct bpf_object_open_opts *opts)
7821*f7c14bbaSAndroid Build Coastguard Worker {
7822*f7c14bbaSAndroid Build Coastguard Worker struct bpf_program *prog;
7823*f7c14bbaSAndroid Build Coastguard Worker int err;
7824*f7c14bbaSAndroid Build Coastguard Worker
7825*f7c14bbaSAndroid Build Coastguard Worker bpf_object__for_each_program(prog, obj) {
7826*f7c14bbaSAndroid Build Coastguard Worker prog->sec_def = find_sec_def(prog->sec_name);
7827*f7c14bbaSAndroid Build Coastguard Worker if (!prog->sec_def) {
7828*f7c14bbaSAndroid Build Coastguard Worker /* couldn't guess, but user might manually specify */
7829*f7c14bbaSAndroid Build Coastguard Worker pr_debug("prog '%s': unrecognized ELF section name '%s'\n",
7830*f7c14bbaSAndroid Build Coastguard Worker prog->name, prog->sec_name);
7831*f7c14bbaSAndroid Build Coastguard Worker continue;
7832*f7c14bbaSAndroid Build Coastguard Worker }
7833*f7c14bbaSAndroid Build Coastguard Worker
7834*f7c14bbaSAndroid Build Coastguard Worker prog->type = prog->sec_def->prog_type;
7835*f7c14bbaSAndroid Build Coastguard Worker prog->expected_attach_type = prog->sec_def->expected_attach_type;
7836*f7c14bbaSAndroid Build Coastguard Worker
7837*f7c14bbaSAndroid Build Coastguard Worker /* sec_def can have custom callback which should be called
7838*f7c14bbaSAndroid Build Coastguard Worker * after bpf_program is initialized to adjust its properties
7839*f7c14bbaSAndroid Build Coastguard Worker */
7840*f7c14bbaSAndroid Build Coastguard Worker if (prog->sec_def->prog_setup_fn) {
7841*f7c14bbaSAndroid Build Coastguard Worker err = prog->sec_def->prog_setup_fn(prog, prog->sec_def->cookie);
7842*f7c14bbaSAndroid Build Coastguard Worker if (err < 0) {
7843*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': failed to initialize: %d\n",
7844*f7c14bbaSAndroid Build Coastguard Worker prog->name, err);
7845*f7c14bbaSAndroid Build Coastguard Worker return err;
7846*f7c14bbaSAndroid Build Coastguard Worker }
7847*f7c14bbaSAndroid Build Coastguard Worker }
7848*f7c14bbaSAndroid Build Coastguard Worker }
7849*f7c14bbaSAndroid Build Coastguard Worker
7850*f7c14bbaSAndroid Build Coastguard Worker return 0;
7851*f7c14bbaSAndroid Build Coastguard Worker }
7852*f7c14bbaSAndroid Build Coastguard Worker
bpf_object_open(const char * path,const void * obj_buf,size_t obj_buf_sz,const struct bpf_object_open_opts * opts)7853*f7c14bbaSAndroid Build Coastguard Worker static struct bpf_object *bpf_object_open(const char *path, const void *obj_buf, size_t obj_buf_sz,
7854*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_object_open_opts *opts)
7855*f7c14bbaSAndroid Build Coastguard Worker {
7856*f7c14bbaSAndroid Build Coastguard Worker const char *obj_name, *kconfig, *btf_tmp_path, *token_path;
7857*f7c14bbaSAndroid Build Coastguard Worker struct bpf_object *obj;
7858*f7c14bbaSAndroid Build Coastguard Worker char tmp_name[64];
7859*f7c14bbaSAndroid Build Coastguard Worker int err;
7860*f7c14bbaSAndroid Build Coastguard Worker char *log_buf;
7861*f7c14bbaSAndroid Build Coastguard Worker size_t log_size;
7862*f7c14bbaSAndroid Build Coastguard Worker __u32 log_level;
7863*f7c14bbaSAndroid Build Coastguard Worker
7864*f7c14bbaSAndroid Build Coastguard Worker if (elf_version(EV_CURRENT) == EV_NONE) {
7865*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to init libelf for %s\n",
7866*f7c14bbaSAndroid Build Coastguard Worker path ? : "(mem buf)");
7867*f7c14bbaSAndroid Build Coastguard Worker return ERR_PTR(-LIBBPF_ERRNO__LIBELF);
7868*f7c14bbaSAndroid Build Coastguard Worker }
7869*f7c14bbaSAndroid Build Coastguard Worker
7870*f7c14bbaSAndroid Build Coastguard Worker if (!OPTS_VALID(opts, bpf_object_open_opts))
7871*f7c14bbaSAndroid Build Coastguard Worker return ERR_PTR(-EINVAL);
7872*f7c14bbaSAndroid Build Coastguard Worker
7873*f7c14bbaSAndroid Build Coastguard Worker obj_name = OPTS_GET(opts, object_name, NULL);
7874*f7c14bbaSAndroid Build Coastguard Worker if (obj_buf) {
7875*f7c14bbaSAndroid Build Coastguard Worker if (!obj_name) {
7876*f7c14bbaSAndroid Build Coastguard Worker snprintf(tmp_name, sizeof(tmp_name), "%lx-%lx",
7877*f7c14bbaSAndroid Build Coastguard Worker (unsigned long)obj_buf,
7878*f7c14bbaSAndroid Build Coastguard Worker (unsigned long)obj_buf_sz);
7879*f7c14bbaSAndroid Build Coastguard Worker obj_name = tmp_name;
7880*f7c14bbaSAndroid Build Coastguard Worker }
7881*f7c14bbaSAndroid Build Coastguard Worker path = obj_name;
7882*f7c14bbaSAndroid Build Coastguard Worker pr_debug("loading object '%s' from buffer\n", obj_name);
7883*f7c14bbaSAndroid Build Coastguard Worker }
7884*f7c14bbaSAndroid Build Coastguard Worker
7885*f7c14bbaSAndroid Build Coastguard Worker log_buf = OPTS_GET(opts, kernel_log_buf, NULL);
7886*f7c14bbaSAndroid Build Coastguard Worker log_size = OPTS_GET(opts, kernel_log_size, 0);
7887*f7c14bbaSAndroid Build Coastguard Worker log_level = OPTS_GET(opts, kernel_log_level, 0);
7888*f7c14bbaSAndroid Build Coastguard Worker if (log_size > UINT_MAX)
7889*f7c14bbaSAndroid Build Coastguard Worker return ERR_PTR(-EINVAL);
7890*f7c14bbaSAndroid Build Coastguard Worker if (log_size && !log_buf)
7891*f7c14bbaSAndroid Build Coastguard Worker return ERR_PTR(-EINVAL);
7892*f7c14bbaSAndroid Build Coastguard Worker
7893*f7c14bbaSAndroid Build Coastguard Worker token_path = OPTS_GET(opts, bpf_token_path, NULL);
7894*f7c14bbaSAndroid Build Coastguard Worker /* if user didn't specify bpf_token_path explicitly, check if
7895*f7c14bbaSAndroid Build Coastguard Worker * LIBBPF_BPF_TOKEN_PATH envvar was set and treat it as bpf_token_path
7896*f7c14bbaSAndroid Build Coastguard Worker * option
7897*f7c14bbaSAndroid Build Coastguard Worker */
7898*f7c14bbaSAndroid Build Coastguard Worker if (!token_path)
7899*f7c14bbaSAndroid Build Coastguard Worker token_path = getenv("LIBBPF_BPF_TOKEN_PATH");
7900*f7c14bbaSAndroid Build Coastguard Worker if (token_path && strlen(token_path) >= PATH_MAX)
7901*f7c14bbaSAndroid Build Coastguard Worker return ERR_PTR(-ENAMETOOLONG);
7902*f7c14bbaSAndroid Build Coastguard Worker
7903*f7c14bbaSAndroid Build Coastguard Worker obj = bpf_object__new(path, obj_buf, obj_buf_sz, obj_name);
7904*f7c14bbaSAndroid Build Coastguard Worker if (IS_ERR(obj))
7905*f7c14bbaSAndroid Build Coastguard Worker return obj;
7906*f7c14bbaSAndroid Build Coastguard Worker
7907*f7c14bbaSAndroid Build Coastguard Worker obj->log_buf = log_buf;
7908*f7c14bbaSAndroid Build Coastguard Worker obj->log_size = log_size;
7909*f7c14bbaSAndroid Build Coastguard Worker obj->log_level = log_level;
7910*f7c14bbaSAndroid Build Coastguard Worker
7911*f7c14bbaSAndroid Build Coastguard Worker if (token_path) {
7912*f7c14bbaSAndroid Build Coastguard Worker obj->token_path = strdup(token_path);
7913*f7c14bbaSAndroid Build Coastguard Worker if (!obj->token_path) {
7914*f7c14bbaSAndroid Build Coastguard Worker err = -ENOMEM;
7915*f7c14bbaSAndroid Build Coastguard Worker goto out;
7916*f7c14bbaSAndroid Build Coastguard Worker }
7917*f7c14bbaSAndroid Build Coastguard Worker }
7918*f7c14bbaSAndroid Build Coastguard Worker
7919*f7c14bbaSAndroid Build Coastguard Worker btf_tmp_path = OPTS_GET(opts, btf_custom_path, NULL);
7920*f7c14bbaSAndroid Build Coastguard Worker if (btf_tmp_path) {
7921*f7c14bbaSAndroid Build Coastguard Worker if (strlen(btf_tmp_path) >= PATH_MAX) {
7922*f7c14bbaSAndroid Build Coastguard Worker err = -ENAMETOOLONG;
7923*f7c14bbaSAndroid Build Coastguard Worker goto out;
7924*f7c14bbaSAndroid Build Coastguard Worker }
7925*f7c14bbaSAndroid Build Coastguard Worker obj->btf_custom_path = strdup(btf_tmp_path);
7926*f7c14bbaSAndroid Build Coastguard Worker if (!obj->btf_custom_path) {
7927*f7c14bbaSAndroid Build Coastguard Worker err = -ENOMEM;
7928*f7c14bbaSAndroid Build Coastguard Worker goto out;
7929*f7c14bbaSAndroid Build Coastguard Worker }
7930*f7c14bbaSAndroid Build Coastguard Worker }
7931*f7c14bbaSAndroid Build Coastguard Worker
7932*f7c14bbaSAndroid Build Coastguard Worker kconfig = OPTS_GET(opts, kconfig, NULL);
7933*f7c14bbaSAndroid Build Coastguard Worker if (kconfig) {
7934*f7c14bbaSAndroid Build Coastguard Worker obj->kconfig = strdup(kconfig);
7935*f7c14bbaSAndroid Build Coastguard Worker if (!obj->kconfig) {
7936*f7c14bbaSAndroid Build Coastguard Worker err = -ENOMEM;
7937*f7c14bbaSAndroid Build Coastguard Worker goto out;
7938*f7c14bbaSAndroid Build Coastguard Worker }
7939*f7c14bbaSAndroid Build Coastguard Worker }
7940*f7c14bbaSAndroid Build Coastguard Worker
7941*f7c14bbaSAndroid Build Coastguard Worker err = bpf_object__elf_init(obj);
7942*f7c14bbaSAndroid Build Coastguard Worker err = err ? : bpf_object__check_endianness(obj);
7943*f7c14bbaSAndroid Build Coastguard Worker err = err ? : bpf_object__elf_collect(obj);
7944*f7c14bbaSAndroid Build Coastguard Worker err = err ? : bpf_object__collect_externs(obj);
7945*f7c14bbaSAndroid Build Coastguard Worker err = err ? : bpf_object_fixup_btf(obj);
7946*f7c14bbaSAndroid Build Coastguard Worker err = err ? : bpf_object__init_maps(obj, opts);
7947*f7c14bbaSAndroid Build Coastguard Worker err = err ? : bpf_object_init_progs(obj, opts);
7948*f7c14bbaSAndroid Build Coastguard Worker err = err ? : bpf_object__collect_relos(obj);
7949*f7c14bbaSAndroid Build Coastguard Worker if (err)
7950*f7c14bbaSAndroid Build Coastguard Worker goto out;
7951*f7c14bbaSAndroid Build Coastguard Worker
7952*f7c14bbaSAndroid Build Coastguard Worker bpf_object__elf_finish(obj);
7953*f7c14bbaSAndroid Build Coastguard Worker
7954*f7c14bbaSAndroid Build Coastguard Worker return obj;
7955*f7c14bbaSAndroid Build Coastguard Worker out:
7956*f7c14bbaSAndroid Build Coastguard Worker bpf_object__close(obj);
7957*f7c14bbaSAndroid Build Coastguard Worker return ERR_PTR(err);
7958*f7c14bbaSAndroid Build Coastguard Worker }
7959*f7c14bbaSAndroid Build Coastguard Worker
7960*f7c14bbaSAndroid Build Coastguard Worker struct bpf_object *
bpf_object__open_file(const char * path,const struct bpf_object_open_opts * opts)7961*f7c14bbaSAndroid Build Coastguard Worker bpf_object__open_file(const char *path, const struct bpf_object_open_opts *opts)
7962*f7c14bbaSAndroid Build Coastguard Worker {
7963*f7c14bbaSAndroid Build Coastguard Worker if (!path)
7964*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(-EINVAL);
7965*f7c14bbaSAndroid Build Coastguard Worker
7966*f7c14bbaSAndroid Build Coastguard Worker pr_debug("loading %s\n", path);
7967*f7c14bbaSAndroid Build Coastguard Worker
7968*f7c14bbaSAndroid Build Coastguard Worker return libbpf_ptr(bpf_object_open(path, NULL, 0, opts));
7969*f7c14bbaSAndroid Build Coastguard Worker }
7970*f7c14bbaSAndroid Build Coastguard Worker
bpf_object__open(const char * path)7971*f7c14bbaSAndroid Build Coastguard Worker struct bpf_object *bpf_object__open(const char *path)
7972*f7c14bbaSAndroid Build Coastguard Worker {
7973*f7c14bbaSAndroid Build Coastguard Worker return bpf_object__open_file(path, NULL);
7974*f7c14bbaSAndroid Build Coastguard Worker }
7975*f7c14bbaSAndroid Build Coastguard Worker
7976*f7c14bbaSAndroid Build Coastguard Worker struct bpf_object *
bpf_object__open_mem(const void * obj_buf,size_t obj_buf_sz,const struct bpf_object_open_opts * opts)7977*f7c14bbaSAndroid Build Coastguard Worker bpf_object__open_mem(const void *obj_buf, size_t obj_buf_sz,
7978*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_object_open_opts *opts)
7979*f7c14bbaSAndroid Build Coastguard Worker {
7980*f7c14bbaSAndroid Build Coastguard Worker if (!obj_buf || obj_buf_sz == 0)
7981*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(-EINVAL);
7982*f7c14bbaSAndroid Build Coastguard Worker
7983*f7c14bbaSAndroid Build Coastguard Worker return libbpf_ptr(bpf_object_open(NULL, obj_buf, obj_buf_sz, opts));
7984*f7c14bbaSAndroid Build Coastguard Worker }
7985*f7c14bbaSAndroid Build Coastguard Worker
bpf_object_unload(struct bpf_object * obj)7986*f7c14bbaSAndroid Build Coastguard Worker static int bpf_object_unload(struct bpf_object *obj)
7987*f7c14bbaSAndroid Build Coastguard Worker {
7988*f7c14bbaSAndroid Build Coastguard Worker size_t i;
7989*f7c14bbaSAndroid Build Coastguard Worker
7990*f7c14bbaSAndroid Build Coastguard Worker if (!obj)
7991*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-EINVAL);
7992*f7c14bbaSAndroid Build Coastguard Worker
7993*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < obj->nr_maps; i++) {
7994*f7c14bbaSAndroid Build Coastguard Worker zclose(obj->maps[i].fd);
7995*f7c14bbaSAndroid Build Coastguard Worker if (obj->maps[i].st_ops)
7996*f7c14bbaSAndroid Build Coastguard Worker zfree(&obj->maps[i].st_ops->kern_vdata);
7997*f7c14bbaSAndroid Build Coastguard Worker }
7998*f7c14bbaSAndroid Build Coastguard Worker
7999*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < obj->nr_programs; i++)
8000*f7c14bbaSAndroid Build Coastguard Worker bpf_program__unload(&obj->programs[i]);
8001*f7c14bbaSAndroid Build Coastguard Worker
8002*f7c14bbaSAndroid Build Coastguard Worker return 0;
8003*f7c14bbaSAndroid Build Coastguard Worker }
8004*f7c14bbaSAndroid Build Coastguard Worker
bpf_object__sanitize_maps(struct bpf_object * obj)8005*f7c14bbaSAndroid Build Coastguard Worker static int bpf_object__sanitize_maps(struct bpf_object *obj)
8006*f7c14bbaSAndroid Build Coastguard Worker {
8007*f7c14bbaSAndroid Build Coastguard Worker struct bpf_map *m;
8008*f7c14bbaSAndroid Build Coastguard Worker
8009*f7c14bbaSAndroid Build Coastguard Worker bpf_object__for_each_map(m, obj) {
8010*f7c14bbaSAndroid Build Coastguard Worker if (!bpf_map__is_internal(m))
8011*f7c14bbaSAndroid Build Coastguard Worker continue;
8012*f7c14bbaSAndroid Build Coastguard Worker if (!kernel_supports(obj, FEAT_ARRAY_MMAP))
8013*f7c14bbaSAndroid Build Coastguard Worker m->def.map_flags &= ~BPF_F_MMAPABLE;
8014*f7c14bbaSAndroid Build Coastguard Worker }
8015*f7c14bbaSAndroid Build Coastguard Worker
8016*f7c14bbaSAndroid Build Coastguard Worker return 0;
8017*f7c14bbaSAndroid Build Coastguard Worker }
8018*f7c14bbaSAndroid Build Coastguard Worker
libbpf_kallsyms_parse(kallsyms_cb_t cb,void * ctx)8019*f7c14bbaSAndroid Build Coastguard Worker int libbpf_kallsyms_parse(kallsyms_cb_t cb, void *ctx)
8020*f7c14bbaSAndroid Build Coastguard Worker {
8021*f7c14bbaSAndroid Build Coastguard Worker char sym_type, sym_name[500];
8022*f7c14bbaSAndroid Build Coastguard Worker unsigned long long sym_addr;
8023*f7c14bbaSAndroid Build Coastguard Worker int ret, err = 0;
8024*f7c14bbaSAndroid Build Coastguard Worker FILE *f;
8025*f7c14bbaSAndroid Build Coastguard Worker
8026*f7c14bbaSAndroid Build Coastguard Worker f = fopen("/proc/kallsyms", "re");
8027*f7c14bbaSAndroid Build Coastguard Worker if (!f) {
8028*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
8029*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to open /proc/kallsyms: %d\n", err);
8030*f7c14bbaSAndroid Build Coastguard Worker return err;
8031*f7c14bbaSAndroid Build Coastguard Worker }
8032*f7c14bbaSAndroid Build Coastguard Worker
8033*f7c14bbaSAndroid Build Coastguard Worker while (true) {
8034*f7c14bbaSAndroid Build Coastguard Worker ret = fscanf(f, "%llx %c %499s%*[^\n]\n",
8035*f7c14bbaSAndroid Build Coastguard Worker &sym_addr, &sym_type, sym_name);
8036*f7c14bbaSAndroid Build Coastguard Worker if (ret == EOF && feof(f))
8037*f7c14bbaSAndroid Build Coastguard Worker break;
8038*f7c14bbaSAndroid Build Coastguard Worker if (ret != 3) {
8039*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to read kallsyms entry: %d\n", ret);
8040*f7c14bbaSAndroid Build Coastguard Worker err = -EINVAL;
8041*f7c14bbaSAndroid Build Coastguard Worker break;
8042*f7c14bbaSAndroid Build Coastguard Worker }
8043*f7c14bbaSAndroid Build Coastguard Worker
8044*f7c14bbaSAndroid Build Coastguard Worker err = cb(sym_addr, sym_type, sym_name, ctx);
8045*f7c14bbaSAndroid Build Coastguard Worker if (err)
8046*f7c14bbaSAndroid Build Coastguard Worker break;
8047*f7c14bbaSAndroid Build Coastguard Worker }
8048*f7c14bbaSAndroid Build Coastguard Worker
8049*f7c14bbaSAndroid Build Coastguard Worker fclose(f);
8050*f7c14bbaSAndroid Build Coastguard Worker return err;
8051*f7c14bbaSAndroid Build Coastguard Worker }
8052*f7c14bbaSAndroid Build Coastguard Worker
kallsyms_cb(unsigned long long sym_addr,char sym_type,const char * sym_name,void * ctx)8053*f7c14bbaSAndroid Build Coastguard Worker static int kallsyms_cb(unsigned long long sym_addr, char sym_type,
8054*f7c14bbaSAndroid Build Coastguard Worker const char *sym_name, void *ctx)
8055*f7c14bbaSAndroid Build Coastguard Worker {
8056*f7c14bbaSAndroid Build Coastguard Worker struct bpf_object *obj = ctx;
8057*f7c14bbaSAndroid Build Coastguard Worker const struct btf_type *t;
8058*f7c14bbaSAndroid Build Coastguard Worker struct extern_desc *ext;
8059*f7c14bbaSAndroid Build Coastguard Worker
8060*f7c14bbaSAndroid Build Coastguard Worker ext = find_extern_by_name(obj, sym_name);
8061*f7c14bbaSAndroid Build Coastguard Worker if (!ext || ext->type != EXT_KSYM)
8062*f7c14bbaSAndroid Build Coastguard Worker return 0;
8063*f7c14bbaSAndroid Build Coastguard Worker
8064*f7c14bbaSAndroid Build Coastguard Worker t = btf__type_by_id(obj->btf, ext->btf_id);
8065*f7c14bbaSAndroid Build Coastguard Worker if (!btf_is_var(t))
8066*f7c14bbaSAndroid Build Coastguard Worker return 0;
8067*f7c14bbaSAndroid Build Coastguard Worker
8068*f7c14bbaSAndroid Build Coastguard Worker if (ext->is_set && ext->ksym.addr != sym_addr) {
8069*f7c14bbaSAndroid Build Coastguard Worker pr_warn("extern (ksym) '%s': resolution is ambiguous: 0x%llx or 0x%llx\n",
8070*f7c14bbaSAndroid Build Coastguard Worker sym_name, ext->ksym.addr, sym_addr);
8071*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
8072*f7c14bbaSAndroid Build Coastguard Worker }
8073*f7c14bbaSAndroid Build Coastguard Worker if (!ext->is_set) {
8074*f7c14bbaSAndroid Build Coastguard Worker ext->is_set = true;
8075*f7c14bbaSAndroid Build Coastguard Worker ext->ksym.addr = sym_addr;
8076*f7c14bbaSAndroid Build Coastguard Worker pr_debug("extern (ksym) '%s': set to 0x%llx\n", sym_name, sym_addr);
8077*f7c14bbaSAndroid Build Coastguard Worker }
8078*f7c14bbaSAndroid Build Coastguard Worker return 0;
8079*f7c14bbaSAndroid Build Coastguard Worker }
8080*f7c14bbaSAndroid Build Coastguard Worker
bpf_object__read_kallsyms_file(struct bpf_object * obj)8081*f7c14bbaSAndroid Build Coastguard Worker static int bpf_object__read_kallsyms_file(struct bpf_object *obj)
8082*f7c14bbaSAndroid Build Coastguard Worker {
8083*f7c14bbaSAndroid Build Coastguard Worker return libbpf_kallsyms_parse(kallsyms_cb, obj);
8084*f7c14bbaSAndroid Build Coastguard Worker }
8085*f7c14bbaSAndroid Build Coastguard Worker
find_ksym_btf_id(struct bpf_object * obj,const char * ksym_name,__u16 kind,struct btf ** res_btf,struct module_btf ** res_mod_btf)8086*f7c14bbaSAndroid Build Coastguard Worker static int find_ksym_btf_id(struct bpf_object *obj, const char *ksym_name,
8087*f7c14bbaSAndroid Build Coastguard Worker __u16 kind, struct btf **res_btf,
8088*f7c14bbaSAndroid Build Coastguard Worker struct module_btf **res_mod_btf)
8089*f7c14bbaSAndroid Build Coastguard Worker {
8090*f7c14bbaSAndroid Build Coastguard Worker struct module_btf *mod_btf;
8091*f7c14bbaSAndroid Build Coastguard Worker struct btf *btf;
8092*f7c14bbaSAndroid Build Coastguard Worker int i, id, err;
8093*f7c14bbaSAndroid Build Coastguard Worker
8094*f7c14bbaSAndroid Build Coastguard Worker btf = obj->btf_vmlinux;
8095*f7c14bbaSAndroid Build Coastguard Worker mod_btf = NULL;
8096*f7c14bbaSAndroid Build Coastguard Worker id = btf__find_by_name_kind(btf, ksym_name, kind);
8097*f7c14bbaSAndroid Build Coastguard Worker
8098*f7c14bbaSAndroid Build Coastguard Worker if (id == -ENOENT) {
8099*f7c14bbaSAndroid Build Coastguard Worker err = load_module_btfs(obj);
8100*f7c14bbaSAndroid Build Coastguard Worker if (err)
8101*f7c14bbaSAndroid Build Coastguard Worker return err;
8102*f7c14bbaSAndroid Build Coastguard Worker
8103*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < obj->btf_module_cnt; i++) {
8104*f7c14bbaSAndroid Build Coastguard Worker /* we assume module_btf's BTF FD is always >0 */
8105*f7c14bbaSAndroid Build Coastguard Worker mod_btf = &obj->btf_modules[i];
8106*f7c14bbaSAndroid Build Coastguard Worker btf = mod_btf->btf;
8107*f7c14bbaSAndroid Build Coastguard Worker id = btf__find_by_name_kind_own(btf, ksym_name, kind);
8108*f7c14bbaSAndroid Build Coastguard Worker if (id != -ENOENT)
8109*f7c14bbaSAndroid Build Coastguard Worker break;
8110*f7c14bbaSAndroid Build Coastguard Worker }
8111*f7c14bbaSAndroid Build Coastguard Worker }
8112*f7c14bbaSAndroid Build Coastguard Worker if (id <= 0)
8113*f7c14bbaSAndroid Build Coastguard Worker return -ESRCH;
8114*f7c14bbaSAndroid Build Coastguard Worker
8115*f7c14bbaSAndroid Build Coastguard Worker *res_btf = btf;
8116*f7c14bbaSAndroid Build Coastguard Worker *res_mod_btf = mod_btf;
8117*f7c14bbaSAndroid Build Coastguard Worker return id;
8118*f7c14bbaSAndroid Build Coastguard Worker }
8119*f7c14bbaSAndroid Build Coastguard Worker
bpf_object__resolve_ksym_var_btf_id(struct bpf_object * obj,struct extern_desc * ext)8120*f7c14bbaSAndroid Build Coastguard Worker static int bpf_object__resolve_ksym_var_btf_id(struct bpf_object *obj,
8121*f7c14bbaSAndroid Build Coastguard Worker struct extern_desc *ext)
8122*f7c14bbaSAndroid Build Coastguard Worker {
8123*f7c14bbaSAndroid Build Coastguard Worker const struct btf_type *targ_var, *targ_type;
8124*f7c14bbaSAndroid Build Coastguard Worker __u32 targ_type_id, local_type_id;
8125*f7c14bbaSAndroid Build Coastguard Worker struct module_btf *mod_btf = NULL;
8126*f7c14bbaSAndroid Build Coastguard Worker const char *targ_var_name;
8127*f7c14bbaSAndroid Build Coastguard Worker struct btf *btf = NULL;
8128*f7c14bbaSAndroid Build Coastguard Worker int id, err;
8129*f7c14bbaSAndroid Build Coastguard Worker
8130*f7c14bbaSAndroid Build Coastguard Worker id = find_ksym_btf_id(obj, ext->name, BTF_KIND_VAR, &btf, &mod_btf);
8131*f7c14bbaSAndroid Build Coastguard Worker if (id < 0) {
8132*f7c14bbaSAndroid Build Coastguard Worker if (id == -ESRCH && ext->is_weak)
8133*f7c14bbaSAndroid Build Coastguard Worker return 0;
8134*f7c14bbaSAndroid Build Coastguard Worker pr_warn("extern (var ksym) '%s': not found in kernel BTF\n",
8135*f7c14bbaSAndroid Build Coastguard Worker ext->name);
8136*f7c14bbaSAndroid Build Coastguard Worker return id;
8137*f7c14bbaSAndroid Build Coastguard Worker }
8138*f7c14bbaSAndroid Build Coastguard Worker
8139*f7c14bbaSAndroid Build Coastguard Worker /* find local type_id */
8140*f7c14bbaSAndroid Build Coastguard Worker local_type_id = ext->ksym.type_id;
8141*f7c14bbaSAndroid Build Coastguard Worker
8142*f7c14bbaSAndroid Build Coastguard Worker /* find target type_id */
8143*f7c14bbaSAndroid Build Coastguard Worker targ_var = btf__type_by_id(btf, id);
8144*f7c14bbaSAndroid Build Coastguard Worker targ_var_name = btf__name_by_offset(btf, targ_var->name_off);
8145*f7c14bbaSAndroid Build Coastguard Worker targ_type = skip_mods_and_typedefs(btf, targ_var->type, &targ_type_id);
8146*f7c14bbaSAndroid Build Coastguard Worker
8147*f7c14bbaSAndroid Build Coastguard Worker err = bpf_core_types_are_compat(obj->btf, local_type_id,
8148*f7c14bbaSAndroid Build Coastguard Worker btf, targ_type_id);
8149*f7c14bbaSAndroid Build Coastguard Worker if (err <= 0) {
8150*f7c14bbaSAndroid Build Coastguard Worker const struct btf_type *local_type;
8151*f7c14bbaSAndroid Build Coastguard Worker const char *targ_name, *local_name;
8152*f7c14bbaSAndroid Build Coastguard Worker
8153*f7c14bbaSAndroid Build Coastguard Worker local_type = btf__type_by_id(obj->btf, local_type_id);
8154*f7c14bbaSAndroid Build Coastguard Worker local_name = btf__name_by_offset(obj->btf, local_type->name_off);
8155*f7c14bbaSAndroid Build Coastguard Worker targ_name = btf__name_by_offset(btf, targ_type->name_off);
8156*f7c14bbaSAndroid Build Coastguard Worker
8157*f7c14bbaSAndroid Build Coastguard Worker pr_warn("extern (var ksym) '%s': incompatible types, expected [%d] %s %s, but kernel has [%d] %s %s\n",
8158*f7c14bbaSAndroid Build Coastguard Worker ext->name, local_type_id,
8159*f7c14bbaSAndroid Build Coastguard Worker btf_kind_str(local_type), local_name, targ_type_id,
8160*f7c14bbaSAndroid Build Coastguard Worker btf_kind_str(targ_type), targ_name);
8161*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
8162*f7c14bbaSAndroid Build Coastguard Worker }
8163*f7c14bbaSAndroid Build Coastguard Worker
8164*f7c14bbaSAndroid Build Coastguard Worker ext->is_set = true;
8165*f7c14bbaSAndroid Build Coastguard Worker ext->ksym.kernel_btf_obj_fd = mod_btf ? mod_btf->fd : 0;
8166*f7c14bbaSAndroid Build Coastguard Worker ext->ksym.kernel_btf_id = id;
8167*f7c14bbaSAndroid Build Coastguard Worker pr_debug("extern (var ksym) '%s': resolved to [%d] %s %s\n",
8168*f7c14bbaSAndroid Build Coastguard Worker ext->name, id, btf_kind_str(targ_var), targ_var_name);
8169*f7c14bbaSAndroid Build Coastguard Worker
8170*f7c14bbaSAndroid Build Coastguard Worker return 0;
8171*f7c14bbaSAndroid Build Coastguard Worker }
8172*f7c14bbaSAndroid Build Coastguard Worker
bpf_object__resolve_ksym_func_btf_id(struct bpf_object * obj,struct extern_desc * ext)8173*f7c14bbaSAndroid Build Coastguard Worker static int bpf_object__resolve_ksym_func_btf_id(struct bpf_object *obj,
8174*f7c14bbaSAndroid Build Coastguard Worker struct extern_desc *ext)
8175*f7c14bbaSAndroid Build Coastguard Worker {
8176*f7c14bbaSAndroid Build Coastguard Worker int local_func_proto_id, kfunc_proto_id, kfunc_id;
8177*f7c14bbaSAndroid Build Coastguard Worker struct module_btf *mod_btf = NULL;
8178*f7c14bbaSAndroid Build Coastguard Worker const struct btf_type *kern_func;
8179*f7c14bbaSAndroid Build Coastguard Worker struct btf *kern_btf = NULL;
8180*f7c14bbaSAndroid Build Coastguard Worker int ret;
8181*f7c14bbaSAndroid Build Coastguard Worker
8182*f7c14bbaSAndroid Build Coastguard Worker local_func_proto_id = ext->ksym.type_id;
8183*f7c14bbaSAndroid Build Coastguard Worker
8184*f7c14bbaSAndroid Build Coastguard Worker kfunc_id = find_ksym_btf_id(obj, ext->essent_name ?: ext->name, BTF_KIND_FUNC, &kern_btf,
8185*f7c14bbaSAndroid Build Coastguard Worker &mod_btf);
8186*f7c14bbaSAndroid Build Coastguard Worker if (kfunc_id < 0) {
8187*f7c14bbaSAndroid Build Coastguard Worker if (kfunc_id == -ESRCH && ext->is_weak)
8188*f7c14bbaSAndroid Build Coastguard Worker return 0;
8189*f7c14bbaSAndroid Build Coastguard Worker pr_warn("extern (func ksym) '%s': not found in kernel or module BTFs\n",
8190*f7c14bbaSAndroid Build Coastguard Worker ext->name);
8191*f7c14bbaSAndroid Build Coastguard Worker return kfunc_id;
8192*f7c14bbaSAndroid Build Coastguard Worker }
8193*f7c14bbaSAndroid Build Coastguard Worker
8194*f7c14bbaSAndroid Build Coastguard Worker kern_func = btf__type_by_id(kern_btf, kfunc_id);
8195*f7c14bbaSAndroid Build Coastguard Worker kfunc_proto_id = kern_func->type;
8196*f7c14bbaSAndroid Build Coastguard Worker
8197*f7c14bbaSAndroid Build Coastguard Worker ret = bpf_core_types_are_compat(obj->btf, local_func_proto_id,
8198*f7c14bbaSAndroid Build Coastguard Worker kern_btf, kfunc_proto_id);
8199*f7c14bbaSAndroid Build Coastguard Worker if (ret <= 0) {
8200*f7c14bbaSAndroid Build Coastguard Worker if (ext->is_weak)
8201*f7c14bbaSAndroid Build Coastguard Worker return 0;
8202*f7c14bbaSAndroid Build Coastguard Worker
8203*f7c14bbaSAndroid Build Coastguard Worker pr_warn("extern (func ksym) '%s': func_proto [%d] incompatible with %s [%d]\n",
8204*f7c14bbaSAndroid Build Coastguard Worker ext->name, local_func_proto_id,
8205*f7c14bbaSAndroid Build Coastguard Worker mod_btf ? mod_btf->name : "vmlinux", kfunc_proto_id);
8206*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
8207*f7c14bbaSAndroid Build Coastguard Worker }
8208*f7c14bbaSAndroid Build Coastguard Worker
8209*f7c14bbaSAndroid Build Coastguard Worker /* set index for module BTF fd in fd_array, if unset */
8210*f7c14bbaSAndroid Build Coastguard Worker if (mod_btf && !mod_btf->fd_array_idx) {
8211*f7c14bbaSAndroid Build Coastguard Worker /* insn->off is s16 */
8212*f7c14bbaSAndroid Build Coastguard Worker if (obj->fd_array_cnt == INT16_MAX) {
8213*f7c14bbaSAndroid Build Coastguard Worker pr_warn("extern (func ksym) '%s': module BTF fd index %d too big to fit in bpf_insn offset\n",
8214*f7c14bbaSAndroid Build Coastguard Worker ext->name, mod_btf->fd_array_idx);
8215*f7c14bbaSAndroid Build Coastguard Worker return -E2BIG;
8216*f7c14bbaSAndroid Build Coastguard Worker }
8217*f7c14bbaSAndroid Build Coastguard Worker /* Cannot use index 0 for module BTF fd */
8218*f7c14bbaSAndroid Build Coastguard Worker if (!obj->fd_array_cnt)
8219*f7c14bbaSAndroid Build Coastguard Worker obj->fd_array_cnt = 1;
8220*f7c14bbaSAndroid Build Coastguard Worker
8221*f7c14bbaSAndroid Build Coastguard Worker ret = libbpf_ensure_mem((void **)&obj->fd_array, &obj->fd_array_cap, sizeof(int),
8222*f7c14bbaSAndroid Build Coastguard Worker obj->fd_array_cnt + 1);
8223*f7c14bbaSAndroid Build Coastguard Worker if (ret)
8224*f7c14bbaSAndroid Build Coastguard Worker return ret;
8225*f7c14bbaSAndroid Build Coastguard Worker mod_btf->fd_array_idx = obj->fd_array_cnt;
8226*f7c14bbaSAndroid Build Coastguard Worker /* we assume module BTF FD is always >0 */
8227*f7c14bbaSAndroid Build Coastguard Worker obj->fd_array[obj->fd_array_cnt++] = mod_btf->fd;
8228*f7c14bbaSAndroid Build Coastguard Worker }
8229*f7c14bbaSAndroid Build Coastguard Worker
8230*f7c14bbaSAndroid Build Coastguard Worker ext->is_set = true;
8231*f7c14bbaSAndroid Build Coastguard Worker ext->ksym.kernel_btf_id = kfunc_id;
8232*f7c14bbaSAndroid Build Coastguard Worker ext->ksym.btf_fd_idx = mod_btf ? mod_btf->fd_array_idx : 0;
8233*f7c14bbaSAndroid Build Coastguard Worker /* Also set kernel_btf_obj_fd to make sure that bpf_object__relocate_data()
8234*f7c14bbaSAndroid Build Coastguard Worker * populates FD into ld_imm64 insn when it's used to point to kfunc.
8235*f7c14bbaSAndroid Build Coastguard Worker * {kernel_btf_id, btf_fd_idx} -> fixup bpf_call.
8236*f7c14bbaSAndroid Build Coastguard Worker * {kernel_btf_id, kernel_btf_obj_fd} -> fixup ld_imm64.
8237*f7c14bbaSAndroid Build Coastguard Worker */
8238*f7c14bbaSAndroid Build Coastguard Worker ext->ksym.kernel_btf_obj_fd = mod_btf ? mod_btf->fd : 0;
8239*f7c14bbaSAndroid Build Coastguard Worker pr_debug("extern (func ksym) '%s': resolved to %s [%d]\n",
8240*f7c14bbaSAndroid Build Coastguard Worker ext->name, mod_btf ? mod_btf->name : "vmlinux", kfunc_id);
8241*f7c14bbaSAndroid Build Coastguard Worker
8242*f7c14bbaSAndroid Build Coastguard Worker return 0;
8243*f7c14bbaSAndroid Build Coastguard Worker }
8244*f7c14bbaSAndroid Build Coastguard Worker
bpf_object__resolve_ksyms_btf_id(struct bpf_object * obj)8245*f7c14bbaSAndroid Build Coastguard Worker static int bpf_object__resolve_ksyms_btf_id(struct bpf_object *obj)
8246*f7c14bbaSAndroid Build Coastguard Worker {
8247*f7c14bbaSAndroid Build Coastguard Worker const struct btf_type *t;
8248*f7c14bbaSAndroid Build Coastguard Worker struct extern_desc *ext;
8249*f7c14bbaSAndroid Build Coastguard Worker int i, err;
8250*f7c14bbaSAndroid Build Coastguard Worker
8251*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < obj->nr_extern; i++) {
8252*f7c14bbaSAndroid Build Coastguard Worker ext = &obj->externs[i];
8253*f7c14bbaSAndroid Build Coastguard Worker if (ext->type != EXT_KSYM || !ext->ksym.type_id)
8254*f7c14bbaSAndroid Build Coastguard Worker continue;
8255*f7c14bbaSAndroid Build Coastguard Worker
8256*f7c14bbaSAndroid Build Coastguard Worker if (obj->gen_loader) {
8257*f7c14bbaSAndroid Build Coastguard Worker ext->is_set = true;
8258*f7c14bbaSAndroid Build Coastguard Worker ext->ksym.kernel_btf_obj_fd = 0;
8259*f7c14bbaSAndroid Build Coastguard Worker ext->ksym.kernel_btf_id = 0;
8260*f7c14bbaSAndroid Build Coastguard Worker continue;
8261*f7c14bbaSAndroid Build Coastguard Worker }
8262*f7c14bbaSAndroid Build Coastguard Worker t = btf__type_by_id(obj->btf, ext->btf_id);
8263*f7c14bbaSAndroid Build Coastguard Worker if (btf_is_var(t))
8264*f7c14bbaSAndroid Build Coastguard Worker err = bpf_object__resolve_ksym_var_btf_id(obj, ext);
8265*f7c14bbaSAndroid Build Coastguard Worker else
8266*f7c14bbaSAndroid Build Coastguard Worker err = bpf_object__resolve_ksym_func_btf_id(obj, ext);
8267*f7c14bbaSAndroid Build Coastguard Worker if (err)
8268*f7c14bbaSAndroid Build Coastguard Worker return err;
8269*f7c14bbaSAndroid Build Coastguard Worker }
8270*f7c14bbaSAndroid Build Coastguard Worker return 0;
8271*f7c14bbaSAndroid Build Coastguard Worker }
8272*f7c14bbaSAndroid Build Coastguard Worker
bpf_object__resolve_externs(struct bpf_object * obj,const char * extra_kconfig)8273*f7c14bbaSAndroid Build Coastguard Worker static int bpf_object__resolve_externs(struct bpf_object *obj,
8274*f7c14bbaSAndroid Build Coastguard Worker const char *extra_kconfig)
8275*f7c14bbaSAndroid Build Coastguard Worker {
8276*f7c14bbaSAndroid Build Coastguard Worker bool need_config = false, need_kallsyms = false;
8277*f7c14bbaSAndroid Build Coastguard Worker bool need_vmlinux_btf = false;
8278*f7c14bbaSAndroid Build Coastguard Worker struct extern_desc *ext;
8279*f7c14bbaSAndroid Build Coastguard Worker void *kcfg_data = NULL;
8280*f7c14bbaSAndroid Build Coastguard Worker int err, i;
8281*f7c14bbaSAndroid Build Coastguard Worker
8282*f7c14bbaSAndroid Build Coastguard Worker if (obj->nr_extern == 0)
8283*f7c14bbaSAndroid Build Coastguard Worker return 0;
8284*f7c14bbaSAndroid Build Coastguard Worker
8285*f7c14bbaSAndroid Build Coastguard Worker if (obj->kconfig_map_idx >= 0)
8286*f7c14bbaSAndroid Build Coastguard Worker kcfg_data = obj->maps[obj->kconfig_map_idx].mmaped;
8287*f7c14bbaSAndroid Build Coastguard Worker
8288*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < obj->nr_extern; i++) {
8289*f7c14bbaSAndroid Build Coastguard Worker ext = &obj->externs[i];
8290*f7c14bbaSAndroid Build Coastguard Worker
8291*f7c14bbaSAndroid Build Coastguard Worker if (ext->type == EXT_KSYM) {
8292*f7c14bbaSAndroid Build Coastguard Worker if (ext->ksym.type_id)
8293*f7c14bbaSAndroid Build Coastguard Worker need_vmlinux_btf = true;
8294*f7c14bbaSAndroid Build Coastguard Worker else
8295*f7c14bbaSAndroid Build Coastguard Worker need_kallsyms = true;
8296*f7c14bbaSAndroid Build Coastguard Worker continue;
8297*f7c14bbaSAndroid Build Coastguard Worker } else if (ext->type == EXT_KCFG) {
8298*f7c14bbaSAndroid Build Coastguard Worker void *ext_ptr = kcfg_data + ext->kcfg.data_off;
8299*f7c14bbaSAndroid Build Coastguard Worker __u64 value = 0;
8300*f7c14bbaSAndroid Build Coastguard Worker
8301*f7c14bbaSAndroid Build Coastguard Worker /* Kconfig externs need actual /proc/config.gz */
8302*f7c14bbaSAndroid Build Coastguard Worker if (str_has_pfx(ext->name, "CONFIG_")) {
8303*f7c14bbaSAndroid Build Coastguard Worker need_config = true;
8304*f7c14bbaSAndroid Build Coastguard Worker continue;
8305*f7c14bbaSAndroid Build Coastguard Worker }
8306*f7c14bbaSAndroid Build Coastguard Worker
8307*f7c14bbaSAndroid Build Coastguard Worker /* Virtual kcfg externs are customly handled by libbpf */
8308*f7c14bbaSAndroid Build Coastguard Worker if (strcmp(ext->name, "LINUX_KERNEL_VERSION") == 0) {
8309*f7c14bbaSAndroid Build Coastguard Worker value = get_kernel_version();
8310*f7c14bbaSAndroid Build Coastguard Worker if (!value) {
8311*f7c14bbaSAndroid Build Coastguard Worker pr_warn("extern (kcfg) '%s': failed to get kernel version\n", ext->name);
8312*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
8313*f7c14bbaSAndroid Build Coastguard Worker }
8314*f7c14bbaSAndroid Build Coastguard Worker } else if (strcmp(ext->name, "LINUX_HAS_BPF_COOKIE") == 0) {
8315*f7c14bbaSAndroid Build Coastguard Worker value = kernel_supports(obj, FEAT_BPF_COOKIE);
8316*f7c14bbaSAndroid Build Coastguard Worker } else if (strcmp(ext->name, "LINUX_HAS_SYSCALL_WRAPPER") == 0) {
8317*f7c14bbaSAndroid Build Coastguard Worker value = kernel_supports(obj, FEAT_SYSCALL_WRAPPER);
8318*f7c14bbaSAndroid Build Coastguard Worker } else if (!str_has_pfx(ext->name, "LINUX_") || !ext->is_weak) {
8319*f7c14bbaSAndroid Build Coastguard Worker /* Currently libbpf supports only CONFIG_ and LINUX_ prefixed
8320*f7c14bbaSAndroid Build Coastguard Worker * __kconfig externs, where LINUX_ ones are virtual and filled out
8321*f7c14bbaSAndroid Build Coastguard Worker * customly by libbpf (their values don't come from Kconfig).
8322*f7c14bbaSAndroid Build Coastguard Worker * If LINUX_xxx variable is not recognized by libbpf, but is marked
8323*f7c14bbaSAndroid Build Coastguard Worker * __weak, it defaults to zero value, just like for CONFIG_xxx
8324*f7c14bbaSAndroid Build Coastguard Worker * externs.
8325*f7c14bbaSAndroid Build Coastguard Worker */
8326*f7c14bbaSAndroid Build Coastguard Worker pr_warn("extern (kcfg) '%s': unrecognized virtual extern\n", ext->name);
8327*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
8328*f7c14bbaSAndroid Build Coastguard Worker }
8329*f7c14bbaSAndroid Build Coastguard Worker
8330*f7c14bbaSAndroid Build Coastguard Worker err = set_kcfg_value_num(ext, ext_ptr, value);
8331*f7c14bbaSAndroid Build Coastguard Worker if (err)
8332*f7c14bbaSAndroid Build Coastguard Worker return err;
8333*f7c14bbaSAndroid Build Coastguard Worker pr_debug("extern (kcfg) '%s': set to 0x%llx\n",
8334*f7c14bbaSAndroid Build Coastguard Worker ext->name, (long long)value);
8335*f7c14bbaSAndroid Build Coastguard Worker } else {
8336*f7c14bbaSAndroid Build Coastguard Worker pr_warn("extern '%s': unrecognized extern kind\n", ext->name);
8337*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
8338*f7c14bbaSAndroid Build Coastguard Worker }
8339*f7c14bbaSAndroid Build Coastguard Worker }
8340*f7c14bbaSAndroid Build Coastguard Worker if (need_config && extra_kconfig) {
8341*f7c14bbaSAndroid Build Coastguard Worker err = bpf_object__read_kconfig_mem(obj, extra_kconfig, kcfg_data);
8342*f7c14bbaSAndroid Build Coastguard Worker if (err)
8343*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
8344*f7c14bbaSAndroid Build Coastguard Worker need_config = false;
8345*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < obj->nr_extern; i++) {
8346*f7c14bbaSAndroid Build Coastguard Worker ext = &obj->externs[i];
8347*f7c14bbaSAndroid Build Coastguard Worker if (ext->type == EXT_KCFG && !ext->is_set) {
8348*f7c14bbaSAndroid Build Coastguard Worker need_config = true;
8349*f7c14bbaSAndroid Build Coastguard Worker break;
8350*f7c14bbaSAndroid Build Coastguard Worker }
8351*f7c14bbaSAndroid Build Coastguard Worker }
8352*f7c14bbaSAndroid Build Coastguard Worker }
8353*f7c14bbaSAndroid Build Coastguard Worker if (need_config) {
8354*f7c14bbaSAndroid Build Coastguard Worker err = bpf_object__read_kconfig_file(obj, kcfg_data);
8355*f7c14bbaSAndroid Build Coastguard Worker if (err)
8356*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
8357*f7c14bbaSAndroid Build Coastguard Worker }
8358*f7c14bbaSAndroid Build Coastguard Worker if (need_kallsyms) {
8359*f7c14bbaSAndroid Build Coastguard Worker err = bpf_object__read_kallsyms_file(obj);
8360*f7c14bbaSAndroid Build Coastguard Worker if (err)
8361*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
8362*f7c14bbaSAndroid Build Coastguard Worker }
8363*f7c14bbaSAndroid Build Coastguard Worker if (need_vmlinux_btf) {
8364*f7c14bbaSAndroid Build Coastguard Worker err = bpf_object__resolve_ksyms_btf_id(obj);
8365*f7c14bbaSAndroid Build Coastguard Worker if (err)
8366*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
8367*f7c14bbaSAndroid Build Coastguard Worker }
8368*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < obj->nr_extern; i++) {
8369*f7c14bbaSAndroid Build Coastguard Worker ext = &obj->externs[i];
8370*f7c14bbaSAndroid Build Coastguard Worker
8371*f7c14bbaSAndroid Build Coastguard Worker if (!ext->is_set && !ext->is_weak) {
8372*f7c14bbaSAndroid Build Coastguard Worker pr_warn("extern '%s' (strong): not resolved\n", ext->name);
8373*f7c14bbaSAndroid Build Coastguard Worker return -ESRCH;
8374*f7c14bbaSAndroid Build Coastguard Worker } else if (!ext->is_set) {
8375*f7c14bbaSAndroid Build Coastguard Worker pr_debug("extern '%s' (weak): not resolved, defaulting to zero\n",
8376*f7c14bbaSAndroid Build Coastguard Worker ext->name);
8377*f7c14bbaSAndroid Build Coastguard Worker }
8378*f7c14bbaSAndroid Build Coastguard Worker }
8379*f7c14bbaSAndroid Build Coastguard Worker
8380*f7c14bbaSAndroid Build Coastguard Worker return 0;
8381*f7c14bbaSAndroid Build Coastguard Worker }
8382*f7c14bbaSAndroid Build Coastguard Worker
bpf_map_prepare_vdata(const struct bpf_map * map)8383*f7c14bbaSAndroid Build Coastguard Worker static void bpf_map_prepare_vdata(const struct bpf_map *map)
8384*f7c14bbaSAndroid Build Coastguard Worker {
8385*f7c14bbaSAndroid Build Coastguard Worker struct bpf_struct_ops *st_ops;
8386*f7c14bbaSAndroid Build Coastguard Worker __u32 i;
8387*f7c14bbaSAndroid Build Coastguard Worker
8388*f7c14bbaSAndroid Build Coastguard Worker st_ops = map->st_ops;
8389*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < btf_vlen(st_ops->type); i++) {
8390*f7c14bbaSAndroid Build Coastguard Worker struct bpf_program *prog = st_ops->progs[i];
8391*f7c14bbaSAndroid Build Coastguard Worker void *kern_data;
8392*f7c14bbaSAndroid Build Coastguard Worker int prog_fd;
8393*f7c14bbaSAndroid Build Coastguard Worker
8394*f7c14bbaSAndroid Build Coastguard Worker if (!prog)
8395*f7c14bbaSAndroid Build Coastguard Worker continue;
8396*f7c14bbaSAndroid Build Coastguard Worker
8397*f7c14bbaSAndroid Build Coastguard Worker prog_fd = bpf_program__fd(prog);
8398*f7c14bbaSAndroid Build Coastguard Worker kern_data = st_ops->kern_vdata + st_ops->kern_func_off[i];
8399*f7c14bbaSAndroid Build Coastguard Worker *(unsigned long *)kern_data = prog_fd;
8400*f7c14bbaSAndroid Build Coastguard Worker }
8401*f7c14bbaSAndroid Build Coastguard Worker }
8402*f7c14bbaSAndroid Build Coastguard Worker
bpf_object_prepare_struct_ops(struct bpf_object * obj)8403*f7c14bbaSAndroid Build Coastguard Worker static int bpf_object_prepare_struct_ops(struct bpf_object *obj)
8404*f7c14bbaSAndroid Build Coastguard Worker {
8405*f7c14bbaSAndroid Build Coastguard Worker struct bpf_map *map;
8406*f7c14bbaSAndroid Build Coastguard Worker int i;
8407*f7c14bbaSAndroid Build Coastguard Worker
8408*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < obj->nr_maps; i++) {
8409*f7c14bbaSAndroid Build Coastguard Worker map = &obj->maps[i];
8410*f7c14bbaSAndroid Build Coastguard Worker
8411*f7c14bbaSAndroid Build Coastguard Worker if (!bpf_map__is_struct_ops(map))
8412*f7c14bbaSAndroid Build Coastguard Worker continue;
8413*f7c14bbaSAndroid Build Coastguard Worker
8414*f7c14bbaSAndroid Build Coastguard Worker if (!map->autocreate)
8415*f7c14bbaSAndroid Build Coastguard Worker continue;
8416*f7c14bbaSAndroid Build Coastguard Worker
8417*f7c14bbaSAndroid Build Coastguard Worker bpf_map_prepare_vdata(map);
8418*f7c14bbaSAndroid Build Coastguard Worker }
8419*f7c14bbaSAndroid Build Coastguard Worker
8420*f7c14bbaSAndroid Build Coastguard Worker return 0;
8421*f7c14bbaSAndroid Build Coastguard Worker }
8422*f7c14bbaSAndroid Build Coastguard Worker
bpf_object_load(struct bpf_object * obj,int extra_log_level,const char * target_btf_path)8423*f7c14bbaSAndroid Build Coastguard Worker static int bpf_object_load(struct bpf_object *obj, int extra_log_level, const char *target_btf_path)
8424*f7c14bbaSAndroid Build Coastguard Worker {
8425*f7c14bbaSAndroid Build Coastguard Worker int err, i;
8426*f7c14bbaSAndroid Build Coastguard Worker
8427*f7c14bbaSAndroid Build Coastguard Worker if (!obj)
8428*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-EINVAL);
8429*f7c14bbaSAndroid Build Coastguard Worker
8430*f7c14bbaSAndroid Build Coastguard Worker if (obj->loaded) {
8431*f7c14bbaSAndroid Build Coastguard Worker pr_warn("object '%s': load can't be attempted twice\n", obj->name);
8432*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-EINVAL);
8433*f7c14bbaSAndroid Build Coastguard Worker }
8434*f7c14bbaSAndroid Build Coastguard Worker
8435*f7c14bbaSAndroid Build Coastguard Worker if (obj->gen_loader)
8436*f7c14bbaSAndroid Build Coastguard Worker bpf_gen__init(obj->gen_loader, extra_log_level, obj->nr_programs, obj->nr_maps);
8437*f7c14bbaSAndroid Build Coastguard Worker
8438*f7c14bbaSAndroid Build Coastguard Worker err = bpf_object_prepare_token(obj);
8439*f7c14bbaSAndroid Build Coastguard Worker err = err ? : bpf_object__probe_loading(obj);
8440*f7c14bbaSAndroid Build Coastguard Worker err = err ? : bpf_object__load_vmlinux_btf(obj, false);
8441*f7c14bbaSAndroid Build Coastguard Worker err = err ? : bpf_object__resolve_externs(obj, obj->kconfig);
8442*f7c14bbaSAndroid Build Coastguard Worker err = err ? : bpf_object__sanitize_maps(obj);
8443*f7c14bbaSAndroid Build Coastguard Worker err = err ? : bpf_object__init_kern_struct_ops_maps(obj);
8444*f7c14bbaSAndroid Build Coastguard Worker err = err ? : bpf_object_adjust_struct_ops_autoload(obj);
8445*f7c14bbaSAndroid Build Coastguard Worker err = err ? : bpf_object__relocate(obj, obj->btf_custom_path ? : target_btf_path);
8446*f7c14bbaSAndroid Build Coastguard Worker err = err ? : bpf_object__sanitize_and_load_btf(obj);
8447*f7c14bbaSAndroid Build Coastguard Worker err = err ? : bpf_object__create_maps(obj);
8448*f7c14bbaSAndroid Build Coastguard Worker err = err ? : bpf_object__load_progs(obj, extra_log_level);
8449*f7c14bbaSAndroid Build Coastguard Worker err = err ? : bpf_object_init_prog_arrays(obj);
8450*f7c14bbaSAndroid Build Coastguard Worker err = err ? : bpf_object_prepare_struct_ops(obj);
8451*f7c14bbaSAndroid Build Coastguard Worker
8452*f7c14bbaSAndroid Build Coastguard Worker if (obj->gen_loader) {
8453*f7c14bbaSAndroid Build Coastguard Worker /* reset FDs */
8454*f7c14bbaSAndroid Build Coastguard Worker if (obj->btf)
8455*f7c14bbaSAndroid Build Coastguard Worker btf__set_fd(obj->btf, -1);
8456*f7c14bbaSAndroid Build Coastguard Worker if (!err)
8457*f7c14bbaSAndroid Build Coastguard Worker err = bpf_gen__finish(obj->gen_loader, obj->nr_programs, obj->nr_maps);
8458*f7c14bbaSAndroid Build Coastguard Worker }
8459*f7c14bbaSAndroid Build Coastguard Worker
8460*f7c14bbaSAndroid Build Coastguard Worker /* clean up fd_array */
8461*f7c14bbaSAndroid Build Coastguard Worker zfree(&obj->fd_array);
8462*f7c14bbaSAndroid Build Coastguard Worker
8463*f7c14bbaSAndroid Build Coastguard Worker /* clean up module BTFs */
8464*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < obj->btf_module_cnt; i++) {
8465*f7c14bbaSAndroid Build Coastguard Worker close(obj->btf_modules[i].fd);
8466*f7c14bbaSAndroid Build Coastguard Worker btf__free(obj->btf_modules[i].btf);
8467*f7c14bbaSAndroid Build Coastguard Worker free(obj->btf_modules[i].name);
8468*f7c14bbaSAndroid Build Coastguard Worker }
8469*f7c14bbaSAndroid Build Coastguard Worker free(obj->btf_modules);
8470*f7c14bbaSAndroid Build Coastguard Worker
8471*f7c14bbaSAndroid Build Coastguard Worker /* clean up vmlinux BTF */
8472*f7c14bbaSAndroid Build Coastguard Worker btf__free(obj->btf_vmlinux);
8473*f7c14bbaSAndroid Build Coastguard Worker obj->btf_vmlinux = NULL;
8474*f7c14bbaSAndroid Build Coastguard Worker
8475*f7c14bbaSAndroid Build Coastguard Worker obj->loaded = true; /* doesn't matter if successfully or not */
8476*f7c14bbaSAndroid Build Coastguard Worker
8477*f7c14bbaSAndroid Build Coastguard Worker if (err)
8478*f7c14bbaSAndroid Build Coastguard Worker goto out;
8479*f7c14bbaSAndroid Build Coastguard Worker
8480*f7c14bbaSAndroid Build Coastguard Worker return 0;
8481*f7c14bbaSAndroid Build Coastguard Worker out:
8482*f7c14bbaSAndroid Build Coastguard Worker /* unpin any maps that were auto-pinned during load */
8483*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < obj->nr_maps; i++)
8484*f7c14bbaSAndroid Build Coastguard Worker if (obj->maps[i].pinned && !obj->maps[i].reused)
8485*f7c14bbaSAndroid Build Coastguard Worker bpf_map__unpin(&obj->maps[i], NULL);
8486*f7c14bbaSAndroid Build Coastguard Worker
8487*f7c14bbaSAndroid Build Coastguard Worker bpf_object_unload(obj);
8488*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to load object '%s'\n", obj->path);
8489*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(err);
8490*f7c14bbaSAndroid Build Coastguard Worker }
8491*f7c14bbaSAndroid Build Coastguard Worker
bpf_object__load(struct bpf_object * obj)8492*f7c14bbaSAndroid Build Coastguard Worker int bpf_object__load(struct bpf_object *obj)
8493*f7c14bbaSAndroid Build Coastguard Worker {
8494*f7c14bbaSAndroid Build Coastguard Worker return bpf_object_load(obj, 0, NULL);
8495*f7c14bbaSAndroid Build Coastguard Worker }
8496*f7c14bbaSAndroid Build Coastguard Worker
make_parent_dir(const char * path)8497*f7c14bbaSAndroid Build Coastguard Worker static int make_parent_dir(const char *path)
8498*f7c14bbaSAndroid Build Coastguard Worker {
8499*f7c14bbaSAndroid Build Coastguard Worker char *cp, errmsg[STRERR_BUFSIZE];
8500*f7c14bbaSAndroid Build Coastguard Worker char *dname, *dir;
8501*f7c14bbaSAndroid Build Coastguard Worker int err = 0;
8502*f7c14bbaSAndroid Build Coastguard Worker
8503*f7c14bbaSAndroid Build Coastguard Worker dname = strdup(path);
8504*f7c14bbaSAndroid Build Coastguard Worker if (dname == NULL)
8505*f7c14bbaSAndroid Build Coastguard Worker return -ENOMEM;
8506*f7c14bbaSAndroid Build Coastguard Worker
8507*f7c14bbaSAndroid Build Coastguard Worker dir = dirname(dname);
8508*f7c14bbaSAndroid Build Coastguard Worker if (mkdir(dir, 0700) && errno != EEXIST)
8509*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
8510*f7c14bbaSAndroid Build Coastguard Worker
8511*f7c14bbaSAndroid Build Coastguard Worker free(dname);
8512*f7c14bbaSAndroid Build Coastguard Worker if (err) {
8513*f7c14bbaSAndroid Build Coastguard Worker cp = libbpf_strerror_r(-err, errmsg, sizeof(errmsg));
8514*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to mkdir %s: %s\n", path, cp);
8515*f7c14bbaSAndroid Build Coastguard Worker }
8516*f7c14bbaSAndroid Build Coastguard Worker return err;
8517*f7c14bbaSAndroid Build Coastguard Worker }
8518*f7c14bbaSAndroid Build Coastguard Worker
check_path(const char * path)8519*f7c14bbaSAndroid Build Coastguard Worker static int check_path(const char *path)
8520*f7c14bbaSAndroid Build Coastguard Worker {
8521*f7c14bbaSAndroid Build Coastguard Worker char *cp, errmsg[STRERR_BUFSIZE];
8522*f7c14bbaSAndroid Build Coastguard Worker struct statfs st_fs;
8523*f7c14bbaSAndroid Build Coastguard Worker char *dname, *dir;
8524*f7c14bbaSAndroid Build Coastguard Worker int err = 0;
8525*f7c14bbaSAndroid Build Coastguard Worker
8526*f7c14bbaSAndroid Build Coastguard Worker if (path == NULL)
8527*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
8528*f7c14bbaSAndroid Build Coastguard Worker
8529*f7c14bbaSAndroid Build Coastguard Worker dname = strdup(path);
8530*f7c14bbaSAndroid Build Coastguard Worker if (dname == NULL)
8531*f7c14bbaSAndroid Build Coastguard Worker return -ENOMEM;
8532*f7c14bbaSAndroid Build Coastguard Worker
8533*f7c14bbaSAndroid Build Coastguard Worker dir = dirname(dname);
8534*f7c14bbaSAndroid Build Coastguard Worker if (statfs(dir, &st_fs)) {
8535*f7c14bbaSAndroid Build Coastguard Worker cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
8536*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to statfs %s: %s\n", dir, cp);
8537*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
8538*f7c14bbaSAndroid Build Coastguard Worker }
8539*f7c14bbaSAndroid Build Coastguard Worker free(dname);
8540*f7c14bbaSAndroid Build Coastguard Worker
8541*f7c14bbaSAndroid Build Coastguard Worker if (!err && st_fs.f_type != BPF_FS_MAGIC) {
8542*f7c14bbaSAndroid Build Coastguard Worker pr_warn("specified path %s is not on BPF FS\n", path);
8543*f7c14bbaSAndroid Build Coastguard Worker err = -EINVAL;
8544*f7c14bbaSAndroid Build Coastguard Worker }
8545*f7c14bbaSAndroid Build Coastguard Worker
8546*f7c14bbaSAndroid Build Coastguard Worker return err;
8547*f7c14bbaSAndroid Build Coastguard Worker }
8548*f7c14bbaSAndroid Build Coastguard Worker
bpf_program__pin(struct bpf_program * prog,const char * path)8549*f7c14bbaSAndroid Build Coastguard Worker int bpf_program__pin(struct bpf_program *prog, const char *path)
8550*f7c14bbaSAndroid Build Coastguard Worker {
8551*f7c14bbaSAndroid Build Coastguard Worker char *cp, errmsg[STRERR_BUFSIZE];
8552*f7c14bbaSAndroid Build Coastguard Worker int err;
8553*f7c14bbaSAndroid Build Coastguard Worker
8554*f7c14bbaSAndroid Build Coastguard Worker if (prog->fd < 0) {
8555*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': can't pin program that wasn't loaded\n", prog->name);
8556*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-EINVAL);
8557*f7c14bbaSAndroid Build Coastguard Worker }
8558*f7c14bbaSAndroid Build Coastguard Worker
8559*f7c14bbaSAndroid Build Coastguard Worker err = make_parent_dir(path);
8560*f7c14bbaSAndroid Build Coastguard Worker if (err)
8561*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(err);
8562*f7c14bbaSAndroid Build Coastguard Worker
8563*f7c14bbaSAndroid Build Coastguard Worker err = check_path(path);
8564*f7c14bbaSAndroid Build Coastguard Worker if (err)
8565*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(err);
8566*f7c14bbaSAndroid Build Coastguard Worker
8567*f7c14bbaSAndroid Build Coastguard Worker if (bpf_obj_pin(prog->fd, path)) {
8568*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
8569*f7c14bbaSAndroid Build Coastguard Worker cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg));
8570*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': failed to pin at '%s': %s\n", prog->name, path, cp);
8571*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(err);
8572*f7c14bbaSAndroid Build Coastguard Worker }
8573*f7c14bbaSAndroid Build Coastguard Worker
8574*f7c14bbaSAndroid Build Coastguard Worker pr_debug("prog '%s': pinned at '%s'\n", prog->name, path);
8575*f7c14bbaSAndroid Build Coastguard Worker return 0;
8576*f7c14bbaSAndroid Build Coastguard Worker }
8577*f7c14bbaSAndroid Build Coastguard Worker
bpf_program__unpin(struct bpf_program * prog,const char * path)8578*f7c14bbaSAndroid Build Coastguard Worker int bpf_program__unpin(struct bpf_program *prog, const char *path)
8579*f7c14bbaSAndroid Build Coastguard Worker {
8580*f7c14bbaSAndroid Build Coastguard Worker int err;
8581*f7c14bbaSAndroid Build Coastguard Worker
8582*f7c14bbaSAndroid Build Coastguard Worker if (prog->fd < 0) {
8583*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': can't unpin program that wasn't loaded\n", prog->name);
8584*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-EINVAL);
8585*f7c14bbaSAndroid Build Coastguard Worker }
8586*f7c14bbaSAndroid Build Coastguard Worker
8587*f7c14bbaSAndroid Build Coastguard Worker err = check_path(path);
8588*f7c14bbaSAndroid Build Coastguard Worker if (err)
8589*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(err);
8590*f7c14bbaSAndroid Build Coastguard Worker
8591*f7c14bbaSAndroid Build Coastguard Worker err = unlink(path);
8592*f7c14bbaSAndroid Build Coastguard Worker if (err)
8593*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-errno);
8594*f7c14bbaSAndroid Build Coastguard Worker
8595*f7c14bbaSAndroid Build Coastguard Worker pr_debug("prog '%s': unpinned from '%s'\n", prog->name, path);
8596*f7c14bbaSAndroid Build Coastguard Worker return 0;
8597*f7c14bbaSAndroid Build Coastguard Worker }
8598*f7c14bbaSAndroid Build Coastguard Worker
bpf_map__pin(struct bpf_map * map,const char * path)8599*f7c14bbaSAndroid Build Coastguard Worker int bpf_map__pin(struct bpf_map *map, const char *path)
8600*f7c14bbaSAndroid Build Coastguard Worker {
8601*f7c14bbaSAndroid Build Coastguard Worker char *cp, errmsg[STRERR_BUFSIZE];
8602*f7c14bbaSAndroid Build Coastguard Worker int err;
8603*f7c14bbaSAndroid Build Coastguard Worker
8604*f7c14bbaSAndroid Build Coastguard Worker if (map == NULL) {
8605*f7c14bbaSAndroid Build Coastguard Worker pr_warn("invalid map pointer\n");
8606*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-EINVAL);
8607*f7c14bbaSAndroid Build Coastguard Worker }
8608*f7c14bbaSAndroid Build Coastguard Worker
8609*f7c14bbaSAndroid Build Coastguard Worker if (map->fd < 0) {
8610*f7c14bbaSAndroid Build Coastguard Worker pr_warn("map '%s': can't pin BPF map without FD (was it created?)\n", map->name);
8611*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-EINVAL);
8612*f7c14bbaSAndroid Build Coastguard Worker }
8613*f7c14bbaSAndroid Build Coastguard Worker
8614*f7c14bbaSAndroid Build Coastguard Worker if (map->pin_path) {
8615*f7c14bbaSAndroid Build Coastguard Worker if (path && strcmp(path, map->pin_path)) {
8616*f7c14bbaSAndroid Build Coastguard Worker pr_warn("map '%s' already has pin path '%s' different from '%s'\n",
8617*f7c14bbaSAndroid Build Coastguard Worker bpf_map__name(map), map->pin_path, path);
8618*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-EINVAL);
8619*f7c14bbaSAndroid Build Coastguard Worker } else if (map->pinned) {
8620*f7c14bbaSAndroid Build Coastguard Worker pr_debug("map '%s' already pinned at '%s'; not re-pinning\n",
8621*f7c14bbaSAndroid Build Coastguard Worker bpf_map__name(map), map->pin_path);
8622*f7c14bbaSAndroid Build Coastguard Worker return 0;
8623*f7c14bbaSAndroid Build Coastguard Worker }
8624*f7c14bbaSAndroid Build Coastguard Worker } else {
8625*f7c14bbaSAndroid Build Coastguard Worker if (!path) {
8626*f7c14bbaSAndroid Build Coastguard Worker pr_warn("missing a path to pin map '%s' at\n",
8627*f7c14bbaSAndroid Build Coastguard Worker bpf_map__name(map));
8628*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-EINVAL);
8629*f7c14bbaSAndroid Build Coastguard Worker } else if (map->pinned) {
8630*f7c14bbaSAndroid Build Coastguard Worker pr_warn("map '%s' already pinned\n", bpf_map__name(map));
8631*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-EEXIST);
8632*f7c14bbaSAndroid Build Coastguard Worker }
8633*f7c14bbaSAndroid Build Coastguard Worker
8634*f7c14bbaSAndroid Build Coastguard Worker map->pin_path = strdup(path);
8635*f7c14bbaSAndroid Build Coastguard Worker if (!map->pin_path) {
8636*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
8637*f7c14bbaSAndroid Build Coastguard Worker goto out_err;
8638*f7c14bbaSAndroid Build Coastguard Worker }
8639*f7c14bbaSAndroid Build Coastguard Worker }
8640*f7c14bbaSAndroid Build Coastguard Worker
8641*f7c14bbaSAndroid Build Coastguard Worker err = make_parent_dir(map->pin_path);
8642*f7c14bbaSAndroid Build Coastguard Worker if (err)
8643*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(err);
8644*f7c14bbaSAndroid Build Coastguard Worker
8645*f7c14bbaSAndroid Build Coastguard Worker err = check_path(map->pin_path);
8646*f7c14bbaSAndroid Build Coastguard Worker if (err)
8647*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(err);
8648*f7c14bbaSAndroid Build Coastguard Worker
8649*f7c14bbaSAndroid Build Coastguard Worker if (bpf_obj_pin(map->fd, map->pin_path)) {
8650*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
8651*f7c14bbaSAndroid Build Coastguard Worker goto out_err;
8652*f7c14bbaSAndroid Build Coastguard Worker }
8653*f7c14bbaSAndroid Build Coastguard Worker
8654*f7c14bbaSAndroid Build Coastguard Worker map->pinned = true;
8655*f7c14bbaSAndroid Build Coastguard Worker pr_debug("pinned map '%s'\n", map->pin_path);
8656*f7c14bbaSAndroid Build Coastguard Worker
8657*f7c14bbaSAndroid Build Coastguard Worker return 0;
8658*f7c14bbaSAndroid Build Coastguard Worker
8659*f7c14bbaSAndroid Build Coastguard Worker out_err:
8660*f7c14bbaSAndroid Build Coastguard Worker cp = libbpf_strerror_r(-err, errmsg, sizeof(errmsg));
8661*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to pin map: %s\n", cp);
8662*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(err);
8663*f7c14bbaSAndroid Build Coastguard Worker }
8664*f7c14bbaSAndroid Build Coastguard Worker
bpf_map__unpin(struct bpf_map * map,const char * path)8665*f7c14bbaSAndroid Build Coastguard Worker int bpf_map__unpin(struct bpf_map *map, const char *path)
8666*f7c14bbaSAndroid Build Coastguard Worker {
8667*f7c14bbaSAndroid Build Coastguard Worker int err;
8668*f7c14bbaSAndroid Build Coastguard Worker
8669*f7c14bbaSAndroid Build Coastguard Worker if (map == NULL) {
8670*f7c14bbaSAndroid Build Coastguard Worker pr_warn("invalid map pointer\n");
8671*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-EINVAL);
8672*f7c14bbaSAndroid Build Coastguard Worker }
8673*f7c14bbaSAndroid Build Coastguard Worker
8674*f7c14bbaSAndroid Build Coastguard Worker if (map->pin_path) {
8675*f7c14bbaSAndroid Build Coastguard Worker if (path && strcmp(path, map->pin_path)) {
8676*f7c14bbaSAndroid Build Coastguard Worker pr_warn("map '%s' already has pin path '%s' different from '%s'\n",
8677*f7c14bbaSAndroid Build Coastguard Worker bpf_map__name(map), map->pin_path, path);
8678*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-EINVAL);
8679*f7c14bbaSAndroid Build Coastguard Worker }
8680*f7c14bbaSAndroid Build Coastguard Worker path = map->pin_path;
8681*f7c14bbaSAndroid Build Coastguard Worker } else if (!path) {
8682*f7c14bbaSAndroid Build Coastguard Worker pr_warn("no path to unpin map '%s' from\n",
8683*f7c14bbaSAndroid Build Coastguard Worker bpf_map__name(map));
8684*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-EINVAL);
8685*f7c14bbaSAndroid Build Coastguard Worker }
8686*f7c14bbaSAndroid Build Coastguard Worker
8687*f7c14bbaSAndroid Build Coastguard Worker err = check_path(path);
8688*f7c14bbaSAndroid Build Coastguard Worker if (err)
8689*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(err);
8690*f7c14bbaSAndroid Build Coastguard Worker
8691*f7c14bbaSAndroid Build Coastguard Worker err = unlink(path);
8692*f7c14bbaSAndroid Build Coastguard Worker if (err != 0)
8693*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-errno);
8694*f7c14bbaSAndroid Build Coastguard Worker
8695*f7c14bbaSAndroid Build Coastguard Worker map->pinned = false;
8696*f7c14bbaSAndroid Build Coastguard Worker pr_debug("unpinned map '%s' from '%s'\n", bpf_map__name(map), path);
8697*f7c14bbaSAndroid Build Coastguard Worker
8698*f7c14bbaSAndroid Build Coastguard Worker return 0;
8699*f7c14bbaSAndroid Build Coastguard Worker }
8700*f7c14bbaSAndroid Build Coastguard Worker
bpf_map__set_pin_path(struct bpf_map * map,const char * path)8701*f7c14bbaSAndroid Build Coastguard Worker int bpf_map__set_pin_path(struct bpf_map *map, const char *path)
8702*f7c14bbaSAndroid Build Coastguard Worker {
8703*f7c14bbaSAndroid Build Coastguard Worker char *new = NULL;
8704*f7c14bbaSAndroid Build Coastguard Worker
8705*f7c14bbaSAndroid Build Coastguard Worker if (path) {
8706*f7c14bbaSAndroid Build Coastguard Worker new = strdup(path);
8707*f7c14bbaSAndroid Build Coastguard Worker if (!new)
8708*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-errno);
8709*f7c14bbaSAndroid Build Coastguard Worker }
8710*f7c14bbaSAndroid Build Coastguard Worker
8711*f7c14bbaSAndroid Build Coastguard Worker free(map->pin_path);
8712*f7c14bbaSAndroid Build Coastguard Worker map->pin_path = new;
8713*f7c14bbaSAndroid Build Coastguard Worker return 0;
8714*f7c14bbaSAndroid Build Coastguard Worker }
8715*f7c14bbaSAndroid Build Coastguard Worker
8716*f7c14bbaSAndroid Build Coastguard Worker __alias(bpf_map__pin_path)
8717*f7c14bbaSAndroid Build Coastguard Worker const char *bpf_map__get_pin_path(const struct bpf_map *map);
8718*f7c14bbaSAndroid Build Coastguard Worker
bpf_map__pin_path(const struct bpf_map * map)8719*f7c14bbaSAndroid Build Coastguard Worker const char *bpf_map__pin_path(const struct bpf_map *map)
8720*f7c14bbaSAndroid Build Coastguard Worker {
8721*f7c14bbaSAndroid Build Coastguard Worker return map->pin_path;
8722*f7c14bbaSAndroid Build Coastguard Worker }
8723*f7c14bbaSAndroid Build Coastguard Worker
bpf_map__is_pinned(const struct bpf_map * map)8724*f7c14bbaSAndroid Build Coastguard Worker bool bpf_map__is_pinned(const struct bpf_map *map)
8725*f7c14bbaSAndroid Build Coastguard Worker {
8726*f7c14bbaSAndroid Build Coastguard Worker return map->pinned;
8727*f7c14bbaSAndroid Build Coastguard Worker }
8728*f7c14bbaSAndroid Build Coastguard Worker
sanitize_pin_path(char * s)8729*f7c14bbaSAndroid Build Coastguard Worker static void sanitize_pin_path(char *s)
8730*f7c14bbaSAndroid Build Coastguard Worker {
8731*f7c14bbaSAndroid Build Coastguard Worker /* bpffs disallows periods in path names */
8732*f7c14bbaSAndroid Build Coastguard Worker while (*s) {
8733*f7c14bbaSAndroid Build Coastguard Worker if (*s == '.')
8734*f7c14bbaSAndroid Build Coastguard Worker *s = '_';
8735*f7c14bbaSAndroid Build Coastguard Worker s++;
8736*f7c14bbaSAndroid Build Coastguard Worker }
8737*f7c14bbaSAndroid Build Coastguard Worker }
8738*f7c14bbaSAndroid Build Coastguard Worker
bpf_object__pin_maps(struct bpf_object * obj,const char * path)8739*f7c14bbaSAndroid Build Coastguard Worker int bpf_object__pin_maps(struct bpf_object *obj, const char *path)
8740*f7c14bbaSAndroid Build Coastguard Worker {
8741*f7c14bbaSAndroid Build Coastguard Worker struct bpf_map *map;
8742*f7c14bbaSAndroid Build Coastguard Worker int err;
8743*f7c14bbaSAndroid Build Coastguard Worker
8744*f7c14bbaSAndroid Build Coastguard Worker if (!obj)
8745*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-ENOENT);
8746*f7c14bbaSAndroid Build Coastguard Worker
8747*f7c14bbaSAndroid Build Coastguard Worker if (!obj->loaded) {
8748*f7c14bbaSAndroid Build Coastguard Worker pr_warn("object not yet loaded; load it first\n");
8749*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-ENOENT);
8750*f7c14bbaSAndroid Build Coastguard Worker }
8751*f7c14bbaSAndroid Build Coastguard Worker
8752*f7c14bbaSAndroid Build Coastguard Worker bpf_object__for_each_map(map, obj) {
8753*f7c14bbaSAndroid Build Coastguard Worker char *pin_path = NULL;
8754*f7c14bbaSAndroid Build Coastguard Worker char buf[PATH_MAX];
8755*f7c14bbaSAndroid Build Coastguard Worker
8756*f7c14bbaSAndroid Build Coastguard Worker if (!map->autocreate)
8757*f7c14bbaSAndroid Build Coastguard Worker continue;
8758*f7c14bbaSAndroid Build Coastguard Worker
8759*f7c14bbaSAndroid Build Coastguard Worker if (path) {
8760*f7c14bbaSAndroid Build Coastguard Worker err = pathname_concat(buf, sizeof(buf), path, bpf_map__name(map));
8761*f7c14bbaSAndroid Build Coastguard Worker if (err)
8762*f7c14bbaSAndroid Build Coastguard Worker goto err_unpin_maps;
8763*f7c14bbaSAndroid Build Coastguard Worker sanitize_pin_path(buf);
8764*f7c14bbaSAndroid Build Coastguard Worker pin_path = buf;
8765*f7c14bbaSAndroid Build Coastguard Worker } else if (!map->pin_path) {
8766*f7c14bbaSAndroid Build Coastguard Worker continue;
8767*f7c14bbaSAndroid Build Coastguard Worker }
8768*f7c14bbaSAndroid Build Coastguard Worker
8769*f7c14bbaSAndroid Build Coastguard Worker err = bpf_map__pin(map, pin_path);
8770*f7c14bbaSAndroid Build Coastguard Worker if (err)
8771*f7c14bbaSAndroid Build Coastguard Worker goto err_unpin_maps;
8772*f7c14bbaSAndroid Build Coastguard Worker }
8773*f7c14bbaSAndroid Build Coastguard Worker
8774*f7c14bbaSAndroid Build Coastguard Worker return 0;
8775*f7c14bbaSAndroid Build Coastguard Worker
8776*f7c14bbaSAndroid Build Coastguard Worker err_unpin_maps:
8777*f7c14bbaSAndroid Build Coastguard Worker while ((map = bpf_object__prev_map(obj, map))) {
8778*f7c14bbaSAndroid Build Coastguard Worker if (!map->pin_path)
8779*f7c14bbaSAndroid Build Coastguard Worker continue;
8780*f7c14bbaSAndroid Build Coastguard Worker
8781*f7c14bbaSAndroid Build Coastguard Worker bpf_map__unpin(map, NULL);
8782*f7c14bbaSAndroid Build Coastguard Worker }
8783*f7c14bbaSAndroid Build Coastguard Worker
8784*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(err);
8785*f7c14bbaSAndroid Build Coastguard Worker }
8786*f7c14bbaSAndroid Build Coastguard Worker
bpf_object__unpin_maps(struct bpf_object * obj,const char * path)8787*f7c14bbaSAndroid Build Coastguard Worker int bpf_object__unpin_maps(struct bpf_object *obj, const char *path)
8788*f7c14bbaSAndroid Build Coastguard Worker {
8789*f7c14bbaSAndroid Build Coastguard Worker struct bpf_map *map;
8790*f7c14bbaSAndroid Build Coastguard Worker int err;
8791*f7c14bbaSAndroid Build Coastguard Worker
8792*f7c14bbaSAndroid Build Coastguard Worker if (!obj)
8793*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-ENOENT);
8794*f7c14bbaSAndroid Build Coastguard Worker
8795*f7c14bbaSAndroid Build Coastguard Worker bpf_object__for_each_map(map, obj) {
8796*f7c14bbaSAndroid Build Coastguard Worker char *pin_path = NULL;
8797*f7c14bbaSAndroid Build Coastguard Worker char buf[PATH_MAX];
8798*f7c14bbaSAndroid Build Coastguard Worker
8799*f7c14bbaSAndroid Build Coastguard Worker if (path) {
8800*f7c14bbaSAndroid Build Coastguard Worker err = pathname_concat(buf, sizeof(buf), path, bpf_map__name(map));
8801*f7c14bbaSAndroid Build Coastguard Worker if (err)
8802*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(err);
8803*f7c14bbaSAndroid Build Coastguard Worker sanitize_pin_path(buf);
8804*f7c14bbaSAndroid Build Coastguard Worker pin_path = buf;
8805*f7c14bbaSAndroid Build Coastguard Worker } else if (!map->pin_path) {
8806*f7c14bbaSAndroid Build Coastguard Worker continue;
8807*f7c14bbaSAndroid Build Coastguard Worker }
8808*f7c14bbaSAndroid Build Coastguard Worker
8809*f7c14bbaSAndroid Build Coastguard Worker err = bpf_map__unpin(map, pin_path);
8810*f7c14bbaSAndroid Build Coastguard Worker if (err)
8811*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(err);
8812*f7c14bbaSAndroid Build Coastguard Worker }
8813*f7c14bbaSAndroid Build Coastguard Worker
8814*f7c14bbaSAndroid Build Coastguard Worker return 0;
8815*f7c14bbaSAndroid Build Coastguard Worker }
8816*f7c14bbaSAndroid Build Coastguard Worker
bpf_object__pin_programs(struct bpf_object * obj,const char * path)8817*f7c14bbaSAndroid Build Coastguard Worker int bpf_object__pin_programs(struct bpf_object *obj, const char *path)
8818*f7c14bbaSAndroid Build Coastguard Worker {
8819*f7c14bbaSAndroid Build Coastguard Worker struct bpf_program *prog;
8820*f7c14bbaSAndroid Build Coastguard Worker char buf[PATH_MAX];
8821*f7c14bbaSAndroid Build Coastguard Worker int err;
8822*f7c14bbaSAndroid Build Coastguard Worker
8823*f7c14bbaSAndroid Build Coastguard Worker if (!obj)
8824*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-ENOENT);
8825*f7c14bbaSAndroid Build Coastguard Worker
8826*f7c14bbaSAndroid Build Coastguard Worker if (!obj->loaded) {
8827*f7c14bbaSAndroid Build Coastguard Worker pr_warn("object not yet loaded; load it first\n");
8828*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-ENOENT);
8829*f7c14bbaSAndroid Build Coastguard Worker }
8830*f7c14bbaSAndroid Build Coastguard Worker
8831*f7c14bbaSAndroid Build Coastguard Worker bpf_object__for_each_program(prog, obj) {
8832*f7c14bbaSAndroid Build Coastguard Worker err = pathname_concat(buf, sizeof(buf), path, prog->name);
8833*f7c14bbaSAndroid Build Coastguard Worker if (err)
8834*f7c14bbaSAndroid Build Coastguard Worker goto err_unpin_programs;
8835*f7c14bbaSAndroid Build Coastguard Worker
8836*f7c14bbaSAndroid Build Coastguard Worker err = bpf_program__pin(prog, buf);
8837*f7c14bbaSAndroid Build Coastguard Worker if (err)
8838*f7c14bbaSAndroid Build Coastguard Worker goto err_unpin_programs;
8839*f7c14bbaSAndroid Build Coastguard Worker }
8840*f7c14bbaSAndroid Build Coastguard Worker
8841*f7c14bbaSAndroid Build Coastguard Worker return 0;
8842*f7c14bbaSAndroid Build Coastguard Worker
8843*f7c14bbaSAndroid Build Coastguard Worker err_unpin_programs:
8844*f7c14bbaSAndroid Build Coastguard Worker while ((prog = bpf_object__prev_program(obj, prog))) {
8845*f7c14bbaSAndroid Build Coastguard Worker if (pathname_concat(buf, sizeof(buf), path, prog->name))
8846*f7c14bbaSAndroid Build Coastguard Worker continue;
8847*f7c14bbaSAndroid Build Coastguard Worker
8848*f7c14bbaSAndroid Build Coastguard Worker bpf_program__unpin(prog, buf);
8849*f7c14bbaSAndroid Build Coastguard Worker }
8850*f7c14bbaSAndroid Build Coastguard Worker
8851*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(err);
8852*f7c14bbaSAndroid Build Coastguard Worker }
8853*f7c14bbaSAndroid Build Coastguard Worker
bpf_object__unpin_programs(struct bpf_object * obj,const char * path)8854*f7c14bbaSAndroid Build Coastguard Worker int bpf_object__unpin_programs(struct bpf_object *obj, const char *path)
8855*f7c14bbaSAndroid Build Coastguard Worker {
8856*f7c14bbaSAndroid Build Coastguard Worker struct bpf_program *prog;
8857*f7c14bbaSAndroid Build Coastguard Worker int err;
8858*f7c14bbaSAndroid Build Coastguard Worker
8859*f7c14bbaSAndroid Build Coastguard Worker if (!obj)
8860*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-ENOENT);
8861*f7c14bbaSAndroid Build Coastguard Worker
8862*f7c14bbaSAndroid Build Coastguard Worker bpf_object__for_each_program(prog, obj) {
8863*f7c14bbaSAndroid Build Coastguard Worker char buf[PATH_MAX];
8864*f7c14bbaSAndroid Build Coastguard Worker
8865*f7c14bbaSAndroid Build Coastguard Worker err = pathname_concat(buf, sizeof(buf), path, prog->name);
8866*f7c14bbaSAndroid Build Coastguard Worker if (err)
8867*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(err);
8868*f7c14bbaSAndroid Build Coastguard Worker
8869*f7c14bbaSAndroid Build Coastguard Worker err = bpf_program__unpin(prog, buf);
8870*f7c14bbaSAndroid Build Coastguard Worker if (err)
8871*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(err);
8872*f7c14bbaSAndroid Build Coastguard Worker }
8873*f7c14bbaSAndroid Build Coastguard Worker
8874*f7c14bbaSAndroid Build Coastguard Worker return 0;
8875*f7c14bbaSAndroid Build Coastguard Worker }
8876*f7c14bbaSAndroid Build Coastguard Worker
bpf_object__pin(struct bpf_object * obj,const char * path)8877*f7c14bbaSAndroid Build Coastguard Worker int bpf_object__pin(struct bpf_object *obj, const char *path)
8878*f7c14bbaSAndroid Build Coastguard Worker {
8879*f7c14bbaSAndroid Build Coastguard Worker int err;
8880*f7c14bbaSAndroid Build Coastguard Worker
8881*f7c14bbaSAndroid Build Coastguard Worker err = bpf_object__pin_maps(obj, path);
8882*f7c14bbaSAndroid Build Coastguard Worker if (err)
8883*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(err);
8884*f7c14bbaSAndroid Build Coastguard Worker
8885*f7c14bbaSAndroid Build Coastguard Worker err = bpf_object__pin_programs(obj, path);
8886*f7c14bbaSAndroid Build Coastguard Worker if (err) {
8887*f7c14bbaSAndroid Build Coastguard Worker bpf_object__unpin_maps(obj, path);
8888*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(err);
8889*f7c14bbaSAndroid Build Coastguard Worker }
8890*f7c14bbaSAndroid Build Coastguard Worker
8891*f7c14bbaSAndroid Build Coastguard Worker return 0;
8892*f7c14bbaSAndroid Build Coastguard Worker }
8893*f7c14bbaSAndroid Build Coastguard Worker
bpf_object__unpin(struct bpf_object * obj,const char * path)8894*f7c14bbaSAndroid Build Coastguard Worker int bpf_object__unpin(struct bpf_object *obj, const char *path)
8895*f7c14bbaSAndroid Build Coastguard Worker {
8896*f7c14bbaSAndroid Build Coastguard Worker int err;
8897*f7c14bbaSAndroid Build Coastguard Worker
8898*f7c14bbaSAndroid Build Coastguard Worker err = bpf_object__unpin_programs(obj, path);
8899*f7c14bbaSAndroid Build Coastguard Worker if (err)
8900*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(err);
8901*f7c14bbaSAndroid Build Coastguard Worker
8902*f7c14bbaSAndroid Build Coastguard Worker err = bpf_object__unpin_maps(obj, path);
8903*f7c14bbaSAndroid Build Coastguard Worker if (err)
8904*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(err);
8905*f7c14bbaSAndroid Build Coastguard Worker
8906*f7c14bbaSAndroid Build Coastguard Worker return 0;
8907*f7c14bbaSAndroid Build Coastguard Worker }
8908*f7c14bbaSAndroid Build Coastguard Worker
bpf_map__destroy(struct bpf_map * map)8909*f7c14bbaSAndroid Build Coastguard Worker static void bpf_map__destroy(struct bpf_map *map)
8910*f7c14bbaSAndroid Build Coastguard Worker {
8911*f7c14bbaSAndroid Build Coastguard Worker if (map->inner_map) {
8912*f7c14bbaSAndroid Build Coastguard Worker bpf_map__destroy(map->inner_map);
8913*f7c14bbaSAndroid Build Coastguard Worker zfree(&map->inner_map);
8914*f7c14bbaSAndroid Build Coastguard Worker }
8915*f7c14bbaSAndroid Build Coastguard Worker
8916*f7c14bbaSAndroid Build Coastguard Worker zfree(&map->init_slots);
8917*f7c14bbaSAndroid Build Coastguard Worker map->init_slots_sz = 0;
8918*f7c14bbaSAndroid Build Coastguard Worker
8919*f7c14bbaSAndroid Build Coastguard Worker if (map->mmaped && map->mmaped != map->obj->arena_data)
8920*f7c14bbaSAndroid Build Coastguard Worker munmap(map->mmaped, bpf_map_mmap_sz(map));
8921*f7c14bbaSAndroid Build Coastguard Worker map->mmaped = NULL;
8922*f7c14bbaSAndroid Build Coastguard Worker
8923*f7c14bbaSAndroid Build Coastguard Worker if (map->st_ops) {
8924*f7c14bbaSAndroid Build Coastguard Worker zfree(&map->st_ops->data);
8925*f7c14bbaSAndroid Build Coastguard Worker zfree(&map->st_ops->progs);
8926*f7c14bbaSAndroid Build Coastguard Worker zfree(&map->st_ops->kern_func_off);
8927*f7c14bbaSAndroid Build Coastguard Worker zfree(&map->st_ops);
8928*f7c14bbaSAndroid Build Coastguard Worker }
8929*f7c14bbaSAndroid Build Coastguard Worker
8930*f7c14bbaSAndroid Build Coastguard Worker zfree(&map->name);
8931*f7c14bbaSAndroid Build Coastguard Worker zfree(&map->real_name);
8932*f7c14bbaSAndroid Build Coastguard Worker zfree(&map->pin_path);
8933*f7c14bbaSAndroid Build Coastguard Worker
8934*f7c14bbaSAndroid Build Coastguard Worker if (map->fd >= 0)
8935*f7c14bbaSAndroid Build Coastguard Worker zclose(map->fd);
8936*f7c14bbaSAndroid Build Coastguard Worker }
8937*f7c14bbaSAndroid Build Coastguard Worker
bpf_object__close(struct bpf_object * obj)8938*f7c14bbaSAndroid Build Coastguard Worker void bpf_object__close(struct bpf_object *obj)
8939*f7c14bbaSAndroid Build Coastguard Worker {
8940*f7c14bbaSAndroid Build Coastguard Worker size_t i;
8941*f7c14bbaSAndroid Build Coastguard Worker
8942*f7c14bbaSAndroid Build Coastguard Worker if (IS_ERR_OR_NULL(obj))
8943*f7c14bbaSAndroid Build Coastguard Worker return;
8944*f7c14bbaSAndroid Build Coastguard Worker
8945*f7c14bbaSAndroid Build Coastguard Worker usdt_manager_free(obj->usdt_man);
8946*f7c14bbaSAndroid Build Coastguard Worker obj->usdt_man = NULL;
8947*f7c14bbaSAndroid Build Coastguard Worker
8948*f7c14bbaSAndroid Build Coastguard Worker bpf_gen__free(obj->gen_loader);
8949*f7c14bbaSAndroid Build Coastguard Worker bpf_object__elf_finish(obj);
8950*f7c14bbaSAndroid Build Coastguard Worker bpf_object_unload(obj);
8951*f7c14bbaSAndroid Build Coastguard Worker btf__free(obj->btf);
8952*f7c14bbaSAndroid Build Coastguard Worker btf__free(obj->btf_vmlinux);
8953*f7c14bbaSAndroid Build Coastguard Worker btf_ext__free(obj->btf_ext);
8954*f7c14bbaSAndroid Build Coastguard Worker
8955*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < obj->nr_maps; i++)
8956*f7c14bbaSAndroid Build Coastguard Worker bpf_map__destroy(&obj->maps[i]);
8957*f7c14bbaSAndroid Build Coastguard Worker
8958*f7c14bbaSAndroid Build Coastguard Worker zfree(&obj->btf_custom_path);
8959*f7c14bbaSAndroid Build Coastguard Worker zfree(&obj->kconfig);
8960*f7c14bbaSAndroid Build Coastguard Worker
8961*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < obj->nr_extern; i++)
8962*f7c14bbaSAndroid Build Coastguard Worker zfree(&obj->externs[i].essent_name);
8963*f7c14bbaSAndroid Build Coastguard Worker
8964*f7c14bbaSAndroid Build Coastguard Worker zfree(&obj->externs);
8965*f7c14bbaSAndroid Build Coastguard Worker obj->nr_extern = 0;
8966*f7c14bbaSAndroid Build Coastguard Worker
8967*f7c14bbaSAndroid Build Coastguard Worker zfree(&obj->maps);
8968*f7c14bbaSAndroid Build Coastguard Worker obj->nr_maps = 0;
8969*f7c14bbaSAndroid Build Coastguard Worker
8970*f7c14bbaSAndroid Build Coastguard Worker if (obj->programs && obj->nr_programs) {
8971*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < obj->nr_programs; i++)
8972*f7c14bbaSAndroid Build Coastguard Worker bpf_program__exit(&obj->programs[i]);
8973*f7c14bbaSAndroid Build Coastguard Worker }
8974*f7c14bbaSAndroid Build Coastguard Worker zfree(&obj->programs);
8975*f7c14bbaSAndroid Build Coastguard Worker
8976*f7c14bbaSAndroid Build Coastguard Worker zfree(&obj->feat_cache);
8977*f7c14bbaSAndroid Build Coastguard Worker zfree(&obj->token_path);
8978*f7c14bbaSAndroid Build Coastguard Worker if (obj->token_fd > 0)
8979*f7c14bbaSAndroid Build Coastguard Worker close(obj->token_fd);
8980*f7c14bbaSAndroid Build Coastguard Worker
8981*f7c14bbaSAndroid Build Coastguard Worker zfree(&obj->arena_data);
8982*f7c14bbaSAndroid Build Coastguard Worker
8983*f7c14bbaSAndroid Build Coastguard Worker free(obj);
8984*f7c14bbaSAndroid Build Coastguard Worker }
8985*f7c14bbaSAndroid Build Coastguard Worker
bpf_object__name(const struct bpf_object * obj)8986*f7c14bbaSAndroid Build Coastguard Worker const char *bpf_object__name(const struct bpf_object *obj)
8987*f7c14bbaSAndroid Build Coastguard Worker {
8988*f7c14bbaSAndroid Build Coastguard Worker return obj ? obj->name : libbpf_err_ptr(-EINVAL);
8989*f7c14bbaSAndroid Build Coastguard Worker }
8990*f7c14bbaSAndroid Build Coastguard Worker
bpf_object__kversion(const struct bpf_object * obj)8991*f7c14bbaSAndroid Build Coastguard Worker unsigned int bpf_object__kversion(const struct bpf_object *obj)
8992*f7c14bbaSAndroid Build Coastguard Worker {
8993*f7c14bbaSAndroid Build Coastguard Worker return obj ? obj->kern_version : 0;
8994*f7c14bbaSAndroid Build Coastguard Worker }
8995*f7c14bbaSAndroid Build Coastguard Worker
bpf_object__btf(const struct bpf_object * obj)8996*f7c14bbaSAndroid Build Coastguard Worker struct btf *bpf_object__btf(const struct bpf_object *obj)
8997*f7c14bbaSAndroid Build Coastguard Worker {
8998*f7c14bbaSAndroid Build Coastguard Worker return obj ? obj->btf : NULL;
8999*f7c14bbaSAndroid Build Coastguard Worker }
9000*f7c14bbaSAndroid Build Coastguard Worker
bpf_object__btf_fd(const struct bpf_object * obj)9001*f7c14bbaSAndroid Build Coastguard Worker int bpf_object__btf_fd(const struct bpf_object *obj)
9002*f7c14bbaSAndroid Build Coastguard Worker {
9003*f7c14bbaSAndroid Build Coastguard Worker return obj->btf ? btf__fd(obj->btf) : -1;
9004*f7c14bbaSAndroid Build Coastguard Worker }
9005*f7c14bbaSAndroid Build Coastguard Worker
bpf_object__set_kversion(struct bpf_object * obj,__u32 kern_version)9006*f7c14bbaSAndroid Build Coastguard Worker int bpf_object__set_kversion(struct bpf_object *obj, __u32 kern_version)
9007*f7c14bbaSAndroid Build Coastguard Worker {
9008*f7c14bbaSAndroid Build Coastguard Worker if (obj->loaded)
9009*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-EINVAL);
9010*f7c14bbaSAndroid Build Coastguard Worker
9011*f7c14bbaSAndroid Build Coastguard Worker obj->kern_version = kern_version;
9012*f7c14bbaSAndroid Build Coastguard Worker
9013*f7c14bbaSAndroid Build Coastguard Worker return 0;
9014*f7c14bbaSAndroid Build Coastguard Worker }
9015*f7c14bbaSAndroid Build Coastguard Worker
bpf_object__gen_loader(struct bpf_object * obj,struct gen_loader_opts * opts)9016*f7c14bbaSAndroid Build Coastguard Worker int bpf_object__gen_loader(struct bpf_object *obj, struct gen_loader_opts *opts)
9017*f7c14bbaSAndroid Build Coastguard Worker {
9018*f7c14bbaSAndroid Build Coastguard Worker struct bpf_gen *gen;
9019*f7c14bbaSAndroid Build Coastguard Worker
9020*f7c14bbaSAndroid Build Coastguard Worker if (!opts)
9021*f7c14bbaSAndroid Build Coastguard Worker return -EFAULT;
9022*f7c14bbaSAndroid Build Coastguard Worker if (!OPTS_VALID(opts, gen_loader_opts))
9023*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
9024*f7c14bbaSAndroid Build Coastguard Worker gen = calloc(sizeof(*gen), 1);
9025*f7c14bbaSAndroid Build Coastguard Worker if (!gen)
9026*f7c14bbaSAndroid Build Coastguard Worker return -ENOMEM;
9027*f7c14bbaSAndroid Build Coastguard Worker gen->opts = opts;
9028*f7c14bbaSAndroid Build Coastguard Worker obj->gen_loader = gen;
9029*f7c14bbaSAndroid Build Coastguard Worker return 0;
9030*f7c14bbaSAndroid Build Coastguard Worker }
9031*f7c14bbaSAndroid Build Coastguard Worker
9032*f7c14bbaSAndroid Build Coastguard Worker static struct bpf_program *
__bpf_program__iter(const struct bpf_program * p,const struct bpf_object * obj,bool forward)9033*f7c14bbaSAndroid Build Coastguard Worker __bpf_program__iter(const struct bpf_program *p, const struct bpf_object *obj,
9034*f7c14bbaSAndroid Build Coastguard Worker bool forward)
9035*f7c14bbaSAndroid Build Coastguard Worker {
9036*f7c14bbaSAndroid Build Coastguard Worker size_t nr_programs = obj->nr_programs;
9037*f7c14bbaSAndroid Build Coastguard Worker ssize_t idx;
9038*f7c14bbaSAndroid Build Coastguard Worker
9039*f7c14bbaSAndroid Build Coastguard Worker if (!nr_programs)
9040*f7c14bbaSAndroid Build Coastguard Worker return NULL;
9041*f7c14bbaSAndroid Build Coastguard Worker
9042*f7c14bbaSAndroid Build Coastguard Worker if (!p)
9043*f7c14bbaSAndroid Build Coastguard Worker /* Iter from the beginning */
9044*f7c14bbaSAndroid Build Coastguard Worker return forward ? &obj->programs[0] :
9045*f7c14bbaSAndroid Build Coastguard Worker &obj->programs[nr_programs - 1];
9046*f7c14bbaSAndroid Build Coastguard Worker
9047*f7c14bbaSAndroid Build Coastguard Worker if (p->obj != obj) {
9048*f7c14bbaSAndroid Build Coastguard Worker pr_warn("error: program handler doesn't match object\n");
9049*f7c14bbaSAndroid Build Coastguard Worker return errno = EINVAL, NULL;
9050*f7c14bbaSAndroid Build Coastguard Worker }
9051*f7c14bbaSAndroid Build Coastguard Worker
9052*f7c14bbaSAndroid Build Coastguard Worker idx = (p - obj->programs) + (forward ? 1 : -1);
9053*f7c14bbaSAndroid Build Coastguard Worker if (idx >= obj->nr_programs || idx < 0)
9054*f7c14bbaSAndroid Build Coastguard Worker return NULL;
9055*f7c14bbaSAndroid Build Coastguard Worker return &obj->programs[idx];
9056*f7c14bbaSAndroid Build Coastguard Worker }
9057*f7c14bbaSAndroid Build Coastguard Worker
9058*f7c14bbaSAndroid Build Coastguard Worker struct bpf_program *
bpf_object__next_program(const struct bpf_object * obj,struct bpf_program * prev)9059*f7c14bbaSAndroid Build Coastguard Worker bpf_object__next_program(const struct bpf_object *obj, struct bpf_program *prev)
9060*f7c14bbaSAndroid Build Coastguard Worker {
9061*f7c14bbaSAndroid Build Coastguard Worker struct bpf_program *prog = prev;
9062*f7c14bbaSAndroid Build Coastguard Worker
9063*f7c14bbaSAndroid Build Coastguard Worker do {
9064*f7c14bbaSAndroid Build Coastguard Worker prog = __bpf_program__iter(prog, obj, true);
9065*f7c14bbaSAndroid Build Coastguard Worker } while (prog && prog_is_subprog(obj, prog));
9066*f7c14bbaSAndroid Build Coastguard Worker
9067*f7c14bbaSAndroid Build Coastguard Worker return prog;
9068*f7c14bbaSAndroid Build Coastguard Worker }
9069*f7c14bbaSAndroid Build Coastguard Worker
9070*f7c14bbaSAndroid Build Coastguard Worker struct bpf_program *
bpf_object__prev_program(const struct bpf_object * obj,struct bpf_program * next)9071*f7c14bbaSAndroid Build Coastguard Worker bpf_object__prev_program(const struct bpf_object *obj, struct bpf_program *next)
9072*f7c14bbaSAndroid Build Coastguard Worker {
9073*f7c14bbaSAndroid Build Coastguard Worker struct bpf_program *prog = next;
9074*f7c14bbaSAndroid Build Coastguard Worker
9075*f7c14bbaSAndroid Build Coastguard Worker do {
9076*f7c14bbaSAndroid Build Coastguard Worker prog = __bpf_program__iter(prog, obj, false);
9077*f7c14bbaSAndroid Build Coastguard Worker } while (prog && prog_is_subprog(obj, prog));
9078*f7c14bbaSAndroid Build Coastguard Worker
9079*f7c14bbaSAndroid Build Coastguard Worker return prog;
9080*f7c14bbaSAndroid Build Coastguard Worker }
9081*f7c14bbaSAndroid Build Coastguard Worker
bpf_program__set_ifindex(struct bpf_program * prog,__u32 ifindex)9082*f7c14bbaSAndroid Build Coastguard Worker void bpf_program__set_ifindex(struct bpf_program *prog, __u32 ifindex)
9083*f7c14bbaSAndroid Build Coastguard Worker {
9084*f7c14bbaSAndroid Build Coastguard Worker prog->prog_ifindex = ifindex;
9085*f7c14bbaSAndroid Build Coastguard Worker }
9086*f7c14bbaSAndroid Build Coastguard Worker
bpf_program__name(const struct bpf_program * prog)9087*f7c14bbaSAndroid Build Coastguard Worker const char *bpf_program__name(const struct bpf_program *prog)
9088*f7c14bbaSAndroid Build Coastguard Worker {
9089*f7c14bbaSAndroid Build Coastguard Worker return prog->name;
9090*f7c14bbaSAndroid Build Coastguard Worker }
9091*f7c14bbaSAndroid Build Coastguard Worker
bpf_program__section_name(const struct bpf_program * prog)9092*f7c14bbaSAndroid Build Coastguard Worker const char *bpf_program__section_name(const struct bpf_program *prog)
9093*f7c14bbaSAndroid Build Coastguard Worker {
9094*f7c14bbaSAndroid Build Coastguard Worker return prog->sec_name;
9095*f7c14bbaSAndroid Build Coastguard Worker }
9096*f7c14bbaSAndroid Build Coastguard Worker
bpf_program__autoload(const struct bpf_program * prog)9097*f7c14bbaSAndroid Build Coastguard Worker bool bpf_program__autoload(const struct bpf_program *prog)
9098*f7c14bbaSAndroid Build Coastguard Worker {
9099*f7c14bbaSAndroid Build Coastguard Worker return prog->autoload;
9100*f7c14bbaSAndroid Build Coastguard Worker }
9101*f7c14bbaSAndroid Build Coastguard Worker
bpf_program__set_autoload(struct bpf_program * prog,bool autoload)9102*f7c14bbaSAndroid Build Coastguard Worker int bpf_program__set_autoload(struct bpf_program *prog, bool autoload)
9103*f7c14bbaSAndroid Build Coastguard Worker {
9104*f7c14bbaSAndroid Build Coastguard Worker if (prog->obj->loaded)
9105*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-EINVAL);
9106*f7c14bbaSAndroid Build Coastguard Worker
9107*f7c14bbaSAndroid Build Coastguard Worker prog->autoload = autoload;
9108*f7c14bbaSAndroid Build Coastguard Worker return 0;
9109*f7c14bbaSAndroid Build Coastguard Worker }
9110*f7c14bbaSAndroid Build Coastguard Worker
bpf_program__autoattach(const struct bpf_program * prog)9111*f7c14bbaSAndroid Build Coastguard Worker bool bpf_program__autoattach(const struct bpf_program *prog)
9112*f7c14bbaSAndroid Build Coastguard Worker {
9113*f7c14bbaSAndroid Build Coastguard Worker return prog->autoattach;
9114*f7c14bbaSAndroid Build Coastguard Worker }
9115*f7c14bbaSAndroid Build Coastguard Worker
bpf_program__set_autoattach(struct bpf_program * prog,bool autoattach)9116*f7c14bbaSAndroid Build Coastguard Worker void bpf_program__set_autoattach(struct bpf_program *prog, bool autoattach)
9117*f7c14bbaSAndroid Build Coastguard Worker {
9118*f7c14bbaSAndroid Build Coastguard Worker prog->autoattach = autoattach;
9119*f7c14bbaSAndroid Build Coastguard Worker }
9120*f7c14bbaSAndroid Build Coastguard Worker
bpf_program__insns(const struct bpf_program * prog)9121*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_insn *bpf_program__insns(const struct bpf_program *prog)
9122*f7c14bbaSAndroid Build Coastguard Worker {
9123*f7c14bbaSAndroid Build Coastguard Worker return prog->insns;
9124*f7c14bbaSAndroid Build Coastguard Worker }
9125*f7c14bbaSAndroid Build Coastguard Worker
bpf_program__insn_cnt(const struct bpf_program * prog)9126*f7c14bbaSAndroid Build Coastguard Worker size_t bpf_program__insn_cnt(const struct bpf_program *prog)
9127*f7c14bbaSAndroid Build Coastguard Worker {
9128*f7c14bbaSAndroid Build Coastguard Worker return prog->insns_cnt;
9129*f7c14bbaSAndroid Build Coastguard Worker }
9130*f7c14bbaSAndroid Build Coastguard Worker
bpf_program__set_insns(struct bpf_program * prog,struct bpf_insn * new_insns,size_t new_insn_cnt)9131*f7c14bbaSAndroid Build Coastguard Worker int bpf_program__set_insns(struct bpf_program *prog,
9132*f7c14bbaSAndroid Build Coastguard Worker struct bpf_insn *new_insns, size_t new_insn_cnt)
9133*f7c14bbaSAndroid Build Coastguard Worker {
9134*f7c14bbaSAndroid Build Coastguard Worker struct bpf_insn *insns;
9135*f7c14bbaSAndroid Build Coastguard Worker
9136*f7c14bbaSAndroid Build Coastguard Worker if (prog->obj->loaded)
9137*f7c14bbaSAndroid Build Coastguard Worker return -EBUSY;
9138*f7c14bbaSAndroid Build Coastguard Worker
9139*f7c14bbaSAndroid Build Coastguard Worker insns = libbpf_reallocarray(prog->insns, new_insn_cnt, sizeof(*insns));
9140*f7c14bbaSAndroid Build Coastguard Worker /* NULL is a valid return from reallocarray if the new count is zero */
9141*f7c14bbaSAndroid Build Coastguard Worker if (!insns && new_insn_cnt) {
9142*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': failed to realloc prog code\n", prog->name);
9143*f7c14bbaSAndroid Build Coastguard Worker return -ENOMEM;
9144*f7c14bbaSAndroid Build Coastguard Worker }
9145*f7c14bbaSAndroid Build Coastguard Worker memcpy(insns, new_insns, new_insn_cnt * sizeof(*insns));
9146*f7c14bbaSAndroid Build Coastguard Worker
9147*f7c14bbaSAndroid Build Coastguard Worker prog->insns = insns;
9148*f7c14bbaSAndroid Build Coastguard Worker prog->insns_cnt = new_insn_cnt;
9149*f7c14bbaSAndroid Build Coastguard Worker return 0;
9150*f7c14bbaSAndroid Build Coastguard Worker }
9151*f7c14bbaSAndroid Build Coastguard Worker
bpf_program__fd(const struct bpf_program * prog)9152*f7c14bbaSAndroid Build Coastguard Worker int bpf_program__fd(const struct bpf_program *prog)
9153*f7c14bbaSAndroid Build Coastguard Worker {
9154*f7c14bbaSAndroid Build Coastguard Worker if (!prog)
9155*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-EINVAL);
9156*f7c14bbaSAndroid Build Coastguard Worker
9157*f7c14bbaSAndroid Build Coastguard Worker if (prog->fd < 0)
9158*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-ENOENT);
9159*f7c14bbaSAndroid Build Coastguard Worker
9160*f7c14bbaSAndroid Build Coastguard Worker return prog->fd;
9161*f7c14bbaSAndroid Build Coastguard Worker }
9162*f7c14bbaSAndroid Build Coastguard Worker
9163*f7c14bbaSAndroid Build Coastguard Worker __alias(bpf_program__type)
9164*f7c14bbaSAndroid Build Coastguard Worker enum bpf_prog_type bpf_program__get_type(const struct bpf_program *prog);
9165*f7c14bbaSAndroid Build Coastguard Worker
bpf_program__type(const struct bpf_program * prog)9166*f7c14bbaSAndroid Build Coastguard Worker enum bpf_prog_type bpf_program__type(const struct bpf_program *prog)
9167*f7c14bbaSAndroid Build Coastguard Worker {
9168*f7c14bbaSAndroid Build Coastguard Worker return prog->type;
9169*f7c14bbaSAndroid Build Coastguard Worker }
9170*f7c14bbaSAndroid Build Coastguard Worker
9171*f7c14bbaSAndroid Build Coastguard Worker static size_t custom_sec_def_cnt;
9172*f7c14bbaSAndroid Build Coastguard Worker static struct bpf_sec_def *custom_sec_defs;
9173*f7c14bbaSAndroid Build Coastguard Worker static struct bpf_sec_def custom_fallback_def;
9174*f7c14bbaSAndroid Build Coastguard Worker static bool has_custom_fallback_def;
9175*f7c14bbaSAndroid Build Coastguard Worker static int last_custom_sec_def_handler_id;
9176*f7c14bbaSAndroid Build Coastguard Worker
bpf_program__set_type(struct bpf_program * prog,enum bpf_prog_type type)9177*f7c14bbaSAndroid Build Coastguard Worker int bpf_program__set_type(struct bpf_program *prog, enum bpf_prog_type type)
9178*f7c14bbaSAndroid Build Coastguard Worker {
9179*f7c14bbaSAndroid Build Coastguard Worker if (prog->obj->loaded)
9180*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-EBUSY);
9181*f7c14bbaSAndroid Build Coastguard Worker
9182*f7c14bbaSAndroid Build Coastguard Worker /* if type is not changed, do nothing */
9183*f7c14bbaSAndroid Build Coastguard Worker if (prog->type == type)
9184*f7c14bbaSAndroid Build Coastguard Worker return 0;
9185*f7c14bbaSAndroid Build Coastguard Worker
9186*f7c14bbaSAndroid Build Coastguard Worker prog->type = type;
9187*f7c14bbaSAndroid Build Coastguard Worker
9188*f7c14bbaSAndroid Build Coastguard Worker /* If a program type was changed, we need to reset associated SEC()
9189*f7c14bbaSAndroid Build Coastguard Worker * handler, as it will be invalid now. The only exception is a generic
9190*f7c14bbaSAndroid Build Coastguard Worker * fallback handler, which by definition is program type-agnostic and
9191*f7c14bbaSAndroid Build Coastguard Worker * is a catch-all custom handler, optionally set by the application,
9192*f7c14bbaSAndroid Build Coastguard Worker * so should be able to handle any type of BPF program.
9193*f7c14bbaSAndroid Build Coastguard Worker */
9194*f7c14bbaSAndroid Build Coastguard Worker if (prog->sec_def != &custom_fallback_def)
9195*f7c14bbaSAndroid Build Coastguard Worker prog->sec_def = NULL;
9196*f7c14bbaSAndroid Build Coastguard Worker return 0;
9197*f7c14bbaSAndroid Build Coastguard Worker }
9198*f7c14bbaSAndroid Build Coastguard Worker
9199*f7c14bbaSAndroid Build Coastguard Worker __alias(bpf_program__expected_attach_type)
9200*f7c14bbaSAndroid Build Coastguard Worker enum bpf_attach_type bpf_program__get_expected_attach_type(const struct bpf_program *prog);
9201*f7c14bbaSAndroid Build Coastguard Worker
bpf_program__expected_attach_type(const struct bpf_program * prog)9202*f7c14bbaSAndroid Build Coastguard Worker enum bpf_attach_type bpf_program__expected_attach_type(const struct bpf_program *prog)
9203*f7c14bbaSAndroid Build Coastguard Worker {
9204*f7c14bbaSAndroid Build Coastguard Worker return prog->expected_attach_type;
9205*f7c14bbaSAndroid Build Coastguard Worker }
9206*f7c14bbaSAndroid Build Coastguard Worker
bpf_program__set_expected_attach_type(struct bpf_program * prog,enum bpf_attach_type type)9207*f7c14bbaSAndroid Build Coastguard Worker int bpf_program__set_expected_attach_type(struct bpf_program *prog,
9208*f7c14bbaSAndroid Build Coastguard Worker enum bpf_attach_type type)
9209*f7c14bbaSAndroid Build Coastguard Worker {
9210*f7c14bbaSAndroid Build Coastguard Worker if (prog->obj->loaded)
9211*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-EBUSY);
9212*f7c14bbaSAndroid Build Coastguard Worker
9213*f7c14bbaSAndroid Build Coastguard Worker prog->expected_attach_type = type;
9214*f7c14bbaSAndroid Build Coastguard Worker return 0;
9215*f7c14bbaSAndroid Build Coastguard Worker }
9216*f7c14bbaSAndroid Build Coastguard Worker
bpf_program__flags(const struct bpf_program * prog)9217*f7c14bbaSAndroid Build Coastguard Worker __u32 bpf_program__flags(const struct bpf_program *prog)
9218*f7c14bbaSAndroid Build Coastguard Worker {
9219*f7c14bbaSAndroid Build Coastguard Worker return prog->prog_flags;
9220*f7c14bbaSAndroid Build Coastguard Worker }
9221*f7c14bbaSAndroid Build Coastguard Worker
bpf_program__set_flags(struct bpf_program * prog,__u32 flags)9222*f7c14bbaSAndroid Build Coastguard Worker int bpf_program__set_flags(struct bpf_program *prog, __u32 flags)
9223*f7c14bbaSAndroid Build Coastguard Worker {
9224*f7c14bbaSAndroid Build Coastguard Worker if (prog->obj->loaded)
9225*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-EBUSY);
9226*f7c14bbaSAndroid Build Coastguard Worker
9227*f7c14bbaSAndroid Build Coastguard Worker prog->prog_flags = flags;
9228*f7c14bbaSAndroid Build Coastguard Worker return 0;
9229*f7c14bbaSAndroid Build Coastguard Worker }
9230*f7c14bbaSAndroid Build Coastguard Worker
bpf_program__log_level(const struct bpf_program * prog)9231*f7c14bbaSAndroid Build Coastguard Worker __u32 bpf_program__log_level(const struct bpf_program *prog)
9232*f7c14bbaSAndroid Build Coastguard Worker {
9233*f7c14bbaSAndroid Build Coastguard Worker return prog->log_level;
9234*f7c14bbaSAndroid Build Coastguard Worker }
9235*f7c14bbaSAndroid Build Coastguard Worker
bpf_program__set_log_level(struct bpf_program * prog,__u32 log_level)9236*f7c14bbaSAndroid Build Coastguard Worker int bpf_program__set_log_level(struct bpf_program *prog, __u32 log_level)
9237*f7c14bbaSAndroid Build Coastguard Worker {
9238*f7c14bbaSAndroid Build Coastguard Worker if (prog->obj->loaded)
9239*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-EBUSY);
9240*f7c14bbaSAndroid Build Coastguard Worker
9241*f7c14bbaSAndroid Build Coastguard Worker prog->log_level = log_level;
9242*f7c14bbaSAndroid Build Coastguard Worker return 0;
9243*f7c14bbaSAndroid Build Coastguard Worker }
9244*f7c14bbaSAndroid Build Coastguard Worker
bpf_program__log_buf(const struct bpf_program * prog,size_t * log_size)9245*f7c14bbaSAndroid Build Coastguard Worker const char *bpf_program__log_buf(const struct bpf_program *prog, size_t *log_size)
9246*f7c14bbaSAndroid Build Coastguard Worker {
9247*f7c14bbaSAndroid Build Coastguard Worker *log_size = prog->log_size;
9248*f7c14bbaSAndroid Build Coastguard Worker return prog->log_buf;
9249*f7c14bbaSAndroid Build Coastguard Worker }
9250*f7c14bbaSAndroid Build Coastguard Worker
bpf_program__set_log_buf(struct bpf_program * prog,char * log_buf,size_t log_size)9251*f7c14bbaSAndroid Build Coastguard Worker int bpf_program__set_log_buf(struct bpf_program *prog, char *log_buf, size_t log_size)
9252*f7c14bbaSAndroid Build Coastguard Worker {
9253*f7c14bbaSAndroid Build Coastguard Worker if (log_size && !log_buf)
9254*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
9255*f7c14bbaSAndroid Build Coastguard Worker if (prog->log_size > UINT_MAX)
9256*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
9257*f7c14bbaSAndroid Build Coastguard Worker if (prog->obj->loaded)
9258*f7c14bbaSAndroid Build Coastguard Worker return -EBUSY;
9259*f7c14bbaSAndroid Build Coastguard Worker
9260*f7c14bbaSAndroid Build Coastguard Worker prog->log_buf = log_buf;
9261*f7c14bbaSAndroid Build Coastguard Worker prog->log_size = log_size;
9262*f7c14bbaSAndroid Build Coastguard Worker return 0;
9263*f7c14bbaSAndroid Build Coastguard Worker }
9264*f7c14bbaSAndroid Build Coastguard Worker
9265*f7c14bbaSAndroid Build Coastguard Worker #define SEC_DEF(sec_pfx, ptype, atype, flags, ...) { \
9266*f7c14bbaSAndroid Build Coastguard Worker .sec = (char *)sec_pfx, \
9267*f7c14bbaSAndroid Build Coastguard Worker .prog_type = BPF_PROG_TYPE_##ptype, \
9268*f7c14bbaSAndroid Build Coastguard Worker .expected_attach_type = atype, \
9269*f7c14bbaSAndroid Build Coastguard Worker .cookie = (long)(flags), \
9270*f7c14bbaSAndroid Build Coastguard Worker .prog_prepare_load_fn = libbpf_prepare_prog_load, \
9271*f7c14bbaSAndroid Build Coastguard Worker __VA_ARGS__ \
9272*f7c14bbaSAndroid Build Coastguard Worker }
9273*f7c14bbaSAndroid Build Coastguard Worker
9274*f7c14bbaSAndroid Build Coastguard Worker static int attach_kprobe(const struct bpf_program *prog, long cookie, struct bpf_link **link);
9275*f7c14bbaSAndroid Build Coastguard Worker static int attach_uprobe(const struct bpf_program *prog, long cookie, struct bpf_link **link);
9276*f7c14bbaSAndroid Build Coastguard Worker static int attach_ksyscall(const struct bpf_program *prog, long cookie, struct bpf_link **link);
9277*f7c14bbaSAndroid Build Coastguard Worker static int attach_usdt(const struct bpf_program *prog, long cookie, struct bpf_link **link);
9278*f7c14bbaSAndroid Build Coastguard Worker static int attach_tp(const struct bpf_program *prog, long cookie, struct bpf_link **link);
9279*f7c14bbaSAndroid Build Coastguard Worker static int attach_raw_tp(const struct bpf_program *prog, long cookie, struct bpf_link **link);
9280*f7c14bbaSAndroid Build Coastguard Worker static int attach_trace(const struct bpf_program *prog, long cookie, struct bpf_link **link);
9281*f7c14bbaSAndroid Build Coastguard Worker static int attach_kprobe_multi(const struct bpf_program *prog, long cookie, struct bpf_link **link);
9282*f7c14bbaSAndroid Build Coastguard Worker static int attach_uprobe_multi(const struct bpf_program *prog, long cookie, struct bpf_link **link);
9283*f7c14bbaSAndroid Build Coastguard Worker static int attach_lsm(const struct bpf_program *prog, long cookie, struct bpf_link **link);
9284*f7c14bbaSAndroid Build Coastguard Worker static int attach_iter(const struct bpf_program *prog, long cookie, struct bpf_link **link);
9285*f7c14bbaSAndroid Build Coastguard Worker
9286*f7c14bbaSAndroid Build Coastguard Worker static const struct bpf_sec_def section_defs[] = {
9287*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("socket", SOCKET_FILTER, 0, SEC_NONE),
9288*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("sk_reuseport/migrate", SK_REUSEPORT, BPF_SK_REUSEPORT_SELECT_OR_MIGRATE, SEC_ATTACHABLE),
9289*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("sk_reuseport", SK_REUSEPORT, BPF_SK_REUSEPORT_SELECT, SEC_ATTACHABLE),
9290*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("kprobe+", KPROBE, 0, SEC_NONE, attach_kprobe),
9291*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("uprobe+", KPROBE, 0, SEC_NONE, attach_uprobe),
9292*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("uprobe.s+", KPROBE, 0, SEC_SLEEPABLE, attach_uprobe),
9293*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("kretprobe+", KPROBE, 0, SEC_NONE, attach_kprobe),
9294*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("uretprobe+", KPROBE, 0, SEC_NONE, attach_uprobe),
9295*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("uretprobe.s+", KPROBE, 0, SEC_SLEEPABLE, attach_uprobe),
9296*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("kprobe.multi+", KPROBE, BPF_TRACE_KPROBE_MULTI, SEC_NONE, attach_kprobe_multi),
9297*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("kretprobe.multi+", KPROBE, BPF_TRACE_KPROBE_MULTI, SEC_NONE, attach_kprobe_multi),
9298*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("uprobe.multi+", KPROBE, BPF_TRACE_UPROBE_MULTI, SEC_NONE, attach_uprobe_multi),
9299*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("uretprobe.multi+", KPROBE, BPF_TRACE_UPROBE_MULTI, SEC_NONE, attach_uprobe_multi),
9300*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("uprobe.multi.s+", KPROBE, BPF_TRACE_UPROBE_MULTI, SEC_SLEEPABLE, attach_uprobe_multi),
9301*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("uretprobe.multi.s+", KPROBE, BPF_TRACE_UPROBE_MULTI, SEC_SLEEPABLE, attach_uprobe_multi),
9302*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("ksyscall+", KPROBE, 0, SEC_NONE, attach_ksyscall),
9303*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("kretsyscall+", KPROBE, 0, SEC_NONE, attach_ksyscall),
9304*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("usdt+", KPROBE, 0, SEC_USDT, attach_usdt),
9305*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("usdt.s+", KPROBE, 0, SEC_USDT | SEC_SLEEPABLE, attach_usdt),
9306*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("tc/ingress", SCHED_CLS, BPF_TCX_INGRESS, SEC_NONE), /* alias for tcx */
9307*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("tc/egress", SCHED_CLS, BPF_TCX_EGRESS, SEC_NONE), /* alias for tcx */
9308*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("tcx/ingress", SCHED_CLS, BPF_TCX_INGRESS, SEC_NONE),
9309*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("tcx/egress", SCHED_CLS, BPF_TCX_EGRESS, SEC_NONE),
9310*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("tc", SCHED_CLS, 0, SEC_NONE), /* deprecated / legacy, use tcx */
9311*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("classifier", SCHED_CLS, 0, SEC_NONE), /* deprecated / legacy, use tcx */
9312*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("action", SCHED_ACT, 0, SEC_NONE), /* deprecated / legacy, use tcx */
9313*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("netkit/primary", SCHED_CLS, BPF_NETKIT_PRIMARY, SEC_NONE),
9314*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("netkit/peer", SCHED_CLS, BPF_NETKIT_PEER, SEC_NONE),
9315*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("tracepoint+", TRACEPOINT, 0, SEC_NONE, attach_tp),
9316*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("tp+", TRACEPOINT, 0, SEC_NONE, attach_tp),
9317*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("raw_tracepoint+", RAW_TRACEPOINT, 0, SEC_NONE, attach_raw_tp),
9318*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("raw_tp+", RAW_TRACEPOINT, 0, SEC_NONE, attach_raw_tp),
9319*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("raw_tracepoint.w+", RAW_TRACEPOINT_WRITABLE, 0, SEC_NONE, attach_raw_tp),
9320*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("raw_tp.w+", RAW_TRACEPOINT_WRITABLE, 0, SEC_NONE, attach_raw_tp),
9321*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("tp_btf+", TRACING, BPF_TRACE_RAW_TP, SEC_ATTACH_BTF, attach_trace),
9322*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("fentry+", TRACING, BPF_TRACE_FENTRY, SEC_ATTACH_BTF, attach_trace),
9323*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("fmod_ret+", TRACING, BPF_MODIFY_RETURN, SEC_ATTACH_BTF, attach_trace),
9324*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("fexit+", TRACING, BPF_TRACE_FEXIT, SEC_ATTACH_BTF, attach_trace),
9325*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("fentry.s+", TRACING, BPF_TRACE_FENTRY, SEC_ATTACH_BTF | SEC_SLEEPABLE, attach_trace),
9326*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("fmod_ret.s+", TRACING, BPF_MODIFY_RETURN, SEC_ATTACH_BTF | SEC_SLEEPABLE, attach_trace),
9327*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("fexit.s+", TRACING, BPF_TRACE_FEXIT, SEC_ATTACH_BTF | SEC_SLEEPABLE, attach_trace),
9328*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("freplace+", EXT, 0, SEC_ATTACH_BTF, attach_trace),
9329*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("lsm+", LSM, BPF_LSM_MAC, SEC_ATTACH_BTF, attach_lsm),
9330*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("lsm.s+", LSM, BPF_LSM_MAC, SEC_ATTACH_BTF | SEC_SLEEPABLE, attach_lsm),
9331*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("lsm_cgroup+", LSM, BPF_LSM_CGROUP, SEC_ATTACH_BTF),
9332*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("iter+", TRACING, BPF_TRACE_ITER, SEC_ATTACH_BTF, attach_iter),
9333*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("iter.s+", TRACING, BPF_TRACE_ITER, SEC_ATTACH_BTF | SEC_SLEEPABLE, attach_iter),
9334*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("syscall", SYSCALL, 0, SEC_SLEEPABLE),
9335*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("xdp.frags/devmap", XDP, BPF_XDP_DEVMAP, SEC_XDP_FRAGS),
9336*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("xdp/devmap", XDP, BPF_XDP_DEVMAP, SEC_ATTACHABLE),
9337*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("xdp.frags/cpumap", XDP, BPF_XDP_CPUMAP, SEC_XDP_FRAGS),
9338*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("xdp/cpumap", XDP, BPF_XDP_CPUMAP, SEC_ATTACHABLE),
9339*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("xdp.frags", XDP, BPF_XDP, SEC_XDP_FRAGS),
9340*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("xdp", XDP, BPF_XDP, SEC_ATTACHABLE_OPT),
9341*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("perf_event", PERF_EVENT, 0, SEC_NONE),
9342*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("lwt_in", LWT_IN, 0, SEC_NONE),
9343*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("lwt_out", LWT_OUT, 0, SEC_NONE),
9344*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("lwt_xmit", LWT_XMIT, 0, SEC_NONE),
9345*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("lwt_seg6local", LWT_SEG6LOCAL, 0, SEC_NONE),
9346*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("sockops", SOCK_OPS, BPF_CGROUP_SOCK_OPS, SEC_ATTACHABLE_OPT),
9347*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("sk_skb/stream_parser", SK_SKB, BPF_SK_SKB_STREAM_PARSER, SEC_ATTACHABLE_OPT),
9348*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("sk_skb/stream_verdict",SK_SKB, BPF_SK_SKB_STREAM_VERDICT, SEC_ATTACHABLE_OPT),
9349*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("sk_skb/verdict", SK_SKB, BPF_SK_SKB_VERDICT, SEC_ATTACHABLE_OPT),
9350*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("sk_skb", SK_SKB, 0, SEC_NONE),
9351*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("sk_msg", SK_MSG, BPF_SK_MSG_VERDICT, SEC_ATTACHABLE_OPT),
9352*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("lirc_mode2", LIRC_MODE2, BPF_LIRC_MODE2, SEC_ATTACHABLE_OPT),
9353*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("flow_dissector", FLOW_DISSECTOR, BPF_FLOW_DISSECTOR, SEC_ATTACHABLE_OPT),
9354*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("cgroup_skb/ingress", CGROUP_SKB, BPF_CGROUP_INET_INGRESS, SEC_ATTACHABLE_OPT),
9355*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("cgroup_skb/egress", CGROUP_SKB, BPF_CGROUP_INET_EGRESS, SEC_ATTACHABLE_OPT),
9356*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("cgroup/skb", CGROUP_SKB, 0, SEC_NONE),
9357*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("cgroup/sock_create", CGROUP_SOCK, BPF_CGROUP_INET_SOCK_CREATE, SEC_ATTACHABLE),
9358*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("cgroup/sock_release", CGROUP_SOCK, BPF_CGROUP_INET_SOCK_RELEASE, SEC_ATTACHABLE),
9359*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("cgroup/sock", CGROUP_SOCK, BPF_CGROUP_INET_SOCK_CREATE, SEC_ATTACHABLE_OPT),
9360*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("cgroup/post_bind4", CGROUP_SOCK, BPF_CGROUP_INET4_POST_BIND, SEC_ATTACHABLE),
9361*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("cgroup/post_bind6", CGROUP_SOCK, BPF_CGROUP_INET6_POST_BIND, SEC_ATTACHABLE),
9362*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("cgroup/bind4", CGROUP_SOCK_ADDR, BPF_CGROUP_INET4_BIND, SEC_ATTACHABLE),
9363*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("cgroup/bind6", CGROUP_SOCK_ADDR, BPF_CGROUP_INET6_BIND, SEC_ATTACHABLE),
9364*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("cgroup/connect4", CGROUP_SOCK_ADDR, BPF_CGROUP_INET4_CONNECT, SEC_ATTACHABLE),
9365*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("cgroup/connect6", CGROUP_SOCK_ADDR, BPF_CGROUP_INET6_CONNECT, SEC_ATTACHABLE),
9366*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("cgroup/connect_unix", CGROUP_SOCK_ADDR, BPF_CGROUP_UNIX_CONNECT, SEC_ATTACHABLE),
9367*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("cgroup/sendmsg4", CGROUP_SOCK_ADDR, BPF_CGROUP_UDP4_SENDMSG, SEC_ATTACHABLE),
9368*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("cgroup/sendmsg6", CGROUP_SOCK_ADDR, BPF_CGROUP_UDP6_SENDMSG, SEC_ATTACHABLE),
9369*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("cgroup/sendmsg_unix", CGROUP_SOCK_ADDR, BPF_CGROUP_UNIX_SENDMSG, SEC_ATTACHABLE),
9370*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("cgroup/recvmsg4", CGROUP_SOCK_ADDR, BPF_CGROUP_UDP4_RECVMSG, SEC_ATTACHABLE),
9371*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("cgroup/recvmsg6", CGROUP_SOCK_ADDR, BPF_CGROUP_UDP6_RECVMSG, SEC_ATTACHABLE),
9372*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("cgroup/recvmsg_unix", CGROUP_SOCK_ADDR, BPF_CGROUP_UNIX_RECVMSG, SEC_ATTACHABLE),
9373*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("cgroup/getpeername4", CGROUP_SOCK_ADDR, BPF_CGROUP_INET4_GETPEERNAME, SEC_ATTACHABLE),
9374*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("cgroup/getpeername6", CGROUP_SOCK_ADDR, BPF_CGROUP_INET6_GETPEERNAME, SEC_ATTACHABLE),
9375*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("cgroup/getpeername_unix", CGROUP_SOCK_ADDR, BPF_CGROUP_UNIX_GETPEERNAME, SEC_ATTACHABLE),
9376*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("cgroup/getsockname4", CGROUP_SOCK_ADDR, BPF_CGROUP_INET4_GETSOCKNAME, SEC_ATTACHABLE),
9377*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("cgroup/getsockname6", CGROUP_SOCK_ADDR, BPF_CGROUP_INET6_GETSOCKNAME, SEC_ATTACHABLE),
9378*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("cgroup/getsockname_unix", CGROUP_SOCK_ADDR, BPF_CGROUP_UNIX_GETSOCKNAME, SEC_ATTACHABLE),
9379*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("cgroup/sysctl", CGROUP_SYSCTL, BPF_CGROUP_SYSCTL, SEC_ATTACHABLE),
9380*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("cgroup/getsockopt", CGROUP_SOCKOPT, BPF_CGROUP_GETSOCKOPT, SEC_ATTACHABLE),
9381*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("cgroup/setsockopt", CGROUP_SOCKOPT, BPF_CGROUP_SETSOCKOPT, SEC_ATTACHABLE),
9382*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("cgroup/dev", CGROUP_DEVICE, BPF_CGROUP_DEVICE, SEC_ATTACHABLE_OPT),
9383*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("struct_ops+", STRUCT_OPS, 0, SEC_NONE),
9384*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("struct_ops.s+", STRUCT_OPS, 0, SEC_SLEEPABLE),
9385*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("sk_lookup", SK_LOOKUP, BPF_SK_LOOKUP, SEC_ATTACHABLE),
9386*f7c14bbaSAndroid Build Coastguard Worker SEC_DEF("netfilter", NETFILTER, BPF_NETFILTER, SEC_NONE),
9387*f7c14bbaSAndroid Build Coastguard Worker };
9388*f7c14bbaSAndroid Build Coastguard Worker
libbpf_register_prog_handler(const char * sec,enum bpf_prog_type prog_type,enum bpf_attach_type exp_attach_type,const struct libbpf_prog_handler_opts * opts)9389*f7c14bbaSAndroid Build Coastguard Worker int libbpf_register_prog_handler(const char *sec,
9390*f7c14bbaSAndroid Build Coastguard Worker enum bpf_prog_type prog_type,
9391*f7c14bbaSAndroid Build Coastguard Worker enum bpf_attach_type exp_attach_type,
9392*f7c14bbaSAndroid Build Coastguard Worker const struct libbpf_prog_handler_opts *opts)
9393*f7c14bbaSAndroid Build Coastguard Worker {
9394*f7c14bbaSAndroid Build Coastguard Worker struct bpf_sec_def *sec_def;
9395*f7c14bbaSAndroid Build Coastguard Worker
9396*f7c14bbaSAndroid Build Coastguard Worker if (!OPTS_VALID(opts, libbpf_prog_handler_opts))
9397*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-EINVAL);
9398*f7c14bbaSAndroid Build Coastguard Worker
9399*f7c14bbaSAndroid Build Coastguard Worker if (last_custom_sec_def_handler_id == INT_MAX) /* prevent overflow */
9400*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-E2BIG);
9401*f7c14bbaSAndroid Build Coastguard Worker
9402*f7c14bbaSAndroid Build Coastguard Worker if (sec) {
9403*f7c14bbaSAndroid Build Coastguard Worker sec_def = libbpf_reallocarray(custom_sec_defs, custom_sec_def_cnt + 1,
9404*f7c14bbaSAndroid Build Coastguard Worker sizeof(*sec_def));
9405*f7c14bbaSAndroid Build Coastguard Worker if (!sec_def)
9406*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-ENOMEM);
9407*f7c14bbaSAndroid Build Coastguard Worker
9408*f7c14bbaSAndroid Build Coastguard Worker custom_sec_defs = sec_def;
9409*f7c14bbaSAndroid Build Coastguard Worker sec_def = &custom_sec_defs[custom_sec_def_cnt];
9410*f7c14bbaSAndroid Build Coastguard Worker } else {
9411*f7c14bbaSAndroid Build Coastguard Worker if (has_custom_fallback_def)
9412*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-EBUSY);
9413*f7c14bbaSAndroid Build Coastguard Worker
9414*f7c14bbaSAndroid Build Coastguard Worker sec_def = &custom_fallback_def;
9415*f7c14bbaSAndroid Build Coastguard Worker }
9416*f7c14bbaSAndroid Build Coastguard Worker
9417*f7c14bbaSAndroid Build Coastguard Worker sec_def->sec = sec ? strdup(sec) : NULL;
9418*f7c14bbaSAndroid Build Coastguard Worker if (sec && !sec_def->sec)
9419*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-ENOMEM);
9420*f7c14bbaSAndroid Build Coastguard Worker
9421*f7c14bbaSAndroid Build Coastguard Worker sec_def->prog_type = prog_type;
9422*f7c14bbaSAndroid Build Coastguard Worker sec_def->expected_attach_type = exp_attach_type;
9423*f7c14bbaSAndroid Build Coastguard Worker sec_def->cookie = OPTS_GET(opts, cookie, 0);
9424*f7c14bbaSAndroid Build Coastguard Worker
9425*f7c14bbaSAndroid Build Coastguard Worker sec_def->prog_setup_fn = OPTS_GET(opts, prog_setup_fn, NULL);
9426*f7c14bbaSAndroid Build Coastguard Worker sec_def->prog_prepare_load_fn = OPTS_GET(opts, prog_prepare_load_fn, NULL);
9427*f7c14bbaSAndroid Build Coastguard Worker sec_def->prog_attach_fn = OPTS_GET(opts, prog_attach_fn, NULL);
9428*f7c14bbaSAndroid Build Coastguard Worker
9429*f7c14bbaSAndroid Build Coastguard Worker sec_def->handler_id = ++last_custom_sec_def_handler_id;
9430*f7c14bbaSAndroid Build Coastguard Worker
9431*f7c14bbaSAndroid Build Coastguard Worker if (sec)
9432*f7c14bbaSAndroid Build Coastguard Worker custom_sec_def_cnt++;
9433*f7c14bbaSAndroid Build Coastguard Worker else
9434*f7c14bbaSAndroid Build Coastguard Worker has_custom_fallback_def = true;
9435*f7c14bbaSAndroid Build Coastguard Worker
9436*f7c14bbaSAndroid Build Coastguard Worker return sec_def->handler_id;
9437*f7c14bbaSAndroid Build Coastguard Worker }
9438*f7c14bbaSAndroid Build Coastguard Worker
libbpf_unregister_prog_handler(int handler_id)9439*f7c14bbaSAndroid Build Coastguard Worker int libbpf_unregister_prog_handler(int handler_id)
9440*f7c14bbaSAndroid Build Coastguard Worker {
9441*f7c14bbaSAndroid Build Coastguard Worker struct bpf_sec_def *sec_defs;
9442*f7c14bbaSAndroid Build Coastguard Worker int i;
9443*f7c14bbaSAndroid Build Coastguard Worker
9444*f7c14bbaSAndroid Build Coastguard Worker if (handler_id <= 0)
9445*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-EINVAL);
9446*f7c14bbaSAndroid Build Coastguard Worker
9447*f7c14bbaSAndroid Build Coastguard Worker if (has_custom_fallback_def && custom_fallback_def.handler_id == handler_id) {
9448*f7c14bbaSAndroid Build Coastguard Worker memset(&custom_fallback_def, 0, sizeof(custom_fallback_def));
9449*f7c14bbaSAndroid Build Coastguard Worker has_custom_fallback_def = false;
9450*f7c14bbaSAndroid Build Coastguard Worker return 0;
9451*f7c14bbaSAndroid Build Coastguard Worker }
9452*f7c14bbaSAndroid Build Coastguard Worker
9453*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < custom_sec_def_cnt; i++) {
9454*f7c14bbaSAndroid Build Coastguard Worker if (custom_sec_defs[i].handler_id == handler_id)
9455*f7c14bbaSAndroid Build Coastguard Worker break;
9456*f7c14bbaSAndroid Build Coastguard Worker }
9457*f7c14bbaSAndroid Build Coastguard Worker
9458*f7c14bbaSAndroid Build Coastguard Worker if (i == custom_sec_def_cnt)
9459*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-ENOENT);
9460*f7c14bbaSAndroid Build Coastguard Worker
9461*f7c14bbaSAndroid Build Coastguard Worker free(custom_sec_defs[i].sec);
9462*f7c14bbaSAndroid Build Coastguard Worker for (i = i + 1; i < custom_sec_def_cnt; i++)
9463*f7c14bbaSAndroid Build Coastguard Worker custom_sec_defs[i - 1] = custom_sec_defs[i];
9464*f7c14bbaSAndroid Build Coastguard Worker custom_sec_def_cnt--;
9465*f7c14bbaSAndroid Build Coastguard Worker
9466*f7c14bbaSAndroid Build Coastguard Worker /* try to shrink the array, but it's ok if we couldn't */
9467*f7c14bbaSAndroid Build Coastguard Worker sec_defs = libbpf_reallocarray(custom_sec_defs, custom_sec_def_cnt, sizeof(*sec_defs));
9468*f7c14bbaSAndroid Build Coastguard Worker /* if new count is zero, reallocarray can return a valid NULL result;
9469*f7c14bbaSAndroid Build Coastguard Worker * in this case the previous pointer will be freed, so we *have to*
9470*f7c14bbaSAndroid Build Coastguard Worker * reassign old pointer to the new value (even if it's NULL)
9471*f7c14bbaSAndroid Build Coastguard Worker */
9472*f7c14bbaSAndroid Build Coastguard Worker if (sec_defs || custom_sec_def_cnt == 0)
9473*f7c14bbaSAndroid Build Coastguard Worker custom_sec_defs = sec_defs;
9474*f7c14bbaSAndroid Build Coastguard Worker
9475*f7c14bbaSAndroid Build Coastguard Worker return 0;
9476*f7c14bbaSAndroid Build Coastguard Worker }
9477*f7c14bbaSAndroid Build Coastguard Worker
sec_def_matches(const struct bpf_sec_def * sec_def,const char * sec_name)9478*f7c14bbaSAndroid Build Coastguard Worker static bool sec_def_matches(const struct bpf_sec_def *sec_def, const char *sec_name)
9479*f7c14bbaSAndroid Build Coastguard Worker {
9480*f7c14bbaSAndroid Build Coastguard Worker size_t len = strlen(sec_def->sec);
9481*f7c14bbaSAndroid Build Coastguard Worker
9482*f7c14bbaSAndroid Build Coastguard Worker /* "type/" always has to have proper SEC("type/extras") form */
9483*f7c14bbaSAndroid Build Coastguard Worker if (sec_def->sec[len - 1] == '/') {
9484*f7c14bbaSAndroid Build Coastguard Worker if (str_has_pfx(sec_name, sec_def->sec))
9485*f7c14bbaSAndroid Build Coastguard Worker return true;
9486*f7c14bbaSAndroid Build Coastguard Worker return false;
9487*f7c14bbaSAndroid Build Coastguard Worker }
9488*f7c14bbaSAndroid Build Coastguard Worker
9489*f7c14bbaSAndroid Build Coastguard Worker /* "type+" means it can be either exact SEC("type") or
9490*f7c14bbaSAndroid Build Coastguard Worker * well-formed SEC("type/extras") with proper '/' separator
9491*f7c14bbaSAndroid Build Coastguard Worker */
9492*f7c14bbaSAndroid Build Coastguard Worker if (sec_def->sec[len - 1] == '+') {
9493*f7c14bbaSAndroid Build Coastguard Worker len--;
9494*f7c14bbaSAndroid Build Coastguard Worker /* not even a prefix */
9495*f7c14bbaSAndroid Build Coastguard Worker if (strncmp(sec_name, sec_def->sec, len) != 0)
9496*f7c14bbaSAndroid Build Coastguard Worker return false;
9497*f7c14bbaSAndroid Build Coastguard Worker /* exact match or has '/' separator */
9498*f7c14bbaSAndroid Build Coastguard Worker if (sec_name[len] == '\0' || sec_name[len] == '/')
9499*f7c14bbaSAndroid Build Coastguard Worker return true;
9500*f7c14bbaSAndroid Build Coastguard Worker return false;
9501*f7c14bbaSAndroid Build Coastguard Worker }
9502*f7c14bbaSAndroid Build Coastguard Worker
9503*f7c14bbaSAndroid Build Coastguard Worker return strcmp(sec_name, sec_def->sec) == 0;
9504*f7c14bbaSAndroid Build Coastguard Worker }
9505*f7c14bbaSAndroid Build Coastguard Worker
find_sec_def(const char * sec_name)9506*f7c14bbaSAndroid Build Coastguard Worker static const struct bpf_sec_def *find_sec_def(const char *sec_name)
9507*f7c14bbaSAndroid Build Coastguard Worker {
9508*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_sec_def *sec_def;
9509*f7c14bbaSAndroid Build Coastguard Worker int i, n;
9510*f7c14bbaSAndroid Build Coastguard Worker
9511*f7c14bbaSAndroid Build Coastguard Worker n = custom_sec_def_cnt;
9512*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < n; i++) {
9513*f7c14bbaSAndroid Build Coastguard Worker sec_def = &custom_sec_defs[i];
9514*f7c14bbaSAndroid Build Coastguard Worker if (sec_def_matches(sec_def, sec_name))
9515*f7c14bbaSAndroid Build Coastguard Worker return sec_def;
9516*f7c14bbaSAndroid Build Coastguard Worker }
9517*f7c14bbaSAndroid Build Coastguard Worker
9518*f7c14bbaSAndroid Build Coastguard Worker n = ARRAY_SIZE(section_defs);
9519*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < n; i++) {
9520*f7c14bbaSAndroid Build Coastguard Worker sec_def = §ion_defs[i];
9521*f7c14bbaSAndroid Build Coastguard Worker if (sec_def_matches(sec_def, sec_name))
9522*f7c14bbaSAndroid Build Coastguard Worker return sec_def;
9523*f7c14bbaSAndroid Build Coastguard Worker }
9524*f7c14bbaSAndroid Build Coastguard Worker
9525*f7c14bbaSAndroid Build Coastguard Worker if (has_custom_fallback_def)
9526*f7c14bbaSAndroid Build Coastguard Worker return &custom_fallback_def;
9527*f7c14bbaSAndroid Build Coastguard Worker
9528*f7c14bbaSAndroid Build Coastguard Worker return NULL;
9529*f7c14bbaSAndroid Build Coastguard Worker }
9530*f7c14bbaSAndroid Build Coastguard Worker
9531*f7c14bbaSAndroid Build Coastguard Worker #define MAX_TYPE_NAME_SIZE 32
9532*f7c14bbaSAndroid Build Coastguard Worker
libbpf_get_type_names(bool attach_type)9533*f7c14bbaSAndroid Build Coastguard Worker static char *libbpf_get_type_names(bool attach_type)
9534*f7c14bbaSAndroid Build Coastguard Worker {
9535*f7c14bbaSAndroid Build Coastguard Worker int i, len = ARRAY_SIZE(section_defs) * MAX_TYPE_NAME_SIZE;
9536*f7c14bbaSAndroid Build Coastguard Worker char *buf;
9537*f7c14bbaSAndroid Build Coastguard Worker
9538*f7c14bbaSAndroid Build Coastguard Worker buf = malloc(len);
9539*f7c14bbaSAndroid Build Coastguard Worker if (!buf)
9540*f7c14bbaSAndroid Build Coastguard Worker return NULL;
9541*f7c14bbaSAndroid Build Coastguard Worker
9542*f7c14bbaSAndroid Build Coastguard Worker buf[0] = '\0';
9543*f7c14bbaSAndroid Build Coastguard Worker /* Forge string buf with all available names */
9544*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < ARRAY_SIZE(section_defs); i++) {
9545*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_sec_def *sec_def = §ion_defs[i];
9546*f7c14bbaSAndroid Build Coastguard Worker
9547*f7c14bbaSAndroid Build Coastguard Worker if (attach_type) {
9548*f7c14bbaSAndroid Build Coastguard Worker if (sec_def->prog_prepare_load_fn != libbpf_prepare_prog_load)
9549*f7c14bbaSAndroid Build Coastguard Worker continue;
9550*f7c14bbaSAndroid Build Coastguard Worker
9551*f7c14bbaSAndroid Build Coastguard Worker if (!(sec_def->cookie & SEC_ATTACHABLE))
9552*f7c14bbaSAndroid Build Coastguard Worker continue;
9553*f7c14bbaSAndroid Build Coastguard Worker }
9554*f7c14bbaSAndroid Build Coastguard Worker
9555*f7c14bbaSAndroid Build Coastguard Worker if (strlen(buf) + strlen(section_defs[i].sec) + 2 > len) {
9556*f7c14bbaSAndroid Build Coastguard Worker free(buf);
9557*f7c14bbaSAndroid Build Coastguard Worker return NULL;
9558*f7c14bbaSAndroid Build Coastguard Worker }
9559*f7c14bbaSAndroid Build Coastguard Worker strcat(buf, " ");
9560*f7c14bbaSAndroid Build Coastguard Worker strcat(buf, section_defs[i].sec);
9561*f7c14bbaSAndroid Build Coastguard Worker }
9562*f7c14bbaSAndroid Build Coastguard Worker
9563*f7c14bbaSAndroid Build Coastguard Worker return buf;
9564*f7c14bbaSAndroid Build Coastguard Worker }
9565*f7c14bbaSAndroid Build Coastguard Worker
libbpf_prog_type_by_name(const char * name,enum bpf_prog_type * prog_type,enum bpf_attach_type * expected_attach_type)9566*f7c14bbaSAndroid Build Coastguard Worker int libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,
9567*f7c14bbaSAndroid Build Coastguard Worker enum bpf_attach_type *expected_attach_type)
9568*f7c14bbaSAndroid Build Coastguard Worker {
9569*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_sec_def *sec_def;
9570*f7c14bbaSAndroid Build Coastguard Worker char *type_names;
9571*f7c14bbaSAndroid Build Coastguard Worker
9572*f7c14bbaSAndroid Build Coastguard Worker if (!name)
9573*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-EINVAL);
9574*f7c14bbaSAndroid Build Coastguard Worker
9575*f7c14bbaSAndroid Build Coastguard Worker sec_def = find_sec_def(name);
9576*f7c14bbaSAndroid Build Coastguard Worker if (sec_def) {
9577*f7c14bbaSAndroid Build Coastguard Worker *prog_type = sec_def->prog_type;
9578*f7c14bbaSAndroid Build Coastguard Worker *expected_attach_type = sec_def->expected_attach_type;
9579*f7c14bbaSAndroid Build Coastguard Worker return 0;
9580*f7c14bbaSAndroid Build Coastguard Worker }
9581*f7c14bbaSAndroid Build Coastguard Worker
9582*f7c14bbaSAndroid Build Coastguard Worker pr_debug("failed to guess program type from ELF section '%s'\n", name);
9583*f7c14bbaSAndroid Build Coastguard Worker type_names = libbpf_get_type_names(false);
9584*f7c14bbaSAndroid Build Coastguard Worker if (type_names != NULL) {
9585*f7c14bbaSAndroid Build Coastguard Worker pr_debug("supported section(type) names are:%s\n", type_names);
9586*f7c14bbaSAndroid Build Coastguard Worker free(type_names);
9587*f7c14bbaSAndroid Build Coastguard Worker }
9588*f7c14bbaSAndroid Build Coastguard Worker
9589*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-ESRCH);
9590*f7c14bbaSAndroid Build Coastguard Worker }
9591*f7c14bbaSAndroid Build Coastguard Worker
libbpf_bpf_attach_type_str(enum bpf_attach_type t)9592*f7c14bbaSAndroid Build Coastguard Worker const char *libbpf_bpf_attach_type_str(enum bpf_attach_type t)
9593*f7c14bbaSAndroid Build Coastguard Worker {
9594*f7c14bbaSAndroid Build Coastguard Worker if (t < 0 || t >= ARRAY_SIZE(attach_type_name))
9595*f7c14bbaSAndroid Build Coastguard Worker return NULL;
9596*f7c14bbaSAndroid Build Coastguard Worker
9597*f7c14bbaSAndroid Build Coastguard Worker return attach_type_name[t];
9598*f7c14bbaSAndroid Build Coastguard Worker }
9599*f7c14bbaSAndroid Build Coastguard Worker
libbpf_bpf_link_type_str(enum bpf_link_type t)9600*f7c14bbaSAndroid Build Coastguard Worker const char *libbpf_bpf_link_type_str(enum bpf_link_type t)
9601*f7c14bbaSAndroid Build Coastguard Worker {
9602*f7c14bbaSAndroid Build Coastguard Worker if (t < 0 || t >= ARRAY_SIZE(link_type_name))
9603*f7c14bbaSAndroid Build Coastguard Worker return NULL;
9604*f7c14bbaSAndroid Build Coastguard Worker
9605*f7c14bbaSAndroid Build Coastguard Worker return link_type_name[t];
9606*f7c14bbaSAndroid Build Coastguard Worker }
9607*f7c14bbaSAndroid Build Coastguard Worker
libbpf_bpf_map_type_str(enum bpf_map_type t)9608*f7c14bbaSAndroid Build Coastguard Worker const char *libbpf_bpf_map_type_str(enum bpf_map_type t)
9609*f7c14bbaSAndroid Build Coastguard Worker {
9610*f7c14bbaSAndroid Build Coastguard Worker if (t < 0 || t >= ARRAY_SIZE(map_type_name))
9611*f7c14bbaSAndroid Build Coastguard Worker return NULL;
9612*f7c14bbaSAndroid Build Coastguard Worker
9613*f7c14bbaSAndroid Build Coastguard Worker return map_type_name[t];
9614*f7c14bbaSAndroid Build Coastguard Worker }
9615*f7c14bbaSAndroid Build Coastguard Worker
libbpf_bpf_prog_type_str(enum bpf_prog_type t)9616*f7c14bbaSAndroid Build Coastguard Worker const char *libbpf_bpf_prog_type_str(enum bpf_prog_type t)
9617*f7c14bbaSAndroid Build Coastguard Worker {
9618*f7c14bbaSAndroid Build Coastguard Worker if (t < 0 || t >= ARRAY_SIZE(prog_type_name))
9619*f7c14bbaSAndroid Build Coastguard Worker return NULL;
9620*f7c14bbaSAndroid Build Coastguard Worker
9621*f7c14bbaSAndroid Build Coastguard Worker return prog_type_name[t];
9622*f7c14bbaSAndroid Build Coastguard Worker }
9623*f7c14bbaSAndroid Build Coastguard Worker
find_struct_ops_map_by_offset(struct bpf_object * obj,int sec_idx,size_t offset)9624*f7c14bbaSAndroid Build Coastguard Worker static struct bpf_map *find_struct_ops_map_by_offset(struct bpf_object *obj,
9625*f7c14bbaSAndroid Build Coastguard Worker int sec_idx,
9626*f7c14bbaSAndroid Build Coastguard Worker size_t offset)
9627*f7c14bbaSAndroid Build Coastguard Worker {
9628*f7c14bbaSAndroid Build Coastguard Worker struct bpf_map *map;
9629*f7c14bbaSAndroid Build Coastguard Worker size_t i;
9630*f7c14bbaSAndroid Build Coastguard Worker
9631*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < obj->nr_maps; i++) {
9632*f7c14bbaSAndroid Build Coastguard Worker map = &obj->maps[i];
9633*f7c14bbaSAndroid Build Coastguard Worker if (!bpf_map__is_struct_ops(map))
9634*f7c14bbaSAndroid Build Coastguard Worker continue;
9635*f7c14bbaSAndroid Build Coastguard Worker if (map->sec_idx == sec_idx &&
9636*f7c14bbaSAndroid Build Coastguard Worker map->sec_offset <= offset &&
9637*f7c14bbaSAndroid Build Coastguard Worker offset - map->sec_offset < map->def.value_size)
9638*f7c14bbaSAndroid Build Coastguard Worker return map;
9639*f7c14bbaSAndroid Build Coastguard Worker }
9640*f7c14bbaSAndroid Build Coastguard Worker
9641*f7c14bbaSAndroid Build Coastguard Worker return NULL;
9642*f7c14bbaSAndroid Build Coastguard Worker }
9643*f7c14bbaSAndroid Build Coastguard Worker
9644*f7c14bbaSAndroid Build Coastguard Worker /* Collect the reloc from ELF, populate the st_ops->progs[], and update
9645*f7c14bbaSAndroid Build Coastguard Worker * st_ops->data for shadow type.
9646*f7c14bbaSAndroid Build Coastguard Worker */
bpf_object__collect_st_ops_relos(struct bpf_object * obj,Elf64_Shdr * shdr,Elf_Data * data)9647*f7c14bbaSAndroid Build Coastguard Worker static int bpf_object__collect_st_ops_relos(struct bpf_object *obj,
9648*f7c14bbaSAndroid Build Coastguard Worker Elf64_Shdr *shdr, Elf_Data *data)
9649*f7c14bbaSAndroid Build Coastguard Worker {
9650*f7c14bbaSAndroid Build Coastguard Worker const struct btf_member *member;
9651*f7c14bbaSAndroid Build Coastguard Worker struct bpf_struct_ops *st_ops;
9652*f7c14bbaSAndroid Build Coastguard Worker struct bpf_program *prog;
9653*f7c14bbaSAndroid Build Coastguard Worker unsigned int shdr_idx;
9654*f7c14bbaSAndroid Build Coastguard Worker const struct btf *btf;
9655*f7c14bbaSAndroid Build Coastguard Worker struct bpf_map *map;
9656*f7c14bbaSAndroid Build Coastguard Worker unsigned int moff, insn_idx;
9657*f7c14bbaSAndroid Build Coastguard Worker const char *name;
9658*f7c14bbaSAndroid Build Coastguard Worker __u32 member_idx;
9659*f7c14bbaSAndroid Build Coastguard Worker Elf64_Sym *sym;
9660*f7c14bbaSAndroid Build Coastguard Worker Elf64_Rel *rel;
9661*f7c14bbaSAndroid Build Coastguard Worker int i, nrels;
9662*f7c14bbaSAndroid Build Coastguard Worker
9663*f7c14bbaSAndroid Build Coastguard Worker btf = obj->btf;
9664*f7c14bbaSAndroid Build Coastguard Worker nrels = shdr->sh_size / shdr->sh_entsize;
9665*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < nrels; i++) {
9666*f7c14bbaSAndroid Build Coastguard Worker rel = elf_rel_by_idx(data, i);
9667*f7c14bbaSAndroid Build Coastguard Worker if (!rel) {
9668*f7c14bbaSAndroid Build Coastguard Worker pr_warn("struct_ops reloc: failed to get %d reloc\n", i);
9669*f7c14bbaSAndroid Build Coastguard Worker return -LIBBPF_ERRNO__FORMAT;
9670*f7c14bbaSAndroid Build Coastguard Worker }
9671*f7c14bbaSAndroid Build Coastguard Worker
9672*f7c14bbaSAndroid Build Coastguard Worker sym = elf_sym_by_idx(obj, ELF64_R_SYM(rel->r_info));
9673*f7c14bbaSAndroid Build Coastguard Worker if (!sym) {
9674*f7c14bbaSAndroid Build Coastguard Worker pr_warn("struct_ops reloc: symbol %zx not found\n",
9675*f7c14bbaSAndroid Build Coastguard Worker (size_t)ELF64_R_SYM(rel->r_info));
9676*f7c14bbaSAndroid Build Coastguard Worker return -LIBBPF_ERRNO__FORMAT;
9677*f7c14bbaSAndroid Build Coastguard Worker }
9678*f7c14bbaSAndroid Build Coastguard Worker
9679*f7c14bbaSAndroid Build Coastguard Worker name = elf_sym_str(obj, sym->st_name) ?: "<?>";
9680*f7c14bbaSAndroid Build Coastguard Worker map = find_struct_ops_map_by_offset(obj, shdr->sh_info, rel->r_offset);
9681*f7c14bbaSAndroid Build Coastguard Worker if (!map) {
9682*f7c14bbaSAndroid Build Coastguard Worker pr_warn("struct_ops reloc: cannot find map at rel->r_offset %zu\n",
9683*f7c14bbaSAndroid Build Coastguard Worker (size_t)rel->r_offset);
9684*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
9685*f7c14bbaSAndroid Build Coastguard Worker }
9686*f7c14bbaSAndroid Build Coastguard Worker
9687*f7c14bbaSAndroid Build Coastguard Worker moff = rel->r_offset - map->sec_offset;
9688*f7c14bbaSAndroid Build Coastguard Worker shdr_idx = sym->st_shndx;
9689*f7c14bbaSAndroid Build Coastguard Worker st_ops = map->st_ops;
9690*f7c14bbaSAndroid Build Coastguard Worker pr_debug("struct_ops reloc %s: for %lld value %lld shdr_idx %u rel->r_offset %zu map->sec_offset %zu name %d (\'%s\')\n",
9691*f7c14bbaSAndroid Build Coastguard Worker map->name,
9692*f7c14bbaSAndroid Build Coastguard Worker (long long)(rel->r_info >> 32),
9693*f7c14bbaSAndroid Build Coastguard Worker (long long)sym->st_value,
9694*f7c14bbaSAndroid Build Coastguard Worker shdr_idx, (size_t)rel->r_offset,
9695*f7c14bbaSAndroid Build Coastguard Worker map->sec_offset, sym->st_name, name);
9696*f7c14bbaSAndroid Build Coastguard Worker
9697*f7c14bbaSAndroid Build Coastguard Worker if (shdr_idx >= SHN_LORESERVE) {
9698*f7c14bbaSAndroid Build Coastguard Worker pr_warn("struct_ops reloc %s: rel->r_offset %zu shdr_idx %u unsupported non-static function\n",
9699*f7c14bbaSAndroid Build Coastguard Worker map->name, (size_t)rel->r_offset, shdr_idx);
9700*f7c14bbaSAndroid Build Coastguard Worker return -LIBBPF_ERRNO__RELOC;
9701*f7c14bbaSAndroid Build Coastguard Worker }
9702*f7c14bbaSAndroid Build Coastguard Worker if (sym->st_value % BPF_INSN_SZ) {
9703*f7c14bbaSAndroid Build Coastguard Worker pr_warn("struct_ops reloc %s: invalid target program offset %llu\n",
9704*f7c14bbaSAndroid Build Coastguard Worker map->name, (unsigned long long)sym->st_value);
9705*f7c14bbaSAndroid Build Coastguard Worker return -LIBBPF_ERRNO__FORMAT;
9706*f7c14bbaSAndroid Build Coastguard Worker }
9707*f7c14bbaSAndroid Build Coastguard Worker insn_idx = sym->st_value / BPF_INSN_SZ;
9708*f7c14bbaSAndroid Build Coastguard Worker
9709*f7c14bbaSAndroid Build Coastguard Worker member = find_member_by_offset(st_ops->type, moff * 8);
9710*f7c14bbaSAndroid Build Coastguard Worker if (!member) {
9711*f7c14bbaSAndroid Build Coastguard Worker pr_warn("struct_ops reloc %s: cannot find member at moff %u\n",
9712*f7c14bbaSAndroid Build Coastguard Worker map->name, moff);
9713*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
9714*f7c14bbaSAndroid Build Coastguard Worker }
9715*f7c14bbaSAndroid Build Coastguard Worker member_idx = member - btf_members(st_ops->type);
9716*f7c14bbaSAndroid Build Coastguard Worker name = btf__name_by_offset(btf, member->name_off);
9717*f7c14bbaSAndroid Build Coastguard Worker
9718*f7c14bbaSAndroid Build Coastguard Worker if (!resolve_func_ptr(btf, member->type, NULL)) {
9719*f7c14bbaSAndroid Build Coastguard Worker pr_warn("struct_ops reloc %s: cannot relocate non func ptr %s\n",
9720*f7c14bbaSAndroid Build Coastguard Worker map->name, name);
9721*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
9722*f7c14bbaSAndroid Build Coastguard Worker }
9723*f7c14bbaSAndroid Build Coastguard Worker
9724*f7c14bbaSAndroid Build Coastguard Worker prog = find_prog_by_sec_insn(obj, shdr_idx, insn_idx);
9725*f7c14bbaSAndroid Build Coastguard Worker if (!prog) {
9726*f7c14bbaSAndroid Build Coastguard Worker pr_warn("struct_ops reloc %s: cannot find prog at shdr_idx %u to relocate func ptr %s\n",
9727*f7c14bbaSAndroid Build Coastguard Worker map->name, shdr_idx, name);
9728*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
9729*f7c14bbaSAndroid Build Coastguard Worker }
9730*f7c14bbaSAndroid Build Coastguard Worker
9731*f7c14bbaSAndroid Build Coastguard Worker /* prevent the use of BPF prog with invalid type */
9732*f7c14bbaSAndroid Build Coastguard Worker if (prog->type != BPF_PROG_TYPE_STRUCT_OPS) {
9733*f7c14bbaSAndroid Build Coastguard Worker pr_warn("struct_ops reloc %s: prog %s is not struct_ops BPF program\n",
9734*f7c14bbaSAndroid Build Coastguard Worker map->name, prog->name);
9735*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
9736*f7c14bbaSAndroid Build Coastguard Worker }
9737*f7c14bbaSAndroid Build Coastguard Worker
9738*f7c14bbaSAndroid Build Coastguard Worker st_ops->progs[member_idx] = prog;
9739*f7c14bbaSAndroid Build Coastguard Worker
9740*f7c14bbaSAndroid Build Coastguard Worker /* st_ops->data will be exposed to users, being returned by
9741*f7c14bbaSAndroid Build Coastguard Worker * bpf_map__initial_value() as a pointer to the shadow
9742*f7c14bbaSAndroid Build Coastguard Worker * type. All function pointers in the original struct type
9743*f7c14bbaSAndroid Build Coastguard Worker * should be converted to a pointer to struct bpf_program
9744*f7c14bbaSAndroid Build Coastguard Worker * in the shadow type.
9745*f7c14bbaSAndroid Build Coastguard Worker */
9746*f7c14bbaSAndroid Build Coastguard Worker *((struct bpf_program **)(st_ops->data + moff)) = prog;
9747*f7c14bbaSAndroid Build Coastguard Worker }
9748*f7c14bbaSAndroid Build Coastguard Worker
9749*f7c14bbaSAndroid Build Coastguard Worker return 0;
9750*f7c14bbaSAndroid Build Coastguard Worker }
9751*f7c14bbaSAndroid Build Coastguard Worker
9752*f7c14bbaSAndroid Build Coastguard Worker #define BTF_TRACE_PREFIX "btf_trace_"
9753*f7c14bbaSAndroid Build Coastguard Worker #define BTF_LSM_PREFIX "bpf_lsm_"
9754*f7c14bbaSAndroid Build Coastguard Worker #define BTF_ITER_PREFIX "bpf_iter_"
9755*f7c14bbaSAndroid Build Coastguard Worker #define BTF_MAX_NAME_SIZE 128
9756*f7c14bbaSAndroid Build Coastguard Worker
btf_get_kernel_prefix_kind(enum bpf_attach_type attach_type,const char ** prefix,int * kind)9757*f7c14bbaSAndroid Build Coastguard Worker void btf_get_kernel_prefix_kind(enum bpf_attach_type attach_type,
9758*f7c14bbaSAndroid Build Coastguard Worker const char **prefix, int *kind)
9759*f7c14bbaSAndroid Build Coastguard Worker {
9760*f7c14bbaSAndroid Build Coastguard Worker switch (attach_type) {
9761*f7c14bbaSAndroid Build Coastguard Worker case BPF_TRACE_RAW_TP:
9762*f7c14bbaSAndroid Build Coastguard Worker *prefix = BTF_TRACE_PREFIX;
9763*f7c14bbaSAndroid Build Coastguard Worker *kind = BTF_KIND_TYPEDEF;
9764*f7c14bbaSAndroid Build Coastguard Worker break;
9765*f7c14bbaSAndroid Build Coastguard Worker case BPF_LSM_MAC:
9766*f7c14bbaSAndroid Build Coastguard Worker case BPF_LSM_CGROUP:
9767*f7c14bbaSAndroid Build Coastguard Worker *prefix = BTF_LSM_PREFIX;
9768*f7c14bbaSAndroid Build Coastguard Worker *kind = BTF_KIND_FUNC;
9769*f7c14bbaSAndroid Build Coastguard Worker break;
9770*f7c14bbaSAndroid Build Coastguard Worker case BPF_TRACE_ITER:
9771*f7c14bbaSAndroid Build Coastguard Worker *prefix = BTF_ITER_PREFIX;
9772*f7c14bbaSAndroid Build Coastguard Worker *kind = BTF_KIND_FUNC;
9773*f7c14bbaSAndroid Build Coastguard Worker break;
9774*f7c14bbaSAndroid Build Coastguard Worker default:
9775*f7c14bbaSAndroid Build Coastguard Worker *prefix = "";
9776*f7c14bbaSAndroid Build Coastguard Worker *kind = BTF_KIND_FUNC;
9777*f7c14bbaSAndroid Build Coastguard Worker }
9778*f7c14bbaSAndroid Build Coastguard Worker }
9779*f7c14bbaSAndroid Build Coastguard Worker
find_btf_by_prefix_kind(const struct btf * btf,const char * prefix,const char * name,__u32 kind)9780*f7c14bbaSAndroid Build Coastguard Worker static int find_btf_by_prefix_kind(const struct btf *btf, const char *prefix,
9781*f7c14bbaSAndroid Build Coastguard Worker const char *name, __u32 kind)
9782*f7c14bbaSAndroid Build Coastguard Worker {
9783*f7c14bbaSAndroid Build Coastguard Worker char btf_type_name[BTF_MAX_NAME_SIZE];
9784*f7c14bbaSAndroid Build Coastguard Worker int ret;
9785*f7c14bbaSAndroid Build Coastguard Worker
9786*f7c14bbaSAndroid Build Coastguard Worker ret = snprintf(btf_type_name, sizeof(btf_type_name),
9787*f7c14bbaSAndroid Build Coastguard Worker "%s%s", prefix, name);
9788*f7c14bbaSAndroid Build Coastguard Worker /* snprintf returns the number of characters written excluding the
9789*f7c14bbaSAndroid Build Coastguard Worker * terminating null. So, if >= BTF_MAX_NAME_SIZE are written, it
9790*f7c14bbaSAndroid Build Coastguard Worker * indicates truncation.
9791*f7c14bbaSAndroid Build Coastguard Worker */
9792*f7c14bbaSAndroid Build Coastguard Worker if (ret < 0 || ret >= sizeof(btf_type_name))
9793*f7c14bbaSAndroid Build Coastguard Worker return -ENAMETOOLONG;
9794*f7c14bbaSAndroid Build Coastguard Worker return btf__find_by_name_kind(btf, btf_type_name, kind);
9795*f7c14bbaSAndroid Build Coastguard Worker }
9796*f7c14bbaSAndroid Build Coastguard Worker
find_attach_btf_id(struct btf * btf,const char * name,enum bpf_attach_type attach_type)9797*f7c14bbaSAndroid Build Coastguard Worker static inline int find_attach_btf_id(struct btf *btf, const char *name,
9798*f7c14bbaSAndroid Build Coastguard Worker enum bpf_attach_type attach_type)
9799*f7c14bbaSAndroid Build Coastguard Worker {
9800*f7c14bbaSAndroid Build Coastguard Worker const char *prefix;
9801*f7c14bbaSAndroid Build Coastguard Worker int kind;
9802*f7c14bbaSAndroid Build Coastguard Worker
9803*f7c14bbaSAndroid Build Coastguard Worker btf_get_kernel_prefix_kind(attach_type, &prefix, &kind);
9804*f7c14bbaSAndroid Build Coastguard Worker return find_btf_by_prefix_kind(btf, prefix, name, kind);
9805*f7c14bbaSAndroid Build Coastguard Worker }
9806*f7c14bbaSAndroid Build Coastguard Worker
libbpf_find_vmlinux_btf_id(const char * name,enum bpf_attach_type attach_type)9807*f7c14bbaSAndroid Build Coastguard Worker int libbpf_find_vmlinux_btf_id(const char *name,
9808*f7c14bbaSAndroid Build Coastguard Worker enum bpf_attach_type attach_type)
9809*f7c14bbaSAndroid Build Coastguard Worker {
9810*f7c14bbaSAndroid Build Coastguard Worker struct btf *btf;
9811*f7c14bbaSAndroid Build Coastguard Worker int err;
9812*f7c14bbaSAndroid Build Coastguard Worker
9813*f7c14bbaSAndroid Build Coastguard Worker btf = btf__load_vmlinux_btf();
9814*f7c14bbaSAndroid Build Coastguard Worker err = libbpf_get_error(btf);
9815*f7c14bbaSAndroid Build Coastguard Worker if (err) {
9816*f7c14bbaSAndroid Build Coastguard Worker pr_warn("vmlinux BTF is not found\n");
9817*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(err);
9818*f7c14bbaSAndroid Build Coastguard Worker }
9819*f7c14bbaSAndroid Build Coastguard Worker
9820*f7c14bbaSAndroid Build Coastguard Worker err = find_attach_btf_id(btf, name, attach_type);
9821*f7c14bbaSAndroid Build Coastguard Worker if (err <= 0)
9822*f7c14bbaSAndroid Build Coastguard Worker pr_warn("%s is not found in vmlinux BTF\n", name);
9823*f7c14bbaSAndroid Build Coastguard Worker
9824*f7c14bbaSAndroid Build Coastguard Worker btf__free(btf);
9825*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(err);
9826*f7c14bbaSAndroid Build Coastguard Worker }
9827*f7c14bbaSAndroid Build Coastguard Worker
libbpf_find_prog_btf_id(const char * name,__u32 attach_prog_fd)9828*f7c14bbaSAndroid Build Coastguard Worker static int libbpf_find_prog_btf_id(const char *name, __u32 attach_prog_fd)
9829*f7c14bbaSAndroid Build Coastguard Worker {
9830*f7c14bbaSAndroid Build Coastguard Worker struct bpf_prog_info info;
9831*f7c14bbaSAndroid Build Coastguard Worker __u32 info_len = sizeof(info);
9832*f7c14bbaSAndroid Build Coastguard Worker struct btf *btf;
9833*f7c14bbaSAndroid Build Coastguard Worker int err;
9834*f7c14bbaSAndroid Build Coastguard Worker
9835*f7c14bbaSAndroid Build Coastguard Worker memset(&info, 0, info_len);
9836*f7c14bbaSAndroid Build Coastguard Worker err = bpf_prog_get_info_by_fd(attach_prog_fd, &info, &info_len);
9837*f7c14bbaSAndroid Build Coastguard Worker if (err) {
9838*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed bpf_prog_get_info_by_fd for FD %d: %d\n",
9839*f7c14bbaSAndroid Build Coastguard Worker attach_prog_fd, err);
9840*f7c14bbaSAndroid Build Coastguard Worker return err;
9841*f7c14bbaSAndroid Build Coastguard Worker }
9842*f7c14bbaSAndroid Build Coastguard Worker
9843*f7c14bbaSAndroid Build Coastguard Worker err = -EINVAL;
9844*f7c14bbaSAndroid Build Coastguard Worker if (!info.btf_id) {
9845*f7c14bbaSAndroid Build Coastguard Worker pr_warn("The target program doesn't have BTF\n");
9846*f7c14bbaSAndroid Build Coastguard Worker goto out;
9847*f7c14bbaSAndroid Build Coastguard Worker }
9848*f7c14bbaSAndroid Build Coastguard Worker btf = btf__load_from_kernel_by_id(info.btf_id);
9849*f7c14bbaSAndroid Build Coastguard Worker err = libbpf_get_error(btf);
9850*f7c14bbaSAndroid Build Coastguard Worker if (err) {
9851*f7c14bbaSAndroid Build Coastguard Worker pr_warn("Failed to get BTF %d of the program: %d\n", info.btf_id, err);
9852*f7c14bbaSAndroid Build Coastguard Worker goto out;
9853*f7c14bbaSAndroid Build Coastguard Worker }
9854*f7c14bbaSAndroid Build Coastguard Worker err = btf__find_by_name_kind(btf, name, BTF_KIND_FUNC);
9855*f7c14bbaSAndroid Build Coastguard Worker btf__free(btf);
9856*f7c14bbaSAndroid Build Coastguard Worker if (err <= 0) {
9857*f7c14bbaSAndroid Build Coastguard Worker pr_warn("%s is not found in prog's BTF\n", name);
9858*f7c14bbaSAndroid Build Coastguard Worker goto out;
9859*f7c14bbaSAndroid Build Coastguard Worker }
9860*f7c14bbaSAndroid Build Coastguard Worker out:
9861*f7c14bbaSAndroid Build Coastguard Worker return err;
9862*f7c14bbaSAndroid Build Coastguard Worker }
9863*f7c14bbaSAndroid Build Coastguard Worker
find_kernel_btf_id(struct bpf_object * obj,const char * attach_name,enum bpf_attach_type attach_type,int * btf_obj_fd,int * btf_type_id)9864*f7c14bbaSAndroid Build Coastguard Worker static int find_kernel_btf_id(struct bpf_object *obj, const char *attach_name,
9865*f7c14bbaSAndroid Build Coastguard Worker enum bpf_attach_type attach_type,
9866*f7c14bbaSAndroid Build Coastguard Worker int *btf_obj_fd, int *btf_type_id)
9867*f7c14bbaSAndroid Build Coastguard Worker {
9868*f7c14bbaSAndroid Build Coastguard Worker int ret, i;
9869*f7c14bbaSAndroid Build Coastguard Worker
9870*f7c14bbaSAndroid Build Coastguard Worker ret = find_attach_btf_id(obj->btf_vmlinux, attach_name, attach_type);
9871*f7c14bbaSAndroid Build Coastguard Worker if (ret > 0) {
9872*f7c14bbaSAndroid Build Coastguard Worker *btf_obj_fd = 0; /* vmlinux BTF */
9873*f7c14bbaSAndroid Build Coastguard Worker *btf_type_id = ret;
9874*f7c14bbaSAndroid Build Coastguard Worker return 0;
9875*f7c14bbaSAndroid Build Coastguard Worker }
9876*f7c14bbaSAndroid Build Coastguard Worker if (ret != -ENOENT)
9877*f7c14bbaSAndroid Build Coastguard Worker return ret;
9878*f7c14bbaSAndroid Build Coastguard Worker
9879*f7c14bbaSAndroid Build Coastguard Worker ret = load_module_btfs(obj);
9880*f7c14bbaSAndroid Build Coastguard Worker if (ret)
9881*f7c14bbaSAndroid Build Coastguard Worker return ret;
9882*f7c14bbaSAndroid Build Coastguard Worker
9883*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < obj->btf_module_cnt; i++) {
9884*f7c14bbaSAndroid Build Coastguard Worker const struct module_btf *mod = &obj->btf_modules[i];
9885*f7c14bbaSAndroid Build Coastguard Worker
9886*f7c14bbaSAndroid Build Coastguard Worker ret = find_attach_btf_id(mod->btf, attach_name, attach_type);
9887*f7c14bbaSAndroid Build Coastguard Worker if (ret > 0) {
9888*f7c14bbaSAndroid Build Coastguard Worker *btf_obj_fd = mod->fd;
9889*f7c14bbaSAndroid Build Coastguard Worker *btf_type_id = ret;
9890*f7c14bbaSAndroid Build Coastguard Worker return 0;
9891*f7c14bbaSAndroid Build Coastguard Worker }
9892*f7c14bbaSAndroid Build Coastguard Worker if (ret == -ENOENT)
9893*f7c14bbaSAndroid Build Coastguard Worker continue;
9894*f7c14bbaSAndroid Build Coastguard Worker
9895*f7c14bbaSAndroid Build Coastguard Worker return ret;
9896*f7c14bbaSAndroid Build Coastguard Worker }
9897*f7c14bbaSAndroid Build Coastguard Worker
9898*f7c14bbaSAndroid Build Coastguard Worker return -ESRCH;
9899*f7c14bbaSAndroid Build Coastguard Worker }
9900*f7c14bbaSAndroid Build Coastguard Worker
libbpf_find_attach_btf_id(struct bpf_program * prog,const char * attach_name,int * btf_obj_fd,int * btf_type_id)9901*f7c14bbaSAndroid Build Coastguard Worker static int libbpf_find_attach_btf_id(struct bpf_program *prog, const char *attach_name,
9902*f7c14bbaSAndroid Build Coastguard Worker int *btf_obj_fd, int *btf_type_id)
9903*f7c14bbaSAndroid Build Coastguard Worker {
9904*f7c14bbaSAndroid Build Coastguard Worker enum bpf_attach_type attach_type = prog->expected_attach_type;
9905*f7c14bbaSAndroid Build Coastguard Worker __u32 attach_prog_fd = prog->attach_prog_fd;
9906*f7c14bbaSAndroid Build Coastguard Worker int err = 0;
9907*f7c14bbaSAndroid Build Coastguard Worker
9908*f7c14bbaSAndroid Build Coastguard Worker /* BPF program's BTF ID */
9909*f7c14bbaSAndroid Build Coastguard Worker if (prog->type == BPF_PROG_TYPE_EXT || attach_prog_fd) {
9910*f7c14bbaSAndroid Build Coastguard Worker if (!attach_prog_fd) {
9911*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': attach program FD is not set\n", prog->name);
9912*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
9913*f7c14bbaSAndroid Build Coastguard Worker }
9914*f7c14bbaSAndroid Build Coastguard Worker err = libbpf_find_prog_btf_id(attach_name, attach_prog_fd);
9915*f7c14bbaSAndroid Build Coastguard Worker if (err < 0) {
9916*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': failed to find BPF program (FD %d) BTF ID for '%s': %d\n",
9917*f7c14bbaSAndroid Build Coastguard Worker prog->name, attach_prog_fd, attach_name, err);
9918*f7c14bbaSAndroid Build Coastguard Worker return err;
9919*f7c14bbaSAndroid Build Coastguard Worker }
9920*f7c14bbaSAndroid Build Coastguard Worker *btf_obj_fd = 0;
9921*f7c14bbaSAndroid Build Coastguard Worker *btf_type_id = err;
9922*f7c14bbaSAndroid Build Coastguard Worker return 0;
9923*f7c14bbaSAndroid Build Coastguard Worker }
9924*f7c14bbaSAndroid Build Coastguard Worker
9925*f7c14bbaSAndroid Build Coastguard Worker /* kernel/module BTF ID */
9926*f7c14bbaSAndroid Build Coastguard Worker if (prog->obj->gen_loader) {
9927*f7c14bbaSAndroid Build Coastguard Worker bpf_gen__record_attach_target(prog->obj->gen_loader, attach_name, attach_type);
9928*f7c14bbaSAndroid Build Coastguard Worker *btf_obj_fd = 0;
9929*f7c14bbaSAndroid Build Coastguard Worker *btf_type_id = 1;
9930*f7c14bbaSAndroid Build Coastguard Worker } else {
9931*f7c14bbaSAndroid Build Coastguard Worker err = find_kernel_btf_id(prog->obj, attach_name,
9932*f7c14bbaSAndroid Build Coastguard Worker attach_type, btf_obj_fd,
9933*f7c14bbaSAndroid Build Coastguard Worker btf_type_id);
9934*f7c14bbaSAndroid Build Coastguard Worker }
9935*f7c14bbaSAndroid Build Coastguard Worker if (err) {
9936*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': failed to find kernel BTF type ID of '%s': %d\n",
9937*f7c14bbaSAndroid Build Coastguard Worker prog->name, attach_name, err);
9938*f7c14bbaSAndroid Build Coastguard Worker return err;
9939*f7c14bbaSAndroid Build Coastguard Worker }
9940*f7c14bbaSAndroid Build Coastguard Worker return 0;
9941*f7c14bbaSAndroid Build Coastguard Worker }
9942*f7c14bbaSAndroid Build Coastguard Worker
libbpf_attach_type_by_name(const char * name,enum bpf_attach_type * attach_type)9943*f7c14bbaSAndroid Build Coastguard Worker int libbpf_attach_type_by_name(const char *name,
9944*f7c14bbaSAndroid Build Coastguard Worker enum bpf_attach_type *attach_type)
9945*f7c14bbaSAndroid Build Coastguard Worker {
9946*f7c14bbaSAndroid Build Coastguard Worker char *type_names;
9947*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_sec_def *sec_def;
9948*f7c14bbaSAndroid Build Coastguard Worker
9949*f7c14bbaSAndroid Build Coastguard Worker if (!name)
9950*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-EINVAL);
9951*f7c14bbaSAndroid Build Coastguard Worker
9952*f7c14bbaSAndroid Build Coastguard Worker sec_def = find_sec_def(name);
9953*f7c14bbaSAndroid Build Coastguard Worker if (!sec_def) {
9954*f7c14bbaSAndroid Build Coastguard Worker pr_debug("failed to guess attach type based on ELF section name '%s'\n", name);
9955*f7c14bbaSAndroid Build Coastguard Worker type_names = libbpf_get_type_names(true);
9956*f7c14bbaSAndroid Build Coastguard Worker if (type_names != NULL) {
9957*f7c14bbaSAndroid Build Coastguard Worker pr_debug("attachable section(type) names are:%s\n", type_names);
9958*f7c14bbaSAndroid Build Coastguard Worker free(type_names);
9959*f7c14bbaSAndroid Build Coastguard Worker }
9960*f7c14bbaSAndroid Build Coastguard Worker
9961*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-EINVAL);
9962*f7c14bbaSAndroid Build Coastguard Worker }
9963*f7c14bbaSAndroid Build Coastguard Worker
9964*f7c14bbaSAndroid Build Coastguard Worker if (sec_def->prog_prepare_load_fn != libbpf_prepare_prog_load)
9965*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-EINVAL);
9966*f7c14bbaSAndroid Build Coastguard Worker if (!(sec_def->cookie & SEC_ATTACHABLE))
9967*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-EINVAL);
9968*f7c14bbaSAndroid Build Coastguard Worker
9969*f7c14bbaSAndroid Build Coastguard Worker *attach_type = sec_def->expected_attach_type;
9970*f7c14bbaSAndroid Build Coastguard Worker return 0;
9971*f7c14bbaSAndroid Build Coastguard Worker }
9972*f7c14bbaSAndroid Build Coastguard Worker
bpf_map__fd(const struct bpf_map * map)9973*f7c14bbaSAndroid Build Coastguard Worker int bpf_map__fd(const struct bpf_map *map)
9974*f7c14bbaSAndroid Build Coastguard Worker {
9975*f7c14bbaSAndroid Build Coastguard Worker if (!map)
9976*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-EINVAL);
9977*f7c14bbaSAndroid Build Coastguard Worker if (!map_is_created(map))
9978*f7c14bbaSAndroid Build Coastguard Worker return -1;
9979*f7c14bbaSAndroid Build Coastguard Worker return map->fd;
9980*f7c14bbaSAndroid Build Coastguard Worker }
9981*f7c14bbaSAndroid Build Coastguard Worker
map_uses_real_name(const struct bpf_map * map)9982*f7c14bbaSAndroid Build Coastguard Worker static bool map_uses_real_name(const struct bpf_map *map)
9983*f7c14bbaSAndroid Build Coastguard Worker {
9984*f7c14bbaSAndroid Build Coastguard Worker /* Since libbpf started to support custom .data.* and .rodata.* maps,
9985*f7c14bbaSAndroid Build Coastguard Worker * their user-visible name differs from kernel-visible name. Users see
9986*f7c14bbaSAndroid Build Coastguard Worker * such map's corresponding ELF section name as a map name.
9987*f7c14bbaSAndroid Build Coastguard Worker * This check distinguishes .data/.rodata from .data.* and .rodata.*
9988*f7c14bbaSAndroid Build Coastguard Worker * maps to know which name has to be returned to the user.
9989*f7c14bbaSAndroid Build Coastguard Worker */
9990*f7c14bbaSAndroid Build Coastguard Worker if (map->libbpf_type == LIBBPF_MAP_DATA && strcmp(map->real_name, DATA_SEC) != 0)
9991*f7c14bbaSAndroid Build Coastguard Worker return true;
9992*f7c14bbaSAndroid Build Coastguard Worker if (map->libbpf_type == LIBBPF_MAP_RODATA && strcmp(map->real_name, RODATA_SEC) != 0)
9993*f7c14bbaSAndroid Build Coastguard Worker return true;
9994*f7c14bbaSAndroid Build Coastguard Worker return false;
9995*f7c14bbaSAndroid Build Coastguard Worker }
9996*f7c14bbaSAndroid Build Coastguard Worker
bpf_map__name(const struct bpf_map * map)9997*f7c14bbaSAndroid Build Coastguard Worker const char *bpf_map__name(const struct bpf_map *map)
9998*f7c14bbaSAndroid Build Coastguard Worker {
9999*f7c14bbaSAndroid Build Coastguard Worker if (!map)
10000*f7c14bbaSAndroid Build Coastguard Worker return NULL;
10001*f7c14bbaSAndroid Build Coastguard Worker
10002*f7c14bbaSAndroid Build Coastguard Worker if (map_uses_real_name(map))
10003*f7c14bbaSAndroid Build Coastguard Worker return map->real_name;
10004*f7c14bbaSAndroid Build Coastguard Worker
10005*f7c14bbaSAndroid Build Coastguard Worker return map->name;
10006*f7c14bbaSAndroid Build Coastguard Worker }
10007*f7c14bbaSAndroid Build Coastguard Worker
bpf_map__type(const struct bpf_map * map)10008*f7c14bbaSAndroid Build Coastguard Worker enum bpf_map_type bpf_map__type(const struct bpf_map *map)
10009*f7c14bbaSAndroid Build Coastguard Worker {
10010*f7c14bbaSAndroid Build Coastguard Worker return map->def.type;
10011*f7c14bbaSAndroid Build Coastguard Worker }
10012*f7c14bbaSAndroid Build Coastguard Worker
bpf_map__set_type(struct bpf_map * map,enum bpf_map_type type)10013*f7c14bbaSAndroid Build Coastguard Worker int bpf_map__set_type(struct bpf_map *map, enum bpf_map_type type)
10014*f7c14bbaSAndroid Build Coastguard Worker {
10015*f7c14bbaSAndroid Build Coastguard Worker if (map_is_created(map))
10016*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-EBUSY);
10017*f7c14bbaSAndroid Build Coastguard Worker map->def.type = type;
10018*f7c14bbaSAndroid Build Coastguard Worker return 0;
10019*f7c14bbaSAndroid Build Coastguard Worker }
10020*f7c14bbaSAndroid Build Coastguard Worker
bpf_map__map_flags(const struct bpf_map * map)10021*f7c14bbaSAndroid Build Coastguard Worker __u32 bpf_map__map_flags(const struct bpf_map *map)
10022*f7c14bbaSAndroid Build Coastguard Worker {
10023*f7c14bbaSAndroid Build Coastguard Worker return map->def.map_flags;
10024*f7c14bbaSAndroid Build Coastguard Worker }
10025*f7c14bbaSAndroid Build Coastguard Worker
bpf_map__set_map_flags(struct bpf_map * map,__u32 flags)10026*f7c14bbaSAndroid Build Coastguard Worker int bpf_map__set_map_flags(struct bpf_map *map, __u32 flags)
10027*f7c14bbaSAndroid Build Coastguard Worker {
10028*f7c14bbaSAndroid Build Coastguard Worker if (map_is_created(map))
10029*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-EBUSY);
10030*f7c14bbaSAndroid Build Coastguard Worker map->def.map_flags = flags;
10031*f7c14bbaSAndroid Build Coastguard Worker return 0;
10032*f7c14bbaSAndroid Build Coastguard Worker }
10033*f7c14bbaSAndroid Build Coastguard Worker
bpf_map__map_extra(const struct bpf_map * map)10034*f7c14bbaSAndroid Build Coastguard Worker __u64 bpf_map__map_extra(const struct bpf_map *map)
10035*f7c14bbaSAndroid Build Coastguard Worker {
10036*f7c14bbaSAndroid Build Coastguard Worker return map->map_extra;
10037*f7c14bbaSAndroid Build Coastguard Worker }
10038*f7c14bbaSAndroid Build Coastguard Worker
bpf_map__set_map_extra(struct bpf_map * map,__u64 map_extra)10039*f7c14bbaSAndroid Build Coastguard Worker int bpf_map__set_map_extra(struct bpf_map *map, __u64 map_extra)
10040*f7c14bbaSAndroid Build Coastguard Worker {
10041*f7c14bbaSAndroid Build Coastguard Worker if (map_is_created(map))
10042*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-EBUSY);
10043*f7c14bbaSAndroid Build Coastguard Worker map->map_extra = map_extra;
10044*f7c14bbaSAndroid Build Coastguard Worker return 0;
10045*f7c14bbaSAndroid Build Coastguard Worker }
10046*f7c14bbaSAndroid Build Coastguard Worker
bpf_map__numa_node(const struct bpf_map * map)10047*f7c14bbaSAndroid Build Coastguard Worker __u32 bpf_map__numa_node(const struct bpf_map *map)
10048*f7c14bbaSAndroid Build Coastguard Worker {
10049*f7c14bbaSAndroid Build Coastguard Worker return map->numa_node;
10050*f7c14bbaSAndroid Build Coastguard Worker }
10051*f7c14bbaSAndroid Build Coastguard Worker
bpf_map__set_numa_node(struct bpf_map * map,__u32 numa_node)10052*f7c14bbaSAndroid Build Coastguard Worker int bpf_map__set_numa_node(struct bpf_map *map, __u32 numa_node)
10053*f7c14bbaSAndroid Build Coastguard Worker {
10054*f7c14bbaSAndroid Build Coastguard Worker if (map_is_created(map))
10055*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-EBUSY);
10056*f7c14bbaSAndroid Build Coastguard Worker map->numa_node = numa_node;
10057*f7c14bbaSAndroid Build Coastguard Worker return 0;
10058*f7c14bbaSAndroid Build Coastguard Worker }
10059*f7c14bbaSAndroid Build Coastguard Worker
bpf_map__key_size(const struct bpf_map * map)10060*f7c14bbaSAndroid Build Coastguard Worker __u32 bpf_map__key_size(const struct bpf_map *map)
10061*f7c14bbaSAndroid Build Coastguard Worker {
10062*f7c14bbaSAndroid Build Coastguard Worker return map->def.key_size;
10063*f7c14bbaSAndroid Build Coastguard Worker }
10064*f7c14bbaSAndroid Build Coastguard Worker
bpf_map__set_key_size(struct bpf_map * map,__u32 size)10065*f7c14bbaSAndroid Build Coastguard Worker int bpf_map__set_key_size(struct bpf_map *map, __u32 size)
10066*f7c14bbaSAndroid Build Coastguard Worker {
10067*f7c14bbaSAndroid Build Coastguard Worker if (map_is_created(map))
10068*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-EBUSY);
10069*f7c14bbaSAndroid Build Coastguard Worker map->def.key_size = size;
10070*f7c14bbaSAndroid Build Coastguard Worker return 0;
10071*f7c14bbaSAndroid Build Coastguard Worker }
10072*f7c14bbaSAndroid Build Coastguard Worker
bpf_map__value_size(const struct bpf_map * map)10073*f7c14bbaSAndroid Build Coastguard Worker __u32 bpf_map__value_size(const struct bpf_map *map)
10074*f7c14bbaSAndroid Build Coastguard Worker {
10075*f7c14bbaSAndroid Build Coastguard Worker return map->def.value_size;
10076*f7c14bbaSAndroid Build Coastguard Worker }
10077*f7c14bbaSAndroid Build Coastguard Worker
map_btf_datasec_resize(struct bpf_map * map,__u32 size)10078*f7c14bbaSAndroid Build Coastguard Worker static int map_btf_datasec_resize(struct bpf_map *map, __u32 size)
10079*f7c14bbaSAndroid Build Coastguard Worker {
10080*f7c14bbaSAndroid Build Coastguard Worker struct btf *btf;
10081*f7c14bbaSAndroid Build Coastguard Worker struct btf_type *datasec_type, *var_type;
10082*f7c14bbaSAndroid Build Coastguard Worker struct btf_var_secinfo *var;
10083*f7c14bbaSAndroid Build Coastguard Worker const struct btf_type *array_type;
10084*f7c14bbaSAndroid Build Coastguard Worker const struct btf_array *array;
10085*f7c14bbaSAndroid Build Coastguard Worker int vlen, element_sz, new_array_id;
10086*f7c14bbaSAndroid Build Coastguard Worker __u32 nr_elements;
10087*f7c14bbaSAndroid Build Coastguard Worker
10088*f7c14bbaSAndroid Build Coastguard Worker /* check btf existence */
10089*f7c14bbaSAndroid Build Coastguard Worker btf = bpf_object__btf(map->obj);
10090*f7c14bbaSAndroid Build Coastguard Worker if (!btf)
10091*f7c14bbaSAndroid Build Coastguard Worker return -ENOENT;
10092*f7c14bbaSAndroid Build Coastguard Worker
10093*f7c14bbaSAndroid Build Coastguard Worker /* verify map is datasec */
10094*f7c14bbaSAndroid Build Coastguard Worker datasec_type = btf_type_by_id(btf, bpf_map__btf_value_type_id(map));
10095*f7c14bbaSAndroid Build Coastguard Worker if (!btf_is_datasec(datasec_type)) {
10096*f7c14bbaSAndroid Build Coastguard Worker pr_warn("map '%s': cannot be resized, map value type is not a datasec\n",
10097*f7c14bbaSAndroid Build Coastguard Worker bpf_map__name(map));
10098*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
10099*f7c14bbaSAndroid Build Coastguard Worker }
10100*f7c14bbaSAndroid Build Coastguard Worker
10101*f7c14bbaSAndroid Build Coastguard Worker /* verify datasec has at least one var */
10102*f7c14bbaSAndroid Build Coastguard Worker vlen = btf_vlen(datasec_type);
10103*f7c14bbaSAndroid Build Coastguard Worker if (vlen == 0) {
10104*f7c14bbaSAndroid Build Coastguard Worker pr_warn("map '%s': cannot be resized, map value datasec is empty\n",
10105*f7c14bbaSAndroid Build Coastguard Worker bpf_map__name(map));
10106*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
10107*f7c14bbaSAndroid Build Coastguard Worker }
10108*f7c14bbaSAndroid Build Coastguard Worker
10109*f7c14bbaSAndroid Build Coastguard Worker /* verify last var in the datasec is an array */
10110*f7c14bbaSAndroid Build Coastguard Worker var = &btf_var_secinfos(datasec_type)[vlen - 1];
10111*f7c14bbaSAndroid Build Coastguard Worker var_type = btf_type_by_id(btf, var->type);
10112*f7c14bbaSAndroid Build Coastguard Worker array_type = skip_mods_and_typedefs(btf, var_type->type, NULL);
10113*f7c14bbaSAndroid Build Coastguard Worker if (!btf_is_array(array_type)) {
10114*f7c14bbaSAndroid Build Coastguard Worker pr_warn("map '%s': cannot be resized, last var must be an array\n",
10115*f7c14bbaSAndroid Build Coastguard Worker bpf_map__name(map));
10116*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
10117*f7c14bbaSAndroid Build Coastguard Worker }
10118*f7c14bbaSAndroid Build Coastguard Worker
10119*f7c14bbaSAndroid Build Coastguard Worker /* verify request size aligns with array */
10120*f7c14bbaSAndroid Build Coastguard Worker array = btf_array(array_type);
10121*f7c14bbaSAndroid Build Coastguard Worker element_sz = btf__resolve_size(btf, array->type);
10122*f7c14bbaSAndroid Build Coastguard Worker if (element_sz <= 0 || (size - var->offset) % element_sz != 0) {
10123*f7c14bbaSAndroid Build Coastguard Worker pr_warn("map '%s': cannot be resized, element size (%d) doesn't align with new total size (%u)\n",
10124*f7c14bbaSAndroid Build Coastguard Worker bpf_map__name(map), element_sz, size);
10125*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
10126*f7c14bbaSAndroid Build Coastguard Worker }
10127*f7c14bbaSAndroid Build Coastguard Worker
10128*f7c14bbaSAndroid Build Coastguard Worker /* create a new array based on the existing array, but with new length */
10129*f7c14bbaSAndroid Build Coastguard Worker nr_elements = (size - var->offset) / element_sz;
10130*f7c14bbaSAndroid Build Coastguard Worker new_array_id = btf__add_array(btf, array->index_type, array->type, nr_elements);
10131*f7c14bbaSAndroid Build Coastguard Worker if (new_array_id < 0)
10132*f7c14bbaSAndroid Build Coastguard Worker return new_array_id;
10133*f7c14bbaSAndroid Build Coastguard Worker
10134*f7c14bbaSAndroid Build Coastguard Worker /* adding a new btf type invalidates existing pointers to btf objects,
10135*f7c14bbaSAndroid Build Coastguard Worker * so refresh pointers before proceeding
10136*f7c14bbaSAndroid Build Coastguard Worker */
10137*f7c14bbaSAndroid Build Coastguard Worker datasec_type = btf_type_by_id(btf, map->btf_value_type_id);
10138*f7c14bbaSAndroid Build Coastguard Worker var = &btf_var_secinfos(datasec_type)[vlen - 1];
10139*f7c14bbaSAndroid Build Coastguard Worker var_type = btf_type_by_id(btf, var->type);
10140*f7c14bbaSAndroid Build Coastguard Worker
10141*f7c14bbaSAndroid Build Coastguard Worker /* finally update btf info */
10142*f7c14bbaSAndroid Build Coastguard Worker datasec_type->size = size;
10143*f7c14bbaSAndroid Build Coastguard Worker var->size = size - var->offset;
10144*f7c14bbaSAndroid Build Coastguard Worker var_type->type = new_array_id;
10145*f7c14bbaSAndroid Build Coastguard Worker
10146*f7c14bbaSAndroid Build Coastguard Worker return 0;
10147*f7c14bbaSAndroid Build Coastguard Worker }
10148*f7c14bbaSAndroid Build Coastguard Worker
bpf_map__set_value_size(struct bpf_map * map,__u32 size)10149*f7c14bbaSAndroid Build Coastguard Worker int bpf_map__set_value_size(struct bpf_map *map, __u32 size)
10150*f7c14bbaSAndroid Build Coastguard Worker {
10151*f7c14bbaSAndroid Build Coastguard Worker if (map->obj->loaded || map->reused)
10152*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-EBUSY);
10153*f7c14bbaSAndroid Build Coastguard Worker
10154*f7c14bbaSAndroid Build Coastguard Worker if (map->mmaped) {
10155*f7c14bbaSAndroid Build Coastguard Worker size_t mmap_old_sz, mmap_new_sz;
10156*f7c14bbaSAndroid Build Coastguard Worker int err;
10157*f7c14bbaSAndroid Build Coastguard Worker
10158*f7c14bbaSAndroid Build Coastguard Worker if (map->def.type != BPF_MAP_TYPE_ARRAY)
10159*f7c14bbaSAndroid Build Coastguard Worker return -EOPNOTSUPP;
10160*f7c14bbaSAndroid Build Coastguard Worker
10161*f7c14bbaSAndroid Build Coastguard Worker mmap_old_sz = bpf_map_mmap_sz(map);
10162*f7c14bbaSAndroid Build Coastguard Worker mmap_new_sz = array_map_mmap_sz(size, map->def.max_entries);
10163*f7c14bbaSAndroid Build Coastguard Worker err = bpf_map_mmap_resize(map, mmap_old_sz, mmap_new_sz);
10164*f7c14bbaSAndroid Build Coastguard Worker if (err) {
10165*f7c14bbaSAndroid Build Coastguard Worker pr_warn("map '%s': failed to resize memory-mapped region: %d\n",
10166*f7c14bbaSAndroid Build Coastguard Worker bpf_map__name(map), err);
10167*f7c14bbaSAndroid Build Coastguard Worker return err;
10168*f7c14bbaSAndroid Build Coastguard Worker }
10169*f7c14bbaSAndroid Build Coastguard Worker err = map_btf_datasec_resize(map, size);
10170*f7c14bbaSAndroid Build Coastguard Worker if (err && err != -ENOENT) {
10171*f7c14bbaSAndroid Build Coastguard Worker pr_warn("map '%s': failed to adjust resized BTF, clearing BTF key/value info: %d\n",
10172*f7c14bbaSAndroid Build Coastguard Worker bpf_map__name(map), err);
10173*f7c14bbaSAndroid Build Coastguard Worker map->btf_value_type_id = 0;
10174*f7c14bbaSAndroid Build Coastguard Worker map->btf_key_type_id = 0;
10175*f7c14bbaSAndroid Build Coastguard Worker }
10176*f7c14bbaSAndroid Build Coastguard Worker }
10177*f7c14bbaSAndroid Build Coastguard Worker
10178*f7c14bbaSAndroid Build Coastguard Worker map->def.value_size = size;
10179*f7c14bbaSAndroid Build Coastguard Worker return 0;
10180*f7c14bbaSAndroid Build Coastguard Worker }
10181*f7c14bbaSAndroid Build Coastguard Worker
bpf_map__btf_key_type_id(const struct bpf_map * map)10182*f7c14bbaSAndroid Build Coastguard Worker __u32 bpf_map__btf_key_type_id(const struct bpf_map *map)
10183*f7c14bbaSAndroid Build Coastguard Worker {
10184*f7c14bbaSAndroid Build Coastguard Worker return map ? map->btf_key_type_id : 0;
10185*f7c14bbaSAndroid Build Coastguard Worker }
10186*f7c14bbaSAndroid Build Coastguard Worker
bpf_map__btf_value_type_id(const struct bpf_map * map)10187*f7c14bbaSAndroid Build Coastguard Worker __u32 bpf_map__btf_value_type_id(const struct bpf_map *map)
10188*f7c14bbaSAndroid Build Coastguard Worker {
10189*f7c14bbaSAndroid Build Coastguard Worker return map ? map->btf_value_type_id : 0;
10190*f7c14bbaSAndroid Build Coastguard Worker }
10191*f7c14bbaSAndroid Build Coastguard Worker
bpf_map__set_initial_value(struct bpf_map * map,const void * data,size_t size)10192*f7c14bbaSAndroid Build Coastguard Worker int bpf_map__set_initial_value(struct bpf_map *map,
10193*f7c14bbaSAndroid Build Coastguard Worker const void *data, size_t size)
10194*f7c14bbaSAndroid Build Coastguard Worker {
10195*f7c14bbaSAndroid Build Coastguard Worker size_t actual_sz;
10196*f7c14bbaSAndroid Build Coastguard Worker
10197*f7c14bbaSAndroid Build Coastguard Worker if (map->obj->loaded || map->reused)
10198*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-EBUSY);
10199*f7c14bbaSAndroid Build Coastguard Worker
10200*f7c14bbaSAndroid Build Coastguard Worker if (!map->mmaped || map->libbpf_type == LIBBPF_MAP_KCONFIG)
10201*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-EINVAL);
10202*f7c14bbaSAndroid Build Coastguard Worker
10203*f7c14bbaSAndroid Build Coastguard Worker if (map->def.type == BPF_MAP_TYPE_ARENA)
10204*f7c14bbaSAndroid Build Coastguard Worker actual_sz = map->obj->arena_data_sz;
10205*f7c14bbaSAndroid Build Coastguard Worker else
10206*f7c14bbaSAndroid Build Coastguard Worker actual_sz = map->def.value_size;
10207*f7c14bbaSAndroid Build Coastguard Worker if (size != actual_sz)
10208*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-EINVAL);
10209*f7c14bbaSAndroid Build Coastguard Worker
10210*f7c14bbaSAndroid Build Coastguard Worker memcpy(map->mmaped, data, size);
10211*f7c14bbaSAndroid Build Coastguard Worker return 0;
10212*f7c14bbaSAndroid Build Coastguard Worker }
10213*f7c14bbaSAndroid Build Coastguard Worker
bpf_map__initial_value(const struct bpf_map * map,size_t * psize)10214*f7c14bbaSAndroid Build Coastguard Worker void *bpf_map__initial_value(const struct bpf_map *map, size_t *psize)
10215*f7c14bbaSAndroid Build Coastguard Worker {
10216*f7c14bbaSAndroid Build Coastguard Worker if (bpf_map__is_struct_ops(map)) {
10217*f7c14bbaSAndroid Build Coastguard Worker if (psize)
10218*f7c14bbaSAndroid Build Coastguard Worker *psize = map->def.value_size;
10219*f7c14bbaSAndroid Build Coastguard Worker return map->st_ops->data;
10220*f7c14bbaSAndroid Build Coastguard Worker }
10221*f7c14bbaSAndroid Build Coastguard Worker
10222*f7c14bbaSAndroid Build Coastguard Worker if (!map->mmaped)
10223*f7c14bbaSAndroid Build Coastguard Worker return NULL;
10224*f7c14bbaSAndroid Build Coastguard Worker
10225*f7c14bbaSAndroid Build Coastguard Worker if (map->def.type == BPF_MAP_TYPE_ARENA)
10226*f7c14bbaSAndroid Build Coastguard Worker *psize = map->obj->arena_data_sz;
10227*f7c14bbaSAndroid Build Coastguard Worker else
10228*f7c14bbaSAndroid Build Coastguard Worker *psize = map->def.value_size;
10229*f7c14bbaSAndroid Build Coastguard Worker
10230*f7c14bbaSAndroid Build Coastguard Worker return map->mmaped;
10231*f7c14bbaSAndroid Build Coastguard Worker }
10232*f7c14bbaSAndroid Build Coastguard Worker
bpf_map__is_internal(const struct bpf_map * map)10233*f7c14bbaSAndroid Build Coastguard Worker bool bpf_map__is_internal(const struct bpf_map *map)
10234*f7c14bbaSAndroid Build Coastguard Worker {
10235*f7c14bbaSAndroid Build Coastguard Worker return map->libbpf_type != LIBBPF_MAP_UNSPEC;
10236*f7c14bbaSAndroid Build Coastguard Worker }
10237*f7c14bbaSAndroid Build Coastguard Worker
bpf_map__ifindex(const struct bpf_map * map)10238*f7c14bbaSAndroid Build Coastguard Worker __u32 bpf_map__ifindex(const struct bpf_map *map)
10239*f7c14bbaSAndroid Build Coastguard Worker {
10240*f7c14bbaSAndroid Build Coastguard Worker return map->map_ifindex;
10241*f7c14bbaSAndroid Build Coastguard Worker }
10242*f7c14bbaSAndroid Build Coastguard Worker
bpf_map__set_ifindex(struct bpf_map * map,__u32 ifindex)10243*f7c14bbaSAndroid Build Coastguard Worker int bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex)
10244*f7c14bbaSAndroid Build Coastguard Worker {
10245*f7c14bbaSAndroid Build Coastguard Worker if (map_is_created(map))
10246*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-EBUSY);
10247*f7c14bbaSAndroid Build Coastguard Worker map->map_ifindex = ifindex;
10248*f7c14bbaSAndroid Build Coastguard Worker return 0;
10249*f7c14bbaSAndroid Build Coastguard Worker }
10250*f7c14bbaSAndroid Build Coastguard Worker
bpf_map__set_inner_map_fd(struct bpf_map * map,int fd)10251*f7c14bbaSAndroid Build Coastguard Worker int bpf_map__set_inner_map_fd(struct bpf_map *map, int fd)
10252*f7c14bbaSAndroid Build Coastguard Worker {
10253*f7c14bbaSAndroid Build Coastguard Worker if (!bpf_map_type__is_map_in_map(map->def.type)) {
10254*f7c14bbaSAndroid Build Coastguard Worker pr_warn("error: unsupported map type\n");
10255*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-EINVAL);
10256*f7c14bbaSAndroid Build Coastguard Worker }
10257*f7c14bbaSAndroid Build Coastguard Worker if (map->inner_map_fd != -1) {
10258*f7c14bbaSAndroid Build Coastguard Worker pr_warn("error: inner_map_fd already specified\n");
10259*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-EINVAL);
10260*f7c14bbaSAndroid Build Coastguard Worker }
10261*f7c14bbaSAndroid Build Coastguard Worker if (map->inner_map) {
10262*f7c14bbaSAndroid Build Coastguard Worker bpf_map__destroy(map->inner_map);
10263*f7c14bbaSAndroid Build Coastguard Worker zfree(&map->inner_map);
10264*f7c14bbaSAndroid Build Coastguard Worker }
10265*f7c14bbaSAndroid Build Coastguard Worker map->inner_map_fd = fd;
10266*f7c14bbaSAndroid Build Coastguard Worker return 0;
10267*f7c14bbaSAndroid Build Coastguard Worker }
10268*f7c14bbaSAndroid Build Coastguard Worker
10269*f7c14bbaSAndroid Build Coastguard Worker static struct bpf_map *
__bpf_map__iter(const struct bpf_map * m,const struct bpf_object * obj,int i)10270*f7c14bbaSAndroid Build Coastguard Worker __bpf_map__iter(const struct bpf_map *m, const struct bpf_object *obj, int i)
10271*f7c14bbaSAndroid Build Coastguard Worker {
10272*f7c14bbaSAndroid Build Coastguard Worker ssize_t idx;
10273*f7c14bbaSAndroid Build Coastguard Worker struct bpf_map *s, *e;
10274*f7c14bbaSAndroid Build Coastguard Worker
10275*f7c14bbaSAndroid Build Coastguard Worker if (!obj || !obj->maps)
10276*f7c14bbaSAndroid Build Coastguard Worker return errno = EINVAL, NULL;
10277*f7c14bbaSAndroid Build Coastguard Worker
10278*f7c14bbaSAndroid Build Coastguard Worker s = obj->maps;
10279*f7c14bbaSAndroid Build Coastguard Worker e = obj->maps + obj->nr_maps;
10280*f7c14bbaSAndroid Build Coastguard Worker
10281*f7c14bbaSAndroid Build Coastguard Worker if ((m < s) || (m >= e)) {
10282*f7c14bbaSAndroid Build Coastguard Worker pr_warn("error in %s: map handler doesn't belong to object\n",
10283*f7c14bbaSAndroid Build Coastguard Worker __func__);
10284*f7c14bbaSAndroid Build Coastguard Worker return errno = EINVAL, NULL;
10285*f7c14bbaSAndroid Build Coastguard Worker }
10286*f7c14bbaSAndroid Build Coastguard Worker
10287*f7c14bbaSAndroid Build Coastguard Worker idx = (m - obj->maps) + i;
10288*f7c14bbaSAndroid Build Coastguard Worker if (idx >= obj->nr_maps || idx < 0)
10289*f7c14bbaSAndroid Build Coastguard Worker return NULL;
10290*f7c14bbaSAndroid Build Coastguard Worker return &obj->maps[idx];
10291*f7c14bbaSAndroid Build Coastguard Worker }
10292*f7c14bbaSAndroid Build Coastguard Worker
10293*f7c14bbaSAndroid Build Coastguard Worker struct bpf_map *
bpf_object__next_map(const struct bpf_object * obj,const struct bpf_map * prev)10294*f7c14bbaSAndroid Build Coastguard Worker bpf_object__next_map(const struct bpf_object *obj, const struct bpf_map *prev)
10295*f7c14bbaSAndroid Build Coastguard Worker {
10296*f7c14bbaSAndroid Build Coastguard Worker if (prev == NULL)
10297*f7c14bbaSAndroid Build Coastguard Worker return obj->maps;
10298*f7c14bbaSAndroid Build Coastguard Worker
10299*f7c14bbaSAndroid Build Coastguard Worker return __bpf_map__iter(prev, obj, 1);
10300*f7c14bbaSAndroid Build Coastguard Worker }
10301*f7c14bbaSAndroid Build Coastguard Worker
10302*f7c14bbaSAndroid Build Coastguard Worker struct bpf_map *
bpf_object__prev_map(const struct bpf_object * obj,const struct bpf_map * next)10303*f7c14bbaSAndroid Build Coastguard Worker bpf_object__prev_map(const struct bpf_object *obj, const struct bpf_map *next)
10304*f7c14bbaSAndroid Build Coastguard Worker {
10305*f7c14bbaSAndroid Build Coastguard Worker if (next == NULL) {
10306*f7c14bbaSAndroid Build Coastguard Worker if (!obj->nr_maps)
10307*f7c14bbaSAndroid Build Coastguard Worker return NULL;
10308*f7c14bbaSAndroid Build Coastguard Worker return obj->maps + obj->nr_maps - 1;
10309*f7c14bbaSAndroid Build Coastguard Worker }
10310*f7c14bbaSAndroid Build Coastguard Worker
10311*f7c14bbaSAndroid Build Coastguard Worker return __bpf_map__iter(next, obj, -1);
10312*f7c14bbaSAndroid Build Coastguard Worker }
10313*f7c14bbaSAndroid Build Coastguard Worker
10314*f7c14bbaSAndroid Build Coastguard Worker struct bpf_map *
bpf_object__find_map_by_name(const struct bpf_object * obj,const char * name)10315*f7c14bbaSAndroid Build Coastguard Worker bpf_object__find_map_by_name(const struct bpf_object *obj, const char *name)
10316*f7c14bbaSAndroid Build Coastguard Worker {
10317*f7c14bbaSAndroid Build Coastguard Worker struct bpf_map *pos;
10318*f7c14bbaSAndroid Build Coastguard Worker
10319*f7c14bbaSAndroid Build Coastguard Worker bpf_object__for_each_map(pos, obj) {
10320*f7c14bbaSAndroid Build Coastguard Worker /* if it's a special internal map name (which always starts
10321*f7c14bbaSAndroid Build Coastguard Worker * with dot) then check if that special name matches the
10322*f7c14bbaSAndroid Build Coastguard Worker * real map name (ELF section name)
10323*f7c14bbaSAndroid Build Coastguard Worker */
10324*f7c14bbaSAndroid Build Coastguard Worker if (name[0] == '.') {
10325*f7c14bbaSAndroid Build Coastguard Worker if (pos->real_name && strcmp(pos->real_name, name) == 0)
10326*f7c14bbaSAndroid Build Coastguard Worker return pos;
10327*f7c14bbaSAndroid Build Coastguard Worker continue;
10328*f7c14bbaSAndroid Build Coastguard Worker }
10329*f7c14bbaSAndroid Build Coastguard Worker /* otherwise map name has to be an exact match */
10330*f7c14bbaSAndroid Build Coastguard Worker if (map_uses_real_name(pos)) {
10331*f7c14bbaSAndroid Build Coastguard Worker if (strcmp(pos->real_name, name) == 0)
10332*f7c14bbaSAndroid Build Coastguard Worker return pos;
10333*f7c14bbaSAndroid Build Coastguard Worker continue;
10334*f7c14bbaSAndroid Build Coastguard Worker }
10335*f7c14bbaSAndroid Build Coastguard Worker if (strcmp(pos->name, name) == 0)
10336*f7c14bbaSAndroid Build Coastguard Worker return pos;
10337*f7c14bbaSAndroid Build Coastguard Worker }
10338*f7c14bbaSAndroid Build Coastguard Worker return errno = ENOENT, NULL;
10339*f7c14bbaSAndroid Build Coastguard Worker }
10340*f7c14bbaSAndroid Build Coastguard Worker
10341*f7c14bbaSAndroid Build Coastguard Worker int
bpf_object__find_map_fd_by_name(const struct bpf_object * obj,const char * name)10342*f7c14bbaSAndroid Build Coastguard Worker bpf_object__find_map_fd_by_name(const struct bpf_object *obj, const char *name)
10343*f7c14bbaSAndroid Build Coastguard Worker {
10344*f7c14bbaSAndroid Build Coastguard Worker return bpf_map__fd(bpf_object__find_map_by_name(obj, name));
10345*f7c14bbaSAndroid Build Coastguard Worker }
10346*f7c14bbaSAndroid Build Coastguard Worker
validate_map_op(const struct bpf_map * map,size_t key_sz,size_t value_sz,bool check_value_sz)10347*f7c14bbaSAndroid Build Coastguard Worker static int validate_map_op(const struct bpf_map *map, size_t key_sz,
10348*f7c14bbaSAndroid Build Coastguard Worker size_t value_sz, bool check_value_sz)
10349*f7c14bbaSAndroid Build Coastguard Worker {
10350*f7c14bbaSAndroid Build Coastguard Worker if (!map_is_created(map)) /* map is not yet created */
10351*f7c14bbaSAndroid Build Coastguard Worker return -ENOENT;
10352*f7c14bbaSAndroid Build Coastguard Worker
10353*f7c14bbaSAndroid Build Coastguard Worker if (map->def.key_size != key_sz) {
10354*f7c14bbaSAndroid Build Coastguard Worker pr_warn("map '%s': unexpected key size %zu provided, expected %u\n",
10355*f7c14bbaSAndroid Build Coastguard Worker map->name, key_sz, map->def.key_size);
10356*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
10357*f7c14bbaSAndroid Build Coastguard Worker }
10358*f7c14bbaSAndroid Build Coastguard Worker
10359*f7c14bbaSAndroid Build Coastguard Worker if (map->fd < 0) {
10360*f7c14bbaSAndroid Build Coastguard Worker pr_warn("map '%s': can't use BPF map without FD (was it created?)\n", map->name);
10361*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
10362*f7c14bbaSAndroid Build Coastguard Worker }
10363*f7c14bbaSAndroid Build Coastguard Worker
10364*f7c14bbaSAndroid Build Coastguard Worker if (!check_value_sz)
10365*f7c14bbaSAndroid Build Coastguard Worker return 0;
10366*f7c14bbaSAndroid Build Coastguard Worker
10367*f7c14bbaSAndroid Build Coastguard Worker switch (map->def.type) {
10368*f7c14bbaSAndroid Build Coastguard Worker case BPF_MAP_TYPE_PERCPU_ARRAY:
10369*f7c14bbaSAndroid Build Coastguard Worker case BPF_MAP_TYPE_PERCPU_HASH:
10370*f7c14bbaSAndroid Build Coastguard Worker case BPF_MAP_TYPE_LRU_PERCPU_HASH:
10371*f7c14bbaSAndroid Build Coastguard Worker case BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE: {
10372*f7c14bbaSAndroid Build Coastguard Worker int num_cpu = libbpf_num_possible_cpus();
10373*f7c14bbaSAndroid Build Coastguard Worker size_t elem_sz = roundup(map->def.value_size, 8);
10374*f7c14bbaSAndroid Build Coastguard Worker
10375*f7c14bbaSAndroid Build Coastguard Worker if (value_sz != num_cpu * elem_sz) {
10376*f7c14bbaSAndroid Build Coastguard Worker pr_warn("map '%s': unexpected value size %zu provided for per-CPU map, expected %d * %zu = %zd\n",
10377*f7c14bbaSAndroid Build Coastguard Worker map->name, value_sz, num_cpu, elem_sz, num_cpu * elem_sz);
10378*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
10379*f7c14bbaSAndroid Build Coastguard Worker }
10380*f7c14bbaSAndroid Build Coastguard Worker break;
10381*f7c14bbaSAndroid Build Coastguard Worker }
10382*f7c14bbaSAndroid Build Coastguard Worker default:
10383*f7c14bbaSAndroid Build Coastguard Worker if (map->def.value_size != value_sz) {
10384*f7c14bbaSAndroid Build Coastguard Worker pr_warn("map '%s': unexpected value size %zu provided, expected %u\n",
10385*f7c14bbaSAndroid Build Coastguard Worker map->name, value_sz, map->def.value_size);
10386*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
10387*f7c14bbaSAndroid Build Coastguard Worker }
10388*f7c14bbaSAndroid Build Coastguard Worker break;
10389*f7c14bbaSAndroid Build Coastguard Worker }
10390*f7c14bbaSAndroid Build Coastguard Worker return 0;
10391*f7c14bbaSAndroid Build Coastguard Worker }
10392*f7c14bbaSAndroid Build Coastguard Worker
bpf_map__lookup_elem(const struct bpf_map * map,const void * key,size_t key_sz,void * value,size_t value_sz,__u64 flags)10393*f7c14bbaSAndroid Build Coastguard Worker int bpf_map__lookup_elem(const struct bpf_map *map,
10394*f7c14bbaSAndroid Build Coastguard Worker const void *key, size_t key_sz,
10395*f7c14bbaSAndroid Build Coastguard Worker void *value, size_t value_sz, __u64 flags)
10396*f7c14bbaSAndroid Build Coastguard Worker {
10397*f7c14bbaSAndroid Build Coastguard Worker int err;
10398*f7c14bbaSAndroid Build Coastguard Worker
10399*f7c14bbaSAndroid Build Coastguard Worker err = validate_map_op(map, key_sz, value_sz, true);
10400*f7c14bbaSAndroid Build Coastguard Worker if (err)
10401*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(err);
10402*f7c14bbaSAndroid Build Coastguard Worker
10403*f7c14bbaSAndroid Build Coastguard Worker return bpf_map_lookup_elem_flags(map->fd, key, value, flags);
10404*f7c14bbaSAndroid Build Coastguard Worker }
10405*f7c14bbaSAndroid Build Coastguard Worker
bpf_map__update_elem(const struct bpf_map * map,const void * key,size_t key_sz,const void * value,size_t value_sz,__u64 flags)10406*f7c14bbaSAndroid Build Coastguard Worker int bpf_map__update_elem(const struct bpf_map *map,
10407*f7c14bbaSAndroid Build Coastguard Worker const void *key, size_t key_sz,
10408*f7c14bbaSAndroid Build Coastguard Worker const void *value, size_t value_sz, __u64 flags)
10409*f7c14bbaSAndroid Build Coastguard Worker {
10410*f7c14bbaSAndroid Build Coastguard Worker int err;
10411*f7c14bbaSAndroid Build Coastguard Worker
10412*f7c14bbaSAndroid Build Coastguard Worker err = validate_map_op(map, key_sz, value_sz, true);
10413*f7c14bbaSAndroid Build Coastguard Worker if (err)
10414*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(err);
10415*f7c14bbaSAndroid Build Coastguard Worker
10416*f7c14bbaSAndroid Build Coastguard Worker return bpf_map_update_elem(map->fd, key, value, flags);
10417*f7c14bbaSAndroid Build Coastguard Worker }
10418*f7c14bbaSAndroid Build Coastguard Worker
bpf_map__delete_elem(const struct bpf_map * map,const void * key,size_t key_sz,__u64 flags)10419*f7c14bbaSAndroid Build Coastguard Worker int bpf_map__delete_elem(const struct bpf_map *map,
10420*f7c14bbaSAndroid Build Coastguard Worker const void *key, size_t key_sz, __u64 flags)
10421*f7c14bbaSAndroid Build Coastguard Worker {
10422*f7c14bbaSAndroid Build Coastguard Worker int err;
10423*f7c14bbaSAndroid Build Coastguard Worker
10424*f7c14bbaSAndroid Build Coastguard Worker err = validate_map_op(map, key_sz, 0, false /* check_value_sz */);
10425*f7c14bbaSAndroid Build Coastguard Worker if (err)
10426*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(err);
10427*f7c14bbaSAndroid Build Coastguard Worker
10428*f7c14bbaSAndroid Build Coastguard Worker return bpf_map_delete_elem_flags(map->fd, key, flags);
10429*f7c14bbaSAndroid Build Coastguard Worker }
10430*f7c14bbaSAndroid Build Coastguard Worker
bpf_map__lookup_and_delete_elem(const struct bpf_map * map,const void * key,size_t key_sz,void * value,size_t value_sz,__u64 flags)10431*f7c14bbaSAndroid Build Coastguard Worker int bpf_map__lookup_and_delete_elem(const struct bpf_map *map,
10432*f7c14bbaSAndroid Build Coastguard Worker const void *key, size_t key_sz,
10433*f7c14bbaSAndroid Build Coastguard Worker void *value, size_t value_sz, __u64 flags)
10434*f7c14bbaSAndroid Build Coastguard Worker {
10435*f7c14bbaSAndroid Build Coastguard Worker int err;
10436*f7c14bbaSAndroid Build Coastguard Worker
10437*f7c14bbaSAndroid Build Coastguard Worker err = validate_map_op(map, key_sz, value_sz, true);
10438*f7c14bbaSAndroid Build Coastguard Worker if (err)
10439*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(err);
10440*f7c14bbaSAndroid Build Coastguard Worker
10441*f7c14bbaSAndroid Build Coastguard Worker return bpf_map_lookup_and_delete_elem_flags(map->fd, key, value, flags);
10442*f7c14bbaSAndroid Build Coastguard Worker }
10443*f7c14bbaSAndroid Build Coastguard Worker
bpf_map__get_next_key(const struct bpf_map * map,const void * cur_key,void * next_key,size_t key_sz)10444*f7c14bbaSAndroid Build Coastguard Worker int bpf_map__get_next_key(const struct bpf_map *map,
10445*f7c14bbaSAndroid Build Coastguard Worker const void *cur_key, void *next_key, size_t key_sz)
10446*f7c14bbaSAndroid Build Coastguard Worker {
10447*f7c14bbaSAndroid Build Coastguard Worker int err;
10448*f7c14bbaSAndroid Build Coastguard Worker
10449*f7c14bbaSAndroid Build Coastguard Worker err = validate_map_op(map, key_sz, 0, false /* check_value_sz */);
10450*f7c14bbaSAndroid Build Coastguard Worker if (err)
10451*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(err);
10452*f7c14bbaSAndroid Build Coastguard Worker
10453*f7c14bbaSAndroid Build Coastguard Worker return bpf_map_get_next_key(map->fd, cur_key, next_key);
10454*f7c14bbaSAndroid Build Coastguard Worker }
10455*f7c14bbaSAndroid Build Coastguard Worker
libbpf_get_error(const void * ptr)10456*f7c14bbaSAndroid Build Coastguard Worker long libbpf_get_error(const void *ptr)
10457*f7c14bbaSAndroid Build Coastguard Worker {
10458*f7c14bbaSAndroid Build Coastguard Worker if (!IS_ERR_OR_NULL(ptr))
10459*f7c14bbaSAndroid Build Coastguard Worker return 0;
10460*f7c14bbaSAndroid Build Coastguard Worker
10461*f7c14bbaSAndroid Build Coastguard Worker if (IS_ERR(ptr))
10462*f7c14bbaSAndroid Build Coastguard Worker errno = -PTR_ERR(ptr);
10463*f7c14bbaSAndroid Build Coastguard Worker
10464*f7c14bbaSAndroid Build Coastguard Worker /* If ptr == NULL, then errno should be already set by the failing
10465*f7c14bbaSAndroid Build Coastguard Worker * API, because libbpf never returns NULL on success and it now always
10466*f7c14bbaSAndroid Build Coastguard Worker * sets errno on error. So no extra errno handling for ptr == NULL
10467*f7c14bbaSAndroid Build Coastguard Worker * case.
10468*f7c14bbaSAndroid Build Coastguard Worker */
10469*f7c14bbaSAndroid Build Coastguard Worker return -errno;
10470*f7c14bbaSAndroid Build Coastguard Worker }
10471*f7c14bbaSAndroid Build Coastguard Worker
10472*f7c14bbaSAndroid Build Coastguard Worker /* Replace link's underlying BPF program with the new one */
bpf_link__update_program(struct bpf_link * link,struct bpf_program * prog)10473*f7c14bbaSAndroid Build Coastguard Worker int bpf_link__update_program(struct bpf_link *link, struct bpf_program *prog)
10474*f7c14bbaSAndroid Build Coastguard Worker {
10475*f7c14bbaSAndroid Build Coastguard Worker int ret;
10476*f7c14bbaSAndroid Build Coastguard Worker int prog_fd = bpf_program__fd(prog);
10477*f7c14bbaSAndroid Build Coastguard Worker
10478*f7c14bbaSAndroid Build Coastguard Worker if (prog_fd < 0) {
10479*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': can't use BPF program without FD (was it loaded?)\n",
10480*f7c14bbaSAndroid Build Coastguard Worker prog->name);
10481*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-EINVAL);
10482*f7c14bbaSAndroid Build Coastguard Worker }
10483*f7c14bbaSAndroid Build Coastguard Worker
10484*f7c14bbaSAndroid Build Coastguard Worker ret = bpf_link_update(bpf_link__fd(link), prog_fd, NULL);
10485*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_errno(ret);
10486*f7c14bbaSAndroid Build Coastguard Worker }
10487*f7c14bbaSAndroid Build Coastguard Worker
10488*f7c14bbaSAndroid Build Coastguard Worker /* Release "ownership" of underlying BPF resource (typically, BPF program
10489*f7c14bbaSAndroid Build Coastguard Worker * attached to some BPF hook, e.g., tracepoint, kprobe, etc). Disconnected
10490*f7c14bbaSAndroid Build Coastguard Worker * link, when destructed through bpf_link__destroy() call won't attempt to
10491*f7c14bbaSAndroid Build Coastguard Worker * detach/unregisted that BPF resource. This is useful in situations where,
10492*f7c14bbaSAndroid Build Coastguard Worker * say, attached BPF program has to outlive userspace program that attached it
10493*f7c14bbaSAndroid Build Coastguard Worker * in the system. Depending on type of BPF program, though, there might be
10494*f7c14bbaSAndroid Build Coastguard Worker * additional steps (like pinning BPF program in BPF FS) necessary to ensure
10495*f7c14bbaSAndroid Build Coastguard Worker * exit of userspace program doesn't trigger automatic detachment and clean up
10496*f7c14bbaSAndroid Build Coastguard Worker * inside the kernel.
10497*f7c14bbaSAndroid Build Coastguard Worker */
bpf_link__disconnect(struct bpf_link * link)10498*f7c14bbaSAndroid Build Coastguard Worker void bpf_link__disconnect(struct bpf_link *link)
10499*f7c14bbaSAndroid Build Coastguard Worker {
10500*f7c14bbaSAndroid Build Coastguard Worker link->disconnected = true;
10501*f7c14bbaSAndroid Build Coastguard Worker }
10502*f7c14bbaSAndroid Build Coastguard Worker
bpf_link__destroy(struct bpf_link * link)10503*f7c14bbaSAndroid Build Coastguard Worker int bpf_link__destroy(struct bpf_link *link)
10504*f7c14bbaSAndroid Build Coastguard Worker {
10505*f7c14bbaSAndroid Build Coastguard Worker int err = 0;
10506*f7c14bbaSAndroid Build Coastguard Worker
10507*f7c14bbaSAndroid Build Coastguard Worker if (IS_ERR_OR_NULL(link))
10508*f7c14bbaSAndroid Build Coastguard Worker return 0;
10509*f7c14bbaSAndroid Build Coastguard Worker
10510*f7c14bbaSAndroid Build Coastguard Worker if (!link->disconnected && link->detach)
10511*f7c14bbaSAndroid Build Coastguard Worker err = link->detach(link);
10512*f7c14bbaSAndroid Build Coastguard Worker if (link->pin_path)
10513*f7c14bbaSAndroid Build Coastguard Worker free(link->pin_path);
10514*f7c14bbaSAndroid Build Coastguard Worker if (link->dealloc)
10515*f7c14bbaSAndroid Build Coastguard Worker link->dealloc(link);
10516*f7c14bbaSAndroid Build Coastguard Worker else
10517*f7c14bbaSAndroid Build Coastguard Worker free(link);
10518*f7c14bbaSAndroid Build Coastguard Worker
10519*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(err);
10520*f7c14bbaSAndroid Build Coastguard Worker }
10521*f7c14bbaSAndroid Build Coastguard Worker
bpf_link__fd(const struct bpf_link * link)10522*f7c14bbaSAndroid Build Coastguard Worker int bpf_link__fd(const struct bpf_link *link)
10523*f7c14bbaSAndroid Build Coastguard Worker {
10524*f7c14bbaSAndroid Build Coastguard Worker return link->fd;
10525*f7c14bbaSAndroid Build Coastguard Worker }
10526*f7c14bbaSAndroid Build Coastguard Worker
bpf_link__pin_path(const struct bpf_link * link)10527*f7c14bbaSAndroid Build Coastguard Worker const char *bpf_link__pin_path(const struct bpf_link *link)
10528*f7c14bbaSAndroid Build Coastguard Worker {
10529*f7c14bbaSAndroid Build Coastguard Worker return link->pin_path;
10530*f7c14bbaSAndroid Build Coastguard Worker }
10531*f7c14bbaSAndroid Build Coastguard Worker
bpf_link__detach_fd(struct bpf_link * link)10532*f7c14bbaSAndroid Build Coastguard Worker static int bpf_link__detach_fd(struct bpf_link *link)
10533*f7c14bbaSAndroid Build Coastguard Worker {
10534*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_errno(close(link->fd));
10535*f7c14bbaSAndroid Build Coastguard Worker }
10536*f7c14bbaSAndroid Build Coastguard Worker
bpf_link__open(const char * path)10537*f7c14bbaSAndroid Build Coastguard Worker struct bpf_link *bpf_link__open(const char *path)
10538*f7c14bbaSAndroid Build Coastguard Worker {
10539*f7c14bbaSAndroid Build Coastguard Worker struct bpf_link *link;
10540*f7c14bbaSAndroid Build Coastguard Worker int fd;
10541*f7c14bbaSAndroid Build Coastguard Worker
10542*f7c14bbaSAndroid Build Coastguard Worker fd = bpf_obj_get(path);
10543*f7c14bbaSAndroid Build Coastguard Worker if (fd < 0) {
10544*f7c14bbaSAndroid Build Coastguard Worker fd = -errno;
10545*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to open link at %s: %d\n", path, fd);
10546*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(fd);
10547*f7c14bbaSAndroid Build Coastguard Worker }
10548*f7c14bbaSAndroid Build Coastguard Worker
10549*f7c14bbaSAndroid Build Coastguard Worker link = calloc(1, sizeof(*link));
10550*f7c14bbaSAndroid Build Coastguard Worker if (!link) {
10551*f7c14bbaSAndroid Build Coastguard Worker close(fd);
10552*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(-ENOMEM);
10553*f7c14bbaSAndroid Build Coastguard Worker }
10554*f7c14bbaSAndroid Build Coastguard Worker link->detach = &bpf_link__detach_fd;
10555*f7c14bbaSAndroid Build Coastguard Worker link->fd = fd;
10556*f7c14bbaSAndroid Build Coastguard Worker
10557*f7c14bbaSAndroid Build Coastguard Worker link->pin_path = strdup(path);
10558*f7c14bbaSAndroid Build Coastguard Worker if (!link->pin_path) {
10559*f7c14bbaSAndroid Build Coastguard Worker bpf_link__destroy(link);
10560*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(-ENOMEM);
10561*f7c14bbaSAndroid Build Coastguard Worker }
10562*f7c14bbaSAndroid Build Coastguard Worker
10563*f7c14bbaSAndroid Build Coastguard Worker return link;
10564*f7c14bbaSAndroid Build Coastguard Worker }
10565*f7c14bbaSAndroid Build Coastguard Worker
bpf_link__detach(struct bpf_link * link)10566*f7c14bbaSAndroid Build Coastguard Worker int bpf_link__detach(struct bpf_link *link)
10567*f7c14bbaSAndroid Build Coastguard Worker {
10568*f7c14bbaSAndroid Build Coastguard Worker return bpf_link_detach(link->fd) ? -errno : 0;
10569*f7c14bbaSAndroid Build Coastguard Worker }
10570*f7c14bbaSAndroid Build Coastguard Worker
bpf_link__pin(struct bpf_link * link,const char * path)10571*f7c14bbaSAndroid Build Coastguard Worker int bpf_link__pin(struct bpf_link *link, const char *path)
10572*f7c14bbaSAndroid Build Coastguard Worker {
10573*f7c14bbaSAndroid Build Coastguard Worker int err;
10574*f7c14bbaSAndroid Build Coastguard Worker
10575*f7c14bbaSAndroid Build Coastguard Worker if (link->pin_path)
10576*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-EBUSY);
10577*f7c14bbaSAndroid Build Coastguard Worker err = make_parent_dir(path);
10578*f7c14bbaSAndroid Build Coastguard Worker if (err)
10579*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(err);
10580*f7c14bbaSAndroid Build Coastguard Worker err = check_path(path);
10581*f7c14bbaSAndroid Build Coastguard Worker if (err)
10582*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(err);
10583*f7c14bbaSAndroid Build Coastguard Worker
10584*f7c14bbaSAndroid Build Coastguard Worker link->pin_path = strdup(path);
10585*f7c14bbaSAndroid Build Coastguard Worker if (!link->pin_path)
10586*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-ENOMEM);
10587*f7c14bbaSAndroid Build Coastguard Worker
10588*f7c14bbaSAndroid Build Coastguard Worker if (bpf_obj_pin(link->fd, link->pin_path)) {
10589*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
10590*f7c14bbaSAndroid Build Coastguard Worker zfree(&link->pin_path);
10591*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(err);
10592*f7c14bbaSAndroid Build Coastguard Worker }
10593*f7c14bbaSAndroid Build Coastguard Worker
10594*f7c14bbaSAndroid Build Coastguard Worker pr_debug("link fd=%d: pinned at %s\n", link->fd, link->pin_path);
10595*f7c14bbaSAndroid Build Coastguard Worker return 0;
10596*f7c14bbaSAndroid Build Coastguard Worker }
10597*f7c14bbaSAndroid Build Coastguard Worker
bpf_link__unpin(struct bpf_link * link)10598*f7c14bbaSAndroid Build Coastguard Worker int bpf_link__unpin(struct bpf_link *link)
10599*f7c14bbaSAndroid Build Coastguard Worker {
10600*f7c14bbaSAndroid Build Coastguard Worker int err;
10601*f7c14bbaSAndroid Build Coastguard Worker
10602*f7c14bbaSAndroid Build Coastguard Worker if (!link->pin_path)
10603*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-EINVAL);
10604*f7c14bbaSAndroid Build Coastguard Worker
10605*f7c14bbaSAndroid Build Coastguard Worker err = unlink(link->pin_path);
10606*f7c14bbaSAndroid Build Coastguard Worker if (err != 0)
10607*f7c14bbaSAndroid Build Coastguard Worker return -errno;
10608*f7c14bbaSAndroid Build Coastguard Worker
10609*f7c14bbaSAndroid Build Coastguard Worker pr_debug("link fd=%d: unpinned from %s\n", link->fd, link->pin_path);
10610*f7c14bbaSAndroid Build Coastguard Worker zfree(&link->pin_path);
10611*f7c14bbaSAndroid Build Coastguard Worker return 0;
10612*f7c14bbaSAndroid Build Coastguard Worker }
10613*f7c14bbaSAndroid Build Coastguard Worker
10614*f7c14bbaSAndroid Build Coastguard Worker struct bpf_link_perf {
10615*f7c14bbaSAndroid Build Coastguard Worker struct bpf_link link;
10616*f7c14bbaSAndroid Build Coastguard Worker int perf_event_fd;
10617*f7c14bbaSAndroid Build Coastguard Worker /* legacy kprobe support: keep track of probe identifier and type */
10618*f7c14bbaSAndroid Build Coastguard Worker char *legacy_probe_name;
10619*f7c14bbaSAndroid Build Coastguard Worker bool legacy_is_kprobe;
10620*f7c14bbaSAndroid Build Coastguard Worker bool legacy_is_retprobe;
10621*f7c14bbaSAndroid Build Coastguard Worker };
10622*f7c14bbaSAndroid Build Coastguard Worker
10623*f7c14bbaSAndroid Build Coastguard Worker static int remove_kprobe_event_legacy(const char *probe_name, bool retprobe);
10624*f7c14bbaSAndroid Build Coastguard Worker static int remove_uprobe_event_legacy(const char *probe_name, bool retprobe);
10625*f7c14bbaSAndroid Build Coastguard Worker
bpf_link_perf_detach(struct bpf_link * link)10626*f7c14bbaSAndroid Build Coastguard Worker static int bpf_link_perf_detach(struct bpf_link *link)
10627*f7c14bbaSAndroid Build Coastguard Worker {
10628*f7c14bbaSAndroid Build Coastguard Worker struct bpf_link_perf *perf_link = container_of(link, struct bpf_link_perf, link);
10629*f7c14bbaSAndroid Build Coastguard Worker int err = 0;
10630*f7c14bbaSAndroid Build Coastguard Worker
10631*f7c14bbaSAndroid Build Coastguard Worker if (ioctl(perf_link->perf_event_fd, PERF_EVENT_IOC_DISABLE, 0) < 0)
10632*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
10633*f7c14bbaSAndroid Build Coastguard Worker
10634*f7c14bbaSAndroid Build Coastguard Worker if (perf_link->perf_event_fd != link->fd)
10635*f7c14bbaSAndroid Build Coastguard Worker close(perf_link->perf_event_fd);
10636*f7c14bbaSAndroid Build Coastguard Worker close(link->fd);
10637*f7c14bbaSAndroid Build Coastguard Worker
10638*f7c14bbaSAndroid Build Coastguard Worker /* legacy uprobe/kprobe needs to be removed after perf event fd closure */
10639*f7c14bbaSAndroid Build Coastguard Worker if (perf_link->legacy_probe_name) {
10640*f7c14bbaSAndroid Build Coastguard Worker if (perf_link->legacy_is_kprobe) {
10641*f7c14bbaSAndroid Build Coastguard Worker err = remove_kprobe_event_legacy(perf_link->legacy_probe_name,
10642*f7c14bbaSAndroid Build Coastguard Worker perf_link->legacy_is_retprobe);
10643*f7c14bbaSAndroid Build Coastguard Worker } else {
10644*f7c14bbaSAndroid Build Coastguard Worker err = remove_uprobe_event_legacy(perf_link->legacy_probe_name,
10645*f7c14bbaSAndroid Build Coastguard Worker perf_link->legacy_is_retprobe);
10646*f7c14bbaSAndroid Build Coastguard Worker }
10647*f7c14bbaSAndroid Build Coastguard Worker }
10648*f7c14bbaSAndroid Build Coastguard Worker
10649*f7c14bbaSAndroid Build Coastguard Worker return err;
10650*f7c14bbaSAndroid Build Coastguard Worker }
10651*f7c14bbaSAndroid Build Coastguard Worker
bpf_link_perf_dealloc(struct bpf_link * link)10652*f7c14bbaSAndroid Build Coastguard Worker static void bpf_link_perf_dealloc(struct bpf_link *link)
10653*f7c14bbaSAndroid Build Coastguard Worker {
10654*f7c14bbaSAndroid Build Coastguard Worker struct bpf_link_perf *perf_link = container_of(link, struct bpf_link_perf, link);
10655*f7c14bbaSAndroid Build Coastguard Worker
10656*f7c14bbaSAndroid Build Coastguard Worker free(perf_link->legacy_probe_name);
10657*f7c14bbaSAndroid Build Coastguard Worker free(perf_link);
10658*f7c14bbaSAndroid Build Coastguard Worker }
10659*f7c14bbaSAndroid Build Coastguard Worker
bpf_program__attach_perf_event_opts(const struct bpf_program * prog,int pfd,const struct bpf_perf_event_opts * opts)10660*f7c14bbaSAndroid Build Coastguard Worker struct bpf_link *bpf_program__attach_perf_event_opts(const struct bpf_program *prog, int pfd,
10661*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_perf_event_opts *opts)
10662*f7c14bbaSAndroid Build Coastguard Worker {
10663*f7c14bbaSAndroid Build Coastguard Worker char errmsg[STRERR_BUFSIZE];
10664*f7c14bbaSAndroid Build Coastguard Worker struct bpf_link_perf *link;
10665*f7c14bbaSAndroid Build Coastguard Worker int prog_fd, link_fd = -1, err;
10666*f7c14bbaSAndroid Build Coastguard Worker bool force_ioctl_attach;
10667*f7c14bbaSAndroid Build Coastguard Worker
10668*f7c14bbaSAndroid Build Coastguard Worker if (!OPTS_VALID(opts, bpf_perf_event_opts))
10669*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(-EINVAL);
10670*f7c14bbaSAndroid Build Coastguard Worker
10671*f7c14bbaSAndroid Build Coastguard Worker if (pfd < 0) {
10672*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': invalid perf event FD %d\n",
10673*f7c14bbaSAndroid Build Coastguard Worker prog->name, pfd);
10674*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(-EINVAL);
10675*f7c14bbaSAndroid Build Coastguard Worker }
10676*f7c14bbaSAndroid Build Coastguard Worker prog_fd = bpf_program__fd(prog);
10677*f7c14bbaSAndroid Build Coastguard Worker if (prog_fd < 0) {
10678*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': can't attach BPF program without FD (was it loaded?)\n",
10679*f7c14bbaSAndroid Build Coastguard Worker prog->name);
10680*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(-EINVAL);
10681*f7c14bbaSAndroid Build Coastguard Worker }
10682*f7c14bbaSAndroid Build Coastguard Worker
10683*f7c14bbaSAndroid Build Coastguard Worker link = calloc(1, sizeof(*link));
10684*f7c14bbaSAndroid Build Coastguard Worker if (!link)
10685*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(-ENOMEM);
10686*f7c14bbaSAndroid Build Coastguard Worker link->link.detach = &bpf_link_perf_detach;
10687*f7c14bbaSAndroid Build Coastguard Worker link->link.dealloc = &bpf_link_perf_dealloc;
10688*f7c14bbaSAndroid Build Coastguard Worker link->perf_event_fd = pfd;
10689*f7c14bbaSAndroid Build Coastguard Worker
10690*f7c14bbaSAndroid Build Coastguard Worker force_ioctl_attach = OPTS_GET(opts, force_ioctl_attach, false);
10691*f7c14bbaSAndroid Build Coastguard Worker if (kernel_supports(prog->obj, FEAT_PERF_LINK) && !force_ioctl_attach) {
10692*f7c14bbaSAndroid Build Coastguard Worker DECLARE_LIBBPF_OPTS(bpf_link_create_opts, link_opts,
10693*f7c14bbaSAndroid Build Coastguard Worker .perf_event.bpf_cookie = OPTS_GET(opts, bpf_cookie, 0));
10694*f7c14bbaSAndroid Build Coastguard Worker
10695*f7c14bbaSAndroid Build Coastguard Worker link_fd = bpf_link_create(prog_fd, pfd, BPF_PERF_EVENT, &link_opts);
10696*f7c14bbaSAndroid Build Coastguard Worker if (link_fd < 0) {
10697*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
10698*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': failed to create BPF link for perf_event FD %d: %d (%s)\n",
10699*f7c14bbaSAndroid Build Coastguard Worker prog->name, pfd,
10700*f7c14bbaSAndroid Build Coastguard Worker err, libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
10701*f7c14bbaSAndroid Build Coastguard Worker goto err_out;
10702*f7c14bbaSAndroid Build Coastguard Worker }
10703*f7c14bbaSAndroid Build Coastguard Worker link->link.fd = link_fd;
10704*f7c14bbaSAndroid Build Coastguard Worker } else {
10705*f7c14bbaSAndroid Build Coastguard Worker if (OPTS_GET(opts, bpf_cookie, 0)) {
10706*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': user context value is not supported\n", prog->name);
10707*f7c14bbaSAndroid Build Coastguard Worker err = -EOPNOTSUPP;
10708*f7c14bbaSAndroid Build Coastguard Worker goto err_out;
10709*f7c14bbaSAndroid Build Coastguard Worker }
10710*f7c14bbaSAndroid Build Coastguard Worker
10711*f7c14bbaSAndroid Build Coastguard Worker if (ioctl(pfd, PERF_EVENT_IOC_SET_BPF, prog_fd) < 0) {
10712*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
10713*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': failed to attach to perf_event FD %d: %s\n",
10714*f7c14bbaSAndroid Build Coastguard Worker prog->name, pfd, libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
10715*f7c14bbaSAndroid Build Coastguard Worker if (err == -EPROTO)
10716*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': try add PERF_SAMPLE_CALLCHAIN to or remove exclude_callchain_[kernel|user] from pfd %d\n",
10717*f7c14bbaSAndroid Build Coastguard Worker prog->name, pfd);
10718*f7c14bbaSAndroid Build Coastguard Worker goto err_out;
10719*f7c14bbaSAndroid Build Coastguard Worker }
10720*f7c14bbaSAndroid Build Coastguard Worker link->link.fd = pfd;
10721*f7c14bbaSAndroid Build Coastguard Worker }
10722*f7c14bbaSAndroid Build Coastguard Worker if (ioctl(pfd, PERF_EVENT_IOC_ENABLE, 0) < 0) {
10723*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
10724*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': failed to enable perf_event FD %d: %s\n",
10725*f7c14bbaSAndroid Build Coastguard Worker prog->name, pfd, libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
10726*f7c14bbaSAndroid Build Coastguard Worker goto err_out;
10727*f7c14bbaSAndroid Build Coastguard Worker }
10728*f7c14bbaSAndroid Build Coastguard Worker
10729*f7c14bbaSAndroid Build Coastguard Worker return &link->link;
10730*f7c14bbaSAndroid Build Coastguard Worker err_out:
10731*f7c14bbaSAndroid Build Coastguard Worker if (link_fd >= 0)
10732*f7c14bbaSAndroid Build Coastguard Worker close(link_fd);
10733*f7c14bbaSAndroid Build Coastguard Worker free(link);
10734*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(err);
10735*f7c14bbaSAndroid Build Coastguard Worker }
10736*f7c14bbaSAndroid Build Coastguard Worker
bpf_program__attach_perf_event(const struct bpf_program * prog,int pfd)10737*f7c14bbaSAndroid Build Coastguard Worker struct bpf_link *bpf_program__attach_perf_event(const struct bpf_program *prog, int pfd)
10738*f7c14bbaSAndroid Build Coastguard Worker {
10739*f7c14bbaSAndroid Build Coastguard Worker return bpf_program__attach_perf_event_opts(prog, pfd, NULL);
10740*f7c14bbaSAndroid Build Coastguard Worker }
10741*f7c14bbaSAndroid Build Coastguard Worker
10742*f7c14bbaSAndroid Build Coastguard Worker /*
10743*f7c14bbaSAndroid Build Coastguard Worker * this function is expected to parse integer in the range of [0, 2^31-1] from
10744*f7c14bbaSAndroid Build Coastguard Worker * given file using scanf format string fmt. If actual parsed value is
10745*f7c14bbaSAndroid Build Coastguard Worker * negative, the result might be indistinguishable from error
10746*f7c14bbaSAndroid Build Coastguard Worker */
parse_uint_from_file(const char * file,const char * fmt)10747*f7c14bbaSAndroid Build Coastguard Worker static int parse_uint_from_file(const char *file, const char *fmt)
10748*f7c14bbaSAndroid Build Coastguard Worker {
10749*f7c14bbaSAndroid Build Coastguard Worker char buf[STRERR_BUFSIZE];
10750*f7c14bbaSAndroid Build Coastguard Worker int err, ret;
10751*f7c14bbaSAndroid Build Coastguard Worker FILE *f;
10752*f7c14bbaSAndroid Build Coastguard Worker
10753*f7c14bbaSAndroid Build Coastguard Worker f = fopen(file, "re");
10754*f7c14bbaSAndroid Build Coastguard Worker if (!f) {
10755*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
10756*f7c14bbaSAndroid Build Coastguard Worker pr_debug("failed to open '%s': %s\n", file,
10757*f7c14bbaSAndroid Build Coastguard Worker libbpf_strerror_r(err, buf, sizeof(buf)));
10758*f7c14bbaSAndroid Build Coastguard Worker return err;
10759*f7c14bbaSAndroid Build Coastguard Worker }
10760*f7c14bbaSAndroid Build Coastguard Worker err = fscanf(f, fmt, &ret);
10761*f7c14bbaSAndroid Build Coastguard Worker if (err != 1) {
10762*f7c14bbaSAndroid Build Coastguard Worker err = err == EOF ? -EIO : -errno;
10763*f7c14bbaSAndroid Build Coastguard Worker pr_debug("failed to parse '%s': %s\n", file,
10764*f7c14bbaSAndroid Build Coastguard Worker libbpf_strerror_r(err, buf, sizeof(buf)));
10765*f7c14bbaSAndroid Build Coastguard Worker fclose(f);
10766*f7c14bbaSAndroid Build Coastguard Worker return err;
10767*f7c14bbaSAndroid Build Coastguard Worker }
10768*f7c14bbaSAndroid Build Coastguard Worker fclose(f);
10769*f7c14bbaSAndroid Build Coastguard Worker return ret;
10770*f7c14bbaSAndroid Build Coastguard Worker }
10771*f7c14bbaSAndroid Build Coastguard Worker
determine_kprobe_perf_type(void)10772*f7c14bbaSAndroid Build Coastguard Worker static int determine_kprobe_perf_type(void)
10773*f7c14bbaSAndroid Build Coastguard Worker {
10774*f7c14bbaSAndroid Build Coastguard Worker const char *file = "/sys/bus/event_source/devices/kprobe/type";
10775*f7c14bbaSAndroid Build Coastguard Worker
10776*f7c14bbaSAndroid Build Coastguard Worker return parse_uint_from_file(file, "%d\n");
10777*f7c14bbaSAndroid Build Coastguard Worker }
10778*f7c14bbaSAndroid Build Coastguard Worker
determine_uprobe_perf_type(void)10779*f7c14bbaSAndroid Build Coastguard Worker static int determine_uprobe_perf_type(void)
10780*f7c14bbaSAndroid Build Coastguard Worker {
10781*f7c14bbaSAndroid Build Coastguard Worker const char *file = "/sys/bus/event_source/devices/uprobe/type";
10782*f7c14bbaSAndroid Build Coastguard Worker
10783*f7c14bbaSAndroid Build Coastguard Worker return parse_uint_from_file(file, "%d\n");
10784*f7c14bbaSAndroid Build Coastguard Worker }
10785*f7c14bbaSAndroid Build Coastguard Worker
determine_kprobe_retprobe_bit(void)10786*f7c14bbaSAndroid Build Coastguard Worker static int determine_kprobe_retprobe_bit(void)
10787*f7c14bbaSAndroid Build Coastguard Worker {
10788*f7c14bbaSAndroid Build Coastguard Worker const char *file = "/sys/bus/event_source/devices/kprobe/format/retprobe";
10789*f7c14bbaSAndroid Build Coastguard Worker
10790*f7c14bbaSAndroid Build Coastguard Worker return parse_uint_from_file(file, "config:%d\n");
10791*f7c14bbaSAndroid Build Coastguard Worker }
10792*f7c14bbaSAndroid Build Coastguard Worker
determine_uprobe_retprobe_bit(void)10793*f7c14bbaSAndroid Build Coastguard Worker static int determine_uprobe_retprobe_bit(void)
10794*f7c14bbaSAndroid Build Coastguard Worker {
10795*f7c14bbaSAndroid Build Coastguard Worker const char *file = "/sys/bus/event_source/devices/uprobe/format/retprobe";
10796*f7c14bbaSAndroid Build Coastguard Worker
10797*f7c14bbaSAndroid Build Coastguard Worker return parse_uint_from_file(file, "config:%d\n");
10798*f7c14bbaSAndroid Build Coastguard Worker }
10799*f7c14bbaSAndroid Build Coastguard Worker
10800*f7c14bbaSAndroid Build Coastguard Worker #define PERF_UPROBE_REF_CTR_OFFSET_BITS 32
10801*f7c14bbaSAndroid Build Coastguard Worker #define PERF_UPROBE_REF_CTR_OFFSET_SHIFT 32
10802*f7c14bbaSAndroid Build Coastguard Worker
perf_event_open_probe(bool uprobe,bool retprobe,const char * name,uint64_t offset,int pid,size_t ref_ctr_off)10803*f7c14bbaSAndroid Build Coastguard Worker static int perf_event_open_probe(bool uprobe, bool retprobe, const char *name,
10804*f7c14bbaSAndroid Build Coastguard Worker uint64_t offset, int pid, size_t ref_ctr_off)
10805*f7c14bbaSAndroid Build Coastguard Worker {
10806*f7c14bbaSAndroid Build Coastguard Worker const size_t attr_sz = sizeof(struct perf_event_attr);
10807*f7c14bbaSAndroid Build Coastguard Worker struct perf_event_attr attr;
10808*f7c14bbaSAndroid Build Coastguard Worker char errmsg[STRERR_BUFSIZE];
10809*f7c14bbaSAndroid Build Coastguard Worker int type, pfd;
10810*f7c14bbaSAndroid Build Coastguard Worker
10811*f7c14bbaSAndroid Build Coastguard Worker if ((__u64)ref_ctr_off >= (1ULL << PERF_UPROBE_REF_CTR_OFFSET_BITS))
10812*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
10813*f7c14bbaSAndroid Build Coastguard Worker
10814*f7c14bbaSAndroid Build Coastguard Worker memset(&attr, 0, attr_sz);
10815*f7c14bbaSAndroid Build Coastguard Worker
10816*f7c14bbaSAndroid Build Coastguard Worker type = uprobe ? determine_uprobe_perf_type()
10817*f7c14bbaSAndroid Build Coastguard Worker : determine_kprobe_perf_type();
10818*f7c14bbaSAndroid Build Coastguard Worker if (type < 0) {
10819*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to determine %s perf type: %s\n",
10820*f7c14bbaSAndroid Build Coastguard Worker uprobe ? "uprobe" : "kprobe",
10821*f7c14bbaSAndroid Build Coastguard Worker libbpf_strerror_r(type, errmsg, sizeof(errmsg)));
10822*f7c14bbaSAndroid Build Coastguard Worker return type;
10823*f7c14bbaSAndroid Build Coastguard Worker }
10824*f7c14bbaSAndroid Build Coastguard Worker if (retprobe) {
10825*f7c14bbaSAndroid Build Coastguard Worker int bit = uprobe ? determine_uprobe_retprobe_bit()
10826*f7c14bbaSAndroid Build Coastguard Worker : determine_kprobe_retprobe_bit();
10827*f7c14bbaSAndroid Build Coastguard Worker
10828*f7c14bbaSAndroid Build Coastguard Worker if (bit < 0) {
10829*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to determine %s retprobe bit: %s\n",
10830*f7c14bbaSAndroid Build Coastguard Worker uprobe ? "uprobe" : "kprobe",
10831*f7c14bbaSAndroid Build Coastguard Worker libbpf_strerror_r(bit, errmsg, sizeof(errmsg)));
10832*f7c14bbaSAndroid Build Coastguard Worker return bit;
10833*f7c14bbaSAndroid Build Coastguard Worker }
10834*f7c14bbaSAndroid Build Coastguard Worker attr.config |= 1 << bit;
10835*f7c14bbaSAndroid Build Coastguard Worker }
10836*f7c14bbaSAndroid Build Coastguard Worker attr.size = attr_sz;
10837*f7c14bbaSAndroid Build Coastguard Worker attr.type = type;
10838*f7c14bbaSAndroid Build Coastguard Worker attr.config |= (__u64)ref_ctr_off << PERF_UPROBE_REF_CTR_OFFSET_SHIFT;
10839*f7c14bbaSAndroid Build Coastguard Worker attr.config1 = ptr_to_u64(name); /* kprobe_func or uprobe_path */
10840*f7c14bbaSAndroid Build Coastguard Worker attr.config2 = offset; /* kprobe_addr or probe_offset */
10841*f7c14bbaSAndroid Build Coastguard Worker
10842*f7c14bbaSAndroid Build Coastguard Worker /* pid filter is meaningful only for uprobes */
10843*f7c14bbaSAndroid Build Coastguard Worker pfd = syscall(__NR_perf_event_open, &attr,
10844*f7c14bbaSAndroid Build Coastguard Worker pid < 0 ? -1 : pid /* pid */,
10845*f7c14bbaSAndroid Build Coastguard Worker pid == -1 ? 0 : -1 /* cpu */,
10846*f7c14bbaSAndroid Build Coastguard Worker -1 /* group_fd */, PERF_FLAG_FD_CLOEXEC);
10847*f7c14bbaSAndroid Build Coastguard Worker return pfd >= 0 ? pfd : -errno;
10848*f7c14bbaSAndroid Build Coastguard Worker }
10849*f7c14bbaSAndroid Build Coastguard Worker
append_to_file(const char * file,const char * fmt,...)10850*f7c14bbaSAndroid Build Coastguard Worker static int append_to_file(const char *file, const char *fmt, ...)
10851*f7c14bbaSAndroid Build Coastguard Worker {
10852*f7c14bbaSAndroid Build Coastguard Worker int fd, n, err = 0;
10853*f7c14bbaSAndroid Build Coastguard Worker va_list ap;
10854*f7c14bbaSAndroid Build Coastguard Worker char buf[1024];
10855*f7c14bbaSAndroid Build Coastguard Worker
10856*f7c14bbaSAndroid Build Coastguard Worker va_start(ap, fmt);
10857*f7c14bbaSAndroid Build Coastguard Worker n = vsnprintf(buf, sizeof(buf), fmt, ap);
10858*f7c14bbaSAndroid Build Coastguard Worker va_end(ap);
10859*f7c14bbaSAndroid Build Coastguard Worker
10860*f7c14bbaSAndroid Build Coastguard Worker if (n < 0 || n >= sizeof(buf))
10861*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
10862*f7c14bbaSAndroid Build Coastguard Worker
10863*f7c14bbaSAndroid Build Coastguard Worker fd = open(file, O_WRONLY | O_APPEND | O_CLOEXEC, 0);
10864*f7c14bbaSAndroid Build Coastguard Worker if (fd < 0)
10865*f7c14bbaSAndroid Build Coastguard Worker return -errno;
10866*f7c14bbaSAndroid Build Coastguard Worker
10867*f7c14bbaSAndroid Build Coastguard Worker if (write(fd, buf, n) < 0)
10868*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
10869*f7c14bbaSAndroid Build Coastguard Worker
10870*f7c14bbaSAndroid Build Coastguard Worker close(fd);
10871*f7c14bbaSAndroid Build Coastguard Worker return err;
10872*f7c14bbaSAndroid Build Coastguard Worker }
10873*f7c14bbaSAndroid Build Coastguard Worker
10874*f7c14bbaSAndroid Build Coastguard Worker #define DEBUGFS "/sys/kernel/debug/tracing"
10875*f7c14bbaSAndroid Build Coastguard Worker #define TRACEFS "/sys/kernel/tracing"
10876*f7c14bbaSAndroid Build Coastguard Worker
use_debugfs(void)10877*f7c14bbaSAndroid Build Coastguard Worker static bool use_debugfs(void)
10878*f7c14bbaSAndroid Build Coastguard Worker {
10879*f7c14bbaSAndroid Build Coastguard Worker static int has_debugfs = -1;
10880*f7c14bbaSAndroid Build Coastguard Worker
10881*f7c14bbaSAndroid Build Coastguard Worker if (has_debugfs < 0)
10882*f7c14bbaSAndroid Build Coastguard Worker has_debugfs = faccessat(AT_FDCWD, DEBUGFS, F_OK, AT_EACCESS) == 0;
10883*f7c14bbaSAndroid Build Coastguard Worker
10884*f7c14bbaSAndroid Build Coastguard Worker return has_debugfs == 1;
10885*f7c14bbaSAndroid Build Coastguard Worker }
10886*f7c14bbaSAndroid Build Coastguard Worker
tracefs_path(void)10887*f7c14bbaSAndroid Build Coastguard Worker static const char *tracefs_path(void)
10888*f7c14bbaSAndroid Build Coastguard Worker {
10889*f7c14bbaSAndroid Build Coastguard Worker return use_debugfs() ? DEBUGFS : TRACEFS;
10890*f7c14bbaSAndroid Build Coastguard Worker }
10891*f7c14bbaSAndroid Build Coastguard Worker
tracefs_kprobe_events(void)10892*f7c14bbaSAndroid Build Coastguard Worker static const char *tracefs_kprobe_events(void)
10893*f7c14bbaSAndroid Build Coastguard Worker {
10894*f7c14bbaSAndroid Build Coastguard Worker return use_debugfs() ? DEBUGFS"/kprobe_events" : TRACEFS"/kprobe_events";
10895*f7c14bbaSAndroid Build Coastguard Worker }
10896*f7c14bbaSAndroid Build Coastguard Worker
tracefs_uprobe_events(void)10897*f7c14bbaSAndroid Build Coastguard Worker static const char *tracefs_uprobe_events(void)
10898*f7c14bbaSAndroid Build Coastguard Worker {
10899*f7c14bbaSAndroid Build Coastguard Worker return use_debugfs() ? DEBUGFS"/uprobe_events" : TRACEFS"/uprobe_events";
10900*f7c14bbaSAndroid Build Coastguard Worker }
10901*f7c14bbaSAndroid Build Coastguard Worker
tracefs_available_filter_functions(void)10902*f7c14bbaSAndroid Build Coastguard Worker static const char *tracefs_available_filter_functions(void)
10903*f7c14bbaSAndroid Build Coastguard Worker {
10904*f7c14bbaSAndroid Build Coastguard Worker return use_debugfs() ? DEBUGFS"/available_filter_functions"
10905*f7c14bbaSAndroid Build Coastguard Worker : TRACEFS"/available_filter_functions";
10906*f7c14bbaSAndroid Build Coastguard Worker }
10907*f7c14bbaSAndroid Build Coastguard Worker
tracefs_available_filter_functions_addrs(void)10908*f7c14bbaSAndroid Build Coastguard Worker static const char *tracefs_available_filter_functions_addrs(void)
10909*f7c14bbaSAndroid Build Coastguard Worker {
10910*f7c14bbaSAndroid Build Coastguard Worker return use_debugfs() ? DEBUGFS"/available_filter_functions_addrs"
10911*f7c14bbaSAndroid Build Coastguard Worker : TRACEFS"/available_filter_functions_addrs";
10912*f7c14bbaSAndroid Build Coastguard Worker }
10913*f7c14bbaSAndroid Build Coastguard Worker
gen_kprobe_legacy_event_name(char * buf,size_t buf_sz,const char * kfunc_name,size_t offset)10914*f7c14bbaSAndroid Build Coastguard Worker static void gen_kprobe_legacy_event_name(char *buf, size_t buf_sz,
10915*f7c14bbaSAndroid Build Coastguard Worker const char *kfunc_name, size_t offset)
10916*f7c14bbaSAndroid Build Coastguard Worker {
10917*f7c14bbaSAndroid Build Coastguard Worker static int index = 0;
10918*f7c14bbaSAndroid Build Coastguard Worker int i;
10919*f7c14bbaSAndroid Build Coastguard Worker
10920*f7c14bbaSAndroid Build Coastguard Worker snprintf(buf, buf_sz, "libbpf_%u_%s_0x%zx_%d", getpid(), kfunc_name, offset,
10921*f7c14bbaSAndroid Build Coastguard Worker __sync_fetch_and_add(&index, 1));
10922*f7c14bbaSAndroid Build Coastguard Worker
10923*f7c14bbaSAndroid Build Coastguard Worker /* sanitize binary_path in the probe name */
10924*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; buf[i]; i++) {
10925*f7c14bbaSAndroid Build Coastguard Worker if (!isalnum(buf[i]))
10926*f7c14bbaSAndroid Build Coastguard Worker buf[i] = '_';
10927*f7c14bbaSAndroid Build Coastguard Worker }
10928*f7c14bbaSAndroid Build Coastguard Worker }
10929*f7c14bbaSAndroid Build Coastguard Worker
add_kprobe_event_legacy(const char * probe_name,bool retprobe,const char * kfunc_name,size_t offset)10930*f7c14bbaSAndroid Build Coastguard Worker static int add_kprobe_event_legacy(const char *probe_name, bool retprobe,
10931*f7c14bbaSAndroid Build Coastguard Worker const char *kfunc_name, size_t offset)
10932*f7c14bbaSAndroid Build Coastguard Worker {
10933*f7c14bbaSAndroid Build Coastguard Worker return append_to_file(tracefs_kprobe_events(), "%c:%s/%s %s+0x%zx",
10934*f7c14bbaSAndroid Build Coastguard Worker retprobe ? 'r' : 'p',
10935*f7c14bbaSAndroid Build Coastguard Worker retprobe ? "kretprobes" : "kprobes",
10936*f7c14bbaSAndroid Build Coastguard Worker probe_name, kfunc_name, offset);
10937*f7c14bbaSAndroid Build Coastguard Worker }
10938*f7c14bbaSAndroid Build Coastguard Worker
remove_kprobe_event_legacy(const char * probe_name,bool retprobe)10939*f7c14bbaSAndroid Build Coastguard Worker static int remove_kprobe_event_legacy(const char *probe_name, bool retprobe)
10940*f7c14bbaSAndroid Build Coastguard Worker {
10941*f7c14bbaSAndroid Build Coastguard Worker return append_to_file(tracefs_kprobe_events(), "-:%s/%s",
10942*f7c14bbaSAndroid Build Coastguard Worker retprobe ? "kretprobes" : "kprobes", probe_name);
10943*f7c14bbaSAndroid Build Coastguard Worker }
10944*f7c14bbaSAndroid Build Coastguard Worker
determine_kprobe_perf_type_legacy(const char * probe_name,bool retprobe)10945*f7c14bbaSAndroid Build Coastguard Worker static int determine_kprobe_perf_type_legacy(const char *probe_name, bool retprobe)
10946*f7c14bbaSAndroid Build Coastguard Worker {
10947*f7c14bbaSAndroid Build Coastguard Worker char file[256];
10948*f7c14bbaSAndroid Build Coastguard Worker
10949*f7c14bbaSAndroid Build Coastguard Worker snprintf(file, sizeof(file), "%s/events/%s/%s/id",
10950*f7c14bbaSAndroid Build Coastguard Worker tracefs_path(), retprobe ? "kretprobes" : "kprobes", probe_name);
10951*f7c14bbaSAndroid Build Coastguard Worker
10952*f7c14bbaSAndroid Build Coastguard Worker return parse_uint_from_file(file, "%d\n");
10953*f7c14bbaSAndroid Build Coastguard Worker }
10954*f7c14bbaSAndroid Build Coastguard Worker
perf_event_kprobe_open_legacy(const char * probe_name,bool retprobe,const char * kfunc_name,size_t offset,int pid)10955*f7c14bbaSAndroid Build Coastguard Worker static int perf_event_kprobe_open_legacy(const char *probe_name, bool retprobe,
10956*f7c14bbaSAndroid Build Coastguard Worker const char *kfunc_name, size_t offset, int pid)
10957*f7c14bbaSAndroid Build Coastguard Worker {
10958*f7c14bbaSAndroid Build Coastguard Worker const size_t attr_sz = sizeof(struct perf_event_attr);
10959*f7c14bbaSAndroid Build Coastguard Worker struct perf_event_attr attr;
10960*f7c14bbaSAndroid Build Coastguard Worker char errmsg[STRERR_BUFSIZE];
10961*f7c14bbaSAndroid Build Coastguard Worker int type, pfd, err;
10962*f7c14bbaSAndroid Build Coastguard Worker
10963*f7c14bbaSAndroid Build Coastguard Worker err = add_kprobe_event_legacy(probe_name, retprobe, kfunc_name, offset);
10964*f7c14bbaSAndroid Build Coastguard Worker if (err < 0) {
10965*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to add legacy kprobe event for '%s+0x%zx': %s\n",
10966*f7c14bbaSAndroid Build Coastguard Worker kfunc_name, offset,
10967*f7c14bbaSAndroid Build Coastguard Worker libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
10968*f7c14bbaSAndroid Build Coastguard Worker return err;
10969*f7c14bbaSAndroid Build Coastguard Worker }
10970*f7c14bbaSAndroid Build Coastguard Worker type = determine_kprobe_perf_type_legacy(probe_name, retprobe);
10971*f7c14bbaSAndroid Build Coastguard Worker if (type < 0) {
10972*f7c14bbaSAndroid Build Coastguard Worker err = type;
10973*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to determine legacy kprobe event id for '%s+0x%zx': %s\n",
10974*f7c14bbaSAndroid Build Coastguard Worker kfunc_name, offset,
10975*f7c14bbaSAndroid Build Coastguard Worker libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
10976*f7c14bbaSAndroid Build Coastguard Worker goto err_clean_legacy;
10977*f7c14bbaSAndroid Build Coastguard Worker }
10978*f7c14bbaSAndroid Build Coastguard Worker
10979*f7c14bbaSAndroid Build Coastguard Worker memset(&attr, 0, attr_sz);
10980*f7c14bbaSAndroid Build Coastguard Worker attr.size = attr_sz;
10981*f7c14bbaSAndroid Build Coastguard Worker attr.config = type;
10982*f7c14bbaSAndroid Build Coastguard Worker attr.type = PERF_TYPE_TRACEPOINT;
10983*f7c14bbaSAndroid Build Coastguard Worker
10984*f7c14bbaSAndroid Build Coastguard Worker pfd = syscall(__NR_perf_event_open, &attr,
10985*f7c14bbaSAndroid Build Coastguard Worker pid < 0 ? -1 : pid, /* pid */
10986*f7c14bbaSAndroid Build Coastguard Worker pid == -1 ? 0 : -1, /* cpu */
10987*f7c14bbaSAndroid Build Coastguard Worker -1 /* group_fd */, PERF_FLAG_FD_CLOEXEC);
10988*f7c14bbaSAndroid Build Coastguard Worker if (pfd < 0) {
10989*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
10990*f7c14bbaSAndroid Build Coastguard Worker pr_warn("legacy kprobe perf_event_open() failed: %s\n",
10991*f7c14bbaSAndroid Build Coastguard Worker libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
10992*f7c14bbaSAndroid Build Coastguard Worker goto err_clean_legacy;
10993*f7c14bbaSAndroid Build Coastguard Worker }
10994*f7c14bbaSAndroid Build Coastguard Worker return pfd;
10995*f7c14bbaSAndroid Build Coastguard Worker
10996*f7c14bbaSAndroid Build Coastguard Worker err_clean_legacy:
10997*f7c14bbaSAndroid Build Coastguard Worker /* Clear the newly added legacy kprobe_event */
10998*f7c14bbaSAndroid Build Coastguard Worker remove_kprobe_event_legacy(probe_name, retprobe);
10999*f7c14bbaSAndroid Build Coastguard Worker return err;
11000*f7c14bbaSAndroid Build Coastguard Worker }
11001*f7c14bbaSAndroid Build Coastguard Worker
arch_specific_syscall_pfx(void)11002*f7c14bbaSAndroid Build Coastguard Worker static const char *arch_specific_syscall_pfx(void)
11003*f7c14bbaSAndroid Build Coastguard Worker {
11004*f7c14bbaSAndroid Build Coastguard Worker #if defined(__x86_64__)
11005*f7c14bbaSAndroid Build Coastguard Worker return "x64";
11006*f7c14bbaSAndroid Build Coastguard Worker #elif defined(__i386__)
11007*f7c14bbaSAndroid Build Coastguard Worker return "ia32";
11008*f7c14bbaSAndroid Build Coastguard Worker #elif defined(__s390x__)
11009*f7c14bbaSAndroid Build Coastguard Worker return "s390x";
11010*f7c14bbaSAndroid Build Coastguard Worker #elif defined(__s390__)
11011*f7c14bbaSAndroid Build Coastguard Worker return "s390";
11012*f7c14bbaSAndroid Build Coastguard Worker #elif defined(__arm__)
11013*f7c14bbaSAndroid Build Coastguard Worker return "arm";
11014*f7c14bbaSAndroid Build Coastguard Worker #elif defined(__aarch64__)
11015*f7c14bbaSAndroid Build Coastguard Worker return "arm64";
11016*f7c14bbaSAndroid Build Coastguard Worker #elif defined(__mips__)
11017*f7c14bbaSAndroid Build Coastguard Worker return "mips";
11018*f7c14bbaSAndroid Build Coastguard Worker #elif defined(__riscv)
11019*f7c14bbaSAndroid Build Coastguard Worker return "riscv";
11020*f7c14bbaSAndroid Build Coastguard Worker #elif defined(__powerpc__)
11021*f7c14bbaSAndroid Build Coastguard Worker return "powerpc";
11022*f7c14bbaSAndroid Build Coastguard Worker #elif defined(__powerpc64__)
11023*f7c14bbaSAndroid Build Coastguard Worker return "powerpc64";
11024*f7c14bbaSAndroid Build Coastguard Worker #else
11025*f7c14bbaSAndroid Build Coastguard Worker return NULL;
11026*f7c14bbaSAndroid Build Coastguard Worker #endif
11027*f7c14bbaSAndroid Build Coastguard Worker }
11028*f7c14bbaSAndroid Build Coastguard Worker
probe_kern_syscall_wrapper(int token_fd)11029*f7c14bbaSAndroid Build Coastguard Worker int probe_kern_syscall_wrapper(int token_fd)
11030*f7c14bbaSAndroid Build Coastguard Worker {
11031*f7c14bbaSAndroid Build Coastguard Worker char syscall_name[64];
11032*f7c14bbaSAndroid Build Coastguard Worker const char *ksys_pfx;
11033*f7c14bbaSAndroid Build Coastguard Worker
11034*f7c14bbaSAndroid Build Coastguard Worker ksys_pfx = arch_specific_syscall_pfx();
11035*f7c14bbaSAndroid Build Coastguard Worker if (!ksys_pfx)
11036*f7c14bbaSAndroid Build Coastguard Worker return 0;
11037*f7c14bbaSAndroid Build Coastguard Worker
11038*f7c14bbaSAndroid Build Coastguard Worker snprintf(syscall_name, sizeof(syscall_name), "__%s_sys_bpf", ksys_pfx);
11039*f7c14bbaSAndroid Build Coastguard Worker
11040*f7c14bbaSAndroid Build Coastguard Worker if (determine_kprobe_perf_type() >= 0) {
11041*f7c14bbaSAndroid Build Coastguard Worker int pfd;
11042*f7c14bbaSAndroid Build Coastguard Worker
11043*f7c14bbaSAndroid Build Coastguard Worker pfd = perf_event_open_probe(false, false, syscall_name, 0, getpid(), 0);
11044*f7c14bbaSAndroid Build Coastguard Worker if (pfd >= 0)
11045*f7c14bbaSAndroid Build Coastguard Worker close(pfd);
11046*f7c14bbaSAndroid Build Coastguard Worker
11047*f7c14bbaSAndroid Build Coastguard Worker return pfd >= 0 ? 1 : 0;
11048*f7c14bbaSAndroid Build Coastguard Worker } else { /* legacy mode */
11049*f7c14bbaSAndroid Build Coastguard Worker char probe_name[128];
11050*f7c14bbaSAndroid Build Coastguard Worker
11051*f7c14bbaSAndroid Build Coastguard Worker gen_kprobe_legacy_event_name(probe_name, sizeof(probe_name), syscall_name, 0);
11052*f7c14bbaSAndroid Build Coastguard Worker if (add_kprobe_event_legacy(probe_name, false, syscall_name, 0) < 0)
11053*f7c14bbaSAndroid Build Coastguard Worker return 0;
11054*f7c14bbaSAndroid Build Coastguard Worker
11055*f7c14bbaSAndroid Build Coastguard Worker (void)remove_kprobe_event_legacy(probe_name, false);
11056*f7c14bbaSAndroid Build Coastguard Worker return 1;
11057*f7c14bbaSAndroid Build Coastguard Worker }
11058*f7c14bbaSAndroid Build Coastguard Worker }
11059*f7c14bbaSAndroid Build Coastguard Worker
11060*f7c14bbaSAndroid Build Coastguard Worker struct bpf_link *
bpf_program__attach_kprobe_opts(const struct bpf_program * prog,const char * func_name,const struct bpf_kprobe_opts * opts)11061*f7c14bbaSAndroid Build Coastguard Worker bpf_program__attach_kprobe_opts(const struct bpf_program *prog,
11062*f7c14bbaSAndroid Build Coastguard Worker const char *func_name,
11063*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_kprobe_opts *opts)
11064*f7c14bbaSAndroid Build Coastguard Worker {
11065*f7c14bbaSAndroid Build Coastguard Worker DECLARE_LIBBPF_OPTS(bpf_perf_event_opts, pe_opts);
11066*f7c14bbaSAndroid Build Coastguard Worker enum probe_attach_mode attach_mode;
11067*f7c14bbaSAndroid Build Coastguard Worker char errmsg[STRERR_BUFSIZE];
11068*f7c14bbaSAndroid Build Coastguard Worker char *legacy_probe = NULL;
11069*f7c14bbaSAndroid Build Coastguard Worker struct bpf_link *link;
11070*f7c14bbaSAndroid Build Coastguard Worker size_t offset;
11071*f7c14bbaSAndroid Build Coastguard Worker bool retprobe, legacy;
11072*f7c14bbaSAndroid Build Coastguard Worker int pfd, err;
11073*f7c14bbaSAndroid Build Coastguard Worker
11074*f7c14bbaSAndroid Build Coastguard Worker if (!OPTS_VALID(opts, bpf_kprobe_opts))
11075*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(-EINVAL);
11076*f7c14bbaSAndroid Build Coastguard Worker
11077*f7c14bbaSAndroid Build Coastguard Worker attach_mode = OPTS_GET(opts, attach_mode, PROBE_ATTACH_MODE_DEFAULT);
11078*f7c14bbaSAndroid Build Coastguard Worker retprobe = OPTS_GET(opts, retprobe, false);
11079*f7c14bbaSAndroid Build Coastguard Worker offset = OPTS_GET(opts, offset, 0);
11080*f7c14bbaSAndroid Build Coastguard Worker pe_opts.bpf_cookie = OPTS_GET(opts, bpf_cookie, 0);
11081*f7c14bbaSAndroid Build Coastguard Worker
11082*f7c14bbaSAndroid Build Coastguard Worker legacy = determine_kprobe_perf_type() < 0;
11083*f7c14bbaSAndroid Build Coastguard Worker switch (attach_mode) {
11084*f7c14bbaSAndroid Build Coastguard Worker case PROBE_ATTACH_MODE_LEGACY:
11085*f7c14bbaSAndroid Build Coastguard Worker legacy = true;
11086*f7c14bbaSAndroid Build Coastguard Worker pe_opts.force_ioctl_attach = true;
11087*f7c14bbaSAndroid Build Coastguard Worker break;
11088*f7c14bbaSAndroid Build Coastguard Worker case PROBE_ATTACH_MODE_PERF:
11089*f7c14bbaSAndroid Build Coastguard Worker if (legacy)
11090*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(-ENOTSUP);
11091*f7c14bbaSAndroid Build Coastguard Worker pe_opts.force_ioctl_attach = true;
11092*f7c14bbaSAndroid Build Coastguard Worker break;
11093*f7c14bbaSAndroid Build Coastguard Worker case PROBE_ATTACH_MODE_LINK:
11094*f7c14bbaSAndroid Build Coastguard Worker if (legacy || !kernel_supports(prog->obj, FEAT_PERF_LINK))
11095*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(-ENOTSUP);
11096*f7c14bbaSAndroid Build Coastguard Worker break;
11097*f7c14bbaSAndroid Build Coastguard Worker case PROBE_ATTACH_MODE_DEFAULT:
11098*f7c14bbaSAndroid Build Coastguard Worker break;
11099*f7c14bbaSAndroid Build Coastguard Worker default:
11100*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(-EINVAL);
11101*f7c14bbaSAndroid Build Coastguard Worker }
11102*f7c14bbaSAndroid Build Coastguard Worker
11103*f7c14bbaSAndroid Build Coastguard Worker if (!legacy) {
11104*f7c14bbaSAndroid Build Coastguard Worker pfd = perf_event_open_probe(false /* uprobe */, retprobe,
11105*f7c14bbaSAndroid Build Coastguard Worker func_name, offset,
11106*f7c14bbaSAndroid Build Coastguard Worker -1 /* pid */, 0 /* ref_ctr_off */);
11107*f7c14bbaSAndroid Build Coastguard Worker } else {
11108*f7c14bbaSAndroid Build Coastguard Worker char probe_name[256];
11109*f7c14bbaSAndroid Build Coastguard Worker
11110*f7c14bbaSAndroid Build Coastguard Worker gen_kprobe_legacy_event_name(probe_name, sizeof(probe_name),
11111*f7c14bbaSAndroid Build Coastguard Worker func_name, offset);
11112*f7c14bbaSAndroid Build Coastguard Worker
11113*f7c14bbaSAndroid Build Coastguard Worker legacy_probe = strdup(probe_name);
11114*f7c14bbaSAndroid Build Coastguard Worker if (!legacy_probe)
11115*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(-ENOMEM);
11116*f7c14bbaSAndroid Build Coastguard Worker
11117*f7c14bbaSAndroid Build Coastguard Worker pfd = perf_event_kprobe_open_legacy(legacy_probe, retprobe, func_name,
11118*f7c14bbaSAndroid Build Coastguard Worker offset, -1 /* pid */);
11119*f7c14bbaSAndroid Build Coastguard Worker }
11120*f7c14bbaSAndroid Build Coastguard Worker if (pfd < 0) {
11121*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
11122*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': failed to create %s '%s+0x%zx' perf event: %s\n",
11123*f7c14bbaSAndroid Build Coastguard Worker prog->name, retprobe ? "kretprobe" : "kprobe",
11124*f7c14bbaSAndroid Build Coastguard Worker func_name, offset,
11125*f7c14bbaSAndroid Build Coastguard Worker libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
11126*f7c14bbaSAndroid Build Coastguard Worker goto err_out;
11127*f7c14bbaSAndroid Build Coastguard Worker }
11128*f7c14bbaSAndroid Build Coastguard Worker link = bpf_program__attach_perf_event_opts(prog, pfd, &pe_opts);
11129*f7c14bbaSAndroid Build Coastguard Worker err = libbpf_get_error(link);
11130*f7c14bbaSAndroid Build Coastguard Worker if (err) {
11131*f7c14bbaSAndroid Build Coastguard Worker close(pfd);
11132*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': failed to attach to %s '%s+0x%zx': %s\n",
11133*f7c14bbaSAndroid Build Coastguard Worker prog->name, retprobe ? "kretprobe" : "kprobe",
11134*f7c14bbaSAndroid Build Coastguard Worker func_name, offset,
11135*f7c14bbaSAndroid Build Coastguard Worker libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
11136*f7c14bbaSAndroid Build Coastguard Worker goto err_clean_legacy;
11137*f7c14bbaSAndroid Build Coastguard Worker }
11138*f7c14bbaSAndroid Build Coastguard Worker if (legacy) {
11139*f7c14bbaSAndroid Build Coastguard Worker struct bpf_link_perf *perf_link = container_of(link, struct bpf_link_perf, link);
11140*f7c14bbaSAndroid Build Coastguard Worker
11141*f7c14bbaSAndroid Build Coastguard Worker perf_link->legacy_probe_name = legacy_probe;
11142*f7c14bbaSAndroid Build Coastguard Worker perf_link->legacy_is_kprobe = true;
11143*f7c14bbaSAndroid Build Coastguard Worker perf_link->legacy_is_retprobe = retprobe;
11144*f7c14bbaSAndroid Build Coastguard Worker }
11145*f7c14bbaSAndroid Build Coastguard Worker
11146*f7c14bbaSAndroid Build Coastguard Worker return link;
11147*f7c14bbaSAndroid Build Coastguard Worker
11148*f7c14bbaSAndroid Build Coastguard Worker err_clean_legacy:
11149*f7c14bbaSAndroid Build Coastguard Worker if (legacy)
11150*f7c14bbaSAndroid Build Coastguard Worker remove_kprobe_event_legacy(legacy_probe, retprobe);
11151*f7c14bbaSAndroid Build Coastguard Worker err_out:
11152*f7c14bbaSAndroid Build Coastguard Worker free(legacy_probe);
11153*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(err);
11154*f7c14bbaSAndroid Build Coastguard Worker }
11155*f7c14bbaSAndroid Build Coastguard Worker
bpf_program__attach_kprobe(const struct bpf_program * prog,bool retprobe,const char * func_name)11156*f7c14bbaSAndroid Build Coastguard Worker struct bpf_link *bpf_program__attach_kprobe(const struct bpf_program *prog,
11157*f7c14bbaSAndroid Build Coastguard Worker bool retprobe,
11158*f7c14bbaSAndroid Build Coastguard Worker const char *func_name)
11159*f7c14bbaSAndroid Build Coastguard Worker {
11160*f7c14bbaSAndroid Build Coastguard Worker DECLARE_LIBBPF_OPTS(bpf_kprobe_opts, opts,
11161*f7c14bbaSAndroid Build Coastguard Worker .retprobe = retprobe,
11162*f7c14bbaSAndroid Build Coastguard Worker );
11163*f7c14bbaSAndroid Build Coastguard Worker
11164*f7c14bbaSAndroid Build Coastguard Worker return bpf_program__attach_kprobe_opts(prog, func_name, &opts);
11165*f7c14bbaSAndroid Build Coastguard Worker }
11166*f7c14bbaSAndroid Build Coastguard Worker
bpf_program__attach_ksyscall(const struct bpf_program * prog,const char * syscall_name,const struct bpf_ksyscall_opts * opts)11167*f7c14bbaSAndroid Build Coastguard Worker struct bpf_link *bpf_program__attach_ksyscall(const struct bpf_program *prog,
11168*f7c14bbaSAndroid Build Coastguard Worker const char *syscall_name,
11169*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_ksyscall_opts *opts)
11170*f7c14bbaSAndroid Build Coastguard Worker {
11171*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_OPTS(bpf_kprobe_opts, kprobe_opts);
11172*f7c14bbaSAndroid Build Coastguard Worker char func_name[128];
11173*f7c14bbaSAndroid Build Coastguard Worker
11174*f7c14bbaSAndroid Build Coastguard Worker if (!OPTS_VALID(opts, bpf_ksyscall_opts))
11175*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(-EINVAL);
11176*f7c14bbaSAndroid Build Coastguard Worker
11177*f7c14bbaSAndroid Build Coastguard Worker if (kernel_supports(prog->obj, FEAT_SYSCALL_WRAPPER)) {
11178*f7c14bbaSAndroid Build Coastguard Worker /* arch_specific_syscall_pfx() should never return NULL here
11179*f7c14bbaSAndroid Build Coastguard Worker * because it is guarded by kernel_supports(). However, since
11180*f7c14bbaSAndroid Build Coastguard Worker * compiler does not know that we have an explicit conditional
11181*f7c14bbaSAndroid Build Coastguard Worker * as well.
11182*f7c14bbaSAndroid Build Coastguard Worker */
11183*f7c14bbaSAndroid Build Coastguard Worker snprintf(func_name, sizeof(func_name), "__%s_sys_%s",
11184*f7c14bbaSAndroid Build Coastguard Worker arch_specific_syscall_pfx() ? : "", syscall_name);
11185*f7c14bbaSAndroid Build Coastguard Worker } else {
11186*f7c14bbaSAndroid Build Coastguard Worker snprintf(func_name, sizeof(func_name), "__se_sys_%s", syscall_name);
11187*f7c14bbaSAndroid Build Coastguard Worker }
11188*f7c14bbaSAndroid Build Coastguard Worker
11189*f7c14bbaSAndroid Build Coastguard Worker kprobe_opts.retprobe = OPTS_GET(opts, retprobe, false);
11190*f7c14bbaSAndroid Build Coastguard Worker kprobe_opts.bpf_cookie = OPTS_GET(opts, bpf_cookie, 0);
11191*f7c14bbaSAndroid Build Coastguard Worker
11192*f7c14bbaSAndroid Build Coastguard Worker return bpf_program__attach_kprobe_opts(prog, func_name, &kprobe_opts);
11193*f7c14bbaSAndroid Build Coastguard Worker }
11194*f7c14bbaSAndroid Build Coastguard Worker
11195*f7c14bbaSAndroid Build Coastguard Worker /* Adapted from perf/util/string.c */
glob_match(const char * str,const char * pat)11196*f7c14bbaSAndroid Build Coastguard Worker bool glob_match(const char *str, const char *pat)
11197*f7c14bbaSAndroid Build Coastguard Worker {
11198*f7c14bbaSAndroid Build Coastguard Worker while (*str && *pat && *pat != '*') {
11199*f7c14bbaSAndroid Build Coastguard Worker if (*pat == '?') { /* Matches any single character */
11200*f7c14bbaSAndroid Build Coastguard Worker str++;
11201*f7c14bbaSAndroid Build Coastguard Worker pat++;
11202*f7c14bbaSAndroid Build Coastguard Worker continue;
11203*f7c14bbaSAndroid Build Coastguard Worker }
11204*f7c14bbaSAndroid Build Coastguard Worker if (*str != *pat)
11205*f7c14bbaSAndroid Build Coastguard Worker return false;
11206*f7c14bbaSAndroid Build Coastguard Worker str++;
11207*f7c14bbaSAndroid Build Coastguard Worker pat++;
11208*f7c14bbaSAndroid Build Coastguard Worker }
11209*f7c14bbaSAndroid Build Coastguard Worker /* Check wild card */
11210*f7c14bbaSAndroid Build Coastguard Worker if (*pat == '*') {
11211*f7c14bbaSAndroid Build Coastguard Worker while (*pat == '*')
11212*f7c14bbaSAndroid Build Coastguard Worker pat++;
11213*f7c14bbaSAndroid Build Coastguard Worker if (!*pat) /* Tail wild card matches all */
11214*f7c14bbaSAndroid Build Coastguard Worker return true;
11215*f7c14bbaSAndroid Build Coastguard Worker while (*str)
11216*f7c14bbaSAndroid Build Coastguard Worker if (glob_match(str++, pat))
11217*f7c14bbaSAndroid Build Coastguard Worker return true;
11218*f7c14bbaSAndroid Build Coastguard Worker }
11219*f7c14bbaSAndroid Build Coastguard Worker return !*str && !*pat;
11220*f7c14bbaSAndroid Build Coastguard Worker }
11221*f7c14bbaSAndroid Build Coastguard Worker
11222*f7c14bbaSAndroid Build Coastguard Worker struct kprobe_multi_resolve {
11223*f7c14bbaSAndroid Build Coastguard Worker const char *pattern;
11224*f7c14bbaSAndroid Build Coastguard Worker unsigned long *addrs;
11225*f7c14bbaSAndroid Build Coastguard Worker size_t cap;
11226*f7c14bbaSAndroid Build Coastguard Worker size_t cnt;
11227*f7c14bbaSAndroid Build Coastguard Worker };
11228*f7c14bbaSAndroid Build Coastguard Worker
11229*f7c14bbaSAndroid Build Coastguard Worker struct avail_kallsyms_data {
11230*f7c14bbaSAndroid Build Coastguard Worker char **syms;
11231*f7c14bbaSAndroid Build Coastguard Worker size_t cnt;
11232*f7c14bbaSAndroid Build Coastguard Worker struct kprobe_multi_resolve *res;
11233*f7c14bbaSAndroid Build Coastguard Worker };
11234*f7c14bbaSAndroid Build Coastguard Worker
avail_func_cmp(const void * a,const void * b)11235*f7c14bbaSAndroid Build Coastguard Worker static int avail_func_cmp(const void *a, const void *b)
11236*f7c14bbaSAndroid Build Coastguard Worker {
11237*f7c14bbaSAndroid Build Coastguard Worker return strcmp(*(const char **)a, *(const char **)b);
11238*f7c14bbaSAndroid Build Coastguard Worker }
11239*f7c14bbaSAndroid Build Coastguard Worker
avail_kallsyms_cb(unsigned long long sym_addr,char sym_type,const char * sym_name,void * ctx)11240*f7c14bbaSAndroid Build Coastguard Worker static int avail_kallsyms_cb(unsigned long long sym_addr, char sym_type,
11241*f7c14bbaSAndroid Build Coastguard Worker const char *sym_name, void *ctx)
11242*f7c14bbaSAndroid Build Coastguard Worker {
11243*f7c14bbaSAndroid Build Coastguard Worker struct avail_kallsyms_data *data = ctx;
11244*f7c14bbaSAndroid Build Coastguard Worker struct kprobe_multi_resolve *res = data->res;
11245*f7c14bbaSAndroid Build Coastguard Worker int err;
11246*f7c14bbaSAndroid Build Coastguard Worker
11247*f7c14bbaSAndroid Build Coastguard Worker if (!bsearch(&sym_name, data->syms, data->cnt, sizeof(*data->syms), avail_func_cmp))
11248*f7c14bbaSAndroid Build Coastguard Worker return 0;
11249*f7c14bbaSAndroid Build Coastguard Worker
11250*f7c14bbaSAndroid Build Coastguard Worker err = libbpf_ensure_mem((void **)&res->addrs, &res->cap, sizeof(*res->addrs), res->cnt + 1);
11251*f7c14bbaSAndroid Build Coastguard Worker if (err)
11252*f7c14bbaSAndroid Build Coastguard Worker return err;
11253*f7c14bbaSAndroid Build Coastguard Worker
11254*f7c14bbaSAndroid Build Coastguard Worker res->addrs[res->cnt++] = (unsigned long)sym_addr;
11255*f7c14bbaSAndroid Build Coastguard Worker return 0;
11256*f7c14bbaSAndroid Build Coastguard Worker }
11257*f7c14bbaSAndroid Build Coastguard Worker
libbpf_available_kallsyms_parse(struct kprobe_multi_resolve * res)11258*f7c14bbaSAndroid Build Coastguard Worker static int libbpf_available_kallsyms_parse(struct kprobe_multi_resolve *res)
11259*f7c14bbaSAndroid Build Coastguard Worker {
11260*f7c14bbaSAndroid Build Coastguard Worker const char *available_functions_file = tracefs_available_filter_functions();
11261*f7c14bbaSAndroid Build Coastguard Worker struct avail_kallsyms_data data;
11262*f7c14bbaSAndroid Build Coastguard Worker char sym_name[500];
11263*f7c14bbaSAndroid Build Coastguard Worker FILE *f;
11264*f7c14bbaSAndroid Build Coastguard Worker int err = 0, ret, i;
11265*f7c14bbaSAndroid Build Coastguard Worker char **syms = NULL;
11266*f7c14bbaSAndroid Build Coastguard Worker size_t cap = 0, cnt = 0;
11267*f7c14bbaSAndroid Build Coastguard Worker
11268*f7c14bbaSAndroid Build Coastguard Worker f = fopen(available_functions_file, "re");
11269*f7c14bbaSAndroid Build Coastguard Worker if (!f) {
11270*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
11271*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to open %s: %d\n", available_functions_file, err);
11272*f7c14bbaSAndroid Build Coastguard Worker return err;
11273*f7c14bbaSAndroid Build Coastguard Worker }
11274*f7c14bbaSAndroid Build Coastguard Worker
11275*f7c14bbaSAndroid Build Coastguard Worker while (true) {
11276*f7c14bbaSAndroid Build Coastguard Worker char *name;
11277*f7c14bbaSAndroid Build Coastguard Worker
11278*f7c14bbaSAndroid Build Coastguard Worker ret = fscanf(f, "%499s%*[^\n]\n", sym_name);
11279*f7c14bbaSAndroid Build Coastguard Worker if (ret == EOF && feof(f))
11280*f7c14bbaSAndroid Build Coastguard Worker break;
11281*f7c14bbaSAndroid Build Coastguard Worker
11282*f7c14bbaSAndroid Build Coastguard Worker if (ret != 1) {
11283*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to parse available_filter_functions entry: %d\n", ret);
11284*f7c14bbaSAndroid Build Coastguard Worker err = -EINVAL;
11285*f7c14bbaSAndroid Build Coastguard Worker goto cleanup;
11286*f7c14bbaSAndroid Build Coastguard Worker }
11287*f7c14bbaSAndroid Build Coastguard Worker
11288*f7c14bbaSAndroid Build Coastguard Worker if (!glob_match(sym_name, res->pattern))
11289*f7c14bbaSAndroid Build Coastguard Worker continue;
11290*f7c14bbaSAndroid Build Coastguard Worker
11291*f7c14bbaSAndroid Build Coastguard Worker err = libbpf_ensure_mem((void **)&syms, &cap, sizeof(*syms), cnt + 1);
11292*f7c14bbaSAndroid Build Coastguard Worker if (err)
11293*f7c14bbaSAndroid Build Coastguard Worker goto cleanup;
11294*f7c14bbaSAndroid Build Coastguard Worker
11295*f7c14bbaSAndroid Build Coastguard Worker name = strdup(sym_name);
11296*f7c14bbaSAndroid Build Coastguard Worker if (!name) {
11297*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
11298*f7c14bbaSAndroid Build Coastguard Worker goto cleanup;
11299*f7c14bbaSAndroid Build Coastguard Worker }
11300*f7c14bbaSAndroid Build Coastguard Worker
11301*f7c14bbaSAndroid Build Coastguard Worker syms[cnt++] = name;
11302*f7c14bbaSAndroid Build Coastguard Worker }
11303*f7c14bbaSAndroid Build Coastguard Worker
11304*f7c14bbaSAndroid Build Coastguard Worker /* no entries found, bail out */
11305*f7c14bbaSAndroid Build Coastguard Worker if (cnt == 0) {
11306*f7c14bbaSAndroid Build Coastguard Worker err = -ENOENT;
11307*f7c14bbaSAndroid Build Coastguard Worker goto cleanup;
11308*f7c14bbaSAndroid Build Coastguard Worker }
11309*f7c14bbaSAndroid Build Coastguard Worker
11310*f7c14bbaSAndroid Build Coastguard Worker /* sort available functions */
11311*f7c14bbaSAndroid Build Coastguard Worker qsort(syms, cnt, sizeof(*syms), avail_func_cmp);
11312*f7c14bbaSAndroid Build Coastguard Worker
11313*f7c14bbaSAndroid Build Coastguard Worker data.syms = syms;
11314*f7c14bbaSAndroid Build Coastguard Worker data.res = res;
11315*f7c14bbaSAndroid Build Coastguard Worker data.cnt = cnt;
11316*f7c14bbaSAndroid Build Coastguard Worker libbpf_kallsyms_parse(avail_kallsyms_cb, &data);
11317*f7c14bbaSAndroid Build Coastguard Worker
11318*f7c14bbaSAndroid Build Coastguard Worker if (res->cnt == 0)
11319*f7c14bbaSAndroid Build Coastguard Worker err = -ENOENT;
11320*f7c14bbaSAndroid Build Coastguard Worker
11321*f7c14bbaSAndroid Build Coastguard Worker cleanup:
11322*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < cnt; i++)
11323*f7c14bbaSAndroid Build Coastguard Worker free((char *)syms[i]);
11324*f7c14bbaSAndroid Build Coastguard Worker free(syms);
11325*f7c14bbaSAndroid Build Coastguard Worker
11326*f7c14bbaSAndroid Build Coastguard Worker fclose(f);
11327*f7c14bbaSAndroid Build Coastguard Worker return err;
11328*f7c14bbaSAndroid Build Coastguard Worker }
11329*f7c14bbaSAndroid Build Coastguard Worker
has_available_filter_functions_addrs(void)11330*f7c14bbaSAndroid Build Coastguard Worker static bool has_available_filter_functions_addrs(void)
11331*f7c14bbaSAndroid Build Coastguard Worker {
11332*f7c14bbaSAndroid Build Coastguard Worker return access(tracefs_available_filter_functions_addrs(), R_OK) != -1;
11333*f7c14bbaSAndroid Build Coastguard Worker }
11334*f7c14bbaSAndroid Build Coastguard Worker
libbpf_available_kprobes_parse(struct kprobe_multi_resolve * res)11335*f7c14bbaSAndroid Build Coastguard Worker static int libbpf_available_kprobes_parse(struct kprobe_multi_resolve *res)
11336*f7c14bbaSAndroid Build Coastguard Worker {
11337*f7c14bbaSAndroid Build Coastguard Worker const char *available_path = tracefs_available_filter_functions_addrs();
11338*f7c14bbaSAndroid Build Coastguard Worker char sym_name[500];
11339*f7c14bbaSAndroid Build Coastguard Worker FILE *f;
11340*f7c14bbaSAndroid Build Coastguard Worker int ret, err = 0;
11341*f7c14bbaSAndroid Build Coastguard Worker unsigned long long sym_addr;
11342*f7c14bbaSAndroid Build Coastguard Worker
11343*f7c14bbaSAndroid Build Coastguard Worker f = fopen(available_path, "re");
11344*f7c14bbaSAndroid Build Coastguard Worker if (!f) {
11345*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
11346*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to open %s: %d\n", available_path, err);
11347*f7c14bbaSAndroid Build Coastguard Worker return err;
11348*f7c14bbaSAndroid Build Coastguard Worker }
11349*f7c14bbaSAndroid Build Coastguard Worker
11350*f7c14bbaSAndroid Build Coastguard Worker while (true) {
11351*f7c14bbaSAndroid Build Coastguard Worker ret = fscanf(f, "%llx %499s%*[^\n]\n", &sym_addr, sym_name);
11352*f7c14bbaSAndroid Build Coastguard Worker if (ret == EOF && feof(f))
11353*f7c14bbaSAndroid Build Coastguard Worker break;
11354*f7c14bbaSAndroid Build Coastguard Worker
11355*f7c14bbaSAndroid Build Coastguard Worker if (ret != 2) {
11356*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to parse available_filter_functions_addrs entry: %d\n",
11357*f7c14bbaSAndroid Build Coastguard Worker ret);
11358*f7c14bbaSAndroid Build Coastguard Worker err = -EINVAL;
11359*f7c14bbaSAndroid Build Coastguard Worker goto cleanup;
11360*f7c14bbaSAndroid Build Coastguard Worker }
11361*f7c14bbaSAndroid Build Coastguard Worker
11362*f7c14bbaSAndroid Build Coastguard Worker if (!glob_match(sym_name, res->pattern))
11363*f7c14bbaSAndroid Build Coastguard Worker continue;
11364*f7c14bbaSAndroid Build Coastguard Worker
11365*f7c14bbaSAndroid Build Coastguard Worker err = libbpf_ensure_mem((void **)&res->addrs, &res->cap,
11366*f7c14bbaSAndroid Build Coastguard Worker sizeof(*res->addrs), res->cnt + 1);
11367*f7c14bbaSAndroid Build Coastguard Worker if (err)
11368*f7c14bbaSAndroid Build Coastguard Worker goto cleanup;
11369*f7c14bbaSAndroid Build Coastguard Worker
11370*f7c14bbaSAndroid Build Coastguard Worker res->addrs[res->cnt++] = (unsigned long)sym_addr;
11371*f7c14bbaSAndroid Build Coastguard Worker }
11372*f7c14bbaSAndroid Build Coastguard Worker
11373*f7c14bbaSAndroid Build Coastguard Worker if (res->cnt == 0)
11374*f7c14bbaSAndroid Build Coastguard Worker err = -ENOENT;
11375*f7c14bbaSAndroid Build Coastguard Worker
11376*f7c14bbaSAndroid Build Coastguard Worker cleanup:
11377*f7c14bbaSAndroid Build Coastguard Worker fclose(f);
11378*f7c14bbaSAndroid Build Coastguard Worker return err;
11379*f7c14bbaSAndroid Build Coastguard Worker }
11380*f7c14bbaSAndroid Build Coastguard Worker
11381*f7c14bbaSAndroid Build Coastguard Worker struct bpf_link *
bpf_program__attach_kprobe_multi_opts(const struct bpf_program * prog,const char * pattern,const struct bpf_kprobe_multi_opts * opts)11382*f7c14bbaSAndroid Build Coastguard Worker bpf_program__attach_kprobe_multi_opts(const struct bpf_program *prog,
11383*f7c14bbaSAndroid Build Coastguard Worker const char *pattern,
11384*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_kprobe_multi_opts *opts)
11385*f7c14bbaSAndroid Build Coastguard Worker {
11386*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_OPTS(bpf_link_create_opts, lopts);
11387*f7c14bbaSAndroid Build Coastguard Worker struct kprobe_multi_resolve res = {
11388*f7c14bbaSAndroid Build Coastguard Worker .pattern = pattern,
11389*f7c14bbaSAndroid Build Coastguard Worker };
11390*f7c14bbaSAndroid Build Coastguard Worker struct bpf_link *link = NULL;
11391*f7c14bbaSAndroid Build Coastguard Worker char errmsg[STRERR_BUFSIZE];
11392*f7c14bbaSAndroid Build Coastguard Worker const unsigned long *addrs;
11393*f7c14bbaSAndroid Build Coastguard Worker int err, link_fd, prog_fd;
11394*f7c14bbaSAndroid Build Coastguard Worker const __u64 *cookies;
11395*f7c14bbaSAndroid Build Coastguard Worker const char **syms;
11396*f7c14bbaSAndroid Build Coastguard Worker bool retprobe;
11397*f7c14bbaSAndroid Build Coastguard Worker size_t cnt;
11398*f7c14bbaSAndroid Build Coastguard Worker
11399*f7c14bbaSAndroid Build Coastguard Worker if (!OPTS_VALID(opts, bpf_kprobe_multi_opts))
11400*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(-EINVAL);
11401*f7c14bbaSAndroid Build Coastguard Worker
11402*f7c14bbaSAndroid Build Coastguard Worker prog_fd = bpf_program__fd(prog);
11403*f7c14bbaSAndroid Build Coastguard Worker if (prog_fd < 0) {
11404*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': can't attach BPF program without FD (was it loaded?)\n",
11405*f7c14bbaSAndroid Build Coastguard Worker prog->name);
11406*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(-EINVAL);
11407*f7c14bbaSAndroid Build Coastguard Worker }
11408*f7c14bbaSAndroid Build Coastguard Worker
11409*f7c14bbaSAndroid Build Coastguard Worker syms = OPTS_GET(opts, syms, false);
11410*f7c14bbaSAndroid Build Coastguard Worker addrs = OPTS_GET(opts, addrs, false);
11411*f7c14bbaSAndroid Build Coastguard Worker cnt = OPTS_GET(opts, cnt, false);
11412*f7c14bbaSAndroid Build Coastguard Worker cookies = OPTS_GET(opts, cookies, false);
11413*f7c14bbaSAndroid Build Coastguard Worker
11414*f7c14bbaSAndroid Build Coastguard Worker if (!pattern && !addrs && !syms)
11415*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(-EINVAL);
11416*f7c14bbaSAndroid Build Coastguard Worker if (pattern && (addrs || syms || cookies || cnt))
11417*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(-EINVAL);
11418*f7c14bbaSAndroid Build Coastguard Worker if (!pattern && !cnt)
11419*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(-EINVAL);
11420*f7c14bbaSAndroid Build Coastguard Worker if (addrs && syms)
11421*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(-EINVAL);
11422*f7c14bbaSAndroid Build Coastguard Worker
11423*f7c14bbaSAndroid Build Coastguard Worker if (pattern) {
11424*f7c14bbaSAndroid Build Coastguard Worker if (has_available_filter_functions_addrs())
11425*f7c14bbaSAndroid Build Coastguard Worker err = libbpf_available_kprobes_parse(&res);
11426*f7c14bbaSAndroid Build Coastguard Worker else
11427*f7c14bbaSAndroid Build Coastguard Worker err = libbpf_available_kallsyms_parse(&res);
11428*f7c14bbaSAndroid Build Coastguard Worker if (err)
11429*f7c14bbaSAndroid Build Coastguard Worker goto error;
11430*f7c14bbaSAndroid Build Coastguard Worker addrs = res.addrs;
11431*f7c14bbaSAndroid Build Coastguard Worker cnt = res.cnt;
11432*f7c14bbaSAndroid Build Coastguard Worker }
11433*f7c14bbaSAndroid Build Coastguard Worker
11434*f7c14bbaSAndroid Build Coastguard Worker retprobe = OPTS_GET(opts, retprobe, false);
11435*f7c14bbaSAndroid Build Coastguard Worker
11436*f7c14bbaSAndroid Build Coastguard Worker lopts.kprobe_multi.syms = syms;
11437*f7c14bbaSAndroid Build Coastguard Worker lopts.kprobe_multi.addrs = addrs;
11438*f7c14bbaSAndroid Build Coastguard Worker lopts.kprobe_multi.cookies = cookies;
11439*f7c14bbaSAndroid Build Coastguard Worker lopts.kprobe_multi.cnt = cnt;
11440*f7c14bbaSAndroid Build Coastguard Worker lopts.kprobe_multi.flags = retprobe ? BPF_F_KPROBE_MULTI_RETURN : 0;
11441*f7c14bbaSAndroid Build Coastguard Worker
11442*f7c14bbaSAndroid Build Coastguard Worker link = calloc(1, sizeof(*link));
11443*f7c14bbaSAndroid Build Coastguard Worker if (!link) {
11444*f7c14bbaSAndroid Build Coastguard Worker err = -ENOMEM;
11445*f7c14bbaSAndroid Build Coastguard Worker goto error;
11446*f7c14bbaSAndroid Build Coastguard Worker }
11447*f7c14bbaSAndroid Build Coastguard Worker link->detach = &bpf_link__detach_fd;
11448*f7c14bbaSAndroid Build Coastguard Worker
11449*f7c14bbaSAndroid Build Coastguard Worker link_fd = bpf_link_create(prog_fd, 0, BPF_TRACE_KPROBE_MULTI, &lopts);
11450*f7c14bbaSAndroid Build Coastguard Worker if (link_fd < 0) {
11451*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
11452*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': failed to attach: %s\n",
11453*f7c14bbaSAndroid Build Coastguard Worker prog->name, libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
11454*f7c14bbaSAndroid Build Coastguard Worker goto error;
11455*f7c14bbaSAndroid Build Coastguard Worker }
11456*f7c14bbaSAndroid Build Coastguard Worker link->fd = link_fd;
11457*f7c14bbaSAndroid Build Coastguard Worker free(res.addrs);
11458*f7c14bbaSAndroid Build Coastguard Worker return link;
11459*f7c14bbaSAndroid Build Coastguard Worker
11460*f7c14bbaSAndroid Build Coastguard Worker error:
11461*f7c14bbaSAndroid Build Coastguard Worker free(link);
11462*f7c14bbaSAndroid Build Coastguard Worker free(res.addrs);
11463*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(err);
11464*f7c14bbaSAndroid Build Coastguard Worker }
11465*f7c14bbaSAndroid Build Coastguard Worker
attach_kprobe(const struct bpf_program * prog,long cookie,struct bpf_link ** link)11466*f7c14bbaSAndroid Build Coastguard Worker static int attach_kprobe(const struct bpf_program *prog, long cookie, struct bpf_link **link)
11467*f7c14bbaSAndroid Build Coastguard Worker {
11468*f7c14bbaSAndroid Build Coastguard Worker DECLARE_LIBBPF_OPTS(bpf_kprobe_opts, opts);
11469*f7c14bbaSAndroid Build Coastguard Worker unsigned long offset = 0;
11470*f7c14bbaSAndroid Build Coastguard Worker const char *func_name;
11471*f7c14bbaSAndroid Build Coastguard Worker char *func;
11472*f7c14bbaSAndroid Build Coastguard Worker int n;
11473*f7c14bbaSAndroid Build Coastguard Worker
11474*f7c14bbaSAndroid Build Coastguard Worker *link = NULL;
11475*f7c14bbaSAndroid Build Coastguard Worker
11476*f7c14bbaSAndroid Build Coastguard Worker /* no auto-attach for SEC("kprobe") and SEC("kretprobe") */
11477*f7c14bbaSAndroid Build Coastguard Worker if (strcmp(prog->sec_name, "kprobe") == 0 || strcmp(prog->sec_name, "kretprobe") == 0)
11478*f7c14bbaSAndroid Build Coastguard Worker return 0;
11479*f7c14bbaSAndroid Build Coastguard Worker
11480*f7c14bbaSAndroid Build Coastguard Worker opts.retprobe = str_has_pfx(prog->sec_name, "kretprobe/");
11481*f7c14bbaSAndroid Build Coastguard Worker if (opts.retprobe)
11482*f7c14bbaSAndroid Build Coastguard Worker func_name = prog->sec_name + sizeof("kretprobe/") - 1;
11483*f7c14bbaSAndroid Build Coastguard Worker else
11484*f7c14bbaSAndroid Build Coastguard Worker func_name = prog->sec_name + sizeof("kprobe/") - 1;
11485*f7c14bbaSAndroid Build Coastguard Worker
11486*f7c14bbaSAndroid Build Coastguard Worker n = sscanf(func_name, "%m[a-zA-Z0-9_.]+%li", &func, &offset);
11487*f7c14bbaSAndroid Build Coastguard Worker if (n < 1) {
11488*f7c14bbaSAndroid Build Coastguard Worker pr_warn("kprobe name is invalid: %s\n", func_name);
11489*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
11490*f7c14bbaSAndroid Build Coastguard Worker }
11491*f7c14bbaSAndroid Build Coastguard Worker if (opts.retprobe && offset != 0) {
11492*f7c14bbaSAndroid Build Coastguard Worker free(func);
11493*f7c14bbaSAndroid Build Coastguard Worker pr_warn("kretprobes do not support offset specification\n");
11494*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
11495*f7c14bbaSAndroid Build Coastguard Worker }
11496*f7c14bbaSAndroid Build Coastguard Worker
11497*f7c14bbaSAndroid Build Coastguard Worker opts.offset = offset;
11498*f7c14bbaSAndroid Build Coastguard Worker *link = bpf_program__attach_kprobe_opts(prog, func, &opts);
11499*f7c14bbaSAndroid Build Coastguard Worker free(func);
11500*f7c14bbaSAndroid Build Coastguard Worker return libbpf_get_error(*link);
11501*f7c14bbaSAndroid Build Coastguard Worker }
11502*f7c14bbaSAndroid Build Coastguard Worker
attach_ksyscall(const struct bpf_program * prog,long cookie,struct bpf_link ** link)11503*f7c14bbaSAndroid Build Coastguard Worker static int attach_ksyscall(const struct bpf_program *prog, long cookie, struct bpf_link **link)
11504*f7c14bbaSAndroid Build Coastguard Worker {
11505*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_OPTS(bpf_ksyscall_opts, opts);
11506*f7c14bbaSAndroid Build Coastguard Worker const char *syscall_name;
11507*f7c14bbaSAndroid Build Coastguard Worker
11508*f7c14bbaSAndroid Build Coastguard Worker *link = NULL;
11509*f7c14bbaSAndroid Build Coastguard Worker
11510*f7c14bbaSAndroid Build Coastguard Worker /* no auto-attach for SEC("ksyscall") and SEC("kretsyscall") */
11511*f7c14bbaSAndroid Build Coastguard Worker if (strcmp(prog->sec_name, "ksyscall") == 0 || strcmp(prog->sec_name, "kretsyscall") == 0)
11512*f7c14bbaSAndroid Build Coastguard Worker return 0;
11513*f7c14bbaSAndroid Build Coastguard Worker
11514*f7c14bbaSAndroid Build Coastguard Worker opts.retprobe = str_has_pfx(prog->sec_name, "kretsyscall/");
11515*f7c14bbaSAndroid Build Coastguard Worker if (opts.retprobe)
11516*f7c14bbaSAndroid Build Coastguard Worker syscall_name = prog->sec_name + sizeof("kretsyscall/") - 1;
11517*f7c14bbaSAndroid Build Coastguard Worker else
11518*f7c14bbaSAndroid Build Coastguard Worker syscall_name = prog->sec_name + sizeof("ksyscall/") - 1;
11519*f7c14bbaSAndroid Build Coastguard Worker
11520*f7c14bbaSAndroid Build Coastguard Worker *link = bpf_program__attach_ksyscall(prog, syscall_name, &opts);
11521*f7c14bbaSAndroid Build Coastguard Worker return *link ? 0 : -errno;
11522*f7c14bbaSAndroid Build Coastguard Worker }
11523*f7c14bbaSAndroid Build Coastguard Worker
attach_kprobe_multi(const struct bpf_program * prog,long cookie,struct bpf_link ** link)11524*f7c14bbaSAndroid Build Coastguard Worker static int attach_kprobe_multi(const struct bpf_program *prog, long cookie, struct bpf_link **link)
11525*f7c14bbaSAndroid Build Coastguard Worker {
11526*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_OPTS(bpf_kprobe_multi_opts, opts);
11527*f7c14bbaSAndroid Build Coastguard Worker const char *spec;
11528*f7c14bbaSAndroid Build Coastguard Worker char *pattern;
11529*f7c14bbaSAndroid Build Coastguard Worker int n;
11530*f7c14bbaSAndroid Build Coastguard Worker
11531*f7c14bbaSAndroid Build Coastguard Worker *link = NULL;
11532*f7c14bbaSAndroid Build Coastguard Worker
11533*f7c14bbaSAndroid Build Coastguard Worker /* no auto-attach for SEC("kprobe.multi") and SEC("kretprobe.multi") */
11534*f7c14bbaSAndroid Build Coastguard Worker if (strcmp(prog->sec_name, "kprobe.multi") == 0 ||
11535*f7c14bbaSAndroid Build Coastguard Worker strcmp(prog->sec_name, "kretprobe.multi") == 0)
11536*f7c14bbaSAndroid Build Coastguard Worker return 0;
11537*f7c14bbaSAndroid Build Coastguard Worker
11538*f7c14bbaSAndroid Build Coastguard Worker opts.retprobe = str_has_pfx(prog->sec_name, "kretprobe.multi/");
11539*f7c14bbaSAndroid Build Coastguard Worker if (opts.retprobe)
11540*f7c14bbaSAndroid Build Coastguard Worker spec = prog->sec_name + sizeof("kretprobe.multi/") - 1;
11541*f7c14bbaSAndroid Build Coastguard Worker else
11542*f7c14bbaSAndroid Build Coastguard Worker spec = prog->sec_name + sizeof("kprobe.multi/") - 1;
11543*f7c14bbaSAndroid Build Coastguard Worker
11544*f7c14bbaSAndroid Build Coastguard Worker n = sscanf(spec, "%m[a-zA-Z0-9_.*?]", &pattern);
11545*f7c14bbaSAndroid Build Coastguard Worker if (n < 1) {
11546*f7c14bbaSAndroid Build Coastguard Worker pr_warn("kprobe multi pattern is invalid: %s\n", pattern);
11547*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
11548*f7c14bbaSAndroid Build Coastguard Worker }
11549*f7c14bbaSAndroid Build Coastguard Worker
11550*f7c14bbaSAndroid Build Coastguard Worker *link = bpf_program__attach_kprobe_multi_opts(prog, pattern, &opts);
11551*f7c14bbaSAndroid Build Coastguard Worker free(pattern);
11552*f7c14bbaSAndroid Build Coastguard Worker return libbpf_get_error(*link);
11553*f7c14bbaSAndroid Build Coastguard Worker }
11554*f7c14bbaSAndroid Build Coastguard Worker
attach_uprobe_multi(const struct bpf_program * prog,long cookie,struct bpf_link ** link)11555*f7c14bbaSAndroid Build Coastguard Worker static int attach_uprobe_multi(const struct bpf_program *prog, long cookie, struct bpf_link **link)
11556*f7c14bbaSAndroid Build Coastguard Worker {
11557*f7c14bbaSAndroid Build Coastguard Worker char *probe_type = NULL, *binary_path = NULL, *func_name = NULL;
11558*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_OPTS(bpf_uprobe_multi_opts, opts);
11559*f7c14bbaSAndroid Build Coastguard Worker int n, ret = -EINVAL;
11560*f7c14bbaSAndroid Build Coastguard Worker
11561*f7c14bbaSAndroid Build Coastguard Worker *link = NULL;
11562*f7c14bbaSAndroid Build Coastguard Worker
11563*f7c14bbaSAndroid Build Coastguard Worker n = sscanf(prog->sec_name, "%m[^/]/%m[^:]:%m[^\n]",
11564*f7c14bbaSAndroid Build Coastguard Worker &probe_type, &binary_path, &func_name);
11565*f7c14bbaSAndroid Build Coastguard Worker switch (n) {
11566*f7c14bbaSAndroid Build Coastguard Worker case 1:
11567*f7c14bbaSAndroid Build Coastguard Worker /* handle SEC("u[ret]probe") - format is valid, but auto-attach is impossible. */
11568*f7c14bbaSAndroid Build Coastguard Worker ret = 0;
11569*f7c14bbaSAndroid Build Coastguard Worker break;
11570*f7c14bbaSAndroid Build Coastguard Worker case 3:
11571*f7c14bbaSAndroid Build Coastguard Worker opts.retprobe = strcmp(probe_type, "uretprobe.multi") == 0;
11572*f7c14bbaSAndroid Build Coastguard Worker *link = bpf_program__attach_uprobe_multi(prog, -1, binary_path, func_name, &opts);
11573*f7c14bbaSAndroid Build Coastguard Worker ret = libbpf_get_error(*link);
11574*f7c14bbaSAndroid Build Coastguard Worker break;
11575*f7c14bbaSAndroid Build Coastguard Worker default:
11576*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': invalid format of section definition '%s'\n", prog->name,
11577*f7c14bbaSAndroid Build Coastguard Worker prog->sec_name);
11578*f7c14bbaSAndroid Build Coastguard Worker break;
11579*f7c14bbaSAndroid Build Coastguard Worker }
11580*f7c14bbaSAndroid Build Coastguard Worker free(probe_type);
11581*f7c14bbaSAndroid Build Coastguard Worker free(binary_path);
11582*f7c14bbaSAndroid Build Coastguard Worker free(func_name);
11583*f7c14bbaSAndroid Build Coastguard Worker return ret;
11584*f7c14bbaSAndroid Build Coastguard Worker }
11585*f7c14bbaSAndroid Build Coastguard Worker
gen_uprobe_legacy_event_name(char * buf,size_t buf_sz,const char * binary_path,uint64_t offset)11586*f7c14bbaSAndroid Build Coastguard Worker static void gen_uprobe_legacy_event_name(char *buf, size_t buf_sz,
11587*f7c14bbaSAndroid Build Coastguard Worker const char *binary_path, uint64_t offset)
11588*f7c14bbaSAndroid Build Coastguard Worker {
11589*f7c14bbaSAndroid Build Coastguard Worker int i;
11590*f7c14bbaSAndroid Build Coastguard Worker
11591*f7c14bbaSAndroid Build Coastguard Worker snprintf(buf, buf_sz, "libbpf_%u_%s_0x%zx", getpid(), binary_path, (size_t)offset);
11592*f7c14bbaSAndroid Build Coastguard Worker
11593*f7c14bbaSAndroid Build Coastguard Worker /* sanitize binary_path in the probe name */
11594*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; buf[i]; i++) {
11595*f7c14bbaSAndroid Build Coastguard Worker if (!isalnum(buf[i]))
11596*f7c14bbaSAndroid Build Coastguard Worker buf[i] = '_';
11597*f7c14bbaSAndroid Build Coastguard Worker }
11598*f7c14bbaSAndroid Build Coastguard Worker }
11599*f7c14bbaSAndroid Build Coastguard Worker
add_uprobe_event_legacy(const char * probe_name,bool retprobe,const char * binary_path,size_t offset)11600*f7c14bbaSAndroid Build Coastguard Worker static inline int add_uprobe_event_legacy(const char *probe_name, bool retprobe,
11601*f7c14bbaSAndroid Build Coastguard Worker const char *binary_path, size_t offset)
11602*f7c14bbaSAndroid Build Coastguard Worker {
11603*f7c14bbaSAndroid Build Coastguard Worker return append_to_file(tracefs_uprobe_events(), "%c:%s/%s %s:0x%zx",
11604*f7c14bbaSAndroid Build Coastguard Worker retprobe ? 'r' : 'p',
11605*f7c14bbaSAndroid Build Coastguard Worker retprobe ? "uretprobes" : "uprobes",
11606*f7c14bbaSAndroid Build Coastguard Worker probe_name, binary_path, offset);
11607*f7c14bbaSAndroid Build Coastguard Worker }
11608*f7c14bbaSAndroid Build Coastguard Worker
remove_uprobe_event_legacy(const char * probe_name,bool retprobe)11609*f7c14bbaSAndroid Build Coastguard Worker static inline int remove_uprobe_event_legacy(const char *probe_name, bool retprobe)
11610*f7c14bbaSAndroid Build Coastguard Worker {
11611*f7c14bbaSAndroid Build Coastguard Worker return append_to_file(tracefs_uprobe_events(), "-:%s/%s",
11612*f7c14bbaSAndroid Build Coastguard Worker retprobe ? "uretprobes" : "uprobes", probe_name);
11613*f7c14bbaSAndroid Build Coastguard Worker }
11614*f7c14bbaSAndroid Build Coastguard Worker
determine_uprobe_perf_type_legacy(const char * probe_name,bool retprobe)11615*f7c14bbaSAndroid Build Coastguard Worker static int determine_uprobe_perf_type_legacy(const char *probe_name, bool retprobe)
11616*f7c14bbaSAndroid Build Coastguard Worker {
11617*f7c14bbaSAndroid Build Coastguard Worker char file[512];
11618*f7c14bbaSAndroid Build Coastguard Worker
11619*f7c14bbaSAndroid Build Coastguard Worker snprintf(file, sizeof(file), "%s/events/%s/%s/id",
11620*f7c14bbaSAndroid Build Coastguard Worker tracefs_path(), retprobe ? "uretprobes" : "uprobes", probe_name);
11621*f7c14bbaSAndroid Build Coastguard Worker
11622*f7c14bbaSAndroid Build Coastguard Worker return parse_uint_from_file(file, "%d\n");
11623*f7c14bbaSAndroid Build Coastguard Worker }
11624*f7c14bbaSAndroid Build Coastguard Worker
perf_event_uprobe_open_legacy(const char * probe_name,bool retprobe,const char * binary_path,size_t offset,int pid)11625*f7c14bbaSAndroid Build Coastguard Worker static int perf_event_uprobe_open_legacy(const char *probe_name, bool retprobe,
11626*f7c14bbaSAndroid Build Coastguard Worker const char *binary_path, size_t offset, int pid)
11627*f7c14bbaSAndroid Build Coastguard Worker {
11628*f7c14bbaSAndroid Build Coastguard Worker const size_t attr_sz = sizeof(struct perf_event_attr);
11629*f7c14bbaSAndroid Build Coastguard Worker struct perf_event_attr attr;
11630*f7c14bbaSAndroid Build Coastguard Worker int type, pfd, err;
11631*f7c14bbaSAndroid Build Coastguard Worker
11632*f7c14bbaSAndroid Build Coastguard Worker err = add_uprobe_event_legacy(probe_name, retprobe, binary_path, offset);
11633*f7c14bbaSAndroid Build Coastguard Worker if (err < 0) {
11634*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to add legacy uprobe event for %s:0x%zx: %d\n",
11635*f7c14bbaSAndroid Build Coastguard Worker binary_path, (size_t)offset, err);
11636*f7c14bbaSAndroid Build Coastguard Worker return err;
11637*f7c14bbaSAndroid Build Coastguard Worker }
11638*f7c14bbaSAndroid Build Coastguard Worker type = determine_uprobe_perf_type_legacy(probe_name, retprobe);
11639*f7c14bbaSAndroid Build Coastguard Worker if (type < 0) {
11640*f7c14bbaSAndroid Build Coastguard Worker err = type;
11641*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to determine legacy uprobe event id for %s:0x%zx: %d\n",
11642*f7c14bbaSAndroid Build Coastguard Worker binary_path, offset, err);
11643*f7c14bbaSAndroid Build Coastguard Worker goto err_clean_legacy;
11644*f7c14bbaSAndroid Build Coastguard Worker }
11645*f7c14bbaSAndroid Build Coastguard Worker
11646*f7c14bbaSAndroid Build Coastguard Worker memset(&attr, 0, attr_sz);
11647*f7c14bbaSAndroid Build Coastguard Worker attr.size = attr_sz;
11648*f7c14bbaSAndroid Build Coastguard Worker attr.config = type;
11649*f7c14bbaSAndroid Build Coastguard Worker attr.type = PERF_TYPE_TRACEPOINT;
11650*f7c14bbaSAndroid Build Coastguard Worker
11651*f7c14bbaSAndroid Build Coastguard Worker pfd = syscall(__NR_perf_event_open, &attr,
11652*f7c14bbaSAndroid Build Coastguard Worker pid < 0 ? -1 : pid, /* pid */
11653*f7c14bbaSAndroid Build Coastguard Worker pid == -1 ? 0 : -1, /* cpu */
11654*f7c14bbaSAndroid Build Coastguard Worker -1 /* group_fd */, PERF_FLAG_FD_CLOEXEC);
11655*f7c14bbaSAndroid Build Coastguard Worker if (pfd < 0) {
11656*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
11657*f7c14bbaSAndroid Build Coastguard Worker pr_warn("legacy uprobe perf_event_open() failed: %d\n", err);
11658*f7c14bbaSAndroid Build Coastguard Worker goto err_clean_legacy;
11659*f7c14bbaSAndroid Build Coastguard Worker }
11660*f7c14bbaSAndroid Build Coastguard Worker return pfd;
11661*f7c14bbaSAndroid Build Coastguard Worker
11662*f7c14bbaSAndroid Build Coastguard Worker err_clean_legacy:
11663*f7c14bbaSAndroid Build Coastguard Worker /* Clear the newly added legacy uprobe_event */
11664*f7c14bbaSAndroid Build Coastguard Worker remove_uprobe_event_legacy(probe_name, retprobe);
11665*f7c14bbaSAndroid Build Coastguard Worker return err;
11666*f7c14bbaSAndroid Build Coastguard Worker }
11667*f7c14bbaSAndroid Build Coastguard Worker
11668*f7c14bbaSAndroid Build Coastguard Worker /* Find offset of function name in archive specified by path. Currently
11669*f7c14bbaSAndroid Build Coastguard Worker * supported are .zip files that do not compress their contents, as used on
11670*f7c14bbaSAndroid Build Coastguard Worker * Android in the form of APKs, for example. "file_name" is the name of the ELF
11671*f7c14bbaSAndroid Build Coastguard Worker * file inside the archive. "func_name" matches symbol name or name@@LIB for
11672*f7c14bbaSAndroid Build Coastguard Worker * library functions.
11673*f7c14bbaSAndroid Build Coastguard Worker *
11674*f7c14bbaSAndroid Build Coastguard Worker * An overview of the APK format specifically provided here:
11675*f7c14bbaSAndroid Build Coastguard Worker * https://en.wikipedia.org/w/index.php?title=Apk_(file_format)&oldid=1139099120#Package_contents
11676*f7c14bbaSAndroid Build Coastguard Worker */
elf_find_func_offset_from_archive(const char * archive_path,const char * file_name,const char * func_name)11677*f7c14bbaSAndroid Build Coastguard Worker static long elf_find_func_offset_from_archive(const char *archive_path, const char *file_name,
11678*f7c14bbaSAndroid Build Coastguard Worker const char *func_name)
11679*f7c14bbaSAndroid Build Coastguard Worker {
11680*f7c14bbaSAndroid Build Coastguard Worker struct zip_archive *archive;
11681*f7c14bbaSAndroid Build Coastguard Worker struct zip_entry entry;
11682*f7c14bbaSAndroid Build Coastguard Worker long ret;
11683*f7c14bbaSAndroid Build Coastguard Worker Elf *elf;
11684*f7c14bbaSAndroid Build Coastguard Worker
11685*f7c14bbaSAndroid Build Coastguard Worker archive = zip_archive_open(archive_path);
11686*f7c14bbaSAndroid Build Coastguard Worker if (IS_ERR(archive)) {
11687*f7c14bbaSAndroid Build Coastguard Worker ret = PTR_ERR(archive);
11688*f7c14bbaSAndroid Build Coastguard Worker pr_warn("zip: failed to open %s: %ld\n", archive_path, ret);
11689*f7c14bbaSAndroid Build Coastguard Worker return ret;
11690*f7c14bbaSAndroid Build Coastguard Worker }
11691*f7c14bbaSAndroid Build Coastguard Worker
11692*f7c14bbaSAndroid Build Coastguard Worker ret = zip_archive_find_entry(archive, file_name, &entry);
11693*f7c14bbaSAndroid Build Coastguard Worker if (ret) {
11694*f7c14bbaSAndroid Build Coastguard Worker pr_warn("zip: could not find archive member %s in %s: %ld\n", file_name,
11695*f7c14bbaSAndroid Build Coastguard Worker archive_path, ret);
11696*f7c14bbaSAndroid Build Coastguard Worker goto out;
11697*f7c14bbaSAndroid Build Coastguard Worker }
11698*f7c14bbaSAndroid Build Coastguard Worker pr_debug("zip: found entry for %s in %s at 0x%lx\n", file_name, archive_path,
11699*f7c14bbaSAndroid Build Coastguard Worker (unsigned long)entry.data_offset);
11700*f7c14bbaSAndroid Build Coastguard Worker
11701*f7c14bbaSAndroid Build Coastguard Worker if (entry.compression) {
11702*f7c14bbaSAndroid Build Coastguard Worker pr_warn("zip: entry %s of %s is compressed and cannot be handled\n", file_name,
11703*f7c14bbaSAndroid Build Coastguard Worker archive_path);
11704*f7c14bbaSAndroid Build Coastguard Worker ret = -LIBBPF_ERRNO__FORMAT;
11705*f7c14bbaSAndroid Build Coastguard Worker goto out;
11706*f7c14bbaSAndroid Build Coastguard Worker }
11707*f7c14bbaSAndroid Build Coastguard Worker
11708*f7c14bbaSAndroid Build Coastguard Worker elf = elf_memory((void *)entry.data, entry.data_length);
11709*f7c14bbaSAndroid Build Coastguard Worker if (!elf) {
11710*f7c14bbaSAndroid Build Coastguard Worker pr_warn("elf: could not read elf file %s from %s: %s\n", file_name, archive_path,
11711*f7c14bbaSAndroid Build Coastguard Worker elf_errmsg(-1));
11712*f7c14bbaSAndroid Build Coastguard Worker ret = -LIBBPF_ERRNO__LIBELF;
11713*f7c14bbaSAndroid Build Coastguard Worker goto out;
11714*f7c14bbaSAndroid Build Coastguard Worker }
11715*f7c14bbaSAndroid Build Coastguard Worker
11716*f7c14bbaSAndroid Build Coastguard Worker ret = elf_find_func_offset(elf, file_name, func_name);
11717*f7c14bbaSAndroid Build Coastguard Worker if (ret > 0) {
11718*f7c14bbaSAndroid Build Coastguard Worker pr_debug("elf: symbol address match for %s of %s in %s: 0x%x + 0x%lx = 0x%lx\n",
11719*f7c14bbaSAndroid Build Coastguard Worker func_name, file_name, archive_path, entry.data_offset, ret,
11720*f7c14bbaSAndroid Build Coastguard Worker ret + entry.data_offset);
11721*f7c14bbaSAndroid Build Coastguard Worker ret += entry.data_offset;
11722*f7c14bbaSAndroid Build Coastguard Worker }
11723*f7c14bbaSAndroid Build Coastguard Worker elf_end(elf);
11724*f7c14bbaSAndroid Build Coastguard Worker
11725*f7c14bbaSAndroid Build Coastguard Worker out:
11726*f7c14bbaSAndroid Build Coastguard Worker zip_archive_close(archive);
11727*f7c14bbaSAndroid Build Coastguard Worker return ret;
11728*f7c14bbaSAndroid Build Coastguard Worker }
11729*f7c14bbaSAndroid Build Coastguard Worker
arch_specific_lib_paths(void)11730*f7c14bbaSAndroid Build Coastguard Worker static const char *arch_specific_lib_paths(void)
11731*f7c14bbaSAndroid Build Coastguard Worker {
11732*f7c14bbaSAndroid Build Coastguard Worker /*
11733*f7c14bbaSAndroid Build Coastguard Worker * Based on https://packages.debian.org/sid/libc6.
11734*f7c14bbaSAndroid Build Coastguard Worker *
11735*f7c14bbaSAndroid Build Coastguard Worker * Assume that the traced program is built for the same architecture
11736*f7c14bbaSAndroid Build Coastguard Worker * as libbpf, which should cover the vast majority of cases.
11737*f7c14bbaSAndroid Build Coastguard Worker */
11738*f7c14bbaSAndroid Build Coastguard Worker #if defined(__x86_64__)
11739*f7c14bbaSAndroid Build Coastguard Worker return "/lib/x86_64-linux-gnu";
11740*f7c14bbaSAndroid Build Coastguard Worker #elif defined(__i386__)
11741*f7c14bbaSAndroid Build Coastguard Worker return "/lib/i386-linux-gnu";
11742*f7c14bbaSAndroid Build Coastguard Worker #elif defined(__s390x__)
11743*f7c14bbaSAndroid Build Coastguard Worker return "/lib/s390x-linux-gnu";
11744*f7c14bbaSAndroid Build Coastguard Worker #elif defined(__s390__)
11745*f7c14bbaSAndroid Build Coastguard Worker return "/lib/s390-linux-gnu";
11746*f7c14bbaSAndroid Build Coastguard Worker #elif defined(__arm__) && defined(__SOFTFP__)
11747*f7c14bbaSAndroid Build Coastguard Worker return "/lib/arm-linux-gnueabi";
11748*f7c14bbaSAndroid Build Coastguard Worker #elif defined(__arm__) && !defined(__SOFTFP__)
11749*f7c14bbaSAndroid Build Coastguard Worker return "/lib/arm-linux-gnueabihf";
11750*f7c14bbaSAndroid Build Coastguard Worker #elif defined(__aarch64__)
11751*f7c14bbaSAndroid Build Coastguard Worker return "/lib/aarch64-linux-gnu";
11752*f7c14bbaSAndroid Build Coastguard Worker #elif defined(__mips__) && defined(__MIPSEL__) && _MIPS_SZLONG == 64
11753*f7c14bbaSAndroid Build Coastguard Worker return "/lib/mips64el-linux-gnuabi64";
11754*f7c14bbaSAndroid Build Coastguard Worker #elif defined(__mips__) && defined(__MIPSEL__) && _MIPS_SZLONG == 32
11755*f7c14bbaSAndroid Build Coastguard Worker return "/lib/mipsel-linux-gnu";
11756*f7c14bbaSAndroid Build Coastguard Worker #elif defined(__powerpc64__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
11757*f7c14bbaSAndroid Build Coastguard Worker return "/lib/powerpc64le-linux-gnu";
11758*f7c14bbaSAndroid Build Coastguard Worker #elif defined(__sparc__) && defined(__arch64__)
11759*f7c14bbaSAndroid Build Coastguard Worker return "/lib/sparc64-linux-gnu";
11760*f7c14bbaSAndroid Build Coastguard Worker #elif defined(__riscv) && __riscv_xlen == 64
11761*f7c14bbaSAndroid Build Coastguard Worker return "/lib/riscv64-linux-gnu";
11762*f7c14bbaSAndroid Build Coastguard Worker #else
11763*f7c14bbaSAndroid Build Coastguard Worker return NULL;
11764*f7c14bbaSAndroid Build Coastguard Worker #endif
11765*f7c14bbaSAndroid Build Coastguard Worker }
11766*f7c14bbaSAndroid Build Coastguard Worker
11767*f7c14bbaSAndroid Build Coastguard Worker /* Get full path to program/shared library. */
resolve_full_path(const char * file,char * result,size_t result_sz)11768*f7c14bbaSAndroid Build Coastguard Worker static int resolve_full_path(const char *file, char *result, size_t result_sz)
11769*f7c14bbaSAndroid Build Coastguard Worker {
11770*f7c14bbaSAndroid Build Coastguard Worker const char *search_paths[3] = {};
11771*f7c14bbaSAndroid Build Coastguard Worker int i, perm;
11772*f7c14bbaSAndroid Build Coastguard Worker
11773*f7c14bbaSAndroid Build Coastguard Worker if (str_has_sfx(file, ".so") || strstr(file, ".so.")) {
11774*f7c14bbaSAndroid Build Coastguard Worker search_paths[0] = getenv("LD_LIBRARY_PATH");
11775*f7c14bbaSAndroid Build Coastguard Worker search_paths[1] = "/usr/lib64:/usr/lib";
11776*f7c14bbaSAndroid Build Coastguard Worker search_paths[2] = arch_specific_lib_paths();
11777*f7c14bbaSAndroid Build Coastguard Worker perm = R_OK;
11778*f7c14bbaSAndroid Build Coastguard Worker } else {
11779*f7c14bbaSAndroid Build Coastguard Worker search_paths[0] = getenv("PATH");
11780*f7c14bbaSAndroid Build Coastguard Worker search_paths[1] = "/usr/bin:/usr/sbin";
11781*f7c14bbaSAndroid Build Coastguard Worker perm = R_OK | X_OK;
11782*f7c14bbaSAndroid Build Coastguard Worker }
11783*f7c14bbaSAndroid Build Coastguard Worker
11784*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < ARRAY_SIZE(search_paths); i++) {
11785*f7c14bbaSAndroid Build Coastguard Worker const char *s;
11786*f7c14bbaSAndroid Build Coastguard Worker
11787*f7c14bbaSAndroid Build Coastguard Worker if (!search_paths[i])
11788*f7c14bbaSAndroid Build Coastguard Worker continue;
11789*f7c14bbaSAndroid Build Coastguard Worker for (s = search_paths[i]; s != NULL; s = strchr(s, ':')) {
11790*f7c14bbaSAndroid Build Coastguard Worker char *next_path;
11791*f7c14bbaSAndroid Build Coastguard Worker int seg_len;
11792*f7c14bbaSAndroid Build Coastguard Worker
11793*f7c14bbaSAndroid Build Coastguard Worker if (s[0] == ':')
11794*f7c14bbaSAndroid Build Coastguard Worker s++;
11795*f7c14bbaSAndroid Build Coastguard Worker next_path = strchr(s, ':');
11796*f7c14bbaSAndroid Build Coastguard Worker seg_len = next_path ? next_path - s : strlen(s);
11797*f7c14bbaSAndroid Build Coastguard Worker if (!seg_len)
11798*f7c14bbaSAndroid Build Coastguard Worker continue;
11799*f7c14bbaSAndroid Build Coastguard Worker snprintf(result, result_sz, "%.*s/%s", seg_len, s, file);
11800*f7c14bbaSAndroid Build Coastguard Worker /* ensure it has required permissions */
11801*f7c14bbaSAndroid Build Coastguard Worker if (faccessat(AT_FDCWD, result, perm, AT_EACCESS) < 0)
11802*f7c14bbaSAndroid Build Coastguard Worker continue;
11803*f7c14bbaSAndroid Build Coastguard Worker pr_debug("resolved '%s' to '%s'\n", file, result);
11804*f7c14bbaSAndroid Build Coastguard Worker return 0;
11805*f7c14bbaSAndroid Build Coastguard Worker }
11806*f7c14bbaSAndroid Build Coastguard Worker }
11807*f7c14bbaSAndroid Build Coastguard Worker return -ENOENT;
11808*f7c14bbaSAndroid Build Coastguard Worker }
11809*f7c14bbaSAndroid Build Coastguard Worker
11810*f7c14bbaSAndroid Build Coastguard Worker struct bpf_link *
bpf_program__attach_uprobe_multi(const struct bpf_program * prog,pid_t pid,const char * path,const char * func_pattern,const struct bpf_uprobe_multi_opts * opts)11811*f7c14bbaSAndroid Build Coastguard Worker bpf_program__attach_uprobe_multi(const struct bpf_program *prog,
11812*f7c14bbaSAndroid Build Coastguard Worker pid_t pid,
11813*f7c14bbaSAndroid Build Coastguard Worker const char *path,
11814*f7c14bbaSAndroid Build Coastguard Worker const char *func_pattern,
11815*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_uprobe_multi_opts *opts)
11816*f7c14bbaSAndroid Build Coastguard Worker {
11817*f7c14bbaSAndroid Build Coastguard Worker const unsigned long *ref_ctr_offsets = NULL, *offsets = NULL;
11818*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_OPTS(bpf_link_create_opts, lopts);
11819*f7c14bbaSAndroid Build Coastguard Worker unsigned long *resolved_offsets = NULL;
11820*f7c14bbaSAndroid Build Coastguard Worker int err = 0, link_fd, prog_fd;
11821*f7c14bbaSAndroid Build Coastguard Worker struct bpf_link *link = NULL;
11822*f7c14bbaSAndroid Build Coastguard Worker char errmsg[STRERR_BUFSIZE];
11823*f7c14bbaSAndroid Build Coastguard Worker char full_path[PATH_MAX];
11824*f7c14bbaSAndroid Build Coastguard Worker const __u64 *cookies;
11825*f7c14bbaSAndroid Build Coastguard Worker const char **syms;
11826*f7c14bbaSAndroid Build Coastguard Worker size_t cnt;
11827*f7c14bbaSAndroid Build Coastguard Worker
11828*f7c14bbaSAndroid Build Coastguard Worker if (!OPTS_VALID(opts, bpf_uprobe_multi_opts))
11829*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(-EINVAL);
11830*f7c14bbaSAndroid Build Coastguard Worker
11831*f7c14bbaSAndroid Build Coastguard Worker prog_fd = bpf_program__fd(prog);
11832*f7c14bbaSAndroid Build Coastguard Worker if (prog_fd < 0) {
11833*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': can't attach BPF program without FD (was it loaded?)\n",
11834*f7c14bbaSAndroid Build Coastguard Worker prog->name);
11835*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(-EINVAL);
11836*f7c14bbaSAndroid Build Coastguard Worker }
11837*f7c14bbaSAndroid Build Coastguard Worker
11838*f7c14bbaSAndroid Build Coastguard Worker syms = OPTS_GET(opts, syms, NULL);
11839*f7c14bbaSAndroid Build Coastguard Worker offsets = OPTS_GET(opts, offsets, NULL);
11840*f7c14bbaSAndroid Build Coastguard Worker ref_ctr_offsets = OPTS_GET(opts, ref_ctr_offsets, NULL);
11841*f7c14bbaSAndroid Build Coastguard Worker cookies = OPTS_GET(opts, cookies, NULL);
11842*f7c14bbaSAndroid Build Coastguard Worker cnt = OPTS_GET(opts, cnt, 0);
11843*f7c14bbaSAndroid Build Coastguard Worker
11844*f7c14bbaSAndroid Build Coastguard Worker /*
11845*f7c14bbaSAndroid Build Coastguard Worker * User can specify 2 mutually exclusive set of inputs:
11846*f7c14bbaSAndroid Build Coastguard Worker *
11847*f7c14bbaSAndroid Build Coastguard Worker * 1) use only path/func_pattern/pid arguments
11848*f7c14bbaSAndroid Build Coastguard Worker *
11849*f7c14bbaSAndroid Build Coastguard Worker * 2) use path/pid with allowed combinations of:
11850*f7c14bbaSAndroid Build Coastguard Worker * syms/offsets/ref_ctr_offsets/cookies/cnt
11851*f7c14bbaSAndroid Build Coastguard Worker *
11852*f7c14bbaSAndroid Build Coastguard Worker * - syms and offsets are mutually exclusive
11853*f7c14bbaSAndroid Build Coastguard Worker * - ref_ctr_offsets and cookies are optional
11854*f7c14bbaSAndroid Build Coastguard Worker *
11855*f7c14bbaSAndroid Build Coastguard Worker * Any other usage results in error.
11856*f7c14bbaSAndroid Build Coastguard Worker */
11857*f7c14bbaSAndroid Build Coastguard Worker
11858*f7c14bbaSAndroid Build Coastguard Worker if (!path)
11859*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(-EINVAL);
11860*f7c14bbaSAndroid Build Coastguard Worker if (!func_pattern && cnt == 0)
11861*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(-EINVAL);
11862*f7c14bbaSAndroid Build Coastguard Worker
11863*f7c14bbaSAndroid Build Coastguard Worker if (func_pattern) {
11864*f7c14bbaSAndroid Build Coastguard Worker if (syms || offsets || ref_ctr_offsets || cookies || cnt)
11865*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(-EINVAL);
11866*f7c14bbaSAndroid Build Coastguard Worker } else {
11867*f7c14bbaSAndroid Build Coastguard Worker if (!!syms == !!offsets)
11868*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(-EINVAL);
11869*f7c14bbaSAndroid Build Coastguard Worker }
11870*f7c14bbaSAndroid Build Coastguard Worker
11871*f7c14bbaSAndroid Build Coastguard Worker if (func_pattern) {
11872*f7c14bbaSAndroid Build Coastguard Worker if (!strchr(path, '/')) {
11873*f7c14bbaSAndroid Build Coastguard Worker err = resolve_full_path(path, full_path, sizeof(full_path));
11874*f7c14bbaSAndroid Build Coastguard Worker if (err) {
11875*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': failed to resolve full path for '%s': %d\n",
11876*f7c14bbaSAndroid Build Coastguard Worker prog->name, path, err);
11877*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(err);
11878*f7c14bbaSAndroid Build Coastguard Worker }
11879*f7c14bbaSAndroid Build Coastguard Worker path = full_path;
11880*f7c14bbaSAndroid Build Coastguard Worker }
11881*f7c14bbaSAndroid Build Coastguard Worker
11882*f7c14bbaSAndroid Build Coastguard Worker err = elf_resolve_pattern_offsets(path, func_pattern,
11883*f7c14bbaSAndroid Build Coastguard Worker &resolved_offsets, &cnt);
11884*f7c14bbaSAndroid Build Coastguard Worker if (err < 0)
11885*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(err);
11886*f7c14bbaSAndroid Build Coastguard Worker offsets = resolved_offsets;
11887*f7c14bbaSAndroid Build Coastguard Worker } else if (syms) {
11888*f7c14bbaSAndroid Build Coastguard Worker err = elf_resolve_syms_offsets(path, cnt, syms, &resolved_offsets, STT_FUNC);
11889*f7c14bbaSAndroid Build Coastguard Worker if (err < 0)
11890*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(err);
11891*f7c14bbaSAndroid Build Coastguard Worker offsets = resolved_offsets;
11892*f7c14bbaSAndroid Build Coastguard Worker }
11893*f7c14bbaSAndroid Build Coastguard Worker
11894*f7c14bbaSAndroid Build Coastguard Worker lopts.uprobe_multi.path = path;
11895*f7c14bbaSAndroid Build Coastguard Worker lopts.uprobe_multi.offsets = offsets;
11896*f7c14bbaSAndroid Build Coastguard Worker lopts.uprobe_multi.ref_ctr_offsets = ref_ctr_offsets;
11897*f7c14bbaSAndroid Build Coastguard Worker lopts.uprobe_multi.cookies = cookies;
11898*f7c14bbaSAndroid Build Coastguard Worker lopts.uprobe_multi.cnt = cnt;
11899*f7c14bbaSAndroid Build Coastguard Worker lopts.uprobe_multi.flags = OPTS_GET(opts, retprobe, false) ? BPF_F_UPROBE_MULTI_RETURN : 0;
11900*f7c14bbaSAndroid Build Coastguard Worker
11901*f7c14bbaSAndroid Build Coastguard Worker if (pid == 0)
11902*f7c14bbaSAndroid Build Coastguard Worker pid = getpid();
11903*f7c14bbaSAndroid Build Coastguard Worker if (pid > 0)
11904*f7c14bbaSAndroid Build Coastguard Worker lopts.uprobe_multi.pid = pid;
11905*f7c14bbaSAndroid Build Coastguard Worker
11906*f7c14bbaSAndroid Build Coastguard Worker link = calloc(1, sizeof(*link));
11907*f7c14bbaSAndroid Build Coastguard Worker if (!link) {
11908*f7c14bbaSAndroid Build Coastguard Worker err = -ENOMEM;
11909*f7c14bbaSAndroid Build Coastguard Worker goto error;
11910*f7c14bbaSAndroid Build Coastguard Worker }
11911*f7c14bbaSAndroid Build Coastguard Worker link->detach = &bpf_link__detach_fd;
11912*f7c14bbaSAndroid Build Coastguard Worker
11913*f7c14bbaSAndroid Build Coastguard Worker link_fd = bpf_link_create(prog_fd, 0, BPF_TRACE_UPROBE_MULTI, &lopts);
11914*f7c14bbaSAndroid Build Coastguard Worker if (link_fd < 0) {
11915*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
11916*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': failed to attach multi-uprobe: %s\n",
11917*f7c14bbaSAndroid Build Coastguard Worker prog->name, libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
11918*f7c14bbaSAndroid Build Coastguard Worker goto error;
11919*f7c14bbaSAndroid Build Coastguard Worker }
11920*f7c14bbaSAndroid Build Coastguard Worker link->fd = link_fd;
11921*f7c14bbaSAndroid Build Coastguard Worker free(resolved_offsets);
11922*f7c14bbaSAndroid Build Coastguard Worker return link;
11923*f7c14bbaSAndroid Build Coastguard Worker
11924*f7c14bbaSAndroid Build Coastguard Worker error:
11925*f7c14bbaSAndroid Build Coastguard Worker free(resolved_offsets);
11926*f7c14bbaSAndroid Build Coastguard Worker free(link);
11927*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(err);
11928*f7c14bbaSAndroid Build Coastguard Worker }
11929*f7c14bbaSAndroid Build Coastguard Worker
11930*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_API struct bpf_link *
bpf_program__attach_uprobe_opts(const struct bpf_program * prog,pid_t pid,const char * binary_path,size_t func_offset,const struct bpf_uprobe_opts * opts)11931*f7c14bbaSAndroid Build Coastguard Worker bpf_program__attach_uprobe_opts(const struct bpf_program *prog, pid_t pid,
11932*f7c14bbaSAndroid Build Coastguard Worker const char *binary_path, size_t func_offset,
11933*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_uprobe_opts *opts)
11934*f7c14bbaSAndroid Build Coastguard Worker {
11935*f7c14bbaSAndroid Build Coastguard Worker const char *archive_path = NULL, *archive_sep = NULL;
11936*f7c14bbaSAndroid Build Coastguard Worker char errmsg[STRERR_BUFSIZE], *legacy_probe = NULL;
11937*f7c14bbaSAndroid Build Coastguard Worker DECLARE_LIBBPF_OPTS(bpf_perf_event_opts, pe_opts);
11938*f7c14bbaSAndroid Build Coastguard Worker enum probe_attach_mode attach_mode;
11939*f7c14bbaSAndroid Build Coastguard Worker char full_path[PATH_MAX];
11940*f7c14bbaSAndroid Build Coastguard Worker struct bpf_link *link;
11941*f7c14bbaSAndroid Build Coastguard Worker size_t ref_ctr_off;
11942*f7c14bbaSAndroid Build Coastguard Worker int pfd, err;
11943*f7c14bbaSAndroid Build Coastguard Worker bool retprobe, legacy;
11944*f7c14bbaSAndroid Build Coastguard Worker const char *func_name;
11945*f7c14bbaSAndroid Build Coastguard Worker
11946*f7c14bbaSAndroid Build Coastguard Worker if (!OPTS_VALID(opts, bpf_uprobe_opts))
11947*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(-EINVAL);
11948*f7c14bbaSAndroid Build Coastguard Worker
11949*f7c14bbaSAndroid Build Coastguard Worker attach_mode = OPTS_GET(opts, attach_mode, PROBE_ATTACH_MODE_DEFAULT);
11950*f7c14bbaSAndroid Build Coastguard Worker retprobe = OPTS_GET(opts, retprobe, false);
11951*f7c14bbaSAndroid Build Coastguard Worker ref_ctr_off = OPTS_GET(opts, ref_ctr_offset, 0);
11952*f7c14bbaSAndroid Build Coastguard Worker pe_opts.bpf_cookie = OPTS_GET(opts, bpf_cookie, 0);
11953*f7c14bbaSAndroid Build Coastguard Worker
11954*f7c14bbaSAndroid Build Coastguard Worker if (!binary_path)
11955*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(-EINVAL);
11956*f7c14bbaSAndroid Build Coastguard Worker
11957*f7c14bbaSAndroid Build Coastguard Worker /* Check if "binary_path" refers to an archive. */
11958*f7c14bbaSAndroid Build Coastguard Worker archive_sep = strstr(binary_path, "!/");
11959*f7c14bbaSAndroid Build Coastguard Worker if (archive_sep) {
11960*f7c14bbaSAndroid Build Coastguard Worker full_path[0] = '\0';
11961*f7c14bbaSAndroid Build Coastguard Worker libbpf_strlcpy(full_path, binary_path,
11962*f7c14bbaSAndroid Build Coastguard Worker min(sizeof(full_path), (size_t)(archive_sep - binary_path + 1)));
11963*f7c14bbaSAndroid Build Coastguard Worker archive_path = full_path;
11964*f7c14bbaSAndroid Build Coastguard Worker binary_path = archive_sep + 2;
11965*f7c14bbaSAndroid Build Coastguard Worker } else if (!strchr(binary_path, '/')) {
11966*f7c14bbaSAndroid Build Coastguard Worker err = resolve_full_path(binary_path, full_path, sizeof(full_path));
11967*f7c14bbaSAndroid Build Coastguard Worker if (err) {
11968*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': failed to resolve full path for '%s': %d\n",
11969*f7c14bbaSAndroid Build Coastguard Worker prog->name, binary_path, err);
11970*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(err);
11971*f7c14bbaSAndroid Build Coastguard Worker }
11972*f7c14bbaSAndroid Build Coastguard Worker binary_path = full_path;
11973*f7c14bbaSAndroid Build Coastguard Worker }
11974*f7c14bbaSAndroid Build Coastguard Worker func_name = OPTS_GET(opts, func_name, NULL);
11975*f7c14bbaSAndroid Build Coastguard Worker if (func_name) {
11976*f7c14bbaSAndroid Build Coastguard Worker long sym_off;
11977*f7c14bbaSAndroid Build Coastguard Worker
11978*f7c14bbaSAndroid Build Coastguard Worker if (archive_path) {
11979*f7c14bbaSAndroid Build Coastguard Worker sym_off = elf_find_func_offset_from_archive(archive_path, binary_path,
11980*f7c14bbaSAndroid Build Coastguard Worker func_name);
11981*f7c14bbaSAndroid Build Coastguard Worker binary_path = archive_path;
11982*f7c14bbaSAndroid Build Coastguard Worker } else {
11983*f7c14bbaSAndroid Build Coastguard Worker sym_off = elf_find_func_offset_from_file(binary_path, func_name);
11984*f7c14bbaSAndroid Build Coastguard Worker }
11985*f7c14bbaSAndroid Build Coastguard Worker if (sym_off < 0)
11986*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(sym_off);
11987*f7c14bbaSAndroid Build Coastguard Worker func_offset += sym_off;
11988*f7c14bbaSAndroid Build Coastguard Worker }
11989*f7c14bbaSAndroid Build Coastguard Worker
11990*f7c14bbaSAndroid Build Coastguard Worker legacy = determine_uprobe_perf_type() < 0;
11991*f7c14bbaSAndroid Build Coastguard Worker switch (attach_mode) {
11992*f7c14bbaSAndroid Build Coastguard Worker case PROBE_ATTACH_MODE_LEGACY:
11993*f7c14bbaSAndroid Build Coastguard Worker legacy = true;
11994*f7c14bbaSAndroid Build Coastguard Worker pe_opts.force_ioctl_attach = true;
11995*f7c14bbaSAndroid Build Coastguard Worker break;
11996*f7c14bbaSAndroid Build Coastguard Worker case PROBE_ATTACH_MODE_PERF:
11997*f7c14bbaSAndroid Build Coastguard Worker if (legacy)
11998*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(-ENOTSUP);
11999*f7c14bbaSAndroid Build Coastguard Worker pe_opts.force_ioctl_attach = true;
12000*f7c14bbaSAndroid Build Coastguard Worker break;
12001*f7c14bbaSAndroid Build Coastguard Worker case PROBE_ATTACH_MODE_LINK:
12002*f7c14bbaSAndroid Build Coastguard Worker if (legacy || !kernel_supports(prog->obj, FEAT_PERF_LINK))
12003*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(-ENOTSUP);
12004*f7c14bbaSAndroid Build Coastguard Worker break;
12005*f7c14bbaSAndroid Build Coastguard Worker case PROBE_ATTACH_MODE_DEFAULT:
12006*f7c14bbaSAndroid Build Coastguard Worker break;
12007*f7c14bbaSAndroid Build Coastguard Worker default:
12008*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(-EINVAL);
12009*f7c14bbaSAndroid Build Coastguard Worker }
12010*f7c14bbaSAndroid Build Coastguard Worker
12011*f7c14bbaSAndroid Build Coastguard Worker if (!legacy) {
12012*f7c14bbaSAndroid Build Coastguard Worker pfd = perf_event_open_probe(true /* uprobe */, retprobe, binary_path,
12013*f7c14bbaSAndroid Build Coastguard Worker func_offset, pid, ref_ctr_off);
12014*f7c14bbaSAndroid Build Coastguard Worker } else {
12015*f7c14bbaSAndroid Build Coastguard Worker char probe_name[PATH_MAX + 64];
12016*f7c14bbaSAndroid Build Coastguard Worker
12017*f7c14bbaSAndroid Build Coastguard Worker if (ref_ctr_off)
12018*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(-EINVAL);
12019*f7c14bbaSAndroid Build Coastguard Worker
12020*f7c14bbaSAndroid Build Coastguard Worker gen_uprobe_legacy_event_name(probe_name, sizeof(probe_name),
12021*f7c14bbaSAndroid Build Coastguard Worker binary_path, func_offset);
12022*f7c14bbaSAndroid Build Coastguard Worker
12023*f7c14bbaSAndroid Build Coastguard Worker legacy_probe = strdup(probe_name);
12024*f7c14bbaSAndroid Build Coastguard Worker if (!legacy_probe)
12025*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(-ENOMEM);
12026*f7c14bbaSAndroid Build Coastguard Worker
12027*f7c14bbaSAndroid Build Coastguard Worker pfd = perf_event_uprobe_open_legacy(legacy_probe, retprobe,
12028*f7c14bbaSAndroid Build Coastguard Worker binary_path, func_offset, pid);
12029*f7c14bbaSAndroid Build Coastguard Worker }
12030*f7c14bbaSAndroid Build Coastguard Worker if (pfd < 0) {
12031*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
12032*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': failed to create %s '%s:0x%zx' perf event: %s\n",
12033*f7c14bbaSAndroid Build Coastguard Worker prog->name, retprobe ? "uretprobe" : "uprobe",
12034*f7c14bbaSAndroid Build Coastguard Worker binary_path, func_offset,
12035*f7c14bbaSAndroid Build Coastguard Worker libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
12036*f7c14bbaSAndroid Build Coastguard Worker goto err_out;
12037*f7c14bbaSAndroid Build Coastguard Worker }
12038*f7c14bbaSAndroid Build Coastguard Worker
12039*f7c14bbaSAndroid Build Coastguard Worker link = bpf_program__attach_perf_event_opts(prog, pfd, &pe_opts);
12040*f7c14bbaSAndroid Build Coastguard Worker err = libbpf_get_error(link);
12041*f7c14bbaSAndroid Build Coastguard Worker if (err) {
12042*f7c14bbaSAndroid Build Coastguard Worker close(pfd);
12043*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': failed to attach to %s '%s:0x%zx': %s\n",
12044*f7c14bbaSAndroid Build Coastguard Worker prog->name, retprobe ? "uretprobe" : "uprobe",
12045*f7c14bbaSAndroid Build Coastguard Worker binary_path, func_offset,
12046*f7c14bbaSAndroid Build Coastguard Worker libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
12047*f7c14bbaSAndroid Build Coastguard Worker goto err_clean_legacy;
12048*f7c14bbaSAndroid Build Coastguard Worker }
12049*f7c14bbaSAndroid Build Coastguard Worker if (legacy) {
12050*f7c14bbaSAndroid Build Coastguard Worker struct bpf_link_perf *perf_link = container_of(link, struct bpf_link_perf, link);
12051*f7c14bbaSAndroid Build Coastguard Worker
12052*f7c14bbaSAndroid Build Coastguard Worker perf_link->legacy_probe_name = legacy_probe;
12053*f7c14bbaSAndroid Build Coastguard Worker perf_link->legacy_is_kprobe = false;
12054*f7c14bbaSAndroid Build Coastguard Worker perf_link->legacy_is_retprobe = retprobe;
12055*f7c14bbaSAndroid Build Coastguard Worker }
12056*f7c14bbaSAndroid Build Coastguard Worker return link;
12057*f7c14bbaSAndroid Build Coastguard Worker
12058*f7c14bbaSAndroid Build Coastguard Worker err_clean_legacy:
12059*f7c14bbaSAndroid Build Coastguard Worker if (legacy)
12060*f7c14bbaSAndroid Build Coastguard Worker remove_uprobe_event_legacy(legacy_probe, retprobe);
12061*f7c14bbaSAndroid Build Coastguard Worker err_out:
12062*f7c14bbaSAndroid Build Coastguard Worker free(legacy_probe);
12063*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(err);
12064*f7c14bbaSAndroid Build Coastguard Worker }
12065*f7c14bbaSAndroid Build Coastguard Worker
12066*f7c14bbaSAndroid Build Coastguard Worker /* Format of u[ret]probe section definition supporting auto-attach:
12067*f7c14bbaSAndroid Build Coastguard Worker * u[ret]probe/binary:function[+offset]
12068*f7c14bbaSAndroid Build Coastguard Worker *
12069*f7c14bbaSAndroid Build Coastguard Worker * binary can be an absolute/relative path or a filename; the latter is resolved to a
12070*f7c14bbaSAndroid Build Coastguard Worker * full binary path via bpf_program__attach_uprobe_opts.
12071*f7c14bbaSAndroid Build Coastguard Worker *
12072*f7c14bbaSAndroid Build Coastguard Worker * Specifying uprobe+ ensures we carry out strict matching; either "uprobe" must be
12073*f7c14bbaSAndroid Build Coastguard Worker * specified (and auto-attach is not possible) or the above format is specified for
12074*f7c14bbaSAndroid Build Coastguard Worker * auto-attach.
12075*f7c14bbaSAndroid Build Coastguard Worker */
attach_uprobe(const struct bpf_program * prog,long cookie,struct bpf_link ** link)12076*f7c14bbaSAndroid Build Coastguard Worker static int attach_uprobe(const struct bpf_program *prog, long cookie, struct bpf_link **link)
12077*f7c14bbaSAndroid Build Coastguard Worker {
12078*f7c14bbaSAndroid Build Coastguard Worker DECLARE_LIBBPF_OPTS(bpf_uprobe_opts, opts);
12079*f7c14bbaSAndroid Build Coastguard Worker char *probe_type = NULL, *binary_path = NULL, *func_name = NULL, *func_off;
12080*f7c14bbaSAndroid Build Coastguard Worker int n, c, ret = -EINVAL;
12081*f7c14bbaSAndroid Build Coastguard Worker long offset = 0;
12082*f7c14bbaSAndroid Build Coastguard Worker
12083*f7c14bbaSAndroid Build Coastguard Worker *link = NULL;
12084*f7c14bbaSAndroid Build Coastguard Worker
12085*f7c14bbaSAndroid Build Coastguard Worker n = sscanf(prog->sec_name, "%m[^/]/%m[^:]:%m[^\n]",
12086*f7c14bbaSAndroid Build Coastguard Worker &probe_type, &binary_path, &func_name);
12087*f7c14bbaSAndroid Build Coastguard Worker switch (n) {
12088*f7c14bbaSAndroid Build Coastguard Worker case 1:
12089*f7c14bbaSAndroid Build Coastguard Worker /* handle SEC("u[ret]probe") - format is valid, but auto-attach is impossible. */
12090*f7c14bbaSAndroid Build Coastguard Worker ret = 0;
12091*f7c14bbaSAndroid Build Coastguard Worker break;
12092*f7c14bbaSAndroid Build Coastguard Worker case 2:
12093*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': section '%s' missing ':function[+offset]' specification\n",
12094*f7c14bbaSAndroid Build Coastguard Worker prog->name, prog->sec_name);
12095*f7c14bbaSAndroid Build Coastguard Worker break;
12096*f7c14bbaSAndroid Build Coastguard Worker case 3:
12097*f7c14bbaSAndroid Build Coastguard Worker /* check if user specifies `+offset`, if yes, this should be
12098*f7c14bbaSAndroid Build Coastguard Worker * the last part of the string, make sure sscanf read to EOL
12099*f7c14bbaSAndroid Build Coastguard Worker */
12100*f7c14bbaSAndroid Build Coastguard Worker func_off = strrchr(func_name, '+');
12101*f7c14bbaSAndroid Build Coastguard Worker if (func_off) {
12102*f7c14bbaSAndroid Build Coastguard Worker n = sscanf(func_off, "+%li%n", &offset, &c);
12103*f7c14bbaSAndroid Build Coastguard Worker if (n == 1 && *(func_off + c) == '\0')
12104*f7c14bbaSAndroid Build Coastguard Worker func_off[0] = '\0';
12105*f7c14bbaSAndroid Build Coastguard Worker else
12106*f7c14bbaSAndroid Build Coastguard Worker offset = 0;
12107*f7c14bbaSAndroid Build Coastguard Worker }
12108*f7c14bbaSAndroid Build Coastguard Worker opts.retprobe = strcmp(probe_type, "uretprobe") == 0 ||
12109*f7c14bbaSAndroid Build Coastguard Worker strcmp(probe_type, "uretprobe.s") == 0;
12110*f7c14bbaSAndroid Build Coastguard Worker if (opts.retprobe && offset != 0) {
12111*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': uretprobes do not support offset specification\n",
12112*f7c14bbaSAndroid Build Coastguard Worker prog->name);
12113*f7c14bbaSAndroid Build Coastguard Worker break;
12114*f7c14bbaSAndroid Build Coastguard Worker }
12115*f7c14bbaSAndroid Build Coastguard Worker opts.func_name = func_name;
12116*f7c14bbaSAndroid Build Coastguard Worker *link = bpf_program__attach_uprobe_opts(prog, -1, binary_path, offset, &opts);
12117*f7c14bbaSAndroid Build Coastguard Worker ret = libbpf_get_error(*link);
12118*f7c14bbaSAndroid Build Coastguard Worker break;
12119*f7c14bbaSAndroid Build Coastguard Worker default:
12120*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': invalid format of section definition '%s'\n", prog->name,
12121*f7c14bbaSAndroid Build Coastguard Worker prog->sec_name);
12122*f7c14bbaSAndroid Build Coastguard Worker break;
12123*f7c14bbaSAndroid Build Coastguard Worker }
12124*f7c14bbaSAndroid Build Coastguard Worker free(probe_type);
12125*f7c14bbaSAndroid Build Coastguard Worker free(binary_path);
12126*f7c14bbaSAndroid Build Coastguard Worker free(func_name);
12127*f7c14bbaSAndroid Build Coastguard Worker
12128*f7c14bbaSAndroid Build Coastguard Worker return ret;
12129*f7c14bbaSAndroid Build Coastguard Worker }
12130*f7c14bbaSAndroid Build Coastguard Worker
bpf_program__attach_uprobe(const struct bpf_program * prog,bool retprobe,pid_t pid,const char * binary_path,size_t func_offset)12131*f7c14bbaSAndroid Build Coastguard Worker struct bpf_link *bpf_program__attach_uprobe(const struct bpf_program *prog,
12132*f7c14bbaSAndroid Build Coastguard Worker bool retprobe, pid_t pid,
12133*f7c14bbaSAndroid Build Coastguard Worker const char *binary_path,
12134*f7c14bbaSAndroid Build Coastguard Worker size_t func_offset)
12135*f7c14bbaSAndroid Build Coastguard Worker {
12136*f7c14bbaSAndroid Build Coastguard Worker DECLARE_LIBBPF_OPTS(bpf_uprobe_opts, opts, .retprobe = retprobe);
12137*f7c14bbaSAndroid Build Coastguard Worker
12138*f7c14bbaSAndroid Build Coastguard Worker return bpf_program__attach_uprobe_opts(prog, pid, binary_path, func_offset, &opts);
12139*f7c14bbaSAndroid Build Coastguard Worker }
12140*f7c14bbaSAndroid Build Coastguard Worker
bpf_program__attach_usdt(const struct bpf_program * prog,pid_t pid,const char * binary_path,const char * usdt_provider,const char * usdt_name,const struct bpf_usdt_opts * opts)12141*f7c14bbaSAndroid Build Coastguard Worker struct bpf_link *bpf_program__attach_usdt(const struct bpf_program *prog,
12142*f7c14bbaSAndroid Build Coastguard Worker pid_t pid, const char *binary_path,
12143*f7c14bbaSAndroid Build Coastguard Worker const char *usdt_provider, const char *usdt_name,
12144*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_usdt_opts *opts)
12145*f7c14bbaSAndroid Build Coastguard Worker {
12146*f7c14bbaSAndroid Build Coastguard Worker char resolved_path[512];
12147*f7c14bbaSAndroid Build Coastguard Worker struct bpf_object *obj = prog->obj;
12148*f7c14bbaSAndroid Build Coastguard Worker struct bpf_link *link;
12149*f7c14bbaSAndroid Build Coastguard Worker __u64 usdt_cookie;
12150*f7c14bbaSAndroid Build Coastguard Worker int err;
12151*f7c14bbaSAndroid Build Coastguard Worker
12152*f7c14bbaSAndroid Build Coastguard Worker if (!OPTS_VALID(opts, bpf_uprobe_opts))
12153*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(-EINVAL);
12154*f7c14bbaSAndroid Build Coastguard Worker
12155*f7c14bbaSAndroid Build Coastguard Worker if (bpf_program__fd(prog) < 0) {
12156*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': can't attach BPF program without FD (was it loaded?)\n",
12157*f7c14bbaSAndroid Build Coastguard Worker prog->name);
12158*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(-EINVAL);
12159*f7c14bbaSAndroid Build Coastguard Worker }
12160*f7c14bbaSAndroid Build Coastguard Worker
12161*f7c14bbaSAndroid Build Coastguard Worker if (!binary_path)
12162*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(-EINVAL);
12163*f7c14bbaSAndroid Build Coastguard Worker
12164*f7c14bbaSAndroid Build Coastguard Worker if (!strchr(binary_path, '/')) {
12165*f7c14bbaSAndroid Build Coastguard Worker err = resolve_full_path(binary_path, resolved_path, sizeof(resolved_path));
12166*f7c14bbaSAndroid Build Coastguard Worker if (err) {
12167*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': failed to resolve full path for '%s': %d\n",
12168*f7c14bbaSAndroid Build Coastguard Worker prog->name, binary_path, err);
12169*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(err);
12170*f7c14bbaSAndroid Build Coastguard Worker }
12171*f7c14bbaSAndroid Build Coastguard Worker binary_path = resolved_path;
12172*f7c14bbaSAndroid Build Coastguard Worker }
12173*f7c14bbaSAndroid Build Coastguard Worker
12174*f7c14bbaSAndroid Build Coastguard Worker /* USDT manager is instantiated lazily on first USDT attach. It will
12175*f7c14bbaSAndroid Build Coastguard Worker * be destroyed together with BPF object in bpf_object__close().
12176*f7c14bbaSAndroid Build Coastguard Worker */
12177*f7c14bbaSAndroid Build Coastguard Worker if (IS_ERR(obj->usdt_man))
12178*f7c14bbaSAndroid Build Coastguard Worker return libbpf_ptr(obj->usdt_man);
12179*f7c14bbaSAndroid Build Coastguard Worker if (!obj->usdt_man) {
12180*f7c14bbaSAndroid Build Coastguard Worker obj->usdt_man = usdt_manager_new(obj);
12181*f7c14bbaSAndroid Build Coastguard Worker if (IS_ERR(obj->usdt_man))
12182*f7c14bbaSAndroid Build Coastguard Worker return libbpf_ptr(obj->usdt_man);
12183*f7c14bbaSAndroid Build Coastguard Worker }
12184*f7c14bbaSAndroid Build Coastguard Worker
12185*f7c14bbaSAndroid Build Coastguard Worker usdt_cookie = OPTS_GET(opts, usdt_cookie, 0);
12186*f7c14bbaSAndroid Build Coastguard Worker link = usdt_manager_attach_usdt(obj->usdt_man, prog, pid, binary_path,
12187*f7c14bbaSAndroid Build Coastguard Worker usdt_provider, usdt_name, usdt_cookie);
12188*f7c14bbaSAndroid Build Coastguard Worker err = libbpf_get_error(link);
12189*f7c14bbaSAndroid Build Coastguard Worker if (err)
12190*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(err);
12191*f7c14bbaSAndroid Build Coastguard Worker return link;
12192*f7c14bbaSAndroid Build Coastguard Worker }
12193*f7c14bbaSAndroid Build Coastguard Worker
attach_usdt(const struct bpf_program * prog,long cookie,struct bpf_link ** link)12194*f7c14bbaSAndroid Build Coastguard Worker static int attach_usdt(const struct bpf_program *prog, long cookie, struct bpf_link **link)
12195*f7c14bbaSAndroid Build Coastguard Worker {
12196*f7c14bbaSAndroid Build Coastguard Worker char *path = NULL, *provider = NULL, *name = NULL;
12197*f7c14bbaSAndroid Build Coastguard Worker const char *sec_name;
12198*f7c14bbaSAndroid Build Coastguard Worker int n, err;
12199*f7c14bbaSAndroid Build Coastguard Worker
12200*f7c14bbaSAndroid Build Coastguard Worker sec_name = bpf_program__section_name(prog);
12201*f7c14bbaSAndroid Build Coastguard Worker if (strcmp(sec_name, "usdt") == 0) {
12202*f7c14bbaSAndroid Build Coastguard Worker /* no auto-attach for just SEC("usdt") */
12203*f7c14bbaSAndroid Build Coastguard Worker *link = NULL;
12204*f7c14bbaSAndroid Build Coastguard Worker return 0;
12205*f7c14bbaSAndroid Build Coastguard Worker }
12206*f7c14bbaSAndroid Build Coastguard Worker
12207*f7c14bbaSAndroid Build Coastguard Worker n = sscanf(sec_name, "usdt/%m[^:]:%m[^:]:%m[^:]", &path, &provider, &name);
12208*f7c14bbaSAndroid Build Coastguard Worker if (n != 3) {
12209*f7c14bbaSAndroid Build Coastguard Worker pr_warn("invalid section '%s', expected SEC(\"usdt/<path>:<provider>:<name>\")\n",
12210*f7c14bbaSAndroid Build Coastguard Worker sec_name);
12211*f7c14bbaSAndroid Build Coastguard Worker err = -EINVAL;
12212*f7c14bbaSAndroid Build Coastguard Worker } else {
12213*f7c14bbaSAndroid Build Coastguard Worker *link = bpf_program__attach_usdt(prog, -1 /* any process */, path,
12214*f7c14bbaSAndroid Build Coastguard Worker provider, name, NULL);
12215*f7c14bbaSAndroid Build Coastguard Worker err = libbpf_get_error(*link);
12216*f7c14bbaSAndroid Build Coastguard Worker }
12217*f7c14bbaSAndroid Build Coastguard Worker free(path);
12218*f7c14bbaSAndroid Build Coastguard Worker free(provider);
12219*f7c14bbaSAndroid Build Coastguard Worker free(name);
12220*f7c14bbaSAndroid Build Coastguard Worker return err;
12221*f7c14bbaSAndroid Build Coastguard Worker }
12222*f7c14bbaSAndroid Build Coastguard Worker
determine_tracepoint_id(const char * tp_category,const char * tp_name)12223*f7c14bbaSAndroid Build Coastguard Worker static int determine_tracepoint_id(const char *tp_category,
12224*f7c14bbaSAndroid Build Coastguard Worker const char *tp_name)
12225*f7c14bbaSAndroid Build Coastguard Worker {
12226*f7c14bbaSAndroid Build Coastguard Worker char file[PATH_MAX];
12227*f7c14bbaSAndroid Build Coastguard Worker int ret;
12228*f7c14bbaSAndroid Build Coastguard Worker
12229*f7c14bbaSAndroid Build Coastguard Worker ret = snprintf(file, sizeof(file), "%s/events/%s/%s/id",
12230*f7c14bbaSAndroid Build Coastguard Worker tracefs_path(), tp_category, tp_name);
12231*f7c14bbaSAndroid Build Coastguard Worker if (ret < 0)
12232*f7c14bbaSAndroid Build Coastguard Worker return -errno;
12233*f7c14bbaSAndroid Build Coastguard Worker if (ret >= sizeof(file)) {
12234*f7c14bbaSAndroid Build Coastguard Worker pr_debug("tracepoint %s/%s path is too long\n",
12235*f7c14bbaSAndroid Build Coastguard Worker tp_category, tp_name);
12236*f7c14bbaSAndroid Build Coastguard Worker return -E2BIG;
12237*f7c14bbaSAndroid Build Coastguard Worker }
12238*f7c14bbaSAndroid Build Coastguard Worker return parse_uint_from_file(file, "%d\n");
12239*f7c14bbaSAndroid Build Coastguard Worker }
12240*f7c14bbaSAndroid Build Coastguard Worker
perf_event_open_tracepoint(const char * tp_category,const char * tp_name)12241*f7c14bbaSAndroid Build Coastguard Worker static int perf_event_open_tracepoint(const char *tp_category,
12242*f7c14bbaSAndroid Build Coastguard Worker const char *tp_name)
12243*f7c14bbaSAndroid Build Coastguard Worker {
12244*f7c14bbaSAndroid Build Coastguard Worker const size_t attr_sz = sizeof(struct perf_event_attr);
12245*f7c14bbaSAndroid Build Coastguard Worker struct perf_event_attr attr;
12246*f7c14bbaSAndroid Build Coastguard Worker char errmsg[STRERR_BUFSIZE];
12247*f7c14bbaSAndroid Build Coastguard Worker int tp_id, pfd, err;
12248*f7c14bbaSAndroid Build Coastguard Worker
12249*f7c14bbaSAndroid Build Coastguard Worker tp_id = determine_tracepoint_id(tp_category, tp_name);
12250*f7c14bbaSAndroid Build Coastguard Worker if (tp_id < 0) {
12251*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to determine tracepoint '%s/%s' perf event ID: %s\n",
12252*f7c14bbaSAndroid Build Coastguard Worker tp_category, tp_name,
12253*f7c14bbaSAndroid Build Coastguard Worker libbpf_strerror_r(tp_id, errmsg, sizeof(errmsg)));
12254*f7c14bbaSAndroid Build Coastguard Worker return tp_id;
12255*f7c14bbaSAndroid Build Coastguard Worker }
12256*f7c14bbaSAndroid Build Coastguard Worker
12257*f7c14bbaSAndroid Build Coastguard Worker memset(&attr, 0, attr_sz);
12258*f7c14bbaSAndroid Build Coastguard Worker attr.type = PERF_TYPE_TRACEPOINT;
12259*f7c14bbaSAndroid Build Coastguard Worker attr.size = attr_sz;
12260*f7c14bbaSAndroid Build Coastguard Worker attr.config = tp_id;
12261*f7c14bbaSAndroid Build Coastguard Worker
12262*f7c14bbaSAndroid Build Coastguard Worker pfd = syscall(__NR_perf_event_open, &attr, -1 /* pid */, 0 /* cpu */,
12263*f7c14bbaSAndroid Build Coastguard Worker -1 /* group_fd */, PERF_FLAG_FD_CLOEXEC);
12264*f7c14bbaSAndroid Build Coastguard Worker if (pfd < 0) {
12265*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
12266*f7c14bbaSAndroid Build Coastguard Worker pr_warn("tracepoint '%s/%s' perf_event_open() failed: %s\n",
12267*f7c14bbaSAndroid Build Coastguard Worker tp_category, tp_name,
12268*f7c14bbaSAndroid Build Coastguard Worker libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
12269*f7c14bbaSAndroid Build Coastguard Worker return err;
12270*f7c14bbaSAndroid Build Coastguard Worker }
12271*f7c14bbaSAndroid Build Coastguard Worker return pfd;
12272*f7c14bbaSAndroid Build Coastguard Worker }
12273*f7c14bbaSAndroid Build Coastguard Worker
bpf_program__attach_tracepoint_opts(const struct bpf_program * prog,const char * tp_category,const char * tp_name,const struct bpf_tracepoint_opts * opts)12274*f7c14bbaSAndroid Build Coastguard Worker struct bpf_link *bpf_program__attach_tracepoint_opts(const struct bpf_program *prog,
12275*f7c14bbaSAndroid Build Coastguard Worker const char *tp_category,
12276*f7c14bbaSAndroid Build Coastguard Worker const char *tp_name,
12277*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_tracepoint_opts *opts)
12278*f7c14bbaSAndroid Build Coastguard Worker {
12279*f7c14bbaSAndroid Build Coastguard Worker DECLARE_LIBBPF_OPTS(bpf_perf_event_opts, pe_opts);
12280*f7c14bbaSAndroid Build Coastguard Worker char errmsg[STRERR_BUFSIZE];
12281*f7c14bbaSAndroid Build Coastguard Worker struct bpf_link *link;
12282*f7c14bbaSAndroid Build Coastguard Worker int pfd, err;
12283*f7c14bbaSAndroid Build Coastguard Worker
12284*f7c14bbaSAndroid Build Coastguard Worker if (!OPTS_VALID(opts, bpf_tracepoint_opts))
12285*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(-EINVAL);
12286*f7c14bbaSAndroid Build Coastguard Worker
12287*f7c14bbaSAndroid Build Coastguard Worker pe_opts.bpf_cookie = OPTS_GET(opts, bpf_cookie, 0);
12288*f7c14bbaSAndroid Build Coastguard Worker
12289*f7c14bbaSAndroid Build Coastguard Worker pfd = perf_event_open_tracepoint(tp_category, tp_name);
12290*f7c14bbaSAndroid Build Coastguard Worker if (pfd < 0) {
12291*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': failed to create tracepoint '%s/%s' perf event: %s\n",
12292*f7c14bbaSAndroid Build Coastguard Worker prog->name, tp_category, tp_name,
12293*f7c14bbaSAndroid Build Coastguard Worker libbpf_strerror_r(pfd, errmsg, sizeof(errmsg)));
12294*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(pfd);
12295*f7c14bbaSAndroid Build Coastguard Worker }
12296*f7c14bbaSAndroid Build Coastguard Worker link = bpf_program__attach_perf_event_opts(prog, pfd, &pe_opts);
12297*f7c14bbaSAndroid Build Coastguard Worker err = libbpf_get_error(link);
12298*f7c14bbaSAndroid Build Coastguard Worker if (err) {
12299*f7c14bbaSAndroid Build Coastguard Worker close(pfd);
12300*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': failed to attach to tracepoint '%s/%s': %s\n",
12301*f7c14bbaSAndroid Build Coastguard Worker prog->name, tp_category, tp_name,
12302*f7c14bbaSAndroid Build Coastguard Worker libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
12303*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(err);
12304*f7c14bbaSAndroid Build Coastguard Worker }
12305*f7c14bbaSAndroid Build Coastguard Worker return link;
12306*f7c14bbaSAndroid Build Coastguard Worker }
12307*f7c14bbaSAndroid Build Coastguard Worker
bpf_program__attach_tracepoint(const struct bpf_program * prog,const char * tp_category,const char * tp_name)12308*f7c14bbaSAndroid Build Coastguard Worker struct bpf_link *bpf_program__attach_tracepoint(const struct bpf_program *prog,
12309*f7c14bbaSAndroid Build Coastguard Worker const char *tp_category,
12310*f7c14bbaSAndroid Build Coastguard Worker const char *tp_name)
12311*f7c14bbaSAndroid Build Coastguard Worker {
12312*f7c14bbaSAndroid Build Coastguard Worker return bpf_program__attach_tracepoint_opts(prog, tp_category, tp_name, NULL);
12313*f7c14bbaSAndroid Build Coastguard Worker }
12314*f7c14bbaSAndroid Build Coastguard Worker
attach_tp(const struct bpf_program * prog,long cookie,struct bpf_link ** link)12315*f7c14bbaSAndroid Build Coastguard Worker static int attach_tp(const struct bpf_program *prog, long cookie, struct bpf_link **link)
12316*f7c14bbaSAndroid Build Coastguard Worker {
12317*f7c14bbaSAndroid Build Coastguard Worker char *sec_name, *tp_cat, *tp_name;
12318*f7c14bbaSAndroid Build Coastguard Worker
12319*f7c14bbaSAndroid Build Coastguard Worker *link = NULL;
12320*f7c14bbaSAndroid Build Coastguard Worker
12321*f7c14bbaSAndroid Build Coastguard Worker /* no auto-attach for SEC("tp") or SEC("tracepoint") */
12322*f7c14bbaSAndroid Build Coastguard Worker if (strcmp(prog->sec_name, "tp") == 0 || strcmp(prog->sec_name, "tracepoint") == 0)
12323*f7c14bbaSAndroid Build Coastguard Worker return 0;
12324*f7c14bbaSAndroid Build Coastguard Worker
12325*f7c14bbaSAndroid Build Coastguard Worker sec_name = strdup(prog->sec_name);
12326*f7c14bbaSAndroid Build Coastguard Worker if (!sec_name)
12327*f7c14bbaSAndroid Build Coastguard Worker return -ENOMEM;
12328*f7c14bbaSAndroid Build Coastguard Worker
12329*f7c14bbaSAndroid Build Coastguard Worker /* extract "tp/<category>/<name>" or "tracepoint/<category>/<name>" */
12330*f7c14bbaSAndroid Build Coastguard Worker if (str_has_pfx(prog->sec_name, "tp/"))
12331*f7c14bbaSAndroid Build Coastguard Worker tp_cat = sec_name + sizeof("tp/") - 1;
12332*f7c14bbaSAndroid Build Coastguard Worker else
12333*f7c14bbaSAndroid Build Coastguard Worker tp_cat = sec_name + sizeof("tracepoint/") - 1;
12334*f7c14bbaSAndroid Build Coastguard Worker tp_name = strchr(tp_cat, '/');
12335*f7c14bbaSAndroid Build Coastguard Worker if (!tp_name) {
12336*f7c14bbaSAndroid Build Coastguard Worker free(sec_name);
12337*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
12338*f7c14bbaSAndroid Build Coastguard Worker }
12339*f7c14bbaSAndroid Build Coastguard Worker *tp_name = '\0';
12340*f7c14bbaSAndroid Build Coastguard Worker tp_name++;
12341*f7c14bbaSAndroid Build Coastguard Worker
12342*f7c14bbaSAndroid Build Coastguard Worker *link = bpf_program__attach_tracepoint(prog, tp_cat, tp_name);
12343*f7c14bbaSAndroid Build Coastguard Worker free(sec_name);
12344*f7c14bbaSAndroid Build Coastguard Worker return libbpf_get_error(*link);
12345*f7c14bbaSAndroid Build Coastguard Worker }
12346*f7c14bbaSAndroid Build Coastguard Worker
12347*f7c14bbaSAndroid Build Coastguard Worker struct bpf_link *
bpf_program__attach_raw_tracepoint_opts(const struct bpf_program * prog,const char * tp_name,struct bpf_raw_tracepoint_opts * opts)12348*f7c14bbaSAndroid Build Coastguard Worker bpf_program__attach_raw_tracepoint_opts(const struct bpf_program *prog,
12349*f7c14bbaSAndroid Build Coastguard Worker const char *tp_name,
12350*f7c14bbaSAndroid Build Coastguard Worker struct bpf_raw_tracepoint_opts *opts)
12351*f7c14bbaSAndroid Build Coastguard Worker {
12352*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_OPTS(bpf_raw_tp_opts, raw_opts);
12353*f7c14bbaSAndroid Build Coastguard Worker char errmsg[STRERR_BUFSIZE];
12354*f7c14bbaSAndroid Build Coastguard Worker struct bpf_link *link;
12355*f7c14bbaSAndroid Build Coastguard Worker int prog_fd, pfd;
12356*f7c14bbaSAndroid Build Coastguard Worker
12357*f7c14bbaSAndroid Build Coastguard Worker if (!OPTS_VALID(opts, bpf_raw_tracepoint_opts))
12358*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(-EINVAL);
12359*f7c14bbaSAndroid Build Coastguard Worker
12360*f7c14bbaSAndroid Build Coastguard Worker prog_fd = bpf_program__fd(prog);
12361*f7c14bbaSAndroid Build Coastguard Worker if (prog_fd < 0) {
12362*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': can't attach before loaded\n", prog->name);
12363*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(-EINVAL);
12364*f7c14bbaSAndroid Build Coastguard Worker }
12365*f7c14bbaSAndroid Build Coastguard Worker
12366*f7c14bbaSAndroid Build Coastguard Worker link = calloc(1, sizeof(*link));
12367*f7c14bbaSAndroid Build Coastguard Worker if (!link)
12368*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(-ENOMEM);
12369*f7c14bbaSAndroid Build Coastguard Worker link->detach = &bpf_link__detach_fd;
12370*f7c14bbaSAndroid Build Coastguard Worker
12371*f7c14bbaSAndroid Build Coastguard Worker raw_opts.tp_name = tp_name;
12372*f7c14bbaSAndroid Build Coastguard Worker raw_opts.cookie = OPTS_GET(opts, cookie, 0);
12373*f7c14bbaSAndroid Build Coastguard Worker pfd = bpf_raw_tracepoint_open_opts(prog_fd, &raw_opts);
12374*f7c14bbaSAndroid Build Coastguard Worker if (pfd < 0) {
12375*f7c14bbaSAndroid Build Coastguard Worker pfd = -errno;
12376*f7c14bbaSAndroid Build Coastguard Worker free(link);
12377*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': failed to attach to raw tracepoint '%s': %s\n",
12378*f7c14bbaSAndroid Build Coastguard Worker prog->name, tp_name, libbpf_strerror_r(pfd, errmsg, sizeof(errmsg)));
12379*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(pfd);
12380*f7c14bbaSAndroid Build Coastguard Worker }
12381*f7c14bbaSAndroid Build Coastguard Worker link->fd = pfd;
12382*f7c14bbaSAndroid Build Coastguard Worker return link;
12383*f7c14bbaSAndroid Build Coastguard Worker }
12384*f7c14bbaSAndroid Build Coastguard Worker
bpf_program__attach_raw_tracepoint(const struct bpf_program * prog,const char * tp_name)12385*f7c14bbaSAndroid Build Coastguard Worker struct bpf_link *bpf_program__attach_raw_tracepoint(const struct bpf_program *prog,
12386*f7c14bbaSAndroid Build Coastguard Worker const char *tp_name)
12387*f7c14bbaSAndroid Build Coastguard Worker {
12388*f7c14bbaSAndroid Build Coastguard Worker return bpf_program__attach_raw_tracepoint_opts(prog, tp_name, NULL);
12389*f7c14bbaSAndroid Build Coastguard Worker }
12390*f7c14bbaSAndroid Build Coastguard Worker
attach_raw_tp(const struct bpf_program * prog,long cookie,struct bpf_link ** link)12391*f7c14bbaSAndroid Build Coastguard Worker static int attach_raw_tp(const struct bpf_program *prog, long cookie, struct bpf_link **link)
12392*f7c14bbaSAndroid Build Coastguard Worker {
12393*f7c14bbaSAndroid Build Coastguard Worker static const char *const prefixes[] = {
12394*f7c14bbaSAndroid Build Coastguard Worker "raw_tp",
12395*f7c14bbaSAndroid Build Coastguard Worker "raw_tracepoint",
12396*f7c14bbaSAndroid Build Coastguard Worker "raw_tp.w",
12397*f7c14bbaSAndroid Build Coastguard Worker "raw_tracepoint.w",
12398*f7c14bbaSAndroid Build Coastguard Worker };
12399*f7c14bbaSAndroid Build Coastguard Worker size_t i;
12400*f7c14bbaSAndroid Build Coastguard Worker const char *tp_name = NULL;
12401*f7c14bbaSAndroid Build Coastguard Worker
12402*f7c14bbaSAndroid Build Coastguard Worker *link = NULL;
12403*f7c14bbaSAndroid Build Coastguard Worker
12404*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < ARRAY_SIZE(prefixes); i++) {
12405*f7c14bbaSAndroid Build Coastguard Worker size_t pfx_len;
12406*f7c14bbaSAndroid Build Coastguard Worker
12407*f7c14bbaSAndroid Build Coastguard Worker if (!str_has_pfx(prog->sec_name, prefixes[i]))
12408*f7c14bbaSAndroid Build Coastguard Worker continue;
12409*f7c14bbaSAndroid Build Coastguard Worker
12410*f7c14bbaSAndroid Build Coastguard Worker pfx_len = strlen(prefixes[i]);
12411*f7c14bbaSAndroid Build Coastguard Worker /* no auto-attach case of, e.g., SEC("raw_tp") */
12412*f7c14bbaSAndroid Build Coastguard Worker if (prog->sec_name[pfx_len] == '\0')
12413*f7c14bbaSAndroid Build Coastguard Worker return 0;
12414*f7c14bbaSAndroid Build Coastguard Worker
12415*f7c14bbaSAndroid Build Coastguard Worker if (prog->sec_name[pfx_len] != '/')
12416*f7c14bbaSAndroid Build Coastguard Worker continue;
12417*f7c14bbaSAndroid Build Coastguard Worker
12418*f7c14bbaSAndroid Build Coastguard Worker tp_name = prog->sec_name + pfx_len + 1;
12419*f7c14bbaSAndroid Build Coastguard Worker break;
12420*f7c14bbaSAndroid Build Coastguard Worker }
12421*f7c14bbaSAndroid Build Coastguard Worker
12422*f7c14bbaSAndroid Build Coastguard Worker if (!tp_name) {
12423*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': invalid section name '%s'\n",
12424*f7c14bbaSAndroid Build Coastguard Worker prog->name, prog->sec_name);
12425*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
12426*f7c14bbaSAndroid Build Coastguard Worker }
12427*f7c14bbaSAndroid Build Coastguard Worker
12428*f7c14bbaSAndroid Build Coastguard Worker *link = bpf_program__attach_raw_tracepoint(prog, tp_name);
12429*f7c14bbaSAndroid Build Coastguard Worker return libbpf_get_error(*link);
12430*f7c14bbaSAndroid Build Coastguard Worker }
12431*f7c14bbaSAndroid Build Coastguard Worker
12432*f7c14bbaSAndroid Build Coastguard Worker /* Common logic for all BPF program types that attach to a btf_id */
bpf_program__attach_btf_id(const struct bpf_program * prog,const struct bpf_trace_opts * opts)12433*f7c14bbaSAndroid Build Coastguard Worker static struct bpf_link *bpf_program__attach_btf_id(const struct bpf_program *prog,
12434*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_trace_opts *opts)
12435*f7c14bbaSAndroid Build Coastguard Worker {
12436*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_OPTS(bpf_link_create_opts, link_opts);
12437*f7c14bbaSAndroid Build Coastguard Worker char errmsg[STRERR_BUFSIZE];
12438*f7c14bbaSAndroid Build Coastguard Worker struct bpf_link *link;
12439*f7c14bbaSAndroid Build Coastguard Worker int prog_fd, pfd;
12440*f7c14bbaSAndroid Build Coastguard Worker
12441*f7c14bbaSAndroid Build Coastguard Worker if (!OPTS_VALID(opts, bpf_trace_opts))
12442*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(-EINVAL);
12443*f7c14bbaSAndroid Build Coastguard Worker
12444*f7c14bbaSAndroid Build Coastguard Worker prog_fd = bpf_program__fd(prog);
12445*f7c14bbaSAndroid Build Coastguard Worker if (prog_fd < 0) {
12446*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': can't attach before loaded\n", prog->name);
12447*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(-EINVAL);
12448*f7c14bbaSAndroid Build Coastguard Worker }
12449*f7c14bbaSAndroid Build Coastguard Worker
12450*f7c14bbaSAndroid Build Coastguard Worker link = calloc(1, sizeof(*link));
12451*f7c14bbaSAndroid Build Coastguard Worker if (!link)
12452*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(-ENOMEM);
12453*f7c14bbaSAndroid Build Coastguard Worker link->detach = &bpf_link__detach_fd;
12454*f7c14bbaSAndroid Build Coastguard Worker
12455*f7c14bbaSAndroid Build Coastguard Worker /* libbpf is smart enough to redirect to BPF_RAW_TRACEPOINT_OPEN on old kernels */
12456*f7c14bbaSAndroid Build Coastguard Worker link_opts.tracing.cookie = OPTS_GET(opts, cookie, 0);
12457*f7c14bbaSAndroid Build Coastguard Worker pfd = bpf_link_create(prog_fd, 0, bpf_program__expected_attach_type(prog), &link_opts);
12458*f7c14bbaSAndroid Build Coastguard Worker if (pfd < 0) {
12459*f7c14bbaSAndroid Build Coastguard Worker pfd = -errno;
12460*f7c14bbaSAndroid Build Coastguard Worker free(link);
12461*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': failed to attach: %s\n",
12462*f7c14bbaSAndroid Build Coastguard Worker prog->name, libbpf_strerror_r(pfd, errmsg, sizeof(errmsg)));
12463*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(pfd);
12464*f7c14bbaSAndroid Build Coastguard Worker }
12465*f7c14bbaSAndroid Build Coastguard Worker link->fd = pfd;
12466*f7c14bbaSAndroid Build Coastguard Worker return link;
12467*f7c14bbaSAndroid Build Coastguard Worker }
12468*f7c14bbaSAndroid Build Coastguard Worker
bpf_program__attach_trace(const struct bpf_program * prog)12469*f7c14bbaSAndroid Build Coastguard Worker struct bpf_link *bpf_program__attach_trace(const struct bpf_program *prog)
12470*f7c14bbaSAndroid Build Coastguard Worker {
12471*f7c14bbaSAndroid Build Coastguard Worker return bpf_program__attach_btf_id(prog, NULL);
12472*f7c14bbaSAndroid Build Coastguard Worker }
12473*f7c14bbaSAndroid Build Coastguard Worker
bpf_program__attach_trace_opts(const struct bpf_program * prog,const struct bpf_trace_opts * opts)12474*f7c14bbaSAndroid Build Coastguard Worker struct bpf_link *bpf_program__attach_trace_opts(const struct bpf_program *prog,
12475*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_trace_opts *opts)
12476*f7c14bbaSAndroid Build Coastguard Worker {
12477*f7c14bbaSAndroid Build Coastguard Worker return bpf_program__attach_btf_id(prog, opts);
12478*f7c14bbaSAndroid Build Coastguard Worker }
12479*f7c14bbaSAndroid Build Coastguard Worker
bpf_program__attach_lsm(const struct bpf_program * prog)12480*f7c14bbaSAndroid Build Coastguard Worker struct bpf_link *bpf_program__attach_lsm(const struct bpf_program *prog)
12481*f7c14bbaSAndroid Build Coastguard Worker {
12482*f7c14bbaSAndroid Build Coastguard Worker return bpf_program__attach_btf_id(prog, NULL);
12483*f7c14bbaSAndroid Build Coastguard Worker }
12484*f7c14bbaSAndroid Build Coastguard Worker
attach_trace(const struct bpf_program * prog,long cookie,struct bpf_link ** link)12485*f7c14bbaSAndroid Build Coastguard Worker static int attach_trace(const struct bpf_program *prog, long cookie, struct bpf_link **link)
12486*f7c14bbaSAndroid Build Coastguard Worker {
12487*f7c14bbaSAndroid Build Coastguard Worker *link = bpf_program__attach_trace(prog);
12488*f7c14bbaSAndroid Build Coastguard Worker return libbpf_get_error(*link);
12489*f7c14bbaSAndroid Build Coastguard Worker }
12490*f7c14bbaSAndroid Build Coastguard Worker
attach_lsm(const struct bpf_program * prog,long cookie,struct bpf_link ** link)12491*f7c14bbaSAndroid Build Coastguard Worker static int attach_lsm(const struct bpf_program *prog, long cookie, struct bpf_link **link)
12492*f7c14bbaSAndroid Build Coastguard Worker {
12493*f7c14bbaSAndroid Build Coastguard Worker *link = bpf_program__attach_lsm(prog);
12494*f7c14bbaSAndroid Build Coastguard Worker return libbpf_get_error(*link);
12495*f7c14bbaSAndroid Build Coastguard Worker }
12496*f7c14bbaSAndroid Build Coastguard Worker
12497*f7c14bbaSAndroid Build Coastguard Worker static struct bpf_link *
bpf_program_attach_fd(const struct bpf_program * prog,int target_fd,const char * target_name,const struct bpf_link_create_opts * opts)12498*f7c14bbaSAndroid Build Coastguard Worker bpf_program_attach_fd(const struct bpf_program *prog,
12499*f7c14bbaSAndroid Build Coastguard Worker int target_fd, const char *target_name,
12500*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_link_create_opts *opts)
12501*f7c14bbaSAndroid Build Coastguard Worker {
12502*f7c14bbaSAndroid Build Coastguard Worker enum bpf_attach_type attach_type;
12503*f7c14bbaSAndroid Build Coastguard Worker char errmsg[STRERR_BUFSIZE];
12504*f7c14bbaSAndroid Build Coastguard Worker struct bpf_link *link;
12505*f7c14bbaSAndroid Build Coastguard Worker int prog_fd, link_fd;
12506*f7c14bbaSAndroid Build Coastguard Worker
12507*f7c14bbaSAndroid Build Coastguard Worker prog_fd = bpf_program__fd(prog);
12508*f7c14bbaSAndroid Build Coastguard Worker if (prog_fd < 0) {
12509*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': can't attach before loaded\n", prog->name);
12510*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(-EINVAL);
12511*f7c14bbaSAndroid Build Coastguard Worker }
12512*f7c14bbaSAndroid Build Coastguard Worker
12513*f7c14bbaSAndroid Build Coastguard Worker link = calloc(1, sizeof(*link));
12514*f7c14bbaSAndroid Build Coastguard Worker if (!link)
12515*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(-ENOMEM);
12516*f7c14bbaSAndroid Build Coastguard Worker link->detach = &bpf_link__detach_fd;
12517*f7c14bbaSAndroid Build Coastguard Worker
12518*f7c14bbaSAndroid Build Coastguard Worker attach_type = bpf_program__expected_attach_type(prog);
12519*f7c14bbaSAndroid Build Coastguard Worker link_fd = bpf_link_create(prog_fd, target_fd, attach_type, opts);
12520*f7c14bbaSAndroid Build Coastguard Worker if (link_fd < 0) {
12521*f7c14bbaSAndroid Build Coastguard Worker link_fd = -errno;
12522*f7c14bbaSAndroid Build Coastguard Worker free(link);
12523*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': failed to attach to %s: %s\n",
12524*f7c14bbaSAndroid Build Coastguard Worker prog->name, target_name,
12525*f7c14bbaSAndroid Build Coastguard Worker libbpf_strerror_r(link_fd, errmsg, sizeof(errmsg)));
12526*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(link_fd);
12527*f7c14bbaSAndroid Build Coastguard Worker }
12528*f7c14bbaSAndroid Build Coastguard Worker link->fd = link_fd;
12529*f7c14bbaSAndroid Build Coastguard Worker return link;
12530*f7c14bbaSAndroid Build Coastguard Worker }
12531*f7c14bbaSAndroid Build Coastguard Worker
12532*f7c14bbaSAndroid Build Coastguard Worker struct bpf_link *
bpf_program__attach_cgroup(const struct bpf_program * prog,int cgroup_fd)12533*f7c14bbaSAndroid Build Coastguard Worker bpf_program__attach_cgroup(const struct bpf_program *prog, int cgroup_fd)
12534*f7c14bbaSAndroid Build Coastguard Worker {
12535*f7c14bbaSAndroid Build Coastguard Worker return bpf_program_attach_fd(prog, cgroup_fd, "cgroup", NULL);
12536*f7c14bbaSAndroid Build Coastguard Worker }
12537*f7c14bbaSAndroid Build Coastguard Worker
12538*f7c14bbaSAndroid Build Coastguard Worker struct bpf_link *
bpf_program__attach_netns(const struct bpf_program * prog,int netns_fd)12539*f7c14bbaSAndroid Build Coastguard Worker bpf_program__attach_netns(const struct bpf_program *prog, int netns_fd)
12540*f7c14bbaSAndroid Build Coastguard Worker {
12541*f7c14bbaSAndroid Build Coastguard Worker return bpf_program_attach_fd(prog, netns_fd, "netns", NULL);
12542*f7c14bbaSAndroid Build Coastguard Worker }
12543*f7c14bbaSAndroid Build Coastguard Worker
bpf_program__attach_xdp(const struct bpf_program * prog,int ifindex)12544*f7c14bbaSAndroid Build Coastguard Worker struct bpf_link *bpf_program__attach_xdp(const struct bpf_program *prog, int ifindex)
12545*f7c14bbaSAndroid Build Coastguard Worker {
12546*f7c14bbaSAndroid Build Coastguard Worker /* target_fd/target_ifindex use the same field in LINK_CREATE */
12547*f7c14bbaSAndroid Build Coastguard Worker return bpf_program_attach_fd(prog, ifindex, "xdp", NULL);
12548*f7c14bbaSAndroid Build Coastguard Worker }
12549*f7c14bbaSAndroid Build Coastguard Worker
12550*f7c14bbaSAndroid Build Coastguard Worker struct bpf_link *
bpf_program__attach_tcx(const struct bpf_program * prog,int ifindex,const struct bpf_tcx_opts * opts)12551*f7c14bbaSAndroid Build Coastguard Worker bpf_program__attach_tcx(const struct bpf_program *prog, int ifindex,
12552*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_tcx_opts *opts)
12553*f7c14bbaSAndroid Build Coastguard Worker {
12554*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_OPTS(bpf_link_create_opts, link_create_opts);
12555*f7c14bbaSAndroid Build Coastguard Worker __u32 relative_id;
12556*f7c14bbaSAndroid Build Coastguard Worker int relative_fd;
12557*f7c14bbaSAndroid Build Coastguard Worker
12558*f7c14bbaSAndroid Build Coastguard Worker if (!OPTS_VALID(opts, bpf_tcx_opts))
12559*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(-EINVAL);
12560*f7c14bbaSAndroid Build Coastguard Worker
12561*f7c14bbaSAndroid Build Coastguard Worker relative_id = OPTS_GET(opts, relative_id, 0);
12562*f7c14bbaSAndroid Build Coastguard Worker relative_fd = OPTS_GET(opts, relative_fd, 0);
12563*f7c14bbaSAndroid Build Coastguard Worker
12564*f7c14bbaSAndroid Build Coastguard Worker /* validate we don't have unexpected combinations of non-zero fields */
12565*f7c14bbaSAndroid Build Coastguard Worker if (!ifindex) {
12566*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': target netdevice ifindex cannot be zero\n",
12567*f7c14bbaSAndroid Build Coastguard Worker prog->name);
12568*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(-EINVAL);
12569*f7c14bbaSAndroid Build Coastguard Worker }
12570*f7c14bbaSAndroid Build Coastguard Worker if (relative_fd && relative_id) {
12571*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': relative_fd and relative_id cannot be set at the same time\n",
12572*f7c14bbaSAndroid Build Coastguard Worker prog->name);
12573*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(-EINVAL);
12574*f7c14bbaSAndroid Build Coastguard Worker }
12575*f7c14bbaSAndroid Build Coastguard Worker
12576*f7c14bbaSAndroid Build Coastguard Worker link_create_opts.tcx.expected_revision = OPTS_GET(opts, expected_revision, 0);
12577*f7c14bbaSAndroid Build Coastguard Worker link_create_opts.tcx.relative_fd = relative_fd;
12578*f7c14bbaSAndroid Build Coastguard Worker link_create_opts.tcx.relative_id = relative_id;
12579*f7c14bbaSAndroid Build Coastguard Worker link_create_opts.flags = OPTS_GET(opts, flags, 0);
12580*f7c14bbaSAndroid Build Coastguard Worker
12581*f7c14bbaSAndroid Build Coastguard Worker /* target_fd/target_ifindex use the same field in LINK_CREATE */
12582*f7c14bbaSAndroid Build Coastguard Worker return bpf_program_attach_fd(prog, ifindex, "tcx", &link_create_opts);
12583*f7c14bbaSAndroid Build Coastguard Worker }
12584*f7c14bbaSAndroid Build Coastguard Worker
12585*f7c14bbaSAndroid Build Coastguard Worker struct bpf_link *
bpf_program__attach_netkit(const struct bpf_program * prog,int ifindex,const struct bpf_netkit_opts * opts)12586*f7c14bbaSAndroid Build Coastguard Worker bpf_program__attach_netkit(const struct bpf_program *prog, int ifindex,
12587*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_netkit_opts *opts)
12588*f7c14bbaSAndroid Build Coastguard Worker {
12589*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_OPTS(bpf_link_create_opts, link_create_opts);
12590*f7c14bbaSAndroid Build Coastguard Worker __u32 relative_id;
12591*f7c14bbaSAndroid Build Coastguard Worker int relative_fd;
12592*f7c14bbaSAndroid Build Coastguard Worker
12593*f7c14bbaSAndroid Build Coastguard Worker if (!OPTS_VALID(opts, bpf_netkit_opts))
12594*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(-EINVAL);
12595*f7c14bbaSAndroid Build Coastguard Worker
12596*f7c14bbaSAndroid Build Coastguard Worker relative_id = OPTS_GET(opts, relative_id, 0);
12597*f7c14bbaSAndroid Build Coastguard Worker relative_fd = OPTS_GET(opts, relative_fd, 0);
12598*f7c14bbaSAndroid Build Coastguard Worker
12599*f7c14bbaSAndroid Build Coastguard Worker /* validate we don't have unexpected combinations of non-zero fields */
12600*f7c14bbaSAndroid Build Coastguard Worker if (!ifindex) {
12601*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': target netdevice ifindex cannot be zero\n",
12602*f7c14bbaSAndroid Build Coastguard Worker prog->name);
12603*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(-EINVAL);
12604*f7c14bbaSAndroid Build Coastguard Worker }
12605*f7c14bbaSAndroid Build Coastguard Worker if (relative_fd && relative_id) {
12606*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': relative_fd and relative_id cannot be set at the same time\n",
12607*f7c14bbaSAndroid Build Coastguard Worker prog->name);
12608*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(-EINVAL);
12609*f7c14bbaSAndroid Build Coastguard Worker }
12610*f7c14bbaSAndroid Build Coastguard Worker
12611*f7c14bbaSAndroid Build Coastguard Worker link_create_opts.netkit.expected_revision = OPTS_GET(opts, expected_revision, 0);
12612*f7c14bbaSAndroid Build Coastguard Worker link_create_opts.netkit.relative_fd = relative_fd;
12613*f7c14bbaSAndroid Build Coastguard Worker link_create_opts.netkit.relative_id = relative_id;
12614*f7c14bbaSAndroid Build Coastguard Worker link_create_opts.flags = OPTS_GET(opts, flags, 0);
12615*f7c14bbaSAndroid Build Coastguard Worker
12616*f7c14bbaSAndroid Build Coastguard Worker return bpf_program_attach_fd(prog, ifindex, "netkit", &link_create_opts);
12617*f7c14bbaSAndroid Build Coastguard Worker }
12618*f7c14bbaSAndroid Build Coastguard Worker
bpf_program__attach_freplace(const struct bpf_program * prog,int target_fd,const char * attach_func_name)12619*f7c14bbaSAndroid Build Coastguard Worker struct bpf_link *bpf_program__attach_freplace(const struct bpf_program *prog,
12620*f7c14bbaSAndroid Build Coastguard Worker int target_fd,
12621*f7c14bbaSAndroid Build Coastguard Worker const char *attach_func_name)
12622*f7c14bbaSAndroid Build Coastguard Worker {
12623*f7c14bbaSAndroid Build Coastguard Worker int btf_id;
12624*f7c14bbaSAndroid Build Coastguard Worker
12625*f7c14bbaSAndroid Build Coastguard Worker if (!!target_fd != !!attach_func_name) {
12626*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': supply none or both of target_fd and attach_func_name\n",
12627*f7c14bbaSAndroid Build Coastguard Worker prog->name);
12628*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(-EINVAL);
12629*f7c14bbaSAndroid Build Coastguard Worker }
12630*f7c14bbaSAndroid Build Coastguard Worker
12631*f7c14bbaSAndroid Build Coastguard Worker if (prog->type != BPF_PROG_TYPE_EXT) {
12632*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': only BPF_PROG_TYPE_EXT can attach as freplace",
12633*f7c14bbaSAndroid Build Coastguard Worker prog->name);
12634*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(-EINVAL);
12635*f7c14bbaSAndroid Build Coastguard Worker }
12636*f7c14bbaSAndroid Build Coastguard Worker
12637*f7c14bbaSAndroid Build Coastguard Worker if (target_fd) {
12638*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_OPTS(bpf_link_create_opts, target_opts);
12639*f7c14bbaSAndroid Build Coastguard Worker
12640*f7c14bbaSAndroid Build Coastguard Worker btf_id = libbpf_find_prog_btf_id(attach_func_name, target_fd);
12641*f7c14bbaSAndroid Build Coastguard Worker if (btf_id < 0)
12642*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(btf_id);
12643*f7c14bbaSAndroid Build Coastguard Worker
12644*f7c14bbaSAndroid Build Coastguard Worker target_opts.target_btf_id = btf_id;
12645*f7c14bbaSAndroid Build Coastguard Worker
12646*f7c14bbaSAndroid Build Coastguard Worker return bpf_program_attach_fd(prog, target_fd, "freplace",
12647*f7c14bbaSAndroid Build Coastguard Worker &target_opts);
12648*f7c14bbaSAndroid Build Coastguard Worker } else {
12649*f7c14bbaSAndroid Build Coastguard Worker /* no target, so use raw_tracepoint_open for compatibility
12650*f7c14bbaSAndroid Build Coastguard Worker * with old kernels
12651*f7c14bbaSAndroid Build Coastguard Worker */
12652*f7c14bbaSAndroid Build Coastguard Worker return bpf_program__attach_trace(prog);
12653*f7c14bbaSAndroid Build Coastguard Worker }
12654*f7c14bbaSAndroid Build Coastguard Worker }
12655*f7c14bbaSAndroid Build Coastguard Worker
12656*f7c14bbaSAndroid Build Coastguard Worker struct bpf_link *
bpf_program__attach_iter(const struct bpf_program * prog,const struct bpf_iter_attach_opts * opts)12657*f7c14bbaSAndroid Build Coastguard Worker bpf_program__attach_iter(const struct bpf_program *prog,
12658*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_iter_attach_opts *opts)
12659*f7c14bbaSAndroid Build Coastguard Worker {
12660*f7c14bbaSAndroid Build Coastguard Worker DECLARE_LIBBPF_OPTS(bpf_link_create_opts, link_create_opts);
12661*f7c14bbaSAndroid Build Coastguard Worker char errmsg[STRERR_BUFSIZE];
12662*f7c14bbaSAndroid Build Coastguard Worker struct bpf_link *link;
12663*f7c14bbaSAndroid Build Coastguard Worker int prog_fd, link_fd;
12664*f7c14bbaSAndroid Build Coastguard Worker __u32 target_fd = 0;
12665*f7c14bbaSAndroid Build Coastguard Worker
12666*f7c14bbaSAndroid Build Coastguard Worker if (!OPTS_VALID(opts, bpf_iter_attach_opts))
12667*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(-EINVAL);
12668*f7c14bbaSAndroid Build Coastguard Worker
12669*f7c14bbaSAndroid Build Coastguard Worker link_create_opts.iter_info = OPTS_GET(opts, link_info, (void *)0);
12670*f7c14bbaSAndroid Build Coastguard Worker link_create_opts.iter_info_len = OPTS_GET(opts, link_info_len, 0);
12671*f7c14bbaSAndroid Build Coastguard Worker
12672*f7c14bbaSAndroid Build Coastguard Worker prog_fd = bpf_program__fd(prog);
12673*f7c14bbaSAndroid Build Coastguard Worker if (prog_fd < 0) {
12674*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': can't attach before loaded\n", prog->name);
12675*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(-EINVAL);
12676*f7c14bbaSAndroid Build Coastguard Worker }
12677*f7c14bbaSAndroid Build Coastguard Worker
12678*f7c14bbaSAndroid Build Coastguard Worker link = calloc(1, sizeof(*link));
12679*f7c14bbaSAndroid Build Coastguard Worker if (!link)
12680*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(-ENOMEM);
12681*f7c14bbaSAndroid Build Coastguard Worker link->detach = &bpf_link__detach_fd;
12682*f7c14bbaSAndroid Build Coastguard Worker
12683*f7c14bbaSAndroid Build Coastguard Worker link_fd = bpf_link_create(prog_fd, target_fd, BPF_TRACE_ITER,
12684*f7c14bbaSAndroid Build Coastguard Worker &link_create_opts);
12685*f7c14bbaSAndroid Build Coastguard Worker if (link_fd < 0) {
12686*f7c14bbaSAndroid Build Coastguard Worker link_fd = -errno;
12687*f7c14bbaSAndroid Build Coastguard Worker free(link);
12688*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': failed to attach to iterator: %s\n",
12689*f7c14bbaSAndroid Build Coastguard Worker prog->name, libbpf_strerror_r(link_fd, errmsg, sizeof(errmsg)));
12690*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(link_fd);
12691*f7c14bbaSAndroid Build Coastguard Worker }
12692*f7c14bbaSAndroid Build Coastguard Worker link->fd = link_fd;
12693*f7c14bbaSAndroid Build Coastguard Worker return link;
12694*f7c14bbaSAndroid Build Coastguard Worker }
12695*f7c14bbaSAndroid Build Coastguard Worker
attach_iter(const struct bpf_program * prog,long cookie,struct bpf_link ** link)12696*f7c14bbaSAndroid Build Coastguard Worker static int attach_iter(const struct bpf_program *prog, long cookie, struct bpf_link **link)
12697*f7c14bbaSAndroid Build Coastguard Worker {
12698*f7c14bbaSAndroid Build Coastguard Worker *link = bpf_program__attach_iter(prog, NULL);
12699*f7c14bbaSAndroid Build Coastguard Worker return libbpf_get_error(*link);
12700*f7c14bbaSAndroid Build Coastguard Worker }
12701*f7c14bbaSAndroid Build Coastguard Worker
bpf_program__attach_netfilter(const struct bpf_program * prog,const struct bpf_netfilter_opts * opts)12702*f7c14bbaSAndroid Build Coastguard Worker struct bpf_link *bpf_program__attach_netfilter(const struct bpf_program *prog,
12703*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_netfilter_opts *opts)
12704*f7c14bbaSAndroid Build Coastguard Worker {
12705*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_OPTS(bpf_link_create_opts, lopts);
12706*f7c14bbaSAndroid Build Coastguard Worker struct bpf_link *link;
12707*f7c14bbaSAndroid Build Coastguard Worker int prog_fd, link_fd;
12708*f7c14bbaSAndroid Build Coastguard Worker
12709*f7c14bbaSAndroid Build Coastguard Worker if (!OPTS_VALID(opts, bpf_netfilter_opts))
12710*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(-EINVAL);
12711*f7c14bbaSAndroid Build Coastguard Worker
12712*f7c14bbaSAndroid Build Coastguard Worker prog_fd = bpf_program__fd(prog);
12713*f7c14bbaSAndroid Build Coastguard Worker if (prog_fd < 0) {
12714*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': can't attach before loaded\n", prog->name);
12715*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(-EINVAL);
12716*f7c14bbaSAndroid Build Coastguard Worker }
12717*f7c14bbaSAndroid Build Coastguard Worker
12718*f7c14bbaSAndroid Build Coastguard Worker link = calloc(1, sizeof(*link));
12719*f7c14bbaSAndroid Build Coastguard Worker if (!link)
12720*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(-ENOMEM);
12721*f7c14bbaSAndroid Build Coastguard Worker
12722*f7c14bbaSAndroid Build Coastguard Worker link->detach = &bpf_link__detach_fd;
12723*f7c14bbaSAndroid Build Coastguard Worker
12724*f7c14bbaSAndroid Build Coastguard Worker lopts.netfilter.pf = OPTS_GET(opts, pf, 0);
12725*f7c14bbaSAndroid Build Coastguard Worker lopts.netfilter.hooknum = OPTS_GET(opts, hooknum, 0);
12726*f7c14bbaSAndroid Build Coastguard Worker lopts.netfilter.priority = OPTS_GET(opts, priority, 0);
12727*f7c14bbaSAndroid Build Coastguard Worker lopts.netfilter.flags = OPTS_GET(opts, flags, 0);
12728*f7c14bbaSAndroid Build Coastguard Worker
12729*f7c14bbaSAndroid Build Coastguard Worker link_fd = bpf_link_create(prog_fd, 0, BPF_NETFILTER, &lopts);
12730*f7c14bbaSAndroid Build Coastguard Worker if (link_fd < 0) {
12731*f7c14bbaSAndroid Build Coastguard Worker char errmsg[STRERR_BUFSIZE];
12732*f7c14bbaSAndroid Build Coastguard Worker
12733*f7c14bbaSAndroid Build Coastguard Worker link_fd = -errno;
12734*f7c14bbaSAndroid Build Coastguard Worker free(link);
12735*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': failed to attach to netfilter: %s\n",
12736*f7c14bbaSAndroid Build Coastguard Worker prog->name, libbpf_strerror_r(link_fd, errmsg, sizeof(errmsg)));
12737*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(link_fd);
12738*f7c14bbaSAndroid Build Coastguard Worker }
12739*f7c14bbaSAndroid Build Coastguard Worker link->fd = link_fd;
12740*f7c14bbaSAndroid Build Coastguard Worker
12741*f7c14bbaSAndroid Build Coastguard Worker return link;
12742*f7c14bbaSAndroid Build Coastguard Worker }
12743*f7c14bbaSAndroid Build Coastguard Worker
bpf_program__attach(const struct bpf_program * prog)12744*f7c14bbaSAndroid Build Coastguard Worker struct bpf_link *bpf_program__attach(const struct bpf_program *prog)
12745*f7c14bbaSAndroid Build Coastguard Worker {
12746*f7c14bbaSAndroid Build Coastguard Worker struct bpf_link *link = NULL;
12747*f7c14bbaSAndroid Build Coastguard Worker int err;
12748*f7c14bbaSAndroid Build Coastguard Worker
12749*f7c14bbaSAndroid Build Coastguard Worker if (!prog->sec_def || !prog->sec_def->prog_attach_fn)
12750*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(-EOPNOTSUPP);
12751*f7c14bbaSAndroid Build Coastguard Worker
12752*f7c14bbaSAndroid Build Coastguard Worker if (bpf_program__fd(prog) < 0) {
12753*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': can't attach BPF program without FD (was it loaded?)\n",
12754*f7c14bbaSAndroid Build Coastguard Worker prog->name);
12755*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(-EINVAL);
12756*f7c14bbaSAndroid Build Coastguard Worker }
12757*f7c14bbaSAndroid Build Coastguard Worker
12758*f7c14bbaSAndroid Build Coastguard Worker err = prog->sec_def->prog_attach_fn(prog, prog->sec_def->cookie, &link);
12759*f7c14bbaSAndroid Build Coastguard Worker if (err)
12760*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(err);
12761*f7c14bbaSAndroid Build Coastguard Worker
12762*f7c14bbaSAndroid Build Coastguard Worker /* When calling bpf_program__attach() explicitly, auto-attach support
12763*f7c14bbaSAndroid Build Coastguard Worker * is expected to work, so NULL returned link is considered an error.
12764*f7c14bbaSAndroid Build Coastguard Worker * This is different for skeleton's attach, see comment in
12765*f7c14bbaSAndroid Build Coastguard Worker * bpf_object__attach_skeleton().
12766*f7c14bbaSAndroid Build Coastguard Worker */
12767*f7c14bbaSAndroid Build Coastguard Worker if (!link)
12768*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(-EOPNOTSUPP);
12769*f7c14bbaSAndroid Build Coastguard Worker
12770*f7c14bbaSAndroid Build Coastguard Worker return link;
12771*f7c14bbaSAndroid Build Coastguard Worker }
12772*f7c14bbaSAndroid Build Coastguard Worker
12773*f7c14bbaSAndroid Build Coastguard Worker struct bpf_link_struct_ops {
12774*f7c14bbaSAndroid Build Coastguard Worker struct bpf_link link;
12775*f7c14bbaSAndroid Build Coastguard Worker int map_fd;
12776*f7c14bbaSAndroid Build Coastguard Worker };
12777*f7c14bbaSAndroid Build Coastguard Worker
bpf_link__detach_struct_ops(struct bpf_link * link)12778*f7c14bbaSAndroid Build Coastguard Worker static int bpf_link__detach_struct_ops(struct bpf_link *link)
12779*f7c14bbaSAndroid Build Coastguard Worker {
12780*f7c14bbaSAndroid Build Coastguard Worker struct bpf_link_struct_ops *st_link;
12781*f7c14bbaSAndroid Build Coastguard Worker __u32 zero = 0;
12782*f7c14bbaSAndroid Build Coastguard Worker
12783*f7c14bbaSAndroid Build Coastguard Worker st_link = container_of(link, struct bpf_link_struct_ops, link);
12784*f7c14bbaSAndroid Build Coastguard Worker
12785*f7c14bbaSAndroid Build Coastguard Worker if (st_link->map_fd < 0)
12786*f7c14bbaSAndroid Build Coastguard Worker /* w/o a real link */
12787*f7c14bbaSAndroid Build Coastguard Worker return bpf_map_delete_elem(link->fd, &zero);
12788*f7c14bbaSAndroid Build Coastguard Worker
12789*f7c14bbaSAndroid Build Coastguard Worker return close(link->fd);
12790*f7c14bbaSAndroid Build Coastguard Worker }
12791*f7c14bbaSAndroid Build Coastguard Worker
bpf_map__attach_struct_ops(const struct bpf_map * map)12792*f7c14bbaSAndroid Build Coastguard Worker struct bpf_link *bpf_map__attach_struct_ops(const struct bpf_map *map)
12793*f7c14bbaSAndroid Build Coastguard Worker {
12794*f7c14bbaSAndroid Build Coastguard Worker struct bpf_link_struct_ops *link;
12795*f7c14bbaSAndroid Build Coastguard Worker __u32 zero = 0;
12796*f7c14bbaSAndroid Build Coastguard Worker int err, fd;
12797*f7c14bbaSAndroid Build Coastguard Worker
12798*f7c14bbaSAndroid Build Coastguard Worker if (!bpf_map__is_struct_ops(map))
12799*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(-EINVAL);
12800*f7c14bbaSAndroid Build Coastguard Worker
12801*f7c14bbaSAndroid Build Coastguard Worker if (map->fd < 0) {
12802*f7c14bbaSAndroid Build Coastguard Worker pr_warn("map '%s': can't attach BPF map without FD (was it created?)\n", map->name);
12803*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(-EINVAL);
12804*f7c14bbaSAndroid Build Coastguard Worker }
12805*f7c14bbaSAndroid Build Coastguard Worker
12806*f7c14bbaSAndroid Build Coastguard Worker link = calloc(1, sizeof(*link));
12807*f7c14bbaSAndroid Build Coastguard Worker if (!link)
12808*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(-EINVAL);
12809*f7c14bbaSAndroid Build Coastguard Worker
12810*f7c14bbaSAndroid Build Coastguard Worker /* kern_vdata should be prepared during the loading phase. */
12811*f7c14bbaSAndroid Build Coastguard Worker err = bpf_map_update_elem(map->fd, &zero, map->st_ops->kern_vdata, 0);
12812*f7c14bbaSAndroid Build Coastguard Worker /* It can be EBUSY if the map has been used to create or
12813*f7c14bbaSAndroid Build Coastguard Worker * update a link before. We don't allow updating the value of
12814*f7c14bbaSAndroid Build Coastguard Worker * a struct_ops once it is set. That ensures that the value
12815*f7c14bbaSAndroid Build Coastguard Worker * never changed. So, it is safe to skip EBUSY.
12816*f7c14bbaSAndroid Build Coastguard Worker */
12817*f7c14bbaSAndroid Build Coastguard Worker if (err && (!(map->def.map_flags & BPF_F_LINK) || err != -EBUSY)) {
12818*f7c14bbaSAndroid Build Coastguard Worker free(link);
12819*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(err);
12820*f7c14bbaSAndroid Build Coastguard Worker }
12821*f7c14bbaSAndroid Build Coastguard Worker
12822*f7c14bbaSAndroid Build Coastguard Worker link->link.detach = bpf_link__detach_struct_ops;
12823*f7c14bbaSAndroid Build Coastguard Worker
12824*f7c14bbaSAndroid Build Coastguard Worker if (!(map->def.map_flags & BPF_F_LINK)) {
12825*f7c14bbaSAndroid Build Coastguard Worker /* w/o a real link */
12826*f7c14bbaSAndroid Build Coastguard Worker link->link.fd = map->fd;
12827*f7c14bbaSAndroid Build Coastguard Worker link->map_fd = -1;
12828*f7c14bbaSAndroid Build Coastguard Worker return &link->link;
12829*f7c14bbaSAndroid Build Coastguard Worker }
12830*f7c14bbaSAndroid Build Coastguard Worker
12831*f7c14bbaSAndroid Build Coastguard Worker fd = bpf_link_create(map->fd, 0, BPF_STRUCT_OPS, NULL);
12832*f7c14bbaSAndroid Build Coastguard Worker if (fd < 0) {
12833*f7c14bbaSAndroid Build Coastguard Worker free(link);
12834*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(fd);
12835*f7c14bbaSAndroid Build Coastguard Worker }
12836*f7c14bbaSAndroid Build Coastguard Worker
12837*f7c14bbaSAndroid Build Coastguard Worker link->link.fd = fd;
12838*f7c14bbaSAndroid Build Coastguard Worker link->map_fd = map->fd;
12839*f7c14bbaSAndroid Build Coastguard Worker
12840*f7c14bbaSAndroid Build Coastguard Worker return &link->link;
12841*f7c14bbaSAndroid Build Coastguard Worker }
12842*f7c14bbaSAndroid Build Coastguard Worker
12843*f7c14bbaSAndroid Build Coastguard Worker /*
12844*f7c14bbaSAndroid Build Coastguard Worker * Swap the back struct_ops of a link with a new struct_ops map.
12845*f7c14bbaSAndroid Build Coastguard Worker */
bpf_link__update_map(struct bpf_link * link,const struct bpf_map * map)12846*f7c14bbaSAndroid Build Coastguard Worker int bpf_link__update_map(struct bpf_link *link, const struct bpf_map *map)
12847*f7c14bbaSAndroid Build Coastguard Worker {
12848*f7c14bbaSAndroid Build Coastguard Worker struct bpf_link_struct_ops *st_ops_link;
12849*f7c14bbaSAndroid Build Coastguard Worker __u32 zero = 0;
12850*f7c14bbaSAndroid Build Coastguard Worker int err;
12851*f7c14bbaSAndroid Build Coastguard Worker
12852*f7c14bbaSAndroid Build Coastguard Worker if (!bpf_map__is_struct_ops(map))
12853*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
12854*f7c14bbaSAndroid Build Coastguard Worker
12855*f7c14bbaSAndroid Build Coastguard Worker if (map->fd < 0) {
12856*f7c14bbaSAndroid Build Coastguard Worker pr_warn("map '%s': can't use BPF map without FD (was it created?)\n", map->name);
12857*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
12858*f7c14bbaSAndroid Build Coastguard Worker }
12859*f7c14bbaSAndroid Build Coastguard Worker
12860*f7c14bbaSAndroid Build Coastguard Worker st_ops_link = container_of(link, struct bpf_link_struct_ops, link);
12861*f7c14bbaSAndroid Build Coastguard Worker /* Ensure the type of a link is correct */
12862*f7c14bbaSAndroid Build Coastguard Worker if (st_ops_link->map_fd < 0)
12863*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
12864*f7c14bbaSAndroid Build Coastguard Worker
12865*f7c14bbaSAndroid Build Coastguard Worker err = bpf_map_update_elem(map->fd, &zero, map->st_ops->kern_vdata, 0);
12866*f7c14bbaSAndroid Build Coastguard Worker /* It can be EBUSY if the map has been used to create or
12867*f7c14bbaSAndroid Build Coastguard Worker * update a link before. We don't allow updating the value of
12868*f7c14bbaSAndroid Build Coastguard Worker * a struct_ops once it is set. That ensures that the value
12869*f7c14bbaSAndroid Build Coastguard Worker * never changed. So, it is safe to skip EBUSY.
12870*f7c14bbaSAndroid Build Coastguard Worker */
12871*f7c14bbaSAndroid Build Coastguard Worker if (err && err != -EBUSY)
12872*f7c14bbaSAndroid Build Coastguard Worker return err;
12873*f7c14bbaSAndroid Build Coastguard Worker
12874*f7c14bbaSAndroid Build Coastguard Worker err = bpf_link_update(link->fd, map->fd, NULL);
12875*f7c14bbaSAndroid Build Coastguard Worker if (err < 0)
12876*f7c14bbaSAndroid Build Coastguard Worker return err;
12877*f7c14bbaSAndroid Build Coastguard Worker
12878*f7c14bbaSAndroid Build Coastguard Worker st_ops_link->map_fd = map->fd;
12879*f7c14bbaSAndroid Build Coastguard Worker
12880*f7c14bbaSAndroid Build Coastguard Worker return 0;
12881*f7c14bbaSAndroid Build Coastguard Worker }
12882*f7c14bbaSAndroid Build Coastguard Worker
12883*f7c14bbaSAndroid Build Coastguard Worker typedef enum bpf_perf_event_ret (*bpf_perf_event_print_t)(struct perf_event_header *hdr,
12884*f7c14bbaSAndroid Build Coastguard Worker void *private_data);
12885*f7c14bbaSAndroid Build Coastguard Worker
12886*f7c14bbaSAndroid Build Coastguard Worker static enum bpf_perf_event_ret
perf_event_read_simple(void * mmap_mem,size_t mmap_size,size_t page_size,void ** copy_mem,size_t * copy_size,bpf_perf_event_print_t fn,void * private_data)12887*f7c14bbaSAndroid Build Coastguard Worker perf_event_read_simple(void *mmap_mem, size_t mmap_size, size_t page_size,
12888*f7c14bbaSAndroid Build Coastguard Worker void **copy_mem, size_t *copy_size,
12889*f7c14bbaSAndroid Build Coastguard Worker bpf_perf_event_print_t fn, void *private_data)
12890*f7c14bbaSAndroid Build Coastguard Worker {
12891*f7c14bbaSAndroid Build Coastguard Worker struct perf_event_mmap_page *header = mmap_mem;
12892*f7c14bbaSAndroid Build Coastguard Worker __u64 data_head = ring_buffer_read_head(header);
12893*f7c14bbaSAndroid Build Coastguard Worker __u64 data_tail = header->data_tail;
12894*f7c14bbaSAndroid Build Coastguard Worker void *base = ((__u8 *)header) + page_size;
12895*f7c14bbaSAndroid Build Coastguard Worker int ret = LIBBPF_PERF_EVENT_CONT;
12896*f7c14bbaSAndroid Build Coastguard Worker struct perf_event_header *ehdr;
12897*f7c14bbaSAndroid Build Coastguard Worker size_t ehdr_size;
12898*f7c14bbaSAndroid Build Coastguard Worker
12899*f7c14bbaSAndroid Build Coastguard Worker while (data_head != data_tail) {
12900*f7c14bbaSAndroid Build Coastguard Worker ehdr = base + (data_tail & (mmap_size - 1));
12901*f7c14bbaSAndroid Build Coastguard Worker ehdr_size = ehdr->size;
12902*f7c14bbaSAndroid Build Coastguard Worker
12903*f7c14bbaSAndroid Build Coastguard Worker if (((void *)ehdr) + ehdr_size > base + mmap_size) {
12904*f7c14bbaSAndroid Build Coastguard Worker void *copy_start = ehdr;
12905*f7c14bbaSAndroid Build Coastguard Worker size_t len_first = base + mmap_size - copy_start;
12906*f7c14bbaSAndroid Build Coastguard Worker size_t len_secnd = ehdr_size - len_first;
12907*f7c14bbaSAndroid Build Coastguard Worker
12908*f7c14bbaSAndroid Build Coastguard Worker if (*copy_size < ehdr_size) {
12909*f7c14bbaSAndroid Build Coastguard Worker free(*copy_mem);
12910*f7c14bbaSAndroid Build Coastguard Worker *copy_mem = malloc(ehdr_size);
12911*f7c14bbaSAndroid Build Coastguard Worker if (!*copy_mem) {
12912*f7c14bbaSAndroid Build Coastguard Worker *copy_size = 0;
12913*f7c14bbaSAndroid Build Coastguard Worker ret = LIBBPF_PERF_EVENT_ERROR;
12914*f7c14bbaSAndroid Build Coastguard Worker break;
12915*f7c14bbaSAndroid Build Coastguard Worker }
12916*f7c14bbaSAndroid Build Coastguard Worker *copy_size = ehdr_size;
12917*f7c14bbaSAndroid Build Coastguard Worker }
12918*f7c14bbaSAndroid Build Coastguard Worker
12919*f7c14bbaSAndroid Build Coastguard Worker memcpy(*copy_mem, copy_start, len_first);
12920*f7c14bbaSAndroid Build Coastguard Worker memcpy(*copy_mem + len_first, base, len_secnd);
12921*f7c14bbaSAndroid Build Coastguard Worker ehdr = *copy_mem;
12922*f7c14bbaSAndroid Build Coastguard Worker }
12923*f7c14bbaSAndroid Build Coastguard Worker
12924*f7c14bbaSAndroid Build Coastguard Worker ret = fn(ehdr, private_data);
12925*f7c14bbaSAndroid Build Coastguard Worker data_tail += ehdr_size;
12926*f7c14bbaSAndroid Build Coastguard Worker if (ret != LIBBPF_PERF_EVENT_CONT)
12927*f7c14bbaSAndroid Build Coastguard Worker break;
12928*f7c14bbaSAndroid Build Coastguard Worker }
12929*f7c14bbaSAndroid Build Coastguard Worker
12930*f7c14bbaSAndroid Build Coastguard Worker ring_buffer_write_tail(header, data_tail);
12931*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(ret);
12932*f7c14bbaSAndroid Build Coastguard Worker }
12933*f7c14bbaSAndroid Build Coastguard Worker
12934*f7c14bbaSAndroid Build Coastguard Worker struct perf_buffer;
12935*f7c14bbaSAndroid Build Coastguard Worker
12936*f7c14bbaSAndroid Build Coastguard Worker struct perf_buffer_params {
12937*f7c14bbaSAndroid Build Coastguard Worker struct perf_event_attr *attr;
12938*f7c14bbaSAndroid Build Coastguard Worker /* if event_cb is specified, it takes precendence */
12939*f7c14bbaSAndroid Build Coastguard Worker perf_buffer_event_fn event_cb;
12940*f7c14bbaSAndroid Build Coastguard Worker /* sample_cb and lost_cb are higher-level common-case callbacks */
12941*f7c14bbaSAndroid Build Coastguard Worker perf_buffer_sample_fn sample_cb;
12942*f7c14bbaSAndroid Build Coastguard Worker perf_buffer_lost_fn lost_cb;
12943*f7c14bbaSAndroid Build Coastguard Worker void *ctx;
12944*f7c14bbaSAndroid Build Coastguard Worker int cpu_cnt;
12945*f7c14bbaSAndroid Build Coastguard Worker int *cpus;
12946*f7c14bbaSAndroid Build Coastguard Worker int *map_keys;
12947*f7c14bbaSAndroid Build Coastguard Worker };
12948*f7c14bbaSAndroid Build Coastguard Worker
12949*f7c14bbaSAndroid Build Coastguard Worker struct perf_cpu_buf {
12950*f7c14bbaSAndroid Build Coastguard Worker struct perf_buffer *pb;
12951*f7c14bbaSAndroid Build Coastguard Worker void *base; /* mmap()'ed memory */
12952*f7c14bbaSAndroid Build Coastguard Worker void *buf; /* for reconstructing segmented data */
12953*f7c14bbaSAndroid Build Coastguard Worker size_t buf_size;
12954*f7c14bbaSAndroid Build Coastguard Worker int fd;
12955*f7c14bbaSAndroid Build Coastguard Worker int cpu;
12956*f7c14bbaSAndroid Build Coastguard Worker int map_key;
12957*f7c14bbaSAndroid Build Coastguard Worker };
12958*f7c14bbaSAndroid Build Coastguard Worker
12959*f7c14bbaSAndroid Build Coastguard Worker struct perf_buffer {
12960*f7c14bbaSAndroid Build Coastguard Worker perf_buffer_event_fn event_cb;
12961*f7c14bbaSAndroid Build Coastguard Worker perf_buffer_sample_fn sample_cb;
12962*f7c14bbaSAndroid Build Coastguard Worker perf_buffer_lost_fn lost_cb;
12963*f7c14bbaSAndroid Build Coastguard Worker void *ctx; /* passed into callbacks */
12964*f7c14bbaSAndroid Build Coastguard Worker
12965*f7c14bbaSAndroid Build Coastguard Worker size_t page_size;
12966*f7c14bbaSAndroid Build Coastguard Worker size_t mmap_size;
12967*f7c14bbaSAndroid Build Coastguard Worker struct perf_cpu_buf **cpu_bufs;
12968*f7c14bbaSAndroid Build Coastguard Worker struct epoll_event *events;
12969*f7c14bbaSAndroid Build Coastguard Worker int cpu_cnt; /* number of allocated CPU buffers */
12970*f7c14bbaSAndroid Build Coastguard Worker int epoll_fd; /* perf event FD */
12971*f7c14bbaSAndroid Build Coastguard Worker int map_fd; /* BPF_MAP_TYPE_PERF_EVENT_ARRAY BPF map FD */
12972*f7c14bbaSAndroid Build Coastguard Worker };
12973*f7c14bbaSAndroid Build Coastguard Worker
perf_buffer__free_cpu_buf(struct perf_buffer * pb,struct perf_cpu_buf * cpu_buf)12974*f7c14bbaSAndroid Build Coastguard Worker static void perf_buffer__free_cpu_buf(struct perf_buffer *pb,
12975*f7c14bbaSAndroid Build Coastguard Worker struct perf_cpu_buf *cpu_buf)
12976*f7c14bbaSAndroid Build Coastguard Worker {
12977*f7c14bbaSAndroid Build Coastguard Worker if (!cpu_buf)
12978*f7c14bbaSAndroid Build Coastguard Worker return;
12979*f7c14bbaSAndroid Build Coastguard Worker if (cpu_buf->base &&
12980*f7c14bbaSAndroid Build Coastguard Worker munmap(cpu_buf->base, pb->mmap_size + pb->page_size))
12981*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to munmap cpu_buf #%d\n", cpu_buf->cpu);
12982*f7c14bbaSAndroid Build Coastguard Worker if (cpu_buf->fd >= 0) {
12983*f7c14bbaSAndroid Build Coastguard Worker ioctl(cpu_buf->fd, PERF_EVENT_IOC_DISABLE, 0);
12984*f7c14bbaSAndroid Build Coastguard Worker close(cpu_buf->fd);
12985*f7c14bbaSAndroid Build Coastguard Worker }
12986*f7c14bbaSAndroid Build Coastguard Worker free(cpu_buf->buf);
12987*f7c14bbaSAndroid Build Coastguard Worker free(cpu_buf);
12988*f7c14bbaSAndroid Build Coastguard Worker }
12989*f7c14bbaSAndroid Build Coastguard Worker
perf_buffer__free(struct perf_buffer * pb)12990*f7c14bbaSAndroid Build Coastguard Worker void perf_buffer__free(struct perf_buffer *pb)
12991*f7c14bbaSAndroid Build Coastguard Worker {
12992*f7c14bbaSAndroid Build Coastguard Worker int i;
12993*f7c14bbaSAndroid Build Coastguard Worker
12994*f7c14bbaSAndroid Build Coastguard Worker if (IS_ERR_OR_NULL(pb))
12995*f7c14bbaSAndroid Build Coastguard Worker return;
12996*f7c14bbaSAndroid Build Coastguard Worker if (pb->cpu_bufs) {
12997*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < pb->cpu_cnt; i++) {
12998*f7c14bbaSAndroid Build Coastguard Worker struct perf_cpu_buf *cpu_buf = pb->cpu_bufs[i];
12999*f7c14bbaSAndroid Build Coastguard Worker
13000*f7c14bbaSAndroid Build Coastguard Worker if (!cpu_buf)
13001*f7c14bbaSAndroid Build Coastguard Worker continue;
13002*f7c14bbaSAndroid Build Coastguard Worker
13003*f7c14bbaSAndroid Build Coastguard Worker bpf_map_delete_elem(pb->map_fd, &cpu_buf->map_key);
13004*f7c14bbaSAndroid Build Coastguard Worker perf_buffer__free_cpu_buf(pb, cpu_buf);
13005*f7c14bbaSAndroid Build Coastguard Worker }
13006*f7c14bbaSAndroid Build Coastguard Worker free(pb->cpu_bufs);
13007*f7c14bbaSAndroid Build Coastguard Worker }
13008*f7c14bbaSAndroid Build Coastguard Worker if (pb->epoll_fd >= 0)
13009*f7c14bbaSAndroid Build Coastguard Worker close(pb->epoll_fd);
13010*f7c14bbaSAndroid Build Coastguard Worker free(pb->events);
13011*f7c14bbaSAndroid Build Coastguard Worker free(pb);
13012*f7c14bbaSAndroid Build Coastguard Worker }
13013*f7c14bbaSAndroid Build Coastguard Worker
13014*f7c14bbaSAndroid Build Coastguard Worker static struct perf_cpu_buf *
perf_buffer__open_cpu_buf(struct perf_buffer * pb,struct perf_event_attr * attr,int cpu,int map_key)13015*f7c14bbaSAndroid Build Coastguard Worker perf_buffer__open_cpu_buf(struct perf_buffer *pb, struct perf_event_attr *attr,
13016*f7c14bbaSAndroid Build Coastguard Worker int cpu, int map_key)
13017*f7c14bbaSAndroid Build Coastguard Worker {
13018*f7c14bbaSAndroid Build Coastguard Worker struct perf_cpu_buf *cpu_buf;
13019*f7c14bbaSAndroid Build Coastguard Worker char msg[STRERR_BUFSIZE];
13020*f7c14bbaSAndroid Build Coastguard Worker int err;
13021*f7c14bbaSAndroid Build Coastguard Worker
13022*f7c14bbaSAndroid Build Coastguard Worker cpu_buf = calloc(1, sizeof(*cpu_buf));
13023*f7c14bbaSAndroid Build Coastguard Worker if (!cpu_buf)
13024*f7c14bbaSAndroid Build Coastguard Worker return ERR_PTR(-ENOMEM);
13025*f7c14bbaSAndroid Build Coastguard Worker
13026*f7c14bbaSAndroid Build Coastguard Worker cpu_buf->pb = pb;
13027*f7c14bbaSAndroid Build Coastguard Worker cpu_buf->cpu = cpu;
13028*f7c14bbaSAndroid Build Coastguard Worker cpu_buf->map_key = map_key;
13029*f7c14bbaSAndroid Build Coastguard Worker
13030*f7c14bbaSAndroid Build Coastguard Worker cpu_buf->fd = syscall(__NR_perf_event_open, attr, -1 /* pid */, cpu,
13031*f7c14bbaSAndroid Build Coastguard Worker -1, PERF_FLAG_FD_CLOEXEC);
13032*f7c14bbaSAndroid Build Coastguard Worker if (cpu_buf->fd < 0) {
13033*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
13034*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to open perf buffer event on cpu #%d: %s\n",
13035*f7c14bbaSAndroid Build Coastguard Worker cpu, libbpf_strerror_r(err, msg, sizeof(msg)));
13036*f7c14bbaSAndroid Build Coastguard Worker goto error;
13037*f7c14bbaSAndroid Build Coastguard Worker }
13038*f7c14bbaSAndroid Build Coastguard Worker
13039*f7c14bbaSAndroid Build Coastguard Worker cpu_buf->base = mmap(NULL, pb->mmap_size + pb->page_size,
13040*f7c14bbaSAndroid Build Coastguard Worker PROT_READ | PROT_WRITE, MAP_SHARED,
13041*f7c14bbaSAndroid Build Coastguard Worker cpu_buf->fd, 0);
13042*f7c14bbaSAndroid Build Coastguard Worker if (cpu_buf->base == MAP_FAILED) {
13043*f7c14bbaSAndroid Build Coastguard Worker cpu_buf->base = NULL;
13044*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
13045*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to mmap perf buffer on cpu #%d: %s\n",
13046*f7c14bbaSAndroid Build Coastguard Worker cpu, libbpf_strerror_r(err, msg, sizeof(msg)));
13047*f7c14bbaSAndroid Build Coastguard Worker goto error;
13048*f7c14bbaSAndroid Build Coastguard Worker }
13049*f7c14bbaSAndroid Build Coastguard Worker
13050*f7c14bbaSAndroid Build Coastguard Worker if (ioctl(cpu_buf->fd, PERF_EVENT_IOC_ENABLE, 0) < 0) {
13051*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
13052*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to enable perf buffer event on cpu #%d: %s\n",
13053*f7c14bbaSAndroid Build Coastguard Worker cpu, libbpf_strerror_r(err, msg, sizeof(msg)));
13054*f7c14bbaSAndroid Build Coastguard Worker goto error;
13055*f7c14bbaSAndroid Build Coastguard Worker }
13056*f7c14bbaSAndroid Build Coastguard Worker
13057*f7c14bbaSAndroid Build Coastguard Worker return cpu_buf;
13058*f7c14bbaSAndroid Build Coastguard Worker
13059*f7c14bbaSAndroid Build Coastguard Worker error:
13060*f7c14bbaSAndroid Build Coastguard Worker perf_buffer__free_cpu_buf(pb, cpu_buf);
13061*f7c14bbaSAndroid Build Coastguard Worker return (struct perf_cpu_buf *)ERR_PTR(err);
13062*f7c14bbaSAndroid Build Coastguard Worker }
13063*f7c14bbaSAndroid Build Coastguard Worker
13064*f7c14bbaSAndroid Build Coastguard Worker static struct perf_buffer *__perf_buffer__new(int map_fd, size_t page_cnt,
13065*f7c14bbaSAndroid Build Coastguard Worker struct perf_buffer_params *p);
13066*f7c14bbaSAndroid Build Coastguard Worker
perf_buffer__new(int map_fd,size_t page_cnt,perf_buffer_sample_fn sample_cb,perf_buffer_lost_fn lost_cb,void * ctx,const struct perf_buffer_opts * opts)13067*f7c14bbaSAndroid Build Coastguard Worker struct perf_buffer *perf_buffer__new(int map_fd, size_t page_cnt,
13068*f7c14bbaSAndroid Build Coastguard Worker perf_buffer_sample_fn sample_cb,
13069*f7c14bbaSAndroid Build Coastguard Worker perf_buffer_lost_fn lost_cb,
13070*f7c14bbaSAndroid Build Coastguard Worker void *ctx,
13071*f7c14bbaSAndroid Build Coastguard Worker const struct perf_buffer_opts *opts)
13072*f7c14bbaSAndroid Build Coastguard Worker {
13073*f7c14bbaSAndroid Build Coastguard Worker const size_t attr_sz = sizeof(struct perf_event_attr);
13074*f7c14bbaSAndroid Build Coastguard Worker struct perf_buffer_params p = {};
13075*f7c14bbaSAndroid Build Coastguard Worker struct perf_event_attr attr;
13076*f7c14bbaSAndroid Build Coastguard Worker __u32 sample_period;
13077*f7c14bbaSAndroid Build Coastguard Worker
13078*f7c14bbaSAndroid Build Coastguard Worker if (!OPTS_VALID(opts, perf_buffer_opts))
13079*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(-EINVAL);
13080*f7c14bbaSAndroid Build Coastguard Worker
13081*f7c14bbaSAndroid Build Coastguard Worker sample_period = OPTS_GET(opts, sample_period, 1);
13082*f7c14bbaSAndroid Build Coastguard Worker if (!sample_period)
13083*f7c14bbaSAndroid Build Coastguard Worker sample_period = 1;
13084*f7c14bbaSAndroid Build Coastguard Worker
13085*f7c14bbaSAndroid Build Coastguard Worker memset(&attr, 0, attr_sz);
13086*f7c14bbaSAndroid Build Coastguard Worker attr.size = attr_sz;
13087*f7c14bbaSAndroid Build Coastguard Worker attr.config = PERF_COUNT_SW_BPF_OUTPUT;
13088*f7c14bbaSAndroid Build Coastguard Worker attr.type = PERF_TYPE_SOFTWARE;
13089*f7c14bbaSAndroid Build Coastguard Worker attr.sample_type = PERF_SAMPLE_RAW;
13090*f7c14bbaSAndroid Build Coastguard Worker attr.sample_period = sample_period;
13091*f7c14bbaSAndroid Build Coastguard Worker attr.wakeup_events = sample_period;
13092*f7c14bbaSAndroid Build Coastguard Worker
13093*f7c14bbaSAndroid Build Coastguard Worker p.attr = &attr;
13094*f7c14bbaSAndroid Build Coastguard Worker p.sample_cb = sample_cb;
13095*f7c14bbaSAndroid Build Coastguard Worker p.lost_cb = lost_cb;
13096*f7c14bbaSAndroid Build Coastguard Worker p.ctx = ctx;
13097*f7c14bbaSAndroid Build Coastguard Worker
13098*f7c14bbaSAndroid Build Coastguard Worker return libbpf_ptr(__perf_buffer__new(map_fd, page_cnt, &p));
13099*f7c14bbaSAndroid Build Coastguard Worker }
13100*f7c14bbaSAndroid Build Coastguard Worker
perf_buffer__new_raw(int map_fd,size_t page_cnt,struct perf_event_attr * attr,perf_buffer_event_fn event_cb,void * ctx,const struct perf_buffer_raw_opts * opts)13101*f7c14bbaSAndroid Build Coastguard Worker struct perf_buffer *perf_buffer__new_raw(int map_fd, size_t page_cnt,
13102*f7c14bbaSAndroid Build Coastguard Worker struct perf_event_attr *attr,
13103*f7c14bbaSAndroid Build Coastguard Worker perf_buffer_event_fn event_cb, void *ctx,
13104*f7c14bbaSAndroid Build Coastguard Worker const struct perf_buffer_raw_opts *opts)
13105*f7c14bbaSAndroid Build Coastguard Worker {
13106*f7c14bbaSAndroid Build Coastguard Worker struct perf_buffer_params p = {};
13107*f7c14bbaSAndroid Build Coastguard Worker
13108*f7c14bbaSAndroid Build Coastguard Worker if (!attr)
13109*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(-EINVAL);
13110*f7c14bbaSAndroid Build Coastguard Worker
13111*f7c14bbaSAndroid Build Coastguard Worker if (!OPTS_VALID(opts, perf_buffer_raw_opts))
13112*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(-EINVAL);
13113*f7c14bbaSAndroid Build Coastguard Worker
13114*f7c14bbaSAndroid Build Coastguard Worker p.attr = attr;
13115*f7c14bbaSAndroid Build Coastguard Worker p.event_cb = event_cb;
13116*f7c14bbaSAndroid Build Coastguard Worker p.ctx = ctx;
13117*f7c14bbaSAndroid Build Coastguard Worker p.cpu_cnt = OPTS_GET(opts, cpu_cnt, 0);
13118*f7c14bbaSAndroid Build Coastguard Worker p.cpus = OPTS_GET(opts, cpus, NULL);
13119*f7c14bbaSAndroid Build Coastguard Worker p.map_keys = OPTS_GET(opts, map_keys, NULL);
13120*f7c14bbaSAndroid Build Coastguard Worker
13121*f7c14bbaSAndroid Build Coastguard Worker return libbpf_ptr(__perf_buffer__new(map_fd, page_cnt, &p));
13122*f7c14bbaSAndroid Build Coastguard Worker }
13123*f7c14bbaSAndroid Build Coastguard Worker
__perf_buffer__new(int map_fd,size_t page_cnt,struct perf_buffer_params * p)13124*f7c14bbaSAndroid Build Coastguard Worker static struct perf_buffer *__perf_buffer__new(int map_fd, size_t page_cnt,
13125*f7c14bbaSAndroid Build Coastguard Worker struct perf_buffer_params *p)
13126*f7c14bbaSAndroid Build Coastguard Worker {
13127*f7c14bbaSAndroid Build Coastguard Worker const char *online_cpus_file = "/sys/devices/system/cpu/online";
13128*f7c14bbaSAndroid Build Coastguard Worker struct bpf_map_info map;
13129*f7c14bbaSAndroid Build Coastguard Worker char msg[STRERR_BUFSIZE];
13130*f7c14bbaSAndroid Build Coastguard Worker struct perf_buffer *pb;
13131*f7c14bbaSAndroid Build Coastguard Worker bool *online = NULL;
13132*f7c14bbaSAndroid Build Coastguard Worker __u32 map_info_len;
13133*f7c14bbaSAndroid Build Coastguard Worker int err, i, j, n;
13134*f7c14bbaSAndroid Build Coastguard Worker
13135*f7c14bbaSAndroid Build Coastguard Worker if (page_cnt == 0 || (page_cnt & (page_cnt - 1))) {
13136*f7c14bbaSAndroid Build Coastguard Worker pr_warn("page count should be power of two, but is %zu\n",
13137*f7c14bbaSAndroid Build Coastguard Worker page_cnt);
13138*f7c14bbaSAndroid Build Coastguard Worker return ERR_PTR(-EINVAL);
13139*f7c14bbaSAndroid Build Coastguard Worker }
13140*f7c14bbaSAndroid Build Coastguard Worker
13141*f7c14bbaSAndroid Build Coastguard Worker /* best-effort sanity checks */
13142*f7c14bbaSAndroid Build Coastguard Worker memset(&map, 0, sizeof(map));
13143*f7c14bbaSAndroid Build Coastguard Worker map_info_len = sizeof(map);
13144*f7c14bbaSAndroid Build Coastguard Worker err = bpf_map_get_info_by_fd(map_fd, &map, &map_info_len);
13145*f7c14bbaSAndroid Build Coastguard Worker if (err) {
13146*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
13147*f7c14bbaSAndroid Build Coastguard Worker /* if BPF_OBJ_GET_INFO_BY_FD is supported, will return
13148*f7c14bbaSAndroid Build Coastguard Worker * -EBADFD, -EFAULT, or -E2BIG on real error
13149*f7c14bbaSAndroid Build Coastguard Worker */
13150*f7c14bbaSAndroid Build Coastguard Worker if (err != -EINVAL) {
13151*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to get map info for map FD %d: %s\n",
13152*f7c14bbaSAndroid Build Coastguard Worker map_fd, libbpf_strerror_r(err, msg, sizeof(msg)));
13153*f7c14bbaSAndroid Build Coastguard Worker return ERR_PTR(err);
13154*f7c14bbaSAndroid Build Coastguard Worker }
13155*f7c14bbaSAndroid Build Coastguard Worker pr_debug("failed to get map info for FD %d; API not supported? Ignoring...\n",
13156*f7c14bbaSAndroid Build Coastguard Worker map_fd);
13157*f7c14bbaSAndroid Build Coastguard Worker } else {
13158*f7c14bbaSAndroid Build Coastguard Worker if (map.type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) {
13159*f7c14bbaSAndroid Build Coastguard Worker pr_warn("map '%s' should be BPF_MAP_TYPE_PERF_EVENT_ARRAY\n",
13160*f7c14bbaSAndroid Build Coastguard Worker map.name);
13161*f7c14bbaSAndroid Build Coastguard Worker return ERR_PTR(-EINVAL);
13162*f7c14bbaSAndroid Build Coastguard Worker }
13163*f7c14bbaSAndroid Build Coastguard Worker }
13164*f7c14bbaSAndroid Build Coastguard Worker
13165*f7c14bbaSAndroid Build Coastguard Worker pb = calloc(1, sizeof(*pb));
13166*f7c14bbaSAndroid Build Coastguard Worker if (!pb)
13167*f7c14bbaSAndroid Build Coastguard Worker return ERR_PTR(-ENOMEM);
13168*f7c14bbaSAndroid Build Coastguard Worker
13169*f7c14bbaSAndroid Build Coastguard Worker pb->event_cb = p->event_cb;
13170*f7c14bbaSAndroid Build Coastguard Worker pb->sample_cb = p->sample_cb;
13171*f7c14bbaSAndroid Build Coastguard Worker pb->lost_cb = p->lost_cb;
13172*f7c14bbaSAndroid Build Coastguard Worker pb->ctx = p->ctx;
13173*f7c14bbaSAndroid Build Coastguard Worker
13174*f7c14bbaSAndroid Build Coastguard Worker pb->page_size = getpagesize();
13175*f7c14bbaSAndroid Build Coastguard Worker pb->mmap_size = pb->page_size * page_cnt;
13176*f7c14bbaSAndroid Build Coastguard Worker pb->map_fd = map_fd;
13177*f7c14bbaSAndroid Build Coastguard Worker
13178*f7c14bbaSAndroid Build Coastguard Worker pb->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
13179*f7c14bbaSAndroid Build Coastguard Worker if (pb->epoll_fd < 0) {
13180*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
13181*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to create epoll instance: %s\n",
13182*f7c14bbaSAndroid Build Coastguard Worker libbpf_strerror_r(err, msg, sizeof(msg)));
13183*f7c14bbaSAndroid Build Coastguard Worker goto error;
13184*f7c14bbaSAndroid Build Coastguard Worker }
13185*f7c14bbaSAndroid Build Coastguard Worker
13186*f7c14bbaSAndroid Build Coastguard Worker if (p->cpu_cnt > 0) {
13187*f7c14bbaSAndroid Build Coastguard Worker pb->cpu_cnt = p->cpu_cnt;
13188*f7c14bbaSAndroid Build Coastguard Worker } else {
13189*f7c14bbaSAndroid Build Coastguard Worker pb->cpu_cnt = libbpf_num_possible_cpus();
13190*f7c14bbaSAndroid Build Coastguard Worker if (pb->cpu_cnt < 0) {
13191*f7c14bbaSAndroid Build Coastguard Worker err = pb->cpu_cnt;
13192*f7c14bbaSAndroid Build Coastguard Worker goto error;
13193*f7c14bbaSAndroid Build Coastguard Worker }
13194*f7c14bbaSAndroid Build Coastguard Worker if (map.max_entries && map.max_entries < pb->cpu_cnt)
13195*f7c14bbaSAndroid Build Coastguard Worker pb->cpu_cnt = map.max_entries;
13196*f7c14bbaSAndroid Build Coastguard Worker }
13197*f7c14bbaSAndroid Build Coastguard Worker
13198*f7c14bbaSAndroid Build Coastguard Worker pb->events = calloc(pb->cpu_cnt, sizeof(*pb->events));
13199*f7c14bbaSAndroid Build Coastguard Worker if (!pb->events) {
13200*f7c14bbaSAndroid Build Coastguard Worker err = -ENOMEM;
13201*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to allocate events: out of memory\n");
13202*f7c14bbaSAndroid Build Coastguard Worker goto error;
13203*f7c14bbaSAndroid Build Coastguard Worker }
13204*f7c14bbaSAndroid Build Coastguard Worker pb->cpu_bufs = calloc(pb->cpu_cnt, sizeof(*pb->cpu_bufs));
13205*f7c14bbaSAndroid Build Coastguard Worker if (!pb->cpu_bufs) {
13206*f7c14bbaSAndroid Build Coastguard Worker err = -ENOMEM;
13207*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to allocate buffers: out of memory\n");
13208*f7c14bbaSAndroid Build Coastguard Worker goto error;
13209*f7c14bbaSAndroid Build Coastguard Worker }
13210*f7c14bbaSAndroid Build Coastguard Worker
13211*f7c14bbaSAndroid Build Coastguard Worker err = parse_cpu_mask_file(online_cpus_file, &online, &n);
13212*f7c14bbaSAndroid Build Coastguard Worker if (err) {
13213*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to get online CPU mask: %d\n", err);
13214*f7c14bbaSAndroid Build Coastguard Worker goto error;
13215*f7c14bbaSAndroid Build Coastguard Worker }
13216*f7c14bbaSAndroid Build Coastguard Worker
13217*f7c14bbaSAndroid Build Coastguard Worker for (i = 0, j = 0; i < pb->cpu_cnt; i++) {
13218*f7c14bbaSAndroid Build Coastguard Worker struct perf_cpu_buf *cpu_buf;
13219*f7c14bbaSAndroid Build Coastguard Worker int cpu, map_key;
13220*f7c14bbaSAndroid Build Coastguard Worker
13221*f7c14bbaSAndroid Build Coastguard Worker cpu = p->cpu_cnt > 0 ? p->cpus[i] : i;
13222*f7c14bbaSAndroid Build Coastguard Worker map_key = p->cpu_cnt > 0 ? p->map_keys[i] : i;
13223*f7c14bbaSAndroid Build Coastguard Worker
13224*f7c14bbaSAndroid Build Coastguard Worker /* in case user didn't explicitly requested particular CPUs to
13225*f7c14bbaSAndroid Build Coastguard Worker * be attached to, skip offline/not present CPUs
13226*f7c14bbaSAndroid Build Coastguard Worker */
13227*f7c14bbaSAndroid Build Coastguard Worker if (p->cpu_cnt <= 0 && (cpu >= n || !online[cpu]))
13228*f7c14bbaSAndroid Build Coastguard Worker continue;
13229*f7c14bbaSAndroid Build Coastguard Worker
13230*f7c14bbaSAndroid Build Coastguard Worker cpu_buf = perf_buffer__open_cpu_buf(pb, p->attr, cpu, map_key);
13231*f7c14bbaSAndroid Build Coastguard Worker if (IS_ERR(cpu_buf)) {
13232*f7c14bbaSAndroid Build Coastguard Worker err = PTR_ERR(cpu_buf);
13233*f7c14bbaSAndroid Build Coastguard Worker goto error;
13234*f7c14bbaSAndroid Build Coastguard Worker }
13235*f7c14bbaSAndroid Build Coastguard Worker
13236*f7c14bbaSAndroid Build Coastguard Worker pb->cpu_bufs[j] = cpu_buf;
13237*f7c14bbaSAndroid Build Coastguard Worker
13238*f7c14bbaSAndroid Build Coastguard Worker err = bpf_map_update_elem(pb->map_fd, &map_key,
13239*f7c14bbaSAndroid Build Coastguard Worker &cpu_buf->fd, 0);
13240*f7c14bbaSAndroid Build Coastguard Worker if (err) {
13241*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
13242*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to set cpu #%d, key %d -> perf FD %d: %s\n",
13243*f7c14bbaSAndroid Build Coastguard Worker cpu, map_key, cpu_buf->fd,
13244*f7c14bbaSAndroid Build Coastguard Worker libbpf_strerror_r(err, msg, sizeof(msg)));
13245*f7c14bbaSAndroid Build Coastguard Worker goto error;
13246*f7c14bbaSAndroid Build Coastguard Worker }
13247*f7c14bbaSAndroid Build Coastguard Worker
13248*f7c14bbaSAndroid Build Coastguard Worker pb->events[j].events = EPOLLIN;
13249*f7c14bbaSAndroid Build Coastguard Worker pb->events[j].data.ptr = cpu_buf;
13250*f7c14bbaSAndroid Build Coastguard Worker if (epoll_ctl(pb->epoll_fd, EPOLL_CTL_ADD, cpu_buf->fd,
13251*f7c14bbaSAndroid Build Coastguard Worker &pb->events[j]) < 0) {
13252*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
13253*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to epoll_ctl cpu #%d perf FD %d: %s\n",
13254*f7c14bbaSAndroid Build Coastguard Worker cpu, cpu_buf->fd,
13255*f7c14bbaSAndroid Build Coastguard Worker libbpf_strerror_r(err, msg, sizeof(msg)));
13256*f7c14bbaSAndroid Build Coastguard Worker goto error;
13257*f7c14bbaSAndroid Build Coastguard Worker }
13258*f7c14bbaSAndroid Build Coastguard Worker j++;
13259*f7c14bbaSAndroid Build Coastguard Worker }
13260*f7c14bbaSAndroid Build Coastguard Worker pb->cpu_cnt = j;
13261*f7c14bbaSAndroid Build Coastguard Worker free(online);
13262*f7c14bbaSAndroid Build Coastguard Worker
13263*f7c14bbaSAndroid Build Coastguard Worker return pb;
13264*f7c14bbaSAndroid Build Coastguard Worker
13265*f7c14bbaSAndroid Build Coastguard Worker error:
13266*f7c14bbaSAndroid Build Coastguard Worker free(online);
13267*f7c14bbaSAndroid Build Coastguard Worker if (pb)
13268*f7c14bbaSAndroid Build Coastguard Worker perf_buffer__free(pb);
13269*f7c14bbaSAndroid Build Coastguard Worker return ERR_PTR(err);
13270*f7c14bbaSAndroid Build Coastguard Worker }
13271*f7c14bbaSAndroid Build Coastguard Worker
13272*f7c14bbaSAndroid Build Coastguard Worker struct perf_sample_raw {
13273*f7c14bbaSAndroid Build Coastguard Worker struct perf_event_header header;
13274*f7c14bbaSAndroid Build Coastguard Worker uint32_t size;
13275*f7c14bbaSAndroid Build Coastguard Worker char data[];
13276*f7c14bbaSAndroid Build Coastguard Worker };
13277*f7c14bbaSAndroid Build Coastguard Worker
13278*f7c14bbaSAndroid Build Coastguard Worker struct perf_sample_lost {
13279*f7c14bbaSAndroid Build Coastguard Worker struct perf_event_header header;
13280*f7c14bbaSAndroid Build Coastguard Worker uint64_t id;
13281*f7c14bbaSAndroid Build Coastguard Worker uint64_t lost;
13282*f7c14bbaSAndroid Build Coastguard Worker uint64_t sample_id;
13283*f7c14bbaSAndroid Build Coastguard Worker };
13284*f7c14bbaSAndroid Build Coastguard Worker
13285*f7c14bbaSAndroid Build Coastguard Worker static enum bpf_perf_event_ret
perf_buffer__process_record(struct perf_event_header * e,void * ctx)13286*f7c14bbaSAndroid Build Coastguard Worker perf_buffer__process_record(struct perf_event_header *e, void *ctx)
13287*f7c14bbaSAndroid Build Coastguard Worker {
13288*f7c14bbaSAndroid Build Coastguard Worker struct perf_cpu_buf *cpu_buf = ctx;
13289*f7c14bbaSAndroid Build Coastguard Worker struct perf_buffer *pb = cpu_buf->pb;
13290*f7c14bbaSAndroid Build Coastguard Worker void *data = e;
13291*f7c14bbaSAndroid Build Coastguard Worker
13292*f7c14bbaSAndroid Build Coastguard Worker /* user wants full control over parsing perf event */
13293*f7c14bbaSAndroid Build Coastguard Worker if (pb->event_cb)
13294*f7c14bbaSAndroid Build Coastguard Worker return pb->event_cb(pb->ctx, cpu_buf->cpu, e);
13295*f7c14bbaSAndroid Build Coastguard Worker
13296*f7c14bbaSAndroid Build Coastguard Worker switch (e->type) {
13297*f7c14bbaSAndroid Build Coastguard Worker case PERF_RECORD_SAMPLE: {
13298*f7c14bbaSAndroid Build Coastguard Worker struct perf_sample_raw *s = data;
13299*f7c14bbaSAndroid Build Coastguard Worker
13300*f7c14bbaSAndroid Build Coastguard Worker if (pb->sample_cb)
13301*f7c14bbaSAndroid Build Coastguard Worker pb->sample_cb(pb->ctx, cpu_buf->cpu, s->data, s->size);
13302*f7c14bbaSAndroid Build Coastguard Worker break;
13303*f7c14bbaSAndroid Build Coastguard Worker }
13304*f7c14bbaSAndroid Build Coastguard Worker case PERF_RECORD_LOST: {
13305*f7c14bbaSAndroid Build Coastguard Worker struct perf_sample_lost *s = data;
13306*f7c14bbaSAndroid Build Coastguard Worker
13307*f7c14bbaSAndroid Build Coastguard Worker if (pb->lost_cb)
13308*f7c14bbaSAndroid Build Coastguard Worker pb->lost_cb(pb->ctx, cpu_buf->cpu, s->lost);
13309*f7c14bbaSAndroid Build Coastguard Worker break;
13310*f7c14bbaSAndroid Build Coastguard Worker }
13311*f7c14bbaSAndroid Build Coastguard Worker default:
13312*f7c14bbaSAndroid Build Coastguard Worker pr_warn("unknown perf sample type %d\n", e->type);
13313*f7c14bbaSAndroid Build Coastguard Worker return LIBBPF_PERF_EVENT_ERROR;
13314*f7c14bbaSAndroid Build Coastguard Worker }
13315*f7c14bbaSAndroid Build Coastguard Worker return LIBBPF_PERF_EVENT_CONT;
13316*f7c14bbaSAndroid Build Coastguard Worker }
13317*f7c14bbaSAndroid Build Coastguard Worker
perf_buffer__process_records(struct perf_buffer * pb,struct perf_cpu_buf * cpu_buf)13318*f7c14bbaSAndroid Build Coastguard Worker static int perf_buffer__process_records(struct perf_buffer *pb,
13319*f7c14bbaSAndroid Build Coastguard Worker struct perf_cpu_buf *cpu_buf)
13320*f7c14bbaSAndroid Build Coastguard Worker {
13321*f7c14bbaSAndroid Build Coastguard Worker enum bpf_perf_event_ret ret;
13322*f7c14bbaSAndroid Build Coastguard Worker
13323*f7c14bbaSAndroid Build Coastguard Worker ret = perf_event_read_simple(cpu_buf->base, pb->mmap_size,
13324*f7c14bbaSAndroid Build Coastguard Worker pb->page_size, &cpu_buf->buf,
13325*f7c14bbaSAndroid Build Coastguard Worker &cpu_buf->buf_size,
13326*f7c14bbaSAndroid Build Coastguard Worker perf_buffer__process_record, cpu_buf);
13327*f7c14bbaSAndroid Build Coastguard Worker if (ret != LIBBPF_PERF_EVENT_CONT)
13328*f7c14bbaSAndroid Build Coastguard Worker return ret;
13329*f7c14bbaSAndroid Build Coastguard Worker return 0;
13330*f7c14bbaSAndroid Build Coastguard Worker }
13331*f7c14bbaSAndroid Build Coastguard Worker
perf_buffer__epoll_fd(const struct perf_buffer * pb)13332*f7c14bbaSAndroid Build Coastguard Worker int perf_buffer__epoll_fd(const struct perf_buffer *pb)
13333*f7c14bbaSAndroid Build Coastguard Worker {
13334*f7c14bbaSAndroid Build Coastguard Worker return pb->epoll_fd;
13335*f7c14bbaSAndroid Build Coastguard Worker }
13336*f7c14bbaSAndroid Build Coastguard Worker
perf_buffer__poll(struct perf_buffer * pb,int timeout_ms)13337*f7c14bbaSAndroid Build Coastguard Worker int perf_buffer__poll(struct perf_buffer *pb, int timeout_ms)
13338*f7c14bbaSAndroid Build Coastguard Worker {
13339*f7c14bbaSAndroid Build Coastguard Worker int i, cnt, err;
13340*f7c14bbaSAndroid Build Coastguard Worker
13341*f7c14bbaSAndroid Build Coastguard Worker cnt = epoll_wait(pb->epoll_fd, pb->events, pb->cpu_cnt, timeout_ms);
13342*f7c14bbaSAndroid Build Coastguard Worker if (cnt < 0)
13343*f7c14bbaSAndroid Build Coastguard Worker return -errno;
13344*f7c14bbaSAndroid Build Coastguard Worker
13345*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < cnt; i++) {
13346*f7c14bbaSAndroid Build Coastguard Worker struct perf_cpu_buf *cpu_buf = pb->events[i].data.ptr;
13347*f7c14bbaSAndroid Build Coastguard Worker
13348*f7c14bbaSAndroid Build Coastguard Worker err = perf_buffer__process_records(pb, cpu_buf);
13349*f7c14bbaSAndroid Build Coastguard Worker if (err) {
13350*f7c14bbaSAndroid Build Coastguard Worker pr_warn("error while processing records: %d\n", err);
13351*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(err);
13352*f7c14bbaSAndroid Build Coastguard Worker }
13353*f7c14bbaSAndroid Build Coastguard Worker }
13354*f7c14bbaSAndroid Build Coastguard Worker return cnt;
13355*f7c14bbaSAndroid Build Coastguard Worker }
13356*f7c14bbaSAndroid Build Coastguard Worker
13357*f7c14bbaSAndroid Build Coastguard Worker /* Return number of PERF_EVENT_ARRAY map slots set up by this perf_buffer
13358*f7c14bbaSAndroid Build Coastguard Worker * manager.
13359*f7c14bbaSAndroid Build Coastguard Worker */
perf_buffer__buffer_cnt(const struct perf_buffer * pb)13360*f7c14bbaSAndroid Build Coastguard Worker size_t perf_buffer__buffer_cnt(const struct perf_buffer *pb)
13361*f7c14bbaSAndroid Build Coastguard Worker {
13362*f7c14bbaSAndroid Build Coastguard Worker return pb->cpu_cnt;
13363*f7c14bbaSAndroid Build Coastguard Worker }
13364*f7c14bbaSAndroid Build Coastguard Worker
13365*f7c14bbaSAndroid Build Coastguard Worker /*
13366*f7c14bbaSAndroid Build Coastguard Worker * Return perf_event FD of a ring buffer in *buf_idx* slot of
13367*f7c14bbaSAndroid Build Coastguard Worker * PERF_EVENT_ARRAY BPF map. This FD can be polled for new data using
13368*f7c14bbaSAndroid Build Coastguard Worker * select()/poll()/epoll() Linux syscalls.
13369*f7c14bbaSAndroid Build Coastguard Worker */
perf_buffer__buffer_fd(const struct perf_buffer * pb,size_t buf_idx)13370*f7c14bbaSAndroid Build Coastguard Worker int perf_buffer__buffer_fd(const struct perf_buffer *pb, size_t buf_idx)
13371*f7c14bbaSAndroid Build Coastguard Worker {
13372*f7c14bbaSAndroid Build Coastguard Worker struct perf_cpu_buf *cpu_buf;
13373*f7c14bbaSAndroid Build Coastguard Worker
13374*f7c14bbaSAndroid Build Coastguard Worker if (buf_idx >= pb->cpu_cnt)
13375*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-EINVAL);
13376*f7c14bbaSAndroid Build Coastguard Worker
13377*f7c14bbaSAndroid Build Coastguard Worker cpu_buf = pb->cpu_bufs[buf_idx];
13378*f7c14bbaSAndroid Build Coastguard Worker if (!cpu_buf)
13379*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-ENOENT);
13380*f7c14bbaSAndroid Build Coastguard Worker
13381*f7c14bbaSAndroid Build Coastguard Worker return cpu_buf->fd;
13382*f7c14bbaSAndroid Build Coastguard Worker }
13383*f7c14bbaSAndroid Build Coastguard Worker
perf_buffer__buffer(struct perf_buffer * pb,int buf_idx,void ** buf,size_t * buf_size)13384*f7c14bbaSAndroid Build Coastguard Worker int perf_buffer__buffer(struct perf_buffer *pb, int buf_idx, void **buf, size_t *buf_size)
13385*f7c14bbaSAndroid Build Coastguard Worker {
13386*f7c14bbaSAndroid Build Coastguard Worker struct perf_cpu_buf *cpu_buf;
13387*f7c14bbaSAndroid Build Coastguard Worker
13388*f7c14bbaSAndroid Build Coastguard Worker if (buf_idx >= pb->cpu_cnt)
13389*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-EINVAL);
13390*f7c14bbaSAndroid Build Coastguard Worker
13391*f7c14bbaSAndroid Build Coastguard Worker cpu_buf = pb->cpu_bufs[buf_idx];
13392*f7c14bbaSAndroid Build Coastguard Worker if (!cpu_buf)
13393*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-ENOENT);
13394*f7c14bbaSAndroid Build Coastguard Worker
13395*f7c14bbaSAndroid Build Coastguard Worker *buf = cpu_buf->base;
13396*f7c14bbaSAndroid Build Coastguard Worker *buf_size = pb->mmap_size;
13397*f7c14bbaSAndroid Build Coastguard Worker return 0;
13398*f7c14bbaSAndroid Build Coastguard Worker }
13399*f7c14bbaSAndroid Build Coastguard Worker
13400*f7c14bbaSAndroid Build Coastguard Worker /*
13401*f7c14bbaSAndroid Build Coastguard Worker * Consume data from perf ring buffer corresponding to slot *buf_idx* in
13402*f7c14bbaSAndroid Build Coastguard Worker * PERF_EVENT_ARRAY BPF map without waiting/polling. If there is no data to
13403*f7c14bbaSAndroid Build Coastguard Worker * consume, do nothing and return success.
13404*f7c14bbaSAndroid Build Coastguard Worker * Returns:
13405*f7c14bbaSAndroid Build Coastguard Worker * - 0 on success;
13406*f7c14bbaSAndroid Build Coastguard Worker * - <0 on failure.
13407*f7c14bbaSAndroid Build Coastguard Worker */
perf_buffer__consume_buffer(struct perf_buffer * pb,size_t buf_idx)13408*f7c14bbaSAndroid Build Coastguard Worker int perf_buffer__consume_buffer(struct perf_buffer *pb, size_t buf_idx)
13409*f7c14bbaSAndroid Build Coastguard Worker {
13410*f7c14bbaSAndroid Build Coastguard Worker struct perf_cpu_buf *cpu_buf;
13411*f7c14bbaSAndroid Build Coastguard Worker
13412*f7c14bbaSAndroid Build Coastguard Worker if (buf_idx >= pb->cpu_cnt)
13413*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-EINVAL);
13414*f7c14bbaSAndroid Build Coastguard Worker
13415*f7c14bbaSAndroid Build Coastguard Worker cpu_buf = pb->cpu_bufs[buf_idx];
13416*f7c14bbaSAndroid Build Coastguard Worker if (!cpu_buf)
13417*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-ENOENT);
13418*f7c14bbaSAndroid Build Coastguard Worker
13419*f7c14bbaSAndroid Build Coastguard Worker return perf_buffer__process_records(pb, cpu_buf);
13420*f7c14bbaSAndroid Build Coastguard Worker }
13421*f7c14bbaSAndroid Build Coastguard Worker
perf_buffer__consume(struct perf_buffer * pb)13422*f7c14bbaSAndroid Build Coastguard Worker int perf_buffer__consume(struct perf_buffer *pb)
13423*f7c14bbaSAndroid Build Coastguard Worker {
13424*f7c14bbaSAndroid Build Coastguard Worker int i, err;
13425*f7c14bbaSAndroid Build Coastguard Worker
13426*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < pb->cpu_cnt; i++) {
13427*f7c14bbaSAndroid Build Coastguard Worker struct perf_cpu_buf *cpu_buf = pb->cpu_bufs[i];
13428*f7c14bbaSAndroid Build Coastguard Worker
13429*f7c14bbaSAndroid Build Coastguard Worker if (!cpu_buf)
13430*f7c14bbaSAndroid Build Coastguard Worker continue;
13431*f7c14bbaSAndroid Build Coastguard Worker
13432*f7c14bbaSAndroid Build Coastguard Worker err = perf_buffer__process_records(pb, cpu_buf);
13433*f7c14bbaSAndroid Build Coastguard Worker if (err) {
13434*f7c14bbaSAndroid Build Coastguard Worker pr_warn("perf_buffer: failed to process records in buffer #%d: %d\n", i, err);
13435*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(err);
13436*f7c14bbaSAndroid Build Coastguard Worker }
13437*f7c14bbaSAndroid Build Coastguard Worker }
13438*f7c14bbaSAndroid Build Coastguard Worker return 0;
13439*f7c14bbaSAndroid Build Coastguard Worker }
13440*f7c14bbaSAndroid Build Coastguard Worker
bpf_program__set_attach_target(struct bpf_program * prog,int attach_prog_fd,const char * attach_func_name)13441*f7c14bbaSAndroid Build Coastguard Worker int bpf_program__set_attach_target(struct bpf_program *prog,
13442*f7c14bbaSAndroid Build Coastguard Worker int attach_prog_fd,
13443*f7c14bbaSAndroid Build Coastguard Worker const char *attach_func_name)
13444*f7c14bbaSAndroid Build Coastguard Worker {
13445*f7c14bbaSAndroid Build Coastguard Worker int btf_obj_fd = 0, btf_id = 0, err;
13446*f7c14bbaSAndroid Build Coastguard Worker
13447*f7c14bbaSAndroid Build Coastguard Worker if (!prog || attach_prog_fd < 0)
13448*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-EINVAL);
13449*f7c14bbaSAndroid Build Coastguard Worker
13450*f7c14bbaSAndroid Build Coastguard Worker if (prog->obj->loaded)
13451*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-EINVAL);
13452*f7c14bbaSAndroid Build Coastguard Worker
13453*f7c14bbaSAndroid Build Coastguard Worker if (attach_prog_fd && !attach_func_name) {
13454*f7c14bbaSAndroid Build Coastguard Worker /* remember attach_prog_fd and let bpf_program__load() find
13455*f7c14bbaSAndroid Build Coastguard Worker * BTF ID during the program load
13456*f7c14bbaSAndroid Build Coastguard Worker */
13457*f7c14bbaSAndroid Build Coastguard Worker prog->attach_prog_fd = attach_prog_fd;
13458*f7c14bbaSAndroid Build Coastguard Worker return 0;
13459*f7c14bbaSAndroid Build Coastguard Worker }
13460*f7c14bbaSAndroid Build Coastguard Worker
13461*f7c14bbaSAndroid Build Coastguard Worker if (attach_prog_fd) {
13462*f7c14bbaSAndroid Build Coastguard Worker btf_id = libbpf_find_prog_btf_id(attach_func_name,
13463*f7c14bbaSAndroid Build Coastguard Worker attach_prog_fd);
13464*f7c14bbaSAndroid Build Coastguard Worker if (btf_id < 0)
13465*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(btf_id);
13466*f7c14bbaSAndroid Build Coastguard Worker } else {
13467*f7c14bbaSAndroid Build Coastguard Worker if (!attach_func_name)
13468*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-EINVAL);
13469*f7c14bbaSAndroid Build Coastguard Worker
13470*f7c14bbaSAndroid Build Coastguard Worker /* load btf_vmlinux, if not yet */
13471*f7c14bbaSAndroid Build Coastguard Worker err = bpf_object__load_vmlinux_btf(prog->obj, true);
13472*f7c14bbaSAndroid Build Coastguard Worker if (err)
13473*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(err);
13474*f7c14bbaSAndroid Build Coastguard Worker err = find_kernel_btf_id(prog->obj, attach_func_name,
13475*f7c14bbaSAndroid Build Coastguard Worker prog->expected_attach_type,
13476*f7c14bbaSAndroid Build Coastguard Worker &btf_obj_fd, &btf_id);
13477*f7c14bbaSAndroid Build Coastguard Worker if (err)
13478*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(err);
13479*f7c14bbaSAndroid Build Coastguard Worker }
13480*f7c14bbaSAndroid Build Coastguard Worker
13481*f7c14bbaSAndroid Build Coastguard Worker prog->attach_btf_id = btf_id;
13482*f7c14bbaSAndroid Build Coastguard Worker prog->attach_btf_obj_fd = btf_obj_fd;
13483*f7c14bbaSAndroid Build Coastguard Worker prog->attach_prog_fd = attach_prog_fd;
13484*f7c14bbaSAndroid Build Coastguard Worker return 0;
13485*f7c14bbaSAndroid Build Coastguard Worker }
13486*f7c14bbaSAndroid Build Coastguard Worker
parse_cpu_mask_str(const char * s,bool ** mask,int * mask_sz)13487*f7c14bbaSAndroid Build Coastguard Worker int parse_cpu_mask_str(const char *s, bool **mask, int *mask_sz)
13488*f7c14bbaSAndroid Build Coastguard Worker {
13489*f7c14bbaSAndroid Build Coastguard Worker int err = 0, n, len, start, end = -1;
13490*f7c14bbaSAndroid Build Coastguard Worker bool *tmp;
13491*f7c14bbaSAndroid Build Coastguard Worker
13492*f7c14bbaSAndroid Build Coastguard Worker *mask = NULL;
13493*f7c14bbaSAndroid Build Coastguard Worker *mask_sz = 0;
13494*f7c14bbaSAndroid Build Coastguard Worker
13495*f7c14bbaSAndroid Build Coastguard Worker /* Each sub string separated by ',' has format \d+-\d+ or \d+ */
13496*f7c14bbaSAndroid Build Coastguard Worker while (*s) {
13497*f7c14bbaSAndroid Build Coastguard Worker if (*s == ',' || *s == '\n') {
13498*f7c14bbaSAndroid Build Coastguard Worker s++;
13499*f7c14bbaSAndroid Build Coastguard Worker continue;
13500*f7c14bbaSAndroid Build Coastguard Worker }
13501*f7c14bbaSAndroid Build Coastguard Worker n = sscanf(s, "%d%n-%d%n", &start, &len, &end, &len);
13502*f7c14bbaSAndroid Build Coastguard Worker if (n <= 0 || n > 2) {
13503*f7c14bbaSAndroid Build Coastguard Worker pr_warn("Failed to get CPU range %s: %d\n", s, n);
13504*f7c14bbaSAndroid Build Coastguard Worker err = -EINVAL;
13505*f7c14bbaSAndroid Build Coastguard Worker goto cleanup;
13506*f7c14bbaSAndroid Build Coastguard Worker } else if (n == 1) {
13507*f7c14bbaSAndroid Build Coastguard Worker end = start;
13508*f7c14bbaSAndroid Build Coastguard Worker }
13509*f7c14bbaSAndroid Build Coastguard Worker if (start < 0 || start > end) {
13510*f7c14bbaSAndroid Build Coastguard Worker pr_warn("Invalid CPU range [%d,%d] in %s\n",
13511*f7c14bbaSAndroid Build Coastguard Worker start, end, s);
13512*f7c14bbaSAndroid Build Coastguard Worker err = -EINVAL;
13513*f7c14bbaSAndroid Build Coastguard Worker goto cleanup;
13514*f7c14bbaSAndroid Build Coastguard Worker }
13515*f7c14bbaSAndroid Build Coastguard Worker tmp = realloc(*mask, end + 1);
13516*f7c14bbaSAndroid Build Coastguard Worker if (!tmp) {
13517*f7c14bbaSAndroid Build Coastguard Worker err = -ENOMEM;
13518*f7c14bbaSAndroid Build Coastguard Worker goto cleanup;
13519*f7c14bbaSAndroid Build Coastguard Worker }
13520*f7c14bbaSAndroid Build Coastguard Worker *mask = tmp;
13521*f7c14bbaSAndroid Build Coastguard Worker memset(tmp + *mask_sz, 0, start - *mask_sz);
13522*f7c14bbaSAndroid Build Coastguard Worker memset(tmp + start, 1, end - start + 1);
13523*f7c14bbaSAndroid Build Coastguard Worker *mask_sz = end + 1;
13524*f7c14bbaSAndroid Build Coastguard Worker s += len;
13525*f7c14bbaSAndroid Build Coastguard Worker }
13526*f7c14bbaSAndroid Build Coastguard Worker if (!*mask_sz) {
13527*f7c14bbaSAndroid Build Coastguard Worker pr_warn("Empty CPU range\n");
13528*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
13529*f7c14bbaSAndroid Build Coastguard Worker }
13530*f7c14bbaSAndroid Build Coastguard Worker return 0;
13531*f7c14bbaSAndroid Build Coastguard Worker cleanup:
13532*f7c14bbaSAndroid Build Coastguard Worker free(*mask);
13533*f7c14bbaSAndroid Build Coastguard Worker *mask = NULL;
13534*f7c14bbaSAndroid Build Coastguard Worker return err;
13535*f7c14bbaSAndroid Build Coastguard Worker }
13536*f7c14bbaSAndroid Build Coastguard Worker
parse_cpu_mask_file(const char * fcpu,bool ** mask,int * mask_sz)13537*f7c14bbaSAndroid Build Coastguard Worker int parse_cpu_mask_file(const char *fcpu, bool **mask, int *mask_sz)
13538*f7c14bbaSAndroid Build Coastguard Worker {
13539*f7c14bbaSAndroid Build Coastguard Worker int fd, err = 0, len;
13540*f7c14bbaSAndroid Build Coastguard Worker char buf[128];
13541*f7c14bbaSAndroid Build Coastguard Worker
13542*f7c14bbaSAndroid Build Coastguard Worker fd = open(fcpu, O_RDONLY | O_CLOEXEC);
13543*f7c14bbaSAndroid Build Coastguard Worker if (fd < 0) {
13544*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
13545*f7c14bbaSAndroid Build Coastguard Worker pr_warn("Failed to open cpu mask file %s: %d\n", fcpu, err);
13546*f7c14bbaSAndroid Build Coastguard Worker return err;
13547*f7c14bbaSAndroid Build Coastguard Worker }
13548*f7c14bbaSAndroid Build Coastguard Worker len = read(fd, buf, sizeof(buf));
13549*f7c14bbaSAndroid Build Coastguard Worker close(fd);
13550*f7c14bbaSAndroid Build Coastguard Worker if (len <= 0) {
13551*f7c14bbaSAndroid Build Coastguard Worker err = len ? -errno : -EINVAL;
13552*f7c14bbaSAndroid Build Coastguard Worker pr_warn("Failed to read cpu mask from %s: %d\n", fcpu, err);
13553*f7c14bbaSAndroid Build Coastguard Worker return err;
13554*f7c14bbaSAndroid Build Coastguard Worker }
13555*f7c14bbaSAndroid Build Coastguard Worker if (len >= sizeof(buf)) {
13556*f7c14bbaSAndroid Build Coastguard Worker pr_warn("CPU mask is too big in file %s\n", fcpu);
13557*f7c14bbaSAndroid Build Coastguard Worker return -E2BIG;
13558*f7c14bbaSAndroid Build Coastguard Worker }
13559*f7c14bbaSAndroid Build Coastguard Worker buf[len] = '\0';
13560*f7c14bbaSAndroid Build Coastguard Worker
13561*f7c14bbaSAndroid Build Coastguard Worker return parse_cpu_mask_str(buf, mask, mask_sz);
13562*f7c14bbaSAndroid Build Coastguard Worker }
13563*f7c14bbaSAndroid Build Coastguard Worker
libbpf_num_possible_cpus(void)13564*f7c14bbaSAndroid Build Coastguard Worker int libbpf_num_possible_cpus(void)
13565*f7c14bbaSAndroid Build Coastguard Worker {
13566*f7c14bbaSAndroid Build Coastguard Worker static const char *fcpu = "/sys/devices/system/cpu/possible";
13567*f7c14bbaSAndroid Build Coastguard Worker static int cpus;
13568*f7c14bbaSAndroid Build Coastguard Worker int err, n, i, tmp_cpus;
13569*f7c14bbaSAndroid Build Coastguard Worker bool *mask;
13570*f7c14bbaSAndroid Build Coastguard Worker
13571*f7c14bbaSAndroid Build Coastguard Worker tmp_cpus = READ_ONCE(cpus);
13572*f7c14bbaSAndroid Build Coastguard Worker if (tmp_cpus > 0)
13573*f7c14bbaSAndroid Build Coastguard Worker return tmp_cpus;
13574*f7c14bbaSAndroid Build Coastguard Worker
13575*f7c14bbaSAndroid Build Coastguard Worker err = parse_cpu_mask_file(fcpu, &mask, &n);
13576*f7c14bbaSAndroid Build Coastguard Worker if (err)
13577*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(err);
13578*f7c14bbaSAndroid Build Coastguard Worker
13579*f7c14bbaSAndroid Build Coastguard Worker tmp_cpus = 0;
13580*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < n; i++) {
13581*f7c14bbaSAndroid Build Coastguard Worker if (mask[i])
13582*f7c14bbaSAndroid Build Coastguard Worker tmp_cpus++;
13583*f7c14bbaSAndroid Build Coastguard Worker }
13584*f7c14bbaSAndroid Build Coastguard Worker free(mask);
13585*f7c14bbaSAndroid Build Coastguard Worker
13586*f7c14bbaSAndroid Build Coastguard Worker WRITE_ONCE(cpus, tmp_cpus);
13587*f7c14bbaSAndroid Build Coastguard Worker return tmp_cpus;
13588*f7c14bbaSAndroid Build Coastguard Worker }
13589*f7c14bbaSAndroid Build Coastguard Worker
populate_skeleton_maps(const struct bpf_object * obj,struct bpf_map_skeleton * maps,size_t map_cnt,size_t map_skel_sz)13590*f7c14bbaSAndroid Build Coastguard Worker static int populate_skeleton_maps(const struct bpf_object *obj,
13591*f7c14bbaSAndroid Build Coastguard Worker struct bpf_map_skeleton *maps,
13592*f7c14bbaSAndroid Build Coastguard Worker size_t map_cnt, size_t map_skel_sz)
13593*f7c14bbaSAndroid Build Coastguard Worker {
13594*f7c14bbaSAndroid Build Coastguard Worker int i;
13595*f7c14bbaSAndroid Build Coastguard Worker
13596*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < map_cnt; i++) {
13597*f7c14bbaSAndroid Build Coastguard Worker struct bpf_map_skeleton *map_skel = (void *)maps + i * map_skel_sz;
13598*f7c14bbaSAndroid Build Coastguard Worker struct bpf_map **map = map_skel->map;
13599*f7c14bbaSAndroid Build Coastguard Worker const char *name = map_skel->name;
13600*f7c14bbaSAndroid Build Coastguard Worker void **mmaped = map_skel->mmaped;
13601*f7c14bbaSAndroid Build Coastguard Worker
13602*f7c14bbaSAndroid Build Coastguard Worker *map = bpf_object__find_map_by_name(obj, name);
13603*f7c14bbaSAndroid Build Coastguard Worker if (!*map) {
13604*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to find skeleton map '%s'\n", name);
13605*f7c14bbaSAndroid Build Coastguard Worker return -ESRCH;
13606*f7c14bbaSAndroid Build Coastguard Worker }
13607*f7c14bbaSAndroid Build Coastguard Worker
13608*f7c14bbaSAndroid Build Coastguard Worker /* externs shouldn't be pre-setup from user code */
13609*f7c14bbaSAndroid Build Coastguard Worker if (mmaped && (*map)->libbpf_type != LIBBPF_MAP_KCONFIG)
13610*f7c14bbaSAndroid Build Coastguard Worker *mmaped = (*map)->mmaped;
13611*f7c14bbaSAndroid Build Coastguard Worker }
13612*f7c14bbaSAndroid Build Coastguard Worker return 0;
13613*f7c14bbaSAndroid Build Coastguard Worker }
13614*f7c14bbaSAndroid Build Coastguard Worker
populate_skeleton_progs(const struct bpf_object * obj,struct bpf_prog_skeleton * progs,size_t prog_cnt,size_t prog_skel_sz)13615*f7c14bbaSAndroid Build Coastguard Worker static int populate_skeleton_progs(const struct bpf_object *obj,
13616*f7c14bbaSAndroid Build Coastguard Worker struct bpf_prog_skeleton *progs,
13617*f7c14bbaSAndroid Build Coastguard Worker size_t prog_cnt, size_t prog_skel_sz)
13618*f7c14bbaSAndroid Build Coastguard Worker {
13619*f7c14bbaSAndroid Build Coastguard Worker int i;
13620*f7c14bbaSAndroid Build Coastguard Worker
13621*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < prog_cnt; i++) {
13622*f7c14bbaSAndroid Build Coastguard Worker struct bpf_prog_skeleton *prog_skel = (void *)progs + i * prog_skel_sz;
13623*f7c14bbaSAndroid Build Coastguard Worker struct bpf_program **prog = prog_skel->prog;
13624*f7c14bbaSAndroid Build Coastguard Worker const char *name = prog_skel->name;
13625*f7c14bbaSAndroid Build Coastguard Worker
13626*f7c14bbaSAndroid Build Coastguard Worker *prog = bpf_object__find_program_by_name(obj, name);
13627*f7c14bbaSAndroid Build Coastguard Worker if (!*prog) {
13628*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to find skeleton program '%s'\n", name);
13629*f7c14bbaSAndroid Build Coastguard Worker return -ESRCH;
13630*f7c14bbaSAndroid Build Coastguard Worker }
13631*f7c14bbaSAndroid Build Coastguard Worker }
13632*f7c14bbaSAndroid Build Coastguard Worker return 0;
13633*f7c14bbaSAndroid Build Coastguard Worker }
13634*f7c14bbaSAndroid Build Coastguard Worker
bpf_object__open_skeleton(struct bpf_object_skeleton * s,const struct bpf_object_open_opts * opts)13635*f7c14bbaSAndroid Build Coastguard Worker int bpf_object__open_skeleton(struct bpf_object_skeleton *s,
13636*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_object_open_opts *opts)
13637*f7c14bbaSAndroid Build Coastguard Worker {
13638*f7c14bbaSAndroid Build Coastguard Worker DECLARE_LIBBPF_OPTS(bpf_object_open_opts, skel_opts,
13639*f7c14bbaSAndroid Build Coastguard Worker .object_name = s->name,
13640*f7c14bbaSAndroid Build Coastguard Worker );
13641*f7c14bbaSAndroid Build Coastguard Worker struct bpf_object *obj;
13642*f7c14bbaSAndroid Build Coastguard Worker int err;
13643*f7c14bbaSAndroid Build Coastguard Worker
13644*f7c14bbaSAndroid Build Coastguard Worker /* Attempt to preserve opts->object_name, unless overriden by user
13645*f7c14bbaSAndroid Build Coastguard Worker * explicitly. Overwriting object name for skeletons is discouraged,
13646*f7c14bbaSAndroid Build Coastguard Worker * as it breaks global data maps, because they contain object name
13647*f7c14bbaSAndroid Build Coastguard Worker * prefix as their own map name prefix. When skeleton is generated,
13648*f7c14bbaSAndroid Build Coastguard Worker * bpftool is making an assumption that this name will stay the same.
13649*f7c14bbaSAndroid Build Coastguard Worker */
13650*f7c14bbaSAndroid Build Coastguard Worker if (opts) {
13651*f7c14bbaSAndroid Build Coastguard Worker memcpy(&skel_opts, opts, sizeof(*opts));
13652*f7c14bbaSAndroid Build Coastguard Worker if (!opts->object_name)
13653*f7c14bbaSAndroid Build Coastguard Worker skel_opts.object_name = s->name;
13654*f7c14bbaSAndroid Build Coastguard Worker }
13655*f7c14bbaSAndroid Build Coastguard Worker
13656*f7c14bbaSAndroid Build Coastguard Worker obj = bpf_object__open_mem(s->data, s->data_sz, &skel_opts);
13657*f7c14bbaSAndroid Build Coastguard Worker err = libbpf_get_error(obj);
13658*f7c14bbaSAndroid Build Coastguard Worker if (err) {
13659*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to initialize skeleton BPF object '%s': %d\n",
13660*f7c14bbaSAndroid Build Coastguard Worker s->name, err);
13661*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(err);
13662*f7c14bbaSAndroid Build Coastguard Worker }
13663*f7c14bbaSAndroid Build Coastguard Worker
13664*f7c14bbaSAndroid Build Coastguard Worker *s->obj = obj;
13665*f7c14bbaSAndroid Build Coastguard Worker err = populate_skeleton_maps(obj, s->maps, s->map_cnt, s->map_skel_sz);
13666*f7c14bbaSAndroid Build Coastguard Worker if (err) {
13667*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to populate skeleton maps for '%s': %d\n", s->name, err);
13668*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(err);
13669*f7c14bbaSAndroid Build Coastguard Worker }
13670*f7c14bbaSAndroid Build Coastguard Worker
13671*f7c14bbaSAndroid Build Coastguard Worker err = populate_skeleton_progs(obj, s->progs, s->prog_cnt, s->prog_skel_sz);
13672*f7c14bbaSAndroid Build Coastguard Worker if (err) {
13673*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to populate skeleton progs for '%s': %d\n", s->name, err);
13674*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(err);
13675*f7c14bbaSAndroid Build Coastguard Worker }
13676*f7c14bbaSAndroid Build Coastguard Worker
13677*f7c14bbaSAndroid Build Coastguard Worker return 0;
13678*f7c14bbaSAndroid Build Coastguard Worker }
13679*f7c14bbaSAndroid Build Coastguard Worker
bpf_object__open_subskeleton(struct bpf_object_subskeleton * s)13680*f7c14bbaSAndroid Build Coastguard Worker int bpf_object__open_subskeleton(struct bpf_object_subskeleton *s)
13681*f7c14bbaSAndroid Build Coastguard Worker {
13682*f7c14bbaSAndroid Build Coastguard Worker int err, len, var_idx, i;
13683*f7c14bbaSAndroid Build Coastguard Worker const char *var_name;
13684*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_map *map;
13685*f7c14bbaSAndroid Build Coastguard Worker struct btf *btf;
13686*f7c14bbaSAndroid Build Coastguard Worker __u32 map_type_id;
13687*f7c14bbaSAndroid Build Coastguard Worker const struct btf_type *map_type, *var_type;
13688*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_var_skeleton *var_skel;
13689*f7c14bbaSAndroid Build Coastguard Worker struct btf_var_secinfo *var;
13690*f7c14bbaSAndroid Build Coastguard Worker
13691*f7c14bbaSAndroid Build Coastguard Worker if (!s->obj)
13692*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-EINVAL);
13693*f7c14bbaSAndroid Build Coastguard Worker
13694*f7c14bbaSAndroid Build Coastguard Worker btf = bpf_object__btf(s->obj);
13695*f7c14bbaSAndroid Build Coastguard Worker if (!btf) {
13696*f7c14bbaSAndroid Build Coastguard Worker pr_warn("subskeletons require BTF at runtime (object %s)\n",
13697*f7c14bbaSAndroid Build Coastguard Worker bpf_object__name(s->obj));
13698*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-errno);
13699*f7c14bbaSAndroid Build Coastguard Worker }
13700*f7c14bbaSAndroid Build Coastguard Worker
13701*f7c14bbaSAndroid Build Coastguard Worker err = populate_skeleton_maps(s->obj, s->maps, s->map_cnt, s->map_skel_sz);
13702*f7c14bbaSAndroid Build Coastguard Worker if (err) {
13703*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to populate subskeleton maps: %d\n", err);
13704*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(err);
13705*f7c14bbaSAndroid Build Coastguard Worker }
13706*f7c14bbaSAndroid Build Coastguard Worker
13707*f7c14bbaSAndroid Build Coastguard Worker err = populate_skeleton_progs(s->obj, s->progs, s->prog_cnt, s->prog_skel_sz);
13708*f7c14bbaSAndroid Build Coastguard Worker if (err) {
13709*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to populate subskeleton maps: %d\n", err);
13710*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(err);
13711*f7c14bbaSAndroid Build Coastguard Worker }
13712*f7c14bbaSAndroid Build Coastguard Worker
13713*f7c14bbaSAndroid Build Coastguard Worker for (var_idx = 0; var_idx < s->var_cnt; var_idx++) {
13714*f7c14bbaSAndroid Build Coastguard Worker var_skel = (void *)s->vars + var_idx * s->var_skel_sz;
13715*f7c14bbaSAndroid Build Coastguard Worker map = *var_skel->map;
13716*f7c14bbaSAndroid Build Coastguard Worker map_type_id = bpf_map__btf_value_type_id(map);
13717*f7c14bbaSAndroid Build Coastguard Worker map_type = btf__type_by_id(btf, map_type_id);
13718*f7c14bbaSAndroid Build Coastguard Worker
13719*f7c14bbaSAndroid Build Coastguard Worker if (!btf_is_datasec(map_type)) {
13720*f7c14bbaSAndroid Build Coastguard Worker pr_warn("type for map '%1$s' is not a datasec: %2$s",
13721*f7c14bbaSAndroid Build Coastguard Worker bpf_map__name(map),
13722*f7c14bbaSAndroid Build Coastguard Worker __btf_kind_str(btf_kind(map_type)));
13723*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(-EINVAL);
13724*f7c14bbaSAndroid Build Coastguard Worker }
13725*f7c14bbaSAndroid Build Coastguard Worker
13726*f7c14bbaSAndroid Build Coastguard Worker len = btf_vlen(map_type);
13727*f7c14bbaSAndroid Build Coastguard Worker var = btf_var_secinfos(map_type);
13728*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < len; i++, var++) {
13729*f7c14bbaSAndroid Build Coastguard Worker var_type = btf__type_by_id(btf, var->type);
13730*f7c14bbaSAndroid Build Coastguard Worker var_name = btf__name_by_offset(btf, var_type->name_off);
13731*f7c14bbaSAndroid Build Coastguard Worker if (strcmp(var_name, var_skel->name) == 0) {
13732*f7c14bbaSAndroid Build Coastguard Worker *var_skel->addr = map->mmaped + var->offset;
13733*f7c14bbaSAndroid Build Coastguard Worker break;
13734*f7c14bbaSAndroid Build Coastguard Worker }
13735*f7c14bbaSAndroid Build Coastguard Worker }
13736*f7c14bbaSAndroid Build Coastguard Worker }
13737*f7c14bbaSAndroid Build Coastguard Worker return 0;
13738*f7c14bbaSAndroid Build Coastguard Worker }
13739*f7c14bbaSAndroid Build Coastguard Worker
bpf_object__destroy_subskeleton(struct bpf_object_subskeleton * s)13740*f7c14bbaSAndroid Build Coastguard Worker void bpf_object__destroy_subskeleton(struct bpf_object_subskeleton *s)
13741*f7c14bbaSAndroid Build Coastguard Worker {
13742*f7c14bbaSAndroid Build Coastguard Worker if (!s)
13743*f7c14bbaSAndroid Build Coastguard Worker return;
13744*f7c14bbaSAndroid Build Coastguard Worker free(s->maps);
13745*f7c14bbaSAndroid Build Coastguard Worker free(s->progs);
13746*f7c14bbaSAndroid Build Coastguard Worker free(s->vars);
13747*f7c14bbaSAndroid Build Coastguard Worker free(s);
13748*f7c14bbaSAndroid Build Coastguard Worker }
13749*f7c14bbaSAndroid Build Coastguard Worker
bpf_object__load_skeleton(struct bpf_object_skeleton * s)13750*f7c14bbaSAndroid Build Coastguard Worker int bpf_object__load_skeleton(struct bpf_object_skeleton *s)
13751*f7c14bbaSAndroid Build Coastguard Worker {
13752*f7c14bbaSAndroid Build Coastguard Worker int i, err;
13753*f7c14bbaSAndroid Build Coastguard Worker
13754*f7c14bbaSAndroid Build Coastguard Worker err = bpf_object__load(*s->obj);
13755*f7c14bbaSAndroid Build Coastguard Worker if (err) {
13756*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to load BPF skeleton '%s': %d\n", s->name, err);
13757*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(err);
13758*f7c14bbaSAndroid Build Coastguard Worker }
13759*f7c14bbaSAndroid Build Coastguard Worker
13760*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < s->map_cnt; i++) {
13761*f7c14bbaSAndroid Build Coastguard Worker struct bpf_map_skeleton *map_skel = (void *)s->maps + i * s->map_skel_sz;
13762*f7c14bbaSAndroid Build Coastguard Worker struct bpf_map *map = *map_skel->map;
13763*f7c14bbaSAndroid Build Coastguard Worker size_t mmap_sz = bpf_map_mmap_sz(map);
13764*f7c14bbaSAndroid Build Coastguard Worker int prot, map_fd = map->fd;
13765*f7c14bbaSAndroid Build Coastguard Worker void **mmaped = map_skel->mmaped;
13766*f7c14bbaSAndroid Build Coastguard Worker
13767*f7c14bbaSAndroid Build Coastguard Worker if (!mmaped)
13768*f7c14bbaSAndroid Build Coastguard Worker continue;
13769*f7c14bbaSAndroid Build Coastguard Worker
13770*f7c14bbaSAndroid Build Coastguard Worker if (!(map->def.map_flags & BPF_F_MMAPABLE)) {
13771*f7c14bbaSAndroid Build Coastguard Worker *mmaped = NULL;
13772*f7c14bbaSAndroid Build Coastguard Worker continue;
13773*f7c14bbaSAndroid Build Coastguard Worker }
13774*f7c14bbaSAndroid Build Coastguard Worker
13775*f7c14bbaSAndroid Build Coastguard Worker if (map->def.type == BPF_MAP_TYPE_ARENA) {
13776*f7c14bbaSAndroid Build Coastguard Worker *mmaped = map->mmaped;
13777*f7c14bbaSAndroid Build Coastguard Worker continue;
13778*f7c14bbaSAndroid Build Coastguard Worker }
13779*f7c14bbaSAndroid Build Coastguard Worker
13780*f7c14bbaSAndroid Build Coastguard Worker if (map->def.map_flags & BPF_F_RDONLY_PROG)
13781*f7c14bbaSAndroid Build Coastguard Worker prot = PROT_READ;
13782*f7c14bbaSAndroid Build Coastguard Worker else
13783*f7c14bbaSAndroid Build Coastguard Worker prot = PROT_READ | PROT_WRITE;
13784*f7c14bbaSAndroid Build Coastguard Worker
13785*f7c14bbaSAndroid Build Coastguard Worker /* Remap anonymous mmap()-ed "map initialization image" as
13786*f7c14bbaSAndroid Build Coastguard Worker * a BPF map-backed mmap()-ed memory, but preserving the same
13787*f7c14bbaSAndroid Build Coastguard Worker * memory address. This will cause kernel to change process'
13788*f7c14bbaSAndroid Build Coastguard Worker * page table to point to a different piece of kernel memory,
13789*f7c14bbaSAndroid Build Coastguard Worker * but from userspace point of view memory address (and its
13790*f7c14bbaSAndroid Build Coastguard Worker * contents, being identical at this point) will stay the
13791*f7c14bbaSAndroid Build Coastguard Worker * same. This mapping will be released by bpf_object__close()
13792*f7c14bbaSAndroid Build Coastguard Worker * as per normal clean up procedure, so we don't need to worry
13793*f7c14bbaSAndroid Build Coastguard Worker * about it from skeleton's clean up perspective.
13794*f7c14bbaSAndroid Build Coastguard Worker */
13795*f7c14bbaSAndroid Build Coastguard Worker *mmaped = mmap(map->mmaped, mmap_sz, prot, MAP_SHARED | MAP_FIXED, map_fd, 0);
13796*f7c14bbaSAndroid Build Coastguard Worker if (*mmaped == MAP_FAILED) {
13797*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
13798*f7c14bbaSAndroid Build Coastguard Worker *mmaped = NULL;
13799*f7c14bbaSAndroid Build Coastguard Worker pr_warn("failed to re-mmap() map '%s': %d\n",
13800*f7c14bbaSAndroid Build Coastguard Worker bpf_map__name(map), err);
13801*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(err);
13802*f7c14bbaSAndroid Build Coastguard Worker }
13803*f7c14bbaSAndroid Build Coastguard Worker }
13804*f7c14bbaSAndroid Build Coastguard Worker
13805*f7c14bbaSAndroid Build Coastguard Worker return 0;
13806*f7c14bbaSAndroid Build Coastguard Worker }
13807*f7c14bbaSAndroid Build Coastguard Worker
bpf_object__attach_skeleton(struct bpf_object_skeleton * s)13808*f7c14bbaSAndroid Build Coastguard Worker int bpf_object__attach_skeleton(struct bpf_object_skeleton *s)
13809*f7c14bbaSAndroid Build Coastguard Worker {
13810*f7c14bbaSAndroid Build Coastguard Worker int i, err;
13811*f7c14bbaSAndroid Build Coastguard Worker
13812*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < s->prog_cnt; i++) {
13813*f7c14bbaSAndroid Build Coastguard Worker struct bpf_prog_skeleton *prog_skel = (void *)s->progs + i * s->prog_skel_sz;
13814*f7c14bbaSAndroid Build Coastguard Worker struct bpf_program *prog = *prog_skel->prog;
13815*f7c14bbaSAndroid Build Coastguard Worker struct bpf_link **link = prog_skel->link;
13816*f7c14bbaSAndroid Build Coastguard Worker
13817*f7c14bbaSAndroid Build Coastguard Worker if (!prog->autoload || !prog->autoattach)
13818*f7c14bbaSAndroid Build Coastguard Worker continue;
13819*f7c14bbaSAndroid Build Coastguard Worker
13820*f7c14bbaSAndroid Build Coastguard Worker /* auto-attaching not supported for this program */
13821*f7c14bbaSAndroid Build Coastguard Worker if (!prog->sec_def || !prog->sec_def->prog_attach_fn)
13822*f7c14bbaSAndroid Build Coastguard Worker continue;
13823*f7c14bbaSAndroid Build Coastguard Worker
13824*f7c14bbaSAndroid Build Coastguard Worker /* if user already set the link manually, don't attempt auto-attach */
13825*f7c14bbaSAndroid Build Coastguard Worker if (*link)
13826*f7c14bbaSAndroid Build Coastguard Worker continue;
13827*f7c14bbaSAndroid Build Coastguard Worker
13828*f7c14bbaSAndroid Build Coastguard Worker err = prog->sec_def->prog_attach_fn(prog, prog->sec_def->cookie, link);
13829*f7c14bbaSAndroid Build Coastguard Worker if (err) {
13830*f7c14bbaSAndroid Build Coastguard Worker pr_warn("prog '%s': failed to auto-attach: %d\n",
13831*f7c14bbaSAndroid Build Coastguard Worker bpf_program__name(prog), err);
13832*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err(err);
13833*f7c14bbaSAndroid Build Coastguard Worker }
13834*f7c14bbaSAndroid Build Coastguard Worker
13835*f7c14bbaSAndroid Build Coastguard Worker /* It's possible that for some SEC() definitions auto-attach
13836*f7c14bbaSAndroid Build Coastguard Worker * is supported in some cases (e.g., if definition completely
13837*f7c14bbaSAndroid Build Coastguard Worker * specifies target information), but is not in other cases.
13838*f7c14bbaSAndroid Build Coastguard Worker * SEC("uprobe") is one such case. If user specified target
13839*f7c14bbaSAndroid Build Coastguard Worker * binary and function name, such BPF program can be
13840*f7c14bbaSAndroid Build Coastguard Worker * auto-attached. But if not, it shouldn't trigger skeleton's
13841*f7c14bbaSAndroid Build Coastguard Worker * attach to fail. It should just be skipped.
13842*f7c14bbaSAndroid Build Coastguard Worker * attach_fn signals such case with returning 0 (no error) and
13843*f7c14bbaSAndroid Build Coastguard Worker * setting link to NULL.
13844*f7c14bbaSAndroid Build Coastguard Worker */
13845*f7c14bbaSAndroid Build Coastguard Worker }
13846*f7c14bbaSAndroid Build Coastguard Worker
13847*f7c14bbaSAndroid Build Coastguard Worker return 0;
13848*f7c14bbaSAndroid Build Coastguard Worker }
13849*f7c14bbaSAndroid Build Coastguard Worker
bpf_object__detach_skeleton(struct bpf_object_skeleton * s)13850*f7c14bbaSAndroid Build Coastguard Worker void bpf_object__detach_skeleton(struct bpf_object_skeleton *s)
13851*f7c14bbaSAndroid Build Coastguard Worker {
13852*f7c14bbaSAndroid Build Coastguard Worker int i;
13853*f7c14bbaSAndroid Build Coastguard Worker
13854*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < s->prog_cnt; i++) {
13855*f7c14bbaSAndroid Build Coastguard Worker struct bpf_prog_skeleton *prog_skel = (void *)s->progs + i * s->prog_skel_sz;
13856*f7c14bbaSAndroid Build Coastguard Worker struct bpf_link **link = prog_skel->link;
13857*f7c14bbaSAndroid Build Coastguard Worker
13858*f7c14bbaSAndroid Build Coastguard Worker bpf_link__destroy(*link);
13859*f7c14bbaSAndroid Build Coastguard Worker *link = NULL;
13860*f7c14bbaSAndroid Build Coastguard Worker }
13861*f7c14bbaSAndroid Build Coastguard Worker }
13862*f7c14bbaSAndroid Build Coastguard Worker
bpf_object__destroy_skeleton(struct bpf_object_skeleton * s)13863*f7c14bbaSAndroid Build Coastguard Worker void bpf_object__destroy_skeleton(struct bpf_object_skeleton *s)
13864*f7c14bbaSAndroid Build Coastguard Worker {
13865*f7c14bbaSAndroid Build Coastguard Worker if (!s)
13866*f7c14bbaSAndroid Build Coastguard Worker return;
13867*f7c14bbaSAndroid Build Coastguard Worker
13868*f7c14bbaSAndroid Build Coastguard Worker if (s->progs)
13869*f7c14bbaSAndroid Build Coastguard Worker bpf_object__detach_skeleton(s);
13870*f7c14bbaSAndroid Build Coastguard Worker if (s->obj)
13871*f7c14bbaSAndroid Build Coastguard Worker bpf_object__close(*s->obj);
13872*f7c14bbaSAndroid Build Coastguard Worker free(s->maps);
13873*f7c14bbaSAndroid Build Coastguard Worker free(s->progs);
13874*f7c14bbaSAndroid Build Coastguard Worker free(s);
13875*f7c14bbaSAndroid Build Coastguard Worker }
13876