bpf: Remove unnecessary err < 0 check in bpf_struct_ops_map_update_elem
[linux-2.6-block.git] / tools / lib / bpf / libbpf.c
CommitLineData
1bc38b8f 1// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
6061a3d6 2
1b76c13e
WN
3/*
4 * Common eBPF ELF object loading operations.
5 *
6 * Copyright (C) 2013-2015 Alexei Starovoitov <ast@kernel.org>
7 * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com>
8 * Copyright (C) 2015 Huawei Inc.
f367540c 9 * Copyright (C) 2017 Nicira, Inc.
d859900c 10 * Copyright (C) 2019 Isovalent, Inc.
1b76c13e
WN
11 */
12
b4269954 13#ifndef _GNU_SOURCE
531b014e 14#define _GNU_SOURCE
b4269954 15#endif
1b76c13e 16#include <stdlib.h>
b3f59d66
WN
17#include <stdio.h>
18#include <stdarg.h>
f367540c 19#include <libgen.h>
34090915 20#include <inttypes.h>
8ab9da57 21#include <limits.h>
b3f59d66 22#include <string.h>
1b76c13e 23#include <unistd.h>
cdb2f920 24#include <endian.h>
1a5e3fb1
WN
25#include <fcntl.h>
26#include <errno.h>
113e6b7e 27#include <ctype.h>
1b76c13e 28#include <asm/unistd.h>
e28ff1a8 29#include <linux/err.h>
cb1e5e96 30#include <linux/kernel.h>
1b76c13e 31#include <linux/bpf.h>
38d5d3b3 32#include <linux/btf.h>
47eff617 33#include <linux/filter.h>
f367540c 34#include <linux/limits.h>
438363c0 35#include <linux/perf_event.h>
9eea8faf 36#include <linux/bpf_perf_event.h>
a64af0ef 37#include <linux/ring_buffer.h>
fb84b822 38#include <sys/epoll.h>
63f2f5ee 39#include <sys/ioctl.h>
fb84b822 40#include <sys/mman.h>
f367540c
JS
41#include <sys/stat.h>
42#include <sys/types.h>
43#include <sys/vfs.h>
ddc7c304 44#include <sys/utsname.h>
dc3a2d25 45#include <sys/resource.h>
1a5e3fb1
WN
46#include <libelf.h>
47#include <gelf.h>
166750bc 48#include <zlib.h>
1b76c13e
WN
49
50#include "libbpf.h"
52d3352e 51#include "bpf.h"
8a138aed 52#include "btf.h"
6d41907c 53#include "str_error.h"
d7c4b398 54#include "libbpf_internal.h"
ddc7c304 55#include "hashmap.h"
67234743 56#include "bpf_gen_internal.h"
c44fd845 57#include "zip.h"
b3f59d66 58
f367540c
JS
59#ifndef BPF_FS_MAGIC
60#define BPF_FS_MAGIC 0xcafe4a11
61#endif
62
6b434b61
AN
63#define BPF_FS_DEFAULT_PATH "/sys/fs/bpf"
64
9c0f8cbd
AN
65#define BPF_INSN_SZ (sizeof(struct bpf_insn))
66
ff466b58
AI
67/* vsprintf() in __base_pr() uses nonliteral format string. It may break
68 * compilation if user enables corresponding warning. Disable it explicitly.
69 */
70#pragma GCC diagnostic ignored "-Wformat-nonliteral"
71
b3f59d66
WN
72#define __printf(a, b) __attribute__((format(printf, a, b)))
73
590a0088 74static struct bpf_map *bpf_object__add_map(struct bpf_object *obj);
aea28a60 75static bool prog_is_subprog(const struct bpf_object *obj, const struct bpf_program *prog);
f04deb90 76static int map_set_def_max_entries(struct bpf_map *map);
590a0088 77
ccde5760
DM
78static 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",
bf90438c 89 [BPF_CGROUP_UNIX_CONNECT] = "cgroup_unix_connect",
ccde5760
DM
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",
bf90438c 94 [BPF_CGROUP_UNIX_GETPEERNAME] = "cgroup_unix_getpeername",
ccde5760
DM
95 [BPF_CGROUP_INET4_GETSOCKNAME] = "cgroup_inet4_getsockname",
96 [BPF_CGROUP_INET6_GETSOCKNAME] = "cgroup_inet6_getsockname",
bf90438c 97 [BPF_CGROUP_UNIX_GETSOCKNAME] = "cgroup_unix_getsockname",
ccde5760
DM
98 [BPF_CGROUP_UDP4_SENDMSG] = "cgroup_udp4_sendmsg",
99 [BPF_CGROUP_UDP6_SENDMSG] = "cgroup_udp6_sendmsg",
bf90438c 100 [BPF_CGROUP_UNIX_SENDMSG] = "cgroup_unix_sendmsg",
ccde5760
DM
101 [BPF_CGROUP_SYSCTL] = "cgroup_sysctl",
102 [BPF_CGROUP_UDP4_RECVMSG] = "cgroup_udp4_recvmsg",
103 [BPF_CGROUP_UDP6_RECVMSG] = "cgroup_udp6_recvmsg",
bf90438c 104 [BPF_CGROUP_UNIX_RECVMSG] = "cgroup_unix_recvmsg",
ccde5760
DM
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",
bffcf348 118 [BPF_LSM_CGROUP] = "lsm_cgroup",
ccde5760
DM
119 [BPF_SK_LOOKUP] = "sk_lookup",
120 [BPF_TRACE_ITER] = "trace_iter",
121 [BPF_XDP_DEVMAP] = "xdp_devmap",
122 [BPF_XDP_CPUMAP] = "xdp_cpumap",
123 [BPF_XDP] = "xdp",
124 [BPF_SK_REUSEPORT_SELECT] = "sk_reuseport_select",
125 [BPF_SK_REUSEPORT_SELECT_OR_MIGRATE] = "sk_reuseport_select_or_migrate",
126 [BPF_PERF_EVENT] = "perf_event",
127 [BPF_TRACE_KPROBE_MULTI] = "trace_kprobe_multi",
8d1608d7 128 [BPF_STRUCT_OPS] = "struct_ops",
132328e8 129 [BPF_NETFILTER] = "netfilter",
fe20ce3a
DB
130 [BPF_TCX_INGRESS] = "tcx_ingress",
131 [BPF_TCX_EGRESS] = "tcx_egress",
8097e460 132 [BPF_TRACE_UPROBE_MULTI] = "trace_uprobe_multi",
05c31b4a
DB
133 [BPF_NETKIT_PRIMARY] = "netkit_primary",
134 [BPF_NETKIT_PEER] = "netkit_peer",
ccde5760
DM
135};
136
ba5d1b58
DM
137static const char * const link_type_name[] = {
138 [BPF_LINK_TYPE_UNSPEC] = "unspec",
139 [BPF_LINK_TYPE_RAW_TRACEPOINT] = "raw_tracepoint",
140 [BPF_LINK_TYPE_TRACING] = "tracing",
141 [BPF_LINK_TYPE_CGROUP] = "cgroup",
142 [BPF_LINK_TYPE_ITER] = "iter",
143 [BPF_LINK_TYPE_NETNS] = "netns",
144 [BPF_LINK_TYPE_XDP] = "xdp",
145 [BPF_LINK_TYPE_PERF_EVENT] = "perf_event",
146 [BPF_LINK_TYPE_KPROBE_MULTI] = "kprobe_multi",
147 [BPF_LINK_TYPE_STRUCT_OPS] = "struct_ops",
d0fe92fb 148 [BPF_LINK_TYPE_NETFILTER] = "netfilter",
55cc3768 149 [BPF_LINK_TYPE_TCX] = "tcx",
8097e460 150 [BPF_LINK_TYPE_UPROBE_MULTI] = "uprobe_multi",
05c31b4a 151 [BPF_LINK_TYPE_NETKIT] = "netkit",
ba5d1b58
DM
152};
153
3e6dc020
DM
154static const char * const map_type_name[] = {
155 [BPF_MAP_TYPE_UNSPEC] = "unspec",
156 [BPF_MAP_TYPE_HASH] = "hash",
157 [BPF_MAP_TYPE_ARRAY] = "array",
158 [BPF_MAP_TYPE_PROG_ARRAY] = "prog_array",
159 [BPF_MAP_TYPE_PERF_EVENT_ARRAY] = "perf_event_array",
160 [BPF_MAP_TYPE_PERCPU_HASH] = "percpu_hash",
161 [BPF_MAP_TYPE_PERCPU_ARRAY] = "percpu_array",
162 [BPF_MAP_TYPE_STACK_TRACE] = "stack_trace",
163 [BPF_MAP_TYPE_CGROUP_ARRAY] = "cgroup_array",
164 [BPF_MAP_TYPE_LRU_HASH] = "lru_hash",
165 [BPF_MAP_TYPE_LRU_PERCPU_HASH] = "lru_percpu_hash",
166 [BPF_MAP_TYPE_LPM_TRIE] = "lpm_trie",
167 [BPF_MAP_TYPE_ARRAY_OF_MAPS] = "array_of_maps",
168 [BPF_MAP_TYPE_HASH_OF_MAPS] = "hash_of_maps",
169 [BPF_MAP_TYPE_DEVMAP] = "devmap",
170 [BPF_MAP_TYPE_DEVMAP_HASH] = "devmap_hash",
171 [BPF_MAP_TYPE_SOCKMAP] = "sockmap",
172 [BPF_MAP_TYPE_CPUMAP] = "cpumap",
173 [BPF_MAP_TYPE_XSKMAP] = "xskmap",
174 [BPF_MAP_TYPE_SOCKHASH] = "sockhash",
175 [BPF_MAP_TYPE_CGROUP_STORAGE] = "cgroup_storage",
176 [BPF_MAP_TYPE_REUSEPORT_SOCKARRAY] = "reuseport_sockarray",
177 [BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE] = "percpu_cgroup_storage",
178 [BPF_MAP_TYPE_QUEUE] = "queue",
179 [BPF_MAP_TYPE_STACK] = "stack",
180 [BPF_MAP_TYPE_SK_STORAGE] = "sk_storage",
181 [BPF_MAP_TYPE_STRUCT_OPS] = "struct_ops",
182 [BPF_MAP_TYPE_RINGBUF] = "ringbuf",
183 [BPF_MAP_TYPE_INODE_STORAGE] = "inode_storage",
184 [BPF_MAP_TYPE_TASK_STORAGE] = "task_storage",
185 [BPF_MAP_TYPE_BLOOM_FILTER] = "bloom_filter",
583c1f42 186 [BPF_MAP_TYPE_USER_RINGBUF] = "user_ringbuf",
4fe64af2 187 [BPF_MAP_TYPE_CGRP_STORAGE] = "cgrp_storage",
79ff13e9 188 [BPF_MAP_TYPE_ARENA] = "arena",
3e6dc020
DM
189};
190
d18616e7
DM
191static const char * const prog_type_name[] = {
192 [BPF_PROG_TYPE_UNSPEC] = "unspec",
193 [BPF_PROG_TYPE_SOCKET_FILTER] = "socket_filter",
194 [BPF_PROG_TYPE_KPROBE] = "kprobe",
195 [BPF_PROG_TYPE_SCHED_CLS] = "sched_cls",
196 [BPF_PROG_TYPE_SCHED_ACT] = "sched_act",
197 [BPF_PROG_TYPE_TRACEPOINT] = "tracepoint",
198 [BPF_PROG_TYPE_XDP] = "xdp",
199 [BPF_PROG_TYPE_PERF_EVENT] = "perf_event",
200 [BPF_PROG_TYPE_CGROUP_SKB] = "cgroup_skb",
201 [BPF_PROG_TYPE_CGROUP_SOCK] = "cgroup_sock",
202 [BPF_PROG_TYPE_LWT_IN] = "lwt_in",
203 [BPF_PROG_TYPE_LWT_OUT] = "lwt_out",
204 [BPF_PROG_TYPE_LWT_XMIT] = "lwt_xmit",
205 [BPF_PROG_TYPE_SOCK_OPS] = "sock_ops",
206 [BPF_PROG_TYPE_SK_SKB] = "sk_skb",
207 [BPF_PROG_TYPE_CGROUP_DEVICE] = "cgroup_device",
208 [BPF_PROG_TYPE_SK_MSG] = "sk_msg",
209 [BPF_PROG_TYPE_RAW_TRACEPOINT] = "raw_tracepoint",
210 [BPF_PROG_TYPE_CGROUP_SOCK_ADDR] = "cgroup_sock_addr",
211 [BPF_PROG_TYPE_LWT_SEG6LOCAL] = "lwt_seg6local",
212 [BPF_PROG_TYPE_LIRC_MODE2] = "lirc_mode2",
213 [BPF_PROG_TYPE_SK_REUSEPORT] = "sk_reuseport",
214 [BPF_PROG_TYPE_FLOW_DISSECTOR] = "flow_dissector",
215 [BPF_PROG_TYPE_CGROUP_SYSCTL] = "cgroup_sysctl",
216 [BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE] = "raw_tracepoint_writable",
217 [BPF_PROG_TYPE_CGROUP_SOCKOPT] = "cgroup_sockopt",
218 [BPF_PROG_TYPE_TRACING] = "tracing",
219 [BPF_PROG_TYPE_STRUCT_OPS] = "struct_ops",
220 [BPF_PROG_TYPE_EXT] = "ext",
221 [BPF_PROG_TYPE_LSM] = "lsm",
222 [BPF_PROG_TYPE_SK_LOOKUP] = "sk_lookup",
223 [BPF_PROG_TYPE_SYSCALL] = "syscall",
d0fe92fb 224 [BPF_PROG_TYPE_NETFILTER] = "netfilter",
d18616e7
DM
225};
226
a8a1f7d0
SF
227static int __base_pr(enum libbpf_print_level level, const char *format,
228 va_list args)
b3f59d66 229{
6f1ae8b6
YS
230 if (level == LIBBPF_DEBUG)
231 return 0;
232
a8a1f7d0 233 return vfprintf(stderr, format, args);
b3f59d66
WN
234}
235
a8a1f7d0 236static libbpf_print_fn_t __libbpf_pr = __base_pr;
b3f59d66 237
e87fd8ba 238libbpf_print_fn_t libbpf_set_print(libbpf_print_fn_t fn)
b3f59d66 239{
f1cb927c
JK
240 libbpf_print_fn_t old_print_fn;
241
242 old_print_fn = __atomic_exchange_n(&__libbpf_pr, fn, __ATOMIC_RELAXED);
e87fd8ba 243
e87fd8ba 244 return old_print_fn;
b3f59d66 245}
1a5e3fb1 246
8461ef8b
YS
247__printf(2, 3)
248void libbpf_print(enum libbpf_print_level level, const char *format, ...)
249{
250 va_list args;
d7c5802f 251 int old_errno;
f1cb927c 252 libbpf_print_fn_t print_fn;
8461ef8b 253
f1cb927c
JK
254 print_fn = __atomic_load_n(&__libbpf_pr, __ATOMIC_RELAXED);
255 if (!print_fn)
6f1ae8b6
YS
256 return;
257
d7c5802f
AN
258 old_errno = errno;
259
8461ef8b 260 va_start(args, format);
6f1ae8b6 261 __libbpf_pr(level, format, args);
8461ef8b 262 va_end(args);
d7c5802f
AN
263
264 errno = old_errno;
8461ef8b
YS
265}
266
dc3a2d25
THJ
267static void pr_perm_msg(int err)
268{
269 struct rlimit limit;
270 char buf[100];
271
272 if (err != -EPERM || geteuid() != 0)
273 return;
274
275 err = getrlimit(RLIMIT_MEMLOCK, &limit);
276 if (err)
277 return;
278
279 if (limit.rlim_cur == RLIM_INFINITY)
280 return;
281
282 if (limit.rlim_cur < 1024)
b5c7d0d0 283 snprintf(buf, sizeof(buf), "%zu bytes", (size_t)limit.rlim_cur);
dc3a2d25
THJ
284 else if (limit.rlim_cur < 1024*1024)
285 snprintf(buf, sizeof(buf), "%.1f KiB", (double)limit.rlim_cur / 1024);
286 else
287 snprintf(buf, sizeof(buf), "%.1f MiB", (double)limit.rlim_cur / (1024*1024));
288
289 pr_warn("permission error while running as root; try raising 'ulimit -l'? current value: %s\n",
290 buf);
291}
292
6371ca3b
WN
293#define STRERR_BUFSIZE 128
294
1a5e3fb1
WN
295/* Copied from tools/perf/util/util.h */
296#ifndef zfree
297# define zfree(ptr) ({ free(*ptr); *ptr = NULL; })
298#endif
299
300#ifndef zclose
301# define zclose(fd) ({ \
302 int ___err = 0; \
303 if ((fd) >= 0) \
304 ___err = close((fd)); \
305 fd = -1; \
306 ___err; })
307#endif
308
34be1646
SL
309static inline __u64 ptr_to_u64(const void *ptr)
310{
311 return (__u64) (unsigned long) ptr;
312}
313
5981881d
AN
314int libbpf_set_strict_mode(enum libbpf_strict_mode mode)
315{
bd054102 316 /* as of v1.0 libbpf_set_strict_mode() is a no-op */
5981881d
AN
317 return 0;
318}
319
7615209f
AN
320__u32 libbpf_major_version(void)
321{
322 return LIBBPF_MAJOR_VERSION;
323}
324
325__u32 libbpf_minor_version(void)
326{
327 return LIBBPF_MINOR_VERSION;
328}
329
330const char *libbpf_version_string(void)
331{
332#define __S(X) #X
333#define _S(X) __S(X)
334 return "v" _S(LIBBPF_MAJOR_VERSION) "." _S(LIBBPF_MINOR_VERSION);
335#undef _S
336#undef __S
337}
338
166750bc
AN
339enum reloc_type {
340 RELO_LD64,
341 RELO_CALL,
342 RELO_DATA,
a18f7214
AS
343 RELO_EXTERN_LD64,
344 RELO_EXTERN_CALL,
53eddb5e 345 RELO_SUBPROG_ADDR,
d0e92887 346 RELO_CORE,
166750bc
AN
347};
348
349struct reloc_desc {
350 enum reloc_type type;
351 int insn_idx;
d0e92887
AS
352 union {
353 const struct bpf_core_relo *core_relo; /* used when type == RELO_CORE */
354 struct {
355 int map_idx;
356 int sym_off;
3055ddd6 357 int ext_idx;
d0e92887
AS
358 };
359 };
166750bc
AN
360};
361
15ea31fa
AN
362/* stored as sec_def->cookie for all libbpf-supported SEC()s */
363enum sec_def_flags {
364 SEC_NONE = 0,
365 /* expected_attach_type is optional, if kernel doesn't support that */
366 SEC_EXP_ATTACH_OPT = 1,
367 /* legacy, only used by libbpf_get_type_names() and
368 * libbpf_attach_type_by_name(), not used by libbpf itself at all.
369 * This used to be associated with cgroup (and few other) BPF programs
370 * that were attachable through BPF_PROG_ATTACH command. Pretty
371 * meaningless nowadays, though.
372 */
373 SEC_ATTACHABLE = 2,
374 SEC_ATTACHABLE_OPT = SEC_ATTACHABLE | SEC_EXP_ATTACH_OPT,
375 /* attachment target is specified through BTF ID in either kernel or
e3ba8e4e
KM
376 * other BPF program's BTF object
377 */
15ea31fa
AN
378 SEC_ATTACH_BTF = 4,
379 /* BPF program type allows sleeping/blocking in kernel */
380 SEC_SLEEPABLE = 8,
082c4bfb 381 /* BPF program support non-linear XDP buffer */
450b167f 382 SEC_XDP_FRAGS = 16,
5902da6d
JO
383 /* Setup proper attach type for usdt probes. */
384 SEC_USDT = 32,
15ea31fa
AN
385};
386
25498a19 387struct bpf_sec_def {
697f104d 388 char *sec;
25498a19
AN
389 enum bpf_prog_type prog_type;
390 enum bpf_attach_type expected_attach_type;
15ea31fa 391 long cookie;
697f104d 392 int handler_id;
12d9466d 393
4fa5bcfe
AN
394 libbpf_prog_setup_fn_t prog_setup_fn;
395 libbpf_prog_prepare_load_fn_t prog_prepare_load_fn;
396 libbpf_prog_attach_fn_t prog_attach_fn;
25498a19
AN
397};
398
a5b8bd47
WN
399/*
400 * bpf_prog should be a better name but it has been used in
401 * linux/filter.h.
402 */
403struct bpf_program {
bd054102 404 char *name;
52109584 405 char *sec_name;
c1122392 406 size_t sec_idx;
bd054102 407 const struct bpf_sec_def *sec_def;
c1122392
AN
408 /* this program's instruction offset (in number of instructions)
409 * within its containing ELF section
410 */
411 size_t sec_insn_off;
412 /* number of original instructions in ELF section belonging to this
413 * program, not taking into account subprogram instructions possible
414 * appended later during relocation
415 */
416 size_t sec_insn_cnt;
417 /* Offset (in number of instructions) of the start of instruction
418 * belonging to this BPF program within its containing main BPF
419 * program. For the entry-point (main) BPF program, this is always
420 * zero. For a sub-program, this gets reset before each of main BPF
421 * programs are processed and relocated and is used to determined
422 * whether sub-program was already appended to the main program, and
423 * if yes, at which instruction offset.
424 */
425 size_t sub_insn_off;
426
c1122392
AN
427 /* instructions that belong to BPF program; insns[0] is located at
428 * sec_insn_off instruction within its ELF section in ELF file, so
429 * when mapping ELF file instruction index to the local instruction,
430 * one needs to subtract sec_insn_off; and vice versa.
431 */
a5b8bd47 432 struct bpf_insn *insns;
c1122392
AN
433 /* actual number of instruction in this BPF program's image; for
434 * entry-point BPF programs this includes the size of main program
435 * itself plus all the used sub-programs, appended at the end
436 */
c3c55696 437 size_t insns_cnt;
34090915 438
166750bc 439 struct reloc_desc *reloc_desc;
34090915 440 int nr_reloc;
b3ce9079
AN
441
442 /* BPF verifier log settings */
443 char *log_buf;
444 size_t log_size;
445 __u32 log_level;
55cffde2 446
aa9b1ac3 447 struct bpf_object *obj;
d7be143b 448
cf90a20d 449 int fd;
a3820c48 450 bool autoload;
43cb8cba 451 bool autoattach;
7e2925f6 452 bool sym_global;
aea28a60 453 bool mark_btf_static;
c1122392 454 enum bpf_prog_type type;
d7be143b 455 enum bpf_attach_type expected_attach_type;
7e2925f6 456 int exception_cb_idx;
cf90a20d 457
c1122392 458 int prog_ifindex;
91abb4a6 459 __u32 attach_btf_obj_fd;
12a8654b 460 __u32 attach_btf_id;
e7bf94db 461 __u32 attach_prog_fd;
cf90a20d 462
2993e051
YS
463 void *func_info;
464 __u32 func_info_rec_size;
f0187f0b 465 __u32 func_info_cnt;
47eff617 466
3d650141
MKL
467 void *line_info;
468 __u32 line_info_rec_size;
469 __u32 line_info_cnt;
04656198 470 __u32 prog_flags;
a5b8bd47
WN
471};
472
590a0088
MKL
473struct bpf_struct_ops {
474 const char *tname;
475 const struct btf_type *type;
476 struct bpf_program **progs;
477 __u32 *kern_func_off;
478 /* e.g. struct tcp_congestion_ops in bpf_prog's btf format */
479 void *data;
480 /* e.g. struct bpf_struct_ops_tcp_congestion_ops in
481 * btf_vmlinux's format.
482 * struct bpf_struct_ops_tcp_congestion_ops {
483 * [... some other kernel fields ...]
484 * struct tcp_congestion_ops data;
485 * }
486 * kern_vdata-size == sizeof(struct bpf_struct_ops_tcp_congestion_ops)
487 * bpf_map__init_kern_struct_ops() will populate the "kern_vdata"
488 * from "data".
489 */
490 void *kern_vdata;
491 __u32 type_id;
492};
493
ac9d1389
AN
494#define DATA_SEC ".data"
495#define BSS_SEC ".bss"
496#define RODATA_SEC ".rodata"
81bfdd08 497#define KCONFIG_SEC ".kconfig"
1c0c7074 498#define KSYMS_SEC ".ksyms"
590a0088 499#define STRUCT_OPS_SEC ".struct_ops"
809a69d6 500#define STRUCT_OPS_LINK_SEC ".struct_ops.link"
2e7ba4f8 501#define ARENA_SEC ".arena.1"
ac9d1389 502
d859900c
DB
503enum libbpf_map_type {
504 LIBBPF_MAP_UNSPEC,
505 LIBBPF_MAP_DATA,
506 LIBBPF_MAP_BSS,
507 LIBBPF_MAP_RODATA,
81bfdd08 508 LIBBPF_MAP_KCONFIG,
d859900c
DB
509};
510
146bf811
AN
511struct bpf_map_def {
512 unsigned int type;
513 unsigned int key_size;
514 unsigned int value_size;
515 unsigned int max_entries;
516 unsigned int map_flags;
517};
518
9d759a9b 519struct bpf_map {
ec41817b 520 struct bpf_object *obj;
561bbcca 521 char *name;
aed65917
AN
522 /* real_name is defined for special internal maps (.rodata*,
523 * .data*, .bss, .kconfig) and preserves their original ELF section
e3ba8e4e 524 * name. This is important to be able to find corresponding BTF
aed65917
AN
525 * DATASEC information.
526 */
527 char *real_name;
01af3bf0 528 int fd;
db48814b
AN
529 int sec_idx;
530 size_t sec_offset;
f0307a7e 531 int map_ifindex;
addb9fc9 532 int inner_map_fd;
9d759a9b 533 struct bpf_map_def def;
1bdb6c9a 534 __u32 numa_node;
646f02ff 535 __u32 btf_var_idx;
9e926acd 536 int mod_btf_fd;
5b891af7
MKL
537 __u32 btf_key_type_id;
538 __u32 btf_value_type_id;
590a0088 539 __u32 btf_vmlinux_value_type_id;
d859900c 540 enum libbpf_map_type libbpf_type;
eba9c5f4 541 void *mmaped;
590a0088 542 struct bpf_struct_ops *st_ops;
646f02ff
AN
543 struct bpf_map *inner_map;
544 void **init_slots;
545 int init_slots_sz;
4580b25f
THJ
546 char *pin_path;
547 bool pinned;
ec6d5f47 548 bool reused;
ec41817b 549 bool autocreate;
47512102 550 __u64 map_extra;
d859900c
DB
551};
552
166750bc
AN
553enum extern_type {
554 EXT_UNKNOWN,
2e33efe3 555 EXT_KCFG,
1c0c7074 556 EXT_KSYM,
2e33efe3
AN
557};
558
559enum kcfg_type {
560 KCFG_UNKNOWN,
561 KCFG_CHAR,
562 KCFG_BOOL,
563 KCFG_INT,
564 KCFG_TRISTATE,
565 KCFG_CHAR_ARR,
166750bc
AN
566};
567
568struct extern_desc {
2e33efe3 569 enum extern_type type;
166750bc
AN
570 int sym_idx;
571 int btf_id;
2e33efe3
AN
572 int sec_btf_id;
573 const char *name;
5964a223 574 char *essent_name;
166750bc 575 bool is_set;
2e33efe3
AN
576 bool is_weak;
577 union {
578 struct {
579 enum kcfg_type type;
580 int sz;
581 int align;
582 int data_off;
583 bool is_signed;
584 } kcfg;
1c0c7074
AN
585 struct {
586 unsigned long long addr;
d370bbe1
HL
587
588 /* target btf_id of the corresponding kernel var. */
284d2587
AN
589 int kernel_btf_obj_fd;
590 int kernel_btf_id;
d370bbe1
HL
591
592 /* local btf_id of the ksym extern's type. */
593 __u32 type_id;
9dbe6015
KKD
594 /* BTF fd index to be patched in for insn->off, this is
595 * 0 for vmlinux BTF, index in obj->fd_array for module
596 * BTF
597 */
598 __s16 btf_fd_idx;
1c0c7074 599 } ksym;
2e33efe3 600 };
166750bc
AN
601};
602
4f33a53d
AN
603struct module_btf {
604 struct btf *btf;
605 char *name;
606 __u32 id;
91abb4a6 607 int fd;
9dbe6015 608 int fd_array_idx;
4f33a53d
AN
609};
610
25bbbd7a
AN
611enum sec_type {
612 SEC_UNUSED = 0,
613 SEC_RELO,
614 SEC_BSS,
615 SEC_DATA,
616 SEC_RODATA,
240bf8a5 617 SEC_ST_OPS,
25bbbd7a
AN
618};
619
620struct elf_sec_desc {
621 enum sec_type sec_type;
622 Elf64_Shdr *shdr;
623 Elf_Data *data;
624};
625
29a30ff5
AN
626struct elf_state {
627 int fd;
628 const void *obj_buf;
629 size_t obj_buf_sz;
630 Elf *elf;
ad23b723 631 Elf64_Ehdr *ehdr;
29a30ff5 632 Elf_Data *symbols;
2e7ba4f8 633 Elf_Data *arena_data;
29a30ff5
AN
634 size_t shstrndx; /* section index for section name strings */
635 size_t strtabidx;
25bbbd7a 636 struct elf_sec_desc *secs;
51deedc9 637 size_t sec_cnt;
29a30ff5
AN
638 int btf_maps_shndx;
639 __u32 btf_maps_sec_btf_id;
640 int text_shndx;
641 int symbols_shndx;
240bf8a5 642 bool has_st_ops;
2e7ba4f8 643 int arena_data_shndx;
4f33a53d
AN
644};
645
2e4913e0
AN
646struct usdt_manager;
647
1a5e3fb1 648struct bpf_object {
d859900c 649 char name[BPF_OBJ_NAME_LEN];
cb1e5e96 650 char license[64];
438363c0 651 __u32 kern_version;
0b3d1efa 652
a5b8bd47
WN
653 struct bpf_program *programs;
654 size_t nr_programs;
9d759a9b
WN
655 struct bpf_map *maps;
656 size_t nr_maps;
bf829271 657 size_t maps_cap;
9d759a9b 658
8601fd42 659 char *kconfig;
166750bc
AN
660 struct extern_desc *externs;
661 int nr_extern;
81bfdd08 662 int kconfig_map_idx;
166750bc 663
52d3352e 664 bool loaded;
c3c55696 665 bool has_subcalls;
25bbbd7a 666 bool has_rodata;
a5b8bd47 667
e2fa0156
AS
668 struct bpf_gen *gen_loader;
669
29a30ff5
AN
670 /* Information when doing ELF related work. Only valid if efile.elf is not NULL */
671 struct elf_state efile;
10931d24 672
8a138aed 673 struct btf *btf;
0f7515ca
AN
674 struct btf_ext *btf_ext;
675
a6ed02ca
KS
676 /* Parse and load BTF vmlinux if any of the programs in the object need
677 * it at load time.
678 */
679 struct btf *btf_vmlinux;
1373ff59
SC
680 /* Path to the custom BTF to be used for BPF CO-RE relocations as an
681 * override for vmlinux BTF.
682 */
683 char *btf_custom_path;
0f7515ca
AN
684 /* vmlinux BTF override for CO-RE relocations */
685 struct btf *btf_vmlinux_override;
4f33a53d
AN
686 /* Lazily initialized kernel module BTFs */
687 struct module_btf *btf_modules;
688 bool btf_modules_loaded;
689 size_t btf_module_cnt;
690 size_t btf_module_cap;
8a138aed 691
e0e3ea88
AN
692 /* optional log settings passed to BPF_BTF_LOAD and BPF_PROG_LOAD commands */
693 char *log_buf;
694 size_t log_size;
695 __u32 log_level;
696
9dbe6015
KKD
697 int *fd_array;
698 size_t fd_array_cap;
699 size_t fd_array_cnt;
700
2e4913e0
AN
701 struct usdt_manager *usdt_man;
702
2e7ba4f8
AN
703 struct bpf_map *arena_map;
704 void *arena_data;
705 size_t arena_data_sz;
706
6b434b61
AN
707 struct kern_feature_cache *feat_cache;
708 char *token_path;
709 int token_fd;
710
1a5e3fb1
WN
711 char path[];
712};
1a5e3fb1 713
88a82120
AN
714static const char *elf_sym_str(const struct bpf_object *obj, size_t off);
715static const char *elf_sec_str(const struct bpf_object *obj, size_t off);
716static Elf_Scn *elf_sec_by_idx(const struct bpf_object *obj, size_t idx);
717static Elf_Scn *elf_sec_by_name(const struct bpf_object *obj, const char *name);
ad23b723 718static Elf64_Shdr *elf_sec_hdr(const struct bpf_object *obj, Elf_Scn *scn);
88a82120
AN
719static const char *elf_sec_name(const struct bpf_object *obj, Elf_Scn *scn);
720static Elf_Data *elf_sec_data(const struct bpf_object *obj, Elf_Scn *scn);
ad23b723
AN
721static Elf64_Sym *elf_sym_by_idx(const struct bpf_object *obj, size_t idx);
722static Elf64_Rel *elf_rel_by_idx(Elf_Data *data, size_t idx);
88a82120 723
29cd77f4 724void bpf_program__unload(struct bpf_program *prog)
55cffde2
WN
725{
726 if (!prog)
727 return;
728
cf90a20d 729 zclose(prog->fd);
2993e051 730
2993e051 731 zfree(&prog->func_info);
07a09d1b 732 zfree(&prog->line_info);
55cffde2
WN
733}
734
a5b8bd47
WN
735static void bpf_program__exit(struct bpf_program *prog)
736{
737 if (!prog)
738 return;
739
55cffde2 740 bpf_program__unload(prog);
88cda1c9 741 zfree(&prog->name);
52109584 742 zfree(&prog->sec_name);
a5b8bd47 743 zfree(&prog->insns);
34090915
WN
744 zfree(&prog->reloc_desc);
745
746 prog->nr_reloc = 0;
a5b8bd47 747 prog->insns_cnt = 0;
c1122392 748 prog->sec_idx = -1;
a5b8bd47
WN
749}
750
c3c55696
AN
751static bool insn_is_subprog_call(const struct bpf_insn *insn)
752{
753 return BPF_CLASS(insn->code) == BPF_JMP &&
754 BPF_OP(insn->code) == BPF_CALL &&
755 BPF_SRC(insn->code) == BPF_K &&
756 insn->src_reg == BPF_PSEUDO_CALL &&
757 insn->dst_reg == 0 &&
758 insn->off == 0;
759}
760
aa0b8d43
MKL
761static bool is_call_insn(const struct bpf_insn *insn)
762{
763 return insn->code == (BPF_JMP | BPF_CALL);
764}
765
53eddb5e
YS
766static bool insn_is_pseudo_func(struct bpf_insn *insn)
767{
aa0b8d43 768 return is_ldimm64_insn(insn) && insn->src_reg == BPF_PSEUDO_FUNC;
53eddb5e
YS
769}
770
a5b8bd47 771static int
c3c55696
AN
772bpf_object__init_prog(struct bpf_object *obj, struct bpf_program *prog,
773 const char *name, size_t sec_idx, const char *sec_name,
774 size_t sec_off, void *insn_data, size_t insn_data_sz)
a5b8bd47 775{
c1122392
AN
776 if (insn_data_sz == 0 || insn_data_sz % BPF_INSN_SZ || sec_off % BPF_INSN_SZ) {
777 pr_warn("sec '%s': corrupted program '%s', offset %zu, size %zu\n",
778 sec_name, name, sec_off, insn_data_sz);
a5b8bd47
WN
779 return -EINVAL;
780 }
781
1ad9cbb8 782 memset(prog, 0, sizeof(*prog));
c3c55696
AN
783 prog->obj = obj;
784
c1122392
AN
785 prog->sec_idx = sec_idx;
786 prog->sec_insn_off = sec_off / BPF_INSN_SZ;
787 prog->sec_insn_cnt = insn_data_sz / BPF_INSN_SZ;
788 /* insns_cnt can later be increased by appending used subprograms */
789 prog->insns_cnt = prog->sec_insn_cnt;
a5b8bd47 790
c1122392 791 prog->type = BPF_PROG_TYPE_UNSPEC;
cf90a20d 792 prog->fd = -1;
7e2925f6 793 prog->exception_cb_idx = -1;
a3820c48
AN
794
795 /* libbpf's convention for SEC("?abc...") is that it's just like
796 * SEC("abc...") but the corresponding bpf_program starts out with
797 * autoload set to false.
798 */
799 if (sec_name[0] == '?') {
800 prog->autoload = false;
801 /* from now on forget there was ? in section name */
802 sec_name++;
803 } else {
804 prog->autoload = true;
805 }
a5b8bd47 806
43cb8cba
HL
807 prog->autoattach = true;
808
b3ce9079
AN
809 /* inherit object's log_level */
810 prog->log_level = obj->log_level;
811
52109584
AN
812 prog->sec_name = strdup(sec_name);
813 if (!prog->sec_name)
c1122392
AN
814 goto errout;
815
816 prog->name = strdup(name);
817 if (!prog->name)
a5b8bd47 818 goto errout;
a5b8bd47 819
c1122392
AN
820 prog->insns = malloc(insn_data_sz);
821 if (!prog->insns)
a5b8bd47 822 goto errout;
c1122392 823 memcpy(prog->insns, insn_data, insn_data_sz);
a5b8bd47
WN
824
825 return 0;
826errout:
c1122392 827 pr_warn("sec '%s': failed to allocate memory for prog '%s'\n", sec_name, name);
a5b8bd47
WN
828 bpf_program__exit(prog);
829 return -ENOMEM;
830}
831
832static int
c1122392
AN
833bpf_object__add_programs(struct bpf_object *obj, Elf_Data *sec_data,
834 const char *sec_name, int sec_idx)
a5b8bd47 835{
6245947c 836 Elf_Data *symbols = obj->efile.symbols;
c1122392
AN
837 struct bpf_program *prog, *progs;
838 void *data = sec_data->d_buf;
6245947c
AN
839 size_t sec_sz = sec_data->d_size, sec_off, prog_sz, nr_syms;
840 int nr_progs, err, i;
c1122392 841 const char *name;
ad23b723 842 Elf64_Sym *sym;
a5b8bd47
WN
843
844 progs = obj->programs;
845 nr_progs = obj->nr_programs;
ad23b723 846 nr_syms = symbols->d_size / sizeof(Elf64_Sym);
a5b8bd47 847
6245947c 848 for (i = 0; i < nr_syms; i++) {
ad23b723
AN
849 sym = elf_sym_by_idx(obj, i);
850
851 if (sym->st_shndx != sec_idx)
6245947c 852 continue;
ad23b723 853 if (ELF64_ST_TYPE(sym->st_info) != STT_FUNC)
6245947c 854 continue;
88cda1c9 855
ad23b723
AN
856 prog_sz = sym->st_size;
857 sec_off = sym->st_value;
88cda1c9 858
ad23b723 859 name = elf_sym_str(obj, sym->st_name);
c1122392
AN
860 if (!name) {
861 pr_warn("sec '%s': failed to get symbol name for offset %zu\n",
862 sec_name, sec_off);
863 return -LIBBPF_ERRNO__FORMAT;
864 }
88cda1c9 865
c1122392
AN
866 if (sec_off + prog_sz > sec_sz) {
867 pr_warn("sec '%s': program at offset %zu crosses section boundary\n",
868 sec_name, sec_off);
869 return -LIBBPF_ERRNO__FORMAT;
870 }
88cda1c9 871
ad23b723 872 if (sec_idx != obj->efile.text_shndx && ELF64_ST_BIND(sym->st_info) == STB_LOCAL) {
513f485c
AN
873 pr_warn("sec '%s': program '%s' is static and not supported\n", sec_name, name);
874 return -ENOTSUP;
875 }
876
c3c55696
AN
877 pr_debug("sec '%s': found program '%s' at insn offset %zu (%zu bytes), code size %zu insns (%zu bytes)\n",
878 sec_name, name, sec_off / BPF_INSN_SZ, sec_off, prog_sz / BPF_INSN_SZ, prog_sz);
88cda1c9 879
c3c55696 880 progs = libbpf_reallocarray(progs, nr_progs + 1, sizeof(*progs));
c1122392
AN
881 if (!progs) {
882 /*
883 * In this case the original obj->programs
884 * is still valid, so don't need special treat for
885 * bpf_close_object().
886 */
887 pr_warn("sec '%s': failed to alloc memory for new program '%s'\n",
888 sec_name, name);
889 return -ENOMEM;
88cda1c9 890 }
c1122392 891 obj->programs = progs;
88cda1c9 892
c1122392 893 prog = &progs[nr_progs];
9a94f277 894
c3c55696
AN
895 err = bpf_object__init_prog(obj, prog, name, sec_idx, sec_name,
896 sec_off, data + sec_off, prog_sz);
c1122392
AN
897 if (err)
898 return err;
9a94f277 899
7e2925f6
KKD
900 if (ELF64_ST_BIND(sym->st_info) != STB_LOCAL)
901 prog->sym_global = true;
902
e5670fa0
AN
903 /* if function is a global/weak symbol, but has restricted
904 * (STV_HIDDEN or STV_INTERNAL) visibility, mark its BTF FUNC
905 * as static to enable more permissive BPF verification mode
906 * with more outside context available to BPF verifier
aea28a60 907 */
7e2925f6
KKD
908 if (prog->sym_global && (ELF64_ST_VISIBILITY(sym->st_other) == STV_HIDDEN
909 || ELF64_ST_VISIBILITY(sym->st_other) == STV_INTERNAL))
aea28a60
AN
910 prog->mark_btf_static = true;
911
c1122392
AN
912 nr_progs++;
913 obj->nr_programs = nr_progs;
88cda1c9
MKL
914 }
915
916 return 0;
917}
918
590a0088
MKL
919static const struct btf_member *
920find_member_by_offset(const struct btf_type *t, __u32 bit_offset)
921{
922 struct btf_member *m;
923 int i;
924
925 for (i = 0, m = btf_members(t); i < btf_vlen(t); i++, m++) {
926 if (btf_member_bit_offset(t, i) == bit_offset)
927 return m;
928 }
929
930 return NULL;
931}
932
933static const struct btf_member *
934find_member_by_name(const struct btf *btf, const struct btf_type *t,
935 const char *name)
936{
937 struct btf_member *m;
938 int i;
939
940 for (i = 0, m = btf_members(t); i < btf_vlen(t); i++, m++) {
941 if (!strcmp(btf__name_by_offset(btf, m->name_off), name))
942 return m;
943 }
944
945 return NULL;
946}
947
9e926acd
KFL
948static int find_ksym_btf_id(struct bpf_object *obj, const char *ksym_name,
949 __u16 kind, struct btf **res_btf,
950 struct module_btf **res_mod_btf);
951
590a0088 952#define STRUCT_OPS_VALUE_PREFIX "bpf_struct_ops_"
a6ed02ca
KS
953static int find_btf_by_prefix_kind(const struct btf *btf, const char *prefix,
954 const char *name, __u32 kind);
590a0088
MKL
955
956static int
a2a5172c 957find_struct_ops_kern_types(struct bpf_object *obj, const char *tname_raw,
9e926acd 958 struct module_btf **mod_btf,
590a0088
MKL
959 const struct btf_type **type, __u32 *type_id,
960 const struct btf_type **vtype, __u32 *vtype_id,
961 const struct btf_member **data_member)
962{
963 const struct btf_type *kern_type, *kern_vtype;
964 const struct btf_member *kern_data_member;
9e926acd 965 struct btf *btf;
590a0088 966 __s32 kern_vtype_id, kern_type_id;
a2a5172c 967 char tname[256];
590a0088
MKL
968 __u32 i;
969
a2a5172c
EZ
970 snprintf(tname, sizeof(tname), "%.*s",
971 (int)bpf_core_essential_name_len(tname_raw), tname_raw);
972
9e926acd
KFL
973 kern_type_id = find_ksym_btf_id(obj, tname, BTF_KIND_STRUCT,
974 &btf, mod_btf);
590a0088
MKL
975 if (kern_type_id < 0) {
976 pr_warn("struct_ops init_kern: struct %s is not found in kernel BTF\n",
977 tname);
978 return kern_type_id;
979 }
980 kern_type = btf__type_by_id(btf, kern_type_id);
981
982 /* Find the corresponding "map_value" type that will be used
983 * in map_update(BPF_MAP_TYPE_STRUCT_OPS). For example,
984 * find "struct bpf_struct_ops_tcp_congestion_ops" from the
985 * btf_vmlinux.
986 */
a6ed02ca
KS
987 kern_vtype_id = find_btf_by_prefix_kind(btf, STRUCT_OPS_VALUE_PREFIX,
988 tname, BTF_KIND_STRUCT);
590a0088 989 if (kern_vtype_id < 0) {
a6ed02ca
KS
990 pr_warn("struct_ops init_kern: struct %s%s is not found in kernel BTF\n",
991 STRUCT_OPS_VALUE_PREFIX, tname);
590a0088
MKL
992 return kern_vtype_id;
993 }
994 kern_vtype = btf__type_by_id(btf, kern_vtype_id);
995
996 /* Find "struct tcp_congestion_ops" from
997 * struct bpf_struct_ops_tcp_congestion_ops {
998 * [ ... ]
999 * struct tcp_congestion_ops data;
1000 * }
1001 */
1002 kern_data_member = btf_members(kern_vtype);
1003 for (i = 0; i < btf_vlen(kern_vtype); i++, kern_data_member++) {
1004 if (kern_data_member->type == kern_type_id)
1005 break;
1006 }
1007 if (i == btf_vlen(kern_vtype)) {
a6ed02ca
KS
1008 pr_warn("struct_ops init_kern: struct %s data is not found in struct %s%s\n",
1009 tname, STRUCT_OPS_VALUE_PREFIX, tname);
590a0088
MKL
1010 return -EINVAL;
1011 }
1012
1013 *type = kern_type;
1014 *type_id = kern_type_id;
1015 *vtype = kern_vtype;
1016 *vtype_id = kern_vtype_id;
1017 *data_member = kern_data_member;
1018
1019 return 0;
1020}
1021
1022static bool bpf_map__is_struct_ops(const struct bpf_map *map)
1023{
1024 return map->def.type == BPF_MAP_TYPE_STRUCT_OPS;
1025}
1026
69e4a9d2
KFL
1027static bool is_valid_st_ops_program(struct bpf_object *obj,
1028 const struct bpf_program *prog)
1029{
1030 int i;
1031
1032 for (i = 0; i < obj->nr_programs; i++) {
1033 if (&obj->programs[i] == prog)
1034 return prog->type == BPF_PROG_TYPE_STRUCT_OPS;
1035 }
1036
1037 return false;
1038}
1039
fe9d049c
EZ
1040/* For each struct_ops program P, referenced from some struct_ops map M,
1041 * enable P.autoload if there are Ms for which M.autocreate is true,
1042 * disable P.autoload if for all Ms M.autocreate is false.
1043 * Don't change P.autoload for programs that are not referenced from any maps.
1044 */
1045static int bpf_object_adjust_struct_ops_autoload(struct bpf_object *obj)
1046{
1047 struct bpf_program *prog, *slot_prog;
1048 struct bpf_map *map;
1049 int i, j, k, vlen;
1050
1051 for (i = 0; i < obj->nr_programs; ++i) {
1052 int should_load = false;
1053 int use_cnt = 0;
1054
1055 prog = &obj->programs[i];
1056 if (prog->type != BPF_PROG_TYPE_STRUCT_OPS)
1057 continue;
1058
1059 for (j = 0; j < obj->nr_maps; ++j) {
1060 map = &obj->maps[j];
1061 if (!bpf_map__is_struct_ops(map))
1062 continue;
1063
1064 vlen = btf_vlen(map->st_ops->type);
1065 for (k = 0; k < vlen; ++k) {
1066 slot_prog = map->st_ops->progs[k];
1067 if (prog != slot_prog)
1068 continue;
1069
1070 use_cnt++;
1071 if (map->autocreate)
1072 should_load = true;
1073 }
1074 }
1075 if (use_cnt)
1076 prog->autoload = should_load;
1077 }
1078
1079 return 0;
1080}
1081
590a0088 1082/* Init the map's fields that depend on kern_btf */
9e926acd 1083static int bpf_map__init_kern_struct_ops(struct bpf_map *map)
590a0088
MKL
1084{
1085 const struct btf_member *member, *kern_member, *kern_data_member;
1086 const struct btf_type *type, *kern_type, *kern_vtype;
1087 __u32 i, kern_type_id, kern_vtype_id, kern_data_off;
9e926acd
KFL
1088 struct bpf_object *obj = map->obj;
1089 const struct btf *btf = obj->btf;
590a0088 1090 struct bpf_struct_ops *st_ops;
9e926acd
KFL
1091 const struct btf *kern_btf;
1092 struct module_btf *mod_btf;
590a0088
MKL
1093 void *data, *kern_data;
1094 const char *tname;
1095 int err;
1096
1097 st_ops = map->st_ops;
1098 type = st_ops->type;
1099 tname = st_ops->tname;
9e926acd 1100 err = find_struct_ops_kern_types(obj, tname, &mod_btf,
590a0088
MKL
1101 &kern_type, &kern_type_id,
1102 &kern_vtype, &kern_vtype_id,
1103 &kern_data_member);
1104 if (err)
1105 return err;
1106
9e926acd
KFL
1107 kern_btf = mod_btf ? mod_btf->btf : obj->btf_vmlinux;
1108
590a0088
MKL
1109 pr_debug("struct_ops init_kern %s: type_id:%u kern_type_id:%u kern_vtype_id:%u\n",
1110 map->name, st_ops->type_id, kern_type_id, kern_vtype_id);
1111
9e926acd 1112 map->mod_btf_fd = mod_btf ? mod_btf->fd : -1;
590a0088
MKL
1113 map->def.value_size = kern_vtype->size;
1114 map->btf_vmlinux_value_type_id = kern_vtype_id;
1115
1116 st_ops->kern_vdata = calloc(1, kern_vtype->size);
1117 if (!st_ops->kern_vdata)
1118 return -ENOMEM;
1119
1120 data = st_ops->data;
1121 kern_data_off = kern_data_member->offset / 8;
1122 kern_data = st_ops->kern_vdata + kern_data_off;
1123
1124 member = btf_members(type);
1125 for (i = 0; i < btf_vlen(type); i++, member++) {
1126 const struct btf_type *mtype, *kern_mtype;
1127 __u32 mtype_id, kern_mtype_id;
1128 void *mdata, *kern_mdata;
1129 __s64 msize, kern_msize;
1130 __u32 moff, kern_moff;
1131 __u32 kern_member_idx;
1132 const char *mname;
1133
1134 mname = btf__name_by_offset(btf, member->name_off);
c911fc61
KFL
1135 moff = member->offset / 8;
1136 mdata = data + moff;
1137 msize = btf__resolve_size(btf, member->type);
1138 if (msize < 0) {
1139 pr_warn("struct_ops init_kern %s: failed to resolve the size of member %s\n",
1140 map->name, mname);
1141 return msize;
1142 }
1143
590a0088
MKL
1144 kern_member = find_member_by_name(kern_btf, kern_type, mname);
1145 if (!kern_member) {
c911fc61
KFL
1146 /* Skip all zeros or null fields if they are not
1147 * presented in the kernel BTF.
1148 */
1149 if (libbpf_is_mem_zeroed(mdata, msize)) {
1150 pr_info("struct_ops %s: member %s not found in kernel, skipping it as it's set to zero\n",
1151 map->name, mname);
1152 continue;
1153 }
1154
590a0088
MKL
1155 pr_warn("struct_ops init_kern %s: Cannot find member %s in kernel BTF\n",
1156 map->name, mname);
1157 return -ENOTSUP;
1158 }
1159
1160 kern_member_idx = kern_member - btf_members(kern_type);
1161 if (btf_member_bitfield_size(type, i) ||
1162 btf_member_bitfield_size(kern_type, kern_member_idx)) {
1163 pr_warn("struct_ops init_kern %s: bitfield %s is not supported\n",
1164 map->name, mname);
1165 return -ENOTSUP;
1166 }
1167
590a0088 1168 kern_moff = kern_member->offset / 8;
590a0088
MKL
1169 kern_mdata = kern_data + kern_moff;
1170
1171 mtype = skip_mods_and_typedefs(btf, member->type, &mtype_id);
1172 kern_mtype = skip_mods_and_typedefs(kern_btf, kern_member->type,
1173 &kern_mtype_id);
1174 if (BTF_INFO_KIND(mtype->info) !=
1175 BTF_INFO_KIND(kern_mtype->info)) {
1176 pr_warn("struct_ops init_kern %s: Unmatched member type %s %u != %u(kernel)\n",
1177 map->name, mname, BTF_INFO_KIND(mtype->info),
1178 BTF_INFO_KIND(kern_mtype->info));
1179 return -ENOTSUP;
1180 }
1181
1182 if (btf_is_ptr(mtype)) {
1183 struct bpf_program *prog;
1184
69e4a9d2
KFL
1185 /* Update the value from the shadow type */
1186 prog = *(void **)mdata;
1187 st_ops->progs[i] = prog;
d2836ddd
MKL
1188 if (!prog)
1189 continue;
69e4a9d2
KFL
1190 if (!is_valid_st_ops_program(obj, prog)) {
1191 pr_warn("struct_ops init_kern %s: member %s is not a struct_ops program\n",
1192 map->name, mname);
1193 return -ENOTSUP;
1194 }
d2836ddd 1195
590a0088
MKL
1196 kern_mtype = skip_mods_and_typedefs(kern_btf,
1197 kern_mtype->type,
1198 &kern_mtype_id);
d2836ddd
MKL
1199
1200 /* mtype->type must be a func_proto which was
1201 * guaranteed in bpf_object__collect_st_ops_relos(),
1202 * so only check kern_mtype for func_proto here.
1203 */
1204 if (!btf_is_func_proto(kern_mtype)) {
1205 pr_warn("struct_ops init_kern %s: kernel member %s is not a func ptr\n",
590a0088
MKL
1206 map->name, mname);
1207 return -ENOTSUP;
1208 }
1209
9e926acd
KFL
1210 if (mod_btf)
1211 prog->attach_btf_obj_fd = mod_btf->fd;
d9ab2f76
EZ
1212
1213 /* if we haven't yet processed this BPF program, record proper
1214 * attach_btf_id and member_idx
1215 */
1216 if (!prog->attach_btf_id) {
1217 prog->attach_btf_id = kern_type_id;
1218 prog->expected_attach_type = kern_member_idx;
1219 }
1220
1221 /* struct_ops BPF prog can be re-used between multiple
1222 * .struct_ops & .struct_ops.link as long as it's the
1223 * same struct_ops struct definition and the same
1224 * function pointer field
1225 */
1226 if (prog->attach_btf_id != kern_type_id) {
1227 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",
1228 map->name, mname, prog->name, prog->sec_name, prog->type,
1229 prog->attach_btf_id, kern_type_id);
1230 return -EINVAL;
1231 }
1232 if (prog->expected_attach_type != kern_member_idx) {
1233 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",
1234 map->name, mname, prog->name, prog->sec_name, prog->type,
1235 prog->expected_attach_type, kern_member_idx);
1236 return -EINVAL;
1237 }
590a0088
MKL
1238
1239 st_ops->kern_func_off[i] = kern_data_off + kern_moff;
1240
1241 pr_debug("struct_ops init_kern %s: func ptr %s is set to prog %s from data(+%u) to kern_data(+%u)\n",
1242 map->name, mname, prog->name, moff,
1243 kern_moff);
1244
1245 continue;
1246 }
1247
590a0088 1248 kern_msize = btf__resolve_size(kern_btf, kern_mtype_id);
c911fc61 1249 if (kern_msize < 0 || msize != kern_msize) {
590a0088
MKL
1250 pr_warn("struct_ops init_kern %s: Error in size of member %s: %zd != %zd(kernel)\n",
1251 map->name, mname, (ssize_t)msize,
1252 (ssize_t)kern_msize);
1253 return -ENOTSUP;
1254 }
1255
1256 pr_debug("struct_ops init_kern %s: copy %s %u bytes from data(+%u) to kern_data(+%u)\n",
1257 map->name, mname, (unsigned int)msize,
1258 moff, kern_moff);
1259 memcpy(kern_mdata, mdata, msize);
1260 }
1261
1262 return 0;
1263}
1264
1265static int bpf_object__init_kern_struct_ops_maps(struct bpf_object *obj)
1266{
590a0088
MKL
1267 struct bpf_map *map;
1268 size_t i;
1269 int err;
1270
1271 for (i = 0; i < obj->nr_maps; i++) {
1272 map = &obj->maps[i];
1273
1274 if (!bpf_map__is_struct_ops(map))
1275 continue;
1276
8db05261
EZ
1277 if (!map->autocreate)
1278 continue;
1279
9e926acd 1280 err = bpf_map__init_kern_struct_ops(map);
a6ed02ca 1281 if (err)
590a0088 1282 return err;
590a0088
MKL
1283 }
1284
590a0088
MKL
1285 return 0;
1286}
1287
809a69d6 1288static int init_struct_ops_maps(struct bpf_object *obj, const char *sec_name,
240bf8a5 1289 int shndx, Elf_Data *data)
590a0088
MKL
1290{
1291 const struct btf_type *type, *datasec;
1292 const struct btf_var_secinfo *vsi;
1293 struct bpf_struct_ops *st_ops;
1294 const char *tname, *var_name;
1295 __s32 type_id, datasec_id;
1296 const struct btf *btf;
1297 struct bpf_map *map;
1298 __u32 i;
1299
809a69d6 1300 if (shndx == -1)
590a0088
MKL
1301 return 0;
1302
1303 btf = obj->btf;
809a69d6 1304 datasec_id = btf__find_by_name_kind(btf, sec_name,
590a0088
MKL
1305 BTF_KIND_DATASEC);
1306 if (datasec_id < 0) {
1307 pr_warn("struct_ops init: DATASEC %s not found\n",
809a69d6 1308 sec_name);
590a0088
MKL
1309 return -EINVAL;
1310 }
1311
1312 datasec = btf__type_by_id(btf, datasec_id);
1313 vsi = btf_var_secinfos(datasec);
1314 for (i = 0; i < btf_vlen(datasec); i++, vsi++) {
1315 type = btf__type_by_id(obj->btf, vsi->type);
1316 var_name = btf__name_by_offset(obj->btf, type->name_off);
1317
1318 type_id = btf__resolve_type(obj->btf, vsi->type);
1319 if (type_id < 0) {
1320 pr_warn("struct_ops init: Cannot resolve var type_id %u in DATASEC %s\n",
809a69d6 1321 vsi->type, sec_name);
590a0088
MKL
1322 return -EINVAL;
1323 }
1324
1325 type = btf__type_by_id(obj->btf, type_id);
1326 tname = btf__name_by_offset(obj->btf, type->name_off);
1327 if (!tname[0]) {
1328 pr_warn("struct_ops init: anonymous type is not supported\n");
1329 return -ENOTSUP;
1330 }
1331 if (!btf_is_struct(type)) {
1332 pr_warn("struct_ops init: %s is not a struct\n", tname);
1333 return -EINVAL;
1334 }
1335
1336 map = bpf_object__add_map(obj);
1337 if (IS_ERR(map))
1338 return PTR_ERR(map);
1339
809a69d6 1340 map->sec_idx = shndx;
590a0088
MKL
1341 map->sec_offset = vsi->offset;
1342 map->name = strdup(var_name);
1343 if (!map->name)
1344 return -ENOMEM;
3644d285 1345 map->btf_value_type_id = type_id;
590a0088 1346
5ad0ecbe
EZ
1347 /* Follow same convention as for programs autoload:
1348 * SEC("?.struct_ops") means map is not created by default.
1349 */
1350 if (sec_name[0] == '?') {
1351 map->autocreate = false;
1352 /* from now on forget there was ? in section name */
1353 sec_name++;
1354 }
1355
590a0088
MKL
1356 map->def.type = BPF_MAP_TYPE_STRUCT_OPS;
1357 map->def.key_size = sizeof(int);
1358 map->def.value_size = type->size;
1359 map->def.max_entries = 1;
240bf8a5 1360 map->def.map_flags = strcmp(sec_name, STRUCT_OPS_LINK_SEC) == 0 ? BPF_F_LINK : 0;
590a0088
MKL
1361
1362 map->st_ops = calloc(1, sizeof(*map->st_ops));
1363 if (!map->st_ops)
1364 return -ENOMEM;
1365 st_ops = map->st_ops;
1366 st_ops->data = malloc(type->size);
1367 st_ops->progs = calloc(btf_vlen(type), sizeof(*st_ops->progs));
1368 st_ops->kern_func_off = malloc(btf_vlen(type) *
1369 sizeof(*st_ops->kern_func_off));
1370 if (!st_ops->data || !st_ops->progs || !st_ops->kern_func_off)
1371 return -ENOMEM;
1372
809a69d6 1373 if (vsi->offset + type->size > data->d_size) {
590a0088 1374 pr_warn("struct_ops init: var %s is beyond the end of DATASEC %s\n",
809a69d6 1375 var_name, sec_name);
590a0088
MKL
1376 return -EINVAL;
1377 }
1378
1379 memcpy(st_ops->data,
809a69d6 1380 data->d_buf + vsi->offset,
590a0088
MKL
1381 type->size);
1382 st_ops->tname = tname;
1383 st_ops->type = type;
1384 st_ops->type_id = type_id;
1385
1386 pr_debug("struct_ops init: struct %s(type_id=%u) %s found at offset %u\n",
1387 tname, type_id, var_name, vsi->offset);
1388 }
1389
1390 return 0;
1391}
1392
809a69d6
KFL
1393static int bpf_object_init_struct_ops(struct bpf_object *obj)
1394{
240bf8a5
EZ
1395 const char *sec_name;
1396 int sec_idx, err;
809a69d6 1397
240bf8a5
EZ
1398 for (sec_idx = 0; sec_idx < obj->efile.sec_cnt; ++sec_idx) {
1399 struct elf_sec_desc *desc = &obj->efile.secs[sec_idx];
1400
1401 if (desc->sec_type != SEC_ST_OPS)
1402 continue;
1403
1404 sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, sec_idx));
1405 if (!sec_name)
1406 return -LIBBPF_ERRNO__FORMAT;
1407
1408 err = init_struct_ops_maps(obj, sec_name, sec_idx, desc->data);
1409 if (err)
1410 return err;
1411 }
1412
1413 return 0;
809a69d6
KFL
1414}
1415
6c956392 1416static struct bpf_object *bpf_object__new(const char *path,
5e61f270 1417 const void *obj_buf,
2ce8450e
AN
1418 size_t obj_buf_sz,
1419 const char *obj_name)
1a5e3fb1
WN
1420{
1421 struct bpf_object *obj;
d859900c 1422 char *end;
1a5e3fb1
WN
1423
1424 obj = calloc(1, sizeof(struct bpf_object) + strlen(path) + 1);
1425 if (!obj) {
be18010e 1426 pr_warn("alloc memory failed for %s\n", path);
6371ca3b 1427 return ERR_PTR(-ENOMEM);
1a5e3fb1
WN
1428 }
1429
1430 strcpy(obj->path, path);
2ce8450e 1431 if (obj_name) {
9fc205b4 1432 libbpf_strlcpy(obj->name, obj_name, sizeof(obj->name));
2ce8450e
AN
1433 } else {
1434 /* Using basename() GNU version which doesn't modify arg. */
9fc205b4 1435 libbpf_strlcpy(obj->name, basename((void *)path), sizeof(obj->name));
2ce8450e
AN
1436 end = strchr(obj->name, '.');
1437 if (end)
1438 *end = 0;
1439 }
6c956392 1440
d859900c 1441 obj->efile.fd = -1;
6c956392 1442 /*
76e1022b 1443 * Caller of this function should also call
6c956392
WN
1444 * bpf_object__elf_finish() after data collection to return
1445 * obj_buf to user. If not, we should duplicate the buffer to
1446 * avoid user freeing them before elf finish.
1447 */
1448 obj->efile.obj_buf = obj_buf;
1449 obj->efile.obj_buf_sz = obj_buf_sz;
abd29c93 1450 obj->efile.btf_maps_shndx = -1;
81bfdd08 1451 obj->kconfig_map_idx = -1;
6c956392 1452
5e61f270 1453 obj->kern_version = get_kernel_version();
52d3352e 1454 obj->loaded = false;
9a208eff 1455
1a5e3fb1
WN
1456 return obj;
1457}
1458
1459static void bpf_object__elf_finish(struct bpf_object *obj)
1460{
29a30ff5 1461 if (!obj->efile.elf)
1a5e3fb1
WN
1462 return;
1463
b71a2ebf
GC
1464 elf_end(obj->efile.elf);
1465 obj->efile.elf = NULL;
bec7d68c 1466 obj->efile.symbols = NULL;
2e7ba4f8 1467 obj->efile.arena_data = NULL;
b62f06e8 1468
25bbbd7a
AN
1469 zfree(&obj->efile.secs);
1470 obj->efile.sec_cnt = 0;
1a5e3fb1 1471 zclose(obj->efile.fd);
6c956392
WN
1472 obj->efile.obj_buf = NULL;
1473 obj->efile.obj_buf_sz = 0;
1a5e3fb1
WN
1474}
1475
1476static int bpf_object__elf_init(struct bpf_object *obj)
1477{
ad23b723 1478 Elf64_Ehdr *ehdr;
1a5e3fb1 1479 int err = 0;
ad23b723 1480 Elf *elf;
1a5e3fb1 1481
29a30ff5 1482 if (obj->efile.elf) {
88a82120 1483 pr_warn("elf: init internal error\n");
6371ca3b 1484 return -LIBBPF_ERRNO__LIBELF;
1a5e3fb1
WN
1485 }
1486
6c956392 1487 if (obj->efile.obj_buf_sz > 0) {
146bf811 1488 /* obj_buf should have been validated by bpf_object__open_mem(). */
ad23b723 1489 elf = elf_memory((char *)obj->efile.obj_buf, obj->efile.obj_buf_sz);
6c956392 1490 } else {
92274e24 1491 obj->efile.fd = open(obj->path, O_RDONLY | O_CLOEXEC);
6c956392 1492 if (obj->efile.fd < 0) {
be5c5d4e 1493 char errmsg[STRERR_BUFSIZE], *cp;
1ce6a9fc 1494
be5c5d4e
AN
1495 err = -errno;
1496 cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg));
88a82120 1497 pr_warn("elf: failed to open %s: %s\n", obj->path, cp);
be5c5d4e 1498 return err;
6c956392
WN
1499 }
1500
ad23b723 1501 elf = elf_begin(obj->efile.fd, ELF_C_READ_MMAP, NULL);
1a5e3fb1
WN
1502 }
1503
ad23b723 1504 if (!elf) {
88a82120 1505 pr_warn("elf: failed to open %s as ELF file: %s\n", obj->path, elf_errmsg(-1));
6371ca3b 1506 err = -LIBBPF_ERRNO__LIBELF;
1a5e3fb1
WN
1507 goto errout;
1508 }
1509
ad23b723
AN
1510 obj->efile.elf = elf;
1511
1512 if (elf_kind(elf) != ELF_K_ELF) {
1513 err = -LIBBPF_ERRNO__FORMAT;
1514 pr_warn("elf: '%s' is not a proper ELF object\n", obj->path);
1515 goto errout;
1516 }
1517
1518 if (gelf_getclass(elf) != ELFCLASS64) {
1519 err = -LIBBPF_ERRNO__FORMAT;
1520 pr_warn("elf: '%s' is not a 64-bit ELF object\n", obj->path);
1521 goto errout;
1522 }
1523
1524 obj->efile.ehdr = ehdr = elf64_getehdr(elf);
1525 if (!obj->efile.ehdr) {
88a82120 1526 pr_warn("elf: failed to get ELF header from %s: %s\n", obj->path, elf_errmsg(-1));
6371ca3b 1527 err = -LIBBPF_ERRNO__FORMAT;
1a5e3fb1
WN
1528 goto errout;
1529 }
1a5e3fb1 1530
ad23b723 1531 if (elf_getshdrstrndx(elf, &obj->efile.shstrndx)) {
88a82120
AN
1532 pr_warn("elf: failed to get section names section index for %s: %s\n",
1533 obj->path, elf_errmsg(-1));
1534 err = -LIBBPF_ERRNO__FORMAT;
1535 goto errout;
1536 }
1537
70e79866 1538 /* ELF is corrupted/truncated, avoid calling elf_strptr. */
ad23b723 1539 if (!elf_rawdata(elf_getscn(elf, obj->efile.shstrndx), NULL)) {
88a82120
AN
1540 pr_warn("elf: failed to get section names strings from %s: %s\n",
1541 obj->path, elf_errmsg(-1));
8f3f5792
NK
1542 err = -LIBBPF_ERRNO__FORMAT;
1543 goto errout;
88a82120
AN
1544 }
1545
9b16137a 1546 /* Old LLVM set e_machine to EM_NONE */
ad23b723 1547 if (ehdr->e_type != ET_REL || (ehdr->e_machine && ehdr->e_machine != EM_BPF)) {
88a82120 1548 pr_warn("elf: %s is not a valid eBPF object file\n", obj->path);
6371ca3b 1549 err = -LIBBPF_ERRNO__FORMAT;
1a5e3fb1
WN
1550 goto errout;
1551 }
1552
1553 return 0;
1554errout:
1555 bpf_object__elf_finish(obj);
1556 return err;
1557}
1558
12ef5634 1559static int bpf_object__check_endianness(struct bpf_object *obj)
cc4228d5 1560{
3930198d 1561#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
ad23b723 1562 if (obj->efile.ehdr->e_ident[EI_DATA] == ELFDATA2LSB)
12ef5634 1563 return 0;
3930198d 1564#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
ad23b723 1565 if (obj->efile.ehdr->e_ident[EI_DATA] == ELFDATA2MSB)
12ef5634
AN
1566 return 0;
1567#else
1568# error "Unrecognized __BYTE_ORDER__"
1569#endif
88a82120 1570 pr_warn("elf: endianness mismatch in %s.\n", obj->path);
6371ca3b 1571 return -LIBBPF_ERRNO__ENDIAN;
cc4228d5
WN
1572}
1573
cb1e5e96 1574static int
399dc65e 1575bpf_object__init_license(struct bpf_object *obj, void *data, size_t size)
cb1e5e96 1576{
35a85550
SHY
1577 if (!data) {
1578 pr_warn("invalid license section in %s\n", obj->path);
1579 return -LIBBPF_ERRNO__FORMAT;
1580 }
f9798239
AN
1581 /* libbpf_strlcpy() only copies first N - 1 bytes, so size + 1 won't
1582 * go over allowed ELF data section buffer
1583 */
1584 libbpf_strlcpy(obj->license, data, min(size + 1, sizeof(obj->license)));
cb1e5e96
WN
1585 pr_debug("license of %s is %s\n", obj->path, obj->license);
1586 return 0;
1587}
1588
54b8625c
JF
1589static int
1590bpf_object__init_kversion(struct bpf_object *obj, void *data, size_t size)
1591{
1592 __u32 kver;
1593
35a85550 1594 if (!data || size != sizeof(kver)) {
be18010e 1595 pr_warn("invalid kver section in %s\n", obj->path);
54b8625c
JF
1596 return -LIBBPF_ERRNO__FORMAT;
1597 }
1598 memcpy(&kver, data, sizeof(kver));
1599 obj->kern_version = kver;
1600 pr_debug("kernel version of %s is %x\n", obj->path, obj->kern_version);
1601 return 0;
1602}
1603
addb9fc9
NS
1604static bool bpf_map_type__is_map_in_map(enum bpf_map_type type)
1605{
1606 if (type == BPF_MAP_TYPE_ARRAY_OF_MAPS ||
1607 type == BPF_MAP_TYPE_HASH_OF_MAPS)
1608 return true;
1609 return false;
1610}
1611
b96c07f3 1612static int find_elf_sec_sz(const struct bpf_object *obj, const char *name, __u32 *size)
1713d68b 1613{
25bbbd7a
AN
1614 Elf_Data *data;
1615 Elf_Scn *scn;
1713d68b 1616
25bbbd7a 1617 if (!name)
1713d68b 1618 return -EINVAL;
88a82120 1619
25bbbd7a
AN
1620 scn = elf_sec_by_name(obj, name);
1621 data = elf_sec_data(obj, scn);
1622 if (data) {
25bbbd7a 1623 *size = data->d_size;
08894d9c 1624 return 0; /* found it */
1713d68b
DB
1625 }
1626
08894d9c 1627 return -ENOENT;
1713d68b
DB
1628}
1629
f33f742d 1630static Elf64_Sym *find_elf_var_sym(const struct bpf_object *obj, const char *name)
1713d68b
DB
1631{
1632 Elf_Data *symbols = obj->efile.symbols;
1633 const char *sname;
1634 size_t si;
1635
ad23b723
AN
1636 for (si = 0; si < symbols->d_size / sizeof(Elf64_Sym); si++) {
1637 Elf64_Sym *sym = elf_sym_by_idx(obj, si);
1713d68b 1638
3a06ec0a
AN
1639 if (ELF64_ST_TYPE(sym->st_info) != STT_OBJECT)
1640 continue;
1641
1642 if (ELF64_ST_BIND(sym->st_info) != STB_GLOBAL &&
1643 ELF64_ST_BIND(sym->st_info) != STB_WEAK)
1713d68b
DB
1644 continue;
1645
ad23b723 1646 sname = elf_sym_str(obj, sym->st_name);
1713d68b 1647 if (!sname) {
ad23b723 1648 pr_warn("failed to get sym name string for var %s\n", name);
f33f742d 1649 return ERR_PTR(-EIO);
1713d68b 1650 }
f33f742d
AN
1651 if (strcmp(name, sname) == 0)
1652 return sym;
1713d68b
DB
1653 }
1654
f33f742d 1655 return ERR_PTR(-ENOENT);
1713d68b
DB
1656}
1657
9fa5e1a1
AN
1658/* Some versions of Android don't provide memfd_create() in their libc
1659 * implementation, so avoid complications and just go straight to Linux
1660 * syscall.
1661 */
1662static int sys_memfd_create(const char *name, unsigned flags)
1663{
1664 return syscall(__NR_memfd_create, name, flags);
1665}
1666
dac645b9
AN
1667static int create_placeholder_fd(void)
1668{
1669 int fd;
1670
9fa5e1a1 1671 fd = ensure_good_fd(sys_memfd_create("libbpf-placeholder-fd", MFD_CLOEXEC));
dac645b9
AN
1672 if (fd < 0)
1673 return -errno;
1674 return fd;
1675}
1676
bf829271 1677static struct bpf_map *bpf_object__add_map(struct bpf_object *obj)
d859900c 1678{
69721203
AN
1679 struct bpf_map *map;
1680 int err;
bf829271 1681
69721203
AN
1682 err = libbpf_ensure_mem((void **)&obj->maps, &obj->maps_cap,
1683 sizeof(*obj->maps), obj->nr_maps + 1);
1684 if (err)
1685 return ERR_PTR(err);
bf829271 1686
69721203 1687 map = &obj->maps[obj->nr_maps++];
ec41817b 1688 map->obj = obj;
dac645b9
AN
1689 /* Preallocate map FD without actually creating BPF map just yet.
1690 * These map FD "placeholders" will be reused later without changing
1691 * FD value when map is actually created in the kernel.
1692 *
1693 * This is useful to be able to perform BPF program relocations
1694 * without having to create BPF maps before that step. This allows us
1695 * to finalize and load BTF very late in BPF object's loading phase,
1696 * right before BPF maps have to be created and BPF programs have to
1697 * be loaded. By having these map FD placeholders we can perform all
1698 * the sanitizations, relocations, and any other adjustments before we
1699 * start creating actual BPF kernel objects (BTF, maps, progs).
1700 */
1701 map->fd = create_placeholder_fd();
1702 if (map->fd < 0)
1703 return ERR_PTR(map->fd);
69721203 1704 map->inner_map_fd = -1;
ec41817b 1705 map->autocreate = true;
bf829271 1706
69721203 1707 return map;
d859900c
DB
1708}
1709
79ff13e9 1710static size_t array_map_mmap_sz(unsigned int value_sz, unsigned int max_entries)
eba9c5f4 1711{
9d0a2331 1712 const long page_sz = sysconf(_SC_PAGE_SIZE);
eba9c5f4
AN
1713 size_t map_sz;
1714
9d0a2331 1715 map_sz = (size_t)roundup(value_sz, 8) * max_entries;
eba9c5f4
AN
1716 map_sz = roundup(map_sz, page_sz);
1717 return map_sz;
1718}
1719
79ff13e9
AS
1720static size_t bpf_map_mmap_sz(const struct bpf_map *map)
1721{
1722 const long page_sz = sysconf(_SC_PAGE_SIZE);
1723
1724 switch (map->def.type) {
1725 case BPF_MAP_TYPE_ARRAY:
1726 return array_map_mmap_sz(map->def.value_size, map->def.max_entries);
1727 case BPF_MAP_TYPE_ARENA:
1728 return page_sz * map->def.max_entries;
1729 default:
1730 return 0; /* not supported */
1731 }
1732}
1733
9d0a2331
JK
1734static int bpf_map_mmap_resize(struct bpf_map *map, size_t old_sz, size_t new_sz)
1735{
1736 void *mmaped;
1737
1738 if (!map->mmaped)
1739 return -EINVAL;
1740
1741 if (old_sz == new_sz)
1742 return 0;
1743
1744 mmaped = mmap(NULL, new_sz, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
1745 if (mmaped == MAP_FAILED)
1746 return -errno;
1747
1748 memcpy(mmaped, map->mmaped, min(old_sz, new_sz));
1749 munmap(map->mmaped, old_sz);
1750 map->mmaped = mmaped;
1751 return 0;
1752}
1753
aed65917 1754static char *internal_map_name(struct bpf_object *obj, const char *real_name)
81bfdd08 1755{
113e6b7e 1756 char map_name[BPF_OBJ_NAME_LEN], *p;
aed65917
AN
1757 int pfx_len, sfx_len = max((size_t)7, strlen(real_name));
1758
1759 /* This is one of the more confusing parts of libbpf for various
1760 * reasons, some of which are historical. The original idea for naming
1761 * internal names was to include as much of BPF object name prefix as
1762 * possible, so that it can be distinguished from similar internal
1763 * maps of a different BPF object.
1764 * As an example, let's say we have bpf_object named 'my_object_name'
1765 * and internal map corresponding to '.rodata' ELF section. The final
1766 * map name advertised to user and to the kernel will be
1767 * 'my_objec.rodata', taking first 8 characters of object name and
1768 * entire 7 characters of '.rodata'.
1769 * Somewhat confusingly, if internal map ELF section name is shorter
1770 * than 7 characters, e.g., '.bss', we still reserve 7 characters
1771 * for the suffix, even though we only have 4 actual characters, and
1772 * resulting map will be called 'my_objec.bss', not even using all 15
1773 * characters allowed by the kernel. Oh well, at least the truncated
1774 * object name is somewhat consistent in this case. But if the map
1775 * name is '.kconfig', we'll still have entirety of '.kconfig' added
1776 * (8 chars) and thus will be left with only first 7 characters of the
1777 * object name ('my_obje'). Happy guessing, user, that the final map
1778 * name will be "my_obje.kconfig".
1779 * Now, with libbpf starting to support arbitrarily named .rodata.*
1780 * and .data.* data sections, it's possible that ELF section name is
1781 * longer than allowed 15 chars, so we now need to be careful to take
1782 * only up to 15 first characters of ELF name, taking no BPF object
1783 * name characters at all. So '.rodata.abracadabra' will result in
1784 * '.rodata.abracad' kernel and user-visible name.
1785 * We need to keep this convoluted logic intact for .data, .bss and
1786 * .rodata maps, but for new custom .data.custom and .rodata.custom
1787 * maps we use their ELF names as is, not prepending bpf_object name
1788 * in front. We still need to truncate them to 15 characters for the
1789 * kernel. Full name can be recovered for such maps by using DATASEC
1790 * BTF type associated with such map's value type, though.
1791 */
1792 if (sfx_len >= BPF_OBJ_NAME_LEN)
1793 sfx_len = BPF_OBJ_NAME_LEN - 1;
1794
1795 /* if there are two or more dots in map name, it's a custom dot map */
1796 if (strchr(real_name + 1, '.') != NULL)
1797 pfx_len = 0;
1798 else
1799 pfx_len = min((size_t)BPF_OBJ_NAME_LEN - sfx_len - 1, strlen(obj->name));
81bfdd08
AN
1800
1801 snprintf(map_name, sizeof(map_name), "%.*s%.*s", pfx_len, obj->name,
aed65917 1802 sfx_len, real_name);
81bfdd08 1803
113e6b7e
THJ
1804 /* sanitise map name to characters allowed by kernel */
1805 for (p = map_name; *p && p < map_name + sizeof(map_name); p++)
1806 if (!isalnum(*p) && *p != '_' && *p != '.')
1807 *p = '_';
1808
81bfdd08
AN
1809 return strdup(map_name);
1810}
1811
262cfb74 1812static int
4fcac46c
AN
1813map_fill_btf_type_info(struct bpf_object *obj, struct bpf_map *map);
1814
1815/* Internal BPF map is mmap()'able only if at least one of corresponding
1816 * DATASEC's VARs are to be exposed through BPF skeleton. I.e., it's a GLOBAL
1817 * variable and it's not marked as __hidden (which turns it into, effectively,
1818 * a STATIC variable).
1819 */
1820static bool map_is_mmapable(struct bpf_object *obj, struct bpf_map *map)
1821{
1822 const struct btf_type *t, *vt;
1823 struct btf_var_secinfo *vsi;
1824 int i, n;
1825
1826 if (!map->btf_value_type_id)
1827 return false;
1828
1829 t = btf__type_by_id(obj->btf, map->btf_value_type_id);
1830 if (!btf_is_datasec(t))
1831 return false;
1832
1833 vsi = btf_var_secinfos(t);
1834 for (i = 0, n = btf_vlen(t); i < n; i++, vsi++) {
1835 vt = btf__type_by_id(obj->btf, vsi->type);
1836 if (!btf_is_var(vt))
1837 continue;
1838
1839 if (btf_var(vt)->linkage != BTF_VAR_STATIC)
1840 return true;
1841 }
1842
1843 return false;
1844}
262cfb74 1845
d859900c 1846static int
bf829271 1847bpf_object__init_internal_map(struct bpf_object *obj, enum libbpf_map_type type,
aed65917 1848 const char *real_name, int sec_idx, void *data, size_t data_sz)
d859900c 1849{
bf829271
AN
1850 struct bpf_map_def *def;
1851 struct bpf_map *map;
9d0a2331 1852 size_t mmap_sz;
eba9c5f4 1853 int err;
bf829271
AN
1854
1855 map = bpf_object__add_map(obj);
1856 if (IS_ERR(map))
1857 return PTR_ERR(map);
d859900c
DB
1858
1859 map->libbpf_type = type;
db48814b
AN
1860 map->sec_idx = sec_idx;
1861 map->sec_offset = 0;
aed65917
AN
1862 map->real_name = strdup(real_name);
1863 map->name = internal_map_name(obj, real_name);
1864 if (!map->real_name || !map->name) {
1865 zfree(&map->real_name);
1866 zfree(&map->name);
d859900c
DB
1867 return -ENOMEM;
1868 }
1869
bf829271 1870 def = &map->def;
d859900c
DB
1871 def->type = BPF_MAP_TYPE_ARRAY;
1872 def->key_size = sizeof(int);
eba9c5f4 1873 def->value_size = data_sz;
d859900c 1874 def->max_entries = 1;
81bfdd08 1875 def->map_flags = type == LIBBPF_MAP_RODATA || type == LIBBPF_MAP_KCONFIG
2e7ba4f8 1876 ? BPF_F_RDONLY_PROG : 0;
4fcac46c
AN
1877
1878 /* failures are fine because of maps like .rodata.str1.1 */
1879 (void) map_fill_btf_type_info(obj, map);
1880
1881 if (map_is_mmapable(obj, map))
1882 def->map_flags |= BPF_F_MMAPABLE;
7fe74b43
AN
1883
1884 pr_debug("map '%s' (global data): at sec_idx %d, offset %zu, flags %x.\n",
81bfdd08 1885 map->name, map->sec_idx, map->sec_offset, def->map_flags);
7fe74b43 1886
79ff13e9 1887 mmap_sz = bpf_map_mmap_sz(map);
9d0a2331 1888 map->mmaped = mmap(NULL, mmap_sz, PROT_READ | PROT_WRITE,
eba9c5f4
AN
1889 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
1890 if (map->mmaped == MAP_FAILED) {
1891 err = -errno;
1892 map->mmaped = NULL;
1893 pr_warn("failed to alloc map '%s' content buffer: %d\n",
1894 map->name, err);
aed65917 1895 zfree(&map->real_name);
eba9c5f4
AN
1896 zfree(&map->name);
1897 return err;
d859900c
DB
1898 }
1899
166750bc 1900 if (data)
eba9c5f4
AN
1901 memcpy(map->mmaped, data, data_sz);
1902
e1d1dc46 1903 pr_debug("map %td is \"%s\"\n", map - obj->maps, map->name);
d859900c
DB
1904 return 0;
1905}
1906
bf829271
AN
1907static int bpf_object__init_global_data_maps(struct bpf_object *obj)
1908{
25bbbd7a 1909 struct elf_sec_desc *sec_desc;
aed65917 1910 const char *sec_name;
25bbbd7a 1911 int err = 0, sec_idx;
bf829271 1912
bf829271
AN
1913 /*
1914 * Populate obj->maps with libbpf internal maps.
1915 */
25bbbd7a
AN
1916 for (sec_idx = 1; sec_idx < obj->efile.sec_cnt; sec_idx++) {
1917 sec_desc = &obj->efile.secs[sec_idx];
1918
47ea7417 1919 /* Skip recognized sections with size 0. */
d4e6d684 1920 if (!sec_desc->data || sec_desc->data->d_size == 0)
47ea7417
JH
1921 continue;
1922
25bbbd7a
AN
1923 switch (sec_desc->sec_type) {
1924 case SEC_DATA:
aed65917 1925 sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, sec_idx));
25bbbd7a 1926 err = bpf_object__init_internal_map(obj, LIBBPF_MAP_DATA,
aed65917 1927 sec_name, sec_idx,
25bbbd7a
AN
1928 sec_desc->data->d_buf,
1929 sec_desc->data->d_size);
1930 break;
1931 case SEC_RODATA:
1932 obj->has_rodata = true;
aed65917 1933 sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, sec_idx));
25bbbd7a 1934 err = bpf_object__init_internal_map(obj, LIBBPF_MAP_RODATA,
aed65917 1935 sec_name, sec_idx,
25bbbd7a
AN
1936 sec_desc->data->d_buf,
1937 sec_desc->data->d_size);
1938 break;
1939 case SEC_BSS:
aed65917 1940 sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, sec_idx));
25bbbd7a 1941 err = bpf_object__init_internal_map(obj, LIBBPF_MAP_BSS,
aed65917 1942 sec_name, sec_idx,
25bbbd7a
AN
1943 NULL,
1944 sec_desc->data->d_size);
1945 break;
1946 default:
1947 /* skip */
1948 break;
1949 }
bf829271
AN
1950 if (err)
1951 return err;
1952 }
1953 return 0;
1954}
1955
166750bc
AN
1956
1957static struct extern_desc *find_extern_by_name(const struct bpf_object *obj,
1958 const void *name)
1959{
1960 int i;
1961
1962 for (i = 0; i < obj->nr_extern; i++) {
1963 if (strcmp(obj->externs[i].name, name) == 0)
1964 return &obj->externs[i];
1965 }
1966 return NULL;
1967}
1968
2e33efe3
AN
1969static int set_kcfg_value_tri(struct extern_desc *ext, void *ext_val,
1970 char value)
166750bc 1971{
2e33efe3
AN
1972 switch (ext->kcfg.type) {
1973 case KCFG_BOOL:
166750bc 1974 if (value == 'm') {
55d00c37 1975 pr_warn("extern (kcfg) '%s': value '%c' implies tristate or char type\n",
166750bc
AN
1976 ext->name, value);
1977 return -EINVAL;
1978 }
1979 *(bool *)ext_val = value == 'y' ? true : false;
1980 break;
2e33efe3 1981 case KCFG_TRISTATE:
166750bc
AN
1982 if (value == 'y')
1983 *(enum libbpf_tristate *)ext_val = TRI_YES;
1984 else if (value == 'm')
1985 *(enum libbpf_tristate *)ext_val = TRI_MODULE;
1986 else /* value == 'n' */
1987 *(enum libbpf_tristate *)ext_val = TRI_NO;
1988 break;
2e33efe3 1989 case KCFG_CHAR:
166750bc
AN
1990 *(char *)ext_val = value;
1991 break;
2e33efe3
AN
1992 case KCFG_UNKNOWN:
1993 case KCFG_INT:
1994 case KCFG_CHAR_ARR:
166750bc 1995 default:
55d00c37 1996 pr_warn("extern (kcfg) '%s': value '%c' implies bool, tristate, or char type\n",
166750bc
AN
1997 ext->name, value);
1998 return -EINVAL;
1999 }
2000 ext->is_set = true;
2001 return 0;
2002}
2003
2e33efe3
AN
2004static int set_kcfg_value_str(struct extern_desc *ext, char *ext_val,
2005 const char *value)
166750bc
AN
2006{
2007 size_t len;
2008
2e33efe3 2009 if (ext->kcfg.type != KCFG_CHAR_ARR) {
55d00c37
AN
2010 pr_warn("extern (kcfg) '%s': value '%s' implies char array type\n",
2011 ext->name, value);
166750bc
AN
2012 return -EINVAL;
2013 }
2014
2015 len = strlen(value);
2016 if (value[len - 1] != '"') {
2e33efe3 2017 pr_warn("extern (kcfg) '%s': invalid string config '%s'\n",
166750bc
AN
2018 ext->name, value);
2019 return -EINVAL;
2020 }
2021
2022 /* strip quotes */
2023 len -= 2;
2e33efe3 2024 if (len >= ext->kcfg.sz) {
55d00c37 2025 pr_warn("extern (kcfg) '%s': long string '%s' of (%zu bytes) truncated to %d bytes\n",
2e33efe3
AN
2026 ext->name, value, len, ext->kcfg.sz - 1);
2027 len = ext->kcfg.sz - 1;
166750bc
AN
2028 }
2029 memcpy(ext_val, value + 1, len);
2030 ext_val[len] = '\0';
2031 ext->is_set = true;
2032 return 0;
2033}
2034
2035static int parse_u64(const char *value, __u64 *res)
2036{
2037 char *value_end;
2038 int err;
2039
2040 errno = 0;
2041 *res = strtoull(value, &value_end, 0);
2042 if (errno) {
2043 err = -errno;
2044 pr_warn("failed to parse '%s' as integer: %d\n", value, err);
2045 return err;
2046 }
2047 if (*value_end) {
2048 pr_warn("failed to parse '%s' as integer completely\n", value);
2049 return -EINVAL;
2050 }
2051 return 0;
2052}
2053
2e33efe3 2054static bool is_kcfg_value_in_range(const struct extern_desc *ext, __u64 v)
166750bc 2055{
2e33efe3 2056 int bit_sz = ext->kcfg.sz * 8;
166750bc 2057
2e33efe3 2058 if (ext->kcfg.sz == 8)
166750bc
AN
2059 return true;
2060
2061 /* Validate that value stored in u64 fits in integer of `ext->sz`
2062 * bytes size without any loss of information. If the target integer
2063 * is signed, we rely on the following limits of integer type of
2064 * Y bits and subsequent transformation:
2065 *
2066 * -2^(Y-1) <= X <= 2^(Y-1) - 1
2067 * 0 <= X + 2^(Y-1) <= 2^Y - 1
2068 * 0 <= X + 2^(Y-1) < 2^Y
2069 *
2070 * For unsigned target integer, check that all the (64 - Y) bits are
2071 * zero.
2072 */
2e33efe3 2073 if (ext->kcfg.is_signed)
166750bc
AN
2074 return v + (1ULL << (bit_sz - 1)) < (1ULL << bit_sz);
2075 else
2076 return (v >> bit_sz) == 0;
2077}
2078
2e33efe3
AN
2079static int set_kcfg_value_num(struct extern_desc *ext, void *ext_val,
2080 __u64 value)
166750bc 2081{
55d00c37
AN
2082 if (ext->kcfg.type != KCFG_INT && ext->kcfg.type != KCFG_CHAR &&
2083 ext->kcfg.type != KCFG_BOOL) {
2084 pr_warn("extern (kcfg) '%s': value '%llu' implies integer, char, or boolean type\n",
7745ff98 2085 ext->name, (unsigned long long)value);
166750bc
AN
2086 return -EINVAL;
2087 }
55d00c37
AN
2088 if (ext->kcfg.type == KCFG_BOOL && value > 1) {
2089 pr_warn("extern (kcfg) '%s': value '%llu' isn't boolean compatible\n",
2090 ext->name, (unsigned long long)value);
2091 return -EINVAL;
2092
2093 }
2e33efe3 2094 if (!is_kcfg_value_in_range(ext, value)) {
55d00c37 2095 pr_warn("extern (kcfg) '%s': value '%llu' doesn't fit in %d bytes\n",
2e33efe3 2096 ext->name, (unsigned long long)value, ext->kcfg.sz);
166750bc
AN
2097 return -ERANGE;
2098 }
2e33efe3 2099 switch (ext->kcfg.sz) {
e3ba8e4e
KM
2100 case 1:
2101 *(__u8 *)ext_val = value;
2102 break;
2103 case 2:
2104 *(__u16 *)ext_val = value;
2105 break;
2106 case 4:
2107 *(__u32 *)ext_val = value;
2108 break;
2109 case 8:
2110 *(__u64 *)ext_val = value;
2111 break;
2112 default:
2113 return -EINVAL;
166750bc
AN
2114 }
2115 ext->is_set = true;
2116 return 0;
2117}
2118
8601fd42
AN
2119static int bpf_object__process_kconfig_line(struct bpf_object *obj,
2120 char *buf, void *data)
166750bc 2121{
166750bc 2122 struct extern_desc *ext;
8601fd42 2123 char *sep, *value;
166750bc
AN
2124 int len, err = 0;
2125 void *ext_val;
2126 __u64 num;
166750bc 2127
13d35a0c 2128 if (!str_has_pfx(buf, "CONFIG_"))
8601fd42 2129 return 0;
166750bc 2130
8601fd42
AN
2131 sep = strchr(buf, '=');
2132 if (!sep) {
2133 pr_warn("failed to parse '%s': no separator\n", buf);
2134 return -EINVAL;
2135 }
2136
2137 /* Trim ending '\n' */
2138 len = strlen(buf);
2139 if (buf[len - 1] == '\n')
2140 buf[len - 1] = '\0';
2141 /* Split on '=' and ensure that a value is present. */
2142 *sep = '\0';
2143 if (!sep[1]) {
2144 *sep = '=';
2145 pr_warn("failed to parse '%s': no value\n", buf);
2146 return -EINVAL;
2147 }
2148
2149 ext = find_extern_by_name(obj, buf);
2150 if (!ext || ext->is_set)
2151 return 0;
2152
2e33efe3 2153 ext_val = data + ext->kcfg.data_off;
8601fd42
AN
2154 value = sep + 1;
2155
2156 switch (*value) {
2157 case 'y': case 'n': case 'm':
2e33efe3 2158 err = set_kcfg_value_tri(ext, ext_val, *value);
8601fd42
AN
2159 break;
2160 case '"':
2e33efe3 2161 err = set_kcfg_value_str(ext, ext_val, value);
8601fd42
AN
2162 break;
2163 default:
2164 /* assume integer */
2165 err = parse_u64(value, &num);
2166 if (err) {
55d00c37 2167 pr_warn("extern (kcfg) '%s': value '%s' isn't a valid integer\n", ext->name, value);
8601fd42
AN
2168 return err;
2169 }
55d00c37
AN
2170 if (ext->kcfg.type != KCFG_INT && ext->kcfg.type != KCFG_CHAR) {
2171 pr_warn("extern (kcfg) '%s': value '%s' implies integer type\n", ext->name, value);
2172 return -EINVAL;
2173 }
2e33efe3 2174 err = set_kcfg_value_num(ext, ext_val, num);
8601fd42 2175 break;
166750bc 2176 }
8601fd42
AN
2177 if (err)
2178 return err;
55d00c37 2179 pr_debug("extern (kcfg) '%s': set to %s\n", ext->name, value);
8601fd42
AN
2180 return 0;
2181}
2182
2183static int bpf_object__read_kconfig_file(struct bpf_object *obj, void *data)
2184{
2185 char buf[PATH_MAX];
2186 struct utsname uts;
2187 int len, err = 0;
2188 gzFile file;
2189
2190 uname(&uts);
2191 len = snprintf(buf, PATH_MAX, "/boot/config-%s", uts.release);
2192 if (len < 0)
2193 return -EINVAL;
2194 else if (len >= PATH_MAX)
2195 return -ENAMETOOLONG;
2196
2197 /* gzopen also accepts uncompressed files. */
8e50750f 2198 file = gzopen(buf, "re");
8601fd42 2199 if (!file)
8e50750f 2200 file = gzopen("/proc/config.gz", "re");
8601fd42 2201
166750bc 2202 if (!file) {
8601fd42 2203 pr_warn("failed to open system Kconfig\n");
166750bc
AN
2204 return -ENOENT;
2205 }
2206
2207 while (gzgets(file, buf, sizeof(buf))) {
8601fd42
AN
2208 err = bpf_object__process_kconfig_line(obj, buf, data);
2209 if (err) {
2210 pr_warn("error parsing system Kconfig line '%s': %d\n",
2211 buf, err);
166750bc
AN
2212 goto out;
2213 }
8601fd42 2214 }
166750bc 2215
8601fd42
AN
2216out:
2217 gzclose(file);
2218 return err;
2219}
166750bc 2220
8601fd42
AN
2221static int bpf_object__read_kconfig_mem(struct bpf_object *obj,
2222 const char *config, void *data)
2223{
2224 char buf[PATH_MAX];
2225 int err = 0;
2226 FILE *file;
166750bc 2227
8601fd42
AN
2228 file = fmemopen((void *)config, strlen(config), "r");
2229 if (!file) {
2230 err = -errno;
2231 pr_warn("failed to open in-memory Kconfig: %d\n", err);
2232 return err;
2233 }
2234
2235 while (fgets(buf, sizeof(buf), file)) {
2236 err = bpf_object__process_kconfig_line(obj, buf, data);
2237 if (err) {
2238 pr_warn("error parsing in-memory Kconfig line '%s': %d\n",
2239 buf, err);
166750bc
AN
2240 break;
2241 }
166750bc
AN
2242 }
2243
8601fd42 2244 fclose(file);
166750bc
AN
2245 return err;
2246}
2247
81bfdd08 2248static int bpf_object__init_kconfig_map(struct bpf_object *obj)
166750bc 2249{
2e33efe3 2250 struct extern_desc *last_ext = NULL, *ext;
166750bc 2251 size_t map_sz;
2e33efe3 2252 int i, err;
166750bc 2253
2e33efe3
AN
2254 for (i = 0; i < obj->nr_extern; i++) {
2255 ext = &obj->externs[i];
2256 if (ext->type == EXT_KCFG)
2257 last_ext = ext;
2258 }
166750bc 2259
2e33efe3
AN
2260 if (!last_ext)
2261 return 0;
166750bc 2262
2e33efe3 2263 map_sz = last_ext->kcfg.data_off + last_ext->kcfg.sz;
81bfdd08 2264 err = bpf_object__init_internal_map(obj, LIBBPF_MAP_KCONFIG,
aed65917 2265 ".kconfig", obj->efile.symbols_shndx,
166750bc
AN
2266 NULL, map_sz);
2267 if (err)
2268 return err;
2269
81bfdd08 2270 obj->kconfig_map_idx = obj->nr_maps - 1;
166750bc
AN
2271
2272 return 0;
2273}
2274
42869d28 2275const struct btf_type *
ddc7c304 2276skip_mods_and_typedefs(const struct btf *btf, __u32 id, __u32 *res_id)
abd29c93
AN
2277{
2278 const struct btf_type *t = btf__type_by_id(btf, id);
8837fe5d 2279
ddc7c304
AN
2280 if (res_id)
2281 *res_id = id;
2282
2283 while (btf_is_mod(t) || btf_is_typedef(t)) {
2284 if (res_id)
2285 *res_id = t->type;
2286 t = btf__type_by_id(btf, t->type);
abd29c93 2287 }
ddc7c304
AN
2288
2289 return t;
abd29c93
AN
2290}
2291
590a0088
MKL
2292static const struct btf_type *
2293resolve_func_ptr(const struct btf *btf, __u32 id, __u32 *res_id)
2294{
2295 const struct btf_type *t;
2296
2297 t = skip_mods_and_typedefs(btf, id, NULL);
2298 if (!btf_is_ptr(t))
2299 return NULL;
2300
2301 t = skip_mods_and_typedefs(btf, t->type, res_id);
2302
2303 return btf_is_func_proto(t) ? t : NULL;
2304}
2305
774e132e 2306static const char *__btf_kind_str(__u16 kind)
81ba0889 2307{
774e132e 2308 switch (kind) {
81ba0889
AN
2309 case BTF_KIND_UNKN: return "void";
2310 case BTF_KIND_INT: return "int";
2311 case BTF_KIND_PTR: return "ptr";
2312 case BTF_KIND_ARRAY: return "array";
2313 case BTF_KIND_STRUCT: return "struct";
2314 case BTF_KIND_UNION: return "union";
2315 case BTF_KIND_ENUM: return "enum";
2316 case BTF_KIND_FWD: return "fwd";
2317 case BTF_KIND_TYPEDEF: return "typedef";
2318 case BTF_KIND_VOLATILE: return "volatile";
2319 case BTF_KIND_CONST: return "const";
2320 case BTF_KIND_RESTRICT: return "restrict";
2321 case BTF_KIND_FUNC: return "func";
2322 case BTF_KIND_FUNC_PROTO: return "func_proto";
2323 case BTF_KIND_VAR: return "var";
2324 case BTF_KIND_DATASEC: return "datasec";
22541a9e 2325 case BTF_KIND_FLOAT: return "float";
223f903e 2326 case BTF_KIND_DECL_TAG: return "decl_tag";
2dc1e488 2327 case BTF_KIND_TYPE_TAG: return "type_tag";
f2a62588 2328 case BTF_KIND_ENUM64: return "enum64";
81ba0889
AN
2329 default: return "unknown";
2330 }
2331}
2332
42869d28 2333const char *btf_kind_str(const struct btf_type *t)
774e132e
MKL
2334{
2335 return __btf_kind_str(btf_kind(t));
2336}
2337
ef99b02b
AN
2338/*
2339 * Fetch integer attribute of BTF map definition. Such attributes are
2340 * represented using a pointer to an array, in which dimensionality of array
2341 * encodes specified integer value. E.g., int (*type)[BPF_MAP_TYPE_ARRAY];
2342 * encodes `type => BPF_MAP_TYPE_ARRAY` key/value pair completely using BTF
2343 * type definition, while using only sizeof(void *) space in ELF data section.
2344 */
2345static bool get_map_field_int(const char *map_name, const struct btf *btf,
8983b731
AN
2346 const struct btf_member *m, __u32 *res)
2347{
ddc7c304 2348 const struct btf_type *t = skip_mods_and_typedefs(btf, m->type, NULL);
abd29c93 2349 const char *name = btf__name_by_offset(btf, m->name_off);
ef99b02b
AN
2350 const struct btf_array *arr_info;
2351 const struct btf_type *arr_t;
abd29c93 2352
b03bc685 2353 if (!btf_is_ptr(t)) {
81ba0889
AN
2354 pr_warn("map '%s': attr '%s': expected PTR, got %s.\n",
2355 map_name, name, btf_kind_str(t));
abd29c93
AN
2356 return false;
2357 }
ef99b02b
AN
2358
2359 arr_t = btf__type_by_id(btf, t->type);
2360 if (!arr_t) {
be18010e
KW
2361 pr_warn("map '%s': attr '%s': type [%u] not found.\n",
2362 map_name, name, t->type);
abd29c93
AN
2363 return false;
2364 }
b03bc685 2365 if (!btf_is_array(arr_t)) {
81ba0889
AN
2366 pr_warn("map '%s': attr '%s': expected ARRAY, got %s.\n",
2367 map_name, name, btf_kind_str(arr_t));
abd29c93
AN
2368 return false;
2369 }
b03bc685 2370 arr_info = btf_array(arr_t);
ef99b02b 2371 *res = arr_info->nelems;
abd29c93
AN
2372 return true;
2373}
2374
d147357e
AS
2375static bool get_map_field_long(const char *map_name, const struct btf *btf,
2376 const struct btf_member *m, __u64 *res)
2377{
2378 const struct btf_type *t = skip_mods_and_typedefs(btf, m->type, NULL);
2379 const char *name = btf__name_by_offset(btf, m->name_off);
2380
2381 if (btf_is_ptr(t)) {
2382 __u32 res32;
2383 bool ret;
2384
2385 ret = get_map_field_int(map_name, btf, m, &res32);
2386 if (ret)
2387 *res = (__u64)res32;
2388 return ret;
2389 }
2390
2391 if (!btf_is_enum(t) && !btf_is_enum64(t)) {
2392 pr_warn("map '%s': attr '%s': expected ENUM or ENUM64, got %s.\n",
2393 map_name, name, btf_kind_str(t));
2394 return false;
2395 }
2396
2397 if (btf_vlen(t) != 1) {
2398 pr_warn("map '%s': attr '%s': invalid __ulong\n",
2399 map_name, name);
2400 return false;
2401 }
2402
2403 if (btf_is_enum(t)) {
2404 const struct btf_enum *e = btf_enum(t);
2405
2406 *res = e->val;
2407 } else {
2408 const struct btf_enum64 *e = btf_enum64(t);
2409
2410 *res = btf_enum64_value(e);
2411 }
2412 return true;
2413}
2414
e588c116
WY
2415static int pathname_concat(char *buf, size_t buf_sz, const char *path, const char *name)
2416{
2417 int len;
2418
2419 len = snprintf(buf, buf_sz, "%s/%s", path, name);
2420 if (len < 0)
2421 return -EINVAL;
2422 if (len >= buf_sz)
2423 return -ENAMETOOLONG;
2424
2425 return 0;
2426}
2427
57a00f41
THJ
2428static int build_map_pin_path(struct bpf_map *map, const char *path)
2429{
2430 char buf[PATH_MAX];
e588c116 2431 int err;
57a00f41
THJ
2432
2433 if (!path)
6b434b61 2434 path = BPF_FS_DEFAULT_PATH;
57a00f41 2435
e588c116
WY
2436 err = pathname_concat(buf, sizeof(buf), path, bpf_map__name(map));
2437 if (err)
2438 return err;
57a00f41 2439
6e9cab2e 2440 return bpf_map__set_pin_path(map, buf);
57a00f41
THJ
2441}
2442
146bf811
AN
2443/* should match definition in bpf_helpers.h */
2444enum libbpf_pin_type {
2445 LIBBPF_PIN_NONE,
2446 /* PIN_BY_NAME: pin maps by name (in /sys/fs/bpf by default) */
2447 LIBBPF_PIN_BY_NAME,
2448};
2449
c7ef5ec9
AN
2450int parse_btf_map_def(const char *map_name, struct btf *btf,
2451 const struct btf_type *def_t, bool strict,
2452 struct btf_map_def *map_def, struct btf_map_def *inner_def)
abd29c93 2453{
41017e56 2454 const struct btf_type *t;
abd29c93 2455 const struct btf_member *m;
c7ef5ec9 2456 bool is_inner = inner_def == NULL;
abd29c93
AN
2457 int vlen, i;
2458
c7ef5ec9
AN
2459 vlen = btf_vlen(def_t);
2460 m = btf_members(def_t);
abd29c93 2461 for (i = 0; i < vlen; i++, m++) {
c7ef5ec9 2462 const char *name = btf__name_by_offset(btf, m->name_off);
abd29c93
AN
2463
2464 if (!name) {
c7ef5ec9 2465 pr_warn("map '%s': invalid field #%d.\n", map_name, i);
abd29c93
AN
2466 return -EINVAL;
2467 }
2468 if (strcmp(name, "type") == 0) {
c7ef5ec9 2469 if (!get_map_field_int(map_name, btf, m, &map_def->map_type))
abd29c93 2470 return -EINVAL;
c7ef5ec9 2471 map_def->parts |= MAP_DEF_MAP_TYPE;
abd29c93 2472 } else if (strcmp(name, "max_entries") == 0) {
c7ef5ec9 2473 if (!get_map_field_int(map_name, btf, m, &map_def->max_entries))
abd29c93 2474 return -EINVAL;
c7ef5ec9 2475 map_def->parts |= MAP_DEF_MAX_ENTRIES;
abd29c93 2476 } else if (strcmp(name, "map_flags") == 0) {
c7ef5ec9 2477 if (!get_map_field_int(map_name, btf, m, &map_def->map_flags))
abd29c93 2478 return -EINVAL;
c7ef5ec9 2479 map_def->parts |= MAP_DEF_MAP_FLAGS;
1bdb6c9a 2480 } else if (strcmp(name, "numa_node") == 0) {
c7ef5ec9 2481 if (!get_map_field_int(map_name, btf, m, &map_def->numa_node))
1bdb6c9a 2482 return -EINVAL;
c7ef5ec9 2483 map_def->parts |= MAP_DEF_NUMA_NODE;
abd29c93
AN
2484 } else if (strcmp(name, "key_size") == 0) {
2485 __u32 sz;
2486
c7ef5ec9 2487 if (!get_map_field_int(map_name, btf, m, &sz))
abd29c93 2488 return -EINVAL;
c7ef5ec9 2489 if (map_def->key_size && map_def->key_size != sz) {
be18010e 2490 pr_warn("map '%s': conflicting key size %u != %u.\n",
c7ef5ec9 2491 map_name, map_def->key_size, sz);
abd29c93
AN
2492 return -EINVAL;
2493 }
c7ef5ec9
AN
2494 map_def->key_size = sz;
2495 map_def->parts |= MAP_DEF_KEY_SIZE;
abd29c93
AN
2496 } else if (strcmp(name, "key") == 0) {
2497 __s64 sz;
2498
c7ef5ec9 2499 t = btf__type_by_id(btf, m->type);
abd29c93 2500 if (!t) {
be18010e 2501 pr_warn("map '%s': key type [%d] not found.\n",
c7ef5ec9 2502 map_name, m->type);
abd29c93
AN
2503 return -EINVAL;
2504 }
b03bc685 2505 if (!btf_is_ptr(t)) {
81ba0889 2506 pr_warn("map '%s': key spec is not PTR: %s.\n",
c7ef5ec9 2507 map_name, btf_kind_str(t));
abd29c93
AN
2508 return -EINVAL;
2509 }
c7ef5ec9 2510 sz = btf__resolve_size(btf, t->type);
abd29c93 2511 if (sz < 0) {
679152d3 2512 pr_warn("map '%s': can't determine key size for type [%u]: %zd.\n",
c7ef5ec9 2513 map_name, t->type, (ssize_t)sz);
abd29c93
AN
2514 return sz;
2515 }
c7ef5ec9 2516 if (map_def->key_size && map_def->key_size != sz) {
679152d3 2517 pr_warn("map '%s': conflicting key size %u != %zd.\n",
c7ef5ec9 2518 map_name, map_def->key_size, (ssize_t)sz);
abd29c93
AN
2519 return -EINVAL;
2520 }
c7ef5ec9
AN
2521 map_def->key_size = sz;
2522 map_def->key_type_id = t->type;
2523 map_def->parts |= MAP_DEF_KEY_SIZE | MAP_DEF_KEY_TYPE;
abd29c93
AN
2524 } else if (strcmp(name, "value_size") == 0) {
2525 __u32 sz;
2526
c7ef5ec9 2527 if (!get_map_field_int(map_name, btf, m, &sz))
abd29c93 2528 return -EINVAL;
c7ef5ec9 2529 if (map_def->value_size && map_def->value_size != sz) {
be18010e 2530 pr_warn("map '%s': conflicting value size %u != %u.\n",
c7ef5ec9 2531 map_name, map_def->value_size, sz);
abd29c93
AN
2532 return -EINVAL;
2533 }
c7ef5ec9
AN
2534 map_def->value_size = sz;
2535 map_def->parts |= MAP_DEF_VALUE_SIZE;
abd29c93
AN
2536 } else if (strcmp(name, "value") == 0) {
2537 __s64 sz;
2538
c7ef5ec9 2539 t = btf__type_by_id(btf, m->type);
abd29c93 2540 if (!t) {
be18010e 2541 pr_warn("map '%s': value type [%d] not found.\n",
c7ef5ec9 2542 map_name, m->type);
abd29c93
AN
2543 return -EINVAL;
2544 }
b03bc685 2545 if (!btf_is_ptr(t)) {
81ba0889 2546 pr_warn("map '%s': value spec is not PTR: %s.\n",
c7ef5ec9 2547 map_name, btf_kind_str(t));
abd29c93
AN
2548 return -EINVAL;
2549 }
c7ef5ec9 2550 sz = btf__resolve_size(btf, t->type);
abd29c93 2551 if (sz < 0) {
679152d3 2552 pr_warn("map '%s': can't determine value size for type [%u]: %zd.\n",
c7ef5ec9 2553 map_name, t->type, (ssize_t)sz);
abd29c93
AN
2554 return sz;
2555 }
c7ef5ec9 2556 if (map_def->value_size && map_def->value_size != sz) {
679152d3 2557 pr_warn("map '%s': conflicting value size %u != %zd.\n",
c7ef5ec9 2558 map_name, map_def->value_size, (ssize_t)sz);
abd29c93
AN
2559 return -EINVAL;
2560 }
c7ef5ec9
AN
2561 map_def->value_size = sz;
2562 map_def->value_type_id = t->type;
2563 map_def->parts |= MAP_DEF_VALUE_SIZE | MAP_DEF_VALUE_TYPE;
646f02ff
AN
2564 }
2565 else if (strcmp(name, "values") == 0) {
341ac5ff
HC
2566 bool is_map_in_map = bpf_map_type__is_map_in_map(map_def->map_type);
2567 bool is_prog_array = map_def->map_type == BPF_MAP_TYPE_PROG_ARRAY;
2568 const char *desc = is_map_in_map ? "map-in-map inner" : "prog-array value";
c7ef5ec9 2569 char inner_map_name[128];
646f02ff
AN
2570 int err;
2571
2572 if (is_inner) {
2573 pr_warn("map '%s': multi-level inner maps not supported.\n",
c7ef5ec9 2574 map_name);
646f02ff
AN
2575 return -ENOTSUP;
2576 }
2577 if (i != vlen - 1) {
2578 pr_warn("map '%s': '%s' member should be last.\n",
c7ef5ec9 2579 map_name, name);
646f02ff
AN
2580 return -EINVAL;
2581 }
341ac5ff
HC
2582 if (!is_map_in_map && !is_prog_array) {
2583 pr_warn("map '%s': should be map-in-map or prog-array.\n",
c7ef5ec9 2584 map_name);
646f02ff
AN
2585 return -ENOTSUP;
2586 }
c7ef5ec9 2587 if (map_def->value_size && map_def->value_size != 4) {
646f02ff 2588 pr_warn("map '%s': conflicting value size %u != 4.\n",
c7ef5ec9 2589 map_name, map_def->value_size);
646f02ff
AN
2590 return -EINVAL;
2591 }
c7ef5ec9
AN
2592 map_def->value_size = 4;
2593 t = btf__type_by_id(btf, m->type);
646f02ff 2594 if (!t) {
341ac5ff
HC
2595 pr_warn("map '%s': %s type [%d] not found.\n",
2596 map_name, desc, m->type);
646f02ff
AN
2597 return -EINVAL;
2598 }
2599 if (!btf_is_array(t) || btf_array(t)->nelems) {
341ac5ff
HC
2600 pr_warn("map '%s': %s spec is not a zero-sized array.\n",
2601 map_name, desc);
646f02ff
AN
2602 return -EINVAL;
2603 }
c7ef5ec9 2604 t = skip_mods_and_typedefs(btf, btf_array(t)->type, NULL);
646f02ff 2605 if (!btf_is_ptr(t)) {
341ac5ff
HC
2606 pr_warn("map '%s': %s def is of unexpected kind %s.\n",
2607 map_name, desc, btf_kind_str(t));
646f02ff
AN
2608 return -EINVAL;
2609 }
c7ef5ec9 2610 t = skip_mods_and_typedefs(btf, t->type, NULL);
341ac5ff
HC
2611 if (is_prog_array) {
2612 if (!btf_is_func_proto(t)) {
2613 pr_warn("map '%s': prog-array value def is of unexpected kind %s.\n",
2614 map_name, btf_kind_str(t));
2615 return -EINVAL;
2616 }
2617 continue;
2618 }
646f02ff 2619 if (!btf_is_struct(t)) {
81ba0889 2620 pr_warn("map '%s': map-in-map inner def is of unexpected kind %s.\n",
c7ef5ec9 2621 map_name, btf_kind_str(t));
646f02ff
AN
2622 return -EINVAL;
2623 }
2624
c7ef5ec9
AN
2625 snprintf(inner_map_name, sizeof(inner_map_name), "%s.inner", map_name);
2626 err = parse_btf_map_def(inner_map_name, btf, t, strict, inner_def, NULL);
646f02ff
AN
2627 if (err)
2628 return err;
c7ef5ec9
AN
2629
2630 map_def->parts |= MAP_DEF_INNER_MAP;
57a00f41
THJ
2631 } else if (strcmp(name, "pinning") == 0) {
2632 __u32 val;
57a00f41 2633
646f02ff 2634 if (is_inner) {
c7ef5ec9 2635 pr_warn("map '%s': inner def can't be pinned.\n", map_name);
646f02ff
AN
2636 return -EINVAL;
2637 }
c7ef5ec9 2638 if (!get_map_field_int(map_name, btf, m, &val))
57a00f41 2639 return -EINVAL;
c7ef5ec9 2640 if (val != LIBBPF_PIN_NONE && val != LIBBPF_PIN_BY_NAME) {
57a00f41 2641 pr_warn("map '%s': invalid pinning value %u.\n",
c7ef5ec9 2642 map_name, val);
57a00f41
THJ
2643 return -EINVAL;
2644 }
c7ef5ec9
AN
2645 map_def->pinning = val;
2646 map_def->parts |= MAP_DEF_PINNING;
47512102 2647 } else if (strcmp(name, "map_extra") == 0) {
d147357e 2648 __u64 map_extra;
47512102 2649
d147357e 2650 if (!get_map_field_long(map_name, btf, m, &map_extra))
47512102
JK
2651 return -EINVAL;
2652 map_def->map_extra = map_extra;
2653 map_def->parts |= MAP_DEF_MAP_EXTRA;
abd29c93
AN
2654 } else {
2655 if (strict) {
c7ef5ec9 2656 pr_warn("map '%s': unknown field '%s'.\n", map_name, name);
abd29c93
AN
2657 return -ENOTSUP;
2658 }
c7ef5ec9 2659 pr_debug("map '%s': ignoring unknown field '%s'.\n", map_name, name);
abd29c93
AN
2660 }
2661 }
2662
c7ef5ec9
AN
2663 if (map_def->map_type == BPF_MAP_TYPE_UNSPEC) {
2664 pr_warn("map '%s': map type isn't specified.\n", map_name);
abd29c93
AN
2665 return -EINVAL;
2666 }
2667
2668 return 0;
2669}
2670
597fbc46
AN
2671static size_t adjust_ringbuf_sz(size_t sz)
2672{
2673 __u32 page_sz = sysconf(_SC_PAGE_SIZE);
2674 __u32 mul;
2675
2676 /* if user forgot to set any size, make sure they see error */
2677 if (sz == 0)
2678 return 0;
2679 /* Kernel expects BPF_MAP_TYPE_RINGBUF's max_entries to be
2680 * a power-of-2 multiple of kernel's page size. If user diligently
2681 * satisified these conditions, pass the size through.
2682 */
2683 if ((sz % page_sz) == 0 && is_pow_of_2(sz / page_sz))
2684 return sz;
2685
2686 /* Otherwise find closest (page_sz * power_of_2) product bigger than
2687 * user-set size to satisfy both user size request and kernel
2688 * requirements and substitute correct max_entries for map creation.
2689 */
2690 for (mul = 1; mul <= UINT_MAX / page_sz; mul <<= 1) {
2691 if (mul * page_sz > sz)
2692 return mul * page_sz;
2693 }
2694
2695 /* if it's impossible to satisfy the conditions (i.e., user size is
2696 * very close to UINT_MAX but is not a power-of-2 multiple of
2697 * page_size) then just return original size and let kernel reject it
2698 */
2699 return sz;
2700}
2701
b66ccae0
DV
2702static bool map_is_ringbuf(const struct bpf_map *map)
2703{
2704 return map->def.type == BPF_MAP_TYPE_RINGBUF ||
2705 map->def.type == BPF_MAP_TYPE_USER_RINGBUF;
2706}
2707
c7ef5ec9
AN
2708static void fill_map_from_def(struct bpf_map *map, const struct btf_map_def *def)
2709{
2710 map->def.type = def->map_type;
2711 map->def.key_size = def->key_size;
2712 map->def.value_size = def->value_size;
2713 map->def.max_entries = def->max_entries;
2714 map->def.map_flags = def->map_flags;
47512102 2715 map->map_extra = def->map_extra;
c7ef5ec9
AN
2716
2717 map->numa_node = def->numa_node;
2718 map->btf_key_type_id = def->key_type_id;
2719 map->btf_value_type_id = def->value_type_id;
2720
597fbc46 2721 /* auto-adjust BPF ringbuf map max_entries to be a multiple of page size */
b66ccae0 2722 if (map_is_ringbuf(map))
597fbc46
AN
2723 map->def.max_entries = adjust_ringbuf_sz(map->def.max_entries);
2724
c7ef5ec9
AN
2725 if (def->parts & MAP_DEF_MAP_TYPE)
2726 pr_debug("map '%s': found type = %u.\n", map->name, def->map_type);
2727
2728 if (def->parts & MAP_DEF_KEY_TYPE)
2729 pr_debug("map '%s': found key [%u], sz = %u.\n",
2730 map->name, def->key_type_id, def->key_size);
2731 else if (def->parts & MAP_DEF_KEY_SIZE)
2732 pr_debug("map '%s': found key_size = %u.\n", map->name, def->key_size);
2733
2734 if (def->parts & MAP_DEF_VALUE_TYPE)
2735 pr_debug("map '%s': found value [%u], sz = %u.\n",
2736 map->name, def->value_type_id, def->value_size);
2737 else if (def->parts & MAP_DEF_VALUE_SIZE)
2738 pr_debug("map '%s': found value_size = %u.\n", map->name, def->value_size);
2739
2740 if (def->parts & MAP_DEF_MAX_ENTRIES)
2741 pr_debug("map '%s': found max_entries = %u.\n", map->name, def->max_entries);
2742 if (def->parts & MAP_DEF_MAP_FLAGS)
47512102
JK
2743 pr_debug("map '%s': found map_flags = 0x%x.\n", map->name, def->map_flags);
2744 if (def->parts & MAP_DEF_MAP_EXTRA)
2745 pr_debug("map '%s': found map_extra = 0x%llx.\n", map->name,
2746 (unsigned long long)def->map_extra);
c7ef5ec9
AN
2747 if (def->parts & MAP_DEF_PINNING)
2748 pr_debug("map '%s': found pinning = %u.\n", map->name, def->pinning);
2749 if (def->parts & MAP_DEF_NUMA_NODE)
2750 pr_debug("map '%s': found numa_node = %u.\n", map->name, def->numa_node);
2751
2752 if (def->parts & MAP_DEF_INNER_MAP)
2753 pr_debug("map '%s': found inner map definition.\n", map->name);
2754}
2755
c1cccec9
AN
2756static const char *btf_var_linkage_str(__u32 linkage)
2757{
2758 switch (linkage) {
2759 case BTF_VAR_STATIC: return "static";
2760 case BTF_VAR_GLOBAL_ALLOCATED: return "global";
2761 case BTF_VAR_GLOBAL_EXTERN: return "extern";
2762 default: return "unknown";
2763 }
2764}
2765
41017e56
AN
2766static int bpf_object__init_user_btf_map(struct bpf_object *obj,
2767 const struct btf_type *sec,
2768 int var_idx, int sec_idx,
2769 const Elf_Data *data, bool strict,
2770 const char *pin_root_path)
2771{
c7ef5ec9 2772 struct btf_map_def map_def = {}, inner_def = {};
41017e56
AN
2773 const struct btf_type *var, *def;
2774 const struct btf_var_secinfo *vi;
2775 const struct btf_var *var_extra;
2776 const char *map_name;
2777 struct bpf_map *map;
c7ef5ec9 2778 int err;
41017e56
AN
2779
2780 vi = btf_var_secinfos(sec) + var_idx;
2781 var = btf__type_by_id(obj->btf, vi->type);
2782 var_extra = btf_var(var);
2783 map_name = btf__name_by_offset(obj->btf, var->name_off);
2784
2785 if (map_name == NULL || map_name[0] == '\0') {
2786 pr_warn("map #%d: empty name.\n", var_idx);
2787 return -EINVAL;
2788 }
2789 if ((__u64)vi->offset + vi->size > data->d_size) {
2790 pr_warn("map '%s' BTF data is corrupted.\n", map_name);
2791 return -EINVAL;
2792 }
2793 if (!btf_is_var(var)) {
81ba0889
AN
2794 pr_warn("map '%s': unexpected var kind %s.\n",
2795 map_name, btf_kind_str(var));
41017e56
AN
2796 return -EINVAL;
2797 }
c1cccec9
AN
2798 if (var_extra->linkage != BTF_VAR_GLOBAL_ALLOCATED) {
2799 pr_warn("map '%s': unsupported map linkage %s.\n",
2800 map_name, btf_var_linkage_str(var_extra->linkage));
41017e56
AN
2801 return -EOPNOTSUPP;
2802 }
2803
2804 def = skip_mods_and_typedefs(obj->btf, var->type, NULL);
2805 if (!btf_is_struct(def)) {
81ba0889
AN
2806 pr_warn("map '%s': unexpected def kind %s.\n",
2807 map_name, btf_kind_str(var));
41017e56
AN
2808 return -EINVAL;
2809 }
2810 if (def->size > vi->size) {
2811 pr_warn("map '%s': invalid def size.\n", map_name);
2812 return -EINVAL;
2813 }
2814
2815 map = bpf_object__add_map(obj);
2816 if (IS_ERR(map))
2817 return PTR_ERR(map);
2818 map->name = strdup(map_name);
2819 if (!map->name) {
2820 pr_warn("map '%s': failed to alloc map name.\n", map_name);
2821 return -ENOMEM;
2822 }
2823 map->libbpf_type = LIBBPF_MAP_UNSPEC;
2824 map->def.type = BPF_MAP_TYPE_UNSPEC;
2825 map->sec_idx = sec_idx;
2826 map->sec_offset = vi->offset;
646f02ff 2827 map->btf_var_idx = var_idx;
41017e56
AN
2828 pr_debug("map '%s': at sec_idx %d, offset %zu.\n",
2829 map_name, map->sec_idx, map->sec_offset);
2830
c7ef5ec9
AN
2831 err = parse_btf_map_def(map->name, obj->btf, def, strict, &map_def, &inner_def);
2832 if (err)
2833 return err;
2834
2835 fill_map_from_def(map, &map_def);
2836
2837 if (map_def.pinning == LIBBPF_PIN_BY_NAME) {
2838 err = build_map_pin_path(map, pin_root_path);
2839 if (err) {
2840 pr_warn("map '%s': couldn't build pin path.\n", map->name);
2841 return err;
2842 }
2843 }
2844
2845 if (map_def.parts & MAP_DEF_INNER_MAP) {
2846 map->inner_map = calloc(1, sizeof(*map->inner_map));
2847 if (!map->inner_map)
2848 return -ENOMEM;
dac645b9
AN
2849 map->inner_map->fd = create_placeholder_fd();
2850 if (map->inner_map->fd < 0)
2851 return map->inner_map->fd;
c7ef5ec9
AN
2852 map->inner_map->sec_idx = sec_idx;
2853 map->inner_map->name = malloc(strlen(map_name) + sizeof(".inner") + 1);
2854 if (!map->inner_map->name)
2855 return -ENOMEM;
2856 sprintf(map->inner_map->name, "%s.inner", map_name);
2857
2858 fill_map_from_def(map->inner_map, &inner_def);
2859 }
2860
4fcac46c 2861 err = map_fill_btf_type_info(obj, map);
262cfb74
DK
2862 if (err)
2863 return err;
2864
c7ef5ec9 2865 return 0;
41017e56
AN
2866}
2867
2e7ba4f8
AN
2868static int init_arena_map_data(struct bpf_object *obj, struct bpf_map *map,
2869 const char *sec_name, int sec_idx,
2870 void *data, size_t data_sz)
2871{
2872 const long page_sz = sysconf(_SC_PAGE_SIZE);
2873 size_t mmap_sz;
2874
2875 mmap_sz = bpf_map_mmap_sz(obj->arena_map);
2876 if (roundup(data_sz, page_sz) > mmap_sz) {
2877 pr_warn("elf: sec '%s': declared ARENA map size (%zu) is too small to hold global __arena variables of size %zu\n",
2878 sec_name, mmap_sz, data_sz);
2879 return -E2BIG;
2880 }
2881
2882 obj->arena_data = malloc(data_sz);
2883 if (!obj->arena_data)
2884 return -ENOMEM;
2885 memcpy(obj->arena_data, data, data_sz);
2886 obj->arena_data_sz = data_sz;
2887
2888 /* make bpf_map__init_value() work for ARENA maps */
2889 map->mmaped = obj->arena_data;
2890
2891 return 0;
2892}
2893
57a00f41
THJ
2894static int bpf_object__init_user_btf_maps(struct bpf_object *obj, bool strict,
2895 const char *pin_root_path)
abd29c93
AN
2896{
2897 const struct btf_type *sec = NULL;
2898 int nr_types, i, vlen, err;
2899 const struct btf_type *t;
2900 const char *name;
2901 Elf_Data *data;
2902 Elf_Scn *scn;
2903
2904 if (obj->efile.btf_maps_shndx < 0)
2905 return 0;
2906
88a82120
AN
2907 scn = elf_sec_by_idx(obj, obj->efile.btf_maps_shndx);
2908 data = elf_sec_data(obj, scn);
abd29c93 2909 if (!scn || !data) {
88a82120
AN
2910 pr_warn("elf: failed to get %s map definitions for %s\n",
2911 MAPS_ELF_SEC, obj->path);
abd29c93
AN
2912 return -EINVAL;
2913 }
2914
6a886de0
HC
2915 nr_types = btf__type_cnt(obj->btf);
2916 for (i = 1; i < nr_types; i++) {
abd29c93 2917 t = btf__type_by_id(obj->btf, i);
b03bc685 2918 if (!btf_is_datasec(t))
abd29c93
AN
2919 continue;
2920 name = btf__name_by_offset(obj->btf, t->name_off);
2921 if (strcmp(name, MAPS_ELF_SEC) == 0) {
2922 sec = t;
646f02ff 2923 obj->efile.btf_maps_sec_btf_id = i;
abd29c93
AN
2924 break;
2925 }
2926 }
2927
2928 if (!sec) {
be18010e 2929 pr_warn("DATASEC '%s' not found.\n", MAPS_ELF_SEC);
abd29c93
AN
2930 return -ENOENT;
2931 }
2932
b03bc685 2933 vlen = btf_vlen(sec);
abd29c93
AN
2934 for (i = 0; i < vlen; i++) {
2935 err = bpf_object__init_user_btf_map(obj, sec, i,
2936 obj->efile.btf_maps_shndx,
8983b731
AN
2937 data, strict,
2938 pin_root_path);
abd29c93
AN
2939 if (err)
2940 return err;
2941 }
2942
2e7ba4f8
AN
2943 for (i = 0; i < obj->nr_maps; i++) {
2944 struct bpf_map *map = &obj->maps[i];
2945
2946 if (map->def.type != BPF_MAP_TYPE_ARENA)
2947 continue;
2948
2949 if (obj->arena_map) {
2950 pr_warn("map '%s': only single ARENA map is supported (map '%s' is also ARENA)\n",
2951 map->name, obj->arena_map->name);
2952 return -EINVAL;
2953 }
2954 obj->arena_map = map;
2955
2956 if (obj->efile.arena_data) {
2957 err = init_arena_map_data(obj, map, ARENA_SEC, obj->efile.arena_data_shndx,
2958 obj->efile.arena_data->d_buf,
2959 obj->efile.arena_data->d_size);
2960 if (err)
2961 return err;
2962 }
2963 }
2964 if (obj->efile.arena_data && !obj->arena_map) {
2965 pr_warn("elf: sec '%s': to use global __arena variables the ARENA map should be explicitly declared in SEC(\".maps\")\n",
2966 ARENA_SEC);
2967 return -ENOENT;
2968 }
2969
abd29c93
AN
2970 return 0;
2971}
2972
0d13bfce 2973static int bpf_object__init_maps(struct bpf_object *obj,
01af3bf0 2974 const struct bpf_object_open_opts *opts)
bf829271 2975{
166750bc
AN
2976 const char *pin_root_path;
2977 bool strict;
bd054102 2978 int err = 0;
8837fe5d 2979
166750bc
AN
2980 strict = !OPTS_GET(opts, relaxed_maps, false);
2981 pin_root_path = OPTS_GET(opts, pin_root_path, NULL);
bf829271 2982
40e1bcab 2983 err = bpf_object__init_user_btf_maps(obj, strict, pin_root_path);
166750bc 2984 err = err ?: bpf_object__init_global_data_maps(obj);
81bfdd08 2985 err = err ?: bpf_object__init_kconfig_map(obj);
809a69d6 2986 err = err ?: bpf_object_init_struct_ops(obj);
bf829271 2987
3b3af91c 2988 return err;
561bbcca
WN
2989}
2990
e3d91b0c
JDB
2991static bool section_have_execinstr(struct bpf_object *obj, int idx)
2992{
ad23b723 2993 Elf64_Shdr *sh;
e3d91b0c 2994
ad23b723
AN
2995 sh = elf_sec_hdr(obj, elf_sec_by_idx(obj, idx));
2996 if (!sh)
e3d91b0c
JDB
2997 return false;
2998
ad23b723 2999 return sh->sh_flags & SHF_EXECINSTR;
e3d91b0c
JDB
3000}
3001
6ebaa3fb
EZ
3002static bool starts_with_qmark(const char *s)
3003{
3004 return s && s[0] == '?';
3005}
3006
0f0e55d8
AN
3007static bool btf_needs_sanitization(struct bpf_object *obj)
3008{
9ca1f56a
AS
3009 bool has_func_global = kernel_supports(obj, FEAT_BTF_GLOBAL_FUNC);
3010 bool has_datasec = kernel_supports(obj, FEAT_BTF_DATASEC);
3011 bool has_float = kernel_supports(obj, FEAT_BTF_FLOAT);
3012 bool has_func = kernel_supports(obj, FEAT_BTF_FUNC);
223f903e 3013 bool has_decl_tag = kernel_supports(obj, FEAT_BTF_DECL_TAG);
2dc1e488 3014 bool has_type_tag = kernel_supports(obj, FEAT_BTF_TYPE_TAG);
f2a62588 3015 bool has_enum64 = kernel_supports(obj, FEAT_BTF_ENUM64);
6ebaa3fb 3016 bool has_qmark_datasec = kernel_supports(obj, FEAT_BTF_QMARK_DATASEC);
0f0e55d8 3017
2dc1e488 3018 return !has_func || !has_datasec || !has_func_global || !has_float ||
6ebaa3fb 3019 !has_decl_tag || !has_type_tag || !has_enum64 || !has_qmark_datasec;
0f0e55d8
AN
3020}
3021
f2a62588 3022static int bpf_object__sanitize_btf(struct bpf_object *obj, struct btf *btf)
d7c4b398 3023{
9ca1f56a
AS
3024 bool has_func_global = kernel_supports(obj, FEAT_BTF_GLOBAL_FUNC);
3025 bool has_datasec = kernel_supports(obj, FEAT_BTF_DATASEC);
3026 bool has_float = kernel_supports(obj, FEAT_BTF_FLOAT);
3027 bool has_func = kernel_supports(obj, FEAT_BTF_FUNC);
223f903e 3028 bool has_decl_tag = kernel_supports(obj, FEAT_BTF_DECL_TAG);
2dc1e488 3029 bool has_type_tag = kernel_supports(obj, FEAT_BTF_TYPE_TAG);
f2a62588 3030 bool has_enum64 = kernel_supports(obj, FEAT_BTF_ENUM64);
6ebaa3fb 3031 bool has_qmark_datasec = kernel_supports(obj, FEAT_BTF_QMARK_DATASEC);
c49a44b3 3032 int enum64_placeholder_id = 0;
d7c4b398
AN
3033 struct btf_type *t;
3034 int i, j, vlen;
d7c4b398 3035
6a886de0 3036 for (i = 1; i < btf__type_cnt(btf); i++) {
d7c4b398 3037 t = (struct btf_type *)btf__type_by_id(btf, i);
d7c4b398 3038
223f903e
YS
3039 if ((!has_datasec && btf_is_var(t)) || (!has_decl_tag && btf_is_decl_tag(t))) {
3040 /* replace VAR/DECL_TAG with INT */
d7c4b398 3041 t->info = BTF_INFO_ENC(BTF_KIND_INT, 0, 0);
1d4126c4
AN
3042 /*
3043 * using size = 1 is the safest choice, 4 will be too
3044 * big and cause kernel BTF validation failure if
3045 * original variable took less than 4 bytes
3046 */
3047 t->size = 1;
708852dc 3048 *(int *)(t + 1) = BTF_INT_ENC(0, 0, 8);
b03bc685 3049 } else if (!has_datasec && btf_is_datasec(t)) {
d7c4b398 3050 /* replace DATASEC with STRUCT */
b03bc685
AN
3051 const struct btf_var_secinfo *v = btf_var_secinfos(t);
3052 struct btf_member *m = btf_members(t);
d7c4b398
AN
3053 struct btf_type *vt;
3054 char *name;
3055
3056 name = (char *)btf__name_by_offset(btf, t->name_off);
3057 while (*name) {
6ebaa3fb 3058 if (*name == '.' || *name == '?')
d7c4b398
AN
3059 *name = '_';
3060 name++;
3061 }
3062
b03bc685 3063 vlen = btf_vlen(t);
d7c4b398
AN
3064 t->info = BTF_INFO_ENC(BTF_KIND_STRUCT, 0, vlen);
3065 for (j = 0; j < vlen; j++, v++, m++) {
3066 /* order of field assignments is important */
3067 m->offset = v->offset * 8;
3068 m->type = v->type;
3069 /* preserve variable name as member name */
3070 vt = (void *)btf__type_by_id(btf, v->type);
3071 m->name_off = vt->name_off;
3072 }
6ebaa3fb
EZ
3073 } else if (!has_qmark_datasec && btf_is_datasec(t) &&
3074 starts_with_qmark(btf__name_by_offset(btf, t->name_off))) {
3075 /* replace '?' prefix with '_' for DATASEC names */
3076 char *name;
3077
3078 name = (char *)btf__name_by_offset(btf, t->name_off);
3079 if (name[0] == '?')
3080 name[0] = '_';
b03bc685 3081 } else if (!has_func && btf_is_func_proto(t)) {
d7c4b398 3082 /* replace FUNC_PROTO with ENUM */
b03bc685 3083 vlen = btf_vlen(t);
d7c4b398
AN
3084 t->info = BTF_INFO_ENC(BTF_KIND_ENUM, 0, vlen);
3085 t->size = sizeof(__u32); /* kernel enforced */
b03bc685 3086 } else if (!has_func && btf_is_func(t)) {
d7c4b398
AN
3087 /* replace FUNC with TYPEDEF */
3088 t->info = BTF_INFO_ENC(BTF_KIND_TYPEDEF, 0, 0);
2d3eb67f
AS
3089 } else if (!has_func_global && btf_is_func(t)) {
3090 /* replace BTF_FUNC_GLOBAL with BTF_FUNC_STATIC */
3091 t->info = BTF_INFO_ENC(BTF_KIND_FUNC, 0, 0);
22541a9e
IL
3092 } else if (!has_float && btf_is_float(t)) {
3093 /* replace FLOAT with an equally-sized empty STRUCT;
3094 * since C compilers do not accept e.g. "float" as a
3095 * valid struct name, make it anonymous
3096 */
3097 t->name_off = 0;
3098 t->info = BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 0);
2dc1e488
YS
3099 } else if (!has_type_tag && btf_is_type_tag(t)) {
3100 /* replace TYPE_TAG with a CONST */
3101 t->name_off = 0;
3102 t->info = BTF_INFO_ENC(BTF_KIND_CONST, 0, 0);
f2a62588
YS
3103 } else if (!has_enum64 && btf_is_enum(t)) {
3104 /* clear the kflag */
3105 t->info = btf_type_info(btf_kind(t), btf_vlen(t), false);
3106 } else if (!has_enum64 && btf_is_enum64(t)) {
3107 /* replace ENUM64 with a union */
3108 struct btf_member *m;
3109
3110 if (enum64_placeholder_id == 0) {
3111 enum64_placeholder_id = btf__add_int(btf, "enum64_placeholder", 1, 0);
3112 if (enum64_placeholder_id < 0)
3113 return enum64_placeholder_id;
3114
3115 t = (struct btf_type *)btf__type_by_id(btf, i);
3116 }
3117
3118 m = btf_members(t);
3119 vlen = btf_vlen(t);
3120 t->info = BTF_INFO_ENC(BTF_KIND_UNION, 0, vlen);
3121 for (j = 0; j < vlen; j++, m++) {
3122 m->type = enum64_placeholder_id;
3123 m->offset = 0;
3124 }
e3ba8e4e 3125 }
d7c4b398 3126 }
f2a62588
YS
3127
3128 return 0;
d7c4b398
AN
3129}
3130
b35f14f4 3131static bool libbpf_needs_btf(const struct bpf_object *obj)
abd29c93 3132{
b35f14f4 3133 return obj->efile.btf_maps_shndx >= 0 ||
240bf8a5 3134 obj->efile.has_st_ops ||
b35f14f4
AN
3135 obj->nr_extern > 0;
3136}
3137
3138static bool kernel_needs_btf(const struct bpf_object *obj)
3139{
240bf8a5 3140 return obj->efile.has_st_ops;
abd29c93
AN
3141}
3142
063183bf 3143static int bpf_object__init_btf(struct bpf_object *obj,
9c6660d0
AN
3144 Elf_Data *btf_data,
3145 Elf_Data *btf_ext_data)
3146{
b7d7f3e1 3147 int err = -ENOENT;
9c6660d0
AN
3148
3149 if (btf_data) {
3150 obj->btf = btf__new(btf_data->d_buf, btf_data->d_size);
e9fc3ce9
AN
3151 err = libbpf_get_error(obj->btf);
3152 if (err) {
b7d7f3e1 3153 obj->btf = NULL;
e9fc3ce9 3154 pr_warn("Error loading ELF section %s: %d.\n", BTF_ELF_SEC, err);
9c6660d0
AN
3155 goto out;
3156 }
4c01925f
AN
3157 /* enforce 8-byte pointers for BPF-targeted BTFs */
3158 btf__set_pointer_size(obj->btf, 8);
9c6660d0
AN
3159 }
3160 if (btf_ext_data) {
11d5daa8
AN
3161 struct btf_ext_info *ext_segs[3];
3162 int seg_num, sec_num;
3163
9c6660d0
AN
3164 if (!obj->btf) {
3165 pr_debug("Ignore ELF section %s because its depending ELF section %s is not found.\n",
3166 BTF_EXT_ELF_SEC, BTF_ELF_SEC);
3167 goto out;
3168 }
e9fc3ce9
AN
3169 obj->btf_ext = btf_ext__new(btf_ext_data->d_buf, btf_ext_data->d_size);
3170 err = libbpf_get_error(obj->btf_ext);
3171 if (err) {
3172 pr_warn("Error loading ELF section %s: %d. Ignored and continue.\n",
3173 BTF_EXT_ELF_SEC, err);
9c6660d0
AN
3174 obj->btf_ext = NULL;
3175 goto out;
3176 }
11d5daa8
AN
3177
3178 /* setup .BTF.ext to ELF section mapping */
3179 ext_segs[0] = &obj->btf_ext->func_info;
3180 ext_segs[1] = &obj->btf_ext->line_info;
3181 ext_segs[2] = &obj->btf_ext->core_relo_info;
3182 for (seg_num = 0; seg_num < ARRAY_SIZE(ext_segs); seg_num++) {
3183 struct btf_ext_info *seg = ext_segs[seg_num];
3184 const struct btf_ext_info_sec *sec;
3185 const char *sec_name;
3186 Elf_Scn *scn;
3187
3188 if (seg->sec_cnt == 0)
3189 continue;
3190
3191 seg->sec_idxs = calloc(seg->sec_cnt, sizeof(*seg->sec_idxs));
3192 if (!seg->sec_idxs) {
3193 err = -ENOMEM;
3194 goto out;
3195 }
3196
3197 sec_num = 0;
3198 for_each_btf_ext_sec(seg, sec) {
3199 /* preventively increment index to avoid doing
3200 * this before every continue below
3201 */
3202 sec_num++;
3203
3204 sec_name = btf__name_by_offset(obj->btf, sec->sec_name_off);
3205 if (str_is_empty(sec_name))
3206 continue;
3207 scn = elf_sec_by_name(obj, sec_name);
3208 if (!scn)
3209 continue;
3210
3211 seg->sec_idxs[sec_num - 1] = elf_ndxscn(scn);
3212 }
3213 }
9c6660d0
AN
3214 }
3215out:
b35f14f4 3216 if (err && libbpf_needs_btf(obj)) {
be18010e 3217 pr_warn("BTF is required, but is missing or corrupted.\n");
b7d7f3e1 3218 return err;
abd29c93 3219 }
9c6660d0
AN
3220 return 0;
3221}
3222
b96c07f3
AN
3223static int compare_vsi_off(const void *_a, const void *_b)
3224{
3225 const struct btf_var_secinfo *a = _a;
3226 const struct btf_var_secinfo *b = _b;
3227
3228 return a->offset - b->offset;
3229}
3230
3231static int btf_fixup_datasec(struct bpf_object *obj, struct btf *btf,
3232 struct btf_type *t)
3233{
f33f742d
AN
3234 __u32 size = 0, i, vars = btf_vlen(t);
3235 const char *sec_name = btf__name_by_offset(btf, t->name_off);
b96c07f3 3236 struct btf_var_secinfo *vsi;
4fcac46c 3237 bool fixup_offsets = false;
f33f742d 3238 int err;
b96c07f3 3239
f33f742d 3240 if (!sec_name) {
b96c07f3
AN
3241 pr_debug("No name found in string section for DATASEC kind.\n");
3242 return -ENOENT;
3243 }
3244
4fcac46c
AN
3245 /* Extern-backing datasecs (.ksyms, .kconfig) have their size and
3246 * variable offsets set at the previous step. Further, not every
3247 * extern BTF VAR has corresponding ELF symbol preserved, so we skip
3248 * all fixups altogether for such sections and go straight to sorting
3249 * VARs within their DATASEC.
b96c07f3 3250 */
4fcac46c 3251 if (strcmp(sec_name, KCONFIG_SEC) == 0 || strcmp(sec_name, KSYMS_SEC) == 0)
b96c07f3
AN
3252 goto sort_vars;
3253
4fcac46c
AN
3254 /* Clang leaves DATASEC size and VAR offsets as zeroes, so we need to
3255 * fix this up. But BPF static linker already fixes this up and fills
3256 * all the sizes and offsets during static linking. So this step has
3257 * to be optional. But the STV_HIDDEN handling is non-optional for any
3258 * non-extern DATASEC, so the variable fixup loop below handles both
3259 * functions at the same time, paying the cost of BTF VAR <-> ELF
3260 * symbol matching just once.
3261 */
3262 if (t->size == 0) {
3263 err = find_elf_sec_sz(obj, sec_name, &size);
3264 if (err || !size) {
3265 pr_debug("sec '%s': failed to determine size from ELF: size %u, err %d\n",
3266 sec_name, size, err);
3267 return -ENOENT;
3268 }
b96c07f3 3269
4fcac46c
AN
3270 t->size = size;
3271 fixup_offsets = true;
3272 }
b96c07f3
AN
3273
3274 for (i = 0, vsi = btf_var_secinfos(t); i < vars; i++, vsi++) {
f33f742d
AN
3275 const struct btf_type *t_var;
3276 struct btf_var *var;
3277 const char *var_name;
3278 Elf64_Sym *sym;
3279
b96c07f3 3280 t_var = btf__type_by_id(btf, vsi->type);
88918dc1 3281 if (!t_var || !btf_is_var(t_var)) {
f33f742d 3282 pr_debug("sec '%s': unexpected non-VAR type found\n", sec_name);
b96c07f3
AN
3283 return -EINVAL;
3284 }
3285
88918dc1 3286 var = btf_var(t_var);
f33f742d 3287 if (var->linkage == BTF_VAR_STATIC || var->linkage == BTF_VAR_GLOBAL_EXTERN)
b96c07f3
AN
3288 continue;
3289
f33f742d
AN
3290 var_name = btf__name_by_offset(btf, t_var->name_off);
3291 if (!var_name) {
3292 pr_debug("sec '%s': failed to find name of DATASEC's member #%d\n",
3293 sec_name, i);
b96c07f3
AN
3294 return -ENOENT;
3295 }
3296
f33f742d
AN
3297 sym = find_elf_var_sym(obj, var_name);
3298 if (IS_ERR(sym)) {
3299 pr_debug("sec '%s': failed to find ELF symbol for VAR '%s'\n",
3300 sec_name, var_name);
b96c07f3
AN
3301 return -ENOENT;
3302 }
3303
4fcac46c
AN
3304 if (fixup_offsets)
3305 vsi->offset = sym->st_value;
3306
3307 /* if variable is a global/weak symbol, but has restricted
3308 * (STV_HIDDEN or STV_INTERNAL) visibility, mark its BTF VAR
3309 * as static. This follows similar logic for functions (BPF
3310 * subprogs) and influences libbpf's further decisions about
3311 * whether to make global data BPF array maps as
3312 * BPF_F_MMAPABLE.
3313 */
3314 if (ELF64_ST_VISIBILITY(sym->st_other) == STV_HIDDEN
3315 || ELF64_ST_VISIBILITY(sym->st_other) == STV_INTERNAL)
3316 var->linkage = BTF_VAR_STATIC;
b96c07f3
AN
3317 }
3318
3319sort_vars:
3320 qsort(btf_var_secinfos(t), vars, sizeof(*vsi), compare_vsi_off);
3321 return 0;
3322}
3323
f33f742d 3324static int bpf_object_fixup_btf(struct bpf_object *obj)
b96c07f3 3325{
f33f742d 3326 int i, n, err = 0;
b96c07f3 3327
f33f742d
AN
3328 if (!obj->btf)
3329 return 0;
3330
3331 n = btf__type_cnt(obj->btf);
6a886de0 3332 for (i = 1; i < n; i++) {
f33f742d 3333 struct btf_type *t = btf_type_by_id(obj->btf, i);
b96c07f3
AN
3334
3335 /* Loader needs to fix up some of the things compiler
3336 * couldn't get its hands on while emitting BTF. This
3337 * is section size and global variable offset. We use
3338 * the info from the ELF itself for this purpose.
3339 */
3340 if (btf_is_datasec(t)) {
f33f742d 3341 err = btf_fixup_datasec(obj, obj->btf, t);
b96c07f3 3342 if (err)
f33f742d 3343 return err;
b96c07f3
AN
3344 }
3345 }
3346
166750bc
AN
3347 return 0;
3348}
3349
fe62de31 3350static bool prog_needs_vmlinux_btf(struct bpf_program *prog)
a6ed02ca 3351{
1e092a03
KS
3352 if (prog->type == BPF_PROG_TYPE_STRUCT_OPS ||
3353 prog->type == BPF_PROG_TYPE_LSM)
a6ed02ca
KS
3354 return true;
3355
3356 /* BPF_PROG_TYPE_TRACING programs which do not attach to other programs
3357 * also need vmlinux BTF
3358 */
3359 if (prog->type == BPF_PROG_TYPE_TRACING && !prog->attach_prog_fd)
3360 return true;
3361
3362 return false;
3363}
3364
8b7b0e5f
DV
3365static bool map_needs_vmlinux_btf(struct bpf_map *map)
3366{
3367 return bpf_map__is_struct_ops(map);
3368}
3369
fe62de31 3370static bool obj_needs_vmlinux_btf(const struct bpf_object *obj)
a6ed02ca
KS
3371{
3372 struct bpf_program *prog;
8b7b0e5f 3373 struct bpf_map *map;
fe62de31 3374 int i;
a6ed02ca 3375
1373ff59
SC
3376 /* CO-RE relocations need kernel BTF, only when btf_custom_path
3377 * is not specified
3378 */
3379 if (obj->btf_ext && obj->btf_ext->core_relo_info.len && !obj->btf_custom_path)
fe62de31 3380 return true;
192b6638 3381
d370bbe1
HL
3382 /* Support for typed ksyms needs kernel BTF */
3383 for (i = 0; i < obj->nr_extern; i++) {
3384 const struct extern_desc *ext;
3385
3386 ext = &obj->externs[i];
fe62de31
AN
3387 if (ext->type == EXT_KSYM && ext->ksym.type_id)
3388 return true;
d370bbe1
HL
3389 }
3390
a6ed02ca 3391 bpf_object__for_each_program(prog, obj) {
a3820c48 3392 if (!prog->autoload)
d9297581 3393 continue;
fe62de31
AN
3394 if (prog_needs_vmlinux_btf(prog))
3395 return true;
a6ed02ca
KS
3396 }
3397
8b7b0e5f
DV
3398 bpf_object__for_each_map(map, obj) {
3399 if (map_needs_vmlinux_btf(map))
3400 return true;
3401 }
3402
fe62de31
AN
3403 return false;
3404}
3405
3406static int bpf_object__load_vmlinux_btf(struct bpf_object *obj, bool force)
3407{
3408 int err;
3409
3410 /* btf_vmlinux could be loaded earlier */
67234743 3411 if (obj->btf_vmlinux || obj->gen_loader)
fe62de31
AN
3412 return 0;
3413
3414 if (!force && !obj_needs_vmlinux_btf(obj))
192b6638
AN
3415 return 0;
3416
a710eed3 3417 obj->btf_vmlinux = btf__load_vmlinux_btf();
e9fc3ce9
AN
3418 err = libbpf_get_error(obj->btf_vmlinux);
3419 if (err) {
192b6638
AN
3420 pr_warn("Error loading vmlinux BTF: %d\n", err);
3421 obj->btf_vmlinux = NULL;
3422 return err;
3423 }
a6ed02ca
KS
3424 return 0;
3425}
3426
063183bf
AN
3427static int bpf_object__sanitize_and_load_btf(struct bpf_object *obj)
3428{
0f0e55d8
AN
3429 struct btf *kern_btf = obj->btf;
3430 bool btf_mandatory, sanitize;
aea28a60 3431 int i, err = 0;
063183bf
AN
3432
3433 if (!obj->btf)
3434 return 0;
3435
9ca1f56a 3436 if (!kernel_supports(obj, FEAT_BTF)) {
68b08647
AN
3437 if (kernel_needs_btf(obj)) {
3438 err = -EOPNOTSUPP;
3439 goto report;
3440 }
3441 pr_debug("Kernel doesn't support BTF, skipping uploading it.\n");
3442 return 0;
3443 }
3444
aea28a60
AN
3445 /* Even though some subprogs are global/weak, user might prefer more
3446 * permissive BPF verification process that BPF verifier performs for
3447 * static functions, taking into account more context from the caller
3448 * functions. In such case, they need to mark such subprogs with
3449 * __attribute__((visibility("hidden"))) and libbpf will adjust
3450 * corresponding FUNC BTF type to be marked as static and trigger more
3451 * involved BPF verification process.
3452 */
3453 for (i = 0; i < obj->nr_programs; i++) {
3454 struct bpf_program *prog = &obj->programs[i];
3455 struct btf_type *t;
3456 const char *name;
3457 int j, n;
3458
3459 if (!prog->mark_btf_static || !prog_is_subprog(obj, prog))
3460 continue;
3461
6a886de0
HC
3462 n = btf__type_cnt(obj->btf);
3463 for (j = 1; j < n; j++) {
aea28a60
AN
3464 t = btf_type_by_id(obj->btf, j);
3465 if (!btf_is_func(t) || btf_func_linkage(t) != BTF_FUNC_GLOBAL)
3466 continue;
3467
3468 name = btf__str_by_offset(obj->btf, t->name_off);
3469 if (strcmp(name, prog->name) != 0)
3470 continue;
3471
3472 t->info = btf_type_info(BTF_KIND_FUNC, BTF_FUNC_STATIC, 0);
3473 break;
3474 }
3475 }
3476
0f0e55d8
AN
3477 sanitize = btf_needs_sanitization(obj);
3478 if (sanitize) {
5c3320d7 3479 const void *raw_data;
0f0e55d8 3480 __u32 sz;
063183bf 3481
0f0e55d8 3482 /* clone BTF to sanitize a copy and leave the original intact */
6a886de0 3483 raw_data = btf__raw_data(obj->btf, &sz);
5c3320d7 3484 kern_btf = btf__new(raw_data, sz);
e9fc3ce9
AN
3485 err = libbpf_get_error(kern_btf);
3486 if (err)
3487 return err;
04efe591 3488
4c01925f
AN
3489 /* enforce 8-byte pointers for BPF-targeted BTFs */
3490 btf__set_pointer_size(obj->btf, 8);
f2a62588
YS
3491 err = bpf_object__sanitize_btf(obj, kern_btf);
3492 if (err)
3493 return err;
063183bf 3494 }
0f0e55d8 3495
67234743
AS
3496 if (obj->gen_loader) {
3497 __u32 raw_size = 0;
6a886de0 3498 const void *raw_data = btf__raw_data(kern_btf, &raw_size);
67234743
AS
3499
3500 if (!raw_data)
3501 return -ENOMEM;
3502 bpf_gen__load_btf(obj->gen_loader, raw_data, raw_size);
3503 /* Pretend to have valid FD to pass various fd >= 0 checks.
3504 * This fd == 0 will not be used with any syscall and will be reset to -1 eventually.
3505 */
3506 btf__set_fd(kern_btf, 0);
3507 } else {
e0e3ea88
AN
3508 /* currently BPF_BTF_LOAD only supports log_level 1 */
3509 err = btf_load_into_kernel(kern_btf, obj->log_buf, obj->log_size,
6b434b61 3510 obj->log_level ? 1 : 0, obj->token_fd);
67234743 3511 }
0f0e55d8
AN
3512 if (sanitize) {
3513 if (!err) {
3514 /* move fd to libbpf's BTF */
3515 btf__set_fd(obj->btf, btf__fd(kern_btf));
3516 btf__set_fd(kern_btf, -1);
3517 }
3518 btf__free(kern_btf);
3519 }
68b08647 3520report:
0f0e55d8
AN
3521 if (err) {
3522 btf_mandatory = kernel_needs_btf(obj);
3523 pr_warn("Error loading .BTF into kernel: %d. %s\n", err,
3524 btf_mandatory ? "BTF is mandatory, can't proceed."
3525 : "BTF is optional, ignoring.");
3526 if (!btf_mandatory)
3527 err = 0;
3528 }
3529 return err;
063183bf
AN
3530}
3531
88a82120
AN
3532static const char *elf_sym_str(const struct bpf_object *obj, size_t off)
3533{
3534 const char *name;
3535
3536 name = elf_strptr(obj->efile.elf, obj->efile.strtabidx, off);
3537 if (!name) {
3538 pr_warn("elf: failed to get section name string at offset %zu from %s: %s\n",
3539 off, obj->path, elf_errmsg(-1));
3540 return NULL;
3541 }
3542
3543 return name;
3544}
3545
3546static const char *elf_sec_str(const struct bpf_object *obj, size_t off)
3547{
3548 const char *name;
3549
3550 name = elf_strptr(obj->efile.elf, obj->efile.shstrndx, off);
3551 if (!name) {
3552 pr_warn("elf: failed to get section name string at offset %zu from %s: %s\n",
3553 off, obj->path, elf_errmsg(-1));
3554 return NULL;
3555 }
3556
3557 return name;
3558}
3559
3560static Elf_Scn *elf_sec_by_idx(const struct bpf_object *obj, size_t idx)
3561{
3562 Elf_Scn *scn;
3563
3564 scn = elf_getscn(obj->efile.elf, idx);
3565 if (!scn) {
3566 pr_warn("elf: failed to get section(%zu) from %s: %s\n",
3567 idx, obj->path, elf_errmsg(-1));
3568 return NULL;
3569 }
3570 return scn;
3571}
3572
3573static Elf_Scn *elf_sec_by_name(const struct bpf_object *obj, const char *name)
3574{
3575 Elf_Scn *scn = NULL;
3576 Elf *elf = obj->efile.elf;
3577 const char *sec_name;
3578
3579 while ((scn = elf_nextscn(elf, scn)) != NULL) {
3580 sec_name = elf_sec_name(obj, scn);
3581 if (!sec_name)
3582 return NULL;
3583
3584 if (strcmp(sec_name, name) != 0)
3585 continue;
3586
3587 return scn;
3588 }
3589 return NULL;
3590}
3591
ad23b723 3592static Elf64_Shdr *elf_sec_hdr(const struct bpf_object *obj, Elf_Scn *scn)
88a82120 3593{
ad23b723
AN
3594 Elf64_Shdr *shdr;
3595
88a82120 3596 if (!scn)
ad23b723 3597 return NULL;
88a82120 3598
ad23b723
AN
3599 shdr = elf64_getshdr(scn);
3600 if (!shdr) {
88a82120
AN
3601 pr_warn("elf: failed to get section(%zu) header from %s: %s\n",
3602 elf_ndxscn(scn), obj->path, elf_errmsg(-1));
ad23b723 3603 return NULL;
88a82120
AN
3604 }
3605
ad23b723 3606 return shdr;
88a82120
AN
3607}
3608
3609static const char *elf_sec_name(const struct bpf_object *obj, Elf_Scn *scn)
3610{
3611 const char *name;
ad23b723 3612 Elf64_Shdr *sh;
88a82120
AN
3613
3614 if (!scn)
3615 return NULL;
3616
ad23b723
AN
3617 sh = elf_sec_hdr(obj, scn);
3618 if (!sh)
88a82120
AN
3619 return NULL;
3620
ad23b723 3621 name = elf_sec_str(obj, sh->sh_name);
88a82120
AN
3622 if (!name) {
3623 pr_warn("elf: failed to get section(%zu) name from %s: %s\n",
3624 elf_ndxscn(scn), obj->path, elf_errmsg(-1));
3625 return NULL;
3626 }
3627
3628 return name;
3629}
3630
3631static Elf_Data *elf_sec_data(const struct bpf_object *obj, Elf_Scn *scn)
3632{
3633 Elf_Data *data;
3634
3635 if (!scn)
3636 return NULL;
3637
3638 data = elf_getdata(scn, 0);
3639 if (!data) {
3640 pr_warn("elf: failed to get section(%zu) %s data from %s: %s\n",
3641 elf_ndxscn(scn), elf_sec_name(obj, scn) ?: "<?>",
3642 obj->path, elf_errmsg(-1));
3643 return NULL;
3644 }
3645
3646 return data;
3647}
3648
ad23b723
AN
3649static Elf64_Sym *elf_sym_by_idx(const struct bpf_object *obj, size_t idx)
3650{
3651 if (idx >= obj->efile.symbols->d_size / sizeof(Elf64_Sym))
3652 return NULL;
3653
3654 return (Elf64_Sym *)obj->efile.symbols->d_buf + idx;
3655}
3656
3657static Elf64_Rel *elf_rel_by_idx(Elf_Data *data, size_t idx)
3658{
3659 if (idx >= data->d_size / sizeof(Elf64_Rel))
3660 return NULL;
3661
3662 return (Elf64_Rel *)data->d_buf + idx;
3663}
3664
50e09460
AN
3665static bool is_sec_name_dwarf(const char *name)
3666{
3667 /* approximation, but the actual list is too long */
13d35a0c 3668 return str_has_pfx(name, ".debug_");
50e09460
AN
3669}
3670
ad23b723 3671static bool ignore_elf_section(Elf64_Shdr *hdr, const char *name)
50e09460
AN
3672{
3673 /* no special handling of .strtab */
3674 if (hdr->sh_type == SHT_STRTAB)
3675 return true;
3676
3677 /* ignore .llvm_addrsig section as well */
faf6ed32 3678 if (hdr->sh_type == SHT_LLVM_ADDRSIG)
50e09460
AN
3679 return true;
3680
3681 /* no subprograms will lead to an empty .text section, ignore it */
3682 if (hdr->sh_type == SHT_PROGBITS && hdr->sh_size == 0 &&
3683 strcmp(name, ".text") == 0)
3684 return true;
3685
3686 /* DWARF sections */
3687 if (is_sec_name_dwarf(name))
3688 return true;
3689
13d35a0c 3690 if (str_has_pfx(name, ".rel")) {
50e09460
AN
3691 name += sizeof(".rel") - 1;
3692 /* DWARF section relocations */
3693 if (is_sec_name_dwarf(name))
3694 return true;
3695
3696 /* .BTF and .BTF.ext don't need relocations */
3697 if (strcmp(name, BTF_ELF_SEC) == 0 ||
3698 strcmp(name, BTF_EXT_ELF_SEC) == 0)
3699 return true;
3700 }
3701
3702 return false;
3703}
3704
db2b8b06
AN
3705static int cmp_progs(const void *_a, const void *_b)
3706{
3707 const struct bpf_program *a = _a;
3708 const struct bpf_program *b = _b;
3709
3710 if (a->sec_idx != b->sec_idx)
3711 return a->sec_idx < b->sec_idx ? -1 : 1;
3712
3713 /* sec_insn_off can't be the same within the section */
3714 return a->sec_insn_off < b->sec_insn_off ? -1 : 1;
3715}
3716
0d13bfce 3717static int bpf_object__elf_collect(struct bpf_object *obj)
29603665 3718{
25bbbd7a 3719 struct elf_sec_desc *sec_desc;
29603665 3720 Elf *elf = obj->efile.elf;
f0187f0b 3721 Elf_Data *btf_ext_data = NULL;
1713d68b 3722 Elf_Data *btf_data = NULL;
666810e8 3723 int idx = 0, err = 0;
0201c575
AN
3724 const char *name;
3725 Elf_Data *data;
3726 Elf_Scn *scn;
ad23b723 3727 Elf64_Shdr *sh;
29603665 3728
0d6988e1 3729 /* ELF section indices are 0-based, but sec #0 is special "invalid"
51deedc9
SHY
3730 * section. Since section count retrieved by elf_getshdrnum() does
3731 * include sec #0, it is already the necessary size of an array to keep
3732 * all the sections.
25bbbd7a 3733 */
51deedc9
SHY
3734 if (elf_getshdrnum(obj->efile.elf, &obj->efile.sec_cnt)) {
3735 pr_warn("elf: failed to get the number of sections for %s: %s\n",
3736 obj->path, elf_errmsg(-1));
3737 return -LIBBPF_ERRNO__FORMAT;
3738 }
25bbbd7a
AN
3739 obj->efile.secs = calloc(obj->efile.sec_cnt, sizeof(*obj->efile.secs));
3740 if (!obj->efile.secs)
3741 return -ENOMEM;
29603665 3742
0201c575
AN
3743 /* a bunch of ELF parsing functionality depends on processing symbols,
3744 * so do the first pass and find the symbol table
3745 */
3746 scn = NULL;
29603665 3747 while ((scn = elf_nextscn(elf, scn)) != NULL) {
ad23b723
AN
3748 sh = elf_sec_hdr(obj, scn);
3749 if (!sh)
0201c575
AN
3750 return -LIBBPF_ERRNO__FORMAT;
3751
ad23b723 3752 if (sh->sh_type == SHT_SYMTAB) {
0201c575
AN
3753 if (obj->efile.symbols) {
3754 pr_warn("elf: multiple symbol tables in %s\n", obj->path);
3755 return -LIBBPF_ERRNO__FORMAT;
3756 }
29603665 3757
0201c575
AN
3758 data = elf_sec_data(obj, scn);
3759 if (!data)
3760 return -LIBBPF_ERRNO__FORMAT;
3761
25bbbd7a
AN
3762 idx = elf_ndxscn(scn);
3763
0201c575 3764 obj->efile.symbols = data;
25bbbd7a 3765 obj->efile.symbols_shndx = idx;
ad23b723 3766 obj->efile.strtabidx = sh->sh_link;
0201c575
AN
3767 }
3768 }
3769
03e601f4
THJ
3770 if (!obj->efile.symbols) {
3771 pr_warn("elf: couldn't find symbol table in %s, stripped object file?\n",
3772 obj->path);
3773 return -ENOENT;
3774 }
3775
0201c575
AN
3776 scn = NULL;
3777 while ((scn = elf_nextscn(elf, scn)) != NULL) {
25bbbd7a
AN
3778 idx = elf_ndxscn(scn);
3779 sec_desc = &obj->efile.secs[idx];
88a82120 3780
ad23b723
AN
3781 sh = elf_sec_hdr(obj, scn);
3782 if (!sh)
01b29d1d 3783 return -LIBBPF_ERRNO__FORMAT;
29603665 3784
ad23b723 3785 name = elf_sec_str(obj, sh->sh_name);
88a82120 3786 if (!name)
01b29d1d 3787 return -LIBBPF_ERRNO__FORMAT;
29603665 3788
ad23b723 3789 if (ignore_elf_section(sh, name))
50e09460
AN
3790 continue;
3791
88a82120
AN
3792 data = elf_sec_data(obj, scn);
3793 if (!data)
01b29d1d 3794 return -LIBBPF_ERRNO__FORMAT;
88a82120
AN
3795
3796 pr_debug("elf: section(%d) %s, size %ld, link %d, flags %lx, type=%d\n",
077c066a 3797 idx, name, (unsigned long)data->d_size,
ad23b723
AN
3798 (int)sh->sh_link, (unsigned long)sh->sh_flags,
3799 (int)sh->sh_type);
cb1e5e96 3800
1713d68b 3801 if (strcmp(name, "license") == 0) {
88a82120 3802 err = bpf_object__init_license(obj, data->d_buf, data->d_size);
01b29d1d
AN
3803 if (err)
3804 return err;
1713d68b 3805 } else if (strcmp(name, "version") == 0) {
88a82120 3806 err = bpf_object__init_kversion(obj, data->d_buf, data->d_size);
54b8625c
JF
3807 if (err)
3808 return err;
1713d68b 3809 } else if (strcmp(name, "maps") == 0) {
e19db676
AN
3810 pr_warn("elf: legacy map definitions in 'maps' section are not supported by libbpf v1.0+\n");
3811 return -ENOTSUP;
abd29c93
AN
3812 } else if (strcmp(name, MAPS_ELF_SEC) == 0) {
3813 obj->efile.btf_maps_shndx = idx;
1713d68b 3814 } else if (strcmp(name, BTF_ELF_SEC) == 0) {
62554d52
AN
3815 if (sh->sh_type != SHT_PROGBITS)
3816 return -LIBBPF_ERRNO__FORMAT;
1713d68b 3817 btf_data = data;
2993e051 3818 } else if (strcmp(name, BTF_EXT_ELF_SEC) == 0) {
62554d52
AN
3819 if (sh->sh_type != SHT_PROGBITS)
3820 return -LIBBPF_ERRNO__FORMAT;
f0187f0b 3821 btf_ext_data = data;
ad23b723 3822 } else if (sh->sh_type == SHT_SYMTAB) {
0201c575 3823 /* already processed during the first pass above */
ad23b723
AN
3824 } else if (sh->sh_type == SHT_PROGBITS && data->d_size > 0) {
3825 if (sh->sh_flags & SHF_EXECINSTR) {
f8c7a4d4
JS
3826 if (strcmp(name, ".text") == 0)
3827 obj->efile.text_shndx = idx;
c1122392 3828 err = bpf_object__add_programs(obj, data, name, idx);
88a82120 3829 if (err)
01b29d1d 3830 return err;
aed65917
AN
3831 } else if (strcmp(name, DATA_SEC) == 0 ||
3832 str_has_pfx(name, DATA_SEC ".")) {
25bbbd7a
AN
3833 sec_desc->sec_type = SEC_DATA;
3834 sec_desc->shdr = sh;
3835 sec_desc->data = data;
aed65917
AN
3836 } else if (strcmp(name, RODATA_SEC) == 0 ||
3837 str_has_pfx(name, RODATA_SEC ".")) {
25bbbd7a
AN
3838 sec_desc->sec_type = SEC_RODATA;
3839 sec_desc->shdr = sh;
3840 sec_desc->data = data;
240bf8a5 3841 } else if (strcmp(name, STRUCT_OPS_SEC) == 0 ||
5ad0ecbe
EZ
3842 strcmp(name, STRUCT_OPS_LINK_SEC) == 0 ||
3843 strcmp(name, "?" STRUCT_OPS_SEC) == 0 ||
3844 strcmp(name, "?" STRUCT_OPS_LINK_SEC) == 0) {
240bf8a5
EZ
3845 sec_desc->sec_type = SEC_ST_OPS;
3846 sec_desc->shdr = sh;
3847 sec_desc->data = data;
3848 obj->efile.has_st_ops = true;
2e7ba4f8
AN
3849 } else if (strcmp(name, ARENA_SEC) == 0) {
3850 obj->efile.arena_data = data;
3851 obj->efile.arena_data_shndx = idx;
d859900c 3852 } else {
50e09460
AN
3853 pr_info("elf: skipping unrecognized data section(%d) %s\n",
3854 idx, name);
a5b8bd47 3855 }
ad23b723 3856 } else if (sh->sh_type == SHT_REL) {
25bbbd7a 3857 int targ_sec_idx = sh->sh_info; /* points to other section */
e3d91b0c 3858
b7332d28
AN
3859 if (sh->sh_entsize != sizeof(Elf64_Rel) ||
3860 targ_sec_idx >= obj->efile.sec_cnt)
3861 return -LIBBPF_ERRNO__FORMAT;
3862
e3d91b0c 3863 /* Only do relo for section with exec instructions */
25bbbd7a 3864 if (!section_have_execinstr(obj, targ_sec_idx) &&
646f02ff 3865 strcmp(name, ".rel" STRUCT_OPS_SEC) &&
809a69d6 3866 strcmp(name, ".rel" STRUCT_OPS_LINK_SEC) &&
5ad0ecbe
EZ
3867 strcmp(name, ".rel?" STRUCT_OPS_SEC) &&
3868 strcmp(name, ".rel?" STRUCT_OPS_LINK_SEC) &&
646f02ff 3869 strcmp(name, ".rel" MAPS_ELF_SEC)) {
50e09460 3870 pr_info("elf: skipping relo section(%d) %s for section(%d) %s\n",
25bbbd7a
AN
3871 idx, name, targ_sec_idx,
3872 elf_sec_name(obj, elf_sec_by_idx(obj, targ_sec_idx)) ?: "<?>");
e3d91b0c
JDB
3873 continue;
3874 }
b62f06e8 3875
25bbbd7a
AN
3876 sec_desc->sec_type = SEC_RELO;
3877 sec_desc->shdr = sh;
3878 sec_desc->data = data;
dc79f035
AS
3879 } else if (sh->sh_type == SHT_NOBITS && (strcmp(name, BSS_SEC) == 0 ||
3880 str_has_pfx(name, BSS_SEC "."))) {
25bbbd7a
AN
3881 sec_desc->sec_type = SEC_BSS;
3882 sec_desc->shdr = sh;
3883 sec_desc->data = data;
077c066a 3884 } else {
2e80be60 3885 pr_info("elf: skipping section(%d) %s (size %zu)\n", idx, name,
ad23b723 3886 (size_t)sh->sh_size);
bec7d68c 3887 }
29603665 3888 }
561bbcca 3889
d3a3aa0c 3890 if (!obj->efile.strtabidx || obj->efile.strtabidx > idx) {
88a82120 3891 pr_warn("elf: symbol strings section missing or invalid in %s\n", obj->path);
f102154d 3892 return -LIBBPF_ERRNO__FORMAT;
77ba9a5b 3893 }
db2b8b06
AN
3894
3895 /* sort BPF programs by section name and in-section instruction offset
e3ba8e4e
KM
3896 * for faster search
3897 */
2a6a9bf2
AN
3898 if (obj->nr_programs)
3899 qsort(obj->programs, obj->nr_programs, sizeof(*obj->programs), cmp_progs);
db2b8b06 3900
0d13bfce 3901 return bpf_object__init_btf(obj, btf_data, btf_ext_data);
29603665
WN
3902}
3903
ad23b723 3904static bool sym_is_extern(const Elf64_Sym *sym)
166750bc 3905{
ad23b723 3906 int bind = ELF64_ST_BIND(sym->st_info);
166750bc
AN
3907 /* externs are symbols w/ type=NOTYPE, bind=GLOBAL|WEAK, section=UND */
3908 return sym->st_shndx == SHN_UNDEF &&
3909 (bind == STB_GLOBAL || bind == STB_WEAK) &&
ad23b723 3910 ELF64_ST_TYPE(sym->st_info) == STT_NOTYPE;
166750bc
AN
3911}
3912
ad23b723 3913static bool sym_is_subprog(const Elf64_Sym *sym, int text_shndx)
53eddb5e 3914{
ad23b723
AN
3915 int bind = ELF64_ST_BIND(sym->st_info);
3916 int type = ELF64_ST_TYPE(sym->st_info);
53eddb5e
YS
3917
3918 /* in .text section */
3919 if (sym->st_shndx != text_shndx)
3920 return false;
3921
3922 /* local function */
3923 if (bind == STB_LOCAL && type == STT_SECTION)
3924 return true;
3925
3926 /* global function */
3927 return bind == STB_GLOBAL && type == STT_FUNC;
3928}
3929
166750bc
AN
3930static int find_extern_btf_id(const struct btf *btf, const char *ext_name)
3931{
3932 const struct btf_type *t;
5bd022ec 3933 const char *tname;
166750bc
AN
3934 int i, n;
3935
3936 if (!btf)
3937 return -ESRCH;
3938
6a886de0
HC
3939 n = btf__type_cnt(btf);
3940 for (i = 1; i < n; i++) {
166750bc
AN
3941 t = btf__type_by_id(btf, i);
3942
5bd022ec 3943 if (!btf_is_var(t) && !btf_is_func(t))
166750bc
AN
3944 continue;
3945
5bd022ec
MKL
3946 tname = btf__name_by_offset(btf, t->name_off);
3947 if (strcmp(tname, ext_name))
166750bc
AN
3948 continue;
3949
5bd022ec
MKL
3950 if (btf_is_var(t) &&
3951 btf_var(t)->linkage != BTF_VAR_GLOBAL_EXTERN)
3952 return -EINVAL;
3953
3954 if (btf_is_func(t) && btf_func_linkage(t) != BTF_FUNC_EXTERN)
166750bc
AN
3955 return -EINVAL;
3956
3957 return i;
3958 }
3959
3960 return -ENOENT;
3961}
3962
2e33efe3
AN
3963static int find_extern_sec_btf_id(struct btf *btf, int ext_btf_id) {
3964 const struct btf_var_secinfo *vs;
3965 const struct btf_type *t;
3966 int i, j, n;
3967
3968 if (!btf)
3969 return -ESRCH;
3970
6a886de0
HC
3971 n = btf__type_cnt(btf);
3972 for (i = 1; i < n; i++) {
2e33efe3
AN
3973 t = btf__type_by_id(btf, i);
3974
3975 if (!btf_is_datasec(t))
3976 continue;
3977
3978 vs = btf_var_secinfos(t);
3979 for (j = 0; j < btf_vlen(t); j++, vs++) {
3980 if (vs->type == ext_btf_id)
3981 return i;
3982 }
3983 }
3984
3985 return -ENOENT;
3986}
3987
3988static enum kcfg_type find_kcfg_type(const struct btf *btf, int id,
3989 bool *is_signed)
166750bc
AN
3990{
3991 const struct btf_type *t;
3992 const char *name;
3993
3994 t = skip_mods_and_typedefs(btf, id, NULL);
3995 name = btf__name_by_offset(btf, t->name_off);
3996
3997 if (is_signed)
3998 *is_signed = false;
3999 switch (btf_kind(t)) {
4000 case BTF_KIND_INT: {
4001 int enc = btf_int_encoding(t);
4002
4003 if (enc & BTF_INT_BOOL)
2e33efe3 4004 return t->size == 1 ? KCFG_BOOL : KCFG_UNKNOWN;
166750bc
AN
4005 if (is_signed)
4006 *is_signed = enc & BTF_INT_SIGNED;
4007 if (t->size == 1)
2e33efe3 4008 return KCFG_CHAR;
166750bc 4009 if (t->size < 1 || t->size > 8 || (t->size & (t->size - 1)))
2e33efe3
AN
4010 return KCFG_UNKNOWN;
4011 return KCFG_INT;
166750bc
AN
4012 }
4013 case BTF_KIND_ENUM:
4014 if (t->size != 4)
2e33efe3 4015 return KCFG_UNKNOWN;
166750bc 4016 if (strcmp(name, "libbpf_tristate"))
2e33efe3
AN
4017 return KCFG_UNKNOWN;
4018 return KCFG_TRISTATE;
f2a62588
YS
4019 case BTF_KIND_ENUM64:
4020 if (strcmp(name, "libbpf_tristate"))
4021 return KCFG_UNKNOWN;
4022 return KCFG_TRISTATE;
166750bc
AN
4023 case BTF_KIND_ARRAY:
4024 if (btf_array(t)->nelems == 0)
2e33efe3
AN
4025 return KCFG_UNKNOWN;
4026 if (find_kcfg_type(btf, btf_array(t)->type, NULL) != KCFG_CHAR)
4027 return KCFG_UNKNOWN;
4028 return KCFG_CHAR_ARR;
166750bc 4029 default:
2e33efe3 4030 return KCFG_UNKNOWN;
166750bc
AN
4031 }
4032}
4033
4034static int cmp_externs(const void *_a, const void *_b)
4035{
4036 const struct extern_desc *a = _a;
4037 const struct extern_desc *b = _b;
4038
2e33efe3
AN
4039 if (a->type != b->type)
4040 return a->type < b->type ? -1 : 1;
4041
4042 if (a->type == EXT_KCFG) {
4043 /* descending order by alignment requirements */
4044 if (a->kcfg.align != b->kcfg.align)
4045 return a->kcfg.align > b->kcfg.align ? -1 : 1;
4046 /* ascending order by size, within same alignment class */
4047 if (a->kcfg.sz != b->kcfg.sz)
4048 return a->kcfg.sz < b->kcfg.sz ? -1 : 1;
4049 }
4050
166750bc
AN
4051 /* resolve ties by name */
4052 return strcmp(a->name, b->name);
4053}
4054
1c0c7074
AN
4055static int find_int_btf_id(const struct btf *btf)
4056{
4057 const struct btf_type *t;
4058 int i, n;
4059
6a886de0
HC
4060 n = btf__type_cnt(btf);
4061 for (i = 1; i < n; i++) {
1c0c7074
AN
4062 t = btf__type_by_id(btf, i);
4063
4064 if (btf_is_int(t) && btf_int_bits(t) == 32)
4065 return i;
4066 }
4067
4068 return 0;
4069}
4070
5bd022ec
MKL
4071static int add_dummy_ksym_var(struct btf *btf)
4072{
4073 int i, int_btf_id, sec_btf_id, dummy_var_btf_id;
4074 const struct btf_var_secinfo *vs;
4075 const struct btf_type *sec;
4076
9683e577
IR
4077 if (!btf)
4078 return 0;
4079
5bd022ec
MKL
4080 sec_btf_id = btf__find_by_name_kind(btf, KSYMS_SEC,
4081 BTF_KIND_DATASEC);
4082 if (sec_btf_id < 0)
4083 return 0;
4084
4085 sec = btf__type_by_id(btf, sec_btf_id);
4086 vs = btf_var_secinfos(sec);
4087 for (i = 0; i < btf_vlen(sec); i++, vs++) {
4088 const struct btf_type *vt;
4089
4090 vt = btf__type_by_id(btf, vs->type);
4091 if (btf_is_func(vt))
4092 break;
4093 }
4094
4095 /* No func in ksyms sec. No need to add dummy var. */
4096 if (i == btf_vlen(sec))
4097 return 0;
4098
4099 int_btf_id = find_int_btf_id(btf);
4100 dummy_var_btf_id = btf__add_var(btf,
4101 "dummy_ksym",
4102 BTF_VAR_GLOBAL_ALLOCATED,
4103 int_btf_id);
4104 if (dummy_var_btf_id < 0)
4105 pr_warn("cannot create a dummy_ksym var\n");
4106
4107 return dummy_var_btf_id;
4108}
4109
166750bc
AN
4110static int bpf_object__collect_externs(struct bpf_object *obj)
4111{
1c0c7074 4112 struct btf_type *sec, *kcfg_sec = NULL, *ksym_sec = NULL;
166750bc
AN
4113 const struct btf_type *t;
4114 struct extern_desc *ext;
5bd022ec 4115 int i, n, off, dummy_var_btf_id;
2e33efe3 4116 const char *ext_name, *sec_name;
5964a223 4117 size_t ext_essent_len;
166750bc 4118 Elf_Scn *scn;
ad23b723 4119 Elf64_Shdr *sh;
166750bc
AN
4120
4121 if (!obj->efile.symbols)
4122 return 0;
4123
88a82120 4124 scn = elf_sec_by_idx(obj, obj->efile.symbols_shndx);
ad23b723 4125 sh = elf_sec_hdr(obj, scn);
83390787 4126 if (!sh || sh->sh_entsize != sizeof(Elf64_Sym))
166750bc 4127 return -LIBBPF_ERRNO__FORMAT;
166750bc 4128
5bd022ec
MKL
4129 dummy_var_btf_id = add_dummy_ksym_var(obj->btf);
4130 if (dummy_var_btf_id < 0)
4131 return dummy_var_btf_id;
4132
ad23b723 4133 n = sh->sh_size / sh->sh_entsize;
166750bc 4134 pr_debug("looking for externs among %d symbols...\n", n);
88a82120 4135
166750bc 4136 for (i = 0; i < n; i++) {
ad23b723 4137 Elf64_Sym *sym = elf_sym_by_idx(obj, i);
166750bc 4138
ad23b723 4139 if (!sym)
166750bc 4140 return -LIBBPF_ERRNO__FORMAT;
ad23b723 4141 if (!sym_is_extern(sym))
166750bc 4142 continue;
ad23b723 4143 ext_name = elf_sym_str(obj, sym->st_name);
166750bc
AN
4144 if (!ext_name || !ext_name[0])
4145 continue;
4146
4147 ext = obj->externs;
029258d7 4148 ext = libbpf_reallocarray(ext, obj->nr_extern + 1, sizeof(*ext));
166750bc
AN
4149 if (!ext)
4150 return -ENOMEM;
4151 obj->externs = ext;
4152 ext = &ext[obj->nr_extern];
4153 memset(ext, 0, sizeof(*ext));
4154 obj->nr_extern++;
4155
4156 ext->btf_id = find_extern_btf_id(obj->btf, ext_name);
4157 if (ext->btf_id <= 0) {
4158 pr_warn("failed to find BTF for extern '%s': %d\n",
4159 ext_name, ext->btf_id);
4160 return ext->btf_id;
4161 }
4162 t = btf__type_by_id(obj->btf, ext->btf_id);
4163 ext->name = btf__name_by_offset(obj->btf, t->name_off);
4164 ext->sym_idx = i;
ad23b723 4165 ext->is_weak = ELF64_ST_BIND(sym->st_info) == STB_WEAK;
2e33efe3 4166
5964a223
DM
4167 ext_essent_len = bpf_core_essential_name_len(ext->name);
4168 ext->essent_name = NULL;
4169 if (ext_essent_len != strlen(ext->name)) {
4170 ext->essent_name = strndup(ext->name, ext_essent_len);
4171 if (!ext->essent_name)
4172 return -ENOMEM;
4173 }
4174
2e33efe3
AN
4175 ext->sec_btf_id = find_extern_sec_btf_id(obj->btf, ext->btf_id);
4176 if (ext->sec_btf_id <= 0) {
4177 pr_warn("failed to find BTF for extern '%s' [%d] section: %d\n",
4178 ext_name, ext->btf_id, ext->sec_btf_id);
4179 return ext->sec_btf_id;
166750bc 4180 }
2e33efe3
AN
4181 sec = (void *)btf__type_by_id(obj->btf, ext->sec_btf_id);
4182 sec_name = btf__name_by_offset(obj->btf, sec->name_off);
4183
4184 if (strcmp(sec_name, KCONFIG_SEC) == 0) {
5bd022ec
MKL
4185 if (btf_is_func(t)) {
4186 pr_warn("extern function %s is unsupported under %s section\n",
4187 ext->name, KCONFIG_SEC);
4188 return -ENOTSUP;
4189 }
2e33efe3
AN
4190 kcfg_sec = sec;
4191 ext->type = EXT_KCFG;
4192 ext->kcfg.sz = btf__resolve_size(obj->btf, t->type);
4193 if (ext->kcfg.sz <= 0) {
4194 pr_warn("failed to resolve size of extern (kcfg) '%s': %d\n",
4195 ext_name, ext->kcfg.sz);
4196 return ext->kcfg.sz;
4197 }
4198 ext->kcfg.align = btf__align_of(obj->btf, t->type);
4199 if (ext->kcfg.align <= 0) {
4200 pr_warn("failed to determine alignment of extern (kcfg) '%s': %d\n",
4201 ext_name, ext->kcfg.align);
4202 return -EINVAL;
4203 }
4204 ext->kcfg.type = find_kcfg_type(obj->btf, t->type,
e3ba8e4e 4205 &ext->kcfg.is_signed);
2e33efe3 4206 if (ext->kcfg.type == KCFG_UNKNOWN) {
55d00c37 4207 pr_warn("extern (kcfg) '%s': type is unsupported\n", ext_name);
2e33efe3
AN
4208 return -ENOTSUP;
4209 }
1c0c7074 4210 } else if (strcmp(sec_name, KSYMS_SEC) == 0) {
1c0c7074
AN
4211 ksym_sec = sec;
4212 ext->type = EXT_KSYM;
d370bbe1
HL
4213 skip_mods_and_typedefs(obj->btf, t->type,
4214 &ext->ksym.type_id);
2e33efe3
AN
4215 } else {
4216 pr_warn("unrecognized extern section '%s'\n", sec_name);
166750bc
AN
4217 return -ENOTSUP;
4218 }
4219 }
4220 pr_debug("collected %d externs total\n", obj->nr_extern);
4221
4222 if (!obj->nr_extern)
4223 return 0;
4224
2e33efe3 4225 /* sort externs by type, for kcfg ones also by (align, size, name) */
166750bc 4226 qsort(obj->externs, obj->nr_extern, sizeof(*ext), cmp_externs);
166750bc 4227
1c0c7074
AN
4228 /* for .ksyms section, we need to turn all externs into allocated
4229 * variables in BTF to pass kernel verification; we do this by
4230 * pretending that each extern is a 8-byte variable
4231 */
4232 if (ksym_sec) {
4233 /* find existing 4-byte integer type in BTF to use for fake
4234 * extern variables in DATASEC
4235 */
4236 int int_btf_id = find_int_btf_id(obj->btf);
5bd022ec
MKL
4237 /* For extern function, a dummy_var added earlier
4238 * will be used to replace the vs->type and
4239 * its name string will be used to refill
4240 * the missing param's name.
4241 */
4242 const struct btf_type *dummy_var;
1c0c7074 4243
5bd022ec 4244 dummy_var = btf__type_by_id(obj->btf, dummy_var_btf_id);
1c0c7074
AN
4245 for (i = 0; i < obj->nr_extern; i++) {
4246 ext = &obj->externs[i];
4247 if (ext->type != EXT_KSYM)
4248 continue;
4249 pr_debug("extern (ksym) #%d: symbol %d, name %s\n",
4250 i, ext->sym_idx, ext->name);
4251 }
4252
4253 sec = ksym_sec;
4254 n = btf_vlen(sec);
4255 for (i = 0, off = 0; i < n; i++, off += sizeof(int)) {
4256 struct btf_var_secinfo *vs = btf_var_secinfos(sec) + i;
4257 struct btf_type *vt;
4258
4259 vt = (void *)btf__type_by_id(obj->btf, vs->type);
4260 ext_name = btf__name_by_offset(obj->btf, vt->name_off);
4261 ext = find_extern_by_name(obj, ext_name);
4262 if (!ext) {
5bd022ec
MKL
4263 pr_warn("failed to find extern definition for BTF %s '%s'\n",
4264 btf_kind_str(vt), ext_name);
1c0c7074
AN
4265 return -ESRCH;
4266 }
5bd022ec
MKL
4267 if (btf_is_func(vt)) {
4268 const struct btf_type *func_proto;
4269 struct btf_param *param;
4270 int j;
4271
4272 func_proto = btf__type_by_id(obj->btf,
4273 vt->type);
4274 param = btf_params(func_proto);
4275 /* Reuse the dummy_var string if the
4276 * func proto does not have param name.
4277 */
4278 for (j = 0; j < btf_vlen(func_proto); j++)
4279 if (param[j].type && !param[j].name_off)
4280 param[j].name_off =
4281 dummy_var->name_off;
4282 vs->type = dummy_var_btf_id;
4283 vt->info &= ~0xffff;
4284 vt->info |= BTF_FUNC_GLOBAL;
4285 } else {
4286 btf_var(vt)->linkage = BTF_VAR_GLOBAL_ALLOCATED;
4287 vt->type = int_btf_id;
4288 }
1c0c7074
AN
4289 vs->offset = off;
4290 vs->size = sizeof(int);
4291 }
4292 sec->size = off;
4293 }
4294
2e33efe3
AN
4295 if (kcfg_sec) {
4296 sec = kcfg_sec;
4297 /* for kcfg externs calculate their offsets within a .kconfig map */
4298 off = 0;
4299 for (i = 0; i < obj->nr_extern; i++) {
4300 ext = &obj->externs[i];
4301 if (ext->type != EXT_KCFG)
4302 continue;
166750bc 4303
2e33efe3
AN
4304 ext->kcfg.data_off = roundup(off, ext->kcfg.align);
4305 off = ext->kcfg.data_off + ext->kcfg.sz;
1c0c7074 4306 pr_debug("extern (kcfg) #%d: symbol %d, off %u, name %s\n",
2e33efe3
AN
4307 i, ext->sym_idx, ext->kcfg.data_off, ext->name);
4308 }
4309 sec->size = off;
4310 n = btf_vlen(sec);
4311 for (i = 0; i < n; i++) {
4312 struct btf_var_secinfo *vs = btf_var_secinfos(sec) + i;
4313
4314 t = btf__type_by_id(obj->btf, vs->type);
4315 ext_name = btf__name_by_offset(obj->btf, t->name_off);
4316 ext = find_extern_by_name(obj, ext_name);
4317 if (!ext) {
4318 pr_warn("failed to find extern definition for BTF var '%s'\n",
4319 ext_name);
4320 return -ESRCH;
4321 }
4322 btf_var(t)->linkage = BTF_VAR_GLOBAL_ALLOCATED;
4323 vs->offset = ext->kcfg.data_off;
166750bc 4324 }
166750bc 4325 }
166750bc
AN
4326 return 0;
4327}
4328
bd054102
AN
4329static bool prog_is_subprog(const struct bpf_object *obj, const struct bpf_program *prog)
4330{
197afc63 4331 return prog->sec_idx == obj->efile.text_shndx && obj->nr_programs > 1;
c3c55696
AN
4332}
4333
01af3bf0
AN
4334struct bpf_program *
4335bpf_object__find_program_by_name(const struct bpf_object *obj,
4336 const char *name)
4337{
4338 struct bpf_program *prog;
4339
4340 bpf_object__for_each_program(prog, obj) {
c3c55696
AN
4341 if (prog_is_subprog(obj, prog))
4342 continue;
01af3bf0
AN
4343 if (!strcmp(prog->name, name))
4344 return prog;
4345 }
e9fc3ce9 4346 return errno = ENOENT, NULL;
01af3bf0
AN
4347}
4348
d859900c
DB
4349static bool bpf_object__shndx_is_data(const struct bpf_object *obj,
4350 int shndx)
4351{
25bbbd7a
AN
4352 switch (obj->efile.secs[shndx].sec_type) {
4353 case SEC_BSS:
4354 case SEC_DATA:
4355 case SEC_RODATA:
4356 return true;
4357 default:
4358 return false;
4359 }
d859900c
DB
4360}
4361
4362static bool bpf_object__shndx_is_maps(const struct bpf_object *obj,
4363 int shndx)
4364{
e19db676 4365 return shndx == obj->efile.btf_maps_shndx;
d859900c
DB
4366}
4367
d859900c
DB
4368static enum libbpf_map_type
4369bpf_object__section_to_libbpf_map_type(const struct bpf_object *obj, int shndx)
4370{
25bbbd7a
AN
4371 if (shndx == obj->efile.symbols_shndx)
4372 return LIBBPF_MAP_KCONFIG;
4373
4374 switch (obj->efile.secs[shndx].sec_type) {
4375 case SEC_BSS:
d859900c 4376 return LIBBPF_MAP_BSS;
25bbbd7a
AN
4377 case SEC_DATA:
4378 return LIBBPF_MAP_DATA;
4379 case SEC_RODATA:
d859900c 4380 return LIBBPF_MAP_RODATA;
25bbbd7a 4381 default:
d859900c 4382 return LIBBPF_MAP_UNSPEC;
25bbbd7a 4383 }
d859900c
DB
4384}
4385
1f8e2bcb
AN
4386static int bpf_program__record_reloc(struct bpf_program *prog,
4387 struct reloc_desc *reloc_desc,
9c0f8cbd 4388 __u32 insn_idx, const char *sym_name,
ad23b723 4389 const Elf64_Sym *sym, const Elf64_Rel *rel)
1f8e2bcb
AN
4390{
4391 struct bpf_insn *insn = &prog->insns[insn_idx];
4392 size_t map_idx, nr_maps = prog->obj->nr_maps;
4393 struct bpf_object *obj = prog->obj;
4394 __u32 shdr_idx = sym->st_shndx;
4395 enum libbpf_map_type type;
9c0f8cbd 4396 const char *sym_sec_name;
1f8e2bcb
AN
4397 struct bpf_map *map;
4398
aa0b8d43 4399 if (!is_call_insn(insn) && !is_ldimm64_insn(insn)) {
9c0f8cbd
AN
4400 pr_warn("prog '%s': invalid relo against '%s' for insns[%d].code 0x%x\n",
4401 prog->name, sym_name, insn_idx, insn->code);
1f8e2bcb
AN
4402 return -LIBBPF_ERRNO__RELOC;
4403 }
166750bc
AN
4404
4405 if (sym_is_extern(sym)) {
ad23b723 4406 int sym_idx = ELF64_R_SYM(rel->r_info);
166750bc
AN
4407 int i, n = obj->nr_extern;
4408 struct extern_desc *ext;
4409
4410 for (i = 0; i < n; i++) {
4411 ext = &obj->externs[i];
4412 if (ext->sym_idx == sym_idx)
4413 break;
4414 }
4415 if (i >= n) {
9c0f8cbd
AN
4416 pr_warn("prog '%s': extern relo failed to find extern for '%s' (%d)\n",
4417 prog->name, sym_name, sym_idx);
166750bc
AN
4418 return -LIBBPF_ERRNO__RELOC;
4419 }
9c0f8cbd
AN
4420 pr_debug("prog '%s': found extern #%d '%s' (sym %d) for insn #%u\n",
4421 prog->name, i, ext->name, ext->sym_idx, insn_idx);
5bd022ec 4422 if (insn->code == (BPF_JMP | BPF_CALL))
a18f7214 4423 reloc_desc->type = RELO_EXTERN_CALL;
5bd022ec 4424 else
a18f7214 4425 reloc_desc->type = RELO_EXTERN_LD64;
166750bc 4426 reloc_desc->insn_idx = insn_idx;
3055ddd6 4427 reloc_desc->ext_idx = i;
166750bc
AN
4428 return 0;
4429 }
4430
aa0b8d43
MKL
4431 /* sub-program call relocation */
4432 if (is_call_insn(insn)) {
4433 if (insn->src_reg != BPF_PSEUDO_CALL) {
4434 pr_warn("prog '%s': incorrect bpf_call opcode\n", prog->name);
4435 return -LIBBPF_ERRNO__RELOC;
4436 }
4437 /* text_shndx can be 0, if no default "main" program exists */
4438 if (!shdr_idx || shdr_idx != obj->efile.text_shndx) {
4439 sym_sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, shdr_idx));
4440 pr_warn("prog '%s': bad call relo against '%s' in section '%s'\n",
4441 prog->name, sym_name, sym_sec_name);
4442 return -LIBBPF_ERRNO__RELOC;
4443 }
4444 if (sym->st_value % BPF_INSN_SZ) {
4445 pr_warn("prog '%s': bad call relo against '%s' at offset %zu\n",
4446 prog->name, sym_name, (size_t)sym->st_value);
4447 return -LIBBPF_ERRNO__RELOC;
4448 }
4449 reloc_desc->type = RELO_CALL;
4450 reloc_desc->insn_idx = insn_idx;
4451 reloc_desc->sym_off = sym->st_value;
4452 return 0;
4453 }
4454
1f8e2bcb 4455 if (!shdr_idx || shdr_idx >= SHN_LORESERVE) {
9c0f8cbd
AN
4456 pr_warn("prog '%s': invalid relo against '%s' in special section 0x%x; forgot to initialize global var?..\n",
4457 prog->name, sym_name, shdr_idx);
1f8e2bcb
AN
4458 return -LIBBPF_ERRNO__RELOC;
4459 }
4460
53eddb5e
YS
4461 /* loading subprog addresses */
4462 if (sym_is_subprog(sym, obj->efile.text_shndx)) {
4463 /* global_func: sym->st_value = offset in the section, insn->imm = 0.
4464 * local_func: sym->st_value = 0, insn->imm = offset in the section.
4465 */
4466 if ((sym->st_value % BPF_INSN_SZ) || (insn->imm % BPF_INSN_SZ)) {
4467 pr_warn("prog '%s': bad subprog addr relo against '%s' at offset %zu+%d\n",
4468 prog->name, sym_name, (size_t)sym->st_value, insn->imm);
4469 return -LIBBPF_ERRNO__RELOC;
4470 }
4471
4472 reloc_desc->type = RELO_SUBPROG_ADDR;
4473 reloc_desc->insn_idx = insn_idx;
4474 reloc_desc->sym_off = sym->st_value;
4475 return 0;
4476 }
4477
1f8e2bcb 4478 type = bpf_object__section_to_libbpf_map_type(obj, shdr_idx);
9c0f8cbd 4479 sym_sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, shdr_idx));
1f8e2bcb 4480
2e7ba4f8
AN
4481 /* arena data relocation */
4482 if (shdr_idx == obj->efile.arena_data_shndx) {
4483 reloc_desc->type = RELO_DATA;
4484 reloc_desc->insn_idx = insn_idx;
4485 reloc_desc->map_idx = obj->arena_map - obj->maps;
4486 reloc_desc->sym_off = sym->st_value;
4487 return 0;
4488 }
4489
1f8e2bcb
AN
4490 /* generic map reference relocation */
4491 if (type == LIBBPF_MAP_UNSPEC) {
4492 if (!bpf_object__shndx_is_maps(obj, shdr_idx)) {
9c0f8cbd
AN
4493 pr_warn("prog '%s': bad map relo against '%s' in section '%s'\n",
4494 prog->name, sym_name, sym_sec_name);
1f8e2bcb
AN
4495 return -LIBBPF_ERRNO__RELOC;
4496 }
4497 for (map_idx = 0; map_idx < nr_maps; map_idx++) {
4498 map = &obj->maps[map_idx];
4499 if (map->libbpf_type != type ||
4500 map->sec_idx != sym->st_shndx ||
4501 map->sec_offset != sym->st_value)
4502 continue;
9c0f8cbd
AN
4503 pr_debug("prog '%s': found map %zd (%s, sec %d, off %zu) for insn #%u\n",
4504 prog->name, map_idx, map->name, map->sec_idx,
1f8e2bcb
AN
4505 map->sec_offset, insn_idx);
4506 break;
4507 }
4508 if (map_idx >= nr_maps) {
9c0f8cbd
AN
4509 pr_warn("prog '%s': map relo failed to find map for section '%s', off %zu\n",
4510 prog->name, sym_sec_name, (size_t)sym->st_value);
1f8e2bcb
AN
4511 return -LIBBPF_ERRNO__RELOC;
4512 }
4513 reloc_desc->type = RELO_LD64;
4514 reloc_desc->insn_idx = insn_idx;
4515 reloc_desc->map_idx = map_idx;
53f8dd43 4516 reloc_desc->sym_off = 0; /* sym->st_value determines map_idx */
1f8e2bcb
AN
4517 return 0;
4518 }
4519
4520 /* global data map relocation */
4521 if (!bpf_object__shndx_is_data(obj, shdr_idx)) {
9c0f8cbd
AN
4522 pr_warn("prog '%s': bad data relo against section '%s'\n",
4523 prog->name, sym_sec_name);
1f8e2bcb 4524 return -LIBBPF_ERRNO__RELOC;
1f8e2bcb 4525 }
1f8e2bcb
AN
4526 for (map_idx = 0; map_idx < nr_maps; map_idx++) {
4527 map = &obj->maps[map_idx];
25bbbd7a 4528 if (map->libbpf_type != type || map->sec_idx != sym->st_shndx)
1f8e2bcb 4529 continue;
9c0f8cbd
AN
4530 pr_debug("prog '%s': found data map %zd (%s, sec %d, off %zu) for insn %u\n",
4531 prog->name, map_idx, map->name, map->sec_idx,
4532 map->sec_offset, insn_idx);
1f8e2bcb
AN
4533 break;
4534 }
4535 if (map_idx >= nr_maps) {
9c0f8cbd
AN
4536 pr_warn("prog '%s': data relo failed to find map for section '%s'\n",
4537 prog->name, sym_sec_name);
1f8e2bcb
AN
4538 return -LIBBPF_ERRNO__RELOC;
4539 }
4540
4541 reloc_desc->type = RELO_DATA;
4542 reloc_desc->insn_idx = insn_idx;
4543 reloc_desc->map_idx = map_idx;
53f8dd43 4544 reloc_desc->sym_off = sym->st_value;
1f8e2bcb
AN
4545 return 0;
4546}
4547
db2b8b06
AN
4548static bool prog_contains_insn(const struct bpf_program *prog, size_t insn_idx)
4549{
4550 return insn_idx >= prog->sec_insn_off &&
4551 insn_idx < prog->sec_insn_off + prog->sec_insn_cnt;
4552}
4553
4554static struct bpf_program *find_prog_by_sec_insn(const struct bpf_object *obj,
4555 size_t sec_idx, size_t insn_idx)
4556{
4557 int l = 0, r = obj->nr_programs - 1, m;
4558 struct bpf_program *prog;
4559
d0d382f9
SHY
4560 if (!obj->nr_programs)
4561 return NULL;
4562
db2b8b06
AN
4563 while (l < r) {
4564 m = l + (r - l + 1) / 2;
4565 prog = &obj->programs[m];
4566
4567 if (prog->sec_idx < sec_idx ||
4568 (prog->sec_idx == sec_idx && prog->sec_insn_off <= insn_idx))
4569 l = m;
4570 else
4571 r = m - 1;
4572 }
4573 /* matching program could be at index l, but it still might be the
4574 * wrong one, so we need to double check conditions for the last time
4575 */
4576 prog = &obj->programs[l];
4577 if (prog->sec_idx == sec_idx && prog_contains_insn(prog, insn_idx))
4578 return prog;
4579 return NULL;
4580}
4581
34090915 4582static int
ad23b723 4583bpf_object__collect_prog_relos(struct bpf_object *obj, Elf64_Shdr *shdr, Elf_Data *data)
34090915 4584{
9c0f8cbd 4585 const char *relo_sec_name, *sec_name;
b7332d28 4586 size_t sec_idx = shdr->sh_info, sym_idx;
c3c55696
AN
4587 struct bpf_program *prog;
4588 struct reloc_desc *relos;
1f8e2bcb 4589 int err, i, nrels;
c3c55696
AN
4590 const char *sym_name;
4591 __u32 insn_idx;
6245947c
AN
4592 Elf_Scn *scn;
4593 Elf_Data *scn_data;
ad23b723
AN
4594 Elf64_Sym *sym;
4595 Elf64_Rel *rel;
34090915 4596
b7332d28
AN
4597 if (sec_idx >= obj->efile.sec_cnt)
4598 return -EINVAL;
4599
6245947c
AN
4600 scn = elf_sec_by_idx(obj, sec_idx);
4601 scn_data = elf_sec_data(obj, scn);
fc3a5534
MZ
4602 if (!scn_data)
4603 return -LIBBPF_ERRNO__FORMAT;
6245947c 4604
9c0f8cbd 4605 relo_sec_name = elf_sec_str(obj, shdr->sh_name);
6245947c 4606 sec_name = elf_sec_name(obj, scn);
9c0f8cbd
AN
4607 if (!relo_sec_name || !sec_name)
4608 return -EINVAL;
4609
4610 pr_debug("sec '%s': collecting relocation for section(%zu) '%s'\n",
4611 relo_sec_name, sec_idx, sec_name);
34090915
WN
4612 nrels = shdr->sh_size / shdr->sh_entsize;
4613
34090915 4614 for (i = 0; i < nrels; i++) {
ad23b723
AN
4615 rel = elf_rel_by_idx(data, i);
4616 if (!rel) {
9c0f8cbd 4617 pr_warn("sec '%s': failed to get relo #%d\n", relo_sec_name, i);
6371ca3b 4618 return -LIBBPF_ERRNO__FORMAT;
34090915 4619 }
ad23b723 4620
b7332d28
AN
4621 sym_idx = ELF64_R_SYM(rel->r_info);
4622 sym = elf_sym_by_idx(obj, sym_idx);
ad23b723 4623 if (!sym) {
b7332d28
AN
4624 pr_warn("sec '%s': symbol #%zu not found for relo #%d\n",
4625 relo_sec_name, sym_idx, i);
4626 return -LIBBPF_ERRNO__FORMAT;
4627 }
4628
4629 if (sym->st_shndx >= obj->efile.sec_cnt) {
4630 pr_warn("sec '%s': corrupted symbol #%zu pointing to invalid section #%zu for relo #%d\n",
4631 relo_sec_name, sym_idx, (size_t)sym->st_shndx, i);
6371ca3b 4632 return -LIBBPF_ERRNO__FORMAT;
34090915 4633 }
6245947c 4634
ad23b723 4635 if (rel->r_offset % BPF_INSN_SZ || rel->r_offset >= scn_data->d_size) {
9c0f8cbd 4636 pr_warn("sec '%s': invalid offset 0x%zx for relo #%d\n",
b7332d28 4637 relo_sec_name, (size_t)rel->r_offset, i);
1f8e2bcb 4638 return -LIBBPF_ERRNO__FORMAT;
9c0f8cbd 4639 }
d859900c 4640
ad23b723 4641 insn_idx = rel->r_offset / BPF_INSN_SZ;
c3c55696
AN
4642 /* relocations against static functions are recorded as
4643 * relocations against the section that contains a function;
4644 * in such case, symbol will be STT_SECTION and sym.st_name
4645 * will point to empty string (0), so fetch section name
4646 * instead
4647 */
ad23b723
AN
4648 if (ELF64_ST_TYPE(sym->st_info) == STT_SECTION && sym->st_name == 0)
4649 sym_name = elf_sec_name(obj, elf_sec_by_idx(obj, sym->st_shndx));
c3c55696 4650 else
ad23b723 4651 sym_name = elf_sym_str(obj, sym->st_name);
c3c55696 4652 sym_name = sym_name ?: "<?";
d859900c 4653
9c0f8cbd
AN
4654 pr_debug("sec '%s': relo #%d: insn #%u against '%s'\n",
4655 relo_sec_name, i, insn_idx, sym_name);
666810e8 4656
c3c55696
AN
4657 prog = find_prog_by_sec_insn(obj, sec_idx, insn_idx);
4658 if (!prog) {
6245947c 4659 pr_debug("sec '%s': relo #%d: couldn't find program in section '%s' for insn #%u, probably overridden weak function, skipping...\n",
c3c55696 4660 relo_sec_name, i, sec_name, insn_idx);
6245947c 4661 continue;
c3c55696
AN
4662 }
4663
4664 relos = libbpf_reallocarray(prog->reloc_desc,
4665 prog->nr_reloc + 1, sizeof(*relos));
4666 if (!relos)
4667 return -ENOMEM;
4668 prog->reloc_desc = relos;
4669
4670 /* adjust insn_idx to local BPF program frame of reference */
4671 insn_idx -= prog->sec_insn_off;
4672 err = bpf_program__record_reloc(prog, &relos[prog->nr_reloc],
ad23b723 4673 insn_idx, sym_name, sym, rel);
1f8e2bcb
AN
4674 if (err)
4675 return err;
c3c55696
AN
4676
4677 prog->nr_reloc++;
34090915
WN
4678 }
4679 return 0;
4680}
4681
4fcac46c 4682static int map_fill_btf_type_info(struct bpf_object *obj, struct bpf_map *map)
8a138aed 4683{
aaf6886d 4684 int id;
8a138aed 4685
a8fee962
AN
4686 if (!obj->btf)
4687 return -ENOENT;
4688
590a0088
MKL
4689 /* if it's BTF-defined map, we don't need to search for type IDs.
4690 * For struct_ops map, it does not need btf_key_type_id and
4691 * btf_value_type_id.
4692 */
aaf6886d 4693 if (map->sec_idx == obj->efile.btf_maps_shndx || bpf_map__is_struct_ops(map))
abd29c93
AN
4694 return 0;
4695
aaf6886d
AN
4696 /*
4697 * LLVM annotates global data differently in BTF, that is,
4698 * only as '.data', '.bss' or '.rodata'.
4699 */
4700 if (!bpf_map__is_internal(map))
4701 return -ENOENT;
4702
4703 id = btf__find_by_name(obj->btf, map->real_name);
4704 if (id < 0)
4705 return id;
8a138aed 4706
aaf6886d
AN
4707 map->btf_key_type_id = 0;
4708 map->btf_value_type_id = id;
8a138aed
MKL
4709 return 0;
4710}
4711
97eb3138
MP
4712static int bpf_get_map_info_from_fdinfo(int fd, struct bpf_map_info *info)
4713{
4714 char file[PATH_MAX], buff[4096];
4715 FILE *fp;
4716 __u32 val;
4717 int err;
4718
4719 snprintf(file, sizeof(file), "/proc/%d/fdinfo/%d", getpid(), fd);
4720 memset(info, 0, sizeof(*info));
4721
59842c54 4722 fp = fopen(file, "re");
97eb3138
MP
4723 if (!fp) {
4724 err = -errno;
4725 pr_warn("failed to open %s: %d. No procfs support?\n", file,
4726 err);
4727 return err;
4728 }
4729
4730 while (fgets(buff, sizeof(buff), fp)) {
4731 if (sscanf(buff, "map_type:\t%u", &val) == 1)
4732 info->type = val;
4733 else if (sscanf(buff, "key_size:\t%u", &val) == 1)
4734 info->key_size = val;
4735 else if (sscanf(buff, "value_size:\t%u", &val) == 1)
4736 info->value_size = val;
4737 else if (sscanf(buff, "max_entries:\t%u", &val) == 1)
4738 info->max_entries = val;
4739 else if (sscanf(buff, "map_flags:\t%i", &val) == 1)
4740 info->map_flags = val;
4741 }
4742
4743 fclose(fp);
4744
4745 return 0;
4746}
4747
ec41817b
AN
4748bool bpf_map__autocreate(const struct bpf_map *map)
4749{
4750 return map->autocreate;
4751}
4752
4753int bpf_map__set_autocreate(struct bpf_map *map, bool autocreate)
4754{
4755 if (map->obj->loaded)
4756 return libbpf_err(-EBUSY);
4757
4758 map->autocreate = autocreate;
4759 return 0;
4760}
4761
26736eb9
JK
4762int bpf_map__reuse_fd(struct bpf_map *map, int fd)
4763{
813847a3 4764 struct bpf_map_info info;
bf3f0037 4765 __u32 len = sizeof(info), name_len;
26736eb9
JK
4766 int new_fd, err;
4767 char *new_name;
4768
813847a3 4769 memset(&info, 0, len);
629dfc66 4770 err = bpf_map_get_info_by_fd(fd, &info, &len);
97eb3138
MP
4771 if (err && errno == EINVAL)
4772 err = bpf_get_map_info_from_fdinfo(fd, &info);
26736eb9 4773 if (err)
e9fc3ce9 4774 return libbpf_err(err);
26736eb9 4775
bf3f0037
AW
4776 name_len = strlen(info.name);
4777 if (name_len == BPF_OBJ_NAME_LEN - 1 && strncmp(map->name, info.name, name_len) == 0)
4778 new_name = strdup(map->name);
4779 else
4780 new_name = strdup(info.name);
4781
26736eb9 4782 if (!new_name)
e9fc3ce9 4783 return libbpf_err(-errno);
26736eb9 4784
4aadd292
AN
4785 /*
4786 * Like dup(), but make sure new FD is >= 3 and has O_CLOEXEC set.
4787 * This is similar to what we do in ensure_good_fd(), but without
4788 * closing original FD.
4789 */
4790 new_fd = fcntl(fd, F_DUPFD_CLOEXEC, 3);
d1b4574a
THJ
4791 if (new_fd < 0) {
4792 err = -errno;
26736eb9 4793 goto err_free_new_name;
d1b4574a 4794 }
26736eb9 4795
dac645b9
AN
4796 err = reuse_fd(map->fd, new_fd);
4797 if (err)
4798 goto err_free_new_name;
4799
26736eb9
JK
4800 free(map->name);
4801
26736eb9
JK
4802 map->name = new_name;
4803 map->def.type = info.type;
4804 map->def.key_size = info.key_size;
4805 map->def.value_size = info.value_size;
4806 map->def.max_entries = info.max_entries;
4807 map->def.map_flags = info.map_flags;
4808 map->btf_key_type_id = info.btf_key_type_id;
4809 map->btf_value_type_id = info.btf_value_type_id;
ec6d5f47 4810 map->reused = true;
47512102 4811 map->map_extra = info.map_extra;
26736eb9
JK
4812
4813 return 0;
4814
26736eb9
JK
4815err_free_new_name:
4816 free(new_name);
e9fc3ce9 4817 return libbpf_err(err);
26736eb9
JK
4818}
4819
1bdb6c9a 4820__u32 bpf_map__max_entries(const struct bpf_map *map)
1a11a4c7 4821{
1bdb6c9a
AN
4822 return map->def.max_entries;
4823}
1a11a4c7 4824
b3278099
AN
4825struct bpf_map *bpf_map__inner_map(struct bpf_map *map)
4826{
4827 if (!bpf_map_type__is_map_in_map(map->def.type))
e9fc3ce9 4828 return errno = EINVAL, NULL;
b3278099
AN
4829
4830 return map->inner_map;
4831}
4832
1bdb6c9a
AN
4833int bpf_map__set_max_entries(struct bpf_map *map, __u32 max_entries)
4834{
597fbc46 4835 if (map->obj->loaded)
e9fc3ce9 4836 return libbpf_err(-EBUSY);
597fbc46 4837
1a11a4c7 4838 map->def.max_entries = max_entries;
597fbc46
AN
4839
4840 /* auto-adjust BPF ringbuf map max_entries to be a multiple of page size */
b66ccae0 4841 if (map_is_ringbuf(map))
597fbc46
AN
4842 map->def.max_entries = adjust_ringbuf_sz(map->def.max_entries);
4843
1a11a4c7
AI
4844 return 0;
4845}
4846
6b434b61
AN
4847static int bpf_object_prepare_token(struct bpf_object *obj)
4848{
4849 const char *bpffs_path;
4850 int bpffs_fd = -1, token_fd, err;
4851 bool mandatory;
4852 enum libbpf_print_level level;
4853
4854 /* token is explicitly prevented */
4855 if (obj->token_path && obj->token_path[0] == '\0') {
4856 pr_debug("object '%s': token is prevented, skipping...\n", obj->name);
4857 return 0;
4858 }
4859
4860 mandatory = obj->token_path != NULL;
4861 level = mandatory ? LIBBPF_WARN : LIBBPF_DEBUG;
4862
4863 bpffs_path = obj->token_path ?: BPF_FS_DEFAULT_PATH;
4864 bpffs_fd = open(bpffs_path, O_DIRECTORY, O_RDWR);
4865 if (bpffs_fd < 0) {
4866 err = -errno;
4867 __pr(level, "object '%s': failed (%d) to open BPF FS mount at '%s'%s\n",
4868 obj->name, err, bpffs_path,
4869 mandatory ? "" : ", skipping optional step...");
4870 return mandatory ? err : 0;
4871 }
4872
4873 token_fd = bpf_token_create(bpffs_fd, 0);
4874 close(bpffs_fd);
4875 if (token_fd < 0) {
4876 if (!mandatory && token_fd == -ENOENT) {
4877 pr_debug("object '%s': BPF FS at '%s' doesn't have BPF token delegation set up, skipping...\n",
4878 obj->name, bpffs_path);
4879 return 0;
4880 }
4881 __pr(level, "object '%s': failed (%d) to create BPF token from '%s'%s\n",
4882 obj->name, token_fd, bpffs_path,
4883 mandatory ? "" : ", skipping optional step...");
4884 return mandatory ? token_fd : 0;
4885 }
4886
4887 obj->feat_cache = calloc(1, sizeof(*obj->feat_cache));
4888 if (!obj->feat_cache) {
4889 close(token_fd);
4890 return -ENOMEM;
4891 }
4892
4893 obj->token_fd = token_fd;
4894 obj->feat_cache->token_fd = token_fd;
4895
4896 return 0;
4897}
4898
47eff617 4899static int
fd9eef1a 4900bpf_object__probe_loading(struct bpf_object *obj)
47eff617 4901{
47eff617
SF
4902 char *cp, errmsg[STRERR_BUFSIZE];
4903 struct bpf_insn insns[] = {
4904 BPF_MOV64_IMM(BPF_REG_0, 0),
4905 BPF_EXIT_INSN(),
4906 };
e32660ac 4907 int ret, insn_cnt = ARRAY_SIZE(insns);
6b434b61
AN
4908 LIBBPF_OPTS(bpf_prog_load_opts, opts,
4909 .token_fd = obj->token_fd,
4910 .prog_flags = obj->token_fd ? BPF_F_TOKEN_FD : 0,
4911 );
47eff617 4912
f9bceaa5
SF
4913 if (obj->gen_loader)
4914 return 0;
4915
e542f2c4
AN
4916 ret = bump_rlimit_memlock();
4917 if (ret)
4918 pr_warn("Failed to bump RLIMIT_MEMLOCK (err = %d), you might need to do it explicitly!\n", ret);
4919
47eff617 4920 /* make sure basic loading works */
6b434b61 4921 ret = bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, NULL, "GPL", insns, insn_cnt, &opts);
e32660ac 4922 if (ret < 0)
6b434b61 4923 ret = bpf_prog_load(BPF_PROG_TYPE_TRACEPOINT, NULL, "GPL", insns, insn_cnt, &opts);
47eff617 4924 if (ret < 0) {
fd9eef1a
EC
4925 ret = errno;
4926 cp = libbpf_strerror_r(ret, errmsg, sizeof(errmsg));
4927 pr_warn("Error in %s():%s(%d). Couldn't load trivial BPF "
4928 "program. Make sure your kernel supports BPF "
4929 "(CONFIG_BPF_SYSCALL=y) and/or that RLIMIT_MEMLOCK is "
4930 "set to big enough value.\n", __func__, cp, ret);
4931 return -ret;
47eff617
SF
4932 }
4933 close(ret);
4934
fd9eef1a
EC
4935 return 0;
4936}
4937
d6dd1d49
AN
4938bool kernel_supports(const struct bpf_object *obj, enum kern_feature_id feat_id)
4939{
8263b338 4940 if (obj->gen_loader)
d6dd1d49
AN
4941 /* To generate loader program assume the latest kernel
4942 * to avoid doing extra prog_load, map_create syscalls.
4943 */
4944 return true;
4945
6b434b61
AN
4946 if (obj->token_fd)
4947 return feat_supported(obj->feat_cache, feat_id);
4948
d6dd1d49
AN
4949 return feat_supported(NULL, feat_id);
4950}
4951
57a00f41
THJ
4952static bool map_is_reuse_compat(const struct bpf_map *map, int map_fd)
4953{
813847a3 4954 struct bpf_map_info map_info;
57a00f41 4955 char msg[STRERR_BUFSIZE];
813847a3 4956 __u32 map_info_len = sizeof(map_info);
97eb3138 4957 int err;
57a00f41 4958
813847a3 4959 memset(&map_info, 0, map_info_len);
629dfc66 4960 err = bpf_map_get_info_by_fd(map_fd, &map_info, &map_info_len);
97eb3138
MP
4961 if (err && errno == EINVAL)
4962 err = bpf_get_map_info_from_fdinfo(map_fd, &map_info);
4963 if (err) {
4964 pr_warn("failed to get map info for map FD %d: %s\n", map_fd,
4965 libbpf_strerror_r(errno, msg, sizeof(msg)));
57a00f41
THJ
4966 return false;
4967 }
4968
4969 return (map_info.type == map->def.type &&
4970 map_info.key_size == map->def.key_size &&
4971 map_info.value_size == map->def.value_size &&
4972 map_info.max_entries == map->def.max_entries &&
47512102
JK
4973 map_info.map_flags == map->def.map_flags &&
4974 map_info.map_extra == map->map_extra);
57a00f41
THJ
4975}
4976
4977static int
4978bpf_object__reuse_map(struct bpf_map *map)
4979{
4980 char *cp, errmsg[STRERR_BUFSIZE];
4981 int err, pin_fd;
4982
4983 pin_fd = bpf_obj_get(map->pin_path);
4984 if (pin_fd < 0) {
4985 err = -errno;
4986 if (err == -ENOENT) {
4987 pr_debug("found no pinned map to reuse at '%s'\n",
4988 map->pin_path);
4989 return 0;
4990 }
4991
4992 cp = libbpf_strerror_r(-err, errmsg, sizeof(errmsg));
4993 pr_warn("couldn't retrieve pinned map '%s': %s\n",
4994 map->pin_path, cp);
4995 return err;
4996 }
4997
4998 if (!map_is_reuse_compat(map, pin_fd)) {
4999 pr_warn("couldn't reuse pinned map at '%s': parameter mismatch\n",
5000 map->pin_path);
5001 close(pin_fd);
5002 return -EINVAL;
5003 }
5004
5005 err = bpf_map__reuse_fd(map, pin_fd);
d0f325c3 5006 close(pin_fd);
e3ba8e4e 5007 if (err)
57a00f41 5008 return err;
e3ba8e4e 5009
57a00f41
THJ
5010 map->pinned = true;
5011 pr_debug("reused pinned map at '%s'\n", map->pin_path);
5012
5013 return 0;
5014}
5015
d859900c
DB
5016static int
5017bpf_object__populate_internal_map(struct bpf_object *obj, struct bpf_map *map)
5018{
166750bc 5019 enum libbpf_map_type map_type = map->libbpf_type;
d859900c
DB
5020 char *cp, errmsg[STRERR_BUFSIZE];
5021 int err, zero = 0;
d859900c 5022
67234743
AS
5023 if (obj->gen_loader) {
5024 bpf_gen__map_update_elem(obj->gen_loader, map - obj->maps,
5025 map->mmaped, map->def.value_size);
5026 if (map_type == LIBBPF_MAP_RODATA || map_type == LIBBPF_MAP_KCONFIG)
5027 bpf_gen__map_freeze(obj->gen_loader, map - obj->maps);
5028 return 0;
5029 }
2e7ba4f8 5030
eba9c5f4
AN
5031 err = bpf_map_update_elem(map->fd, &zero, map->mmaped, 0);
5032 if (err) {
5033 err = -errno;
5034 cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg));
5035 pr_warn("Error setting initial map(%s) contents: %s\n",
5036 map->name, cp);
5037 return err;
5038 }
d859900c 5039
81bfdd08
AN
5040 /* Freeze .rodata and .kconfig map as read-only from syscall side. */
5041 if (map_type == LIBBPF_MAP_RODATA || map_type == LIBBPF_MAP_KCONFIG) {
d859900c
DB
5042 err = bpf_map_freeze(map->fd);
5043 if (err) {
eba9c5f4
AN
5044 err = -errno;
5045 cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg));
be18010e
KW
5046 pr_warn("Error freezing map(%s) as read-only: %s\n",
5047 map->name, cp);
eba9c5f4 5048 return err;
d859900c
DB
5049 }
5050 }
eba9c5f4 5051 return 0;
d859900c
DB
5052}
5053
2d39d7c5
AN
5054static void bpf_map__destroy(struct bpf_map *map);
5055
f08c18e0
AN
5056static bool map_is_created(const struct bpf_map *map)
5057{
5058 return map->obj->loaded || map->reused;
5059}
5060
67234743 5061static int bpf_object__create_map(struct bpf_object *obj, struct bpf_map *map, bool is_inner)
2d39d7c5 5062{
992c4225 5063 LIBBPF_OPTS(bpf_map_create_opts, create_attr);
2d39d7c5 5064 struct bpf_map_def *def = &map->def;
992c4225 5065 const char *map_name = NULL;
dac645b9 5066 int err = 0, map_fd;
2d39d7c5 5067
9ca1f56a 5068 if (kernel_supports(obj, FEAT_PROG_NAME))
992c4225 5069 map_name = map->name;
2d39d7c5 5070 create_attr.map_ifindex = map->map_ifindex;
2d39d7c5 5071 create_attr.map_flags = def->map_flags;
1bdb6c9a 5072 create_attr.numa_node = map->numa_node;
47512102 5073 create_attr.map_extra = map->map_extra;
6b434b61
AN
5074 create_attr.token_fd = obj->token_fd;
5075 if (obj->token_fd)
5076 create_attr.map_flags |= BPF_F_TOKEN_FD;
2d39d7c5 5077
9e926acd 5078 if (bpf_map__is_struct_ops(map)) {
992c4225 5079 create_attr.btf_vmlinux_value_type_id = map->btf_vmlinux_value_type_id;
9e926acd
KFL
5080 if (map->mod_btf_fd >= 0) {
5081 create_attr.value_type_btf_obj_fd = map->mod_btf_fd;
5082 create_attr.map_flags |= BPF_F_VTYPE_BTF_OBJ_FD;
5083 }
5084 }
2d39d7c5 5085
262cfb74 5086 if (obj->btf && btf__fd(obj->btf) >= 0) {
2d39d7c5
AN
5087 create_attr.btf_fd = btf__fd(obj->btf);
5088 create_attr.btf_key_type_id = map->btf_key_type_id;
5089 create_attr.btf_value_type_id = map->btf_value_type_id;
5090 }
5091
646f02ff
AN
5092 if (bpf_map_type__is_map_in_map(def->type)) {
5093 if (map->inner_map) {
f04deb90
AG
5094 err = map_set_def_max_entries(map->inner_map);
5095 if (err)
5096 return err;
67234743 5097 err = bpf_object__create_map(obj, map->inner_map, true);
646f02ff
AN
5098 if (err) {
5099 pr_warn("map '%s': failed to create inner map: %d\n",
5100 map->name, err);
5101 return err;
5102 }
f08c18e0 5103 map->inner_map_fd = map->inner_map->fd;
646f02ff
AN
5104 }
5105 if (map->inner_map_fd >= 0)
5106 create_attr.inner_map_fd = map->inner_map_fd;
5107 }
5108
f7310523
HC
5109 switch (def->type) {
5110 case BPF_MAP_TYPE_PERF_EVENT_ARRAY:
5111 case BPF_MAP_TYPE_CGROUP_ARRAY:
5112 case BPF_MAP_TYPE_STACK_TRACE:
5113 case BPF_MAP_TYPE_ARRAY_OF_MAPS:
5114 case BPF_MAP_TYPE_HASH_OF_MAPS:
5115 case BPF_MAP_TYPE_DEVMAP:
5116 case BPF_MAP_TYPE_DEVMAP_HASH:
5117 case BPF_MAP_TYPE_CPUMAP:
5118 case BPF_MAP_TYPE_XSKMAP:
5119 case BPF_MAP_TYPE_SOCKMAP:
5120 case BPF_MAP_TYPE_SOCKHASH:
5121 case BPF_MAP_TYPE_QUEUE:
5122 case BPF_MAP_TYPE_STACK:
79ff13e9 5123 case BPF_MAP_TYPE_ARENA:
f7310523
HC
5124 create_attr.btf_fd = 0;
5125 create_attr.btf_key_type_id = 0;
5126 create_attr.btf_value_type_id = 0;
5127 map->btf_key_type_id = 0;
5128 map->btf_value_type_id = 0;
3644d285
KFL
5129 break;
5130 case BPF_MAP_TYPE_STRUCT_OPS:
5131 create_attr.btf_value_type_id = 0;
5132 break;
f7310523
HC
5133 default:
5134 break;
5135 }
5136
67234743 5137 if (obj->gen_loader) {
992c4225 5138 bpf_gen__map_create(obj->gen_loader, def->type, map_name,
a4fbfdd7 5139 def->key_size, def->value_size, def->max_entries,
992c4225 5140 &create_attr, is_inner ? -1 : map - obj->maps);
dac645b9
AN
5141 /* We keep pretenting we have valid FD to pass various fd >= 0
5142 * checks by just keeping original placeholder FDs in place.
5143 * See bpf_object__add_map() comment.
5144 * This placeholder fd will not be used with any syscall and
5145 * will be reset to -1 eventually.
67234743 5146 */
dac645b9 5147 map_fd = map->fd;
67234743 5148 } else {
dac645b9
AN
5149 map_fd = bpf_map_create(def->type, map_name,
5150 def->key_size, def->value_size,
5151 def->max_entries, &create_attr);
67234743 5152 }
dac645b9 5153 if (map_fd < 0 && (create_attr.btf_key_type_id || create_attr.btf_value_type_id)) {
2d39d7c5 5154 char *cp, errmsg[STRERR_BUFSIZE];
2d39d7c5 5155
a21ab4c5 5156 err = -errno;
2d39d7c5
AN
5157 cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg));
5158 pr_warn("Error in bpf_create_map_xattr(%s):%s(%d). Retrying without BTF.\n",
5159 map->name, cp, err);
5160 create_attr.btf_fd = 0;
5161 create_attr.btf_key_type_id = 0;
5162 create_attr.btf_value_type_id = 0;
5163 map->btf_key_type_id = 0;
5164 map->btf_value_type_id = 0;
dac645b9
AN
5165 map_fd = bpf_map_create(def->type, map_name,
5166 def->key_size, def->value_size,
5167 def->max_entries, &create_attr);
2d39d7c5
AN
5168 }
5169
646f02ff 5170 if (bpf_map_type__is_map_in_map(def->type) && map->inner_map) {
67234743
AS
5171 if (obj->gen_loader)
5172 map->inner_map->fd = -1;
646f02ff
AN
5173 bpf_map__destroy(map->inner_map);
5174 zfree(&map->inner_map);
5175 }
5176
dac645b9
AN
5177 if (map_fd < 0)
5178 return map_fd;
5179
5180 /* obj->gen_loader case, prevent reuse_fd() from closing map_fd */
5181 if (map->fd == map_fd)
5182 return 0;
5183
5184 /* Keep placeholder FD value but now point it to the BPF map object.
5185 * This way everything that relied on this map's FD (e.g., relocated
5186 * ldimm64 instructions) will stay valid and won't need adjustments.
5187 * map->fd stays valid but now point to what map_fd points to.
5188 */
5189 return reuse_fd(map->fd, map_fd);
2d39d7c5
AN
5190}
5191
341ac5ff 5192static int init_map_in_map_slots(struct bpf_object *obj, struct bpf_map *map)
a0f2b7ac
HL
5193{
5194 const struct bpf_map *targ_map;
5195 unsigned int i;
67234743 5196 int fd, err = 0;
a0f2b7ac
HL
5197
5198 for (i = 0; i < map->init_slots_sz; i++) {
5199 if (!map->init_slots[i])
5200 continue;
5201
5202 targ_map = map->init_slots[i];
f08c18e0 5203 fd = targ_map->fd;
341ac5ff 5204
67234743 5205 if (obj->gen_loader) {
be05c944
AS
5206 bpf_gen__populate_outer_map(obj->gen_loader,
5207 map - obj->maps, i,
5208 targ_map - obj->maps);
67234743
AS
5209 } else {
5210 err = bpf_map_update_elem(map->fd, &i, &fd, 0);
5211 }
a0f2b7ac
HL
5212 if (err) {
5213 err = -errno;
5214 pr_warn("map '%s': failed to initialize slot [%d] to map '%s' fd=%d: %d\n",
341ac5ff 5215 map->name, i, targ_map->name, fd, err);
a0f2b7ac
HL
5216 return err;
5217 }
5218 pr_debug("map '%s': slot [%d] set to map '%s' fd=%d\n",
5219 map->name, i, targ_map->name, fd);
5220 }
5221
5222 zfree(&map->init_slots);
5223 map->init_slots_sz = 0;
5224
5225 return 0;
5226}
5227
341ac5ff
HC
5228static int init_prog_array_slots(struct bpf_object *obj, struct bpf_map *map)
5229{
5230 const struct bpf_program *targ_prog;
5231 unsigned int i;
5232 int fd, err;
5233
5234 if (obj->gen_loader)
5235 return -ENOTSUP;
5236
5237 for (i = 0; i < map->init_slots_sz; i++) {
5238 if (!map->init_slots[i])
5239 continue;
5240
5241 targ_prog = map->init_slots[i];
5242 fd = bpf_program__fd(targ_prog);
5243
5244 err = bpf_map_update_elem(map->fd, &i, &fd, 0);
5245 if (err) {
5246 err = -errno;
5247 pr_warn("map '%s': failed to initialize slot [%d] to prog '%s' fd=%d: %d\n",
5248 map->name, i, targ_prog->name, fd, err);
5249 return err;
5250 }
5251 pr_debug("map '%s': slot [%d] set to prog '%s' fd=%d\n",
5252 map->name, i, targ_prog->name, fd);
5253 }
5254
5255 zfree(&map->init_slots);
5256 map->init_slots_sz = 0;
5257
5258 return 0;
5259}
5260
5261static int bpf_object_init_prog_arrays(struct bpf_object *obj)
5262{
5263 struct bpf_map *map;
5264 int i, err;
5265
5266 for (i = 0; i < obj->nr_maps; i++) {
5267 map = &obj->maps[i];
5268
5269 if (!map->init_slots_sz || map->def.type != BPF_MAP_TYPE_PROG_ARRAY)
5270 continue;
5271
5272 err = init_prog_array_slots(obj, map);
dac645b9 5273 if (err < 0)
341ac5ff 5274 return err;
341ac5ff
HC
5275 }
5276 return 0;
5277}
5278
a4fbfdd7
ST
5279static int map_set_def_max_entries(struct bpf_map *map)
5280{
5281 if (map->def.type == BPF_MAP_TYPE_PERF_EVENT_ARRAY && !map->def.max_entries) {
5282 int nr_cpus;
5283
5284 nr_cpus = libbpf_num_possible_cpus();
5285 if (nr_cpus < 0) {
5286 pr_warn("map '%s': failed to determine number of system CPUs: %d\n",
5287 map->name, nr_cpus);
5288 return nr_cpus;
5289 }
5290 pr_debug("map '%s': setting size to %d\n", map->name, nr_cpus);
5291 map->def.max_entries = nr_cpus;
5292 }
5293
5294 return 0;
5295}
5296
52d3352e
WN
5297static int
5298bpf_object__create_maps(struct bpf_object *obj)
5299{
2d39d7c5
AN
5300 struct bpf_map *map;
5301 char *cp, errmsg[STRERR_BUFSIZE];
5302 unsigned int i, j;
8a138aed 5303 int err;
043c5bb3 5304 bool retried;
52d3352e 5305
9d759a9b 5306 for (i = 0; i < obj->nr_maps; i++) {
2d39d7c5 5307 map = &obj->maps[i];
8a138aed 5308
16e0c35c
AN
5309 /* To support old kernels, we skip creating global data maps
5310 * (.rodata, .data, .kconfig, etc); later on, during program
5311 * loading, if we detect that at least one of the to-be-loaded
5312 * programs is referencing any global data map, we'll error
5313 * out with program name and relocation index logged.
5314 * This approach allows to accommodate Clang emitting
5315 * unnecessary .rodata.str1.1 sections for string literals,
5316 * but also it allows to have CO-RE applications that use
5317 * global variables in some of BPF programs, but not others.
5318 * If those global variable-using programs are not loaded at
5319 * runtime due to bpf_program__set_autoload(prog, false),
5320 * bpf_object loading will succeed just fine even on old
5321 * kernels.
5322 */
ec41817b
AN
5323 if (bpf_map__is_internal(map) && !kernel_supports(obj, FEAT_GLOBAL_DATA))
5324 map->autocreate = false;
5325
5326 if (!map->autocreate) {
5327 pr_debug("map '%s': skipped auto-creating...\n", map->name);
16e0c35c 5328 continue;
229fae38 5329 }
16e0c35c 5330
a4fbfdd7
ST
5331 err = map_set_def_max_entries(map);
5332 if (err)
5333 goto err_out;
5334
043c5bb3
MP
5335 retried = false;
5336retry:
57a00f41
THJ
5337 if (map->pin_path) {
5338 err = bpf_object__reuse_map(map);
5339 if (err) {
2d39d7c5 5340 pr_warn("map '%s': error reusing pinned map\n",
57a00f41 5341 map->name);
2d39d7c5 5342 goto err_out;
57a00f41 5343 }
043c5bb3
MP
5344 if (retried && map->fd < 0) {
5345 pr_warn("map '%s': cannot find pinned map\n",
5346 map->name);
5347 err = -ENOENT;
5348 goto err_out;
5349 }
57a00f41
THJ
5350 }
5351
fa98b54b 5352 if (map->reused) {
2d39d7c5 5353 pr_debug("map '%s': skipping creation (preset fd=%d)\n",
26736eb9 5354 map->name, map->fd);
2c193d32 5355 } else {
67234743 5356 err = bpf_object__create_map(obj, map, false);
2c193d32 5357 if (err)
d859900c 5358 goto err_out;
d859900c 5359
2c193d32
HL
5360 pr_debug("map '%s': created successfully, fd=%d\n",
5361 map->name, map->fd);
646f02ff 5362
2c193d32
HL
5363 if (bpf_map__is_internal(map)) {
5364 err = bpf_object__populate_internal_map(obj, map);
dac645b9 5365 if (err < 0)
2c193d32 5366 goto err_out;
d859900c 5367 }
79ff13e9
AS
5368 if (map->def.type == BPF_MAP_TYPE_ARENA) {
5369 map->mmaped = mmap((void *)map->map_extra, bpf_map_mmap_sz(map),
5370 PROT_READ | PROT_WRITE,
5371 map->map_extra ? MAP_SHARED | MAP_FIXED : MAP_SHARED,
5372 map->fd, 0);
5373 if (map->mmaped == MAP_FAILED) {
5374 err = -errno;
5375 map->mmaped = NULL;
5376 pr_warn("map '%s': failed to mmap arena: %d\n",
5377 map->name, err);
5378 return err;
5379 }
2e7ba4f8
AN
5380 if (obj->arena_data) {
5381 memcpy(map->mmaped, obj->arena_data, obj->arena_data_sz);
5382 zfree(&obj->arena_data);
5383 }
79ff13e9 5384 }
341ac5ff
HC
5385 if (map->init_slots_sz && map->def.type != BPF_MAP_TYPE_PROG_ARRAY) {
5386 err = init_map_in_map_slots(obj, map);
dac645b9 5387 if (err < 0)
646f02ff 5388 goto err_out;
646f02ff 5389 }
646f02ff
AN
5390 }
5391
57a00f41
THJ
5392 if (map->pin_path && !map->pinned) {
5393 err = bpf_map__pin(map, NULL);
5394 if (err) {
043c5bb3
MP
5395 if (!retried && err == -EEXIST) {
5396 retried = true;
5397 goto retry;
5398 }
2d39d7c5
AN
5399 pr_warn("map '%s': failed to auto-pin at '%s': %d\n",
5400 map->name, map->pin_path, err);
2d39d7c5 5401 goto err_out;
57a00f41
THJ
5402 }
5403 }
52d3352e
WN
5404 }
5405
52d3352e 5406 return 0;
2d39d7c5
AN
5407
5408err_out:
5409 cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg));
5410 pr_warn("map '%s': failed to create: %s(%d)\n", map->name, cp, err);
5411 pr_perm_msg(err);
5412 for (j = 0; j < i; j++)
5413 zclose(obj->maps[j].fd);
5414 return err;
52d3352e
WN
5415}
5416
ddc7c304
AN
5417static bool bpf_core_is_flavor_sep(const char *s)
5418{
5419 /* check X___Y name pattern, where X and Y are not underscores */
5420 return s[0] != '_' && /* X */
5421 s[1] == '_' && s[2] == '_' && s[3] == '_' && /* ___ */
5422 s[4] != '_'; /* Y */
5423}
5424
5425/* Given 'some_struct_name___with_flavor' return the length of a name prefix
5426 * before last triple underscore. Struct name part after last triple
5427 * underscore is ignored by BPF CO-RE relocation during relocation matching.
5428 */
b0588390 5429size_t bpf_core_essential_name_len(const char *name)
ddc7c304
AN
5430{
5431 size_t n = strlen(name);
5432 int i;
5433
5434 for (i = n - 5; i >= 0; i--) {
5435 if (bpf_core_is_flavor_sep(name + i))
5436 return i + 1;
5437 }
5438 return n;
5439}
5440
8de6cae4 5441void bpf_core_free_cands(struct bpf_core_cand_list *cands)
ddc7c304 5442{
8de6cae4
MV
5443 if (!cands)
5444 return;
5445
0f7515ca
AN
5446 free(cands->cands);
5447 free(cands);
ddc7c304
AN
5448}
5449
8de6cae4
MV
5450int bpf_core_add_cands(struct bpf_core_cand *local_cand,
5451 size_t local_essent_len,
5452 const struct btf *targ_btf,
5453 const char *targ_btf_name,
5454 int targ_start_id,
5455 struct bpf_core_cand_list *cands)
ddc7c304 5456{
301ba4d7 5457 struct bpf_core_cand *new_cands, *cand;
03d5b991
AN
5458 const struct btf_type *t, *local_t;
5459 const char *targ_name, *local_name;
0f7515ca
AN
5460 size_t targ_essent_len;
5461 int n, i;
ddc7c304 5462
03d5b991
AN
5463 local_t = btf__type_by_id(local_cand->btf, local_cand->id);
5464 local_name = btf__str_by_offset(local_cand->btf, local_t->name_off);
5465
6a886de0
HC
5466 n = btf__type_cnt(targ_btf);
5467 for (i = targ_start_id; i < n; i++) {
ddc7c304 5468 t = btf__type_by_id(targ_btf, i);
23b2a3a8 5469 if (!btf_kind_core_compat(t, local_t))
ddc7c304
AN
5470 continue;
5471
3fc32f40
AN
5472 targ_name = btf__name_by_offset(targ_btf, t->name_off);
5473 if (str_is_empty(targ_name))
d121e1d3
AN
5474 continue;
5475
ddc7c304
AN
5476 targ_essent_len = bpf_core_essential_name_len(targ_name);
5477 if (targ_essent_len != local_essent_len)
5478 continue;
5479
03d5b991 5480 if (strncmp(local_name, targ_name, local_essent_len) != 0)
0f7515ca
AN
5481 continue;
5482
5483 pr_debug("CO-RE relocating [%d] %s %s: found target candidate [%d] %s %s in [%s]\n",
03d5b991
AN
5484 local_cand->id, btf_kind_str(local_t),
5485 local_name, i, btf_kind_str(t), targ_name,
0f7515ca
AN
5486 targ_btf_name);
5487 new_cands = libbpf_reallocarray(cands->cands, cands->len + 1,
5488 sizeof(*cands->cands));
5489 if (!new_cands)
5490 return -ENOMEM;
5491
5492 cand = &new_cands[cands->len];
5493 cand->btf = targ_btf;
0f7515ca
AN
5494 cand->id = i;
5495
5496 cands->cands = new_cands;
5497 cands->len++;
ddc7c304 5498 }
0f7515ca
AN
5499 return 0;
5500}
5501
4f33a53d
AN
5502static int load_module_btfs(struct bpf_object *obj)
5503{
5504 struct bpf_btf_info info;
5505 struct module_btf *mod_btf;
5506 struct btf *btf;
5507 char name[64];
5508 __u32 id = 0, len;
5509 int err, fd;
5510
5511 if (obj->btf_modules_loaded)
5512 return 0;
5513
67234743
AS
5514 if (obj->gen_loader)
5515 return 0;
5516
4f33a53d
AN
5517 /* don't do this again, even if we find no module BTFs */
5518 obj->btf_modules_loaded = true;
5519
5520 /* kernel too old to support module BTFs */
9ca1f56a 5521 if (!kernel_supports(obj, FEAT_MODULE_BTF))
4f33a53d
AN
5522 return 0;
5523
5524 while (true) {
5525 err = bpf_btf_get_next_id(id, &id);
5526 if (err && errno == ENOENT)
5527 return 0;
2d2c9516
AT
5528 if (err && errno == EPERM) {
5529 pr_debug("skipping module BTFs loading, missing privileges\n");
5530 return 0;
5531 }
4f33a53d
AN
5532 if (err) {
5533 err = -errno;
5534 pr_warn("failed to iterate BTF objects: %d\n", err);
5535 return err;
ddc7c304 5536 }
4f33a53d
AN
5537
5538 fd = bpf_btf_get_fd_by_id(id);
5539 if (fd < 0) {
5540 if (errno == ENOENT)
5541 continue; /* expected race: BTF was unloaded */
5542 err = -errno;
5543 pr_warn("failed to get BTF object #%d FD: %d\n", id, err);
5544 return err;
5545 }
5546
5547 len = sizeof(info);
5548 memset(&info, 0, sizeof(info));
5549 info.name = ptr_to_u64(name);
5550 info.name_len = sizeof(name);
5551
629dfc66 5552 err = bpf_btf_get_info_by_fd(fd, &info, &len);
4f33a53d
AN
5553 if (err) {
5554 err = -errno;
5555 pr_warn("failed to get BTF object #%d info: %d\n", id, err);
91abb4a6 5556 goto err_out;
4f33a53d
AN
5557 }
5558
5559 /* ignore non-module BTFs */
5560 if (!info.kernel_btf || strcmp(name, "vmlinux") == 0) {
5561 close(fd);
5562 continue;
5563 }
5564
5565 btf = btf_get_from_fd(fd, obj->btf_vmlinux);
e9fc3ce9
AN
5566 err = libbpf_get_error(btf);
5567 if (err) {
5568 pr_warn("failed to load module [%s]'s BTF object #%d: %d\n",
5569 name, id, err);
91abb4a6 5570 goto err_out;
4f33a53d
AN
5571 }
5572
3b029e06 5573 err = libbpf_ensure_mem((void **)&obj->btf_modules, &obj->btf_module_cap,
e3ba8e4e 5574 sizeof(*obj->btf_modules), obj->btf_module_cnt + 1);
4f33a53d 5575 if (err)
91abb4a6 5576 goto err_out;
4f33a53d
AN
5577
5578 mod_btf = &obj->btf_modules[obj->btf_module_cnt++];
5579
5580 mod_btf->btf = btf;
5581 mod_btf->id = id;
91abb4a6 5582 mod_btf->fd = fd;
4f33a53d 5583 mod_btf->name = strdup(name);
91abb4a6
AN
5584 if (!mod_btf->name) {
5585 err = -ENOMEM;
5586 goto err_out;
5587 }
5588 continue;
5589
5590err_out:
5591 close(fd);
5592 return err;
ddc7c304 5593 }
4f33a53d
AN
5594
5595 return 0;
5596}
5597
301ba4d7 5598static struct bpf_core_cand_list *
0f7515ca
AN
5599bpf_core_find_cands(struct bpf_object *obj, const struct btf *local_btf, __u32 local_type_id)
5600{
301ba4d7
AS
5601 struct bpf_core_cand local_cand = {};
5602 struct bpf_core_cand_list *cands;
4f33a53d 5603 const struct btf *main_btf;
03d5b991
AN
5604 const struct btf_type *local_t;
5605 const char *local_name;
0f7515ca 5606 size_t local_essent_len;
4f33a53d 5607 int err, i;
0f7515ca
AN
5608
5609 local_cand.btf = local_btf;
03d5b991
AN
5610 local_cand.id = local_type_id;
5611 local_t = btf__type_by_id(local_btf, local_type_id);
5612 if (!local_t)
0f7515ca
AN
5613 return ERR_PTR(-EINVAL);
5614
03d5b991
AN
5615 local_name = btf__name_by_offset(local_btf, local_t->name_off);
5616 if (str_is_empty(local_name))
0f7515ca 5617 return ERR_PTR(-EINVAL);
03d5b991 5618 local_essent_len = bpf_core_essential_name_len(local_name);
0f7515ca
AN
5619
5620 cands = calloc(1, sizeof(*cands));
5621 if (!cands)
5622 return ERR_PTR(-ENOMEM);
5623
5624 /* Attempt to find target candidates in vmlinux BTF first */
4f33a53d
AN
5625 main_btf = obj->btf_vmlinux_override ?: obj->btf_vmlinux;
5626 err = bpf_core_add_cands(&local_cand, local_essent_len, main_btf, "vmlinux", 1, cands);
5627 if (err)
5628 goto err_out;
5629
5630 /* if vmlinux BTF has any candidate, don't got for module BTFs */
5631 if (cands->len)
5632 return cands;
5633
5634 /* if vmlinux BTF was overridden, don't attempt to load module BTFs */
5635 if (obj->btf_vmlinux_override)
5636 return cands;
5637
5638 /* now look through module BTFs, trying to still find candidates */
5639 err = load_module_btfs(obj);
5640 if (err)
5641 goto err_out;
5642
5643 for (i = 0; i < obj->btf_module_cnt; i++) {
5644 err = bpf_core_add_cands(&local_cand, local_essent_len,
5645 obj->btf_modules[i].btf,
5646 obj->btf_modules[i].name,
6a886de0 5647 btf__type_cnt(obj->btf_vmlinux),
4f33a53d
AN
5648 cands);
5649 if (err)
5650 goto err_out;
0f7515ca
AN
5651 }
5652
5653 return cands;
ddc7c304 5654err_out:
4f33a53d 5655 bpf_core_free_cands(cands);
ddc7c304
AN
5656 return ERR_PTR(err);
5657}
5658
3fc32f40
AN
5659/* Check local and target types for compatibility. This check is used for
5660 * type-based CO-RE relocations and follow slightly different rules than
5661 * field-based relocations. This function assumes that root types were already
5662 * checked for name match. Beyond that initial root-level name check, names
5663 * are completely ignored. Compatibility rules are as follows:
5664 * - any two STRUCTs/UNIONs/FWDs/ENUMs/INTs are considered compatible, but
5665 * kind should match for local and target types (i.e., STRUCT is not
5666 * compatible with UNION);
5667 * - for ENUMs, the size is ignored;
5668 * - for INT, size and signedness are ignored;
5669 * - for ARRAY, dimensionality is ignored, element types are checked for
5670 * compatibility recursively;
5671 * - CONST/VOLATILE/RESTRICT modifiers are ignored;
5672 * - TYPEDEFs/PTRs are compatible if types they pointing to are compatible;
5673 * - FUNC_PROTOs are compatible if they have compatible signature: same
5674 * number of input args and compatible return and argument types.
5675 * These rules are not set in stone and probably will be adjusted as we get
5676 * more experience with using BPF CO-RE relocations.
5677 */
b0588390
AS
5678int bpf_core_types_are_compat(const struct btf *local_btf, __u32 local_id,
5679 const struct btf *targ_btf, __u32 targ_id)
3fc32f40 5680{
fd75733d 5681 return __bpf_core_types_are_compat(local_btf, local_id, targ_btf, targ_id, 32);
3fc32f40
AN
5682}
5683
ec6209c8
DM
5684int bpf_core_types_match(const struct btf *local_btf, __u32 local_id,
5685 const struct btf *targ_btf, __u32 targ_id)
5686{
5687 return __bpf_core_types_match(local_btf, local_id, targ_btf, targ_id, false, 32);
5688}
5689
c302378b 5690static size_t bpf_core_hash_fn(const long key, void *ctx)
ddc7c304 5691{
c302378b 5692 return key;
ddc7c304
AN
5693}
5694
c302378b 5695static bool bpf_core_equal_fn(const long k1, const long k2, void *ctx)
ddc7c304
AN
5696{
5697 return k1 == k2;
5698}
5699
d0e92887
AS
5700static int record_relo_core(struct bpf_program *prog,
5701 const struct bpf_core_relo *core_relo, int insn_idx)
5702{
5703 struct reloc_desc *relos, *relo;
5704
5705 relos = libbpf_reallocarray(prog->reloc_desc,
5706 prog->nr_reloc + 1, sizeof(*relos));
5707 if (!relos)
5708 return -ENOMEM;
5709 relo = &relos[prog->nr_reloc];
5710 relo->type = RELO_CORE;
5711 relo->insn_idx = insn_idx;
5712 relo->core_relo = core_relo;
5713 prog->reloc_desc = relos;
5714 prog->nr_reloc++;
5715 return 0;
5716}
5717
9fdc4273
AN
5718static const struct bpf_core_relo *find_relo_core(struct bpf_program *prog, int insn_idx)
5719{
5720 struct reloc_desc *relo;
5721 int i;
5722
5723 for (i = 0; i < prog->nr_reloc; i++) {
5724 relo = &prog->reloc_desc[i];
5725 if (relo->type != RELO_CORE || relo->insn_idx != insn_idx)
5726 continue;
5727
5728 return relo->core_relo;
5729 }
5730
5731 return NULL;
5732}
5733
adb8fa19
MV
5734static int bpf_core_resolve_relo(struct bpf_program *prog,
5735 const struct bpf_core_relo *relo,
5736 int relo_idx,
5737 const struct btf *local_btf,
5738 struct hashmap *cand_cache,
5739 struct bpf_core_relo_res *targ_res)
3ee4f533 5740{
78c1f8d0 5741 struct bpf_core_spec specs_scratch[3] = {};
301ba4d7 5742 struct bpf_core_cand_list *cands = NULL;
3ee4f533
AS
5743 const char *prog_name = prog->name;
5744 const struct btf_type *local_type;
5745 const char *local_name;
5746 __u32 local_id = relo->type_id;
adb8fa19 5747 int err;
3ee4f533
AS
5748
5749 local_type = btf__type_by_id(local_btf, local_id);
5750 if (!local_type)
5751 return -EINVAL;
5752
5753 local_name = btf__name_by_offset(local_btf, local_type->name_off);
5754 if (!local_name)
5755 return -EINVAL;
5756
46334a0c 5757 if (relo->kind != BPF_CORE_TYPE_ID_LOCAL &&
c302378b 5758 !hashmap__find(cand_cache, local_id, &cands)) {
3ee4f533
AS
5759 cands = bpf_core_find_cands(prog->obj, local_btf, local_id);
5760 if (IS_ERR(cands)) {
5761 pr_warn("prog '%s': relo #%d: target candidate search failed for [%d] %s %s: %ld\n",
5762 prog_name, relo_idx, local_id, btf_kind_str(local_type),
5763 local_name, PTR_ERR(cands));
5764 return PTR_ERR(cands);
5765 }
c302378b 5766 err = hashmap__set(cand_cache, local_id, cands, NULL, NULL);
3ee4f533
AS
5767 if (err) {
5768 bpf_core_free_cands(cands);
5769 return err;
5770 }
5771 }
5772
adb8fa19
MV
5773 return bpf_core_calc_relo_insn(prog_name, relo, relo_idx, local_btf, cands, specs_scratch,
5774 targ_res);
3ee4f533
AS
5775}
5776
ddc7c304 5777static int
28b93c64 5778bpf_object__relocate_core(struct bpf_object *obj, const char *targ_btf_path)
ddc7c304
AN
5779{
5780 const struct btf_ext_info_sec *sec;
adb8fa19 5781 struct bpf_core_relo_res targ_res;
28b93c64 5782 const struct bpf_core_relo *rec;
ddc7c304
AN
5783 const struct btf_ext_info *seg;
5784 struct hashmap_entry *entry;
5785 struct hashmap *cand_cache = NULL;
5786 struct bpf_program *prog;
adb8fa19 5787 struct bpf_insn *insn;
ddc7c304 5788 const char *sec_name;
11d5daa8 5789 int i, err = 0, insn_idx, sec_idx, sec_num;
ddc7c304 5790
28b93c64
AN
5791 if (obj->btf_ext->core_relo_info.len == 0)
5792 return 0;
5793
0f7515ca
AN
5794 if (targ_btf_path) {
5795 obj->btf_vmlinux_override = btf__parse(targ_btf_path, NULL);
e9fc3ce9
AN
5796 err = libbpf_get_error(obj->btf_vmlinux_override);
5797 if (err) {
0f7515ca
AN
5798 pr_warn("failed to parse target BTF: %d\n", err);
5799 return err;
5800 }
ddc7c304
AN
5801 }
5802
5803 cand_cache = hashmap__new(bpf_core_hash_fn, bpf_core_equal_fn, NULL);
5804 if (IS_ERR(cand_cache)) {
5805 err = PTR_ERR(cand_cache);
5806 goto out;
5807 }
5808
28b93c64 5809 seg = &obj->btf_ext->core_relo_info;
11d5daa8 5810 sec_num = 0;
ddc7c304 5811 for_each_btf_ext_sec(seg, sec) {
11d5daa8
AN
5812 sec_idx = seg->sec_idxs[sec_num];
5813 sec_num++;
5814
ddc7c304
AN
5815 sec_name = btf__name_by_offset(obj->btf, sec->sec_name_off);
5816 if (str_is_empty(sec_name)) {
5817 err = -EINVAL;
5818 goto out;
5819 }
ddc7c304 5820
11d5daa8 5821 pr_debug("sec '%s': found %d CO-RE relocations\n", sec_name, sec->num_info);
ddc7c304
AN
5822
5823 for_each_btf_ext_rec(seg, sec, i, rec) {
adb8fa19
MV
5824 if (rec->insn_off % BPF_INSN_SZ)
5825 return -EINVAL;
db2b8b06
AN
5826 insn_idx = rec->insn_off / BPF_INSN_SZ;
5827 prog = find_prog_by_sec_insn(obj, sec_idx, insn_idx);
5828 if (!prog) {
e89d57d9
AN
5829 /* When __weak subprog is "overridden" by another instance
5830 * of the subprog from a different object file, linker still
5831 * appends all the .BTF.ext info that used to belong to that
5832 * eliminated subprogram.
5833 * This is similar to what x86-64 linker does for relocations.
5834 * So just ignore such relocations just like we ignore
5835 * subprog instructions when discovering subprograms.
5836 */
5837 pr_debug("sec '%s': skipping CO-RE relocation #%d for insn #%d belonging to eliminated weak subprogram\n",
5838 sec_name, i, insn_idx);
5839 continue;
db2b8b06 5840 }
47f7cf63
AN
5841 /* no need to apply CO-RE relocation if the program is
5842 * not going to be loaded
5843 */
a3820c48 5844 if (!prog->autoload)
47f7cf63 5845 continue;
db2b8b06 5846
adb8fa19
MV
5847 /* adjust insn_idx from section frame of reference to the local
5848 * program's frame of reference; (sub-)program code is not yet
5849 * relocated, so it's enough to just subtract in-section offset
5850 */
5851 insn_idx = insn_idx - prog->sec_insn_off;
5852 if (insn_idx >= prog->insns_cnt)
5853 return -EINVAL;
5854 insn = &prog->insns[insn_idx];
5855
185cfe83
AN
5856 err = record_relo_core(prog, rec, insn_idx);
5857 if (err) {
5858 pr_warn("prog '%s': relo #%d: failed to record relocation: %d\n",
5859 prog->name, i, err);
5860 goto out;
adb8fa19
MV
5861 }
5862
185cfe83
AN
5863 if (prog->obj->gen_loader)
5864 continue;
5865
adb8fa19 5866 err = bpf_core_resolve_relo(prog, rec, i, obj->btf, cand_cache, &targ_res);
ddc7c304 5867 if (err) {
be18010e 5868 pr_warn("prog '%s': relo #%d: failed to relocate: %d\n",
9c0f8cbd 5869 prog->name, i, err);
ddc7c304
AN
5870 goto out;
5871 }
adb8fa19
MV
5872
5873 err = bpf_core_patch_insn(prog->name, insn, insn_idx, rec, i, &targ_res);
5874 if (err) {
5875 pr_warn("prog '%s': relo #%d: failed to patch insn #%u: %d\n",
5876 prog->name, i, insn_idx, err);
5877 goto out;
5878 }
ddc7c304
AN
5879 }
5880 }
5881
5882out:
4f33a53d 5883 /* obj->btf_vmlinux and module BTFs are freed after object load */
0f7515ca
AN
5884 btf__free(obj->btf_vmlinux_override);
5885 obj->btf_vmlinux_override = NULL;
5886
ddc7c304
AN
5887 if (!IS_ERR_OR_NULL(cand_cache)) {
5888 hashmap__for_each_entry(cand_cache, entry, i) {
c302378b 5889 bpf_core_free_cands(entry->pvalue);
ddc7c304
AN
5890 }
5891 hashmap__free(cand_cache);
5892 }
5893 return err;
5894}
5895
ec41817b 5896/* base map load ldimm64 special constant, used also for log fixup logic */
3055ddd6
AN
5897#define POISON_LDIMM64_MAP_BASE 2001000000
5898#define POISON_LDIMM64_MAP_PFX "200100"
ec41817b
AN
5899
5900static void poison_map_ldimm64(struct bpf_program *prog, int relo_idx,
5901 int insn_idx, struct bpf_insn *insn,
5902 int map_idx, const struct bpf_map *map)
5903{
5904 int i;
5905
5906 pr_debug("prog '%s': relo #%d: poisoning insn #%d that loads map #%d '%s'\n",
5907 prog->name, relo_idx, insn_idx, map_idx, map->name);
5908
5909 /* we turn single ldimm64 into two identical invalid calls */
5910 for (i = 0; i < 2; i++) {
5911 insn->code = BPF_JMP | BPF_CALL;
5912 insn->dst_reg = 0;
5913 insn->src_reg = 0;
5914 insn->off = 0;
5915 /* if this instruction is reachable (not a dead code),
5916 * verifier will complain with something like:
5917 * invalid func unknown#2001000123
5918 * where lower 123 is map index into obj->maps[] array
5919 */
3055ddd6 5920 insn->imm = POISON_LDIMM64_MAP_BASE + map_idx;
ec41817b
AN
5921
5922 insn++;
5923 }
5924}
5925
05b6f766
AN
5926/* unresolved kfunc call special constant, used also for log fixup logic */
5927#define POISON_CALL_KFUNC_BASE 2002000000
5928#define POISON_CALL_KFUNC_PFX "2002"
5929
5930static void poison_kfunc_call(struct bpf_program *prog, int relo_idx,
5931 int insn_idx, struct bpf_insn *insn,
5932 int ext_idx, const struct extern_desc *ext)
5933{
5934 pr_debug("prog '%s': relo #%d: poisoning insn #%d that calls kfunc '%s'\n",
5935 prog->name, relo_idx, insn_idx, ext->name);
5936
5937 /* we turn kfunc call into invalid helper call with identifiable constant */
5938 insn->code = BPF_JMP | BPF_CALL;
5939 insn->dst_reg = 0;
5940 insn->src_reg = 0;
5941 insn->off = 0;
5942 /* if this instruction is reachable (not a dead code),
5943 * verifier will complain with something like:
5944 * invalid func unknown#2001000123
5945 * where lower 123 is extern index into obj->externs[] array
5946 */
5947 insn->imm = POISON_CALL_KFUNC_BASE + ext_idx;
5948}
5949
c3c55696
AN
5950/* Relocate data references within program code:
5951 * - map references;
5952 * - global variable references;
5953 * - extern references.
5954 */
48cca7e4 5955static int
c3c55696 5956bpf_object__relocate_data(struct bpf_object *obj, struct bpf_program *prog)
8a47a6c5 5957{
c3c55696 5958 int i;
8a47a6c5
WN
5959
5960 for (i = 0; i < prog->nr_reloc; i++) {
53f8dd43 5961 struct reloc_desc *relo = &prog->reloc_desc[i];
166750bc 5962 struct bpf_insn *insn = &prog->insns[relo->insn_idx];
ec41817b 5963 const struct bpf_map *map;
2e33efe3 5964 struct extern_desc *ext;
8a47a6c5 5965
166750bc
AN
5966 switch (relo->type) {
5967 case RELO_LD64:
ec41817b 5968 map = &obj->maps[relo->map_idx];
e2fa0156
AS
5969 if (obj->gen_loader) {
5970 insn[0].src_reg = BPF_PSEUDO_MAP_IDX;
5971 insn[0].imm = relo->map_idx;
ec41817b 5972 } else if (map->autocreate) {
e2fa0156 5973 insn[0].src_reg = BPF_PSEUDO_MAP_FD;
ec41817b
AN
5974 insn[0].imm = map->fd;
5975 } else {
5976 poison_map_ldimm64(prog, i, relo->insn_idx, insn,
5977 relo->map_idx, map);
e2fa0156 5978 }
166750bc
AN
5979 break;
5980 case RELO_DATA:
ec41817b 5981 map = &obj->maps[relo->map_idx];
166750bc 5982 insn[1].imm = insn[0].imm + relo->sym_off;
e2fa0156
AS
5983 if (obj->gen_loader) {
5984 insn[0].src_reg = BPF_PSEUDO_MAP_IDX_VALUE;
5985 insn[0].imm = relo->map_idx;
ec41817b 5986 } else if (map->autocreate) {
e2fa0156 5987 insn[0].src_reg = BPF_PSEUDO_MAP_VALUE;
ec41817b
AN
5988 insn[0].imm = map->fd;
5989 } else {
5990 poison_map_ldimm64(prog, i, relo->insn_idx, insn,
5991 relo->map_idx, map);
e2fa0156 5992 }
166750bc 5993 break;
a18f7214 5994 case RELO_EXTERN_LD64:
3055ddd6 5995 ext = &obj->externs[relo->ext_idx];
1c0c7074 5996 if (ext->type == EXT_KCFG) {
e2fa0156
AS
5997 if (obj->gen_loader) {
5998 insn[0].src_reg = BPF_PSEUDO_MAP_IDX_VALUE;
5999 insn[0].imm = obj->kconfig_map_idx;
6000 } else {
6001 insn[0].src_reg = BPF_PSEUDO_MAP_VALUE;
6002 insn[0].imm = obj->maps[obj->kconfig_map_idx].fd;
6003 }
1c0c7074
AN
6004 insn[1].imm = ext->kcfg.data_off;
6005 } else /* EXT_KSYM */ {
2211c825 6006 if (ext->ksym.type_id && ext->is_set) { /* typed ksyms */
d370bbe1 6007 insn[0].src_reg = BPF_PSEUDO_BTF_ID;
284d2587
AN
6008 insn[0].imm = ext->ksym.kernel_btf_id;
6009 insn[1].imm = ext->ksym.kernel_btf_obj_fd;
2211c825 6010 } else { /* typeless ksyms or unresolved typed ksyms */
d370bbe1
HL
6011 insn[0].imm = (__u32)ext->ksym.addr;
6012 insn[1].imm = ext->ksym.addr >> 32;
6013 }
1c0c7074 6014 }
166750bc 6015 break;
a18f7214 6016 case RELO_EXTERN_CALL:
3055ddd6 6017 ext = &obj->externs[relo->ext_idx];
5bd022ec 6018 insn[0].src_reg = BPF_PSEUDO_KFUNC_CALL;
466b2e13
KKD
6019 if (ext->is_set) {
6020 insn[0].imm = ext->ksym.kernel_btf_id;
6021 insn[0].off = ext->ksym.btf_fd_idx;
05b6f766
AN
6022 } else { /* unresolved weak kfunc call */
6023 poison_kfunc_call(prog, i, relo->insn_idx, insn,
6024 relo->ext_idx, ext);
466b2e13 6025 }
5bd022ec 6026 break;
53eddb5e 6027 case RELO_SUBPROG_ADDR:
b1268826
AS
6028 if (insn[0].src_reg != BPF_PSEUDO_FUNC) {
6029 pr_warn("prog '%s': relo #%d: bad insn\n",
6030 prog->name, i);
6031 return -EINVAL;
6032 }
6033 /* handled already */
53eddb5e 6034 break;
166750bc 6035 case RELO_CALL:
b1268826 6036 /* handled already */
166750bc 6037 break;
d0e92887
AS
6038 case RELO_CORE:
6039 /* will be handled by bpf_program_record_relos() */
6040 break;
166750bc 6041 default:
9c0f8cbd
AN
6042 pr_warn("prog '%s': relo #%d: bad relo type %d\n",
6043 prog->name, i, relo->type);
166750bc 6044 return -EINVAL;
8a47a6c5 6045 }
8a47a6c5
WN
6046 }
6047
c3c55696
AN
6048 return 0;
6049}
6050
8505e870
AN
6051static int adjust_prog_btf_ext_info(const struct bpf_object *obj,
6052 const struct bpf_program *prog,
6053 const struct btf_ext_info *ext_info,
6054 void **prog_info, __u32 *prog_rec_cnt,
6055 __u32 *prog_rec_sz)
6056{
6057 void *copy_start = NULL, *copy_end = NULL;
6058 void *rec, *rec_end, *new_prog_info;
6059 const struct btf_ext_info_sec *sec;
6060 size_t old_sz, new_sz;
11d5daa8 6061 int i, sec_num, sec_idx, off_adj;
8505e870 6062
11d5daa8 6063 sec_num = 0;
8505e870 6064 for_each_btf_ext_sec(ext_info, sec) {
11d5daa8
AN
6065 sec_idx = ext_info->sec_idxs[sec_num];
6066 sec_num++;
6067 if (prog->sec_idx != sec_idx)
8505e870
AN
6068 continue;
6069
6070 for_each_btf_ext_rec(ext_info, sec, i, rec) {
6071 __u32 insn_off = *(__u32 *)rec / BPF_INSN_SZ;
6072
6073 if (insn_off < prog->sec_insn_off)
6074 continue;
6075 if (insn_off >= prog->sec_insn_off + prog->sec_insn_cnt)
6076 break;
6077
6078 if (!copy_start)
6079 copy_start = rec;
6080 copy_end = rec + ext_info->rec_size;
6081 }
6082
6083 if (!copy_start)
6084 return -ENOENT;
6085
6086 /* append func/line info of a given (sub-)program to the main
6087 * program func/line info
6088 */
8eb62958 6089 old_sz = (size_t)(*prog_rec_cnt) * ext_info->rec_size;
8505e870
AN
6090 new_sz = old_sz + (copy_end - copy_start);
6091 new_prog_info = realloc(*prog_info, new_sz);
6092 if (!new_prog_info)
6093 return -ENOMEM;
6094 *prog_info = new_prog_info;
6095 *prog_rec_cnt = new_sz / ext_info->rec_size;
6096 memcpy(new_prog_info + old_sz, copy_start, copy_end - copy_start);
6097
6098 /* Kernel instruction offsets are in units of 8-byte
6099 * instructions, while .BTF.ext instruction offsets generated
6100 * by Clang are in units of bytes. So convert Clang offsets
6101 * into kernel offsets and adjust offset according to program
6102 * relocated position.
6103 */
6104 off_adj = prog->sub_insn_off - prog->sec_insn_off;
6105 rec = new_prog_info + old_sz;
6106 rec_end = new_prog_info + new_sz;
6107 for (; rec < rec_end; rec += ext_info->rec_size) {
6108 __u32 *insn_off = rec;
6109
6110 *insn_off = *insn_off / BPF_INSN_SZ + off_adj;
6111 }
6112 *prog_rec_sz = ext_info->rec_size;
6113 return 0;
6114 }
6115
6116 return -ENOENT;
6117}
6118
6119static int
6120reloc_prog_func_and_line_info(const struct bpf_object *obj,
6121 struct bpf_program *main_prog,
6122 const struct bpf_program *prog)
6123{
6124 int err;
6125
6126 /* no .BTF.ext relocation if .BTF.ext is missing or kernel doesn't
2f38fe68 6127 * support func/line info
8505e870 6128 */
9ca1f56a 6129 if (!obj->btf_ext || !kernel_supports(obj, FEAT_BTF_FUNC))
8505e870
AN
6130 return 0;
6131
6132 /* only attempt func info relocation if main program's func_info
6133 * relocation was successful
6134 */
6135 if (main_prog != prog && !main_prog->func_info)
6136 goto line_info;
6137
6138 err = adjust_prog_btf_ext_info(obj, prog, &obj->btf_ext->func_info,
6139 &main_prog->func_info,
6140 &main_prog->func_info_cnt,
6141 &main_prog->func_info_rec_size);
6142 if (err) {
6143 if (err != -ENOENT) {
6144 pr_warn("prog '%s': error relocating .BTF.ext function info: %d\n",
6145 prog->name, err);
6146 return err;
6147 }
6148 if (main_prog->func_info) {
6149 /*
6150 * Some info has already been found but has problem
6151 * in the last btf_ext reloc. Must have to error out.
6152 */
6153 pr_warn("prog '%s': missing .BTF.ext function info.\n", prog->name);
6154 return err;
6155 }
6156 /* Have problem loading the very first info. Ignore the rest. */
6157 pr_warn("prog '%s': missing .BTF.ext function info for the main program, skipping all of .BTF.ext func info.\n",
6158 prog->name);
6159 }
6160
6161line_info:
6162 /* don't relocate line info if main program's relocation failed */
6163 if (main_prog != prog && !main_prog->line_info)
6164 return 0;
6165
6166 err = adjust_prog_btf_ext_info(obj, prog, &obj->btf_ext->line_info,
6167 &main_prog->line_info,
6168 &main_prog->line_info_cnt,
6169 &main_prog->line_info_rec_size);
6170 if (err) {
6171 if (err != -ENOENT) {
6172 pr_warn("prog '%s': error relocating .BTF.ext line info: %d\n",
6173 prog->name, err);
6174 return err;
6175 }
6176 if (main_prog->line_info) {
6177 /*
6178 * Some info has already been found but has problem
6179 * in the last btf_ext reloc. Must have to error out.
6180 */
6181 pr_warn("prog '%s': missing .BTF.ext line info.\n", prog->name);
6182 return err;
6183 }
6184 /* Have problem loading the very first info. Ignore the rest. */
6185 pr_warn("prog '%s': missing .BTF.ext line info for the main program, skipping all of .BTF.ext line info.\n",
6186 prog->name);
6187 }
6188 return 0;
6189}
6190
c3c55696
AN
6191static int cmp_relo_by_insn_idx(const void *key, const void *elem)
6192{
6193 size_t insn_idx = *(const size_t *)key;
6194 const struct reloc_desc *relo = elem;
6195
6196 if (insn_idx == relo->insn_idx)
6197 return 0;
6198 return insn_idx < relo->insn_idx ? -1 : 1;
6199}
6200
6201static struct reloc_desc *find_prog_insn_relo(const struct bpf_program *prog, size_t insn_idx)
6202{
2a6a9bf2
AN
6203 if (!prog->nr_reloc)
6204 return NULL;
c3c55696
AN
6205 return bsearch(&insn_idx, prog->reloc_desc, prog->nr_reloc,
6206 sizeof(*prog->reloc_desc), cmp_relo_by_insn_idx);
6207}
6208
b1268826
AS
6209static int append_subprog_relos(struct bpf_program *main_prog, struct bpf_program *subprog)
6210{
6211 int new_cnt = main_prog->nr_reloc + subprog->nr_reloc;
6212 struct reloc_desc *relos;
6213 int i;
6214
6215 if (main_prog == subprog)
6216 return 0;
6217 relos = libbpf_reallocarray(main_prog->reloc_desc, new_cnt, sizeof(*relos));
8a0260db
AN
6218 /* if new count is zero, reallocarray can return a valid NULL result;
6219 * in this case the previous pointer will be freed, so we *have to*
6220 * reassign old pointer to the new value (even if it's NULL)
6221 */
6222 if (!relos && new_cnt)
b1268826 6223 return -ENOMEM;
2a6a9bf2
AN
6224 if (subprog->nr_reloc)
6225 memcpy(relos + main_prog->nr_reloc, subprog->reloc_desc,
6226 sizeof(*relos) * subprog->nr_reloc);
b1268826
AS
6227
6228 for (i = main_prog->nr_reloc; i < new_cnt; i++)
6229 relos[i].insn_idx += subprog->sub_insn_off;
6230 /* After insn_idx adjustment the 'relos' array is still sorted
6231 * by insn_idx and doesn't break bsearch.
6232 */
6233 main_prog->reloc_desc = relos;
6234 main_prog->nr_reloc = new_cnt;
6235 return 0;
6236}
6237
6c918709
KKD
6238static int
6239bpf_object__append_subprog_code(struct bpf_object *obj, struct bpf_program *main_prog,
6240 struct bpf_program *subprog)
6241{
6242 struct bpf_insn *insns;
6243 size_t new_cnt;
6244 int err;
6245
6246 subprog->sub_insn_off = main_prog->insns_cnt;
6247
6248 new_cnt = main_prog->insns_cnt + subprog->insns_cnt;
6249 insns = libbpf_reallocarray(main_prog->insns, new_cnt, sizeof(*insns));
6250 if (!insns) {
6251 pr_warn("prog '%s': failed to realloc prog code\n", main_prog->name);
6252 return -ENOMEM;
6253 }
6254 main_prog->insns = insns;
6255 main_prog->insns_cnt = new_cnt;
6256
6257 memcpy(main_prog->insns + subprog->sub_insn_off, subprog->insns,
6258 subprog->insns_cnt * sizeof(*insns));
6259
6260 pr_debug("prog '%s': added %zu insns from sub-prog '%s'\n",
6261 main_prog->name, subprog->insns_cnt, subprog->name);
6262
6263 /* The subprog insns are now appended. Append its relos too. */
6264 err = append_subprog_relos(main_prog, subprog);
6265 if (err)
6266 return err;
6267 return 0;
6268}
6269
c3c55696
AN
6270static int
6271bpf_object__reloc_code(struct bpf_object *obj, struct bpf_program *main_prog,
6272 struct bpf_program *prog)
6273{
7e2925f6 6274 size_t sub_insn_idx, insn_idx;
c3c55696 6275 struct bpf_program *subprog;
c3c55696 6276 struct reloc_desc *relo;
7e2925f6 6277 struct bpf_insn *insn;
c3c55696
AN
6278 int err;
6279
6280 err = reloc_prog_func_and_line_info(obj, main_prog, prog);
6281 if (err)
6282 return err;
6283
6284 for (insn_idx = 0; insn_idx < prog->sec_insn_cnt; insn_idx++) {
6285 insn = &main_prog->insns[prog->sub_insn_off + insn_idx];
53eddb5e 6286 if (!insn_is_subprog_call(insn) && !insn_is_pseudo_func(insn))
c3c55696
AN
6287 continue;
6288
6289 relo = find_prog_insn_relo(prog, insn_idx);
a18f7214 6290 if (relo && relo->type == RELO_EXTERN_CALL)
b1268826
AS
6291 /* kfunc relocations will be handled later
6292 * in bpf_object__relocate_data()
6293 */
6294 continue;
53eddb5e 6295 if (relo && relo->type != RELO_CALL && relo->type != RELO_SUBPROG_ADDR) {
c3c55696
AN
6296 pr_warn("prog '%s': unexpected relo for insn #%zu, type %d\n",
6297 prog->name, insn_idx, relo->type);
6298 return -LIBBPF_ERRNO__RELOC;
6299 }
6300 if (relo) {
6301 /* sub-program instruction index is a combination of
6302 * an offset of a symbol pointed to by relocation and
6303 * call instruction's imm field; for global functions,
6304 * call always has imm = -1, but for static functions
6305 * relocation is against STT_SECTION and insn->imm
6306 * points to a start of a static function
53eddb5e
YS
6307 *
6308 * for subprog addr relocation, the relo->sym_off + insn->imm is
6309 * the byte offset in the corresponding section.
c3c55696 6310 */
53eddb5e
YS
6311 if (relo->type == RELO_CALL)
6312 sub_insn_idx = relo->sym_off / BPF_INSN_SZ + insn->imm + 1;
6313 else
6314 sub_insn_idx = (relo->sym_off + insn->imm) / BPF_INSN_SZ;
6315 } else if (insn_is_pseudo_func(insn)) {
6316 /*
6317 * RELO_SUBPROG_ADDR relo is always emitted even if both
6318 * functions are in the same section, so it shouldn't reach here.
6319 */
6320 pr_warn("prog '%s': missing subprog addr relo for insn #%zu\n",
6321 prog->name, insn_idx);
6322 return -LIBBPF_ERRNO__RELOC;
c3c55696
AN
6323 } else {
6324 /* if subprogram call is to a static function within
6325 * the same ELF section, there won't be any relocation
6326 * emitted, but it also means there is no additional
6327 * offset necessary, insns->imm is relative to
6328 * instruction's original position within the section
6329 */
6330 sub_insn_idx = prog->sec_insn_off + insn_idx + insn->imm + 1;
6331 }
6332
6333 /* we enforce that sub-programs should be in .text section */
6334 subprog = find_prog_by_sec_insn(obj, obj->efile.text_shndx, sub_insn_idx);
6335 if (!subprog) {
6336 pr_warn("prog '%s': no .text section found yet sub-program call exists\n",
6337 prog->name);
6338 return -LIBBPF_ERRNO__RELOC;
6339 }
6340
6341 /* if it's the first call instruction calling into this
6342 * subprogram (meaning this subprog hasn't been processed
6343 * yet) within the context of current main program:
6344 * - append it at the end of main program's instructions blog;
6345 * - process is recursively, while current program is put on hold;
6346 * - if that subprogram calls some other not yet processes
6347 * subprogram, same thing will happen recursively until
6348 * there are no more unprocesses subprograms left to append
6349 * and relocate.
6350 */
6351 if (subprog->sub_insn_off == 0) {
6c918709 6352 err = bpf_object__append_subprog_code(obj, main_prog, subprog);
b1268826
AS
6353 if (err)
6354 return err;
c3c55696
AN
6355 err = bpf_object__reloc_code(obj, main_prog, subprog);
6356 if (err)
6357 return err;
6358 }
6359
6360 /* main_prog->insns memory could have been re-allocated, so
6361 * calculate pointer again
6362 */
6363 insn = &main_prog->insns[prog->sub_insn_off + insn_idx];
6364 /* calculate correct instruction position within current main
6365 * prog; each main prog can have a different set of
6366 * subprograms appended (potentially in different order as
6367 * well), so position of any subprog can be different for
e3ba8e4e
KM
6368 * different main programs
6369 */
c3c55696
AN
6370 insn->imm = subprog->sub_insn_off - (prog->sub_insn_off + insn_idx) - 1;
6371
c3c55696
AN
6372 pr_debug("prog '%s': insn #%zu relocated, imm %d points to subprog '%s' (now at %zu offset)\n",
6373 prog->name, insn_idx, insn->imm, subprog->name, subprog->sub_insn_off);
6374 }
6375
6376 return 0;
6377}
6378
6379/*
6380 * Relocate sub-program calls.
6381 *
6382 * Algorithm operates as follows. Each entry-point BPF program (referred to as
6383 * main prog) is processed separately. For each subprog (non-entry functions,
6384 * that can be called from either entry progs or other subprogs) gets their
6385 * sub_insn_off reset to zero. This serves as indicator that this subprogram
6386 * hasn't been yet appended and relocated within current main prog. Once its
6387 * relocated, sub_insn_off will point at the position within current main prog
6388 * where given subprog was appended. This will further be used to relocate all
6389 * the call instructions jumping into this subprog.
6390 *
6391 * We start with main program and process all call instructions. If the call
6392 * is into a subprog that hasn't been processed (i.e., subprog->sub_insn_off
6393 * is zero), subprog instructions are appended at the end of main program's
6394 * instruction array. Then main program is "put on hold" while we recursively
6395 * process newly appended subprogram. If that subprogram calls into another
6396 * subprogram that hasn't been appended, new subprogram is appended again to
6397 * the *main* prog's instructions (subprog's instructions are always left
6398 * untouched, as they need to be in unmodified state for subsequent main progs
6399 * and subprog instructions are always sent only as part of a main prog) and
6400 * the process continues recursively. Once all the subprogs called from a main
6401 * prog or any of its subprogs are appended (and relocated), all their
6402 * positions within finalized instructions array are known, so it's easy to
6403 * rewrite call instructions with correct relative offsets, corresponding to
6404 * desired target subprog.
6405 *
6406 * Its important to realize that some subprogs might not be called from some
6407 * main prog and any of its called/used subprogs. Those will keep their
6408 * subprog->sub_insn_off as zero at all times and won't be appended to current
6409 * main prog and won't be relocated within the context of current main prog.
6410 * They might still be used from other main progs later.
6411 *
6412 * Visually this process can be shown as below. Suppose we have two main
6413 * programs mainA and mainB and BPF object contains three subprogs: subA,
6414 * subB, and subC. mainA calls only subA, mainB calls only subC, but subA and
6415 * subC both call subB:
6416 *
6417 * +--------+ +-------+
6418 * | v v |
6419 * +--+---+ +--+-+-+ +---+--+
6420 * | subA | | subB | | subC |
6421 * +--+---+ +------+ +---+--+
6422 * ^ ^
6423 * | |
6424 * +---+-------+ +------+----+
6425 * | mainA | | mainB |
6426 * +-----------+ +-----------+
6427 *
6428 * We'll start relocating mainA, will find subA, append it and start
6429 * processing sub A recursively:
6430 *
6431 * +-----------+------+
6432 * | mainA | subA |
6433 * +-----------+------+
6434 *
6435 * At this point we notice that subB is used from subA, so we append it and
6436 * relocate (there are no further subcalls from subB):
6437 *
6438 * +-----------+------+------+
6439 * | mainA | subA | subB |
6440 * +-----------+------+------+
6441 *
6442 * At this point, we relocate subA calls, then go one level up and finish with
6443 * relocatin mainA calls. mainA is done.
6444 *
6445 * For mainB process is similar but results in different order. We start with
6446 * mainB and skip subA and subB, as mainB never calls them (at least
6447 * directly), but we see subC is needed, so we append and start processing it:
6448 *
6449 * +-----------+------+
6450 * | mainB | subC |
6451 * +-----------+------+
6452 * Now we see subC needs subB, so we go back to it, append and relocate it:
6453 *
6454 * +-----------+------+------+
6455 * | mainB | subC | subB |
6456 * +-----------+------+------+
6457 *
6458 * At this point we unwind recursion, relocate calls in subC, then in mainB.
6459 */
6460static int
6461bpf_object__relocate_calls(struct bpf_object *obj, struct bpf_program *prog)
6462{
6463 struct bpf_program *subprog;
d3d93e34 6464 int i, err;
c3c55696 6465
c3c55696
AN
6466 /* mark all subprogs as not relocated (yet) within the context of
6467 * current main program
6468 */
6469 for (i = 0; i < obj->nr_programs; i++) {
6470 subprog = &obj->programs[i];
6471 if (!prog_is_subprog(obj, subprog))
6472 continue;
6473
6474 subprog->sub_insn_off = 0;
c3c55696
AN
6475 }
6476
6477 err = bpf_object__reloc_code(obj, prog, prog);
6478 if (err)
6479 return err;
6480
8a47a6c5
WN
6481 return 0;
6482}
6483
67234743
AS
6484static void
6485bpf_object__free_relocs(struct bpf_object *obj)
6486{
6487 struct bpf_program *prog;
6488 int i;
6489
6490 /* free up relocation descriptors */
6491 for (i = 0; i < obj->nr_programs; i++) {
6492 prog = &obj->programs[i];
6493 zfree(&prog->reloc_desc);
6494 prog->nr_reloc = 0;
6495 }
6496}
6497
d0e92887
AS
6498static int cmp_relocs(const void *_a, const void *_b)
6499{
6500 const struct reloc_desc *a = _a;
6501 const struct reloc_desc *b = _b;
6502
6503 if (a->insn_idx != b->insn_idx)
6504 return a->insn_idx < b->insn_idx ? -1 : 1;
6505
6506 /* no two relocations should have the same insn_idx, but ... */
6507 if (a->type != b->type)
6508 return a->type < b->type ? -1 : 1;
6509
6510 return 0;
6511}
6512
6513static void bpf_object__sort_relos(struct bpf_object *obj)
6514{
6515 int i;
6516
6517 for (i = 0; i < obj->nr_programs; i++) {
6518 struct bpf_program *p = &obj->programs[i];
6519
6520 if (!p->nr_reloc)
6521 continue;
6522
6523 qsort(p->reloc_desc, p->nr_reloc, sizeof(*p->reloc_desc), cmp_relocs);
6524 }
6525}
6526
fb03be7c
AN
6527static int bpf_prog_assign_exc_cb(struct bpf_object *obj, struct bpf_program *prog)
6528{
6529 const char *str = "exception_callback:";
6530 size_t pfx_len = strlen(str);
6531 int i, j, n;
6532
6533 if (!obj->btf || !kernel_supports(obj, FEAT_BTF_DECL_TAG))
6534 return 0;
6535
6536 n = btf__type_cnt(obj->btf);
6537 for (i = 1; i < n; i++) {
6538 const char *name;
6539 struct btf_type *t;
6540
6541 t = btf_type_by_id(obj->btf, i);
6542 if (!btf_is_decl_tag(t) || btf_decl_tag(t)->component_idx != -1)
6543 continue;
6544
6545 name = btf__str_by_offset(obj->btf, t->name_off);
6546 if (strncmp(name, str, pfx_len) != 0)
6547 continue;
6548
6549 t = btf_type_by_id(obj->btf, t->type);
6550 if (!btf_is_func(t) || btf_func_linkage(t) != BTF_FUNC_GLOBAL) {
6551 pr_warn("prog '%s': exception_callback:<value> decl tag not applied to the main program\n",
6552 prog->name);
6553 return -EINVAL;
6554 }
6555 if (strcmp(prog->name, btf__str_by_offset(obj->btf, t->name_off)) != 0)
6556 continue;
6557 /* Multiple callbacks are specified for the same prog,
6558 * the verifier will eventually return an error for this
6559 * case, hence simply skip appending a subprog.
6560 */
6561 if (prog->exception_cb_idx >= 0) {
6562 prog->exception_cb_idx = -1;
6563 break;
6564 }
6565
6566 name += pfx_len;
6567 if (str_is_empty(name)) {
6568 pr_warn("prog '%s': exception_callback:<value> decl tag contains empty value\n",
6569 prog->name);
6570 return -EINVAL;
6571 }
6572
6573 for (j = 0; j < obj->nr_programs; j++) {
6574 struct bpf_program *subprog = &obj->programs[j];
6575
6576 if (!prog_is_subprog(obj, subprog))
6577 continue;
6578 if (strcmp(name, subprog->name) != 0)
6579 continue;
6580 /* Enforce non-hidden, as from verifier point of
6581 * view it expects global functions, whereas the
6582 * mark_btf_static fixes up linkage as static.
6583 */
6584 if (!subprog->sym_global || subprog->mark_btf_static) {
6585 pr_warn("prog '%s': exception callback %s must be a global non-hidden function\n",
6586 prog->name, subprog->name);
6587 return -EINVAL;
6588 }
6589 /* Let's see if we already saw a static exception callback with the same name */
6590 if (prog->exception_cb_idx >= 0) {
6591 pr_warn("prog '%s': multiple subprogs with same name as exception callback '%s'\n",
6592 prog->name, subprog->name);
6593 return -EINVAL;
6594 }
6595 prog->exception_cb_idx = j;
6596 break;
6597 }
6598
6599 if (prog->exception_cb_idx >= 0)
6600 continue;
6601
6602 pr_warn("prog '%s': cannot find exception callback '%s'\n", prog->name, name);
6603 return -ENOENT;
6604 }
6605
6606 return 0;
6607}
6608
2f38fe68
AN
6609static struct {
6610 enum bpf_prog_type prog_type;
6611 const char *ctx_name;
6612} global_ctx_map[] = {
6613 { BPF_PROG_TYPE_CGROUP_DEVICE, "bpf_cgroup_dev_ctx" },
6614 { BPF_PROG_TYPE_CGROUP_SKB, "__sk_buff" },
6615 { BPF_PROG_TYPE_CGROUP_SOCK, "bpf_sock" },
6616 { BPF_PROG_TYPE_CGROUP_SOCK_ADDR, "bpf_sock_addr" },
6617 { BPF_PROG_TYPE_CGROUP_SOCKOPT, "bpf_sockopt" },
6618 { BPF_PROG_TYPE_CGROUP_SYSCTL, "bpf_sysctl" },
6619 { BPF_PROG_TYPE_FLOW_DISSECTOR, "__sk_buff" },
6620 { BPF_PROG_TYPE_KPROBE, "bpf_user_pt_regs_t" },
6621 { BPF_PROG_TYPE_LWT_IN, "__sk_buff" },
6622 { BPF_PROG_TYPE_LWT_OUT, "__sk_buff" },
6623 { BPF_PROG_TYPE_LWT_SEG6LOCAL, "__sk_buff" },
6624 { BPF_PROG_TYPE_LWT_XMIT, "__sk_buff" },
6625 { BPF_PROG_TYPE_NETFILTER, "bpf_nf_ctx" },
6626 { BPF_PROG_TYPE_PERF_EVENT, "bpf_perf_event_data" },
6627 { BPF_PROG_TYPE_RAW_TRACEPOINT, "bpf_raw_tracepoint_args" },
6628 { BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE, "bpf_raw_tracepoint_args" },
6629 { BPF_PROG_TYPE_SCHED_ACT, "__sk_buff" },
6630 { BPF_PROG_TYPE_SCHED_CLS, "__sk_buff" },
6631 { BPF_PROG_TYPE_SK_LOOKUP, "bpf_sk_lookup" },
6632 { BPF_PROG_TYPE_SK_MSG, "sk_msg_md" },
6633 { BPF_PROG_TYPE_SK_REUSEPORT, "sk_reuseport_md" },
6634 { BPF_PROG_TYPE_SK_SKB, "__sk_buff" },
6635 { BPF_PROG_TYPE_SOCK_OPS, "bpf_sock_ops" },
6636 { BPF_PROG_TYPE_SOCKET_FILTER, "__sk_buff" },
6637 { BPF_PROG_TYPE_XDP, "xdp_md" },
6638 /* all other program types don't have "named" context structs */
6639};
6640
9eea8faf
AN
6641/* forward declarations for arch-specific underlying types of bpf_user_pt_regs_t typedef,
6642 * for below __builtin_types_compatible_p() checks;
6643 * with this approach we don't need any extra arch-specific #ifdef guards
6644 */
6645struct pt_regs;
6646struct user_pt_regs;
6647struct user_regs_struct;
6648
76ec90a9
AN
6649static bool need_func_arg_type_fixup(const struct btf *btf, const struct bpf_program *prog,
6650 const char *subprog_name, int arg_idx,
6651 int arg_type_id, const char *ctx_name)
6652{
6653 const struct btf_type *t;
6654 const char *tname;
6655
6656 /* check if existing parameter already matches verifier expectations */
6657 t = skip_mods_and_typedefs(btf, arg_type_id, NULL);
6658 if (!btf_is_ptr(t))
6659 goto out_warn;
6660
6661 /* typedef bpf_user_pt_regs_t is a special PITA case, valid for kprobe
6662 * and perf_event programs, so check this case early on and forget
6663 * about it for subsequent checks
6664 */
6665 while (btf_is_mod(t))
6666 t = btf__type_by_id(btf, t->type);
6667 if (btf_is_typedef(t) &&
6668 (prog->type == BPF_PROG_TYPE_KPROBE || prog->type == BPF_PROG_TYPE_PERF_EVENT)) {
6669 tname = btf__str_by_offset(btf, t->name_off) ?: "<anon>";
6670 if (strcmp(tname, "bpf_user_pt_regs_t") == 0)
6671 return false; /* canonical type for kprobe/perf_event */
6672 }
6673
6674 /* now we can ignore typedefs moving forward */
6675 t = skip_mods_and_typedefs(btf, t->type, NULL);
6676
6677 /* if it's `void *`, definitely fix up BTF info */
6678 if (btf_is_void(t))
6679 return true;
6680
6681 /* if it's already proper canonical type, no need to fix up */
6682 tname = btf__str_by_offset(btf, t->name_off) ?: "<anon>";
6683 if (btf_is_struct(t) && strcmp(tname, ctx_name) == 0)
6684 return false;
6685
6686 /* special cases */
6687 switch (prog->type) {
6688 case BPF_PROG_TYPE_KPROBE:
76ec90a9
AN
6689 /* `struct pt_regs *` is expected, but we need to fix up */
6690 if (btf_is_struct(t) && strcmp(tname, "pt_regs") == 0)
6691 return true;
6692 break;
9eea8faf
AN
6693 case BPF_PROG_TYPE_PERF_EVENT:
6694 if (__builtin_types_compatible_p(bpf_user_pt_regs_t, struct pt_regs) &&
6695 btf_is_struct(t) && strcmp(tname, "pt_regs") == 0)
d7bc416a 6696 return true;
9eea8faf
AN
6697 if (__builtin_types_compatible_p(bpf_user_pt_regs_t, struct user_pt_regs) &&
6698 btf_is_struct(t) && strcmp(tname, "user_pt_regs") == 0)
d7bc416a 6699 return true;
9eea8faf
AN
6700 if (__builtin_types_compatible_p(bpf_user_pt_regs_t, struct user_regs_struct) &&
6701 btf_is_struct(t) && strcmp(tname, "user_regs_struct") == 0)
d7bc416a 6702 return true;
9eea8faf 6703 break;
76ec90a9
AN
6704 case BPF_PROG_TYPE_RAW_TRACEPOINT:
6705 case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
6706 /* allow u64* as ctx */
6707 if (btf_is_int(t) && t->size == 8)
6708 return true;
6709 break;
6710 default:
6711 break;
6712 }
6713
6714out_warn:
6715 pr_warn("prog '%s': subprog '%s' arg#%d is expected to be of `struct %s *` type\n",
6716 prog->name, subprog_name, arg_idx, ctx_name);
6717 return false;
6718}
6719
2f38fe68
AN
6720static int clone_func_btf_info(struct btf *btf, int orig_fn_id, struct bpf_program *prog)
6721{
6722 int fn_id, fn_proto_id, ret_type_id, orig_proto_id;
6723 int i, err, arg_cnt, fn_name_off, linkage;
6724 struct btf_type *fn_t, *fn_proto_t, *t;
6725 struct btf_param *p;
6726
6727 /* caller already validated FUNC -> FUNC_PROTO validity */
6728 fn_t = btf_type_by_id(btf, orig_fn_id);
6729 fn_proto_t = btf_type_by_id(btf, fn_t->type);
6730
6731 /* Note that each btf__add_xxx() operation invalidates
6732 * all btf_type and string pointers, so we need to be
6733 * very careful when cloning BTF types. BTF type
6734 * pointers have to be always refetched. And to avoid
6735 * problems with invalidated string pointers, we
6736 * add empty strings initially, then just fix up
6737 * name_off offsets in place. Offsets are stable for
6738 * existing strings, so that works out.
6739 */
6740 fn_name_off = fn_t->name_off; /* we are about to invalidate fn_t */
6741 linkage = btf_func_linkage(fn_t);
6742 orig_proto_id = fn_t->type; /* original FUNC_PROTO ID */
6743 ret_type_id = fn_proto_t->type; /* fn_proto_t will be invalidated */
6744 arg_cnt = btf_vlen(fn_proto_t);
6745
6746 /* clone FUNC_PROTO and its params */
6747 fn_proto_id = btf__add_func_proto(btf, ret_type_id);
6748 if (fn_proto_id < 0)
6749 return -EINVAL;
6750
6751 for (i = 0; i < arg_cnt; i++) {
6752 int name_off;
6753
6754 /* copy original parameter data */
6755 t = btf_type_by_id(btf, orig_proto_id);
6756 p = &btf_params(t)[i];
6757 name_off = p->name_off;
6758
6759 err = btf__add_func_param(btf, "", p->type);
6760 if (err)
6761 return err;
6762
6763 fn_proto_t = btf_type_by_id(btf, fn_proto_id);
6764 p = &btf_params(fn_proto_t)[i];
6765 p->name_off = name_off; /* use remembered str offset */
6766 }
6767
6768 /* clone FUNC now, btf__add_func() enforces non-empty name, so use
6769 * entry program's name as a placeholder, which we replace immediately
6770 * with original name_off
6771 */
6772 fn_id = btf__add_func(btf, prog->name, linkage, fn_proto_id);
6773 if (fn_id < 0)
6774 return -EINVAL;
6775
6776 fn_t = btf_type_by_id(btf, fn_id);
6777 fn_t->name_off = fn_name_off; /* reuse original string */
6778
6779 return fn_id;
6780}
6781
6782/* Check if main program or global subprog's function prototype has `arg:ctx`
6783 * argument tags, and, if necessary, substitute correct type to match what BPF
6784 * verifier would expect, taking into account specific program type. This
6785 * allows to support __arg_ctx tag transparently on old kernels that don't yet
6786 * have a native support for it in the verifier, making user's life much
6787 * easier.
6788 */
6789static int bpf_program_fixup_func_info(struct bpf_object *obj, struct bpf_program *prog)
6790{
76ec90a9 6791 const char *ctx_name = NULL, *ctx_tag = "arg:ctx", *fn_name;
2f38fe68
AN
6792 struct bpf_func_info_min *func_rec;
6793 struct btf_type *fn_t, *fn_proto_t;
6794 struct btf *btf = obj->btf;
6795 const struct btf_type *t;
6796 struct btf_param *p;
6797 int ptr_id = 0, struct_id, tag_id, orig_fn_id;
6798 int i, n, arg_idx, arg_cnt, err, rec_idx;
6799 int *orig_ids;
6800
6801 /* no .BTF.ext, no problem */
6802 if (!obj->btf_ext || !prog->func_info)
6803 return 0;
6804
01b55f4f 6805 /* don't do any fix ups if kernel natively supports __arg_ctx */
0e6d0a9d 6806 if (kernel_supports(obj, FEAT_ARG_CTX_TAG))
01b55f4f
AN
6807 return 0;
6808
2f38fe68
AN
6809 /* some BPF program types just don't have named context structs, so
6810 * this fallback mechanism doesn't work for them
6811 */
6812 for (i = 0; i < ARRAY_SIZE(global_ctx_map); i++) {
6813 if (global_ctx_map[i].prog_type != prog->type)
6814 continue;
6815 ctx_name = global_ctx_map[i].ctx_name;
6816 break;
6817 }
6818 if (!ctx_name)
6819 return 0;
6820
6821 /* remember original func BTF IDs to detect if we already cloned them */
6822 orig_ids = calloc(prog->func_info_cnt, sizeof(*orig_ids));
6823 if (!orig_ids)
6824 return -ENOMEM;
6825 for (i = 0; i < prog->func_info_cnt; i++) {
6826 func_rec = prog->func_info + prog->func_info_rec_size * i;
6827 orig_ids[i] = func_rec->type_id;
6828 }
6829
6830 /* go through each DECL_TAG with "arg:ctx" and see if it points to one
6831 * of our subprogs; if yes and subprog is global and needs adjustment,
6832 * clone and adjust FUNC -> FUNC_PROTO combo
6833 */
6834 for (i = 1, n = btf__type_cnt(btf); i < n; i++) {
6835 /* only DECL_TAG with "arg:ctx" value are interesting */
6836 t = btf__type_by_id(btf, i);
6837 if (!btf_is_decl_tag(t))
6838 continue;
6839 if (strcmp(btf__str_by_offset(btf, t->name_off), ctx_tag) != 0)
6840 continue;
6841
6842 /* only global funcs need adjustment, if at all */
6843 orig_fn_id = t->type;
6844 fn_t = btf_type_by_id(btf, orig_fn_id);
6845 if (!btf_is_func(fn_t) || btf_func_linkage(fn_t) != BTF_FUNC_GLOBAL)
6846 continue;
6847
6848 /* sanity check FUNC -> FUNC_PROTO chain, just in case */
6849 fn_proto_t = btf_type_by_id(btf, fn_t->type);
6850 if (!fn_proto_t || !btf_is_func_proto(fn_proto_t))
6851 continue;
6852
6853 /* find corresponding func_info record */
6854 func_rec = NULL;
6855 for (rec_idx = 0; rec_idx < prog->func_info_cnt; rec_idx++) {
6856 if (orig_ids[rec_idx] == t->type) {
6857 func_rec = prog->func_info + prog->func_info_rec_size * rec_idx;
6858 break;
6859 }
6860 }
6861 /* current main program doesn't call into this subprog */
6862 if (!func_rec)
6863 continue;
6864
6865 /* some more sanity checking of DECL_TAG */
6866 arg_cnt = btf_vlen(fn_proto_t);
6867 arg_idx = btf_decl_tag(t)->component_idx;
6868 if (arg_idx < 0 || arg_idx >= arg_cnt)
6869 continue;
6870
76ec90a9 6871 /* check if we should fix up argument type */
2f38fe68 6872 p = &btf_params(fn_proto_t)[arg_idx];
76ec90a9
AN
6873 fn_name = btf__str_by_offset(btf, fn_t->name_off) ?: "<anon>";
6874 if (!need_func_arg_type_fixup(btf, prog, fn_name, arg_idx, p->type, ctx_name))
6875 continue;
2f38fe68
AN
6876
6877 /* clone fn/fn_proto, unless we already did it for another arg */
6878 if (func_rec->type_id == orig_fn_id) {
6879 int fn_id;
6880
6881 fn_id = clone_func_btf_info(btf, orig_fn_id, prog);
6882 if (fn_id < 0) {
6883 err = fn_id;
6884 goto err_out;
6885 }
6886
6887 /* point func_info record to a cloned FUNC type */
6888 func_rec->type_id = fn_id;
6889 }
6890
6891 /* create PTR -> STRUCT type chain to mark PTR_TO_CTX argument;
6892 * we do it just once per main BPF program, as all global
6893 * funcs share the same program type, so need only PTR ->
6894 * STRUCT type chain
6895 */
6896 if (ptr_id == 0) {
6897 struct_id = btf__add_struct(btf, ctx_name, 0);
6898 ptr_id = btf__add_ptr(btf, struct_id);
6899 if (ptr_id < 0 || struct_id < 0) {
6900 err = -EINVAL;
6901 goto err_out;
6902 }
6903 }
6904
6905 /* for completeness, clone DECL_TAG and point it to cloned param */
6906 tag_id = btf__add_decl_tag(btf, ctx_tag, func_rec->type_id, arg_idx);
6907 if (tag_id < 0) {
6908 err = -EINVAL;
6909 goto err_out;
6910 }
6911
6912 /* all the BTF manipulations invalidated pointers, refetch them */
6913 fn_t = btf_type_by_id(btf, func_rec->type_id);
6914 fn_proto_t = btf_type_by_id(btf, fn_t->type);
6915
6916 /* fix up type ID pointed to by param */
6917 p = &btf_params(fn_proto_t)[arg_idx];
6918 p->type = ptr_id;
6919 }
6920
6921 free(orig_ids);
6922 return 0;
6923err_out:
6924 free(orig_ids);
6925 return err;
6926}
6927
6928static int bpf_object__relocate(struct bpf_object *obj, const char *targ_btf_path)
8a47a6c5
WN
6929{
6930 struct bpf_program *prog;
b1268826 6931 size_t i, j;
8a47a6c5
WN
6932 int err;
6933
ddc7c304
AN
6934 if (obj->btf_ext) {
6935 err = bpf_object__relocate_core(obj, targ_btf_path);
6936 if (err) {
be18010e
KW
6937 pr_warn("failed to perform CO-RE relocations: %d\n",
6938 err);
ddc7c304
AN
6939 return err;
6940 }
185cfe83 6941 bpf_object__sort_relos(obj);
ddc7c304 6942 }
b1268826
AS
6943
6944 /* Before relocating calls pre-process relocations and mark
6945 * few ld_imm64 instructions that points to subprogs.
6946 * Otherwise bpf_object__reloc_code() later would have to consider
6947 * all ld_imm64 insns as relocation candidates. That would
6948 * reduce relocation speed, since amount of find_prog_insn_relo()
6949 * would increase and most of them will fail to find a relo.
9173cac3
AN
6950 */
6951 for (i = 0; i < obj->nr_programs; i++) {
6952 prog = &obj->programs[i];
b1268826
AS
6953 for (j = 0; j < prog->nr_reloc; j++) {
6954 struct reloc_desc *relo = &prog->reloc_desc[j];
6955 struct bpf_insn *insn = &prog->insns[relo->insn_idx];
6956
6957 /* mark the insn, so it's recognized by insn_is_pseudo_func() */
6958 if (relo->type == RELO_SUBPROG_ADDR)
6959 insn[0].src_reg = BPF_PSEUDO_FUNC;
9173cac3 6960 }
9173cac3 6961 }
b1268826
AS
6962
6963 /* relocate subprogram calls and append used subprograms to main
c3c55696
AN
6964 * programs; each copy of subprogram code needs to be relocated
6965 * differently for each main program, because its code location might
b1268826
AS
6966 * have changed.
6967 * Append subprog relos to main programs to allow data relos to be
6968 * processed after text is completely relocated.
9173cac3 6969 */
8a47a6c5
WN
6970 for (i = 0; i < obj->nr_programs; i++) {
6971 prog = &obj->programs[i];
c3c55696
AN
6972 /* sub-program's sub-calls are relocated within the context of
6973 * its main program only
6974 */
6975 if (prog_is_subprog(obj, prog))
9173cac3 6976 continue;
a3820c48 6977 if (!prog->autoload)
16e0c35c 6978 continue;
8a47a6c5 6979
c3c55696 6980 err = bpf_object__relocate_calls(obj, prog);
8a47a6c5 6981 if (err) {
9c0f8cbd
AN
6982 pr_warn("prog '%s': failed to relocate calls: %d\n",
6983 prog->name, err);
8a47a6c5
WN
6984 return err;
6985 }
7e2925f6 6986
fb03be7c
AN
6987 err = bpf_prog_assign_exc_cb(obj, prog);
6988 if (err)
6989 return err;
7e2925f6
KKD
6990 /* Now, also append exception callback if it has not been done already. */
6991 if (prog->exception_cb_idx >= 0) {
6992 struct bpf_program *subprog = &obj->programs[prog->exception_cb_idx];
6993
6994 /* Calling exception callback directly is disallowed, which the
6995 * verifier will reject later. In case it was processed already,
6996 * we can skip this step, otherwise for all other valid cases we
6997 * have to append exception callback now.
6998 */
6999 if (subprog->sub_insn_off == 0) {
7000 err = bpf_object__append_subprog_code(obj, prog, subprog);
7001 if (err)
7002 return err;
7003 err = bpf_object__reloc_code(obj, prog, subprog);
7004 if (err)
7005 return err;
7006 }
7007 }
8a47a6c5 7008 }
b1268826
AS
7009 for (i = 0; i < obj->nr_programs; i++) {
7010 prog = &obj->programs[i];
7011 if (prog_is_subprog(obj, prog))
7012 continue;
a3820c48 7013 if (!prog->autoload)
16e0c35c 7014 continue;
2f38fe68
AN
7015
7016 /* Process data relos for main programs */
b1268826
AS
7017 err = bpf_object__relocate_data(obj, prog);
7018 if (err) {
7019 pr_warn("prog '%s': failed to relocate data references: %d\n",
7020 prog->name, err);
7021 return err;
7022 }
2f38fe68
AN
7023
7024 /* Fix up .BTF.ext information, if necessary */
7025 err = bpf_program_fixup_func_info(obj, prog);
7026 if (err) {
7027 pr_warn("prog '%s': failed to perform .BTF.ext fix ups: %d\n",
7028 prog->name, err);
7029 return err;
7030 }
b1268826 7031 }
185cfe83 7032
8a47a6c5
WN
7033 return 0;
7034}
7035
646f02ff 7036static int bpf_object__collect_st_ops_relos(struct bpf_object *obj,
ad23b723 7037 Elf64_Shdr *shdr, Elf_Data *data);
646f02ff
AN
7038
7039static int bpf_object__collect_map_relos(struct bpf_object *obj,
ad23b723 7040 Elf64_Shdr *shdr, Elf_Data *data)
646f02ff 7041{
15728ad3
AN
7042 const int bpf_ptr_sz = 8, host_ptr_sz = sizeof(void *);
7043 int i, j, nrels, new_sz;
063e6881 7044 const struct btf_var_secinfo *vi = NULL;
646f02ff 7045 const struct btf_type *sec, *var, *def;
341ac5ff
HC
7046 struct bpf_map *map = NULL, *targ_map = NULL;
7047 struct bpf_program *targ_prog = NULL;
7048 bool is_prog_array, is_map_in_map;
646f02ff 7049 const struct btf_member *member;
341ac5ff 7050 const char *name, *mname, *type;
646f02ff 7051 unsigned int moff;
ad23b723
AN
7052 Elf64_Sym *sym;
7053 Elf64_Rel *rel;
646f02ff
AN
7054 void *tmp;
7055
7056 if (!obj->efile.btf_maps_sec_btf_id || !obj->btf)
7057 return -EINVAL;
7058 sec = btf__type_by_id(obj->btf, obj->efile.btf_maps_sec_btf_id);
7059 if (!sec)
7060 return -EINVAL;
7061
646f02ff
AN
7062 nrels = shdr->sh_size / shdr->sh_entsize;
7063 for (i = 0; i < nrels; i++) {
ad23b723
AN
7064 rel = elf_rel_by_idx(data, i);
7065 if (!rel) {
646f02ff
AN
7066 pr_warn(".maps relo #%d: failed to get ELF relo\n", i);
7067 return -LIBBPF_ERRNO__FORMAT;
7068 }
ad23b723
AN
7069
7070 sym = elf_sym_by_idx(obj, ELF64_R_SYM(rel->r_info));
7071 if (!sym) {
646f02ff 7072 pr_warn(".maps relo #%d: symbol %zx not found\n",
ad23b723 7073 i, (size_t)ELF64_R_SYM(rel->r_info));
646f02ff
AN
7074 return -LIBBPF_ERRNO__FORMAT;
7075 }
ad23b723 7076 name = elf_sym_str(obj, sym->st_name) ?: "<?>";
646f02ff 7077
ad23b723
AN
7078 pr_debug(".maps relo #%d: for %zd value %zd rel->r_offset %zu name %d ('%s')\n",
7079 i, (ssize_t)(rel->r_info >> 32), (size_t)sym->st_value,
7080 (size_t)rel->r_offset, sym->st_name, name);
646f02ff
AN
7081
7082 for (j = 0; j < obj->nr_maps; j++) {
7083 map = &obj->maps[j];
7084 if (map->sec_idx != obj->efile.btf_maps_shndx)
7085 continue;
7086
7087 vi = btf_var_secinfos(sec) + map->btf_var_idx;
ad23b723
AN
7088 if (vi->offset <= rel->r_offset &&
7089 rel->r_offset + bpf_ptr_sz <= vi->offset + vi->size)
646f02ff
AN
7090 break;
7091 }
7092 if (j == obj->nr_maps) {
ad23b723
AN
7093 pr_warn(".maps relo #%d: cannot find map '%s' at rel->r_offset %zu\n",
7094 i, name, (size_t)rel->r_offset);
646f02ff
AN
7095 return -EINVAL;
7096 }
7097
341ac5ff
HC
7098 is_map_in_map = bpf_map_type__is_map_in_map(map->def.type);
7099 is_prog_array = map->def.type == BPF_MAP_TYPE_PROG_ARRAY;
7100 type = is_map_in_map ? "map" : "prog";
7101 if (is_map_in_map) {
7102 if (sym->st_shndx != obj->efile.btf_maps_shndx) {
7103 pr_warn(".maps relo #%d: '%s' isn't a BTF-defined map\n",
7104 i, name);
7105 return -LIBBPF_ERRNO__RELOC;
7106 }
7107 if (map->def.type == BPF_MAP_TYPE_HASH_OF_MAPS &&
7108 map->def.key_size != sizeof(int)) {
7109 pr_warn(".maps relo #%d: hash-of-maps '%s' should have key size %zu.\n",
7110 i, map->name, sizeof(int));
7111 return -EINVAL;
7112 }
7113 targ_map = bpf_object__find_map_by_name(obj, name);
7114 if (!targ_map) {
7115 pr_warn(".maps relo #%d: '%s' isn't a valid map reference\n",
7116 i, name);
7117 return -ESRCH;
7118 }
7119 } else if (is_prog_array) {
7120 targ_prog = bpf_object__find_program_by_name(obj, name);
7121 if (!targ_prog) {
7122 pr_warn(".maps relo #%d: '%s' isn't a valid program reference\n",
7123 i, name);
7124 return -ESRCH;
7125 }
7126 if (targ_prog->sec_idx != sym->st_shndx ||
7127 targ_prog->sec_insn_off * 8 != sym->st_value ||
7128 prog_is_subprog(obj, targ_prog)) {
7129 pr_warn(".maps relo #%d: '%s' isn't an entry-point program\n",
7130 i, name);
7131 return -LIBBPF_ERRNO__RELOC;
7132 }
7133 } else {
646f02ff
AN
7134 return -EINVAL;
7135 }
7136
646f02ff
AN
7137 var = btf__type_by_id(obj->btf, vi->type);
7138 def = skip_mods_and_typedefs(obj->btf, var->type, NULL);
7139 if (btf_vlen(def) == 0)
7140 return -EINVAL;
7141 member = btf_members(def) + btf_vlen(def) - 1;
7142 mname = btf__name_by_offset(obj->btf, member->name_off);
7143 if (strcmp(mname, "values"))
7144 return -EINVAL;
7145
7146 moff = btf_member_bit_offset(def, btf_vlen(def) - 1) / 8;
ad23b723 7147 if (rel->r_offset - vi->offset < moff)
646f02ff
AN
7148 return -EINVAL;
7149
ad23b723 7150 moff = rel->r_offset - vi->offset - moff;
15728ad3
AN
7151 /* here we use BPF pointer size, which is always 64 bit, as we
7152 * are parsing ELF that was built for BPF target
7153 */
7154 if (moff % bpf_ptr_sz)
646f02ff 7155 return -EINVAL;
15728ad3 7156 moff /= bpf_ptr_sz;
646f02ff
AN
7157 if (moff >= map->init_slots_sz) {
7158 new_sz = moff + 1;
029258d7 7159 tmp = libbpf_reallocarray(map->init_slots, new_sz, host_ptr_sz);
646f02ff
AN
7160 if (!tmp)
7161 return -ENOMEM;
7162 map->init_slots = tmp;
7163 memset(map->init_slots + map->init_slots_sz, 0,
15728ad3 7164 (new_sz - map->init_slots_sz) * host_ptr_sz);
646f02ff
AN
7165 map->init_slots_sz = new_sz;
7166 }
341ac5ff 7167 map->init_slots[moff] = is_map_in_map ? (void *)targ_map : (void *)targ_prog;
646f02ff 7168
341ac5ff
HC
7169 pr_debug(".maps relo #%d: map '%s' slot [%d] points to %s '%s'\n",
7170 i, map->name, moff, type, name);
646f02ff
AN
7171 }
7172
7173 return 0;
7174}
590a0088 7175
c3c55696
AN
7176static int bpf_object__collect_relos(struct bpf_object *obj)
7177{
7178 int i, err;
34090915 7179
25bbbd7a
AN
7180 for (i = 0; i < obj->efile.sec_cnt; i++) {
7181 struct elf_sec_desc *sec_desc = &obj->efile.secs[i];
7182 Elf64_Shdr *shdr;
7183 Elf_Data *data;
7184 int idx;
7185
7186 if (sec_desc->sec_type != SEC_RELO)
7187 continue;
7188
7189 shdr = sec_desc->shdr;
7190 data = sec_desc->data;
7191 idx = shdr->sh_info;
34090915 7192
240bf8a5 7193 if (shdr->sh_type != SHT_REL || idx < 0 || idx >= obj->efile.sec_cnt) {
be18010e 7194 pr_warn("internal error at %d\n", __LINE__);
6371ca3b 7195 return -LIBBPF_ERRNO__INTERNAL;
34090915
WN
7196 }
7197
240bf8a5 7198 if (obj->efile.secs[idx].sec_type == SEC_ST_OPS)
646f02ff 7199 err = bpf_object__collect_st_ops_relos(obj, shdr, data);
c3c55696 7200 else if (idx == obj->efile.btf_maps_shndx)
646f02ff 7201 err = bpf_object__collect_map_relos(obj, shdr, data);
c3c55696
AN
7202 else
7203 err = bpf_object__collect_prog_relos(obj, shdr, data);
34090915 7204 if (err)
6371ca3b 7205 return err;
34090915 7206 }
c3c55696 7207
d0e92887 7208 bpf_object__sort_relos(obj);
34090915
WN
7209 return 0;
7210}
7211
109cea5a
AN
7212static bool insn_is_helper_call(struct bpf_insn *insn, enum bpf_func_id *func_id)
7213{
9b2f6fec 7214 if (BPF_CLASS(insn->code) == BPF_JMP &&
109cea5a
AN
7215 BPF_OP(insn->code) == BPF_CALL &&
7216 BPF_SRC(insn->code) == BPF_K &&
9b2f6fec
AN
7217 insn->src_reg == 0 &&
7218 insn->dst_reg == 0) {
7219 *func_id = insn->imm;
109cea5a
AN
7220 return true;
7221 }
7222 return false;
7223}
7224
42869d28 7225static int bpf_object__sanitize_prog(struct bpf_object *obj, struct bpf_program *prog)
109cea5a
AN
7226{
7227 struct bpf_insn *insn = prog->insns;
7228 enum bpf_func_id func_id;
7229 int i;
7230
67234743
AS
7231 if (obj->gen_loader)
7232 return 0;
7233
109cea5a
AN
7234 for (i = 0; i < prog->insns_cnt; i++, insn++) {
7235 if (!insn_is_helper_call(insn, &func_id))
7236 continue;
7237
7238 /* on kernels that don't yet support
7239 * bpf_probe_read_{kernel,user}[_str] helpers, fall back
7240 * to bpf_probe_read() which works well for old kernels
7241 */
7242 switch (func_id) {
7243 case BPF_FUNC_probe_read_kernel:
7244 case BPF_FUNC_probe_read_user:
9ca1f56a 7245 if (!kernel_supports(obj, FEAT_PROBE_READ_KERN))
109cea5a
AN
7246 insn->imm = BPF_FUNC_probe_read;
7247 break;
7248 case BPF_FUNC_probe_read_kernel_str:
7249 case BPF_FUNC_probe_read_user_str:
9ca1f56a 7250 if (!kernel_supports(obj, FEAT_PROBE_READ_KERN))
109cea5a
AN
7251 insn->imm = BPF_FUNC_probe_read_str;
7252 break;
7253 default:
7254 break;
7255 }
7256 }
7257 return 0;
7258}
7259
15ea31fa
AN
7260static int libbpf_find_attach_btf_id(struct bpf_program *prog, const char *attach_name,
7261 int *btf_obj_fd, int *btf_type_id);
12d9466d 7262
4fa5bcfe
AN
7263/* this is called as prog->sec_def->prog_prepare_load_fn for libbpf-supported sec_defs */
7264static int libbpf_prepare_prog_load(struct bpf_program *prog,
7265 struct bpf_prog_load_opts *opts, long cookie)
12d9466d 7266{
15ea31fa
AN
7267 enum sec_def_flags def = cookie;
7268
12d9466d 7269 /* old kernels might not support specifying expected_attach_type */
15ea31fa 7270 if ((def & SEC_EXP_ATTACH_OPT) && !kernel_supports(prog->obj, FEAT_EXP_ATTACH_TYPE))
d10ef2b8 7271 opts->expected_attach_type = 0;
12d9466d 7272
15ea31fa 7273 if (def & SEC_SLEEPABLE)
d10ef2b8 7274 opts->prog_flags |= BPF_F_SLEEPABLE;
12d9466d 7275
082c4bfb
LB
7276 if (prog->type == BPF_PROG_TYPE_XDP && (def & SEC_XDP_FRAGS))
7277 opts->prog_flags |= BPF_F_XDP_HAS_FRAGS;
7278
5902da6d
JO
7279 /* special check for usdt to use uprobe_multi link */
7280 if ((def & SEC_USDT) && kernel_supports(prog->obj, FEAT_UPROBE_MULTI_LINK))
7281 prog->expected_attach_type = BPF_TRACE_UPROBE_MULTI;
7282
cc7d8f2c 7283 if ((def & SEC_ATTACH_BTF) && !prog->attach_btf_id) {
12d9466d 7284 int btf_obj_fd = 0, btf_type_id = 0, err;
15ea31fa 7285 const char *attach_name;
12d9466d 7286
cc7d8f2c
AN
7287 attach_name = strchr(prog->sec_name, '/');
7288 if (!attach_name) {
7289 /* if BPF program is annotated with just SEC("fentry")
7290 * (or similar) without declaratively specifying
7291 * target, then it is expected that target will be
7292 * specified with bpf_program__set_attach_target() at
7293 * runtime before BPF object load step. If not, then
7294 * there is nothing to load into the kernel as BPF
7295 * verifier won't be able to validate BPF program
7296 * correctness anyways.
7297 */
7298 pr_warn("prog '%s': no BTF-based attach target is specified, use bpf_program__set_attach_target()\n",
7299 prog->name);
7300 return -EINVAL;
7301 }
7302 attach_name++; /* skip over / */
7303
15ea31fa 7304 err = libbpf_find_attach_btf_id(prog, attach_name, &btf_obj_fd, &btf_type_id);
12d9466d
AN
7305 if (err)
7306 return err;
7307
7308 /* cache resolved BTF FD and BTF type ID in the prog */
7309 prog->attach_btf_obj_fd = btf_obj_fd;
7310 prog->attach_btf_id = btf_type_id;
7311
7312 /* but by now libbpf common logic is not utilizing
7313 * prog->atach_btf_obj_fd/prog->attach_btf_id anymore because
d10ef2b8
AN
7314 * this callback is called after opts were populated by
7315 * libbpf, so this callback has to update opts explicitly here
12d9466d 7316 */
d10ef2b8
AN
7317 opts->attach_btf_obj_fd = btf_obj_fd;
7318 opts->attach_btf_id = btf_type_id;
12d9466d
AN
7319 }
7320 return 0;
7321}
7322
9fdc4273
AN
7323static void fixup_verifier_log(struct bpf_program *prog, char *buf, size_t buf_sz);
7324
cf90a20d
AN
7325static int bpf_object_load_prog(struct bpf_object *obj, struct bpf_program *prog,
7326 struct bpf_insn *insns, int insns_cnt,
7327 const char *license, __u32 kern_version, int *prog_fd)
55cffde2 7328{
d10ef2b8
AN
7329 LIBBPF_OPTS(bpf_prog_load_opts, load_attr);
7330 const char *prog_name = NULL;
1ce6a9fc 7331 char *cp, errmsg[STRERR_BUFSIZE];
8395f320 7332 size_t log_buf_size = 0;
b3ce9079 7333 char *log_buf = NULL, *tmp;
b3ce9079
AN
7334 bool own_log_buf = true;
7335 __u32 log_level = prog->log_level;
9bf48fa1 7336 int ret, err;
55cffde2 7337
80b2b5c3
AM
7338 if (prog->type == BPF_PROG_TYPE_UNSPEC) {
7339 /*
7340 * The program type must be set. Most likely we couldn't find a proper
7341 * section definition at load time, and thus we didn't infer the type.
7342 */
7343 pr_warn("prog '%s': missing BPF prog type, check ELF section name '%s'\n",
7344 prog->name, prog->sec_name);
7345 return -EINVAL;
7346 }
7347
fba01a06
AN
7348 if (!insns || !insns_cnt)
7349 return -EINVAL;
7350
25bbbd7a 7351 if (kernel_supports(obj, FEAT_PROG_NAME))
d10ef2b8 7352 prog_name = prog->name;
12d9466d
AN
7353 load_attr.attach_prog_fd = prog->attach_prog_fd;
7354 load_attr.attach_btf_obj_fd = prog->attach_btf_obj_fd;
6aef10a4
AN
7355 load_attr.attach_btf_id = prog->attach_btf_id;
7356 load_attr.kern_version = kern_version;
7357 load_attr.prog_ifindex = prog->prog_ifindex;
7358
0f0e55d8 7359 /* specify func_info/line_info only if kernel supports them */
9bf48fa1
QM
7360 if (obj->btf && btf__fd(obj->btf) >= 0 && kernel_supports(obj, FEAT_BTF_FUNC)) {
7361 load_attr.prog_btf_fd = btf__fd(obj->btf);
0f0e55d8
AN
7362 load_attr.func_info = prog->func_info;
7363 load_attr.func_info_rec_size = prog->func_info_rec_size;
7364 load_attr.func_info_cnt = prog->func_info_cnt;
7365 load_attr.line_info = prog->line_info;
7366 load_attr.line_info_rec_size = prog->line_info_rec_size;
7367 load_attr.line_info_cnt = prog->line_info_cnt;
7368 }
b3ce9079 7369 load_attr.log_level = log_level;
04656198 7370 load_attr.prog_flags = prog->prog_flags;
25bbbd7a 7371 load_attr.fd_array = obj->fd_array;
55cffde2 7372
6b434b61
AN
7373 load_attr.token_fd = obj->token_fd;
7374 if (obj->token_fd)
7375 load_attr.prog_flags |= BPF_F_TOKEN_FD;
7376
12d9466d 7377 /* adjust load_attr if sec_def provides custom preload callback */
4fa5bcfe
AN
7378 if (prog->sec_def && prog->sec_def->prog_prepare_load_fn) {
7379 err = prog->sec_def->prog_prepare_load_fn(prog, &load_attr, prog->sec_def->cookie);
12d9466d
AN
7380 if (err < 0) {
7381 pr_warn("prog '%s': failed to prepare load attributes: %d\n",
7382 prog->name, err);
7383 return err;
7384 }
b63b3c49
JO
7385 insns = prog->insns;
7386 insns_cnt = prog->insns_cnt;
12d9466d
AN
7387 }
7388
5902da6d
JO
7389 /* allow prog_prepare_load_fn to change expected_attach_type */
7390 load_attr.expected_attach_type = prog->expected_attach_type;
7391
25bbbd7a 7392 if (obj->gen_loader) {
d10ef2b8
AN
7393 bpf_gen__prog_load(obj->gen_loader, prog->type, prog->name,
7394 license, insns, insns_cnt, &load_attr,
25bbbd7a 7395 prog - obj->programs);
be2f2d16 7396 *prog_fd = -1;
67234743
AS
7397 return 0;
7398 }
8395f320 7399
b3ce9079 7400retry_load:
bb412cf1 7401 /* if log_level is zero, we don't request logs initially even if
b3ce9079
AN
7402 * custom log_buf is specified; if the program load fails, then we'll
7403 * bump log_level to 1 and use either custom log_buf or we'll allocate
7404 * our own and retry the load to get details on what failed
7405 */
7406 if (log_level) {
7407 if (prog->log_buf) {
7408 log_buf = prog->log_buf;
7409 log_buf_size = prog->log_size;
7410 own_log_buf = false;
7411 } else if (obj->log_buf) {
7412 log_buf = obj->log_buf;
7413 log_buf_size = obj->log_size;
7414 own_log_buf = false;
7415 } else {
7416 log_buf_size = max((size_t)BPF_LOG_BUF_SIZE, log_buf_size * 2);
7417 tmp = realloc(log_buf, log_buf_size);
7418 if (!tmp) {
7419 ret = -ENOMEM;
7420 goto out;
7421 }
7422 log_buf = tmp;
7423 log_buf[0] = '\0';
7424 own_log_buf = true;
7425 }
8395f320 7426 }
55cffde2 7427
6aef10a4 7428 load_attr.log_buf = log_buf;
d10ef2b8 7429 load_attr.log_size = log_buf_size;
b3ce9079 7430 load_attr.log_level = log_level;
55cffde2 7431
b3ce9079 7432 ret = bpf_prog_load(prog->type, prog_name, license, insns, insns_cnt, &load_attr);
55cffde2 7433 if (ret >= 0) {
b3ce9079 7434 if (log_level && own_log_buf) {
ad9a7f96
AN
7435 pr_debug("prog '%s': -- BEGIN PROG LOAD LOG --\n%s-- END PROG LOAD LOG --\n",
7436 prog->name, log_buf);
7437 }
5d23328d 7438
25bbbd7a
AN
7439 if (obj->has_rodata && kernel_supports(obj, FEAT_PROG_BIND_MAP)) {
7440 struct bpf_map *map;
7441 int i;
7442
7443 for (i = 0; i < obj->nr_maps; i++) {
7444 map = &prog->obj->maps[i];
7445 if (map->libbpf_type != LIBBPF_MAP_RODATA)
7446 continue;
5d23328d 7447
f08c18e0 7448 if (bpf_prog_bind_map(ret, map->fd, NULL)) {
25bbbd7a 7449 cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
ad9a7f96
AN
7450 pr_warn("prog '%s': failed to bind map '%s': %s\n",
7451 prog->name, map->real_name, cp);
25bbbd7a
AN
7452 /* Don't fail hard if can't bind rodata. */
7453 }
5d23328d
YZ
7454 }
7455 }
7456
be2f2d16 7457 *prog_fd = ret;
55cffde2
WN
7458 ret = 0;
7459 goto out;
7460 }
7461
b3ce9079
AN
7462 if (log_level == 0) {
7463 log_level = 1;
da11b417
AS
7464 goto retry_load;
7465 }
b3ce9079
AN
7466 /* On ENOSPC, increase log buffer size and retry, unless custom
7467 * log_buf is specified.
7468 * Be careful to not overflow u32, though. Kernel's log buf size limit
7469 * isn't part of UAPI so it can always be bumped to full 4GB. So don't
7470 * multiply by 2 unless we are sure we'll fit within 32 bits.
7471 * Currently, we'll get -EINVAL when we reach (UINT_MAX >> 2).
7472 */
7473 if (own_log_buf && errno == ENOSPC && log_buf_size <= UINT_MAX / 2)
7474 goto retry_load;
2eda2145
AN
7475
7476 ret = -errno;
9fdc4273
AN
7477
7478 /* post-process verifier log to improve error descriptions */
7479 fixup_verifier_log(prog, log_buf, log_buf_size);
7480
24d6a808 7481 cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
ad9a7f96 7482 pr_warn("prog '%s': BPF program load failed: %s\n", prog->name, cp);
dc3a2d25 7483 pr_perm_msg(ret);
55cffde2 7484
b3ce9079 7485 if (own_log_buf && log_buf && log_buf[0] != '\0') {
ad9a7f96
AN
7486 pr_warn("prog '%s': -- BEGIN PROG LOAD LOG --\n%s-- END PROG LOAD LOG --\n",
7487 prog->name, log_buf);
7488 }
55cffde2
WN
7489
7490out:
b3ce9079
AN
7491 if (own_log_buf)
7492 free(log_buf);
55cffde2
WN
7493 return ret;
7494}
7495
9fdc4273
AN
7496static char *find_prev_line(char *buf, char *cur)
7497{
7498 char *p;
7499
7500 if (cur == buf) /* end of a log buf */
7501 return NULL;
7502
7503 p = cur - 1;
7504 while (p - 1 >= buf && *(p - 1) != '\n')
7505 p--;
7506
7507 return p;
7508}
7509
7510static void patch_log(char *buf, size_t buf_sz, size_t log_sz,
7511 char *orig, size_t orig_sz, const char *patch)
7512{
7513 /* size of the remaining log content to the right from the to-be-replaced part */
7514 size_t rem_sz = (buf + log_sz) - (orig + orig_sz);
7515 size_t patch_sz = strlen(patch);
7516
7517 if (patch_sz != orig_sz) {
7518 /* If patch line(s) are longer than original piece of verifier log,
7519 * shift log contents by (patch_sz - orig_sz) bytes to the right
7520 * starting from after to-be-replaced part of the log.
7521 *
7522 * If patch line(s) are shorter than original piece of verifier log,
7523 * shift log contents by (orig_sz - patch_sz) bytes to the left
7524 * starting from after to-be-replaced part of the log
7525 *
7526 * We need to be careful about not overflowing available
7527 * buf_sz capacity. If that's the case, we'll truncate the end
7528 * of the original log, as necessary.
7529 */
7530 if (patch_sz > orig_sz) {
7531 if (orig + patch_sz >= buf + buf_sz) {
7532 /* patch is big enough to cover remaining space completely */
7533 patch_sz -= (orig + patch_sz) - (buf + buf_sz) + 1;
7534 rem_sz = 0;
7535 } else if (patch_sz - orig_sz > buf_sz - log_sz) {
7536 /* patch causes part of remaining log to be truncated */
7537 rem_sz -= (patch_sz - orig_sz) - (buf_sz - log_sz);
7538 }
7539 }
7540 /* shift remaining log to the right by calculated amount */
7541 memmove(orig + patch_sz, orig + orig_sz, rem_sz);
7542 }
7543
7544 memcpy(orig, patch, patch_sz);
7545}
7546
7547static void fixup_log_failed_core_relo(struct bpf_program *prog,
7548 char *buf, size_t buf_sz, size_t log_sz,
7549 char *line1, char *line2, char *line3)
7550{
7551 /* Expected log for failed and not properly guarded CO-RE relocation:
7552 * line1 -> 123: (85) call unknown#195896080
7553 * line2 -> invalid func unknown#195896080
7554 * line3 -> <anything else or end of buffer>
7555 *
7556 * "123" is the index of the instruction that was poisoned. We extract
7557 * instruction index to find corresponding CO-RE relocation and
7558 * replace this part of the log with more relevant information about
7559 * failed CO-RE relocation.
7560 */
7561 const struct bpf_core_relo *relo;
7562 struct bpf_core_spec spec;
7563 char patch[512], spec_buf[256];
b198881d 7564 int insn_idx, err, spec_len;
9fdc4273
AN
7565
7566 if (sscanf(line1, "%d: (%*d) call unknown#195896080\n", &insn_idx) != 1)
7567 return;
7568
7569 relo = find_relo_core(prog, insn_idx);
7570 if (!relo)
7571 return;
7572
7573 err = bpf_core_parse_spec(prog->name, prog->obj->btf, relo, &spec);
7574 if (err)
7575 return;
7576
b198881d 7577 spec_len = bpf_core_format_spec(spec_buf, sizeof(spec_buf), &spec);
9fdc4273
AN
7578 snprintf(patch, sizeof(patch),
7579 "%d: <invalid CO-RE relocation>\n"
b198881d
AN
7580 "failed to resolve CO-RE relocation %s%s\n",
7581 insn_idx, spec_buf, spec_len >= sizeof(spec_buf) ? "..." : "");
9fdc4273
AN
7582
7583 patch_log(buf, buf_sz, log_sz, line1, line3 - line1, patch);
7584}
7585
ec41817b
AN
7586static void fixup_log_missing_map_load(struct bpf_program *prog,
7587 char *buf, size_t buf_sz, size_t log_sz,
7588 char *line1, char *line2, char *line3)
7589{
3055ddd6 7590 /* Expected log for failed and not properly guarded map reference:
ec41817b
AN
7591 * line1 -> 123: (85) call unknown#2001000345
7592 * line2 -> invalid func unknown#2001000345
7593 * line3 -> <anything else or end of buffer>
7594 *
7595 * "123" is the index of the instruction that was poisoned.
3055ddd6 7596 * "345" in "2001000345" is a map index in obj->maps to fetch map name.
ec41817b
AN
7597 */
7598 struct bpf_object *obj = prog->obj;
7599 const struct bpf_map *map;
7600 int insn_idx, map_idx;
7601 char patch[128];
7602
7603 if (sscanf(line1, "%d: (%*d) call unknown#%d\n", &insn_idx, &map_idx) != 2)
7604 return;
7605
3055ddd6 7606 map_idx -= POISON_LDIMM64_MAP_BASE;
ec41817b
AN
7607 if (map_idx < 0 || map_idx >= obj->nr_maps)
7608 return;
7609 map = &obj->maps[map_idx];
7610
7611 snprintf(patch, sizeof(patch),
7612 "%d: <invalid BPF map reference>\n"
7613 "BPF map '%s' is referenced but wasn't created\n",
7614 insn_idx, map->name);
7615
7616 patch_log(buf, buf_sz, log_sz, line1, line3 - line1, patch);
7617}
7618
05b6f766
AN
7619static void fixup_log_missing_kfunc_call(struct bpf_program *prog,
7620 char *buf, size_t buf_sz, size_t log_sz,
7621 char *line1, char *line2, char *line3)
7622{
7623 /* Expected log for failed and not properly guarded kfunc call:
7624 * line1 -> 123: (85) call unknown#2002000345
7625 * line2 -> invalid func unknown#2002000345
7626 * line3 -> <anything else or end of buffer>
7627 *
7628 * "123" is the index of the instruction that was poisoned.
7629 * "345" in "2002000345" is an extern index in obj->externs to fetch kfunc name.
7630 */
7631 struct bpf_object *obj = prog->obj;
7632 const struct extern_desc *ext;
7633 int insn_idx, ext_idx;
7634 char patch[128];
7635
7636 if (sscanf(line1, "%d: (%*d) call unknown#%d\n", &insn_idx, &ext_idx) != 2)
7637 return;
7638
7639 ext_idx -= POISON_CALL_KFUNC_BASE;
7640 if (ext_idx < 0 || ext_idx >= obj->nr_extern)
7641 return;
7642 ext = &obj->externs[ext_idx];
7643
7644 snprintf(patch, sizeof(patch),
7645 "%d: <invalid kfunc call>\n"
7646 "kfunc '%s' is referenced but wasn't resolved\n",
7647 insn_idx, ext->name);
7648
7649 patch_log(buf, buf_sz, log_sz, line1, line3 - line1, patch);
7650}
7651
9fdc4273
AN
7652static void fixup_verifier_log(struct bpf_program *prog, char *buf, size_t buf_sz)
7653{
7654 /* look for familiar error patterns in last N lines of the log */
7655 const size_t max_last_line_cnt = 10;
7656 char *prev_line, *cur_line, *next_line;
7657 size_t log_sz;
7658 int i;
7659
7660 if (!buf)
7661 return;
7662
7663 log_sz = strlen(buf) + 1;
7664 next_line = buf + log_sz - 1;
7665
7666 for (i = 0; i < max_last_line_cnt; i++, next_line = cur_line) {
7667 cur_line = find_prev_line(buf, next_line);
7668 if (!cur_line)
7669 return;
7670
9fdc4273
AN
7671 if (str_has_pfx(cur_line, "invalid func unknown#195896080\n")) {
7672 prev_line = find_prev_line(buf, cur_line);
7673 if (!prev_line)
7674 continue;
7675
3055ddd6 7676 /* failed CO-RE relocation case */
9fdc4273
AN
7677 fixup_log_failed_core_relo(prog, buf, buf_sz, log_sz,
7678 prev_line, cur_line, next_line);
7679 return;
3055ddd6 7680 } else if (str_has_pfx(cur_line, "invalid func unknown#"POISON_LDIMM64_MAP_PFX)) {
ec41817b
AN
7681 prev_line = find_prev_line(buf, cur_line);
7682 if (!prev_line)
7683 continue;
7684
3055ddd6 7685 /* reference to uncreated BPF map */
ec41817b
AN
7686 fixup_log_missing_map_load(prog, buf, buf_sz, log_sz,
7687 prev_line, cur_line, next_line);
7688 return;
05b6f766
AN
7689 } else if (str_has_pfx(cur_line, "invalid func unknown#"POISON_CALL_KFUNC_PFX)) {
7690 prev_line = find_prev_line(buf, cur_line);
7691 if (!prev_line)
7692 continue;
7693
7694 /* reference to unresolved kfunc */
7695 fixup_log_missing_kfunc_call(prog, buf, buf_sz, log_sz,
7696 prev_line, cur_line, next_line);
7697 return;
9fdc4273
AN
7698 }
7699 }
7700}
7701
d0e92887 7702static int bpf_program_record_relos(struct bpf_program *prog)
67234743
AS
7703{
7704 struct bpf_object *obj = prog->obj;
7705 int i;
7706
7707 for (i = 0; i < prog->nr_reloc; i++) {
7708 struct reloc_desc *relo = &prog->reloc_desc[i];
3055ddd6 7709 struct extern_desc *ext = &obj->externs[relo->ext_idx];
708cdc57 7710 int kind;
67234743
AS
7711
7712 switch (relo->type) {
a18f7214 7713 case RELO_EXTERN_LD64:
67234743
AS
7714 if (ext->type != EXT_KSYM)
7715 continue;
708cdc57
AS
7716 kind = btf_is_var(btf__type_by_id(obj->btf, ext->btf_id)) ?
7717 BTF_KIND_VAR : BTF_KIND_FUNC;
c24941cd
KKD
7718 bpf_gen__record_extern(obj->gen_loader, ext->name,
7719 ext->is_weak, !ext->ksym.type_id,
708cdc57 7720 true, kind, relo->insn_idx);
67234743 7721 break;
a18f7214 7722 case RELO_EXTERN_CALL:
c24941cd 7723 bpf_gen__record_extern(obj->gen_loader, ext->name,
708cdc57 7724 ext->is_weak, false, false, BTF_KIND_FUNC,
67234743
AS
7725 relo->insn_idx);
7726 break;
d0e92887
AS
7727 case RELO_CORE: {
7728 struct bpf_core_relo cr = {
7729 .insn_off = relo->insn_idx * 8,
7730 .type_id = relo->core_relo->type_id,
7731 .access_str_off = relo->core_relo->access_str_off,
7732 .kind = relo->core_relo->kind,
7733 };
7734
7735 bpf_gen__record_relo_core(obj->gen_loader, &cr);
7736 break;
7737 }
67234743
AS
7738 default:
7739 continue;
7740 }
7741 }
7742 return 0;
7743}
7744
55cffde2 7745static int
60276f98 7746bpf_object__load_progs(struct bpf_object *obj, int log_level)
55cffde2 7747{
d9297581 7748 struct bpf_program *prog;
55cffde2
WN
7749 size_t i;
7750 int err;
7751
109cea5a
AN
7752 for (i = 0; i < obj->nr_programs; i++) {
7753 prog = &obj->programs[i];
7754 err = bpf_object__sanitize_prog(obj, prog);
7755 if (err)
7756 return err;
7757 }
7758
55cffde2 7759 for (i = 0; i < obj->nr_programs; i++) {
d9297581 7760 prog = &obj->programs[i];
c3c55696 7761 if (prog_is_subprog(obj, prog))
48cca7e4 7762 continue;
a3820c48 7763 if (!prog->autoload) {
9c0f8cbd 7764 pr_debug("prog '%s': skipped loading\n", prog->name);
d9297581
AN
7765 continue;
7766 }
7767 prog->log_level |= log_level;
cf90a20d
AN
7768
7769 if (obj->gen_loader)
7770 bpf_program_record_relos(prog);
7771
7772 err = bpf_object_load_prog(obj, prog, prog->insns, prog->insns_cnt,
7773 obj->license, obj->kern_version, &prog->fd);
7774 if (err) {
7775 pr_warn("prog '%s': failed to load: %d\n", prog->name, err);
55cffde2 7776 return err;
cf90a20d 7777 }
55cffde2 7778 }
185cfe83
AN
7779
7780 bpf_object__free_relocs(obj);
55cffde2
WN
7781 return 0;
7782}
7783
25498a19
AN
7784static const struct bpf_sec_def *find_sec_def(const char *sec_name);
7785
91b4d1d1
AN
7786static int bpf_object_init_progs(struct bpf_object *obj, const struct bpf_object_open_opts *opts)
7787{
7788 struct bpf_program *prog;
12d9466d 7789 int err;
91b4d1d1
AN
7790
7791 bpf_object__for_each_program(prog, obj) {
7792 prog->sec_def = find_sec_def(prog->sec_name);
7793 if (!prog->sec_def) {
7794 /* couldn't guess, but user might manually specify */
7795 pr_debug("prog '%s': unrecognized ELF section name '%s'\n",
7796 prog->name, prog->sec_name);
7797 continue;
7798 }
7799
df286716
GS
7800 prog->type = prog->sec_def->prog_type;
7801 prog->expected_attach_type = prog->sec_def->expected_attach_type;
91b4d1d1 7802
12d9466d
AN
7803 /* sec_def can have custom callback which should be called
7804 * after bpf_program is initialized to adjust its properties
7805 */
4fa5bcfe
AN
7806 if (prog->sec_def->prog_setup_fn) {
7807 err = prog->sec_def->prog_setup_fn(prog, prog->sec_def->cookie);
12d9466d
AN
7808 if (err < 0) {
7809 pr_warn("prog '%s': failed to initialize: %d\n",
7810 prog->name, err);
7811 return err;
7812 }
7813 }
91b4d1d1
AN
7814 }
7815
7816 return 0;
7817}
7818
ad9a7f96
AN
7819static struct bpf_object *bpf_object_open(const char *path, const void *obj_buf, size_t obj_buf_sz,
7820 const struct bpf_object_open_opts *opts)
1a5e3fb1 7821{
6b434b61 7822 const char *obj_name, *kconfig, *btf_tmp_path, *token_path;
1a5e3fb1 7823 struct bpf_object *obj;
291ee02b 7824 char tmp_name[64];
d17aff80 7825 int err;
e0e3ea88
AN
7826 char *log_buf;
7827 size_t log_size;
7828 __u32 log_level;
1a5e3fb1
WN
7829
7830 if (elf_version(EV_CURRENT) == EV_NONE) {
be18010e
KW
7831 pr_warn("failed to init libelf for %s\n",
7832 path ? : "(mem buf)");
6371ca3b 7833 return ERR_PTR(-LIBBPF_ERRNO__LIBELF);
1a5e3fb1
WN
7834 }
7835
291ee02b
AN
7836 if (!OPTS_VALID(opts, bpf_object_open_opts))
7837 return ERR_PTR(-EINVAL);
7838
1aace10f 7839 obj_name = OPTS_GET(opts, object_name, NULL);
291ee02b
AN
7840 if (obj_buf) {
7841 if (!obj_name) {
7842 snprintf(tmp_name, sizeof(tmp_name), "%lx-%lx",
7843 (unsigned long)obj_buf,
7844 (unsigned long)obj_buf_sz);
7845 obj_name = tmp_name;
7846 }
7847 path = obj_name;
7848 pr_debug("loading object '%s' from buffer\n", obj_name);
7849 }
7850
e0e3ea88
AN
7851 log_buf = OPTS_GET(opts, kernel_log_buf, NULL);
7852 log_size = OPTS_GET(opts, kernel_log_size, 0);
7853 log_level = OPTS_GET(opts, kernel_log_level, 0);
7854 if (log_size > UINT_MAX)
7855 return ERR_PTR(-EINVAL);
7856 if (log_size && !log_buf)
7857 return ERR_PTR(-EINVAL);
7858
6b434b61 7859 token_path = OPTS_GET(opts, bpf_token_path, NULL);
cac270ad
AN
7860 /* if user didn't specify bpf_token_path explicitly, check if
7861 * LIBBPF_BPF_TOKEN_PATH envvar was set and treat it as bpf_token_path
7862 * option
7863 */
7864 if (!token_path)
7865 token_path = getenv("LIBBPF_BPF_TOKEN_PATH");
6b434b61
AN
7866 if (token_path && strlen(token_path) >= PATH_MAX)
7867 return ERR_PTR(-ENAMETOOLONG);
7868
2ce8450e 7869 obj = bpf_object__new(path, obj_buf, obj_buf_sz, obj_name);
6371ca3b
WN
7870 if (IS_ERR(obj))
7871 return obj;
1a5e3fb1 7872
e0e3ea88
AN
7873 obj->log_buf = log_buf;
7874 obj->log_size = log_size;
7875 obj->log_level = log_level;
7876
6b434b61
AN
7877 if (token_path) {
7878 obj->token_path = strdup(token_path);
7879 if (!obj->token_path) {
7880 err = -ENOMEM;
7881 goto out;
7882 }
7883 }
7884
1373ff59
SC
7885 btf_tmp_path = OPTS_GET(opts, btf_custom_path, NULL);
7886 if (btf_tmp_path) {
7887 if (strlen(btf_tmp_path) >= PATH_MAX) {
7888 err = -ENAMETOOLONG;
7889 goto out;
7890 }
7891 obj->btf_custom_path = strdup(btf_tmp_path);
7892 if (!obj->btf_custom_path) {
7893 err = -ENOMEM;
7894 goto out;
7895 }
7896 }
7897
8601fd42
AN
7898 kconfig = OPTS_GET(opts, kconfig, NULL);
7899 if (kconfig) {
7900 obj->kconfig = strdup(kconfig);
18353c87
SC
7901 if (!obj->kconfig) {
7902 err = -ENOMEM;
7903 goto out;
7904 }
166750bc 7905 }
291ee02b 7906
0d13bfce
AN
7907 err = bpf_object__elf_init(obj);
7908 err = err ? : bpf_object__check_endianness(obj);
7909 err = err ? : bpf_object__elf_collect(obj);
166750bc 7910 err = err ? : bpf_object__collect_externs(obj);
f33f742d 7911 err = err ? : bpf_object_fixup_btf(obj);
0d13bfce 7912 err = err ? : bpf_object__init_maps(obj, opts);
91b4d1d1 7913 err = err ? : bpf_object_init_progs(obj, opts);
c3c55696 7914 err = err ? : bpf_object__collect_relos(obj);
0d13bfce
AN
7915 if (err)
7916 goto out;
dd4436bb 7917
91b4d1d1 7918 bpf_object__elf_finish(obj);
dd4436bb 7919
1a5e3fb1
WN
7920 return obj;
7921out:
7922 bpf_object__close(obj);
6371ca3b 7923 return ERR_PTR(err);
1a5e3fb1
WN
7924}
7925
2ce8450e 7926struct bpf_object *
01af3bf0 7927bpf_object__open_file(const char *path, const struct bpf_object_open_opts *opts)
2ce8450e 7928{
2ce8450e 7929 if (!path)
e9fc3ce9 7930 return libbpf_err_ptr(-EINVAL);
2ce8450e
AN
7931
7932 pr_debug("loading %s\n", path);
7933
ad9a7f96 7934 return libbpf_ptr(bpf_object_open(path, NULL, 0, opts));
2ce8450e
AN
7935}
7936
146bf811
AN
7937struct bpf_object *bpf_object__open(const char *path)
7938{
7939 return bpf_object__open_file(path, NULL);
7940}
7941
2ce8450e
AN
7942struct bpf_object *
7943bpf_object__open_mem(const void *obj_buf, size_t obj_buf_sz,
01af3bf0 7944 const struct bpf_object_open_opts *opts)
6c956392 7945{
2ce8450e 7946 if (!obj_buf || obj_buf_sz == 0)
e9fc3ce9 7947 return libbpf_err_ptr(-EINVAL);
6c956392 7948
ad9a7f96 7949 return libbpf_ptr(bpf_object_open(NULL, obj_buf, obj_buf_sz, opts));
2ce8450e
AN
7950}
7951
4a404a7e 7952static int bpf_object_unload(struct bpf_object *obj)
52d3352e
WN
7953{
7954 size_t i;
7955
7956 if (!obj)
e9fc3ce9 7957 return libbpf_err(-EINVAL);
52d3352e 7958
590a0088 7959 for (i = 0; i < obj->nr_maps; i++) {
9d759a9b 7960 zclose(obj->maps[i].fd);
590a0088
MKL
7961 if (obj->maps[i].st_ops)
7962 zfree(&obj->maps[i].st_ops->kern_vdata);
7963 }
52d3352e 7964
55cffde2
WN
7965 for (i = 0; i < obj->nr_programs; i++)
7966 bpf_program__unload(&obj->programs[i]);
7967
52d3352e
WN
7968 return 0;
7969}
7970
0d13bfce
AN
7971static int bpf_object__sanitize_maps(struct bpf_object *obj)
7972{
7973 struct bpf_map *m;
7974
7975 bpf_object__for_each_map(m, obj) {
7976 if (!bpf_map__is_internal(m))
7977 continue;
9ca1f56a 7978 if (!kernel_supports(obj, FEAT_ARRAY_MMAP))
6920b086 7979 m->def.map_flags &= ~BPF_F_MMAPABLE;
0d13bfce
AN
7980 }
7981
7982 return 0;
7983}
7984
85153ac0 7985int libbpf_kallsyms_parse(kallsyms_cb_t cb, void *ctx)
1c0c7074
AN
7986{
7987 char sym_type, sym_name[500];
7988 unsigned long long sym_addr;
1c0c7074
AN
7989 int ret, err = 0;
7990 FILE *f;
7991
59842c54 7992 f = fopen("/proc/kallsyms", "re");
1c0c7074
AN
7993 if (!f) {
7994 err = -errno;
7995 pr_warn("failed to open /proc/kallsyms: %d\n", err);
7996 return err;
7997 }
7998
7999 while (true) {
8000 ret = fscanf(f, "%llx %c %499s%*[^\n]\n",
8001 &sym_addr, &sym_type, sym_name);
8002 if (ret == EOF && feof(f))
8003 break;
8004 if (ret != 3) {
135c783f 8005 pr_warn("failed to read kallsyms entry: %d\n", ret);
1c0c7074 8006 err = -EINVAL;
85153ac0 8007 break;
1c0c7074
AN
8008 }
8009
85153ac0
JO
8010 err = cb(sym_addr, sym_type, sym_name, ctx);
8011 if (err)
8012 break;
1c0c7074
AN
8013 }
8014
1c0c7074
AN
8015 fclose(f);
8016 return err;
8017}
8018
85153ac0
JO
8019static int kallsyms_cb(unsigned long long sym_addr, char sym_type,
8020 const char *sym_name, void *ctx)
8021{
8022 struct bpf_object *obj = ctx;
8023 const struct btf_type *t;
8024 struct extern_desc *ext;
8025
8026 ext = find_extern_by_name(obj, sym_name);
8027 if (!ext || ext->type != EXT_KSYM)
8028 return 0;
8029
8030 t = btf__type_by_id(obj->btf, ext->btf_id);
8031 if (!btf_is_var(t))
8032 return 0;
8033
8034 if (ext->is_set && ext->ksym.addr != sym_addr) {
55d00c37 8035 pr_warn("extern (ksym) '%s': resolution is ambiguous: 0x%llx or 0x%llx\n",
85153ac0
JO
8036 sym_name, ext->ksym.addr, sym_addr);
8037 return -EINVAL;
8038 }
8039 if (!ext->is_set) {
8040 ext->is_set = true;
8041 ext->ksym.addr = sym_addr;
55d00c37 8042 pr_debug("extern (ksym) '%s': set to 0x%llx\n", sym_name, sym_addr);
85153ac0
JO
8043 }
8044 return 0;
8045}
8046
8047static int bpf_object__read_kallsyms_file(struct bpf_object *obj)
8048{
8049 return libbpf_kallsyms_parse(kallsyms_cb, obj);
8050}
8051
774e132e
MKL
8052static int find_ksym_btf_id(struct bpf_object *obj, const char *ksym_name,
8053 __u16 kind, struct btf **res_btf,
9dbe6015 8054 struct module_btf **res_mod_btf)
d370bbe1 8055{
9dbe6015 8056 struct module_btf *mod_btf;
284d2587 8057 struct btf *btf;
9dbe6015 8058 int i, id, err;
d370bbe1 8059
933d1aa3 8060 btf = obj->btf_vmlinux;
9dbe6015 8061 mod_btf = NULL;
774e132e
MKL
8062 id = btf__find_by_name_kind(btf, ksym_name, kind);
8063
933d1aa3
MKL
8064 if (id == -ENOENT) {
8065 err = load_module_btfs(obj);
8066 if (err)
8067 return err;
d370bbe1 8068
933d1aa3 8069 for (i = 0; i < obj->btf_module_cnt; i++) {
9dbe6015
KKD
8070 /* we assume module_btf's BTF FD is always >0 */
8071 mod_btf = &obj->btf_modules[i];
8072 btf = mod_btf->btf;
8073 id = btf__find_by_name_kind_own(btf, ksym_name, kind);
933d1aa3
MKL
8074 if (id != -ENOENT)
8075 break;
8076 }
8077 }
2211c825 8078 if (id <= 0)
933d1aa3 8079 return -ESRCH;
d370bbe1 8080
774e132e 8081 *res_btf = btf;
9dbe6015 8082 *res_mod_btf = mod_btf;
774e132e
MKL
8083 return id;
8084}
8085
8086static int bpf_object__resolve_ksym_var_btf_id(struct bpf_object *obj,
8087 struct extern_desc *ext)
8088{
8089 const struct btf_type *targ_var, *targ_type;
8090 __u32 targ_type_id, local_type_id;
9dbe6015 8091 struct module_btf *mod_btf = NULL;
774e132e 8092 const char *targ_var_name;
774e132e 8093 struct btf *btf = NULL;
9dbe6015 8094 int id, err;
774e132e 8095
9dbe6015 8096 id = find_ksym_btf_id(obj, ext->name, BTF_KIND_VAR, &btf, &mod_btf);
466b2e13
KKD
8097 if (id < 0) {
8098 if (id == -ESRCH && ext->is_weak)
8099 return 0;
2211c825
HL
8100 pr_warn("extern (var ksym) '%s': not found in kernel BTF\n",
8101 ext->name);
774e132e 8102 return id;
2211c825 8103 }
774e132e 8104
933d1aa3
MKL
8105 /* find local type_id */
8106 local_type_id = ext->ksym.type_id;
284d2587 8107
933d1aa3
MKL
8108 /* find target type_id */
8109 targ_var = btf__type_by_id(btf, id);
8110 targ_var_name = btf__name_by_offset(btf, targ_var->name_off);
8111 targ_type = skip_mods_and_typedefs(btf, targ_var->type, &targ_type_id);
d370bbe1 8112
933d1aa3
MKL
8113 err = bpf_core_types_are_compat(obj->btf, local_type_id,
8114 btf, targ_type_id);
8115 if (err <= 0) {
8116 const struct btf_type *local_type;
8117 const char *targ_name, *local_name;
d370bbe1 8118
933d1aa3
MKL
8119 local_type = btf__type_by_id(obj->btf, local_type_id);
8120 local_name = btf__name_by_offset(obj->btf, local_type->name_off);
8121 targ_name = btf__name_by_offset(btf, targ_type->name_off);
d370bbe1 8122
933d1aa3
MKL
8123 pr_warn("extern (var ksym) '%s': incompatible types, expected [%d] %s %s, but kernel has [%d] %s %s\n",
8124 ext->name, local_type_id,
8125 btf_kind_str(local_type), local_name, targ_type_id,
8126 btf_kind_str(targ_type), targ_name);
8127 return -EINVAL;
8128 }
d370bbe1 8129
933d1aa3 8130 ext->is_set = true;
9dbe6015 8131 ext->ksym.kernel_btf_obj_fd = mod_btf ? mod_btf->fd : 0;
933d1aa3
MKL
8132 ext->ksym.kernel_btf_id = id;
8133 pr_debug("extern (var ksym) '%s': resolved to [%d] %s %s\n",
8134 ext->name, id, btf_kind_str(targ_var), targ_var_name);
d370bbe1 8135
933d1aa3
MKL
8136 return 0;
8137}
d370bbe1 8138
5bd022ec
MKL
8139static int bpf_object__resolve_ksym_func_btf_id(struct bpf_object *obj,
8140 struct extern_desc *ext)
8141{
8142 int local_func_proto_id, kfunc_proto_id, kfunc_id;
9dbe6015 8143 struct module_btf *mod_btf = NULL;
5bd022ec
MKL
8144 const struct btf_type *kern_func;
8145 struct btf *kern_btf = NULL;
9dbe6015 8146 int ret;
5bd022ec
MKL
8147
8148 local_func_proto_id = ext->ksym.type_id;
8149
5964a223
DM
8150 kfunc_id = find_ksym_btf_id(obj, ext->essent_name ?: ext->name, BTF_KIND_FUNC, &kern_btf,
8151 &mod_btf);
5bd022ec 8152 if (kfunc_id < 0) {
466b2e13
KKD
8153 if (kfunc_id == -ESRCH && ext->is_weak)
8154 return 0;
8155 pr_warn("extern (func ksym) '%s': not found in kernel or module BTFs\n",
5bd022ec
MKL
8156 ext->name);
8157 return kfunc_id;
8158 }
8159
5bd022ec
MKL
8160 kern_func = btf__type_by_id(kern_btf, kfunc_id);
8161 kfunc_proto_id = kern_func->type;
8162
8163 ret = bpf_core_types_are_compat(obj->btf, local_func_proto_id,
8164 kern_btf, kfunc_proto_id);
8165 if (ret <= 0) {
5964a223
DM
8166 if (ext->is_weak)
8167 return 0;
8168
f709160d
AN
8169 pr_warn("extern (func ksym) '%s': func_proto [%d] incompatible with %s [%d]\n",
8170 ext->name, local_func_proto_id,
8171 mod_btf ? mod_btf->name : "vmlinux", kfunc_proto_id);
5bd022ec
MKL
8172 return -EINVAL;
8173 }
8174
9dbe6015
KKD
8175 /* set index for module BTF fd in fd_array, if unset */
8176 if (mod_btf && !mod_btf->fd_array_idx) {
8177 /* insn->off is s16 */
8178 if (obj->fd_array_cnt == INT16_MAX) {
8179 pr_warn("extern (func ksym) '%s': module BTF fd index %d too big to fit in bpf_insn offset\n",
8180 ext->name, mod_btf->fd_array_idx);
8181 return -E2BIG;
8182 }
8183 /* Cannot use index 0 for module BTF fd */
8184 if (!obj->fd_array_cnt)
8185 obj->fd_array_cnt = 1;
8186
8187 ret = libbpf_ensure_mem((void **)&obj->fd_array, &obj->fd_array_cap, sizeof(int),
8188 obj->fd_array_cnt + 1);
8189 if (ret)
8190 return ret;
8191 mod_btf->fd_array_idx = obj->fd_array_cnt;
8192 /* we assume module BTF FD is always >0 */
8193 obj->fd_array[obj->fd_array_cnt++] = mod_btf->fd;
8194 }
8195
5bd022ec 8196 ext->is_set = true;
5bd022ec 8197 ext->ksym.kernel_btf_id = kfunc_id;
9dbe6015 8198 ext->ksym.btf_fd_idx = mod_btf ? mod_btf->fd_array_idx : 0;
5fc13ad5
AS
8199 /* Also set kernel_btf_obj_fd to make sure that bpf_object__relocate_data()
8200 * populates FD into ld_imm64 insn when it's used to point to kfunc.
8201 * {kernel_btf_id, btf_fd_idx} -> fixup bpf_call.
8202 * {kernel_btf_id, kernel_btf_obj_fd} -> fixup ld_imm64.
8203 */
8204 ext->ksym.kernel_btf_obj_fd = mod_btf ? mod_btf->fd : 0;
f709160d
AN
8205 pr_debug("extern (func ksym) '%s': resolved to %s [%d]\n",
8206 ext->name, mod_btf ? mod_btf->name : "vmlinux", kfunc_id);
5bd022ec
MKL
8207
8208 return 0;
8209}
8210
933d1aa3
MKL
8211static int bpf_object__resolve_ksyms_btf_id(struct bpf_object *obj)
8212{
5bd022ec 8213 const struct btf_type *t;
933d1aa3
MKL
8214 struct extern_desc *ext;
8215 int i, err;
8216
8217 for (i = 0; i < obj->nr_extern; i++) {
8218 ext = &obj->externs[i];
8219 if (ext->type != EXT_KSYM || !ext->ksym.type_id)
8220 continue;
8221
67234743
AS
8222 if (obj->gen_loader) {
8223 ext->is_set = true;
8224 ext->ksym.kernel_btf_obj_fd = 0;
8225 ext->ksym.kernel_btf_id = 0;
8226 continue;
8227 }
5bd022ec
MKL
8228 t = btf__type_by_id(obj->btf, ext->btf_id);
8229 if (btf_is_var(t))
8230 err = bpf_object__resolve_ksym_var_btf_id(obj, ext);
8231 else
8232 err = bpf_object__resolve_ksym_func_btf_id(obj, ext);
933d1aa3
MKL
8233 if (err)
8234 return err;
d370bbe1
HL
8235 }
8236 return 0;
8237}
8238
166750bc 8239static int bpf_object__resolve_externs(struct bpf_object *obj,
8601fd42 8240 const char *extra_kconfig)
166750bc 8241{
1c0c7074 8242 bool need_config = false, need_kallsyms = false;
d370bbe1 8243 bool need_vmlinux_btf = false;
166750bc 8244 struct extern_desc *ext;
2e33efe3 8245 void *kcfg_data = NULL;
166750bc 8246 int err, i;
166750bc
AN
8247
8248 if (obj->nr_extern == 0)
8249 return 0;
8250
2e33efe3
AN
8251 if (obj->kconfig_map_idx >= 0)
8252 kcfg_data = obj->maps[obj->kconfig_map_idx].mmaped;
166750bc
AN
8253
8254 for (i = 0; i < obj->nr_extern; i++) {
8255 ext = &obj->externs[i];
8256
55d00c37
AN
8257 if (ext->type == EXT_KSYM) {
8258 if (ext->ksym.type_id)
8259 need_vmlinux_btf = true;
8260 else
8261 need_kallsyms = true;
8262 continue;
8263 } else if (ext->type == EXT_KCFG) {
8264 void *ext_ptr = kcfg_data + ext->kcfg.data_off;
8265 __u64 value = 0;
166750bc 8266
55d00c37
AN
8267 /* Kconfig externs need actual /proc/config.gz */
8268 if (str_has_pfx(ext->name, "CONFIG_")) {
8269 need_config = true;
8270 continue;
8271 }
8272
8273 /* Virtual kcfg externs are customly handled by libbpf */
8274 if (strcmp(ext->name, "LINUX_KERNEL_VERSION") == 0) {
8275 value = get_kernel_version();
8276 if (!value) {
8277 pr_warn("extern (kcfg) '%s': failed to get kernel version\n", ext->name);
8278 return -EINVAL;
8279 }
8280 } else if (strcmp(ext->name, "LINUX_HAS_BPF_COOKIE") == 0) {
8281 value = kernel_supports(obj, FEAT_BPF_COOKIE);
6f5d467d
AN
8282 } else if (strcmp(ext->name, "LINUX_HAS_SYSCALL_WRAPPER") == 0) {
8283 value = kernel_supports(obj, FEAT_SYSCALL_WRAPPER);
55d00c37
AN
8284 } else if (!str_has_pfx(ext->name, "LINUX_") || !ext->is_weak) {
8285 /* Currently libbpf supports only CONFIG_ and LINUX_ prefixed
8286 * __kconfig externs, where LINUX_ ones are virtual and filled out
8287 * customly by libbpf (their values don't come from Kconfig).
8288 * If LINUX_xxx variable is not recognized by libbpf, but is marked
8289 * __weak, it defaults to zero value, just like for CONFIG_xxx
8290 * externs.
8291 */
8292 pr_warn("extern (kcfg) '%s': unrecognized virtual extern\n", ext->name);
166750bc
AN
8293 return -EINVAL;
8294 }
55d00c37
AN
8295
8296 err = set_kcfg_value_num(ext, ext_ptr, value);
166750bc
AN
8297 if (err)
8298 return err;
55d00c37
AN
8299 pr_debug("extern (kcfg) '%s': set to 0x%llx\n",
8300 ext->name, (long long)value);
166750bc 8301 } else {
55d00c37 8302 pr_warn("extern '%s': unrecognized extern kind\n", ext->name);
166750bc
AN
8303 return -EINVAL;
8304 }
8305 }
8601fd42 8306 if (need_config && extra_kconfig) {
2e33efe3 8307 err = bpf_object__read_kconfig_mem(obj, extra_kconfig, kcfg_data);
8601fd42
AN
8308 if (err)
8309 return -EINVAL;
8310 need_config = false;
8311 for (i = 0; i < obj->nr_extern; i++) {
8312 ext = &obj->externs[i];
2e33efe3 8313 if (ext->type == EXT_KCFG && !ext->is_set) {
8601fd42
AN
8314 need_config = true;
8315 break;
8316 }
8317 }
8318 }
166750bc 8319 if (need_config) {
2e33efe3 8320 err = bpf_object__read_kconfig_file(obj, kcfg_data);
166750bc
AN
8321 if (err)
8322 return -EINVAL;
8323 }
1c0c7074
AN
8324 if (need_kallsyms) {
8325 err = bpf_object__read_kallsyms_file(obj);
8326 if (err)
8327 return -EINVAL;
8328 }
d370bbe1
HL
8329 if (need_vmlinux_btf) {
8330 err = bpf_object__resolve_ksyms_btf_id(obj);
8331 if (err)
8332 return -EINVAL;
8333 }
166750bc
AN
8334 for (i = 0; i < obj->nr_extern; i++) {
8335 ext = &obj->externs[i];
8336
8337 if (!ext->is_set && !ext->is_weak) {
55d00c37 8338 pr_warn("extern '%s' (strong): not resolved\n", ext->name);
166750bc
AN
8339 return -ESRCH;
8340 } else if (!ext->is_set) {
55d00c37 8341 pr_debug("extern '%s' (weak): not resolved, defaulting to zero\n",
166750bc
AN
8342 ext->name);
8343 }
8344 }
8345
8346 return 0;
8347}
8348
8d1608d7
KFL
8349static void bpf_map_prepare_vdata(const struct bpf_map *map)
8350{
8351 struct bpf_struct_ops *st_ops;
8352 __u32 i;
8353
8354 st_ops = map->st_ops;
8355 for (i = 0; i < btf_vlen(st_ops->type); i++) {
8356 struct bpf_program *prog = st_ops->progs[i];
8357 void *kern_data;
8358 int prog_fd;
8359
8360 if (!prog)
8361 continue;
8362
8363 prog_fd = bpf_program__fd(prog);
8364 kern_data = st_ops->kern_vdata + st_ops->kern_func_off[i];
8365 *(unsigned long *)kern_data = prog_fd;
8366 }
8367}
8368
8369static int bpf_object_prepare_struct_ops(struct bpf_object *obj)
8370{
8db05261 8371 struct bpf_map *map;
8d1608d7
KFL
8372 int i;
8373
8db05261
EZ
8374 for (i = 0; i < obj->nr_maps; i++) {
8375 map = &obj->maps[i];
8376
8377 if (!bpf_map__is_struct_ops(map))
8378 continue;
8379
8380 if (!map->autocreate)
8381 continue;
8382
8383 bpf_map_prepare_vdata(map);
8384 }
8d1608d7
KFL
8385
8386 return 0;
8387}
8388
e7b924ca 8389static int bpf_object_load(struct bpf_object *obj, int extra_log_level, const char *target_btf_path)
52d3352e 8390{
ec6d5f47 8391 int err, i;
6371ca3b 8392
52d3352e 8393 if (!obj)
e9fc3ce9 8394 return libbpf_err(-EINVAL);
52d3352e
WN
8395
8396 if (obj->loaded) {
d9297581 8397 pr_warn("object '%s': load can't be attempted twice\n", obj->name);
e9fc3ce9 8398 return libbpf_err(-EINVAL);
52d3352e
WN
8399 }
8400
67234743 8401 if (obj->gen_loader)
be315829 8402 bpf_gen__init(obj->gen_loader, extra_log_level, obj->nr_programs, obj->nr_maps);
67234743 8403
6b434b61
AN
8404 err = bpf_object_prepare_token(obj);
8405 err = err ? : bpf_object__probe_loading(obj);
fe62de31 8406 err = err ? : bpf_object__load_vmlinux_btf(obj, false);
8601fd42 8407 err = err ? : bpf_object__resolve_externs(obj, obj->kconfig);
0d13bfce 8408 err = err ? : bpf_object__sanitize_maps(obj);
590a0088 8409 err = err ? : bpf_object__init_kern_struct_ops_maps(obj);
fe9d049c 8410 err = err ? : bpf_object_adjust_struct_ops_autoload(obj);
e7b924ca 8411 err = err ? : bpf_object__relocate(obj, obj->btf_custom_path ? : target_btf_path);
1004742d 8412 err = err ? : bpf_object__sanitize_and_load_btf(obj);
dac645b9 8413 err = err ? : bpf_object__create_maps(obj);
e7b924ca 8414 err = err ? : bpf_object__load_progs(obj, extra_log_level);
341ac5ff 8415 err = err ? : bpf_object_init_prog_arrays(obj);
8d1608d7 8416 err = err ? : bpf_object_prepare_struct_ops(obj);
a6ed02ca 8417
67234743
AS
8418 if (obj->gen_loader) {
8419 /* reset FDs */
4729445b
KKD
8420 if (obj->btf)
8421 btf__set_fd(obj->btf, -1);
67234743 8422 if (!err)
ba05fd36 8423 err = bpf_gen__finish(obj->gen_loader, obj->nr_programs, obj->nr_maps);
67234743
AS
8424 }
8425
9dbe6015
KKD
8426 /* clean up fd_array */
8427 zfree(&obj->fd_array);
8428
4f33a53d
AN
8429 /* clean up module BTFs */
8430 for (i = 0; i < obj->btf_module_cnt; i++) {
91abb4a6 8431 close(obj->btf_modules[i].fd);
4f33a53d
AN
8432 btf__free(obj->btf_modules[i].btf);
8433 free(obj->btf_modules[i].name);
8434 }
8435 free(obj->btf_modules);
8436
8437 /* clean up vmlinux BTF */
a6ed02ca
KS
8438 btf__free(obj->btf_vmlinux);
8439 obj->btf_vmlinux = NULL;
8440
d9297581
AN
8441 obj->loaded = true; /* doesn't matter if successfully or not */
8442
0d13bfce
AN
8443 if (err)
8444 goto out;
52d3352e
WN
8445
8446 return 0;
8447out:
ec6d5f47
THJ
8448 /* unpin any maps that were auto-pinned during load */
8449 for (i = 0; i < obj->nr_maps; i++)
8450 if (obj->maps[i].pinned && !obj->maps[i].reused)
8451 bpf_map__unpin(&obj->maps[i], NULL);
8452
4a404a7e 8453 bpf_object_unload(obj);
be18010e 8454 pr_warn("failed to load object '%s'\n", obj->path);
e9fc3ce9 8455 return libbpf_err(err);
52d3352e
WN
8456}
8457
e7b924ca
AN
8458int bpf_object__load(struct bpf_object *obj)
8459{
8460 return bpf_object_load(obj, 0, NULL);
60276f98
QM
8461}
8462
196f8487
THJ
8463static int make_parent_dir(const char *path)
8464{
8465 char *cp, errmsg[STRERR_BUFSIZE];
8466 char *dname, *dir;
8467 int err = 0;
8468
8469 dname = strdup(path);
8470 if (dname == NULL)
8471 return -ENOMEM;
8472
8473 dir = dirname(dname);
8474 if (mkdir(dir, 0700) && errno != EEXIST)
8475 err = -errno;
8476
8477 free(dname);
8478 if (err) {
8479 cp = libbpf_strerror_r(-err, errmsg, sizeof(errmsg));
8480 pr_warn("failed to mkdir %s: %s\n", path, cp);
8481 }
8482 return err;
8483}
8484
f367540c
JS
8485static int check_path(const char *path)
8486{
1ce6a9fc 8487 char *cp, errmsg[STRERR_BUFSIZE];
f367540c
JS
8488 struct statfs st_fs;
8489 char *dname, *dir;
8490 int err = 0;
8491
8492 if (path == NULL)
8493 return -EINVAL;
8494
8495 dname = strdup(path);
8496 if (dname == NULL)
8497 return -ENOMEM;
8498
8499 dir = dirname(dname);
8500 if (statfs(dir, &st_fs)) {
24d6a808 8501 cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
be18010e 8502 pr_warn("failed to statfs %s: %s\n", dir, cp);
f367540c
JS
8503 err = -errno;
8504 }
8505 free(dname);
8506
8507 if (!err && st_fs.f_type != BPF_FS_MAGIC) {
be18010e 8508 pr_warn("specified path %s is not on BPF FS\n", path);
f367540c
JS
8509 err = -EINVAL;
8510 }
8511
8512 return err;
8513}
8514
cf90a20d 8515int bpf_program__pin(struct bpf_program *prog, const char *path)
f367540c 8516{
1ce6a9fc 8517 char *cp, errmsg[STRERR_BUFSIZE];
f367540c
JS
8518 int err;
8519
cf90a20d
AN
8520 if (prog->fd < 0) {
8521 pr_warn("prog '%s': can't pin program that wasn't loaded\n", prog->name);
8522 return libbpf_err(-EINVAL);
8523 }
8524
196f8487
THJ
8525 err = make_parent_dir(path);
8526 if (err)
e9fc3ce9 8527 return libbpf_err(err);
196f8487 8528
f367540c
JS
8529 err = check_path(path);
8530 if (err)
e9fc3ce9 8531 return libbpf_err(err);
f367540c 8532
cf90a20d 8533 if (bpf_obj_pin(prog->fd, path)) {
23ab656b
THJ
8534 err = -errno;
8535 cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg));
cf90a20d 8536 pr_warn("prog '%s': failed to pin at '%s': %s\n", prog->name, path, cp);
e9fc3ce9 8537 return libbpf_err(err);
f367540c 8538 }
f367540c 8539
cf90a20d 8540 pr_debug("prog '%s': pinned at '%s'\n", prog->name, path);
f367540c
JS
8541 return 0;
8542}
8543
cf90a20d 8544int bpf_program__unpin(struct bpf_program *prog, const char *path)
0c19a9fb
SF
8545{
8546 int err;
8547
cf90a20d
AN
8548 if (prog->fd < 0) {
8549 pr_warn("prog '%s': can't unpin program that wasn't loaded\n", prog->name);
e9fc3ce9 8550 return libbpf_err(-EINVAL);
0c19a9fb
SF
8551 }
8552
f367540c
JS
8553 err = check_path(path);
8554 if (err)
e9fc3ce9 8555 return libbpf_err(err);
f367540c 8556
cf90a20d 8557 err = unlink(path);
0c19a9fb 8558 if (err)
e9fc3ce9 8559 return libbpf_err(-errno);
0c19a9fb 8560
cf90a20d 8561 pr_debug("prog '%s': unpinned from '%s'\n", prog->name, path);
f367540c
JS
8562 return 0;
8563}
8564
b6989f35
JS
8565int bpf_map__pin(struct bpf_map *map, const char *path)
8566{
1ce6a9fc 8567 char *cp, errmsg[STRERR_BUFSIZE];
b6989f35
JS
8568 int err;
8569
b6989f35 8570 if (map == NULL) {
be18010e 8571 pr_warn("invalid map pointer\n");
e9fc3ce9 8572 return libbpf_err(-EINVAL);
b6989f35
JS
8573 }
8574
4580b25f
THJ
8575 if (map->pin_path) {
8576 if (path && strcmp(path, map->pin_path)) {
8577 pr_warn("map '%s' already has pin path '%s' different from '%s'\n",
8578 bpf_map__name(map), map->pin_path, path);
e9fc3ce9 8579 return libbpf_err(-EINVAL);
4580b25f
THJ
8580 } else if (map->pinned) {
8581 pr_debug("map '%s' already pinned at '%s'; not re-pinning\n",
8582 bpf_map__name(map), map->pin_path);
8583 return 0;
8584 }
8585 } else {
8586 if (!path) {
8587 pr_warn("missing a path to pin map '%s' at\n",
8588 bpf_map__name(map));
e9fc3ce9 8589 return libbpf_err(-EINVAL);
4580b25f
THJ
8590 } else if (map->pinned) {
8591 pr_warn("map '%s' already pinned\n", bpf_map__name(map));
e9fc3ce9 8592 return libbpf_err(-EEXIST);
4580b25f
THJ
8593 }
8594
8595 map->pin_path = strdup(path);
8596 if (!map->pin_path) {
8597 err = -errno;
8598 goto out_err;
8599 }
b6989f35
JS
8600 }
8601
196f8487
THJ
8602 err = make_parent_dir(map->pin_path);
8603 if (err)
e9fc3ce9 8604 return libbpf_err(err);
196f8487 8605
4580b25f
THJ
8606 err = check_path(map->pin_path);
8607 if (err)
e9fc3ce9 8608 return libbpf_err(err);
4580b25f
THJ
8609
8610 if (bpf_obj_pin(map->fd, map->pin_path)) {
8611 err = -errno;
8612 goto out_err;
8613 }
8614
8615 map->pinned = true;
8616 pr_debug("pinned map '%s'\n", map->pin_path);
0c19a9fb 8617
b6989f35 8618 return 0;
4580b25f
THJ
8619
8620out_err:
8621 cp = libbpf_strerror_r(-err, errmsg, sizeof(errmsg));
8622 pr_warn("failed to pin map: %s\n", cp);
e9fc3ce9 8623 return libbpf_err(err);
b6989f35
JS
8624}
8625
0c19a9fb
SF
8626int bpf_map__unpin(struct bpf_map *map, const char *path)
8627{
8628 int err;
8629
0c19a9fb 8630 if (map == NULL) {
be18010e 8631 pr_warn("invalid map pointer\n");
e9fc3ce9 8632 return libbpf_err(-EINVAL);
0c19a9fb
SF
8633 }
8634
4580b25f
THJ
8635 if (map->pin_path) {
8636 if (path && strcmp(path, map->pin_path)) {
8637 pr_warn("map '%s' already has pin path '%s' different from '%s'\n",
8638 bpf_map__name(map), map->pin_path, path);
e9fc3ce9 8639 return libbpf_err(-EINVAL);
4580b25f
THJ
8640 }
8641 path = map->pin_path;
8642 } else if (!path) {
8643 pr_warn("no path to unpin map '%s' from\n",
8644 bpf_map__name(map));
e9fc3ce9 8645 return libbpf_err(-EINVAL);
4580b25f
THJ
8646 }
8647
8648 err = check_path(path);
8649 if (err)
e9fc3ce9 8650 return libbpf_err(err);
4580b25f 8651
0c19a9fb
SF
8652 err = unlink(path);
8653 if (err != 0)
e9fc3ce9 8654 return libbpf_err(-errno);
4580b25f
THJ
8655
8656 map->pinned = false;
8657 pr_debug("unpinned map '%s' from '%s'\n", bpf_map__name(map), path);
0c19a9fb
SF
8658
8659 return 0;
8660}
8661
4580b25f
THJ
8662int bpf_map__set_pin_path(struct bpf_map *map, const char *path)
8663{
8664 char *new = NULL;
8665
8666 if (path) {
8667 new = strdup(path);
8668 if (!new)
e9fc3ce9 8669 return libbpf_err(-errno);
4580b25f
THJ
8670 }
8671
8672 free(map->pin_path);
8673 map->pin_path = new;
8674 return 0;
8675}
8676
20eccf29
AN
8677__alias(bpf_map__pin_path)
8678const char *bpf_map__get_pin_path(const struct bpf_map *map);
4580b25f 8679
e244d34d
EL
8680const char *bpf_map__pin_path(const struct bpf_map *map)
8681{
8682 return map->pin_path;
8683}
8684
4580b25f
THJ
8685bool bpf_map__is_pinned(const struct bpf_map *map)
8686{
8687 return map->pinned;
8688}
8689
9cf309c5
THJ
8690static void sanitize_pin_path(char *s)
8691{
8692 /* bpffs disallows periods in path names */
8693 while (*s) {
8694 if (*s == '.')
8695 *s = '_';
8696 s++;
8697 }
8698}
8699
0c19a9fb 8700int bpf_object__pin_maps(struct bpf_object *obj, const char *path)
d5148d85 8701{
d5148d85
JS
8702 struct bpf_map *map;
8703 int err;
8704
8705 if (!obj)
e9fc3ce9 8706 return libbpf_err(-ENOENT);
d5148d85
JS
8707
8708 if (!obj->loaded) {
be18010e 8709 pr_warn("object not yet loaded; load it first\n");
e9fc3ce9 8710 return libbpf_err(-ENOENT);
d5148d85
JS
8711 }
8712
f74a53d9 8713 bpf_object__for_each_map(map, obj) {
4580b25f 8714 char *pin_path = NULL;
0c19a9fb 8715 char buf[PATH_MAX];
0c19a9fb 8716
ec41817b 8717 if (!map->autocreate)
229fae38
SC
8718 continue;
8719
4580b25f 8720 if (path) {
e588c116
WY
8721 err = pathname_concat(buf, sizeof(buf), path, bpf_map__name(map));
8722 if (err)
4580b25f 8723 goto err_unpin_maps;
9cf309c5 8724 sanitize_pin_path(buf);
4580b25f
THJ
8725 pin_path = buf;
8726 } else if (!map->pin_path) {
8727 continue;
0c19a9fb
SF
8728 }
8729
4580b25f 8730 err = bpf_map__pin(map, pin_path);
0c19a9fb
SF
8731 if (err)
8732 goto err_unpin_maps;
8733 }
8734
8735 return 0;
8736
8737err_unpin_maps:
bcc40fc0 8738 while ((map = bpf_object__prev_map(obj, map))) {
4580b25f 8739 if (!map->pin_path)
0c19a9fb
SF
8740 continue;
8741
4580b25f 8742 bpf_map__unpin(map, NULL);
0c19a9fb
SF
8743 }
8744
e9fc3ce9 8745 return libbpf_err(err);
0c19a9fb
SF
8746}
8747
8748int bpf_object__unpin_maps(struct bpf_object *obj, const char *path)
8749{
8750 struct bpf_map *map;
8751 int err;
8752
8753 if (!obj)
e9fc3ce9 8754 return libbpf_err(-ENOENT);
0c19a9fb 8755
f74a53d9 8756 bpf_object__for_each_map(map, obj) {
4580b25f 8757 char *pin_path = NULL;
d5148d85 8758 char buf[PATH_MAX];
d5148d85 8759
4580b25f 8760 if (path) {
e588c116
WY
8761 err = pathname_concat(buf, sizeof(buf), path, bpf_map__name(map));
8762 if (err)
8763 return libbpf_err(err);
9cf309c5 8764 sanitize_pin_path(buf);
4580b25f
THJ
8765 pin_path = buf;
8766 } else if (!map->pin_path) {
8767 continue;
8768 }
d5148d85 8769
4580b25f 8770 err = bpf_map__unpin(map, pin_path);
d5148d85 8771 if (err)
e9fc3ce9 8772 return libbpf_err(err);
d5148d85
JS
8773 }
8774
0c19a9fb
SF
8775 return 0;
8776}
8777
8778int bpf_object__pin_programs(struct bpf_object *obj, const char *path)
8779{
8780 struct bpf_program *prog;
e588c116 8781 char buf[PATH_MAX];
0c19a9fb
SF
8782 int err;
8783
8784 if (!obj)
e9fc3ce9 8785 return libbpf_err(-ENOENT);
0c19a9fb
SF
8786
8787 if (!obj->loaded) {
be18010e 8788 pr_warn("object not yet loaded; load it first\n");
e9fc3ce9 8789 return libbpf_err(-ENOENT);
0c19a9fb
SF
8790 }
8791
0c19a9fb 8792 bpf_object__for_each_program(prog, obj) {
e588c116
WY
8793 err = pathname_concat(buf, sizeof(buf), path, prog->name);
8794 if (err)
0c19a9fb 8795 goto err_unpin_programs;
0c19a9fb
SF
8796
8797 err = bpf_program__pin(prog, buf);
8798 if (err)
8799 goto err_unpin_programs;
8800 }
8801
8802 return 0;
8803
8804err_unpin_programs:
bcc40fc0 8805 while ((prog = bpf_object__prev_program(obj, prog))) {
e588c116 8806 if (pathname_concat(buf, sizeof(buf), path, prog->name))
0c19a9fb
SF
8807 continue;
8808
8809 bpf_program__unpin(prog, buf);
8810 }
8811
e9fc3ce9 8812 return libbpf_err(err);
0c19a9fb
SF
8813}
8814
8815int bpf_object__unpin_programs(struct bpf_object *obj, const char *path)
8816{
8817 struct bpf_program *prog;
8818 int err;
8819
8820 if (!obj)
e9fc3ce9 8821 return libbpf_err(-ENOENT);
0c19a9fb 8822
d5148d85
JS
8823 bpf_object__for_each_program(prog, obj) {
8824 char buf[PATH_MAX];
d5148d85 8825
e588c116
WY
8826 err = pathname_concat(buf, sizeof(buf), path, prog->name);
8827 if (err)
8828 return libbpf_err(err);
d5148d85 8829
0c19a9fb 8830 err = bpf_program__unpin(prog, buf);
d5148d85 8831 if (err)
e9fc3ce9 8832 return libbpf_err(err);
d5148d85
JS
8833 }
8834
8835 return 0;
8836}
8837
0c19a9fb
SF
8838int bpf_object__pin(struct bpf_object *obj, const char *path)
8839{
8840 int err;
8841
8842 err = bpf_object__pin_maps(obj, path);
8843 if (err)
e9fc3ce9 8844 return libbpf_err(err);
0c19a9fb
SF
8845
8846 err = bpf_object__pin_programs(obj, path);
8847 if (err) {
8848 bpf_object__unpin_maps(obj, path);
e9fc3ce9 8849 return libbpf_err(err);
0c19a9fb
SF
8850 }
8851
8852 return 0;
8853}
8854
068ca522
DX
8855int bpf_object__unpin(struct bpf_object *obj, const char *path)
8856{
8857 int err;
8858
8859 err = bpf_object__unpin_programs(obj, path);
8860 if (err)
8861 return libbpf_err(err);
8862
8863 err = bpf_object__unpin_maps(obj, path);
8864 if (err)
8865 return libbpf_err(err);
8866
8867 return 0;
8868}
8869
2d39d7c5
AN
8870static void bpf_map__destroy(struct bpf_map *map)
8871{
646f02ff
AN
8872 if (map->inner_map) {
8873 bpf_map__destroy(map->inner_map);
8874 zfree(&map->inner_map);
8875 }
8876
8877 zfree(&map->init_slots);
8878 map->init_slots_sz = 0;
8879
2e7ba4f8
AN
8880 if (map->mmaped && map->mmaped != map->obj->arena_data)
8881 munmap(map->mmaped, bpf_map_mmap_sz(map));
8882 map->mmaped = NULL;
2d39d7c5
AN
8883
8884 if (map->st_ops) {
8885 zfree(&map->st_ops->data);
8886 zfree(&map->st_ops->progs);
8887 zfree(&map->st_ops->kern_func_off);
8888 zfree(&map->st_ops);
8889 }
8890
8891 zfree(&map->name);
aed65917 8892 zfree(&map->real_name);
2d39d7c5
AN
8893 zfree(&map->pin_path);
8894
8895 if (map->fd >= 0)
8896 zclose(map->fd);
8897}
8898
1a5e3fb1
WN
8899void bpf_object__close(struct bpf_object *obj)
8900{
a5b8bd47
WN
8901 size_t i;
8902
50450fc7 8903 if (IS_ERR_OR_NULL(obj))
1a5e3fb1
WN
8904 return;
8905
2e4913e0
AN
8906 usdt_manager_free(obj->usdt_man);
8907 obj->usdt_man = NULL;
8908
67234743 8909 bpf_gen__free(obj->gen_loader);
1a5e3fb1 8910 bpf_object__elf_finish(obj);
4a404a7e 8911 bpf_object_unload(obj);
8a138aed 8912 btf__free(obj->btf);
29d67fde 8913 btf__free(obj->btf_vmlinux);
2993e051 8914 btf_ext__free(obj->btf_ext);
1a5e3fb1 8915
2d39d7c5
AN
8916 for (i = 0; i < obj->nr_maps; i++)
8917 bpf_map__destroy(&obj->maps[i]);
d859900c 8918
1373ff59 8919 zfree(&obj->btf_custom_path);
8601fd42 8920 zfree(&obj->kconfig);
5964a223
DM
8921
8922 for (i = 0; i < obj->nr_extern; i++)
8923 zfree(&obj->externs[i].essent_name);
8924
166750bc
AN
8925 zfree(&obj->externs);
8926 obj->nr_extern = 0;
8927
9d759a9b
WN
8928 zfree(&obj->maps);
8929 obj->nr_maps = 0;
a5b8bd47
WN
8930
8931 if (obj->programs && obj->nr_programs) {
8932 for (i = 0; i < obj->nr_programs; i++)
8933 bpf_program__exit(&obj->programs[i]);
8934 }
8935 zfree(&obj->programs);
8936
6b434b61
AN
8937 zfree(&obj->feat_cache);
8938 zfree(&obj->token_path);
8939 if (obj->token_fd > 0)
8940 close(obj->token_fd);
8941
2e7ba4f8
AN
8942 zfree(&obj->arena_data);
8943
1a5e3fb1
WN
8944 free(obj);
8945}
aa9b1ac3 8946
a324aae3 8947const char *bpf_object__name(const struct bpf_object *obj)
acf860ae 8948{
e9fc3ce9 8949 return obj ? obj->name : libbpf_err_ptr(-EINVAL);
acf860ae
WN
8950}
8951
a324aae3 8952unsigned int bpf_object__kversion(const struct bpf_object *obj)
45825d8a 8953{
a7fe0450 8954 return obj ? obj->kern_version : 0;
45825d8a
WN
8955}
8956
a324aae3 8957struct btf *bpf_object__btf(const struct bpf_object *obj)
789f6bab
AI
8958{
8959 return obj ? obj->btf : NULL;
8960}
8961
8a138aed
MKL
8962int bpf_object__btf_fd(const struct bpf_object *obj)
8963{
8964 return obj->btf ? btf__fd(obj->btf) : -1;
8965}
8966
155f556d
RDT
8967int bpf_object__set_kversion(struct bpf_object *obj, __u32 kern_version)
8968{
8969 if (obj->loaded)
e9fc3ce9 8970 return libbpf_err(-EINVAL);
155f556d
RDT
8971
8972 obj->kern_version = kern_version;
8973
8974 return 0;
8975}
8976
67234743
AS
8977int bpf_object__gen_loader(struct bpf_object *obj, struct gen_loader_opts *opts)
8978{
8979 struct bpf_gen *gen;
8980
8981 if (!opts)
8982 return -EFAULT;
8983 if (!OPTS_VALID(opts, gen_loader_opts))
8984 return -EINVAL;
8985 gen = calloc(sizeof(*gen), 1);
8986 if (!gen)
8987 return -ENOMEM;
8988 gen->opts = opts;
8989 obj->gen_loader = gen;
8990 return 0;
8991}
8992
eac7d845 8993static struct bpf_program *
a324aae3
AN
8994__bpf_program__iter(const struct bpf_program *p, const struct bpf_object *obj,
8995 bool forward)
aa9b1ac3 8996{
a83d6e76 8997 size_t nr_programs = obj->nr_programs;
0c19a9fb 8998 ssize_t idx;
aa9b1ac3 8999
a83d6e76 9000 if (!nr_programs)
aa9b1ac3 9001 return NULL;
aa9b1ac3 9002
a83d6e76
MKL
9003 if (!p)
9004 /* Iter from the beginning */
9005 return forward ? &obj->programs[0] :
9006 &obj->programs[nr_programs - 1];
9007
0c19a9fb 9008 if (p->obj != obj) {
be18010e 9009 pr_warn("error: program handler doesn't match object\n");
e9fc3ce9 9010 return errno = EINVAL, NULL;
aa9b1ac3
WN
9011 }
9012
a83d6e76 9013 idx = (p - obj->programs) + (forward ? 1 : -1);
0c19a9fb 9014 if (idx >= obj->nr_programs || idx < 0)
aa9b1ac3
WN
9015 return NULL;
9016 return &obj->programs[idx];
9017}
9018
2088a3a7
HC
9019struct bpf_program *
9020bpf_object__next_program(const struct bpf_object *obj, struct bpf_program *prev)
eac7d845
JK
9021{
9022 struct bpf_program *prog = prev;
9023
9024 do {
a83d6e76 9025 prog = __bpf_program__iter(prog, obj, true);
c3c55696 9026 } while (prog && prog_is_subprog(obj, prog));
0c19a9fb
SF
9027
9028 return prog;
9029}
9030
2088a3a7
HC
9031struct bpf_program *
9032bpf_object__prev_program(const struct bpf_object *obj, struct bpf_program *next)
0c19a9fb
SF
9033{
9034 struct bpf_program *prog = next;
9035
0c19a9fb 9036 do {
a83d6e76 9037 prog = __bpf_program__iter(prog, obj, false);
c3c55696 9038 } while (prog && prog_is_subprog(obj, prog));
eac7d845
JK
9039
9040 return prog;
9041}
9042
9aba3613
JK
9043void bpf_program__set_ifindex(struct bpf_program *prog, __u32 ifindex)
9044{
9045 prog->prog_ifindex = ifindex;
9046}
9047
01af3bf0
AN
9048const char *bpf_program__name(const struct bpf_program *prog)
9049{
9050 return prog->name;
9051}
9052
52109584
AN
9053const char *bpf_program__section_name(const struct bpf_program *prog)
9054{
9055 return prog->sec_name;
9056}
9057
d9297581
AN
9058bool bpf_program__autoload(const struct bpf_program *prog)
9059{
a3820c48 9060 return prog->autoload;
d9297581
AN
9061}
9062
9063int bpf_program__set_autoload(struct bpf_program *prog, bool autoload)
9064{
9065 if (prog->obj->loaded)
e9fc3ce9 9066 return libbpf_err(-EINVAL);
d9297581 9067
a3820c48 9068 prog->autoload = autoload;
d9297581
AN
9069 return 0;
9070}
9071
43cb8cba
HL
9072bool bpf_program__autoattach(const struct bpf_program *prog)
9073{
9074 return prog->autoattach;
9075}
9076
9077void bpf_program__set_autoattach(struct bpf_program *prog, bool autoattach)
9078{
9079 prog->autoattach = autoattach;
9080}
9081
65a7fa2e
AN
9082const struct bpf_insn *bpf_program__insns(const struct bpf_program *prog)
9083{
9084 return prog->insns;
9085}
9086
9087size_t bpf_program__insn_cnt(const struct bpf_program *prog)
9088{
9089 return prog->insns_cnt;
9090}
9091
b63b3c49
JO
9092int bpf_program__set_insns(struct bpf_program *prog,
9093 struct bpf_insn *new_insns, size_t new_insn_cnt)
9094{
9095 struct bpf_insn *insns;
9096
9097 if (prog->obj->loaded)
9098 return -EBUSY;
9099
9100 insns = libbpf_reallocarray(prog->insns, new_insn_cnt, sizeof(*insns));
8a0260db
AN
9101 /* NULL is a valid return from reallocarray if the new count is zero */
9102 if (!insns && new_insn_cnt) {
b63b3c49
JO
9103 pr_warn("prog '%s': failed to realloc prog code\n", prog->name);
9104 return -ENOMEM;
9105 }
9106 memcpy(insns, new_insns, new_insn_cnt * sizeof(*insns));
9107
9108 prog->insns = insns;
9109 prog->insns_cnt = new_insn_cnt;
9110 return 0;
9111}
9112
cf90a20d 9113int bpf_program__fd(const struct bpf_program *prog)
b580563e 9114{
1e960043 9115 if (!prog)
e9fc3ce9 9116 return libbpf_err(-EINVAL);
1e960043 9117
cf90a20d 9118 if (prog->fd < 0)
e9fc3ce9 9119 return libbpf_err(-ENOENT);
b580563e 9120
cf90a20d 9121 return prog->fd;
aa9b1ac3 9122}
9d759a9b 9123
20eccf29
AN
9124__alias(bpf_program__type)
9125enum bpf_prog_type bpf_program__get_type(const struct bpf_program *prog);
9126
9127enum bpf_prog_type bpf_program__type(const struct bpf_program *prog)
f1eead9e
AN
9128{
9129 return prog->type;
9130}
9131
c628747c
AN
9132static size_t custom_sec_def_cnt;
9133static struct bpf_sec_def *custom_sec_defs;
9134static struct bpf_sec_def custom_fallback_def;
9135static bool has_custom_fallback_def;
9136static int last_custom_sec_def_handler_id;
9137
93442f13 9138int bpf_program__set_type(struct bpf_program *prog, enum bpf_prog_type type)
5f44e4c8 9139{
93442f13
GS
9140 if (prog->obj->loaded)
9141 return libbpf_err(-EBUSY);
9142
c628747c
AN
9143 /* if type is not changed, do nothing */
9144 if (prog->type == type)
9145 return 0;
9146
5f44e4c8 9147 prog->type = type;
c628747c
AN
9148
9149 /* If a program type was changed, we need to reset associated SEC()
9150 * handler, as it will be invalid now. The only exception is a generic
9151 * fallback handler, which by definition is program type-agnostic and
9152 * is a catch-all custom handler, optionally set by the application,
9153 * so should be able to handle any type of BPF program.
9154 */
9155 if (prog->sec_def != &custom_fallback_def)
9156 prog->sec_def = NULL;
93442f13 9157 return 0;
5f44e4c8
WN
9158}
9159
20eccf29
AN
9160__alias(bpf_program__expected_attach_type)
9161enum bpf_attach_type bpf_program__get_expected_attach_type(const struct bpf_program *prog);
9162
9163enum bpf_attach_type bpf_program__expected_attach_type(const struct bpf_program *prog)
f1eead9e
AN
9164{
9165 return prog->expected_attach_type;
9166}
9167
93442f13 9168int bpf_program__set_expected_attach_type(struct bpf_program *prog,
16962b24 9169 enum bpf_attach_type type)
d7be143b 9170{
93442f13
GS
9171 if (prog->obj->loaded)
9172 return libbpf_err(-EBUSY);
9173
d7be143b 9174 prog->expected_attach_type = type;
93442f13 9175 return 0;
d7be143b
AI
9176}
9177
a6ca7158
AN
9178__u32 bpf_program__flags(const struct bpf_program *prog)
9179{
9180 return prog->prog_flags;
9181}
9182
8cccee9e 9183int bpf_program__set_flags(struct bpf_program *prog, __u32 flags)
a6ca7158
AN
9184{
9185 if (prog->obj->loaded)
9186 return libbpf_err(-EBUSY);
9187
8cccee9e 9188 prog->prog_flags = flags;
a6ca7158
AN
9189 return 0;
9190}
9191
dbdd2c7f
AN
9192__u32 bpf_program__log_level(const struct bpf_program *prog)
9193{
9194 return prog->log_level;
9195}
9196
9197int bpf_program__set_log_level(struct bpf_program *prog, __u32 log_level)
9198{
9199 if (prog->obj->loaded)
9200 return libbpf_err(-EBUSY);
9201
9202 prog->log_level = log_level;
9203 return 0;
9204}
9205
b3ce9079
AN
9206const char *bpf_program__log_buf(const struct bpf_program *prog, size_t *log_size)
9207{
9208 *log_size = prog->log_size;
9209 return prog->log_buf;
9210}
9211
9212int bpf_program__set_log_buf(struct bpf_program *prog, char *log_buf, size_t log_size)
9213{
9214 if (log_size && !log_buf)
9215 return -EINVAL;
9216 if (prog->log_size > UINT_MAX)
9217 return -EINVAL;
9218 if (prog->obj->loaded)
9219 return -EBUSY;
9220
9221 prog->log_buf = log_buf;
9222 prog->log_size = log_size;
a6ca7158
AN
9223 return 0;
9224}
9225
15ea31fa 9226#define SEC_DEF(sec_pfx, ptype, atype, flags, ...) { \
697f104d 9227 .sec = (char *)sec_pfx, \
d7a18ea7 9228 .prog_type = BPF_PROG_TYPE_##ptype, \
15ea31fa
AN
9229 .expected_attach_type = atype, \
9230 .cookie = (long)(flags), \
4fa5bcfe 9231 .prog_prepare_load_fn = libbpf_prepare_prog_load, \
d7a18ea7
AN
9232 __VA_ARGS__ \
9233}
9234
4fa5bcfe 9235static int attach_kprobe(const struct bpf_program *prog, long cookie, struct bpf_link **link);
39f8dc43 9236static int attach_uprobe(const struct bpf_program *prog, long cookie, struct bpf_link **link);
708ac5be 9237static int attach_ksyscall(const struct bpf_program *prog, long cookie, struct bpf_link **link);
2e4913e0 9238static int attach_usdt(const struct bpf_program *prog, long cookie, struct bpf_link **link);
4fa5bcfe
AN
9239static int attach_tp(const struct bpf_program *prog, long cookie, struct bpf_link **link);
9240static int attach_raw_tp(const struct bpf_program *prog, long cookie, struct bpf_link **link);
9241static int attach_trace(const struct bpf_program *prog, long cookie, struct bpf_link **link);
ddc6b049 9242static int attach_kprobe_multi(const struct bpf_program *prog, long cookie, struct bpf_link **link);
5bfdd32d 9243static int attach_uprobe_multi(const struct bpf_program *prog, long cookie, struct bpf_link **link);
4fa5bcfe
AN
9244static int attach_lsm(const struct bpf_program *prog, long cookie, struct bpf_link **link);
9245static int attach_iter(const struct bpf_program *prog, long cookie, struct bpf_link **link);
d7a18ea7 9246
d7a18ea7 9247static const struct bpf_sec_def section_defs[] = {
450b167f
AN
9248 SEC_DEF("socket", SOCKET_FILTER, 0, SEC_NONE),
9249 SEC_DEF("sk_reuseport/migrate", SK_REUSEPORT, BPF_SK_REUSEPORT_SELECT_OR_MIGRATE, SEC_ATTACHABLE),
9250 SEC_DEF("sk_reuseport", SK_REUSEPORT, BPF_SK_REUSEPORT_SELECT, SEC_ATTACHABLE),
9af8efc4 9251 SEC_DEF("kprobe+", KPROBE, 0, SEC_NONE, attach_kprobe),
39f8dc43 9252 SEC_DEF("uprobe+", KPROBE, 0, SEC_NONE, attach_uprobe),
c4cac71f 9253 SEC_DEF("uprobe.s+", KPROBE, 0, SEC_SLEEPABLE, attach_uprobe),
9af8efc4 9254 SEC_DEF("kretprobe+", KPROBE, 0, SEC_NONE, attach_kprobe),
39f8dc43 9255 SEC_DEF("uretprobe+", KPROBE, 0, SEC_NONE, attach_uprobe),
c4cac71f 9256 SEC_DEF("uretprobe.s+", KPROBE, 0, SEC_SLEEPABLE, attach_uprobe),
9af8efc4
AN
9257 SEC_DEF("kprobe.multi+", KPROBE, BPF_TRACE_KPROBE_MULTI, SEC_NONE, attach_kprobe_multi),
9258 SEC_DEF("kretprobe.multi+", KPROBE, BPF_TRACE_KPROBE_MULTI, SEC_NONE, attach_kprobe_multi),
5bfdd32d
JO
9259 SEC_DEF("uprobe.multi+", KPROBE, BPF_TRACE_UPROBE_MULTI, SEC_NONE, attach_uprobe_multi),
9260 SEC_DEF("uretprobe.multi+", KPROBE, BPF_TRACE_UPROBE_MULTI, SEC_NONE, attach_uprobe_multi),
9261 SEC_DEF("uprobe.multi.s+", KPROBE, BPF_TRACE_UPROBE_MULTI, SEC_SLEEPABLE, attach_uprobe_multi),
9262 SEC_DEF("uretprobe.multi.s+", KPROBE, BPF_TRACE_UPROBE_MULTI, SEC_SLEEPABLE, attach_uprobe_multi),
708ac5be
AN
9263 SEC_DEF("ksyscall+", KPROBE, 0, SEC_NONE, attach_ksyscall),
9264 SEC_DEF("kretsyscall+", KPROBE, 0, SEC_NONE, attach_ksyscall),
5902da6d
JO
9265 SEC_DEF("usdt+", KPROBE, 0, SEC_USDT, attach_usdt),
9266 SEC_DEF("usdt.s+", KPROBE, 0, SEC_USDT | SEC_SLEEPABLE, attach_usdt),
fe20ce3a
DB
9267 SEC_DEF("tc/ingress", SCHED_CLS, BPF_TCX_INGRESS, SEC_NONE), /* alias for tcx */
9268 SEC_DEF("tc/egress", SCHED_CLS, BPF_TCX_EGRESS, SEC_NONE), /* alias for tcx */
9269 SEC_DEF("tcx/ingress", SCHED_CLS, BPF_TCX_INGRESS, SEC_NONE),
9270 SEC_DEF("tcx/egress", SCHED_CLS, BPF_TCX_EGRESS, SEC_NONE),
9271 SEC_DEF("tc", SCHED_CLS, 0, SEC_NONE), /* deprecated / legacy, use tcx */
9272 SEC_DEF("classifier", SCHED_CLS, 0, SEC_NONE), /* deprecated / legacy, use tcx */
9273 SEC_DEF("action", SCHED_ACT, 0, SEC_NONE), /* deprecated / legacy, use tcx */
05c31b4a
DB
9274 SEC_DEF("netkit/primary", SCHED_CLS, BPF_NETKIT_PRIMARY, SEC_NONE),
9275 SEC_DEF("netkit/peer", SCHED_CLS, BPF_NETKIT_PEER, SEC_NONE),
9af8efc4
AN
9276 SEC_DEF("tracepoint+", TRACEPOINT, 0, SEC_NONE, attach_tp),
9277 SEC_DEF("tp+", TRACEPOINT, 0, SEC_NONE, attach_tp),
9278 SEC_DEF("raw_tracepoint+", RAW_TRACEPOINT, 0, SEC_NONE, attach_raw_tp),
9279 SEC_DEF("raw_tp+", RAW_TRACEPOINT, 0, SEC_NONE, attach_raw_tp),
9280 SEC_DEF("raw_tracepoint.w+", RAW_TRACEPOINT_WRITABLE, 0, SEC_NONE, attach_raw_tp),
9281 SEC_DEF("raw_tp.w+", RAW_TRACEPOINT_WRITABLE, 0, SEC_NONE, attach_raw_tp),
cc7d8f2c
AN
9282 SEC_DEF("tp_btf+", TRACING, BPF_TRACE_RAW_TP, SEC_ATTACH_BTF, attach_trace),
9283 SEC_DEF("fentry+", TRACING, BPF_TRACE_FENTRY, SEC_ATTACH_BTF, attach_trace),
9284 SEC_DEF("fmod_ret+", TRACING, BPF_MODIFY_RETURN, SEC_ATTACH_BTF, attach_trace),
9285 SEC_DEF("fexit+", TRACING, BPF_TRACE_FEXIT, SEC_ATTACH_BTF, attach_trace),
9286 SEC_DEF("fentry.s+", TRACING, BPF_TRACE_FENTRY, SEC_ATTACH_BTF | SEC_SLEEPABLE, attach_trace),
9287 SEC_DEF("fmod_ret.s+", TRACING, BPF_MODIFY_RETURN, SEC_ATTACH_BTF | SEC_SLEEPABLE, attach_trace),
9288 SEC_DEF("fexit.s+", TRACING, BPF_TRACE_FEXIT, SEC_ATTACH_BTF | SEC_SLEEPABLE, attach_trace),
9289 SEC_DEF("freplace+", EXT, 0, SEC_ATTACH_BTF, attach_trace),
9290 SEC_DEF("lsm+", LSM, BPF_LSM_MAC, SEC_ATTACH_BTF, attach_lsm),
9291 SEC_DEF("lsm.s+", LSM, BPF_LSM_MAC, SEC_ATTACH_BTF | SEC_SLEEPABLE, attach_lsm),
bffcf348 9292 SEC_DEF("lsm_cgroup+", LSM, BPF_LSM_CGROUP, SEC_ATTACH_BTF),
cc7d8f2c
AN
9293 SEC_DEF("iter+", TRACING, BPF_TRACE_ITER, SEC_ATTACH_BTF, attach_iter),
9294 SEC_DEF("iter.s+", TRACING, BPF_TRACE_ITER, SEC_ATTACH_BTF | SEC_SLEEPABLE, attach_iter),
15ea31fa 9295 SEC_DEF("syscall", SYSCALL, 0, SEC_SLEEPABLE),
082c4bfb 9296 SEC_DEF("xdp.frags/devmap", XDP, BPF_XDP_DEVMAP, SEC_XDP_FRAGS),
4a4d4cee 9297 SEC_DEF("xdp/devmap", XDP, BPF_XDP_DEVMAP, SEC_ATTACHABLE),
082c4bfb 9298 SEC_DEF("xdp.frags/cpumap", XDP, BPF_XDP_CPUMAP, SEC_XDP_FRAGS),
4a4d4cee 9299 SEC_DEF("xdp/cpumap", XDP, BPF_XDP_CPUMAP, SEC_ATTACHABLE),
082c4bfb 9300 SEC_DEF("xdp.frags", XDP, BPF_XDP, SEC_XDP_FRAGS),
450b167f
AN
9301 SEC_DEF("xdp", XDP, BPF_XDP, SEC_ATTACHABLE_OPT),
9302 SEC_DEF("perf_event", PERF_EVENT, 0, SEC_NONE),
9303 SEC_DEF("lwt_in", LWT_IN, 0, SEC_NONE),
9304 SEC_DEF("lwt_out", LWT_OUT, 0, SEC_NONE),
9305 SEC_DEF("lwt_xmit", LWT_XMIT, 0, SEC_NONE),
9306 SEC_DEF("lwt_seg6local", LWT_SEG6LOCAL, 0, SEC_NONE),
9307 SEC_DEF("sockops", SOCK_OPS, BPF_CGROUP_SOCK_OPS, SEC_ATTACHABLE_OPT),
9308 SEC_DEF("sk_skb/stream_parser", SK_SKB, BPF_SK_SKB_STREAM_PARSER, SEC_ATTACHABLE_OPT),
9309 SEC_DEF("sk_skb/stream_verdict",SK_SKB, BPF_SK_SKB_STREAM_VERDICT, SEC_ATTACHABLE_OPT),
9310 SEC_DEF("sk_skb", SK_SKB, 0, SEC_NONE),
9311 SEC_DEF("sk_msg", SK_MSG, BPF_SK_MSG_VERDICT, SEC_ATTACHABLE_OPT),
9312 SEC_DEF("lirc_mode2", LIRC_MODE2, BPF_LIRC_MODE2, SEC_ATTACHABLE_OPT),
9313 SEC_DEF("flow_dissector", FLOW_DISSECTOR, BPF_FLOW_DISSECTOR, SEC_ATTACHABLE_OPT),
9314 SEC_DEF("cgroup_skb/ingress", CGROUP_SKB, BPF_CGROUP_INET_INGRESS, SEC_ATTACHABLE_OPT),
9315 SEC_DEF("cgroup_skb/egress", CGROUP_SKB, BPF_CGROUP_INET_EGRESS, SEC_ATTACHABLE_OPT),
9316 SEC_DEF("cgroup/skb", CGROUP_SKB, 0, SEC_NONE),
9317 SEC_DEF("cgroup/sock_create", CGROUP_SOCK, BPF_CGROUP_INET_SOCK_CREATE, SEC_ATTACHABLE),
9318 SEC_DEF("cgroup/sock_release", CGROUP_SOCK, BPF_CGROUP_INET_SOCK_RELEASE, SEC_ATTACHABLE),
9319 SEC_DEF("cgroup/sock", CGROUP_SOCK, BPF_CGROUP_INET_SOCK_CREATE, SEC_ATTACHABLE_OPT),
9320 SEC_DEF("cgroup/post_bind4", CGROUP_SOCK, BPF_CGROUP_INET4_POST_BIND, SEC_ATTACHABLE),
9321 SEC_DEF("cgroup/post_bind6", CGROUP_SOCK, BPF_CGROUP_INET6_POST_BIND, SEC_ATTACHABLE),
9322 SEC_DEF("cgroup/bind4", CGROUP_SOCK_ADDR, BPF_CGROUP_INET4_BIND, SEC_ATTACHABLE),
9323 SEC_DEF("cgroup/bind6", CGROUP_SOCK_ADDR, BPF_CGROUP_INET6_BIND, SEC_ATTACHABLE),
9324 SEC_DEF("cgroup/connect4", CGROUP_SOCK_ADDR, BPF_CGROUP_INET4_CONNECT, SEC_ATTACHABLE),
9325 SEC_DEF("cgroup/connect6", CGROUP_SOCK_ADDR, BPF_CGROUP_INET6_CONNECT, SEC_ATTACHABLE),
bf90438c 9326 SEC_DEF("cgroup/connect_unix", CGROUP_SOCK_ADDR, BPF_CGROUP_UNIX_CONNECT, SEC_ATTACHABLE),
450b167f
AN
9327 SEC_DEF("cgroup/sendmsg4", CGROUP_SOCK_ADDR, BPF_CGROUP_UDP4_SENDMSG, SEC_ATTACHABLE),
9328 SEC_DEF("cgroup/sendmsg6", CGROUP_SOCK_ADDR, BPF_CGROUP_UDP6_SENDMSG, SEC_ATTACHABLE),
bf90438c 9329 SEC_DEF("cgroup/sendmsg_unix", CGROUP_SOCK_ADDR, BPF_CGROUP_UNIX_SENDMSG, SEC_ATTACHABLE),
450b167f
AN
9330 SEC_DEF("cgroup/recvmsg4", CGROUP_SOCK_ADDR, BPF_CGROUP_UDP4_RECVMSG, SEC_ATTACHABLE),
9331 SEC_DEF("cgroup/recvmsg6", CGROUP_SOCK_ADDR, BPF_CGROUP_UDP6_RECVMSG, SEC_ATTACHABLE),
bf90438c 9332 SEC_DEF("cgroup/recvmsg_unix", CGROUP_SOCK_ADDR, BPF_CGROUP_UNIX_RECVMSG, SEC_ATTACHABLE),
450b167f
AN
9333 SEC_DEF("cgroup/getpeername4", CGROUP_SOCK_ADDR, BPF_CGROUP_INET4_GETPEERNAME, SEC_ATTACHABLE),
9334 SEC_DEF("cgroup/getpeername6", CGROUP_SOCK_ADDR, BPF_CGROUP_INET6_GETPEERNAME, SEC_ATTACHABLE),
bf90438c 9335 SEC_DEF("cgroup/getpeername_unix", CGROUP_SOCK_ADDR, BPF_CGROUP_UNIX_GETPEERNAME, SEC_ATTACHABLE),
450b167f
AN
9336 SEC_DEF("cgroup/getsockname4", CGROUP_SOCK_ADDR, BPF_CGROUP_INET4_GETSOCKNAME, SEC_ATTACHABLE),
9337 SEC_DEF("cgroup/getsockname6", CGROUP_SOCK_ADDR, BPF_CGROUP_INET6_GETSOCKNAME, SEC_ATTACHABLE),
bf90438c 9338 SEC_DEF("cgroup/getsockname_unix", CGROUP_SOCK_ADDR, BPF_CGROUP_UNIX_GETSOCKNAME, SEC_ATTACHABLE),
450b167f
AN
9339 SEC_DEF("cgroup/sysctl", CGROUP_SYSCTL, BPF_CGROUP_SYSCTL, SEC_ATTACHABLE),
9340 SEC_DEF("cgroup/getsockopt", CGROUP_SOCKOPT, BPF_CGROUP_GETSOCKOPT, SEC_ATTACHABLE),
9341 SEC_DEF("cgroup/setsockopt", CGROUP_SOCKOPT, BPF_CGROUP_SETSOCKOPT, SEC_ATTACHABLE),
9342 SEC_DEF("cgroup/dev", CGROUP_DEVICE, BPF_CGROUP_DEVICE, SEC_ATTACHABLE_OPT),
dd94d45c 9343 SEC_DEF("struct_ops+", STRUCT_OPS, 0, SEC_NONE),
913b2255 9344 SEC_DEF("struct_ops.s+", STRUCT_OPS, 0, SEC_SLEEPABLE),
450b167f 9345 SEC_DEF("sk_lookup", SK_LOOKUP, BPF_SK_LOOKUP, SEC_ATTACHABLE),
132328e8 9346 SEC_DEF("netfilter", NETFILTER, BPF_NETFILTER, SEC_NONE),
583c9009 9347};
d7be143b 9348
697f104d
AN
9349int libbpf_register_prog_handler(const char *sec,
9350 enum bpf_prog_type prog_type,
9351 enum bpf_attach_type exp_attach_type,
9352 const struct libbpf_prog_handler_opts *opts)
d7a18ea7 9353{
697f104d 9354 struct bpf_sec_def *sec_def;
d7a18ea7 9355
697f104d
AN
9356 if (!OPTS_VALID(opts, libbpf_prog_handler_opts))
9357 return libbpf_err(-EINVAL);
dd94d45c 9358
697f104d
AN
9359 if (last_custom_sec_def_handler_id == INT_MAX) /* prevent overflow */
9360 return libbpf_err(-E2BIG);
dd94d45c 9361
697f104d
AN
9362 if (sec) {
9363 sec_def = libbpf_reallocarray(custom_sec_defs, custom_sec_def_cnt + 1,
9364 sizeof(*sec_def));
9365 if (!sec_def)
9366 return libbpf_err(-ENOMEM);
dd94d45c 9367
697f104d
AN
9368 custom_sec_defs = sec_def;
9369 sec_def = &custom_sec_defs[custom_sec_def_cnt];
9370 } else {
9371 if (has_custom_fallback_def)
9372 return libbpf_err(-EBUSY);
dd94d45c 9373
697f104d
AN
9374 sec_def = &custom_fallback_def;
9375 }
9376
9377 sec_def->sec = sec ? strdup(sec) : NULL;
9378 if (sec && !sec_def->sec)
9379 return libbpf_err(-ENOMEM);
9380
9381 sec_def->prog_type = prog_type;
9382 sec_def->expected_attach_type = exp_attach_type;
9383 sec_def->cookie = OPTS_GET(opts, cookie, 0);
9384
9385 sec_def->prog_setup_fn = OPTS_GET(opts, prog_setup_fn, NULL);
9386 sec_def->prog_prepare_load_fn = OPTS_GET(opts, prog_prepare_load_fn, NULL);
9387 sec_def->prog_attach_fn = OPTS_GET(opts, prog_attach_fn, NULL);
9388
9389 sec_def->handler_id = ++last_custom_sec_def_handler_id;
9390
9391 if (sec)
9392 custom_sec_def_cnt++;
9393 else
9394 has_custom_fallback_def = true;
9395
9396 return sec_def->handler_id;
9397}
9398
9399int libbpf_unregister_prog_handler(int handler_id)
9400{
9401 struct bpf_sec_def *sec_defs;
9402 int i;
9403
9404 if (handler_id <= 0)
9405 return libbpf_err(-EINVAL);
9406
9407 if (has_custom_fallback_def && custom_fallback_def.handler_id == handler_id) {
9408 memset(&custom_fallback_def, 0, sizeof(custom_fallback_def));
9409 has_custom_fallback_def = false;
9410 return 0;
9411 }
9412
9413 for (i = 0; i < custom_sec_def_cnt; i++) {
9414 if (custom_sec_defs[i].handler_id == handler_id)
9415 break;
9416 }
9417
9418 if (i == custom_sec_def_cnt)
9419 return libbpf_err(-ENOENT);
9420
9421 free(custom_sec_defs[i].sec);
9422 for (i = i + 1; i < custom_sec_def_cnt; i++)
9423 custom_sec_defs[i - 1] = custom_sec_defs[i];
9424 custom_sec_def_cnt--;
9425
9426 /* try to shrink the array, but it's ok if we couldn't */
9427 sec_defs = libbpf_reallocarray(custom_sec_defs, custom_sec_def_cnt, sizeof(*sec_defs));
8a0260db
AN
9428 /* if new count is zero, reallocarray can return a valid NULL result;
9429 * in this case the previous pointer will be freed, so we *have to*
9430 * reassign old pointer to the new value (even if it's NULL)
9431 */
9432 if (sec_defs || custom_sec_def_cnt == 0)
697f104d
AN
9433 custom_sec_defs = sec_defs;
9434
9435 return 0;
9436}
9437
450b167f 9438static bool sec_def_matches(const struct bpf_sec_def *sec_def, const char *sec_name)
697f104d
AN
9439{
9440 size_t len = strlen(sec_def->sec);
9441
9442 /* "type/" always has to have proper SEC("type/extras") form */
9443 if (sec_def->sec[len - 1] == '/') {
9444 if (str_has_pfx(sec_name, sec_def->sec))
9445 return true;
9446 return false;
9447 }
9448
9449 /* "type+" means it can be either exact SEC("type") or
9450 * well-formed SEC("type/extras") with proper '/' separator
9451 */
9452 if (sec_def->sec[len - 1] == '+') {
9453 len--;
9454 /* not even a prefix */
9455 if (strncmp(sec_name, sec_def->sec, len) != 0)
9456 return false;
9457 /* exact match or has '/' separator */
9458 if (sec_name[len] == '\0' || sec_name[len] == '/')
9459 return true;
9460 return false;
9461 }
9462
697f104d
AN
9463 return strcmp(sec_name, sec_def->sec) == 0;
9464}
9465
9466static const struct bpf_sec_def *find_sec_def(const char *sec_name)
9467{
9468 const struct bpf_sec_def *sec_def;
9469 int i, n;
697f104d
AN
9470
9471 n = custom_sec_def_cnt;
9472 for (i = 0; i < n; i++) {
9473 sec_def = &custom_sec_defs[i];
450b167f 9474 if (sec_def_matches(sec_def, sec_name))
697f104d
AN
9475 return sec_def;
9476 }
9477
9478 n = ARRAY_SIZE(section_defs);
9479 for (i = 0; i < n; i++) {
9480 sec_def = &section_defs[i];
450b167f 9481 if (sec_def_matches(sec_def, sec_name))
dd94d45c 9482 return sec_def;
d7a18ea7 9483 }
697f104d
AN
9484
9485 if (has_custom_fallback_def)
9486 return &custom_fallback_def;
9487
d7a18ea7
AN
9488 return NULL;
9489}
9490
697f104d
AN
9491#define MAX_TYPE_NAME_SIZE 32
9492
c76e4c22
TS
9493static char *libbpf_get_type_names(bool attach_type)
9494{
d7a18ea7 9495 int i, len = ARRAY_SIZE(section_defs) * MAX_TYPE_NAME_SIZE;
c76e4c22
TS
9496 char *buf;
9497
9498 buf = malloc(len);
9499 if (!buf)
9500 return NULL;
9501
9502 buf[0] = '\0';
9503 /* Forge string buf with all available names */
d7a18ea7 9504 for (i = 0; i < ARRAY_SIZE(section_defs); i++) {
15ea31fa
AN
9505 const struct bpf_sec_def *sec_def = &section_defs[i];
9506
9507 if (attach_type) {
4fa5bcfe 9508 if (sec_def->prog_prepare_load_fn != libbpf_prepare_prog_load)
15ea31fa
AN
9509 continue;
9510
9511 if (!(sec_def->cookie & SEC_ATTACHABLE))
9512 continue;
9513 }
c76e4c22 9514
d7a18ea7 9515 if (strlen(buf) + strlen(section_defs[i].sec) + 2 > len) {
c76e4c22
TS
9516 free(buf);
9517 return NULL;
9518 }
9519 strcat(buf, " ");
d7a18ea7 9520 strcat(buf, section_defs[i].sec);
c76e4c22
TS
9521 }
9522
9523 return buf;
9524}
9525
b60df2a0
JK
9526int libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,
9527 enum bpf_attach_type *expected_attach_type)
583c9009 9528{
d7a18ea7 9529 const struct bpf_sec_def *sec_def;
c76e4c22 9530 char *type_names;
583c9009 9531
b60df2a0 9532 if (!name)
e9fc3ce9 9533 return libbpf_err(-EINVAL);
583c9009 9534
d7a18ea7
AN
9535 sec_def = find_sec_def(name);
9536 if (sec_def) {
9537 *prog_type = sec_def->prog_type;
9538 *expected_attach_type = sec_def->expected_attach_type;
b60df2a0
JK
9539 return 0;
9540 }
d7a18ea7 9541
4a3d6c6a 9542 pr_debug("failed to guess program type from ELF section '%s'\n", name);
c76e4c22
TS
9543 type_names = libbpf_get_type_names(false);
9544 if (type_names != NULL) {
3f519353 9545 pr_debug("supported section(type) names are:%s\n", type_names);
c76e4c22
TS
9546 free(type_names);
9547 }
9548
e9fc3ce9 9549 return libbpf_err(-ESRCH);
b60df2a0 9550}
583c9009 9551
ccde5760
DM
9552const char *libbpf_bpf_attach_type_str(enum bpf_attach_type t)
9553{
9554 if (t < 0 || t >= ARRAY_SIZE(attach_type_name))
9555 return NULL;
9556
9557 return attach_type_name[t];
9558}
9559
ba5d1b58
DM
9560const char *libbpf_bpf_link_type_str(enum bpf_link_type t)
9561{
9562 if (t < 0 || t >= ARRAY_SIZE(link_type_name))
9563 return NULL;
9564
9565 return link_type_name[t];
9566}
9567
3e6dc020
DM
9568const char *libbpf_bpf_map_type_str(enum bpf_map_type t)
9569{
9570 if (t < 0 || t >= ARRAY_SIZE(map_type_name))
9571 return NULL;
9572
9573 return map_type_name[t];
9574}
9575
d18616e7
DM
9576const char *libbpf_bpf_prog_type_str(enum bpf_prog_type t)
9577{
9578 if (t < 0 || t >= ARRAY_SIZE(prog_type_name))
9579 return NULL;
9580
9581 return prog_type_name[t];
9582}
9583
590a0088 9584static struct bpf_map *find_struct_ops_map_by_offset(struct bpf_object *obj,
809a69d6 9585 int sec_idx,
590a0088
MKL
9586 size_t offset)
9587{
9588 struct bpf_map *map;
9589 size_t i;
9590
9591 for (i = 0; i < obj->nr_maps; i++) {
9592 map = &obj->maps[i];
9593 if (!bpf_map__is_struct_ops(map))
9594 continue;
809a69d6
KFL
9595 if (map->sec_idx == sec_idx &&
9596 map->sec_offset <= offset &&
590a0088
MKL
9597 offset - map->sec_offset < map->def.value_size)
9598 return map;
9599 }
9600
9601 return NULL;
9602}
9603
69e4a9d2
KFL
9604/* Collect the reloc from ELF, populate the st_ops->progs[], and update
9605 * st_ops->data for shadow type.
9606 */
646f02ff 9607static int bpf_object__collect_st_ops_relos(struct bpf_object *obj,
ad23b723 9608 Elf64_Shdr *shdr, Elf_Data *data)
590a0088
MKL
9609{
9610 const struct btf_member *member;
9611 struct bpf_struct_ops *st_ops;
9612 struct bpf_program *prog;
9613 unsigned int shdr_idx;
9614 const struct btf *btf;
9615 struct bpf_map *map;
7e06aad5 9616 unsigned int moff, insn_idx;
590a0088 9617 const char *name;
1d1a3bcf 9618 __u32 member_idx;
ad23b723
AN
9619 Elf64_Sym *sym;
9620 Elf64_Rel *rel;
590a0088
MKL
9621 int i, nrels;
9622
590a0088
MKL
9623 btf = obj->btf;
9624 nrels = shdr->sh_size / shdr->sh_entsize;
9625 for (i = 0; i < nrels; i++) {
ad23b723
AN
9626 rel = elf_rel_by_idx(data, i);
9627 if (!rel) {
590a0088
MKL
9628 pr_warn("struct_ops reloc: failed to get %d reloc\n", i);
9629 return -LIBBPF_ERRNO__FORMAT;
9630 }
9631
ad23b723
AN
9632 sym = elf_sym_by_idx(obj, ELF64_R_SYM(rel->r_info));
9633 if (!sym) {
590a0088 9634 pr_warn("struct_ops reloc: symbol %zx not found\n",
ad23b723 9635 (size_t)ELF64_R_SYM(rel->r_info));
590a0088
MKL
9636 return -LIBBPF_ERRNO__FORMAT;
9637 }
9638
ad23b723 9639 name = elf_sym_str(obj, sym->st_name) ?: "<?>";
809a69d6 9640 map = find_struct_ops_map_by_offset(obj, shdr->sh_info, rel->r_offset);
590a0088 9641 if (!map) {
ad23b723
AN
9642 pr_warn("struct_ops reloc: cannot find map at rel->r_offset %zu\n",
9643 (size_t)rel->r_offset);
590a0088
MKL
9644 return -EINVAL;
9645 }
9646
ad23b723
AN
9647 moff = rel->r_offset - map->sec_offset;
9648 shdr_idx = sym->st_shndx;
590a0088 9649 st_ops = map->st_ops;
ad23b723 9650 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",
590a0088 9651 map->name,
ad23b723
AN
9652 (long long)(rel->r_info >> 32),
9653 (long long)sym->st_value,
9654 shdr_idx, (size_t)rel->r_offset,
9655 map->sec_offset, sym->st_name, name);
590a0088
MKL
9656
9657 if (shdr_idx >= SHN_LORESERVE) {
ad23b723
AN
9658 pr_warn("struct_ops reloc %s: rel->r_offset %zu shdr_idx %u unsupported non-static function\n",
9659 map->name, (size_t)rel->r_offset, shdr_idx);
590a0088
MKL
9660 return -LIBBPF_ERRNO__RELOC;
9661 }
ad23b723 9662 if (sym->st_value % BPF_INSN_SZ) {
7e06aad5 9663 pr_warn("struct_ops reloc %s: invalid target program offset %llu\n",
ad23b723 9664 map->name, (unsigned long long)sym->st_value);
7e06aad5
AN
9665 return -LIBBPF_ERRNO__FORMAT;
9666 }
ad23b723 9667 insn_idx = sym->st_value / BPF_INSN_SZ;
590a0088
MKL
9668
9669 member = find_member_by_offset(st_ops->type, moff * 8);
9670 if (!member) {
9671 pr_warn("struct_ops reloc %s: cannot find member at moff %u\n",
9672 map->name, moff);
9673 return -EINVAL;
9674 }
9675 member_idx = member - btf_members(st_ops->type);
9676 name = btf__name_by_offset(btf, member->name_off);
9677
9678 if (!resolve_func_ptr(btf, member->type, NULL)) {
9679 pr_warn("struct_ops reloc %s: cannot relocate non func ptr %s\n",
9680 map->name, name);
9681 return -EINVAL;
9682 }
9683
7e06aad5 9684 prog = find_prog_by_sec_insn(obj, shdr_idx, insn_idx);
590a0088
MKL
9685 if (!prog) {
9686 pr_warn("struct_ops reloc %s: cannot find prog at shdr_idx %u to relocate func ptr %s\n",
9687 map->name, shdr_idx, name);
9688 return -EINVAL;
9689 }
9690
91b4d1d1
AN
9691 /* prevent the use of BPF prog with invalid type */
9692 if (prog->type != BPF_PROG_TYPE_STRUCT_OPS) {
9693 pr_warn("struct_ops reloc %s: prog %s is not struct_ops BPF program\n",
9694 map->name, prog->name);
9695 return -EINVAL;
9696 }
590a0088 9697
590a0088 9698 st_ops->progs[member_idx] = prog;
69e4a9d2
KFL
9699
9700 /* st_ops->data will be exposed to users, being returned by
9701 * bpf_map__initial_value() as a pointer to the shadow
9702 * type. All function pointers in the original struct type
9703 * should be converted to a pointer to struct bpf_program
9704 * in the shadow type.
9705 */
9706 *((struct bpf_program **)(st_ops->data + moff)) = prog;
590a0088
MKL
9707 }
9708
9709 return 0;
590a0088
MKL
9710}
9711
a6ed02ca 9712#define BTF_TRACE_PREFIX "btf_trace_"
1e092a03 9713#define BTF_LSM_PREFIX "bpf_lsm_"
21aef70e 9714#define BTF_ITER_PREFIX "bpf_iter_"
a6ed02ca
KS
9715#define BTF_MAX_NAME_SIZE 128
9716
67234743
AS
9717void btf_get_kernel_prefix_kind(enum bpf_attach_type attach_type,
9718 const char **prefix, int *kind)
9719{
9720 switch (attach_type) {
9721 case BPF_TRACE_RAW_TP:
9722 *prefix = BTF_TRACE_PREFIX;
9723 *kind = BTF_KIND_TYPEDEF;
9724 break;
9725 case BPF_LSM_MAC:
bffcf348 9726 case BPF_LSM_CGROUP:
67234743
AS
9727 *prefix = BTF_LSM_PREFIX;
9728 *kind = BTF_KIND_FUNC;
9729 break;
9730 case BPF_TRACE_ITER:
9731 *prefix = BTF_ITER_PREFIX;
9732 *kind = BTF_KIND_FUNC;
9733 break;
9734 default:
9735 *prefix = "";
9736 *kind = BTF_KIND_FUNC;
9737 }
9738}
9739
a6ed02ca
KS
9740static int find_btf_by_prefix_kind(const struct btf *btf, const char *prefix,
9741 const char *name, __u32 kind)
9742{
9743 char btf_type_name[BTF_MAX_NAME_SIZE];
9744 int ret;
9745
9746 ret = snprintf(btf_type_name, sizeof(btf_type_name),
9747 "%s%s", prefix, name);
9748 /* snprintf returns the number of characters written excluding the
c139e40a 9749 * terminating null. So, if >= BTF_MAX_NAME_SIZE are written, it
a6ed02ca
KS
9750 * indicates truncation.
9751 */
9752 if (ret < 0 || ret >= sizeof(btf_type_name))
9753 return -ENAMETOOLONG;
9754 return btf__find_by_name_kind(btf, btf_type_name, kind);
9755}
9756
91abb4a6
AN
9757static inline int find_attach_btf_id(struct btf *btf, const char *name,
9758 enum bpf_attach_type attach_type)
a6ed02ca 9759{
67234743
AS
9760 const char *prefix;
9761 int kind;
a6ed02ca 9762
67234743
AS
9763 btf_get_kernel_prefix_kind(attach_type, &prefix, &kind);
9764 return find_btf_by_prefix_kind(btf, prefix, name, kind);
a6ed02ca
KS
9765}
9766
b8c54ea4
AS
9767int libbpf_find_vmlinux_btf_id(const char *name,
9768 enum bpf_attach_type attach_type)
12a8654b 9769{
a6ed02ca 9770 struct btf *btf;
3521ffa2 9771 int err;
12a8654b 9772
a710eed3 9773 btf = btf__load_vmlinux_btf();
e9fc3ce9
AN
9774 err = libbpf_get_error(btf);
9775 if (err) {
12a8654b 9776 pr_warn("vmlinux BTF is not found\n");
e9fc3ce9 9777 return libbpf_err(err);
12a8654b
AS
9778 }
9779
91abb4a6
AN
9780 err = find_attach_btf_id(btf, name, attach_type);
9781 if (err <= 0)
9782 pr_warn("%s is not found in vmlinux BTF\n", name);
9783
3521ffa2 9784 btf__free(btf);
e9fc3ce9 9785 return libbpf_err(err);
b8c54ea4
AS
9786}
9787
e7bf94db
AS
9788static int libbpf_find_prog_btf_id(const char *name, __u32 attach_prog_fd)
9789{
813847a3 9790 struct bpf_prog_info info;
ebc7b50a 9791 __u32 info_len = sizeof(info);
6cc93e2f 9792 struct btf *btf;
6d2d73cd 9793 int err;
e7bf94db 9794
813847a3 9795 memset(&info, 0, info_len);
629dfc66 9796 err = bpf_prog_get_info_by_fd(attach_prog_fd, &info, &info_len);
e9fc3ce9 9797 if (err) {
629dfc66 9798 pr_warn("failed bpf_prog_get_info_by_fd for FD %d: %d\n",
ebc7b50a 9799 attach_prog_fd, err);
e9fc3ce9 9800 return err;
e7bf94db 9801 }
6d2d73cd
QM
9802
9803 err = -EINVAL;
ebc7b50a 9804 if (!info.btf_id) {
e7bf94db
AS
9805 pr_warn("The target program doesn't have BTF\n");
9806 goto out;
9807 }
ebc7b50a
DM
9808 btf = btf__load_from_kernel_by_id(info.btf_id);
9809 err = libbpf_get_error(btf);
9810 if (err) {
9811 pr_warn("Failed to get BTF %d of the program: %d\n", info.btf_id, err);
e7bf94db
AS
9812 goto out;
9813 }
9814 err = btf__find_by_name_kind(btf, name, BTF_KIND_FUNC);
9815 btf__free(btf);
9816 if (err <= 0) {
9817 pr_warn("%s is not found in prog's BTF\n", name);
9818 goto out;
9819 }
9820out:
e7bf94db
AS
9821 return err;
9822}
9823
91abb4a6
AN
9824static int find_kernel_btf_id(struct bpf_object *obj, const char *attach_name,
9825 enum bpf_attach_type attach_type,
9826 int *btf_obj_fd, int *btf_type_id)
9827{
9828 int ret, i;
9829
9830 ret = find_attach_btf_id(obj->btf_vmlinux, attach_name, attach_type);
9831 if (ret > 0) {
9832 *btf_obj_fd = 0; /* vmlinux BTF */
9833 *btf_type_id = ret;
9834 return 0;
9835 }
9836 if (ret != -ENOENT)
9837 return ret;
9838
9839 ret = load_module_btfs(obj);
9840 if (ret)
9841 return ret;
9842
9843 for (i = 0; i < obj->btf_module_cnt; i++) {
9844 const struct module_btf *mod = &obj->btf_modules[i];
9845
9846 ret = find_attach_btf_id(mod->btf, attach_name, attach_type);
9847 if (ret > 0) {
9848 *btf_obj_fd = mod->fd;
9849 *btf_type_id = ret;
9850 return 0;
9851 }
9852 if (ret == -ENOENT)
9853 continue;
9854
9855 return ret;
9856 }
9857
9858 return -ESRCH;
9859}
9860
15ea31fa
AN
9861static int libbpf_find_attach_btf_id(struct bpf_program *prog, const char *attach_name,
9862 int *btf_obj_fd, int *btf_type_id)
b8c54ea4 9863{
a6ed02ca
KS
9864 enum bpf_attach_type attach_type = prog->expected_attach_type;
9865 __u32 attach_prog_fd = prog->attach_prog_fd;
b6291a6f 9866 int err = 0;
b8c54ea4 9867
91abb4a6 9868 /* BPF program's BTF ID */
749c202c
AN
9869 if (prog->type == BPF_PROG_TYPE_EXT || attach_prog_fd) {
9870 if (!attach_prog_fd) {
9871 pr_warn("prog '%s': attach program FD is not set\n", prog->name);
9872 return -EINVAL;
9873 }
91abb4a6
AN
9874 err = libbpf_find_prog_btf_id(attach_name, attach_prog_fd);
9875 if (err < 0) {
749c202c
AN
9876 pr_warn("prog '%s': failed to find BPF program (FD %d) BTF ID for '%s': %d\n",
9877 prog->name, attach_prog_fd, attach_name, err);
91abb4a6
AN
9878 return err;
9879 }
9880 *btf_obj_fd = 0;
9881 *btf_type_id = err;
9882 return 0;
9883 }
9884
9885 /* kernel/module BTF ID */
67234743
AS
9886 if (prog->obj->gen_loader) {
9887 bpf_gen__record_attach_target(prog->obj->gen_loader, attach_name, attach_type);
9888 *btf_obj_fd = 0;
9889 *btf_type_id = 1;
9890 } else {
9e926acd
KFL
9891 err = find_kernel_btf_id(prog->obj, attach_name,
9892 attach_type, btf_obj_fd,
9893 btf_type_id);
67234743 9894 }
91abb4a6 9895 if (err) {
749c202c
AN
9896 pr_warn("prog '%s': failed to find kernel BTF type ID of '%s': %d\n",
9897 prog->name, attach_name, err);
b8c54ea4 9898 return err;
12a8654b 9899 }
91abb4a6 9900 return 0;
12a8654b
AS
9901}
9902
956b620f
AI
9903int libbpf_attach_type_by_name(const char *name,
9904 enum bpf_attach_type *attach_type)
9905{
c76e4c22 9906 char *type_names;
b6291a6f 9907 const struct bpf_sec_def *sec_def;
956b620f
AI
9908
9909 if (!name)
e9fc3ce9 9910 return libbpf_err(-EINVAL);
956b620f 9911
b6291a6f
AN
9912 sec_def = find_sec_def(name);
9913 if (!sec_def) {
9914 pr_debug("failed to guess attach type based on ELF section name '%s'\n", name);
9915 type_names = libbpf_get_type_names(true);
9916 if (type_names != NULL) {
9917 pr_debug("attachable section(type) names are:%s\n", type_names);
9918 free(type_names);
9919 }
9920
9921 return libbpf_err(-EINVAL);
c76e4c22
TS
9922 }
9923
4fa5bcfe 9924 if (sec_def->prog_prepare_load_fn != libbpf_prepare_prog_load)
15ea31fa
AN
9925 return libbpf_err(-EINVAL);
9926 if (!(sec_def->cookie & SEC_ATTACHABLE))
b6291a6f
AN
9927 return libbpf_err(-EINVAL);
9928
9929 *attach_type = sec_def->expected_attach_type;
9930 return 0;
956b620f
AI
9931}
9932
a324aae3 9933int bpf_map__fd(const struct bpf_map *map)
9d759a9b 9934{
f08c18e0
AN
9935 if (!map)
9936 return libbpf_err(-EINVAL);
9937 if (!map_is_created(map))
9938 return -1;
9939 return map->fd;
9d759a9b
WN
9940}
9941
aed65917
AN
9942static bool map_uses_real_name(const struct bpf_map *map)
9943{
9944 /* Since libbpf started to support custom .data.* and .rodata.* maps,
9945 * their user-visible name differs from kernel-visible name. Users see
9946 * such map's corresponding ELF section name as a map name.
9947 * This check distinguishes .data/.rodata from .data.* and .rodata.*
9948 * maps to know which name has to be returned to the user.
9949 */
9950 if (map->libbpf_type == LIBBPF_MAP_DATA && strcmp(map->real_name, DATA_SEC) != 0)
9951 return true;
9952 if (map->libbpf_type == LIBBPF_MAP_RODATA && strcmp(map->real_name, RODATA_SEC) != 0)
9953 return true;
9954 return false;
9955}
9956
a324aae3 9957const char *bpf_map__name(const struct bpf_map *map)
561bbcca 9958{
aed65917
AN
9959 if (!map)
9960 return NULL;
9961
9962 if (map_uses_real_name(map))
9963 return map->real_name;
9964
9965 return map->name;
561bbcca
WN
9966}
9967
1bdb6c9a
AN
9968enum bpf_map_type bpf_map__type(const struct bpf_map *map)
9969{
9970 return map->def.type;
9971}
9972
9973int bpf_map__set_type(struct bpf_map *map, enum bpf_map_type type)
9974{
f08c18e0 9975 if (map_is_created(map))
e9fc3ce9 9976 return libbpf_err(-EBUSY);
1bdb6c9a
AN
9977 map->def.type = type;
9978 return 0;
9979}
9980
9981__u32 bpf_map__map_flags(const struct bpf_map *map)
9982{
9983 return map->def.map_flags;
9984}
9985
9986int bpf_map__set_map_flags(struct bpf_map *map, __u32 flags)
9987{
f08c18e0 9988 if (map_is_created(map))
e9fc3ce9 9989 return libbpf_err(-EBUSY);
1bdb6c9a
AN
9990 map->def.map_flags = flags;
9991 return 0;
9992}
9993
47512102
JK
9994__u64 bpf_map__map_extra(const struct bpf_map *map)
9995{
9996 return map->map_extra;
9997}
9998
9999int bpf_map__set_map_extra(struct bpf_map *map, __u64 map_extra)
10000{
f08c18e0 10001 if (map_is_created(map))
47512102
JK
10002 return libbpf_err(-EBUSY);
10003 map->map_extra = map_extra;
10004 return 0;
10005}
10006
1bdb6c9a
AN
10007__u32 bpf_map__numa_node(const struct bpf_map *map)
10008{
10009 return map->numa_node;
10010}
10011
10012int bpf_map__set_numa_node(struct bpf_map *map, __u32 numa_node)
10013{
f08c18e0 10014 if (map_is_created(map))
e9fc3ce9 10015 return libbpf_err(-EBUSY);
1bdb6c9a
AN
10016 map->numa_node = numa_node;
10017 return 0;
10018}
10019
10020__u32 bpf_map__key_size(const struct bpf_map *map)
10021{
10022 return map->def.key_size;
10023}
10024
10025int bpf_map__set_key_size(struct bpf_map *map, __u32 size)
10026{
f08c18e0 10027 if (map_is_created(map))
e9fc3ce9 10028 return libbpf_err(-EBUSY);
1bdb6c9a
AN
10029 map->def.key_size = size;
10030 return 0;
10031}
10032
10033__u32 bpf_map__value_size(const struct bpf_map *map)
10034{
10035 return map->def.value_size;
10036}
10037
9d0a2331
JK
10038static int map_btf_datasec_resize(struct bpf_map *map, __u32 size)
10039{
10040 struct btf *btf;
10041 struct btf_type *datasec_type, *var_type;
10042 struct btf_var_secinfo *var;
10043 const struct btf_type *array_type;
10044 const struct btf_array *array;
4c857a71
JK
10045 int vlen, element_sz, new_array_id;
10046 __u32 nr_elements;
9d0a2331
JK
10047
10048 /* check btf existence */
10049 btf = bpf_object__btf(map->obj);
10050 if (!btf)
10051 return -ENOENT;
10052
10053 /* verify map is datasec */
10054 datasec_type = btf_type_by_id(btf, bpf_map__btf_value_type_id(map));
10055 if (!btf_is_datasec(datasec_type)) {
10056 pr_warn("map '%s': cannot be resized, map value type is not a datasec\n",
10057 bpf_map__name(map));
10058 return -EINVAL;
10059 }
10060
10061 /* verify datasec has at least one var */
10062 vlen = btf_vlen(datasec_type);
10063 if (vlen == 0) {
10064 pr_warn("map '%s': cannot be resized, map value datasec is empty\n",
10065 bpf_map__name(map));
10066 return -EINVAL;
10067 }
10068
10069 /* verify last var in the datasec is an array */
10070 var = &btf_var_secinfos(datasec_type)[vlen - 1];
10071 var_type = btf_type_by_id(btf, var->type);
10072 array_type = skip_mods_and_typedefs(btf, var_type->type, NULL);
10073 if (!btf_is_array(array_type)) {
10074 pr_warn("map '%s': cannot be resized, last var must be an array\n",
10075 bpf_map__name(map));
10076 return -EINVAL;
10077 }
10078
10079 /* verify request size aligns with array */
10080 array = btf_array(array_type);
10081 element_sz = btf__resolve_size(btf, array->type);
10082 if (element_sz <= 0 || (size - var->offset) % element_sz != 0) {
10083 pr_warn("map '%s': cannot be resized, element size (%d) doesn't align with new total size (%u)\n",
10084 bpf_map__name(map), element_sz, size);
10085 return -EINVAL;
10086 }
10087
10088 /* create a new array based on the existing array, but with new length */
10089 nr_elements = (size - var->offset) / element_sz;
10090 new_array_id = btf__add_array(btf, array->index_type, array->type, nr_elements);
10091 if (new_array_id < 0)
10092 return new_array_id;
10093
10094 /* adding a new btf type invalidates existing pointers to btf objects,
10095 * so refresh pointers before proceeding
10096 */
10097 datasec_type = btf_type_by_id(btf, map->btf_value_type_id);
10098 var = &btf_var_secinfos(datasec_type)[vlen - 1];
10099 var_type = btf_type_by_id(btf, var->type);
10100
10101 /* finally update btf info */
10102 datasec_type->size = size;
10103 var->size = size - var->offset;
10104 var_type->type = new_array_id;
10105
10106 return 0;
10107}
10108
1bdb6c9a
AN
10109int bpf_map__set_value_size(struct bpf_map *map, __u32 size)
10110{
f08c18e0 10111 if (map->obj->loaded || map->reused)
e9fc3ce9 10112 return libbpf_err(-EBUSY);
9d0a2331
JK
10113
10114 if (map->mmaped) {
9d0a2331 10115 size_t mmap_old_sz, mmap_new_sz;
79ff13e9
AS
10116 int err;
10117
10118 if (map->def.type != BPF_MAP_TYPE_ARRAY)
10119 return -EOPNOTSUPP;
9d0a2331 10120
79ff13e9
AS
10121 mmap_old_sz = bpf_map_mmap_sz(map);
10122 mmap_new_sz = array_map_mmap_sz(size, map->def.max_entries);
9d0a2331
JK
10123 err = bpf_map_mmap_resize(map, mmap_old_sz, mmap_new_sz);
10124 if (err) {
10125 pr_warn("map '%s': failed to resize memory-mapped region: %d\n",
10126 bpf_map__name(map), err);
10127 return err;
10128 }
10129 err = map_btf_datasec_resize(map, size);
10130 if (err && err != -ENOENT) {
10131 pr_warn("map '%s': failed to adjust resized BTF, clearing BTF key/value info: %d\n",
10132 bpf_map__name(map), err);
10133 map->btf_value_type_id = 0;
10134 map->btf_key_type_id = 0;
10135 }
10136 }
10137
1bdb6c9a
AN
10138 map->def.value_size = size;
10139 return 0;
10140}
10141
5b891af7 10142__u32 bpf_map__btf_key_type_id(const struct bpf_map *map)
8a138aed 10143{
61746dbe 10144 return map ? map->btf_key_type_id : 0;
8a138aed
MKL
10145}
10146
5b891af7 10147__u32 bpf_map__btf_value_type_id(const struct bpf_map *map)
8a138aed 10148{
61746dbe 10149 return map ? map->btf_value_type_id : 0;
8a138aed
MKL
10150}
10151
e2842be5
THJ
10152int bpf_map__set_initial_value(struct bpf_map *map,
10153 const void *data, size_t size)
10154{
2e7ba4f8
AN
10155 size_t actual_sz;
10156
f08c18e0
AN
10157 if (map->obj->loaded || map->reused)
10158 return libbpf_err(-EBUSY);
10159
2e7ba4f8
AN
10160 if (!map->mmaped || map->libbpf_type == LIBBPF_MAP_KCONFIG)
10161 return libbpf_err(-EINVAL);
10162
10163 if (map->def.type == BPF_MAP_TYPE_ARENA)
10164 actual_sz = map->obj->arena_data_sz;
10165 else
10166 actual_sz = map->def.value_size;
10167 if (size != actual_sz)
e9fc3ce9 10168 return libbpf_err(-EINVAL);
e2842be5
THJ
10169
10170 memcpy(map->mmaped, data, size);
10171 return 0;
10172}
10173
2e7ba4f8 10174void *bpf_map__initial_value(const struct bpf_map *map, size_t *psize)
7723256b 10175{
69e4a9d2
KFL
10176 if (bpf_map__is_struct_ops(map)) {
10177 if (psize)
10178 *psize = map->def.value_size;
10179 return map->st_ops->data;
10180 }
10181
7723256b
AS
10182 if (!map->mmaped)
10183 return NULL;
2e7ba4f8
AN
10184
10185 if (map->def.type == BPF_MAP_TYPE_ARENA)
10186 *psize = map->obj->arena_data_sz;
10187 else
10188 *psize = map->def.value_size;
10189
7723256b
AS
10190 return map->mmaped;
10191}
10192
a324aae3 10193bool bpf_map__is_internal(const struct bpf_map *map)
d859900c
DB
10194{
10195 return map->libbpf_type != LIBBPF_MAP_UNSPEC;
10196}
10197
1bdb6c9a
AN
10198__u32 bpf_map__ifindex(const struct bpf_map *map)
10199{
10200 return map->map_ifindex;
10201}
10202
10203int bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex)
9aba3613 10204{
f08c18e0 10205 if (map_is_created(map))
e9fc3ce9 10206 return libbpf_err(-EBUSY);
9aba3613 10207 map->map_ifindex = ifindex;
1bdb6c9a 10208 return 0;
9aba3613
JK
10209}
10210
addb9fc9
NS
10211int bpf_map__set_inner_map_fd(struct bpf_map *map, int fd)
10212{
10213 if (!bpf_map_type__is_map_in_map(map->def.type)) {
be18010e 10214 pr_warn("error: unsupported map type\n");
e9fc3ce9 10215 return libbpf_err(-EINVAL);
addb9fc9
NS
10216 }
10217 if (map->inner_map_fd != -1) {
be18010e 10218 pr_warn("error: inner_map_fd already specified\n");
e9fc3ce9 10219 return libbpf_err(-EINVAL);
addb9fc9 10220 }
8f7b239e
AN
10221 if (map->inner_map) {
10222 bpf_map__destroy(map->inner_map);
10223 zfree(&map->inner_map);
10224 }
addb9fc9
NS
10225 map->inner_map_fd = fd;
10226 return 0;
10227}
10228
0c19a9fb 10229static struct bpf_map *
a324aae3 10230__bpf_map__iter(const struct bpf_map *m, const struct bpf_object *obj, int i)
9d759a9b 10231{
0c19a9fb 10232 ssize_t idx;
9d759a9b
WN
10233 struct bpf_map *s, *e;
10234
10235 if (!obj || !obj->maps)
e9fc3ce9 10236 return errno = EINVAL, NULL;
9d759a9b
WN
10237
10238 s = obj->maps;
10239 e = obj->maps + obj->nr_maps;
10240
0c19a9fb 10241 if ((m < s) || (m >= e)) {
be18010e
KW
10242 pr_warn("error in %s: map handler doesn't belong to object\n",
10243 __func__);
e9fc3ce9 10244 return errno = EINVAL, NULL;
9d759a9b
WN
10245 }
10246
0c19a9fb
SF
10247 idx = (m - obj->maps) + i;
10248 if (idx >= obj->nr_maps || idx < 0)
9d759a9b
WN
10249 return NULL;
10250 return &obj->maps[idx];
10251}
561bbcca 10252
2088a3a7
HC
10253struct bpf_map *
10254bpf_object__next_map(const struct bpf_object *obj, const struct bpf_map *prev)
0c19a9fb
SF
10255{
10256 if (prev == NULL)
10257 return obj->maps;
10258
10259 return __bpf_map__iter(prev, obj, 1);
10260}
10261
2088a3a7
HC
10262struct bpf_map *
10263bpf_object__prev_map(const struct bpf_object *obj, const struct bpf_map *next)
0c19a9fb
SF
10264{
10265 if (next == NULL) {
10266 if (!obj->nr_maps)
10267 return NULL;
10268 return obj->maps + obj->nr_maps - 1;
10269 }
10270
10271 return __bpf_map__iter(next, obj, -1);
10272}
10273
561bbcca 10274struct bpf_map *
a324aae3 10275bpf_object__find_map_by_name(const struct bpf_object *obj, const char *name)
561bbcca
WN
10276{
10277 struct bpf_map *pos;
10278
f74a53d9 10279 bpf_object__for_each_map(pos, obj) {
26071635
AN
10280 /* if it's a special internal map name (which always starts
10281 * with dot) then check if that special name matches the
10282 * real map name (ELF section name)
10283 */
10284 if (name[0] == '.') {
10285 if (pos->real_name && strcmp(pos->real_name, name) == 0)
10286 return pos;
10287 continue;
10288 }
10289 /* otherwise map name has to be an exact match */
aed65917
AN
10290 if (map_uses_real_name(pos)) {
10291 if (strcmp(pos->real_name, name) == 0)
10292 return pos;
10293 continue;
10294 }
10295 if (strcmp(pos->name, name) == 0)
561bbcca
WN
10296 return pos;
10297 }
e9fc3ce9 10298 return errno = ENOENT, NULL;
561bbcca 10299}
5a6acad1 10300
f3cea32d 10301int
a324aae3 10302bpf_object__find_map_fd_by_name(const struct bpf_object *obj, const char *name)
f3cea32d
MF
10303{
10304 return bpf_map__fd(bpf_object__find_map_by_name(obj, name));
10305}
10306
737d0646
AN
10307static int validate_map_op(const struct bpf_map *map, size_t key_sz,
10308 size_t value_sz, bool check_value_sz)
10309{
f08c18e0 10310 if (!map_is_created(map)) /* map is not yet created */
737d0646
AN
10311 return -ENOENT;
10312
10313 if (map->def.key_size != key_sz) {
10314 pr_warn("map '%s': unexpected key size %zu provided, expected %u\n",
10315 map->name, key_sz, map->def.key_size);
10316 return -EINVAL;
10317 }
10318
10319 if (!check_value_sz)
10320 return 0;
10321
10322 switch (map->def.type) {
10323 case BPF_MAP_TYPE_PERCPU_ARRAY:
10324 case BPF_MAP_TYPE_PERCPU_HASH:
10325 case BPF_MAP_TYPE_LRU_PERCPU_HASH:
10326 case BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE: {
10327 int num_cpu = libbpf_num_possible_cpus();
10328 size_t elem_sz = roundup(map->def.value_size, 8);
10329
10330 if (value_sz != num_cpu * elem_sz) {
10331 pr_warn("map '%s': unexpected value size %zu provided for per-CPU map, expected %d * %zu = %zd\n",
10332 map->name, value_sz, num_cpu, elem_sz, num_cpu * elem_sz);
10333 return -EINVAL;
10334 }
10335 break;
10336 }
10337 default:
10338 if (map->def.value_size != value_sz) {
10339 pr_warn("map '%s': unexpected value size %zu provided, expected %u\n",
10340 map->name, value_sz, map->def.value_size);
10341 return -EINVAL;
10342 }
10343 break;
10344 }
10345 return 0;
10346}
10347
10348int bpf_map__lookup_elem(const struct bpf_map *map,
10349 const void *key, size_t key_sz,
10350 void *value, size_t value_sz, __u64 flags)
10351{
10352 int err;
10353
10354 err = validate_map_op(map, key_sz, value_sz, true);
10355 if (err)
10356 return libbpf_err(err);
10357
10358 return bpf_map_lookup_elem_flags(map->fd, key, value, flags);
10359}
10360
10361int bpf_map__update_elem(const struct bpf_map *map,
10362 const void *key, size_t key_sz,
10363 const void *value, size_t value_sz, __u64 flags)
10364{
10365 int err;
10366
10367 err = validate_map_op(map, key_sz, value_sz, true);
10368 if (err)
10369 return libbpf_err(err);
10370
10371 return bpf_map_update_elem(map->fd, key, value, flags);
10372}
10373
10374int bpf_map__delete_elem(const struct bpf_map *map,
10375 const void *key, size_t key_sz, __u64 flags)
10376{
10377 int err;
10378
10379 err = validate_map_op(map, key_sz, 0, false /* check_value_sz */);
10380 if (err)
10381 return libbpf_err(err);
10382
10383 return bpf_map_delete_elem_flags(map->fd, key, flags);
10384}
10385
10386int bpf_map__lookup_and_delete_elem(const struct bpf_map *map,
10387 const void *key, size_t key_sz,
10388 void *value, size_t value_sz, __u64 flags)
10389{
10390 int err;
10391
10392 err = validate_map_op(map, key_sz, value_sz, true);
10393 if (err)
10394 return libbpf_err(err);
10395
10396 return bpf_map_lookup_and_delete_elem_flags(map->fd, key, value, flags);
10397}
10398
10399int bpf_map__get_next_key(const struct bpf_map *map,
10400 const void *cur_key, void *next_key, size_t key_sz)
10401{
10402 int err;
10403
10404 err = validate_map_op(map, key_sz, 0, false /* check_value_sz */);
10405 if (err)
10406 return libbpf_err(err);
10407
10408 return bpf_map_get_next_key(map->fd, cur_key, next_key);
10409}
10410
e28ff1a8
JS
10411long libbpf_get_error(const void *ptr)
10412{
e9fc3ce9
AN
10413 if (!IS_ERR_OR_NULL(ptr))
10414 return 0;
10415
10416 if (IS_ERR(ptr))
10417 errno = -PTR_ERR(ptr);
10418
10419 /* If ptr == NULL, then errno should be already set by the failing
10420 * API, because libbpf never returns NULL on success and it now always
10421 * sets errno on error. So no extra errno handling for ptr == NULL
10422 * case.
10423 */
10424 return -errno;
e28ff1a8 10425}
6f6d33f3 10426
cc4f864b
AN
10427/* Replace link's underlying BPF program with the new one */
10428int bpf_link__update_program(struct bpf_link *link, struct bpf_program *prog)
10429{
e9fc3ce9 10430 int ret;
c139e40a 10431
e9fc3ce9
AN
10432 ret = bpf_link_update(bpf_link__fd(link), bpf_program__fd(prog), NULL);
10433 return libbpf_err_errno(ret);
cc4f864b
AN
10434}
10435
d6958706
AN
10436/* Release "ownership" of underlying BPF resource (typically, BPF program
10437 * attached to some BPF hook, e.g., tracepoint, kprobe, etc). Disconnected
10438 * link, when destructed through bpf_link__destroy() call won't attempt to
10439 * detach/unregisted that BPF resource. This is useful in situations where,
10440 * say, attached BPF program has to outlive userspace program that attached it
10441 * in the system. Depending on type of BPF program, though, there might be
10442 * additional steps (like pinning BPF program in BPF FS) necessary to ensure
10443 * exit of userspace program doesn't trigger automatic detachment and clean up
10444 * inside the kernel.
10445 */
10446void bpf_link__disconnect(struct bpf_link *link)
10447{
10448 link->disconnected = true;
10449}
10450
1c2e9efc
AN
10451int bpf_link__destroy(struct bpf_link *link)
10452{
d6958706 10453 int err = 0;
1c2e9efc 10454
50450fc7 10455 if (IS_ERR_OR_NULL(link))
1c2e9efc
AN
10456 return 0;
10457
d6958706
AN
10458 if (!link->disconnected && link->detach)
10459 err = link->detach(link);
c016b68e
AN
10460 if (link->pin_path)
10461 free(link->pin_path);
d88b71d4
AN
10462 if (link->dealloc)
10463 link->dealloc(link);
10464 else
10465 free(link);
1c2e9efc 10466
e9fc3ce9 10467 return libbpf_err(err);
1c2e9efc
AN
10468}
10469
c016b68e
AN
10470int bpf_link__fd(const struct bpf_link *link)
10471{
10472 return link->fd;
10473}
10474
10475const char *bpf_link__pin_path(const struct bpf_link *link)
10476{
10477 return link->pin_path;
10478}
10479
10480static int bpf_link__detach_fd(struct bpf_link *link)
10481{
e9fc3ce9 10482 return libbpf_err_errno(close(link->fd));
c016b68e
AN
10483}
10484
10485struct bpf_link *bpf_link__open(const char *path)
10486{
10487 struct bpf_link *link;
10488 int fd;
10489
10490 fd = bpf_obj_get(path);
10491 if (fd < 0) {
10492 fd = -errno;
10493 pr_warn("failed to open link at %s: %d\n", path, fd);
e9fc3ce9 10494 return libbpf_err_ptr(fd);
c016b68e
AN
10495 }
10496
10497 link = calloc(1, sizeof(*link));
10498 if (!link) {
10499 close(fd);
e9fc3ce9 10500 return libbpf_err_ptr(-ENOMEM);
c016b68e
AN
10501 }
10502 link->detach = &bpf_link__detach_fd;
10503 link->fd = fd;
10504
10505 link->pin_path = strdup(path);
10506 if (!link->pin_path) {
10507 bpf_link__destroy(link);
e9fc3ce9 10508 return libbpf_err_ptr(-ENOMEM);
c016b68e
AN
10509 }
10510
10511 return link;
10512}
10513
2e49527e
AN
10514int bpf_link__detach(struct bpf_link *link)
10515{
10516 return bpf_link_detach(link->fd) ? -errno : 0;
10517}
10518
c016b68e
AN
10519int bpf_link__pin(struct bpf_link *link, const char *path)
10520{
10521 int err;
10522
10523 if (link->pin_path)
e9fc3ce9 10524 return libbpf_err(-EBUSY);
c016b68e
AN
10525 err = make_parent_dir(path);
10526 if (err)
e9fc3ce9 10527 return libbpf_err(err);
c016b68e
AN
10528 err = check_path(path);
10529 if (err)
e9fc3ce9 10530 return libbpf_err(err);
c016b68e
AN
10531
10532 link->pin_path = strdup(path);
10533 if (!link->pin_path)
e9fc3ce9 10534 return libbpf_err(-ENOMEM);
c016b68e
AN
10535
10536 if (bpf_obj_pin(link->fd, link->pin_path)) {
10537 err = -errno;
10538 zfree(&link->pin_path);
e9fc3ce9 10539 return libbpf_err(err);
c016b68e
AN
10540 }
10541
10542 pr_debug("link fd=%d: pinned at %s\n", link->fd, link->pin_path);
10543 return 0;
10544}
10545
10546int bpf_link__unpin(struct bpf_link *link)
10547{
10548 int err;
10549
10550 if (!link->pin_path)
e9fc3ce9 10551 return libbpf_err(-EINVAL);
c016b68e
AN
10552
10553 err = unlink(link->pin_path);
10554 if (err != 0)
af0efa05 10555 return -errno;
c016b68e
AN
10556
10557 pr_debug("link fd=%d: unpinned from %s\n", link->fd, link->pin_path);
10558 zfree(&link->pin_path);
10559 return 0;
10560}
63f2f5ee 10561
668ace0e
AN
10562struct bpf_link_perf {
10563 struct bpf_link link;
10564 int perf_event_fd;
ca304b40
RDT
10565 /* legacy kprobe support: keep track of probe identifier and type */
10566 char *legacy_probe_name;
46ed5fc3 10567 bool legacy_is_kprobe;
ca304b40 10568 bool legacy_is_retprobe;
668ace0e
AN
10569};
10570
46ed5fc3 10571static int remove_kprobe_event_legacy(const char *probe_name, bool retprobe);
cc10623c 10572static int remove_uprobe_event_legacy(const char *probe_name, bool retprobe);
46ed5fc3 10573
668ace0e 10574static int bpf_link_perf_detach(struct bpf_link *link)
63f2f5ee 10575{
668ace0e
AN
10576 struct bpf_link_perf *perf_link = container_of(link, struct bpf_link_perf, link);
10577 int err = 0;
63f2f5ee 10578
668ace0e 10579 if (ioctl(perf_link->perf_event_fd, PERF_EVENT_IOC_DISABLE, 0) < 0)
63f2f5ee
AN
10580 err = -errno;
10581
668ace0e
AN
10582 if (perf_link->perf_event_fd != link->fd)
10583 close(perf_link->perf_event_fd);
c016b68e 10584 close(link->fd);
668ace0e 10585
cc10623c 10586 /* legacy uprobe/kprobe needs to be removed after perf event fd closure */
46ed5fc3
AN
10587 if (perf_link->legacy_probe_name) {
10588 if (perf_link->legacy_is_kprobe) {
10589 err = remove_kprobe_event_legacy(perf_link->legacy_probe_name,
10590 perf_link->legacy_is_retprobe);
cc10623c
AN
10591 } else {
10592 err = remove_uprobe_event_legacy(perf_link->legacy_probe_name,
10593 perf_link->legacy_is_retprobe);
46ed5fc3
AN
10594 }
10595 }
ca304b40
RDT
10596
10597 return err;
63f2f5ee
AN
10598}
10599
668ace0e
AN
10600static void bpf_link_perf_dealloc(struct bpf_link *link)
10601{
10602 struct bpf_link_perf *perf_link = container_of(link, struct bpf_link_perf, link);
10603
ca304b40 10604 free(perf_link->legacy_probe_name);
668ace0e
AN
10605 free(perf_link);
10606}
10607
942025c9 10608struct bpf_link *bpf_program__attach_perf_event_opts(const struct bpf_program *prog, int pfd,
47faff37 10609 const struct bpf_perf_event_opts *opts)
63f2f5ee
AN
10610{
10611 char errmsg[STRERR_BUFSIZE];
668ace0e
AN
10612 struct bpf_link_perf *link;
10613 int prog_fd, link_fd = -1, err;
f8b299bc 10614 bool force_ioctl_attach;
63f2f5ee 10615
47faff37
AN
10616 if (!OPTS_VALID(opts, bpf_perf_event_opts))
10617 return libbpf_err_ptr(-EINVAL);
10618
63f2f5ee 10619 if (pfd < 0) {
52109584
AN
10620 pr_warn("prog '%s': invalid perf event FD %d\n",
10621 prog->name, pfd);
e9fc3ce9 10622 return libbpf_err_ptr(-EINVAL);
63f2f5ee
AN
10623 }
10624 prog_fd = bpf_program__fd(prog);
10625 if (prog_fd < 0) {
52109584
AN
10626 pr_warn("prog '%s': can't attach BPF program w/o FD (did you load it?)\n",
10627 prog->name);
e9fc3ce9 10628 return libbpf_err_ptr(-EINVAL);
63f2f5ee
AN
10629 }
10630
d6958706 10631 link = calloc(1, sizeof(*link));
63f2f5ee 10632 if (!link)
e9fc3ce9 10633 return libbpf_err_ptr(-ENOMEM);
668ace0e
AN
10634 link->link.detach = &bpf_link_perf_detach;
10635 link->link.dealloc = &bpf_link_perf_dealloc;
10636 link->perf_event_fd = pfd;
63f2f5ee 10637
f8b299bc
MD
10638 force_ioctl_attach = OPTS_GET(opts, force_ioctl_attach, false);
10639 if (kernel_supports(prog->obj, FEAT_PERF_LINK) && !force_ioctl_attach) {
47faff37
AN
10640 DECLARE_LIBBPF_OPTS(bpf_link_create_opts, link_opts,
10641 .perf_event.bpf_cookie = OPTS_GET(opts, bpf_cookie, 0));
10642
10643 link_fd = bpf_link_create(prog_fd, pfd, BPF_PERF_EVENT, &link_opts);
668ace0e
AN
10644 if (link_fd < 0) {
10645 err = -errno;
10646 pr_warn("prog '%s': failed to create BPF link for perf_event FD %d: %d (%s)\n",
10647 prog->name, pfd,
10648 err, libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
10649 goto err_out;
10650 }
10651 link->link.fd = link_fd;
10652 } else {
47faff37
AN
10653 if (OPTS_GET(opts, bpf_cookie, 0)) {
10654 pr_warn("prog '%s': user context value is not supported\n", prog->name);
10655 err = -EOPNOTSUPP;
10656 goto err_out;
10657 }
10658
668ace0e
AN
10659 if (ioctl(pfd, PERF_EVENT_IOC_SET_BPF, prog_fd) < 0) {
10660 err = -errno;
10661 pr_warn("prog '%s': failed to attach to perf_event FD %d: %s\n",
10662 prog->name, pfd, libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
10663 if (err == -EPROTO)
10664 pr_warn("prog '%s': try add PERF_SAMPLE_CALLCHAIN to or remove exclude_callchain_[kernel|user] from pfd %d\n",
10665 prog->name, pfd);
10666 goto err_out;
10667 }
10668 link->link.fd = pfd;
63f2f5ee
AN
10669 }
10670 if (ioctl(pfd, PERF_EVENT_IOC_ENABLE, 0) < 0) {
10671 err = -errno;
668ace0e 10672 pr_warn("prog '%s': failed to enable perf_event FD %d: %s\n",
52109584 10673 prog->name, pfd, libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
668ace0e 10674 goto err_out;
63f2f5ee 10675 }
668ace0e
AN
10676
10677 return &link->link;
10678err_out:
10679 if (link_fd >= 0)
10680 close(link_fd);
10681 free(link);
10682 return libbpf_err_ptr(err);
63f2f5ee
AN
10683}
10684
942025c9 10685struct bpf_link *bpf_program__attach_perf_event(const struct bpf_program *prog, int pfd)
47faff37
AN
10686{
10687 return bpf_program__attach_perf_event_opts(prog, pfd, NULL);
10688}
10689
b2650027
AN
10690/*
10691 * this function is expected to parse integer in the range of [0, 2^31-1] from
10692 * given file using scanf format string fmt. If actual parsed value is
10693 * negative, the result might be indistinguishable from error
10694 */
10695static int parse_uint_from_file(const char *file, const char *fmt)
10696{
10697 char buf[STRERR_BUFSIZE];
10698 int err, ret;
10699 FILE *f;
10700
59842c54 10701 f = fopen(file, "re");
b2650027
AN
10702 if (!f) {
10703 err = -errno;
10704 pr_debug("failed to open '%s': %s\n", file,
10705 libbpf_strerror_r(err, buf, sizeof(buf)));
10706 return err;
10707 }
10708 err = fscanf(f, fmt, &ret);
10709 if (err != 1) {
10710 err = err == EOF ? -EIO : -errno;
10711 pr_debug("failed to parse '%s': %s\n", file,
10712 libbpf_strerror_r(err, buf, sizeof(buf)));
10713 fclose(f);
10714 return err;
10715 }
10716 fclose(f);
10717 return ret;
10718}
10719
10720static int determine_kprobe_perf_type(void)
10721{
10722 const char *file = "/sys/bus/event_source/devices/kprobe/type";
10723
10724 return parse_uint_from_file(file, "%d\n");
10725}
10726
10727static int determine_uprobe_perf_type(void)
10728{
10729 const char *file = "/sys/bus/event_source/devices/uprobe/type";
10730
10731 return parse_uint_from_file(file, "%d\n");
10732}
10733
10734static int determine_kprobe_retprobe_bit(void)
10735{
10736 const char *file = "/sys/bus/event_source/devices/kprobe/format/retprobe";
10737
10738 return parse_uint_from_file(file, "config:%d\n");
10739}
10740
10741static int determine_uprobe_retprobe_bit(void)
10742{
10743 const char *file = "/sys/bus/event_source/devices/uprobe/format/retprobe";
10744
10745 return parse_uint_from_file(file, "config:%d\n");
10746}
10747
5e3b8356
AN
10748#define PERF_UPROBE_REF_CTR_OFFSET_BITS 32
10749#define PERF_UPROBE_REF_CTR_OFFSET_SHIFT 32
10750
b2650027 10751static int perf_event_open_probe(bool uprobe, bool retprobe, const char *name,
5e3b8356 10752 uint64_t offset, int pid, size_t ref_ctr_off)
b2650027 10753{
813847a3
AN
10754 const size_t attr_sz = sizeof(struct perf_event_attr);
10755 struct perf_event_attr attr;
b2650027 10756 char errmsg[STRERR_BUFSIZE];
708ac5be 10757 int type, pfd;
b2650027 10758
1520e846 10759 if ((__u64)ref_ctr_off >= (1ULL << PERF_UPROBE_REF_CTR_OFFSET_BITS))
5e3b8356
AN
10760 return -EINVAL;
10761
813847a3
AN
10762 memset(&attr, 0, attr_sz);
10763
b2650027
AN
10764 type = uprobe ? determine_uprobe_perf_type()
10765 : determine_kprobe_perf_type();
10766 if (type < 0) {
be18010e
KW
10767 pr_warn("failed to determine %s perf type: %s\n",
10768 uprobe ? "uprobe" : "kprobe",
10769 libbpf_strerror_r(type, errmsg, sizeof(errmsg)));
b2650027
AN
10770 return type;
10771 }
10772 if (retprobe) {
10773 int bit = uprobe ? determine_uprobe_retprobe_bit()
10774 : determine_kprobe_retprobe_bit();
10775
10776 if (bit < 0) {
be18010e
KW
10777 pr_warn("failed to determine %s retprobe bit: %s\n",
10778 uprobe ? "uprobe" : "kprobe",
10779 libbpf_strerror_r(bit, errmsg, sizeof(errmsg)));
b2650027
AN
10780 return bit;
10781 }
10782 attr.config |= 1 << bit;
10783 }
813847a3 10784 attr.size = attr_sz;
b2650027 10785 attr.type = type;
5e3b8356 10786 attr.config |= (__u64)ref_ctr_off << PERF_UPROBE_REF_CTR_OFFSET_SHIFT;
36db2a94
AN
10787 attr.config1 = ptr_to_u64(name); /* kprobe_func or uprobe_path */
10788 attr.config2 = offset; /* kprobe_addr or probe_offset */
b2650027
AN
10789
10790 /* pid filter is meaningful only for uprobes */
10791 pfd = syscall(__NR_perf_event_open, &attr,
10792 pid < 0 ? -1 : pid /* pid */,
10793 pid == -1 ? 0 : -1 /* cpu */,
10794 -1 /* group_fd */, PERF_FLAG_FD_CLOEXEC);
708ac5be 10795 return pfd >= 0 ? pfd : -errno;
b2650027
AN
10796}
10797
46ed5fc3
AN
10798static int append_to_file(const char *file, const char *fmt, ...)
10799{
10800 int fd, n, err = 0;
10801 va_list ap;
01dc26c9
LP
10802 char buf[1024];
10803
10804 va_start(ap, fmt);
10805 n = vsnprintf(buf, sizeof(buf), fmt, ap);
10806 va_end(ap);
10807
10808 if (n < 0 || n >= sizeof(buf))
10809 return -EINVAL;
46ed5fc3 10810
92274e24 10811 fd = open(file, O_WRONLY | O_APPEND | O_CLOEXEC, 0);
46ed5fc3
AN
10812 if (fd < 0)
10813 return -errno;
10814
01dc26c9 10815 if (write(fd, buf, n) < 0)
46ed5fc3
AN
10816 err = -errno;
10817
10818 close(fd);
10819 return err;
10820}
10821
a1ac9fd6
AN
10822#define DEBUGFS "/sys/kernel/debug/tracing"
10823#define TRACEFS "/sys/kernel/tracing"
10824
10825static bool use_debugfs(void)
10826{
10827 static int has_debugfs = -1;
10828
10829 if (has_debugfs < 0)
6a4ab886 10830 has_debugfs = faccessat(AT_FDCWD, DEBUGFS, F_OK, AT_EACCESS) == 0;
a1ac9fd6
AN
10831
10832 return has_debugfs == 1;
10833}
10834
10835static const char *tracefs_path(void)
10836{
10837 return use_debugfs() ? DEBUGFS : TRACEFS;
10838}
10839
10840static const char *tracefs_kprobe_events(void)
10841{
10842 return use_debugfs() ? DEBUGFS"/kprobe_events" : TRACEFS"/kprobe_events";
10843}
10844
10845static const char *tracefs_uprobe_events(void)
10846{
10847 return use_debugfs() ? DEBUGFS"/uprobe_events" : TRACEFS"/uprobe_events";
10848}
10849
8a3fe76f
JL
10850static const char *tracefs_available_filter_functions(void)
10851{
10852 return use_debugfs() ? DEBUGFS"/available_filter_functions"
10853 : TRACEFS"/available_filter_functions";
10854}
10855
56baeeba
JL
10856static const char *tracefs_available_filter_functions_addrs(void)
10857{
10858 return use_debugfs() ? DEBUGFS"/available_filter_functions_addrs"
10859 : TRACEFS"/available_filter_functions_addrs";
10860}
10861
46ed5fc3
AN
10862static void gen_kprobe_legacy_event_name(char *buf, size_t buf_sz,
10863 const char *kfunc_name, size_t offset)
10864{
51a33c60 10865 static int index = 0;
2fa07453 10866 int i;
51a33c60
QW
10867
10868 snprintf(buf, buf_sz, "libbpf_%u_%s_0x%zx_%d", getpid(), kfunc_name, offset,
10869 __sync_fetch_and_add(&index, 1));
2fa07453
MD
10870
10871 /* sanitize binary_path in the probe name */
10872 for (i = 0; buf[i]; i++) {
10873 if (!isalnum(buf[i]))
10874 buf[i] = '_';
10875 }
46ed5fc3
AN
10876}
10877
10878static int add_kprobe_event_legacy(const char *probe_name, bool retprobe,
10879 const char *kfunc_name, size_t offset)
10880{
a1ac9fd6 10881 return append_to_file(tracefs_kprobe_events(), "%c:%s/%s %s+0x%zx",
46ed5fc3
AN
10882 retprobe ? 'r' : 'p',
10883 retprobe ? "kretprobes" : "kprobes",
10884 probe_name, kfunc_name, offset);
10885}
10886
10887static int remove_kprobe_event_legacy(const char *probe_name, bool retprobe)
10888{
a1ac9fd6
AN
10889 return append_to_file(tracefs_kprobe_events(), "-:%s/%s",
10890 retprobe ? "kretprobes" : "kprobes", probe_name);
46ed5fc3
AN
10891}
10892
10893static int determine_kprobe_perf_type_legacy(const char *probe_name, bool retprobe)
10894{
10895 char file[256];
10896
a1ac9fd6
AN
10897 snprintf(file, sizeof(file), "%s/events/%s/%s/id",
10898 tracefs_path(), retprobe ? "kretprobes" : "kprobes", probe_name);
46ed5fc3
AN
10899
10900 return parse_uint_from_file(file, "%d\n");
10901}
10902
10903static int perf_event_kprobe_open_legacy(const char *probe_name, bool retprobe,
10904 const char *kfunc_name, size_t offset, int pid)
ca304b40 10905{
813847a3
AN
10906 const size_t attr_sz = sizeof(struct perf_event_attr);
10907 struct perf_event_attr attr;
ca304b40
RDT
10908 char errmsg[STRERR_BUFSIZE];
10909 int type, pfd, err;
10910
46ed5fc3 10911 err = add_kprobe_event_legacy(probe_name, retprobe, kfunc_name, offset);
ca304b40 10912 if (err < 0) {
46ed5fc3
AN
10913 pr_warn("failed to add legacy kprobe event for '%s+0x%zx': %s\n",
10914 kfunc_name, offset,
ca304b40
RDT
10915 libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
10916 return err;
10917 }
46ed5fc3 10918 type = determine_kprobe_perf_type_legacy(probe_name, retprobe);
ca304b40 10919 if (type < 0) {
80940293 10920 err = type;
46ed5fc3
AN
10921 pr_warn("failed to determine legacy kprobe event id for '%s+0x%zx': %s\n",
10922 kfunc_name, offset,
80940293
CW
10923 libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
10924 goto err_clean_legacy;
ca304b40 10925 }
813847a3
AN
10926
10927 memset(&attr, 0, attr_sz);
10928 attr.size = attr_sz;
ca304b40
RDT
10929 attr.config = type;
10930 attr.type = PERF_TYPE_TRACEPOINT;
10931
10932 pfd = syscall(__NR_perf_event_open, &attr,
10933 pid < 0 ? -1 : pid, /* pid */
10934 pid == -1 ? 0 : -1, /* cpu */
10935 -1 /* group_fd */, PERF_FLAG_FD_CLOEXEC);
10936 if (pfd < 0) {
10937 err = -errno;
10938 pr_warn("legacy kprobe perf_event_open() failed: %s\n",
10939 libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
80940293 10940 goto err_clean_legacy;
ca304b40
RDT
10941 }
10942 return pfd;
80940293
CW
10943
10944err_clean_legacy:
10945 /* Clear the newly added legacy kprobe_event */
10946 remove_kprobe_event_legacy(probe_name, retprobe);
10947 return err;
ca304b40
RDT
10948}
10949
708ac5be
AN
10950static const char *arch_specific_syscall_pfx(void)
10951{
10952#if defined(__x86_64__)
10953 return "x64";
10954#elif defined(__i386__)
10955 return "ia32";
10956#elif defined(__s390x__)
10957 return "s390x";
10958#elif defined(__s390__)
10959 return "s390";
10960#elif defined(__arm__)
10961 return "arm";
10962#elif defined(__aarch64__)
10963 return "arm64";
10964#elif defined(__mips__)
10965 return "mips";
10966#elif defined(__riscv)
10967 return "riscv";
64893e83
DM
10968#elif defined(__powerpc__)
10969 return "powerpc";
10970#elif defined(__powerpc64__)
10971 return "powerpc64";
708ac5be
AN
10972#else
10973 return NULL;
10974#endif
10975}
10976
f3dcee93 10977int probe_kern_syscall_wrapper(int token_fd)
708ac5be
AN
10978{
10979 char syscall_name[64];
10980 const char *ksys_pfx;
10981
10982 ksys_pfx = arch_specific_syscall_pfx();
10983 if (!ksys_pfx)
10984 return 0;
10985
10986 snprintf(syscall_name, sizeof(syscall_name), "__%s_sys_bpf", ksys_pfx);
10987
10988 if (determine_kprobe_perf_type() >= 0) {
10989 int pfd;
10990
10991 pfd = perf_event_open_probe(false, false, syscall_name, 0, getpid(), 0);
10992 if (pfd >= 0)
10993 close(pfd);
10994
10995 return pfd >= 0 ? 1 : 0;
10996 } else { /* legacy mode */
10997 char probe_name[128];
10998
10999 gen_kprobe_legacy_event_name(probe_name, sizeof(probe_name), syscall_name, 0);
11000 if (add_kprobe_event_legacy(probe_name, false, syscall_name, 0) < 0)
11001 return 0;
11002
11003 (void)remove_kprobe_event_legacy(probe_name, false);
11004 return 1;
11005 }
11006}
11007
da97553e 11008struct bpf_link *
942025c9 11009bpf_program__attach_kprobe_opts(const struct bpf_program *prog,
ac0ed488 11010 const char *func_name,
47faff37 11011 const struct bpf_kprobe_opts *opts)
b2650027 11012{
47faff37 11013 DECLARE_LIBBPF_OPTS(bpf_perf_event_opts, pe_opts);
f8b299bc 11014 enum probe_attach_mode attach_mode;
b2650027 11015 char errmsg[STRERR_BUFSIZE];
ca304b40 11016 char *legacy_probe = NULL;
b2650027 11017 struct bpf_link *link;
46ed5fc3 11018 size_t offset;
ca304b40 11019 bool retprobe, legacy;
b2650027
AN
11020 int pfd, err;
11021
da97553e
JO
11022 if (!OPTS_VALID(opts, bpf_kprobe_opts))
11023 return libbpf_err_ptr(-EINVAL);
11024
f8b299bc 11025 attach_mode = OPTS_GET(opts, attach_mode, PROBE_ATTACH_MODE_DEFAULT);
da97553e
JO
11026 retprobe = OPTS_GET(opts, retprobe, false);
11027 offset = OPTS_GET(opts, offset, 0);
47faff37 11028 pe_opts.bpf_cookie = OPTS_GET(opts, bpf_cookie, 0);
da97553e 11029
ca304b40 11030 legacy = determine_kprobe_perf_type() < 0;
f8b299bc
MD
11031 switch (attach_mode) {
11032 case PROBE_ATTACH_MODE_LEGACY:
11033 legacy = true;
11034 pe_opts.force_ioctl_attach = true;
11035 break;
11036 case PROBE_ATTACH_MODE_PERF:
11037 if (legacy)
11038 return libbpf_err_ptr(-ENOTSUP);
11039 pe_opts.force_ioctl_attach = true;
11040 break;
11041 case PROBE_ATTACH_MODE_LINK:
11042 if (legacy || !kernel_supports(prog->obj, FEAT_PERF_LINK))
11043 return libbpf_err_ptr(-ENOTSUP);
11044 break;
11045 case PROBE_ATTACH_MODE_DEFAULT:
11046 break;
11047 default:
11048 return libbpf_err_ptr(-EINVAL);
11049 }
11050
ca304b40
RDT
11051 if (!legacy) {
11052 pfd = perf_event_open_probe(false /* uprobe */, retprobe,
11053 func_name, offset,
11054 -1 /* pid */, 0 /* ref_ctr_off */);
11055 } else {
46ed5fc3
AN
11056 char probe_name[256];
11057
11058 gen_kprobe_legacy_event_name(probe_name, sizeof(probe_name),
11059 func_name, offset);
11060
71cff670 11061 legacy_probe = strdup(probe_name);
ca304b40
RDT
11062 if (!legacy_probe)
11063 return libbpf_err_ptr(-ENOMEM);
11064
46ed5fc3 11065 pfd = perf_event_kprobe_open_legacy(legacy_probe, retprobe, func_name,
ca304b40
RDT
11066 offset, -1 /* pid */);
11067 }
b2650027 11068 if (pfd < 0) {
46ed5fc3
AN
11069 err = -errno;
11070 pr_warn("prog '%s': failed to create %s '%s+0x%zx' perf event: %s\n",
11071 prog->name, retprobe ? "kretprobe" : "kprobe",
11072 func_name, offset,
303a2572
AN
11073 libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
11074 goto err_out;
b2650027 11075 }
47faff37 11076 link = bpf_program__attach_perf_event_opts(prog, pfd, &pe_opts);
e9fc3ce9
AN
11077 err = libbpf_get_error(link);
11078 if (err) {
b2650027 11079 close(pfd);
46ed5fc3
AN
11080 pr_warn("prog '%s': failed to attach to %s '%s+0x%zx': %s\n",
11081 prog->name, retprobe ? "kretprobe" : "kprobe",
11082 func_name, offset,
be18010e 11083 libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
80940293 11084 goto err_clean_legacy;
b2650027 11085 }
ca304b40
RDT
11086 if (legacy) {
11087 struct bpf_link_perf *perf_link = container_of(link, struct bpf_link_perf, link);
11088
11089 perf_link->legacy_probe_name = legacy_probe;
46ed5fc3 11090 perf_link->legacy_is_kprobe = true;
ca304b40
RDT
11091 perf_link->legacy_is_retprobe = retprobe;
11092 }
11093
b2650027 11094 return link;
80940293
CW
11095
11096err_clean_legacy:
11097 if (legacy)
11098 remove_kprobe_event_legacy(legacy_probe, retprobe);
303a2572
AN
11099err_out:
11100 free(legacy_probe);
11101 return libbpf_err_ptr(err);
b2650027
AN
11102}
11103
942025c9 11104struct bpf_link *bpf_program__attach_kprobe(const struct bpf_program *prog,
ac0ed488
JO
11105 bool retprobe,
11106 const char *func_name)
11107{
da97553e 11108 DECLARE_LIBBPF_OPTS(bpf_kprobe_opts, opts,
ac0ed488 11109 .retprobe = retprobe,
da97553e 11110 );
ac0ed488
JO
11111
11112 return bpf_program__attach_kprobe_opts(prog, func_name, &opts);
11113}
11114
708ac5be
AN
11115struct bpf_link *bpf_program__attach_ksyscall(const struct bpf_program *prog,
11116 const char *syscall_name,
11117 const struct bpf_ksyscall_opts *opts)
11118{
11119 LIBBPF_OPTS(bpf_kprobe_opts, kprobe_opts);
11120 char func_name[128];
11121
11122 if (!OPTS_VALID(opts, bpf_ksyscall_opts))
11123 return libbpf_err_ptr(-EINVAL);
11124
11125 if (kernel_supports(prog->obj, FEAT_SYSCALL_WRAPPER)) {
64893e83
DM
11126 /* arch_specific_syscall_pfx() should never return NULL here
11127 * because it is guarded by kernel_supports(). However, since
11128 * compiler does not know that we have an explicit conditional
11129 * as well.
11130 */
708ac5be 11131 snprintf(func_name, sizeof(func_name), "__%s_sys_%s",
64893e83 11132 arch_specific_syscall_pfx() ? : "", syscall_name);
708ac5be
AN
11133 } else {
11134 snprintf(func_name, sizeof(func_name), "__se_sys_%s", syscall_name);
11135 }
11136
11137 kprobe_opts.retprobe = OPTS_GET(opts, retprobe, false);
11138 kprobe_opts.bpf_cookie = OPTS_GET(opts, bpf_cookie, 0);
11139
11140 return bpf_program__attach_kprobe_opts(prog, func_name, &kprobe_opts);
11141}
11142
ddc6b049 11143/* Adapted from perf/util/string.c */
e613d1d0 11144bool glob_match(const char *str, const char *pat)
ddc6b049
JO
11145{
11146 while (*str && *pat && *pat != '*') {
11147 if (*pat == '?') { /* Matches any single character */
11148 str++;
11149 pat++;
11150 continue;
11151 }
11152 if (*str != *pat)
11153 return false;
11154 str++;
11155 pat++;
11156 }
11157 /* Check wild card */
11158 if (*pat == '*') {
11159 while (*pat == '*')
11160 pat++;
11161 if (!*pat) /* Tail wild card matches all */
11162 return true;
11163 while (*str)
11164 if (glob_match(str++, pat))
11165 return true;
11166 }
11167 return !*str && !*pat;
11168}
11169
11170struct kprobe_multi_resolve {
11171 const char *pattern;
11172 unsigned long *addrs;
11173 size_t cap;
11174 size_t cnt;
11175};
11176
8a3fe76f
JL
11177struct avail_kallsyms_data {
11178 char **syms;
11179 size_t cnt;
11180 struct kprobe_multi_resolve *res;
11181};
11182
11183static int avail_func_cmp(const void *a, const void *b)
11184{
11185 return strcmp(*(const char **)a, *(const char **)b);
11186}
11187
11188static int avail_kallsyms_cb(unsigned long long sym_addr, char sym_type,
11189 const char *sym_name, void *ctx)
ddc6b049 11190{
8a3fe76f
JL
11191 struct avail_kallsyms_data *data = ctx;
11192 struct kprobe_multi_resolve *res = data->res;
ddc6b049
JO
11193 int err;
11194
8a3fe76f 11195 if (!bsearch(&sym_name, data->syms, data->cnt, sizeof(*data->syms), avail_func_cmp))
ddc6b049
JO
11196 return 0;
11197
8a3fe76f 11198 err = libbpf_ensure_mem((void **)&res->addrs, &res->cap, sizeof(*res->addrs), res->cnt + 1);
ddc6b049
JO
11199 if (err)
11200 return err;
11201
8a3fe76f 11202 res->addrs[res->cnt++] = (unsigned long)sym_addr;
ddc6b049
JO
11203 return 0;
11204}
11205
8a3fe76f
JL
11206static int libbpf_available_kallsyms_parse(struct kprobe_multi_resolve *res)
11207{
11208 const char *available_functions_file = tracefs_available_filter_functions();
11209 struct avail_kallsyms_data data;
11210 char sym_name[500];
11211 FILE *f;
11212 int err = 0, ret, i;
11213 char **syms = NULL;
11214 size_t cap = 0, cnt = 0;
11215
11216 f = fopen(available_functions_file, "re");
11217 if (!f) {
11218 err = -errno;
11219 pr_warn("failed to open %s: %d\n", available_functions_file, err);
11220 return err;
11221 }
11222
11223 while (true) {
11224 char *name;
11225
11226 ret = fscanf(f, "%499s%*[^\n]\n", sym_name);
11227 if (ret == EOF && feof(f))
11228 break;
11229
11230 if (ret != 1) {
11231 pr_warn("failed to parse available_filter_functions entry: %d\n", ret);
11232 err = -EINVAL;
11233 goto cleanup;
11234 }
11235
11236 if (!glob_match(sym_name, res->pattern))
11237 continue;
11238
11239 err = libbpf_ensure_mem((void **)&syms, &cap, sizeof(*syms), cnt + 1);
11240 if (err)
11241 goto cleanup;
11242
11243 name = strdup(sym_name);
11244 if (!name) {
11245 err = -errno;
11246 goto cleanup;
11247 }
11248
11249 syms[cnt++] = name;
11250 }
11251
11252 /* no entries found, bail out */
11253 if (cnt == 0) {
11254 err = -ENOENT;
11255 goto cleanup;
11256 }
11257
11258 /* sort available functions */
11259 qsort(syms, cnt, sizeof(*syms), avail_func_cmp);
11260
11261 data.syms = syms;
11262 data.res = res;
11263 data.cnt = cnt;
11264 libbpf_kallsyms_parse(avail_kallsyms_cb, &data);
11265
11266 if (res->cnt == 0)
11267 err = -ENOENT;
11268
11269cleanup:
11270 for (i = 0; i < cnt; i++)
11271 free((char *)syms[i]);
11272 free(syms);
11273
11274 fclose(f);
11275 return err;
11276}
11277
56baeeba
JL
11278static bool has_available_filter_functions_addrs(void)
11279{
11280 return access(tracefs_available_filter_functions_addrs(), R_OK) != -1;
11281}
11282
11283static int libbpf_available_kprobes_parse(struct kprobe_multi_resolve *res)
11284{
11285 const char *available_path = tracefs_available_filter_functions_addrs();
11286 char sym_name[500];
11287 FILE *f;
11288 int ret, err = 0;
11289 unsigned long long sym_addr;
11290
11291 f = fopen(available_path, "re");
11292 if (!f) {
11293 err = -errno;
11294 pr_warn("failed to open %s: %d\n", available_path, err);
11295 return err;
11296 }
11297
11298 while (true) {
11299 ret = fscanf(f, "%llx %499s%*[^\n]\n", &sym_addr, sym_name);
11300 if (ret == EOF && feof(f))
11301 break;
11302
11303 if (ret != 2) {
11304 pr_warn("failed to parse available_filter_functions_addrs entry: %d\n",
11305 ret);
11306 err = -EINVAL;
11307 goto cleanup;
11308 }
11309
11310 if (!glob_match(sym_name, res->pattern))
11311 continue;
11312
11313 err = libbpf_ensure_mem((void **)&res->addrs, &res->cap,
11314 sizeof(*res->addrs), res->cnt + 1);
11315 if (err)
11316 goto cleanup;
11317
11318 res->addrs[res->cnt++] = (unsigned long)sym_addr;
11319 }
11320
11321 if (res->cnt == 0)
11322 err = -ENOENT;
11323
11324cleanup:
11325 fclose(f);
11326 return err;
11327}
11328
ddc6b049
JO
11329struct bpf_link *
11330bpf_program__attach_kprobe_multi_opts(const struct bpf_program *prog,
11331 const char *pattern,
11332 const struct bpf_kprobe_multi_opts *opts)
11333{
11334 LIBBPF_OPTS(bpf_link_create_opts, lopts);
11335 struct kprobe_multi_resolve res = {
11336 .pattern = pattern,
11337 };
11338 struct bpf_link *link = NULL;
11339 char errmsg[STRERR_BUFSIZE];
11340 const unsigned long *addrs;
11341 int err, link_fd, prog_fd;
11342 const __u64 *cookies;
11343 const char **syms;
11344 bool retprobe;
11345 size_t cnt;
11346
11347 if (!OPTS_VALID(opts, bpf_kprobe_multi_opts))
11348 return libbpf_err_ptr(-EINVAL);
11349
11350 syms = OPTS_GET(opts, syms, false);
11351 addrs = OPTS_GET(opts, addrs, false);
11352 cnt = OPTS_GET(opts, cnt, false);
11353 cookies = OPTS_GET(opts, cookies, false);
11354
11355 if (!pattern && !addrs && !syms)
11356 return libbpf_err_ptr(-EINVAL);
11357 if (pattern && (addrs || syms || cookies || cnt))
11358 return libbpf_err_ptr(-EINVAL);
11359 if (!pattern && !cnt)
11360 return libbpf_err_ptr(-EINVAL);
11361 if (addrs && syms)
11362 return libbpf_err_ptr(-EINVAL);
11363
11364 if (pattern) {
56baeeba
JL
11365 if (has_available_filter_functions_addrs())
11366 err = libbpf_available_kprobes_parse(&res);
11367 else
11368 err = libbpf_available_kallsyms_parse(&res);
ddc6b049
JO
11369 if (err)
11370 goto error;
ddc6b049
JO
11371 addrs = res.addrs;
11372 cnt = res.cnt;
11373 }
11374
11375 retprobe = OPTS_GET(opts, retprobe, false);
11376
11377 lopts.kprobe_multi.syms = syms;
11378 lopts.kprobe_multi.addrs = addrs;
11379 lopts.kprobe_multi.cookies = cookies;
11380 lopts.kprobe_multi.cnt = cnt;
11381 lopts.kprobe_multi.flags = retprobe ? BPF_F_KPROBE_MULTI_RETURN : 0;
11382
11383 link = calloc(1, sizeof(*link));
11384 if (!link) {
11385 err = -ENOMEM;
11386 goto error;
11387 }
11388 link->detach = &bpf_link__detach_fd;
11389
11390 prog_fd = bpf_program__fd(prog);
11391 link_fd = bpf_link_create(prog_fd, 0, BPF_TRACE_KPROBE_MULTI, &lopts);
11392 if (link_fd < 0) {
11393 err = -errno;
11394 pr_warn("prog '%s': failed to attach: %s\n",
11395 prog->name, libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
11396 goto error;
11397 }
11398 link->fd = link_fd;
11399 free(res.addrs);
11400 return link;
11401
11402error:
11403 free(link);
11404 free(res.addrs);
11405 return libbpf_err_ptr(err);
11406}
11407
4fa5bcfe 11408static int attach_kprobe(const struct bpf_program *prog, long cookie, struct bpf_link **link)
d7a18ea7 11409{
da97553e 11410 DECLARE_LIBBPF_OPTS(bpf_kprobe_opts, opts);
a2488b5f 11411 unsigned long offset = 0;
d7a18ea7 11412 const char *func_name;
a2488b5f 11413 char *func;
4fa5bcfe 11414 int n;
d7a18ea7 11415
9af8efc4
AN
11416 *link = NULL;
11417
11418 /* no auto-attach for SEC("kprobe") and SEC("kretprobe") */
11419 if (strcmp(prog->sec_name, "kprobe") == 0 || strcmp(prog->sec_name, "kretprobe") == 0)
11420 return 0;
11421
13d35a0c
AN
11422 opts.retprobe = str_has_pfx(prog->sec_name, "kretprobe/");
11423 if (opts.retprobe)
11424 func_name = prog->sec_name + sizeof("kretprobe/") - 1;
11425 else
11426 func_name = prog->sec_name + sizeof("kprobe/") - 1;
d7a18ea7 11427
e3f9bc35 11428 n = sscanf(func_name, "%m[a-zA-Z0-9_.]+%li", &func, &offset);
a2488b5f 11429 if (n < 1) {
a2488b5f 11430 pr_warn("kprobe name is invalid: %s\n", func_name);
4fa5bcfe 11431 return -EINVAL;
a2488b5f
AM
11432 }
11433 if (opts.retprobe && offset != 0) {
1f71a468 11434 free(func);
a2488b5f 11435 pr_warn("kretprobes do not support offset specification\n");
4fa5bcfe 11436 return -EINVAL;
a2488b5f 11437 }
d7a18ea7 11438
a2488b5f 11439 opts.offset = offset;
4fa5bcfe 11440 *link = bpf_program__attach_kprobe_opts(prog, func, &opts);
a2488b5f 11441 free(func);
4fa5bcfe 11442 return libbpf_get_error(*link);
d7a18ea7
AN
11443}
11444
708ac5be
AN
11445static int attach_ksyscall(const struct bpf_program *prog, long cookie, struct bpf_link **link)
11446{
11447 LIBBPF_OPTS(bpf_ksyscall_opts, opts);
11448 const char *syscall_name;
11449
11450 *link = NULL;
11451
11452 /* no auto-attach for SEC("ksyscall") and SEC("kretsyscall") */
11453 if (strcmp(prog->sec_name, "ksyscall") == 0 || strcmp(prog->sec_name, "kretsyscall") == 0)
11454 return 0;
11455
11456 opts.retprobe = str_has_pfx(prog->sec_name, "kretsyscall/");
11457 if (opts.retprobe)
11458 syscall_name = prog->sec_name + sizeof("kretsyscall/") - 1;
11459 else
11460 syscall_name = prog->sec_name + sizeof("ksyscall/") - 1;
11461
11462 *link = bpf_program__attach_ksyscall(prog, syscall_name, &opts);
11463 return *link ? 0 : -errno;
11464}
11465
ddc6b049
JO
11466static int attach_kprobe_multi(const struct bpf_program *prog, long cookie, struct bpf_link **link)
11467{
11468 LIBBPF_OPTS(bpf_kprobe_multi_opts, opts);
11469 const char *spec;
11470 char *pattern;
11471 int n;
11472
9af8efc4
AN
11473 *link = NULL;
11474
11475 /* no auto-attach for SEC("kprobe.multi") and SEC("kretprobe.multi") */
11476 if (strcmp(prog->sec_name, "kprobe.multi") == 0 ||
11477 strcmp(prog->sec_name, "kretprobe.multi") == 0)
11478 return 0;
11479
ddc6b049
JO
11480 opts.retprobe = str_has_pfx(prog->sec_name, "kretprobe.multi/");
11481 if (opts.retprobe)
11482 spec = prog->sec_name + sizeof("kretprobe.multi/") - 1;
11483 else
11484 spec = prog->sec_name + sizeof("kprobe.multi/") - 1;
11485
11486 n = sscanf(spec, "%m[a-zA-Z0-9_.*?]", &pattern);
11487 if (n < 1) {
11488 pr_warn("kprobe multi pattern is invalid: %s\n", pattern);
11489 return -EINVAL;
11490 }
11491
11492 *link = bpf_program__attach_kprobe_multi_opts(prog, pattern, &opts);
11493 free(pattern);
11494 return libbpf_get_error(*link);
11495}
11496
5bfdd32d
JO
11497static int attach_uprobe_multi(const struct bpf_program *prog, long cookie, struct bpf_link **link)
11498{
11499 char *probe_type = NULL, *binary_path = NULL, *func_name = NULL;
11500 LIBBPF_OPTS(bpf_uprobe_multi_opts, opts);
11501 int n, ret = -EINVAL;
11502
11503 *link = NULL;
11504
2147c8d0 11505 n = sscanf(prog->sec_name, "%m[^/]/%m[^:]:%m[^\n]",
5bfdd32d
JO
11506 &probe_type, &binary_path, &func_name);
11507 switch (n) {
11508 case 1:
11509 /* handle SEC("u[ret]probe") - format is valid, but auto-attach is impossible. */
11510 ret = 0;
11511 break;
11512 case 3:
11513 opts.retprobe = strcmp(probe_type, "uretprobe.multi") == 0;
11514 *link = bpf_program__attach_uprobe_multi(prog, -1, binary_path, func_name, &opts);
11515 ret = libbpf_get_error(*link);
11516 break;
11517 default:
11518 pr_warn("prog '%s': invalid format of section definition '%s'\n", prog->name,
11519 prog->sec_name);
11520 break;
11521 }
11522 free(probe_type);
11523 free(binary_path);
11524 free(func_name);
11525 return ret;
11526}
11527
cc10623c
AN
11528static void gen_uprobe_legacy_event_name(char *buf, size_t buf_sz,
11529 const char *binary_path, uint64_t offset)
11530{
11531 int i;
11532
11533 snprintf(buf, buf_sz, "libbpf_%u_%s_0x%zx", getpid(), binary_path, (size_t)offset);
11534
11535 /* sanitize binary_path in the probe name */
11536 for (i = 0; buf[i]; i++) {
11537 if (!isalnum(buf[i]))
11538 buf[i] = '_';
11539 }
11540}
11541
11542static inline int add_uprobe_event_legacy(const char *probe_name, bool retprobe,
11543 const char *binary_path, size_t offset)
11544{
a1ac9fd6 11545 return append_to_file(tracefs_uprobe_events(), "%c:%s/%s %s:0x%zx",
cc10623c
AN
11546 retprobe ? 'r' : 'p',
11547 retprobe ? "uretprobes" : "uprobes",
11548 probe_name, binary_path, offset);
11549}
11550
11551static inline int remove_uprobe_event_legacy(const char *probe_name, bool retprobe)
11552{
a1ac9fd6
AN
11553 return append_to_file(tracefs_uprobe_events(), "-:%s/%s",
11554 retprobe ? "uretprobes" : "uprobes", probe_name);
cc10623c
AN
11555}
11556
11557static int determine_uprobe_perf_type_legacy(const char *probe_name, bool retprobe)
11558{
11559 char file[512];
11560
a1ac9fd6
AN
11561 snprintf(file, sizeof(file), "%s/events/%s/%s/id",
11562 tracefs_path(), retprobe ? "uretprobes" : "uprobes", probe_name);
cc10623c
AN
11563
11564 return parse_uint_from_file(file, "%d\n");
11565}
11566
11567static int perf_event_uprobe_open_legacy(const char *probe_name, bool retprobe,
11568 const char *binary_path, size_t offset, int pid)
11569{
813847a3 11570 const size_t attr_sz = sizeof(struct perf_event_attr);
cc10623c
AN
11571 struct perf_event_attr attr;
11572 int type, pfd, err;
11573
11574 err = add_uprobe_event_legacy(probe_name, retprobe, binary_path, offset);
11575 if (err < 0) {
11576 pr_warn("failed to add legacy uprobe event for %s:0x%zx: %d\n",
11577 binary_path, (size_t)offset, err);
11578 return err;
11579 }
11580 type = determine_uprobe_perf_type_legacy(probe_name, retprobe);
11581 if (type < 0) {
2655144f 11582 err = type;
cc10623c 11583 pr_warn("failed to determine legacy uprobe event id for %s:0x%zx: %d\n",
2655144f
CW
11584 binary_path, offset, err);
11585 goto err_clean_legacy;
cc10623c
AN
11586 }
11587
813847a3
AN
11588 memset(&attr, 0, attr_sz);
11589 attr.size = attr_sz;
cc10623c
AN
11590 attr.config = type;
11591 attr.type = PERF_TYPE_TRACEPOINT;
11592
11593 pfd = syscall(__NR_perf_event_open, &attr,
11594 pid < 0 ? -1 : pid, /* pid */
11595 pid == -1 ? 0 : -1, /* cpu */
11596 -1 /* group_fd */, PERF_FLAG_FD_CLOEXEC);
11597 if (pfd < 0) {
11598 err = -errno;
11599 pr_warn("legacy uprobe perf_event_open() failed: %d\n", err);
2655144f 11600 goto err_clean_legacy;
cc10623c
AN
11601 }
11602 return pfd;
2655144f
CW
11603
11604err_clean_legacy:
11605 /* Clear the newly added legacy uprobe_event */
11606 remove_uprobe_event_legacy(probe_name, retprobe);
11607 return err;
cc10623c
AN
11608}
11609
c44fd845
DM
11610/* Find offset of function name in archive specified by path. Currently
11611 * supported are .zip files that do not compress their contents, as used on
11612 * Android in the form of APKs, for example. "file_name" is the name of the ELF
11613 * file inside the archive. "func_name" matches symbol name or name@@LIB for
11614 * library functions.
11615 *
11616 * An overview of the APK format specifically provided here:
11617 * https://en.wikipedia.org/w/index.php?title=Apk_(file_format)&oldid=1139099120#Package_contents
11618 */
11619static long elf_find_func_offset_from_archive(const char *archive_path, const char *file_name,
11620 const char *func_name)
11621{
11622 struct zip_archive *archive;
11623 struct zip_entry entry;
11624 long ret;
11625 Elf *elf;
11626
11627 archive = zip_archive_open(archive_path);
11628 if (IS_ERR(archive)) {
11629 ret = PTR_ERR(archive);
11630 pr_warn("zip: failed to open %s: %ld\n", archive_path, ret);
11631 return ret;
11632 }
11633
11634 ret = zip_archive_find_entry(archive, file_name, &entry);
11635 if (ret) {
11636 pr_warn("zip: could not find archive member %s in %s: %ld\n", file_name,
11637 archive_path, ret);
11638 goto out;
11639 }
11640 pr_debug("zip: found entry for %s in %s at 0x%lx\n", file_name, archive_path,
11641 (unsigned long)entry.data_offset);
11642
11643 if (entry.compression) {
11644 pr_warn("zip: entry %s of %s is compressed and cannot be handled\n", file_name,
11645 archive_path);
11646 ret = -LIBBPF_ERRNO__FORMAT;
11647 goto out;
11648 }
11649
11650 elf = elf_memory((void *)entry.data, entry.data_length);
11651 if (!elf) {
11652 pr_warn("elf: could not read elf file %s from %s: %s\n", file_name, archive_path,
11653 elf_errmsg(-1));
11654 ret = -LIBBPF_ERRNO__LIBELF;
11655 goto out;
11656 }
11657
11658 ret = elf_find_func_offset(elf, file_name, func_name);
11659 if (ret > 0) {
11660 pr_debug("elf: symbol address match for %s of %s in %s: 0x%x + 0x%lx = 0x%lx\n",
11661 func_name, file_name, archive_path, entry.data_offset, ret,
11662 ret + entry.data_offset);
11663 ret += entry.data_offset;
11664 }
11665 elf_end(elf);
11666
11667out:
11668 zip_archive_close(archive);
11669 return ret;
11670}
11671
56818931
IL
11672static const char *arch_specific_lib_paths(void)
11673{
11674 /*
11675 * Based on https://packages.debian.org/sid/libc6.
11676 *
11677 * Assume that the traced program is built for the same architecture
11678 * as libbpf, which should cover the vast majority of cases.
11679 */
11680#if defined(__x86_64__)
11681 return "/lib/x86_64-linux-gnu";
11682#elif defined(__i386__)
11683 return "/lib/i386-linux-gnu";
11684#elif defined(__s390x__)
11685 return "/lib/s390x-linux-gnu";
11686#elif defined(__s390__)
11687 return "/lib/s390-linux-gnu";
11688#elif defined(__arm__) && defined(__SOFTFP__)
11689 return "/lib/arm-linux-gnueabi";
11690#elif defined(__arm__) && !defined(__SOFTFP__)
11691 return "/lib/arm-linux-gnueabihf";
11692#elif defined(__aarch64__)
11693 return "/lib/aarch64-linux-gnu";
11694#elif defined(__mips__) && defined(__MIPSEL__) && _MIPS_SZLONG == 64
11695 return "/lib/mips64el-linux-gnuabi64";
11696#elif defined(__mips__) && defined(__MIPSEL__) && _MIPS_SZLONG == 32
11697 return "/lib/mipsel-linux-gnu";
11698#elif defined(__powerpc64__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
11699 return "/lib/powerpc64le-linux-gnu";
11700#elif defined(__sparc__) && defined(__arch64__)
11701 return "/lib/sparc64-linux-gnu";
11702#elif defined(__riscv) && __riscv_xlen == 64
11703 return "/lib/riscv64-linux-gnu";
11704#else
11705 return NULL;
11706#endif
11707}
11708
1ce3a60e
AM
11709/* Get full path to program/shared library. */
11710static int resolve_full_path(const char *file, char *result, size_t result_sz)
11711{
56818931 11712 const char *search_paths[3] = {};
9e32084e 11713 int i, perm;
1ce3a60e 11714
a1c9d61b 11715 if (str_has_sfx(file, ".so") || strstr(file, ".so.")) {
1ce3a60e
AM
11716 search_paths[0] = getenv("LD_LIBRARY_PATH");
11717 search_paths[1] = "/usr/lib64:/usr/lib";
56818931 11718 search_paths[2] = arch_specific_lib_paths();
9e32084e 11719 perm = R_OK;
1ce3a60e
AM
11720 } else {
11721 search_paths[0] = getenv("PATH");
11722 search_paths[1] = "/usr/bin:/usr/sbin";
9e32084e 11723 perm = R_OK | X_OK;
1ce3a60e
AM
11724 }
11725
11726 for (i = 0; i < ARRAY_SIZE(search_paths); i++) {
11727 const char *s;
11728
11729 if (!search_paths[i])
11730 continue;
11731 for (s = search_paths[i]; s != NULL; s = strchr(s, ':')) {
11732 char *next_path;
11733 int seg_len;
11734
11735 if (s[0] == ':')
11736 s++;
11737 next_path = strchr(s, ':');
11738 seg_len = next_path ? next_path - s : strlen(s);
11739 if (!seg_len)
11740 continue;
11741 snprintf(result, result_sz, "%.*s/%s", seg_len, s, file);
9e32084e 11742 /* ensure it has required permissions */
6a4ab886 11743 if (faccessat(AT_FDCWD, result, perm, AT_EACCESS) < 0)
1ce3a60e
AM
11744 continue;
11745 pr_debug("resolved '%s' to '%s'\n", file, result);
11746 return 0;
11747 }
11748 }
11749 return -ENOENT;
11750}
11751
3140cf12
JO
11752struct bpf_link *
11753bpf_program__attach_uprobe_multi(const struct bpf_program *prog,
11754 pid_t pid,
11755 const char *path,
11756 const char *func_pattern,
11757 const struct bpf_uprobe_multi_opts *opts)
11758{
11759 const unsigned long *ref_ctr_offsets = NULL, *offsets = NULL;
11760 LIBBPF_OPTS(bpf_link_create_opts, lopts);
11761 unsigned long *resolved_offsets = NULL;
11762 int err = 0, link_fd, prog_fd;
11763 struct bpf_link *link = NULL;
11764 char errmsg[STRERR_BUFSIZE];
11765 char full_path[PATH_MAX];
11766 const __u64 *cookies;
11767 const char **syms;
11768 size_t cnt;
11769
11770 if (!OPTS_VALID(opts, bpf_uprobe_multi_opts))
11771 return libbpf_err_ptr(-EINVAL);
11772
11773 syms = OPTS_GET(opts, syms, NULL);
11774 offsets = OPTS_GET(opts, offsets, NULL);
11775 ref_ctr_offsets = OPTS_GET(opts, ref_ctr_offsets, NULL);
11776 cookies = OPTS_GET(opts, cookies, NULL);
11777 cnt = OPTS_GET(opts, cnt, 0);
11778
11779 /*
11780 * User can specify 2 mutually exclusive set of inputs:
11781 *
11782 * 1) use only path/func_pattern/pid arguments
11783 *
11784 * 2) use path/pid with allowed combinations of:
11785 * syms/offsets/ref_ctr_offsets/cookies/cnt
11786 *
11787 * - syms and offsets are mutually exclusive
11788 * - ref_ctr_offsets and cookies are optional
11789 *
11790 * Any other usage results in error.
11791 */
11792
11793 if (!path)
11794 return libbpf_err_ptr(-EINVAL);
11795 if (!func_pattern && cnt == 0)
11796 return libbpf_err_ptr(-EINVAL);
11797
11798 if (func_pattern) {
11799 if (syms || offsets || ref_ctr_offsets || cookies || cnt)
11800 return libbpf_err_ptr(-EINVAL);
11801 } else {
11802 if (!!syms == !!offsets)
11803 return libbpf_err_ptr(-EINVAL);
11804 }
11805
11806 if (func_pattern) {
11807 if (!strchr(path, '/')) {
11808 err = resolve_full_path(path, full_path, sizeof(full_path));
11809 if (err) {
11810 pr_warn("prog '%s': failed to resolve full path for '%s': %d\n",
11811 prog->name, path, err);
11812 return libbpf_err_ptr(err);
11813 }
11814 path = full_path;
11815 }
11816
11817 err = elf_resolve_pattern_offsets(path, func_pattern,
11818 &resolved_offsets, &cnt);
11819 if (err < 0)
11820 return libbpf_err_ptr(err);
11821 offsets = resolved_offsets;
11822 } else if (syms) {
48f0dfd8 11823 err = elf_resolve_syms_offsets(path, cnt, syms, &resolved_offsets, STT_FUNC);
3140cf12
JO
11824 if (err < 0)
11825 return libbpf_err_ptr(err);
11826 offsets = resolved_offsets;
11827 }
11828
11829 lopts.uprobe_multi.path = path;
11830 lopts.uprobe_multi.offsets = offsets;
11831 lopts.uprobe_multi.ref_ctr_offsets = ref_ctr_offsets;
11832 lopts.uprobe_multi.cookies = cookies;
11833 lopts.uprobe_multi.cnt = cnt;
11834 lopts.uprobe_multi.flags = OPTS_GET(opts, retprobe, false) ? BPF_F_UPROBE_MULTI_RETURN : 0;
11835
11836 if (pid == 0)
11837 pid = getpid();
11838 if (pid > 0)
11839 lopts.uprobe_multi.pid = pid;
11840
11841 link = calloc(1, sizeof(*link));
11842 if (!link) {
11843 err = -ENOMEM;
11844 goto error;
11845 }
11846 link->detach = &bpf_link__detach_fd;
11847
11848 prog_fd = bpf_program__fd(prog);
11849 link_fd = bpf_link_create(prog_fd, 0, BPF_TRACE_UPROBE_MULTI, &lopts);
11850 if (link_fd < 0) {
11851 err = -errno;
11852 pr_warn("prog '%s': failed to attach multi-uprobe: %s\n",
11853 prog->name, libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
11854 goto error;
11855 }
11856 link->fd = link_fd;
11857 free(resolved_offsets);
11858 return link;
11859
11860error:
11861 free(resolved_offsets);
11862 free(link);
11863 return libbpf_err_ptr(err);
11864}
11865
47faff37 11866LIBBPF_API struct bpf_link *
942025c9 11867bpf_program__attach_uprobe_opts(const struct bpf_program *prog, pid_t pid,
47faff37
AN
11868 const char *binary_path, size_t func_offset,
11869 const struct bpf_uprobe_opts *opts)
b2650027 11870{
c44fd845 11871 const char *archive_path = NULL, *archive_sep = NULL;
cc10623c 11872 char errmsg[STRERR_BUFSIZE], *legacy_probe = NULL;
c44fd845 11873 DECLARE_LIBBPF_OPTS(bpf_perf_event_opts, pe_opts);
f8b299bc 11874 enum probe_attach_mode attach_mode;
c44fd845 11875 char full_path[PATH_MAX];
b2650027 11876 struct bpf_link *link;
5e3b8356 11877 size_t ref_ctr_off;
b2650027 11878 int pfd, err;
cc10623c 11879 bool retprobe, legacy;
433966e3 11880 const char *func_name;
47faff37
AN
11881
11882 if (!OPTS_VALID(opts, bpf_uprobe_opts))
11883 return libbpf_err_ptr(-EINVAL);
11884
f8b299bc 11885 attach_mode = OPTS_GET(opts, attach_mode, PROBE_ATTACH_MODE_DEFAULT);
47faff37 11886 retprobe = OPTS_GET(opts, retprobe, false);
5e3b8356 11887 ref_ctr_off = OPTS_GET(opts, ref_ctr_offset, 0);
47faff37 11888 pe_opts.bpf_cookie = OPTS_GET(opts, bpf_cookie, 0);
b2650027 11889
8ed2f5a6
HC
11890 if (!binary_path)
11891 return libbpf_err_ptr(-EINVAL);
11892
c44fd845
DM
11893 /* Check if "binary_path" refers to an archive. */
11894 archive_sep = strstr(binary_path, "!/");
11895 if (archive_sep) {
11896 full_path[0] = '\0';
11897 libbpf_strlcpy(full_path, binary_path,
11898 min(sizeof(full_path), (size_t)(archive_sep - binary_path + 1)));
11899 archive_path = full_path;
11900 binary_path = archive_sep + 2;
11901 } else if (!strchr(binary_path, '/')) {
11902 err = resolve_full_path(binary_path, full_path, sizeof(full_path));
1ce3a60e 11903 if (err) {
2e4913e0
AN
11904 pr_warn("prog '%s': failed to resolve full path for '%s': %d\n",
11905 prog->name, binary_path, err);
1ce3a60e
AM
11906 return libbpf_err_ptr(err);
11907 }
c44fd845 11908 binary_path = full_path;
1ce3a60e 11909 }
433966e3
AM
11910 func_name = OPTS_GET(opts, func_name, NULL);
11911 if (func_name) {
11912 long sym_off;
11913
c44fd845
DM
11914 if (archive_path) {
11915 sym_off = elf_find_func_offset_from_archive(archive_path, binary_path,
11916 func_name);
11917 binary_path = archive_path;
11918 } else {
11919 sym_off = elf_find_func_offset_from_file(binary_path, func_name);
11920 }
433966e3
AM
11921 if (sym_off < 0)
11922 return libbpf_err_ptr(sym_off);
11923 func_offset += sym_off;
11924 }
1ce3a60e 11925
cc10623c 11926 legacy = determine_uprobe_perf_type() < 0;
f8b299bc
MD
11927 switch (attach_mode) {
11928 case PROBE_ATTACH_MODE_LEGACY:
11929 legacy = true;
11930 pe_opts.force_ioctl_attach = true;
11931 break;
11932 case PROBE_ATTACH_MODE_PERF:
11933 if (legacy)
11934 return libbpf_err_ptr(-ENOTSUP);
11935 pe_opts.force_ioctl_attach = true;
11936 break;
11937 case PROBE_ATTACH_MODE_LINK:
11938 if (legacy || !kernel_supports(prog->obj, FEAT_PERF_LINK))
11939 return libbpf_err_ptr(-ENOTSUP);
11940 break;
11941 case PROBE_ATTACH_MODE_DEFAULT:
11942 break;
11943 default:
11944 return libbpf_err_ptr(-EINVAL);
11945 }
11946
cc10623c
AN
11947 if (!legacy) {
11948 pfd = perf_event_open_probe(true /* uprobe */, retprobe, binary_path,
11949 func_offset, pid, ref_ctr_off);
11950 } else {
1ce3a60e 11951 char probe_name[PATH_MAX + 64];
cc10623c
AN
11952
11953 if (ref_ctr_off)
11954 return libbpf_err_ptr(-EINVAL);
11955
11956 gen_uprobe_legacy_event_name(probe_name, sizeof(probe_name),
11957 binary_path, func_offset);
11958
11959 legacy_probe = strdup(probe_name);
11960 if (!legacy_probe)
11961 return libbpf_err_ptr(-ENOMEM);
11962
11963 pfd = perf_event_uprobe_open_legacy(legacy_probe, retprobe,
11964 binary_path, func_offset, pid);
11965 }
b2650027 11966 if (pfd < 0) {
cc10623c 11967 err = -errno;
52109584
AN
11968 pr_warn("prog '%s': failed to create %s '%s:0x%zx' perf event: %s\n",
11969 prog->name, retprobe ? "uretprobe" : "uprobe",
be18010e 11970 binary_path, func_offset,
cc10623c
AN
11971 libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
11972 goto err_out;
b2650027 11973 }
cc10623c 11974
47faff37 11975 link = bpf_program__attach_perf_event_opts(prog, pfd, &pe_opts);
e9fc3ce9
AN
11976 err = libbpf_get_error(link);
11977 if (err) {
b2650027 11978 close(pfd);
52109584
AN
11979 pr_warn("prog '%s': failed to attach to %s '%s:0x%zx': %s\n",
11980 prog->name, retprobe ? "uretprobe" : "uprobe",
be18010e
KW
11981 binary_path, func_offset,
11982 libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
2655144f 11983 goto err_clean_legacy;
cc10623c
AN
11984 }
11985 if (legacy) {
11986 struct bpf_link_perf *perf_link = container_of(link, struct bpf_link_perf, link);
11987
11988 perf_link->legacy_probe_name = legacy_probe;
11989 perf_link->legacy_is_kprobe = false;
11990 perf_link->legacy_is_retprobe = retprobe;
b2650027
AN
11991 }
11992 return link;
2655144f
CW
11993
11994err_clean_legacy:
11995 if (legacy)
11996 remove_uprobe_event_legacy(legacy_probe, retprobe);
cc10623c
AN
11997err_out:
11998 free(legacy_probe);
11999 return libbpf_err_ptr(err);
b2650027
AN
12000}
12001
39f8dc43
AM
12002/* Format of u[ret]probe section definition supporting auto-attach:
12003 * u[ret]probe/binary:function[+offset]
12004 *
12005 * binary can be an absolute/relative path or a filename; the latter is resolved to a
12006 * full binary path via bpf_program__attach_uprobe_opts.
12007 *
12008 * Specifying uprobe+ ensures we carry out strict matching; either "uprobe" must be
12009 * specified (and auto-attach is not possible) or the above format is specified for
12010 * auto-attach.
12011 */
12012static int attach_uprobe(const struct bpf_program *prog, long cookie, struct bpf_link **link)
12013{
12014 DECLARE_LIBBPF_OPTS(bpf_uprobe_opts, opts);
2147c8d0
HC
12015 char *probe_type = NULL, *binary_path = NULL, *func_name = NULL, *func_off;
12016 int n, c, ret = -EINVAL;
90db26e6 12017 long offset = 0;
39f8dc43
AM
12018
12019 *link = NULL;
12020
2147c8d0
HC
12021 n = sscanf(prog->sec_name, "%m[^/]/%m[^:]:%m[^\n]",
12022 &probe_type, &binary_path, &func_name);
90db26e6
AM
12023 switch (n) {
12024 case 1:
12025 /* handle SEC("u[ret]probe") - format is valid, but auto-attach is impossible. */
12026 ret = 0;
12027 break;
12028 case 2:
12029 pr_warn("prog '%s': section '%s' missing ':function[+offset]' specification\n",
12030 prog->name, prog->sec_name);
12031 break;
12032 case 3:
2147c8d0
HC
12033 /* check if user specifies `+offset`, if yes, this should be
12034 * the last part of the string, make sure sscanf read to EOL
12035 */
12036 func_off = strrchr(func_name, '+');
12037 if (func_off) {
12038 n = sscanf(func_off, "+%li%n", &offset, &c);
12039 if (n == 1 && *(func_off + c) == '\0')
12040 func_off[0] = '\0';
12041 else
12042 offset = 0;
12043 }
c4cac71f
DK
12044 opts.retprobe = strcmp(probe_type, "uretprobe") == 0 ||
12045 strcmp(probe_type, "uretprobe.s") == 0;
90db26e6
AM
12046 if (opts.retprobe && offset != 0) {
12047 pr_warn("prog '%s': uretprobes do not support offset specification\n",
12048 prog->name);
12049 break;
12050 }
12051 opts.func_name = func_name;
12052 *link = bpf_program__attach_uprobe_opts(prog, -1, binary_path, offset, &opts);
12053 ret = libbpf_get_error(*link);
12054 break;
12055 default:
12056 pr_warn("prog '%s': invalid format of section definition '%s'\n", prog->name,
39f8dc43 12057 prog->sec_name);
90db26e6 12058 break;
39f8dc43 12059 }
90db26e6
AM
12060 free(probe_type);
12061 free(binary_path);
12062 free(func_name);
39f8dc43 12063
90db26e6 12064 return ret;
39f8dc43
AM
12065}
12066
942025c9 12067struct bpf_link *bpf_program__attach_uprobe(const struct bpf_program *prog,
47faff37
AN
12068 bool retprobe, pid_t pid,
12069 const char *binary_path,
12070 size_t func_offset)
12071{
12072 DECLARE_LIBBPF_OPTS(bpf_uprobe_opts, opts, .retprobe = retprobe);
12073
12074 return bpf_program__attach_uprobe_opts(prog, pid, binary_path, func_offset, &opts);
12075}
12076
2e4913e0
AN
12077struct bpf_link *bpf_program__attach_usdt(const struct bpf_program *prog,
12078 pid_t pid, const char *binary_path,
12079 const char *usdt_provider, const char *usdt_name,
12080 const struct bpf_usdt_opts *opts)
12081{
12082 char resolved_path[512];
12083 struct bpf_object *obj = prog->obj;
12084 struct bpf_link *link;
5af25a41 12085 __u64 usdt_cookie;
2e4913e0
AN
12086 int err;
12087
12088 if (!OPTS_VALID(opts, bpf_uprobe_opts))
12089 return libbpf_err_ptr(-EINVAL);
12090
12091 if (bpf_program__fd(prog) < 0) {
12092 pr_warn("prog '%s': can't attach BPF program w/o FD (did you load it?)\n",
12093 prog->name);
12094 return libbpf_err_ptr(-EINVAL);
12095 }
12096
8ed2f5a6
HC
12097 if (!binary_path)
12098 return libbpf_err_ptr(-EINVAL);
12099
2e4913e0
AN
12100 if (!strchr(binary_path, '/')) {
12101 err = resolve_full_path(binary_path, resolved_path, sizeof(resolved_path));
12102 if (err) {
12103 pr_warn("prog '%s': failed to resolve full path for '%s': %d\n",
12104 prog->name, binary_path, err);
12105 return libbpf_err_ptr(err);
12106 }
12107 binary_path = resolved_path;
12108 }
12109
12110 /* USDT manager is instantiated lazily on first USDT attach. It will
12111 * be destroyed together with BPF object in bpf_object__close().
12112 */
12113 if (IS_ERR(obj->usdt_man))
12114 return libbpf_ptr(obj->usdt_man);
12115 if (!obj->usdt_man) {
12116 obj->usdt_man = usdt_manager_new(obj);
12117 if (IS_ERR(obj->usdt_man))
12118 return libbpf_ptr(obj->usdt_man);
12119 }
12120
12121 usdt_cookie = OPTS_GET(opts, usdt_cookie, 0);
12122 link = usdt_manager_attach_usdt(obj->usdt_man, prog, pid, binary_path,
e3ba8e4e 12123 usdt_provider, usdt_name, usdt_cookie);
2e4913e0
AN
12124 err = libbpf_get_error(link);
12125 if (err)
12126 return libbpf_err_ptr(err);
12127 return link;
12128}
12129
12130static int attach_usdt(const struct bpf_program *prog, long cookie, struct bpf_link **link)
12131{
12132 char *path = NULL, *provider = NULL, *name = NULL;
12133 const char *sec_name;
12134 int n, err;
12135
12136 sec_name = bpf_program__section_name(prog);
12137 if (strcmp(sec_name, "usdt") == 0) {
12138 /* no auto-attach for just SEC("usdt") */
12139 *link = NULL;
12140 return 0;
12141 }
12142
12143 n = sscanf(sec_name, "usdt/%m[^:]:%m[^:]:%m[^:]", &path, &provider, &name);
12144 if (n != 3) {
12145 pr_warn("invalid section '%s', expected SEC(\"usdt/<path>:<provider>:<name>\")\n",
12146 sec_name);
12147 err = -EINVAL;
12148 } else {
12149 *link = bpf_program__attach_usdt(prog, -1 /* any process */, path,
12150 provider, name, NULL);
12151 err = libbpf_get_error(*link);
12152 }
12153 free(path);
12154 free(provider);
12155 free(name);
12156 return err;
12157}
12158
f6de59c1
AN
12159static int determine_tracepoint_id(const char *tp_category,
12160 const char *tp_name)
12161{
12162 char file[PATH_MAX];
12163 int ret;
12164
a1ac9fd6
AN
12165 ret = snprintf(file, sizeof(file), "%s/events/%s/%s/id",
12166 tracefs_path(), tp_category, tp_name);
f6de59c1
AN
12167 if (ret < 0)
12168 return -errno;
12169 if (ret >= sizeof(file)) {
12170 pr_debug("tracepoint %s/%s path is too long\n",
12171 tp_category, tp_name);
12172 return -E2BIG;
12173 }
12174 return parse_uint_from_file(file, "%d\n");
12175}
12176
12177static int perf_event_open_tracepoint(const char *tp_category,
12178 const char *tp_name)
12179{
813847a3
AN
12180 const size_t attr_sz = sizeof(struct perf_event_attr);
12181 struct perf_event_attr attr;
f6de59c1
AN
12182 char errmsg[STRERR_BUFSIZE];
12183 int tp_id, pfd, err;
12184
12185 tp_id = determine_tracepoint_id(tp_category, tp_name);
12186 if (tp_id < 0) {
be18010e
KW
12187 pr_warn("failed to determine tracepoint '%s/%s' perf event ID: %s\n",
12188 tp_category, tp_name,
12189 libbpf_strerror_r(tp_id, errmsg, sizeof(errmsg)));
f6de59c1
AN
12190 return tp_id;
12191 }
12192
813847a3 12193 memset(&attr, 0, attr_sz);
f6de59c1 12194 attr.type = PERF_TYPE_TRACEPOINT;
813847a3 12195 attr.size = attr_sz;
f6de59c1
AN
12196 attr.config = tp_id;
12197
12198 pfd = syscall(__NR_perf_event_open, &attr, -1 /* pid */, 0 /* cpu */,
12199 -1 /* group_fd */, PERF_FLAG_FD_CLOEXEC);
12200 if (pfd < 0) {
12201 err = -errno;
be18010e
KW
12202 pr_warn("tracepoint '%s/%s' perf_event_open() failed: %s\n",
12203 tp_category, tp_name,
12204 libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
f6de59c1
AN
12205 return err;
12206 }
12207 return pfd;
12208}
12209
942025c9 12210struct bpf_link *bpf_program__attach_tracepoint_opts(const struct bpf_program *prog,
47faff37
AN
12211 const char *tp_category,
12212 const char *tp_name,
12213 const struct bpf_tracepoint_opts *opts)
f6de59c1 12214{
47faff37 12215 DECLARE_LIBBPF_OPTS(bpf_perf_event_opts, pe_opts);
f6de59c1
AN
12216 char errmsg[STRERR_BUFSIZE];
12217 struct bpf_link *link;
12218 int pfd, err;
12219
47faff37
AN
12220 if (!OPTS_VALID(opts, bpf_tracepoint_opts))
12221 return libbpf_err_ptr(-EINVAL);
12222
12223 pe_opts.bpf_cookie = OPTS_GET(opts, bpf_cookie, 0);
12224
f6de59c1
AN
12225 pfd = perf_event_open_tracepoint(tp_category, tp_name);
12226 if (pfd < 0) {
52109584
AN
12227 pr_warn("prog '%s': failed to create tracepoint '%s/%s' perf event: %s\n",
12228 prog->name, tp_category, tp_name,
be18010e 12229 libbpf_strerror_r(pfd, errmsg, sizeof(errmsg)));
e9fc3ce9 12230 return libbpf_err_ptr(pfd);
f6de59c1 12231 }
47faff37 12232 link = bpf_program__attach_perf_event_opts(prog, pfd, &pe_opts);
e9fc3ce9
AN
12233 err = libbpf_get_error(link);
12234 if (err) {
f6de59c1 12235 close(pfd);
52109584
AN
12236 pr_warn("prog '%s': failed to attach to tracepoint '%s/%s': %s\n",
12237 prog->name, tp_category, tp_name,
be18010e 12238 libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
e9fc3ce9 12239 return libbpf_err_ptr(err);
f6de59c1
AN
12240 }
12241 return link;
12242}
12243
942025c9 12244struct bpf_link *bpf_program__attach_tracepoint(const struct bpf_program *prog,
47faff37
AN
12245 const char *tp_category,
12246 const char *tp_name)
12247{
12248 return bpf_program__attach_tracepoint_opts(prog, tp_category, tp_name, NULL);
12249}
12250
4fa5bcfe 12251static int attach_tp(const struct bpf_program *prog, long cookie, struct bpf_link **link)
d7a18ea7
AN
12252{
12253 char *sec_name, *tp_cat, *tp_name;
d7a18ea7 12254
9af8efc4
AN
12255 *link = NULL;
12256
12257 /* no auto-attach for SEC("tp") or SEC("tracepoint") */
12258 if (strcmp(prog->sec_name, "tp") == 0 || strcmp(prog->sec_name, "tracepoint") == 0)
12259 return 0;
12260
ac6a6586
AN
12261 sec_name = strdup(prog->sec_name);
12262 if (!sec_name)
12263 return -ENOMEM;
12264
13d35a0c
AN
12265 /* extract "tp/<category>/<name>" or "tracepoint/<category>/<name>" */
12266 if (str_has_pfx(prog->sec_name, "tp/"))
12267 tp_cat = sec_name + sizeof("tp/") - 1;
12268 else
12269 tp_cat = sec_name + sizeof("tracepoint/") - 1;
d7a18ea7
AN
12270 tp_name = strchr(tp_cat, '/');
12271 if (!tp_name) {
e9fc3ce9 12272 free(sec_name);
4fa5bcfe 12273 return -EINVAL;
d7a18ea7
AN
12274 }
12275 *tp_name = '\0';
12276 tp_name++;
12277
4fa5bcfe 12278 *link = bpf_program__attach_tracepoint(prog, tp_cat, tp_name);
d7a18ea7 12279 free(sec_name);
4fa5bcfe 12280 return libbpf_get_error(*link);
d7a18ea7
AN
12281}
12282
942025c9 12283struct bpf_link *bpf_program__attach_raw_tracepoint(const struct bpf_program *prog,
84bf5e1f
AN
12284 const char *tp_name)
12285{
12286 char errmsg[STRERR_BUFSIZE];
c016b68e 12287 struct bpf_link *link;
84bf5e1f
AN
12288 int prog_fd, pfd;
12289
12290 prog_fd = bpf_program__fd(prog);
12291 if (prog_fd < 0) {
52109584 12292 pr_warn("prog '%s': can't attach before loaded\n", prog->name);
e9fc3ce9 12293 return libbpf_err_ptr(-EINVAL);
84bf5e1f
AN
12294 }
12295
d6958706 12296 link = calloc(1, sizeof(*link));
84bf5e1f 12297 if (!link)
e9fc3ce9 12298 return libbpf_err_ptr(-ENOMEM);
c016b68e 12299 link->detach = &bpf_link__detach_fd;
84bf5e1f
AN
12300
12301 pfd = bpf_raw_tracepoint_open(tp_name, prog_fd);
12302 if (pfd < 0) {
12303 pfd = -errno;
12304 free(link);
52109584
AN
12305 pr_warn("prog '%s': failed to attach to raw tracepoint '%s': %s\n",
12306 prog->name, tp_name, libbpf_strerror_r(pfd, errmsg, sizeof(errmsg)));
e9fc3ce9 12307 return libbpf_err_ptr(pfd);
84bf5e1f
AN
12308 }
12309 link->fd = pfd;
c016b68e 12310 return link;
84bf5e1f
AN
12311}
12312
4fa5bcfe 12313static int attach_raw_tp(const struct bpf_program *prog, long cookie, struct bpf_link **link)
d7a18ea7 12314{
ccaf12d6 12315 static const char *const prefixes[] = {
9af8efc4
AN
12316 "raw_tp",
12317 "raw_tracepoint",
12318 "raw_tp.w",
12319 "raw_tracepoint.w",
ccaf12d6
HT
12320 };
12321 size_t i;
12322 const char *tp_name = NULL;
13d35a0c 12323
9af8efc4
AN
12324 *link = NULL;
12325
ccaf12d6 12326 for (i = 0; i < ARRAY_SIZE(prefixes); i++) {
9af8efc4
AN
12327 size_t pfx_len;
12328
12329 if (!str_has_pfx(prog->sec_name, prefixes[i]))
12330 continue;
12331
12332 pfx_len = strlen(prefixes[i]);
12333 /* no auto-attach case of, e.g., SEC("raw_tp") */
12334 if (prog->sec_name[pfx_len] == '\0')
12335 return 0;
12336
12337 if (prog->sec_name[pfx_len] != '/')
12338 continue;
12339
12340 tp_name = prog->sec_name + pfx_len + 1;
12341 break;
ccaf12d6 12342 }
9af8efc4 12343
ccaf12d6
HT
12344 if (!tp_name) {
12345 pr_warn("prog '%s': invalid section name '%s'\n",
12346 prog->name, prog->sec_name);
4fa5bcfe 12347 return -EINVAL;
ccaf12d6 12348 }
d7a18ea7 12349
4fa5bcfe 12350 *link = bpf_program__attach_raw_tracepoint(prog, tp_name);
5fd2a60a 12351 return libbpf_get_error(*link);
d7a18ea7
AN
12352}
12353
1e092a03 12354/* Common logic for all BPF program types that attach to a btf_id */
129b9c5e
KFL
12355static struct bpf_link *bpf_program__attach_btf_id(const struct bpf_program *prog,
12356 const struct bpf_trace_opts *opts)
b8c54ea4 12357{
129b9c5e 12358 LIBBPF_OPTS(bpf_link_create_opts, link_opts);
b8c54ea4 12359 char errmsg[STRERR_BUFSIZE];
c016b68e 12360 struct bpf_link *link;
b8c54ea4
AS
12361 int prog_fd, pfd;
12362
129b9c5e
KFL
12363 if (!OPTS_VALID(opts, bpf_trace_opts))
12364 return libbpf_err_ptr(-EINVAL);
12365
b8c54ea4
AS
12366 prog_fd = bpf_program__fd(prog);
12367 if (prog_fd < 0) {
52109584 12368 pr_warn("prog '%s': can't attach before loaded\n", prog->name);
e9fc3ce9 12369 return libbpf_err_ptr(-EINVAL);
b8c54ea4
AS
12370 }
12371
d6958706 12372 link = calloc(1, sizeof(*link));
b8c54ea4 12373 if (!link)
e9fc3ce9 12374 return libbpf_err_ptr(-ENOMEM);
c016b68e 12375 link->detach = &bpf_link__detach_fd;
b8c54ea4 12376
8462e0b4 12377 /* libbpf is smart enough to redirect to BPF_RAW_TRACEPOINT_OPEN on old kernels */
129b9c5e
KFL
12378 link_opts.tracing.cookie = OPTS_GET(opts, cookie, 0);
12379 pfd = bpf_link_create(prog_fd, 0, bpf_program__expected_attach_type(prog), &link_opts);
b8c54ea4
AS
12380 if (pfd < 0) {
12381 pfd = -errno;
12382 free(link);
52109584
AN
12383 pr_warn("prog '%s': failed to attach: %s\n",
12384 prog->name, libbpf_strerror_r(pfd, errmsg, sizeof(errmsg)));
e9fc3ce9 12385 return libbpf_err_ptr(pfd);
b8c54ea4
AS
12386 }
12387 link->fd = pfd;
003fed59 12388 return link;
b8c54ea4
AS
12389}
12390
942025c9 12391struct bpf_link *bpf_program__attach_trace(const struct bpf_program *prog)
1e092a03 12392{
129b9c5e
KFL
12393 return bpf_program__attach_btf_id(prog, NULL);
12394}
12395
12396struct bpf_link *bpf_program__attach_trace_opts(const struct bpf_program *prog,
12397 const struct bpf_trace_opts *opts)
12398{
12399 return bpf_program__attach_btf_id(prog, opts);
1e092a03
KS
12400}
12401
942025c9 12402struct bpf_link *bpf_program__attach_lsm(const struct bpf_program *prog)
1e092a03 12403{
129b9c5e 12404 return bpf_program__attach_btf_id(prog, NULL);
1e092a03
KS
12405}
12406
4fa5bcfe 12407static int attach_trace(const struct bpf_program *prog, long cookie, struct bpf_link **link)
d7a18ea7 12408{
4fa5bcfe
AN
12409 *link = bpf_program__attach_trace(prog);
12410 return libbpf_get_error(*link);
d7a18ea7
AN
12411}
12412
4fa5bcfe 12413static int attach_lsm(const struct bpf_program *prog, long cookie, struct bpf_link **link)
1e092a03 12414{
4fa5bcfe
AN
12415 *link = bpf_program__attach_lsm(prog);
12416 return libbpf_get_error(*link);
1e092a03
KS
12417}
12418
d60d81ac 12419static struct bpf_link *
55cc3768
DB
12420bpf_program_attach_fd(const struct bpf_program *prog,
12421 int target_fd, const char *target_name,
12422 const struct bpf_link_create_opts *opts)
cc4f864b 12423{
cc4f864b
AN
12424 enum bpf_attach_type attach_type;
12425 char errmsg[STRERR_BUFSIZE];
12426 struct bpf_link *link;
12427 int prog_fd, link_fd;
12428
12429 prog_fd = bpf_program__fd(prog);
12430 if (prog_fd < 0) {
52109584 12431 pr_warn("prog '%s': can't attach before loaded\n", prog->name);
e9fc3ce9 12432 return libbpf_err_ptr(-EINVAL);
cc4f864b
AN
12433 }
12434
12435 link = calloc(1, sizeof(*link));
12436 if (!link)
e9fc3ce9 12437 return libbpf_err_ptr(-ENOMEM);
cc4f864b
AN
12438 link->detach = &bpf_link__detach_fd;
12439
20eccf29 12440 attach_type = bpf_program__expected_attach_type(prog);
55cc3768 12441 link_fd = bpf_link_create(prog_fd, target_fd, attach_type, opts);
cc4f864b
AN
12442 if (link_fd < 0) {
12443 link_fd = -errno;
12444 free(link);
52109584
AN
12445 pr_warn("prog '%s': failed to attach to %s: %s\n",
12446 prog->name, target_name,
cc4f864b 12447 libbpf_strerror_r(link_fd, errmsg, sizeof(errmsg)));
e9fc3ce9 12448 return libbpf_err_ptr(link_fd);
cc4f864b
AN
12449 }
12450 link->fd = link_fd;
12451 return link;
12452}
12453
d60d81ac 12454struct bpf_link *
942025c9 12455bpf_program__attach_cgroup(const struct bpf_program *prog, int cgroup_fd)
d60d81ac 12456{
55cc3768 12457 return bpf_program_attach_fd(prog, cgroup_fd, "cgroup", NULL);
d60d81ac
JS
12458}
12459
12460struct bpf_link *
942025c9 12461bpf_program__attach_netns(const struct bpf_program *prog, int netns_fd)
d60d81ac 12462{
55cc3768 12463 return bpf_program_attach_fd(prog, netns_fd, "netns", NULL);
d60d81ac
JS
12464}
12465
942025c9 12466struct bpf_link *bpf_program__attach_xdp(const struct bpf_program *prog, int ifindex)
dc8698ca
AN
12467{
12468 /* target_fd/target_ifindex use the same field in LINK_CREATE */
55cc3768
DB
12469 return bpf_program_attach_fd(prog, ifindex, "xdp", NULL);
12470}
12471
12472struct bpf_link *
12473bpf_program__attach_tcx(const struct bpf_program *prog, int ifindex,
12474 const struct bpf_tcx_opts *opts)
12475{
12476 LIBBPF_OPTS(bpf_link_create_opts, link_create_opts);
12477 __u32 relative_id;
12478 int relative_fd;
12479
12480 if (!OPTS_VALID(opts, bpf_tcx_opts))
12481 return libbpf_err_ptr(-EINVAL);
12482
12483 relative_id = OPTS_GET(opts, relative_id, 0);
12484 relative_fd = OPTS_GET(opts, relative_fd, 0);
12485
12486 /* validate we don't have unexpected combinations of non-zero fields */
12487 if (!ifindex) {
12488 pr_warn("prog '%s': target netdevice ifindex cannot be zero\n",
12489 prog->name);
12490 return libbpf_err_ptr(-EINVAL);
12491 }
12492 if (relative_fd && relative_id) {
12493 pr_warn("prog '%s': relative_fd and relative_id cannot be set at the same time\n",
12494 prog->name);
12495 return libbpf_err_ptr(-EINVAL);
12496 }
12497
12498 link_create_opts.tcx.expected_revision = OPTS_GET(opts, expected_revision, 0);
12499 link_create_opts.tcx.relative_fd = relative_fd;
12500 link_create_opts.tcx.relative_id = relative_id;
12501 link_create_opts.flags = OPTS_GET(opts, flags, 0);
12502
12503 /* target_fd/target_ifindex use the same field in LINK_CREATE */
12504 return bpf_program_attach_fd(prog, ifindex, "tcx", &link_create_opts);
a5359091
THJ
12505}
12506
05c31b4a
DB
12507struct bpf_link *
12508bpf_program__attach_netkit(const struct bpf_program *prog, int ifindex,
12509 const struct bpf_netkit_opts *opts)
12510{
12511 LIBBPF_OPTS(bpf_link_create_opts, link_create_opts);
12512 __u32 relative_id;
12513 int relative_fd;
12514
12515 if (!OPTS_VALID(opts, bpf_netkit_opts))
12516 return libbpf_err_ptr(-EINVAL);
12517
12518 relative_id = OPTS_GET(opts, relative_id, 0);
12519 relative_fd = OPTS_GET(opts, relative_fd, 0);
12520
12521 /* validate we don't have unexpected combinations of non-zero fields */
12522 if (!ifindex) {
12523 pr_warn("prog '%s': target netdevice ifindex cannot be zero\n",
12524 prog->name);
12525 return libbpf_err_ptr(-EINVAL);
12526 }
12527 if (relative_fd && relative_id) {
12528 pr_warn("prog '%s': relative_fd and relative_id cannot be set at the same time\n",
12529 prog->name);
12530 return libbpf_err_ptr(-EINVAL);
12531 }
12532
12533 link_create_opts.netkit.expected_revision = OPTS_GET(opts, expected_revision, 0);
12534 link_create_opts.netkit.relative_fd = relative_fd;
12535 link_create_opts.netkit.relative_id = relative_id;
12536 link_create_opts.flags = OPTS_GET(opts, flags, 0);
12537
12538 return bpf_program_attach_fd(prog, ifindex, "netkit", &link_create_opts);
12539}
12540
942025c9 12541struct bpf_link *bpf_program__attach_freplace(const struct bpf_program *prog,
a5359091
THJ
12542 int target_fd,
12543 const char *attach_func_name)
12544{
12545 int btf_id;
12546
12547 if (!!target_fd != !!attach_func_name) {
12548 pr_warn("prog '%s': supply none or both of target_fd and attach_func_name\n",
12549 prog->name);
e9fc3ce9 12550 return libbpf_err_ptr(-EINVAL);
a5359091
THJ
12551 }
12552
12553 if (prog->type != BPF_PROG_TYPE_EXT) {
12554 pr_warn("prog '%s': only BPF_PROG_TYPE_EXT can attach as freplace",
12555 prog->name);
e9fc3ce9 12556 return libbpf_err_ptr(-EINVAL);
a5359091
THJ
12557 }
12558
12559 if (target_fd) {
55cc3768
DB
12560 LIBBPF_OPTS(bpf_link_create_opts, target_opts);
12561
a5359091
THJ
12562 btf_id = libbpf_find_prog_btf_id(attach_func_name, target_fd);
12563 if (btf_id < 0)
e9fc3ce9 12564 return libbpf_err_ptr(btf_id);
a5359091 12565
55cc3768
DB
12566 target_opts.target_btf_id = btf_id;
12567
12568 return bpf_program_attach_fd(prog, target_fd, "freplace",
12569 &target_opts);
a5359091
THJ
12570 } else {
12571 /* no target, so use raw_tracepoint_open for compatibility
12572 * with old kernels
12573 */
12574 return bpf_program__attach_trace(prog);
12575 }
dc8698ca
AN
12576}
12577
c09add2f 12578struct bpf_link *
942025c9 12579bpf_program__attach_iter(const struct bpf_program *prog,
c09add2f
YS
12580 const struct bpf_iter_attach_opts *opts)
12581{
cd31039a 12582 DECLARE_LIBBPF_OPTS(bpf_link_create_opts, link_create_opts);
c09add2f
YS
12583 char errmsg[STRERR_BUFSIZE];
12584 struct bpf_link *link;
12585 int prog_fd, link_fd;
cd31039a 12586 __u32 target_fd = 0;
c09add2f
YS
12587
12588 if (!OPTS_VALID(opts, bpf_iter_attach_opts))
e9fc3ce9 12589 return libbpf_err_ptr(-EINVAL);
c09add2f 12590
74fc097d
YS
12591 link_create_opts.iter_info = OPTS_GET(opts, link_info, (void *)0);
12592 link_create_opts.iter_info_len = OPTS_GET(opts, link_info_len, 0);
cd31039a 12593
c09add2f
YS
12594 prog_fd = bpf_program__fd(prog);
12595 if (prog_fd < 0) {
52109584 12596 pr_warn("prog '%s': can't attach before loaded\n", prog->name);
e9fc3ce9 12597 return libbpf_err_ptr(-EINVAL);
c09add2f
YS
12598 }
12599
12600 link = calloc(1, sizeof(*link));
12601 if (!link)
e9fc3ce9 12602 return libbpf_err_ptr(-ENOMEM);
c09add2f
YS
12603 link->detach = &bpf_link__detach_fd;
12604
cd31039a
YS
12605 link_fd = bpf_link_create(prog_fd, target_fd, BPF_TRACE_ITER,
12606 &link_create_opts);
c09add2f
YS
12607 if (link_fd < 0) {
12608 link_fd = -errno;
12609 free(link);
52109584
AN
12610 pr_warn("prog '%s': failed to attach to iterator: %s\n",
12611 prog->name, libbpf_strerror_r(link_fd, errmsg, sizeof(errmsg)));
e9fc3ce9 12612 return libbpf_err_ptr(link_fd);
c09add2f
YS
12613 }
12614 link->fd = link_fd;
12615 return link;
12616}
12617
4fa5bcfe 12618static int attach_iter(const struct bpf_program *prog, long cookie, struct bpf_link **link)
e9fc3ce9 12619{
4fa5bcfe
AN
12620 *link = bpf_program__attach_iter(prog, NULL);
12621 return libbpf_get_error(*link);
e9fc3ce9
AN
12622}
12623
52364abb
FW
12624struct bpf_link *bpf_program__attach_netfilter(const struct bpf_program *prog,
12625 const struct bpf_netfilter_opts *opts)
12626{
12627 LIBBPF_OPTS(bpf_link_create_opts, lopts);
12628 struct bpf_link *link;
12629 int prog_fd, link_fd;
12630
12631 if (!OPTS_VALID(opts, bpf_netfilter_opts))
12632 return libbpf_err_ptr(-EINVAL);
12633
12634 prog_fd = bpf_program__fd(prog);
12635 if (prog_fd < 0) {
12636 pr_warn("prog '%s': can't attach before loaded\n", prog->name);
12637 return libbpf_err_ptr(-EINVAL);
12638 }
12639
12640 link = calloc(1, sizeof(*link));
12641 if (!link)
12642 return libbpf_err_ptr(-ENOMEM);
12643
12644 link->detach = &bpf_link__detach_fd;
12645
12646 lopts.netfilter.pf = OPTS_GET(opts, pf, 0);
12647 lopts.netfilter.hooknum = OPTS_GET(opts, hooknum, 0);
12648 lopts.netfilter.priority = OPTS_GET(opts, priority, 0);
12649 lopts.netfilter.flags = OPTS_GET(opts, flags, 0);
12650
12651 link_fd = bpf_link_create(prog_fd, 0, BPF_NETFILTER, &lopts);
12652 if (link_fd < 0) {
12653 char errmsg[STRERR_BUFSIZE];
12654
12655 link_fd = -errno;
12656 free(link);
12657 pr_warn("prog '%s': failed to attach to netfilter: %s\n",
12658 prog->name, libbpf_strerror_r(link_fd, errmsg, sizeof(errmsg)));
12659 return libbpf_err_ptr(link_fd);
12660 }
12661 link->fd = link_fd;
12662
12663 return link;
12664}
12665
942025c9 12666struct bpf_link *bpf_program__attach(const struct bpf_program *prog)
d7a18ea7 12667{
4fa5bcfe
AN
12668 struct bpf_link *link = NULL;
12669 int err;
12670
12671 if (!prog->sec_def || !prog->sec_def->prog_attach_fn)
12672 return libbpf_err_ptr(-EOPNOTSUPP);
d7a18ea7 12673
4fa5bcfe
AN
12674 err = prog->sec_def->prog_attach_fn(prog, prog->sec_def->cookie, &link);
12675 if (err)
12676 return libbpf_err_ptr(err);
12677
12678 /* When calling bpf_program__attach() explicitly, auto-attach support
12679 * is expected to work, so NULL returned link is considered an error.
12680 * This is different for skeleton's attach, see comment in
12681 * bpf_object__attach_skeleton().
12682 */
12683 if (!link)
12684 return libbpf_err_ptr(-EOPNOTSUPP);
12685
12686 return link;
d7a18ea7
AN
12687}
12688
8d1608d7
KFL
12689struct bpf_link_struct_ops {
12690 struct bpf_link link;
12691 int map_fd;
12692};
12693
590a0088
MKL
12694static int bpf_link__detach_struct_ops(struct bpf_link *link)
12695{
8d1608d7 12696 struct bpf_link_struct_ops *st_link;
590a0088
MKL
12697 __u32 zero = 0;
12698
8d1608d7 12699 st_link = container_of(link, struct bpf_link_struct_ops, link);
590a0088 12700
8d1608d7
KFL
12701 if (st_link->map_fd < 0)
12702 /* w/o a real link */
12703 return bpf_map_delete_elem(link->fd, &zero);
12704
12705 return close(link->fd);
590a0088
MKL
12706}
12707
942025c9 12708struct bpf_link *bpf_map__attach_struct_ops(const struct bpf_map *map)
590a0088 12709{
8d1608d7
KFL
12710 struct bpf_link_struct_ops *link;
12711 __u32 zero = 0;
12712 int err, fd;
590a0088
MKL
12713
12714 if (!bpf_map__is_struct_ops(map) || map->fd == -1)
e9fc3ce9 12715 return libbpf_err_ptr(-EINVAL);
590a0088
MKL
12716
12717 link = calloc(1, sizeof(*link));
12718 if (!link)
e9fc3ce9 12719 return libbpf_err_ptr(-EINVAL);
590a0088 12720
8d1608d7
KFL
12721 /* kern_vdata should be prepared during the loading phase. */
12722 err = bpf_map_update_elem(map->fd, &zero, map->st_ops->kern_vdata, 0);
12723 /* It can be EBUSY if the map has been used to create or
12724 * update a link before. We don't allow updating the value of
12725 * a struct_ops once it is set. That ensures that the value
12726 * never changed. So, it is safe to skip EBUSY.
12727 */
12728 if (err && (!(map->def.map_flags & BPF_F_LINK) || err != -EBUSY)) {
12729 free(link);
12730 return libbpf_err_ptr(err);
12731 }
590a0088 12732
8d1608d7 12733 link->link.detach = bpf_link__detach_struct_ops;
590a0088 12734
8d1608d7
KFL
12735 if (!(map->def.map_flags & BPF_F_LINK)) {
12736 /* w/o a real link */
12737 link->link.fd = map->fd;
12738 link->map_fd = -1;
12739 return &link->link;
590a0088
MKL
12740 }
12741
8d1608d7
KFL
12742 fd = bpf_link_create(map->fd, 0, BPF_STRUCT_OPS, NULL);
12743 if (fd < 0) {
590a0088 12744 free(link);
8d1608d7 12745 return libbpf_err_ptr(fd);
590a0088
MKL
12746 }
12747
8d1608d7
KFL
12748 link->link.fd = fd;
12749 link->map_fd = map->fd;
590a0088 12750
8d1608d7 12751 return &link->link;
590a0088
MKL
12752}
12753
912dd4b0
KFL
12754/*
12755 * Swap the back struct_ops of a link with a new struct_ops map.
12756 */
12757int bpf_link__update_map(struct bpf_link *link, const struct bpf_map *map)
12758{
12759 struct bpf_link_struct_ops *st_ops_link;
12760 __u32 zero = 0;
12761 int err;
12762
f08c18e0 12763 if (!bpf_map__is_struct_ops(map) || !map_is_created(map))
912dd4b0
KFL
12764 return -EINVAL;
12765
12766 st_ops_link = container_of(link, struct bpf_link_struct_ops, link);
12767 /* Ensure the type of a link is correct */
12768 if (st_ops_link->map_fd < 0)
12769 return -EINVAL;
12770
12771 err = bpf_map_update_elem(map->fd, &zero, map->st_ops->kern_vdata, 0);
12772 /* It can be EBUSY if the map has been used to create or
12773 * update a link before. We don't allow updating the value of
12774 * a struct_ops once it is set. That ensures that the value
12775 * never changed. So, it is safe to skip EBUSY.
12776 */
12777 if (err && err != -EBUSY)
12778 return err;
12779
12780 err = bpf_link_update(link->fd, map->fd, NULL);
12781 if (err < 0)
12782 return err;
12783
12784 st_ops_link->map_fd = map->fd;
12785
12786 return 0;
12787}
12788
22dd7a58
AN
12789typedef enum bpf_perf_event_ret (*bpf_perf_event_print_t)(struct perf_event_header *hdr,
12790 void *private_data);
12791
7218c28c
CL
12792static enum bpf_perf_event_ret
12793perf_event_read_simple(void *mmap_mem, size_t mmap_size, size_t page_size,
12794 void **copy_mem, size_t *copy_size,
12795 bpf_perf_event_print_t fn, void *private_data)
d0cabbb0 12796{
3dca2115 12797 struct perf_event_mmap_page *header = mmap_mem;
a64af0ef 12798 __u64 data_head = ring_buffer_read_head(header);
d0cabbb0 12799 __u64 data_tail = header->data_tail;
3dca2115
DB
12800 void *base = ((__u8 *)header) + page_size;
12801 int ret = LIBBPF_PERF_EVENT_CONT;
12802 struct perf_event_header *ehdr;
12803 size_t ehdr_size;
12804
12805 while (data_head != data_tail) {
12806 ehdr = base + (data_tail & (mmap_size - 1));
12807 ehdr_size = ehdr->size;
12808
12809 if (((void *)ehdr) + ehdr_size > base + mmap_size) {
12810 void *copy_start = ehdr;
12811 size_t len_first = base + mmap_size - copy_start;
12812 size_t len_secnd = ehdr_size - len_first;
12813
12814 if (*copy_size < ehdr_size) {
12815 free(*copy_mem);
12816 *copy_mem = malloc(ehdr_size);
12817 if (!*copy_mem) {
12818 *copy_size = 0;
d0cabbb0
JK
12819 ret = LIBBPF_PERF_EVENT_ERROR;
12820 break;
12821 }
3dca2115 12822 *copy_size = ehdr_size;
d0cabbb0
JK
12823 }
12824
3dca2115
DB
12825 memcpy(*copy_mem, copy_start, len_first);
12826 memcpy(*copy_mem + len_first, base, len_secnd);
12827 ehdr = *copy_mem;
d0cabbb0
JK
12828 }
12829
3dca2115
DB
12830 ret = fn(ehdr, private_data);
12831 data_tail += ehdr_size;
d0cabbb0
JK
12832 if (ret != LIBBPF_PERF_EVENT_CONT)
12833 break;
d0cabbb0
JK
12834 }
12835
a64af0ef 12836 ring_buffer_write_tail(header, data_tail);
e9fc3ce9 12837 return libbpf_err(ret);
d0cabbb0 12838}
34be1646 12839
fb84b822
AN
12840struct perf_buffer;
12841
12842struct perf_buffer_params {
12843 struct perf_event_attr *attr;
12844 /* if event_cb is specified, it takes precendence */
12845 perf_buffer_event_fn event_cb;
12846 /* sample_cb and lost_cb are higher-level common-case callbacks */
12847 perf_buffer_sample_fn sample_cb;
12848 perf_buffer_lost_fn lost_cb;
12849 void *ctx;
12850 int cpu_cnt;
12851 int *cpus;
12852 int *map_keys;
12853};
12854
12855struct perf_cpu_buf {
12856 struct perf_buffer *pb;
12857 void *base; /* mmap()'ed memory */
12858 void *buf; /* for reconstructing segmented data */
12859 size_t buf_size;
12860 int fd;
12861 int cpu;
12862 int map_key;
12863};
12864
12865struct perf_buffer {
12866 perf_buffer_event_fn event_cb;
12867 perf_buffer_sample_fn sample_cb;
12868 perf_buffer_lost_fn lost_cb;
12869 void *ctx; /* passed into callbacks */
12870
12871 size_t page_size;
12872 size_t mmap_size;
12873 struct perf_cpu_buf **cpu_bufs;
12874 struct epoll_event *events;
783b8f01 12875 int cpu_cnt; /* number of allocated CPU buffers */
fb84b822
AN
12876 int epoll_fd; /* perf event FD */
12877 int map_fd; /* BPF_MAP_TYPE_PERF_EVENT_ARRAY BPF map FD */
12878};
12879
12880static void perf_buffer__free_cpu_buf(struct perf_buffer *pb,
12881 struct perf_cpu_buf *cpu_buf)
12882{
12883 if (!cpu_buf)
12884 return;
12885 if (cpu_buf->base &&
12886 munmap(cpu_buf->base, pb->mmap_size + pb->page_size))
be18010e 12887 pr_warn("failed to munmap cpu_buf #%d\n", cpu_buf->cpu);
fb84b822
AN
12888 if (cpu_buf->fd >= 0) {
12889 ioctl(cpu_buf->fd, PERF_EVENT_IOC_DISABLE, 0);
12890 close(cpu_buf->fd);
12891 }
12892 free(cpu_buf->buf);
12893 free(cpu_buf);
12894}
12895
12896void perf_buffer__free(struct perf_buffer *pb)
12897{
12898 int i;
12899
50450fc7 12900 if (IS_ERR_OR_NULL(pb))
fb84b822
AN
12901 return;
12902 if (pb->cpu_bufs) {
601b05ca 12903 for (i = 0; i < pb->cpu_cnt; i++) {
fb84b822
AN
12904 struct perf_cpu_buf *cpu_buf = pb->cpu_bufs[i];
12905
601b05ca
EC
12906 if (!cpu_buf)
12907 continue;
12908
fb84b822
AN
12909 bpf_map_delete_elem(pb->map_fd, &cpu_buf->map_key);
12910 perf_buffer__free_cpu_buf(pb, cpu_buf);
12911 }
12912 free(pb->cpu_bufs);
12913 }
12914 if (pb->epoll_fd >= 0)
12915 close(pb->epoll_fd);
12916 free(pb->events);
12917 free(pb);
12918}
12919
12920static struct perf_cpu_buf *
12921perf_buffer__open_cpu_buf(struct perf_buffer *pb, struct perf_event_attr *attr,
12922 int cpu, int map_key)
12923{
12924 struct perf_cpu_buf *cpu_buf;
12925 char msg[STRERR_BUFSIZE];
12926 int err;
12927
12928 cpu_buf = calloc(1, sizeof(*cpu_buf));
12929 if (!cpu_buf)
12930 return ERR_PTR(-ENOMEM);
12931
12932 cpu_buf->pb = pb;
12933 cpu_buf->cpu = cpu;
12934 cpu_buf->map_key = map_key;
12935
12936 cpu_buf->fd = syscall(__NR_perf_event_open, attr, -1 /* pid */, cpu,
12937 -1, PERF_FLAG_FD_CLOEXEC);
12938 if (cpu_buf->fd < 0) {
12939 err = -errno;
be18010e
KW
12940 pr_warn("failed to open perf buffer event on cpu #%d: %s\n",
12941 cpu, libbpf_strerror_r(err, msg, sizeof(msg)));
fb84b822
AN
12942 goto error;
12943 }
12944
12945 cpu_buf->base = mmap(NULL, pb->mmap_size + pb->page_size,
12946 PROT_READ | PROT_WRITE, MAP_SHARED,
12947 cpu_buf->fd, 0);
12948 if (cpu_buf->base == MAP_FAILED) {
12949 cpu_buf->base = NULL;
12950 err = -errno;
be18010e
KW
12951 pr_warn("failed to mmap perf buffer on cpu #%d: %s\n",
12952 cpu, libbpf_strerror_r(err, msg, sizeof(msg)));
fb84b822
AN
12953 goto error;
12954 }
12955
12956 if (ioctl(cpu_buf->fd, PERF_EVENT_IOC_ENABLE, 0) < 0) {
12957 err = -errno;
be18010e
KW
12958 pr_warn("failed to enable perf buffer event on cpu #%d: %s\n",
12959 cpu, libbpf_strerror_r(err, msg, sizeof(msg)));
fb84b822
AN
12960 goto error;
12961 }
12962
12963 return cpu_buf;
12964
12965error:
12966 perf_buffer__free_cpu_buf(pb, cpu_buf);
12967 return (struct perf_cpu_buf *)ERR_PTR(err);
12968}
12969
12970static struct perf_buffer *__perf_buffer__new(int map_fd, size_t page_cnt,
12971 struct perf_buffer_params *p);
12972
22dd7a58
AN
12973struct perf_buffer *perf_buffer__new(int map_fd, size_t page_cnt,
12974 perf_buffer_sample_fn sample_cb,
12975 perf_buffer_lost_fn lost_cb,
12976 void *ctx,
12977 const struct perf_buffer_opts *opts)
fb84b822 12978{
813847a3 12979 const size_t attr_sz = sizeof(struct perf_event_attr);
fb84b822 12980 struct perf_buffer_params p = {};
813847a3 12981 struct perf_event_attr attr;
ab8684b8 12982 __u32 sample_period;
41788934
AN
12983
12984 if (!OPTS_VALID(opts, perf_buffer_opts))
12985 return libbpf_err_ptr(-EINVAL);
4be6e05c 12986
ab8684b8
JD
12987 sample_period = OPTS_GET(opts, sample_period, 1);
12988 if (!sample_period)
12989 sample_period = 1;
12990
813847a3
AN
12991 memset(&attr, 0, attr_sz);
12992 attr.size = attr_sz;
65bb2e0f 12993 attr.config = PERF_COUNT_SW_BPF_OUTPUT;
4be6e05c
ACM
12994 attr.type = PERF_TYPE_SOFTWARE;
12995 attr.sample_type = PERF_SAMPLE_RAW;
ab8684b8
JD
12996 attr.sample_period = sample_period;
12997 attr.wakeup_events = sample_period;
fb84b822
AN
12998
12999 p.attr = &attr;
41788934
AN
13000 p.sample_cb = sample_cb;
13001 p.lost_cb = lost_cb;
13002 p.ctx = ctx;
fb84b822 13003
e9fc3ce9 13004 return libbpf_ptr(__perf_buffer__new(map_fd, page_cnt, &p));
fb84b822
AN
13005}
13006
22dd7a58
AN
13007struct perf_buffer *perf_buffer__new_raw(int map_fd, size_t page_cnt,
13008 struct perf_event_attr *attr,
13009 perf_buffer_event_fn event_cb, void *ctx,
13010 const struct perf_buffer_raw_opts *opts)
fb84b822
AN
13011{
13012 struct perf_buffer_params p = {};
13013
41332d6e 13014 if (!attr)
41788934
AN
13015 return libbpf_err_ptr(-EINVAL);
13016
13017 if (!OPTS_VALID(opts, perf_buffer_raw_opts))
13018 return libbpf_err_ptr(-EINVAL);
13019
13020 p.attr = attr;
13021 p.event_cb = event_cb;
13022 p.ctx = ctx;
13023 p.cpu_cnt = OPTS_GET(opts, cpu_cnt, 0);
13024 p.cpus = OPTS_GET(opts, cpus, NULL);
13025 p.map_keys = OPTS_GET(opts, map_keys, NULL);
fb84b822 13026
e9fc3ce9 13027 return libbpf_ptr(__perf_buffer__new(map_fd, page_cnt, &p));
fb84b822
AN
13028}
13029
13030static struct perf_buffer *__perf_buffer__new(int map_fd, size_t page_cnt,
13031 struct perf_buffer_params *p)
13032{
783b8f01 13033 const char *online_cpus_file = "/sys/devices/system/cpu/online";
0e289487 13034 struct bpf_map_info map;
fb84b822
AN
13035 char msg[STRERR_BUFSIZE];
13036 struct perf_buffer *pb;
783b8f01 13037 bool *online = NULL;
fb84b822 13038 __u32 map_info_len;
783b8f01 13039 int err, i, j, n;
fb84b822 13040
41332d6e 13041 if (page_cnt == 0 || (page_cnt & (page_cnt - 1))) {
be18010e
KW
13042 pr_warn("page count should be power of two, but is %zu\n",
13043 page_cnt);
fb84b822
AN
13044 return ERR_PTR(-EINVAL);
13045 }
13046
0e289487
AN
13047 /* best-effort sanity checks */
13048 memset(&map, 0, sizeof(map));
fb84b822 13049 map_info_len = sizeof(map);
629dfc66 13050 err = bpf_map_get_info_by_fd(map_fd, &map, &map_info_len);
fb84b822
AN
13051 if (err) {
13052 err = -errno;
0e289487
AN
13053 /* if BPF_OBJ_GET_INFO_BY_FD is supported, will return
13054 * -EBADFD, -EFAULT, or -E2BIG on real error
13055 */
13056 if (err != -EINVAL) {
13057 pr_warn("failed to get map info for map FD %d: %s\n",
13058 map_fd, libbpf_strerror_r(err, msg, sizeof(msg)));
13059 return ERR_PTR(err);
13060 }
13061 pr_debug("failed to get map info for FD %d; API not supported? Ignoring...\n",
13062 map_fd);
13063 } else {
13064 if (map.type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) {
13065 pr_warn("map '%s' should be BPF_MAP_TYPE_PERF_EVENT_ARRAY\n",
13066 map.name);
13067 return ERR_PTR(-EINVAL);
13068 }
fb84b822
AN
13069 }
13070
13071 pb = calloc(1, sizeof(*pb));
13072 if (!pb)
13073 return ERR_PTR(-ENOMEM);
13074
13075 pb->event_cb = p->event_cb;
13076 pb->sample_cb = p->sample_cb;
13077 pb->lost_cb = p->lost_cb;
13078 pb->ctx = p->ctx;
13079
13080 pb->page_size = getpagesize();
13081 pb->mmap_size = pb->page_size * page_cnt;
13082 pb->map_fd = map_fd;
13083
13084 pb->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
13085 if (pb->epoll_fd < 0) {
13086 err = -errno;
be18010e
KW
13087 pr_warn("failed to create epoll instance: %s\n",
13088 libbpf_strerror_r(err, msg, sizeof(msg)));
fb84b822
AN
13089 goto error;
13090 }
13091
13092 if (p->cpu_cnt > 0) {
13093 pb->cpu_cnt = p->cpu_cnt;
13094 } else {
13095 pb->cpu_cnt = libbpf_num_possible_cpus();
13096 if (pb->cpu_cnt < 0) {
13097 err = pb->cpu_cnt;
13098 goto error;
13099 }
0e289487 13100 if (map.max_entries && map.max_entries < pb->cpu_cnt)
fb84b822
AN
13101 pb->cpu_cnt = map.max_entries;
13102 }
13103
13104 pb->events = calloc(pb->cpu_cnt, sizeof(*pb->events));
13105 if (!pb->events) {
13106 err = -ENOMEM;
be18010e 13107 pr_warn("failed to allocate events: out of memory\n");
fb84b822
AN
13108 goto error;
13109 }
13110 pb->cpu_bufs = calloc(pb->cpu_cnt, sizeof(*pb->cpu_bufs));
13111 if (!pb->cpu_bufs) {
13112 err = -ENOMEM;
be18010e 13113 pr_warn("failed to allocate buffers: out of memory\n");
fb84b822
AN
13114 goto error;
13115 }
13116
783b8f01
AN
13117 err = parse_cpu_mask_file(online_cpus_file, &online, &n);
13118 if (err) {
13119 pr_warn("failed to get online CPU mask: %d\n", err);
13120 goto error;
13121 }
13122
13123 for (i = 0, j = 0; i < pb->cpu_cnt; i++) {
fb84b822
AN
13124 struct perf_cpu_buf *cpu_buf;
13125 int cpu, map_key;
13126
13127 cpu = p->cpu_cnt > 0 ? p->cpus[i] : i;
13128 map_key = p->cpu_cnt > 0 ? p->map_keys[i] : i;
13129
783b8f01
AN
13130 /* in case user didn't explicitly requested particular CPUs to
13131 * be attached to, skip offline/not present CPUs
13132 */
13133 if (p->cpu_cnt <= 0 && (cpu >= n || !online[cpu]))
13134 continue;
13135
fb84b822
AN
13136 cpu_buf = perf_buffer__open_cpu_buf(pb, p->attr, cpu, map_key);
13137 if (IS_ERR(cpu_buf)) {
13138 err = PTR_ERR(cpu_buf);
13139 goto error;
13140 }
13141
783b8f01 13142 pb->cpu_bufs[j] = cpu_buf;
fb84b822
AN
13143
13144 err = bpf_map_update_elem(pb->map_fd, &map_key,
13145 &cpu_buf->fd, 0);
13146 if (err) {
13147 err = -errno;
be18010e
KW
13148 pr_warn("failed to set cpu #%d, key %d -> perf FD %d: %s\n",
13149 cpu, map_key, cpu_buf->fd,
13150 libbpf_strerror_r(err, msg, sizeof(msg)));
fb84b822
AN
13151 goto error;
13152 }
13153
783b8f01
AN
13154 pb->events[j].events = EPOLLIN;
13155 pb->events[j].data.ptr = cpu_buf;
fb84b822 13156 if (epoll_ctl(pb->epoll_fd, EPOLL_CTL_ADD, cpu_buf->fd,
783b8f01 13157 &pb->events[j]) < 0) {
fb84b822 13158 err = -errno;
be18010e
KW
13159 pr_warn("failed to epoll_ctl cpu #%d perf FD %d: %s\n",
13160 cpu, cpu_buf->fd,
13161 libbpf_strerror_r(err, msg, sizeof(msg)));
fb84b822
AN
13162 goto error;
13163 }
783b8f01 13164 j++;
fb84b822 13165 }
783b8f01
AN
13166 pb->cpu_cnt = j;
13167 free(online);
fb84b822
AN
13168
13169 return pb;
13170
13171error:
783b8f01 13172 free(online);
fb84b822
AN
13173 if (pb)
13174 perf_buffer__free(pb);
13175 return ERR_PTR(err);
13176}
13177
13178struct perf_sample_raw {
13179 struct perf_event_header header;
13180 uint32_t size;
385bbf7b 13181 char data[];
fb84b822
AN
13182};
13183
13184struct perf_sample_lost {
13185 struct perf_event_header header;
13186 uint64_t id;
13187 uint64_t lost;
13188 uint64_t sample_id;
13189};
13190
13191static enum bpf_perf_event_ret
13192perf_buffer__process_record(struct perf_event_header *e, void *ctx)
13193{
13194 struct perf_cpu_buf *cpu_buf = ctx;
13195 struct perf_buffer *pb = cpu_buf->pb;
13196 void *data = e;
13197
13198 /* user wants full control over parsing perf event */
13199 if (pb->event_cb)
13200 return pb->event_cb(pb->ctx, cpu_buf->cpu, e);
13201
13202 switch (e->type) {
13203 case PERF_RECORD_SAMPLE: {
13204 struct perf_sample_raw *s = data;
13205
13206 if (pb->sample_cb)
13207 pb->sample_cb(pb->ctx, cpu_buf->cpu, s->data, s->size);
13208 break;
13209 }
13210 case PERF_RECORD_LOST: {
13211 struct perf_sample_lost *s = data;
13212
13213 if (pb->lost_cb)
13214 pb->lost_cb(pb->ctx, cpu_buf->cpu, s->lost);
13215 break;
13216 }
13217 default:
be18010e 13218 pr_warn("unknown perf sample type %d\n", e->type);
fb84b822
AN
13219 return LIBBPF_PERF_EVENT_ERROR;
13220 }
13221 return LIBBPF_PERF_EVENT_CONT;
13222}
13223
13224static int perf_buffer__process_records(struct perf_buffer *pb,
13225 struct perf_cpu_buf *cpu_buf)
13226{
13227 enum bpf_perf_event_ret ret;
13228
7218c28c
CL
13229 ret = perf_event_read_simple(cpu_buf->base, pb->mmap_size,
13230 pb->page_size, &cpu_buf->buf,
13231 &cpu_buf->buf_size,
13232 perf_buffer__process_record, cpu_buf);
fb84b822
AN
13233 if (ret != LIBBPF_PERF_EVENT_CONT)
13234 return ret;
13235 return 0;
13236}
13237
dca5612f
AN
13238int perf_buffer__epoll_fd(const struct perf_buffer *pb)
13239{
13240 return pb->epoll_fd;
13241}
13242
fb84b822
AN
13243int perf_buffer__poll(struct perf_buffer *pb, int timeout_ms)
13244{
13245 int i, cnt, err;
13246
13247 cnt = epoll_wait(pb->epoll_fd, pb->events, pb->cpu_cnt, timeout_ms);
e9fc3ce9 13248 if (cnt < 0)
af0efa05 13249 return -errno;
e9fc3ce9 13250
fb84b822
AN
13251 for (i = 0; i < cnt; i++) {
13252 struct perf_cpu_buf *cpu_buf = pb->events[i].data.ptr;
13253
13254 err = perf_buffer__process_records(pb, cpu_buf);
13255 if (err) {
be18010e 13256 pr_warn("error while processing records: %d\n", err);
e9fc3ce9 13257 return libbpf_err(err);
fb84b822
AN
13258 }
13259 }
e9fc3ce9 13260 return cnt;
fb84b822
AN
13261}
13262
dca5612f
AN
13263/* Return number of PERF_EVENT_ARRAY map slots set up by this perf_buffer
13264 * manager.
13265 */
13266size_t perf_buffer__buffer_cnt(const struct perf_buffer *pb)
13267{
13268 return pb->cpu_cnt;
13269}
13270
13271/*
13272 * Return perf_event FD of a ring buffer in *buf_idx* slot of
13273 * PERF_EVENT_ARRAY BPF map. This FD can be polled for new data using
13274 * select()/poll()/epoll() Linux syscalls.
13275 */
13276int perf_buffer__buffer_fd(const struct perf_buffer *pb, size_t buf_idx)
13277{
13278 struct perf_cpu_buf *cpu_buf;
13279
13280 if (buf_idx >= pb->cpu_cnt)
e9fc3ce9 13281 return libbpf_err(-EINVAL);
dca5612f
AN
13282
13283 cpu_buf = pb->cpu_bufs[buf_idx];
13284 if (!cpu_buf)
e9fc3ce9 13285 return libbpf_err(-ENOENT);
dca5612f
AN
13286
13287 return cpu_buf->fd;
13288}
13289
9ff5efde
JD
13290int perf_buffer__buffer(struct perf_buffer *pb, int buf_idx, void **buf, size_t *buf_size)
13291{
13292 struct perf_cpu_buf *cpu_buf;
13293
13294 if (buf_idx >= pb->cpu_cnt)
13295 return libbpf_err(-EINVAL);
13296
13297 cpu_buf = pb->cpu_bufs[buf_idx];
13298 if (!cpu_buf)
13299 return libbpf_err(-ENOENT);
13300
13301 *buf = cpu_buf->base;
13302 *buf_size = pb->mmap_size;
13303 return 0;
13304}
13305
dca5612f
AN
13306/*
13307 * Consume data from perf ring buffer corresponding to slot *buf_idx* in
13308 * PERF_EVENT_ARRAY BPF map without waiting/polling. If there is no data to
13309 * consume, do nothing and return success.
13310 * Returns:
13311 * - 0 on success;
13312 * - <0 on failure.
13313 */
13314int perf_buffer__consume_buffer(struct perf_buffer *pb, size_t buf_idx)
13315{
13316 struct perf_cpu_buf *cpu_buf;
13317
13318 if (buf_idx >= pb->cpu_cnt)
e9fc3ce9 13319 return libbpf_err(-EINVAL);
dca5612f
AN
13320
13321 cpu_buf = pb->cpu_bufs[buf_idx];
13322 if (!cpu_buf)
e9fc3ce9 13323 return libbpf_err(-ENOENT);
dca5612f
AN
13324
13325 return perf_buffer__process_records(pb, cpu_buf);
13326}
13327
272d51af
EC
13328int perf_buffer__consume(struct perf_buffer *pb)
13329{
13330 int i, err;
13331
13332 for (i = 0; i < pb->cpu_cnt; i++) {
13333 struct perf_cpu_buf *cpu_buf = pb->cpu_bufs[i];
13334
13335 if (!cpu_buf)
13336 continue;
13337
13338 err = perf_buffer__process_records(pb, cpu_buf);
13339 if (err) {
dca5612f 13340 pr_warn("perf_buffer: failed to process records in buffer #%d: %d\n", i, err);
e9fc3ce9 13341 return libbpf_err(err);
272d51af
EC
13342 }
13343 }
13344 return 0;
13345}
13346
ff26ce5c
EC
13347int bpf_program__set_attach_target(struct bpf_program *prog,
13348 int attach_prog_fd,
13349 const char *attach_func_name)
13350{
fe62de31 13351 int btf_obj_fd = 0, btf_id = 0, err;
ff26ce5c 13352
2d5ec1c6 13353 if (!prog || attach_prog_fd < 0)
e9fc3ce9 13354 return libbpf_err(-EINVAL);
ff26ce5c 13355
fe62de31 13356 if (prog->obj->loaded)
e9fc3ce9 13357 return libbpf_err(-EINVAL);
fe62de31 13358
2d5ec1c6
AN
13359 if (attach_prog_fd && !attach_func_name) {
13360 /* remember attach_prog_fd and let bpf_program__load() find
13361 * BTF ID during the program load
13362 */
13363 prog->attach_prog_fd = attach_prog_fd;
13364 return 0;
13365 }
13366
fe62de31 13367 if (attach_prog_fd) {
ff26ce5c
EC
13368 btf_id = libbpf_find_prog_btf_id(attach_func_name,
13369 attach_prog_fd);
fe62de31 13370 if (btf_id < 0)
e9fc3ce9 13371 return libbpf_err(btf_id);
fe62de31 13372 } else {
2d5ec1c6
AN
13373 if (!attach_func_name)
13374 return libbpf_err(-EINVAL);
13375
fe62de31
AN
13376 /* load btf_vmlinux, if not yet */
13377 err = bpf_object__load_vmlinux_btf(prog->obj, true);
13378 if (err)
e9fc3ce9 13379 return libbpf_err(err);
fe62de31
AN
13380 err = find_kernel_btf_id(prog->obj, attach_func_name,
13381 prog->expected_attach_type,
13382 &btf_obj_fd, &btf_id);
13383 if (err)
e9fc3ce9 13384 return libbpf_err(err);
fe62de31 13385 }
ff26ce5c
EC
13386
13387 prog->attach_btf_id = btf_id;
fe62de31 13388 prog->attach_btf_obj_fd = btf_obj_fd;
ff26ce5c
EC
13389 prog->attach_prog_fd = attach_prog_fd;
13390 return 0;
13391}
13392
6803ee25 13393int parse_cpu_mask_str(const char *s, bool **mask, int *mask_sz)
6446b315 13394{
6803ee25
AN
13395 int err = 0, n, len, start, end = -1;
13396 bool *tmp;
6446b315 13397
6803ee25
AN
13398 *mask = NULL;
13399 *mask_sz = 0;
13400
13401 /* Each sub string separated by ',' has format \d+-\d+ or \d+ */
13402 while (*s) {
13403 if (*s == ',' || *s == '\n') {
13404 s++;
13405 continue;
13406 }
13407 n = sscanf(s, "%d%n-%d%n", &start, &len, &end, &len);
13408 if (n <= 0 || n > 2) {
13409 pr_warn("Failed to get CPU range %s: %d\n", s, n);
13410 err = -EINVAL;
13411 goto cleanup;
13412 } else if (n == 1) {
13413 end = start;
13414 }
13415 if (start < 0 || start > end) {
13416 pr_warn("Invalid CPU range [%d,%d] in %s\n",
13417 start, end, s);
13418 err = -EINVAL;
13419 goto cleanup;
13420 }
13421 tmp = realloc(*mask, end + 1);
13422 if (!tmp) {
13423 err = -ENOMEM;
13424 goto cleanup;
13425 }
13426 *mask = tmp;
13427 memset(tmp + *mask_sz, 0, start - *mask_sz);
13428 memset(tmp + start, 1, end - start + 1);
13429 *mask_sz = end + 1;
13430 s += len;
13431 }
13432 if (!*mask_sz) {
13433 pr_warn("Empty CPU range\n");
13434 return -EINVAL;
13435 }
13436 return 0;
13437cleanup:
13438 free(*mask);
13439 *mask = NULL;
13440 return err;
13441}
13442
13443int parse_cpu_mask_file(const char *fcpu, bool **mask, int *mask_sz)
13444{
13445 int fd, err = 0, len;
13446 char buf[128];
6446b315 13447
92274e24 13448 fd = open(fcpu, O_RDONLY | O_CLOEXEC);
6446b315 13449 if (fd < 0) {
6803ee25
AN
13450 err = -errno;
13451 pr_warn("Failed to open cpu mask file %s: %d\n", fcpu, err);
13452 return err;
6446b315
HL
13453 }
13454 len = read(fd, buf, sizeof(buf));
13455 close(fd);
13456 if (len <= 0) {
6803ee25
AN
13457 err = len ? -errno : -EINVAL;
13458 pr_warn("Failed to read cpu mask from %s: %d\n", fcpu, err);
13459 return err;
6446b315 13460 }
6803ee25
AN
13461 if (len >= sizeof(buf)) {
13462 pr_warn("CPU mask is too big in file %s\n", fcpu);
13463 return -E2BIG;
6446b315
HL
13464 }
13465 buf[len] = '\0';
13466
6803ee25
AN
13467 return parse_cpu_mask_str(buf, mask, mask_sz);
13468}
13469
13470int libbpf_num_possible_cpus(void)
13471{
13472 static const char *fcpu = "/sys/devices/system/cpu/possible";
13473 static int cpus;
13474 int err, n, i, tmp_cpus;
13475 bool *mask;
13476
13477 tmp_cpus = READ_ONCE(cpus);
13478 if (tmp_cpus > 0)
13479 return tmp_cpus;
13480
13481 err = parse_cpu_mask_file(fcpu, &mask, &n);
13482 if (err)
e9fc3ce9 13483 return libbpf_err(err);
6803ee25
AN
13484
13485 tmp_cpus = 0;
13486 for (i = 0; i < n; i++) {
13487 if (mask[i])
13488 tmp_cpus++;
6446b315 13489 }
6803ee25 13490 free(mask);
56fbc241
TC
13491
13492 WRITE_ONCE(cpus, tmp_cpus);
13493 return tmp_cpus;
6446b315 13494}
d66562fb 13495
430025e5
DK
13496static int populate_skeleton_maps(const struct bpf_object *obj,
13497 struct bpf_map_skeleton *maps,
13498 size_t map_cnt)
13499{
13500 int i;
13501
13502 for (i = 0; i < map_cnt; i++) {
13503 struct bpf_map **map = maps[i].map;
13504 const char *name = maps[i].name;
13505 void **mmaped = maps[i].mmaped;
13506
13507 *map = bpf_object__find_map_by_name(obj, name);
13508 if (!*map) {
13509 pr_warn("failed to find skeleton map '%s'\n", name);
13510 return -ESRCH;
13511 }
13512
13513 /* externs shouldn't be pre-setup from user code */
13514 if (mmaped && (*map)->libbpf_type != LIBBPF_MAP_KCONFIG)
13515 *mmaped = (*map)->mmaped;
13516 }
13517 return 0;
13518}
13519
13520static int populate_skeleton_progs(const struct bpf_object *obj,
13521 struct bpf_prog_skeleton *progs,
13522 size_t prog_cnt)
13523{
13524 int i;
13525
13526 for (i = 0; i < prog_cnt; i++) {
13527 struct bpf_program **prog = progs[i].prog;
13528 const char *name = progs[i].name;
13529
13530 *prog = bpf_object__find_program_by_name(obj, name);
13531 if (!*prog) {
13532 pr_warn("failed to find skeleton program '%s'\n", name);
13533 return -ESRCH;
13534 }
13535 }
13536 return 0;
13537}
13538
d66562fb
AN
13539int bpf_object__open_skeleton(struct bpf_object_skeleton *s,
13540 const struct bpf_object_open_opts *opts)
13541{
13542 DECLARE_LIBBPF_OPTS(bpf_object_open_opts, skel_opts,
13543 .object_name = s->name,
13544 );
13545 struct bpf_object *obj;
430025e5 13546 int err;
d66562fb
AN
13547
13548 /* Attempt to preserve opts->object_name, unless overriden by user
13549 * explicitly. Overwriting object name for skeletons is discouraged,
13550 * as it breaks global data maps, because they contain object name
13551 * prefix as their own map name prefix. When skeleton is generated,
13552 * bpftool is making an assumption that this name will stay the same.
13553 */
13554 if (opts) {
13555 memcpy(&skel_opts, opts, sizeof(*opts));
13556 if (!opts->object_name)
13557 skel_opts.object_name = s->name;
13558 }
13559
13560 obj = bpf_object__open_mem(s->data, s->data_sz, &skel_opts);
e9fc3ce9
AN
13561 err = libbpf_get_error(obj);
13562 if (err) {
13563 pr_warn("failed to initialize skeleton BPF object '%s': %d\n",
13564 s->name, err);
13565 return libbpf_err(err);
d66562fb
AN
13566 }
13567
13568 *s->obj = obj;
430025e5
DK
13569 err = populate_skeleton_maps(obj, s->maps, s->map_cnt);
13570 if (err) {
13571 pr_warn("failed to populate skeleton maps for '%s': %d\n", s->name, err);
13572 return libbpf_err(err);
13573 }
13574
13575 err = populate_skeleton_progs(obj, s->progs, s->prog_cnt);
13576 if (err) {
13577 pr_warn("failed to populate skeleton progs for '%s': %d\n", s->name, err);
13578 return libbpf_err(err);
13579 }
d66562fb 13580
430025e5
DK
13581 return 0;
13582}
d66562fb 13583
430025e5
DK
13584int bpf_object__open_subskeleton(struct bpf_object_subskeleton *s)
13585{
13586 int err, len, var_idx, i;
13587 const char *var_name;
13588 const struct bpf_map *map;
13589 struct btf *btf;
13590 __u32 map_type_id;
13591 const struct btf_type *map_type, *var_type;
13592 const struct bpf_var_skeleton *var_skel;
13593 struct btf_var_secinfo *var;
d66562fb 13594
430025e5
DK
13595 if (!s->obj)
13596 return libbpf_err(-EINVAL);
13597
13598 btf = bpf_object__btf(s->obj);
13599 if (!btf) {
13600 pr_warn("subskeletons require BTF at runtime (object %s)\n",
e3ba8e4e 13601 bpf_object__name(s->obj));
430025e5 13602 return libbpf_err(-errno);
d66562fb
AN
13603 }
13604
430025e5
DK
13605 err = populate_skeleton_maps(s->obj, s->maps, s->map_cnt);
13606 if (err) {
13607 pr_warn("failed to populate subskeleton maps: %d\n", err);
13608 return libbpf_err(err);
13609 }
d66562fb 13610
430025e5
DK
13611 err = populate_skeleton_progs(s->obj, s->progs, s->prog_cnt);
13612 if (err) {
13613 pr_warn("failed to populate subskeleton maps: %d\n", err);
13614 return libbpf_err(err);
d66562fb
AN
13615 }
13616
430025e5
DK
13617 for (var_idx = 0; var_idx < s->var_cnt; var_idx++) {
13618 var_skel = &s->vars[var_idx];
13619 map = *var_skel->map;
13620 map_type_id = bpf_map__btf_value_type_id(map);
13621 map_type = btf__type_by_id(btf, map_type_id);
13622
13623 if (!btf_is_datasec(map_type)) {
13624 pr_warn("type for map '%1$s' is not a datasec: %2$s",
13625 bpf_map__name(map),
13626 __btf_kind_str(btf_kind(map_type)));
13627 return libbpf_err(-EINVAL);
13628 }
13629
13630 len = btf_vlen(map_type);
13631 var = btf_var_secinfos(map_type);
13632 for (i = 0; i < len; i++, var++) {
13633 var_type = btf__type_by_id(btf, var->type);
13634 var_name = btf__name_by_offset(btf, var_type->name_off);
13635 if (strcmp(var_name, var_skel->name) == 0) {
13636 *var_skel->addr = map->mmaped + var->offset;
13637 break;
13638 }
13639 }
13640 }
d66562fb
AN
13641 return 0;
13642}
13643
430025e5
DK
13644void bpf_object__destroy_subskeleton(struct bpf_object_subskeleton *s)
13645{
13646 if (!s)
13647 return;
13648 free(s->maps);
13649 free(s->progs);
13650 free(s->vars);
13651 free(s);
13652}
13653
d66562fb
AN
13654int bpf_object__load_skeleton(struct bpf_object_skeleton *s)
13655{
13656 int i, err;
13657
13658 err = bpf_object__load(*s->obj);
13659 if (err) {
13660 pr_warn("failed to load BPF skeleton '%s': %d\n", s->name, err);
e9fc3ce9 13661 return libbpf_err(err);
d66562fb
AN
13662 }
13663
13664 for (i = 0; i < s->map_cnt; i++) {
13665 struct bpf_map *map = *s->maps[i].map;
79ff13e9 13666 size_t mmap_sz = bpf_map_mmap_sz(map);
f08c18e0 13667 int prot, map_fd = map->fd;
d66562fb 13668 void **mmaped = s->maps[i].mmaped;
d66562fb
AN
13669
13670 if (!mmaped)
13671 continue;
13672
13673 if (!(map->def.map_flags & BPF_F_MMAPABLE)) {
13674 *mmaped = NULL;
13675 continue;
13676 }
13677
2e7ba4f8
AN
13678 if (map->def.type == BPF_MAP_TYPE_ARENA) {
13679 *mmaped = map->mmaped;
13680 continue;
13681 }
13682
d66562fb
AN
13683 if (map->def.map_flags & BPF_F_RDONLY_PROG)
13684 prot = PROT_READ;
13685 else
13686 prot = PROT_READ | PROT_WRITE;
13687
13688 /* Remap anonymous mmap()-ed "map initialization image" as
13689 * a BPF map-backed mmap()-ed memory, but preserving the same
13690 * memory address. This will cause kernel to change process'
13691 * page table to point to a different piece of kernel memory,
13692 * but from userspace point of view memory address (and its
13693 * contents, being identical at this point) will stay the
13694 * same. This mapping will be released by bpf_object__close()
13695 * as per normal clean up procedure, so we don't need to worry
13696 * about it from skeleton's clean up perspective.
13697 */
9d0a2331 13698 *mmaped = mmap(map->mmaped, mmap_sz, prot, MAP_SHARED | MAP_FIXED, map_fd, 0);
2ad97d47 13699 if (*mmaped == MAP_FAILED) {
d66562fb
AN
13700 err = -errno;
13701 *mmaped = NULL;
13702 pr_warn("failed to re-mmap() map '%s': %d\n",
13703 bpf_map__name(map), err);
e9fc3ce9 13704 return libbpf_err(err);
d66562fb
AN
13705 }
13706 }
13707
13708 return 0;
13709}
13710
13711int bpf_object__attach_skeleton(struct bpf_object_skeleton *s)
13712{
e9fc3ce9 13713 int i, err;
d66562fb
AN
13714
13715 for (i = 0; i < s->prog_cnt; i++) {
13716 struct bpf_program *prog = *s->progs[i].prog;
13717 struct bpf_link **link = s->progs[i].link;
d66562fb 13718
43cb8cba 13719 if (!prog->autoload || !prog->autoattach)
d9297581
AN
13720 continue;
13721
5532dfd4 13722 /* auto-attaching not supported for this program */
4fa5bcfe 13723 if (!prog->sec_def || !prog->sec_def->prog_attach_fn)
d66562fb
AN
13724 continue;
13725
4fa5bcfe
AN
13726 /* if user already set the link manually, don't attempt auto-attach */
13727 if (*link)
13728 continue;
13729
13730 err = prog->sec_def->prog_attach_fn(prog, prog->sec_def->cookie, link);
e9fc3ce9 13731 if (err) {
4fa5bcfe 13732 pr_warn("prog '%s': failed to auto-attach: %d\n",
e9fc3ce9
AN
13733 bpf_program__name(prog), err);
13734 return libbpf_err(err);
d66562fb 13735 }
4fa5bcfe
AN
13736
13737 /* It's possible that for some SEC() definitions auto-attach
13738 * is supported in some cases (e.g., if definition completely
13739 * specifies target information), but is not in other cases.
13740 * SEC("uprobe") is one such case. If user specified target
13741 * binary and function name, such BPF program can be
13742 * auto-attached. But if not, it shouldn't trigger skeleton's
13743 * attach to fail. It should just be skipped.
13744 * attach_fn signals such case with returning 0 (no error) and
13745 * setting link to NULL.
13746 */
d66562fb
AN
13747 }
13748
13749 return 0;
13750}
13751
13752void bpf_object__detach_skeleton(struct bpf_object_skeleton *s)
13753{
13754 int i;
13755
13756 for (i = 0; i < s->prog_cnt; i++) {
13757 struct bpf_link **link = s->progs[i].link;
13758
50450fc7 13759 bpf_link__destroy(*link);
d66562fb
AN
13760 *link = NULL;
13761 }
13762}
13763
13764void bpf_object__destroy_skeleton(struct bpf_object_skeleton *s)
13765{
a32ea51a
YS
13766 if (!s)
13767 return;
13768
d66562fb
AN
13769 if (s->progs)
13770 bpf_object__detach_skeleton(s);
13771 if (s->obj)
13772 bpf_object__close(*s->obj);
13773 free(s->maps);
13774 free(s->progs);
13775 free(s);
13776}