1 // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
4 * Common eBPF ELF object loading operations.
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.
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>
42 #include <sys/types.h>
44 #include <sys/utsname.h>
45 #include <sys/resource.h>
53 #include "str_error.h"
54 #include "libbpf_internal.h"
56 #include "bpf_gen_internal.h"
60 #define BPF_FS_MAGIC 0xcafe4a11
63 #define BPF_FS_DEFAULT_PATH "/sys/fs/bpf"
65 #define BPF_INSN_SZ (sizeof(struct bpf_insn))
67 /* vsprintf() in __base_pr() uses nonliteral format string. It may break
68 * compilation if user enables corresponding warning. Disable it explicitly.
70 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
72 #define __printf(a, b) __attribute__((format(printf, a, b)))
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
);
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",
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",
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",
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",
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",
230 static int __base_pr(enum libbpf_print_level level
, const char *format
,
233 const char *env_var
= "LIBBPF_LOG_LEVEL";
234 static enum libbpf_print_level min_level
= LIBBPF_INFO
;
235 static bool initialized
;
241 verbosity
= getenv(env_var
);
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
;
250 fprintf(stderr
, "libbpf: unrecognized '%s' envvar value: '%s', should be one of 'warn', 'debug', or 'info'.\n",
255 /* if too verbose, skip logging */
256 if (level
> min_level
)
259 return vfprintf(stderr
, format
, args
);
262 static libbpf_print_fn_t __libbpf_pr
= __base_pr
;
264 libbpf_print_fn_t
libbpf_set_print(libbpf_print_fn_t fn
)
266 libbpf_print_fn_t old_print_fn
;
268 old_print_fn
= __atomic_exchange_n(&__libbpf_pr
, fn
, __ATOMIC_RELAXED
);
274 void libbpf_print(enum libbpf_print_level level
, const char *format
, ...)
278 libbpf_print_fn_t print_fn
;
280 print_fn
= __atomic_load_n(&__libbpf_pr
, __ATOMIC_RELAXED
);
286 va_start(args
, format
);
287 __libbpf_pr(level
, format
, args
);
293 static void pr_perm_msg(int err
)
298 if (err
!= -EPERM
|| geteuid() != 0)
301 err
= getrlimit(RLIMIT_MEMLOCK
, &limit
);
305 if (limit
.rlim_cur
== RLIM_INFINITY
)
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);
313 snprintf(buf
, sizeof(buf
), "%.1f MiB", (double)limit
.rlim_cur
/ (1024*1024));
315 pr_warn("permission error while running as root; try raising 'ulimit -l'? current value: %s\n",
319 #define STRERR_BUFSIZE 128
321 /* Copied from tools/perf/util/util.h */
323 # define zfree(ptr) ({ free(*ptr); *ptr = NULL; })
327 # define zclose(fd) ({ \
330 ___err = close((fd)); \
335 static inline __u64
ptr_to_u64(const void *ptr
)
337 return (__u64
) (unsigned long) ptr
;
340 int libbpf_set_strict_mode(enum libbpf_strict_mode mode
)
342 /* as of v1.0 libbpf_set_strict_mode() is a no-op */
346 __u32
libbpf_major_version(void)
348 return LIBBPF_MAJOR_VERSION
;
351 __u32
libbpf_minor_version(void)
353 return LIBBPF_MINOR_VERSION
;
356 const char *libbpf_version_string(void)
360 return "v" _S(LIBBPF_MAJOR_VERSION
) "." _S(LIBBPF_MINOR_VERSION
);
376 enum reloc_type type
;
379 const struct bpf_core_relo
*core_relo
; /* used when type == RELO_CORE */
388 /* stored as sec_def->cookie for all libbpf-supported SEC()s */
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.
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
405 /* BPF program type allows sleeping/blocking in kernel */
407 /* BPF program support non-linear XDP buffer */
409 /* Setup proper attach type for usdt probes. */
415 enum bpf_prog_type prog_type
;
416 enum bpf_attach_type expected_attach_type
;
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
;
426 * bpf_prog should be a better name but it has been used in
433 const struct bpf_sec_def
*sec_def
;
434 /* this program's instruction offset (in number of instructions)
435 * within its containing ELF section
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
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.
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.
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
465 struct reloc_desc
*reloc_desc
;
468 /* BPF verifier log settings */
473 struct bpf_object
*obj
;
479 bool mark_btf_static
;
480 enum bpf_prog_type type
;
481 enum bpf_attach_type expected_attach_type
;
482 int exception_cb_idx
;
485 __u32 attach_btf_obj_fd
;
487 __u32 attach_prog_fd
;
490 __u32 func_info_rec_size
;
494 __u32 line_info_rec_size
;
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 */
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;
510 * kern_vdata-size == sizeof(struct bpf_struct_ops_tcp_congestion_ops)
511 * bpf_map__init_kern_struct_ops() will populate the "kern_vdata"
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"
527 enum libbpf_map_type
{
537 unsigned int key_size
;
538 unsigned int value_size
;
539 unsigned int max_entries
;
540 unsigned int map_flags
;
544 struct bpf_object
*obj
;
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.
557 struct bpf_map_def def
;
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
;
566 struct bpf_struct_ops
*st_ops
;
567 struct bpf_map
*inner_map
;
594 enum extern_type type
;
611 unsigned long long addr
;
613 /* target btf_id of the corresponding kernel var. */
614 int kernel_btf_obj_fd
;
617 /* local btf_id of the ksym extern's type. */
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
645 struct elf_sec_desc
{
646 enum sec_type sec_type
;
658 Elf_Data
*arena_data
;
659 size_t shstrndx
; /* section index for section name strings */
661 struct elf_sec_desc
*secs
;
664 __u32 btf_maps_sec_btf_id
;
668 int arena_data_shndx
;
674 char name
[BPF_OBJ_NAME_LEN
];
678 struct bpf_program
*programs
;
680 struct bpf_map
*maps
;
685 struct extern_desc
*externs
;
693 struct bpf_gen
*gen_loader
;
695 /* Information when doing ELF related work. Only valid if efile.elf is not NULL */
696 struct elf_state efile
;
698 unsigned char byteorder
;
701 struct btf_ext
*btf_ext
;
703 /* Parse and load BTF vmlinux if any of the programs in the object need
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.
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
;
719 /* optional log settings passed to BPF_BTF_LOAD and BPF_PROG_LOAD commands */
728 struct usdt_manager
*usdt_man
;
730 struct bpf_map
*arena_map
;
732 size_t arena_data_sz
;
734 struct kern_feature_cache
*feat_cache
;
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
);
751 void bpf_program__unload(struct bpf_program
*prog
)
758 zfree(&prog
->func_info
);
759 zfree(&prog
->line_info
);
762 static void bpf_program__exit(struct bpf_program
*prog
)
767 bpf_program__unload(prog
);
769 zfree(&prog
->sec_name
);
771 zfree(&prog
->reloc_desc
);
778 static bool insn_is_subprog_call(const struct bpf_insn
*insn
)
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 &&
788 static bool is_call_insn(const struct bpf_insn
*insn
)
790 return insn
->code
== (BPF_JMP
| BPF_CALL
);
793 static bool insn_is_pseudo_func(struct bpf_insn
*insn
)
795 return is_ldimm64_insn(insn
) && insn
->src_reg
== BPF_PSEUDO_FUNC
;
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
)
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
);
809 memset(prog
, 0, sizeof(*prog
));
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
;
818 prog
->type
= BPF_PROG_TYPE_UNSPEC
;
820 prog
->exception_cb_idx
= -1;
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.
826 if (sec_name
[0] == '?') {
827 prog
->autoload
= false;
828 /* from now on forget there was ? in section name */
831 prog
->autoload
= true;
834 prog
->autoattach
= true;
836 /* inherit object's log_level */
837 prog
->log_level
= obj
->log_level
;
839 prog
->sec_name
= strdup(sec_name
);
843 prog
->name
= strdup(name
);
847 prog
->insns
= malloc(insn_data_sz
);
850 memcpy(prog
->insns
, insn_data
, insn_data_sz
);
854 pr_warn("sec '%s': failed to allocate memory for prog '%s'\n", sec_name
, name
);
855 bpf_program__exit(prog
);
860 bpf_object__add_programs(struct bpf_object
*obj
, Elf_Data
*sec_data
,
861 const char *sec_name
, int sec_idx
)
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
;
871 progs
= obj
->programs
;
872 nr_progs
= obj
->nr_programs
;
873 nr_syms
= symbols
->d_size
/ sizeof(Elf64_Sym
);
875 for (i
= 0; i
< nr_syms
; i
++) {
876 sym
= elf_sym_by_idx(obj
, i
);
878 if (sym
->st_shndx
!= sec_idx
)
880 if (ELF64_ST_TYPE(sym
->st_info
) != STT_FUNC
)
883 prog_sz
= sym
->st_size
;
884 sec_off
= sym
->st_value
;
886 name
= elf_sym_str(obj
, sym
->st_name
);
888 pr_warn("sec '%s': failed to get symbol name for offset %zu\n",
890 return -LIBBPF_ERRNO__FORMAT
;
893 if (sec_off
+ prog_sz
> sec_sz
) {
894 pr_warn("sec '%s': program at offset %zu crosses section boundary\n",
896 return -LIBBPF_ERRNO__FORMAT
;
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
);
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
);
907 progs
= libbpf_reallocarray(progs
, nr_progs
+ 1, sizeof(*progs
));
910 * In this case the original obj->programs
911 * is still valid, so don't need special treat for
912 * bpf_close_object().
914 pr_warn("sec '%s': failed to alloc memory for new program '%s'\n",
918 obj
->programs
= progs
;
920 prog
= &progs
[nr_progs
];
922 err
= bpf_object__init_prog(obj
, prog
, name
, sec_idx
, sec_name
,
923 sec_off
, data
+ sec_off
, prog_sz
);
927 if (ELF64_ST_BIND(sym
->st_info
) != STB_LOCAL
)
928 prog
->sym_global
= true;
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
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;
940 obj
->nr_programs
= nr_progs
;
946 static void bpf_object_bswap_progs(struct bpf_object
*obj
)
948 struct bpf_program
*prog
= obj
->programs
;
949 struct bpf_insn
*insn
;
952 for (p
= 0; p
< obj
->nr_programs
; p
++, prog
++) {
954 for (i
= 0; i
< prog
->insns_cnt
; i
++, insn
++)
955 bpf_insn_bswap(insn
);
957 pr_debug("converted %zu BPF programs to native byte order\n", obj
->nr_programs
);
960 static const struct btf_member
*
961 find_member_by_offset(const struct btf_type
*t
, __u32 bit_offset
)
963 struct btf_member
*m
;
966 for (i
= 0, m
= btf_members(t
); i
< btf_vlen(t
); i
++, m
++) {
967 if (btf_member_bit_offset(t
, i
) == bit_offset
)
974 static const struct btf_member
*
975 find_member_by_name(const struct btf
*btf
, const struct btf_type
*t
,
978 struct btf_member
*m
;
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
))
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
);
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
);
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
)
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
;
1011 snprintf(tname
, sizeof(tname
), "%.*s",
1012 (int)bpf_core_essential_name_len(tname_raw
), tname_raw
);
1014 kern_type_id
= find_ksym_btf_id(obj
, tname
, BTF_KIND_STRUCT
,
1016 if (kern_type_id
< 0) {
1017 pr_warn("struct_ops init_kern: struct %s is not found in kernel BTF\n",
1019 return kern_type_id
;
1021 kern_type
= btf__type_by_id(btf
, kern_type_id
);
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
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
;
1035 kern_vtype
= btf__type_by_id(btf
, kern_vtype_id
);
1037 /* Find "struct tcp_congestion_ops" from
1038 * struct bpf_struct_ops_tcp_congestion_ops {
1040 * struct tcp_congestion_ops data;
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
)
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
);
1055 *type_id
= kern_type_id
;
1056 *vtype
= kern_vtype
;
1057 *vtype_id
= kern_vtype_id
;
1058 *data_member
= kern_data_member
;
1063 static bool bpf_map__is_struct_ops(const struct bpf_map
*map
)
1065 return map
->def
.type
== BPF_MAP_TYPE_STRUCT_OPS
;
1068 static bool is_valid_st_ops_program(struct bpf_object
*obj
,
1069 const struct bpf_program
*prog
)
1073 for (i
= 0; i
< obj
->nr_programs
; i
++) {
1074 if (&obj
->programs
[i
] == prog
)
1075 return prog
->type
== BPF_PROG_TYPE_STRUCT_OPS
;
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.
1086 static int bpf_object_adjust_struct_ops_autoload(struct bpf_object
*obj
)
1088 struct bpf_program
*prog
, *slot_prog
;
1089 struct bpf_map
*map
;
1092 for (i
= 0; i
< obj
->nr_programs
; ++i
) {
1093 int should_load
= false;
1096 prog
= &obj
->programs
[i
];
1097 if (prog
->type
!= BPF_PROG_TYPE_STRUCT_OPS
)
1100 for (j
= 0; j
< obj
->nr_maps
; ++j
) {
1101 const struct btf_type
*type
;
1103 map
= &obj
->maps
[j
];
1104 if (!bpf_map__is_struct_ops(map
))
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
)
1115 if (map
->autocreate
)
1120 prog
->autoload
= should_load
;
1126 /* Init the map's fields that depend on kern_btf */
1127 static int bpf_map__init_kern_struct_ops(struct bpf_map
*map
)
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
;
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
,
1151 kern_btf
= mod_btf
? mod_btf
->btf
: obj
->btf_vmlinux
;
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
);
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
;
1160 st_ops
->kern_vdata
= calloc(1, kern_vtype
->size
);
1161 if (!st_ops
->kern_vdata
)
1164 data
= st_ops
->data
;
1165 kern_data_off
= kern_data_member
->offset
/ 8;
1166 kern_data
= st_ops
->kern_vdata
+ kern_data_off
;
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
;
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
);
1184 pr_warn("struct_ops init_kern %s: failed to resolve the size of member %s\n",
1189 kern_member
= find_member_by_name(kern_btf
, kern_type
, mname
);
1191 if (!libbpf_is_mem_zeroed(mdata
, msize
)) {
1192 pr_warn("struct_ops init_kern %s: Cannot find member %s in kernel BTF\n",
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.
1205 st_ops
->progs
[i
]->autoload
= false;
1206 st_ops
->progs
[i
] = NULL
;
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",
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",
1223 kern_moff
= kern_member
->offset
/ 8;
1224 kern_mdata
= kern_data
+ kern_moff
;
1226 mtype
= skip_mods_and_typedefs(btf
, member
->type
, &mtype_id
);
1227 kern_mtype
= skip_mods_and_typedefs(kern_btf
, kern_member
->type
,
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
));
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
1243 if (st_ops
->progs
[i
] && st_ops
->progs
[i
] != prog
)
1244 st_ops
->progs
[i
]->autoload
= false;
1246 /* Update the value from the shadow type */
1247 st_ops
->progs
[i
] = prog
;
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",
1257 kern_mtype
= skip_mods_and_typedefs(kern_btf
,
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.
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",
1272 prog
->attach_btf_obj_fd
= mod_btf
->fd
;
1274 /* if we haven't yet processed this BPF program, record proper
1275 * attach_btf_id and member_idx
1277 if (!prog
->attach_btf_id
) {
1278 prog
->attach_btf_id
= kern_type_id
;
1279 prog
->expected_attach_type
= kern_member_idx
;
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
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
);
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
);
1300 st_ops
->kern_func_off
[i
] = kern_data_off
+ kern_moff
;
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
,
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
);
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
,
1320 memcpy(kern_mdata
, mdata
, msize
);
1326 static int bpf_object__init_kern_struct_ops_maps(struct bpf_object
*obj
)
1328 struct bpf_map
*map
;
1332 for (i
= 0; i
< obj
->nr_maps
; i
++) {
1333 map
= &obj
->maps
[i
];
1335 if (!bpf_map__is_struct_ops(map
))
1338 if (!map
->autocreate
)
1341 err
= bpf_map__init_kern_struct_ops(map
);
1349 static int init_struct_ops_maps(struct bpf_object
*obj
, const char *sec_name
,
1350 int shndx
, Elf_Data
*data
)
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
;
1365 datasec_id
= btf__find_by_name_kind(btf
, sec_name
,
1367 if (datasec_id
< 0) {
1368 pr_warn("struct_ops init: DATASEC %s not found\n",
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
);
1379 type_id
= btf__resolve_type(obj
->btf
, vsi
->type
);
1381 pr_warn("struct_ops init: Cannot resolve var type_id %u in DATASEC %s\n",
1382 vsi
->type
, sec_name
);
1386 type
= btf__type_by_id(obj
->btf
, type_id
);
1387 tname
= btf__name_by_offset(obj
->btf
, type
->name_off
);
1389 pr_warn("struct_ops init: anonymous type is not supported\n");
1392 if (!btf_is_struct(type
)) {
1393 pr_warn("struct_ops init: %s is not a struct\n", tname
);
1397 map
= bpf_object__add_map(obj
);
1399 return PTR_ERR(map
);
1401 map
->sec_idx
= shndx
;
1402 map
->sec_offset
= vsi
->offset
;
1403 map
->name
= strdup(var_name
);
1406 map
->btf_value_type_id
= type_id
;
1408 /* Follow same convention as for programs autoload:
1409 * SEC("?.struct_ops") means map is not created by default.
1411 if (sec_name
[0] == '?') {
1412 map
->autocreate
= false;
1413 /* from now on forget there was ? in section name */
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;
1424 map
->st_ops
= calloc(1, sizeof(*map
->st_ops
));
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
)
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
);
1441 memcpy(st_ops
->data
,
1442 data
->d_buf
+ vsi
->offset
,
1444 st_ops
->type_id
= type_id
;
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
);
1453 static int bpf_object_init_struct_ops(struct bpf_object
*obj
)
1455 const char *sec_name
;
1458 for (sec_idx
= 0; sec_idx
< obj
->efile
.sec_cnt
; ++sec_idx
) {
1459 struct elf_sec_desc
*desc
= &obj
->efile
.secs
[sec_idx
];
1461 if (desc
->sec_type
!= SEC_ST_OPS
)
1464 sec_name
= elf_sec_name(obj
, elf_sec_by_idx(obj
, sec_idx
));
1466 return -LIBBPF_ERRNO__FORMAT
;
1468 err
= init_struct_ops_maps(obj
, sec_name
, sec_idx
, desc
->data
);
1476 static struct bpf_object
*bpf_object__new(const char *path
,
1477 const void *obj_buf
,
1479 const char *obj_name
)
1481 struct bpf_object
*obj
;
1484 obj
= calloc(1, sizeof(struct bpf_object
) + strlen(path
) + 1);
1486 pr_warn("alloc memory failed for %s\n", path
);
1487 return ERR_PTR(-ENOMEM
);
1490 strcpy(obj
->path
, path
);
1492 libbpf_strlcpy(obj
->name
, obj_name
, sizeof(obj
->name
));
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
, '.');
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.
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;
1513 obj
->kern_version
= get_kernel_version();
1514 obj
->loaded
= false;
1519 static void bpf_object__elf_finish(struct bpf_object
*obj
)
1521 if (!obj
->efile
.elf
)
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
;
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;
1537 static int bpf_object__elf_init(struct bpf_object
*obj
)
1543 if (obj
->efile
.elf
) {
1544 pr_warn("elf: init internal error\n");
1545 return -LIBBPF_ERRNO__LIBELF
;
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
);
1552 obj
->efile
.fd
= open(obj
->path
, O_RDONLY
| O_CLOEXEC
);
1553 if (obj
->efile
.fd
< 0) {
1555 pr_warn("elf: failed to open %s: %s\n", obj
->path
, errstr(err
));
1559 elf
= elf_begin(obj
->efile
.fd
, ELF_C_READ_MMAP
, NULL
);
1563 pr_warn("elf: failed to open %s as ELF file: %s\n", obj
->path
, elf_errmsg(-1));
1564 err
= -LIBBPF_ERRNO__LIBELF
;
1568 obj
->efile
.elf
= elf
;
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
);
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
);
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
;
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
);
1596 /* and save after bpf_object_open() frees ELF data */
1597 obj
->byteorder
= ehdr
->e_ident
[EI_DATA
];
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
;
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
;
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
;
1623 bpf_object__elf_finish(obj
);
1627 static bool is_native_endianness(struct bpf_object
*obj
)
1629 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
1630 return obj
->byteorder
== ELFDATA2LSB
;
1631 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
1632 return obj
->byteorder
== ELFDATA2MSB
;
1634 # error "Unrecognized __BYTE_ORDER__"
1639 bpf_object__init_license(struct bpf_object
*obj
, void *data
, size_t size
)
1642 pr_warn("invalid license section in %s\n", obj
->path
);
1643 return -LIBBPF_ERRNO__FORMAT
;
1645 /* libbpf_strlcpy() only copies first N - 1 bytes, so size + 1 won't
1646 * go over allowed ELF data section buffer
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
);
1654 bpf_object__init_kversion(struct bpf_object
*obj
, void *data
, size_t size
)
1658 if (!data
|| size
!= sizeof(kver
)) {
1659 pr_warn("invalid kver section in %s\n", obj
->path
);
1660 return -LIBBPF_ERRNO__FORMAT
;
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
);
1668 static bool bpf_map_type__is_map_in_map(enum bpf_map_type type
)
1670 if (type
== BPF_MAP_TYPE_ARRAY_OF_MAPS
||
1671 type
== BPF_MAP_TYPE_HASH_OF_MAPS
)
1676 static int find_elf_sec_sz(const struct bpf_object
*obj
, const char *name
, __u32
*size
)
1684 scn
= elf_sec_by_name(obj
, name
);
1685 data
= elf_sec_data(obj
, scn
);
1687 *size
= data
->d_size
;
1688 return 0; /* found it */
1694 static Elf64_Sym
*find_elf_var_sym(const struct bpf_object
*obj
, const char *name
)
1696 Elf_Data
*symbols
= obj
->efile
.symbols
;
1700 for (si
= 0; si
< symbols
->d_size
/ sizeof(Elf64_Sym
); si
++) {
1701 Elf64_Sym
*sym
= elf_sym_by_idx(obj
, si
);
1703 if (ELF64_ST_TYPE(sym
->st_info
) != STT_OBJECT
)
1706 if (ELF64_ST_BIND(sym
->st_info
) != STB_GLOBAL
&&
1707 ELF64_ST_BIND(sym
->st_info
) != STB_WEAK
)
1710 sname
= elf_sym_str(obj
, sym
->st_name
);
1712 pr_warn("failed to get sym name string for var %s\n", name
);
1713 return ERR_PTR(-EIO
);
1715 if (strcmp(name
, sname
) == 0)
1719 return ERR_PTR(-ENOENT
);
1722 /* Some versions of Android don't provide memfd_create() in their libc
1723 * implementation, so avoid complications and just go straight to Linux
1726 static int sys_memfd_create(const char *name
, unsigned flags
)
1728 return syscall(__NR_memfd_create
, name
, flags
);
1732 #define MFD_CLOEXEC 0x0001U
1735 static int create_placeholder_fd(void)
1739 fd
= ensure_good_fd(sys_memfd_create("libbpf-placeholder-fd", MFD_CLOEXEC
));
1745 static struct bpf_map
*bpf_object__add_map(struct bpf_object
*obj
)
1747 struct bpf_map
*map
;
1750 err
= libbpf_ensure_mem((void **)&obj
->maps
, &obj
->maps_cap
,
1751 sizeof(*obj
->maps
), obj
->nr_maps
+ 1);
1753 return ERR_PTR(err
);
1755 map
= &obj
->maps
[obj
->nr_maps
++];
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.
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).
1769 map
->fd
= create_placeholder_fd();
1771 return ERR_PTR(map
->fd
);
1772 map
->inner_map_fd
= -1;
1773 map
->autocreate
= true;
1778 static size_t array_map_mmap_sz(unsigned int value_sz
, unsigned int max_entries
)
1780 const long page_sz
= sysconf(_SC_PAGE_SIZE
);
1783 map_sz
= (size_t)roundup(value_sz
, 8) * max_entries
;
1784 map_sz
= roundup(map_sz
, page_sz
);
1788 static size_t bpf_map_mmap_sz(const struct bpf_map
*map
)
1790 const long page_sz
= sysconf(_SC_PAGE_SIZE
);
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
;
1798 return 0; /* not supported */
1802 static int bpf_map_mmap_resize(struct bpf_map
*map
, size_t old_sz
, size_t new_sz
)
1809 if (old_sz
== new_sz
)
1812 mmaped
= mmap(NULL
, new_sz
, PROT_READ
| PROT_WRITE
, MAP_SHARED
| MAP_ANONYMOUS
, -1, 0);
1813 if (mmaped
== MAP_FAILED
)
1816 memcpy(mmaped
, map
->mmaped
, min(old_sz
, new_sz
));
1817 munmap(map
->mmaped
, old_sz
);
1818 map
->mmaped
= mmaped
;
1822 static char *internal_map_name(struct bpf_object
*obj
, const char *real_name
)
1824 char map_name
[BPF_OBJ_NAME_LEN
], *p
;
1825 int pfx_len
, sfx_len
= max((size_t)7, strlen(real_name
));
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.
1860 if (sfx_len
>= BPF_OBJ_NAME_LEN
)
1861 sfx_len
= BPF_OBJ_NAME_LEN
- 1;
1863 /* if there are two or more dots in map name, it's a custom dot map */
1864 if (strchr(real_name
+ 1, '.') != NULL
)
1867 pfx_len
= min((size_t)BPF_OBJ_NAME_LEN
- sfx_len
- 1, strlen(obj
->name
));
1869 snprintf(map_name
, sizeof(map_name
), "%.*s%.*s", pfx_len
, obj
->name
,
1870 sfx_len
, real_name
);
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
!= '.')
1877 return strdup(map_name
);
1881 map_fill_btf_type_info(struct bpf_object
*obj
, struct bpf_map
*map
);
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).
1888 static bool map_is_mmapable(struct bpf_object
*obj
, struct bpf_map
*map
)
1890 const struct btf_type
*t
, *vt
;
1891 struct btf_var_secinfo
*vsi
;
1894 if (!map
->btf_value_type_id
)
1897 t
= btf__type_by_id(obj
->btf
, map
->btf_value_type_id
);
1898 if (!btf_is_datasec(t
))
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
))
1907 if (btf_var(vt
)->linkage
!= BTF_VAR_STATIC
)
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
)
1918 struct bpf_map_def
*def
;
1919 struct bpf_map
*map
;
1923 map
= bpf_object__add_map(obj
);
1925 return PTR_ERR(map
);
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
);
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;
1946 /* failures are fine because of maps like .rodata.str1.1 */
1947 (void) map_fill_btf_type_info(obj
, map
);
1949 if (map_is_mmapable(obj
, map
))
1950 def
->map_flags
|= BPF_F_MMAPABLE
;
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
);
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
) {
1961 pr_warn("failed to alloc map '%s' content buffer: %s\n", map
->name
, errstr(err
));
1962 zfree(&map
->real_name
);
1968 memcpy(map
->mmaped
, data
, data_sz
);
1970 pr_debug("map %td is \"%s\"\n", map
- obj
->maps
, map
->name
);
1974 static int bpf_object__init_global_data_maps(struct bpf_object
*obj
)
1976 struct elf_sec_desc
*sec_desc
;
1977 const char *sec_name
;
1978 int err
= 0, sec_idx
;
1981 * Populate obj->maps with libbpf internal maps.
1983 for (sec_idx
= 1; sec_idx
< obj
->efile
.sec_cnt
; sec_idx
++) {
1984 sec_desc
= &obj
->efile
.secs
[sec_idx
];
1986 /* Skip recognized sections with size 0. */
1987 if (!sec_desc
->data
|| sec_desc
->data
->d_size
== 0)
1990 switch (sec_desc
->sec_type
) {
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
,
1995 sec_desc
->data
->d_buf
,
1996 sec_desc
->data
->d_size
);
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
,
2003 sec_desc
->data
->d_buf
,
2004 sec_desc
->data
->d_size
);
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
,
2011 sec_desc
->data
->d_size
);
2024 static struct extern_desc
*find_extern_by_name(const struct bpf_object
*obj
,
2029 for (i
= 0; i
< obj
->nr_extern
; i
++) {
2030 if (strcmp(obj
->externs
[i
].name
, name
) == 0)
2031 return &obj
->externs
[i
];
2036 static struct extern_desc
*find_extern_by_name_with_len(const struct bpf_object
*obj
,
2037 const void *name
, int len
)
2039 const char *ext_name
;
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
];
2050 static int set_kcfg_value_tri(struct extern_desc
*ext
, void *ext_val
,
2053 switch (ext
->kcfg
.type
) {
2056 pr_warn("extern (kcfg) '%s': value '%c' implies tristate or char type\n",
2060 *(bool *)ext_val
= value
== 'y' ? true : false;
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
;
2071 *(char *)ext_val
= value
;
2077 pr_warn("extern (kcfg) '%s': value '%c' implies bool, tristate, or char type\n",
2085 static int set_kcfg_value_str(struct extern_desc
*ext
, char *ext_val
,
2090 if (ext
->kcfg
.type
!= KCFG_CHAR_ARR
) {
2091 pr_warn("extern (kcfg) '%s': value '%s' implies char array type\n",
2096 len
= strlen(value
);
2097 if (value
[len
- 1] != '"') {
2098 pr_warn("extern (kcfg) '%s': invalid string config '%s'\n",
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;
2110 memcpy(ext_val
, value
+ 1, len
);
2111 ext_val
[len
] = '\0';
2116 static int parse_u64(const char *value
, __u64
*res
)
2122 *res
= strtoull(value
, &value_end
, 0);
2125 pr_warn("failed to parse '%s': %s\n", value
, errstr(err
));
2129 pr_warn("failed to parse '%s' as integer completely\n", value
);
2135 static bool is_kcfg_value_in_range(const struct extern_desc
*ext
, __u64 v
)
2137 int bit_sz
= ext
->kcfg
.sz
* 8;
2139 if (ext
->kcfg
.sz
== 8)
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:
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
2151 * For unsigned target integer, check that all the (64 - Y) bits are
2154 if (ext
->kcfg
.is_signed
)
2155 return v
+ (1ULL << (bit_sz
- 1)) < (1ULL << bit_sz
);
2157 return (v
>> bit_sz
) == 0;
2160 static int set_kcfg_value_num(struct extern_desc
*ext
, void *ext_val
,
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
);
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
);
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
);
2180 switch (ext
->kcfg
.sz
) {
2182 *(__u8
*)ext_val
= value
;
2185 *(__u16
*)ext_val
= value
;
2188 *(__u32
*)ext_val
= value
;
2191 *(__u64
*)ext_val
= value
;
2200 static int bpf_object__process_kconfig_line(struct bpf_object
*obj
,
2201 char *buf
, void *data
)
2203 struct extern_desc
*ext
;
2209 if (!str_has_pfx(buf
, "CONFIG_"))
2212 sep
= strchr(buf
, '=');
2214 pr_warn("failed to parse '%s': no separator\n", buf
);
2218 /* Trim ending '\n' */
2220 if (buf
[len
- 1] == '\n')
2221 buf
[len
- 1] = '\0';
2222 /* Split on '=' and ensure that a value is present. */
2226 pr_warn("failed to parse '%s': no value\n", buf
);
2230 ext
= find_extern_by_name(obj
, buf
);
2231 if (!ext
|| ext
->is_set
)
2234 ext_val
= data
+ ext
->kcfg
.data_off
;
2238 case 'y': case 'n': case 'm':
2239 err
= set_kcfg_value_tri(ext
, ext_val
, *value
);
2242 err
= set_kcfg_value_str(ext
, ext_val
, value
);
2245 /* assume integer */
2246 err
= parse_u64(value
, &num
);
2248 pr_warn("extern (kcfg) '%s': value '%s' isn't a valid integer\n", ext
->name
, value
);
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
);
2255 err
= set_kcfg_value_num(ext
, ext_val
, num
);
2260 pr_debug("extern (kcfg) '%s': set to %s\n", ext
->name
, value
);
2264 static int bpf_object__read_kconfig_file(struct bpf_object
*obj
, void *data
)
2272 len
= snprintf(buf
, PATH_MAX
, "/boot/config-%s", uts
.release
);
2275 else if (len
>= PATH_MAX
)
2276 return -ENAMETOOLONG
;
2278 /* gzopen also accepts uncompressed files. */
2279 file
= gzopen(buf
, "re");
2281 file
= gzopen("/proc/config.gz", "re");
2284 pr_warn("failed to open system Kconfig\n");
2288 while (gzgets(file
, buf
, sizeof(buf
))) {
2289 err
= bpf_object__process_kconfig_line(obj
, buf
, data
);
2291 pr_warn("error parsing system Kconfig line '%s': %s\n",
2302 static int bpf_object__read_kconfig_mem(struct bpf_object
*obj
,
2303 const char *config
, void *data
)
2309 file
= fmemopen((void *)config
, strlen(config
), "r");
2312 pr_warn("failed to open in-memory Kconfig: %s\n", errstr(err
));
2316 while (fgets(buf
, sizeof(buf
), file
)) {
2317 err
= bpf_object__process_kconfig_line(obj
, buf
, data
);
2319 pr_warn("error parsing in-memory Kconfig line '%s': %s\n",
2329 static int bpf_object__init_kconfig_map(struct bpf_object
*obj
)
2331 struct extern_desc
*last_ext
= NULL
, *ext
;
2335 for (i
= 0; i
< obj
->nr_extern
; i
++) {
2336 ext
= &obj
->externs
[i
];
2337 if (ext
->type
== EXT_KCFG
)
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
,
2351 obj
->kconfig_map_idx
= obj
->nr_maps
- 1;
2356 const struct btf_type
*
2357 skip_mods_and_typedefs(const struct btf
*btf
, __u32 id
, __u32
*res_id
)
2359 const struct btf_type
*t
= btf__type_by_id(btf
, id
);
2364 while (btf_is_mod(t
) || btf_is_typedef(t
)) {
2367 t
= btf__type_by_id(btf
, t
->type
);
2373 static const struct btf_type
*
2374 resolve_func_ptr(const struct btf
*btf
, __u32 id
, __u32
*res_id
)
2376 const struct btf_type
*t
;
2378 t
= skip_mods_and_typedefs(btf
, id
, NULL
);
2382 t
= skip_mods_and_typedefs(btf
, t
->type
, res_id
);
2384 return btf_is_func_proto(t
) ? t
: NULL
;
2387 static const char *__btf_kind_str(__u16 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";
2414 const char *btf_kind_str(const struct btf_type
*t
)
2416 return __btf_kind_str(btf_kind(t
));
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.
2426 static bool get_map_field_int(const char *map_name
, const struct btf
*btf
,
2427 const struct btf_member
*m
, __u32
*res
)
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
;
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
));
2440 arr_t
= btf__type_by_id(btf
, t
->type
);
2442 pr_warn("map '%s': attr '%s': type [%u] not found.\n",
2443 map_name
, name
, t
->type
);
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
));
2451 arr_info
= btf_array(arr_t
);
2452 *res
= arr_info
->nelems
;
2456 static bool get_map_field_long(const char *map_name
, const struct btf
*btf
,
2457 const struct btf_member
*m
, __u64
*res
)
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
);
2462 if (btf_is_ptr(t
)) {
2466 ret
= get_map_field_int(map_name
, btf
, m
, &res32
);
2468 *res
= (__u64
)res32
;
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
));
2478 if (btf_vlen(t
) != 1) {
2479 pr_warn("map '%s': attr '%s': invalid __ulong\n",
2484 if (btf_is_enum(t
)) {
2485 const struct btf_enum
*e
= btf_enum(t
);
2489 const struct btf_enum64
*e
= btf_enum64(t
);
2491 *res
= btf_enum64_value(e
);
2496 static int pathname_concat(char *buf
, size_t buf_sz
, const char *path
, const char *name
)
2500 len
= snprintf(buf
, buf_sz
, "%s/%s", path
, name
);
2504 return -ENAMETOOLONG
;
2509 static int build_map_pin_path(struct bpf_map
*map
, const char *path
)
2515 path
= BPF_FS_DEFAULT_PATH
;
2517 err
= pathname_concat(buf
, sizeof(buf
), path
, bpf_map__name(map
));
2521 return bpf_map__set_pin_path(map
, buf
);
2524 /* should match definition in bpf_helpers.h */
2525 enum libbpf_pin_type
{
2527 /* PIN_BY_NAME: pin maps by name (in /sys/fs/bpf by default) */
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
)
2535 const struct btf_type
*t
;
2536 const struct btf_member
*m
;
2537 bool is_inner
= inner_def
== NULL
;
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
);
2546 pr_warn("map '%s': invalid field #%d.\n", map_name
, i
);
2549 if (strcmp(name
, "type") == 0) {
2550 if (!get_map_field_int(map_name
, btf
, m
, &map_def
->map_type
))
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
))
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
))
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
))
2564 map_def
->parts
|= MAP_DEF_NUMA_NODE
;
2565 } else if (strcmp(name
, "key_size") == 0) {
2568 if (!get_map_field_int(map_name
, btf
, m
, &sz
))
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
);
2575 map_def
->key_size
= sz
;
2576 map_def
->parts
|= MAP_DEF_KEY_SIZE
;
2577 } else if (strcmp(name
, "key") == 0) {
2580 t
= btf__type_by_id(btf
, m
->type
);
2582 pr_warn("map '%s': key type [%d] not found.\n",
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
));
2591 sz
= btf__resolve_size(btf
, t
->type
);
2593 pr_warn("map '%s': can't determine key size for type [%u]: %zd.\n",
2594 map_name
, t
->type
, (ssize_t
)sz
);
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
);
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) {
2608 if (!get_map_field_int(map_name
, btf
, m
, &sz
))
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
);
2615 map_def
->value_size
= sz
;
2616 map_def
->parts
|= MAP_DEF_VALUE_SIZE
;
2617 } else if (strcmp(name
, "value") == 0) {
2620 t
= btf__type_by_id(btf
, m
->type
);
2622 pr_warn("map '%s': value type [%d] not found.\n",
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
));
2631 sz
= btf__resolve_size(btf
, t
->type
);
2633 pr_warn("map '%s': can't determine value size for type [%u]: %zd.\n",
2634 map_name
, t
->type
, (ssize_t
)sz
);
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
);
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
;
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];
2654 pr_warn("map '%s': multi-level inner maps not supported.\n",
2658 if (i
!= vlen
- 1) {
2659 pr_warn("map '%s': '%s' member should be last.\n",
2663 if (!is_map_in_map
&& !is_prog_array
) {
2664 pr_warn("map '%s': should be map-in-map or prog-array.\n",
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
);
2673 map_def
->value_size
= 4;
2674 t
= btf__type_by_id(btf
, m
->type
);
2676 pr_warn("map '%s': %s type [%d] not found.\n",
2677 map_name
, desc
, m
->type
);
2680 if (!btf_is_array(t
) || btf_array(t
)->nelems
) {
2681 pr_warn("map '%s': %s spec is not a zero-sized array.\n",
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
));
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
));
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
));
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
);
2711 map_def
->parts
|= MAP_DEF_INNER_MAP
;
2712 } else if (strcmp(name
, "pinning") == 0) {
2716 pr_warn("map '%s': inner def can't be pinned.\n", map_name
);
2719 if (!get_map_field_int(map_name
, btf
, m
, &val
))
2721 if (val
!= LIBBPF_PIN_NONE
&& val
!= LIBBPF_PIN_BY_NAME
) {
2722 pr_warn("map '%s': invalid pinning value %u.\n",
2726 map_def
->pinning
= val
;
2727 map_def
->parts
|= MAP_DEF_PINNING
;
2728 } else if (strcmp(name
, "map_extra") == 0) {
2731 if (!get_map_field_long(map_name
, btf
, m
, &map_extra
))
2733 map_def
->map_extra
= map_extra
;
2734 map_def
->parts
|= MAP_DEF_MAP_EXTRA
;
2737 pr_warn("map '%s': unknown field '%s'.\n", map_name
, name
);
2740 pr_debug("map '%s': ignoring unknown field '%s'.\n", map_name
, name
);
2744 if (map_def
->map_type
== BPF_MAP_TYPE_UNSPEC
) {
2745 pr_warn("map '%s': map type isn't specified.\n", map_name
);
2752 static size_t adjust_ringbuf_sz(size_t sz
)
2754 __u32 page_sz
= sysconf(_SC_PAGE_SIZE
);
2757 /* if user forgot to set any size, make sure they see error */
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.
2764 if ((sz
% page_sz
) == 0 && is_pow_of_2(sz
/ page_sz
))
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.
2771 for (mul
= 1; mul
<= UINT_MAX
/ page_sz
; mul
<<= 1) {
2772 if (mul
* page_sz
> sz
)
2773 return mul
* page_sz
;
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
2783 static bool map_is_ringbuf(const struct bpf_map
*map
)
2785 return map
->def
.type
== BPF_MAP_TYPE_RINGBUF
||
2786 map
->def
.type
== BPF_MAP_TYPE_USER_RINGBUF
;
2789 static void fill_map_from_def(struct bpf_map
*map
, const struct btf_map_def
*def
)
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
;
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
;
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
);
2806 if (def
->parts
& MAP_DEF_MAP_TYPE
)
2807 pr_debug("map '%s': found type = %u.\n", map
->name
, def
->map_type
);
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
);
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
);
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
);
2833 if (def
->parts
& MAP_DEF_INNER_MAP
)
2834 pr_debug("map '%s': found inner map definition.\n", map
->name
);
2837 static const char *btf_var_linkage_str(__u32 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";
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
)
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
;
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
);
2866 if (map_name
== NULL
|| map_name
[0] == '\0') {
2867 pr_warn("map #%d: empty name.\n", var_idx
);
2870 if ((__u64
)vi
->offset
+ vi
->size
> data
->d_size
) {
2871 pr_warn("map '%s' BTF data is corrupted.\n", map_name
);
2874 if (!btf_is_var(var
)) {
2875 pr_warn("map '%s': unexpected var kind %s.\n",
2876 map_name
, btf_kind_str(var
));
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
));
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
));
2891 if (def
->size
> vi
->size
) {
2892 pr_warn("map '%s': invalid def size.\n", map_name
);
2896 map
= bpf_object__add_map(obj
);
2898 return PTR_ERR(map
);
2899 map
->name
= strdup(map_name
);
2901 pr_warn("map '%s': failed to alloc map name.\n", map_name
);
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
);
2912 err
= parse_btf_map_def(map
->name
, obj
->btf
, def
, strict
, &map_def
, &inner_def
);
2916 fill_map_from_def(map
, &map_def
);
2918 if (map_def
.pinning
== LIBBPF_PIN_BY_NAME
) {
2919 err
= build_map_pin_path(map
, pin_root_path
);
2921 pr_warn("map '%s': couldn't build pin path.\n", map
->name
);
2926 if (map_def
.parts
& MAP_DEF_INNER_MAP
) {
2927 map
->inner_map
= calloc(1, sizeof(*map
->inner_map
));
2928 if (!map
->inner_map
)
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
)
2937 sprintf(map
->inner_map
->name
, "%s.inner", map_name
);
2939 fill_map_from_def(map
->inner_map
, &inner_def
);
2942 err
= map_fill_btf_type_info(obj
, map
);
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
)
2953 const long page_sz
= sysconf(_SC_PAGE_SIZE
);
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
);
2963 obj
->arena_data
= malloc(data_sz
);
2964 if (!obj
->arena_data
)
2966 memcpy(obj
->arena_data
, data
, data_sz
);
2967 obj
->arena_data_sz
= data_sz
;
2969 /* make bpf_map__init_value() work for ARENA maps */
2970 map
->mmaped
= obj
->arena_data
;
2975 static int bpf_object__init_user_btf_maps(struct bpf_object
*obj
, bool strict
,
2976 const char *pin_root_path
)
2978 const struct btf_type
*sec
= NULL
;
2979 int nr_types
, i
, vlen
, err
;
2980 const struct btf_type
*t
;
2985 if (obj
->efile
.btf_maps_shndx
< 0)
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
);
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
))
3001 name
= btf__name_by_offset(obj
->btf
, t
->name_off
);
3002 if (strcmp(name
, MAPS_ELF_SEC
) == 0) {
3004 obj
->efile
.btf_maps_sec_btf_id
= i
;
3010 pr_warn("DATASEC '%s' not found.\n", MAPS_ELF_SEC
);
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
,
3024 for (i
= 0; i
< obj
->nr_maps
; i
++) {
3025 struct bpf_map
*map
= &obj
->maps
[i
];
3027 if (map
->def
.type
!= BPF_MAP_TYPE_ARENA
)
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
);
3035 obj
->arena_map
= map
;
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
);
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",
3054 static int bpf_object__init_maps(struct bpf_object
*obj
,
3055 const struct bpf_object_open_opts
*opts
)
3057 const char *pin_root_path
;
3061 strict
= !OPTS_GET(opts
, relaxed_maps
, false);
3062 pin_root_path
= OPTS_GET(opts
, pin_root_path
, NULL
);
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
);
3072 static bool section_have_execinstr(struct bpf_object
*obj
, int idx
)
3076 sh
= elf_sec_hdr(obj
, elf_sec_by_idx(obj
, idx
));
3080 return sh
->sh_flags
& SHF_EXECINSTR
;
3083 static bool starts_with_qmark(const char *s
)
3085 return s
&& s
[0] == '?';
3088 static bool btf_needs_sanitization(struct bpf_object
*obj
)
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
);
3099 return !has_func
|| !has_datasec
|| !has_func_global
|| !has_float
||
3100 !has_decl_tag
|| !has_type_tag
|| !has_enum64
|| !has_qmark_datasec
;
3103 static int bpf_object__sanitize_btf(struct bpf_object
*obj
, struct btf
*btf
)
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;
3117 for (i
= 1; i
< btf__type_cnt(btf
); i
++) {
3118 t
= (struct btf_type
*)btf__type_by_id(btf
, i
);
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);
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
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
;
3137 name
= (char *)btf__name_by_offset(btf
, t
->name_off
);
3139 if (*name
== '.' || *name
== '?')
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;
3150 /* preserve variable name as member name */
3151 vt
= (void *)btf__type_by_id(btf
, v
->type
);
3152 m
->name_off
= vt
->name_off
;
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 */
3159 name
= (char *)btf__name_by_offset(btf
, t
->name_off
);
3162 } else if (!has_func
&& btf_is_func_proto(t
)) {
3163 /* replace FUNC_PROTO with ENUM */
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
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 */
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
;
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
;
3196 t
= (struct btf_type
*)btf__type_by_id(btf
, i
);
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
;
3212 static bool libbpf_needs_btf(const struct bpf_object
*obj
)
3214 return obj
->efile
.btf_maps_shndx
>= 0 ||
3215 obj
->efile
.has_st_ops
||
3219 static bool kernel_needs_btf(const struct bpf_object
*obj
)
3221 return obj
->efile
.has_st_ops
;
3224 static int bpf_object__init_btf(struct bpf_object
*obj
,
3226 Elf_Data
*btf_ext_data
)
3231 obj
->btf
= btf__new(btf_data
->d_buf
, btf_data
->d_size
);
3232 err
= libbpf_get_error(obj
->btf
);
3235 pr_warn("Error loading ELF section %s: %s.\n", BTF_ELF_SEC
, errstr(err
));
3238 /* enforce 8-byte pointers for BPF-targeted BTFs */
3239 btf__set_pointer_size(obj
->btf
, 8);
3242 struct btf_ext_info
*ext_segs
[3];
3243 int seg_num
, sec_num
;
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
);
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
);
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
;
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
;
3269 if (seg
->sec_cnt
== 0)
3272 seg
->sec_idxs
= calloc(seg
->sec_cnt
, sizeof(*seg
->sec_idxs
));
3273 if (!seg
->sec_idxs
) {
3279 for_each_btf_ext_sec(seg
, sec
) {
3280 /* preventively increment index to avoid doing
3281 * this before every continue below
3285 sec_name
= btf__name_by_offset(obj
->btf
, sec
->sec_name_off
);
3286 if (str_is_empty(sec_name
))
3288 scn
= elf_sec_by_name(obj
, sec_name
);
3292 seg
->sec_idxs
[sec_num
- 1] = elf_ndxscn(scn
);
3297 if (err
&& libbpf_needs_btf(obj
)) {
3298 pr_warn("BTF is required, but is missing or corrupted.\n");
3304 static int compare_vsi_off(const void *_a
, const void *_b
)
3306 const struct btf_var_secinfo
*a
= _a
;
3307 const struct btf_var_secinfo
*b
= _b
;
3309 return a
->offset
- b
->offset
;
3312 static int btf_fixup_datasec(struct bpf_object
*obj
, struct btf
*btf
,
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;
3322 pr_debug("No name found in string section for DATASEC kind.\n");
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.
3332 if (strcmp(sec_name
, KCONFIG_SEC
) == 0 || strcmp(sec_name
, KSYMS_SEC
) == 0)
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.
3344 err
= find_elf_sec_sz(obj
, sec_name
, &size
);
3346 pr_debug("sec '%s': failed to determine size from ELF: size %u, err %s\n",
3347 sec_name
, size
, errstr(err
));
3352 fixup_offsets
= true;
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
;
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
);
3367 var
= btf_var(t_var
);
3368 if (var
->linkage
== BTF_VAR_STATIC
|| var
->linkage
== BTF_VAR_GLOBAL_EXTERN
)
3371 var_name
= btf__name_by_offset(btf
, t_var
->name_off
);
3373 pr_debug("sec '%s': failed to find name of DATASEC's member #%d\n",
3378 sym
= find_elf_var_sym(obj
, var_name
);
3380 pr_debug("sec '%s': failed to find ELF symbol for VAR '%s'\n",
3381 sec_name
, var_name
);
3386 vsi
->offset
= sym
->st_value
;
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
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
;
3401 qsort(btf_var_secinfos(t
), vars
, sizeof(*vsi
), compare_vsi_off
);
3405 static int bpf_object_fixup_btf(struct bpf_object
*obj
)
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
);
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.
3421 if (btf_is_datasec(t
)) {
3422 err
= btf_fixup_datasec(obj
, obj
->btf
, t
);
3431 static bool prog_needs_vmlinux_btf(struct bpf_program
*prog
)
3433 if (prog
->type
== BPF_PROG_TYPE_STRUCT_OPS
||
3434 prog
->type
== BPF_PROG_TYPE_LSM
)
3437 /* BPF_PROG_TYPE_TRACING programs which do not attach to other programs
3438 * also need vmlinux BTF
3440 if (prog
->type
== BPF_PROG_TYPE_TRACING
&& !prog
->attach_prog_fd
)
3446 static bool map_needs_vmlinux_btf(struct bpf_map
*map
)
3448 return bpf_map__is_struct_ops(map
);
3451 static bool obj_needs_vmlinux_btf(const struct bpf_object
*obj
)
3453 struct bpf_program
*prog
;
3454 struct bpf_map
*map
;
3457 /* CO-RE relocations need kernel BTF, only when btf_custom_path
3460 if (obj
->btf_ext
&& obj
->btf_ext
->core_relo_info
.len
&& !obj
->btf_custom_path
)
3463 /* Support for typed ksyms needs kernel BTF */
3464 for (i
= 0; i
< obj
->nr_extern
; i
++) {
3465 const struct extern_desc
*ext
;
3467 ext
= &obj
->externs
[i
];
3468 if (ext
->type
== EXT_KSYM
&& ext
->ksym
.type_id
)
3472 bpf_object__for_each_program(prog
, obj
) {
3473 if (!prog
->autoload
)
3475 if (prog_needs_vmlinux_btf(prog
))
3479 bpf_object__for_each_map(map
, obj
) {
3480 if (map_needs_vmlinux_btf(map
))
3487 static int bpf_object__load_vmlinux_btf(struct bpf_object
*obj
, bool force
)
3491 /* btf_vmlinux could be loaded earlier */
3492 if (obj
->btf_vmlinux
|| obj
->gen_loader
)
3495 if (!force
&& !obj_needs_vmlinux_btf(obj
))
3498 obj
->btf_vmlinux
= btf__load_vmlinux_btf();
3499 err
= libbpf_get_error(obj
->btf_vmlinux
);
3501 pr_warn("Error loading vmlinux BTF: %s\n", errstr(err
));
3502 obj
->btf_vmlinux
= NULL
;
3508 static int bpf_object__sanitize_and_load_btf(struct bpf_object
*obj
)
3510 struct btf
*kern_btf
= obj
->btf
;
3511 bool btf_mandatory
, sanitize
;
3517 if (!kernel_supports(obj
, FEAT_BTF
)) {
3518 if (kernel_needs_btf(obj
)) {
3522 pr_debug("Kernel doesn't support BTF, skipping uploading it.\n");
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.
3534 for (i
= 0; i
< obj
->nr_programs
; i
++) {
3535 struct bpf_program
*prog
= &obj
->programs
[i
];
3540 if (!prog
->mark_btf_static
|| !prog_is_subprog(obj
, prog
))
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
)
3549 name
= btf__str_by_offset(obj
->btf
, t
->name_off
);
3550 if (strcmp(name
, prog
->name
) != 0)
3553 t
->info
= btf_type_info(BTF_KIND_FUNC
, BTF_FUNC_STATIC
, 0);
3558 sanitize
= btf_needs_sanitization(obj
);
3560 const void *raw_data
;
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
);
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
);
3577 if (obj
->gen_loader
) {
3579 const void *raw_data
= btf__raw_data(kern_btf
, &raw_size
);
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.
3587 btf__set_fd(kern_btf
, 0);
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
);
3595 /* move fd to libbpf's BTF */
3596 btf__set_fd(obj
->btf
, btf__fd(kern_btf
));
3597 btf__set_fd(kern_btf
, -1);
3599 btf__free(kern_btf
);
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",
3608 pr_info("Error loading .BTF into kernel: %s. BTF is optional, ignoring.\n",
3616 static const char *elf_sym_str(const struct bpf_object
*obj
, size_t off
)
3620 name
= elf_strptr(obj
->efile
.elf
, obj
->efile
.strtabidx
, off
);
3622 pr_warn("elf: failed to get section name string at offset %zu from %s: %s\n",
3623 off
, obj
->path
, elf_errmsg(-1));
3630 static const char *elf_sec_str(const struct bpf_object
*obj
, size_t off
)
3634 name
= elf_strptr(obj
->efile
.elf
, obj
->efile
.shstrndx
, off
);
3636 pr_warn("elf: failed to get section name string at offset %zu from %s: %s\n",
3637 off
, obj
->path
, elf_errmsg(-1));
3644 static Elf_Scn
*elf_sec_by_idx(const struct bpf_object
*obj
, size_t idx
)
3648 scn
= elf_getscn(obj
->efile
.elf
, idx
);
3650 pr_warn("elf: failed to get section(%zu) from %s: %s\n",
3651 idx
, obj
->path
, elf_errmsg(-1));
3657 static Elf_Scn
*elf_sec_by_name(const struct bpf_object
*obj
, const char *name
)
3659 Elf_Scn
*scn
= NULL
;
3660 Elf
*elf
= obj
->efile
.elf
;
3661 const char *sec_name
;
3663 while ((scn
= elf_nextscn(elf
, scn
)) != NULL
) {
3664 sec_name
= elf_sec_name(obj
, scn
);
3668 if (strcmp(sec_name
, name
) != 0)
3676 static Elf64_Shdr
*elf_sec_hdr(const struct bpf_object
*obj
, Elf_Scn
*scn
)
3683 shdr
= elf64_getshdr(scn
);
3685 pr_warn("elf: failed to get section(%zu) header from %s: %s\n",
3686 elf_ndxscn(scn
), obj
->path
, elf_errmsg(-1));
3693 static const char *elf_sec_name(const struct bpf_object
*obj
, Elf_Scn
*scn
)
3701 sh
= elf_sec_hdr(obj
, scn
);
3705 name
= elf_sec_str(obj
, sh
->sh_name
);
3707 pr_warn("elf: failed to get section(%zu) name from %s: %s\n",
3708 elf_ndxscn(scn
), obj
->path
, elf_errmsg(-1));
3715 static Elf_Data
*elf_sec_data(const struct bpf_object
*obj
, Elf_Scn
*scn
)
3722 data
= elf_getdata(scn
, 0);
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));
3733 static Elf64_Sym
*elf_sym_by_idx(const struct bpf_object
*obj
, size_t idx
)
3735 if (idx
>= obj
->efile
.symbols
->d_size
/ sizeof(Elf64_Sym
))
3738 return (Elf64_Sym
*)obj
->efile
.symbols
->d_buf
+ idx
;
3741 static Elf64_Rel
*elf_rel_by_idx(Elf_Data
*data
, size_t idx
)
3743 if (idx
>= data
->d_size
/ sizeof(Elf64_Rel
))
3746 return (Elf64_Rel
*)data
->d_buf
+ idx
;
3749 static bool is_sec_name_dwarf(const char *name
)
3751 /* approximation, but the actual list is too long */
3752 return str_has_pfx(name
, ".debug_");
3755 static bool ignore_elf_section(Elf64_Shdr
*hdr
, const char *name
)
3757 /* no special handling of .strtab */
3758 if (hdr
->sh_type
== SHT_STRTAB
)
3761 /* ignore .llvm_addrsig section as well */
3762 if (hdr
->sh_type
== SHT_LLVM_ADDRSIG
)
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)
3770 /* DWARF sections */
3771 if (is_sec_name_dwarf(name
))
3774 if (str_has_pfx(name
, ".rel")) {
3775 name
+= sizeof(".rel") - 1;
3776 /* DWARF section relocations */
3777 if (is_sec_name_dwarf(name
))
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)
3789 static int cmp_progs(const void *_a
, const void *_b
)
3791 const struct bpf_program
*a
= _a
;
3792 const struct bpf_program
*b
= _b
;
3794 if (a
->sec_idx
!= b
->sec_idx
)
3795 return a
->sec_idx
< b
->sec_idx
? -1 : 1;
3797 /* sec_insn_off can't be the same within the section */
3798 return a
->sec_insn_off
< b
->sec_insn_off
? -1 : 1;
3801 static int bpf_object__elf_collect(struct bpf_object
*obj
)
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;
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
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
;
3823 obj
->efile
.secs
= calloc(obj
->efile
.sec_cnt
, sizeof(*obj
->efile
.secs
));
3824 if (!obj
->efile
.secs
)
3827 /* a bunch of ELF parsing functionality depends on processing symbols,
3828 * so do the first pass and find the symbol table
3831 while ((scn
= elf_nextscn(elf
, scn
)) != NULL
) {
3832 sh
= elf_sec_hdr(obj
, scn
);
3834 return -LIBBPF_ERRNO__FORMAT
;
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
;
3842 data
= elf_sec_data(obj
, scn
);
3844 return -LIBBPF_ERRNO__FORMAT
;
3846 idx
= elf_ndxscn(scn
);
3848 obj
->efile
.symbols
= data
;
3849 obj
->efile
.symbols_shndx
= idx
;
3850 obj
->efile
.strtabidx
= sh
->sh_link
;
3854 if (!obj
->efile
.symbols
) {
3855 pr_warn("elf: couldn't find symbol table in %s, stripped object file?\n",
3861 while ((scn
= elf_nextscn(elf
, scn
)) != NULL
) {
3862 idx
= elf_ndxscn(scn
);
3863 sec_desc
= &obj
->efile
.secs
[idx
];
3865 sh
= elf_sec_hdr(obj
, scn
);
3867 return -LIBBPF_ERRNO__FORMAT
;
3869 name
= elf_sec_str(obj
, sh
->sh_name
);
3871 return -LIBBPF_ERRNO__FORMAT
;
3873 if (ignore_elf_section(sh
, name
))
3876 data
= elf_sec_data(obj
, scn
);
3878 return -LIBBPF_ERRNO__FORMAT
;
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
,
3885 if (strcmp(name
, "license") == 0) {
3886 err
= bpf_object__init_license(obj
, data
->d_buf
, data
->d_size
);
3889 } else if (strcmp(name
, "version") == 0) {
3890 err
= bpf_object__init_kversion(obj
, data
->d_buf
, data
->d_size
);
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");
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
;
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
);
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
;
3937 pr_info("elf: skipping unrecognized data section(%d) %s\n",
3940 } else if (sh
->sh_type
== SHT_REL
) {
3941 int targ_sec_idx
= sh
->sh_info
; /* points to other section */
3943 if (sh
->sh_entsize
!= sizeof(Elf64_Rel
) ||
3944 targ_sec_idx
>= obj
->efile
.sec_cnt
)
3945 return -LIBBPF_ERRNO__FORMAT
;
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
)) ?: "<?>");
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
;
3969 pr_info("elf: skipping section(%d) %s (size %zu)\n", idx
, name
,
3970 (size_t)sh
->sh_size
);
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
;
3979 /* change BPF program insns to native endianness for introspection */
3980 if (!is_native_endianness(obj
))
3981 bpf_object_bswap_progs(obj
);
3983 /* sort BPF programs by section name and in-section instruction offset
3986 if (obj
->nr_programs
)
3987 qsort(obj
->programs
, obj
->nr_programs
, sizeof(*obj
->programs
), cmp_progs
);
3989 return bpf_object__init_btf(obj
, btf_data
, btf_ext_data
);
3992 static bool sym_is_extern(const Elf64_Sym
*sym
)
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
;
4001 static bool sym_is_subprog(const Elf64_Sym
*sym
, int text_shndx
)
4003 int bind
= ELF64_ST_BIND(sym
->st_info
);
4004 int type
= ELF64_ST_TYPE(sym
->st_info
);
4006 /* in .text section */
4007 if (sym
->st_shndx
!= text_shndx
)
4010 /* local function */
4011 if (bind
== STB_LOCAL
&& type
== STT_SECTION
)
4014 /* global function */
4015 return (bind
== STB_GLOBAL
|| bind
== STB_WEAK
) && type
== STT_FUNC
;
4018 static int find_extern_btf_id(const struct btf
*btf
, const char *ext_name
)
4020 const struct btf_type
*t
;
4027 n
= btf__type_cnt(btf
);
4028 for (i
= 1; i
< n
; i
++) {
4029 t
= btf__type_by_id(btf
, i
);
4031 if (!btf_is_var(t
) && !btf_is_func(t
))
4034 tname
= btf__name_by_offset(btf
, t
->name_off
);
4035 if (strcmp(tname
, ext_name
))
4038 if (btf_is_var(t
) &&
4039 btf_var(t
)->linkage
!= BTF_VAR_GLOBAL_EXTERN
)
4042 if (btf_is_func(t
) && btf_func_linkage(t
) != BTF_FUNC_EXTERN
)
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
;
4059 n
= btf__type_cnt(btf
);
4060 for (i
= 1; i
< n
; i
++) {
4061 t
= btf__type_by_id(btf
, i
);
4063 if (!btf_is_datasec(t
))
4066 vs
= btf_var_secinfos(t
);
4067 for (j
= 0; j
< btf_vlen(t
); j
++, vs
++) {
4068 if (vs
->type
== ext_btf_id
)
4076 static enum kcfg_type
find_kcfg_type(const struct btf
*btf
, int id
,
4079 const struct btf_type
*t
;
4082 t
= skip_mods_and_typedefs(btf
, id
, NULL
);
4083 name
= btf__name_by_offset(btf
, t
->name_off
);
4087 switch (btf_kind(t
)) {
4088 case BTF_KIND_INT
: {
4089 int enc
= btf_int_encoding(t
);
4091 if (enc
& BTF_INT_BOOL
)
4092 return t
->size
== 1 ? KCFG_BOOL
: KCFG_UNKNOWN
;
4094 *is_signed
= enc
& BTF_INT_SIGNED
;
4097 if (t
->size
< 1 || t
->size
> 8 || (t
->size
& (t
->size
- 1)))
4098 return KCFG_UNKNOWN
;
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
;
4118 return KCFG_UNKNOWN
;
4122 static int cmp_externs(const void *_a
, const void *_b
)
4124 const struct extern_desc
*a
= _a
;
4125 const struct extern_desc
*b
= _b
;
4127 if (a
->type
!= b
->type
)
4128 return a
->type
< b
->type
? -1 : 1;
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;
4139 /* resolve ties by name */
4140 return strcmp(a
->name
, b
->name
);
4143 static int find_int_btf_id(const struct btf
*btf
)
4145 const struct btf_type
*t
;
4148 n
= btf__type_cnt(btf
);
4149 for (i
= 1; i
< n
; i
++) {
4150 t
= btf__type_by_id(btf
, i
);
4152 if (btf_is_int(t
) && btf_int_bits(t
) == 32)
4159 static int add_dummy_ksym_var(struct btf
*btf
)
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
;
4168 sec_btf_id
= btf__find_by_name_kind(btf
, KSYMS_SEC
,
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
;
4178 vt
= btf__type_by_id(btf
, vs
->type
);
4179 if (btf_is_func(vt
))
4183 /* No func in ksyms sec. No need to add dummy var. */
4184 if (i
== btf_vlen(sec
))
4187 int_btf_id
= find_int_btf_id(btf
);
4188 dummy_var_btf_id
= btf__add_var(btf
,
4190 BTF_VAR_GLOBAL_ALLOCATED
,
4192 if (dummy_var_btf_id
< 0)
4193 pr_warn("cannot create a dummy_ksym var\n");
4195 return dummy_var_btf_id
;
4198 static int bpf_object__collect_externs(struct bpf_object
*obj
)
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
;
4209 if (!obj
->efile
.symbols
)
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
;
4217 dummy_var_btf_id
= add_dummy_ksym_var(obj
->btf
);
4218 if (dummy_var_btf_id
< 0)
4219 return dummy_var_btf_id
;
4221 n
= sh
->sh_size
/ sh
->sh_entsize
;
4222 pr_debug("looking for externs among %d symbols...\n", n
);
4224 for (i
= 0; i
< n
; i
++) {
4225 Elf64_Sym
*sym
= elf_sym_by_idx(obj
, i
);
4228 return -LIBBPF_ERRNO__FORMAT
;
4229 if (!sym_is_extern(sym
))
4231 ext_name
= elf_sym_str(obj
, sym
->st_name
);
4232 if (!ext_name
|| !ext_name
[0])
4236 ext
= libbpf_reallocarray(ext
, obj
->nr_extern
+ 1, sizeof(*ext
));
4240 ext
= &ext
[obj
->nr_extern
];
4241 memset(ext
, 0, sizeof(*ext
));
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
);
4250 t
= btf__type_by_id(obj
->btf
, ext
->btf_id
);
4251 ext
->name
= btf__name_by_offset(obj
->btf
, t
->name_off
);
4253 ext
->is_weak
= ELF64_ST_BIND(sym
->st_info
) == STB_WEAK
;
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
)
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
;
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
);
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
);
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
;
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
);
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
);
4298 } else if (strcmp(sec_name
, KSYMS_SEC
) == 0) {
4300 ext
->type
= EXT_KSYM
;
4301 skip_mods_and_typedefs(obj
->btf
, t
->type
,
4302 &ext
->ksym
.type_id
);
4304 pr_warn("unrecognized extern section '%s'\n", sec_name
);
4308 pr_debug("collected %d externs total\n", obj
->nr_extern
);
4310 if (!obj
->nr_extern
)
4313 /* sort externs by type, for kcfg ones also by (align, size, name) */
4314 qsort(obj
->externs
, obj
->nr_extern
, sizeof(*ext
), cmp_externs
);
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
4321 /* find existing 4-byte integer type in BTF to use for fake
4322 * extern variables in DATASEC
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.
4330 const struct btf_type
*dummy_var
;
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
)
4337 pr_debug("extern (ksym) #%d: symbol %d, name %s\n",
4338 i
, ext
->sym_idx
, ext
->name
);
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
;
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
);
4351 pr_warn("failed to find extern definition for BTF %s '%s'\n",
4352 btf_kind_str(vt
), ext_name
);
4355 if (btf_is_func(vt
)) {
4356 const struct btf_type
*func_proto
;
4357 struct btf_param
*param
;
4360 func_proto
= btf__type_by_id(obj
->btf
,
4362 param
= btf_params(func_proto
);
4363 /* Reuse the dummy_var string if the
4364 * func proto does not have param name.
4366 for (j
= 0; j
< btf_vlen(func_proto
); j
++)
4367 if (param
[j
].type
&& !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
;
4374 btf_var(vt
)->linkage
= BTF_VAR_GLOBAL_ALLOCATED
;
4375 vt
->type
= int_btf_id
;
4378 vs
->size
= sizeof(int);
4385 /* for kcfg externs calculate their offsets within a .kconfig map */
4387 for (i
= 0; i
< obj
->nr_extern
; i
++) {
4388 ext
= &obj
->externs
[i
];
4389 if (ext
->type
!= EXT_KCFG
)
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
);
4399 for (i
= 0; i
< n
; i
++) {
4400 struct btf_var_secinfo
*vs
= btf_var_secinfos(sec
) + i
;
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
);
4406 pr_warn("failed to find extern definition for BTF var '%s'\n",
4410 btf_var(t
)->linkage
= BTF_VAR_GLOBAL_ALLOCATED
;
4411 vs
->offset
= ext
->kcfg
.data_off
;
4417 static bool prog_is_subprog(const struct bpf_object
*obj
, const struct bpf_program
*prog
)
4419 return prog
->sec_idx
== obj
->efile
.text_shndx
;
4422 struct bpf_program
*
4423 bpf_object__find_program_by_name(const struct bpf_object
*obj
,
4426 struct bpf_program
*prog
;
4428 bpf_object__for_each_program(prog
, obj
) {
4429 if (prog_is_subprog(obj
, prog
))
4431 if (!strcmp(prog
->name
, name
))
4434 return errno
= ENOENT
, NULL
;
4437 static bool bpf_object__shndx_is_data(const struct bpf_object
*obj
,
4440 switch (obj
->efile
.secs
[shndx
].sec_type
) {
4450 static bool bpf_object__shndx_is_maps(const struct bpf_object
*obj
,
4453 return shndx
== obj
->efile
.btf_maps_shndx
;
4456 static enum libbpf_map_type
4457 bpf_object__section_to_libbpf_map_type(const struct bpf_object
*obj
, int shndx
)
4459 if (shndx
== obj
->efile
.symbols_shndx
)
4460 return LIBBPF_MAP_KCONFIG
;
4462 switch (obj
->efile
.secs
[shndx
].sec_type
) {
4464 return LIBBPF_MAP_BSS
;
4466 return LIBBPF_MAP_DATA
;
4468 return LIBBPF_MAP_RODATA
;
4470 return LIBBPF_MAP_UNSPEC
;
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
)
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
;
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
;
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
;
4498 for (i
= 0; i
< n
; i
++) {
4499 ext
= &obj
->externs
[i
];
4500 if (ext
->sym_idx
== sym_idx
)
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
;
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
;
4513 reloc_desc
->type
= RELO_EXTERN_LD64
;
4514 reloc_desc
->insn_idx
= insn_idx
;
4515 reloc_desc
->ext_idx
= i
;
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
;
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
;
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
;
4537 reloc_desc
->type
= RELO_CALL
;
4538 reloc_desc
->insn_idx
= insn_idx
;
4539 reloc_desc
->sym_off
= sym
->st_value
;
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
;
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.
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
;
4560 reloc_desc
->type
= RELO_SUBPROG_ADDR
;
4561 reloc_desc
->insn_idx
= insn_idx
;
4562 reloc_desc
->sym_off
= sym
->st_value
;
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
));
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
;
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
;
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
)
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
);
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
;
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 */
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
;
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
)
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
);
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
;
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
;
4636 static bool prog_contains_insn(const struct bpf_program
*prog
, size_t insn_idx
)
4638 return insn_idx
>= prog
->sec_insn_off
&&
4639 insn_idx
< prog
->sec_insn_off
+ prog
->sec_insn_cnt
;
4642 static struct bpf_program
*find_prog_by_sec_insn(const struct bpf_object
*obj
,
4643 size_t sec_idx
, size_t insn_idx
)
4645 int l
= 0, r
= obj
->nr_programs
- 1, m
;
4646 struct bpf_program
*prog
;
4648 if (!obj
->nr_programs
)
4652 m
= l
+ (r
- l
+ 1) / 2;
4653 prog
= &obj
->programs
[m
];
4655 if (prog
->sec_idx
< sec_idx
||
4656 (prog
->sec_idx
== sec_idx
&& prog
->sec_insn_off
<= insn_idx
))
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
4664 prog
= &obj
->programs
[l
];
4665 if (prog
->sec_idx
== sec_idx
&& prog_contains_insn(prog
, insn_idx
))
4671 bpf_object__collect_prog_relos(struct bpf_object
*obj
, Elf64_Shdr
*shdr
, Elf_Data
*data
)
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
;
4678 const char *sym_name
;
4685 if (sec_idx
>= obj
->efile
.sec_cnt
)
4688 scn
= elf_sec_by_idx(obj
, sec_idx
);
4689 scn_data
= elf_sec_data(obj
, scn
);
4691 return -LIBBPF_ERRNO__FORMAT
;
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
)
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
;
4702 for (i
= 0; i
< nrels
; i
++) {
4703 rel
= elf_rel_by_idx(data
, i
);
4705 pr_warn("sec '%s': failed to get relo #%d\n", relo_sec_name
, i
);
4706 return -LIBBPF_ERRNO__FORMAT
;
4709 sym_idx
= ELF64_R_SYM(rel
->r_info
);
4710 sym
= elf_sym_by_idx(obj
, sym_idx
);
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
;
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
;
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
;
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
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
));
4739 sym_name
= elf_sym_str(obj
, sym
->st_name
);
4740 sym_name
= sym_name
?: "<?";
4742 pr_debug("sec '%s': relo #%d: insn #%u against '%s'\n",
4743 relo_sec_name
, i
, insn_idx
, sym_name
);
4745 prog
= find_prog_by_sec_insn(obj
, sec_idx
, insn_idx
);
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
);
4752 relos
= libbpf_reallocarray(prog
->reloc_desc
,
4753 prog
->nr_reloc
+ 1, sizeof(*relos
));
4756 prog
->reloc_desc
= relos
;
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
);
4770 static int map_fill_btf_type_info(struct bpf_object
*obj
, struct bpf_map
*map
)
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.
4781 if (map
->sec_idx
== obj
->efile
.btf_maps_shndx
|| bpf_map__is_struct_ops(map
))
4785 * LLVM annotates global data differently in BTF, that is,
4786 * only as '.data', '.bss' or '.rodata'.
4788 if (!bpf_map__is_internal(map
))
4791 id
= btf__find_by_name(obj
->btf
, map
->real_name
);
4795 map
->btf_key_type_id
= 0;
4796 map
->btf_value_type_id
= id
;
4800 static int bpf_get_map_info_from_fdinfo(int fd
, struct bpf_map_info
*info
)
4802 char file
[PATH_MAX
], buff
[4096];
4807 snprintf(file
, sizeof(file
), "/proc/%d/fdinfo/%d", getpid(), fd
);
4808 memset(info
, 0, sizeof(*info
));
4810 fp
= fopen(file
, "re");
4813 pr_warn("failed to open %s: %s. No procfs support?\n", file
,
4818 while (fgets(buff
, sizeof(buff
), fp
)) {
4819 if (sscanf(buff
, "map_type:\t%u", &val
) == 1)
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
;
4836 bool bpf_map__autocreate(const struct bpf_map
*map
)
4838 return map
->autocreate
;
4841 int bpf_map__set_autocreate(struct bpf_map
*map
, bool autocreate
)
4843 if (map
->obj
->loaded
)
4844 return libbpf_err(-EBUSY
);
4846 map
->autocreate
= autocreate
;
4850 int bpf_map__set_autoattach(struct bpf_map
*map
, bool autoattach
)
4852 if (!bpf_map__is_struct_ops(map
))
4853 return libbpf_err(-EINVAL
);
4855 map
->autoattach
= autoattach
;
4859 bool bpf_map__autoattach(const struct bpf_map
*map
)
4861 return map
->autoattach
;
4864 int bpf_map__reuse_fd(struct bpf_map
*map
, int fd
)
4866 struct bpf_map_info info
;
4867 __u32 len
= sizeof(info
), name_len
;
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
);
4876 return libbpf_err(err
);
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
);
4882 new_name
= strdup(info
.name
);
4885 return libbpf_err(-errno
);
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.
4892 new_fd
= fcntl(fd
, F_DUPFD_CLOEXEC
, 3);
4895 goto err_free_new_name
;
4898 err
= reuse_fd(map
->fd
, new_fd
);
4900 goto err_free_new_name
;
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
;
4913 map
->map_extra
= info
.map_extra
;
4919 return libbpf_err(err
);
4922 __u32
bpf_map__max_entries(const struct bpf_map
*map
)
4924 return map
->def
.max_entries
;
4927 struct bpf_map
*bpf_map__inner_map(struct bpf_map
*map
)
4929 if (!bpf_map_type__is_map_in_map(map
->def
.type
))
4930 return errno
= EINVAL
, NULL
;
4932 return map
->inner_map
;
4935 int bpf_map__set_max_entries(struct bpf_map
*map
, __u32 max_entries
)
4937 if (map
->obj
->loaded
)
4938 return libbpf_err(-EBUSY
);
4940 map
->def
.max_entries
= max_entries
;
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
);
4949 static int bpf_object_prepare_token(struct bpf_object
*obj
)
4951 const char *bpffs_path
;
4952 int bpffs_fd
= -1, token_fd
, err
;
4954 enum libbpf_print_level level
;
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
);
4962 mandatory
= obj
->token_path
!= NULL
;
4963 level
= mandatory
? LIBBPF_WARN
: LIBBPF_DEBUG
;
4965 bpffs_path
= obj
->token_path
?: BPF_FS_DEFAULT_PATH
;
4966 bpffs_fd
= open(bpffs_path
, O_DIRECTORY
, O_RDWR
);
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;
4975 token_fd
= bpf_token_create(bpffs_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
);
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;
4989 obj
->feat_cache
= calloc(1, sizeof(*obj
->feat_cache
));
4990 if (!obj
->feat_cache
) {
4995 obj
->token_fd
= token_fd
;
4996 obj
->feat_cache
->token_fd
= token_fd
;
5002 bpf_object__probe_loading(struct bpf_object
*obj
)
5004 struct bpf_insn insns
[] = {
5005 BPF_MOV64_IMM(BPF_REG_0
, 0),
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,
5014 if (obj
->gen_loader
)
5017 ret
= bump_rlimit_memlock();
5019 pr_warn("Failed to bump RLIMIT_MEMLOCK (err = %s), you might need to do it explicitly!\n",
5022 /* make sure basic loading works */
5023 ret
= bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER
, NULL
, "GPL", insns
, insn_cnt
, &opts
);
5025 ret
= bpf_prog_load(BPF_PROG_TYPE_TRACEPOINT
, NULL
, "GPL", insns
, insn_cnt
, &opts
);
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
));
5037 bool kernel_supports(const struct bpf_object
*obj
, enum kern_feature_id feat_id
)
5039 if (obj
->gen_loader
)
5040 /* To generate loader program assume the latest kernel
5041 * to avoid doing extra prog_load, map_create syscalls.
5046 return feat_supported(obj
->feat_cache
, feat_id
);
5048 return feat_supported(NULL
, feat_id
);
5051 static bool map_is_reuse_compat(const struct bpf_map
*map
, int map_fd
)
5053 struct bpf_map_info map_info
;
5054 __u32 map_info_len
= sizeof(map_info
);
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
);
5062 pr_warn("failed to get map info for map FD %d: %s\n", map_fd
,
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
);
5076 bpf_object__reuse_map(struct bpf_map
*map
)
5080 pin_fd
= bpf_obj_get(map
->pin_path
);
5083 if (err
== -ENOENT
) {
5084 pr_debug("found no pinned map to reuse at '%s'\n",
5089 pr_warn("couldn't retrieve pinned map '%s': %s\n",
5090 map
->pin_path
, errstr(err
));
5094 if (!map_is_reuse_compat(map
, pin_fd
)) {
5095 pr_warn("couldn't reuse pinned map at '%s': parameter mismatch\n",
5101 err
= bpf_map__reuse_fd(map
, pin_fd
);
5107 pr_debug("reused pinned map at '%s'\n", map
->pin_path
);
5113 bpf_object__populate_internal_map(struct bpf_object
*obj
, struct bpf_map
*map
)
5115 enum libbpf_map_type map_type
= map
->libbpf_type
;
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
);
5127 err
= bpf_map_update_elem(map
->fd
, &zero
, map
->mmaped
, 0);
5130 pr_warn("map '%s': failed to set initial contents: %s\n",
5131 bpf_map__name(map
), errstr(err
));
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
);
5140 pr_warn("map '%s': failed to freeze as read-only: %s\n",
5141 bpf_map__name(map
), errstr(err
));
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.
5155 mmap_sz
= bpf_map_mmap_sz(map
);
5156 if (map
->def
.map_flags
& BPF_F_MMAPABLE
) {
5160 if (map
->def
.map_flags
& BPF_F_RDONLY_PROG
)
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
) {
5167 pr_warn("map '%s': failed to re-mmap() contents: %s\n",
5168 bpf_map__name(map
), errstr(err
));
5171 map
->mmaped
= mmaped
;
5172 } else if (map
->mmaped
) {
5173 munmap(map
->mmaped
, mmap_sz
);
5180 static void bpf_map__destroy(struct bpf_map
*map
);
5182 static bool map_is_created(const struct bpf_map
*map
)
5184 return map
->obj
->loaded
|| map
->reused
;
5187 static int bpf_object__create_map(struct bpf_object
*obj
, struct bpf_map
*map
, bool is_inner
)
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
;
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
;
5202 create_attr
.map_flags
|= BPF_F_TOKEN_FD
;
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
;
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
;
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
);
5223 err
= bpf_object__create_map(obj
, map
->inner_map
, true);
5225 pr_warn("map '%s': failed to create inner map: %s\n",
5226 map
->name
, errstr(err
));
5229 map
->inner_map_fd
= map
->inner_map
->fd
;
5231 if (map
->inner_map_fd
>= 0)
5232 create_attr
.inner_map_fd
= map
->inner_map_fd
;
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;
5256 case BPF_MAP_TYPE_STRUCT_OPS
:
5257 create_attr
.btf_value_type_id
= 0;
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.
5275 map_fd
= bpf_map_create(def
->type
, map_name
,
5276 def
->key_size
, def
->value_size
,
5277 def
->max_entries
, &create_attr
);
5279 if (map_fd
< 0 && (create_attr
.btf_key_type_id
|| create_attr
.btf_value_type_id
)) {
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
);
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
);
5303 /* obj->gen_loader case, prevent reuse_fd() from closing map_fd */
5304 if (map
->fd
== map_fd
)
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.
5312 return reuse_fd(map
->fd
, map_fd
);
5315 static int init_map_in_map_slots(struct bpf_object
*obj
, struct bpf_map
*map
)
5317 const struct bpf_map
*targ_map
;
5321 for (i
= 0; i
< map
->init_slots_sz
; i
++) {
5322 if (!map
->init_slots
[i
])
5325 targ_map
= map
->init_slots
[i
];
5328 if (obj
->gen_loader
) {
5329 bpf_gen__populate_outer_map(obj
->gen_loader
,
5331 targ_map
- obj
->maps
);
5333 err
= bpf_map_update_elem(map
->fd
, &i
, &fd
, 0);
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
));
5341 pr_debug("map '%s': slot [%d] set to map '%s' fd=%d\n",
5342 map
->name
, i
, targ_map
->name
, fd
);
5345 zfree(&map
->init_slots
);
5346 map
->init_slots_sz
= 0;
5351 static int init_prog_array_slots(struct bpf_object
*obj
, struct bpf_map
*map
)
5353 const struct bpf_program
*targ_prog
;
5357 if (obj
->gen_loader
)
5360 for (i
= 0; i
< map
->init_slots_sz
; i
++) {
5361 if (!map
->init_slots
[i
])
5364 targ_prog
= map
->init_slots
[i
];
5365 fd
= bpf_program__fd(targ_prog
);
5367 err
= bpf_map_update_elem(map
->fd
, &i
, &fd
, 0);
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
));
5374 pr_debug("map '%s': slot [%d] set to prog '%s' fd=%d\n",
5375 map
->name
, i
, targ_prog
->name
, fd
);
5378 zfree(&map
->init_slots
);
5379 map
->init_slots_sz
= 0;
5384 static int bpf_object_init_prog_arrays(struct bpf_object
*obj
)
5386 struct bpf_map
*map
;
5389 for (i
= 0; i
< obj
->nr_maps
; i
++) {
5390 map
= &obj
->maps
[i
];
5392 if (!map
->init_slots_sz
|| map
->def
.type
!= BPF_MAP_TYPE_PROG_ARRAY
)
5395 err
= init_prog_array_slots(obj
, map
);
5402 static int map_set_def_max_entries(struct bpf_map
*map
)
5404 if (map
->def
.type
== BPF_MAP_TYPE_PERF_EVENT_ARRAY
&& !map
->def
.max_entries
) {
5407 nr_cpus
= libbpf_num_possible_cpus();
5409 pr_warn("map '%s': failed to determine number of system CPUs: %d\n",
5410 map
->name
, nr_cpus
);
5413 pr_debug("map '%s': setting size to %d\n", map
->name
, nr_cpus
);
5414 map
->def
.max_entries
= nr_cpus
;
5421 bpf_object__create_maps(struct bpf_object
*obj
)
5423 struct bpf_map
*map
;
5428 for (i
= 0; i
< obj
->nr_maps
; i
++) {
5429 map
= &obj
->maps
[i
];
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
5445 if (bpf_map__is_internal(map
) && !kernel_supports(obj
, FEAT_GLOBAL_DATA
))
5446 map
->autocreate
= false;
5448 if (!map
->autocreate
) {
5449 pr_debug("map '%s': skipped auto-creating...\n", map
->name
);
5453 err
= map_set_def_max_entries(map
);
5459 if (map
->pin_path
) {
5460 err
= bpf_object__reuse_map(map
);
5462 pr_warn("map '%s': error reusing pinned map\n",
5466 if (retried
&& map
->fd
< 0) {
5467 pr_warn("map '%s': cannot find pinned map\n",
5475 pr_debug("map '%s': skipping creation (preset fd=%d)\n",
5476 map
->name
, map
->fd
);
5478 err
= bpf_object__create_map(obj
, map
, false);
5482 pr_debug("map '%s': created successfully, fd=%d\n",
5483 map
->name
, map
->fd
);
5485 if (bpf_map__is_internal(map
)) {
5486 err
= bpf_object__populate_internal_map(obj
, map
);
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
,
5494 if (map
->mmaped
== MAP_FAILED
) {
5497 pr_warn("map '%s': failed to mmap arena: %s\n",
5498 map
->name
, errstr(err
));
5501 if (obj
->arena_data
) {
5502 memcpy(map
->mmaped
, obj
->arena_data
, obj
->arena_data_sz
);
5503 zfree(&obj
->arena_data
);
5506 if (map
->init_slots_sz
&& map
->def
.type
!= BPF_MAP_TYPE_PROG_ARRAY
) {
5507 err
= init_map_in_map_slots(obj
, map
);
5513 if (map
->pin_path
&& !map
->pinned
) {
5514 err
= bpf_map__pin(map
, NULL
);
5516 if (!retried
&& err
== -EEXIST
) {
5520 pr_warn("map '%s': failed to auto-pin at '%s': %s\n",
5521 map
->name
, map
->pin_path
, errstr(err
));
5530 pr_warn("map '%s': failed to create: %s\n", map
->name
, errstr(err
));
5532 for (j
= 0; j
< i
; j
++)
5533 zclose(obj
->maps
[j
].fd
);
5537 static bool bpf_core_is_flavor_sep(const char *s
)
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 */
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.
5549 size_t bpf_core_essential_name_len(const char *name
)
5551 size_t n
= strlen(name
);
5554 for (i
= n
- 5; i
>= 0; i
--) {
5555 if (bpf_core_is_flavor_sep(name
+ i
))
5561 void bpf_core_free_cands(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
,
5575 struct bpf_core_cand_list
*cands
)
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
;
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
);
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
))
5592 targ_name
= btf__name_by_offset(targ_btf
, t
->name_off
);
5593 if (str_is_empty(targ_name
))
5596 targ_essent_len
= bpf_core_essential_name_len(targ_name
);
5597 if (targ_essent_len
!= local_essent_len
)
5600 if (strncmp(local_name
, targ_name
, local_essent_len
) != 0)
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
,
5607 new_cands
= libbpf_reallocarray(cands
->cands
, cands
->len
+ 1,
5608 sizeof(*cands
->cands
));
5612 cand
= &new_cands
[cands
->len
];
5613 cand
->btf
= targ_btf
;
5616 cands
->cands
= new_cands
;
5622 static int load_module_btfs(struct bpf_object
*obj
)
5624 struct bpf_btf_info info
;
5625 struct module_btf
*mod_btf
;
5631 if (obj
->btf_modules_loaded
)
5634 if (obj
->gen_loader
)
5637 /* don't do this again, even if we find no module BTFs */
5638 obj
->btf_modules_loaded
= true;
5640 /* kernel too old to support module BTFs */
5641 if (!kernel_supports(obj
, FEAT_MODULE_BTF
))
5645 err
= bpf_btf_get_next_id(id
, &id
);
5646 if (err
&& errno
== ENOENT
)
5648 if (err
&& errno
== EPERM
) {
5649 pr_debug("skipping module BTFs loading, missing privileges\n");
5654 pr_warn("failed to iterate BTF objects: %s\n", errstr(err
));
5658 fd
= bpf_btf_get_fd_by_id(id
);
5660 if (errno
== ENOENT
)
5661 continue; /* expected race: BTF was unloaded */
5663 pr_warn("failed to get BTF object #%d FD: %s\n", id
, errstr(err
));
5668 memset(&info
, 0, sizeof(info
));
5669 info
.name
= ptr_to_u64(name
);
5670 info
.name_len
= sizeof(name
);
5672 err
= bpf_btf_get_info_by_fd(fd
, &info
, &len
);
5675 pr_warn("failed to get BTF object #%d info: %s\n", id
, errstr(err
));
5679 /* ignore non-module BTFs */
5680 if (!info
.kernel_btf
|| strcmp(name
, "vmlinux") == 0) {
5685 btf
= btf_get_from_fd(fd
, obj
->btf_vmlinux
);
5686 err
= libbpf_get_error(btf
);
5688 pr_warn("failed to load module [%s]'s BTF object #%d: %s\n",
5689 name
, id
, errstr(err
));
5693 err
= libbpf_ensure_mem((void **)&obj
->btf_modules
, &obj
->btf_module_cap
,
5694 sizeof(*obj
->btf_modules
), obj
->btf_module_cnt
+ 1);
5698 mod_btf
= &obj
->btf_modules
[obj
->btf_module_cnt
++];
5703 mod_btf
->name
= strdup(name
);
5704 if (!mod_btf
->name
) {
5718 static struct bpf_core_cand_list
*
5719 bpf_core_find_cands(struct bpf_object
*obj
, const struct btf
*local_btf
, __u32 local_type_id
)
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
;
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
);
5733 return ERR_PTR(-EINVAL
);
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
);
5740 cands
= calloc(1, sizeof(*cands
));
5742 return ERR_PTR(-ENOMEM
);
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
);
5750 /* if vmlinux BTF has any candidate, don't got for module BTFs */
5754 /* if vmlinux BTF was overridden, don't attempt to load module BTFs */
5755 if (obj
->btf_vmlinux_override
)
5758 /* now look through module BTFs, trying to still find candidates */
5759 err
= load_module_btfs(obj
);
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
),
5775 bpf_core_free_cands(cands
);
5776 return ERR_PTR(err
);
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.
5798 int bpf_core_types_are_compat(const struct btf
*local_btf
, __u32 local_id
,
5799 const struct btf
*targ_btf
, __u32 targ_id
)
5801 return __bpf_core_types_are_compat(local_btf
, local_id
, targ_btf
, targ_id
, 32);
5804 int bpf_core_types_match(const struct btf
*local_btf
, __u32 local_id
,
5805 const struct btf
*targ_btf
, __u32 targ_id
)
5807 return __bpf_core_types_match(local_btf
, local_id
, targ_btf
, targ_id
, false, 32);
5810 static size_t bpf_core_hash_fn(const long key
, void *ctx
)
5815 static bool bpf_core_equal_fn(const long k1
, const long k2
, void *ctx
)
5820 static int record_relo_core(struct bpf_program
*prog
,
5821 const struct bpf_core_relo
*core_relo
, int insn_idx
)
5823 struct reloc_desc
*relos
, *relo
;
5825 relos
= libbpf_reallocarray(prog
->reloc_desc
,
5826 prog
->nr_reloc
+ 1, sizeof(*relos
));
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
;
5838 static const struct bpf_core_relo
*find_relo_core(struct bpf_program
*prog
, int insn_idx
)
5840 struct reloc_desc
*relo
;
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
)
5848 return relo
->core_relo
;
5854 static int bpf_core_resolve_relo(struct bpf_program
*prog
,
5855 const struct bpf_core_relo
*relo
,
5857 const struct btf
*local_btf
,
5858 struct hashmap
*cand_cache
,
5859 struct bpf_core_relo_res
*targ_res
)
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
;
5869 local_type
= btf__type_by_id(local_btf
, local_id
);
5873 local_name
= btf__name_by_offset(local_btf
, local_type
->name_off
);
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
);
5886 err
= hashmap__set(cand_cache
, local_id
, cands
, NULL
, NULL
);
5888 bpf_core_free_cands(cands
);
5893 return bpf_core_calc_relo_insn(prog_name
, relo
, relo_idx
, local_btf
, cands
, specs_scratch
,
5898 bpf_object__relocate_core(struct bpf_object
*obj
, const char *targ_btf_path
)
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
;
5911 if (obj
->btf_ext
->core_relo_info
.len
== 0)
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
);
5918 pr_warn("failed to parse target BTF: %s\n", errstr(err
));
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
);
5929 seg
= &obj
->btf_ext
->core_relo_info
;
5931 for_each_btf_ext_sec(seg
, sec
) {
5932 sec_idx
= seg
->sec_idxs
[sec_num
];
5935 sec_name
= btf__name_by_offset(obj
->btf
, sec
->sec_name_off
);
5936 if (str_is_empty(sec_name
)) {
5941 pr_debug("sec '%s': found %d CO-RE relocations\n", sec_name
, sec
->num_info
);
5943 for_each_btf_ext_rec(seg
, sec
, i
, rec
) {
5944 if (rec
->insn_off
% BPF_INSN_SZ
)
5946 insn_idx
= rec
->insn_off
/ BPF_INSN_SZ
;
5947 prog
= find_prog_by_sec_insn(obj
, sec_idx
, insn_idx
);
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.
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
);
5961 /* no need to apply CO-RE relocation if the program is
5962 * not going to be loaded
5964 if (!prog
->autoload
)
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
5971 insn_idx
= insn_idx
- prog
->sec_insn_off
;
5972 if (insn_idx
>= prog
->insns_cnt
)
5974 insn
= &prog
->insns
[insn_idx
];
5976 err
= record_relo_core(prog
, rec
, insn_idx
);
5978 pr_warn("prog '%s': relo #%d: failed to record relocation: %s\n",
5979 prog
->name
, i
, errstr(err
));
5983 if (prog
->obj
->gen_loader
)
5986 err
= bpf_core_resolve_relo(prog
, rec
, i
, obj
->btf
, cand_cache
, &targ_res
);
5988 pr_warn("prog '%s': relo #%d: failed to relocate: %s\n",
5989 prog
->name
, i
, errstr(err
));
5993 err
= bpf_core_patch_insn(prog
->name
, insn
, insn_idx
, rec
, i
, &targ_res
);
5995 pr_warn("prog '%s': relo #%d: failed to patch insn #%u: %s\n",
5996 prog
->name
, i
, insn_idx
, errstr(err
));
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
;
6007 if (!IS_ERR_OR_NULL(cand_cache
)) {
6008 hashmap__for_each_entry(cand_cache
, entry
, i
) {
6009 bpf_core_free_cands(entry
->pvalue
);
6011 hashmap__free(cand_cache
);
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"
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
)
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
);
6029 /* we turn single ldimm64 into two identical invalid calls */
6030 for (i
= 0; i
< 2; i
++) {
6031 insn
->code
= BPF_JMP
| BPF_CALL
;
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
6040 insn
->imm
= POISON_LDIMM64_MAP_BASE
+ map_idx
;
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"
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
)
6054 pr_debug("prog '%s': relo #%d: poisoning insn #%d that calls kfunc '%s'\n",
6055 prog
->name
, relo_idx
, insn_idx
, ext
->name
);
6057 /* we turn kfunc call into invalid helper call with identifiable constant */
6058 insn
->code
= BPF_JMP
| BPF_CALL
;
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
6067 insn
->imm
= POISON_CALL_KFUNC_BASE
+ ext_idx
;
6070 /* Relocate data references within program code:
6072 * - global variable references;
6073 * - extern references.
6076 bpf_object__relocate_data(struct bpf_object
*obj
, struct bpf_program
*prog
)
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
;
6086 switch (relo
->type
) {
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
;
6096 poison_map_ldimm64(prog
, i
, relo
->insn_idx
, insn
,
6097 relo
->map_idx
, map
);
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
;
6110 poison_map_ldimm64(prog
, i
, relo
->insn_idx
, insn
,
6111 relo
->map_idx
, map
);
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
;
6121 insn
[0].src_reg
= BPF_PSEUDO_MAP_VALUE
;
6122 insn
[0].imm
= obj
->maps
[obj
->kconfig_map_idx
].fd
;
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;
6136 case RELO_EXTERN_CALL
:
6137 ext
= &obj
->externs
[relo
->ext_idx
];
6138 insn
[0].src_reg
= BPF_PSEUDO_KFUNC_CALL
;
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
);
6147 case RELO_SUBPROG_ADDR
:
6148 if (insn
[0].src_reg
!= BPF_PSEUDO_FUNC
) {
6149 pr_warn("prog '%s': relo #%d: bad insn\n",
6153 /* handled already */
6156 /* handled already */
6159 /* will be handled by bpf_program_record_relos() */
6162 pr_warn("prog '%s': relo #%d: bad relo type %d\n",
6163 prog
->name
, i
, relo
->type
);
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
,
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
;
6184 for_each_btf_ext_sec(ext_info
, sec
) {
6185 sec_idx
= ext_info
->sec_idxs
[sec_num
];
6187 if (prog
->sec_idx
!= sec_idx
)
6190 for_each_btf_ext_rec(ext_info
, sec
, i
, rec
) {
6191 __u32 insn_off
= *(__u32
*)rec
/ BPF_INSN_SZ
;
6193 if (insn_off
< prog
->sec_insn_off
)
6195 if (insn_off
>= prog
->sec_insn_off
+ prog
->sec_insn_cnt
)
6200 copy_end
= rec
+ ext_info
->rec_size
;
6206 /* append func/line info of a given (sub-)program to the main
6207 * program func/line info
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
);
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
);
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.
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
;
6230 *insn_off
= *insn_off
/ BPF_INSN_SZ
+ off_adj
;
6232 *prog_rec_sz
= ext_info
->rec_size
;
6240 reloc_prog_func_and_line_info(const struct bpf_object
*obj
,
6241 struct bpf_program
*main_prog
,
6242 const struct bpf_program
*prog
)
6246 /* no .BTF.ext relocation if .BTF.ext is missing or kernel doesn't
6247 * support func/line info
6249 if (!obj
->btf_ext
|| !kernel_supports(obj
, FEAT_BTF_FUNC
))
6252 /* only attempt func info relocation if main program's func_info
6253 * relocation was successful
6255 if (main_prog
!= prog
&& !main_prog
->func_info
)
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
);
6263 if (err
!= -ENOENT
) {
6264 pr_warn("prog '%s': error relocating .BTF.ext function info: %s\n",
6265 prog
->name
, errstr(err
));
6268 if (main_prog
->func_info
) {
6270 * Some info has already been found but has problem
6271 * in the last btf_ext reloc. Must have to error out.
6273 pr_warn("prog '%s': missing .BTF.ext function info.\n", prog
->name
);
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",
6282 /* don't relocate line info if main program's relocation failed */
6283 if (main_prog
!= prog
&& !main_prog
->line_info
)
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
);
6291 if (err
!= -ENOENT
) {
6292 pr_warn("prog '%s': error relocating .BTF.ext line info: %s\n",
6293 prog
->name
, errstr(err
));
6296 if (main_prog
->line_info
) {
6298 * Some info has already been found but has problem
6299 * in the last btf_ext reloc. Must have to error out.
6301 pr_warn("prog '%s': missing .BTF.ext line info.\n", prog
->name
);
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",
6311 static int cmp_relo_by_insn_idx(const void *key
, const void *elem
)
6313 size_t insn_idx
= *(const size_t *)key
;
6314 const struct reloc_desc
*relo
= elem
;
6316 if (insn_idx
== relo
->insn_idx
)
6318 return insn_idx
< relo
->insn_idx
? -1 : 1;
6321 static struct reloc_desc
*find_prog_insn_relo(const struct bpf_program
*prog
, size_t insn_idx
)
6323 if (!prog
->nr_reloc
)
6325 return bsearch(&insn_idx
, prog
->reloc_desc
, prog
->nr_reloc
,
6326 sizeof(*prog
->reloc_desc
), cmp_relo_by_insn_idx
);
6329 static int append_subprog_relos(struct bpf_program
*main_prog
, struct bpf_program
*subprog
)
6331 int new_cnt
= main_prog
->nr_reloc
+ subprog
->nr_reloc
;
6332 struct reloc_desc
*relos
;
6335 if (main_prog
== subprog
)
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)
6342 if (!relos
&& new_cnt
)
6344 if (subprog
->nr_reloc
)
6345 memcpy(relos
+ main_prog
->nr_reloc
, subprog
->reloc_desc
,
6346 sizeof(*relos
) * subprog
->nr_reloc
);
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.
6353 main_prog
->reloc_desc
= relos
;
6354 main_prog
->nr_reloc
= new_cnt
;
6359 bpf_object__append_subprog_code(struct bpf_object
*obj
, struct bpf_program
*main_prog
,
6360 struct bpf_program
*subprog
)
6362 struct bpf_insn
*insns
;
6366 subprog
->sub_insn_off
= main_prog
->insns_cnt
;
6368 new_cnt
= main_prog
->insns_cnt
+ subprog
->insns_cnt
;
6369 insns
= libbpf_reallocarray(main_prog
->insns
, new_cnt
, sizeof(*insns
));
6371 pr_warn("prog '%s': failed to realloc prog code\n", main_prog
->name
);
6374 main_prog
->insns
= insns
;
6375 main_prog
->insns_cnt
= new_cnt
;
6377 memcpy(main_prog
->insns
+ subprog
->sub_insn_off
, subprog
->insns
,
6378 subprog
->insns_cnt
* sizeof(*insns
));
6380 pr_debug("prog '%s': added %zu insns from sub-prog '%s'\n",
6381 main_prog
->name
, subprog
->insns_cnt
, subprog
->name
);
6383 /* The subprog insns are now appended. Append its relos too. */
6384 err
= append_subprog_relos(main_prog
, subprog
);
6391 bpf_object__reloc_code(struct bpf_object
*obj
, struct bpf_program
*main_prog
,
6392 struct bpf_program
*prog
)
6394 size_t sub_insn_idx
, insn_idx
;
6395 struct bpf_program
*subprog
;
6396 struct reloc_desc
*relo
;
6397 struct bpf_insn
*insn
;
6400 err
= reloc_prog_func_and_line_info(obj
, main_prog
, prog
);
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
))
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()
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
;
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
6428 * for subprog addr relocation, the relo->sym_off + insn->imm is
6429 * the byte offset in the corresponding section.
6431 if (relo
->type
== RELO_CALL
)
6432 sub_insn_idx
= relo
->sym_off
/ BPF_INSN_SZ
+ insn
->imm
+ 1;
6434 sub_insn_idx
= (relo
->sym_off
+ insn
->imm
) / BPF_INSN_SZ
;
6435 } else if (insn_is_pseudo_func(insn
)) {
6437 * RELO_SUBPROG_ADDR relo is always emitted even if both
6438 * functions are in the same section, so it shouldn't reach here.
6440 pr_warn("prog '%s': missing subprog addr relo for insn #%zu\n",
6441 prog
->name
, insn_idx
);
6442 return -LIBBPF_ERRNO__RELOC
;
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
6450 sub_insn_idx
= prog
->sec_insn_off
+ insn_idx
+ insn
->imm
+ 1;
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
);
6456 pr_warn("prog '%s': no .text section found yet sub-program call exists\n",
6458 return -LIBBPF_ERRNO__RELOC
;
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
6471 if (subprog
->sub_insn_off
== 0) {
6472 err
= bpf_object__append_subprog_code(obj
, main_prog
, subprog
);
6475 err
= bpf_object__reloc_code(obj
, main_prog
, subprog
);
6480 /* main_prog->insns memory could have been re-allocated, so
6481 * calculate pointer again
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
6490 insn
->imm
= subprog
->sub_insn_off
- (prog
->sub_insn_off
+ insn_idx
) - 1;
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
);
6500 * Relocate sub-program calls.
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.
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.
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.
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:
6537 * +--------+ +-------+
6539 * +--+---+ +--+-+-+ +---+--+
6540 * | subA | | subB | | subC |
6541 * +--+---+ +------+ +---+--+
6544 * +---+-------+ +------+----+
6545 * | mainA | | mainB |
6546 * +-----------+ +-----------+
6548 * We'll start relocating mainA, will find subA, append it and start
6549 * processing sub A recursively:
6551 * +-----------+------+
6553 * +-----------+------+
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):
6558 * +-----------+------+------+
6559 * | mainA | subA | subB |
6560 * +-----------+------+------+
6562 * At this point, we relocate subA calls, then go one level up and finish with
6563 * relocatin mainA calls. mainA is done.
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:
6569 * +-----------+------+
6571 * +-----------+------+
6572 * Now we see subC needs subB, so we go back to it, append and relocate it:
6574 * +-----------+------+------+
6575 * | mainB | subC | subB |
6576 * +-----------+------+------+
6578 * At this point we unwind recursion, relocate calls in subC, then in mainB.
6581 bpf_object__relocate_calls(struct bpf_object
*obj
, struct bpf_program
*prog
)
6583 struct bpf_program
*subprog
;
6586 /* mark all subprogs as not relocated (yet) within the context of
6587 * current main program
6589 for (i
= 0; i
< obj
->nr_programs
; i
++) {
6590 subprog
= &obj
->programs
[i
];
6591 if (!prog_is_subprog(obj
, subprog
))
6594 subprog
->sub_insn_off
= 0;
6597 err
= bpf_object__reloc_code(obj
, prog
, prog
);
6605 bpf_object__free_relocs(struct bpf_object
*obj
)
6607 struct bpf_program
*prog
;
6610 /* free up relocation descriptors */
6611 for (i
= 0; i
< obj
->nr_programs
; i
++) {
6612 prog
= &obj
->programs
[i
];
6613 zfree(&prog
->reloc_desc
);
6618 static int cmp_relocs(const void *_a
, const void *_b
)
6620 const struct reloc_desc
*a
= _a
;
6621 const struct reloc_desc
*b
= _b
;
6623 if (a
->insn_idx
!= b
->insn_idx
)
6624 return a
->insn_idx
< b
->insn_idx
? -1 : 1;
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;
6633 static void bpf_object__sort_relos(struct bpf_object
*obj
)
6637 for (i
= 0; i
< obj
->nr_programs
; i
++) {
6638 struct bpf_program
*p
= &obj
->programs
[i
];
6643 qsort(p
->reloc_desc
, p
->nr_reloc
, sizeof(*p
->reloc_desc
), cmp_relocs
);
6647 static int bpf_prog_assign_exc_cb(struct bpf_object
*obj
, struct bpf_program
*prog
)
6649 const char *str
= "exception_callback:";
6650 size_t pfx_len
= strlen(str
);
6653 if (!obj
->btf
|| !kernel_supports(obj
, FEAT_BTF_DECL_TAG
))
6656 n
= btf__type_cnt(obj
->btf
);
6657 for (i
= 1; i
< n
; i
++) {
6661 t
= btf_type_by_id(obj
->btf
, i
);
6662 if (!btf_is_decl_tag(t
) || btf_decl_tag(t
)->component_idx
!= -1)
6665 name
= btf__str_by_offset(obj
->btf
, t
->name_off
);
6666 if (strncmp(name
, str
, pfx_len
) != 0)
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",
6675 if (strcmp(prog
->name
, btf__str_by_offset(obj
->btf
, t
->name_off
)) != 0)
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.
6681 if (prog
->exception_cb_idx
>= 0) {
6682 prog
->exception_cb_idx
= -1;
6687 if (str_is_empty(name
)) {
6688 pr_warn("prog '%s': exception_callback:<value> decl tag contains empty value\n",
6693 for (j
= 0; j
< obj
->nr_programs
; j
++) {
6694 struct bpf_program
*subprog
= &obj
->programs
[j
];
6696 if (!prog_is_subprog(obj
, subprog
))
6698 if (strcmp(name
, subprog
->name
) != 0)
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.
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
);
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
);
6715 prog
->exception_cb_idx
= j
;
6719 if (prog
->exception_cb_idx
>= 0)
6722 pr_warn("prog '%s': cannot find exception callback '%s'\n", prog
->name
, name
);
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 */
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
6766 struct user_pt_regs
;
6767 struct user_regs_struct
;
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
)
6773 const struct btf_type
*t
;
6776 /* check if existing parameter already matches verifier expectations */
6777 t
= skip_mods_and_typedefs(btf
, arg_type_id
, NULL
);
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
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 */
6794 /* now we can ignore typedefs moving forward */
6795 t
= skip_mods_and_typedefs(btf
, t
->type
, NULL
);
6797 /* if it's `void *`, definitely fix up BTF info */
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)
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)
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)
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)
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)
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)
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
);
6840 static int clone_func_btf_info(struct btf
*btf
, int orig_fn_id
, struct bpf_program
*prog
)
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
;
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
);
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.
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
);
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)
6871 for (i
= 0; i
< arg_cnt
; i
++) {
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
;
6879 err
= btf__add_func_param(btf
, "", p
->type
);
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 */
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
6892 fn_id
= btf__add_func(btf
, prog
->name
, linkage
, fn_proto_id
);
6896 fn_t
= btf_type_by_id(btf
, fn_id
);
6897 fn_t
->name_off
= fn_name_off
; /* reuse original string */
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
6909 static int bpf_program_fixup_func_info(struct bpf_object
*obj
, struct bpf_program
*prog
)
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
;
6921 /* no .BTF.ext, no problem */
6922 if (!obj
->btf_ext
|| !prog
->func_info
)
6925 /* don't do any fix ups if kernel natively supports __arg_ctx */
6926 if (kernel_supports(obj
, FEAT_ARG_CTX_TAG
))
6929 /* some BPF program types just don't have named context structs, so
6930 * this fallback mechanism doesn't work for them
6932 for (i
= 0; i
< ARRAY_SIZE(global_ctx_map
); i
++) {
6933 if (global_ctx_map
[i
].prog_type
!= prog
->type
)
6935 ctx_name
= global_ctx_map
[i
].ctx_name
;
6941 /* remember original func BTF IDs to detect if we already cloned them */
6942 orig_ids
= calloc(prog
->func_info_cnt
, sizeof(*orig_ids
));
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
;
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
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
))
6959 if (strcmp(btf__str_by_offset(btf
, t
->name_off
), ctx_tag
) != 0)
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
)
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
))
6973 /* find corresponding func_info record */
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
;
6981 /* current main program doesn't call into this subprog */
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
)
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
))
6997 /* clone fn/fn_proto, unless we already did it for another arg */
6998 if (func_rec
->type_id
== orig_fn_id
) {
7001 fn_id
= clone_func_btf_info(btf
, orig_fn_id
, prog
);
7007 /* point func_info record to a cloned FUNC type */
7008 func_rec
->type_id
= fn_id
;
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 ->
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) {
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
);
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
);
7036 /* fix up type ID pointed to by param */
7037 p
= &btf_params(fn_proto_t
)[arg_idx
];
7048 static int bpf_object__relocate(struct bpf_object
*obj
, const char *targ_btf_path
)
7050 struct bpf_program
*prog
;
7055 err
= bpf_object__relocate_core(obj
, targ_btf_path
);
7057 pr_warn("failed to perform CO-RE relocations: %s\n",
7061 bpf_object__sort_relos(obj
);
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.
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
];
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
;
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
7087 * Append subprog relos to main programs to allow data relos to be
7088 * processed after text is completely relocated.
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
7095 if (prog_is_subprog(obj
, prog
))
7097 if (!prog
->autoload
)
7100 err
= bpf_object__relocate_calls(obj
, prog
);
7102 pr_warn("prog '%s': failed to relocate calls: %s\n",
7103 prog
->name
, errstr(err
));
7107 err
= bpf_prog_assign_exc_cb(obj
, prog
);
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
];
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.
7119 if (subprog
->sub_insn_off
== 0) {
7120 err
= bpf_object__append_subprog_code(obj
, prog
, subprog
);
7123 err
= bpf_object__reloc_code(obj
, prog
, subprog
);
7129 for (i
= 0; i
< obj
->nr_programs
; i
++) {
7130 prog
= &obj
->programs
[i
];
7131 if (prog_is_subprog(obj
, prog
))
7133 if (!prog
->autoload
)
7136 /* Process data relos for main programs */
7137 err
= bpf_object__relocate_data(obj
, prog
);
7139 pr_warn("prog '%s': failed to relocate data references: %s\n",
7140 prog
->name
, errstr(err
));
7144 /* Fix up .BTF.ext information, if necessary */
7145 err
= bpf_program_fixup_func_info(obj
, prog
);
7147 pr_warn("prog '%s': failed to perform .BTF.ext fix ups: %s\n",
7148 prog
->name
, errstr(err
));
7156 static int bpf_object__collect_st_ops_relos(struct bpf_object
*obj
,
7157 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
)
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
;
7176 if (!obj
->efile
.btf_maps_sec_btf_id
|| !obj
->btf
)
7178 sec
= btf__type_by_id(obj
->btf
, obj
->efile
.btf_maps_sec_btf_id
);
7182 nrels
= shdr
->sh_size
/ shdr
->sh_entsize
;
7183 for (i
= 0; i
< nrels
; i
++) {
7184 rel
= elf_rel_by_idx(data
, i
);
7186 pr_warn(".maps relo #%d: failed to get ELF relo\n", i
);
7187 return -LIBBPF_ERRNO__FORMAT
;
7190 sym
= elf_sym_by_idx(obj
, ELF64_R_SYM(rel
->r_info
));
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
;
7196 name
= elf_sym_str(obj
, sym
->st_name
) ?: "<?>";
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
);
7202 for (j
= 0; j
< obj
->nr_maps
; j
++) {
7203 map
= &obj
->maps
[j
];
7204 if (map
->sec_idx
!= obj
->efile
.btf_maps_shndx
)
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
)
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
);
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",
7225 return -LIBBPF_ERRNO__RELOC
;
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));
7233 targ_map
= bpf_object__find_map_by_name(obj
, name
);
7235 pr_warn(".maps relo #%d: '%s' isn't a valid map reference\n",
7239 } else if (is_prog_array
) {
7240 targ_prog
= bpf_object__find_program_by_name(obj
, name
);
7242 pr_warn(".maps relo #%d: '%s' isn't a valid program reference\n",
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",
7251 return -LIBBPF_ERRNO__RELOC
;
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)
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"))
7266 moff
= btf_member_bit_offset(def
, btf_vlen(def
) - 1) / 8;
7267 if (rel
->r_offset
- vi
->offset
< moff
)
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
7274 if (moff
% bpf_ptr_sz
)
7277 if (moff
>= map
->init_slots_sz
) {
7279 tmp
= libbpf_reallocarray(map
->init_slots
, new_sz
, host_ptr_sz
);
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
;
7287 map
->init_slots
[moff
] = is_map_in_map
? (void *)targ_map
: (void *)targ_prog
;
7289 pr_debug(".maps relo #%d: map '%s' slot [%d] points to %s '%s'\n",
7290 i
, map
->name
, moff
, type
, name
);
7296 static int bpf_object__collect_relos(struct bpf_object
*obj
)
7300 for (i
= 0; i
< obj
->efile
.sec_cnt
; i
++) {
7301 struct elf_sec_desc
*sec_desc
= &obj
->efile
.secs
[i
];
7306 if (sec_desc
->sec_type
!= SEC_RELO
)
7309 shdr
= sec_desc
->shdr
;
7310 data
= sec_desc
->data
;
7311 idx
= shdr
->sh_info
;
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
;
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
);
7323 err
= bpf_object__collect_prog_relos(obj
, shdr
, data
);
7328 bpf_object__sort_relos(obj
);
7332 static bool insn_is_helper_call(struct bpf_insn
*insn
, enum bpf_func_id
*func_id
)
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
;
7345 static int bpf_object__sanitize_prog(struct bpf_object
*obj
, struct bpf_program
*prog
)
7347 struct bpf_insn
*insn
= prog
->insns
;
7348 enum bpf_func_id func_id
;
7351 if (obj
->gen_loader
)
7354 for (i
= 0; i
< prog
->insns_cnt
; i
++, insn
++) {
7355 if (!insn_is_helper_call(insn
, &func_id
))
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
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
;
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
;
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
);
7383 /* this is called as prog->sec_def->prog_prepare_load_fn for libbpf-supported sec_defs */
7384 static int libbpf_prepare_prog_load(struct bpf_program
*prog
,
7385 struct bpf_prog_load_opts
*opts
, long cookie
)
7387 enum sec_def_flags def
= cookie
;
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;
7393 if (def
& SEC_SLEEPABLE
)
7394 opts
->prog_flags
|= BPF_F_SLEEPABLE
;
7396 if (prog
->type
== BPF_PROG_TYPE_XDP
&& (def
& SEC_XDP_FRAGS
))
7397 opts
->prog_flags
|= BPF_F_XDP_HAS_FRAGS
;
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
7405 prog
->expected_attach_type
= BPF_TRACE_UPROBE_MULTI
;
7406 opts
->expected_attach_type
= BPF_TRACE_UPROBE_MULTI
;
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
;
7413 attach_name
= strchr(prog
->sec_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.
7424 pr_warn("prog '%s': no BTF-based attach target is specified, use bpf_program__set_attach_target()\n",
7428 attach_name
++; /* skip over / */
7430 err
= libbpf_find_attach_btf_id(prog
, attach_name
, &btf_obj_fd
, &btf_type_id
);
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
;
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
7443 opts
->attach_btf_obj_fd
= btf_obj_fd
;
7444 opts
->attach_btf_id
= btf_type_id
;
7449 static void fixup_verifier_log(struct bpf_program
*prog
, char *buf
, size_t buf_sz
);
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
)
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
;
7463 /* Be more helpful by rejecting programs that can't be validated early
7464 * with more meaningful and actionable error message.
7466 switch (prog
->type
) {
7467 case BPF_PROG_TYPE_UNSPEC
:
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.
7472 pr_warn("prog '%s': missing BPF prog type, check ELF section name '%s'\n",
7473 prog
->name
, prog
->sec_name
);
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",
7486 if (!insns
|| !insns_cnt
)
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
;
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
;
7508 load_attr
.log_level
= log_level
;
7509 load_attr
.prog_flags
= prog
->prog_flags
;
7510 load_attr
.fd_array
= obj
->fd_array
;
7512 load_attr
.token_fd
= obj
->token_fd
;
7514 load_attr
.prog_flags
|= BPF_F_TOKEN_FD
;
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
);
7520 pr_warn("prog '%s': failed to prepare load attributes: %s\n",
7521 prog
->name
, errstr(err
));
7524 insns
= prog
->insns
;
7525 insns_cnt
= prog
->insns_cnt
;
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
);
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
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;
7552 log_buf_size
= max((size_t)BPF_LOG_BUF_SIZE
, log_buf_size
* 2);
7553 tmp
= realloc(log_buf
, log_buf_size
);
7564 load_attr
.log_buf
= log_buf
;
7565 load_attr
.log_size
= log_buf_size
;
7566 load_attr
.log_level
= log_level
;
7568 ret
= bpf_prog_load(prog
->type
, prog_name
, license
, insns
, insns_cnt
, &load_attr
);
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
);
7575 if (obj
->has_rodata
&& kernel_supports(obj
, FEAT_PROG_BIND_MAP
)) {
7576 struct bpf_map
*map
;
7579 for (i
= 0; i
< obj
->nr_maps
; i
++) {
7580 map
= &prog
->obj
->maps
[i
];
7581 if (map
->libbpf_type
!= LIBBPF_MAP_RODATA
)
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. */
7597 if (log_level
== 0) {
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).
7608 if (own_log_buf
&& errno
== ENOSPC
&& log_buf_size
<= UINT_MAX
/ 2)
7613 /* post-process verifier log to improve error descriptions */
7614 fixup_verifier_log(prog
, log_buf
, log_buf_size
);
7616 pr_warn("prog '%s': BPF program load failed: %s\n", prog
->name
, errstr(errno
));
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
);
7630 static char *find_prev_line(char *buf
, char *cur
)
7634 if (cur
== buf
) /* end of a log buf */
7638 while (p
- 1 >= buf
&& *(p
- 1) != '\n')
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
)
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
);
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.
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
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.
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;
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
);
7674 /* shift remaining log to the right by calculated amount */
7675 memmove(orig
+ patch_sz
, orig
+ orig_sz
, rem_sz
);
7678 memcpy(orig
, patch
, patch_sz
);
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
)
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>
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.
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
;
7700 if (sscanf(line1
, "%d: (%*d) call unknown#195896080\n", &insn_idx
) != 1)
7703 relo
= find_relo_core(prog
, insn_idx
);
7707 err
= bpf_core_parse_spec(prog
->name
, prog
->obj
->btf
, relo
, &spec
);
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
) ? "..." : "");
7717 patch_log(buf
, buf_sz
, log_sz
, line1
, line3
- line1
, patch
);
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
)
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>
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.
7732 struct bpf_object
*obj
= prog
->obj
;
7733 const struct bpf_map
*map
;
7734 int insn_idx
, map_idx
;
7737 if (sscanf(line1
, "%d: (%*d) call unknown#%d\n", &insn_idx
, &map_idx
) != 2)
7740 map_idx
-= POISON_LDIMM64_MAP_BASE
;
7741 if (map_idx
< 0 || map_idx
>= obj
->nr_maps
)
7743 map
= &obj
->maps
[map_idx
];
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
);
7750 patch_log(buf
, buf_sz
, log_sz
, line1
, line3
- line1
, patch
);
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
)
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>
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.
7765 struct bpf_object
*obj
= prog
->obj
;
7766 const struct extern_desc
*ext
;
7767 int insn_idx
, ext_idx
;
7770 if (sscanf(line1
, "%d: (%*d) call unknown#%d\n", &insn_idx
, &ext_idx
) != 2)
7773 ext_idx
-= POISON_CALL_KFUNC_BASE
;
7774 if (ext_idx
< 0 || ext_idx
>= obj
->nr_extern
)
7776 ext
= &obj
->externs
[ext_idx
];
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
);
7783 patch_log(buf
, buf_sz
, log_sz
, line1
, line3
- line1
, patch
);
7786 static void fixup_verifier_log(struct bpf_program
*prog
, char *buf
, size_t buf_sz
)
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
;
7797 log_sz
= strlen(buf
) + 1;
7798 next_line
= buf
+ log_sz
- 1;
7800 for (i
= 0; i
< max_last_line_cnt
; i
++, next_line
= cur_line
) {
7801 cur_line
= find_prev_line(buf
, next_line
);
7805 if (str_has_pfx(cur_line
, "invalid func unknown#195896080\n")) {
7806 prev_line
= find_prev_line(buf
, cur_line
);
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
);
7814 } else if (str_has_pfx(cur_line
, "invalid func unknown#"POISON_LDIMM64_MAP_PFX
)) {
7815 prev_line
= find_prev_line(buf
, cur_line
);
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
);
7823 } else if (str_has_pfx(cur_line
, "invalid func unknown#"POISON_CALL_KFUNC_PFX
)) {
7824 prev_line
= find_prev_line(buf
, cur_line
);
7828 /* reference to unresolved kfunc */
7829 fixup_log_missing_kfunc_call(prog
, buf
, buf_sz
, log_sz
,
7830 prev_line
, cur_line
, next_line
);
7836 static int bpf_program_record_relos(struct bpf_program
*prog
)
7838 struct bpf_object
*obj
= prog
->obj
;
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
];
7846 switch (relo
->type
) {
7847 case RELO_EXTERN_LD64
:
7848 if (ext
->type
!= EXT_KSYM
)
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
);
7856 case RELO_EXTERN_CALL
:
7857 bpf_gen__record_extern(obj
->gen_loader
, ext
->name
,
7858 ext
->is_weak
, false, false, BTF_KIND_FUNC
,
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
,
7869 bpf_gen__record_relo_core(obj
->gen_loader
, &cr
);
7880 bpf_object__load_progs(struct bpf_object
*obj
, int log_level
)
7882 struct bpf_program
*prog
;
7886 for (i
= 0; i
< obj
->nr_programs
; i
++) {
7887 prog
= &obj
->programs
[i
];
7888 err
= bpf_object__sanitize_prog(obj
, prog
);
7893 for (i
= 0; i
< obj
->nr_programs
; i
++) {
7894 prog
= &obj
->programs
[i
];
7895 if (prog_is_subprog(obj
, prog
))
7897 if (!prog
->autoload
) {
7898 pr_debug("prog '%s': skipped loading\n", prog
->name
);
7901 prog
->log_level
|= log_level
;
7903 if (obj
->gen_loader
)
7904 bpf_program_record_relos(prog
);
7906 err
= bpf_object_load_prog(obj
, prog
, prog
->insns
, prog
->insns_cnt
,
7907 obj
->license
, obj
->kern_version
, &prog
->fd
);
7909 pr_warn("prog '%s': failed to load: %s\n", prog
->name
, errstr(err
));
7914 bpf_object__free_relocs(obj
);
7918 static const struct bpf_sec_def
*find_sec_def(const char *sec_name
);
7920 static int bpf_object_init_progs(struct bpf_object
*obj
, const struct bpf_object_open_opts
*opts
)
7922 struct bpf_program
*prog
;
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
);
7934 prog
->type
= prog
->sec_def
->prog_type
;
7935 prog
->expected_attach_type
= prog
->sec_def
->expected_attach_type
;
7937 /* sec_def can have custom callback which should be called
7938 * after bpf_program is initialized to adjust its properties
7940 if (prog
->sec_def
->prog_setup_fn
) {
7941 err
= prog
->sec_def
->prog_setup_fn(prog
, prog
->sec_def
->cookie
);
7943 pr_warn("prog '%s': failed to initialize: %s\n",
7944 prog
->name
, errstr(err
));
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
)
7957 const char *kconfig
, *btf_tmp_path
, *token_path
;
7958 struct bpf_object
*obj
;
7964 if (obj_buf
&& !obj_name
)
7965 return ERR_PTR(-EINVAL
);
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
);
7973 if (!OPTS_VALID(opts
, bpf_object_open_opts
))
7974 return ERR_PTR(-EINVAL
);
7976 obj_name
= OPTS_GET(opts
, object_name
, NULL
) ?: obj_name
;
7979 pr_debug("loading object '%s' from buffer\n", obj_name
);
7981 pr_debug("loading object from %s\n", path
);
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
);
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
7998 token_path
= getenv("LIBBPF_BPF_TOKEN_PATH");
7999 if (token_path
&& strlen(token_path
) >= PATH_MAX
)
8000 return ERR_PTR(-ENAMETOOLONG
);
8002 obj
= bpf_object__new(path
, obj_buf
, obj_buf_sz
, obj_name
);
8006 obj
->log_buf
= log_buf
;
8007 obj
->log_size
= log_size
;
8008 obj
->log_level
= log_level
;
8011 obj
->token_path
= strdup(token_path
);
8012 if (!obj
->token_path
) {
8018 btf_tmp_path
= OPTS_GET(opts
, btf_custom_path
, NULL
);
8020 if (strlen(btf_tmp_path
) >= PATH_MAX
) {
8021 err
= -ENAMETOOLONG
;
8024 obj
->btf_custom_path
= strdup(btf_tmp_path
);
8025 if (!obj
->btf_custom_path
) {
8031 kconfig
= OPTS_GET(opts
, kconfig
, NULL
);
8033 obj
->kconfig
= strdup(kconfig
);
8034 if (!obj
->kconfig
) {
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
);
8050 bpf_object__elf_finish(obj
);
8054 bpf_object__close(obj
);
8055 return ERR_PTR(err
);
8059 bpf_object__open_file(const char *path
, const struct bpf_object_open_opts
*opts
)
8062 return libbpf_err_ptr(-EINVAL
);
8064 return libbpf_ptr(bpf_object_open(path
, NULL
, 0, NULL
, opts
));
8067 struct bpf_object
*bpf_object__open(const char *path
)
8069 return bpf_object__open_file(path
, NULL
);
8073 bpf_object__open_mem(const void *obj_buf
, size_t obj_buf_sz
,
8074 const struct bpf_object_open_opts
*opts
)
8078 if (!obj_buf
|| obj_buf_sz
== 0)
8079 return libbpf_err_ptr(-EINVAL
);
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
);
8084 return libbpf_ptr(bpf_object_open(NULL
, obj_buf
, obj_buf_sz
, tmp_name
, opts
));
8087 static int bpf_object_unload(struct bpf_object
*obj
)
8092 return libbpf_err(-EINVAL
);
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
);
8100 for (i
= 0; i
< obj
->nr_programs
; i
++)
8101 bpf_program__unload(&obj
->programs
[i
]);
8106 static int bpf_object__sanitize_maps(struct bpf_object
*obj
)
8110 bpf_object__for_each_map(m
, obj
) {
8111 if (!bpf_map__is_internal(m
))
8113 if (!kernel_supports(obj
, FEAT_ARRAY_MMAP
))
8114 m
->def
.map_flags
&= ~BPF_F_MMAPABLE
;
8120 typedef int (*kallsyms_cb_t
)(unsigned long long sym_addr
, char sym_type
,
8121 const char *sym_name
, void *ctx
);
8123 static int libbpf_kallsyms_parse(kallsyms_cb_t cb
, void *ctx
)
8125 char sym_type
, sym_name
[500];
8126 unsigned long long sym_addr
;
8130 f
= fopen("/proc/kallsyms", "re");
8133 pr_warn("failed to open /proc/kallsyms: %s\n", errstr(err
));
8138 ret
= fscanf(f
, "%llx %c %499s%*[^\n]\n",
8139 &sym_addr
, &sym_type
, sym_name
);
8140 if (ret
== EOF
&& feof(f
))
8143 pr_warn("failed to read kallsyms entry: %d\n", ret
);
8148 err
= cb(sym_addr
, sym_type
, sym_name
, ctx
);
8157 static int kallsyms_cb(unsigned long long sym_addr
, char sym_type
,
8158 const char *sym_name
, void *ctx
)
8160 struct bpf_object
*obj
= ctx
;
8161 const struct btf_type
*t
;
8162 struct extern_desc
*ext
;
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
);
8169 ext
= find_extern_by_name(obj
, sym_name
);
8170 if (!ext
|| ext
->type
!= EXT_KSYM
)
8173 t
= btf__type_by_id(obj
->btf
, ext
->btf_id
);
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
);
8184 ext
->ksym
.addr
= sym_addr
;
8185 pr_debug("extern (ksym) '%s': set to 0x%llx\n", sym_name
, sym_addr
);
8190 static int bpf_object__read_kallsyms_file(struct bpf_object
*obj
)
8192 return libbpf_kallsyms_parse(kallsyms_cb
, obj
);
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
)
8199 struct module_btf
*mod_btf
;
8203 btf
= obj
->btf_vmlinux
;
8205 id
= btf__find_by_name_kind(btf
, ksym_name
, kind
);
8207 if (id
== -ENOENT
) {
8208 err
= load_module_btfs(obj
);
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
];
8216 id
= btf__find_by_name_kind_own(btf
, ksym_name
, kind
);
8225 *res_mod_btf
= mod_btf
;
8229 static int bpf_object__resolve_ksym_var_btf_id(struct bpf_object
*obj
,
8230 struct extern_desc
*ext
)
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
;
8239 id
= find_ksym_btf_id(obj
, ext
->name
, BTF_KIND_VAR
, &btf
, &mod_btf
);
8241 if (id
== -ESRCH
&& ext
->is_weak
)
8243 pr_warn("extern (var ksym) '%s': not found in kernel BTF\n",
8248 /* find local type_id */
8249 local_type_id
= ext
->ksym
.type_id
;
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
);
8256 err
= bpf_core_types_are_compat(obj
->btf
, local_type_id
,
8259 const struct btf_type
*local_type
;
8260 const char *targ_name
, *local_name
;
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
);
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
);
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
);
8282 static int bpf_object__resolve_ksym_func_btf_id(struct bpf_object
*obj
,
8283 struct extern_desc
*ext
)
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
;
8291 local_func_proto_id
= ext
->ksym
.type_id
;
8293 kfunc_id
= find_ksym_btf_id(obj
, ext
->essent_name
?: ext
->name
, BTF_KIND_FUNC
, &kern_btf
,
8296 if (kfunc_id
== -ESRCH
&& ext
->is_weak
)
8298 pr_warn("extern (func ksym) '%s': not found in kernel or module BTFs\n",
8303 kern_func
= btf__type_by_id(kern_btf
, kfunc_id
);
8304 kfunc_proto_id
= kern_func
->type
;
8306 ret
= bpf_core_types_are_compat(obj
->btf
, local_func_proto_id
,
8307 kern_btf
, kfunc_proto_id
);
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
);
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
);
8326 /* Cannot use index 0 for module BTF fd */
8327 if (!obj
->fd_array_cnt
)
8328 obj
->fd_array_cnt
= 1;
8330 ret
= libbpf_ensure_mem((void **)&obj
->fd_array
, &obj
->fd_array_cap
, sizeof(int),
8331 obj
->fd_array_cnt
+ 1);
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
;
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.
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
);
8354 static int bpf_object__resolve_ksyms_btf_id(struct bpf_object
*obj
)
8356 const struct btf_type
*t
;
8357 struct extern_desc
*ext
;
8360 for (i
= 0; i
< obj
->nr_extern
; i
++) {
8361 ext
= &obj
->externs
[i
];
8362 if (ext
->type
!= EXT_KSYM
|| !ext
->ksym
.type_id
)
8365 if (obj
->gen_loader
) {
8367 ext
->ksym
.kernel_btf_obj_fd
= 0;
8368 ext
->ksym
.kernel_btf_id
= 0;
8371 t
= btf__type_by_id(obj
->btf
, ext
->btf_id
);
8373 err
= bpf_object__resolve_ksym_var_btf_id(obj
, ext
);
8375 err
= bpf_object__resolve_ksym_func_btf_id(obj
, ext
);
8382 static int bpf_object__resolve_externs(struct bpf_object
*obj
,
8383 const char *extra_kconfig
)
8385 bool need_config
= false, need_kallsyms
= false;
8386 bool need_vmlinux_btf
= false;
8387 struct extern_desc
*ext
;
8388 void *kcfg_data
= NULL
;
8391 if (obj
->nr_extern
== 0)
8394 if (obj
->kconfig_map_idx
>= 0)
8395 kcfg_data
= obj
->maps
[obj
->kconfig_map_idx
].mmaped
;
8397 for (i
= 0; i
< obj
->nr_extern
; i
++) {
8398 ext
= &obj
->externs
[i
];
8400 if (ext
->type
== EXT_KSYM
) {
8401 if (ext
->ksym
.type_id
)
8402 need_vmlinux_btf
= true;
8404 need_kallsyms
= true;
8406 } else if (ext
->type
== EXT_KCFG
) {
8407 void *ext_ptr
= kcfg_data
+ ext
->kcfg
.data_off
;
8410 /* Kconfig externs need actual /proc/config.gz */
8411 if (str_has_pfx(ext
->name
, "CONFIG_")) {
8416 /* Virtual kcfg externs are customly handled by libbpf */
8417 if (strcmp(ext
->name
, "LINUX_KERNEL_VERSION") == 0) {
8418 value
= get_kernel_version();
8420 pr_warn("extern (kcfg) '%s': failed to get kernel version\n", ext
->name
);
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
8435 pr_warn("extern (kcfg) '%s': unrecognized virtual extern\n", ext
->name
);
8439 err
= set_kcfg_value_num(ext
, ext_ptr
, value
);
8442 pr_debug("extern (kcfg) '%s': set to 0x%llx\n",
8443 ext
->name
, (long long)value
);
8445 pr_warn("extern '%s': unrecognized extern kind\n", ext
->name
);
8449 if (need_config
&& extra_kconfig
) {
8450 err
= bpf_object__read_kconfig_mem(obj
, extra_kconfig
, kcfg_data
);
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
) {
8463 err
= bpf_object__read_kconfig_file(obj
, kcfg_data
);
8467 if (need_kallsyms
) {
8468 err
= bpf_object__read_kallsyms_file(obj
);
8472 if (need_vmlinux_btf
) {
8473 err
= bpf_object__resolve_ksyms_btf_id(obj
);
8477 for (i
= 0; i
< obj
->nr_extern
; i
++) {
8478 ext
= &obj
->externs
[i
];
8480 if (!ext
->is_set
&& !ext
->is_weak
) {
8481 pr_warn("extern '%s' (strong): not resolved\n", ext
->name
);
8483 } else if (!ext
->is_set
) {
8484 pr_debug("extern '%s' (weak): not resolved, defaulting to zero\n",
8492 static void bpf_map_prepare_vdata(const struct bpf_map
*map
)
8494 const struct btf_type
*type
;
8495 struct bpf_struct_ops
*st_ops
;
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
];
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
;
8514 static int bpf_object_prepare_struct_ops(struct bpf_object
*obj
)
8516 struct bpf_map
*map
;
8519 for (i
= 0; i
< obj
->nr_maps
; i
++) {
8520 map
= &obj
->maps
[i
];
8522 if (!bpf_map__is_struct_ops(map
))
8525 if (!map
->autocreate
)
8528 bpf_map_prepare_vdata(map
);
8534 static int bpf_object_load(struct bpf_object
*obj
, int extra_log_level
, const char *target_btf_path
)
8539 return libbpf_err(-EINVAL
);
8542 pr_warn("object '%s': load can't be attempted twice\n", obj
->name
);
8543 return libbpf_err(-EINVAL
);
8546 /* Disallow kernel loading programs of non-native endianness but
8547 * permit cross-endian creation of "light skeleton".
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
);
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
);
8570 if (obj
->gen_loader
) {
8573 btf__set_fd(obj
->btf
, -1);
8575 err
= bpf_gen__finish(obj
->gen_loader
, obj
->nr_programs
, obj
->nr_maps
);
8578 /* clean up fd_array */
8579 zfree(&obj
->fd_array
);
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
);
8587 free(obj
->btf_modules
);
8589 /* clean up vmlinux BTF */
8590 btf__free(obj
->btf_vmlinux
);
8591 obj
->btf_vmlinux
= NULL
;
8593 obj
->loaded
= true; /* doesn't matter if successfully or not */
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
);
8605 bpf_object_unload(obj
);
8606 pr_warn("failed to load object '%s'\n", obj
->path
);
8607 return libbpf_err(err
);
8610 int bpf_object__load(struct bpf_object
*obj
)
8612 return bpf_object_load(obj
, 0, NULL
);
8615 static int make_parent_dir(const char *path
)
8620 dname
= strdup(path
);
8624 dir
= dirname(dname
);
8625 if (mkdir(dir
, 0700) && errno
!= EEXIST
)
8630 pr_warn("failed to mkdir %s: %s\n", path
, errstr(err
));
8635 static int check_path(const char *path
)
8637 struct statfs st_fs
;
8644 dname
= strdup(path
);
8648 dir
= dirname(dname
);
8649 if (statfs(dir
, &st_fs
)) {
8650 pr_warn("failed to statfs %s: %s\n", dir
, errstr(errno
));
8655 if (!err
&& st_fs
.f_type
!= BPF_FS_MAGIC
) {
8656 pr_warn("specified path %s is not on BPF FS\n", path
);
8663 int bpf_program__pin(struct bpf_program
*prog
, const char *path
)
8668 pr_warn("prog '%s': can't pin program that wasn't loaded\n", prog
->name
);
8669 return libbpf_err(-EINVAL
);
8672 err
= make_parent_dir(path
);
8674 return libbpf_err(err
);
8676 err
= check_path(path
);
8678 return libbpf_err(err
);
8680 if (bpf_obj_pin(prog
->fd
, path
)) {
8682 pr_warn("prog '%s': failed to pin at '%s': %s\n", prog
->name
, path
, errstr(err
));
8683 return libbpf_err(err
);
8686 pr_debug("prog '%s': pinned at '%s'\n", prog
->name
, path
);
8690 int bpf_program__unpin(struct bpf_program
*prog
, const char *path
)
8695 pr_warn("prog '%s': can't unpin program that wasn't loaded\n", prog
->name
);
8696 return libbpf_err(-EINVAL
);
8699 err
= check_path(path
);
8701 return libbpf_err(err
);
8705 return libbpf_err(-errno
);
8707 pr_debug("prog '%s': unpinned from '%s'\n", prog
->name
, path
);
8711 int bpf_map__pin(struct bpf_map
*map
, const char *path
)
8716 pr_warn("invalid map pointer\n");
8717 return libbpf_err(-EINVAL
);
8721 pr_warn("map '%s': can't pin BPF map without FD (was it created?)\n", map
->name
);
8722 return libbpf_err(-EINVAL
);
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
);
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
);
8745 map
->pin_path
= strdup(path
);
8746 if (!map
->pin_path
) {
8752 err
= make_parent_dir(map
->pin_path
);
8754 return libbpf_err(err
);
8756 err
= check_path(map
->pin_path
);
8758 return libbpf_err(err
);
8760 if (bpf_obj_pin(map
->fd
, map
->pin_path
)) {
8766 pr_debug("pinned map '%s'\n", map
->pin_path
);
8771 pr_warn("failed to pin map: %s\n", errstr(err
));
8772 return libbpf_err(err
);
8775 int bpf_map__unpin(struct bpf_map
*map
, const char *path
)
8780 pr_warn("invalid map pointer\n");
8781 return libbpf_err(-EINVAL
);
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
);
8790 path
= map
->pin_path
;
8792 pr_warn("no path to unpin map '%s' from\n",
8793 bpf_map__name(map
));
8794 return libbpf_err(-EINVAL
);
8797 err
= check_path(path
);
8799 return libbpf_err(err
);
8803 return libbpf_err(-errno
);
8805 map
->pinned
= false;
8806 pr_debug("unpinned map '%s' from '%s'\n", bpf_map__name(map
), path
);
8811 int bpf_map__set_pin_path(struct bpf_map
*map
, const char *path
)
8818 return libbpf_err(-errno
);
8821 free(map
->pin_path
);
8822 map
->pin_path
= new;
8826 __alias(bpf_map__pin_path
)
8827 const char *bpf_map__get_pin_path(const struct bpf_map
*map
);
8829 const char *bpf_map__pin_path(const struct bpf_map
*map
)
8831 return map
->pin_path
;
8834 bool bpf_map__is_pinned(const struct bpf_map
*map
)
8839 static void sanitize_pin_path(char *s
)
8841 /* bpffs disallows periods in path names */
8849 int bpf_object__pin_maps(struct bpf_object
*obj
, const char *path
)
8851 struct bpf_map
*map
;
8855 return libbpf_err(-ENOENT
);
8858 pr_warn("object not yet loaded; load it first\n");
8859 return libbpf_err(-ENOENT
);
8862 bpf_object__for_each_map(map
, obj
) {
8863 char *pin_path
= NULL
;
8866 if (!map
->autocreate
)
8870 err
= pathname_concat(buf
, sizeof(buf
), path
, bpf_map__name(map
));
8872 goto err_unpin_maps
;
8873 sanitize_pin_path(buf
);
8875 } else if (!map
->pin_path
) {
8879 err
= bpf_map__pin(map
, pin_path
);
8881 goto err_unpin_maps
;
8887 while ((map
= bpf_object__prev_map(obj
, map
))) {
8891 bpf_map__unpin(map
, NULL
);
8894 return libbpf_err(err
);
8897 int bpf_object__unpin_maps(struct bpf_object
*obj
, const char *path
)
8899 struct bpf_map
*map
;
8903 return libbpf_err(-ENOENT
);
8905 bpf_object__for_each_map(map
, obj
) {
8906 char *pin_path
= NULL
;
8910 err
= pathname_concat(buf
, sizeof(buf
), path
, bpf_map__name(map
));
8912 return libbpf_err(err
);
8913 sanitize_pin_path(buf
);
8915 } else if (!map
->pin_path
) {
8919 err
= bpf_map__unpin(map
, pin_path
);
8921 return libbpf_err(err
);
8927 int bpf_object__pin_programs(struct bpf_object
*obj
, const char *path
)
8929 struct bpf_program
*prog
;
8934 return libbpf_err(-ENOENT
);
8937 pr_warn("object not yet loaded; load it first\n");
8938 return libbpf_err(-ENOENT
);
8941 bpf_object__for_each_program(prog
, obj
) {
8942 err
= pathname_concat(buf
, sizeof(buf
), path
, prog
->name
);
8944 goto err_unpin_programs
;
8946 err
= bpf_program__pin(prog
, buf
);
8948 goto err_unpin_programs
;
8954 while ((prog
= bpf_object__prev_program(obj
, prog
))) {
8955 if (pathname_concat(buf
, sizeof(buf
), path
, prog
->name
))
8958 bpf_program__unpin(prog
, buf
);
8961 return libbpf_err(err
);
8964 int bpf_object__unpin_programs(struct bpf_object
*obj
, const char *path
)
8966 struct bpf_program
*prog
;
8970 return libbpf_err(-ENOENT
);
8972 bpf_object__for_each_program(prog
, obj
) {
8975 err
= pathname_concat(buf
, sizeof(buf
), path
, prog
->name
);
8977 return libbpf_err(err
);
8979 err
= bpf_program__unpin(prog
, buf
);
8981 return libbpf_err(err
);
8987 int bpf_object__pin(struct bpf_object
*obj
, const char *path
)
8991 err
= bpf_object__pin_maps(obj
, path
);
8993 return libbpf_err(err
);
8995 err
= bpf_object__pin_programs(obj
, path
);
8997 bpf_object__unpin_maps(obj
, path
);
8998 return libbpf_err(err
);
9004 int bpf_object__unpin(struct bpf_object
*obj
, const char *path
)
9008 err
= bpf_object__unpin_programs(obj
, path
);
9010 return libbpf_err(err
);
9012 err
= bpf_object__unpin_maps(obj
, path
);
9014 return libbpf_err(err
);
9019 static void bpf_map__destroy(struct bpf_map
*map
)
9021 if (map
->inner_map
) {
9022 bpf_map__destroy(map
->inner_map
);
9023 zfree(&map
->inner_map
);
9026 zfree(&map
->init_slots
);
9027 map
->init_slots_sz
= 0;
9029 if (map
->mmaped
&& map
->mmaped
!= map
->obj
->arena_data
)
9030 munmap(map
->mmaped
, bpf_map_mmap_sz(map
));
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
);
9041 zfree(&map
->real_name
);
9042 zfree(&map
->pin_path
);
9048 void bpf_object__close(struct bpf_object
*obj
)
9052 if (IS_ERR_OR_NULL(obj
))
9055 usdt_manager_free(obj
->usdt_man
);
9056 obj
->usdt_man
= NULL
;
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
);
9065 for (i
= 0; i
< obj
->nr_maps
; i
++)
9066 bpf_map__destroy(&obj
->maps
[i
]);
9068 zfree(&obj
->btf_custom_path
);
9069 zfree(&obj
->kconfig
);
9071 for (i
= 0; i
< obj
->nr_extern
; i
++)
9072 zfree(&obj
->externs
[i
].essent_name
);
9074 zfree(&obj
->externs
);
9080 if (obj
->programs
&& obj
->nr_programs
) {
9081 for (i
= 0; i
< obj
->nr_programs
; i
++)
9082 bpf_program__exit(&obj
->programs
[i
]);
9084 zfree(&obj
->programs
);
9086 zfree(&obj
->feat_cache
);
9087 zfree(&obj
->token_path
);
9088 if (obj
->token_fd
> 0)
9089 close(obj
->token_fd
);
9091 zfree(&obj
->arena_data
);
9096 const char *bpf_object__name(const struct bpf_object
*obj
)
9098 return obj
? obj
->name
: libbpf_err_ptr(-EINVAL
);
9101 unsigned int bpf_object__kversion(const struct bpf_object
*obj
)
9103 return obj
? obj
->kern_version
: 0;
9106 int bpf_object__token_fd(const struct bpf_object
*obj
)
9108 return obj
->token_fd
?: -1;
9111 struct btf
*bpf_object__btf(const struct bpf_object
*obj
)
9113 return obj
? obj
->btf
: NULL
;
9116 int bpf_object__btf_fd(const struct bpf_object
*obj
)
9118 return obj
->btf
? btf__fd(obj
->btf
) : -1;
9121 int bpf_object__set_kversion(struct bpf_object
*obj
, __u32 kern_version
)
9124 return libbpf_err(-EINVAL
);
9126 obj
->kern_version
= kern_version
;
9131 int bpf_object__gen_loader(struct bpf_object
*obj
, struct gen_loader_opts
*opts
)
9133 struct bpf_gen
*gen
;
9137 if (!OPTS_VALID(opts
, gen_loader_opts
))
9139 gen
= calloc(sizeof(*gen
), 1);
9143 gen
->swapped_endian
= !is_native_endianness(obj
);
9144 obj
->gen_loader
= gen
;
9148 static struct bpf_program
*
9149 __bpf_program__iter(const struct bpf_program
*p
, const struct bpf_object
*obj
,
9152 size_t nr_programs
= obj
->nr_programs
;
9159 /* Iter from the beginning */
9160 return forward
? &obj
->programs
[0] :
9161 &obj
->programs
[nr_programs
- 1];
9163 if (p
->obj
!= obj
) {
9164 pr_warn("error: program handler doesn't match object\n");
9165 return errno
= EINVAL
, NULL
;
9168 idx
= (p
- obj
->programs
) + (forward
? 1 : -1);
9169 if (idx
>= obj
->nr_programs
|| idx
< 0)
9171 return &obj
->programs
[idx
];
9174 struct bpf_program
*
9175 bpf_object__next_program(const struct bpf_object
*obj
, struct bpf_program
*prev
)
9177 struct bpf_program
*prog
= prev
;
9180 prog
= __bpf_program__iter(prog
, obj
, true);
9181 } while (prog
&& prog_is_subprog(obj
, prog
));
9186 struct bpf_program
*
9187 bpf_object__prev_program(const struct bpf_object
*obj
, struct bpf_program
*next
)
9189 struct bpf_program
*prog
= next
;
9192 prog
= __bpf_program__iter(prog
, obj
, false);
9193 } while (prog
&& prog_is_subprog(obj
, prog
));
9198 void bpf_program__set_ifindex(struct bpf_program
*prog
, __u32 ifindex
)
9200 prog
->prog_ifindex
= ifindex
;
9203 const char *bpf_program__name(const struct bpf_program
*prog
)
9208 const char *bpf_program__section_name(const struct bpf_program
*prog
)
9210 return prog
->sec_name
;
9213 bool bpf_program__autoload(const struct bpf_program
*prog
)
9215 return prog
->autoload
;
9218 int bpf_program__set_autoload(struct bpf_program
*prog
, bool autoload
)
9220 if (prog
->obj
->loaded
)
9221 return libbpf_err(-EINVAL
);
9223 prog
->autoload
= autoload
;
9227 bool bpf_program__autoattach(const struct bpf_program
*prog
)
9229 return prog
->autoattach
;
9232 void bpf_program__set_autoattach(struct bpf_program
*prog
, bool autoattach
)
9234 prog
->autoattach
= autoattach
;
9237 const struct bpf_insn
*bpf_program__insns(const struct bpf_program
*prog
)
9242 size_t bpf_program__insn_cnt(const struct bpf_program
*prog
)
9244 return prog
->insns_cnt
;
9247 int bpf_program__set_insns(struct bpf_program
*prog
,
9248 struct bpf_insn
*new_insns
, size_t new_insn_cnt
)
9250 struct bpf_insn
*insns
;
9252 if (prog
->obj
->loaded
)
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
);
9261 memcpy(insns
, new_insns
, new_insn_cnt
* sizeof(*insns
));
9263 prog
->insns
= insns
;
9264 prog
->insns_cnt
= new_insn_cnt
;
9268 int bpf_program__fd(const struct bpf_program
*prog
)
9271 return libbpf_err(-EINVAL
);
9274 return libbpf_err(-ENOENT
);
9279 __alias(bpf_program__type
)
9280 enum bpf_prog_type
bpf_program__get_type(const struct bpf_program
*prog
);
9282 enum bpf_prog_type
bpf_program__type(const struct bpf_program
*prog
)
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
;
9293 int bpf_program__set_type(struct bpf_program
*prog
, enum bpf_prog_type type
)
9295 if (prog
->obj
->loaded
)
9296 return libbpf_err(-EBUSY
);
9298 /* if type is not changed, do nothing */
9299 if (prog
->type
== type
)
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.
9310 if (prog
->sec_def
!= &custom_fallback_def
)
9311 prog
->sec_def
= NULL
;
9315 __alias(bpf_program__expected_attach_type
)
9316 enum bpf_attach_type
bpf_program__get_expected_attach_type(const struct bpf_program
*prog
);
9318 enum bpf_attach_type
bpf_program__expected_attach_type(const struct bpf_program
*prog
)
9320 return prog
->expected_attach_type
;
9323 int bpf_program__set_expected_attach_type(struct bpf_program
*prog
,
9324 enum bpf_attach_type type
)
9326 if (prog
->obj
->loaded
)
9327 return libbpf_err(-EBUSY
);
9329 prog
->expected_attach_type
= type
;
9333 __u32
bpf_program__flags(const struct bpf_program
*prog
)
9335 return prog
->prog_flags
;
9338 int bpf_program__set_flags(struct bpf_program
*prog
, __u32 flags
)
9340 if (prog
->obj
->loaded
)
9341 return libbpf_err(-EBUSY
);
9343 prog
->prog_flags
= flags
;
9347 __u32
bpf_program__log_level(const struct bpf_program
*prog
)
9349 return prog
->log_level
;
9352 int bpf_program__set_log_level(struct bpf_program
*prog
, __u32 log_level
)
9354 if (prog
->obj
->loaded
)
9355 return libbpf_err(-EBUSY
);
9357 prog
->log_level
= log_level
;
9361 const char *bpf_program__log_buf(const struct bpf_program
*prog
, size_t *log_size
)
9363 *log_size
= prog
->log_size
;
9364 return prog
->log_buf
;
9367 int bpf_program__set_log_buf(struct bpf_program
*prog
, char *log_buf
, size_t log_size
)
9369 if (log_size
&& !log_buf
)
9371 if (prog
->log_size
> UINT_MAX
)
9373 if (prog
->obj
->loaded
)
9376 prog
->log_buf
= log_buf
;
9377 prog
->log_size
= log_size
;
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, \
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
);
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
),
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
)
9514 struct bpf_sec_def
*sec_def
;
9516 if (!OPTS_VALID(opts
, libbpf_prog_handler_opts
))
9517 return libbpf_err(-EINVAL
);
9519 if (last_custom_sec_def_handler_id
== INT_MAX
) /* prevent overflow */
9520 return libbpf_err(-E2BIG
);
9523 sec_def
= libbpf_reallocarray(custom_sec_defs
, custom_sec_def_cnt
+ 1,
9526 return libbpf_err(-ENOMEM
);
9528 custom_sec_defs
= sec_def
;
9529 sec_def
= &custom_sec_defs
[custom_sec_def_cnt
];
9531 if (has_custom_fallback_def
)
9532 return libbpf_err(-EBUSY
);
9534 sec_def
= &custom_fallback_def
;
9537 sec_def
->sec
= sec
? strdup(sec
) : NULL
;
9538 if (sec
&& !sec_def
->sec
)
9539 return libbpf_err(-ENOMEM
);
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);
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
);
9549 sec_def
->handler_id
= ++last_custom_sec_def_handler_id
;
9552 custom_sec_def_cnt
++;
9554 has_custom_fallback_def
= true;
9556 return sec_def
->handler_id
;
9559 int libbpf_unregister_prog_handler(int handler_id
)
9561 struct bpf_sec_def
*sec_defs
;
9564 if (handler_id
<= 0)
9565 return libbpf_err(-EINVAL
);
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;
9573 for (i
= 0; i
< custom_sec_def_cnt
; i
++) {
9574 if (custom_sec_defs
[i
].handler_id
== handler_id
)
9578 if (i
== custom_sec_def_cnt
)
9579 return libbpf_err(-ENOENT
);
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
--;
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)
9592 if (sec_defs
|| custom_sec_def_cnt
== 0)
9593 custom_sec_defs
= sec_defs
;
9598 static bool sec_def_matches(const struct bpf_sec_def
*sec_def
, const char *sec_name
)
9600 size_t len
= strlen(sec_def
->sec
);
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
))
9609 /* "type+" means it can be either exact SEC("type") or
9610 * well-formed SEC("type/extras") with proper '/' separator
9612 if (sec_def
->sec
[len
- 1] == '+') {
9614 /* not even a prefix */
9615 if (strncmp(sec_name
, sec_def
->sec
, len
) != 0)
9617 /* exact match or has '/' separator */
9618 if (sec_name
[len
] == '\0' || sec_name
[len
] == '/')
9623 return strcmp(sec_name
, sec_def
->sec
) == 0;
9626 static const struct bpf_sec_def
*find_sec_def(const char *sec_name
)
9628 const struct bpf_sec_def
*sec_def
;
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
))
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
))
9645 if (has_custom_fallback_def
)
9646 return &custom_fallback_def
;
9651 #define MAX_TYPE_NAME_SIZE 32
9653 static char *libbpf_get_type_names(bool attach_type
)
9655 int i
, len
= ARRAY_SIZE(section_defs
) * MAX_TYPE_NAME_SIZE
;
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
];
9668 if (sec_def
->prog_prepare_load_fn
!= libbpf_prepare_prog_load
)
9671 if (!(sec_def
->cookie
& SEC_ATTACHABLE
))
9675 if (strlen(buf
) + strlen(section_defs
[i
].sec
) + 2 > len
) {
9680 strcat(buf
, section_defs
[i
].sec
);
9686 int libbpf_prog_type_by_name(const char *name
, enum bpf_prog_type
*prog_type
,
9687 enum bpf_attach_type
*expected_attach_type
)
9689 const struct bpf_sec_def
*sec_def
;
9693 return libbpf_err(-EINVAL
);
9695 sec_def
= find_sec_def(name
);
9697 *prog_type
= sec_def
->prog_type
;
9698 *expected_attach_type
= sec_def
->expected_attach_type
;
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
);
9709 return libbpf_err(-ESRCH
);
9712 const char *libbpf_bpf_attach_type_str(enum bpf_attach_type t
)
9714 if (t
< 0 || t
>= ARRAY_SIZE(attach_type_name
))
9717 return attach_type_name
[t
];
9720 const char *libbpf_bpf_link_type_str(enum bpf_link_type t
)
9722 if (t
< 0 || t
>= ARRAY_SIZE(link_type_name
))
9725 return link_type_name
[t
];
9728 const char *libbpf_bpf_map_type_str(enum bpf_map_type t
)
9730 if (t
< 0 || t
>= ARRAY_SIZE(map_type_name
))
9733 return map_type_name
[t
];
9736 const char *libbpf_bpf_prog_type_str(enum bpf_prog_type t
)
9738 if (t
< 0 || t
>= ARRAY_SIZE(prog_type_name
))
9741 return prog_type_name
[t
];
9744 static struct bpf_map
*find_struct_ops_map_by_offset(struct bpf_object
*obj
,
9748 struct bpf_map
*map
;
9751 for (i
= 0; i
< obj
->nr_maps
; i
++) {
9752 map
= &obj
->maps
[i
];
9753 if (!bpf_map__is_struct_ops(map
))
9755 if (map
->sec_idx
== sec_idx
&&
9756 map
->sec_offset
<= offset
&&
9757 offset
- map
->sec_offset
< map
->def
.value_size
)
9764 /* Collect the reloc from ELF, populate the st_ops->progs[], and update
9765 * st_ops->data for shadow type.
9767 static int bpf_object__collect_st_ops_relos(struct bpf_object
*obj
,
9768 Elf64_Shdr
*shdr
, Elf_Data
*data
)
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
;
9785 nrels
= shdr
->sh_size
/ shdr
->sh_entsize
;
9786 for (i
= 0; i
< nrels
; i
++) {
9787 rel
= elf_rel_by_idx(data
, i
);
9789 pr_warn("struct_ops reloc: failed to get %d reloc\n", i
);
9790 return -LIBBPF_ERRNO__FORMAT
;
9793 sym
= elf_sym_by_idx(obj
, ELF64_R_SYM(rel
->r_info
));
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
;
9800 name
= elf_sym_str(obj
, sym
->st_name
) ?: "<?>";
9801 map
= find_struct_ops_map_by_offset(obj
, shdr
->sh_info
, rel
->r_offset
);
9803 pr_warn("struct_ops reloc: cannot find map at rel->r_offset %zu\n",
9804 (size_t)rel
->r_offset
);
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",
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
);
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
;
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
;
9828 insn_idx
= sym
->st_value
/ BPF_INSN_SZ
;
9830 type
= btf__type_by_id(btf
, st_ops
->type_id
);
9831 member
= find_member_by_offset(type
, moff
* 8);
9833 pr_warn("struct_ops reloc %s: cannot find member at moff %u\n",
9837 member_idx
= member
- btf_members(type
);
9838 name
= btf__name_by_offset(btf
, member
->name_off
);
9840 if (!resolve_func_ptr(btf
, member
->type
, NULL
)) {
9841 pr_warn("struct_ops reloc %s: cannot relocate non func ptr %s\n",
9846 prog
= find_prog_by_sec_insn(obj
, shdr_idx
, insn_idx
);
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
);
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
);
9860 st_ops
->progs
[member_idx
] = prog
;
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.
9868 *((struct bpf_program
**)(st_ops
->data
+ moff
)) = prog
;
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
9879 void btf_get_kernel_prefix_kind(enum bpf_attach_type attach_type
,
9880 const char **prefix
, int *kind
)
9882 switch (attach_type
) {
9883 case BPF_TRACE_RAW_TP
:
9884 *prefix
= BTF_TRACE_PREFIX
;
9885 *kind
= BTF_KIND_TYPEDEF
;
9888 case BPF_LSM_CGROUP
:
9889 *prefix
= BTF_LSM_PREFIX
;
9890 *kind
= BTF_KIND_FUNC
;
9892 case BPF_TRACE_ITER
:
9893 *prefix
= BTF_ITER_PREFIX
;
9894 *kind
= BTF_KIND_FUNC
;
9898 *kind
= BTF_KIND_FUNC
;
9902 static int find_btf_by_prefix_kind(const struct btf
*btf
, const char *prefix
,
9903 const char *name
, __u32 kind
)
9905 char btf_type_name
[BTF_MAX_NAME_SIZE
];
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.
9914 if (ret
< 0 || ret
>= sizeof(btf_type_name
))
9915 return -ENAMETOOLONG
;
9916 return btf__find_by_name_kind(btf
, btf_type_name
, kind
);
9919 static inline int find_attach_btf_id(struct btf
*btf
, const char *name
,
9920 enum bpf_attach_type attach_type
)
9925 btf_get_kernel_prefix_kind(attach_type
, &prefix
, &kind
);
9926 return find_btf_by_prefix_kind(btf
, prefix
, name
, kind
);
9929 int libbpf_find_vmlinux_btf_id(const char *name
,
9930 enum bpf_attach_type attach_type
)
9935 btf
= btf__load_vmlinux_btf();
9936 err
= libbpf_get_error(btf
);
9938 pr_warn("vmlinux BTF is not found\n");
9939 return libbpf_err(err
);
9942 err
= find_attach_btf_id(btf
, name
, attach_type
);
9944 pr_warn("%s is not found in vmlinux BTF\n", name
);
9947 return libbpf_err(err
);
9950 static int libbpf_find_prog_btf_id(const char *name
, __u32 attach_prog_fd
)
9952 struct bpf_prog_info info
;
9953 __u32 info_len
= sizeof(info
);
9957 memset(&info
, 0, info_len
);
9958 err
= bpf_prog_get_info_by_fd(attach_prog_fd
, &info
, &info_len
);
9960 pr_warn("failed bpf_prog_get_info_by_fd for FD %d: %s\n",
9961 attach_prog_fd
, errstr(err
));
9967 pr_warn("The target program doesn't have BTF\n");
9970 btf
= btf__load_from_kernel_by_id(info
.btf_id
);
9971 err
= libbpf_get_error(btf
);
9973 pr_warn("Failed to get BTF %d of the program: %s\n", info
.btf_id
, errstr(err
));
9976 err
= btf__find_by_name_kind(btf
, name
, BTF_KIND_FUNC
);
9979 pr_warn("%s is not found in prog's BTF\n", name
);
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
)
9990 int ret
, i
, mod_len
;
9991 const char *fn_name
, *mod_name
= NULL
;
9993 fn_name
= strchr(attach_name
, ':');
9995 mod_name
= attach_name
;
9996 mod_len
= fn_name
- mod_name
;
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
,
10005 *btf_obj_fd
= 0; /* vmlinux BTF */
10006 *btf_type_id
= ret
;
10009 if (ret
!= -ENOENT
)
10013 ret
= load_module_btfs(obj
);
10017 for (i
= 0; i
< obj
->btf_module_cnt
; i
++) {
10018 const struct module_btf
*mod
= &obj
->btf_modules
[i
];
10020 if (mod_name
&& strncmp(mod
->name
, mod_name
, mod_len
) != 0)
10023 ret
= find_attach_btf_id(mod
->btf
,
10024 mod_name
? fn_name
: attach_name
,
10027 *btf_obj_fd
= mod
->fd
;
10028 *btf_type_id
= ret
;
10031 if (ret
== -ENOENT
)
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
)
10043 enum bpf_attach_type attach_type
= prog
->expected_attach_type
;
10044 __u32 attach_prog_fd
= prog
->attach_prog_fd
;
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
);
10053 err
= libbpf_find_prog_btf_id(attach_name
, attach_prog_fd
);
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
));
10060 *btf_type_id
= err
;
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
);
10070 err
= find_kernel_btf_id(prog
->obj
, attach_name
,
10071 attach_type
, btf_obj_fd
,
10075 pr_warn("prog '%s': failed to find kernel BTF type ID of '%s': %s\n",
10076 prog
->name
, attach_name
, errstr(err
));
10082 int libbpf_attach_type_by_name(const char *name
,
10083 enum bpf_attach_type
*attach_type
)
10086 const struct bpf_sec_def
*sec_def
;
10089 return libbpf_err(-EINVAL
);
10091 sec_def
= find_sec_def(name
);
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
);
10100 return libbpf_err(-EINVAL
);
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
);
10108 *attach_type
= sec_def
->expected_attach_type
;
10112 int bpf_map__fd(const struct bpf_map
*map
)
10115 return libbpf_err(-EINVAL
);
10116 if (!map_is_created(map
))
10121 static bool map_uses_real_name(const struct bpf_map
*map
)
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.
10129 if (map
->libbpf_type
== LIBBPF_MAP_DATA
&& strcmp(map
->real_name
, DATA_SEC
) != 0)
10131 if (map
->libbpf_type
== LIBBPF_MAP_RODATA
&& strcmp(map
->real_name
, RODATA_SEC
) != 0)
10136 const char *bpf_map__name(const struct bpf_map
*map
)
10141 if (map_uses_real_name(map
))
10142 return map
->real_name
;
10147 enum bpf_map_type
bpf_map__type(const struct bpf_map
*map
)
10149 return map
->def
.type
;
10152 int bpf_map__set_type(struct bpf_map
*map
, enum bpf_map_type type
)
10154 if (map_is_created(map
))
10155 return libbpf_err(-EBUSY
);
10156 map
->def
.type
= type
;
10160 __u32
bpf_map__map_flags(const struct bpf_map
*map
)
10162 return map
->def
.map_flags
;
10165 int bpf_map__set_map_flags(struct bpf_map
*map
, __u32 flags
)
10167 if (map_is_created(map
))
10168 return libbpf_err(-EBUSY
);
10169 map
->def
.map_flags
= flags
;
10173 __u64
bpf_map__map_extra(const struct bpf_map
*map
)
10175 return map
->map_extra
;
10178 int bpf_map__set_map_extra(struct bpf_map
*map
, __u64 map_extra
)
10180 if (map_is_created(map
))
10181 return libbpf_err(-EBUSY
);
10182 map
->map_extra
= map_extra
;
10186 __u32
bpf_map__numa_node(const struct bpf_map
*map
)
10188 return map
->numa_node
;
10191 int bpf_map__set_numa_node(struct bpf_map
*map
, __u32 numa_node
)
10193 if (map_is_created(map
))
10194 return libbpf_err(-EBUSY
);
10195 map
->numa_node
= numa_node
;
10199 __u32
bpf_map__key_size(const struct bpf_map
*map
)
10201 return map
->def
.key_size
;
10204 int bpf_map__set_key_size(struct bpf_map
*map
, __u32 size
)
10206 if (map_is_created(map
))
10207 return libbpf_err(-EBUSY
);
10208 map
->def
.key_size
= size
;
10212 __u32
bpf_map__value_size(const struct bpf_map
*map
)
10214 return map
->def
.value_size
;
10217 static int map_btf_datasec_resize(struct bpf_map
*map
, __u32 size
)
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
;
10227 /* check btf existence */
10228 btf
= bpf_object__btf(map
->obj
);
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
));
10240 /* verify datasec has at least one var */
10241 vlen
= btf_vlen(datasec_type
);
10243 pr_warn("map '%s': cannot be resized, map value datasec is empty\n",
10244 bpf_map__name(map
));
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
));
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
);
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
;
10273 /* adding a new btf type invalidates existing pointers to btf objects,
10274 * so refresh pointers before proceeding
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
);
10280 /* finally update btf info */
10281 datasec_type
->size
= size
;
10282 var
->size
= size
- var
->offset
;
10283 var_type
->type
= new_array_id
;
10288 int bpf_map__set_value_size(struct bpf_map
*map
, __u32 size
)
10290 if (map
->obj
->loaded
|| map
->reused
)
10291 return libbpf_err(-EBUSY
);
10294 size_t mmap_old_sz
, mmap_new_sz
;
10297 if (map
->def
.type
!= BPF_MAP_TYPE_ARRAY
)
10298 return -EOPNOTSUPP
;
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
);
10304 pr_warn("map '%s': failed to resize memory-mapped region: %s\n",
10305 bpf_map__name(map
), errstr(err
));
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;
10317 map
->def
.value_size
= size
;
10321 __u32
bpf_map__btf_key_type_id(const struct bpf_map
*map
)
10323 return map
? map
->btf_key_type_id
: 0;
10326 __u32
bpf_map__btf_value_type_id(const struct bpf_map
*map
)
10328 return map
? map
->btf_value_type_id
: 0;
10331 int bpf_map__set_initial_value(struct bpf_map
*map
,
10332 const void *data
, size_t size
)
10336 if (map
->obj
->loaded
|| map
->reused
)
10337 return libbpf_err(-EBUSY
);
10339 if (!map
->mmaped
|| map
->libbpf_type
== LIBBPF_MAP_KCONFIG
)
10340 return libbpf_err(-EINVAL
);
10342 if (map
->def
.type
== BPF_MAP_TYPE_ARENA
)
10343 actual_sz
= map
->obj
->arena_data_sz
;
10345 actual_sz
= map
->def
.value_size
;
10346 if (size
!= actual_sz
)
10347 return libbpf_err(-EINVAL
);
10349 memcpy(map
->mmaped
, data
, size
);
10353 void *bpf_map__initial_value(const struct bpf_map
*map
, size_t *psize
)
10355 if (bpf_map__is_struct_ops(map
)) {
10357 *psize
= map
->def
.value_size
;
10358 return map
->st_ops
->data
;
10364 if (map
->def
.type
== BPF_MAP_TYPE_ARENA
)
10365 *psize
= map
->obj
->arena_data_sz
;
10367 *psize
= map
->def
.value_size
;
10369 return map
->mmaped
;
10372 bool bpf_map__is_internal(const struct bpf_map
*map
)
10374 return map
->libbpf_type
!= LIBBPF_MAP_UNSPEC
;
10377 __u32
bpf_map__ifindex(const struct bpf_map
*map
)
10379 return map
->map_ifindex
;
10382 int bpf_map__set_ifindex(struct bpf_map
*map
, __u32 ifindex
)
10384 if (map_is_created(map
))
10385 return libbpf_err(-EBUSY
);
10386 map
->map_ifindex
= ifindex
;
10390 int bpf_map__set_inner_map_fd(struct bpf_map
*map
, int fd
)
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
);
10396 if (map
->inner_map_fd
!= -1) {
10397 pr_warn("error: inner_map_fd already specified\n");
10398 return libbpf_err(-EINVAL
);
10400 if (map
->inner_map
) {
10401 bpf_map__destroy(map
->inner_map
);
10402 zfree(&map
->inner_map
);
10404 map
->inner_map_fd
= fd
;
10408 static struct bpf_map
*
10409 __bpf_map__iter(const struct bpf_map
*m
, const struct bpf_object
*obj
, int i
)
10412 struct bpf_map
*s
, *e
;
10414 if (!obj
|| !obj
->maps
)
10415 return errno
= EINVAL
, NULL
;
10418 e
= obj
->maps
+ obj
->nr_maps
;
10420 if ((m
< s
) || (m
>= e
)) {
10421 pr_warn("error in %s: map handler doesn't belong to object\n",
10423 return errno
= EINVAL
, NULL
;
10426 idx
= (m
- obj
->maps
) + i
;
10427 if (idx
>= obj
->nr_maps
|| idx
< 0)
10429 return &obj
->maps
[idx
];
10433 bpf_object__next_map(const struct bpf_object
*obj
, const struct bpf_map
*prev
)
10435 if (prev
== NULL
&& obj
!= NULL
)
10438 return __bpf_map__iter(prev
, obj
, 1);
10442 bpf_object__prev_map(const struct bpf_object
*obj
, const struct bpf_map
*next
)
10444 if (next
== NULL
&& obj
!= NULL
) {
10447 return obj
->maps
+ obj
->nr_maps
- 1;
10450 return __bpf_map__iter(next
, obj
, -1);
10454 bpf_object__find_map_by_name(const struct bpf_object
*obj
, const char *name
)
10456 struct bpf_map
*pos
;
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)
10463 if (name
[0] == '.') {
10464 if (pos
->real_name
&& strcmp(pos
->real_name
, name
) == 0)
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)
10474 if (strcmp(pos
->name
, name
) == 0)
10477 return errno
= ENOENT
, NULL
;
10481 bpf_object__find_map_fd_by_name(const struct bpf_object
*obj
, const char *name
)
10483 return bpf_map__fd(bpf_object__find_map_by_name(obj
, name
));
10486 static int validate_map_op(const struct bpf_map
*map
, size_t key_sz
,
10487 size_t value_sz
, bool check_value_sz
)
10489 if (!map_is_created(map
)) /* map is not yet created */
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
);
10499 pr_warn("map '%s': can't use BPF map without FD (was it created?)\n", map
->name
);
10503 if (!check_value_sz
)
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);
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
);
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
);
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
)
10538 err
= validate_map_op(map
, key_sz
, value_sz
, true);
10540 return libbpf_err(err
);
10542 return bpf_map_lookup_elem_flags(map
->fd
, key
, value
, 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
)
10551 err
= validate_map_op(map
, key_sz
, value_sz
, true);
10553 return libbpf_err(err
);
10555 return bpf_map_update_elem(map
->fd
, key
, value
, flags
);
10558 int bpf_map__delete_elem(const struct bpf_map
*map
,
10559 const void *key
, size_t key_sz
, __u64 flags
)
10563 err
= validate_map_op(map
, key_sz
, 0, false /* check_value_sz */);
10565 return libbpf_err(err
);
10567 return bpf_map_delete_elem_flags(map
->fd
, key
, 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
)
10576 err
= validate_map_op(map
, key_sz
, value_sz
, true);
10578 return libbpf_err(err
);
10580 return bpf_map_lookup_and_delete_elem_flags(map
->fd
, key
, value
, flags
);
10583 int bpf_map__get_next_key(const struct bpf_map
*map
,
10584 const void *cur_key
, void *next_key
, size_t key_sz
)
10588 err
= validate_map_op(map
, key_sz
, 0, false /* check_value_sz */);
10590 return libbpf_err(err
);
10592 return bpf_map_get_next_key(map
->fd
, cur_key
, next_key
);
10595 long libbpf_get_error(const void *ptr
)
10597 if (!IS_ERR_OR_NULL(ptr
))
10601 errno
= -PTR_ERR(ptr
);
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
10611 /* Replace link's underlying BPF program with the new one */
10612 int bpf_link__update_program(struct bpf_link
*link
, struct bpf_program
*prog
)
10615 int prog_fd
= bpf_program__fd(prog
);
10618 pr_warn("prog '%s': can't use BPF program without FD (was it loaded?)\n",
10620 return libbpf_err(-EINVAL
);
10623 ret
= bpf_link_update(bpf_link__fd(link
), prog_fd
, NULL
);
10624 return libbpf_err_errno(ret
);
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.
10637 void bpf_link__disconnect(struct bpf_link
*link
)
10639 link
->disconnected
= true;
10642 int bpf_link__destroy(struct bpf_link
*link
)
10646 if (IS_ERR_OR_NULL(link
))
10649 if (!link
->disconnected
&& link
->detach
)
10650 err
= link
->detach(link
);
10651 if (link
->pin_path
)
10652 free(link
->pin_path
);
10654 link
->dealloc(link
);
10658 return libbpf_err(err
);
10661 int bpf_link__fd(const struct bpf_link
*link
)
10666 const char *bpf_link__pin_path(const struct bpf_link
*link
)
10668 return link
->pin_path
;
10671 static int bpf_link__detach_fd(struct bpf_link
*link
)
10673 return libbpf_err_errno(close(link
->fd
));
10676 struct bpf_link
*bpf_link__open(const char *path
)
10678 struct bpf_link
*link
;
10681 fd
= bpf_obj_get(path
);
10684 pr_warn("failed to open link at %s: %d\n", path
, fd
);
10685 return libbpf_err_ptr(fd
);
10688 link
= calloc(1, sizeof(*link
));
10691 return libbpf_err_ptr(-ENOMEM
);
10693 link
->detach
= &bpf_link__detach_fd
;
10696 link
->pin_path
= strdup(path
);
10697 if (!link
->pin_path
) {
10698 bpf_link__destroy(link
);
10699 return libbpf_err_ptr(-ENOMEM
);
10705 int bpf_link__detach(struct bpf_link
*link
)
10707 return bpf_link_detach(link
->fd
) ? -errno
: 0;
10710 int bpf_link__pin(struct bpf_link
*link
, const char *path
)
10714 if (link
->pin_path
)
10715 return libbpf_err(-EBUSY
);
10716 err
= make_parent_dir(path
);
10718 return libbpf_err(err
);
10719 err
= check_path(path
);
10721 return libbpf_err(err
);
10723 link
->pin_path
= strdup(path
);
10724 if (!link
->pin_path
)
10725 return libbpf_err(-ENOMEM
);
10727 if (bpf_obj_pin(link
->fd
, link
->pin_path
)) {
10729 zfree(&link
->pin_path
);
10730 return libbpf_err(err
);
10733 pr_debug("link fd=%d: pinned at %s\n", link
->fd
, link
->pin_path
);
10737 int bpf_link__unpin(struct bpf_link
*link
)
10741 if (!link
->pin_path
)
10742 return libbpf_err(-EINVAL
);
10744 err
= unlink(link
->pin_path
);
10748 pr_debug("link fd=%d: unpinned from %s\n", link
->fd
, link
->pin_path
);
10749 zfree(&link
->pin_path
);
10753 struct bpf_link_perf
{
10754 struct bpf_link link
;
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
;
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
);
10765 static int bpf_link_perf_detach(struct bpf_link
*link
)
10767 struct bpf_link_perf
*perf_link
= container_of(link
, struct bpf_link_perf
, link
);
10770 if (ioctl(perf_link
->perf_event_fd
, PERF_EVENT_IOC_DISABLE
, 0) < 0)
10773 if (perf_link
->perf_event_fd
!= link
->fd
)
10774 close(perf_link
->perf_event_fd
);
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
);
10783 err
= remove_uprobe_event_legacy(perf_link
->legacy_probe_name
,
10784 perf_link
->legacy_is_retprobe
);
10791 static void bpf_link_perf_dealloc(struct bpf_link
*link
)
10793 struct bpf_link_perf
*perf_link
= container_of(link
, struct bpf_link_perf
, link
);
10795 free(perf_link
->legacy_probe_name
);
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
)
10802 struct bpf_link_perf
*link
;
10803 int prog_fd
, link_fd
= -1, err
;
10804 bool force_ioctl_attach
;
10806 if (!OPTS_VALID(opts
, bpf_perf_event_opts
))
10807 return libbpf_err_ptr(-EINVAL
);
10810 pr_warn("prog '%s': invalid perf event FD %d\n",
10812 return libbpf_err_ptr(-EINVAL
);
10814 prog_fd
= bpf_program__fd(prog
);
10816 pr_warn("prog '%s': can't attach BPF program without FD (was it loaded?)\n",
10818 return libbpf_err_ptr(-EINVAL
);
10821 link
= calloc(1, sizeof(*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
;
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));
10833 link_fd
= bpf_link_create(prog_fd
, pfd
, BPF_PERF_EVENT
, &link_opts
);
10836 pr_warn("prog '%s': failed to create BPF link for perf_event FD %d: %s\n",
10837 prog
->name
, pfd
, errstr(err
));
10840 link
->link
.fd
= link_fd
;
10842 if (OPTS_GET(opts
, bpf_cookie
, 0)) {
10843 pr_warn("prog '%s': user context value is not supported\n", prog
->name
);
10848 if (ioctl(pfd
, PERF_EVENT_IOC_SET_BPF
, prog_fd
) < 0) {
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",
10857 link
->link
.fd
= pfd
;
10859 if (ioctl(pfd
, PERF_EVENT_IOC_ENABLE
, 0) < 0) {
10861 pr_warn("prog '%s': failed to enable perf_event FD %d: %s\n",
10862 prog
->name
, pfd
, errstr(err
));
10866 return &link
->link
;
10871 return libbpf_err_ptr(err
);
10874 struct bpf_link
*bpf_program__attach_perf_event(const struct bpf_program
*prog
, int pfd
)
10876 return bpf_program__attach_perf_event_opts(prog
, pfd
, NULL
);
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
10884 static int parse_uint_from_file(const char *file
, const char *fmt
)
10889 f
= fopen(file
, "re");
10892 pr_debug("failed to open '%s': %s\n", file
, errstr(err
));
10895 err
= fscanf(f
, fmt
, &ret
);
10897 err
= err
== EOF
? -EIO
: -errno
;
10898 pr_debug("failed to parse '%s': %s\n", file
, errstr(err
));
10906 static int determine_kprobe_perf_type(void)
10908 const char *file
= "/sys/bus/event_source/devices/kprobe/type";
10910 return parse_uint_from_file(file
, "%d\n");
10913 static int determine_uprobe_perf_type(void)
10915 const char *file
= "/sys/bus/event_source/devices/uprobe/type";
10917 return parse_uint_from_file(file
, "%d\n");
10920 static int determine_kprobe_retprobe_bit(void)
10922 const char *file
= "/sys/bus/event_source/devices/kprobe/format/retprobe";
10924 return parse_uint_from_file(file
, "config:%d\n");
10927 static int determine_uprobe_retprobe_bit(void)
10929 const char *file
= "/sys/bus/event_source/devices/uprobe/format/retprobe";
10931 return parse_uint_from_file(file
, "config:%d\n");
10934 #define PERF_UPROBE_REF_CTR_OFFSET_BITS 32
10935 #define PERF_UPROBE_REF_CTR_OFFSET_SHIFT 32
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
)
10940 const size_t attr_sz
= sizeof(struct perf_event_attr
);
10941 struct perf_event_attr attr
;
10944 if ((__u64
)ref_ctr_off
>= (1ULL << PERF_UPROBE_REF_CTR_OFFSET_BITS
))
10947 memset(&attr
, 0, attr_sz
);
10949 type
= uprobe
? determine_uprobe_perf_type()
10950 : determine_kprobe_perf_type();
10952 pr_warn("failed to determine %s perf type: %s\n",
10953 uprobe
? "uprobe" : "kprobe",
10958 int bit
= uprobe
? determine_uprobe_retprobe_bit()
10959 : determine_kprobe_retprobe_bit();
10962 pr_warn("failed to determine %s retprobe bit: %s\n",
10963 uprobe
? "uprobe" : "kprobe",
10967 attr
.config
|= 1 << bit
;
10969 attr
.size
= attr_sz
;
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 */
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
;
10983 static int append_to_file(const char *file
, const char *fmt
, ...)
10985 int fd
, n
, err
= 0;
10990 n
= vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
10993 if (n
< 0 || n
>= sizeof(buf
))
10996 fd
= open(file
, O_WRONLY
| O_APPEND
| O_CLOEXEC
, 0);
11000 if (write(fd
, buf
, n
) < 0)
11007 #define DEBUGFS "/sys/kernel/debug/tracing"
11008 #define TRACEFS "/sys/kernel/tracing"
11010 static bool use_debugfs(void)
11012 static int has_debugfs
= -1;
11014 if (has_debugfs
< 0)
11015 has_debugfs
= faccessat(AT_FDCWD
, DEBUGFS
, F_OK
, AT_EACCESS
) == 0;
11017 return has_debugfs
== 1;
11020 static const char *tracefs_path(void)
11022 return use_debugfs() ? DEBUGFS
: TRACEFS
;
11025 static const char *tracefs_kprobe_events(void)
11027 return use_debugfs() ? DEBUGFS
"/kprobe_events" : TRACEFS
"/kprobe_events";
11030 static const char *tracefs_uprobe_events(void)
11032 return use_debugfs() ? DEBUGFS
"/uprobe_events" : TRACEFS
"/uprobe_events";
11035 static const char *tracefs_available_filter_functions(void)
11037 return use_debugfs() ? DEBUGFS
"/available_filter_functions"
11038 : TRACEFS
"/available_filter_functions";
11041 static const char *tracefs_available_filter_functions_addrs(void)
11043 return use_debugfs() ? DEBUGFS
"/available_filter_functions_addrs"
11044 : TRACEFS
"/available_filter_functions_addrs";
11047 static void gen_kprobe_legacy_event_name(char *buf
, size_t buf_sz
,
11048 const char *kfunc_name
, size_t offset
)
11050 static int index
= 0;
11053 snprintf(buf
, buf_sz
, "libbpf_%u_%s_0x%zx_%d", getpid(), kfunc_name
, offset
,
11054 __sync_fetch_and_add(&index
, 1));
11056 /* sanitize binary_path in the probe name */
11057 for (i
= 0; buf
[i
]; i
++) {
11058 if (!isalnum(buf
[i
]))
11063 static int add_kprobe_event_legacy(const char *probe_name
, bool retprobe
,
11064 const char *kfunc_name
, size_t offset
)
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
);
11072 static int remove_kprobe_event_legacy(const char *probe_name
, bool retprobe
)
11074 return append_to_file(tracefs_kprobe_events(), "-:%s/%s",
11075 retprobe
? "kretprobes" : "kprobes", probe_name
);
11078 static int determine_kprobe_perf_type_legacy(const char *probe_name
, bool retprobe
)
11082 snprintf(file
, sizeof(file
), "%s/events/%s/%s/id",
11083 tracefs_path(), retprobe
? "kretprobes" : "kprobes", probe_name
);
11085 return parse_uint_from_file(file
, "%d\n");
11088 static int perf_event_kprobe_open_legacy(const char *probe_name
, bool retprobe
,
11089 const char *kfunc_name
, size_t offset
, int pid
)
11091 const size_t attr_sz
= sizeof(struct perf_event_attr
);
11092 struct perf_event_attr attr
;
11093 int type
, pfd
, err
;
11095 err
= add_kprobe_event_legacy(probe_name
, retprobe
, kfunc_name
, offset
);
11097 pr_warn("failed to add legacy kprobe event for '%s+0x%zx': %s\n",
11098 kfunc_name
, offset
,
11102 type
= determine_kprobe_perf_type_legacy(probe_name
, retprobe
);
11105 pr_warn("failed to determine legacy kprobe event id for '%s+0x%zx': %s\n",
11106 kfunc_name
, offset
,
11108 goto err_clean_legacy
;
11111 memset(&attr
, 0, attr_sz
);
11112 attr
.size
= attr_sz
;
11113 attr
.config
= type
;
11114 attr
.type
= PERF_TYPE_TRACEPOINT
;
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
);
11122 pr_warn("legacy kprobe perf_event_open() failed: %s\n",
11124 goto err_clean_legacy
;
11129 /* Clear the newly added legacy kprobe_event */
11130 remove_kprobe_event_legacy(probe_name
, retprobe
);
11134 static const char *arch_specific_syscall_pfx(void)
11136 #if defined(__x86_64__)
11138 #elif defined(__i386__)
11140 #elif defined(__s390x__)
11142 #elif defined(__s390__)
11144 #elif defined(__arm__)
11146 #elif defined(__aarch64__)
11148 #elif defined(__mips__)
11150 #elif defined(__riscv)
11152 #elif defined(__powerpc__)
11154 #elif defined(__powerpc64__)
11155 return "powerpc64";
11161 int probe_kern_syscall_wrapper(int token_fd
)
11163 char syscall_name
[64];
11164 const char *ksys_pfx
;
11166 ksys_pfx
= arch_specific_syscall_pfx();
11170 snprintf(syscall_name
, sizeof(syscall_name
), "__%s_sys_bpf", ksys_pfx
);
11172 if (determine_kprobe_perf_type() >= 0) {
11175 pfd
= perf_event_open_probe(false, false, syscall_name
, 0, getpid(), 0);
11179 return pfd
>= 0 ? 1 : 0;
11180 } else { /* legacy mode */
11181 char probe_name
[128];
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)
11187 (void)remove_kprobe_event_legacy(probe_name
, false);
11193 bpf_program__attach_kprobe_opts(const struct bpf_program
*prog
,
11194 const char *func_name
,
11195 const struct bpf_kprobe_opts
*opts
)
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
;
11202 bool retprobe
, legacy
;
11205 if (!OPTS_VALID(opts
, bpf_kprobe_opts
))
11206 return libbpf_err_ptr(-EINVAL
);
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);
11213 legacy
= determine_kprobe_perf_type() < 0;
11214 switch (attach_mode
) {
11215 case PROBE_ATTACH_MODE_LEGACY
:
11217 pe_opts
.force_ioctl_attach
= true;
11219 case PROBE_ATTACH_MODE_PERF
:
11221 return libbpf_err_ptr(-ENOTSUP
);
11222 pe_opts
.force_ioctl_attach
= true;
11224 case PROBE_ATTACH_MODE_LINK
:
11225 if (legacy
|| !kernel_supports(prog
->obj
, FEAT_PERF_LINK
))
11226 return libbpf_err_ptr(-ENOTSUP
);
11228 case PROBE_ATTACH_MODE_DEFAULT
:
11231 return libbpf_err_ptr(-EINVAL
);
11235 pfd
= perf_event_open_probe(false /* uprobe */, retprobe
,
11237 -1 /* pid */, 0 /* ref_ctr_off */);
11239 char probe_name
[256];
11241 gen_kprobe_legacy_event_name(probe_name
, sizeof(probe_name
),
11242 func_name
, offset
);
11244 legacy_probe
= strdup(probe_name
);
11246 return libbpf_err_ptr(-ENOMEM
);
11248 pfd
= perf_event_kprobe_open_legacy(legacy_probe
, retprobe
, func_name
,
11249 offset
, -1 /* pid */);
11253 pr_warn("prog '%s': failed to create %s '%s+0x%zx' perf event: %s\n",
11254 prog
->name
, retprobe
? "kretprobe" : "kprobe",
11259 link
= bpf_program__attach_perf_event_opts(prog
, pfd
, &pe_opts
);
11260 err
= libbpf_get_error(link
);
11263 pr_warn("prog '%s': failed to attach to %s '%s+0x%zx': %s\n",
11264 prog
->name
, retprobe
? "kretprobe" : "kprobe",
11267 goto err_clean_legacy
;
11270 struct bpf_link_perf
*perf_link
= container_of(link
, struct bpf_link_perf
, link
);
11272 perf_link
->legacy_probe_name
= legacy_probe
;
11273 perf_link
->legacy_is_kprobe
= true;
11274 perf_link
->legacy_is_retprobe
= retprobe
;
11281 remove_kprobe_event_legacy(legacy_probe
, retprobe
);
11283 free(legacy_probe
);
11284 return libbpf_err_ptr(err
);
11287 struct bpf_link
*bpf_program__attach_kprobe(const struct bpf_program
*prog
,
11289 const char *func_name
)
11291 DECLARE_LIBBPF_OPTS(bpf_kprobe_opts
, opts
,
11292 .retprobe
= retprobe
,
11295 return bpf_program__attach_kprobe_opts(prog
, func_name
, &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
)
11302 LIBBPF_OPTS(bpf_kprobe_opts
, kprobe_opts
);
11303 char func_name
[128];
11305 if (!OPTS_VALID(opts
, bpf_ksyscall_opts
))
11306 return libbpf_err_ptr(-EINVAL
);
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
11314 snprintf(func_name
, sizeof(func_name
), "__%s_sys_%s",
11315 arch_specific_syscall_pfx() ? : "", syscall_name
);
11317 snprintf(func_name
, sizeof(func_name
), "__se_sys_%s", syscall_name
);
11320 kprobe_opts
.retprobe
= OPTS_GET(opts
, retprobe
, false);
11321 kprobe_opts
.bpf_cookie
= OPTS_GET(opts
, bpf_cookie
, 0);
11323 return bpf_program__attach_kprobe_opts(prog
, func_name
, &kprobe_opts
);
11326 /* Adapted from perf/util/string.c */
11327 bool glob_match(const char *str
, const char *pat
)
11329 while (*str
&& *pat
&& *pat
!= '*') {
11330 if (*pat
== '?') { /* Matches any single character */
11340 /* Check wild card */
11342 while (*pat
== '*')
11344 if (!*pat
) /* Tail wild card matches all */
11347 if (glob_match(str
++, pat
))
11350 return !*str
&& !*pat
;
11353 struct kprobe_multi_resolve
{
11354 const char *pattern
;
11355 unsigned long *addrs
;
11360 struct avail_kallsyms_data
{
11363 struct kprobe_multi_resolve
*res
;
11366 static int avail_func_cmp(const void *a
, const void *b
)
11368 return strcmp(*(const char **)a
, *(const char **)b
);
11371 static int avail_kallsyms_cb(unsigned long long sym_addr
, char sym_type
,
11372 const char *sym_name
, void *ctx
)
11374 struct avail_kallsyms_data
*data
= ctx
;
11375 struct kprobe_multi_resolve
*res
= data
->res
;
11378 if (!bsearch(&sym_name
, data
->syms
, data
->cnt
, sizeof(*data
->syms
), avail_func_cmp
))
11381 err
= libbpf_ensure_mem((void **)&res
->addrs
, &res
->cap
, sizeof(*res
->addrs
), res
->cnt
+ 1);
11385 res
->addrs
[res
->cnt
++] = (unsigned long)sym_addr
;
11389 static int libbpf_available_kallsyms_parse(struct kprobe_multi_resolve
*res
)
11391 const char *available_functions_file
= tracefs_available_filter_functions();
11392 struct avail_kallsyms_data data
;
11393 char sym_name
[500];
11395 int err
= 0, ret
, i
;
11396 char **syms
= NULL
;
11397 size_t cap
= 0, cnt
= 0;
11399 f
= fopen(available_functions_file
, "re");
11402 pr_warn("failed to open %s: %s\n", available_functions_file
, errstr(err
));
11409 ret
= fscanf(f
, "%499s%*[^\n]\n", sym_name
);
11410 if (ret
== EOF
&& feof(f
))
11414 pr_warn("failed to parse available_filter_functions entry: %d\n", ret
);
11419 if (!glob_match(sym_name
, res
->pattern
))
11422 err
= libbpf_ensure_mem((void **)&syms
, &cap
, sizeof(*syms
), cnt
+ 1);
11426 name
= strdup(sym_name
);
11432 syms
[cnt
++] = name
;
11435 /* no entries found, bail out */
11441 /* sort available functions */
11442 qsort(syms
, cnt
, sizeof(*syms
), avail_func_cmp
);
11447 libbpf_kallsyms_parse(avail_kallsyms_cb
, &data
);
11453 for (i
= 0; i
< cnt
; i
++)
11454 free((char *)syms
[i
]);
11461 static bool has_available_filter_functions_addrs(void)
11463 return access(tracefs_available_filter_functions_addrs(), R_OK
) != -1;
11466 static int libbpf_available_kprobes_parse(struct kprobe_multi_resolve
*res
)
11468 const char *available_path
= tracefs_available_filter_functions_addrs();
11469 char sym_name
[500];
11472 unsigned long long sym_addr
;
11474 f
= fopen(available_path
, "re");
11477 pr_warn("failed to open %s: %s\n", available_path
, errstr(err
));
11482 ret
= fscanf(f
, "%llx %499s%*[^\n]\n", &sym_addr
, sym_name
);
11483 if (ret
== EOF
&& feof(f
))
11487 pr_warn("failed to parse available_filter_functions_addrs entry: %d\n",
11493 if (!glob_match(sym_name
, res
->pattern
))
11496 err
= libbpf_ensure_mem((void **)&res
->addrs
, &res
->cap
,
11497 sizeof(*res
->addrs
), res
->cnt
+ 1);
11501 res
->addrs
[res
->cnt
++] = (unsigned long)sym_addr
;
11513 bpf_program__attach_kprobe_multi_opts(const struct bpf_program
*prog
,
11514 const char *pattern
,
11515 const struct bpf_kprobe_multi_opts
*opts
)
11517 LIBBPF_OPTS(bpf_link_create_opts
, lopts
);
11518 struct kprobe_multi_resolve res
= {
11519 .pattern
= pattern
,
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
;
11530 if (!OPTS_VALID(opts
, bpf_kprobe_multi_opts
))
11531 return libbpf_err_ptr(-EINVAL
);
11533 prog_fd
= bpf_program__fd(prog
);
11535 pr_warn("prog '%s': can't attach BPF program without FD (was it loaded?)\n",
11537 return libbpf_err_ptr(-EINVAL
);
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);
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
);
11552 return libbpf_err_ptr(-EINVAL
);
11555 if (has_available_filter_functions_addrs())
11556 err
= libbpf_available_kprobes_parse(&res
);
11558 err
= libbpf_available_kallsyms_parse(&res
);
11565 retprobe
= OPTS_GET(opts
, retprobe
, false);
11566 session
= OPTS_GET(opts
, session
, false);
11568 if (retprobe
&& session
)
11569 return libbpf_err_ptr(-EINVAL
);
11571 attach_type
= session
? BPF_TRACE_KPROBE_SESSION
: BPF_TRACE_KPROBE_MULTI
;
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;
11579 link
= calloc(1, sizeof(*link
));
11584 link
->detach
= &bpf_link__detach_fd
;
11586 link_fd
= bpf_link_create(prog_fd
, 0, attach_type
, &lopts
);
11589 pr_warn("prog '%s': failed to attach: %s\n",
11590 prog
->name
, errstr(err
));
11593 link
->fd
= link_fd
;
11600 return libbpf_err_ptr(err
);
11603 static int attach_kprobe(const struct bpf_program
*prog
, long cookie
, struct bpf_link
**link
)
11605 DECLARE_LIBBPF_OPTS(bpf_kprobe_opts
, opts
);
11606 unsigned long offset
= 0;
11607 const char *func_name
;
11613 /* no auto-attach for SEC("kprobe") and SEC("kretprobe") */
11614 if (strcmp(prog
->sec_name
, "kprobe") == 0 || strcmp(prog
->sec_name
, "kretprobe") == 0)
11617 opts
.retprobe
= str_has_pfx(prog
->sec_name
, "kretprobe/");
11619 func_name
= prog
->sec_name
+ sizeof("kretprobe/") - 1;
11621 func_name
= prog
->sec_name
+ sizeof("kprobe/") - 1;
11623 n
= sscanf(func_name
, "%m[a-zA-Z0-9_.]+%li", &func
, &offset
);
11625 pr_warn("kprobe name is invalid: %s\n", func_name
);
11628 if (opts
.retprobe
&& offset
!= 0) {
11630 pr_warn("kretprobes do not support offset specification\n");
11634 opts
.offset
= offset
;
11635 *link
= bpf_program__attach_kprobe_opts(prog
, func
, &opts
);
11637 return libbpf_get_error(*link
);
11640 static int attach_ksyscall(const struct bpf_program
*prog
, long cookie
, struct bpf_link
**link
)
11642 LIBBPF_OPTS(bpf_ksyscall_opts
, opts
);
11643 const char *syscall_name
;
11647 /* no auto-attach for SEC("ksyscall") and SEC("kretsyscall") */
11648 if (strcmp(prog
->sec_name
, "ksyscall") == 0 || strcmp(prog
->sec_name
, "kretsyscall") == 0)
11651 opts
.retprobe
= str_has_pfx(prog
->sec_name
, "kretsyscall/");
11653 syscall_name
= prog
->sec_name
+ sizeof("kretsyscall/") - 1;
11655 syscall_name
= prog
->sec_name
+ sizeof("ksyscall/") - 1;
11657 *link
= bpf_program__attach_ksyscall(prog
, syscall_name
, &opts
);
11658 return *link
? 0 : -errno
;
11661 static int attach_kprobe_multi(const struct bpf_program
*prog
, long cookie
, struct bpf_link
**link
)
11663 LIBBPF_OPTS(bpf_kprobe_multi_opts
, opts
);
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)
11675 opts
.retprobe
= str_has_pfx(prog
->sec_name
, "kretprobe.multi/");
11677 spec
= prog
->sec_name
+ sizeof("kretprobe.multi/") - 1;
11679 spec
= prog
->sec_name
+ sizeof("kprobe.multi/") - 1;
11681 n
= sscanf(spec
, "%m[a-zA-Z0-9_.*?]", &pattern
);
11683 pr_warn("kprobe multi pattern is invalid: %s\n", spec
);
11687 *link
= bpf_program__attach_kprobe_multi_opts(prog
, pattern
, &opts
);
11689 return libbpf_get_error(*link
);
11692 static int attach_kprobe_session(const struct bpf_program
*prog
, long cookie
,
11693 struct bpf_link
**link
)
11695 LIBBPF_OPTS(bpf_kprobe_multi_opts
, opts
, .session
= true);
11702 /* no auto-attach for SEC("kprobe.session") */
11703 if (strcmp(prog
->sec_name
, "kprobe.session") == 0)
11706 spec
= prog
->sec_name
+ sizeof("kprobe.session/") - 1;
11707 n
= sscanf(spec
, "%m[a-zA-Z0-9_.*?]", &pattern
);
11709 pr_warn("kprobe session pattern is invalid: %s\n", spec
);
11713 *link
= bpf_program__attach_kprobe_multi_opts(prog
, pattern
, &opts
);
11715 return *link
? 0 : -errno
;
11718 static int attach_uprobe_multi(const struct bpf_program
*prog
, long cookie
, struct bpf_link
**link
)
11720 char *probe_type
= NULL
, *binary_path
= NULL
, *func_name
= NULL
;
11721 LIBBPF_OPTS(bpf_uprobe_multi_opts
, opts
);
11722 int n
, ret
= -EINVAL
;
11726 n
= sscanf(prog
->sec_name
, "%m[^/]/%m[^:]:%m[^\n]",
11727 &probe_type
, &binary_path
, &func_name
);
11730 /* handle SEC("u[ret]probe") - format is valid, but auto-attach is impossible. */
11734 opts
.session
= str_has_pfx(probe_type
, "uprobe.session");
11735 opts
.retprobe
= str_has_pfx(probe_type
, "uretprobe.multi");
11737 *link
= bpf_program__attach_uprobe_multi(prog
, -1, binary_path
, func_name
, &opts
);
11738 ret
= libbpf_get_error(*link
);
11741 pr_warn("prog '%s': invalid format of section definition '%s'\n", prog
->name
,
11751 static void gen_uprobe_legacy_event_name(char *buf
, size_t buf_sz
,
11752 const char *binary_path
, uint64_t offset
)
11756 snprintf(buf
, buf_sz
, "libbpf_%u_%s_0x%zx", getpid(), binary_path
, (size_t)offset
);
11758 /* sanitize binary_path in the probe name */
11759 for (i
= 0; buf
[i
]; i
++) {
11760 if (!isalnum(buf
[i
]))
11765 static inline int add_uprobe_event_legacy(const char *probe_name
, bool retprobe
,
11766 const char *binary_path
, size_t offset
)
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
);
11774 static inline int remove_uprobe_event_legacy(const char *probe_name
, bool retprobe
)
11776 return append_to_file(tracefs_uprobe_events(), "-:%s/%s",
11777 retprobe
? "uretprobes" : "uprobes", probe_name
);
11780 static int determine_uprobe_perf_type_legacy(const char *probe_name
, bool retprobe
)
11784 snprintf(file
, sizeof(file
), "%s/events/%s/%s/id",
11785 tracefs_path(), retprobe
? "uretprobes" : "uprobes", probe_name
);
11787 return parse_uint_from_file(file
, "%d\n");
11790 static int perf_event_uprobe_open_legacy(const char *probe_name
, bool retprobe
,
11791 const char *binary_path
, size_t offset
, int pid
)
11793 const size_t attr_sz
= sizeof(struct perf_event_attr
);
11794 struct perf_event_attr attr
;
11795 int type
, pfd
, err
;
11797 err
= add_uprobe_event_legacy(probe_name
, retprobe
, binary_path
, offset
);
11799 pr_warn("failed to add legacy uprobe event for %s:0x%zx: %s\n",
11800 binary_path
, (size_t)offset
, errstr(err
));
11803 type
= determine_uprobe_perf_type_legacy(probe_name
, retprobe
);
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
;
11811 memset(&attr
, 0, attr_sz
);
11812 attr
.size
= attr_sz
;
11813 attr
.config
= type
;
11814 attr
.type
= PERF_TYPE_TRACEPOINT
;
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
);
11822 pr_warn("legacy uprobe perf_event_open() failed: %s\n", errstr(err
));
11823 goto err_clean_legacy
;
11828 /* Clear the newly added legacy uprobe_event */
11829 remove_uprobe_event_legacy(probe_name
, retprobe
);
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.
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
11842 static long elf_find_func_offset_from_archive(const char *archive_path
, const char *file_name
,
11843 const char *func_name
)
11845 struct zip_archive
*archive
;
11846 struct zip_entry entry
;
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
);
11857 ret
= zip_archive_find_entry(archive
, file_name
, &entry
);
11859 pr_warn("zip: could not find archive member %s in %s: %ld\n", file_name
,
11860 archive_path
, ret
);
11863 pr_debug("zip: found entry for %s in %s at 0x%lx\n", file_name
, archive_path
,
11864 (unsigned long)entry
.data_offset
);
11866 if (entry
.compression
) {
11867 pr_warn("zip: entry %s of %s is compressed and cannot be handled\n", file_name
,
11869 ret
= -LIBBPF_ERRNO__FORMAT
;
11873 elf
= elf_memory((void *)entry
.data
, entry
.data_length
);
11875 pr_warn("elf: could not read elf file %s from %s: %s\n", file_name
, archive_path
,
11877 ret
= -LIBBPF_ERRNO__LIBELF
;
11881 ret
= elf_find_func_offset(elf
, file_name
, func_name
);
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
;
11891 zip_archive_close(archive
);
11895 static const char *arch_specific_lib_paths(void)
11898 * Based on https://packages.debian.org/sid/libc6.
11900 * Assume that the traced program is built for the same architecture
11901 * as libbpf, which should cover the vast majority of cases.
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";
11932 /* Get full path to program/shared library. */
11933 static int resolve_full_path(const char *file
, char *result
, size_t result_sz
)
11935 const char *search_paths
[3] = {};
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();
11944 search_paths
[0] = getenv("PATH");
11945 search_paths
[1] = "/usr/bin:/usr/sbin";
11946 perm
= R_OK
| X_OK
;
11949 for (i
= 0; i
< ARRAY_SIZE(search_paths
); i
++) {
11952 if (!search_paths
[i
])
11954 for (s
= search_paths
[i
]; s
!= NULL
; s
= strchr(s
, ':')) {
11960 next_path
= strchr(s
, ':');
11961 seg_len
= next_path
? next_path
- s
: strlen(s
);
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)
11968 pr_debug("resolved '%s' to '%s'\n", file
, result
);
11976 bpf_program__attach_uprobe_multi(const struct bpf_program
*prog
,
11979 const char *func_pattern
,
11980 const struct bpf_uprobe_multi_opts
*opts
)
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
;
11994 if (!OPTS_VALID(opts
, bpf_uprobe_multi_opts
))
11995 return libbpf_err_ptr(-EINVAL
);
11997 prog_fd
= bpf_program__fd(prog
);
11999 pr_warn("prog '%s': can't attach BPF program without FD (was it loaded?)\n",
12001 return libbpf_err_ptr(-EINVAL
);
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);
12013 * User can specify 2 mutually exclusive set of inputs:
12015 * 1) use only path/func_pattern/pid arguments
12017 * 2) use path/pid with allowed combinations of:
12018 * syms/offsets/ref_ctr_offsets/cookies/cnt
12020 * - syms and offsets are mutually exclusive
12021 * - ref_ctr_offsets and cookies are optional
12023 * Any other usage results in error.
12027 return libbpf_err_ptr(-EINVAL
);
12028 if (!func_pattern
&& cnt
== 0)
12029 return libbpf_err_ptr(-EINVAL
);
12031 if (func_pattern
) {
12032 if (syms
|| offsets
|| ref_ctr_offsets
|| cookies
|| cnt
)
12033 return libbpf_err_ptr(-EINVAL
);
12035 if (!!syms
== !!offsets
)
12036 return libbpf_err_ptr(-EINVAL
);
12039 if (retprobe
&& session
)
12040 return libbpf_err_ptr(-EINVAL
);
12042 if (func_pattern
) {
12043 if (!strchr(path
, '/')) {
12044 err
= resolve_full_path(path
, full_path
, sizeof(full_path
));
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
);
12053 err
= elf_resolve_pattern_offsets(path
, func_pattern
,
12054 &resolved_offsets
, &cnt
);
12056 return libbpf_err_ptr(err
);
12057 offsets
= resolved_offsets
;
12059 err
= elf_resolve_syms_offsets(path
, cnt
, syms
, &resolved_offsets
, STT_FUNC
);
12061 return libbpf_err_ptr(err
);
12062 offsets
= resolved_offsets
;
12065 attach_type
= session
? BPF_TRACE_UPROBE_SESSION
: BPF_TRACE_UPROBE_MULTI
;
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;
12077 lopts
.uprobe_multi
.pid
= pid
;
12079 link
= calloc(1, sizeof(*link
));
12084 link
->detach
= &bpf_link__detach_fd
;
12086 link_fd
= bpf_link_create(prog_fd
, 0, attach_type
, &lopts
);
12089 pr_warn("prog '%s': failed to attach multi-uprobe: %s\n",
12090 prog
->name
, errstr(err
));
12093 link
->fd
= link_fd
;
12094 free(resolved_offsets
);
12098 free(resolved_offsets
);
12100 return libbpf_err_ptr(err
);
12103 LIBBPF_API
struct bpf_link
*
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
)
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
;
12116 bool retprobe
, legacy
;
12117 const char *func_name
;
12119 if (!OPTS_VALID(opts
, bpf_uprobe_opts
))
12120 return libbpf_err_ptr(-EINVAL
);
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);
12128 return libbpf_err_ptr(-EINVAL
);
12130 /* Check if "binary_path" refers to an archive. */
12131 archive_sep
= strstr(binary_path
, "!/");
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
));
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
);
12145 binary_path
= full_path
;
12147 func_name
= OPTS_GET(opts
, func_name
, NULL
);
12151 if (archive_path
) {
12152 sym_off
= elf_find_func_offset_from_archive(archive_path
, binary_path
,
12154 binary_path
= archive_path
;
12156 sym_off
= elf_find_func_offset_from_file(binary_path
, func_name
);
12159 return libbpf_err_ptr(sym_off
);
12160 func_offset
+= sym_off
;
12163 legacy
= determine_uprobe_perf_type() < 0;
12164 switch (attach_mode
) {
12165 case PROBE_ATTACH_MODE_LEGACY
:
12167 pe_opts
.force_ioctl_attach
= true;
12169 case PROBE_ATTACH_MODE_PERF
:
12171 return libbpf_err_ptr(-ENOTSUP
);
12172 pe_opts
.force_ioctl_attach
= true;
12174 case PROBE_ATTACH_MODE_LINK
:
12175 if (legacy
|| !kernel_supports(prog
->obj
, FEAT_PERF_LINK
))
12176 return libbpf_err_ptr(-ENOTSUP
);
12178 case PROBE_ATTACH_MODE_DEFAULT
:
12181 return libbpf_err_ptr(-EINVAL
);
12185 pfd
= perf_event_open_probe(true /* uprobe */, retprobe
, binary_path
,
12186 func_offset
, pid
, ref_ctr_off
);
12188 char probe_name
[PATH_MAX
+ 64];
12191 return libbpf_err_ptr(-EINVAL
);
12193 gen_uprobe_legacy_event_name(probe_name
, sizeof(probe_name
),
12194 binary_path
, func_offset
);
12196 legacy_probe
= strdup(probe_name
);
12198 return libbpf_err_ptr(-ENOMEM
);
12200 pfd
= perf_event_uprobe_open_legacy(legacy_probe
, retprobe
,
12201 binary_path
, func_offset
, pid
);
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
,
12212 link
= bpf_program__attach_perf_event_opts(prog
, pfd
, &pe_opts
);
12213 err
= libbpf_get_error(link
);
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
,
12220 goto err_clean_legacy
;
12223 struct bpf_link_perf
*perf_link
= container_of(link
, struct bpf_link_perf
, link
);
12225 perf_link
->legacy_probe_name
= legacy_probe
;
12226 perf_link
->legacy_is_kprobe
= false;
12227 perf_link
->legacy_is_retprobe
= retprobe
;
12233 remove_uprobe_event_legacy(legacy_probe
, retprobe
);
12235 free(legacy_probe
);
12236 return libbpf_err_ptr(err
);
12239 /* Format of u[ret]probe section definition supporting auto-attach:
12240 * u[ret]probe/binary:function[+offset]
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.
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
12249 static int attach_uprobe(const struct bpf_program
*prog
, long cookie
, struct bpf_link
**link
)
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
;
12258 n
= sscanf(prog
->sec_name
, "%m[^/]/%m[^:]:%m[^\n]",
12259 &probe_type
, &binary_path
, &func_name
);
12262 /* handle SEC("u[ret]probe") - format is valid, but auto-attach is impossible. */
12266 pr_warn("prog '%s': section '%s' missing ':function[+offset]' specification\n",
12267 prog
->name
, prog
->sec_name
);
12270 /* check if user specifies `+offset`, if yes, this should be
12271 * the last part of the string, make sure sscanf read to EOL
12273 func_off
= strrchr(func_name
, '+');
12275 n
= sscanf(func_off
, "+%li%n", &offset
, &c
);
12276 if (n
== 1 && *(func_off
+ c
) == '\0')
12277 func_off
[0] = '\0';
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",
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
);
12293 pr_warn("prog '%s': invalid format of section definition '%s'\n", prog
->name
,
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
)
12309 DECLARE_LIBBPF_OPTS(bpf_uprobe_opts
, opts
, .retprobe
= retprobe
);
12311 return bpf_program__attach_uprobe_opts(prog
, pid
, binary_path
, func_offset
, &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
)
12319 char resolved_path
[512];
12320 struct bpf_object
*obj
= prog
->obj
;
12321 struct bpf_link
*link
;
12325 if (!OPTS_VALID(opts
, bpf_uprobe_opts
))
12326 return libbpf_err_ptr(-EINVAL
);
12328 if (bpf_program__fd(prog
) < 0) {
12329 pr_warn("prog '%s': can't attach BPF program without FD (was it loaded?)\n",
12331 return libbpf_err_ptr(-EINVAL
);
12335 return libbpf_err_ptr(-EINVAL
);
12337 if (!strchr(binary_path
, '/')) {
12338 err
= resolve_full_path(binary_path
, resolved_path
, sizeof(resolved_path
));
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
);
12344 binary_path
= resolved_path
;
12347 /* USDT manager is instantiated lazily on first USDT attach. It will
12348 * be destroyed together with BPF object in bpf_object__close().
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
);
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
);
12363 return libbpf_err_ptr(err
);
12367 static int attach_usdt(const struct bpf_program
*prog
, long cookie
, struct bpf_link
**link
)
12369 char *path
= NULL
, *provider
= NULL
, *name
= NULL
;
12370 const char *sec_name
;
12373 sec_name
= bpf_program__section_name(prog
);
12374 if (strcmp(sec_name
, "usdt") == 0) {
12375 /* no auto-attach for just SEC("usdt") */
12380 n
= sscanf(sec_name
, "usdt/%m[^:]:%m[^:]:%m[^:]", &path
, &provider
, &name
);
12382 pr_warn("invalid section '%s', expected SEC(\"usdt/<path>:<provider>:<name>\")\n",
12386 *link
= bpf_program__attach_usdt(prog
, -1 /* any process */, path
,
12387 provider
, name
, NULL
);
12388 err
= libbpf_get_error(*link
);
12396 static int determine_tracepoint_id(const char *tp_category
,
12397 const char *tp_name
)
12399 char file
[PATH_MAX
];
12402 ret
= snprintf(file
, sizeof(file
), "%s/events/%s/%s/id",
12403 tracefs_path(), tp_category
, tp_name
);
12406 if (ret
>= sizeof(file
)) {
12407 pr_debug("tracepoint %s/%s path is too long\n",
12408 tp_category
, tp_name
);
12411 return parse_uint_from_file(file
, "%d\n");
12414 static int perf_event_open_tracepoint(const char *tp_category
,
12415 const char *tp_name
)
12417 const size_t attr_sz
= sizeof(struct perf_event_attr
);
12418 struct perf_event_attr attr
;
12419 int tp_id
, pfd
, err
;
12421 tp_id
= determine_tracepoint_id(tp_category
, tp_name
);
12423 pr_warn("failed to determine tracepoint '%s/%s' perf event ID: %s\n",
12424 tp_category
, tp_name
,
12429 memset(&attr
, 0, attr_sz
);
12430 attr
.type
= PERF_TYPE_TRACEPOINT
;
12431 attr
.size
= attr_sz
;
12432 attr
.config
= tp_id
;
12434 pfd
= syscall(__NR_perf_event_open
, &attr
, -1 /* pid */, 0 /* cpu */,
12435 -1 /* group_fd */, PERF_FLAG_FD_CLOEXEC
);
12438 pr_warn("tracepoint '%s/%s' perf_event_open() failed: %s\n",
12439 tp_category
, tp_name
,
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
)
12451 DECLARE_LIBBPF_OPTS(bpf_perf_event_opts
, pe_opts
);
12452 struct bpf_link
*link
;
12455 if (!OPTS_VALID(opts
, bpf_tracepoint_opts
))
12456 return libbpf_err_ptr(-EINVAL
);
12458 pe_opts
.bpf_cookie
= OPTS_GET(opts
, bpf_cookie
, 0);
12460 pfd
= perf_event_open_tracepoint(tp_category
, tp_name
);
12462 pr_warn("prog '%s': failed to create tracepoint '%s/%s' perf event: %s\n",
12463 prog
->name
, tp_category
, tp_name
,
12465 return libbpf_err_ptr(pfd
);
12467 link
= bpf_program__attach_perf_event_opts(prog
, pfd
, &pe_opts
);
12468 err
= libbpf_get_error(link
);
12471 pr_warn("prog '%s': failed to attach to tracepoint '%s/%s': %s\n",
12472 prog
->name
, tp_category
, tp_name
,
12474 return libbpf_err_ptr(err
);
12479 struct bpf_link
*bpf_program__attach_tracepoint(const struct bpf_program
*prog
,
12480 const char *tp_category
,
12481 const char *tp_name
)
12483 return bpf_program__attach_tracepoint_opts(prog
, tp_category
, tp_name
, NULL
);
12486 static int attach_tp(const struct bpf_program
*prog
, long cookie
, struct bpf_link
**link
)
12488 char *sec_name
, *tp_cat
, *tp_name
;
12492 /* no auto-attach for SEC("tp") or SEC("tracepoint") */
12493 if (strcmp(prog
->sec_name
, "tp") == 0 || strcmp(prog
->sec_name
, "tracepoint") == 0)
12496 sec_name
= strdup(prog
->sec_name
);
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;
12504 tp_cat
= sec_name
+ sizeof("tracepoint/") - 1;
12505 tp_name
= strchr(tp_cat
, '/');
12513 *link
= bpf_program__attach_tracepoint(prog
, tp_cat
, tp_name
);
12515 return libbpf_get_error(*link
);
12519 bpf_program__attach_raw_tracepoint_opts(const struct bpf_program
*prog
,
12520 const char *tp_name
,
12521 struct bpf_raw_tracepoint_opts
*opts
)
12523 LIBBPF_OPTS(bpf_raw_tp_opts
, raw_opts
);
12524 struct bpf_link
*link
;
12527 if (!OPTS_VALID(opts
, bpf_raw_tracepoint_opts
))
12528 return libbpf_err_ptr(-EINVAL
);
12530 prog_fd
= bpf_program__fd(prog
);
12532 pr_warn("prog '%s': can't attach before loaded\n", prog
->name
);
12533 return libbpf_err_ptr(-EINVAL
);
12536 link
= calloc(1, sizeof(*link
));
12538 return libbpf_err_ptr(-ENOMEM
);
12539 link
->detach
= &bpf_link__detach_fd
;
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
);
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
);
12555 struct bpf_link
*bpf_program__attach_raw_tracepoint(const struct bpf_program
*prog
,
12556 const char *tp_name
)
12558 return bpf_program__attach_raw_tracepoint_opts(prog
, tp_name
, NULL
);
12561 static int attach_raw_tp(const struct bpf_program
*prog
, long cookie
, struct bpf_link
**link
)
12563 static const char *const prefixes
[] = {
12567 "raw_tracepoint.w",
12570 const char *tp_name
= NULL
;
12574 for (i
= 0; i
< ARRAY_SIZE(prefixes
); i
++) {
12577 if (!str_has_pfx(prog
->sec_name
, prefixes
[i
]))
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')
12585 if (prog
->sec_name
[pfx_len
] != '/')
12588 tp_name
= prog
->sec_name
+ pfx_len
+ 1;
12593 pr_warn("prog '%s': invalid section name '%s'\n",
12594 prog
->name
, prog
->sec_name
);
12598 *link
= bpf_program__attach_raw_tracepoint(prog
, tp_name
);
12599 return libbpf_get_error(*link
);
12602 /* Common logic for all BPF program types that attach to a btf_id */
12603 static struct bpf_link
*bpf_program__attach_btf_id(const struct bpf_program
*prog
,
12604 const struct bpf_trace_opts
*opts
)
12606 LIBBPF_OPTS(bpf_link_create_opts
, link_opts
);
12607 struct bpf_link
*link
;
12610 if (!OPTS_VALID(opts
, bpf_trace_opts
))
12611 return libbpf_err_ptr(-EINVAL
);
12613 prog_fd
= bpf_program__fd(prog
);
12615 pr_warn("prog '%s': can't attach before loaded\n", prog
->name
);
12616 return libbpf_err_ptr(-EINVAL
);
12619 link
= calloc(1, sizeof(*link
));
12621 return libbpf_err_ptr(-ENOMEM
);
12622 link
->detach
= &bpf_link__detach_fd
;
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
);
12630 pr_warn("prog '%s': failed to attach: %s\n",
12631 prog
->name
, errstr(pfd
));
12632 return libbpf_err_ptr(pfd
);
12638 struct bpf_link
*bpf_program__attach_trace(const struct bpf_program
*prog
)
12640 return bpf_program__attach_btf_id(prog
, NULL
);
12643 struct bpf_link
*bpf_program__attach_trace_opts(const struct bpf_program
*prog
,
12644 const struct bpf_trace_opts
*opts
)
12646 return bpf_program__attach_btf_id(prog
, opts
);
12649 struct bpf_link
*bpf_program__attach_lsm(const struct bpf_program
*prog
)
12651 return bpf_program__attach_btf_id(prog
, NULL
);
12654 static int attach_trace(const struct bpf_program
*prog
, long cookie
, struct bpf_link
**link
)
12656 *link
= bpf_program__attach_trace(prog
);
12657 return libbpf_get_error(*link
);
12660 static int attach_lsm(const struct bpf_program
*prog
, long cookie
, struct bpf_link
**link
)
12662 *link
= bpf_program__attach_lsm(prog
);
12663 return libbpf_get_error(*link
);
12666 static struct bpf_link
*
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
)
12671 enum bpf_attach_type attach_type
;
12672 struct bpf_link
*link
;
12673 int prog_fd
, link_fd
;
12675 prog_fd
= bpf_program__fd(prog
);
12677 pr_warn("prog '%s': can't attach before loaded\n", prog
->name
);
12678 return libbpf_err_ptr(-EINVAL
);
12681 link
= calloc(1, sizeof(*link
));
12683 return libbpf_err_ptr(-ENOMEM
);
12684 link
->detach
= &bpf_link__detach_fd
;
12686 attach_type
= bpf_program__expected_attach_type(prog
);
12687 link_fd
= bpf_link_create(prog_fd
, target_fd
, attach_type
, opts
);
12691 pr_warn("prog '%s': failed to attach to %s: %s\n",
12692 prog
->name
, target_name
,
12694 return libbpf_err_ptr(link_fd
);
12696 link
->fd
= link_fd
;
12701 bpf_program__attach_cgroup(const struct bpf_program
*prog
, int cgroup_fd
)
12703 return bpf_program_attach_fd(prog
, cgroup_fd
, "cgroup", NULL
);
12707 bpf_program__attach_netns(const struct bpf_program
*prog
, int netns_fd
)
12709 return bpf_program_attach_fd(prog
, netns_fd
, "netns", NULL
);
12713 bpf_program__attach_sockmap(const struct bpf_program
*prog
, int map_fd
)
12715 return bpf_program_attach_fd(prog
, map_fd
, "sockmap", NULL
);
12718 struct bpf_link
*bpf_program__attach_xdp(const struct bpf_program
*prog
, int ifindex
)
12720 /* target_fd/target_ifindex use the same field in LINK_CREATE */
12721 return bpf_program_attach_fd(prog
, ifindex
, "xdp", NULL
);
12725 bpf_program__attach_tcx(const struct bpf_program
*prog
, int ifindex
,
12726 const struct bpf_tcx_opts
*opts
)
12728 LIBBPF_OPTS(bpf_link_create_opts
, link_create_opts
);
12732 if (!OPTS_VALID(opts
, bpf_tcx_opts
))
12733 return libbpf_err_ptr(-EINVAL
);
12735 relative_id
= OPTS_GET(opts
, relative_id
, 0);
12736 relative_fd
= OPTS_GET(opts
, relative_fd
, 0);
12738 /* validate we don't have unexpected combinations of non-zero fields */
12740 pr_warn("prog '%s': target netdevice ifindex cannot be zero\n",
12742 return libbpf_err_ptr(-EINVAL
);
12744 if (relative_fd
&& relative_id
) {
12745 pr_warn("prog '%s': relative_fd and relative_id cannot be set at the same time\n",
12747 return libbpf_err_ptr(-EINVAL
);
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);
12755 /* target_fd/target_ifindex use the same field in LINK_CREATE */
12756 return bpf_program_attach_fd(prog
, ifindex
, "tcx", &link_create_opts
);
12760 bpf_program__attach_netkit(const struct bpf_program
*prog
, int ifindex
,
12761 const struct bpf_netkit_opts
*opts
)
12763 LIBBPF_OPTS(bpf_link_create_opts
, link_create_opts
);
12767 if (!OPTS_VALID(opts
, bpf_netkit_opts
))
12768 return libbpf_err_ptr(-EINVAL
);
12770 relative_id
= OPTS_GET(opts
, relative_id
, 0);
12771 relative_fd
= OPTS_GET(opts
, relative_fd
, 0);
12773 /* validate we don't have unexpected combinations of non-zero fields */
12775 pr_warn("prog '%s': target netdevice ifindex cannot be zero\n",
12777 return libbpf_err_ptr(-EINVAL
);
12779 if (relative_fd
&& relative_id
) {
12780 pr_warn("prog '%s': relative_fd and relative_id cannot be set at the same time\n",
12782 return libbpf_err_ptr(-EINVAL
);
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);
12790 return bpf_program_attach_fd(prog
, ifindex
, "netkit", &link_create_opts
);
12793 struct bpf_link
*bpf_program__attach_freplace(const struct bpf_program
*prog
,
12795 const char *attach_func_name
)
12799 if (!!target_fd
!= !!attach_func_name
) {
12800 pr_warn("prog '%s': supply none or both of target_fd and attach_func_name\n",
12802 return libbpf_err_ptr(-EINVAL
);
12805 if (prog
->type
!= BPF_PROG_TYPE_EXT
) {
12806 pr_warn("prog '%s': only BPF_PROG_TYPE_EXT can attach as freplace\n",
12808 return libbpf_err_ptr(-EINVAL
);
12812 LIBBPF_OPTS(bpf_link_create_opts
, target_opts
);
12814 btf_id
= libbpf_find_prog_btf_id(attach_func_name
, target_fd
);
12816 return libbpf_err_ptr(btf_id
);
12818 target_opts
.target_btf_id
= btf_id
;
12820 return bpf_program_attach_fd(prog
, target_fd
, "freplace",
12823 /* no target, so use raw_tracepoint_open for compatibility
12826 return bpf_program__attach_trace(prog
);
12831 bpf_program__attach_iter(const struct bpf_program
*prog
,
12832 const struct bpf_iter_attach_opts
*opts
)
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;
12839 if (!OPTS_VALID(opts
, bpf_iter_attach_opts
))
12840 return libbpf_err_ptr(-EINVAL
);
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);
12845 prog_fd
= bpf_program__fd(prog
);
12847 pr_warn("prog '%s': can't attach before loaded\n", prog
->name
);
12848 return libbpf_err_ptr(-EINVAL
);
12851 link
= calloc(1, sizeof(*link
));
12853 return libbpf_err_ptr(-ENOMEM
);
12854 link
->detach
= &bpf_link__detach_fd
;
12856 link_fd
= bpf_link_create(prog_fd
, target_fd
, BPF_TRACE_ITER
,
12857 &link_create_opts
);
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
);
12865 link
->fd
= link_fd
;
12869 static int attach_iter(const struct bpf_program
*prog
, long cookie
, struct bpf_link
**link
)
12871 *link
= bpf_program__attach_iter(prog
, NULL
);
12872 return libbpf_get_error(*link
);
12875 struct bpf_link
*bpf_program__attach_netfilter(const struct bpf_program
*prog
,
12876 const struct bpf_netfilter_opts
*opts
)
12878 LIBBPF_OPTS(bpf_link_create_opts
, lopts
);
12879 struct bpf_link
*link
;
12880 int prog_fd
, link_fd
;
12882 if (!OPTS_VALID(opts
, bpf_netfilter_opts
))
12883 return libbpf_err_ptr(-EINVAL
);
12885 prog_fd
= bpf_program__fd(prog
);
12887 pr_warn("prog '%s': can't attach before loaded\n", prog
->name
);
12888 return libbpf_err_ptr(-EINVAL
);
12891 link
= calloc(1, sizeof(*link
));
12893 return libbpf_err_ptr(-ENOMEM
);
12895 link
->detach
= &bpf_link__detach_fd
;
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);
12902 link_fd
= bpf_link_create(prog_fd
, 0, BPF_NETFILTER
, &lopts
);
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
);
12910 link
->fd
= link_fd
;
12915 struct bpf_link
*bpf_program__attach(const struct bpf_program
*prog
)
12917 struct bpf_link
*link
= NULL
;
12920 if (!prog
->sec_def
|| !prog
->sec_def
->prog_attach_fn
)
12921 return libbpf_err_ptr(-EOPNOTSUPP
);
12923 if (bpf_program__fd(prog
) < 0) {
12924 pr_warn("prog '%s': can't attach BPF program without FD (was it loaded?)\n",
12926 return libbpf_err_ptr(-EINVAL
);
12929 err
= prog
->sec_def
->prog_attach_fn(prog
, prog
->sec_def
->cookie
, &link
);
12931 return libbpf_err_ptr(err
);
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().
12939 return libbpf_err_ptr(-EOPNOTSUPP
);
12944 struct bpf_link_struct_ops
{
12945 struct bpf_link link
;
12949 static int bpf_link__detach_struct_ops(struct bpf_link
*link
)
12951 struct bpf_link_struct_ops
*st_link
;
12954 st_link
= container_of(link
, struct bpf_link_struct_ops
, link
);
12956 if (st_link
->map_fd
< 0)
12957 /* w/o a real link */
12958 return bpf_map_delete_elem(link
->fd
, &zero
);
12960 return close(link
->fd
);
12963 struct bpf_link
*bpf_map__attach_struct_ops(const struct bpf_map
*map
)
12965 struct bpf_link_struct_ops
*link
;
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
);
12975 pr_warn("map '%s': can't attach BPF map without FD (was it created?)\n", map
->name
);
12976 return libbpf_err_ptr(-EINVAL
);
12979 link
= calloc(1, sizeof(*link
));
12981 return libbpf_err_ptr(-EINVAL
);
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.
12990 if (err
&& (!(map
->def
.map_flags
& BPF_F_LINK
) || err
!= -EBUSY
)) {
12992 return libbpf_err_ptr(err
);
12995 link
->link
.detach
= bpf_link__detach_struct_ops
;
12997 if (!(map
->def
.map_flags
& BPF_F_LINK
)) {
12998 /* w/o a real link */
12999 link
->link
.fd
= map
->fd
;
13001 return &link
->link
;
13004 fd
= bpf_link_create(map
->fd
, 0, BPF_STRUCT_OPS
, NULL
);
13007 return libbpf_err_ptr(fd
);
13010 link
->link
.fd
= fd
;
13011 link
->map_fd
= map
->fd
;
13013 return &link
->link
;
13017 * Swap the back struct_ops of a link with a new struct_ops map.
13019 int bpf_link__update_map(struct bpf_link
*link
, const struct bpf_map
*map
)
13021 struct bpf_link_struct_ops
*st_ops_link
;
13025 if (!bpf_map__is_struct_ops(map
))
13029 pr_warn("map '%s': can't use BPF map without FD (was it created?)\n", map
->name
);
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)
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.
13044 if (err
&& err
!= -EBUSY
)
13047 err
= bpf_link_update(link
->fd
, map
->fd
, NULL
);
13051 st_ops_link
->map_fd
= map
->fd
;
13056 typedef enum bpf_perf_event_ret (*bpf_perf_event_print_t
)(struct perf_event_header
*hdr
,
13057 void *private_data
);
13059 static enum bpf_perf_event_ret
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
)
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
;
13072 while (data_head
!= data_tail
) {
13073 ehdr
= base
+ (data_tail
& (mmap_size
- 1));
13074 ehdr_size
= ehdr
->size
;
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
;
13081 if (*copy_size
< ehdr_size
) {
13083 *copy_mem
= malloc(ehdr_size
);
13086 ret
= LIBBPF_PERF_EVENT_ERROR
;
13089 *copy_size
= ehdr_size
;
13092 memcpy(*copy_mem
, copy_start
, len_first
);
13093 memcpy(*copy_mem
+ len_first
, base
, len_secnd
);
13097 ret
= fn(ehdr
, private_data
);
13098 data_tail
+= ehdr_size
;
13099 if (ret
!= LIBBPF_PERF_EVENT_CONT
)
13103 ring_buffer_write_tail(header
, data_tail
);
13104 return libbpf_err(ret
);
13107 struct perf_buffer
;
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
;
13122 struct perf_cpu_buf
{
13123 struct perf_buffer
*pb
;
13124 void *base
; /* mmap()'ed memory */
13125 void *buf
; /* for reconstructing segmented data */
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 */
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 */
13147 static void perf_buffer__free_cpu_buf(struct perf_buffer
*pb
,
13148 struct perf_cpu_buf
*cpu_buf
)
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
);
13159 free(cpu_buf
->buf
);
13163 void perf_buffer__free(struct perf_buffer
*pb
)
13167 if (IS_ERR_OR_NULL(pb
))
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
];
13176 bpf_map_delete_elem(pb
->map_fd
, &cpu_buf
->map_key
);
13177 perf_buffer__free_cpu_buf(pb
, cpu_buf
);
13179 free(pb
->cpu_bufs
);
13181 if (pb
->epoll_fd
>= 0)
13182 close(pb
->epoll_fd
);
13187 static struct perf_cpu_buf
*
13188 perf_buffer__open_cpu_buf(struct perf_buffer
*pb
, struct perf_event_attr
*attr
,
13189 int cpu
, int map_key
)
13191 struct perf_cpu_buf
*cpu_buf
;
13194 cpu_buf
= calloc(1, sizeof(*cpu_buf
));
13196 return ERR_PTR(-ENOMEM
);
13199 cpu_buf
->cpu
= cpu
;
13200 cpu_buf
->map_key
= map_key
;
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) {
13206 pr_warn("failed to open perf buffer event on cpu #%d: %s\n",
13211 cpu_buf
->base
= mmap(NULL
, pb
->mmap_size
+ pb
->page_size
,
13212 PROT_READ
| PROT_WRITE
, MAP_SHARED
,
13214 if (cpu_buf
->base
== MAP_FAILED
) {
13215 cpu_buf
->base
= NULL
;
13217 pr_warn("failed to mmap perf buffer on cpu #%d: %s\n",
13222 if (ioctl(cpu_buf
->fd
, PERF_EVENT_IOC_ENABLE
, 0) < 0) {
13224 pr_warn("failed to enable perf buffer event on cpu #%d: %s\n",
13232 perf_buffer__free_cpu_buf(pb
, cpu_buf
);
13233 return (struct perf_cpu_buf
*)ERR_PTR(err
);
13236 static struct perf_buffer
*__perf_buffer__new(int map_fd
, size_t page_cnt
,
13237 struct perf_buffer_params
*p
);
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
,
13243 const struct perf_buffer_opts
*opts
)
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
;
13250 if (!OPTS_VALID(opts
, perf_buffer_opts
))
13251 return libbpf_err_ptr(-EINVAL
);
13253 sample_period
= OPTS_GET(opts
, sample_period
, 1);
13254 if (!sample_period
)
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
;
13266 p
.sample_cb
= sample_cb
;
13267 p
.lost_cb
= lost_cb
;
13270 return libbpf_ptr(__perf_buffer__new(map_fd
, page_cnt
, &p
));
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
)
13278 struct perf_buffer_params p
= {};
13281 return libbpf_err_ptr(-EINVAL
);
13283 if (!OPTS_VALID(opts
, perf_buffer_raw_opts
))
13284 return libbpf_err_ptr(-EINVAL
);
13287 p
.event_cb
= event_cb
;
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
);
13293 return libbpf_ptr(__perf_buffer__new(map_fd
, page_cnt
, &p
));
13296 static struct perf_buffer
*__perf_buffer__new(int map_fd
, size_t page_cnt
,
13297 struct perf_buffer_params
*p
)
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
;
13306 if (page_cnt
== 0 || (page_cnt
& (page_cnt
- 1))) {
13307 pr_warn("page count should be power of two, but is %zu\n",
13309 return ERR_PTR(-EINVAL
);
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
);
13318 /* if BPF_OBJ_GET_INFO_BY_FD is supported, will return
13319 * -EBADFD, -EFAULT, or -E2BIG on real error
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
);
13326 pr_debug("failed to get map info for FD %d; API not supported? Ignoring...\n",
13329 if (map
.type
!= BPF_MAP_TYPE_PERF_EVENT_ARRAY
) {
13330 pr_warn("map '%s' should be BPF_MAP_TYPE_PERF_EVENT_ARRAY\n",
13332 return ERR_PTR(-EINVAL
);
13336 pb
= calloc(1, sizeof(*pb
));
13338 return ERR_PTR(-ENOMEM
);
13340 pb
->event_cb
= p
->event_cb
;
13341 pb
->sample_cb
= p
->sample_cb
;
13342 pb
->lost_cb
= p
->lost_cb
;
13345 pb
->page_size
= getpagesize();
13346 pb
->mmap_size
= pb
->page_size
* page_cnt
;
13347 pb
->map_fd
= map_fd
;
13349 pb
->epoll_fd
= epoll_create1(EPOLL_CLOEXEC
);
13350 if (pb
->epoll_fd
< 0) {
13352 pr_warn("failed to create epoll instance: %s\n",
13357 if (p
->cpu_cnt
> 0) {
13358 pb
->cpu_cnt
= p
->cpu_cnt
;
13360 pb
->cpu_cnt
= libbpf_num_possible_cpus();
13361 if (pb
->cpu_cnt
< 0) {
13365 if (map
.max_entries
&& map
.max_entries
< pb
->cpu_cnt
)
13366 pb
->cpu_cnt
= map
.max_entries
;
13369 pb
->events
= calloc(pb
->cpu_cnt
, sizeof(*pb
->events
));
13372 pr_warn("failed to allocate events: out of memory\n");
13375 pb
->cpu_bufs
= calloc(pb
->cpu_cnt
, sizeof(*pb
->cpu_bufs
));
13376 if (!pb
->cpu_bufs
) {
13378 pr_warn("failed to allocate buffers: out of memory\n");
13382 err
= parse_cpu_mask_file(online_cpus_file
, &online
, &n
);
13384 pr_warn("failed to get online CPU mask: %s\n", errstr(err
));
13388 for (i
= 0, j
= 0; i
< pb
->cpu_cnt
; i
++) {
13389 struct perf_cpu_buf
*cpu_buf
;
13392 cpu
= p
->cpu_cnt
> 0 ? p
->cpus
[i
] : i
;
13393 map_key
= p
->cpu_cnt
> 0 ? p
->map_keys
[i
] : i
;
13395 /* in case user didn't explicitly requested particular CPUs to
13396 * be attached to, skip offline/not present CPUs
13398 if (p
->cpu_cnt
<= 0 && (cpu
>= n
|| !online
[cpu
]))
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
);
13407 pb
->cpu_bufs
[j
] = cpu_buf
;
13409 err
= bpf_map_update_elem(pb
->map_fd
, &map_key
,
13413 pr_warn("failed to set cpu #%d, key %d -> perf FD %d: %s\n",
13414 cpu
, map_key
, cpu_buf
->fd
,
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) {
13424 pr_warn("failed to epoll_ctl cpu #%d perf FD %d: %s\n",
13439 perf_buffer__free(pb
);
13440 return ERR_PTR(err
);
13443 struct perf_sample_raw
{
13444 struct perf_event_header header
;
13449 struct perf_sample_lost
{
13450 struct perf_event_header header
;
13453 uint64_t sample_id
;
13456 static enum bpf_perf_event_ret
13457 perf_buffer__process_record(struct perf_event_header
*e
, void *ctx
)
13459 struct perf_cpu_buf
*cpu_buf
= ctx
;
13460 struct perf_buffer
*pb
= cpu_buf
->pb
;
13463 /* user wants full control over parsing perf event */
13465 return pb
->event_cb(pb
->ctx
, cpu_buf
->cpu
, e
);
13468 case PERF_RECORD_SAMPLE
: {
13469 struct perf_sample_raw
*s
= data
;
13472 pb
->sample_cb(pb
->ctx
, cpu_buf
->cpu
, s
->data
, s
->size
);
13475 case PERF_RECORD_LOST
: {
13476 struct perf_sample_lost
*s
= data
;
13479 pb
->lost_cb(pb
->ctx
, cpu_buf
->cpu
, s
->lost
);
13483 pr_warn("unknown perf sample type %d\n", e
->type
);
13484 return LIBBPF_PERF_EVENT_ERROR
;
13486 return LIBBPF_PERF_EVENT_CONT
;
13489 static int perf_buffer__process_records(struct perf_buffer
*pb
,
13490 struct perf_cpu_buf
*cpu_buf
)
13492 enum bpf_perf_event_ret ret
;
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
)
13503 int perf_buffer__epoll_fd(const struct perf_buffer
*pb
)
13505 return pb
->epoll_fd
;
13508 int perf_buffer__poll(struct perf_buffer
*pb
, int timeout_ms
)
13512 cnt
= epoll_wait(pb
->epoll_fd
, pb
->events
, pb
->cpu_cnt
, timeout_ms
);
13516 for (i
= 0; i
< cnt
; i
++) {
13517 struct perf_cpu_buf
*cpu_buf
= pb
->events
[i
].data
.ptr
;
13519 err
= perf_buffer__process_records(pb
, cpu_buf
);
13521 pr_warn("error while processing records: %s\n", errstr(err
));
13522 return libbpf_err(err
);
13528 /* Return number of PERF_EVENT_ARRAY map slots set up by this perf_buffer
13531 size_t perf_buffer__buffer_cnt(const struct perf_buffer
*pb
)
13533 return pb
->cpu_cnt
;
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.
13541 int perf_buffer__buffer_fd(const struct perf_buffer
*pb
, size_t buf_idx
)
13543 struct perf_cpu_buf
*cpu_buf
;
13545 if (buf_idx
>= pb
->cpu_cnt
)
13546 return libbpf_err(-EINVAL
);
13548 cpu_buf
= pb
->cpu_bufs
[buf_idx
];
13550 return libbpf_err(-ENOENT
);
13552 return cpu_buf
->fd
;
13555 int perf_buffer__buffer(struct perf_buffer
*pb
, int buf_idx
, void **buf
, size_t *buf_size
)
13557 struct perf_cpu_buf
*cpu_buf
;
13559 if (buf_idx
>= pb
->cpu_cnt
)
13560 return libbpf_err(-EINVAL
);
13562 cpu_buf
= pb
->cpu_bufs
[buf_idx
];
13564 return libbpf_err(-ENOENT
);
13566 *buf
= cpu_buf
->base
;
13567 *buf_size
= pb
->mmap_size
;
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.
13579 int perf_buffer__consume_buffer(struct perf_buffer
*pb
, size_t buf_idx
)
13581 struct perf_cpu_buf
*cpu_buf
;
13583 if (buf_idx
>= pb
->cpu_cnt
)
13584 return libbpf_err(-EINVAL
);
13586 cpu_buf
= pb
->cpu_bufs
[buf_idx
];
13588 return libbpf_err(-ENOENT
);
13590 return perf_buffer__process_records(pb
, cpu_buf
);
13593 int perf_buffer__consume(struct perf_buffer
*pb
)
13597 for (i
= 0; i
< pb
->cpu_cnt
; i
++) {
13598 struct perf_cpu_buf
*cpu_buf
= pb
->cpu_bufs
[i
];
13603 err
= perf_buffer__process_records(pb
, cpu_buf
);
13605 pr_warn("perf_buffer: failed to process records in buffer #%d: %s\n",
13607 return libbpf_err(err
);
13613 int bpf_program__set_attach_target(struct bpf_program
*prog
,
13614 int attach_prog_fd
,
13615 const char *attach_func_name
)
13617 int btf_obj_fd
= 0, btf_id
= 0, err
;
13619 if (!prog
|| attach_prog_fd
< 0)
13620 return libbpf_err(-EINVAL
);
13622 if (prog
->obj
->loaded
)
13623 return libbpf_err(-EINVAL
);
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
13629 prog
->attach_prog_fd
= attach_prog_fd
;
13633 if (attach_prog_fd
) {
13634 btf_id
= libbpf_find_prog_btf_id(attach_func_name
,
13637 return libbpf_err(btf_id
);
13639 if (!attach_func_name
)
13640 return libbpf_err(-EINVAL
);
13642 /* load btf_vmlinux, if not yet */
13643 err
= bpf_object__load_vmlinux_btf(prog
->obj
, true);
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
);
13650 return libbpf_err(err
);
13653 prog
->attach_btf_id
= btf_id
;
13654 prog
->attach_btf_obj_fd
= btf_obj_fd
;
13655 prog
->attach_prog_fd
= attach_prog_fd
;
13659 int parse_cpu_mask_str(const char *s
, bool **mask
, int *mask_sz
)
13661 int err
= 0, n
, len
, start
, end
= -1;
13667 /* Each sub string separated by ',' has format \d+-\d+ or \d+ */
13669 if (*s
== ',' || *s
== '\n') {
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
);
13678 } else if (n
== 1) {
13681 if (start
< 0 || start
> end
) {
13682 pr_warn("Invalid CPU range [%d,%d] in %s\n",
13687 tmp
= realloc(*mask
, end
+ 1);
13693 memset(tmp
+ *mask_sz
, 0, start
- *mask_sz
);
13694 memset(tmp
+ start
, 1, end
- start
+ 1);
13695 *mask_sz
= end
+ 1;
13699 pr_warn("Empty CPU range\n");
13709 int parse_cpu_mask_file(const char *fcpu
, bool **mask
, int *mask_sz
)
13711 int fd
, err
= 0, len
;
13714 fd
= open(fcpu
, O_RDONLY
| O_CLOEXEC
);
13717 pr_warn("Failed to open cpu mask file %s: %s\n", fcpu
, errstr(err
));
13720 len
= read(fd
, buf
, sizeof(buf
));
13723 err
= len
? -errno
: -EINVAL
;
13724 pr_warn("Failed to read cpu mask from %s: %s\n", fcpu
, errstr(err
));
13727 if (len
>= sizeof(buf
)) {
13728 pr_warn("CPU mask is too big in file %s\n", fcpu
);
13733 return parse_cpu_mask_str(buf
, mask
, mask_sz
);
13736 int libbpf_num_possible_cpus(void)
13738 static const char *fcpu
= "/sys/devices/system/cpu/possible";
13740 int err
, n
, i
, tmp_cpus
;
13743 tmp_cpus
= READ_ONCE(cpus
);
13747 err
= parse_cpu_mask_file(fcpu
, &mask
, &n
);
13749 return libbpf_err(err
);
13752 for (i
= 0; i
< n
; i
++) {
13758 WRITE_ONCE(cpus
, tmp_cpus
);
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
)
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
;
13774 *map
= bpf_object__find_map_by_name(obj
, name
);
13776 pr_warn("failed to find skeleton map '%s'\n", name
);
13780 /* externs shouldn't be pre-setup from user code */
13781 if (mmaped
&& (*map
)->libbpf_type
!= LIBBPF_MAP_KCONFIG
)
13782 *mmaped
= (*map
)->mmaped
;
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
)
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
;
13798 *prog
= bpf_object__find_program_by_name(obj
, name
);
13800 pr_warn("failed to find skeleton program '%s'\n", name
);
13807 int bpf_object__open_skeleton(struct bpf_object_skeleton
*s
,
13808 const struct bpf_object_open_opts
*opts
)
13810 struct bpf_object
*obj
;
13813 obj
= bpf_object_open(NULL
, s
->data
, s
->data_sz
, s
->name
, opts
);
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
);
13822 err
= populate_skeleton_maps(obj
, s
->maps
, s
->map_cnt
, s
->map_skel_sz
);
13824 pr_warn("failed to populate skeleton maps for '%s': %s\n", s
->name
, errstr(err
));
13825 return libbpf_err(err
);
13828 err
= populate_skeleton_progs(obj
, s
->progs
, s
->prog_cnt
, s
->prog_skel_sz
);
13830 pr_warn("failed to populate skeleton progs for '%s': %s\n", s
->name
, errstr(err
));
13831 return libbpf_err(err
);
13837 int bpf_object__open_subskeleton(struct bpf_object_subskeleton
*s
)
13839 int err
, len
, var_idx
, i
;
13840 const char *var_name
;
13841 const struct bpf_map
*map
;
13844 const struct btf_type
*map_type
, *var_type
;
13845 const struct bpf_var_skeleton
*var_skel
;
13846 struct btf_var_secinfo
*var
;
13849 return libbpf_err(-EINVAL
);
13851 btf
= bpf_object__btf(s
->obj
);
13853 pr_warn("subskeletons require BTF at runtime (object %s)\n",
13854 bpf_object__name(s
->obj
));
13855 return libbpf_err(-errno
);
13858 err
= populate_skeleton_maps(s
->obj
, s
->maps
, s
->map_cnt
, s
->map_skel_sz
);
13860 pr_warn("failed to populate subskeleton maps: %s\n", errstr(err
));
13861 return libbpf_err(err
);
13864 err
= populate_skeleton_progs(s
->obj
, s
->progs
, s
->prog_cnt
, s
->prog_skel_sz
);
13866 pr_warn("failed to populate subskeleton maps: %s\n", errstr(err
));
13867 return libbpf_err(err
);
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
);
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
);
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
;
13897 void bpf_object__destroy_subskeleton(struct bpf_object_subskeleton
*s
)
13907 int bpf_object__load_skeleton(struct bpf_object_skeleton
*s
)
13911 err
= bpf_object__load(*s
->obj
);
13913 pr_warn("failed to load BPF skeleton '%s': %s\n", s
->name
, errstr(err
));
13914 return libbpf_err(err
);
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
;
13921 if (!map_skel
->mmaped
)
13924 *map_skel
->mmaped
= map
->mmaped
;
13930 int bpf_object__attach_skeleton(struct bpf_object_skeleton
*s
)
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
;
13939 if (!prog
->autoload
|| !prog
->autoattach
)
13942 /* auto-attaching not supported for this program */
13943 if (!prog
->sec_def
|| !prog
->sec_def
->prog_attach_fn
)
13946 /* if user already set the link manually, don't attempt auto-attach */
13950 err
= prog
->sec_def
->prog_attach_fn(prog
, prog
->sec_def
->cookie
, link
);
13952 pr_warn("prog '%s': failed to auto-attach: %s\n",
13953 bpf_program__name(prog
), errstr(err
));
13954 return libbpf_err(err
);
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.
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
;
13975 if (!map
->autocreate
|| !map
->autoattach
)
13978 /* only struct_ops maps can be attached */
13979 if (!bpf_map__is_struct_ops(map
))
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
));
13989 link
= map_skel
->link
;
13993 *link
= bpf_map__attach_struct_ops(map
);
13996 pr_warn("map '%s': failed to auto-attach: %s\n",
13997 bpf_map__name(map
), errstr(err
));
13998 return libbpf_err(err
);
14005 void bpf_object__detach_skeleton(struct bpf_object_skeleton
*s
)
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
;
14013 bpf_link__destroy(*link
);
14017 if (s
->map_skel_sz
< sizeof(struct bpf_map_skeleton
))
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
;
14025 bpf_link__destroy(*link
);
14031 void bpf_object__destroy_skeleton(struct bpf_object_skeleton
*s
)
14036 bpf_object__detach_skeleton(s
);
14038 bpf_object__close(*s
->obj
);