1 // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2
3 /*
4 * Common eBPF ELF object loading operations.
5 *
6 * Copyright (C) 2013-2015 Alexei Starovoitov <ast@kernel.org>
7 * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com>
8 * Copyright (C) 2015 Huawei Inc.
9 * Copyright (C) 2017 Nicira, Inc.
10 * Copyright (C) 2019 Isovalent, Inc.
11 */
12
13 #ifndef _GNU_SOURCE
14 #define _GNU_SOURCE
15 #endif
16 #include <stdlib.h>
17 #include <stdio.h>
18 #include <stdarg.h>
19 #include <libgen.h>
20 #include <inttypes.h>
21 #include <limits.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <endian.h>
25 #include <fcntl.h>
26 #include <errno.h>
27 #include <ctype.h>
28 #include <asm/unistd.h>
29 #include <linux/err.h>
30 #include <linux/kernel.h>
31 #include <linux/bpf.h>
32 #include <linux/btf.h>
33 #include <linux/filter.h>
34 #include <linux/limits.h>
35 #include <linux/perf_event.h>
36 #include <linux/bpf_perf_event.h>
37 #include <linux/ring_buffer.h>
38 #include <sys/epoll.h>
39 #include <sys/ioctl.h>
40 #include <sys/mman.h>
41 #include <sys/stat.h>
42 #include <sys/types.h>
43 #include <sys/vfs.h>
44 #include <sys/utsname.h>
45 #include <sys/resource.h>
46 #include <libelf.h>
47 #include <gelf.h>
48 #include <zlib.h>
49
50 #include "libbpf.h"
51 #include "bpf.h"
52 #include "btf.h"
53 #include "str_error.h"
54 #include "libbpf_internal.h"
55 #include "hashmap.h"
56 #include "bpf_gen_internal.h"
57 #include "zip.h"
58
59 #ifndef BPF_FS_MAGIC
60 #define BPF_FS_MAGIC 0xcafe4a11
61 #endif
62
63 #define BPF_FS_DEFAULT_PATH "/sys/fs/bpf"
64
65 #define BPF_INSN_SZ (sizeof(struct bpf_insn))
66
67 /* vsprintf() in __base_pr() uses nonliteral format string. It may break
68 * compilation if user enables corresponding warning. Disable it explicitly.
69 */
70 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
71
72 #define __printf(a, b) __attribute__((format(printf, a, b)))
73
74 static struct bpf_map *bpf_object__add_map(struct bpf_object *obj);
75 static bool prog_is_subprog(const struct bpf_object *obj, const struct bpf_program *prog);
76 static int map_set_def_max_entries(struct bpf_map *map);
77
78 static const char * const attach_type_name[] = {
79 [BPF_CGROUP_INET_INGRESS] = "cgroup_inet_ingress",
80 [BPF_CGROUP_INET_EGRESS] = "cgroup_inet_egress",
81 [BPF_CGROUP_INET_SOCK_CREATE] = "cgroup_inet_sock_create",
82 [BPF_CGROUP_INET_SOCK_RELEASE] = "cgroup_inet_sock_release",
83 [BPF_CGROUP_SOCK_OPS] = "cgroup_sock_ops",
84 [BPF_CGROUP_DEVICE] = "cgroup_device",
85 [BPF_CGROUP_INET4_BIND] = "cgroup_inet4_bind",
86 [BPF_CGROUP_INET6_BIND] = "cgroup_inet6_bind",
87 [BPF_CGROUP_INET4_CONNECT] = "cgroup_inet4_connect",
88 [BPF_CGROUP_INET6_CONNECT] = "cgroup_inet6_connect",
89 [BPF_CGROUP_UNIX_CONNECT] = "cgroup_unix_connect",
90 [BPF_CGROUP_INET4_POST_BIND] = "cgroup_inet4_post_bind",
91 [BPF_CGROUP_INET6_POST_BIND] = "cgroup_inet6_post_bind",
92 [BPF_CGROUP_INET4_GETPEERNAME] = "cgroup_inet4_getpeername",
93 [BPF_CGROUP_INET6_GETPEERNAME] = "cgroup_inet6_getpeername",
94 [BPF_CGROUP_UNIX_GETPEERNAME] = "cgroup_unix_getpeername",
95 [BPF_CGROUP_INET4_GETSOCKNAME] = "cgroup_inet4_getsockname",
96 [BPF_CGROUP_INET6_GETSOCKNAME] = "cgroup_inet6_getsockname",
97 [BPF_CGROUP_UNIX_GETSOCKNAME] = "cgroup_unix_getsockname",
98 [BPF_CGROUP_UDP4_SENDMSG] = "cgroup_udp4_sendmsg",
99 [BPF_CGROUP_UDP6_SENDMSG] = "cgroup_udp6_sendmsg",
100 [BPF_CGROUP_UNIX_SENDMSG] = "cgroup_unix_sendmsg",
101 [BPF_CGROUP_SYSCTL] = "cgroup_sysctl",
102 [BPF_CGROUP_UDP4_RECVMSG] = "cgroup_udp4_recvmsg",
103 [BPF_CGROUP_UDP6_RECVMSG] = "cgroup_udp6_recvmsg",
104 [BPF_CGROUP_UNIX_RECVMSG] = "cgroup_unix_recvmsg",
105 [BPF_CGROUP_GETSOCKOPT] = "cgroup_getsockopt",
106 [BPF_CGROUP_SETSOCKOPT] = "cgroup_setsockopt",
107 [BPF_SK_SKB_STREAM_PARSER] = "sk_skb_stream_parser",
108 [BPF_SK_SKB_STREAM_VERDICT] = "sk_skb_stream_verdict",
109 [BPF_SK_SKB_VERDICT] = "sk_skb_verdict",
110 [BPF_SK_MSG_VERDICT] = "sk_msg_verdict",
111 [BPF_LIRC_MODE2] = "lirc_mode2",
112 [BPF_FLOW_DISSECTOR] = "flow_dissector",
113 [BPF_TRACE_RAW_TP] = "trace_raw_tp",
114 [BPF_TRACE_FENTRY] = "trace_fentry",
115 [BPF_TRACE_FEXIT] = "trace_fexit",
116 [BPF_MODIFY_RETURN] = "modify_return",
117 [BPF_LSM_MAC] = "lsm_mac",
118 [BPF_LSM_CGROUP] = "lsm_cgroup",
119 [BPF_SK_LOOKUP] = "sk_lookup",
120 [BPF_TRACE_ITER] = "trace_iter",
121 [BPF_XDP_DEVMAP] = "xdp_devmap",
122 [BPF_XDP_CPUMAP] = "xdp_cpumap",
123 [BPF_XDP] = "xdp",
124 [BPF_SK_REUSEPORT_SELECT] = "sk_reuseport_select",
125 [BPF_SK_REUSEPORT_SELECT_OR_MIGRATE] = "sk_reuseport_select_or_migrate",
126 [BPF_PERF_EVENT] = "perf_event",
127 [BPF_TRACE_KPROBE_MULTI] = "trace_kprobe_multi",
128 [BPF_STRUCT_OPS] = "struct_ops",
129 [BPF_NETFILTER] = "netfilter",
130 [BPF_TCX_INGRESS] = "tcx_ingress",
131 [BPF_TCX_EGRESS] = "tcx_egress",
132 [BPF_TRACE_UPROBE_MULTI] = "trace_uprobe_multi",
133 [BPF_NETKIT_PRIMARY] = "netkit_primary",
134 [BPF_NETKIT_PEER] = "netkit_peer",
135 [BPF_TRACE_KPROBE_SESSION] = "trace_kprobe_session",
136 [BPF_TRACE_UPROBE_SESSION] = "trace_uprobe_session",
137 };
138
139 static const char * const link_type_name[] = {
140 [BPF_LINK_TYPE_UNSPEC] = "unspec",
141 [BPF_LINK_TYPE_RAW_TRACEPOINT] = "raw_tracepoint",
142 [BPF_LINK_TYPE_TRACING] = "tracing",
143 [BPF_LINK_TYPE_CGROUP] = "cgroup",
144 [BPF_LINK_TYPE_ITER] = "iter",
145 [BPF_LINK_TYPE_NETNS] = "netns",
146 [BPF_LINK_TYPE_XDP] = "xdp",
147 [BPF_LINK_TYPE_PERF_EVENT] = "perf_event",
148 [BPF_LINK_TYPE_KPROBE_MULTI] = "kprobe_multi",
149 [BPF_LINK_TYPE_STRUCT_OPS] = "struct_ops",
150 [BPF_LINK_TYPE_NETFILTER] = "netfilter",
151 [BPF_LINK_TYPE_TCX] = "tcx",
152 [BPF_LINK_TYPE_UPROBE_MULTI] = "uprobe_multi",
153 [BPF_LINK_TYPE_NETKIT] = "netkit",
154 [BPF_LINK_TYPE_SOCKMAP] = "sockmap",
155 };
156
157 static const char * const map_type_name[] = {
158 [BPF_MAP_TYPE_UNSPEC] = "unspec",
159 [BPF_MAP_TYPE_HASH] = "hash",
160 [BPF_MAP_TYPE_ARRAY] = "array",
161 [BPF_MAP_TYPE_PROG_ARRAY] = "prog_array",
162 [BPF_MAP_TYPE_PERF_EVENT_ARRAY] = "perf_event_array",
163 [BPF_MAP_TYPE_PERCPU_HASH] = "percpu_hash",
164 [BPF_MAP_TYPE_PERCPU_ARRAY] = "percpu_array",
165 [BPF_MAP_TYPE_STACK_TRACE] = "stack_trace",
166 [BPF_MAP_TYPE_CGROUP_ARRAY] = "cgroup_array",
167 [BPF_MAP_TYPE_LRU_HASH] = "lru_hash",
168 [BPF_MAP_TYPE_LRU_PERCPU_HASH] = "lru_percpu_hash",
169 [BPF_MAP_TYPE_LPM_TRIE] = "lpm_trie",
170 [BPF_MAP_TYPE_ARRAY_OF_MAPS] = "array_of_maps",
171 [BPF_MAP_TYPE_HASH_OF_MAPS] = "hash_of_maps",
172 [BPF_MAP_TYPE_DEVMAP] = "devmap",
173 [BPF_MAP_TYPE_DEVMAP_HASH] = "devmap_hash",
174 [BPF_MAP_TYPE_SOCKMAP] = "sockmap",
175 [BPF_MAP_TYPE_CPUMAP] = "cpumap",
176 [BPF_MAP_TYPE_XSKMAP] = "xskmap",
177 [BPF_MAP_TYPE_SOCKHASH] = "sockhash",
178 [BPF_MAP_TYPE_CGROUP_STORAGE] = "cgroup_storage",
179 [BPF_MAP_TYPE_REUSEPORT_SOCKARRAY] = "reuseport_sockarray",
180 [BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE] = "percpu_cgroup_storage",
181 [BPF_MAP_TYPE_QUEUE] = "queue",
182 [BPF_MAP_TYPE_STACK] = "stack",
183 [BPF_MAP_TYPE_SK_STORAGE] = "sk_storage",
184 [BPF_MAP_TYPE_STRUCT_OPS] = "struct_ops",
185 [BPF_MAP_TYPE_RINGBUF] = "ringbuf",
186 [BPF_MAP_TYPE_INODE_STORAGE] = "inode_storage",
187 [BPF_MAP_TYPE_TASK_STORAGE] = "task_storage",
188 [BPF_MAP_TYPE_BLOOM_FILTER] = "bloom_filter",
189 [BPF_MAP_TYPE_USER_RINGBUF] = "user_ringbuf",
190 [BPF_MAP_TYPE_CGRP_STORAGE] = "cgrp_storage",
191 [BPF_MAP_TYPE_ARENA] = "arena",
192 };
193
194 static const char * const prog_type_name[] = {
195 [BPF_PROG_TYPE_UNSPEC] = "unspec",
196 [BPF_PROG_TYPE_SOCKET_FILTER] = "socket_filter",
197 [BPF_PROG_TYPE_KPROBE] = "kprobe",
198 [BPF_PROG_TYPE_SCHED_CLS] = "sched_cls",
199 [BPF_PROG_TYPE_SCHED_ACT] = "sched_act",
200 [BPF_PROG_TYPE_TRACEPOINT] = "tracepoint",
201 [BPF_PROG_TYPE_XDP] = "xdp",
202 [BPF_PROG_TYPE_PERF_EVENT] = "perf_event",
203 [BPF_PROG_TYPE_CGROUP_SKB] = "cgroup_skb",
204 [BPF_PROG_TYPE_CGROUP_SOCK] = "cgroup_sock",
205 [BPF_PROG_TYPE_LWT_IN] = "lwt_in",
206 [BPF_PROG_TYPE_LWT_OUT] = "lwt_out",
207 [BPF_PROG_TYPE_LWT_XMIT] = "lwt_xmit",
208 [BPF_PROG_TYPE_SOCK_OPS] = "sock_ops",
209 [BPF_PROG_TYPE_SK_SKB] = "sk_skb",
210 [BPF_PROG_TYPE_CGROUP_DEVICE] = "cgroup_device",
211 [BPF_PROG_TYPE_SK_MSG] = "sk_msg",
212 [BPF_PROG_TYPE_RAW_TRACEPOINT] = "raw_tracepoint",
213 [BPF_PROG_TYPE_CGROUP_SOCK_ADDR] = "cgroup_sock_addr",
214 [BPF_PROG_TYPE_LWT_SEG6LOCAL] = "lwt_seg6local",
215 [BPF_PROG_TYPE_LIRC_MODE2] = "lirc_mode2",
216 [BPF_PROG_TYPE_SK_REUSEPORT] = "sk_reuseport",
217 [BPF_PROG_TYPE_FLOW_DISSECTOR] = "flow_dissector",
218 [BPF_PROG_TYPE_CGROUP_SYSCTL] = "cgroup_sysctl",
219 [BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE] = "raw_tracepoint_writable",
220 [BPF_PROG_TYPE_CGROUP_SOCKOPT] = "cgroup_sockopt",
221 [BPF_PROG_TYPE_TRACING] = "tracing",
222 [BPF_PROG_TYPE_STRUCT_OPS] = "struct_ops",
223 [BPF_PROG_TYPE_EXT] = "ext",
224 [BPF_PROG_TYPE_LSM] = "lsm",
225 [BPF_PROG_TYPE_SK_LOOKUP] = "sk_lookup",
226 [BPF_PROG_TYPE_SYSCALL] = "syscall",
227 [BPF_PROG_TYPE_NETFILTER] = "netfilter",
228 };
229
__base_pr(enum libbpf_print_level level,const char * format,va_list args)230 static int __base_pr(enum libbpf_print_level level, const char *format,
231 va_list args)
232 {
233 const char *env_var = "LIBBPF_LOG_LEVEL";
234 static enum libbpf_print_level min_level = LIBBPF_INFO;
235 static bool initialized;
236
237 if (!initialized) {
238 char *verbosity;
239
240 initialized = true;
241 verbosity = getenv(env_var);
242 if (verbosity) {
243 if (strcasecmp(verbosity, "warn") == 0)
244 min_level = LIBBPF_WARN;
245 else if (strcasecmp(verbosity, "debug") == 0)
246 min_level = LIBBPF_DEBUG;
247 else if (strcasecmp(verbosity, "info") == 0)
248 min_level = LIBBPF_INFO;
249 else
250 fprintf(stderr, "libbpf: unrecognized '%s' envvar value: '%s', should be one of 'warn', 'debug', or 'info'.\n",
251 env_var, verbosity);
252 }
253 }
254
255 /* if too verbose, skip logging */
256 if (level > min_level)
257 return 0;
258
259 return vfprintf(stderr, format, args);
260 }
261
262 static libbpf_print_fn_t __libbpf_pr = __base_pr;
263
libbpf_set_print(libbpf_print_fn_t fn)264 libbpf_print_fn_t libbpf_set_print(libbpf_print_fn_t fn)
265 {
266 libbpf_print_fn_t old_print_fn;
267
268 old_print_fn = __atomic_exchange_n(&__libbpf_pr, fn, __ATOMIC_RELAXED);
269
270 return old_print_fn;
271 }
272
273 __printf(2, 3)
libbpf_print(enum libbpf_print_level level,const char * format,...)274 void libbpf_print(enum libbpf_print_level level, const char *format, ...)
275 {
276 va_list args;
277 int old_errno;
278 libbpf_print_fn_t print_fn;
279
280 print_fn = __atomic_load_n(&__libbpf_pr, __ATOMIC_RELAXED);
281 if (!print_fn)
282 return;
283
284 old_errno = errno;
285
286 va_start(args, format);
287 __libbpf_pr(level, format, args);
288 va_end(args);
289
290 errno = old_errno;
291 }
292
pr_perm_msg(int err)293 static void pr_perm_msg(int err)
294 {
295 struct rlimit limit;
296 char buf[100];
297
298 if (err != -EPERM || geteuid() != 0)
299 return;
300
301 err = getrlimit(RLIMIT_MEMLOCK, &limit);
302 if (err)
303 return;
304
305 if (limit.rlim_cur == RLIM_INFINITY)
306 return;
307
308 if (limit.rlim_cur < 1024)
309 snprintf(buf, sizeof(buf), "%zu bytes", (size_t)limit.rlim_cur);
310 else if (limit.rlim_cur < 1024*1024)
311 snprintf(buf, sizeof(buf), "%.1f KiB", (double)limit.rlim_cur / 1024);
312 else
313 snprintf(buf, sizeof(buf), "%.1f MiB", (double)limit.rlim_cur / (1024*1024));
314
315 pr_warn("permission error while running as root; try raising 'ulimit -l'? current value: %s\n",
316 buf);
317 }
318
319 #define STRERR_BUFSIZE 128
320
321 /* Copied from tools/perf/util/util.h */
322 #ifndef zfree
323 # define zfree(ptr) ({ free(*ptr); *ptr = NULL; })
324 #endif
325
326 #ifndef zclose
327 # define zclose(fd) ({ \
328 int ___err = 0; \
329 if ((fd) >= 0) \
330 ___err = close((fd)); \
331 fd = -1; \
332 ___err; })
333 #endif
334
ptr_to_u64(const void * ptr)335 static inline __u64 ptr_to_u64(const void *ptr)
336 {
337 return (__u64) (unsigned long) ptr;
338 }
339
libbpf_set_strict_mode(enum libbpf_strict_mode mode)340 int libbpf_set_strict_mode(enum libbpf_strict_mode mode)
341 {
342 /* as of v1.0 libbpf_set_strict_mode() is a no-op */
343 return 0;
344 }
345
libbpf_major_version(void)346 __u32 libbpf_major_version(void)
347 {
348 return LIBBPF_MAJOR_VERSION;
349 }
350
libbpf_minor_version(void)351 __u32 libbpf_minor_version(void)
352 {
353 return LIBBPF_MINOR_VERSION;
354 }
355
libbpf_version_string(void)356 const char *libbpf_version_string(void)
357 {
358 #define __S(X) #X
359 #define _S(X) __S(X)
360 return "v" _S(LIBBPF_MAJOR_VERSION) "." _S(LIBBPF_MINOR_VERSION);
361 #undef _S
362 #undef __S
363 }
364
365 enum reloc_type {
366 RELO_LD64,
367 RELO_CALL,
368 RELO_DATA,
369 RELO_EXTERN_LD64,
370 RELO_EXTERN_CALL,
371 RELO_SUBPROG_ADDR,
372 RELO_CORE,
373 };
374
375 struct reloc_desc {
376 enum reloc_type type;
377 int insn_idx;
378 union {
379 const struct bpf_core_relo *core_relo; /* used when type == RELO_CORE */
380 struct {
381 int map_idx;
382 int sym_off;
383 int ext_idx;
384 };
385 };
386 };
387
388 /* stored as sec_def->cookie for all libbpf-supported SEC()s */
389 enum sec_def_flags {
390 SEC_NONE = 0,
391 /* expected_attach_type is optional, if kernel doesn't support that */
392 SEC_EXP_ATTACH_OPT = 1,
393 /* legacy, only used by libbpf_get_type_names() and
394 * libbpf_attach_type_by_name(), not used by libbpf itself at all.
395 * This used to be associated with cgroup (and few other) BPF programs
396 * that were attachable through BPF_PROG_ATTACH command. Pretty
397 * meaningless nowadays, though.
398 */
399 SEC_ATTACHABLE = 2,
400 SEC_ATTACHABLE_OPT = SEC_ATTACHABLE | SEC_EXP_ATTACH_OPT,
401 /* attachment target is specified through BTF ID in either kernel or
402 * other BPF program's BTF object
403 */
404 SEC_ATTACH_BTF = 4,
405 /* BPF program type allows sleeping/blocking in kernel */
406 SEC_SLEEPABLE = 8,
407 /* BPF program support non-linear XDP buffer */
408 SEC_XDP_FRAGS = 16,
409 /* Setup proper attach type for usdt probes. */
410 SEC_USDT = 32,
411 };
412
413 struct bpf_sec_def {
414 char *sec;
415 enum bpf_prog_type prog_type;
416 enum bpf_attach_type expected_attach_type;
417 long cookie;
418 int handler_id;
419
420 libbpf_prog_setup_fn_t prog_setup_fn;
421 libbpf_prog_prepare_load_fn_t prog_prepare_load_fn;
422 libbpf_prog_attach_fn_t prog_attach_fn;
423 };
424
425 /*
426 * bpf_prog should be a better name but it has been used in
427 * linux/filter.h.
428 */
429 struct bpf_program {
430 char *name;
431 char *sec_name;
432 size_t sec_idx;
433 const struct bpf_sec_def *sec_def;
434 /* this program's instruction offset (in number of instructions)
435 * within its containing ELF section
436 */
437 size_t sec_insn_off;
438 /* number of original instructions in ELF section belonging to this
439 * program, not taking into account subprogram instructions possible
440 * appended later during relocation
441 */
442 size_t sec_insn_cnt;
443 /* Offset (in number of instructions) of the start of instruction
444 * belonging to this BPF program within its containing main BPF
445 * program. For the entry-point (main) BPF program, this is always
446 * zero. For a sub-program, this gets reset before each of main BPF
447 * programs are processed and relocated and is used to determined
448 * whether sub-program was already appended to the main program, and
449 * if yes, at which instruction offset.
450 */
451 size_t sub_insn_off;
452
453 /* instructions that belong to BPF program; insns[0] is located at
454 * sec_insn_off instruction within its ELF section in ELF file, so
455 * when mapping ELF file instruction index to the local instruction,
456 * one needs to subtract sec_insn_off; and vice versa.
457 */
458 struct bpf_insn *insns;
459 /* actual number of instruction in this BPF program's image; for
460 * entry-point BPF programs this includes the size of main program
461 * itself plus all the used sub-programs, appended at the end
462 */
463 size_t insns_cnt;
464
465 struct reloc_desc *reloc_desc;
466 int nr_reloc;
467
468 /* BPF verifier log settings */
469 char *log_buf;
470 size_t log_size;
471 __u32 log_level;
472
473 struct bpf_object *obj;
474
475 int fd;
476 bool autoload;
477 bool autoattach;
478 bool sym_global;
479 bool mark_btf_static;
480 enum bpf_prog_type type;
481 enum bpf_attach_type expected_attach_type;
482 int exception_cb_idx;
483
484 int prog_ifindex;
485 __u32 attach_btf_obj_fd;
486 __u32 attach_btf_id;
487 __u32 attach_prog_fd;
488
489 void *func_info;
490 __u32 func_info_rec_size;
491 __u32 func_info_cnt;
492
493 void *line_info;
494 __u32 line_info_rec_size;
495 __u32 line_info_cnt;
496 __u32 prog_flags;
497 };
498
499 struct bpf_struct_ops {
500 struct bpf_program **progs;
501 __u32 *kern_func_off;
502 /* e.g. struct tcp_congestion_ops in bpf_prog's btf format */
503 void *data;
504 /* e.g. struct bpf_struct_ops_tcp_congestion_ops in
505 * btf_vmlinux's format.
506 * struct bpf_struct_ops_tcp_congestion_ops {
507 * [... some other kernel fields ...]
508 * struct tcp_congestion_ops data;
509 * }
510 * kern_vdata-size == sizeof(struct bpf_struct_ops_tcp_congestion_ops)
511 * bpf_map__init_kern_struct_ops() will populate the "kern_vdata"
512 * from "data".
513 */
514 void *kern_vdata;
515 __u32 type_id;
516 };
517
518 #define DATA_SEC ".data"
519 #define BSS_SEC ".bss"
520 #define RODATA_SEC ".rodata"
521 #define KCONFIG_SEC ".kconfig"
522 #define KSYMS_SEC ".ksyms"
523 #define STRUCT_OPS_SEC ".struct_ops"
524 #define STRUCT_OPS_LINK_SEC ".struct_ops.link"
525 #define ARENA_SEC ".addr_space.1"
526
527 enum libbpf_map_type {
528 LIBBPF_MAP_UNSPEC,
529 LIBBPF_MAP_DATA,
530 LIBBPF_MAP_BSS,
531 LIBBPF_MAP_RODATA,
532 LIBBPF_MAP_KCONFIG,
533 };
534
535 struct bpf_map_def {
536 unsigned int type;
537 unsigned int key_size;
538 unsigned int value_size;
539 unsigned int max_entries;
540 unsigned int map_flags;
541 };
542
543 struct bpf_map {
544 struct bpf_object *obj;
545 char *name;
546 /* real_name is defined for special internal maps (.rodata*,
547 * .data*, .bss, .kconfig) and preserves their original ELF section
548 * name. This is important to be able to find corresponding BTF
549 * DATASEC information.
550 */
551 char *real_name;
552 int fd;
553 int sec_idx;
554 size_t sec_offset;
555 int map_ifindex;
556 int inner_map_fd;
557 struct bpf_map_def def;
558 __u32 numa_node;
559 __u32 btf_var_idx;
560 int mod_btf_fd;
561 __u32 btf_key_type_id;
562 __u32 btf_value_type_id;
563 __u32 btf_vmlinux_value_type_id;
564 enum libbpf_map_type libbpf_type;
565 void *mmaped;
566 struct bpf_struct_ops *st_ops;
567 struct bpf_map *inner_map;
568 void **init_slots;
569 int init_slots_sz;
570 char *pin_path;
571 bool pinned;
572 bool reused;
573 bool autocreate;
574 bool autoattach;
575 __u64 map_extra;
576 };
577
578 enum extern_type {
579 EXT_UNKNOWN,
580 EXT_KCFG,
581 EXT_KSYM,
582 };
583
584 enum kcfg_type {
585 KCFG_UNKNOWN,
586 KCFG_CHAR,
587 KCFG_BOOL,
588 KCFG_INT,
589 KCFG_TRISTATE,
590 KCFG_CHAR_ARR,
591 };
592
593 struct extern_desc {
594 enum extern_type type;
595 int sym_idx;
596 int btf_id;
597 int sec_btf_id;
598 const char *name;
599 char *essent_name;
600 bool is_set;
601 bool is_weak;
602 union {
603 struct {
604 enum kcfg_type type;
605 int sz;
606 int align;
607 int data_off;
608 bool is_signed;
609 } kcfg;
610 struct {
611 unsigned long long addr;
612
613 /* target btf_id of the corresponding kernel var. */
614 int kernel_btf_obj_fd;
615 int kernel_btf_id;
616
617 /* local btf_id of the ksym extern's type. */
618 __u32 type_id;
619 /* BTF fd index to be patched in for insn->off, this is
620 * 0 for vmlinux BTF, index in obj->fd_array for module
621 * BTF
622 */
623 __s16 btf_fd_idx;
624 } ksym;
625 };
626 };
627
628 struct module_btf {
629 struct btf *btf;
630 char *name;
631 __u32 id;
632 int fd;
633 int fd_array_idx;
634 };
635
636 enum sec_type {
637 SEC_UNUSED = 0,
638 SEC_RELO,
639 SEC_BSS,
640 SEC_DATA,
641 SEC_RODATA,
642 SEC_ST_OPS,
643 };
644
645 struct elf_sec_desc {
646 enum sec_type sec_type;
647 Elf64_Shdr *shdr;
648 Elf_Data *data;
649 };
650
651 struct elf_state {
652 int fd;
653 const void *obj_buf;
654 size_t obj_buf_sz;
655 Elf *elf;
656 Elf64_Ehdr *ehdr;
657 Elf_Data *symbols;
658 Elf_Data *arena_data;
659 size_t shstrndx; /* section index for section name strings */
660 size_t strtabidx;
661 struct elf_sec_desc *secs;
662 size_t sec_cnt;
663 int btf_maps_shndx;
664 __u32 btf_maps_sec_btf_id;
665 int text_shndx;
666 int symbols_shndx;
667 bool has_st_ops;
668 int arena_data_shndx;
669 };
670
671 struct usdt_manager;
672
673 struct bpf_object {
674 char name[BPF_OBJ_NAME_LEN];
675 char license[64];
676 __u32 kern_version;
677
678 struct bpf_program *programs;
679 size_t nr_programs;
680 struct bpf_map *maps;
681 size_t nr_maps;
682 size_t maps_cap;
683
684 char *kconfig;
685 struct extern_desc *externs;
686 int nr_extern;
687 int kconfig_map_idx;
688
689 bool loaded;
690 bool has_subcalls;
691 bool has_rodata;
692
693 struct bpf_gen *gen_loader;
694
695 /* Information when doing ELF related work. Only valid if efile.elf is not NULL */
696 struct elf_state efile;
697
698 unsigned char byteorder;
699
700 struct btf *btf;
701 struct btf_ext *btf_ext;
702
703 /* Parse and load BTF vmlinux if any of the programs in the object need
704 * it at load time.
705 */
706 struct btf *btf_vmlinux;
707 /* Path to the custom BTF to be used for BPF CO-RE relocations as an
708 * override for vmlinux BTF.
709 */
710 char *btf_custom_path;
711 /* vmlinux BTF override for CO-RE relocations */
712 struct btf *btf_vmlinux_override;
713 /* Lazily initialized kernel module BTFs */
714 struct module_btf *btf_modules;
715 bool btf_modules_loaded;
716 size_t btf_module_cnt;
717 size_t btf_module_cap;
718
719 /* optional log settings passed to BPF_BTF_LOAD and BPF_PROG_LOAD commands */
720 char *log_buf;
721 size_t log_size;
722 __u32 log_level;
723
724 int *fd_array;
725 size_t fd_array_cap;
726 size_t fd_array_cnt;
727
728 struct usdt_manager *usdt_man;
729
730 struct bpf_map *arena_map;
731 void *arena_data;
732 size_t arena_data_sz;
733
734 struct kern_feature_cache *feat_cache;
735 char *token_path;
736 int token_fd;
737
738 char path[];
739 };
740
741 static const char *elf_sym_str(const struct bpf_object *obj, size_t off);
742 static const char *elf_sec_str(const struct bpf_object *obj, size_t off);
743 static Elf_Scn *elf_sec_by_idx(const struct bpf_object *obj, size_t idx);
744 static Elf_Scn *elf_sec_by_name(const struct bpf_object *obj, const char *name);
745 static Elf64_Shdr *elf_sec_hdr(const struct bpf_object *obj, Elf_Scn *scn);
746 static const char *elf_sec_name(const struct bpf_object *obj, Elf_Scn *scn);
747 static Elf_Data *elf_sec_data(const struct bpf_object *obj, Elf_Scn *scn);
748 static Elf64_Sym *elf_sym_by_idx(const struct bpf_object *obj, size_t idx);
749 static Elf64_Rel *elf_rel_by_idx(Elf_Data *data, size_t idx);
750
bpf_program__unload(struct bpf_program * prog)751 void bpf_program__unload(struct bpf_program *prog)
752 {
753 if (!prog)
754 return;
755
756 zclose(prog->fd);
757
758 zfree(&prog->func_info);
759 zfree(&prog->line_info);
760 }
761
bpf_program__exit(struct bpf_program * prog)762 static void bpf_program__exit(struct bpf_program *prog)
763 {
764 if (!prog)
765 return;
766
767 bpf_program__unload(prog);
768 zfree(&prog->name);
769 zfree(&prog->sec_name);
770 zfree(&prog->insns);
771 zfree(&prog->reloc_desc);
772
773 prog->nr_reloc = 0;
774 prog->insns_cnt = 0;
775 prog->sec_idx = -1;
776 }
777
insn_is_subprog_call(const struct bpf_insn * insn)778 static bool insn_is_subprog_call(const struct bpf_insn *insn)
779 {
780 return BPF_CLASS(insn->code) == BPF_JMP &&
781 BPF_OP(insn->code) == BPF_CALL &&
782 BPF_SRC(insn->code) == BPF_K &&
783 insn->src_reg == BPF_PSEUDO_CALL &&
784 insn->dst_reg == 0 &&
785 insn->off == 0;
786 }
787
is_call_insn(const struct bpf_insn * insn)788 static bool is_call_insn(const struct bpf_insn *insn)
789 {
790 return insn->code == (BPF_JMP | BPF_CALL);
791 }
792
insn_is_pseudo_func(struct bpf_insn * insn)793 static bool insn_is_pseudo_func(struct bpf_insn *insn)
794 {
795 return is_ldimm64_insn(insn) && insn->src_reg == BPF_PSEUDO_FUNC;
796 }
797
798 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)799 bpf_object__init_prog(struct bpf_object *obj, struct bpf_program *prog,
800 const char *name, size_t sec_idx, const char *sec_name,
801 size_t sec_off, void *insn_data, size_t insn_data_sz)
802 {
803 if (insn_data_sz == 0 || insn_data_sz % BPF_INSN_SZ || sec_off % BPF_INSN_SZ) {
804 pr_warn("sec '%s': corrupted program '%s', offset %zu, size %zu\n",
805 sec_name, name, sec_off, insn_data_sz);
806 return -EINVAL;
807 }
808
809 memset(prog, 0, sizeof(*prog));
810 prog->obj = obj;
811
812 prog->sec_idx = sec_idx;
813 prog->sec_insn_off = sec_off / BPF_INSN_SZ;
814 prog->sec_insn_cnt = insn_data_sz / BPF_INSN_SZ;
815 /* insns_cnt can later be increased by appending used subprograms */
816 prog->insns_cnt = prog->sec_insn_cnt;
817
818 prog->type = BPF_PROG_TYPE_UNSPEC;
819 prog->fd = -1;
820 prog->exception_cb_idx = -1;
821
822 /* libbpf's convention for SEC("?abc...") is that it's just like
823 * SEC("abc...") but the corresponding bpf_program starts out with
824 * autoload set to false.
825 */
826 if (sec_name[0] == '?') {
827 prog->autoload = false;
828 /* from now on forget there was ? in section name */
829 sec_name++;
830 } else {
831 prog->autoload = true;
832 }
833
834 prog->autoattach = true;
835
836 /* inherit object's log_level */
837 prog->log_level = obj->log_level;
838
839 prog->sec_name = strdup(sec_name);
840 if (!prog->sec_name)
841 goto errout;
842
843 prog->name = strdup(name);
844 if (!prog->name)
845 goto errout;
846
847 prog->insns = malloc(insn_data_sz);
848 if (!prog->insns)
849 goto errout;
850 memcpy(prog->insns, insn_data, insn_data_sz);
851
852 return 0;
853 errout:
854 pr_warn("sec '%s': failed to allocate memory for prog '%s'\n", sec_name, name);
855 bpf_program__exit(prog);
856 return -ENOMEM;
857 }
858
859 static int
bpf_object__add_programs(struct bpf_object * obj,Elf_Data * sec_data,const char * sec_name,int sec_idx)860 bpf_object__add_programs(struct bpf_object *obj, Elf_Data *sec_data,
861 const char *sec_name, int sec_idx)
862 {
863 Elf_Data *symbols = obj->efile.symbols;
864 struct bpf_program *prog, *progs;
865 void *data = sec_data->d_buf;
866 size_t sec_sz = sec_data->d_size, sec_off, prog_sz, nr_syms;
867 int nr_progs, err, i;
868 const char *name;
869 Elf64_Sym *sym;
870
871 progs = obj->programs;
872 nr_progs = obj->nr_programs;
873 nr_syms = symbols->d_size / sizeof(Elf64_Sym);
874
875 for (i = 0; i < nr_syms; i++) {
876 sym = elf_sym_by_idx(obj, i);
877
878 if (sym->st_shndx != sec_idx)
879 continue;
880 if (ELF64_ST_TYPE(sym->st_info) != STT_FUNC)
881 continue;
882
883 prog_sz = sym->st_size;
884 sec_off = sym->st_value;
885
886 name = elf_sym_str(obj, sym->st_name);
887 if (!name) {
888 pr_warn("sec '%s': failed to get symbol name for offset %zu\n",
889 sec_name, sec_off);
890 return -LIBBPF_ERRNO__FORMAT;
891 }
892
893 if (sec_off + prog_sz > sec_sz) {
894 pr_warn("sec '%s': program at offset %zu crosses section boundary\n",
895 sec_name, sec_off);
896 return -LIBBPF_ERRNO__FORMAT;
897 }
898
899 if (sec_idx != obj->efile.text_shndx && ELF64_ST_BIND(sym->st_info) == STB_LOCAL) {
900 pr_warn("sec '%s': program '%s' is static and not supported\n", sec_name, name);
901 return -ENOTSUP;
902 }
903
904 pr_debug("sec '%s': found program '%s' at insn offset %zu (%zu bytes), code size %zu insns (%zu bytes)\n",
905 sec_name, name, sec_off / BPF_INSN_SZ, sec_off, prog_sz / BPF_INSN_SZ, prog_sz);
906
907 progs = libbpf_reallocarray(progs, nr_progs + 1, sizeof(*progs));
908 if (!progs) {
909 /*
910 * In this case the original obj->programs
911 * is still valid, so don't need special treat for
912 * bpf_close_object().
913 */
914 pr_warn("sec '%s': failed to alloc memory for new program '%s'\n",
915 sec_name, name);
916 return -ENOMEM;
917 }
918 obj->programs = progs;
919
920 prog = &progs[nr_progs];
921
922 err = bpf_object__init_prog(obj, prog, name, sec_idx, sec_name,
923 sec_off, data + sec_off, prog_sz);
924 if (err)
925 return err;
926
927 if (ELF64_ST_BIND(sym->st_info) != STB_LOCAL)
928 prog->sym_global = true;
929
930 /* if function is a global/weak symbol, but has restricted
931 * (STV_HIDDEN or STV_INTERNAL) visibility, mark its BTF FUNC
932 * as static to enable more permissive BPF verification mode
933 * with more outside context available to BPF verifier
934 */
935 if (prog->sym_global && (ELF64_ST_VISIBILITY(sym->st_other) == STV_HIDDEN
936 || ELF64_ST_VISIBILITY(sym->st_other) == STV_INTERNAL))
937 prog->mark_btf_static = true;
938
939 nr_progs++;
940 obj->nr_programs = nr_progs;
941 }
942
943 return 0;
944 }
945
bpf_object_bswap_progs(struct bpf_object * obj)946 static void bpf_object_bswap_progs(struct bpf_object *obj)
947 {
948 struct bpf_program *prog = obj->programs;
949 struct bpf_insn *insn;
950 int p, i;
951
952 for (p = 0; p < obj->nr_programs; p++, prog++) {
953 insn = prog->insns;
954 for (i = 0; i < prog->insns_cnt; i++, insn++)
955 bpf_insn_bswap(insn);
956 }
957 pr_debug("converted %zu BPF programs to native byte order\n", obj->nr_programs);
958 }
959
960 static const struct btf_member *
find_member_by_offset(const struct btf_type * t,__u32 bit_offset)961 find_member_by_offset(const struct btf_type *t, __u32 bit_offset)
962 {
963 struct btf_member *m;
964 int i;
965
966 for (i = 0, m = btf_members(t); i < btf_vlen(t); i++, m++) {
967 if (btf_member_bit_offset(t, i) == bit_offset)
968 return m;
969 }
970
971 return NULL;
972 }
973
974 static const struct btf_member *
find_member_by_name(const struct btf * btf,const struct btf_type * t,const char * name)975 find_member_by_name(const struct btf *btf, const struct btf_type *t,
976 const char *name)
977 {
978 struct btf_member *m;
979 int i;
980
981 for (i = 0, m = btf_members(t); i < btf_vlen(t); i++, m++) {
982 if (!strcmp(btf__name_by_offset(btf, m->name_off), name))
983 return m;
984 }
985
986 return NULL;
987 }
988
989 static int find_ksym_btf_id(struct bpf_object *obj, const char *ksym_name,
990 __u16 kind, struct btf **res_btf,
991 struct module_btf **res_mod_btf);
992
993 #define STRUCT_OPS_VALUE_PREFIX "bpf_struct_ops_"
994 static int find_btf_by_prefix_kind(const struct btf *btf, const char *prefix,
995 const char *name, __u32 kind);
996
997 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)998 find_struct_ops_kern_types(struct bpf_object *obj, const char *tname_raw,
999 struct module_btf **mod_btf,
1000 const struct btf_type **type, __u32 *type_id,
1001 const struct btf_type **vtype, __u32 *vtype_id,
1002 const struct btf_member **data_member)
1003 {
1004 const struct btf_type *kern_type, *kern_vtype;
1005 const struct btf_member *kern_data_member;
1006 struct btf *btf = NULL;
1007 __s32 kern_vtype_id, kern_type_id;
1008 char tname[256];
1009 __u32 i;
1010
1011 snprintf(tname, sizeof(tname), "%.*s",
1012 (int)bpf_core_essential_name_len(tname_raw), tname_raw);
1013
1014 kern_type_id = find_ksym_btf_id(obj, tname, BTF_KIND_STRUCT,
1015 &btf, mod_btf);
1016 if (kern_type_id < 0) {
1017 pr_warn("struct_ops init_kern: struct %s is not found in kernel BTF\n",
1018 tname);
1019 return kern_type_id;
1020 }
1021 kern_type = btf__type_by_id(btf, kern_type_id);
1022
1023 /* Find the corresponding "map_value" type that will be used
1024 * in map_update(BPF_MAP_TYPE_STRUCT_OPS). For example,
1025 * find "struct bpf_struct_ops_tcp_congestion_ops" from the
1026 * btf_vmlinux.
1027 */
1028 kern_vtype_id = find_btf_by_prefix_kind(btf, STRUCT_OPS_VALUE_PREFIX,
1029 tname, BTF_KIND_STRUCT);
1030 if (kern_vtype_id < 0) {
1031 pr_warn("struct_ops init_kern: struct %s%s is not found in kernel BTF\n",
1032 STRUCT_OPS_VALUE_PREFIX, tname);
1033 return kern_vtype_id;
1034 }
1035 kern_vtype = btf__type_by_id(btf, kern_vtype_id);
1036
1037 /* Find "struct tcp_congestion_ops" from
1038 * struct bpf_struct_ops_tcp_congestion_ops {
1039 * [ ... ]
1040 * struct tcp_congestion_ops data;
1041 * }
1042 */
1043 kern_data_member = btf_members(kern_vtype);
1044 for (i = 0; i < btf_vlen(kern_vtype); i++, kern_data_member++) {
1045 if (kern_data_member->type == kern_type_id)
1046 break;
1047 }
1048 if (i == btf_vlen(kern_vtype)) {
1049 pr_warn("struct_ops init_kern: struct %s data is not found in struct %s%s\n",
1050 tname, STRUCT_OPS_VALUE_PREFIX, tname);
1051 return -EINVAL;
1052 }
1053
1054 *type = kern_type;
1055 *type_id = kern_type_id;
1056 *vtype = kern_vtype;
1057 *vtype_id = kern_vtype_id;
1058 *data_member = kern_data_member;
1059
1060 return 0;
1061 }
1062
bpf_map__is_struct_ops(const struct bpf_map * map)1063 static bool bpf_map__is_struct_ops(const struct bpf_map *map)
1064 {
1065 return map->def.type == BPF_MAP_TYPE_STRUCT_OPS;
1066 }
1067
is_valid_st_ops_program(struct bpf_object * obj,const struct bpf_program * prog)1068 static bool is_valid_st_ops_program(struct bpf_object *obj,
1069 const struct bpf_program *prog)
1070 {
1071 int i;
1072
1073 for (i = 0; i < obj->nr_programs; i++) {
1074 if (&obj->programs[i] == prog)
1075 return prog->type == BPF_PROG_TYPE_STRUCT_OPS;
1076 }
1077
1078 return false;
1079 }
1080
1081 /* For each struct_ops program P, referenced from some struct_ops map M,
1082 * enable P.autoload if there are Ms for which M.autocreate is true,
1083 * disable P.autoload if for all Ms M.autocreate is false.
1084 * Don't change P.autoload for programs that are not referenced from any maps.
1085 */
bpf_object_adjust_struct_ops_autoload(struct bpf_object * obj)1086 static int bpf_object_adjust_struct_ops_autoload(struct bpf_object *obj)
1087 {
1088 struct bpf_program *prog, *slot_prog;
1089 struct bpf_map *map;
1090 int i, j, k, vlen;
1091
1092 for (i = 0; i < obj->nr_programs; ++i) {
1093 int should_load = false;
1094 int use_cnt = 0;
1095
1096 prog = &obj->programs[i];
1097 if (prog->type != BPF_PROG_TYPE_STRUCT_OPS)
1098 continue;
1099
1100 for (j = 0; j < obj->nr_maps; ++j) {
1101 const struct btf_type *type;
1102
1103 map = &obj->maps[j];
1104 if (!bpf_map__is_struct_ops(map))
1105 continue;
1106
1107 type = btf__type_by_id(obj->btf, map->st_ops->type_id);
1108 vlen = btf_vlen(type);
1109 for (k = 0; k < vlen; ++k) {
1110 slot_prog = map->st_ops->progs[k];
1111 if (prog != slot_prog)
1112 continue;
1113
1114 use_cnt++;
1115 if (map->autocreate)
1116 should_load = true;
1117 }
1118 }
1119 if (use_cnt)
1120 prog->autoload = should_load;
1121 }
1122
1123 return 0;
1124 }
1125
1126 /* Init the map's fields that depend on kern_btf */
bpf_map__init_kern_struct_ops(struct bpf_map * map)1127 static int bpf_map__init_kern_struct_ops(struct bpf_map *map)
1128 {
1129 const struct btf_member *member, *kern_member, *kern_data_member;
1130 const struct btf_type *type, *kern_type, *kern_vtype;
1131 __u32 i, kern_type_id, kern_vtype_id, kern_data_off;
1132 struct bpf_object *obj = map->obj;
1133 const struct btf *btf = obj->btf;
1134 struct bpf_struct_ops *st_ops;
1135 const struct btf *kern_btf;
1136 struct module_btf *mod_btf = NULL;
1137 void *data, *kern_data;
1138 const char *tname;
1139 int err;
1140
1141 st_ops = map->st_ops;
1142 type = btf__type_by_id(btf, st_ops->type_id);
1143 tname = btf__name_by_offset(btf, type->name_off);
1144 err = find_struct_ops_kern_types(obj, tname, &mod_btf,
1145 &kern_type, &kern_type_id,
1146 &kern_vtype, &kern_vtype_id,
1147 &kern_data_member);
1148 if (err)
1149 return err;
1150
1151 kern_btf = mod_btf ? mod_btf->btf : obj->btf_vmlinux;
1152
1153 pr_debug("struct_ops init_kern %s: type_id:%u kern_type_id:%u kern_vtype_id:%u\n",
1154 map->name, st_ops->type_id, kern_type_id, kern_vtype_id);
1155
1156 map->mod_btf_fd = mod_btf ? mod_btf->fd : -1;
1157 map->def.value_size = kern_vtype->size;
1158 map->btf_vmlinux_value_type_id = kern_vtype_id;
1159
1160 st_ops->kern_vdata = calloc(1, kern_vtype->size);
1161 if (!st_ops->kern_vdata)
1162 return -ENOMEM;
1163
1164 data = st_ops->data;
1165 kern_data_off = kern_data_member->offset / 8;
1166 kern_data = st_ops->kern_vdata + kern_data_off;
1167
1168 member = btf_members(type);
1169 for (i = 0; i < btf_vlen(type); i++, member++) {
1170 const struct btf_type *mtype, *kern_mtype;
1171 __u32 mtype_id, kern_mtype_id;
1172 void *mdata, *kern_mdata;
1173 struct bpf_program *prog;
1174 __s64 msize, kern_msize;
1175 __u32 moff, kern_moff;
1176 __u32 kern_member_idx;
1177 const char *mname;
1178
1179 mname = btf__name_by_offset(btf, member->name_off);
1180 moff = member->offset / 8;
1181 mdata = data + moff;
1182 msize = btf__resolve_size(btf, member->type);
1183 if (msize < 0) {
1184 pr_warn("struct_ops init_kern %s: failed to resolve the size of member %s\n",
1185 map->name, mname);
1186 return msize;
1187 }
1188
1189 kern_member = find_member_by_name(kern_btf, kern_type, mname);
1190 if (!kern_member) {
1191 if (!libbpf_is_mem_zeroed(mdata, msize)) {
1192 pr_warn("struct_ops init_kern %s: Cannot find member %s in kernel BTF\n",
1193 map->name, mname);
1194 return -ENOTSUP;
1195 }
1196
1197 if (st_ops->progs[i]) {
1198 /* If we had declaratively set struct_ops callback, we need to
1199 * force its autoload to false, because it doesn't have
1200 * a chance of succeeding from POV of the current struct_ops map.
1201 * If this program is still referenced somewhere else, though,
1202 * then bpf_object_adjust_struct_ops_autoload() will update its
1203 * autoload accordingly.
1204 */
1205 st_ops->progs[i]->autoload = false;
1206 st_ops->progs[i] = NULL;
1207 }
1208
1209 /* Skip all-zero/NULL fields if they are not present in the kernel BTF */
1210 pr_info("struct_ops %s: member %s not found in kernel, skipping it as it's set to zero\n",
1211 map->name, mname);
1212 continue;
1213 }
1214
1215 kern_member_idx = kern_member - btf_members(kern_type);
1216 if (btf_member_bitfield_size(type, i) ||
1217 btf_member_bitfield_size(kern_type, kern_member_idx)) {
1218 pr_warn("struct_ops init_kern %s: bitfield %s is not supported\n",
1219 map->name, mname);
1220 return -ENOTSUP;
1221 }
1222
1223 kern_moff = kern_member->offset / 8;
1224 kern_mdata = kern_data + kern_moff;
1225
1226 mtype = skip_mods_and_typedefs(btf, member->type, &mtype_id);
1227 kern_mtype = skip_mods_and_typedefs(kern_btf, kern_member->type,
1228 &kern_mtype_id);
1229 if (BTF_INFO_KIND(mtype->info) !=
1230 BTF_INFO_KIND(kern_mtype->info)) {
1231 pr_warn("struct_ops init_kern %s: Unmatched member type %s %u != %u(kernel)\n",
1232 map->name, mname, BTF_INFO_KIND(mtype->info),
1233 BTF_INFO_KIND(kern_mtype->info));
1234 return -ENOTSUP;
1235 }
1236
1237 if (btf_is_ptr(mtype)) {
1238 prog = *(void **)mdata;
1239 /* just like for !kern_member case above, reset declaratively
1240 * set (at compile time) program's autload to false,
1241 * if user replaced it with another program or NULL
1242 */
1243 if (st_ops->progs[i] && st_ops->progs[i] != prog)
1244 st_ops->progs[i]->autoload = false;
1245
1246 /* Update the value from the shadow type */
1247 st_ops->progs[i] = prog;
1248 if (!prog)
1249 continue;
1250
1251 if (!is_valid_st_ops_program(obj, prog)) {
1252 pr_warn("struct_ops init_kern %s: member %s is not a struct_ops program\n",
1253 map->name, mname);
1254 return -ENOTSUP;
1255 }
1256
1257 kern_mtype = skip_mods_and_typedefs(kern_btf,
1258 kern_mtype->type,
1259 &kern_mtype_id);
1260
1261 /* mtype->type must be a func_proto which was
1262 * guaranteed in bpf_object__collect_st_ops_relos(),
1263 * so only check kern_mtype for func_proto here.
1264 */
1265 if (!btf_is_func_proto(kern_mtype)) {
1266 pr_warn("struct_ops init_kern %s: kernel member %s is not a func ptr\n",
1267 map->name, mname);
1268 return -ENOTSUP;
1269 }
1270
1271 if (mod_btf)
1272 prog->attach_btf_obj_fd = mod_btf->fd;
1273
1274 /* if we haven't yet processed this BPF program, record proper
1275 * attach_btf_id and member_idx
1276 */
1277 if (!prog->attach_btf_id) {
1278 prog->attach_btf_id = kern_type_id;
1279 prog->expected_attach_type = kern_member_idx;
1280 }
1281
1282 /* struct_ops BPF prog can be re-used between multiple
1283 * .struct_ops & .struct_ops.link as long as it's the
1284 * same struct_ops struct definition and the same
1285 * function pointer field
1286 */
1287 if (prog->attach_btf_id != kern_type_id) {
1288 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",
1289 map->name, mname, prog->name, prog->sec_name, prog->type,
1290 prog->attach_btf_id, kern_type_id);
1291 return -EINVAL;
1292 }
1293 if (prog->expected_attach_type != kern_member_idx) {
1294 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",
1295 map->name, mname, prog->name, prog->sec_name, prog->type,
1296 prog->expected_attach_type, kern_member_idx);
1297 return -EINVAL;
1298 }
1299
1300 st_ops->kern_func_off[i] = kern_data_off + kern_moff;
1301
1302 pr_debug("struct_ops init_kern %s: func ptr %s is set to prog %s from data(+%u) to kern_data(+%u)\n",
1303 map->name, mname, prog->name, moff,
1304 kern_moff);
1305
1306 continue;
1307 }
1308
1309 kern_msize = btf__resolve_size(kern_btf, kern_mtype_id);
1310 if (kern_msize < 0 || msize != kern_msize) {
1311 pr_warn("struct_ops init_kern %s: Error in size of member %s: %zd != %zd(kernel)\n",
1312 map->name, mname, (ssize_t)msize,
1313 (ssize_t)kern_msize);
1314 return -ENOTSUP;
1315 }
1316
1317 pr_debug("struct_ops init_kern %s: copy %s %u bytes from data(+%u) to kern_data(+%u)\n",
1318 map->name, mname, (unsigned int)msize,
1319 moff, kern_moff);
1320 memcpy(kern_mdata, mdata, msize);
1321 }
1322
1323 return 0;
1324 }
1325
bpf_object__init_kern_struct_ops_maps(struct bpf_object * obj)1326 static int bpf_object__init_kern_struct_ops_maps(struct bpf_object *obj)
1327 {
1328 struct bpf_map *map;
1329 size_t i;
1330 int err;
1331
1332 for (i = 0; i < obj->nr_maps; i++) {
1333 map = &obj->maps[i];
1334
1335 if (!bpf_map__is_struct_ops(map))
1336 continue;
1337
1338 if (!map->autocreate)
1339 continue;
1340
1341 err = bpf_map__init_kern_struct_ops(map);
1342 if (err)
1343 return err;
1344 }
1345
1346 return 0;
1347 }
1348
init_struct_ops_maps(struct bpf_object * obj,const char * sec_name,int shndx,Elf_Data * data)1349 static int init_struct_ops_maps(struct bpf_object *obj, const char *sec_name,
1350 int shndx, Elf_Data *data)
1351 {
1352 const struct btf_type *type, *datasec;
1353 const struct btf_var_secinfo *vsi;
1354 struct bpf_struct_ops *st_ops;
1355 const char *tname, *var_name;
1356 __s32 type_id, datasec_id;
1357 const struct btf *btf;
1358 struct bpf_map *map;
1359 __u32 i;
1360
1361 if (shndx == -1)
1362 return 0;
1363
1364 btf = obj->btf;
1365 datasec_id = btf__find_by_name_kind(btf, sec_name,
1366 BTF_KIND_DATASEC);
1367 if (datasec_id < 0) {
1368 pr_warn("struct_ops init: DATASEC %s not found\n",
1369 sec_name);
1370 return -EINVAL;
1371 }
1372
1373 datasec = btf__type_by_id(btf, datasec_id);
1374 vsi = btf_var_secinfos(datasec);
1375 for (i = 0; i < btf_vlen(datasec); i++, vsi++) {
1376 type = btf__type_by_id(obj->btf, vsi->type);
1377 var_name = btf__name_by_offset(obj->btf, type->name_off);
1378
1379 type_id = btf__resolve_type(obj->btf, vsi->type);
1380 if (type_id < 0) {
1381 pr_warn("struct_ops init: Cannot resolve var type_id %u in DATASEC %s\n",
1382 vsi->type, sec_name);
1383 return -EINVAL;
1384 }
1385
1386 type = btf__type_by_id(obj->btf, type_id);
1387 tname = btf__name_by_offset(obj->btf, type->name_off);
1388 if (!tname[0]) {
1389 pr_warn("struct_ops init: anonymous type is not supported\n");
1390 return -ENOTSUP;
1391 }
1392 if (!btf_is_struct(type)) {
1393 pr_warn("struct_ops init: %s is not a struct\n", tname);
1394 return -EINVAL;
1395 }
1396
1397 map = bpf_object__add_map(obj);
1398 if (IS_ERR(map))
1399 return PTR_ERR(map);
1400
1401 map->sec_idx = shndx;
1402 map->sec_offset = vsi->offset;
1403 map->name = strdup(var_name);
1404 if (!map->name)
1405 return -ENOMEM;
1406 map->btf_value_type_id = type_id;
1407
1408 /* Follow same convention as for programs autoload:
1409 * SEC("?.struct_ops") means map is not created by default.
1410 */
1411 if (sec_name[0] == '?') {
1412 map->autocreate = false;
1413 /* from now on forget there was ? in section name */
1414 sec_name++;
1415 }
1416
1417 map->def.type = BPF_MAP_TYPE_STRUCT_OPS;
1418 map->def.key_size = sizeof(int);
1419 map->def.value_size = type->size;
1420 map->def.max_entries = 1;
1421 map->def.map_flags = strcmp(sec_name, STRUCT_OPS_LINK_SEC) == 0 ? BPF_F_LINK : 0;
1422 map->autoattach = true;
1423
1424 map->st_ops = calloc(1, sizeof(*map->st_ops));
1425 if (!map->st_ops)
1426 return -ENOMEM;
1427 st_ops = map->st_ops;
1428 st_ops->data = malloc(type->size);
1429 st_ops->progs = calloc(btf_vlen(type), sizeof(*st_ops->progs));
1430 st_ops->kern_func_off = malloc(btf_vlen(type) *
1431 sizeof(*st_ops->kern_func_off));
1432 if (!st_ops->data || !st_ops->progs || !st_ops->kern_func_off)
1433 return -ENOMEM;
1434
1435 if (vsi->offset + type->size > data->d_size) {
1436 pr_warn("struct_ops init: var %s is beyond the end of DATASEC %s\n",
1437 var_name, sec_name);
1438 return -EINVAL;
1439 }
1440
1441 memcpy(st_ops->data,
1442 data->d_buf + vsi->offset,
1443 type->size);
1444 st_ops->type_id = type_id;
1445
1446 pr_debug("struct_ops init: struct %s(type_id=%u) %s found at offset %u\n",
1447 tname, type_id, var_name, vsi->offset);
1448 }
1449
1450 return 0;
1451 }
1452
bpf_object_init_struct_ops(struct bpf_object * obj)1453 static int bpf_object_init_struct_ops(struct bpf_object *obj)
1454 {
1455 const char *sec_name;
1456 int sec_idx, err;
1457
1458 for (sec_idx = 0; sec_idx < obj->efile.sec_cnt; ++sec_idx) {
1459 struct elf_sec_desc *desc = &obj->efile.secs[sec_idx];
1460
1461 if (desc->sec_type != SEC_ST_OPS)
1462 continue;
1463
1464 sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, sec_idx));
1465 if (!sec_name)
1466 return -LIBBPF_ERRNO__FORMAT;
1467
1468 err = init_struct_ops_maps(obj, sec_name, sec_idx, desc->data);
1469 if (err)
1470 return err;
1471 }
1472
1473 return 0;
1474 }
1475
bpf_object__new(const char * path,const void * obj_buf,size_t obj_buf_sz,const char * obj_name)1476 static struct bpf_object *bpf_object__new(const char *path,
1477 const void *obj_buf,
1478 size_t obj_buf_sz,
1479 const char *obj_name)
1480 {
1481 struct bpf_object *obj;
1482 char *end;
1483
1484 obj = calloc(1, sizeof(struct bpf_object) + strlen(path) + 1);
1485 if (!obj) {
1486 pr_warn("alloc memory failed for %s\n", path);
1487 return ERR_PTR(-ENOMEM);
1488 }
1489
1490 strcpy(obj->path, path);
1491 if (obj_name) {
1492 libbpf_strlcpy(obj->name, obj_name, sizeof(obj->name));
1493 } else {
1494 /* Using basename() GNU version which doesn't modify arg. */
1495 libbpf_strlcpy(obj->name, basename((void *)path), sizeof(obj->name));
1496 end = strchr(obj->name, '.');
1497 if (end)
1498 *end = 0;
1499 }
1500
1501 obj->efile.fd = -1;
1502 /*
1503 * Caller of this function should also call
1504 * bpf_object__elf_finish() after data collection to return
1505 * obj_buf to user. If not, we should duplicate the buffer to
1506 * avoid user freeing them before elf finish.
1507 */
1508 obj->efile.obj_buf = obj_buf;
1509 obj->efile.obj_buf_sz = obj_buf_sz;
1510 obj->efile.btf_maps_shndx = -1;
1511 obj->kconfig_map_idx = -1;
1512
1513 obj->kern_version = get_kernel_version();
1514 obj->loaded = false;
1515
1516 return obj;
1517 }
1518
bpf_object__elf_finish(struct bpf_object * obj)1519 static void bpf_object__elf_finish(struct bpf_object *obj)
1520 {
1521 if (!obj->efile.elf)
1522 return;
1523
1524 elf_end(obj->efile.elf);
1525 obj->efile.elf = NULL;
1526 obj->efile.ehdr = NULL;
1527 obj->efile.symbols = NULL;
1528 obj->efile.arena_data = NULL;
1529
1530 zfree(&obj->efile.secs);
1531 obj->efile.sec_cnt = 0;
1532 zclose(obj->efile.fd);
1533 obj->efile.obj_buf = NULL;
1534 obj->efile.obj_buf_sz = 0;
1535 }
1536
bpf_object__elf_init(struct bpf_object * obj)1537 static int bpf_object__elf_init(struct bpf_object *obj)
1538 {
1539 Elf64_Ehdr *ehdr;
1540 int err = 0;
1541 Elf *elf;
1542
1543 if (obj->efile.elf) {
1544 pr_warn("elf: init internal error\n");
1545 return -LIBBPF_ERRNO__LIBELF;
1546 }
1547
1548 if (obj->efile.obj_buf_sz > 0) {
1549 /* obj_buf should have been validated by bpf_object__open_mem(). */
1550 elf = elf_memory((char *)obj->efile.obj_buf, obj->efile.obj_buf_sz);
1551 } else {
1552 obj->efile.fd = open(obj->path, O_RDONLY | O_CLOEXEC);
1553 if (obj->efile.fd < 0) {
1554 err = -errno;
1555 pr_warn("elf: failed to open %s: %s\n", obj->path, errstr(err));
1556 return err;
1557 }
1558
1559 elf = elf_begin(obj->efile.fd, ELF_C_READ_MMAP, NULL);
1560 }
1561
1562 if (!elf) {
1563 pr_warn("elf: failed to open %s as ELF file: %s\n", obj->path, elf_errmsg(-1));
1564 err = -LIBBPF_ERRNO__LIBELF;
1565 goto errout;
1566 }
1567
1568 obj->efile.elf = elf;
1569
1570 if (elf_kind(elf) != ELF_K_ELF) {
1571 err = -LIBBPF_ERRNO__FORMAT;
1572 pr_warn("elf: '%s' is not a proper ELF object\n", obj->path);
1573 goto errout;
1574 }
1575
1576 if (gelf_getclass(elf) != ELFCLASS64) {
1577 err = -LIBBPF_ERRNO__FORMAT;
1578 pr_warn("elf: '%s' is not a 64-bit ELF object\n", obj->path);
1579 goto errout;
1580 }
1581
1582 obj->efile.ehdr = ehdr = elf64_getehdr(elf);
1583 if (!obj->efile.ehdr) {
1584 pr_warn("elf: failed to get ELF header from %s: %s\n", obj->path, elf_errmsg(-1));
1585 err = -LIBBPF_ERRNO__FORMAT;
1586 goto errout;
1587 }
1588
1589 /* Validate ELF object endianness... */
1590 if (ehdr->e_ident[EI_DATA] != ELFDATA2LSB &&
1591 ehdr->e_ident[EI_DATA] != ELFDATA2MSB) {
1592 err = -LIBBPF_ERRNO__ENDIAN;
1593 pr_warn("elf: '%s' has unknown byte order\n", obj->path);
1594 goto errout;
1595 }
1596 /* and save after bpf_object_open() frees ELF data */
1597 obj->byteorder = ehdr->e_ident[EI_DATA];
1598
1599 if (elf_getshdrstrndx(elf, &obj->efile.shstrndx)) {
1600 pr_warn("elf: failed to get section names section index for %s: %s\n",
1601 obj->path, elf_errmsg(-1));
1602 err = -LIBBPF_ERRNO__FORMAT;
1603 goto errout;
1604 }
1605
1606 /* ELF is corrupted/truncated, avoid calling elf_strptr. */
1607 if (!elf_rawdata(elf_getscn(elf, obj->efile.shstrndx), NULL)) {
1608 pr_warn("elf: failed to get section names strings from %s: %s\n",
1609 obj->path, elf_errmsg(-1));
1610 err = -LIBBPF_ERRNO__FORMAT;
1611 goto errout;
1612 }
1613
1614 /* Old LLVM set e_machine to EM_NONE */
1615 if (ehdr->e_type != ET_REL || (ehdr->e_machine && ehdr->e_machine != EM_BPF)) {
1616 pr_warn("elf: %s is not a valid eBPF object file\n", obj->path);
1617 err = -LIBBPF_ERRNO__FORMAT;
1618 goto errout;
1619 }
1620
1621 return 0;
1622 errout:
1623 bpf_object__elf_finish(obj);
1624 return err;
1625 }
1626
is_native_endianness(struct bpf_object * obj)1627 static bool is_native_endianness(struct bpf_object *obj)
1628 {
1629 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
1630 return obj->byteorder == ELFDATA2LSB;
1631 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
1632 return obj->byteorder == ELFDATA2MSB;
1633 #else
1634 # error "Unrecognized __BYTE_ORDER__"
1635 #endif
1636 }
1637
1638 static int
bpf_object__init_license(struct bpf_object * obj,void * data,size_t size)1639 bpf_object__init_license(struct bpf_object *obj, void *data, size_t size)
1640 {
1641 if (!data) {
1642 pr_warn("invalid license section in %s\n", obj->path);
1643 return -LIBBPF_ERRNO__FORMAT;
1644 }
1645 /* libbpf_strlcpy() only copies first N - 1 bytes, so size + 1 won't
1646 * go over allowed ELF data section buffer
1647 */
1648 libbpf_strlcpy(obj->license, data, min(size + 1, sizeof(obj->license)));
1649 pr_debug("license of %s is %s\n", obj->path, obj->license);
1650 return 0;
1651 }
1652
1653 static int
bpf_object__init_kversion(struct bpf_object * obj,void * data,size_t size)1654 bpf_object__init_kversion(struct bpf_object *obj, void *data, size_t size)
1655 {
1656 __u32 kver;
1657
1658 if (!data || size != sizeof(kver)) {
1659 pr_warn("invalid kver section in %s\n", obj->path);
1660 return -LIBBPF_ERRNO__FORMAT;
1661 }
1662 memcpy(&kver, data, sizeof(kver));
1663 obj->kern_version = kver;
1664 pr_debug("kernel version of %s is %x\n", obj->path, obj->kern_version);
1665 return 0;
1666 }
1667
bpf_map_type__is_map_in_map(enum bpf_map_type type)1668 static bool bpf_map_type__is_map_in_map(enum bpf_map_type type)
1669 {
1670 if (type == BPF_MAP_TYPE_ARRAY_OF_MAPS ||
1671 type == BPF_MAP_TYPE_HASH_OF_MAPS)
1672 return true;
1673 return false;
1674 }
1675
find_elf_sec_sz(const struct bpf_object * obj,const char * name,__u32 * size)1676 static int find_elf_sec_sz(const struct bpf_object *obj, const char *name, __u32 *size)
1677 {
1678 Elf_Data *data;
1679 Elf_Scn *scn;
1680
1681 if (!name)
1682 return -EINVAL;
1683
1684 scn = elf_sec_by_name(obj, name);
1685 data = elf_sec_data(obj, scn);
1686 if (data) {
1687 *size = data->d_size;
1688 return 0; /* found it */
1689 }
1690
1691 return -ENOENT;
1692 }
1693
find_elf_var_sym(const struct bpf_object * obj,const char * name)1694 static Elf64_Sym *find_elf_var_sym(const struct bpf_object *obj, const char *name)
1695 {
1696 Elf_Data *symbols = obj->efile.symbols;
1697 const char *sname;
1698 size_t si;
1699
1700 for (si = 0; si < symbols->d_size / sizeof(Elf64_Sym); si++) {
1701 Elf64_Sym *sym = elf_sym_by_idx(obj, si);
1702
1703 if (ELF64_ST_TYPE(sym->st_info) != STT_OBJECT)
1704 continue;
1705
1706 if (ELF64_ST_BIND(sym->st_info) != STB_GLOBAL &&
1707 ELF64_ST_BIND(sym->st_info) != STB_WEAK)
1708 continue;
1709
1710 sname = elf_sym_str(obj, sym->st_name);
1711 if (!sname) {
1712 pr_warn("failed to get sym name string for var %s\n", name);
1713 return ERR_PTR(-EIO);
1714 }
1715 if (strcmp(name, sname) == 0)
1716 return sym;
1717 }
1718
1719 return ERR_PTR(-ENOENT);
1720 }
1721
1722 /* Some versions of Android don't provide memfd_create() in their libc
1723 * implementation, so avoid complications and just go straight to Linux
1724 * syscall.
1725 */
sys_memfd_create(const char * name,unsigned flags)1726 static int sys_memfd_create(const char *name, unsigned flags)
1727 {
1728 return syscall(__NR_memfd_create, name, flags);
1729 }
1730
1731 #ifndef MFD_CLOEXEC
1732 #define MFD_CLOEXEC 0x0001U
1733 #endif
1734
create_placeholder_fd(void)1735 static int create_placeholder_fd(void)
1736 {
1737 int fd;
1738
1739 fd = ensure_good_fd(sys_memfd_create("libbpf-placeholder-fd", MFD_CLOEXEC));
1740 if (fd < 0)
1741 return -errno;
1742 return fd;
1743 }
1744
bpf_object__add_map(struct bpf_object * obj)1745 static struct bpf_map *bpf_object__add_map(struct bpf_object *obj)
1746 {
1747 struct bpf_map *map;
1748 int err;
1749
1750 err = libbpf_ensure_mem((void **)&obj->maps, &obj->maps_cap,
1751 sizeof(*obj->maps), obj->nr_maps + 1);
1752 if (err)
1753 return ERR_PTR(err);
1754
1755 map = &obj->maps[obj->nr_maps++];
1756 map->obj = obj;
1757 /* Preallocate map FD without actually creating BPF map just yet.
1758 * These map FD "placeholders" will be reused later without changing
1759 * FD value when map is actually created in the kernel.
1760 *
1761 * This is useful to be able to perform BPF program relocations
1762 * without having to create BPF maps before that step. This allows us
1763 * to finalize and load BTF very late in BPF object's loading phase,
1764 * right before BPF maps have to be created and BPF programs have to
1765 * be loaded. By having these map FD placeholders we can perform all
1766 * the sanitizations, relocations, and any other adjustments before we
1767 * start creating actual BPF kernel objects (BTF, maps, progs).
1768 */
1769 map->fd = create_placeholder_fd();
1770 if (map->fd < 0)
1771 return ERR_PTR(map->fd);
1772 map->inner_map_fd = -1;
1773 map->autocreate = true;
1774
1775 return map;
1776 }
1777
array_map_mmap_sz(unsigned int value_sz,unsigned int max_entries)1778 static size_t array_map_mmap_sz(unsigned int value_sz, unsigned int max_entries)
1779 {
1780 const long page_sz = sysconf(_SC_PAGE_SIZE);
1781 size_t map_sz;
1782
1783 map_sz = (size_t)roundup(value_sz, 8) * max_entries;
1784 map_sz = roundup(map_sz, page_sz);
1785 return map_sz;
1786 }
1787
bpf_map_mmap_sz(const struct bpf_map * map)1788 static size_t bpf_map_mmap_sz(const struct bpf_map *map)
1789 {
1790 const long page_sz = sysconf(_SC_PAGE_SIZE);
1791
1792 switch (map->def.type) {
1793 case BPF_MAP_TYPE_ARRAY:
1794 return array_map_mmap_sz(map->def.value_size, map->def.max_entries);
1795 case BPF_MAP_TYPE_ARENA:
1796 return page_sz * map->def.max_entries;
1797 default:
1798 return 0; /* not supported */
1799 }
1800 }
1801
bpf_map_mmap_resize(struct bpf_map * map,size_t old_sz,size_t new_sz)1802 static int bpf_map_mmap_resize(struct bpf_map *map, size_t old_sz, size_t new_sz)
1803 {
1804 void *mmaped;
1805
1806 if (!map->mmaped)
1807 return -EINVAL;
1808
1809 if (old_sz == new_sz)
1810 return 0;
1811
1812 mmaped = mmap(NULL, new_sz, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
1813 if (mmaped == MAP_FAILED)
1814 return -errno;
1815
1816 memcpy(mmaped, map->mmaped, min(old_sz, new_sz));
1817 munmap(map->mmaped, old_sz);
1818 map->mmaped = mmaped;
1819 return 0;
1820 }
1821
internal_map_name(struct bpf_object * obj,const char * real_name)1822 static char *internal_map_name(struct bpf_object *obj, const char *real_name)
1823 {
1824 char map_name[BPF_OBJ_NAME_LEN], *p;
1825 int pfx_len, sfx_len = max((size_t)7, strlen(real_name));
1826
1827 /* This is one of the more confusing parts of libbpf for various
1828 * reasons, some of which are historical. The original idea for naming
1829 * internal names was to include as much of BPF object name prefix as
1830 * possible, so that it can be distinguished from similar internal
1831 * maps of a different BPF object.
1832 * As an example, let's say we have bpf_object named 'my_object_name'
1833 * and internal map corresponding to '.rodata' ELF section. The final
1834 * map name advertised to user and to the kernel will be
1835 * 'my_objec.rodata', taking first 8 characters of object name and
1836 * entire 7 characters of '.rodata'.
1837 * Somewhat confusingly, if internal map ELF section name is shorter
1838 * than 7 characters, e.g., '.bss', we still reserve 7 characters
1839 * for the suffix, even though we only have 4 actual characters, and
1840 * resulting map will be called 'my_objec.bss', not even using all 15
1841 * characters allowed by the kernel. Oh well, at least the truncated
1842 * object name is somewhat consistent in this case. But if the map
1843 * name is '.kconfig', we'll still have entirety of '.kconfig' added
1844 * (8 chars) and thus will be left with only first 7 characters of the
1845 * object name ('my_obje'). Happy guessing, user, that the final map
1846 * name will be "my_obje.kconfig".
1847 * Now, with libbpf starting to support arbitrarily named .rodata.*
1848 * and .data.* data sections, it's possible that ELF section name is
1849 * longer than allowed 15 chars, so we now need to be careful to take
1850 * only up to 15 first characters of ELF name, taking no BPF object
1851 * name characters at all. So '.rodata.abracadabra' will result in
1852 * '.rodata.abracad' kernel and user-visible name.
1853 * We need to keep this convoluted logic intact for .data, .bss and
1854 * .rodata maps, but for new custom .data.custom and .rodata.custom
1855 * maps we use their ELF names as is, not prepending bpf_object name
1856 * in front. We still need to truncate them to 15 characters for the
1857 * kernel. Full name can be recovered for such maps by using DATASEC
1858 * BTF type associated with such map's value type, though.
1859 */
1860 if (sfx_len >= BPF_OBJ_NAME_LEN)
1861 sfx_len = BPF_OBJ_NAME_LEN - 1;
1862
1863 /* if there are two or more dots in map name, it's a custom dot map */
1864 if (strchr(real_name + 1, '.') != NULL)
1865 pfx_len = 0;
1866 else
1867 pfx_len = min((size_t)BPF_OBJ_NAME_LEN - sfx_len - 1, strlen(obj->name));
1868
1869 snprintf(map_name, sizeof(map_name), "%.*s%.*s", pfx_len, obj->name,
1870 sfx_len, real_name);
1871
1872 /* sanities map name to characters allowed by kernel */
1873 for (p = map_name; *p && p < map_name + sizeof(map_name); p++)
1874 if (!isalnum(*p) && *p != '_' && *p != '.')
1875 *p = '_';
1876
1877 return strdup(map_name);
1878 }
1879
1880 static int
1881 map_fill_btf_type_info(struct bpf_object *obj, struct bpf_map *map);
1882
1883 /* Internal BPF map is mmap()'able only if at least one of corresponding
1884 * DATASEC's VARs are to be exposed through BPF skeleton. I.e., it's a GLOBAL
1885 * variable and it's not marked as __hidden (which turns it into, effectively,
1886 * a STATIC variable).
1887 */
map_is_mmapable(struct bpf_object * obj,struct bpf_map * map)1888 static bool map_is_mmapable(struct bpf_object *obj, struct bpf_map *map)
1889 {
1890 const struct btf_type *t, *vt;
1891 struct btf_var_secinfo *vsi;
1892 int i, n;
1893
1894 if (!map->btf_value_type_id)
1895 return false;
1896
1897 t = btf__type_by_id(obj->btf, map->btf_value_type_id);
1898 if (!btf_is_datasec(t))
1899 return false;
1900
1901 vsi = btf_var_secinfos(t);
1902 for (i = 0, n = btf_vlen(t); i < n; i++, vsi++) {
1903 vt = btf__type_by_id(obj->btf, vsi->type);
1904 if (!btf_is_var(vt))
1905 continue;
1906
1907 if (btf_var(vt)->linkage != BTF_VAR_STATIC)
1908 return true;
1909 }
1910
1911 return false;
1912 }
1913
1914 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)1915 bpf_object__init_internal_map(struct bpf_object *obj, enum libbpf_map_type type,
1916 const char *real_name, int sec_idx, void *data, size_t data_sz)
1917 {
1918 struct bpf_map_def *def;
1919 struct bpf_map *map;
1920 size_t mmap_sz;
1921 int err;
1922
1923 map = bpf_object__add_map(obj);
1924 if (IS_ERR(map))
1925 return PTR_ERR(map);
1926
1927 map->libbpf_type = type;
1928 map->sec_idx = sec_idx;
1929 map->sec_offset = 0;
1930 map->real_name = strdup(real_name);
1931 map->name = internal_map_name(obj, real_name);
1932 if (!map->real_name || !map->name) {
1933 zfree(&map->real_name);
1934 zfree(&map->name);
1935 return -ENOMEM;
1936 }
1937
1938 def = &map->def;
1939 def->type = BPF_MAP_TYPE_ARRAY;
1940 def->key_size = sizeof(int);
1941 def->value_size = data_sz;
1942 def->max_entries = 1;
1943 def->map_flags = type == LIBBPF_MAP_RODATA || type == LIBBPF_MAP_KCONFIG
1944 ? BPF_F_RDONLY_PROG : 0;
1945
1946 /* failures are fine because of maps like .rodata.str1.1 */
1947 (void) map_fill_btf_type_info(obj, map);
1948
1949 if (map_is_mmapable(obj, map))
1950 def->map_flags |= BPF_F_MMAPABLE;
1951
1952 pr_debug("map '%s' (global data): at sec_idx %d, offset %zu, flags %x.\n",
1953 map->name, map->sec_idx, map->sec_offset, def->map_flags);
1954
1955 mmap_sz = bpf_map_mmap_sz(map);
1956 map->mmaped = mmap(NULL, mmap_sz, PROT_READ | PROT_WRITE,
1957 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
1958 if (map->mmaped == MAP_FAILED) {
1959 err = -errno;
1960 map->mmaped = NULL;
1961 pr_warn("failed to alloc map '%s' content buffer: %s\n", map->name, errstr(err));
1962 zfree(&map->real_name);
1963 zfree(&map->name);
1964 return err;
1965 }
1966
1967 if (data)
1968 memcpy(map->mmaped, data, data_sz);
1969
1970 pr_debug("map %td is \"%s\"\n", map - obj->maps, map->name);
1971 return 0;
1972 }
1973
bpf_object__init_global_data_maps(struct bpf_object * obj)1974 static int bpf_object__init_global_data_maps(struct bpf_object *obj)
1975 {
1976 struct elf_sec_desc *sec_desc;
1977 const char *sec_name;
1978 int err = 0, sec_idx;
1979
1980 /*
1981 * Populate obj->maps with libbpf internal maps.
1982 */
1983 for (sec_idx = 1; sec_idx < obj->efile.sec_cnt; sec_idx++) {
1984 sec_desc = &obj->efile.secs[sec_idx];
1985
1986 /* Skip recognized sections with size 0. */
1987 if (!sec_desc->data || sec_desc->data->d_size == 0)
1988 continue;
1989
1990 switch (sec_desc->sec_type) {
1991 case SEC_DATA:
1992 sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, sec_idx));
1993 err = bpf_object__init_internal_map(obj, LIBBPF_MAP_DATA,
1994 sec_name, sec_idx,
1995 sec_desc->data->d_buf,
1996 sec_desc->data->d_size);
1997 break;
1998 case SEC_RODATA:
1999 obj->has_rodata = true;
2000 sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, sec_idx));
2001 err = bpf_object__init_internal_map(obj, LIBBPF_MAP_RODATA,
2002 sec_name, sec_idx,
2003 sec_desc->data->d_buf,
2004 sec_desc->data->d_size);
2005 break;
2006 case SEC_BSS:
2007 sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, sec_idx));
2008 err = bpf_object__init_internal_map(obj, LIBBPF_MAP_BSS,
2009 sec_name, sec_idx,
2010 NULL,
2011 sec_desc->data->d_size);
2012 break;
2013 default:
2014 /* skip */
2015 break;
2016 }
2017 if (err)
2018 return err;
2019 }
2020 return 0;
2021 }
2022
2023
find_extern_by_name(const struct bpf_object * obj,const void * name)2024 static struct extern_desc *find_extern_by_name(const struct bpf_object *obj,
2025 const void *name)
2026 {
2027 int i;
2028
2029 for (i = 0; i < obj->nr_extern; i++) {
2030 if (strcmp(obj->externs[i].name, name) == 0)
2031 return &obj->externs[i];
2032 }
2033 return NULL;
2034 }
2035
find_extern_by_name_with_len(const struct bpf_object * obj,const void * name,int len)2036 static struct extern_desc *find_extern_by_name_with_len(const struct bpf_object *obj,
2037 const void *name, int len)
2038 {
2039 const char *ext_name;
2040 int i;
2041
2042 for (i = 0; i < obj->nr_extern; i++) {
2043 ext_name = obj->externs[i].name;
2044 if (strlen(ext_name) == len && strncmp(ext_name, name, len) == 0)
2045 return &obj->externs[i];
2046 }
2047 return NULL;
2048 }
2049
set_kcfg_value_tri(struct extern_desc * ext,void * ext_val,char value)2050 static int set_kcfg_value_tri(struct extern_desc *ext, void *ext_val,
2051 char value)
2052 {
2053 switch (ext->kcfg.type) {
2054 case KCFG_BOOL:
2055 if (value == 'm') {
2056 pr_warn("extern (kcfg) '%s': value '%c' implies tristate or char type\n",
2057 ext->name, value);
2058 return -EINVAL;
2059 }
2060 *(bool *)ext_val = value == 'y' ? true : false;
2061 break;
2062 case KCFG_TRISTATE:
2063 if (value == 'y')
2064 *(enum libbpf_tristate *)ext_val = TRI_YES;
2065 else if (value == 'm')
2066 *(enum libbpf_tristate *)ext_val = TRI_MODULE;
2067 else /* value == 'n' */
2068 *(enum libbpf_tristate *)ext_val = TRI_NO;
2069 break;
2070 case KCFG_CHAR:
2071 *(char *)ext_val = value;
2072 break;
2073 case KCFG_UNKNOWN:
2074 case KCFG_INT:
2075 case KCFG_CHAR_ARR:
2076 default:
2077 pr_warn("extern (kcfg) '%s': value '%c' implies bool, tristate, or char type\n",
2078 ext->name, value);
2079 return -EINVAL;
2080 }
2081 ext->is_set = true;
2082 return 0;
2083 }
2084
set_kcfg_value_str(struct extern_desc * ext,char * ext_val,const char * value)2085 static int set_kcfg_value_str(struct extern_desc *ext, char *ext_val,
2086 const char *value)
2087 {
2088 size_t len;
2089
2090 if (ext->kcfg.type != KCFG_CHAR_ARR) {
2091 pr_warn("extern (kcfg) '%s': value '%s' implies char array type\n",
2092 ext->name, value);
2093 return -EINVAL;
2094 }
2095
2096 len = strlen(value);
2097 if (value[len - 1] != '"') {
2098 pr_warn("extern (kcfg) '%s': invalid string config '%s'\n",
2099 ext->name, value);
2100 return -EINVAL;
2101 }
2102
2103 /* strip quotes */
2104 len -= 2;
2105 if (len >= ext->kcfg.sz) {
2106 pr_warn("extern (kcfg) '%s': long string '%s' of (%zu bytes) truncated to %d bytes\n",
2107 ext->name, value, len, ext->kcfg.sz - 1);
2108 len = ext->kcfg.sz - 1;
2109 }
2110 memcpy(ext_val, value + 1, len);
2111 ext_val[len] = '\0';
2112 ext->is_set = true;
2113 return 0;
2114 }
2115
parse_u64(const char * value,__u64 * res)2116 static int parse_u64(const char *value, __u64 *res)
2117 {
2118 char *value_end;
2119 int err;
2120
2121 errno = 0;
2122 *res = strtoull(value, &value_end, 0);
2123 if (errno) {
2124 err = -errno;
2125 pr_warn("failed to parse '%s': %s\n", value, errstr(err));
2126 return err;
2127 }
2128 if (*value_end) {
2129 pr_warn("failed to parse '%s' as integer completely\n", value);
2130 return -EINVAL;
2131 }
2132 return 0;
2133 }
2134
is_kcfg_value_in_range(const struct extern_desc * ext,__u64 v)2135 static bool is_kcfg_value_in_range(const struct extern_desc *ext, __u64 v)
2136 {
2137 int bit_sz = ext->kcfg.sz * 8;
2138
2139 if (ext->kcfg.sz == 8)
2140 return true;
2141
2142 /* Validate that value stored in u64 fits in integer of `ext->sz`
2143 * bytes size without any loss of information. If the target integer
2144 * is signed, we rely on the following limits of integer type of
2145 * Y bits and subsequent transformation:
2146 *
2147 * -2^(Y-1) <= X <= 2^(Y-1) - 1
2148 * 0 <= X + 2^(Y-1) <= 2^Y - 1
2149 * 0 <= X + 2^(Y-1) < 2^Y
2150 *
2151 * For unsigned target integer, check that all the (64 - Y) bits are
2152 * zero.
2153 */
2154 if (ext->kcfg.is_signed)
2155 return v + (1ULL << (bit_sz - 1)) < (1ULL << bit_sz);
2156 else
2157 return (v >> bit_sz) == 0;
2158 }
2159
set_kcfg_value_num(struct extern_desc * ext,void * ext_val,__u64 value)2160 static int set_kcfg_value_num(struct extern_desc *ext, void *ext_val,
2161 __u64 value)
2162 {
2163 if (ext->kcfg.type != KCFG_INT && ext->kcfg.type != KCFG_CHAR &&
2164 ext->kcfg.type != KCFG_BOOL) {
2165 pr_warn("extern (kcfg) '%s': value '%llu' implies integer, char, or boolean type\n",
2166 ext->name, (unsigned long long)value);
2167 return -EINVAL;
2168 }
2169 if (ext->kcfg.type == KCFG_BOOL && value > 1) {
2170 pr_warn("extern (kcfg) '%s': value '%llu' isn't boolean compatible\n",
2171 ext->name, (unsigned long long)value);
2172 return -EINVAL;
2173
2174 }
2175 if (!is_kcfg_value_in_range(ext, value)) {
2176 pr_warn("extern (kcfg) '%s': value '%llu' doesn't fit in %d bytes\n",
2177 ext->name, (unsigned long long)value, ext->kcfg.sz);
2178 return -ERANGE;
2179 }
2180 switch (ext->kcfg.sz) {
2181 case 1:
2182 *(__u8 *)ext_val = value;
2183 break;
2184 case 2:
2185 *(__u16 *)ext_val = value;
2186 break;
2187 case 4:
2188 *(__u32 *)ext_val = value;
2189 break;
2190 case 8:
2191 *(__u64 *)ext_val = value;
2192 break;
2193 default:
2194 return -EINVAL;
2195 }
2196 ext->is_set = true;
2197 return 0;
2198 }
2199
bpf_object__process_kconfig_line(struct bpf_object * obj,char * buf,void * data)2200 static int bpf_object__process_kconfig_line(struct bpf_object *obj,
2201 char *buf, void *data)
2202 {
2203 struct extern_desc *ext;
2204 char *sep, *value;
2205 int len, err = 0;
2206 void *ext_val;
2207 __u64 num;
2208
2209 if (!str_has_pfx(buf, "CONFIG_"))
2210 return 0;
2211
2212 sep = strchr(buf, '=');
2213 if (!sep) {
2214 pr_warn("failed to parse '%s': no separator\n", buf);
2215 return -EINVAL;
2216 }
2217
2218 /* Trim ending '\n' */
2219 len = strlen(buf);
2220 if (buf[len - 1] == '\n')
2221 buf[len - 1] = '\0';
2222 /* Split on '=' and ensure that a value is present. */
2223 *sep = '\0';
2224 if (!sep[1]) {
2225 *sep = '=';
2226 pr_warn("failed to parse '%s': no value\n", buf);
2227 return -EINVAL;
2228 }
2229
2230 ext = find_extern_by_name(obj, buf);
2231 if (!ext || ext->is_set)
2232 return 0;
2233
2234 ext_val = data + ext->kcfg.data_off;
2235 value = sep + 1;
2236
2237 switch (*value) {
2238 case 'y': case 'n': case 'm':
2239 err = set_kcfg_value_tri(ext, ext_val, *value);
2240 break;
2241 case '"':
2242 err = set_kcfg_value_str(ext, ext_val, value);
2243 break;
2244 default:
2245 /* assume integer */
2246 err = parse_u64(value, &num);
2247 if (err) {
2248 pr_warn("extern (kcfg) '%s': value '%s' isn't a valid integer\n", ext->name, value);
2249 return err;
2250 }
2251 if (ext->kcfg.type != KCFG_INT && ext->kcfg.type != KCFG_CHAR) {
2252 pr_warn("extern (kcfg) '%s': value '%s' implies integer type\n", ext->name, value);
2253 return -EINVAL;
2254 }
2255 err = set_kcfg_value_num(ext, ext_val, num);
2256 break;
2257 }
2258 if (err)
2259 return err;
2260 pr_debug("extern (kcfg) '%s': set to %s\n", ext->name, value);
2261 return 0;
2262 }
2263
bpf_object__read_kconfig_file(struct bpf_object * obj,void * data)2264 static int bpf_object__read_kconfig_file(struct bpf_object *obj, void *data)
2265 {
2266 char buf[PATH_MAX];
2267 struct utsname uts;
2268 int len, err = 0;
2269 gzFile file;
2270
2271 uname(&uts);
2272 len = snprintf(buf, PATH_MAX, "/boot/config-%s", uts.release);
2273 if (len < 0)
2274 return -EINVAL;
2275 else if (len >= PATH_MAX)
2276 return -ENAMETOOLONG;
2277
2278 /* gzopen also accepts uncompressed files. */
2279 file = gzopen(buf, "re");
2280 if (!file)
2281 file = gzopen("/proc/config.gz", "re");
2282
2283 if (!file) {
2284 pr_warn("failed to open system Kconfig\n");
2285 return -ENOENT;
2286 }
2287
2288 while (gzgets(file, buf, sizeof(buf))) {
2289 err = bpf_object__process_kconfig_line(obj, buf, data);
2290 if (err) {
2291 pr_warn("error parsing system Kconfig line '%s': %s\n",
2292 buf, errstr(err));
2293 goto out;
2294 }
2295 }
2296
2297 out:
2298 gzclose(file);
2299 return err;
2300 }
2301
bpf_object__read_kconfig_mem(struct bpf_object * obj,const char * config,void * data)2302 static int bpf_object__read_kconfig_mem(struct bpf_object *obj,
2303 const char *config, void *data)
2304 {
2305 char buf[PATH_MAX];
2306 int err = 0;
2307 FILE *file;
2308
2309 file = fmemopen((void *)config, strlen(config), "r");
2310 if (!file) {
2311 err = -errno;
2312 pr_warn("failed to open in-memory Kconfig: %s\n", errstr(err));
2313 return err;
2314 }
2315
2316 while (fgets(buf, sizeof(buf), file)) {
2317 err = bpf_object__process_kconfig_line(obj, buf, data);
2318 if (err) {
2319 pr_warn("error parsing in-memory Kconfig line '%s': %s\n",
2320 buf, errstr(err));
2321 break;
2322 }
2323 }
2324
2325 fclose(file);
2326 return err;
2327 }
2328
bpf_object__init_kconfig_map(struct bpf_object * obj)2329 static int bpf_object__init_kconfig_map(struct bpf_object *obj)
2330 {
2331 struct extern_desc *last_ext = NULL, *ext;
2332 size_t map_sz;
2333 int i, err;
2334
2335 for (i = 0; i < obj->nr_extern; i++) {
2336 ext = &obj->externs[i];
2337 if (ext->type == EXT_KCFG)
2338 last_ext = ext;
2339 }
2340
2341 if (!last_ext)
2342 return 0;
2343
2344 map_sz = last_ext->kcfg.data_off + last_ext->kcfg.sz;
2345 err = bpf_object__init_internal_map(obj, LIBBPF_MAP_KCONFIG,
2346 ".kconfig", obj->efile.symbols_shndx,
2347 NULL, map_sz);
2348 if (err)
2349 return err;
2350
2351 obj->kconfig_map_idx = obj->nr_maps - 1;
2352
2353 return 0;
2354 }
2355
2356 const struct btf_type *
skip_mods_and_typedefs(const struct btf * btf,__u32 id,__u32 * res_id)2357 skip_mods_and_typedefs(const struct btf *btf, __u32 id, __u32 *res_id)
2358 {
2359 const struct btf_type *t = btf__type_by_id(btf, id);
2360
2361 if (res_id)
2362 *res_id = id;
2363
2364 while (btf_is_mod(t) || btf_is_typedef(t)) {
2365 if (res_id)
2366 *res_id = t->type;
2367 t = btf__type_by_id(btf, t->type);
2368 }
2369
2370 return t;
2371 }
2372
2373 static const struct btf_type *
resolve_func_ptr(const struct btf * btf,__u32 id,__u32 * res_id)2374 resolve_func_ptr(const struct btf *btf, __u32 id, __u32 *res_id)
2375 {
2376 const struct btf_type *t;
2377
2378 t = skip_mods_and_typedefs(btf, id, NULL);
2379 if (!btf_is_ptr(t))
2380 return NULL;
2381
2382 t = skip_mods_and_typedefs(btf, t->type, res_id);
2383
2384 return btf_is_func_proto(t) ? t : NULL;
2385 }
2386
__btf_kind_str(__u16 kind)2387 static const char *__btf_kind_str(__u16 kind)
2388 {
2389 switch (kind) {
2390 case BTF_KIND_UNKN: return "void";
2391 case BTF_KIND_INT: return "int";
2392 case BTF_KIND_PTR: return "ptr";
2393 case BTF_KIND_ARRAY: return "array";
2394 case BTF_KIND_STRUCT: return "struct";
2395 case BTF_KIND_UNION: return "union";
2396 case BTF_KIND_ENUM: return "enum";
2397 case BTF_KIND_FWD: return "fwd";
2398 case BTF_KIND_TYPEDEF: return "typedef";
2399 case BTF_KIND_VOLATILE: return "volatile";
2400 case BTF_KIND_CONST: return "const";
2401 case BTF_KIND_RESTRICT: return "restrict";
2402 case BTF_KIND_FUNC: return "func";
2403 case BTF_KIND_FUNC_PROTO: return "func_proto";
2404 case BTF_KIND_VAR: return "var";
2405 case BTF_KIND_DATASEC: return "datasec";
2406 case BTF_KIND_FLOAT: return "float";
2407 case BTF_KIND_DECL_TAG: return "decl_tag";
2408 case BTF_KIND_TYPE_TAG: return "type_tag";
2409 case BTF_KIND_ENUM64: return "enum64";
2410 default: return "unknown";
2411 }
2412 }
2413
btf_kind_str(const struct btf_type * t)2414 const char *btf_kind_str(const struct btf_type *t)
2415 {
2416 return __btf_kind_str(btf_kind(t));
2417 }
2418
2419 /*
2420 * Fetch integer attribute of BTF map definition. Such attributes are
2421 * represented using a pointer to an array, in which dimensionality of array
2422 * encodes specified integer value. E.g., int (*type)[BPF_MAP_TYPE_ARRAY];
2423 * encodes `type => BPF_MAP_TYPE_ARRAY` key/value pair completely using BTF
2424 * type definition, while using only sizeof(void *) space in ELF data section.
2425 */
get_map_field_int(const char * map_name,const struct btf * btf,const struct btf_member * m,__u32 * res)2426 static bool get_map_field_int(const char *map_name, const struct btf *btf,
2427 const struct btf_member *m, __u32 *res)
2428 {
2429 const struct btf_type *t = skip_mods_and_typedefs(btf, m->type, NULL);
2430 const char *name = btf__name_by_offset(btf, m->name_off);
2431 const struct btf_array *arr_info;
2432 const struct btf_type *arr_t;
2433
2434 if (!btf_is_ptr(t)) {
2435 pr_warn("map '%s': attr '%s': expected PTR, got %s.\n",
2436 map_name, name, btf_kind_str(t));
2437 return false;
2438 }
2439
2440 arr_t = btf__type_by_id(btf, t->type);
2441 if (!arr_t) {
2442 pr_warn("map '%s': attr '%s': type [%u] not found.\n",
2443 map_name, name, t->type);
2444 return false;
2445 }
2446 if (!btf_is_array(arr_t)) {
2447 pr_warn("map '%s': attr '%s': expected ARRAY, got %s.\n",
2448 map_name, name, btf_kind_str(arr_t));
2449 return false;
2450 }
2451 arr_info = btf_array(arr_t);
2452 *res = arr_info->nelems;
2453 return true;
2454 }
2455
get_map_field_long(const char * map_name,const struct btf * btf,const struct btf_member * m,__u64 * res)2456 static bool get_map_field_long(const char *map_name, const struct btf *btf,
2457 const struct btf_member *m, __u64 *res)
2458 {
2459 const struct btf_type *t = skip_mods_and_typedefs(btf, m->type, NULL);
2460 const char *name = btf__name_by_offset(btf, m->name_off);
2461
2462 if (btf_is_ptr(t)) {
2463 __u32 res32;
2464 bool ret;
2465
2466 ret = get_map_field_int(map_name, btf, m, &res32);
2467 if (ret)
2468 *res = (__u64)res32;
2469 return ret;
2470 }
2471
2472 if (!btf_is_enum(t) && !btf_is_enum64(t)) {
2473 pr_warn("map '%s': attr '%s': expected ENUM or ENUM64, got %s.\n",
2474 map_name, name, btf_kind_str(t));
2475 return false;
2476 }
2477
2478 if (btf_vlen(t) != 1) {
2479 pr_warn("map '%s': attr '%s': invalid __ulong\n",
2480 map_name, name);
2481 return false;
2482 }
2483
2484 if (btf_is_enum(t)) {
2485 const struct btf_enum *e = btf_enum(t);
2486
2487 *res = e->val;
2488 } else {
2489 const struct btf_enum64 *e = btf_enum64(t);
2490
2491 *res = btf_enum64_value(e);
2492 }
2493 return true;
2494 }
2495
pathname_concat(char * buf,size_t buf_sz,const char * path,const char * name)2496 static int pathname_concat(char *buf, size_t buf_sz, const char *path, const char *name)
2497 {
2498 int len;
2499
2500 len = snprintf(buf, buf_sz, "%s/%s", path, name);
2501 if (len < 0)
2502 return -EINVAL;
2503 if (len >= buf_sz)
2504 return -ENAMETOOLONG;
2505
2506 return 0;
2507 }
2508
build_map_pin_path(struct bpf_map * map,const char * path)2509 static int build_map_pin_path(struct bpf_map *map, const char *path)
2510 {
2511 char buf[PATH_MAX];
2512 int err;
2513
2514 if (!path)
2515 path = BPF_FS_DEFAULT_PATH;
2516
2517 err = pathname_concat(buf, sizeof(buf), path, bpf_map__name(map));
2518 if (err)
2519 return err;
2520
2521 return bpf_map__set_pin_path(map, buf);
2522 }
2523
2524 /* should match definition in bpf_helpers.h */
2525 enum libbpf_pin_type {
2526 LIBBPF_PIN_NONE,
2527 /* PIN_BY_NAME: pin maps by name (in /sys/fs/bpf by default) */
2528 LIBBPF_PIN_BY_NAME,
2529 };
2530
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)2531 int parse_btf_map_def(const char *map_name, struct btf *btf,
2532 const struct btf_type *def_t, bool strict,
2533 struct btf_map_def *map_def, struct btf_map_def *inner_def)
2534 {
2535 const struct btf_type *t;
2536 const struct btf_member *m;
2537 bool is_inner = inner_def == NULL;
2538 int vlen, i;
2539
2540 vlen = btf_vlen(def_t);
2541 m = btf_members(def_t);
2542 for (i = 0; i < vlen; i++, m++) {
2543 const char *name = btf__name_by_offset(btf, m->name_off);
2544
2545 if (!name) {
2546 pr_warn("map '%s': invalid field #%d.\n", map_name, i);
2547 return -EINVAL;
2548 }
2549 if (strcmp(name, "type") == 0) {
2550 if (!get_map_field_int(map_name, btf, m, &map_def->map_type))
2551 return -EINVAL;
2552 map_def->parts |= MAP_DEF_MAP_TYPE;
2553 } else if (strcmp(name, "max_entries") == 0) {
2554 if (!get_map_field_int(map_name, btf, m, &map_def->max_entries))
2555 return -EINVAL;
2556 map_def->parts |= MAP_DEF_MAX_ENTRIES;
2557 } else if (strcmp(name, "map_flags") == 0) {
2558 if (!get_map_field_int(map_name, btf, m, &map_def->map_flags))
2559 return -EINVAL;
2560 map_def->parts |= MAP_DEF_MAP_FLAGS;
2561 } else if (strcmp(name, "numa_node") == 0) {
2562 if (!get_map_field_int(map_name, btf, m, &map_def->numa_node))
2563 return -EINVAL;
2564 map_def->parts |= MAP_DEF_NUMA_NODE;
2565 } else if (strcmp(name, "key_size") == 0) {
2566 __u32 sz;
2567
2568 if (!get_map_field_int(map_name, btf, m, &sz))
2569 return -EINVAL;
2570 if (map_def->key_size && map_def->key_size != sz) {
2571 pr_warn("map '%s': conflicting key size %u != %u.\n",
2572 map_name, map_def->key_size, sz);
2573 return -EINVAL;
2574 }
2575 map_def->key_size = sz;
2576 map_def->parts |= MAP_DEF_KEY_SIZE;
2577 } else if (strcmp(name, "key") == 0) {
2578 __s64 sz;
2579
2580 t = btf__type_by_id(btf, m->type);
2581 if (!t) {
2582 pr_warn("map '%s': key type [%d] not found.\n",
2583 map_name, m->type);
2584 return -EINVAL;
2585 }
2586 if (!btf_is_ptr(t)) {
2587 pr_warn("map '%s': key spec is not PTR: %s.\n",
2588 map_name, btf_kind_str(t));
2589 return -EINVAL;
2590 }
2591 sz = btf__resolve_size(btf, t->type);
2592 if (sz < 0) {
2593 pr_warn("map '%s': can't determine key size for type [%u]: %zd.\n",
2594 map_name, t->type, (ssize_t)sz);
2595 return sz;
2596 }
2597 if (map_def->key_size && map_def->key_size != sz) {
2598 pr_warn("map '%s': conflicting key size %u != %zd.\n",
2599 map_name, map_def->key_size, (ssize_t)sz);
2600 return -EINVAL;
2601 }
2602 map_def->key_size = sz;
2603 map_def->key_type_id = t->type;
2604 map_def->parts |= MAP_DEF_KEY_SIZE | MAP_DEF_KEY_TYPE;
2605 } else if (strcmp(name, "value_size") == 0) {
2606 __u32 sz;
2607
2608 if (!get_map_field_int(map_name, btf, m, &sz))
2609 return -EINVAL;
2610 if (map_def->value_size && map_def->value_size != sz) {
2611 pr_warn("map '%s': conflicting value size %u != %u.\n",
2612 map_name, map_def->value_size, sz);
2613 return -EINVAL;
2614 }
2615 map_def->value_size = sz;
2616 map_def->parts |= MAP_DEF_VALUE_SIZE;
2617 } else if (strcmp(name, "value") == 0) {
2618 __s64 sz;
2619
2620 t = btf__type_by_id(btf, m->type);
2621 if (!t) {
2622 pr_warn("map '%s': value type [%d] not found.\n",
2623 map_name, m->type);
2624 return -EINVAL;
2625 }
2626 if (!btf_is_ptr(t)) {
2627 pr_warn("map '%s': value spec is not PTR: %s.\n",
2628 map_name, btf_kind_str(t));
2629 return -EINVAL;
2630 }
2631 sz = btf__resolve_size(btf, t->type);
2632 if (sz < 0) {
2633 pr_warn("map '%s': can't determine value size for type [%u]: %zd.\n",
2634 map_name, t->type, (ssize_t)sz);
2635 return sz;
2636 }
2637 if (map_def->value_size && map_def->value_size != sz) {
2638 pr_warn("map '%s': conflicting value size %u != %zd.\n",
2639 map_name, map_def->value_size, (ssize_t)sz);
2640 return -EINVAL;
2641 }
2642 map_def->value_size = sz;
2643 map_def->value_type_id = t->type;
2644 map_def->parts |= MAP_DEF_VALUE_SIZE | MAP_DEF_VALUE_TYPE;
2645 }
2646 else if (strcmp(name, "values") == 0) {
2647 bool is_map_in_map = bpf_map_type__is_map_in_map(map_def->map_type);
2648 bool is_prog_array = map_def->map_type == BPF_MAP_TYPE_PROG_ARRAY;
2649 const char *desc = is_map_in_map ? "map-in-map inner" : "prog-array value";
2650 char inner_map_name[128];
2651 int err;
2652
2653 if (is_inner) {
2654 pr_warn("map '%s': multi-level inner maps not supported.\n",
2655 map_name);
2656 return -ENOTSUP;
2657 }
2658 if (i != vlen - 1) {
2659 pr_warn("map '%s': '%s' member should be last.\n",
2660 map_name, name);
2661 return -EINVAL;
2662 }
2663 if (!is_map_in_map && !is_prog_array) {
2664 pr_warn("map '%s': should be map-in-map or prog-array.\n",
2665 map_name);
2666 return -ENOTSUP;
2667 }
2668 if (map_def->value_size && map_def->value_size != 4) {
2669 pr_warn("map '%s': conflicting value size %u != 4.\n",
2670 map_name, map_def->value_size);
2671 return -EINVAL;
2672 }
2673 map_def->value_size = 4;
2674 t = btf__type_by_id(btf, m->type);
2675 if (!t) {
2676 pr_warn("map '%s': %s type [%d] not found.\n",
2677 map_name, desc, m->type);
2678 return -EINVAL;
2679 }
2680 if (!btf_is_array(t) || btf_array(t)->nelems) {
2681 pr_warn("map '%s': %s spec is not a zero-sized array.\n",
2682 map_name, desc);
2683 return -EINVAL;
2684 }
2685 t = skip_mods_and_typedefs(btf, btf_array(t)->type, NULL);
2686 if (!btf_is_ptr(t)) {
2687 pr_warn("map '%s': %s def is of unexpected kind %s.\n",
2688 map_name, desc, btf_kind_str(t));
2689 return -EINVAL;
2690 }
2691 t = skip_mods_and_typedefs(btf, t->type, NULL);
2692 if (is_prog_array) {
2693 if (!btf_is_func_proto(t)) {
2694 pr_warn("map '%s': prog-array value def is of unexpected kind %s.\n",
2695 map_name, btf_kind_str(t));
2696 return -EINVAL;
2697 }
2698 continue;
2699 }
2700 if (!btf_is_struct(t)) {
2701 pr_warn("map '%s': map-in-map inner def is of unexpected kind %s.\n",
2702 map_name, btf_kind_str(t));
2703 return -EINVAL;
2704 }
2705
2706 snprintf(inner_map_name, sizeof(inner_map_name), "%s.inner", map_name);
2707 err = parse_btf_map_def(inner_map_name, btf, t, strict, inner_def, NULL);
2708 if (err)
2709 return err;
2710
2711 map_def->parts |= MAP_DEF_INNER_MAP;
2712 } else if (strcmp(name, "pinning") == 0) {
2713 __u32 val;
2714
2715 if (is_inner) {
2716 pr_warn("map '%s': inner def can't be pinned.\n", map_name);
2717 return -EINVAL;
2718 }
2719 if (!get_map_field_int(map_name, btf, m, &val))
2720 return -EINVAL;
2721 if (val != LIBBPF_PIN_NONE && val != LIBBPF_PIN_BY_NAME) {
2722 pr_warn("map '%s': invalid pinning value %u.\n",
2723 map_name, val);
2724 return -EINVAL;
2725 }
2726 map_def->pinning = val;
2727 map_def->parts |= MAP_DEF_PINNING;
2728 } else if (strcmp(name, "map_extra") == 0) {
2729 __u64 map_extra;
2730
2731 if (!get_map_field_long(map_name, btf, m, &map_extra))
2732 return -EINVAL;
2733 map_def->map_extra = map_extra;
2734 map_def->parts |= MAP_DEF_MAP_EXTRA;
2735 } else {
2736 if (strict) {
2737 pr_warn("map '%s': unknown field '%s'.\n", map_name, name);
2738 return -ENOTSUP;
2739 }
2740 pr_debug("map '%s': ignoring unknown field '%s'.\n", map_name, name);
2741 }
2742 }
2743
2744 if (map_def->map_type == BPF_MAP_TYPE_UNSPEC) {
2745 pr_warn("map '%s': map type isn't specified.\n", map_name);
2746 return -EINVAL;
2747 }
2748
2749 return 0;
2750 }
2751
adjust_ringbuf_sz(size_t sz)2752 static size_t adjust_ringbuf_sz(size_t sz)
2753 {
2754 __u32 page_sz = sysconf(_SC_PAGE_SIZE);
2755 __u32 mul;
2756
2757 /* if user forgot to set any size, make sure they see error */
2758 if (sz == 0)
2759 return 0;
2760 /* Kernel expects BPF_MAP_TYPE_RINGBUF's max_entries to be
2761 * a power-of-2 multiple of kernel's page size. If user diligently
2762 * satisified these conditions, pass the size through.
2763 */
2764 if ((sz % page_sz) == 0 && is_pow_of_2(sz / page_sz))
2765 return sz;
2766
2767 /* Otherwise find closest (page_sz * power_of_2) product bigger than
2768 * user-set size to satisfy both user size request and kernel
2769 * requirements and substitute correct max_entries for map creation.
2770 */
2771 for (mul = 1; mul <= UINT_MAX / page_sz; mul <<= 1) {
2772 if (mul * page_sz > sz)
2773 return mul * page_sz;
2774 }
2775
2776 /* if it's impossible to satisfy the conditions (i.e., user size is
2777 * very close to UINT_MAX but is not a power-of-2 multiple of
2778 * page_size) then just return original size and let kernel reject it
2779 */
2780 return sz;
2781 }
2782
map_is_ringbuf(const struct bpf_map * map)2783 static bool map_is_ringbuf(const struct bpf_map *map)
2784 {
2785 return map->def.type == BPF_MAP_TYPE_RINGBUF ||
2786 map->def.type == BPF_MAP_TYPE_USER_RINGBUF;
2787 }
2788
fill_map_from_def(struct bpf_map * map,const struct btf_map_def * def)2789 static void fill_map_from_def(struct bpf_map *map, const struct btf_map_def *def)
2790 {
2791 map->def.type = def->map_type;
2792 map->def.key_size = def->key_size;
2793 map->def.value_size = def->value_size;
2794 map->def.max_entries = def->max_entries;
2795 map->def.map_flags = def->map_flags;
2796 map->map_extra = def->map_extra;
2797
2798 map->numa_node = def->numa_node;
2799 map->btf_key_type_id = def->key_type_id;
2800 map->btf_value_type_id = def->value_type_id;
2801
2802 /* auto-adjust BPF ringbuf map max_entries to be a multiple of page size */
2803 if (map_is_ringbuf(map))
2804 map->def.max_entries = adjust_ringbuf_sz(map->def.max_entries);
2805
2806 if (def->parts & MAP_DEF_MAP_TYPE)
2807 pr_debug("map '%s': found type = %u.\n", map->name, def->map_type);
2808
2809 if (def->parts & MAP_DEF_KEY_TYPE)
2810 pr_debug("map '%s': found key [%u], sz = %u.\n",
2811 map->name, def->key_type_id, def->key_size);
2812 else if (def->parts & MAP_DEF_KEY_SIZE)
2813 pr_debug("map '%s': found key_size = %u.\n", map->name, def->key_size);
2814
2815 if (def->parts & MAP_DEF_VALUE_TYPE)
2816 pr_debug("map '%s': found value [%u], sz = %u.\n",
2817 map->name, def->value_type_id, def->value_size);
2818 else if (def->parts & MAP_DEF_VALUE_SIZE)
2819 pr_debug("map '%s': found value_size = %u.\n", map->name, def->value_size);
2820
2821 if (def->parts & MAP_DEF_MAX_ENTRIES)
2822 pr_debug("map '%s': found max_entries = %u.\n", map->name, def->max_entries);
2823 if (def->parts & MAP_DEF_MAP_FLAGS)
2824 pr_debug("map '%s': found map_flags = 0x%x.\n", map->name, def->map_flags);
2825 if (def->parts & MAP_DEF_MAP_EXTRA)
2826 pr_debug("map '%s': found map_extra = 0x%llx.\n", map->name,
2827 (unsigned long long)def->map_extra);
2828 if (def->parts & MAP_DEF_PINNING)
2829 pr_debug("map '%s': found pinning = %u.\n", map->name, def->pinning);
2830 if (def->parts & MAP_DEF_NUMA_NODE)
2831 pr_debug("map '%s': found numa_node = %u.\n", map->name, def->numa_node);
2832
2833 if (def->parts & MAP_DEF_INNER_MAP)
2834 pr_debug("map '%s': found inner map definition.\n", map->name);
2835 }
2836
btf_var_linkage_str(__u32 linkage)2837 static const char *btf_var_linkage_str(__u32 linkage)
2838 {
2839 switch (linkage) {
2840 case BTF_VAR_STATIC: return "static";
2841 case BTF_VAR_GLOBAL_ALLOCATED: return "global";
2842 case BTF_VAR_GLOBAL_EXTERN: return "extern";
2843 default: return "unknown";
2844 }
2845 }
2846
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)2847 static int bpf_object__init_user_btf_map(struct bpf_object *obj,
2848 const struct btf_type *sec,
2849 int var_idx, int sec_idx,
2850 const Elf_Data *data, bool strict,
2851 const char *pin_root_path)
2852 {
2853 struct btf_map_def map_def = {}, inner_def = {};
2854 const struct btf_type *var, *def;
2855 const struct btf_var_secinfo *vi;
2856 const struct btf_var *var_extra;
2857 const char *map_name;
2858 struct bpf_map *map;
2859 int err;
2860
2861 vi = btf_var_secinfos(sec) + var_idx;
2862 var = btf__type_by_id(obj->btf, vi->type);
2863 var_extra = btf_var(var);
2864 map_name = btf__name_by_offset(obj->btf, var->name_off);
2865
2866 if (map_name == NULL || map_name[0] == '\0') {
2867 pr_warn("map #%d: empty name.\n", var_idx);
2868 return -EINVAL;
2869 }
2870 if ((__u64)vi->offset + vi->size > data->d_size) {
2871 pr_warn("map '%s' BTF data is corrupted.\n", map_name);
2872 return -EINVAL;
2873 }
2874 if (!btf_is_var(var)) {
2875 pr_warn("map '%s': unexpected var kind %s.\n",
2876 map_name, btf_kind_str(var));
2877 return -EINVAL;
2878 }
2879 if (var_extra->linkage != BTF_VAR_GLOBAL_ALLOCATED) {
2880 pr_warn("map '%s': unsupported map linkage %s.\n",
2881 map_name, btf_var_linkage_str(var_extra->linkage));
2882 return -EOPNOTSUPP;
2883 }
2884
2885 def = skip_mods_and_typedefs(obj->btf, var->type, NULL);
2886 if (!btf_is_struct(def)) {
2887 pr_warn("map '%s': unexpected def kind %s.\n",
2888 map_name, btf_kind_str(var));
2889 return -EINVAL;
2890 }
2891 if (def->size > vi->size) {
2892 pr_warn("map '%s': invalid def size.\n", map_name);
2893 return -EINVAL;
2894 }
2895
2896 map = bpf_object__add_map(obj);
2897 if (IS_ERR(map))
2898 return PTR_ERR(map);
2899 map->name = strdup(map_name);
2900 if (!map->name) {
2901 pr_warn("map '%s': failed to alloc map name.\n", map_name);
2902 return -ENOMEM;
2903 }
2904 map->libbpf_type = LIBBPF_MAP_UNSPEC;
2905 map->def.type = BPF_MAP_TYPE_UNSPEC;
2906 map->sec_idx = sec_idx;
2907 map->sec_offset = vi->offset;
2908 map->btf_var_idx = var_idx;
2909 pr_debug("map '%s': at sec_idx %d, offset %zu.\n",
2910 map_name, map->sec_idx, map->sec_offset);
2911
2912 err = parse_btf_map_def(map->name, obj->btf, def, strict, &map_def, &inner_def);
2913 if (err)
2914 return err;
2915
2916 fill_map_from_def(map, &map_def);
2917
2918 if (map_def.pinning == LIBBPF_PIN_BY_NAME) {
2919 err = build_map_pin_path(map, pin_root_path);
2920 if (err) {
2921 pr_warn("map '%s': couldn't build pin path.\n", map->name);
2922 return err;
2923 }
2924 }
2925
2926 if (map_def.parts & MAP_DEF_INNER_MAP) {
2927 map->inner_map = calloc(1, sizeof(*map->inner_map));
2928 if (!map->inner_map)
2929 return -ENOMEM;
2930 map->inner_map->fd = create_placeholder_fd();
2931 if (map->inner_map->fd < 0)
2932 return map->inner_map->fd;
2933 map->inner_map->sec_idx = sec_idx;
2934 map->inner_map->name = malloc(strlen(map_name) + sizeof(".inner") + 1);
2935 if (!map->inner_map->name)
2936 return -ENOMEM;
2937 sprintf(map->inner_map->name, "%s.inner", map_name);
2938
2939 fill_map_from_def(map->inner_map, &inner_def);
2940 }
2941
2942 err = map_fill_btf_type_info(obj, map);
2943 if (err)
2944 return err;
2945
2946 return 0;
2947 }
2948
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)2949 static int init_arena_map_data(struct bpf_object *obj, struct bpf_map *map,
2950 const char *sec_name, int sec_idx,
2951 void *data, size_t data_sz)
2952 {
2953 const long page_sz = sysconf(_SC_PAGE_SIZE);
2954 size_t mmap_sz;
2955
2956 mmap_sz = bpf_map_mmap_sz(obj->arena_map);
2957 if (roundup(data_sz, page_sz) > mmap_sz) {
2958 pr_warn("elf: sec '%s': declared ARENA map size (%zu) is too small to hold global __arena variables of size %zu\n",
2959 sec_name, mmap_sz, data_sz);
2960 return -E2BIG;
2961 }
2962
2963 obj->arena_data = malloc(data_sz);
2964 if (!obj->arena_data)
2965 return -ENOMEM;
2966 memcpy(obj->arena_data, data, data_sz);
2967 obj->arena_data_sz = data_sz;
2968
2969 /* make bpf_map__init_value() work for ARENA maps */
2970 map->mmaped = obj->arena_data;
2971
2972 return 0;
2973 }
2974
bpf_object__init_user_btf_maps(struct bpf_object * obj,bool strict,const char * pin_root_path)2975 static int bpf_object__init_user_btf_maps(struct bpf_object *obj, bool strict,
2976 const char *pin_root_path)
2977 {
2978 const struct btf_type *sec = NULL;
2979 int nr_types, i, vlen, err;
2980 const struct btf_type *t;
2981 const char *name;
2982 Elf_Data *data;
2983 Elf_Scn *scn;
2984
2985 if (obj->efile.btf_maps_shndx < 0)
2986 return 0;
2987
2988 scn = elf_sec_by_idx(obj, obj->efile.btf_maps_shndx);
2989 data = elf_sec_data(obj, scn);
2990 if (!scn || !data) {
2991 pr_warn("elf: failed to get %s map definitions for %s\n",
2992 MAPS_ELF_SEC, obj->path);
2993 return -EINVAL;
2994 }
2995
2996 nr_types = btf__type_cnt(obj->btf);
2997 for (i = 1; i < nr_types; i++) {
2998 t = btf__type_by_id(obj->btf, i);
2999 if (!btf_is_datasec(t))
3000 continue;
3001 name = btf__name_by_offset(obj->btf, t->name_off);
3002 if (strcmp(name, MAPS_ELF_SEC) == 0) {
3003 sec = t;
3004 obj->efile.btf_maps_sec_btf_id = i;
3005 break;
3006 }
3007 }
3008
3009 if (!sec) {
3010 pr_warn("DATASEC '%s' not found.\n", MAPS_ELF_SEC);
3011 return -ENOENT;
3012 }
3013
3014 vlen = btf_vlen(sec);
3015 for (i = 0; i < vlen; i++) {
3016 err = bpf_object__init_user_btf_map(obj, sec, i,
3017 obj->efile.btf_maps_shndx,
3018 data, strict,
3019 pin_root_path);
3020 if (err)
3021 return err;
3022 }
3023
3024 for (i = 0; i < obj->nr_maps; i++) {
3025 struct bpf_map *map = &obj->maps[i];
3026
3027 if (map->def.type != BPF_MAP_TYPE_ARENA)
3028 continue;
3029
3030 if (obj->arena_map) {
3031 pr_warn("map '%s': only single ARENA map is supported (map '%s' is also ARENA)\n",
3032 map->name, obj->arena_map->name);
3033 return -EINVAL;
3034 }
3035 obj->arena_map = map;
3036
3037 if (obj->efile.arena_data) {
3038 err = init_arena_map_data(obj, map, ARENA_SEC, obj->efile.arena_data_shndx,
3039 obj->efile.arena_data->d_buf,
3040 obj->efile.arena_data->d_size);
3041 if (err)
3042 return err;
3043 }
3044 }
3045 if (obj->efile.arena_data && !obj->arena_map) {
3046 pr_warn("elf: sec '%s': to use global __arena variables the ARENA map should be explicitly declared in SEC(\".maps\")\n",
3047 ARENA_SEC);
3048 return -ENOENT;
3049 }
3050
3051 return 0;
3052 }
3053
bpf_object__init_maps(struct bpf_object * obj,const struct bpf_object_open_opts * opts)3054 static int bpf_object__init_maps(struct bpf_object *obj,
3055 const struct bpf_object_open_opts *opts)
3056 {
3057 const char *pin_root_path;
3058 bool strict;
3059 int err = 0;
3060
3061 strict = !OPTS_GET(opts, relaxed_maps, false);
3062 pin_root_path = OPTS_GET(opts, pin_root_path, NULL);
3063
3064 err = bpf_object__init_user_btf_maps(obj, strict, pin_root_path);
3065 err = err ?: bpf_object__init_global_data_maps(obj);
3066 err = err ?: bpf_object__init_kconfig_map(obj);
3067 err = err ?: bpf_object_init_struct_ops(obj);
3068
3069 return err;
3070 }
3071
section_have_execinstr(struct bpf_object * obj,int idx)3072 static bool section_have_execinstr(struct bpf_object *obj, int idx)
3073 {
3074 Elf64_Shdr *sh;
3075
3076 sh = elf_sec_hdr(obj, elf_sec_by_idx(obj, idx));
3077 if (!sh)
3078 return false;
3079
3080 return sh->sh_flags & SHF_EXECINSTR;
3081 }
3082
starts_with_qmark(const char * s)3083 static bool starts_with_qmark(const char *s)
3084 {
3085 return s && s[0] == '?';
3086 }
3087
btf_needs_sanitization(struct bpf_object * obj)3088 static bool btf_needs_sanitization(struct bpf_object *obj)
3089 {
3090 bool has_func_global = kernel_supports(obj, FEAT_BTF_GLOBAL_FUNC);
3091 bool has_datasec = kernel_supports(obj, FEAT_BTF_DATASEC);
3092 bool has_float = kernel_supports(obj, FEAT_BTF_FLOAT);
3093 bool has_func = kernel_supports(obj, FEAT_BTF_FUNC);
3094 bool has_decl_tag = kernel_supports(obj, FEAT_BTF_DECL_TAG);
3095 bool has_type_tag = kernel_supports(obj, FEAT_BTF_TYPE_TAG);
3096 bool has_enum64 = kernel_supports(obj, FEAT_BTF_ENUM64);
3097 bool has_qmark_datasec = kernel_supports(obj, FEAT_BTF_QMARK_DATASEC);
3098
3099 return !has_func || !has_datasec || !has_func_global || !has_float ||
3100 !has_decl_tag || !has_type_tag || !has_enum64 || !has_qmark_datasec;
3101 }
3102
bpf_object__sanitize_btf(struct bpf_object * obj,struct btf * btf)3103 static int bpf_object__sanitize_btf(struct bpf_object *obj, struct btf *btf)
3104 {
3105 bool has_func_global = kernel_supports(obj, FEAT_BTF_GLOBAL_FUNC);
3106 bool has_datasec = kernel_supports(obj, FEAT_BTF_DATASEC);
3107 bool has_float = kernel_supports(obj, FEAT_BTF_FLOAT);
3108 bool has_func = kernel_supports(obj, FEAT_BTF_FUNC);
3109 bool has_decl_tag = kernel_supports(obj, FEAT_BTF_DECL_TAG);
3110 bool has_type_tag = kernel_supports(obj, FEAT_BTF_TYPE_TAG);
3111 bool has_enum64 = kernel_supports(obj, FEAT_BTF_ENUM64);
3112 bool has_qmark_datasec = kernel_supports(obj, FEAT_BTF_QMARK_DATASEC);
3113 int enum64_placeholder_id = 0;
3114 struct btf_type *t;
3115 int i, j, vlen;
3116
3117 for (i = 1; i < btf__type_cnt(btf); i++) {
3118 t = (struct btf_type *)btf__type_by_id(btf, i);
3119
3120 if ((!has_datasec && btf_is_var(t)) || (!has_decl_tag && btf_is_decl_tag(t))) {
3121 /* replace VAR/DECL_TAG with INT */
3122 t->info = BTF_INFO_ENC(BTF_KIND_INT, 0, 0);
3123 /*
3124 * using size = 1 is the safest choice, 4 will be too
3125 * big and cause kernel BTF validation failure if
3126 * original variable took less than 4 bytes
3127 */
3128 t->size = 1;
3129 *(int *)(t + 1) = BTF_INT_ENC(0, 0, 8);
3130 } else if (!has_datasec && btf_is_datasec(t)) {
3131 /* replace DATASEC with STRUCT */
3132 const struct btf_var_secinfo *v = btf_var_secinfos(t);
3133 struct btf_member *m = btf_members(t);
3134 struct btf_type *vt;
3135 char *name;
3136
3137 name = (char *)btf__name_by_offset(btf, t->name_off);
3138 while (*name) {
3139 if (*name == '.' || *name == '?')
3140 *name = '_';
3141 name++;
3142 }
3143
3144 vlen = btf_vlen(t);
3145 t->info = BTF_INFO_ENC(BTF_KIND_STRUCT, 0, vlen);
3146 for (j = 0; j < vlen; j++, v++, m++) {
3147 /* order of field assignments is important */
3148 m->offset = v->offset * 8;
3149 m->type = v->type;
3150 /* preserve variable name as member name */
3151 vt = (void *)btf__type_by_id(btf, v->type);
3152 m->name_off = vt->name_off;
3153 }
3154 } else if (!has_qmark_datasec && btf_is_datasec(t) &&
3155 starts_with_qmark(btf__name_by_offset(btf, t->name_off))) {
3156 /* replace '?' prefix with '_' for DATASEC names */
3157 char *name;
3158
3159 name = (char *)btf__name_by_offset(btf, t->name_off);
3160 if (name[0] == '?')
3161 name[0] = '_';
3162 } else if (!has_func && btf_is_func_proto(t)) {
3163 /* replace FUNC_PROTO with ENUM */
3164 vlen = btf_vlen(t);
3165 t->info = BTF_INFO_ENC(BTF_KIND_ENUM, 0, vlen);
3166 t->size = sizeof(__u32); /* kernel enforced */
3167 } else if (!has_func && btf_is_func(t)) {
3168 /* replace FUNC with TYPEDEF */
3169 t->info = BTF_INFO_ENC(BTF_KIND_TYPEDEF, 0, 0);
3170 } else if (!has_func_global && btf_is_func(t)) {
3171 /* replace BTF_FUNC_GLOBAL with BTF_FUNC_STATIC */
3172 t->info = BTF_INFO_ENC(BTF_KIND_FUNC, 0, 0);
3173 } else if (!has_float && btf_is_float(t)) {
3174 /* replace FLOAT with an equally-sized empty STRUCT;
3175 * since C compilers do not accept e.g. "float" as a
3176 * valid struct name, make it anonymous
3177 */
3178 t->name_off = 0;
3179 t->info = BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 0);
3180 } else if (!has_type_tag && btf_is_type_tag(t)) {
3181 /* replace TYPE_TAG with a CONST */
3182 t->name_off = 0;
3183 t->info = BTF_INFO_ENC(BTF_KIND_CONST, 0, 0);
3184 } else if (!has_enum64 && btf_is_enum(t)) {
3185 /* clear the kflag */
3186 t->info = btf_type_info(btf_kind(t), btf_vlen(t), false);
3187 } else if (!has_enum64 && btf_is_enum64(t)) {
3188 /* replace ENUM64 with a union */
3189 struct btf_member *m;
3190
3191 if (enum64_placeholder_id == 0) {
3192 enum64_placeholder_id = btf__add_int(btf, "enum64_placeholder", 1, 0);
3193 if (enum64_placeholder_id < 0)
3194 return enum64_placeholder_id;
3195
3196 t = (struct btf_type *)btf__type_by_id(btf, i);
3197 }
3198
3199 m = btf_members(t);
3200 vlen = btf_vlen(t);
3201 t->info = BTF_INFO_ENC(BTF_KIND_UNION, 0, vlen);
3202 for (j = 0; j < vlen; j++, m++) {
3203 m->type = enum64_placeholder_id;
3204 m->offset = 0;
3205 }
3206 }
3207 }
3208
3209 return 0;
3210 }
3211
libbpf_needs_btf(const struct bpf_object * obj)3212 static bool libbpf_needs_btf(const struct bpf_object *obj)
3213 {
3214 return obj->efile.btf_maps_shndx >= 0 ||
3215 obj->efile.has_st_ops ||
3216 obj->nr_extern > 0;
3217 }
3218
kernel_needs_btf(const struct bpf_object * obj)3219 static bool kernel_needs_btf(const struct bpf_object *obj)
3220 {
3221 return obj->efile.has_st_ops;
3222 }
3223
bpf_object__init_btf(struct bpf_object * obj,Elf_Data * btf_data,Elf_Data * btf_ext_data)3224 static int bpf_object__init_btf(struct bpf_object *obj,
3225 Elf_Data *btf_data,
3226 Elf_Data *btf_ext_data)
3227 {
3228 int err = -ENOENT;
3229
3230 if (btf_data) {
3231 obj->btf = btf__new(btf_data->d_buf, btf_data->d_size);
3232 err = libbpf_get_error(obj->btf);
3233 if (err) {
3234 obj->btf = NULL;
3235 pr_warn("Error loading ELF section %s: %s.\n", BTF_ELF_SEC, errstr(err));
3236 goto out;
3237 }
3238 /* enforce 8-byte pointers for BPF-targeted BTFs */
3239 btf__set_pointer_size(obj->btf, 8);
3240 }
3241 if (btf_ext_data) {
3242 struct btf_ext_info *ext_segs[3];
3243 int seg_num, sec_num;
3244
3245 if (!obj->btf) {
3246 pr_debug("Ignore ELF section %s because its depending ELF section %s is not found.\n",
3247 BTF_EXT_ELF_SEC, BTF_ELF_SEC);
3248 goto out;
3249 }
3250 obj->btf_ext = btf_ext__new(btf_ext_data->d_buf, btf_ext_data->d_size);
3251 err = libbpf_get_error(obj->btf_ext);
3252 if (err) {
3253 pr_warn("Error loading ELF section %s: %s. Ignored and continue.\n",
3254 BTF_EXT_ELF_SEC, errstr(err));
3255 obj->btf_ext = NULL;
3256 goto out;
3257 }
3258
3259 /* setup .BTF.ext to ELF section mapping */
3260 ext_segs[0] = &obj->btf_ext->func_info;
3261 ext_segs[1] = &obj->btf_ext->line_info;
3262 ext_segs[2] = &obj->btf_ext->core_relo_info;
3263 for (seg_num = 0; seg_num < ARRAY_SIZE(ext_segs); seg_num++) {
3264 struct btf_ext_info *seg = ext_segs[seg_num];
3265 const struct btf_ext_info_sec *sec;
3266 const char *sec_name;
3267 Elf_Scn *scn;
3268
3269 if (seg->sec_cnt == 0)
3270 continue;
3271
3272 seg->sec_idxs = calloc(seg->sec_cnt, sizeof(*seg->sec_idxs));
3273 if (!seg->sec_idxs) {
3274 err = -ENOMEM;
3275 goto out;
3276 }
3277
3278 sec_num = 0;
3279 for_each_btf_ext_sec(seg, sec) {
3280 /* preventively increment index to avoid doing
3281 * this before every continue below
3282 */
3283 sec_num++;
3284
3285 sec_name = btf__name_by_offset(obj->btf, sec->sec_name_off);
3286 if (str_is_empty(sec_name))
3287 continue;
3288 scn = elf_sec_by_name(obj, sec_name);
3289 if (!scn)
3290 continue;
3291
3292 seg->sec_idxs[sec_num - 1] = elf_ndxscn(scn);
3293 }
3294 }
3295 }
3296 out:
3297 if (err && libbpf_needs_btf(obj)) {
3298 pr_warn("BTF is required, but is missing or corrupted.\n");
3299 return err;
3300 }
3301 return 0;
3302 }
3303
compare_vsi_off(const void * _a,const void * _b)3304 static int compare_vsi_off(const void *_a, const void *_b)
3305 {
3306 const struct btf_var_secinfo *a = _a;
3307 const struct btf_var_secinfo *b = _b;
3308
3309 return a->offset - b->offset;
3310 }
3311
btf_fixup_datasec(struct bpf_object * obj,struct btf * btf,struct btf_type * t)3312 static int btf_fixup_datasec(struct bpf_object *obj, struct btf *btf,
3313 struct btf_type *t)
3314 {
3315 __u32 size = 0, i, vars = btf_vlen(t);
3316 const char *sec_name = btf__name_by_offset(btf, t->name_off);
3317 struct btf_var_secinfo *vsi;
3318 bool fixup_offsets = false;
3319 int err;
3320
3321 if (!sec_name) {
3322 pr_debug("No name found in string section for DATASEC kind.\n");
3323 return -ENOENT;
3324 }
3325
3326 /* Extern-backing datasecs (.ksyms, .kconfig) have their size and
3327 * variable offsets set at the previous step. Further, not every
3328 * extern BTF VAR has corresponding ELF symbol preserved, so we skip
3329 * all fixups altogether for such sections and go straight to sorting
3330 * VARs within their DATASEC.
3331 */
3332 if (strcmp(sec_name, KCONFIG_SEC) == 0 || strcmp(sec_name, KSYMS_SEC) == 0)
3333 goto sort_vars;
3334
3335 /* Clang leaves DATASEC size and VAR offsets as zeroes, so we need to
3336 * fix this up. But BPF static linker already fixes this up and fills
3337 * all the sizes and offsets during static linking. So this step has
3338 * to be optional. But the STV_HIDDEN handling is non-optional for any
3339 * non-extern DATASEC, so the variable fixup loop below handles both
3340 * functions at the same time, paying the cost of BTF VAR <-> ELF
3341 * symbol matching just once.
3342 */
3343 if (t->size == 0) {
3344 err = find_elf_sec_sz(obj, sec_name, &size);
3345 if (err || !size) {
3346 pr_debug("sec '%s': failed to determine size from ELF: size %u, err %s\n",
3347 sec_name, size, errstr(err));
3348 return -ENOENT;
3349 }
3350
3351 t->size = size;
3352 fixup_offsets = true;
3353 }
3354
3355 for (i = 0, vsi = btf_var_secinfos(t); i < vars; i++, vsi++) {
3356 const struct btf_type *t_var;
3357 struct btf_var *var;
3358 const char *var_name;
3359 Elf64_Sym *sym;
3360
3361 t_var = btf__type_by_id(btf, vsi->type);
3362 if (!t_var || !btf_is_var(t_var)) {
3363 pr_debug("sec '%s': unexpected non-VAR type found\n", sec_name);
3364 return -EINVAL;
3365 }
3366
3367 var = btf_var(t_var);
3368 if (var->linkage == BTF_VAR_STATIC || var->linkage == BTF_VAR_GLOBAL_EXTERN)
3369 continue;
3370
3371 var_name = btf__name_by_offset(btf, t_var->name_off);
3372 if (!var_name) {
3373 pr_debug("sec '%s': failed to find name of DATASEC's member #%d\n",
3374 sec_name, i);
3375 return -ENOENT;
3376 }
3377
3378 sym = find_elf_var_sym(obj, var_name);
3379 if (IS_ERR(sym)) {
3380 pr_debug("sec '%s': failed to find ELF symbol for VAR '%s'\n",
3381 sec_name, var_name);
3382 return -ENOENT;
3383 }
3384
3385 if (fixup_offsets)
3386 vsi->offset = sym->st_value;
3387
3388 /* if variable is a global/weak symbol, but has restricted
3389 * (STV_HIDDEN or STV_INTERNAL) visibility, mark its BTF VAR
3390 * as static. This follows similar logic for functions (BPF
3391 * subprogs) and influences libbpf's further decisions about
3392 * whether to make global data BPF array maps as
3393 * BPF_F_MMAPABLE.
3394 */
3395 if (ELF64_ST_VISIBILITY(sym->st_other) == STV_HIDDEN
3396 || ELF64_ST_VISIBILITY(sym->st_other) == STV_INTERNAL)
3397 var->linkage = BTF_VAR_STATIC;
3398 }
3399
3400 sort_vars:
3401 qsort(btf_var_secinfos(t), vars, sizeof(*vsi), compare_vsi_off);
3402 return 0;
3403 }
3404
bpf_object_fixup_btf(struct bpf_object * obj)3405 static int bpf_object_fixup_btf(struct bpf_object *obj)
3406 {
3407 int i, n, err = 0;
3408
3409 if (!obj->btf)
3410 return 0;
3411
3412 n = btf__type_cnt(obj->btf);
3413 for (i = 1; i < n; i++) {
3414 struct btf_type *t = btf_type_by_id(obj->btf, i);
3415
3416 /* Loader needs to fix up some of the things compiler
3417 * couldn't get its hands on while emitting BTF. This
3418 * is section size and global variable offset. We use
3419 * the info from the ELF itself for this purpose.
3420 */
3421 if (btf_is_datasec(t)) {
3422 err = btf_fixup_datasec(obj, obj->btf, t);
3423 if (err)
3424 return err;
3425 }
3426 }
3427
3428 return 0;
3429 }
3430
prog_needs_vmlinux_btf(struct bpf_program * prog)3431 static bool prog_needs_vmlinux_btf(struct bpf_program *prog)
3432 {
3433 if (prog->type == BPF_PROG_TYPE_STRUCT_OPS ||
3434 prog->type == BPF_PROG_TYPE_LSM)
3435 return true;
3436
3437 /* BPF_PROG_TYPE_TRACING programs which do not attach to other programs
3438 * also need vmlinux BTF
3439 */
3440 if (prog->type == BPF_PROG_TYPE_TRACING && !prog->attach_prog_fd)
3441 return true;
3442
3443 return false;
3444 }
3445
map_needs_vmlinux_btf(struct bpf_map * map)3446 static bool map_needs_vmlinux_btf(struct bpf_map *map)
3447 {
3448 return bpf_map__is_struct_ops(map);
3449 }
3450
obj_needs_vmlinux_btf(const struct bpf_object * obj)3451 static bool obj_needs_vmlinux_btf(const struct bpf_object *obj)
3452 {
3453 struct bpf_program *prog;
3454 struct bpf_map *map;
3455 int i;
3456
3457 /* CO-RE relocations need kernel BTF, only when btf_custom_path
3458 * is not specified
3459 */
3460 if (obj->btf_ext && obj->btf_ext->core_relo_info.len && !obj->btf_custom_path)
3461 return true;
3462
3463 /* Support for typed ksyms needs kernel BTF */
3464 for (i = 0; i < obj->nr_extern; i++) {
3465 const struct extern_desc *ext;
3466
3467 ext = &obj->externs[i];
3468 if (ext->type == EXT_KSYM && ext->ksym.type_id)
3469 return true;
3470 }
3471
3472 bpf_object__for_each_program(prog, obj) {
3473 if (!prog->autoload)
3474 continue;
3475 if (prog_needs_vmlinux_btf(prog))
3476 return true;
3477 }
3478
3479 bpf_object__for_each_map(map, obj) {
3480 if (map_needs_vmlinux_btf(map))
3481 return true;
3482 }
3483
3484 return false;
3485 }
3486
bpf_object__load_vmlinux_btf(struct bpf_object * obj,bool force)3487 static int bpf_object__load_vmlinux_btf(struct bpf_object *obj, bool force)
3488 {
3489 int err;
3490
3491 /* btf_vmlinux could be loaded earlier */
3492 if (obj->btf_vmlinux || obj->gen_loader)
3493 return 0;
3494
3495 if (!force && !obj_needs_vmlinux_btf(obj))
3496 return 0;
3497
3498 obj->btf_vmlinux = btf__load_vmlinux_btf();
3499 err = libbpf_get_error(obj->btf_vmlinux);
3500 if (err) {
3501 pr_warn("Error loading vmlinux BTF: %s\n", errstr(err));
3502 obj->btf_vmlinux = NULL;
3503 return err;
3504 }
3505 return 0;
3506 }
3507
bpf_object__sanitize_and_load_btf(struct bpf_object * obj)3508 static int bpf_object__sanitize_and_load_btf(struct bpf_object *obj)
3509 {
3510 struct btf *kern_btf = obj->btf;
3511 bool btf_mandatory, sanitize;
3512 int i, err = 0;
3513
3514 if (!obj->btf)
3515 return 0;
3516
3517 if (!kernel_supports(obj, FEAT_BTF)) {
3518 if (kernel_needs_btf(obj)) {
3519 err = -EOPNOTSUPP;
3520 goto report;
3521 }
3522 pr_debug("Kernel doesn't support BTF, skipping uploading it.\n");
3523 return 0;
3524 }
3525
3526 /* Even though some subprogs are global/weak, user might prefer more
3527 * permissive BPF verification process that BPF verifier performs for
3528 * static functions, taking into account more context from the caller
3529 * functions. In such case, they need to mark such subprogs with
3530 * __attribute__((visibility("hidden"))) and libbpf will adjust
3531 * corresponding FUNC BTF type to be marked as static and trigger more
3532 * involved BPF verification process.
3533 */
3534 for (i = 0; i < obj->nr_programs; i++) {
3535 struct bpf_program *prog = &obj->programs[i];
3536 struct btf_type *t;
3537 const char *name;
3538 int j, n;
3539
3540 if (!prog->mark_btf_static || !prog_is_subprog(obj, prog))
3541 continue;
3542
3543 n = btf__type_cnt(obj->btf);
3544 for (j = 1; j < n; j++) {
3545 t = btf_type_by_id(obj->btf, j);
3546 if (!btf_is_func(t) || btf_func_linkage(t) != BTF_FUNC_GLOBAL)
3547 continue;
3548
3549 name = btf__str_by_offset(obj->btf, t->name_off);
3550 if (strcmp(name, prog->name) != 0)
3551 continue;
3552
3553 t->info = btf_type_info(BTF_KIND_FUNC, BTF_FUNC_STATIC, 0);
3554 break;
3555 }
3556 }
3557
3558 sanitize = btf_needs_sanitization(obj);
3559 if (sanitize) {
3560 const void *raw_data;
3561 __u32 sz;
3562
3563 /* clone BTF to sanitize a copy and leave the original intact */
3564 raw_data = btf__raw_data(obj->btf, &sz);
3565 kern_btf = btf__new(raw_data, sz);
3566 err = libbpf_get_error(kern_btf);
3567 if (err)
3568 return err;
3569
3570 /* enforce 8-byte pointers for BPF-targeted BTFs */
3571 btf__set_pointer_size(obj->btf, 8);
3572 err = bpf_object__sanitize_btf(obj, kern_btf);
3573 if (err)
3574 return err;
3575 }
3576
3577 if (obj->gen_loader) {
3578 __u32 raw_size = 0;
3579 const void *raw_data = btf__raw_data(kern_btf, &raw_size);
3580
3581 if (!raw_data)
3582 return -ENOMEM;
3583 bpf_gen__load_btf(obj->gen_loader, raw_data, raw_size);
3584 /* Pretend to have valid FD to pass various fd >= 0 checks.
3585 * This fd == 0 will not be used with any syscall and will be reset to -1 eventually.
3586 */
3587 btf__set_fd(kern_btf, 0);
3588 } else {
3589 /* currently BPF_BTF_LOAD only supports log_level 1 */
3590 err = btf_load_into_kernel(kern_btf, obj->log_buf, obj->log_size,
3591 obj->log_level ? 1 : 0, obj->token_fd);
3592 }
3593 if (sanitize) {
3594 if (!err) {
3595 /* move fd to libbpf's BTF */
3596 btf__set_fd(obj->btf, btf__fd(kern_btf));
3597 btf__set_fd(kern_btf, -1);
3598 }
3599 btf__free(kern_btf);
3600 }
3601 report:
3602 if (err) {
3603 btf_mandatory = kernel_needs_btf(obj);
3604 if (btf_mandatory) {
3605 pr_warn("Error loading .BTF into kernel: %s. BTF is mandatory, can't proceed.\n",
3606 errstr(err));
3607 } else {
3608 pr_info("Error loading .BTF into kernel: %s. BTF is optional, ignoring.\n",
3609 errstr(err));
3610 err = 0;
3611 }
3612 }
3613 return err;
3614 }
3615
elf_sym_str(const struct bpf_object * obj,size_t off)3616 static const char *elf_sym_str(const struct bpf_object *obj, size_t off)
3617 {
3618 const char *name;
3619
3620 name = elf_strptr(obj->efile.elf, obj->efile.strtabidx, off);
3621 if (!name) {
3622 pr_warn("elf: failed to get section name string at offset %zu from %s: %s\n",
3623 off, obj->path, elf_errmsg(-1));
3624 return NULL;
3625 }
3626
3627 return name;
3628 }
3629
elf_sec_str(const struct bpf_object * obj,size_t off)3630 static const char *elf_sec_str(const struct bpf_object *obj, size_t off)
3631 {
3632 const char *name;
3633
3634 name = elf_strptr(obj->efile.elf, obj->efile.shstrndx, off);
3635 if (!name) {
3636 pr_warn("elf: failed to get section name string at offset %zu from %s: %s\n",
3637 off, obj->path, elf_errmsg(-1));
3638 return NULL;
3639 }
3640
3641 return name;
3642 }
3643
elf_sec_by_idx(const struct bpf_object * obj,size_t idx)3644 static Elf_Scn *elf_sec_by_idx(const struct bpf_object *obj, size_t idx)
3645 {
3646 Elf_Scn *scn;
3647
3648 scn = elf_getscn(obj->efile.elf, idx);
3649 if (!scn) {
3650 pr_warn("elf: failed to get section(%zu) from %s: %s\n",
3651 idx, obj->path, elf_errmsg(-1));
3652 return NULL;
3653 }
3654 return scn;
3655 }
3656
elf_sec_by_name(const struct bpf_object * obj,const char * name)3657 static Elf_Scn *elf_sec_by_name(const struct bpf_object *obj, const char *name)
3658 {
3659 Elf_Scn *scn = NULL;
3660 Elf *elf = obj->efile.elf;
3661 const char *sec_name;
3662
3663 while ((scn = elf_nextscn(elf, scn)) != NULL) {
3664 sec_name = elf_sec_name(obj, scn);
3665 if (!sec_name)
3666 return NULL;
3667
3668 if (strcmp(sec_name, name) != 0)
3669 continue;
3670
3671 return scn;
3672 }
3673 return NULL;
3674 }
3675
elf_sec_hdr(const struct bpf_object * obj,Elf_Scn * scn)3676 static Elf64_Shdr *elf_sec_hdr(const struct bpf_object *obj, Elf_Scn *scn)
3677 {
3678 Elf64_Shdr *shdr;
3679
3680 if (!scn)
3681 return NULL;
3682
3683 shdr = elf64_getshdr(scn);
3684 if (!shdr) {
3685 pr_warn("elf: failed to get section(%zu) header from %s: %s\n",
3686 elf_ndxscn(scn), obj->path, elf_errmsg(-1));
3687 return NULL;
3688 }
3689
3690 return shdr;
3691 }
3692
elf_sec_name(const struct bpf_object * obj,Elf_Scn * scn)3693 static const char *elf_sec_name(const struct bpf_object *obj, Elf_Scn *scn)
3694 {
3695 const char *name;
3696 Elf64_Shdr *sh;
3697
3698 if (!scn)
3699 return NULL;
3700
3701 sh = elf_sec_hdr(obj, scn);
3702 if (!sh)
3703 return NULL;
3704
3705 name = elf_sec_str(obj, sh->sh_name);
3706 if (!name) {
3707 pr_warn("elf: failed to get section(%zu) name from %s: %s\n",
3708 elf_ndxscn(scn), obj->path, elf_errmsg(-1));
3709 return NULL;
3710 }
3711
3712 return name;
3713 }
3714
elf_sec_data(const struct bpf_object * obj,Elf_Scn * scn)3715 static Elf_Data *elf_sec_data(const struct bpf_object *obj, Elf_Scn *scn)
3716 {
3717 Elf_Data *data;
3718
3719 if (!scn)
3720 return NULL;
3721
3722 data = elf_getdata(scn, 0);
3723 if (!data) {
3724 pr_warn("elf: failed to get section(%zu) %s data from %s: %s\n",
3725 elf_ndxscn(scn), elf_sec_name(obj, scn) ?: "<?>",
3726 obj->path, elf_errmsg(-1));
3727 return NULL;
3728 }
3729
3730 return data;
3731 }
3732
elf_sym_by_idx(const struct bpf_object * obj,size_t idx)3733 static Elf64_Sym *elf_sym_by_idx(const struct bpf_object *obj, size_t idx)
3734 {
3735 if (idx >= obj->efile.symbols->d_size / sizeof(Elf64_Sym))
3736 return NULL;
3737
3738 return (Elf64_Sym *)obj->efile.symbols->d_buf + idx;
3739 }
3740
elf_rel_by_idx(Elf_Data * data,size_t idx)3741 static Elf64_Rel *elf_rel_by_idx(Elf_Data *data, size_t idx)
3742 {
3743 if (idx >= data->d_size / sizeof(Elf64_Rel))
3744 return NULL;
3745
3746 return (Elf64_Rel *)data->d_buf + idx;
3747 }
3748
is_sec_name_dwarf(const char * name)3749 static bool is_sec_name_dwarf(const char *name)
3750 {
3751 /* approximation, but the actual list is too long */
3752 return str_has_pfx(name, ".debug_");
3753 }
3754
ignore_elf_section(Elf64_Shdr * hdr,const char * name)3755 static bool ignore_elf_section(Elf64_Shdr *hdr, const char *name)
3756 {
3757 /* no special handling of .strtab */
3758 if (hdr->sh_type == SHT_STRTAB)
3759 return true;
3760
3761 /* ignore .llvm_addrsig section as well */
3762 if (hdr->sh_type == SHT_LLVM_ADDRSIG)
3763 return true;
3764
3765 /* no subprograms will lead to an empty .text section, ignore it */
3766 if (hdr->sh_type == SHT_PROGBITS && hdr->sh_size == 0 &&
3767 strcmp(name, ".text") == 0)
3768 return true;
3769
3770 /* DWARF sections */
3771 if (is_sec_name_dwarf(name))
3772 return true;
3773
3774 if (str_has_pfx(name, ".rel")) {
3775 name += sizeof(".rel") - 1;
3776 /* DWARF section relocations */
3777 if (is_sec_name_dwarf(name))
3778 return true;
3779
3780 /* .BTF and .BTF.ext don't need relocations */
3781 if (strcmp(name, BTF_ELF_SEC) == 0 ||
3782 strcmp(name, BTF_EXT_ELF_SEC) == 0)
3783 return true;
3784 }
3785
3786 return false;
3787 }
3788
cmp_progs(const void * _a,const void * _b)3789 static int cmp_progs(const void *_a, const void *_b)
3790 {
3791 const struct bpf_program *a = _a;
3792 const struct bpf_program *b = _b;
3793
3794 if (a->sec_idx != b->sec_idx)
3795 return a->sec_idx < b->sec_idx ? -1 : 1;
3796
3797 /* sec_insn_off can't be the same within the section */
3798 return a->sec_insn_off < b->sec_insn_off ? -1 : 1;
3799 }
3800
bpf_object__elf_collect(struct bpf_object * obj)3801 static int bpf_object__elf_collect(struct bpf_object *obj)
3802 {
3803 struct elf_sec_desc *sec_desc;
3804 Elf *elf = obj->efile.elf;
3805 Elf_Data *btf_ext_data = NULL;
3806 Elf_Data *btf_data = NULL;
3807 int idx = 0, err = 0;
3808 const char *name;
3809 Elf_Data *data;
3810 Elf_Scn *scn;
3811 Elf64_Shdr *sh;
3812
3813 /* ELF section indices are 0-based, but sec #0 is special "invalid"
3814 * section. Since section count retrieved by elf_getshdrnum() does
3815 * include sec #0, it is already the necessary size of an array to keep
3816 * all the sections.
3817 */
3818 if (elf_getshdrnum(obj->efile.elf, &obj->efile.sec_cnt)) {
3819 pr_warn("elf: failed to get the number of sections for %s: %s\n",
3820 obj->path, elf_errmsg(-1));
3821 return -LIBBPF_ERRNO__FORMAT;
3822 }
3823 obj->efile.secs = calloc(obj->efile.sec_cnt, sizeof(*obj->efile.secs));
3824 if (!obj->efile.secs)
3825 return -ENOMEM;
3826
3827 /* a bunch of ELF parsing functionality depends on processing symbols,
3828 * so do the first pass and find the symbol table
3829 */
3830 scn = NULL;
3831 while ((scn = elf_nextscn(elf, scn)) != NULL) {
3832 sh = elf_sec_hdr(obj, scn);
3833 if (!sh)
3834 return -LIBBPF_ERRNO__FORMAT;
3835
3836 if (sh->sh_type == SHT_SYMTAB) {
3837 if (obj->efile.symbols) {
3838 pr_warn("elf: multiple symbol tables in %s\n", obj->path);
3839 return -LIBBPF_ERRNO__FORMAT;
3840 }
3841
3842 data = elf_sec_data(obj, scn);
3843 if (!data)
3844 return -LIBBPF_ERRNO__FORMAT;
3845
3846 idx = elf_ndxscn(scn);
3847
3848 obj->efile.symbols = data;
3849 obj->efile.symbols_shndx = idx;
3850 obj->efile.strtabidx = sh->sh_link;
3851 }
3852 }
3853
3854 if (!obj->efile.symbols) {
3855 pr_warn("elf: couldn't find symbol table in %s, stripped object file?\n",
3856 obj->path);
3857 return -ENOENT;
3858 }
3859
3860 scn = NULL;
3861 while ((scn = elf_nextscn(elf, scn)) != NULL) {
3862 idx = elf_ndxscn(scn);
3863 sec_desc = &obj->efile.secs[idx];
3864
3865 sh = elf_sec_hdr(obj, scn);
3866 if (!sh)
3867 return -LIBBPF_ERRNO__FORMAT;
3868
3869 name = elf_sec_str(obj, sh->sh_name);
3870 if (!name)
3871 return -LIBBPF_ERRNO__FORMAT;
3872
3873 if (ignore_elf_section(sh, name))
3874 continue;
3875
3876 data = elf_sec_data(obj, scn);
3877 if (!data)
3878 return -LIBBPF_ERRNO__FORMAT;
3879
3880 pr_debug("elf: section(%d) %s, size %ld, link %d, flags %lx, type=%d\n",
3881 idx, name, (unsigned long)data->d_size,
3882 (int)sh->sh_link, (unsigned long)sh->sh_flags,
3883 (int)sh->sh_type);
3884
3885 if (strcmp(name, "license") == 0) {
3886 err = bpf_object__init_license(obj, data->d_buf, data->d_size);
3887 if (err)
3888 return err;
3889 } else if (strcmp(name, "version") == 0) {
3890 err = bpf_object__init_kversion(obj, data->d_buf, data->d_size);
3891 if (err)
3892 return err;
3893 } else if (strcmp(name, "maps") == 0) {
3894 pr_warn("elf: legacy map definitions in 'maps' section are not supported by libbpf v1.0+\n");
3895 return -ENOTSUP;
3896 } else if (strcmp(name, MAPS_ELF_SEC) == 0) {
3897 obj->efile.btf_maps_shndx = idx;
3898 } else if (strcmp(name, BTF_ELF_SEC) == 0) {
3899 if (sh->sh_type != SHT_PROGBITS)
3900 return -LIBBPF_ERRNO__FORMAT;
3901 btf_data = data;
3902 } else if (strcmp(name, BTF_EXT_ELF_SEC) == 0) {
3903 if (sh->sh_type != SHT_PROGBITS)
3904 return -LIBBPF_ERRNO__FORMAT;
3905 btf_ext_data = data;
3906 } else if (sh->sh_type == SHT_SYMTAB) {
3907 /* already processed during the first pass above */
3908 } else if (sh->sh_type == SHT_PROGBITS && data->d_size > 0) {
3909 if (sh->sh_flags & SHF_EXECINSTR) {
3910 if (strcmp(name, ".text") == 0)
3911 obj->efile.text_shndx = idx;
3912 err = bpf_object__add_programs(obj, data, name, idx);
3913 if (err)
3914 return err;
3915 } else if (strcmp(name, DATA_SEC) == 0 ||
3916 str_has_pfx(name, DATA_SEC ".")) {
3917 sec_desc->sec_type = SEC_DATA;
3918 sec_desc->shdr = sh;
3919 sec_desc->data = data;
3920 } else if (strcmp(name, RODATA_SEC) == 0 ||
3921 str_has_pfx(name, RODATA_SEC ".")) {
3922 sec_desc->sec_type = SEC_RODATA;
3923 sec_desc->shdr = sh;
3924 sec_desc->data = data;
3925 } else if (strcmp(name, STRUCT_OPS_SEC) == 0 ||
3926 strcmp(name, STRUCT_OPS_LINK_SEC) == 0 ||
3927 strcmp(name, "?" STRUCT_OPS_SEC) == 0 ||
3928 strcmp(name, "?" STRUCT_OPS_LINK_SEC) == 0) {
3929 sec_desc->sec_type = SEC_ST_OPS;
3930 sec_desc->shdr = sh;
3931 sec_desc->data = data;
3932 obj->efile.has_st_ops = true;
3933 } else if (strcmp(name, ARENA_SEC) == 0) {
3934 obj->efile.arena_data = data;
3935 obj->efile.arena_data_shndx = idx;
3936 } else {
3937 pr_info("elf: skipping unrecognized data section(%d) %s\n",
3938 idx, name);
3939 }
3940 } else if (sh->sh_type == SHT_REL) {
3941 int targ_sec_idx = sh->sh_info; /* points to other section */
3942
3943 if (sh->sh_entsize != sizeof(Elf64_Rel) ||
3944 targ_sec_idx >= obj->efile.sec_cnt)
3945 return -LIBBPF_ERRNO__FORMAT;
3946
3947 /* Only do relo for section with exec instructions */
3948 if (!section_have_execinstr(obj, targ_sec_idx) &&
3949 strcmp(name, ".rel" STRUCT_OPS_SEC) &&
3950 strcmp(name, ".rel" STRUCT_OPS_LINK_SEC) &&
3951 strcmp(name, ".rel?" STRUCT_OPS_SEC) &&
3952 strcmp(name, ".rel?" STRUCT_OPS_LINK_SEC) &&
3953 strcmp(name, ".rel" MAPS_ELF_SEC)) {
3954 pr_info("elf: skipping relo section(%d) %s for section(%d) %s\n",
3955 idx, name, targ_sec_idx,
3956 elf_sec_name(obj, elf_sec_by_idx(obj, targ_sec_idx)) ?: "<?>");
3957 continue;
3958 }
3959
3960 sec_desc->sec_type = SEC_RELO;
3961 sec_desc->shdr = sh;
3962 sec_desc->data = data;
3963 } else if (sh->sh_type == SHT_NOBITS && (strcmp(name, BSS_SEC) == 0 ||
3964 str_has_pfx(name, BSS_SEC "."))) {
3965 sec_desc->sec_type = SEC_BSS;
3966 sec_desc->shdr = sh;
3967 sec_desc->data = data;
3968 } else {
3969 pr_info("elf: skipping section(%d) %s (size %zu)\n", idx, name,
3970 (size_t)sh->sh_size);
3971 }
3972 }
3973
3974 if (!obj->efile.strtabidx || obj->efile.strtabidx > idx) {
3975 pr_warn("elf: symbol strings section missing or invalid in %s\n", obj->path);
3976 return -LIBBPF_ERRNO__FORMAT;
3977 }
3978
3979 /* change BPF program insns to native endianness for introspection */
3980 if (!is_native_endianness(obj))
3981 bpf_object_bswap_progs(obj);
3982
3983 /* sort BPF programs by section name and in-section instruction offset
3984 * for faster search
3985 */
3986 if (obj->nr_programs)
3987 qsort(obj->programs, obj->nr_programs, sizeof(*obj->programs), cmp_progs);
3988
3989 return bpf_object__init_btf(obj, btf_data, btf_ext_data);
3990 }
3991
sym_is_extern(const Elf64_Sym * sym)3992 static bool sym_is_extern(const Elf64_Sym *sym)
3993 {
3994 int bind = ELF64_ST_BIND(sym->st_info);
3995 /* externs are symbols w/ type=NOTYPE, bind=GLOBAL|WEAK, section=UND */
3996 return sym->st_shndx == SHN_UNDEF &&
3997 (bind == STB_GLOBAL || bind == STB_WEAK) &&
3998 ELF64_ST_TYPE(sym->st_info) == STT_NOTYPE;
3999 }
4000
sym_is_subprog(const Elf64_Sym * sym,int text_shndx)4001 static bool sym_is_subprog(const Elf64_Sym *sym, int text_shndx)
4002 {
4003 int bind = ELF64_ST_BIND(sym->st_info);
4004 int type = ELF64_ST_TYPE(sym->st_info);
4005
4006 /* in .text section */
4007 if (sym->st_shndx != text_shndx)
4008 return false;
4009
4010 /* local function */
4011 if (bind == STB_LOCAL && type == STT_SECTION)
4012 return true;
4013
4014 /* global function */
4015 return (bind == STB_GLOBAL || bind == STB_WEAK) && type == STT_FUNC;
4016 }
4017
find_extern_btf_id(const struct btf * btf,const char * ext_name)4018 static int find_extern_btf_id(const struct btf *btf, const char *ext_name)
4019 {
4020 const struct btf_type *t;
4021 const char *tname;
4022 int i, n;
4023
4024 if (!btf)
4025 return -ESRCH;
4026
4027 n = btf__type_cnt(btf);
4028 for (i = 1; i < n; i++) {
4029 t = btf__type_by_id(btf, i);
4030
4031 if (!btf_is_var(t) && !btf_is_func(t))
4032 continue;
4033
4034 tname = btf__name_by_offset(btf, t->name_off);
4035 if (strcmp(tname, ext_name))
4036 continue;
4037
4038 if (btf_is_var(t) &&
4039 btf_var(t)->linkage != BTF_VAR_GLOBAL_EXTERN)
4040 return -EINVAL;
4041
4042 if (btf_is_func(t) && btf_func_linkage(t) != BTF_FUNC_EXTERN)
4043 return -EINVAL;
4044
4045 return i;
4046 }
4047
4048 return -ENOENT;
4049 }
4050
find_extern_sec_btf_id(struct btf * btf,int ext_btf_id)4051 static int find_extern_sec_btf_id(struct btf *btf, int ext_btf_id) {
4052 const struct btf_var_secinfo *vs;
4053 const struct btf_type *t;
4054 int i, j, n;
4055
4056 if (!btf)
4057 return -ESRCH;
4058
4059 n = btf__type_cnt(btf);
4060 for (i = 1; i < n; i++) {
4061 t = btf__type_by_id(btf, i);
4062
4063 if (!btf_is_datasec(t))
4064 continue;
4065
4066 vs = btf_var_secinfos(t);
4067 for (j = 0; j < btf_vlen(t); j++, vs++) {
4068 if (vs->type == ext_btf_id)
4069 return i;
4070 }
4071 }
4072
4073 return -ENOENT;
4074 }
4075
find_kcfg_type(const struct btf * btf,int id,bool * is_signed)4076 static enum kcfg_type find_kcfg_type(const struct btf *btf, int id,
4077 bool *is_signed)
4078 {
4079 const struct btf_type *t;
4080 const char *name;
4081
4082 t = skip_mods_and_typedefs(btf, id, NULL);
4083 name = btf__name_by_offset(btf, t->name_off);
4084
4085 if (is_signed)
4086 *is_signed = false;
4087 switch (btf_kind(t)) {
4088 case BTF_KIND_INT: {
4089 int enc = btf_int_encoding(t);
4090
4091 if (enc & BTF_INT_BOOL)
4092 return t->size == 1 ? KCFG_BOOL : KCFG_UNKNOWN;
4093 if (is_signed)
4094 *is_signed = enc & BTF_INT_SIGNED;
4095 if (t->size == 1)
4096 return KCFG_CHAR;
4097 if (t->size < 1 || t->size > 8 || (t->size & (t->size - 1)))
4098 return KCFG_UNKNOWN;
4099 return KCFG_INT;
4100 }
4101 case BTF_KIND_ENUM:
4102 if (t->size != 4)
4103 return KCFG_UNKNOWN;
4104 if (strcmp(name, "libbpf_tristate"))
4105 return KCFG_UNKNOWN;
4106 return KCFG_TRISTATE;
4107 case BTF_KIND_ENUM64:
4108 if (strcmp(name, "libbpf_tristate"))
4109 return KCFG_UNKNOWN;
4110 return KCFG_TRISTATE;
4111 case BTF_KIND_ARRAY:
4112 if (btf_array(t)->nelems == 0)
4113 return KCFG_UNKNOWN;
4114 if (find_kcfg_type(btf, btf_array(t)->type, NULL) != KCFG_CHAR)
4115 return KCFG_UNKNOWN;
4116 return KCFG_CHAR_ARR;
4117 default:
4118 return KCFG_UNKNOWN;
4119 }
4120 }
4121
cmp_externs(const void * _a,const void * _b)4122 static int cmp_externs(const void *_a, const void *_b)
4123 {
4124 const struct extern_desc *a = _a;
4125 const struct extern_desc *b = _b;
4126
4127 if (a->type != b->type)
4128 return a->type < b->type ? -1 : 1;
4129
4130 if (a->type == EXT_KCFG) {
4131 /* descending order by alignment requirements */
4132 if (a->kcfg.align != b->kcfg.align)
4133 return a->kcfg.align > b->kcfg.align ? -1 : 1;
4134 /* ascending order by size, within same alignment class */
4135 if (a->kcfg.sz != b->kcfg.sz)
4136 return a->kcfg.sz < b->kcfg.sz ? -1 : 1;
4137 }
4138
4139 /* resolve ties by name */
4140 return strcmp(a->name, b->name);
4141 }
4142
find_int_btf_id(const struct btf * btf)4143 static int find_int_btf_id(const struct btf *btf)
4144 {
4145 const struct btf_type *t;
4146 int i, n;
4147
4148 n = btf__type_cnt(btf);
4149 for (i = 1; i < n; i++) {
4150 t = btf__type_by_id(btf, i);
4151
4152 if (btf_is_int(t) && btf_int_bits(t) == 32)
4153 return i;
4154 }
4155
4156 return 0;
4157 }
4158
add_dummy_ksym_var(struct btf * btf)4159 static int add_dummy_ksym_var(struct btf *btf)
4160 {
4161 int i, int_btf_id, sec_btf_id, dummy_var_btf_id;
4162 const struct btf_var_secinfo *vs;
4163 const struct btf_type *sec;
4164
4165 if (!btf)
4166 return 0;
4167
4168 sec_btf_id = btf__find_by_name_kind(btf, KSYMS_SEC,
4169 BTF_KIND_DATASEC);
4170 if (sec_btf_id < 0)
4171 return 0;
4172
4173 sec = btf__type_by_id(btf, sec_btf_id);
4174 vs = btf_var_secinfos(sec);
4175 for (i = 0; i < btf_vlen(sec); i++, vs++) {
4176 const struct btf_type *vt;
4177
4178 vt = btf__type_by_id(btf, vs->type);
4179 if (btf_is_func(vt))
4180 break;
4181 }
4182
4183 /* No func in ksyms sec. No need to add dummy var. */
4184 if (i == btf_vlen(sec))
4185 return 0;
4186
4187 int_btf_id = find_int_btf_id(btf);
4188 dummy_var_btf_id = btf__add_var(btf,
4189 "dummy_ksym",
4190 BTF_VAR_GLOBAL_ALLOCATED,
4191 int_btf_id);
4192 if (dummy_var_btf_id < 0)
4193 pr_warn("cannot create a dummy_ksym var\n");
4194
4195 return dummy_var_btf_id;
4196 }
4197
bpf_object__collect_externs(struct bpf_object * obj)4198 static int bpf_object__collect_externs(struct bpf_object *obj)
4199 {
4200 struct btf_type *sec, *kcfg_sec = NULL, *ksym_sec = NULL;
4201 const struct btf_type *t;
4202 struct extern_desc *ext;
4203 int i, n, off, dummy_var_btf_id;
4204 const char *ext_name, *sec_name;
4205 size_t ext_essent_len;
4206 Elf_Scn *scn;
4207 Elf64_Shdr *sh;
4208
4209 if (!obj->efile.symbols)
4210 return 0;
4211
4212 scn = elf_sec_by_idx(obj, obj->efile.symbols_shndx);
4213 sh = elf_sec_hdr(obj, scn);
4214 if (!sh || sh->sh_entsize != sizeof(Elf64_Sym))
4215 return -LIBBPF_ERRNO__FORMAT;
4216
4217 dummy_var_btf_id = add_dummy_ksym_var(obj->btf);
4218 if (dummy_var_btf_id < 0)
4219 return dummy_var_btf_id;
4220
4221 n = sh->sh_size / sh->sh_entsize;
4222 pr_debug("looking for externs among %d symbols...\n", n);
4223
4224 for (i = 0; i < n; i++) {
4225 Elf64_Sym *sym = elf_sym_by_idx(obj, i);
4226
4227 if (!sym)
4228 return -LIBBPF_ERRNO__FORMAT;
4229 if (!sym_is_extern(sym))
4230 continue;
4231 ext_name = elf_sym_str(obj, sym->st_name);
4232 if (!ext_name || !ext_name[0])
4233 continue;
4234
4235 ext = obj->externs;
4236 ext = libbpf_reallocarray(ext, obj->nr_extern + 1, sizeof(*ext));
4237 if (!ext)
4238 return -ENOMEM;
4239 obj->externs = ext;
4240 ext = &ext[obj->nr_extern];
4241 memset(ext, 0, sizeof(*ext));
4242 obj->nr_extern++;
4243
4244 ext->btf_id = find_extern_btf_id(obj->btf, ext_name);
4245 if (ext->btf_id <= 0) {
4246 pr_warn("failed to find BTF for extern '%s': %d\n",
4247 ext_name, ext->btf_id);
4248 return ext->btf_id;
4249 }
4250 t = btf__type_by_id(obj->btf, ext->btf_id);
4251 ext->name = btf__name_by_offset(obj->btf, t->name_off);
4252 ext->sym_idx = i;
4253 ext->is_weak = ELF64_ST_BIND(sym->st_info) == STB_WEAK;
4254
4255 ext_essent_len = bpf_core_essential_name_len(ext->name);
4256 ext->essent_name = NULL;
4257 if (ext_essent_len != strlen(ext->name)) {
4258 ext->essent_name = strndup(ext->name, ext_essent_len);
4259 if (!ext->essent_name)
4260 return -ENOMEM;
4261 }
4262
4263 ext->sec_btf_id = find_extern_sec_btf_id(obj->btf, ext->btf_id);
4264 if (ext->sec_btf_id <= 0) {
4265 pr_warn("failed to find BTF for extern '%s' [%d] section: %d\n",
4266 ext_name, ext->btf_id, ext->sec_btf_id);
4267 return ext->sec_btf_id;
4268 }
4269 sec = (void *)btf__type_by_id(obj->btf, ext->sec_btf_id);
4270 sec_name = btf__name_by_offset(obj->btf, sec->name_off);
4271
4272 if (strcmp(sec_name, KCONFIG_SEC) == 0) {
4273 if (btf_is_func(t)) {
4274 pr_warn("extern function %s is unsupported under %s section\n",
4275 ext->name, KCONFIG_SEC);
4276 return -ENOTSUP;
4277 }
4278 kcfg_sec = sec;
4279 ext->type = EXT_KCFG;
4280 ext->kcfg.sz = btf__resolve_size(obj->btf, t->type);
4281 if (ext->kcfg.sz <= 0) {
4282 pr_warn("failed to resolve size of extern (kcfg) '%s': %d\n",
4283 ext_name, ext->kcfg.sz);
4284 return ext->kcfg.sz;
4285 }
4286 ext->kcfg.align = btf__align_of(obj->btf, t->type);
4287 if (ext->kcfg.align <= 0) {
4288 pr_warn("failed to determine alignment of extern (kcfg) '%s': %d\n",
4289 ext_name, ext->kcfg.align);
4290 return -EINVAL;
4291 }
4292 ext->kcfg.type = find_kcfg_type(obj->btf, t->type,
4293 &ext->kcfg.is_signed);
4294 if (ext->kcfg.type == KCFG_UNKNOWN) {
4295 pr_warn("extern (kcfg) '%s': type is unsupported\n", ext_name);
4296 return -ENOTSUP;
4297 }
4298 } else if (strcmp(sec_name, KSYMS_SEC) == 0) {
4299 ksym_sec = sec;
4300 ext->type = EXT_KSYM;
4301 skip_mods_and_typedefs(obj->btf, t->type,
4302 &ext->ksym.type_id);
4303 } else {
4304 pr_warn("unrecognized extern section '%s'\n", sec_name);
4305 return -ENOTSUP;
4306 }
4307 }
4308 pr_debug("collected %d externs total\n", obj->nr_extern);
4309
4310 if (!obj->nr_extern)
4311 return 0;
4312
4313 /* sort externs by type, for kcfg ones also by (align, size, name) */
4314 qsort(obj->externs, obj->nr_extern, sizeof(*ext), cmp_externs);
4315
4316 /* for .ksyms section, we need to turn all externs into allocated
4317 * variables in BTF to pass kernel verification; we do this by
4318 * pretending that each extern is a 8-byte variable
4319 */
4320 if (ksym_sec) {
4321 /* find existing 4-byte integer type in BTF to use for fake
4322 * extern variables in DATASEC
4323 */
4324 int int_btf_id = find_int_btf_id(obj->btf);
4325 /* For extern function, a dummy_var added earlier
4326 * will be used to replace the vs->type and
4327 * its name string will be used to refill
4328 * the missing param's name.
4329 */
4330 const struct btf_type *dummy_var;
4331
4332 dummy_var = btf__type_by_id(obj->btf, dummy_var_btf_id);
4333 for (i = 0; i < obj->nr_extern; i++) {
4334 ext = &obj->externs[i];
4335 if (ext->type != EXT_KSYM)
4336 continue;
4337 pr_debug("extern (ksym) #%d: symbol %d, name %s\n",
4338 i, ext->sym_idx, ext->name);
4339 }
4340
4341 sec = ksym_sec;
4342 n = btf_vlen(sec);
4343 for (i = 0, off = 0; i < n; i++, off += sizeof(int)) {
4344 struct btf_var_secinfo *vs = btf_var_secinfos(sec) + i;
4345 struct btf_type *vt;
4346
4347 vt = (void *)btf__type_by_id(obj->btf, vs->type);
4348 ext_name = btf__name_by_offset(obj->btf, vt->name_off);
4349 ext = find_extern_by_name(obj, ext_name);
4350 if (!ext) {
4351 pr_warn("failed to find extern definition for BTF %s '%s'\n",
4352 btf_kind_str(vt), ext_name);
4353 return -ESRCH;
4354 }
4355 if (btf_is_func(vt)) {
4356 const struct btf_type *func_proto;
4357 struct btf_param *param;
4358 int j;
4359
4360 func_proto = btf__type_by_id(obj->btf,
4361 vt->type);
4362 param = btf_params(func_proto);
4363 /* Reuse the dummy_var string if the
4364 * func proto does not have param name.
4365 */
4366 for (j = 0; j < btf_vlen(func_proto); j++)
4367 if (param[j].type && !param[j].name_off)
4368 param[j].name_off =
4369 dummy_var->name_off;
4370 vs->type = dummy_var_btf_id;
4371 vt->info &= ~0xffff;
4372 vt->info |= BTF_FUNC_GLOBAL;
4373 } else {
4374 btf_var(vt)->linkage = BTF_VAR_GLOBAL_ALLOCATED;
4375 vt->type = int_btf_id;
4376 }
4377 vs->offset = off;
4378 vs->size = sizeof(int);
4379 }
4380 sec->size = off;
4381 }
4382
4383 if (kcfg_sec) {
4384 sec = kcfg_sec;
4385 /* for kcfg externs calculate their offsets within a .kconfig map */
4386 off = 0;
4387 for (i = 0; i < obj->nr_extern; i++) {
4388 ext = &obj->externs[i];
4389 if (ext->type != EXT_KCFG)
4390 continue;
4391
4392 ext->kcfg.data_off = roundup(off, ext->kcfg.align);
4393 off = ext->kcfg.data_off + ext->kcfg.sz;
4394 pr_debug("extern (kcfg) #%d: symbol %d, off %u, name %s\n",
4395 i, ext->sym_idx, ext->kcfg.data_off, ext->name);
4396 }
4397 sec->size = off;
4398 n = btf_vlen(sec);
4399 for (i = 0; i < n; i++) {
4400 struct btf_var_secinfo *vs = btf_var_secinfos(sec) + i;
4401
4402 t = btf__type_by_id(obj->btf, vs->type);
4403 ext_name = btf__name_by_offset(obj->btf, t->name_off);
4404 ext = find_extern_by_name(obj, ext_name);
4405 if (!ext) {
4406 pr_warn("failed to find extern definition for BTF var '%s'\n",
4407 ext_name);
4408 return -ESRCH;
4409 }
4410 btf_var(t)->linkage = BTF_VAR_GLOBAL_ALLOCATED;
4411 vs->offset = ext->kcfg.data_off;
4412 }
4413 }
4414 return 0;
4415 }
4416
prog_is_subprog(const struct bpf_object * obj,const struct bpf_program * prog)4417 static bool prog_is_subprog(const struct bpf_object *obj, const struct bpf_program *prog)
4418 {
4419 return prog->sec_idx == obj->efile.text_shndx;
4420 }
4421
4422 struct bpf_program *
bpf_object__find_program_by_name(const struct bpf_object * obj,const char * name)4423 bpf_object__find_program_by_name(const struct bpf_object *obj,
4424 const char *name)
4425 {
4426 struct bpf_program *prog;
4427
4428 bpf_object__for_each_program(prog, obj) {
4429 if (prog_is_subprog(obj, prog))
4430 continue;
4431 if (!strcmp(prog->name, name))
4432 return prog;
4433 }
4434 return errno = ENOENT, NULL;
4435 }
4436
bpf_object__shndx_is_data(const struct bpf_object * obj,int shndx)4437 static bool bpf_object__shndx_is_data(const struct bpf_object *obj,
4438 int shndx)
4439 {
4440 switch (obj->efile.secs[shndx].sec_type) {
4441 case SEC_BSS:
4442 case SEC_DATA:
4443 case SEC_RODATA:
4444 return true;
4445 default:
4446 return false;
4447 }
4448 }
4449
bpf_object__shndx_is_maps(const struct bpf_object * obj,int shndx)4450 static bool bpf_object__shndx_is_maps(const struct bpf_object *obj,
4451 int shndx)
4452 {
4453 return shndx == obj->efile.btf_maps_shndx;
4454 }
4455
4456 static enum libbpf_map_type
bpf_object__section_to_libbpf_map_type(const struct bpf_object * obj,int shndx)4457 bpf_object__section_to_libbpf_map_type(const struct bpf_object *obj, int shndx)
4458 {
4459 if (shndx == obj->efile.symbols_shndx)
4460 return LIBBPF_MAP_KCONFIG;
4461
4462 switch (obj->efile.secs[shndx].sec_type) {
4463 case SEC_BSS:
4464 return LIBBPF_MAP_BSS;
4465 case SEC_DATA:
4466 return LIBBPF_MAP_DATA;
4467 case SEC_RODATA:
4468 return LIBBPF_MAP_RODATA;
4469 default:
4470 return LIBBPF_MAP_UNSPEC;
4471 }
4472 }
4473
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)4474 static int bpf_program__record_reloc(struct bpf_program *prog,
4475 struct reloc_desc *reloc_desc,
4476 __u32 insn_idx, const char *sym_name,
4477 const Elf64_Sym *sym, const Elf64_Rel *rel)
4478 {
4479 struct bpf_insn *insn = &prog->insns[insn_idx];
4480 size_t map_idx, nr_maps = prog->obj->nr_maps;
4481 struct bpf_object *obj = prog->obj;
4482 __u32 shdr_idx = sym->st_shndx;
4483 enum libbpf_map_type type;
4484 const char *sym_sec_name;
4485 struct bpf_map *map;
4486
4487 if (!is_call_insn(insn) && !is_ldimm64_insn(insn)) {
4488 pr_warn("prog '%s': invalid relo against '%s' for insns[%d].code 0x%x\n",
4489 prog->name, sym_name, insn_idx, insn->code);
4490 return -LIBBPF_ERRNO__RELOC;
4491 }
4492
4493 if (sym_is_extern(sym)) {
4494 int sym_idx = ELF64_R_SYM(rel->r_info);
4495 int i, n = obj->nr_extern;
4496 struct extern_desc *ext;
4497
4498 for (i = 0; i < n; i++) {
4499 ext = &obj->externs[i];
4500 if (ext->sym_idx == sym_idx)
4501 break;
4502 }
4503 if (i >= n) {
4504 pr_warn("prog '%s': extern relo failed to find extern for '%s' (%d)\n",
4505 prog->name, sym_name, sym_idx);
4506 return -LIBBPF_ERRNO__RELOC;
4507 }
4508 pr_debug("prog '%s': found extern #%d '%s' (sym %d) for insn #%u\n",
4509 prog->name, i, ext->name, ext->sym_idx, insn_idx);
4510 if (insn->code == (BPF_JMP | BPF_CALL))
4511 reloc_desc->type = RELO_EXTERN_CALL;
4512 else
4513 reloc_desc->type = RELO_EXTERN_LD64;
4514 reloc_desc->insn_idx = insn_idx;
4515 reloc_desc->ext_idx = i;
4516 return 0;
4517 }
4518
4519 /* sub-program call relocation */
4520 if (is_call_insn(insn)) {
4521 if (insn->src_reg != BPF_PSEUDO_CALL) {
4522 pr_warn("prog '%s': incorrect bpf_call opcode\n", prog->name);
4523 return -LIBBPF_ERRNO__RELOC;
4524 }
4525 /* text_shndx can be 0, if no default "main" program exists */
4526 if (!shdr_idx || shdr_idx != obj->efile.text_shndx) {
4527 sym_sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, shdr_idx));
4528 pr_warn("prog '%s': bad call relo against '%s' in section '%s'\n",
4529 prog->name, sym_name, sym_sec_name);
4530 return -LIBBPF_ERRNO__RELOC;
4531 }
4532 if (sym->st_value % BPF_INSN_SZ) {
4533 pr_warn("prog '%s': bad call relo against '%s' at offset %zu\n",
4534 prog->name, sym_name, (size_t)sym->st_value);
4535 return -LIBBPF_ERRNO__RELOC;
4536 }
4537 reloc_desc->type = RELO_CALL;
4538 reloc_desc->insn_idx = insn_idx;
4539 reloc_desc->sym_off = sym->st_value;
4540 return 0;
4541 }
4542
4543 if (!shdr_idx || shdr_idx >= SHN_LORESERVE) {
4544 pr_warn("prog '%s': invalid relo against '%s' in special section 0x%x; forgot to initialize global var?..\n",
4545 prog->name, sym_name, shdr_idx);
4546 return -LIBBPF_ERRNO__RELOC;
4547 }
4548
4549 /* loading subprog addresses */
4550 if (sym_is_subprog(sym, obj->efile.text_shndx)) {
4551 /* global_func: sym->st_value = offset in the section, insn->imm = 0.
4552 * local_func: sym->st_value = 0, insn->imm = offset in the section.
4553 */
4554 if ((sym->st_value % BPF_INSN_SZ) || (insn->imm % BPF_INSN_SZ)) {
4555 pr_warn("prog '%s': bad subprog addr relo against '%s' at offset %zu+%d\n",
4556 prog->name, sym_name, (size_t)sym->st_value, insn->imm);
4557 return -LIBBPF_ERRNO__RELOC;
4558 }
4559
4560 reloc_desc->type = RELO_SUBPROG_ADDR;
4561 reloc_desc->insn_idx = insn_idx;
4562 reloc_desc->sym_off = sym->st_value;
4563 return 0;
4564 }
4565
4566 type = bpf_object__section_to_libbpf_map_type(obj, shdr_idx);
4567 sym_sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, shdr_idx));
4568
4569 /* arena data relocation */
4570 if (shdr_idx == obj->efile.arena_data_shndx) {
4571 reloc_desc->type = RELO_DATA;
4572 reloc_desc->insn_idx = insn_idx;
4573 reloc_desc->map_idx = obj->arena_map - obj->maps;
4574 reloc_desc->sym_off = sym->st_value;
4575 return 0;
4576 }
4577
4578 /* generic map reference relocation */
4579 if (type == LIBBPF_MAP_UNSPEC) {
4580 if (!bpf_object__shndx_is_maps(obj, shdr_idx)) {
4581 pr_warn("prog '%s': bad map relo against '%s' in section '%s'\n",
4582 prog->name, sym_name, sym_sec_name);
4583 return -LIBBPF_ERRNO__RELOC;
4584 }
4585 for (map_idx = 0; map_idx < nr_maps; map_idx++) {
4586 map = &obj->maps[map_idx];
4587 if (map->libbpf_type != type ||
4588 map->sec_idx != sym->st_shndx ||
4589 map->sec_offset != sym->st_value)
4590 continue;
4591 pr_debug("prog '%s': found map %zd (%s, sec %d, off %zu) for insn #%u\n",
4592 prog->name, map_idx, map->name, map->sec_idx,
4593 map->sec_offset, insn_idx);
4594 break;
4595 }
4596 if (map_idx >= nr_maps) {
4597 pr_warn("prog '%s': map relo failed to find map for section '%s', off %zu\n",
4598 prog->name, sym_sec_name, (size_t)sym->st_value);
4599 return -LIBBPF_ERRNO__RELOC;
4600 }
4601 reloc_desc->type = RELO_LD64;
4602 reloc_desc->insn_idx = insn_idx;
4603 reloc_desc->map_idx = map_idx;
4604 reloc_desc->sym_off = 0; /* sym->st_value determines map_idx */
4605 return 0;
4606 }
4607
4608 /* global data map relocation */
4609 if (!bpf_object__shndx_is_data(obj, shdr_idx)) {
4610 pr_warn("prog '%s': bad data relo against section '%s'\n",
4611 prog->name, sym_sec_name);
4612 return -LIBBPF_ERRNO__RELOC;
4613 }
4614 for (map_idx = 0; map_idx < nr_maps; map_idx++) {
4615 map = &obj->maps[map_idx];
4616 if (map->libbpf_type != type || map->sec_idx != sym->st_shndx)
4617 continue;
4618 pr_debug("prog '%s': found data map %zd (%s, sec %d, off %zu) for insn %u\n",
4619 prog->name, map_idx, map->name, map->sec_idx,
4620 map->sec_offset, insn_idx);
4621 break;
4622 }
4623 if (map_idx >= nr_maps) {
4624 pr_warn("prog '%s': data relo failed to find map for section '%s'\n",
4625 prog->name, sym_sec_name);
4626 return -LIBBPF_ERRNO__RELOC;
4627 }
4628
4629 reloc_desc->type = RELO_DATA;
4630 reloc_desc->insn_idx = insn_idx;
4631 reloc_desc->map_idx = map_idx;
4632 reloc_desc->sym_off = sym->st_value;
4633 return 0;
4634 }
4635
prog_contains_insn(const struct bpf_program * prog,size_t insn_idx)4636 static bool prog_contains_insn(const struct bpf_program *prog, size_t insn_idx)
4637 {
4638 return insn_idx >= prog->sec_insn_off &&
4639 insn_idx < prog->sec_insn_off + prog->sec_insn_cnt;
4640 }
4641
find_prog_by_sec_insn(const struct bpf_object * obj,size_t sec_idx,size_t insn_idx)4642 static struct bpf_program *find_prog_by_sec_insn(const struct bpf_object *obj,
4643 size_t sec_idx, size_t insn_idx)
4644 {
4645 int l = 0, r = obj->nr_programs - 1, m;
4646 struct bpf_program *prog;
4647
4648 if (!obj->nr_programs)
4649 return NULL;
4650
4651 while (l < r) {
4652 m = l + (r - l + 1) / 2;
4653 prog = &obj->programs[m];
4654
4655 if (prog->sec_idx < sec_idx ||
4656 (prog->sec_idx == sec_idx && prog->sec_insn_off <= insn_idx))
4657 l = m;
4658 else
4659 r = m - 1;
4660 }
4661 /* matching program could be at index l, but it still might be the
4662 * wrong one, so we need to double check conditions for the last time
4663 */
4664 prog = &obj->programs[l];
4665 if (prog->sec_idx == sec_idx && prog_contains_insn(prog, insn_idx))
4666 return prog;
4667 return NULL;
4668 }
4669
4670 static int
bpf_object__collect_prog_relos(struct bpf_object * obj,Elf64_Shdr * shdr,Elf_Data * data)4671 bpf_object__collect_prog_relos(struct bpf_object *obj, Elf64_Shdr *shdr, Elf_Data *data)
4672 {
4673 const char *relo_sec_name, *sec_name;
4674 size_t sec_idx = shdr->sh_info, sym_idx;
4675 struct bpf_program *prog;
4676 struct reloc_desc *relos;
4677 int err, i, nrels;
4678 const char *sym_name;
4679 __u32 insn_idx;
4680 Elf_Scn *scn;
4681 Elf_Data *scn_data;
4682 Elf64_Sym *sym;
4683 Elf64_Rel *rel;
4684
4685 if (sec_idx >= obj->efile.sec_cnt)
4686 return -EINVAL;
4687
4688 scn = elf_sec_by_idx(obj, sec_idx);
4689 scn_data = elf_sec_data(obj, scn);
4690 if (!scn_data)
4691 return -LIBBPF_ERRNO__FORMAT;
4692
4693 relo_sec_name = elf_sec_str(obj, shdr->sh_name);
4694 sec_name = elf_sec_name(obj, scn);
4695 if (!relo_sec_name || !sec_name)
4696 return -EINVAL;
4697
4698 pr_debug("sec '%s': collecting relocation for section(%zu) '%s'\n",
4699 relo_sec_name, sec_idx, sec_name);
4700 nrels = shdr->sh_size / shdr->sh_entsize;
4701
4702 for (i = 0; i < nrels; i++) {
4703 rel = elf_rel_by_idx(data, i);
4704 if (!rel) {
4705 pr_warn("sec '%s': failed to get relo #%d\n", relo_sec_name, i);
4706 return -LIBBPF_ERRNO__FORMAT;
4707 }
4708
4709 sym_idx = ELF64_R_SYM(rel->r_info);
4710 sym = elf_sym_by_idx(obj, sym_idx);
4711 if (!sym) {
4712 pr_warn("sec '%s': symbol #%zu not found for relo #%d\n",
4713 relo_sec_name, sym_idx, i);
4714 return -LIBBPF_ERRNO__FORMAT;
4715 }
4716
4717 if (sym->st_shndx >= obj->efile.sec_cnt) {
4718 pr_warn("sec '%s': corrupted symbol #%zu pointing to invalid section #%zu for relo #%d\n",
4719 relo_sec_name, sym_idx, (size_t)sym->st_shndx, i);
4720 return -LIBBPF_ERRNO__FORMAT;
4721 }
4722
4723 if (rel->r_offset % BPF_INSN_SZ || rel->r_offset >= scn_data->d_size) {
4724 pr_warn("sec '%s': invalid offset 0x%zx for relo #%d\n",
4725 relo_sec_name, (size_t)rel->r_offset, i);
4726 return -LIBBPF_ERRNO__FORMAT;
4727 }
4728
4729 insn_idx = rel->r_offset / BPF_INSN_SZ;
4730 /* relocations against static functions are recorded as
4731 * relocations against the section that contains a function;
4732 * in such case, symbol will be STT_SECTION and sym.st_name
4733 * will point to empty string (0), so fetch section name
4734 * instead
4735 */
4736 if (ELF64_ST_TYPE(sym->st_info) == STT_SECTION && sym->st_name == 0)
4737 sym_name = elf_sec_name(obj, elf_sec_by_idx(obj, sym->st_shndx));
4738 else
4739 sym_name = elf_sym_str(obj, sym->st_name);
4740 sym_name = sym_name ?: "<?";
4741
4742 pr_debug("sec '%s': relo #%d: insn #%u against '%s'\n",
4743 relo_sec_name, i, insn_idx, sym_name);
4744
4745 prog = find_prog_by_sec_insn(obj, sec_idx, insn_idx);
4746 if (!prog) {
4747 pr_debug("sec '%s': relo #%d: couldn't find program in section '%s' for insn #%u, probably overridden weak function, skipping...\n",
4748 relo_sec_name, i, sec_name, insn_idx);
4749 continue;
4750 }
4751
4752 relos = libbpf_reallocarray(prog->reloc_desc,
4753 prog->nr_reloc + 1, sizeof(*relos));
4754 if (!relos)
4755 return -ENOMEM;
4756 prog->reloc_desc = relos;
4757
4758 /* adjust insn_idx to local BPF program frame of reference */
4759 insn_idx -= prog->sec_insn_off;
4760 err = bpf_program__record_reloc(prog, &relos[prog->nr_reloc],
4761 insn_idx, sym_name, sym, rel);
4762 if (err)
4763 return err;
4764
4765 prog->nr_reloc++;
4766 }
4767 return 0;
4768 }
4769
map_fill_btf_type_info(struct bpf_object * obj,struct bpf_map * map)4770 static int map_fill_btf_type_info(struct bpf_object *obj, struct bpf_map *map)
4771 {
4772 int id;
4773
4774 if (!obj->btf)
4775 return -ENOENT;
4776
4777 /* if it's BTF-defined map, we don't need to search for type IDs.
4778 * For struct_ops map, it does not need btf_key_type_id and
4779 * btf_value_type_id.
4780 */
4781 if (map->sec_idx == obj->efile.btf_maps_shndx || bpf_map__is_struct_ops(map))
4782 return 0;
4783
4784 /*
4785 * LLVM annotates global data differently in BTF, that is,
4786 * only as '.data', '.bss' or '.rodata'.
4787 */
4788 if (!bpf_map__is_internal(map))
4789 return -ENOENT;
4790
4791 id = btf__find_by_name(obj->btf, map->real_name);
4792 if (id < 0)
4793 return id;
4794
4795 map->btf_key_type_id = 0;
4796 map->btf_value_type_id = id;
4797 return 0;
4798 }
4799
bpf_get_map_info_from_fdinfo(int fd,struct bpf_map_info * info)4800 static int bpf_get_map_info_from_fdinfo(int fd, struct bpf_map_info *info)
4801 {
4802 char file[PATH_MAX], buff[4096];
4803 FILE *fp;
4804 __u32 val;
4805 int err;
4806
4807 snprintf(file, sizeof(file), "/proc/%d/fdinfo/%d", getpid(), fd);
4808 memset(info, 0, sizeof(*info));
4809
4810 fp = fopen(file, "re");
4811 if (!fp) {
4812 err = -errno;
4813 pr_warn("failed to open %s: %s. No procfs support?\n", file,
4814 errstr(err));
4815 return err;
4816 }
4817
4818 while (fgets(buff, sizeof(buff), fp)) {
4819 if (sscanf(buff, "map_type:\t%u", &val) == 1)
4820 info->type = val;
4821 else if (sscanf(buff, "key_size:\t%u", &val) == 1)
4822 info->key_size = val;
4823 else if (sscanf(buff, "value_size:\t%u", &val) == 1)
4824 info->value_size = val;
4825 else if (sscanf(buff, "max_entries:\t%u", &val) == 1)
4826 info->max_entries = val;
4827 else if (sscanf(buff, "map_flags:\t%i", &val) == 1)
4828 info->map_flags = val;
4829 }
4830
4831 fclose(fp);
4832
4833 return 0;
4834 }
4835
bpf_map__autocreate(const struct bpf_map * map)4836 bool bpf_map__autocreate(const struct bpf_map *map)
4837 {
4838 return map->autocreate;
4839 }
4840
bpf_map__set_autocreate(struct bpf_map * map,bool autocreate)4841 int bpf_map__set_autocreate(struct bpf_map *map, bool autocreate)
4842 {
4843 if (map->obj->loaded)
4844 return libbpf_err(-EBUSY);
4845
4846 map->autocreate = autocreate;
4847 return 0;
4848 }
4849
bpf_map__set_autoattach(struct bpf_map * map,bool autoattach)4850 int bpf_map__set_autoattach(struct bpf_map *map, bool autoattach)
4851 {
4852 if (!bpf_map__is_struct_ops(map))
4853 return libbpf_err(-EINVAL);
4854
4855 map->autoattach = autoattach;
4856 return 0;
4857 }
4858
bpf_map__autoattach(const struct bpf_map * map)4859 bool bpf_map__autoattach(const struct bpf_map *map)
4860 {
4861 return map->autoattach;
4862 }
4863
bpf_map__reuse_fd(struct bpf_map * map,int fd)4864 int bpf_map__reuse_fd(struct bpf_map *map, int fd)
4865 {
4866 struct bpf_map_info info;
4867 __u32 len = sizeof(info), name_len;
4868 int new_fd, err;
4869 char *new_name;
4870
4871 memset(&info, 0, len);
4872 err = bpf_map_get_info_by_fd(fd, &info, &len);
4873 if (err && errno == EINVAL)
4874 err = bpf_get_map_info_from_fdinfo(fd, &info);
4875 if (err)
4876 return libbpf_err(err);
4877
4878 name_len = strlen(info.name);
4879 if (name_len == BPF_OBJ_NAME_LEN - 1 && strncmp(map->name, info.name, name_len) == 0)
4880 new_name = strdup(map->name);
4881 else
4882 new_name = strdup(info.name);
4883
4884 if (!new_name)
4885 return libbpf_err(-errno);
4886
4887 /*
4888 * Like dup(), but make sure new FD is >= 3 and has O_CLOEXEC set.
4889 * This is similar to what we do in ensure_good_fd(), but without
4890 * closing original FD.
4891 */
4892 new_fd = fcntl(fd, F_DUPFD_CLOEXEC, 3);
4893 if (new_fd < 0) {
4894 err = -errno;
4895 goto err_free_new_name;
4896 }
4897
4898 err = reuse_fd(map->fd, new_fd);
4899 if (err)
4900 goto err_free_new_name;
4901
4902 free(map->name);
4903
4904 map->name = new_name;
4905 map->def.type = info.type;
4906 map->def.key_size = info.key_size;
4907 map->def.value_size = info.value_size;
4908 map->def.max_entries = info.max_entries;
4909 map->def.map_flags = info.map_flags;
4910 map->btf_key_type_id = info.btf_key_type_id;
4911 map->btf_value_type_id = info.btf_value_type_id;
4912 map->reused = true;
4913 map->map_extra = info.map_extra;
4914
4915 return 0;
4916
4917 err_free_new_name:
4918 free(new_name);
4919 return libbpf_err(err);
4920 }
4921
bpf_map__max_entries(const struct bpf_map * map)4922 __u32 bpf_map__max_entries(const struct bpf_map *map)
4923 {
4924 return map->def.max_entries;
4925 }
4926
bpf_map__inner_map(struct bpf_map * map)4927 struct bpf_map *bpf_map__inner_map(struct bpf_map *map)
4928 {
4929 if (!bpf_map_type__is_map_in_map(map->def.type))
4930 return errno = EINVAL, NULL;
4931
4932 return map->inner_map;
4933 }
4934
bpf_map__set_max_entries(struct bpf_map * map,__u32 max_entries)4935 int bpf_map__set_max_entries(struct bpf_map *map, __u32 max_entries)
4936 {
4937 if (map->obj->loaded)
4938 return libbpf_err(-EBUSY);
4939
4940 map->def.max_entries = max_entries;
4941
4942 /* auto-adjust BPF ringbuf map max_entries to be a multiple of page size */
4943 if (map_is_ringbuf(map))
4944 map->def.max_entries = adjust_ringbuf_sz(map->def.max_entries);
4945
4946 return 0;
4947 }
4948
bpf_object_prepare_token(struct bpf_object * obj)4949 static int bpf_object_prepare_token(struct bpf_object *obj)
4950 {
4951 const char *bpffs_path;
4952 int bpffs_fd = -1, token_fd, err;
4953 bool mandatory;
4954 enum libbpf_print_level level;
4955
4956 /* token is explicitly prevented */
4957 if (obj->token_path && obj->token_path[0] == '\0') {
4958 pr_debug("object '%s': token is prevented, skipping...\n", obj->name);
4959 return 0;
4960 }
4961
4962 mandatory = obj->token_path != NULL;
4963 level = mandatory ? LIBBPF_WARN : LIBBPF_DEBUG;
4964
4965 bpffs_path = obj->token_path ?: BPF_FS_DEFAULT_PATH;
4966 bpffs_fd = open(bpffs_path, O_DIRECTORY, O_RDWR);
4967 if (bpffs_fd < 0) {
4968 err = -errno;
4969 __pr(level, "object '%s': failed (%s) to open BPF FS mount at '%s'%s\n",
4970 obj->name, errstr(err), bpffs_path,
4971 mandatory ? "" : ", skipping optional step...");
4972 return mandatory ? err : 0;
4973 }
4974
4975 token_fd = bpf_token_create(bpffs_fd, 0);
4976 close(bpffs_fd);
4977 if (token_fd < 0) {
4978 if (!mandatory && token_fd == -ENOENT) {
4979 pr_debug("object '%s': BPF FS at '%s' doesn't have BPF token delegation set up, skipping...\n",
4980 obj->name, bpffs_path);
4981 return 0;
4982 }
4983 __pr(level, "object '%s': failed (%d) to create BPF token from '%s'%s\n",
4984 obj->name, token_fd, bpffs_path,
4985 mandatory ? "" : ", skipping optional step...");
4986 return mandatory ? token_fd : 0;
4987 }
4988
4989 obj->feat_cache = calloc(1, sizeof(*obj->feat_cache));
4990 if (!obj->feat_cache) {
4991 close(token_fd);
4992 return -ENOMEM;
4993 }
4994
4995 obj->token_fd = token_fd;
4996 obj->feat_cache->token_fd = token_fd;
4997
4998 return 0;
4999 }
5000
5001 static int
bpf_object__probe_loading(struct bpf_object * obj)5002 bpf_object__probe_loading(struct bpf_object *obj)
5003 {
5004 struct bpf_insn insns[] = {
5005 BPF_MOV64_IMM(BPF_REG_0, 0),
5006 BPF_EXIT_INSN(),
5007 };
5008 int ret, insn_cnt = ARRAY_SIZE(insns);
5009 LIBBPF_OPTS(bpf_prog_load_opts, opts,
5010 .token_fd = obj->token_fd,
5011 .prog_flags = obj->token_fd ? BPF_F_TOKEN_FD : 0,
5012 );
5013
5014 if (obj->gen_loader)
5015 return 0;
5016
5017 ret = bump_rlimit_memlock();
5018 if (ret)
5019 pr_warn("Failed to bump RLIMIT_MEMLOCK (err = %s), you might need to do it explicitly!\n",
5020 errstr(ret));
5021
5022 /* make sure basic loading works */
5023 ret = bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, NULL, "GPL", insns, insn_cnt, &opts);
5024 if (ret < 0)
5025 ret = bpf_prog_load(BPF_PROG_TYPE_TRACEPOINT, NULL, "GPL", insns, insn_cnt, &opts);
5026 if (ret < 0) {
5027 ret = errno;
5028 pr_warn("Error in %s(): %s. Couldn't load trivial BPF program. Make sure your kernel supports BPF (CONFIG_BPF_SYSCALL=y) and/or that RLIMIT_MEMLOCK is set to big enough value.\n",
5029 __func__, errstr(ret));
5030 return -ret;
5031 }
5032 close(ret);
5033
5034 return 0;
5035 }
5036
kernel_supports(const struct bpf_object * obj,enum kern_feature_id feat_id)5037 bool kernel_supports(const struct bpf_object *obj, enum kern_feature_id feat_id)
5038 {
5039 if (obj->gen_loader)
5040 /* To generate loader program assume the latest kernel
5041 * to avoid doing extra prog_load, map_create syscalls.
5042 */
5043 return true;
5044
5045 if (obj->token_fd)
5046 return feat_supported(obj->feat_cache, feat_id);
5047
5048 return feat_supported(NULL, feat_id);
5049 }
5050
map_is_reuse_compat(const struct bpf_map * map,int map_fd)5051 static bool map_is_reuse_compat(const struct bpf_map *map, int map_fd)
5052 {
5053 struct bpf_map_info map_info;
5054 __u32 map_info_len = sizeof(map_info);
5055 int err;
5056
5057 memset(&map_info, 0, map_info_len);
5058 err = bpf_map_get_info_by_fd(map_fd, &map_info, &map_info_len);
5059 if (err && errno == EINVAL)
5060 err = bpf_get_map_info_from_fdinfo(map_fd, &map_info);
5061 if (err) {
5062 pr_warn("failed to get map info for map FD %d: %s\n", map_fd,
5063 errstr(err));
5064 return false;
5065 }
5066
5067 return (map_info.type == map->def.type &&
5068 map_info.key_size == map->def.key_size &&
5069 map_info.value_size == map->def.value_size &&
5070 map_info.max_entries == map->def.max_entries &&
5071 map_info.map_flags == map->def.map_flags &&
5072 map_info.map_extra == map->map_extra);
5073 }
5074
5075 static int
bpf_object__reuse_map(struct bpf_map * map)5076 bpf_object__reuse_map(struct bpf_map *map)
5077 {
5078 int err, pin_fd;
5079
5080 pin_fd = bpf_obj_get(map->pin_path);
5081 if (pin_fd < 0) {
5082 err = -errno;
5083 if (err == -ENOENT) {
5084 pr_debug("found no pinned map to reuse at '%s'\n",
5085 map->pin_path);
5086 return 0;
5087 }
5088
5089 pr_warn("couldn't retrieve pinned map '%s': %s\n",
5090 map->pin_path, errstr(err));
5091 return err;
5092 }
5093
5094 if (!map_is_reuse_compat(map, pin_fd)) {
5095 pr_warn("couldn't reuse pinned map at '%s': parameter mismatch\n",
5096 map->pin_path);
5097 close(pin_fd);
5098 return -EINVAL;
5099 }
5100
5101 err = bpf_map__reuse_fd(map, pin_fd);
5102 close(pin_fd);
5103 if (err)
5104 return err;
5105
5106 map->pinned = true;
5107 pr_debug("reused pinned map at '%s'\n", map->pin_path);
5108
5109 return 0;
5110 }
5111
5112 static int
bpf_object__populate_internal_map(struct bpf_object * obj,struct bpf_map * map)5113 bpf_object__populate_internal_map(struct bpf_object *obj, struct bpf_map *map)
5114 {
5115 enum libbpf_map_type map_type = map->libbpf_type;
5116 int err, zero = 0;
5117 size_t mmap_sz;
5118
5119 if (obj->gen_loader) {
5120 bpf_gen__map_update_elem(obj->gen_loader, map - obj->maps,
5121 map->mmaped, map->def.value_size);
5122 if (map_type == LIBBPF_MAP_RODATA || map_type == LIBBPF_MAP_KCONFIG)
5123 bpf_gen__map_freeze(obj->gen_loader, map - obj->maps);
5124 return 0;
5125 }
5126
5127 err = bpf_map_update_elem(map->fd, &zero, map->mmaped, 0);
5128 if (err) {
5129 err = -errno;
5130 pr_warn("map '%s': failed to set initial contents: %s\n",
5131 bpf_map__name(map), errstr(err));
5132 return err;
5133 }
5134
5135 /* Freeze .rodata and .kconfig map as read-only from syscall side. */
5136 if (map_type == LIBBPF_MAP_RODATA || map_type == LIBBPF_MAP_KCONFIG) {
5137 err = bpf_map_freeze(map->fd);
5138 if (err) {
5139 err = -errno;
5140 pr_warn("map '%s': failed to freeze as read-only: %s\n",
5141 bpf_map__name(map), errstr(err));
5142 return err;
5143 }
5144 }
5145
5146 /* Remap anonymous mmap()-ed "map initialization image" as
5147 * a BPF map-backed mmap()-ed memory, but preserving the same
5148 * memory address. This will cause kernel to change process'
5149 * page table to point to a different piece of kernel memory,
5150 * but from userspace point of view memory address (and its
5151 * contents, being identical at this point) will stay the
5152 * same. This mapping will be released by bpf_object__close()
5153 * as per normal clean up procedure.
5154 */
5155 mmap_sz = bpf_map_mmap_sz(map);
5156 if (map->def.map_flags & BPF_F_MMAPABLE) {
5157 void *mmaped;
5158 int prot;
5159
5160 if (map->def.map_flags & BPF_F_RDONLY_PROG)
5161 prot = PROT_READ;
5162 else
5163 prot = PROT_READ | PROT_WRITE;
5164 mmaped = mmap(map->mmaped, mmap_sz, prot, MAP_SHARED | MAP_FIXED, map->fd, 0);
5165 if (mmaped == MAP_FAILED) {
5166 err = -errno;
5167 pr_warn("map '%s': failed to re-mmap() contents: %s\n",
5168 bpf_map__name(map), errstr(err));
5169 return err;
5170 }
5171 map->mmaped = mmaped;
5172 } else if (map->mmaped) {
5173 munmap(map->mmaped, mmap_sz);
5174 map->mmaped = NULL;
5175 }
5176
5177 return 0;
5178 }
5179
5180 static void bpf_map__destroy(struct bpf_map *map);
5181
map_is_created(const struct bpf_map * map)5182 static bool map_is_created(const struct bpf_map *map)
5183 {
5184 return map->obj->loaded || map->reused;
5185 }
5186
bpf_object__create_map(struct bpf_object * obj,struct bpf_map * map,bool is_inner)5187 static int bpf_object__create_map(struct bpf_object *obj, struct bpf_map *map, bool is_inner)
5188 {
5189 LIBBPF_OPTS(bpf_map_create_opts, create_attr);
5190 struct bpf_map_def *def = &map->def;
5191 const char *map_name = NULL;
5192 int err = 0, map_fd;
5193
5194 if (kernel_supports(obj, FEAT_PROG_NAME))
5195 map_name = map->name;
5196 create_attr.map_ifindex = map->map_ifindex;
5197 create_attr.map_flags = def->map_flags;
5198 create_attr.numa_node = map->numa_node;
5199 create_attr.map_extra = map->map_extra;
5200 create_attr.token_fd = obj->token_fd;
5201 if (obj->token_fd)
5202 create_attr.map_flags |= BPF_F_TOKEN_FD;
5203
5204 if (bpf_map__is_struct_ops(map)) {
5205 create_attr.btf_vmlinux_value_type_id = map->btf_vmlinux_value_type_id;
5206 if (map->mod_btf_fd >= 0) {
5207 create_attr.value_type_btf_obj_fd = map->mod_btf_fd;
5208 create_attr.map_flags |= BPF_F_VTYPE_BTF_OBJ_FD;
5209 }
5210 }
5211
5212 if (obj->btf && btf__fd(obj->btf) >= 0) {
5213 create_attr.btf_fd = btf__fd(obj->btf);
5214 create_attr.btf_key_type_id = map->btf_key_type_id;
5215 create_attr.btf_value_type_id = map->btf_value_type_id;
5216 }
5217
5218 if (bpf_map_type__is_map_in_map(def->type)) {
5219 if (map->inner_map) {
5220 err = map_set_def_max_entries(map->inner_map);
5221 if (err)
5222 return err;
5223 err = bpf_object__create_map(obj, map->inner_map, true);
5224 if (err) {
5225 pr_warn("map '%s': failed to create inner map: %s\n",
5226 map->name, errstr(err));
5227 return err;
5228 }
5229 map->inner_map_fd = map->inner_map->fd;
5230 }
5231 if (map->inner_map_fd >= 0)
5232 create_attr.inner_map_fd = map->inner_map_fd;
5233 }
5234
5235 switch (def->type) {
5236 case BPF_MAP_TYPE_PERF_EVENT_ARRAY:
5237 case BPF_MAP_TYPE_CGROUP_ARRAY:
5238 case BPF_MAP_TYPE_STACK_TRACE:
5239 case BPF_MAP_TYPE_ARRAY_OF_MAPS:
5240 case BPF_MAP_TYPE_HASH_OF_MAPS:
5241 case BPF_MAP_TYPE_DEVMAP:
5242 case BPF_MAP_TYPE_DEVMAP_HASH:
5243 case BPF_MAP_TYPE_CPUMAP:
5244 case BPF_MAP_TYPE_XSKMAP:
5245 case BPF_MAP_TYPE_SOCKMAP:
5246 case BPF_MAP_TYPE_SOCKHASH:
5247 case BPF_MAP_TYPE_QUEUE:
5248 case BPF_MAP_TYPE_STACK:
5249 case BPF_MAP_TYPE_ARENA:
5250 create_attr.btf_fd = 0;
5251 create_attr.btf_key_type_id = 0;
5252 create_attr.btf_value_type_id = 0;
5253 map->btf_key_type_id = 0;
5254 map->btf_value_type_id = 0;
5255 break;
5256 case BPF_MAP_TYPE_STRUCT_OPS:
5257 create_attr.btf_value_type_id = 0;
5258 break;
5259 default:
5260 break;
5261 }
5262
5263 if (obj->gen_loader) {
5264 bpf_gen__map_create(obj->gen_loader, def->type, map_name,
5265 def->key_size, def->value_size, def->max_entries,
5266 &create_attr, is_inner ? -1 : map - obj->maps);
5267 /* We keep pretenting we have valid FD to pass various fd >= 0
5268 * checks by just keeping original placeholder FDs in place.
5269 * See bpf_object__add_map() comment.
5270 * This placeholder fd will not be used with any syscall and
5271 * will be reset to -1 eventually.
5272 */
5273 map_fd = map->fd;
5274 } else {
5275 map_fd = bpf_map_create(def->type, map_name,
5276 def->key_size, def->value_size,
5277 def->max_entries, &create_attr);
5278 }
5279 if (map_fd < 0 && (create_attr.btf_key_type_id || create_attr.btf_value_type_id)) {
5280 err = -errno;
5281 pr_warn("Error in bpf_create_map_xattr(%s): %s. Retrying without BTF.\n",
5282 map->name, errstr(err));
5283 create_attr.btf_fd = 0;
5284 create_attr.btf_key_type_id = 0;
5285 create_attr.btf_value_type_id = 0;
5286 map->btf_key_type_id = 0;
5287 map->btf_value_type_id = 0;
5288 map_fd = bpf_map_create(def->type, map_name,
5289 def->key_size, def->value_size,
5290 def->max_entries, &create_attr);
5291 }
5292
5293 if (bpf_map_type__is_map_in_map(def->type) && map->inner_map) {
5294 if (obj->gen_loader)
5295 map->inner_map->fd = -1;
5296 bpf_map__destroy(map->inner_map);
5297 zfree(&map->inner_map);
5298 }
5299
5300 if (map_fd < 0)
5301 return map_fd;
5302
5303 /* obj->gen_loader case, prevent reuse_fd() from closing map_fd */
5304 if (map->fd == map_fd)
5305 return 0;
5306
5307 /* Keep placeholder FD value but now point it to the BPF map object.
5308 * This way everything that relied on this map's FD (e.g., relocated
5309 * ldimm64 instructions) will stay valid and won't need adjustments.
5310 * map->fd stays valid but now point to what map_fd points to.
5311 */
5312 return reuse_fd(map->fd, map_fd);
5313 }
5314
init_map_in_map_slots(struct bpf_object * obj,struct bpf_map * map)5315 static int init_map_in_map_slots(struct bpf_object *obj, struct bpf_map *map)
5316 {
5317 const struct bpf_map *targ_map;
5318 unsigned int i;
5319 int fd, err = 0;
5320
5321 for (i = 0; i < map->init_slots_sz; i++) {
5322 if (!map->init_slots[i])
5323 continue;
5324
5325 targ_map = map->init_slots[i];
5326 fd = targ_map->fd;
5327
5328 if (obj->gen_loader) {
5329 bpf_gen__populate_outer_map(obj->gen_loader,
5330 map - obj->maps, i,
5331 targ_map - obj->maps);
5332 } else {
5333 err = bpf_map_update_elem(map->fd, &i, &fd, 0);
5334 }
5335 if (err) {
5336 err = -errno;
5337 pr_warn("map '%s': failed to initialize slot [%d] to map '%s' fd=%d: %s\n",
5338 map->name, i, targ_map->name, fd, errstr(err));
5339 return err;
5340 }
5341 pr_debug("map '%s': slot [%d] set to map '%s' fd=%d\n",
5342 map->name, i, targ_map->name, fd);
5343 }
5344
5345 zfree(&map->init_slots);
5346 map->init_slots_sz = 0;
5347
5348 return 0;
5349 }
5350
init_prog_array_slots(struct bpf_object * obj,struct bpf_map * map)5351 static int init_prog_array_slots(struct bpf_object *obj, struct bpf_map *map)
5352 {
5353 const struct bpf_program *targ_prog;
5354 unsigned int i;
5355 int fd, err;
5356
5357 if (obj->gen_loader)
5358 return -ENOTSUP;
5359
5360 for (i = 0; i < map->init_slots_sz; i++) {
5361 if (!map->init_slots[i])
5362 continue;
5363
5364 targ_prog = map->init_slots[i];
5365 fd = bpf_program__fd(targ_prog);
5366
5367 err = bpf_map_update_elem(map->fd, &i, &fd, 0);
5368 if (err) {
5369 err = -errno;
5370 pr_warn("map '%s': failed to initialize slot [%d] to prog '%s' fd=%d: %s\n",
5371 map->name, i, targ_prog->name, fd, errstr(err));
5372 return err;
5373 }
5374 pr_debug("map '%s': slot [%d] set to prog '%s' fd=%d\n",
5375 map->name, i, targ_prog->name, fd);
5376 }
5377
5378 zfree(&map->init_slots);
5379 map->init_slots_sz = 0;
5380
5381 return 0;
5382 }
5383
bpf_object_init_prog_arrays(struct bpf_object * obj)5384 static int bpf_object_init_prog_arrays(struct bpf_object *obj)
5385 {
5386 struct bpf_map *map;
5387 int i, err;
5388
5389 for (i = 0; i < obj->nr_maps; i++) {
5390 map = &obj->maps[i];
5391
5392 if (!map->init_slots_sz || map->def.type != BPF_MAP_TYPE_PROG_ARRAY)
5393 continue;
5394
5395 err = init_prog_array_slots(obj, map);
5396 if (err < 0)
5397 return err;
5398 }
5399 return 0;
5400 }
5401
map_set_def_max_entries(struct bpf_map * map)5402 static int map_set_def_max_entries(struct bpf_map *map)
5403 {
5404 if (map->def.type == BPF_MAP_TYPE_PERF_EVENT_ARRAY && !map->def.max_entries) {
5405 int nr_cpus;
5406
5407 nr_cpus = libbpf_num_possible_cpus();
5408 if (nr_cpus < 0) {
5409 pr_warn("map '%s': failed to determine number of system CPUs: %d\n",
5410 map->name, nr_cpus);
5411 return nr_cpus;
5412 }
5413 pr_debug("map '%s': setting size to %d\n", map->name, nr_cpus);
5414 map->def.max_entries = nr_cpus;
5415 }
5416
5417 return 0;
5418 }
5419
5420 static int
bpf_object__create_maps(struct bpf_object * obj)5421 bpf_object__create_maps(struct bpf_object *obj)
5422 {
5423 struct bpf_map *map;
5424 unsigned int i, j;
5425 int err;
5426 bool retried;
5427
5428 for (i = 0; i < obj->nr_maps; i++) {
5429 map = &obj->maps[i];
5430
5431 /* To support old kernels, we skip creating global data maps
5432 * (.rodata, .data, .kconfig, etc); later on, during program
5433 * loading, if we detect that at least one of the to-be-loaded
5434 * programs is referencing any global data map, we'll error
5435 * out with program name and relocation index logged.
5436 * This approach allows to accommodate Clang emitting
5437 * unnecessary .rodata.str1.1 sections for string literals,
5438 * but also it allows to have CO-RE applications that use
5439 * global variables in some of BPF programs, but not others.
5440 * If those global variable-using programs are not loaded at
5441 * runtime due to bpf_program__set_autoload(prog, false),
5442 * bpf_object loading will succeed just fine even on old
5443 * kernels.
5444 */
5445 if (bpf_map__is_internal(map) && !kernel_supports(obj, FEAT_GLOBAL_DATA))
5446 map->autocreate = false;
5447
5448 if (!map->autocreate) {
5449 pr_debug("map '%s': skipped auto-creating...\n", map->name);
5450 continue;
5451 }
5452
5453 err = map_set_def_max_entries(map);
5454 if (err)
5455 goto err_out;
5456
5457 retried = false;
5458 retry:
5459 if (map->pin_path) {
5460 err = bpf_object__reuse_map(map);
5461 if (err) {
5462 pr_warn("map '%s': error reusing pinned map\n",
5463 map->name);
5464 goto err_out;
5465 }
5466 if (retried && map->fd < 0) {
5467 pr_warn("map '%s': cannot find pinned map\n",
5468 map->name);
5469 err = -ENOENT;
5470 goto err_out;
5471 }
5472 }
5473
5474 if (map->reused) {
5475 pr_debug("map '%s': skipping creation (preset fd=%d)\n",
5476 map->name, map->fd);
5477 } else {
5478 err = bpf_object__create_map(obj, map, false);
5479 if (err)
5480 goto err_out;
5481
5482 pr_debug("map '%s': created successfully, fd=%d\n",
5483 map->name, map->fd);
5484
5485 if (bpf_map__is_internal(map)) {
5486 err = bpf_object__populate_internal_map(obj, map);
5487 if (err < 0)
5488 goto err_out;
5489 } else if (map->def.type == BPF_MAP_TYPE_ARENA) {
5490 map->mmaped = mmap((void *)(long)map->map_extra,
5491 bpf_map_mmap_sz(map), PROT_READ | PROT_WRITE,
5492 map->map_extra ? MAP_SHARED | MAP_FIXED : MAP_SHARED,
5493 map->fd, 0);
5494 if (map->mmaped == MAP_FAILED) {
5495 err = -errno;
5496 map->mmaped = NULL;
5497 pr_warn("map '%s': failed to mmap arena: %s\n",
5498 map->name, errstr(err));
5499 return err;
5500 }
5501 if (obj->arena_data) {
5502 memcpy(map->mmaped, obj->arena_data, obj->arena_data_sz);
5503 zfree(&obj->arena_data);
5504 }
5505 }
5506 if (map->init_slots_sz && map->def.type != BPF_MAP_TYPE_PROG_ARRAY) {
5507 err = init_map_in_map_slots(obj, map);
5508 if (err < 0)
5509 goto err_out;
5510 }
5511 }
5512
5513 if (map->pin_path && !map->pinned) {
5514 err = bpf_map__pin(map, NULL);
5515 if (err) {
5516 if (!retried && err == -EEXIST) {
5517 retried = true;
5518 goto retry;
5519 }
5520 pr_warn("map '%s': failed to auto-pin at '%s': %s\n",
5521 map->name, map->pin_path, errstr(err));
5522 goto err_out;
5523 }
5524 }
5525 }
5526
5527 return 0;
5528
5529 err_out:
5530 pr_warn("map '%s': failed to create: %s\n", map->name, errstr(err));
5531 pr_perm_msg(err);
5532 for (j = 0; j < i; j++)
5533 zclose(obj->maps[j].fd);
5534 return err;
5535 }
5536
bpf_core_is_flavor_sep(const char * s)5537 static bool bpf_core_is_flavor_sep(const char *s)
5538 {
5539 /* check X___Y name pattern, where X and Y are not underscores */
5540 return s[0] != '_' && /* X */
5541 s[1] == '_' && s[2] == '_' && s[3] == '_' && /* ___ */
5542 s[4] != '_'; /* Y */
5543 }
5544
5545 /* Given 'some_struct_name___with_flavor' return the length of a name prefix
5546 * before last triple underscore. Struct name part after last triple
5547 * underscore is ignored by BPF CO-RE relocation during relocation matching.
5548 */
bpf_core_essential_name_len(const char * name)5549 size_t bpf_core_essential_name_len(const char *name)
5550 {
5551 size_t n = strlen(name);
5552 int i;
5553
5554 for (i = n - 5; i >= 0; i--) {
5555 if (bpf_core_is_flavor_sep(name + i))
5556 return i + 1;
5557 }
5558 return n;
5559 }
5560
bpf_core_free_cands(struct bpf_core_cand_list * cands)5561 void bpf_core_free_cands(struct bpf_core_cand_list *cands)
5562 {
5563 if (!cands)
5564 return;
5565
5566 free(cands->cands);
5567 free(cands);
5568 }
5569
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)5570 int bpf_core_add_cands(struct bpf_core_cand *local_cand,
5571 size_t local_essent_len,
5572 const struct btf *targ_btf,
5573 const char *targ_btf_name,
5574 int targ_start_id,
5575 struct bpf_core_cand_list *cands)
5576 {
5577 struct bpf_core_cand *new_cands, *cand;
5578 const struct btf_type *t, *local_t;
5579 const char *targ_name, *local_name;
5580 size_t targ_essent_len;
5581 int n, i;
5582
5583 local_t = btf__type_by_id(local_cand->btf, local_cand->id);
5584 local_name = btf__str_by_offset(local_cand->btf, local_t->name_off);
5585
5586 n = btf__type_cnt(targ_btf);
5587 for (i = targ_start_id; i < n; i++) {
5588 t = btf__type_by_id(targ_btf, i);
5589 if (!btf_kind_core_compat(t, local_t))
5590 continue;
5591
5592 targ_name = btf__name_by_offset(targ_btf, t->name_off);
5593 if (str_is_empty(targ_name))
5594 continue;
5595
5596 targ_essent_len = bpf_core_essential_name_len(targ_name);
5597 if (targ_essent_len != local_essent_len)
5598 continue;
5599
5600 if (strncmp(local_name, targ_name, local_essent_len) != 0)
5601 continue;
5602
5603 pr_debug("CO-RE relocating [%d] %s %s: found target candidate [%d] %s %s in [%s]\n",
5604 local_cand->id, btf_kind_str(local_t),
5605 local_name, i, btf_kind_str(t), targ_name,
5606 targ_btf_name);
5607 new_cands = libbpf_reallocarray(cands->cands, cands->len + 1,
5608 sizeof(*cands->cands));
5609 if (!new_cands)
5610 return -ENOMEM;
5611
5612 cand = &new_cands[cands->len];
5613 cand->btf = targ_btf;
5614 cand->id = i;
5615
5616 cands->cands = new_cands;
5617 cands->len++;
5618 }
5619 return 0;
5620 }
5621
load_module_btfs(struct bpf_object * obj)5622 static int load_module_btfs(struct bpf_object *obj)
5623 {
5624 struct bpf_btf_info info;
5625 struct module_btf *mod_btf;
5626 struct btf *btf;
5627 char name[64];
5628 __u32 id = 0, len;
5629 int err, fd;
5630
5631 if (obj->btf_modules_loaded)
5632 return 0;
5633
5634 if (obj->gen_loader)
5635 return 0;
5636
5637 /* don't do this again, even if we find no module BTFs */
5638 obj->btf_modules_loaded = true;
5639
5640 /* kernel too old to support module BTFs */
5641 if (!kernel_supports(obj, FEAT_MODULE_BTF))
5642 return 0;
5643
5644 while (true) {
5645 err = bpf_btf_get_next_id(id, &id);
5646 if (err && errno == ENOENT)
5647 return 0;
5648 if (err && errno == EPERM) {
5649 pr_debug("skipping module BTFs loading, missing privileges\n");
5650 return 0;
5651 }
5652 if (err) {
5653 err = -errno;
5654 pr_warn("failed to iterate BTF objects: %s\n", errstr(err));
5655 return err;
5656 }
5657
5658 fd = bpf_btf_get_fd_by_id(id);
5659 if (fd < 0) {
5660 if (errno == ENOENT)
5661 continue; /* expected race: BTF was unloaded */
5662 err = -errno;
5663 pr_warn("failed to get BTF object #%d FD: %s\n", id, errstr(err));
5664 return err;
5665 }
5666
5667 len = sizeof(info);
5668 memset(&info, 0, sizeof(info));
5669 info.name = ptr_to_u64(name);
5670 info.name_len = sizeof(name);
5671
5672 err = bpf_btf_get_info_by_fd(fd, &info, &len);
5673 if (err) {
5674 err = -errno;
5675 pr_warn("failed to get BTF object #%d info: %s\n", id, errstr(err));
5676 goto err_out;
5677 }
5678
5679 /* ignore non-module BTFs */
5680 if (!info.kernel_btf || strcmp(name, "vmlinux") == 0) {
5681 close(fd);
5682 continue;
5683 }
5684
5685 btf = btf_get_from_fd(fd, obj->btf_vmlinux);
5686 err = libbpf_get_error(btf);
5687 if (err) {
5688 pr_warn("failed to load module [%s]'s BTF object #%d: %s\n",
5689 name, id, errstr(err));
5690 goto err_out;
5691 }
5692
5693 err = libbpf_ensure_mem((void **)&obj->btf_modules, &obj->btf_module_cap,
5694 sizeof(*obj->btf_modules), obj->btf_module_cnt + 1);
5695 if (err)
5696 goto err_out;
5697
5698 mod_btf = &obj->btf_modules[obj->btf_module_cnt++];
5699
5700 mod_btf->btf = btf;
5701 mod_btf->id = id;
5702 mod_btf->fd = fd;
5703 mod_btf->name = strdup(name);
5704 if (!mod_btf->name) {
5705 err = -ENOMEM;
5706 goto err_out;
5707 }
5708 continue;
5709
5710 err_out:
5711 close(fd);
5712 return err;
5713 }
5714
5715 return 0;
5716 }
5717
5718 static struct bpf_core_cand_list *
bpf_core_find_cands(struct bpf_object * obj,const struct btf * local_btf,__u32 local_type_id)5719 bpf_core_find_cands(struct bpf_object *obj, const struct btf *local_btf, __u32 local_type_id)
5720 {
5721 struct bpf_core_cand local_cand = {};
5722 struct bpf_core_cand_list *cands;
5723 const struct btf *main_btf;
5724 const struct btf_type *local_t;
5725 const char *local_name;
5726 size_t local_essent_len;
5727 int err, i;
5728
5729 local_cand.btf = local_btf;
5730 local_cand.id = local_type_id;
5731 local_t = btf__type_by_id(local_btf, local_type_id);
5732 if (!local_t)
5733 return ERR_PTR(-EINVAL);
5734
5735 local_name = btf__name_by_offset(local_btf, local_t->name_off);
5736 if (str_is_empty(local_name))
5737 return ERR_PTR(-EINVAL);
5738 local_essent_len = bpf_core_essential_name_len(local_name);
5739
5740 cands = calloc(1, sizeof(*cands));
5741 if (!cands)
5742 return ERR_PTR(-ENOMEM);
5743
5744 /* Attempt to find target candidates in vmlinux BTF first */
5745 main_btf = obj->btf_vmlinux_override ?: obj->btf_vmlinux;
5746 err = bpf_core_add_cands(&local_cand, local_essent_len, main_btf, "vmlinux", 1, cands);
5747 if (err)
5748 goto err_out;
5749
5750 /* if vmlinux BTF has any candidate, don't got for module BTFs */
5751 if (cands->len)
5752 return cands;
5753
5754 /* if vmlinux BTF was overridden, don't attempt to load module BTFs */
5755 if (obj->btf_vmlinux_override)
5756 return cands;
5757
5758 /* now look through module BTFs, trying to still find candidates */
5759 err = load_module_btfs(obj);
5760 if (err)
5761 goto err_out;
5762
5763 for (i = 0; i < obj->btf_module_cnt; i++) {
5764 err = bpf_core_add_cands(&local_cand, local_essent_len,
5765 obj->btf_modules[i].btf,
5766 obj->btf_modules[i].name,
5767 btf__type_cnt(obj->btf_vmlinux),
5768 cands);
5769 if (err)
5770 goto err_out;
5771 }
5772
5773 return cands;
5774 err_out:
5775 bpf_core_free_cands(cands);
5776 return ERR_PTR(err);
5777 }
5778
5779 /* Check local and target types for compatibility. This check is used for
5780 * type-based CO-RE relocations and follow slightly different rules than
5781 * field-based relocations. This function assumes that root types were already
5782 * checked for name match. Beyond that initial root-level name check, names
5783 * are completely ignored. Compatibility rules are as follows:
5784 * - any two STRUCTs/UNIONs/FWDs/ENUMs/INTs are considered compatible, but
5785 * kind should match for local and target types (i.e., STRUCT is not
5786 * compatible with UNION);
5787 * - for ENUMs, the size is ignored;
5788 * - for INT, size and signedness are ignored;
5789 * - for ARRAY, dimensionality is ignored, element types are checked for
5790 * compatibility recursively;
5791 * - CONST/VOLATILE/RESTRICT modifiers are ignored;
5792 * - TYPEDEFs/PTRs are compatible if types they pointing to are compatible;
5793 * - FUNC_PROTOs are compatible if they have compatible signature: same
5794 * number of input args and compatible return and argument types.
5795 * These rules are not set in stone and probably will be adjusted as we get
5796 * more experience with using BPF CO-RE relocations.
5797 */
bpf_core_types_are_compat(const struct btf * local_btf,__u32 local_id,const struct btf * targ_btf,__u32 targ_id)5798 int bpf_core_types_are_compat(const struct btf *local_btf, __u32 local_id,
5799 const struct btf *targ_btf, __u32 targ_id)
5800 {
5801 return __bpf_core_types_are_compat(local_btf, local_id, targ_btf, targ_id, 32);
5802 }
5803
bpf_core_types_match(const struct btf * local_btf,__u32 local_id,const struct btf * targ_btf,__u32 targ_id)5804 int bpf_core_types_match(const struct btf *local_btf, __u32 local_id,
5805 const struct btf *targ_btf, __u32 targ_id)
5806 {
5807 return __bpf_core_types_match(local_btf, local_id, targ_btf, targ_id, false, 32);
5808 }
5809
bpf_core_hash_fn(const long key,void * ctx)5810 static size_t bpf_core_hash_fn(const long key, void *ctx)
5811 {
5812 return key;
5813 }
5814
bpf_core_equal_fn(const long k1,const long k2,void * ctx)5815 static bool bpf_core_equal_fn(const long k1, const long k2, void *ctx)
5816 {
5817 return k1 == k2;
5818 }
5819
record_relo_core(struct bpf_program * prog,const struct bpf_core_relo * core_relo,int insn_idx)5820 static int record_relo_core(struct bpf_program *prog,
5821 const struct bpf_core_relo *core_relo, int insn_idx)
5822 {
5823 struct reloc_desc *relos, *relo;
5824
5825 relos = libbpf_reallocarray(prog->reloc_desc,
5826 prog->nr_reloc + 1, sizeof(*relos));
5827 if (!relos)
5828 return -ENOMEM;
5829 relo = &relos[prog->nr_reloc];
5830 relo->type = RELO_CORE;
5831 relo->insn_idx = insn_idx;
5832 relo->core_relo = core_relo;
5833 prog->reloc_desc = relos;
5834 prog->nr_reloc++;
5835 return 0;
5836 }
5837
find_relo_core(struct bpf_program * prog,int insn_idx)5838 static const struct bpf_core_relo *find_relo_core(struct bpf_program *prog, int insn_idx)
5839 {
5840 struct reloc_desc *relo;
5841 int i;
5842
5843 for (i = 0; i < prog->nr_reloc; i++) {
5844 relo = &prog->reloc_desc[i];
5845 if (relo->type != RELO_CORE || relo->insn_idx != insn_idx)
5846 continue;
5847
5848 return relo->core_relo;
5849 }
5850
5851 return NULL;
5852 }
5853
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)5854 static int bpf_core_resolve_relo(struct bpf_program *prog,
5855 const struct bpf_core_relo *relo,
5856 int relo_idx,
5857 const struct btf *local_btf,
5858 struct hashmap *cand_cache,
5859 struct bpf_core_relo_res *targ_res)
5860 {
5861 struct bpf_core_spec specs_scratch[3] = {};
5862 struct bpf_core_cand_list *cands = NULL;
5863 const char *prog_name = prog->name;
5864 const struct btf_type *local_type;
5865 const char *local_name;
5866 __u32 local_id = relo->type_id;
5867 int err;
5868
5869 local_type = btf__type_by_id(local_btf, local_id);
5870 if (!local_type)
5871 return -EINVAL;
5872
5873 local_name = btf__name_by_offset(local_btf, local_type->name_off);
5874 if (!local_name)
5875 return -EINVAL;
5876
5877 if (relo->kind != BPF_CORE_TYPE_ID_LOCAL &&
5878 !hashmap__find(cand_cache, local_id, &cands)) {
5879 cands = bpf_core_find_cands(prog->obj, local_btf, local_id);
5880 if (IS_ERR(cands)) {
5881 pr_warn("prog '%s': relo #%d: target candidate search failed for [%d] %s %s: %ld\n",
5882 prog_name, relo_idx, local_id, btf_kind_str(local_type),
5883 local_name, PTR_ERR(cands));
5884 return PTR_ERR(cands);
5885 }
5886 err = hashmap__set(cand_cache, local_id, cands, NULL, NULL);
5887 if (err) {
5888 bpf_core_free_cands(cands);
5889 return err;
5890 }
5891 }
5892
5893 return bpf_core_calc_relo_insn(prog_name, relo, relo_idx, local_btf, cands, specs_scratch,
5894 targ_res);
5895 }
5896
5897 static int
bpf_object__relocate_core(struct bpf_object * obj,const char * targ_btf_path)5898 bpf_object__relocate_core(struct bpf_object *obj, const char *targ_btf_path)
5899 {
5900 const struct btf_ext_info_sec *sec;
5901 struct bpf_core_relo_res targ_res;
5902 const struct bpf_core_relo *rec;
5903 const struct btf_ext_info *seg;
5904 struct hashmap_entry *entry;
5905 struct hashmap *cand_cache = NULL;
5906 struct bpf_program *prog;
5907 struct bpf_insn *insn;
5908 const char *sec_name;
5909 int i, err = 0, insn_idx, sec_idx, sec_num;
5910
5911 if (obj->btf_ext->core_relo_info.len == 0)
5912 return 0;
5913
5914 if (targ_btf_path) {
5915 obj->btf_vmlinux_override = btf__parse(targ_btf_path, NULL);
5916 err = libbpf_get_error(obj->btf_vmlinux_override);
5917 if (err) {
5918 pr_warn("failed to parse target BTF: %s\n", errstr(err));
5919 return err;
5920 }
5921 }
5922
5923 cand_cache = hashmap__new(bpf_core_hash_fn, bpf_core_equal_fn, NULL);
5924 if (IS_ERR(cand_cache)) {
5925 err = PTR_ERR(cand_cache);
5926 goto out;
5927 }
5928
5929 seg = &obj->btf_ext->core_relo_info;
5930 sec_num = 0;
5931 for_each_btf_ext_sec(seg, sec) {
5932 sec_idx = seg->sec_idxs[sec_num];
5933 sec_num++;
5934
5935 sec_name = btf__name_by_offset(obj->btf, sec->sec_name_off);
5936 if (str_is_empty(sec_name)) {
5937 err = -EINVAL;
5938 goto out;
5939 }
5940
5941 pr_debug("sec '%s': found %d CO-RE relocations\n", sec_name, sec->num_info);
5942
5943 for_each_btf_ext_rec(seg, sec, i, rec) {
5944 if (rec->insn_off % BPF_INSN_SZ)
5945 return -EINVAL;
5946 insn_idx = rec->insn_off / BPF_INSN_SZ;
5947 prog = find_prog_by_sec_insn(obj, sec_idx, insn_idx);
5948 if (!prog) {
5949 /* When __weak subprog is "overridden" by another instance
5950 * of the subprog from a different object file, linker still
5951 * appends all the .BTF.ext info that used to belong to that
5952 * eliminated subprogram.
5953 * This is similar to what x86-64 linker does for relocations.
5954 * So just ignore such relocations just like we ignore
5955 * subprog instructions when discovering subprograms.
5956 */
5957 pr_debug("sec '%s': skipping CO-RE relocation #%d for insn #%d belonging to eliminated weak subprogram\n",
5958 sec_name, i, insn_idx);
5959 continue;
5960 }
5961 /* no need to apply CO-RE relocation if the program is
5962 * not going to be loaded
5963 */
5964 if (!prog->autoload)
5965 continue;
5966
5967 /* adjust insn_idx from section frame of reference to the local
5968 * program's frame of reference; (sub-)program code is not yet
5969 * relocated, so it's enough to just subtract in-section offset
5970 */
5971 insn_idx = insn_idx - prog->sec_insn_off;
5972 if (insn_idx >= prog->insns_cnt)
5973 return -EINVAL;
5974 insn = &prog->insns[insn_idx];
5975
5976 err = record_relo_core(prog, rec, insn_idx);
5977 if (err) {
5978 pr_warn("prog '%s': relo #%d: failed to record relocation: %s\n",
5979 prog->name, i, errstr(err));
5980 goto out;
5981 }
5982
5983 if (prog->obj->gen_loader)
5984 continue;
5985
5986 err = bpf_core_resolve_relo(prog, rec, i, obj->btf, cand_cache, &targ_res);
5987 if (err) {
5988 pr_warn("prog '%s': relo #%d: failed to relocate: %s\n",
5989 prog->name, i, errstr(err));
5990 goto out;
5991 }
5992
5993 err = bpf_core_patch_insn(prog->name, insn, insn_idx, rec, i, &targ_res);
5994 if (err) {
5995 pr_warn("prog '%s': relo #%d: failed to patch insn #%u: %s\n",
5996 prog->name, i, insn_idx, errstr(err));
5997 goto out;
5998 }
5999 }
6000 }
6001
6002 out:
6003 /* obj->btf_vmlinux and module BTFs are freed after object load */
6004 btf__free(obj->btf_vmlinux_override);
6005 obj->btf_vmlinux_override = NULL;
6006
6007 if (!IS_ERR_OR_NULL(cand_cache)) {
6008 hashmap__for_each_entry(cand_cache, entry, i) {
6009 bpf_core_free_cands(entry->pvalue);
6010 }
6011 hashmap__free(cand_cache);
6012 }
6013 return err;
6014 }
6015
6016 /* base map load ldimm64 special constant, used also for log fixup logic */
6017 #define POISON_LDIMM64_MAP_BASE 2001000000
6018 #define POISON_LDIMM64_MAP_PFX "200100"
6019
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)6020 static void poison_map_ldimm64(struct bpf_program *prog, int relo_idx,
6021 int insn_idx, struct bpf_insn *insn,
6022 int map_idx, const struct bpf_map *map)
6023 {
6024 int i;
6025
6026 pr_debug("prog '%s': relo #%d: poisoning insn #%d that loads map #%d '%s'\n",
6027 prog->name, relo_idx, insn_idx, map_idx, map->name);
6028
6029 /* we turn single ldimm64 into two identical invalid calls */
6030 for (i = 0; i < 2; i++) {
6031 insn->code = BPF_JMP | BPF_CALL;
6032 insn->dst_reg = 0;
6033 insn->src_reg = 0;
6034 insn->off = 0;
6035 /* if this instruction is reachable (not a dead code),
6036 * verifier will complain with something like:
6037 * invalid func unknown#2001000123
6038 * where lower 123 is map index into obj->maps[] array
6039 */
6040 insn->imm = POISON_LDIMM64_MAP_BASE + map_idx;
6041
6042 insn++;
6043 }
6044 }
6045
6046 /* unresolved kfunc call special constant, used also for log fixup logic */
6047 #define POISON_CALL_KFUNC_BASE 2002000000
6048 #define POISON_CALL_KFUNC_PFX "2002"
6049
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)6050 static void poison_kfunc_call(struct bpf_program *prog, int relo_idx,
6051 int insn_idx, struct bpf_insn *insn,
6052 int ext_idx, const struct extern_desc *ext)
6053 {
6054 pr_debug("prog '%s': relo #%d: poisoning insn #%d that calls kfunc '%s'\n",
6055 prog->name, relo_idx, insn_idx, ext->name);
6056
6057 /* we turn kfunc call into invalid helper call with identifiable constant */
6058 insn->code = BPF_JMP | BPF_CALL;
6059 insn->dst_reg = 0;
6060 insn->src_reg = 0;
6061 insn->off = 0;
6062 /* if this instruction is reachable (not a dead code),
6063 * verifier will complain with something like:
6064 * invalid func unknown#2001000123
6065 * where lower 123 is extern index into obj->externs[] array
6066 */
6067 insn->imm = POISON_CALL_KFUNC_BASE + ext_idx;
6068 }
6069
6070 /* Relocate data references within program code:
6071 * - map references;
6072 * - global variable references;
6073 * - extern references.
6074 */
6075 static int
bpf_object__relocate_data(struct bpf_object * obj,struct bpf_program * prog)6076 bpf_object__relocate_data(struct bpf_object *obj, struct bpf_program *prog)
6077 {
6078 int i;
6079
6080 for (i = 0; i < prog->nr_reloc; i++) {
6081 struct reloc_desc *relo = &prog->reloc_desc[i];
6082 struct bpf_insn *insn = &prog->insns[relo->insn_idx];
6083 const struct bpf_map *map;
6084 struct extern_desc *ext;
6085
6086 switch (relo->type) {
6087 case RELO_LD64:
6088 map = &obj->maps[relo->map_idx];
6089 if (obj->gen_loader) {
6090 insn[0].src_reg = BPF_PSEUDO_MAP_IDX;
6091 insn[0].imm = relo->map_idx;
6092 } else if (map->autocreate) {
6093 insn[0].src_reg = BPF_PSEUDO_MAP_FD;
6094 insn[0].imm = map->fd;
6095 } else {
6096 poison_map_ldimm64(prog, i, relo->insn_idx, insn,
6097 relo->map_idx, map);
6098 }
6099 break;
6100 case RELO_DATA:
6101 map = &obj->maps[relo->map_idx];
6102 insn[1].imm = insn[0].imm + relo->sym_off;
6103 if (obj->gen_loader) {
6104 insn[0].src_reg = BPF_PSEUDO_MAP_IDX_VALUE;
6105 insn[0].imm = relo->map_idx;
6106 } else if (map->autocreate) {
6107 insn[0].src_reg = BPF_PSEUDO_MAP_VALUE;
6108 insn[0].imm = map->fd;
6109 } else {
6110 poison_map_ldimm64(prog, i, relo->insn_idx, insn,
6111 relo->map_idx, map);
6112 }
6113 break;
6114 case RELO_EXTERN_LD64:
6115 ext = &obj->externs[relo->ext_idx];
6116 if (ext->type == EXT_KCFG) {
6117 if (obj->gen_loader) {
6118 insn[0].src_reg = BPF_PSEUDO_MAP_IDX_VALUE;
6119 insn[0].imm = obj->kconfig_map_idx;
6120 } else {
6121 insn[0].src_reg = BPF_PSEUDO_MAP_VALUE;
6122 insn[0].imm = obj->maps[obj->kconfig_map_idx].fd;
6123 }
6124 insn[1].imm = ext->kcfg.data_off;
6125 } else /* EXT_KSYM */ {
6126 if (ext->ksym.type_id && ext->is_set) { /* typed ksyms */
6127 insn[0].src_reg = BPF_PSEUDO_BTF_ID;
6128 insn[0].imm = ext->ksym.kernel_btf_id;
6129 insn[1].imm = ext->ksym.kernel_btf_obj_fd;
6130 } else { /* typeless ksyms or unresolved typed ksyms */
6131 insn[0].imm = (__u32)ext->ksym.addr;
6132 insn[1].imm = ext->ksym.addr >> 32;
6133 }
6134 }
6135 break;
6136 case RELO_EXTERN_CALL:
6137 ext = &obj->externs[relo->ext_idx];
6138 insn[0].src_reg = BPF_PSEUDO_KFUNC_CALL;
6139 if (ext->is_set) {
6140 insn[0].imm = ext->ksym.kernel_btf_id;
6141 insn[0].off = ext->ksym.btf_fd_idx;
6142 } else { /* unresolved weak kfunc call */
6143 poison_kfunc_call(prog, i, relo->insn_idx, insn,
6144 relo->ext_idx, ext);
6145 }
6146 break;
6147 case RELO_SUBPROG_ADDR:
6148 if (insn[0].src_reg != BPF_PSEUDO_FUNC) {
6149 pr_warn("prog '%s': relo #%d: bad insn\n",
6150 prog->name, i);
6151 return -EINVAL;
6152 }
6153 /* handled already */
6154 break;
6155 case RELO_CALL:
6156 /* handled already */
6157 break;
6158 case RELO_CORE:
6159 /* will be handled by bpf_program_record_relos() */
6160 break;
6161 default:
6162 pr_warn("prog '%s': relo #%d: bad relo type %d\n",
6163 prog->name, i, relo->type);
6164 return -EINVAL;
6165 }
6166 }
6167
6168 return 0;
6169 }
6170
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)6171 static int adjust_prog_btf_ext_info(const struct bpf_object *obj,
6172 const struct bpf_program *prog,
6173 const struct btf_ext_info *ext_info,
6174 void **prog_info, __u32 *prog_rec_cnt,
6175 __u32 *prog_rec_sz)
6176 {
6177 void *copy_start = NULL, *copy_end = NULL;
6178 void *rec, *rec_end, *new_prog_info;
6179 const struct btf_ext_info_sec *sec;
6180 size_t old_sz, new_sz;
6181 int i, sec_num, sec_idx, off_adj;
6182
6183 sec_num = 0;
6184 for_each_btf_ext_sec(ext_info, sec) {
6185 sec_idx = ext_info->sec_idxs[sec_num];
6186 sec_num++;
6187 if (prog->sec_idx != sec_idx)
6188 continue;
6189
6190 for_each_btf_ext_rec(ext_info, sec, i, rec) {
6191 __u32 insn_off = *(__u32 *)rec / BPF_INSN_SZ;
6192
6193 if (insn_off < prog->sec_insn_off)
6194 continue;
6195 if (insn_off >= prog->sec_insn_off + prog->sec_insn_cnt)
6196 break;
6197
6198 if (!copy_start)
6199 copy_start = rec;
6200 copy_end = rec + ext_info->rec_size;
6201 }
6202
6203 if (!copy_start)
6204 return -ENOENT;
6205
6206 /* append func/line info of a given (sub-)program to the main
6207 * program func/line info
6208 */
6209 old_sz = (size_t)(*prog_rec_cnt) * ext_info->rec_size;
6210 new_sz = old_sz + (copy_end - copy_start);
6211 new_prog_info = realloc(*prog_info, new_sz);
6212 if (!new_prog_info)
6213 return -ENOMEM;
6214 *prog_info = new_prog_info;
6215 *prog_rec_cnt = new_sz / ext_info->rec_size;
6216 memcpy(new_prog_info + old_sz, copy_start, copy_end - copy_start);
6217
6218 /* Kernel instruction offsets are in units of 8-byte
6219 * instructions, while .BTF.ext instruction offsets generated
6220 * by Clang are in units of bytes. So convert Clang offsets
6221 * into kernel offsets and adjust offset according to program
6222 * relocated position.
6223 */
6224 off_adj = prog->sub_insn_off - prog->sec_insn_off;
6225 rec = new_prog_info + old_sz;
6226 rec_end = new_prog_info + new_sz;
6227 for (; rec < rec_end; rec += ext_info->rec_size) {
6228 __u32 *insn_off = rec;
6229
6230 *insn_off = *insn_off / BPF_INSN_SZ + off_adj;
6231 }
6232 *prog_rec_sz = ext_info->rec_size;
6233 return 0;
6234 }
6235
6236 return -ENOENT;
6237 }
6238
6239 static int
reloc_prog_func_and_line_info(const struct bpf_object * obj,struct bpf_program * main_prog,const struct bpf_program * prog)6240 reloc_prog_func_and_line_info(const struct bpf_object *obj,
6241 struct bpf_program *main_prog,
6242 const struct bpf_program *prog)
6243 {
6244 int err;
6245
6246 /* no .BTF.ext relocation if .BTF.ext is missing or kernel doesn't
6247 * support func/line info
6248 */
6249 if (!obj->btf_ext || !kernel_supports(obj, FEAT_BTF_FUNC))
6250 return 0;
6251
6252 /* only attempt func info relocation if main program's func_info
6253 * relocation was successful
6254 */
6255 if (main_prog != prog && !main_prog->func_info)
6256 goto line_info;
6257
6258 err = adjust_prog_btf_ext_info(obj, prog, &obj->btf_ext->func_info,
6259 &main_prog->func_info,
6260 &main_prog->func_info_cnt,
6261 &main_prog->func_info_rec_size);
6262 if (err) {
6263 if (err != -ENOENT) {
6264 pr_warn("prog '%s': error relocating .BTF.ext function info: %s\n",
6265 prog->name, errstr(err));
6266 return err;
6267 }
6268 if (main_prog->func_info) {
6269 /*
6270 * Some info has already been found but has problem
6271 * in the last btf_ext reloc. Must have to error out.
6272 */
6273 pr_warn("prog '%s': missing .BTF.ext function info.\n", prog->name);
6274 return err;
6275 }
6276 /* Have problem loading the very first info. Ignore the rest. */
6277 pr_warn("prog '%s': missing .BTF.ext function info for the main program, skipping all of .BTF.ext func info.\n",
6278 prog->name);
6279 }
6280
6281 line_info:
6282 /* don't relocate line info if main program's relocation failed */
6283 if (main_prog != prog && !main_prog->line_info)
6284 return 0;
6285
6286 err = adjust_prog_btf_ext_info(obj, prog, &obj->btf_ext->line_info,
6287 &main_prog->line_info,
6288 &main_prog->line_info_cnt,
6289 &main_prog->line_info_rec_size);
6290 if (err) {
6291 if (err != -ENOENT) {
6292 pr_warn("prog '%s': error relocating .BTF.ext line info: %s\n",
6293 prog->name, errstr(err));
6294 return err;
6295 }
6296 if (main_prog->line_info) {
6297 /*
6298 * Some info has already been found but has problem
6299 * in the last btf_ext reloc. Must have to error out.
6300 */
6301 pr_warn("prog '%s': missing .BTF.ext line info.\n", prog->name);
6302 return err;
6303 }
6304 /* Have problem loading the very first info. Ignore the rest. */
6305 pr_warn("prog '%s': missing .BTF.ext line info for the main program, skipping all of .BTF.ext line info.\n",
6306 prog->name);
6307 }
6308 return 0;
6309 }
6310
cmp_relo_by_insn_idx(const void * key,const void * elem)6311 static int cmp_relo_by_insn_idx(const void *key, const void *elem)
6312 {
6313 size_t insn_idx = *(const size_t *)key;
6314 const struct reloc_desc *relo = elem;
6315
6316 if (insn_idx == relo->insn_idx)
6317 return 0;
6318 return insn_idx < relo->insn_idx ? -1 : 1;
6319 }
6320
find_prog_insn_relo(const struct bpf_program * prog,size_t insn_idx)6321 static struct reloc_desc *find_prog_insn_relo(const struct bpf_program *prog, size_t insn_idx)
6322 {
6323 if (!prog->nr_reloc)
6324 return NULL;
6325 return bsearch(&insn_idx, prog->reloc_desc, prog->nr_reloc,
6326 sizeof(*prog->reloc_desc), cmp_relo_by_insn_idx);
6327 }
6328
append_subprog_relos(struct bpf_program * main_prog,struct bpf_program * subprog)6329 static int append_subprog_relos(struct bpf_program *main_prog, struct bpf_program *subprog)
6330 {
6331 int new_cnt = main_prog->nr_reloc + subprog->nr_reloc;
6332 struct reloc_desc *relos;
6333 int i;
6334
6335 if (main_prog == subprog)
6336 return 0;
6337 relos = libbpf_reallocarray(main_prog->reloc_desc, new_cnt, sizeof(*relos));
6338 /* if new count is zero, reallocarray can return a valid NULL result;
6339 * in this case the previous pointer will be freed, so we *have to*
6340 * reassign old pointer to the new value (even if it's NULL)
6341 */
6342 if (!relos && new_cnt)
6343 return -ENOMEM;
6344 if (subprog->nr_reloc)
6345 memcpy(relos + main_prog->nr_reloc, subprog->reloc_desc,
6346 sizeof(*relos) * subprog->nr_reloc);
6347
6348 for (i = main_prog->nr_reloc; i < new_cnt; i++)
6349 relos[i].insn_idx += subprog->sub_insn_off;
6350 /* After insn_idx adjustment the 'relos' array is still sorted
6351 * by insn_idx and doesn't break bsearch.
6352 */
6353 main_prog->reloc_desc = relos;
6354 main_prog->nr_reloc = new_cnt;
6355 return 0;
6356 }
6357
6358 static int
bpf_object__append_subprog_code(struct bpf_object * obj,struct bpf_program * main_prog,struct bpf_program * subprog)6359 bpf_object__append_subprog_code(struct bpf_object *obj, struct bpf_program *main_prog,
6360 struct bpf_program *subprog)
6361 {
6362 struct bpf_insn *insns;
6363 size_t new_cnt;
6364 int err;
6365
6366 subprog->sub_insn_off = main_prog->insns_cnt;
6367
6368 new_cnt = main_prog->insns_cnt + subprog->insns_cnt;
6369 insns = libbpf_reallocarray(main_prog->insns, new_cnt, sizeof(*insns));
6370 if (!insns) {
6371 pr_warn("prog '%s': failed to realloc prog code\n", main_prog->name);
6372 return -ENOMEM;
6373 }
6374 main_prog->insns = insns;
6375 main_prog->insns_cnt = new_cnt;
6376
6377 memcpy(main_prog->insns + subprog->sub_insn_off, subprog->insns,
6378 subprog->insns_cnt * sizeof(*insns));
6379
6380 pr_debug("prog '%s': added %zu insns from sub-prog '%s'\n",
6381 main_prog->name, subprog->insns_cnt, subprog->name);
6382
6383 /* The subprog insns are now appended. Append its relos too. */
6384 err = append_subprog_relos(main_prog, subprog);
6385 if (err)
6386 return err;
6387 return 0;
6388 }
6389
6390 static int
bpf_object__reloc_code(struct bpf_object * obj,struct bpf_program * main_prog,struct bpf_program * prog)6391 bpf_object__reloc_code(struct bpf_object *obj, struct bpf_program *main_prog,
6392 struct bpf_program *prog)
6393 {
6394 size_t sub_insn_idx, insn_idx;
6395 struct bpf_program *subprog;
6396 struct reloc_desc *relo;
6397 struct bpf_insn *insn;
6398 int err;
6399
6400 err = reloc_prog_func_and_line_info(obj, main_prog, prog);
6401 if (err)
6402 return err;
6403
6404 for (insn_idx = 0; insn_idx < prog->sec_insn_cnt; insn_idx++) {
6405 insn = &main_prog->insns[prog->sub_insn_off + insn_idx];
6406 if (!insn_is_subprog_call(insn) && !insn_is_pseudo_func(insn))
6407 continue;
6408
6409 relo = find_prog_insn_relo(prog, insn_idx);
6410 if (relo && relo->type == RELO_EXTERN_CALL)
6411 /* kfunc relocations will be handled later
6412 * in bpf_object__relocate_data()
6413 */
6414 continue;
6415 if (relo && relo->type != RELO_CALL && relo->type != RELO_SUBPROG_ADDR) {
6416 pr_warn("prog '%s': unexpected relo for insn #%zu, type %d\n",
6417 prog->name, insn_idx, relo->type);
6418 return -LIBBPF_ERRNO__RELOC;
6419 }
6420 if (relo) {
6421 /* sub-program instruction index is a combination of
6422 * an offset of a symbol pointed to by relocation and
6423 * call instruction's imm field; for global functions,
6424 * call always has imm = -1, but for static functions
6425 * relocation is against STT_SECTION and insn->imm
6426 * points to a start of a static function
6427 *
6428 * for subprog addr relocation, the relo->sym_off + insn->imm is
6429 * the byte offset in the corresponding section.
6430 */
6431 if (relo->type == RELO_CALL)
6432 sub_insn_idx = relo->sym_off / BPF_INSN_SZ + insn->imm + 1;
6433 else
6434 sub_insn_idx = (relo->sym_off + insn->imm) / BPF_INSN_SZ;
6435 } else if (insn_is_pseudo_func(insn)) {
6436 /*
6437 * RELO_SUBPROG_ADDR relo is always emitted even if both
6438 * functions are in the same section, so it shouldn't reach here.
6439 */
6440 pr_warn("prog '%s': missing subprog addr relo for insn #%zu\n",
6441 prog->name, insn_idx);
6442 return -LIBBPF_ERRNO__RELOC;
6443 } else {
6444 /* if subprogram call is to a static function within
6445 * the same ELF section, there won't be any relocation
6446 * emitted, but it also means there is no additional
6447 * offset necessary, insns->imm is relative to
6448 * instruction's original position within the section
6449 */
6450 sub_insn_idx = prog->sec_insn_off + insn_idx + insn->imm + 1;
6451 }
6452
6453 /* we enforce that sub-programs should be in .text section */
6454 subprog = find_prog_by_sec_insn(obj, obj->efile.text_shndx, sub_insn_idx);
6455 if (!subprog) {
6456 pr_warn("prog '%s': no .text section found yet sub-program call exists\n",
6457 prog->name);
6458 return -LIBBPF_ERRNO__RELOC;
6459 }
6460
6461 /* if it's the first call instruction calling into this
6462 * subprogram (meaning this subprog hasn't been processed
6463 * yet) within the context of current main program:
6464 * - append it at the end of main program's instructions blog;
6465 * - process is recursively, while current program is put on hold;
6466 * - if that subprogram calls some other not yet processes
6467 * subprogram, same thing will happen recursively until
6468 * there are no more unprocesses subprograms left to append
6469 * and relocate.
6470 */
6471 if (subprog->sub_insn_off == 0) {
6472 err = bpf_object__append_subprog_code(obj, main_prog, subprog);
6473 if (err)
6474 return err;
6475 err = bpf_object__reloc_code(obj, main_prog, subprog);
6476 if (err)
6477 return err;
6478 }
6479
6480 /* main_prog->insns memory could have been re-allocated, so
6481 * calculate pointer again
6482 */
6483 insn = &main_prog->insns[prog->sub_insn_off + insn_idx];
6484 /* calculate correct instruction position within current main
6485 * prog; each main prog can have a different set of
6486 * subprograms appended (potentially in different order as
6487 * well), so position of any subprog can be different for
6488 * different main programs
6489 */
6490 insn->imm = subprog->sub_insn_off - (prog->sub_insn_off + insn_idx) - 1;
6491
6492 pr_debug("prog '%s': insn #%zu relocated, imm %d points to subprog '%s' (now at %zu offset)\n",
6493 prog->name, insn_idx, insn->imm, subprog->name, subprog->sub_insn_off);
6494 }
6495
6496 return 0;
6497 }
6498
6499 /*
6500 * Relocate sub-program calls.
6501 *
6502 * Algorithm operates as follows. Each entry-point BPF program (referred to as
6503 * main prog) is processed separately. For each subprog (non-entry functions,
6504 * that can be called from either entry progs or other subprogs) gets their
6505 * sub_insn_off reset to zero. This serves as indicator that this subprogram
6506 * hasn't been yet appended and relocated within current main prog. Once its
6507 * relocated, sub_insn_off will point at the position within current main prog
6508 * where given subprog was appended. This will further be used to relocate all
6509 * the call instructions jumping into this subprog.
6510 *
6511 * We start with main program and process all call instructions. If the call
6512 * is into a subprog that hasn't been processed (i.e., subprog->sub_insn_off
6513 * is zero), subprog instructions are appended at the end of main program's
6514 * instruction array. Then main program is "put on hold" while we recursively
6515 * process newly appended subprogram. If that subprogram calls into another
6516 * subprogram that hasn't been appended, new subprogram is appended again to
6517 * the *main* prog's instructions (subprog's instructions are always left
6518 * untouched, as they need to be in unmodified state for subsequent main progs
6519 * and subprog instructions are always sent only as part of a main prog) and
6520 * the process continues recursively. Once all the subprogs called from a main
6521 * prog or any of its subprogs are appended (and relocated), all their
6522 * positions within finalized instructions array are known, so it's easy to
6523 * rewrite call instructions with correct relative offsets, corresponding to
6524 * desired target subprog.
6525 *
6526 * Its important to realize that some subprogs might not be called from some
6527 * main prog and any of its called/used subprogs. Those will keep their
6528 * subprog->sub_insn_off as zero at all times and won't be appended to current
6529 * main prog and won't be relocated within the context of current main prog.
6530 * They might still be used from other main progs later.
6531 *
6532 * Visually this process can be shown as below. Suppose we have two main
6533 * programs mainA and mainB and BPF object contains three subprogs: subA,
6534 * subB, and subC. mainA calls only subA, mainB calls only subC, but subA and
6535 * subC both call subB:
6536 *
6537 * +--------+ +-------+
6538 * | v v |
6539 * +--+---+ +--+-+-+ +---+--+
6540 * | subA | | subB | | subC |
6541 * +--+---+ +------+ +---+--+
6542 * ^ ^
6543 * | |
6544 * +---+-------+ +------+----+
6545 * | mainA | | mainB |
6546 * +-----------+ +-----------+
6547 *
6548 * We'll start relocating mainA, will find subA, append it and start
6549 * processing sub A recursively:
6550 *
6551 * +-----------+------+
6552 * | mainA | subA |
6553 * +-----------+------+
6554 *
6555 * At this point we notice that subB is used from subA, so we append it and
6556 * relocate (there are no further subcalls from subB):
6557 *
6558 * +-----------+------+------+
6559 * | mainA | subA | subB |
6560 * +-----------+------+------+
6561 *
6562 * At this point, we relocate subA calls, then go one level up and finish with
6563 * relocatin mainA calls. mainA is done.
6564 *
6565 * For mainB process is similar but results in different order. We start with
6566 * mainB and skip subA and subB, as mainB never calls them (at least
6567 * directly), but we see subC is needed, so we append and start processing it:
6568 *
6569 * +-----------+------+
6570 * | mainB | subC |
6571 * +-----------+------+
6572 * Now we see subC needs subB, so we go back to it, append and relocate it:
6573 *
6574 * +-----------+------+------+
6575 * | mainB | subC | subB |
6576 * +-----------+------+------+
6577 *
6578 * At this point we unwind recursion, relocate calls in subC, then in mainB.
6579 */
6580 static int
bpf_object__relocate_calls(struct bpf_object * obj,struct bpf_program * prog)6581 bpf_object__relocate_calls(struct bpf_object *obj, struct bpf_program *prog)
6582 {
6583 struct bpf_program *subprog;
6584 int i, err;
6585
6586 /* mark all subprogs as not relocated (yet) within the context of
6587 * current main program
6588 */
6589 for (i = 0; i < obj->nr_programs; i++) {
6590 subprog = &obj->programs[i];
6591 if (!prog_is_subprog(obj, subprog))
6592 continue;
6593
6594 subprog->sub_insn_off = 0;
6595 }
6596
6597 err = bpf_object__reloc_code(obj, prog, prog);
6598 if (err)
6599 return err;
6600
6601 return 0;
6602 }
6603
6604 static void
bpf_object__free_relocs(struct bpf_object * obj)6605 bpf_object__free_relocs(struct bpf_object *obj)
6606 {
6607 struct bpf_program *prog;
6608 int i;
6609
6610 /* free up relocation descriptors */
6611 for (i = 0; i < obj->nr_programs; i++) {
6612 prog = &obj->programs[i];
6613 zfree(&prog->reloc_desc);
6614 prog->nr_reloc = 0;
6615 }
6616 }
6617
cmp_relocs(const void * _a,const void * _b)6618 static int cmp_relocs(const void *_a, const void *_b)
6619 {
6620 const struct reloc_desc *a = _a;
6621 const struct reloc_desc *b = _b;
6622
6623 if (a->insn_idx != b->insn_idx)
6624 return a->insn_idx < b->insn_idx ? -1 : 1;
6625
6626 /* no two relocations should have the same insn_idx, but ... */
6627 if (a->type != b->type)
6628 return a->type < b->type ? -1 : 1;
6629
6630 return 0;
6631 }
6632
bpf_object__sort_relos(struct bpf_object * obj)6633 static void bpf_object__sort_relos(struct bpf_object *obj)
6634 {
6635 int i;
6636
6637 for (i = 0; i < obj->nr_programs; i++) {
6638 struct bpf_program *p = &obj->programs[i];
6639
6640 if (!p->nr_reloc)
6641 continue;
6642
6643 qsort(p->reloc_desc, p->nr_reloc, sizeof(*p->reloc_desc), cmp_relocs);
6644 }
6645 }
6646
bpf_prog_assign_exc_cb(struct bpf_object * obj,struct bpf_program * prog)6647 static int bpf_prog_assign_exc_cb(struct bpf_object *obj, struct bpf_program *prog)
6648 {
6649 const char *str = "exception_callback:";
6650 size_t pfx_len = strlen(str);
6651 int i, j, n;
6652
6653 if (!obj->btf || !kernel_supports(obj, FEAT_BTF_DECL_TAG))
6654 return 0;
6655
6656 n = btf__type_cnt(obj->btf);
6657 for (i = 1; i < n; i++) {
6658 const char *name;
6659 struct btf_type *t;
6660
6661 t = btf_type_by_id(obj->btf, i);
6662 if (!btf_is_decl_tag(t) || btf_decl_tag(t)->component_idx != -1)
6663 continue;
6664
6665 name = btf__str_by_offset(obj->btf, t->name_off);
6666 if (strncmp(name, str, pfx_len) != 0)
6667 continue;
6668
6669 t = btf_type_by_id(obj->btf, t->type);
6670 if (!btf_is_func(t) || btf_func_linkage(t) != BTF_FUNC_GLOBAL) {
6671 pr_warn("prog '%s': exception_callback:<value> decl tag not applied to the main program\n",
6672 prog->name);
6673 return -EINVAL;
6674 }
6675 if (strcmp(prog->name, btf__str_by_offset(obj->btf, t->name_off)) != 0)
6676 continue;
6677 /* Multiple callbacks are specified for the same prog,
6678 * the verifier will eventually return an error for this
6679 * case, hence simply skip appending a subprog.
6680 */
6681 if (prog->exception_cb_idx >= 0) {
6682 prog->exception_cb_idx = -1;
6683 break;
6684 }
6685
6686 name += pfx_len;
6687 if (str_is_empty(name)) {
6688 pr_warn("prog '%s': exception_callback:<value> decl tag contains empty value\n",
6689 prog->name);
6690 return -EINVAL;
6691 }
6692
6693 for (j = 0; j < obj->nr_programs; j++) {
6694 struct bpf_program *subprog = &obj->programs[j];
6695
6696 if (!prog_is_subprog(obj, subprog))
6697 continue;
6698 if (strcmp(name, subprog->name) != 0)
6699 continue;
6700 /* Enforce non-hidden, as from verifier point of
6701 * view it expects global functions, whereas the
6702 * mark_btf_static fixes up linkage as static.
6703 */
6704 if (!subprog->sym_global || subprog->mark_btf_static) {
6705 pr_warn("prog '%s': exception callback %s must be a global non-hidden function\n",
6706 prog->name, subprog->name);
6707 return -EINVAL;
6708 }
6709 /* Let's see if we already saw a static exception callback with the same name */
6710 if (prog->exception_cb_idx >= 0) {
6711 pr_warn("prog '%s': multiple subprogs with same name as exception callback '%s'\n",
6712 prog->name, subprog->name);
6713 return -EINVAL;
6714 }
6715 prog->exception_cb_idx = j;
6716 break;
6717 }
6718
6719 if (prog->exception_cb_idx >= 0)
6720 continue;
6721
6722 pr_warn("prog '%s': cannot find exception callback '%s'\n", prog->name, name);
6723 return -ENOENT;
6724 }
6725
6726 return 0;
6727 }
6728
6729 static struct {
6730 enum bpf_prog_type prog_type;
6731 const char *ctx_name;
6732 } global_ctx_map[] = {
6733 { BPF_PROG_TYPE_CGROUP_DEVICE, "bpf_cgroup_dev_ctx" },
6734 { BPF_PROG_TYPE_CGROUP_SKB, "__sk_buff" },
6735 { BPF_PROG_TYPE_CGROUP_SOCK, "bpf_sock" },
6736 { BPF_PROG_TYPE_CGROUP_SOCK_ADDR, "bpf_sock_addr" },
6737 { BPF_PROG_TYPE_CGROUP_SOCKOPT, "bpf_sockopt" },
6738 { BPF_PROG_TYPE_CGROUP_SYSCTL, "bpf_sysctl" },
6739 { BPF_PROG_TYPE_FLOW_DISSECTOR, "__sk_buff" },
6740 { BPF_PROG_TYPE_KPROBE, "bpf_user_pt_regs_t" },
6741 { BPF_PROG_TYPE_LWT_IN, "__sk_buff" },
6742 { BPF_PROG_TYPE_LWT_OUT, "__sk_buff" },
6743 { BPF_PROG_TYPE_LWT_SEG6LOCAL, "__sk_buff" },
6744 { BPF_PROG_TYPE_LWT_XMIT, "__sk_buff" },
6745 { BPF_PROG_TYPE_NETFILTER, "bpf_nf_ctx" },
6746 { BPF_PROG_TYPE_PERF_EVENT, "bpf_perf_event_data" },
6747 { BPF_PROG_TYPE_RAW_TRACEPOINT, "bpf_raw_tracepoint_args" },
6748 { BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE, "bpf_raw_tracepoint_args" },
6749 { BPF_PROG_TYPE_SCHED_ACT, "__sk_buff" },
6750 { BPF_PROG_TYPE_SCHED_CLS, "__sk_buff" },
6751 { BPF_PROG_TYPE_SK_LOOKUP, "bpf_sk_lookup" },
6752 { BPF_PROG_TYPE_SK_MSG, "sk_msg_md" },
6753 { BPF_PROG_TYPE_SK_REUSEPORT, "sk_reuseport_md" },
6754 { BPF_PROG_TYPE_SK_SKB, "__sk_buff" },
6755 { BPF_PROG_TYPE_SOCK_OPS, "bpf_sock_ops" },
6756 { BPF_PROG_TYPE_SOCKET_FILTER, "__sk_buff" },
6757 { BPF_PROG_TYPE_XDP, "xdp_md" },
6758 /* all other program types don't have "named" context structs */
6759 };
6760
6761 /* forward declarations for arch-specific underlying types of bpf_user_pt_regs_t typedef,
6762 * for below __builtin_types_compatible_p() checks;
6763 * with this approach we don't need any extra arch-specific #ifdef guards
6764 */
6765 struct pt_regs;
6766 struct user_pt_regs;
6767 struct user_regs_struct;
6768
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)6769 static bool need_func_arg_type_fixup(const struct btf *btf, const struct bpf_program *prog,
6770 const char *subprog_name, int arg_idx,
6771 int arg_type_id, const char *ctx_name)
6772 {
6773 const struct btf_type *t;
6774 const char *tname;
6775
6776 /* check if existing parameter already matches verifier expectations */
6777 t = skip_mods_and_typedefs(btf, arg_type_id, NULL);
6778 if (!btf_is_ptr(t))
6779 goto out_warn;
6780
6781 /* typedef bpf_user_pt_regs_t is a special PITA case, valid for kprobe
6782 * and perf_event programs, so check this case early on and forget
6783 * about it for subsequent checks
6784 */
6785 while (btf_is_mod(t))
6786 t = btf__type_by_id(btf, t->type);
6787 if (btf_is_typedef(t) &&
6788 (prog->type == BPF_PROG_TYPE_KPROBE || prog->type == BPF_PROG_TYPE_PERF_EVENT)) {
6789 tname = btf__str_by_offset(btf, t->name_off) ?: "<anon>";
6790 if (strcmp(tname, "bpf_user_pt_regs_t") == 0)
6791 return false; /* canonical type for kprobe/perf_event */
6792 }
6793
6794 /* now we can ignore typedefs moving forward */
6795 t = skip_mods_and_typedefs(btf, t->type, NULL);
6796
6797 /* if it's `void *`, definitely fix up BTF info */
6798 if (btf_is_void(t))
6799 return true;
6800
6801 /* if it's already proper canonical type, no need to fix up */
6802 tname = btf__str_by_offset(btf, t->name_off) ?: "<anon>";
6803 if (btf_is_struct(t) && strcmp(tname, ctx_name) == 0)
6804 return false;
6805
6806 /* special cases */
6807 switch (prog->type) {
6808 case BPF_PROG_TYPE_KPROBE:
6809 /* `struct pt_regs *` is expected, but we need to fix up */
6810 if (btf_is_struct(t) && strcmp(tname, "pt_regs") == 0)
6811 return true;
6812 break;
6813 case BPF_PROG_TYPE_PERF_EVENT:
6814 if (__builtin_types_compatible_p(bpf_user_pt_regs_t, struct pt_regs) &&
6815 btf_is_struct(t) && strcmp(tname, "pt_regs") == 0)
6816 return true;
6817 if (__builtin_types_compatible_p(bpf_user_pt_regs_t, struct user_pt_regs) &&
6818 btf_is_struct(t) && strcmp(tname, "user_pt_regs") == 0)
6819 return true;
6820 if (__builtin_types_compatible_p(bpf_user_pt_regs_t, struct user_regs_struct) &&
6821 btf_is_struct(t) && strcmp(tname, "user_regs_struct") == 0)
6822 return true;
6823 break;
6824 case BPF_PROG_TYPE_RAW_TRACEPOINT:
6825 case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
6826 /* allow u64* as ctx */
6827 if (btf_is_int(t) && t->size == 8)
6828 return true;
6829 break;
6830 default:
6831 break;
6832 }
6833
6834 out_warn:
6835 pr_warn("prog '%s': subprog '%s' arg#%d is expected to be of `struct %s *` type\n",
6836 prog->name, subprog_name, arg_idx, ctx_name);
6837 return false;
6838 }
6839
clone_func_btf_info(struct btf * btf,int orig_fn_id,struct bpf_program * prog)6840 static int clone_func_btf_info(struct btf *btf, int orig_fn_id, struct bpf_program *prog)
6841 {
6842 int fn_id, fn_proto_id, ret_type_id, orig_proto_id;
6843 int i, err, arg_cnt, fn_name_off, linkage;
6844 struct btf_type *fn_t, *fn_proto_t, *t;
6845 struct btf_param *p;
6846
6847 /* caller already validated FUNC -> FUNC_PROTO validity */
6848 fn_t = btf_type_by_id(btf, orig_fn_id);
6849 fn_proto_t = btf_type_by_id(btf, fn_t->type);
6850
6851 /* Note that each btf__add_xxx() operation invalidates
6852 * all btf_type and string pointers, so we need to be
6853 * very careful when cloning BTF types. BTF type
6854 * pointers have to be always refetched. And to avoid
6855 * problems with invalidated string pointers, we
6856 * add empty strings initially, then just fix up
6857 * name_off offsets in place. Offsets are stable for
6858 * existing strings, so that works out.
6859 */
6860 fn_name_off = fn_t->name_off; /* we are about to invalidate fn_t */
6861 linkage = btf_func_linkage(fn_t);
6862 orig_proto_id = fn_t->type; /* original FUNC_PROTO ID */
6863 ret_type_id = fn_proto_t->type; /* fn_proto_t will be invalidated */
6864 arg_cnt = btf_vlen(fn_proto_t);
6865
6866 /* clone FUNC_PROTO and its params */
6867 fn_proto_id = btf__add_func_proto(btf, ret_type_id);
6868 if (fn_proto_id < 0)
6869 return -EINVAL;
6870
6871 for (i = 0; i < arg_cnt; i++) {
6872 int name_off;
6873
6874 /* copy original parameter data */
6875 t = btf_type_by_id(btf, orig_proto_id);
6876 p = &btf_params(t)[i];
6877 name_off = p->name_off;
6878
6879 err = btf__add_func_param(btf, "", p->type);
6880 if (err)
6881 return err;
6882
6883 fn_proto_t = btf_type_by_id(btf, fn_proto_id);
6884 p = &btf_params(fn_proto_t)[i];
6885 p->name_off = name_off; /* use remembered str offset */
6886 }
6887
6888 /* clone FUNC now, btf__add_func() enforces non-empty name, so use
6889 * entry program's name as a placeholder, which we replace immediately
6890 * with original name_off
6891 */
6892 fn_id = btf__add_func(btf, prog->name, linkage, fn_proto_id);
6893 if (fn_id < 0)
6894 return -EINVAL;
6895
6896 fn_t = btf_type_by_id(btf, fn_id);
6897 fn_t->name_off = fn_name_off; /* reuse original string */
6898
6899 return fn_id;
6900 }
6901
6902 /* Check if main program or global subprog's function prototype has `arg:ctx`
6903 * argument tags, and, if necessary, substitute correct type to match what BPF
6904 * verifier would expect, taking into account specific program type. This
6905 * allows to support __arg_ctx tag transparently on old kernels that don't yet
6906 * have a native support for it in the verifier, making user's life much
6907 * easier.
6908 */
bpf_program_fixup_func_info(struct bpf_object * obj,struct bpf_program * prog)6909 static int bpf_program_fixup_func_info(struct bpf_object *obj, struct bpf_program *prog)
6910 {
6911 const char *ctx_name = NULL, *ctx_tag = "arg:ctx", *fn_name;
6912 struct bpf_func_info_min *func_rec;
6913 struct btf_type *fn_t, *fn_proto_t;
6914 struct btf *btf = obj->btf;
6915 const struct btf_type *t;
6916 struct btf_param *p;
6917 int ptr_id = 0, struct_id, tag_id, orig_fn_id;
6918 int i, n, arg_idx, arg_cnt, err, rec_idx;
6919 int *orig_ids;
6920
6921 /* no .BTF.ext, no problem */
6922 if (!obj->btf_ext || !prog->func_info)
6923 return 0;
6924
6925 /* don't do any fix ups if kernel natively supports __arg_ctx */
6926 if (kernel_supports(obj, FEAT_ARG_CTX_TAG))
6927 return 0;
6928
6929 /* some BPF program types just don't have named context structs, so
6930 * this fallback mechanism doesn't work for them
6931 */
6932 for (i = 0; i < ARRAY_SIZE(global_ctx_map); i++) {
6933 if (global_ctx_map[i].prog_type != prog->type)
6934 continue;
6935 ctx_name = global_ctx_map[i].ctx_name;
6936 break;
6937 }
6938 if (!ctx_name)
6939 return 0;
6940
6941 /* remember original func BTF IDs to detect if we already cloned them */
6942 orig_ids = calloc(prog->func_info_cnt, sizeof(*orig_ids));
6943 if (!orig_ids)
6944 return -ENOMEM;
6945 for (i = 0; i < prog->func_info_cnt; i++) {
6946 func_rec = prog->func_info + prog->func_info_rec_size * i;
6947 orig_ids[i] = func_rec->type_id;
6948 }
6949
6950 /* go through each DECL_TAG with "arg:ctx" and see if it points to one
6951 * of our subprogs; if yes and subprog is global and needs adjustment,
6952 * clone and adjust FUNC -> FUNC_PROTO combo
6953 */
6954 for (i = 1, n = btf__type_cnt(btf); i < n; i++) {
6955 /* only DECL_TAG with "arg:ctx" value are interesting */
6956 t = btf__type_by_id(btf, i);
6957 if (!btf_is_decl_tag(t))
6958 continue;
6959 if (strcmp(btf__str_by_offset(btf, t->name_off), ctx_tag) != 0)
6960 continue;
6961
6962 /* only global funcs need adjustment, if at all */
6963 orig_fn_id = t->type;
6964 fn_t = btf_type_by_id(btf, orig_fn_id);
6965 if (!btf_is_func(fn_t) || btf_func_linkage(fn_t) != BTF_FUNC_GLOBAL)
6966 continue;
6967
6968 /* sanity check FUNC -> FUNC_PROTO chain, just in case */
6969 fn_proto_t = btf_type_by_id(btf, fn_t->type);
6970 if (!fn_proto_t || !btf_is_func_proto(fn_proto_t))
6971 continue;
6972
6973 /* find corresponding func_info record */
6974 func_rec = NULL;
6975 for (rec_idx = 0; rec_idx < prog->func_info_cnt; rec_idx++) {
6976 if (orig_ids[rec_idx] == t->type) {
6977 func_rec = prog->func_info + prog->func_info_rec_size * rec_idx;
6978 break;
6979 }
6980 }
6981 /* current main program doesn't call into this subprog */
6982 if (!func_rec)
6983 continue;
6984
6985 /* some more sanity checking of DECL_TAG */
6986 arg_cnt = btf_vlen(fn_proto_t);
6987 arg_idx = btf_decl_tag(t)->component_idx;
6988 if (arg_idx < 0 || arg_idx >= arg_cnt)
6989 continue;
6990
6991 /* check if we should fix up argument type */
6992 p = &btf_params(fn_proto_t)[arg_idx];
6993 fn_name = btf__str_by_offset(btf, fn_t->name_off) ?: "<anon>";
6994 if (!need_func_arg_type_fixup(btf, prog, fn_name, arg_idx, p->type, ctx_name))
6995 continue;
6996
6997 /* clone fn/fn_proto, unless we already did it for another arg */
6998 if (func_rec->type_id == orig_fn_id) {
6999 int fn_id;
7000
7001 fn_id = clone_func_btf_info(btf, orig_fn_id, prog);
7002 if (fn_id < 0) {
7003 err = fn_id;
7004 goto err_out;
7005 }
7006
7007 /* point func_info record to a cloned FUNC type */
7008 func_rec->type_id = fn_id;
7009 }
7010
7011 /* create PTR -> STRUCT type chain to mark PTR_TO_CTX argument;
7012 * we do it just once per main BPF program, as all global
7013 * funcs share the same program type, so need only PTR ->
7014 * STRUCT type chain
7015 */
7016 if (ptr_id == 0) {
7017 struct_id = btf__add_struct(btf, ctx_name, 0);
7018 ptr_id = btf__add_ptr(btf, struct_id);
7019 if (ptr_id < 0 || struct_id < 0) {
7020 err = -EINVAL;
7021 goto err_out;
7022 }
7023 }
7024
7025 /* for completeness, clone DECL_TAG and point it to cloned param */
7026 tag_id = btf__add_decl_tag(btf, ctx_tag, func_rec->type_id, arg_idx);
7027 if (tag_id < 0) {
7028 err = -EINVAL;
7029 goto err_out;
7030 }
7031
7032 /* all the BTF manipulations invalidated pointers, refetch them */
7033 fn_t = btf_type_by_id(btf, func_rec->type_id);
7034 fn_proto_t = btf_type_by_id(btf, fn_t->type);
7035
7036 /* fix up type ID pointed to by param */
7037 p = &btf_params(fn_proto_t)[arg_idx];
7038 p->type = ptr_id;
7039 }
7040
7041 free(orig_ids);
7042 return 0;
7043 err_out:
7044 free(orig_ids);
7045 return err;
7046 }
7047
bpf_object__relocate(struct bpf_object * obj,const char * targ_btf_path)7048 static int bpf_object__relocate(struct bpf_object *obj, const char *targ_btf_path)
7049 {
7050 struct bpf_program *prog;
7051 size_t i, j;
7052 int err;
7053
7054 if (obj->btf_ext) {
7055 err = bpf_object__relocate_core(obj, targ_btf_path);
7056 if (err) {
7057 pr_warn("failed to perform CO-RE relocations: %s\n",
7058 errstr(err));
7059 return err;
7060 }
7061 bpf_object__sort_relos(obj);
7062 }
7063
7064 /* Before relocating calls pre-process relocations and mark
7065 * few ld_imm64 instructions that points to subprogs.
7066 * Otherwise bpf_object__reloc_code() later would have to consider
7067 * all ld_imm64 insns as relocation candidates. That would
7068 * reduce relocation speed, since amount of find_prog_insn_relo()
7069 * would increase and most of them will fail to find a relo.
7070 */
7071 for (i = 0; i < obj->nr_programs; i++) {
7072 prog = &obj->programs[i];
7073 for (j = 0; j < prog->nr_reloc; j++) {
7074 struct reloc_desc *relo = &prog->reloc_desc[j];
7075 struct bpf_insn *insn = &prog->insns[relo->insn_idx];
7076
7077 /* mark the insn, so it's recognized by insn_is_pseudo_func() */
7078 if (relo->type == RELO_SUBPROG_ADDR)
7079 insn[0].src_reg = BPF_PSEUDO_FUNC;
7080 }
7081 }
7082
7083 /* relocate subprogram calls and append used subprograms to main
7084 * programs; each copy of subprogram code needs to be relocated
7085 * differently for each main program, because its code location might
7086 * have changed.
7087 * Append subprog relos to main programs to allow data relos to be
7088 * processed after text is completely relocated.
7089 */
7090 for (i = 0; i < obj->nr_programs; i++) {
7091 prog = &obj->programs[i];
7092 /* sub-program's sub-calls are relocated within the context of
7093 * its main program only
7094 */
7095 if (prog_is_subprog(obj, prog))
7096 continue;
7097 if (!prog->autoload)
7098 continue;
7099
7100 err = bpf_object__relocate_calls(obj, prog);
7101 if (err) {
7102 pr_warn("prog '%s': failed to relocate calls: %s\n",
7103 prog->name, errstr(err));
7104 return err;
7105 }
7106
7107 err = bpf_prog_assign_exc_cb(obj, prog);
7108 if (err)
7109 return err;
7110 /* Now, also append exception callback if it has not been done already. */
7111 if (prog->exception_cb_idx >= 0) {
7112 struct bpf_program *subprog = &obj->programs[prog->exception_cb_idx];
7113
7114 /* Calling exception callback directly is disallowed, which the
7115 * verifier will reject later. In case it was processed already,
7116 * we can skip this step, otherwise for all other valid cases we
7117 * have to append exception callback now.
7118 */
7119 if (subprog->sub_insn_off == 0) {
7120 err = bpf_object__append_subprog_code(obj, prog, subprog);
7121 if (err)
7122 return err;
7123 err = bpf_object__reloc_code(obj, prog, subprog);
7124 if (err)
7125 return err;
7126 }
7127 }
7128 }
7129 for (i = 0; i < obj->nr_programs; i++) {
7130 prog = &obj->programs[i];
7131 if (prog_is_subprog(obj, prog))
7132 continue;
7133 if (!prog->autoload)
7134 continue;
7135
7136 /* Process data relos for main programs */
7137 err = bpf_object__relocate_data(obj, prog);
7138 if (err) {
7139 pr_warn("prog '%s': failed to relocate data references: %s\n",
7140 prog->name, errstr(err));
7141 return err;
7142 }
7143
7144 /* Fix up .BTF.ext information, if necessary */
7145 err = bpf_program_fixup_func_info(obj, prog);
7146 if (err) {
7147 pr_warn("prog '%s': failed to perform .BTF.ext fix ups: %s\n",
7148 prog->name, errstr(err));
7149 return err;
7150 }
7151 }
7152
7153 return 0;
7154 }
7155
7156 static int bpf_object__collect_st_ops_relos(struct bpf_object *obj,
7157 Elf64_Shdr *shdr, Elf_Data *data);
7158
bpf_object__collect_map_relos(struct bpf_object * obj,Elf64_Shdr * shdr,Elf_Data * data)7159 static int bpf_object__collect_map_relos(struct bpf_object *obj,
7160 Elf64_Shdr *shdr, Elf_Data *data)
7161 {
7162 const int bpf_ptr_sz = 8, host_ptr_sz = sizeof(void *);
7163 int i, j, nrels, new_sz;
7164 const struct btf_var_secinfo *vi = NULL;
7165 const struct btf_type *sec, *var, *def;
7166 struct bpf_map *map = NULL, *targ_map = NULL;
7167 struct bpf_program *targ_prog = NULL;
7168 bool is_prog_array, is_map_in_map;
7169 const struct btf_member *member;
7170 const char *name, *mname, *type;
7171 unsigned int moff;
7172 Elf64_Sym *sym;
7173 Elf64_Rel *rel;
7174 void *tmp;
7175
7176 if (!obj->efile.btf_maps_sec_btf_id || !obj->btf)
7177 return -EINVAL;
7178 sec = btf__type_by_id(obj->btf, obj->efile.btf_maps_sec_btf_id);
7179 if (!sec)
7180 return -EINVAL;
7181
7182 nrels = shdr->sh_size / shdr->sh_entsize;
7183 for (i = 0; i < nrels; i++) {
7184 rel = elf_rel_by_idx(data, i);
7185 if (!rel) {
7186 pr_warn(".maps relo #%d: failed to get ELF relo\n", i);
7187 return -LIBBPF_ERRNO__FORMAT;
7188 }
7189
7190 sym = elf_sym_by_idx(obj, ELF64_R_SYM(rel->r_info));
7191 if (!sym) {
7192 pr_warn(".maps relo #%d: symbol %zx not found\n",
7193 i, (size_t)ELF64_R_SYM(rel->r_info));
7194 return -LIBBPF_ERRNO__FORMAT;
7195 }
7196 name = elf_sym_str(obj, sym->st_name) ?: "<?>";
7197
7198 pr_debug(".maps relo #%d: for %zd value %zd rel->r_offset %zu name %d ('%s')\n",
7199 i, (ssize_t)(rel->r_info >> 32), (size_t)sym->st_value,
7200 (size_t)rel->r_offset, sym->st_name, name);
7201
7202 for (j = 0; j < obj->nr_maps; j++) {
7203 map = &obj->maps[j];
7204 if (map->sec_idx != obj->efile.btf_maps_shndx)
7205 continue;
7206
7207 vi = btf_var_secinfos(sec) + map->btf_var_idx;
7208 if (vi->offset <= rel->r_offset &&
7209 rel->r_offset + bpf_ptr_sz <= vi->offset + vi->size)
7210 break;
7211 }
7212 if (j == obj->nr_maps) {
7213 pr_warn(".maps relo #%d: cannot find map '%s' at rel->r_offset %zu\n",
7214 i, name, (size_t)rel->r_offset);
7215 return -EINVAL;
7216 }
7217
7218 is_map_in_map = bpf_map_type__is_map_in_map(map->def.type);
7219 is_prog_array = map->def.type == BPF_MAP_TYPE_PROG_ARRAY;
7220 type = is_map_in_map ? "map" : "prog";
7221 if (is_map_in_map) {
7222 if (sym->st_shndx != obj->efile.btf_maps_shndx) {
7223 pr_warn(".maps relo #%d: '%s' isn't a BTF-defined map\n",
7224 i, name);
7225 return -LIBBPF_ERRNO__RELOC;
7226 }
7227 if (map->def.type == BPF_MAP_TYPE_HASH_OF_MAPS &&
7228 map->def.key_size != sizeof(int)) {
7229 pr_warn(".maps relo #%d: hash-of-maps '%s' should have key size %zu.\n",
7230 i, map->name, sizeof(int));
7231 return -EINVAL;
7232 }
7233 targ_map = bpf_object__find_map_by_name(obj, name);
7234 if (!targ_map) {
7235 pr_warn(".maps relo #%d: '%s' isn't a valid map reference\n",
7236 i, name);
7237 return -ESRCH;
7238 }
7239 } else if (is_prog_array) {
7240 targ_prog = bpf_object__find_program_by_name(obj, name);
7241 if (!targ_prog) {
7242 pr_warn(".maps relo #%d: '%s' isn't a valid program reference\n",
7243 i, name);
7244 return -ESRCH;
7245 }
7246 if (targ_prog->sec_idx != sym->st_shndx ||
7247 targ_prog->sec_insn_off * 8 != sym->st_value ||
7248 prog_is_subprog(obj, targ_prog)) {
7249 pr_warn(".maps relo #%d: '%s' isn't an entry-point program\n",
7250 i, name);
7251 return -LIBBPF_ERRNO__RELOC;
7252 }
7253 } else {
7254 return -EINVAL;
7255 }
7256
7257 var = btf__type_by_id(obj->btf, vi->type);
7258 def = skip_mods_and_typedefs(obj->btf, var->type, NULL);
7259 if (btf_vlen(def) == 0)
7260 return -EINVAL;
7261 member = btf_members(def) + btf_vlen(def) - 1;
7262 mname = btf__name_by_offset(obj->btf, member->name_off);
7263 if (strcmp(mname, "values"))
7264 return -EINVAL;
7265
7266 moff = btf_member_bit_offset(def, btf_vlen(def) - 1) / 8;
7267 if (rel->r_offset - vi->offset < moff)
7268 return -EINVAL;
7269
7270 moff = rel->r_offset - vi->offset - moff;
7271 /* here we use BPF pointer size, which is always 64 bit, as we
7272 * are parsing ELF that was built for BPF target
7273 */
7274 if (moff % bpf_ptr_sz)
7275 return -EINVAL;
7276 moff /= bpf_ptr_sz;
7277 if (moff >= map->init_slots_sz) {
7278 new_sz = moff + 1;
7279 tmp = libbpf_reallocarray(map->init_slots, new_sz, host_ptr_sz);
7280 if (!tmp)
7281 return -ENOMEM;
7282 map->init_slots = tmp;
7283 memset(map->init_slots + map->init_slots_sz, 0,
7284 (new_sz - map->init_slots_sz) * host_ptr_sz);
7285 map->init_slots_sz = new_sz;
7286 }
7287 map->init_slots[moff] = is_map_in_map ? (void *)targ_map : (void *)targ_prog;
7288
7289 pr_debug(".maps relo #%d: map '%s' slot [%d] points to %s '%s'\n",
7290 i, map->name, moff, type, name);
7291 }
7292
7293 return 0;
7294 }
7295
bpf_object__collect_relos(struct bpf_object * obj)7296 static int bpf_object__collect_relos(struct bpf_object *obj)
7297 {
7298 int i, err;
7299
7300 for (i = 0; i < obj->efile.sec_cnt; i++) {
7301 struct elf_sec_desc *sec_desc = &obj->efile.secs[i];
7302 Elf64_Shdr *shdr;
7303 Elf_Data *data;
7304 int idx;
7305
7306 if (sec_desc->sec_type != SEC_RELO)
7307 continue;
7308
7309 shdr = sec_desc->shdr;
7310 data = sec_desc->data;
7311 idx = shdr->sh_info;
7312
7313 if (shdr->sh_type != SHT_REL || idx < 0 || idx >= obj->efile.sec_cnt) {
7314 pr_warn("internal error at %d\n", __LINE__);
7315 return -LIBBPF_ERRNO__INTERNAL;
7316 }
7317
7318 if (obj->efile.secs[idx].sec_type == SEC_ST_OPS)
7319 err = bpf_object__collect_st_ops_relos(obj, shdr, data);
7320 else if (idx == obj->efile.btf_maps_shndx)
7321 err = bpf_object__collect_map_relos(obj, shdr, data);
7322 else
7323 err = bpf_object__collect_prog_relos(obj, shdr, data);
7324 if (err)
7325 return err;
7326 }
7327
7328 bpf_object__sort_relos(obj);
7329 return 0;
7330 }
7331
insn_is_helper_call(struct bpf_insn * insn,enum bpf_func_id * func_id)7332 static bool insn_is_helper_call(struct bpf_insn *insn, enum bpf_func_id *func_id)
7333 {
7334 if (BPF_CLASS(insn->code) == BPF_JMP &&
7335 BPF_OP(insn->code) == BPF_CALL &&
7336 BPF_SRC(insn->code) == BPF_K &&
7337 insn->src_reg == 0 &&
7338 insn->dst_reg == 0) {
7339 *func_id = insn->imm;
7340 return true;
7341 }
7342 return false;
7343 }
7344
bpf_object__sanitize_prog(struct bpf_object * obj,struct bpf_program * prog)7345 static int bpf_object__sanitize_prog(struct bpf_object *obj, struct bpf_program *prog)
7346 {
7347 struct bpf_insn *insn = prog->insns;
7348 enum bpf_func_id func_id;
7349 int i;
7350
7351 if (obj->gen_loader)
7352 return 0;
7353
7354 for (i = 0; i < prog->insns_cnt; i++, insn++) {
7355 if (!insn_is_helper_call(insn, &func_id))
7356 continue;
7357
7358 /* on kernels that don't yet support
7359 * bpf_probe_read_{kernel,user}[_str] helpers, fall back
7360 * to bpf_probe_read() which works well for old kernels
7361 */
7362 switch (func_id) {
7363 case BPF_FUNC_probe_read_kernel:
7364 case BPF_FUNC_probe_read_user:
7365 if (!kernel_supports(obj, FEAT_PROBE_READ_KERN))
7366 insn->imm = BPF_FUNC_probe_read;
7367 break;
7368 case BPF_FUNC_probe_read_kernel_str:
7369 case BPF_FUNC_probe_read_user_str:
7370 if (!kernel_supports(obj, FEAT_PROBE_READ_KERN))
7371 insn->imm = BPF_FUNC_probe_read_str;
7372 break;
7373 default:
7374 break;
7375 }
7376 }
7377 return 0;
7378 }
7379
7380 static int libbpf_find_attach_btf_id(struct bpf_program *prog, const char *attach_name,
7381 int *btf_obj_fd, int *btf_type_id);
7382
7383 /* 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)7384 static int libbpf_prepare_prog_load(struct bpf_program *prog,
7385 struct bpf_prog_load_opts *opts, long cookie)
7386 {
7387 enum sec_def_flags def = cookie;
7388
7389 /* old kernels might not support specifying expected_attach_type */
7390 if ((def & SEC_EXP_ATTACH_OPT) && !kernel_supports(prog->obj, FEAT_EXP_ATTACH_TYPE))
7391 opts->expected_attach_type = 0;
7392
7393 if (def & SEC_SLEEPABLE)
7394 opts->prog_flags |= BPF_F_SLEEPABLE;
7395
7396 if (prog->type == BPF_PROG_TYPE_XDP && (def & SEC_XDP_FRAGS))
7397 opts->prog_flags |= BPF_F_XDP_HAS_FRAGS;
7398
7399 /* special check for usdt to use uprobe_multi link */
7400 if ((def & SEC_USDT) && kernel_supports(prog->obj, FEAT_UPROBE_MULTI_LINK)) {
7401 /* for BPF_TRACE_UPROBE_MULTI, user might want to query expected_attach_type
7402 * in prog, and expected_attach_type we set in kernel is from opts, so we
7403 * update both.
7404 */
7405 prog->expected_attach_type = BPF_TRACE_UPROBE_MULTI;
7406 opts->expected_attach_type = BPF_TRACE_UPROBE_MULTI;
7407 }
7408
7409 if ((def & SEC_ATTACH_BTF) && !prog->attach_btf_id) {
7410 int btf_obj_fd = 0, btf_type_id = 0, err;
7411 const char *attach_name;
7412
7413 attach_name = strchr(prog->sec_name, '/');
7414 if (!attach_name) {
7415 /* if BPF program is annotated with just SEC("fentry")
7416 * (or similar) without declaratively specifying
7417 * target, then it is expected that target will be
7418 * specified with bpf_program__set_attach_target() at
7419 * runtime before BPF object load step. If not, then
7420 * there is nothing to load into the kernel as BPF
7421 * verifier won't be able to validate BPF program
7422 * correctness anyways.
7423 */
7424 pr_warn("prog '%s': no BTF-based attach target is specified, use bpf_program__set_attach_target()\n",
7425 prog->name);
7426 return -EINVAL;
7427 }
7428 attach_name++; /* skip over / */
7429
7430 err = libbpf_find_attach_btf_id(prog, attach_name, &btf_obj_fd, &btf_type_id);
7431 if (err)
7432 return err;
7433
7434 /* cache resolved BTF FD and BTF type ID in the prog */
7435 prog->attach_btf_obj_fd = btf_obj_fd;
7436 prog->attach_btf_id = btf_type_id;
7437
7438 /* but by now libbpf common logic is not utilizing
7439 * prog->atach_btf_obj_fd/prog->attach_btf_id anymore because
7440 * this callback is called after opts were populated by
7441 * libbpf, so this callback has to update opts explicitly here
7442 */
7443 opts->attach_btf_obj_fd = btf_obj_fd;
7444 opts->attach_btf_id = btf_type_id;
7445 }
7446 return 0;
7447 }
7448
7449 static void fixup_verifier_log(struct bpf_program *prog, char *buf, size_t buf_sz);
7450
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)7451 static int bpf_object_load_prog(struct bpf_object *obj, struct bpf_program *prog,
7452 struct bpf_insn *insns, int insns_cnt,
7453 const char *license, __u32 kern_version, int *prog_fd)
7454 {
7455 LIBBPF_OPTS(bpf_prog_load_opts, load_attr);
7456 const char *prog_name = NULL;
7457 size_t log_buf_size = 0;
7458 char *log_buf = NULL, *tmp;
7459 bool own_log_buf = true;
7460 __u32 log_level = prog->log_level;
7461 int ret, err;
7462
7463 /* Be more helpful by rejecting programs that can't be validated early
7464 * with more meaningful and actionable error message.
7465 */
7466 switch (prog->type) {
7467 case BPF_PROG_TYPE_UNSPEC:
7468 /*
7469 * The program type must be set. Most likely we couldn't find a proper
7470 * section definition at load time, and thus we didn't infer the type.
7471 */
7472 pr_warn("prog '%s': missing BPF prog type, check ELF section name '%s'\n",
7473 prog->name, prog->sec_name);
7474 return -EINVAL;
7475 case BPF_PROG_TYPE_STRUCT_OPS:
7476 if (prog->attach_btf_id == 0) {
7477 pr_warn("prog '%s': SEC(\"struct_ops\") program isn't referenced anywhere, did you forget to use it?\n",
7478 prog->name);
7479 return -EINVAL;
7480 }
7481 break;
7482 default:
7483 break;
7484 }
7485
7486 if (!insns || !insns_cnt)
7487 return -EINVAL;
7488
7489 if (kernel_supports(obj, FEAT_PROG_NAME))
7490 prog_name = prog->name;
7491 load_attr.attach_prog_fd = prog->attach_prog_fd;
7492 load_attr.attach_btf_obj_fd = prog->attach_btf_obj_fd;
7493 load_attr.attach_btf_id = prog->attach_btf_id;
7494 load_attr.kern_version = kern_version;
7495 load_attr.prog_ifindex = prog->prog_ifindex;
7496 load_attr.expected_attach_type = prog->expected_attach_type;
7497
7498 /* specify func_info/line_info only if kernel supports them */
7499 if (obj->btf && btf__fd(obj->btf) >= 0 && kernel_supports(obj, FEAT_BTF_FUNC)) {
7500 load_attr.prog_btf_fd = btf__fd(obj->btf);
7501 load_attr.func_info = prog->func_info;
7502 load_attr.func_info_rec_size = prog->func_info_rec_size;
7503 load_attr.func_info_cnt = prog->func_info_cnt;
7504 load_attr.line_info = prog->line_info;
7505 load_attr.line_info_rec_size = prog->line_info_rec_size;
7506 load_attr.line_info_cnt = prog->line_info_cnt;
7507 }
7508 load_attr.log_level = log_level;
7509 load_attr.prog_flags = prog->prog_flags;
7510 load_attr.fd_array = obj->fd_array;
7511
7512 load_attr.token_fd = obj->token_fd;
7513 if (obj->token_fd)
7514 load_attr.prog_flags |= BPF_F_TOKEN_FD;
7515
7516 /* adjust load_attr if sec_def provides custom preload callback */
7517 if (prog->sec_def && prog->sec_def->prog_prepare_load_fn) {
7518 err = prog->sec_def->prog_prepare_load_fn(prog, &load_attr, prog->sec_def->cookie);
7519 if (err < 0) {
7520 pr_warn("prog '%s': failed to prepare load attributes: %s\n",
7521 prog->name, errstr(err));
7522 return err;
7523 }
7524 insns = prog->insns;
7525 insns_cnt = prog->insns_cnt;
7526 }
7527
7528 if (obj->gen_loader) {
7529 bpf_gen__prog_load(obj->gen_loader, prog->type, prog->name,
7530 license, insns, insns_cnt, &load_attr,
7531 prog - obj->programs);
7532 *prog_fd = -1;
7533 return 0;
7534 }
7535
7536 retry_load:
7537 /* if log_level is zero, we don't request logs initially even if
7538 * custom log_buf is specified; if the program load fails, then we'll
7539 * bump log_level to 1 and use either custom log_buf or we'll allocate
7540 * our own and retry the load to get details on what failed
7541 */
7542 if (log_level) {
7543 if (prog->log_buf) {
7544 log_buf = prog->log_buf;
7545 log_buf_size = prog->log_size;
7546 own_log_buf = false;
7547 } else if (obj->log_buf) {
7548 log_buf = obj->log_buf;
7549 log_buf_size = obj->log_size;
7550 own_log_buf = false;
7551 } else {
7552 log_buf_size = max((size_t)BPF_LOG_BUF_SIZE, log_buf_size * 2);
7553 tmp = realloc(log_buf, log_buf_size);
7554 if (!tmp) {
7555 ret = -ENOMEM;
7556 goto out;
7557 }
7558 log_buf = tmp;
7559 log_buf[0] = '\0';
7560 own_log_buf = true;
7561 }
7562 }
7563
7564 load_attr.log_buf = log_buf;
7565 load_attr.log_size = log_buf_size;
7566 load_attr.log_level = log_level;
7567
7568 ret = bpf_prog_load(prog->type, prog_name, license, insns, insns_cnt, &load_attr);
7569 if (ret >= 0) {
7570 if (log_level && own_log_buf) {
7571 pr_debug("prog '%s': -- BEGIN PROG LOAD LOG --\n%s-- END PROG LOAD LOG --\n",
7572 prog->name, log_buf);
7573 }
7574
7575 if (obj->has_rodata && kernel_supports(obj, FEAT_PROG_BIND_MAP)) {
7576 struct bpf_map *map;
7577 int i;
7578
7579 for (i = 0; i < obj->nr_maps; i++) {
7580 map = &prog->obj->maps[i];
7581 if (map->libbpf_type != LIBBPF_MAP_RODATA)
7582 continue;
7583
7584 if (bpf_prog_bind_map(ret, map->fd, NULL)) {
7585 pr_warn("prog '%s': failed to bind map '%s': %s\n",
7586 prog->name, map->real_name, errstr(errno));
7587 /* Don't fail hard if can't bind rodata. */
7588 }
7589 }
7590 }
7591
7592 *prog_fd = ret;
7593 ret = 0;
7594 goto out;
7595 }
7596
7597 if (log_level == 0) {
7598 log_level = 1;
7599 goto retry_load;
7600 }
7601 /* On ENOSPC, increase log buffer size and retry, unless custom
7602 * log_buf is specified.
7603 * Be careful to not overflow u32, though. Kernel's log buf size limit
7604 * isn't part of UAPI so it can always be bumped to full 4GB. So don't
7605 * multiply by 2 unless we are sure we'll fit within 32 bits.
7606 * Currently, we'll get -EINVAL when we reach (UINT_MAX >> 2).
7607 */
7608 if (own_log_buf && errno == ENOSPC && log_buf_size <= UINT_MAX / 2)
7609 goto retry_load;
7610
7611 ret = -errno;
7612
7613 /* post-process verifier log to improve error descriptions */
7614 fixup_verifier_log(prog, log_buf, log_buf_size);
7615
7616 pr_warn("prog '%s': BPF program load failed: %s\n", prog->name, errstr(errno));
7617 pr_perm_msg(ret);
7618
7619 if (own_log_buf && log_buf && log_buf[0] != '\0') {
7620 pr_warn("prog '%s': -- BEGIN PROG LOAD LOG --\n%s-- END PROG LOAD LOG --\n",
7621 prog->name, log_buf);
7622 }
7623
7624 out:
7625 if (own_log_buf)
7626 free(log_buf);
7627 return ret;
7628 }
7629
find_prev_line(char * buf,char * cur)7630 static char *find_prev_line(char *buf, char *cur)
7631 {
7632 char *p;
7633
7634 if (cur == buf) /* end of a log buf */
7635 return NULL;
7636
7637 p = cur - 1;
7638 while (p - 1 >= buf && *(p - 1) != '\n')
7639 p--;
7640
7641 return p;
7642 }
7643
patch_log(char * buf,size_t buf_sz,size_t log_sz,char * orig,size_t orig_sz,const char * patch)7644 static void patch_log(char *buf, size_t buf_sz, size_t log_sz,
7645 char *orig, size_t orig_sz, const char *patch)
7646 {
7647 /* size of the remaining log content to the right from the to-be-replaced part */
7648 size_t rem_sz = (buf + log_sz) - (orig + orig_sz);
7649 size_t patch_sz = strlen(patch);
7650
7651 if (patch_sz != orig_sz) {
7652 /* If patch line(s) are longer than original piece of verifier log,
7653 * shift log contents by (patch_sz - orig_sz) bytes to the right
7654 * starting from after to-be-replaced part of the log.
7655 *
7656 * If patch line(s) are shorter than original piece of verifier log,
7657 * shift log contents by (orig_sz - patch_sz) bytes to the left
7658 * starting from after to-be-replaced part of the log
7659 *
7660 * We need to be careful about not overflowing available
7661 * buf_sz capacity. If that's the case, we'll truncate the end
7662 * of the original log, as necessary.
7663 */
7664 if (patch_sz > orig_sz) {
7665 if (orig + patch_sz >= buf + buf_sz) {
7666 /* patch is big enough to cover remaining space completely */
7667 patch_sz -= (orig + patch_sz) - (buf + buf_sz) + 1;
7668 rem_sz = 0;
7669 } else if (patch_sz - orig_sz > buf_sz - log_sz) {
7670 /* patch causes part of remaining log to be truncated */
7671 rem_sz -= (patch_sz - orig_sz) - (buf_sz - log_sz);
7672 }
7673 }
7674 /* shift remaining log to the right by calculated amount */
7675 memmove(orig + patch_sz, orig + orig_sz, rem_sz);
7676 }
7677
7678 memcpy(orig, patch, patch_sz);
7679 }
7680
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)7681 static void fixup_log_failed_core_relo(struct bpf_program *prog,
7682 char *buf, size_t buf_sz, size_t log_sz,
7683 char *line1, char *line2, char *line3)
7684 {
7685 /* Expected log for failed and not properly guarded CO-RE relocation:
7686 * line1 -> 123: (85) call unknown#195896080
7687 * line2 -> invalid func unknown#195896080
7688 * line3 -> <anything else or end of buffer>
7689 *
7690 * "123" is the index of the instruction that was poisoned. We extract
7691 * instruction index to find corresponding CO-RE relocation and
7692 * replace this part of the log with more relevant information about
7693 * failed CO-RE relocation.
7694 */
7695 const struct bpf_core_relo *relo;
7696 struct bpf_core_spec spec;
7697 char patch[512], spec_buf[256];
7698 int insn_idx, err, spec_len;
7699
7700 if (sscanf(line1, "%d: (%*d) call unknown#195896080\n", &insn_idx) != 1)
7701 return;
7702
7703 relo = find_relo_core(prog, insn_idx);
7704 if (!relo)
7705 return;
7706
7707 err = bpf_core_parse_spec(prog->name, prog->obj->btf, relo, &spec);
7708 if (err)
7709 return;
7710
7711 spec_len = bpf_core_format_spec(spec_buf, sizeof(spec_buf), &spec);
7712 snprintf(patch, sizeof(patch),
7713 "%d: <invalid CO-RE relocation>\n"
7714 "failed to resolve CO-RE relocation %s%s\n",
7715 insn_idx, spec_buf, spec_len >= sizeof(spec_buf) ? "..." : "");
7716
7717 patch_log(buf, buf_sz, log_sz, line1, line3 - line1, patch);
7718 }
7719
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)7720 static void fixup_log_missing_map_load(struct bpf_program *prog,
7721 char *buf, size_t buf_sz, size_t log_sz,
7722 char *line1, char *line2, char *line3)
7723 {
7724 /* Expected log for failed and not properly guarded map reference:
7725 * line1 -> 123: (85) call unknown#2001000345
7726 * line2 -> invalid func unknown#2001000345
7727 * line3 -> <anything else or end of buffer>
7728 *
7729 * "123" is the index of the instruction that was poisoned.
7730 * "345" in "2001000345" is a map index in obj->maps to fetch map name.
7731 */
7732 struct bpf_object *obj = prog->obj;
7733 const struct bpf_map *map;
7734 int insn_idx, map_idx;
7735 char patch[128];
7736
7737 if (sscanf(line1, "%d: (%*d) call unknown#%d\n", &insn_idx, &map_idx) != 2)
7738 return;
7739
7740 map_idx -= POISON_LDIMM64_MAP_BASE;
7741 if (map_idx < 0 || map_idx >= obj->nr_maps)
7742 return;
7743 map = &obj->maps[map_idx];
7744
7745 snprintf(patch, sizeof(patch),
7746 "%d: <invalid BPF map reference>\n"
7747 "BPF map '%s' is referenced but wasn't created\n",
7748 insn_idx, map->name);
7749
7750 patch_log(buf, buf_sz, log_sz, line1, line3 - line1, patch);
7751 }
7752
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)7753 static void fixup_log_missing_kfunc_call(struct bpf_program *prog,
7754 char *buf, size_t buf_sz, size_t log_sz,
7755 char *line1, char *line2, char *line3)
7756 {
7757 /* Expected log for failed and not properly guarded kfunc call:
7758 * line1 -> 123: (85) call unknown#2002000345
7759 * line2 -> invalid func unknown#2002000345
7760 * line3 -> <anything else or end of buffer>
7761 *
7762 * "123" is the index of the instruction that was poisoned.
7763 * "345" in "2002000345" is an extern index in obj->externs to fetch kfunc name.
7764 */
7765 struct bpf_object *obj = prog->obj;
7766 const struct extern_desc *ext;
7767 int insn_idx, ext_idx;
7768 char patch[128];
7769
7770 if (sscanf(line1, "%d: (%*d) call unknown#%d\n", &insn_idx, &ext_idx) != 2)
7771 return;
7772
7773 ext_idx -= POISON_CALL_KFUNC_BASE;
7774 if (ext_idx < 0 || ext_idx >= obj->nr_extern)
7775 return;
7776 ext = &obj->externs[ext_idx];
7777
7778 snprintf(patch, sizeof(patch),
7779 "%d: <invalid kfunc call>\n"
7780 "kfunc '%s' is referenced but wasn't resolved\n",
7781 insn_idx, ext->name);
7782
7783 patch_log(buf, buf_sz, log_sz, line1, line3 - line1, patch);
7784 }
7785
fixup_verifier_log(struct bpf_program * prog,char * buf,size_t buf_sz)7786 static void fixup_verifier_log(struct bpf_program *prog, char *buf, size_t buf_sz)
7787 {
7788 /* look for familiar error patterns in last N lines of the log */
7789 const size_t max_last_line_cnt = 10;
7790 char *prev_line, *cur_line, *next_line;
7791 size_t log_sz;
7792 int i;
7793
7794 if (!buf)
7795 return;
7796
7797 log_sz = strlen(buf) + 1;
7798 next_line = buf + log_sz - 1;
7799
7800 for (i = 0; i < max_last_line_cnt; i++, next_line = cur_line) {
7801 cur_line = find_prev_line(buf, next_line);
7802 if (!cur_line)
7803 return;
7804
7805 if (str_has_pfx(cur_line, "invalid func unknown#195896080\n")) {
7806 prev_line = find_prev_line(buf, cur_line);
7807 if (!prev_line)
7808 continue;
7809
7810 /* failed CO-RE relocation case */
7811 fixup_log_failed_core_relo(prog, buf, buf_sz, log_sz,
7812 prev_line, cur_line, next_line);
7813 return;
7814 } else if (str_has_pfx(cur_line, "invalid func unknown#"POISON_LDIMM64_MAP_PFX)) {
7815 prev_line = find_prev_line(buf, cur_line);
7816 if (!prev_line)
7817 continue;
7818
7819 /* reference to uncreated BPF map */
7820 fixup_log_missing_map_load(prog, buf, buf_sz, log_sz,
7821 prev_line, cur_line, next_line);
7822 return;
7823 } else if (str_has_pfx(cur_line, "invalid func unknown#"POISON_CALL_KFUNC_PFX)) {
7824 prev_line = find_prev_line(buf, cur_line);
7825 if (!prev_line)
7826 continue;
7827
7828 /* reference to unresolved kfunc */
7829 fixup_log_missing_kfunc_call(prog, buf, buf_sz, log_sz,
7830 prev_line, cur_line, next_line);
7831 return;
7832 }
7833 }
7834 }
7835
bpf_program_record_relos(struct bpf_program * prog)7836 static int bpf_program_record_relos(struct bpf_program *prog)
7837 {
7838 struct bpf_object *obj = prog->obj;
7839 int i;
7840
7841 for (i = 0; i < prog->nr_reloc; i++) {
7842 struct reloc_desc *relo = &prog->reloc_desc[i];
7843 struct extern_desc *ext = &obj->externs[relo->ext_idx];
7844 int kind;
7845
7846 switch (relo->type) {
7847 case RELO_EXTERN_LD64:
7848 if (ext->type != EXT_KSYM)
7849 continue;
7850 kind = btf_is_var(btf__type_by_id(obj->btf, ext->btf_id)) ?
7851 BTF_KIND_VAR : BTF_KIND_FUNC;
7852 bpf_gen__record_extern(obj->gen_loader, ext->name,
7853 ext->is_weak, !ext->ksym.type_id,
7854 true, kind, relo->insn_idx);
7855 break;
7856 case RELO_EXTERN_CALL:
7857 bpf_gen__record_extern(obj->gen_loader, ext->name,
7858 ext->is_weak, false, false, BTF_KIND_FUNC,
7859 relo->insn_idx);
7860 break;
7861 case RELO_CORE: {
7862 struct bpf_core_relo cr = {
7863 .insn_off = relo->insn_idx * 8,
7864 .type_id = relo->core_relo->type_id,
7865 .access_str_off = relo->core_relo->access_str_off,
7866 .kind = relo->core_relo->kind,
7867 };
7868
7869 bpf_gen__record_relo_core(obj->gen_loader, &cr);
7870 break;
7871 }
7872 default:
7873 continue;
7874 }
7875 }
7876 return 0;
7877 }
7878
7879 static int
bpf_object__load_progs(struct bpf_object * obj,int log_level)7880 bpf_object__load_progs(struct bpf_object *obj, int log_level)
7881 {
7882 struct bpf_program *prog;
7883 size_t i;
7884 int err;
7885
7886 for (i = 0; i < obj->nr_programs; i++) {
7887 prog = &obj->programs[i];
7888 err = bpf_object__sanitize_prog(obj, prog);
7889 if (err)
7890 return err;
7891 }
7892
7893 for (i = 0; i < obj->nr_programs; i++) {
7894 prog = &obj->programs[i];
7895 if (prog_is_subprog(obj, prog))
7896 continue;
7897 if (!prog->autoload) {
7898 pr_debug("prog '%s': skipped loading\n", prog->name);
7899 continue;
7900 }
7901 prog->log_level |= log_level;
7902
7903 if (obj->gen_loader)
7904 bpf_program_record_relos(prog);
7905
7906 err = bpf_object_load_prog(obj, prog, prog->insns, prog->insns_cnt,
7907 obj->license, obj->kern_version, &prog->fd);
7908 if (err) {
7909 pr_warn("prog '%s': failed to load: %s\n", prog->name, errstr(err));
7910 return err;
7911 }
7912 }
7913
7914 bpf_object__free_relocs(obj);
7915 return 0;
7916 }
7917
7918 static const struct bpf_sec_def *find_sec_def(const char *sec_name);
7919
bpf_object_init_progs(struct bpf_object * obj,const struct bpf_object_open_opts * opts)7920 static int bpf_object_init_progs(struct bpf_object *obj, const struct bpf_object_open_opts *opts)
7921 {
7922 struct bpf_program *prog;
7923 int err;
7924
7925 bpf_object__for_each_program(prog, obj) {
7926 prog->sec_def = find_sec_def(prog->sec_name);
7927 if (!prog->sec_def) {
7928 /* couldn't guess, but user might manually specify */
7929 pr_debug("prog '%s': unrecognized ELF section name '%s'\n",
7930 prog->name, prog->sec_name);
7931 continue;
7932 }
7933
7934 prog->type = prog->sec_def->prog_type;
7935 prog->expected_attach_type = prog->sec_def->expected_attach_type;
7936
7937 /* sec_def can have custom callback which should be called
7938 * after bpf_program is initialized to adjust its properties
7939 */
7940 if (prog->sec_def->prog_setup_fn) {
7941 err = prog->sec_def->prog_setup_fn(prog, prog->sec_def->cookie);
7942 if (err < 0) {
7943 pr_warn("prog '%s': failed to initialize: %s\n",
7944 prog->name, errstr(err));
7945 return err;
7946 }
7947 }
7948 }
7949
7950 return 0;
7951 }
7952
bpf_object_open(const char * path,const void * obj_buf,size_t obj_buf_sz,const char * obj_name,const struct bpf_object_open_opts * opts)7953 static struct bpf_object *bpf_object_open(const char *path, const void *obj_buf, size_t obj_buf_sz,
7954 const char *obj_name,
7955 const struct bpf_object_open_opts *opts)
7956 {
7957 const char *kconfig, *btf_tmp_path, *token_path;
7958 struct bpf_object *obj;
7959 int err;
7960 char *log_buf;
7961 size_t log_size;
7962 __u32 log_level;
7963
7964 if (obj_buf && !obj_name)
7965 return ERR_PTR(-EINVAL);
7966
7967 if (elf_version(EV_CURRENT) == EV_NONE) {
7968 pr_warn("failed to init libelf for %s\n",
7969 path ? : "(mem buf)");
7970 return ERR_PTR(-LIBBPF_ERRNO__LIBELF);
7971 }
7972
7973 if (!OPTS_VALID(opts, bpf_object_open_opts))
7974 return ERR_PTR(-EINVAL);
7975
7976 obj_name = OPTS_GET(opts, object_name, NULL) ?: obj_name;
7977 if (obj_buf) {
7978 path = obj_name;
7979 pr_debug("loading object '%s' from buffer\n", obj_name);
7980 } else {
7981 pr_debug("loading object from %s\n", path);
7982 }
7983
7984 log_buf = OPTS_GET(opts, kernel_log_buf, NULL);
7985 log_size = OPTS_GET(opts, kernel_log_size, 0);
7986 log_level = OPTS_GET(opts, kernel_log_level, 0);
7987 if (log_size > UINT_MAX)
7988 return ERR_PTR(-EINVAL);
7989 if (log_size && !log_buf)
7990 return ERR_PTR(-EINVAL);
7991
7992 token_path = OPTS_GET(opts, bpf_token_path, NULL);
7993 /* if user didn't specify bpf_token_path explicitly, check if
7994 * LIBBPF_BPF_TOKEN_PATH envvar was set and treat it as bpf_token_path
7995 * option
7996 */
7997 if (!token_path)
7998 token_path = getenv("LIBBPF_BPF_TOKEN_PATH");
7999 if (token_path && strlen(token_path) >= PATH_MAX)
8000 return ERR_PTR(-ENAMETOOLONG);
8001
8002 obj = bpf_object__new(path, obj_buf, obj_buf_sz, obj_name);
8003 if (IS_ERR(obj))
8004 return obj;
8005
8006 obj->log_buf = log_buf;
8007 obj->log_size = log_size;
8008 obj->log_level = log_level;
8009
8010 if (token_path) {
8011 obj->token_path = strdup(token_path);
8012 if (!obj->token_path) {
8013 err = -ENOMEM;
8014 goto out;
8015 }
8016 }
8017
8018 btf_tmp_path = OPTS_GET(opts, btf_custom_path, NULL);
8019 if (btf_tmp_path) {
8020 if (strlen(btf_tmp_path) >= PATH_MAX) {
8021 err = -ENAMETOOLONG;
8022 goto out;
8023 }
8024 obj->btf_custom_path = strdup(btf_tmp_path);
8025 if (!obj->btf_custom_path) {
8026 err = -ENOMEM;
8027 goto out;
8028 }
8029 }
8030
8031 kconfig = OPTS_GET(opts, kconfig, NULL);
8032 if (kconfig) {
8033 obj->kconfig = strdup(kconfig);
8034 if (!obj->kconfig) {
8035 err = -ENOMEM;
8036 goto out;
8037 }
8038 }
8039
8040 err = bpf_object__elf_init(obj);
8041 err = err ? : bpf_object__elf_collect(obj);
8042 err = err ? : bpf_object__collect_externs(obj);
8043 err = err ? : bpf_object_fixup_btf(obj);
8044 err = err ? : bpf_object__init_maps(obj, opts);
8045 err = err ? : bpf_object_init_progs(obj, opts);
8046 err = err ? : bpf_object__collect_relos(obj);
8047 if (err)
8048 goto out;
8049
8050 bpf_object__elf_finish(obj);
8051
8052 return obj;
8053 out:
8054 bpf_object__close(obj);
8055 return ERR_PTR(err);
8056 }
8057
8058 struct bpf_object *
bpf_object__open_file(const char * path,const struct bpf_object_open_opts * opts)8059 bpf_object__open_file(const char *path, const struct bpf_object_open_opts *opts)
8060 {
8061 if (!path)
8062 return libbpf_err_ptr(-EINVAL);
8063
8064 return libbpf_ptr(bpf_object_open(path, NULL, 0, NULL, opts));
8065 }
8066
bpf_object__open(const char * path)8067 struct bpf_object *bpf_object__open(const char *path)
8068 {
8069 return bpf_object__open_file(path, NULL);
8070 }
8071
8072 struct bpf_object *
bpf_object__open_mem(const void * obj_buf,size_t obj_buf_sz,const struct bpf_object_open_opts * opts)8073 bpf_object__open_mem(const void *obj_buf, size_t obj_buf_sz,
8074 const struct bpf_object_open_opts *opts)
8075 {
8076 char tmp_name[64];
8077
8078 if (!obj_buf || obj_buf_sz == 0)
8079 return libbpf_err_ptr(-EINVAL);
8080
8081 /* create a (quite useless) default "name" for this memory buffer object */
8082 snprintf(tmp_name, sizeof(tmp_name), "%lx-%zx", (unsigned long)obj_buf, obj_buf_sz);
8083
8084 return libbpf_ptr(bpf_object_open(NULL, obj_buf, obj_buf_sz, tmp_name, opts));
8085 }
8086
bpf_object_unload(struct bpf_object * obj)8087 static int bpf_object_unload(struct bpf_object *obj)
8088 {
8089 size_t i;
8090
8091 if (!obj)
8092 return libbpf_err(-EINVAL);
8093
8094 for (i = 0; i < obj->nr_maps; i++) {
8095 zclose(obj->maps[i].fd);
8096 if (obj->maps[i].st_ops)
8097 zfree(&obj->maps[i].st_ops->kern_vdata);
8098 }
8099
8100 for (i = 0; i < obj->nr_programs; i++)
8101 bpf_program__unload(&obj->programs[i]);
8102
8103 return 0;
8104 }
8105
bpf_object__sanitize_maps(struct bpf_object * obj)8106 static int bpf_object__sanitize_maps(struct bpf_object *obj)
8107 {
8108 struct bpf_map *m;
8109
8110 bpf_object__for_each_map(m, obj) {
8111 if (!bpf_map__is_internal(m))
8112 continue;
8113 if (!kernel_supports(obj, FEAT_ARRAY_MMAP))
8114 m->def.map_flags &= ~BPF_F_MMAPABLE;
8115 }
8116
8117 return 0;
8118 }
8119
8120 typedef int (*kallsyms_cb_t)(unsigned long long sym_addr, char sym_type,
8121 const char *sym_name, void *ctx);
8122
libbpf_kallsyms_parse(kallsyms_cb_t cb,void * ctx)8123 static int libbpf_kallsyms_parse(kallsyms_cb_t cb, void *ctx)
8124 {
8125 char sym_type, sym_name[500];
8126 unsigned long long sym_addr;
8127 int ret, err = 0;
8128 FILE *f;
8129
8130 f = fopen("/proc/kallsyms", "re");
8131 if (!f) {
8132 err = -errno;
8133 pr_warn("failed to open /proc/kallsyms: %s\n", errstr(err));
8134 return err;
8135 }
8136
8137 while (true) {
8138 ret = fscanf(f, "%llx %c %499s%*[^\n]\n",
8139 &sym_addr, &sym_type, sym_name);
8140 if (ret == EOF && feof(f))
8141 break;
8142 if (ret != 3) {
8143 pr_warn("failed to read kallsyms entry: %d\n", ret);
8144 err = -EINVAL;
8145 break;
8146 }
8147
8148 err = cb(sym_addr, sym_type, sym_name, ctx);
8149 if (err)
8150 break;
8151 }
8152
8153 fclose(f);
8154 return err;
8155 }
8156
kallsyms_cb(unsigned long long sym_addr,char sym_type,const char * sym_name,void * ctx)8157 static int kallsyms_cb(unsigned long long sym_addr, char sym_type,
8158 const char *sym_name, void *ctx)
8159 {
8160 struct bpf_object *obj = ctx;
8161 const struct btf_type *t;
8162 struct extern_desc *ext;
8163 char *res;
8164
8165 res = strstr(sym_name, ".llvm.");
8166 if (sym_type == 'd' && res)
8167 ext = find_extern_by_name_with_len(obj, sym_name, res - sym_name);
8168 else
8169 ext = find_extern_by_name(obj, sym_name);
8170 if (!ext || ext->type != EXT_KSYM)
8171 return 0;
8172
8173 t = btf__type_by_id(obj->btf, ext->btf_id);
8174 if (!btf_is_var(t))
8175 return 0;
8176
8177 if (ext->is_set && ext->ksym.addr != sym_addr) {
8178 pr_warn("extern (ksym) '%s': resolution is ambiguous: 0x%llx or 0x%llx\n",
8179 sym_name, ext->ksym.addr, sym_addr);
8180 return -EINVAL;
8181 }
8182 if (!ext->is_set) {
8183 ext->is_set = true;
8184 ext->ksym.addr = sym_addr;
8185 pr_debug("extern (ksym) '%s': set to 0x%llx\n", sym_name, sym_addr);
8186 }
8187 return 0;
8188 }
8189
bpf_object__read_kallsyms_file(struct bpf_object * obj)8190 static int bpf_object__read_kallsyms_file(struct bpf_object *obj)
8191 {
8192 return libbpf_kallsyms_parse(kallsyms_cb, obj);
8193 }
8194
find_ksym_btf_id(struct bpf_object * obj,const char * ksym_name,__u16 kind,struct btf ** res_btf,struct module_btf ** res_mod_btf)8195 static int find_ksym_btf_id(struct bpf_object *obj, const char *ksym_name,
8196 __u16 kind, struct btf **res_btf,
8197 struct module_btf **res_mod_btf)
8198 {
8199 struct module_btf *mod_btf;
8200 struct btf *btf;
8201 int i, id, err;
8202
8203 btf = obj->btf_vmlinux;
8204 mod_btf = NULL;
8205 id = btf__find_by_name_kind(btf, ksym_name, kind);
8206
8207 if (id == -ENOENT) {
8208 err = load_module_btfs(obj);
8209 if (err)
8210 return err;
8211
8212 for (i = 0; i < obj->btf_module_cnt; i++) {
8213 /* we assume module_btf's BTF FD is always >0 */
8214 mod_btf = &obj->btf_modules[i];
8215 btf = mod_btf->btf;
8216 id = btf__find_by_name_kind_own(btf, ksym_name, kind);
8217 if (id != -ENOENT)
8218 break;
8219 }
8220 }
8221 if (id <= 0)
8222 return -ESRCH;
8223
8224 *res_btf = btf;
8225 *res_mod_btf = mod_btf;
8226 return id;
8227 }
8228
bpf_object__resolve_ksym_var_btf_id(struct bpf_object * obj,struct extern_desc * ext)8229 static int bpf_object__resolve_ksym_var_btf_id(struct bpf_object *obj,
8230 struct extern_desc *ext)
8231 {
8232 const struct btf_type *targ_var, *targ_type;
8233 __u32 targ_type_id, local_type_id;
8234 struct module_btf *mod_btf = NULL;
8235 const char *targ_var_name;
8236 struct btf *btf = NULL;
8237 int id, err;
8238
8239 id = find_ksym_btf_id(obj, ext->name, BTF_KIND_VAR, &btf, &mod_btf);
8240 if (id < 0) {
8241 if (id == -ESRCH && ext->is_weak)
8242 return 0;
8243 pr_warn("extern (var ksym) '%s': not found in kernel BTF\n",
8244 ext->name);
8245 return id;
8246 }
8247
8248 /* find local type_id */
8249 local_type_id = ext->ksym.type_id;
8250
8251 /* find target type_id */
8252 targ_var = btf__type_by_id(btf, id);
8253 targ_var_name = btf__name_by_offset(btf, targ_var->name_off);
8254 targ_type = skip_mods_and_typedefs(btf, targ_var->type, &targ_type_id);
8255
8256 err = bpf_core_types_are_compat(obj->btf, local_type_id,
8257 btf, targ_type_id);
8258 if (err <= 0) {
8259 const struct btf_type *local_type;
8260 const char *targ_name, *local_name;
8261
8262 local_type = btf__type_by_id(obj->btf, local_type_id);
8263 local_name = btf__name_by_offset(obj->btf, local_type->name_off);
8264 targ_name = btf__name_by_offset(btf, targ_type->name_off);
8265
8266 pr_warn("extern (var ksym) '%s': incompatible types, expected [%d] %s %s, but kernel has [%d] %s %s\n",
8267 ext->name, local_type_id,
8268 btf_kind_str(local_type), local_name, targ_type_id,
8269 btf_kind_str(targ_type), targ_name);
8270 return -EINVAL;
8271 }
8272
8273 ext->is_set = true;
8274 ext->ksym.kernel_btf_obj_fd = mod_btf ? mod_btf->fd : 0;
8275 ext->ksym.kernel_btf_id = id;
8276 pr_debug("extern (var ksym) '%s': resolved to [%d] %s %s\n",
8277 ext->name, id, btf_kind_str(targ_var), targ_var_name);
8278
8279 return 0;
8280 }
8281
bpf_object__resolve_ksym_func_btf_id(struct bpf_object * obj,struct extern_desc * ext)8282 static int bpf_object__resolve_ksym_func_btf_id(struct bpf_object *obj,
8283 struct extern_desc *ext)
8284 {
8285 int local_func_proto_id, kfunc_proto_id, kfunc_id;
8286 struct module_btf *mod_btf = NULL;
8287 const struct btf_type *kern_func;
8288 struct btf *kern_btf = NULL;
8289 int ret;
8290
8291 local_func_proto_id = ext->ksym.type_id;
8292
8293 kfunc_id = find_ksym_btf_id(obj, ext->essent_name ?: ext->name, BTF_KIND_FUNC, &kern_btf,
8294 &mod_btf);
8295 if (kfunc_id < 0) {
8296 if (kfunc_id == -ESRCH && ext->is_weak)
8297 return 0;
8298 pr_warn("extern (func ksym) '%s': not found in kernel or module BTFs\n",
8299 ext->name);
8300 return kfunc_id;
8301 }
8302
8303 kern_func = btf__type_by_id(kern_btf, kfunc_id);
8304 kfunc_proto_id = kern_func->type;
8305
8306 ret = bpf_core_types_are_compat(obj->btf, local_func_proto_id,
8307 kern_btf, kfunc_proto_id);
8308 if (ret <= 0) {
8309 if (ext->is_weak)
8310 return 0;
8311
8312 pr_warn("extern (func ksym) '%s': func_proto [%d] incompatible with %s [%d]\n",
8313 ext->name, local_func_proto_id,
8314 mod_btf ? mod_btf->name : "vmlinux", kfunc_proto_id);
8315 return -EINVAL;
8316 }
8317
8318 /* set index for module BTF fd in fd_array, if unset */
8319 if (mod_btf && !mod_btf->fd_array_idx) {
8320 /* insn->off is s16 */
8321 if (obj->fd_array_cnt == INT16_MAX) {
8322 pr_warn("extern (func ksym) '%s': module BTF fd index %d too big to fit in bpf_insn offset\n",
8323 ext->name, mod_btf->fd_array_idx);
8324 return -E2BIG;
8325 }
8326 /* Cannot use index 0 for module BTF fd */
8327 if (!obj->fd_array_cnt)
8328 obj->fd_array_cnt = 1;
8329
8330 ret = libbpf_ensure_mem((void **)&obj->fd_array, &obj->fd_array_cap, sizeof(int),
8331 obj->fd_array_cnt + 1);
8332 if (ret)
8333 return ret;
8334 mod_btf->fd_array_idx = obj->fd_array_cnt;
8335 /* we assume module BTF FD is always >0 */
8336 obj->fd_array[obj->fd_array_cnt++] = mod_btf->fd;
8337 }
8338
8339 ext->is_set = true;
8340 ext->ksym.kernel_btf_id = kfunc_id;
8341 ext->ksym.btf_fd_idx = mod_btf ? mod_btf->fd_array_idx : 0;
8342 /* Also set kernel_btf_obj_fd to make sure that bpf_object__relocate_data()
8343 * populates FD into ld_imm64 insn when it's used to point to kfunc.
8344 * {kernel_btf_id, btf_fd_idx} -> fixup bpf_call.
8345 * {kernel_btf_id, kernel_btf_obj_fd} -> fixup ld_imm64.
8346 */
8347 ext->ksym.kernel_btf_obj_fd = mod_btf ? mod_btf->fd : 0;
8348 pr_debug("extern (func ksym) '%s': resolved to %s [%d]\n",
8349 ext->name, mod_btf ? mod_btf->name : "vmlinux", kfunc_id);
8350
8351 return 0;
8352 }
8353
bpf_object__resolve_ksyms_btf_id(struct bpf_object * obj)8354 static int bpf_object__resolve_ksyms_btf_id(struct bpf_object *obj)
8355 {
8356 const struct btf_type *t;
8357 struct extern_desc *ext;
8358 int i, err;
8359
8360 for (i = 0; i < obj->nr_extern; i++) {
8361 ext = &obj->externs[i];
8362 if (ext->type != EXT_KSYM || !ext->ksym.type_id)
8363 continue;
8364
8365 if (obj->gen_loader) {
8366 ext->is_set = true;
8367 ext->ksym.kernel_btf_obj_fd = 0;
8368 ext->ksym.kernel_btf_id = 0;
8369 continue;
8370 }
8371 t = btf__type_by_id(obj->btf, ext->btf_id);
8372 if (btf_is_var(t))
8373 err = bpf_object__resolve_ksym_var_btf_id(obj, ext);
8374 else
8375 err = bpf_object__resolve_ksym_func_btf_id(obj, ext);
8376 if (err)
8377 return err;
8378 }
8379 return 0;
8380 }
8381
bpf_object__resolve_externs(struct bpf_object * obj,const char * extra_kconfig)8382 static int bpf_object__resolve_externs(struct bpf_object *obj,
8383 const char *extra_kconfig)
8384 {
8385 bool need_config = false, need_kallsyms = false;
8386 bool need_vmlinux_btf = false;
8387 struct extern_desc *ext;
8388 void *kcfg_data = NULL;
8389 int err, i;
8390
8391 if (obj->nr_extern == 0)
8392 return 0;
8393
8394 if (obj->kconfig_map_idx >= 0)
8395 kcfg_data = obj->maps[obj->kconfig_map_idx].mmaped;
8396
8397 for (i = 0; i < obj->nr_extern; i++) {
8398 ext = &obj->externs[i];
8399
8400 if (ext->type == EXT_KSYM) {
8401 if (ext->ksym.type_id)
8402 need_vmlinux_btf = true;
8403 else
8404 need_kallsyms = true;
8405 continue;
8406 } else if (ext->type == EXT_KCFG) {
8407 void *ext_ptr = kcfg_data + ext->kcfg.data_off;
8408 __u64 value = 0;
8409
8410 /* Kconfig externs need actual /proc/config.gz */
8411 if (str_has_pfx(ext->name, "CONFIG_")) {
8412 need_config = true;
8413 continue;
8414 }
8415
8416 /* Virtual kcfg externs are customly handled by libbpf */
8417 if (strcmp(ext->name, "LINUX_KERNEL_VERSION") == 0) {
8418 value = get_kernel_version();
8419 if (!value) {
8420 pr_warn("extern (kcfg) '%s': failed to get kernel version\n", ext->name);
8421 return -EINVAL;
8422 }
8423 } else if (strcmp(ext->name, "LINUX_HAS_BPF_COOKIE") == 0) {
8424 value = kernel_supports(obj, FEAT_BPF_COOKIE);
8425 } else if (strcmp(ext->name, "LINUX_HAS_SYSCALL_WRAPPER") == 0) {
8426 value = kernel_supports(obj, FEAT_SYSCALL_WRAPPER);
8427 } else if (!str_has_pfx(ext->name, "LINUX_") || !ext->is_weak) {
8428 /* Currently libbpf supports only CONFIG_ and LINUX_ prefixed
8429 * __kconfig externs, where LINUX_ ones are virtual and filled out
8430 * customly by libbpf (their values don't come from Kconfig).
8431 * If LINUX_xxx variable is not recognized by libbpf, but is marked
8432 * __weak, it defaults to zero value, just like for CONFIG_xxx
8433 * externs.
8434 */
8435 pr_warn("extern (kcfg) '%s': unrecognized virtual extern\n", ext->name);
8436 return -EINVAL;
8437 }
8438
8439 err = set_kcfg_value_num(ext, ext_ptr, value);
8440 if (err)
8441 return err;
8442 pr_debug("extern (kcfg) '%s': set to 0x%llx\n",
8443 ext->name, (long long)value);
8444 } else {
8445 pr_warn("extern '%s': unrecognized extern kind\n", ext->name);
8446 return -EINVAL;
8447 }
8448 }
8449 if (need_config && extra_kconfig) {
8450 err = bpf_object__read_kconfig_mem(obj, extra_kconfig, kcfg_data);
8451 if (err)
8452 return -EINVAL;
8453 need_config = false;
8454 for (i = 0; i < obj->nr_extern; i++) {
8455 ext = &obj->externs[i];
8456 if (ext->type == EXT_KCFG && !ext->is_set) {
8457 need_config = true;
8458 break;
8459 }
8460 }
8461 }
8462 if (need_config) {
8463 err = bpf_object__read_kconfig_file(obj, kcfg_data);
8464 if (err)
8465 return -EINVAL;
8466 }
8467 if (need_kallsyms) {
8468 err = bpf_object__read_kallsyms_file(obj);
8469 if (err)
8470 return -EINVAL;
8471 }
8472 if (need_vmlinux_btf) {
8473 err = bpf_object__resolve_ksyms_btf_id(obj);
8474 if (err)
8475 return -EINVAL;
8476 }
8477 for (i = 0; i < obj->nr_extern; i++) {
8478 ext = &obj->externs[i];
8479
8480 if (!ext->is_set && !ext->is_weak) {
8481 pr_warn("extern '%s' (strong): not resolved\n", ext->name);
8482 return -ESRCH;
8483 } else if (!ext->is_set) {
8484 pr_debug("extern '%s' (weak): not resolved, defaulting to zero\n",
8485 ext->name);
8486 }
8487 }
8488
8489 return 0;
8490 }
8491
bpf_map_prepare_vdata(const struct bpf_map * map)8492 static void bpf_map_prepare_vdata(const struct bpf_map *map)
8493 {
8494 const struct btf_type *type;
8495 struct bpf_struct_ops *st_ops;
8496 __u32 i;
8497
8498 st_ops = map->st_ops;
8499 type = btf__type_by_id(map->obj->btf, st_ops->type_id);
8500 for (i = 0; i < btf_vlen(type); i++) {
8501 struct bpf_program *prog = st_ops->progs[i];
8502 void *kern_data;
8503 int prog_fd;
8504
8505 if (!prog)
8506 continue;
8507
8508 prog_fd = bpf_program__fd(prog);
8509 kern_data = st_ops->kern_vdata + st_ops->kern_func_off[i];
8510 *(unsigned long *)kern_data = prog_fd;
8511 }
8512 }
8513
bpf_object_prepare_struct_ops(struct bpf_object * obj)8514 static int bpf_object_prepare_struct_ops(struct bpf_object *obj)
8515 {
8516 struct bpf_map *map;
8517 int i;
8518
8519 for (i = 0; i < obj->nr_maps; i++) {
8520 map = &obj->maps[i];
8521
8522 if (!bpf_map__is_struct_ops(map))
8523 continue;
8524
8525 if (!map->autocreate)
8526 continue;
8527
8528 bpf_map_prepare_vdata(map);
8529 }
8530
8531 return 0;
8532 }
8533
bpf_object_load(struct bpf_object * obj,int extra_log_level,const char * target_btf_path)8534 static int bpf_object_load(struct bpf_object *obj, int extra_log_level, const char *target_btf_path)
8535 {
8536 int err, i;
8537
8538 if (!obj)
8539 return libbpf_err(-EINVAL);
8540
8541 if (obj->loaded) {
8542 pr_warn("object '%s': load can't be attempted twice\n", obj->name);
8543 return libbpf_err(-EINVAL);
8544 }
8545
8546 /* Disallow kernel loading programs of non-native endianness but
8547 * permit cross-endian creation of "light skeleton".
8548 */
8549 if (obj->gen_loader) {
8550 bpf_gen__init(obj->gen_loader, extra_log_level, obj->nr_programs, obj->nr_maps);
8551 } else if (!is_native_endianness(obj)) {
8552 pr_warn("object '%s': loading non-native endianness is unsupported\n", obj->name);
8553 return libbpf_err(-LIBBPF_ERRNO__ENDIAN);
8554 }
8555
8556 err = bpf_object_prepare_token(obj);
8557 err = err ? : bpf_object__probe_loading(obj);
8558 err = err ? : bpf_object__load_vmlinux_btf(obj, false);
8559 err = err ? : bpf_object__resolve_externs(obj, obj->kconfig);
8560 err = err ? : bpf_object__sanitize_maps(obj);
8561 err = err ? : bpf_object__init_kern_struct_ops_maps(obj);
8562 err = err ? : bpf_object_adjust_struct_ops_autoload(obj);
8563 err = err ? : bpf_object__relocate(obj, obj->btf_custom_path ? : target_btf_path);
8564 err = err ? : bpf_object__sanitize_and_load_btf(obj);
8565 err = err ? : bpf_object__create_maps(obj);
8566 err = err ? : bpf_object__load_progs(obj, extra_log_level);
8567 err = err ? : bpf_object_init_prog_arrays(obj);
8568 err = err ? : bpf_object_prepare_struct_ops(obj);
8569
8570 if (obj->gen_loader) {
8571 /* reset FDs */
8572 if (obj->btf)
8573 btf__set_fd(obj->btf, -1);
8574 if (!err)
8575 err = bpf_gen__finish(obj->gen_loader, obj->nr_programs, obj->nr_maps);
8576 }
8577
8578 /* clean up fd_array */
8579 zfree(&obj->fd_array);
8580
8581 /* clean up module BTFs */
8582 for (i = 0; i < obj->btf_module_cnt; i++) {
8583 close(obj->btf_modules[i].fd);
8584 btf__free(obj->btf_modules[i].btf);
8585 free(obj->btf_modules[i].name);
8586 }
8587 free(obj->btf_modules);
8588
8589 /* clean up vmlinux BTF */
8590 btf__free(obj->btf_vmlinux);
8591 obj->btf_vmlinux = NULL;
8592
8593 obj->loaded = true; /* doesn't matter if successfully or not */
8594
8595 if (err)
8596 goto out;
8597
8598 return 0;
8599 out:
8600 /* unpin any maps that were auto-pinned during load */
8601 for (i = 0; i < obj->nr_maps; i++)
8602 if (obj->maps[i].pinned && !obj->maps[i].reused)
8603 bpf_map__unpin(&obj->maps[i], NULL);
8604
8605 bpf_object_unload(obj);
8606 pr_warn("failed to load object '%s'\n", obj->path);
8607 return libbpf_err(err);
8608 }
8609
bpf_object__load(struct bpf_object * obj)8610 int bpf_object__load(struct bpf_object *obj)
8611 {
8612 return bpf_object_load(obj, 0, NULL);
8613 }
8614
make_parent_dir(const char * path)8615 static int make_parent_dir(const char *path)
8616 {
8617 char *dname, *dir;
8618 int err = 0;
8619
8620 dname = strdup(path);
8621 if (dname == NULL)
8622 return -ENOMEM;
8623
8624 dir = dirname(dname);
8625 if (mkdir(dir, 0700) && errno != EEXIST)
8626 err = -errno;
8627
8628 free(dname);
8629 if (err) {
8630 pr_warn("failed to mkdir %s: %s\n", path, errstr(err));
8631 }
8632 return err;
8633 }
8634
check_path(const char * path)8635 static int check_path(const char *path)
8636 {
8637 struct statfs st_fs;
8638 char *dname, *dir;
8639 int err = 0;
8640
8641 if (path == NULL)
8642 return -EINVAL;
8643
8644 dname = strdup(path);
8645 if (dname == NULL)
8646 return -ENOMEM;
8647
8648 dir = dirname(dname);
8649 if (statfs(dir, &st_fs)) {
8650 pr_warn("failed to statfs %s: %s\n", dir, errstr(errno));
8651 err = -errno;
8652 }
8653 free(dname);
8654
8655 if (!err && st_fs.f_type != BPF_FS_MAGIC) {
8656 pr_warn("specified path %s is not on BPF FS\n", path);
8657 err = -EINVAL;
8658 }
8659
8660 return err;
8661 }
8662
bpf_program__pin(struct bpf_program * prog,const char * path)8663 int bpf_program__pin(struct bpf_program *prog, const char *path)
8664 {
8665 int err;
8666
8667 if (prog->fd < 0) {
8668 pr_warn("prog '%s': can't pin program that wasn't loaded\n", prog->name);
8669 return libbpf_err(-EINVAL);
8670 }
8671
8672 err = make_parent_dir(path);
8673 if (err)
8674 return libbpf_err(err);
8675
8676 err = check_path(path);
8677 if (err)
8678 return libbpf_err(err);
8679
8680 if (bpf_obj_pin(prog->fd, path)) {
8681 err = -errno;
8682 pr_warn("prog '%s': failed to pin at '%s': %s\n", prog->name, path, errstr(err));
8683 return libbpf_err(err);
8684 }
8685
8686 pr_debug("prog '%s': pinned at '%s'\n", prog->name, path);
8687 return 0;
8688 }
8689
bpf_program__unpin(struct bpf_program * prog,const char * path)8690 int bpf_program__unpin(struct bpf_program *prog, const char *path)
8691 {
8692 int err;
8693
8694 if (prog->fd < 0) {
8695 pr_warn("prog '%s': can't unpin program that wasn't loaded\n", prog->name);
8696 return libbpf_err(-EINVAL);
8697 }
8698
8699 err = check_path(path);
8700 if (err)
8701 return libbpf_err(err);
8702
8703 err = unlink(path);
8704 if (err)
8705 return libbpf_err(-errno);
8706
8707 pr_debug("prog '%s': unpinned from '%s'\n", prog->name, path);
8708 return 0;
8709 }
8710
bpf_map__pin(struct bpf_map * map,const char * path)8711 int bpf_map__pin(struct bpf_map *map, const char *path)
8712 {
8713 int err;
8714
8715 if (map == NULL) {
8716 pr_warn("invalid map pointer\n");
8717 return libbpf_err(-EINVAL);
8718 }
8719
8720 if (map->fd < 0) {
8721 pr_warn("map '%s': can't pin BPF map without FD (was it created?)\n", map->name);
8722 return libbpf_err(-EINVAL);
8723 }
8724
8725 if (map->pin_path) {
8726 if (path && strcmp(path, map->pin_path)) {
8727 pr_warn("map '%s' already has pin path '%s' different from '%s'\n",
8728 bpf_map__name(map), map->pin_path, path);
8729 return libbpf_err(-EINVAL);
8730 } else if (map->pinned) {
8731 pr_debug("map '%s' already pinned at '%s'; not re-pinning\n",
8732 bpf_map__name(map), map->pin_path);
8733 return 0;
8734 }
8735 } else {
8736 if (!path) {
8737 pr_warn("missing a path to pin map '%s' at\n",
8738 bpf_map__name(map));
8739 return libbpf_err(-EINVAL);
8740 } else if (map->pinned) {
8741 pr_warn("map '%s' already pinned\n", bpf_map__name(map));
8742 return libbpf_err(-EEXIST);
8743 }
8744
8745 map->pin_path = strdup(path);
8746 if (!map->pin_path) {
8747 err = -errno;
8748 goto out_err;
8749 }
8750 }
8751
8752 err = make_parent_dir(map->pin_path);
8753 if (err)
8754 return libbpf_err(err);
8755
8756 err = check_path(map->pin_path);
8757 if (err)
8758 return libbpf_err(err);
8759
8760 if (bpf_obj_pin(map->fd, map->pin_path)) {
8761 err = -errno;
8762 goto out_err;
8763 }
8764
8765 map->pinned = true;
8766 pr_debug("pinned map '%s'\n", map->pin_path);
8767
8768 return 0;
8769
8770 out_err:
8771 pr_warn("failed to pin map: %s\n", errstr(err));
8772 return libbpf_err(err);
8773 }
8774
bpf_map__unpin(struct bpf_map * map,const char * path)8775 int bpf_map__unpin(struct bpf_map *map, const char *path)
8776 {
8777 int err;
8778
8779 if (map == NULL) {
8780 pr_warn("invalid map pointer\n");
8781 return libbpf_err(-EINVAL);
8782 }
8783
8784 if (map->pin_path) {
8785 if (path && strcmp(path, map->pin_path)) {
8786 pr_warn("map '%s' already has pin path '%s' different from '%s'\n",
8787 bpf_map__name(map), map->pin_path, path);
8788 return libbpf_err(-EINVAL);
8789 }
8790 path = map->pin_path;
8791 } else if (!path) {
8792 pr_warn("no path to unpin map '%s' from\n",
8793 bpf_map__name(map));
8794 return libbpf_err(-EINVAL);
8795 }
8796
8797 err = check_path(path);
8798 if (err)
8799 return libbpf_err(err);
8800
8801 err = unlink(path);
8802 if (err != 0)
8803 return libbpf_err(-errno);
8804
8805 map->pinned = false;
8806 pr_debug("unpinned map '%s' from '%s'\n", bpf_map__name(map), path);
8807
8808 return 0;
8809 }
8810
bpf_map__set_pin_path(struct bpf_map * map,const char * path)8811 int bpf_map__set_pin_path(struct bpf_map *map, const char *path)
8812 {
8813 char *new = NULL;
8814
8815 if (path) {
8816 new = strdup(path);
8817 if (!new)
8818 return libbpf_err(-errno);
8819 }
8820
8821 free(map->pin_path);
8822 map->pin_path = new;
8823 return 0;
8824 }
8825
8826 __alias(bpf_map__pin_path)
8827 const char *bpf_map__get_pin_path(const struct bpf_map *map);
8828
bpf_map__pin_path(const struct bpf_map * map)8829 const char *bpf_map__pin_path(const struct bpf_map *map)
8830 {
8831 return map->pin_path;
8832 }
8833
bpf_map__is_pinned(const struct bpf_map * map)8834 bool bpf_map__is_pinned(const struct bpf_map *map)
8835 {
8836 return map->pinned;
8837 }
8838
sanitize_pin_path(char * s)8839 static void sanitize_pin_path(char *s)
8840 {
8841 /* bpffs disallows periods in path names */
8842 while (*s) {
8843 if (*s == '.')
8844 *s = '_';
8845 s++;
8846 }
8847 }
8848
bpf_object__pin_maps(struct bpf_object * obj,const char * path)8849 int bpf_object__pin_maps(struct bpf_object *obj, const char *path)
8850 {
8851 struct bpf_map *map;
8852 int err;
8853
8854 if (!obj)
8855 return libbpf_err(-ENOENT);
8856
8857 if (!obj->loaded) {
8858 pr_warn("object not yet loaded; load it first\n");
8859 return libbpf_err(-ENOENT);
8860 }
8861
8862 bpf_object__for_each_map(map, obj) {
8863 char *pin_path = NULL;
8864 char buf[PATH_MAX];
8865
8866 if (!map->autocreate)
8867 continue;
8868
8869 if (path) {
8870 err = pathname_concat(buf, sizeof(buf), path, bpf_map__name(map));
8871 if (err)
8872 goto err_unpin_maps;
8873 sanitize_pin_path(buf);
8874 pin_path = buf;
8875 } else if (!map->pin_path) {
8876 continue;
8877 }
8878
8879 err = bpf_map__pin(map, pin_path);
8880 if (err)
8881 goto err_unpin_maps;
8882 }
8883
8884 return 0;
8885
8886 err_unpin_maps:
8887 while ((map = bpf_object__prev_map(obj, map))) {
8888 if (!map->pin_path)
8889 continue;
8890
8891 bpf_map__unpin(map, NULL);
8892 }
8893
8894 return libbpf_err(err);
8895 }
8896
bpf_object__unpin_maps(struct bpf_object * obj,const char * path)8897 int bpf_object__unpin_maps(struct bpf_object *obj, const char *path)
8898 {
8899 struct bpf_map *map;
8900 int err;
8901
8902 if (!obj)
8903 return libbpf_err(-ENOENT);
8904
8905 bpf_object__for_each_map(map, obj) {
8906 char *pin_path = NULL;
8907 char buf[PATH_MAX];
8908
8909 if (path) {
8910 err = pathname_concat(buf, sizeof(buf), path, bpf_map__name(map));
8911 if (err)
8912 return libbpf_err(err);
8913 sanitize_pin_path(buf);
8914 pin_path = buf;
8915 } else if (!map->pin_path) {
8916 continue;
8917 }
8918
8919 err = bpf_map__unpin(map, pin_path);
8920 if (err)
8921 return libbpf_err(err);
8922 }
8923
8924 return 0;
8925 }
8926
bpf_object__pin_programs(struct bpf_object * obj,const char * path)8927 int bpf_object__pin_programs(struct bpf_object *obj, const char *path)
8928 {
8929 struct bpf_program *prog;
8930 char buf[PATH_MAX];
8931 int err;
8932
8933 if (!obj)
8934 return libbpf_err(-ENOENT);
8935
8936 if (!obj->loaded) {
8937 pr_warn("object not yet loaded; load it first\n");
8938 return libbpf_err(-ENOENT);
8939 }
8940
8941 bpf_object__for_each_program(prog, obj) {
8942 err = pathname_concat(buf, sizeof(buf), path, prog->name);
8943 if (err)
8944 goto err_unpin_programs;
8945
8946 err = bpf_program__pin(prog, buf);
8947 if (err)
8948 goto err_unpin_programs;
8949 }
8950
8951 return 0;
8952
8953 err_unpin_programs:
8954 while ((prog = bpf_object__prev_program(obj, prog))) {
8955 if (pathname_concat(buf, sizeof(buf), path, prog->name))
8956 continue;
8957
8958 bpf_program__unpin(prog, buf);
8959 }
8960
8961 return libbpf_err(err);
8962 }
8963
bpf_object__unpin_programs(struct bpf_object * obj,const char * path)8964 int bpf_object__unpin_programs(struct bpf_object *obj, const char *path)
8965 {
8966 struct bpf_program *prog;
8967 int err;
8968
8969 if (!obj)
8970 return libbpf_err(-ENOENT);
8971
8972 bpf_object__for_each_program(prog, obj) {
8973 char buf[PATH_MAX];
8974
8975 err = pathname_concat(buf, sizeof(buf), path, prog->name);
8976 if (err)
8977 return libbpf_err(err);
8978
8979 err = bpf_program__unpin(prog, buf);
8980 if (err)
8981 return libbpf_err(err);
8982 }
8983
8984 return 0;
8985 }
8986
bpf_object__pin(struct bpf_object * obj,const char * path)8987 int bpf_object__pin(struct bpf_object *obj, const char *path)
8988 {
8989 int err;
8990
8991 err = bpf_object__pin_maps(obj, path);
8992 if (err)
8993 return libbpf_err(err);
8994
8995 err = bpf_object__pin_programs(obj, path);
8996 if (err) {
8997 bpf_object__unpin_maps(obj, path);
8998 return libbpf_err(err);
8999 }
9000
9001 return 0;
9002 }
9003
bpf_object__unpin(struct bpf_object * obj,const char * path)9004 int bpf_object__unpin(struct bpf_object *obj, const char *path)
9005 {
9006 int err;
9007
9008 err = bpf_object__unpin_programs(obj, path);
9009 if (err)
9010 return libbpf_err(err);
9011
9012 err = bpf_object__unpin_maps(obj, path);
9013 if (err)
9014 return libbpf_err(err);
9015
9016 return 0;
9017 }
9018
bpf_map__destroy(struct bpf_map * map)9019 static void bpf_map__destroy(struct bpf_map *map)
9020 {
9021 if (map->inner_map) {
9022 bpf_map__destroy(map->inner_map);
9023 zfree(&map->inner_map);
9024 }
9025
9026 zfree(&map->init_slots);
9027 map->init_slots_sz = 0;
9028
9029 if (map->mmaped && map->mmaped != map->obj->arena_data)
9030 munmap(map->mmaped, bpf_map_mmap_sz(map));
9031 map->mmaped = NULL;
9032
9033 if (map->st_ops) {
9034 zfree(&map->st_ops->data);
9035 zfree(&map->st_ops->progs);
9036 zfree(&map->st_ops->kern_func_off);
9037 zfree(&map->st_ops);
9038 }
9039
9040 zfree(&map->name);
9041 zfree(&map->real_name);
9042 zfree(&map->pin_path);
9043
9044 if (map->fd >= 0)
9045 zclose(map->fd);
9046 }
9047
bpf_object__close(struct bpf_object * obj)9048 void bpf_object__close(struct bpf_object *obj)
9049 {
9050 size_t i;
9051
9052 if (IS_ERR_OR_NULL(obj))
9053 return;
9054
9055 usdt_manager_free(obj->usdt_man);
9056 obj->usdt_man = NULL;
9057
9058 bpf_gen__free(obj->gen_loader);
9059 bpf_object__elf_finish(obj);
9060 bpf_object_unload(obj);
9061 btf__free(obj->btf);
9062 btf__free(obj->btf_vmlinux);
9063 btf_ext__free(obj->btf_ext);
9064
9065 for (i = 0; i < obj->nr_maps; i++)
9066 bpf_map__destroy(&obj->maps[i]);
9067
9068 zfree(&obj->btf_custom_path);
9069 zfree(&obj->kconfig);
9070
9071 for (i = 0; i < obj->nr_extern; i++)
9072 zfree(&obj->externs[i].essent_name);
9073
9074 zfree(&obj->externs);
9075 obj->nr_extern = 0;
9076
9077 zfree(&obj->maps);
9078 obj->nr_maps = 0;
9079
9080 if (obj->programs && obj->nr_programs) {
9081 for (i = 0; i < obj->nr_programs; i++)
9082 bpf_program__exit(&obj->programs[i]);
9083 }
9084 zfree(&obj->programs);
9085
9086 zfree(&obj->feat_cache);
9087 zfree(&obj->token_path);
9088 if (obj->token_fd > 0)
9089 close(obj->token_fd);
9090
9091 zfree(&obj->arena_data);
9092
9093 free(obj);
9094 }
9095
bpf_object__name(const struct bpf_object * obj)9096 const char *bpf_object__name(const struct bpf_object *obj)
9097 {
9098 return obj ? obj->name : libbpf_err_ptr(-EINVAL);
9099 }
9100
bpf_object__kversion(const struct bpf_object * obj)9101 unsigned int bpf_object__kversion(const struct bpf_object *obj)
9102 {
9103 return obj ? obj->kern_version : 0;
9104 }
9105
bpf_object__token_fd(const struct bpf_object * obj)9106 int bpf_object__token_fd(const struct bpf_object *obj)
9107 {
9108 return obj->token_fd ?: -1;
9109 }
9110
bpf_object__btf(const struct bpf_object * obj)9111 struct btf *bpf_object__btf(const struct bpf_object *obj)
9112 {
9113 return obj ? obj->btf : NULL;
9114 }
9115
bpf_object__btf_fd(const struct bpf_object * obj)9116 int bpf_object__btf_fd(const struct bpf_object *obj)
9117 {
9118 return obj->btf ? btf__fd(obj->btf) : -1;
9119 }
9120
bpf_object__set_kversion(struct bpf_object * obj,__u32 kern_version)9121 int bpf_object__set_kversion(struct bpf_object *obj, __u32 kern_version)
9122 {
9123 if (obj->loaded)
9124 return libbpf_err(-EINVAL);
9125
9126 obj->kern_version = kern_version;
9127
9128 return 0;
9129 }
9130
bpf_object__gen_loader(struct bpf_object * obj,struct gen_loader_opts * opts)9131 int bpf_object__gen_loader(struct bpf_object *obj, struct gen_loader_opts *opts)
9132 {
9133 struct bpf_gen *gen;
9134
9135 if (!opts)
9136 return -EFAULT;
9137 if (!OPTS_VALID(opts, gen_loader_opts))
9138 return -EINVAL;
9139 gen = calloc(sizeof(*gen), 1);
9140 if (!gen)
9141 return -ENOMEM;
9142 gen->opts = opts;
9143 gen->swapped_endian = !is_native_endianness(obj);
9144 obj->gen_loader = gen;
9145 return 0;
9146 }
9147
9148 static struct bpf_program *
__bpf_program__iter(const struct bpf_program * p,const struct bpf_object * obj,bool forward)9149 __bpf_program__iter(const struct bpf_program *p, const struct bpf_object *obj,
9150 bool forward)
9151 {
9152 size_t nr_programs = obj->nr_programs;
9153 ssize_t idx;
9154
9155 if (!nr_programs)
9156 return NULL;
9157
9158 if (!p)
9159 /* Iter from the beginning */
9160 return forward ? &obj->programs[0] :
9161 &obj->programs[nr_programs - 1];
9162
9163 if (p->obj != obj) {
9164 pr_warn("error: program handler doesn't match object\n");
9165 return errno = EINVAL, NULL;
9166 }
9167
9168 idx = (p - obj->programs) + (forward ? 1 : -1);
9169 if (idx >= obj->nr_programs || idx < 0)
9170 return NULL;
9171 return &obj->programs[idx];
9172 }
9173
9174 struct bpf_program *
bpf_object__next_program(const struct bpf_object * obj,struct bpf_program * prev)9175 bpf_object__next_program(const struct bpf_object *obj, struct bpf_program *prev)
9176 {
9177 struct bpf_program *prog = prev;
9178
9179 do {
9180 prog = __bpf_program__iter(prog, obj, true);
9181 } while (prog && prog_is_subprog(obj, prog));
9182
9183 return prog;
9184 }
9185
9186 struct bpf_program *
bpf_object__prev_program(const struct bpf_object * obj,struct bpf_program * next)9187 bpf_object__prev_program(const struct bpf_object *obj, struct bpf_program *next)
9188 {
9189 struct bpf_program *prog = next;
9190
9191 do {
9192 prog = __bpf_program__iter(prog, obj, false);
9193 } while (prog && prog_is_subprog(obj, prog));
9194
9195 return prog;
9196 }
9197
bpf_program__set_ifindex(struct bpf_program * prog,__u32 ifindex)9198 void bpf_program__set_ifindex(struct bpf_program *prog, __u32 ifindex)
9199 {
9200 prog->prog_ifindex = ifindex;
9201 }
9202
bpf_program__name(const struct bpf_program * prog)9203 const char *bpf_program__name(const struct bpf_program *prog)
9204 {
9205 return prog->name;
9206 }
9207
bpf_program__section_name(const struct bpf_program * prog)9208 const char *bpf_program__section_name(const struct bpf_program *prog)
9209 {
9210 return prog->sec_name;
9211 }
9212
bpf_program__autoload(const struct bpf_program * prog)9213 bool bpf_program__autoload(const struct bpf_program *prog)
9214 {
9215 return prog->autoload;
9216 }
9217
bpf_program__set_autoload(struct bpf_program * prog,bool autoload)9218 int bpf_program__set_autoload(struct bpf_program *prog, bool autoload)
9219 {
9220 if (prog->obj->loaded)
9221 return libbpf_err(-EINVAL);
9222
9223 prog->autoload = autoload;
9224 return 0;
9225 }
9226
bpf_program__autoattach(const struct bpf_program * prog)9227 bool bpf_program__autoattach(const struct bpf_program *prog)
9228 {
9229 return prog->autoattach;
9230 }
9231
bpf_program__set_autoattach(struct bpf_program * prog,bool autoattach)9232 void bpf_program__set_autoattach(struct bpf_program *prog, bool autoattach)
9233 {
9234 prog->autoattach = autoattach;
9235 }
9236
bpf_program__insns(const struct bpf_program * prog)9237 const struct bpf_insn *bpf_program__insns(const struct bpf_program *prog)
9238 {
9239 return prog->insns;
9240 }
9241
bpf_program__insn_cnt(const struct bpf_program * prog)9242 size_t bpf_program__insn_cnt(const struct bpf_program *prog)
9243 {
9244 return prog->insns_cnt;
9245 }
9246
bpf_program__set_insns(struct bpf_program * prog,struct bpf_insn * new_insns,size_t new_insn_cnt)9247 int bpf_program__set_insns(struct bpf_program *prog,
9248 struct bpf_insn *new_insns, size_t new_insn_cnt)
9249 {
9250 struct bpf_insn *insns;
9251
9252 if (prog->obj->loaded)
9253 return -EBUSY;
9254
9255 insns = libbpf_reallocarray(prog->insns, new_insn_cnt, sizeof(*insns));
9256 /* NULL is a valid return from reallocarray if the new count is zero */
9257 if (!insns && new_insn_cnt) {
9258 pr_warn("prog '%s': failed to realloc prog code\n", prog->name);
9259 return -ENOMEM;
9260 }
9261 memcpy(insns, new_insns, new_insn_cnt * sizeof(*insns));
9262
9263 prog->insns = insns;
9264 prog->insns_cnt = new_insn_cnt;
9265 return 0;
9266 }
9267
bpf_program__fd(const struct bpf_program * prog)9268 int bpf_program__fd(const struct bpf_program *prog)
9269 {
9270 if (!prog)
9271 return libbpf_err(-EINVAL);
9272
9273 if (prog->fd < 0)
9274 return libbpf_err(-ENOENT);
9275
9276 return prog->fd;
9277 }
9278
9279 __alias(bpf_program__type)
9280 enum bpf_prog_type bpf_program__get_type(const struct bpf_program *prog);
9281
bpf_program__type(const struct bpf_program * prog)9282 enum bpf_prog_type bpf_program__type(const struct bpf_program *prog)
9283 {
9284 return prog->type;
9285 }
9286
9287 static size_t custom_sec_def_cnt;
9288 static struct bpf_sec_def *custom_sec_defs;
9289 static struct bpf_sec_def custom_fallback_def;
9290 static bool has_custom_fallback_def;
9291 static int last_custom_sec_def_handler_id;
9292
bpf_program__set_type(struct bpf_program * prog,enum bpf_prog_type type)9293 int bpf_program__set_type(struct bpf_program *prog, enum bpf_prog_type type)
9294 {
9295 if (prog->obj->loaded)
9296 return libbpf_err(-EBUSY);
9297
9298 /* if type is not changed, do nothing */
9299 if (prog->type == type)
9300 return 0;
9301
9302 prog->type = type;
9303
9304 /* If a program type was changed, we need to reset associated SEC()
9305 * handler, as it will be invalid now. The only exception is a generic
9306 * fallback handler, which by definition is program type-agnostic and
9307 * is a catch-all custom handler, optionally set by the application,
9308 * so should be able to handle any type of BPF program.
9309 */
9310 if (prog->sec_def != &custom_fallback_def)
9311 prog->sec_def = NULL;
9312 return 0;
9313 }
9314
9315 __alias(bpf_program__expected_attach_type)
9316 enum bpf_attach_type bpf_program__get_expected_attach_type(const struct bpf_program *prog);
9317
bpf_program__expected_attach_type(const struct bpf_program * prog)9318 enum bpf_attach_type bpf_program__expected_attach_type(const struct bpf_program *prog)
9319 {
9320 return prog->expected_attach_type;
9321 }
9322
bpf_program__set_expected_attach_type(struct bpf_program * prog,enum bpf_attach_type type)9323 int bpf_program__set_expected_attach_type(struct bpf_program *prog,
9324 enum bpf_attach_type type)
9325 {
9326 if (prog->obj->loaded)
9327 return libbpf_err(-EBUSY);
9328
9329 prog->expected_attach_type = type;
9330 return 0;
9331 }
9332
bpf_program__flags(const struct bpf_program * prog)9333 __u32 bpf_program__flags(const struct bpf_program *prog)
9334 {
9335 return prog->prog_flags;
9336 }
9337
bpf_program__set_flags(struct bpf_program * prog,__u32 flags)9338 int bpf_program__set_flags(struct bpf_program *prog, __u32 flags)
9339 {
9340 if (prog->obj->loaded)
9341 return libbpf_err(-EBUSY);
9342
9343 prog->prog_flags = flags;
9344 return 0;
9345 }
9346
bpf_program__log_level(const struct bpf_program * prog)9347 __u32 bpf_program__log_level(const struct bpf_program *prog)
9348 {
9349 return prog->log_level;
9350 }
9351
bpf_program__set_log_level(struct bpf_program * prog,__u32 log_level)9352 int bpf_program__set_log_level(struct bpf_program *prog, __u32 log_level)
9353 {
9354 if (prog->obj->loaded)
9355 return libbpf_err(-EBUSY);
9356
9357 prog->log_level = log_level;
9358 return 0;
9359 }
9360
bpf_program__log_buf(const struct bpf_program * prog,size_t * log_size)9361 const char *bpf_program__log_buf(const struct bpf_program *prog, size_t *log_size)
9362 {
9363 *log_size = prog->log_size;
9364 return prog->log_buf;
9365 }
9366
bpf_program__set_log_buf(struct bpf_program * prog,char * log_buf,size_t log_size)9367 int bpf_program__set_log_buf(struct bpf_program *prog, char *log_buf, size_t log_size)
9368 {
9369 if (log_size && !log_buf)
9370 return -EINVAL;
9371 if (prog->log_size > UINT_MAX)
9372 return -EINVAL;
9373 if (prog->obj->loaded)
9374 return -EBUSY;
9375
9376 prog->log_buf = log_buf;
9377 prog->log_size = log_size;
9378 return 0;
9379 }
9380
9381 #define SEC_DEF(sec_pfx, ptype, atype, flags, ...) { \
9382 .sec = (char *)sec_pfx, \
9383 .prog_type = BPF_PROG_TYPE_##ptype, \
9384 .expected_attach_type = atype, \
9385 .cookie = (long)(flags), \
9386 .prog_prepare_load_fn = libbpf_prepare_prog_load, \
9387 __VA_ARGS__ \
9388 }
9389
9390 static int attach_kprobe(const struct bpf_program *prog, long cookie, struct bpf_link **link);
9391 static int attach_uprobe(const struct bpf_program *prog, long cookie, struct bpf_link **link);
9392 static int attach_ksyscall(const struct bpf_program *prog, long cookie, struct bpf_link **link);
9393 static int attach_usdt(const struct bpf_program *prog, long cookie, struct bpf_link **link);
9394 static int attach_tp(const struct bpf_program *prog, long cookie, struct bpf_link **link);
9395 static int attach_raw_tp(const struct bpf_program *prog, long cookie, struct bpf_link **link);
9396 static int attach_trace(const struct bpf_program *prog, long cookie, struct bpf_link **link);
9397 static int attach_kprobe_multi(const struct bpf_program *prog, long cookie, struct bpf_link **link);
9398 static int attach_kprobe_session(const struct bpf_program *prog, long cookie, struct bpf_link **link);
9399 static int attach_uprobe_multi(const struct bpf_program *prog, long cookie, struct bpf_link **link);
9400 static int attach_lsm(const struct bpf_program *prog, long cookie, struct bpf_link **link);
9401 static int attach_iter(const struct bpf_program *prog, long cookie, struct bpf_link **link);
9402
9403 static const struct bpf_sec_def section_defs[] = {
9404 SEC_DEF("socket", SOCKET_FILTER, 0, SEC_NONE),
9405 SEC_DEF("sk_reuseport/migrate", SK_REUSEPORT, BPF_SK_REUSEPORT_SELECT_OR_MIGRATE, SEC_ATTACHABLE),
9406 SEC_DEF("sk_reuseport", SK_REUSEPORT, BPF_SK_REUSEPORT_SELECT, SEC_ATTACHABLE),
9407 SEC_DEF("kprobe+", KPROBE, 0, SEC_NONE, attach_kprobe),
9408 SEC_DEF("uprobe+", KPROBE, 0, SEC_NONE, attach_uprobe),
9409 SEC_DEF("uprobe.s+", KPROBE, 0, SEC_SLEEPABLE, attach_uprobe),
9410 SEC_DEF("kretprobe+", KPROBE, 0, SEC_NONE, attach_kprobe),
9411 SEC_DEF("uretprobe+", KPROBE, 0, SEC_NONE, attach_uprobe),
9412 SEC_DEF("uretprobe.s+", KPROBE, 0, SEC_SLEEPABLE, attach_uprobe),
9413 SEC_DEF("kprobe.multi+", KPROBE, BPF_TRACE_KPROBE_MULTI, SEC_NONE, attach_kprobe_multi),
9414 SEC_DEF("kretprobe.multi+", KPROBE, BPF_TRACE_KPROBE_MULTI, SEC_NONE, attach_kprobe_multi),
9415 SEC_DEF("kprobe.session+", KPROBE, BPF_TRACE_KPROBE_SESSION, SEC_NONE, attach_kprobe_session),
9416 SEC_DEF("uprobe.multi+", KPROBE, BPF_TRACE_UPROBE_MULTI, SEC_NONE, attach_uprobe_multi),
9417 SEC_DEF("uretprobe.multi+", KPROBE, BPF_TRACE_UPROBE_MULTI, SEC_NONE, attach_uprobe_multi),
9418 SEC_DEF("uprobe.session+", KPROBE, BPF_TRACE_UPROBE_SESSION, SEC_NONE, attach_uprobe_multi),
9419 SEC_DEF("uprobe.multi.s+", KPROBE, BPF_TRACE_UPROBE_MULTI, SEC_SLEEPABLE, attach_uprobe_multi),
9420 SEC_DEF("uretprobe.multi.s+", KPROBE, BPF_TRACE_UPROBE_MULTI, SEC_SLEEPABLE, attach_uprobe_multi),
9421 SEC_DEF("uprobe.session.s+", KPROBE, BPF_TRACE_UPROBE_SESSION, SEC_SLEEPABLE, attach_uprobe_multi),
9422 SEC_DEF("ksyscall+", KPROBE, 0, SEC_NONE, attach_ksyscall),
9423 SEC_DEF("kretsyscall+", KPROBE, 0, SEC_NONE, attach_ksyscall),
9424 SEC_DEF("usdt+", KPROBE, 0, SEC_USDT, attach_usdt),
9425 SEC_DEF("usdt.s+", KPROBE, 0, SEC_USDT | SEC_SLEEPABLE, attach_usdt),
9426 SEC_DEF("tc/ingress", SCHED_CLS, BPF_TCX_INGRESS, SEC_NONE), /* alias for tcx */
9427 SEC_DEF("tc/egress", SCHED_CLS, BPF_TCX_EGRESS, SEC_NONE), /* alias for tcx */
9428 SEC_DEF("tcx/ingress", SCHED_CLS, BPF_TCX_INGRESS, SEC_NONE),
9429 SEC_DEF("tcx/egress", SCHED_CLS, BPF_TCX_EGRESS, SEC_NONE),
9430 SEC_DEF("tc", SCHED_CLS, 0, SEC_NONE), /* deprecated / legacy, use tcx */
9431 SEC_DEF("classifier", SCHED_CLS, 0, SEC_NONE), /* deprecated / legacy, use tcx */
9432 SEC_DEF("action", SCHED_ACT, 0, SEC_NONE), /* deprecated / legacy, use tcx */
9433 SEC_DEF("netkit/primary", SCHED_CLS, BPF_NETKIT_PRIMARY, SEC_NONE),
9434 SEC_DEF("netkit/peer", SCHED_CLS, BPF_NETKIT_PEER, SEC_NONE),
9435 SEC_DEF("tracepoint+", TRACEPOINT, 0, SEC_NONE, attach_tp),
9436 SEC_DEF("tp+", TRACEPOINT, 0, SEC_NONE, attach_tp),
9437 SEC_DEF("raw_tracepoint+", RAW_TRACEPOINT, 0, SEC_NONE, attach_raw_tp),
9438 SEC_DEF("raw_tp+", RAW_TRACEPOINT, 0, SEC_NONE, attach_raw_tp),
9439 SEC_DEF("raw_tracepoint.w+", RAW_TRACEPOINT_WRITABLE, 0, SEC_NONE, attach_raw_tp),
9440 SEC_DEF("raw_tp.w+", RAW_TRACEPOINT_WRITABLE, 0, SEC_NONE, attach_raw_tp),
9441 SEC_DEF("tp_btf+", TRACING, BPF_TRACE_RAW_TP, SEC_ATTACH_BTF, attach_trace),
9442 SEC_DEF("fentry+", TRACING, BPF_TRACE_FENTRY, SEC_ATTACH_BTF, attach_trace),
9443 SEC_DEF("fmod_ret+", TRACING, BPF_MODIFY_RETURN, SEC_ATTACH_BTF, attach_trace),
9444 SEC_DEF("fexit+", TRACING, BPF_TRACE_FEXIT, SEC_ATTACH_BTF, attach_trace),
9445 SEC_DEF("fentry.s+", TRACING, BPF_TRACE_FENTRY, SEC_ATTACH_BTF | SEC_SLEEPABLE, attach_trace),
9446 SEC_DEF("fmod_ret.s+", TRACING, BPF_MODIFY_RETURN, SEC_ATTACH_BTF | SEC_SLEEPABLE, attach_trace),
9447 SEC_DEF("fexit.s+", TRACING, BPF_TRACE_FEXIT, SEC_ATTACH_BTF | SEC_SLEEPABLE, attach_trace),
9448 SEC_DEF("freplace+", EXT, 0, SEC_ATTACH_BTF, attach_trace),
9449 SEC_DEF("lsm+", LSM, BPF_LSM_MAC, SEC_ATTACH_BTF, attach_lsm),
9450 SEC_DEF("lsm.s+", LSM, BPF_LSM_MAC, SEC_ATTACH_BTF | SEC_SLEEPABLE, attach_lsm),
9451 SEC_DEF("lsm_cgroup+", LSM, BPF_LSM_CGROUP, SEC_ATTACH_BTF),
9452 SEC_DEF("iter+", TRACING, BPF_TRACE_ITER, SEC_ATTACH_BTF, attach_iter),
9453 SEC_DEF("iter.s+", TRACING, BPF_TRACE_ITER, SEC_ATTACH_BTF | SEC_SLEEPABLE, attach_iter),
9454 SEC_DEF("syscall", SYSCALL, 0, SEC_SLEEPABLE),
9455 SEC_DEF("xdp.frags/devmap", XDP, BPF_XDP_DEVMAP, SEC_XDP_FRAGS),
9456 SEC_DEF("xdp/devmap", XDP, BPF_XDP_DEVMAP, SEC_ATTACHABLE),
9457 SEC_DEF("xdp.frags/cpumap", XDP, BPF_XDP_CPUMAP, SEC_XDP_FRAGS),
9458 SEC_DEF("xdp/cpumap", XDP, BPF_XDP_CPUMAP, SEC_ATTACHABLE),
9459 SEC_DEF("xdp.frags", XDP, BPF_XDP, SEC_XDP_FRAGS),
9460 SEC_DEF("xdp", XDP, BPF_XDP, SEC_ATTACHABLE_OPT),
9461 SEC_DEF("perf_event", PERF_EVENT, 0, SEC_NONE),
9462 SEC_DEF("lwt_in", LWT_IN, 0, SEC_NONE),
9463 SEC_DEF("lwt_out", LWT_OUT, 0, SEC_NONE),
9464 SEC_DEF("lwt_xmit", LWT_XMIT, 0, SEC_NONE),
9465 SEC_DEF("lwt_seg6local", LWT_SEG6LOCAL, 0, SEC_NONE),
9466 SEC_DEF("sockops", SOCK_OPS, BPF_CGROUP_SOCK_OPS, SEC_ATTACHABLE_OPT),
9467 SEC_DEF("sk_skb/stream_parser", SK_SKB, BPF_SK_SKB_STREAM_PARSER, SEC_ATTACHABLE_OPT),
9468 SEC_DEF("sk_skb/stream_verdict",SK_SKB, BPF_SK_SKB_STREAM_VERDICT, SEC_ATTACHABLE_OPT),
9469 SEC_DEF("sk_skb/verdict", SK_SKB, BPF_SK_SKB_VERDICT, SEC_ATTACHABLE_OPT),
9470 SEC_DEF("sk_skb", SK_SKB, 0, SEC_NONE),
9471 SEC_DEF("sk_msg", SK_MSG, BPF_SK_MSG_VERDICT, SEC_ATTACHABLE_OPT),
9472 SEC_DEF("lirc_mode2", LIRC_MODE2, BPF_LIRC_MODE2, SEC_ATTACHABLE_OPT),
9473 SEC_DEF("flow_dissector", FLOW_DISSECTOR, BPF_FLOW_DISSECTOR, SEC_ATTACHABLE_OPT),
9474 SEC_DEF("cgroup_skb/ingress", CGROUP_SKB, BPF_CGROUP_INET_INGRESS, SEC_ATTACHABLE_OPT),
9475 SEC_DEF("cgroup_skb/egress", CGROUP_SKB, BPF_CGROUP_INET_EGRESS, SEC_ATTACHABLE_OPT),
9476 SEC_DEF("cgroup/skb", CGROUP_SKB, 0, SEC_NONE),
9477 SEC_DEF("cgroup/sock_create", CGROUP_SOCK, BPF_CGROUP_INET_SOCK_CREATE, SEC_ATTACHABLE),
9478 SEC_DEF("cgroup/sock_release", CGROUP_SOCK, BPF_CGROUP_INET_SOCK_RELEASE, SEC_ATTACHABLE),
9479 SEC_DEF("cgroup/sock", CGROUP_SOCK, BPF_CGROUP_INET_SOCK_CREATE, SEC_ATTACHABLE_OPT),
9480 SEC_DEF("cgroup/post_bind4", CGROUP_SOCK, BPF_CGROUP_INET4_POST_BIND, SEC_ATTACHABLE),
9481 SEC_DEF("cgroup/post_bind6", CGROUP_SOCK, BPF_CGROUP_INET6_POST_BIND, SEC_ATTACHABLE),
9482 SEC_DEF("cgroup/bind4", CGROUP_SOCK_ADDR, BPF_CGROUP_INET4_BIND, SEC_ATTACHABLE),
9483 SEC_DEF("cgroup/bind6", CGROUP_SOCK_ADDR, BPF_CGROUP_INET6_BIND, SEC_ATTACHABLE),
9484 SEC_DEF("cgroup/connect4", CGROUP_SOCK_ADDR, BPF_CGROUP_INET4_CONNECT, SEC_ATTACHABLE),
9485 SEC_DEF("cgroup/connect6", CGROUP_SOCK_ADDR, BPF_CGROUP_INET6_CONNECT, SEC_ATTACHABLE),
9486 SEC_DEF("cgroup/connect_unix", CGROUP_SOCK_ADDR, BPF_CGROUP_UNIX_CONNECT, SEC_ATTACHABLE),
9487 SEC_DEF("cgroup/sendmsg4", CGROUP_SOCK_ADDR, BPF_CGROUP_UDP4_SENDMSG, SEC_ATTACHABLE),
9488 SEC_DEF("cgroup/sendmsg6", CGROUP_SOCK_ADDR, BPF_CGROUP_UDP6_SENDMSG, SEC_ATTACHABLE),
9489 SEC_DEF("cgroup/sendmsg_unix", CGROUP_SOCK_ADDR, BPF_CGROUP_UNIX_SENDMSG, SEC_ATTACHABLE),
9490 SEC_DEF("cgroup/recvmsg4", CGROUP_SOCK_ADDR, BPF_CGROUP_UDP4_RECVMSG, SEC_ATTACHABLE),
9491 SEC_DEF("cgroup/recvmsg6", CGROUP_SOCK_ADDR, BPF_CGROUP_UDP6_RECVMSG, SEC_ATTACHABLE),
9492 SEC_DEF("cgroup/recvmsg_unix", CGROUP_SOCK_ADDR, BPF_CGROUP_UNIX_RECVMSG, SEC_ATTACHABLE),
9493 SEC_DEF("cgroup/getpeername4", CGROUP_SOCK_ADDR, BPF_CGROUP_INET4_GETPEERNAME, SEC_ATTACHABLE),
9494 SEC_DEF("cgroup/getpeername6", CGROUP_SOCK_ADDR, BPF_CGROUP_INET6_GETPEERNAME, SEC_ATTACHABLE),
9495 SEC_DEF("cgroup/getpeername_unix", CGROUP_SOCK_ADDR, BPF_CGROUP_UNIX_GETPEERNAME, SEC_ATTACHABLE),
9496 SEC_DEF("cgroup/getsockname4", CGROUP_SOCK_ADDR, BPF_CGROUP_INET4_GETSOCKNAME, SEC_ATTACHABLE),
9497 SEC_DEF("cgroup/getsockname6", CGROUP_SOCK_ADDR, BPF_CGROUP_INET6_GETSOCKNAME, SEC_ATTACHABLE),
9498 SEC_DEF("cgroup/getsockname_unix", CGROUP_SOCK_ADDR, BPF_CGROUP_UNIX_GETSOCKNAME, SEC_ATTACHABLE),
9499 SEC_DEF("cgroup/sysctl", CGROUP_SYSCTL, BPF_CGROUP_SYSCTL, SEC_ATTACHABLE),
9500 SEC_DEF("cgroup/getsockopt", CGROUP_SOCKOPT, BPF_CGROUP_GETSOCKOPT, SEC_ATTACHABLE),
9501 SEC_DEF("cgroup/setsockopt", CGROUP_SOCKOPT, BPF_CGROUP_SETSOCKOPT, SEC_ATTACHABLE),
9502 SEC_DEF("cgroup/dev", CGROUP_DEVICE, BPF_CGROUP_DEVICE, SEC_ATTACHABLE_OPT),
9503 SEC_DEF("struct_ops+", STRUCT_OPS, 0, SEC_NONE),
9504 SEC_DEF("struct_ops.s+", STRUCT_OPS, 0, SEC_SLEEPABLE),
9505 SEC_DEF("sk_lookup", SK_LOOKUP, BPF_SK_LOOKUP, SEC_ATTACHABLE),
9506 SEC_DEF("netfilter", NETFILTER, BPF_NETFILTER, SEC_NONE),
9507 };
9508
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)9509 int libbpf_register_prog_handler(const char *sec,
9510 enum bpf_prog_type prog_type,
9511 enum bpf_attach_type exp_attach_type,
9512 const struct libbpf_prog_handler_opts *opts)
9513 {
9514 struct bpf_sec_def *sec_def;
9515
9516 if (!OPTS_VALID(opts, libbpf_prog_handler_opts))
9517 return libbpf_err(-EINVAL);
9518
9519 if (last_custom_sec_def_handler_id == INT_MAX) /* prevent overflow */
9520 return libbpf_err(-E2BIG);
9521
9522 if (sec) {
9523 sec_def = libbpf_reallocarray(custom_sec_defs, custom_sec_def_cnt + 1,
9524 sizeof(*sec_def));
9525 if (!sec_def)
9526 return libbpf_err(-ENOMEM);
9527
9528 custom_sec_defs = sec_def;
9529 sec_def = &custom_sec_defs[custom_sec_def_cnt];
9530 } else {
9531 if (has_custom_fallback_def)
9532 return libbpf_err(-EBUSY);
9533
9534 sec_def = &custom_fallback_def;
9535 }
9536
9537 sec_def->sec = sec ? strdup(sec) : NULL;
9538 if (sec && !sec_def->sec)
9539 return libbpf_err(-ENOMEM);
9540
9541 sec_def->prog_type = prog_type;
9542 sec_def->expected_attach_type = exp_attach_type;
9543 sec_def->cookie = OPTS_GET(opts, cookie, 0);
9544
9545 sec_def->prog_setup_fn = OPTS_GET(opts, prog_setup_fn, NULL);
9546 sec_def->prog_prepare_load_fn = OPTS_GET(opts, prog_prepare_load_fn, NULL);
9547 sec_def->prog_attach_fn = OPTS_GET(opts, prog_attach_fn, NULL);
9548
9549 sec_def->handler_id = ++last_custom_sec_def_handler_id;
9550
9551 if (sec)
9552 custom_sec_def_cnt++;
9553 else
9554 has_custom_fallback_def = true;
9555
9556 return sec_def->handler_id;
9557 }
9558
libbpf_unregister_prog_handler(int handler_id)9559 int libbpf_unregister_prog_handler(int handler_id)
9560 {
9561 struct bpf_sec_def *sec_defs;
9562 int i;
9563
9564 if (handler_id <= 0)
9565 return libbpf_err(-EINVAL);
9566
9567 if (has_custom_fallback_def && custom_fallback_def.handler_id == handler_id) {
9568 memset(&custom_fallback_def, 0, sizeof(custom_fallback_def));
9569 has_custom_fallback_def = false;
9570 return 0;
9571 }
9572
9573 for (i = 0; i < custom_sec_def_cnt; i++) {
9574 if (custom_sec_defs[i].handler_id == handler_id)
9575 break;
9576 }
9577
9578 if (i == custom_sec_def_cnt)
9579 return libbpf_err(-ENOENT);
9580
9581 free(custom_sec_defs[i].sec);
9582 for (i = i + 1; i < custom_sec_def_cnt; i++)
9583 custom_sec_defs[i - 1] = custom_sec_defs[i];
9584 custom_sec_def_cnt--;
9585
9586 /* try to shrink the array, but it's ok if we couldn't */
9587 sec_defs = libbpf_reallocarray(custom_sec_defs, custom_sec_def_cnt, sizeof(*sec_defs));
9588 /* if new count is zero, reallocarray can return a valid NULL result;
9589 * in this case the previous pointer will be freed, so we *have to*
9590 * reassign old pointer to the new value (even if it's NULL)
9591 */
9592 if (sec_defs || custom_sec_def_cnt == 0)
9593 custom_sec_defs = sec_defs;
9594
9595 return 0;
9596 }
9597
sec_def_matches(const struct bpf_sec_def * sec_def,const char * sec_name)9598 static bool sec_def_matches(const struct bpf_sec_def *sec_def, const char *sec_name)
9599 {
9600 size_t len = strlen(sec_def->sec);
9601
9602 /* "type/" always has to have proper SEC("type/extras") form */
9603 if (sec_def->sec[len - 1] == '/') {
9604 if (str_has_pfx(sec_name, sec_def->sec))
9605 return true;
9606 return false;
9607 }
9608
9609 /* "type+" means it can be either exact SEC("type") or
9610 * well-formed SEC("type/extras") with proper '/' separator
9611 */
9612 if (sec_def->sec[len - 1] == '+') {
9613 len--;
9614 /* not even a prefix */
9615 if (strncmp(sec_name, sec_def->sec, len) != 0)
9616 return false;
9617 /* exact match or has '/' separator */
9618 if (sec_name[len] == '\0' || sec_name[len] == '/')
9619 return true;
9620 return false;
9621 }
9622
9623 return strcmp(sec_name, sec_def->sec) == 0;
9624 }
9625
find_sec_def(const char * sec_name)9626 static const struct bpf_sec_def *find_sec_def(const char *sec_name)
9627 {
9628 const struct bpf_sec_def *sec_def;
9629 int i, n;
9630
9631 n = custom_sec_def_cnt;
9632 for (i = 0; i < n; i++) {
9633 sec_def = &custom_sec_defs[i];
9634 if (sec_def_matches(sec_def, sec_name))
9635 return sec_def;
9636 }
9637
9638 n = ARRAY_SIZE(section_defs);
9639 for (i = 0; i < n; i++) {
9640 sec_def = §ion_defs[i];
9641 if (sec_def_matches(sec_def, sec_name))
9642 return sec_def;
9643 }
9644
9645 if (has_custom_fallback_def)
9646 return &custom_fallback_def;
9647
9648 return NULL;
9649 }
9650
9651 #define MAX_TYPE_NAME_SIZE 32
9652
libbpf_get_type_names(bool attach_type)9653 static char *libbpf_get_type_names(bool attach_type)
9654 {
9655 int i, len = ARRAY_SIZE(section_defs) * MAX_TYPE_NAME_SIZE;
9656 char *buf;
9657
9658 buf = malloc(len);
9659 if (!buf)
9660 return NULL;
9661
9662 buf[0] = '\0';
9663 /* Forge string buf with all available names */
9664 for (i = 0; i < ARRAY_SIZE(section_defs); i++) {
9665 const struct bpf_sec_def *sec_def = §ion_defs[i];
9666
9667 if (attach_type) {
9668 if (sec_def->prog_prepare_load_fn != libbpf_prepare_prog_load)
9669 continue;
9670
9671 if (!(sec_def->cookie & SEC_ATTACHABLE))
9672 continue;
9673 }
9674
9675 if (strlen(buf) + strlen(section_defs[i].sec) + 2 > len) {
9676 free(buf);
9677 return NULL;
9678 }
9679 strcat(buf, " ");
9680 strcat(buf, section_defs[i].sec);
9681 }
9682
9683 return buf;
9684 }
9685
libbpf_prog_type_by_name(const char * name,enum bpf_prog_type * prog_type,enum bpf_attach_type * expected_attach_type)9686 int libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,
9687 enum bpf_attach_type *expected_attach_type)
9688 {
9689 const struct bpf_sec_def *sec_def;
9690 char *type_names;
9691
9692 if (!name)
9693 return libbpf_err(-EINVAL);
9694
9695 sec_def = find_sec_def(name);
9696 if (sec_def) {
9697 *prog_type = sec_def->prog_type;
9698 *expected_attach_type = sec_def->expected_attach_type;
9699 return 0;
9700 }
9701
9702 pr_debug("failed to guess program type from ELF section '%s'\n", name);
9703 type_names = libbpf_get_type_names(false);
9704 if (type_names != NULL) {
9705 pr_debug("supported section(type) names are:%s\n", type_names);
9706 free(type_names);
9707 }
9708
9709 return libbpf_err(-ESRCH);
9710 }
9711
libbpf_bpf_attach_type_str(enum bpf_attach_type t)9712 const char *libbpf_bpf_attach_type_str(enum bpf_attach_type t)
9713 {
9714 if (t < 0 || t >= ARRAY_SIZE(attach_type_name))
9715 return NULL;
9716
9717 return attach_type_name[t];
9718 }
9719
libbpf_bpf_link_type_str(enum bpf_link_type t)9720 const char *libbpf_bpf_link_type_str(enum bpf_link_type t)
9721 {
9722 if (t < 0 || t >= ARRAY_SIZE(link_type_name))
9723 return NULL;
9724
9725 return link_type_name[t];
9726 }
9727
libbpf_bpf_map_type_str(enum bpf_map_type t)9728 const char *libbpf_bpf_map_type_str(enum bpf_map_type t)
9729 {
9730 if (t < 0 || t >= ARRAY_SIZE(map_type_name))
9731 return NULL;
9732
9733 return map_type_name[t];
9734 }
9735
libbpf_bpf_prog_type_str(enum bpf_prog_type t)9736 const char *libbpf_bpf_prog_type_str(enum bpf_prog_type t)
9737 {
9738 if (t < 0 || t >= ARRAY_SIZE(prog_type_name))
9739 return NULL;
9740
9741 return prog_type_name[t];
9742 }
9743
find_struct_ops_map_by_offset(struct bpf_object * obj,int sec_idx,size_t offset)9744 static struct bpf_map *find_struct_ops_map_by_offset(struct bpf_object *obj,
9745 int sec_idx,
9746 size_t offset)
9747 {
9748 struct bpf_map *map;
9749 size_t i;
9750
9751 for (i = 0; i < obj->nr_maps; i++) {
9752 map = &obj->maps[i];
9753 if (!bpf_map__is_struct_ops(map))
9754 continue;
9755 if (map->sec_idx == sec_idx &&
9756 map->sec_offset <= offset &&
9757 offset - map->sec_offset < map->def.value_size)
9758 return map;
9759 }
9760
9761 return NULL;
9762 }
9763
9764 /* Collect the reloc from ELF, populate the st_ops->progs[], and update
9765 * st_ops->data for shadow type.
9766 */
bpf_object__collect_st_ops_relos(struct bpf_object * obj,Elf64_Shdr * shdr,Elf_Data * data)9767 static int bpf_object__collect_st_ops_relos(struct bpf_object *obj,
9768 Elf64_Shdr *shdr, Elf_Data *data)
9769 {
9770 const struct btf_type *type;
9771 const struct btf_member *member;
9772 struct bpf_struct_ops *st_ops;
9773 struct bpf_program *prog;
9774 unsigned int shdr_idx;
9775 const struct btf *btf;
9776 struct bpf_map *map;
9777 unsigned int moff, insn_idx;
9778 const char *name;
9779 __u32 member_idx;
9780 Elf64_Sym *sym;
9781 Elf64_Rel *rel;
9782 int i, nrels;
9783
9784 btf = obj->btf;
9785 nrels = shdr->sh_size / shdr->sh_entsize;
9786 for (i = 0; i < nrels; i++) {
9787 rel = elf_rel_by_idx(data, i);
9788 if (!rel) {
9789 pr_warn("struct_ops reloc: failed to get %d reloc\n", i);
9790 return -LIBBPF_ERRNO__FORMAT;
9791 }
9792
9793 sym = elf_sym_by_idx(obj, ELF64_R_SYM(rel->r_info));
9794 if (!sym) {
9795 pr_warn("struct_ops reloc: symbol %zx not found\n",
9796 (size_t)ELF64_R_SYM(rel->r_info));
9797 return -LIBBPF_ERRNO__FORMAT;
9798 }
9799
9800 name = elf_sym_str(obj, sym->st_name) ?: "<?>";
9801 map = find_struct_ops_map_by_offset(obj, shdr->sh_info, rel->r_offset);
9802 if (!map) {
9803 pr_warn("struct_ops reloc: cannot find map at rel->r_offset %zu\n",
9804 (size_t)rel->r_offset);
9805 return -EINVAL;
9806 }
9807
9808 moff = rel->r_offset - map->sec_offset;
9809 shdr_idx = sym->st_shndx;
9810 st_ops = map->st_ops;
9811 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",
9812 map->name,
9813 (long long)(rel->r_info >> 32),
9814 (long long)sym->st_value,
9815 shdr_idx, (size_t)rel->r_offset,
9816 map->sec_offset, sym->st_name, name);
9817
9818 if (shdr_idx >= SHN_LORESERVE) {
9819 pr_warn("struct_ops reloc %s: rel->r_offset %zu shdr_idx %u unsupported non-static function\n",
9820 map->name, (size_t)rel->r_offset, shdr_idx);
9821 return -LIBBPF_ERRNO__RELOC;
9822 }
9823 if (sym->st_value % BPF_INSN_SZ) {
9824 pr_warn("struct_ops reloc %s: invalid target program offset %llu\n",
9825 map->name, (unsigned long long)sym->st_value);
9826 return -LIBBPF_ERRNO__FORMAT;
9827 }
9828 insn_idx = sym->st_value / BPF_INSN_SZ;
9829
9830 type = btf__type_by_id(btf, st_ops->type_id);
9831 member = find_member_by_offset(type, moff * 8);
9832 if (!member) {
9833 pr_warn("struct_ops reloc %s: cannot find member at moff %u\n",
9834 map->name, moff);
9835 return -EINVAL;
9836 }
9837 member_idx = member - btf_members(type);
9838 name = btf__name_by_offset(btf, member->name_off);
9839
9840 if (!resolve_func_ptr(btf, member->type, NULL)) {
9841 pr_warn("struct_ops reloc %s: cannot relocate non func ptr %s\n",
9842 map->name, name);
9843 return -EINVAL;
9844 }
9845
9846 prog = find_prog_by_sec_insn(obj, shdr_idx, insn_idx);
9847 if (!prog) {
9848 pr_warn("struct_ops reloc %s: cannot find prog at shdr_idx %u to relocate func ptr %s\n",
9849 map->name, shdr_idx, name);
9850 return -EINVAL;
9851 }
9852
9853 /* prevent the use of BPF prog with invalid type */
9854 if (prog->type != BPF_PROG_TYPE_STRUCT_OPS) {
9855 pr_warn("struct_ops reloc %s: prog %s is not struct_ops BPF program\n",
9856 map->name, prog->name);
9857 return -EINVAL;
9858 }
9859
9860 st_ops->progs[member_idx] = prog;
9861
9862 /* st_ops->data will be exposed to users, being returned by
9863 * bpf_map__initial_value() as a pointer to the shadow
9864 * type. All function pointers in the original struct type
9865 * should be converted to a pointer to struct bpf_program
9866 * in the shadow type.
9867 */
9868 *((struct bpf_program **)(st_ops->data + moff)) = prog;
9869 }
9870
9871 return 0;
9872 }
9873
9874 #define BTF_TRACE_PREFIX "btf_trace_"
9875 #define BTF_LSM_PREFIX "bpf_lsm_"
9876 #define BTF_ITER_PREFIX "bpf_iter_"
9877 #define BTF_MAX_NAME_SIZE 128
9878
btf_get_kernel_prefix_kind(enum bpf_attach_type attach_type,const char ** prefix,int * kind)9879 void btf_get_kernel_prefix_kind(enum bpf_attach_type attach_type,
9880 const char **prefix, int *kind)
9881 {
9882 switch (attach_type) {
9883 case BPF_TRACE_RAW_TP:
9884 *prefix = BTF_TRACE_PREFIX;
9885 *kind = BTF_KIND_TYPEDEF;
9886 break;
9887 case BPF_LSM_MAC:
9888 case BPF_LSM_CGROUP:
9889 *prefix = BTF_LSM_PREFIX;
9890 *kind = BTF_KIND_FUNC;
9891 break;
9892 case BPF_TRACE_ITER:
9893 *prefix = BTF_ITER_PREFIX;
9894 *kind = BTF_KIND_FUNC;
9895 break;
9896 default:
9897 *prefix = "";
9898 *kind = BTF_KIND_FUNC;
9899 }
9900 }
9901
find_btf_by_prefix_kind(const struct btf * btf,const char * prefix,const char * name,__u32 kind)9902 static int find_btf_by_prefix_kind(const struct btf *btf, const char *prefix,
9903 const char *name, __u32 kind)
9904 {
9905 char btf_type_name[BTF_MAX_NAME_SIZE];
9906 int ret;
9907
9908 ret = snprintf(btf_type_name, sizeof(btf_type_name),
9909 "%s%s", prefix, name);
9910 /* snprintf returns the number of characters written excluding the
9911 * terminating null. So, if >= BTF_MAX_NAME_SIZE are written, it
9912 * indicates truncation.
9913 */
9914 if (ret < 0 || ret >= sizeof(btf_type_name))
9915 return -ENAMETOOLONG;
9916 return btf__find_by_name_kind(btf, btf_type_name, kind);
9917 }
9918
find_attach_btf_id(struct btf * btf,const char * name,enum bpf_attach_type attach_type)9919 static inline int find_attach_btf_id(struct btf *btf, const char *name,
9920 enum bpf_attach_type attach_type)
9921 {
9922 const char *prefix;
9923 int kind;
9924
9925 btf_get_kernel_prefix_kind(attach_type, &prefix, &kind);
9926 return find_btf_by_prefix_kind(btf, prefix, name, kind);
9927 }
9928
libbpf_find_vmlinux_btf_id(const char * name,enum bpf_attach_type attach_type)9929 int libbpf_find_vmlinux_btf_id(const char *name,
9930 enum bpf_attach_type attach_type)
9931 {
9932 struct btf *btf;
9933 int err;
9934
9935 btf = btf__load_vmlinux_btf();
9936 err = libbpf_get_error(btf);
9937 if (err) {
9938 pr_warn("vmlinux BTF is not found\n");
9939 return libbpf_err(err);
9940 }
9941
9942 err = find_attach_btf_id(btf, name, attach_type);
9943 if (err <= 0)
9944 pr_warn("%s is not found in vmlinux BTF\n", name);
9945
9946 btf__free(btf);
9947 return libbpf_err(err);
9948 }
9949
libbpf_find_prog_btf_id(const char * name,__u32 attach_prog_fd)9950 static int libbpf_find_prog_btf_id(const char *name, __u32 attach_prog_fd)
9951 {
9952 struct bpf_prog_info info;
9953 __u32 info_len = sizeof(info);
9954 struct btf *btf;
9955 int err;
9956
9957 memset(&info, 0, info_len);
9958 err = bpf_prog_get_info_by_fd(attach_prog_fd, &info, &info_len);
9959 if (err) {
9960 pr_warn("failed bpf_prog_get_info_by_fd for FD %d: %s\n",
9961 attach_prog_fd, errstr(err));
9962 return err;
9963 }
9964
9965 err = -EINVAL;
9966 if (!info.btf_id) {
9967 pr_warn("The target program doesn't have BTF\n");
9968 goto out;
9969 }
9970 btf = btf__load_from_kernel_by_id(info.btf_id);
9971 err = libbpf_get_error(btf);
9972 if (err) {
9973 pr_warn("Failed to get BTF %d of the program: %s\n", info.btf_id, errstr(err));
9974 goto out;
9975 }
9976 err = btf__find_by_name_kind(btf, name, BTF_KIND_FUNC);
9977 btf__free(btf);
9978 if (err <= 0) {
9979 pr_warn("%s is not found in prog's BTF\n", name);
9980 goto out;
9981 }
9982 out:
9983 return err;
9984 }
9985
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)9986 static int find_kernel_btf_id(struct bpf_object *obj, const char *attach_name,
9987 enum bpf_attach_type attach_type,
9988 int *btf_obj_fd, int *btf_type_id)
9989 {
9990 int ret, i, mod_len;
9991 const char *fn_name, *mod_name = NULL;
9992
9993 fn_name = strchr(attach_name, ':');
9994 if (fn_name) {
9995 mod_name = attach_name;
9996 mod_len = fn_name - mod_name;
9997 fn_name++;
9998 }
9999
10000 if (!mod_name || strncmp(mod_name, "vmlinux", mod_len) == 0) {
10001 ret = find_attach_btf_id(obj->btf_vmlinux,
10002 mod_name ? fn_name : attach_name,
10003 attach_type);
10004 if (ret > 0) {
10005 *btf_obj_fd = 0; /* vmlinux BTF */
10006 *btf_type_id = ret;
10007 return 0;
10008 }
10009 if (ret != -ENOENT)
10010 return ret;
10011 }
10012
10013 ret = load_module_btfs(obj);
10014 if (ret)
10015 return ret;
10016
10017 for (i = 0; i < obj->btf_module_cnt; i++) {
10018 const struct module_btf *mod = &obj->btf_modules[i];
10019
10020 if (mod_name && strncmp(mod->name, mod_name, mod_len) != 0)
10021 continue;
10022
10023 ret = find_attach_btf_id(mod->btf,
10024 mod_name ? fn_name : attach_name,
10025 attach_type);
10026 if (ret > 0) {
10027 *btf_obj_fd = mod->fd;
10028 *btf_type_id = ret;
10029 return 0;
10030 }
10031 if (ret == -ENOENT)
10032 continue;
10033
10034 return ret;
10035 }
10036
10037 return -ESRCH;
10038 }
10039
libbpf_find_attach_btf_id(struct bpf_program * prog,const char * attach_name,int * btf_obj_fd,int * btf_type_id)10040 static int libbpf_find_attach_btf_id(struct bpf_program *prog, const char *attach_name,
10041 int *btf_obj_fd, int *btf_type_id)
10042 {
10043 enum bpf_attach_type attach_type = prog->expected_attach_type;
10044 __u32 attach_prog_fd = prog->attach_prog_fd;
10045 int err = 0;
10046
10047 /* BPF program's BTF ID */
10048 if (prog->type == BPF_PROG_TYPE_EXT || attach_prog_fd) {
10049 if (!attach_prog_fd) {
10050 pr_warn("prog '%s': attach program FD is not set\n", prog->name);
10051 return -EINVAL;
10052 }
10053 err = libbpf_find_prog_btf_id(attach_name, attach_prog_fd);
10054 if (err < 0) {
10055 pr_warn("prog '%s': failed to find BPF program (FD %d) BTF ID for '%s': %s\n",
10056 prog->name, attach_prog_fd, attach_name, errstr(err));
10057 return err;
10058 }
10059 *btf_obj_fd = 0;
10060 *btf_type_id = err;
10061 return 0;
10062 }
10063
10064 /* kernel/module BTF ID */
10065 if (prog->obj->gen_loader) {
10066 bpf_gen__record_attach_target(prog->obj->gen_loader, attach_name, attach_type);
10067 *btf_obj_fd = 0;
10068 *btf_type_id = 1;
10069 } else {
10070 err = find_kernel_btf_id(prog->obj, attach_name,
10071 attach_type, btf_obj_fd,
10072 btf_type_id);
10073 }
10074 if (err) {
10075 pr_warn("prog '%s': failed to find kernel BTF type ID of '%s': %s\n",
10076 prog->name, attach_name, errstr(err));
10077 return err;
10078 }
10079 return 0;
10080 }
10081
libbpf_attach_type_by_name(const char * name,enum bpf_attach_type * attach_type)10082 int libbpf_attach_type_by_name(const char *name,
10083 enum bpf_attach_type *attach_type)
10084 {
10085 char *type_names;
10086 const struct bpf_sec_def *sec_def;
10087
10088 if (!name)
10089 return libbpf_err(-EINVAL);
10090
10091 sec_def = find_sec_def(name);
10092 if (!sec_def) {
10093 pr_debug("failed to guess attach type based on ELF section name '%s'\n", name);
10094 type_names = libbpf_get_type_names(true);
10095 if (type_names != NULL) {
10096 pr_debug("attachable section(type) names are:%s\n", type_names);
10097 free(type_names);
10098 }
10099
10100 return libbpf_err(-EINVAL);
10101 }
10102
10103 if (sec_def->prog_prepare_load_fn != libbpf_prepare_prog_load)
10104 return libbpf_err(-EINVAL);
10105 if (!(sec_def->cookie & SEC_ATTACHABLE))
10106 return libbpf_err(-EINVAL);
10107
10108 *attach_type = sec_def->expected_attach_type;
10109 return 0;
10110 }
10111
bpf_map__fd(const struct bpf_map * map)10112 int bpf_map__fd(const struct bpf_map *map)
10113 {
10114 if (!map)
10115 return libbpf_err(-EINVAL);
10116 if (!map_is_created(map))
10117 return -1;
10118 return map->fd;
10119 }
10120
map_uses_real_name(const struct bpf_map * map)10121 static bool map_uses_real_name(const struct bpf_map *map)
10122 {
10123 /* Since libbpf started to support custom .data.* and .rodata.* maps,
10124 * their user-visible name differs from kernel-visible name. Users see
10125 * such map's corresponding ELF section name as a map name.
10126 * This check distinguishes .data/.rodata from .data.* and .rodata.*
10127 * maps to know which name has to be returned to the user.
10128 */
10129 if (map->libbpf_type == LIBBPF_MAP_DATA && strcmp(map->real_name, DATA_SEC) != 0)
10130 return true;
10131 if (map->libbpf_type == LIBBPF_MAP_RODATA && strcmp(map->real_name, RODATA_SEC) != 0)
10132 return true;
10133 return false;
10134 }
10135
bpf_map__name(const struct bpf_map * map)10136 const char *bpf_map__name(const struct bpf_map *map)
10137 {
10138 if (!map)
10139 return NULL;
10140
10141 if (map_uses_real_name(map))
10142 return map->real_name;
10143
10144 return map->name;
10145 }
10146
bpf_map__type(const struct bpf_map * map)10147 enum bpf_map_type bpf_map__type(const struct bpf_map *map)
10148 {
10149 return map->def.type;
10150 }
10151
bpf_map__set_type(struct bpf_map * map,enum bpf_map_type type)10152 int bpf_map__set_type(struct bpf_map *map, enum bpf_map_type type)
10153 {
10154 if (map_is_created(map))
10155 return libbpf_err(-EBUSY);
10156 map->def.type = type;
10157 return 0;
10158 }
10159
bpf_map__map_flags(const struct bpf_map * map)10160 __u32 bpf_map__map_flags(const struct bpf_map *map)
10161 {
10162 return map->def.map_flags;
10163 }
10164
bpf_map__set_map_flags(struct bpf_map * map,__u32 flags)10165 int bpf_map__set_map_flags(struct bpf_map *map, __u32 flags)
10166 {
10167 if (map_is_created(map))
10168 return libbpf_err(-EBUSY);
10169 map->def.map_flags = flags;
10170 return 0;
10171 }
10172
bpf_map__map_extra(const struct bpf_map * map)10173 __u64 bpf_map__map_extra(const struct bpf_map *map)
10174 {
10175 return map->map_extra;
10176 }
10177
bpf_map__set_map_extra(struct bpf_map * map,__u64 map_extra)10178 int bpf_map__set_map_extra(struct bpf_map *map, __u64 map_extra)
10179 {
10180 if (map_is_created(map))
10181 return libbpf_err(-EBUSY);
10182 map->map_extra = map_extra;
10183 return 0;
10184 }
10185
bpf_map__numa_node(const struct bpf_map * map)10186 __u32 bpf_map__numa_node(const struct bpf_map *map)
10187 {
10188 return map->numa_node;
10189 }
10190
bpf_map__set_numa_node(struct bpf_map * map,__u32 numa_node)10191 int bpf_map__set_numa_node(struct bpf_map *map, __u32 numa_node)
10192 {
10193 if (map_is_created(map))
10194 return libbpf_err(-EBUSY);
10195 map->numa_node = numa_node;
10196 return 0;
10197 }
10198
bpf_map__key_size(const struct bpf_map * map)10199 __u32 bpf_map__key_size(const struct bpf_map *map)
10200 {
10201 return map->def.key_size;
10202 }
10203
bpf_map__set_key_size(struct bpf_map * map,__u32 size)10204 int bpf_map__set_key_size(struct bpf_map *map, __u32 size)
10205 {
10206 if (map_is_created(map))
10207 return libbpf_err(-EBUSY);
10208 map->def.key_size = size;
10209 return 0;
10210 }
10211
bpf_map__value_size(const struct bpf_map * map)10212 __u32 bpf_map__value_size(const struct bpf_map *map)
10213 {
10214 return map->def.value_size;
10215 }
10216
map_btf_datasec_resize(struct bpf_map * map,__u32 size)10217 static int map_btf_datasec_resize(struct bpf_map *map, __u32 size)
10218 {
10219 struct btf *btf;
10220 struct btf_type *datasec_type, *var_type;
10221 struct btf_var_secinfo *var;
10222 const struct btf_type *array_type;
10223 const struct btf_array *array;
10224 int vlen, element_sz, new_array_id;
10225 __u32 nr_elements;
10226
10227 /* check btf existence */
10228 btf = bpf_object__btf(map->obj);
10229 if (!btf)
10230 return -ENOENT;
10231
10232 /* verify map is datasec */
10233 datasec_type = btf_type_by_id(btf, bpf_map__btf_value_type_id(map));
10234 if (!btf_is_datasec(datasec_type)) {
10235 pr_warn("map '%s': cannot be resized, map value type is not a datasec\n",
10236 bpf_map__name(map));
10237 return -EINVAL;
10238 }
10239
10240 /* verify datasec has at least one var */
10241 vlen = btf_vlen(datasec_type);
10242 if (vlen == 0) {
10243 pr_warn("map '%s': cannot be resized, map value datasec is empty\n",
10244 bpf_map__name(map));
10245 return -EINVAL;
10246 }
10247
10248 /* verify last var in the datasec is an array */
10249 var = &btf_var_secinfos(datasec_type)[vlen - 1];
10250 var_type = btf_type_by_id(btf, var->type);
10251 array_type = skip_mods_and_typedefs(btf, var_type->type, NULL);
10252 if (!btf_is_array(array_type)) {
10253 pr_warn("map '%s': cannot be resized, last var must be an array\n",
10254 bpf_map__name(map));
10255 return -EINVAL;
10256 }
10257
10258 /* verify request size aligns with array */
10259 array = btf_array(array_type);
10260 element_sz = btf__resolve_size(btf, array->type);
10261 if (element_sz <= 0 || (size - var->offset) % element_sz != 0) {
10262 pr_warn("map '%s': cannot be resized, element size (%d) doesn't align with new total size (%u)\n",
10263 bpf_map__name(map), element_sz, size);
10264 return -EINVAL;
10265 }
10266
10267 /* create a new array based on the existing array, but with new length */
10268 nr_elements = (size - var->offset) / element_sz;
10269 new_array_id = btf__add_array(btf, array->index_type, array->type, nr_elements);
10270 if (new_array_id < 0)
10271 return new_array_id;
10272
10273 /* adding a new btf type invalidates existing pointers to btf objects,
10274 * so refresh pointers before proceeding
10275 */
10276 datasec_type = btf_type_by_id(btf, map->btf_value_type_id);
10277 var = &btf_var_secinfos(datasec_type)[vlen - 1];
10278 var_type = btf_type_by_id(btf, var->type);
10279
10280 /* finally update btf info */
10281 datasec_type->size = size;
10282 var->size = size - var->offset;
10283 var_type->type = new_array_id;
10284
10285 return 0;
10286 }
10287
bpf_map__set_value_size(struct bpf_map * map,__u32 size)10288 int bpf_map__set_value_size(struct bpf_map *map, __u32 size)
10289 {
10290 if (map->obj->loaded || map->reused)
10291 return libbpf_err(-EBUSY);
10292
10293 if (map->mmaped) {
10294 size_t mmap_old_sz, mmap_new_sz;
10295 int err;
10296
10297 if (map->def.type != BPF_MAP_TYPE_ARRAY)
10298 return -EOPNOTSUPP;
10299
10300 mmap_old_sz = bpf_map_mmap_sz(map);
10301 mmap_new_sz = array_map_mmap_sz(size, map->def.max_entries);
10302 err = bpf_map_mmap_resize(map, mmap_old_sz, mmap_new_sz);
10303 if (err) {
10304 pr_warn("map '%s': failed to resize memory-mapped region: %s\n",
10305 bpf_map__name(map), errstr(err));
10306 return err;
10307 }
10308 err = map_btf_datasec_resize(map, size);
10309 if (err && err != -ENOENT) {
10310 pr_warn("map '%s': failed to adjust resized BTF, clearing BTF key/value info: %s\n",
10311 bpf_map__name(map), errstr(err));
10312 map->btf_value_type_id = 0;
10313 map->btf_key_type_id = 0;
10314 }
10315 }
10316
10317 map->def.value_size = size;
10318 return 0;
10319 }
10320
bpf_map__btf_key_type_id(const struct bpf_map * map)10321 __u32 bpf_map__btf_key_type_id(const struct bpf_map *map)
10322 {
10323 return map ? map->btf_key_type_id : 0;
10324 }
10325
bpf_map__btf_value_type_id(const struct bpf_map * map)10326 __u32 bpf_map__btf_value_type_id(const struct bpf_map *map)
10327 {
10328 return map ? map->btf_value_type_id : 0;
10329 }
10330
bpf_map__set_initial_value(struct bpf_map * map,const void * data,size_t size)10331 int bpf_map__set_initial_value(struct bpf_map *map,
10332 const void *data, size_t size)
10333 {
10334 size_t actual_sz;
10335
10336 if (map->obj->loaded || map->reused)
10337 return libbpf_err(-EBUSY);
10338
10339 if (!map->mmaped || map->libbpf_type == LIBBPF_MAP_KCONFIG)
10340 return libbpf_err(-EINVAL);
10341
10342 if (map->def.type == BPF_MAP_TYPE_ARENA)
10343 actual_sz = map->obj->arena_data_sz;
10344 else
10345 actual_sz = map->def.value_size;
10346 if (size != actual_sz)
10347 return libbpf_err(-EINVAL);
10348
10349 memcpy(map->mmaped, data, size);
10350 return 0;
10351 }
10352
bpf_map__initial_value(const struct bpf_map * map,size_t * psize)10353 void *bpf_map__initial_value(const struct bpf_map *map, size_t *psize)
10354 {
10355 if (bpf_map__is_struct_ops(map)) {
10356 if (psize)
10357 *psize = map->def.value_size;
10358 return map->st_ops->data;
10359 }
10360
10361 if (!map->mmaped)
10362 return NULL;
10363
10364 if (map->def.type == BPF_MAP_TYPE_ARENA)
10365 *psize = map->obj->arena_data_sz;
10366 else
10367 *psize = map->def.value_size;
10368
10369 return map->mmaped;
10370 }
10371
bpf_map__is_internal(const struct bpf_map * map)10372 bool bpf_map__is_internal(const struct bpf_map *map)
10373 {
10374 return map->libbpf_type != LIBBPF_MAP_UNSPEC;
10375 }
10376
bpf_map__ifindex(const struct bpf_map * map)10377 __u32 bpf_map__ifindex(const struct bpf_map *map)
10378 {
10379 return map->map_ifindex;
10380 }
10381
bpf_map__set_ifindex(struct bpf_map * map,__u32 ifindex)10382 int bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex)
10383 {
10384 if (map_is_created(map))
10385 return libbpf_err(-EBUSY);
10386 map->map_ifindex = ifindex;
10387 return 0;
10388 }
10389
bpf_map__set_inner_map_fd(struct bpf_map * map,int fd)10390 int bpf_map__set_inner_map_fd(struct bpf_map *map, int fd)
10391 {
10392 if (!bpf_map_type__is_map_in_map(map->def.type)) {
10393 pr_warn("error: unsupported map type\n");
10394 return libbpf_err(-EINVAL);
10395 }
10396 if (map->inner_map_fd != -1) {
10397 pr_warn("error: inner_map_fd already specified\n");
10398 return libbpf_err(-EINVAL);
10399 }
10400 if (map->inner_map) {
10401 bpf_map__destroy(map->inner_map);
10402 zfree(&map->inner_map);
10403 }
10404 map->inner_map_fd = fd;
10405 return 0;
10406 }
10407
10408 static struct bpf_map *
__bpf_map__iter(const struct bpf_map * m,const struct bpf_object * obj,int i)10409 __bpf_map__iter(const struct bpf_map *m, const struct bpf_object *obj, int i)
10410 {
10411 ssize_t idx;
10412 struct bpf_map *s, *e;
10413
10414 if (!obj || !obj->maps)
10415 return errno = EINVAL, NULL;
10416
10417 s = obj->maps;
10418 e = obj->maps + obj->nr_maps;
10419
10420 if ((m < s) || (m >= e)) {
10421 pr_warn("error in %s: map handler doesn't belong to object\n",
10422 __func__);
10423 return errno = EINVAL, NULL;
10424 }
10425
10426 idx = (m - obj->maps) + i;
10427 if (idx >= obj->nr_maps || idx < 0)
10428 return NULL;
10429 return &obj->maps[idx];
10430 }
10431
10432 struct bpf_map *
bpf_object__next_map(const struct bpf_object * obj,const struct bpf_map * prev)10433 bpf_object__next_map(const struct bpf_object *obj, const struct bpf_map *prev)
10434 {
10435 if (prev == NULL && obj != NULL)
10436 return obj->maps;
10437
10438 return __bpf_map__iter(prev, obj, 1);
10439 }
10440
10441 struct bpf_map *
bpf_object__prev_map(const struct bpf_object * obj,const struct bpf_map * next)10442 bpf_object__prev_map(const struct bpf_object *obj, const struct bpf_map *next)
10443 {
10444 if (next == NULL && obj != NULL) {
10445 if (!obj->nr_maps)
10446 return NULL;
10447 return obj->maps + obj->nr_maps - 1;
10448 }
10449
10450 return __bpf_map__iter(next, obj, -1);
10451 }
10452
10453 struct bpf_map *
bpf_object__find_map_by_name(const struct bpf_object * obj,const char * name)10454 bpf_object__find_map_by_name(const struct bpf_object *obj, const char *name)
10455 {
10456 struct bpf_map *pos;
10457
10458 bpf_object__for_each_map(pos, obj) {
10459 /* if it's a special internal map name (which always starts
10460 * with dot) then check if that special name matches the
10461 * real map name (ELF section name)
10462 */
10463 if (name[0] == '.') {
10464 if (pos->real_name && strcmp(pos->real_name, name) == 0)
10465 return pos;
10466 continue;
10467 }
10468 /* otherwise map name has to be an exact match */
10469 if (map_uses_real_name(pos)) {
10470 if (strcmp(pos->real_name, name) == 0)
10471 return pos;
10472 continue;
10473 }
10474 if (strcmp(pos->name, name) == 0)
10475 return pos;
10476 }
10477 return errno = ENOENT, NULL;
10478 }
10479
10480 int
bpf_object__find_map_fd_by_name(const struct bpf_object * obj,const char * name)10481 bpf_object__find_map_fd_by_name(const struct bpf_object *obj, const char *name)
10482 {
10483 return bpf_map__fd(bpf_object__find_map_by_name(obj, name));
10484 }
10485
validate_map_op(const struct bpf_map * map,size_t key_sz,size_t value_sz,bool check_value_sz)10486 static int validate_map_op(const struct bpf_map *map, size_t key_sz,
10487 size_t value_sz, bool check_value_sz)
10488 {
10489 if (!map_is_created(map)) /* map is not yet created */
10490 return -ENOENT;
10491
10492 if (map->def.key_size != key_sz) {
10493 pr_warn("map '%s': unexpected key size %zu provided, expected %u\n",
10494 map->name, key_sz, map->def.key_size);
10495 return -EINVAL;
10496 }
10497
10498 if (map->fd < 0) {
10499 pr_warn("map '%s': can't use BPF map without FD (was it created?)\n", map->name);
10500 return -EINVAL;
10501 }
10502
10503 if (!check_value_sz)
10504 return 0;
10505
10506 switch (map->def.type) {
10507 case BPF_MAP_TYPE_PERCPU_ARRAY:
10508 case BPF_MAP_TYPE_PERCPU_HASH:
10509 case BPF_MAP_TYPE_LRU_PERCPU_HASH:
10510 case BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE: {
10511 int num_cpu = libbpf_num_possible_cpus();
10512 size_t elem_sz = roundup(map->def.value_size, 8);
10513
10514 if (value_sz != num_cpu * elem_sz) {
10515 pr_warn("map '%s': unexpected value size %zu provided for per-CPU map, expected %d * %zu = %zd\n",
10516 map->name, value_sz, num_cpu, elem_sz, num_cpu * elem_sz);
10517 return -EINVAL;
10518 }
10519 break;
10520 }
10521 default:
10522 if (map->def.value_size != value_sz) {
10523 pr_warn("map '%s': unexpected value size %zu provided, expected %u\n",
10524 map->name, value_sz, map->def.value_size);
10525 return -EINVAL;
10526 }
10527 break;
10528 }
10529 return 0;
10530 }
10531
bpf_map__lookup_elem(const struct bpf_map * map,const void * key,size_t key_sz,void * value,size_t value_sz,__u64 flags)10532 int bpf_map__lookup_elem(const struct bpf_map *map,
10533 const void *key, size_t key_sz,
10534 void *value, size_t value_sz, __u64 flags)
10535 {
10536 int err;
10537
10538 err = validate_map_op(map, key_sz, value_sz, true);
10539 if (err)
10540 return libbpf_err(err);
10541
10542 return bpf_map_lookup_elem_flags(map->fd, key, value, flags);
10543 }
10544
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)10545 int bpf_map__update_elem(const struct bpf_map *map,
10546 const void *key, size_t key_sz,
10547 const void *value, size_t value_sz, __u64 flags)
10548 {
10549 int err;
10550
10551 err = validate_map_op(map, key_sz, value_sz, true);
10552 if (err)
10553 return libbpf_err(err);
10554
10555 return bpf_map_update_elem(map->fd, key, value, flags);
10556 }
10557
bpf_map__delete_elem(const struct bpf_map * map,const void * key,size_t key_sz,__u64 flags)10558 int bpf_map__delete_elem(const struct bpf_map *map,
10559 const void *key, size_t key_sz, __u64 flags)
10560 {
10561 int err;
10562
10563 err = validate_map_op(map, key_sz, 0, false /* check_value_sz */);
10564 if (err)
10565 return libbpf_err(err);
10566
10567 return bpf_map_delete_elem_flags(map->fd, key, flags);
10568 }
10569
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)10570 int bpf_map__lookup_and_delete_elem(const struct bpf_map *map,
10571 const void *key, size_t key_sz,
10572 void *value, size_t value_sz, __u64 flags)
10573 {
10574 int err;
10575
10576 err = validate_map_op(map, key_sz, value_sz, true);
10577 if (err)
10578 return libbpf_err(err);
10579
10580 return bpf_map_lookup_and_delete_elem_flags(map->fd, key, value, flags);
10581 }
10582
bpf_map__get_next_key(const struct bpf_map * map,const void * cur_key,void * next_key,size_t key_sz)10583 int bpf_map__get_next_key(const struct bpf_map *map,
10584 const void *cur_key, void *next_key, size_t key_sz)
10585 {
10586 int err;
10587
10588 err = validate_map_op(map, key_sz, 0, false /* check_value_sz */);
10589 if (err)
10590 return libbpf_err(err);
10591
10592 return bpf_map_get_next_key(map->fd, cur_key, next_key);
10593 }
10594
libbpf_get_error(const void * ptr)10595 long libbpf_get_error(const void *ptr)
10596 {
10597 if (!IS_ERR_OR_NULL(ptr))
10598 return 0;
10599
10600 if (IS_ERR(ptr))
10601 errno = -PTR_ERR(ptr);
10602
10603 /* If ptr == NULL, then errno should be already set by the failing
10604 * API, because libbpf never returns NULL on success and it now always
10605 * sets errno on error. So no extra errno handling for ptr == NULL
10606 * case.
10607 */
10608 return -errno;
10609 }
10610
10611 /* Replace link's underlying BPF program with the new one */
bpf_link__update_program(struct bpf_link * link,struct bpf_program * prog)10612 int bpf_link__update_program(struct bpf_link *link, struct bpf_program *prog)
10613 {
10614 int ret;
10615 int prog_fd = bpf_program__fd(prog);
10616
10617 if (prog_fd < 0) {
10618 pr_warn("prog '%s': can't use BPF program without FD (was it loaded?)\n",
10619 prog->name);
10620 return libbpf_err(-EINVAL);
10621 }
10622
10623 ret = bpf_link_update(bpf_link__fd(link), prog_fd, NULL);
10624 return libbpf_err_errno(ret);
10625 }
10626
10627 /* Release "ownership" of underlying BPF resource (typically, BPF program
10628 * attached to some BPF hook, e.g., tracepoint, kprobe, etc). Disconnected
10629 * link, when destructed through bpf_link__destroy() call won't attempt to
10630 * detach/unregisted that BPF resource. This is useful in situations where,
10631 * say, attached BPF program has to outlive userspace program that attached it
10632 * in the system. Depending on type of BPF program, though, there might be
10633 * additional steps (like pinning BPF program in BPF FS) necessary to ensure
10634 * exit of userspace program doesn't trigger automatic detachment and clean up
10635 * inside the kernel.
10636 */
bpf_link__disconnect(struct bpf_link * link)10637 void bpf_link__disconnect(struct bpf_link *link)
10638 {
10639 link->disconnected = true;
10640 }
10641
bpf_link__destroy(struct bpf_link * link)10642 int bpf_link__destroy(struct bpf_link *link)
10643 {
10644 int err = 0;
10645
10646 if (IS_ERR_OR_NULL(link))
10647 return 0;
10648
10649 if (!link->disconnected && link->detach)
10650 err = link->detach(link);
10651 if (link->pin_path)
10652 free(link->pin_path);
10653 if (link->dealloc)
10654 link->dealloc(link);
10655 else
10656 free(link);
10657
10658 return libbpf_err(err);
10659 }
10660
bpf_link__fd(const struct bpf_link * link)10661 int bpf_link__fd(const struct bpf_link *link)
10662 {
10663 return link->fd;
10664 }
10665
bpf_link__pin_path(const struct bpf_link * link)10666 const char *bpf_link__pin_path(const struct bpf_link *link)
10667 {
10668 return link->pin_path;
10669 }
10670
bpf_link__detach_fd(struct bpf_link * link)10671 static int bpf_link__detach_fd(struct bpf_link *link)
10672 {
10673 return libbpf_err_errno(close(link->fd));
10674 }
10675
bpf_link__open(const char * path)10676 struct bpf_link *bpf_link__open(const char *path)
10677 {
10678 struct bpf_link *link;
10679 int fd;
10680
10681 fd = bpf_obj_get(path);
10682 if (fd < 0) {
10683 fd = -errno;
10684 pr_warn("failed to open link at %s: %d\n", path, fd);
10685 return libbpf_err_ptr(fd);
10686 }
10687
10688 link = calloc(1, sizeof(*link));
10689 if (!link) {
10690 close(fd);
10691 return libbpf_err_ptr(-ENOMEM);
10692 }
10693 link->detach = &bpf_link__detach_fd;
10694 link->fd = fd;
10695
10696 link->pin_path = strdup(path);
10697 if (!link->pin_path) {
10698 bpf_link__destroy(link);
10699 return libbpf_err_ptr(-ENOMEM);
10700 }
10701
10702 return link;
10703 }
10704
bpf_link__detach(struct bpf_link * link)10705 int bpf_link__detach(struct bpf_link *link)
10706 {
10707 return bpf_link_detach(link->fd) ? -errno : 0;
10708 }
10709
bpf_link__pin(struct bpf_link * link,const char * path)10710 int bpf_link__pin(struct bpf_link *link, const char *path)
10711 {
10712 int err;
10713
10714 if (link->pin_path)
10715 return libbpf_err(-EBUSY);
10716 err = make_parent_dir(path);
10717 if (err)
10718 return libbpf_err(err);
10719 err = check_path(path);
10720 if (err)
10721 return libbpf_err(err);
10722
10723 link->pin_path = strdup(path);
10724 if (!link->pin_path)
10725 return libbpf_err(-ENOMEM);
10726
10727 if (bpf_obj_pin(link->fd, link->pin_path)) {
10728 err = -errno;
10729 zfree(&link->pin_path);
10730 return libbpf_err(err);
10731 }
10732
10733 pr_debug("link fd=%d: pinned at %s\n", link->fd, link->pin_path);
10734 return 0;
10735 }
10736
bpf_link__unpin(struct bpf_link * link)10737 int bpf_link__unpin(struct bpf_link *link)
10738 {
10739 int err;
10740
10741 if (!link->pin_path)
10742 return libbpf_err(-EINVAL);
10743
10744 err = unlink(link->pin_path);
10745 if (err != 0)
10746 return -errno;
10747
10748 pr_debug("link fd=%d: unpinned from %s\n", link->fd, link->pin_path);
10749 zfree(&link->pin_path);
10750 return 0;
10751 }
10752
10753 struct bpf_link_perf {
10754 struct bpf_link link;
10755 int perf_event_fd;
10756 /* legacy kprobe support: keep track of probe identifier and type */
10757 char *legacy_probe_name;
10758 bool legacy_is_kprobe;
10759 bool legacy_is_retprobe;
10760 };
10761
10762 static int remove_kprobe_event_legacy(const char *probe_name, bool retprobe);
10763 static int remove_uprobe_event_legacy(const char *probe_name, bool retprobe);
10764
bpf_link_perf_detach(struct bpf_link * link)10765 static int bpf_link_perf_detach(struct bpf_link *link)
10766 {
10767 struct bpf_link_perf *perf_link = container_of(link, struct bpf_link_perf, link);
10768 int err = 0;
10769
10770 if (ioctl(perf_link->perf_event_fd, PERF_EVENT_IOC_DISABLE, 0) < 0)
10771 err = -errno;
10772
10773 if (perf_link->perf_event_fd != link->fd)
10774 close(perf_link->perf_event_fd);
10775 close(link->fd);
10776
10777 /* legacy uprobe/kprobe needs to be removed after perf event fd closure */
10778 if (perf_link->legacy_probe_name) {
10779 if (perf_link->legacy_is_kprobe) {
10780 err = remove_kprobe_event_legacy(perf_link->legacy_probe_name,
10781 perf_link->legacy_is_retprobe);
10782 } else {
10783 err = remove_uprobe_event_legacy(perf_link->legacy_probe_name,
10784 perf_link->legacy_is_retprobe);
10785 }
10786 }
10787
10788 return err;
10789 }
10790
bpf_link_perf_dealloc(struct bpf_link * link)10791 static void bpf_link_perf_dealloc(struct bpf_link *link)
10792 {
10793 struct bpf_link_perf *perf_link = container_of(link, struct bpf_link_perf, link);
10794
10795 free(perf_link->legacy_probe_name);
10796 free(perf_link);
10797 }
10798
bpf_program__attach_perf_event_opts(const struct bpf_program * prog,int pfd,const struct bpf_perf_event_opts * opts)10799 struct bpf_link *bpf_program__attach_perf_event_opts(const struct bpf_program *prog, int pfd,
10800 const struct bpf_perf_event_opts *opts)
10801 {
10802 struct bpf_link_perf *link;
10803 int prog_fd, link_fd = -1, err;
10804 bool force_ioctl_attach;
10805
10806 if (!OPTS_VALID(opts, bpf_perf_event_opts))
10807 return libbpf_err_ptr(-EINVAL);
10808
10809 if (pfd < 0) {
10810 pr_warn("prog '%s': invalid perf event FD %d\n",
10811 prog->name, pfd);
10812 return libbpf_err_ptr(-EINVAL);
10813 }
10814 prog_fd = bpf_program__fd(prog);
10815 if (prog_fd < 0) {
10816 pr_warn("prog '%s': can't attach BPF program without FD (was it loaded?)\n",
10817 prog->name);
10818 return libbpf_err_ptr(-EINVAL);
10819 }
10820
10821 link = calloc(1, sizeof(*link));
10822 if (!link)
10823 return libbpf_err_ptr(-ENOMEM);
10824 link->link.detach = &bpf_link_perf_detach;
10825 link->link.dealloc = &bpf_link_perf_dealloc;
10826 link->perf_event_fd = pfd;
10827
10828 force_ioctl_attach = OPTS_GET(opts, force_ioctl_attach, false);
10829 if (kernel_supports(prog->obj, FEAT_PERF_LINK) && !force_ioctl_attach) {
10830 DECLARE_LIBBPF_OPTS(bpf_link_create_opts, link_opts,
10831 .perf_event.bpf_cookie = OPTS_GET(opts, bpf_cookie, 0));
10832
10833 link_fd = bpf_link_create(prog_fd, pfd, BPF_PERF_EVENT, &link_opts);
10834 if (link_fd < 0) {
10835 err = -errno;
10836 pr_warn("prog '%s': failed to create BPF link for perf_event FD %d: %s\n",
10837 prog->name, pfd, errstr(err));
10838 goto err_out;
10839 }
10840 link->link.fd = link_fd;
10841 } else {
10842 if (OPTS_GET(opts, bpf_cookie, 0)) {
10843 pr_warn("prog '%s': user context value is not supported\n", prog->name);
10844 err = -EOPNOTSUPP;
10845 goto err_out;
10846 }
10847
10848 if (ioctl(pfd, PERF_EVENT_IOC_SET_BPF, prog_fd) < 0) {
10849 err = -errno;
10850 pr_warn("prog '%s': failed to attach to perf_event FD %d: %s\n",
10851 prog->name, pfd, errstr(err));
10852 if (err == -EPROTO)
10853 pr_warn("prog '%s': try add PERF_SAMPLE_CALLCHAIN to or remove exclude_callchain_[kernel|user] from pfd %d\n",
10854 prog->name, pfd);
10855 goto err_out;
10856 }
10857 link->link.fd = pfd;
10858 }
10859 if (ioctl(pfd, PERF_EVENT_IOC_ENABLE, 0) < 0) {
10860 err = -errno;
10861 pr_warn("prog '%s': failed to enable perf_event FD %d: %s\n",
10862 prog->name, pfd, errstr(err));
10863 goto err_out;
10864 }
10865
10866 return &link->link;
10867 err_out:
10868 if (link_fd >= 0)
10869 close(link_fd);
10870 free(link);
10871 return libbpf_err_ptr(err);
10872 }
10873
bpf_program__attach_perf_event(const struct bpf_program * prog,int pfd)10874 struct bpf_link *bpf_program__attach_perf_event(const struct bpf_program *prog, int pfd)
10875 {
10876 return bpf_program__attach_perf_event_opts(prog, pfd, NULL);
10877 }
10878
10879 /*
10880 * this function is expected to parse integer in the range of [0, 2^31-1] from
10881 * given file using scanf format string fmt. If actual parsed value is
10882 * negative, the result might be indistinguishable from error
10883 */
parse_uint_from_file(const char * file,const char * fmt)10884 static int parse_uint_from_file(const char *file, const char *fmt)
10885 {
10886 int err, ret;
10887 FILE *f;
10888
10889 f = fopen(file, "re");
10890 if (!f) {
10891 err = -errno;
10892 pr_debug("failed to open '%s': %s\n", file, errstr(err));
10893 return err;
10894 }
10895 err = fscanf(f, fmt, &ret);
10896 if (err != 1) {
10897 err = err == EOF ? -EIO : -errno;
10898 pr_debug("failed to parse '%s': %s\n", file, errstr(err));
10899 fclose(f);
10900 return err;
10901 }
10902 fclose(f);
10903 return ret;
10904 }
10905
determine_kprobe_perf_type(void)10906 static int determine_kprobe_perf_type(void)
10907 {
10908 const char *file = "/sys/bus/event_source/devices/kprobe/type";
10909
10910 return parse_uint_from_file(file, "%d\n");
10911 }
10912
determine_uprobe_perf_type(void)10913 static int determine_uprobe_perf_type(void)
10914 {
10915 const char *file = "/sys/bus/event_source/devices/uprobe/type";
10916
10917 return parse_uint_from_file(file, "%d\n");
10918 }
10919
determine_kprobe_retprobe_bit(void)10920 static int determine_kprobe_retprobe_bit(void)
10921 {
10922 const char *file = "/sys/bus/event_source/devices/kprobe/format/retprobe";
10923
10924 return parse_uint_from_file(file, "config:%d\n");
10925 }
10926
determine_uprobe_retprobe_bit(void)10927 static int determine_uprobe_retprobe_bit(void)
10928 {
10929 const char *file = "/sys/bus/event_source/devices/uprobe/format/retprobe";
10930
10931 return parse_uint_from_file(file, "config:%d\n");
10932 }
10933
10934 #define PERF_UPROBE_REF_CTR_OFFSET_BITS 32
10935 #define PERF_UPROBE_REF_CTR_OFFSET_SHIFT 32
10936
perf_event_open_probe(bool uprobe,bool retprobe,const char * name,uint64_t offset,int pid,size_t ref_ctr_off)10937 static int perf_event_open_probe(bool uprobe, bool retprobe, const char *name,
10938 uint64_t offset, int pid, size_t ref_ctr_off)
10939 {
10940 const size_t attr_sz = sizeof(struct perf_event_attr);
10941 struct perf_event_attr attr;
10942 int type, pfd;
10943
10944 if ((__u64)ref_ctr_off >= (1ULL << PERF_UPROBE_REF_CTR_OFFSET_BITS))
10945 return -EINVAL;
10946
10947 memset(&attr, 0, attr_sz);
10948
10949 type = uprobe ? determine_uprobe_perf_type()
10950 : determine_kprobe_perf_type();
10951 if (type < 0) {
10952 pr_warn("failed to determine %s perf type: %s\n",
10953 uprobe ? "uprobe" : "kprobe",
10954 errstr(type));
10955 return type;
10956 }
10957 if (retprobe) {
10958 int bit = uprobe ? determine_uprobe_retprobe_bit()
10959 : determine_kprobe_retprobe_bit();
10960
10961 if (bit < 0) {
10962 pr_warn("failed to determine %s retprobe bit: %s\n",
10963 uprobe ? "uprobe" : "kprobe",
10964 errstr(bit));
10965 return bit;
10966 }
10967 attr.config |= 1 << bit;
10968 }
10969 attr.size = attr_sz;
10970 attr.type = type;
10971 attr.config |= (__u64)ref_ctr_off << PERF_UPROBE_REF_CTR_OFFSET_SHIFT;
10972 attr.config1 = ptr_to_u64(name); /* kprobe_func or uprobe_path */
10973 attr.config2 = offset; /* kprobe_addr or probe_offset */
10974
10975 /* pid filter is meaningful only for uprobes */
10976 pfd = syscall(__NR_perf_event_open, &attr,
10977 pid < 0 ? -1 : pid /* pid */,
10978 pid == -1 ? 0 : -1 /* cpu */,
10979 -1 /* group_fd */, PERF_FLAG_FD_CLOEXEC);
10980 return pfd >= 0 ? pfd : -errno;
10981 }
10982
append_to_file(const char * file,const char * fmt,...)10983 static int append_to_file(const char *file, const char *fmt, ...)
10984 {
10985 int fd, n, err = 0;
10986 va_list ap;
10987 char buf[1024];
10988
10989 va_start(ap, fmt);
10990 n = vsnprintf(buf, sizeof(buf), fmt, ap);
10991 va_end(ap);
10992
10993 if (n < 0 || n >= sizeof(buf))
10994 return -EINVAL;
10995
10996 fd = open(file, O_WRONLY | O_APPEND | O_CLOEXEC, 0);
10997 if (fd < 0)
10998 return -errno;
10999
11000 if (write(fd, buf, n) < 0)
11001 err = -errno;
11002
11003 close(fd);
11004 return err;
11005 }
11006
11007 #define DEBUGFS "/sys/kernel/debug/tracing"
11008 #define TRACEFS "/sys/kernel/tracing"
11009
use_debugfs(void)11010 static bool use_debugfs(void)
11011 {
11012 static int has_debugfs = -1;
11013
11014 if (has_debugfs < 0)
11015 has_debugfs = faccessat(AT_FDCWD, DEBUGFS, F_OK, AT_EACCESS) == 0;
11016
11017 return has_debugfs == 1;
11018 }
11019
tracefs_path(void)11020 static const char *tracefs_path(void)
11021 {
11022 return use_debugfs() ? DEBUGFS : TRACEFS;
11023 }
11024
tracefs_kprobe_events(void)11025 static const char *tracefs_kprobe_events(void)
11026 {
11027 return use_debugfs() ? DEBUGFS"/kprobe_events" : TRACEFS"/kprobe_events";
11028 }
11029
tracefs_uprobe_events(void)11030 static const char *tracefs_uprobe_events(void)
11031 {
11032 return use_debugfs() ? DEBUGFS"/uprobe_events" : TRACEFS"/uprobe_events";
11033 }
11034
tracefs_available_filter_functions(void)11035 static const char *tracefs_available_filter_functions(void)
11036 {
11037 return use_debugfs() ? DEBUGFS"/available_filter_functions"
11038 : TRACEFS"/available_filter_functions";
11039 }
11040
tracefs_available_filter_functions_addrs(void)11041 static const char *tracefs_available_filter_functions_addrs(void)
11042 {
11043 return use_debugfs() ? DEBUGFS"/available_filter_functions_addrs"
11044 : TRACEFS"/available_filter_functions_addrs";
11045 }
11046
gen_kprobe_legacy_event_name(char * buf,size_t buf_sz,const char * kfunc_name,size_t offset)11047 static void gen_kprobe_legacy_event_name(char *buf, size_t buf_sz,
11048 const char *kfunc_name, size_t offset)
11049 {
11050 static int index = 0;
11051 int i;
11052
11053 snprintf(buf, buf_sz, "libbpf_%u_%s_0x%zx_%d", getpid(), kfunc_name, offset,
11054 __sync_fetch_and_add(&index, 1));
11055
11056 /* sanitize binary_path in the probe name */
11057 for (i = 0; buf[i]; i++) {
11058 if (!isalnum(buf[i]))
11059 buf[i] = '_';
11060 }
11061 }
11062
add_kprobe_event_legacy(const char * probe_name,bool retprobe,const char * kfunc_name,size_t offset)11063 static int add_kprobe_event_legacy(const char *probe_name, bool retprobe,
11064 const char *kfunc_name, size_t offset)
11065 {
11066 return append_to_file(tracefs_kprobe_events(), "%c:%s/%s %s+0x%zx",
11067 retprobe ? 'r' : 'p',
11068 retprobe ? "kretprobes" : "kprobes",
11069 probe_name, kfunc_name, offset);
11070 }
11071
remove_kprobe_event_legacy(const char * probe_name,bool retprobe)11072 static int remove_kprobe_event_legacy(const char *probe_name, bool retprobe)
11073 {
11074 return append_to_file(tracefs_kprobe_events(), "-:%s/%s",
11075 retprobe ? "kretprobes" : "kprobes", probe_name);
11076 }
11077
determine_kprobe_perf_type_legacy(const char * probe_name,bool retprobe)11078 static int determine_kprobe_perf_type_legacy(const char *probe_name, bool retprobe)
11079 {
11080 char file[256];
11081
11082 snprintf(file, sizeof(file), "%s/events/%s/%s/id",
11083 tracefs_path(), retprobe ? "kretprobes" : "kprobes", probe_name);
11084
11085 return parse_uint_from_file(file, "%d\n");
11086 }
11087
perf_event_kprobe_open_legacy(const char * probe_name,bool retprobe,const char * kfunc_name,size_t offset,int pid)11088 static int perf_event_kprobe_open_legacy(const char *probe_name, bool retprobe,
11089 const char *kfunc_name, size_t offset, int pid)
11090 {
11091 const size_t attr_sz = sizeof(struct perf_event_attr);
11092 struct perf_event_attr attr;
11093 int type, pfd, err;
11094
11095 err = add_kprobe_event_legacy(probe_name, retprobe, kfunc_name, offset);
11096 if (err < 0) {
11097 pr_warn("failed to add legacy kprobe event for '%s+0x%zx': %s\n",
11098 kfunc_name, offset,
11099 errstr(err));
11100 return err;
11101 }
11102 type = determine_kprobe_perf_type_legacy(probe_name, retprobe);
11103 if (type < 0) {
11104 err = type;
11105 pr_warn("failed to determine legacy kprobe event id for '%s+0x%zx': %s\n",
11106 kfunc_name, offset,
11107 errstr(err));
11108 goto err_clean_legacy;
11109 }
11110
11111 memset(&attr, 0, attr_sz);
11112 attr.size = attr_sz;
11113 attr.config = type;
11114 attr.type = PERF_TYPE_TRACEPOINT;
11115
11116 pfd = syscall(__NR_perf_event_open, &attr,
11117 pid < 0 ? -1 : pid, /* pid */
11118 pid == -1 ? 0 : -1, /* cpu */
11119 -1 /* group_fd */, PERF_FLAG_FD_CLOEXEC);
11120 if (pfd < 0) {
11121 err = -errno;
11122 pr_warn("legacy kprobe perf_event_open() failed: %s\n",
11123 errstr(err));
11124 goto err_clean_legacy;
11125 }
11126 return pfd;
11127
11128 err_clean_legacy:
11129 /* Clear the newly added legacy kprobe_event */
11130 remove_kprobe_event_legacy(probe_name, retprobe);
11131 return err;
11132 }
11133
arch_specific_syscall_pfx(void)11134 static const char *arch_specific_syscall_pfx(void)
11135 {
11136 #if defined(__x86_64__)
11137 return "x64";
11138 #elif defined(__i386__)
11139 return "ia32";
11140 #elif defined(__s390x__)
11141 return "s390x";
11142 #elif defined(__s390__)
11143 return "s390";
11144 #elif defined(__arm__)
11145 return "arm";
11146 #elif defined(__aarch64__)
11147 return "arm64";
11148 #elif defined(__mips__)
11149 return "mips";
11150 #elif defined(__riscv)
11151 return "riscv";
11152 #elif defined(__powerpc__)
11153 return "powerpc";
11154 #elif defined(__powerpc64__)
11155 return "powerpc64";
11156 #else
11157 return NULL;
11158 #endif
11159 }
11160
probe_kern_syscall_wrapper(int token_fd)11161 int probe_kern_syscall_wrapper(int token_fd)
11162 {
11163 char syscall_name[64];
11164 const char *ksys_pfx;
11165
11166 ksys_pfx = arch_specific_syscall_pfx();
11167 if (!ksys_pfx)
11168 return 0;
11169
11170 snprintf(syscall_name, sizeof(syscall_name), "__%s_sys_bpf", ksys_pfx);
11171
11172 if (determine_kprobe_perf_type() >= 0) {
11173 int pfd;
11174
11175 pfd = perf_event_open_probe(false, false, syscall_name, 0, getpid(), 0);
11176 if (pfd >= 0)
11177 close(pfd);
11178
11179 return pfd >= 0 ? 1 : 0;
11180 } else { /* legacy mode */
11181 char probe_name[128];
11182
11183 gen_kprobe_legacy_event_name(probe_name, sizeof(probe_name), syscall_name, 0);
11184 if (add_kprobe_event_legacy(probe_name, false, syscall_name, 0) < 0)
11185 return 0;
11186
11187 (void)remove_kprobe_event_legacy(probe_name, false);
11188 return 1;
11189 }
11190 }
11191
11192 struct bpf_link *
bpf_program__attach_kprobe_opts(const struct bpf_program * prog,const char * func_name,const struct bpf_kprobe_opts * opts)11193 bpf_program__attach_kprobe_opts(const struct bpf_program *prog,
11194 const char *func_name,
11195 const struct bpf_kprobe_opts *opts)
11196 {
11197 DECLARE_LIBBPF_OPTS(bpf_perf_event_opts, pe_opts);
11198 enum probe_attach_mode attach_mode;
11199 char *legacy_probe = NULL;
11200 struct bpf_link *link;
11201 size_t offset;
11202 bool retprobe, legacy;
11203 int pfd, err;
11204
11205 if (!OPTS_VALID(opts, bpf_kprobe_opts))
11206 return libbpf_err_ptr(-EINVAL);
11207
11208 attach_mode = OPTS_GET(opts, attach_mode, PROBE_ATTACH_MODE_DEFAULT);
11209 retprobe = OPTS_GET(opts, retprobe, false);
11210 offset = OPTS_GET(opts, offset, 0);
11211 pe_opts.bpf_cookie = OPTS_GET(opts, bpf_cookie, 0);
11212
11213 legacy = determine_kprobe_perf_type() < 0;
11214 switch (attach_mode) {
11215 case PROBE_ATTACH_MODE_LEGACY:
11216 legacy = true;
11217 pe_opts.force_ioctl_attach = true;
11218 break;
11219 case PROBE_ATTACH_MODE_PERF:
11220 if (legacy)
11221 return libbpf_err_ptr(-ENOTSUP);
11222 pe_opts.force_ioctl_attach = true;
11223 break;
11224 case PROBE_ATTACH_MODE_LINK:
11225 if (legacy || !kernel_supports(prog->obj, FEAT_PERF_LINK))
11226 return libbpf_err_ptr(-ENOTSUP);
11227 break;
11228 case PROBE_ATTACH_MODE_DEFAULT:
11229 break;
11230 default:
11231 return libbpf_err_ptr(-EINVAL);
11232 }
11233
11234 if (!legacy) {
11235 pfd = perf_event_open_probe(false /* uprobe */, retprobe,
11236 func_name, offset,
11237 -1 /* pid */, 0 /* ref_ctr_off */);
11238 } else {
11239 char probe_name[256];
11240
11241 gen_kprobe_legacy_event_name(probe_name, sizeof(probe_name),
11242 func_name, offset);
11243
11244 legacy_probe = strdup(probe_name);
11245 if (!legacy_probe)
11246 return libbpf_err_ptr(-ENOMEM);
11247
11248 pfd = perf_event_kprobe_open_legacy(legacy_probe, retprobe, func_name,
11249 offset, -1 /* pid */);
11250 }
11251 if (pfd < 0) {
11252 err = -errno;
11253 pr_warn("prog '%s': failed to create %s '%s+0x%zx' perf event: %s\n",
11254 prog->name, retprobe ? "kretprobe" : "kprobe",
11255 func_name, offset,
11256 errstr(err));
11257 goto err_out;
11258 }
11259 link = bpf_program__attach_perf_event_opts(prog, pfd, &pe_opts);
11260 err = libbpf_get_error(link);
11261 if (err) {
11262 close(pfd);
11263 pr_warn("prog '%s': failed to attach to %s '%s+0x%zx': %s\n",
11264 prog->name, retprobe ? "kretprobe" : "kprobe",
11265 func_name, offset,
11266 errstr(err));
11267 goto err_clean_legacy;
11268 }
11269 if (legacy) {
11270 struct bpf_link_perf *perf_link = container_of(link, struct bpf_link_perf, link);
11271
11272 perf_link->legacy_probe_name = legacy_probe;
11273 perf_link->legacy_is_kprobe = true;
11274 perf_link->legacy_is_retprobe = retprobe;
11275 }
11276
11277 return link;
11278
11279 err_clean_legacy:
11280 if (legacy)
11281 remove_kprobe_event_legacy(legacy_probe, retprobe);
11282 err_out:
11283 free(legacy_probe);
11284 return libbpf_err_ptr(err);
11285 }
11286
bpf_program__attach_kprobe(const struct bpf_program * prog,bool retprobe,const char * func_name)11287 struct bpf_link *bpf_program__attach_kprobe(const struct bpf_program *prog,
11288 bool retprobe,
11289 const char *func_name)
11290 {
11291 DECLARE_LIBBPF_OPTS(bpf_kprobe_opts, opts,
11292 .retprobe = retprobe,
11293 );
11294
11295 return bpf_program__attach_kprobe_opts(prog, func_name, &opts);
11296 }
11297
bpf_program__attach_ksyscall(const struct bpf_program * prog,const char * syscall_name,const struct bpf_ksyscall_opts * opts)11298 struct bpf_link *bpf_program__attach_ksyscall(const struct bpf_program *prog,
11299 const char *syscall_name,
11300 const struct bpf_ksyscall_opts *opts)
11301 {
11302 LIBBPF_OPTS(bpf_kprobe_opts, kprobe_opts);
11303 char func_name[128];
11304
11305 if (!OPTS_VALID(opts, bpf_ksyscall_opts))
11306 return libbpf_err_ptr(-EINVAL);
11307
11308 if (kernel_supports(prog->obj, FEAT_SYSCALL_WRAPPER)) {
11309 /* arch_specific_syscall_pfx() should never return NULL here
11310 * because it is guarded by kernel_supports(). However, since
11311 * compiler does not know that we have an explicit conditional
11312 * as well.
11313 */
11314 snprintf(func_name, sizeof(func_name), "__%s_sys_%s",
11315 arch_specific_syscall_pfx() ? : "", syscall_name);
11316 } else {
11317 snprintf(func_name, sizeof(func_name), "__se_sys_%s", syscall_name);
11318 }
11319
11320 kprobe_opts.retprobe = OPTS_GET(opts, retprobe, false);
11321 kprobe_opts.bpf_cookie = OPTS_GET(opts, bpf_cookie, 0);
11322
11323 return bpf_program__attach_kprobe_opts(prog, func_name, &kprobe_opts);
11324 }
11325
11326 /* Adapted from perf/util/string.c */
glob_match(const char * str,const char * pat)11327 bool glob_match(const char *str, const char *pat)
11328 {
11329 while (*str && *pat && *pat != '*') {
11330 if (*pat == '?') { /* Matches any single character */
11331 str++;
11332 pat++;
11333 continue;
11334 }
11335 if (*str != *pat)
11336 return false;
11337 str++;
11338 pat++;
11339 }
11340 /* Check wild card */
11341 if (*pat == '*') {
11342 while (*pat == '*')
11343 pat++;
11344 if (!*pat) /* Tail wild card matches all */
11345 return true;
11346 while (*str)
11347 if (glob_match(str++, pat))
11348 return true;
11349 }
11350 return !*str && !*pat;
11351 }
11352
11353 struct kprobe_multi_resolve {
11354 const char *pattern;
11355 unsigned long *addrs;
11356 size_t cap;
11357 size_t cnt;
11358 };
11359
11360 struct avail_kallsyms_data {
11361 char **syms;
11362 size_t cnt;
11363 struct kprobe_multi_resolve *res;
11364 };
11365
avail_func_cmp(const void * a,const void * b)11366 static int avail_func_cmp(const void *a, const void *b)
11367 {
11368 return strcmp(*(const char **)a, *(const char **)b);
11369 }
11370
avail_kallsyms_cb(unsigned long long sym_addr,char sym_type,const char * sym_name,void * ctx)11371 static int avail_kallsyms_cb(unsigned long long sym_addr, char sym_type,
11372 const char *sym_name, void *ctx)
11373 {
11374 struct avail_kallsyms_data *data = ctx;
11375 struct kprobe_multi_resolve *res = data->res;
11376 int err;
11377
11378 if (!bsearch(&sym_name, data->syms, data->cnt, sizeof(*data->syms), avail_func_cmp))
11379 return 0;
11380
11381 err = libbpf_ensure_mem((void **)&res->addrs, &res->cap, sizeof(*res->addrs), res->cnt + 1);
11382 if (err)
11383 return err;
11384
11385 res->addrs[res->cnt++] = (unsigned long)sym_addr;
11386 return 0;
11387 }
11388
libbpf_available_kallsyms_parse(struct kprobe_multi_resolve * res)11389 static int libbpf_available_kallsyms_parse(struct kprobe_multi_resolve *res)
11390 {
11391 const char *available_functions_file = tracefs_available_filter_functions();
11392 struct avail_kallsyms_data data;
11393 char sym_name[500];
11394 FILE *f;
11395 int err = 0, ret, i;
11396 char **syms = NULL;
11397 size_t cap = 0, cnt = 0;
11398
11399 f = fopen(available_functions_file, "re");
11400 if (!f) {
11401 err = -errno;
11402 pr_warn("failed to open %s: %s\n", available_functions_file, errstr(err));
11403 return err;
11404 }
11405
11406 while (true) {
11407 char *name;
11408
11409 ret = fscanf(f, "%499s%*[^\n]\n", sym_name);
11410 if (ret == EOF && feof(f))
11411 break;
11412
11413 if (ret != 1) {
11414 pr_warn("failed to parse available_filter_functions entry: %d\n", ret);
11415 err = -EINVAL;
11416 goto cleanup;
11417 }
11418
11419 if (!glob_match(sym_name, res->pattern))
11420 continue;
11421
11422 err = libbpf_ensure_mem((void **)&syms, &cap, sizeof(*syms), cnt + 1);
11423 if (err)
11424 goto cleanup;
11425
11426 name = strdup(sym_name);
11427 if (!name) {
11428 err = -errno;
11429 goto cleanup;
11430 }
11431
11432 syms[cnt++] = name;
11433 }
11434
11435 /* no entries found, bail out */
11436 if (cnt == 0) {
11437 err = -ENOENT;
11438 goto cleanup;
11439 }
11440
11441 /* sort available functions */
11442 qsort(syms, cnt, sizeof(*syms), avail_func_cmp);
11443
11444 data.syms = syms;
11445 data.res = res;
11446 data.cnt = cnt;
11447 libbpf_kallsyms_parse(avail_kallsyms_cb, &data);
11448
11449 if (res->cnt == 0)
11450 err = -ENOENT;
11451
11452 cleanup:
11453 for (i = 0; i < cnt; i++)
11454 free((char *)syms[i]);
11455 free(syms);
11456
11457 fclose(f);
11458 return err;
11459 }
11460
has_available_filter_functions_addrs(void)11461 static bool has_available_filter_functions_addrs(void)
11462 {
11463 return access(tracefs_available_filter_functions_addrs(), R_OK) != -1;
11464 }
11465
libbpf_available_kprobes_parse(struct kprobe_multi_resolve * res)11466 static int libbpf_available_kprobes_parse(struct kprobe_multi_resolve *res)
11467 {
11468 const char *available_path = tracefs_available_filter_functions_addrs();
11469 char sym_name[500];
11470 FILE *f;
11471 int ret, err = 0;
11472 unsigned long long sym_addr;
11473
11474 f = fopen(available_path, "re");
11475 if (!f) {
11476 err = -errno;
11477 pr_warn("failed to open %s: %s\n", available_path, errstr(err));
11478 return err;
11479 }
11480
11481 while (true) {
11482 ret = fscanf(f, "%llx %499s%*[^\n]\n", &sym_addr, sym_name);
11483 if (ret == EOF && feof(f))
11484 break;
11485
11486 if (ret != 2) {
11487 pr_warn("failed to parse available_filter_functions_addrs entry: %d\n",
11488 ret);
11489 err = -EINVAL;
11490 goto cleanup;
11491 }
11492
11493 if (!glob_match(sym_name, res->pattern))
11494 continue;
11495
11496 err = libbpf_ensure_mem((void **)&res->addrs, &res->cap,
11497 sizeof(*res->addrs), res->cnt + 1);
11498 if (err)
11499 goto cleanup;
11500
11501 res->addrs[res->cnt++] = (unsigned long)sym_addr;
11502 }
11503
11504 if (res->cnt == 0)
11505 err = -ENOENT;
11506
11507 cleanup:
11508 fclose(f);
11509 return err;
11510 }
11511
11512 struct bpf_link *
bpf_program__attach_kprobe_multi_opts(const struct bpf_program * prog,const char * pattern,const struct bpf_kprobe_multi_opts * opts)11513 bpf_program__attach_kprobe_multi_opts(const struct bpf_program *prog,
11514 const char *pattern,
11515 const struct bpf_kprobe_multi_opts *opts)
11516 {
11517 LIBBPF_OPTS(bpf_link_create_opts, lopts);
11518 struct kprobe_multi_resolve res = {
11519 .pattern = pattern,
11520 };
11521 enum bpf_attach_type attach_type;
11522 struct bpf_link *link = NULL;
11523 const unsigned long *addrs;
11524 int err, link_fd, prog_fd;
11525 bool retprobe, session;
11526 const __u64 *cookies;
11527 const char **syms;
11528 size_t cnt;
11529
11530 if (!OPTS_VALID(opts, bpf_kprobe_multi_opts))
11531 return libbpf_err_ptr(-EINVAL);
11532
11533 prog_fd = bpf_program__fd(prog);
11534 if (prog_fd < 0) {
11535 pr_warn("prog '%s': can't attach BPF program without FD (was it loaded?)\n",
11536 prog->name);
11537 return libbpf_err_ptr(-EINVAL);
11538 }
11539
11540 syms = OPTS_GET(opts, syms, false);
11541 addrs = OPTS_GET(opts, addrs, false);
11542 cnt = OPTS_GET(opts, cnt, false);
11543 cookies = OPTS_GET(opts, cookies, false);
11544
11545 if (!pattern && !addrs && !syms)
11546 return libbpf_err_ptr(-EINVAL);
11547 if (pattern && (addrs || syms || cookies || cnt))
11548 return libbpf_err_ptr(-EINVAL);
11549 if (!pattern && !cnt)
11550 return libbpf_err_ptr(-EINVAL);
11551 if (addrs && syms)
11552 return libbpf_err_ptr(-EINVAL);
11553
11554 if (pattern) {
11555 if (has_available_filter_functions_addrs())
11556 err = libbpf_available_kprobes_parse(&res);
11557 else
11558 err = libbpf_available_kallsyms_parse(&res);
11559 if (err)
11560 goto error;
11561 addrs = res.addrs;
11562 cnt = res.cnt;
11563 }
11564
11565 retprobe = OPTS_GET(opts, retprobe, false);
11566 session = OPTS_GET(opts, session, false);
11567
11568 if (retprobe && session)
11569 return libbpf_err_ptr(-EINVAL);
11570
11571 attach_type = session ? BPF_TRACE_KPROBE_SESSION : BPF_TRACE_KPROBE_MULTI;
11572
11573 lopts.kprobe_multi.syms = syms;
11574 lopts.kprobe_multi.addrs = addrs;
11575 lopts.kprobe_multi.cookies = cookies;
11576 lopts.kprobe_multi.cnt = cnt;
11577 lopts.kprobe_multi.flags = retprobe ? BPF_F_KPROBE_MULTI_RETURN : 0;
11578
11579 link = calloc(1, sizeof(*link));
11580 if (!link) {
11581 err = -ENOMEM;
11582 goto error;
11583 }
11584 link->detach = &bpf_link__detach_fd;
11585
11586 link_fd = bpf_link_create(prog_fd, 0, attach_type, &lopts);
11587 if (link_fd < 0) {
11588 err = -errno;
11589 pr_warn("prog '%s': failed to attach: %s\n",
11590 prog->name, errstr(err));
11591 goto error;
11592 }
11593 link->fd = link_fd;
11594 free(res.addrs);
11595 return link;
11596
11597 error:
11598 free(link);
11599 free(res.addrs);
11600 return libbpf_err_ptr(err);
11601 }
11602
attach_kprobe(const struct bpf_program * prog,long cookie,struct bpf_link ** link)11603 static int attach_kprobe(const struct bpf_program *prog, long cookie, struct bpf_link **link)
11604 {
11605 DECLARE_LIBBPF_OPTS(bpf_kprobe_opts, opts);
11606 unsigned long offset = 0;
11607 const char *func_name;
11608 char *func;
11609 int n;
11610
11611 *link = NULL;
11612
11613 /* no auto-attach for SEC("kprobe") and SEC("kretprobe") */
11614 if (strcmp(prog->sec_name, "kprobe") == 0 || strcmp(prog->sec_name, "kretprobe") == 0)
11615 return 0;
11616
11617 opts.retprobe = str_has_pfx(prog->sec_name, "kretprobe/");
11618 if (opts.retprobe)
11619 func_name = prog->sec_name + sizeof("kretprobe/") - 1;
11620 else
11621 func_name = prog->sec_name + sizeof("kprobe/") - 1;
11622
11623 n = sscanf(func_name, "%m[a-zA-Z0-9_.]+%li", &func, &offset);
11624 if (n < 1) {
11625 pr_warn("kprobe name is invalid: %s\n", func_name);
11626 return -EINVAL;
11627 }
11628 if (opts.retprobe && offset != 0) {
11629 free(func);
11630 pr_warn("kretprobes do not support offset specification\n");
11631 return -EINVAL;
11632 }
11633
11634 opts.offset = offset;
11635 *link = bpf_program__attach_kprobe_opts(prog, func, &opts);
11636 free(func);
11637 return libbpf_get_error(*link);
11638 }
11639
attach_ksyscall(const struct bpf_program * prog,long cookie,struct bpf_link ** link)11640 static int attach_ksyscall(const struct bpf_program *prog, long cookie, struct bpf_link **link)
11641 {
11642 LIBBPF_OPTS(bpf_ksyscall_opts, opts);
11643 const char *syscall_name;
11644
11645 *link = NULL;
11646
11647 /* no auto-attach for SEC("ksyscall") and SEC("kretsyscall") */
11648 if (strcmp(prog->sec_name, "ksyscall") == 0 || strcmp(prog->sec_name, "kretsyscall") == 0)
11649 return 0;
11650
11651 opts.retprobe = str_has_pfx(prog->sec_name, "kretsyscall/");
11652 if (opts.retprobe)
11653 syscall_name = prog->sec_name + sizeof("kretsyscall/") - 1;
11654 else
11655 syscall_name = prog->sec_name + sizeof("ksyscall/") - 1;
11656
11657 *link = bpf_program__attach_ksyscall(prog, syscall_name, &opts);
11658 return *link ? 0 : -errno;
11659 }
11660
attach_kprobe_multi(const struct bpf_program * prog,long cookie,struct bpf_link ** link)11661 static int attach_kprobe_multi(const struct bpf_program *prog, long cookie, struct bpf_link **link)
11662 {
11663 LIBBPF_OPTS(bpf_kprobe_multi_opts, opts);
11664 const char *spec;
11665 char *pattern;
11666 int n;
11667
11668 *link = NULL;
11669
11670 /* no auto-attach for SEC("kprobe.multi") and SEC("kretprobe.multi") */
11671 if (strcmp(prog->sec_name, "kprobe.multi") == 0 ||
11672 strcmp(prog->sec_name, "kretprobe.multi") == 0)
11673 return 0;
11674
11675 opts.retprobe = str_has_pfx(prog->sec_name, "kretprobe.multi/");
11676 if (opts.retprobe)
11677 spec = prog->sec_name + sizeof("kretprobe.multi/") - 1;
11678 else
11679 spec = prog->sec_name + sizeof("kprobe.multi/") - 1;
11680
11681 n = sscanf(spec, "%m[a-zA-Z0-9_.*?]", &pattern);
11682 if (n < 1) {
11683 pr_warn("kprobe multi pattern is invalid: %s\n", spec);
11684 return -EINVAL;
11685 }
11686
11687 *link = bpf_program__attach_kprobe_multi_opts(prog, pattern, &opts);
11688 free(pattern);
11689 return libbpf_get_error(*link);
11690 }
11691
attach_kprobe_session(const struct bpf_program * prog,long cookie,struct bpf_link ** link)11692 static int attach_kprobe_session(const struct bpf_program *prog, long cookie,
11693 struct bpf_link **link)
11694 {
11695 LIBBPF_OPTS(bpf_kprobe_multi_opts, opts, .session = true);
11696 const char *spec;
11697 char *pattern;
11698 int n;
11699
11700 *link = NULL;
11701
11702 /* no auto-attach for SEC("kprobe.session") */
11703 if (strcmp(prog->sec_name, "kprobe.session") == 0)
11704 return 0;
11705
11706 spec = prog->sec_name + sizeof("kprobe.session/") - 1;
11707 n = sscanf(spec, "%m[a-zA-Z0-9_.*?]", &pattern);
11708 if (n < 1) {
11709 pr_warn("kprobe session pattern is invalid: %s\n", spec);
11710 return -EINVAL;
11711 }
11712
11713 *link = bpf_program__attach_kprobe_multi_opts(prog, pattern, &opts);
11714 free(pattern);
11715 return *link ? 0 : -errno;
11716 }
11717
attach_uprobe_multi(const struct bpf_program * prog,long cookie,struct bpf_link ** link)11718 static int attach_uprobe_multi(const struct bpf_program *prog, long cookie, struct bpf_link **link)
11719 {
11720 char *probe_type = NULL, *binary_path = NULL, *func_name = NULL;
11721 LIBBPF_OPTS(bpf_uprobe_multi_opts, opts);
11722 int n, ret = -EINVAL;
11723
11724 *link = NULL;
11725
11726 n = sscanf(prog->sec_name, "%m[^/]/%m[^:]:%m[^\n]",
11727 &probe_type, &binary_path, &func_name);
11728 switch (n) {
11729 case 1:
11730 /* handle SEC("u[ret]probe") - format is valid, but auto-attach is impossible. */
11731 ret = 0;
11732 break;
11733 case 3:
11734 opts.session = str_has_pfx(probe_type, "uprobe.session");
11735 opts.retprobe = str_has_pfx(probe_type, "uretprobe.multi");
11736
11737 *link = bpf_program__attach_uprobe_multi(prog, -1, binary_path, func_name, &opts);
11738 ret = libbpf_get_error(*link);
11739 break;
11740 default:
11741 pr_warn("prog '%s': invalid format of section definition '%s'\n", prog->name,
11742 prog->sec_name);
11743 break;
11744 }
11745 free(probe_type);
11746 free(binary_path);
11747 free(func_name);
11748 return ret;
11749 }
11750
gen_uprobe_legacy_event_name(char * buf,size_t buf_sz,const char * binary_path,uint64_t offset)11751 static void gen_uprobe_legacy_event_name(char *buf, size_t buf_sz,
11752 const char *binary_path, uint64_t offset)
11753 {
11754 int i;
11755
11756 snprintf(buf, buf_sz, "libbpf_%u_%s_0x%zx", getpid(), binary_path, (size_t)offset);
11757
11758 /* sanitize binary_path in the probe name */
11759 for (i = 0; buf[i]; i++) {
11760 if (!isalnum(buf[i]))
11761 buf[i] = '_';
11762 }
11763 }
11764
add_uprobe_event_legacy(const char * probe_name,bool retprobe,const char * binary_path,size_t offset)11765 static inline int add_uprobe_event_legacy(const char *probe_name, bool retprobe,
11766 const char *binary_path, size_t offset)
11767 {
11768 return append_to_file(tracefs_uprobe_events(), "%c:%s/%s %s:0x%zx",
11769 retprobe ? 'r' : 'p',
11770 retprobe ? "uretprobes" : "uprobes",
11771 probe_name, binary_path, offset);
11772 }
11773
remove_uprobe_event_legacy(const char * probe_name,bool retprobe)11774 static inline int remove_uprobe_event_legacy(const char *probe_name, bool retprobe)
11775 {
11776 return append_to_file(tracefs_uprobe_events(), "-:%s/%s",
11777 retprobe ? "uretprobes" : "uprobes", probe_name);
11778 }
11779
determine_uprobe_perf_type_legacy(const char * probe_name,bool retprobe)11780 static int determine_uprobe_perf_type_legacy(const char *probe_name, bool retprobe)
11781 {
11782 char file[512];
11783
11784 snprintf(file, sizeof(file), "%s/events/%s/%s/id",
11785 tracefs_path(), retprobe ? "uretprobes" : "uprobes", probe_name);
11786
11787 return parse_uint_from_file(file, "%d\n");
11788 }
11789
perf_event_uprobe_open_legacy(const char * probe_name,bool retprobe,const char * binary_path,size_t offset,int pid)11790 static int perf_event_uprobe_open_legacy(const char *probe_name, bool retprobe,
11791 const char *binary_path, size_t offset, int pid)
11792 {
11793 const size_t attr_sz = sizeof(struct perf_event_attr);
11794 struct perf_event_attr attr;
11795 int type, pfd, err;
11796
11797 err = add_uprobe_event_legacy(probe_name, retprobe, binary_path, offset);
11798 if (err < 0) {
11799 pr_warn("failed to add legacy uprobe event for %s:0x%zx: %s\n",
11800 binary_path, (size_t)offset, errstr(err));
11801 return err;
11802 }
11803 type = determine_uprobe_perf_type_legacy(probe_name, retprobe);
11804 if (type < 0) {
11805 err = type;
11806 pr_warn("failed to determine legacy uprobe event id for %s:0x%zx: %s\n",
11807 binary_path, offset, errstr(err));
11808 goto err_clean_legacy;
11809 }
11810
11811 memset(&attr, 0, attr_sz);
11812 attr.size = attr_sz;
11813 attr.config = type;
11814 attr.type = PERF_TYPE_TRACEPOINT;
11815
11816 pfd = syscall(__NR_perf_event_open, &attr,
11817 pid < 0 ? -1 : pid, /* pid */
11818 pid == -1 ? 0 : -1, /* cpu */
11819 -1 /* group_fd */, PERF_FLAG_FD_CLOEXEC);
11820 if (pfd < 0) {
11821 err = -errno;
11822 pr_warn("legacy uprobe perf_event_open() failed: %s\n", errstr(err));
11823 goto err_clean_legacy;
11824 }
11825 return pfd;
11826
11827 err_clean_legacy:
11828 /* Clear the newly added legacy uprobe_event */
11829 remove_uprobe_event_legacy(probe_name, retprobe);
11830 return err;
11831 }
11832
11833 /* Find offset of function name in archive specified by path. Currently
11834 * supported are .zip files that do not compress their contents, as used on
11835 * Android in the form of APKs, for example. "file_name" is the name of the ELF
11836 * file inside the archive. "func_name" matches symbol name or name@@LIB for
11837 * library functions.
11838 *
11839 * An overview of the APK format specifically provided here:
11840 * https://en.wikipedia.org/w/index.php?title=Apk_(file_format)&oldid=1139099120#Package_contents
11841 */
elf_find_func_offset_from_archive(const char * archive_path,const char * file_name,const char * func_name)11842 static long elf_find_func_offset_from_archive(const char *archive_path, const char *file_name,
11843 const char *func_name)
11844 {
11845 struct zip_archive *archive;
11846 struct zip_entry entry;
11847 long ret;
11848 Elf *elf;
11849
11850 archive = zip_archive_open(archive_path);
11851 if (IS_ERR(archive)) {
11852 ret = PTR_ERR(archive);
11853 pr_warn("zip: failed to open %s: %ld\n", archive_path, ret);
11854 return ret;
11855 }
11856
11857 ret = zip_archive_find_entry(archive, file_name, &entry);
11858 if (ret) {
11859 pr_warn("zip: could not find archive member %s in %s: %ld\n", file_name,
11860 archive_path, ret);
11861 goto out;
11862 }
11863 pr_debug("zip: found entry for %s in %s at 0x%lx\n", file_name, archive_path,
11864 (unsigned long)entry.data_offset);
11865
11866 if (entry.compression) {
11867 pr_warn("zip: entry %s of %s is compressed and cannot be handled\n", file_name,
11868 archive_path);
11869 ret = -LIBBPF_ERRNO__FORMAT;
11870 goto out;
11871 }
11872
11873 elf = elf_memory((void *)entry.data, entry.data_length);
11874 if (!elf) {
11875 pr_warn("elf: could not read elf file %s from %s: %s\n", file_name, archive_path,
11876 elf_errmsg(-1));
11877 ret = -LIBBPF_ERRNO__LIBELF;
11878 goto out;
11879 }
11880
11881 ret = elf_find_func_offset(elf, file_name, func_name);
11882 if (ret > 0) {
11883 pr_debug("elf: symbol address match for %s of %s in %s: 0x%x + 0x%lx = 0x%lx\n",
11884 func_name, file_name, archive_path, entry.data_offset, ret,
11885 ret + entry.data_offset);
11886 ret += entry.data_offset;
11887 }
11888 elf_end(elf);
11889
11890 out:
11891 zip_archive_close(archive);
11892 return ret;
11893 }
11894
arch_specific_lib_paths(void)11895 static const char *arch_specific_lib_paths(void)
11896 {
11897 /*
11898 * Based on https://packages.debian.org/sid/libc6.
11899 *
11900 * Assume that the traced program is built for the same architecture
11901 * as libbpf, which should cover the vast majority of cases.
11902 */
11903 #if defined(__x86_64__)
11904 return "/lib/x86_64-linux-gnu";
11905 #elif defined(__i386__)
11906 return "/lib/i386-linux-gnu";
11907 #elif defined(__s390x__)
11908 return "/lib/s390x-linux-gnu";
11909 #elif defined(__s390__)
11910 return "/lib/s390-linux-gnu";
11911 #elif defined(__arm__) && defined(__SOFTFP__)
11912 return "/lib/arm-linux-gnueabi";
11913 #elif defined(__arm__) && !defined(__SOFTFP__)
11914 return "/lib/arm-linux-gnueabihf";
11915 #elif defined(__aarch64__)
11916 return "/lib/aarch64-linux-gnu";
11917 #elif defined(__mips__) && defined(__MIPSEL__) && _MIPS_SZLONG == 64
11918 return "/lib/mips64el-linux-gnuabi64";
11919 #elif defined(__mips__) && defined(__MIPSEL__) && _MIPS_SZLONG == 32
11920 return "/lib/mipsel-linux-gnu";
11921 #elif defined(__powerpc64__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
11922 return "/lib/powerpc64le-linux-gnu";
11923 #elif defined(__sparc__) && defined(__arch64__)
11924 return "/lib/sparc64-linux-gnu";
11925 #elif defined(__riscv) && __riscv_xlen == 64
11926 return "/lib/riscv64-linux-gnu";
11927 #else
11928 return NULL;
11929 #endif
11930 }
11931
11932 /* Get full path to program/shared library. */
resolve_full_path(const char * file,char * result,size_t result_sz)11933 static int resolve_full_path(const char *file, char *result, size_t result_sz)
11934 {
11935 const char *search_paths[3] = {};
11936 int i, perm;
11937
11938 if (str_has_sfx(file, ".so") || strstr(file, ".so.")) {
11939 search_paths[0] = getenv("LD_LIBRARY_PATH");
11940 search_paths[1] = "/usr/lib64:/usr/lib";
11941 search_paths[2] = arch_specific_lib_paths();
11942 perm = R_OK;
11943 } else {
11944 search_paths[0] = getenv("PATH");
11945 search_paths[1] = "/usr/bin:/usr/sbin";
11946 perm = R_OK | X_OK;
11947 }
11948
11949 for (i = 0; i < ARRAY_SIZE(search_paths); i++) {
11950 const char *s;
11951
11952 if (!search_paths[i])
11953 continue;
11954 for (s = search_paths[i]; s != NULL; s = strchr(s, ':')) {
11955 char *next_path;
11956 int seg_len;
11957
11958 if (s[0] == ':')
11959 s++;
11960 next_path = strchr(s, ':');
11961 seg_len = next_path ? next_path - s : strlen(s);
11962 if (!seg_len)
11963 continue;
11964 snprintf(result, result_sz, "%.*s/%s", seg_len, s, file);
11965 /* ensure it has required permissions */
11966 if (faccessat(AT_FDCWD, result, perm, AT_EACCESS) < 0)
11967 continue;
11968 pr_debug("resolved '%s' to '%s'\n", file, result);
11969 return 0;
11970 }
11971 }
11972 return -ENOENT;
11973 }
11974
11975 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)11976 bpf_program__attach_uprobe_multi(const struct bpf_program *prog,
11977 pid_t pid,
11978 const char *path,
11979 const char *func_pattern,
11980 const struct bpf_uprobe_multi_opts *opts)
11981 {
11982 const unsigned long *ref_ctr_offsets = NULL, *offsets = NULL;
11983 LIBBPF_OPTS(bpf_link_create_opts, lopts);
11984 unsigned long *resolved_offsets = NULL;
11985 enum bpf_attach_type attach_type;
11986 int err = 0, link_fd, prog_fd;
11987 struct bpf_link *link = NULL;
11988 char full_path[PATH_MAX];
11989 bool retprobe, session;
11990 const __u64 *cookies;
11991 const char **syms;
11992 size_t cnt;
11993
11994 if (!OPTS_VALID(opts, bpf_uprobe_multi_opts))
11995 return libbpf_err_ptr(-EINVAL);
11996
11997 prog_fd = bpf_program__fd(prog);
11998 if (prog_fd < 0) {
11999 pr_warn("prog '%s': can't attach BPF program without FD (was it loaded?)\n",
12000 prog->name);
12001 return libbpf_err_ptr(-EINVAL);
12002 }
12003
12004 syms = OPTS_GET(opts, syms, NULL);
12005 offsets = OPTS_GET(opts, offsets, NULL);
12006 ref_ctr_offsets = OPTS_GET(opts, ref_ctr_offsets, NULL);
12007 cookies = OPTS_GET(opts, cookies, NULL);
12008 cnt = OPTS_GET(opts, cnt, 0);
12009 retprobe = OPTS_GET(opts, retprobe, false);
12010 session = OPTS_GET(opts, session, false);
12011
12012 /*
12013 * User can specify 2 mutually exclusive set of inputs:
12014 *
12015 * 1) use only path/func_pattern/pid arguments
12016 *
12017 * 2) use path/pid with allowed combinations of:
12018 * syms/offsets/ref_ctr_offsets/cookies/cnt
12019 *
12020 * - syms and offsets are mutually exclusive
12021 * - ref_ctr_offsets and cookies are optional
12022 *
12023 * Any other usage results in error.
12024 */
12025
12026 if (!path)
12027 return libbpf_err_ptr(-EINVAL);
12028 if (!func_pattern && cnt == 0)
12029 return libbpf_err_ptr(-EINVAL);
12030
12031 if (func_pattern) {
12032 if (syms || offsets || ref_ctr_offsets || cookies || cnt)
12033 return libbpf_err_ptr(-EINVAL);
12034 } else {
12035 if (!!syms == !!offsets)
12036 return libbpf_err_ptr(-EINVAL);
12037 }
12038
12039 if (retprobe && session)
12040 return libbpf_err_ptr(-EINVAL);
12041
12042 if (func_pattern) {
12043 if (!strchr(path, '/')) {
12044 err = resolve_full_path(path, full_path, sizeof(full_path));
12045 if (err) {
12046 pr_warn("prog '%s': failed to resolve full path for '%s': %s\n",
12047 prog->name, path, errstr(err));
12048 return libbpf_err_ptr(err);
12049 }
12050 path = full_path;
12051 }
12052
12053 err = elf_resolve_pattern_offsets(path, func_pattern,
12054 &resolved_offsets, &cnt);
12055 if (err < 0)
12056 return libbpf_err_ptr(err);
12057 offsets = resolved_offsets;
12058 } else if (syms) {
12059 err = elf_resolve_syms_offsets(path, cnt, syms, &resolved_offsets, STT_FUNC);
12060 if (err < 0)
12061 return libbpf_err_ptr(err);
12062 offsets = resolved_offsets;
12063 }
12064
12065 attach_type = session ? BPF_TRACE_UPROBE_SESSION : BPF_TRACE_UPROBE_MULTI;
12066
12067 lopts.uprobe_multi.path = path;
12068 lopts.uprobe_multi.offsets = offsets;
12069 lopts.uprobe_multi.ref_ctr_offsets = ref_ctr_offsets;
12070 lopts.uprobe_multi.cookies = cookies;
12071 lopts.uprobe_multi.cnt = cnt;
12072 lopts.uprobe_multi.flags = retprobe ? BPF_F_UPROBE_MULTI_RETURN : 0;
12073
12074 if (pid == 0)
12075 pid = getpid();
12076 if (pid > 0)
12077 lopts.uprobe_multi.pid = pid;
12078
12079 link = calloc(1, sizeof(*link));
12080 if (!link) {
12081 err = -ENOMEM;
12082 goto error;
12083 }
12084 link->detach = &bpf_link__detach_fd;
12085
12086 link_fd = bpf_link_create(prog_fd, 0, attach_type, &lopts);
12087 if (link_fd < 0) {
12088 err = -errno;
12089 pr_warn("prog '%s': failed to attach multi-uprobe: %s\n",
12090 prog->name, errstr(err));
12091 goto error;
12092 }
12093 link->fd = link_fd;
12094 free(resolved_offsets);
12095 return link;
12096
12097 error:
12098 free(resolved_offsets);
12099 free(link);
12100 return libbpf_err_ptr(err);
12101 }
12102
12103 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)12104 bpf_program__attach_uprobe_opts(const struct bpf_program *prog, pid_t pid,
12105 const char *binary_path, size_t func_offset,
12106 const struct bpf_uprobe_opts *opts)
12107 {
12108 const char *archive_path = NULL, *archive_sep = NULL;
12109 char *legacy_probe = NULL;
12110 DECLARE_LIBBPF_OPTS(bpf_perf_event_opts, pe_opts);
12111 enum probe_attach_mode attach_mode;
12112 char full_path[PATH_MAX];
12113 struct bpf_link *link;
12114 size_t ref_ctr_off;
12115 int pfd, err;
12116 bool retprobe, legacy;
12117 const char *func_name;
12118
12119 if (!OPTS_VALID(opts, bpf_uprobe_opts))
12120 return libbpf_err_ptr(-EINVAL);
12121
12122 attach_mode = OPTS_GET(opts, attach_mode, PROBE_ATTACH_MODE_DEFAULT);
12123 retprobe = OPTS_GET(opts, retprobe, false);
12124 ref_ctr_off = OPTS_GET(opts, ref_ctr_offset, 0);
12125 pe_opts.bpf_cookie = OPTS_GET(opts, bpf_cookie, 0);
12126
12127 if (!binary_path)
12128 return libbpf_err_ptr(-EINVAL);
12129
12130 /* Check if "binary_path" refers to an archive. */
12131 archive_sep = strstr(binary_path, "!/");
12132 if (archive_sep) {
12133 full_path[0] = '\0';
12134 libbpf_strlcpy(full_path, binary_path,
12135 min(sizeof(full_path), (size_t)(archive_sep - binary_path + 1)));
12136 archive_path = full_path;
12137 binary_path = archive_sep + 2;
12138 } else if (!strchr(binary_path, '/')) {
12139 err = resolve_full_path(binary_path, full_path, sizeof(full_path));
12140 if (err) {
12141 pr_warn("prog '%s': failed to resolve full path for '%s': %s\n",
12142 prog->name, binary_path, errstr(err));
12143 return libbpf_err_ptr(err);
12144 }
12145 binary_path = full_path;
12146 }
12147 func_name = OPTS_GET(opts, func_name, NULL);
12148 if (func_name) {
12149 long sym_off;
12150
12151 if (archive_path) {
12152 sym_off = elf_find_func_offset_from_archive(archive_path, binary_path,
12153 func_name);
12154 binary_path = archive_path;
12155 } else {
12156 sym_off = elf_find_func_offset_from_file(binary_path, func_name);
12157 }
12158 if (sym_off < 0)
12159 return libbpf_err_ptr(sym_off);
12160 func_offset += sym_off;
12161 }
12162
12163 legacy = determine_uprobe_perf_type() < 0;
12164 switch (attach_mode) {
12165 case PROBE_ATTACH_MODE_LEGACY:
12166 legacy = true;
12167 pe_opts.force_ioctl_attach = true;
12168 break;
12169 case PROBE_ATTACH_MODE_PERF:
12170 if (legacy)
12171 return libbpf_err_ptr(-ENOTSUP);
12172 pe_opts.force_ioctl_attach = true;
12173 break;
12174 case PROBE_ATTACH_MODE_LINK:
12175 if (legacy || !kernel_supports(prog->obj, FEAT_PERF_LINK))
12176 return libbpf_err_ptr(-ENOTSUP);
12177 break;
12178 case PROBE_ATTACH_MODE_DEFAULT:
12179 break;
12180 default:
12181 return libbpf_err_ptr(-EINVAL);
12182 }
12183
12184 if (!legacy) {
12185 pfd = perf_event_open_probe(true /* uprobe */, retprobe, binary_path,
12186 func_offset, pid, ref_ctr_off);
12187 } else {
12188 char probe_name[PATH_MAX + 64];
12189
12190 if (ref_ctr_off)
12191 return libbpf_err_ptr(-EINVAL);
12192
12193 gen_uprobe_legacy_event_name(probe_name, sizeof(probe_name),
12194 binary_path, func_offset);
12195
12196 legacy_probe = strdup(probe_name);
12197 if (!legacy_probe)
12198 return libbpf_err_ptr(-ENOMEM);
12199
12200 pfd = perf_event_uprobe_open_legacy(legacy_probe, retprobe,
12201 binary_path, func_offset, pid);
12202 }
12203 if (pfd < 0) {
12204 err = -errno;
12205 pr_warn("prog '%s': failed to create %s '%s:0x%zx' perf event: %s\n",
12206 prog->name, retprobe ? "uretprobe" : "uprobe",
12207 binary_path, func_offset,
12208 errstr(err));
12209 goto err_out;
12210 }
12211
12212 link = bpf_program__attach_perf_event_opts(prog, pfd, &pe_opts);
12213 err = libbpf_get_error(link);
12214 if (err) {
12215 close(pfd);
12216 pr_warn("prog '%s': failed to attach to %s '%s:0x%zx': %s\n",
12217 prog->name, retprobe ? "uretprobe" : "uprobe",
12218 binary_path, func_offset,
12219 errstr(err));
12220 goto err_clean_legacy;
12221 }
12222 if (legacy) {
12223 struct bpf_link_perf *perf_link = container_of(link, struct bpf_link_perf, link);
12224
12225 perf_link->legacy_probe_name = legacy_probe;
12226 perf_link->legacy_is_kprobe = false;
12227 perf_link->legacy_is_retprobe = retprobe;
12228 }
12229 return link;
12230
12231 err_clean_legacy:
12232 if (legacy)
12233 remove_uprobe_event_legacy(legacy_probe, retprobe);
12234 err_out:
12235 free(legacy_probe);
12236 return libbpf_err_ptr(err);
12237 }
12238
12239 /* Format of u[ret]probe section definition supporting auto-attach:
12240 * u[ret]probe/binary:function[+offset]
12241 *
12242 * binary can be an absolute/relative path or a filename; the latter is resolved to a
12243 * full binary path via bpf_program__attach_uprobe_opts.
12244 *
12245 * Specifying uprobe+ ensures we carry out strict matching; either "uprobe" must be
12246 * specified (and auto-attach is not possible) or the above format is specified for
12247 * auto-attach.
12248 */
attach_uprobe(const struct bpf_program * prog,long cookie,struct bpf_link ** link)12249 static int attach_uprobe(const struct bpf_program *prog, long cookie, struct bpf_link **link)
12250 {
12251 DECLARE_LIBBPF_OPTS(bpf_uprobe_opts, opts);
12252 char *probe_type = NULL, *binary_path = NULL, *func_name = NULL, *func_off;
12253 int n, c, ret = -EINVAL;
12254 long offset = 0;
12255
12256 *link = NULL;
12257
12258 n = sscanf(prog->sec_name, "%m[^/]/%m[^:]:%m[^\n]",
12259 &probe_type, &binary_path, &func_name);
12260 switch (n) {
12261 case 1:
12262 /* handle SEC("u[ret]probe") - format is valid, but auto-attach is impossible. */
12263 ret = 0;
12264 break;
12265 case 2:
12266 pr_warn("prog '%s': section '%s' missing ':function[+offset]' specification\n",
12267 prog->name, prog->sec_name);
12268 break;
12269 case 3:
12270 /* check if user specifies `+offset`, if yes, this should be
12271 * the last part of the string, make sure sscanf read to EOL
12272 */
12273 func_off = strrchr(func_name, '+');
12274 if (func_off) {
12275 n = sscanf(func_off, "+%li%n", &offset, &c);
12276 if (n == 1 && *(func_off + c) == '\0')
12277 func_off[0] = '\0';
12278 else
12279 offset = 0;
12280 }
12281 opts.retprobe = strcmp(probe_type, "uretprobe") == 0 ||
12282 strcmp(probe_type, "uretprobe.s") == 0;
12283 if (opts.retprobe && offset != 0) {
12284 pr_warn("prog '%s': uretprobes do not support offset specification\n",
12285 prog->name);
12286 break;
12287 }
12288 opts.func_name = func_name;
12289 *link = bpf_program__attach_uprobe_opts(prog, -1, binary_path, offset, &opts);
12290 ret = libbpf_get_error(*link);
12291 break;
12292 default:
12293 pr_warn("prog '%s': invalid format of section definition '%s'\n", prog->name,
12294 prog->sec_name);
12295 break;
12296 }
12297 free(probe_type);
12298 free(binary_path);
12299 free(func_name);
12300
12301 return ret;
12302 }
12303
bpf_program__attach_uprobe(const struct bpf_program * prog,bool retprobe,pid_t pid,const char * binary_path,size_t func_offset)12304 struct bpf_link *bpf_program__attach_uprobe(const struct bpf_program *prog,
12305 bool retprobe, pid_t pid,
12306 const char *binary_path,
12307 size_t func_offset)
12308 {
12309 DECLARE_LIBBPF_OPTS(bpf_uprobe_opts, opts, .retprobe = retprobe);
12310
12311 return bpf_program__attach_uprobe_opts(prog, pid, binary_path, func_offset, &opts);
12312 }
12313
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)12314 struct bpf_link *bpf_program__attach_usdt(const struct bpf_program *prog,
12315 pid_t pid, const char *binary_path,
12316 const char *usdt_provider, const char *usdt_name,
12317 const struct bpf_usdt_opts *opts)
12318 {
12319 char resolved_path[512];
12320 struct bpf_object *obj = prog->obj;
12321 struct bpf_link *link;
12322 __u64 usdt_cookie;
12323 int err;
12324
12325 if (!OPTS_VALID(opts, bpf_uprobe_opts))
12326 return libbpf_err_ptr(-EINVAL);
12327
12328 if (bpf_program__fd(prog) < 0) {
12329 pr_warn("prog '%s': can't attach BPF program without FD (was it loaded?)\n",
12330 prog->name);
12331 return libbpf_err_ptr(-EINVAL);
12332 }
12333
12334 if (!binary_path)
12335 return libbpf_err_ptr(-EINVAL);
12336
12337 if (!strchr(binary_path, '/')) {
12338 err = resolve_full_path(binary_path, resolved_path, sizeof(resolved_path));
12339 if (err) {
12340 pr_warn("prog '%s': failed to resolve full path for '%s': %s\n",
12341 prog->name, binary_path, errstr(err));
12342 return libbpf_err_ptr(err);
12343 }
12344 binary_path = resolved_path;
12345 }
12346
12347 /* USDT manager is instantiated lazily on first USDT attach. It will
12348 * be destroyed together with BPF object in bpf_object__close().
12349 */
12350 if (IS_ERR(obj->usdt_man))
12351 return libbpf_ptr(obj->usdt_man);
12352 if (!obj->usdt_man) {
12353 obj->usdt_man = usdt_manager_new(obj);
12354 if (IS_ERR(obj->usdt_man))
12355 return libbpf_ptr(obj->usdt_man);
12356 }
12357
12358 usdt_cookie = OPTS_GET(opts, usdt_cookie, 0);
12359 link = usdt_manager_attach_usdt(obj->usdt_man, prog, pid, binary_path,
12360 usdt_provider, usdt_name, usdt_cookie);
12361 err = libbpf_get_error(link);
12362 if (err)
12363 return libbpf_err_ptr(err);
12364 return link;
12365 }
12366
attach_usdt(const struct bpf_program * prog,long cookie,struct bpf_link ** link)12367 static int attach_usdt(const struct bpf_program *prog, long cookie, struct bpf_link **link)
12368 {
12369 char *path = NULL, *provider = NULL, *name = NULL;
12370 const char *sec_name;
12371 int n, err;
12372
12373 sec_name = bpf_program__section_name(prog);
12374 if (strcmp(sec_name, "usdt") == 0) {
12375 /* no auto-attach for just SEC("usdt") */
12376 *link = NULL;
12377 return 0;
12378 }
12379
12380 n = sscanf(sec_name, "usdt/%m[^:]:%m[^:]:%m[^:]", &path, &provider, &name);
12381 if (n != 3) {
12382 pr_warn("invalid section '%s', expected SEC(\"usdt/<path>:<provider>:<name>\")\n",
12383 sec_name);
12384 err = -EINVAL;
12385 } else {
12386 *link = bpf_program__attach_usdt(prog, -1 /* any process */, path,
12387 provider, name, NULL);
12388 err = libbpf_get_error(*link);
12389 }
12390 free(path);
12391 free(provider);
12392 free(name);
12393 return err;
12394 }
12395
determine_tracepoint_id(const char * tp_category,const char * tp_name)12396 static int determine_tracepoint_id(const char *tp_category,
12397 const char *tp_name)
12398 {
12399 char file[PATH_MAX];
12400 int ret;
12401
12402 ret = snprintf(file, sizeof(file), "%s/events/%s/%s/id",
12403 tracefs_path(), tp_category, tp_name);
12404 if (ret < 0)
12405 return -errno;
12406 if (ret >= sizeof(file)) {
12407 pr_debug("tracepoint %s/%s path is too long\n",
12408 tp_category, tp_name);
12409 return -E2BIG;
12410 }
12411 return parse_uint_from_file(file, "%d\n");
12412 }
12413
perf_event_open_tracepoint(const char * tp_category,const char * tp_name)12414 static int perf_event_open_tracepoint(const char *tp_category,
12415 const char *tp_name)
12416 {
12417 const size_t attr_sz = sizeof(struct perf_event_attr);
12418 struct perf_event_attr attr;
12419 int tp_id, pfd, err;
12420
12421 tp_id = determine_tracepoint_id(tp_category, tp_name);
12422 if (tp_id < 0) {
12423 pr_warn("failed to determine tracepoint '%s/%s' perf event ID: %s\n",
12424 tp_category, tp_name,
12425 errstr(tp_id));
12426 return tp_id;
12427 }
12428
12429 memset(&attr, 0, attr_sz);
12430 attr.type = PERF_TYPE_TRACEPOINT;
12431 attr.size = attr_sz;
12432 attr.config = tp_id;
12433
12434 pfd = syscall(__NR_perf_event_open, &attr, -1 /* pid */, 0 /* cpu */,
12435 -1 /* group_fd */, PERF_FLAG_FD_CLOEXEC);
12436 if (pfd < 0) {
12437 err = -errno;
12438 pr_warn("tracepoint '%s/%s' perf_event_open() failed: %s\n",
12439 tp_category, tp_name,
12440 errstr(err));
12441 return err;
12442 }
12443 return pfd;
12444 }
12445
bpf_program__attach_tracepoint_opts(const struct bpf_program * prog,const char * tp_category,const char * tp_name,const struct bpf_tracepoint_opts * opts)12446 struct bpf_link *bpf_program__attach_tracepoint_opts(const struct bpf_program *prog,
12447 const char *tp_category,
12448 const char *tp_name,
12449 const struct bpf_tracepoint_opts *opts)
12450 {
12451 DECLARE_LIBBPF_OPTS(bpf_perf_event_opts, pe_opts);
12452 struct bpf_link *link;
12453 int pfd, err;
12454
12455 if (!OPTS_VALID(opts, bpf_tracepoint_opts))
12456 return libbpf_err_ptr(-EINVAL);
12457
12458 pe_opts.bpf_cookie = OPTS_GET(opts, bpf_cookie, 0);
12459
12460 pfd = perf_event_open_tracepoint(tp_category, tp_name);
12461 if (pfd < 0) {
12462 pr_warn("prog '%s': failed to create tracepoint '%s/%s' perf event: %s\n",
12463 prog->name, tp_category, tp_name,
12464 errstr(pfd));
12465 return libbpf_err_ptr(pfd);
12466 }
12467 link = bpf_program__attach_perf_event_opts(prog, pfd, &pe_opts);
12468 err = libbpf_get_error(link);
12469 if (err) {
12470 close(pfd);
12471 pr_warn("prog '%s': failed to attach to tracepoint '%s/%s': %s\n",
12472 prog->name, tp_category, tp_name,
12473 errstr(err));
12474 return libbpf_err_ptr(err);
12475 }
12476 return link;
12477 }
12478
bpf_program__attach_tracepoint(const struct bpf_program * prog,const char * tp_category,const char * tp_name)12479 struct bpf_link *bpf_program__attach_tracepoint(const struct bpf_program *prog,
12480 const char *tp_category,
12481 const char *tp_name)
12482 {
12483 return bpf_program__attach_tracepoint_opts(prog, tp_category, tp_name, NULL);
12484 }
12485
attach_tp(const struct bpf_program * prog,long cookie,struct bpf_link ** link)12486 static int attach_tp(const struct bpf_program *prog, long cookie, struct bpf_link **link)
12487 {
12488 char *sec_name, *tp_cat, *tp_name;
12489
12490 *link = NULL;
12491
12492 /* no auto-attach for SEC("tp") or SEC("tracepoint") */
12493 if (strcmp(prog->sec_name, "tp") == 0 || strcmp(prog->sec_name, "tracepoint") == 0)
12494 return 0;
12495
12496 sec_name = strdup(prog->sec_name);
12497 if (!sec_name)
12498 return -ENOMEM;
12499
12500 /* extract "tp/<category>/<name>" or "tracepoint/<category>/<name>" */
12501 if (str_has_pfx(prog->sec_name, "tp/"))
12502 tp_cat = sec_name + sizeof("tp/") - 1;
12503 else
12504 tp_cat = sec_name + sizeof("tracepoint/") - 1;
12505 tp_name = strchr(tp_cat, '/');
12506 if (!tp_name) {
12507 free(sec_name);
12508 return -EINVAL;
12509 }
12510 *tp_name = '\0';
12511 tp_name++;
12512
12513 *link = bpf_program__attach_tracepoint(prog, tp_cat, tp_name);
12514 free(sec_name);
12515 return libbpf_get_error(*link);
12516 }
12517
12518 struct bpf_link *
bpf_program__attach_raw_tracepoint_opts(const struct bpf_program * prog,const char * tp_name,struct bpf_raw_tracepoint_opts * opts)12519 bpf_program__attach_raw_tracepoint_opts(const struct bpf_program *prog,
12520 const char *tp_name,
12521 struct bpf_raw_tracepoint_opts *opts)
12522 {
12523 LIBBPF_OPTS(bpf_raw_tp_opts, raw_opts);
12524 struct bpf_link *link;
12525 int prog_fd, pfd;
12526
12527 if (!OPTS_VALID(opts, bpf_raw_tracepoint_opts))
12528 return libbpf_err_ptr(-EINVAL);
12529
12530 prog_fd = bpf_program__fd(prog);
12531 if (prog_fd < 0) {
12532 pr_warn("prog '%s': can't attach before loaded\n", prog->name);
12533 return libbpf_err_ptr(-EINVAL);
12534 }
12535
12536 link = calloc(1, sizeof(*link));
12537 if (!link)
12538 return libbpf_err_ptr(-ENOMEM);
12539 link->detach = &bpf_link__detach_fd;
12540
12541 raw_opts.tp_name = tp_name;
12542 raw_opts.cookie = OPTS_GET(opts, cookie, 0);
12543 pfd = bpf_raw_tracepoint_open_opts(prog_fd, &raw_opts);
12544 if (pfd < 0) {
12545 pfd = -errno;
12546 free(link);
12547 pr_warn("prog '%s': failed to attach to raw tracepoint '%s': %s\n",
12548 prog->name, tp_name, errstr(pfd));
12549 return libbpf_err_ptr(pfd);
12550 }
12551 link->fd = pfd;
12552 return link;
12553 }
12554
bpf_program__attach_raw_tracepoint(const struct bpf_program * prog,const char * tp_name)12555 struct bpf_link *bpf_program__attach_raw_tracepoint(const struct bpf_program *prog,
12556 const char *tp_name)
12557 {
12558 return bpf_program__attach_raw_tracepoint_opts(prog, tp_name, NULL);
12559 }
12560
attach_raw_tp(const struct bpf_program * prog,long cookie,struct bpf_link ** link)12561 static int attach_raw_tp(const struct bpf_program *prog, long cookie, struct bpf_link **link)
12562 {
12563 static const char *const prefixes[] = {
12564 "raw_tp",
12565 "raw_tracepoint",
12566 "raw_tp.w",
12567 "raw_tracepoint.w",
12568 };
12569 size_t i;
12570 const char *tp_name = NULL;
12571
12572 *link = NULL;
12573
12574 for (i = 0; i < ARRAY_SIZE(prefixes); i++) {
12575 size_t pfx_len;
12576
12577 if (!str_has_pfx(prog->sec_name, prefixes[i]))
12578 continue;
12579
12580 pfx_len = strlen(prefixes[i]);
12581 /* no auto-attach case of, e.g., SEC("raw_tp") */
12582 if (prog->sec_name[pfx_len] == '\0')
12583 return 0;
12584
12585 if (prog->sec_name[pfx_len] != '/')
12586 continue;
12587
12588 tp_name = prog->sec_name + pfx_len + 1;
12589 break;
12590 }
12591
12592 if (!tp_name) {
12593 pr_warn("prog '%s': invalid section name '%s'\n",
12594 prog->name, prog->sec_name);
12595 return -EINVAL;
12596 }
12597
12598 *link = bpf_program__attach_raw_tracepoint(prog, tp_name);
12599 return libbpf_get_error(*link);
12600 }
12601
12602 /* 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)12603 static struct bpf_link *bpf_program__attach_btf_id(const struct bpf_program *prog,
12604 const struct bpf_trace_opts *opts)
12605 {
12606 LIBBPF_OPTS(bpf_link_create_opts, link_opts);
12607 struct bpf_link *link;
12608 int prog_fd, pfd;
12609
12610 if (!OPTS_VALID(opts, bpf_trace_opts))
12611 return libbpf_err_ptr(-EINVAL);
12612
12613 prog_fd = bpf_program__fd(prog);
12614 if (prog_fd < 0) {
12615 pr_warn("prog '%s': can't attach before loaded\n", prog->name);
12616 return libbpf_err_ptr(-EINVAL);
12617 }
12618
12619 link = calloc(1, sizeof(*link));
12620 if (!link)
12621 return libbpf_err_ptr(-ENOMEM);
12622 link->detach = &bpf_link__detach_fd;
12623
12624 /* libbpf is smart enough to redirect to BPF_RAW_TRACEPOINT_OPEN on old kernels */
12625 link_opts.tracing.cookie = OPTS_GET(opts, cookie, 0);
12626 pfd = bpf_link_create(prog_fd, 0, bpf_program__expected_attach_type(prog), &link_opts);
12627 if (pfd < 0) {
12628 pfd = -errno;
12629 free(link);
12630 pr_warn("prog '%s': failed to attach: %s\n",
12631 prog->name, errstr(pfd));
12632 return libbpf_err_ptr(pfd);
12633 }
12634 link->fd = pfd;
12635 return link;
12636 }
12637
bpf_program__attach_trace(const struct bpf_program * prog)12638 struct bpf_link *bpf_program__attach_trace(const struct bpf_program *prog)
12639 {
12640 return bpf_program__attach_btf_id(prog, NULL);
12641 }
12642
bpf_program__attach_trace_opts(const struct bpf_program * prog,const struct bpf_trace_opts * opts)12643 struct bpf_link *bpf_program__attach_trace_opts(const struct bpf_program *prog,
12644 const struct bpf_trace_opts *opts)
12645 {
12646 return bpf_program__attach_btf_id(prog, opts);
12647 }
12648
bpf_program__attach_lsm(const struct bpf_program * prog)12649 struct bpf_link *bpf_program__attach_lsm(const struct bpf_program *prog)
12650 {
12651 return bpf_program__attach_btf_id(prog, NULL);
12652 }
12653
attach_trace(const struct bpf_program * prog,long cookie,struct bpf_link ** link)12654 static int attach_trace(const struct bpf_program *prog, long cookie, struct bpf_link **link)
12655 {
12656 *link = bpf_program__attach_trace(prog);
12657 return libbpf_get_error(*link);
12658 }
12659
attach_lsm(const struct bpf_program * prog,long cookie,struct bpf_link ** link)12660 static int attach_lsm(const struct bpf_program *prog, long cookie, struct bpf_link **link)
12661 {
12662 *link = bpf_program__attach_lsm(prog);
12663 return libbpf_get_error(*link);
12664 }
12665
12666 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)12667 bpf_program_attach_fd(const struct bpf_program *prog,
12668 int target_fd, const char *target_name,
12669 const struct bpf_link_create_opts *opts)
12670 {
12671 enum bpf_attach_type attach_type;
12672 struct bpf_link *link;
12673 int prog_fd, link_fd;
12674
12675 prog_fd = bpf_program__fd(prog);
12676 if (prog_fd < 0) {
12677 pr_warn("prog '%s': can't attach before loaded\n", prog->name);
12678 return libbpf_err_ptr(-EINVAL);
12679 }
12680
12681 link = calloc(1, sizeof(*link));
12682 if (!link)
12683 return libbpf_err_ptr(-ENOMEM);
12684 link->detach = &bpf_link__detach_fd;
12685
12686 attach_type = bpf_program__expected_attach_type(prog);
12687 link_fd = bpf_link_create(prog_fd, target_fd, attach_type, opts);
12688 if (link_fd < 0) {
12689 link_fd = -errno;
12690 free(link);
12691 pr_warn("prog '%s': failed to attach to %s: %s\n",
12692 prog->name, target_name,
12693 errstr(link_fd));
12694 return libbpf_err_ptr(link_fd);
12695 }
12696 link->fd = link_fd;
12697 return link;
12698 }
12699
12700 struct bpf_link *
bpf_program__attach_cgroup(const struct bpf_program * prog,int cgroup_fd)12701 bpf_program__attach_cgroup(const struct bpf_program *prog, int cgroup_fd)
12702 {
12703 return bpf_program_attach_fd(prog, cgroup_fd, "cgroup", NULL);
12704 }
12705
12706 struct bpf_link *
bpf_program__attach_netns(const struct bpf_program * prog,int netns_fd)12707 bpf_program__attach_netns(const struct bpf_program *prog, int netns_fd)
12708 {
12709 return bpf_program_attach_fd(prog, netns_fd, "netns", NULL);
12710 }
12711
12712 struct bpf_link *
bpf_program__attach_sockmap(const struct bpf_program * prog,int map_fd)12713 bpf_program__attach_sockmap(const struct bpf_program *prog, int map_fd)
12714 {
12715 return bpf_program_attach_fd(prog, map_fd, "sockmap", NULL);
12716 }
12717
bpf_program__attach_xdp(const struct bpf_program * prog,int ifindex)12718 struct bpf_link *bpf_program__attach_xdp(const struct bpf_program *prog, int ifindex)
12719 {
12720 /* target_fd/target_ifindex use the same field in LINK_CREATE */
12721 return bpf_program_attach_fd(prog, ifindex, "xdp", NULL);
12722 }
12723
12724 struct bpf_link *
bpf_program__attach_tcx(const struct bpf_program * prog,int ifindex,const struct bpf_tcx_opts * opts)12725 bpf_program__attach_tcx(const struct bpf_program *prog, int ifindex,
12726 const struct bpf_tcx_opts *opts)
12727 {
12728 LIBBPF_OPTS(bpf_link_create_opts, link_create_opts);
12729 __u32 relative_id;
12730 int relative_fd;
12731
12732 if (!OPTS_VALID(opts, bpf_tcx_opts))
12733 return libbpf_err_ptr(-EINVAL);
12734
12735 relative_id = OPTS_GET(opts, relative_id, 0);
12736 relative_fd = OPTS_GET(opts, relative_fd, 0);
12737
12738 /* validate we don't have unexpected combinations of non-zero fields */
12739 if (!ifindex) {
12740 pr_warn("prog '%s': target netdevice ifindex cannot be zero\n",
12741 prog->name);
12742 return libbpf_err_ptr(-EINVAL);
12743 }
12744 if (relative_fd && relative_id) {
12745 pr_warn("prog '%s': relative_fd and relative_id cannot be set at the same time\n",
12746 prog->name);
12747 return libbpf_err_ptr(-EINVAL);
12748 }
12749
12750 link_create_opts.tcx.expected_revision = OPTS_GET(opts, expected_revision, 0);
12751 link_create_opts.tcx.relative_fd = relative_fd;
12752 link_create_opts.tcx.relative_id = relative_id;
12753 link_create_opts.flags = OPTS_GET(opts, flags, 0);
12754
12755 /* target_fd/target_ifindex use the same field in LINK_CREATE */
12756 return bpf_program_attach_fd(prog, ifindex, "tcx", &link_create_opts);
12757 }
12758
12759 struct bpf_link *
bpf_program__attach_netkit(const struct bpf_program * prog,int ifindex,const struct bpf_netkit_opts * opts)12760 bpf_program__attach_netkit(const struct bpf_program *prog, int ifindex,
12761 const struct bpf_netkit_opts *opts)
12762 {
12763 LIBBPF_OPTS(bpf_link_create_opts, link_create_opts);
12764 __u32 relative_id;
12765 int relative_fd;
12766
12767 if (!OPTS_VALID(opts, bpf_netkit_opts))
12768 return libbpf_err_ptr(-EINVAL);
12769
12770 relative_id = OPTS_GET(opts, relative_id, 0);
12771 relative_fd = OPTS_GET(opts, relative_fd, 0);
12772
12773 /* validate we don't have unexpected combinations of non-zero fields */
12774 if (!ifindex) {
12775 pr_warn("prog '%s': target netdevice ifindex cannot be zero\n",
12776 prog->name);
12777 return libbpf_err_ptr(-EINVAL);
12778 }
12779 if (relative_fd && relative_id) {
12780 pr_warn("prog '%s': relative_fd and relative_id cannot be set at the same time\n",
12781 prog->name);
12782 return libbpf_err_ptr(-EINVAL);
12783 }
12784
12785 link_create_opts.netkit.expected_revision = OPTS_GET(opts, expected_revision, 0);
12786 link_create_opts.netkit.relative_fd = relative_fd;
12787 link_create_opts.netkit.relative_id = relative_id;
12788 link_create_opts.flags = OPTS_GET(opts, flags, 0);
12789
12790 return bpf_program_attach_fd(prog, ifindex, "netkit", &link_create_opts);
12791 }
12792
bpf_program__attach_freplace(const struct bpf_program * prog,int target_fd,const char * attach_func_name)12793 struct bpf_link *bpf_program__attach_freplace(const struct bpf_program *prog,
12794 int target_fd,
12795 const char *attach_func_name)
12796 {
12797 int btf_id;
12798
12799 if (!!target_fd != !!attach_func_name) {
12800 pr_warn("prog '%s': supply none or both of target_fd and attach_func_name\n",
12801 prog->name);
12802 return libbpf_err_ptr(-EINVAL);
12803 }
12804
12805 if (prog->type != BPF_PROG_TYPE_EXT) {
12806 pr_warn("prog '%s': only BPF_PROG_TYPE_EXT can attach as freplace\n",
12807 prog->name);
12808 return libbpf_err_ptr(-EINVAL);
12809 }
12810
12811 if (target_fd) {
12812 LIBBPF_OPTS(bpf_link_create_opts, target_opts);
12813
12814 btf_id = libbpf_find_prog_btf_id(attach_func_name, target_fd);
12815 if (btf_id < 0)
12816 return libbpf_err_ptr(btf_id);
12817
12818 target_opts.target_btf_id = btf_id;
12819
12820 return bpf_program_attach_fd(prog, target_fd, "freplace",
12821 &target_opts);
12822 } else {
12823 /* no target, so use raw_tracepoint_open for compatibility
12824 * with old kernels
12825 */
12826 return bpf_program__attach_trace(prog);
12827 }
12828 }
12829
12830 struct bpf_link *
bpf_program__attach_iter(const struct bpf_program * prog,const struct bpf_iter_attach_opts * opts)12831 bpf_program__attach_iter(const struct bpf_program *prog,
12832 const struct bpf_iter_attach_opts *opts)
12833 {
12834 DECLARE_LIBBPF_OPTS(bpf_link_create_opts, link_create_opts);
12835 struct bpf_link *link;
12836 int prog_fd, link_fd;
12837 __u32 target_fd = 0;
12838
12839 if (!OPTS_VALID(opts, bpf_iter_attach_opts))
12840 return libbpf_err_ptr(-EINVAL);
12841
12842 link_create_opts.iter_info = OPTS_GET(opts, link_info, (void *)0);
12843 link_create_opts.iter_info_len = OPTS_GET(opts, link_info_len, 0);
12844
12845 prog_fd = bpf_program__fd(prog);
12846 if (prog_fd < 0) {
12847 pr_warn("prog '%s': can't attach before loaded\n", prog->name);
12848 return libbpf_err_ptr(-EINVAL);
12849 }
12850
12851 link = calloc(1, sizeof(*link));
12852 if (!link)
12853 return libbpf_err_ptr(-ENOMEM);
12854 link->detach = &bpf_link__detach_fd;
12855
12856 link_fd = bpf_link_create(prog_fd, target_fd, BPF_TRACE_ITER,
12857 &link_create_opts);
12858 if (link_fd < 0) {
12859 link_fd = -errno;
12860 free(link);
12861 pr_warn("prog '%s': failed to attach to iterator: %s\n",
12862 prog->name, errstr(link_fd));
12863 return libbpf_err_ptr(link_fd);
12864 }
12865 link->fd = link_fd;
12866 return link;
12867 }
12868
attach_iter(const struct bpf_program * prog,long cookie,struct bpf_link ** link)12869 static int attach_iter(const struct bpf_program *prog, long cookie, struct bpf_link **link)
12870 {
12871 *link = bpf_program__attach_iter(prog, NULL);
12872 return libbpf_get_error(*link);
12873 }
12874
bpf_program__attach_netfilter(const struct bpf_program * prog,const struct bpf_netfilter_opts * opts)12875 struct bpf_link *bpf_program__attach_netfilter(const struct bpf_program *prog,
12876 const struct bpf_netfilter_opts *opts)
12877 {
12878 LIBBPF_OPTS(bpf_link_create_opts, lopts);
12879 struct bpf_link *link;
12880 int prog_fd, link_fd;
12881
12882 if (!OPTS_VALID(opts, bpf_netfilter_opts))
12883 return libbpf_err_ptr(-EINVAL);
12884
12885 prog_fd = bpf_program__fd(prog);
12886 if (prog_fd < 0) {
12887 pr_warn("prog '%s': can't attach before loaded\n", prog->name);
12888 return libbpf_err_ptr(-EINVAL);
12889 }
12890
12891 link = calloc(1, sizeof(*link));
12892 if (!link)
12893 return libbpf_err_ptr(-ENOMEM);
12894
12895 link->detach = &bpf_link__detach_fd;
12896
12897 lopts.netfilter.pf = OPTS_GET(opts, pf, 0);
12898 lopts.netfilter.hooknum = OPTS_GET(opts, hooknum, 0);
12899 lopts.netfilter.priority = OPTS_GET(opts, priority, 0);
12900 lopts.netfilter.flags = OPTS_GET(opts, flags, 0);
12901
12902 link_fd = bpf_link_create(prog_fd, 0, BPF_NETFILTER, &lopts);
12903 if (link_fd < 0) {
12904 link_fd = -errno;
12905 free(link);
12906 pr_warn("prog '%s': failed to attach to netfilter: %s\n",
12907 prog->name, errstr(link_fd));
12908 return libbpf_err_ptr(link_fd);
12909 }
12910 link->fd = link_fd;
12911
12912 return link;
12913 }
12914
bpf_program__attach(const struct bpf_program * prog)12915 struct bpf_link *bpf_program__attach(const struct bpf_program *prog)
12916 {
12917 struct bpf_link *link = NULL;
12918 int err;
12919
12920 if (!prog->sec_def || !prog->sec_def->prog_attach_fn)
12921 return libbpf_err_ptr(-EOPNOTSUPP);
12922
12923 if (bpf_program__fd(prog) < 0) {
12924 pr_warn("prog '%s': can't attach BPF program without FD (was it loaded?)\n",
12925 prog->name);
12926 return libbpf_err_ptr(-EINVAL);
12927 }
12928
12929 err = prog->sec_def->prog_attach_fn(prog, prog->sec_def->cookie, &link);
12930 if (err)
12931 return libbpf_err_ptr(err);
12932
12933 /* When calling bpf_program__attach() explicitly, auto-attach support
12934 * is expected to work, so NULL returned link is considered an error.
12935 * This is different for skeleton's attach, see comment in
12936 * bpf_object__attach_skeleton().
12937 */
12938 if (!link)
12939 return libbpf_err_ptr(-EOPNOTSUPP);
12940
12941 return link;
12942 }
12943
12944 struct bpf_link_struct_ops {
12945 struct bpf_link link;
12946 int map_fd;
12947 };
12948
bpf_link__detach_struct_ops(struct bpf_link * link)12949 static int bpf_link__detach_struct_ops(struct bpf_link *link)
12950 {
12951 struct bpf_link_struct_ops *st_link;
12952 __u32 zero = 0;
12953
12954 st_link = container_of(link, struct bpf_link_struct_ops, link);
12955
12956 if (st_link->map_fd < 0)
12957 /* w/o a real link */
12958 return bpf_map_delete_elem(link->fd, &zero);
12959
12960 return close(link->fd);
12961 }
12962
bpf_map__attach_struct_ops(const struct bpf_map * map)12963 struct bpf_link *bpf_map__attach_struct_ops(const struct bpf_map *map)
12964 {
12965 struct bpf_link_struct_ops *link;
12966 __u32 zero = 0;
12967 int err, fd;
12968
12969 if (!bpf_map__is_struct_ops(map)) {
12970 pr_warn("map '%s': can't attach non-struct_ops map\n", map->name);
12971 return libbpf_err_ptr(-EINVAL);
12972 }
12973
12974 if (map->fd < 0) {
12975 pr_warn("map '%s': can't attach BPF map without FD (was it created?)\n", map->name);
12976 return libbpf_err_ptr(-EINVAL);
12977 }
12978
12979 link = calloc(1, sizeof(*link));
12980 if (!link)
12981 return libbpf_err_ptr(-EINVAL);
12982
12983 /* kern_vdata should be prepared during the loading phase. */
12984 err = bpf_map_update_elem(map->fd, &zero, map->st_ops->kern_vdata, 0);
12985 /* It can be EBUSY if the map has been used to create or
12986 * update a link before. We don't allow updating the value of
12987 * a struct_ops once it is set. That ensures that the value
12988 * never changed. So, it is safe to skip EBUSY.
12989 */
12990 if (err && (!(map->def.map_flags & BPF_F_LINK) || err != -EBUSY)) {
12991 free(link);
12992 return libbpf_err_ptr(err);
12993 }
12994
12995 link->link.detach = bpf_link__detach_struct_ops;
12996
12997 if (!(map->def.map_flags & BPF_F_LINK)) {
12998 /* w/o a real link */
12999 link->link.fd = map->fd;
13000 link->map_fd = -1;
13001 return &link->link;
13002 }
13003
13004 fd = bpf_link_create(map->fd, 0, BPF_STRUCT_OPS, NULL);
13005 if (fd < 0) {
13006 free(link);
13007 return libbpf_err_ptr(fd);
13008 }
13009
13010 link->link.fd = fd;
13011 link->map_fd = map->fd;
13012
13013 return &link->link;
13014 }
13015
13016 /*
13017 * Swap the back struct_ops of a link with a new struct_ops map.
13018 */
bpf_link__update_map(struct bpf_link * link,const struct bpf_map * map)13019 int bpf_link__update_map(struct bpf_link *link, const struct bpf_map *map)
13020 {
13021 struct bpf_link_struct_ops *st_ops_link;
13022 __u32 zero = 0;
13023 int err;
13024
13025 if (!bpf_map__is_struct_ops(map))
13026 return -EINVAL;
13027
13028 if (map->fd < 0) {
13029 pr_warn("map '%s': can't use BPF map without FD (was it created?)\n", map->name);
13030 return -EINVAL;
13031 }
13032
13033 st_ops_link = container_of(link, struct bpf_link_struct_ops, link);
13034 /* Ensure the type of a link is correct */
13035 if (st_ops_link->map_fd < 0)
13036 return -EINVAL;
13037
13038 err = bpf_map_update_elem(map->fd, &zero, map->st_ops->kern_vdata, 0);
13039 /* It can be EBUSY if the map has been used to create or
13040 * update a link before. We don't allow updating the value of
13041 * a struct_ops once it is set. That ensures that the value
13042 * never changed. So, it is safe to skip EBUSY.
13043 */
13044 if (err && err != -EBUSY)
13045 return err;
13046
13047 err = bpf_link_update(link->fd, map->fd, NULL);
13048 if (err < 0)
13049 return err;
13050
13051 st_ops_link->map_fd = map->fd;
13052
13053 return 0;
13054 }
13055
13056 typedef enum bpf_perf_event_ret (*bpf_perf_event_print_t)(struct perf_event_header *hdr,
13057 void *private_data);
13058
13059 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)13060 perf_event_read_simple(void *mmap_mem, size_t mmap_size, size_t page_size,
13061 void **copy_mem, size_t *copy_size,
13062 bpf_perf_event_print_t fn, void *private_data)
13063 {
13064 struct perf_event_mmap_page *header = mmap_mem;
13065 __u64 data_head = ring_buffer_read_head(header);
13066 __u64 data_tail = header->data_tail;
13067 void *base = ((__u8 *)header) + page_size;
13068 int ret = LIBBPF_PERF_EVENT_CONT;
13069 struct perf_event_header *ehdr;
13070 size_t ehdr_size;
13071
13072 while (data_head != data_tail) {
13073 ehdr = base + (data_tail & (mmap_size - 1));
13074 ehdr_size = ehdr->size;
13075
13076 if (((void *)ehdr) + ehdr_size > base + mmap_size) {
13077 void *copy_start = ehdr;
13078 size_t len_first = base + mmap_size - copy_start;
13079 size_t len_secnd = ehdr_size - len_first;
13080
13081 if (*copy_size < ehdr_size) {
13082 free(*copy_mem);
13083 *copy_mem = malloc(ehdr_size);
13084 if (!*copy_mem) {
13085 *copy_size = 0;
13086 ret = LIBBPF_PERF_EVENT_ERROR;
13087 break;
13088 }
13089 *copy_size = ehdr_size;
13090 }
13091
13092 memcpy(*copy_mem, copy_start, len_first);
13093 memcpy(*copy_mem + len_first, base, len_secnd);
13094 ehdr = *copy_mem;
13095 }
13096
13097 ret = fn(ehdr, private_data);
13098 data_tail += ehdr_size;
13099 if (ret != LIBBPF_PERF_EVENT_CONT)
13100 break;
13101 }
13102
13103 ring_buffer_write_tail(header, data_tail);
13104 return libbpf_err(ret);
13105 }
13106
13107 struct perf_buffer;
13108
13109 struct perf_buffer_params {
13110 struct perf_event_attr *attr;
13111 /* if event_cb is specified, it takes precendence */
13112 perf_buffer_event_fn event_cb;
13113 /* sample_cb and lost_cb are higher-level common-case callbacks */
13114 perf_buffer_sample_fn sample_cb;
13115 perf_buffer_lost_fn lost_cb;
13116 void *ctx;
13117 int cpu_cnt;
13118 int *cpus;
13119 int *map_keys;
13120 };
13121
13122 struct perf_cpu_buf {
13123 struct perf_buffer *pb;
13124 void *base; /* mmap()'ed memory */
13125 void *buf; /* for reconstructing segmented data */
13126 size_t buf_size;
13127 int fd;
13128 int cpu;
13129 int map_key;
13130 };
13131
13132 struct perf_buffer {
13133 perf_buffer_event_fn event_cb;
13134 perf_buffer_sample_fn sample_cb;
13135 perf_buffer_lost_fn lost_cb;
13136 void *ctx; /* passed into callbacks */
13137
13138 size_t page_size;
13139 size_t mmap_size;
13140 struct perf_cpu_buf **cpu_bufs;
13141 struct epoll_event *events;
13142 int cpu_cnt; /* number of allocated CPU buffers */
13143 int epoll_fd; /* perf event FD */
13144 int map_fd; /* BPF_MAP_TYPE_PERF_EVENT_ARRAY BPF map FD */
13145 };
13146
perf_buffer__free_cpu_buf(struct perf_buffer * pb,struct perf_cpu_buf * cpu_buf)13147 static void perf_buffer__free_cpu_buf(struct perf_buffer *pb,
13148 struct perf_cpu_buf *cpu_buf)
13149 {
13150 if (!cpu_buf)
13151 return;
13152 if (cpu_buf->base &&
13153 munmap(cpu_buf->base, pb->mmap_size + pb->page_size))
13154 pr_warn("failed to munmap cpu_buf #%d\n", cpu_buf->cpu);
13155 if (cpu_buf->fd >= 0) {
13156 ioctl(cpu_buf->fd, PERF_EVENT_IOC_DISABLE, 0);
13157 close(cpu_buf->fd);
13158 }
13159 free(cpu_buf->buf);
13160 free(cpu_buf);
13161 }
13162
perf_buffer__free(struct perf_buffer * pb)13163 void perf_buffer__free(struct perf_buffer *pb)
13164 {
13165 int i;
13166
13167 if (IS_ERR_OR_NULL(pb))
13168 return;
13169 if (pb->cpu_bufs) {
13170 for (i = 0; i < pb->cpu_cnt; i++) {
13171 struct perf_cpu_buf *cpu_buf = pb->cpu_bufs[i];
13172
13173 if (!cpu_buf)
13174 continue;
13175
13176 bpf_map_delete_elem(pb->map_fd, &cpu_buf->map_key);
13177 perf_buffer__free_cpu_buf(pb, cpu_buf);
13178 }
13179 free(pb->cpu_bufs);
13180 }
13181 if (pb->epoll_fd >= 0)
13182 close(pb->epoll_fd);
13183 free(pb->events);
13184 free(pb);
13185 }
13186
13187 static struct perf_cpu_buf *
perf_buffer__open_cpu_buf(struct perf_buffer * pb,struct perf_event_attr * attr,int cpu,int map_key)13188 perf_buffer__open_cpu_buf(struct perf_buffer *pb, struct perf_event_attr *attr,
13189 int cpu, int map_key)
13190 {
13191 struct perf_cpu_buf *cpu_buf;
13192 int err;
13193
13194 cpu_buf = calloc(1, sizeof(*cpu_buf));
13195 if (!cpu_buf)
13196 return ERR_PTR(-ENOMEM);
13197
13198 cpu_buf->pb = pb;
13199 cpu_buf->cpu = cpu;
13200 cpu_buf->map_key = map_key;
13201
13202 cpu_buf->fd = syscall(__NR_perf_event_open, attr, -1 /* pid */, cpu,
13203 -1, PERF_FLAG_FD_CLOEXEC);
13204 if (cpu_buf->fd < 0) {
13205 err = -errno;
13206 pr_warn("failed to open perf buffer event on cpu #%d: %s\n",
13207 cpu, errstr(err));
13208 goto error;
13209 }
13210
13211 cpu_buf->base = mmap(NULL, pb->mmap_size + pb->page_size,
13212 PROT_READ | PROT_WRITE, MAP_SHARED,
13213 cpu_buf->fd, 0);
13214 if (cpu_buf->base == MAP_FAILED) {
13215 cpu_buf->base = NULL;
13216 err = -errno;
13217 pr_warn("failed to mmap perf buffer on cpu #%d: %s\n",
13218 cpu, errstr(err));
13219 goto error;
13220 }
13221
13222 if (ioctl(cpu_buf->fd, PERF_EVENT_IOC_ENABLE, 0) < 0) {
13223 err = -errno;
13224 pr_warn("failed to enable perf buffer event on cpu #%d: %s\n",
13225 cpu, errstr(err));
13226 goto error;
13227 }
13228
13229 return cpu_buf;
13230
13231 error:
13232 perf_buffer__free_cpu_buf(pb, cpu_buf);
13233 return (struct perf_cpu_buf *)ERR_PTR(err);
13234 }
13235
13236 static struct perf_buffer *__perf_buffer__new(int map_fd, size_t page_cnt,
13237 struct perf_buffer_params *p);
13238
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)13239 struct perf_buffer *perf_buffer__new(int map_fd, size_t page_cnt,
13240 perf_buffer_sample_fn sample_cb,
13241 perf_buffer_lost_fn lost_cb,
13242 void *ctx,
13243 const struct perf_buffer_opts *opts)
13244 {
13245 const size_t attr_sz = sizeof(struct perf_event_attr);
13246 struct perf_buffer_params p = {};
13247 struct perf_event_attr attr;
13248 __u32 sample_period;
13249
13250 if (!OPTS_VALID(opts, perf_buffer_opts))
13251 return libbpf_err_ptr(-EINVAL);
13252
13253 sample_period = OPTS_GET(opts, sample_period, 1);
13254 if (!sample_period)
13255 sample_period = 1;
13256
13257 memset(&attr, 0, attr_sz);
13258 attr.size = attr_sz;
13259 attr.config = PERF_COUNT_SW_BPF_OUTPUT;
13260 attr.type = PERF_TYPE_SOFTWARE;
13261 attr.sample_type = PERF_SAMPLE_RAW;
13262 attr.sample_period = sample_period;
13263 attr.wakeup_events = sample_period;
13264
13265 p.attr = &attr;
13266 p.sample_cb = sample_cb;
13267 p.lost_cb = lost_cb;
13268 p.ctx = ctx;
13269
13270 return libbpf_ptr(__perf_buffer__new(map_fd, page_cnt, &p));
13271 }
13272
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)13273 struct perf_buffer *perf_buffer__new_raw(int map_fd, size_t page_cnt,
13274 struct perf_event_attr *attr,
13275 perf_buffer_event_fn event_cb, void *ctx,
13276 const struct perf_buffer_raw_opts *opts)
13277 {
13278 struct perf_buffer_params p = {};
13279
13280 if (!attr)
13281 return libbpf_err_ptr(-EINVAL);
13282
13283 if (!OPTS_VALID(opts, perf_buffer_raw_opts))
13284 return libbpf_err_ptr(-EINVAL);
13285
13286 p.attr = attr;
13287 p.event_cb = event_cb;
13288 p.ctx = ctx;
13289 p.cpu_cnt = OPTS_GET(opts, cpu_cnt, 0);
13290 p.cpus = OPTS_GET(opts, cpus, NULL);
13291 p.map_keys = OPTS_GET(opts, map_keys, NULL);
13292
13293 return libbpf_ptr(__perf_buffer__new(map_fd, page_cnt, &p));
13294 }
13295
__perf_buffer__new(int map_fd,size_t page_cnt,struct perf_buffer_params * p)13296 static struct perf_buffer *__perf_buffer__new(int map_fd, size_t page_cnt,
13297 struct perf_buffer_params *p)
13298 {
13299 const char *online_cpus_file = "/sys/devices/system/cpu/online";
13300 struct bpf_map_info map;
13301 struct perf_buffer *pb;
13302 bool *online = NULL;
13303 __u32 map_info_len;
13304 int err, i, j, n;
13305
13306 if (page_cnt == 0 || (page_cnt & (page_cnt - 1))) {
13307 pr_warn("page count should be power of two, but is %zu\n",
13308 page_cnt);
13309 return ERR_PTR(-EINVAL);
13310 }
13311
13312 /* best-effort sanity checks */
13313 memset(&map, 0, sizeof(map));
13314 map_info_len = sizeof(map);
13315 err = bpf_map_get_info_by_fd(map_fd, &map, &map_info_len);
13316 if (err) {
13317 err = -errno;
13318 /* if BPF_OBJ_GET_INFO_BY_FD is supported, will return
13319 * -EBADFD, -EFAULT, or -E2BIG on real error
13320 */
13321 if (err != -EINVAL) {
13322 pr_warn("failed to get map info for map FD %d: %s\n",
13323 map_fd, errstr(err));
13324 return ERR_PTR(err);
13325 }
13326 pr_debug("failed to get map info for FD %d; API not supported? Ignoring...\n",
13327 map_fd);
13328 } else {
13329 if (map.type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) {
13330 pr_warn("map '%s' should be BPF_MAP_TYPE_PERF_EVENT_ARRAY\n",
13331 map.name);
13332 return ERR_PTR(-EINVAL);
13333 }
13334 }
13335
13336 pb = calloc(1, sizeof(*pb));
13337 if (!pb)
13338 return ERR_PTR(-ENOMEM);
13339
13340 pb->event_cb = p->event_cb;
13341 pb->sample_cb = p->sample_cb;
13342 pb->lost_cb = p->lost_cb;
13343 pb->ctx = p->ctx;
13344
13345 pb->page_size = getpagesize();
13346 pb->mmap_size = pb->page_size * page_cnt;
13347 pb->map_fd = map_fd;
13348
13349 pb->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
13350 if (pb->epoll_fd < 0) {
13351 err = -errno;
13352 pr_warn("failed to create epoll instance: %s\n",
13353 errstr(err));
13354 goto error;
13355 }
13356
13357 if (p->cpu_cnt > 0) {
13358 pb->cpu_cnt = p->cpu_cnt;
13359 } else {
13360 pb->cpu_cnt = libbpf_num_possible_cpus();
13361 if (pb->cpu_cnt < 0) {
13362 err = pb->cpu_cnt;
13363 goto error;
13364 }
13365 if (map.max_entries && map.max_entries < pb->cpu_cnt)
13366 pb->cpu_cnt = map.max_entries;
13367 }
13368
13369 pb->events = calloc(pb->cpu_cnt, sizeof(*pb->events));
13370 if (!pb->events) {
13371 err = -ENOMEM;
13372 pr_warn("failed to allocate events: out of memory\n");
13373 goto error;
13374 }
13375 pb->cpu_bufs = calloc(pb->cpu_cnt, sizeof(*pb->cpu_bufs));
13376 if (!pb->cpu_bufs) {
13377 err = -ENOMEM;
13378 pr_warn("failed to allocate buffers: out of memory\n");
13379 goto error;
13380 }
13381
13382 err = parse_cpu_mask_file(online_cpus_file, &online, &n);
13383 if (err) {
13384 pr_warn("failed to get online CPU mask: %s\n", errstr(err));
13385 goto error;
13386 }
13387
13388 for (i = 0, j = 0; i < pb->cpu_cnt; i++) {
13389 struct perf_cpu_buf *cpu_buf;
13390 int cpu, map_key;
13391
13392 cpu = p->cpu_cnt > 0 ? p->cpus[i] : i;
13393 map_key = p->cpu_cnt > 0 ? p->map_keys[i] : i;
13394
13395 /* in case user didn't explicitly requested particular CPUs to
13396 * be attached to, skip offline/not present CPUs
13397 */
13398 if (p->cpu_cnt <= 0 && (cpu >= n || !online[cpu]))
13399 continue;
13400
13401 cpu_buf = perf_buffer__open_cpu_buf(pb, p->attr, cpu, map_key);
13402 if (IS_ERR(cpu_buf)) {
13403 err = PTR_ERR(cpu_buf);
13404 goto error;
13405 }
13406
13407 pb->cpu_bufs[j] = cpu_buf;
13408
13409 err = bpf_map_update_elem(pb->map_fd, &map_key,
13410 &cpu_buf->fd, 0);
13411 if (err) {
13412 err = -errno;
13413 pr_warn("failed to set cpu #%d, key %d -> perf FD %d: %s\n",
13414 cpu, map_key, cpu_buf->fd,
13415 errstr(err));
13416 goto error;
13417 }
13418
13419 pb->events[j].events = EPOLLIN;
13420 pb->events[j].data.ptr = cpu_buf;
13421 if (epoll_ctl(pb->epoll_fd, EPOLL_CTL_ADD, cpu_buf->fd,
13422 &pb->events[j]) < 0) {
13423 err = -errno;
13424 pr_warn("failed to epoll_ctl cpu #%d perf FD %d: %s\n",
13425 cpu, cpu_buf->fd,
13426 errstr(err));
13427 goto error;
13428 }
13429 j++;
13430 }
13431 pb->cpu_cnt = j;
13432 free(online);
13433
13434 return pb;
13435
13436 error:
13437 free(online);
13438 if (pb)
13439 perf_buffer__free(pb);
13440 return ERR_PTR(err);
13441 }
13442
13443 struct perf_sample_raw {
13444 struct perf_event_header header;
13445 uint32_t size;
13446 char data[];
13447 };
13448
13449 struct perf_sample_lost {
13450 struct perf_event_header header;
13451 uint64_t id;
13452 uint64_t lost;
13453 uint64_t sample_id;
13454 };
13455
13456 static enum bpf_perf_event_ret
perf_buffer__process_record(struct perf_event_header * e,void * ctx)13457 perf_buffer__process_record(struct perf_event_header *e, void *ctx)
13458 {
13459 struct perf_cpu_buf *cpu_buf = ctx;
13460 struct perf_buffer *pb = cpu_buf->pb;
13461 void *data = e;
13462
13463 /* user wants full control over parsing perf event */
13464 if (pb->event_cb)
13465 return pb->event_cb(pb->ctx, cpu_buf->cpu, e);
13466
13467 switch (e->type) {
13468 case PERF_RECORD_SAMPLE: {
13469 struct perf_sample_raw *s = data;
13470
13471 if (pb->sample_cb)
13472 pb->sample_cb(pb->ctx, cpu_buf->cpu, s->data, s->size);
13473 break;
13474 }
13475 case PERF_RECORD_LOST: {
13476 struct perf_sample_lost *s = data;
13477
13478 if (pb->lost_cb)
13479 pb->lost_cb(pb->ctx, cpu_buf->cpu, s->lost);
13480 break;
13481 }
13482 default:
13483 pr_warn("unknown perf sample type %d\n", e->type);
13484 return LIBBPF_PERF_EVENT_ERROR;
13485 }
13486 return LIBBPF_PERF_EVENT_CONT;
13487 }
13488
perf_buffer__process_records(struct perf_buffer * pb,struct perf_cpu_buf * cpu_buf)13489 static int perf_buffer__process_records(struct perf_buffer *pb,
13490 struct perf_cpu_buf *cpu_buf)
13491 {
13492 enum bpf_perf_event_ret ret;
13493
13494 ret = perf_event_read_simple(cpu_buf->base, pb->mmap_size,
13495 pb->page_size, &cpu_buf->buf,
13496 &cpu_buf->buf_size,
13497 perf_buffer__process_record, cpu_buf);
13498 if (ret != LIBBPF_PERF_EVENT_CONT)
13499 return ret;
13500 return 0;
13501 }
13502
perf_buffer__epoll_fd(const struct perf_buffer * pb)13503 int perf_buffer__epoll_fd(const struct perf_buffer *pb)
13504 {
13505 return pb->epoll_fd;
13506 }
13507
perf_buffer__poll(struct perf_buffer * pb,int timeout_ms)13508 int perf_buffer__poll(struct perf_buffer *pb, int timeout_ms)
13509 {
13510 int i, cnt, err;
13511
13512 cnt = epoll_wait(pb->epoll_fd, pb->events, pb->cpu_cnt, timeout_ms);
13513 if (cnt < 0)
13514 return -errno;
13515
13516 for (i = 0; i < cnt; i++) {
13517 struct perf_cpu_buf *cpu_buf = pb->events[i].data.ptr;
13518
13519 err = perf_buffer__process_records(pb, cpu_buf);
13520 if (err) {
13521 pr_warn("error while processing records: %s\n", errstr(err));
13522 return libbpf_err(err);
13523 }
13524 }
13525 return cnt;
13526 }
13527
13528 /* Return number of PERF_EVENT_ARRAY map slots set up by this perf_buffer
13529 * manager.
13530 */
perf_buffer__buffer_cnt(const struct perf_buffer * pb)13531 size_t perf_buffer__buffer_cnt(const struct perf_buffer *pb)
13532 {
13533 return pb->cpu_cnt;
13534 }
13535
13536 /*
13537 * Return perf_event FD of a ring buffer in *buf_idx* slot of
13538 * PERF_EVENT_ARRAY BPF map. This FD can be polled for new data using
13539 * select()/poll()/epoll() Linux syscalls.
13540 */
perf_buffer__buffer_fd(const struct perf_buffer * pb,size_t buf_idx)13541 int perf_buffer__buffer_fd(const struct perf_buffer *pb, size_t buf_idx)
13542 {
13543 struct perf_cpu_buf *cpu_buf;
13544
13545 if (buf_idx >= pb->cpu_cnt)
13546 return libbpf_err(-EINVAL);
13547
13548 cpu_buf = pb->cpu_bufs[buf_idx];
13549 if (!cpu_buf)
13550 return libbpf_err(-ENOENT);
13551
13552 return cpu_buf->fd;
13553 }
13554
perf_buffer__buffer(struct perf_buffer * pb,int buf_idx,void ** buf,size_t * buf_size)13555 int perf_buffer__buffer(struct perf_buffer *pb, int buf_idx, void **buf, size_t *buf_size)
13556 {
13557 struct perf_cpu_buf *cpu_buf;
13558
13559 if (buf_idx >= pb->cpu_cnt)
13560 return libbpf_err(-EINVAL);
13561
13562 cpu_buf = pb->cpu_bufs[buf_idx];
13563 if (!cpu_buf)
13564 return libbpf_err(-ENOENT);
13565
13566 *buf = cpu_buf->base;
13567 *buf_size = pb->mmap_size;
13568 return 0;
13569 }
13570
13571 /*
13572 * Consume data from perf ring buffer corresponding to slot *buf_idx* in
13573 * PERF_EVENT_ARRAY BPF map without waiting/polling. If there is no data to
13574 * consume, do nothing and return success.
13575 * Returns:
13576 * - 0 on success;
13577 * - <0 on failure.
13578 */
perf_buffer__consume_buffer(struct perf_buffer * pb,size_t buf_idx)13579 int perf_buffer__consume_buffer(struct perf_buffer *pb, size_t buf_idx)
13580 {
13581 struct perf_cpu_buf *cpu_buf;
13582
13583 if (buf_idx >= pb->cpu_cnt)
13584 return libbpf_err(-EINVAL);
13585
13586 cpu_buf = pb->cpu_bufs[buf_idx];
13587 if (!cpu_buf)
13588 return libbpf_err(-ENOENT);
13589
13590 return perf_buffer__process_records(pb, cpu_buf);
13591 }
13592
perf_buffer__consume(struct perf_buffer * pb)13593 int perf_buffer__consume(struct perf_buffer *pb)
13594 {
13595 int i, err;
13596
13597 for (i = 0; i < pb->cpu_cnt; i++) {
13598 struct perf_cpu_buf *cpu_buf = pb->cpu_bufs[i];
13599
13600 if (!cpu_buf)
13601 continue;
13602
13603 err = perf_buffer__process_records(pb, cpu_buf);
13604 if (err) {
13605 pr_warn("perf_buffer: failed to process records in buffer #%d: %s\n",
13606 i, errstr(err));
13607 return libbpf_err(err);
13608 }
13609 }
13610 return 0;
13611 }
13612
bpf_program__set_attach_target(struct bpf_program * prog,int attach_prog_fd,const char * attach_func_name)13613 int bpf_program__set_attach_target(struct bpf_program *prog,
13614 int attach_prog_fd,
13615 const char *attach_func_name)
13616 {
13617 int btf_obj_fd = 0, btf_id = 0, err;
13618
13619 if (!prog || attach_prog_fd < 0)
13620 return libbpf_err(-EINVAL);
13621
13622 if (prog->obj->loaded)
13623 return libbpf_err(-EINVAL);
13624
13625 if (attach_prog_fd && !attach_func_name) {
13626 /* remember attach_prog_fd and let bpf_program__load() find
13627 * BTF ID during the program load
13628 */
13629 prog->attach_prog_fd = attach_prog_fd;
13630 return 0;
13631 }
13632
13633 if (attach_prog_fd) {
13634 btf_id = libbpf_find_prog_btf_id(attach_func_name,
13635 attach_prog_fd);
13636 if (btf_id < 0)
13637 return libbpf_err(btf_id);
13638 } else {
13639 if (!attach_func_name)
13640 return libbpf_err(-EINVAL);
13641
13642 /* load btf_vmlinux, if not yet */
13643 err = bpf_object__load_vmlinux_btf(prog->obj, true);
13644 if (err)
13645 return libbpf_err(err);
13646 err = find_kernel_btf_id(prog->obj, attach_func_name,
13647 prog->expected_attach_type,
13648 &btf_obj_fd, &btf_id);
13649 if (err)
13650 return libbpf_err(err);
13651 }
13652
13653 prog->attach_btf_id = btf_id;
13654 prog->attach_btf_obj_fd = btf_obj_fd;
13655 prog->attach_prog_fd = attach_prog_fd;
13656 return 0;
13657 }
13658
parse_cpu_mask_str(const char * s,bool ** mask,int * mask_sz)13659 int parse_cpu_mask_str(const char *s, bool **mask, int *mask_sz)
13660 {
13661 int err = 0, n, len, start, end = -1;
13662 bool *tmp;
13663
13664 *mask = NULL;
13665 *mask_sz = 0;
13666
13667 /* Each sub string separated by ',' has format \d+-\d+ or \d+ */
13668 while (*s) {
13669 if (*s == ',' || *s == '\n') {
13670 s++;
13671 continue;
13672 }
13673 n = sscanf(s, "%d%n-%d%n", &start, &len, &end, &len);
13674 if (n <= 0 || n > 2) {
13675 pr_warn("Failed to get CPU range %s: %d\n", s, n);
13676 err = -EINVAL;
13677 goto cleanup;
13678 } else if (n == 1) {
13679 end = start;
13680 }
13681 if (start < 0 || start > end) {
13682 pr_warn("Invalid CPU range [%d,%d] in %s\n",
13683 start, end, s);
13684 err = -EINVAL;
13685 goto cleanup;
13686 }
13687 tmp = realloc(*mask, end + 1);
13688 if (!tmp) {
13689 err = -ENOMEM;
13690 goto cleanup;
13691 }
13692 *mask = tmp;
13693 memset(tmp + *mask_sz, 0, start - *mask_sz);
13694 memset(tmp + start, 1, end - start + 1);
13695 *mask_sz = end + 1;
13696 s += len;
13697 }
13698 if (!*mask_sz) {
13699 pr_warn("Empty CPU range\n");
13700 return -EINVAL;
13701 }
13702 return 0;
13703 cleanup:
13704 free(*mask);
13705 *mask = NULL;
13706 return err;
13707 }
13708
parse_cpu_mask_file(const char * fcpu,bool ** mask,int * mask_sz)13709 int parse_cpu_mask_file(const char *fcpu, bool **mask, int *mask_sz)
13710 {
13711 int fd, err = 0, len;
13712 char buf[128];
13713
13714 fd = open(fcpu, O_RDONLY | O_CLOEXEC);
13715 if (fd < 0) {
13716 err = -errno;
13717 pr_warn("Failed to open cpu mask file %s: %s\n", fcpu, errstr(err));
13718 return err;
13719 }
13720 len = read(fd, buf, sizeof(buf));
13721 close(fd);
13722 if (len <= 0) {
13723 err = len ? -errno : -EINVAL;
13724 pr_warn("Failed to read cpu mask from %s: %s\n", fcpu, errstr(err));
13725 return err;
13726 }
13727 if (len >= sizeof(buf)) {
13728 pr_warn("CPU mask is too big in file %s\n", fcpu);
13729 return -E2BIG;
13730 }
13731 buf[len] = '\0';
13732
13733 return parse_cpu_mask_str(buf, mask, mask_sz);
13734 }
13735
libbpf_num_possible_cpus(void)13736 int libbpf_num_possible_cpus(void)
13737 {
13738 static const char *fcpu = "/sys/devices/system/cpu/possible";
13739 static int cpus;
13740 int err, n, i, tmp_cpus;
13741 bool *mask;
13742
13743 tmp_cpus = READ_ONCE(cpus);
13744 if (tmp_cpus > 0)
13745 return tmp_cpus;
13746
13747 err = parse_cpu_mask_file(fcpu, &mask, &n);
13748 if (err)
13749 return libbpf_err(err);
13750
13751 tmp_cpus = 0;
13752 for (i = 0; i < n; i++) {
13753 if (mask[i])
13754 tmp_cpus++;
13755 }
13756 free(mask);
13757
13758 WRITE_ONCE(cpus, tmp_cpus);
13759 return tmp_cpus;
13760 }
13761
populate_skeleton_maps(const struct bpf_object * obj,struct bpf_map_skeleton * maps,size_t map_cnt,size_t map_skel_sz)13762 static int populate_skeleton_maps(const struct bpf_object *obj,
13763 struct bpf_map_skeleton *maps,
13764 size_t map_cnt, size_t map_skel_sz)
13765 {
13766 int i;
13767
13768 for (i = 0; i < map_cnt; i++) {
13769 struct bpf_map_skeleton *map_skel = (void *)maps + i * map_skel_sz;
13770 struct bpf_map **map = map_skel->map;
13771 const char *name = map_skel->name;
13772 void **mmaped = map_skel->mmaped;
13773
13774 *map = bpf_object__find_map_by_name(obj, name);
13775 if (!*map) {
13776 pr_warn("failed to find skeleton map '%s'\n", name);
13777 return -ESRCH;
13778 }
13779
13780 /* externs shouldn't be pre-setup from user code */
13781 if (mmaped && (*map)->libbpf_type != LIBBPF_MAP_KCONFIG)
13782 *mmaped = (*map)->mmaped;
13783 }
13784 return 0;
13785 }
13786
populate_skeleton_progs(const struct bpf_object * obj,struct bpf_prog_skeleton * progs,size_t prog_cnt,size_t prog_skel_sz)13787 static int populate_skeleton_progs(const struct bpf_object *obj,
13788 struct bpf_prog_skeleton *progs,
13789 size_t prog_cnt, size_t prog_skel_sz)
13790 {
13791 int i;
13792
13793 for (i = 0; i < prog_cnt; i++) {
13794 struct bpf_prog_skeleton *prog_skel = (void *)progs + i * prog_skel_sz;
13795 struct bpf_program **prog = prog_skel->prog;
13796 const char *name = prog_skel->name;
13797
13798 *prog = bpf_object__find_program_by_name(obj, name);
13799 if (!*prog) {
13800 pr_warn("failed to find skeleton program '%s'\n", name);
13801 return -ESRCH;
13802 }
13803 }
13804 return 0;
13805 }
13806
bpf_object__open_skeleton(struct bpf_object_skeleton * s,const struct bpf_object_open_opts * opts)13807 int bpf_object__open_skeleton(struct bpf_object_skeleton *s,
13808 const struct bpf_object_open_opts *opts)
13809 {
13810 struct bpf_object *obj;
13811 int err;
13812
13813 obj = bpf_object_open(NULL, s->data, s->data_sz, s->name, opts);
13814 if (IS_ERR(obj)) {
13815 err = PTR_ERR(obj);
13816 pr_warn("failed to initialize skeleton BPF object '%s': %s\n",
13817 s->name, errstr(err));
13818 return libbpf_err(err);
13819 }
13820
13821 *s->obj = obj;
13822 err = populate_skeleton_maps(obj, s->maps, s->map_cnt, s->map_skel_sz);
13823 if (err) {
13824 pr_warn("failed to populate skeleton maps for '%s': %s\n", s->name, errstr(err));
13825 return libbpf_err(err);
13826 }
13827
13828 err = populate_skeleton_progs(obj, s->progs, s->prog_cnt, s->prog_skel_sz);
13829 if (err) {
13830 pr_warn("failed to populate skeleton progs for '%s': %s\n", s->name, errstr(err));
13831 return libbpf_err(err);
13832 }
13833
13834 return 0;
13835 }
13836
bpf_object__open_subskeleton(struct bpf_object_subskeleton * s)13837 int bpf_object__open_subskeleton(struct bpf_object_subskeleton *s)
13838 {
13839 int err, len, var_idx, i;
13840 const char *var_name;
13841 const struct bpf_map *map;
13842 struct btf *btf;
13843 __u32 map_type_id;
13844 const struct btf_type *map_type, *var_type;
13845 const struct bpf_var_skeleton *var_skel;
13846 struct btf_var_secinfo *var;
13847
13848 if (!s->obj)
13849 return libbpf_err(-EINVAL);
13850
13851 btf = bpf_object__btf(s->obj);
13852 if (!btf) {
13853 pr_warn("subskeletons require BTF at runtime (object %s)\n",
13854 bpf_object__name(s->obj));
13855 return libbpf_err(-errno);
13856 }
13857
13858 err = populate_skeleton_maps(s->obj, s->maps, s->map_cnt, s->map_skel_sz);
13859 if (err) {
13860 pr_warn("failed to populate subskeleton maps: %s\n", errstr(err));
13861 return libbpf_err(err);
13862 }
13863
13864 err = populate_skeleton_progs(s->obj, s->progs, s->prog_cnt, s->prog_skel_sz);
13865 if (err) {
13866 pr_warn("failed to populate subskeleton maps: %s\n", errstr(err));
13867 return libbpf_err(err);
13868 }
13869
13870 for (var_idx = 0; var_idx < s->var_cnt; var_idx++) {
13871 var_skel = (void *)s->vars + var_idx * s->var_skel_sz;
13872 map = *var_skel->map;
13873 map_type_id = bpf_map__btf_value_type_id(map);
13874 map_type = btf__type_by_id(btf, map_type_id);
13875
13876 if (!btf_is_datasec(map_type)) {
13877 pr_warn("type for map '%1$s' is not a datasec: %2$s\n",
13878 bpf_map__name(map),
13879 __btf_kind_str(btf_kind(map_type)));
13880 return libbpf_err(-EINVAL);
13881 }
13882
13883 len = btf_vlen(map_type);
13884 var = btf_var_secinfos(map_type);
13885 for (i = 0; i < len; i++, var++) {
13886 var_type = btf__type_by_id(btf, var->type);
13887 var_name = btf__name_by_offset(btf, var_type->name_off);
13888 if (strcmp(var_name, var_skel->name) == 0) {
13889 *var_skel->addr = map->mmaped + var->offset;
13890 break;
13891 }
13892 }
13893 }
13894 return 0;
13895 }
13896
bpf_object__destroy_subskeleton(struct bpf_object_subskeleton * s)13897 void bpf_object__destroy_subskeleton(struct bpf_object_subskeleton *s)
13898 {
13899 if (!s)
13900 return;
13901 free(s->maps);
13902 free(s->progs);
13903 free(s->vars);
13904 free(s);
13905 }
13906
bpf_object__load_skeleton(struct bpf_object_skeleton * s)13907 int bpf_object__load_skeleton(struct bpf_object_skeleton *s)
13908 {
13909 int i, err;
13910
13911 err = bpf_object__load(*s->obj);
13912 if (err) {
13913 pr_warn("failed to load BPF skeleton '%s': %s\n", s->name, errstr(err));
13914 return libbpf_err(err);
13915 }
13916
13917 for (i = 0; i < s->map_cnt; i++) {
13918 struct bpf_map_skeleton *map_skel = (void *)s->maps + i * s->map_skel_sz;
13919 struct bpf_map *map = *map_skel->map;
13920
13921 if (!map_skel->mmaped)
13922 continue;
13923
13924 *map_skel->mmaped = map->mmaped;
13925 }
13926
13927 return 0;
13928 }
13929
bpf_object__attach_skeleton(struct bpf_object_skeleton * s)13930 int bpf_object__attach_skeleton(struct bpf_object_skeleton *s)
13931 {
13932 int i, err;
13933
13934 for (i = 0; i < s->prog_cnt; i++) {
13935 struct bpf_prog_skeleton *prog_skel = (void *)s->progs + i * s->prog_skel_sz;
13936 struct bpf_program *prog = *prog_skel->prog;
13937 struct bpf_link **link = prog_skel->link;
13938
13939 if (!prog->autoload || !prog->autoattach)
13940 continue;
13941
13942 /* auto-attaching not supported for this program */
13943 if (!prog->sec_def || !prog->sec_def->prog_attach_fn)
13944 continue;
13945
13946 /* if user already set the link manually, don't attempt auto-attach */
13947 if (*link)
13948 continue;
13949
13950 err = prog->sec_def->prog_attach_fn(prog, prog->sec_def->cookie, link);
13951 if (err) {
13952 pr_warn("prog '%s': failed to auto-attach: %s\n",
13953 bpf_program__name(prog), errstr(err));
13954 return libbpf_err(err);
13955 }
13956
13957 /* It's possible that for some SEC() definitions auto-attach
13958 * is supported in some cases (e.g., if definition completely
13959 * specifies target information), but is not in other cases.
13960 * SEC("uprobe") is one such case. If user specified target
13961 * binary and function name, such BPF program can be
13962 * auto-attached. But if not, it shouldn't trigger skeleton's
13963 * attach to fail. It should just be skipped.
13964 * attach_fn signals such case with returning 0 (no error) and
13965 * setting link to NULL.
13966 */
13967 }
13968
13969
13970 for (i = 0; i < s->map_cnt; i++) {
13971 struct bpf_map_skeleton *map_skel = (void *)s->maps + i * s->map_skel_sz;
13972 struct bpf_map *map = *map_skel->map;
13973 struct bpf_link **link;
13974
13975 if (!map->autocreate || !map->autoattach)
13976 continue;
13977
13978 /* only struct_ops maps can be attached */
13979 if (!bpf_map__is_struct_ops(map))
13980 continue;
13981
13982 /* skeleton is created with earlier version of bpftool, notify user */
13983 if (s->map_skel_sz < offsetofend(struct bpf_map_skeleton, link)) {
13984 pr_warn("map '%s': BPF skeleton version is old, skipping map auto-attachment...\n",
13985 bpf_map__name(map));
13986 continue;
13987 }
13988
13989 link = map_skel->link;
13990 if (*link)
13991 continue;
13992
13993 *link = bpf_map__attach_struct_ops(map);
13994 if (!*link) {
13995 err = -errno;
13996 pr_warn("map '%s': failed to auto-attach: %s\n",
13997 bpf_map__name(map), errstr(err));
13998 return libbpf_err(err);
13999 }
14000 }
14001
14002 return 0;
14003 }
14004
bpf_object__detach_skeleton(struct bpf_object_skeleton * s)14005 void bpf_object__detach_skeleton(struct bpf_object_skeleton *s)
14006 {
14007 int i;
14008
14009 for (i = 0; i < s->prog_cnt; i++) {
14010 struct bpf_prog_skeleton *prog_skel = (void *)s->progs + i * s->prog_skel_sz;
14011 struct bpf_link **link = prog_skel->link;
14012
14013 bpf_link__destroy(*link);
14014 *link = NULL;
14015 }
14016
14017 if (s->map_skel_sz < sizeof(struct bpf_map_skeleton))
14018 return;
14019
14020 for (i = 0; i < s->map_cnt; i++) {
14021 struct bpf_map_skeleton *map_skel = (void *)s->maps + i * s->map_skel_sz;
14022 struct bpf_link **link = map_skel->link;
14023
14024 if (link) {
14025 bpf_link__destroy(*link);
14026 *link = NULL;
14027 }
14028 }
14029 }
14030
bpf_object__destroy_skeleton(struct bpf_object_skeleton * s)14031 void bpf_object__destroy_skeleton(struct bpf_object_skeleton *s)
14032 {
14033 if (!s)
14034 return;
14035
14036 bpf_object__detach_skeleton(s);
14037 if (s->obj)
14038 bpf_object__close(*s->obj);
14039 free(s->maps);
14040 free(s->progs);
14041 free(s);
14042 }
14043