libbpf: Ensure libbpf always opens files with O_CLOEXEC
[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>
a64af0ef 36#include <linux/ring_buffer.h>
fb84b822 37#include <sys/epoll.h>
63f2f5ee 38#include <sys/ioctl.h>
fb84b822 39#include <sys/mman.h>
f367540c
JS
40#include <sys/stat.h>
41#include <sys/types.h>
42#include <sys/vfs.h>
ddc7c304 43#include <sys/utsname.h>
dc3a2d25 44#include <sys/resource.h>
1a5e3fb1
WN
45#include <libelf.h>
46#include <gelf.h>
166750bc 47#include <zlib.h>
1b76c13e
WN
48
49#include "libbpf.h"
52d3352e 50#include "bpf.h"
8a138aed 51#include "btf.h"
6d41907c 52#include "str_error.h"
d7c4b398 53#include "libbpf_internal.h"
ddc7c304 54#include "hashmap.h"
67234743 55#include "bpf_gen_internal.h"
c44fd845 56#include "zip.h"
b3f59d66 57
f367540c
JS
58#ifndef BPF_FS_MAGIC
59#define BPF_FS_MAGIC 0xcafe4a11
60#endif
61
9c0f8cbd
AN
62#define BPF_INSN_SZ (sizeof(struct bpf_insn))
63
ff466b58
AI
64/* vsprintf() in __base_pr() uses nonliteral format string. It may break
65 * compilation if user enables corresponding warning. Disable it explicitly.
66 */
67#pragma GCC diagnostic ignored "-Wformat-nonliteral"
68
b3f59d66
WN
69#define __printf(a, b) __attribute__((format(printf, a, b)))
70
590a0088 71static struct bpf_map *bpf_object__add_map(struct bpf_object *obj);
aea28a60 72static bool prog_is_subprog(const struct bpf_object *obj, const struct bpf_program *prog);
590a0088 73
ccde5760
DM
74static const char * const attach_type_name[] = {
75 [BPF_CGROUP_INET_INGRESS] = "cgroup_inet_ingress",
76 [BPF_CGROUP_INET_EGRESS] = "cgroup_inet_egress",
77 [BPF_CGROUP_INET_SOCK_CREATE] = "cgroup_inet_sock_create",
78 [BPF_CGROUP_INET_SOCK_RELEASE] = "cgroup_inet_sock_release",
79 [BPF_CGROUP_SOCK_OPS] = "cgroup_sock_ops",
80 [BPF_CGROUP_DEVICE] = "cgroup_device",
81 [BPF_CGROUP_INET4_BIND] = "cgroup_inet4_bind",
82 [BPF_CGROUP_INET6_BIND] = "cgroup_inet6_bind",
83 [BPF_CGROUP_INET4_CONNECT] = "cgroup_inet4_connect",
84 [BPF_CGROUP_INET6_CONNECT] = "cgroup_inet6_connect",
85 [BPF_CGROUP_INET4_POST_BIND] = "cgroup_inet4_post_bind",
86 [BPF_CGROUP_INET6_POST_BIND] = "cgroup_inet6_post_bind",
87 [BPF_CGROUP_INET4_GETPEERNAME] = "cgroup_inet4_getpeername",
88 [BPF_CGROUP_INET6_GETPEERNAME] = "cgroup_inet6_getpeername",
89 [BPF_CGROUP_INET4_GETSOCKNAME] = "cgroup_inet4_getsockname",
90 [BPF_CGROUP_INET6_GETSOCKNAME] = "cgroup_inet6_getsockname",
91 [BPF_CGROUP_UDP4_SENDMSG] = "cgroup_udp4_sendmsg",
92 [BPF_CGROUP_UDP6_SENDMSG] = "cgroup_udp6_sendmsg",
93 [BPF_CGROUP_SYSCTL] = "cgroup_sysctl",
94 [BPF_CGROUP_UDP4_RECVMSG] = "cgroup_udp4_recvmsg",
95 [BPF_CGROUP_UDP6_RECVMSG] = "cgroup_udp6_recvmsg",
96 [BPF_CGROUP_GETSOCKOPT] = "cgroup_getsockopt",
97 [BPF_CGROUP_SETSOCKOPT] = "cgroup_setsockopt",
98 [BPF_SK_SKB_STREAM_PARSER] = "sk_skb_stream_parser",
99 [BPF_SK_SKB_STREAM_VERDICT] = "sk_skb_stream_verdict",
100 [BPF_SK_SKB_VERDICT] = "sk_skb_verdict",
101 [BPF_SK_MSG_VERDICT] = "sk_msg_verdict",
102 [BPF_LIRC_MODE2] = "lirc_mode2",
103 [BPF_FLOW_DISSECTOR] = "flow_dissector",
104 [BPF_TRACE_RAW_TP] = "trace_raw_tp",
105 [BPF_TRACE_FENTRY] = "trace_fentry",
106 [BPF_TRACE_FEXIT] = "trace_fexit",
107 [BPF_MODIFY_RETURN] = "modify_return",
108 [BPF_LSM_MAC] = "lsm_mac",
bffcf348 109 [BPF_LSM_CGROUP] = "lsm_cgroup",
ccde5760
DM
110 [BPF_SK_LOOKUP] = "sk_lookup",
111 [BPF_TRACE_ITER] = "trace_iter",
112 [BPF_XDP_DEVMAP] = "xdp_devmap",
113 [BPF_XDP_CPUMAP] = "xdp_cpumap",
114 [BPF_XDP] = "xdp",
115 [BPF_SK_REUSEPORT_SELECT] = "sk_reuseport_select",
116 [BPF_SK_REUSEPORT_SELECT_OR_MIGRATE] = "sk_reuseport_select_or_migrate",
117 [BPF_PERF_EVENT] = "perf_event",
118 [BPF_TRACE_KPROBE_MULTI] = "trace_kprobe_multi",
8d1608d7 119 [BPF_STRUCT_OPS] = "struct_ops",
ccde5760
DM
120};
121
ba5d1b58
DM
122static const char * const link_type_name[] = {
123 [BPF_LINK_TYPE_UNSPEC] = "unspec",
124 [BPF_LINK_TYPE_RAW_TRACEPOINT] = "raw_tracepoint",
125 [BPF_LINK_TYPE_TRACING] = "tracing",
126 [BPF_LINK_TYPE_CGROUP] = "cgroup",
127 [BPF_LINK_TYPE_ITER] = "iter",
128 [BPF_LINK_TYPE_NETNS] = "netns",
129 [BPF_LINK_TYPE_XDP] = "xdp",
130 [BPF_LINK_TYPE_PERF_EVENT] = "perf_event",
131 [BPF_LINK_TYPE_KPROBE_MULTI] = "kprobe_multi",
132 [BPF_LINK_TYPE_STRUCT_OPS] = "struct_ops",
d0fe92fb 133 [BPF_LINK_TYPE_NETFILTER] = "netfilter",
ba5d1b58
DM
134};
135
3e6dc020
DM
136static const char * const map_type_name[] = {
137 [BPF_MAP_TYPE_UNSPEC] = "unspec",
138 [BPF_MAP_TYPE_HASH] = "hash",
139 [BPF_MAP_TYPE_ARRAY] = "array",
140 [BPF_MAP_TYPE_PROG_ARRAY] = "prog_array",
141 [BPF_MAP_TYPE_PERF_EVENT_ARRAY] = "perf_event_array",
142 [BPF_MAP_TYPE_PERCPU_HASH] = "percpu_hash",
143 [BPF_MAP_TYPE_PERCPU_ARRAY] = "percpu_array",
144 [BPF_MAP_TYPE_STACK_TRACE] = "stack_trace",
145 [BPF_MAP_TYPE_CGROUP_ARRAY] = "cgroup_array",
146 [BPF_MAP_TYPE_LRU_HASH] = "lru_hash",
147 [BPF_MAP_TYPE_LRU_PERCPU_HASH] = "lru_percpu_hash",
148 [BPF_MAP_TYPE_LPM_TRIE] = "lpm_trie",
149 [BPF_MAP_TYPE_ARRAY_OF_MAPS] = "array_of_maps",
150 [BPF_MAP_TYPE_HASH_OF_MAPS] = "hash_of_maps",
151 [BPF_MAP_TYPE_DEVMAP] = "devmap",
152 [BPF_MAP_TYPE_DEVMAP_HASH] = "devmap_hash",
153 [BPF_MAP_TYPE_SOCKMAP] = "sockmap",
154 [BPF_MAP_TYPE_CPUMAP] = "cpumap",
155 [BPF_MAP_TYPE_XSKMAP] = "xskmap",
156 [BPF_MAP_TYPE_SOCKHASH] = "sockhash",
157 [BPF_MAP_TYPE_CGROUP_STORAGE] = "cgroup_storage",
158 [BPF_MAP_TYPE_REUSEPORT_SOCKARRAY] = "reuseport_sockarray",
159 [BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE] = "percpu_cgroup_storage",
160 [BPF_MAP_TYPE_QUEUE] = "queue",
161 [BPF_MAP_TYPE_STACK] = "stack",
162 [BPF_MAP_TYPE_SK_STORAGE] = "sk_storage",
163 [BPF_MAP_TYPE_STRUCT_OPS] = "struct_ops",
164 [BPF_MAP_TYPE_RINGBUF] = "ringbuf",
165 [BPF_MAP_TYPE_INODE_STORAGE] = "inode_storage",
166 [BPF_MAP_TYPE_TASK_STORAGE] = "task_storage",
167 [BPF_MAP_TYPE_BLOOM_FILTER] = "bloom_filter",
583c1f42 168 [BPF_MAP_TYPE_USER_RINGBUF] = "user_ringbuf",
4fe64af2 169 [BPF_MAP_TYPE_CGRP_STORAGE] = "cgrp_storage",
3e6dc020
DM
170};
171
d18616e7
DM
172static const char * const prog_type_name[] = {
173 [BPF_PROG_TYPE_UNSPEC] = "unspec",
174 [BPF_PROG_TYPE_SOCKET_FILTER] = "socket_filter",
175 [BPF_PROG_TYPE_KPROBE] = "kprobe",
176 [BPF_PROG_TYPE_SCHED_CLS] = "sched_cls",
177 [BPF_PROG_TYPE_SCHED_ACT] = "sched_act",
178 [BPF_PROG_TYPE_TRACEPOINT] = "tracepoint",
179 [BPF_PROG_TYPE_XDP] = "xdp",
180 [BPF_PROG_TYPE_PERF_EVENT] = "perf_event",
181 [BPF_PROG_TYPE_CGROUP_SKB] = "cgroup_skb",
182 [BPF_PROG_TYPE_CGROUP_SOCK] = "cgroup_sock",
183 [BPF_PROG_TYPE_LWT_IN] = "lwt_in",
184 [BPF_PROG_TYPE_LWT_OUT] = "lwt_out",
185 [BPF_PROG_TYPE_LWT_XMIT] = "lwt_xmit",
186 [BPF_PROG_TYPE_SOCK_OPS] = "sock_ops",
187 [BPF_PROG_TYPE_SK_SKB] = "sk_skb",
188 [BPF_PROG_TYPE_CGROUP_DEVICE] = "cgroup_device",
189 [BPF_PROG_TYPE_SK_MSG] = "sk_msg",
190 [BPF_PROG_TYPE_RAW_TRACEPOINT] = "raw_tracepoint",
191 [BPF_PROG_TYPE_CGROUP_SOCK_ADDR] = "cgroup_sock_addr",
192 [BPF_PROG_TYPE_LWT_SEG6LOCAL] = "lwt_seg6local",
193 [BPF_PROG_TYPE_LIRC_MODE2] = "lirc_mode2",
194 [BPF_PROG_TYPE_SK_REUSEPORT] = "sk_reuseport",
195 [BPF_PROG_TYPE_FLOW_DISSECTOR] = "flow_dissector",
196 [BPF_PROG_TYPE_CGROUP_SYSCTL] = "cgroup_sysctl",
197 [BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE] = "raw_tracepoint_writable",
198 [BPF_PROG_TYPE_CGROUP_SOCKOPT] = "cgroup_sockopt",
199 [BPF_PROG_TYPE_TRACING] = "tracing",
200 [BPF_PROG_TYPE_STRUCT_OPS] = "struct_ops",
201 [BPF_PROG_TYPE_EXT] = "ext",
202 [BPF_PROG_TYPE_LSM] = "lsm",
203 [BPF_PROG_TYPE_SK_LOOKUP] = "sk_lookup",
204 [BPF_PROG_TYPE_SYSCALL] = "syscall",
d0fe92fb 205 [BPF_PROG_TYPE_NETFILTER] = "netfilter",
d18616e7
DM
206};
207
a8a1f7d0
SF
208static int __base_pr(enum libbpf_print_level level, const char *format,
209 va_list args)
b3f59d66 210{
6f1ae8b6
YS
211 if (level == LIBBPF_DEBUG)
212 return 0;
213
a8a1f7d0 214 return vfprintf(stderr, format, args);
b3f59d66
WN
215}
216
a8a1f7d0 217static libbpf_print_fn_t __libbpf_pr = __base_pr;
b3f59d66 218
e87fd8ba 219libbpf_print_fn_t libbpf_set_print(libbpf_print_fn_t fn)
b3f59d66 220{
f1cb927c
JK
221 libbpf_print_fn_t old_print_fn;
222
223 old_print_fn = __atomic_exchange_n(&__libbpf_pr, fn, __ATOMIC_RELAXED);
e87fd8ba 224
e87fd8ba 225 return old_print_fn;
b3f59d66 226}
1a5e3fb1 227
8461ef8b
YS
228__printf(2, 3)
229void libbpf_print(enum libbpf_print_level level, const char *format, ...)
230{
231 va_list args;
d7c5802f 232 int old_errno;
f1cb927c 233 libbpf_print_fn_t print_fn;
8461ef8b 234
f1cb927c
JK
235 print_fn = __atomic_load_n(&__libbpf_pr, __ATOMIC_RELAXED);
236 if (!print_fn)
6f1ae8b6
YS
237 return;
238
d7c5802f
AN
239 old_errno = errno;
240
8461ef8b 241 va_start(args, format);
6f1ae8b6 242 __libbpf_pr(level, format, args);
8461ef8b 243 va_end(args);
d7c5802f
AN
244
245 errno = old_errno;
8461ef8b
YS
246}
247
dc3a2d25
THJ
248static void pr_perm_msg(int err)
249{
250 struct rlimit limit;
251 char buf[100];
252
253 if (err != -EPERM || geteuid() != 0)
254 return;
255
256 err = getrlimit(RLIMIT_MEMLOCK, &limit);
257 if (err)
258 return;
259
260 if (limit.rlim_cur == RLIM_INFINITY)
261 return;
262
263 if (limit.rlim_cur < 1024)
b5c7d0d0 264 snprintf(buf, sizeof(buf), "%zu bytes", (size_t)limit.rlim_cur);
dc3a2d25
THJ
265 else if (limit.rlim_cur < 1024*1024)
266 snprintf(buf, sizeof(buf), "%.1f KiB", (double)limit.rlim_cur / 1024);
267 else
268 snprintf(buf, sizeof(buf), "%.1f MiB", (double)limit.rlim_cur / (1024*1024));
269
270 pr_warn("permission error while running as root; try raising 'ulimit -l'? current value: %s\n",
271 buf);
272}
273
6371ca3b
WN
274#define STRERR_BUFSIZE 128
275
1a5e3fb1
WN
276/* Copied from tools/perf/util/util.h */
277#ifndef zfree
278# define zfree(ptr) ({ free(*ptr); *ptr = NULL; })
279#endif
280
281#ifndef zclose
282# define zclose(fd) ({ \
283 int ___err = 0; \
284 if ((fd) >= 0) \
285 ___err = close((fd)); \
286 fd = -1; \
287 ___err; })
288#endif
289
34be1646
SL
290static inline __u64 ptr_to_u64(const void *ptr)
291{
292 return (__u64) (unsigned long) ptr;
293}
294
5981881d
AN
295int libbpf_set_strict_mode(enum libbpf_strict_mode mode)
296{
bd054102 297 /* as of v1.0 libbpf_set_strict_mode() is a no-op */
5981881d
AN
298 return 0;
299}
300
7615209f
AN
301__u32 libbpf_major_version(void)
302{
303 return LIBBPF_MAJOR_VERSION;
304}
305
306__u32 libbpf_minor_version(void)
307{
308 return LIBBPF_MINOR_VERSION;
309}
310
311const char *libbpf_version_string(void)
312{
313#define __S(X) #X
314#define _S(X) __S(X)
315 return "v" _S(LIBBPF_MAJOR_VERSION) "." _S(LIBBPF_MINOR_VERSION);
316#undef _S
317#undef __S
318}
319
166750bc
AN
320enum reloc_type {
321 RELO_LD64,
322 RELO_CALL,
323 RELO_DATA,
a18f7214
AS
324 RELO_EXTERN_LD64,
325 RELO_EXTERN_CALL,
53eddb5e 326 RELO_SUBPROG_ADDR,
d0e92887 327 RELO_CORE,
166750bc
AN
328};
329
330struct reloc_desc {
331 enum reloc_type type;
332 int insn_idx;
d0e92887
AS
333 union {
334 const struct bpf_core_relo *core_relo; /* used when type == RELO_CORE */
335 struct {
336 int map_idx;
337 int sym_off;
3055ddd6 338 int ext_idx;
d0e92887
AS
339 };
340 };
166750bc
AN
341};
342
15ea31fa
AN
343/* stored as sec_def->cookie for all libbpf-supported SEC()s */
344enum sec_def_flags {
345 SEC_NONE = 0,
346 /* expected_attach_type is optional, if kernel doesn't support that */
347 SEC_EXP_ATTACH_OPT = 1,
348 /* legacy, only used by libbpf_get_type_names() and
349 * libbpf_attach_type_by_name(), not used by libbpf itself at all.
350 * This used to be associated with cgroup (and few other) BPF programs
351 * that were attachable through BPF_PROG_ATTACH command. Pretty
352 * meaningless nowadays, though.
353 */
354 SEC_ATTACHABLE = 2,
355 SEC_ATTACHABLE_OPT = SEC_ATTACHABLE | SEC_EXP_ATTACH_OPT,
356 /* attachment target is specified through BTF ID in either kernel or
e3ba8e4e
KM
357 * other BPF program's BTF object
358 */
15ea31fa
AN
359 SEC_ATTACH_BTF = 4,
360 /* BPF program type allows sleeping/blocking in kernel */
361 SEC_SLEEPABLE = 8,
082c4bfb 362 /* BPF program support non-linear XDP buffer */
450b167f 363 SEC_XDP_FRAGS = 16,
15ea31fa
AN
364};
365
25498a19 366struct bpf_sec_def {
697f104d 367 char *sec;
25498a19
AN
368 enum bpf_prog_type prog_type;
369 enum bpf_attach_type expected_attach_type;
15ea31fa 370 long cookie;
697f104d 371 int handler_id;
12d9466d 372
4fa5bcfe
AN
373 libbpf_prog_setup_fn_t prog_setup_fn;
374 libbpf_prog_prepare_load_fn_t prog_prepare_load_fn;
375 libbpf_prog_attach_fn_t prog_attach_fn;
25498a19
AN
376};
377
a5b8bd47
WN
378/*
379 * bpf_prog should be a better name but it has been used in
380 * linux/filter.h.
381 */
382struct bpf_program {
bd054102 383 char *name;
52109584 384 char *sec_name;
c1122392 385 size_t sec_idx;
bd054102 386 const struct bpf_sec_def *sec_def;
c1122392
AN
387 /* this program's instruction offset (in number of instructions)
388 * within its containing ELF section
389 */
390 size_t sec_insn_off;
391 /* number of original instructions in ELF section belonging to this
392 * program, not taking into account subprogram instructions possible
393 * appended later during relocation
394 */
395 size_t sec_insn_cnt;
396 /* Offset (in number of instructions) of the start of instruction
397 * belonging to this BPF program within its containing main BPF
398 * program. For the entry-point (main) BPF program, this is always
399 * zero. For a sub-program, this gets reset before each of main BPF
400 * programs are processed and relocated and is used to determined
401 * whether sub-program was already appended to the main program, and
402 * if yes, at which instruction offset.
403 */
404 size_t sub_insn_off;
405
c1122392
AN
406 /* instructions that belong to BPF program; insns[0] is located at
407 * sec_insn_off instruction within its ELF section in ELF file, so
408 * when mapping ELF file instruction index to the local instruction,
409 * one needs to subtract sec_insn_off; and vice versa.
410 */
a5b8bd47 411 struct bpf_insn *insns;
c1122392
AN
412 /* actual number of instruction in this BPF program's image; for
413 * entry-point BPF programs this includes the size of main program
414 * itself plus all the used sub-programs, appended at the end
415 */
c3c55696 416 size_t insns_cnt;
34090915 417
166750bc 418 struct reloc_desc *reloc_desc;
34090915 419 int nr_reloc;
b3ce9079
AN
420
421 /* BPF verifier log settings */
422 char *log_buf;
423 size_t log_size;
424 __u32 log_level;
55cffde2 425
aa9b1ac3 426 struct bpf_object *obj;
d7be143b 427
cf90a20d 428 int fd;
a3820c48 429 bool autoload;
43cb8cba 430 bool autoattach;
aea28a60 431 bool mark_btf_static;
c1122392 432 enum bpf_prog_type type;
d7be143b 433 enum bpf_attach_type expected_attach_type;
cf90a20d 434
c1122392 435 int prog_ifindex;
91abb4a6 436 __u32 attach_btf_obj_fd;
12a8654b 437 __u32 attach_btf_id;
e7bf94db 438 __u32 attach_prog_fd;
cf90a20d 439
2993e051
YS
440 void *func_info;
441 __u32 func_info_rec_size;
f0187f0b 442 __u32 func_info_cnt;
47eff617 443
3d650141
MKL
444 void *line_info;
445 __u32 line_info_rec_size;
446 __u32 line_info_cnt;
04656198 447 __u32 prog_flags;
a5b8bd47
WN
448};
449
590a0088
MKL
450struct bpf_struct_ops {
451 const char *tname;
452 const struct btf_type *type;
453 struct bpf_program **progs;
454 __u32 *kern_func_off;
455 /* e.g. struct tcp_congestion_ops in bpf_prog's btf format */
456 void *data;
457 /* e.g. struct bpf_struct_ops_tcp_congestion_ops in
458 * btf_vmlinux's format.
459 * struct bpf_struct_ops_tcp_congestion_ops {
460 * [... some other kernel fields ...]
461 * struct tcp_congestion_ops data;
462 * }
463 * kern_vdata-size == sizeof(struct bpf_struct_ops_tcp_congestion_ops)
464 * bpf_map__init_kern_struct_ops() will populate the "kern_vdata"
465 * from "data".
466 */
467 void *kern_vdata;
468 __u32 type_id;
469};
470
ac9d1389
AN
471#define DATA_SEC ".data"
472#define BSS_SEC ".bss"
473#define RODATA_SEC ".rodata"
81bfdd08 474#define KCONFIG_SEC ".kconfig"
1c0c7074 475#define KSYMS_SEC ".ksyms"
590a0088 476#define STRUCT_OPS_SEC ".struct_ops"
809a69d6 477#define STRUCT_OPS_LINK_SEC ".struct_ops.link"
ac9d1389 478
d859900c
DB
479enum libbpf_map_type {
480 LIBBPF_MAP_UNSPEC,
481 LIBBPF_MAP_DATA,
482 LIBBPF_MAP_BSS,
483 LIBBPF_MAP_RODATA,
81bfdd08 484 LIBBPF_MAP_KCONFIG,
d859900c
DB
485};
486
146bf811
AN
487struct bpf_map_def {
488 unsigned int type;
489 unsigned int key_size;
490 unsigned int value_size;
491 unsigned int max_entries;
492 unsigned int map_flags;
493};
494
9d759a9b 495struct bpf_map {
ec41817b 496 struct bpf_object *obj;
561bbcca 497 char *name;
aed65917
AN
498 /* real_name is defined for special internal maps (.rodata*,
499 * .data*, .bss, .kconfig) and preserves their original ELF section
e3ba8e4e 500 * name. This is important to be able to find corresponding BTF
aed65917
AN
501 * DATASEC information.
502 */
503 char *real_name;
01af3bf0 504 int fd;
db48814b
AN
505 int sec_idx;
506 size_t sec_offset;
f0307a7e 507 int map_ifindex;
addb9fc9 508 int inner_map_fd;
9d759a9b 509 struct bpf_map_def def;
1bdb6c9a 510 __u32 numa_node;
646f02ff 511 __u32 btf_var_idx;
5b891af7
MKL
512 __u32 btf_key_type_id;
513 __u32 btf_value_type_id;
590a0088 514 __u32 btf_vmlinux_value_type_id;
d859900c 515 enum libbpf_map_type libbpf_type;
eba9c5f4 516 void *mmaped;
590a0088 517 struct bpf_struct_ops *st_ops;
646f02ff
AN
518 struct bpf_map *inner_map;
519 void **init_slots;
520 int init_slots_sz;
4580b25f
THJ
521 char *pin_path;
522 bool pinned;
ec6d5f47 523 bool reused;
ec41817b 524 bool autocreate;
47512102 525 __u64 map_extra;
d859900c
DB
526};
527
166750bc
AN
528enum extern_type {
529 EXT_UNKNOWN,
2e33efe3 530 EXT_KCFG,
1c0c7074 531 EXT_KSYM,
2e33efe3
AN
532};
533
534enum kcfg_type {
535 KCFG_UNKNOWN,
536 KCFG_CHAR,
537 KCFG_BOOL,
538 KCFG_INT,
539 KCFG_TRISTATE,
540 KCFG_CHAR_ARR,
166750bc
AN
541};
542
543struct extern_desc {
2e33efe3 544 enum extern_type type;
166750bc
AN
545 int sym_idx;
546 int btf_id;
2e33efe3
AN
547 int sec_btf_id;
548 const char *name;
166750bc 549 bool is_set;
2e33efe3
AN
550 bool is_weak;
551 union {
552 struct {
553 enum kcfg_type type;
554 int sz;
555 int align;
556 int data_off;
557 bool is_signed;
558 } kcfg;
1c0c7074
AN
559 struct {
560 unsigned long long addr;
d370bbe1
HL
561
562 /* target btf_id of the corresponding kernel var. */
284d2587
AN
563 int kernel_btf_obj_fd;
564 int kernel_btf_id;
d370bbe1
HL
565
566 /* local btf_id of the ksym extern's type. */
567 __u32 type_id;
9dbe6015
KKD
568 /* BTF fd index to be patched in for insn->off, this is
569 * 0 for vmlinux BTF, index in obj->fd_array for module
570 * BTF
571 */
572 __s16 btf_fd_idx;
1c0c7074 573 } ksym;
2e33efe3 574 };
166750bc
AN
575};
576
4f33a53d
AN
577struct module_btf {
578 struct btf *btf;
579 char *name;
580 __u32 id;
91abb4a6 581 int fd;
9dbe6015 582 int fd_array_idx;
4f33a53d
AN
583};
584
25bbbd7a
AN
585enum sec_type {
586 SEC_UNUSED = 0,
587 SEC_RELO,
588 SEC_BSS,
589 SEC_DATA,
590 SEC_RODATA,
591};
592
593struct elf_sec_desc {
594 enum sec_type sec_type;
595 Elf64_Shdr *shdr;
596 Elf_Data *data;
597};
598
29a30ff5
AN
599struct elf_state {
600 int fd;
601 const void *obj_buf;
602 size_t obj_buf_sz;
603 Elf *elf;
ad23b723 604 Elf64_Ehdr *ehdr;
29a30ff5 605 Elf_Data *symbols;
29a30ff5 606 Elf_Data *st_ops_data;
809a69d6 607 Elf_Data *st_ops_link_data;
29a30ff5
AN
608 size_t shstrndx; /* section index for section name strings */
609 size_t strtabidx;
25bbbd7a 610 struct elf_sec_desc *secs;
51deedc9 611 size_t sec_cnt;
29a30ff5
AN
612 int btf_maps_shndx;
613 __u32 btf_maps_sec_btf_id;
614 int text_shndx;
615 int symbols_shndx;
29a30ff5 616 int st_ops_shndx;
809a69d6 617 int st_ops_link_shndx;
4f33a53d
AN
618};
619
2e4913e0
AN
620struct usdt_manager;
621
1a5e3fb1 622struct bpf_object {
d859900c 623 char name[BPF_OBJ_NAME_LEN];
cb1e5e96 624 char license[64];
438363c0 625 __u32 kern_version;
0b3d1efa 626
a5b8bd47
WN
627 struct bpf_program *programs;
628 size_t nr_programs;
9d759a9b
WN
629 struct bpf_map *maps;
630 size_t nr_maps;
bf829271 631 size_t maps_cap;
9d759a9b 632
8601fd42 633 char *kconfig;
166750bc
AN
634 struct extern_desc *externs;
635 int nr_extern;
81bfdd08 636 int kconfig_map_idx;
166750bc 637
52d3352e 638 bool loaded;
c3c55696 639 bool has_subcalls;
25bbbd7a 640 bool has_rodata;
a5b8bd47 641
e2fa0156
AS
642 struct bpf_gen *gen_loader;
643
29a30ff5
AN
644 /* Information when doing ELF related work. Only valid if efile.elf is not NULL */
645 struct elf_state efile;
10931d24 646
8a138aed 647 struct btf *btf;
0f7515ca
AN
648 struct btf_ext *btf_ext;
649
a6ed02ca
KS
650 /* Parse and load BTF vmlinux if any of the programs in the object need
651 * it at load time.
652 */
653 struct btf *btf_vmlinux;
1373ff59
SC
654 /* Path to the custom BTF to be used for BPF CO-RE relocations as an
655 * override for vmlinux BTF.
656 */
657 char *btf_custom_path;
0f7515ca
AN
658 /* vmlinux BTF override for CO-RE relocations */
659 struct btf *btf_vmlinux_override;
4f33a53d
AN
660 /* Lazily initialized kernel module BTFs */
661 struct module_btf *btf_modules;
662 bool btf_modules_loaded;
663 size_t btf_module_cnt;
664 size_t btf_module_cap;
8a138aed 665
e0e3ea88
AN
666 /* optional log settings passed to BPF_BTF_LOAD and BPF_PROG_LOAD commands */
667 char *log_buf;
668 size_t log_size;
669 __u32 log_level;
670
9dbe6015
KKD
671 int *fd_array;
672 size_t fd_array_cap;
673 size_t fd_array_cnt;
674
2e4913e0
AN
675 struct usdt_manager *usdt_man;
676
1a5e3fb1
WN
677 char path[];
678};
1a5e3fb1 679
88a82120
AN
680static const char *elf_sym_str(const struct bpf_object *obj, size_t off);
681static const char *elf_sec_str(const struct bpf_object *obj, size_t off);
682static Elf_Scn *elf_sec_by_idx(const struct bpf_object *obj, size_t idx);
683static Elf_Scn *elf_sec_by_name(const struct bpf_object *obj, const char *name);
ad23b723 684static Elf64_Shdr *elf_sec_hdr(const struct bpf_object *obj, Elf_Scn *scn);
88a82120
AN
685static const char *elf_sec_name(const struct bpf_object *obj, Elf_Scn *scn);
686static Elf_Data *elf_sec_data(const struct bpf_object *obj, Elf_Scn *scn);
ad23b723
AN
687static Elf64_Sym *elf_sym_by_idx(const struct bpf_object *obj, size_t idx);
688static Elf64_Rel *elf_rel_by_idx(Elf_Data *data, size_t idx);
88a82120 689
29cd77f4 690void bpf_program__unload(struct bpf_program *prog)
55cffde2
WN
691{
692 if (!prog)
693 return;
694
cf90a20d 695 zclose(prog->fd);
2993e051 696
2993e051 697 zfree(&prog->func_info);
07a09d1b 698 zfree(&prog->line_info);
55cffde2
WN
699}
700
a5b8bd47
WN
701static void bpf_program__exit(struct bpf_program *prog)
702{
703 if (!prog)
704 return;
705
55cffde2 706 bpf_program__unload(prog);
88cda1c9 707 zfree(&prog->name);
52109584 708 zfree(&prog->sec_name);
a5b8bd47 709 zfree(&prog->insns);
34090915
WN
710 zfree(&prog->reloc_desc);
711
712 prog->nr_reloc = 0;
a5b8bd47 713 prog->insns_cnt = 0;
c1122392 714 prog->sec_idx = -1;
a5b8bd47
WN
715}
716
c3c55696
AN
717static bool insn_is_subprog_call(const struct bpf_insn *insn)
718{
719 return BPF_CLASS(insn->code) == BPF_JMP &&
720 BPF_OP(insn->code) == BPF_CALL &&
721 BPF_SRC(insn->code) == BPF_K &&
722 insn->src_reg == BPF_PSEUDO_CALL &&
723 insn->dst_reg == 0 &&
724 insn->off == 0;
725}
726
aa0b8d43
MKL
727static bool is_call_insn(const struct bpf_insn *insn)
728{
729 return insn->code == (BPF_JMP | BPF_CALL);
730}
731
53eddb5e
YS
732static bool insn_is_pseudo_func(struct bpf_insn *insn)
733{
aa0b8d43 734 return is_ldimm64_insn(insn) && insn->src_reg == BPF_PSEUDO_FUNC;
53eddb5e
YS
735}
736
a5b8bd47 737static int
c3c55696
AN
738bpf_object__init_prog(struct bpf_object *obj, struct bpf_program *prog,
739 const char *name, size_t sec_idx, const char *sec_name,
740 size_t sec_off, void *insn_data, size_t insn_data_sz)
a5b8bd47 741{
c1122392
AN
742 if (insn_data_sz == 0 || insn_data_sz % BPF_INSN_SZ || sec_off % BPF_INSN_SZ) {
743 pr_warn("sec '%s': corrupted program '%s', offset %zu, size %zu\n",
744 sec_name, name, sec_off, insn_data_sz);
a5b8bd47
WN
745 return -EINVAL;
746 }
747
1ad9cbb8 748 memset(prog, 0, sizeof(*prog));
c3c55696
AN
749 prog->obj = obj;
750
c1122392
AN
751 prog->sec_idx = sec_idx;
752 prog->sec_insn_off = sec_off / BPF_INSN_SZ;
753 prog->sec_insn_cnt = insn_data_sz / BPF_INSN_SZ;
754 /* insns_cnt can later be increased by appending used subprograms */
755 prog->insns_cnt = prog->sec_insn_cnt;
a5b8bd47 756
c1122392 757 prog->type = BPF_PROG_TYPE_UNSPEC;
cf90a20d 758 prog->fd = -1;
a3820c48
AN
759
760 /* libbpf's convention for SEC("?abc...") is that it's just like
761 * SEC("abc...") but the corresponding bpf_program starts out with
762 * autoload set to false.
763 */
764 if (sec_name[0] == '?') {
765 prog->autoload = false;
766 /* from now on forget there was ? in section name */
767 sec_name++;
768 } else {
769 prog->autoload = true;
770 }
a5b8bd47 771
43cb8cba
HL
772 prog->autoattach = true;
773
b3ce9079
AN
774 /* inherit object's log_level */
775 prog->log_level = obj->log_level;
776
52109584
AN
777 prog->sec_name = strdup(sec_name);
778 if (!prog->sec_name)
c1122392
AN
779 goto errout;
780
781 prog->name = strdup(name);
782 if (!prog->name)
a5b8bd47 783 goto errout;
a5b8bd47 784
c1122392
AN
785 prog->insns = malloc(insn_data_sz);
786 if (!prog->insns)
a5b8bd47 787 goto errout;
c1122392 788 memcpy(prog->insns, insn_data, insn_data_sz);
a5b8bd47
WN
789
790 return 0;
791errout:
c1122392 792 pr_warn("sec '%s': failed to allocate memory for prog '%s'\n", sec_name, name);
a5b8bd47
WN
793 bpf_program__exit(prog);
794 return -ENOMEM;
795}
796
797static int
c1122392
AN
798bpf_object__add_programs(struct bpf_object *obj, Elf_Data *sec_data,
799 const char *sec_name, int sec_idx)
a5b8bd47 800{
6245947c 801 Elf_Data *symbols = obj->efile.symbols;
c1122392
AN
802 struct bpf_program *prog, *progs;
803 void *data = sec_data->d_buf;
6245947c
AN
804 size_t sec_sz = sec_data->d_size, sec_off, prog_sz, nr_syms;
805 int nr_progs, err, i;
c1122392 806 const char *name;
ad23b723 807 Elf64_Sym *sym;
a5b8bd47
WN
808
809 progs = obj->programs;
810 nr_progs = obj->nr_programs;
ad23b723 811 nr_syms = symbols->d_size / sizeof(Elf64_Sym);
a5b8bd47 812
6245947c 813 for (i = 0; i < nr_syms; i++) {
ad23b723
AN
814 sym = elf_sym_by_idx(obj, i);
815
816 if (sym->st_shndx != sec_idx)
6245947c 817 continue;
ad23b723 818 if (ELF64_ST_TYPE(sym->st_info) != STT_FUNC)
6245947c 819 continue;
88cda1c9 820
ad23b723
AN
821 prog_sz = sym->st_size;
822 sec_off = sym->st_value;
88cda1c9 823
ad23b723 824 name = elf_sym_str(obj, sym->st_name);
c1122392
AN
825 if (!name) {
826 pr_warn("sec '%s': failed to get symbol name for offset %zu\n",
827 sec_name, sec_off);
828 return -LIBBPF_ERRNO__FORMAT;
829 }
88cda1c9 830
c1122392
AN
831 if (sec_off + prog_sz > sec_sz) {
832 pr_warn("sec '%s': program at offset %zu crosses section boundary\n",
833 sec_name, sec_off);
834 return -LIBBPF_ERRNO__FORMAT;
835 }
88cda1c9 836
ad23b723 837 if (sec_idx != obj->efile.text_shndx && ELF64_ST_BIND(sym->st_info) == STB_LOCAL) {
513f485c
AN
838 pr_warn("sec '%s': program '%s' is static and not supported\n", sec_name, name);
839 return -ENOTSUP;
840 }
841
c3c55696
AN
842 pr_debug("sec '%s': found program '%s' at insn offset %zu (%zu bytes), code size %zu insns (%zu bytes)\n",
843 sec_name, name, sec_off / BPF_INSN_SZ, sec_off, prog_sz / BPF_INSN_SZ, prog_sz);
88cda1c9 844
c3c55696 845 progs = libbpf_reallocarray(progs, nr_progs + 1, sizeof(*progs));
c1122392
AN
846 if (!progs) {
847 /*
848 * In this case the original obj->programs
849 * is still valid, so don't need special treat for
850 * bpf_close_object().
851 */
852 pr_warn("sec '%s': failed to alloc memory for new program '%s'\n",
853 sec_name, name);
854 return -ENOMEM;
88cda1c9 855 }
c1122392 856 obj->programs = progs;
88cda1c9 857
c1122392 858 prog = &progs[nr_progs];
9a94f277 859
c3c55696
AN
860 err = bpf_object__init_prog(obj, prog, name, sec_idx, sec_name,
861 sec_off, data + sec_off, prog_sz);
c1122392
AN
862 if (err)
863 return err;
9a94f277 864
e5670fa0
AN
865 /* if function is a global/weak symbol, but has restricted
866 * (STV_HIDDEN or STV_INTERNAL) visibility, mark its BTF FUNC
867 * as static to enable more permissive BPF verification mode
868 * with more outside context available to BPF verifier
aea28a60 869 */
ad23b723
AN
870 if (ELF64_ST_BIND(sym->st_info) != STB_LOCAL
871 && (ELF64_ST_VISIBILITY(sym->st_other) == STV_HIDDEN
872 || ELF64_ST_VISIBILITY(sym->st_other) == STV_INTERNAL))
aea28a60
AN
873 prog->mark_btf_static = true;
874
c1122392
AN
875 nr_progs++;
876 obj->nr_programs = nr_progs;
88cda1c9
MKL
877 }
878
879 return 0;
880}
881
590a0088
MKL
882static const struct btf_member *
883find_member_by_offset(const struct btf_type *t, __u32 bit_offset)
884{
885 struct btf_member *m;
886 int i;
887
888 for (i = 0, m = btf_members(t); i < btf_vlen(t); i++, m++) {
889 if (btf_member_bit_offset(t, i) == bit_offset)
890 return m;
891 }
892
893 return NULL;
894}
895
896static const struct btf_member *
897find_member_by_name(const struct btf *btf, const struct btf_type *t,
898 const char *name)
899{
900 struct btf_member *m;
901 int i;
902
903 for (i = 0, m = btf_members(t); i < btf_vlen(t); i++, m++) {
904 if (!strcmp(btf__name_by_offset(btf, m->name_off), name))
905 return m;
906 }
907
908 return NULL;
909}
910
911#define STRUCT_OPS_VALUE_PREFIX "bpf_struct_ops_"
a6ed02ca
KS
912static int find_btf_by_prefix_kind(const struct btf *btf, const char *prefix,
913 const char *name, __u32 kind);
590a0088
MKL
914
915static int
916find_struct_ops_kern_types(const struct btf *btf, const char *tname,
917 const struct btf_type **type, __u32 *type_id,
918 const struct btf_type **vtype, __u32 *vtype_id,
919 const struct btf_member **data_member)
920{
921 const struct btf_type *kern_type, *kern_vtype;
922 const struct btf_member *kern_data_member;
923 __s32 kern_vtype_id, kern_type_id;
590a0088
MKL
924 __u32 i;
925
926 kern_type_id = btf__find_by_name_kind(btf, tname, BTF_KIND_STRUCT);
927 if (kern_type_id < 0) {
928 pr_warn("struct_ops init_kern: struct %s is not found in kernel BTF\n",
929 tname);
930 return kern_type_id;
931 }
932 kern_type = btf__type_by_id(btf, kern_type_id);
933
934 /* Find the corresponding "map_value" type that will be used
935 * in map_update(BPF_MAP_TYPE_STRUCT_OPS). For example,
936 * find "struct bpf_struct_ops_tcp_congestion_ops" from the
937 * btf_vmlinux.
938 */
a6ed02ca
KS
939 kern_vtype_id = find_btf_by_prefix_kind(btf, STRUCT_OPS_VALUE_PREFIX,
940 tname, BTF_KIND_STRUCT);
590a0088 941 if (kern_vtype_id < 0) {
a6ed02ca
KS
942 pr_warn("struct_ops init_kern: struct %s%s is not found in kernel BTF\n",
943 STRUCT_OPS_VALUE_PREFIX, tname);
590a0088
MKL
944 return kern_vtype_id;
945 }
946 kern_vtype = btf__type_by_id(btf, kern_vtype_id);
947
948 /* Find "struct tcp_congestion_ops" from
949 * struct bpf_struct_ops_tcp_congestion_ops {
950 * [ ... ]
951 * struct tcp_congestion_ops data;
952 * }
953 */
954 kern_data_member = btf_members(kern_vtype);
955 for (i = 0; i < btf_vlen(kern_vtype); i++, kern_data_member++) {
956 if (kern_data_member->type == kern_type_id)
957 break;
958 }
959 if (i == btf_vlen(kern_vtype)) {
a6ed02ca
KS
960 pr_warn("struct_ops init_kern: struct %s data is not found in struct %s%s\n",
961 tname, STRUCT_OPS_VALUE_PREFIX, tname);
590a0088
MKL
962 return -EINVAL;
963 }
964
965 *type = kern_type;
966 *type_id = kern_type_id;
967 *vtype = kern_vtype;
968 *vtype_id = kern_vtype_id;
969 *data_member = kern_data_member;
970
971 return 0;
972}
973
974static bool bpf_map__is_struct_ops(const struct bpf_map *map)
975{
976 return map->def.type == BPF_MAP_TYPE_STRUCT_OPS;
977}
978
979/* Init the map's fields that depend on kern_btf */
980static int bpf_map__init_kern_struct_ops(struct bpf_map *map,
981 const struct btf *btf,
982 const struct btf *kern_btf)
983{
984 const struct btf_member *member, *kern_member, *kern_data_member;
985 const struct btf_type *type, *kern_type, *kern_vtype;
986 __u32 i, kern_type_id, kern_vtype_id, kern_data_off;
987 struct bpf_struct_ops *st_ops;
988 void *data, *kern_data;
989 const char *tname;
990 int err;
991
992 st_ops = map->st_ops;
993 type = st_ops->type;
994 tname = st_ops->tname;
995 err = find_struct_ops_kern_types(kern_btf, tname,
996 &kern_type, &kern_type_id,
997 &kern_vtype, &kern_vtype_id,
998 &kern_data_member);
999 if (err)
1000 return err;
1001
1002 pr_debug("struct_ops init_kern %s: type_id:%u kern_type_id:%u kern_vtype_id:%u\n",
1003 map->name, st_ops->type_id, kern_type_id, kern_vtype_id);
1004
1005 map->def.value_size = kern_vtype->size;
1006 map->btf_vmlinux_value_type_id = kern_vtype_id;
1007
1008 st_ops->kern_vdata = calloc(1, kern_vtype->size);
1009 if (!st_ops->kern_vdata)
1010 return -ENOMEM;
1011
1012 data = st_ops->data;
1013 kern_data_off = kern_data_member->offset / 8;
1014 kern_data = st_ops->kern_vdata + kern_data_off;
1015
1016 member = btf_members(type);
1017 for (i = 0; i < btf_vlen(type); i++, member++) {
1018 const struct btf_type *mtype, *kern_mtype;
1019 __u32 mtype_id, kern_mtype_id;
1020 void *mdata, *kern_mdata;
1021 __s64 msize, kern_msize;
1022 __u32 moff, kern_moff;
1023 __u32 kern_member_idx;
1024 const char *mname;
1025
1026 mname = btf__name_by_offset(btf, member->name_off);
1027 kern_member = find_member_by_name(kern_btf, kern_type, mname);
1028 if (!kern_member) {
1029 pr_warn("struct_ops init_kern %s: Cannot find member %s in kernel BTF\n",
1030 map->name, mname);
1031 return -ENOTSUP;
1032 }
1033
1034 kern_member_idx = kern_member - btf_members(kern_type);
1035 if (btf_member_bitfield_size(type, i) ||
1036 btf_member_bitfield_size(kern_type, kern_member_idx)) {
1037 pr_warn("struct_ops init_kern %s: bitfield %s is not supported\n",
1038 map->name, mname);
1039 return -ENOTSUP;
1040 }
1041
1042 moff = member->offset / 8;
1043 kern_moff = kern_member->offset / 8;
1044
1045 mdata = data + moff;
1046 kern_mdata = kern_data + kern_moff;
1047
1048 mtype = skip_mods_and_typedefs(btf, member->type, &mtype_id);
1049 kern_mtype = skip_mods_and_typedefs(kern_btf, kern_member->type,
1050 &kern_mtype_id);
1051 if (BTF_INFO_KIND(mtype->info) !=
1052 BTF_INFO_KIND(kern_mtype->info)) {
1053 pr_warn("struct_ops init_kern %s: Unmatched member type %s %u != %u(kernel)\n",
1054 map->name, mname, BTF_INFO_KIND(mtype->info),
1055 BTF_INFO_KIND(kern_mtype->info));
1056 return -ENOTSUP;
1057 }
1058
1059 if (btf_is_ptr(mtype)) {
1060 struct bpf_program *prog;
1061
d2836ddd
MKL
1062 prog = st_ops->progs[i];
1063 if (!prog)
1064 continue;
1065
590a0088
MKL
1066 kern_mtype = skip_mods_and_typedefs(kern_btf,
1067 kern_mtype->type,
1068 &kern_mtype_id);
d2836ddd
MKL
1069
1070 /* mtype->type must be a func_proto which was
1071 * guaranteed in bpf_object__collect_st_ops_relos(),
1072 * so only check kern_mtype for func_proto here.
1073 */
1074 if (!btf_is_func_proto(kern_mtype)) {
1075 pr_warn("struct_ops init_kern %s: kernel member %s is not a func ptr\n",
590a0088
MKL
1076 map->name, mname);
1077 return -ENOTSUP;
1078 }
1079
590a0088
MKL
1080 prog->attach_btf_id = kern_type_id;
1081 prog->expected_attach_type = kern_member_idx;
1082
1083 st_ops->kern_func_off[i] = kern_data_off + kern_moff;
1084
1085 pr_debug("struct_ops init_kern %s: func ptr %s is set to prog %s from data(+%u) to kern_data(+%u)\n",
1086 map->name, mname, prog->name, moff,
1087 kern_moff);
1088
1089 continue;
1090 }
1091
1092 msize = btf__resolve_size(btf, mtype_id);
1093 kern_msize = btf__resolve_size(kern_btf, kern_mtype_id);
1094 if (msize < 0 || kern_msize < 0 || msize != kern_msize) {
1095 pr_warn("struct_ops init_kern %s: Error in size of member %s: %zd != %zd(kernel)\n",
1096 map->name, mname, (ssize_t)msize,
1097 (ssize_t)kern_msize);
1098 return -ENOTSUP;
1099 }
1100
1101 pr_debug("struct_ops init_kern %s: copy %s %u bytes from data(+%u) to kern_data(+%u)\n",
1102 map->name, mname, (unsigned int)msize,
1103 moff, kern_moff);
1104 memcpy(kern_mdata, mdata, msize);
1105 }
1106
1107 return 0;
1108}
1109
1110static int bpf_object__init_kern_struct_ops_maps(struct bpf_object *obj)
1111{
590a0088
MKL
1112 struct bpf_map *map;
1113 size_t i;
1114 int err;
1115
1116 for (i = 0; i < obj->nr_maps; i++) {
1117 map = &obj->maps[i];
1118
1119 if (!bpf_map__is_struct_ops(map))
1120 continue;
1121
a6ed02ca
KS
1122 err = bpf_map__init_kern_struct_ops(map, obj->btf,
1123 obj->btf_vmlinux);
1124 if (err)
590a0088 1125 return err;
590a0088
MKL
1126 }
1127
590a0088
MKL
1128 return 0;
1129}
1130
809a69d6
KFL
1131static int init_struct_ops_maps(struct bpf_object *obj, const char *sec_name,
1132 int shndx, Elf_Data *data, __u32 map_flags)
590a0088
MKL
1133{
1134 const struct btf_type *type, *datasec;
1135 const struct btf_var_secinfo *vsi;
1136 struct bpf_struct_ops *st_ops;
1137 const char *tname, *var_name;
1138 __s32 type_id, datasec_id;
1139 const struct btf *btf;
1140 struct bpf_map *map;
1141 __u32 i;
1142
809a69d6 1143 if (shndx == -1)
590a0088
MKL
1144 return 0;
1145
1146 btf = obj->btf;
809a69d6 1147 datasec_id = btf__find_by_name_kind(btf, sec_name,
590a0088
MKL
1148 BTF_KIND_DATASEC);
1149 if (datasec_id < 0) {
1150 pr_warn("struct_ops init: DATASEC %s not found\n",
809a69d6 1151 sec_name);
590a0088
MKL
1152 return -EINVAL;
1153 }
1154
1155 datasec = btf__type_by_id(btf, datasec_id);
1156 vsi = btf_var_secinfos(datasec);
1157 for (i = 0; i < btf_vlen(datasec); i++, vsi++) {
1158 type = btf__type_by_id(obj->btf, vsi->type);
1159 var_name = btf__name_by_offset(obj->btf, type->name_off);
1160
1161 type_id = btf__resolve_type(obj->btf, vsi->type);
1162 if (type_id < 0) {
1163 pr_warn("struct_ops init: Cannot resolve var type_id %u in DATASEC %s\n",
809a69d6 1164 vsi->type, sec_name);
590a0088
MKL
1165 return -EINVAL;
1166 }
1167
1168 type = btf__type_by_id(obj->btf, type_id);
1169 tname = btf__name_by_offset(obj->btf, type->name_off);
1170 if (!tname[0]) {
1171 pr_warn("struct_ops init: anonymous type is not supported\n");
1172 return -ENOTSUP;
1173 }
1174 if (!btf_is_struct(type)) {
1175 pr_warn("struct_ops init: %s is not a struct\n", tname);
1176 return -EINVAL;
1177 }
1178
1179 map = bpf_object__add_map(obj);
1180 if (IS_ERR(map))
1181 return PTR_ERR(map);
1182
809a69d6 1183 map->sec_idx = shndx;
590a0088
MKL
1184 map->sec_offset = vsi->offset;
1185 map->name = strdup(var_name);
1186 if (!map->name)
1187 return -ENOMEM;
1188
1189 map->def.type = BPF_MAP_TYPE_STRUCT_OPS;
1190 map->def.key_size = sizeof(int);
1191 map->def.value_size = type->size;
1192 map->def.max_entries = 1;
809a69d6 1193 map->def.map_flags = map_flags;
590a0088
MKL
1194
1195 map->st_ops = calloc(1, sizeof(*map->st_ops));
1196 if (!map->st_ops)
1197 return -ENOMEM;
1198 st_ops = map->st_ops;
1199 st_ops->data = malloc(type->size);
1200 st_ops->progs = calloc(btf_vlen(type), sizeof(*st_ops->progs));
1201 st_ops->kern_func_off = malloc(btf_vlen(type) *
1202 sizeof(*st_ops->kern_func_off));
1203 if (!st_ops->data || !st_ops->progs || !st_ops->kern_func_off)
1204 return -ENOMEM;
1205
809a69d6 1206 if (vsi->offset + type->size > data->d_size) {
590a0088 1207 pr_warn("struct_ops init: var %s is beyond the end of DATASEC %s\n",
809a69d6 1208 var_name, sec_name);
590a0088
MKL
1209 return -EINVAL;
1210 }
1211
1212 memcpy(st_ops->data,
809a69d6 1213 data->d_buf + vsi->offset,
590a0088
MKL
1214 type->size);
1215 st_ops->tname = tname;
1216 st_ops->type = type;
1217 st_ops->type_id = type_id;
1218
1219 pr_debug("struct_ops init: struct %s(type_id=%u) %s found at offset %u\n",
1220 tname, type_id, var_name, vsi->offset);
1221 }
1222
1223 return 0;
1224}
1225
809a69d6
KFL
1226static int bpf_object_init_struct_ops(struct bpf_object *obj)
1227{
1228 int err;
1229
1230 err = init_struct_ops_maps(obj, STRUCT_OPS_SEC, obj->efile.st_ops_shndx,
1231 obj->efile.st_ops_data, 0);
1232 err = err ?: init_struct_ops_maps(obj, STRUCT_OPS_LINK_SEC,
1233 obj->efile.st_ops_link_shndx,
1234 obj->efile.st_ops_link_data,
1235 BPF_F_LINK);
1236 return err;
1237}
1238
6c956392 1239static struct bpf_object *bpf_object__new(const char *path,
5e61f270 1240 const void *obj_buf,
2ce8450e
AN
1241 size_t obj_buf_sz,
1242 const char *obj_name)
1a5e3fb1
WN
1243{
1244 struct bpf_object *obj;
d859900c 1245 char *end;
1a5e3fb1
WN
1246
1247 obj = calloc(1, sizeof(struct bpf_object) + strlen(path) + 1);
1248 if (!obj) {
be18010e 1249 pr_warn("alloc memory failed for %s\n", path);
6371ca3b 1250 return ERR_PTR(-ENOMEM);
1a5e3fb1
WN
1251 }
1252
1253 strcpy(obj->path, path);
2ce8450e 1254 if (obj_name) {
9fc205b4 1255 libbpf_strlcpy(obj->name, obj_name, sizeof(obj->name));
2ce8450e
AN
1256 } else {
1257 /* Using basename() GNU version which doesn't modify arg. */
9fc205b4 1258 libbpf_strlcpy(obj->name, basename((void *)path), sizeof(obj->name));
2ce8450e
AN
1259 end = strchr(obj->name, '.');
1260 if (end)
1261 *end = 0;
1262 }
6c956392 1263
d859900c 1264 obj->efile.fd = -1;
6c956392 1265 /*
76e1022b 1266 * Caller of this function should also call
6c956392
WN
1267 * bpf_object__elf_finish() after data collection to return
1268 * obj_buf to user. If not, we should duplicate the buffer to
1269 * avoid user freeing them before elf finish.
1270 */
1271 obj->efile.obj_buf = obj_buf;
1272 obj->efile.obj_buf_sz = obj_buf_sz;
abd29c93 1273 obj->efile.btf_maps_shndx = -1;
590a0088 1274 obj->efile.st_ops_shndx = -1;
809a69d6 1275 obj->efile.st_ops_link_shndx = -1;
81bfdd08 1276 obj->kconfig_map_idx = -1;
6c956392 1277
5e61f270 1278 obj->kern_version = get_kernel_version();
52d3352e 1279 obj->loaded = false;
9a208eff 1280
1a5e3fb1
WN
1281 return obj;
1282}
1283
1284static void bpf_object__elf_finish(struct bpf_object *obj)
1285{
29a30ff5 1286 if (!obj->efile.elf)
1a5e3fb1
WN
1287 return;
1288
b71a2ebf
GC
1289 elf_end(obj->efile.elf);
1290 obj->efile.elf = NULL;
bec7d68c 1291 obj->efile.symbols = NULL;
590a0088 1292 obj->efile.st_ops_data = NULL;
809a69d6 1293 obj->efile.st_ops_link_data = NULL;
b62f06e8 1294
25bbbd7a
AN
1295 zfree(&obj->efile.secs);
1296 obj->efile.sec_cnt = 0;
1a5e3fb1 1297 zclose(obj->efile.fd);
6c956392
WN
1298 obj->efile.obj_buf = NULL;
1299 obj->efile.obj_buf_sz = 0;
1a5e3fb1
WN
1300}
1301
1302static int bpf_object__elf_init(struct bpf_object *obj)
1303{
ad23b723 1304 Elf64_Ehdr *ehdr;
1a5e3fb1 1305 int err = 0;
ad23b723 1306 Elf *elf;
1a5e3fb1 1307
29a30ff5 1308 if (obj->efile.elf) {
88a82120 1309 pr_warn("elf: init internal error\n");
6371ca3b 1310 return -LIBBPF_ERRNO__LIBELF;
1a5e3fb1
WN
1311 }
1312
6c956392 1313 if (obj->efile.obj_buf_sz > 0) {
146bf811 1314 /* obj_buf should have been validated by bpf_object__open_mem(). */
ad23b723 1315 elf = elf_memory((char *)obj->efile.obj_buf, obj->efile.obj_buf_sz);
6c956392 1316 } else {
92274e24 1317 obj->efile.fd = open(obj->path, O_RDONLY | O_CLOEXEC);
6c956392 1318 if (obj->efile.fd < 0) {
be5c5d4e 1319 char errmsg[STRERR_BUFSIZE], *cp;
1ce6a9fc 1320
be5c5d4e
AN
1321 err = -errno;
1322 cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg));
88a82120 1323 pr_warn("elf: failed to open %s: %s\n", obj->path, cp);
be5c5d4e 1324 return err;
6c956392
WN
1325 }
1326
ad23b723 1327 elf = elf_begin(obj->efile.fd, ELF_C_READ_MMAP, NULL);
1a5e3fb1
WN
1328 }
1329
ad23b723 1330 if (!elf) {
88a82120 1331 pr_warn("elf: failed to open %s as ELF file: %s\n", obj->path, elf_errmsg(-1));
6371ca3b 1332 err = -LIBBPF_ERRNO__LIBELF;
1a5e3fb1
WN
1333 goto errout;
1334 }
1335
ad23b723
AN
1336 obj->efile.elf = elf;
1337
1338 if (elf_kind(elf) != ELF_K_ELF) {
1339 err = -LIBBPF_ERRNO__FORMAT;
1340 pr_warn("elf: '%s' is not a proper ELF object\n", obj->path);
1341 goto errout;
1342 }
1343
1344 if (gelf_getclass(elf) != ELFCLASS64) {
1345 err = -LIBBPF_ERRNO__FORMAT;
1346 pr_warn("elf: '%s' is not a 64-bit ELF object\n", obj->path);
1347 goto errout;
1348 }
1349
1350 obj->efile.ehdr = ehdr = elf64_getehdr(elf);
1351 if (!obj->efile.ehdr) {
88a82120 1352 pr_warn("elf: failed to get ELF header from %s: %s\n", obj->path, elf_errmsg(-1));
6371ca3b 1353 err = -LIBBPF_ERRNO__FORMAT;
1a5e3fb1
WN
1354 goto errout;
1355 }
1a5e3fb1 1356
ad23b723 1357 if (elf_getshdrstrndx(elf, &obj->efile.shstrndx)) {
88a82120
AN
1358 pr_warn("elf: failed to get section names section index for %s: %s\n",
1359 obj->path, elf_errmsg(-1));
1360 err = -LIBBPF_ERRNO__FORMAT;
1361 goto errout;
1362 }
1363
70e79866 1364 /* ELF is corrupted/truncated, avoid calling elf_strptr. */
ad23b723 1365 if (!elf_rawdata(elf_getscn(elf, obj->efile.shstrndx), NULL)) {
88a82120
AN
1366 pr_warn("elf: failed to get section names strings from %s: %s\n",
1367 obj->path, elf_errmsg(-1));
8f3f5792
NK
1368 err = -LIBBPF_ERRNO__FORMAT;
1369 goto errout;
88a82120
AN
1370 }
1371
9b16137a 1372 /* Old LLVM set e_machine to EM_NONE */
ad23b723 1373 if (ehdr->e_type != ET_REL || (ehdr->e_machine && ehdr->e_machine != EM_BPF)) {
88a82120 1374 pr_warn("elf: %s is not a valid eBPF object file\n", obj->path);
6371ca3b 1375 err = -LIBBPF_ERRNO__FORMAT;
1a5e3fb1
WN
1376 goto errout;
1377 }
1378
1379 return 0;
1380errout:
1381 bpf_object__elf_finish(obj);
1382 return err;
1383}
1384
12ef5634 1385static int bpf_object__check_endianness(struct bpf_object *obj)
cc4228d5 1386{
3930198d 1387#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
ad23b723 1388 if (obj->efile.ehdr->e_ident[EI_DATA] == ELFDATA2LSB)
12ef5634 1389 return 0;
3930198d 1390#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
ad23b723 1391 if (obj->efile.ehdr->e_ident[EI_DATA] == ELFDATA2MSB)
12ef5634
AN
1392 return 0;
1393#else
1394# error "Unrecognized __BYTE_ORDER__"
1395#endif
88a82120 1396 pr_warn("elf: endianness mismatch in %s.\n", obj->path);
6371ca3b 1397 return -LIBBPF_ERRNO__ENDIAN;
cc4228d5
WN
1398}
1399
cb1e5e96 1400static int
399dc65e 1401bpf_object__init_license(struct bpf_object *obj, void *data, size_t size)
cb1e5e96 1402{
35a85550
SHY
1403 if (!data) {
1404 pr_warn("invalid license section in %s\n", obj->path);
1405 return -LIBBPF_ERRNO__FORMAT;
1406 }
f9798239
AN
1407 /* libbpf_strlcpy() only copies first N - 1 bytes, so size + 1 won't
1408 * go over allowed ELF data section buffer
1409 */
1410 libbpf_strlcpy(obj->license, data, min(size + 1, sizeof(obj->license)));
cb1e5e96
WN
1411 pr_debug("license of %s is %s\n", obj->path, obj->license);
1412 return 0;
1413}
1414
54b8625c
JF
1415static int
1416bpf_object__init_kversion(struct bpf_object *obj, void *data, size_t size)
1417{
1418 __u32 kver;
1419
35a85550 1420 if (!data || size != sizeof(kver)) {
be18010e 1421 pr_warn("invalid kver section in %s\n", obj->path);
54b8625c
JF
1422 return -LIBBPF_ERRNO__FORMAT;
1423 }
1424 memcpy(&kver, data, sizeof(kver));
1425 obj->kern_version = kver;
1426 pr_debug("kernel version of %s is %x\n", obj->path, obj->kern_version);
1427 return 0;
1428}
1429
addb9fc9
NS
1430static bool bpf_map_type__is_map_in_map(enum bpf_map_type type)
1431{
1432 if (type == BPF_MAP_TYPE_ARRAY_OF_MAPS ||
1433 type == BPF_MAP_TYPE_HASH_OF_MAPS)
1434 return true;
1435 return false;
1436}
1437
b96c07f3 1438static int find_elf_sec_sz(const struct bpf_object *obj, const char *name, __u32 *size)
1713d68b 1439{
25bbbd7a
AN
1440 Elf_Data *data;
1441 Elf_Scn *scn;
1713d68b 1442
25bbbd7a 1443 if (!name)
1713d68b 1444 return -EINVAL;
88a82120 1445
25bbbd7a
AN
1446 scn = elf_sec_by_name(obj, name);
1447 data = elf_sec_data(obj, scn);
1448 if (data) {
25bbbd7a 1449 *size = data->d_size;
08894d9c 1450 return 0; /* found it */
1713d68b
DB
1451 }
1452
08894d9c 1453 return -ENOENT;
1713d68b
DB
1454}
1455
f33f742d 1456static Elf64_Sym *find_elf_var_sym(const struct bpf_object *obj, const char *name)
1713d68b
DB
1457{
1458 Elf_Data *symbols = obj->efile.symbols;
1459 const char *sname;
1460 size_t si;
1461
ad23b723
AN
1462 for (si = 0; si < symbols->d_size / sizeof(Elf64_Sym); si++) {
1463 Elf64_Sym *sym = elf_sym_by_idx(obj, si);
1713d68b 1464
3a06ec0a
AN
1465 if (ELF64_ST_TYPE(sym->st_info) != STT_OBJECT)
1466 continue;
1467
1468 if (ELF64_ST_BIND(sym->st_info) != STB_GLOBAL &&
1469 ELF64_ST_BIND(sym->st_info) != STB_WEAK)
1713d68b
DB
1470 continue;
1471
ad23b723 1472 sname = elf_sym_str(obj, sym->st_name);
1713d68b 1473 if (!sname) {
ad23b723 1474 pr_warn("failed to get sym name string for var %s\n", name);
f33f742d 1475 return ERR_PTR(-EIO);
1713d68b 1476 }
f33f742d
AN
1477 if (strcmp(name, sname) == 0)
1478 return sym;
1713d68b
DB
1479 }
1480
f33f742d 1481 return ERR_PTR(-ENOENT);
1713d68b
DB
1482}
1483
bf829271 1484static struct bpf_map *bpf_object__add_map(struct bpf_object *obj)
d859900c 1485{
69721203
AN
1486 struct bpf_map *map;
1487 int err;
bf829271 1488
69721203
AN
1489 err = libbpf_ensure_mem((void **)&obj->maps, &obj->maps_cap,
1490 sizeof(*obj->maps), obj->nr_maps + 1);
1491 if (err)
1492 return ERR_PTR(err);
bf829271 1493
69721203 1494 map = &obj->maps[obj->nr_maps++];
ec41817b 1495 map->obj = obj;
69721203
AN
1496 map->fd = -1;
1497 map->inner_map_fd = -1;
ec41817b 1498 map->autocreate = true;
bf829271 1499
69721203 1500 return map;
d859900c
DB
1501}
1502
9d0a2331 1503static size_t bpf_map_mmap_sz(unsigned int value_sz, unsigned int max_entries)
eba9c5f4 1504{
9d0a2331 1505 const long page_sz = sysconf(_SC_PAGE_SIZE);
eba9c5f4
AN
1506 size_t map_sz;
1507
9d0a2331 1508 map_sz = (size_t)roundup(value_sz, 8) * max_entries;
eba9c5f4
AN
1509 map_sz = roundup(map_sz, page_sz);
1510 return map_sz;
1511}
1512
9d0a2331
JK
1513static int bpf_map_mmap_resize(struct bpf_map *map, size_t old_sz, size_t new_sz)
1514{
1515 void *mmaped;
1516
1517 if (!map->mmaped)
1518 return -EINVAL;
1519
1520 if (old_sz == new_sz)
1521 return 0;
1522
1523 mmaped = mmap(NULL, new_sz, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
1524 if (mmaped == MAP_FAILED)
1525 return -errno;
1526
1527 memcpy(mmaped, map->mmaped, min(old_sz, new_sz));
1528 munmap(map->mmaped, old_sz);
1529 map->mmaped = mmaped;
1530 return 0;
1531}
1532
aed65917 1533static char *internal_map_name(struct bpf_object *obj, const char *real_name)
81bfdd08 1534{
113e6b7e 1535 char map_name[BPF_OBJ_NAME_LEN], *p;
aed65917
AN
1536 int pfx_len, sfx_len = max((size_t)7, strlen(real_name));
1537
1538 /* This is one of the more confusing parts of libbpf for various
1539 * reasons, some of which are historical. The original idea for naming
1540 * internal names was to include as much of BPF object name prefix as
1541 * possible, so that it can be distinguished from similar internal
1542 * maps of a different BPF object.
1543 * As an example, let's say we have bpf_object named 'my_object_name'
1544 * and internal map corresponding to '.rodata' ELF section. The final
1545 * map name advertised to user and to the kernel will be
1546 * 'my_objec.rodata', taking first 8 characters of object name and
1547 * entire 7 characters of '.rodata'.
1548 * Somewhat confusingly, if internal map ELF section name is shorter
1549 * than 7 characters, e.g., '.bss', we still reserve 7 characters
1550 * for the suffix, even though we only have 4 actual characters, and
1551 * resulting map will be called 'my_objec.bss', not even using all 15
1552 * characters allowed by the kernel. Oh well, at least the truncated
1553 * object name is somewhat consistent in this case. But if the map
1554 * name is '.kconfig', we'll still have entirety of '.kconfig' added
1555 * (8 chars) and thus will be left with only first 7 characters of the
1556 * object name ('my_obje'). Happy guessing, user, that the final map
1557 * name will be "my_obje.kconfig".
1558 * Now, with libbpf starting to support arbitrarily named .rodata.*
1559 * and .data.* data sections, it's possible that ELF section name is
1560 * longer than allowed 15 chars, so we now need to be careful to take
1561 * only up to 15 first characters of ELF name, taking no BPF object
1562 * name characters at all. So '.rodata.abracadabra' will result in
1563 * '.rodata.abracad' kernel and user-visible name.
1564 * We need to keep this convoluted logic intact for .data, .bss and
1565 * .rodata maps, but for new custom .data.custom and .rodata.custom
1566 * maps we use their ELF names as is, not prepending bpf_object name
1567 * in front. We still need to truncate them to 15 characters for the
1568 * kernel. Full name can be recovered for such maps by using DATASEC
1569 * BTF type associated with such map's value type, though.
1570 */
1571 if (sfx_len >= BPF_OBJ_NAME_LEN)
1572 sfx_len = BPF_OBJ_NAME_LEN - 1;
1573
1574 /* if there are two or more dots in map name, it's a custom dot map */
1575 if (strchr(real_name + 1, '.') != NULL)
1576 pfx_len = 0;
1577 else
1578 pfx_len = min((size_t)BPF_OBJ_NAME_LEN - sfx_len - 1, strlen(obj->name));
81bfdd08
AN
1579
1580 snprintf(map_name, sizeof(map_name), "%.*s%.*s", pfx_len, obj->name,
aed65917 1581 sfx_len, real_name);
81bfdd08 1582
113e6b7e
THJ
1583 /* sanitise map name to characters allowed by kernel */
1584 for (p = map_name; *p && p < map_name + sizeof(map_name); p++)
1585 if (!isalnum(*p) && *p != '_' && *p != '.')
1586 *p = '_';
1587
81bfdd08
AN
1588 return strdup(map_name);
1589}
1590
262cfb74 1591static int
4fcac46c
AN
1592map_fill_btf_type_info(struct bpf_object *obj, struct bpf_map *map);
1593
1594/* Internal BPF map is mmap()'able only if at least one of corresponding
1595 * DATASEC's VARs are to be exposed through BPF skeleton. I.e., it's a GLOBAL
1596 * variable and it's not marked as __hidden (which turns it into, effectively,
1597 * a STATIC variable).
1598 */
1599static bool map_is_mmapable(struct bpf_object *obj, struct bpf_map *map)
1600{
1601 const struct btf_type *t, *vt;
1602 struct btf_var_secinfo *vsi;
1603 int i, n;
1604
1605 if (!map->btf_value_type_id)
1606 return false;
1607
1608 t = btf__type_by_id(obj->btf, map->btf_value_type_id);
1609 if (!btf_is_datasec(t))
1610 return false;
1611
1612 vsi = btf_var_secinfos(t);
1613 for (i = 0, n = btf_vlen(t); i < n; i++, vsi++) {
1614 vt = btf__type_by_id(obj->btf, vsi->type);
1615 if (!btf_is_var(vt))
1616 continue;
1617
1618 if (btf_var(vt)->linkage != BTF_VAR_STATIC)
1619 return true;
1620 }
1621
1622 return false;
1623}
262cfb74 1624
d859900c 1625static int
bf829271 1626bpf_object__init_internal_map(struct bpf_object *obj, enum libbpf_map_type type,
aed65917 1627 const char *real_name, int sec_idx, void *data, size_t data_sz)
d859900c 1628{
bf829271
AN
1629 struct bpf_map_def *def;
1630 struct bpf_map *map;
9d0a2331 1631 size_t mmap_sz;
eba9c5f4 1632 int err;
bf829271
AN
1633
1634 map = bpf_object__add_map(obj);
1635 if (IS_ERR(map))
1636 return PTR_ERR(map);
d859900c
DB
1637
1638 map->libbpf_type = type;
db48814b
AN
1639 map->sec_idx = sec_idx;
1640 map->sec_offset = 0;
aed65917
AN
1641 map->real_name = strdup(real_name);
1642 map->name = internal_map_name(obj, real_name);
1643 if (!map->real_name || !map->name) {
1644 zfree(&map->real_name);
1645 zfree(&map->name);
d859900c
DB
1646 return -ENOMEM;
1647 }
1648
bf829271 1649 def = &map->def;
d859900c
DB
1650 def->type = BPF_MAP_TYPE_ARRAY;
1651 def->key_size = sizeof(int);
eba9c5f4 1652 def->value_size = data_sz;
d859900c 1653 def->max_entries = 1;
81bfdd08 1654 def->map_flags = type == LIBBPF_MAP_RODATA || type == LIBBPF_MAP_KCONFIG
166750bc 1655 ? BPF_F_RDONLY_PROG : 0;
4fcac46c
AN
1656
1657 /* failures are fine because of maps like .rodata.str1.1 */
1658 (void) map_fill_btf_type_info(obj, map);
1659
1660 if (map_is_mmapable(obj, map))
1661 def->map_flags |= BPF_F_MMAPABLE;
7fe74b43
AN
1662
1663 pr_debug("map '%s' (global data): at sec_idx %d, offset %zu, flags %x.\n",
81bfdd08 1664 map->name, map->sec_idx, map->sec_offset, def->map_flags);
7fe74b43 1665
9d0a2331
JK
1666 mmap_sz = bpf_map_mmap_sz(map->def.value_size, map->def.max_entries);
1667 map->mmaped = mmap(NULL, mmap_sz, PROT_READ | PROT_WRITE,
eba9c5f4
AN
1668 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
1669 if (map->mmaped == MAP_FAILED) {
1670 err = -errno;
1671 map->mmaped = NULL;
1672 pr_warn("failed to alloc map '%s' content buffer: %d\n",
1673 map->name, err);
aed65917 1674 zfree(&map->real_name);
eba9c5f4
AN
1675 zfree(&map->name);
1676 return err;
d859900c
DB
1677 }
1678
166750bc 1679 if (data)
eba9c5f4
AN
1680 memcpy(map->mmaped, data, data_sz);
1681
e1d1dc46 1682 pr_debug("map %td is \"%s\"\n", map - obj->maps, map->name);
d859900c
DB
1683 return 0;
1684}
1685
bf829271
AN
1686static int bpf_object__init_global_data_maps(struct bpf_object *obj)
1687{
25bbbd7a 1688 struct elf_sec_desc *sec_desc;
aed65917 1689 const char *sec_name;
25bbbd7a 1690 int err = 0, sec_idx;
bf829271 1691
bf829271
AN
1692 /*
1693 * Populate obj->maps with libbpf internal maps.
1694 */
25bbbd7a
AN
1695 for (sec_idx = 1; sec_idx < obj->efile.sec_cnt; sec_idx++) {
1696 sec_desc = &obj->efile.secs[sec_idx];
1697
47ea7417 1698 /* Skip recognized sections with size 0. */
d4e6d684 1699 if (!sec_desc->data || sec_desc->data->d_size == 0)
47ea7417
JH
1700 continue;
1701
25bbbd7a
AN
1702 switch (sec_desc->sec_type) {
1703 case SEC_DATA:
aed65917 1704 sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, sec_idx));
25bbbd7a 1705 err = bpf_object__init_internal_map(obj, LIBBPF_MAP_DATA,
aed65917 1706 sec_name, sec_idx,
25bbbd7a
AN
1707 sec_desc->data->d_buf,
1708 sec_desc->data->d_size);
1709 break;
1710 case SEC_RODATA:
1711 obj->has_rodata = true;
aed65917 1712 sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, sec_idx));
25bbbd7a 1713 err = bpf_object__init_internal_map(obj, LIBBPF_MAP_RODATA,
aed65917 1714 sec_name, sec_idx,
25bbbd7a
AN
1715 sec_desc->data->d_buf,
1716 sec_desc->data->d_size);
1717 break;
1718 case SEC_BSS:
aed65917 1719 sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, sec_idx));
25bbbd7a 1720 err = bpf_object__init_internal_map(obj, LIBBPF_MAP_BSS,
aed65917 1721 sec_name, sec_idx,
25bbbd7a
AN
1722 NULL,
1723 sec_desc->data->d_size);
1724 break;
1725 default:
1726 /* skip */
1727 break;
1728 }
bf829271
AN
1729 if (err)
1730 return err;
1731 }
1732 return 0;
1733}
1734
166750bc
AN
1735
1736static struct extern_desc *find_extern_by_name(const struct bpf_object *obj,
1737 const void *name)
1738{
1739 int i;
1740
1741 for (i = 0; i < obj->nr_extern; i++) {
1742 if (strcmp(obj->externs[i].name, name) == 0)
1743 return &obj->externs[i];
1744 }
1745 return NULL;
1746}
1747
2e33efe3
AN
1748static int set_kcfg_value_tri(struct extern_desc *ext, void *ext_val,
1749 char value)
166750bc 1750{
2e33efe3
AN
1751 switch (ext->kcfg.type) {
1752 case KCFG_BOOL:
166750bc 1753 if (value == 'm') {
55d00c37 1754 pr_warn("extern (kcfg) '%s': value '%c' implies tristate or char type\n",
166750bc
AN
1755 ext->name, value);
1756 return -EINVAL;
1757 }
1758 *(bool *)ext_val = value == 'y' ? true : false;
1759 break;
2e33efe3 1760 case KCFG_TRISTATE:
166750bc
AN
1761 if (value == 'y')
1762 *(enum libbpf_tristate *)ext_val = TRI_YES;
1763 else if (value == 'm')
1764 *(enum libbpf_tristate *)ext_val = TRI_MODULE;
1765 else /* value == 'n' */
1766 *(enum libbpf_tristate *)ext_val = TRI_NO;
1767 break;
2e33efe3 1768 case KCFG_CHAR:
166750bc
AN
1769 *(char *)ext_val = value;
1770 break;
2e33efe3
AN
1771 case KCFG_UNKNOWN:
1772 case KCFG_INT:
1773 case KCFG_CHAR_ARR:
166750bc 1774 default:
55d00c37 1775 pr_warn("extern (kcfg) '%s': value '%c' implies bool, tristate, or char type\n",
166750bc
AN
1776 ext->name, value);
1777 return -EINVAL;
1778 }
1779 ext->is_set = true;
1780 return 0;
1781}
1782
2e33efe3
AN
1783static int set_kcfg_value_str(struct extern_desc *ext, char *ext_val,
1784 const char *value)
166750bc
AN
1785{
1786 size_t len;
1787
2e33efe3 1788 if (ext->kcfg.type != KCFG_CHAR_ARR) {
55d00c37
AN
1789 pr_warn("extern (kcfg) '%s': value '%s' implies char array type\n",
1790 ext->name, value);
166750bc
AN
1791 return -EINVAL;
1792 }
1793
1794 len = strlen(value);
1795 if (value[len - 1] != '"') {
2e33efe3 1796 pr_warn("extern (kcfg) '%s': invalid string config '%s'\n",
166750bc
AN
1797 ext->name, value);
1798 return -EINVAL;
1799 }
1800
1801 /* strip quotes */
1802 len -= 2;
2e33efe3 1803 if (len >= ext->kcfg.sz) {
55d00c37 1804 pr_warn("extern (kcfg) '%s': long string '%s' of (%zu bytes) truncated to %d bytes\n",
2e33efe3
AN
1805 ext->name, value, len, ext->kcfg.sz - 1);
1806 len = ext->kcfg.sz - 1;
166750bc
AN
1807 }
1808 memcpy(ext_val, value + 1, len);
1809 ext_val[len] = '\0';
1810 ext->is_set = true;
1811 return 0;
1812}
1813
1814static int parse_u64(const char *value, __u64 *res)
1815{
1816 char *value_end;
1817 int err;
1818
1819 errno = 0;
1820 *res = strtoull(value, &value_end, 0);
1821 if (errno) {
1822 err = -errno;
1823 pr_warn("failed to parse '%s' as integer: %d\n", value, err);
1824 return err;
1825 }
1826 if (*value_end) {
1827 pr_warn("failed to parse '%s' as integer completely\n", value);
1828 return -EINVAL;
1829 }
1830 return 0;
1831}
1832
2e33efe3 1833static bool is_kcfg_value_in_range(const struct extern_desc *ext, __u64 v)
166750bc 1834{
2e33efe3 1835 int bit_sz = ext->kcfg.sz * 8;
166750bc 1836
2e33efe3 1837 if (ext->kcfg.sz == 8)
166750bc
AN
1838 return true;
1839
1840 /* Validate that value stored in u64 fits in integer of `ext->sz`
1841 * bytes size without any loss of information. If the target integer
1842 * is signed, we rely on the following limits of integer type of
1843 * Y bits and subsequent transformation:
1844 *
1845 * -2^(Y-1) <= X <= 2^(Y-1) - 1
1846 * 0 <= X + 2^(Y-1) <= 2^Y - 1
1847 * 0 <= X + 2^(Y-1) < 2^Y
1848 *
1849 * For unsigned target integer, check that all the (64 - Y) bits are
1850 * zero.
1851 */
2e33efe3 1852 if (ext->kcfg.is_signed)
166750bc
AN
1853 return v + (1ULL << (bit_sz - 1)) < (1ULL << bit_sz);
1854 else
1855 return (v >> bit_sz) == 0;
1856}
1857
2e33efe3
AN
1858static int set_kcfg_value_num(struct extern_desc *ext, void *ext_val,
1859 __u64 value)
166750bc 1860{
55d00c37
AN
1861 if (ext->kcfg.type != KCFG_INT && ext->kcfg.type != KCFG_CHAR &&
1862 ext->kcfg.type != KCFG_BOOL) {
1863 pr_warn("extern (kcfg) '%s': value '%llu' implies integer, char, or boolean type\n",
7745ff98 1864 ext->name, (unsigned long long)value);
166750bc
AN
1865 return -EINVAL;
1866 }
55d00c37
AN
1867 if (ext->kcfg.type == KCFG_BOOL && value > 1) {
1868 pr_warn("extern (kcfg) '%s': value '%llu' isn't boolean compatible\n",
1869 ext->name, (unsigned long long)value);
1870 return -EINVAL;
1871
1872 }
2e33efe3 1873 if (!is_kcfg_value_in_range(ext, value)) {
55d00c37 1874 pr_warn("extern (kcfg) '%s': value '%llu' doesn't fit in %d bytes\n",
2e33efe3 1875 ext->name, (unsigned long long)value, ext->kcfg.sz);
166750bc
AN
1876 return -ERANGE;
1877 }
2e33efe3 1878 switch (ext->kcfg.sz) {
e3ba8e4e
KM
1879 case 1:
1880 *(__u8 *)ext_val = value;
1881 break;
1882 case 2:
1883 *(__u16 *)ext_val = value;
1884 break;
1885 case 4:
1886 *(__u32 *)ext_val = value;
1887 break;
1888 case 8:
1889 *(__u64 *)ext_val = value;
1890 break;
1891 default:
1892 return -EINVAL;
166750bc
AN
1893 }
1894 ext->is_set = true;
1895 return 0;
1896}
1897
8601fd42
AN
1898static int bpf_object__process_kconfig_line(struct bpf_object *obj,
1899 char *buf, void *data)
166750bc 1900{
166750bc 1901 struct extern_desc *ext;
8601fd42 1902 char *sep, *value;
166750bc
AN
1903 int len, err = 0;
1904 void *ext_val;
1905 __u64 num;
166750bc 1906
13d35a0c 1907 if (!str_has_pfx(buf, "CONFIG_"))
8601fd42 1908 return 0;
166750bc 1909
8601fd42
AN
1910 sep = strchr(buf, '=');
1911 if (!sep) {
1912 pr_warn("failed to parse '%s': no separator\n", buf);
1913 return -EINVAL;
1914 }
1915
1916 /* Trim ending '\n' */
1917 len = strlen(buf);
1918 if (buf[len - 1] == '\n')
1919 buf[len - 1] = '\0';
1920 /* Split on '=' and ensure that a value is present. */
1921 *sep = '\0';
1922 if (!sep[1]) {
1923 *sep = '=';
1924 pr_warn("failed to parse '%s': no value\n", buf);
1925 return -EINVAL;
1926 }
1927
1928 ext = find_extern_by_name(obj, buf);
1929 if (!ext || ext->is_set)
1930 return 0;
1931
2e33efe3 1932 ext_val = data + ext->kcfg.data_off;
8601fd42
AN
1933 value = sep + 1;
1934
1935 switch (*value) {
1936 case 'y': case 'n': case 'm':
2e33efe3 1937 err = set_kcfg_value_tri(ext, ext_val, *value);
8601fd42
AN
1938 break;
1939 case '"':
2e33efe3 1940 err = set_kcfg_value_str(ext, ext_val, value);
8601fd42
AN
1941 break;
1942 default:
1943 /* assume integer */
1944 err = parse_u64(value, &num);
1945 if (err) {
55d00c37 1946 pr_warn("extern (kcfg) '%s': value '%s' isn't a valid integer\n", ext->name, value);
8601fd42
AN
1947 return err;
1948 }
55d00c37
AN
1949 if (ext->kcfg.type != KCFG_INT && ext->kcfg.type != KCFG_CHAR) {
1950 pr_warn("extern (kcfg) '%s': value '%s' implies integer type\n", ext->name, value);
1951 return -EINVAL;
1952 }
2e33efe3 1953 err = set_kcfg_value_num(ext, ext_val, num);
8601fd42 1954 break;
166750bc 1955 }
8601fd42
AN
1956 if (err)
1957 return err;
55d00c37 1958 pr_debug("extern (kcfg) '%s': set to %s\n", ext->name, value);
8601fd42
AN
1959 return 0;
1960}
1961
1962static int bpf_object__read_kconfig_file(struct bpf_object *obj, void *data)
1963{
1964 char buf[PATH_MAX];
1965 struct utsname uts;
1966 int len, err = 0;
1967 gzFile file;
1968
1969 uname(&uts);
1970 len = snprintf(buf, PATH_MAX, "/boot/config-%s", uts.release);
1971 if (len < 0)
1972 return -EINVAL;
1973 else if (len >= PATH_MAX)
1974 return -ENAMETOOLONG;
1975
1976 /* gzopen also accepts uncompressed files. */
1977 file = gzopen(buf, "r");
1978 if (!file)
1979 file = gzopen("/proc/config.gz", "r");
1980
166750bc 1981 if (!file) {
8601fd42 1982 pr_warn("failed to open system Kconfig\n");
166750bc
AN
1983 return -ENOENT;
1984 }
1985
1986 while (gzgets(file, buf, sizeof(buf))) {
8601fd42
AN
1987 err = bpf_object__process_kconfig_line(obj, buf, data);
1988 if (err) {
1989 pr_warn("error parsing system Kconfig line '%s': %d\n",
1990 buf, err);
166750bc
AN
1991 goto out;
1992 }
8601fd42 1993 }
166750bc 1994
8601fd42
AN
1995out:
1996 gzclose(file);
1997 return err;
1998}
166750bc 1999
8601fd42
AN
2000static int bpf_object__read_kconfig_mem(struct bpf_object *obj,
2001 const char *config, void *data)
2002{
2003 char buf[PATH_MAX];
2004 int err = 0;
2005 FILE *file;
166750bc 2006
8601fd42
AN
2007 file = fmemopen((void *)config, strlen(config), "r");
2008 if (!file) {
2009 err = -errno;
2010 pr_warn("failed to open in-memory Kconfig: %d\n", err);
2011 return err;
2012 }
2013
2014 while (fgets(buf, sizeof(buf), file)) {
2015 err = bpf_object__process_kconfig_line(obj, buf, data);
2016 if (err) {
2017 pr_warn("error parsing in-memory Kconfig line '%s': %d\n",
2018 buf, err);
166750bc
AN
2019 break;
2020 }
166750bc
AN
2021 }
2022
8601fd42 2023 fclose(file);
166750bc
AN
2024 return err;
2025}
2026
81bfdd08 2027static int bpf_object__init_kconfig_map(struct bpf_object *obj)
166750bc 2028{
2e33efe3 2029 struct extern_desc *last_ext = NULL, *ext;
166750bc 2030 size_t map_sz;
2e33efe3 2031 int i, err;
166750bc 2032
2e33efe3
AN
2033 for (i = 0; i < obj->nr_extern; i++) {
2034 ext = &obj->externs[i];
2035 if (ext->type == EXT_KCFG)
2036 last_ext = ext;
2037 }
166750bc 2038
2e33efe3
AN
2039 if (!last_ext)
2040 return 0;
166750bc 2041
2e33efe3 2042 map_sz = last_ext->kcfg.data_off + last_ext->kcfg.sz;
81bfdd08 2043 err = bpf_object__init_internal_map(obj, LIBBPF_MAP_KCONFIG,
aed65917 2044 ".kconfig", obj->efile.symbols_shndx,
166750bc
AN
2045 NULL, map_sz);
2046 if (err)
2047 return err;
2048
81bfdd08 2049 obj->kconfig_map_idx = obj->nr_maps - 1;
166750bc
AN
2050
2051 return 0;
2052}
2053
42869d28 2054const struct btf_type *
ddc7c304 2055skip_mods_and_typedefs(const struct btf *btf, __u32 id, __u32 *res_id)
abd29c93
AN
2056{
2057 const struct btf_type *t = btf__type_by_id(btf, id);
8837fe5d 2058
ddc7c304
AN
2059 if (res_id)
2060 *res_id = id;
2061
2062 while (btf_is_mod(t) || btf_is_typedef(t)) {
2063 if (res_id)
2064 *res_id = t->type;
2065 t = btf__type_by_id(btf, t->type);
abd29c93 2066 }
ddc7c304
AN
2067
2068 return t;
abd29c93
AN
2069}
2070
590a0088
MKL
2071static const struct btf_type *
2072resolve_func_ptr(const struct btf *btf, __u32 id, __u32 *res_id)
2073{
2074 const struct btf_type *t;
2075
2076 t = skip_mods_and_typedefs(btf, id, NULL);
2077 if (!btf_is_ptr(t))
2078 return NULL;
2079
2080 t = skip_mods_and_typedefs(btf, t->type, res_id);
2081
2082 return btf_is_func_proto(t) ? t : NULL;
2083}
2084
774e132e 2085static const char *__btf_kind_str(__u16 kind)
81ba0889 2086{
774e132e 2087 switch (kind) {
81ba0889
AN
2088 case BTF_KIND_UNKN: return "void";
2089 case BTF_KIND_INT: return "int";
2090 case BTF_KIND_PTR: return "ptr";
2091 case BTF_KIND_ARRAY: return "array";
2092 case BTF_KIND_STRUCT: return "struct";
2093 case BTF_KIND_UNION: return "union";
2094 case BTF_KIND_ENUM: return "enum";
2095 case BTF_KIND_FWD: return "fwd";
2096 case BTF_KIND_TYPEDEF: return "typedef";
2097 case BTF_KIND_VOLATILE: return "volatile";
2098 case BTF_KIND_CONST: return "const";
2099 case BTF_KIND_RESTRICT: return "restrict";
2100 case BTF_KIND_FUNC: return "func";
2101 case BTF_KIND_FUNC_PROTO: return "func_proto";
2102 case BTF_KIND_VAR: return "var";
2103 case BTF_KIND_DATASEC: return "datasec";
22541a9e 2104 case BTF_KIND_FLOAT: return "float";
223f903e 2105 case BTF_KIND_DECL_TAG: return "decl_tag";
2dc1e488 2106 case BTF_KIND_TYPE_TAG: return "type_tag";
f2a62588 2107 case BTF_KIND_ENUM64: return "enum64";
81ba0889
AN
2108 default: return "unknown";
2109 }
2110}
2111
42869d28 2112const char *btf_kind_str(const struct btf_type *t)
774e132e
MKL
2113{
2114 return __btf_kind_str(btf_kind(t));
2115}
2116
ef99b02b
AN
2117/*
2118 * Fetch integer attribute of BTF map definition. Such attributes are
2119 * represented using a pointer to an array, in which dimensionality of array
2120 * encodes specified integer value. E.g., int (*type)[BPF_MAP_TYPE_ARRAY];
2121 * encodes `type => BPF_MAP_TYPE_ARRAY` key/value pair completely using BTF
2122 * type definition, while using only sizeof(void *) space in ELF data section.
2123 */
2124static bool get_map_field_int(const char *map_name, const struct btf *btf,
8983b731
AN
2125 const struct btf_member *m, __u32 *res)
2126{
ddc7c304 2127 const struct btf_type *t = skip_mods_and_typedefs(btf, m->type, NULL);
abd29c93 2128 const char *name = btf__name_by_offset(btf, m->name_off);
ef99b02b
AN
2129 const struct btf_array *arr_info;
2130 const struct btf_type *arr_t;
abd29c93 2131
b03bc685 2132 if (!btf_is_ptr(t)) {
81ba0889
AN
2133 pr_warn("map '%s': attr '%s': expected PTR, got %s.\n",
2134 map_name, name, btf_kind_str(t));
abd29c93
AN
2135 return false;
2136 }
ef99b02b
AN
2137
2138 arr_t = btf__type_by_id(btf, t->type);
2139 if (!arr_t) {
be18010e
KW
2140 pr_warn("map '%s': attr '%s': type [%u] not found.\n",
2141 map_name, name, t->type);
abd29c93
AN
2142 return false;
2143 }
b03bc685 2144 if (!btf_is_array(arr_t)) {
81ba0889
AN
2145 pr_warn("map '%s': attr '%s': expected ARRAY, got %s.\n",
2146 map_name, name, btf_kind_str(arr_t));
abd29c93
AN
2147 return false;
2148 }
b03bc685 2149 arr_info = btf_array(arr_t);
ef99b02b 2150 *res = arr_info->nelems;
abd29c93
AN
2151 return true;
2152}
2153
e588c116
WY
2154static int pathname_concat(char *buf, size_t buf_sz, const char *path, const char *name)
2155{
2156 int len;
2157
2158 len = snprintf(buf, buf_sz, "%s/%s", path, name);
2159 if (len < 0)
2160 return -EINVAL;
2161 if (len >= buf_sz)
2162 return -ENAMETOOLONG;
2163
2164 return 0;
2165}
2166
57a00f41
THJ
2167static int build_map_pin_path(struct bpf_map *map, const char *path)
2168{
2169 char buf[PATH_MAX];
e588c116 2170 int err;
57a00f41
THJ
2171
2172 if (!path)
2173 path = "/sys/fs/bpf";
2174
e588c116
WY
2175 err = pathname_concat(buf, sizeof(buf), path, bpf_map__name(map));
2176 if (err)
2177 return err;
57a00f41 2178
6e9cab2e 2179 return bpf_map__set_pin_path(map, buf);
57a00f41
THJ
2180}
2181
146bf811
AN
2182/* should match definition in bpf_helpers.h */
2183enum libbpf_pin_type {
2184 LIBBPF_PIN_NONE,
2185 /* PIN_BY_NAME: pin maps by name (in /sys/fs/bpf by default) */
2186 LIBBPF_PIN_BY_NAME,
2187};
2188
c7ef5ec9
AN
2189int parse_btf_map_def(const char *map_name, struct btf *btf,
2190 const struct btf_type *def_t, bool strict,
2191 struct btf_map_def *map_def, struct btf_map_def *inner_def)
abd29c93 2192{
41017e56 2193 const struct btf_type *t;
abd29c93 2194 const struct btf_member *m;
c7ef5ec9 2195 bool is_inner = inner_def == NULL;
abd29c93
AN
2196 int vlen, i;
2197
c7ef5ec9
AN
2198 vlen = btf_vlen(def_t);
2199 m = btf_members(def_t);
abd29c93 2200 for (i = 0; i < vlen; i++, m++) {
c7ef5ec9 2201 const char *name = btf__name_by_offset(btf, m->name_off);
abd29c93
AN
2202
2203 if (!name) {
c7ef5ec9 2204 pr_warn("map '%s': invalid field #%d.\n", map_name, i);
abd29c93
AN
2205 return -EINVAL;
2206 }
2207 if (strcmp(name, "type") == 0) {
c7ef5ec9 2208 if (!get_map_field_int(map_name, btf, m, &map_def->map_type))
abd29c93 2209 return -EINVAL;
c7ef5ec9 2210 map_def->parts |= MAP_DEF_MAP_TYPE;
abd29c93 2211 } else if (strcmp(name, "max_entries") == 0) {
c7ef5ec9 2212 if (!get_map_field_int(map_name, btf, m, &map_def->max_entries))
abd29c93 2213 return -EINVAL;
c7ef5ec9 2214 map_def->parts |= MAP_DEF_MAX_ENTRIES;
abd29c93 2215 } else if (strcmp(name, "map_flags") == 0) {
c7ef5ec9 2216 if (!get_map_field_int(map_name, btf, m, &map_def->map_flags))
abd29c93 2217 return -EINVAL;
c7ef5ec9 2218 map_def->parts |= MAP_DEF_MAP_FLAGS;
1bdb6c9a 2219 } else if (strcmp(name, "numa_node") == 0) {
c7ef5ec9 2220 if (!get_map_field_int(map_name, btf, m, &map_def->numa_node))
1bdb6c9a 2221 return -EINVAL;
c7ef5ec9 2222 map_def->parts |= MAP_DEF_NUMA_NODE;
abd29c93
AN
2223 } else if (strcmp(name, "key_size") == 0) {
2224 __u32 sz;
2225
c7ef5ec9 2226 if (!get_map_field_int(map_name, btf, m, &sz))
abd29c93 2227 return -EINVAL;
c7ef5ec9 2228 if (map_def->key_size && map_def->key_size != sz) {
be18010e 2229 pr_warn("map '%s': conflicting key size %u != %u.\n",
c7ef5ec9 2230 map_name, map_def->key_size, sz);
abd29c93
AN
2231 return -EINVAL;
2232 }
c7ef5ec9
AN
2233 map_def->key_size = sz;
2234 map_def->parts |= MAP_DEF_KEY_SIZE;
abd29c93
AN
2235 } else if (strcmp(name, "key") == 0) {
2236 __s64 sz;
2237
c7ef5ec9 2238 t = btf__type_by_id(btf, m->type);
abd29c93 2239 if (!t) {
be18010e 2240 pr_warn("map '%s': key type [%d] not found.\n",
c7ef5ec9 2241 map_name, m->type);
abd29c93
AN
2242 return -EINVAL;
2243 }
b03bc685 2244 if (!btf_is_ptr(t)) {
81ba0889 2245 pr_warn("map '%s': key spec is not PTR: %s.\n",
c7ef5ec9 2246 map_name, btf_kind_str(t));
abd29c93
AN
2247 return -EINVAL;
2248 }
c7ef5ec9 2249 sz = btf__resolve_size(btf, t->type);
abd29c93 2250 if (sz < 0) {
679152d3 2251 pr_warn("map '%s': can't determine key size for type [%u]: %zd.\n",
c7ef5ec9 2252 map_name, t->type, (ssize_t)sz);
abd29c93
AN
2253 return sz;
2254 }
c7ef5ec9 2255 if (map_def->key_size && map_def->key_size != sz) {
679152d3 2256 pr_warn("map '%s': conflicting key size %u != %zd.\n",
c7ef5ec9 2257 map_name, map_def->key_size, (ssize_t)sz);
abd29c93
AN
2258 return -EINVAL;
2259 }
c7ef5ec9
AN
2260 map_def->key_size = sz;
2261 map_def->key_type_id = t->type;
2262 map_def->parts |= MAP_DEF_KEY_SIZE | MAP_DEF_KEY_TYPE;
abd29c93
AN
2263 } else if (strcmp(name, "value_size") == 0) {
2264 __u32 sz;
2265
c7ef5ec9 2266 if (!get_map_field_int(map_name, btf, m, &sz))
abd29c93 2267 return -EINVAL;
c7ef5ec9 2268 if (map_def->value_size && map_def->value_size != sz) {
be18010e 2269 pr_warn("map '%s': conflicting value size %u != %u.\n",
c7ef5ec9 2270 map_name, map_def->value_size, sz);
abd29c93
AN
2271 return -EINVAL;
2272 }
c7ef5ec9
AN
2273 map_def->value_size = sz;
2274 map_def->parts |= MAP_DEF_VALUE_SIZE;
abd29c93
AN
2275 } else if (strcmp(name, "value") == 0) {
2276 __s64 sz;
2277
c7ef5ec9 2278 t = btf__type_by_id(btf, m->type);
abd29c93 2279 if (!t) {
be18010e 2280 pr_warn("map '%s': value type [%d] not found.\n",
c7ef5ec9 2281 map_name, m->type);
abd29c93
AN
2282 return -EINVAL;
2283 }
b03bc685 2284 if (!btf_is_ptr(t)) {
81ba0889 2285 pr_warn("map '%s': value spec is not PTR: %s.\n",
c7ef5ec9 2286 map_name, btf_kind_str(t));
abd29c93
AN
2287 return -EINVAL;
2288 }
c7ef5ec9 2289 sz = btf__resolve_size(btf, t->type);
abd29c93 2290 if (sz < 0) {
679152d3 2291 pr_warn("map '%s': can't determine value size for type [%u]: %zd.\n",
c7ef5ec9 2292 map_name, t->type, (ssize_t)sz);
abd29c93
AN
2293 return sz;
2294 }
c7ef5ec9 2295 if (map_def->value_size && map_def->value_size != sz) {
679152d3 2296 pr_warn("map '%s': conflicting value size %u != %zd.\n",
c7ef5ec9 2297 map_name, map_def->value_size, (ssize_t)sz);
abd29c93
AN
2298 return -EINVAL;
2299 }
c7ef5ec9
AN
2300 map_def->value_size = sz;
2301 map_def->value_type_id = t->type;
2302 map_def->parts |= MAP_DEF_VALUE_SIZE | MAP_DEF_VALUE_TYPE;
646f02ff
AN
2303 }
2304 else if (strcmp(name, "values") == 0) {
341ac5ff
HC
2305 bool is_map_in_map = bpf_map_type__is_map_in_map(map_def->map_type);
2306 bool is_prog_array = map_def->map_type == BPF_MAP_TYPE_PROG_ARRAY;
2307 const char *desc = is_map_in_map ? "map-in-map inner" : "prog-array value";
c7ef5ec9 2308 char inner_map_name[128];
646f02ff
AN
2309 int err;
2310
2311 if (is_inner) {
2312 pr_warn("map '%s': multi-level inner maps not supported.\n",
c7ef5ec9 2313 map_name);
646f02ff
AN
2314 return -ENOTSUP;
2315 }
2316 if (i != vlen - 1) {
2317 pr_warn("map '%s': '%s' member should be last.\n",
c7ef5ec9 2318 map_name, name);
646f02ff
AN
2319 return -EINVAL;
2320 }
341ac5ff
HC
2321 if (!is_map_in_map && !is_prog_array) {
2322 pr_warn("map '%s': should be map-in-map or prog-array.\n",
c7ef5ec9 2323 map_name);
646f02ff
AN
2324 return -ENOTSUP;
2325 }
c7ef5ec9 2326 if (map_def->value_size && map_def->value_size != 4) {
646f02ff 2327 pr_warn("map '%s': conflicting value size %u != 4.\n",
c7ef5ec9 2328 map_name, map_def->value_size);
646f02ff
AN
2329 return -EINVAL;
2330 }
c7ef5ec9
AN
2331 map_def->value_size = 4;
2332 t = btf__type_by_id(btf, m->type);
646f02ff 2333 if (!t) {
341ac5ff
HC
2334 pr_warn("map '%s': %s type [%d] not found.\n",
2335 map_name, desc, m->type);
646f02ff
AN
2336 return -EINVAL;
2337 }
2338 if (!btf_is_array(t) || btf_array(t)->nelems) {
341ac5ff
HC
2339 pr_warn("map '%s': %s spec is not a zero-sized array.\n",
2340 map_name, desc);
646f02ff
AN
2341 return -EINVAL;
2342 }
c7ef5ec9 2343 t = skip_mods_and_typedefs(btf, btf_array(t)->type, NULL);
646f02ff 2344 if (!btf_is_ptr(t)) {
341ac5ff
HC
2345 pr_warn("map '%s': %s def is of unexpected kind %s.\n",
2346 map_name, desc, btf_kind_str(t));
646f02ff
AN
2347 return -EINVAL;
2348 }
c7ef5ec9 2349 t = skip_mods_and_typedefs(btf, t->type, NULL);
341ac5ff
HC
2350 if (is_prog_array) {
2351 if (!btf_is_func_proto(t)) {
2352 pr_warn("map '%s': prog-array value def is of unexpected kind %s.\n",
2353 map_name, btf_kind_str(t));
2354 return -EINVAL;
2355 }
2356 continue;
2357 }
646f02ff 2358 if (!btf_is_struct(t)) {
81ba0889 2359 pr_warn("map '%s': map-in-map inner def is of unexpected kind %s.\n",
c7ef5ec9 2360 map_name, btf_kind_str(t));
646f02ff
AN
2361 return -EINVAL;
2362 }
2363
c7ef5ec9
AN
2364 snprintf(inner_map_name, sizeof(inner_map_name), "%s.inner", map_name);
2365 err = parse_btf_map_def(inner_map_name, btf, t, strict, inner_def, NULL);
646f02ff
AN
2366 if (err)
2367 return err;
c7ef5ec9
AN
2368
2369 map_def->parts |= MAP_DEF_INNER_MAP;
57a00f41
THJ
2370 } else if (strcmp(name, "pinning") == 0) {
2371 __u32 val;
57a00f41 2372
646f02ff 2373 if (is_inner) {
c7ef5ec9 2374 pr_warn("map '%s': inner def can't be pinned.\n", map_name);
646f02ff
AN
2375 return -EINVAL;
2376 }
c7ef5ec9 2377 if (!get_map_field_int(map_name, btf, m, &val))
57a00f41 2378 return -EINVAL;
c7ef5ec9 2379 if (val != LIBBPF_PIN_NONE && val != LIBBPF_PIN_BY_NAME) {
57a00f41 2380 pr_warn("map '%s': invalid pinning value %u.\n",
c7ef5ec9 2381 map_name, val);
57a00f41
THJ
2382 return -EINVAL;
2383 }
c7ef5ec9
AN
2384 map_def->pinning = val;
2385 map_def->parts |= MAP_DEF_PINNING;
47512102
JK
2386 } else if (strcmp(name, "map_extra") == 0) {
2387 __u32 map_extra;
2388
2389 if (!get_map_field_int(map_name, btf, m, &map_extra))
2390 return -EINVAL;
2391 map_def->map_extra = map_extra;
2392 map_def->parts |= MAP_DEF_MAP_EXTRA;
abd29c93
AN
2393 } else {
2394 if (strict) {
c7ef5ec9 2395 pr_warn("map '%s': unknown field '%s'.\n", map_name, name);
abd29c93
AN
2396 return -ENOTSUP;
2397 }
c7ef5ec9 2398 pr_debug("map '%s': ignoring unknown field '%s'.\n", map_name, name);
abd29c93
AN
2399 }
2400 }
2401
c7ef5ec9
AN
2402 if (map_def->map_type == BPF_MAP_TYPE_UNSPEC) {
2403 pr_warn("map '%s': map type isn't specified.\n", map_name);
abd29c93
AN
2404 return -EINVAL;
2405 }
2406
2407 return 0;
2408}
2409
597fbc46
AN
2410static size_t adjust_ringbuf_sz(size_t sz)
2411{
2412 __u32 page_sz = sysconf(_SC_PAGE_SIZE);
2413 __u32 mul;
2414
2415 /* if user forgot to set any size, make sure they see error */
2416 if (sz == 0)
2417 return 0;
2418 /* Kernel expects BPF_MAP_TYPE_RINGBUF's max_entries to be
2419 * a power-of-2 multiple of kernel's page size. If user diligently
2420 * satisified these conditions, pass the size through.
2421 */
2422 if ((sz % page_sz) == 0 && is_pow_of_2(sz / page_sz))
2423 return sz;
2424
2425 /* Otherwise find closest (page_sz * power_of_2) product bigger than
2426 * user-set size to satisfy both user size request and kernel
2427 * requirements and substitute correct max_entries for map creation.
2428 */
2429 for (mul = 1; mul <= UINT_MAX / page_sz; mul <<= 1) {
2430 if (mul * page_sz > sz)
2431 return mul * page_sz;
2432 }
2433
2434 /* if it's impossible to satisfy the conditions (i.e., user size is
2435 * very close to UINT_MAX but is not a power-of-2 multiple of
2436 * page_size) then just return original size and let kernel reject it
2437 */
2438 return sz;
2439}
2440
b66ccae0
DV
2441static bool map_is_ringbuf(const struct bpf_map *map)
2442{
2443 return map->def.type == BPF_MAP_TYPE_RINGBUF ||
2444 map->def.type == BPF_MAP_TYPE_USER_RINGBUF;
2445}
2446
c7ef5ec9
AN
2447static void fill_map_from_def(struct bpf_map *map, const struct btf_map_def *def)
2448{
2449 map->def.type = def->map_type;
2450 map->def.key_size = def->key_size;
2451 map->def.value_size = def->value_size;
2452 map->def.max_entries = def->max_entries;
2453 map->def.map_flags = def->map_flags;
47512102 2454 map->map_extra = def->map_extra;
c7ef5ec9
AN
2455
2456 map->numa_node = def->numa_node;
2457 map->btf_key_type_id = def->key_type_id;
2458 map->btf_value_type_id = def->value_type_id;
2459
597fbc46 2460 /* auto-adjust BPF ringbuf map max_entries to be a multiple of page size */
b66ccae0 2461 if (map_is_ringbuf(map))
597fbc46
AN
2462 map->def.max_entries = adjust_ringbuf_sz(map->def.max_entries);
2463
c7ef5ec9
AN
2464 if (def->parts & MAP_DEF_MAP_TYPE)
2465 pr_debug("map '%s': found type = %u.\n", map->name, def->map_type);
2466
2467 if (def->parts & MAP_DEF_KEY_TYPE)
2468 pr_debug("map '%s': found key [%u], sz = %u.\n",
2469 map->name, def->key_type_id, def->key_size);
2470 else if (def->parts & MAP_DEF_KEY_SIZE)
2471 pr_debug("map '%s': found key_size = %u.\n", map->name, def->key_size);
2472
2473 if (def->parts & MAP_DEF_VALUE_TYPE)
2474 pr_debug("map '%s': found value [%u], sz = %u.\n",
2475 map->name, def->value_type_id, def->value_size);
2476 else if (def->parts & MAP_DEF_VALUE_SIZE)
2477 pr_debug("map '%s': found value_size = %u.\n", map->name, def->value_size);
2478
2479 if (def->parts & MAP_DEF_MAX_ENTRIES)
2480 pr_debug("map '%s': found max_entries = %u.\n", map->name, def->max_entries);
2481 if (def->parts & MAP_DEF_MAP_FLAGS)
47512102
JK
2482 pr_debug("map '%s': found map_flags = 0x%x.\n", map->name, def->map_flags);
2483 if (def->parts & MAP_DEF_MAP_EXTRA)
2484 pr_debug("map '%s': found map_extra = 0x%llx.\n", map->name,
2485 (unsigned long long)def->map_extra);
c7ef5ec9
AN
2486 if (def->parts & MAP_DEF_PINNING)
2487 pr_debug("map '%s': found pinning = %u.\n", map->name, def->pinning);
2488 if (def->parts & MAP_DEF_NUMA_NODE)
2489 pr_debug("map '%s': found numa_node = %u.\n", map->name, def->numa_node);
2490
2491 if (def->parts & MAP_DEF_INNER_MAP)
2492 pr_debug("map '%s': found inner map definition.\n", map->name);
2493}
2494
c1cccec9
AN
2495static const char *btf_var_linkage_str(__u32 linkage)
2496{
2497 switch (linkage) {
2498 case BTF_VAR_STATIC: return "static";
2499 case BTF_VAR_GLOBAL_ALLOCATED: return "global";
2500 case BTF_VAR_GLOBAL_EXTERN: return "extern";
2501 default: return "unknown";
2502 }
2503}
2504
41017e56
AN
2505static int bpf_object__init_user_btf_map(struct bpf_object *obj,
2506 const struct btf_type *sec,
2507 int var_idx, int sec_idx,
2508 const Elf_Data *data, bool strict,
2509 const char *pin_root_path)
2510{
c7ef5ec9 2511 struct btf_map_def map_def = {}, inner_def = {};
41017e56
AN
2512 const struct btf_type *var, *def;
2513 const struct btf_var_secinfo *vi;
2514 const struct btf_var *var_extra;
2515 const char *map_name;
2516 struct bpf_map *map;
c7ef5ec9 2517 int err;
41017e56
AN
2518
2519 vi = btf_var_secinfos(sec) + var_idx;
2520 var = btf__type_by_id(obj->btf, vi->type);
2521 var_extra = btf_var(var);
2522 map_name = btf__name_by_offset(obj->btf, var->name_off);
2523
2524 if (map_name == NULL || map_name[0] == '\0') {
2525 pr_warn("map #%d: empty name.\n", var_idx);
2526 return -EINVAL;
2527 }
2528 if ((__u64)vi->offset + vi->size > data->d_size) {
2529 pr_warn("map '%s' BTF data is corrupted.\n", map_name);
2530 return -EINVAL;
2531 }
2532 if (!btf_is_var(var)) {
81ba0889
AN
2533 pr_warn("map '%s': unexpected var kind %s.\n",
2534 map_name, btf_kind_str(var));
41017e56
AN
2535 return -EINVAL;
2536 }
c1cccec9
AN
2537 if (var_extra->linkage != BTF_VAR_GLOBAL_ALLOCATED) {
2538 pr_warn("map '%s': unsupported map linkage %s.\n",
2539 map_name, btf_var_linkage_str(var_extra->linkage));
41017e56
AN
2540 return -EOPNOTSUPP;
2541 }
2542
2543 def = skip_mods_and_typedefs(obj->btf, var->type, NULL);
2544 if (!btf_is_struct(def)) {
81ba0889
AN
2545 pr_warn("map '%s': unexpected def kind %s.\n",
2546 map_name, btf_kind_str(var));
41017e56
AN
2547 return -EINVAL;
2548 }
2549 if (def->size > vi->size) {
2550 pr_warn("map '%s': invalid def size.\n", map_name);
2551 return -EINVAL;
2552 }
2553
2554 map = bpf_object__add_map(obj);
2555 if (IS_ERR(map))
2556 return PTR_ERR(map);
2557 map->name = strdup(map_name);
2558 if (!map->name) {
2559 pr_warn("map '%s': failed to alloc map name.\n", map_name);
2560 return -ENOMEM;
2561 }
2562 map->libbpf_type = LIBBPF_MAP_UNSPEC;
2563 map->def.type = BPF_MAP_TYPE_UNSPEC;
2564 map->sec_idx = sec_idx;
2565 map->sec_offset = vi->offset;
646f02ff 2566 map->btf_var_idx = var_idx;
41017e56
AN
2567 pr_debug("map '%s': at sec_idx %d, offset %zu.\n",
2568 map_name, map->sec_idx, map->sec_offset);
2569
c7ef5ec9
AN
2570 err = parse_btf_map_def(map->name, obj->btf, def, strict, &map_def, &inner_def);
2571 if (err)
2572 return err;
2573
2574 fill_map_from_def(map, &map_def);
2575
2576 if (map_def.pinning == LIBBPF_PIN_BY_NAME) {
2577 err = build_map_pin_path(map, pin_root_path);
2578 if (err) {
2579 pr_warn("map '%s': couldn't build pin path.\n", map->name);
2580 return err;
2581 }
2582 }
2583
2584 if (map_def.parts & MAP_DEF_INNER_MAP) {
2585 map->inner_map = calloc(1, sizeof(*map->inner_map));
2586 if (!map->inner_map)
2587 return -ENOMEM;
2588 map->inner_map->fd = -1;
2589 map->inner_map->sec_idx = sec_idx;
2590 map->inner_map->name = malloc(strlen(map_name) + sizeof(".inner") + 1);
2591 if (!map->inner_map->name)
2592 return -ENOMEM;
2593 sprintf(map->inner_map->name, "%s.inner", map_name);
2594
2595 fill_map_from_def(map->inner_map, &inner_def);
2596 }
2597
4fcac46c 2598 err = map_fill_btf_type_info(obj, map);
262cfb74
DK
2599 if (err)
2600 return err;
2601
c7ef5ec9 2602 return 0;
41017e56
AN
2603}
2604
57a00f41
THJ
2605static int bpf_object__init_user_btf_maps(struct bpf_object *obj, bool strict,
2606 const char *pin_root_path)
abd29c93
AN
2607{
2608 const struct btf_type *sec = NULL;
2609 int nr_types, i, vlen, err;
2610 const struct btf_type *t;
2611 const char *name;
2612 Elf_Data *data;
2613 Elf_Scn *scn;
2614
2615 if (obj->efile.btf_maps_shndx < 0)
2616 return 0;
2617
88a82120
AN
2618 scn = elf_sec_by_idx(obj, obj->efile.btf_maps_shndx);
2619 data = elf_sec_data(obj, scn);
abd29c93 2620 if (!scn || !data) {
88a82120
AN
2621 pr_warn("elf: failed to get %s map definitions for %s\n",
2622 MAPS_ELF_SEC, obj->path);
abd29c93
AN
2623 return -EINVAL;
2624 }
2625
6a886de0
HC
2626 nr_types = btf__type_cnt(obj->btf);
2627 for (i = 1; i < nr_types; i++) {
abd29c93 2628 t = btf__type_by_id(obj->btf, i);
b03bc685 2629 if (!btf_is_datasec(t))
abd29c93
AN
2630 continue;
2631 name = btf__name_by_offset(obj->btf, t->name_off);
2632 if (strcmp(name, MAPS_ELF_SEC) == 0) {
2633 sec = t;
646f02ff 2634 obj->efile.btf_maps_sec_btf_id = i;
abd29c93
AN
2635 break;
2636 }
2637 }
2638
2639 if (!sec) {
be18010e 2640 pr_warn("DATASEC '%s' not found.\n", MAPS_ELF_SEC);
abd29c93
AN
2641 return -ENOENT;
2642 }
2643
b03bc685 2644 vlen = btf_vlen(sec);
abd29c93
AN
2645 for (i = 0; i < vlen; i++) {
2646 err = bpf_object__init_user_btf_map(obj, sec, i,
2647 obj->efile.btf_maps_shndx,
8983b731
AN
2648 data, strict,
2649 pin_root_path);
abd29c93
AN
2650 if (err)
2651 return err;
2652 }
2653
2654 return 0;
2655}
2656
0d13bfce 2657static int bpf_object__init_maps(struct bpf_object *obj,
01af3bf0 2658 const struct bpf_object_open_opts *opts)
bf829271 2659{
166750bc
AN
2660 const char *pin_root_path;
2661 bool strict;
bd054102 2662 int err = 0;
8837fe5d 2663
166750bc
AN
2664 strict = !OPTS_GET(opts, relaxed_maps, false);
2665 pin_root_path = OPTS_GET(opts, pin_root_path, NULL);
bf829271 2666
40e1bcab 2667 err = bpf_object__init_user_btf_maps(obj, strict, pin_root_path);
166750bc 2668 err = err ?: bpf_object__init_global_data_maps(obj);
81bfdd08 2669 err = err ?: bpf_object__init_kconfig_map(obj);
809a69d6 2670 err = err ?: bpf_object_init_struct_ops(obj);
bf829271 2671
3b3af91c 2672 return err;
561bbcca
WN
2673}
2674
e3d91b0c
JDB
2675static bool section_have_execinstr(struct bpf_object *obj, int idx)
2676{
ad23b723 2677 Elf64_Shdr *sh;
e3d91b0c 2678
ad23b723
AN
2679 sh = elf_sec_hdr(obj, elf_sec_by_idx(obj, idx));
2680 if (!sh)
e3d91b0c
JDB
2681 return false;
2682
ad23b723 2683 return sh->sh_flags & SHF_EXECINSTR;
e3d91b0c
JDB
2684}
2685
0f0e55d8
AN
2686static bool btf_needs_sanitization(struct bpf_object *obj)
2687{
9ca1f56a
AS
2688 bool has_func_global = kernel_supports(obj, FEAT_BTF_GLOBAL_FUNC);
2689 bool has_datasec = kernel_supports(obj, FEAT_BTF_DATASEC);
2690 bool has_float = kernel_supports(obj, FEAT_BTF_FLOAT);
2691 bool has_func = kernel_supports(obj, FEAT_BTF_FUNC);
223f903e 2692 bool has_decl_tag = kernel_supports(obj, FEAT_BTF_DECL_TAG);
2dc1e488 2693 bool has_type_tag = kernel_supports(obj, FEAT_BTF_TYPE_TAG);
f2a62588 2694 bool has_enum64 = kernel_supports(obj, FEAT_BTF_ENUM64);
0f0e55d8 2695
2dc1e488 2696 return !has_func || !has_datasec || !has_func_global || !has_float ||
f2a62588 2697 !has_decl_tag || !has_type_tag || !has_enum64;
0f0e55d8
AN
2698}
2699
f2a62588 2700static int bpf_object__sanitize_btf(struct bpf_object *obj, struct btf *btf)
d7c4b398 2701{
9ca1f56a
AS
2702 bool has_func_global = kernel_supports(obj, FEAT_BTF_GLOBAL_FUNC);
2703 bool has_datasec = kernel_supports(obj, FEAT_BTF_DATASEC);
2704 bool has_float = kernel_supports(obj, FEAT_BTF_FLOAT);
2705 bool has_func = kernel_supports(obj, FEAT_BTF_FUNC);
223f903e 2706 bool has_decl_tag = kernel_supports(obj, FEAT_BTF_DECL_TAG);
2dc1e488 2707 bool has_type_tag = kernel_supports(obj, FEAT_BTF_TYPE_TAG);
f2a62588 2708 bool has_enum64 = kernel_supports(obj, FEAT_BTF_ENUM64);
c49a44b3 2709 int enum64_placeholder_id = 0;
d7c4b398
AN
2710 struct btf_type *t;
2711 int i, j, vlen;
d7c4b398 2712
6a886de0 2713 for (i = 1; i < btf__type_cnt(btf); i++) {
d7c4b398 2714 t = (struct btf_type *)btf__type_by_id(btf, i);
d7c4b398 2715
223f903e
YS
2716 if ((!has_datasec && btf_is_var(t)) || (!has_decl_tag && btf_is_decl_tag(t))) {
2717 /* replace VAR/DECL_TAG with INT */
d7c4b398 2718 t->info = BTF_INFO_ENC(BTF_KIND_INT, 0, 0);
1d4126c4
AN
2719 /*
2720 * using size = 1 is the safest choice, 4 will be too
2721 * big and cause kernel BTF validation failure if
2722 * original variable took less than 4 bytes
2723 */
2724 t->size = 1;
708852dc 2725 *(int *)(t + 1) = BTF_INT_ENC(0, 0, 8);
b03bc685 2726 } else if (!has_datasec && btf_is_datasec(t)) {
d7c4b398 2727 /* replace DATASEC with STRUCT */
b03bc685
AN
2728 const struct btf_var_secinfo *v = btf_var_secinfos(t);
2729 struct btf_member *m = btf_members(t);
d7c4b398
AN
2730 struct btf_type *vt;
2731 char *name;
2732
2733 name = (char *)btf__name_by_offset(btf, t->name_off);
2734 while (*name) {
2735 if (*name == '.')
2736 *name = '_';
2737 name++;
2738 }
2739
b03bc685 2740 vlen = btf_vlen(t);
d7c4b398
AN
2741 t->info = BTF_INFO_ENC(BTF_KIND_STRUCT, 0, vlen);
2742 for (j = 0; j < vlen; j++, v++, m++) {
2743 /* order of field assignments is important */
2744 m->offset = v->offset * 8;
2745 m->type = v->type;
2746 /* preserve variable name as member name */
2747 vt = (void *)btf__type_by_id(btf, v->type);
2748 m->name_off = vt->name_off;
2749 }
b03bc685 2750 } else if (!has_func && btf_is_func_proto(t)) {
d7c4b398 2751 /* replace FUNC_PROTO with ENUM */
b03bc685 2752 vlen = btf_vlen(t);
d7c4b398
AN
2753 t->info = BTF_INFO_ENC(BTF_KIND_ENUM, 0, vlen);
2754 t->size = sizeof(__u32); /* kernel enforced */
b03bc685 2755 } else if (!has_func && btf_is_func(t)) {
d7c4b398
AN
2756 /* replace FUNC with TYPEDEF */
2757 t->info = BTF_INFO_ENC(BTF_KIND_TYPEDEF, 0, 0);
2d3eb67f
AS
2758 } else if (!has_func_global && btf_is_func(t)) {
2759 /* replace BTF_FUNC_GLOBAL with BTF_FUNC_STATIC */
2760 t->info = BTF_INFO_ENC(BTF_KIND_FUNC, 0, 0);
22541a9e
IL
2761 } else if (!has_float && btf_is_float(t)) {
2762 /* replace FLOAT with an equally-sized empty STRUCT;
2763 * since C compilers do not accept e.g. "float" as a
2764 * valid struct name, make it anonymous
2765 */
2766 t->name_off = 0;
2767 t->info = BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 0);
2dc1e488
YS
2768 } else if (!has_type_tag && btf_is_type_tag(t)) {
2769 /* replace TYPE_TAG with a CONST */
2770 t->name_off = 0;
2771 t->info = BTF_INFO_ENC(BTF_KIND_CONST, 0, 0);
f2a62588
YS
2772 } else if (!has_enum64 && btf_is_enum(t)) {
2773 /* clear the kflag */
2774 t->info = btf_type_info(btf_kind(t), btf_vlen(t), false);
2775 } else if (!has_enum64 && btf_is_enum64(t)) {
2776 /* replace ENUM64 with a union */
2777 struct btf_member *m;
2778
2779 if (enum64_placeholder_id == 0) {
2780 enum64_placeholder_id = btf__add_int(btf, "enum64_placeholder", 1, 0);
2781 if (enum64_placeholder_id < 0)
2782 return enum64_placeholder_id;
2783
2784 t = (struct btf_type *)btf__type_by_id(btf, i);
2785 }
2786
2787 m = btf_members(t);
2788 vlen = btf_vlen(t);
2789 t->info = BTF_INFO_ENC(BTF_KIND_UNION, 0, vlen);
2790 for (j = 0; j < vlen; j++, m++) {
2791 m->type = enum64_placeholder_id;
2792 m->offset = 0;
2793 }
e3ba8e4e 2794 }
d7c4b398 2795 }
f2a62588
YS
2796
2797 return 0;
d7c4b398
AN
2798}
2799
b35f14f4 2800static bool libbpf_needs_btf(const struct bpf_object *obj)
abd29c93 2801{
b35f14f4
AN
2802 return obj->efile.btf_maps_shndx >= 0 ||
2803 obj->efile.st_ops_shndx >= 0 ||
809a69d6 2804 obj->efile.st_ops_link_shndx >= 0 ||
b35f14f4
AN
2805 obj->nr_extern > 0;
2806}
2807
2808static bool kernel_needs_btf(const struct bpf_object *obj)
2809{
809a69d6 2810 return obj->efile.st_ops_shndx >= 0 || obj->efile.st_ops_link_shndx >= 0;
abd29c93
AN
2811}
2812
063183bf 2813static int bpf_object__init_btf(struct bpf_object *obj,
9c6660d0
AN
2814 Elf_Data *btf_data,
2815 Elf_Data *btf_ext_data)
2816{
b7d7f3e1 2817 int err = -ENOENT;
9c6660d0
AN
2818
2819 if (btf_data) {
2820 obj->btf = btf__new(btf_data->d_buf, btf_data->d_size);
e9fc3ce9
AN
2821 err = libbpf_get_error(obj->btf);
2822 if (err) {
b7d7f3e1 2823 obj->btf = NULL;
e9fc3ce9 2824 pr_warn("Error loading ELF section %s: %d.\n", BTF_ELF_SEC, err);
9c6660d0
AN
2825 goto out;
2826 }
4c01925f
AN
2827 /* enforce 8-byte pointers for BPF-targeted BTFs */
2828 btf__set_pointer_size(obj->btf, 8);
9c6660d0
AN
2829 }
2830 if (btf_ext_data) {
11d5daa8
AN
2831 struct btf_ext_info *ext_segs[3];
2832 int seg_num, sec_num;
2833
9c6660d0
AN
2834 if (!obj->btf) {
2835 pr_debug("Ignore ELF section %s because its depending ELF section %s is not found.\n",
2836 BTF_EXT_ELF_SEC, BTF_ELF_SEC);
2837 goto out;
2838 }
e9fc3ce9
AN
2839 obj->btf_ext = btf_ext__new(btf_ext_data->d_buf, btf_ext_data->d_size);
2840 err = libbpf_get_error(obj->btf_ext);
2841 if (err) {
2842 pr_warn("Error loading ELF section %s: %d. Ignored and continue.\n",
2843 BTF_EXT_ELF_SEC, err);
9c6660d0
AN
2844 obj->btf_ext = NULL;
2845 goto out;
2846 }
11d5daa8
AN
2847
2848 /* setup .BTF.ext to ELF section mapping */
2849 ext_segs[0] = &obj->btf_ext->func_info;
2850 ext_segs[1] = &obj->btf_ext->line_info;
2851 ext_segs[2] = &obj->btf_ext->core_relo_info;
2852 for (seg_num = 0; seg_num < ARRAY_SIZE(ext_segs); seg_num++) {
2853 struct btf_ext_info *seg = ext_segs[seg_num];
2854 const struct btf_ext_info_sec *sec;
2855 const char *sec_name;
2856 Elf_Scn *scn;
2857
2858 if (seg->sec_cnt == 0)
2859 continue;
2860
2861 seg->sec_idxs = calloc(seg->sec_cnt, sizeof(*seg->sec_idxs));
2862 if (!seg->sec_idxs) {
2863 err = -ENOMEM;
2864 goto out;
2865 }
2866
2867 sec_num = 0;
2868 for_each_btf_ext_sec(seg, sec) {
2869 /* preventively increment index to avoid doing
2870 * this before every continue below
2871 */
2872 sec_num++;
2873
2874 sec_name = btf__name_by_offset(obj->btf, sec->sec_name_off);
2875 if (str_is_empty(sec_name))
2876 continue;
2877 scn = elf_sec_by_name(obj, sec_name);
2878 if (!scn)
2879 continue;
2880
2881 seg->sec_idxs[sec_num - 1] = elf_ndxscn(scn);
2882 }
2883 }
9c6660d0
AN
2884 }
2885out:
b35f14f4 2886 if (err && libbpf_needs_btf(obj)) {
be18010e 2887 pr_warn("BTF is required, but is missing or corrupted.\n");
b7d7f3e1 2888 return err;
abd29c93 2889 }
9c6660d0
AN
2890 return 0;
2891}
2892
b96c07f3
AN
2893static int compare_vsi_off(const void *_a, const void *_b)
2894{
2895 const struct btf_var_secinfo *a = _a;
2896 const struct btf_var_secinfo *b = _b;
2897
2898 return a->offset - b->offset;
2899}
2900
2901static int btf_fixup_datasec(struct bpf_object *obj, struct btf *btf,
2902 struct btf_type *t)
2903{
f33f742d
AN
2904 __u32 size = 0, i, vars = btf_vlen(t);
2905 const char *sec_name = btf__name_by_offset(btf, t->name_off);
b96c07f3 2906 struct btf_var_secinfo *vsi;
4fcac46c 2907 bool fixup_offsets = false;
f33f742d 2908 int err;
b96c07f3 2909
f33f742d 2910 if (!sec_name) {
b96c07f3
AN
2911 pr_debug("No name found in string section for DATASEC kind.\n");
2912 return -ENOENT;
2913 }
2914
4fcac46c
AN
2915 /* Extern-backing datasecs (.ksyms, .kconfig) have their size and
2916 * variable offsets set at the previous step. Further, not every
2917 * extern BTF VAR has corresponding ELF symbol preserved, so we skip
2918 * all fixups altogether for such sections and go straight to sorting
2919 * VARs within their DATASEC.
b96c07f3 2920 */
4fcac46c 2921 if (strcmp(sec_name, KCONFIG_SEC) == 0 || strcmp(sec_name, KSYMS_SEC) == 0)
b96c07f3
AN
2922 goto sort_vars;
2923
4fcac46c
AN
2924 /* Clang leaves DATASEC size and VAR offsets as zeroes, so we need to
2925 * fix this up. But BPF static linker already fixes this up and fills
2926 * all the sizes and offsets during static linking. So this step has
2927 * to be optional. But the STV_HIDDEN handling is non-optional for any
2928 * non-extern DATASEC, so the variable fixup loop below handles both
2929 * functions at the same time, paying the cost of BTF VAR <-> ELF
2930 * symbol matching just once.
2931 */
2932 if (t->size == 0) {
2933 err = find_elf_sec_sz(obj, sec_name, &size);
2934 if (err || !size) {
2935 pr_debug("sec '%s': failed to determine size from ELF: size %u, err %d\n",
2936 sec_name, size, err);
2937 return -ENOENT;
2938 }
b96c07f3 2939
4fcac46c
AN
2940 t->size = size;
2941 fixup_offsets = true;
2942 }
b96c07f3
AN
2943
2944 for (i = 0, vsi = btf_var_secinfos(t); i < vars; i++, vsi++) {
f33f742d
AN
2945 const struct btf_type *t_var;
2946 struct btf_var *var;
2947 const char *var_name;
2948 Elf64_Sym *sym;
2949
b96c07f3 2950 t_var = btf__type_by_id(btf, vsi->type);
88918dc1 2951 if (!t_var || !btf_is_var(t_var)) {
f33f742d 2952 pr_debug("sec '%s': unexpected non-VAR type found\n", sec_name);
b96c07f3
AN
2953 return -EINVAL;
2954 }
2955
88918dc1 2956 var = btf_var(t_var);
f33f742d 2957 if (var->linkage == BTF_VAR_STATIC || var->linkage == BTF_VAR_GLOBAL_EXTERN)
b96c07f3
AN
2958 continue;
2959
f33f742d
AN
2960 var_name = btf__name_by_offset(btf, t_var->name_off);
2961 if (!var_name) {
2962 pr_debug("sec '%s': failed to find name of DATASEC's member #%d\n",
2963 sec_name, i);
b96c07f3
AN
2964 return -ENOENT;
2965 }
2966
f33f742d
AN
2967 sym = find_elf_var_sym(obj, var_name);
2968 if (IS_ERR(sym)) {
2969 pr_debug("sec '%s': failed to find ELF symbol for VAR '%s'\n",
2970 sec_name, var_name);
b96c07f3
AN
2971 return -ENOENT;
2972 }
2973
4fcac46c
AN
2974 if (fixup_offsets)
2975 vsi->offset = sym->st_value;
2976
2977 /* if variable is a global/weak symbol, but has restricted
2978 * (STV_HIDDEN or STV_INTERNAL) visibility, mark its BTF VAR
2979 * as static. This follows similar logic for functions (BPF
2980 * subprogs) and influences libbpf's further decisions about
2981 * whether to make global data BPF array maps as
2982 * BPF_F_MMAPABLE.
2983 */
2984 if (ELF64_ST_VISIBILITY(sym->st_other) == STV_HIDDEN
2985 || ELF64_ST_VISIBILITY(sym->st_other) == STV_INTERNAL)
2986 var->linkage = BTF_VAR_STATIC;
b96c07f3
AN
2987 }
2988
2989sort_vars:
2990 qsort(btf_var_secinfos(t), vars, sizeof(*vsi), compare_vsi_off);
2991 return 0;
2992}
2993
f33f742d 2994static int bpf_object_fixup_btf(struct bpf_object *obj)
b96c07f3 2995{
f33f742d 2996 int i, n, err = 0;
b96c07f3 2997
f33f742d
AN
2998 if (!obj->btf)
2999 return 0;
3000
3001 n = btf__type_cnt(obj->btf);
6a886de0 3002 for (i = 1; i < n; i++) {
f33f742d 3003 struct btf_type *t = btf_type_by_id(obj->btf, i);
b96c07f3
AN
3004
3005 /* Loader needs to fix up some of the things compiler
3006 * couldn't get its hands on while emitting BTF. This
3007 * is section size and global variable offset. We use
3008 * the info from the ELF itself for this purpose.
3009 */
3010 if (btf_is_datasec(t)) {
f33f742d 3011 err = btf_fixup_datasec(obj, obj->btf, t);
b96c07f3 3012 if (err)
f33f742d 3013 return err;
b96c07f3
AN
3014 }
3015 }
3016
166750bc
AN
3017 return 0;
3018}
3019
fe62de31 3020static bool prog_needs_vmlinux_btf(struct bpf_program *prog)
a6ed02ca 3021{
1e092a03
KS
3022 if (prog->type == BPF_PROG_TYPE_STRUCT_OPS ||
3023 prog->type == BPF_PROG_TYPE_LSM)
a6ed02ca
KS
3024 return true;
3025
3026 /* BPF_PROG_TYPE_TRACING programs which do not attach to other programs
3027 * also need vmlinux BTF
3028 */
3029 if (prog->type == BPF_PROG_TYPE_TRACING && !prog->attach_prog_fd)
3030 return true;
3031
3032 return false;
3033}
3034
fe62de31 3035static bool obj_needs_vmlinux_btf(const struct bpf_object *obj)
a6ed02ca
KS
3036{
3037 struct bpf_program *prog;
fe62de31 3038 int i;
a6ed02ca 3039
1373ff59
SC
3040 /* CO-RE relocations need kernel BTF, only when btf_custom_path
3041 * is not specified
3042 */
3043 if (obj->btf_ext && obj->btf_ext->core_relo_info.len && !obj->btf_custom_path)
fe62de31 3044 return true;
192b6638 3045
d370bbe1
HL
3046 /* Support for typed ksyms needs kernel BTF */
3047 for (i = 0; i < obj->nr_extern; i++) {
3048 const struct extern_desc *ext;
3049
3050 ext = &obj->externs[i];
fe62de31
AN
3051 if (ext->type == EXT_KSYM && ext->ksym.type_id)
3052 return true;
d370bbe1
HL
3053 }
3054
a6ed02ca 3055 bpf_object__for_each_program(prog, obj) {
a3820c48 3056 if (!prog->autoload)
d9297581 3057 continue;
fe62de31
AN
3058 if (prog_needs_vmlinux_btf(prog))
3059 return true;
a6ed02ca
KS
3060 }
3061
fe62de31
AN
3062 return false;
3063}
3064
3065static int bpf_object__load_vmlinux_btf(struct bpf_object *obj, bool force)
3066{
3067 int err;
3068
3069 /* btf_vmlinux could be loaded earlier */
67234743 3070 if (obj->btf_vmlinux || obj->gen_loader)
fe62de31
AN
3071 return 0;
3072
3073 if (!force && !obj_needs_vmlinux_btf(obj))
192b6638
AN
3074 return 0;
3075
a710eed3 3076 obj->btf_vmlinux = btf__load_vmlinux_btf();
e9fc3ce9
AN
3077 err = libbpf_get_error(obj->btf_vmlinux);
3078 if (err) {
192b6638
AN
3079 pr_warn("Error loading vmlinux BTF: %d\n", err);
3080 obj->btf_vmlinux = NULL;
3081 return err;
3082 }
a6ed02ca
KS
3083 return 0;
3084}
3085
063183bf
AN
3086static int bpf_object__sanitize_and_load_btf(struct bpf_object *obj)
3087{
0f0e55d8
AN
3088 struct btf *kern_btf = obj->btf;
3089 bool btf_mandatory, sanitize;
aea28a60 3090 int i, err = 0;
063183bf
AN
3091
3092 if (!obj->btf)
3093 return 0;
3094
9ca1f56a 3095 if (!kernel_supports(obj, FEAT_BTF)) {
68b08647
AN
3096 if (kernel_needs_btf(obj)) {
3097 err = -EOPNOTSUPP;
3098 goto report;
3099 }
3100 pr_debug("Kernel doesn't support BTF, skipping uploading it.\n");
3101 return 0;
3102 }
3103
aea28a60
AN
3104 /* Even though some subprogs are global/weak, user might prefer more
3105 * permissive BPF verification process that BPF verifier performs for
3106 * static functions, taking into account more context from the caller
3107 * functions. In such case, they need to mark such subprogs with
3108 * __attribute__((visibility("hidden"))) and libbpf will adjust
3109 * corresponding FUNC BTF type to be marked as static and trigger more
3110 * involved BPF verification process.
3111 */
3112 for (i = 0; i < obj->nr_programs; i++) {
3113 struct bpf_program *prog = &obj->programs[i];
3114 struct btf_type *t;
3115 const char *name;
3116 int j, n;
3117
3118 if (!prog->mark_btf_static || !prog_is_subprog(obj, prog))
3119 continue;
3120
6a886de0
HC
3121 n = btf__type_cnt(obj->btf);
3122 for (j = 1; j < n; j++) {
aea28a60
AN
3123 t = btf_type_by_id(obj->btf, j);
3124 if (!btf_is_func(t) || btf_func_linkage(t) != BTF_FUNC_GLOBAL)
3125 continue;
3126
3127 name = btf__str_by_offset(obj->btf, t->name_off);
3128 if (strcmp(name, prog->name) != 0)
3129 continue;
3130
3131 t->info = btf_type_info(BTF_KIND_FUNC, BTF_FUNC_STATIC, 0);
3132 break;
3133 }
3134 }
3135
0f0e55d8
AN
3136 sanitize = btf_needs_sanitization(obj);
3137 if (sanitize) {
5c3320d7 3138 const void *raw_data;
0f0e55d8 3139 __u32 sz;
063183bf 3140
0f0e55d8 3141 /* clone BTF to sanitize a copy and leave the original intact */
6a886de0 3142 raw_data = btf__raw_data(obj->btf, &sz);
5c3320d7 3143 kern_btf = btf__new(raw_data, sz);
e9fc3ce9
AN
3144 err = libbpf_get_error(kern_btf);
3145 if (err)
3146 return err;
04efe591 3147
4c01925f
AN
3148 /* enforce 8-byte pointers for BPF-targeted BTFs */
3149 btf__set_pointer_size(obj->btf, 8);
f2a62588
YS
3150 err = bpf_object__sanitize_btf(obj, kern_btf);
3151 if (err)
3152 return err;
063183bf 3153 }
0f0e55d8 3154
67234743
AS
3155 if (obj->gen_loader) {
3156 __u32 raw_size = 0;
6a886de0 3157 const void *raw_data = btf__raw_data(kern_btf, &raw_size);
67234743
AS
3158
3159 if (!raw_data)
3160 return -ENOMEM;
3161 bpf_gen__load_btf(obj->gen_loader, raw_data, raw_size);
3162 /* Pretend to have valid FD to pass various fd >= 0 checks.
3163 * This fd == 0 will not be used with any syscall and will be reset to -1 eventually.
3164 */
3165 btf__set_fd(kern_btf, 0);
3166 } else {
e0e3ea88
AN
3167 /* currently BPF_BTF_LOAD only supports log_level 1 */
3168 err = btf_load_into_kernel(kern_btf, obj->log_buf, obj->log_size,
3169 obj->log_level ? 1 : 0);
67234743 3170 }
0f0e55d8
AN
3171 if (sanitize) {
3172 if (!err) {
3173 /* move fd to libbpf's BTF */
3174 btf__set_fd(obj->btf, btf__fd(kern_btf));
3175 btf__set_fd(kern_btf, -1);
3176 }
3177 btf__free(kern_btf);
3178 }
68b08647 3179report:
0f0e55d8
AN
3180 if (err) {
3181 btf_mandatory = kernel_needs_btf(obj);
3182 pr_warn("Error loading .BTF into kernel: %d. %s\n", err,
3183 btf_mandatory ? "BTF is mandatory, can't proceed."
3184 : "BTF is optional, ignoring.");
3185 if (!btf_mandatory)
3186 err = 0;
3187 }
3188 return err;
063183bf
AN
3189}
3190
88a82120
AN
3191static const char *elf_sym_str(const struct bpf_object *obj, size_t off)
3192{
3193 const char *name;
3194
3195 name = elf_strptr(obj->efile.elf, obj->efile.strtabidx, off);
3196 if (!name) {
3197 pr_warn("elf: failed to get section name string at offset %zu from %s: %s\n",
3198 off, obj->path, elf_errmsg(-1));
3199 return NULL;
3200 }
3201
3202 return name;
3203}
3204
3205static const char *elf_sec_str(const struct bpf_object *obj, size_t off)
3206{
3207 const char *name;
3208
3209 name = elf_strptr(obj->efile.elf, obj->efile.shstrndx, off);
3210 if (!name) {
3211 pr_warn("elf: failed to get section name string at offset %zu from %s: %s\n",
3212 off, obj->path, elf_errmsg(-1));
3213 return NULL;
3214 }
3215
3216 return name;
3217}
3218
3219static Elf_Scn *elf_sec_by_idx(const struct bpf_object *obj, size_t idx)
3220{
3221 Elf_Scn *scn;
3222
3223 scn = elf_getscn(obj->efile.elf, idx);
3224 if (!scn) {
3225 pr_warn("elf: failed to get section(%zu) from %s: %s\n",
3226 idx, obj->path, elf_errmsg(-1));
3227 return NULL;
3228 }
3229 return scn;
3230}
3231
3232static Elf_Scn *elf_sec_by_name(const struct bpf_object *obj, const char *name)
3233{
3234 Elf_Scn *scn = NULL;
3235 Elf *elf = obj->efile.elf;
3236 const char *sec_name;
3237
3238 while ((scn = elf_nextscn(elf, scn)) != NULL) {
3239 sec_name = elf_sec_name(obj, scn);
3240 if (!sec_name)
3241 return NULL;
3242
3243 if (strcmp(sec_name, name) != 0)
3244 continue;
3245
3246 return scn;
3247 }
3248 return NULL;
3249}
3250
ad23b723 3251static Elf64_Shdr *elf_sec_hdr(const struct bpf_object *obj, Elf_Scn *scn)
88a82120 3252{
ad23b723
AN
3253 Elf64_Shdr *shdr;
3254
88a82120 3255 if (!scn)
ad23b723 3256 return NULL;
88a82120 3257
ad23b723
AN
3258 shdr = elf64_getshdr(scn);
3259 if (!shdr) {
88a82120
AN
3260 pr_warn("elf: failed to get section(%zu) header from %s: %s\n",
3261 elf_ndxscn(scn), obj->path, elf_errmsg(-1));
ad23b723 3262 return NULL;
88a82120
AN
3263 }
3264
ad23b723 3265 return shdr;
88a82120
AN
3266}
3267
3268static const char *elf_sec_name(const struct bpf_object *obj, Elf_Scn *scn)
3269{
3270 const char *name;
ad23b723 3271 Elf64_Shdr *sh;
88a82120
AN
3272
3273 if (!scn)
3274 return NULL;
3275
ad23b723
AN
3276 sh = elf_sec_hdr(obj, scn);
3277 if (!sh)
88a82120
AN
3278 return NULL;
3279
ad23b723 3280 name = elf_sec_str(obj, sh->sh_name);
88a82120
AN
3281 if (!name) {
3282 pr_warn("elf: failed to get section(%zu) name from %s: %s\n",
3283 elf_ndxscn(scn), obj->path, elf_errmsg(-1));
3284 return NULL;
3285 }
3286
3287 return name;
3288}
3289
3290static Elf_Data *elf_sec_data(const struct bpf_object *obj, Elf_Scn *scn)
3291{
3292 Elf_Data *data;
3293
3294 if (!scn)
3295 return NULL;
3296
3297 data = elf_getdata(scn, 0);
3298 if (!data) {
3299 pr_warn("elf: failed to get section(%zu) %s data from %s: %s\n",
3300 elf_ndxscn(scn), elf_sec_name(obj, scn) ?: "<?>",
3301 obj->path, elf_errmsg(-1));
3302 return NULL;
3303 }
3304
3305 return data;
3306}
3307
ad23b723
AN
3308static Elf64_Sym *elf_sym_by_idx(const struct bpf_object *obj, size_t idx)
3309{
3310 if (idx >= obj->efile.symbols->d_size / sizeof(Elf64_Sym))
3311 return NULL;
3312
3313 return (Elf64_Sym *)obj->efile.symbols->d_buf + idx;
3314}
3315
3316static Elf64_Rel *elf_rel_by_idx(Elf_Data *data, size_t idx)
3317{
3318 if (idx >= data->d_size / sizeof(Elf64_Rel))
3319 return NULL;
3320
3321 return (Elf64_Rel *)data->d_buf + idx;
3322}
3323
50e09460
AN
3324static bool is_sec_name_dwarf(const char *name)
3325{
3326 /* approximation, but the actual list is too long */
13d35a0c 3327 return str_has_pfx(name, ".debug_");
50e09460
AN
3328}
3329
ad23b723 3330static bool ignore_elf_section(Elf64_Shdr *hdr, const char *name)
50e09460
AN
3331{
3332 /* no special handling of .strtab */
3333 if (hdr->sh_type == SHT_STRTAB)
3334 return true;
3335
3336 /* ignore .llvm_addrsig section as well */
faf6ed32 3337 if (hdr->sh_type == SHT_LLVM_ADDRSIG)
50e09460
AN
3338 return true;
3339
3340 /* no subprograms will lead to an empty .text section, ignore it */
3341 if (hdr->sh_type == SHT_PROGBITS && hdr->sh_size == 0 &&
3342 strcmp(name, ".text") == 0)
3343 return true;
3344
3345 /* DWARF sections */
3346 if (is_sec_name_dwarf(name))
3347 return true;
3348
13d35a0c 3349 if (str_has_pfx(name, ".rel")) {
50e09460
AN
3350 name += sizeof(".rel") - 1;
3351 /* DWARF section relocations */
3352 if (is_sec_name_dwarf(name))
3353 return true;
3354
3355 /* .BTF and .BTF.ext don't need relocations */
3356 if (strcmp(name, BTF_ELF_SEC) == 0 ||
3357 strcmp(name, BTF_EXT_ELF_SEC) == 0)
3358 return true;
3359 }
3360
3361 return false;
3362}
3363
db2b8b06
AN
3364static int cmp_progs(const void *_a, const void *_b)
3365{
3366 const struct bpf_program *a = _a;
3367 const struct bpf_program *b = _b;
3368
3369 if (a->sec_idx != b->sec_idx)
3370 return a->sec_idx < b->sec_idx ? -1 : 1;
3371
3372 /* sec_insn_off can't be the same within the section */
3373 return a->sec_insn_off < b->sec_insn_off ? -1 : 1;
3374}
3375
0d13bfce 3376static int bpf_object__elf_collect(struct bpf_object *obj)
29603665 3377{
25bbbd7a 3378 struct elf_sec_desc *sec_desc;
29603665 3379 Elf *elf = obj->efile.elf;
f0187f0b 3380 Elf_Data *btf_ext_data = NULL;
1713d68b 3381 Elf_Data *btf_data = NULL;
666810e8 3382 int idx = 0, err = 0;
0201c575
AN
3383 const char *name;
3384 Elf_Data *data;
3385 Elf_Scn *scn;
ad23b723 3386 Elf64_Shdr *sh;
29603665 3387
0d6988e1 3388 /* ELF section indices are 0-based, but sec #0 is special "invalid"
51deedc9
SHY
3389 * section. Since section count retrieved by elf_getshdrnum() does
3390 * include sec #0, it is already the necessary size of an array to keep
3391 * all the sections.
25bbbd7a 3392 */
51deedc9
SHY
3393 if (elf_getshdrnum(obj->efile.elf, &obj->efile.sec_cnt)) {
3394 pr_warn("elf: failed to get the number of sections for %s: %s\n",
3395 obj->path, elf_errmsg(-1));
3396 return -LIBBPF_ERRNO__FORMAT;
3397 }
25bbbd7a
AN
3398 obj->efile.secs = calloc(obj->efile.sec_cnt, sizeof(*obj->efile.secs));
3399 if (!obj->efile.secs)
3400 return -ENOMEM;
29603665 3401
0201c575
AN
3402 /* a bunch of ELF parsing functionality depends on processing symbols,
3403 * so do the first pass and find the symbol table
3404 */
3405 scn = NULL;
29603665 3406 while ((scn = elf_nextscn(elf, scn)) != NULL) {
ad23b723
AN
3407 sh = elf_sec_hdr(obj, scn);
3408 if (!sh)
0201c575
AN
3409 return -LIBBPF_ERRNO__FORMAT;
3410
ad23b723 3411 if (sh->sh_type == SHT_SYMTAB) {
0201c575
AN
3412 if (obj->efile.symbols) {
3413 pr_warn("elf: multiple symbol tables in %s\n", obj->path);
3414 return -LIBBPF_ERRNO__FORMAT;
3415 }
29603665 3416
0201c575
AN
3417 data = elf_sec_data(obj, scn);
3418 if (!data)
3419 return -LIBBPF_ERRNO__FORMAT;
3420
25bbbd7a
AN
3421 idx = elf_ndxscn(scn);
3422
0201c575 3423 obj->efile.symbols = data;
25bbbd7a 3424 obj->efile.symbols_shndx = idx;
ad23b723 3425 obj->efile.strtabidx = sh->sh_link;
0201c575
AN
3426 }
3427 }
3428
03e601f4
THJ
3429 if (!obj->efile.symbols) {
3430 pr_warn("elf: couldn't find symbol table in %s, stripped object file?\n",
3431 obj->path);
3432 return -ENOENT;
3433 }
3434
0201c575
AN
3435 scn = NULL;
3436 while ((scn = elf_nextscn(elf, scn)) != NULL) {
25bbbd7a
AN
3437 idx = elf_ndxscn(scn);
3438 sec_desc = &obj->efile.secs[idx];
88a82120 3439
ad23b723
AN
3440 sh = elf_sec_hdr(obj, scn);
3441 if (!sh)
01b29d1d 3442 return -LIBBPF_ERRNO__FORMAT;
29603665 3443
ad23b723 3444 name = elf_sec_str(obj, sh->sh_name);
88a82120 3445 if (!name)
01b29d1d 3446 return -LIBBPF_ERRNO__FORMAT;
29603665 3447
ad23b723 3448 if (ignore_elf_section(sh, name))
50e09460
AN
3449 continue;
3450
88a82120
AN
3451 data = elf_sec_data(obj, scn);
3452 if (!data)
01b29d1d 3453 return -LIBBPF_ERRNO__FORMAT;
88a82120
AN
3454
3455 pr_debug("elf: section(%d) %s, size %ld, link %d, flags %lx, type=%d\n",
077c066a 3456 idx, name, (unsigned long)data->d_size,
ad23b723
AN
3457 (int)sh->sh_link, (unsigned long)sh->sh_flags,
3458 (int)sh->sh_type);
cb1e5e96 3459
1713d68b 3460 if (strcmp(name, "license") == 0) {
88a82120 3461 err = bpf_object__init_license(obj, data->d_buf, data->d_size);
01b29d1d
AN
3462 if (err)
3463 return err;
1713d68b 3464 } else if (strcmp(name, "version") == 0) {
88a82120 3465 err = bpf_object__init_kversion(obj, data->d_buf, data->d_size);
54b8625c
JF
3466 if (err)
3467 return err;
1713d68b 3468 } else if (strcmp(name, "maps") == 0) {
e19db676
AN
3469 pr_warn("elf: legacy map definitions in 'maps' section are not supported by libbpf v1.0+\n");
3470 return -ENOTSUP;
abd29c93
AN
3471 } else if (strcmp(name, MAPS_ELF_SEC) == 0) {
3472 obj->efile.btf_maps_shndx = idx;
1713d68b 3473 } else if (strcmp(name, BTF_ELF_SEC) == 0) {
62554d52
AN
3474 if (sh->sh_type != SHT_PROGBITS)
3475 return -LIBBPF_ERRNO__FORMAT;
1713d68b 3476 btf_data = data;
2993e051 3477 } else if (strcmp(name, BTF_EXT_ELF_SEC) == 0) {
62554d52
AN
3478 if (sh->sh_type != SHT_PROGBITS)
3479 return -LIBBPF_ERRNO__FORMAT;
f0187f0b 3480 btf_ext_data = data;
ad23b723 3481 } else if (sh->sh_type == SHT_SYMTAB) {
0201c575 3482 /* already processed during the first pass above */
ad23b723
AN
3483 } else if (sh->sh_type == SHT_PROGBITS && data->d_size > 0) {
3484 if (sh->sh_flags & SHF_EXECINSTR) {
f8c7a4d4
JS
3485 if (strcmp(name, ".text") == 0)
3486 obj->efile.text_shndx = idx;
c1122392 3487 err = bpf_object__add_programs(obj, data, name, idx);
88a82120 3488 if (err)
01b29d1d 3489 return err;
aed65917
AN
3490 } else if (strcmp(name, DATA_SEC) == 0 ||
3491 str_has_pfx(name, DATA_SEC ".")) {
25bbbd7a
AN
3492 sec_desc->sec_type = SEC_DATA;
3493 sec_desc->shdr = sh;
3494 sec_desc->data = data;
aed65917
AN
3495 } else if (strcmp(name, RODATA_SEC) == 0 ||
3496 str_has_pfx(name, RODATA_SEC ".")) {
25bbbd7a
AN
3497 sec_desc->sec_type = SEC_RODATA;
3498 sec_desc->shdr = sh;
3499 sec_desc->data = data;
590a0088
MKL
3500 } else if (strcmp(name, STRUCT_OPS_SEC) == 0) {
3501 obj->efile.st_ops_data = data;
3502 obj->efile.st_ops_shndx = idx;
809a69d6
KFL
3503 } else if (strcmp(name, STRUCT_OPS_LINK_SEC) == 0) {
3504 obj->efile.st_ops_link_data = data;
3505 obj->efile.st_ops_link_shndx = idx;
d859900c 3506 } else {
50e09460
AN
3507 pr_info("elf: skipping unrecognized data section(%d) %s\n",
3508 idx, name);
a5b8bd47 3509 }
ad23b723 3510 } else if (sh->sh_type == SHT_REL) {
25bbbd7a 3511 int targ_sec_idx = sh->sh_info; /* points to other section */
e3d91b0c 3512
b7332d28
AN
3513 if (sh->sh_entsize != sizeof(Elf64_Rel) ||
3514 targ_sec_idx >= obj->efile.sec_cnt)
3515 return -LIBBPF_ERRNO__FORMAT;
3516
e3d91b0c 3517 /* Only do relo for section with exec instructions */
25bbbd7a 3518 if (!section_have_execinstr(obj, targ_sec_idx) &&
646f02ff 3519 strcmp(name, ".rel" STRUCT_OPS_SEC) &&
809a69d6 3520 strcmp(name, ".rel" STRUCT_OPS_LINK_SEC) &&
646f02ff 3521 strcmp(name, ".rel" MAPS_ELF_SEC)) {
50e09460 3522 pr_info("elf: skipping relo section(%d) %s for section(%d) %s\n",
25bbbd7a
AN
3523 idx, name, targ_sec_idx,
3524 elf_sec_name(obj, elf_sec_by_idx(obj, targ_sec_idx)) ?: "<?>");
e3d91b0c
JDB
3525 continue;
3526 }
b62f06e8 3527
25bbbd7a
AN
3528 sec_desc->sec_type = SEC_RELO;
3529 sec_desc->shdr = sh;
3530 sec_desc->data = data;
dc79f035
AS
3531 } else if (sh->sh_type == SHT_NOBITS && (strcmp(name, BSS_SEC) == 0 ||
3532 str_has_pfx(name, BSS_SEC "."))) {
25bbbd7a
AN
3533 sec_desc->sec_type = SEC_BSS;
3534 sec_desc->shdr = sh;
3535 sec_desc->data = data;
077c066a 3536 } else {
2e80be60 3537 pr_info("elf: skipping section(%d) %s (size %zu)\n", idx, name,
ad23b723 3538 (size_t)sh->sh_size);
bec7d68c 3539 }
29603665 3540 }
561bbcca 3541
d3a3aa0c 3542 if (!obj->efile.strtabidx || obj->efile.strtabidx > idx) {
88a82120 3543 pr_warn("elf: symbol strings section missing or invalid in %s\n", obj->path);
f102154d 3544 return -LIBBPF_ERRNO__FORMAT;
77ba9a5b 3545 }
db2b8b06
AN
3546
3547 /* sort BPF programs by section name and in-section instruction offset
e3ba8e4e
KM
3548 * for faster search
3549 */
2a6a9bf2
AN
3550 if (obj->nr_programs)
3551 qsort(obj->programs, obj->nr_programs, sizeof(*obj->programs), cmp_progs);
db2b8b06 3552
0d13bfce 3553 return bpf_object__init_btf(obj, btf_data, btf_ext_data);
29603665
WN
3554}
3555
ad23b723 3556static bool sym_is_extern(const Elf64_Sym *sym)
166750bc 3557{
ad23b723 3558 int bind = ELF64_ST_BIND(sym->st_info);
166750bc
AN
3559 /* externs are symbols w/ type=NOTYPE, bind=GLOBAL|WEAK, section=UND */
3560 return sym->st_shndx == SHN_UNDEF &&
3561 (bind == STB_GLOBAL || bind == STB_WEAK) &&
ad23b723 3562 ELF64_ST_TYPE(sym->st_info) == STT_NOTYPE;
166750bc
AN
3563}
3564
ad23b723 3565static bool sym_is_subprog(const Elf64_Sym *sym, int text_shndx)
53eddb5e 3566{
ad23b723
AN
3567 int bind = ELF64_ST_BIND(sym->st_info);
3568 int type = ELF64_ST_TYPE(sym->st_info);
53eddb5e
YS
3569
3570 /* in .text section */
3571 if (sym->st_shndx != text_shndx)
3572 return false;
3573
3574 /* local function */
3575 if (bind == STB_LOCAL && type == STT_SECTION)
3576 return true;
3577
3578 /* global function */
3579 return bind == STB_GLOBAL && type == STT_FUNC;
3580}
3581
166750bc
AN
3582static int find_extern_btf_id(const struct btf *btf, const char *ext_name)
3583{
3584 const struct btf_type *t;
5bd022ec 3585 const char *tname;
166750bc
AN
3586 int i, n;
3587
3588 if (!btf)
3589 return -ESRCH;
3590
6a886de0
HC
3591 n = btf__type_cnt(btf);
3592 for (i = 1; i < n; i++) {
166750bc
AN
3593 t = btf__type_by_id(btf, i);
3594
5bd022ec 3595 if (!btf_is_var(t) && !btf_is_func(t))
166750bc
AN
3596 continue;
3597
5bd022ec
MKL
3598 tname = btf__name_by_offset(btf, t->name_off);
3599 if (strcmp(tname, ext_name))
166750bc
AN
3600 continue;
3601
5bd022ec
MKL
3602 if (btf_is_var(t) &&
3603 btf_var(t)->linkage != BTF_VAR_GLOBAL_EXTERN)
3604 return -EINVAL;
3605
3606 if (btf_is_func(t) && btf_func_linkage(t) != BTF_FUNC_EXTERN)
166750bc
AN
3607 return -EINVAL;
3608
3609 return i;
3610 }
3611
3612 return -ENOENT;
3613}
3614
2e33efe3
AN
3615static int find_extern_sec_btf_id(struct btf *btf, int ext_btf_id) {
3616 const struct btf_var_secinfo *vs;
3617 const struct btf_type *t;
3618 int i, j, n;
3619
3620 if (!btf)
3621 return -ESRCH;
3622
6a886de0
HC
3623 n = btf__type_cnt(btf);
3624 for (i = 1; i < n; i++) {
2e33efe3
AN
3625 t = btf__type_by_id(btf, i);
3626
3627 if (!btf_is_datasec(t))
3628 continue;
3629
3630 vs = btf_var_secinfos(t);
3631 for (j = 0; j < btf_vlen(t); j++, vs++) {
3632 if (vs->type == ext_btf_id)
3633 return i;
3634 }
3635 }
3636
3637 return -ENOENT;
3638}
3639
3640static enum kcfg_type find_kcfg_type(const struct btf *btf, int id,
3641 bool *is_signed)
166750bc
AN
3642{
3643 const struct btf_type *t;
3644 const char *name;
3645
3646 t = skip_mods_and_typedefs(btf, id, NULL);
3647 name = btf__name_by_offset(btf, t->name_off);
3648
3649 if (is_signed)
3650 *is_signed = false;
3651 switch (btf_kind(t)) {
3652 case BTF_KIND_INT: {
3653 int enc = btf_int_encoding(t);
3654
3655 if (enc & BTF_INT_BOOL)
2e33efe3 3656 return t->size == 1 ? KCFG_BOOL : KCFG_UNKNOWN;
166750bc
AN
3657 if (is_signed)
3658 *is_signed = enc & BTF_INT_SIGNED;
3659 if (t->size == 1)
2e33efe3 3660 return KCFG_CHAR;
166750bc 3661 if (t->size < 1 || t->size > 8 || (t->size & (t->size - 1)))
2e33efe3
AN
3662 return KCFG_UNKNOWN;
3663 return KCFG_INT;
166750bc
AN
3664 }
3665 case BTF_KIND_ENUM:
3666 if (t->size != 4)
2e33efe3 3667 return KCFG_UNKNOWN;
166750bc 3668 if (strcmp(name, "libbpf_tristate"))
2e33efe3
AN
3669 return KCFG_UNKNOWN;
3670 return KCFG_TRISTATE;
f2a62588
YS
3671 case BTF_KIND_ENUM64:
3672 if (strcmp(name, "libbpf_tristate"))
3673 return KCFG_UNKNOWN;
3674 return KCFG_TRISTATE;
166750bc
AN
3675 case BTF_KIND_ARRAY:
3676 if (btf_array(t)->nelems == 0)
2e33efe3
AN
3677 return KCFG_UNKNOWN;
3678 if (find_kcfg_type(btf, btf_array(t)->type, NULL) != KCFG_CHAR)
3679 return KCFG_UNKNOWN;
3680 return KCFG_CHAR_ARR;
166750bc 3681 default:
2e33efe3 3682 return KCFG_UNKNOWN;
166750bc
AN
3683 }
3684}
3685
3686static int cmp_externs(const void *_a, const void *_b)
3687{
3688 const struct extern_desc *a = _a;
3689 const struct extern_desc *b = _b;
3690
2e33efe3
AN
3691 if (a->type != b->type)
3692 return a->type < b->type ? -1 : 1;
3693
3694 if (a->type == EXT_KCFG) {
3695 /* descending order by alignment requirements */
3696 if (a->kcfg.align != b->kcfg.align)
3697 return a->kcfg.align > b->kcfg.align ? -1 : 1;
3698 /* ascending order by size, within same alignment class */
3699 if (a->kcfg.sz != b->kcfg.sz)
3700 return a->kcfg.sz < b->kcfg.sz ? -1 : 1;
3701 }
3702
166750bc
AN
3703 /* resolve ties by name */
3704 return strcmp(a->name, b->name);
3705}
3706
1c0c7074
AN
3707static int find_int_btf_id(const struct btf *btf)
3708{
3709 const struct btf_type *t;
3710 int i, n;
3711
6a886de0
HC
3712 n = btf__type_cnt(btf);
3713 for (i = 1; i < n; i++) {
1c0c7074
AN
3714 t = btf__type_by_id(btf, i);
3715
3716 if (btf_is_int(t) && btf_int_bits(t) == 32)
3717 return i;
3718 }
3719
3720 return 0;
3721}
3722
5bd022ec
MKL
3723static int add_dummy_ksym_var(struct btf *btf)
3724{
3725 int i, int_btf_id, sec_btf_id, dummy_var_btf_id;
3726 const struct btf_var_secinfo *vs;
3727 const struct btf_type *sec;
3728
9683e577
IR
3729 if (!btf)
3730 return 0;
3731
5bd022ec
MKL
3732 sec_btf_id = btf__find_by_name_kind(btf, KSYMS_SEC,
3733 BTF_KIND_DATASEC);
3734 if (sec_btf_id < 0)
3735 return 0;
3736
3737 sec = btf__type_by_id(btf, sec_btf_id);
3738 vs = btf_var_secinfos(sec);
3739 for (i = 0; i < btf_vlen(sec); i++, vs++) {
3740 const struct btf_type *vt;
3741
3742 vt = btf__type_by_id(btf, vs->type);
3743 if (btf_is_func(vt))
3744 break;
3745 }
3746
3747 /* No func in ksyms sec. No need to add dummy var. */
3748 if (i == btf_vlen(sec))
3749 return 0;
3750
3751 int_btf_id = find_int_btf_id(btf);
3752 dummy_var_btf_id = btf__add_var(btf,
3753 "dummy_ksym",
3754 BTF_VAR_GLOBAL_ALLOCATED,
3755 int_btf_id);
3756 if (dummy_var_btf_id < 0)
3757 pr_warn("cannot create a dummy_ksym var\n");
3758
3759 return dummy_var_btf_id;
3760}
3761
166750bc
AN
3762static int bpf_object__collect_externs(struct bpf_object *obj)
3763{
1c0c7074 3764 struct btf_type *sec, *kcfg_sec = NULL, *ksym_sec = NULL;
166750bc
AN
3765 const struct btf_type *t;
3766 struct extern_desc *ext;
5bd022ec 3767 int i, n, off, dummy_var_btf_id;
2e33efe3 3768 const char *ext_name, *sec_name;
166750bc 3769 Elf_Scn *scn;
ad23b723 3770 Elf64_Shdr *sh;
166750bc
AN
3771
3772 if (!obj->efile.symbols)
3773 return 0;
3774
88a82120 3775 scn = elf_sec_by_idx(obj, obj->efile.symbols_shndx);
ad23b723 3776 sh = elf_sec_hdr(obj, scn);
83390787 3777 if (!sh || sh->sh_entsize != sizeof(Elf64_Sym))
166750bc 3778 return -LIBBPF_ERRNO__FORMAT;
166750bc 3779
5bd022ec
MKL
3780 dummy_var_btf_id = add_dummy_ksym_var(obj->btf);
3781 if (dummy_var_btf_id < 0)
3782 return dummy_var_btf_id;
3783
ad23b723 3784 n = sh->sh_size / sh->sh_entsize;
166750bc 3785 pr_debug("looking for externs among %d symbols...\n", n);
88a82120 3786
166750bc 3787 for (i = 0; i < n; i++) {
ad23b723 3788 Elf64_Sym *sym = elf_sym_by_idx(obj, i);
166750bc 3789
ad23b723 3790 if (!sym)
166750bc 3791 return -LIBBPF_ERRNO__FORMAT;
ad23b723 3792 if (!sym_is_extern(sym))
166750bc 3793 continue;
ad23b723 3794 ext_name = elf_sym_str(obj, sym->st_name);
166750bc
AN
3795 if (!ext_name || !ext_name[0])
3796 continue;
3797
3798 ext = obj->externs;
029258d7 3799 ext = libbpf_reallocarray(ext, obj->nr_extern + 1, sizeof(*ext));
166750bc
AN
3800 if (!ext)
3801 return -ENOMEM;
3802 obj->externs = ext;
3803 ext = &ext[obj->nr_extern];
3804 memset(ext, 0, sizeof(*ext));
3805 obj->nr_extern++;
3806
3807 ext->btf_id = find_extern_btf_id(obj->btf, ext_name);
3808 if (ext->btf_id <= 0) {
3809 pr_warn("failed to find BTF for extern '%s': %d\n",
3810 ext_name, ext->btf_id);
3811 return ext->btf_id;
3812 }
3813 t = btf__type_by_id(obj->btf, ext->btf_id);
3814 ext->name = btf__name_by_offset(obj->btf, t->name_off);
3815 ext->sym_idx = i;
ad23b723 3816 ext->is_weak = ELF64_ST_BIND(sym->st_info) == STB_WEAK;
2e33efe3
AN
3817
3818 ext->sec_btf_id = find_extern_sec_btf_id(obj->btf, ext->btf_id);
3819 if (ext->sec_btf_id <= 0) {
3820 pr_warn("failed to find BTF for extern '%s' [%d] section: %d\n",
3821 ext_name, ext->btf_id, ext->sec_btf_id);
3822 return ext->sec_btf_id;
166750bc 3823 }
2e33efe3
AN
3824 sec = (void *)btf__type_by_id(obj->btf, ext->sec_btf_id);
3825 sec_name = btf__name_by_offset(obj->btf, sec->name_off);
3826
3827 if (strcmp(sec_name, KCONFIG_SEC) == 0) {
5bd022ec
MKL
3828 if (btf_is_func(t)) {
3829 pr_warn("extern function %s is unsupported under %s section\n",
3830 ext->name, KCONFIG_SEC);
3831 return -ENOTSUP;
3832 }
2e33efe3
AN
3833 kcfg_sec = sec;
3834 ext->type = EXT_KCFG;
3835 ext->kcfg.sz = btf__resolve_size(obj->btf, t->type);
3836 if (ext->kcfg.sz <= 0) {
3837 pr_warn("failed to resolve size of extern (kcfg) '%s': %d\n",
3838 ext_name, ext->kcfg.sz);
3839 return ext->kcfg.sz;
3840 }
3841 ext->kcfg.align = btf__align_of(obj->btf, t->type);
3842 if (ext->kcfg.align <= 0) {
3843 pr_warn("failed to determine alignment of extern (kcfg) '%s': %d\n",
3844 ext_name, ext->kcfg.align);
3845 return -EINVAL;
3846 }
3847 ext->kcfg.type = find_kcfg_type(obj->btf, t->type,
e3ba8e4e 3848 &ext->kcfg.is_signed);
2e33efe3 3849 if (ext->kcfg.type == KCFG_UNKNOWN) {
55d00c37 3850 pr_warn("extern (kcfg) '%s': type is unsupported\n", ext_name);
2e33efe3
AN
3851 return -ENOTSUP;
3852 }
1c0c7074 3853 } else if (strcmp(sec_name, KSYMS_SEC) == 0) {
1c0c7074
AN
3854 ksym_sec = sec;
3855 ext->type = EXT_KSYM;
d370bbe1
HL
3856 skip_mods_and_typedefs(obj->btf, t->type,
3857 &ext->ksym.type_id);
2e33efe3
AN
3858 } else {
3859 pr_warn("unrecognized extern section '%s'\n", sec_name);
166750bc
AN
3860 return -ENOTSUP;
3861 }
3862 }
3863 pr_debug("collected %d externs total\n", obj->nr_extern);
3864
3865 if (!obj->nr_extern)
3866 return 0;
3867
2e33efe3 3868 /* sort externs by type, for kcfg ones also by (align, size, name) */
166750bc 3869 qsort(obj->externs, obj->nr_extern, sizeof(*ext), cmp_externs);
166750bc 3870
1c0c7074
AN
3871 /* for .ksyms section, we need to turn all externs into allocated
3872 * variables in BTF to pass kernel verification; we do this by
3873 * pretending that each extern is a 8-byte variable
3874 */
3875 if (ksym_sec) {
3876 /* find existing 4-byte integer type in BTF to use for fake
3877 * extern variables in DATASEC
3878 */
3879 int int_btf_id = find_int_btf_id(obj->btf);
5bd022ec
MKL
3880 /* For extern function, a dummy_var added earlier
3881 * will be used to replace the vs->type and
3882 * its name string will be used to refill
3883 * the missing param's name.
3884 */
3885 const struct btf_type *dummy_var;
1c0c7074 3886
5bd022ec 3887 dummy_var = btf__type_by_id(obj->btf, dummy_var_btf_id);
1c0c7074
AN
3888 for (i = 0; i < obj->nr_extern; i++) {
3889 ext = &obj->externs[i];
3890 if (ext->type != EXT_KSYM)
3891 continue;
3892 pr_debug("extern (ksym) #%d: symbol %d, name %s\n",
3893 i, ext->sym_idx, ext->name);
3894 }
3895
3896 sec = ksym_sec;
3897 n = btf_vlen(sec);
3898 for (i = 0, off = 0; i < n; i++, off += sizeof(int)) {
3899 struct btf_var_secinfo *vs = btf_var_secinfos(sec) + i;
3900 struct btf_type *vt;
3901
3902 vt = (void *)btf__type_by_id(obj->btf, vs->type);
3903 ext_name = btf__name_by_offset(obj->btf, vt->name_off);
3904 ext = find_extern_by_name(obj, ext_name);
3905 if (!ext) {
5bd022ec
MKL
3906 pr_warn("failed to find extern definition for BTF %s '%s'\n",
3907 btf_kind_str(vt), ext_name);
1c0c7074
AN
3908 return -ESRCH;
3909 }
5bd022ec
MKL
3910 if (btf_is_func(vt)) {
3911 const struct btf_type *func_proto;
3912 struct btf_param *param;
3913 int j;
3914
3915 func_proto = btf__type_by_id(obj->btf,
3916 vt->type);
3917 param = btf_params(func_proto);
3918 /* Reuse the dummy_var string if the
3919 * func proto does not have param name.
3920 */
3921 for (j = 0; j < btf_vlen(func_proto); j++)
3922 if (param[j].type && !param[j].name_off)
3923 param[j].name_off =
3924 dummy_var->name_off;
3925 vs->type = dummy_var_btf_id;
3926 vt->info &= ~0xffff;
3927 vt->info |= BTF_FUNC_GLOBAL;
3928 } else {
3929 btf_var(vt)->linkage = BTF_VAR_GLOBAL_ALLOCATED;
3930 vt->type = int_btf_id;
3931 }
1c0c7074
AN
3932 vs->offset = off;
3933 vs->size = sizeof(int);
3934 }
3935 sec->size = off;
3936 }
3937
2e33efe3
AN
3938 if (kcfg_sec) {
3939 sec = kcfg_sec;
3940 /* for kcfg externs calculate their offsets within a .kconfig map */
3941 off = 0;
3942 for (i = 0; i < obj->nr_extern; i++) {
3943 ext = &obj->externs[i];
3944 if (ext->type != EXT_KCFG)
3945 continue;
166750bc 3946
2e33efe3
AN
3947 ext->kcfg.data_off = roundup(off, ext->kcfg.align);
3948 off = ext->kcfg.data_off + ext->kcfg.sz;
1c0c7074 3949 pr_debug("extern (kcfg) #%d: symbol %d, off %u, name %s\n",
2e33efe3
AN
3950 i, ext->sym_idx, ext->kcfg.data_off, ext->name);
3951 }
3952 sec->size = off;
3953 n = btf_vlen(sec);
3954 for (i = 0; i < n; i++) {
3955 struct btf_var_secinfo *vs = btf_var_secinfos(sec) + i;
3956
3957 t = btf__type_by_id(obj->btf, vs->type);
3958 ext_name = btf__name_by_offset(obj->btf, t->name_off);
3959 ext = find_extern_by_name(obj, ext_name);
3960 if (!ext) {
3961 pr_warn("failed to find extern definition for BTF var '%s'\n",
3962 ext_name);
3963 return -ESRCH;
3964 }
3965 btf_var(t)->linkage = BTF_VAR_GLOBAL_ALLOCATED;
3966 vs->offset = ext->kcfg.data_off;
166750bc 3967 }
166750bc 3968 }
166750bc
AN
3969 return 0;
3970}
3971
bd054102
AN
3972static bool prog_is_subprog(const struct bpf_object *obj, const struct bpf_program *prog)
3973{
197afc63 3974 return prog->sec_idx == obj->efile.text_shndx && obj->nr_programs > 1;
c3c55696
AN
3975}
3976
01af3bf0
AN
3977struct bpf_program *
3978bpf_object__find_program_by_name(const struct bpf_object *obj,
3979 const char *name)
3980{
3981 struct bpf_program *prog;
3982
3983 bpf_object__for_each_program(prog, obj) {
c3c55696
AN
3984 if (prog_is_subprog(obj, prog))
3985 continue;
01af3bf0
AN
3986 if (!strcmp(prog->name, name))
3987 return prog;
3988 }
e9fc3ce9 3989 return errno = ENOENT, NULL;
01af3bf0
AN
3990}
3991
d859900c
DB
3992static bool bpf_object__shndx_is_data(const struct bpf_object *obj,
3993 int shndx)
3994{
25bbbd7a
AN
3995 switch (obj->efile.secs[shndx].sec_type) {
3996 case SEC_BSS:
3997 case SEC_DATA:
3998 case SEC_RODATA:
3999 return true;
4000 default:
4001 return false;
4002 }
d859900c
DB
4003}
4004
4005static bool bpf_object__shndx_is_maps(const struct bpf_object *obj,
4006 int shndx)
4007{
e19db676 4008 return shndx == obj->efile.btf_maps_shndx;
d859900c
DB
4009}
4010
d859900c
DB
4011static enum libbpf_map_type
4012bpf_object__section_to_libbpf_map_type(const struct bpf_object *obj, int shndx)
4013{
25bbbd7a
AN
4014 if (shndx == obj->efile.symbols_shndx)
4015 return LIBBPF_MAP_KCONFIG;
4016
4017 switch (obj->efile.secs[shndx].sec_type) {
4018 case SEC_BSS:
d859900c 4019 return LIBBPF_MAP_BSS;
25bbbd7a
AN
4020 case SEC_DATA:
4021 return LIBBPF_MAP_DATA;
4022 case SEC_RODATA:
d859900c 4023 return LIBBPF_MAP_RODATA;
25bbbd7a 4024 default:
d859900c 4025 return LIBBPF_MAP_UNSPEC;
25bbbd7a 4026 }
d859900c
DB
4027}
4028
1f8e2bcb
AN
4029static int bpf_program__record_reloc(struct bpf_program *prog,
4030 struct reloc_desc *reloc_desc,
9c0f8cbd 4031 __u32 insn_idx, const char *sym_name,
ad23b723 4032 const Elf64_Sym *sym, const Elf64_Rel *rel)
1f8e2bcb
AN
4033{
4034 struct bpf_insn *insn = &prog->insns[insn_idx];
4035 size_t map_idx, nr_maps = prog->obj->nr_maps;
4036 struct bpf_object *obj = prog->obj;
4037 __u32 shdr_idx = sym->st_shndx;
4038 enum libbpf_map_type type;
9c0f8cbd 4039 const char *sym_sec_name;
1f8e2bcb
AN
4040 struct bpf_map *map;
4041
aa0b8d43 4042 if (!is_call_insn(insn) && !is_ldimm64_insn(insn)) {
9c0f8cbd
AN
4043 pr_warn("prog '%s': invalid relo against '%s' for insns[%d].code 0x%x\n",
4044 prog->name, sym_name, insn_idx, insn->code);
1f8e2bcb
AN
4045 return -LIBBPF_ERRNO__RELOC;
4046 }
166750bc
AN
4047
4048 if (sym_is_extern(sym)) {
ad23b723 4049 int sym_idx = ELF64_R_SYM(rel->r_info);
166750bc
AN
4050 int i, n = obj->nr_extern;
4051 struct extern_desc *ext;
4052
4053 for (i = 0; i < n; i++) {
4054 ext = &obj->externs[i];
4055 if (ext->sym_idx == sym_idx)
4056 break;
4057 }
4058 if (i >= n) {
9c0f8cbd
AN
4059 pr_warn("prog '%s': extern relo failed to find extern for '%s' (%d)\n",
4060 prog->name, sym_name, sym_idx);
166750bc
AN
4061 return -LIBBPF_ERRNO__RELOC;
4062 }
9c0f8cbd
AN
4063 pr_debug("prog '%s': found extern #%d '%s' (sym %d) for insn #%u\n",
4064 prog->name, i, ext->name, ext->sym_idx, insn_idx);
5bd022ec 4065 if (insn->code == (BPF_JMP | BPF_CALL))
a18f7214 4066 reloc_desc->type = RELO_EXTERN_CALL;
5bd022ec 4067 else
a18f7214 4068 reloc_desc->type = RELO_EXTERN_LD64;
166750bc 4069 reloc_desc->insn_idx = insn_idx;
3055ddd6 4070 reloc_desc->ext_idx = i;
166750bc
AN
4071 return 0;
4072 }
4073
aa0b8d43
MKL
4074 /* sub-program call relocation */
4075 if (is_call_insn(insn)) {
4076 if (insn->src_reg != BPF_PSEUDO_CALL) {
4077 pr_warn("prog '%s': incorrect bpf_call opcode\n", prog->name);
4078 return -LIBBPF_ERRNO__RELOC;
4079 }
4080 /* text_shndx can be 0, if no default "main" program exists */
4081 if (!shdr_idx || shdr_idx != obj->efile.text_shndx) {
4082 sym_sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, shdr_idx));
4083 pr_warn("prog '%s': bad call relo against '%s' in section '%s'\n",
4084 prog->name, sym_name, sym_sec_name);
4085 return -LIBBPF_ERRNO__RELOC;
4086 }
4087 if (sym->st_value % BPF_INSN_SZ) {
4088 pr_warn("prog '%s': bad call relo against '%s' at offset %zu\n",
4089 prog->name, sym_name, (size_t)sym->st_value);
4090 return -LIBBPF_ERRNO__RELOC;
4091 }
4092 reloc_desc->type = RELO_CALL;
4093 reloc_desc->insn_idx = insn_idx;
4094 reloc_desc->sym_off = sym->st_value;
4095 return 0;
4096 }
4097
1f8e2bcb 4098 if (!shdr_idx || shdr_idx >= SHN_LORESERVE) {
9c0f8cbd
AN
4099 pr_warn("prog '%s': invalid relo against '%s' in special section 0x%x; forgot to initialize global var?..\n",
4100 prog->name, sym_name, shdr_idx);
1f8e2bcb
AN
4101 return -LIBBPF_ERRNO__RELOC;
4102 }
4103
53eddb5e
YS
4104 /* loading subprog addresses */
4105 if (sym_is_subprog(sym, obj->efile.text_shndx)) {
4106 /* global_func: sym->st_value = offset in the section, insn->imm = 0.
4107 * local_func: sym->st_value = 0, insn->imm = offset in the section.
4108 */
4109 if ((sym->st_value % BPF_INSN_SZ) || (insn->imm % BPF_INSN_SZ)) {
4110 pr_warn("prog '%s': bad subprog addr relo against '%s' at offset %zu+%d\n",
4111 prog->name, sym_name, (size_t)sym->st_value, insn->imm);
4112 return -LIBBPF_ERRNO__RELOC;
4113 }
4114
4115 reloc_desc->type = RELO_SUBPROG_ADDR;
4116 reloc_desc->insn_idx = insn_idx;
4117 reloc_desc->sym_off = sym->st_value;
4118 return 0;
4119 }
4120
1f8e2bcb 4121 type = bpf_object__section_to_libbpf_map_type(obj, shdr_idx);
9c0f8cbd 4122 sym_sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, shdr_idx));
1f8e2bcb
AN
4123
4124 /* generic map reference relocation */
4125 if (type == LIBBPF_MAP_UNSPEC) {
4126 if (!bpf_object__shndx_is_maps(obj, shdr_idx)) {
9c0f8cbd
AN
4127 pr_warn("prog '%s': bad map relo against '%s' in section '%s'\n",
4128 prog->name, sym_name, sym_sec_name);
1f8e2bcb
AN
4129 return -LIBBPF_ERRNO__RELOC;
4130 }
4131 for (map_idx = 0; map_idx < nr_maps; map_idx++) {
4132 map = &obj->maps[map_idx];
4133 if (map->libbpf_type != type ||
4134 map->sec_idx != sym->st_shndx ||
4135 map->sec_offset != sym->st_value)
4136 continue;
9c0f8cbd
AN
4137 pr_debug("prog '%s': found map %zd (%s, sec %d, off %zu) for insn #%u\n",
4138 prog->name, map_idx, map->name, map->sec_idx,
1f8e2bcb
AN
4139 map->sec_offset, insn_idx);
4140 break;
4141 }
4142 if (map_idx >= nr_maps) {
9c0f8cbd
AN
4143 pr_warn("prog '%s': map relo failed to find map for section '%s', off %zu\n",
4144 prog->name, sym_sec_name, (size_t)sym->st_value);
1f8e2bcb
AN
4145 return -LIBBPF_ERRNO__RELOC;
4146 }
4147 reloc_desc->type = RELO_LD64;
4148 reloc_desc->insn_idx = insn_idx;
4149 reloc_desc->map_idx = map_idx;
53f8dd43 4150 reloc_desc->sym_off = 0; /* sym->st_value determines map_idx */
1f8e2bcb
AN
4151 return 0;
4152 }
4153
4154 /* global data map relocation */
4155 if (!bpf_object__shndx_is_data(obj, shdr_idx)) {
9c0f8cbd
AN
4156 pr_warn("prog '%s': bad data relo against section '%s'\n",
4157 prog->name, sym_sec_name);
1f8e2bcb 4158 return -LIBBPF_ERRNO__RELOC;
1f8e2bcb 4159 }
1f8e2bcb
AN
4160 for (map_idx = 0; map_idx < nr_maps; map_idx++) {
4161 map = &obj->maps[map_idx];
25bbbd7a 4162 if (map->libbpf_type != type || map->sec_idx != sym->st_shndx)
1f8e2bcb 4163 continue;
9c0f8cbd
AN
4164 pr_debug("prog '%s': found data map %zd (%s, sec %d, off %zu) for insn %u\n",
4165 prog->name, map_idx, map->name, map->sec_idx,
4166 map->sec_offset, insn_idx);
1f8e2bcb
AN
4167 break;
4168 }
4169 if (map_idx >= nr_maps) {
9c0f8cbd
AN
4170 pr_warn("prog '%s': data relo failed to find map for section '%s'\n",
4171 prog->name, sym_sec_name);
1f8e2bcb
AN
4172 return -LIBBPF_ERRNO__RELOC;
4173 }
4174
4175 reloc_desc->type = RELO_DATA;
4176 reloc_desc->insn_idx = insn_idx;
4177 reloc_desc->map_idx = map_idx;
53f8dd43 4178 reloc_desc->sym_off = sym->st_value;
1f8e2bcb
AN
4179 return 0;
4180}
4181
db2b8b06
AN
4182static bool prog_contains_insn(const struct bpf_program *prog, size_t insn_idx)
4183{
4184 return insn_idx >= prog->sec_insn_off &&
4185 insn_idx < prog->sec_insn_off + prog->sec_insn_cnt;
4186}
4187
4188static struct bpf_program *find_prog_by_sec_insn(const struct bpf_object *obj,
4189 size_t sec_idx, size_t insn_idx)
4190{
4191 int l = 0, r = obj->nr_programs - 1, m;
4192 struct bpf_program *prog;
4193
d0d382f9
SHY
4194 if (!obj->nr_programs)
4195 return NULL;
4196
db2b8b06
AN
4197 while (l < r) {
4198 m = l + (r - l + 1) / 2;
4199 prog = &obj->programs[m];
4200
4201 if (prog->sec_idx < sec_idx ||
4202 (prog->sec_idx == sec_idx && prog->sec_insn_off <= insn_idx))
4203 l = m;
4204 else
4205 r = m - 1;
4206 }
4207 /* matching program could be at index l, but it still might be the
4208 * wrong one, so we need to double check conditions for the last time
4209 */
4210 prog = &obj->programs[l];
4211 if (prog->sec_idx == sec_idx && prog_contains_insn(prog, insn_idx))
4212 return prog;
4213 return NULL;
4214}
4215
34090915 4216static int
ad23b723 4217bpf_object__collect_prog_relos(struct bpf_object *obj, Elf64_Shdr *shdr, Elf_Data *data)
34090915 4218{
9c0f8cbd 4219 const char *relo_sec_name, *sec_name;
b7332d28 4220 size_t sec_idx = shdr->sh_info, sym_idx;
c3c55696
AN
4221 struct bpf_program *prog;
4222 struct reloc_desc *relos;
1f8e2bcb 4223 int err, i, nrels;
c3c55696
AN
4224 const char *sym_name;
4225 __u32 insn_idx;
6245947c
AN
4226 Elf_Scn *scn;
4227 Elf_Data *scn_data;
ad23b723
AN
4228 Elf64_Sym *sym;
4229 Elf64_Rel *rel;
34090915 4230
b7332d28
AN
4231 if (sec_idx >= obj->efile.sec_cnt)
4232 return -EINVAL;
4233
6245947c
AN
4234 scn = elf_sec_by_idx(obj, sec_idx);
4235 scn_data = elf_sec_data(obj, scn);
4236
9c0f8cbd 4237 relo_sec_name = elf_sec_str(obj, shdr->sh_name);
6245947c 4238 sec_name = elf_sec_name(obj, scn);
9c0f8cbd
AN
4239 if (!relo_sec_name || !sec_name)
4240 return -EINVAL;
4241
4242 pr_debug("sec '%s': collecting relocation for section(%zu) '%s'\n",
4243 relo_sec_name, sec_idx, sec_name);
34090915
WN
4244 nrels = shdr->sh_size / shdr->sh_entsize;
4245
34090915 4246 for (i = 0; i < nrels; i++) {
ad23b723
AN
4247 rel = elf_rel_by_idx(data, i);
4248 if (!rel) {
9c0f8cbd 4249 pr_warn("sec '%s': failed to get relo #%d\n", relo_sec_name, i);
6371ca3b 4250 return -LIBBPF_ERRNO__FORMAT;
34090915 4251 }
ad23b723 4252
b7332d28
AN
4253 sym_idx = ELF64_R_SYM(rel->r_info);
4254 sym = elf_sym_by_idx(obj, sym_idx);
ad23b723 4255 if (!sym) {
b7332d28
AN
4256 pr_warn("sec '%s': symbol #%zu not found for relo #%d\n",
4257 relo_sec_name, sym_idx, i);
4258 return -LIBBPF_ERRNO__FORMAT;
4259 }
4260
4261 if (sym->st_shndx >= obj->efile.sec_cnt) {
4262 pr_warn("sec '%s': corrupted symbol #%zu pointing to invalid section #%zu for relo #%d\n",
4263 relo_sec_name, sym_idx, (size_t)sym->st_shndx, i);
6371ca3b 4264 return -LIBBPF_ERRNO__FORMAT;
34090915 4265 }
6245947c 4266
ad23b723 4267 if (rel->r_offset % BPF_INSN_SZ || rel->r_offset >= scn_data->d_size) {
9c0f8cbd 4268 pr_warn("sec '%s': invalid offset 0x%zx for relo #%d\n",
b7332d28 4269 relo_sec_name, (size_t)rel->r_offset, i);
1f8e2bcb 4270 return -LIBBPF_ERRNO__FORMAT;
9c0f8cbd 4271 }
d859900c 4272
ad23b723 4273 insn_idx = rel->r_offset / BPF_INSN_SZ;
c3c55696
AN
4274 /* relocations against static functions are recorded as
4275 * relocations against the section that contains a function;
4276 * in such case, symbol will be STT_SECTION and sym.st_name
4277 * will point to empty string (0), so fetch section name
4278 * instead
4279 */
ad23b723
AN
4280 if (ELF64_ST_TYPE(sym->st_info) == STT_SECTION && sym->st_name == 0)
4281 sym_name = elf_sec_name(obj, elf_sec_by_idx(obj, sym->st_shndx));
c3c55696 4282 else
ad23b723 4283 sym_name = elf_sym_str(obj, sym->st_name);
c3c55696 4284 sym_name = sym_name ?: "<?";
d859900c 4285
9c0f8cbd
AN
4286 pr_debug("sec '%s': relo #%d: insn #%u against '%s'\n",
4287 relo_sec_name, i, insn_idx, sym_name);
666810e8 4288
c3c55696
AN
4289 prog = find_prog_by_sec_insn(obj, sec_idx, insn_idx);
4290 if (!prog) {
6245947c 4291 pr_debug("sec '%s': relo #%d: couldn't find program in section '%s' for insn #%u, probably overridden weak function, skipping...\n",
c3c55696 4292 relo_sec_name, i, sec_name, insn_idx);
6245947c 4293 continue;
c3c55696
AN
4294 }
4295
4296 relos = libbpf_reallocarray(prog->reloc_desc,
4297 prog->nr_reloc + 1, sizeof(*relos));
4298 if (!relos)
4299 return -ENOMEM;
4300 prog->reloc_desc = relos;
4301
4302 /* adjust insn_idx to local BPF program frame of reference */
4303 insn_idx -= prog->sec_insn_off;
4304 err = bpf_program__record_reloc(prog, &relos[prog->nr_reloc],
ad23b723 4305 insn_idx, sym_name, sym, rel);
1f8e2bcb
AN
4306 if (err)
4307 return err;
c3c55696
AN
4308
4309 prog->nr_reloc++;
34090915
WN
4310 }
4311 return 0;
4312}
4313
4fcac46c 4314static int map_fill_btf_type_info(struct bpf_object *obj, struct bpf_map *map)
8a138aed 4315{
aaf6886d 4316 int id;
8a138aed 4317
a8fee962
AN
4318 if (!obj->btf)
4319 return -ENOENT;
4320
590a0088
MKL
4321 /* if it's BTF-defined map, we don't need to search for type IDs.
4322 * For struct_ops map, it does not need btf_key_type_id and
4323 * btf_value_type_id.
4324 */
aaf6886d 4325 if (map->sec_idx == obj->efile.btf_maps_shndx || bpf_map__is_struct_ops(map))
abd29c93
AN
4326 return 0;
4327
aaf6886d
AN
4328 /*
4329 * LLVM annotates global data differently in BTF, that is,
4330 * only as '.data', '.bss' or '.rodata'.
4331 */
4332 if (!bpf_map__is_internal(map))
4333 return -ENOENT;
4334
4335 id = btf__find_by_name(obj->btf, map->real_name);
4336 if (id < 0)
4337 return id;
8a138aed 4338
aaf6886d
AN
4339 map->btf_key_type_id = 0;
4340 map->btf_value_type_id = id;
8a138aed
MKL
4341 return 0;
4342}
4343
97eb3138
MP
4344static int bpf_get_map_info_from_fdinfo(int fd, struct bpf_map_info *info)
4345{
4346 char file[PATH_MAX], buff[4096];
4347 FILE *fp;
4348 __u32 val;
4349 int err;
4350
4351 snprintf(file, sizeof(file), "/proc/%d/fdinfo/%d", getpid(), fd);
4352 memset(info, 0, sizeof(*info));
4353
59842c54 4354 fp = fopen(file, "re");
97eb3138
MP
4355 if (!fp) {
4356 err = -errno;
4357 pr_warn("failed to open %s: %d. No procfs support?\n", file,
4358 err);
4359 return err;
4360 }
4361
4362 while (fgets(buff, sizeof(buff), fp)) {
4363 if (sscanf(buff, "map_type:\t%u", &val) == 1)
4364 info->type = val;
4365 else if (sscanf(buff, "key_size:\t%u", &val) == 1)
4366 info->key_size = val;
4367 else if (sscanf(buff, "value_size:\t%u", &val) == 1)
4368 info->value_size = val;
4369 else if (sscanf(buff, "max_entries:\t%u", &val) == 1)
4370 info->max_entries = val;
4371 else if (sscanf(buff, "map_flags:\t%i", &val) == 1)
4372 info->map_flags = val;
4373 }
4374
4375 fclose(fp);
4376
4377 return 0;
4378}
4379
ec41817b
AN
4380bool bpf_map__autocreate(const struct bpf_map *map)
4381{
4382 return map->autocreate;
4383}
4384
4385int bpf_map__set_autocreate(struct bpf_map *map, bool autocreate)
4386{
4387 if (map->obj->loaded)
4388 return libbpf_err(-EBUSY);
4389
4390 map->autocreate = autocreate;
4391 return 0;
4392}
4393
26736eb9
JK
4394int bpf_map__reuse_fd(struct bpf_map *map, int fd)
4395{
813847a3 4396 struct bpf_map_info info;
bf3f0037 4397 __u32 len = sizeof(info), name_len;
26736eb9
JK
4398 int new_fd, err;
4399 char *new_name;
4400
813847a3 4401 memset(&info, 0, len);
629dfc66 4402 err = bpf_map_get_info_by_fd(fd, &info, &len);
97eb3138
MP
4403 if (err && errno == EINVAL)
4404 err = bpf_get_map_info_from_fdinfo(fd, &info);
26736eb9 4405 if (err)
e9fc3ce9 4406 return libbpf_err(err);
26736eb9 4407
bf3f0037
AW
4408 name_len = strlen(info.name);
4409 if (name_len == BPF_OBJ_NAME_LEN - 1 && strncmp(map->name, info.name, name_len) == 0)
4410 new_name = strdup(map->name);
4411 else
4412 new_name = strdup(info.name);
4413
26736eb9 4414 if (!new_name)
e9fc3ce9 4415 return libbpf_err(-errno);
26736eb9
JK
4416
4417 new_fd = open("/", O_RDONLY | O_CLOEXEC);
d1b4574a
THJ
4418 if (new_fd < 0) {
4419 err = -errno;
26736eb9 4420 goto err_free_new_name;
d1b4574a 4421 }
26736eb9
JK
4422
4423 new_fd = dup3(fd, new_fd, O_CLOEXEC);
d1b4574a
THJ
4424 if (new_fd < 0) {
4425 err = -errno;
26736eb9 4426 goto err_close_new_fd;
d1b4574a 4427 }
26736eb9
JK
4428
4429 err = zclose(map->fd);
d1b4574a
THJ
4430 if (err) {
4431 err = -errno;
26736eb9 4432 goto err_close_new_fd;
d1b4574a 4433 }
26736eb9
JK
4434 free(map->name);
4435
4436 map->fd = new_fd;
4437 map->name = new_name;
4438 map->def.type = info.type;
4439 map->def.key_size = info.key_size;
4440 map->def.value_size = info.value_size;
4441 map->def.max_entries = info.max_entries;
4442 map->def.map_flags = info.map_flags;
4443 map->btf_key_type_id = info.btf_key_type_id;
4444 map->btf_value_type_id = info.btf_value_type_id;
ec6d5f47 4445 map->reused = true;
47512102 4446 map->map_extra = info.map_extra;
26736eb9
JK
4447
4448 return 0;
4449
4450err_close_new_fd:
4451 close(new_fd);
4452err_free_new_name:
4453 free(new_name);
e9fc3ce9 4454 return libbpf_err(err);
26736eb9
JK
4455}
4456
1bdb6c9a 4457__u32 bpf_map__max_entries(const struct bpf_map *map)
1a11a4c7 4458{
1bdb6c9a
AN
4459 return map->def.max_entries;
4460}
1a11a4c7 4461
b3278099
AN
4462struct bpf_map *bpf_map__inner_map(struct bpf_map *map)
4463{
4464 if (!bpf_map_type__is_map_in_map(map->def.type))
e9fc3ce9 4465 return errno = EINVAL, NULL;
b3278099
AN
4466
4467 return map->inner_map;
4468}
4469
1bdb6c9a
AN
4470int bpf_map__set_max_entries(struct bpf_map *map, __u32 max_entries)
4471{
597fbc46 4472 if (map->obj->loaded)
e9fc3ce9 4473 return libbpf_err(-EBUSY);
597fbc46 4474
1a11a4c7 4475 map->def.max_entries = max_entries;
597fbc46
AN
4476
4477 /* auto-adjust BPF ringbuf map max_entries to be a multiple of page size */
b66ccae0 4478 if (map_is_ringbuf(map))
597fbc46
AN
4479 map->def.max_entries = adjust_ringbuf_sz(map->def.max_entries);
4480
1a11a4c7
AI
4481 return 0;
4482}
4483
47eff617 4484static int
fd9eef1a 4485bpf_object__probe_loading(struct bpf_object *obj)
47eff617 4486{
47eff617
SF
4487 char *cp, errmsg[STRERR_BUFSIZE];
4488 struct bpf_insn insns[] = {
4489 BPF_MOV64_IMM(BPF_REG_0, 0),
4490 BPF_EXIT_INSN(),
4491 };
e32660ac 4492 int ret, insn_cnt = ARRAY_SIZE(insns);
47eff617 4493
f9bceaa5
SF
4494 if (obj->gen_loader)
4495 return 0;
4496
e542f2c4
AN
4497 ret = bump_rlimit_memlock();
4498 if (ret)
4499 pr_warn("Failed to bump RLIMIT_MEMLOCK (err = %d), you might need to do it explicitly!\n", ret);
4500
47eff617 4501 /* make sure basic loading works */
e32660ac
AN
4502 ret = bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, NULL, "GPL", insns, insn_cnt, NULL);
4503 if (ret < 0)
4504 ret = bpf_prog_load(BPF_PROG_TYPE_TRACEPOINT, NULL, "GPL", insns, insn_cnt, NULL);
47eff617 4505 if (ret < 0) {
fd9eef1a
EC
4506 ret = errno;
4507 cp = libbpf_strerror_r(ret, errmsg, sizeof(errmsg));
4508 pr_warn("Error in %s():%s(%d). Couldn't load trivial BPF "
4509 "program. Make sure your kernel supports BPF "
4510 "(CONFIG_BPF_SYSCALL=y) and/or that RLIMIT_MEMLOCK is "
4511 "set to big enough value.\n", __func__, cp, ret);
4512 return -ret;
47eff617
SF
4513 }
4514 close(ret);
4515
fd9eef1a
EC
4516 return 0;
4517}
4518
bb180fb2
AN
4519static int probe_fd(int fd)
4520{
4521 if (fd >= 0)
4522 close(fd);
4523 return fd >= 0;
4524}
4525
47b6cb4d 4526static int probe_kern_prog_name(void)
fd9eef1a 4527{
1f235777 4528 const size_t attr_sz = offsetofend(union bpf_attr, prog_name);
fd9eef1a
EC
4529 struct bpf_insn insns[] = {
4530 BPF_MOV64_IMM(BPF_REG_0, 0),
4531 BPF_EXIT_INSN(),
4532 };
1f235777
HL
4533 union bpf_attr attr;
4534 int ret;
4535
4536 memset(&attr, 0, attr_sz);
4537 attr.prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
4538 attr.license = ptr_to_u64("GPL");
4539 attr.insns = ptr_to_u64(insns);
4540 attr.insn_cnt = (__u32)ARRAY_SIZE(insns);
4541 libbpf_strlcpy(attr.prog_name, "libbpf_nametest", sizeof(attr.prog_name));
fd9eef1a
EC
4542
4543 /* make sure loading with name works */
1f235777 4544 ret = sys_bpf_prog_load(&attr, attr_sz, PROG_LOAD_ATTEMPTS);
bb180fb2 4545 return probe_fd(ret);
47eff617
SF
4546}
4547
47b6cb4d 4548static int probe_kern_global_data(void)
8837fe5d 4549{
8837fe5d
DB
4550 char *cp, errmsg[STRERR_BUFSIZE];
4551 struct bpf_insn insns[] = {
4552 BPF_LD_MAP_VALUE(BPF_REG_1, 0, 16),
4553 BPF_ST_MEM(BPF_DW, BPF_REG_1, 0, 42),
4554 BPF_MOV64_IMM(BPF_REG_0, 0),
4555 BPF_EXIT_INSN(),
4556 };
e32660ac 4557 int ret, map, insn_cnt = ARRAY_SIZE(insns);
8837fe5d 4558
10b62d6a 4559 map = bpf_map_create(BPF_MAP_TYPE_ARRAY, "libbpf_global", sizeof(int), 32, 1, NULL);
8837fe5d 4560 if (map < 0) {
23ab656b
THJ
4561 ret = -errno;
4562 cp = libbpf_strerror_r(ret, errmsg, sizeof(errmsg));
be18010e 4563 pr_warn("Error in %s():%s(%d). Couldn't create simple array map.\n",
23ab656b
THJ
4564 __func__, cp, -ret);
4565 return ret;
8837fe5d
DB
4566 }
4567
4568 insns[0].imm = map;
4569
e32660ac 4570 ret = bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, NULL, "GPL", insns, insn_cnt, NULL);
47b6cb4d 4571 close(map);
bb180fb2 4572 return probe_fd(ret);
8837fe5d
DB
4573}
4574
68b08647
AN
4575static int probe_kern_btf(void)
4576{
4577 static const char strs[] = "\0int";
4578 __u32 types[] = {
4579 /* int */
4580 BTF_TYPE_INT_ENC(1, BTF_INT_SIGNED, 0, 32, 4),
4581 };
4582
4583 return probe_fd(libbpf__load_raw_btf((char *)types, sizeof(types),
4584 strs, sizeof(strs)));
4585}
4586
47b6cb4d 4587static int probe_kern_btf_func(void)
d7c4b398 4588{
8983b731 4589 static const char strs[] = "\0int\0x\0a";
d7c4b398
AN
4590 /* void x(int a) {} */
4591 __u32 types[] = {
4592 /* int */
4593 BTF_TYPE_INT_ENC(1, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
4594 /* FUNC_PROTO */ /* [2] */
4595 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_FUNC_PROTO, 0, 1), 0),
4596 BTF_PARAM_ENC(7, 1),
4597 /* FUNC x */ /* [3] */
4598 BTF_TYPE_ENC(5, BTF_INFO_ENC(BTF_KIND_FUNC, 0, 0), 2),
4599 };
d7c4b398 4600
bb180fb2
AN
4601 return probe_fd(libbpf__load_raw_btf((char *)types, sizeof(types),
4602 strs, sizeof(strs)));
d7c4b398
AN
4603}
4604
47b6cb4d 4605static int probe_kern_btf_func_global(void)
2d3eb67f
AS
4606{
4607 static const char strs[] = "\0int\0x\0a";
4608 /* static void x(int a) {} */
4609 __u32 types[] = {
4610 /* int */
4611 BTF_TYPE_INT_ENC(1, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
4612 /* FUNC_PROTO */ /* [2] */
4613 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_FUNC_PROTO, 0, 1), 0),
4614 BTF_PARAM_ENC(7, 1),
4615 /* FUNC x BTF_FUNC_GLOBAL */ /* [3] */
4616 BTF_TYPE_ENC(5, BTF_INFO_ENC(BTF_KIND_FUNC, 0, BTF_FUNC_GLOBAL), 2),
4617 };
2d3eb67f 4618
bb180fb2
AN
4619 return probe_fd(libbpf__load_raw_btf((char *)types, sizeof(types),
4620 strs, sizeof(strs)));
2d3eb67f
AS
4621}
4622
47b6cb4d 4623static int probe_kern_btf_datasec(void)
d7c4b398 4624{
8983b731 4625 static const char strs[] = "\0x\0.data";
d7c4b398
AN
4626 /* static int a; */
4627 __u32 types[] = {
4628 /* int */
4629 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
4630 /* VAR x */ /* [2] */
4631 BTF_TYPE_ENC(1, BTF_INFO_ENC(BTF_KIND_VAR, 0, 0), 1),
4632 BTF_VAR_STATIC,
4633 /* DATASEC val */ /* [3] */
4634 BTF_TYPE_ENC(3, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
4635 BTF_VAR_SECINFO_ENC(2, 0, 4),
4636 };
cfd49210 4637
bb180fb2
AN
4638 return probe_fd(libbpf__load_raw_btf((char *)types, sizeof(types),
4639 strs, sizeof(strs)));
d7c4b398
AN
4640}
4641
22541a9e
IL
4642static int probe_kern_btf_float(void)
4643{
4644 static const char strs[] = "\0float";
4645 __u32 types[] = {
4646 /* float */
4647 BTF_TYPE_FLOAT_ENC(1, 4),
4648 };
4649
4650 return probe_fd(libbpf__load_raw_btf((char *)types, sizeof(types),
4651 strs, sizeof(strs)));
4652}
4653
223f903e 4654static int probe_kern_btf_decl_tag(void)
5b84bd10
YS
4655{
4656 static const char strs[] = "\0tag";
4657 __u32 types[] = {
4658 /* int */
4659 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
4660 /* VAR x */ /* [2] */
4661 BTF_TYPE_ENC(1, BTF_INFO_ENC(BTF_KIND_VAR, 0, 0), 1),
4662 BTF_VAR_STATIC,
4663 /* attr */
223f903e 4664 BTF_TYPE_DECL_TAG_ENC(1, 2, -1),
5b84bd10
YS
4665 };
4666
4667 return probe_fd(libbpf__load_raw_btf((char *)types, sizeof(types),
4668 strs, sizeof(strs)));
4669}
4670
2dc1e488
YS
4671static int probe_kern_btf_type_tag(void)
4672{
4673 static const char strs[] = "\0tag";
4674 __u32 types[] = {
4675 /* int */
4676 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
4677 /* attr */
4678 BTF_TYPE_TYPE_TAG_ENC(1, 1), /* [2] */
4679 /* ptr */
4680 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 2), /* [3] */
4681 };
4682
4683 return probe_fd(libbpf__load_raw_btf((char *)types, sizeof(types),
4684 strs, sizeof(strs)));
4685}
4686
47b6cb4d 4687static int probe_kern_array_mmap(void)
7fe74b43 4688{
a9606f40
AN
4689 LIBBPF_OPTS(bpf_map_create_opts, opts, .map_flags = BPF_F_MMAPABLE);
4690 int fd;
7fe74b43 4691
10b62d6a 4692 fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, "libbpf_mmap", sizeof(int), sizeof(int), 1, &opts);
a9606f40 4693 return probe_fd(fd);
7fe74b43
AN
4694}
4695
47b6cb4d 4696static int probe_kern_exp_attach_type(void)
25498a19 4697{
e32660ac 4698 LIBBPF_OPTS(bpf_prog_load_opts, opts, .expected_attach_type = BPF_CGROUP_INET_SOCK_CREATE);
25498a19
AN
4699 struct bpf_insn insns[] = {
4700 BPF_MOV64_IMM(BPF_REG_0, 0),
4701 BPF_EXIT_INSN(),
4702 };
e32660ac 4703 int fd, insn_cnt = ARRAY_SIZE(insns);
25498a19 4704
25498a19
AN
4705 /* use any valid combination of program type and (optional)
4706 * non-zero expected attach type (i.e., not a BPF_CGROUP_INET_INGRESS)
4707 * to see if kernel supports expected_attach_type field for
4708 * BPF_PROG_LOAD command
4709 */
e32660ac
AN
4710 fd = bpf_prog_load(BPF_PROG_TYPE_CGROUP_SOCK, NULL, "GPL", insns, insn_cnt, &opts);
4711 return probe_fd(fd);
25498a19
AN
4712}
4713
109cea5a
AN
4714static int probe_kern_probe_read_kernel(void)
4715{
109cea5a
AN
4716 struct bpf_insn insns[] = {
4717 BPF_MOV64_REG(BPF_REG_1, BPF_REG_10), /* r1 = r10 (fp) */
4718 BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -8), /* r1 += -8 */
4719 BPF_MOV64_IMM(BPF_REG_2, 8), /* r2 = 8 */
4720 BPF_MOV64_IMM(BPF_REG_3, 0), /* r3 = 0 */
4721 BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_probe_read_kernel),
4722 BPF_EXIT_INSN(),
4723 };
e32660ac 4724 int fd, insn_cnt = ARRAY_SIZE(insns);
109cea5a 4725
d252a4a4 4726 fd = bpf_prog_load(BPF_PROG_TYPE_TRACEPOINT, NULL, "GPL", insns, insn_cnt, NULL);
e32660ac 4727 return probe_fd(fd);
109cea5a
AN
4728}
4729
5d23328d
YZ
4730static int probe_prog_bind_map(void)
4731{
5d23328d
YZ
4732 char *cp, errmsg[STRERR_BUFSIZE];
4733 struct bpf_insn insns[] = {
4734 BPF_MOV64_IMM(BPF_REG_0, 0),
4735 BPF_EXIT_INSN(),
4736 };
e32660ac 4737 int ret, map, prog, insn_cnt = ARRAY_SIZE(insns);
5d23328d 4738
10b62d6a 4739 map = bpf_map_create(BPF_MAP_TYPE_ARRAY, "libbpf_det_bind", sizeof(int), 32, 1, NULL);
5d23328d
YZ
4740 if (map < 0) {
4741 ret = -errno;
4742 cp = libbpf_strerror_r(ret, errmsg, sizeof(errmsg));
4743 pr_warn("Error in %s():%s(%d). Couldn't create simple array map.\n",
4744 __func__, cp, -ret);
4745 return ret;
4746 }
4747
e32660ac 4748 prog = bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, NULL, "GPL", insns, insn_cnt, NULL);
5d23328d
YZ
4749 if (prog < 0) {
4750 close(map);
4751 return 0;
4752 }
4753
4754 ret = bpf_prog_bind_map(prog, map, NULL);
4755
4756 close(map);
4757 close(prog);
4758
4759 return ret >= 0;
4760}
4761
4f33a53d
AN
4762static int probe_module_btf(void)
4763{
4764 static const char strs[] = "\0int";
4765 __u32 types[] = {
4766 /* int */
4767 BTF_TYPE_INT_ENC(1, BTF_INT_SIGNED, 0, 32, 4),
4768 };
4769 struct bpf_btf_info info;
4770 __u32 len = sizeof(info);
4771 char name[16];
4772 int fd, err;
4773
4774 fd = libbpf__load_raw_btf((char *)types, sizeof(types), strs, sizeof(strs));
4775 if (fd < 0)
4776 return 0; /* BTF not supported at all */
4777
4778 memset(&info, 0, sizeof(info));
4779 info.name = ptr_to_u64(name);
4780 info.name_len = sizeof(name);
4781
4782 /* check that BPF_OBJ_GET_INFO_BY_FD supports specifying name pointer;
4783 * kernel's module BTF support coincides with support for
4784 * name/name_len fields in struct bpf_btf_info.
4785 */
629dfc66 4786 err = bpf_btf_get_info_by_fd(fd, &info, &len);
4f33a53d
AN
4787 close(fd);
4788 return !err;
4789}
4790
668ace0e
AN
4791static int probe_perf_link(void)
4792{
668ace0e
AN
4793 struct bpf_insn insns[] = {
4794 BPF_MOV64_IMM(BPF_REG_0, 0),
4795 BPF_EXIT_INSN(),
4796 };
4797 int prog_fd, link_fd, err;
4798
e32660ac
AN
4799 prog_fd = bpf_prog_load(BPF_PROG_TYPE_TRACEPOINT, NULL, "GPL",
4800 insns, ARRAY_SIZE(insns), NULL);
668ace0e
AN
4801 if (prog_fd < 0)
4802 return -errno;
4803
4804 /* use invalid perf_event FD to get EBADF, if link is supported;
4805 * otherwise EINVAL should be returned
4806 */
4807 link_fd = bpf_link_create(prog_fd, -1, BPF_PERF_EVENT, NULL);
4808 err = -errno; /* close() can clobber errno */
4809
4810 if (link_fd >= 0)
4811 close(link_fd);
4812 close(prog_fd);
4813
4814 return link_fd < 0 && err == -EBADF;
4815}
4816
2e4913e0
AN
4817static int probe_kern_bpf_cookie(void)
4818{
4819 struct bpf_insn insns[] = {
4820 BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_get_attach_cookie),
4821 BPF_EXIT_INSN(),
4822 };
4823 int ret, insn_cnt = ARRAY_SIZE(insns);
4824
4825 ret = bpf_prog_load(BPF_PROG_TYPE_KPROBE, NULL, "GPL", insns, insn_cnt, NULL);
4826 return probe_fd(ret);
4827}
4828
f2a62588
YS
4829static int probe_kern_btf_enum64(void)
4830{
4831 static const char strs[] = "\0enum64";
4832 __u32 types[] = {
4833 BTF_TYPE_ENC(1, BTF_INFO_ENC(BTF_KIND_ENUM64, 0, 0), 8),
4834 };
4835
4836 return probe_fd(libbpf__load_raw_btf((char *)types, sizeof(types),
4837 strs, sizeof(strs)));
4838}
4839
708ac5be
AN
4840static int probe_kern_syscall_wrapper(void);
4841
47b6cb4d
AN
4842enum kern_feature_result {
4843 FEAT_UNKNOWN = 0,
4844 FEAT_SUPPORTED = 1,
4845 FEAT_MISSING = 2,
4846};
4847
4848typedef int (*feature_probe_fn)(void);
4849
4850static struct kern_feature_desc {
4851 const char *desc;
4852 feature_probe_fn probe;
4853 enum kern_feature_result res;
4854} feature_probes[__FEAT_CNT] = {
4855 [FEAT_PROG_NAME] = {
4856 "BPF program name", probe_kern_prog_name,
4857 },
4858 [FEAT_GLOBAL_DATA] = {
4859 "global variables", probe_kern_global_data,
4860 },
68b08647
AN
4861 [FEAT_BTF] = {
4862 "minimal BTF", probe_kern_btf,
4863 },
47b6cb4d
AN
4864 [FEAT_BTF_FUNC] = {
4865 "BTF functions", probe_kern_btf_func,
4866 },
4867 [FEAT_BTF_GLOBAL_FUNC] = {
4868 "BTF global function", probe_kern_btf_func_global,
4869 },
4870 [FEAT_BTF_DATASEC] = {
4871 "BTF data section and variable", probe_kern_btf_datasec,
4872 },
4873 [FEAT_ARRAY_MMAP] = {
4874 "ARRAY map mmap()", probe_kern_array_mmap,
4875 },
4876 [FEAT_EXP_ATTACH_TYPE] = {
4877 "BPF_PROG_LOAD expected_attach_type attribute",
4878 probe_kern_exp_attach_type,
4879 },
109cea5a
AN
4880 [FEAT_PROBE_READ_KERN] = {
4881 "bpf_probe_read_kernel() helper", probe_kern_probe_read_kernel,
5d23328d
YZ
4882 },
4883 [FEAT_PROG_BIND_MAP] = {
4884 "BPF_PROG_BIND_MAP support", probe_prog_bind_map,
4f33a53d
AN
4885 },
4886 [FEAT_MODULE_BTF] = {
4887 "module BTF support", probe_module_btf,
4888 },
22541a9e
IL
4889 [FEAT_BTF_FLOAT] = {
4890 "BTF_KIND_FLOAT support", probe_kern_btf_float,
4891 },
668ace0e
AN
4892 [FEAT_PERF_LINK] = {
4893 "BPF perf link support", probe_perf_link,
4894 },
223f903e
YS
4895 [FEAT_BTF_DECL_TAG] = {
4896 "BTF_KIND_DECL_TAG support", probe_kern_btf_decl_tag,
5b84bd10 4897 },
2dc1e488
YS
4898 [FEAT_BTF_TYPE_TAG] = {
4899 "BTF_KIND_TYPE_TAG support", probe_kern_btf_type_tag,
4900 },
e542f2c4
AN
4901 [FEAT_MEMCG_ACCOUNT] = {
4902 "memcg-based memory accounting", probe_memcg_account,
4903 },
2e4913e0
AN
4904 [FEAT_BPF_COOKIE] = {
4905 "BPF cookie support", probe_kern_bpf_cookie,
4906 },
f2a62588
YS
4907 [FEAT_BTF_ENUM64] = {
4908 "BTF_KIND_ENUM64 support", probe_kern_btf_enum64,
4909 },
708ac5be
AN
4910 [FEAT_SYSCALL_WRAPPER] = {
4911 "Kernel using syscall wrapper", probe_kern_syscall_wrapper,
4912 },
47b6cb4d 4913};
8837fe5d 4914
e542f2c4 4915bool kernel_supports(const struct bpf_object *obj, enum kern_feature_id feat_id)
47b6cb4d
AN
4916{
4917 struct kern_feature_desc *feat = &feature_probes[feat_id];
4918 int ret;
4919
e542f2c4 4920 if (obj && obj->gen_loader)
67234743
AS
4921 /* To generate loader program assume the latest kernel
4922 * to avoid doing extra prog_load, map_create syscalls.
4923 */
4924 return true;
4925
47b6cb4d
AN
4926 if (READ_ONCE(feat->res) == FEAT_UNKNOWN) {
4927 ret = feat->probe();
4928 if (ret > 0) {
4929 WRITE_ONCE(feat->res, FEAT_SUPPORTED);
4930 } else if (ret == 0) {
4931 WRITE_ONCE(feat->res, FEAT_MISSING);
4932 } else {
4933 pr_warn("Detection of kernel %s support failed: %d\n", feat->desc, ret);
4934 WRITE_ONCE(feat->res, FEAT_MISSING);
4935 }
8837fe5d
DB
4936 }
4937
47b6cb4d 4938 return READ_ONCE(feat->res) == FEAT_SUPPORTED;
47eff617
SF
4939}
4940
57a00f41
THJ
4941static bool map_is_reuse_compat(const struct bpf_map *map, int map_fd)
4942{
813847a3 4943 struct bpf_map_info map_info;
57a00f41 4944 char msg[STRERR_BUFSIZE];
813847a3 4945 __u32 map_info_len = sizeof(map_info);
97eb3138 4946 int err;
57a00f41 4947
813847a3 4948 memset(&map_info, 0, map_info_len);
629dfc66 4949 err = bpf_map_get_info_by_fd(map_fd, &map_info, &map_info_len);
97eb3138
MP
4950 if (err && errno == EINVAL)
4951 err = bpf_get_map_info_from_fdinfo(map_fd, &map_info);
4952 if (err) {
4953 pr_warn("failed to get map info for map FD %d: %s\n", map_fd,
4954 libbpf_strerror_r(errno, msg, sizeof(msg)));
57a00f41
THJ
4955 return false;
4956 }
4957
4958 return (map_info.type == map->def.type &&
4959 map_info.key_size == map->def.key_size &&
4960 map_info.value_size == map->def.value_size &&
4961 map_info.max_entries == map->def.max_entries &&
47512102
JK
4962 map_info.map_flags == map->def.map_flags &&
4963 map_info.map_extra == map->map_extra);
57a00f41
THJ
4964}
4965
4966static int
4967bpf_object__reuse_map(struct bpf_map *map)
4968{
4969 char *cp, errmsg[STRERR_BUFSIZE];
4970 int err, pin_fd;
4971
4972 pin_fd = bpf_obj_get(map->pin_path);
4973 if (pin_fd < 0) {
4974 err = -errno;
4975 if (err == -ENOENT) {
4976 pr_debug("found no pinned map to reuse at '%s'\n",
4977 map->pin_path);
4978 return 0;
4979 }
4980
4981 cp = libbpf_strerror_r(-err, errmsg, sizeof(errmsg));
4982 pr_warn("couldn't retrieve pinned map '%s': %s\n",
4983 map->pin_path, cp);
4984 return err;
4985 }
4986
4987 if (!map_is_reuse_compat(map, pin_fd)) {
4988 pr_warn("couldn't reuse pinned map at '%s': parameter mismatch\n",
4989 map->pin_path);
4990 close(pin_fd);
4991 return -EINVAL;
4992 }
4993
4994 err = bpf_map__reuse_fd(map, pin_fd);
d0f325c3 4995 close(pin_fd);
e3ba8e4e 4996 if (err)
57a00f41 4997 return err;
e3ba8e4e 4998
57a00f41
THJ
4999 map->pinned = true;
5000 pr_debug("reused pinned map at '%s'\n", map->pin_path);
5001
5002 return 0;
5003}
5004
d859900c
DB
5005static int
5006bpf_object__populate_internal_map(struct bpf_object *obj, struct bpf_map *map)
5007{
166750bc 5008 enum libbpf_map_type map_type = map->libbpf_type;
d859900c
DB
5009 char *cp, errmsg[STRERR_BUFSIZE];
5010 int err, zero = 0;
d859900c 5011
67234743
AS
5012 if (obj->gen_loader) {
5013 bpf_gen__map_update_elem(obj->gen_loader, map - obj->maps,
5014 map->mmaped, map->def.value_size);
5015 if (map_type == LIBBPF_MAP_RODATA || map_type == LIBBPF_MAP_KCONFIG)
5016 bpf_gen__map_freeze(obj->gen_loader, map - obj->maps);
5017 return 0;
5018 }
eba9c5f4
AN
5019 err = bpf_map_update_elem(map->fd, &zero, map->mmaped, 0);
5020 if (err) {
5021 err = -errno;
5022 cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg));
5023 pr_warn("Error setting initial map(%s) contents: %s\n",
5024 map->name, cp);
5025 return err;
5026 }
d859900c 5027
81bfdd08
AN
5028 /* Freeze .rodata and .kconfig map as read-only from syscall side. */
5029 if (map_type == LIBBPF_MAP_RODATA || map_type == LIBBPF_MAP_KCONFIG) {
d859900c
DB
5030 err = bpf_map_freeze(map->fd);
5031 if (err) {
eba9c5f4
AN
5032 err = -errno;
5033 cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg));
be18010e
KW
5034 pr_warn("Error freezing map(%s) as read-only: %s\n",
5035 map->name, cp);
eba9c5f4 5036 return err;
d859900c
DB
5037 }
5038 }
eba9c5f4 5039 return 0;
d859900c
DB
5040}
5041
2d39d7c5
AN
5042static void bpf_map__destroy(struct bpf_map *map);
5043
67234743 5044static int bpf_object__create_map(struct bpf_object *obj, struct bpf_map *map, bool is_inner)
2d39d7c5 5045{
992c4225 5046 LIBBPF_OPTS(bpf_map_create_opts, create_attr);
2d39d7c5 5047 struct bpf_map_def *def = &map->def;
992c4225 5048 const char *map_name = NULL;
a21ab4c5 5049 int err = 0;
2d39d7c5 5050
9ca1f56a 5051 if (kernel_supports(obj, FEAT_PROG_NAME))
992c4225 5052 map_name = map->name;
2d39d7c5 5053 create_attr.map_ifindex = map->map_ifindex;
2d39d7c5 5054 create_attr.map_flags = def->map_flags;
1bdb6c9a 5055 create_attr.numa_node = map->numa_node;
47512102 5056 create_attr.map_extra = map->map_extra;
2d39d7c5 5057
2d39d7c5 5058 if (bpf_map__is_struct_ops(map))
992c4225 5059 create_attr.btf_vmlinux_value_type_id = map->btf_vmlinux_value_type_id;
2d39d7c5 5060
262cfb74 5061 if (obj->btf && btf__fd(obj->btf) >= 0) {
2d39d7c5
AN
5062 create_attr.btf_fd = btf__fd(obj->btf);
5063 create_attr.btf_key_type_id = map->btf_key_type_id;
5064 create_attr.btf_value_type_id = map->btf_value_type_id;
5065 }
5066
646f02ff
AN
5067 if (bpf_map_type__is_map_in_map(def->type)) {
5068 if (map->inner_map) {
67234743 5069 err = bpf_object__create_map(obj, map->inner_map, true);
646f02ff
AN
5070 if (err) {
5071 pr_warn("map '%s': failed to create inner map: %d\n",
5072 map->name, err);
5073 return err;
5074 }
5075 map->inner_map_fd = bpf_map__fd(map->inner_map);
5076 }
5077 if (map->inner_map_fd >= 0)
5078 create_attr.inner_map_fd = map->inner_map_fd;
5079 }
5080
f7310523
HC
5081 switch (def->type) {
5082 case BPF_MAP_TYPE_PERF_EVENT_ARRAY:
5083 case BPF_MAP_TYPE_CGROUP_ARRAY:
5084 case BPF_MAP_TYPE_STACK_TRACE:
5085 case BPF_MAP_TYPE_ARRAY_OF_MAPS:
5086 case BPF_MAP_TYPE_HASH_OF_MAPS:
5087 case BPF_MAP_TYPE_DEVMAP:
5088 case BPF_MAP_TYPE_DEVMAP_HASH:
5089 case BPF_MAP_TYPE_CPUMAP:
5090 case BPF_MAP_TYPE_XSKMAP:
5091 case BPF_MAP_TYPE_SOCKMAP:
5092 case BPF_MAP_TYPE_SOCKHASH:
5093 case BPF_MAP_TYPE_QUEUE:
5094 case BPF_MAP_TYPE_STACK:
f7310523
HC
5095 create_attr.btf_fd = 0;
5096 create_attr.btf_key_type_id = 0;
5097 create_attr.btf_value_type_id = 0;
5098 map->btf_key_type_id = 0;
5099 map->btf_value_type_id = 0;
5100 default:
5101 break;
5102 }
5103
67234743 5104 if (obj->gen_loader) {
992c4225 5105 bpf_gen__map_create(obj->gen_loader, def->type, map_name,
a4fbfdd7 5106 def->key_size, def->value_size, def->max_entries,
992c4225 5107 &create_attr, is_inner ? -1 : map - obj->maps);
67234743
AS
5108 /* Pretend to have valid FD to pass various fd >= 0 checks.
5109 * This fd == 0 will not be used with any syscall and will be reset to -1 eventually.
5110 */
5111 map->fd = 0;
5112 } else {
992c4225
AN
5113 map->fd = bpf_map_create(def->type, map_name,
5114 def->key_size, def->value_size,
a4fbfdd7 5115 def->max_entries, &create_attr);
67234743 5116 }
2d39d7c5
AN
5117 if (map->fd < 0 && (create_attr.btf_key_type_id ||
5118 create_attr.btf_value_type_id)) {
5119 char *cp, errmsg[STRERR_BUFSIZE];
2d39d7c5 5120
a21ab4c5 5121 err = -errno;
2d39d7c5
AN
5122 cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg));
5123 pr_warn("Error in bpf_create_map_xattr(%s):%s(%d). Retrying without BTF.\n",
5124 map->name, cp, err);
5125 create_attr.btf_fd = 0;
5126 create_attr.btf_key_type_id = 0;
5127 create_attr.btf_value_type_id = 0;
5128 map->btf_key_type_id = 0;
5129 map->btf_value_type_id = 0;
992c4225
AN
5130 map->fd = bpf_map_create(def->type, map_name,
5131 def->key_size, def->value_size,
a4fbfdd7 5132 def->max_entries, &create_attr);
2d39d7c5
AN
5133 }
5134
a21ab4c5 5135 err = map->fd < 0 ? -errno : 0;
2d39d7c5 5136
646f02ff 5137 if (bpf_map_type__is_map_in_map(def->type) && map->inner_map) {
67234743
AS
5138 if (obj->gen_loader)
5139 map->inner_map->fd = -1;
646f02ff
AN
5140 bpf_map__destroy(map->inner_map);
5141 zfree(&map->inner_map);
5142 }
5143
a21ab4c5 5144 return err;
2d39d7c5
AN
5145}
5146
341ac5ff 5147static int init_map_in_map_slots(struct bpf_object *obj, struct bpf_map *map)
a0f2b7ac
HL
5148{
5149 const struct bpf_map *targ_map;
5150 unsigned int i;
67234743 5151 int fd, err = 0;
a0f2b7ac
HL
5152
5153 for (i = 0; i < map->init_slots_sz; i++) {
5154 if (!map->init_slots[i])
5155 continue;
5156
5157 targ_map = map->init_slots[i];
5158 fd = bpf_map__fd(targ_map);
341ac5ff 5159
67234743 5160 if (obj->gen_loader) {
be05c944
AS
5161 bpf_gen__populate_outer_map(obj->gen_loader,
5162 map - obj->maps, i,
5163 targ_map - obj->maps);
67234743
AS
5164 } else {
5165 err = bpf_map_update_elem(map->fd, &i, &fd, 0);
5166 }
a0f2b7ac
HL
5167 if (err) {
5168 err = -errno;
5169 pr_warn("map '%s': failed to initialize slot [%d] to map '%s' fd=%d: %d\n",
341ac5ff 5170 map->name, i, targ_map->name, fd, err);
a0f2b7ac
HL
5171 return err;
5172 }
5173 pr_debug("map '%s': slot [%d] set to map '%s' fd=%d\n",
5174 map->name, i, targ_map->name, fd);
5175 }
5176
5177 zfree(&map->init_slots);
5178 map->init_slots_sz = 0;
5179
5180 return 0;
5181}
5182
341ac5ff
HC
5183static int init_prog_array_slots(struct bpf_object *obj, struct bpf_map *map)
5184{
5185 const struct bpf_program *targ_prog;
5186 unsigned int i;
5187 int fd, err;
5188
5189 if (obj->gen_loader)
5190 return -ENOTSUP;
5191
5192 for (i = 0; i < map->init_slots_sz; i++) {
5193 if (!map->init_slots[i])
5194 continue;
5195
5196 targ_prog = map->init_slots[i];
5197 fd = bpf_program__fd(targ_prog);
5198
5199 err = bpf_map_update_elem(map->fd, &i, &fd, 0);
5200 if (err) {
5201 err = -errno;
5202 pr_warn("map '%s': failed to initialize slot [%d] to prog '%s' fd=%d: %d\n",
5203 map->name, i, targ_prog->name, fd, err);
5204 return err;
5205 }
5206 pr_debug("map '%s': slot [%d] set to prog '%s' fd=%d\n",
5207 map->name, i, targ_prog->name, fd);
5208 }
5209
5210 zfree(&map->init_slots);
5211 map->init_slots_sz = 0;
5212
5213 return 0;
5214}
5215
5216static int bpf_object_init_prog_arrays(struct bpf_object *obj)
5217{
5218 struct bpf_map *map;
5219 int i, err;
5220
5221 for (i = 0; i < obj->nr_maps; i++) {
5222 map = &obj->maps[i];
5223
5224 if (!map->init_slots_sz || map->def.type != BPF_MAP_TYPE_PROG_ARRAY)
5225 continue;
5226
5227 err = init_prog_array_slots(obj, map);
5228 if (err < 0) {
5229 zclose(map->fd);
5230 return err;
5231 }
5232 }
5233 return 0;
5234}
5235
a4fbfdd7
ST
5236static int map_set_def_max_entries(struct bpf_map *map)
5237{
5238 if (map->def.type == BPF_MAP_TYPE_PERF_EVENT_ARRAY && !map->def.max_entries) {
5239 int nr_cpus;
5240
5241 nr_cpus = libbpf_num_possible_cpus();
5242 if (nr_cpus < 0) {
5243 pr_warn("map '%s': failed to determine number of system CPUs: %d\n",
5244 map->name, nr_cpus);
5245 return nr_cpus;
5246 }
5247 pr_debug("map '%s': setting size to %d\n", map->name, nr_cpus);
5248 map->def.max_entries = nr_cpus;
5249 }
5250
5251 return 0;
5252}
5253
52d3352e
WN
5254static int
5255bpf_object__create_maps(struct bpf_object *obj)
5256{
2d39d7c5
AN
5257 struct bpf_map *map;
5258 char *cp, errmsg[STRERR_BUFSIZE];
5259 unsigned int i, j;
8a138aed 5260 int err;
043c5bb3 5261 bool retried;
52d3352e 5262
9d759a9b 5263 for (i = 0; i < obj->nr_maps; i++) {
2d39d7c5 5264 map = &obj->maps[i];
8a138aed 5265
16e0c35c
AN
5266 /* To support old kernels, we skip creating global data maps
5267 * (.rodata, .data, .kconfig, etc); later on, during program
5268 * loading, if we detect that at least one of the to-be-loaded
5269 * programs is referencing any global data map, we'll error
5270 * out with program name and relocation index logged.
5271 * This approach allows to accommodate Clang emitting
5272 * unnecessary .rodata.str1.1 sections for string literals,
5273 * but also it allows to have CO-RE applications that use
5274 * global variables in some of BPF programs, but not others.
5275 * If those global variable-using programs are not loaded at
5276 * runtime due to bpf_program__set_autoload(prog, false),
5277 * bpf_object loading will succeed just fine even on old
5278 * kernels.
5279 */
ec41817b
AN
5280 if (bpf_map__is_internal(map) && !kernel_supports(obj, FEAT_GLOBAL_DATA))
5281 map->autocreate = false;
5282
5283 if (!map->autocreate) {
5284 pr_debug("map '%s': skipped auto-creating...\n", map->name);
16e0c35c 5285 continue;
229fae38 5286 }
16e0c35c 5287
a4fbfdd7
ST
5288 err = map_set_def_max_entries(map);
5289 if (err)
5290 goto err_out;
5291
043c5bb3
MP
5292 retried = false;
5293retry:
57a00f41
THJ
5294 if (map->pin_path) {
5295 err = bpf_object__reuse_map(map);
5296 if (err) {
2d39d7c5 5297 pr_warn("map '%s': error reusing pinned map\n",
57a00f41 5298 map->name);
2d39d7c5 5299 goto err_out;
57a00f41 5300 }
043c5bb3
MP
5301 if (retried && map->fd < 0) {
5302 pr_warn("map '%s': cannot find pinned map\n",
5303 map->name);
5304 err = -ENOENT;
5305 goto err_out;
5306 }
57a00f41
THJ
5307 }
5308
26736eb9 5309 if (map->fd >= 0) {
2d39d7c5 5310 pr_debug("map '%s': skipping creation (preset fd=%d)\n",
26736eb9 5311 map->name, map->fd);
2c193d32 5312 } else {
67234743 5313 err = bpf_object__create_map(obj, map, false);
2c193d32 5314 if (err)
d859900c 5315 goto err_out;
d859900c 5316
2c193d32
HL
5317 pr_debug("map '%s': created successfully, fd=%d\n",
5318 map->name, map->fd);
646f02ff 5319
2c193d32
HL
5320 if (bpf_map__is_internal(map)) {
5321 err = bpf_object__populate_internal_map(obj, map);
5322 if (err < 0) {
5323 zclose(map->fd);
5324 goto err_out;
5325 }
d859900c 5326 }
646f02ff 5327
341ac5ff
HC
5328 if (map->init_slots_sz && map->def.type != BPF_MAP_TYPE_PROG_ARRAY) {
5329 err = init_map_in_map_slots(obj, map);
2c193d32
HL
5330 if (err < 0) {
5331 zclose(map->fd);
646f02ff
AN
5332 goto err_out;
5333 }
646f02ff 5334 }
646f02ff
AN
5335 }
5336
57a00f41
THJ
5337 if (map->pin_path && !map->pinned) {
5338 err = bpf_map__pin(map, NULL);
5339 if (err) {
043c5bb3
MP
5340 zclose(map->fd);
5341 if (!retried && err == -EEXIST) {
5342 retried = true;
5343 goto retry;
5344 }
2d39d7c5
AN
5345 pr_warn("map '%s': failed to auto-pin at '%s': %d\n",
5346 map->name, map->pin_path, err);
2d39d7c5 5347 goto err_out;
57a00f41
THJ
5348 }
5349 }
52d3352e
WN
5350 }
5351
52d3352e 5352 return 0;
2d39d7c5
AN
5353
5354err_out:
5355 cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg));
5356 pr_warn("map '%s': failed to create: %s(%d)\n", map->name, cp, err);
5357 pr_perm_msg(err);
5358 for (j = 0; j < i; j++)
5359 zclose(obj->maps[j].fd);
5360 return err;
52d3352e
WN
5361}
5362
ddc7c304
AN
5363static bool bpf_core_is_flavor_sep(const char *s)
5364{
5365 /* check X___Y name pattern, where X and Y are not underscores */
5366 return s[0] != '_' && /* X */
5367 s[1] == '_' && s[2] == '_' && s[3] == '_' && /* ___ */
5368 s[4] != '_'; /* Y */
5369}
5370
5371/* Given 'some_struct_name___with_flavor' return the length of a name prefix
5372 * before last triple underscore. Struct name part after last triple
5373 * underscore is ignored by BPF CO-RE relocation during relocation matching.
5374 */
b0588390 5375size_t bpf_core_essential_name_len(const char *name)
ddc7c304
AN
5376{
5377 size_t n = strlen(name);
5378 int i;
5379
5380 for (i = n - 5; i >= 0; i--) {
5381 if (bpf_core_is_flavor_sep(name + i))
5382 return i + 1;
5383 }
5384 return n;
5385}
5386
8de6cae4 5387void bpf_core_free_cands(struct bpf_core_cand_list *cands)
ddc7c304 5388{
8de6cae4
MV
5389 if (!cands)
5390 return;
5391
0f7515ca
AN
5392 free(cands->cands);
5393 free(cands);
ddc7c304
AN
5394}
5395
8de6cae4
MV
5396int bpf_core_add_cands(struct bpf_core_cand *local_cand,
5397 size_t local_essent_len,
5398 const struct btf *targ_btf,
5399 const char *targ_btf_name,
5400 int targ_start_id,
5401 struct bpf_core_cand_list *cands)
ddc7c304 5402{
301ba4d7 5403 struct bpf_core_cand *new_cands, *cand;
03d5b991
AN
5404 const struct btf_type *t, *local_t;
5405 const char *targ_name, *local_name;
0f7515ca
AN
5406 size_t targ_essent_len;
5407 int n, i;
ddc7c304 5408
03d5b991
AN
5409 local_t = btf__type_by_id(local_cand->btf, local_cand->id);
5410 local_name = btf__str_by_offset(local_cand->btf, local_t->name_off);
5411
6a886de0
HC
5412 n = btf__type_cnt(targ_btf);
5413 for (i = targ_start_id; i < n; i++) {
ddc7c304 5414 t = btf__type_by_id(targ_btf, i);
23b2a3a8 5415 if (!btf_kind_core_compat(t, local_t))
ddc7c304
AN
5416 continue;
5417
3fc32f40
AN
5418 targ_name = btf__name_by_offset(targ_btf, t->name_off);
5419 if (str_is_empty(targ_name))
d121e1d3
AN
5420 continue;
5421
ddc7c304
AN
5422 targ_essent_len = bpf_core_essential_name_len(targ_name);
5423 if (targ_essent_len != local_essent_len)
5424 continue;
5425
03d5b991 5426 if (strncmp(local_name, targ_name, local_essent_len) != 0)
0f7515ca
AN
5427 continue;
5428
5429 pr_debug("CO-RE relocating [%d] %s %s: found target candidate [%d] %s %s in [%s]\n",
03d5b991
AN
5430 local_cand->id, btf_kind_str(local_t),
5431 local_name, i, btf_kind_str(t), targ_name,
0f7515ca
AN
5432 targ_btf_name);
5433 new_cands = libbpf_reallocarray(cands->cands, cands->len + 1,
5434 sizeof(*cands->cands));
5435 if (!new_cands)
5436 return -ENOMEM;
5437
5438 cand = &new_cands[cands->len];
5439 cand->btf = targ_btf;
0f7515ca
AN
5440 cand->id = i;
5441
5442 cands->cands = new_cands;
5443 cands->len++;
ddc7c304 5444 }
0f7515ca
AN
5445 return 0;
5446}
5447
4f33a53d
AN
5448static int load_module_btfs(struct bpf_object *obj)
5449{
5450 struct bpf_btf_info info;
5451 struct module_btf *mod_btf;
5452 struct btf *btf;
5453 char name[64];
5454 __u32 id = 0, len;
5455 int err, fd;
5456
5457 if (obj->btf_modules_loaded)
5458 return 0;
5459
67234743
AS
5460 if (obj->gen_loader)
5461 return 0;
5462
4f33a53d
AN
5463 /* don't do this again, even if we find no module BTFs */
5464 obj->btf_modules_loaded = true;
5465
5466 /* kernel too old to support module BTFs */
9ca1f56a 5467 if (!kernel_supports(obj, FEAT_MODULE_BTF))
4f33a53d
AN
5468 return 0;
5469
5470 while (true) {
5471 err = bpf_btf_get_next_id(id, &id);
5472 if (err && errno == ENOENT)
5473 return 0;
5474 if (err) {
5475 err = -errno;
5476 pr_warn("failed to iterate BTF objects: %d\n", err);
5477 return err;
ddc7c304 5478 }
4f33a53d
AN
5479
5480 fd = bpf_btf_get_fd_by_id(id);
5481 if (fd < 0) {
5482 if (errno == ENOENT)
5483 continue; /* expected race: BTF was unloaded */
5484 err = -errno;
5485 pr_warn("failed to get BTF object #%d FD: %d\n", id, err);
5486 return err;
5487 }
5488
5489 len = sizeof(info);
5490 memset(&info, 0, sizeof(info));
5491 info.name = ptr_to_u64(name);
5492 info.name_len = sizeof(name);
5493
629dfc66 5494 err = bpf_btf_get_info_by_fd(fd, &info, &len);
4f33a53d
AN
5495 if (err) {
5496 err = -errno;
5497 pr_warn("failed to get BTF object #%d info: %d\n", id, err);
91abb4a6 5498 goto err_out;
4f33a53d
AN
5499 }
5500
5501 /* ignore non-module BTFs */
5502 if (!info.kernel_btf || strcmp(name, "vmlinux") == 0) {
5503 close(fd);
5504 continue;
5505 }
5506
5507 btf = btf_get_from_fd(fd, obj->btf_vmlinux);
e9fc3ce9
AN
5508 err = libbpf_get_error(btf);
5509 if (err) {
5510 pr_warn("failed to load module [%s]'s BTF object #%d: %d\n",
5511 name, id, err);
91abb4a6 5512 goto err_out;
4f33a53d
AN
5513 }
5514
3b029e06 5515 err = libbpf_ensure_mem((void **)&obj->btf_modules, &obj->btf_module_cap,
e3ba8e4e 5516 sizeof(*obj->btf_modules), obj->btf_module_cnt + 1);
4f33a53d 5517 if (err)
91abb4a6 5518 goto err_out;
4f33a53d
AN
5519
5520 mod_btf = &obj->btf_modules[obj->btf_module_cnt++];
5521
5522 mod_btf->btf = btf;
5523 mod_btf->id = id;
91abb4a6 5524 mod_btf->fd = fd;
4f33a53d 5525 mod_btf->name = strdup(name);
91abb4a6
AN
5526 if (!mod_btf->name) {
5527 err = -ENOMEM;
5528 goto err_out;
5529 }
5530 continue;
5531
5532err_out:
5533 close(fd);
5534 return err;
ddc7c304 5535 }
4f33a53d
AN
5536
5537 return 0;
5538}
5539
301ba4d7 5540static struct bpf_core_cand_list *
0f7515ca
AN
5541bpf_core_find_cands(struct bpf_object *obj, const struct btf *local_btf, __u32 local_type_id)
5542{
301ba4d7
AS
5543 struct bpf_core_cand local_cand = {};
5544 struct bpf_core_cand_list *cands;
4f33a53d 5545 const struct btf *main_btf;
03d5b991
AN
5546 const struct btf_type *local_t;
5547 const char *local_name;
0f7515ca 5548 size_t local_essent_len;
4f33a53d 5549 int err, i;
0f7515ca
AN
5550
5551 local_cand.btf = local_btf;
03d5b991
AN
5552 local_cand.id = local_type_id;
5553 local_t = btf__type_by_id(local_btf, local_type_id);
5554 if (!local_t)
0f7515ca
AN
5555 return ERR_PTR(-EINVAL);
5556
03d5b991
AN
5557 local_name = btf__name_by_offset(local_btf, local_t->name_off);
5558 if (str_is_empty(local_name))
0f7515ca 5559 return ERR_PTR(-EINVAL);
03d5b991 5560 local_essent_len = bpf_core_essential_name_len(local_name);
0f7515ca
AN
5561
5562 cands = calloc(1, sizeof(*cands));
5563 if (!cands)
5564 return ERR_PTR(-ENOMEM);
5565
5566 /* Attempt to find target candidates in vmlinux BTF first */
4f33a53d
AN
5567 main_btf = obj->btf_vmlinux_override ?: obj->btf_vmlinux;
5568 err = bpf_core_add_cands(&local_cand, local_essent_len, main_btf, "vmlinux", 1, cands);
5569 if (err)
5570 goto err_out;
5571
5572 /* if vmlinux BTF has any candidate, don't got for module BTFs */
5573 if (cands->len)
5574 return cands;
5575
5576 /* if vmlinux BTF was overridden, don't attempt to load module BTFs */
5577 if (obj->btf_vmlinux_override)
5578 return cands;
5579
5580 /* now look through module BTFs, trying to still find candidates */
5581 err = load_module_btfs(obj);
5582 if (err)
5583 goto err_out;
5584
5585 for (i = 0; i < obj->btf_module_cnt; i++) {
5586 err = bpf_core_add_cands(&local_cand, local_essent_len,
5587 obj->btf_modules[i].btf,
5588 obj->btf_modules[i].name,
6a886de0 5589 btf__type_cnt(obj->btf_vmlinux),
4f33a53d
AN
5590 cands);
5591 if (err)
5592 goto err_out;
0f7515ca
AN
5593 }
5594
5595 return cands;
ddc7c304 5596err_out:
4f33a53d 5597 bpf_core_free_cands(cands);
ddc7c304
AN
5598 return ERR_PTR(err);
5599}
5600
3fc32f40
AN
5601/* Check local and target types for compatibility. This check is used for
5602 * type-based CO-RE relocations and follow slightly different rules than
5603 * field-based relocations. This function assumes that root types were already
5604 * checked for name match. Beyond that initial root-level name check, names
5605 * are completely ignored. Compatibility rules are as follows:
5606 * - any two STRUCTs/UNIONs/FWDs/ENUMs/INTs are considered compatible, but
5607 * kind should match for local and target types (i.e., STRUCT is not
5608 * compatible with UNION);
5609 * - for ENUMs, the size is ignored;
5610 * - for INT, size and signedness are ignored;
5611 * - for ARRAY, dimensionality is ignored, element types are checked for
5612 * compatibility recursively;
5613 * - CONST/VOLATILE/RESTRICT modifiers are ignored;
5614 * - TYPEDEFs/PTRs are compatible if types they pointing to are compatible;
5615 * - FUNC_PROTOs are compatible if they have compatible signature: same
5616 * number of input args and compatible return and argument types.
5617 * These rules are not set in stone and probably will be adjusted as we get
5618 * more experience with using BPF CO-RE relocations.
5619 */
b0588390
AS
5620int bpf_core_types_are_compat(const struct btf *local_btf, __u32 local_id,
5621 const struct btf *targ_btf, __u32 targ_id)
3fc32f40 5622{
fd75733d 5623 return __bpf_core_types_are_compat(local_btf, local_id, targ_btf, targ_id, 32);
3fc32f40
AN
5624}
5625
ec6209c8
DM
5626int bpf_core_types_match(const struct btf *local_btf, __u32 local_id,
5627 const struct btf *targ_btf, __u32 targ_id)
5628{
5629 return __bpf_core_types_match(local_btf, local_id, targ_btf, targ_id, false, 32);
5630}
5631
c302378b 5632static size_t bpf_core_hash_fn(const long key, void *ctx)
ddc7c304 5633{
c302378b 5634 return key;
ddc7c304
AN
5635}
5636
c302378b 5637static bool bpf_core_equal_fn(const long k1, const long k2, void *ctx)
ddc7c304
AN
5638{
5639 return k1 == k2;
5640}
5641
d0e92887
AS
5642static int record_relo_core(struct bpf_program *prog,
5643 const struct bpf_core_relo *core_relo, int insn_idx)
5644{
5645 struct reloc_desc *relos, *relo;
5646
5647 relos = libbpf_reallocarray(prog->reloc_desc,
5648 prog->nr_reloc + 1, sizeof(*relos));
5649 if (!relos)
5650 return -ENOMEM;
5651 relo = &relos[prog->nr_reloc];
5652 relo->type = RELO_CORE;
5653 relo->insn_idx = insn_idx;
5654 relo->core_relo = core_relo;
5655 prog->reloc_desc = relos;
5656 prog->nr_reloc++;
5657 return 0;
5658}
5659
9fdc4273
AN
5660static const struct bpf_core_relo *find_relo_core(struct bpf_program *prog, int insn_idx)
5661{
5662 struct reloc_desc *relo;
5663 int i;
5664
5665 for (i = 0; i < prog->nr_reloc; i++) {
5666 relo = &prog->reloc_desc[i];
5667 if (relo->type != RELO_CORE || relo->insn_idx != insn_idx)
5668 continue;
5669
5670 return relo->core_relo;
5671 }
5672
5673 return NULL;
5674}
5675
adb8fa19
MV
5676static int bpf_core_resolve_relo(struct bpf_program *prog,
5677 const struct bpf_core_relo *relo,
5678 int relo_idx,
5679 const struct btf *local_btf,
5680 struct hashmap *cand_cache,
5681 struct bpf_core_relo_res *targ_res)
3ee4f533 5682{
78c1f8d0 5683 struct bpf_core_spec specs_scratch[3] = {};
301ba4d7 5684 struct bpf_core_cand_list *cands = NULL;
3ee4f533
AS
5685 const char *prog_name = prog->name;
5686 const struct btf_type *local_type;
5687 const char *local_name;
5688 __u32 local_id = relo->type_id;
adb8fa19 5689 int err;
3ee4f533
AS
5690
5691 local_type = btf__type_by_id(local_btf, local_id);
5692 if (!local_type)
5693 return -EINVAL;
5694
5695 local_name = btf__name_by_offset(local_btf, local_type->name_off);
5696 if (!local_name)
5697 return -EINVAL;
5698
46334a0c 5699 if (relo->kind != BPF_CORE_TYPE_ID_LOCAL &&
c302378b 5700 !hashmap__find(cand_cache, local_id, &cands)) {
3ee4f533
AS
5701 cands = bpf_core_find_cands(prog->obj, local_btf, local_id);
5702 if (IS_ERR(cands)) {
5703 pr_warn("prog '%s': relo #%d: target candidate search failed for [%d] %s %s: %ld\n",
5704 prog_name, relo_idx, local_id, btf_kind_str(local_type),
5705 local_name, PTR_ERR(cands));
5706 return PTR_ERR(cands);
5707 }
c302378b 5708 err = hashmap__set(cand_cache, local_id, cands, NULL, NULL);
3ee4f533
AS
5709 if (err) {
5710 bpf_core_free_cands(cands);
5711 return err;
5712 }
5713 }
5714
adb8fa19
MV
5715 return bpf_core_calc_relo_insn(prog_name, relo, relo_idx, local_btf, cands, specs_scratch,
5716 targ_res);
3ee4f533
AS
5717}
5718
ddc7c304 5719static int
28b93c64 5720bpf_object__relocate_core(struct bpf_object *obj, const char *targ_btf_path)
ddc7c304
AN
5721{
5722 const struct btf_ext_info_sec *sec;
adb8fa19 5723 struct bpf_core_relo_res targ_res;
28b93c64 5724 const struct bpf_core_relo *rec;
ddc7c304
AN
5725 const struct btf_ext_info *seg;
5726 struct hashmap_entry *entry;
5727 struct hashmap *cand_cache = NULL;
5728 struct bpf_program *prog;
adb8fa19 5729 struct bpf_insn *insn;
ddc7c304 5730 const char *sec_name;
11d5daa8 5731 int i, err = 0, insn_idx, sec_idx, sec_num;
ddc7c304 5732
28b93c64
AN
5733 if (obj->btf_ext->core_relo_info.len == 0)
5734 return 0;
5735
0f7515ca
AN
5736 if (targ_btf_path) {
5737 obj->btf_vmlinux_override = btf__parse(targ_btf_path, NULL);
e9fc3ce9
AN
5738 err = libbpf_get_error(obj->btf_vmlinux_override);
5739 if (err) {
0f7515ca
AN
5740 pr_warn("failed to parse target BTF: %d\n", err);
5741 return err;
5742 }
ddc7c304
AN
5743 }
5744
5745 cand_cache = hashmap__new(bpf_core_hash_fn, bpf_core_equal_fn, NULL);
5746 if (IS_ERR(cand_cache)) {
5747 err = PTR_ERR(cand_cache);
5748 goto out;
5749 }
5750
28b93c64 5751 seg = &obj->btf_ext->core_relo_info;
11d5daa8 5752 sec_num = 0;
ddc7c304 5753 for_each_btf_ext_sec(seg, sec) {
11d5daa8
AN
5754 sec_idx = seg->sec_idxs[sec_num];
5755 sec_num++;
5756
ddc7c304
AN
5757 sec_name = btf__name_by_offset(obj->btf, sec->sec_name_off);
5758 if (str_is_empty(sec_name)) {
5759 err = -EINVAL;
5760 goto out;
5761 }
ddc7c304 5762
11d5daa8 5763 pr_debug("sec '%s': found %d CO-RE relocations\n", sec_name, sec->num_info);
ddc7c304
AN
5764
5765 for_each_btf_ext_rec(seg, sec, i, rec) {
adb8fa19
MV
5766 if (rec->insn_off % BPF_INSN_SZ)
5767 return -EINVAL;
db2b8b06
AN
5768 insn_idx = rec->insn_off / BPF_INSN_SZ;
5769 prog = find_prog_by_sec_insn(obj, sec_idx, insn_idx);
5770 if (!prog) {
e89d57d9
AN
5771 /* When __weak subprog is "overridden" by another instance
5772 * of the subprog from a different object file, linker still
5773 * appends all the .BTF.ext info that used to belong to that
5774 * eliminated subprogram.
5775 * This is similar to what x86-64 linker does for relocations.
5776 * So just ignore such relocations just like we ignore
5777 * subprog instructions when discovering subprograms.
5778 */
5779 pr_debug("sec '%s': skipping CO-RE relocation #%d for insn #%d belonging to eliminated weak subprogram\n",
5780 sec_name, i, insn_idx);
5781 continue;
db2b8b06 5782 }
47f7cf63
AN
5783 /* no need to apply CO-RE relocation if the program is
5784 * not going to be loaded
5785 */
a3820c48 5786 if (!prog->autoload)
47f7cf63 5787 continue;
db2b8b06 5788
adb8fa19
MV
5789 /* adjust insn_idx from section frame of reference to the local
5790 * program's frame of reference; (sub-)program code is not yet
5791 * relocated, so it's enough to just subtract in-section offset
5792 */
5793 insn_idx = insn_idx - prog->sec_insn_off;
5794 if (insn_idx >= prog->insns_cnt)
5795 return -EINVAL;
5796 insn = &prog->insns[insn_idx];
5797
185cfe83
AN
5798 err = record_relo_core(prog, rec, insn_idx);
5799 if (err) {
5800 pr_warn("prog '%s': relo #%d: failed to record relocation: %d\n",
5801 prog->name, i, err);
5802 goto out;
adb8fa19
MV
5803 }
5804
185cfe83
AN
5805 if (prog->obj->gen_loader)
5806 continue;
5807
adb8fa19 5808 err = bpf_core_resolve_relo(prog, rec, i, obj->btf, cand_cache, &targ_res);
ddc7c304 5809 if (err) {
be18010e 5810 pr_warn("prog '%s': relo #%d: failed to relocate: %d\n",
9c0f8cbd 5811 prog->name, i, err);
ddc7c304
AN
5812 goto out;
5813 }
adb8fa19
MV
5814
5815 err = bpf_core_patch_insn(prog->name, insn, insn_idx, rec, i, &targ_res);
5816 if (err) {
5817 pr_warn("prog '%s': relo #%d: failed to patch insn #%u: %d\n",
5818 prog->name, i, insn_idx, err);
5819 goto out;
5820 }
ddc7c304
AN
5821 }
5822 }
5823
5824out:
4f33a53d 5825 /* obj->btf_vmlinux and module BTFs are freed after object load */
0f7515ca
AN
5826 btf__free(obj->btf_vmlinux_override);
5827 obj->btf_vmlinux_override = NULL;
5828
ddc7c304
AN
5829 if (!IS_ERR_OR_NULL(cand_cache)) {
5830 hashmap__for_each_entry(cand_cache, entry, i) {
c302378b 5831 bpf_core_free_cands(entry->pvalue);
ddc7c304
AN
5832 }
5833 hashmap__free(cand_cache);
5834 }
5835 return err;
5836}
5837
ec41817b 5838/* base map load ldimm64 special constant, used also for log fixup logic */
3055ddd6
AN
5839#define POISON_LDIMM64_MAP_BASE 2001000000
5840#define POISON_LDIMM64_MAP_PFX "200100"
ec41817b
AN
5841
5842static void poison_map_ldimm64(struct bpf_program *prog, int relo_idx,
5843 int insn_idx, struct bpf_insn *insn,
5844 int map_idx, const struct bpf_map *map)
5845{
5846 int i;
5847
5848 pr_debug("prog '%s': relo #%d: poisoning insn #%d that loads map #%d '%s'\n",
5849 prog->name, relo_idx, insn_idx, map_idx, map->name);
5850
5851 /* we turn single ldimm64 into two identical invalid calls */
5852 for (i = 0; i < 2; i++) {
5853 insn->code = BPF_JMP | BPF_CALL;
5854 insn->dst_reg = 0;
5855 insn->src_reg = 0;
5856 insn->off = 0;
5857 /* if this instruction is reachable (not a dead code),
5858 * verifier will complain with something like:
5859 * invalid func unknown#2001000123
5860 * where lower 123 is map index into obj->maps[] array
5861 */
3055ddd6 5862 insn->imm = POISON_LDIMM64_MAP_BASE + map_idx;
ec41817b
AN
5863
5864 insn++;
5865 }
5866}
5867
05b6f766
AN
5868/* unresolved kfunc call special constant, used also for log fixup logic */
5869#define POISON_CALL_KFUNC_BASE 2002000000
5870#define POISON_CALL_KFUNC_PFX "2002"
5871
5872static void poison_kfunc_call(struct bpf_program *prog, int relo_idx,
5873 int insn_idx, struct bpf_insn *insn,
5874 int ext_idx, const struct extern_desc *ext)
5875{
5876 pr_debug("prog '%s': relo #%d: poisoning insn #%d that calls kfunc '%s'\n",
5877 prog->name, relo_idx, insn_idx, ext->name);
5878
5879 /* we turn kfunc call into invalid helper call with identifiable constant */
5880 insn->code = BPF_JMP | BPF_CALL;
5881 insn->dst_reg = 0;
5882 insn->src_reg = 0;
5883 insn->off = 0;
5884 /* if this instruction is reachable (not a dead code),
5885 * verifier will complain with something like:
5886 * invalid func unknown#2001000123
5887 * where lower 123 is extern index into obj->externs[] array
5888 */
5889 insn->imm = POISON_CALL_KFUNC_BASE + ext_idx;
5890}
5891
c3c55696
AN
5892/* Relocate data references within program code:
5893 * - map references;
5894 * - global variable references;
5895 * - extern references.
5896 */
48cca7e4 5897static int
c3c55696 5898bpf_object__relocate_data(struct bpf_object *obj, struct bpf_program *prog)
8a47a6c5 5899{
c3c55696 5900 int i;
8a47a6c5
WN
5901
5902 for (i = 0; i < prog->nr_reloc; i++) {
53f8dd43 5903 struct reloc_desc *relo = &prog->reloc_desc[i];
166750bc 5904 struct bpf_insn *insn = &prog->insns[relo->insn_idx];
ec41817b 5905 const struct bpf_map *map;
2e33efe3 5906 struct extern_desc *ext;
8a47a6c5 5907
166750bc
AN
5908 switch (relo->type) {
5909 case RELO_LD64:
ec41817b 5910 map = &obj->maps[relo->map_idx];
e2fa0156
AS
5911 if (obj->gen_loader) {
5912 insn[0].src_reg = BPF_PSEUDO_MAP_IDX;
5913 insn[0].imm = relo->map_idx;
ec41817b 5914 } else if (map->autocreate) {
e2fa0156 5915 insn[0].src_reg = BPF_PSEUDO_MAP_FD;
ec41817b
AN
5916 insn[0].imm = map->fd;
5917 } else {
5918 poison_map_ldimm64(prog, i, relo->insn_idx, insn,
5919 relo->map_idx, map);
e2fa0156 5920 }
166750bc
AN
5921 break;
5922 case RELO_DATA:
ec41817b 5923 map = &obj->maps[relo->map_idx];
166750bc 5924 insn[1].imm = insn[0].imm + relo->sym_off;
e2fa0156
AS
5925 if (obj->gen_loader) {
5926 insn[0].src_reg = BPF_PSEUDO_MAP_IDX_VALUE;
5927 insn[0].imm = relo->map_idx;
ec41817b 5928 } else if (map->autocreate) {
e2fa0156 5929 insn[0].src_reg = BPF_PSEUDO_MAP_VALUE;
ec41817b
AN
5930 insn[0].imm = map->fd;
5931 } else {
5932 poison_map_ldimm64(prog, i, relo->insn_idx, insn,
5933 relo->map_idx, map);
e2fa0156 5934 }
166750bc 5935 break;
a18f7214 5936 case RELO_EXTERN_LD64:
3055ddd6 5937 ext = &obj->externs[relo->ext_idx];
1c0c7074 5938 if (ext->type == EXT_KCFG) {
e2fa0156
AS
5939 if (obj->gen_loader) {
5940 insn[0].src_reg = BPF_PSEUDO_MAP_IDX_VALUE;
5941 insn[0].imm = obj->kconfig_map_idx;
5942 } else {
5943 insn[0].src_reg = BPF_PSEUDO_MAP_VALUE;
5944 insn[0].imm = obj->maps[obj->kconfig_map_idx].fd;
5945 }
1c0c7074
AN
5946 insn[1].imm = ext->kcfg.data_off;
5947 } else /* EXT_KSYM */ {
2211c825 5948 if (ext->ksym.type_id && ext->is_set) { /* typed ksyms */
d370bbe1 5949 insn[0].src_reg = BPF_PSEUDO_BTF_ID;
284d2587
AN
5950 insn[0].imm = ext->ksym.kernel_btf_id;
5951 insn[1].imm = ext->ksym.kernel_btf_obj_fd;
2211c825 5952 } else { /* typeless ksyms or unresolved typed ksyms */
d370bbe1
HL
5953 insn[0].imm = (__u32)ext->ksym.addr;
5954 insn[1].imm = ext->ksym.addr >> 32;
5955 }
1c0c7074 5956 }
166750bc 5957 break;
a18f7214 5958 case RELO_EXTERN_CALL:
3055ddd6 5959 ext = &obj->externs[relo->ext_idx];
5bd022ec 5960 insn[0].src_reg = BPF_PSEUDO_KFUNC_CALL;
466b2e13
KKD
5961 if (ext->is_set) {
5962 insn[0].imm = ext->ksym.kernel_btf_id;
5963 insn[0].off = ext->ksym.btf_fd_idx;
05b6f766
AN
5964 } else { /* unresolved weak kfunc call */
5965 poison_kfunc_call(prog, i, relo->insn_idx, insn,
5966 relo->ext_idx, ext);
466b2e13 5967 }
5bd022ec 5968 break;
53eddb5e 5969 case RELO_SUBPROG_ADDR:
b1268826
AS
5970 if (insn[0].src_reg != BPF_PSEUDO_FUNC) {
5971 pr_warn("prog '%s': relo #%d: bad insn\n",
5972 prog->name, i);
5973 return -EINVAL;
5974 }
5975 /* handled already */
53eddb5e 5976 break;
166750bc 5977 case RELO_CALL:
b1268826 5978 /* handled already */
166750bc 5979 break;
d0e92887
AS
5980 case RELO_CORE:
5981 /* will be handled by bpf_program_record_relos() */
5982 break;
166750bc 5983 default:
9c0f8cbd
AN
5984 pr_warn("prog '%s': relo #%d: bad relo type %d\n",
5985 prog->name, i, relo->type);
166750bc 5986 return -EINVAL;
8a47a6c5 5987 }
8a47a6c5
WN
5988 }
5989
c3c55696
AN
5990 return 0;
5991}
5992
8505e870
AN
5993static int adjust_prog_btf_ext_info(const struct bpf_object *obj,
5994 const struct bpf_program *prog,
5995 const struct btf_ext_info *ext_info,
5996 void **prog_info, __u32 *prog_rec_cnt,
5997 __u32 *prog_rec_sz)
5998{
5999 void *copy_start = NULL, *copy_end = NULL;
6000 void *rec, *rec_end, *new_prog_info;
6001 const struct btf_ext_info_sec *sec;
6002 size_t old_sz, new_sz;
11d5daa8 6003 int i, sec_num, sec_idx, off_adj;
8505e870 6004
11d5daa8 6005 sec_num = 0;
8505e870 6006 for_each_btf_ext_sec(ext_info, sec) {
11d5daa8
AN
6007 sec_idx = ext_info->sec_idxs[sec_num];
6008 sec_num++;
6009 if (prog->sec_idx != sec_idx)
8505e870
AN
6010 continue;
6011
6012 for_each_btf_ext_rec(ext_info, sec, i, rec) {
6013 __u32 insn_off = *(__u32 *)rec / BPF_INSN_SZ;
6014
6015 if (insn_off < prog->sec_insn_off)
6016 continue;
6017 if (insn_off >= prog->sec_insn_off + prog->sec_insn_cnt)
6018 break;
6019
6020 if (!copy_start)
6021 copy_start = rec;
6022 copy_end = rec + ext_info->rec_size;
6023 }
6024
6025 if (!copy_start)
6026 return -ENOENT;
6027
6028 /* append func/line info of a given (sub-)program to the main
6029 * program func/line info
6030 */
8eb62958 6031 old_sz = (size_t)(*prog_rec_cnt) * ext_info->rec_size;
8505e870
AN
6032 new_sz = old_sz + (copy_end - copy_start);
6033 new_prog_info = realloc(*prog_info, new_sz);
6034 if (!new_prog_info)
6035 return -ENOMEM;
6036 *prog_info = new_prog_info;
6037 *prog_rec_cnt = new_sz / ext_info->rec_size;
6038 memcpy(new_prog_info + old_sz, copy_start, copy_end - copy_start);
6039
6040 /* Kernel instruction offsets are in units of 8-byte
6041 * instructions, while .BTF.ext instruction offsets generated
6042 * by Clang are in units of bytes. So convert Clang offsets
6043 * into kernel offsets and adjust offset according to program
6044 * relocated position.
6045 */
6046 off_adj = prog->sub_insn_off - prog->sec_insn_off;
6047 rec = new_prog_info + old_sz;
6048 rec_end = new_prog_info + new_sz;
6049 for (; rec < rec_end; rec += ext_info->rec_size) {
6050 __u32 *insn_off = rec;
6051
6052 *insn_off = *insn_off / BPF_INSN_SZ + off_adj;
6053 }
6054 *prog_rec_sz = ext_info->rec_size;
6055 return 0;
6056 }
6057
6058 return -ENOENT;
6059}
6060
6061static int
6062reloc_prog_func_and_line_info(const struct bpf_object *obj,
6063 struct bpf_program *main_prog,
6064 const struct bpf_program *prog)
6065{
6066 int err;
6067
6068 /* no .BTF.ext relocation if .BTF.ext is missing or kernel doesn't
6069 * supprot func/line info
6070 */
9ca1f56a 6071 if (!obj->btf_ext || !kernel_supports(obj, FEAT_BTF_FUNC))
8505e870
AN
6072 return 0;
6073
6074 /* only attempt func info relocation if main program's func_info
6075 * relocation was successful
6076 */
6077 if (main_prog != prog && !main_prog->func_info)
6078 goto line_info;
6079
6080 err = adjust_prog_btf_ext_info(obj, prog, &obj->btf_ext->func_info,
6081 &main_prog->func_info,
6082 &main_prog->func_info_cnt,
6083 &main_prog->func_info_rec_size);
6084 if (err) {
6085 if (err != -ENOENT) {
6086 pr_warn("prog '%s': error relocating .BTF.ext function info: %d\n",
6087 prog->name, err);
6088 return err;
6089 }
6090 if (main_prog->func_info) {
6091 /*
6092 * Some info has already been found but has problem
6093 * in the last btf_ext reloc. Must have to error out.
6094 */
6095 pr_warn("prog '%s': missing .BTF.ext function info.\n", prog->name);
6096 return err;
6097 }
6098 /* Have problem loading the very first info. Ignore the rest. */
6099 pr_warn("prog '%s': missing .BTF.ext function info for the main program, skipping all of .BTF.ext func info.\n",
6100 prog->name);
6101 }
6102
6103line_info:
6104 /* don't relocate line info if main program's relocation failed */
6105 if (main_prog != prog && !main_prog->line_info)
6106 return 0;
6107
6108 err = adjust_prog_btf_ext_info(obj, prog, &obj->btf_ext->line_info,
6109 &main_prog->line_info,
6110 &main_prog->line_info_cnt,
6111 &main_prog->line_info_rec_size);
6112 if (err) {
6113 if (err != -ENOENT) {
6114 pr_warn("prog '%s': error relocating .BTF.ext line info: %d\n",
6115 prog->name, err);
6116 return err;
6117 }
6118 if (main_prog->line_info) {
6119 /*
6120 * Some info has already been found but has problem
6121 * in the last btf_ext reloc. Must have to error out.
6122 */
6123 pr_warn("prog '%s': missing .BTF.ext line info.\n", prog->name);
6124 return err;
6125 }
6126 /* Have problem loading the very first info. Ignore the rest. */
6127 pr_warn("prog '%s': missing .BTF.ext line info for the main program, skipping all of .BTF.ext line info.\n",
6128 prog->name);
6129 }
6130 return 0;
6131}
6132
c3c55696
AN
6133static int cmp_relo_by_insn_idx(const void *key, const void *elem)
6134{
6135 size_t insn_idx = *(const size_t *)key;
6136 const struct reloc_desc *relo = elem;
6137
6138 if (insn_idx == relo->insn_idx)
6139 return 0;
6140 return insn_idx < relo->insn_idx ? -1 : 1;
6141}
6142
6143static struct reloc_desc *find_prog_insn_relo(const struct bpf_program *prog, size_t insn_idx)
6144{
2a6a9bf2
AN
6145 if (!prog->nr_reloc)
6146 return NULL;
c3c55696
AN
6147 return bsearch(&insn_idx, prog->reloc_desc, prog->nr_reloc,
6148 sizeof(*prog->reloc_desc), cmp_relo_by_insn_idx);
6149}
6150
b1268826
AS
6151static int append_subprog_relos(struct bpf_program *main_prog, struct bpf_program *subprog)
6152{
6153 int new_cnt = main_prog->nr_reloc + subprog->nr_reloc;
6154 struct reloc_desc *relos;
6155 int i;
6156
6157 if (main_prog == subprog)
6158 return 0;
6159 relos = libbpf_reallocarray(main_prog->reloc_desc, new_cnt, sizeof(*relos));
6160 if (!relos)
6161 return -ENOMEM;
2a6a9bf2
AN
6162 if (subprog->nr_reloc)
6163 memcpy(relos + main_prog->nr_reloc, subprog->reloc_desc,
6164 sizeof(*relos) * subprog->nr_reloc);
b1268826
AS
6165
6166 for (i = main_prog->nr_reloc; i < new_cnt; i++)
6167 relos[i].insn_idx += subprog->sub_insn_off;
6168 /* After insn_idx adjustment the 'relos' array is still sorted
6169 * by insn_idx and doesn't break bsearch.
6170 */
6171 main_prog->reloc_desc = relos;
6172 main_prog->nr_reloc = new_cnt;
6173 return 0;
6174}
6175
c3c55696
AN
6176static int
6177bpf_object__reloc_code(struct bpf_object *obj, struct bpf_program *main_prog,
6178 struct bpf_program *prog)
6179{
6180 size_t sub_insn_idx, insn_idx, new_cnt;
6181 struct bpf_program *subprog;
6182 struct bpf_insn *insns, *insn;
6183 struct reloc_desc *relo;
6184 int err;
6185
6186 err = reloc_prog_func_and_line_info(obj, main_prog, prog);
6187 if (err)
6188 return err;
6189
6190 for (insn_idx = 0; insn_idx < prog->sec_insn_cnt; insn_idx++) {
6191 insn = &main_prog->insns[prog->sub_insn_off + insn_idx];
53eddb5e 6192 if (!insn_is_subprog_call(insn) && !insn_is_pseudo_func(insn))
c3c55696
AN
6193 continue;
6194
6195 relo = find_prog_insn_relo(prog, insn_idx);
a18f7214 6196 if (relo && relo->type == RELO_EXTERN_CALL)
b1268826
AS
6197 /* kfunc relocations will be handled later
6198 * in bpf_object__relocate_data()
6199 */
6200 continue;
53eddb5e 6201 if (relo && relo->type != RELO_CALL && relo->type != RELO_SUBPROG_ADDR) {
c3c55696
AN
6202 pr_warn("prog '%s': unexpected relo for insn #%zu, type %d\n",
6203 prog->name, insn_idx, relo->type);
6204 return -LIBBPF_ERRNO__RELOC;
6205 }
6206 if (relo) {
6207 /* sub-program instruction index is a combination of
6208 * an offset of a symbol pointed to by relocation and
6209 * call instruction's imm field; for global functions,
6210 * call always has imm = -1, but for static functions
6211 * relocation is against STT_SECTION and insn->imm
6212 * points to a start of a static function
53eddb5e
YS
6213 *
6214 * for subprog addr relocation, the relo->sym_off + insn->imm is
6215 * the byte offset in the corresponding section.
c3c55696 6216 */
53eddb5e
YS
6217 if (relo->type == RELO_CALL)
6218 sub_insn_idx = relo->sym_off / BPF_INSN_SZ + insn->imm + 1;
6219 else
6220 sub_insn_idx = (relo->sym_off + insn->imm) / BPF_INSN_SZ;
6221 } else if (insn_is_pseudo_func(insn)) {
6222 /*
6223 * RELO_SUBPROG_ADDR relo is always emitted even if both
6224 * functions are in the same section, so it shouldn't reach here.
6225 */
6226 pr_warn("prog '%s': missing subprog addr relo for insn #%zu\n",
6227 prog->name, insn_idx);
6228 return -LIBBPF_ERRNO__RELOC;
c3c55696
AN
6229 } else {
6230 /* if subprogram call is to a static function within
6231 * the same ELF section, there won't be any relocation
6232 * emitted, but it also means there is no additional
6233 * offset necessary, insns->imm is relative to
6234 * instruction's original position within the section
6235 */
6236 sub_insn_idx = prog->sec_insn_off + insn_idx + insn->imm + 1;
6237 }
6238
6239 /* we enforce that sub-programs should be in .text section */
6240 subprog = find_prog_by_sec_insn(obj, obj->efile.text_shndx, sub_insn_idx);
6241 if (!subprog) {
6242 pr_warn("prog '%s': no .text section found yet sub-program call exists\n",
6243 prog->name);
6244 return -LIBBPF_ERRNO__RELOC;
6245 }
6246
6247 /* if it's the first call instruction calling into this
6248 * subprogram (meaning this subprog hasn't been processed
6249 * yet) within the context of current main program:
6250 * - append it at the end of main program's instructions blog;
6251 * - process is recursively, while current program is put on hold;
6252 * - if that subprogram calls some other not yet processes
6253 * subprogram, same thing will happen recursively until
6254 * there are no more unprocesses subprograms left to append
6255 * and relocate.
6256 */
6257 if (subprog->sub_insn_off == 0) {
6258 subprog->sub_insn_off = main_prog->insns_cnt;
6259
6260 new_cnt = main_prog->insns_cnt + subprog->insns_cnt;
6261 insns = libbpf_reallocarray(main_prog->insns, new_cnt, sizeof(*insns));
6262 if (!insns) {
6263 pr_warn("prog '%s': failed to realloc prog code\n", main_prog->name);
6264 return -ENOMEM;
6265 }
6266 main_prog->insns = insns;
6267 main_prog->insns_cnt = new_cnt;
6268
6269 memcpy(main_prog->insns + subprog->sub_insn_off, subprog->insns,
6270 subprog->insns_cnt * sizeof(*insns));
6271
6272 pr_debug("prog '%s': added %zu insns from sub-prog '%s'\n",
6273 main_prog->name, subprog->insns_cnt, subprog->name);
6274
b1268826
AS
6275 /* The subprog insns are now appended. Append its relos too. */
6276 err = append_subprog_relos(main_prog, subprog);
6277 if (err)
6278 return err;
c3c55696
AN
6279 err = bpf_object__reloc_code(obj, main_prog, subprog);
6280 if (err)
6281 return err;
6282 }
6283
6284 /* main_prog->insns memory could have been re-allocated, so
6285 * calculate pointer again
6286 */
6287 insn = &main_prog->insns[prog->sub_insn_off + insn_idx];
6288 /* calculate correct instruction position within current main
6289 * prog; each main prog can have a different set of
6290 * subprograms appended (potentially in different order as
6291 * well), so position of any subprog can be different for
e3ba8e4e
KM
6292 * different main programs
6293 */
c3c55696
AN
6294 insn->imm = subprog->sub_insn_off - (prog->sub_insn_off + insn_idx) - 1;
6295
c3c55696
AN
6296 pr_debug("prog '%s': insn #%zu relocated, imm %d points to subprog '%s' (now at %zu offset)\n",
6297 prog->name, insn_idx, insn->imm, subprog->name, subprog->sub_insn_off);
6298 }
6299
6300 return 0;
6301}
6302
6303/*
6304 * Relocate sub-program calls.
6305 *
6306 * Algorithm operates as follows. Each entry-point BPF program (referred to as
6307 * main prog) is processed separately. For each subprog (non-entry functions,
6308 * that can be called from either entry progs or other subprogs) gets their
6309 * sub_insn_off reset to zero. This serves as indicator that this subprogram
6310 * hasn't been yet appended and relocated within current main prog. Once its
6311 * relocated, sub_insn_off will point at the position within current main prog
6312 * where given subprog was appended. This will further be used to relocate all
6313 * the call instructions jumping into this subprog.
6314 *
6315 * We start with main program and process all call instructions. If the call
6316 * is into a subprog that hasn't been processed (i.e., subprog->sub_insn_off
6317 * is zero), subprog instructions are appended at the end of main program's
6318 * instruction array. Then main program is "put on hold" while we recursively
6319 * process newly appended subprogram. If that subprogram calls into another
6320 * subprogram that hasn't been appended, new subprogram is appended again to
6321 * the *main* prog's instructions (subprog's instructions are always left
6322 * untouched, as they need to be in unmodified state for subsequent main progs
6323 * and subprog instructions are always sent only as part of a main prog) and
6324 * the process continues recursively. Once all the subprogs called from a main
6325 * prog or any of its subprogs are appended (and relocated), all their
6326 * positions within finalized instructions array are known, so it's easy to
6327 * rewrite call instructions with correct relative offsets, corresponding to
6328 * desired target subprog.
6329 *
6330 * Its important to realize that some subprogs might not be called from some
6331 * main prog and any of its called/used subprogs. Those will keep their
6332 * subprog->sub_insn_off as zero at all times and won't be appended to current
6333 * main prog and won't be relocated within the context of current main prog.
6334 * They might still be used from other main progs later.
6335 *
6336 * Visually this process can be shown as below. Suppose we have two main
6337 * programs mainA and mainB and BPF object contains three subprogs: subA,
6338 * subB, and subC. mainA calls only subA, mainB calls only subC, but subA and
6339 * subC both call subB:
6340 *
6341 * +--------+ +-------+
6342 * | v v |
6343 * +--+---+ +--+-+-+ +---+--+
6344 * | subA | | subB | | subC |
6345 * +--+---+ +------+ +---+--+
6346 * ^ ^
6347 * | |
6348 * +---+-------+ +------+----+
6349 * | mainA | | mainB |
6350 * +-----------+ +-----------+
6351 *
6352 * We'll start relocating mainA, will find subA, append it and start
6353 * processing sub A recursively:
6354 *
6355 * +-----------+------+
6356 * | mainA | subA |
6357 * +-----------+------+
6358 *
6359 * At this point we notice that subB is used from subA, so we append it and
6360 * relocate (there are no further subcalls from subB):
6361 *
6362 * +-----------+------+------+
6363 * | mainA | subA | subB |
6364 * +-----------+------+------+
6365 *
6366 * At this point, we relocate subA calls, then go one level up and finish with
6367 * relocatin mainA calls. mainA is done.
6368 *
6369 * For mainB process is similar but results in different order. We start with
6370 * mainB and skip subA and subB, as mainB never calls them (at least
6371 * directly), but we see subC is needed, so we append and start processing it:
6372 *
6373 * +-----------+------+
6374 * | mainB | subC |
6375 * +-----------+------+
6376 * Now we see subC needs subB, so we go back to it, append and relocate it:
6377 *
6378 * +-----------+------+------+
6379 * | mainB | subC | subB |
6380 * +-----------+------+------+
6381 *
6382 * At this point we unwind recursion, relocate calls in subC, then in mainB.
6383 */
6384static int
6385bpf_object__relocate_calls(struct bpf_object *obj, struct bpf_program *prog)
6386{
6387 struct bpf_program *subprog;
d3d93e34 6388 int i, err;
c3c55696 6389
c3c55696
AN
6390 /* mark all subprogs as not relocated (yet) within the context of
6391 * current main program
6392 */
6393 for (i = 0; i < obj->nr_programs; i++) {
6394 subprog = &obj->programs[i];
6395 if (!prog_is_subprog(obj, subprog))
6396 continue;
6397
6398 subprog->sub_insn_off = 0;
c3c55696
AN
6399 }
6400
6401 err = bpf_object__reloc_code(obj, prog, prog);
6402 if (err)
6403 return err;
6404
8a47a6c5
WN
6405 return 0;
6406}
6407
67234743
AS
6408static void
6409bpf_object__free_relocs(struct bpf_object *obj)
6410{
6411 struct bpf_program *prog;
6412 int i;
6413
6414 /* free up relocation descriptors */
6415 for (i = 0; i < obj->nr_programs; i++) {
6416 prog = &obj->programs[i];
6417 zfree(&prog->reloc_desc);
6418 prog->nr_reloc = 0;
6419 }
6420}
6421
d0e92887
AS
6422static int cmp_relocs(const void *_a, const void *_b)
6423{
6424 const struct reloc_desc *a = _a;
6425 const struct reloc_desc *b = _b;
6426
6427 if (a->insn_idx != b->insn_idx)
6428 return a->insn_idx < b->insn_idx ? -1 : 1;
6429
6430 /* no two relocations should have the same insn_idx, but ... */
6431 if (a->type != b->type)
6432 return a->type < b->type ? -1 : 1;
6433
6434 return 0;
6435}
6436
6437static void bpf_object__sort_relos(struct bpf_object *obj)
6438{
6439 int i;
6440
6441 for (i = 0; i < obj->nr_programs; i++) {
6442 struct bpf_program *p = &obj->programs[i];
6443
6444 if (!p->nr_reloc)
6445 continue;
6446
6447 qsort(p->reloc_desc, p->nr_reloc, sizeof(*p->reloc_desc), cmp_relocs);
6448 }
6449}
6450
8a47a6c5 6451static int
ddc7c304 6452bpf_object__relocate(struct bpf_object *obj, const char *targ_btf_path)
8a47a6c5
WN
6453{
6454 struct bpf_program *prog;
b1268826 6455 size_t i, j;
8a47a6c5
WN
6456 int err;
6457
ddc7c304
AN
6458 if (obj->btf_ext) {
6459 err = bpf_object__relocate_core(obj, targ_btf_path);
6460 if (err) {
be18010e
KW
6461 pr_warn("failed to perform CO-RE relocations: %d\n",
6462 err);
ddc7c304
AN
6463 return err;
6464 }
185cfe83 6465 bpf_object__sort_relos(obj);
ddc7c304 6466 }
b1268826
AS
6467
6468 /* Before relocating calls pre-process relocations and mark
6469 * few ld_imm64 instructions that points to subprogs.
6470 * Otherwise bpf_object__reloc_code() later would have to consider
6471 * all ld_imm64 insns as relocation candidates. That would
6472 * reduce relocation speed, since amount of find_prog_insn_relo()
6473 * would increase and most of them will fail to find a relo.
9173cac3
AN
6474 */
6475 for (i = 0; i < obj->nr_programs; i++) {
6476 prog = &obj->programs[i];
b1268826
AS
6477 for (j = 0; j < prog->nr_reloc; j++) {
6478 struct reloc_desc *relo = &prog->reloc_desc[j];
6479 struct bpf_insn *insn = &prog->insns[relo->insn_idx];
6480
6481 /* mark the insn, so it's recognized by insn_is_pseudo_func() */
6482 if (relo->type == RELO_SUBPROG_ADDR)
6483 insn[0].src_reg = BPF_PSEUDO_FUNC;
9173cac3 6484 }
9173cac3 6485 }
b1268826
AS
6486
6487 /* relocate subprogram calls and append used subprograms to main
c3c55696
AN
6488 * programs; each copy of subprogram code needs to be relocated
6489 * differently for each main program, because its code location might
b1268826
AS
6490 * have changed.
6491 * Append subprog relos to main programs to allow data relos to be
6492 * processed after text is completely relocated.
9173cac3 6493 */
8a47a6c5
WN
6494 for (i = 0; i < obj->nr_programs; i++) {
6495 prog = &obj->programs[i];
c3c55696
AN
6496 /* sub-program's sub-calls are relocated within the context of
6497 * its main program only
6498 */
6499 if (prog_is_subprog(obj, prog))
9173cac3 6500 continue;
a3820c48 6501 if (!prog->autoload)
16e0c35c 6502 continue;
8a47a6c5 6503
c3c55696 6504 err = bpf_object__relocate_calls(obj, prog);
8a47a6c5 6505 if (err) {
9c0f8cbd
AN
6506 pr_warn("prog '%s': failed to relocate calls: %d\n",
6507 prog->name, err);
8a47a6c5
WN
6508 return err;
6509 }
6510 }
b1268826
AS
6511 /* Process data relos for main programs */
6512 for (i = 0; i < obj->nr_programs; i++) {
6513 prog = &obj->programs[i];
6514 if (prog_is_subprog(obj, prog))
6515 continue;
a3820c48 6516 if (!prog->autoload)
16e0c35c 6517 continue;
b1268826
AS
6518 err = bpf_object__relocate_data(obj, prog);
6519 if (err) {
6520 pr_warn("prog '%s': failed to relocate data references: %d\n",
6521 prog->name, err);
6522 return err;
6523 }
6524 }
185cfe83 6525
8a47a6c5
WN
6526 return 0;
6527}
6528
646f02ff 6529static int bpf_object__collect_st_ops_relos(struct bpf_object *obj,
ad23b723 6530 Elf64_Shdr *shdr, Elf_Data *data);
646f02ff
AN
6531
6532static int bpf_object__collect_map_relos(struct bpf_object *obj,
ad23b723 6533 Elf64_Shdr *shdr, Elf_Data *data)
646f02ff 6534{
15728ad3
AN
6535 const int bpf_ptr_sz = 8, host_ptr_sz = sizeof(void *);
6536 int i, j, nrels, new_sz;
063e6881 6537 const struct btf_var_secinfo *vi = NULL;
646f02ff 6538 const struct btf_type *sec, *var, *def;
341ac5ff
HC
6539 struct bpf_map *map = NULL, *targ_map = NULL;
6540 struct bpf_program *targ_prog = NULL;
6541 bool is_prog_array, is_map_in_map;
646f02ff 6542 const struct btf_member *member;
341ac5ff 6543 const char *name, *mname, *type;
646f02ff 6544 unsigned int moff;
ad23b723
AN
6545 Elf64_Sym *sym;
6546 Elf64_Rel *rel;
646f02ff
AN
6547 void *tmp;
6548
6549 if (!obj->efile.btf_maps_sec_btf_id || !obj->btf)
6550 return -EINVAL;
6551 sec = btf__type_by_id(obj->btf, obj->efile.btf_maps_sec_btf_id);
6552 if (!sec)
6553 return -EINVAL;
6554
646f02ff
AN
6555 nrels = shdr->sh_size / shdr->sh_entsize;
6556 for (i = 0; i < nrels; i++) {
ad23b723
AN
6557 rel = elf_rel_by_idx(data, i);
6558 if (!rel) {
646f02ff
AN
6559 pr_warn(".maps relo #%d: failed to get ELF relo\n", i);
6560 return -LIBBPF_ERRNO__FORMAT;
6561 }
ad23b723
AN
6562
6563 sym = elf_sym_by_idx(obj, ELF64_R_SYM(rel->r_info));
6564 if (!sym) {
646f02ff 6565 pr_warn(".maps relo #%d: symbol %zx not found\n",
ad23b723 6566 i, (size_t)ELF64_R_SYM(rel->r_info));
646f02ff
AN
6567 return -LIBBPF_ERRNO__FORMAT;
6568 }
ad23b723 6569 name = elf_sym_str(obj, sym->st_name) ?: "<?>";
646f02ff 6570
ad23b723
AN
6571 pr_debug(".maps relo #%d: for %zd value %zd rel->r_offset %zu name %d ('%s')\n",
6572 i, (ssize_t)(rel->r_info >> 32), (size_t)sym->st_value,
6573 (size_t)rel->r_offset, sym->st_name, name);
646f02ff
AN
6574
6575 for (j = 0; j < obj->nr_maps; j++) {
6576 map = &obj->maps[j];
6577 if (map->sec_idx != obj->efile.btf_maps_shndx)
6578 continue;
6579
6580 vi = btf_var_secinfos(sec) + map->btf_var_idx;
ad23b723
AN
6581 if (vi->offset <= rel->r_offset &&
6582 rel->r_offset + bpf_ptr_sz <= vi->offset + vi->size)
646f02ff
AN
6583 break;
6584 }
6585 if (j == obj->nr_maps) {
ad23b723
AN
6586 pr_warn(".maps relo #%d: cannot find map '%s' at rel->r_offset %zu\n",
6587 i, name, (size_t)rel->r_offset);
646f02ff
AN
6588 return -EINVAL;
6589 }
6590
341ac5ff
HC
6591 is_map_in_map = bpf_map_type__is_map_in_map(map->def.type);
6592 is_prog_array = map->def.type == BPF_MAP_TYPE_PROG_ARRAY;
6593 type = is_map_in_map ? "map" : "prog";
6594 if (is_map_in_map) {
6595 if (sym->st_shndx != obj->efile.btf_maps_shndx) {
6596 pr_warn(".maps relo #%d: '%s' isn't a BTF-defined map\n",
6597 i, name);
6598 return -LIBBPF_ERRNO__RELOC;
6599 }
6600 if (map->def.type == BPF_MAP_TYPE_HASH_OF_MAPS &&
6601 map->def.key_size != sizeof(int)) {
6602 pr_warn(".maps relo #%d: hash-of-maps '%s' should have key size %zu.\n",
6603 i, map->name, sizeof(int));
6604 return -EINVAL;
6605 }
6606 targ_map = bpf_object__find_map_by_name(obj, name);
6607 if (!targ_map) {
6608 pr_warn(".maps relo #%d: '%s' isn't a valid map reference\n",
6609 i, name);
6610 return -ESRCH;
6611 }
6612 } else if (is_prog_array) {
6613 targ_prog = bpf_object__find_program_by_name(obj, name);
6614 if (!targ_prog) {
6615 pr_warn(".maps relo #%d: '%s' isn't a valid program reference\n",
6616 i, name);
6617 return -ESRCH;
6618 }
6619 if (targ_prog->sec_idx != sym->st_shndx ||
6620 targ_prog->sec_insn_off * 8 != sym->st_value ||
6621 prog_is_subprog(obj, targ_prog)) {
6622 pr_warn(".maps relo #%d: '%s' isn't an entry-point program\n",
6623 i, name);
6624 return -LIBBPF_ERRNO__RELOC;
6625 }
6626 } else {
646f02ff
AN
6627 return -EINVAL;
6628 }
6629
646f02ff
AN
6630 var = btf__type_by_id(obj->btf, vi->type);
6631 def = skip_mods_and_typedefs(obj->btf, var->type, NULL);
6632 if (btf_vlen(def) == 0)
6633 return -EINVAL;
6634 member = btf_members(def) + btf_vlen(def) - 1;
6635 mname = btf__name_by_offset(obj->btf, member->name_off);
6636 if (strcmp(mname, "values"))
6637 return -EINVAL;
6638
6639 moff = btf_member_bit_offset(def, btf_vlen(def) - 1) / 8;
ad23b723 6640 if (rel->r_offset - vi->offset < moff)
646f02ff
AN
6641 return -EINVAL;
6642
ad23b723 6643 moff = rel->r_offset - vi->offset - moff;
15728ad3
AN
6644 /* here we use BPF pointer size, which is always 64 bit, as we
6645 * are parsing ELF that was built for BPF target
6646 */
6647 if (moff % bpf_ptr_sz)
646f02ff 6648 return -EINVAL;
15728ad3 6649 moff /= bpf_ptr_sz;
646f02ff
AN
6650 if (moff >= map->init_slots_sz) {
6651 new_sz = moff + 1;
029258d7 6652 tmp = libbpf_reallocarray(map->init_slots, new_sz, host_ptr_sz);
646f02ff
AN
6653 if (!tmp)
6654 return -ENOMEM;
6655 map->init_slots = tmp;
6656 memset(map->init_slots + map->init_slots_sz, 0,
15728ad3 6657 (new_sz - map->init_slots_sz) * host_ptr_sz);
646f02ff
AN
6658 map->init_slots_sz = new_sz;
6659 }
341ac5ff 6660 map->init_slots[moff] = is_map_in_map ? (void *)targ_map : (void *)targ_prog;
646f02ff 6661
341ac5ff
HC
6662 pr_debug(".maps relo #%d: map '%s' slot [%d] points to %s '%s'\n",
6663 i, map->name, moff, type, name);
646f02ff
AN
6664 }
6665
6666 return 0;
6667}
590a0088 6668
c3c55696
AN
6669static int bpf_object__collect_relos(struct bpf_object *obj)
6670{
6671 int i, err;
34090915 6672
25bbbd7a
AN
6673 for (i = 0; i < obj->efile.sec_cnt; i++) {
6674 struct elf_sec_desc *sec_desc = &obj->efile.secs[i];
6675 Elf64_Shdr *shdr;
6676 Elf_Data *data;
6677 int idx;
6678
6679 if (sec_desc->sec_type != SEC_RELO)
6680 continue;
6681
6682 shdr = sec_desc->shdr;
6683 data = sec_desc->data;
6684 idx = shdr->sh_info;
34090915
WN
6685
6686 if (shdr->sh_type != SHT_REL) {
be18010e 6687 pr_warn("internal error at %d\n", __LINE__);
6371ca3b 6688 return -LIBBPF_ERRNO__INTERNAL;
34090915
WN
6689 }
6690
809a69d6 6691 if (idx == obj->efile.st_ops_shndx || idx == obj->efile.st_ops_link_shndx)
646f02ff 6692 err = bpf_object__collect_st_ops_relos(obj, shdr, data);
c3c55696 6693 else if (idx == obj->efile.btf_maps_shndx)
646f02ff 6694 err = bpf_object__collect_map_relos(obj, shdr, data);
c3c55696
AN
6695 else
6696 err = bpf_object__collect_prog_relos(obj, shdr, data);
34090915 6697 if (err)
6371ca3b 6698 return err;
34090915 6699 }
c3c55696 6700
d0e92887 6701 bpf_object__sort_relos(obj);
34090915
WN
6702 return 0;
6703}
6704
109cea5a
AN
6705static bool insn_is_helper_call(struct bpf_insn *insn, enum bpf_func_id *func_id)
6706{
9b2f6fec 6707 if (BPF_CLASS(insn->code) == BPF_JMP &&
109cea5a
AN
6708 BPF_OP(insn->code) == BPF_CALL &&
6709 BPF_SRC(insn->code) == BPF_K &&
9b2f6fec
AN
6710 insn->src_reg == 0 &&
6711 insn->dst_reg == 0) {
6712 *func_id = insn->imm;
109cea5a
AN
6713 return true;
6714 }
6715 return false;
6716}
6717
42869d28 6718static int bpf_object__sanitize_prog(struct bpf_object *obj, struct bpf_program *prog)
109cea5a
AN
6719{
6720 struct bpf_insn *insn = prog->insns;
6721 enum bpf_func_id func_id;
6722 int i;
6723
67234743
AS
6724 if (obj->gen_loader)
6725 return 0;
6726
109cea5a
AN
6727 for (i = 0; i < prog->insns_cnt; i++, insn++) {
6728 if (!insn_is_helper_call(insn, &func_id))
6729 continue;
6730
6731 /* on kernels that don't yet support
6732 * bpf_probe_read_{kernel,user}[_str] helpers, fall back
6733 * to bpf_probe_read() which works well for old kernels
6734 */
6735 switch (func_id) {
6736 case BPF_FUNC_probe_read_kernel:
6737 case BPF_FUNC_probe_read_user:
9ca1f56a 6738 if (!kernel_supports(obj, FEAT_PROBE_READ_KERN))
109cea5a
AN
6739 insn->imm = BPF_FUNC_probe_read;
6740 break;
6741 case BPF_FUNC_probe_read_kernel_str:
6742 case BPF_FUNC_probe_read_user_str:
9ca1f56a 6743 if (!kernel_supports(obj, FEAT_PROBE_READ_KERN))
109cea5a
AN
6744 insn->imm = BPF_FUNC_probe_read_str;
6745 break;
6746 default:
6747 break;
6748 }
6749 }
6750 return 0;
6751}
6752
15ea31fa
AN
6753static int libbpf_find_attach_btf_id(struct bpf_program *prog, const char *attach_name,
6754 int *btf_obj_fd, int *btf_type_id);
12d9466d 6755
4fa5bcfe
AN
6756/* this is called as prog->sec_def->prog_prepare_load_fn for libbpf-supported sec_defs */
6757static int libbpf_prepare_prog_load(struct bpf_program *prog,
6758 struct bpf_prog_load_opts *opts, long cookie)
12d9466d 6759{
15ea31fa
AN
6760 enum sec_def_flags def = cookie;
6761
12d9466d 6762 /* old kernels might not support specifying expected_attach_type */
15ea31fa 6763 if ((def & SEC_EXP_ATTACH_OPT) && !kernel_supports(prog->obj, FEAT_EXP_ATTACH_TYPE))
d10ef2b8 6764 opts->expected_attach_type = 0;
12d9466d 6765
15ea31fa 6766 if (def & SEC_SLEEPABLE)
d10ef2b8 6767 opts->prog_flags |= BPF_F_SLEEPABLE;
12d9466d 6768
082c4bfb
LB
6769 if (prog->type == BPF_PROG_TYPE_XDP && (def & SEC_XDP_FRAGS))
6770 opts->prog_flags |= BPF_F_XDP_HAS_FRAGS;
6771
cc7d8f2c 6772 if ((def & SEC_ATTACH_BTF) && !prog->attach_btf_id) {
12d9466d 6773 int btf_obj_fd = 0, btf_type_id = 0, err;
15ea31fa 6774 const char *attach_name;
12d9466d 6775
cc7d8f2c
AN
6776 attach_name = strchr(prog->sec_name, '/');
6777 if (!attach_name) {
6778 /* if BPF program is annotated with just SEC("fentry")
6779 * (or similar) without declaratively specifying
6780 * target, then it is expected that target will be
6781 * specified with bpf_program__set_attach_target() at
6782 * runtime before BPF object load step. If not, then
6783 * there is nothing to load into the kernel as BPF
6784 * verifier won't be able to validate BPF program
6785 * correctness anyways.
6786 */
6787 pr_warn("prog '%s': no BTF-based attach target is specified, use bpf_program__set_attach_target()\n",
6788 prog->name);
6789 return -EINVAL;
6790 }
6791 attach_name++; /* skip over / */
6792
15ea31fa 6793 err = libbpf_find_attach_btf_id(prog, attach_name, &btf_obj_fd, &btf_type_id);
12d9466d
AN
6794 if (err)
6795 return err;
6796
6797 /* cache resolved BTF FD and BTF type ID in the prog */
6798 prog->attach_btf_obj_fd = btf_obj_fd;
6799 prog->attach_btf_id = btf_type_id;
6800
6801 /* but by now libbpf common logic is not utilizing
6802 * prog->atach_btf_obj_fd/prog->attach_btf_id anymore because
d10ef2b8
AN
6803 * this callback is called after opts were populated by
6804 * libbpf, so this callback has to update opts explicitly here
12d9466d 6805 */
d10ef2b8
AN
6806 opts->attach_btf_obj_fd = btf_obj_fd;
6807 opts->attach_btf_id = btf_type_id;
12d9466d
AN
6808 }
6809 return 0;
6810}
6811
9fdc4273
AN
6812static void fixup_verifier_log(struct bpf_program *prog, char *buf, size_t buf_sz);
6813
cf90a20d
AN
6814static int bpf_object_load_prog(struct bpf_object *obj, struct bpf_program *prog,
6815 struct bpf_insn *insns, int insns_cnt,
6816 const char *license, __u32 kern_version, int *prog_fd)
55cffde2 6817{
d10ef2b8
AN
6818 LIBBPF_OPTS(bpf_prog_load_opts, load_attr);
6819 const char *prog_name = NULL;
1ce6a9fc 6820 char *cp, errmsg[STRERR_BUFSIZE];
8395f320 6821 size_t log_buf_size = 0;
b3ce9079 6822 char *log_buf = NULL, *tmp;
12d9466d 6823 int btf_fd, ret, err;
b3ce9079
AN
6824 bool own_log_buf = true;
6825 __u32 log_level = prog->log_level;
55cffde2 6826
80b2b5c3
AM
6827 if (prog->type == BPF_PROG_TYPE_UNSPEC) {
6828 /*
6829 * The program type must be set. Most likely we couldn't find a proper
6830 * section definition at load time, and thus we didn't infer the type.
6831 */
6832 pr_warn("prog '%s': missing BPF prog type, check ELF section name '%s'\n",
6833 prog->name, prog->sec_name);
6834 return -EINVAL;
6835 }
6836
fba01a06
AN
6837 if (!insns || !insns_cnt)
6838 return -EINVAL;
6839
12d9466d 6840 load_attr.expected_attach_type = prog->expected_attach_type;
25bbbd7a 6841 if (kernel_supports(obj, FEAT_PROG_NAME))
d10ef2b8 6842 prog_name = prog->name;
12d9466d
AN
6843 load_attr.attach_prog_fd = prog->attach_prog_fd;
6844 load_attr.attach_btf_obj_fd = prog->attach_btf_obj_fd;
6aef10a4
AN
6845 load_attr.attach_btf_id = prog->attach_btf_id;
6846 load_attr.kern_version = kern_version;
6847 load_attr.prog_ifindex = prog->prog_ifindex;
6848
0f0e55d8 6849 /* specify func_info/line_info only if kernel supports them */
25bbbd7a
AN
6850 btf_fd = bpf_object__btf_fd(obj);
6851 if (btf_fd >= 0 && kernel_supports(obj, FEAT_BTF_FUNC)) {
0f0e55d8
AN
6852 load_attr.prog_btf_fd = btf_fd;
6853 load_attr.func_info = prog->func_info;
6854 load_attr.func_info_rec_size = prog->func_info_rec_size;
6855 load_attr.func_info_cnt = prog->func_info_cnt;
6856 load_attr.line_info = prog->line_info;
6857 load_attr.line_info_rec_size = prog->line_info_rec_size;
6858 load_attr.line_info_cnt = prog->line_info_cnt;
6859 }
b3ce9079 6860 load_attr.log_level = log_level;
04656198 6861 load_attr.prog_flags = prog->prog_flags;
25bbbd7a 6862 load_attr.fd_array = obj->fd_array;
55cffde2 6863
12d9466d 6864 /* adjust load_attr if sec_def provides custom preload callback */
4fa5bcfe
AN
6865 if (prog->sec_def && prog->sec_def->prog_prepare_load_fn) {
6866 err = prog->sec_def->prog_prepare_load_fn(prog, &load_attr, prog->sec_def->cookie);
12d9466d
AN
6867 if (err < 0) {
6868 pr_warn("prog '%s': failed to prepare load attributes: %d\n",
6869 prog->name, err);
6870 return err;
6871 }
b63b3c49
JO
6872 insns = prog->insns;
6873 insns_cnt = prog->insns_cnt;
12d9466d
AN
6874 }
6875
25bbbd7a 6876 if (obj->gen_loader) {
d10ef2b8
AN
6877 bpf_gen__prog_load(obj->gen_loader, prog->type, prog->name,
6878 license, insns, insns_cnt, &load_attr,
25bbbd7a 6879 prog - obj->programs);
be2f2d16 6880 *prog_fd = -1;
67234743
AS
6881 return 0;
6882 }
8395f320 6883
b3ce9079 6884retry_load:
bb412cf1 6885 /* if log_level is zero, we don't request logs initially even if
b3ce9079
AN
6886 * custom log_buf is specified; if the program load fails, then we'll
6887 * bump log_level to 1 and use either custom log_buf or we'll allocate
6888 * our own and retry the load to get details on what failed
6889 */
6890 if (log_level) {
6891 if (prog->log_buf) {
6892 log_buf = prog->log_buf;
6893 log_buf_size = prog->log_size;
6894 own_log_buf = false;
6895 } else if (obj->log_buf) {
6896 log_buf = obj->log_buf;
6897 log_buf_size = obj->log_size;
6898 own_log_buf = false;
6899 } else {
6900 log_buf_size = max((size_t)BPF_LOG_BUF_SIZE, log_buf_size * 2);
6901 tmp = realloc(log_buf, log_buf_size);
6902 if (!tmp) {
6903 ret = -ENOMEM;
6904 goto out;
6905 }
6906 log_buf = tmp;
6907 log_buf[0] = '\0';
6908 own_log_buf = true;
6909 }
8395f320 6910 }
55cffde2 6911
6aef10a4 6912 load_attr.log_buf = log_buf;
d10ef2b8 6913 load_attr.log_size = log_buf_size;
b3ce9079 6914 load_attr.log_level = log_level;
55cffde2 6915
b3ce9079 6916 ret = bpf_prog_load(prog->type, prog_name, license, insns, insns_cnt, &load_attr);
55cffde2 6917 if (ret >= 0) {
b3ce9079 6918 if (log_level && own_log_buf) {
ad9a7f96
AN
6919 pr_debug("prog '%s': -- BEGIN PROG LOAD LOG --\n%s-- END PROG LOAD LOG --\n",
6920 prog->name, log_buf);
6921 }
5d23328d 6922
25bbbd7a
AN
6923 if (obj->has_rodata && kernel_supports(obj, FEAT_PROG_BIND_MAP)) {
6924 struct bpf_map *map;
6925 int i;
6926
6927 for (i = 0; i < obj->nr_maps; i++) {
6928 map = &prog->obj->maps[i];
6929 if (map->libbpf_type != LIBBPF_MAP_RODATA)
6930 continue;
5d23328d 6931
25bbbd7a
AN
6932 if (bpf_prog_bind_map(ret, bpf_map__fd(map), NULL)) {
6933 cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
ad9a7f96
AN
6934 pr_warn("prog '%s': failed to bind map '%s': %s\n",
6935 prog->name, map->real_name, cp);
25bbbd7a
AN
6936 /* Don't fail hard if can't bind rodata. */
6937 }
5d23328d
YZ
6938 }
6939 }
6940
be2f2d16 6941 *prog_fd = ret;
55cffde2
WN
6942 ret = 0;
6943 goto out;
6944 }
6945
b3ce9079
AN
6946 if (log_level == 0) {
6947 log_level = 1;
da11b417
AS
6948 goto retry_load;
6949 }
b3ce9079
AN
6950 /* On ENOSPC, increase log buffer size and retry, unless custom
6951 * log_buf is specified.
6952 * Be careful to not overflow u32, though. Kernel's log buf size limit
6953 * isn't part of UAPI so it can always be bumped to full 4GB. So don't
6954 * multiply by 2 unless we are sure we'll fit within 32 bits.
6955 * Currently, we'll get -EINVAL when we reach (UINT_MAX >> 2).
6956 */
6957 if (own_log_buf && errno == ENOSPC && log_buf_size <= UINT_MAX / 2)
6958 goto retry_load;
2eda2145
AN
6959
6960 ret = -errno;
9fdc4273
AN
6961
6962 /* post-process verifier log to improve error descriptions */
6963 fixup_verifier_log(prog, log_buf, log_buf_size);
6964
24d6a808 6965 cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
ad9a7f96 6966 pr_warn("prog '%s': BPF program load failed: %s\n", prog->name, cp);
dc3a2d25 6967 pr_perm_msg(ret);
55cffde2 6968
b3ce9079 6969 if (own_log_buf && log_buf && log_buf[0] != '\0') {
ad9a7f96
AN
6970 pr_warn("prog '%s': -- BEGIN PROG LOAD LOG --\n%s-- END PROG LOAD LOG --\n",
6971 prog->name, log_buf);
6972 }
55cffde2
WN
6973
6974out:
b3ce9079
AN
6975 if (own_log_buf)
6976 free(log_buf);
55cffde2
WN
6977 return ret;
6978}
6979
9fdc4273
AN
6980static char *find_prev_line(char *buf, char *cur)
6981{
6982 char *p;
6983
6984 if (cur == buf) /* end of a log buf */
6985 return NULL;
6986
6987 p = cur - 1;
6988 while (p - 1 >= buf && *(p - 1) != '\n')
6989 p--;
6990
6991 return p;
6992}
6993
6994static void patch_log(char *buf, size_t buf_sz, size_t log_sz,
6995 char *orig, size_t orig_sz, const char *patch)
6996{
6997 /* size of the remaining log content to the right from the to-be-replaced part */
6998 size_t rem_sz = (buf + log_sz) - (orig + orig_sz);
6999 size_t patch_sz = strlen(patch);
7000
7001 if (patch_sz != orig_sz) {
7002 /* If patch line(s) are longer than original piece of verifier log,
7003 * shift log contents by (patch_sz - orig_sz) bytes to the right
7004 * starting from after to-be-replaced part of the log.
7005 *
7006 * If patch line(s) are shorter than original piece of verifier log,
7007 * shift log contents by (orig_sz - patch_sz) bytes to the left
7008 * starting from after to-be-replaced part of the log
7009 *
7010 * We need to be careful about not overflowing available
7011 * buf_sz capacity. If that's the case, we'll truncate the end
7012 * of the original log, as necessary.
7013 */
7014 if (patch_sz > orig_sz) {
7015 if (orig + patch_sz >= buf + buf_sz) {
7016 /* patch is big enough to cover remaining space completely */
7017 patch_sz -= (orig + patch_sz) - (buf + buf_sz) + 1;
7018 rem_sz = 0;
7019 } else if (patch_sz - orig_sz > buf_sz - log_sz) {
7020 /* patch causes part of remaining log to be truncated */
7021 rem_sz -= (patch_sz - orig_sz) - (buf_sz - log_sz);
7022 }
7023 }
7024 /* shift remaining log to the right by calculated amount */
7025 memmove(orig + patch_sz, orig + orig_sz, rem_sz);
7026 }
7027
7028 memcpy(orig, patch, patch_sz);
7029}
7030
7031static void fixup_log_failed_core_relo(struct bpf_program *prog,
7032 char *buf, size_t buf_sz, size_t log_sz,
7033 char *line1, char *line2, char *line3)
7034{
7035 /* Expected log for failed and not properly guarded CO-RE relocation:
7036 * line1 -> 123: (85) call unknown#195896080
7037 * line2 -> invalid func unknown#195896080
7038 * line3 -> <anything else or end of buffer>
7039 *
7040 * "123" is the index of the instruction that was poisoned. We extract
7041 * instruction index to find corresponding CO-RE relocation and
7042 * replace this part of the log with more relevant information about
7043 * failed CO-RE relocation.
7044 */
7045 const struct bpf_core_relo *relo;
7046 struct bpf_core_spec spec;
7047 char patch[512], spec_buf[256];
b198881d 7048 int insn_idx, err, spec_len;
9fdc4273
AN
7049
7050 if (sscanf(line1, "%d: (%*d) call unknown#195896080\n", &insn_idx) != 1)
7051 return;
7052
7053 relo = find_relo_core(prog, insn_idx);
7054 if (!relo)
7055 return;
7056
7057 err = bpf_core_parse_spec(prog->name, prog->obj->btf, relo, &spec);
7058 if (err)
7059 return;
7060
b198881d 7061 spec_len = bpf_core_format_spec(spec_buf, sizeof(spec_buf), &spec);
9fdc4273
AN
7062 snprintf(patch, sizeof(patch),
7063 "%d: <invalid CO-RE relocation>\n"
b198881d
AN
7064 "failed to resolve CO-RE relocation %s%s\n",
7065 insn_idx, spec_buf, spec_len >= sizeof(spec_buf) ? "..." : "");
9fdc4273
AN
7066
7067 patch_log(buf, buf_sz, log_sz, line1, line3 - line1, patch);
7068}
7069
ec41817b
AN
7070static void fixup_log_missing_map_load(struct bpf_program *prog,
7071 char *buf, size_t buf_sz, size_t log_sz,
7072 char *line1, char *line2, char *line3)
7073{
3055ddd6 7074 /* Expected log for failed and not properly guarded map reference:
ec41817b
AN
7075 * line1 -> 123: (85) call unknown#2001000345
7076 * line2 -> invalid func unknown#2001000345
7077 * line3 -> <anything else or end of buffer>
7078 *
7079 * "123" is the index of the instruction that was poisoned.
3055ddd6 7080 * "345" in "2001000345" is a map index in obj->maps to fetch map name.
ec41817b
AN
7081 */
7082 struct bpf_object *obj = prog->obj;
7083 const struct bpf_map *map;
7084 int insn_idx, map_idx;
7085 char patch[128];
7086
7087 if (sscanf(line1, "%d: (%*d) call unknown#%d\n", &insn_idx, &map_idx) != 2)
7088 return;
7089
3055ddd6 7090 map_idx -= POISON_LDIMM64_MAP_BASE;
ec41817b
AN
7091 if (map_idx < 0 || map_idx >= obj->nr_maps)
7092 return;
7093 map = &obj->maps[map_idx];
7094
7095 snprintf(patch, sizeof(patch),
7096 "%d: <invalid BPF map reference>\n"
7097 "BPF map '%s' is referenced but wasn't created\n",
7098 insn_idx, map->name);
7099
7100 patch_log(buf, buf_sz, log_sz, line1, line3 - line1, patch);
7101}
7102
05b6f766
AN
7103static void fixup_log_missing_kfunc_call(struct bpf_program *prog,
7104 char *buf, size_t buf_sz, size_t log_sz,
7105 char *line1, char *line2, char *line3)
7106{
7107 /* Expected log for failed and not properly guarded kfunc call:
7108 * line1 -> 123: (85) call unknown#2002000345
7109 * line2 -> invalid func unknown#2002000345
7110 * line3 -> <anything else or end of buffer>
7111 *
7112 * "123" is the index of the instruction that was poisoned.
7113 * "345" in "2002000345" is an extern index in obj->externs to fetch kfunc name.
7114 */
7115 struct bpf_object *obj = prog->obj;
7116 const struct extern_desc *ext;
7117 int insn_idx, ext_idx;
7118 char patch[128];
7119
7120 if (sscanf(line1, "%d: (%*d) call unknown#%d\n", &insn_idx, &ext_idx) != 2)
7121 return;
7122
7123 ext_idx -= POISON_CALL_KFUNC_BASE;
7124 if (ext_idx < 0 || ext_idx >= obj->nr_extern)
7125 return;
7126 ext = &obj->externs[ext_idx];
7127
7128 snprintf(patch, sizeof(patch),
7129 "%d: <invalid kfunc call>\n"
7130 "kfunc '%s' is referenced but wasn't resolved\n",
7131 insn_idx, ext->name);
7132
7133 patch_log(buf, buf_sz, log_sz, line1, line3 - line1, patch);
7134}
7135
9fdc4273
AN
7136static void fixup_verifier_log(struct bpf_program *prog, char *buf, size_t buf_sz)
7137{
7138 /* look for familiar error patterns in last N lines of the log */
7139 const size_t max_last_line_cnt = 10;
7140 char *prev_line, *cur_line, *next_line;
7141 size_t log_sz;
7142 int i;
7143
7144 if (!buf)
7145 return;
7146
7147 log_sz = strlen(buf) + 1;
7148 next_line = buf + log_sz - 1;
7149
7150 for (i = 0; i < max_last_line_cnt; i++, next_line = cur_line) {
7151 cur_line = find_prev_line(buf, next_line);
7152 if (!cur_line)
7153 return;
7154
9fdc4273
AN
7155 if (str_has_pfx(cur_line, "invalid func unknown#195896080\n")) {
7156 prev_line = find_prev_line(buf, cur_line);
7157 if (!prev_line)
7158 continue;
7159
3055ddd6 7160 /* failed CO-RE relocation case */
9fdc4273
AN
7161 fixup_log_failed_core_relo(prog, buf, buf_sz, log_sz,
7162 prev_line, cur_line, next_line);
7163 return;
3055ddd6 7164 } else if (str_has_pfx(cur_line, "invalid func unknown#"POISON_LDIMM64_MAP_PFX)) {
ec41817b
AN
7165 prev_line = find_prev_line(buf, cur_line);
7166 if (!prev_line)
7167 continue;
7168
3055ddd6 7169 /* reference to uncreated BPF map */
ec41817b
AN
7170 fixup_log_missing_map_load(prog, buf, buf_sz, log_sz,
7171 prev_line, cur_line, next_line);
7172 return;
05b6f766
AN
7173 } else if (str_has_pfx(cur_line, "invalid func unknown#"POISON_CALL_KFUNC_PFX)) {
7174 prev_line = find_prev_line(buf, cur_line);
7175 if (!prev_line)
7176 continue;
7177
7178 /* reference to unresolved kfunc */
7179 fixup_log_missing_kfunc_call(prog, buf, buf_sz, log_sz,
7180 prev_line, cur_line, next_line);
7181 return;
9fdc4273
AN
7182 }
7183 }
7184}
7185
d0e92887 7186static int bpf_program_record_relos(struct bpf_program *prog)
67234743
AS
7187{
7188 struct bpf_object *obj = prog->obj;
7189 int i;
7190
7191 for (i = 0; i < prog->nr_reloc; i++) {
7192 struct reloc_desc *relo = &prog->reloc_desc[i];
3055ddd6 7193 struct extern_desc *ext = &obj->externs[relo->ext_idx];
708cdc57 7194 int kind;
67234743
AS
7195
7196 switch (relo->type) {
a18f7214 7197 case RELO_EXTERN_LD64:
67234743
AS
7198 if (ext->type != EXT_KSYM)
7199 continue;
708cdc57
AS
7200 kind = btf_is_var(btf__type_by_id(obj->btf, ext->btf_id)) ?
7201 BTF_KIND_VAR : BTF_KIND_FUNC;
c24941cd
KKD
7202 bpf_gen__record_extern(obj->gen_loader, ext->name,
7203 ext->is_weak, !ext->ksym.type_id,
708cdc57 7204 true, kind, relo->insn_idx);
67234743 7205 break;
a18f7214 7206 case RELO_EXTERN_CALL:
c24941cd 7207 bpf_gen__record_extern(obj->gen_loader, ext->name,
708cdc57 7208 ext->is_weak, false, false, BTF_KIND_FUNC,
67234743
AS
7209 relo->insn_idx);
7210 break;
d0e92887
AS
7211 case RELO_CORE: {
7212 struct bpf_core_relo cr = {
7213 .insn_off = relo->insn_idx * 8,
7214 .type_id = relo->core_relo->type_id,
7215 .access_str_off = relo->core_relo->access_str_off,
7216 .kind = relo->core_relo->kind,
7217 };
7218
7219 bpf_gen__record_relo_core(obj->gen_loader, &cr);
7220 break;
7221 }
67234743
AS
7222 default:
7223 continue;
7224 }
7225 }
7226 return 0;
7227}
7228
55cffde2 7229static int
60276f98 7230bpf_object__load_progs(struct bpf_object *obj, int log_level)
55cffde2 7231{
d9297581 7232 struct bpf_program *prog;
55cffde2
WN
7233 size_t i;
7234 int err;
7235
109cea5a
AN
7236 for (i = 0; i < obj->nr_programs; i++) {
7237 prog = &obj->programs[i];
7238 err = bpf_object__sanitize_prog(obj, prog);
7239 if (err)
7240 return err;
7241 }
7242
55cffde2 7243 for (i = 0; i < obj->nr_programs; i++) {
d9297581 7244 prog = &obj->programs[i];
c3c55696 7245 if (prog_is_subprog(obj, prog))
48cca7e4 7246 continue;
a3820c48 7247 if (!prog->autoload) {
9c0f8cbd 7248 pr_debug("prog '%s': skipped loading\n", prog->name);
d9297581
AN
7249 continue;
7250 }
7251 prog->log_level |= log_level;
cf90a20d
AN
7252
7253 if (obj->gen_loader)
7254 bpf_program_record_relos(prog);
7255
7256 err = bpf_object_load_prog(obj, prog, prog->insns, prog->insns_cnt,
7257 obj->license, obj->kern_version, &prog->fd);
7258 if (err) {
7259 pr_warn("prog '%s': failed to load: %d\n", prog->name, err);
55cffde2 7260 return err;
cf90a20d 7261 }
55cffde2 7262 }
185cfe83
AN
7263
7264 bpf_object__free_relocs(obj);
55cffde2
WN
7265 return 0;
7266}
7267
25498a19
AN
7268static const struct bpf_sec_def *find_sec_def(const char *sec_name);
7269
91b4d1d1
AN
7270static int bpf_object_init_progs(struct bpf_object *obj, const struct bpf_object_open_opts *opts)
7271{
7272 struct bpf_program *prog;
12d9466d 7273 int err;
91b4d1d1
AN
7274
7275 bpf_object__for_each_program(prog, obj) {
7276 prog->sec_def = find_sec_def(prog->sec_name);
7277 if (!prog->sec_def) {
7278 /* couldn't guess, but user might manually specify */
7279 pr_debug("prog '%s': unrecognized ELF section name '%s'\n",
7280 prog->name, prog->sec_name);
7281 continue;
7282 }
7283
df286716
GS
7284 prog->type = prog->sec_def->prog_type;
7285 prog->expected_attach_type = prog->sec_def->expected_attach_type;
91b4d1d1 7286
12d9466d
AN
7287 /* sec_def can have custom callback which should be called
7288 * after bpf_program is initialized to adjust its properties
7289 */
4fa5bcfe
AN
7290 if (prog->sec_def->prog_setup_fn) {
7291 err = prog->sec_def->prog_setup_fn(prog, prog->sec_def->cookie);
12d9466d
AN
7292 if (err < 0) {
7293 pr_warn("prog '%s': failed to initialize: %d\n",
7294 prog->name, err);
7295 return err;
7296 }
7297 }
91b4d1d1
AN
7298 }
7299
7300 return 0;
7301}
7302
ad9a7f96
AN
7303static struct bpf_object *bpf_object_open(const char *path, const void *obj_buf, size_t obj_buf_sz,
7304 const struct bpf_object_open_opts *opts)
1a5e3fb1 7305{
1373ff59 7306 const char *obj_name, *kconfig, *btf_tmp_path;
1a5e3fb1 7307 struct bpf_object *obj;
291ee02b 7308 char tmp_name[64];
6371ca3b 7309 int err;
e0e3ea88
AN
7310 char *log_buf;
7311 size_t log_size;
7312 __u32 log_level;
1a5e3fb1
WN
7313
7314 if (elf_version(EV_CURRENT) == EV_NONE) {
be18010e
KW
7315 pr_warn("failed to init libelf for %s\n",
7316 path ? : "(mem buf)");
6371ca3b 7317 return ERR_PTR(-LIBBPF_ERRNO__LIBELF);
1a5e3fb1
WN
7318 }
7319
291ee02b
AN
7320 if (!OPTS_VALID(opts, bpf_object_open_opts))
7321 return ERR_PTR(-EINVAL);
7322
1aace10f 7323 obj_name = OPTS_GET(opts, object_name, NULL);
291ee02b
AN
7324 if (obj_buf) {
7325 if (!obj_name) {
7326 snprintf(tmp_name, sizeof(tmp_name), "%lx-%lx",
7327 (unsigned long)obj_buf,
7328 (unsigned long)obj_buf_sz);
7329 obj_name = tmp_name;
7330 }
7331 path = obj_name;
7332 pr_debug("loading object '%s' from buffer\n", obj_name);
7333 }
7334
e0e3ea88
AN
7335 log_buf = OPTS_GET(opts, kernel_log_buf, NULL);
7336 log_size = OPTS_GET(opts, kernel_log_size, 0);
7337 log_level = OPTS_GET(opts, kernel_log_level, 0);
7338 if (log_size > UINT_MAX)
7339 return ERR_PTR(-EINVAL);
7340 if (log_size && !log_buf)
7341 return ERR_PTR(-EINVAL);
7342
2ce8450e 7343 obj = bpf_object__new(path, obj_buf, obj_buf_sz, obj_name);
6371ca3b
WN
7344 if (IS_ERR(obj))
7345 return obj;
1a5e3fb1 7346
e0e3ea88
AN
7347 obj->log_buf = log_buf;
7348 obj->log_size = log_size;
7349 obj->log_level = log_level;
7350
1373ff59
SC
7351 btf_tmp_path = OPTS_GET(opts, btf_custom_path, NULL);
7352 if (btf_tmp_path) {
7353 if (strlen(btf_tmp_path) >= PATH_MAX) {
7354 err = -ENAMETOOLONG;
7355 goto out;
7356 }
7357 obj->btf_custom_path = strdup(btf_tmp_path);
7358 if (!obj->btf_custom_path) {
7359 err = -ENOMEM;
7360 goto out;
7361 }
7362 }
7363
8601fd42
AN
7364 kconfig = OPTS_GET(opts, kconfig, NULL);
7365 if (kconfig) {
7366 obj->kconfig = strdup(kconfig);
18353c87
SC
7367 if (!obj->kconfig) {
7368 err = -ENOMEM;
7369 goto out;
7370 }
166750bc 7371 }
291ee02b 7372
0d13bfce
AN
7373 err = bpf_object__elf_init(obj);
7374 err = err ? : bpf_object__check_endianness(obj);
7375 err = err ? : bpf_object__elf_collect(obj);
166750bc 7376 err = err ? : bpf_object__collect_externs(obj);
f33f742d 7377 err = err ? : bpf_object_fixup_btf(obj);
0d13bfce 7378 err = err ? : bpf_object__init_maps(obj, opts);
91b4d1d1 7379 err = err ? : bpf_object_init_progs(obj, opts);
c3c55696 7380 err = err ? : bpf_object__collect_relos(obj);
0d13bfce
AN
7381 if (err)
7382 goto out;
dd4436bb 7383
91b4d1d1 7384 bpf_object__elf_finish(obj);
dd4436bb 7385
1a5e3fb1
WN
7386 return obj;
7387out:
7388 bpf_object__close(obj);
6371ca3b 7389 return ERR_PTR(err);
1a5e3fb1
WN
7390}
7391
2ce8450e 7392struct bpf_object *
01af3bf0 7393bpf_object__open_file(const char *path, const struct bpf_object_open_opts *opts)
2ce8450e 7394{
2ce8450e 7395 if (!path)
e9fc3ce9 7396 return libbpf_err_ptr(-EINVAL);
2ce8450e
AN
7397
7398 pr_debug("loading %s\n", path);
7399
ad9a7f96 7400 return libbpf_ptr(bpf_object_open(path, NULL, 0, opts));
2ce8450e
AN
7401}
7402
146bf811
AN
7403struct bpf_object *bpf_object__open(const char *path)
7404{
7405 return bpf_object__open_file(path, NULL);
7406}
7407
2ce8450e
AN
7408struct bpf_object *
7409bpf_object__open_mem(const void *obj_buf, size_t obj_buf_sz,
01af3bf0 7410 const struct bpf_object_open_opts *opts)
6c956392 7411{
2ce8450e 7412 if (!obj_buf || obj_buf_sz == 0)
e9fc3ce9 7413 return libbpf_err_ptr(-EINVAL);
6c956392 7414
ad9a7f96 7415 return libbpf_ptr(bpf_object_open(NULL, obj_buf, obj_buf_sz, opts));
2ce8450e
AN
7416}
7417
4a404a7e 7418static int bpf_object_unload(struct bpf_object *obj)
52d3352e
WN
7419{
7420 size_t i;
7421
7422 if (!obj)
e9fc3ce9 7423 return libbpf_err(-EINVAL);
52d3352e 7424
590a0088 7425 for (i = 0; i < obj->nr_maps; i++) {
9d759a9b 7426 zclose(obj->maps[i].fd);
590a0088
MKL
7427 if (obj->maps[i].st_ops)
7428 zfree(&obj->maps[i].st_ops->kern_vdata);
7429 }
52d3352e 7430
55cffde2
WN
7431 for (i = 0; i < obj->nr_programs; i++)
7432 bpf_program__unload(&obj->programs[i]);
7433
52d3352e
WN
7434 return 0;
7435}
7436
0d13bfce
AN
7437static int bpf_object__sanitize_maps(struct bpf_object *obj)
7438{
7439 struct bpf_map *m;
7440
7441 bpf_object__for_each_map(m, obj) {
7442 if (!bpf_map__is_internal(m))
7443 continue;
9ca1f56a 7444 if (!kernel_supports(obj, FEAT_ARRAY_MMAP))
6920b086 7445 m->def.map_flags &= ~BPF_F_MMAPABLE;
0d13bfce
AN
7446 }
7447
7448 return 0;
7449}
7450
85153ac0 7451int libbpf_kallsyms_parse(kallsyms_cb_t cb, void *ctx)
1c0c7074
AN
7452{
7453 char sym_type, sym_name[500];
7454 unsigned long long sym_addr;
1c0c7074
AN
7455 int ret, err = 0;
7456 FILE *f;
7457
59842c54 7458 f = fopen("/proc/kallsyms", "re");
1c0c7074
AN
7459 if (!f) {
7460 err = -errno;
7461 pr_warn("failed to open /proc/kallsyms: %d\n", err);
7462 return err;
7463 }
7464
7465 while (true) {
7466 ret = fscanf(f, "%llx %c %499s%*[^\n]\n",
7467 &sym_addr, &sym_type, sym_name);
7468 if (ret == EOF && feof(f))
7469 break;
7470 if (ret != 3) {
135c783f 7471 pr_warn("failed to read kallsyms entry: %d\n", ret);
1c0c7074 7472 err = -EINVAL;
85153ac0 7473 break;
1c0c7074
AN
7474 }
7475
85153ac0
JO
7476 err = cb(sym_addr, sym_type, sym_name, ctx);
7477 if (err)
7478 break;
1c0c7074
AN
7479 }
7480
1c0c7074
AN
7481 fclose(f);
7482 return err;
7483}
7484
85153ac0
JO
7485static int kallsyms_cb(unsigned long long sym_addr, char sym_type,
7486 const char *sym_name, void *ctx)
7487{
7488 struct bpf_object *obj = ctx;
7489 const struct btf_type *t;
7490 struct extern_desc *ext;
7491
7492 ext = find_extern_by_name(obj, sym_name);
7493 if (!ext || ext->type != EXT_KSYM)
7494 return 0;
7495
7496 t = btf__type_by_id(obj->btf, ext->btf_id);
7497 if (!btf_is_var(t))
7498 return 0;
7499
7500 if (ext->is_set && ext->ksym.addr != sym_addr) {
55d00c37 7501 pr_warn("extern (ksym) '%s': resolution is ambiguous: 0x%llx or 0x%llx\n",
85153ac0
JO
7502 sym_name, ext->ksym.addr, sym_addr);
7503 return -EINVAL;
7504 }
7505 if (!ext->is_set) {
7506 ext->is_set = true;
7507 ext->ksym.addr = sym_addr;
55d00c37 7508 pr_debug("extern (ksym) '%s': set to 0x%llx\n", sym_name, sym_addr);
85153ac0
JO
7509 }
7510 return 0;
7511}
7512
7513static int bpf_object__read_kallsyms_file(struct bpf_object *obj)
7514{
7515 return libbpf_kallsyms_parse(kallsyms_cb, obj);
7516}
7517
774e132e
MKL
7518static int find_ksym_btf_id(struct bpf_object *obj, const char *ksym_name,
7519 __u16 kind, struct btf **res_btf,
9dbe6015 7520 struct module_btf **res_mod_btf)
d370bbe1 7521{
9dbe6015 7522 struct module_btf *mod_btf;
284d2587 7523 struct btf *btf;
9dbe6015 7524 int i, id, err;
d370bbe1 7525
933d1aa3 7526 btf = obj->btf_vmlinux;
9dbe6015 7527 mod_btf = NULL;
774e132e
MKL
7528 id = btf__find_by_name_kind(btf, ksym_name, kind);
7529
933d1aa3
MKL
7530 if (id == -ENOENT) {
7531 err = load_module_btfs(obj);
7532 if (err)
7533 return err;
d370bbe1 7534
933d1aa3 7535 for (i = 0; i < obj->btf_module_cnt; i++) {
9dbe6015
KKD
7536 /* we assume module_btf's BTF FD is always >0 */
7537 mod_btf = &obj->btf_modules[i];
7538 btf = mod_btf->btf;
7539 id = btf__find_by_name_kind_own(btf, ksym_name, kind);
933d1aa3
MKL
7540 if (id != -ENOENT)
7541 break;
7542 }
7543 }
2211c825 7544 if (id <= 0)
933d1aa3 7545 return -ESRCH;
d370bbe1 7546
774e132e 7547 *res_btf = btf;
9dbe6015 7548 *res_mod_btf = mod_btf;
774e132e
MKL
7549 return id;
7550}
7551
7552static int bpf_object__resolve_ksym_var_btf_id(struct bpf_object *obj,
7553 struct extern_desc *ext)
7554{
7555 const struct btf_type *targ_var, *targ_type;
7556 __u32 targ_type_id, local_type_id;
9dbe6015 7557 struct module_btf *mod_btf = NULL;
774e132e 7558 const char *targ_var_name;
774e132e 7559 struct btf *btf = NULL;
9dbe6015 7560 int id, err;
774e132e 7561
9dbe6015 7562 id = find_ksym_btf_id(obj, ext->name, BTF_KIND_VAR, &btf, &mod_btf);
466b2e13
KKD
7563 if (id < 0) {
7564 if (id == -ESRCH && ext->is_weak)
7565 return 0;
2211c825
HL
7566 pr_warn("extern (var ksym) '%s': not found in kernel BTF\n",
7567 ext->name);
774e132e 7568 return id;
2211c825 7569 }
774e132e 7570
933d1aa3
MKL
7571 /* find local type_id */
7572 local_type_id = ext->ksym.type_id;
284d2587 7573
933d1aa3
MKL
7574 /* find target type_id */
7575 targ_var = btf__type_by_id(btf, id);
7576 targ_var_name = btf__name_by_offset(btf, targ_var->name_off);
7577 targ_type = skip_mods_and_typedefs(btf, targ_var->type, &targ_type_id);
d370bbe1 7578
933d1aa3
MKL
7579 err = bpf_core_types_are_compat(obj->btf, local_type_id,
7580 btf, targ_type_id);
7581 if (err <= 0) {
7582 const struct btf_type *local_type;
7583 const char *targ_name, *local_name;
d370bbe1 7584
933d1aa3
MKL
7585 local_type = btf__type_by_id(obj->btf, local_type_id);
7586 local_name = btf__name_by_offset(obj->btf, local_type->name_off);
7587 targ_name = btf__name_by_offset(btf, targ_type->name_off);
d370bbe1 7588
933d1aa3
MKL
7589 pr_warn("extern (var ksym) '%s': incompatible types, expected [%d] %s %s, but kernel has [%d] %s %s\n",
7590 ext->name, local_type_id,
7591 btf_kind_str(local_type), local_name, targ_type_id,
7592 btf_kind_str(targ_type), targ_name);
7593 return -EINVAL;
7594 }
d370bbe1 7595
933d1aa3 7596 ext->is_set = true;
9dbe6015 7597 ext->ksym.kernel_btf_obj_fd = mod_btf ? mod_btf->fd : 0;
933d1aa3
MKL
7598 ext->ksym.kernel_btf_id = id;
7599 pr_debug("extern (var ksym) '%s': resolved to [%d] %s %s\n",
7600 ext->name, id, btf_kind_str(targ_var), targ_var_name);
d370bbe1 7601
933d1aa3
MKL
7602 return 0;
7603}
d370bbe1 7604
5bd022ec
MKL
7605static int bpf_object__resolve_ksym_func_btf_id(struct bpf_object *obj,
7606 struct extern_desc *ext)
7607{
7608 int local_func_proto_id, kfunc_proto_id, kfunc_id;
9dbe6015 7609 struct module_btf *mod_btf = NULL;
5bd022ec
MKL
7610 const struct btf_type *kern_func;
7611 struct btf *kern_btf = NULL;
9dbe6015 7612 int ret;
5bd022ec
MKL
7613
7614 local_func_proto_id = ext->ksym.type_id;
7615
9dbe6015 7616 kfunc_id = find_ksym_btf_id(obj, ext->name, BTF_KIND_FUNC, &kern_btf, &mod_btf);
5bd022ec 7617 if (kfunc_id < 0) {
466b2e13
KKD
7618 if (kfunc_id == -ESRCH && ext->is_weak)
7619 return 0;
7620 pr_warn("extern (func ksym) '%s': not found in kernel or module BTFs\n",
5bd022ec
MKL
7621 ext->name);
7622 return kfunc_id;
7623 }
7624
5bd022ec
MKL
7625 kern_func = btf__type_by_id(kern_btf, kfunc_id);
7626 kfunc_proto_id = kern_func->type;
7627
7628 ret = bpf_core_types_are_compat(obj->btf, local_func_proto_id,
7629 kern_btf, kfunc_proto_id);
7630 if (ret <= 0) {
f709160d
AN
7631 pr_warn("extern (func ksym) '%s': func_proto [%d] incompatible with %s [%d]\n",
7632 ext->name, local_func_proto_id,
7633 mod_btf ? mod_btf->name : "vmlinux", kfunc_proto_id);
5bd022ec
MKL
7634 return -EINVAL;
7635 }
7636
9dbe6015
KKD
7637 /* set index for module BTF fd in fd_array, if unset */
7638 if (mod_btf && !mod_btf->fd_array_idx) {
7639 /* insn->off is s16 */
7640 if (obj->fd_array_cnt == INT16_MAX) {
7641 pr_warn("extern (func ksym) '%s': module BTF fd index %d too big to fit in bpf_insn offset\n",
7642 ext->name, mod_btf->fd_array_idx);
7643 return -E2BIG;
7644 }
7645 /* Cannot use index 0 for module BTF fd */
7646 if (!obj->fd_array_cnt)
7647 obj->fd_array_cnt = 1;
7648
7649 ret = libbpf_ensure_mem((void **)&obj->fd_array, &obj->fd_array_cap, sizeof(int),
7650 obj->fd_array_cnt + 1);
7651 if (ret)
7652 return ret;
7653 mod_btf->fd_array_idx = obj->fd_array_cnt;
7654 /* we assume module BTF FD is always >0 */
7655 obj->fd_array[obj->fd_array_cnt++] = mod_btf->fd;
7656 }
7657
5bd022ec 7658 ext->is_set = true;
5bd022ec 7659 ext->ksym.kernel_btf_id = kfunc_id;
9dbe6015 7660 ext->ksym.btf_fd_idx = mod_btf ? mod_btf->fd_array_idx : 0;
5fc13ad5
AS
7661 /* Also set kernel_btf_obj_fd to make sure that bpf_object__relocate_data()
7662 * populates FD into ld_imm64 insn when it's used to point to kfunc.
7663 * {kernel_btf_id, btf_fd_idx} -> fixup bpf_call.
7664 * {kernel_btf_id, kernel_btf_obj_fd} -> fixup ld_imm64.
7665 */
7666 ext->ksym.kernel_btf_obj_fd = mod_btf ? mod_btf->fd : 0;
f709160d
AN
7667 pr_debug("extern (func ksym) '%s': resolved to %s [%d]\n",
7668 ext->name, mod_btf ? mod_btf->name : "vmlinux", kfunc_id);
5bd022ec
MKL
7669
7670 return 0;
7671}
7672
933d1aa3
MKL
7673static int bpf_object__resolve_ksyms_btf_id(struct bpf_object *obj)
7674{
5bd022ec 7675 const struct btf_type *t;
933d1aa3
MKL
7676 struct extern_desc *ext;
7677 int i, err;
7678
7679 for (i = 0; i < obj->nr_extern; i++) {
7680 ext = &obj->externs[i];
7681 if (ext->type != EXT_KSYM || !ext->ksym.type_id)
7682 continue;
7683
67234743
AS
7684 if (obj->gen_loader) {
7685 ext->is_set = true;
7686 ext->ksym.kernel_btf_obj_fd = 0;
7687 ext->ksym.kernel_btf_id = 0;
7688 continue;
7689 }
5bd022ec
MKL
7690 t = btf__type_by_id(obj->btf, ext->btf_id);
7691 if (btf_is_var(t))
7692 err = bpf_object__resolve_ksym_var_btf_id(obj, ext);
7693 else
7694 err = bpf_object__resolve_ksym_func_btf_id(obj, ext);
933d1aa3
MKL
7695 if (err)
7696 return err;
d370bbe1
HL
7697 }
7698 return 0;
7699}
7700
166750bc 7701static int bpf_object__resolve_externs(struct bpf_object *obj,
8601fd42 7702 const char *extra_kconfig)
166750bc 7703{
1c0c7074 7704 bool need_config = false, need_kallsyms = false;
d370bbe1 7705 bool need_vmlinux_btf = false;
166750bc 7706 struct extern_desc *ext;
2e33efe3 7707 void *kcfg_data = NULL;
166750bc 7708 int err, i;
166750bc
AN
7709
7710 if (obj->nr_extern == 0)
7711 return 0;
7712
2e33efe3
AN
7713 if (obj->kconfig_map_idx >= 0)
7714 kcfg_data = obj->maps[obj->kconfig_map_idx].mmaped;
166750bc
AN
7715
7716 for (i = 0; i < obj->nr_extern; i++) {
7717 ext = &obj->externs[i];
7718
55d00c37
AN
7719 if (ext->type == EXT_KSYM) {
7720 if (ext->ksym.type_id)
7721 need_vmlinux_btf = true;
7722 else
7723 need_kallsyms = true;
7724 continue;
7725 } else if (ext->type == EXT_KCFG) {
7726 void *ext_ptr = kcfg_data + ext->kcfg.data_off;
7727 __u64 value = 0;
166750bc 7728
55d00c37
AN
7729 /* Kconfig externs need actual /proc/config.gz */
7730 if (str_has_pfx(ext->name, "CONFIG_")) {
7731 need_config = true;
7732 continue;
7733 }
7734
7735 /* Virtual kcfg externs are customly handled by libbpf */
7736 if (strcmp(ext->name, "LINUX_KERNEL_VERSION") == 0) {
7737 value = get_kernel_version();
7738 if (!value) {
7739 pr_warn("extern (kcfg) '%s': failed to get kernel version\n", ext->name);
7740 return -EINVAL;
7741 }
7742 } else if (strcmp(ext->name, "LINUX_HAS_BPF_COOKIE") == 0) {
7743 value = kernel_supports(obj, FEAT_BPF_COOKIE);
6f5d467d
AN
7744 } else if (strcmp(ext->name, "LINUX_HAS_SYSCALL_WRAPPER") == 0) {
7745 value = kernel_supports(obj, FEAT_SYSCALL_WRAPPER);
55d00c37
AN
7746 } else if (!str_has_pfx(ext->name, "LINUX_") || !ext->is_weak) {
7747 /* Currently libbpf supports only CONFIG_ and LINUX_ prefixed
7748 * __kconfig externs, where LINUX_ ones are virtual and filled out
7749 * customly by libbpf (their values don't come from Kconfig).
7750 * If LINUX_xxx variable is not recognized by libbpf, but is marked
7751 * __weak, it defaults to zero value, just like for CONFIG_xxx
7752 * externs.
7753 */
7754 pr_warn("extern (kcfg) '%s': unrecognized virtual extern\n", ext->name);
166750bc
AN
7755 return -EINVAL;
7756 }
55d00c37
AN
7757
7758 err = set_kcfg_value_num(ext, ext_ptr, value);
166750bc
AN
7759 if (err)
7760 return err;
55d00c37
AN
7761 pr_debug("extern (kcfg) '%s': set to 0x%llx\n",
7762 ext->name, (long long)value);
166750bc 7763 } else {
55d00c37 7764 pr_warn("extern '%s': unrecognized extern kind\n", ext->name);
166750bc
AN
7765 return -EINVAL;
7766 }
7767 }
8601fd42 7768 if (need_config && extra_kconfig) {
2e33efe3 7769 err = bpf_object__read_kconfig_mem(obj, extra_kconfig, kcfg_data);
8601fd42
AN
7770 if (err)
7771 return -EINVAL;
7772 need_config = false;
7773 for (i = 0; i < obj->nr_extern; i++) {
7774 ext = &obj->externs[i];
2e33efe3 7775 if (ext->type == EXT_KCFG && !ext->is_set) {
8601fd42
AN
7776 need_config = true;
7777 break;
7778 }
7779 }
7780 }
166750bc 7781 if (need_config) {
2e33efe3 7782 err = bpf_object__read_kconfig_file(obj, kcfg_data);
166750bc
AN
7783 if (err)
7784 return -EINVAL;
7785 }
1c0c7074
AN
7786 if (need_kallsyms) {
7787 err = bpf_object__read_kallsyms_file(obj);
7788 if (err)
7789 return -EINVAL;
7790 }
d370bbe1
HL
7791 if (need_vmlinux_btf) {
7792 err = bpf_object__resolve_ksyms_btf_id(obj);
7793 if (err)
7794 return -EINVAL;
7795 }
166750bc
AN
7796 for (i = 0; i < obj->nr_extern; i++) {
7797 ext = &obj->externs[i];
7798
7799 if (!ext->is_set && !ext->is_weak) {
55d00c37 7800 pr_warn("extern '%s' (strong): not resolved\n", ext->name);
166750bc
AN
7801 return -ESRCH;
7802 } else if (!ext->is_set) {
55d00c37 7803 pr_debug("extern '%s' (weak): not resolved, defaulting to zero\n",
166750bc
AN
7804 ext->name);
7805 }
7806 }
7807
7808 return 0;
7809}
7810
8d1608d7
KFL
7811static void bpf_map_prepare_vdata(const struct bpf_map *map)
7812{
7813 struct bpf_struct_ops *st_ops;
7814 __u32 i;
7815
7816 st_ops = map->st_ops;
7817 for (i = 0; i < btf_vlen(st_ops->type); i++) {
7818 struct bpf_program *prog = st_ops->progs[i];
7819 void *kern_data;
7820 int prog_fd;
7821
7822 if (!prog)
7823 continue;
7824
7825 prog_fd = bpf_program__fd(prog);
7826 kern_data = st_ops->kern_vdata + st_ops->kern_func_off[i];
7827 *(unsigned long *)kern_data = prog_fd;
7828 }
7829}
7830
7831static int bpf_object_prepare_struct_ops(struct bpf_object *obj)
7832{
7833 int i;
7834
7835 for (i = 0; i < obj->nr_maps; i++)
7836 if (bpf_map__is_struct_ops(&obj->maps[i]))
7837 bpf_map_prepare_vdata(&obj->maps[i]);
7838
7839 return 0;
7840}
7841
e7b924ca 7842static int bpf_object_load(struct bpf_object *obj, int extra_log_level, const char *target_btf_path)
52d3352e 7843{
ec6d5f47 7844 int err, i;
6371ca3b 7845
52d3352e 7846 if (!obj)
e9fc3ce9 7847 return libbpf_err(-EINVAL);
52d3352e
WN
7848
7849 if (obj->loaded) {
d9297581 7850 pr_warn("object '%s': load can't be attempted twice\n", obj->name);
e9fc3ce9 7851 return libbpf_err(-EINVAL);
52d3352e
WN
7852 }
7853
67234743 7854 if (obj->gen_loader)
be315829 7855 bpf_gen__init(obj->gen_loader, extra_log_level, obj->nr_programs, obj->nr_maps);
67234743 7856
fd9eef1a 7857 err = bpf_object__probe_loading(obj);
fe62de31 7858 err = err ? : bpf_object__load_vmlinux_btf(obj, false);
8601fd42 7859 err = err ? : bpf_object__resolve_externs(obj, obj->kconfig);
0d13bfce
AN
7860 err = err ? : bpf_object__sanitize_and_load_btf(obj);
7861 err = err ? : bpf_object__sanitize_maps(obj);
590a0088 7862 err = err ? : bpf_object__init_kern_struct_ops_maps(obj);
0d13bfce 7863 err = err ? : bpf_object__create_maps(obj);
e7b924ca
AN
7864 err = err ? : bpf_object__relocate(obj, obj->btf_custom_path ? : target_btf_path);
7865 err = err ? : bpf_object__load_progs(obj, extra_log_level);
341ac5ff 7866 err = err ? : bpf_object_init_prog_arrays(obj);
8d1608d7 7867 err = err ? : bpf_object_prepare_struct_ops(obj);
a6ed02ca 7868
67234743
AS
7869 if (obj->gen_loader) {
7870 /* reset FDs */
4729445b
KKD
7871 if (obj->btf)
7872 btf__set_fd(obj->btf, -1);
67234743
AS
7873 for (i = 0; i < obj->nr_maps; i++)
7874 obj->maps[i].fd = -1;
7875 if (!err)
ba05fd36 7876 err = bpf_gen__finish(obj->gen_loader, obj->nr_programs, obj->nr_maps);
67234743
AS
7877 }
7878
9dbe6015
KKD
7879 /* clean up fd_array */
7880 zfree(&obj->fd_array);
7881
4f33a53d
AN
7882 /* clean up module BTFs */
7883 for (i = 0; i < obj->btf_module_cnt; i++) {
91abb4a6 7884 close(obj->btf_modules[i].fd);
4f33a53d
AN
7885 btf__free(obj->btf_modules[i].btf);
7886 free(obj->btf_modules[i].name);
7887 }
7888 free(obj->btf_modules);
7889
7890 /* clean up vmlinux BTF */
a6ed02ca
KS
7891 btf__free(obj->btf_vmlinux);
7892 obj->btf_vmlinux = NULL;
7893
d9297581
AN
7894 obj->loaded = true; /* doesn't matter if successfully or not */
7895
0d13bfce
AN
7896 if (err)
7897 goto out;
52d3352e
WN
7898
7899 return 0;
7900out:
ec6d5f47
THJ
7901 /* unpin any maps that were auto-pinned during load */
7902 for (i = 0; i < obj->nr_maps; i++)
7903 if (obj->maps[i].pinned && !obj->maps[i].reused)
7904 bpf_map__unpin(&obj->maps[i], NULL);
7905
4a404a7e 7906 bpf_object_unload(obj);
be18010e 7907 pr_warn("failed to load object '%s'\n", obj->path);
e9fc3ce9 7908 return libbpf_err(err);
52d3352e
WN
7909}
7910
e7b924ca
AN
7911int bpf_object__load(struct bpf_object *obj)
7912{
7913 return bpf_object_load(obj, 0, NULL);
60276f98
QM
7914}
7915
196f8487
THJ
7916static int make_parent_dir(const char *path)
7917{
7918 char *cp, errmsg[STRERR_BUFSIZE];
7919 char *dname, *dir;
7920 int err = 0;
7921
7922 dname = strdup(path);
7923 if (dname == NULL)
7924 return -ENOMEM;
7925
7926 dir = dirname(dname);
7927 if (mkdir(dir, 0700) && errno != EEXIST)
7928 err = -errno;
7929
7930 free(dname);
7931 if (err) {
7932 cp = libbpf_strerror_r(-err, errmsg, sizeof(errmsg));
7933 pr_warn("failed to mkdir %s: %s\n", path, cp);
7934 }
7935 return err;
7936}
7937
f367540c
JS
7938static int check_path(const char *path)
7939{
1ce6a9fc 7940 char *cp, errmsg[STRERR_BUFSIZE];
f367540c
JS
7941 struct statfs st_fs;
7942 char *dname, *dir;
7943 int err = 0;
7944
7945 if (path == NULL)
7946 return -EINVAL;
7947
7948 dname = strdup(path);
7949 if (dname == NULL)
7950 return -ENOMEM;
7951
7952 dir = dirname(dname);
7953 if (statfs(dir, &st_fs)) {
24d6a808 7954 cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
be18010e 7955 pr_warn("failed to statfs %s: %s\n", dir, cp);
f367540c
JS
7956 err = -errno;
7957 }
7958 free(dname);
7959
7960 if (!err && st_fs.f_type != BPF_FS_MAGIC) {
be18010e 7961 pr_warn("specified path %s is not on BPF FS\n", path);
f367540c
JS
7962 err = -EINVAL;
7963 }
7964
7965 return err;
7966}
7967
cf90a20d 7968int bpf_program__pin(struct bpf_program *prog, const char *path)
f367540c 7969{
1ce6a9fc 7970 char *cp, errmsg[STRERR_BUFSIZE];
f367540c
JS
7971 int err;
7972
cf90a20d
AN
7973 if (prog->fd < 0) {
7974 pr_warn("prog '%s': can't pin program that wasn't loaded\n", prog->name);
7975 return libbpf_err(-EINVAL);
7976 }
7977
196f8487
THJ
7978 err = make_parent_dir(path);
7979 if (err)
e9fc3ce9 7980 return libbpf_err(err);
196f8487 7981
f367540c
JS
7982 err = check_path(path);
7983 if (err)
e9fc3ce9 7984 return libbpf_err(err);
f367540c 7985
cf90a20d 7986 if (bpf_obj_pin(prog->fd, path)) {
23ab656b
THJ
7987 err = -errno;
7988 cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg));
cf90a20d 7989 pr_warn("prog '%s': failed to pin at '%s': %s\n", prog->name, path, cp);
e9fc3ce9 7990 return libbpf_err(err);
f367540c 7991 }
f367540c 7992
cf90a20d 7993 pr_debug("prog '%s': pinned at '%s'\n", prog->name, path);
f367540c
JS
7994 return 0;
7995}
7996
cf90a20d 7997int bpf_program__unpin(struct bpf_program *prog, const char *path)
0c19a9fb
SF
7998{
7999 int err;
8000
cf90a20d
AN
8001 if (prog->fd < 0) {
8002 pr_warn("prog '%s': can't unpin program that wasn't loaded\n", prog->name);
e9fc3ce9 8003 return libbpf_err(-EINVAL);
0c19a9fb
SF
8004 }
8005
f367540c
JS
8006 err = check_path(path);
8007 if (err)
e9fc3ce9 8008 return libbpf_err(err);
f367540c 8009
cf90a20d 8010 err = unlink(path);
0c19a9fb 8011 if (err)
e9fc3ce9 8012 return libbpf_err(-errno);
0c19a9fb 8013
cf90a20d 8014 pr_debug("prog '%s': unpinned from '%s'\n", prog->name, path);
f367540c
JS
8015 return 0;
8016}
8017
b6989f35
JS
8018int bpf_map__pin(struct bpf_map *map, const char *path)
8019{
1ce6a9fc 8020 char *cp, errmsg[STRERR_BUFSIZE];
b6989f35
JS
8021 int err;
8022
b6989f35 8023 if (map == NULL) {
be18010e 8024 pr_warn("invalid map pointer\n");
e9fc3ce9 8025 return libbpf_err(-EINVAL);
b6989f35
JS
8026 }
8027
4580b25f
THJ
8028 if (map->pin_path) {
8029 if (path && strcmp(path, map->pin_path)) {
8030 pr_warn("map '%s' already has pin path '%s' different from '%s'\n",
8031 bpf_map__name(map), map->pin_path, path);
e9fc3ce9 8032 return libbpf_err(-EINVAL);
4580b25f
THJ
8033 } else if (map->pinned) {
8034 pr_debug("map '%s' already pinned at '%s'; not re-pinning\n",
8035 bpf_map__name(map), map->pin_path);
8036 return 0;
8037 }
8038 } else {
8039 if (!path) {
8040 pr_warn("missing a path to pin map '%s' at\n",
8041 bpf_map__name(map));
e9fc3ce9 8042 return libbpf_err(-EINVAL);
4580b25f
THJ
8043 } else if (map->pinned) {
8044 pr_warn("map '%s' already pinned\n", bpf_map__name(map));
e9fc3ce9 8045 return libbpf_err(-EEXIST);
4580b25f
THJ
8046 }
8047
8048 map->pin_path = strdup(path);
8049 if (!map->pin_path) {
8050 err = -errno;
8051 goto out_err;
8052 }
b6989f35
JS
8053 }
8054
196f8487
THJ
8055 err = make_parent_dir(map->pin_path);
8056 if (err)
e9fc3ce9 8057 return libbpf_err(err);
196f8487 8058
4580b25f
THJ
8059 err = check_path(map->pin_path);
8060 if (err)
e9fc3ce9 8061 return libbpf_err(err);
4580b25f
THJ
8062
8063 if (bpf_obj_pin(map->fd, map->pin_path)) {
8064 err = -errno;
8065 goto out_err;
8066 }
8067
8068 map->pinned = true;
8069 pr_debug("pinned map '%s'\n", map->pin_path);
0c19a9fb 8070
b6989f35 8071 return 0;
4580b25f
THJ
8072
8073out_err:
8074 cp = libbpf_strerror_r(-err, errmsg, sizeof(errmsg));
8075 pr_warn("failed to pin map: %s\n", cp);
e9fc3ce9 8076 return libbpf_err(err);
b6989f35
JS
8077}
8078
0c19a9fb
SF
8079int bpf_map__unpin(struct bpf_map *map, const char *path)
8080{
8081 int err;
8082
0c19a9fb 8083 if (map == NULL) {
be18010e 8084 pr_warn("invalid map pointer\n");
e9fc3ce9 8085 return libbpf_err(-EINVAL);
0c19a9fb
SF
8086 }
8087
4580b25f
THJ
8088 if (map->pin_path) {
8089 if (path && strcmp(path, map->pin_path)) {
8090 pr_warn("map '%s' already has pin path '%s' different from '%s'\n",
8091 bpf_map__name(map), map->pin_path, path);
e9fc3ce9 8092 return libbpf_err(-EINVAL);
4580b25f
THJ
8093 }
8094 path = map->pin_path;
8095 } else if (!path) {
8096 pr_warn("no path to unpin map '%s' from\n",
8097 bpf_map__name(map));
e9fc3ce9 8098 return libbpf_err(-EINVAL);
4580b25f
THJ
8099 }
8100
8101 err = check_path(path);
8102 if (err)
e9fc3ce9 8103 return libbpf_err(err);
4580b25f 8104
0c19a9fb
SF
8105 err = unlink(path);
8106 if (err != 0)
e9fc3ce9 8107 return libbpf_err(-errno);
4580b25f
THJ
8108
8109 map->pinned = false;
8110 pr_debug("unpinned map '%s' from '%s'\n", bpf_map__name(map), path);
0c19a9fb
SF
8111
8112 return 0;
8113}
8114
4580b25f
THJ
8115int bpf_map__set_pin_path(struct bpf_map *map, const char *path)
8116{
8117 char *new = NULL;
8118
8119 if (path) {
8120 new = strdup(path);
8121 if (!new)
e9fc3ce9 8122 return libbpf_err(-errno);
4580b25f
THJ
8123 }
8124
8125 free(map->pin_path);
8126 map->pin_path = new;
8127 return 0;
8128}
8129
20eccf29
AN
8130__alias(bpf_map__pin_path)
8131const char *bpf_map__get_pin_path(const struct bpf_map *map);
4580b25f 8132
e244d34d
EL
8133const char *bpf_map__pin_path(const struct bpf_map *map)
8134{
8135 return map->pin_path;
8136}
8137
4580b25f
THJ
8138bool bpf_map__is_pinned(const struct bpf_map *map)
8139{
8140 return map->pinned;
8141}
8142
9cf309c5
THJ
8143static void sanitize_pin_path(char *s)
8144{
8145 /* bpffs disallows periods in path names */
8146 while (*s) {
8147 if (*s == '.')
8148 *s = '_';
8149 s++;
8150 }
8151}
8152
0c19a9fb 8153int bpf_object__pin_maps(struct bpf_object *obj, const char *path)
d5148d85 8154{
d5148d85
JS
8155 struct bpf_map *map;
8156 int err;
8157
8158 if (!obj)
e9fc3ce9 8159 return libbpf_err(-ENOENT);
d5148d85
JS
8160
8161 if (!obj->loaded) {
be18010e 8162 pr_warn("object not yet loaded; load it first\n");
e9fc3ce9 8163 return libbpf_err(-ENOENT);
d5148d85
JS
8164 }
8165
f74a53d9 8166 bpf_object__for_each_map(map, obj) {
4580b25f 8167 char *pin_path = NULL;
0c19a9fb 8168 char buf[PATH_MAX];
0c19a9fb 8169
ec41817b 8170 if (!map->autocreate)
229fae38
SC
8171 continue;
8172
4580b25f 8173 if (path) {
e588c116
WY
8174 err = pathname_concat(buf, sizeof(buf), path, bpf_map__name(map));
8175 if (err)
4580b25f 8176 goto err_unpin_maps;
9cf309c5 8177 sanitize_pin_path(buf);
4580b25f
THJ
8178 pin_path = buf;
8179 } else if (!map->pin_path) {
8180 continue;
0c19a9fb
SF
8181 }
8182
4580b25f 8183 err = bpf_map__pin(map, pin_path);
0c19a9fb
SF
8184 if (err)
8185 goto err_unpin_maps;
8186 }
8187
8188 return 0;
8189
8190err_unpin_maps:
bcc40fc0 8191 while ((map = bpf_object__prev_map(obj, map))) {
4580b25f 8192 if (!map->pin_path)
0c19a9fb
SF
8193 continue;
8194
4580b25f 8195 bpf_map__unpin(map, NULL);
0c19a9fb
SF
8196 }
8197
e9fc3ce9 8198 return libbpf_err(err);
0c19a9fb
SF
8199}
8200
8201int bpf_object__unpin_maps(struct bpf_object *obj, const char *path)
8202{
8203 struct bpf_map *map;
8204 int err;
8205
8206 if (!obj)
e9fc3ce9 8207 return libbpf_err(-ENOENT);
0c19a9fb 8208
f74a53d9 8209 bpf_object__for_each_map(map, obj) {
4580b25f 8210 char *pin_path = NULL;
d5148d85 8211 char buf[PATH_MAX];
d5148d85 8212
4580b25f 8213 if (path) {
e588c116
WY
8214 err = pathname_concat(buf, sizeof(buf), path, bpf_map__name(map));
8215 if (err)
8216 return libbpf_err(err);
9cf309c5 8217 sanitize_pin_path(buf);
4580b25f
THJ
8218 pin_path = buf;
8219 } else if (!map->pin_path) {
8220 continue;
8221 }
d5148d85 8222
4580b25f 8223 err = bpf_map__unpin(map, pin_path);
d5148d85 8224 if (err)
e9fc3ce9 8225 return libbpf_err(err);
d5148d85
JS
8226 }
8227
0c19a9fb
SF
8228 return 0;
8229}
8230
8231int bpf_object__pin_programs(struct bpf_object *obj, const char *path)
8232{
8233 struct bpf_program *prog;
e588c116 8234 char buf[PATH_MAX];
0c19a9fb
SF
8235 int err;
8236
8237 if (!obj)
e9fc3ce9 8238 return libbpf_err(-ENOENT);
0c19a9fb
SF
8239
8240 if (!obj->loaded) {
be18010e 8241 pr_warn("object not yet loaded; load it first\n");
e9fc3ce9 8242 return libbpf_err(-ENOENT);
0c19a9fb
SF
8243 }
8244
0c19a9fb 8245 bpf_object__for_each_program(prog, obj) {
e588c116
WY
8246 err = pathname_concat(buf, sizeof(buf), path, prog->name);
8247 if (err)
0c19a9fb 8248 goto err_unpin_programs;
0c19a9fb
SF
8249
8250 err = bpf_program__pin(prog, buf);
8251 if (err)
8252 goto err_unpin_programs;
8253 }
8254
8255 return 0;
8256
8257err_unpin_programs:
bcc40fc0 8258 while ((prog = bpf_object__prev_program(obj, prog))) {
e588c116 8259 if (pathname_concat(buf, sizeof(buf), path, prog->name))
0c19a9fb
SF
8260 continue;
8261
8262 bpf_program__unpin(prog, buf);
8263 }
8264
e9fc3ce9 8265 return libbpf_err(err);
0c19a9fb
SF
8266}
8267
8268int bpf_object__unpin_programs(struct bpf_object *obj, const char *path)
8269{
8270 struct bpf_program *prog;
8271 int err;
8272
8273 if (!obj)
e9fc3ce9 8274 return libbpf_err(-ENOENT);
0c19a9fb 8275
d5148d85
JS
8276 bpf_object__for_each_program(prog, obj) {
8277 char buf[PATH_MAX];
d5148d85 8278
e588c116
WY
8279 err = pathname_concat(buf, sizeof(buf), path, prog->name);
8280 if (err)
8281 return libbpf_err(err);
d5148d85 8282
0c19a9fb 8283 err = bpf_program__unpin(prog, buf);
d5148d85 8284 if (err)
e9fc3ce9 8285 return libbpf_err(err);
d5148d85
JS
8286 }
8287
8288 return 0;
8289}
8290
0c19a9fb
SF
8291int bpf_object__pin(struct bpf_object *obj, const char *path)
8292{
8293 int err;
8294
8295 err = bpf_object__pin_maps(obj, path);
8296 if (err)
e9fc3ce9 8297 return libbpf_err(err);
0c19a9fb
SF
8298
8299 err = bpf_object__pin_programs(obj, path);
8300 if (err) {
8301 bpf_object__unpin_maps(obj, path);
e9fc3ce9 8302 return libbpf_err(err);
0c19a9fb
SF
8303 }
8304
8305 return 0;
8306}
8307
2d39d7c5
AN
8308static void bpf_map__destroy(struct bpf_map *map)
8309{
646f02ff
AN
8310 if (map->inner_map) {
8311 bpf_map__destroy(map->inner_map);
8312 zfree(&map->inner_map);
8313 }
8314
8315 zfree(&map->init_slots);
8316 map->init_slots_sz = 0;
8317
2d39d7c5 8318 if (map->mmaped) {
9d0a2331
JK
8319 size_t mmap_sz;
8320
8321 mmap_sz = bpf_map_mmap_sz(map->def.value_size, map->def.max_entries);
8322 munmap(map->mmaped, mmap_sz);
2d39d7c5
AN
8323 map->mmaped = NULL;
8324 }
8325
8326 if (map->st_ops) {
8327 zfree(&map->st_ops->data);
8328 zfree(&map->st_ops->progs);
8329 zfree(&map->st_ops->kern_func_off);
8330 zfree(&map->st_ops);
8331 }
8332
8333 zfree(&map->name);
aed65917 8334 zfree(&map->real_name);
2d39d7c5
AN
8335 zfree(&map->pin_path);
8336
8337 if (map->fd >= 0)
8338 zclose(map->fd);
8339}
8340
1a5e3fb1
WN
8341void bpf_object__close(struct bpf_object *obj)
8342{
a5b8bd47
WN
8343 size_t i;
8344
50450fc7 8345 if (IS_ERR_OR_NULL(obj))
1a5e3fb1
WN
8346 return;
8347
2e4913e0
AN
8348 usdt_manager_free(obj->usdt_man);
8349 obj->usdt_man = NULL;
8350
67234743 8351 bpf_gen__free(obj->gen_loader);
1a5e3fb1 8352 bpf_object__elf_finish(obj);
4a404a7e 8353 bpf_object_unload(obj);
8a138aed 8354 btf__free(obj->btf);
2993e051 8355 btf_ext__free(obj->btf_ext);
1a5e3fb1 8356
2d39d7c5
AN
8357 for (i = 0; i < obj->nr_maps; i++)
8358 bpf_map__destroy(&obj->maps[i]);
d859900c 8359
1373ff59 8360 zfree(&obj->btf_custom_path);
8601fd42 8361 zfree(&obj->kconfig);
166750bc
AN
8362 zfree(&obj->externs);
8363 obj->nr_extern = 0;
8364
9d759a9b
WN
8365 zfree(&obj->maps);
8366 obj->nr_maps = 0;
a5b8bd47
WN
8367
8368 if (obj->programs && obj->nr_programs) {
8369 for (i = 0; i < obj->nr_programs; i++)
8370 bpf_program__exit(&obj->programs[i]);
8371 }
8372 zfree(&obj->programs);
8373
1a5e3fb1
WN
8374 free(obj);
8375}
aa9b1ac3 8376
a324aae3 8377const char *bpf_object__name(const struct bpf_object *obj)
acf860ae 8378{
e9fc3ce9 8379 return obj ? obj->name : libbpf_err_ptr(-EINVAL);
acf860ae
WN
8380}
8381
a324aae3 8382unsigned int bpf_object__kversion(const struct bpf_object *obj)
45825d8a 8383{
a7fe0450 8384 return obj ? obj->kern_version : 0;
45825d8a
WN
8385}
8386
a324aae3 8387struct btf *bpf_object__btf(const struct bpf_object *obj)
789f6bab
AI
8388{
8389 return obj ? obj->btf : NULL;
8390}
8391
8a138aed
MKL
8392int bpf_object__btf_fd(const struct bpf_object *obj)
8393{
8394 return obj->btf ? btf__fd(obj->btf) : -1;
8395}
8396
155f556d
RDT
8397int bpf_object__set_kversion(struct bpf_object *obj, __u32 kern_version)
8398{
8399 if (obj->loaded)
e9fc3ce9 8400 return libbpf_err(-EINVAL);
155f556d
RDT
8401
8402 obj->kern_version = kern_version;
8403
8404 return 0;
8405}
8406
67234743
AS
8407int bpf_object__gen_loader(struct bpf_object *obj, struct gen_loader_opts *opts)
8408{
8409 struct bpf_gen *gen;
8410
8411 if (!opts)
8412 return -EFAULT;
8413 if (!OPTS_VALID(opts, gen_loader_opts))
8414 return -EINVAL;
8415 gen = calloc(sizeof(*gen), 1);
8416 if (!gen)
8417 return -ENOMEM;
8418 gen->opts = opts;
8419 obj->gen_loader = gen;
8420 return 0;
8421}
8422
eac7d845 8423static struct bpf_program *
a324aae3
AN
8424__bpf_program__iter(const struct bpf_program *p, const struct bpf_object *obj,
8425 bool forward)
aa9b1ac3 8426{
a83d6e76 8427 size_t nr_programs = obj->nr_programs;
0c19a9fb 8428 ssize_t idx;
aa9b1ac3 8429
a83d6e76 8430 if (!nr_programs)
aa9b1ac3 8431 return NULL;
aa9b1ac3 8432
a83d6e76
MKL
8433 if (!p)
8434 /* Iter from the beginning */
8435 return forward ? &obj->programs[0] :
8436 &obj->programs[nr_programs - 1];
8437
0c19a9fb 8438 if (p->obj != obj) {
be18010e 8439 pr_warn("error: program handler doesn't match object\n");
e9fc3ce9 8440 return errno = EINVAL, NULL;
aa9b1ac3
WN
8441 }
8442
a83d6e76 8443 idx = (p - obj->programs) + (forward ? 1 : -1);
0c19a9fb 8444 if (idx >= obj->nr_programs || idx < 0)
aa9b1ac3
WN
8445 return NULL;
8446 return &obj->programs[idx];
8447}
8448
2088a3a7
HC
8449struct bpf_program *
8450bpf_object__next_program(const struct bpf_object *obj, struct bpf_program *prev)
eac7d845
JK
8451{
8452 struct bpf_program *prog = prev;
8453
8454 do {
a83d6e76 8455 prog = __bpf_program__iter(prog, obj, true);
c3c55696 8456 } while (prog && prog_is_subprog(obj, prog));
0c19a9fb
SF
8457
8458 return prog;
8459}
8460
2088a3a7
HC
8461struct bpf_program *
8462bpf_object__prev_program(const struct bpf_object *obj, struct bpf_program *next)
0c19a9fb
SF
8463{
8464 struct bpf_program *prog = next;
8465
0c19a9fb 8466 do {
a83d6e76 8467 prog = __bpf_program__iter(prog, obj, false);
c3c55696 8468 } while (prog && prog_is_subprog(obj, prog));
eac7d845
JK
8469
8470 return prog;
8471}
8472
9aba3613
JK
8473void bpf_program__set_ifindex(struct bpf_program *prog, __u32 ifindex)
8474{
8475 prog->prog_ifindex = ifindex;
8476}
8477
01af3bf0
AN
8478const char *bpf_program__name(const struct bpf_program *prog)
8479{
8480 return prog->name;
8481}
8482
52109584
AN
8483const char *bpf_program__section_name(const struct bpf_program *prog)
8484{
8485 return prog->sec_name;
8486}
8487
d9297581
AN
8488bool bpf_program__autoload(const struct bpf_program *prog)
8489{
a3820c48 8490 return prog->autoload;
d9297581
AN
8491}
8492
8493int bpf_program__set_autoload(struct bpf_program *prog, bool autoload)
8494{
8495 if (prog->obj->loaded)
e9fc3ce9 8496 return libbpf_err(-EINVAL);
d9297581 8497
a3820c48 8498 prog->autoload = autoload;
d9297581
AN
8499 return 0;
8500}
8501
43cb8cba
HL
8502bool bpf_program__autoattach(const struct bpf_program *prog)
8503{
8504 return prog->autoattach;
8505}
8506
8507void bpf_program__set_autoattach(struct bpf_program *prog, bool autoattach)
8508{
8509 prog->autoattach = autoattach;
8510}
8511
65a7fa2e
AN
8512const struct bpf_insn *bpf_program__insns(const struct bpf_program *prog)
8513{
8514 return prog->insns;
8515}
8516
8517size_t bpf_program__insn_cnt(const struct bpf_program *prog)
8518{
8519 return prog->insns_cnt;
8520}
8521
b63b3c49
JO
8522int bpf_program__set_insns(struct bpf_program *prog,
8523 struct bpf_insn *new_insns, size_t new_insn_cnt)
8524{
8525 struct bpf_insn *insns;
8526
8527 if (prog->obj->loaded)
8528 return -EBUSY;
8529
8530 insns = libbpf_reallocarray(prog->insns, new_insn_cnt, sizeof(*insns));
8531 if (!insns) {
8532 pr_warn("prog '%s': failed to realloc prog code\n", prog->name);
8533 return -ENOMEM;
8534 }
8535 memcpy(insns, new_insns, new_insn_cnt * sizeof(*insns));
8536
8537 prog->insns = insns;
8538 prog->insns_cnt = new_insn_cnt;
8539 return 0;
8540}
8541
cf90a20d 8542int bpf_program__fd(const struct bpf_program *prog)
b580563e 8543{
1e960043 8544 if (!prog)
e9fc3ce9 8545 return libbpf_err(-EINVAL);
1e960043 8546
cf90a20d 8547 if (prog->fd < 0)
e9fc3ce9 8548 return libbpf_err(-ENOENT);
b580563e 8549
cf90a20d 8550 return prog->fd;
aa9b1ac3 8551}
9d759a9b 8552
20eccf29
AN
8553__alias(bpf_program__type)
8554enum bpf_prog_type bpf_program__get_type(const struct bpf_program *prog);
8555
8556enum bpf_prog_type bpf_program__type(const struct bpf_program *prog)
f1eead9e
AN
8557{
8558 return prog->type;
8559}
8560
93442f13 8561int bpf_program__set_type(struct bpf_program *prog, enum bpf_prog_type type)
5f44e4c8 8562{
93442f13
GS
8563 if (prog->obj->loaded)
8564 return libbpf_err(-EBUSY);
8565
5f44e4c8 8566 prog->type = type;
d6e6286a 8567 prog->sec_def = NULL;
93442f13 8568 return 0;
5f44e4c8
WN
8569}
8570
20eccf29
AN
8571__alias(bpf_program__expected_attach_type)
8572enum bpf_attach_type bpf_program__get_expected_attach_type(const struct bpf_program *prog);
8573
8574enum bpf_attach_type bpf_program__expected_attach_type(const struct bpf_program *prog)
f1eead9e
AN
8575{
8576 return prog->expected_attach_type;
8577}
8578
93442f13 8579int bpf_program__set_expected_attach_type(struct bpf_program *prog,
16962b24 8580 enum bpf_attach_type type)
d7be143b 8581{
93442f13
GS
8582 if (prog->obj->loaded)
8583 return libbpf_err(-EBUSY);
8584
d7be143b 8585 prog->expected_attach_type = type;
93442f13 8586 return 0;
d7be143b
AI
8587}
8588
a6ca7158
AN
8589__u32 bpf_program__flags(const struct bpf_program *prog)
8590{
8591 return prog->prog_flags;
8592}
8593
8cccee9e 8594int bpf_program__set_flags(struct bpf_program *prog, __u32 flags)
a6ca7158
AN
8595{
8596 if (prog->obj->loaded)
8597 return libbpf_err(-EBUSY);
8598
8cccee9e 8599 prog->prog_flags = flags;
a6ca7158
AN
8600 return 0;
8601}
8602
dbdd2c7f
AN
8603__u32 bpf_program__log_level(const struct bpf_program *prog)
8604{
8605 return prog->log_level;
8606}
8607
8608int bpf_program__set_log_level(struct bpf_program *prog, __u32 log_level)
8609{
8610 if (prog->obj->loaded)
8611 return libbpf_err(-EBUSY);
8612
8613 prog->log_level = log_level;
8614 return 0;
8615}
8616
b3ce9079
AN
8617const char *bpf_program__log_buf(const struct bpf_program *prog, size_t *log_size)
8618{
8619 *log_size = prog->log_size;
8620 return prog->log_buf;
8621}
8622
8623int bpf_program__set_log_buf(struct bpf_program *prog, char *log_buf, size_t log_size)
8624{
8625 if (log_size && !log_buf)
8626 return -EINVAL;
8627 if (prog->log_size > UINT_MAX)
8628 return -EINVAL;
8629 if (prog->obj->loaded)
8630 return -EBUSY;
8631
8632 prog->log_buf = log_buf;
8633 prog->log_size = log_size;
a6ca7158
AN
8634 return 0;
8635}
8636
15ea31fa 8637#define SEC_DEF(sec_pfx, ptype, atype, flags, ...) { \
697f104d 8638 .sec = (char *)sec_pfx, \
d7a18ea7 8639 .prog_type = BPF_PROG_TYPE_##ptype, \
15ea31fa
AN
8640 .expected_attach_type = atype, \
8641 .cookie = (long)(flags), \
4fa5bcfe 8642 .prog_prepare_load_fn = libbpf_prepare_prog_load, \
d7a18ea7
AN
8643 __VA_ARGS__ \
8644}
8645
4fa5bcfe 8646static int attach_kprobe(const struct bpf_program *prog, long cookie, struct bpf_link **link);
39f8dc43 8647static int attach_uprobe(const struct bpf_program *prog, long cookie, struct bpf_link **link);
708ac5be 8648static int attach_ksyscall(const struct bpf_program *prog, long cookie, struct bpf_link **link);
2e4913e0 8649static int attach_usdt(const struct bpf_program *prog, long cookie, struct bpf_link **link);
4fa5bcfe
AN
8650static int attach_tp(const struct bpf_program *prog, long cookie, struct bpf_link **link);
8651static int attach_raw_tp(const struct bpf_program *prog, long cookie, struct bpf_link **link);
8652static int attach_trace(const struct bpf_program *prog, long cookie, struct bpf_link **link);
ddc6b049 8653static int attach_kprobe_multi(const struct bpf_program *prog, long cookie, struct bpf_link **link);
4fa5bcfe
AN
8654static int attach_lsm(const struct bpf_program *prog, long cookie, struct bpf_link **link);
8655static int attach_iter(const struct bpf_program *prog, long cookie, struct bpf_link **link);
d7a18ea7 8656
d7a18ea7 8657static const struct bpf_sec_def section_defs[] = {
450b167f
AN
8658 SEC_DEF("socket", SOCKET_FILTER, 0, SEC_NONE),
8659 SEC_DEF("sk_reuseport/migrate", SK_REUSEPORT, BPF_SK_REUSEPORT_SELECT_OR_MIGRATE, SEC_ATTACHABLE),
8660 SEC_DEF("sk_reuseport", SK_REUSEPORT, BPF_SK_REUSEPORT_SELECT, SEC_ATTACHABLE),
9af8efc4 8661 SEC_DEF("kprobe+", KPROBE, 0, SEC_NONE, attach_kprobe),
39f8dc43 8662 SEC_DEF("uprobe+", KPROBE, 0, SEC_NONE, attach_uprobe),
c4cac71f 8663 SEC_DEF("uprobe.s+", KPROBE, 0, SEC_SLEEPABLE, attach_uprobe),
9af8efc4 8664 SEC_DEF("kretprobe+", KPROBE, 0, SEC_NONE, attach_kprobe),
39f8dc43 8665 SEC_DEF("uretprobe+", KPROBE, 0, SEC_NONE, attach_uprobe),
c4cac71f 8666 SEC_DEF("uretprobe.s+", KPROBE, 0, SEC_SLEEPABLE, attach_uprobe),
9af8efc4
AN
8667 SEC_DEF("kprobe.multi+", KPROBE, BPF_TRACE_KPROBE_MULTI, SEC_NONE, attach_kprobe_multi),
8668 SEC_DEF("kretprobe.multi+", KPROBE, BPF_TRACE_KPROBE_MULTI, SEC_NONE, attach_kprobe_multi),
708ac5be
AN
8669 SEC_DEF("ksyscall+", KPROBE, 0, SEC_NONE, attach_ksyscall),
8670 SEC_DEF("kretsyscall+", KPROBE, 0, SEC_NONE, attach_ksyscall),
2e4913e0 8671 SEC_DEF("usdt+", KPROBE, 0, SEC_NONE, attach_usdt),
15ea31fa 8672 SEC_DEF("tc", SCHED_CLS, 0, SEC_NONE),
450b167f
AN
8673 SEC_DEF("classifier", SCHED_CLS, 0, SEC_NONE),
8674 SEC_DEF("action", SCHED_ACT, 0, SEC_NONE),
9af8efc4
AN
8675 SEC_DEF("tracepoint+", TRACEPOINT, 0, SEC_NONE, attach_tp),
8676 SEC_DEF("tp+", TRACEPOINT, 0, SEC_NONE, attach_tp),
8677 SEC_DEF("raw_tracepoint+", RAW_TRACEPOINT, 0, SEC_NONE, attach_raw_tp),
8678 SEC_DEF("raw_tp+", RAW_TRACEPOINT, 0, SEC_NONE, attach_raw_tp),
8679 SEC_DEF("raw_tracepoint.w+", RAW_TRACEPOINT_WRITABLE, 0, SEC_NONE, attach_raw_tp),
8680 SEC_DEF("raw_tp.w+", RAW_TRACEPOINT_WRITABLE, 0, SEC_NONE, attach_raw_tp),
cc7d8f2c
AN
8681 SEC_DEF("tp_btf+", TRACING, BPF_TRACE_RAW_TP, SEC_ATTACH_BTF, attach_trace),
8682 SEC_DEF("fentry+", TRACING, BPF_TRACE_FENTRY, SEC_ATTACH_BTF, attach_trace),
8683 SEC_DEF("fmod_ret+", TRACING, BPF_MODIFY_RETURN, SEC_ATTACH_BTF, attach_trace),
8684 SEC_DEF("fexit+", TRACING, BPF_TRACE_FEXIT, SEC_ATTACH_BTF, attach_trace),
8685 SEC_DEF("fentry.s+", TRACING, BPF_TRACE_FENTRY, SEC_ATTACH_BTF | SEC_SLEEPABLE, attach_trace),
8686 SEC_DEF("fmod_ret.s+", TRACING, BPF_MODIFY_RETURN, SEC_ATTACH_BTF | SEC_SLEEPABLE, attach_trace),
8687 SEC_DEF("fexit.s+", TRACING, BPF_TRACE_FEXIT, SEC_ATTACH_BTF | SEC_SLEEPABLE, attach_trace),
8688 SEC_DEF("freplace+", EXT, 0, SEC_ATTACH_BTF, attach_trace),
8689 SEC_DEF("lsm+", LSM, BPF_LSM_MAC, SEC_ATTACH_BTF, attach_lsm),
8690 SEC_DEF("lsm.s+", LSM, BPF_LSM_MAC, SEC_ATTACH_BTF | SEC_SLEEPABLE, attach_lsm),
bffcf348 8691 SEC_DEF("lsm_cgroup+", LSM, BPF_LSM_CGROUP, SEC_ATTACH_BTF),
cc7d8f2c
AN
8692 SEC_DEF("iter+", TRACING, BPF_TRACE_ITER, SEC_ATTACH_BTF, attach_iter),
8693 SEC_DEF("iter.s+", TRACING, BPF_TRACE_ITER, SEC_ATTACH_BTF | SEC_SLEEPABLE, attach_iter),
15ea31fa 8694 SEC_DEF("syscall", SYSCALL, 0, SEC_SLEEPABLE),
082c4bfb 8695 SEC_DEF("xdp.frags/devmap", XDP, BPF_XDP_DEVMAP, SEC_XDP_FRAGS),
4a4d4cee 8696 SEC_DEF("xdp/devmap", XDP, BPF_XDP_DEVMAP, SEC_ATTACHABLE),
082c4bfb 8697 SEC_DEF("xdp.frags/cpumap", XDP, BPF_XDP_CPUMAP, SEC_XDP_FRAGS),
4a4d4cee 8698 SEC_DEF("xdp/cpumap", XDP, BPF_XDP_CPUMAP, SEC_ATTACHABLE),
082c4bfb 8699 SEC_DEF("xdp.frags", XDP, BPF_XDP, SEC_XDP_FRAGS),
450b167f
AN
8700 SEC_DEF("xdp", XDP, BPF_XDP, SEC_ATTACHABLE_OPT),
8701 SEC_DEF("perf_event", PERF_EVENT, 0, SEC_NONE),
8702 SEC_DEF("lwt_in", LWT_IN, 0, SEC_NONE),
8703 SEC_DEF("lwt_out", LWT_OUT, 0, SEC_NONE),
8704 SEC_DEF("lwt_xmit", LWT_XMIT, 0, SEC_NONE),
8705 SEC_DEF("lwt_seg6local", LWT_SEG6LOCAL, 0, SEC_NONE),
8706 SEC_DEF("sockops", SOCK_OPS, BPF_CGROUP_SOCK_OPS, SEC_ATTACHABLE_OPT),
8707 SEC_DEF("sk_skb/stream_parser", SK_SKB, BPF_SK_SKB_STREAM_PARSER, SEC_ATTACHABLE_OPT),
8708 SEC_DEF("sk_skb/stream_verdict",SK_SKB, BPF_SK_SKB_STREAM_VERDICT, SEC_ATTACHABLE_OPT),
8709 SEC_DEF("sk_skb", SK_SKB, 0, SEC_NONE),
8710 SEC_DEF("sk_msg", SK_MSG, BPF_SK_MSG_VERDICT, SEC_ATTACHABLE_OPT),
8711 SEC_DEF("lirc_mode2", LIRC_MODE2, BPF_LIRC_MODE2, SEC_ATTACHABLE_OPT),
8712 SEC_DEF("flow_dissector", FLOW_DISSECTOR, BPF_FLOW_DISSECTOR, SEC_ATTACHABLE_OPT),
8713 SEC_DEF("cgroup_skb/ingress", CGROUP_SKB, BPF_CGROUP_INET_INGRESS, SEC_ATTACHABLE_OPT),
8714 SEC_DEF("cgroup_skb/egress", CGROUP_SKB, BPF_CGROUP_INET_EGRESS, SEC_ATTACHABLE_OPT),
8715 SEC_DEF("cgroup/skb", CGROUP_SKB, 0, SEC_NONE),
8716 SEC_DEF("cgroup/sock_create", CGROUP_SOCK, BPF_CGROUP_INET_SOCK_CREATE, SEC_ATTACHABLE),
8717 SEC_DEF("cgroup/sock_release", CGROUP_SOCK, BPF_CGROUP_INET_SOCK_RELEASE, SEC_ATTACHABLE),
8718 SEC_DEF("cgroup/sock", CGROUP_SOCK, BPF_CGROUP_INET_SOCK_CREATE, SEC_ATTACHABLE_OPT),
8719 SEC_DEF("cgroup/post_bind4", CGROUP_SOCK, BPF_CGROUP_INET4_POST_BIND, SEC_ATTACHABLE),
8720 SEC_DEF("cgroup/post_bind6", CGROUP_SOCK, BPF_CGROUP_INET6_POST_BIND, SEC_ATTACHABLE),
8721 SEC_DEF("cgroup/bind4", CGROUP_SOCK_ADDR, BPF_CGROUP_INET4_BIND, SEC_ATTACHABLE),
8722 SEC_DEF("cgroup/bind6", CGROUP_SOCK_ADDR, BPF_CGROUP_INET6_BIND, SEC_ATTACHABLE),
8723 SEC_DEF("cgroup/connect4", CGROUP_SOCK_ADDR, BPF_CGROUP_INET4_CONNECT, SEC_ATTACHABLE),
8724 SEC_DEF("cgroup/connect6", CGROUP_SOCK_ADDR, BPF_CGROUP_INET6_CONNECT, SEC_ATTACHABLE),
8725 SEC_DEF("cgroup/sendmsg4", CGROUP_SOCK_ADDR, BPF_CGROUP_UDP4_SENDMSG, SEC_ATTACHABLE),
8726 SEC_DEF("cgroup/sendmsg6", CGROUP_SOCK_ADDR, BPF_CGROUP_UDP6_SENDMSG, SEC_ATTACHABLE),
8727 SEC_DEF("cgroup/recvmsg4", CGROUP_SOCK_ADDR, BPF_CGROUP_UDP4_RECVMSG, SEC_ATTACHABLE),
8728 SEC_DEF("cgroup/recvmsg6", CGROUP_SOCK_ADDR, BPF_CGROUP_UDP6_RECVMSG, SEC_ATTACHABLE),
8729 SEC_DEF("cgroup/getpeername4", CGROUP_SOCK_ADDR, BPF_CGROUP_INET4_GETPEERNAME, SEC_ATTACHABLE),
8730 SEC_DEF("cgroup/getpeername6", CGROUP_SOCK_ADDR, BPF_CGROUP_INET6_GETPEERNAME, SEC_ATTACHABLE),
8731 SEC_DEF("cgroup/getsockname4", CGROUP_SOCK_ADDR, BPF_CGROUP_INET4_GETSOCKNAME, SEC_ATTACHABLE),
8732 SEC_DEF("cgroup/getsockname6", CGROUP_SOCK_ADDR, BPF_CGROUP_INET6_GETSOCKNAME, SEC_ATTACHABLE),
8733 SEC_DEF("cgroup/sysctl", CGROUP_SYSCTL, BPF_CGROUP_SYSCTL, SEC_ATTACHABLE),
8734 SEC_DEF("cgroup/getsockopt", CGROUP_SOCKOPT, BPF_CGROUP_GETSOCKOPT, SEC_ATTACHABLE),
8735 SEC_DEF("cgroup/setsockopt", CGROUP_SOCKOPT, BPF_CGROUP_SETSOCKOPT, SEC_ATTACHABLE),
8736 SEC_DEF("cgroup/dev", CGROUP_DEVICE, BPF_CGROUP_DEVICE, SEC_ATTACHABLE_OPT),
dd94d45c 8737 SEC_DEF("struct_ops+", STRUCT_OPS, 0, SEC_NONE),
913b2255 8738 SEC_DEF("struct_ops.s+", STRUCT_OPS, 0, SEC_SLEEPABLE),
450b167f 8739 SEC_DEF("sk_lookup", SK_LOOKUP, BPF_SK_LOOKUP, SEC_ATTACHABLE),
d0fe92fb 8740 SEC_DEF("netfilter", NETFILTER, 0, SEC_NONE),
583c9009 8741};
d7be143b 8742
697f104d
AN
8743static size_t custom_sec_def_cnt;
8744static struct bpf_sec_def *custom_sec_defs;
8745static struct bpf_sec_def custom_fallback_def;
8746static bool has_custom_fallback_def;
c76e4c22 8747
697f104d
AN
8748static int last_custom_sec_def_handler_id;
8749
8750int libbpf_register_prog_handler(const char *sec,
8751 enum bpf_prog_type prog_type,
8752 enum bpf_attach_type exp_attach_type,
8753 const struct libbpf_prog_handler_opts *opts)
d7a18ea7 8754{
697f104d 8755 struct bpf_sec_def *sec_def;
d7a18ea7 8756
697f104d
AN
8757 if (!OPTS_VALID(opts, libbpf_prog_handler_opts))
8758 return libbpf_err(-EINVAL);
dd94d45c 8759
697f104d
AN
8760 if (last_custom_sec_def_handler_id == INT_MAX) /* prevent overflow */
8761 return libbpf_err(-E2BIG);
dd94d45c 8762
697f104d
AN
8763 if (sec) {
8764 sec_def = libbpf_reallocarray(custom_sec_defs, custom_sec_def_cnt + 1,
8765 sizeof(*sec_def));
8766 if (!sec_def)
8767 return libbpf_err(-ENOMEM);
dd94d45c 8768
697f104d
AN
8769 custom_sec_defs = sec_def;
8770 sec_def = &custom_sec_defs[custom_sec_def_cnt];
8771 } else {
8772 if (has_custom_fallback_def)
8773 return libbpf_err(-EBUSY);
dd94d45c 8774
697f104d
AN
8775 sec_def = &custom_fallback_def;
8776 }
8777
8778 sec_def->sec = sec ? strdup(sec) : NULL;
8779 if (sec && !sec_def->sec)
8780 return libbpf_err(-ENOMEM);
8781
8782 sec_def->prog_type = prog_type;
8783 sec_def->expected_attach_type = exp_attach_type;
8784 sec_def->cookie = OPTS_GET(opts, cookie, 0);
8785
8786 sec_def->prog_setup_fn = OPTS_GET(opts, prog_setup_fn, NULL);
8787 sec_def->prog_prepare_load_fn = OPTS_GET(opts, prog_prepare_load_fn, NULL);
8788 sec_def->prog_attach_fn = OPTS_GET(opts, prog_attach_fn, NULL);
8789
8790 sec_def->handler_id = ++last_custom_sec_def_handler_id;
8791
8792 if (sec)
8793 custom_sec_def_cnt++;
8794 else
8795 has_custom_fallback_def = true;
8796
8797 return sec_def->handler_id;
8798}
8799
8800int libbpf_unregister_prog_handler(int handler_id)
8801{
8802 struct bpf_sec_def *sec_defs;
8803 int i;
8804
8805 if (handler_id <= 0)
8806 return libbpf_err(-EINVAL);
8807
8808 if (has_custom_fallback_def && custom_fallback_def.handler_id == handler_id) {
8809 memset(&custom_fallback_def, 0, sizeof(custom_fallback_def));
8810 has_custom_fallback_def = false;
8811 return 0;
8812 }
8813
8814 for (i = 0; i < custom_sec_def_cnt; i++) {
8815 if (custom_sec_defs[i].handler_id == handler_id)
8816 break;
8817 }
8818
8819 if (i == custom_sec_def_cnt)
8820 return libbpf_err(-ENOENT);
8821
8822 free(custom_sec_defs[i].sec);
8823 for (i = i + 1; i < custom_sec_def_cnt; i++)
8824 custom_sec_defs[i - 1] = custom_sec_defs[i];
8825 custom_sec_def_cnt--;
8826
8827 /* try to shrink the array, but it's ok if we couldn't */
8828 sec_defs = libbpf_reallocarray(custom_sec_defs, custom_sec_def_cnt, sizeof(*sec_defs));
8829 if (sec_defs)
8830 custom_sec_defs = sec_defs;
8831
8832 return 0;
8833}
8834
450b167f 8835static bool sec_def_matches(const struct bpf_sec_def *sec_def, const char *sec_name)
697f104d
AN
8836{
8837 size_t len = strlen(sec_def->sec);
8838
8839 /* "type/" always has to have proper SEC("type/extras") form */
8840 if (sec_def->sec[len - 1] == '/') {
8841 if (str_has_pfx(sec_name, sec_def->sec))
8842 return true;
8843 return false;
8844 }
8845
8846 /* "type+" means it can be either exact SEC("type") or
8847 * well-formed SEC("type/extras") with proper '/' separator
8848 */
8849 if (sec_def->sec[len - 1] == '+') {
8850 len--;
8851 /* not even a prefix */
8852 if (strncmp(sec_name, sec_def->sec, len) != 0)
8853 return false;
8854 /* exact match or has '/' separator */
8855 if (sec_name[len] == '\0' || sec_name[len] == '/')
8856 return true;
8857 return false;
8858 }
8859
697f104d
AN
8860 return strcmp(sec_name, sec_def->sec) == 0;
8861}
8862
8863static const struct bpf_sec_def *find_sec_def(const char *sec_name)
8864{
8865 const struct bpf_sec_def *sec_def;
8866 int i, n;
697f104d
AN
8867
8868 n = custom_sec_def_cnt;
8869 for (i = 0; i < n; i++) {
8870 sec_def = &custom_sec_defs[i];
450b167f 8871 if (sec_def_matches(sec_def, sec_name))
697f104d
AN
8872 return sec_def;
8873 }
8874
8875 n = ARRAY_SIZE(section_defs);
8876 for (i = 0; i < n; i++) {
8877 sec_def = &section_defs[i];
450b167f 8878 if (sec_def_matches(sec_def, sec_name))
dd94d45c 8879 return sec_def;
d7a18ea7 8880 }
697f104d
AN
8881
8882 if (has_custom_fallback_def)
8883 return &custom_fallback_def;
8884
d7a18ea7
AN
8885 return NULL;
8886}
8887
697f104d
AN
8888#define MAX_TYPE_NAME_SIZE 32
8889
c76e4c22
TS
8890static char *libbpf_get_type_names(bool attach_type)
8891{
d7a18ea7 8892 int i, len = ARRAY_SIZE(section_defs) * MAX_TYPE_NAME_SIZE;
c76e4c22
TS
8893 char *buf;
8894
8895 buf = malloc(len);
8896 if (!buf)
8897 return NULL;
8898
8899 buf[0] = '\0';
8900 /* Forge string buf with all available names */
d7a18ea7 8901 for (i = 0; i < ARRAY_SIZE(section_defs); i++) {
15ea31fa
AN
8902 const struct bpf_sec_def *sec_def = &section_defs[i];
8903
8904 if (attach_type) {
4fa5bcfe 8905 if (sec_def->prog_prepare_load_fn != libbpf_prepare_prog_load)
15ea31fa
AN
8906 continue;
8907
8908 if (!(sec_def->cookie & SEC_ATTACHABLE))
8909 continue;
8910 }
c76e4c22 8911
d7a18ea7 8912 if (strlen(buf) + strlen(section_defs[i].sec) + 2 > len) {
c76e4c22
TS
8913 free(buf);
8914 return NULL;
8915 }
8916 strcat(buf, " ");
d7a18ea7 8917 strcat(buf, section_defs[i].sec);
c76e4c22
TS
8918 }
8919
8920 return buf;
8921}
8922
b60df2a0
JK
8923int libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,
8924 enum bpf_attach_type *expected_attach_type)
583c9009 8925{
d7a18ea7 8926 const struct bpf_sec_def *sec_def;
c76e4c22 8927 char *type_names;
583c9009 8928
b60df2a0 8929 if (!name)
e9fc3ce9 8930 return libbpf_err(-EINVAL);
583c9009 8931
d7a18ea7
AN
8932 sec_def = find_sec_def(name);
8933 if (sec_def) {
8934 *prog_type = sec_def->prog_type;
8935 *expected_attach_type = sec_def->expected_attach_type;
b60df2a0
JK
8936 return 0;
8937 }
d7a18ea7 8938
4a3d6c6a 8939 pr_debug("failed to guess program type from ELF section '%s'\n", name);
c76e4c22
TS
8940 type_names = libbpf_get_type_names(false);
8941 if (type_names != NULL) {
3f519353 8942 pr_debug("supported section(type) names are:%s\n", type_names);
c76e4c22
TS
8943 free(type_names);
8944 }
8945
e9fc3ce9 8946 return libbpf_err(-ESRCH);
b60df2a0 8947}
583c9009 8948
ccde5760
DM
8949const char *libbpf_bpf_attach_type_str(enum bpf_attach_type t)
8950{
8951 if (t < 0 || t >= ARRAY_SIZE(attach_type_name))
8952 return NULL;
8953
8954 return attach_type_name[t];
8955}
8956
ba5d1b58
DM
8957const char *libbpf_bpf_link_type_str(enum bpf_link_type t)
8958{
8959 if (t < 0 || t >= ARRAY_SIZE(link_type_name))
8960 return NULL;
8961
8962 return link_type_name[t];
8963}
8964
3e6dc020
DM
8965const char *libbpf_bpf_map_type_str(enum bpf_map_type t)
8966{
8967 if (t < 0 || t >= ARRAY_SIZE(map_type_name))
8968 return NULL;
8969
8970 return map_type_name[t];
8971}
8972
d18616e7
DM
8973const char *libbpf_bpf_prog_type_str(enum bpf_prog_type t)
8974{
8975 if (t < 0 || t >= ARRAY_SIZE(prog_type_name))
8976 return NULL;
8977
8978 return prog_type_name[t];
8979}
8980
590a0088 8981static struct bpf_map *find_struct_ops_map_by_offset(struct bpf_object *obj,
809a69d6 8982 int sec_idx,
590a0088
MKL
8983 size_t offset)
8984{
8985 struct bpf_map *map;
8986 size_t i;
8987
8988 for (i = 0; i < obj->nr_maps; i++) {
8989 map = &obj->maps[i];
8990 if (!bpf_map__is_struct_ops(map))
8991 continue;
809a69d6
KFL
8992 if (map->sec_idx == sec_idx &&
8993 map->sec_offset <= offset &&
590a0088
MKL
8994 offset - map->sec_offset < map->def.value_size)
8995 return map;
8996 }
8997
8998 return NULL;
8999}
9000
9001/* Collect the reloc from ELF and populate the st_ops->progs[] */
646f02ff 9002static int bpf_object__collect_st_ops_relos(struct bpf_object *obj,
ad23b723 9003 Elf64_Shdr *shdr, Elf_Data *data)
590a0088
MKL
9004{
9005 const struct btf_member *member;
9006 struct bpf_struct_ops *st_ops;
9007 struct bpf_program *prog;
9008 unsigned int shdr_idx;
9009 const struct btf *btf;
9010 struct bpf_map *map;
7e06aad5 9011 unsigned int moff, insn_idx;
590a0088 9012 const char *name;
1d1a3bcf 9013 __u32 member_idx;
ad23b723
AN
9014 Elf64_Sym *sym;
9015 Elf64_Rel *rel;
590a0088
MKL
9016 int i, nrels;
9017
590a0088
MKL
9018 btf = obj->btf;
9019 nrels = shdr->sh_size / shdr->sh_entsize;
9020 for (i = 0; i < nrels; i++) {
ad23b723
AN
9021 rel = elf_rel_by_idx(data, i);
9022 if (!rel) {
590a0088
MKL
9023 pr_warn("struct_ops reloc: failed to get %d reloc\n", i);
9024 return -LIBBPF_ERRNO__FORMAT;
9025 }
9026
ad23b723
AN
9027 sym = elf_sym_by_idx(obj, ELF64_R_SYM(rel->r_info));
9028 if (!sym) {
590a0088 9029 pr_warn("struct_ops reloc: symbol %zx not found\n",
ad23b723 9030 (size_t)ELF64_R_SYM(rel->r_info));
590a0088
MKL
9031 return -LIBBPF_ERRNO__FORMAT;
9032 }
9033
ad23b723 9034 name = elf_sym_str(obj, sym->st_name) ?: "<?>";
809a69d6 9035 map = find_struct_ops_map_by_offset(obj, shdr->sh_info, rel->r_offset);
590a0088 9036 if (!map) {
ad23b723
AN
9037 pr_warn("struct_ops reloc: cannot find map at rel->r_offset %zu\n",
9038 (size_t)rel->r_offset);
590a0088
MKL
9039 return -EINVAL;
9040 }
9041
ad23b723
AN
9042 moff = rel->r_offset - map->sec_offset;
9043 shdr_idx = sym->st_shndx;
590a0088 9044 st_ops = map->st_ops;
ad23b723 9045 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 9046 map->name,
ad23b723
AN
9047 (long long)(rel->r_info >> 32),
9048 (long long)sym->st_value,
9049 shdr_idx, (size_t)rel->r_offset,
9050 map->sec_offset, sym->st_name, name);
590a0088
MKL
9051
9052 if (shdr_idx >= SHN_LORESERVE) {
ad23b723
AN
9053 pr_warn("struct_ops reloc %s: rel->r_offset %zu shdr_idx %u unsupported non-static function\n",
9054 map->name, (size_t)rel->r_offset, shdr_idx);
590a0088
MKL
9055 return -LIBBPF_ERRNO__RELOC;
9056 }
ad23b723 9057 if (sym->st_value % BPF_INSN_SZ) {
7e06aad5 9058 pr_warn("struct_ops reloc %s: invalid target program offset %llu\n",
ad23b723 9059 map->name, (unsigned long long)sym->st_value);
7e06aad5
AN
9060 return -LIBBPF_ERRNO__FORMAT;
9061 }
ad23b723 9062 insn_idx = sym->st_value / BPF_INSN_SZ;
590a0088
MKL
9063
9064 member = find_member_by_offset(st_ops->type, moff * 8);
9065 if (!member) {
9066 pr_warn("struct_ops reloc %s: cannot find member at moff %u\n",
9067 map->name, moff);
9068 return -EINVAL;
9069 }
9070 member_idx = member - btf_members(st_ops->type);
9071 name = btf__name_by_offset(btf, member->name_off);
9072
9073 if (!resolve_func_ptr(btf, member->type, NULL)) {
9074 pr_warn("struct_ops reloc %s: cannot relocate non func ptr %s\n",
9075 map->name, name);
9076 return -EINVAL;
9077 }
9078
7e06aad5 9079 prog = find_prog_by_sec_insn(obj, shdr_idx, insn_idx);
590a0088
MKL
9080 if (!prog) {
9081 pr_warn("struct_ops reloc %s: cannot find prog at shdr_idx %u to relocate func ptr %s\n",
9082 map->name, shdr_idx, name);
9083 return -EINVAL;
9084 }
9085
91b4d1d1
AN
9086 /* prevent the use of BPF prog with invalid type */
9087 if (prog->type != BPF_PROG_TYPE_STRUCT_OPS) {
9088 pr_warn("struct_ops reloc %s: prog %s is not struct_ops BPF program\n",
9089 map->name, prog->name);
9090 return -EINVAL;
9091 }
590a0088 9092
91b4d1d1
AN
9093 /* if we haven't yet processed this BPF program, record proper
9094 * attach_btf_id and member_idx
9095 */
9096 if (!prog->attach_btf_id) {
590a0088
MKL
9097 prog->attach_btf_id = st_ops->type_id;
9098 prog->expected_attach_type = member_idx;
590a0088 9099 }
91b4d1d1
AN
9100
9101 /* struct_ops BPF prog can be re-used between multiple
809a69d6
KFL
9102 * .struct_ops & .struct_ops.link as long as it's the
9103 * same struct_ops struct definition and the same
9104 * function pointer field
91b4d1d1
AN
9105 */
9106 if (prog->attach_btf_id != st_ops->type_id ||
9107 prog->expected_attach_type != member_idx) {
9108 pr_warn("struct_ops reloc %s: cannot use prog %s in sec %s with type %u attach_btf_id %u expected_attach_type %u for func ptr %s\n",
9109 map->name, prog->name, prog->sec_name, prog->type,
9110 prog->attach_btf_id, prog->expected_attach_type, name);
9111 return -EINVAL;
9112 }
9113
590a0088
MKL
9114 st_ops->progs[member_idx] = prog;
9115 }
9116
9117 return 0;
590a0088
MKL
9118}
9119
a6ed02ca 9120#define BTF_TRACE_PREFIX "btf_trace_"
1e092a03 9121#define BTF_LSM_PREFIX "bpf_lsm_"
21aef70e 9122#define BTF_ITER_PREFIX "bpf_iter_"
a6ed02ca
KS
9123#define BTF_MAX_NAME_SIZE 128
9124
67234743
AS
9125void btf_get_kernel_prefix_kind(enum bpf_attach_type attach_type,
9126 const char **prefix, int *kind)
9127{
9128 switch (attach_type) {
9129 case BPF_TRACE_RAW_TP:
9130 *prefix = BTF_TRACE_PREFIX;
9131 *kind = BTF_KIND_TYPEDEF;
9132 break;
9133 case BPF_LSM_MAC:
bffcf348 9134 case BPF_LSM_CGROUP:
67234743
AS
9135 *prefix = BTF_LSM_PREFIX;
9136 *kind = BTF_KIND_FUNC;
9137 break;
9138 case BPF_TRACE_ITER:
9139 *prefix = BTF_ITER_PREFIX;
9140 *kind = BTF_KIND_FUNC;
9141 break;
9142 default:
9143 *prefix = "";
9144 *kind = BTF_KIND_FUNC;
9145 }
9146}
9147
a6ed02ca
KS
9148static int find_btf_by_prefix_kind(const struct btf *btf, const char *prefix,
9149 const char *name, __u32 kind)
9150{
9151 char btf_type_name[BTF_MAX_NAME_SIZE];
9152 int ret;
9153
9154 ret = snprintf(btf_type_name, sizeof(btf_type_name),
9155 "%s%s", prefix, name);
9156 /* snprintf returns the number of characters written excluding the
c139e40a 9157 * terminating null. So, if >= BTF_MAX_NAME_SIZE are written, it
a6ed02ca
KS
9158 * indicates truncation.
9159 */
9160 if (ret < 0 || ret >= sizeof(btf_type_name))
9161 return -ENAMETOOLONG;
9162 return btf__find_by_name_kind(btf, btf_type_name, kind);
9163}
9164
91abb4a6
AN
9165static inline int find_attach_btf_id(struct btf *btf, const char *name,
9166 enum bpf_attach_type attach_type)
a6ed02ca 9167{
67234743
AS
9168 const char *prefix;
9169 int kind;
a6ed02ca 9170
67234743
AS
9171 btf_get_kernel_prefix_kind(attach_type, &prefix, &kind);
9172 return find_btf_by_prefix_kind(btf, prefix, name, kind);
a6ed02ca
KS
9173}
9174
b8c54ea4
AS
9175int libbpf_find_vmlinux_btf_id(const char *name,
9176 enum bpf_attach_type attach_type)
12a8654b 9177{
a6ed02ca 9178 struct btf *btf;
3521ffa2 9179 int err;
12a8654b 9180
a710eed3 9181 btf = btf__load_vmlinux_btf();
e9fc3ce9
AN
9182 err = libbpf_get_error(btf);
9183 if (err) {
12a8654b 9184 pr_warn("vmlinux BTF is not found\n");
e9fc3ce9 9185 return libbpf_err(err);
12a8654b
AS
9186 }
9187
91abb4a6
AN
9188 err = find_attach_btf_id(btf, name, attach_type);
9189 if (err <= 0)
9190 pr_warn("%s is not found in vmlinux BTF\n", name);
9191
3521ffa2 9192 btf__free(btf);
e9fc3ce9 9193 return libbpf_err(err);
b8c54ea4
AS
9194}
9195
e7bf94db
AS
9196static int libbpf_find_prog_btf_id(const char *name, __u32 attach_prog_fd)
9197{
813847a3 9198 struct bpf_prog_info info;
ebc7b50a 9199 __u32 info_len = sizeof(info);
6cc93e2f 9200 struct btf *btf;
6d2d73cd 9201 int err;
e7bf94db 9202
813847a3 9203 memset(&info, 0, info_len);
629dfc66 9204 err = bpf_prog_get_info_by_fd(attach_prog_fd, &info, &info_len);
e9fc3ce9 9205 if (err) {
629dfc66 9206 pr_warn("failed bpf_prog_get_info_by_fd for FD %d: %d\n",
ebc7b50a 9207 attach_prog_fd, err);
e9fc3ce9 9208 return err;
e7bf94db 9209 }
6d2d73cd
QM
9210
9211 err = -EINVAL;
ebc7b50a 9212 if (!info.btf_id) {
e7bf94db
AS
9213 pr_warn("The target program doesn't have BTF\n");
9214 goto out;
9215 }
ebc7b50a
DM
9216 btf = btf__load_from_kernel_by_id(info.btf_id);
9217 err = libbpf_get_error(btf);
9218 if (err) {
9219 pr_warn("Failed to get BTF %d of the program: %d\n", info.btf_id, err);
e7bf94db
AS
9220 goto out;
9221 }
9222 err = btf__find_by_name_kind(btf, name, BTF_KIND_FUNC);
9223 btf__free(btf);
9224 if (err <= 0) {
9225 pr_warn("%s is not found in prog's BTF\n", name);
9226 goto out;
9227 }
9228out:
e7bf94db
AS
9229 return err;
9230}
9231
91abb4a6
AN
9232static int find_kernel_btf_id(struct bpf_object *obj, const char *attach_name,
9233 enum bpf_attach_type attach_type,
9234 int *btf_obj_fd, int *btf_type_id)
9235{
9236 int ret, i;
9237
9238 ret = find_attach_btf_id(obj->btf_vmlinux, attach_name, attach_type);
9239 if (ret > 0) {
9240 *btf_obj_fd = 0; /* vmlinux BTF */
9241 *btf_type_id = ret;
9242 return 0;
9243 }
9244 if (ret != -ENOENT)
9245 return ret;
9246
9247 ret = load_module_btfs(obj);
9248 if (ret)
9249 return ret;
9250
9251 for (i = 0; i < obj->btf_module_cnt; i++) {
9252 const struct module_btf *mod = &obj->btf_modules[i];
9253
9254 ret = find_attach_btf_id(mod->btf, attach_name, attach_type);
9255 if (ret > 0) {
9256 *btf_obj_fd = mod->fd;
9257 *btf_type_id = ret;
9258 return 0;
9259 }
9260 if (ret == -ENOENT)
9261 continue;
9262
9263 return ret;
9264 }
9265
9266 return -ESRCH;
9267}
9268
15ea31fa
AN
9269static int libbpf_find_attach_btf_id(struct bpf_program *prog, const char *attach_name,
9270 int *btf_obj_fd, int *btf_type_id)
b8c54ea4 9271{
a6ed02ca
KS
9272 enum bpf_attach_type attach_type = prog->expected_attach_type;
9273 __u32 attach_prog_fd = prog->attach_prog_fd;
b6291a6f 9274 int err = 0;
b8c54ea4 9275
91abb4a6 9276 /* BPF program's BTF ID */
749c202c
AN
9277 if (prog->type == BPF_PROG_TYPE_EXT || attach_prog_fd) {
9278 if (!attach_prog_fd) {
9279 pr_warn("prog '%s': attach program FD is not set\n", prog->name);
9280 return -EINVAL;
9281 }
91abb4a6
AN
9282 err = libbpf_find_prog_btf_id(attach_name, attach_prog_fd);
9283 if (err < 0) {
749c202c
AN
9284 pr_warn("prog '%s': failed to find BPF program (FD %d) BTF ID for '%s': %d\n",
9285 prog->name, attach_prog_fd, attach_name, err);
91abb4a6
AN
9286 return err;
9287 }
9288 *btf_obj_fd = 0;
9289 *btf_type_id = err;
9290 return 0;
9291 }
9292
9293 /* kernel/module BTF ID */
67234743
AS
9294 if (prog->obj->gen_loader) {
9295 bpf_gen__record_attach_target(prog->obj->gen_loader, attach_name, attach_type);
9296 *btf_obj_fd = 0;
9297 *btf_type_id = 1;
9298 } else {
9299 err = find_kernel_btf_id(prog->obj, attach_name, attach_type, btf_obj_fd, btf_type_id);
9300 }
91abb4a6 9301 if (err) {
749c202c
AN
9302 pr_warn("prog '%s': failed to find kernel BTF type ID of '%s': %d\n",
9303 prog->name, attach_name, err);
b8c54ea4 9304 return err;
12a8654b 9305 }
91abb4a6 9306 return 0;
12a8654b
AS
9307}
9308
956b620f
AI
9309int libbpf_attach_type_by_name(const char *name,
9310 enum bpf_attach_type *attach_type)
9311{
c76e4c22 9312 char *type_names;
b6291a6f 9313 const struct bpf_sec_def *sec_def;
956b620f
AI
9314
9315 if (!name)
e9fc3ce9 9316 return libbpf_err(-EINVAL);
956b620f 9317
b6291a6f
AN
9318 sec_def = find_sec_def(name);
9319 if (!sec_def) {
9320 pr_debug("failed to guess attach type based on ELF section name '%s'\n", name);
9321 type_names = libbpf_get_type_names(true);
9322 if (type_names != NULL) {
9323 pr_debug("attachable section(type) names are:%s\n", type_names);
9324 free(type_names);
9325 }
9326
9327 return libbpf_err(-EINVAL);
c76e4c22
TS
9328 }
9329
4fa5bcfe 9330 if (sec_def->prog_prepare_load_fn != libbpf_prepare_prog_load)
15ea31fa
AN
9331 return libbpf_err(-EINVAL);
9332 if (!(sec_def->cookie & SEC_ATTACHABLE))
b6291a6f
AN
9333 return libbpf_err(-EINVAL);
9334
9335 *attach_type = sec_def->expected_attach_type;
9336 return 0;
956b620f
AI
9337}
9338
a324aae3 9339int bpf_map__fd(const struct bpf_map *map)
9d759a9b 9340{
e9fc3ce9 9341 return map ? map->fd : libbpf_err(-EINVAL);
9d759a9b
WN
9342}
9343
aed65917
AN
9344static bool map_uses_real_name(const struct bpf_map *map)
9345{
9346 /* Since libbpf started to support custom .data.* and .rodata.* maps,
9347 * their user-visible name differs from kernel-visible name. Users see
9348 * such map's corresponding ELF section name as a map name.
9349 * This check distinguishes .data/.rodata from .data.* and .rodata.*
9350 * maps to know which name has to be returned to the user.
9351 */
9352 if (map->libbpf_type == LIBBPF_MAP_DATA && strcmp(map->real_name, DATA_SEC) != 0)
9353 return true;
9354 if (map->libbpf_type == LIBBPF_MAP_RODATA && strcmp(map->real_name, RODATA_SEC) != 0)
9355 return true;
9356 return false;
9357}
9358
a324aae3 9359const char *bpf_map__name(const struct bpf_map *map)
561bbcca 9360{
aed65917
AN
9361 if (!map)
9362 return NULL;
9363
9364 if (map_uses_real_name(map))
9365 return map->real_name;
9366
9367 return map->name;
561bbcca
WN
9368}
9369
1bdb6c9a
AN
9370enum bpf_map_type bpf_map__type(const struct bpf_map *map)
9371{
9372 return map->def.type;
9373}
9374
9375int bpf_map__set_type(struct bpf_map *map, enum bpf_map_type type)
9376{
9377 if (map->fd >= 0)
e9fc3ce9 9378 return libbpf_err(-EBUSY);
1bdb6c9a
AN
9379 map->def.type = type;
9380 return 0;
9381}
9382
9383__u32 bpf_map__map_flags(const struct bpf_map *map)
9384{
9385 return map->def.map_flags;
9386}
9387
9388int bpf_map__set_map_flags(struct bpf_map *map, __u32 flags)
9389{
9390 if (map->fd >= 0)
e9fc3ce9 9391 return libbpf_err(-EBUSY);
1bdb6c9a
AN
9392 map->def.map_flags = flags;
9393 return 0;
9394}
9395
47512102
JK
9396__u64 bpf_map__map_extra(const struct bpf_map *map)
9397{
9398 return map->map_extra;
9399}
9400
9401int bpf_map__set_map_extra(struct bpf_map *map, __u64 map_extra)
9402{
9403 if (map->fd >= 0)
9404 return libbpf_err(-EBUSY);
9405 map->map_extra = map_extra;
9406 return 0;
9407}
9408
1bdb6c9a
AN
9409__u32 bpf_map__numa_node(const struct bpf_map *map)
9410{
9411 return map->numa_node;
9412}
9413
9414int bpf_map__set_numa_node(struct bpf_map *map, __u32 numa_node)
9415{
9416 if (map->fd >= 0)
e9fc3ce9 9417 return libbpf_err(-EBUSY);
1bdb6c9a
AN
9418 map->numa_node = numa_node;
9419 return 0;
9420}
9421
9422__u32 bpf_map__key_size(const struct bpf_map *map)
9423{
9424 return map->def.key_size;
9425}
9426
9427int bpf_map__set_key_size(struct bpf_map *map, __u32 size)
9428{
9429 if (map->fd >= 0)
e9fc3ce9 9430 return libbpf_err(-EBUSY);
1bdb6c9a
AN
9431 map->def.key_size = size;
9432 return 0;
9433}
9434
9435__u32 bpf_map__value_size(const struct bpf_map *map)
9436{
9437 return map->def.value_size;
9438}
9439
9d0a2331
JK
9440static int map_btf_datasec_resize(struct bpf_map *map, __u32 size)
9441{
9442 struct btf *btf;
9443 struct btf_type *datasec_type, *var_type;
9444 struct btf_var_secinfo *var;
9445 const struct btf_type *array_type;
9446 const struct btf_array *array;
4c857a71
JK
9447 int vlen, element_sz, new_array_id;
9448 __u32 nr_elements;
9d0a2331
JK
9449
9450 /* check btf existence */
9451 btf = bpf_object__btf(map->obj);
9452 if (!btf)
9453 return -ENOENT;
9454
9455 /* verify map is datasec */
9456 datasec_type = btf_type_by_id(btf, bpf_map__btf_value_type_id(map));
9457 if (!btf_is_datasec(datasec_type)) {
9458 pr_warn("map '%s': cannot be resized, map value type is not a datasec\n",
9459 bpf_map__name(map));
9460 return -EINVAL;
9461 }
9462
9463 /* verify datasec has at least one var */
9464 vlen = btf_vlen(datasec_type);
9465 if (vlen == 0) {
9466 pr_warn("map '%s': cannot be resized, map value datasec is empty\n",
9467 bpf_map__name(map));
9468 return -EINVAL;
9469 }
9470
9471 /* verify last var in the datasec is an array */
9472 var = &btf_var_secinfos(datasec_type)[vlen - 1];
9473 var_type = btf_type_by_id(btf, var->type);
9474 array_type = skip_mods_and_typedefs(btf, var_type->type, NULL);
9475 if (!btf_is_array(array_type)) {
9476 pr_warn("map '%s': cannot be resized, last var must be an array\n",
9477 bpf_map__name(map));
9478 return -EINVAL;
9479 }
9480
9481 /* verify request size aligns with array */
9482 array = btf_array(array_type);
9483 element_sz = btf__resolve_size(btf, array->type);
9484 if (element_sz <= 0 || (size - var->offset) % element_sz != 0) {
9485 pr_warn("map '%s': cannot be resized, element size (%d) doesn't align with new total size (%u)\n",
9486 bpf_map__name(map), element_sz, size);
9487 return -EINVAL;
9488 }
9489
9490 /* create a new array based on the existing array, but with new length */
9491 nr_elements = (size - var->offset) / element_sz;
9492 new_array_id = btf__add_array(btf, array->index_type, array->type, nr_elements);
9493 if (new_array_id < 0)
9494 return new_array_id;
9495
9496 /* adding a new btf type invalidates existing pointers to btf objects,
9497 * so refresh pointers before proceeding
9498 */
9499 datasec_type = btf_type_by_id(btf, map->btf_value_type_id);
9500 var = &btf_var_secinfos(datasec_type)[vlen - 1];
9501 var_type = btf_type_by_id(btf, var->type);
9502
9503 /* finally update btf info */
9504 datasec_type->size = size;
9505 var->size = size - var->offset;
9506 var_type->type = new_array_id;
9507
9508 return 0;
9509}
9510
1bdb6c9a
AN
9511int bpf_map__set_value_size(struct bpf_map *map, __u32 size)
9512{
9513 if (map->fd >= 0)
e9fc3ce9 9514 return libbpf_err(-EBUSY);
9d0a2331
JK
9515
9516 if (map->mmaped) {
9517 int err;
9518 size_t mmap_old_sz, mmap_new_sz;
9519
9520 mmap_old_sz = bpf_map_mmap_sz(map->def.value_size, map->def.max_entries);
9521 mmap_new_sz = bpf_map_mmap_sz(size, map->def.max_entries);
9522 err = bpf_map_mmap_resize(map, mmap_old_sz, mmap_new_sz);
9523 if (err) {
9524 pr_warn("map '%s': failed to resize memory-mapped region: %d\n",
9525 bpf_map__name(map), err);
9526 return err;
9527 }
9528 err = map_btf_datasec_resize(map, size);
9529 if (err && err != -ENOENT) {
9530 pr_warn("map '%s': failed to adjust resized BTF, clearing BTF key/value info: %d\n",
9531 bpf_map__name(map), err);
9532 map->btf_value_type_id = 0;
9533 map->btf_key_type_id = 0;
9534 }
9535 }
9536
1bdb6c9a
AN
9537 map->def.value_size = size;
9538 return 0;
9539}
9540
5b891af7 9541__u32 bpf_map__btf_key_type_id(const struct bpf_map *map)
8a138aed 9542{
61746dbe 9543 return map ? map->btf_key_type_id : 0;
8a138aed
MKL
9544}
9545
5b891af7 9546__u32 bpf_map__btf_value_type_id(const struct bpf_map *map)
8a138aed 9547{
61746dbe 9548 return map ? map->btf_value_type_id : 0;
8a138aed
MKL
9549}
9550
e2842be5
THJ
9551int bpf_map__set_initial_value(struct bpf_map *map,
9552 const void *data, size_t size)
9553{
9554 if (!map->mmaped || map->libbpf_type == LIBBPF_MAP_KCONFIG ||
9555 size != map->def.value_size || map->fd >= 0)
e9fc3ce9 9556 return libbpf_err(-EINVAL);
e2842be5
THJ
9557
9558 memcpy(map->mmaped, data, size);
9559 return 0;
9560}
9561
9d0a2331 9562void *bpf_map__initial_value(struct bpf_map *map, size_t *psize)
7723256b
AS
9563{
9564 if (!map->mmaped)
9565 return NULL;
9566 *psize = map->def.value_size;
9567 return map->mmaped;
9568}
9569
a324aae3 9570bool bpf_map__is_internal(const struct bpf_map *map)
d859900c
DB
9571{
9572 return map->libbpf_type != LIBBPF_MAP_UNSPEC;
9573}
9574
1bdb6c9a
AN
9575__u32 bpf_map__ifindex(const struct bpf_map *map)
9576{
9577 return map->map_ifindex;
9578}
9579
9580int bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex)
9aba3613 9581{
1bdb6c9a 9582 if (map->fd >= 0)
e9fc3ce9 9583 return libbpf_err(-EBUSY);
9aba3613 9584 map->map_ifindex = ifindex;
1bdb6c9a 9585 return 0;
9aba3613
JK
9586}
9587
addb9fc9
NS
9588int bpf_map__set_inner_map_fd(struct bpf_map *map, int fd)
9589{
9590 if (!bpf_map_type__is_map_in_map(map->def.type)) {
be18010e 9591 pr_warn("error: unsupported map type\n");
e9fc3ce9 9592 return libbpf_err(-EINVAL);
addb9fc9
NS
9593 }
9594 if (map->inner_map_fd != -1) {
be18010e 9595 pr_warn("error: inner_map_fd already specified\n");
e9fc3ce9 9596 return libbpf_err(-EINVAL);
addb9fc9 9597 }
8f7b239e
AN
9598 if (map->inner_map) {
9599 bpf_map__destroy(map->inner_map);
9600 zfree(&map->inner_map);
9601 }
addb9fc9
NS
9602 map->inner_map_fd = fd;
9603 return 0;
9604}
9605
0c19a9fb 9606static struct bpf_map *
a324aae3 9607__bpf_map__iter(const struct bpf_map *m, const struct bpf_object *obj, int i)
9d759a9b 9608{
0c19a9fb 9609 ssize_t idx;
9d759a9b
WN
9610 struct bpf_map *s, *e;
9611
9612 if (!obj || !obj->maps)
e9fc3ce9 9613 return errno = EINVAL, NULL;
9d759a9b
WN
9614
9615 s = obj->maps;
9616 e = obj->maps + obj->nr_maps;
9617
0c19a9fb 9618 if ((m < s) || (m >= e)) {
be18010e
KW
9619 pr_warn("error in %s: map handler doesn't belong to object\n",
9620 __func__);
e9fc3ce9 9621 return errno = EINVAL, NULL;
9d759a9b
WN
9622 }
9623
0c19a9fb
SF
9624 idx = (m - obj->maps) + i;
9625 if (idx >= obj->nr_maps || idx < 0)
9d759a9b
WN
9626 return NULL;
9627 return &obj->maps[idx];
9628}
561bbcca 9629
2088a3a7
HC
9630struct bpf_map *
9631bpf_object__next_map(const struct bpf_object *obj, const struct bpf_map *prev)
0c19a9fb
SF
9632{
9633 if (prev == NULL)
9634 return obj->maps;
9635
9636 return __bpf_map__iter(prev, obj, 1);
9637}
9638
2088a3a7
HC
9639struct bpf_map *
9640bpf_object__prev_map(const struct bpf_object *obj, const struct bpf_map *next)
0c19a9fb
SF
9641{
9642 if (next == NULL) {
9643 if (!obj->nr_maps)
9644 return NULL;
9645 return obj->maps + obj->nr_maps - 1;
9646 }
9647
9648 return __bpf_map__iter(next, obj, -1);
9649}
9650
561bbcca 9651struct bpf_map *
a324aae3 9652bpf_object__find_map_by_name(const struct bpf_object *obj, const char *name)
561bbcca
WN
9653{
9654 struct bpf_map *pos;
9655
f74a53d9 9656 bpf_object__for_each_map(pos, obj) {
26071635
AN
9657 /* if it's a special internal map name (which always starts
9658 * with dot) then check if that special name matches the
9659 * real map name (ELF section name)
9660 */
9661 if (name[0] == '.') {
9662 if (pos->real_name && strcmp(pos->real_name, name) == 0)
9663 return pos;
9664 continue;
9665 }
9666 /* otherwise map name has to be an exact match */
aed65917
AN
9667 if (map_uses_real_name(pos)) {
9668 if (strcmp(pos->real_name, name) == 0)
9669 return pos;
9670 continue;
9671 }
9672 if (strcmp(pos->name, name) == 0)
561bbcca
WN
9673 return pos;
9674 }
e9fc3ce9 9675 return errno = ENOENT, NULL;
561bbcca 9676}
5a6acad1 9677
f3cea32d 9678int
a324aae3 9679bpf_object__find_map_fd_by_name(const struct bpf_object *obj, const char *name)
f3cea32d
MF
9680{
9681 return bpf_map__fd(bpf_object__find_map_by_name(obj, name));
9682}
9683
737d0646
AN
9684static int validate_map_op(const struct bpf_map *map, size_t key_sz,
9685 size_t value_sz, bool check_value_sz)
9686{
9687 if (map->fd <= 0)
9688 return -ENOENT;
9689
9690 if (map->def.key_size != key_sz) {
9691 pr_warn("map '%s': unexpected key size %zu provided, expected %u\n",
9692 map->name, key_sz, map->def.key_size);
9693 return -EINVAL;
9694 }
9695
9696 if (!check_value_sz)
9697 return 0;
9698
9699 switch (map->def.type) {
9700 case BPF_MAP_TYPE_PERCPU_ARRAY:
9701 case BPF_MAP_TYPE_PERCPU_HASH:
9702 case BPF_MAP_TYPE_LRU_PERCPU_HASH:
9703 case BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE: {
9704 int num_cpu = libbpf_num_possible_cpus();
9705 size_t elem_sz = roundup(map->def.value_size, 8);
9706
9707 if (value_sz != num_cpu * elem_sz) {
9708 pr_warn("map '%s': unexpected value size %zu provided for per-CPU map, expected %d * %zu = %zd\n",
9709 map->name, value_sz, num_cpu, elem_sz, num_cpu * elem_sz);
9710 return -EINVAL;
9711 }
9712 break;
9713 }
9714 default:
9715 if (map->def.value_size != value_sz) {
9716 pr_warn("map '%s': unexpected value size %zu provided, expected %u\n",
9717 map->name, value_sz, map->def.value_size);
9718 return -EINVAL;
9719 }
9720 break;
9721 }
9722 return 0;
9723}
9724
9725int bpf_map__lookup_elem(const struct bpf_map *map,
9726 const void *key, size_t key_sz,
9727 void *value, size_t value_sz, __u64 flags)
9728{
9729 int err;
9730
9731 err = validate_map_op(map, key_sz, value_sz, true);
9732 if (err)
9733 return libbpf_err(err);
9734
9735 return bpf_map_lookup_elem_flags(map->fd, key, value, flags);
9736}
9737
9738int bpf_map__update_elem(const struct bpf_map *map,
9739 const void *key, size_t key_sz,
9740 const void *value, size_t value_sz, __u64 flags)
9741{
9742 int err;
9743
9744 err = validate_map_op(map, key_sz, value_sz, true);
9745 if (err)
9746 return libbpf_err(err);
9747
9748 return bpf_map_update_elem(map->fd, key, value, flags);
9749}
9750
9751int bpf_map__delete_elem(const struct bpf_map *map,
9752 const void *key, size_t key_sz, __u64 flags)
9753{
9754 int err;
9755
9756 err = validate_map_op(map, key_sz, 0, false /* check_value_sz */);
9757 if (err)
9758 return libbpf_err(err);
9759
9760 return bpf_map_delete_elem_flags(map->fd, key, flags);
9761}
9762
9763int bpf_map__lookup_and_delete_elem(const struct bpf_map *map,
9764 const void *key, size_t key_sz,
9765 void *value, size_t value_sz, __u64 flags)
9766{
9767 int err;
9768
9769 err = validate_map_op(map, key_sz, value_sz, true);
9770 if (err)
9771 return libbpf_err(err);
9772
9773 return bpf_map_lookup_and_delete_elem_flags(map->fd, key, value, flags);
9774}
9775
9776int bpf_map__get_next_key(const struct bpf_map *map,
9777 const void *cur_key, void *next_key, size_t key_sz)
9778{
9779 int err;
9780
9781 err = validate_map_op(map, key_sz, 0, false /* check_value_sz */);
9782 if (err)
9783 return libbpf_err(err);
9784
9785 return bpf_map_get_next_key(map->fd, cur_key, next_key);
9786}
9787
e28ff1a8
JS
9788long libbpf_get_error(const void *ptr)
9789{
e9fc3ce9
AN
9790 if (!IS_ERR_OR_NULL(ptr))
9791 return 0;
9792
9793 if (IS_ERR(ptr))
9794 errno = -PTR_ERR(ptr);
9795
9796 /* If ptr == NULL, then errno should be already set by the failing
9797 * API, because libbpf never returns NULL on success and it now always
9798 * sets errno on error. So no extra errno handling for ptr == NULL
9799 * case.
9800 */
9801 return -errno;
e28ff1a8 9802}
6f6d33f3 9803
cc4f864b
AN
9804/* Replace link's underlying BPF program with the new one */
9805int bpf_link__update_program(struct bpf_link *link, struct bpf_program *prog)
9806{
e9fc3ce9 9807 int ret;
c139e40a 9808
e9fc3ce9
AN
9809 ret = bpf_link_update(bpf_link__fd(link), bpf_program__fd(prog), NULL);
9810 return libbpf_err_errno(ret);
cc4f864b
AN
9811}
9812
d6958706
AN
9813/* Release "ownership" of underlying BPF resource (typically, BPF program
9814 * attached to some BPF hook, e.g., tracepoint, kprobe, etc). Disconnected
9815 * link, when destructed through bpf_link__destroy() call won't attempt to
9816 * detach/unregisted that BPF resource. This is useful in situations where,
9817 * say, attached BPF program has to outlive userspace program that attached it
9818 * in the system. Depending on type of BPF program, though, there might be
9819 * additional steps (like pinning BPF program in BPF FS) necessary to ensure
9820 * exit of userspace program doesn't trigger automatic detachment and clean up
9821 * inside the kernel.
9822 */
9823void bpf_link__disconnect(struct bpf_link *link)
9824{
9825 link->disconnected = true;
9826}
9827
1c2e9efc
AN
9828int bpf_link__destroy(struct bpf_link *link)
9829{
d6958706 9830 int err = 0;
1c2e9efc 9831
50450fc7 9832 if (IS_ERR_OR_NULL(link))
1c2e9efc
AN
9833 return 0;
9834
d6958706
AN
9835 if (!link->disconnected && link->detach)
9836 err = link->detach(link);
c016b68e
AN
9837 if (link->pin_path)
9838 free(link->pin_path);
d88b71d4
AN
9839 if (link->dealloc)
9840 link->dealloc(link);
9841 else
9842 free(link);
1c2e9efc 9843
e9fc3ce9 9844 return libbpf_err(err);
1c2e9efc
AN
9845}
9846
c016b68e
AN
9847int bpf_link__fd(const struct bpf_link *link)
9848{
9849 return link->fd;
9850}
9851
9852const char *bpf_link__pin_path(const struct bpf_link *link)
9853{
9854 return link->pin_path;
9855}
9856
9857static int bpf_link__detach_fd(struct bpf_link *link)
9858{
e9fc3ce9 9859 return libbpf_err_errno(close(link->fd));
c016b68e
AN
9860}
9861
9862struct bpf_link *bpf_link__open(const char *path)
9863{
9864 struct bpf_link *link;
9865 int fd;
9866
9867 fd = bpf_obj_get(path);
9868 if (fd < 0) {
9869 fd = -errno;
9870 pr_warn("failed to open link at %s: %d\n", path, fd);
e9fc3ce9 9871 return libbpf_err_ptr(fd);
c016b68e
AN
9872 }
9873
9874 link = calloc(1, sizeof(*link));
9875 if (!link) {
9876 close(fd);
e9fc3ce9 9877 return libbpf_err_ptr(-ENOMEM);
c016b68e
AN
9878 }
9879 link->detach = &bpf_link__detach_fd;
9880 link->fd = fd;
9881
9882 link->pin_path = strdup(path);
9883 if (!link->pin_path) {
9884 bpf_link__destroy(link);
e9fc3ce9 9885 return libbpf_err_ptr(-ENOMEM);
c016b68e
AN
9886 }
9887
9888 return link;
9889}
9890
2e49527e
AN
9891int bpf_link__detach(struct bpf_link *link)
9892{
9893 return bpf_link_detach(link->fd) ? -errno : 0;
9894}
9895
c016b68e
AN
9896int bpf_link__pin(struct bpf_link *link, const char *path)
9897{
9898 int err;
9899
9900 if (link->pin_path)
e9fc3ce9 9901 return libbpf_err(-EBUSY);
c016b68e
AN
9902 err = make_parent_dir(path);
9903 if (err)
e9fc3ce9 9904 return libbpf_err(err);
c016b68e
AN
9905 err = check_path(path);
9906 if (err)
e9fc3ce9 9907 return libbpf_err(err);
c016b68e
AN
9908
9909 link->pin_path = strdup(path);
9910 if (!link->pin_path)
e9fc3ce9 9911 return libbpf_err(-ENOMEM);
c016b68e
AN
9912
9913 if (bpf_obj_pin(link->fd, link->pin_path)) {
9914 err = -errno;
9915 zfree(&link->pin_path);
e9fc3ce9 9916 return libbpf_err(err);
c016b68e
AN
9917 }
9918
9919 pr_debug("link fd=%d: pinned at %s\n", link->fd, link->pin_path);
9920 return 0;
9921}
9922
9923int bpf_link__unpin(struct bpf_link *link)
9924{
9925 int err;
9926
9927 if (!link->pin_path)
e9fc3ce9 9928 return libbpf_err(-EINVAL);
c016b68e
AN
9929
9930 err = unlink(link->pin_path);
9931 if (err != 0)
af0efa05 9932 return -errno;
c016b68e
AN
9933
9934 pr_debug("link fd=%d: unpinned from %s\n", link->fd, link->pin_path);
9935 zfree(&link->pin_path);
9936 return 0;
9937}
63f2f5ee 9938
668ace0e
AN
9939struct bpf_link_perf {
9940 struct bpf_link link;
9941 int perf_event_fd;
ca304b40
RDT
9942 /* legacy kprobe support: keep track of probe identifier and type */
9943 char *legacy_probe_name;
46ed5fc3 9944 bool legacy_is_kprobe;
ca304b40 9945 bool legacy_is_retprobe;
668ace0e
AN
9946};
9947
46ed5fc3 9948static int remove_kprobe_event_legacy(const char *probe_name, bool retprobe);
cc10623c 9949static int remove_uprobe_event_legacy(const char *probe_name, bool retprobe);
46ed5fc3 9950
668ace0e 9951static int bpf_link_perf_detach(struct bpf_link *link)
63f2f5ee 9952{
668ace0e
AN
9953 struct bpf_link_perf *perf_link = container_of(link, struct bpf_link_perf, link);
9954 int err = 0;
63f2f5ee 9955
668ace0e 9956 if (ioctl(perf_link->perf_event_fd, PERF_EVENT_IOC_DISABLE, 0) < 0)
63f2f5ee
AN
9957 err = -errno;
9958
668ace0e
AN
9959 if (perf_link->perf_event_fd != link->fd)
9960 close(perf_link->perf_event_fd);
c016b68e 9961 close(link->fd);
668ace0e 9962
cc10623c 9963 /* legacy uprobe/kprobe needs to be removed after perf event fd closure */
46ed5fc3
AN
9964 if (perf_link->legacy_probe_name) {
9965 if (perf_link->legacy_is_kprobe) {
9966 err = remove_kprobe_event_legacy(perf_link->legacy_probe_name,
9967 perf_link->legacy_is_retprobe);
cc10623c
AN
9968 } else {
9969 err = remove_uprobe_event_legacy(perf_link->legacy_probe_name,
9970 perf_link->legacy_is_retprobe);
46ed5fc3
AN
9971 }
9972 }
ca304b40
RDT
9973
9974 return err;
63f2f5ee
AN
9975}
9976
668ace0e
AN
9977static void bpf_link_perf_dealloc(struct bpf_link *link)
9978{
9979 struct bpf_link_perf *perf_link = container_of(link, struct bpf_link_perf, link);
9980
ca304b40 9981 free(perf_link->legacy_probe_name);
668ace0e
AN
9982 free(perf_link);
9983}
9984
942025c9 9985struct bpf_link *bpf_program__attach_perf_event_opts(const struct bpf_program *prog, int pfd,
47faff37 9986 const struct bpf_perf_event_opts *opts)
63f2f5ee
AN
9987{
9988 char errmsg[STRERR_BUFSIZE];
668ace0e
AN
9989 struct bpf_link_perf *link;
9990 int prog_fd, link_fd = -1, err;
f8b299bc 9991 bool force_ioctl_attach;
63f2f5ee 9992
47faff37
AN
9993 if (!OPTS_VALID(opts, bpf_perf_event_opts))
9994 return libbpf_err_ptr(-EINVAL);
9995
63f2f5ee 9996 if (pfd < 0) {
52109584
AN
9997 pr_warn("prog '%s': invalid perf event FD %d\n",
9998 prog->name, pfd);
e9fc3ce9 9999 return libbpf_err_ptr(-EINVAL);
63f2f5ee
AN
10000 }
10001 prog_fd = bpf_program__fd(prog);
10002 if (prog_fd < 0) {
52109584
AN
10003 pr_warn("prog '%s': can't attach BPF program w/o FD (did you load it?)\n",
10004 prog->name);
e9fc3ce9 10005 return libbpf_err_ptr(-EINVAL);
63f2f5ee
AN
10006 }
10007
d6958706 10008 link = calloc(1, sizeof(*link));
63f2f5ee 10009 if (!link)
e9fc3ce9 10010 return libbpf_err_ptr(-ENOMEM);
668ace0e
AN
10011 link->link.detach = &bpf_link_perf_detach;
10012 link->link.dealloc = &bpf_link_perf_dealloc;
10013 link->perf_event_fd = pfd;
63f2f5ee 10014
f8b299bc
MD
10015 force_ioctl_attach = OPTS_GET(opts, force_ioctl_attach, false);
10016 if (kernel_supports(prog->obj, FEAT_PERF_LINK) && !force_ioctl_attach) {
47faff37
AN
10017 DECLARE_LIBBPF_OPTS(bpf_link_create_opts, link_opts,
10018 .perf_event.bpf_cookie = OPTS_GET(opts, bpf_cookie, 0));
10019
10020 link_fd = bpf_link_create(prog_fd, pfd, BPF_PERF_EVENT, &link_opts);
668ace0e
AN
10021 if (link_fd < 0) {
10022 err = -errno;
10023 pr_warn("prog '%s': failed to create BPF link for perf_event FD %d: %d (%s)\n",
10024 prog->name, pfd,
10025 err, libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
10026 goto err_out;
10027 }
10028 link->link.fd = link_fd;
10029 } else {
47faff37
AN
10030 if (OPTS_GET(opts, bpf_cookie, 0)) {
10031 pr_warn("prog '%s': user context value is not supported\n", prog->name);
10032 err = -EOPNOTSUPP;
10033 goto err_out;
10034 }
10035
668ace0e
AN
10036 if (ioctl(pfd, PERF_EVENT_IOC_SET_BPF, prog_fd) < 0) {
10037 err = -errno;
10038 pr_warn("prog '%s': failed to attach to perf_event FD %d: %s\n",
10039 prog->name, pfd, libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
10040 if (err == -EPROTO)
10041 pr_warn("prog '%s': try add PERF_SAMPLE_CALLCHAIN to or remove exclude_callchain_[kernel|user] from pfd %d\n",
10042 prog->name, pfd);
10043 goto err_out;
10044 }
10045 link->link.fd = pfd;
63f2f5ee
AN
10046 }
10047 if (ioctl(pfd, PERF_EVENT_IOC_ENABLE, 0) < 0) {
10048 err = -errno;
668ace0e 10049 pr_warn("prog '%s': failed to enable perf_event FD %d: %s\n",
52109584 10050 prog->name, pfd, libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
668ace0e 10051 goto err_out;
63f2f5ee 10052 }
668ace0e
AN
10053
10054 return &link->link;
10055err_out:
10056 if (link_fd >= 0)
10057 close(link_fd);
10058 free(link);
10059 return libbpf_err_ptr(err);
63f2f5ee
AN
10060}
10061
942025c9 10062struct bpf_link *bpf_program__attach_perf_event(const struct bpf_program *prog, int pfd)
47faff37
AN
10063{
10064 return bpf_program__attach_perf_event_opts(prog, pfd, NULL);
10065}
10066
b2650027
AN
10067/*
10068 * this function is expected to parse integer in the range of [0, 2^31-1] from
10069 * given file using scanf format string fmt. If actual parsed value is
10070 * negative, the result might be indistinguishable from error
10071 */
10072static int parse_uint_from_file(const char *file, const char *fmt)
10073{
10074 char buf[STRERR_BUFSIZE];
10075 int err, ret;
10076 FILE *f;
10077
59842c54 10078 f = fopen(file, "re");
b2650027
AN
10079 if (!f) {
10080 err = -errno;
10081 pr_debug("failed to open '%s': %s\n", file,
10082 libbpf_strerror_r(err, buf, sizeof(buf)));
10083 return err;
10084 }
10085 err = fscanf(f, fmt, &ret);
10086 if (err != 1) {
10087 err = err == EOF ? -EIO : -errno;
10088 pr_debug("failed to parse '%s': %s\n", file,
10089 libbpf_strerror_r(err, buf, sizeof(buf)));
10090 fclose(f);
10091 return err;
10092 }
10093 fclose(f);
10094 return ret;
10095}
10096
10097static int determine_kprobe_perf_type(void)
10098{
10099 const char *file = "/sys/bus/event_source/devices/kprobe/type";
10100
10101 return parse_uint_from_file(file, "%d\n");
10102}
10103
10104static int determine_uprobe_perf_type(void)
10105{
10106 const char *file = "/sys/bus/event_source/devices/uprobe/type";
10107
10108 return parse_uint_from_file(file, "%d\n");
10109}
10110
10111static int determine_kprobe_retprobe_bit(void)
10112{
10113 const char *file = "/sys/bus/event_source/devices/kprobe/format/retprobe";
10114
10115 return parse_uint_from_file(file, "config:%d\n");
10116}
10117
10118static int determine_uprobe_retprobe_bit(void)
10119{
10120 const char *file = "/sys/bus/event_source/devices/uprobe/format/retprobe";
10121
10122 return parse_uint_from_file(file, "config:%d\n");
10123}
10124
5e3b8356
AN
10125#define PERF_UPROBE_REF_CTR_OFFSET_BITS 32
10126#define PERF_UPROBE_REF_CTR_OFFSET_SHIFT 32
10127
b2650027 10128static int perf_event_open_probe(bool uprobe, bool retprobe, const char *name,
5e3b8356 10129 uint64_t offset, int pid, size_t ref_ctr_off)
b2650027 10130{
813847a3
AN
10131 const size_t attr_sz = sizeof(struct perf_event_attr);
10132 struct perf_event_attr attr;
b2650027 10133 char errmsg[STRERR_BUFSIZE];
708ac5be 10134 int type, pfd;
b2650027 10135
1520e846 10136 if ((__u64)ref_ctr_off >= (1ULL << PERF_UPROBE_REF_CTR_OFFSET_BITS))
5e3b8356
AN
10137 return -EINVAL;
10138
813847a3
AN
10139 memset(&attr, 0, attr_sz);
10140
b2650027
AN
10141 type = uprobe ? determine_uprobe_perf_type()
10142 : determine_kprobe_perf_type();
10143 if (type < 0) {
be18010e
KW
10144 pr_warn("failed to determine %s perf type: %s\n",
10145 uprobe ? "uprobe" : "kprobe",
10146 libbpf_strerror_r(type, errmsg, sizeof(errmsg)));
b2650027
AN
10147 return type;
10148 }
10149 if (retprobe) {
10150 int bit = uprobe ? determine_uprobe_retprobe_bit()
10151 : determine_kprobe_retprobe_bit();
10152
10153 if (bit < 0) {
be18010e
KW
10154 pr_warn("failed to determine %s retprobe bit: %s\n",
10155 uprobe ? "uprobe" : "kprobe",
10156 libbpf_strerror_r(bit, errmsg, sizeof(errmsg)));
b2650027
AN
10157 return bit;
10158 }
10159 attr.config |= 1 << bit;
10160 }
813847a3 10161 attr.size = attr_sz;
b2650027 10162 attr.type = type;
5e3b8356 10163 attr.config |= (__u64)ref_ctr_off << PERF_UPROBE_REF_CTR_OFFSET_SHIFT;
36db2a94
AN
10164 attr.config1 = ptr_to_u64(name); /* kprobe_func or uprobe_path */
10165 attr.config2 = offset; /* kprobe_addr or probe_offset */
b2650027
AN
10166
10167 /* pid filter is meaningful only for uprobes */
10168 pfd = syscall(__NR_perf_event_open, &attr,
10169 pid < 0 ? -1 : pid /* pid */,
10170 pid == -1 ? 0 : -1 /* cpu */,
10171 -1 /* group_fd */, PERF_FLAG_FD_CLOEXEC);
708ac5be 10172 return pfd >= 0 ? pfd : -errno;
b2650027
AN
10173}
10174
46ed5fc3
AN
10175static int append_to_file(const char *file, const char *fmt, ...)
10176{
10177 int fd, n, err = 0;
10178 va_list ap;
01dc26c9
LP
10179 char buf[1024];
10180
10181 va_start(ap, fmt);
10182 n = vsnprintf(buf, sizeof(buf), fmt, ap);
10183 va_end(ap);
10184
10185 if (n < 0 || n >= sizeof(buf))
10186 return -EINVAL;
46ed5fc3 10187
92274e24 10188 fd = open(file, O_WRONLY | O_APPEND | O_CLOEXEC, 0);
46ed5fc3
AN
10189 if (fd < 0)
10190 return -errno;
10191
01dc26c9 10192 if (write(fd, buf, n) < 0)
46ed5fc3
AN
10193 err = -errno;
10194
10195 close(fd);
10196 return err;
10197}
10198
a1ac9fd6
AN
10199#define DEBUGFS "/sys/kernel/debug/tracing"
10200#define TRACEFS "/sys/kernel/tracing"
10201
10202static bool use_debugfs(void)
10203{
10204 static int has_debugfs = -1;
10205
10206 if (has_debugfs < 0)
6a4ab886 10207 has_debugfs = faccessat(AT_FDCWD, DEBUGFS, F_OK, AT_EACCESS) == 0;
a1ac9fd6
AN
10208
10209 return has_debugfs == 1;
10210}
10211
10212static const char *tracefs_path(void)
10213{
10214 return use_debugfs() ? DEBUGFS : TRACEFS;
10215}
10216
10217static const char *tracefs_kprobe_events(void)
10218{
10219 return use_debugfs() ? DEBUGFS"/kprobe_events" : TRACEFS"/kprobe_events";
10220}
10221
10222static const char *tracefs_uprobe_events(void)
10223{
10224 return use_debugfs() ? DEBUGFS"/uprobe_events" : TRACEFS"/uprobe_events";
10225}
10226
46ed5fc3
AN
10227static void gen_kprobe_legacy_event_name(char *buf, size_t buf_sz,
10228 const char *kfunc_name, size_t offset)
10229{
51a33c60 10230 static int index = 0;
2fa07453 10231 int i;
51a33c60
QW
10232
10233 snprintf(buf, buf_sz, "libbpf_%u_%s_0x%zx_%d", getpid(), kfunc_name, offset,
10234 __sync_fetch_and_add(&index, 1));
2fa07453
MD
10235
10236 /* sanitize binary_path in the probe name */
10237 for (i = 0; buf[i]; i++) {
10238 if (!isalnum(buf[i]))
10239 buf[i] = '_';
10240 }
46ed5fc3
AN
10241}
10242
10243static int add_kprobe_event_legacy(const char *probe_name, bool retprobe,
10244 const char *kfunc_name, size_t offset)
10245{
a1ac9fd6 10246 return append_to_file(tracefs_kprobe_events(), "%c:%s/%s %s+0x%zx",
46ed5fc3
AN
10247 retprobe ? 'r' : 'p',
10248 retprobe ? "kretprobes" : "kprobes",
10249 probe_name, kfunc_name, offset);
10250}
10251
10252static int remove_kprobe_event_legacy(const char *probe_name, bool retprobe)
10253{
a1ac9fd6
AN
10254 return append_to_file(tracefs_kprobe_events(), "-:%s/%s",
10255 retprobe ? "kretprobes" : "kprobes", probe_name);
46ed5fc3
AN
10256}
10257
10258static int determine_kprobe_perf_type_legacy(const char *probe_name, bool retprobe)
10259{
10260 char file[256];
10261
a1ac9fd6
AN
10262 snprintf(file, sizeof(file), "%s/events/%s/%s/id",
10263 tracefs_path(), retprobe ? "kretprobes" : "kprobes", probe_name);
46ed5fc3
AN
10264
10265 return parse_uint_from_file(file, "%d\n");
10266}
10267
10268static int perf_event_kprobe_open_legacy(const char *probe_name, bool retprobe,
10269 const char *kfunc_name, size_t offset, int pid)
ca304b40 10270{
813847a3
AN
10271 const size_t attr_sz = sizeof(struct perf_event_attr);
10272 struct perf_event_attr attr;
ca304b40
RDT
10273 char errmsg[STRERR_BUFSIZE];
10274 int type, pfd, err;
10275
46ed5fc3 10276 err = add_kprobe_event_legacy(probe_name, retprobe, kfunc_name, offset);
ca304b40 10277 if (err < 0) {
46ed5fc3
AN
10278 pr_warn("failed to add legacy kprobe event for '%s+0x%zx': %s\n",
10279 kfunc_name, offset,
ca304b40
RDT
10280 libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
10281 return err;
10282 }
46ed5fc3 10283 type = determine_kprobe_perf_type_legacy(probe_name, retprobe);
ca304b40 10284 if (type < 0) {
80940293 10285 err = type;
46ed5fc3
AN
10286 pr_warn("failed to determine legacy kprobe event id for '%s+0x%zx': %s\n",
10287 kfunc_name, offset,
80940293
CW
10288 libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
10289 goto err_clean_legacy;
ca304b40 10290 }
813847a3
AN
10291
10292 memset(&attr, 0, attr_sz);
10293 attr.size = attr_sz;
ca304b40
RDT
10294 attr.config = type;
10295 attr.type = PERF_TYPE_TRACEPOINT;
10296
10297 pfd = syscall(__NR_perf_event_open, &attr,
10298 pid < 0 ? -1 : pid, /* pid */
10299 pid == -1 ? 0 : -1, /* cpu */
10300 -1 /* group_fd */, PERF_FLAG_FD_CLOEXEC);
10301 if (pfd < 0) {
10302 err = -errno;
10303 pr_warn("legacy kprobe perf_event_open() failed: %s\n",
10304 libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
80940293 10305 goto err_clean_legacy;
ca304b40
RDT
10306 }
10307 return pfd;
80940293
CW
10308
10309err_clean_legacy:
10310 /* Clear the newly added legacy kprobe_event */
10311 remove_kprobe_event_legacy(probe_name, retprobe);
10312 return err;
ca304b40
RDT
10313}
10314
708ac5be
AN
10315static const char *arch_specific_syscall_pfx(void)
10316{
10317#if defined(__x86_64__)
10318 return "x64";
10319#elif defined(__i386__)
10320 return "ia32";
10321#elif defined(__s390x__)
10322 return "s390x";
10323#elif defined(__s390__)
10324 return "s390";
10325#elif defined(__arm__)
10326 return "arm";
10327#elif defined(__aarch64__)
10328 return "arm64";
10329#elif defined(__mips__)
10330 return "mips";
10331#elif defined(__riscv)
10332 return "riscv";
64893e83
DM
10333#elif defined(__powerpc__)
10334 return "powerpc";
10335#elif defined(__powerpc64__)
10336 return "powerpc64";
708ac5be
AN
10337#else
10338 return NULL;
10339#endif
10340}
10341
10342static int probe_kern_syscall_wrapper(void)
10343{
10344 char syscall_name[64];
10345 const char *ksys_pfx;
10346
10347 ksys_pfx = arch_specific_syscall_pfx();
10348 if (!ksys_pfx)
10349 return 0;
10350
10351 snprintf(syscall_name, sizeof(syscall_name), "__%s_sys_bpf", ksys_pfx);
10352
10353 if (determine_kprobe_perf_type() >= 0) {
10354 int pfd;
10355
10356 pfd = perf_event_open_probe(false, false, syscall_name, 0, getpid(), 0);
10357 if (pfd >= 0)
10358 close(pfd);
10359
10360 return pfd >= 0 ? 1 : 0;
10361 } else { /* legacy mode */
10362 char probe_name[128];
10363
10364 gen_kprobe_legacy_event_name(probe_name, sizeof(probe_name), syscall_name, 0);
10365 if (add_kprobe_event_legacy(probe_name, false, syscall_name, 0) < 0)
10366 return 0;
10367
10368 (void)remove_kprobe_event_legacy(probe_name, false);
10369 return 1;
10370 }
10371}
10372
da97553e 10373struct bpf_link *
942025c9 10374bpf_program__attach_kprobe_opts(const struct bpf_program *prog,
ac0ed488 10375 const char *func_name,
47faff37 10376 const struct bpf_kprobe_opts *opts)
b2650027 10377{
47faff37 10378 DECLARE_LIBBPF_OPTS(bpf_perf_event_opts, pe_opts);
f8b299bc 10379 enum probe_attach_mode attach_mode;
b2650027 10380 char errmsg[STRERR_BUFSIZE];
ca304b40 10381 char *legacy_probe = NULL;
b2650027 10382 struct bpf_link *link;
46ed5fc3 10383 size_t offset;
ca304b40 10384 bool retprobe, legacy;
b2650027
AN
10385 int pfd, err;
10386
da97553e
JO
10387 if (!OPTS_VALID(opts, bpf_kprobe_opts))
10388 return libbpf_err_ptr(-EINVAL);
10389
f8b299bc 10390 attach_mode = OPTS_GET(opts, attach_mode, PROBE_ATTACH_MODE_DEFAULT);
da97553e
JO
10391 retprobe = OPTS_GET(opts, retprobe, false);
10392 offset = OPTS_GET(opts, offset, 0);
47faff37 10393 pe_opts.bpf_cookie = OPTS_GET(opts, bpf_cookie, 0);
da97553e 10394
ca304b40 10395 legacy = determine_kprobe_perf_type() < 0;
f8b299bc
MD
10396 switch (attach_mode) {
10397 case PROBE_ATTACH_MODE_LEGACY:
10398 legacy = true;
10399 pe_opts.force_ioctl_attach = true;
10400 break;
10401 case PROBE_ATTACH_MODE_PERF:
10402 if (legacy)
10403 return libbpf_err_ptr(-ENOTSUP);
10404 pe_opts.force_ioctl_attach = true;
10405 break;
10406 case PROBE_ATTACH_MODE_LINK:
10407 if (legacy || !kernel_supports(prog->obj, FEAT_PERF_LINK))
10408 return libbpf_err_ptr(-ENOTSUP);
10409 break;
10410 case PROBE_ATTACH_MODE_DEFAULT:
10411 break;
10412 default:
10413 return libbpf_err_ptr(-EINVAL);
10414 }
10415
ca304b40
RDT
10416 if (!legacy) {
10417 pfd = perf_event_open_probe(false /* uprobe */, retprobe,
10418 func_name, offset,
10419 -1 /* pid */, 0 /* ref_ctr_off */);
10420 } else {
46ed5fc3
AN
10421 char probe_name[256];
10422
10423 gen_kprobe_legacy_event_name(probe_name, sizeof(probe_name),
10424 func_name, offset);
10425
71cff670 10426 legacy_probe = strdup(probe_name);
ca304b40
RDT
10427 if (!legacy_probe)
10428 return libbpf_err_ptr(-ENOMEM);
10429
46ed5fc3 10430 pfd = perf_event_kprobe_open_legacy(legacy_probe, retprobe, func_name,
ca304b40
RDT
10431 offset, -1 /* pid */);
10432 }
b2650027 10433 if (pfd < 0) {
46ed5fc3
AN
10434 err = -errno;
10435 pr_warn("prog '%s': failed to create %s '%s+0x%zx' perf event: %s\n",
10436 prog->name, retprobe ? "kretprobe" : "kprobe",
10437 func_name, offset,
303a2572
AN
10438 libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
10439 goto err_out;
b2650027 10440 }
47faff37 10441 link = bpf_program__attach_perf_event_opts(prog, pfd, &pe_opts);
e9fc3ce9
AN
10442 err = libbpf_get_error(link);
10443 if (err) {
b2650027 10444 close(pfd);
46ed5fc3
AN
10445 pr_warn("prog '%s': failed to attach to %s '%s+0x%zx': %s\n",
10446 prog->name, retprobe ? "kretprobe" : "kprobe",
10447 func_name, offset,
be18010e 10448 libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
80940293 10449 goto err_clean_legacy;
b2650027 10450 }
ca304b40
RDT
10451 if (legacy) {
10452 struct bpf_link_perf *perf_link = container_of(link, struct bpf_link_perf, link);
10453
10454 perf_link->legacy_probe_name = legacy_probe;
46ed5fc3 10455 perf_link->legacy_is_kprobe = true;
ca304b40
RDT
10456 perf_link->legacy_is_retprobe = retprobe;
10457 }
10458
b2650027 10459 return link;
80940293
CW
10460
10461err_clean_legacy:
10462 if (legacy)
10463 remove_kprobe_event_legacy(legacy_probe, retprobe);
303a2572
AN
10464err_out:
10465 free(legacy_probe);
10466 return libbpf_err_ptr(err);
b2650027
AN
10467}
10468
942025c9 10469struct bpf_link *bpf_program__attach_kprobe(const struct bpf_program *prog,
ac0ed488
JO
10470 bool retprobe,
10471 const char *func_name)
10472{
da97553e 10473 DECLARE_LIBBPF_OPTS(bpf_kprobe_opts, opts,
ac0ed488 10474 .retprobe = retprobe,
da97553e 10475 );
ac0ed488
JO
10476
10477 return bpf_program__attach_kprobe_opts(prog, func_name, &opts);
10478}
10479
708ac5be
AN
10480struct bpf_link *bpf_program__attach_ksyscall(const struct bpf_program *prog,
10481 const char *syscall_name,
10482 const struct bpf_ksyscall_opts *opts)
10483{
10484 LIBBPF_OPTS(bpf_kprobe_opts, kprobe_opts);
10485 char func_name[128];
10486
10487 if (!OPTS_VALID(opts, bpf_ksyscall_opts))
10488 return libbpf_err_ptr(-EINVAL);
10489
10490 if (kernel_supports(prog->obj, FEAT_SYSCALL_WRAPPER)) {
64893e83
DM
10491 /* arch_specific_syscall_pfx() should never return NULL here
10492 * because it is guarded by kernel_supports(). However, since
10493 * compiler does not know that we have an explicit conditional
10494 * as well.
10495 */
708ac5be 10496 snprintf(func_name, sizeof(func_name), "__%s_sys_%s",
64893e83 10497 arch_specific_syscall_pfx() ? : "", syscall_name);
708ac5be
AN
10498 } else {
10499 snprintf(func_name, sizeof(func_name), "__se_sys_%s", syscall_name);
10500 }
10501
10502 kprobe_opts.retprobe = OPTS_GET(opts, retprobe, false);
10503 kprobe_opts.bpf_cookie = OPTS_GET(opts, bpf_cookie, 0);
10504
10505 return bpf_program__attach_kprobe_opts(prog, func_name, &kprobe_opts);
10506}
10507
ddc6b049
JO
10508/* Adapted from perf/util/string.c */
10509static bool glob_match(const char *str, const char *pat)
10510{
10511 while (*str && *pat && *pat != '*') {
10512 if (*pat == '?') { /* Matches any single character */
10513 str++;
10514 pat++;
10515 continue;
10516 }
10517 if (*str != *pat)
10518 return false;
10519 str++;
10520 pat++;
10521 }
10522 /* Check wild card */
10523 if (*pat == '*') {
10524 while (*pat == '*')
10525 pat++;
10526 if (!*pat) /* Tail wild card matches all */
10527 return true;
10528 while (*str)
10529 if (glob_match(str++, pat))
10530 return true;
10531 }
10532 return !*str && !*pat;
10533}
10534
10535struct kprobe_multi_resolve {
10536 const char *pattern;
10537 unsigned long *addrs;
10538 size_t cap;
10539 size_t cnt;
10540};
10541
10542static int
10543resolve_kprobe_multi_cb(unsigned long long sym_addr, char sym_type,
10544 const char *sym_name, void *ctx)
10545{
10546 struct kprobe_multi_resolve *res = ctx;
10547 int err;
10548
10549 if (!glob_match(sym_name, res->pattern))
10550 return 0;
10551
10552 err = libbpf_ensure_mem((void **) &res->addrs, &res->cap, sizeof(unsigned long),
10553 res->cnt + 1);
10554 if (err)
10555 return err;
10556
10557 res->addrs[res->cnt++] = (unsigned long) sym_addr;
10558 return 0;
10559}
10560
10561struct bpf_link *
10562bpf_program__attach_kprobe_multi_opts(const struct bpf_program *prog,
10563 const char *pattern,
10564 const struct bpf_kprobe_multi_opts *opts)
10565{
10566 LIBBPF_OPTS(bpf_link_create_opts, lopts);
10567 struct kprobe_multi_resolve res = {
10568 .pattern = pattern,
10569 };
10570 struct bpf_link *link = NULL;
10571 char errmsg[STRERR_BUFSIZE];
10572 const unsigned long *addrs;
10573 int err, link_fd, prog_fd;
10574 const __u64 *cookies;
10575 const char **syms;
10576 bool retprobe;
10577 size_t cnt;
10578
10579 if (!OPTS_VALID(opts, bpf_kprobe_multi_opts))
10580 return libbpf_err_ptr(-EINVAL);
10581
10582 syms = OPTS_GET(opts, syms, false);
10583 addrs = OPTS_GET(opts, addrs, false);
10584 cnt = OPTS_GET(opts, cnt, false);
10585 cookies = OPTS_GET(opts, cookies, false);
10586
10587 if (!pattern && !addrs && !syms)
10588 return libbpf_err_ptr(-EINVAL);
10589 if (pattern && (addrs || syms || cookies || cnt))
10590 return libbpf_err_ptr(-EINVAL);
10591 if (!pattern && !cnt)
10592 return libbpf_err_ptr(-EINVAL);
10593 if (addrs && syms)
10594 return libbpf_err_ptr(-EINVAL);
10595
10596 if (pattern) {
10597 err = libbpf_kallsyms_parse(resolve_kprobe_multi_cb, &res);
10598 if (err)
10599 goto error;
10600 if (!res.cnt) {
10601 err = -ENOENT;
10602 goto error;
10603 }
10604 addrs = res.addrs;
10605 cnt = res.cnt;
10606 }
10607
10608 retprobe = OPTS_GET(opts, retprobe, false);
10609
10610 lopts.kprobe_multi.syms = syms;
10611 lopts.kprobe_multi.addrs = addrs;
10612 lopts.kprobe_multi.cookies = cookies;
10613 lopts.kprobe_multi.cnt = cnt;
10614 lopts.kprobe_multi.flags = retprobe ? BPF_F_KPROBE_MULTI_RETURN : 0;
10615
10616 link = calloc(1, sizeof(*link));
10617 if (!link) {
10618 err = -ENOMEM;
10619 goto error;
10620 }
10621 link->detach = &bpf_link__detach_fd;
10622
10623 prog_fd = bpf_program__fd(prog);
10624 link_fd = bpf_link_create(prog_fd, 0, BPF_TRACE_KPROBE_MULTI, &lopts);
10625 if (link_fd < 0) {
10626 err = -errno;
10627 pr_warn("prog '%s': failed to attach: %s\n",
10628 prog->name, libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
10629 goto error;
10630 }
10631 link->fd = link_fd;
10632 free(res.addrs);
10633 return link;
10634
10635error:
10636 free(link);
10637 free(res.addrs);
10638 return libbpf_err_ptr(err);
10639}
10640
4fa5bcfe 10641static int attach_kprobe(const struct bpf_program *prog, long cookie, struct bpf_link **link)
d7a18ea7 10642{
da97553e 10643 DECLARE_LIBBPF_OPTS(bpf_kprobe_opts, opts);
a2488b5f 10644 unsigned long offset = 0;
d7a18ea7 10645 const char *func_name;
a2488b5f 10646 char *func;
4fa5bcfe 10647 int n;
d7a18ea7 10648
9af8efc4
AN
10649 *link = NULL;
10650
10651 /* no auto-attach for SEC("kprobe") and SEC("kretprobe") */
10652 if (strcmp(prog->sec_name, "kprobe") == 0 || strcmp(prog->sec_name, "kretprobe") == 0)
10653 return 0;
10654
13d35a0c
AN
10655 opts.retprobe = str_has_pfx(prog->sec_name, "kretprobe/");
10656 if (opts.retprobe)
10657 func_name = prog->sec_name + sizeof("kretprobe/") - 1;
10658 else
10659 func_name = prog->sec_name + sizeof("kprobe/") - 1;
d7a18ea7 10660
e3f9bc35 10661 n = sscanf(func_name, "%m[a-zA-Z0-9_.]+%li", &func, &offset);
a2488b5f 10662 if (n < 1) {
a2488b5f 10663 pr_warn("kprobe name is invalid: %s\n", func_name);
4fa5bcfe 10664 return -EINVAL;
a2488b5f
AM
10665 }
10666 if (opts.retprobe && offset != 0) {
1f71a468 10667 free(func);
a2488b5f 10668 pr_warn("kretprobes do not support offset specification\n");
4fa5bcfe 10669 return -EINVAL;
a2488b5f 10670 }
d7a18ea7 10671
a2488b5f 10672 opts.offset = offset;
4fa5bcfe 10673 *link = bpf_program__attach_kprobe_opts(prog, func, &opts);
a2488b5f 10674 free(func);
4fa5bcfe 10675 return libbpf_get_error(*link);
d7a18ea7
AN
10676}
10677
708ac5be
AN
10678static int attach_ksyscall(const struct bpf_program *prog, long cookie, struct bpf_link **link)
10679{
10680 LIBBPF_OPTS(bpf_ksyscall_opts, opts);
10681 const char *syscall_name;
10682
10683 *link = NULL;
10684
10685 /* no auto-attach for SEC("ksyscall") and SEC("kretsyscall") */
10686 if (strcmp(prog->sec_name, "ksyscall") == 0 || strcmp(prog->sec_name, "kretsyscall") == 0)
10687 return 0;
10688
10689 opts.retprobe = str_has_pfx(prog->sec_name, "kretsyscall/");
10690 if (opts.retprobe)
10691 syscall_name = prog->sec_name + sizeof("kretsyscall/") - 1;
10692 else
10693 syscall_name = prog->sec_name + sizeof("ksyscall/") - 1;
10694
10695 *link = bpf_program__attach_ksyscall(prog, syscall_name, &opts);
10696 return *link ? 0 : -errno;
10697}
10698
ddc6b049
JO
10699static int attach_kprobe_multi(const struct bpf_program *prog, long cookie, struct bpf_link **link)
10700{
10701 LIBBPF_OPTS(bpf_kprobe_multi_opts, opts);
10702 const char *spec;
10703 char *pattern;
10704 int n;
10705
9af8efc4
AN
10706 *link = NULL;
10707
10708 /* no auto-attach for SEC("kprobe.multi") and SEC("kretprobe.multi") */
10709 if (strcmp(prog->sec_name, "kprobe.multi") == 0 ||
10710 strcmp(prog->sec_name, "kretprobe.multi") == 0)
10711 return 0;
10712
ddc6b049
JO
10713 opts.retprobe = str_has_pfx(prog->sec_name, "kretprobe.multi/");
10714 if (opts.retprobe)
10715 spec = prog->sec_name + sizeof("kretprobe.multi/") - 1;
10716 else
10717 spec = prog->sec_name + sizeof("kprobe.multi/") - 1;
10718
10719 n = sscanf(spec, "%m[a-zA-Z0-9_.*?]", &pattern);
10720 if (n < 1) {
10721 pr_warn("kprobe multi pattern is invalid: %s\n", pattern);
10722 return -EINVAL;
10723 }
10724
10725 *link = bpf_program__attach_kprobe_multi_opts(prog, pattern, &opts);
10726 free(pattern);
10727 return libbpf_get_error(*link);
10728}
10729
cc10623c
AN
10730static void gen_uprobe_legacy_event_name(char *buf, size_t buf_sz,
10731 const char *binary_path, uint64_t offset)
10732{
10733 int i;
10734
10735 snprintf(buf, buf_sz, "libbpf_%u_%s_0x%zx", getpid(), binary_path, (size_t)offset);
10736
10737 /* sanitize binary_path in the probe name */
10738 for (i = 0; buf[i]; i++) {
10739 if (!isalnum(buf[i]))
10740 buf[i] = '_';
10741 }
10742}
10743
10744static inline int add_uprobe_event_legacy(const char *probe_name, bool retprobe,
10745 const char *binary_path, size_t offset)
10746{
a1ac9fd6 10747 return append_to_file(tracefs_uprobe_events(), "%c:%s/%s %s:0x%zx",
cc10623c
AN
10748 retprobe ? 'r' : 'p',
10749 retprobe ? "uretprobes" : "uprobes",
10750 probe_name, binary_path, offset);
10751}
10752
10753static inline int remove_uprobe_event_legacy(const char *probe_name, bool retprobe)
10754{
a1ac9fd6
AN
10755 return append_to_file(tracefs_uprobe_events(), "-:%s/%s",
10756 retprobe ? "uretprobes" : "uprobes", probe_name);
cc10623c
AN
10757}
10758
10759static int determine_uprobe_perf_type_legacy(const char *probe_name, bool retprobe)
10760{
10761 char file[512];
10762
a1ac9fd6
AN
10763 snprintf(file, sizeof(file), "%s/events/%s/%s/id",
10764 tracefs_path(), retprobe ? "uretprobes" : "uprobes", probe_name);
cc10623c
AN
10765
10766 return parse_uint_from_file(file, "%d\n");
10767}
10768
10769static int perf_event_uprobe_open_legacy(const char *probe_name, bool retprobe,
10770 const char *binary_path, size_t offset, int pid)
10771{
813847a3 10772 const size_t attr_sz = sizeof(struct perf_event_attr);
cc10623c
AN
10773 struct perf_event_attr attr;
10774 int type, pfd, err;
10775
10776 err = add_uprobe_event_legacy(probe_name, retprobe, binary_path, offset);
10777 if (err < 0) {
10778 pr_warn("failed to add legacy uprobe event for %s:0x%zx: %d\n",
10779 binary_path, (size_t)offset, err);
10780 return err;
10781 }
10782 type = determine_uprobe_perf_type_legacy(probe_name, retprobe);
10783 if (type < 0) {
2655144f 10784 err = type;
cc10623c 10785 pr_warn("failed to determine legacy uprobe event id for %s:0x%zx: %d\n",
2655144f
CW
10786 binary_path, offset, err);
10787 goto err_clean_legacy;
cc10623c
AN
10788 }
10789
813847a3
AN
10790 memset(&attr, 0, attr_sz);
10791 attr.size = attr_sz;
cc10623c
AN
10792 attr.config = type;
10793 attr.type = PERF_TYPE_TRACEPOINT;
10794
10795 pfd = syscall(__NR_perf_event_open, &attr,
10796 pid < 0 ? -1 : pid, /* pid */
10797 pid == -1 ? 0 : -1, /* cpu */
10798 -1 /* group_fd */, PERF_FLAG_FD_CLOEXEC);
10799 if (pfd < 0) {
10800 err = -errno;
10801 pr_warn("legacy uprobe perf_event_open() failed: %d\n", err);
2655144f 10802 goto err_clean_legacy;
cc10623c
AN
10803 }
10804 return pfd;
2655144f
CW
10805
10806err_clean_legacy:
10807 /* Clear the newly added legacy uprobe_event */
10808 remove_uprobe_event_legacy(probe_name, retprobe);
10809 return err;
cc10623c
AN
10810}
10811
433966e3
AM
10812/* Return next ELF section of sh_type after scn, or first of that type if scn is NULL. */
10813static Elf_Scn *elf_find_next_scn_by_type(Elf *elf, int sh_type, Elf_Scn *scn)
10814{
10815 while ((scn = elf_nextscn(elf, scn)) != NULL) {
10816 GElf_Shdr sh;
10817
10818 if (!gelf_getshdr(scn, &sh))
10819 continue;
10820 if (sh.sh_type == sh_type)
10821 return scn;
10822 }
10823 return NULL;
10824}
10825
434fdcea
DM
10826/* Find offset of function name in the provided ELF object. "binary_path" is
10827 * the path to the ELF binary represented by "elf", and only used for error
10828 * reporting matters. "name" matches symbol name or name@@LIB for library
10829 * functions.
433966e3 10830 */
434fdcea 10831static long elf_find_func_offset(Elf *elf, const char *binary_path, const char *name)
433966e3 10832{
434fdcea 10833 int i, sh_types[2] = { SHT_DYNSYM, SHT_SYMTAB };
433966e3 10834 bool is_shared_lib, is_name_qualified;
433966e3
AM
10835 long ret = -ENOENT;
10836 size_t name_len;
10837 GElf_Ehdr ehdr;
433966e3 10838
433966e3
AM
10839 if (!gelf_getehdr(elf, &ehdr)) {
10840 pr_warn("elf: failed to get ehdr from %s: %s\n", binary_path, elf_errmsg(-1));
10841 ret = -LIBBPF_ERRNO__FORMAT;
10842 goto out;
10843 }
10844 /* for shared lib case, we do not need to calculate relative offset */
10845 is_shared_lib = ehdr.e_type == ET_DYN;
10846
10847 name_len = strlen(name);
10848 /* Does name specify "@@LIB"? */
10849 is_name_qualified = strstr(name, "@@") != NULL;
10850
434fdcea 10851 /* Search SHT_DYNSYM, SHT_SYMTAB for symbol. This search order is used because if
433966e3
AM
10852 * a binary is stripped, it may only have SHT_DYNSYM, and a fully-statically
10853 * linked binary may not have SHT_DYMSYM, so absence of a section should not be
10854 * reported as a warning/error.
10855 */
10856 for (i = 0; i < ARRAY_SIZE(sh_types); i++) {
10857 size_t nr_syms, strtabidx, idx;
10858 Elf_Data *symbols = NULL;
10859 Elf_Scn *scn = NULL;
10860 int last_bind = -1;
10861 const char *sname;
10862 GElf_Shdr sh;
10863
10864 scn = elf_find_next_scn_by_type(elf, sh_types[i], NULL);
10865 if (!scn) {
10866 pr_debug("elf: failed to find symbol table ELF sections in '%s'\n",
10867 binary_path);
10868 continue;
10869 }
10870 if (!gelf_getshdr(scn, &sh))
10871 continue;
10872 strtabidx = sh.sh_link;
10873 symbols = elf_getdata(scn, 0);
10874 if (!symbols) {
10875 pr_warn("elf: failed to get symbols for symtab section in '%s': %s\n",
10876 binary_path, elf_errmsg(-1));
10877 ret = -LIBBPF_ERRNO__FORMAT;
10878 goto out;
10879 }
10880 nr_syms = symbols->d_size / sh.sh_entsize;
10881
10882 for (idx = 0; idx < nr_syms; idx++) {
10883 int curr_bind;
10884 GElf_Sym sym;
fe928335
AN
10885 Elf_Scn *sym_scn;
10886 GElf_Shdr sym_sh;
433966e3
AM
10887
10888 if (!gelf_getsym(symbols, idx, &sym))
10889 continue;
10890
10891 if (GELF_ST_TYPE(sym.st_info) != STT_FUNC)
10892 continue;
10893
10894 sname = elf_strptr(elf, strtabidx, sym.st_name);
10895 if (!sname)
10896 continue;
10897
10898 curr_bind = GELF_ST_BIND(sym.st_info);
10899
10900 /* User can specify func, func@@LIB or func@@LIB_VERSION. */
10901 if (strncmp(sname, name, name_len) != 0)
10902 continue;
10903 /* ...but we don't want a search for "foo" to match 'foo2" also, so any
10904 * additional characters in sname should be of the form "@@LIB".
10905 */
10906 if (!is_name_qualified && sname[name_len] != '\0' && sname[name_len] != '@')
10907 continue;
10908
10909 if (ret >= 0) {
10910 /* handle multiple matches */
10911 if (last_bind != STB_WEAK && curr_bind != STB_WEAK) {
10912 /* Only accept one non-weak bind. */
10913 pr_warn("elf: ambiguous match for '%s', '%s' in '%s'\n",
10914 sname, name, binary_path);
10915 ret = -LIBBPF_ERRNO__FORMAT;
10916 goto out;
10917 } else if (curr_bind == STB_WEAK) {
10918 /* already have a non-weak bind, and
10919 * this is a weak bind, so ignore.
10920 */
10921 continue;
10922 }
10923 }
fe928335
AN
10924
10925 /* Transform symbol's virtual address (absolute for
10926 * binaries and relative for shared libs) into file
10927 * offset, which is what kernel is expecting for
10928 * uprobe/uretprobe attachment.
10929 * See Documentation/trace/uprobetracer.rst for more
10930 * details.
10931 * This is done by looking up symbol's containing
10932 * section's header and using it's virtual address
10933 * (sh_addr) and corresponding file offset (sh_offset)
10934 * to transform sym.st_value (virtual address) into
10935 * desired final file offset.
10936 */
10937 sym_scn = elf_getscn(elf, sym.st_shndx);
10938 if (!sym_scn)
10939 continue;
10940 if (!gelf_getshdr(sym_scn, &sym_sh))
10941 continue;
10942
10943 ret = sym.st_value - sym_sh.sh_addr + sym_sh.sh_offset;
433966e3
AM
10944 last_bind = curr_bind;
10945 }
433966e3
AM
10946 if (ret > 0)
10947 break;
10948 }
10949
10950 if (ret > 0) {
10951 pr_debug("elf: symbol address match for '%s' in '%s': 0x%lx\n", name, binary_path,
10952 ret);
10953 } else {
10954 if (ret == 0) {
10955 pr_warn("elf: '%s' is 0 in symtab for '%s': %s\n", name, binary_path,
10956 is_shared_lib ? "should not be 0 in a shared library" :
10957 "try using shared library path instead");
10958 ret = -ENOENT;
10959 } else {
10960 pr_warn("elf: failed to find symbol '%s' in '%s'\n", name, binary_path);
10961 }
10962 }
10963out:
434fdcea
DM
10964 return ret;
10965}
10966
10967/* Find offset of function name in ELF object specified by path. "name" matches
10968 * symbol name or name@@LIB for library functions.
10969 */
10970static long elf_find_func_offset_from_file(const char *binary_path, const char *name)
10971{
10972 char errmsg[STRERR_BUFSIZE];
10973 long ret = -ENOENT;
10974 Elf *elf;
10975 int fd;
10976
10977 fd = open(binary_path, O_RDONLY | O_CLOEXEC);
10978 if (fd < 0) {
10979 ret = -errno;
10980 pr_warn("failed to open %s: %s\n", binary_path,
10981 libbpf_strerror_r(ret, errmsg, sizeof(errmsg)));
10982 return ret;
10983 }
10984 elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
10985 if (!elf) {
10986 pr_warn("elf: could not read elf from %s: %s\n", binary_path, elf_errmsg(-1));
10987 close(fd);
10988 return -LIBBPF_ERRNO__FORMAT;
10989 }
10990
10991 ret = elf_find_func_offset(elf, binary_path, name);
433966e3
AM
10992 elf_end(elf);
10993 close(fd);
10994 return ret;
10995}
10996
c44fd845
DM
10997/* Find offset of function name in archive specified by path. Currently
10998 * supported are .zip files that do not compress their contents, as used on
10999 * Android in the form of APKs, for example. "file_name" is the name of the ELF
11000 * file inside the archive. "func_name" matches symbol name or name@@LIB for
11001 * library functions.
11002 *
11003 * An overview of the APK format specifically provided here:
11004 * https://en.wikipedia.org/w/index.php?title=Apk_(file_format)&oldid=1139099120#Package_contents
11005 */
11006static long elf_find_func_offset_from_archive(const char *archive_path, const char *file_name,
11007 const char *func_name)
11008{
11009 struct zip_archive *archive;
11010 struct zip_entry entry;
11011 long ret;
11012 Elf *elf;
11013
11014 archive = zip_archive_open(archive_path);
11015 if (IS_ERR(archive)) {
11016 ret = PTR_ERR(archive);
11017 pr_warn("zip: failed to open %s: %ld\n", archive_path, ret);
11018 return ret;
11019 }
11020
11021 ret = zip_archive_find_entry(archive, file_name, &entry);
11022 if (ret) {
11023 pr_warn("zip: could not find archive member %s in %s: %ld\n", file_name,
11024 archive_path, ret);
11025 goto out;
11026 }
11027 pr_debug("zip: found entry for %s in %s at 0x%lx\n", file_name, archive_path,
11028 (unsigned long)entry.data_offset);
11029
11030 if (entry.compression) {
11031 pr_warn("zip: entry %s of %s is compressed and cannot be handled\n", file_name,
11032 archive_path);
11033 ret = -LIBBPF_ERRNO__FORMAT;
11034 goto out;
11035 }
11036
11037 elf = elf_memory((void *)entry.data, entry.data_length);
11038 if (!elf) {
11039 pr_warn("elf: could not read elf file %s from %s: %s\n", file_name, archive_path,
11040 elf_errmsg(-1));
11041 ret = -LIBBPF_ERRNO__LIBELF;
11042 goto out;
11043 }
11044
11045 ret = elf_find_func_offset(elf, file_name, func_name);
11046 if (ret > 0) {
11047 pr_debug("elf: symbol address match for %s of %s in %s: 0x%x + 0x%lx = 0x%lx\n",
11048 func_name, file_name, archive_path, entry.data_offset, ret,
11049 ret + entry.data_offset);
11050 ret += entry.data_offset;
11051 }
11052 elf_end(elf);
11053
11054out:
11055 zip_archive_close(archive);
11056 return ret;
11057}
11058
56818931
IL
11059static const char *arch_specific_lib_paths(void)
11060{
11061 /*
11062 * Based on https://packages.debian.org/sid/libc6.
11063 *
11064 * Assume that the traced program is built for the same architecture
11065 * as libbpf, which should cover the vast majority of cases.
11066 */
11067#if defined(__x86_64__)
11068 return "/lib/x86_64-linux-gnu";
11069#elif defined(__i386__)
11070 return "/lib/i386-linux-gnu";
11071#elif defined(__s390x__)
11072 return "/lib/s390x-linux-gnu";
11073#elif defined(__s390__)
11074 return "/lib/s390-linux-gnu";
11075#elif defined(__arm__) && defined(__SOFTFP__)
11076 return "/lib/arm-linux-gnueabi";
11077#elif defined(__arm__) && !defined(__SOFTFP__)
11078 return "/lib/arm-linux-gnueabihf";
11079#elif defined(__aarch64__)
11080 return "/lib/aarch64-linux-gnu";
11081#elif defined(__mips__) && defined(__MIPSEL__) && _MIPS_SZLONG == 64
11082 return "/lib/mips64el-linux-gnuabi64";
11083#elif defined(__mips__) && defined(__MIPSEL__) && _MIPS_SZLONG == 32
11084 return "/lib/mipsel-linux-gnu";
11085#elif defined(__powerpc64__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
11086 return "/lib/powerpc64le-linux-gnu";
11087#elif defined(__sparc__) && defined(__arch64__)
11088 return "/lib/sparc64-linux-gnu";
11089#elif defined(__riscv) && __riscv_xlen == 64
11090 return "/lib/riscv64-linux-gnu";
11091#else
11092 return NULL;
11093#endif
11094}
11095
1ce3a60e
AM
11096/* Get full path to program/shared library. */
11097static int resolve_full_path(const char *file, char *result, size_t result_sz)
11098{
56818931 11099 const char *search_paths[3] = {};
9e32084e 11100 int i, perm;
1ce3a60e 11101
a1c9d61b 11102 if (str_has_sfx(file, ".so") || strstr(file, ".so.")) {
1ce3a60e
AM
11103 search_paths[0] = getenv("LD_LIBRARY_PATH");
11104 search_paths[1] = "/usr/lib64:/usr/lib";
56818931 11105 search_paths[2] = arch_specific_lib_paths();
9e32084e 11106 perm = R_OK;
1ce3a60e
AM
11107 } else {
11108 search_paths[0] = getenv("PATH");
11109 search_paths[1] = "/usr/bin:/usr/sbin";
9e32084e 11110 perm = R_OK | X_OK;
1ce3a60e
AM
11111 }
11112
11113 for (i = 0; i < ARRAY_SIZE(search_paths); i++) {
11114 const char *s;
11115
11116 if (!search_paths[i])
11117 continue;
11118 for (s = search_paths[i]; s != NULL; s = strchr(s, ':')) {
11119 char *next_path;
11120 int seg_len;
11121
11122 if (s[0] == ':')
11123 s++;
11124 next_path = strchr(s, ':');
11125 seg_len = next_path ? next_path - s : strlen(s);
11126 if (!seg_len)
11127 continue;
11128 snprintf(result, result_sz, "%.*s/%s", seg_len, s, file);
9e32084e 11129 /* ensure it has required permissions */
6a4ab886 11130 if (faccessat(AT_FDCWD, result, perm, AT_EACCESS) < 0)
1ce3a60e
AM
11131 continue;
11132 pr_debug("resolved '%s' to '%s'\n", file, result);
11133 return 0;
11134 }
11135 }
11136 return -ENOENT;
11137}
11138
47faff37 11139LIBBPF_API struct bpf_link *
942025c9 11140bpf_program__attach_uprobe_opts(const struct bpf_program *prog, pid_t pid,
47faff37
AN
11141 const char *binary_path, size_t func_offset,
11142 const struct bpf_uprobe_opts *opts)
b2650027 11143{
c44fd845 11144 const char *archive_path = NULL, *archive_sep = NULL;
cc10623c 11145 char errmsg[STRERR_BUFSIZE], *legacy_probe = NULL;
c44fd845 11146 DECLARE_LIBBPF_OPTS(bpf_perf_event_opts, pe_opts);
f8b299bc 11147 enum probe_attach_mode attach_mode;
c44fd845 11148 char full_path[PATH_MAX];
b2650027 11149 struct bpf_link *link;
5e3b8356 11150 size_t ref_ctr_off;
b2650027 11151 int pfd, err;
cc10623c 11152 bool retprobe, legacy;
433966e3 11153 const char *func_name;
47faff37
AN
11154
11155 if (!OPTS_VALID(opts, bpf_uprobe_opts))
11156 return libbpf_err_ptr(-EINVAL);
11157
f8b299bc 11158 attach_mode = OPTS_GET(opts, attach_mode, PROBE_ATTACH_MODE_DEFAULT);
47faff37 11159 retprobe = OPTS_GET(opts, retprobe, false);
5e3b8356 11160 ref_ctr_off = OPTS_GET(opts, ref_ctr_offset, 0);
47faff37 11161 pe_opts.bpf_cookie = OPTS_GET(opts, bpf_cookie, 0);
b2650027 11162
8ed2f5a6
HC
11163 if (!binary_path)
11164 return libbpf_err_ptr(-EINVAL);
11165
c44fd845
DM
11166 /* Check if "binary_path" refers to an archive. */
11167 archive_sep = strstr(binary_path, "!/");
11168 if (archive_sep) {
11169 full_path[0] = '\0';
11170 libbpf_strlcpy(full_path, binary_path,
11171 min(sizeof(full_path), (size_t)(archive_sep - binary_path + 1)));
11172 archive_path = full_path;
11173 binary_path = archive_sep + 2;
11174 } else if (!strchr(binary_path, '/')) {
11175 err = resolve_full_path(binary_path, full_path, sizeof(full_path));
1ce3a60e 11176 if (err) {
2e4913e0
AN
11177 pr_warn("prog '%s': failed to resolve full path for '%s': %d\n",
11178 prog->name, binary_path, err);
1ce3a60e
AM
11179 return libbpf_err_ptr(err);
11180 }
c44fd845 11181 binary_path = full_path;
1ce3a60e 11182 }
433966e3
AM
11183 func_name = OPTS_GET(opts, func_name, NULL);
11184 if (func_name) {
11185 long sym_off;
11186
c44fd845
DM
11187 if (archive_path) {
11188 sym_off = elf_find_func_offset_from_archive(archive_path, binary_path,
11189 func_name);
11190 binary_path = archive_path;
11191 } else {
11192 sym_off = elf_find_func_offset_from_file(binary_path, func_name);
11193 }
433966e3
AM
11194 if (sym_off < 0)
11195 return libbpf_err_ptr(sym_off);
11196 func_offset += sym_off;
11197 }
1ce3a60e 11198
cc10623c 11199 legacy = determine_uprobe_perf_type() < 0;
f8b299bc
MD
11200 switch (attach_mode) {
11201 case PROBE_ATTACH_MODE_LEGACY:
11202 legacy = true;
11203 pe_opts.force_ioctl_attach = true;
11204 break;
11205 case PROBE_ATTACH_MODE_PERF:
11206 if (legacy)
11207 return libbpf_err_ptr(-ENOTSUP);
11208 pe_opts.force_ioctl_attach = true;
11209 break;
11210 case PROBE_ATTACH_MODE_LINK:
11211 if (legacy || !kernel_supports(prog->obj, FEAT_PERF_LINK))
11212 return libbpf_err_ptr(-ENOTSUP);
11213 break;
11214 case PROBE_ATTACH_MODE_DEFAULT:
11215 break;
11216 default:
11217 return libbpf_err_ptr(-EINVAL);
11218 }
11219
cc10623c
AN
11220 if (!legacy) {
11221 pfd = perf_event_open_probe(true /* uprobe */, retprobe, binary_path,
11222 func_offset, pid, ref_ctr_off);
11223 } else {
1ce3a60e 11224 char probe_name[PATH_MAX + 64];
cc10623c
AN
11225
11226 if (ref_ctr_off)
11227 return libbpf_err_ptr(-EINVAL);
11228
11229 gen_uprobe_legacy_event_name(probe_name, sizeof(probe_name),
11230 binary_path, func_offset);
11231
11232 legacy_probe = strdup(probe_name);
11233 if (!legacy_probe)
11234 return libbpf_err_ptr(-ENOMEM);
11235
11236 pfd = perf_event_uprobe_open_legacy(legacy_probe, retprobe,
11237 binary_path, func_offset, pid);
11238 }
b2650027 11239 if (pfd < 0) {
cc10623c 11240 err = -errno;
52109584
AN
11241 pr_warn("prog '%s': failed to create %s '%s:0x%zx' perf event: %s\n",
11242 prog->name, retprobe ? "uretprobe" : "uprobe",
be18010e 11243 binary_path, func_offset,
cc10623c
AN
11244 libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
11245 goto err_out;
b2650027 11246 }
cc10623c 11247
47faff37 11248 link = bpf_program__attach_perf_event_opts(prog, pfd, &pe_opts);
e9fc3ce9
AN
11249 err = libbpf_get_error(link);
11250 if (err) {
b2650027 11251 close(pfd);
52109584
AN
11252 pr_warn("prog '%s': failed to attach to %s '%s:0x%zx': %s\n",
11253 prog->name, retprobe ? "uretprobe" : "uprobe",
be18010e
KW
11254 binary_path, func_offset,
11255 libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
2655144f 11256 goto err_clean_legacy;
cc10623c
AN
11257 }
11258 if (legacy) {
11259 struct bpf_link_perf *perf_link = container_of(link, struct bpf_link_perf, link);
11260
11261 perf_link->legacy_probe_name = legacy_probe;
11262 perf_link->legacy_is_kprobe = false;
11263 perf_link->legacy_is_retprobe = retprobe;
b2650027
AN
11264 }
11265 return link;
2655144f
CW
11266
11267err_clean_legacy:
11268 if (legacy)
11269 remove_uprobe_event_legacy(legacy_probe, retprobe);
cc10623c
AN
11270err_out:
11271 free(legacy_probe);
11272 return libbpf_err_ptr(err);
b2650027
AN
11273}
11274
39f8dc43
AM
11275/* Format of u[ret]probe section definition supporting auto-attach:
11276 * u[ret]probe/binary:function[+offset]
11277 *
11278 * binary can be an absolute/relative path or a filename; the latter is resolved to a
11279 * full binary path via bpf_program__attach_uprobe_opts.
11280 *
11281 * Specifying uprobe+ ensures we carry out strict matching; either "uprobe" must be
11282 * specified (and auto-attach is not possible) or the above format is specified for
11283 * auto-attach.
11284 */
11285static int attach_uprobe(const struct bpf_program *prog, long cookie, struct bpf_link **link)
11286{
11287 DECLARE_LIBBPF_OPTS(bpf_uprobe_opts, opts);
90db26e6
AM
11288 char *probe_type = NULL, *binary_path = NULL, *func_name = NULL;
11289 int n, ret = -EINVAL;
11290 long offset = 0;
39f8dc43
AM
11291
11292 *link = NULL;
11293
90db26e6
AM
11294 n = sscanf(prog->sec_name, "%m[^/]/%m[^:]:%m[a-zA-Z0-9_.]+%li",
11295 &probe_type, &binary_path, &func_name, &offset);
11296 switch (n) {
11297 case 1:
11298 /* handle SEC("u[ret]probe") - format is valid, but auto-attach is impossible. */
11299 ret = 0;
11300 break;
11301 case 2:
11302 pr_warn("prog '%s': section '%s' missing ':function[+offset]' specification\n",
11303 prog->name, prog->sec_name);
11304 break;
11305 case 3:
11306 case 4:
c4cac71f
DK
11307 opts.retprobe = strcmp(probe_type, "uretprobe") == 0 ||
11308 strcmp(probe_type, "uretprobe.s") == 0;
90db26e6
AM
11309 if (opts.retprobe && offset != 0) {
11310 pr_warn("prog '%s': uretprobes do not support offset specification\n",
11311 prog->name);
11312 break;
11313 }
11314 opts.func_name = func_name;
11315 *link = bpf_program__attach_uprobe_opts(prog, -1, binary_path, offset, &opts);
11316 ret = libbpf_get_error(*link);
11317 break;
11318 default:
11319 pr_warn("prog '%s': invalid format of section definition '%s'\n", prog->name,
39f8dc43 11320 prog->sec_name);
90db26e6 11321 break;
39f8dc43 11322 }
90db26e6
AM
11323 free(probe_type);
11324 free(binary_path);
11325 free(func_name);
39f8dc43 11326
90db26e6 11327 return ret;
39f8dc43
AM
11328}
11329
942025c9 11330struct bpf_link *bpf_program__attach_uprobe(const struct bpf_program *prog,
47faff37
AN
11331 bool retprobe, pid_t pid,
11332 const char *binary_path,
11333 size_t func_offset)
11334{
11335 DECLARE_LIBBPF_OPTS(bpf_uprobe_opts, opts, .retprobe = retprobe);
11336
11337 return bpf_program__attach_uprobe_opts(prog, pid, binary_path, func_offset, &opts);
11338}
11339
2e4913e0
AN
11340struct bpf_link *bpf_program__attach_usdt(const struct bpf_program *prog,
11341 pid_t pid, const char *binary_path,
11342 const char *usdt_provider, const char *usdt_name,
11343 const struct bpf_usdt_opts *opts)
11344{
11345 char resolved_path[512];
11346 struct bpf_object *obj = prog->obj;
11347 struct bpf_link *link;
5af25a41 11348 __u64 usdt_cookie;
2e4913e0
AN
11349 int err;
11350
11351 if (!OPTS_VALID(opts, bpf_uprobe_opts))
11352 return libbpf_err_ptr(-EINVAL);
11353
11354 if (bpf_program__fd(prog) < 0) {
11355 pr_warn("prog '%s': can't attach BPF program w/o FD (did you load it?)\n",
11356 prog->name);
11357 return libbpf_err_ptr(-EINVAL);
11358 }
11359
8ed2f5a6
HC
11360 if (!binary_path)
11361 return libbpf_err_ptr(-EINVAL);
11362
2e4913e0
AN
11363 if (!strchr(binary_path, '/')) {
11364 err = resolve_full_path(binary_path, resolved_path, sizeof(resolved_path));
11365 if (err) {
11366 pr_warn("prog '%s': failed to resolve full path for '%s': %d\n",
11367 prog->name, binary_path, err);
11368 return libbpf_err_ptr(err);
11369 }
11370 binary_path = resolved_path;
11371 }
11372
11373 /* USDT manager is instantiated lazily on first USDT attach. It will
11374 * be destroyed together with BPF object in bpf_object__close().
11375 */
11376 if (IS_ERR(obj->usdt_man))
11377 return libbpf_ptr(obj->usdt_man);
11378 if (!obj->usdt_man) {
11379 obj->usdt_man = usdt_manager_new(obj);
11380 if (IS_ERR(obj->usdt_man))
11381 return libbpf_ptr(obj->usdt_man);
11382 }
11383
11384 usdt_cookie = OPTS_GET(opts, usdt_cookie, 0);
11385 link = usdt_manager_attach_usdt(obj->usdt_man, prog, pid, binary_path,
e3ba8e4e 11386 usdt_provider, usdt_name, usdt_cookie);
2e4913e0
AN
11387 err = libbpf_get_error(link);
11388 if (err)
11389 return libbpf_err_ptr(err);
11390 return link;
11391}
11392
11393static int attach_usdt(const struct bpf_program *prog, long cookie, struct bpf_link **link)
11394{
11395 char *path = NULL, *provider = NULL, *name = NULL;
11396 const char *sec_name;
11397 int n, err;
11398
11399 sec_name = bpf_program__section_name(prog);
11400 if (strcmp(sec_name, "usdt") == 0) {
11401 /* no auto-attach for just SEC("usdt") */
11402 *link = NULL;
11403 return 0;
11404 }
11405
11406 n = sscanf(sec_name, "usdt/%m[^:]:%m[^:]:%m[^:]", &path, &provider, &name);
11407 if (n != 3) {
11408 pr_warn("invalid section '%s', expected SEC(\"usdt/<path>:<provider>:<name>\")\n",
11409 sec_name);
11410 err = -EINVAL;
11411 } else {
11412 *link = bpf_program__attach_usdt(prog, -1 /* any process */, path,
11413 provider, name, NULL);
11414 err = libbpf_get_error(*link);
11415 }
11416 free(path);
11417 free(provider);
11418 free(name);
11419 return err;
11420}
11421
f6de59c1
AN
11422static int determine_tracepoint_id(const char *tp_category,
11423 const char *tp_name)
11424{
11425 char file[PATH_MAX];
11426 int ret;
11427
a1ac9fd6
AN
11428 ret = snprintf(file, sizeof(file), "%s/events/%s/%s/id",
11429 tracefs_path(), tp_category, tp_name);
f6de59c1
AN
11430 if (ret < 0)
11431 return -errno;
11432 if (ret >= sizeof(file)) {
11433 pr_debug("tracepoint %s/%s path is too long\n",
11434 tp_category, tp_name);
11435 return -E2BIG;
11436 }
11437 return parse_uint_from_file(file, "%d\n");
11438}
11439
11440static int perf_event_open_tracepoint(const char *tp_category,
11441 const char *tp_name)
11442{
813847a3
AN
11443 const size_t attr_sz = sizeof(struct perf_event_attr);
11444 struct perf_event_attr attr;
f6de59c1
AN
11445 char errmsg[STRERR_BUFSIZE];
11446 int tp_id, pfd, err;
11447
11448 tp_id = determine_tracepoint_id(tp_category, tp_name);
11449 if (tp_id < 0) {
be18010e
KW
11450 pr_warn("failed to determine tracepoint '%s/%s' perf event ID: %s\n",
11451 tp_category, tp_name,
11452 libbpf_strerror_r(tp_id, errmsg, sizeof(errmsg)));
f6de59c1
AN
11453 return tp_id;
11454 }
11455
813847a3 11456 memset(&attr, 0, attr_sz);
f6de59c1 11457 attr.type = PERF_TYPE_TRACEPOINT;
813847a3 11458 attr.size = attr_sz;
f6de59c1
AN
11459 attr.config = tp_id;
11460
11461 pfd = syscall(__NR_perf_event_open, &attr, -1 /* pid */, 0 /* cpu */,
11462 -1 /* group_fd */, PERF_FLAG_FD_CLOEXEC);
11463 if (pfd < 0) {
11464 err = -errno;
be18010e
KW
11465 pr_warn("tracepoint '%s/%s' perf_event_open() failed: %s\n",
11466 tp_category, tp_name,
11467 libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
f6de59c1
AN
11468 return err;
11469 }
11470 return pfd;
11471}
11472
942025c9 11473struct bpf_link *bpf_program__attach_tracepoint_opts(const struct bpf_program *prog,
47faff37
AN
11474 const char *tp_category,
11475 const char *tp_name,
11476 const struct bpf_tracepoint_opts *opts)
f6de59c1 11477{
47faff37 11478 DECLARE_LIBBPF_OPTS(bpf_perf_event_opts, pe_opts);
f6de59c1
AN
11479 char errmsg[STRERR_BUFSIZE];
11480 struct bpf_link *link;
11481 int pfd, err;
11482
47faff37
AN
11483 if (!OPTS_VALID(opts, bpf_tracepoint_opts))
11484 return libbpf_err_ptr(-EINVAL);
11485
11486 pe_opts.bpf_cookie = OPTS_GET(opts, bpf_cookie, 0);
11487
f6de59c1
AN
11488 pfd = perf_event_open_tracepoint(tp_category, tp_name);
11489 if (pfd < 0) {
52109584
AN
11490 pr_warn("prog '%s': failed to create tracepoint '%s/%s' perf event: %s\n",
11491 prog->name, tp_category, tp_name,
be18010e 11492 libbpf_strerror_r(pfd, errmsg, sizeof(errmsg)));
e9fc3ce9 11493 return libbpf_err_ptr(pfd);
f6de59c1 11494 }
47faff37 11495 link = bpf_program__attach_perf_event_opts(prog, pfd, &pe_opts);
e9fc3ce9
AN
11496 err = libbpf_get_error(link);
11497 if (err) {
f6de59c1 11498 close(pfd);
52109584
AN
11499 pr_warn("prog '%s': failed to attach to tracepoint '%s/%s': %s\n",
11500 prog->name, tp_category, tp_name,
be18010e 11501 libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
e9fc3ce9 11502 return libbpf_err_ptr(err);
f6de59c1
AN
11503 }
11504 return link;
11505}
11506
942025c9 11507struct bpf_link *bpf_program__attach_tracepoint(const struct bpf_program *prog,
47faff37
AN
11508 const char *tp_category,
11509 const char *tp_name)
11510{
11511 return bpf_program__attach_tracepoint_opts(prog, tp_category, tp_name, NULL);
11512}
11513
4fa5bcfe 11514static int attach_tp(const struct bpf_program *prog, long cookie, struct bpf_link **link)
d7a18ea7
AN
11515{
11516 char *sec_name, *tp_cat, *tp_name;
d7a18ea7 11517
9af8efc4
AN
11518 *link = NULL;
11519
11520 /* no auto-attach for SEC("tp") or SEC("tracepoint") */
11521 if (strcmp(prog->sec_name, "tp") == 0 || strcmp(prog->sec_name, "tracepoint") == 0)
11522 return 0;
11523
ac6a6586
AN
11524 sec_name = strdup(prog->sec_name);
11525 if (!sec_name)
11526 return -ENOMEM;
11527
13d35a0c
AN
11528 /* extract "tp/<category>/<name>" or "tracepoint/<category>/<name>" */
11529 if (str_has_pfx(prog->sec_name, "tp/"))
11530 tp_cat = sec_name + sizeof("tp/") - 1;
11531 else
11532 tp_cat = sec_name + sizeof("tracepoint/") - 1;
d7a18ea7
AN
11533 tp_name = strchr(tp_cat, '/');
11534 if (!tp_name) {
e9fc3ce9 11535 free(sec_name);
4fa5bcfe 11536 return -EINVAL;
d7a18ea7
AN
11537 }
11538 *tp_name = '\0';
11539 tp_name++;
11540
4fa5bcfe 11541 *link = bpf_program__attach_tracepoint(prog, tp_cat, tp_name);
d7a18ea7 11542 free(sec_name);
4fa5bcfe 11543 return libbpf_get_error(*link);
d7a18ea7
AN
11544}
11545
942025c9 11546struct bpf_link *bpf_program__attach_raw_tracepoint(const struct bpf_program *prog,
84bf5e1f
AN
11547 const char *tp_name)
11548{
11549 char errmsg[STRERR_BUFSIZE];
c016b68e 11550 struct bpf_link *link;
84bf5e1f
AN
11551 int prog_fd, pfd;
11552
11553 prog_fd = bpf_program__fd(prog);
11554 if (prog_fd < 0) {
52109584 11555 pr_warn("prog '%s': can't attach before loaded\n", prog->name);
e9fc3ce9 11556 return libbpf_err_ptr(-EINVAL);
84bf5e1f
AN
11557 }
11558
d6958706 11559 link = calloc(1, sizeof(*link));
84bf5e1f 11560 if (!link)
e9fc3ce9 11561 return libbpf_err_ptr(-ENOMEM);
c016b68e 11562 link->detach = &bpf_link__detach_fd;
84bf5e1f
AN
11563
11564 pfd = bpf_raw_tracepoint_open(tp_name, prog_fd);
11565 if (pfd < 0) {
11566 pfd = -errno;
11567 free(link);
52109584
AN
11568 pr_warn("prog '%s': failed to attach to raw tracepoint '%s': %s\n",
11569 prog->name, tp_name, libbpf_strerror_r(pfd, errmsg, sizeof(errmsg)));
e9fc3ce9 11570 return libbpf_err_ptr(pfd);
84bf5e1f
AN
11571 }
11572 link->fd = pfd;
c016b68e 11573 return link;
84bf5e1f
AN
11574}
11575
4fa5bcfe 11576static int attach_raw_tp(const struct bpf_program *prog, long cookie, struct bpf_link **link)
d7a18ea7 11577{
ccaf12d6 11578 static const char *const prefixes[] = {
9af8efc4
AN
11579 "raw_tp",
11580 "raw_tracepoint",
11581 "raw_tp.w",
11582 "raw_tracepoint.w",
ccaf12d6
HT
11583 };
11584 size_t i;
11585 const char *tp_name = NULL;
13d35a0c 11586
9af8efc4
AN
11587 *link = NULL;
11588
ccaf12d6 11589 for (i = 0; i < ARRAY_SIZE(prefixes); i++) {
9af8efc4
AN
11590 size_t pfx_len;
11591
11592 if (!str_has_pfx(prog->sec_name, prefixes[i]))
11593 continue;
11594
11595 pfx_len = strlen(prefixes[i]);
11596 /* no auto-attach case of, e.g., SEC("raw_tp") */
11597 if (prog->sec_name[pfx_len] == '\0')
11598 return 0;
11599
11600 if (prog->sec_name[pfx_len] != '/')
11601 continue;
11602
11603 tp_name = prog->sec_name + pfx_len + 1;
11604 break;
ccaf12d6 11605 }
9af8efc4 11606
ccaf12d6
HT
11607 if (!tp_name) {
11608 pr_warn("prog '%s': invalid section name '%s'\n",
11609 prog->name, prog->sec_name);
4fa5bcfe 11610 return -EINVAL;
ccaf12d6 11611 }
d7a18ea7 11612
4fa5bcfe 11613 *link = bpf_program__attach_raw_tracepoint(prog, tp_name);
5fd2a60a 11614 return libbpf_get_error(*link);
d7a18ea7
AN
11615}
11616
1e092a03 11617/* Common logic for all BPF program types that attach to a btf_id */
129b9c5e
KFL
11618static struct bpf_link *bpf_program__attach_btf_id(const struct bpf_program *prog,
11619 const struct bpf_trace_opts *opts)
b8c54ea4 11620{
129b9c5e 11621 LIBBPF_OPTS(bpf_link_create_opts, link_opts);
b8c54ea4 11622 char errmsg[STRERR_BUFSIZE];
c016b68e 11623 struct bpf_link *link;
b8c54ea4
AS
11624 int prog_fd, pfd;
11625
129b9c5e
KFL
11626 if (!OPTS_VALID(opts, bpf_trace_opts))
11627 return libbpf_err_ptr(-EINVAL);
11628
b8c54ea4
AS
11629 prog_fd = bpf_program__fd(prog);
11630 if (prog_fd < 0) {
52109584 11631 pr_warn("prog '%s': can't attach before loaded\n", prog->name);
e9fc3ce9 11632 return libbpf_err_ptr(-EINVAL);
b8c54ea4
AS
11633 }
11634
d6958706 11635 link = calloc(1, sizeof(*link));
b8c54ea4 11636 if (!link)
e9fc3ce9 11637 return libbpf_err_ptr(-ENOMEM);
c016b68e 11638 link->detach = &bpf_link__detach_fd;
b8c54ea4 11639
8462e0b4 11640 /* libbpf is smart enough to redirect to BPF_RAW_TRACEPOINT_OPEN on old kernels */
129b9c5e
KFL
11641 link_opts.tracing.cookie = OPTS_GET(opts, cookie, 0);
11642 pfd = bpf_link_create(prog_fd, 0, bpf_program__expected_attach_type(prog), &link_opts);
b8c54ea4
AS
11643 if (pfd < 0) {
11644 pfd = -errno;
11645 free(link);
52109584
AN
11646 pr_warn("prog '%s': failed to attach: %s\n",
11647 prog->name, libbpf_strerror_r(pfd, errmsg, sizeof(errmsg)));
e9fc3ce9 11648 return libbpf_err_ptr(pfd);
b8c54ea4
AS
11649 }
11650 link->fd = pfd;
003fed59 11651 return link;
b8c54ea4
AS
11652}
11653
942025c9 11654struct bpf_link *bpf_program__attach_trace(const struct bpf_program *prog)
1e092a03 11655{
129b9c5e
KFL
11656 return bpf_program__attach_btf_id(prog, NULL);
11657}
11658
11659struct bpf_link *bpf_program__attach_trace_opts(const struct bpf_program *prog,
11660 const struct bpf_trace_opts *opts)
11661{
11662 return bpf_program__attach_btf_id(prog, opts);
1e092a03
KS
11663}
11664
942025c9 11665struct bpf_link *bpf_program__attach_lsm(const struct bpf_program *prog)
1e092a03 11666{
129b9c5e 11667 return bpf_program__attach_btf_id(prog, NULL);
1e092a03
KS
11668}
11669
4fa5bcfe 11670static int attach_trace(const struct bpf_program *prog, long cookie, struct bpf_link **link)
d7a18ea7 11671{
4fa5bcfe
AN
11672 *link = bpf_program__attach_trace(prog);
11673 return libbpf_get_error(*link);
d7a18ea7
AN
11674}
11675
4fa5bcfe 11676static int attach_lsm(const struct bpf_program *prog, long cookie, struct bpf_link **link)
1e092a03 11677{
4fa5bcfe
AN
11678 *link = bpf_program__attach_lsm(prog);
11679 return libbpf_get_error(*link);
1e092a03
KS
11680}
11681
d60d81ac 11682static struct bpf_link *
942025c9 11683bpf_program__attach_fd(const struct bpf_program *prog, int target_fd, int btf_id,
d60d81ac 11684 const char *target_name)
cc4f864b 11685{
a5359091
THJ
11686 DECLARE_LIBBPF_OPTS(bpf_link_create_opts, opts,
11687 .target_btf_id = btf_id);
cc4f864b
AN
11688 enum bpf_attach_type attach_type;
11689 char errmsg[STRERR_BUFSIZE];
11690 struct bpf_link *link;
11691 int prog_fd, link_fd;
11692
11693 prog_fd = bpf_program__fd(prog);
11694 if (prog_fd < 0) {
52109584 11695 pr_warn("prog '%s': can't attach before loaded\n", prog->name);
e9fc3ce9 11696 return libbpf_err_ptr(-EINVAL);
cc4f864b
AN
11697 }
11698
11699 link = calloc(1, sizeof(*link));
11700 if (!link)
e9fc3ce9 11701 return libbpf_err_ptr(-ENOMEM);
cc4f864b
AN
11702 link->detach = &bpf_link__detach_fd;
11703
20eccf29 11704 attach_type = bpf_program__expected_attach_type(prog);
a5359091 11705 link_fd = bpf_link_create(prog_fd, target_fd, attach_type, &opts);
cc4f864b
AN
11706 if (link_fd < 0) {
11707 link_fd = -errno;
11708 free(link);
52109584
AN
11709 pr_warn("prog '%s': failed to attach to %s: %s\n",
11710 prog->name, target_name,
cc4f864b 11711 libbpf_strerror_r(link_fd, errmsg, sizeof(errmsg)));
e9fc3ce9 11712 return libbpf_err_ptr(link_fd);
cc4f864b
AN
11713 }
11714 link->fd = link_fd;
11715 return link;
11716}
11717
d60d81ac 11718struct bpf_link *
942025c9 11719bpf_program__attach_cgroup(const struct bpf_program *prog, int cgroup_fd)
d60d81ac 11720{
a5359091 11721 return bpf_program__attach_fd(prog, cgroup_fd, 0, "cgroup");
d60d81ac
JS
11722}
11723
11724struct bpf_link *
942025c9 11725bpf_program__attach_netns(const struct bpf_program *prog, int netns_fd)
d60d81ac 11726{
a5359091 11727 return bpf_program__attach_fd(prog, netns_fd, 0, "netns");
d60d81ac
JS
11728}
11729
942025c9 11730struct bpf_link *bpf_program__attach_xdp(const struct bpf_program *prog, int ifindex)
dc8698ca
AN
11731{
11732 /* target_fd/target_ifindex use the same field in LINK_CREATE */
a5359091
THJ
11733 return bpf_program__attach_fd(prog, ifindex, 0, "xdp");
11734}
11735
942025c9 11736struct bpf_link *bpf_program__attach_freplace(const struct bpf_program *prog,
a5359091
THJ
11737 int target_fd,
11738 const char *attach_func_name)
11739{
11740 int btf_id;
11741
11742 if (!!target_fd != !!attach_func_name) {
11743 pr_warn("prog '%s': supply none or both of target_fd and attach_func_name\n",
11744 prog->name);
e9fc3ce9 11745 return libbpf_err_ptr(-EINVAL);
a5359091
THJ
11746 }
11747
11748 if (prog->type != BPF_PROG_TYPE_EXT) {
11749 pr_warn("prog '%s': only BPF_PROG_TYPE_EXT can attach as freplace",
11750 prog->name);
e9fc3ce9 11751 return libbpf_err_ptr(-EINVAL);
a5359091
THJ
11752 }
11753
11754 if (target_fd) {
11755 btf_id = libbpf_find_prog_btf_id(attach_func_name, target_fd);
11756 if (btf_id < 0)
e9fc3ce9 11757 return libbpf_err_ptr(btf_id);
a5359091
THJ
11758
11759 return bpf_program__attach_fd(prog, target_fd, btf_id, "freplace");
11760 } else {
11761 /* no target, so use raw_tracepoint_open for compatibility
11762 * with old kernels
11763 */
11764 return bpf_program__attach_trace(prog);
11765 }
dc8698ca
AN
11766}
11767
c09add2f 11768struct bpf_link *
942025c9 11769bpf_program__attach_iter(const struct bpf_program *prog,
c09add2f
YS
11770 const struct bpf_iter_attach_opts *opts)
11771{
cd31039a 11772 DECLARE_LIBBPF_OPTS(bpf_link_create_opts, link_create_opts);
c09add2f
YS
11773 char errmsg[STRERR_BUFSIZE];
11774 struct bpf_link *link;
11775 int prog_fd, link_fd;
cd31039a 11776 __u32 target_fd = 0;
c09add2f
YS
11777
11778 if (!OPTS_VALID(opts, bpf_iter_attach_opts))
e9fc3ce9 11779 return libbpf_err_ptr(-EINVAL);
c09add2f 11780
74fc097d
YS
11781 link_create_opts.iter_info = OPTS_GET(opts, link_info, (void *)0);
11782 link_create_opts.iter_info_len = OPTS_GET(opts, link_info_len, 0);
cd31039a 11783
c09add2f
YS
11784 prog_fd = bpf_program__fd(prog);
11785 if (prog_fd < 0) {
52109584 11786 pr_warn("prog '%s': can't attach before loaded\n", prog->name);
e9fc3ce9 11787 return libbpf_err_ptr(-EINVAL);
c09add2f
YS
11788 }
11789
11790 link = calloc(1, sizeof(*link));
11791 if (!link)
e9fc3ce9 11792 return libbpf_err_ptr(-ENOMEM);
c09add2f
YS
11793 link->detach = &bpf_link__detach_fd;
11794
cd31039a
YS
11795 link_fd = bpf_link_create(prog_fd, target_fd, BPF_TRACE_ITER,
11796 &link_create_opts);
c09add2f
YS
11797 if (link_fd < 0) {
11798 link_fd = -errno;
11799 free(link);
52109584
AN
11800 pr_warn("prog '%s': failed to attach to iterator: %s\n",
11801 prog->name, libbpf_strerror_r(link_fd, errmsg, sizeof(errmsg)));
e9fc3ce9 11802 return libbpf_err_ptr(link_fd);
c09add2f
YS
11803 }
11804 link->fd = link_fd;
11805 return link;
11806}
11807
4fa5bcfe 11808static int attach_iter(const struct bpf_program *prog, long cookie, struct bpf_link **link)
e9fc3ce9 11809{
4fa5bcfe
AN
11810 *link = bpf_program__attach_iter(prog, NULL);
11811 return libbpf_get_error(*link);
e9fc3ce9
AN
11812}
11813
942025c9 11814struct bpf_link *bpf_program__attach(const struct bpf_program *prog)
d7a18ea7 11815{
4fa5bcfe
AN
11816 struct bpf_link *link = NULL;
11817 int err;
11818
11819 if (!prog->sec_def || !prog->sec_def->prog_attach_fn)
11820 return libbpf_err_ptr(-EOPNOTSUPP);
d7a18ea7 11821
4fa5bcfe
AN
11822 err = prog->sec_def->prog_attach_fn(prog, prog->sec_def->cookie, &link);
11823 if (err)
11824 return libbpf_err_ptr(err);
11825
11826 /* When calling bpf_program__attach() explicitly, auto-attach support
11827 * is expected to work, so NULL returned link is considered an error.
11828 * This is different for skeleton's attach, see comment in
11829 * bpf_object__attach_skeleton().
11830 */
11831 if (!link)
11832 return libbpf_err_ptr(-EOPNOTSUPP);
11833
11834 return link;
d7a18ea7
AN
11835}
11836
8d1608d7
KFL
11837struct bpf_link_struct_ops {
11838 struct bpf_link link;
11839 int map_fd;
11840};
11841
590a0088
MKL
11842static int bpf_link__detach_struct_ops(struct bpf_link *link)
11843{
8d1608d7 11844 struct bpf_link_struct_ops *st_link;
590a0088
MKL
11845 __u32 zero = 0;
11846
8d1608d7 11847 st_link = container_of(link, struct bpf_link_struct_ops, link);
590a0088 11848
8d1608d7
KFL
11849 if (st_link->map_fd < 0)
11850 /* w/o a real link */
11851 return bpf_map_delete_elem(link->fd, &zero);
11852
11853 return close(link->fd);
590a0088
MKL
11854}
11855
942025c9 11856struct bpf_link *bpf_map__attach_struct_ops(const struct bpf_map *map)
590a0088 11857{
8d1608d7
KFL
11858 struct bpf_link_struct_ops *link;
11859 __u32 zero = 0;
11860 int err, fd;
590a0088
MKL
11861
11862 if (!bpf_map__is_struct_ops(map) || map->fd == -1)
e9fc3ce9 11863 return libbpf_err_ptr(-EINVAL);
590a0088
MKL
11864
11865 link = calloc(1, sizeof(*link));
11866 if (!link)
e9fc3ce9 11867 return libbpf_err_ptr(-EINVAL);
590a0088 11868
8d1608d7
KFL
11869 /* kern_vdata should be prepared during the loading phase. */
11870 err = bpf_map_update_elem(map->fd, &zero, map->st_ops->kern_vdata, 0);
11871 /* It can be EBUSY if the map has been used to create or
11872 * update a link before. We don't allow updating the value of
11873 * a struct_ops once it is set. That ensures that the value
11874 * never changed. So, it is safe to skip EBUSY.
11875 */
11876 if (err && (!(map->def.map_flags & BPF_F_LINK) || err != -EBUSY)) {
11877 free(link);
11878 return libbpf_err_ptr(err);
11879 }
590a0088 11880
8d1608d7 11881 link->link.detach = bpf_link__detach_struct_ops;
590a0088 11882
8d1608d7
KFL
11883 if (!(map->def.map_flags & BPF_F_LINK)) {
11884 /* w/o a real link */
11885 link->link.fd = map->fd;
11886 link->map_fd = -1;
11887 return &link->link;
590a0088
MKL
11888 }
11889
8d1608d7
KFL
11890 fd = bpf_link_create(map->fd, 0, BPF_STRUCT_OPS, NULL);
11891 if (fd < 0) {
590a0088 11892 free(link);
8d1608d7 11893 return libbpf_err_ptr(fd);
590a0088
MKL
11894 }
11895
8d1608d7
KFL
11896 link->link.fd = fd;
11897 link->map_fd = map->fd;
590a0088 11898
8d1608d7 11899 return &link->link;
590a0088
MKL
11900}
11901
912dd4b0
KFL
11902/*
11903 * Swap the back struct_ops of a link with a new struct_ops map.
11904 */
11905int bpf_link__update_map(struct bpf_link *link, const struct bpf_map *map)
11906{
11907 struct bpf_link_struct_ops *st_ops_link;
11908 __u32 zero = 0;
11909 int err;
11910
11911 if (!bpf_map__is_struct_ops(map) || map->fd < 0)
11912 return -EINVAL;
11913
11914 st_ops_link = container_of(link, struct bpf_link_struct_ops, link);
11915 /* Ensure the type of a link is correct */
11916 if (st_ops_link->map_fd < 0)
11917 return -EINVAL;
11918
11919 err = bpf_map_update_elem(map->fd, &zero, map->st_ops->kern_vdata, 0);
11920 /* It can be EBUSY if the map has been used to create or
11921 * update a link before. We don't allow updating the value of
11922 * a struct_ops once it is set. That ensures that the value
11923 * never changed. So, it is safe to skip EBUSY.
11924 */
11925 if (err && err != -EBUSY)
11926 return err;
11927
11928 err = bpf_link_update(link->fd, map->fd, NULL);
11929 if (err < 0)
11930 return err;
11931
11932 st_ops_link->map_fd = map->fd;
11933
11934 return 0;
11935}
11936
22dd7a58
AN
11937typedef enum bpf_perf_event_ret (*bpf_perf_event_print_t)(struct perf_event_header *hdr,
11938 void *private_data);
11939
7218c28c
CL
11940static enum bpf_perf_event_ret
11941perf_event_read_simple(void *mmap_mem, size_t mmap_size, size_t page_size,
11942 void **copy_mem, size_t *copy_size,
11943 bpf_perf_event_print_t fn, void *private_data)
d0cabbb0 11944{
3dca2115 11945 struct perf_event_mmap_page *header = mmap_mem;
a64af0ef 11946 __u64 data_head = ring_buffer_read_head(header);
d0cabbb0 11947 __u64 data_tail = header->data_tail;
3dca2115
DB
11948 void *base = ((__u8 *)header) + page_size;
11949 int ret = LIBBPF_PERF_EVENT_CONT;
11950 struct perf_event_header *ehdr;
11951 size_t ehdr_size;
11952
11953 while (data_head != data_tail) {
11954 ehdr = base + (data_tail & (mmap_size - 1));
11955 ehdr_size = ehdr->size;
11956
11957 if (((void *)ehdr) + ehdr_size > base + mmap_size) {
11958 void *copy_start = ehdr;
11959 size_t len_first = base + mmap_size - copy_start;
11960 size_t len_secnd = ehdr_size - len_first;
11961
11962 if (*copy_size < ehdr_size) {
11963 free(*copy_mem);
11964 *copy_mem = malloc(ehdr_size);
11965 if (!*copy_mem) {
11966 *copy_size = 0;
d0cabbb0
JK
11967 ret = LIBBPF_PERF_EVENT_ERROR;
11968 break;
11969 }
3dca2115 11970 *copy_size = ehdr_size;
d0cabbb0
JK
11971 }
11972
3dca2115
DB
11973 memcpy(*copy_mem, copy_start, len_first);
11974 memcpy(*copy_mem + len_first, base, len_secnd);
11975 ehdr = *copy_mem;
d0cabbb0
JK
11976 }
11977
3dca2115
DB
11978 ret = fn(ehdr, private_data);
11979 data_tail += ehdr_size;
d0cabbb0
JK
11980 if (ret != LIBBPF_PERF_EVENT_CONT)
11981 break;
d0cabbb0
JK
11982 }
11983
a64af0ef 11984 ring_buffer_write_tail(header, data_tail);
e9fc3ce9 11985 return libbpf_err(ret);
d0cabbb0 11986}
34be1646 11987
fb84b822
AN
11988struct perf_buffer;
11989
11990struct perf_buffer_params {
11991 struct perf_event_attr *attr;
11992 /* if event_cb is specified, it takes precendence */
11993 perf_buffer_event_fn event_cb;
11994 /* sample_cb and lost_cb are higher-level common-case callbacks */
11995 perf_buffer_sample_fn sample_cb;
11996 perf_buffer_lost_fn lost_cb;
11997 void *ctx;
11998 int cpu_cnt;
11999 int *cpus;
12000 int *map_keys;
12001};
12002
12003struct perf_cpu_buf {
12004 struct perf_buffer *pb;
12005 void *base; /* mmap()'ed memory */
12006 void *buf; /* for reconstructing segmented data */
12007 size_t buf_size;
12008 int fd;
12009 int cpu;
12010 int map_key;
12011};
12012
12013struct perf_buffer {
12014 perf_buffer_event_fn event_cb;
12015 perf_buffer_sample_fn sample_cb;
12016 perf_buffer_lost_fn lost_cb;
12017 void *ctx; /* passed into callbacks */
12018
12019 size_t page_size;
12020 size_t mmap_size;
12021 struct perf_cpu_buf **cpu_bufs;
12022 struct epoll_event *events;
783b8f01 12023 int cpu_cnt; /* number of allocated CPU buffers */
fb84b822
AN
12024 int epoll_fd; /* perf event FD */
12025 int map_fd; /* BPF_MAP_TYPE_PERF_EVENT_ARRAY BPF map FD */
12026};
12027
12028static void perf_buffer__free_cpu_buf(struct perf_buffer *pb,
12029 struct perf_cpu_buf *cpu_buf)
12030{
12031 if (!cpu_buf)
12032 return;
12033 if (cpu_buf->base &&
12034 munmap(cpu_buf->base, pb->mmap_size + pb->page_size))
be18010e 12035 pr_warn("failed to munmap cpu_buf #%d\n", cpu_buf->cpu);
fb84b822
AN
12036 if (cpu_buf->fd >= 0) {
12037 ioctl(cpu_buf->fd, PERF_EVENT_IOC_DISABLE, 0);
12038 close(cpu_buf->fd);
12039 }
12040 free(cpu_buf->buf);
12041 free(cpu_buf);
12042}
12043
12044void perf_buffer__free(struct perf_buffer *pb)
12045{
12046 int i;
12047
50450fc7 12048 if (IS_ERR_OR_NULL(pb))
fb84b822
AN
12049 return;
12050 if (pb->cpu_bufs) {
601b05ca 12051 for (i = 0; i < pb->cpu_cnt; i++) {
fb84b822
AN
12052 struct perf_cpu_buf *cpu_buf = pb->cpu_bufs[i];
12053
601b05ca
EC
12054 if (!cpu_buf)
12055 continue;
12056
fb84b822
AN
12057 bpf_map_delete_elem(pb->map_fd, &cpu_buf->map_key);
12058 perf_buffer__free_cpu_buf(pb, cpu_buf);
12059 }
12060 free(pb->cpu_bufs);
12061 }
12062 if (pb->epoll_fd >= 0)
12063 close(pb->epoll_fd);
12064 free(pb->events);
12065 free(pb);
12066}
12067
12068static struct perf_cpu_buf *
12069perf_buffer__open_cpu_buf(struct perf_buffer *pb, struct perf_event_attr *attr,
12070 int cpu, int map_key)
12071{
12072 struct perf_cpu_buf *cpu_buf;
12073 char msg[STRERR_BUFSIZE];
12074 int err;
12075
12076 cpu_buf = calloc(1, sizeof(*cpu_buf));
12077 if (!cpu_buf)
12078 return ERR_PTR(-ENOMEM);
12079
12080 cpu_buf->pb = pb;
12081 cpu_buf->cpu = cpu;
12082 cpu_buf->map_key = map_key;
12083
12084 cpu_buf->fd = syscall(__NR_perf_event_open, attr, -1 /* pid */, cpu,
12085 -1, PERF_FLAG_FD_CLOEXEC);
12086 if (cpu_buf->fd < 0) {
12087 err = -errno;
be18010e
KW
12088 pr_warn("failed to open perf buffer event on cpu #%d: %s\n",
12089 cpu, libbpf_strerror_r(err, msg, sizeof(msg)));
fb84b822
AN
12090 goto error;
12091 }
12092
12093 cpu_buf->base = mmap(NULL, pb->mmap_size + pb->page_size,
12094 PROT_READ | PROT_WRITE, MAP_SHARED,
12095 cpu_buf->fd, 0);
12096 if (cpu_buf->base == MAP_FAILED) {
12097 cpu_buf->base = NULL;
12098 err = -errno;
be18010e
KW
12099 pr_warn("failed to mmap perf buffer on cpu #%d: %s\n",
12100 cpu, libbpf_strerror_r(err, msg, sizeof(msg)));
fb84b822
AN
12101 goto error;
12102 }
12103
12104 if (ioctl(cpu_buf->fd, PERF_EVENT_IOC_ENABLE, 0) < 0) {
12105 err = -errno;
be18010e
KW
12106 pr_warn("failed to enable perf buffer event on cpu #%d: %s\n",
12107 cpu, libbpf_strerror_r(err, msg, sizeof(msg)));
fb84b822
AN
12108 goto error;
12109 }
12110
12111 return cpu_buf;
12112
12113error:
12114 perf_buffer__free_cpu_buf(pb, cpu_buf);
12115 return (struct perf_cpu_buf *)ERR_PTR(err);
12116}
12117
12118static struct perf_buffer *__perf_buffer__new(int map_fd, size_t page_cnt,
12119 struct perf_buffer_params *p);
12120
22dd7a58
AN
12121struct perf_buffer *perf_buffer__new(int map_fd, size_t page_cnt,
12122 perf_buffer_sample_fn sample_cb,
12123 perf_buffer_lost_fn lost_cb,
12124 void *ctx,
12125 const struct perf_buffer_opts *opts)
fb84b822 12126{
813847a3 12127 const size_t attr_sz = sizeof(struct perf_event_attr);
fb84b822 12128 struct perf_buffer_params p = {};
813847a3 12129 struct perf_event_attr attr;
ab8684b8 12130 __u32 sample_period;
41788934
AN
12131
12132 if (!OPTS_VALID(opts, perf_buffer_opts))
12133 return libbpf_err_ptr(-EINVAL);
4be6e05c 12134
ab8684b8
JD
12135 sample_period = OPTS_GET(opts, sample_period, 1);
12136 if (!sample_period)
12137 sample_period = 1;
12138
813847a3
AN
12139 memset(&attr, 0, attr_sz);
12140 attr.size = attr_sz;
65bb2e0f 12141 attr.config = PERF_COUNT_SW_BPF_OUTPUT;
4be6e05c
ACM
12142 attr.type = PERF_TYPE_SOFTWARE;
12143 attr.sample_type = PERF_SAMPLE_RAW;
ab8684b8
JD
12144 attr.sample_period = sample_period;
12145 attr.wakeup_events = sample_period;
fb84b822
AN
12146
12147 p.attr = &attr;
41788934
AN
12148 p.sample_cb = sample_cb;
12149 p.lost_cb = lost_cb;
12150 p.ctx = ctx;
fb84b822 12151
e9fc3ce9 12152 return libbpf_ptr(__perf_buffer__new(map_fd, page_cnt, &p));
fb84b822
AN
12153}
12154
22dd7a58
AN
12155struct perf_buffer *perf_buffer__new_raw(int map_fd, size_t page_cnt,
12156 struct perf_event_attr *attr,
12157 perf_buffer_event_fn event_cb, void *ctx,
12158 const struct perf_buffer_raw_opts *opts)
fb84b822
AN
12159{
12160 struct perf_buffer_params p = {};
12161
41332d6e 12162 if (!attr)
41788934
AN
12163 return libbpf_err_ptr(-EINVAL);
12164
12165 if (!OPTS_VALID(opts, perf_buffer_raw_opts))
12166 return libbpf_err_ptr(-EINVAL);
12167
12168 p.attr = attr;
12169 p.event_cb = event_cb;
12170 p.ctx = ctx;
12171 p.cpu_cnt = OPTS_GET(opts, cpu_cnt, 0);
12172 p.cpus = OPTS_GET(opts, cpus, NULL);
12173 p.map_keys = OPTS_GET(opts, map_keys, NULL);
fb84b822 12174
e9fc3ce9 12175 return libbpf_ptr(__perf_buffer__new(map_fd, page_cnt, &p));
fb84b822
AN
12176}
12177
12178static struct perf_buffer *__perf_buffer__new(int map_fd, size_t page_cnt,
12179 struct perf_buffer_params *p)
12180{
783b8f01 12181 const char *online_cpus_file = "/sys/devices/system/cpu/online";
0e289487 12182 struct bpf_map_info map;
fb84b822
AN
12183 char msg[STRERR_BUFSIZE];
12184 struct perf_buffer *pb;
783b8f01 12185 bool *online = NULL;
fb84b822 12186 __u32 map_info_len;
783b8f01 12187 int err, i, j, n;
fb84b822 12188
41332d6e 12189 if (page_cnt == 0 || (page_cnt & (page_cnt - 1))) {
be18010e
KW
12190 pr_warn("page count should be power of two, but is %zu\n",
12191 page_cnt);
fb84b822
AN
12192 return ERR_PTR(-EINVAL);
12193 }
12194
0e289487
AN
12195 /* best-effort sanity checks */
12196 memset(&map, 0, sizeof(map));
fb84b822 12197 map_info_len = sizeof(map);
629dfc66 12198 err = bpf_map_get_info_by_fd(map_fd, &map, &map_info_len);
fb84b822
AN
12199 if (err) {
12200 err = -errno;
0e289487
AN
12201 /* if BPF_OBJ_GET_INFO_BY_FD is supported, will return
12202 * -EBADFD, -EFAULT, or -E2BIG on real error
12203 */
12204 if (err != -EINVAL) {
12205 pr_warn("failed to get map info for map FD %d: %s\n",
12206 map_fd, libbpf_strerror_r(err, msg, sizeof(msg)));
12207 return ERR_PTR(err);
12208 }
12209 pr_debug("failed to get map info for FD %d; API not supported? Ignoring...\n",
12210 map_fd);
12211 } else {
12212 if (map.type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) {
12213 pr_warn("map '%s' should be BPF_MAP_TYPE_PERF_EVENT_ARRAY\n",
12214 map.name);
12215 return ERR_PTR(-EINVAL);
12216 }
fb84b822
AN
12217 }
12218
12219 pb = calloc(1, sizeof(*pb));
12220 if (!pb)
12221 return ERR_PTR(-ENOMEM);
12222
12223 pb->event_cb = p->event_cb;
12224 pb->sample_cb = p->sample_cb;
12225 pb->lost_cb = p->lost_cb;
12226 pb->ctx = p->ctx;
12227
12228 pb->page_size = getpagesize();
12229 pb->mmap_size = pb->page_size * page_cnt;
12230 pb->map_fd = map_fd;
12231
12232 pb->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
12233 if (pb->epoll_fd < 0) {
12234 err = -errno;
be18010e
KW
12235 pr_warn("failed to create epoll instance: %s\n",
12236 libbpf_strerror_r(err, msg, sizeof(msg)));
fb84b822
AN
12237 goto error;
12238 }
12239
12240 if (p->cpu_cnt > 0) {
12241 pb->cpu_cnt = p->cpu_cnt;
12242 } else {
12243 pb->cpu_cnt = libbpf_num_possible_cpus();
12244 if (pb->cpu_cnt < 0) {
12245 err = pb->cpu_cnt;
12246 goto error;
12247 }
0e289487 12248 if (map.max_entries && map.max_entries < pb->cpu_cnt)
fb84b822
AN
12249 pb->cpu_cnt = map.max_entries;
12250 }
12251
12252 pb->events = calloc(pb->cpu_cnt, sizeof(*pb->events));
12253 if (!pb->events) {
12254 err = -ENOMEM;
be18010e 12255 pr_warn("failed to allocate events: out of memory\n");
fb84b822
AN
12256 goto error;
12257 }
12258 pb->cpu_bufs = calloc(pb->cpu_cnt, sizeof(*pb->cpu_bufs));
12259 if (!pb->cpu_bufs) {
12260 err = -ENOMEM;
be18010e 12261 pr_warn("failed to allocate buffers: out of memory\n");
fb84b822
AN
12262 goto error;
12263 }
12264
783b8f01
AN
12265 err = parse_cpu_mask_file(online_cpus_file, &online, &n);
12266 if (err) {
12267 pr_warn("failed to get online CPU mask: %d\n", err);
12268 goto error;
12269 }
12270
12271 for (i = 0, j = 0; i < pb->cpu_cnt; i++) {
fb84b822
AN
12272 struct perf_cpu_buf *cpu_buf;
12273 int cpu, map_key;
12274
12275 cpu = p->cpu_cnt > 0 ? p->cpus[i] : i;
12276 map_key = p->cpu_cnt > 0 ? p->map_keys[i] : i;
12277
783b8f01
AN
12278 /* in case user didn't explicitly requested particular CPUs to
12279 * be attached to, skip offline/not present CPUs
12280 */
12281 if (p->cpu_cnt <= 0 && (cpu >= n || !online[cpu]))
12282 continue;
12283
fb84b822
AN
12284 cpu_buf = perf_buffer__open_cpu_buf(pb, p->attr, cpu, map_key);
12285 if (IS_ERR(cpu_buf)) {
12286 err = PTR_ERR(cpu_buf);
12287 goto error;
12288 }
12289
783b8f01 12290 pb->cpu_bufs[j] = cpu_buf;
fb84b822
AN
12291
12292 err = bpf_map_update_elem(pb->map_fd, &map_key,
12293 &cpu_buf->fd, 0);
12294 if (err) {
12295 err = -errno;
be18010e
KW
12296 pr_warn("failed to set cpu #%d, key %d -> perf FD %d: %s\n",
12297 cpu, map_key, cpu_buf->fd,
12298 libbpf_strerror_r(err, msg, sizeof(msg)));
fb84b822
AN
12299 goto error;
12300 }
12301
783b8f01
AN
12302 pb->events[j].events = EPOLLIN;
12303 pb->events[j].data.ptr = cpu_buf;
fb84b822 12304 if (epoll_ctl(pb->epoll_fd, EPOLL_CTL_ADD, cpu_buf->fd,
783b8f01 12305 &pb->events[j]) < 0) {
fb84b822 12306 err = -errno;
be18010e
KW
12307 pr_warn("failed to epoll_ctl cpu #%d perf FD %d: %s\n",
12308 cpu, cpu_buf->fd,
12309 libbpf_strerror_r(err, msg, sizeof(msg)));
fb84b822
AN
12310 goto error;
12311 }
783b8f01 12312 j++;
fb84b822 12313 }
783b8f01
AN
12314 pb->cpu_cnt = j;
12315 free(online);
fb84b822
AN
12316
12317 return pb;
12318
12319error:
783b8f01 12320 free(online);
fb84b822
AN
12321 if (pb)
12322 perf_buffer__free(pb);
12323 return ERR_PTR(err);
12324}
12325
12326struct perf_sample_raw {
12327 struct perf_event_header header;
12328 uint32_t size;
385bbf7b 12329 char data[];
fb84b822
AN
12330};
12331
12332struct perf_sample_lost {
12333 struct perf_event_header header;
12334 uint64_t id;
12335 uint64_t lost;
12336 uint64_t sample_id;
12337};
12338
12339static enum bpf_perf_event_ret
12340perf_buffer__process_record(struct perf_event_header *e, void *ctx)
12341{
12342 struct perf_cpu_buf *cpu_buf = ctx;
12343 struct perf_buffer *pb = cpu_buf->pb;
12344 void *data = e;
12345
12346 /* user wants full control over parsing perf event */
12347 if (pb->event_cb)
12348 return pb->event_cb(pb->ctx, cpu_buf->cpu, e);
12349
12350 switch (e->type) {
12351 case PERF_RECORD_SAMPLE: {
12352 struct perf_sample_raw *s = data;
12353
12354 if (pb->sample_cb)
12355 pb->sample_cb(pb->ctx, cpu_buf->cpu, s->data, s->size);
12356 break;
12357 }
12358 case PERF_RECORD_LOST: {
12359 struct perf_sample_lost *s = data;
12360
12361 if (pb->lost_cb)
12362 pb->lost_cb(pb->ctx, cpu_buf->cpu, s->lost);
12363 break;
12364 }
12365 default:
be18010e 12366 pr_warn("unknown perf sample type %d\n", e->type);
fb84b822
AN
12367 return LIBBPF_PERF_EVENT_ERROR;
12368 }
12369 return LIBBPF_PERF_EVENT_CONT;
12370}
12371
12372static int perf_buffer__process_records(struct perf_buffer *pb,
12373 struct perf_cpu_buf *cpu_buf)
12374{
12375 enum bpf_perf_event_ret ret;
12376
7218c28c
CL
12377 ret = perf_event_read_simple(cpu_buf->base, pb->mmap_size,
12378 pb->page_size, &cpu_buf->buf,
12379 &cpu_buf->buf_size,
12380 perf_buffer__process_record, cpu_buf);
fb84b822
AN
12381 if (ret != LIBBPF_PERF_EVENT_CONT)
12382 return ret;
12383 return 0;
12384}
12385
dca5612f
AN
12386int perf_buffer__epoll_fd(const struct perf_buffer *pb)
12387{
12388 return pb->epoll_fd;
12389}
12390
fb84b822
AN
12391int perf_buffer__poll(struct perf_buffer *pb, int timeout_ms)
12392{
12393 int i, cnt, err;
12394
12395 cnt = epoll_wait(pb->epoll_fd, pb->events, pb->cpu_cnt, timeout_ms);
e9fc3ce9 12396 if (cnt < 0)
af0efa05 12397 return -errno;
e9fc3ce9 12398
fb84b822
AN
12399 for (i = 0; i < cnt; i++) {
12400 struct perf_cpu_buf *cpu_buf = pb->events[i].data.ptr;
12401
12402 err = perf_buffer__process_records(pb, cpu_buf);
12403 if (err) {
be18010e 12404 pr_warn("error while processing records: %d\n", err);
e9fc3ce9 12405 return libbpf_err(err);
fb84b822
AN
12406 }
12407 }
e9fc3ce9 12408 return cnt;
fb84b822
AN
12409}
12410
dca5612f
AN
12411/* Return number of PERF_EVENT_ARRAY map slots set up by this perf_buffer
12412 * manager.
12413 */
12414size_t perf_buffer__buffer_cnt(const struct perf_buffer *pb)
12415{
12416 return pb->cpu_cnt;
12417}
12418
12419/*
12420 * Return perf_event FD of a ring buffer in *buf_idx* slot of
12421 * PERF_EVENT_ARRAY BPF map. This FD can be polled for new data using
12422 * select()/poll()/epoll() Linux syscalls.
12423 */
12424int perf_buffer__buffer_fd(const struct perf_buffer *pb, size_t buf_idx)
12425{
12426 struct perf_cpu_buf *cpu_buf;
12427
12428 if (buf_idx >= pb->cpu_cnt)
e9fc3ce9 12429 return libbpf_err(-EINVAL);
dca5612f
AN
12430
12431 cpu_buf = pb->cpu_bufs[buf_idx];
12432 if (!cpu_buf)
e9fc3ce9 12433 return libbpf_err(-ENOENT);
dca5612f
AN
12434
12435 return cpu_buf->fd;
12436}
12437
9ff5efde
JD
12438int perf_buffer__buffer(struct perf_buffer *pb, int buf_idx, void **buf, size_t *buf_size)
12439{
12440 struct perf_cpu_buf *cpu_buf;
12441
12442 if (buf_idx >= pb->cpu_cnt)
12443 return libbpf_err(-EINVAL);
12444
12445 cpu_buf = pb->cpu_bufs[buf_idx];
12446 if (!cpu_buf)
12447 return libbpf_err(-ENOENT);
12448
12449 *buf = cpu_buf->base;
12450 *buf_size = pb->mmap_size;
12451 return 0;
12452}
12453
dca5612f
AN
12454/*
12455 * Consume data from perf ring buffer corresponding to slot *buf_idx* in
12456 * PERF_EVENT_ARRAY BPF map without waiting/polling. If there is no data to
12457 * consume, do nothing and return success.
12458 * Returns:
12459 * - 0 on success;
12460 * - <0 on failure.
12461 */
12462int perf_buffer__consume_buffer(struct perf_buffer *pb, size_t buf_idx)
12463{
12464 struct perf_cpu_buf *cpu_buf;
12465
12466 if (buf_idx >= pb->cpu_cnt)
e9fc3ce9 12467 return libbpf_err(-EINVAL);
dca5612f
AN
12468
12469 cpu_buf = pb->cpu_bufs[buf_idx];
12470 if (!cpu_buf)
e9fc3ce9 12471 return libbpf_err(-ENOENT);
dca5612f
AN
12472
12473 return perf_buffer__process_records(pb, cpu_buf);
12474}
12475
272d51af
EC
12476int perf_buffer__consume(struct perf_buffer *pb)
12477{
12478 int i, err;
12479
12480 for (i = 0; i < pb->cpu_cnt; i++) {
12481 struct perf_cpu_buf *cpu_buf = pb->cpu_bufs[i];
12482
12483 if (!cpu_buf)
12484 continue;
12485
12486 err = perf_buffer__process_records(pb, cpu_buf);
12487 if (err) {
dca5612f 12488 pr_warn("perf_buffer: failed to process records in buffer #%d: %d\n", i, err);
e9fc3ce9 12489 return libbpf_err(err);
272d51af
EC
12490 }
12491 }
12492 return 0;
12493}
12494
ff26ce5c
EC
12495int bpf_program__set_attach_target(struct bpf_program *prog,
12496 int attach_prog_fd,
12497 const char *attach_func_name)
12498{
fe62de31 12499 int btf_obj_fd = 0, btf_id = 0, err;
ff26ce5c 12500
2d5ec1c6 12501 if (!prog || attach_prog_fd < 0)
e9fc3ce9 12502 return libbpf_err(-EINVAL);
ff26ce5c 12503
fe62de31 12504 if (prog->obj->loaded)
e9fc3ce9 12505 return libbpf_err(-EINVAL);
fe62de31 12506
2d5ec1c6
AN
12507 if (attach_prog_fd && !attach_func_name) {
12508 /* remember attach_prog_fd and let bpf_program__load() find
12509 * BTF ID during the program load
12510 */
12511 prog->attach_prog_fd = attach_prog_fd;
12512 return 0;
12513 }
12514
fe62de31 12515 if (attach_prog_fd) {
ff26ce5c
EC
12516 btf_id = libbpf_find_prog_btf_id(attach_func_name,
12517 attach_prog_fd);
fe62de31 12518 if (btf_id < 0)
e9fc3ce9 12519 return libbpf_err(btf_id);
fe62de31 12520 } else {
2d5ec1c6
AN
12521 if (!attach_func_name)
12522 return libbpf_err(-EINVAL);
12523
fe62de31
AN
12524 /* load btf_vmlinux, if not yet */
12525 err = bpf_object__load_vmlinux_btf(prog->obj, true);
12526 if (err)
e9fc3ce9 12527 return libbpf_err(err);
fe62de31
AN
12528 err = find_kernel_btf_id(prog->obj, attach_func_name,
12529 prog->expected_attach_type,
12530 &btf_obj_fd, &btf_id);
12531 if (err)
e9fc3ce9 12532 return libbpf_err(err);
fe62de31 12533 }
ff26ce5c
EC
12534
12535 prog->attach_btf_id = btf_id;
fe62de31 12536 prog->attach_btf_obj_fd = btf_obj_fd;
ff26ce5c
EC
12537 prog->attach_prog_fd = attach_prog_fd;
12538 return 0;
12539}
12540
6803ee25 12541int parse_cpu_mask_str(const char *s, bool **mask, int *mask_sz)
6446b315 12542{
6803ee25
AN
12543 int err = 0, n, len, start, end = -1;
12544 bool *tmp;
6446b315 12545
6803ee25
AN
12546 *mask = NULL;
12547 *mask_sz = 0;
12548
12549 /* Each sub string separated by ',' has format \d+-\d+ or \d+ */
12550 while (*s) {
12551 if (*s == ',' || *s == '\n') {
12552 s++;
12553 continue;
12554 }
12555 n = sscanf(s, "%d%n-%d%n", &start, &len, &end, &len);
12556 if (n <= 0 || n > 2) {
12557 pr_warn("Failed to get CPU range %s: %d\n", s, n);
12558 err = -EINVAL;
12559 goto cleanup;
12560 } else if (n == 1) {
12561 end = start;
12562 }
12563 if (start < 0 || start > end) {
12564 pr_warn("Invalid CPU range [%d,%d] in %s\n",
12565 start, end, s);
12566 err = -EINVAL;
12567 goto cleanup;
12568 }
12569 tmp = realloc(*mask, end + 1);
12570 if (!tmp) {
12571 err = -ENOMEM;
12572 goto cleanup;
12573 }
12574 *mask = tmp;
12575 memset(tmp + *mask_sz, 0, start - *mask_sz);
12576 memset(tmp + start, 1, end - start + 1);
12577 *mask_sz = end + 1;
12578 s += len;
12579 }
12580 if (!*mask_sz) {
12581 pr_warn("Empty CPU range\n");
12582 return -EINVAL;
12583 }
12584 return 0;
12585cleanup:
12586 free(*mask);
12587 *mask = NULL;
12588 return err;
12589}
12590
12591int parse_cpu_mask_file(const char *fcpu, bool **mask, int *mask_sz)
12592{
12593 int fd, err = 0, len;
12594 char buf[128];
6446b315 12595
92274e24 12596 fd = open(fcpu, O_RDONLY | O_CLOEXEC);
6446b315 12597 if (fd < 0) {
6803ee25
AN
12598 err = -errno;
12599 pr_warn("Failed to open cpu mask file %s: %d\n", fcpu, err);
12600 return err;
6446b315
HL
12601 }
12602 len = read(fd, buf, sizeof(buf));
12603 close(fd);
12604 if (len <= 0) {
6803ee25
AN
12605 err = len ? -errno : -EINVAL;
12606 pr_warn("Failed to read cpu mask from %s: %d\n", fcpu, err);
12607 return err;
6446b315 12608 }
6803ee25
AN
12609 if (len >= sizeof(buf)) {
12610 pr_warn("CPU mask is too big in file %s\n", fcpu);
12611 return -E2BIG;
6446b315
HL
12612 }
12613 buf[len] = '\0';
12614
6803ee25
AN
12615 return parse_cpu_mask_str(buf, mask, mask_sz);
12616}
12617
12618int libbpf_num_possible_cpus(void)
12619{
12620 static const char *fcpu = "/sys/devices/system/cpu/possible";
12621 static int cpus;
12622 int err, n, i, tmp_cpus;
12623 bool *mask;
12624
12625 tmp_cpus = READ_ONCE(cpus);
12626 if (tmp_cpus > 0)
12627 return tmp_cpus;
12628
12629 err = parse_cpu_mask_file(fcpu, &mask, &n);
12630 if (err)
e9fc3ce9 12631 return libbpf_err(err);
6803ee25
AN
12632
12633 tmp_cpus = 0;
12634 for (i = 0; i < n; i++) {
12635 if (mask[i])
12636 tmp_cpus++;
6446b315 12637 }
6803ee25 12638 free(mask);
56fbc241
TC
12639
12640 WRITE_ONCE(cpus, tmp_cpus);
12641 return tmp_cpus;
6446b315 12642}
d66562fb 12643
430025e5
DK
12644static int populate_skeleton_maps(const struct bpf_object *obj,
12645 struct bpf_map_skeleton *maps,
12646 size_t map_cnt)
12647{
12648 int i;
12649
12650 for (i = 0; i < map_cnt; i++) {
12651 struct bpf_map **map = maps[i].map;
12652 const char *name = maps[i].name;
12653 void **mmaped = maps[i].mmaped;
12654
12655 *map = bpf_object__find_map_by_name(obj, name);
12656 if (!*map) {
12657 pr_warn("failed to find skeleton map '%s'\n", name);
12658 return -ESRCH;
12659 }
12660
12661 /* externs shouldn't be pre-setup from user code */
12662 if (mmaped && (*map)->libbpf_type != LIBBPF_MAP_KCONFIG)
12663 *mmaped = (*map)->mmaped;
12664 }
12665 return 0;
12666}
12667
12668static int populate_skeleton_progs(const struct bpf_object *obj,
12669 struct bpf_prog_skeleton *progs,
12670 size_t prog_cnt)
12671{
12672 int i;
12673
12674 for (i = 0; i < prog_cnt; i++) {
12675 struct bpf_program **prog = progs[i].prog;
12676 const char *name = progs[i].name;
12677
12678 *prog = bpf_object__find_program_by_name(obj, name);
12679 if (!*prog) {
12680 pr_warn("failed to find skeleton program '%s'\n", name);
12681 return -ESRCH;
12682 }
12683 }
12684 return 0;
12685}
12686
d66562fb
AN
12687int bpf_object__open_skeleton(struct bpf_object_skeleton *s,
12688 const struct bpf_object_open_opts *opts)
12689{
12690 DECLARE_LIBBPF_OPTS(bpf_object_open_opts, skel_opts,
12691 .object_name = s->name,
12692 );
12693 struct bpf_object *obj;
430025e5 12694 int err;
d66562fb
AN
12695
12696 /* Attempt to preserve opts->object_name, unless overriden by user
12697 * explicitly. Overwriting object name for skeletons is discouraged,
12698 * as it breaks global data maps, because they contain object name
12699 * prefix as their own map name prefix. When skeleton is generated,
12700 * bpftool is making an assumption that this name will stay the same.
12701 */
12702 if (opts) {
12703 memcpy(&skel_opts, opts, sizeof(*opts));
12704 if (!opts->object_name)
12705 skel_opts.object_name = s->name;
12706 }
12707
12708 obj = bpf_object__open_mem(s->data, s->data_sz, &skel_opts);
e9fc3ce9
AN
12709 err = libbpf_get_error(obj);
12710 if (err) {
12711 pr_warn("failed to initialize skeleton BPF object '%s': %d\n",
12712 s->name, err);
12713 return libbpf_err(err);
d66562fb
AN
12714 }
12715
12716 *s->obj = obj;
430025e5
DK
12717 err = populate_skeleton_maps(obj, s->maps, s->map_cnt);
12718 if (err) {
12719 pr_warn("failed to populate skeleton maps for '%s': %d\n", s->name, err);
12720 return libbpf_err(err);
12721 }
12722
12723 err = populate_skeleton_progs(obj, s->progs, s->prog_cnt);
12724 if (err) {
12725 pr_warn("failed to populate skeleton progs for '%s': %d\n", s->name, err);
12726 return libbpf_err(err);
12727 }
d66562fb 12728
430025e5
DK
12729 return 0;
12730}
d66562fb 12731
430025e5
DK
12732int bpf_object__open_subskeleton(struct bpf_object_subskeleton *s)
12733{
12734 int err, len, var_idx, i;
12735 const char *var_name;
12736 const struct bpf_map *map;
12737 struct btf *btf;
12738 __u32 map_type_id;
12739 const struct btf_type *map_type, *var_type;
12740 const struct bpf_var_skeleton *var_skel;
12741 struct btf_var_secinfo *var;
d66562fb 12742
430025e5
DK
12743 if (!s->obj)
12744 return libbpf_err(-EINVAL);
12745
12746 btf = bpf_object__btf(s->obj);
12747 if (!btf) {
12748 pr_warn("subskeletons require BTF at runtime (object %s)\n",
e3ba8e4e 12749 bpf_object__name(s->obj));
430025e5 12750 return libbpf_err(-errno);
d66562fb
AN
12751 }
12752
430025e5
DK
12753 err = populate_skeleton_maps(s->obj, s->maps, s->map_cnt);
12754 if (err) {
12755 pr_warn("failed to populate subskeleton maps: %d\n", err);
12756 return libbpf_err(err);
12757 }
d66562fb 12758
430025e5
DK
12759 err = populate_skeleton_progs(s->obj, s->progs, s->prog_cnt);
12760 if (err) {
12761 pr_warn("failed to populate subskeleton maps: %d\n", err);
12762 return libbpf_err(err);
d66562fb
AN
12763 }
12764
430025e5
DK
12765 for (var_idx = 0; var_idx < s->var_cnt; var_idx++) {
12766 var_skel = &s->vars[var_idx];
12767 map = *var_skel->map;
12768 map_type_id = bpf_map__btf_value_type_id(map);
12769 map_type = btf__type_by_id(btf, map_type_id);
12770
12771 if (!btf_is_datasec(map_type)) {
12772 pr_warn("type for map '%1$s' is not a datasec: %2$s",
12773 bpf_map__name(map),
12774 __btf_kind_str(btf_kind(map_type)));
12775 return libbpf_err(-EINVAL);
12776 }
12777
12778 len = btf_vlen(map_type);
12779 var = btf_var_secinfos(map_type);
12780 for (i = 0; i < len; i++, var++) {
12781 var_type = btf__type_by_id(btf, var->type);
12782 var_name = btf__name_by_offset(btf, var_type->name_off);
12783 if (strcmp(var_name, var_skel->name) == 0) {
12784 *var_skel->addr = map->mmaped + var->offset;
12785 break;
12786 }
12787 }
12788 }
d66562fb
AN
12789 return 0;
12790}
12791
430025e5
DK
12792void bpf_object__destroy_subskeleton(struct bpf_object_subskeleton *s)
12793{
12794 if (!s)
12795 return;
12796 free(s->maps);
12797 free(s->progs);
12798 free(s->vars);
12799 free(s);
12800}
12801
d66562fb
AN
12802int bpf_object__load_skeleton(struct bpf_object_skeleton *s)
12803{
12804 int i, err;
12805
12806 err = bpf_object__load(*s->obj);
12807 if (err) {
12808 pr_warn("failed to load BPF skeleton '%s': %d\n", s->name, err);
e9fc3ce9 12809 return libbpf_err(err);
d66562fb
AN
12810 }
12811
12812 for (i = 0; i < s->map_cnt; i++) {
12813 struct bpf_map *map = *s->maps[i].map;
9d0a2331 12814 size_t mmap_sz = bpf_map_mmap_sz(map->def.value_size, map->def.max_entries);
d66562fb
AN
12815 int prot, map_fd = bpf_map__fd(map);
12816 void **mmaped = s->maps[i].mmaped;
d66562fb
AN
12817
12818 if (!mmaped)
12819 continue;
12820
12821 if (!(map->def.map_flags & BPF_F_MMAPABLE)) {
12822 *mmaped = NULL;
12823 continue;
12824 }
12825
12826 if (map->def.map_flags & BPF_F_RDONLY_PROG)
12827 prot = PROT_READ;
12828 else
12829 prot = PROT_READ | PROT_WRITE;
12830
12831 /* Remap anonymous mmap()-ed "map initialization image" as
12832 * a BPF map-backed mmap()-ed memory, but preserving the same
12833 * memory address. This will cause kernel to change process'
12834 * page table to point to a different piece of kernel memory,
12835 * but from userspace point of view memory address (and its
12836 * contents, being identical at this point) will stay the
12837 * same. This mapping will be released by bpf_object__close()
12838 * as per normal clean up procedure, so we don't need to worry
12839 * about it from skeleton's clean up perspective.
12840 */
9d0a2331 12841 *mmaped = mmap(map->mmaped, mmap_sz, prot, MAP_SHARED | MAP_FIXED, map_fd, 0);
2ad97d47 12842 if (*mmaped == MAP_FAILED) {
d66562fb
AN
12843 err = -errno;
12844 *mmaped = NULL;
12845 pr_warn("failed to re-mmap() map '%s': %d\n",
12846 bpf_map__name(map), err);
e9fc3ce9 12847 return libbpf_err(err);
d66562fb
AN
12848 }
12849 }
12850
12851 return 0;
12852}
12853
12854int bpf_object__attach_skeleton(struct bpf_object_skeleton *s)
12855{
e9fc3ce9 12856 int i, err;
d66562fb
AN
12857
12858 for (i = 0; i < s->prog_cnt; i++) {
12859 struct bpf_program *prog = *s->progs[i].prog;
12860 struct bpf_link **link = s->progs[i].link;
d66562fb 12861
43cb8cba 12862 if (!prog->autoload || !prog->autoattach)
d9297581
AN
12863 continue;
12864
5532dfd4 12865 /* auto-attaching not supported for this program */
4fa5bcfe 12866 if (!prog->sec_def || !prog->sec_def->prog_attach_fn)
d66562fb
AN
12867 continue;
12868
4fa5bcfe
AN
12869 /* if user already set the link manually, don't attempt auto-attach */
12870 if (*link)
12871 continue;
12872
12873 err = prog->sec_def->prog_attach_fn(prog, prog->sec_def->cookie, link);
e9fc3ce9 12874 if (err) {
4fa5bcfe 12875 pr_warn("prog '%s': failed to auto-attach: %d\n",
e9fc3ce9
AN
12876 bpf_program__name(prog), err);
12877 return libbpf_err(err);
d66562fb 12878 }
4fa5bcfe
AN
12879
12880 /* It's possible that for some SEC() definitions auto-attach
12881 * is supported in some cases (e.g., if definition completely
12882 * specifies target information), but is not in other cases.
12883 * SEC("uprobe") is one such case. If user specified target
12884 * binary and function name, such BPF program can be
12885 * auto-attached. But if not, it shouldn't trigger skeleton's
12886 * attach to fail. It should just be skipped.
12887 * attach_fn signals such case with returning 0 (no error) and
12888 * setting link to NULL.
12889 */
d66562fb
AN
12890 }
12891
12892 return 0;
12893}
12894
12895void bpf_object__detach_skeleton(struct bpf_object_skeleton *s)
12896{
12897 int i;
12898
12899 for (i = 0; i < s->prog_cnt; i++) {
12900 struct bpf_link **link = s->progs[i].link;
12901
50450fc7 12902 bpf_link__destroy(*link);
d66562fb
AN
12903 *link = NULL;
12904 }
12905}
12906
12907void bpf_object__destroy_skeleton(struct bpf_object_skeleton *s)
12908{
a32ea51a
YS
12909 if (!s)
12910 return;
12911
d66562fb
AN
12912 if (s->progs)
12913 bpf_object__detach_skeleton(s);
12914 if (s->obj)
12915 bpf_object__close(*s->obj);
12916 free(s->maps);
12917 free(s->progs);
12918 free(s);
12919}