bpf: Add bloom filter map implementation
[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>
9a208eff 34#include <linux/list.h>
f367540c 35#include <linux/limits.h>
438363c0 36#include <linux/perf_event.h>
a64af0ef 37#include <linux/ring_buffer.h>
5e61f270 38#include <linux/version.h>
fb84b822 39#include <sys/epoll.h>
63f2f5ee 40#include <sys/ioctl.h>
fb84b822 41#include <sys/mman.h>
f367540c
JS
42#include <sys/stat.h>
43#include <sys/types.h>
44#include <sys/vfs.h>
ddc7c304 45#include <sys/utsname.h>
dc3a2d25 46#include <sys/resource.h>
1a5e3fb1
WN
47#include <libelf.h>
48#include <gelf.h>
166750bc 49#include <zlib.h>
1b76c13e
WN
50
51#include "libbpf.h"
52d3352e 52#include "bpf.h"
8a138aed 53#include "btf.h"
6d41907c 54#include "str_error.h"
d7c4b398 55#include "libbpf_internal.h"
ddc7c304 56#include "hashmap.h"
67234743 57#include "bpf_gen_internal.h"
b3f59d66 58
f367540c
JS
59#ifndef BPF_FS_MAGIC
60#define BPF_FS_MAGIC 0xcafe4a11
61#endif
62
9c0f8cbd
AN
63#define BPF_INSN_SZ (sizeof(struct bpf_insn))
64
ff466b58
AI
65/* vsprintf() in __base_pr() uses nonliteral format string. It may break
66 * compilation if user enables corresponding warning. Disable it explicitly.
67 */
68#pragma GCC diagnostic ignored "-Wformat-nonliteral"
69
b3f59d66
WN
70#define __printf(a, b) __attribute__((format(printf, a, b)))
71
590a0088 72static struct bpf_map *bpf_object__add_map(struct bpf_object *obj);
aea28a60 73static bool prog_is_subprog(const struct bpf_object *obj, const struct bpf_program *prog);
590a0088 74
a8a1f7d0
SF
75static int __base_pr(enum libbpf_print_level level, const char *format,
76 va_list args)
b3f59d66 77{
6f1ae8b6
YS
78 if (level == LIBBPF_DEBUG)
79 return 0;
80
a8a1f7d0 81 return vfprintf(stderr, format, args);
b3f59d66
WN
82}
83
a8a1f7d0 84static libbpf_print_fn_t __libbpf_pr = __base_pr;
b3f59d66 85
e87fd8ba 86libbpf_print_fn_t libbpf_set_print(libbpf_print_fn_t fn)
b3f59d66 87{
e87fd8ba
AN
88 libbpf_print_fn_t old_print_fn = __libbpf_pr;
89
6f1ae8b6 90 __libbpf_pr = fn;
e87fd8ba 91 return old_print_fn;
b3f59d66 92}
1a5e3fb1 93
8461ef8b
YS
94__printf(2, 3)
95void libbpf_print(enum libbpf_print_level level, const char *format, ...)
96{
97 va_list args;
98
6f1ae8b6
YS
99 if (!__libbpf_pr)
100 return;
101
8461ef8b 102 va_start(args, format);
6f1ae8b6 103 __libbpf_pr(level, format, args);
8461ef8b
YS
104 va_end(args);
105}
106
dc3a2d25
THJ
107static void pr_perm_msg(int err)
108{
109 struct rlimit limit;
110 char buf[100];
111
112 if (err != -EPERM || geteuid() != 0)
113 return;
114
115 err = getrlimit(RLIMIT_MEMLOCK, &limit);
116 if (err)
117 return;
118
119 if (limit.rlim_cur == RLIM_INFINITY)
120 return;
121
122 if (limit.rlim_cur < 1024)
b5c7d0d0 123 snprintf(buf, sizeof(buf), "%zu bytes", (size_t)limit.rlim_cur);
dc3a2d25
THJ
124 else if (limit.rlim_cur < 1024*1024)
125 snprintf(buf, sizeof(buf), "%.1f KiB", (double)limit.rlim_cur / 1024);
126 else
127 snprintf(buf, sizeof(buf), "%.1f MiB", (double)limit.rlim_cur / (1024*1024));
128
129 pr_warn("permission error while running as root; try raising 'ulimit -l'? current value: %s\n",
130 buf);
131}
132
6371ca3b
WN
133#define STRERR_BUFSIZE 128
134
1a5e3fb1
WN
135/* Copied from tools/perf/util/util.h */
136#ifndef zfree
137# define zfree(ptr) ({ free(*ptr); *ptr = NULL; })
138#endif
139
140#ifndef zclose
141# define zclose(fd) ({ \
142 int ___err = 0; \
143 if ((fd) >= 0) \
144 ___err = close((fd)); \
145 fd = -1; \
146 ___err; })
147#endif
148
34be1646
SL
149static inline __u64 ptr_to_u64(const void *ptr)
150{
151 return (__u64) (unsigned long) ptr;
152}
153
5981881d
AN
154/* this goes away in libbpf 1.0 */
155enum libbpf_strict_mode libbpf_mode = LIBBPF_STRICT_NONE;
156
157int libbpf_set_strict_mode(enum libbpf_strict_mode mode)
158{
159 /* __LIBBPF_STRICT_LAST is the last power-of-2 value used + 1, so to
160 * get all possible values we compensate last +1, and then (2*x - 1)
161 * to get the bit mask
162 */
163 if (mode != LIBBPF_STRICT_ALL
164 && (mode & ~((__LIBBPF_STRICT_LAST - 1) * 2 - 1)))
165 return errno = EINVAL, -EINVAL;
166
167 libbpf_mode = mode;
168 return 0;
169}
170
47b6cb4d 171enum kern_feature_id {
47eff617 172 /* v4.14: kernel support for program & map names. */
47b6cb4d 173 FEAT_PROG_NAME,
8837fe5d 174 /* v5.2: kernel support for global data sections. */
47b6cb4d 175 FEAT_GLOBAL_DATA,
68b08647
AN
176 /* BTF support */
177 FEAT_BTF,
d7c4b398 178 /* BTF_KIND_FUNC and BTF_KIND_FUNC_PROTO support */
47b6cb4d 179 FEAT_BTF_FUNC,
d7c4b398 180 /* BTF_KIND_VAR and BTF_KIND_DATASEC support */
47b6cb4d 181 FEAT_BTF_DATASEC,
2d3eb67f 182 /* BTF_FUNC_GLOBAL is supported */
47b6cb4d
AN
183 FEAT_BTF_GLOBAL_FUNC,
184 /* BPF_F_MMAPABLE is supported for arrays */
185 FEAT_ARRAY_MMAP,
25498a19 186 /* kernel support for expected_attach_type in BPF_PROG_LOAD */
47b6cb4d 187 FEAT_EXP_ATTACH_TYPE,
109cea5a
AN
188 /* bpf_probe_read_{kernel,user}[_str] helpers */
189 FEAT_PROBE_READ_KERN,
5d23328d
YZ
190 /* BPF_PROG_BIND_MAP is supported */
191 FEAT_PROG_BIND_MAP,
4f33a53d
AN
192 /* Kernel support for module BTFs */
193 FEAT_MODULE_BTF,
22541a9e
IL
194 /* BTF_KIND_FLOAT support */
195 FEAT_BTF_FLOAT,
668ace0e
AN
196 /* BPF perf link support */
197 FEAT_PERF_LINK,
223f903e
YS
198 /* BTF_KIND_DECL_TAG support */
199 FEAT_BTF_DECL_TAG,
47b6cb4d 200 __FEAT_CNT,
47eff617
SF
201};
202
9ca1f56a 203static bool kernel_supports(const struct bpf_object *obj, enum kern_feature_id feat_id);
47b6cb4d 204
166750bc
AN
205enum reloc_type {
206 RELO_LD64,
207 RELO_CALL,
208 RELO_DATA,
0c091e5c 209 RELO_EXTERN_VAR,
5bd022ec 210 RELO_EXTERN_FUNC,
53eddb5e 211 RELO_SUBPROG_ADDR,
166750bc
AN
212};
213
214struct reloc_desc {
215 enum reloc_type type;
216 int insn_idx;
217 int map_idx;
218 int sym_off;
219};
220
25498a19
AN
221struct bpf_sec_def;
222
12d9466d
AN
223typedef int (*init_fn_t)(struct bpf_program *prog, long cookie);
224typedef int (*preload_fn_t)(struct bpf_program *prog, struct bpf_prog_load_params *attr, long cookie);
225typedef struct bpf_link *(*attach_fn_t)(const struct bpf_program *prog, long cookie);
25498a19 226
15ea31fa
AN
227/* stored as sec_def->cookie for all libbpf-supported SEC()s */
228enum sec_def_flags {
229 SEC_NONE = 0,
230 /* expected_attach_type is optional, if kernel doesn't support that */
231 SEC_EXP_ATTACH_OPT = 1,
232 /* legacy, only used by libbpf_get_type_names() and
233 * libbpf_attach_type_by_name(), not used by libbpf itself at all.
234 * This used to be associated with cgroup (and few other) BPF programs
235 * that were attachable through BPF_PROG_ATTACH command. Pretty
236 * meaningless nowadays, though.
237 */
238 SEC_ATTACHABLE = 2,
239 SEC_ATTACHABLE_OPT = SEC_ATTACHABLE | SEC_EXP_ATTACH_OPT,
240 /* attachment target is specified through BTF ID in either kernel or
241 * other BPF program's BTF object */
242 SEC_ATTACH_BTF = 4,
243 /* BPF program type allows sleeping/blocking in kernel */
244 SEC_SLEEPABLE = 8,
dd94d45c
AN
245 /* allow non-strict prefix matching */
246 SEC_SLOPPY_PFX = 16,
15ea31fa
AN
247};
248
25498a19
AN
249struct bpf_sec_def {
250 const char *sec;
25498a19
AN
251 enum bpf_prog_type prog_type;
252 enum bpf_attach_type expected_attach_type;
15ea31fa 253 long cookie;
12d9466d
AN
254
255 init_fn_t init_fn;
256 preload_fn_t preload_fn;
25498a19
AN
257 attach_fn_t attach_fn;
258};
259
a5b8bd47
WN
260/*
261 * bpf_prog should be a better name but it has been used in
262 * linux/filter.h.
263 */
264struct bpf_program {
25498a19 265 const struct bpf_sec_def *sec_def;
52109584 266 char *sec_name;
c1122392
AN
267 size_t sec_idx;
268 /* this program's instruction offset (in number of instructions)
269 * within its containing ELF section
270 */
271 size_t sec_insn_off;
272 /* number of original instructions in ELF section belonging to this
273 * program, not taking into account subprogram instructions possible
274 * appended later during relocation
275 */
276 size_t sec_insn_cnt;
277 /* Offset (in number of instructions) of the start of instruction
278 * belonging to this BPF program within its containing main BPF
279 * program. For the entry-point (main) BPF program, this is always
280 * zero. For a sub-program, this gets reset before each of main BPF
281 * programs are processed and relocated and is used to determined
282 * whether sub-program was already appended to the main program, and
283 * if yes, at which instruction offset.
284 */
285 size_t sub_insn_off;
286
287 char *name;
a77f879b 288 /* name with / replaced by _; makes recursive pinning
33a2c75c
SF
289 * in bpf_object__pin_programs easier
290 */
291 char *pin_name;
c1122392
AN
292
293 /* instructions that belong to BPF program; insns[0] is located at
294 * sec_insn_off instruction within its ELF section in ELF file, so
295 * when mapping ELF file instruction index to the local instruction,
296 * one needs to subtract sec_insn_off; and vice versa.
297 */
a5b8bd47 298 struct bpf_insn *insns;
c1122392
AN
299 /* actual number of instruction in this BPF program's image; for
300 * entry-point BPF programs this includes the size of main program
301 * itself plus all the used sub-programs, appended at the end
302 */
c3c55696 303 size_t insns_cnt;
34090915 304
166750bc 305 struct reloc_desc *reloc_desc;
34090915 306 int nr_reloc;
da11b417 307 int log_level;
55cffde2 308
b580563e
WN
309 struct {
310 int nr;
311 int *fds;
312 } instances;
313 bpf_program_prep_t preprocessor;
aa9b1ac3
WN
314
315 struct bpf_object *obj;
316 void *priv;
317 bpf_program_clear_priv_t clear_priv;
d7be143b 318
c1122392 319 bool load;
aea28a60 320 bool mark_btf_static;
c1122392 321 enum bpf_prog_type type;
d7be143b 322 enum bpf_attach_type expected_attach_type;
c1122392 323 int prog_ifindex;
91abb4a6 324 __u32 attach_btf_obj_fd;
12a8654b 325 __u32 attach_btf_id;
e7bf94db 326 __u32 attach_prog_fd;
2993e051
YS
327 void *func_info;
328 __u32 func_info_rec_size;
f0187f0b 329 __u32 func_info_cnt;
47eff617 330
3d650141
MKL
331 void *line_info;
332 __u32 line_info_rec_size;
333 __u32 line_info_cnt;
04656198 334 __u32 prog_flags;
a5b8bd47
WN
335};
336
590a0088
MKL
337struct bpf_struct_ops {
338 const char *tname;
339 const struct btf_type *type;
340 struct bpf_program **progs;
341 __u32 *kern_func_off;
342 /* e.g. struct tcp_congestion_ops in bpf_prog's btf format */
343 void *data;
344 /* e.g. struct bpf_struct_ops_tcp_congestion_ops in
345 * btf_vmlinux's format.
346 * struct bpf_struct_ops_tcp_congestion_ops {
347 * [... some other kernel fields ...]
348 * struct tcp_congestion_ops data;
349 * }
350 * kern_vdata-size == sizeof(struct bpf_struct_ops_tcp_congestion_ops)
351 * bpf_map__init_kern_struct_ops() will populate the "kern_vdata"
352 * from "data".
353 */
354 void *kern_vdata;
355 __u32 type_id;
356};
357
ac9d1389
AN
358#define DATA_SEC ".data"
359#define BSS_SEC ".bss"
360#define RODATA_SEC ".rodata"
81bfdd08 361#define KCONFIG_SEC ".kconfig"
1c0c7074 362#define KSYMS_SEC ".ksyms"
590a0088 363#define STRUCT_OPS_SEC ".struct_ops"
ac9d1389 364
d859900c
DB
365enum libbpf_map_type {
366 LIBBPF_MAP_UNSPEC,
367 LIBBPF_MAP_DATA,
368 LIBBPF_MAP_BSS,
369 LIBBPF_MAP_RODATA,
81bfdd08 370 LIBBPF_MAP_KCONFIG,
d859900c
DB
371};
372
9d759a9b 373struct bpf_map {
561bbcca 374 char *name;
aed65917
AN
375 /* real_name is defined for special internal maps (.rodata*,
376 * .data*, .bss, .kconfig) and preserves their original ELF section
377 * name. This is important to be be able to find corresponding BTF
378 * DATASEC information.
379 */
380 char *real_name;
01af3bf0 381 int fd;
db48814b
AN
382 int sec_idx;
383 size_t sec_offset;
f0307a7e 384 int map_ifindex;
addb9fc9 385 int inner_map_fd;
9d759a9b 386 struct bpf_map_def def;
1bdb6c9a 387 __u32 numa_node;
646f02ff 388 __u32 btf_var_idx;
5b891af7
MKL
389 __u32 btf_key_type_id;
390 __u32 btf_value_type_id;
590a0088 391 __u32 btf_vmlinux_value_type_id;
9d759a9b
WN
392 void *priv;
393 bpf_map_clear_priv_t clear_priv;
d859900c 394 enum libbpf_map_type libbpf_type;
eba9c5f4 395 void *mmaped;
590a0088 396 struct bpf_struct_ops *st_ops;
646f02ff
AN
397 struct bpf_map *inner_map;
398 void **init_slots;
399 int init_slots_sz;
4580b25f
THJ
400 char *pin_path;
401 bool pinned;
ec6d5f47 402 bool reused;
d859900c
DB
403};
404
166750bc
AN
405enum extern_type {
406 EXT_UNKNOWN,
2e33efe3 407 EXT_KCFG,
1c0c7074 408 EXT_KSYM,
2e33efe3
AN
409};
410
411enum kcfg_type {
412 KCFG_UNKNOWN,
413 KCFG_CHAR,
414 KCFG_BOOL,
415 KCFG_INT,
416 KCFG_TRISTATE,
417 KCFG_CHAR_ARR,
166750bc
AN
418};
419
420struct extern_desc {
2e33efe3 421 enum extern_type type;
166750bc
AN
422 int sym_idx;
423 int btf_id;
2e33efe3
AN
424 int sec_btf_id;
425 const char *name;
166750bc 426 bool is_set;
2e33efe3
AN
427 bool is_weak;
428 union {
429 struct {
430 enum kcfg_type type;
431 int sz;
432 int align;
433 int data_off;
434 bool is_signed;
435 } kcfg;
1c0c7074
AN
436 struct {
437 unsigned long long addr;
d370bbe1
HL
438
439 /* target btf_id of the corresponding kernel var. */
284d2587
AN
440 int kernel_btf_obj_fd;
441 int kernel_btf_id;
d370bbe1
HL
442
443 /* local btf_id of the ksym extern's type. */
444 __u32 type_id;
9dbe6015
KKD
445 /* BTF fd index to be patched in for insn->off, this is
446 * 0 for vmlinux BTF, index in obj->fd_array for module
447 * BTF
448 */
449 __s16 btf_fd_idx;
1c0c7074 450 } ksym;
2e33efe3 451 };
166750bc
AN
452};
453
9a208eff
WN
454static LIST_HEAD(bpf_objects_list);
455
4f33a53d
AN
456struct module_btf {
457 struct btf *btf;
458 char *name;
459 __u32 id;
91abb4a6 460 int fd;
9dbe6015 461 int fd_array_idx;
4f33a53d
AN
462};
463
25bbbd7a
AN
464enum sec_type {
465 SEC_UNUSED = 0,
466 SEC_RELO,
467 SEC_BSS,
468 SEC_DATA,
469 SEC_RODATA,
470};
471
472struct elf_sec_desc {
473 enum sec_type sec_type;
474 Elf64_Shdr *shdr;
475 Elf_Data *data;
476};
477
29a30ff5
AN
478struct elf_state {
479 int fd;
480 const void *obj_buf;
481 size_t obj_buf_sz;
482 Elf *elf;
ad23b723 483 Elf64_Ehdr *ehdr;
29a30ff5 484 Elf_Data *symbols;
29a30ff5
AN
485 Elf_Data *st_ops_data;
486 size_t shstrndx; /* section index for section name strings */
487 size_t strtabidx;
25bbbd7a
AN
488 struct elf_sec_desc *secs;
489 int sec_cnt;
29a30ff5
AN
490 int maps_shndx;
491 int btf_maps_shndx;
492 __u32 btf_maps_sec_btf_id;
493 int text_shndx;
494 int symbols_shndx;
29a30ff5
AN
495 int st_ops_shndx;
496};
497
1a5e3fb1 498struct bpf_object {
d859900c 499 char name[BPF_OBJ_NAME_LEN];
cb1e5e96 500 char license[64];
438363c0 501 __u32 kern_version;
0b3d1efa 502
a5b8bd47
WN
503 struct bpf_program *programs;
504 size_t nr_programs;
9d759a9b
WN
505 struct bpf_map *maps;
506 size_t nr_maps;
bf829271 507 size_t maps_cap;
9d759a9b 508
8601fd42 509 char *kconfig;
166750bc
AN
510 struct extern_desc *externs;
511 int nr_extern;
81bfdd08 512 int kconfig_map_idx;
166750bc 513
52d3352e 514 bool loaded;
c3c55696 515 bool has_subcalls;
25bbbd7a 516 bool has_rodata;
a5b8bd47 517
e2fa0156
AS
518 struct bpf_gen *gen_loader;
519
29a30ff5
AN
520 /* Information when doing ELF related work. Only valid if efile.elf is not NULL */
521 struct elf_state efile;
1a5e3fb1 522 /*
29a30ff5 523 * All loaded bpf_object are linked in a list, which is
9a208eff
WN
524 * hidden to caller. bpf_objects__<func> handlers deal with
525 * all objects.
526 */
527 struct list_head list;
10931d24 528
8a138aed 529 struct btf *btf;
0f7515ca
AN
530 struct btf_ext *btf_ext;
531
a6ed02ca
KS
532 /* Parse and load BTF vmlinux if any of the programs in the object need
533 * it at load time.
534 */
535 struct btf *btf_vmlinux;
1373ff59
SC
536 /* Path to the custom BTF to be used for BPF CO-RE relocations as an
537 * override for vmlinux BTF.
538 */
539 char *btf_custom_path;
0f7515ca
AN
540 /* vmlinux BTF override for CO-RE relocations */
541 struct btf *btf_vmlinux_override;
4f33a53d
AN
542 /* Lazily initialized kernel module BTFs */
543 struct module_btf *btf_modules;
544 bool btf_modules_loaded;
545 size_t btf_module_cnt;
546 size_t btf_module_cap;
8a138aed 547
10931d24
WN
548 void *priv;
549 bpf_object_clear_priv_t clear_priv;
550
9dbe6015
KKD
551 int *fd_array;
552 size_t fd_array_cap;
553 size_t fd_array_cnt;
554
1a5e3fb1
WN
555 char path[];
556};
1a5e3fb1 557
88a82120
AN
558static const char *elf_sym_str(const struct bpf_object *obj, size_t off);
559static const char *elf_sec_str(const struct bpf_object *obj, size_t off);
560static Elf_Scn *elf_sec_by_idx(const struct bpf_object *obj, size_t idx);
561static Elf_Scn *elf_sec_by_name(const struct bpf_object *obj, const char *name);
ad23b723 562static Elf64_Shdr *elf_sec_hdr(const struct bpf_object *obj, Elf_Scn *scn);
88a82120
AN
563static const char *elf_sec_name(const struct bpf_object *obj, Elf_Scn *scn);
564static Elf_Data *elf_sec_data(const struct bpf_object *obj, Elf_Scn *scn);
ad23b723
AN
565static Elf64_Sym *elf_sym_by_idx(const struct bpf_object *obj, size_t idx);
566static Elf64_Rel *elf_rel_by_idx(Elf_Data *data, size_t idx);
88a82120 567
29cd77f4 568void bpf_program__unload(struct bpf_program *prog)
55cffde2 569{
b580563e
WN
570 int i;
571
55cffde2
WN
572 if (!prog)
573 return;
574
b580563e
WN
575 /*
576 * If the object is opened but the program was never loaded,
577 * it is possible that prog->instances.nr == -1.
578 */
579 if (prog->instances.nr > 0) {
580 for (i = 0; i < prog->instances.nr; i++)
581 zclose(prog->instances.fds[i]);
582 } else if (prog->instances.nr != -1) {
be18010e
KW
583 pr_warn("Internal error: instances.nr is %d\n",
584 prog->instances.nr);
b580563e
WN
585 }
586
587 prog->instances.nr = -1;
588 zfree(&prog->instances.fds);
2993e051 589
2993e051 590 zfree(&prog->func_info);
07a09d1b 591 zfree(&prog->line_info);
55cffde2
WN
592}
593
a5b8bd47
WN
594static void bpf_program__exit(struct bpf_program *prog)
595{
596 if (!prog)
597 return;
598
aa9b1ac3
WN
599 if (prog->clear_priv)
600 prog->clear_priv(prog, prog->priv);
601
602 prog->priv = NULL;
603 prog->clear_priv = NULL;
604
55cffde2 605 bpf_program__unload(prog);
88cda1c9 606 zfree(&prog->name);
52109584 607 zfree(&prog->sec_name);
33a2c75c 608 zfree(&prog->pin_name);
a5b8bd47 609 zfree(&prog->insns);
34090915
WN
610 zfree(&prog->reloc_desc);
611
612 prog->nr_reloc = 0;
a5b8bd47 613 prog->insns_cnt = 0;
c1122392 614 prog->sec_idx = -1;
a5b8bd47
WN
615}
616
33a2c75c
SF
617static char *__bpf_program__pin_name(struct bpf_program *prog)
618{
619 char *name, *p;
620
a77f879b
SF
621 if (libbpf_mode & LIBBPF_STRICT_SEC_NAME)
622 name = strdup(prog->name);
623 else
624 name = strdup(prog->sec_name);
625
626 if (!name)
627 return NULL;
628
629 p = name;
630
33a2c75c
SF
631 while ((p = strchr(p, '/')))
632 *p = '_';
633
634 return name;
635}
636
c3c55696
AN
637static bool insn_is_subprog_call(const struct bpf_insn *insn)
638{
639 return BPF_CLASS(insn->code) == BPF_JMP &&
640 BPF_OP(insn->code) == BPF_CALL &&
641 BPF_SRC(insn->code) == BPF_K &&
642 insn->src_reg == BPF_PSEUDO_CALL &&
643 insn->dst_reg == 0 &&
644 insn->off == 0;
645}
646
aa0b8d43
MKL
647static bool is_call_insn(const struct bpf_insn *insn)
648{
649 return insn->code == (BPF_JMP | BPF_CALL);
650}
651
53eddb5e
YS
652static bool insn_is_pseudo_func(struct bpf_insn *insn)
653{
aa0b8d43 654 return is_ldimm64_insn(insn) && insn->src_reg == BPF_PSEUDO_FUNC;
53eddb5e
YS
655}
656
a5b8bd47 657static int
c3c55696
AN
658bpf_object__init_prog(struct bpf_object *obj, struct bpf_program *prog,
659 const char *name, size_t sec_idx, const char *sec_name,
660 size_t sec_off, void *insn_data, size_t insn_data_sz)
a5b8bd47 661{
c1122392
AN
662 if (insn_data_sz == 0 || insn_data_sz % BPF_INSN_SZ || sec_off % BPF_INSN_SZ) {
663 pr_warn("sec '%s': corrupted program '%s', offset %zu, size %zu\n",
664 sec_name, name, sec_off, insn_data_sz);
a5b8bd47
WN
665 return -EINVAL;
666 }
667
1ad9cbb8 668 memset(prog, 0, sizeof(*prog));
c3c55696
AN
669 prog->obj = obj;
670
c1122392
AN
671 prog->sec_idx = sec_idx;
672 prog->sec_insn_off = sec_off / BPF_INSN_SZ;
673 prog->sec_insn_cnt = insn_data_sz / BPF_INSN_SZ;
674 /* insns_cnt can later be increased by appending used subprograms */
675 prog->insns_cnt = prog->sec_insn_cnt;
a5b8bd47 676
c1122392
AN
677 prog->type = BPF_PROG_TYPE_UNSPEC;
678 prog->load = true;
a5b8bd47 679
c1122392
AN
680 prog->instances.fds = NULL;
681 prog->instances.nr = -1;
682
52109584
AN
683 prog->sec_name = strdup(sec_name);
684 if (!prog->sec_name)
c1122392
AN
685 goto errout;
686
687 prog->name = strdup(name);
688 if (!prog->name)
a5b8bd47 689 goto errout;
a5b8bd47 690
33a2c75c 691 prog->pin_name = __bpf_program__pin_name(prog);
c1122392 692 if (!prog->pin_name)
33a2c75c 693 goto errout;
33a2c75c 694
c1122392
AN
695 prog->insns = malloc(insn_data_sz);
696 if (!prog->insns)
a5b8bd47 697 goto errout;
c1122392 698 memcpy(prog->insns, insn_data, insn_data_sz);
a5b8bd47
WN
699
700 return 0;
701errout:
c1122392 702 pr_warn("sec '%s': failed to allocate memory for prog '%s'\n", sec_name, name);
a5b8bd47
WN
703 bpf_program__exit(prog);
704 return -ENOMEM;
705}
706
707static int
c1122392
AN
708bpf_object__add_programs(struct bpf_object *obj, Elf_Data *sec_data,
709 const char *sec_name, int sec_idx)
a5b8bd47 710{
6245947c 711 Elf_Data *symbols = obj->efile.symbols;
c1122392
AN
712 struct bpf_program *prog, *progs;
713 void *data = sec_data->d_buf;
6245947c
AN
714 size_t sec_sz = sec_data->d_size, sec_off, prog_sz, nr_syms;
715 int nr_progs, err, i;
c1122392 716 const char *name;
ad23b723 717 Elf64_Sym *sym;
a5b8bd47
WN
718
719 progs = obj->programs;
720 nr_progs = obj->nr_programs;
ad23b723 721 nr_syms = symbols->d_size / sizeof(Elf64_Sym);
c1122392 722 sec_off = 0;
a5b8bd47 723
6245947c 724 for (i = 0; i < nr_syms; i++) {
ad23b723
AN
725 sym = elf_sym_by_idx(obj, i);
726
727 if (sym->st_shndx != sec_idx)
6245947c 728 continue;
ad23b723 729 if (ELF64_ST_TYPE(sym->st_info) != STT_FUNC)
6245947c 730 continue;
88cda1c9 731
ad23b723
AN
732 prog_sz = sym->st_size;
733 sec_off = sym->st_value;
88cda1c9 734
ad23b723 735 name = elf_sym_str(obj, sym->st_name);
c1122392
AN
736 if (!name) {
737 pr_warn("sec '%s': failed to get symbol name for offset %zu\n",
738 sec_name, sec_off);
739 return -LIBBPF_ERRNO__FORMAT;
740 }
88cda1c9 741
c1122392
AN
742 if (sec_off + prog_sz > sec_sz) {
743 pr_warn("sec '%s': program at offset %zu crosses section boundary\n",
744 sec_name, sec_off);
745 return -LIBBPF_ERRNO__FORMAT;
746 }
88cda1c9 747
ad23b723 748 if (sec_idx != obj->efile.text_shndx && ELF64_ST_BIND(sym->st_info) == STB_LOCAL) {
513f485c
AN
749 pr_warn("sec '%s': program '%s' is static and not supported\n", sec_name, name);
750 return -ENOTSUP;
751 }
752
c3c55696
AN
753 pr_debug("sec '%s': found program '%s' at insn offset %zu (%zu bytes), code size %zu insns (%zu bytes)\n",
754 sec_name, name, sec_off / BPF_INSN_SZ, sec_off, prog_sz / BPF_INSN_SZ, prog_sz);
88cda1c9 755
c3c55696 756 progs = libbpf_reallocarray(progs, nr_progs + 1, sizeof(*progs));
c1122392
AN
757 if (!progs) {
758 /*
759 * In this case the original obj->programs
760 * is still valid, so don't need special treat for
761 * bpf_close_object().
762 */
763 pr_warn("sec '%s': failed to alloc memory for new program '%s'\n",
764 sec_name, name);
765 return -ENOMEM;
88cda1c9 766 }
c1122392 767 obj->programs = progs;
88cda1c9 768
c1122392 769 prog = &progs[nr_progs];
9a94f277 770
c3c55696
AN
771 err = bpf_object__init_prog(obj, prog, name, sec_idx, sec_name,
772 sec_off, data + sec_off, prog_sz);
c1122392
AN
773 if (err)
774 return err;
9a94f277 775
e5670fa0
AN
776 /* if function is a global/weak symbol, but has restricted
777 * (STV_HIDDEN or STV_INTERNAL) visibility, mark its BTF FUNC
778 * as static to enable more permissive BPF verification mode
779 * with more outside context available to BPF verifier
aea28a60 780 */
ad23b723
AN
781 if (ELF64_ST_BIND(sym->st_info) != STB_LOCAL
782 && (ELF64_ST_VISIBILITY(sym->st_other) == STV_HIDDEN
783 || ELF64_ST_VISIBILITY(sym->st_other) == STV_INTERNAL))
aea28a60
AN
784 prog->mark_btf_static = true;
785
c1122392
AN
786 nr_progs++;
787 obj->nr_programs = nr_progs;
88cda1c9
MKL
788 }
789
790 return 0;
791}
792
5e61f270
AN
793static __u32 get_kernel_version(void)
794{
795 __u32 major, minor, patch;
796 struct utsname info;
797
798 uname(&info);
799 if (sscanf(info.release, "%u.%u.%u", &major, &minor, &patch) != 3)
800 return 0;
801 return KERNEL_VERSION(major, minor, patch);
802}
803
590a0088
MKL
804static const struct btf_member *
805find_member_by_offset(const struct btf_type *t, __u32 bit_offset)
806{
807 struct btf_member *m;
808 int i;
809
810 for (i = 0, m = btf_members(t); i < btf_vlen(t); i++, m++) {
811 if (btf_member_bit_offset(t, i) == bit_offset)
812 return m;
813 }
814
815 return NULL;
816}
817
818static const struct btf_member *
819find_member_by_name(const struct btf *btf, const struct btf_type *t,
820 const char *name)
821{
822 struct btf_member *m;
823 int i;
824
825 for (i = 0, m = btf_members(t); i < btf_vlen(t); i++, m++) {
826 if (!strcmp(btf__name_by_offset(btf, m->name_off), name))
827 return m;
828 }
829
830 return NULL;
831}
832
833#define STRUCT_OPS_VALUE_PREFIX "bpf_struct_ops_"
a6ed02ca
KS
834static int find_btf_by_prefix_kind(const struct btf *btf, const char *prefix,
835 const char *name, __u32 kind);
590a0088
MKL
836
837static int
838find_struct_ops_kern_types(const struct btf *btf, const char *tname,
839 const struct btf_type **type, __u32 *type_id,
840 const struct btf_type **vtype, __u32 *vtype_id,
841 const struct btf_member **data_member)
842{
843 const struct btf_type *kern_type, *kern_vtype;
844 const struct btf_member *kern_data_member;
845 __s32 kern_vtype_id, kern_type_id;
590a0088
MKL
846 __u32 i;
847
848 kern_type_id = btf__find_by_name_kind(btf, tname, BTF_KIND_STRUCT);
849 if (kern_type_id < 0) {
850 pr_warn("struct_ops init_kern: struct %s is not found in kernel BTF\n",
851 tname);
852 return kern_type_id;
853 }
854 kern_type = btf__type_by_id(btf, kern_type_id);
855
856 /* Find the corresponding "map_value" type that will be used
857 * in map_update(BPF_MAP_TYPE_STRUCT_OPS). For example,
858 * find "struct bpf_struct_ops_tcp_congestion_ops" from the
859 * btf_vmlinux.
860 */
a6ed02ca
KS
861 kern_vtype_id = find_btf_by_prefix_kind(btf, STRUCT_OPS_VALUE_PREFIX,
862 tname, BTF_KIND_STRUCT);
590a0088 863 if (kern_vtype_id < 0) {
a6ed02ca
KS
864 pr_warn("struct_ops init_kern: struct %s%s is not found in kernel BTF\n",
865 STRUCT_OPS_VALUE_PREFIX, tname);
590a0088
MKL
866 return kern_vtype_id;
867 }
868 kern_vtype = btf__type_by_id(btf, kern_vtype_id);
869
870 /* Find "struct tcp_congestion_ops" from
871 * struct bpf_struct_ops_tcp_congestion_ops {
872 * [ ... ]
873 * struct tcp_congestion_ops data;
874 * }
875 */
876 kern_data_member = btf_members(kern_vtype);
877 for (i = 0; i < btf_vlen(kern_vtype); i++, kern_data_member++) {
878 if (kern_data_member->type == kern_type_id)
879 break;
880 }
881 if (i == btf_vlen(kern_vtype)) {
a6ed02ca
KS
882 pr_warn("struct_ops init_kern: struct %s data is not found in struct %s%s\n",
883 tname, STRUCT_OPS_VALUE_PREFIX, tname);
590a0088
MKL
884 return -EINVAL;
885 }
886
887 *type = kern_type;
888 *type_id = kern_type_id;
889 *vtype = kern_vtype;
890 *vtype_id = kern_vtype_id;
891 *data_member = kern_data_member;
892
893 return 0;
894}
895
896static bool bpf_map__is_struct_ops(const struct bpf_map *map)
897{
898 return map->def.type == BPF_MAP_TYPE_STRUCT_OPS;
899}
900
901/* Init the map's fields that depend on kern_btf */
902static int bpf_map__init_kern_struct_ops(struct bpf_map *map,
903 const struct btf *btf,
904 const struct btf *kern_btf)
905{
906 const struct btf_member *member, *kern_member, *kern_data_member;
907 const struct btf_type *type, *kern_type, *kern_vtype;
908 __u32 i, kern_type_id, kern_vtype_id, kern_data_off;
909 struct bpf_struct_ops *st_ops;
910 void *data, *kern_data;
911 const char *tname;
912 int err;
913
914 st_ops = map->st_ops;
915 type = st_ops->type;
916 tname = st_ops->tname;
917 err = find_struct_ops_kern_types(kern_btf, tname,
918 &kern_type, &kern_type_id,
919 &kern_vtype, &kern_vtype_id,
920 &kern_data_member);
921 if (err)
922 return err;
923
924 pr_debug("struct_ops init_kern %s: type_id:%u kern_type_id:%u kern_vtype_id:%u\n",
925 map->name, st_ops->type_id, kern_type_id, kern_vtype_id);
926
927 map->def.value_size = kern_vtype->size;
928 map->btf_vmlinux_value_type_id = kern_vtype_id;
929
930 st_ops->kern_vdata = calloc(1, kern_vtype->size);
931 if (!st_ops->kern_vdata)
932 return -ENOMEM;
933
934 data = st_ops->data;
935 kern_data_off = kern_data_member->offset / 8;
936 kern_data = st_ops->kern_vdata + kern_data_off;
937
938 member = btf_members(type);
939 for (i = 0; i < btf_vlen(type); i++, member++) {
940 const struct btf_type *mtype, *kern_mtype;
941 __u32 mtype_id, kern_mtype_id;
942 void *mdata, *kern_mdata;
943 __s64 msize, kern_msize;
944 __u32 moff, kern_moff;
945 __u32 kern_member_idx;
946 const char *mname;
947
948 mname = btf__name_by_offset(btf, member->name_off);
949 kern_member = find_member_by_name(kern_btf, kern_type, mname);
950 if (!kern_member) {
951 pr_warn("struct_ops init_kern %s: Cannot find member %s in kernel BTF\n",
952 map->name, mname);
953 return -ENOTSUP;
954 }
955
956 kern_member_idx = kern_member - btf_members(kern_type);
957 if (btf_member_bitfield_size(type, i) ||
958 btf_member_bitfield_size(kern_type, kern_member_idx)) {
959 pr_warn("struct_ops init_kern %s: bitfield %s is not supported\n",
960 map->name, mname);
961 return -ENOTSUP;
962 }
963
964 moff = member->offset / 8;
965 kern_moff = kern_member->offset / 8;
966
967 mdata = data + moff;
968 kern_mdata = kern_data + kern_moff;
969
970 mtype = skip_mods_and_typedefs(btf, member->type, &mtype_id);
971 kern_mtype = skip_mods_and_typedefs(kern_btf, kern_member->type,
972 &kern_mtype_id);
973 if (BTF_INFO_KIND(mtype->info) !=
974 BTF_INFO_KIND(kern_mtype->info)) {
975 pr_warn("struct_ops init_kern %s: Unmatched member type %s %u != %u(kernel)\n",
976 map->name, mname, BTF_INFO_KIND(mtype->info),
977 BTF_INFO_KIND(kern_mtype->info));
978 return -ENOTSUP;
979 }
980
981 if (btf_is_ptr(mtype)) {
982 struct bpf_program *prog;
983
d2836ddd
MKL
984 prog = st_ops->progs[i];
985 if (!prog)
986 continue;
987
590a0088
MKL
988 kern_mtype = skip_mods_and_typedefs(kern_btf,
989 kern_mtype->type,
990 &kern_mtype_id);
d2836ddd
MKL
991
992 /* mtype->type must be a func_proto which was
993 * guaranteed in bpf_object__collect_st_ops_relos(),
994 * so only check kern_mtype for func_proto here.
995 */
996 if (!btf_is_func_proto(kern_mtype)) {
997 pr_warn("struct_ops init_kern %s: kernel member %s is not a func ptr\n",
590a0088
MKL
998 map->name, mname);
999 return -ENOTSUP;
1000 }
1001
590a0088
MKL
1002 prog->attach_btf_id = kern_type_id;
1003 prog->expected_attach_type = kern_member_idx;
1004
1005 st_ops->kern_func_off[i] = kern_data_off + kern_moff;
1006
1007 pr_debug("struct_ops init_kern %s: func ptr %s is set to prog %s from data(+%u) to kern_data(+%u)\n",
1008 map->name, mname, prog->name, moff,
1009 kern_moff);
1010
1011 continue;
1012 }
1013
1014 msize = btf__resolve_size(btf, mtype_id);
1015 kern_msize = btf__resolve_size(kern_btf, kern_mtype_id);
1016 if (msize < 0 || kern_msize < 0 || msize != kern_msize) {
1017 pr_warn("struct_ops init_kern %s: Error in size of member %s: %zd != %zd(kernel)\n",
1018 map->name, mname, (ssize_t)msize,
1019 (ssize_t)kern_msize);
1020 return -ENOTSUP;
1021 }
1022
1023 pr_debug("struct_ops init_kern %s: copy %s %u bytes from data(+%u) to kern_data(+%u)\n",
1024 map->name, mname, (unsigned int)msize,
1025 moff, kern_moff);
1026 memcpy(kern_mdata, mdata, msize);
1027 }
1028
1029 return 0;
1030}
1031
1032static int bpf_object__init_kern_struct_ops_maps(struct bpf_object *obj)
1033{
590a0088
MKL
1034 struct bpf_map *map;
1035 size_t i;
1036 int err;
1037
1038 for (i = 0; i < obj->nr_maps; i++) {
1039 map = &obj->maps[i];
1040
1041 if (!bpf_map__is_struct_ops(map))
1042 continue;
1043
a6ed02ca
KS
1044 err = bpf_map__init_kern_struct_ops(map, obj->btf,
1045 obj->btf_vmlinux);
1046 if (err)
590a0088 1047 return err;
590a0088
MKL
1048 }
1049
590a0088
MKL
1050 return 0;
1051}
1052
1053static int bpf_object__init_struct_ops_maps(struct bpf_object *obj)
1054{
1055 const struct btf_type *type, *datasec;
1056 const struct btf_var_secinfo *vsi;
1057 struct bpf_struct_ops *st_ops;
1058 const char *tname, *var_name;
1059 __s32 type_id, datasec_id;
1060 const struct btf *btf;
1061 struct bpf_map *map;
1062 __u32 i;
1063
1064 if (obj->efile.st_ops_shndx == -1)
1065 return 0;
1066
1067 btf = obj->btf;
1068 datasec_id = btf__find_by_name_kind(btf, STRUCT_OPS_SEC,
1069 BTF_KIND_DATASEC);
1070 if (datasec_id < 0) {
1071 pr_warn("struct_ops init: DATASEC %s not found\n",
1072 STRUCT_OPS_SEC);
1073 return -EINVAL;
1074 }
1075
1076 datasec = btf__type_by_id(btf, datasec_id);
1077 vsi = btf_var_secinfos(datasec);
1078 for (i = 0; i < btf_vlen(datasec); i++, vsi++) {
1079 type = btf__type_by_id(obj->btf, vsi->type);
1080 var_name = btf__name_by_offset(obj->btf, type->name_off);
1081
1082 type_id = btf__resolve_type(obj->btf, vsi->type);
1083 if (type_id < 0) {
1084 pr_warn("struct_ops init: Cannot resolve var type_id %u in DATASEC %s\n",
1085 vsi->type, STRUCT_OPS_SEC);
1086 return -EINVAL;
1087 }
1088
1089 type = btf__type_by_id(obj->btf, type_id);
1090 tname = btf__name_by_offset(obj->btf, type->name_off);
1091 if (!tname[0]) {
1092 pr_warn("struct_ops init: anonymous type is not supported\n");
1093 return -ENOTSUP;
1094 }
1095 if (!btf_is_struct(type)) {
1096 pr_warn("struct_ops init: %s is not a struct\n", tname);
1097 return -EINVAL;
1098 }
1099
1100 map = bpf_object__add_map(obj);
1101 if (IS_ERR(map))
1102 return PTR_ERR(map);
1103
1104 map->sec_idx = obj->efile.st_ops_shndx;
1105 map->sec_offset = vsi->offset;
1106 map->name = strdup(var_name);
1107 if (!map->name)
1108 return -ENOMEM;
1109
1110 map->def.type = BPF_MAP_TYPE_STRUCT_OPS;
1111 map->def.key_size = sizeof(int);
1112 map->def.value_size = type->size;
1113 map->def.max_entries = 1;
1114
1115 map->st_ops = calloc(1, sizeof(*map->st_ops));
1116 if (!map->st_ops)
1117 return -ENOMEM;
1118 st_ops = map->st_ops;
1119 st_ops->data = malloc(type->size);
1120 st_ops->progs = calloc(btf_vlen(type), sizeof(*st_ops->progs));
1121 st_ops->kern_func_off = malloc(btf_vlen(type) *
1122 sizeof(*st_ops->kern_func_off));
1123 if (!st_ops->data || !st_ops->progs || !st_ops->kern_func_off)
1124 return -ENOMEM;
1125
1126 if (vsi->offset + type->size > obj->efile.st_ops_data->d_size) {
1127 pr_warn("struct_ops init: var %s is beyond the end of DATASEC %s\n",
1128 var_name, STRUCT_OPS_SEC);
1129 return -EINVAL;
1130 }
1131
1132 memcpy(st_ops->data,
1133 obj->efile.st_ops_data->d_buf + vsi->offset,
1134 type->size);
1135 st_ops->tname = tname;
1136 st_ops->type = type;
1137 st_ops->type_id = type_id;
1138
1139 pr_debug("struct_ops init: struct %s(type_id=%u) %s found at offset %u\n",
1140 tname, type_id, var_name, vsi->offset);
1141 }
1142
1143 return 0;
1144}
1145
6c956392 1146static struct bpf_object *bpf_object__new(const char *path,
5e61f270 1147 const void *obj_buf,
2ce8450e
AN
1148 size_t obj_buf_sz,
1149 const char *obj_name)
1a5e3fb1 1150{
689624f0 1151 bool strict = (libbpf_mode & LIBBPF_STRICT_NO_OBJECT_LIST);
1a5e3fb1 1152 struct bpf_object *obj;
d859900c 1153 char *end;
1a5e3fb1
WN
1154
1155 obj = calloc(1, sizeof(struct bpf_object) + strlen(path) + 1);
1156 if (!obj) {
be18010e 1157 pr_warn("alloc memory failed for %s\n", path);
6371ca3b 1158 return ERR_PTR(-ENOMEM);
1a5e3fb1
WN
1159 }
1160
1161 strcpy(obj->path, path);
2ce8450e
AN
1162 if (obj_name) {
1163 strncpy(obj->name, obj_name, sizeof(obj->name) - 1);
1164 obj->name[sizeof(obj->name) - 1] = 0;
1165 } else {
1166 /* Using basename() GNU version which doesn't modify arg. */
1167 strncpy(obj->name, basename((void *)path),
1168 sizeof(obj->name) - 1);
1169 end = strchr(obj->name, '.');
1170 if (end)
1171 *end = 0;
1172 }
6c956392 1173
d859900c 1174 obj->efile.fd = -1;
6c956392 1175 /*
76e1022b 1176 * Caller of this function should also call
6c956392
WN
1177 * bpf_object__elf_finish() after data collection to return
1178 * obj_buf to user. If not, we should duplicate the buffer to
1179 * avoid user freeing them before elf finish.
1180 */
1181 obj->efile.obj_buf = obj_buf;
1182 obj->efile.obj_buf_sz = obj_buf_sz;
666810e8 1183 obj->efile.maps_shndx = -1;
abd29c93 1184 obj->efile.btf_maps_shndx = -1;
590a0088 1185 obj->efile.st_ops_shndx = -1;
81bfdd08 1186 obj->kconfig_map_idx = -1;
6c956392 1187
5e61f270 1188 obj->kern_version = get_kernel_version();
52d3352e 1189 obj->loaded = false;
9a208eff
WN
1190
1191 INIT_LIST_HEAD(&obj->list);
689624f0
JB
1192 if (!strict)
1193 list_add(&obj->list, &bpf_objects_list);
1a5e3fb1
WN
1194 return obj;
1195}
1196
1197static void bpf_object__elf_finish(struct bpf_object *obj)
1198{
29a30ff5 1199 if (!obj->efile.elf)
1a5e3fb1
WN
1200 return;
1201
1202 if (obj->efile.elf) {
1203 elf_end(obj->efile.elf);
1204 obj->efile.elf = NULL;
1205 }
bec7d68c 1206 obj->efile.symbols = NULL;
590a0088 1207 obj->efile.st_ops_data = NULL;
b62f06e8 1208
25bbbd7a
AN
1209 zfree(&obj->efile.secs);
1210 obj->efile.sec_cnt = 0;
1a5e3fb1 1211 zclose(obj->efile.fd);
6c956392
WN
1212 obj->efile.obj_buf = NULL;
1213 obj->efile.obj_buf_sz = 0;
1a5e3fb1
WN
1214}
1215
1216static int bpf_object__elf_init(struct bpf_object *obj)
1217{
ad23b723 1218 Elf64_Ehdr *ehdr;
1a5e3fb1 1219 int err = 0;
ad23b723 1220 Elf *elf;
1a5e3fb1 1221
29a30ff5 1222 if (obj->efile.elf) {
88a82120 1223 pr_warn("elf: init internal error\n");
6371ca3b 1224 return -LIBBPF_ERRNO__LIBELF;
1a5e3fb1
WN
1225 }
1226
6c956392
WN
1227 if (obj->efile.obj_buf_sz > 0) {
1228 /*
1229 * obj_buf should have been validated by
1230 * bpf_object__open_buffer().
1231 */
ad23b723 1232 elf = elf_memory((char *)obj->efile.obj_buf, obj->efile.obj_buf_sz);
6c956392
WN
1233 } else {
1234 obj->efile.fd = open(obj->path, O_RDONLY);
1235 if (obj->efile.fd < 0) {
be5c5d4e 1236 char errmsg[STRERR_BUFSIZE], *cp;
1ce6a9fc 1237
be5c5d4e
AN
1238 err = -errno;
1239 cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg));
88a82120 1240 pr_warn("elf: failed to open %s: %s\n", obj->path, cp);
be5c5d4e 1241 return err;
6c956392
WN
1242 }
1243
ad23b723 1244 elf = elf_begin(obj->efile.fd, ELF_C_READ_MMAP, NULL);
1a5e3fb1
WN
1245 }
1246
ad23b723 1247 if (!elf) {
88a82120 1248 pr_warn("elf: failed to open %s as ELF file: %s\n", obj->path, elf_errmsg(-1));
6371ca3b 1249 err = -LIBBPF_ERRNO__LIBELF;
1a5e3fb1
WN
1250 goto errout;
1251 }
1252
ad23b723
AN
1253 obj->efile.elf = elf;
1254
1255 if (elf_kind(elf) != ELF_K_ELF) {
1256 err = -LIBBPF_ERRNO__FORMAT;
1257 pr_warn("elf: '%s' is not a proper ELF object\n", obj->path);
1258 goto errout;
1259 }
1260
1261 if (gelf_getclass(elf) != ELFCLASS64) {
1262 err = -LIBBPF_ERRNO__FORMAT;
1263 pr_warn("elf: '%s' is not a 64-bit ELF object\n", obj->path);
1264 goto errout;
1265 }
1266
1267 obj->efile.ehdr = ehdr = elf64_getehdr(elf);
1268 if (!obj->efile.ehdr) {
88a82120 1269 pr_warn("elf: failed to get ELF header from %s: %s\n", obj->path, elf_errmsg(-1));
6371ca3b 1270 err = -LIBBPF_ERRNO__FORMAT;
1a5e3fb1
WN
1271 goto errout;
1272 }
1a5e3fb1 1273
ad23b723 1274 if (elf_getshdrstrndx(elf, &obj->efile.shstrndx)) {
88a82120
AN
1275 pr_warn("elf: failed to get section names section index for %s: %s\n",
1276 obj->path, elf_errmsg(-1));
1277 err = -LIBBPF_ERRNO__FORMAT;
1278 goto errout;
1279 }
1280
1281 /* Elf is corrupted/truncated, avoid calling elf_strptr. */
ad23b723 1282 if (!elf_rawdata(elf_getscn(elf, obj->efile.shstrndx), NULL)) {
88a82120
AN
1283 pr_warn("elf: failed to get section names strings from %s: %s\n",
1284 obj->path, elf_errmsg(-1));
8f3f5792
NK
1285 err = -LIBBPF_ERRNO__FORMAT;
1286 goto errout;
88a82120
AN
1287 }
1288
9b16137a 1289 /* Old LLVM set e_machine to EM_NONE */
ad23b723 1290 if (ehdr->e_type != ET_REL || (ehdr->e_machine && ehdr->e_machine != EM_BPF)) {
88a82120 1291 pr_warn("elf: %s is not a valid eBPF object file\n", obj->path);
6371ca3b 1292 err = -LIBBPF_ERRNO__FORMAT;
1a5e3fb1
WN
1293 goto errout;
1294 }
1295
1296 return 0;
1297errout:
1298 bpf_object__elf_finish(obj);
1299 return err;
1300}
1301
12ef5634 1302static int bpf_object__check_endianness(struct bpf_object *obj)
cc4228d5 1303{
3930198d 1304#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
ad23b723 1305 if (obj->efile.ehdr->e_ident[EI_DATA] == ELFDATA2LSB)
12ef5634 1306 return 0;
3930198d 1307#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
ad23b723 1308 if (obj->efile.ehdr->e_ident[EI_DATA] == ELFDATA2MSB)
12ef5634
AN
1309 return 0;
1310#else
1311# error "Unrecognized __BYTE_ORDER__"
1312#endif
88a82120 1313 pr_warn("elf: endianness mismatch in %s.\n", obj->path);
6371ca3b 1314 return -LIBBPF_ERRNO__ENDIAN;
cc4228d5
WN
1315}
1316
cb1e5e96 1317static int
399dc65e 1318bpf_object__init_license(struct bpf_object *obj, void *data, size_t size)
cb1e5e96 1319{
399dc65e 1320 memcpy(obj->license, data, min(size, sizeof(obj->license) - 1));
cb1e5e96
WN
1321 pr_debug("license of %s is %s\n", obj->path, obj->license);
1322 return 0;
1323}
1324
54b8625c
JF
1325static int
1326bpf_object__init_kversion(struct bpf_object *obj, void *data, size_t size)
1327{
1328 __u32 kver;
1329
1330 if (size != sizeof(kver)) {
be18010e 1331 pr_warn("invalid kver section in %s\n", obj->path);
54b8625c
JF
1332 return -LIBBPF_ERRNO__FORMAT;
1333 }
1334 memcpy(&kver, data, sizeof(kver));
1335 obj->kern_version = kver;
1336 pr_debug("kernel version of %s is %x\n", obj->path, obj->kern_version);
1337 return 0;
1338}
1339
addb9fc9
NS
1340static bool bpf_map_type__is_map_in_map(enum bpf_map_type type)
1341{
1342 if (type == BPF_MAP_TYPE_ARRAY_OF_MAPS ||
1343 type == BPF_MAP_TYPE_HASH_OF_MAPS)
1344 return true;
1345 return false;
1346}
1347
b96c07f3 1348static int find_elf_sec_sz(const struct bpf_object *obj, const char *name, __u32 *size)
1713d68b
DB
1349{
1350 int ret = -ENOENT;
25bbbd7a
AN
1351 Elf_Data *data;
1352 Elf_Scn *scn;
1713d68b
DB
1353
1354 *size = 0;
25bbbd7a 1355 if (!name)
1713d68b 1356 return -EINVAL;
88a82120 1357
25bbbd7a
AN
1358 scn = elf_sec_by_name(obj, name);
1359 data = elf_sec_data(obj, scn);
1360 if (data) {
1361 ret = 0; /* found it */
1362 *size = data->d_size;
1713d68b
DB
1363 }
1364
1365 return *size ? 0 : ret;
1366}
1367
b96c07f3 1368static int find_elf_var_offset(const struct bpf_object *obj, const char *name, __u32 *off)
1713d68b
DB
1369{
1370 Elf_Data *symbols = obj->efile.symbols;
1371 const char *sname;
1372 size_t si;
1373
1374 if (!name || !off)
1375 return -EINVAL;
1376
ad23b723
AN
1377 for (si = 0; si < symbols->d_size / sizeof(Elf64_Sym); si++) {
1378 Elf64_Sym *sym = elf_sym_by_idx(obj, si);
1713d68b 1379
ad23b723
AN
1380 if (ELF64_ST_BIND(sym->st_info) != STB_GLOBAL ||
1381 ELF64_ST_TYPE(sym->st_info) != STT_OBJECT)
1713d68b
DB
1382 continue;
1383
ad23b723 1384 sname = elf_sym_str(obj, sym->st_name);
1713d68b 1385 if (!sname) {
ad23b723 1386 pr_warn("failed to get sym name string for var %s\n", name);
1713d68b
DB
1387 return -EIO;
1388 }
1389 if (strcmp(name, sname) == 0) {
ad23b723 1390 *off = sym->st_value;
1713d68b
DB
1391 return 0;
1392 }
1393 }
1394
1395 return -ENOENT;
1396}
1397
bf829271 1398static struct bpf_map *bpf_object__add_map(struct bpf_object *obj)
d859900c 1399{
bf829271
AN
1400 struct bpf_map *new_maps;
1401 size_t new_cap;
1402 int i;
1403
1404 if (obj->nr_maps < obj->maps_cap)
1405 return &obj->maps[obj->nr_maps++];
1406
95064979 1407 new_cap = max((size_t)4, obj->maps_cap * 3 / 2);
029258d7 1408 new_maps = libbpf_reallocarray(obj->maps, new_cap, sizeof(*obj->maps));
bf829271 1409 if (!new_maps) {
be18010e 1410 pr_warn("alloc maps for object failed\n");
bf829271
AN
1411 return ERR_PTR(-ENOMEM);
1412 }
1413
1414 obj->maps_cap = new_cap;
1415 obj->maps = new_maps;
1416
1417 /* zero out new maps */
1418 memset(obj->maps + obj->nr_maps, 0,
1419 (obj->maps_cap - obj->nr_maps) * sizeof(*obj->maps));
1420 /*
1421 * fill all fd with -1 so won't close incorrect fd (fd=0 is stdin)
1422 * when failure (zclose won't close negative fd)).
1423 */
1424 for (i = obj->nr_maps; i < obj->maps_cap; i++) {
1425 obj->maps[i].fd = -1;
1426 obj->maps[i].inner_map_fd = -1;
1427 }
1428
1429 return &obj->maps[obj->nr_maps++];
d859900c
DB
1430}
1431
eba9c5f4
AN
1432static size_t bpf_map_mmap_sz(const struct bpf_map *map)
1433{
1434 long page_sz = sysconf(_SC_PAGE_SIZE);
1435 size_t map_sz;
1436
c701917e 1437 map_sz = (size_t)roundup(map->def.value_size, 8) * map->def.max_entries;
eba9c5f4
AN
1438 map_sz = roundup(map_sz, page_sz);
1439 return map_sz;
1440}
1441
aed65917 1442static char *internal_map_name(struct bpf_object *obj, const char *real_name)
81bfdd08 1443{
113e6b7e 1444 char map_name[BPF_OBJ_NAME_LEN], *p;
aed65917
AN
1445 int pfx_len, sfx_len = max((size_t)7, strlen(real_name));
1446
1447 /* This is one of the more confusing parts of libbpf for various
1448 * reasons, some of which are historical. The original idea for naming
1449 * internal names was to include as much of BPF object name prefix as
1450 * possible, so that it can be distinguished from similar internal
1451 * maps of a different BPF object.
1452 * As an example, let's say we have bpf_object named 'my_object_name'
1453 * and internal map corresponding to '.rodata' ELF section. The final
1454 * map name advertised to user and to the kernel will be
1455 * 'my_objec.rodata', taking first 8 characters of object name and
1456 * entire 7 characters of '.rodata'.
1457 * Somewhat confusingly, if internal map ELF section name is shorter
1458 * than 7 characters, e.g., '.bss', we still reserve 7 characters
1459 * for the suffix, even though we only have 4 actual characters, and
1460 * resulting map will be called 'my_objec.bss', not even using all 15
1461 * characters allowed by the kernel. Oh well, at least the truncated
1462 * object name is somewhat consistent in this case. But if the map
1463 * name is '.kconfig', we'll still have entirety of '.kconfig' added
1464 * (8 chars) and thus will be left with only first 7 characters of the
1465 * object name ('my_obje'). Happy guessing, user, that the final map
1466 * name will be "my_obje.kconfig".
1467 * Now, with libbpf starting to support arbitrarily named .rodata.*
1468 * and .data.* data sections, it's possible that ELF section name is
1469 * longer than allowed 15 chars, so we now need to be careful to take
1470 * only up to 15 first characters of ELF name, taking no BPF object
1471 * name characters at all. So '.rodata.abracadabra' will result in
1472 * '.rodata.abracad' kernel and user-visible name.
1473 * We need to keep this convoluted logic intact for .data, .bss and
1474 * .rodata maps, but for new custom .data.custom and .rodata.custom
1475 * maps we use their ELF names as is, not prepending bpf_object name
1476 * in front. We still need to truncate them to 15 characters for the
1477 * kernel. Full name can be recovered for such maps by using DATASEC
1478 * BTF type associated with such map's value type, though.
1479 */
1480 if (sfx_len >= BPF_OBJ_NAME_LEN)
1481 sfx_len = BPF_OBJ_NAME_LEN - 1;
1482
1483 /* if there are two or more dots in map name, it's a custom dot map */
1484 if (strchr(real_name + 1, '.') != NULL)
1485 pfx_len = 0;
1486 else
1487 pfx_len = min((size_t)BPF_OBJ_NAME_LEN - sfx_len - 1, strlen(obj->name));
81bfdd08
AN
1488
1489 snprintf(map_name, sizeof(map_name), "%.*s%.*s", pfx_len, obj->name,
aed65917 1490 sfx_len, real_name);
81bfdd08 1491
113e6b7e
THJ
1492 /* sanitise map name to characters allowed by kernel */
1493 for (p = map_name; *p && p < map_name + sizeof(map_name); p++)
1494 if (!isalnum(*p) && *p != '_' && *p != '.')
1495 *p = '_';
1496
81bfdd08
AN
1497 return strdup(map_name);
1498}
1499
d859900c 1500static int
bf829271 1501bpf_object__init_internal_map(struct bpf_object *obj, enum libbpf_map_type type,
aed65917 1502 const char *real_name, int sec_idx, void *data, size_t data_sz)
d859900c 1503{
bf829271
AN
1504 struct bpf_map_def *def;
1505 struct bpf_map *map;
eba9c5f4 1506 int err;
bf829271
AN
1507
1508 map = bpf_object__add_map(obj);
1509 if (IS_ERR(map))
1510 return PTR_ERR(map);
d859900c
DB
1511
1512 map->libbpf_type = type;
db48814b
AN
1513 map->sec_idx = sec_idx;
1514 map->sec_offset = 0;
aed65917
AN
1515 map->real_name = strdup(real_name);
1516 map->name = internal_map_name(obj, real_name);
1517 if (!map->real_name || !map->name) {
1518 zfree(&map->real_name);
1519 zfree(&map->name);
d859900c
DB
1520 return -ENOMEM;
1521 }
1522
bf829271 1523 def = &map->def;
d859900c
DB
1524 def->type = BPF_MAP_TYPE_ARRAY;
1525 def->key_size = sizeof(int);
eba9c5f4 1526 def->value_size = data_sz;
d859900c 1527 def->max_entries = 1;
81bfdd08 1528 def->map_flags = type == LIBBPF_MAP_RODATA || type == LIBBPF_MAP_KCONFIG
166750bc 1529 ? BPF_F_RDONLY_PROG : 0;
0d13bfce 1530 def->map_flags |= BPF_F_MMAPABLE;
7fe74b43
AN
1531
1532 pr_debug("map '%s' (global data): at sec_idx %d, offset %zu, flags %x.\n",
81bfdd08 1533 map->name, map->sec_idx, map->sec_offset, def->map_flags);
7fe74b43 1534
eba9c5f4
AN
1535 map->mmaped = mmap(NULL, bpf_map_mmap_sz(map), PROT_READ | PROT_WRITE,
1536 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
1537 if (map->mmaped == MAP_FAILED) {
1538 err = -errno;
1539 map->mmaped = NULL;
1540 pr_warn("failed to alloc map '%s' content buffer: %d\n",
1541 map->name, err);
aed65917 1542 zfree(&map->real_name);
eba9c5f4
AN
1543 zfree(&map->name);
1544 return err;
d859900c
DB
1545 }
1546
166750bc 1547 if (data)
eba9c5f4
AN
1548 memcpy(map->mmaped, data, data_sz);
1549
e1d1dc46 1550 pr_debug("map %td is \"%s\"\n", map - obj->maps, map->name);
d859900c
DB
1551 return 0;
1552}
1553
bf829271
AN
1554static int bpf_object__init_global_data_maps(struct bpf_object *obj)
1555{
25bbbd7a 1556 struct elf_sec_desc *sec_desc;
aed65917 1557 const char *sec_name;
25bbbd7a 1558 int err = 0, sec_idx;
bf829271 1559
bf829271
AN
1560 /*
1561 * Populate obj->maps with libbpf internal maps.
1562 */
25bbbd7a
AN
1563 for (sec_idx = 1; sec_idx < obj->efile.sec_cnt; sec_idx++) {
1564 sec_desc = &obj->efile.secs[sec_idx];
1565
1566 switch (sec_desc->sec_type) {
1567 case SEC_DATA:
aed65917 1568 sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, sec_idx));
25bbbd7a 1569 err = bpf_object__init_internal_map(obj, LIBBPF_MAP_DATA,
aed65917 1570 sec_name, sec_idx,
25bbbd7a
AN
1571 sec_desc->data->d_buf,
1572 sec_desc->data->d_size);
1573 break;
1574 case SEC_RODATA:
1575 obj->has_rodata = true;
aed65917 1576 sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, sec_idx));
25bbbd7a 1577 err = bpf_object__init_internal_map(obj, LIBBPF_MAP_RODATA,
aed65917 1578 sec_name, sec_idx,
25bbbd7a
AN
1579 sec_desc->data->d_buf,
1580 sec_desc->data->d_size);
1581 break;
1582 case SEC_BSS:
aed65917 1583 sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, sec_idx));
25bbbd7a 1584 err = bpf_object__init_internal_map(obj, LIBBPF_MAP_BSS,
aed65917 1585 sec_name, sec_idx,
25bbbd7a
AN
1586 NULL,
1587 sec_desc->data->d_size);
1588 break;
1589 default:
1590 /* skip */
1591 break;
1592 }
bf829271
AN
1593 if (err)
1594 return err;
1595 }
1596 return 0;
1597}
1598
166750bc
AN
1599
1600static struct extern_desc *find_extern_by_name(const struct bpf_object *obj,
1601 const void *name)
1602{
1603 int i;
1604
1605 for (i = 0; i < obj->nr_extern; i++) {
1606 if (strcmp(obj->externs[i].name, name) == 0)
1607 return &obj->externs[i];
1608 }
1609 return NULL;
1610}
1611
2e33efe3
AN
1612static int set_kcfg_value_tri(struct extern_desc *ext, void *ext_val,
1613 char value)
166750bc 1614{
2e33efe3
AN
1615 switch (ext->kcfg.type) {
1616 case KCFG_BOOL:
166750bc 1617 if (value == 'm') {
2e33efe3 1618 pr_warn("extern (kcfg) %s=%c should be tristate or char\n",
166750bc
AN
1619 ext->name, value);
1620 return -EINVAL;
1621 }
1622 *(bool *)ext_val = value == 'y' ? true : false;
1623 break;
2e33efe3 1624 case KCFG_TRISTATE:
166750bc
AN
1625 if (value == 'y')
1626 *(enum libbpf_tristate *)ext_val = TRI_YES;
1627 else if (value == 'm')
1628 *(enum libbpf_tristate *)ext_val = TRI_MODULE;
1629 else /* value == 'n' */
1630 *(enum libbpf_tristate *)ext_val = TRI_NO;
1631 break;
2e33efe3 1632 case KCFG_CHAR:
166750bc
AN
1633 *(char *)ext_val = value;
1634 break;
2e33efe3
AN
1635 case KCFG_UNKNOWN:
1636 case KCFG_INT:
1637 case KCFG_CHAR_ARR:
166750bc 1638 default:
2e33efe3 1639 pr_warn("extern (kcfg) %s=%c should be bool, tristate, or char\n",
166750bc
AN
1640 ext->name, value);
1641 return -EINVAL;
1642 }
1643 ext->is_set = true;
1644 return 0;
1645}
1646
2e33efe3
AN
1647static int set_kcfg_value_str(struct extern_desc *ext, char *ext_val,
1648 const char *value)
166750bc
AN
1649{
1650 size_t len;
1651
2e33efe3
AN
1652 if (ext->kcfg.type != KCFG_CHAR_ARR) {
1653 pr_warn("extern (kcfg) %s=%s should be char array\n", ext->name, value);
166750bc
AN
1654 return -EINVAL;
1655 }
1656
1657 len = strlen(value);
1658 if (value[len - 1] != '"') {
2e33efe3 1659 pr_warn("extern (kcfg) '%s': invalid string config '%s'\n",
166750bc
AN
1660 ext->name, value);
1661 return -EINVAL;
1662 }
1663
1664 /* strip quotes */
1665 len -= 2;
2e33efe3
AN
1666 if (len >= ext->kcfg.sz) {
1667 pr_warn("extern (kcfg) '%s': long string config %s of (%zu bytes) truncated to %d bytes\n",
1668 ext->name, value, len, ext->kcfg.sz - 1);
1669 len = ext->kcfg.sz - 1;
166750bc
AN
1670 }
1671 memcpy(ext_val, value + 1, len);
1672 ext_val[len] = '\0';
1673 ext->is_set = true;
1674 return 0;
1675}
1676
1677static int parse_u64(const char *value, __u64 *res)
1678{
1679 char *value_end;
1680 int err;
1681
1682 errno = 0;
1683 *res = strtoull(value, &value_end, 0);
1684 if (errno) {
1685 err = -errno;
1686 pr_warn("failed to parse '%s' as integer: %d\n", value, err);
1687 return err;
1688 }
1689 if (*value_end) {
1690 pr_warn("failed to parse '%s' as integer completely\n", value);
1691 return -EINVAL;
1692 }
1693 return 0;
1694}
1695
2e33efe3 1696static bool is_kcfg_value_in_range(const struct extern_desc *ext, __u64 v)
166750bc 1697{
2e33efe3 1698 int bit_sz = ext->kcfg.sz * 8;
166750bc 1699
2e33efe3 1700 if (ext->kcfg.sz == 8)
166750bc
AN
1701 return true;
1702
1703 /* Validate that value stored in u64 fits in integer of `ext->sz`
1704 * bytes size without any loss of information. If the target integer
1705 * is signed, we rely on the following limits of integer type of
1706 * Y bits and subsequent transformation:
1707 *
1708 * -2^(Y-1) <= X <= 2^(Y-1) - 1
1709 * 0 <= X + 2^(Y-1) <= 2^Y - 1
1710 * 0 <= X + 2^(Y-1) < 2^Y
1711 *
1712 * For unsigned target integer, check that all the (64 - Y) bits are
1713 * zero.
1714 */
2e33efe3 1715 if (ext->kcfg.is_signed)
166750bc
AN
1716 return v + (1ULL << (bit_sz - 1)) < (1ULL << bit_sz);
1717 else
1718 return (v >> bit_sz) == 0;
1719}
1720
2e33efe3
AN
1721static int set_kcfg_value_num(struct extern_desc *ext, void *ext_val,
1722 __u64 value)
166750bc 1723{
2e33efe3
AN
1724 if (ext->kcfg.type != KCFG_INT && ext->kcfg.type != KCFG_CHAR) {
1725 pr_warn("extern (kcfg) %s=%llu should be integer\n",
7745ff98 1726 ext->name, (unsigned long long)value);
166750bc
AN
1727 return -EINVAL;
1728 }
2e33efe3
AN
1729 if (!is_kcfg_value_in_range(ext, value)) {
1730 pr_warn("extern (kcfg) %s=%llu value doesn't fit in %d bytes\n",
1731 ext->name, (unsigned long long)value, ext->kcfg.sz);
166750bc
AN
1732 return -ERANGE;
1733 }
2e33efe3 1734 switch (ext->kcfg.sz) {
166750bc
AN
1735 case 1: *(__u8 *)ext_val = value; break;
1736 case 2: *(__u16 *)ext_val = value; break;
1737 case 4: *(__u32 *)ext_val = value; break;
1738 case 8: *(__u64 *)ext_val = value; break;
1739 default:
1740 return -EINVAL;
1741 }
1742 ext->is_set = true;
1743 return 0;
1744}
1745
8601fd42
AN
1746static int bpf_object__process_kconfig_line(struct bpf_object *obj,
1747 char *buf, void *data)
166750bc 1748{
166750bc 1749 struct extern_desc *ext;
8601fd42 1750 char *sep, *value;
166750bc
AN
1751 int len, err = 0;
1752 void *ext_val;
1753 __u64 num;
166750bc 1754
13d35a0c 1755 if (!str_has_pfx(buf, "CONFIG_"))
8601fd42 1756 return 0;
166750bc 1757
8601fd42
AN
1758 sep = strchr(buf, '=');
1759 if (!sep) {
1760 pr_warn("failed to parse '%s': no separator\n", buf);
1761 return -EINVAL;
1762 }
1763
1764 /* Trim ending '\n' */
1765 len = strlen(buf);
1766 if (buf[len - 1] == '\n')
1767 buf[len - 1] = '\0';
1768 /* Split on '=' and ensure that a value is present. */
1769 *sep = '\0';
1770 if (!sep[1]) {
1771 *sep = '=';
1772 pr_warn("failed to parse '%s': no value\n", buf);
1773 return -EINVAL;
1774 }
1775
1776 ext = find_extern_by_name(obj, buf);
1777 if (!ext || ext->is_set)
1778 return 0;
1779
2e33efe3 1780 ext_val = data + ext->kcfg.data_off;
8601fd42
AN
1781 value = sep + 1;
1782
1783 switch (*value) {
1784 case 'y': case 'n': case 'm':
2e33efe3 1785 err = set_kcfg_value_tri(ext, ext_val, *value);
8601fd42
AN
1786 break;
1787 case '"':
2e33efe3 1788 err = set_kcfg_value_str(ext, ext_val, value);
8601fd42
AN
1789 break;
1790 default:
1791 /* assume integer */
1792 err = parse_u64(value, &num);
1793 if (err) {
2e33efe3 1794 pr_warn("extern (kcfg) %s=%s should be integer\n",
8601fd42
AN
1795 ext->name, value);
1796 return err;
1797 }
2e33efe3 1798 err = set_kcfg_value_num(ext, ext_val, num);
8601fd42 1799 break;
166750bc 1800 }
8601fd42
AN
1801 if (err)
1802 return err;
2e33efe3 1803 pr_debug("extern (kcfg) %s=%s\n", ext->name, value);
8601fd42
AN
1804 return 0;
1805}
1806
1807static int bpf_object__read_kconfig_file(struct bpf_object *obj, void *data)
1808{
1809 char buf[PATH_MAX];
1810 struct utsname uts;
1811 int len, err = 0;
1812 gzFile file;
1813
1814 uname(&uts);
1815 len = snprintf(buf, PATH_MAX, "/boot/config-%s", uts.release);
1816 if (len < 0)
1817 return -EINVAL;
1818 else if (len >= PATH_MAX)
1819 return -ENAMETOOLONG;
1820
1821 /* gzopen also accepts uncompressed files. */
1822 file = gzopen(buf, "r");
1823 if (!file)
1824 file = gzopen("/proc/config.gz", "r");
1825
166750bc 1826 if (!file) {
8601fd42 1827 pr_warn("failed to open system Kconfig\n");
166750bc
AN
1828 return -ENOENT;
1829 }
1830
1831 while (gzgets(file, buf, sizeof(buf))) {
8601fd42
AN
1832 err = bpf_object__process_kconfig_line(obj, buf, data);
1833 if (err) {
1834 pr_warn("error parsing system Kconfig line '%s': %d\n",
1835 buf, err);
166750bc
AN
1836 goto out;
1837 }
8601fd42 1838 }
166750bc 1839
8601fd42
AN
1840out:
1841 gzclose(file);
1842 return err;
1843}
166750bc 1844
8601fd42
AN
1845static int bpf_object__read_kconfig_mem(struct bpf_object *obj,
1846 const char *config, void *data)
1847{
1848 char buf[PATH_MAX];
1849 int err = 0;
1850 FILE *file;
166750bc 1851
8601fd42
AN
1852 file = fmemopen((void *)config, strlen(config), "r");
1853 if (!file) {
1854 err = -errno;
1855 pr_warn("failed to open in-memory Kconfig: %d\n", err);
1856 return err;
1857 }
1858
1859 while (fgets(buf, sizeof(buf), file)) {
1860 err = bpf_object__process_kconfig_line(obj, buf, data);
1861 if (err) {
1862 pr_warn("error parsing in-memory Kconfig line '%s': %d\n",
1863 buf, err);
166750bc
AN
1864 break;
1865 }
166750bc
AN
1866 }
1867
8601fd42 1868 fclose(file);
166750bc
AN
1869 return err;
1870}
1871
81bfdd08 1872static int bpf_object__init_kconfig_map(struct bpf_object *obj)
166750bc 1873{
2e33efe3 1874 struct extern_desc *last_ext = NULL, *ext;
166750bc 1875 size_t map_sz;
2e33efe3 1876 int i, err;
166750bc 1877
2e33efe3
AN
1878 for (i = 0; i < obj->nr_extern; i++) {
1879 ext = &obj->externs[i];
1880 if (ext->type == EXT_KCFG)
1881 last_ext = ext;
1882 }
166750bc 1883
2e33efe3
AN
1884 if (!last_ext)
1885 return 0;
166750bc 1886
2e33efe3 1887 map_sz = last_ext->kcfg.data_off + last_ext->kcfg.sz;
81bfdd08 1888 err = bpf_object__init_internal_map(obj, LIBBPF_MAP_KCONFIG,
aed65917 1889 ".kconfig", obj->efile.symbols_shndx,
166750bc
AN
1890 NULL, map_sz);
1891 if (err)
1892 return err;
1893
81bfdd08 1894 obj->kconfig_map_idx = obj->nr_maps - 1;
166750bc
AN
1895
1896 return 0;
1897}
1898
bf829271 1899static int bpf_object__init_user_maps(struct bpf_object *obj, bool strict)
561bbcca 1900{
561bbcca 1901 Elf_Data *symbols = obj->efile.symbols;
bf829271 1902 int i, map_def_sz = 0, nr_maps = 0, nr_syms;
d859900c 1903 Elf_Data *data = NULL;
bf829271
AN
1904 Elf_Scn *scn;
1905
1906 if (obj->efile.maps_shndx < 0)
1907 return 0;
561bbcca 1908
4708bbda
EL
1909 if (!symbols)
1910 return -EINVAL;
1911
88a82120
AN
1912 scn = elf_sec_by_idx(obj, obj->efile.maps_shndx);
1913 data = elf_sec_data(obj, scn);
bf829271 1914 if (!scn || !data) {
88a82120
AN
1915 pr_warn("elf: failed to get legacy map definitions for %s\n",
1916 obj->path);
bf829271 1917 return -EINVAL;
4708bbda 1918 }
561bbcca 1919
4708bbda
EL
1920 /*
1921 * Count number of maps. Each map has a name.
1922 * Array of maps is not supported: only the first element is
1923 * considered.
1924 *
1925 * TODO: Detect array of map and report error.
1926 */
ad23b723 1927 nr_syms = symbols->d_size / sizeof(Elf64_Sym);
bf829271 1928 for (i = 0; i < nr_syms; i++) {
ad23b723 1929 Elf64_Sym *sym = elf_sym_by_idx(obj, i);
4708bbda 1930
ad23b723 1931 if (sym->st_shndx != obj->efile.maps_shndx)
4708bbda 1932 continue;
ad23b723 1933 if (ELF64_ST_TYPE(sym->st_info) == STT_SECTION)
161ecd53 1934 continue;
4708bbda
EL
1935 nr_maps++;
1936 }
b13c5c14 1937 /* Assume equally sized map definitions */
88a82120
AN
1938 pr_debug("elf: found %d legacy map definitions (%zd bytes) in %s\n",
1939 nr_maps, data->d_size, obj->path);
bf829271 1940
98e527af 1941 if (!data->d_size || nr_maps == 0 || (data->d_size % nr_maps) != 0) {
88a82120
AN
1942 pr_warn("elf: unable to determine legacy map definition size in %s\n",
1943 obj->path);
bf829271 1944 return -EINVAL;
addb9fc9 1945 }
98e527af 1946 map_def_sz = data->d_size / nr_maps;
4708bbda 1947
bf829271
AN
1948 /* Fill obj->maps using data in "maps" section. */
1949 for (i = 0; i < nr_syms; i++) {
ad23b723 1950 Elf64_Sym *sym = elf_sym_by_idx(obj, i);
561bbcca 1951 const char *map_name;
4708bbda 1952 struct bpf_map_def *def;
bf829271 1953 struct bpf_map *map;
561bbcca 1954
ad23b723 1955 if (sym->st_shndx != obj->efile.maps_shndx)
561bbcca 1956 continue;
ad23b723 1957 if (ELF64_ST_TYPE(sym->st_info) == STT_SECTION)
c3e8c44a 1958 continue;
561bbcca 1959
bf829271
AN
1960 map = bpf_object__add_map(obj);
1961 if (IS_ERR(map))
1962 return PTR_ERR(map);
1963
ad23b723 1964 map_name = elf_sym_str(obj, sym->st_name);
c51829bb 1965 if (!map_name) {
be18010e
KW
1966 pr_warn("failed to get map #%d name sym string for obj %s\n",
1967 i, obj->path);
c51829bb
AN
1968 return -LIBBPF_ERRNO__FORMAT;
1969 }
d859900c 1970
ad23b723 1971 if (ELF64_ST_BIND(sym->st_info) == STB_LOCAL) {
c1cccec9
AN
1972 pr_warn("map '%s' (legacy): static maps are not supported\n", map_name);
1973 return -ENOTSUP;
1974 }
1975
bf829271 1976 map->libbpf_type = LIBBPF_MAP_UNSPEC;
ad23b723
AN
1977 map->sec_idx = sym->st_shndx;
1978 map->sec_offset = sym->st_value;
db48814b
AN
1979 pr_debug("map '%s' (legacy): at sec_idx %d, offset %zu.\n",
1980 map_name, map->sec_idx, map->sec_offset);
ad23b723 1981 if (sym->st_value + map_def_sz > data->d_size) {
be18010e
KW
1982 pr_warn("corrupted maps section in %s: last map \"%s\" too small\n",
1983 obj->path, map_name);
4708bbda 1984 return -EINVAL;
561bbcca 1985 }
4708bbda 1986
bf829271
AN
1987 map->name = strdup(map_name);
1988 if (!map->name) {
aed65917 1989 pr_warn("map '%s': failed to alloc map name\n", map_name);
973170e6
WN
1990 return -ENOMEM;
1991 }
bf829271 1992 pr_debug("map %d is \"%s\"\n", i, map->name);
ad23b723 1993 def = (struct bpf_map_def *)(data->d_buf + sym->st_value);
b13c5c14
CG
1994 /*
1995 * If the definition of the map in the object file fits in
1996 * bpf_map_def, copy it. Any extra fields in our version
1997 * of bpf_map_def will default to zero as a result of the
1998 * calloc above.
1999 */
2000 if (map_def_sz <= sizeof(struct bpf_map_def)) {
bf829271 2001 memcpy(&map->def, def, map_def_sz);
b13c5c14
CG
2002 } else {
2003 /*
2004 * Here the map structure being read is bigger than what
2005 * we expect, truncate if the excess bits are all zero.
2006 * If they are not zero, reject this map as
2007 * incompatible.
2008 */
2009 char *b;
8983b731 2010
b13c5c14
CG
2011 for (b = ((char *)def) + sizeof(struct bpf_map_def);
2012 b < ((char *)def) + map_def_sz; b++) {
2013 if (*b != 0) {
8983b731 2014 pr_warn("maps section in %s: \"%s\" has unrecognized, non-zero options\n",
be18010e 2015 obj->path, map_name);
c034a177
JF
2016 if (strict)
2017 return -EINVAL;
b13c5c14
CG
2018 }
2019 }
bf829271 2020 memcpy(&map->def, def, sizeof(struct bpf_map_def));
b13c5c14 2021 }
561bbcca 2022 }
bf829271
AN
2023 return 0;
2024}
4708bbda 2025
42869d28 2026const struct btf_type *
ddc7c304 2027skip_mods_and_typedefs(const struct btf *btf, __u32 id, __u32 *res_id)
abd29c93
AN
2028{
2029 const struct btf_type *t = btf__type_by_id(btf, id);
8837fe5d 2030
ddc7c304
AN
2031 if (res_id)
2032 *res_id = id;
2033
2034 while (btf_is_mod(t) || btf_is_typedef(t)) {
2035 if (res_id)
2036 *res_id = t->type;
2037 t = btf__type_by_id(btf, t->type);
abd29c93 2038 }
ddc7c304
AN
2039
2040 return t;
abd29c93
AN
2041}
2042
590a0088
MKL
2043static const struct btf_type *
2044resolve_func_ptr(const struct btf *btf, __u32 id, __u32 *res_id)
2045{
2046 const struct btf_type *t;
2047
2048 t = skip_mods_and_typedefs(btf, id, NULL);
2049 if (!btf_is_ptr(t))
2050 return NULL;
2051
2052 t = skip_mods_and_typedefs(btf, t->type, res_id);
2053
2054 return btf_is_func_proto(t) ? t : NULL;
2055}
2056
774e132e 2057static const char *__btf_kind_str(__u16 kind)
81ba0889 2058{
774e132e 2059 switch (kind) {
81ba0889
AN
2060 case BTF_KIND_UNKN: return "void";
2061 case BTF_KIND_INT: return "int";
2062 case BTF_KIND_PTR: return "ptr";
2063 case BTF_KIND_ARRAY: return "array";
2064 case BTF_KIND_STRUCT: return "struct";
2065 case BTF_KIND_UNION: return "union";
2066 case BTF_KIND_ENUM: return "enum";
2067 case BTF_KIND_FWD: return "fwd";
2068 case BTF_KIND_TYPEDEF: return "typedef";
2069 case BTF_KIND_VOLATILE: return "volatile";
2070 case BTF_KIND_CONST: return "const";
2071 case BTF_KIND_RESTRICT: return "restrict";
2072 case BTF_KIND_FUNC: return "func";
2073 case BTF_KIND_FUNC_PROTO: return "func_proto";
2074 case BTF_KIND_VAR: return "var";
2075 case BTF_KIND_DATASEC: return "datasec";
22541a9e 2076 case BTF_KIND_FLOAT: return "float";
223f903e 2077 case BTF_KIND_DECL_TAG: return "decl_tag";
81ba0889
AN
2078 default: return "unknown";
2079 }
2080}
2081
42869d28 2082const char *btf_kind_str(const struct btf_type *t)
774e132e
MKL
2083{
2084 return __btf_kind_str(btf_kind(t));
2085}
2086
ef99b02b
AN
2087/*
2088 * Fetch integer attribute of BTF map definition. Such attributes are
2089 * represented using a pointer to an array, in which dimensionality of array
2090 * encodes specified integer value. E.g., int (*type)[BPF_MAP_TYPE_ARRAY];
2091 * encodes `type => BPF_MAP_TYPE_ARRAY` key/value pair completely using BTF
2092 * type definition, while using only sizeof(void *) space in ELF data section.
2093 */
2094static bool get_map_field_int(const char *map_name, const struct btf *btf,
8983b731
AN
2095 const struct btf_member *m, __u32 *res)
2096{
ddc7c304 2097 const struct btf_type *t = skip_mods_and_typedefs(btf, m->type, NULL);
abd29c93 2098 const char *name = btf__name_by_offset(btf, m->name_off);
ef99b02b
AN
2099 const struct btf_array *arr_info;
2100 const struct btf_type *arr_t;
abd29c93 2101
b03bc685 2102 if (!btf_is_ptr(t)) {
81ba0889
AN
2103 pr_warn("map '%s': attr '%s': expected PTR, got %s.\n",
2104 map_name, name, btf_kind_str(t));
abd29c93
AN
2105 return false;
2106 }
ef99b02b
AN
2107
2108 arr_t = btf__type_by_id(btf, t->type);
2109 if (!arr_t) {
be18010e
KW
2110 pr_warn("map '%s': attr '%s': type [%u] not found.\n",
2111 map_name, name, t->type);
abd29c93
AN
2112 return false;
2113 }
b03bc685 2114 if (!btf_is_array(arr_t)) {
81ba0889
AN
2115 pr_warn("map '%s': attr '%s': expected ARRAY, got %s.\n",
2116 map_name, name, btf_kind_str(arr_t));
abd29c93
AN
2117 return false;
2118 }
b03bc685 2119 arr_info = btf_array(arr_t);
ef99b02b 2120 *res = arr_info->nelems;
abd29c93
AN
2121 return true;
2122}
2123
57a00f41
THJ
2124static int build_map_pin_path(struct bpf_map *map, const char *path)
2125{
2126 char buf[PATH_MAX];
6e9cab2e 2127 int len;
57a00f41
THJ
2128
2129 if (!path)
2130 path = "/sys/fs/bpf";
2131
2132 len = snprintf(buf, PATH_MAX, "%s/%s", path, bpf_map__name(map));
2133 if (len < 0)
2134 return -EINVAL;
2135 else if (len >= PATH_MAX)
2136 return -ENAMETOOLONG;
2137
6e9cab2e 2138 return bpf_map__set_pin_path(map, buf);
57a00f41
THJ
2139}
2140
c7ef5ec9
AN
2141int parse_btf_map_def(const char *map_name, struct btf *btf,
2142 const struct btf_type *def_t, bool strict,
2143 struct btf_map_def *map_def, struct btf_map_def *inner_def)
abd29c93 2144{
41017e56 2145 const struct btf_type *t;
abd29c93 2146 const struct btf_member *m;
c7ef5ec9 2147 bool is_inner = inner_def == NULL;
abd29c93
AN
2148 int vlen, i;
2149
c7ef5ec9
AN
2150 vlen = btf_vlen(def_t);
2151 m = btf_members(def_t);
abd29c93 2152 for (i = 0; i < vlen; i++, m++) {
c7ef5ec9 2153 const char *name = btf__name_by_offset(btf, m->name_off);
abd29c93
AN
2154
2155 if (!name) {
c7ef5ec9 2156 pr_warn("map '%s': invalid field #%d.\n", map_name, i);
abd29c93
AN
2157 return -EINVAL;
2158 }
2159 if (strcmp(name, "type") == 0) {
c7ef5ec9 2160 if (!get_map_field_int(map_name, btf, m, &map_def->map_type))
abd29c93 2161 return -EINVAL;
c7ef5ec9 2162 map_def->parts |= MAP_DEF_MAP_TYPE;
abd29c93 2163 } else if (strcmp(name, "max_entries") == 0) {
c7ef5ec9 2164 if (!get_map_field_int(map_name, btf, m, &map_def->max_entries))
abd29c93 2165 return -EINVAL;
c7ef5ec9 2166 map_def->parts |= MAP_DEF_MAX_ENTRIES;
abd29c93 2167 } else if (strcmp(name, "map_flags") == 0) {
c7ef5ec9 2168 if (!get_map_field_int(map_name, btf, m, &map_def->map_flags))
abd29c93 2169 return -EINVAL;
c7ef5ec9 2170 map_def->parts |= MAP_DEF_MAP_FLAGS;
1bdb6c9a 2171 } else if (strcmp(name, "numa_node") == 0) {
c7ef5ec9 2172 if (!get_map_field_int(map_name, btf, m, &map_def->numa_node))
1bdb6c9a 2173 return -EINVAL;
c7ef5ec9 2174 map_def->parts |= MAP_DEF_NUMA_NODE;
abd29c93
AN
2175 } else if (strcmp(name, "key_size") == 0) {
2176 __u32 sz;
2177
c7ef5ec9 2178 if (!get_map_field_int(map_name, btf, m, &sz))
abd29c93 2179 return -EINVAL;
c7ef5ec9 2180 if (map_def->key_size && map_def->key_size != sz) {
be18010e 2181 pr_warn("map '%s': conflicting key size %u != %u.\n",
c7ef5ec9 2182 map_name, map_def->key_size, sz);
abd29c93
AN
2183 return -EINVAL;
2184 }
c7ef5ec9
AN
2185 map_def->key_size = sz;
2186 map_def->parts |= MAP_DEF_KEY_SIZE;
abd29c93
AN
2187 } else if (strcmp(name, "key") == 0) {
2188 __s64 sz;
2189
c7ef5ec9 2190 t = btf__type_by_id(btf, m->type);
abd29c93 2191 if (!t) {
be18010e 2192 pr_warn("map '%s': key type [%d] not found.\n",
c7ef5ec9 2193 map_name, m->type);
abd29c93
AN
2194 return -EINVAL;
2195 }
b03bc685 2196 if (!btf_is_ptr(t)) {
81ba0889 2197 pr_warn("map '%s': key spec is not PTR: %s.\n",
c7ef5ec9 2198 map_name, btf_kind_str(t));
abd29c93
AN
2199 return -EINVAL;
2200 }
c7ef5ec9 2201 sz = btf__resolve_size(btf, t->type);
abd29c93 2202 if (sz < 0) {
679152d3 2203 pr_warn("map '%s': can't determine key size for type [%u]: %zd.\n",
c7ef5ec9 2204 map_name, t->type, (ssize_t)sz);
abd29c93
AN
2205 return sz;
2206 }
c7ef5ec9 2207 if (map_def->key_size && map_def->key_size != sz) {
679152d3 2208 pr_warn("map '%s': conflicting key size %u != %zd.\n",
c7ef5ec9 2209 map_name, map_def->key_size, (ssize_t)sz);
abd29c93
AN
2210 return -EINVAL;
2211 }
c7ef5ec9
AN
2212 map_def->key_size = sz;
2213 map_def->key_type_id = t->type;
2214 map_def->parts |= MAP_DEF_KEY_SIZE | MAP_DEF_KEY_TYPE;
abd29c93
AN
2215 } else if (strcmp(name, "value_size") == 0) {
2216 __u32 sz;
2217
c7ef5ec9 2218 if (!get_map_field_int(map_name, btf, m, &sz))
abd29c93 2219 return -EINVAL;
c7ef5ec9 2220 if (map_def->value_size && map_def->value_size != sz) {
be18010e 2221 pr_warn("map '%s': conflicting value size %u != %u.\n",
c7ef5ec9 2222 map_name, map_def->value_size, sz);
abd29c93
AN
2223 return -EINVAL;
2224 }
c7ef5ec9
AN
2225 map_def->value_size = sz;
2226 map_def->parts |= MAP_DEF_VALUE_SIZE;
abd29c93
AN
2227 } else if (strcmp(name, "value") == 0) {
2228 __s64 sz;
2229
c7ef5ec9 2230 t = btf__type_by_id(btf, m->type);
abd29c93 2231 if (!t) {
be18010e 2232 pr_warn("map '%s': value type [%d] not found.\n",
c7ef5ec9 2233 map_name, m->type);
abd29c93
AN
2234 return -EINVAL;
2235 }
b03bc685 2236 if (!btf_is_ptr(t)) {
81ba0889 2237 pr_warn("map '%s': value spec is not PTR: %s.\n",
c7ef5ec9 2238 map_name, btf_kind_str(t));
abd29c93
AN
2239 return -EINVAL;
2240 }
c7ef5ec9 2241 sz = btf__resolve_size(btf, t->type);
abd29c93 2242 if (sz < 0) {
679152d3 2243 pr_warn("map '%s': can't determine value size for type [%u]: %zd.\n",
c7ef5ec9 2244 map_name, t->type, (ssize_t)sz);
abd29c93
AN
2245 return sz;
2246 }
c7ef5ec9 2247 if (map_def->value_size && map_def->value_size != sz) {
679152d3 2248 pr_warn("map '%s': conflicting value size %u != %zd.\n",
c7ef5ec9 2249 map_name, map_def->value_size, (ssize_t)sz);
abd29c93
AN
2250 return -EINVAL;
2251 }
c7ef5ec9
AN
2252 map_def->value_size = sz;
2253 map_def->value_type_id = t->type;
2254 map_def->parts |= MAP_DEF_VALUE_SIZE | MAP_DEF_VALUE_TYPE;
646f02ff
AN
2255 }
2256 else if (strcmp(name, "values") == 0) {
c7ef5ec9 2257 char inner_map_name[128];
646f02ff
AN
2258 int err;
2259
2260 if (is_inner) {
2261 pr_warn("map '%s': multi-level inner maps not supported.\n",
c7ef5ec9 2262 map_name);
646f02ff
AN
2263 return -ENOTSUP;
2264 }
2265 if (i != vlen - 1) {
2266 pr_warn("map '%s': '%s' member should be last.\n",
c7ef5ec9 2267 map_name, name);
646f02ff
AN
2268 return -EINVAL;
2269 }
c7ef5ec9 2270 if (!bpf_map_type__is_map_in_map(map_def->map_type)) {
646f02ff 2271 pr_warn("map '%s': should be map-in-map.\n",
c7ef5ec9 2272 map_name);
646f02ff
AN
2273 return -ENOTSUP;
2274 }
c7ef5ec9 2275 if (map_def->value_size && map_def->value_size != 4) {
646f02ff 2276 pr_warn("map '%s': conflicting value size %u != 4.\n",
c7ef5ec9 2277 map_name, map_def->value_size);
646f02ff
AN
2278 return -EINVAL;
2279 }
c7ef5ec9
AN
2280 map_def->value_size = 4;
2281 t = btf__type_by_id(btf, m->type);
646f02ff
AN
2282 if (!t) {
2283 pr_warn("map '%s': map-in-map inner type [%d] not found.\n",
c7ef5ec9 2284 map_name, m->type);
646f02ff
AN
2285 return -EINVAL;
2286 }
2287 if (!btf_is_array(t) || btf_array(t)->nelems) {
2288 pr_warn("map '%s': map-in-map inner spec is not a zero-sized array.\n",
c7ef5ec9 2289 map_name);
646f02ff
AN
2290 return -EINVAL;
2291 }
c7ef5ec9 2292 t = skip_mods_and_typedefs(btf, btf_array(t)->type, NULL);
646f02ff 2293 if (!btf_is_ptr(t)) {
81ba0889 2294 pr_warn("map '%s': map-in-map inner def is of unexpected kind %s.\n",
c7ef5ec9 2295 map_name, btf_kind_str(t));
646f02ff
AN
2296 return -EINVAL;
2297 }
c7ef5ec9 2298 t = skip_mods_and_typedefs(btf, t->type, NULL);
646f02ff 2299 if (!btf_is_struct(t)) {
81ba0889 2300 pr_warn("map '%s': map-in-map inner def is of unexpected kind %s.\n",
c7ef5ec9 2301 map_name, btf_kind_str(t));
646f02ff
AN
2302 return -EINVAL;
2303 }
2304
c7ef5ec9
AN
2305 snprintf(inner_map_name, sizeof(inner_map_name), "%s.inner", map_name);
2306 err = parse_btf_map_def(inner_map_name, btf, t, strict, inner_def, NULL);
646f02ff
AN
2307 if (err)
2308 return err;
c7ef5ec9
AN
2309
2310 map_def->parts |= MAP_DEF_INNER_MAP;
57a00f41
THJ
2311 } else if (strcmp(name, "pinning") == 0) {
2312 __u32 val;
57a00f41 2313
646f02ff 2314 if (is_inner) {
c7ef5ec9 2315 pr_warn("map '%s': inner def can't be pinned.\n", map_name);
646f02ff
AN
2316 return -EINVAL;
2317 }
c7ef5ec9 2318 if (!get_map_field_int(map_name, btf, m, &val))
57a00f41 2319 return -EINVAL;
c7ef5ec9 2320 if (val != LIBBPF_PIN_NONE && val != LIBBPF_PIN_BY_NAME) {
57a00f41 2321 pr_warn("map '%s': invalid pinning value %u.\n",
c7ef5ec9 2322 map_name, val);
57a00f41
THJ
2323 return -EINVAL;
2324 }
c7ef5ec9
AN
2325 map_def->pinning = val;
2326 map_def->parts |= MAP_DEF_PINNING;
abd29c93
AN
2327 } else {
2328 if (strict) {
c7ef5ec9 2329 pr_warn("map '%s': unknown field '%s'.\n", map_name, name);
abd29c93
AN
2330 return -ENOTSUP;
2331 }
c7ef5ec9 2332 pr_debug("map '%s': ignoring unknown field '%s'.\n", map_name, name);
abd29c93
AN
2333 }
2334 }
2335
c7ef5ec9
AN
2336 if (map_def->map_type == BPF_MAP_TYPE_UNSPEC) {
2337 pr_warn("map '%s': map type isn't specified.\n", map_name);
abd29c93
AN
2338 return -EINVAL;
2339 }
2340
2341 return 0;
2342}
2343
c7ef5ec9
AN
2344static void fill_map_from_def(struct bpf_map *map, const struct btf_map_def *def)
2345{
2346 map->def.type = def->map_type;
2347 map->def.key_size = def->key_size;
2348 map->def.value_size = def->value_size;
2349 map->def.max_entries = def->max_entries;
2350 map->def.map_flags = def->map_flags;
2351
2352 map->numa_node = def->numa_node;
2353 map->btf_key_type_id = def->key_type_id;
2354 map->btf_value_type_id = def->value_type_id;
2355
2356 if (def->parts & MAP_DEF_MAP_TYPE)
2357 pr_debug("map '%s': found type = %u.\n", map->name, def->map_type);
2358
2359 if (def->parts & MAP_DEF_KEY_TYPE)
2360 pr_debug("map '%s': found key [%u], sz = %u.\n",
2361 map->name, def->key_type_id, def->key_size);
2362 else if (def->parts & MAP_DEF_KEY_SIZE)
2363 pr_debug("map '%s': found key_size = %u.\n", map->name, def->key_size);
2364
2365 if (def->parts & MAP_DEF_VALUE_TYPE)
2366 pr_debug("map '%s': found value [%u], sz = %u.\n",
2367 map->name, def->value_type_id, def->value_size);
2368 else if (def->parts & MAP_DEF_VALUE_SIZE)
2369 pr_debug("map '%s': found value_size = %u.\n", map->name, def->value_size);
2370
2371 if (def->parts & MAP_DEF_MAX_ENTRIES)
2372 pr_debug("map '%s': found max_entries = %u.\n", map->name, def->max_entries);
2373 if (def->parts & MAP_DEF_MAP_FLAGS)
2374 pr_debug("map '%s': found map_flags = %u.\n", map->name, def->map_flags);
2375 if (def->parts & MAP_DEF_PINNING)
2376 pr_debug("map '%s': found pinning = %u.\n", map->name, def->pinning);
2377 if (def->parts & MAP_DEF_NUMA_NODE)
2378 pr_debug("map '%s': found numa_node = %u.\n", map->name, def->numa_node);
2379
2380 if (def->parts & MAP_DEF_INNER_MAP)
2381 pr_debug("map '%s': found inner map definition.\n", map->name);
2382}
2383
c1cccec9
AN
2384static const char *btf_var_linkage_str(__u32 linkage)
2385{
2386 switch (linkage) {
2387 case BTF_VAR_STATIC: return "static";
2388 case BTF_VAR_GLOBAL_ALLOCATED: return "global";
2389 case BTF_VAR_GLOBAL_EXTERN: return "extern";
2390 default: return "unknown";
2391 }
2392}
2393
41017e56
AN
2394static int bpf_object__init_user_btf_map(struct bpf_object *obj,
2395 const struct btf_type *sec,
2396 int var_idx, int sec_idx,
2397 const Elf_Data *data, bool strict,
2398 const char *pin_root_path)
2399{
c7ef5ec9 2400 struct btf_map_def map_def = {}, inner_def = {};
41017e56
AN
2401 const struct btf_type *var, *def;
2402 const struct btf_var_secinfo *vi;
2403 const struct btf_var *var_extra;
2404 const char *map_name;
2405 struct bpf_map *map;
c7ef5ec9 2406 int err;
41017e56
AN
2407
2408 vi = btf_var_secinfos(sec) + var_idx;
2409 var = btf__type_by_id(obj->btf, vi->type);
2410 var_extra = btf_var(var);
2411 map_name = btf__name_by_offset(obj->btf, var->name_off);
2412
2413 if (map_name == NULL || map_name[0] == '\0') {
2414 pr_warn("map #%d: empty name.\n", var_idx);
2415 return -EINVAL;
2416 }
2417 if ((__u64)vi->offset + vi->size > data->d_size) {
2418 pr_warn("map '%s' BTF data is corrupted.\n", map_name);
2419 return -EINVAL;
2420 }
2421 if (!btf_is_var(var)) {
81ba0889
AN
2422 pr_warn("map '%s': unexpected var kind %s.\n",
2423 map_name, btf_kind_str(var));
41017e56
AN
2424 return -EINVAL;
2425 }
c1cccec9
AN
2426 if (var_extra->linkage != BTF_VAR_GLOBAL_ALLOCATED) {
2427 pr_warn("map '%s': unsupported map linkage %s.\n",
2428 map_name, btf_var_linkage_str(var_extra->linkage));
41017e56
AN
2429 return -EOPNOTSUPP;
2430 }
2431
2432 def = skip_mods_and_typedefs(obj->btf, var->type, NULL);
2433 if (!btf_is_struct(def)) {
81ba0889
AN
2434 pr_warn("map '%s': unexpected def kind %s.\n",
2435 map_name, btf_kind_str(var));
41017e56
AN
2436 return -EINVAL;
2437 }
2438 if (def->size > vi->size) {
2439 pr_warn("map '%s': invalid def size.\n", map_name);
2440 return -EINVAL;
2441 }
2442
2443 map = bpf_object__add_map(obj);
2444 if (IS_ERR(map))
2445 return PTR_ERR(map);
2446 map->name = strdup(map_name);
2447 if (!map->name) {
2448 pr_warn("map '%s': failed to alloc map name.\n", map_name);
2449 return -ENOMEM;
2450 }
2451 map->libbpf_type = LIBBPF_MAP_UNSPEC;
2452 map->def.type = BPF_MAP_TYPE_UNSPEC;
2453 map->sec_idx = sec_idx;
2454 map->sec_offset = vi->offset;
646f02ff 2455 map->btf_var_idx = var_idx;
41017e56
AN
2456 pr_debug("map '%s': at sec_idx %d, offset %zu.\n",
2457 map_name, map->sec_idx, map->sec_offset);
2458
c7ef5ec9
AN
2459 err = parse_btf_map_def(map->name, obj->btf, def, strict, &map_def, &inner_def);
2460 if (err)
2461 return err;
2462
2463 fill_map_from_def(map, &map_def);
2464
2465 if (map_def.pinning == LIBBPF_PIN_BY_NAME) {
2466 err = build_map_pin_path(map, pin_root_path);
2467 if (err) {
2468 pr_warn("map '%s': couldn't build pin path.\n", map->name);
2469 return err;
2470 }
2471 }
2472
2473 if (map_def.parts & MAP_DEF_INNER_MAP) {
2474 map->inner_map = calloc(1, sizeof(*map->inner_map));
2475 if (!map->inner_map)
2476 return -ENOMEM;
2477 map->inner_map->fd = -1;
2478 map->inner_map->sec_idx = sec_idx;
2479 map->inner_map->name = malloc(strlen(map_name) + sizeof(".inner") + 1);
2480 if (!map->inner_map->name)
2481 return -ENOMEM;
2482 sprintf(map->inner_map->name, "%s.inner", map_name);
2483
2484 fill_map_from_def(map->inner_map, &inner_def);
2485 }
2486
2487 return 0;
41017e56
AN
2488}
2489
57a00f41
THJ
2490static int bpf_object__init_user_btf_maps(struct bpf_object *obj, bool strict,
2491 const char *pin_root_path)
abd29c93
AN
2492{
2493 const struct btf_type *sec = NULL;
2494 int nr_types, i, vlen, err;
2495 const struct btf_type *t;
2496 const char *name;
2497 Elf_Data *data;
2498 Elf_Scn *scn;
2499
2500 if (obj->efile.btf_maps_shndx < 0)
2501 return 0;
2502
88a82120
AN
2503 scn = elf_sec_by_idx(obj, obj->efile.btf_maps_shndx);
2504 data = elf_sec_data(obj, scn);
abd29c93 2505 if (!scn || !data) {
88a82120
AN
2506 pr_warn("elf: failed to get %s map definitions for %s\n",
2507 MAPS_ELF_SEC, obj->path);
abd29c93
AN
2508 return -EINVAL;
2509 }
2510
6a886de0
HC
2511 nr_types = btf__type_cnt(obj->btf);
2512 for (i = 1; i < nr_types; i++) {
abd29c93 2513 t = btf__type_by_id(obj->btf, i);
b03bc685 2514 if (!btf_is_datasec(t))
abd29c93
AN
2515 continue;
2516 name = btf__name_by_offset(obj->btf, t->name_off);
2517 if (strcmp(name, MAPS_ELF_SEC) == 0) {
2518 sec = t;
646f02ff 2519 obj->efile.btf_maps_sec_btf_id = i;
abd29c93
AN
2520 break;
2521 }
2522 }
2523
2524 if (!sec) {
be18010e 2525 pr_warn("DATASEC '%s' not found.\n", MAPS_ELF_SEC);
abd29c93
AN
2526 return -ENOENT;
2527 }
2528
b03bc685 2529 vlen = btf_vlen(sec);
abd29c93
AN
2530 for (i = 0; i < vlen; i++) {
2531 err = bpf_object__init_user_btf_map(obj, sec, i,
2532 obj->efile.btf_maps_shndx,
8983b731
AN
2533 data, strict,
2534 pin_root_path);
abd29c93
AN
2535 if (err)
2536 return err;
2537 }
2538
2539 return 0;
2540}
2541
0d13bfce 2542static int bpf_object__init_maps(struct bpf_object *obj,
01af3bf0 2543 const struct bpf_object_open_opts *opts)
bf829271 2544{
166750bc
AN
2545 const char *pin_root_path;
2546 bool strict;
bf829271 2547 int err;
8837fe5d 2548
166750bc
AN
2549 strict = !OPTS_GET(opts, relaxed_maps, false);
2550 pin_root_path = OPTS_GET(opts, pin_root_path, NULL);
bf829271 2551
166750bc
AN
2552 err = bpf_object__init_user_maps(obj, strict);
2553 err = err ?: bpf_object__init_user_btf_maps(obj, strict, pin_root_path);
2554 err = err ?: bpf_object__init_global_data_maps(obj);
81bfdd08 2555 err = err ?: bpf_object__init_kconfig_map(obj);
590a0088 2556 err = err ?: bpf_object__init_struct_ops_maps(obj);
bf829271 2557
3b3af91c 2558 return err;
561bbcca
WN
2559}
2560
e3d91b0c
JDB
2561static bool section_have_execinstr(struct bpf_object *obj, int idx)
2562{
ad23b723 2563 Elf64_Shdr *sh;
e3d91b0c 2564
ad23b723
AN
2565 sh = elf_sec_hdr(obj, elf_sec_by_idx(obj, idx));
2566 if (!sh)
e3d91b0c
JDB
2567 return false;
2568
ad23b723 2569 return sh->sh_flags & SHF_EXECINSTR;
e3d91b0c
JDB
2570}
2571
0f0e55d8
AN
2572static bool btf_needs_sanitization(struct bpf_object *obj)
2573{
9ca1f56a
AS
2574 bool has_func_global = kernel_supports(obj, FEAT_BTF_GLOBAL_FUNC);
2575 bool has_datasec = kernel_supports(obj, FEAT_BTF_DATASEC);
2576 bool has_float = kernel_supports(obj, FEAT_BTF_FLOAT);
2577 bool has_func = kernel_supports(obj, FEAT_BTF_FUNC);
223f903e 2578 bool has_decl_tag = kernel_supports(obj, FEAT_BTF_DECL_TAG);
0f0e55d8 2579
223f903e 2580 return !has_func || !has_datasec || !has_func_global || !has_float || !has_decl_tag;
0f0e55d8
AN
2581}
2582
2583static void bpf_object__sanitize_btf(struct bpf_object *obj, struct btf *btf)
d7c4b398 2584{
9ca1f56a
AS
2585 bool has_func_global = kernel_supports(obj, FEAT_BTF_GLOBAL_FUNC);
2586 bool has_datasec = kernel_supports(obj, FEAT_BTF_DATASEC);
2587 bool has_float = kernel_supports(obj, FEAT_BTF_FLOAT);
2588 bool has_func = kernel_supports(obj, FEAT_BTF_FUNC);
223f903e 2589 bool has_decl_tag = kernel_supports(obj, FEAT_BTF_DECL_TAG);
d7c4b398
AN
2590 struct btf_type *t;
2591 int i, j, vlen;
d7c4b398 2592
6a886de0 2593 for (i = 1; i < btf__type_cnt(btf); i++) {
d7c4b398 2594 t = (struct btf_type *)btf__type_by_id(btf, i);
d7c4b398 2595
223f903e
YS
2596 if ((!has_datasec && btf_is_var(t)) || (!has_decl_tag && btf_is_decl_tag(t))) {
2597 /* replace VAR/DECL_TAG with INT */
d7c4b398 2598 t->info = BTF_INFO_ENC(BTF_KIND_INT, 0, 0);
1d4126c4
AN
2599 /*
2600 * using size = 1 is the safest choice, 4 will be too
2601 * big and cause kernel BTF validation failure if
2602 * original variable took less than 4 bytes
2603 */
2604 t->size = 1;
708852dc 2605 *(int *)(t + 1) = BTF_INT_ENC(0, 0, 8);
b03bc685 2606 } else if (!has_datasec && btf_is_datasec(t)) {
d7c4b398 2607 /* replace DATASEC with STRUCT */
b03bc685
AN
2608 const struct btf_var_secinfo *v = btf_var_secinfos(t);
2609 struct btf_member *m = btf_members(t);
d7c4b398
AN
2610 struct btf_type *vt;
2611 char *name;
2612
2613 name = (char *)btf__name_by_offset(btf, t->name_off);
2614 while (*name) {
2615 if (*name == '.')
2616 *name = '_';
2617 name++;
2618 }
2619
b03bc685 2620 vlen = btf_vlen(t);
d7c4b398
AN
2621 t->info = BTF_INFO_ENC(BTF_KIND_STRUCT, 0, vlen);
2622 for (j = 0; j < vlen; j++, v++, m++) {
2623 /* order of field assignments is important */
2624 m->offset = v->offset * 8;
2625 m->type = v->type;
2626 /* preserve variable name as member name */
2627 vt = (void *)btf__type_by_id(btf, v->type);
2628 m->name_off = vt->name_off;
2629 }
b03bc685 2630 } else if (!has_func && btf_is_func_proto(t)) {
d7c4b398 2631 /* replace FUNC_PROTO with ENUM */
b03bc685 2632 vlen = btf_vlen(t);
d7c4b398
AN
2633 t->info = BTF_INFO_ENC(BTF_KIND_ENUM, 0, vlen);
2634 t->size = sizeof(__u32); /* kernel enforced */
b03bc685 2635 } else if (!has_func && btf_is_func(t)) {
d7c4b398
AN
2636 /* replace FUNC with TYPEDEF */
2637 t->info = BTF_INFO_ENC(BTF_KIND_TYPEDEF, 0, 0);
2d3eb67f
AS
2638 } else if (!has_func_global && btf_is_func(t)) {
2639 /* replace BTF_FUNC_GLOBAL with BTF_FUNC_STATIC */
2640 t->info = BTF_INFO_ENC(BTF_KIND_FUNC, 0, 0);
22541a9e
IL
2641 } else if (!has_float && btf_is_float(t)) {
2642 /* replace FLOAT with an equally-sized empty STRUCT;
2643 * since C compilers do not accept e.g. "float" as a
2644 * valid struct name, make it anonymous
2645 */
2646 t->name_off = 0;
2647 t->info = BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 0);
d7c4b398
AN
2648 }
2649 }
2650}
2651
b35f14f4 2652static bool libbpf_needs_btf(const struct bpf_object *obj)
abd29c93 2653{
b35f14f4
AN
2654 return obj->efile.btf_maps_shndx >= 0 ||
2655 obj->efile.st_ops_shndx >= 0 ||
2656 obj->nr_extern > 0;
2657}
2658
2659static bool kernel_needs_btf(const struct bpf_object *obj)
2660{
2661 return obj->efile.st_ops_shndx >= 0;
abd29c93
AN
2662}
2663
063183bf 2664static int bpf_object__init_btf(struct bpf_object *obj,
9c6660d0
AN
2665 Elf_Data *btf_data,
2666 Elf_Data *btf_ext_data)
2667{
b7d7f3e1 2668 int err = -ENOENT;
9c6660d0
AN
2669
2670 if (btf_data) {
2671 obj->btf = btf__new(btf_data->d_buf, btf_data->d_size);
e9fc3ce9
AN
2672 err = libbpf_get_error(obj->btf);
2673 if (err) {
b7d7f3e1 2674 obj->btf = NULL;
e9fc3ce9 2675 pr_warn("Error loading ELF section %s: %d.\n", BTF_ELF_SEC, err);
9c6660d0
AN
2676 goto out;
2677 }
4c01925f
AN
2678 /* enforce 8-byte pointers for BPF-targeted BTFs */
2679 btf__set_pointer_size(obj->btf, 8);
9c6660d0
AN
2680 }
2681 if (btf_ext_data) {
2682 if (!obj->btf) {
2683 pr_debug("Ignore ELF section %s because its depending ELF section %s is not found.\n",
2684 BTF_EXT_ELF_SEC, BTF_ELF_SEC);
2685 goto out;
2686 }
e9fc3ce9
AN
2687 obj->btf_ext = btf_ext__new(btf_ext_data->d_buf, btf_ext_data->d_size);
2688 err = libbpf_get_error(obj->btf_ext);
2689 if (err) {
2690 pr_warn("Error loading ELF section %s: %d. Ignored and continue.\n",
2691 BTF_EXT_ELF_SEC, err);
9c6660d0
AN
2692 obj->btf_ext = NULL;
2693 goto out;
2694 }
9c6660d0
AN
2695 }
2696out:
b35f14f4 2697 if (err && libbpf_needs_btf(obj)) {
be18010e 2698 pr_warn("BTF is required, but is missing or corrupted.\n");
b7d7f3e1 2699 return err;
abd29c93 2700 }
9c6660d0
AN
2701 return 0;
2702}
2703
b96c07f3
AN
2704static int compare_vsi_off(const void *_a, const void *_b)
2705{
2706 const struct btf_var_secinfo *a = _a;
2707 const struct btf_var_secinfo *b = _b;
2708
2709 return a->offset - b->offset;
2710}
2711
2712static int btf_fixup_datasec(struct bpf_object *obj, struct btf *btf,
2713 struct btf_type *t)
2714{
2715 __u32 size = 0, off = 0, i, vars = btf_vlen(t);
2716 const char *name = btf__name_by_offset(btf, t->name_off);
2717 const struct btf_type *t_var;
2718 struct btf_var_secinfo *vsi;
2719 const struct btf_var *var;
2720 int ret;
2721
2722 if (!name) {
2723 pr_debug("No name found in string section for DATASEC kind.\n");
2724 return -ENOENT;
2725 }
2726
2727 /* .extern datasec size and var offsets were set correctly during
2728 * extern collection step, so just skip straight to sorting variables
2729 */
2730 if (t->size)
2731 goto sort_vars;
2732
2733 ret = find_elf_sec_sz(obj, name, &size);
2734 if (ret || !size || (t->size && t->size != size)) {
2735 pr_debug("Invalid size for section %s: %u bytes\n", name, size);
2736 return -ENOENT;
2737 }
2738
2739 t->size = size;
2740
2741 for (i = 0, vsi = btf_var_secinfos(t); i < vars; i++, vsi++) {
2742 t_var = btf__type_by_id(btf, vsi->type);
2743 var = btf_var(t_var);
2744
2745 if (!btf_is_var(t_var)) {
2746 pr_debug("Non-VAR type seen in section %s\n", name);
2747 return -EINVAL;
2748 }
2749
2750 if (var->linkage == BTF_VAR_STATIC)
2751 continue;
2752
2753 name = btf__name_by_offset(btf, t_var->name_off);
2754 if (!name) {
2755 pr_debug("No name found in string section for VAR kind\n");
2756 return -ENOENT;
2757 }
2758
2759 ret = find_elf_var_offset(obj, name, &off);
2760 if (ret) {
2761 pr_debug("No offset found in symbol table for VAR %s\n",
2762 name);
2763 return -ENOENT;
2764 }
2765
2766 vsi->offset = off;
2767 }
2768
2769sort_vars:
2770 qsort(btf_var_secinfos(t), vars, sizeof(*vsi), compare_vsi_off);
2771 return 0;
2772}
2773
2774static int btf_finalize_data(struct bpf_object *obj, struct btf *btf)
2775{
2776 int err = 0;
6a886de0 2777 __u32 i, n = btf__type_cnt(btf);
b96c07f3 2778
6a886de0 2779 for (i = 1; i < n; i++) {
b96c07f3
AN
2780 struct btf_type *t = btf_type_by_id(btf, i);
2781
2782 /* Loader needs to fix up some of the things compiler
2783 * couldn't get its hands on while emitting BTF. This
2784 * is section size and global variable offset. We use
2785 * the info from the ELF itself for this purpose.
2786 */
2787 if (btf_is_datasec(t)) {
2788 err = btf_fixup_datasec(obj, btf, t);
2789 if (err)
2790 break;
2791 }
2792 }
2793
2794 return libbpf_err(err);
2795}
2796
2797int btf__finalize_data(struct bpf_object *obj, struct btf *btf)
2798{
2799 return btf_finalize_data(obj, btf);
2800}
2801
166750bc
AN
2802static int bpf_object__finalize_btf(struct bpf_object *obj)
2803{
2804 int err;
2805
2806 if (!obj->btf)
2807 return 0;
2808
b96c07f3 2809 err = btf_finalize_data(obj, obj->btf);
bfc96656
AN
2810 if (err) {
2811 pr_warn("Error finalizing %s: %d.\n", BTF_ELF_SEC, err);
2812 return err;
166750bc 2813 }
bfc96656 2814
166750bc
AN
2815 return 0;
2816}
2817
fe62de31 2818static bool prog_needs_vmlinux_btf(struct bpf_program *prog)
a6ed02ca 2819{
1e092a03
KS
2820 if (prog->type == BPF_PROG_TYPE_STRUCT_OPS ||
2821 prog->type == BPF_PROG_TYPE_LSM)
a6ed02ca
KS
2822 return true;
2823
2824 /* BPF_PROG_TYPE_TRACING programs which do not attach to other programs
2825 * also need vmlinux BTF
2826 */
2827 if (prog->type == BPF_PROG_TYPE_TRACING && !prog->attach_prog_fd)
2828 return true;
2829
2830 return false;
2831}
2832
fe62de31 2833static bool obj_needs_vmlinux_btf(const struct bpf_object *obj)
a6ed02ca
KS
2834{
2835 struct bpf_program *prog;
fe62de31 2836 int i;
a6ed02ca 2837
1373ff59
SC
2838 /* CO-RE relocations need kernel BTF, only when btf_custom_path
2839 * is not specified
2840 */
2841 if (obj->btf_ext && obj->btf_ext->core_relo_info.len && !obj->btf_custom_path)
fe62de31 2842 return true;
192b6638 2843
d370bbe1
HL
2844 /* Support for typed ksyms needs kernel BTF */
2845 for (i = 0; i < obj->nr_extern; i++) {
2846 const struct extern_desc *ext;
2847
2848 ext = &obj->externs[i];
fe62de31
AN
2849 if (ext->type == EXT_KSYM && ext->ksym.type_id)
2850 return true;
d370bbe1
HL
2851 }
2852
a6ed02ca 2853 bpf_object__for_each_program(prog, obj) {
d9297581
AN
2854 if (!prog->load)
2855 continue;
fe62de31
AN
2856 if (prog_needs_vmlinux_btf(prog))
2857 return true;
a6ed02ca
KS
2858 }
2859
fe62de31
AN
2860 return false;
2861}
2862
2863static int bpf_object__load_vmlinux_btf(struct bpf_object *obj, bool force)
2864{
2865 int err;
2866
2867 /* btf_vmlinux could be loaded earlier */
67234743 2868 if (obj->btf_vmlinux || obj->gen_loader)
fe62de31
AN
2869 return 0;
2870
2871 if (!force && !obj_needs_vmlinux_btf(obj))
192b6638
AN
2872 return 0;
2873
a710eed3 2874 obj->btf_vmlinux = btf__load_vmlinux_btf();
e9fc3ce9
AN
2875 err = libbpf_get_error(obj->btf_vmlinux);
2876 if (err) {
192b6638
AN
2877 pr_warn("Error loading vmlinux BTF: %d\n", err);
2878 obj->btf_vmlinux = NULL;
2879 return err;
2880 }
a6ed02ca
KS
2881 return 0;
2882}
2883
063183bf
AN
2884static int bpf_object__sanitize_and_load_btf(struct bpf_object *obj)
2885{
0f0e55d8
AN
2886 struct btf *kern_btf = obj->btf;
2887 bool btf_mandatory, sanitize;
aea28a60 2888 int i, err = 0;
063183bf
AN
2889
2890 if (!obj->btf)
2891 return 0;
2892
9ca1f56a 2893 if (!kernel_supports(obj, FEAT_BTF)) {
68b08647
AN
2894 if (kernel_needs_btf(obj)) {
2895 err = -EOPNOTSUPP;
2896 goto report;
2897 }
2898 pr_debug("Kernel doesn't support BTF, skipping uploading it.\n");
2899 return 0;
2900 }
2901
aea28a60
AN
2902 /* Even though some subprogs are global/weak, user might prefer more
2903 * permissive BPF verification process that BPF verifier performs for
2904 * static functions, taking into account more context from the caller
2905 * functions. In such case, they need to mark such subprogs with
2906 * __attribute__((visibility("hidden"))) and libbpf will adjust
2907 * corresponding FUNC BTF type to be marked as static and trigger more
2908 * involved BPF verification process.
2909 */
2910 for (i = 0; i < obj->nr_programs; i++) {
2911 struct bpf_program *prog = &obj->programs[i];
2912 struct btf_type *t;
2913 const char *name;
2914 int j, n;
2915
2916 if (!prog->mark_btf_static || !prog_is_subprog(obj, prog))
2917 continue;
2918
6a886de0
HC
2919 n = btf__type_cnt(obj->btf);
2920 for (j = 1; j < n; j++) {
aea28a60
AN
2921 t = btf_type_by_id(obj->btf, j);
2922 if (!btf_is_func(t) || btf_func_linkage(t) != BTF_FUNC_GLOBAL)
2923 continue;
2924
2925 name = btf__str_by_offset(obj->btf, t->name_off);
2926 if (strcmp(name, prog->name) != 0)
2927 continue;
2928
2929 t->info = btf_type_info(BTF_KIND_FUNC, BTF_FUNC_STATIC, 0);
2930 break;
2931 }
2932 }
2933
0f0e55d8
AN
2934 sanitize = btf_needs_sanitization(obj);
2935 if (sanitize) {
5c3320d7 2936 const void *raw_data;
0f0e55d8 2937 __u32 sz;
063183bf 2938
0f0e55d8 2939 /* clone BTF to sanitize a copy and leave the original intact */
6a886de0 2940 raw_data = btf__raw_data(obj->btf, &sz);
5c3320d7 2941 kern_btf = btf__new(raw_data, sz);
e9fc3ce9
AN
2942 err = libbpf_get_error(kern_btf);
2943 if (err)
2944 return err;
04efe591 2945
4c01925f
AN
2946 /* enforce 8-byte pointers for BPF-targeted BTFs */
2947 btf__set_pointer_size(obj->btf, 8);
0f0e55d8 2948 bpf_object__sanitize_btf(obj, kern_btf);
063183bf 2949 }
0f0e55d8 2950
67234743
AS
2951 if (obj->gen_loader) {
2952 __u32 raw_size = 0;
6a886de0 2953 const void *raw_data = btf__raw_data(kern_btf, &raw_size);
67234743
AS
2954
2955 if (!raw_data)
2956 return -ENOMEM;
2957 bpf_gen__load_btf(obj->gen_loader, raw_data, raw_size);
2958 /* Pretend to have valid FD to pass various fd >= 0 checks.
2959 * This fd == 0 will not be used with any syscall and will be reset to -1 eventually.
2960 */
2961 btf__set_fd(kern_btf, 0);
2962 } else {
3c7e5859 2963 err = btf__load_into_kernel(kern_btf);
67234743 2964 }
0f0e55d8
AN
2965 if (sanitize) {
2966 if (!err) {
2967 /* move fd to libbpf's BTF */
2968 btf__set_fd(obj->btf, btf__fd(kern_btf));
2969 btf__set_fd(kern_btf, -1);
2970 }
2971 btf__free(kern_btf);
2972 }
68b08647 2973report:
0f0e55d8
AN
2974 if (err) {
2975 btf_mandatory = kernel_needs_btf(obj);
2976 pr_warn("Error loading .BTF into kernel: %d. %s\n", err,
2977 btf_mandatory ? "BTF is mandatory, can't proceed."
2978 : "BTF is optional, ignoring.");
2979 if (!btf_mandatory)
2980 err = 0;
2981 }
2982 return err;
063183bf
AN
2983}
2984
88a82120
AN
2985static const char *elf_sym_str(const struct bpf_object *obj, size_t off)
2986{
2987 const char *name;
2988
2989 name = elf_strptr(obj->efile.elf, obj->efile.strtabidx, off);
2990 if (!name) {
2991 pr_warn("elf: failed to get section name string at offset %zu from %s: %s\n",
2992 off, obj->path, elf_errmsg(-1));
2993 return NULL;
2994 }
2995
2996 return name;
2997}
2998
2999static const char *elf_sec_str(const struct bpf_object *obj, size_t off)
3000{
3001 const char *name;
3002
3003 name = elf_strptr(obj->efile.elf, obj->efile.shstrndx, off);
3004 if (!name) {
3005 pr_warn("elf: failed to get section name string at offset %zu from %s: %s\n",
3006 off, obj->path, elf_errmsg(-1));
3007 return NULL;
3008 }
3009
3010 return name;
3011}
3012
3013static Elf_Scn *elf_sec_by_idx(const struct bpf_object *obj, size_t idx)
3014{
3015 Elf_Scn *scn;
3016
3017 scn = elf_getscn(obj->efile.elf, idx);
3018 if (!scn) {
3019 pr_warn("elf: failed to get section(%zu) from %s: %s\n",
3020 idx, obj->path, elf_errmsg(-1));
3021 return NULL;
3022 }
3023 return scn;
3024}
3025
3026static Elf_Scn *elf_sec_by_name(const struct bpf_object *obj, const char *name)
3027{
3028 Elf_Scn *scn = NULL;
3029 Elf *elf = obj->efile.elf;
3030 const char *sec_name;
3031
3032 while ((scn = elf_nextscn(elf, scn)) != NULL) {
3033 sec_name = elf_sec_name(obj, scn);
3034 if (!sec_name)
3035 return NULL;
3036
3037 if (strcmp(sec_name, name) != 0)
3038 continue;
3039
3040 return scn;
3041 }
3042 return NULL;
3043}
3044
ad23b723 3045static Elf64_Shdr *elf_sec_hdr(const struct bpf_object *obj, Elf_Scn *scn)
88a82120 3046{
ad23b723
AN
3047 Elf64_Shdr *shdr;
3048
88a82120 3049 if (!scn)
ad23b723 3050 return NULL;
88a82120 3051
ad23b723
AN
3052 shdr = elf64_getshdr(scn);
3053 if (!shdr) {
88a82120
AN
3054 pr_warn("elf: failed to get section(%zu) header from %s: %s\n",
3055 elf_ndxscn(scn), obj->path, elf_errmsg(-1));
ad23b723 3056 return NULL;
88a82120
AN
3057 }
3058
ad23b723 3059 return shdr;
88a82120
AN
3060}
3061
3062static const char *elf_sec_name(const struct bpf_object *obj, Elf_Scn *scn)
3063{
3064 const char *name;
ad23b723 3065 Elf64_Shdr *sh;
88a82120
AN
3066
3067 if (!scn)
3068 return NULL;
3069
ad23b723
AN
3070 sh = elf_sec_hdr(obj, scn);
3071 if (!sh)
88a82120
AN
3072 return NULL;
3073
ad23b723 3074 name = elf_sec_str(obj, sh->sh_name);
88a82120
AN
3075 if (!name) {
3076 pr_warn("elf: failed to get section(%zu) name from %s: %s\n",
3077 elf_ndxscn(scn), obj->path, elf_errmsg(-1));
3078 return NULL;
3079 }
3080
3081 return name;
3082}
3083
3084static Elf_Data *elf_sec_data(const struct bpf_object *obj, Elf_Scn *scn)
3085{
3086 Elf_Data *data;
3087
3088 if (!scn)
3089 return NULL;
3090
3091 data = elf_getdata(scn, 0);
3092 if (!data) {
3093 pr_warn("elf: failed to get section(%zu) %s data from %s: %s\n",
3094 elf_ndxscn(scn), elf_sec_name(obj, scn) ?: "<?>",
3095 obj->path, elf_errmsg(-1));
3096 return NULL;
3097 }
3098
3099 return data;
3100}
3101
ad23b723
AN
3102static Elf64_Sym *elf_sym_by_idx(const struct bpf_object *obj, size_t idx)
3103{
3104 if (idx >= obj->efile.symbols->d_size / sizeof(Elf64_Sym))
3105 return NULL;
3106
3107 return (Elf64_Sym *)obj->efile.symbols->d_buf + idx;
3108}
3109
3110static Elf64_Rel *elf_rel_by_idx(Elf_Data *data, size_t idx)
3111{
3112 if (idx >= data->d_size / sizeof(Elf64_Rel))
3113 return NULL;
3114
3115 return (Elf64_Rel *)data->d_buf + idx;
3116}
3117
50e09460
AN
3118static bool is_sec_name_dwarf(const char *name)
3119{
3120 /* approximation, but the actual list is too long */
13d35a0c 3121 return str_has_pfx(name, ".debug_");
50e09460
AN
3122}
3123
ad23b723 3124static bool ignore_elf_section(Elf64_Shdr *hdr, const char *name)
50e09460
AN
3125{
3126 /* no special handling of .strtab */
3127 if (hdr->sh_type == SHT_STRTAB)
3128 return true;
3129
3130 /* ignore .llvm_addrsig section as well */
faf6ed32 3131 if (hdr->sh_type == SHT_LLVM_ADDRSIG)
50e09460
AN
3132 return true;
3133
3134 /* no subprograms will lead to an empty .text section, ignore it */
3135 if (hdr->sh_type == SHT_PROGBITS && hdr->sh_size == 0 &&
3136 strcmp(name, ".text") == 0)
3137 return true;
3138
3139 /* DWARF sections */
3140 if (is_sec_name_dwarf(name))
3141 return true;
3142
13d35a0c 3143 if (str_has_pfx(name, ".rel")) {
50e09460
AN
3144 name += sizeof(".rel") - 1;
3145 /* DWARF section relocations */
3146 if (is_sec_name_dwarf(name))
3147 return true;
3148
3149 /* .BTF and .BTF.ext don't need relocations */
3150 if (strcmp(name, BTF_ELF_SEC) == 0 ||
3151 strcmp(name, BTF_EXT_ELF_SEC) == 0)
3152 return true;
3153 }
3154
3155 return false;
3156}
3157
db2b8b06
AN
3158static int cmp_progs(const void *_a, const void *_b)
3159{
3160 const struct bpf_program *a = _a;
3161 const struct bpf_program *b = _b;
3162
3163 if (a->sec_idx != b->sec_idx)
3164 return a->sec_idx < b->sec_idx ? -1 : 1;
3165
3166 /* sec_insn_off can't be the same within the section */
3167 return a->sec_insn_off < b->sec_insn_off ? -1 : 1;
3168}
3169
0d13bfce 3170static int bpf_object__elf_collect(struct bpf_object *obj)
29603665 3171{
25bbbd7a 3172 struct elf_sec_desc *sec_desc;
29603665 3173 Elf *elf = obj->efile.elf;
f0187f0b 3174 Elf_Data *btf_ext_data = NULL;
1713d68b 3175 Elf_Data *btf_data = NULL;
666810e8 3176 int idx = 0, err = 0;
0201c575
AN
3177 const char *name;
3178 Elf_Data *data;
3179 Elf_Scn *scn;
ad23b723 3180 Elf64_Shdr *sh;
29603665 3181
25bbbd7a
AN
3182 /* ELF section indices are 1-based, so allocate +1 element to keep
3183 * indexing simple. Also include 0th invalid section into sec_cnt for
3184 * simpler and more traditional iteration logic.
3185 */
3186 obj->efile.sec_cnt = 1 + obj->efile.ehdr->e_shnum;
3187 obj->efile.secs = calloc(obj->efile.sec_cnt, sizeof(*obj->efile.secs));
3188 if (!obj->efile.secs)
3189 return -ENOMEM;
3190
0201c575
AN
3191 /* a bunch of ELF parsing functionality depends on processing symbols,
3192 * so do the first pass and find the symbol table
3193 */
3194 scn = NULL;
29603665 3195 while ((scn = elf_nextscn(elf, scn)) != NULL) {
ad23b723
AN
3196 sh = elf_sec_hdr(obj, scn);
3197 if (!sh)
0201c575
AN
3198 return -LIBBPF_ERRNO__FORMAT;
3199
ad23b723 3200 if (sh->sh_type == SHT_SYMTAB) {
0201c575
AN
3201 if (obj->efile.symbols) {
3202 pr_warn("elf: multiple symbol tables in %s\n", obj->path);
3203 return -LIBBPF_ERRNO__FORMAT;
3204 }
29603665 3205
0201c575
AN
3206 data = elf_sec_data(obj, scn);
3207 if (!data)
3208 return -LIBBPF_ERRNO__FORMAT;
3209
25bbbd7a
AN
3210 idx = elf_ndxscn(scn);
3211
0201c575 3212 obj->efile.symbols = data;
25bbbd7a 3213 obj->efile.symbols_shndx = idx;
ad23b723 3214 obj->efile.strtabidx = sh->sh_link;
0201c575
AN
3215 }
3216 }
3217
03e601f4
THJ
3218 if (!obj->efile.symbols) {
3219 pr_warn("elf: couldn't find symbol table in %s, stripped object file?\n",
3220 obj->path);
3221 return -ENOENT;
3222 }
3223
0201c575
AN
3224 scn = NULL;
3225 while ((scn = elf_nextscn(elf, scn)) != NULL) {
25bbbd7a
AN
3226 idx = elf_ndxscn(scn);
3227 sec_desc = &obj->efile.secs[idx];
88a82120 3228
ad23b723
AN
3229 sh = elf_sec_hdr(obj, scn);
3230 if (!sh)
01b29d1d 3231 return -LIBBPF_ERRNO__FORMAT;
29603665 3232
ad23b723 3233 name = elf_sec_str(obj, sh->sh_name);
88a82120 3234 if (!name)
01b29d1d 3235 return -LIBBPF_ERRNO__FORMAT;
29603665 3236
ad23b723 3237 if (ignore_elf_section(sh, name))
50e09460
AN
3238 continue;
3239
88a82120
AN
3240 data = elf_sec_data(obj, scn);
3241 if (!data)
01b29d1d 3242 return -LIBBPF_ERRNO__FORMAT;
88a82120
AN
3243
3244 pr_debug("elf: section(%d) %s, size %ld, link %d, flags %lx, type=%d\n",
077c066a 3245 idx, name, (unsigned long)data->d_size,
ad23b723
AN
3246 (int)sh->sh_link, (unsigned long)sh->sh_flags,
3247 (int)sh->sh_type);
cb1e5e96 3248
1713d68b 3249 if (strcmp(name, "license") == 0) {
88a82120 3250 err = bpf_object__init_license(obj, data->d_buf, data->d_size);
01b29d1d
AN
3251 if (err)
3252 return err;
1713d68b 3253 } else if (strcmp(name, "version") == 0) {
88a82120 3254 err = bpf_object__init_kversion(obj, data->d_buf, data->d_size);
54b8625c
JF
3255 if (err)
3256 return err;
1713d68b 3257 } else if (strcmp(name, "maps") == 0) {
666810e8 3258 obj->efile.maps_shndx = idx;
abd29c93
AN
3259 } else if (strcmp(name, MAPS_ELF_SEC) == 0) {
3260 obj->efile.btf_maps_shndx = idx;
1713d68b
DB
3261 } else if (strcmp(name, BTF_ELF_SEC) == 0) {
3262 btf_data = data;
2993e051 3263 } else if (strcmp(name, BTF_EXT_ELF_SEC) == 0) {
f0187f0b 3264 btf_ext_data = data;
ad23b723 3265 } else if (sh->sh_type == SHT_SYMTAB) {
0201c575 3266 /* already processed during the first pass above */
ad23b723
AN
3267 } else if (sh->sh_type == SHT_PROGBITS && data->d_size > 0) {
3268 if (sh->sh_flags & SHF_EXECINSTR) {
f8c7a4d4
JS
3269 if (strcmp(name, ".text") == 0)
3270 obj->efile.text_shndx = idx;
c1122392 3271 err = bpf_object__add_programs(obj, data, name, idx);
88a82120 3272 if (err)
01b29d1d 3273 return err;
aed65917
AN
3274 } else if (strcmp(name, DATA_SEC) == 0 ||
3275 str_has_pfx(name, DATA_SEC ".")) {
25bbbd7a
AN
3276 sec_desc->sec_type = SEC_DATA;
3277 sec_desc->shdr = sh;
3278 sec_desc->data = data;
aed65917
AN
3279 } else if (strcmp(name, RODATA_SEC) == 0 ||
3280 str_has_pfx(name, RODATA_SEC ".")) {
25bbbd7a
AN
3281 sec_desc->sec_type = SEC_RODATA;
3282 sec_desc->shdr = sh;
3283 sec_desc->data = data;
590a0088
MKL
3284 } else if (strcmp(name, STRUCT_OPS_SEC) == 0) {
3285 obj->efile.st_ops_data = data;
3286 obj->efile.st_ops_shndx = idx;
d859900c 3287 } else {
50e09460
AN
3288 pr_info("elf: skipping unrecognized data section(%d) %s\n",
3289 idx, name);
a5b8bd47 3290 }
ad23b723 3291 } else if (sh->sh_type == SHT_REL) {
25bbbd7a 3292 int targ_sec_idx = sh->sh_info; /* points to other section */
e3d91b0c
JDB
3293
3294 /* Only do relo for section with exec instructions */
25bbbd7a 3295 if (!section_have_execinstr(obj, targ_sec_idx) &&
646f02ff
AN
3296 strcmp(name, ".rel" STRUCT_OPS_SEC) &&
3297 strcmp(name, ".rel" MAPS_ELF_SEC)) {
50e09460 3298 pr_info("elf: skipping relo section(%d) %s for section(%d) %s\n",
25bbbd7a
AN
3299 idx, name, targ_sec_idx,
3300 elf_sec_name(obj, elf_sec_by_idx(obj, targ_sec_idx)) ?: "<?>");
e3d91b0c
JDB
3301 continue;
3302 }
b62f06e8 3303
25bbbd7a
AN
3304 sec_desc->sec_type = SEC_RELO;
3305 sec_desc->shdr = sh;
3306 sec_desc->data = data;
ad23b723 3307 } else if (sh->sh_type == SHT_NOBITS && strcmp(name, BSS_SEC) == 0) {
25bbbd7a
AN
3308 sec_desc->sec_type = SEC_BSS;
3309 sec_desc->shdr = sh;
3310 sec_desc->data = data;
077c066a 3311 } else {
2e80be60 3312 pr_info("elf: skipping section(%d) %s (size %zu)\n", idx, name,
ad23b723 3313 (size_t)sh->sh_size);
bec7d68c 3314 }
29603665 3315 }
561bbcca 3316
d3a3aa0c 3317 if (!obj->efile.strtabidx || obj->efile.strtabidx > idx) {
88a82120 3318 pr_warn("elf: symbol strings section missing or invalid in %s\n", obj->path);
f102154d 3319 return -LIBBPF_ERRNO__FORMAT;
77ba9a5b 3320 }
db2b8b06
AN
3321
3322 /* sort BPF programs by section name and in-section instruction offset
3323 * for faster search */
3324 qsort(obj->programs, obj->nr_programs, sizeof(*obj->programs), cmp_progs);
3325
0d13bfce 3326 return bpf_object__init_btf(obj, btf_data, btf_ext_data);
29603665
WN
3327}
3328
ad23b723 3329static bool sym_is_extern(const Elf64_Sym *sym)
166750bc 3330{
ad23b723 3331 int bind = ELF64_ST_BIND(sym->st_info);
166750bc
AN
3332 /* externs are symbols w/ type=NOTYPE, bind=GLOBAL|WEAK, section=UND */
3333 return sym->st_shndx == SHN_UNDEF &&
3334 (bind == STB_GLOBAL || bind == STB_WEAK) &&
ad23b723 3335 ELF64_ST_TYPE(sym->st_info) == STT_NOTYPE;
166750bc
AN
3336}
3337
ad23b723 3338static bool sym_is_subprog(const Elf64_Sym *sym, int text_shndx)
53eddb5e 3339{
ad23b723
AN
3340 int bind = ELF64_ST_BIND(sym->st_info);
3341 int type = ELF64_ST_TYPE(sym->st_info);
53eddb5e
YS
3342
3343 /* in .text section */
3344 if (sym->st_shndx != text_shndx)
3345 return false;
3346
3347 /* local function */
3348 if (bind == STB_LOCAL && type == STT_SECTION)
3349 return true;
3350
3351 /* global function */
3352 return bind == STB_GLOBAL && type == STT_FUNC;
3353}
3354
166750bc
AN
3355static int find_extern_btf_id(const struct btf *btf, const char *ext_name)
3356{
3357 const struct btf_type *t;
5bd022ec 3358 const char *tname;
166750bc
AN
3359 int i, n;
3360
3361 if (!btf)
3362 return -ESRCH;
3363
6a886de0
HC
3364 n = btf__type_cnt(btf);
3365 for (i = 1; i < n; i++) {
166750bc
AN
3366 t = btf__type_by_id(btf, i);
3367
5bd022ec 3368 if (!btf_is_var(t) && !btf_is_func(t))
166750bc
AN
3369 continue;
3370
5bd022ec
MKL
3371 tname = btf__name_by_offset(btf, t->name_off);
3372 if (strcmp(tname, ext_name))
166750bc
AN
3373 continue;
3374
5bd022ec
MKL
3375 if (btf_is_var(t) &&
3376 btf_var(t)->linkage != BTF_VAR_GLOBAL_EXTERN)
3377 return -EINVAL;
3378
3379 if (btf_is_func(t) && btf_func_linkage(t) != BTF_FUNC_EXTERN)
166750bc
AN
3380 return -EINVAL;
3381
3382 return i;
3383 }
3384
3385 return -ENOENT;
3386}
3387
2e33efe3
AN
3388static int find_extern_sec_btf_id(struct btf *btf, int ext_btf_id) {
3389 const struct btf_var_secinfo *vs;
3390 const struct btf_type *t;
3391 int i, j, n;
3392
3393 if (!btf)
3394 return -ESRCH;
3395
6a886de0
HC
3396 n = btf__type_cnt(btf);
3397 for (i = 1; i < n; i++) {
2e33efe3
AN
3398 t = btf__type_by_id(btf, i);
3399
3400 if (!btf_is_datasec(t))
3401 continue;
3402
3403 vs = btf_var_secinfos(t);
3404 for (j = 0; j < btf_vlen(t); j++, vs++) {
3405 if (vs->type == ext_btf_id)
3406 return i;
3407 }
3408 }
3409
3410 return -ENOENT;
3411}
3412
3413static enum kcfg_type find_kcfg_type(const struct btf *btf, int id,
3414 bool *is_signed)
166750bc
AN
3415{
3416 const struct btf_type *t;
3417 const char *name;
3418
3419 t = skip_mods_and_typedefs(btf, id, NULL);
3420 name = btf__name_by_offset(btf, t->name_off);
3421
3422 if (is_signed)
3423 *is_signed = false;
3424 switch (btf_kind(t)) {
3425 case BTF_KIND_INT: {
3426 int enc = btf_int_encoding(t);
3427
3428 if (enc & BTF_INT_BOOL)
2e33efe3 3429 return t->size == 1 ? KCFG_BOOL : KCFG_UNKNOWN;
166750bc
AN
3430 if (is_signed)
3431 *is_signed = enc & BTF_INT_SIGNED;
3432 if (t->size == 1)
2e33efe3 3433 return KCFG_CHAR;
166750bc 3434 if (t->size < 1 || t->size > 8 || (t->size & (t->size - 1)))
2e33efe3
AN
3435 return KCFG_UNKNOWN;
3436 return KCFG_INT;
166750bc
AN
3437 }
3438 case BTF_KIND_ENUM:
3439 if (t->size != 4)
2e33efe3 3440 return KCFG_UNKNOWN;
166750bc 3441 if (strcmp(name, "libbpf_tristate"))
2e33efe3
AN
3442 return KCFG_UNKNOWN;
3443 return KCFG_TRISTATE;
166750bc
AN
3444 case BTF_KIND_ARRAY:
3445 if (btf_array(t)->nelems == 0)
2e33efe3
AN
3446 return KCFG_UNKNOWN;
3447 if (find_kcfg_type(btf, btf_array(t)->type, NULL) != KCFG_CHAR)
3448 return KCFG_UNKNOWN;
3449 return KCFG_CHAR_ARR;
166750bc 3450 default:
2e33efe3 3451 return KCFG_UNKNOWN;
166750bc
AN
3452 }
3453}
3454
3455static int cmp_externs(const void *_a, const void *_b)
3456{
3457 const struct extern_desc *a = _a;
3458 const struct extern_desc *b = _b;
3459
2e33efe3
AN
3460 if (a->type != b->type)
3461 return a->type < b->type ? -1 : 1;
3462
3463 if (a->type == EXT_KCFG) {
3464 /* descending order by alignment requirements */
3465 if (a->kcfg.align != b->kcfg.align)
3466 return a->kcfg.align > b->kcfg.align ? -1 : 1;
3467 /* ascending order by size, within same alignment class */
3468 if (a->kcfg.sz != b->kcfg.sz)
3469 return a->kcfg.sz < b->kcfg.sz ? -1 : 1;
3470 }
3471
166750bc
AN
3472 /* resolve ties by name */
3473 return strcmp(a->name, b->name);
3474}
3475
1c0c7074
AN
3476static int find_int_btf_id(const struct btf *btf)
3477{
3478 const struct btf_type *t;
3479 int i, n;
3480
6a886de0
HC
3481 n = btf__type_cnt(btf);
3482 for (i = 1; i < n; i++) {
1c0c7074
AN
3483 t = btf__type_by_id(btf, i);
3484
3485 if (btf_is_int(t) && btf_int_bits(t) == 32)
3486 return i;
3487 }
3488
3489 return 0;
3490}
3491
5bd022ec
MKL
3492static int add_dummy_ksym_var(struct btf *btf)
3493{
3494 int i, int_btf_id, sec_btf_id, dummy_var_btf_id;
3495 const struct btf_var_secinfo *vs;
3496 const struct btf_type *sec;
3497
9683e577
IR
3498 if (!btf)
3499 return 0;
3500
5bd022ec
MKL
3501 sec_btf_id = btf__find_by_name_kind(btf, KSYMS_SEC,
3502 BTF_KIND_DATASEC);
3503 if (sec_btf_id < 0)
3504 return 0;
3505
3506 sec = btf__type_by_id(btf, sec_btf_id);
3507 vs = btf_var_secinfos(sec);
3508 for (i = 0; i < btf_vlen(sec); i++, vs++) {
3509 const struct btf_type *vt;
3510
3511 vt = btf__type_by_id(btf, vs->type);
3512 if (btf_is_func(vt))
3513 break;
3514 }
3515
3516 /* No func in ksyms sec. No need to add dummy var. */
3517 if (i == btf_vlen(sec))
3518 return 0;
3519
3520 int_btf_id = find_int_btf_id(btf);
3521 dummy_var_btf_id = btf__add_var(btf,
3522 "dummy_ksym",
3523 BTF_VAR_GLOBAL_ALLOCATED,
3524 int_btf_id);
3525 if (dummy_var_btf_id < 0)
3526 pr_warn("cannot create a dummy_ksym var\n");
3527
3528 return dummy_var_btf_id;
3529}
3530
166750bc
AN
3531static int bpf_object__collect_externs(struct bpf_object *obj)
3532{
1c0c7074 3533 struct btf_type *sec, *kcfg_sec = NULL, *ksym_sec = NULL;
166750bc
AN
3534 const struct btf_type *t;
3535 struct extern_desc *ext;
5bd022ec 3536 int i, n, off, dummy_var_btf_id;
2e33efe3 3537 const char *ext_name, *sec_name;
166750bc 3538 Elf_Scn *scn;
ad23b723 3539 Elf64_Shdr *sh;
166750bc
AN
3540
3541 if (!obj->efile.symbols)
3542 return 0;
3543
88a82120 3544 scn = elf_sec_by_idx(obj, obj->efile.symbols_shndx);
ad23b723
AN
3545 sh = elf_sec_hdr(obj, scn);
3546 if (!sh)
166750bc 3547 return -LIBBPF_ERRNO__FORMAT;
166750bc 3548
5bd022ec
MKL
3549 dummy_var_btf_id = add_dummy_ksym_var(obj->btf);
3550 if (dummy_var_btf_id < 0)
3551 return dummy_var_btf_id;
3552
ad23b723 3553 n = sh->sh_size / sh->sh_entsize;
166750bc 3554 pr_debug("looking for externs among %d symbols...\n", n);
88a82120 3555
166750bc 3556 for (i = 0; i < n; i++) {
ad23b723 3557 Elf64_Sym *sym = elf_sym_by_idx(obj, i);
166750bc 3558
ad23b723 3559 if (!sym)
166750bc 3560 return -LIBBPF_ERRNO__FORMAT;
ad23b723 3561 if (!sym_is_extern(sym))
166750bc 3562 continue;
ad23b723 3563 ext_name = elf_sym_str(obj, sym->st_name);
166750bc
AN
3564 if (!ext_name || !ext_name[0])
3565 continue;
3566
3567 ext = obj->externs;
029258d7 3568 ext = libbpf_reallocarray(ext, obj->nr_extern + 1, sizeof(*ext));
166750bc
AN
3569 if (!ext)
3570 return -ENOMEM;
3571 obj->externs = ext;
3572 ext = &ext[obj->nr_extern];
3573 memset(ext, 0, sizeof(*ext));
3574 obj->nr_extern++;
3575
3576 ext->btf_id = find_extern_btf_id(obj->btf, ext_name);
3577 if (ext->btf_id <= 0) {
3578 pr_warn("failed to find BTF for extern '%s': %d\n",
3579 ext_name, ext->btf_id);
3580 return ext->btf_id;
3581 }
3582 t = btf__type_by_id(obj->btf, ext->btf_id);
3583 ext->name = btf__name_by_offset(obj->btf, t->name_off);
3584 ext->sym_idx = i;
ad23b723 3585 ext->is_weak = ELF64_ST_BIND(sym->st_info) == STB_WEAK;
2e33efe3
AN
3586
3587 ext->sec_btf_id = find_extern_sec_btf_id(obj->btf, ext->btf_id);
3588 if (ext->sec_btf_id <= 0) {
3589 pr_warn("failed to find BTF for extern '%s' [%d] section: %d\n",
3590 ext_name, ext->btf_id, ext->sec_btf_id);
3591 return ext->sec_btf_id;
166750bc 3592 }
2e33efe3
AN
3593 sec = (void *)btf__type_by_id(obj->btf, ext->sec_btf_id);
3594 sec_name = btf__name_by_offset(obj->btf, sec->name_off);
3595
3596 if (strcmp(sec_name, KCONFIG_SEC) == 0) {
5bd022ec
MKL
3597 if (btf_is_func(t)) {
3598 pr_warn("extern function %s is unsupported under %s section\n",
3599 ext->name, KCONFIG_SEC);
3600 return -ENOTSUP;
3601 }
2e33efe3
AN
3602 kcfg_sec = sec;
3603 ext->type = EXT_KCFG;
3604 ext->kcfg.sz = btf__resolve_size(obj->btf, t->type);
3605 if (ext->kcfg.sz <= 0) {
3606 pr_warn("failed to resolve size of extern (kcfg) '%s': %d\n",
3607 ext_name, ext->kcfg.sz);
3608 return ext->kcfg.sz;
3609 }
3610 ext->kcfg.align = btf__align_of(obj->btf, t->type);
3611 if (ext->kcfg.align <= 0) {
3612 pr_warn("failed to determine alignment of extern (kcfg) '%s': %d\n",
3613 ext_name, ext->kcfg.align);
3614 return -EINVAL;
3615 }
3616 ext->kcfg.type = find_kcfg_type(obj->btf, t->type,
3617 &ext->kcfg.is_signed);
3618 if (ext->kcfg.type == KCFG_UNKNOWN) {
3619 pr_warn("extern (kcfg) '%s' type is unsupported\n", ext_name);
3620 return -ENOTSUP;
3621 }
1c0c7074 3622 } else if (strcmp(sec_name, KSYMS_SEC) == 0) {
1c0c7074
AN
3623 ksym_sec = sec;
3624 ext->type = EXT_KSYM;
d370bbe1
HL
3625 skip_mods_and_typedefs(obj->btf, t->type,
3626 &ext->ksym.type_id);
2e33efe3
AN
3627 } else {
3628 pr_warn("unrecognized extern section '%s'\n", sec_name);
166750bc
AN
3629 return -ENOTSUP;
3630 }
3631 }
3632 pr_debug("collected %d externs total\n", obj->nr_extern);
3633
3634 if (!obj->nr_extern)
3635 return 0;
3636
2e33efe3 3637 /* sort externs by type, for kcfg ones also by (align, size, name) */
166750bc 3638 qsort(obj->externs, obj->nr_extern, sizeof(*ext), cmp_externs);
166750bc 3639
1c0c7074
AN
3640 /* for .ksyms section, we need to turn all externs into allocated
3641 * variables in BTF to pass kernel verification; we do this by
3642 * pretending that each extern is a 8-byte variable
3643 */
3644 if (ksym_sec) {
3645 /* find existing 4-byte integer type in BTF to use for fake
3646 * extern variables in DATASEC
3647 */
3648 int int_btf_id = find_int_btf_id(obj->btf);
5bd022ec
MKL
3649 /* For extern function, a dummy_var added earlier
3650 * will be used to replace the vs->type and
3651 * its name string will be used to refill
3652 * the missing param's name.
3653 */
3654 const struct btf_type *dummy_var;
1c0c7074 3655
5bd022ec 3656 dummy_var = btf__type_by_id(obj->btf, dummy_var_btf_id);
1c0c7074
AN
3657 for (i = 0; i < obj->nr_extern; i++) {
3658 ext = &obj->externs[i];
3659 if (ext->type != EXT_KSYM)
3660 continue;
3661 pr_debug("extern (ksym) #%d: symbol %d, name %s\n",
3662 i, ext->sym_idx, ext->name);
3663 }
3664
3665 sec = ksym_sec;
3666 n = btf_vlen(sec);
3667 for (i = 0, off = 0; i < n; i++, off += sizeof(int)) {
3668 struct btf_var_secinfo *vs = btf_var_secinfos(sec) + i;
3669 struct btf_type *vt;
3670
3671 vt = (void *)btf__type_by_id(obj->btf, vs->type);
3672 ext_name = btf__name_by_offset(obj->btf, vt->name_off);
3673 ext = find_extern_by_name(obj, ext_name);
3674 if (!ext) {
5bd022ec
MKL
3675 pr_warn("failed to find extern definition for BTF %s '%s'\n",
3676 btf_kind_str(vt), ext_name);
1c0c7074
AN
3677 return -ESRCH;
3678 }
5bd022ec
MKL
3679 if (btf_is_func(vt)) {
3680 const struct btf_type *func_proto;
3681 struct btf_param *param;
3682 int j;
3683
3684 func_proto = btf__type_by_id(obj->btf,
3685 vt->type);
3686 param = btf_params(func_proto);
3687 /* Reuse the dummy_var string if the
3688 * func proto does not have param name.
3689 */
3690 for (j = 0; j < btf_vlen(func_proto); j++)
3691 if (param[j].type && !param[j].name_off)
3692 param[j].name_off =
3693 dummy_var->name_off;
3694 vs->type = dummy_var_btf_id;
3695 vt->info &= ~0xffff;
3696 vt->info |= BTF_FUNC_GLOBAL;
3697 } else {
3698 btf_var(vt)->linkage = BTF_VAR_GLOBAL_ALLOCATED;
3699 vt->type = int_btf_id;
3700 }
1c0c7074
AN
3701 vs->offset = off;
3702 vs->size = sizeof(int);
3703 }
3704 sec->size = off;
3705 }
3706
2e33efe3
AN
3707 if (kcfg_sec) {
3708 sec = kcfg_sec;
3709 /* for kcfg externs calculate their offsets within a .kconfig map */
3710 off = 0;
3711 for (i = 0; i < obj->nr_extern; i++) {
3712 ext = &obj->externs[i];
3713 if (ext->type != EXT_KCFG)
3714 continue;
166750bc 3715
2e33efe3
AN
3716 ext->kcfg.data_off = roundup(off, ext->kcfg.align);
3717 off = ext->kcfg.data_off + ext->kcfg.sz;
1c0c7074 3718 pr_debug("extern (kcfg) #%d: symbol %d, off %u, name %s\n",
2e33efe3
AN
3719 i, ext->sym_idx, ext->kcfg.data_off, ext->name);
3720 }
3721 sec->size = off;
3722 n = btf_vlen(sec);
3723 for (i = 0; i < n; i++) {
3724 struct btf_var_secinfo *vs = btf_var_secinfos(sec) + i;
3725
3726 t = btf__type_by_id(obj->btf, vs->type);
3727 ext_name = btf__name_by_offset(obj->btf, t->name_off);
3728 ext = find_extern_by_name(obj, ext_name);
3729 if (!ext) {
3730 pr_warn("failed to find extern definition for BTF var '%s'\n",
3731 ext_name);
3732 return -ESRCH;
3733 }
3734 btf_var(t)->linkage = BTF_VAR_GLOBAL_ALLOCATED;
3735 vs->offset = ext->kcfg.data_off;
166750bc 3736 }
166750bc 3737 }
166750bc
AN
3738 return 0;
3739}
3740
6d4b198b 3741struct bpf_program *
a324aae3
AN
3742bpf_object__find_program_by_title(const struct bpf_object *obj,
3743 const char *title)
6d4b198b
JK
3744{
3745 struct bpf_program *pos;
3746
3747 bpf_object__for_each_program(pos, obj) {
52109584 3748 if (pos->sec_name && !strcmp(pos->sec_name, title))
6d4b198b
JK
3749 return pos;
3750 }
e9fc3ce9 3751 return errno = ENOENT, NULL;
6d4b198b
JK
3752}
3753
c3c55696
AN
3754static bool prog_is_subprog(const struct bpf_object *obj,
3755 const struct bpf_program *prog)
3756{
197afc63
AN
3757 /* For legacy reasons, libbpf supports an entry-point BPF programs
3758 * without SEC() attribute, i.e., those in the .text section. But if
3759 * there are 2 or more such programs in the .text section, they all
3760 * must be subprograms called from entry-point BPF programs in
3761 * designated SEC()'tions, otherwise there is no way to distinguish
3762 * which of those programs should be loaded vs which are a subprogram.
3763 * Similarly, if there is a function/program in .text and at least one
3764 * other BPF program with custom SEC() attribute, then we just assume
3765 * .text programs are subprograms (even if they are not called from
3766 * other programs), because libbpf never explicitly supported mixing
3767 * SEC()-designated BPF programs and .text entry-point BPF programs.
3768 */
3769 return prog->sec_idx == obj->efile.text_shndx && obj->nr_programs > 1;
c3c55696
AN
3770}
3771
01af3bf0
AN
3772struct bpf_program *
3773bpf_object__find_program_by_name(const struct bpf_object *obj,
3774 const char *name)
3775{
3776 struct bpf_program *prog;
3777
3778 bpf_object__for_each_program(prog, obj) {
c3c55696
AN
3779 if (prog_is_subprog(obj, prog))
3780 continue;
01af3bf0
AN
3781 if (!strcmp(prog->name, name))
3782 return prog;
3783 }
e9fc3ce9 3784 return errno = ENOENT, NULL;
01af3bf0
AN
3785}
3786
d859900c
DB
3787static bool bpf_object__shndx_is_data(const struct bpf_object *obj,
3788 int shndx)
3789{
25bbbd7a
AN
3790 switch (obj->efile.secs[shndx].sec_type) {
3791 case SEC_BSS:
3792 case SEC_DATA:
3793 case SEC_RODATA:
3794 return true;
3795 default:
3796 return false;
3797 }
d859900c
DB
3798}
3799
3800static bool bpf_object__shndx_is_maps(const struct bpf_object *obj,
3801 int shndx)
3802{
abd29c93
AN
3803 return shndx == obj->efile.maps_shndx ||
3804 shndx == obj->efile.btf_maps_shndx;
d859900c
DB
3805}
3806
d859900c
DB
3807static enum libbpf_map_type
3808bpf_object__section_to_libbpf_map_type(const struct bpf_object *obj, int shndx)
3809{
25bbbd7a
AN
3810 if (shndx == obj->efile.symbols_shndx)
3811 return LIBBPF_MAP_KCONFIG;
3812
3813 switch (obj->efile.secs[shndx].sec_type) {
3814 case SEC_BSS:
d859900c 3815 return LIBBPF_MAP_BSS;
25bbbd7a
AN
3816 case SEC_DATA:
3817 return LIBBPF_MAP_DATA;
3818 case SEC_RODATA:
d859900c 3819 return LIBBPF_MAP_RODATA;
25bbbd7a 3820 default:
d859900c 3821 return LIBBPF_MAP_UNSPEC;
25bbbd7a 3822 }
d859900c
DB
3823}
3824
1f8e2bcb
AN
3825static int bpf_program__record_reloc(struct bpf_program *prog,
3826 struct reloc_desc *reloc_desc,
9c0f8cbd 3827 __u32 insn_idx, const char *sym_name,
ad23b723 3828 const Elf64_Sym *sym, const Elf64_Rel *rel)
1f8e2bcb
AN
3829{
3830 struct bpf_insn *insn = &prog->insns[insn_idx];
3831 size_t map_idx, nr_maps = prog->obj->nr_maps;
3832 struct bpf_object *obj = prog->obj;
3833 __u32 shdr_idx = sym->st_shndx;
3834 enum libbpf_map_type type;
9c0f8cbd 3835 const char *sym_sec_name;
1f8e2bcb
AN
3836 struct bpf_map *map;
3837
aa0b8d43 3838 if (!is_call_insn(insn) && !is_ldimm64_insn(insn)) {
9c0f8cbd
AN
3839 pr_warn("prog '%s': invalid relo against '%s' for insns[%d].code 0x%x\n",
3840 prog->name, sym_name, insn_idx, insn->code);
1f8e2bcb
AN
3841 return -LIBBPF_ERRNO__RELOC;
3842 }
166750bc
AN
3843
3844 if (sym_is_extern(sym)) {
ad23b723 3845 int sym_idx = ELF64_R_SYM(rel->r_info);
166750bc
AN
3846 int i, n = obj->nr_extern;
3847 struct extern_desc *ext;
3848
3849 for (i = 0; i < n; i++) {
3850 ext = &obj->externs[i];
3851 if (ext->sym_idx == sym_idx)
3852 break;
3853 }
3854 if (i >= n) {
9c0f8cbd
AN
3855 pr_warn("prog '%s': extern relo failed to find extern for '%s' (%d)\n",
3856 prog->name, sym_name, sym_idx);
166750bc
AN
3857 return -LIBBPF_ERRNO__RELOC;
3858 }
9c0f8cbd
AN
3859 pr_debug("prog '%s': found extern #%d '%s' (sym %d) for insn #%u\n",
3860 prog->name, i, ext->name, ext->sym_idx, insn_idx);
5bd022ec
MKL
3861 if (insn->code == (BPF_JMP | BPF_CALL))
3862 reloc_desc->type = RELO_EXTERN_FUNC;
3863 else
3864 reloc_desc->type = RELO_EXTERN_VAR;
166750bc 3865 reloc_desc->insn_idx = insn_idx;
2e33efe3 3866 reloc_desc->sym_off = i; /* sym_off stores extern index */
166750bc
AN
3867 return 0;
3868 }
3869
aa0b8d43
MKL
3870 /* sub-program call relocation */
3871 if (is_call_insn(insn)) {
3872 if (insn->src_reg != BPF_PSEUDO_CALL) {
3873 pr_warn("prog '%s': incorrect bpf_call opcode\n", prog->name);
3874 return -LIBBPF_ERRNO__RELOC;
3875 }
3876 /* text_shndx can be 0, if no default "main" program exists */
3877 if (!shdr_idx || shdr_idx != obj->efile.text_shndx) {
3878 sym_sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, shdr_idx));
3879 pr_warn("prog '%s': bad call relo against '%s' in section '%s'\n",
3880 prog->name, sym_name, sym_sec_name);
3881 return -LIBBPF_ERRNO__RELOC;
3882 }
3883 if (sym->st_value % BPF_INSN_SZ) {
3884 pr_warn("prog '%s': bad call relo against '%s' at offset %zu\n",
3885 prog->name, sym_name, (size_t)sym->st_value);
3886 return -LIBBPF_ERRNO__RELOC;
3887 }
3888 reloc_desc->type = RELO_CALL;
3889 reloc_desc->insn_idx = insn_idx;
3890 reloc_desc->sym_off = sym->st_value;
3891 return 0;
3892 }
3893
1f8e2bcb 3894 if (!shdr_idx || shdr_idx >= SHN_LORESERVE) {
9c0f8cbd
AN
3895 pr_warn("prog '%s': invalid relo against '%s' in special section 0x%x; forgot to initialize global var?..\n",
3896 prog->name, sym_name, shdr_idx);
1f8e2bcb
AN
3897 return -LIBBPF_ERRNO__RELOC;
3898 }
3899
53eddb5e
YS
3900 /* loading subprog addresses */
3901 if (sym_is_subprog(sym, obj->efile.text_shndx)) {
3902 /* global_func: sym->st_value = offset in the section, insn->imm = 0.
3903 * local_func: sym->st_value = 0, insn->imm = offset in the section.
3904 */
3905 if ((sym->st_value % BPF_INSN_SZ) || (insn->imm % BPF_INSN_SZ)) {
3906 pr_warn("prog '%s': bad subprog addr relo against '%s' at offset %zu+%d\n",
3907 prog->name, sym_name, (size_t)sym->st_value, insn->imm);
3908 return -LIBBPF_ERRNO__RELOC;
3909 }
3910
3911 reloc_desc->type = RELO_SUBPROG_ADDR;
3912 reloc_desc->insn_idx = insn_idx;
3913 reloc_desc->sym_off = sym->st_value;
3914 return 0;
3915 }
3916
1f8e2bcb 3917 type = bpf_object__section_to_libbpf_map_type(obj, shdr_idx);
9c0f8cbd 3918 sym_sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, shdr_idx));
1f8e2bcb
AN
3919
3920 /* generic map reference relocation */
3921 if (type == LIBBPF_MAP_UNSPEC) {
3922 if (!bpf_object__shndx_is_maps(obj, shdr_idx)) {
9c0f8cbd
AN
3923 pr_warn("prog '%s': bad map relo against '%s' in section '%s'\n",
3924 prog->name, sym_name, sym_sec_name);
1f8e2bcb
AN
3925 return -LIBBPF_ERRNO__RELOC;
3926 }
3927 for (map_idx = 0; map_idx < nr_maps; map_idx++) {
3928 map = &obj->maps[map_idx];
3929 if (map->libbpf_type != type ||
3930 map->sec_idx != sym->st_shndx ||
3931 map->sec_offset != sym->st_value)
3932 continue;
9c0f8cbd
AN
3933 pr_debug("prog '%s': found map %zd (%s, sec %d, off %zu) for insn #%u\n",
3934 prog->name, map_idx, map->name, map->sec_idx,
1f8e2bcb
AN
3935 map->sec_offset, insn_idx);
3936 break;
3937 }
3938 if (map_idx >= nr_maps) {
9c0f8cbd
AN
3939 pr_warn("prog '%s': map relo failed to find map for section '%s', off %zu\n",
3940 prog->name, sym_sec_name, (size_t)sym->st_value);
1f8e2bcb
AN
3941 return -LIBBPF_ERRNO__RELOC;
3942 }
3943 reloc_desc->type = RELO_LD64;
3944 reloc_desc->insn_idx = insn_idx;
3945 reloc_desc->map_idx = map_idx;
53f8dd43 3946 reloc_desc->sym_off = 0; /* sym->st_value determines map_idx */
1f8e2bcb
AN
3947 return 0;
3948 }
3949
3950 /* global data map relocation */
3951 if (!bpf_object__shndx_is_data(obj, shdr_idx)) {
9c0f8cbd
AN
3952 pr_warn("prog '%s': bad data relo against section '%s'\n",
3953 prog->name, sym_sec_name);
1f8e2bcb 3954 return -LIBBPF_ERRNO__RELOC;
1f8e2bcb 3955 }
1f8e2bcb
AN
3956 for (map_idx = 0; map_idx < nr_maps; map_idx++) {
3957 map = &obj->maps[map_idx];
25bbbd7a 3958 if (map->libbpf_type != type || map->sec_idx != sym->st_shndx)
1f8e2bcb 3959 continue;
9c0f8cbd
AN
3960 pr_debug("prog '%s': found data map %zd (%s, sec %d, off %zu) for insn %u\n",
3961 prog->name, map_idx, map->name, map->sec_idx,
3962 map->sec_offset, insn_idx);
1f8e2bcb
AN
3963 break;
3964 }
3965 if (map_idx >= nr_maps) {
9c0f8cbd
AN
3966 pr_warn("prog '%s': data relo failed to find map for section '%s'\n",
3967 prog->name, sym_sec_name);
1f8e2bcb
AN
3968 return -LIBBPF_ERRNO__RELOC;
3969 }
3970
3971 reloc_desc->type = RELO_DATA;
3972 reloc_desc->insn_idx = insn_idx;
3973 reloc_desc->map_idx = map_idx;
53f8dd43 3974 reloc_desc->sym_off = sym->st_value;
1f8e2bcb
AN
3975 return 0;
3976}
3977
db2b8b06
AN
3978static bool prog_contains_insn(const struct bpf_program *prog, size_t insn_idx)
3979{
3980 return insn_idx >= prog->sec_insn_off &&
3981 insn_idx < prog->sec_insn_off + prog->sec_insn_cnt;
3982}
3983
3984static struct bpf_program *find_prog_by_sec_insn(const struct bpf_object *obj,
3985 size_t sec_idx, size_t insn_idx)
3986{
3987 int l = 0, r = obj->nr_programs - 1, m;
3988 struct bpf_program *prog;
3989
3990 while (l < r) {
3991 m = l + (r - l + 1) / 2;
3992 prog = &obj->programs[m];
3993
3994 if (prog->sec_idx < sec_idx ||
3995 (prog->sec_idx == sec_idx && prog->sec_insn_off <= insn_idx))
3996 l = m;
3997 else
3998 r = m - 1;
3999 }
4000 /* matching program could be at index l, but it still might be the
4001 * wrong one, so we need to double check conditions for the last time
4002 */
4003 prog = &obj->programs[l];
4004 if (prog->sec_idx == sec_idx && prog_contains_insn(prog, insn_idx))
4005 return prog;
4006 return NULL;
4007}
4008
34090915 4009static int
ad23b723 4010bpf_object__collect_prog_relos(struct bpf_object *obj, Elf64_Shdr *shdr, Elf_Data *data)
34090915 4011{
9c0f8cbd
AN
4012 const char *relo_sec_name, *sec_name;
4013 size_t sec_idx = shdr->sh_info;
c3c55696
AN
4014 struct bpf_program *prog;
4015 struct reloc_desc *relos;
1f8e2bcb 4016 int err, i, nrels;
c3c55696
AN
4017 const char *sym_name;
4018 __u32 insn_idx;
6245947c
AN
4019 Elf_Scn *scn;
4020 Elf_Data *scn_data;
ad23b723
AN
4021 Elf64_Sym *sym;
4022 Elf64_Rel *rel;
34090915 4023
6245947c
AN
4024 scn = elf_sec_by_idx(obj, sec_idx);
4025 scn_data = elf_sec_data(obj, scn);
4026
9c0f8cbd 4027 relo_sec_name = elf_sec_str(obj, shdr->sh_name);
6245947c 4028 sec_name = elf_sec_name(obj, scn);
9c0f8cbd
AN
4029 if (!relo_sec_name || !sec_name)
4030 return -EINVAL;
4031
4032 pr_debug("sec '%s': collecting relocation for section(%zu) '%s'\n",
4033 relo_sec_name, sec_idx, sec_name);
34090915
WN
4034 nrels = shdr->sh_size / shdr->sh_entsize;
4035
34090915 4036 for (i = 0; i < nrels; i++) {
ad23b723
AN
4037 rel = elf_rel_by_idx(data, i);
4038 if (!rel) {
9c0f8cbd 4039 pr_warn("sec '%s': failed to get relo #%d\n", relo_sec_name, i);
6371ca3b 4040 return -LIBBPF_ERRNO__FORMAT;
34090915 4041 }
ad23b723
AN
4042
4043 sym = elf_sym_by_idx(obj, ELF64_R_SYM(rel->r_info));
4044 if (!sym) {
9c0f8cbd 4045 pr_warn("sec '%s': symbol 0x%zx not found for relo #%d\n",
ad23b723 4046 relo_sec_name, (size_t)ELF64_R_SYM(rel->r_info), i);
6371ca3b 4047 return -LIBBPF_ERRNO__FORMAT;
34090915 4048 }
6245947c 4049
ad23b723 4050 if (rel->r_offset % BPF_INSN_SZ || rel->r_offset >= scn_data->d_size) {
9c0f8cbd 4051 pr_warn("sec '%s': invalid offset 0x%zx for relo #%d\n",
ad23b723 4052 relo_sec_name, (size_t)ELF64_R_SYM(rel->r_info), i);
1f8e2bcb 4053 return -LIBBPF_ERRNO__FORMAT;
9c0f8cbd 4054 }
d859900c 4055
ad23b723 4056 insn_idx = rel->r_offset / BPF_INSN_SZ;
c3c55696
AN
4057 /* relocations against static functions are recorded as
4058 * relocations against the section that contains a function;
4059 * in such case, symbol will be STT_SECTION and sym.st_name
4060 * will point to empty string (0), so fetch section name
4061 * instead
4062 */
ad23b723
AN
4063 if (ELF64_ST_TYPE(sym->st_info) == STT_SECTION && sym->st_name == 0)
4064 sym_name = elf_sec_name(obj, elf_sec_by_idx(obj, sym->st_shndx));
c3c55696 4065 else
ad23b723 4066 sym_name = elf_sym_str(obj, sym->st_name);
c3c55696 4067 sym_name = sym_name ?: "<?";
d859900c 4068
9c0f8cbd
AN
4069 pr_debug("sec '%s': relo #%d: insn #%u against '%s'\n",
4070 relo_sec_name, i, insn_idx, sym_name);
666810e8 4071
c3c55696
AN
4072 prog = find_prog_by_sec_insn(obj, sec_idx, insn_idx);
4073 if (!prog) {
6245947c 4074 pr_debug("sec '%s': relo #%d: couldn't find program in section '%s' for insn #%u, probably overridden weak function, skipping...\n",
c3c55696 4075 relo_sec_name, i, sec_name, insn_idx);
6245947c 4076 continue;
c3c55696
AN
4077 }
4078
4079 relos = libbpf_reallocarray(prog->reloc_desc,
4080 prog->nr_reloc + 1, sizeof(*relos));
4081 if (!relos)
4082 return -ENOMEM;
4083 prog->reloc_desc = relos;
4084
4085 /* adjust insn_idx to local BPF program frame of reference */
4086 insn_idx -= prog->sec_insn_off;
4087 err = bpf_program__record_reloc(prog, &relos[prog->nr_reloc],
ad23b723 4088 insn_idx, sym_name, sym, rel);
1f8e2bcb
AN
4089 if (err)
4090 return err;
c3c55696
AN
4091
4092 prog->nr_reloc++;
34090915
WN
4093 }
4094 return 0;
4095}
4096
abd29c93 4097static int bpf_map_find_btf_info(struct bpf_object *obj, struct bpf_map *map)
8a138aed
MKL
4098{
4099 struct bpf_map_def *def = &map->def;
d859900c 4100 __u32 key_type_id = 0, value_type_id = 0;
96408c43 4101 int ret;
8a138aed 4102
590a0088
MKL
4103 /* if it's BTF-defined map, we don't need to search for type IDs.
4104 * For struct_ops map, it does not need btf_key_type_id and
4105 * btf_value_type_id.
4106 */
4107 if (map->sec_idx == obj->efile.btf_maps_shndx ||
4108 bpf_map__is_struct_ops(map))
abd29c93
AN
4109 return 0;
4110
d859900c 4111 if (!bpf_map__is_internal(map)) {
abd29c93 4112 ret = btf__get_map_kv_tids(obj->btf, map->name, def->key_size,
d859900c
DB
4113 def->value_size, &key_type_id,
4114 &value_type_id);
4115 } else {
4116 /*
4117 * LLVM annotates global data differently in BTF, that is,
4118 * only as '.data', '.bss' or '.rodata'.
4119 */
aed65917 4120 ret = btf__find_by_name(obj->btf, map->real_name);
d859900c
DB
4121 }
4122 if (ret < 0)
96408c43 4123 return ret;
8a138aed 4124
96408c43 4125 map->btf_key_type_id = key_type_id;
d859900c
DB
4126 map->btf_value_type_id = bpf_map__is_internal(map) ?
4127 ret : value_type_id;
8a138aed
MKL
4128 return 0;
4129}
4130
97eb3138
MP
4131static int bpf_get_map_info_from_fdinfo(int fd, struct bpf_map_info *info)
4132{
4133 char file[PATH_MAX], buff[4096];
4134 FILE *fp;
4135 __u32 val;
4136 int err;
4137
4138 snprintf(file, sizeof(file), "/proc/%d/fdinfo/%d", getpid(), fd);
4139 memset(info, 0, sizeof(*info));
4140
4141 fp = fopen(file, "r");
4142 if (!fp) {
4143 err = -errno;
4144 pr_warn("failed to open %s: %d. No procfs support?\n", file,
4145 err);
4146 return err;
4147 }
4148
4149 while (fgets(buff, sizeof(buff), fp)) {
4150 if (sscanf(buff, "map_type:\t%u", &val) == 1)
4151 info->type = val;
4152 else if (sscanf(buff, "key_size:\t%u", &val) == 1)
4153 info->key_size = val;
4154 else if (sscanf(buff, "value_size:\t%u", &val) == 1)
4155 info->value_size = val;
4156 else if (sscanf(buff, "max_entries:\t%u", &val) == 1)
4157 info->max_entries = val;
4158 else if (sscanf(buff, "map_flags:\t%i", &val) == 1)
4159 info->map_flags = val;
4160 }
4161
4162 fclose(fp);
4163
4164 return 0;
4165}
4166
26736eb9
JK
4167int bpf_map__reuse_fd(struct bpf_map *map, int fd)
4168{
4169 struct bpf_map_info info = {};
4170 __u32 len = sizeof(info);
4171 int new_fd, err;
4172 char *new_name;
4173
4174 err = bpf_obj_get_info_by_fd(fd, &info, &len);
97eb3138
MP
4175 if (err && errno == EINVAL)
4176 err = bpf_get_map_info_from_fdinfo(fd, &info);
26736eb9 4177 if (err)
e9fc3ce9 4178 return libbpf_err(err);
26736eb9
JK
4179
4180 new_name = strdup(info.name);
4181 if (!new_name)
e9fc3ce9 4182 return libbpf_err(-errno);
26736eb9
JK
4183
4184 new_fd = open("/", O_RDONLY | O_CLOEXEC);
d1b4574a
THJ
4185 if (new_fd < 0) {
4186 err = -errno;
26736eb9 4187 goto err_free_new_name;
d1b4574a 4188 }
26736eb9
JK
4189
4190 new_fd = dup3(fd, new_fd, O_CLOEXEC);
d1b4574a
THJ
4191 if (new_fd < 0) {
4192 err = -errno;
26736eb9 4193 goto err_close_new_fd;
d1b4574a 4194 }
26736eb9
JK
4195
4196 err = zclose(map->fd);
d1b4574a
THJ
4197 if (err) {
4198 err = -errno;
26736eb9 4199 goto err_close_new_fd;
d1b4574a 4200 }
26736eb9
JK
4201 free(map->name);
4202
4203 map->fd = new_fd;
4204 map->name = new_name;
4205 map->def.type = info.type;
4206 map->def.key_size = info.key_size;
4207 map->def.value_size = info.value_size;
4208 map->def.max_entries = info.max_entries;
4209 map->def.map_flags = info.map_flags;
4210 map->btf_key_type_id = info.btf_key_type_id;
4211 map->btf_value_type_id = info.btf_value_type_id;
ec6d5f47 4212 map->reused = true;
26736eb9
JK
4213
4214 return 0;
4215
4216err_close_new_fd:
4217 close(new_fd);
4218err_free_new_name:
4219 free(new_name);
e9fc3ce9 4220 return libbpf_err(err);
26736eb9
JK
4221}
4222
1bdb6c9a 4223__u32 bpf_map__max_entries(const struct bpf_map *map)
1a11a4c7 4224{
1bdb6c9a
AN
4225 return map->def.max_entries;
4226}
1a11a4c7 4227
b3278099
AN
4228struct bpf_map *bpf_map__inner_map(struct bpf_map *map)
4229{
4230 if (!bpf_map_type__is_map_in_map(map->def.type))
e9fc3ce9 4231 return errno = EINVAL, NULL;
b3278099
AN
4232
4233 return map->inner_map;
4234}
4235
1bdb6c9a
AN
4236int bpf_map__set_max_entries(struct bpf_map *map, __u32 max_entries)
4237{
1a11a4c7 4238 if (map->fd >= 0)
e9fc3ce9 4239 return libbpf_err(-EBUSY);
1a11a4c7 4240 map->def.max_entries = max_entries;
1a11a4c7
AI
4241 return 0;
4242}
4243
1bdb6c9a
AN
4244int bpf_map__resize(struct bpf_map *map, __u32 max_entries)
4245{
4246 if (!map || !max_entries)
e9fc3ce9 4247 return libbpf_err(-EINVAL);
1bdb6c9a
AN
4248
4249 return bpf_map__set_max_entries(map, max_entries);
4250}
4251
47eff617 4252static int
fd9eef1a 4253bpf_object__probe_loading(struct bpf_object *obj)
47eff617
SF
4254{
4255 struct bpf_load_program_attr attr;
4256 char *cp, errmsg[STRERR_BUFSIZE];
4257 struct bpf_insn insns[] = {
4258 BPF_MOV64_IMM(BPF_REG_0, 0),
4259 BPF_EXIT_INSN(),
4260 };
4261 int ret;
4262
f9bceaa5
SF
4263 if (obj->gen_loader)
4264 return 0;
4265
47eff617
SF
4266 /* make sure basic loading works */
4267
4268 memset(&attr, 0, sizeof(attr));
4269 attr.prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
4270 attr.insns = insns;
4271 attr.insns_cnt = ARRAY_SIZE(insns);
4272 attr.license = "GPL";
4273
4274 ret = bpf_load_program_xattr(&attr, NULL, 0);
5c10a3db
JE
4275 if (ret < 0) {
4276 attr.prog_type = BPF_PROG_TYPE_TRACEPOINT;
4277 ret = bpf_load_program_xattr(&attr, NULL, 0);
4278 }
47eff617 4279 if (ret < 0) {
fd9eef1a
EC
4280 ret = errno;
4281 cp = libbpf_strerror_r(ret, errmsg, sizeof(errmsg));
4282 pr_warn("Error in %s():%s(%d). Couldn't load trivial BPF "
4283 "program. Make sure your kernel supports BPF "
4284 "(CONFIG_BPF_SYSCALL=y) and/or that RLIMIT_MEMLOCK is "
4285 "set to big enough value.\n", __func__, cp, ret);
4286 return -ret;
47eff617
SF
4287 }
4288 close(ret);
4289
fd9eef1a
EC
4290 return 0;
4291}
4292
bb180fb2
AN
4293static int probe_fd(int fd)
4294{
4295 if (fd >= 0)
4296 close(fd);
4297 return fd >= 0;
4298}
4299
47b6cb4d 4300static int probe_kern_prog_name(void)
fd9eef1a
EC
4301{
4302 struct bpf_load_program_attr attr;
4303 struct bpf_insn insns[] = {
4304 BPF_MOV64_IMM(BPF_REG_0, 0),
4305 BPF_EXIT_INSN(),
4306 };
4307 int ret;
4308
4309 /* make sure loading with name works */
47eff617 4310
fd9eef1a
EC
4311 memset(&attr, 0, sizeof(attr));
4312 attr.prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
4313 attr.insns = insns;
4314 attr.insns_cnt = ARRAY_SIZE(insns);
4315 attr.license = "GPL";
47eff617
SF
4316 attr.name = "test";
4317 ret = bpf_load_program_xattr(&attr, NULL, 0);
bb180fb2 4318 return probe_fd(ret);
47eff617
SF
4319}
4320
47b6cb4d 4321static int probe_kern_global_data(void)
8837fe5d
DB
4322{
4323 struct bpf_load_program_attr prg_attr;
4324 struct bpf_create_map_attr map_attr;
4325 char *cp, errmsg[STRERR_BUFSIZE];
4326 struct bpf_insn insns[] = {
4327 BPF_LD_MAP_VALUE(BPF_REG_1, 0, 16),
4328 BPF_ST_MEM(BPF_DW, BPF_REG_1, 0, 42),
4329 BPF_MOV64_IMM(BPF_REG_0, 0),
4330 BPF_EXIT_INSN(),
4331 };
4332 int ret, map;
4333
4334 memset(&map_attr, 0, sizeof(map_attr));
4335 map_attr.map_type = BPF_MAP_TYPE_ARRAY;
4336 map_attr.key_size = sizeof(int);
4337 map_attr.value_size = 32;
4338 map_attr.max_entries = 1;
4339
4340 map = bpf_create_map_xattr(&map_attr);
4341 if (map < 0) {
23ab656b
THJ
4342 ret = -errno;
4343 cp = libbpf_strerror_r(ret, errmsg, sizeof(errmsg));
be18010e 4344 pr_warn("Error in %s():%s(%d). Couldn't create simple array map.\n",
23ab656b
THJ
4345 __func__, cp, -ret);
4346 return ret;
8837fe5d
DB
4347 }
4348
4349 insns[0].imm = map;
4350
4351 memset(&prg_attr, 0, sizeof(prg_attr));
4352 prg_attr.prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
4353 prg_attr.insns = insns;
4354 prg_attr.insns_cnt = ARRAY_SIZE(insns);
4355 prg_attr.license = "GPL";
4356
4357 ret = bpf_load_program_xattr(&prg_attr, NULL, 0);
47b6cb4d 4358 close(map);
bb180fb2 4359 return probe_fd(ret);
8837fe5d
DB
4360}
4361
68b08647
AN
4362static int probe_kern_btf(void)
4363{
4364 static const char strs[] = "\0int";
4365 __u32 types[] = {
4366 /* int */
4367 BTF_TYPE_INT_ENC(1, BTF_INT_SIGNED, 0, 32, 4),
4368 };
4369
4370 return probe_fd(libbpf__load_raw_btf((char *)types, sizeof(types),
4371 strs, sizeof(strs)));
4372}
4373
47b6cb4d 4374static int probe_kern_btf_func(void)
d7c4b398 4375{
8983b731 4376 static const char strs[] = "\0int\0x\0a";
d7c4b398
AN
4377 /* void x(int a) {} */
4378 __u32 types[] = {
4379 /* int */
4380 BTF_TYPE_INT_ENC(1, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
4381 /* FUNC_PROTO */ /* [2] */
4382 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_FUNC_PROTO, 0, 1), 0),
4383 BTF_PARAM_ENC(7, 1),
4384 /* FUNC x */ /* [3] */
4385 BTF_TYPE_ENC(5, BTF_INFO_ENC(BTF_KIND_FUNC, 0, 0), 2),
4386 };
d7c4b398 4387
bb180fb2
AN
4388 return probe_fd(libbpf__load_raw_btf((char *)types, sizeof(types),
4389 strs, sizeof(strs)));
d7c4b398
AN
4390}
4391
47b6cb4d 4392static int probe_kern_btf_func_global(void)
2d3eb67f
AS
4393{
4394 static const char strs[] = "\0int\0x\0a";
4395 /* static void x(int a) {} */
4396 __u32 types[] = {
4397 /* int */
4398 BTF_TYPE_INT_ENC(1, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
4399 /* FUNC_PROTO */ /* [2] */
4400 BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_FUNC_PROTO, 0, 1), 0),
4401 BTF_PARAM_ENC(7, 1),
4402 /* FUNC x BTF_FUNC_GLOBAL */ /* [3] */
4403 BTF_TYPE_ENC(5, BTF_INFO_ENC(BTF_KIND_FUNC, 0, BTF_FUNC_GLOBAL), 2),
4404 };
2d3eb67f 4405
bb180fb2
AN
4406 return probe_fd(libbpf__load_raw_btf((char *)types, sizeof(types),
4407 strs, sizeof(strs)));
2d3eb67f
AS
4408}
4409
47b6cb4d 4410static int probe_kern_btf_datasec(void)
d7c4b398 4411{
8983b731 4412 static const char strs[] = "\0x\0.data";
d7c4b398
AN
4413 /* static int a; */
4414 __u32 types[] = {
4415 /* int */
4416 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
4417 /* VAR x */ /* [2] */
4418 BTF_TYPE_ENC(1, BTF_INFO_ENC(BTF_KIND_VAR, 0, 0), 1),
4419 BTF_VAR_STATIC,
4420 /* DATASEC val */ /* [3] */
4421 BTF_TYPE_ENC(3, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
4422 BTF_VAR_SECINFO_ENC(2, 0, 4),
4423 };
cfd49210 4424
bb180fb2
AN
4425 return probe_fd(libbpf__load_raw_btf((char *)types, sizeof(types),
4426 strs, sizeof(strs)));
d7c4b398
AN
4427}
4428
22541a9e
IL
4429static int probe_kern_btf_float(void)
4430{
4431 static const char strs[] = "\0float";
4432 __u32 types[] = {
4433 /* float */
4434 BTF_TYPE_FLOAT_ENC(1, 4),
4435 };
4436
4437 return probe_fd(libbpf__load_raw_btf((char *)types, sizeof(types),
4438 strs, sizeof(strs)));
4439}
4440
223f903e 4441static int probe_kern_btf_decl_tag(void)
5b84bd10
YS
4442{
4443 static const char strs[] = "\0tag";
4444 __u32 types[] = {
4445 /* int */
4446 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
4447 /* VAR x */ /* [2] */
4448 BTF_TYPE_ENC(1, BTF_INFO_ENC(BTF_KIND_VAR, 0, 0), 1),
4449 BTF_VAR_STATIC,
4450 /* attr */
223f903e 4451 BTF_TYPE_DECL_TAG_ENC(1, 2, -1),
5b84bd10
YS
4452 };
4453
4454 return probe_fd(libbpf__load_raw_btf((char *)types, sizeof(types),
4455 strs, sizeof(strs)));
4456}
4457
47b6cb4d 4458static int probe_kern_array_mmap(void)
7fe74b43
AN
4459{
4460 struct bpf_create_map_attr attr = {
4461 .map_type = BPF_MAP_TYPE_ARRAY,
4462 .map_flags = BPF_F_MMAPABLE,
4463 .key_size = sizeof(int),
4464 .value_size = sizeof(int),
4465 .max_entries = 1,
4466 };
7fe74b43 4467
bb180fb2 4468 return probe_fd(bpf_create_map_xattr(&attr));
7fe74b43
AN
4469}
4470
47b6cb4d 4471static int probe_kern_exp_attach_type(void)
25498a19
AN
4472{
4473 struct bpf_load_program_attr attr;
4474 struct bpf_insn insns[] = {
4475 BPF_MOV64_IMM(BPF_REG_0, 0),
4476 BPF_EXIT_INSN(),
4477 };
25498a19
AN
4478
4479 memset(&attr, 0, sizeof(attr));
4480 /* use any valid combination of program type and (optional)
4481 * non-zero expected attach type (i.e., not a BPF_CGROUP_INET_INGRESS)
4482 * to see if kernel supports expected_attach_type field for
4483 * BPF_PROG_LOAD command
4484 */
4485 attr.prog_type = BPF_PROG_TYPE_CGROUP_SOCK;
4486 attr.expected_attach_type = BPF_CGROUP_INET_SOCK_CREATE;
4487 attr.insns = insns;
4488 attr.insns_cnt = ARRAY_SIZE(insns);
4489 attr.license = "GPL";
4490
bb180fb2 4491 return probe_fd(bpf_load_program_xattr(&attr, NULL, 0));
25498a19
AN
4492}
4493
109cea5a
AN
4494static int probe_kern_probe_read_kernel(void)
4495{
4496 struct bpf_load_program_attr attr;
4497 struct bpf_insn insns[] = {
4498 BPF_MOV64_REG(BPF_REG_1, BPF_REG_10), /* r1 = r10 (fp) */
4499 BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -8), /* r1 += -8 */
4500 BPF_MOV64_IMM(BPF_REG_2, 8), /* r2 = 8 */
4501 BPF_MOV64_IMM(BPF_REG_3, 0), /* r3 = 0 */
4502 BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_probe_read_kernel),
4503 BPF_EXIT_INSN(),
4504 };
4505
4506 memset(&attr, 0, sizeof(attr));
4507 attr.prog_type = BPF_PROG_TYPE_KPROBE;
4508 attr.insns = insns;
4509 attr.insns_cnt = ARRAY_SIZE(insns);
4510 attr.license = "GPL";
4511
4512 return probe_fd(bpf_load_program_xattr(&attr, NULL, 0));
4513}
4514
5d23328d
YZ
4515static int probe_prog_bind_map(void)
4516{
4517 struct bpf_load_program_attr prg_attr;
4518 struct bpf_create_map_attr map_attr;
4519 char *cp, errmsg[STRERR_BUFSIZE];
4520 struct bpf_insn insns[] = {
4521 BPF_MOV64_IMM(BPF_REG_0, 0),
4522 BPF_EXIT_INSN(),
4523 };
4524 int ret, map, prog;
4525
4526 memset(&map_attr, 0, sizeof(map_attr));
4527 map_attr.map_type = BPF_MAP_TYPE_ARRAY;
4528 map_attr.key_size = sizeof(int);
4529 map_attr.value_size = 32;
4530 map_attr.max_entries = 1;
4531
4532 map = bpf_create_map_xattr(&map_attr);
4533 if (map < 0) {
4534 ret = -errno;
4535 cp = libbpf_strerror_r(ret, errmsg, sizeof(errmsg));
4536 pr_warn("Error in %s():%s(%d). Couldn't create simple array map.\n",
4537 __func__, cp, -ret);
4538 return ret;
4539 }
4540
4541 memset(&prg_attr, 0, sizeof(prg_attr));
4542 prg_attr.prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
4543 prg_attr.insns = insns;
4544 prg_attr.insns_cnt = ARRAY_SIZE(insns);
4545 prg_attr.license = "GPL";
4546
4547 prog = bpf_load_program_xattr(&prg_attr, NULL, 0);
4548 if (prog < 0) {
4549 close(map);
4550 return 0;
4551 }
4552
4553 ret = bpf_prog_bind_map(prog, map, NULL);
4554
4555 close(map);
4556 close(prog);
4557
4558 return ret >= 0;
4559}
4560
4f33a53d
AN
4561static int probe_module_btf(void)
4562{
4563 static const char strs[] = "\0int";
4564 __u32 types[] = {
4565 /* int */
4566 BTF_TYPE_INT_ENC(1, BTF_INT_SIGNED, 0, 32, 4),
4567 };
4568 struct bpf_btf_info info;
4569 __u32 len = sizeof(info);
4570 char name[16];
4571 int fd, err;
4572
4573 fd = libbpf__load_raw_btf((char *)types, sizeof(types), strs, sizeof(strs));
4574 if (fd < 0)
4575 return 0; /* BTF not supported at all */
4576
4577 memset(&info, 0, sizeof(info));
4578 info.name = ptr_to_u64(name);
4579 info.name_len = sizeof(name);
4580
4581 /* check that BPF_OBJ_GET_INFO_BY_FD supports specifying name pointer;
4582 * kernel's module BTF support coincides with support for
4583 * name/name_len fields in struct bpf_btf_info.
4584 */
4585 err = bpf_obj_get_info_by_fd(fd, &info, &len);
4586 close(fd);
4587 return !err;
4588}
4589
668ace0e
AN
4590static int probe_perf_link(void)
4591{
4592 struct bpf_load_program_attr attr;
4593 struct bpf_insn insns[] = {
4594 BPF_MOV64_IMM(BPF_REG_0, 0),
4595 BPF_EXIT_INSN(),
4596 };
4597 int prog_fd, link_fd, err;
4598
4599 memset(&attr, 0, sizeof(attr));
4600 attr.prog_type = BPF_PROG_TYPE_TRACEPOINT;
4601 attr.insns = insns;
4602 attr.insns_cnt = ARRAY_SIZE(insns);
4603 attr.license = "GPL";
4604 prog_fd = bpf_load_program_xattr(&attr, NULL, 0);
4605 if (prog_fd < 0)
4606 return -errno;
4607
4608 /* use invalid perf_event FD to get EBADF, if link is supported;
4609 * otherwise EINVAL should be returned
4610 */
4611 link_fd = bpf_link_create(prog_fd, -1, BPF_PERF_EVENT, NULL);
4612 err = -errno; /* close() can clobber errno */
4613
4614 if (link_fd >= 0)
4615 close(link_fd);
4616 close(prog_fd);
4617
4618 return link_fd < 0 && err == -EBADF;
4619}
4620
47b6cb4d
AN
4621enum kern_feature_result {
4622 FEAT_UNKNOWN = 0,
4623 FEAT_SUPPORTED = 1,
4624 FEAT_MISSING = 2,
4625};
4626
4627typedef int (*feature_probe_fn)(void);
4628
4629static struct kern_feature_desc {
4630 const char *desc;
4631 feature_probe_fn probe;
4632 enum kern_feature_result res;
4633} feature_probes[__FEAT_CNT] = {
4634 [FEAT_PROG_NAME] = {
4635 "BPF program name", probe_kern_prog_name,
4636 },
4637 [FEAT_GLOBAL_DATA] = {
4638 "global variables", probe_kern_global_data,
4639 },
68b08647
AN
4640 [FEAT_BTF] = {
4641 "minimal BTF", probe_kern_btf,
4642 },
47b6cb4d
AN
4643 [FEAT_BTF_FUNC] = {
4644 "BTF functions", probe_kern_btf_func,
4645 },
4646 [FEAT_BTF_GLOBAL_FUNC] = {
4647 "BTF global function", probe_kern_btf_func_global,
4648 },
4649 [FEAT_BTF_DATASEC] = {
4650 "BTF data section and variable", probe_kern_btf_datasec,
4651 },
4652 [FEAT_ARRAY_MMAP] = {
4653 "ARRAY map mmap()", probe_kern_array_mmap,
4654 },
4655 [FEAT_EXP_ATTACH_TYPE] = {
4656 "BPF_PROG_LOAD expected_attach_type attribute",
4657 probe_kern_exp_attach_type,
4658 },
109cea5a
AN
4659 [FEAT_PROBE_READ_KERN] = {
4660 "bpf_probe_read_kernel() helper", probe_kern_probe_read_kernel,
5d23328d
YZ
4661 },
4662 [FEAT_PROG_BIND_MAP] = {
4663 "BPF_PROG_BIND_MAP support", probe_prog_bind_map,
4f33a53d
AN
4664 },
4665 [FEAT_MODULE_BTF] = {
4666 "module BTF support", probe_module_btf,
4667 },
22541a9e
IL
4668 [FEAT_BTF_FLOAT] = {
4669 "BTF_KIND_FLOAT support", probe_kern_btf_float,
4670 },
668ace0e
AN
4671 [FEAT_PERF_LINK] = {
4672 "BPF perf link support", probe_perf_link,
4673 },
223f903e
YS
4674 [FEAT_BTF_DECL_TAG] = {
4675 "BTF_KIND_DECL_TAG support", probe_kern_btf_decl_tag,
5b84bd10 4676 },
47b6cb4d 4677};
8837fe5d 4678
9ca1f56a 4679static bool kernel_supports(const struct bpf_object *obj, enum kern_feature_id feat_id)
47b6cb4d
AN
4680{
4681 struct kern_feature_desc *feat = &feature_probes[feat_id];
4682 int ret;
4683
67234743
AS
4684 if (obj->gen_loader)
4685 /* To generate loader program assume the latest kernel
4686 * to avoid doing extra prog_load, map_create syscalls.
4687 */
4688 return true;
4689
47b6cb4d
AN
4690 if (READ_ONCE(feat->res) == FEAT_UNKNOWN) {
4691 ret = feat->probe();
4692 if (ret > 0) {
4693 WRITE_ONCE(feat->res, FEAT_SUPPORTED);
4694 } else if (ret == 0) {
4695 WRITE_ONCE(feat->res, FEAT_MISSING);
4696 } else {
4697 pr_warn("Detection of kernel %s support failed: %d\n", feat->desc, ret);
4698 WRITE_ONCE(feat->res, FEAT_MISSING);
4699 }
8837fe5d
DB
4700 }
4701
47b6cb4d 4702 return READ_ONCE(feat->res) == FEAT_SUPPORTED;
47eff617
SF
4703}
4704
57a00f41
THJ
4705static bool map_is_reuse_compat(const struct bpf_map *map, int map_fd)
4706{
4707 struct bpf_map_info map_info = {};
4708 char msg[STRERR_BUFSIZE];
4709 __u32 map_info_len;
97eb3138 4710 int err;
57a00f41
THJ
4711
4712 map_info_len = sizeof(map_info);
4713
97eb3138
MP
4714 err = bpf_obj_get_info_by_fd(map_fd, &map_info, &map_info_len);
4715 if (err && errno == EINVAL)
4716 err = bpf_get_map_info_from_fdinfo(map_fd, &map_info);
4717 if (err) {
4718 pr_warn("failed to get map info for map FD %d: %s\n", map_fd,
4719 libbpf_strerror_r(errno, msg, sizeof(msg)));
57a00f41
THJ
4720 return false;
4721 }
4722
4723 return (map_info.type == map->def.type &&
4724 map_info.key_size == map->def.key_size &&
4725 map_info.value_size == map->def.value_size &&
4726 map_info.max_entries == map->def.max_entries &&
4727 map_info.map_flags == map->def.map_flags);
4728}
4729
4730static int
4731bpf_object__reuse_map(struct bpf_map *map)
4732{
4733 char *cp, errmsg[STRERR_BUFSIZE];
4734 int err, pin_fd;
4735
4736 pin_fd = bpf_obj_get(map->pin_path);
4737 if (pin_fd < 0) {
4738 err = -errno;
4739 if (err == -ENOENT) {
4740 pr_debug("found no pinned map to reuse at '%s'\n",
4741 map->pin_path);
4742 return 0;
4743 }
4744
4745 cp = libbpf_strerror_r(-err, errmsg, sizeof(errmsg));
4746 pr_warn("couldn't retrieve pinned map '%s': %s\n",
4747 map->pin_path, cp);
4748 return err;
4749 }
4750
4751 if (!map_is_reuse_compat(map, pin_fd)) {
4752 pr_warn("couldn't reuse pinned map at '%s': parameter mismatch\n",
4753 map->pin_path);
4754 close(pin_fd);
4755 return -EINVAL;
4756 }
4757
4758 err = bpf_map__reuse_fd(map, pin_fd);
4759 if (err) {
4760 close(pin_fd);
4761 return err;
4762 }
4763 map->pinned = true;
4764 pr_debug("reused pinned map at '%s'\n", map->pin_path);
4765
4766 return 0;
4767}
4768
d859900c
DB
4769static int
4770bpf_object__populate_internal_map(struct bpf_object *obj, struct bpf_map *map)
4771{
166750bc 4772 enum libbpf_map_type map_type = map->libbpf_type;
d859900c
DB
4773 char *cp, errmsg[STRERR_BUFSIZE];
4774 int err, zero = 0;
d859900c 4775
67234743
AS
4776 if (obj->gen_loader) {
4777 bpf_gen__map_update_elem(obj->gen_loader, map - obj->maps,
4778 map->mmaped, map->def.value_size);
4779 if (map_type == LIBBPF_MAP_RODATA || map_type == LIBBPF_MAP_KCONFIG)
4780 bpf_gen__map_freeze(obj->gen_loader, map - obj->maps);
4781 return 0;
4782 }
eba9c5f4
AN
4783 err = bpf_map_update_elem(map->fd, &zero, map->mmaped, 0);
4784 if (err) {
4785 err = -errno;
4786 cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg));
4787 pr_warn("Error setting initial map(%s) contents: %s\n",
4788 map->name, cp);
4789 return err;
4790 }
d859900c 4791
81bfdd08
AN
4792 /* Freeze .rodata and .kconfig map as read-only from syscall side. */
4793 if (map_type == LIBBPF_MAP_RODATA || map_type == LIBBPF_MAP_KCONFIG) {
d859900c
DB
4794 err = bpf_map_freeze(map->fd);
4795 if (err) {
eba9c5f4
AN
4796 err = -errno;
4797 cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg));
be18010e
KW
4798 pr_warn("Error freezing map(%s) as read-only: %s\n",
4799 map->name, cp);
eba9c5f4 4800 return err;
d859900c
DB
4801 }
4802 }
eba9c5f4 4803 return 0;
d859900c
DB
4804}
4805
2d39d7c5
AN
4806static void bpf_map__destroy(struct bpf_map *map);
4807
67234743 4808static int bpf_object__create_map(struct bpf_object *obj, struct bpf_map *map, bool is_inner)
2d39d7c5
AN
4809{
4810 struct bpf_create_map_attr create_attr;
4811 struct bpf_map_def *def = &map->def;
a21ab4c5 4812 int err = 0;
2d39d7c5
AN
4813
4814 memset(&create_attr, 0, sizeof(create_attr));
4815
9ca1f56a 4816 if (kernel_supports(obj, FEAT_PROG_NAME))
2d39d7c5
AN
4817 create_attr.name = map->name;
4818 create_attr.map_ifindex = map->map_ifindex;
4819 create_attr.map_type = def->type;
4820 create_attr.map_flags = def->map_flags;
4821 create_attr.key_size = def->key_size;
4822 create_attr.value_size = def->value_size;
1bdb6c9a 4823 create_attr.numa_node = map->numa_node;
2d39d7c5
AN
4824
4825 if (def->type == BPF_MAP_TYPE_PERF_EVENT_ARRAY && !def->max_entries) {
4826 int nr_cpus;
4827
4828 nr_cpus = libbpf_num_possible_cpus();
4829 if (nr_cpus < 0) {
4830 pr_warn("map '%s': failed to determine number of system CPUs: %d\n",
4831 map->name, nr_cpus);
4832 return nr_cpus;
4833 }
4834 pr_debug("map '%s': setting size to %d\n", map->name, nr_cpus);
4835 create_attr.max_entries = nr_cpus;
4836 } else {
4837 create_attr.max_entries = def->max_entries;
4838 }
4839
4840 if (bpf_map__is_struct_ops(map))
4841 create_attr.btf_vmlinux_value_type_id =
4842 map->btf_vmlinux_value_type_id;
4843
4844 create_attr.btf_fd = 0;
4845 create_attr.btf_key_type_id = 0;
4846 create_attr.btf_value_type_id = 0;
0f0e55d8 4847 if (obj->btf && btf__fd(obj->btf) >= 0 && !bpf_map_find_btf_info(obj, map)) {
2d39d7c5
AN
4848 create_attr.btf_fd = btf__fd(obj->btf);
4849 create_attr.btf_key_type_id = map->btf_key_type_id;
4850 create_attr.btf_value_type_id = map->btf_value_type_id;
4851 }
4852
646f02ff
AN
4853 if (bpf_map_type__is_map_in_map(def->type)) {
4854 if (map->inner_map) {
67234743 4855 err = bpf_object__create_map(obj, map->inner_map, true);
646f02ff
AN
4856 if (err) {
4857 pr_warn("map '%s': failed to create inner map: %d\n",
4858 map->name, err);
4859 return err;
4860 }
4861 map->inner_map_fd = bpf_map__fd(map->inner_map);
4862 }
4863 if (map->inner_map_fd >= 0)
4864 create_attr.inner_map_fd = map->inner_map_fd;
4865 }
4866
f7310523
HC
4867 switch (def->type) {
4868 case BPF_MAP_TYPE_PERF_EVENT_ARRAY:
4869 case BPF_MAP_TYPE_CGROUP_ARRAY:
4870 case BPF_MAP_TYPE_STACK_TRACE:
4871 case BPF_MAP_TYPE_ARRAY_OF_MAPS:
4872 case BPF_MAP_TYPE_HASH_OF_MAPS:
4873 case BPF_MAP_TYPE_DEVMAP:
4874 case BPF_MAP_TYPE_DEVMAP_HASH:
4875 case BPF_MAP_TYPE_CPUMAP:
4876 case BPF_MAP_TYPE_XSKMAP:
4877 case BPF_MAP_TYPE_SOCKMAP:
4878 case BPF_MAP_TYPE_SOCKHASH:
4879 case BPF_MAP_TYPE_QUEUE:
4880 case BPF_MAP_TYPE_STACK:
4881 case BPF_MAP_TYPE_RINGBUF:
4882 create_attr.btf_fd = 0;
4883 create_attr.btf_key_type_id = 0;
4884 create_attr.btf_value_type_id = 0;
4885 map->btf_key_type_id = 0;
4886 map->btf_value_type_id = 0;
4887 default:
4888 break;
4889 }
4890
67234743
AS
4891 if (obj->gen_loader) {
4892 bpf_gen__map_create(obj->gen_loader, &create_attr, is_inner ? -1 : map - obj->maps);
4893 /* Pretend to have valid FD to pass various fd >= 0 checks.
4894 * This fd == 0 will not be used with any syscall and will be reset to -1 eventually.
4895 */
4896 map->fd = 0;
4897 } else {
4898 map->fd = bpf_create_map_xattr(&create_attr);
4899 }
2d39d7c5
AN
4900 if (map->fd < 0 && (create_attr.btf_key_type_id ||
4901 create_attr.btf_value_type_id)) {
4902 char *cp, errmsg[STRERR_BUFSIZE];
2d39d7c5 4903
a21ab4c5 4904 err = -errno;
2d39d7c5
AN
4905 cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg));
4906 pr_warn("Error in bpf_create_map_xattr(%s):%s(%d). Retrying without BTF.\n",
4907 map->name, cp, err);
4908 create_attr.btf_fd = 0;
4909 create_attr.btf_key_type_id = 0;
4910 create_attr.btf_value_type_id = 0;
4911 map->btf_key_type_id = 0;
4912 map->btf_value_type_id = 0;
4913 map->fd = bpf_create_map_xattr(&create_attr);
4914 }
4915
a21ab4c5 4916 err = map->fd < 0 ? -errno : 0;
2d39d7c5 4917
646f02ff 4918 if (bpf_map_type__is_map_in_map(def->type) && map->inner_map) {
67234743
AS
4919 if (obj->gen_loader)
4920 map->inner_map->fd = -1;
646f02ff
AN
4921 bpf_map__destroy(map->inner_map);
4922 zfree(&map->inner_map);
4923 }
4924
a21ab4c5 4925 return err;
2d39d7c5
AN
4926}
4927
67234743 4928static int init_map_slots(struct bpf_object *obj, struct bpf_map *map)
a0f2b7ac
HL
4929{
4930 const struct bpf_map *targ_map;
4931 unsigned int i;
67234743 4932 int fd, err = 0;
a0f2b7ac
HL
4933
4934 for (i = 0; i < map->init_slots_sz; i++) {
4935 if (!map->init_slots[i])
4936 continue;
4937
4938 targ_map = map->init_slots[i];
4939 fd = bpf_map__fd(targ_map);
67234743 4940 if (obj->gen_loader) {
edc0571c 4941 pr_warn("// TODO map_update_elem: idx %td key %d value==map_idx %td\n",
67234743
AS
4942 map - obj->maps, i, targ_map - obj->maps);
4943 return -ENOTSUP;
4944 } else {
4945 err = bpf_map_update_elem(map->fd, &i, &fd, 0);
4946 }
a0f2b7ac
HL
4947 if (err) {
4948 err = -errno;
4949 pr_warn("map '%s': failed to initialize slot [%d] to map '%s' fd=%d: %d\n",
4950 map->name, i, targ_map->name,
4951 fd, err);
4952 return err;
4953 }
4954 pr_debug("map '%s': slot [%d] set to map '%s' fd=%d\n",
4955 map->name, i, targ_map->name, fd);
4956 }
4957
4958 zfree(&map->init_slots);
4959 map->init_slots_sz = 0;
4960
4961 return 0;
4962}
4963
52d3352e
WN
4964static int
4965bpf_object__create_maps(struct bpf_object *obj)
4966{
2d39d7c5
AN
4967 struct bpf_map *map;
4968 char *cp, errmsg[STRERR_BUFSIZE];
4969 unsigned int i, j;
8a138aed 4970 int err;
043c5bb3 4971 bool retried;
52d3352e 4972
9d759a9b 4973 for (i = 0; i < obj->nr_maps; i++) {
2d39d7c5 4974 map = &obj->maps[i];
8a138aed 4975
043c5bb3
MP
4976 retried = false;
4977retry:
57a00f41
THJ
4978 if (map->pin_path) {
4979 err = bpf_object__reuse_map(map);
4980 if (err) {
2d39d7c5 4981 pr_warn("map '%s': error reusing pinned map\n",
57a00f41 4982 map->name);
2d39d7c5 4983 goto err_out;
57a00f41 4984 }
043c5bb3
MP
4985 if (retried && map->fd < 0) {
4986 pr_warn("map '%s': cannot find pinned map\n",
4987 map->name);
4988 err = -ENOENT;
4989 goto err_out;
4990 }
57a00f41
THJ
4991 }
4992
26736eb9 4993 if (map->fd >= 0) {
2d39d7c5 4994 pr_debug("map '%s': skipping creation (preset fd=%d)\n",
26736eb9 4995 map->name, map->fd);
2c193d32 4996 } else {
67234743 4997 err = bpf_object__create_map(obj, map, false);
2c193d32 4998 if (err)
d859900c 4999 goto err_out;
d859900c 5000
2c193d32
HL
5001 pr_debug("map '%s': created successfully, fd=%d\n",
5002 map->name, map->fd);
646f02ff 5003
2c193d32
HL
5004 if (bpf_map__is_internal(map)) {
5005 err = bpf_object__populate_internal_map(obj, map);
5006 if (err < 0) {
5007 zclose(map->fd);
5008 goto err_out;
5009 }
d859900c 5010 }
646f02ff 5011
2c193d32 5012 if (map->init_slots_sz) {
67234743 5013 err = init_map_slots(obj, map);
2c193d32
HL
5014 if (err < 0) {
5015 zclose(map->fd);
646f02ff
AN
5016 goto err_out;
5017 }
646f02ff 5018 }
646f02ff
AN
5019 }
5020
57a00f41
THJ
5021 if (map->pin_path && !map->pinned) {
5022 err = bpf_map__pin(map, NULL);
5023 if (err) {
043c5bb3
MP
5024 zclose(map->fd);
5025 if (!retried && err == -EEXIST) {
5026 retried = true;
5027 goto retry;
5028 }
2d39d7c5
AN
5029 pr_warn("map '%s': failed to auto-pin at '%s': %d\n",
5030 map->name, map->pin_path, err);
2d39d7c5 5031 goto err_out;
57a00f41
THJ
5032 }
5033 }
52d3352e
WN
5034 }
5035
52d3352e 5036 return 0;
2d39d7c5
AN
5037
5038err_out:
5039 cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg));
5040 pr_warn("map '%s': failed to create: %s(%d)\n", map->name, cp, err);
5041 pr_perm_msg(err);
5042 for (j = 0; j < i; j++)
5043 zclose(obj->maps[j].fd);
5044 return err;
52d3352e
WN
5045}
5046
ddc7c304
AN
5047static bool bpf_core_is_flavor_sep(const char *s)
5048{
5049 /* check X___Y name pattern, where X and Y are not underscores */
5050 return s[0] != '_' && /* X */
5051 s[1] == '_' && s[2] == '_' && s[3] == '_' && /* ___ */
5052 s[4] != '_'; /* Y */
5053}
5054
5055/* Given 'some_struct_name___with_flavor' return the length of a name prefix
5056 * before last triple underscore. Struct name part after last triple
5057 * underscore is ignored by BPF CO-RE relocation during relocation matching.
5058 */
b0588390 5059size_t bpf_core_essential_name_len(const char *name)
ddc7c304
AN
5060{
5061 size_t n = strlen(name);
5062 int i;
5063
5064 for (i = n - 5; i >= 0; i--) {
5065 if (bpf_core_is_flavor_sep(name + i))
5066 return i + 1;
5067 }
5068 return n;
5069}
5070
301ba4d7 5071static void bpf_core_free_cands(struct bpf_core_cand_list *cands)
ddc7c304 5072{
0f7515ca
AN
5073 free(cands->cands);
5074 free(cands);
ddc7c304
AN
5075}
5076
301ba4d7 5077static int bpf_core_add_cands(struct bpf_core_cand *local_cand,
0f7515ca
AN
5078 size_t local_essent_len,
5079 const struct btf *targ_btf,
5080 const char *targ_btf_name,
5081 int targ_start_id,
301ba4d7 5082 struct bpf_core_cand_list *cands)
ddc7c304 5083{
301ba4d7 5084 struct bpf_core_cand *new_cands, *cand;
0f7515ca
AN
5085 const struct btf_type *t;
5086 const char *targ_name;
5087 size_t targ_essent_len;
5088 int n, i;
ddc7c304 5089
6a886de0
HC
5090 n = btf__type_cnt(targ_btf);
5091 for (i = targ_start_id; i < n; i++) {
ddc7c304 5092 t = btf__type_by_id(targ_btf, i);
0f7515ca 5093 if (btf_kind(t) != btf_kind(local_cand->t))
ddc7c304
AN
5094 continue;
5095
3fc32f40
AN
5096 targ_name = btf__name_by_offset(targ_btf, t->name_off);
5097 if (str_is_empty(targ_name))
d121e1d3
AN
5098 continue;
5099
ddc7c304
AN
5100 targ_essent_len = bpf_core_essential_name_len(targ_name);
5101 if (targ_essent_len != local_essent_len)
5102 continue;
5103
0f7515ca
AN
5104 if (strncmp(local_cand->name, targ_name, local_essent_len) != 0)
5105 continue;
5106
5107 pr_debug("CO-RE relocating [%d] %s %s: found target candidate [%d] %s %s in [%s]\n",
5108 local_cand->id, btf_kind_str(local_cand->t),
5109 local_cand->name, i, btf_kind_str(t), targ_name,
5110 targ_btf_name);
5111 new_cands = libbpf_reallocarray(cands->cands, cands->len + 1,
5112 sizeof(*cands->cands));
5113 if (!new_cands)
5114 return -ENOMEM;
5115
5116 cand = &new_cands[cands->len];
5117 cand->btf = targ_btf;
5118 cand->t = t;
5119 cand->name = targ_name;
5120 cand->id = i;
5121
5122 cands->cands = new_cands;
5123 cands->len++;
ddc7c304 5124 }
0f7515ca
AN
5125 return 0;
5126}
5127
4f33a53d
AN
5128static int load_module_btfs(struct bpf_object *obj)
5129{
5130 struct bpf_btf_info info;
5131 struct module_btf *mod_btf;
5132 struct btf *btf;
5133 char name[64];
5134 __u32 id = 0, len;
5135 int err, fd;
5136
5137 if (obj->btf_modules_loaded)
5138 return 0;
5139
67234743
AS
5140 if (obj->gen_loader)
5141 return 0;
5142
4f33a53d
AN
5143 /* don't do this again, even if we find no module BTFs */
5144 obj->btf_modules_loaded = true;
5145
5146 /* kernel too old to support module BTFs */
9ca1f56a 5147 if (!kernel_supports(obj, FEAT_MODULE_BTF))
4f33a53d
AN
5148 return 0;
5149
5150 while (true) {
5151 err = bpf_btf_get_next_id(id, &id);
5152 if (err && errno == ENOENT)
5153 return 0;
5154 if (err) {
5155 err = -errno;
5156 pr_warn("failed to iterate BTF objects: %d\n", err);
5157 return err;
ddc7c304 5158 }
4f33a53d
AN
5159
5160 fd = bpf_btf_get_fd_by_id(id);
5161 if (fd < 0) {
5162 if (errno == ENOENT)
5163 continue; /* expected race: BTF was unloaded */
5164 err = -errno;
5165 pr_warn("failed to get BTF object #%d FD: %d\n", id, err);
5166 return err;
5167 }
5168
5169 len = sizeof(info);
5170 memset(&info, 0, sizeof(info));
5171 info.name = ptr_to_u64(name);
5172 info.name_len = sizeof(name);
5173
5174 err = bpf_obj_get_info_by_fd(fd, &info, &len);
5175 if (err) {
5176 err = -errno;
5177 pr_warn("failed to get BTF object #%d info: %d\n", id, err);
91abb4a6 5178 goto err_out;
4f33a53d
AN
5179 }
5180
5181 /* ignore non-module BTFs */
5182 if (!info.kernel_btf || strcmp(name, "vmlinux") == 0) {
5183 close(fd);
5184 continue;
5185 }
5186
5187 btf = btf_get_from_fd(fd, obj->btf_vmlinux);
e9fc3ce9
AN
5188 err = libbpf_get_error(btf);
5189 if (err) {
5190 pr_warn("failed to load module [%s]'s BTF object #%d: %d\n",
5191 name, id, err);
91abb4a6 5192 goto err_out;
4f33a53d
AN
5193 }
5194
3b029e06
AN
5195 err = libbpf_ensure_mem((void **)&obj->btf_modules, &obj->btf_module_cap,
5196 sizeof(*obj->btf_modules), obj->btf_module_cnt + 1);
4f33a53d 5197 if (err)
91abb4a6 5198 goto err_out;
4f33a53d
AN
5199
5200 mod_btf = &obj->btf_modules[obj->btf_module_cnt++];
5201
5202 mod_btf->btf = btf;
5203 mod_btf->id = id;
91abb4a6 5204 mod_btf->fd = fd;
4f33a53d 5205 mod_btf->name = strdup(name);
91abb4a6
AN
5206 if (!mod_btf->name) {
5207 err = -ENOMEM;
5208 goto err_out;
5209 }
5210 continue;
5211
5212err_out:
5213 close(fd);
5214 return err;
ddc7c304 5215 }
4f33a53d
AN
5216
5217 return 0;
5218}
5219
301ba4d7 5220static struct bpf_core_cand_list *
0f7515ca
AN
5221bpf_core_find_cands(struct bpf_object *obj, const struct btf *local_btf, __u32 local_type_id)
5222{
301ba4d7
AS
5223 struct bpf_core_cand local_cand = {};
5224 struct bpf_core_cand_list *cands;
4f33a53d 5225 const struct btf *main_btf;
0f7515ca 5226 size_t local_essent_len;
4f33a53d 5227 int err, i;
0f7515ca
AN
5228
5229 local_cand.btf = local_btf;
5230 local_cand.t = btf__type_by_id(local_btf, local_type_id);
5231 if (!local_cand.t)
5232 return ERR_PTR(-EINVAL);
5233
5234 local_cand.name = btf__name_by_offset(local_btf, local_cand.t->name_off);
5235 if (str_is_empty(local_cand.name))
5236 return ERR_PTR(-EINVAL);
5237 local_essent_len = bpf_core_essential_name_len(local_cand.name);
5238
5239 cands = calloc(1, sizeof(*cands));
5240 if (!cands)
5241 return ERR_PTR(-ENOMEM);
5242
5243 /* Attempt to find target candidates in vmlinux BTF first */
4f33a53d
AN
5244 main_btf = obj->btf_vmlinux_override ?: obj->btf_vmlinux;
5245 err = bpf_core_add_cands(&local_cand, local_essent_len, main_btf, "vmlinux", 1, cands);
5246 if (err)
5247 goto err_out;
5248
5249 /* if vmlinux BTF has any candidate, don't got for module BTFs */
5250 if (cands->len)
5251 return cands;
5252
5253 /* if vmlinux BTF was overridden, don't attempt to load module BTFs */
5254 if (obj->btf_vmlinux_override)
5255 return cands;
5256
5257 /* now look through module BTFs, trying to still find candidates */
5258 err = load_module_btfs(obj);
5259 if (err)
5260 goto err_out;
5261
5262 for (i = 0; i < obj->btf_module_cnt; i++) {
5263 err = bpf_core_add_cands(&local_cand, local_essent_len,
5264 obj->btf_modules[i].btf,
5265 obj->btf_modules[i].name,
6a886de0 5266 btf__type_cnt(obj->btf_vmlinux),
4f33a53d
AN
5267 cands);
5268 if (err)
5269 goto err_out;
0f7515ca
AN
5270 }
5271
5272 return cands;
ddc7c304 5273err_out:
4f33a53d 5274 bpf_core_free_cands(cands);
ddc7c304
AN
5275 return ERR_PTR(err);
5276}
5277
3fc32f40
AN
5278/* Check local and target types for compatibility. This check is used for
5279 * type-based CO-RE relocations and follow slightly different rules than
5280 * field-based relocations. This function assumes that root types were already
5281 * checked for name match. Beyond that initial root-level name check, names
5282 * are completely ignored. Compatibility rules are as follows:
5283 * - any two STRUCTs/UNIONs/FWDs/ENUMs/INTs are considered compatible, but
5284 * kind should match for local and target types (i.e., STRUCT is not
5285 * compatible with UNION);
5286 * - for ENUMs, the size is ignored;
5287 * - for INT, size and signedness are ignored;
5288 * - for ARRAY, dimensionality is ignored, element types are checked for
5289 * compatibility recursively;
5290 * - CONST/VOLATILE/RESTRICT modifiers are ignored;
5291 * - TYPEDEFs/PTRs are compatible if types they pointing to are compatible;
5292 * - FUNC_PROTOs are compatible if they have compatible signature: same
5293 * number of input args and compatible return and argument types.
5294 * These rules are not set in stone and probably will be adjusted as we get
5295 * more experience with using BPF CO-RE relocations.
5296 */
b0588390
AS
5297int bpf_core_types_are_compat(const struct btf *local_btf, __u32 local_id,
5298 const struct btf *targ_btf, __u32 targ_id)
3fc32f40
AN
5299{
5300 const struct btf_type *local_type, *targ_type;
5301 int depth = 32; /* max recursion depth */
5302
5303 /* caller made sure that names match (ignoring flavor suffix) */
5304 local_type = btf__type_by_id(local_btf, local_id);
f872e4bc 5305 targ_type = btf__type_by_id(targ_btf, targ_id);
3fc32f40
AN
5306 if (btf_kind(local_type) != btf_kind(targ_type))
5307 return 0;
5308
5309recur:
5310 depth--;
5311 if (depth < 0)
5312 return -EINVAL;
5313
5314 local_type = skip_mods_and_typedefs(local_btf, local_id, &local_id);
5315 targ_type = skip_mods_and_typedefs(targ_btf, targ_id, &targ_id);
5316 if (!local_type || !targ_type)
5317 return -EINVAL;
5318
5319 if (btf_kind(local_type) != btf_kind(targ_type))
5320 return 0;
5321
5322 switch (btf_kind(local_type)) {
5323 case BTF_KIND_UNKN:
5324 case BTF_KIND_STRUCT:
5325 case BTF_KIND_UNION:
5326 case BTF_KIND_ENUM:
5327 case BTF_KIND_FWD:
5328 return 1;
5329 case BTF_KIND_INT:
5330 /* just reject deprecated bitfield-like integers; all other
5331 * integers are by default compatible between each other
5332 */
5333 return btf_int_offset(local_type) == 0 && btf_int_offset(targ_type) == 0;
5334 case BTF_KIND_PTR:
5335 local_id = local_type->type;
5336 targ_id = targ_type->type;
5337 goto recur;
5338 case BTF_KIND_ARRAY:
5339 local_id = btf_array(local_type)->type;
5340 targ_id = btf_array(targ_type)->type;
5341 goto recur;
5342 case BTF_KIND_FUNC_PROTO: {
5343 struct btf_param *local_p = btf_params(local_type);
5344 struct btf_param *targ_p = btf_params(targ_type);
5345 __u16 local_vlen = btf_vlen(local_type);
5346 __u16 targ_vlen = btf_vlen(targ_type);
5347 int i, err;
5348
5349 if (local_vlen != targ_vlen)
5350 return 0;
5351
5352 for (i = 0; i < local_vlen; i++, local_p++, targ_p++) {
5353 skip_mods_and_typedefs(local_btf, local_p->type, &local_id);
5354 skip_mods_and_typedefs(targ_btf, targ_p->type, &targ_id);
5355 err = bpf_core_types_are_compat(local_btf, local_id, targ_btf, targ_id);
5356 if (err <= 0)
5357 return err;
5358 }
5359
5360 /* tail recurse for return type check */
5361 skip_mods_and_typedefs(local_btf, local_type->type, &local_id);
5362 skip_mods_and_typedefs(targ_btf, targ_type->type, &targ_id);
5363 goto recur;
5364 }
5365 default:
5366 pr_warn("unexpected kind %s relocated, local [%d], target [%d]\n",
5367 btf_kind_str(local_type), local_id, targ_id);
5368 return 0;
5369 }
5370}
5371
ddc7c304
AN
5372static size_t bpf_core_hash_fn(const void *key, void *ctx)
5373{
5374 return (size_t)key;
5375}
5376
5377static bool bpf_core_equal_fn(const void *k1, const void *k2, void *ctx)
5378{
5379 return k1 == k2;
5380}
5381
5382static void *u32_as_hash_key(__u32 x)
5383{
5384 return (void *)(uintptr_t)x;
5385}
5386
3ee4f533
AS
5387static int bpf_core_apply_relo(struct bpf_program *prog,
5388 const struct bpf_core_relo *relo,
5389 int relo_idx,
5390 const struct btf *local_btf,
5391 struct hashmap *cand_cache)
5392{
5393 const void *type_key = u32_as_hash_key(relo->type_id);
301ba4d7 5394 struct bpf_core_cand_list *cands = NULL;
3ee4f533
AS
5395 const char *prog_name = prog->name;
5396 const struct btf_type *local_type;
5397 const char *local_name;
5398 __u32 local_id = relo->type_id;
5399 struct bpf_insn *insn;
5400 int insn_idx, err;
5401
5402 if (relo->insn_off % BPF_INSN_SZ)
5403 return -EINVAL;
5404 insn_idx = relo->insn_off / BPF_INSN_SZ;
5405 /* adjust insn_idx from section frame of reference to the local
5406 * program's frame of reference; (sub-)program code is not yet
5407 * relocated, so it's enough to just subtract in-section offset
5408 */
5409 insn_idx = insn_idx - prog->sec_insn_off;
de5d0dce 5410 if (insn_idx >= prog->insns_cnt)
3ee4f533
AS
5411 return -EINVAL;
5412 insn = &prog->insns[insn_idx];
5413
5414 local_type = btf__type_by_id(local_btf, local_id);
5415 if (!local_type)
5416 return -EINVAL;
5417
5418 local_name = btf__name_by_offset(local_btf, local_type->name_off);
5419 if (!local_name)
5420 return -EINVAL;
5421
5422 if (prog->obj->gen_loader) {
5423 pr_warn("// TODO core_relo: prog %td insn[%d] %s kind %d\n",
5424 prog - prog->obj->programs, relo->insn_off / 8,
5425 local_name, relo->kind);
5426 return -ENOTSUP;
5427 }
5428
5429 if (relo->kind != BPF_TYPE_ID_LOCAL &&
5430 !hashmap__find(cand_cache, type_key, (void **)&cands)) {
5431 cands = bpf_core_find_cands(prog->obj, local_btf, local_id);
5432 if (IS_ERR(cands)) {
5433 pr_warn("prog '%s': relo #%d: target candidate search failed for [%d] %s %s: %ld\n",
5434 prog_name, relo_idx, local_id, btf_kind_str(local_type),
5435 local_name, PTR_ERR(cands));
5436 return PTR_ERR(cands);
5437 }
5438 err = hashmap__set(cand_cache, type_key, cands, NULL, NULL);
5439 if (err) {
5440 bpf_core_free_cands(cands);
5441 return err;
5442 }
5443 }
5444
5445 return bpf_core_apply_relo_insn(prog_name, insn, insn_idx, relo, relo_idx, local_btf, cands);
5446}
5447
ddc7c304 5448static int
28b93c64 5449bpf_object__relocate_core(struct bpf_object *obj, const char *targ_btf_path)
ddc7c304
AN
5450{
5451 const struct btf_ext_info_sec *sec;
28b93c64 5452 const struct bpf_core_relo *rec;
ddc7c304
AN
5453 const struct btf_ext_info *seg;
5454 struct hashmap_entry *entry;
5455 struct hashmap *cand_cache = NULL;
5456 struct bpf_program *prog;
ddc7c304 5457 const char *sec_name;
db2b8b06 5458 int i, err = 0, insn_idx, sec_idx;
ddc7c304 5459
28b93c64
AN
5460 if (obj->btf_ext->core_relo_info.len == 0)
5461 return 0;
5462
0f7515ca
AN
5463 if (targ_btf_path) {
5464 obj->btf_vmlinux_override = btf__parse(targ_btf_path, NULL);
e9fc3ce9
AN
5465 err = libbpf_get_error(obj->btf_vmlinux_override);
5466 if (err) {
0f7515ca
AN
5467 pr_warn("failed to parse target BTF: %d\n", err);
5468 return err;
5469 }
ddc7c304
AN
5470 }
5471
5472 cand_cache = hashmap__new(bpf_core_hash_fn, bpf_core_equal_fn, NULL);
5473 if (IS_ERR(cand_cache)) {
5474 err = PTR_ERR(cand_cache);
5475 goto out;
5476 }
5477
28b93c64 5478 seg = &obj->btf_ext->core_relo_info;
ddc7c304
AN
5479 for_each_btf_ext_sec(seg, sec) {
5480 sec_name = btf__name_by_offset(obj->btf, sec->sec_name_off);
5481 if (str_is_empty(sec_name)) {
5482 err = -EINVAL;
5483 goto out;
5484 }
db2b8b06
AN
5485 /* bpf_object's ELF is gone by now so it's not easy to find
5486 * section index by section name, but we can find *any*
5487 * bpf_program within desired section name and use it's
5488 * prog->sec_idx to do a proper search by section index and
5489 * instruction offset
5490 */
9c82a63c
AN
5491 prog = NULL;
5492 for (i = 0; i < obj->nr_programs; i++) {
db2b8b06 5493 prog = &obj->programs[i];
52109584 5494 if (strcmp(prog->sec_name, sec_name) == 0)
9c82a63c 5495 break;
9c82a63c 5496 }
ddc7c304 5497 if (!prog) {
db2b8b06
AN
5498 pr_warn("sec '%s': failed to find a BPF program\n", sec_name);
5499 return -ENOENT;
ddc7c304 5500 }
db2b8b06 5501 sec_idx = prog->sec_idx;
ddc7c304 5502
9c0f8cbd 5503 pr_debug("sec '%s': found %d CO-RE relocations\n",
ddc7c304
AN
5504 sec_name, sec->num_info);
5505
5506 for_each_btf_ext_rec(seg, sec, i, rec) {
db2b8b06
AN
5507 insn_idx = rec->insn_off / BPF_INSN_SZ;
5508 prog = find_prog_by_sec_insn(obj, sec_idx, insn_idx);
5509 if (!prog) {
5510 pr_warn("sec '%s': failed to find program at insn #%d for CO-RE offset relocation #%d\n",
5511 sec_name, insn_idx, i);
5512 err = -EINVAL;
5513 goto out;
5514 }
47f7cf63
AN
5515 /* no need to apply CO-RE relocation if the program is
5516 * not going to be loaded
5517 */
5518 if (!prog->load)
5519 continue;
db2b8b06 5520
0f7515ca 5521 err = bpf_core_apply_relo(prog, rec, i, obj->btf, cand_cache);
ddc7c304 5522 if (err) {
be18010e 5523 pr_warn("prog '%s': relo #%d: failed to relocate: %d\n",
9c0f8cbd 5524 prog->name, i, err);
ddc7c304
AN
5525 goto out;
5526 }
5527 }
5528 }
5529
5530out:
4f33a53d 5531 /* obj->btf_vmlinux and module BTFs are freed after object load */
0f7515ca
AN
5532 btf__free(obj->btf_vmlinux_override);
5533 obj->btf_vmlinux_override = NULL;
5534
ddc7c304
AN
5535 if (!IS_ERR_OR_NULL(cand_cache)) {
5536 hashmap__for_each_entry(cand_cache, entry, i) {
5537 bpf_core_free_cands(entry->value);
5538 }
5539 hashmap__free(cand_cache);
5540 }
5541 return err;
5542}
5543
c3c55696
AN
5544/* Relocate data references within program code:
5545 * - map references;
5546 * - global variable references;
5547 * - extern references.
5548 */
48cca7e4 5549static int
c3c55696 5550bpf_object__relocate_data(struct bpf_object *obj, struct bpf_program *prog)
8a47a6c5 5551{
c3c55696 5552 int i;
8a47a6c5
WN
5553
5554 for (i = 0; i < prog->nr_reloc; i++) {
53f8dd43 5555 struct reloc_desc *relo = &prog->reloc_desc[i];
166750bc 5556 struct bpf_insn *insn = &prog->insns[relo->insn_idx];
2e33efe3 5557 struct extern_desc *ext;
8a47a6c5 5558
166750bc
AN
5559 switch (relo->type) {
5560 case RELO_LD64:
e2fa0156
AS
5561 if (obj->gen_loader) {
5562 insn[0].src_reg = BPF_PSEUDO_MAP_IDX;
5563 insn[0].imm = relo->map_idx;
5564 } else {
5565 insn[0].src_reg = BPF_PSEUDO_MAP_FD;
5566 insn[0].imm = obj->maps[relo->map_idx].fd;
5567 }
166750bc
AN
5568 break;
5569 case RELO_DATA:
166750bc 5570 insn[1].imm = insn[0].imm + relo->sym_off;
e2fa0156
AS
5571 if (obj->gen_loader) {
5572 insn[0].src_reg = BPF_PSEUDO_MAP_IDX_VALUE;
5573 insn[0].imm = relo->map_idx;
5574 } else {
5575 insn[0].src_reg = BPF_PSEUDO_MAP_VALUE;
5576 insn[0].imm = obj->maps[relo->map_idx].fd;
5577 }
166750bc 5578 break;
0c091e5c 5579 case RELO_EXTERN_VAR:
2e33efe3 5580 ext = &obj->externs[relo->sym_off];
1c0c7074 5581 if (ext->type == EXT_KCFG) {
e2fa0156
AS
5582 if (obj->gen_loader) {
5583 insn[0].src_reg = BPF_PSEUDO_MAP_IDX_VALUE;
5584 insn[0].imm = obj->kconfig_map_idx;
5585 } else {
5586 insn[0].src_reg = BPF_PSEUDO_MAP_VALUE;
5587 insn[0].imm = obj->maps[obj->kconfig_map_idx].fd;
5588 }
1c0c7074
AN
5589 insn[1].imm = ext->kcfg.data_off;
5590 } else /* EXT_KSYM */ {
2211c825 5591 if (ext->ksym.type_id && ext->is_set) { /* typed ksyms */
d370bbe1 5592 insn[0].src_reg = BPF_PSEUDO_BTF_ID;
284d2587
AN
5593 insn[0].imm = ext->ksym.kernel_btf_id;
5594 insn[1].imm = ext->ksym.kernel_btf_obj_fd;
2211c825 5595 } else { /* typeless ksyms or unresolved typed ksyms */
d370bbe1
HL
5596 insn[0].imm = (__u32)ext->ksym.addr;
5597 insn[1].imm = ext->ksym.addr >> 32;
5598 }
1c0c7074 5599 }
166750bc 5600 break;
5bd022ec
MKL
5601 case RELO_EXTERN_FUNC:
5602 ext = &obj->externs[relo->sym_off];
5603 insn[0].src_reg = BPF_PSEUDO_KFUNC_CALL;
466b2e13
KKD
5604 if (ext->is_set) {
5605 insn[0].imm = ext->ksym.kernel_btf_id;
5606 insn[0].off = ext->ksym.btf_fd_idx;
5607 } else { /* unresolved weak kfunc */
5608 insn[0].imm = 0;
5609 insn[0].off = 0;
5610 }
5bd022ec 5611 break;
53eddb5e 5612 case RELO_SUBPROG_ADDR:
b1268826
AS
5613 if (insn[0].src_reg != BPF_PSEUDO_FUNC) {
5614 pr_warn("prog '%s': relo #%d: bad insn\n",
5615 prog->name, i);
5616 return -EINVAL;
5617 }
5618 /* handled already */
53eddb5e 5619 break;
166750bc 5620 case RELO_CALL:
b1268826 5621 /* handled already */
166750bc
AN
5622 break;
5623 default:
9c0f8cbd
AN
5624 pr_warn("prog '%s': relo #%d: bad relo type %d\n",
5625 prog->name, i, relo->type);
166750bc 5626 return -EINVAL;
8a47a6c5 5627 }
8a47a6c5
WN
5628 }
5629
c3c55696
AN
5630 return 0;
5631}
5632
8505e870
AN
5633static int adjust_prog_btf_ext_info(const struct bpf_object *obj,
5634 const struct bpf_program *prog,
5635 const struct btf_ext_info *ext_info,
5636 void **prog_info, __u32 *prog_rec_cnt,
5637 __u32 *prog_rec_sz)
5638{
5639 void *copy_start = NULL, *copy_end = NULL;
5640 void *rec, *rec_end, *new_prog_info;
5641 const struct btf_ext_info_sec *sec;
5642 size_t old_sz, new_sz;
5643 const char *sec_name;
5644 int i, off_adj;
5645
5646 for_each_btf_ext_sec(ext_info, sec) {
5647 sec_name = btf__name_by_offset(obj->btf, sec->sec_name_off);
5648 if (!sec_name)
5649 return -EINVAL;
52109584 5650 if (strcmp(sec_name, prog->sec_name) != 0)
8505e870
AN
5651 continue;
5652
5653 for_each_btf_ext_rec(ext_info, sec, i, rec) {
5654 __u32 insn_off = *(__u32 *)rec / BPF_INSN_SZ;
5655
5656 if (insn_off < prog->sec_insn_off)
5657 continue;
5658 if (insn_off >= prog->sec_insn_off + prog->sec_insn_cnt)
5659 break;
5660
5661 if (!copy_start)
5662 copy_start = rec;
5663 copy_end = rec + ext_info->rec_size;
5664 }
5665
5666 if (!copy_start)
5667 return -ENOENT;
5668
5669 /* append func/line info of a given (sub-)program to the main
5670 * program func/line info
5671 */
8eb62958 5672 old_sz = (size_t)(*prog_rec_cnt) * ext_info->rec_size;
8505e870
AN
5673 new_sz = old_sz + (copy_end - copy_start);
5674 new_prog_info = realloc(*prog_info, new_sz);
5675 if (!new_prog_info)
5676 return -ENOMEM;
5677 *prog_info = new_prog_info;
5678 *prog_rec_cnt = new_sz / ext_info->rec_size;
5679 memcpy(new_prog_info + old_sz, copy_start, copy_end - copy_start);
5680
5681 /* Kernel instruction offsets are in units of 8-byte
5682 * instructions, while .BTF.ext instruction offsets generated
5683 * by Clang are in units of bytes. So convert Clang offsets
5684 * into kernel offsets and adjust offset according to program
5685 * relocated position.
5686 */
5687 off_adj = prog->sub_insn_off - prog->sec_insn_off;
5688 rec = new_prog_info + old_sz;
5689 rec_end = new_prog_info + new_sz;
5690 for (; rec < rec_end; rec += ext_info->rec_size) {
5691 __u32 *insn_off = rec;
5692
5693 *insn_off = *insn_off / BPF_INSN_SZ + off_adj;
5694 }
5695 *prog_rec_sz = ext_info->rec_size;
5696 return 0;
5697 }
5698
5699 return -ENOENT;
5700}
5701
5702static int
5703reloc_prog_func_and_line_info(const struct bpf_object *obj,
5704 struct bpf_program *main_prog,
5705 const struct bpf_program *prog)
5706{
5707 int err;
5708
5709 /* no .BTF.ext relocation if .BTF.ext is missing or kernel doesn't
5710 * supprot func/line info
5711 */
9ca1f56a 5712 if (!obj->btf_ext || !kernel_supports(obj, FEAT_BTF_FUNC))
8505e870
AN
5713 return 0;
5714
5715 /* only attempt func info relocation if main program's func_info
5716 * relocation was successful
5717 */
5718 if (main_prog != prog && !main_prog->func_info)
5719 goto line_info;
5720
5721 err = adjust_prog_btf_ext_info(obj, prog, &obj->btf_ext->func_info,
5722 &main_prog->func_info,
5723 &main_prog->func_info_cnt,
5724 &main_prog->func_info_rec_size);
5725 if (err) {
5726 if (err != -ENOENT) {
5727 pr_warn("prog '%s': error relocating .BTF.ext function info: %d\n",
5728 prog->name, err);
5729 return err;
5730 }
5731 if (main_prog->func_info) {
5732 /*
5733 * Some info has already been found but has problem
5734 * in the last btf_ext reloc. Must have to error out.
5735 */
5736 pr_warn("prog '%s': missing .BTF.ext function info.\n", prog->name);
5737 return err;
5738 }
5739 /* Have problem loading the very first info. Ignore the rest. */
5740 pr_warn("prog '%s': missing .BTF.ext function info for the main program, skipping all of .BTF.ext func info.\n",
5741 prog->name);
5742 }
5743
5744line_info:
5745 /* don't relocate line info if main program's relocation failed */
5746 if (main_prog != prog && !main_prog->line_info)
5747 return 0;
5748
5749 err = adjust_prog_btf_ext_info(obj, prog, &obj->btf_ext->line_info,
5750 &main_prog->line_info,
5751 &main_prog->line_info_cnt,
5752 &main_prog->line_info_rec_size);
5753 if (err) {
5754 if (err != -ENOENT) {
5755 pr_warn("prog '%s': error relocating .BTF.ext line info: %d\n",
5756 prog->name, err);
5757 return err;
5758 }
5759 if (main_prog->line_info) {
5760 /*
5761 * Some info has already been found but has problem
5762 * in the last btf_ext reloc. Must have to error out.
5763 */
5764 pr_warn("prog '%s': missing .BTF.ext line info.\n", prog->name);
5765 return err;
5766 }
5767 /* Have problem loading the very first info. Ignore the rest. */
5768 pr_warn("prog '%s': missing .BTF.ext line info for the main program, skipping all of .BTF.ext line info.\n",
5769 prog->name);
5770 }
5771 return 0;
5772}
5773
c3c55696
AN
5774static int cmp_relo_by_insn_idx(const void *key, const void *elem)
5775{
5776 size_t insn_idx = *(const size_t *)key;
5777 const struct reloc_desc *relo = elem;
5778
5779 if (insn_idx == relo->insn_idx)
5780 return 0;
5781 return insn_idx < relo->insn_idx ? -1 : 1;
5782}
5783
5784static struct reloc_desc *find_prog_insn_relo(const struct bpf_program *prog, size_t insn_idx)
5785{
5786 return bsearch(&insn_idx, prog->reloc_desc, prog->nr_reloc,
5787 sizeof(*prog->reloc_desc), cmp_relo_by_insn_idx);
5788}
5789
b1268826
AS
5790static int append_subprog_relos(struct bpf_program *main_prog, struct bpf_program *subprog)
5791{
5792 int new_cnt = main_prog->nr_reloc + subprog->nr_reloc;
5793 struct reloc_desc *relos;
5794 int i;
5795
5796 if (main_prog == subprog)
5797 return 0;
5798 relos = libbpf_reallocarray(main_prog->reloc_desc, new_cnt, sizeof(*relos));
5799 if (!relos)
5800 return -ENOMEM;
5801 memcpy(relos + main_prog->nr_reloc, subprog->reloc_desc,
5802 sizeof(*relos) * subprog->nr_reloc);
5803
5804 for (i = main_prog->nr_reloc; i < new_cnt; i++)
5805 relos[i].insn_idx += subprog->sub_insn_off;
5806 /* After insn_idx adjustment the 'relos' array is still sorted
5807 * by insn_idx and doesn't break bsearch.
5808 */
5809 main_prog->reloc_desc = relos;
5810 main_prog->nr_reloc = new_cnt;
5811 return 0;
5812}
5813
c3c55696
AN
5814static int
5815bpf_object__reloc_code(struct bpf_object *obj, struct bpf_program *main_prog,
5816 struct bpf_program *prog)
5817{
5818 size_t sub_insn_idx, insn_idx, new_cnt;
5819 struct bpf_program *subprog;
5820 struct bpf_insn *insns, *insn;
5821 struct reloc_desc *relo;
5822 int err;
5823
5824 err = reloc_prog_func_and_line_info(obj, main_prog, prog);
5825 if (err)
5826 return err;
5827
5828 for (insn_idx = 0; insn_idx < prog->sec_insn_cnt; insn_idx++) {
5829 insn = &main_prog->insns[prog->sub_insn_off + insn_idx];
53eddb5e 5830 if (!insn_is_subprog_call(insn) && !insn_is_pseudo_func(insn))
c3c55696
AN
5831 continue;
5832
5833 relo = find_prog_insn_relo(prog, insn_idx);
b1268826
AS
5834 if (relo && relo->type == RELO_EXTERN_FUNC)
5835 /* kfunc relocations will be handled later
5836 * in bpf_object__relocate_data()
5837 */
5838 continue;
53eddb5e 5839 if (relo && relo->type != RELO_CALL && relo->type != RELO_SUBPROG_ADDR) {
c3c55696
AN
5840 pr_warn("prog '%s': unexpected relo for insn #%zu, type %d\n",
5841 prog->name, insn_idx, relo->type);
5842 return -LIBBPF_ERRNO__RELOC;
5843 }
5844 if (relo) {
5845 /* sub-program instruction index is a combination of
5846 * an offset of a symbol pointed to by relocation and
5847 * call instruction's imm field; for global functions,
5848 * call always has imm = -1, but for static functions
5849 * relocation is against STT_SECTION and insn->imm
5850 * points to a start of a static function
53eddb5e
YS
5851 *
5852 * for subprog addr relocation, the relo->sym_off + insn->imm is
5853 * the byte offset in the corresponding section.
c3c55696 5854 */
53eddb5e
YS
5855 if (relo->type == RELO_CALL)
5856 sub_insn_idx = relo->sym_off / BPF_INSN_SZ + insn->imm + 1;
5857 else
5858 sub_insn_idx = (relo->sym_off + insn->imm) / BPF_INSN_SZ;
5859 } else if (insn_is_pseudo_func(insn)) {
5860 /*
5861 * RELO_SUBPROG_ADDR relo is always emitted even if both
5862 * functions are in the same section, so it shouldn't reach here.
5863 */
5864 pr_warn("prog '%s': missing subprog addr relo for insn #%zu\n",
5865 prog->name, insn_idx);
5866 return -LIBBPF_ERRNO__RELOC;
c3c55696
AN
5867 } else {
5868 /* if subprogram call is to a static function within
5869 * the same ELF section, there won't be any relocation
5870 * emitted, but it also means there is no additional
5871 * offset necessary, insns->imm is relative to
5872 * instruction's original position within the section
5873 */
5874 sub_insn_idx = prog->sec_insn_off + insn_idx + insn->imm + 1;
5875 }
5876
5877 /* we enforce that sub-programs should be in .text section */
5878 subprog = find_prog_by_sec_insn(obj, obj->efile.text_shndx, sub_insn_idx);
5879 if (!subprog) {
5880 pr_warn("prog '%s': no .text section found yet sub-program call exists\n",
5881 prog->name);
5882 return -LIBBPF_ERRNO__RELOC;
5883 }
5884
5885 /* if it's the first call instruction calling into this
5886 * subprogram (meaning this subprog hasn't been processed
5887 * yet) within the context of current main program:
5888 * - append it at the end of main program's instructions blog;
5889 * - process is recursively, while current program is put on hold;
5890 * - if that subprogram calls some other not yet processes
5891 * subprogram, same thing will happen recursively until
5892 * there are no more unprocesses subprograms left to append
5893 * and relocate.
5894 */
5895 if (subprog->sub_insn_off == 0) {
5896 subprog->sub_insn_off = main_prog->insns_cnt;
5897
5898 new_cnt = main_prog->insns_cnt + subprog->insns_cnt;
5899 insns = libbpf_reallocarray(main_prog->insns, new_cnt, sizeof(*insns));
5900 if (!insns) {
5901 pr_warn("prog '%s': failed to realloc prog code\n", main_prog->name);
5902 return -ENOMEM;
5903 }
5904 main_prog->insns = insns;
5905 main_prog->insns_cnt = new_cnt;
5906
5907 memcpy(main_prog->insns + subprog->sub_insn_off, subprog->insns,
5908 subprog->insns_cnt * sizeof(*insns));
5909
5910 pr_debug("prog '%s': added %zu insns from sub-prog '%s'\n",
5911 main_prog->name, subprog->insns_cnt, subprog->name);
5912
b1268826
AS
5913 /* The subprog insns are now appended. Append its relos too. */
5914 err = append_subprog_relos(main_prog, subprog);
5915 if (err)
5916 return err;
c3c55696
AN
5917 err = bpf_object__reloc_code(obj, main_prog, subprog);
5918 if (err)
5919 return err;
5920 }
5921
5922 /* main_prog->insns memory could have been re-allocated, so
5923 * calculate pointer again
5924 */
5925 insn = &main_prog->insns[prog->sub_insn_off + insn_idx];
5926 /* calculate correct instruction position within current main
5927 * prog; each main prog can have a different set of
5928 * subprograms appended (potentially in different order as
5929 * well), so position of any subprog can be different for
5930 * different main programs */
5931 insn->imm = subprog->sub_insn_off - (prog->sub_insn_off + insn_idx) - 1;
5932
c3c55696
AN
5933 pr_debug("prog '%s': insn #%zu relocated, imm %d points to subprog '%s' (now at %zu offset)\n",
5934 prog->name, insn_idx, insn->imm, subprog->name, subprog->sub_insn_off);
5935 }
5936
5937 return 0;
5938}
5939
5940/*
5941 * Relocate sub-program calls.
5942 *
5943 * Algorithm operates as follows. Each entry-point BPF program (referred to as
5944 * main prog) is processed separately. For each subprog (non-entry functions,
5945 * that can be called from either entry progs or other subprogs) gets their
5946 * sub_insn_off reset to zero. This serves as indicator that this subprogram
5947 * hasn't been yet appended and relocated within current main prog. Once its
5948 * relocated, sub_insn_off will point at the position within current main prog
5949 * where given subprog was appended. This will further be used to relocate all
5950 * the call instructions jumping into this subprog.
5951 *
5952 * We start with main program and process all call instructions. If the call
5953 * is into a subprog that hasn't been processed (i.e., subprog->sub_insn_off
5954 * is zero), subprog instructions are appended at the end of main program's
5955 * instruction array. Then main program is "put on hold" while we recursively
5956 * process newly appended subprogram. If that subprogram calls into another
5957 * subprogram that hasn't been appended, new subprogram is appended again to
5958 * the *main* prog's instructions (subprog's instructions are always left
5959 * untouched, as they need to be in unmodified state for subsequent main progs
5960 * and subprog instructions are always sent only as part of a main prog) and
5961 * the process continues recursively. Once all the subprogs called from a main
5962 * prog or any of its subprogs are appended (and relocated), all their
5963 * positions within finalized instructions array are known, so it's easy to
5964 * rewrite call instructions with correct relative offsets, corresponding to
5965 * desired target subprog.
5966 *
5967 * Its important to realize that some subprogs might not be called from some
5968 * main prog and any of its called/used subprogs. Those will keep their
5969 * subprog->sub_insn_off as zero at all times and won't be appended to current
5970 * main prog and won't be relocated within the context of current main prog.
5971 * They might still be used from other main progs later.
5972 *
5973 * Visually this process can be shown as below. Suppose we have two main
5974 * programs mainA and mainB and BPF object contains three subprogs: subA,
5975 * subB, and subC. mainA calls only subA, mainB calls only subC, but subA and
5976 * subC both call subB:
5977 *
5978 * +--------+ +-------+
5979 * | v v |
5980 * +--+---+ +--+-+-+ +---+--+
5981 * | subA | | subB | | subC |
5982 * +--+---+ +------+ +---+--+
5983 * ^ ^
5984 * | |
5985 * +---+-------+ +------+----+
5986 * | mainA | | mainB |
5987 * +-----------+ +-----------+
5988 *
5989 * We'll start relocating mainA, will find subA, append it and start
5990 * processing sub A recursively:
5991 *
5992 * +-----------+------+
5993 * | mainA | subA |
5994 * +-----------+------+
5995 *
5996 * At this point we notice that subB is used from subA, so we append it and
5997 * relocate (there are no further subcalls from subB):
5998 *
5999 * +-----------+------+------+
6000 * | mainA | subA | subB |
6001 * +-----------+------+------+
6002 *
6003 * At this point, we relocate subA calls, then go one level up and finish with
6004 * relocatin mainA calls. mainA is done.
6005 *
6006 * For mainB process is similar but results in different order. We start with
6007 * mainB and skip subA and subB, as mainB never calls them (at least
6008 * directly), but we see subC is needed, so we append and start processing it:
6009 *
6010 * +-----------+------+
6011 * | mainB | subC |
6012 * +-----------+------+
6013 * Now we see subC needs subB, so we go back to it, append and relocate it:
6014 *
6015 * +-----------+------+------+
6016 * | mainB | subC | subB |
6017 * +-----------+------+------+
6018 *
6019 * At this point we unwind recursion, relocate calls in subC, then in mainB.
6020 */
6021static int
6022bpf_object__relocate_calls(struct bpf_object *obj, struct bpf_program *prog)
6023{
6024 struct bpf_program *subprog;
d3d93e34 6025 int i, err;
c3c55696 6026
c3c55696
AN
6027 /* mark all subprogs as not relocated (yet) within the context of
6028 * current main program
6029 */
6030 for (i = 0; i < obj->nr_programs; i++) {
6031 subprog = &obj->programs[i];
6032 if (!prog_is_subprog(obj, subprog))
6033 continue;
6034
6035 subprog->sub_insn_off = 0;
c3c55696
AN
6036 }
6037
6038 err = bpf_object__reloc_code(obj, prog, prog);
6039 if (err)
6040 return err;
6041
6042
8a47a6c5
WN
6043 return 0;
6044}
6045
67234743
AS
6046static void
6047bpf_object__free_relocs(struct bpf_object *obj)
6048{
6049 struct bpf_program *prog;
6050 int i;
6051
6052 /* free up relocation descriptors */
6053 for (i = 0; i < obj->nr_programs; i++) {
6054 prog = &obj->programs[i];
6055 zfree(&prog->reloc_desc);
6056 prog->nr_reloc = 0;
6057 }
6058}
6059
8a47a6c5 6060static int
ddc7c304 6061bpf_object__relocate(struct bpf_object *obj, const char *targ_btf_path)
8a47a6c5
WN
6062{
6063 struct bpf_program *prog;
b1268826 6064 size_t i, j;
8a47a6c5
WN
6065 int err;
6066
ddc7c304
AN
6067 if (obj->btf_ext) {
6068 err = bpf_object__relocate_core(obj, targ_btf_path);
6069 if (err) {
be18010e
KW
6070 pr_warn("failed to perform CO-RE relocations: %d\n",
6071 err);
ddc7c304
AN
6072 return err;
6073 }
6074 }
b1268826
AS
6075
6076 /* Before relocating calls pre-process relocations and mark
6077 * few ld_imm64 instructions that points to subprogs.
6078 * Otherwise bpf_object__reloc_code() later would have to consider
6079 * all ld_imm64 insns as relocation candidates. That would
6080 * reduce relocation speed, since amount of find_prog_insn_relo()
6081 * would increase and most of them will fail to find a relo.
9173cac3
AN
6082 */
6083 for (i = 0; i < obj->nr_programs; i++) {
6084 prog = &obj->programs[i];
b1268826
AS
6085 for (j = 0; j < prog->nr_reloc; j++) {
6086 struct reloc_desc *relo = &prog->reloc_desc[j];
6087 struct bpf_insn *insn = &prog->insns[relo->insn_idx];
6088
6089 /* mark the insn, so it's recognized by insn_is_pseudo_func() */
6090 if (relo->type == RELO_SUBPROG_ADDR)
6091 insn[0].src_reg = BPF_PSEUDO_FUNC;
9173cac3 6092 }
9173cac3 6093 }
b1268826
AS
6094
6095 /* relocate subprogram calls and append used subprograms to main
c3c55696
AN
6096 * programs; each copy of subprogram code needs to be relocated
6097 * differently for each main program, because its code location might
b1268826
AS
6098 * have changed.
6099 * Append subprog relos to main programs to allow data relos to be
6100 * processed after text is completely relocated.
9173cac3 6101 */
8a47a6c5
WN
6102 for (i = 0; i < obj->nr_programs; i++) {
6103 prog = &obj->programs[i];
c3c55696
AN
6104 /* sub-program's sub-calls are relocated within the context of
6105 * its main program only
6106 */
6107 if (prog_is_subprog(obj, prog))
9173cac3 6108 continue;
8a47a6c5 6109
c3c55696 6110 err = bpf_object__relocate_calls(obj, prog);
8a47a6c5 6111 if (err) {
9c0f8cbd
AN
6112 pr_warn("prog '%s': failed to relocate calls: %d\n",
6113 prog->name, err);
8a47a6c5
WN
6114 return err;
6115 }
6116 }
b1268826
AS
6117 /* Process data relos for main programs */
6118 for (i = 0; i < obj->nr_programs; i++) {
6119 prog = &obj->programs[i];
6120 if (prog_is_subprog(obj, prog))
6121 continue;
6122 err = bpf_object__relocate_data(obj, prog);
6123 if (err) {
6124 pr_warn("prog '%s': failed to relocate data references: %d\n",
6125 prog->name, err);
6126 return err;
6127 }
6128 }
67234743
AS
6129 if (!obj->gen_loader)
6130 bpf_object__free_relocs(obj);
8a47a6c5
WN
6131 return 0;
6132}
6133
646f02ff 6134static int bpf_object__collect_st_ops_relos(struct bpf_object *obj,
ad23b723 6135 Elf64_Shdr *shdr, Elf_Data *data);
646f02ff
AN
6136
6137static int bpf_object__collect_map_relos(struct bpf_object *obj,
ad23b723 6138 Elf64_Shdr *shdr, Elf_Data *data)
646f02ff 6139{
15728ad3
AN
6140 const int bpf_ptr_sz = 8, host_ptr_sz = sizeof(void *);
6141 int i, j, nrels, new_sz;
063e6881 6142 const struct btf_var_secinfo *vi = NULL;
646f02ff 6143 const struct btf_type *sec, *var, *def;
3168c158 6144 struct bpf_map *map = NULL, *targ_map;
646f02ff 6145 const struct btf_member *member;
646f02ff 6146 const char *name, *mname;
646f02ff 6147 unsigned int moff;
ad23b723
AN
6148 Elf64_Sym *sym;
6149 Elf64_Rel *rel;
646f02ff
AN
6150 void *tmp;
6151
6152 if (!obj->efile.btf_maps_sec_btf_id || !obj->btf)
6153 return -EINVAL;
6154 sec = btf__type_by_id(obj->btf, obj->efile.btf_maps_sec_btf_id);
6155 if (!sec)
6156 return -EINVAL;
6157
646f02ff
AN
6158 nrels = shdr->sh_size / shdr->sh_entsize;
6159 for (i = 0; i < nrels; i++) {
ad23b723
AN
6160 rel = elf_rel_by_idx(data, i);
6161 if (!rel) {
646f02ff
AN
6162 pr_warn(".maps relo #%d: failed to get ELF relo\n", i);
6163 return -LIBBPF_ERRNO__FORMAT;
6164 }
ad23b723
AN
6165
6166 sym = elf_sym_by_idx(obj, ELF64_R_SYM(rel->r_info));
6167 if (!sym) {
646f02ff 6168 pr_warn(".maps relo #%d: symbol %zx not found\n",
ad23b723 6169 i, (size_t)ELF64_R_SYM(rel->r_info));
646f02ff
AN
6170 return -LIBBPF_ERRNO__FORMAT;
6171 }
ad23b723
AN
6172 name = elf_sym_str(obj, sym->st_name) ?: "<?>";
6173 if (sym->st_shndx != obj->efile.btf_maps_shndx) {
646f02ff
AN
6174 pr_warn(".maps relo #%d: '%s' isn't a BTF-defined map\n",
6175 i, name);
6176 return -LIBBPF_ERRNO__RELOC;
6177 }
6178
ad23b723
AN
6179 pr_debug(".maps relo #%d: for %zd value %zd rel->r_offset %zu name %d ('%s')\n",
6180 i, (ssize_t)(rel->r_info >> 32), (size_t)sym->st_value,
6181 (size_t)rel->r_offset, sym->st_name, name);
646f02ff
AN
6182
6183 for (j = 0; j < obj->nr_maps; j++) {
6184 map = &obj->maps[j];
6185 if (map->sec_idx != obj->efile.btf_maps_shndx)
6186 continue;
6187
6188 vi = btf_var_secinfos(sec) + map->btf_var_idx;
ad23b723
AN
6189 if (vi->offset <= rel->r_offset &&
6190 rel->r_offset + bpf_ptr_sz <= vi->offset + vi->size)
646f02ff
AN
6191 break;
6192 }
6193 if (j == obj->nr_maps) {
ad23b723
AN
6194 pr_warn(".maps relo #%d: cannot find map '%s' at rel->r_offset %zu\n",
6195 i, name, (size_t)rel->r_offset);
646f02ff
AN
6196 return -EINVAL;
6197 }
6198
6199 if (!bpf_map_type__is_map_in_map(map->def.type))
6200 return -EINVAL;
6201 if (map->def.type == BPF_MAP_TYPE_HASH_OF_MAPS &&
6202 map->def.key_size != sizeof(int)) {
6203 pr_warn(".maps relo #%d: hash-of-maps '%s' should have key size %zu.\n",
6204 i, map->name, sizeof(int));
6205 return -EINVAL;
6206 }
6207
6208 targ_map = bpf_object__find_map_by_name(obj, name);
6209 if (!targ_map)
6210 return -ESRCH;
6211
6212 var = btf__type_by_id(obj->btf, vi->type);
6213 def = skip_mods_and_typedefs(obj->btf, var->type, NULL);
6214 if (btf_vlen(def) == 0)
6215 return -EINVAL;
6216 member = btf_members(def) + btf_vlen(def) - 1;
6217 mname = btf__name_by_offset(obj->btf, member->name_off);
6218 if (strcmp(mname, "values"))
6219 return -EINVAL;
6220
6221 moff = btf_member_bit_offset(def, btf_vlen(def) - 1) / 8;
ad23b723 6222 if (rel->r_offset - vi->offset < moff)
646f02ff
AN
6223 return -EINVAL;
6224
ad23b723 6225 moff = rel->r_offset - vi->offset - moff;
15728ad3
AN
6226 /* here we use BPF pointer size, which is always 64 bit, as we
6227 * are parsing ELF that was built for BPF target
6228 */
6229 if (moff % bpf_ptr_sz)
646f02ff 6230 return -EINVAL;
15728ad3 6231 moff /= bpf_ptr_sz;
646f02ff
AN
6232 if (moff >= map->init_slots_sz) {
6233 new_sz = moff + 1;
029258d7 6234 tmp = libbpf_reallocarray(map->init_slots, new_sz, host_ptr_sz);
646f02ff
AN
6235 if (!tmp)
6236 return -ENOMEM;
6237 map->init_slots = tmp;
6238 memset(map->init_slots + map->init_slots_sz, 0,
15728ad3 6239 (new_sz - map->init_slots_sz) * host_ptr_sz);
646f02ff
AN
6240 map->init_slots_sz = new_sz;
6241 }
6242 map->init_slots[moff] = targ_map;
6243
6244 pr_debug(".maps relo #%d: map '%s' slot [%d] points to map '%s'\n",
6245 i, map->name, moff, name);
6246 }
6247
6248 return 0;
6249}
590a0088 6250
c3c55696 6251static int cmp_relocs(const void *_a, const void *_b)
34090915 6252{
c3c55696
AN
6253 const struct reloc_desc *a = _a;
6254 const struct reloc_desc *b = _b;
34090915 6255
c3c55696
AN
6256 if (a->insn_idx != b->insn_idx)
6257 return a->insn_idx < b->insn_idx ? -1 : 1;
6258
6259 /* no two relocations should have the same insn_idx, but ... */
6260 if (a->type != b->type)
6261 return a->type < b->type ? -1 : 1;
6262
6263 return 0;
6264}
6265
6266static int bpf_object__collect_relos(struct bpf_object *obj)
6267{
6268 int i, err;
34090915 6269
25bbbd7a
AN
6270 for (i = 0; i < obj->efile.sec_cnt; i++) {
6271 struct elf_sec_desc *sec_desc = &obj->efile.secs[i];
6272 Elf64_Shdr *shdr;
6273 Elf_Data *data;
6274 int idx;
6275
6276 if (sec_desc->sec_type != SEC_RELO)
6277 continue;
6278
6279 shdr = sec_desc->shdr;
6280 data = sec_desc->data;
6281 idx = shdr->sh_info;
34090915
WN
6282
6283 if (shdr->sh_type != SHT_REL) {
be18010e 6284 pr_warn("internal error at %d\n", __LINE__);
6371ca3b 6285 return -LIBBPF_ERRNO__INTERNAL;
34090915
WN
6286 }
6287
c3c55696 6288 if (idx == obj->efile.st_ops_shndx)
646f02ff 6289 err = bpf_object__collect_st_ops_relos(obj, shdr, data);
c3c55696 6290 else if (idx == obj->efile.btf_maps_shndx)
646f02ff 6291 err = bpf_object__collect_map_relos(obj, shdr, data);
c3c55696
AN
6292 else
6293 err = bpf_object__collect_prog_relos(obj, shdr, data);
34090915 6294 if (err)
6371ca3b 6295 return err;
34090915 6296 }
c3c55696
AN
6297
6298 for (i = 0; i < obj->nr_programs; i++) {
6299 struct bpf_program *p = &obj->programs[i];
c139e40a 6300
c3c55696
AN
6301 if (!p->nr_reloc)
6302 continue;
6303
6304 qsort(p->reloc_desc, p->nr_reloc, sizeof(*p->reloc_desc), cmp_relocs);
6305 }
34090915
WN
6306 return 0;
6307}
6308
109cea5a
AN
6309static bool insn_is_helper_call(struct bpf_insn *insn, enum bpf_func_id *func_id)
6310{
9b2f6fec 6311 if (BPF_CLASS(insn->code) == BPF_JMP &&
109cea5a
AN
6312 BPF_OP(insn->code) == BPF_CALL &&
6313 BPF_SRC(insn->code) == BPF_K &&
9b2f6fec
AN
6314 insn->src_reg == 0 &&
6315 insn->dst_reg == 0) {
6316 *func_id = insn->imm;
109cea5a
AN
6317 return true;
6318 }
6319 return false;
6320}
6321
42869d28 6322static int bpf_object__sanitize_prog(struct bpf_object *obj, struct bpf_program *prog)
109cea5a
AN
6323{
6324 struct bpf_insn *insn = prog->insns;
6325 enum bpf_func_id func_id;
6326 int i;
6327
67234743
AS
6328 if (obj->gen_loader)
6329 return 0;
6330
109cea5a
AN
6331 for (i = 0; i < prog->insns_cnt; i++, insn++) {
6332 if (!insn_is_helper_call(insn, &func_id))
6333 continue;
6334
6335 /* on kernels that don't yet support
6336 * bpf_probe_read_{kernel,user}[_str] helpers, fall back
6337 * to bpf_probe_read() which works well for old kernels
6338 */
6339 switch (func_id) {
6340 case BPF_FUNC_probe_read_kernel:
6341 case BPF_FUNC_probe_read_user:
9ca1f56a 6342 if (!kernel_supports(obj, FEAT_PROBE_READ_KERN))
109cea5a
AN
6343 insn->imm = BPF_FUNC_probe_read;
6344 break;
6345 case BPF_FUNC_probe_read_kernel_str:
6346 case BPF_FUNC_probe_read_user_str:
9ca1f56a 6347 if (!kernel_supports(obj, FEAT_PROBE_READ_KERN))
109cea5a
AN
6348 insn->imm = BPF_FUNC_probe_read_str;
6349 break;
6350 default:
6351 break;
6352 }
6353 }
6354 return 0;
6355}
6356
15ea31fa
AN
6357static int libbpf_find_attach_btf_id(struct bpf_program *prog, const char *attach_name,
6358 int *btf_obj_fd, int *btf_type_id);
12d9466d
AN
6359
6360/* this is called as prog->sec_def->preload_fn for libbpf-supported sec_defs */
6361static int libbpf_preload_prog(struct bpf_program *prog,
6362 struct bpf_prog_load_params *attr, long cookie)
6363{
15ea31fa
AN
6364 enum sec_def_flags def = cookie;
6365
12d9466d 6366 /* old kernels might not support specifying expected_attach_type */
15ea31fa 6367 if ((def & SEC_EXP_ATTACH_OPT) && !kernel_supports(prog->obj, FEAT_EXP_ATTACH_TYPE))
12d9466d
AN
6368 attr->expected_attach_type = 0;
6369
15ea31fa 6370 if (def & SEC_SLEEPABLE)
12d9466d
AN
6371 attr->prog_flags |= BPF_F_SLEEPABLE;
6372
6373 if ((prog->type == BPF_PROG_TYPE_TRACING ||
6374 prog->type == BPF_PROG_TYPE_LSM ||
6375 prog->type == BPF_PROG_TYPE_EXT) && !prog->attach_btf_id) {
6376 int btf_obj_fd = 0, btf_type_id = 0, err;
15ea31fa 6377 const char *attach_name;
12d9466d 6378
15ea31fa
AN
6379 attach_name = strchr(prog->sec_name, '/') + 1;
6380 err = libbpf_find_attach_btf_id(prog, attach_name, &btf_obj_fd, &btf_type_id);
12d9466d
AN
6381 if (err)
6382 return err;
6383
6384 /* cache resolved BTF FD and BTF type ID in the prog */
6385 prog->attach_btf_obj_fd = btf_obj_fd;
6386 prog->attach_btf_id = btf_type_id;
6387
6388 /* but by now libbpf common logic is not utilizing
6389 * prog->atach_btf_obj_fd/prog->attach_btf_id anymore because
6390 * this callback is called after attrs were populated by
6391 * libbpf, so this callback has to update attr explicitly here
6392 */
6393 attr->attach_btf_obj_fd = btf_obj_fd;
6394 attr->attach_btf_id = btf_type_id;
6395 }
6396 return 0;
6397}
6398
55cffde2 6399static int
2993e051 6400load_program(struct bpf_program *prog, struct bpf_insn *insns, int insns_cnt,
f0187f0b 6401 char *license, __u32 kern_version, int *pfd)
55cffde2 6402{
6aef10a4 6403 struct bpf_prog_load_params load_attr = {};
25bbbd7a 6404 struct bpf_object *obj = prog->obj;
1ce6a9fc 6405 char *cp, errmsg[STRERR_BUFSIZE];
8395f320
SF
6406 size_t log_buf_size = 0;
6407 char *log_buf = NULL;
12d9466d 6408 int btf_fd, ret, err;
55cffde2 6409
80b2b5c3
AM
6410 if (prog->type == BPF_PROG_TYPE_UNSPEC) {
6411 /*
6412 * The program type must be set. Most likely we couldn't find a proper
6413 * section definition at load time, and thus we didn't infer the type.
6414 */
6415 pr_warn("prog '%s': missing BPF prog type, check ELF section name '%s'\n",
6416 prog->name, prog->sec_name);
6417 return -EINVAL;
6418 }
6419
fba01a06
AN
6420 if (!insns || !insns_cnt)
6421 return -EINVAL;
6422
2993e051 6423 load_attr.prog_type = prog->type;
12d9466d 6424 load_attr.expected_attach_type = prog->expected_attach_type;
25bbbd7a 6425 if (kernel_supports(obj, FEAT_PROG_NAME))
5b32a23e 6426 load_attr.name = prog->name;
d7be143b 6427 load_attr.insns = insns;
6aef10a4 6428 load_attr.insn_cnt = insns_cnt;
d7be143b 6429 load_attr.license = license;
6aef10a4 6430 load_attr.attach_btf_id = prog->attach_btf_id;
12d9466d
AN
6431 load_attr.attach_prog_fd = prog->attach_prog_fd;
6432 load_attr.attach_btf_obj_fd = prog->attach_btf_obj_fd;
6aef10a4
AN
6433 load_attr.attach_btf_id = prog->attach_btf_id;
6434 load_attr.kern_version = kern_version;
6435 load_attr.prog_ifindex = prog->prog_ifindex;
6436
0f0e55d8 6437 /* specify func_info/line_info only if kernel supports them */
25bbbd7a
AN
6438 btf_fd = bpf_object__btf_fd(obj);
6439 if (btf_fd >= 0 && kernel_supports(obj, FEAT_BTF_FUNC)) {
0f0e55d8
AN
6440 load_attr.prog_btf_fd = btf_fd;
6441 load_attr.func_info = prog->func_info;
6442 load_attr.func_info_rec_size = prog->func_info_rec_size;
6443 load_attr.func_info_cnt = prog->func_info_cnt;
6444 load_attr.line_info = prog->line_info;
6445 load_attr.line_info_rec_size = prog->line_info_rec_size;
6446 load_attr.line_info_cnt = prog->line_info_cnt;
6447 }
da11b417 6448 load_attr.log_level = prog->log_level;
04656198 6449 load_attr.prog_flags = prog->prog_flags;
25bbbd7a 6450 load_attr.fd_array = obj->fd_array;
55cffde2 6451
12d9466d
AN
6452 /* adjust load_attr if sec_def provides custom preload callback */
6453 if (prog->sec_def && prog->sec_def->preload_fn) {
6454 err = prog->sec_def->preload_fn(prog, &load_attr, prog->sec_def->cookie);
6455 if (err < 0) {
6456 pr_warn("prog '%s': failed to prepare load attributes: %d\n",
6457 prog->name, err);
6458 return err;
6459 }
6460 }
6461
25bbbd7a
AN
6462 if (obj->gen_loader) {
6463 bpf_gen__prog_load(obj->gen_loader, &load_attr,
6464 prog - obj->programs);
67234743
AS
6465 *pfd = -1;
6466 return 0;
6467 }
da11b417 6468retry_load:
8395f320
SF
6469 if (log_buf_size) {
6470 log_buf = malloc(log_buf_size);
6471 if (!log_buf)
6472 return -ENOMEM;
6473
6474 *log_buf = 0;
6475 }
55cffde2 6476
6aef10a4
AN
6477 load_attr.log_buf = log_buf;
6478 load_attr.log_buf_sz = log_buf_size;
6479 ret = libbpf__bpf_prog_load(&load_attr);
55cffde2
WN
6480
6481 if (ret >= 0) {
8395f320 6482 if (log_buf && load_attr.log_level)
da11b417 6483 pr_debug("verifier log:\n%s", log_buf);
5d23328d 6484
25bbbd7a
AN
6485 if (obj->has_rodata && kernel_supports(obj, FEAT_PROG_BIND_MAP)) {
6486 struct bpf_map *map;
6487 int i;
6488
6489 for (i = 0; i < obj->nr_maps; i++) {
6490 map = &prog->obj->maps[i];
6491 if (map->libbpf_type != LIBBPF_MAP_RODATA)
6492 continue;
5d23328d 6493
25bbbd7a
AN
6494 if (bpf_prog_bind_map(ret, bpf_map__fd(map), NULL)) {
6495 cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
6496 pr_warn("prog '%s': failed to bind .rodata map: %s\n",
6497 prog->name, cp);
6498 /* Don't fail hard if can't bind rodata. */
6499 }
5d23328d
YZ
6500 }
6501 }
6502
55cffde2
WN
6503 *pfd = ret;
6504 ret = 0;
6505 goto out;
6506 }
6507
8395f320
SF
6508 if (!log_buf || errno == ENOSPC) {
6509 log_buf_size = max((size_t)BPF_LOG_BUF_SIZE,
6510 log_buf_size << 1);
6511
da11b417
AS
6512 free(log_buf);
6513 goto retry_load;
6514 }
ef05afa6 6515 ret = errno ? -errno : -LIBBPF_ERRNO__LOAD;
24d6a808 6516 cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
be18010e 6517 pr_warn("load bpf program failed: %s\n", cp);
dc3a2d25 6518 pr_perm_msg(ret);
55cffde2 6519
6371ca3b
WN
6520 if (log_buf && log_buf[0] != '\0') {
6521 ret = -LIBBPF_ERRNO__VERIFY;
be18010e
KW
6522 pr_warn("-- BEGIN DUMP LOG ---\n");
6523 pr_warn("\n%s\n", log_buf);
6524 pr_warn("-- END LOG --\n");
6aef10a4 6525 } else if (load_attr.insn_cnt >= BPF_MAXINSNS) {
be18010e 6526 pr_warn("Program too large (%zu insns), at most %d insns\n",
6aef10a4 6527 load_attr.insn_cnt, BPF_MAXINSNS);
705fa219 6528 ret = -LIBBPF_ERRNO__PROG2BIG;
4f33ddb4 6529 } else if (load_attr.prog_type != BPF_PROG_TYPE_KPROBE) {
705fa219 6530 /* Wrong program type? */
4f33ddb4 6531 int fd;
705fa219 6532
4f33ddb4
THJ
6533 load_attr.prog_type = BPF_PROG_TYPE_KPROBE;
6534 load_attr.expected_attach_type = 0;
6aef10a4
AN
6535 load_attr.log_buf = NULL;
6536 load_attr.log_buf_sz = 0;
6537 fd = libbpf__bpf_prog_load(&load_attr);
4f33ddb4
THJ
6538 if (fd >= 0) {
6539 close(fd);
6540 ret = -LIBBPF_ERRNO__PROGTYPE;
6541 goto out;
6542 }
55cffde2
WN
6543 }
6544
6545out:
6546 free(log_buf);
6547 return ret;
6548}
6549
67234743
AS
6550static int bpf_program__record_externs(struct bpf_program *prog)
6551{
6552 struct bpf_object *obj = prog->obj;
6553 int i;
6554
6555 for (i = 0; i < prog->nr_reloc; i++) {
6556 struct reloc_desc *relo = &prog->reloc_desc[i];
6557 struct extern_desc *ext = &obj->externs[relo->sym_off];
6558
6559 switch (relo->type) {
6560 case RELO_EXTERN_VAR:
6561 if (ext->type != EXT_KSYM)
6562 continue;
6563 if (!ext->ksym.type_id) {
6564 pr_warn("typeless ksym %s is not supported yet\n",
6565 ext->name);
6566 return -ENOTSUP;
6567 }
18f4fccb
KKD
6568 bpf_gen__record_extern(obj->gen_loader, ext->name, ext->is_weak,
6569 BTF_KIND_VAR, relo->insn_idx);
67234743
AS
6570 break;
6571 case RELO_EXTERN_FUNC:
18f4fccb
KKD
6572 bpf_gen__record_extern(obj->gen_loader, ext->name, ext->is_weak,
6573 BTF_KIND_FUNC, relo->insn_idx);
67234743
AS
6574 break;
6575 default:
6576 continue;
6577 }
6578 }
6579 return 0;
6580}
6581
13acb508 6582int bpf_program__load(struct bpf_program *prog, char *license, __u32 kern_ver)
55cffde2 6583{
91abb4a6 6584 int err = 0, fd, i;
13acb508 6585
d9297581 6586 if (prog->obj->loaded) {
52109584 6587 pr_warn("prog '%s': can't load after object was loaded\n", prog->name);
e9fc3ce9 6588 return libbpf_err(-EINVAL);
d9297581
AN
6589 }
6590
b580563e
WN
6591 if (prog->instances.nr < 0 || !prog->instances.fds) {
6592 if (prog->preprocessor) {
be18010e 6593 pr_warn("Internal error: can't load program '%s'\n",
52109584 6594 prog->name);
e9fc3ce9 6595 return libbpf_err(-LIBBPF_ERRNO__INTERNAL);
b580563e 6596 }
55cffde2 6597
b580563e
WN
6598 prog->instances.fds = malloc(sizeof(int));
6599 if (!prog->instances.fds) {
be18010e 6600 pr_warn("Not enough memory for BPF fds\n");
e9fc3ce9 6601 return libbpf_err(-ENOMEM);
b580563e
WN
6602 }
6603 prog->instances.nr = 1;
6604 prog->instances.fds[0] = -1;
6605 }
6606
6607 if (!prog->preprocessor) {
6608 if (prog->instances.nr != 1) {
52109584
AN
6609 pr_warn("prog '%s': inconsistent nr(%d) != 1\n",
6610 prog->name, prog->instances.nr);
b580563e 6611 }
67234743
AS
6612 if (prog->obj->gen_loader)
6613 bpf_program__record_externs(prog);
2993e051 6614 err = load_program(prog, prog->insns, prog->insns_cnt,
13acb508 6615 license, kern_ver, &fd);
b580563e
WN
6616 if (!err)
6617 prog->instances.fds[0] = fd;
6618 goto out;
6619 }
6620
6621 for (i = 0; i < prog->instances.nr; i++) {
6622 struct bpf_prog_prep_result result;
6623 bpf_program_prep_t preprocessor = prog->preprocessor;
6624
1ad9cbb8 6625 memset(&result, 0, sizeof(result));
b580563e
WN
6626 err = preprocessor(prog, i, prog->insns,
6627 prog->insns_cnt, &result);
6628 if (err) {
be18010e 6629 pr_warn("Preprocessing the %dth instance of program '%s' failed\n",
52109584 6630 i, prog->name);
b580563e
WN
6631 goto out;
6632 }
6633
6634 if (!result.new_insn_ptr || !result.new_insn_cnt) {
6635 pr_debug("Skip loading the %dth instance of program '%s'\n",
52109584 6636 i, prog->name);
b580563e
WN
6637 prog->instances.fds[i] = -1;
6638 if (result.pfd)
6639 *result.pfd = -1;
6640 continue;
6641 }
6642
2993e051 6643 err = load_program(prog, result.new_insn_ptr,
13acb508 6644 result.new_insn_cnt, license, kern_ver, &fd);
b580563e 6645 if (err) {
be18010e 6646 pr_warn("Loading the %dth instance of program '%s' failed\n",
52109584 6647 i, prog->name);
b580563e
WN
6648 goto out;
6649 }
6650
6651 if (result.pfd)
6652 *result.pfd = fd;
6653 prog->instances.fds[i] = fd;
6654 }
6655out:
55cffde2 6656 if (err)
52109584 6657 pr_warn("failed to load program '%s'\n", prog->name);
e9fc3ce9 6658 return libbpf_err(err);
55cffde2
WN
6659}
6660
6661static int
60276f98 6662bpf_object__load_progs(struct bpf_object *obj, int log_level)
55cffde2 6663{
d9297581 6664 struct bpf_program *prog;
55cffde2
WN
6665 size_t i;
6666 int err;
6667
109cea5a
AN
6668 for (i = 0; i < obj->nr_programs; i++) {
6669 prog = &obj->programs[i];
6670 err = bpf_object__sanitize_prog(obj, prog);
6671 if (err)
6672 return err;
6673 }
6674
55cffde2 6675 for (i = 0; i < obj->nr_programs; i++) {
d9297581 6676 prog = &obj->programs[i];
c3c55696 6677 if (prog_is_subprog(obj, prog))
48cca7e4 6678 continue;
d9297581 6679 if (!prog->load) {
9c0f8cbd 6680 pr_debug("prog '%s': skipped loading\n", prog->name);
d9297581
AN
6681 continue;
6682 }
6683 prog->log_level |= log_level;
6684 err = bpf_program__load(prog, obj->license, obj->kern_version);
55cffde2
WN
6685 if (err)
6686 return err;
6687 }
67234743
AS
6688 if (obj->gen_loader)
6689 bpf_object__free_relocs(obj);
55cffde2
WN
6690 return 0;
6691}
6692
25498a19
AN
6693static const struct bpf_sec_def *find_sec_def(const char *sec_name);
6694
91b4d1d1
AN
6695static int bpf_object_init_progs(struct bpf_object *obj, const struct bpf_object_open_opts *opts)
6696{
6697 struct bpf_program *prog;
12d9466d 6698 int err;
91b4d1d1
AN
6699
6700 bpf_object__for_each_program(prog, obj) {
6701 prog->sec_def = find_sec_def(prog->sec_name);
6702 if (!prog->sec_def) {
6703 /* couldn't guess, but user might manually specify */
6704 pr_debug("prog '%s': unrecognized ELF section name '%s'\n",
6705 prog->name, prog->sec_name);
6706 continue;
6707 }
6708
91b4d1d1
AN
6709 bpf_program__set_type(prog, prog->sec_def->prog_type);
6710 bpf_program__set_expected_attach_type(prog, prog->sec_def->expected_attach_type);
6711
91b555d7
AN
6712#pragma GCC diagnostic push
6713#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
91b4d1d1
AN
6714 if (prog->sec_def->prog_type == BPF_PROG_TYPE_TRACING ||
6715 prog->sec_def->prog_type == BPF_PROG_TYPE_EXT)
6716 prog->attach_prog_fd = OPTS_GET(opts, attach_prog_fd, 0);
91b555d7 6717#pragma GCC diagnostic pop
12d9466d
AN
6718
6719 /* sec_def can have custom callback which should be called
6720 * after bpf_program is initialized to adjust its properties
6721 */
6722 if (prog->sec_def->init_fn) {
6723 err = prog->sec_def->init_fn(prog, prog->sec_def->cookie);
6724 if (err < 0) {
6725 pr_warn("prog '%s': failed to initialize: %d\n",
6726 prog->name, err);
6727 return err;
6728 }
6729 }
91b4d1d1
AN
6730 }
6731
6732 return 0;
6733}
6734
1a5e3fb1 6735static struct bpf_object *
5e61f270 6736__bpf_object__open(const char *path, const void *obj_buf, size_t obj_buf_sz,
01af3bf0 6737 const struct bpf_object_open_opts *opts)
1a5e3fb1 6738{
1373ff59 6739 const char *obj_name, *kconfig, *btf_tmp_path;
1a5e3fb1 6740 struct bpf_object *obj;
291ee02b 6741 char tmp_name[64];
6371ca3b 6742 int err;
1a5e3fb1
WN
6743
6744 if (elf_version(EV_CURRENT) == EV_NONE) {
be18010e
KW
6745 pr_warn("failed to init libelf for %s\n",
6746 path ? : "(mem buf)");
6371ca3b 6747 return ERR_PTR(-LIBBPF_ERRNO__LIBELF);
1a5e3fb1
WN
6748 }
6749
291ee02b
AN
6750 if (!OPTS_VALID(opts, bpf_object_open_opts))
6751 return ERR_PTR(-EINVAL);
6752
1aace10f 6753 obj_name = OPTS_GET(opts, object_name, NULL);
291ee02b
AN
6754 if (obj_buf) {
6755 if (!obj_name) {
6756 snprintf(tmp_name, sizeof(tmp_name), "%lx-%lx",
6757 (unsigned long)obj_buf,
6758 (unsigned long)obj_buf_sz);
6759 obj_name = tmp_name;
6760 }
6761 path = obj_name;
6762 pr_debug("loading object '%s' from buffer\n", obj_name);
6763 }
6764
2ce8450e 6765 obj = bpf_object__new(path, obj_buf, obj_buf_sz, obj_name);
6371ca3b
WN
6766 if (IS_ERR(obj))
6767 return obj;
1a5e3fb1 6768
1373ff59
SC
6769 btf_tmp_path = OPTS_GET(opts, btf_custom_path, NULL);
6770 if (btf_tmp_path) {
6771 if (strlen(btf_tmp_path) >= PATH_MAX) {
6772 err = -ENAMETOOLONG;
6773 goto out;
6774 }
6775 obj->btf_custom_path = strdup(btf_tmp_path);
6776 if (!obj->btf_custom_path) {
6777 err = -ENOMEM;
6778 goto out;
6779 }
6780 }
6781
8601fd42
AN
6782 kconfig = OPTS_GET(opts, kconfig, NULL);
6783 if (kconfig) {
6784 obj->kconfig = strdup(kconfig);
18353c87
SC
6785 if (!obj->kconfig) {
6786 err = -ENOMEM;
6787 goto out;
6788 }
166750bc 6789 }
291ee02b 6790
0d13bfce
AN
6791 err = bpf_object__elf_init(obj);
6792 err = err ? : bpf_object__check_endianness(obj);
6793 err = err ? : bpf_object__elf_collect(obj);
166750bc
AN
6794 err = err ? : bpf_object__collect_externs(obj);
6795 err = err ? : bpf_object__finalize_btf(obj);
0d13bfce 6796 err = err ? : bpf_object__init_maps(obj, opts);
91b4d1d1 6797 err = err ? : bpf_object_init_progs(obj, opts);
c3c55696 6798 err = err ? : bpf_object__collect_relos(obj);
0d13bfce
AN
6799 if (err)
6800 goto out;
dd4436bb 6801
91b4d1d1 6802 bpf_object__elf_finish(obj);
dd4436bb 6803
1a5e3fb1
WN
6804 return obj;
6805out:
6806 bpf_object__close(obj);
6371ca3b 6807 return ERR_PTR(err);
1a5e3fb1
WN
6808}
6809
5e61f270
AN
6810static struct bpf_object *
6811__bpf_object__open_xattr(struct bpf_object_open_attr *attr, int flags)
1a5e3fb1 6812{
e00aca65 6813 DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts,
291ee02b
AN
6814 .relaxed_maps = flags & MAPS_RELAX_COMPAT,
6815 );
6816
1a5e3fb1 6817 /* param validation */
07f2d4ea 6818 if (!attr->file)
1a5e3fb1
WN
6819 return NULL;
6820
07f2d4ea 6821 pr_debug("loading %s\n", attr->file);
291ee02b 6822 return __bpf_object__open(attr->file, NULL, 0, &opts);
c034a177
JF
6823}
6824
6825struct bpf_object *bpf_object__open_xattr(struct bpf_object_open_attr *attr)
6826{
e9fc3ce9 6827 return libbpf_ptr(__bpf_object__open_xattr(attr, 0));
07f2d4ea
JK
6828}
6829
6830struct bpf_object *bpf_object__open(const char *path)
6831{
6832 struct bpf_object_open_attr attr = {
6833 .file = path,
6834 .prog_type = BPF_PROG_TYPE_UNSPEC,
6835 };
1a5e3fb1 6836
e9fc3ce9 6837 return libbpf_ptr(__bpf_object__open_xattr(&attr, 0));
6c956392
WN
6838}
6839
2ce8450e 6840struct bpf_object *
01af3bf0 6841bpf_object__open_file(const char *path, const struct bpf_object_open_opts *opts)
2ce8450e 6842{
2ce8450e 6843 if (!path)
e9fc3ce9 6844 return libbpf_err_ptr(-EINVAL);
2ce8450e
AN
6845
6846 pr_debug("loading %s\n", path);
6847
e9fc3ce9 6848 return libbpf_ptr(__bpf_object__open(path, NULL, 0, opts));
2ce8450e
AN
6849}
6850
6851struct bpf_object *
6852bpf_object__open_mem(const void *obj_buf, size_t obj_buf_sz,
01af3bf0 6853 const struct bpf_object_open_opts *opts)
6c956392 6854{
2ce8450e 6855 if (!obj_buf || obj_buf_sz == 0)
e9fc3ce9 6856 return libbpf_err_ptr(-EINVAL);
6c956392 6857
e9fc3ce9 6858 return libbpf_ptr(__bpf_object__open(NULL, obj_buf, obj_buf_sz, opts));
2ce8450e
AN
6859}
6860
6861struct bpf_object *
6862bpf_object__open_buffer(const void *obj_buf, size_t obj_buf_sz,
6863 const char *name)
6864{
e00aca65 6865 DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts,
2ce8450e
AN
6866 .object_name = name,
6867 /* wrong default, but backwards-compatible */
6868 .relaxed_maps = true,
6869 );
6870
6871 /* returning NULL is wrong, but backwards-compatible */
6872 if (!obj_buf || obj_buf_sz == 0)
e9fc3ce9 6873 return errno = EINVAL, NULL;
6c956392 6874
e9fc3ce9 6875 return libbpf_ptr(__bpf_object__open(NULL, obj_buf, obj_buf_sz, &opts));
1a5e3fb1
WN
6876}
6877
4a404a7e 6878static int bpf_object_unload(struct bpf_object *obj)
52d3352e
WN
6879{
6880 size_t i;
6881
6882 if (!obj)
e9fc3ce9 6883 return libbpf_err(-EINVAL);
52d3352e 6884
590a0088 6885 for (i = 0; i < obj->nr_maps; i++) {
9d759a9b 6886 zclose(obj->maps[i].fd);
590a0088
MKL
6887 if (obj->maps[i].st_ops)
6888 zfree(&obj->maps[i].st_ops->kern_vdata);
6889 }
52d3352e 6890
55cffde2
WN
6891 for (i = 0; i < obj->nr_programs; i++)
6892 bpf_program__unload(&obj->programs[i]);
6893
52d3352e
WN
6894 return 0;
6895}
6896
4a404a7e
HC
6897int bpf_object__unload(struct bpf_object *obj) __attribute__((alias("bpf_object_unload")));
6898
0d13bfce
AN
6899static int bpf_object__sanitize_maps(struct bpf_object *obj)
6900{
6901 struct bpf_map *m;
6902
6903 bpf_object__for_each_map(m, obj) {
6904 if (!bpf_map__is_internal(m))
6905 continue;
9ca1f56a 6906 if (!kernel_supports(obj, FEAT_GLOBAL_DATA)) {
0d13bfce
AN
6907 pr_warn("kernel doesn't support global data\n");
6908 return -ENOTSUP;
6909 }
9ca1f56a 6910 if (!kernel_supports(obj, FEAT_ARRAY_MMAP))
0d13bfce
AN
6911 m->def.map_flags ^= BPF_F_MMAPABLE;
6912 }
6913
6914 return 0;
6915}
6916
1c0c7074
AN
6917static int bpf_object__read_kallsyms_file(struct bpf_object *obj)
6918{
6919 char sym_type, sym_name[500];
6920 unsigned long long sym_addr;
5bd022ec 6921 const struct btf_type *t;
1c0c7074
AN
6922 struct extern_desc *ext;
6923 int ret, err = 0;
6924 FILE *f;
6925
6926 f = fopen("/proc/kallsyms", "r");
6927 if (!f) {
6928 err = -errno;
6929 pr_warn("failed to open /proc/kallsyms: %d\n", err);
6930 return err;
6931 }
6932
6933 while (true) {
6934 ret = fscanf(f, "%llx %c %499s%*[^\n]\n",
6935 &sym_addr, &sym_type, sym_name);
6936 if (ret == EOF && feof(f))
6937 break;
6938 if (ret != 3) {
135c783f 6939 pr_warn("failed to read kallsyms entry: %d\n", ret);
1c0c7074
AN
6940 err = -EINVAL;
6941 goto out;
6942 }
6943
6944 ext = find_extern_by_name(obj, sym_name);
6945 if (!ext || ext->type != EXT_KSYM)
6946 continue;
6947
5bd022ec
MKL
6948 t = btf__type_by_id(obj->btf, ext->btf_id);
6949 if (!btf_is_var(t))
6950 continue;
6951
1c0c7074
AN
6952 if (ext->is_set && ext->ksym.addr != sym_addr) {
6953 pr_warn("extern (ksym) '%s' resolution is ambiguous: 0x%llx or 0x%llx\n",
6954 sym_name, ext->ksym.addr, sym_addr);
6955 err = -EINVAL;
6956 goto out;
6957 }
6958 if (!ext->is_set) {
6959 ext->is_set = true;
6960 ext->ksym.addr = sym_addr;
6961 pr_debug("extern (ksym) %s=0x%llx\n", sym_name, sym_addr);
6962 }
6963 }
6964
6965out:
6966 fclose(f);
6967 return err;
6968}
6969
774e132e
MKL
6970static int find_ksym_btf_id(struct bpf_object *obj, const char *ksym_name,
6971 __u16 kind, struct btf **res_btf,
9dbe6015 6972 struct module_btf **res_mod_btf)
d370bbe1 6973{
9dbe6015 6974 struct module_btf *mod_btf;
284d2587 6975 struct btf *btf;
9dbe6015 6976 int i, id, err;
d370bbe1 6977
933d1aa3 6978 btf = obj->btf_vmlinux;
9dbe6015 6979 mod_btf = NULL;
774e132e
MKL
6980 id = btf__find_by_name_kind(btf, ksym_name, kind);
6981
933d1aa3
MKL
6982 if (id == -ENOENT) {
6983 err = load_module_btfs(obj);
6984 if (err)
6985 return err;
d370bbe1 6986
933d1aa3 6987 for (i = 0; i < obj->btf_module_cnt; i++) {
9dbe6015
KKD
6988 /* we assume module_btf's BTF FD is always >0 */
6989 mod_btf = &obj->btf_modules[i];
6990 btf = mod_btf->btf;
6991 id = btf__find_by_name_kind_own(btf, ksym_name, kind);
933d1aa3
MKL
6992 if (id != -ENOENT)
6993 break;
6994 }
6995 }
2211c825 6996 if (id <= 0)
933d1aa3 6997 return -ESRCH;
d370bbe1 6998
774e132e 6999 *res_btf = btf;
9dbe6015 7000 *res_mod_btf = mod_btf;
774e132e
MKL
7001 return id;
7002}
7003
7004static int bpf_object__resolve_ksym_var_btf_id(struct bpf_object *obj,
7005 struct extern_desc *ext)
7006{
7007 const struct btf_type *targ_var, *targ_type;
7008 __u32 targ_type_id, local_type_id;
9dbe6015 7009 struct module_btf *mod_btf = NULL;
774e132e 7010 const char *targ_var_name;
774e132e 7011 struct btf *btf = NULL;
9dbe6015 7012 int id, err;
774e132e 7013
9dbe6015 7014 id = find_ksym_btf_id(obj, ext->name, BTF_KIND_VAR, &btf, &mod_btf);
466b2e13
KKD
7015 if (id < 0) {
7016 if (id == -ESRCH && ext->is_weak)
7017 return 0;
2211c825
HL
7018 pr_warn("extern (var ksym) '%s': not found in kernel BTF\n",
7019 ext->name);
774e132e 7020 return id;
2211c825 7021 }
774e132e 7022
933d1aa3
MKL
7023 /* find local type_id */
7024 local_type_id = ext->ksym.type_id;
284d2587 7025
933d1aa3
MKL
7026 /* find target type_id */
7027 targ_var = btf__type_by_id(btf, id);
7028 targ_var_name = btf__name_by_offset(btf, targ_var->name_off);
7029 targ_type = skip_mods_and_typedefs(btf, targ_var->type, &targ_type_id);
d370bbe1 7030
933d1aa3
MKL
7031 err = bpf_core_types_are_compat(obj->btf, local_type_id,
7032 btf, targ_type_id);
7033 if (err <= 0) {
7034 const struct btf_type *local_type;
7035 const char *targ_name, *local_name;
d370bbe1 7036
933d1aa3
MKL
7037 local_type = btf__type_by_id(obj->btf, local_type_id);
7038 local_name = btf__name_by_offset(obj->btf, local_type->name_off);
7039 targ_name = btf__name_by_offset(btf, targ_type->name_off);
d370bbe1 7040
933d1aa3
MKL
7041 pr_warn("extern (var ksym) '%s': incompatible types, expected [%d] %s %s, but kernel has [%d] %s %s\n",
7042 ext->name, local_type_id,
7043 btf_kind_str(local_type), local_name, targ_type_id,
7044 btf_kind_str(targ_type), targ_name);
7045 return -EINVAL;
7046 }
d370bbe1 7047
933d1aa3 7048 ext->is_set = true;
9dbe6015 7049 ext->ksym.kernel_btf_obj_fd = mod_btf ? mod_btf->fd : 0;
933d1aa3
MKL
7050 ext->ksym.kernel_btf_id = id;
7051 pr_debug("extern (var ksym) '%s': resolved to [%d] %s %s\n",
7052 ext->name, id, btf_kind_str(targ_var), targ_var_name);
d370bbe1 7053
933d1aa3
MKL
7054 return 0;
7055}
d370bbe1 7056
5bd022ec
MKL
7057static int bpf_object__resolve_ksym_func_btf_id(struct bpf_object *obj,
7058 struct extern_desc *ext)
7059{
7060 int local_func_proto_id, kfunc_proto_id, kfunc_id;
9dbe6015 7061 struct module_btf *mod_btf = NULL;
5bd022ec
MKL
7062 const struct btf_type *kern_func;
7063 struct btf *kern_btf = NULL;
9dbe6015 7064 int ret;
5bd022ec
MKL
7065
7066 local_func_proto_id = ext->ksym.type_id;
7067
9dbe6015 7068 kfunc_id = find_ksym_btf_id(obj, ext->name, BTF_KIND_FUNC, &kern_btf, &mod_btf);
5bd022ec 7069 if (kfunc_id < 0) {
466b2e13
KKD
7070 if (kfunc_id == -ESRCH && ext->is_weak)
7071 return 0;
7072 pr_warn("extern (func ksym) '%s': not found in kernel or module BTFs\n",
5bd022ec
MKL
7073 ext->name);
7074 return kfunc_id;
7075 }
7076
5bd022ec
MKL
7077 kern_func = btf__type_by_id(kern_btf, kfunc_id);
7078 kfunc_proto_id = kern_func->type;
7079
7080 ret = bpf_core_types_are_compat(obj->btf, local_func_proto_id,
7081 kern_btf, kfunc_proto_id);
7082 if (ret <= 0) {
7083 pr_warn("extern (func ksym) '%s': func_proto [%d] incompatible with kernel [%d]\n",
7084 ext->name, local_func_proto_id, kfunc_proto_id);
7085 return -EINVAL;
7086 }
7087
9dbe6015
KKD
7088 /* set index for module BTF fd in fd_array, if unset */
7089 if (mod_btf && !mod_btf->fd_array_idx) {
7090 /* insn->off is s16 */
7091 if (obj->fd_array_cnt == INT16_MAX) {
7092 pr_warn("extern (func ksym) '%s': module BTF fd index %d too big to fit in bpf_insn offset\n",
7093 ext->name, mod_btf->fd_array_idx);
7094 return -E2BIG;
7095 }
7096 /* Cannot use index 0 for module BTF fd */
7097 if (!obj->fd_array_cnt)
7098 obj->fd_array_cnt = 1;
7099
7100 ret = libbpf_ensure_mem((void **)&obj->fd_array, &obj->fd_array_cap, sizeof(int),
7101 obj->fd_array_cnt + 1);
7102 if (ret)
7103 return ret;
7104 mod_btf->fd_array_idx = obj->fd_array_cnt;
7105 /* we assume module BTF FD is always >0 */
7106 obj->fd_array[obj->fd_array_cnt++] = mod_btf->fd;
7107 }
7108
5bd022ec 7109 ext->is_set = true;
5bd022ec 7110 ext->ksym.kernel_btf_id = kfunc_id;
9dbe6015 7111 ext->ksym.btf_fd_idx = mod_btf ? mod_btf->fd_array_idx : 0;
5bd022ec
MKL
7112 pr_debug("extern (func ksym) '%s': resolved to kernel [%d]\n",
7113 ext->name, kfunc_id);
7114
7115 return 0;
7116}
7117
933d1aa3
MKL
7118static int bpf_object__resolve_ksyms_btf_id(struct bpf_object *obj)
7119{
5bd022ec 7120 const struct btf_type *t;
933d1aa3
MKL
7121 struct extern_desc *ext;
7122 int i, err;
7123
7124 for (i = 0; i < obj->nr_extern; i++) {
7125 ext = &obj->externs[i];
7126 if (ext->type != EXT_KSYM || !ext->ksym.type_id)
7127 continue;
7128
67234743
AS
7129 if (obj->gen_loader) {
7130 ext->is_set = true;
7131 ext->ksym.kernel_btf_obj_fd = 0;
7132 ext->ksym.kernel_btf_id = 0;
7133 continue;
7134 }
5bd022ec
MKL
7135 t = btf__type_by_id(obj->btf, ext->btf_id);
7136 if (btf_is_var(t))
7137 err = bpf_object__resolve_ksym_var_btf_id(obj, ext);
7138 else
7139 err = bpf_object__resolve_ksym_func_btf_id(obj, ext);
933d1aa3
MKL
7140 if (err)
7141 return err;
d370bbe1
HL
7142 }
7143 return 0;
7144}
7145
166750bc 7146static int bpf_object__resolve_externs(struct bpf_object *obj,
8601fd42 7147 const char *extra_kconfig)
166750bc 7148{
1c0c7074 7149 bool need_config = false, need_kallsyms = false;
d370bbe1 7150 bool need_vmlinux_btf = false;
166750bc 7151 struct extern_desc *ext;
2e33efe3 7152 void *kcfg_data = NULL;
166750bc 7153 int err, i;
166750bc
AN
7154
7155 if (obj->nr_extern == 0)
7156 return 0;
7157
2e33efe3
AN
7158 if (obj->kconfig_map_idx >= 0)
7159 kcfg_data = obj->maps[obj->kconfig_map_idx].mmaped;
166750bc
AN
7160
7161 for (i = 0; i < obj->nr_extern; i++) {
7162 ext = &obj->externs[i];
7163
2e33efe3
AN
7164 if (ext->type == EXT_KCFG &&
7165 strcmp(ext->name, "LINUX_KERNEL_VERSION") == 0) {
7166 void *ext_val = kcfg_data + ext->kcfg.data_off;
166750bc
AN
7167 __u32 kver = get_kernel_version();
7168
7169 if (!kver) {
7170 pr_warn("failed to get kernel version\n");
7171 return -EINVAL;
7172 }
2e33efe3 7173 err = set_kcfg_value_num(ext, ext_val, kver);
166750bc
AN
7174 if (err)
7175 return err;
2e33efe3 7176 pr_debug("extern (kcfg) %s=0x%x\n", ext->name, kver);
13d35a0c 7177 } else if (ext->type == EXT_KCFG && str_has_pfx(ext->name, "CONFIG_")) {
166750bc 7178 need_config = true;
1c0c7074 7179 } else if (ext->type == EXT_KSYM) {
d370bbe1
HL
7180 if (ext->ksym.type_id)
7181 need_vmlinux_btf = true;
7182 else
7183 need_kallsyms = true;
166750bc
AN
7184 } else {
7185 pr_warn("unrecognized extern '%s'\n", ext->name);
7186 return -EINVAL;
7187 }
7188 }
8601fd42 7189 if (need_config && extra_kconfig) {
2e33efe3 7190 err = bpf_object__read_kconfig_mem(obj, extra_kconfig, kcfg_data);
8601fd42
AN
7191 if (err)
7192 return -EINVAL;
7193 need_config = false;
7194 for (i = 0; i < obj->nr_extern; i++) {
7195 ext = &obj->externs[i];
2e33efe3 7196 if (ext->type == EXT_KCFG && !ext->is_set) {
8601fd42
AN
7197 need_config = true;
7198 break;
7199 }
7200 }
7201 }
166750bc 7202 if (need_config) {
2e33efe3 7203 err = bpf_object__read_kconfig_file(obj, kcfg_data);
166750bc
AN
7204 if (err)
7205 return -EINVAL;
7206 }
1c0c7074
AN
7207 if (need_kallsyms) {
7208 err = bpf_object__read_kallsyms_file(obj);
7209 if (err)
7210 return -EINVAL;
7211 }
d370bbe1
HL
7212 if (need_vmlinux_btf) {
7213 err = bpf_object__resolve_ksyms_btf_id(obj);
7214 if (err)
7215 return -EINVAL;
7216 }
166750bc
AN
7217 for (i = 0; i < obj->nr_extern; i++) {
7218 ext = &obj->externs[i];
7219
7220 if (!ext->is_set && !ext->is_weak) {
7221 pr_warn("extern %s (strong) not resolved\n", ext->name);
7222 return -ESRCH;
7223 } else if (!ext->is_set) {
7224 pr_debug("extern %s (weak) not resolved, defaulting to zero\n",
7225 ext->name);
7226 }
7227 }
7228
7229 return 0;
7230}
7231
60276f98 7232int bpf_object__load_xattr(struct bpf_object_load_attr *attr)
52d3352e 7233{
60276f98 7234 struct bpf_object *obj;
ec6d5f47 7235 int err, i;
6371ca3b 7236
60276f98 7237 if (!attr)
e9fc3ce9 7238 return libbpf_err(-EINVAL);
60276f98 7239 obj = attr->obj;
52d3352e 7240 if (!obj)
e9fc3ce9 7241 return libbpf_err(-EINVAL);
52d3352e
WN
7242
7243 if (obj->loaded) {
d9297581 7244 pr_warn("object '%s': load can't be attempted twice\n", obj->name);
e9fc3ce9 7245 return libbpf_err(-EINVAL);
52d3352e
WN
7246 }
7247
67234743
AS
7248 if (obj->gen_loader)
7249 bpf_gen__init(obj->gen_loader, attr->log_level);
7250
fd9eef1a 7251 err = bpf_object__probe_loading(obj);
fe62de31 7252 err = err ? : bpf_object__load_vmlinux_btf(obj, false);
8601fd42 7253 err = err ? : bpf_object__resolve_externs(obj, obj->kconfig);
0d13bfce
AN
7254 err = err ? : bpf_object__sanitize_and_load_btf(obj);
7255 err = err ? : bpf_object__sanitize_maps(obj);
590a0088 7256 err = err ? : bpf_object__init_kern_struct_ops_maps(obj);
0d13bfce 7257 err = err ? : bpf_object__create_maps(obj);
1373ff59 7258 err = err ? : bpf_object__relocate(obj, obj->btf_custom_path ? : attr->target_btf_path);
0d13bfce 7259 err = err ? : bpf_object__load_progs(obj, attr->log_level);
a6ed02ca 7260
67234743
AS
7261 if (obj->gen_loader) {
7262 /* reset FDs */
7263 btf__set_fd(obj->btf, -1);
7264 for (i = 0; i < obj->nr_maps; i++)
7265 obj->maps[i].fd = -1;
7266 if (!err)
7267 err = bpf_gen__finish(obj->gen_loader);
7268 }
7269
9dbe6015
KKD
7270 /* clean up fd_array */
7271 zfree(&obj->fd_array);
7272
4f33a53d
AN
7273 /* clean up module BTFs */
7274 for (i = 0; i < obj->btf_module_cnt; i++) {
91abb4a6 7275 close(obj->btf_modules[i].fd);
4f33a53d
AN
7276 btf__free(obj->btf_modules[i].btf);
7277 free(obj->btf_modules[i].name);
7278 }
7279 free(obj->btf_modules);
7280
7281 /* clean up vmlinux BTF */
a6ed02ca
KS
7282 btf__free(obj->btf_vmlinux);
7283 obj->btf_vmlinux = NULL;
7284
d9297581
AN
7285 obj->loaded = true; /* doesn't matter if successfully or not */
7286
0d13bfce
AN
7287 if (err)
7288 goto out;
52d3352e
WN
7289
7290 return 0;
7291out:
ec6d5f47
THJ
7292 /* unpin any maps that were auto-pinned during load */
7293 for (i = 0; i < obj->nr_maps; i++)
7294 if (obj->maps[i].pinned && !obj->maps[i].reused)
7295 bpf_map__unpin(&obj->maps[i], NULL);
7296
4a404a7e 7297 bpf_object_unload(obj);
be18010e 7298 pr_warn("failed to load object '%s'\n", obj->path);
e9fc3ce9 7299 return libbpf_err(err);
52d3352e
WN
7300}
7301
60276f98
QM
7302int bpf_object__load(struct bpf_object *obj)
7303{
7304 struct bpf_object_load_attr attr = {
7305 .obj = obj,
7306 };
7307
7308 return bpf_object__load_xattr(&attr);
7309}
7310
196f8487
THJ
7311static int make_parent_dir(const char *path)
7312{
7313 char *cp, errmsg[STRERR_BUFSIZE];
7314 char *dname, *dir;
7315 int err = 0;
7316
7317 dname = strdup(path);
7318 if (dname == NULL)
7319 return -ENOMEM;
7320
7321 dir = dirname(dname);
7322 if (mkdir(dir, 0700) && errno != EEXIST)
7323 err = -errno;
7324
7325 free(dname);
7326 if (err) {
7327 cp = libbpf_strerror_r(-err, errmsg, sizeof(errmsg));
7328 pr_warn("failed to mkdir %s: %s\n", path, cp);
7329 }
7330 return err;
7331}
7332
f367540c
JS
7333static int check_path(const char *path)
7334{
1ce6a9fc 7335 char *cp, errmsg[STRERR_BUFSIZE];
f367540c
JS
7336 struct statfs st_fs;
7337 char *dname, *dir;
7338 int err = 0;
7339
7340 if (path == NULL)
7341 return -EINVAL;
7342
7343 dname = strdup(path);
7344 if (dname == NULL)
7345 return -ENOMEM;
7346
7347 dir = dirname(dname);
7348 if (statfs(dir, &st_fs)) {
24d6a808 7349 cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
be18010e 7350 pr_warn("failed to statfs %s: %s\n", dir, cp);
f367540c
JS
7351 err = -errno;
7352 }
7353 free(dname);
7354
7355 if (!err && st_fs.f_type != BPF_FS_MAGIC) {
be18010e 7356 pr_warn("specified path %s is not on BPF FS\n", path);
f367540c
JS
7357 err = -EINVAL;
7358 }
7359
7360 return err;
7361}
7362
e21d585c 7363static int bpf_program_pin_instance(struct bpf_program *prog, const char *path, int instance)
f367540c 7364{
1ce6a9fc 7365 char *cp, errmsg[STRERR_BUFSIZE];
f367540c
JS
7366 int err;
7367
196f8487
THJ
7368 err = make_parent_dir(path);
7369 if (err)
e9fc3ce9 7370 return libbpf_err(err);
196f8487 7371
f367540c
JS
7372 err = check_path(path);
7373 if (err)
e9fc3ce9 7374 return libbpf_err(err);
f367540c
JS
7375
7376 if (prog == NULL) {
be18010e 7377 pr_warn("invalid program pointer\n");
e9fc3ce9 7378 return libbpf_err(-EINVAL);
f367540c
JS
7379 }
7380
7381 if (instance < 0 || instance >= prog->instances.nr) {
be18010e 7382 pr_warn("invalid prog instance %d of prog %s (max %d)\n",
52109584 7383 instance, prog->name, prog->instances.nr);
e9fc3ce9 7384 return libbpf_err(-EINVAL);
f367540c
JS
7385 }
7386
7387 if (bpf_obj_pin(prog->instances.fds[instance], path)) {
23ab656b
THJ
7388 err = -errno;
7389 cp = libbpf_strerror_r(err, errmsg, sizeof(errmsg));
be18010e 7390 pr_warn("failed to pin program: %s\n", cp);
e9fc3ce9 7391 return libbpf_err(err);
f367540c
JS
7392 }
7393 pr_debug("pinned program '%s'\n", path);
7394
7395 return 0;
7396}
7397
e21d585c 7398static int bpf_program_unpin_instance(struct bpf_program *prog, const char *path, int instance)
0c19a9fb
SF
7399{
7400 int err;
7401
7402 err = check_path(path);
7403 if (err)
e9fc3ce9 7404 return libbpf_err(err);
0c19a9fb
SF
7405
7406 if (prog == NULL) {
be18010e 7407 pr_warn("invalid program pointer\n");
e9fc3ce9 7408 return libbpf_err(-EINVAL);
0c19a9fb
SF
7409 }
7410
7411 if (instance < 0 || instance >= prog->instances.nr) {
be18010e 7412 pr_warn("invalid prog instance %d of prog %s (max %d)\n",
52109584 7413 instance, prog->name, prog->instances.nr);
e9fc3ce9 7414 return libbpf_err(-EINVAL);
0c19a9fb
SF
7415 }
7416
7417 err = unlink(path);
7418 if (err != 0)
e9fc3ce9
AN
7419 return libbpf_err(-errno);
7420
0c19a9fb
SF
7421 pr_debug("unpinned program '%s'\n", path);
7422
7423 return 0;
7424}
7425
e21d585c
AN
7426__attribute__((alias("bpf_program_pin_instance")))
7427int bpf_object__pin_instance(struct bpf_program *prog, const char *path, int instance);
7428
7429__attribute__((alias("bpf_program_unpin_instance")))
7430int bpf_program__unpin_instance(struct bpf_program *prog, const char *path, int instance);
7431
f367540c
JS
7432int bpf_program__pin(struct bpf_program *prog, const char *path)
7433{
7434 int i, err;
7435
196f8487
THJ
7436 err = make_parent_dir(path);
7437 if (err)
e9fc3ce9 7438 return libbpf_err(err);
196f8487 7439
f367540c
JS
7440 err = check_path(path);
7441 if (err)
e9fc3ce9 7442 return libbpf_err(err);
f367540c
JS
7443
7444 if (prog == NULL) {
be18010e 7445 pr_warn("invalid program pointer\n");
e9fc3ce9 7446 return libbpf_err(-EINVAL);
f367540c
JS
7447 }
7448
7449 if (prog->instances.nr <= 0) {
52109584 7450 pr_warn("no instances of prog %s to pin\n", prog->name);
e9fc3ce9 7451 return libbpf_err(-EINVAL);
f367540c
JS
7452 }
7453
fd734c5c
SF
7454 if (prog->instances.nr == 1) {
7455 /* don't create subdirs when pinning single instance */
e21d585c 7456 return bpf_program_pin_instance(prog, path, 0);
fd734c5c
SF
7457 }
7458
0c19a9fb
SF
7459 for (i = 0; i < prog->instances.nr; i++) {
7460 char buf[PATH_MAX];
7461 int len;
7462
7463 len = snprintf(buf, PATH_MAX, "%s/%d", path, i);
7464 if (len < 0) {
7465 err = -EINVAL;
7466 goto err_unpin;
7467 } else if (len >= PATH_MAX) {
7468 err = -ENAMETOOLONG;
7469 goto err_unpin;
7470 }
7471
e21d585c 7472 err = bpf_program_pin_instance(prog, buf, i);
0c19a9fb
SF
7473 if (err)
7474 goto err_unpin;
7475 }
7476
7477 return 0;
7478
7479err_unpin:
7480 for (i = i - 1; i >= 0; i--) {
7481 char buf[PATH_MAX];
7482 int len;
7483
7484 len = snprintf(buf, PATH_MAX, "%s/%d", path, i);
7485 if (len < 0)
7486 continue;
7487 else if (len >= PATH_MAX)
7488 continue;
7489
e21d585c 7490 bpf_program_unpin_instance(prog, buf, i);
0c19a9fb
SF
7491 }
7492
7493 rmdir(path);
7494
e9fc3ce9 7495 return libbpf_err(err);
0c19a9fb
SF
7496}
7497
7498int bpf_program__unpin(struct bpf_program *prog, const char *path)
7499{
7500 int i, err;
7501
7502 err = check_path(path);
7503 if (err)
e9fc3ce9 7504 return libbpf_err(err);
0c19a9fb
SF
7505
7506 if (prog == NULL) {
be18010e 7507 pr_warn("invalid program pointer\n");
e9fc3ce9 7508 return libbpf_err(-EINVAL);
0c19a9fb
SF
7509 }
7510
7511 if (prog->instances.nr <= 0) {
52109584 7512 pr_warn("no instances of prog %s to pin\n", prog->name);
e9fc3ce9 7513 return libbpf_err(-EINVAL);
fd734c5c
SF
7514 }
7515
7516 if (prog->instances.nr == 1) {
7517 /* don't create subdirs when pinning single instance */
e21d585c 7518 return bpf_program_unpin_instance(prog, path, 0);
0c19a9fb
SF
7519 }
7520
f367540c
JS
7521 for (i = 0; i < prog->instances.nr; i++) {
7522 char buf[PATH_MAX];
7523 int len;
7524
7525 len = snprintf(buf, PATH_MAX, "%s/%d", path, i);
7526 if (len < 0)
e9fc3ce9 7527 return libbpf_err(-EINVAL);
f367540c 7528 else if (len >= PATH_MAX)
e9fc3ce9 7529 return libbpf_err(-ENAMETOOLONG);
f367540c 7530
e21d585c 7531 err = bpf_program_unpin_instance(prog, buf, i);
f367540c
JS
7532 if (err)
7533 return err;
7534 }
7535
0c19a9fb
SF
7536 err = rmdir(path);
7537 if (err)
e9fc3ce9 7538 return libbpf_err(-errno);
0c19a9fb 7539
f367540c
JS
7540 return 0;
7541}
7542
b6989f35
JS
7543int bpf_map__pin(struct bpf_map *map, const char *path)
7544{
1ce6a9fc 7545 char *cp, errmsg[STRERR_BUFSIZE];
b6989f35
JS
7546 int err;
7547
b6989f35 7548 if (map == NULL) {
be18010e 7549 pr_warn("invalid map pointer\n");
e9fc3ce9 7550 return libbpf_err(-EINVAL);
b6989f35
JS
7551 }
7552
4580b25f
THJ
7553 if (map->pin_path) {
7554 if (path && strcmp(path, map->pin_path)) {
7555 pr_warn("map '%s' already has pin path '%s' different from '%s'\n",
7556 bpf_map__name(map), map->pin_path, path);
e9fc3ce9 7557 return libbpf_err(-EINVAL);
4580b25f
THJ
7558 } else if (map->pinned) {
7559 pr_debug("map '%s' already pinned at '%s'; not re-pinning\n",
7560 bpf_map__name(map), map->pin_path);
7561 return 0;
7562 }
7563 } else {
7564 if (!path) {
7565 pr_warn("missing a path to pin map '%s' at\n",
7566 bpf_map__name(map));
e9fc3ce9 7567 return libbpf_err(-EINVAL);
4580b25f
THJ
7568 } else if (map->pinned) {
7569 pr_warn("map '%s' already pinned\n", bpf_map__name(map));
e9fc3ce9 7570 return libbpf_err(-EEXIST);
4580b25f
THJ
7571 }
7572
7573 map->pin_path = strdup(path);
7574 if (!map->pin_path) {
7575 err = -errno;
7576 goto out_err;
7577 }
b6989f35
JS
7578 }
7579
196f8487
THJ
7580 err = make_parent_dir(map->pin_path);
7581 if (err)
e9fc3ce9 7582 return libbpf_err(err);
196f8487 7583
4580b25f
THJ
7584 err = check_path(map->pin_path);
7585 if (err)
e9fc3ce9 7586 return libbpf_err(err);
4580b25f
THJ
7587
7588 if (bpf_obj_pin(map->fd, map->pin_path)) {
7589 err = -errno;
7590 goto out_err;
7591 }
7592
7593 map->pinned = true;
7594 pr_debug("pinned map '%s'\n", map->pin_path);
0c19a9fb 7595
b6989f35 7596 return 0;
4580b25f
THJ
7597
7598out_err:
7599 cp = libbpf_strerror_r(-err, errmsg, sizeof(errmsg));
7600 pr_warn("failed to pin map: %s\n", cp);
e9fc3ce9 7601 return libbpf_err(err);
b6989f35
JS
7602}
7603
0c19a9fb
SF
7604int bpf_map__unpin(struct bpf_map *map, const char *path)
7605{
7606 int err;
7607
0c19a9fb 7608 if (map == NULL) {
be18010e 7609 pr_warn("invalid map pointer\n");
e9fc3ce9 7610 return libbpf_err(-EINVAL);
0c19a9fb
SF
7611 }
7612
4580b25f
THJ
7613 if (map->pin_path) {
7614 if (path && strcmp(path, map->pin_path)) {
7615 pr_warn("map '%s' already has pin path '%s' different from '%s'\n",
7616 bpf_map__name(map), map->pin_path, path);
e9fc3ce9 7617 return libbpf_err(-EINVAL);
4580b25f
THJ
7618 }
7619 path = map->pin_path;
7620 } else if (!path) {
7621 pr_warn("no path to unpin map '%s' from\n",
7622 bpf_map__name(map));
e9fc3ce9 7623 return libbpf_err(-EINVAL);
4580b25f
THJ
7624 }
7625
7626 err = check_path(path);
7627 if (err)
e9fc3ce9 7628 return libbpf_err(err);
4580b25f 7629
0c19a9fb
SF
7630 err = unlink(path);
7631 if (err != 0)
e9fc3ce9 7632 return libbpf_err(-errno);
4580b25f
THJ
7633
7634 map->pinned = false;
7635 pr_debug("unpinned map '%s' from '%s'\n", bpf_map__name(map), path);
0c19a9fb
SF
7636
7637 return 0;
7638}
7639
4580b25f
THJ
7640int bpf_map__set_pin_path(struct bpf_map *map, const char *path)
7641{
7642 char *new = NULL;
7643
7644 if (path) {
7645 new = strdup(path);
7646 if (!new)
e9fc3ce9 7647 return libbpf_err(-errno);
4580b25f
THJ
7648 }
7649
7650 free(map->pin_path);
7651 map->pin_path = new;
7652 return 0;
7653}
7654
7655const char *bpf_map__get_pin_path(const struct bpf_map *map)
7656{
7657 return map->pin_path;
7658}
7659
e244d34d
EL
7660const char *bpf_map__pin_path(const struct bpf_map *map)
7661{
7662 return map->pin_path;
7663}
7664
4580b25f
THJ
7665bool bpf_map__is_pinned(const struct bpf_map *map)
7666{
7667 return map->pinned;
7668}
7669
9cf309c5
THJ
7670static void sanitize_pin_path(char *s)
7671{
7672 /* bpffs disallows periods in path names */
7673 while (*s) {
7674 if (*s == '.')
7675 *s = '_';
7676 s++;
7677 }
7678}
7679
0c19a9fb 7680int bpf_object__pin_maps(struct bpf_object *obj, const char *path)
d5148d85 7681{
d5148d85
JS
7682 struct bpf_map *map;
7683 int err;
7684
7685 if (!obj)
e9fc3ce9 7686 return libbpf_err(-ENOENT);
d5148d85
JS
7687
7688 if (!obj->loaded) {
be18010e 7689 pr_warn("object not yet loaded; load it first\n");
e9fc3ce9 7690 return libbpf_err(-ENOENT);
d5148d85
JS
7691 }
7692
f74a53d9 7693 bpf_object__for_each_map(map, obj) {
4580b25f 7694 char *pin_path = NULL;
0c19a9fb 7695 char buf[PATH_MAX];
0c19a9fb 7696
4580b25f
THJ
7697 if (path) {
7698 int len;
7699
7700 len = snprintf(buf, PATH_MAX, "%s/%s", path,
7701 bpf_map__name(map));
7702 if (len < 0) {
7703 err = -EINVAL;
7704 goto err_unpin_maps;
7705 } else if (len >= PATH_MAX) {
7706 err = -ENAMETOOLONG;
7707 goto err_unpin_maps;
7708 }
9cf309c5 7709 sanitize_pin_path(buf);
4580b25f
THJ
7710 pin_path = buf;
7711 } else if (!map->pin_path) {
7712 continue;
0c19a9fb
SF
7713 }
7714
4580b25f 7715 err = bpf_map__pin(map, pin_path);
0c19a9fb
SF
7716 if (err)
7717 goto err_unpin_maps;
7718 }
7719
7720 return 0;
7721
7722err_unpin_maps:
7723 while ((map = bpf_map__prev(map, obj))) {
4580b25f 7724 if (!map->pin_path)
0c19a9fb
SF
7725 continue;
7726
4580b25f 7727 bpf_map__unpin(map, NULL);
0c19a9fb
SF
7728 }
7729
e9fc3ce9 7730 return libbpf_err(err);
0c19a9fb
SF
7731}
7732
7733int bpf_object__unpin_maps(struct bpf_object *obj, const char *path)
7734{
7735 struct bpf_map *map;
7736 int err;
7737
7738 if (!obj)
e9fc3ce9 7739 return libbpf_err(-ENOENT);
0c19a9fb 7740
f74a53d9 7741 bpf_object__for_each_map(map, obj) {
4580b25f 7742 char *pin_path = NULL;
d5148d85 7743 char buf[PATH_MAX];
d5148d85 7744
4580b25f
THJ
7745 if (path) {
7746 int len;
7747
7748 len = snprintf(buf, PATH_MAX, "%s/%s", path,
7749 bpf_map__name(map));
7750 if (len < 0)
e9fc3ce9 7751 return libbpf_err(-EINVAL);
4580b25f 7752 else if (len >= PATH_MAX)
e9fc3ce9 7753 return libbpf_err(-ENAMETOOLONG);
9cf309c5 7754 sanitize_pin_path(buf);
4580b25f
THJ
7755 pin_path = buf;
7756 } else if (!map->pin_path) {
7757 continue;
7758 }
d5148d85 7759
4580b25f 7760 err = bpf_map__unpin(map, pin_path);
d5148d85 7761 if (err)
e9fc3ce9 7762 return libbpf_err(err);
d5148d85
JS
7763 }
7764
0c19a9fb
SF
7765 return 0;
7766}
7767
7768int bpf_object__pin_programs(struct bpf_object *obj, const char *path)
7769{
7770 struct bpf_program *prog;
7771 int err;
7772
7773 if (!obj)
e9fc3ce9 7774 return libbpf_err(-ENOENT);
0c19a9fb
SF
7775
7776 if (!obj->loaded) {
be18010e 7777 pr_warn("object not yet loaded; load it first\n");
e9fc3ce9 7778 return libbpf_err(-ENOENT);
0c19a9fb
SF
7779 }
7780
0c19a9fb
SF
7781 bpf_object__for_each_program(prog, obj) {
7782 char buf[PATH_MAX];
7783 int len;
7784
7785 len = snprintf(buf, PATH_MAX, "%s/%s", path,
33a2c75c 7786 prog->pin_name);
0c19a9fb
SF
7787 if (len < 0) {
7788 err = -EINVAL;
7789 goto err_unpin_programs;
7790 } else if (len >= PATH_MAX) {
7791 err = -ENAMETOOLONG;
7792 goto err_unpin_programs;
7793 }
7794
7795 err = bpf_program__pin(prog, buf);
7796 if (err)
7797 goto err_unpin_programs;
7798 }
7799
7800 return 0;
7801
7802err_unpin_programs:
7803 while ((prog = bpf_program__prev(prog, obj))) {
7804 char buf[PATH_MAX];
7805 int len;
7806
7807 len = snprintf(buf, PATH_MAX, "%s/%s", path,
33a2c75c 7808 prog->pin_name);
0c19a9fb
SF
7809 if (len < 0)
7810 continue;
7811 else if (len >= PATH_MAX)
7812 continue;
7813
7814 bpf_program__unpin(prog, buf);
7815 }
7816
e9fc3ce9 7817 return libbpf_err(err);
0c19a9fb
SF
7818}
7819
7820int bpf_object__unpin_programs(struct bpf_object *obj, const char *path)
7821{
7822 struct bpf_program *prog;
7823 int err;
7824
7825 if (!obj)
e9fc3ce9 7826 return libbpf_err(-ENOENT);
0c19a9fb 7827
d5148d85
JS
7828 bpf_object__for_each_program(prog, obj) {
7829 char buf[PATH_MAX];
7830 int len;
7831
7832 len = snprintf(buf, PATH_MAX, "%s/%s", path,
33a2c75c 7833 prog->pin_name);
d5148d85 7834 if (len < 0)
e9fc3ce9 7835 return libbpf_err(-EINVAL);
d5148d85 7836 else if (len >= PATH_MAX)
e9fc3ce9 7837 return libbpf_err(-ENAMETOOLONG);
d5148d85 7838
0c19a9fb 7839 err = bpf_program__unpin(prog, buf);
d5148d85 7840 if (err)
e9fc3ce9 7841 return libbpf_err(err);
d5148d85
JS
7842 }
7843
7844 return 0;
7845}
7846
0c19a9fb
SF
7847int bpf_object__pin(struct bpf_object *obj, const char *path)
7848{
7849 int err;
7850
7851 err = bpf_object__pin_maps(obj, path);
7852 if (err)
e9fc3ce9 7853 return libbpf_err(err);
0c19a9fb
SF
7854
7855 err = bpf_object__pin_programs(obj, path);
7856 if (err) {
7857 bpf_object__unpin_maps(obj, path);
e9fc3ce9 7858 return libbpf_err(err);
0c19a9fb
SF
7859 }
7860
7861 return 0;
7862}
7863
2d39d7c5
AN
7864static void bpf_map__destroy(struct bpf_map *map)
7865{
7866 if (map->clear_priv)
7867 map->clear_priv(map, map->priv);
7868 map->priv = NULL;
7869 map->clear_priv = NULL;
7870
646f02ff
AN
7871 if (map->inner_map) {
7872 bpf_map__destroy(map->inner_map);
7873 zfree(&map->inner_map);
7874 }
7875
7876 zfree(&map->init_slots);
7877 map->init_slots_sz = 0;
7878
2d39d7c5
AN
7879 if (map->mmaped) {
7880 munmap(map->mmaped, bpf_map_mmap_sz(map));
7881 map->mmaped = NULL;
7882 }
7883
7884 if (map->st_ops) {
7885 zfree(&map->st_ops->data);
7886 zfree(&map->st_ops->progs);
7887 zfree(&map->st_ops->kern_func_off);
7888 zfree(&map->st_ops);
7889 }
7890
7891 zfree(&map->name);
aed65917 7892 zfree(&map->real_name);
2d39d7c5
AN
7893 zfree(&map->pin_path);
7894
7895 if (map->fd >= 0)
7896 zclose(map->fd);
7897}
7898
1a5e3fb1
WN
7899void bpf_object__close(struct bpf_object *obj)
7900{
a5b8bd47
WN
7901 size_t i;
7902
50450fc7 7903 if (IS_ERR_OR_NULL(obj))
1a5e3fb1
WN
7904 return;
7905
10931d24
WN
7906 if (obj->clear_priv)
7907 obj->clear_priv(obj, obj->priv);
7908
67234743 7909 bpf_gen__free(obj->gen_loader);
1a5e3fb1 7910 bpf_object__elf_finish(obj);
4a404a7e 7911 bpf_object_unload(obj);
8a138aed 7912 btf__free(obj->btf);
2993e051 7913 btf_ext__free(obj->btf_ext);
1a5e3fb1 7914
2d39d7c5
AN
7915 for (i = 0; i < obj->nr_maps; i++)
7916 bpf_map__destroy(&obj->maps[i]);
d859900c 7917
1373ff59 7918 zfree(&obj->btf_custom_path);
8601fd42 7919 zfree(&obj->kconfig);
166750bc
AN
7920 zfree(&obj->externs);
7921 obj->nr_extern = 0;
7922
9d759a9b
WN
7923 zfree(&obj->maps);
7924 obj->nr_maps = 0;
a5b8bd47
WN
7925
7926 if (obj->programs && obj->nr_programs) {
7927 for (i = 0; i < obj->nr_programs; i++)
7928 bpf_program__exit(&obj->programs[i]);
7929 }
7930 zfree(&obj->programs);
7931
9a208eff 7932 list_del(&obj->list);
1a5e3fb1
WN
7933 free(obj);
7934}
aa9b1ac3 7935
9a208eff
WN
7936struct bpf_object *
7937bpf_object__next(struct bpf_object *prev)
7938{
7939 struct bpf_object *next;
689624f0
JB
7940 bool strict = (libbpf_mode & LIBBPF_STRICT_NO_OBJECT_LIST);
7941
7942 if (strict)
7943 return NULL;
9a208eff
WN
7944
7945 if (!prev)
7946 next = list_first_entry(&bpf_objects_list,
7947 struct bpf_object,
7948 list);
7949 else
7950 next = list_next_entry(prev, list);
7951
7952 /* Empty list is noticed here so don't need checking on entry. */
7953 if (&next->list == &bpf_objects_list)
7954 return NULL;
7955
7956 return next;
7957}
7958
a324aae3 7959const char *bpf_object__name(const struct bpf_object *obj)
acf860ae 7960{
e9fc3ce9 7961 return obj ? obj->name : libbpf_err_ptr(-EINVAL);
acf860ae
WN
7962}
7963
a324aae3 7964unsigned int bpf_object__kversion(const struct bpf_object *obj)
45825d8a 7965{
a7fe0450 7966 return obj ? obj->kern_version : 0;
45825d8a
WN
7967}
7968
a324aae3 7969struct btf *bpf_object__btf(const struct bpf_object *obj)
789f6bab
AI
7970{
7971 return obj ? obj->btf : NULL;
7972}
7973
8a138aed
MKL
7974int bpf_object__btf_fd(const struct bpf_object *obj)
7975{
7976 return obj->btf ? btf__fd(obj->btf) : -1;
7977}
7978
155f556d
RDT
7979int bpf_object__set_kversion(struct bpf_object *obj, __u32 kern_version)
7980{
7981 if (obj->loaded)
e9fc3ce9 7982 return libbpf_err(-EINVAL);
155f556d
RDT
7983
7984 obj->kern_version = kern_version;
7985
7986 return 0;
7987}
7988
10931d24
WN
7989int bpf_object__set_priv(struct bpf_object *obj, void *priv,
7990 bpf_object_clear_priv_t clear_priv)
7991{
7992 if (obj->priv && obj->clear_priv)
7993 obj->clear_priv(obj, obj->priv);
7994
7995 obj->priv = priv;
7996 obj->clear_priv = clear_priv;
7997 return 0;
7998}
7999
a324aae3 8000void *bpf_object__priv(const struct bpf_object *obj)
10931d24 8001{
e9fc3ce9 8002 return obj ? obj->priv : libbpf_err_ptr(-EINVAL);
10931d24
WN
8003}
8004
67234743
AS
8005int bpf_object__gen_loader(struct bpf_object *obj, struct gen_loader_opts *opts)
8006{
8007 struct bpf_gen *gen;
8008
8009 if (!opts)
8010 return -EFAULT;
8011 if (!OPTS_VALID(opts, gen_loader_opts))
8012 return -EINVAL;
8013 gen = calloc(sizeof(*gen), 1);
8014 if (!gen)
8015 return -ENOMEM;
8016 gen->opts = opts;
8017 obj->gen_loader = gen;
8018 return 0;
8019}
8020
eac7d845 8021static struct bpf_program *
a324aae3
AN
8022__bpf_program__iter(const struct bpf_program *p, const struct bpf_object *obj,
8023 bool forward)
aa9b1ac3 8024{
a83d6e76 8025 size_t nr_programs = obj->nr_programs;
0c19a9fb 8026 ssize_t idx;
aa9b1ac3 8027
a83d6e76 8028 if (!nr_programs)
aa9b1ac3 8029 return NULL;
aa9b1ac3 8030
a83d6e76
MKL
8031 if (!p)
8032 /* Iter from the beginning */
8033 return forward ? &obj->programs[0] :
8034 &obj->programs[nr_programs - 1];
8035
0c19a9fb 8036 if (p->obj != obj) {
be18010e 8037 pr_warn("error: program handler doesn't match object\n");
e9fc3ce9 8038 return errno = EINVAL, NULL;
aa9b1ac3
WN
8039 }
8040
a83d6e76 8041 idx = (p - obj->programs) + (forward ? 1 : -1);
0c19a9fb 8042 if (idx >= obj->nr_programs || idx < 0)
aa9b1ac3
WN
8043 return NULL;
8044 return &obj->programs[idx];
8045}
8046
eac7d845 8047struct bpf_program *
a324aae3 8048bpf_program__next(struct bpf_program *prev, const struct bpf_object *obj)
2088a3a7
HC
8049{
8050 return bpf_object__next_program(obj, prev);
8051}
8052
8053struct bpf_program *
8054bpf_object__next_program(const struct bpf_object *obj, struct bpf_program *prev)
eac7d845
JK
8055{
8056 struct bpf_program *prog = prev;
8057
8058 do {
a83d6e76 8059 prog = __bpf_program__iter(prog, obj, true);
c3c55696 8060 } while (prog && prog_is_subprog(obj, prog));
0c19a9fb
SF
8061
8062 return prog;
8063}
8064
8065struct bpf_program *
a324aae3 8066bpf_program__prev(struct bpf_program *next, const struct bpf_object *obj)
2088a3a7
HC
8067{
8068 return bpf_object__prev_program(obj, next);
8069}
8070
8071struct bpf_program *
8072bpf_object__prev_program(const struct bpf_object *obj, struct bpf_program *next)
0c19a9fb
SF
8073{
8074 struct bpf_program *prog = next;
8075
0c19a9fb 8076 do {
a83d6e76 8077 prog = __bpf_program__iter(prog, obj, false);
c3c55696 8078 } while (prog && prog_is_subprog(obj, prog));
eac7d845
JK
8079
8080 return prog;
8081}
8082
edb13ed4
ACM
8083int bpf_program__set_priv(struct bpf_program *prog, void *priv,
8084 bpf_program_clear_priv_t clear_priv)
aa9b1ac3
WN
8085{
8086 if (prog->priv && prog->clear_priv)
8087 prog->clear_priv(prog, prog->priv);
8088
8089 prog->priv = priv;
8090 prog->clear_priv = clear_priv;
8091 return 0;
8092}
8093
a324aae3 8094void *bpf_program__priv(const struct bpf_program *prog)
aa9b1ac3 8095{
e9fc3ce9 8096 return prog ? prog->priv : libbpf_err_ptr(-EINVAL);
aa9b1ac3
WN
8097}
8098
9aba3613
JK
8099void bpf_program__set_ifindex(struct bpf_program *prog, __u32 ifindex)
8100{
8101 prog->prog_ifindex = ifindex;
8102}
8103
01af3bf0
AN
8104const char *bpf_program__name(const struct bpf_program *prog)
8105{
8106 return prog->name;
8107}
8108
52109584
AN
8109const char *bpf_program__section_name(const struct bpf_program *prog)
8110{
8111 return prog->sec_name;
8112}
8113
a324aae3 8114const char *bpf_program__title(const struct bpf_program *prog, bool needs_copy)
aa9b1ac3
WN
8115{
8116 const char *title;
8117
52109584 8118 title = prog->sec_name;
715f8db9 8119 if (needs_copy) {
aa9b1ac3
WN
8120 title = strdup(title);
8121 if (!title) {
be18010e 8122 pr_warn("failed to strdup program title\n");
e9fc3ce9 8123 return libbpf_err_ptr(-ENOMEM);
aa9b1ac3
WN
8124 }
8125 }
8126
8127 return title;
8128}
8129
d9297581
AN
8130bool bpf_program__autoload(const struct bpf_program *prog)
8131{
8132 return prog->load;
8133}
8134
8135int bpf_program__set_autoload(struct bpf_program *prog, bool autoload)
8136{
8137 if (prog->obj->loaded)
e9fc3ce9 8138 return libbpf_err(-EINVAL);
d9297581
AN
8139
8140 prog->load = autoload;
8141 return 0;
8142}
8143
a324aae3 8144int bpf_program__fd(const struct bpf_program *prog)
aa9b1ac3 8145{
b580563e
WN
8146 return bpf_program__nth_fd(prog, 0);
8147}
8148
1a734efe
THJ
8149size_t bpf_program__size(const struct bpf_program *prog)
8150{
9c0f8cbd 8151 return prog->insns_cnt * BPF_INSN_SZ;
1a734efe
THJ
8152}
8153
65a7fa2e
AN
8154const struct bpf_insn *bpf_program__insns(const struct bpf_program *prog)
8155{
8156 return prog->insns;
8157}
8158
8159size_t bpf_program__insn_cnt(const struct bpf_program *prog)
8160{
8161 return prog->insns_cnt;
8162}
8163
b580563e
WN
8164int bpf_program__set_prep(struct bpf_program *prog, int nr_instances,
8165 bpf_program_prep_t prep)
8166{
8167 int *instances_fds;
8168
8169 if (nr_instances <= 0 || !prep)
e9fc3ce9 8170 return libbpf_err(-EINVAL);
b580563e
WN
8171
8172 if (prog->instances.nr > 0 || prog->instances.fds) {
be18010e 8173 pr_warn("Can't set pre-processor after loading\n");
e9fc3ce9 8174 return libbpf_err(-EINVAL);
b580563e
WN
8175 }
8176
8177 instances_fds = malloc(sizeof(int) * nr_instances);
8178 if (!instances_fds) {
be18010e 8179 pr_warn("alloc memory failed for fds\n");
e9fc3ce9 8180 return libbpf_err(-ENOMEM);
b580563e
WN
8181 }
8182
8183 /* fill all fd with -1 */
8184 memset(instances_fds, -1, sizeof(int) * nr_instances);
8185
8186 prog->instances.nr = nr_instances;
8187 prog->instances.fds = instances_fds;
8188 prog->preprocessor = prep;
8189 return 0;
8190}
8191
a324aae3 8192int bpf_program__nth_fd(const struct bpf_program *prog, int n)
b580563e
WN
8193{
8194 int fd;
8195
1e960043 8196 if (!prog)
e9fc3ce9 8197 return libbpf_err(-EINVAL);
1e960043 8198
b580563e 8199 if (n >= prog->instances.nr || n < 0) {
be18010e 8200 pr_warn("Can't get the %dth fd from program %s: only %d instances\n",
52109584 8201 n, prog->name, prog->instances.nr);
e9fc3ce9 8202 return libbpf_err(-EINVAL);
b580563e
WN
8203 }
8204
8205 fd = prog->instances.fds[n];
8206 if (fd < 0) {
be18010e 8207 pr_warn("%dth instance of program '%s' is invalid\n",
52109584 8208 n, prog->name);
e9fc3ce9 8209 return libbpf_err(-ENOENT);
b580563e
WN
8210 }
8211
8212 return fd;
aa9b1ac3 8213}
9d759a9b 8214
a46410d5 8215enum bpf_prog_type bpf_program__get_type(const struct bpf_program *prog)
f1eead9e
AN
8216{
8217 return prog->type;
8218}
8219
dd26b7f5 8220void bpf_program__set_type(struct bpf_program *prog, enum bpf_prog_type type)
5f44e4c8
WN
8221{
8222 prog->type = type;
8223}
8224
a324aae3 8225static bool bpf_program__is_type(const struct bpf_program *prog,
5f44e4c8
WN
8226 enum bpf_prog_type type)
8227{
8228 return prog ? (prog->type == type) : false;
8229}
8230
a324aae3
AN
8231#define BPF_PROG_TYPE_FNS(NAME, TYPE) \
8232int bpf_program__set_##NAME(struct bpf_program *prog) \
8233{ \
8234 if (!prog) \
e9fc3ce9 8235 return libbpf_err(-EINVAL); \
a324aae3
AN
8236 bpf_program__set_type(prog, TYPE); \
8237 return 0; \
8238} \
8239 \
8240bool bpf_program__is_##NAME(const struct bpf_program *prog) \
8241{ \
8242 return bpf_program__is_type(prog, TYPE); \
8243} \
ed794073 8244
7803ba73 8245BPF_PROG_TYPE_FNS(socket_filter, BPF_PROG_TYPE_SOCKET_FILTER);
1e092a03 8246BPF_PROG_TYPE_FNS(lsm, BPF_PROG_TYPE_LSM);
ed794073 8247BPF_PROG_TYPE_FNS(kprobe, BPF_PROG_TYPE_KPROBE);
7803ba73
JS
8248BPF_PROG_TYPE_FNS(sched_cls, BPF_PROG_TYPE_SCHED_CLS);
8249BPF_PROG_TYPE_FNS(sched_act, BPF_PROG_TYPE_SCHED_ACT);
ed794073 8250BPF_PROG_TYPE_FNS(tracepoint, BPF_PROG_TYPE_TRACEPOINT);
e14c93fd 8251BPF_PROG_TYPE_FNS(raw_tracepoint, BPF_PROG_TYPE_RAW_TRACEPOINT);
7803ba73
JS
8252BPF_PROG_TYPE_FNS(xdp, BPF_PROG_TYPE_XDP);
8253BPF_PROG_TYPE_FNS(perf_event, BPF_PROG_TYPE_PERF_EVENT);
12a8654b 8254BPF_PROG_TYPE_FNS(tracing, BPF_PROG_TYPE_TRACING);
590a0088 8255BPF_PROG_TYPE_FNS(struct_ops, BPF_PROG_TYPE_STRUCT_OPS);
2db6eab1 8256BPF_PROG_TYPE_FNS(extension, BPF_PROG_TYPE_EXT);
499dd29d 8257BPF_PROG_TYPE_FNS(sk_lookup, BPF_PROG_TYPE_SK_LOOKUP);
5f44e4c8 8258
f1eead9e 8259enum bpf_attach_type
a46410d5 8260bpf_program__get_expected_attach_type(const struct bpf_program *prog)
f1eead9e
AN
8261{
8262 return prog->expected_attach_type;
8263}
8264
16962b24
JF
8265void bpf_program__set_expected_attach_type(struct bpf_program *prog,
8266 enum bpf_attach_type type)
d7be143b
AI
8267{
8268 prog->expected_attach_type = type;
8269}
8270
15ea31fa 8271#define SEC_DEF(sec_pfx, ptype, atype, flags, ...) { \
d7a18ea7 8272 .sec = sec_pfx, \
d7a18ea7 8273 .prog_type = BPF_PROG_TYPE_##ptype, \
15ea31fa
AN
8274 .expected_attach_type = atype, \
8275 .cookie = (long)(flags), \
12d9466d 8276 .preload_fn = libbpf_preload_prog, \
d7a18ea7
AN
8277 __VA_ARGS__ \
8278}
8279
12d9466d
AN
8280static struct bpf_link *attach_kprobe(const struct bpf_program *prog, long cookie);
8281static struct bpf_link *attach_tp(const struct bpf_program *prog, long cookie);
8282static struct bpf_link *attach_raw_tp(const struct bpf_program *prog, long cookie);
8283static struct bpf_link *attach_trace(const struct bpf_program *prog, long cookie);
8284static struct bpf_link *attach_lsm(const struct bpf_program *prog, long cookie);
8285static struct bpf_link *attach_iter(const struct bpf_program *prog, long cookie);
d7a18ea7 8286
d7a18ea7 8287static const struct bpf_sec_def section_defs[] = {
dd94d45c
AN
8288 SEC_DEF("socket", SOCKET_FILTER, 0, SEC_NONE | SEC_SLOPPY_PFX),
8289 SEC_DEF("sk_reuseport/migrate", SK_REUSEPORT, BPF_SK_REUSEPORT_SELECT_OR_MIGRATE, SEC_ATTACHABLE | SEC_SLOPPY_PFX),
8290 SEC_DEF("sk_reuseport", SK_REUSEPORT, BPF_SK_REUSEPORT_SELECT, SEC_ATTACHABLE | SEC_SLOPPY_PFX),
15ea31fa
AN
8291 SEC_DEF("kprobe/", KPROBE, 0, SEC_NONE, attach_kprobe),
8292 SEC_DEF("uprobe/", KPROBE, 0, SEC_NONE),
8293 SEC_DEF("kretprobe/", KPROBE, 0, SEC_NONE, attach_kprobe),
8294 SEC_DEF("uretprobe/", KPROBE, 0, SEC_NONE),
15ea31fa 8295 SEC_DEF("tc", SCHED_CLS, 0, SEC_NONE),
dd94d45c
AN
8296 SEC_DEF("classifier", SCHED_CLS, 0, SEC_NONE | SEC_SLOPPY_PFX),
8297 SEC_DEF("action", SCHED_ACT, 0, SEC_NONE | SEC_SLOPPY_PFX),
15ea31fa
AN
8298 SEC_DEF("tracepoint/", TRACEPOINT, 0, SEC_NONE, attach_tp),
8299 SEC_DEF("tp/", TRACEPOINT, 0, SEC_NONE, attach_tp),
8300 SEC_DEF("raw_tracepoint/", RAW_TRACEPOINT, 0, SEC_NONE, attach_raw_tp),
8301 SEC_DEF("raw_tp/", RAW_TRACEPOINT, 0, SEC_NONE, attach_raw_tp),
ccaf12d6
HT
8302 SEC_DEF("raw_tracepoint.w/", RAW_TRACEPOINT_WRITABLE, 0, SEC_NONE, attach_raw_tp),
8303 SEC_DEF("raw_tp.w/", RAW_TRACEPOINT_WRITABLE, 0, SEC_NONE, attach_raw_tp),
15ea31fa
AN
8304 SEC_DEF("tp_btf/", TRACING, BPF_TRACE_RAW_TP, SEC_ATTACH_BTF, attach_trace),
8305 SEC_DEF("fentry/", TRACING, BPF_TRACE_FENTRY, SEC_ATTACH_BTF, attach_trace),
8306 SEC_DEF("fmod_ret/", TRACING, BPF_MODIFY_RETURN, SEC_ATTACH_BTF, attach_trace),
8307 SEC_DEF("fexit/", TRACING, BPF_TRACE_FEXIT, SEC_ATTACH_BTF, attach_trace),
8308 SEC_DEF("fentry.s/", TRACING, BPF_TRACE_FENTRY, SEC_ATTACH_BTF | SEC_SLEEPABLE, attach_trace),
8309 SEC_DEF("fmod_ret.s/", TRACING, BPF_MODIFY_RETURN, SEC_ATTACH_BTF | SEC_SLEEPABLE, attach_trace),
8310 SEC_DEF("fexit.s/", TRACING, BPF_TRACE_FEXIT, SEC_ATTACH_BTF | SEC_SLEEPABLE, attach_trace),
8311 SEC_DEF("freplace/", EXT, 0, SEC_ATTACH_BTF, attach_trace),
8312 SEC_DEF("lsm/", LSM, BPF_LSM_MAC, SEC_ATTACH_BTF, attach_lsm),
8313 SEC_DEF("lsm.s/", LSM, BPF_LSM_MAC, SEC_ATTACH_BTF | SEC_SLEEPABLE, attach_lsm),
8314 SEC_DEF("iter/", TRACING, BPF_TRACE_ITER, SEC_ATTACH_BTF, attach_iter),
8315 SEC_DEF("syscall", SYSCALL, 0, SEC_SLEEPABLE),
d41ea045
AN
8316 SEC_DEF("xdp_devmap/", XDP, BPF_XDP_DEVMAP, SEC_ATTACHABLE),
8317 SEC_DEF("xdp_cpumap/", XDP, BPF_XDP_CPUMAP, SEC_ATTACHABLE),
dd94d45c
AN
8318 SEC_DEF("xdp", XDP, BPF_XDP, SEC_ATTACHABLE_OPT | SEC_SLOPPY_PFX),
8319 SEC_DEF("perf_event", PERF_EVENT, 0, SEC_NONE | SEC_SLOPPY_PFX),
8320 SEC_DEF("lwt_in", LWT_IN, 0, SEC_NONE | SEC_SLOPPY_PFX),
8321 SEC_DEF("lwt_out", LWT_OUT, 0, SEC_NONE | SEC_SLOPPY_PFX),
8322 SEC_DEF("lwt_xmit", LWT_XMIT, 0, SEC_NONE | SEC_SLOPPY_PFX),
8323 SEC_DEF("lwt_seg6local", LWT_SEG6LOCAL, 0, SEC_NONE | SEC_SLOPPY_PFX),
8324 SEC_DEF("cgroup_skb/ingress", CGROUP_SKB, BPF_CGROUP_INET_INGRESS, SEC_ATTACHABLE_OPT | SEC_SLOPPY_PFX),
8325 SEC_DEF("cgroup_skb/egress", CGROUP_SKB, BPF_CGROUP_INET_EGRESS, SEC_ATTACHABLE_OPT | SEC_SLOPPY_PFX),
8326 SEC_DEF("cgroup/skb", CGROUP_SKB, 0, SEC_NONE | SEC_SLOPPY_PFX),
8327 SEC_DEF("cgroup/sock_create", CGROUP_SOCK, BPF_CGROUP_INET_SOCK_CREATE, SEC_ATTACHABLE | SEC_SLOPPY_PFX),
8328 SEC_DEF("cgroup/sock_release", CGROUP_SOCK, BPF_CGROUP_INET_SOCK_RELEASE, SEC_ATTACHABLE | SEC_SLOPPY_PFX),
8329 SEC_DEF("cgroup/sock", CGROUP_SOCK, BPF_CGROUP_INET_SOCK_CREATE, SEC_ATTACHABLE_OPT | SEC_SLOPPY_PFX),
8330 SEC_DEF("cgroup/post_bind4", CGROUP_SOCK, BPF_CGROUP_INET4_POST_BIND, SEC_ATTACHABLE | SEC_SLOPPY_PFX),
8331 SEC_DEF("cgroup/post_bind6", CGROUP_SOCK, BPF_CGROUP_INET6_POST_BIND, SEC_ATTACHABLE | SEC_SLOPPY_PFX),
8332 SEC_DEF("cgroup/dev", CGROUP_DEVICE, BPF_CGROUP_DEVICE, SEC_ATTACHABLE_OPT | SEC_SLOPPY_PFX),
8333 SEC_DEF("sockops", SOCK_OPS, BPF_CGROUP_SOCK_OPS, SEC_ATTACHABLE_OPT | SEC_SLOPPY_PFX),
8334 SEC_DEF("sk_skb/stream_parser", SK_SKB, BPF_SK_SKB_STREAM_PARSER, SEC_ATTACHABLE_OPT | SEC_SLOPPY_PFX),
8335 SEC_DEF("sk_skb/stream_verdict",SK_SKB, BPF_SK_SKB_STREAM_VERDICT, SEC_ATTACHABLE_OPT | SEC_SLOPPY_PFX),
8336 SEC_DEF("sk_skb", SK_SKB, 0, SEC_NONE | SEC_SLOPPY_PFX),
8337 SEC_DEF("sk_msg", SK_MSG, BPF_SK_MSG_VERDICT, SEC_ATTACHABLE_OPT | SEC_SLOPPY_PFX),
8338 SEC_DEF("lirc_mode2", LIRC_MODE2, BPF_LIRC_MODE2, SEC_ATTACHABLE_OPT | SEC_SLOPPY_PFX),
8339 SEC_DEF("flow_dissector", FLOW_DISSECTOR, BPF_FLOW_DISSECTOR, SEC_ATTACHABLE_OPT | SEC_SLOPPY_PFX),
8340 SEC_DEF("cgroup/bind4", CGROUP_SOCK_ADDR, BPF_CGROUP_INET4_BIND, SEC_ATTACHABLE | SEC_SLOPPY_PFX),
8341 SEC_DEF("cgroup/bind6", CGROUP_SOCK_ADDR, BPF_CGROUP_INET6_BIND, SEC_ATTACHABLE | SEC_SLOPPY_PFX),
8342 SEC_DEF("cgroup/connect4", CGROUP_SOCK_ADDR, BPF_CGROUP_INET4_CONNECT, SEC_ATTACHABLE | SEC_SLOPPY_PFX),
8343 SEC_DEF("cgroup/connect6", CGROUP_SOCK_ADDR, BPF_CGROUP_INET6_CONNECT, SEC_ATTACHABLE | SEC_SLOPPY_PFX),
8344 SEC_DEF("cgroup/sendmsg4", CGROUP_SOCK_ADDR, BPF_CGROUP_UDP4_SENDMSG, SEC_ATTACHABLE | SEC_SLOPPY_PFX),
8345 SEC_DEF("cgroup/sendmsg6", CGROUP_SOCK_ADDR, BPF_CGROUP_UDP6_SENDMSG, SEC_ATTACHABLE | SEC_SLOPPY_PFX),
8346 SEC_DEF("cgroup/recvmsg4", CGROUP_SOCK_ADDR, BPF_CGROUP_UDP4_RECVMSG, SEC_ATTACHABLE | SEC_SLOPPY_PFX),
8347 SEC_DEF("cgroup/recvmsg6", CGROUP_SOCK_ADDR, BPF_CGROUP_UDP6_RECVMSG, SEC_ATTACHABLE | SEC_SLOPPY_PFX),
8348 SEC_DEF("cgroup/getpeername4", CGROUP_SOCK_ADDR, BPF_CGROUP_INET4_GETPEERNAME, SEC_ATTACHABLE | SEC_SLOPPY_PFX),
8349 SEC_DEF("cgroup/getpeername6", CGROUP_SOCK_ADDR, BPF_CGROUP_INET6_GETPEERNAME, SEC_ATTACHABLE | SEC_SLOPPY_PFX),
8350 SEC_DEF("cgroup/getsockname4", CGROUP_SOCK_ADDR, BPF_CGROUP_INET4_GETSOCKNAME, SEC_ATTACHABLE | SEC_SLOPPY_PFX),
8351 SEC_DEF("cgroup/getsockname6", CGROUP_SOCK_ADDR, BPF_CGROUP_INET6_GETSOCKNAME, SEC_ATTACHABLE | SEC_SLOPPY_PFX),
8352 SEC_DEF("cgroup/sysctl", CGROUP_SYSCTL, BPF_CGROUP_SYSCTL, SEC_ATTACHABLE | SEC_SLOPPY_PFX),
8353 SEC_DEF("cgroup/getsockopt", CGROUP_SOCKOPT, BPF_CGROUP_GETSOCKOPT, SEC_ATTACHABLE | SEC_SLOPPY_PFX),
8354 SEC_DEF("cgroup/setsockopt", CGROUP_SOCKOPT, BPF_CGROUP_SETSOCKOPT, SEC_ATTACHABLE | SEC_SLOPPY_PFX),
8355 SEC_DEF("struct_ops+", STRUCT_OPS, 0, SEC_NONE),
7c80c87a 8356 SEC_DEF("sk_lookup", SK_LOOKUP, BPF_SK_LOOKUP, SEC_ATTACHABLE | SEC_SLOPPY_PFX),
583c9009 8357};
d7be143b 8358
c76e4c22
TS
8359#define MAX_TYPE_NAME_SIZE 32
8360
d7a18ea7
AN
8361static const struct bpf_sec_def *find_sec_def(const char *sec_name)
8362{
dd94d45c
AN
8363 const struct bpf_sec_def *sec_def;
8364 enum sec_def_flags sec_flags;
8365 int i, n = ARRAY_SIZE(section_defs), len;
8366 bool strict = libbpf_mode & LIBBPF_STRICT_SEC_NAME;
d7a18ea7
AN
8367
8368 for (i = 0; i < n; i++) {
dd94d45c
AN
8369 sec_def = &section_defs[i];
8370 sec_flags = sec_def->cookie;
8371 len = strlen(sec_def->sec);
8372
8373 /* "type/" always has to have proper SEC("type/extras") form */
8374 if (sec_def->sec[len - 1] == '/') {
8375 if (str_has_pfx(sec_name, sec_def->sec))
8376 return sec_def;
8377 continue;
8378 }
8379
8380 /* "type+" means it can be either exact SEC("type") or
8381 * well-formed SEC("type/extras") with proper '/' separator
8382 */
8383 if (sec_def->sec[len - 1] == '+') {
8384 len--;
8385 /* not even a prefix */
8386 if (strncmp(sec_name, sec_def->sec, len) != 0)
8387 continue;
8388 /* exact match or has '/' separator */
8389 if (sec_name[len] == '\0' || sec_name[len] == '/')
8390 return sec_def;
8391 continue;
8392 }
8393
8394 /* SEC_SLOPPY_PFX definitions are allowed to be just prefix
8395 * matches, unless strict section name mode
8396 * (LIBBPF_STRICT_SEC_NAME) is enabled, in which case the
8397 * match has to be exact.
8398 */
8399 if ((sec_flags & SEC_SLOPPY_PFX) && !strict) {
8400 if (str_has_pfx(sec_name, sec_def->sec))
8401 return sec_def;
8402 continue;
8403 }
8404
8405 /* Definitions not marked SEC_SLOPPY_PFX (e.g.,
8406 * SEC("syscall")) are exact matches in both modes.
8407 */
8408 if (strcmp(sec_name, sec_def->sec) == 0)
8409 return sec_def;
d7a18ea7
AN
8410 }
8411 return NULL;
8412}
8413
c76e4c22
TS
8414static char *libbpf_get_type_names(bool attach_type)
8415{
d7a18ea7 8416 int i, len = ARRAY_SIZE(section_defs) * MAX_TYPE_NAME_SIZE;
c76e4c22
TS
8417 char *buf;
8418
8419 buf = malloc(len);
8420 if (!buf)
8421 return NULL;
8422
8423 buf[0] = '\0';
8424 /* Forge string buf with all available names */
d7a18ea7 8425 for (i = 0; i < ARRAY_SIZE(section_defs); i++) {
15ea31fa
AN
8426 const struct bpf_sec_def *sec_def = &section_defs[i];
8427
8428 if (attach_type) {
8429 if (sec_def->preload_fn != libbpf_preload_prog)
8430 continue;
8431
8432 if (!(sec_def->cookie & SEC_ATTACHABLE))
8433 continue;
8434 }
c76e4c22 8435
d7a18ea7 8436 if (strlen(buf) + strlen(section_defs[i].sec) + 2 > len) {
c76e4c22
TS
8437 free(buf);
8438 return NULL;
8439 }
8440 strcat(buf, " ");
d7a18ea7 8441 strcat(buf, section_defs[i].sec);
c76e4c22
TS
8442 }
8443
8444 return buf;
8445}
8446
b60df2a0
JK
8447int libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,
8448 enum bpf_attach_type *expected_attach_type)
583c9009 8449{
d7a18ea7 8450 const struct bpf_sec_def *sec_def;
c76e4c22 8451 char *type_names;
583c9009 8452
b60df2a0 8453 if (!name)
e9fc3ce9 8454 return libbpf_err(-EINVAL);
583c9009 8455
d7a18ea7
AN
8456 sec_def = find_sec_def(name);
8457 if (sec_def) {
8458 *prog_type = sec_def->prog_type;
8459 *expected_attach_type = sec_def->expected_attach_type;
b60df2a0
JK
8460 return 0;
8461 }
d7a18ea7 8462
4a3d6c6a 8463 pr_debug("failed to guess program type from ELF section '%s'\n", name);
c76e4c22
TS
8464 type_names = libbpf_get_type_names(false);
8465 if (type_names != NULL) {
3f519353 8466 pr_debug("supported section(type) names are:%s\n", type_names);
c76e4c22
TS
8467 free(type_names);
8468 }
8469
e9fc3ce9 8470 return libbpf_err(-ESRCH);
b60df2a0 8471}
583c9009 8472
590a0088
MKL
8473static struct bpf_map *find_struct_ops_map_by_offset(struct bpf_object *obj,
8474 size_t offset)
8475{
8476 struct bpf_map *map;
8477 size_t i;
8478
8479 for (i = 0; i < obj->nr_maps; i++) {
8480 map = &obj->maps[i];
8481 if (!bpf_map__is_struct_ops(map))
8482 continue;
8483 if (map->sec_offset <= offset &&
8484 offset - map->sec_offset < map->def.value_size)
8485 return map;
8486 }
8487
8488 return NULL;
8489}
8490
8491/* Collect the reloc from ELF and populate the st_ops->progs[] */
646f02ff 8492static int bpf_object__collect_st_ops_relos(struct bpf_object *obj,
ad23b723 8493 Elf64_Shdr *shdr, Elf_Data *data)
590a0088
MKL
8494{
8495 const struct btf_member *member;
8496 struct bpf_struct_ops *st_ops;
8497 struct bpf_program *prog;
8498 unsigned int shdr_idx;
8499 const struct btf *btf;
8500 struct bpf_map *map;
7e06aad5 8501 unsigned int moff, insn_idx;
590a0088 8502 const char *name;
1d1a3bcf 8503 __u32 member_idx;
ad23b723
AN
8504 Elf64_Sym *sym;
8505 Elf64_Rel *rel;
590a0088
MKL
8506 int i, nrels;
8507
590a0088
MKL
8508 btf = obj->btf;
8509 nrels = shdr->sh_size / shdr->sh_entsize;
8510 for (i = 0; i < nrels; i++) {
ad23b723
AN
8511 rel = elf_rel_by_idx(data, i);
8512 if (!rel) {
590a0088
MKL
8513 pr_warn("struct_ops reloc: failed to get %d reloc\n", i);
8514 return -LIBBPF_ERRNO__FORMAT;
8515 }
8516
ad23b723
AN
8517 sym = elf_sym_by_idx(obj, ELF64_R_SYM(rel->r_info));
8518 if (!sym) {
590a0088 8519 pr_warn("struct_ops reloc: symbol %zx not found\n",
ad23b723 8520 (size_t)ELF64_R_SYM(rel->r_info));
590a0088
MKL
8521 return -LIBBPF_ERRNO__FORMAT;
8522 }
8523
ad23b723
AN
8524 name = elf_sym_str(obj, sym->st_name) ?: "<?>";
8525 map = find_struct_ops_map_by_offset(obj, rel->r_offset);
590a0088 8526 if (!map) {
ad23b723
AN
8527 pr_warn("struct_ops reloc: cannot find map at rel->r_offset %zu\n",
8528 (size_t)rel->r_offset);
590a0088
MKL
8529 return -EINVAL;
8530 }
8531
ad23b723
AN
8532 moff = rel->r_offset - map->sec_offset;
8533 shdr_idx = sym->st_shndx;
590a0088 8534 st_ops = map->st_ops;
ad23b723 8535 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 8536 map->name,
ad23b723
AN
8537 (long long)(rel->r_info >> 32),
8538 (long long)sym->st_value,
8539 shdr_idx, (size_t)rel->r_offset,
8540 map->sec_offset, sym->st_name, name);
590a0088
MKL
8541
8542 if (shdr_idx >= SHN_LORESERVE) {
ad23b723
AN
8543 pr_warn("struct_ops reloc %s: rel->r_offset %zu shdr_idx %u unsupported non-static function\n",
8544 map->name, (size_t)rel->r_offset, shdr_idx);
590a0088
MKL
8545 return -LIBBPF_ERRNO__RELOC;
8546 }
ad23b723 8547 if (sym->st_value % BPF_INSN_SZ) {
7e06aad5 8548 pr_warn("struct_ops reloc %s: invalid target program offset %llu\n",
ad23b723 8549 map->name, (unsigned long long)sym->st_value);
7e06aad5
AN
8550 return -LIBBPF_ERRNO__FORMAT;
8551 }
ad23b723 8552 insn_idx = sym->st_value / BPF_INSN_SZ;
590a0088
MKL
8553
8554 member = find_member_by_offset(st_ops->type, moff * 8);
8555 if (!member) {
8556 pr_warn("struct_ops reloc %s: cannot find member at moff %u\n",
8557 map->name, moff);
8558 return -EINVAL;
8559 }
8560 member_idx = member - btf_members(st_ops->type);
8561 name = btf__name_by_offset(btf, member->name_off);
8562
8563 if (!resolve_func_ptr(btf, member->type, NULL)) {
8564 pr_warn("struct_ops reloc %s: cannot relocate non func ptr %s\n",
8565 map->name, name);
8566 return -EINVAL;
8567 }
8568
7e06aad5 8569 prog = find_prog_by_sec_insn(obj, shdr_idx, insn_idx);
590a0088
MKL
8570 if (!prog) {
8571 pr_warn("struct_ops reloc %s: cannot find prog at shdr_idx %u to relocate func ptr %s\n",
8572 map->name, shdr_idx, name);
8573 return -EINVAL;
8574 }
8575
91b4d1d1
AN
8576 /* prevent the use of BPF prog with invalid type */
8577 if (prog->type != BPF_PROG_TYPE_STRUCT_OPS) {
8578 pr_warn("struct_ops reloc %s: prog %s is not struct_ops BPF program\n",
8579 map->name, prog->name);
8580 return -EINVAL;
8581 }
590a0088 8582
91b4d1d1
AN
8583 /* if we haven't yet processed this BPF program, record proper
8584 * attach_btf_id and member_idx
8585 */
8586 if (!prog->attach_btf_id) {
590a0088
MKL
8587 prog->attach_btf_id = st_ops->type_id;
8588 prog->expected_attach_type = member_idx;
590a0088 8589 }
91b4d1d1
AN
8590
8591 /* struct_ops BPF prog can be re-used between multiple
8592 * .struct_ops as long as it's the same struct_ops struct
8593 * definition and the same function pointer field
8594 */
8595 if (prog->attach_btf_id != st_ops->type_id ||
8596 prog->expected_attach_type != member_idx) {
8597 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",
8598 map->name, prog->name, prog->sec_name, prog->type,
8599 prog->attach_btf_id, prog->expected_attach_type, name);
8600 return -EINVAL;
8601 }
8602
590a0088
MKL
8603 st_ops->progs[member_idx] = prog;
8604 }
8605
8606 return 0;
590a0088
MKL
8607}
8608
a6ed02ca 8609#define BTF_TRACE_PREFIX "btf_trace_"
1e092a03 8610#define BTF_LSM_PREFIX "bpf_lsm_"
21aef70e 8611#define BTF_ITER_PREFIX "bpf_iter_"
a6ed02ca
KS
8612#define BTF_MAX_NAME_SIZE 128
8613
67234743
AS
8614void btf_get_kernel_prefix_kind(enum bpf_attach_type attach_type,
8615 const char **prefix, int *kind)
8616{
8617 switch (attach_type) {
8618 case BPF_TRACE_RAW_TP:
8619 *prefix = BTF_TRACE_PREFIX;
8620 *kind = BTF_KIND_TYPEDEF;
8621 break;
8622 case BPF_LSM_MAC:
8623 *prefix = BTF_LSM_PREFIX;
8624 *kind = BTF_KIND_FUNC;
8625 break;
8626 case BPF_TRACE_ITER:
8627 *prefix = BTF_ITER_PREFIX;
8628 *kind = BTF_KIND_FUNC;
8629 break;
8630 default:
8631 *prefix = "";
8632 *kind = BTF_KIND_FUNC;
8633 }
8634}
8635
a6ed02ca
KS
8636static int find_btf_by_prefix_kind(const struct btf *btf, const char *prefix,
8637 const char *name, __u32 kind)
8638{
8639 char btf_type_name[BTF_MAX_NAME_SIZE];
8640 int ret;
8641
8642 ret = snprintf(btf_type_name, sizeof(btf_type_name),
8643 "%s%s", prefix, name);
8644 /* snprintf returns the number of characters written excluding the
c139e40a 8645 * terminating null. So, if >= BTF_MAX_NAME_SIZE are written, it
a6ed02ca
KS
8646 * indicates truncation.
8647 */
8648 if (ret < 0 || ret >= sizeof(btf_type_name))
8649 return -ENAMETOOLONG;
8650 return btf__find_by_name_kind(btf, btf_type_name, kind);
8651}
8652
91abb4a6
AN
8653static inline int find_attach_btf_id(struct btf *btf, const char *name,
8654 enum bpf_attach_type attach_type)
a6ed02ca 8655{
67234743
AS
8656 const char *prefix;
8657 int kind;
a6ed02ca 8658
67234743
AS
8659 btf_get_kernel_prefix_kind(attach_type, &prefix, &kind);
8660 return find_btf_by_prefix_kind(btf, prefix, name, kind);
a6ed02ca
KS
8661}
8662
b8c54ea4
AS
8663int libbpf_find_vmlinux_btf_id(const char *name,
8664 enum bpf_attach_type attach_type)
12a8654b 8665{
a6ed02ca 8666 struct btf *btf;
3521ffa2 8667 int err;
12a8654b 8668
a710eed3 8669 btf = btf__load_vmlinux_btf();
e9fc3ce9
AN
8670 err = libbpf_get_error(btf);
8671 if (err) {
12a8654b 8672 pr_warn("vmlinux BTF is not found\n");
e9fc3ce9 8673 return libbpf_err(err);
12a8654b
AS
8674 }
8675
91abb4a6
AN
8676 err = find_attach_btf_id(btf, name, attach_type);
8677 if (err <= 0)
8678 pr_warn("%s is not found in vmlinux BTF\n", name);
8679
3521ffa2 8680 btf__free(btf);
e9fc3ce9 8681 return libbpf_err(err);
b8c54ea4
AS
8682}
8683
e7bf94db
AS
8684static int libbpf_find_prog_btf_id(const char *name, __u32 attach_prog_fd)
8685{
ebc7b50a
DM
8686 struct bpf_prog_info info = {};
8687 __u32 info_len = sizeof(info);
6cc93e2f 8688 struct btf *btf;
6d2d73cd 8689 int err;
e7bf94db 8690
ebc7b50a 8691 err = bpf_obj_get_info_by_fd(attach_prog_fd, &info, &info_len);
e9fc3ce9 8692 if (err) {
ebc7b50a
DM
8693 pr_warn("failed bpf_obj_get_info_by_fd for FD %d: %d\n",
8694 attach_prog_fd, err);
e9fc3ce9 8695 return err;
e7bf94db 8696 }
6d2d73cd
QM
8697
8698 err = -EINVAL;
ebc7b50a 8699 if (!info.btf_id) {
e7bf94db
AS
8700 pr_warn("The target program doesn't have BTF\n");
8701 goto out;
8702 }
ebc7b50a
DM
8703 btf = btf__load_from_kernel_by_id(info.btf_id);
8704 err = libbpf_get_error(btf);
8705 if (err) {
8706 pr_warn("Failed to get BTF %d of the program: %d\n", info.btf_id, err);
e7bf94db
AS
8707 goto out;
8708 }
8709 err = btf__find_by_name_kind(btf, name, BTF_KIND_FUNC);
8710 btf__free(btf);
8711 if (err <= 0) {
8712 pr_warn("%s is not found in prog's BTF\n", name);
8713 goto out;
8714 }
8715out:
e7bf94db
AS
8716 return err;
8717}
8718
91abb4a6
AN
8719static int find_kernel_btf_id(struct bpf_object *obj, const char *attach_name,
8720 enum bpf_attach_type attach_type,
8721 int *btf_obj_fd, int *btf_type_id)
8722{
8723 int ret, i;
8724
8725 ret = find_attach_btf_id(obj->btf_vmlinux, attach_name, attach_type);
8726 if (ret > 0) {
8727 *btf_obj_fd = 0; /* vmlinux BTF */
8728 *btf_type_id = ret;
8729 return 0;
8730 }
8731 if (ret != -ENOENT)
8732 return ret;
8733
8734 ret = load_module_btfs(obj);
8735 if (ret)
8736 return ret;
8737
8738 for (i = 0; i < obj->btf_module_cnt; i++) {
8739 const struct module_btf *mod = &obj->btf_modules[i];
8740
8741 ret = find_attach_btf_id(mod->btf, attach_name, attach_type);
8742 if (ret > 0) {
8743 *btf_obj_fd = mod->fd;
8744 *btf_type_id = ret;
8745 return 0;
8746 }
8747 if (ret == -ENOENT)
8748 continue;
8749
8750 return ret;
8751 }
8752
8753 return -ESRCH;
8754}
8755
15ea31fa
AN
8756static int libbpf_find_attach_btf_id(struct bpf_program *prog, const char *attach_name,
8757 int *btf_obj_fd, int *btf_type_id)
b8c54ea4 8758{
a6ed02ca
KS
8759 enum bpf_attach_type attach_type = prog->expected_attach_type;
8760 __u32 attach_prog_fd = prog->attach_prog_fd;
b6291a6f 8761 int err = 0;
b8c54ea4 8762
91abb4a6
AN
8763 /* BPF program's BTF ID */
8764 if (attach_prog_fd) {
8765 err = libbpf_find_prog_btf_id(attach_name, attach_prog_fd);
8766 if (err < 0) {
8767 pr_warn("failed to find BPF program (FD %d) BTF ID for '%s': %d\n",
8768 attach_prog_fd, attach_name, err);
8769 return err;
8770 }
8771 *btf_obj_fd = 0;
8772 *btf_type_id = err;
8773 return 0;
8774 }
8775
8776 /* kernel/module BTF ID */
67234743
AS
8777 if (prog->obj->gen_loader) {
8778 bpf_gen__record_attach_target(prog->obj->gen_loader, attach_name, attach_type);
8779 *btf_obj_fd = 0;
8780 *btf_type_id = 1;
8781 } else {
8782 err = find_kernel_btf_id(prog->obj, attach_name, attach_type, btf_obj_fd, btf_type_id);
8783 }
91abb4a6
AN
8784 if (err) {
8785 pr_warn("failed to find kernel BTF type ID of '%s': %d\n", attach_name, err);
b8c54ea4 8786 return err;
12a8654b 8787 }
91abb4a6 8788 return 0;
12a8654b
AS
8789}
8790
956b620f
AI
8791int libbpf_attach_type_by_name(const char *name,
8792 enum bpf_attach_type *attach_type)
8793{
c76e4c22 8794 char *type_names;
b6291a6f 8795 const struct bpf_sec_def *sec_def;
956b620f
AI
8796
8797 if (!name)
e9fc3ce9 8798 return libbpf_err(-EINVAL);
956b620f 8799
b6291a6f
AN
8800 sec_def = find_sec_def(name);
8801 if (!sec_def) {
8802 pr_debug("failed to guess attach type based on ELF section name '%s'\n", name);
8803 type_names = libbpf_get_type_names(true);
8804 if (type_names != NULL) {
8805 pr_debug("attachable section(type) names are:%s\n", type_names);
8806 free(type_names);
8807 }
8808
8809 return libbpf_err(-EINVAL);
c76e4c22
TS
8810 }
8811
15ea31fa
AN
8812 if (sec_def->preload_fn != libbpf_preload_prog)
8813 return libbpf_err(-EINVAL);
8814 if (!(sec_def->cookie & SEC_ATTACHABLE))
b6291a6f
AN
8815 return libbpf_err(-EINVAL);
8816
8817 *attach_type = sec_def->expected_attach_type;
8818 return 0;
956b620f
AI
8819}
8820
a324aae3 8821int bpf_map__fd(const struct bpf_map *map)
9d759a9b 8822{
e9fc3ce9 8823 return map ? map->fd : libbpf_err(-EINVAL);
9d759a9b
WN
8824}
8825
a324aae3 8826const struct bpf_map_def *bpf_map__def(const struct bpf_map *map)
9d759a9b 8827{
e9fc3ce9 8828 return map ? &map->def : libbpf_err_ptr(-EINVAL);
9d759a9b
WN
8829}
8830
aed65917
AN
8831static bool map_uses_real_name(const struct bpf_map *map)
8832{
8833 /* Since libbpf started to support custom .data.* and .rodata.* maps,
8834 * their user-visible name differs from kernel-visible name. Users see
8835 * such map's corresponding ELF section name as a map name.
8836 * This check distinguishes .data/.rodata from .data.* and .rodata.*
8837 * maps to know which name has to be returned to the user.
8838 */
8839 if (map->libbpf_type == LIBBPF_MAP_DATA && strcmp(map->real_name, DATA_SEC) != 0)
8840 return true;
8841 if (map->libbpf_type == LIBBPF_MAP_RODATA && strcmp(map->real_name, RODATA_SEC) != 0)
8842 return true;
8843 return false;
8844}
8845
a324aae3 8846const char *bpf_map__name(const struct bpf_map *map)
561bbcca 8847{
aed65917
AN
8848 if (!map)
8849 return NULL;
8850
8851 if (map_uses_real_name(map))
8852 return map->real_name;
8853
8854 return map->name;
561bbcca
WN
8855}
8856
1bdb6c9a
AN
8857enum bpf_map_type bpf_map__type(const struct bpf_map *map)
8858{
8859 return map->def.type;
8860}
8861
8862int bpf_map__set_type(struct bpf_map *map, enum bpf_map_type type)
8863{
8864 if (map->fd >= 0)
e9fc3ce9 8865 return libbpf_err(-EBUSY);
1bdb6c9a
AN
8866 map->def.type = type;
8867 return 0;
8868}
8869
8870__u32 bpf_map__map_flags(const struct bpf_map *map)
8871{
8872 return map->def.map_flags;
8873}
8874
8875int bpf_map__set_map_flags(struct bpf_map *map, __u32 flags)
8876{
8877 if (map->fd >= 0)
e9fc3ce9 8878 return libbpf_err(-EBUSY);
1bdb6c9a
AN
8879 map->def.map_flags = flags;
8880 return 0;
8881}
8882
8883__u32 bpf_map__numa_node(const struct bpf_map *map)
8884{
8885 return map->numa_node;
8886}
8887
8888int bpf_map__set_numa_node(struct bpf_map *map, __u32 numa_node)
8889{
8890 if (map->fd >= 0)
e9fc3ce9 8891 return libbpf_err(-EBUSY);
1bdb6c9a
AN
8892 map->numa_node = numa_node;
8893 return 0;
8894}
8895
8896__u32 bpf_map__key_size(const struct bpf_map *map)
8897{
8898 return map->def.key_size;
8899}
8900
8901int bpf_map__set_key_size(struct bpf_map *map, __u32 size)
8902{
8903 if (map->fd >= 0)
e9fc3ce9 8904 return libbpf_err(-EBUSY);
1bdb6c9a
AN
8905 map->def.key_size = size;
8906 return 0;
8907}
8908
8909__u32 bpf_map__value_size(const struct bpf_map *map)
8910{
8911 return map->def.value_size;
8912}
8913
8914int bpf_map__set_value_size(struct bpf_map *map, __u32 size)
8915{
8916 if (map->fd >= 0)
e9fc3ce9 8917 return libbpf_err(-EBUSY);
1bdb6c9a
AN
8918 map->def.value_size = size;
8919 return 0;
8920}
8921
5b891af7 8922__u32 bpf_map__btf_key_type_id(const struct bpf_map *map)
8a138aed 8923{
61746dbe 8924 return map ? map->btf_key_type_id : 0;
8a138aed
MKL
8925}
8926
5b891af7 8927__u32 bpf_map__btf_value_type_id(const struct bpf_map *map)
8a138aed 8928{
61746dbe 8929 return map ? map->btf_value_type_id : 0;
8a138aed
MKL
8930}
8931
edb13ed4
ACM
8932int bpf_map__set_priv(struct bpf_map *map, void *priv,
8933 bpf_map_clear_priv_t clear_priv)
9d759a9b
WN
8934{
8935 if (!map)
e9fc3ce9 8936 return libbpf_err(-EINVAL);
9d759a9b
WN
8937
8938 if (map->priv) {
8939 if (map->clear_priv)
8940 map->clear_priv(map, map->priv);
8941 }
8942
8943 map->priv = priv;
8944 map->clear_priv = clear_priv;
8945 return 0;
8946}
8947
a324aae3 8948void *bpf_map__priv(const struct bpf_map *map)
9d759a9b 8949{
e9fc3ce9 8950 return map ? map->priv : libbpf_err_ptr(-EINVAL);
9d759a9b
WN
8951}
8952
e2842be5
THJ
8953int bpf_map__set_initial_value(struct bpf_map *map,
8954 const void *data, size_t size)
8955{
8956 if (!map->mmaped || map->libbpf_type == LIBBPF_MAP_KCONFIG ||
8957 size != map->def.value_size || map->fd >= 0)
e9fc3ce9 8958 return libbpf_err(-EINVAL);
e2842be5
THJ
8959
8960 memcpy(map->mmaped, data, size);
8961 return 0;
8962}
8963
7723256b
AS
8964const void *bpf_map__initial_value(struct bpf_map *map, size_t *psize)
8965{
8966 if (!map->mmaped)
8967 return NULL;
8968 *psize = map->def.value_size;
8969 return map->mmaped;
8970}
8971
a324aae3 8972bool bpf_map__is_offload_neutral(const struct bpf_map *map)
f83fb22c
JK
8973{
8974 return map->def.type == BPF_MAP_TYPE_PERF_EVENT_ARRAY;
8975}
8976
a324aae3 8977bool bpf_map__is_internal(const struct bpf_map *map)
d859900c
DB
8978{
8979 return map->libbpf_type != LIBBPF_MAP_UNSPEC;
8980}
8981
1bdb6c9a
AN
8982__u32 bpf_map__ifindex(const struct bpf_map *map)
8983{
8984 return map->map_ifindex;
8985}
8986
8987int bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex)
9aba3613 8988{
1bdb6c9a 8989 if (map->fd >= 0)
e9fc3ce9 8990 return libbpf_err(-EBUSY);
9aba3613 8991 map->map_ifindex = ifindex;
1bdb6c9a 8992 return 0;
9aba3613
JK
8993}
8994
addb9fc9
NS
8995int bpf_map__set_inner_map_fd(struct bpf_map *map, int fd)
8996{
8997 if (!bpf_map_type__is_map_in_map(map->def.type)) {
be18010e 8998 pr_warn("error: unsupported map type\n");
e9fc3ce9 8999 return libbpf_err(-EINVAL);
addb9fc9
NS
9000 }
9001 if (map->inner_map_fd != -1) {
be18010e 9002 pr_warn("error: inner_map_fd already specified\n");
e9fc3ce9 9003 return libbpf_err(-EINVAL);
addb9fc9 9004 }
b3278099 9005 zfree(&map->inner_map);
addb9fc9
NS
9006 map->inner_map_fd = fd;
9007 return 0;
9008}
9009
0c19a9fb 9010static struct bpf_map *
a324aae3 9011__bpf_map__iter(const struct bpf_map *m, const struct bpf_object *obj, int i)
9d759a9b 9012{
0c19a9fb 9013 ssize_t idx;
9d759a9b
WN
9014 struct bpf_map *s, *e;
9015
9016 if (!obj || !obj->maps)
e9fc3ce9 9017 return errno = EINVAL, NULL;
9d759a9b
WN
9018
9019 s = obj->maps;
9020 e = obj->maps + obj->nr_maps;
9021
0c19a9fb 9022 if ((m < s) || (m >= e)) {
be18010e
KW
9023 pr_warn("error in %s: map handler doesn't belong to object\n",
9024 __func__);
e9fc3ce9 9025 return errno = EINVAL, NULL;
9d759a9b
WN
9026 }
9027
0c19a9fb
SF
9028 idx = (m - obj->maps) + i;
9029 if (idx >= obj->nr_maps || idx < 0)
9d759a9b
WN
9030 return NULL;
9031 return &obj->maps[idx];
9032}
561bbcca 9033
0c19a9fb 9034struct bpf_map *
a324aae3 9035bpf_map__next(const struct bpf_map *prev, const struct bpf_object *obj)
2088a3a7
HC
9036{
9037 return bpf_object__next_map(obj, prev);
9038}
9039
9040struct bpf_map *
9041bpf_object__next_map(const struct bpf_object *obj, const struct bpf_map *prev)
0c19a9fb
SF
9042{
9043 if (prev == NULL)
9044 return obj->maps;
9045
9046 return __bpf_map__iter(prev, obj, 1);
9047}
9048
9049struct bpf_map *
a324aae3 9050bpf_map__prev(const struct bpf_map *next, const struct bpf_object *obj)
2088a3a7
HC
9051{
9052 return bpf_object__prev_map(obj, next);
9053}
9054
9055struct bpf_map *
9056bpf_object__prev_map(const struct bpf_object *obj, const struct bpf_map *next)
0c19a9fb
SF
9057{
9058 if (next == NULL) {
9059 if (!obj->nr_maps)
9060 return NULL;
9061 return obj->maps + obj->nr_maps - 1;
9062 }
9063
9064 return __bpf_map__iter(next, obj, -1);
9065}
9066
561bbcca 9067struct bpf_map *
a324aae3 9068bpf_object__find_map_by_name(const struct bpf_object *obj, const char *name)
561bbcca
WN
9069{
9070 struct bpf_map *pos;
9071
f74a53d9 9072 bpf_object__for_each_map(pos, obj) {
26071635
AN
9073 /* if it's a special internal map name (which always starts
9074 * with dot) then check if that special name matches the
9075 * real map name (ELF section name)
9076 */
9077 if (name[0] == '.') {
9078 if (pos->real_name && strcmp(pos->real_name, name) == 0)
9079 return pos;
9080 continue;
9081 }
9082 /* otherwise map name has to be an exact match */
aed65917
AN
9083 if (map_uses_real_name(pos)) {
9084 if (strcmp(pos->real_name, name) == 0)
9085 return pos;
9086 continue;
9087 }
9088 if (strcmp(pos->name, name) == 0)
561bbcca
WN
9089 return pos;
9090 }
e9fc3ce9 9091 return errno = ENOENT, NULL;
561bbcca 9092}
5a6acad1 9093
f3cea32d 9094int
a324aae3 9095bpf_object__find_map_fd_by_name(const struct bpf_object *obj, const char *name)
f3cea32d
MF
9096{
9097 return bpf_map__fd(bpf_object__find_map_by_name(obj, name));
9098}
9099
5a6acad1
WN
9100struct bpf_map *
9101bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset)
9102{
e9fc3ce9 9103 return libbpf_err_ptr(-ENOTSUP);
5a6acad1 9104}
e28ff1a8
JS
9105
9106long libbpf_get_error(const void *ptr)
9107{
e9fc3ce9
AN
9108 if (!IS_ERR_OR_NULL(ptr))
9109 return 0;
9110
9111 if (IS_ERR(ptr))
9112 errno = -PTR_ERR(ptr);
9113
9114 /* If ptr == NULL, then errno should be already set by the failing
9115 * API, because libbpf never returns NULL on success and it now always
9116 * sets errno on error. So no extra errno handling for ptr == NULL
9117 * case.
9118 */
9119 return -errno;
e28ff1a8 9120}
6f6d33f3
JF
9121
9122int bpf_prog_load(const char *file, enum bpf_prog_type type,
9123 struct bpf_object **pobj, int *prog_fd)
d7be143b
AI
9124{
9125 struct bpf_prog_load_attr attr;
9126
9127 memset(&attr, 0, sizeof(struct bpf_prog_load_attr));
9128 attr.file = file;
9129 attr.prog_type = type;
9130 attr.expected_attach_type = 0;
9131
9132 return bpf_prog_load_xattr(&attr, pobj, prog_fd);
9133}
9134
9135int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,
9136 struct bpf_object **pobj, int *prog_fd)
6f6d33f3 9137{
33bae185 9138 struct bpf_object_open_attr open_attr = {};
48cca7e4 9139 struct bpf_program *prog, *first_prog = NULL;
6f6d33f3 9140 struct bpf_object *obj;
f0307a7e 9141 struct bpf_map *map;
6f6d33f3
JF
9142 int err;
9143
d7be143b 9144 if (!attr)
e9fc3ce9 9145 return libbpf_err(-EINVAL);
17387dd5 9146 if (!attr->file)
e9fc3ce9 9147 return libbpf_err(-EINVAL);
d7be143b 9148
33bae185
LY
9149 open_attr.file = attr->file;
9150 open_attr.prog_type = attr->prog_type;
9151
07f2d4ea 9152 obj = bpf_object__open_xattr(&open_attr);
e9fc3ce9
AN
9153 err = libbpf_get_error(obj);
9154 if (err)
9155 return libbpf_err(-ENOENT);
6f6d33f3 9156
48cca7e4 9157 bpf_object__for_each_program(prog, obj) {
dd4436bb 9158 enum bpf_attach_type attach_type = attr->expected_attach_type;
48cca7e4 9159 /*
dd4436bb
AN
9160 * to preserve backwards compatibility, bpf_prog_load treats
9161 * attr->prog_type, if specified, as an override to whatever
9162 * bpf_object__open guessed
48cca7e4 9163 */
dd4436bb
AN
9164 if (attr->prog_type != BPF_PROG_TYPE_UNSPEC) {
9165 bpf_program__set_type(prog, attr->prog_type);
9166 bpf_program__set_expected_attach_type(prog,
9167 attach_type);
9168 }
9169 if (bpf_program__get_type(prog) == BPF_PROG_TYPE_UNSPEC) {
9170 /*
9171 * we haven't guessed from section name and user
9172 * didn't provide a fallback type, too bad...
9173 */
9174 bpf_object__close(obj);
e9fc3ce9 9175 return libbpf_err(-EINVAL);
583c9009 9176 }
48cca7e4 9177
dd4436bb 9178 prog->prog_ifindex = attr->ifindex;
da11b417 9179 prog->log_level = attr->log_level;
2b288740 9180 prog->prog_flags |= attr->prog_flags;
69495d2a 9181 if (!first_prog)
48cca7e4
AS
9182 first_prog = prog;
9183 }
9184
f74a53d9 9185 bpf_object__for_each_map(map, obj) {
f83fb22c
JK
9186 if (!bpf_map__is_offload_neutral(map))
9187 map->map_ifindex = attr->ifindex;
f0307a7e
DB
9188 }
9189
48cca7e4 9190 if (!first_prog) {
be18010e 9191 pr_warn("object file doesn't contain bpf program\n");
48cca7e4 9192 bpf_object__close(obj);
e9fc3ce9 9193 return libbpf_err(-ENOENT);
583c9009
RG
9194 }
9195
6f6d33f3
JF
9196 err = bpf_object__load(obj);
9197 if (err) {
9198 bpf_object__close(obj);
e9fc3ce9 9199 return libbpf_err(err);
6f6d33f3
JF
9200 }
9201
9202 *pobj = obj;
48cca7e4 9203 *prog_fd = bpf_program__fd(first_prog);
6f6d33f3
JF
9204 return 0;
9205}
d0cabbb0 9206
1c2e9efc 9207struct bpf_link {
d6958706 9208 int (*detach)(struct bpf_link *link);
d88b71d4 9209 void (*dealloc)(struct bpf_link *link);
c016b68e
AN
9210 char *pin_path; /* NULL, if not pinned */
9211 int fd; /* hook FD, -1 if not applicable */
d6958706 9212 bool disconnected;
1c2e9efc
AN
9213};
9214
cc4f864b
AN
9215/* Replace link's underlying BPF program with the new one */
9216int bpf_link__update_program(struct bpf_link *link, struct bpf_program *prog)
9217{
e9fc3ce9 9218 int ret;
c139e40a 9219
e9fc3ce9
AN
9220 ret = bpf_link_update(bpf_link__fd(link), bpf_program__fd(prog), NULL);
9221 return libbpf_err_errno(ret);
cc4f864b
AN
9222}
9223
d6958706
AN
9224/* Release "ownership" of underlying BPF resource (typically, BPF program
9225 * attached to some BPF hook, e.g., tracepoint, kprobe, etc). Disconnected
9226 * link, when destructed through bpf_link__destroy() call won't attempt to
9227 * detach/unregisted that BPF resource. This is useful in situations where,
9228 * say, attached BPF program has to outlive userspace program that attached it
9229 * in the system. Depending on type of BPF program, though, there might be
9230 * additional steps (like pinning BPF program in BPF FS) necessary to ensure
9231 * exit of userspace program doesn't trigger automatic detachment and clean up
9232 * inside the kernel.
9233 */
9234void bpf_link__disconnect(struct bpf_link *link)
9235{
9236 link->disconnected = true;
9237}
9238
1c2e9efc
AN
9239int bpf_link__destroy(struct bpf_link *link)
9240{
d6958706 9241 int err = 0;
1c2e9efc 9242
50450fc7 9243 if (IS_ERR_OR_NULL(link))
1c2e9efc
AN
9244 return 0;
9245
d6958706
AN
9246 if (!link->disconnected && link->detach)
9247 err = link->detach(link);
c016b68e
AN
9248 if (link->pin_path)
9249 free(link->pin_path);
d88b71d4
AN
9250 if (link->dealloc)
9251 link->dealloc(link);
9252 else
9253 free(link);
1c2e9efc 9254
e9fc3ce9 9255 return libbpf_err(err);
1c2e9efc
AN
9256}
9257
c016b68e
AN
9258int bpf_link__fd(const struct bpf_link *link)
9259{
9260 return link->fd;
9261}
9262
9263const char *bpf_link__pin_path(const struct bpf_link *link)
9264{
9265 return link->pin_path;
9266}
9267
9268static int bpf_link__detach_fd(struct bpf_link *link)
9269{
e9fc3ce9 9270 return libbpf_err_errno(close(link->fd));
c016b68e
AN
9271}
9272
9273struct bpf_link *bpf_link__open(const char *path)
9274{
9275 struct bpf_link *link;
9276 int fd;
9277
9278 fd = bpf_obj_get(path);
9279 if (fd < 0) {
9280 fd = -errno;
9281 pr_warn("failed to open link at %s: %d\n", path, fd);
e9fc3ce9 9282 return libbpf_err_ptr(fd);
c016b68e
AN
9283 }
9284
9285 link = calloc(1, sizeof(*link));
9286 if (!link) {
9287 close(fd);
e9fc3ce9 9288 return libbpf_err_ptr(-ENOMEM);
c016b68e
AN
9289 }
9290 link->detach = &bpf_link__detach_fd;
9291 link->fd = fd;
9292
9293 link->pin_path = strdup(path);
9294 if (!link->pin_path) {
9295 bpf_link__destroy(link);
e9fc3ce9 9296 return libbpf_err_ptr(-ENOMEM);
c016b68e
AN
9297 }
9298
9299 return link;
9300}
9301
2e49527e
AN
9302int bpf_link__detach(struct bpf_link *link)
9303{
9304 return bpf_link_detach(link->fd) ? -errno : 0;
9305}
9306
c016b68e
AN
9307int bpf_link__pin(struct bpf_link *link, const char *path)
9308{
9309 int err;
9310
9311 if (link->pin_path)
e9fc3ce9 9312 return libbpf_err(-EBUSY);
c016b68e
AN
9313 err = make_parent_dir(path);
9314 if (err)
e9fc3ce9 9315 return libbpf_err(err);
c016b68e
AN
9316 err = check_path(path);
9317 if (err)
e9fc3ce9 9318 return libbpf_err(err);
c016b68e
AN
9319
9320 link->pin_path = strdup(path);
9321 if (!link->pin_path)
e9fc3ce9 9322 return libbpf_err(-ENOMEM);
c016b68e
AN
9323
9324 if (bpf_obj_pin(link->fd, link->pin_path)) {
9325 err = -errno;
9326 zfree(&link->pin_path);
e9fc3ce9 9327 return libbpf_err(err);
c016b68e
AN
9328 }
9329
9330 pr_debug("link fd=%d: pinned at %s\n", link->fd, link->pin_path);
9331 return 0;
9332}
9333
9334int bpf_link__unpin(struct bpf_link *link)
9335{
9336 int err;
9337
9338 if (!link->pin_path)
e9fc3ce9 9339 return libbpf_err(-EINVAL);
c016b68e
AN
9340
9341 err = unlink(link->pin_path);
9342 if (err != 0)
af0efa05 9343 return -errno;
c016b68e
AN
9344
9345 pr_debug("link fd=%d: unpinned from %s\n", link->fd, link->pin_path);
9346 zfree(&link->pin_path);
9347 return 0;
9348}
63f2f5ee 9349
668ace0e
AN
9350struct bpf_link_perf {
9351 struct bpf_link link;
9352 int perf_event_fd;
ca304b40
RDT
9353 /* legacy kprobe support: keep track of probe identifier and type */
9354 char *legacy_probe_name;
46ed5fc3 9355 bool legacy_is_kprobe;
ca304b40 9356 bool legacy_is_retprobe;
668ace0e
AN
9357};
9358
46ed5fc3 9359static int remove_kprobe_event_legacy(const char *probe_name, bool retprobe);
cc10623c 9360static int remove_uprobe_event_legacy(const char *probe_name, bool retprobe);
46ed5fc3 9361
668ace0e 9362static int bpf_link_perf_detach(struct bpf_link *link)
63f2f5ee 9363{
668ace0e
AN
9364 struct bpf_link_perf *perf_link = container_of(link, struct bpf_link_perf, link);
9365 int err = 0;
63f2f5ee 9366
668ace0e 9367 if (ioctl(perf_link->perf_event_fd, PERF_EVENT_IOC_DISABLE, 0) < 0)
63f2f5ee
AN
9368 err = -errno;
9369
668ace0e
AN
9370 if (perf_link->perf_event_fd != link->fd)
9371 close(perf_link->perf_event_fd);
c016b68e 9372 close(link->fd);
668ace0e 9373
cc10623c 9374 /* legacy uprobe/kprobe needs to be removed after perf event fd closure */
46ed5fc3
AN
9375 if (perf_link->legacy_probe_name) {
9376 if (perf_link->legacy_is_kprobe) {
9377 err = remove_kprobe_event_legacy(perf_link->legacy_probe_name,
9378 perf_link->legacy_is_retprobe);
cc10623c
AN
9379 } else {
9380 err = remove_uprobe_event_legacy(perf_link->legacy_probe_name,
9381 perf_link->legacy_is_retprobe);
46ed5fc3
AN
9382 }
9383 }
ca304b40
RDT
9384
9385 return err;
63f2f5ee
AN
9386}
9387
668ace0e
AN
9388static void bpf_link_perf_dealloc(struct bpf_link *link)
9389{
9390 struct bpf_link_perf *perf_link = container_of(link, struct bpf_link_perf, link);
9391
ca304b40 9392 free(perf_link->legacy_probe_name);
668ace0e
AN
9393 free(perf_link);
9394}
9395
942025c9 9396struct bpf_link *bpf_program__attach_perf_event_opts(const struct bpf_program *prog, int pfd,
47faff37 9397 const struct bpf_perf_event_opts *opts)
63f2f5ee
AN
9398{
9399 char errmsg[STRERR_BUFSIZE];
668ace0e
AN
9400 struct bpf_link_perf *link;
9401 int prog_fd, link_fd = -1, err;
63f2f5ee 9402
47faff37
AN
9403 if (!OPTS_VALID(opts, bpf_perf_event_opts))
9404 return libbpf_err_ptr(-EINVAL);
9405
63f2f5ee 9406 if (pfd < 0) {
52109584
AN
9407 pr_warn("prog '%s': invalid perf event FD %d\n",
9408 prog->name, pfd);
e9fc3ce9 9409 return libbpf_err_ptr(-EINVAL);
63f2f5ee
AN
9410 }
9411 prog_fd = bpf_program__fd(prog);
9412 if (prog_fd < 0) {
52109584
AN
9413 pr_warn("prog '%s': can't attach BPF program w/o FD (did you load it?)\n",
9414 prog->name);
e9fc3ce9 9415 return libbpf_err_ptr(-EINVAL);
63f2f5ee
AN
9416 }
9417
d6958706 9418 link = calloc(1, sizeof(*link));
63f2f5ee 9419 if (!link)
e9fc3ce9 9420 return libbpf_err_ptr(-ENOMEM);
668ace0e
AN
9421 link->link.detach = &bpf_link_perf_detach;
9422 link->link.dealloc = &bpf_link_perf_dealloc;
9423 link->perf_event_fd = pfd;
63f2f5ee 9424
668ace0e 9425 if (kernel_supports(prog->obj, FEAT_PERF_LINK)) {
47faff37
AN
9426 DECLARE_LIBBPF_OPTS(bpf_link_create_opts, link_opts,
9427 .perf_event.bpf_cookie = OPTS_GET(opts, bpf_cookie, 0));
9428
9429 link_fd = bpf_link_create(prog_fd, pfd, BPF_PERF_EVENT, &link_opts);
668ace0e
AN
9430 if (link_fd < 0) {
9431 err = -errno;
9432 pr_warn("prog '%s': failed to create BPF link for perf_event FD %d: %d (%s)\n",
9433 prog->name, pfd,
9434 err, libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
9435 goto err_out;
9436 }
9437 link->link.fd = link_fd;
9438 } else {
47faff37
AN
9439 if (OPTS_GET(opts, bpf_cookie, 0)) {
9440 pr_warn("prog '%s': user context value is not supported\n", prog->name);
9441 err = -EOPNOTSUPP;
9442 goto err_out;
9443 }
9444
668ace0e
AN
9445 if (ioctl(pfd, PERF_EVENT_IOC_SET_BPF, prog_fd) < 0) {
9446 err = -errno;
9447 pr_warn("prog '%s': failed to attach to perf_event FD %d: %s\n",
9448 prog->name, pfd, libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
9449 if (err == -EPROTO)
9450 pr_warn("prog '%s': try add PERF_SAMPLE_CALLCHAIN to or remove exclude_callchain_[kernel|user] from pfd %d\n",
9451 prog->name, pfd);
9452 goto err_out;
9453 }
9454 link->link.fd = pfd;
63f2f5ee
AN
9455 }
9456 if (ioctl(pfd, PERF_EVENT_IOC_ENABLE, 0) < 0) {
9457 err = -errno;
668ace0e 9458 pr_warn("prog '%s': failed to enable perf_event FD %d: %s\n",
52109584 9459 prog->name, pfd, libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
668ace0e 9460 goto err_out;
63f2f5ee 9461 }
668ace0e
AN
9462
9463 return &link->link;
9464err_out:
9465 if (link_fd >= 0)
9466 close(link_fd);
9467 free(link);
9468 return libbpf_err_ptr(err);
63f2f5ee
AN
9469}
9470
942025c9 9471struct bpf_link *bpf_program__attach_perf_event(const struct bpf_program *prog, int pfd)
47faff37
AN
9472{
9473 return bpf_program__attach_perf_event_opts(prog, pfd, NULL);
9474}
9475
b2650027
AN
9476/*
9477 * this function is expected to parse integer in the range of [0, 2^31-1] from
9478 * given file using scanf format string fmt. If actual parsed value is
9479 * negative, the result might be indistinguishable from error
9480 */
9481static int parse_uint_from_file(const char *file, const char *fmt)
9482{
9483 char buf[STRERR_BUFSIZE];
9484 int err, ret;
9485 FILE *f;
9486
9487 f = fopen(file, "r");
9488 if (!f) {
9489 err = -errno;
9490 pr_debug("failed to open '%s': %s\n", file,
9491 libbpf_strerror_r(err, buf, sizeof(buf)));
9492 return err;
9493 }
9494 err = fscanf(f, fmt, &ret);
9495 if (err != 1) {
9496 err = err == EOF ? -EIO : -errno;
9497 pr_debug("failed to parse '%s': %s\n", file,
9498 libbpf_strerror_r(err, buf, sizeof(buf)));
9499 fclose(f);
9500 return err;
9501 }
9502 fclose(f);
9503 return ret;
9504}
9505
9506static int determine_kprobe_perf_type(void)
9507{
9508 const char *file = "/sys/bus/event_source/devices/kprobe/type";
9509
9510 return parse_uint_from_file(file, "%d\n");
9511}
9512
9513static int determine_uprobe_perf_type(void)
9514{
9515 const char *file = "/sys/bus/event_source/devices/uprobe/type";
9516
9517 return parse_uint_from_file(file, "%d\n");
9518}
9519
9520static int determine_kprobe_retprobe_bit(void)
9521{
9522 const char *file = "/sys/bus/event_source/devices/kprobe/format/retprobe";
9523
9524 return parse_uint_from_file(file, "config:%d\n");
9525}
9526
9527static int determine_uprobe_retprobe_bit(void)
9528{
9529 const char *file = "/sys/bus/event_source/devices/uprobe/format/retprobe";
9530
9531 return parse_uint_from_file(file, "config:%d\n");
9532}
9533
5e3b8356
AN
9534#define PERF_UPROBE_REF_CTR_OFFSET_BITS 32
9535#define PERF_UPROBE_REF_CTR_OFFSET_SHIFT 32
9536
b2650027 9537static int perf_event_open_probe(bool uprobe, bool retprobe, const char *name,
5e3b8356 9538 uint64_t offset, int pid, size_t ref_ctr_off)
b2650027
AN
9539{
9540 struct perf_event_attr attr = {};
9541 char errmsg[STRERR_BUFSIZE];
9542 int type, pfd, err;
9543
5e3b8356
AN
9544 if (ref_ctr_off >= (1ULL << PERF_UPROBE_REF_CTR_OFFSET_BITS))
9545 return -EINVAL;
9546
b2650027
AN
9547 type = uprobe ? determine_uprobe_perf_type()
9548 : determine_kprobe_perf_type();
9549 if (type < 0) {
be18010e
KW
9550 pr_warn("failed to determine %s perf type: %s\n",
9551 uprobe ? "uprobe" : "kprobe",
9552 libbpf_strerror_r(type, errmsg, sizeof(errmsg)));
b2650027
AN
9553 return type;
9554 }
9555 if (retprobe) {
9556 int bit = uprobe ? determine_uprobe_retprobe_bit()
9557 : determine_kprobe_retprobe_bit();
9558
9559 if (bit < 0) {
be18010e
KW
9560 pr_warn("failed to determine %s retprobe bit: %s\n",
9561 uprobe ? "uprobe" : "kprobe",
9562 libbpf_strerror_r(bit, errmsg, sizeof(errmsg)));
b2650027
AN
9563 return bit;
9564 }
9565 attr.config |= 1 << bit;
9566 }
9567 attr.size = sizeof(attr);
9568 attr.type = type;
5e3b8356 9569 attr.config |= (__u64)ref_ctr_off << PERF_UPROBE_REF_CTR_OFFSET_SHIFT;
36db2a94
AN
9570 attr.config1 = ptr_to_u64(name); /* kprobe_func or uprobe_path */
9571 attr.config2 = offset; /* kprobe_addr or probe_offset */
b2650027
AN
9572
9573 /* pid filter is meaningful only for uprobes */
9574 pfd = syscall(__NR_perf_event_open, &attr,
9575 pid < 0 ? -1 : pid /* pid */,
9576 pid == -1 ? 0 : -1 /* cpu */,
9577 -1 /* group_fd */, PERF_FLAG_FD_CLOEXEC);
9578 if (pfd < 0) {
9579 err = -errno;
be18010e
KW
9580 pr_warn("%s perf_event_open() failed: %s\n",
9581 uprobe ? "uprobe" : "kprobe",
9582 libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
b2650027
AN
9583 return err;
9584 }
9585 return pfd;
9586}
9587
46ed5fc3
AN
9588static int append_to_file(const char *file, const char *fmt, ...)
9589{
9590 int fd, n, err = 0;
9591 va_list ap;
9592
9593 fd = open(file, O_WRONLY | O_APPEND, 0);
9594 if (fd < 0)
9595 return -errno;
9596
9597 va_start(ap, fmt);
9598 n = vdprintf(fd, fmt, ap);
9599 va_end(ap);
9600
9601 if (n < 0)
9602 err = -errno;
9603
9604 close(fd);
9605 return err;
9606}
9607
9608static void gen_kprobe_legacy_event_name(char *buf, size_t buf_sz,
9609 const char *kfunc_name, size_t offset)
9610{
9611 snprintf(buf, buf_sz, "libbpf_%u_%s_0x%zx", getpid(), kfunc_name, offset);
9612}
9613
9614static int add_kprobe_event_legacy(const char *probe_name, bool retprobe,
9615 const char *kfunc_name, size_t offset)
9616{
9617 const char *file = "/sys/kernel/debug/tracing/kprobe_events";
9618
9619 return append_to_file(file, "%c:%s/%s %s+0x%zx",
9620 retprobe ? 'r' : 'p',
9621 retprobe ? "kretprobes" : "kprobes",
9622 probe_name, kfunc_name, offset);
9623}
9624
9625static int remove_kprobe_event_legacy(const char *probe_name, bool retprobe)
9626{
9627 const char *file = "/sys/kernel/debug/tracing/kprobe_events";
9628
9629 return append_to_file(file, "-:%s/%s", retprobe ? "kretprobes" : "kprobes", probe_name);
9630}
9631
9632static int determine_kprobe_perf_type_legacy(const char *probe_name, bool retprobe)
9633{
9634 char file[256];
9635
9636 snprintf(file, sizeof(file),
9637 "/sys/kernel/debug/tracing/events/%s/%s/id",
9638 retprobe ? "kretprobes" : "kprobes", probe_name);
9639
9640 return parse_uint_from_file(file, "%d\n");
9641}
9642
9643static int perf_event_kprobe_open_legacy(const char *probe_name, bool retprobe,
9644 const char *kfunc_name, size_t offset, int pid)
ca304b40
RDT
9645{
9646 struct perf_event_attr attr = {};
9647 char errmsg[STRERR_BUFSIZE];
9648 int type, pfd, err;
9649
46ed5fc3 9650 err = add_kprobe_event_legacy(probe_name, retprobe, kfunc_name, offset);
ca304b40 9651 if (err < 0) {
46ed5fc3
AN
9652 pr_warn("failed to add legacy kprobe event for '%s+0x%zx': %s\n",
9653 kfunc_name, offset,
ca304b40
RDT
9654 libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
9655 return err;
9656 }
46ed5fc3 9657 type = determine_kprobe_perf_type_legacy(probe_name, retprobe);
ca304b40 9658 if (type < 0) {
46ed5fc3
AN
9659 pr_warn("failed to determine legacy kprobe event id for '%s+0x%zx': %s\n",
9660 kfunc_name, offset,
ca304b40
RDT
9661 libbpf_strerror_r(type, errmsg, sizeof(errmsg)));
9662 return type;
9663 }
9664 attr.size = sizeof(attr);
9665 attr.config = type;
9666 attr.type = PERF_TYPE_TRACEPOINT;
9667
9668 pfd = syscall(__NR_perf_event_open, &attr,
9669 pid < 0 ? -1 : pid, /* pid */
9670 pid == -1 ? 0 : -1, /* cpu */
9671 -1 /* group_fd */, PERF_FLAG_FD_CLOEXEC);
9672 if (pfd < 0) {
9673 err = -errno;
9674 pr_warn("legacy kprobe perf_event_open() failed: %s\n",
9675 libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
9676 return err;
9677 }
9678 return pfd;
9679}
9680
da97553e 9681struct bpf_link *
942025c9 9682bpf_program__attach_kprobe_opts(const struct bpf_program *prog,
ac0ed488 9683 const char *func_name,
47faff37 9684 const struct bpf_kprobe_opts *opts)
b2650027 9685{
47faff37 9686 DECLARE_LIBBPF_OPTS(bpf_perf_event_opts, pe_opts);
b2650027 9687 char errmsg[STRERR_BUFSIZE];
ca304b40 9688 char *legacy_probe = NULL;
b2650027 9689 struct bpf_link *link;
46ed5fc3 9690 size_t offset;
ca304b40 9691 bool retprobe, legacy;
b2650027
AN
9692 int pfd, err;
9693
da97553e
JO
9694 if (!OPTS_VALID(opts, bpf_kprobe_opts))
9695 return libbpf_err_ptr(-EINVAL);
9696
9697 retprobe = OPTS_GET(opts, retprobe, false);
9698 offset = OPTS_GET(opts, offset, 0);
47faff37 9699 pe_opts.bpf_cookie = OPTS_GET(opts, bpf_cookie, 0);
da97553e 9700
ca304b40
RDT
9701 legacy = determine_kprobe_perf_type() < 0;
9702 if (!legacy) {
9703 pfd = perf_event_open_probe(false /* uprobe */, retprobe,
9704 func_name, offset,
9705 -1 /* pid */, 0 /* ref_ctr_off */);
9706 } else {
46ed5fc3
AN
9707 char probe_name[256];
9708
9709 gen_kprobe_legacy_event_name(probe_name, sizeof(probe_name),
9710 func_name, offset);
9711
ca304b40
RDT
9712 legacy_probe = strdup(func_name);
9713 if (!legacy_probe)
9714 return libbpf_err_ptr(-ENOMEM);
9715
46ed5fc3 9716 pfd = perf_event_kprobe_open_legacy(legacy_probe, retprobe, func_name,
ca304b40
RDT
9717 offset, -1 /* pid */);
9718 }
b2650027 9719 if (pfd < 0) {
46ed5fc3
AN
9720 err = -errno;
9721 pr_warn("prog '%s': failed to create %s '%s+0x%zx' perf event: %s\n",
9722 prog->name, retprobe ? "kretprobe" : "kprobe",
9723 func_name, offset,
303a2572
AN
9724 libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
9725 goto err_out;
b2650027 9726 }
47faff37 9727 link = bpf_program__attach_perf_event_opts(prog, pfd, &pe_opts);
e9fc3ce9
AN
9728 err = libbpf_get_error(link);
9729 if (err) {
b2650027 9730 close(pfd);
46ed5fc3
AN
9731 pr_warn("prog '%s': failed to attach to %s '%s+0x%zx': %s\n",
9732 prog->name, retprobe ? "kretprobe" : "kprobe",
9733 func_name, offset,
be18010e 9734 libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
303a2572 9735 goto err_out;
b2650027 9736 }
ca304b40
RDT
9737 if (legacy) {
9738 struct bpf_link_perf *perf_link = container_of(link, struct bpf_link_perf, link);
9739
9740 perf_link->legacy_probe_name = legacy_probe;
46ed5fc3 9741 perf_link->legacy_is_kprobe = true;
ca304b40
RDT
9742 perf_link->legacy_is_retprobe = retprobe;
9743 }
9744
b2650027 9745 return link;
303a2572
AN
9746err_out:
9747 free(legacy_probe);
9748 return libbpf_err_ptr(err);
b2650027
AN
9749}
9750
942025c9 9751struct bpf_link *bpf_program__attach_kprobe(const struct bpf_program *prog,
ac0ed488
JO
9752 bool retprobe,
9753 const char *func_name)
9754{
da97553e 9755 DECLARE_LIBBPF_OPTS(bpf_kprobe_opts, opts,
ac0ed488 9756 .retprobe = retprobe,
da97553e 9757 );
ac0ed488
JO
9758
9759 return bpf_program__attach_kprobe_opts(prog, func_name, &opts);
9760}
9761
12d9466d 9762static struct bpf_link *attach_kprobe(const struct bpf_program *prog, long cookie)
d7a18ea7 9763{
da97553e 9764 DECLARE_LIBBPF_OPTS(bpf_kprobe_opts, opts);
a2488b5f
AM
9765 unsigned long offset = 0;
9766 struct bpf_link *link;
d7a18ea7 9767 const char *func_name;
a2488b5f
AM
9768 char *func;
9769 int n, err;
d7a18ea7 9770
13d35a0c
AN
9771 opts.retprobe = str_has_pfx(prog->sec_name, "kretprobe/");
9772 if (opts.retprobe)
9773 func_name = prog->sec_name + sizeof("kretprobe/") - 1;
9774 else
9775 func_name = prog->sec_name + sizeof("kprobe/") - 1;
d7a18ea7 9776
e3f9bc35 9777 n = sscanf(func_name, "%m[a-zA-Z0-9_.]+%li", &func, &offset);
a2488b5f
AM
9778 if (n < 1) {
9779 err = -EINVAL;
9780 pr_warn("kprobe name is invalid: %s\n", func_name);
9781 return libbpf_err_ptr(err);
9782 }
9783 if (opts.retprobe && offset != 0) {
1f71a468 9784 free(func);
a2488b5f
AM
9785 err = -EINVAL;
9786 pr_warn("kretprobes do not support offset specification\n");
9787 return libbpf_err_ptr(err);
9788 }
d7a18ea7 9789
a2488b5f
AM
9790 opts.offset = offset;
9791 link = bpf_program__attach_kprobe_opts(prog, func, &opts);
9792 free(func);
9793 return link;
d7a18ea7
AN
9794}
9795
cc10623c
AN
9796static void gen_uprobe_legacy_event_name(char *buf, size_t buf_sz,
9797 const char *binary_path, uint64_t offset)
9798{
9799 int i;
9800
9801 snprintf(buf, buf_sz, "libbpf_%u_%s_0x%zx", getpid(), binary_path, (size_t)offset);
9802
9803 /* sanitize binary_path in the probe name */
9804 for (i = 0; buf[i]; i++) {
9805 if (!isalnum(buf[i]))
9806 buf[i] = '_';
9807 }
9808}
9809
9810static inline int add_uprobe_event_legacy(const char *probe_name, bool retprobe,
9811 const char *binary_path, size_t offset)
9812{
9813 const char *file = "/sys/kernel/debug/tracing/uprobe_events";
9814
9815 return append_to_file(file, "%c:%s/%s %s:0x%zx",
9816 retprobe ? 'r' : 'p',
9817 retprobe ? "uretprobes" : "uprobes",
9818 probe_name, binary_path, offset);
9819}
9820
9821static inline int remove_uprobe_event_legacy(const char *probe_name, bool retprobe)
9822{
9823 const char *file = "/sys/kernel/debug/tracing/uprobe_events";
9824
9825 return append_to_file(file, "-:%s/%s", retprobe ? "uretprobes" : "uprobes", probe_name);
9826}
9827
9828static int determine_uprobe_perf_type_legacy(const char *probe_name, bool retprobe)
9829{
9830 char file[512];
9831
9832 snprintf(file, sizeof(file),
9833 "/sys/kernel/debug/tracing/events/%s/%s/id",
9834 retprobe ? "uretprobes" : "uprobes", probe_name);
9835
9836 return parse_uint_from_file(file, "%d\n");
9837}
9838
9839static int perf_event_uprobe_open_legacy(const char *probe_name, bool retprobe,
9840 const char *binary_path, size_t offset, int pid)
9841{
9842 struct perf_event_attr attr;
9843 int type, pfd, err;
9844
9845 err = add_uprobe_event_legacy(probe_name, retprobe, binary_path, offset);
9846 if (err < 0) {
9847 pr_warn("failed to add legacy uprobe event for %s:0x%zx: %d\n",
9848 binary_path, (size_t)offset, err);
9849 return err;
9850 }
9851 type = determine_uprobe_perf_type_legacy(probe_name, retprobe);
9852 if (type < 0) {
9853 pr_warn("failed to determine legacy uprobe event id for %s:0x%zx: %d\n",
9854 binary_path, offset, err);
9855 return type;
9856 }
9857
9858 memset(&attr, 0, sizeof(attr));
9859 attr.size = sizeof(attr);
9860 attr.config = type;
9861 attr.type = PERF_TYPE_TRACEPOINT;
9862
9863 pfd = syscall(__NR_perf_event_open, &attr,
9864 pid < 0 ? -1 : pid, /* pid */
9865 pid == -1 ? 0 : -1, /* cpu */
9866 -1 /* group_fd */, PERF_FLAG_FD_CLOEXEC);
9867 if (pfd < 0) {
9868 err = -errno;
9869 pr_warn("legacy uprobe perf_event_open() failed: %d\n", err);
9870 return err;
9871 }
9872 return pfd;
9873}
9874
47faff37 9875LIBBPF_API struct bpf_link *
942025c9 9876bpf_program__attach_uprobe_opts(const struct bpf_program *prog, pid_t pid,
47faff37
AN
9877 const char *binary_path, size_t func_offset,
9878 const struct bpf_uprobe_opts *opts)
b2650027 9879{
47faff37 9880 DECLARE_LIBBPF_OPTS(bpf_perf_event_opts, pe_opts);
cc10623c 9881 char errmsg[STRERR_BUFSIZE], *legacy_probe = NULL;
b2650027 9882 struct bpf_link *link;
5e3b8356 9883 size_t ref_ctr_off;
b2650027 9884 int pfd, err;
cc10623c 9885 bool retprobe, legacy;
47faff37
AN
9886
9887 if (!OPTS_VALID(opts, bpf_uprobe_opts))
9888 return libbpf_err_ptr(-EINVAL);
9889
9890 retprobe = OPTS_GET(opts, retprobe, false);
5e3b8356 9891 ref_ctr_off = OPTS_GET(opts, ref_ctr_offset, 0);
47faff37 9892 pe_opts.bpf_cookie = OPTS_GET(opts, bpf_cookie, 0);
b2650027 9893
cc10623c
AN
9894 legacy = determine_uprobe_perf_type() < 0;
9895 if (!legacy) {
9896 pfd = perf_event_open_probe(true /* uprobe */, retprobe, binary_path,
9897 func_offset, pid, ref_ctr_off);
9898 } else {
9899 char probe_name[512];
9900
9901 if (ref_ctr_off)
9902 return libbpf_err_ptr(-EINVAL);
9903
9904 gen_uprobe_legacy_event_name(probe_name, sizeof(probe_name),
9905 binary_path, func_offset);
9906
9907 legacy_probe = strdup(probe_name);
9908 if (!legacy_probe)
9909 return libbpf_err_ptr(-ENOMEM);
9910
9911 pfd = perf_event_uprobe_open_legacy(legacy_probe, retprobe,
9912 binary_path, func_offset, pid);
9913 }
b2650027 9914 if (pfd < 0) {
cc10623c 9915 err = -errno;
52109584
AN
9916 pr_warn("prog '%s': failed to create %s '%s:0x%zx' perf event: %s\n",
9917 prog->name, retprobe ? "uretprobe" : "uprobe",
be18010e 9918 binary_path, func_offset,
cc10623c
AN
9919 libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
9920 goto err_out;
b2650027 9921 }
cc10623c 9922
47faff37 9923 link = bpf_program__attach_perf_event_opts(prog, pfd, &pe_opts);
e9fc3ce9
AN
9924 err = libbpf_get_error(link);
9925 if (err) {
b2650027 9926 close(pfd);
52109584
AN
9927 pr_warn("prog '%s': failed to attach to %s '%s:0x%zx': %s\n",
9928 prog->name, retprobe ? "uretprobe" : "uprobe",
be18010e
KW
9929 binary_path, func_offset,
9930 libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
cc10623c
AN
9931 goto err_out;
9932 }
9933 if (legacy) {
9934 struct bpf_link_perf *perf_link = container_of(link, struct bpf_link_perf, link);
9935
9936 perf_link->legacy_probe_name = legacy_probe;
9937 perf_link->legacy_is_kprobe = false;
9938 perf_link->legacy_is_retprobe = retprobe;
b2650027
AN
9939 }
9940 return link;
cc10623c
AN
9941err_out:
9942 free(legacy_probe);
9943 return libbpf_err_ptr(err);
9944
b2650027
AN
9945}
9946
942025c9 9947struct bpf_link *bpf_program__attach_uprobe(const struct bpf_program *prog,
47faff37
AN
9948 bool retprobe, pid_t pid,
9949 const char *binary_path,
9950 size_t func_offset)
9951{
9952 DECLARE_LIBBPF_OPTS(bpf_uprobe_opts, opts, .retprobe = retprobe);
9953
9954 return bpf_program__attach_uprobe_opts(prog, pid, binary_path, func_offset, &opts);
9955}
9956
f6de59c1
AN
9957static int determine_tracepoint_id(const char *tp_category,
9958 const char *tp_name)
9959{
9960 char file[PATH_MAX];
9961 int ret;
9962
9963 ret = snprintf(file, sizeof(file),
9964 "/sys/kernel/debug/tracing/events/%s/%s/id",
9965 tp_category, tp_name);
9966 if (ret < 0)
9967 return -errno;
9968 if (ret >= sizeof(file)) {
9969 pr_debug("tracepoint %s/%s path is too long\n",
9970 tp_category, tp_name);
9971 return -E2BIG;
9972 }
9973 return parse_uint_from_file(file, "%d\n");
9974}
9975
9976static int perf_event_open_tracepoint(const char *tp_category,
9977 const char *tp_name)
9978{
9979 struct perf_event_attr attr = {};
9980 char errmsg[STRERR_BUFSIZE];
9981 int tp_id, pfd, err;
9982
9983 tp_id = determine_tracepoint_id(tp_category, tp_name);
9984 if (tp_id < 0) {
be18010e
KW
9985 pr_warn("failed to determine tracepoint '%s/%s' perf event ID: %s\n",
9986 tp_category, tp_name,
9987 libbpf_strerror_r(tp_id, errmsg, sizeof(errmsg)));
f6de59c1
AN
9988 return tp_id;
9989 }
9990
9991 attr.type = PERF_TYPE_TRACEPOINT;
9992 attr.size = sizeof(attr);
9993 attr.config = tp_id;
9994
9995 pfd = syscall(__NR_perf_event_open, &attr, -1 /* pid */, 0 /* cpu */,
9996 -1 /* group_fd */, PERF_FLAG_FD_CLOEXEC);
9997 if (pfd < 0) {
9998 err = -errno;
be18010e
KW
9999 pr_warn("tracepoint '%s/%s' perf_event_open() failed: %s\n",
10000 tp_category, tp_name,
10001 libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
f6de59c1
AN
10002 return err;
10003 }
10004 return pfd;
10005}
10006
942025c9 10007struct bpf_link *bpf_program__attach_tracepoint_opts(const struct bpf_program *prog,
47faff37
AN
10008 const char *tp_category,
10009 const char *tp_name,
10010 const struct bpf_tracepoint_opts *opts)
f6de59c1 10011{
47faff37 10012 DECLARE_LIBBPF_OPTS(bpf_perf_event_opts, pe_opts);
f6de59c1
AN
10013 char errmsg[STRERR_BUFSIZE];
10014 struct bpf_link *link;
10015 int pfd, err;
10016
47faff37
AN
10017 if (!OPTS_VALID(opts, bpf_tracepoint_opts))
10018 return libbpf_err_ptr(-EINVAL);
10019
10020 pe_opts.bpf_cookie = OPTS_GET(opts, bpf_cookie, 0);
10021
f6de59c1
AN
10022 pfd = perf_event_open_tracepoint(tp_category, tp_name);
10023 if (pfd < 0) {
52109584
AN
10024 pr_warn("prog '%s': failed to create tracepoint '%s/%s' perf event: %s\n",
10025 prog->name, tp_category, tp_name,
be18010e 10026 libbpf_strerror_r(pfd, errmsg, sizeof(errmsg)));
e9fc3ce9 10027 return libbpf_err_ptr(pfd);
f6de59c1 10028 }
47faff37 10029 link = bpf_program__attach_perf_event_opts(prog, pfd, &pe_opts);
e9fc3ce9
AN
10030 err = libbpf_get_error(link);
10031 if (err) {
f6de59c1 10032 close(pfd);
52109584
AN
10033 pr_warn("prog '%s': failed to attach to tracepoint '%s/%s': %s\n",
10034 prog->name, tp_category, tp_name,
be18010e 10035 libbpf_strerror_r(err, errmsg, sizeof(errmsg)));
e9fc3ce9 10036 return libbpf_err_ptr(err);
f6de59c1
AN
10037 }
10038 return link;
10039}
10040
942025c9 10041struct bpf_link *bpf_program__attach_tracepoint(const struct bpf_program *prog,
47faff37
AN
10042 const char *tp_category,
10043 const char *tp_name)
10044{
10045 return bpf_program__attach_tracepoint_opts(prog, tp_category, tp_name, NULL);
10046}
10047
12d9466d 10048static struct bpf_link *attach_tp(const struct bpf_program *prog, long cookie)
d7a18ea7
AN
10049{
10050 char *sec_name, *tp_cat, *tp_name;
10051 struct bpf_link *link;
10052
52109584 10053 sec_name = strdup(prog->sec_name);
d7a18ea7 10054 if (!sec_name)
e9fc3ce9 10055 return libbpf_err_ptr(-ENOMEM);
d7a18ea7 10056
13d35a0c
AN
10057 /* extract "tp/<category>/<name>" or "tracepoint/<category>/<name>" */
10058 if (str_has_pfx(prog->sec_name, "tp/"))
10059 tp_cat = sec_name + sizeof("tp/") - 1;
10060 else
10061 tp_cat = sec_name + sizeof("tracepoint/") - 1;
d7a18ea7
AN
10062 tp_name = strchr(tp_cat, '/');
10063 if (!tp_name) {
e9fc3ce9
AN
10064 free(sec_name);
10065 return libbpf_err_ptr(-EINVAL);
d7a18ea7
AN
10066 }
10067 *tp_name = '\0';
10068 tp_name++;
10069
10070 link = bpf_program__attach_tracepoint(prog, tp_cat, tp_name);
d7a18ea7
AN
10071 free(sec_name);
10072 return link;
10073}
10074
942025c9 10075struct bpf_link *bpf_program__attach_raw_tracepoint(const struct bpf_program *prog,
84bf5e1f
AN
10076 const char *tp_name)
10077{
10078 char errmsg[STRERR_BUFSIZE];
c016b68e 10079 struct bpf_link *link;
84bf5e1f
AN
10080 int prog_fd, pfd;
10081
10082 prog_fd = bpf_program__fd(prog);
10083 if (prog_fd < 0) {
52109584 10084 pr_warn("prog '%s': can't attach before loaded\n", prog->name);
e9fc3ce9 10085 return libbpf_err_ptr(-EINVAL);
84bf5e1f
AN
10086 }
10087
d6958706 10088 link = calloc(1, sizeof(*link));
84bf5e1f 10089 if (!link)
e9fc3ce9 10090 return libbpf_err_ptr(-ENOMEM);
c016b68e 10091 link->detach = &bpf_link__detach_fd;
84bf5e1f
AN
10092
10093 pfd = bpf_raw_tracepoint_open(tp_name, prog_fd);
10094 if (pfd < 0) {
10095 pfd = -errno;
10096 free(link);
52109584
AN
10097 pr_warn("prog '%s': failed to attach to raw tracepoint '%s': %s\n",
10098 prog->name, tp_name, libbpf_strerror_r(pfd, errmsg, sizeof(errmsg)));
e9fc3ce9 10099 return libbpf_err_ptr(pfd);
84bf5e1f
AN
10100 }
10101 link->fd = pfd;
c016b68e 10102 return link;
84bf5e1f
AN
10103}
10104
12d9466d 10105static struct bpf_link *attach_raw_tp(const struct bpf_program *prog, long cookie)
d7a18ea7 10106{
ccaf12d6
HT
10107 static const char *const prefixes[] = {
10108 "raw_tp/",
10109 "raw_tracepoint/",
10110 "raw_tp.w/",
10111 "raw_tracepoint.w/",
10112 };
10113 size_t i;
10114 const char *tp_name = NULL;
13d35a0c 10115
ccaf12d6
HT
10116 for (i = 0; i < ARRAY_SIZE(prefixes); i++) {
10117 if (str_has_pfx(prog->sec_name, prefixes[i])) {
10118 tp_name = prog->sec_name + strlen(prefixes[i]);
10119 break;
10120 }
10121 }
10122 if (!tp_name) {
10123 pr_warn("prog '%s': invalid section name '%s'\n",
10124 prog->name, prog->sec_name);
10125 return libbpf_err_ptr(-EINVAL);
10126 }
d7a18ea7
AN
10127
10128 return bpf_program__attach_raw_tracepoint(prog, tp_name);
10129}
10130
1e092a03 10131/* Common logic for all BPF program types that attach to a btf_id */
942025c9 10132static struct bpf_link *bpf_program__attach_btf_id(const struct bpf_program *prog)
b8c54ea4
AS
10133{
10134 char errmsg[STRERR_BUFSIZE];
c016b68e 10135 struct bpf_link *link;
b8c54ea4
AS
10136 int prog_fd, pfd;
10137
10138 prog_fd = bpf_program__fd(prog);
10139 if (prog_fd < 0) {
52109584 10140 pr_warn("prog '%s': can't attach before loaded\n", prog->name);
e9fc3ce9 10141 return libbpf_err_ptr(-EINVAL);
b8c54ea4
AS
10142 }
10143
d6958706 10144 link = calloc(1, sizeof(*link));
b8c54ea4 10145 if (!link)
e9fc3ce9 10146 return libbpf_err_ptr(-ENOMEM);
c016b68e 10147 link->detach = &bpf_link__detach_fd;
b8c54ea4
AS
10148
10149 pfd = bpf_raw_tracepoint_open(NULL, prog_fd);
10150 if (pfd < 0) {
10151 pfd = -errno;
10152 free(link);
52109584
AN
10153 pr_warn("prog '%s': failed to attach: %s\n",
10154 prog->name, libbpf_strerror_r(pfd, errmsg, sizeof(errmsg)));
e9fc3ce9 10155 return libbpf_err_ptr(pfd);
b8c54ea4
AS
10156 }
10157 link->fd = pfd;
10158 return (struct bpf_link *)link;
10159}
10160
942025c9 10161struct bpf_link *bpf_program__attach_trace(const struct bpf_program *prog)
1e092a03
KS
10162{
10163 return bpf_program__attach_btf_id(prog);
10164}
10165
942025c9 10166struct bpf_link *bpf_program__attach_lsm(const struct bpf_program *prog)
1e092a03
KS
10167{
10168 return bpf_program__attach_btf_id(prog);
10169}
10170
12d9466d 10171static struct bpf_link *attach_trace(const struct bpf_program *prog, long cookie)
d7a18ea7
AN
10172{
10173 return bpf_program__attach_trace(prog);
10174}
10175
12d9466d 10176static struct bpf_link *attach_lsm(const struct bpf_program *prog, long cookie)
1e092a03
KS
10177{
10178 return bpf_program__attach_lsm(prog);
10179}
10180
d60d81ac 10181static struct bpf_link *
942025c9 10182bpf_program__attach_fd(const struct bpf_program *prog, int target_fd, int btf_id,
d60d81ac 10183 const char *target_name)
cc4f864b 10184{
a5359091
THJ
10185 DECLARE_LIBBPF_OPTS(bpf_link_create_opts, opts,
10186 .target_btf_id = btf_id);
cc4f864b
AN
10187 enum bpf_attach_type attach_type;
10188 char errmsg[STRERR_BUFSIZE];
10189 struct bpf_link *link;
10190 int prog_fd, link_fd;
10191
10192 prog_fd = bpf_program__fd(prog);
10193 if (prog_fd < 0) {
52109584 10194 pr_warn("prog '%s': can't attach before loaded\n", prog->name);
e9fc3ce9 10195 return libbpf_err_ptr(-EINVAL);
cc4f864b
AN
10196 }
10197
10198 link = calloc(1, sizeof(*link));
10199 if (!link)
e9fc3ce9 10200 return libbpf_err_ptr(-ENOMEM);
cc4f864b
AN
10201 link->detach = &bpf_link__detach_fd;
10202
10203 attach_type = bpf_program__get_expected_attach_type(prog);
a5359091 10204 link_fd = bpf_link_create(prog_fd, target_fd, attach_type, &opts);
cc4f864b
AN
10205 if (link_fd < 0) {
10206 link_fd = -errno;
10207 free(link);
52109584
AN
10208 pr_warn("prog '%s': failed to attach to %s: %s\n",
10209 prog->name, target_name,
cc4f864b 10210 libbpf_strerror_r(link_fd, errmsg, sizeof(errmsg)));
e9fc3ce9 10211 return libbpf_err_ptr(link_fd);
cc4f864b
AN
10212 }
10213 link->fd = link_fd;
10214 return link;
10215}
10216
d60d81ac 10217struct bpf_link *
942025c9 10218bpf_program__attach_cgroup(const struct bpf_program *prog, int cgroup_fd)
d60d81ac 10219{
a5359091 10220 return bpf_program__attach_fd(prog, cgroup_fd, 0, "cgroup");
d60d81ac
JS
10221}
10222
10223struct bpf_link *
942025c9 10224bpf_program__attach_netns(const struct bpf_program *prog, int netns_fd)
d60d81ac 10225{
a5359091 10226 return bpf_program__attach_fd(prog, netns_fd, 0, "netns");
d60d81ac
JS
10227}
10228
942025c9 10229struct bpf_link *bpf_program__attach_xdp(const struct bpf_program *prog, int ifindex)
dc8698ca
AN
10230{
10231 /* target_fd/target_ifindex use the same field in LINK_CREATE */
a5359091
THJ
10232 return bpf_program__attach_fd(prog, ifindex, 0, "xdp");
10233}
10234
942025c9 10235struct bpf_link *bpf_program__attach_freplace(const struct bpf_program *prog,
a5359091
THJ
10236 int target_fd,
10237 const char *attach_func_name)
10238{
10239 int btf_id;
10240
10241 if (!!target_fd != !!attach_func_name) {
10242 pr_warn("prog '%s': supply none or both of target_fd and attach_func_name\n",
10243 prog->name);
e9fc3ce9 10244 return libbpf_err_ptr(-EINVAL);
a5359091
THJ
10245 }
10246
10247 if (prog->type != BPF_PROG_TYPE_EXT) {
10248 pr_warn("prog '%s': only BPF_PROG_TYPE_EXT can attach as freplace",
10249 prog->name);
e9fc3ce9 10250 return libbpf_err_ptr(-EINVAL);
a5359091
THJ
10251 }
10252
10253 if (target_fd) {
10254 btf_id = libbpf_find_prog_btf_id(attach_func_name, target_fd);
10255 if (btf_id < 0)
e9fc3ce9 10256 return libbpf_err_ptr(btf_id);
a5359091
THJ
10257
10258 return bpf_program__attach_fd(prog, target_fd, btf_id, "freplace");
10259 } else {
10260 /* no target, so use raw_tracepoint_open for compatibility
10261 * with old kernels
10262 */
10263 return bpf_program__attach_trace(prog);
10264 }
dc8698ca
AN
10265}
10266
c09add2f 10267struct bpf_link *
942025c9 10268bpf_program__attach_iter(const struct bpf_program *prog,
c09add2f
YS
10269 const struct bpf_iter_attach_opts *opts)
10270{
cd31039a 10271 DECLARE_LIBBPF_OPTS(bpf_link_create_opts, link_create_opts);
c09add2f
YS
10272 char errmsg[STRERR_BUFSIZE];
10273 struct bpf_link *link;
10274 int prog_fd, link_fd;
cd31039a 10275 __u32 target_fd = 0;
c09add2f
YS
10276
10277 if (!OPTS_VALID(opts, bpf_iter_attach_opts))
e9fc3ce9 10278 return libbpf_err_ptr(-EINVAL);
c09add2f 10279
74fc097d
YS
10280 link_create_opts.iter_info = OPTS_GET(opts, link_info, (void *)0);
10281 link_create_opts.iter_info_len = OPTS_GET(opts, link_info_len, 0);
cd31039a 10282
c09add2f
YS
10283 prog_fd = bpf_program__fd(prog);
10284 if (prog_fd < 0) {
52109584 10285 pr_warn("prog '%s': can't attach before loaded\n", prog->name);
e9fc3ce9 10286 return libbpf_err_ptr(-EINVAL);
c09add2f
YS
10287 }
10288
10289 link = calloc(1, sizeof(*link));
10290 if (!link)
e9fc3ce9 10291 return libbpf_err_ptr(-ENOMEM);
c09add2f
YS
10292 link->detach = &bpf_link__detach_fd;
10293
cd31039a
YS
10294 link_fd = bpf_link_create(prog_fd, target_fd, BPF_TRACE_ITER,
10295 &link_create_opts);
c09add2f
YS
10296 if (link_fd < 0) {
10297 link_fd = -errno;
10298 free(link);
52109584
AN
10299 pr_warn("prog '%s': failed to attach to iterator: %s\n",
10300 prog->name, libbpf_strerror_r(link_fd, errmsg, sizeof(errmsg)));
e9fc3ce9 10301 return libbpf_err_ptr(link_fd);
c09add2f
YS
10302 }
10303 link->fd = link_fd;
10304 return link;
10305}
10306
12d9466d 10307static struct bpf_link *attach_iter(const struct bpf_program *prog, long cookie)
e9fc3ce9
AN
10308{
10309 return bpf_program__attach_iter(prog, NULL);
10310}
10311
942025c9 10312struct bpf_link *bpf_program__attach(const struct bpf_program *prog)
d7a18ea7 10313{
5532dfd4 10314 if (!prog->sec_def || !prog->sec_def->attach_fn)
e9fc3ce9 10315 return libbpf_err_ptr(-ESRCH);
d7a18ea7 10316
12d9466d 10317 return prog->sec_def->attach_fn(prog, prog->sec_def->cookie);
d7a18ea7
AN
10318}
10319
590a0088
MKL
10320static int bpf_link__detach_struct_ops(struct bpf_link *link)
10321{
590a0088
MKL
10322 __u32 zero = 0;
10323
c016b68e 10324 if (bpf_map_delete_elem(link->fd, &zero))
590a0088
MKL
10325 return -errno;
10326
10327 return 0;
10328}
10329
942025c9 10330struct bpf_link *bpf_map__attach_struct_ops(const struct bpf_map *map)
590a0088
MKL
10331{
10332 struct bpf_struct_ops *st_ops;
c016b68e 10333 struct bpf_link *link;
590a0088
MKL
10334 __u32 i, zero = 0;
10335 int err;
10336
10337 if (!bpf_map__is_struct_ops(map) || map->fd == -1)
e9fc3ce9 10338 return libbpf_err_ptr(-EINVAL);
590a0088
MKL
10339
10340 link = calloc(1, sizeof(*link));
10341 if (!link)
e9fc3ce9 10342 return libbpf_err_ptr(-EINVAL);
590a0088
MKL
10343
10344 st_ops = map->st_ops;
10345 for (i = 0; i < btf_vlen(st_ops->type); i++) {
10346 struct bpf_program *prog = st_ops->progs[i];
10347 void *kern_data;
10348 int prog_fd;
10349
10350 if (!prog)
10351 continue;
10352
10353 prog_fd = bpf_program__fd(prog);
10354 kern_data = st_ops->kern_vdata + st_ops->kern_func_off[i];
10355 *(unsigned long *)kern_data = prog_fd;
10356 }
10357
10358 err = bpf_map_update_elem(map->fd, &zero, st_ops->kern_vdata, 0);
10359 if (err) {
10360 err = -errno;
10361 free(link);
e9fc3ce9 10362 return libbpf_err_ptr(err);
590a0088
MKL
10363 }
10364
c016b68e 10365 link->detach = bpf_link__detach_struct_ops;
590a0088
MKL
10366 link->fd = map->fd;
10367
c016b68e 10368 return link;
590a0088
MKL
10369}
10370
d0cabbb0 10371enum bpf_perf_event_ret
3dca2115
DB
10372bpf_perf_event_read_simple(void *mmap_mem, size_t mmap_size, size_t page_size,
10373 void **copy_mem, size_t *copy_size,
10374 bpf_perf_event_print_t fn, void *private_data)
d0cabbb0 10375{
3dca2115 10376 struct perf_event_mmap_page *header = mmap_mem;
a64af0ef 10377 __u64 data_head = ring_buffer_read_head(header);
d0cabbb0 10378 __u64 data_tail = header->data_tail;
3dca2115
DB
10379 void *base = ((__u8 *)header) + page_size;
10380 int ret = LIBBPF_PERF_EVENT_CONT;
10381 struct perf_event_header *ehdr;
10382 size_t ehdr_size;
10383
10384 while (data_head != data_tail) {
10385 ehdr = base + (data_tail & (mmap_size - 1));
10386 ehdr_size = ehdr->size;
10387
10388 if (((void *)ehdr) + ehdr_size > base + mmap_size) {
10389 void *copy_start = ehdr;
10390 size_t len_first = base + mmap_size - copy_start;
10391 size_t len_secnd = ehdr_size - len_first;
10392
10393 if (*copy_size < ehdr_size) {
10394 free(*copy_mem);
10395 *copy_mem = malloc(ehdr_size);
10396 if (!*copy_mem) {
10397 *copy_size = 0;
d0cabbb0
JK
10398 ret = LIBBPF_PERF_EVENT_ERROR;
10399 break;
10400 }
3dca2115 10401 *copy_size = ehdr_size;
d0cabbb0
JK
10402 }
10403
3dca2115
DB
10404 memcpy(*copy_mem, copy_start, len_first);
10405 memcpy(*copy_mem + len_first, base, len_secnd);
10406 ehdr = *copy_mem;
d0cabbb0
JK
10407 }
10408
3dca2115
DB
10409 ret = fn(ehdr, private_data);
10410 data_tail += ehdr_size;
d0cabbb0
JK
10411 if (ret != LIBBPF_PERF_EVENT_CONT)
10412 break;
d0cabbb0
JK
10413 }
10414
a64af0ef 10415 ring_buffer_write_tail(header, data_tail);
e9fc3ce9 10416 return libbpf_err(ret);
d0cabbb0 10417}
34be1646 10418
fb84b822
AN
10419struct perf_buffer;
10420
10421struct perf_buffer_params {
10422 struct perf_event_attr *attr;
10423 /* if event_cb is specified, it takes precendence */
10424 perf_buffer_event_fn event_cb;
10425 /* sample_cb and lost_cb are higher-level common-case callbacks */
10426 perf_buffer_sample_fn sample_cb;
10427 perf_buffer_lost_fn lost_cb;
10428 void *ctx;
10429 int cpu_cnt;
10430 int *cpus;
10431 int *map_keys;
10432};
10433
10434struct perf_cpu_buf {
10435 struct perf_buffer *pb;
10436 void *base; /* mmap()'ed memory */
10437 void *buf; /* for reconstructing segmented data */
10438 size_t buf_size;
10439 int fd;
10440 int cpu;
10441 int map_key;
10442};
10443
10444struct perf_buffer {
10445 perf_buffer_event_fn event_cb;
10446 perf_buffer_sample_fn sample_cb;
10447 perf_buffer_lost_fn lost_cb;
10448 void *ctx; /* passed into callbacks */
10449
10450 size_t page_size;
10451 size_t mmap_size;
10452 struct perf_cpu_buf **cpu_bufs;
10453 struct epoll_event *events;
783b8f01 10454 int cpu_cnt; /* number of allocated CPU buffers */
fb84b822
AN
10455 int epoll_fd; /* perf event FD */
10456 int map_fd; /* BPF_MAP_TYPE_PERF_EVENT_ARRAY BPF map FD */
10457};
10458
10459static void perf_buffer__free_cpu_buf(struct perf_buffer *pb,
10460 struct perf_cpu_buf *cpu_buf)
10461{
10462 if (!cpu_buf)
10463 return;
10464 if (cpu_buf->base &&
10465 munmap(cpu_buf->base, pb->mmap_size + pb->page_size))
be18010e 10466 pr_warn("failed to munmap cpu_buf #%d\n", cpu_buf->cpu);
fb84b822
AN
10467 if (cpu_buf->fd >= 0) {
10468 ioctl(cpu_buf->fd, PERF_EVENT_IOC_DISABLE, 0);
10469 close(cpu_buf->fd);
10470 }
10471 free(cpu_buf->buf);
10472 free(cpu_buf);
10473}
10474
10475void perf_buffer__free(struct perf_buffer *pb)
10476{
10477 int i;
10478
50450fc7 10479 if (IS_ERR_OR_NULL(pb))
fb84b822
AN
10480 return;
10481 if (pb->cpu_bufs) {
601b05ca 10482 for (i = 0; i < pb->cpu_cnt; i++) {
fb84b822
AN
10483 struct perf_cpu_buf *cpu_buf = pb->cpu_bufs[i];
10484
601b05ca
EC
10485 if (!cpu_buf)
10486 continue;
10487
fb84b822
AN
10488 bpf_map_delete_elem(pb->map_fd, &cpu_buf->map_key);
10489 perf_buffer__free_cpu_buf(pb, cpu_buf);
10490 }
10491 free(pb->cpu_bufs);
10492 }
10493 if (pb->epoll_fd >= 0)
10494 close(pb->epoll_fd);
10495 free(pb->events);
10496 free(pb);
10497}
10498
10499static struct perf_cpu_buf *
10500perf_buffer__open_cpu_buf(struct perf_buffer *pb, struct perf_event_attr *attr,
10501 int cpu, int map_key)
10502{
10503 struct perf_cpu_buf *cpu_buf;
10504 char msg[STRERR_BUFSIZE];
10505 int err;
10506
10507 cpu_buf = calloc(1, sizeof(*cpu_buf));
10508 if (!cpu_buf)
10509 return ERR_PTR(-ENOMEM);
10510
10511 cpu_buf->pb = pb;
10512 cpu_buf->cpu = cpu;
10513 cpu_buf->map_key = map_key;
10514
10515 cpu_buf->fd = syscall(__NR_perf_event_open, attr, -1 /* pid */, cpu,
10516 -1, PERF_FLAG_FD_CLOEXEC);
10517 if (cpu_buf->fd < 0) {
10518 err = -errno;
be18010e
KW
10519 pr_warn("failed to open perf buffer event on cpu #%d: %s\n",
10520 cpu, libbpf_strerror_r(err, msg, sizeof(msg)));
fb84b822
AN
10521 goto error;
10522 }
10523
10524 cpu_buf->base = mmap(NULL, pb->mmap_size + pb->page_size,
10525 PROT_READ | PROT_WRITE, MAP_SHARED,
10526 cpu_buf->fd, 0);
10527 if (cpu_buf->base == MAP_FAILED) {
10528 cpu_buf->base = NULL;
10529 err = -errno;
be18010e
KW
10530 pr_warn("failed to mmap perf buffer on cpu #%d: %s\n",
10531 cpu, libbpf_strerror_r(err, msg, sizeof(msg)));
fb84b822
AN
10532 goto error;
10533 }
10534
10535 if (ioctl(cpu_buf->fd, PERF_EVENT_IOC_ENABLE, 0) < 0) {
10536 err = -errno;
be18010e
KW
10537 pr_warn("failed to enable perf buffer event on cpu #%d: %s\n",
10538 cpu, libbpf_strerror_r(err, msg, sizeof(msg)));
fb84b822
AN
10539 goto error;
10540 }
10541
10542 return cpu_buf;
10543
10544error:
10545 perf_buffer__free_cpu_buf(pb, cpu_buf);
10546 return (struct perf_cpu_buf *)ERR_PTR(err);
10547}
10548
10549static struct perf_buffer *__perf_buffer__new(int map_fd, size_t page_cnt,
10550 struct perf_buffer_params *p);
10551
10552struct perf_buffer *perf_buffer__new(int map_fd, size_t page_cnt,
10553 const struct perf_buffer_opts *opts)
10554{
10555 struct perf_buffer_params p = {};
4be6e05c
ACM
10556 struct perf_event_attr attr = { 0, };
10557
65bb2e0f 10558 attr.config = PERF_COUNT_SW_BPF_OUTPUT;
4be6e05c
ACM
10559 attr.type = PERF_TYPE_SOFTWARE;
10560 attr.sample_type = PERF_SAMPLE_RAW;
10561 attr.sample_period = 1;
10562 attr.wakeup_events = 1;
fb84b822
AN
10563
10564 p.attr = &attr;
10565 p.sample_cb = opts ? opts->sample_cb : NULL;
10566 p.lost_cb = opts ? opts->lost_cb : NULL;
10567 p.ctx = opts ? opts->ctx : NULL;
10568
e9fc3ce9 10569 return libbpf_ptr(__perf_buffer__new(map_fd, page_cnt, &p));
fb84b822
AN
10570}
10571
10572struct perf_buffer *
10573perf_buffer__new_raw(int map_fd, size_t page_cnt,
10574 const struct perf_buffer_raw_opts *opts)
10575{
10576 struct perf_buffer_params p = {};
10577
10578 p.attr = opts->attr;
10579 p.event_cb = opts->event_cb;
10580 p.ctx = opts->ctx;
10581 p.cpu_cnt = opts->cpu_cnt;
10582 p.cpus = opts->cpus;
10583 p.map_keys = opts->map_keys;
10584
e9fc3ce9 10585 return libbpf_ptr(__perf_buffer__new(map_fd, page_cnt, &p));
fb84b822
AN
10586}
10587
10588static struct perf_buffer *__perf_buffer__new(int map_fd, size_t page_cnt,
10589 struct perf_buffer_params *p)
10590{
783b8f01 10591 const char *online_cpus_file = "/sys/devices/system/cpu/online";
0e289487 10592 struct bpf_map_info map;
fb84b822
AN
10593 char msg[STRERR_BUFSIZE];
10594 struct perf_buffer *pb;
783b8f01 10595 bool *online = NULL;
fb84b822 10596 __u32 map_info_len;
783b8f01 10597 int err, i, j, n;
fb84b822
AN
10598
10599 if (page_cnt & (page_cnt - 1)) {
be18010e
KW
10600 pr_warn("page count should be power of two, but is %zu\n",
10601 page_cnt);
fb84b822
AN
10602 return ERR_PTR(-EINVAL);
10603 }
10604
0e289487
AN
10605 /* best-effort sanity checks */
10606 memset(&map, 0, sizeof(map));
fb84b822
AN
10607 map_info_len = sizeof(map);
10608 err = bpf_obj_get_info_by_fd(map_fd, &map, &map_info_len);
10609 if (err) {
10610 err = -errno;
0e289487
AN
10611 /* if BPF_OBJ_GET_INFO_BY_FD is supported, will return
10612 * -EBADFD, -EFAULT, or -E2BIG on real error
10613 */
10614 if (err != -EINVAL) {
10615 pr_warn("failed to get map info for map FD %d: %s\n",
10616 map_fd, libbpf_strerror_r(err, msg, sizeof(msg)));
10617 return ERR_PTR(err);
10618 }
10619 pr_debug("failed to get map info for FD %d; API not supported? Ignoring...\n",
10620 map_fd);
10621 } else {
10622 if (map.type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) {
10623 pr_warn("map '%s' should be BPF_MAP_TYPE_PERF_EVENT_ARRAY\n",
10624 map.name);
10625 return ERR_PTR(-EINVAL);
10626 }
fb84b822
AN
10627 }
10628
10629 pb = calloc(1, sizeof(*pb));
10630 if (!pb)
10631 return ERR_PTR(-ENOMEM);
10632
10633 pb->event_cb = p->event_cb;
10634 pb->sample_cb = p->sample_cb;
10635 pb->lost_cb = p->lost_cb;
10636 pb->ctx = p->ctx;
10637
10638 pb->page_size = getpagesize();
10639 pb->mmap_size = pb->page_size * page_cnt;
10640 pb->map_fd = map_fd;
10641
10642 pb->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
10643 if (pb->epoll_fd < 0) {
10644 err = -errno;
be18010e
KW
10645 pr_warn("failed to create epoll instance: %s\n",
10646 libbpf_strerror_r(err, msg, sizeof(msg)));
fb84b822
AN
10647 goto error;
10648 }
10649
10650 if (p->cpu_cnt > 0) {
10651 pb->cpu_cnt = p->cpu_cnt;
10652 } else {
10653 pb->cpu_cnt = libbpf_num_possible_cpus();
10654 if (pb->cpu_cnt < 0) {
10655 err = pb->cpu_cnt;
10656 goto error;
10657 }
0e289487 10658 if (map.max_entries && map.max_entries < pb->cpu_cnt)
fb84b822
AN
10659 pb->cpu_cnt = map.max_entries;
10660 }
10661
10662 pb->events = calloc(pb->cpu_cnt, sizeof(*pb->events));
10663 if (!pb->events) {
10664 err = -ENOMEM;
be18010e 10665 pr_warn("failed to allocate events: out of memory\n");
fb84b822
AN
10666 goto error;
10667 }
10668 pb->cpu_bufs = calloc(pb->cpu_cnt, sizeof(*pb->cpu_bufs));
10669 if (!pb->cpu_bufs) {
10670 err = -ENOMEM;
be18010e 10671 pr_warn("failed to allocate buffers: out of memory\n");
fb84b822
AN
10672 goto error;
10673 }
10674
783b8f01
AN
10675 err = parse_cpu_mask_file(online_cpus_file, &online, &n);
10676 if (err) {
10677 pr_warn("failed to get online CPU mask: %d\n", err);
10678 goto error;
10679 }
10680
10681 for (i = 0, j = 0; i < pb->cpu_cnt; i++) {
fb84b822
AN
10682 struct perf_cpu_buf *cpu_buf;
10683 int cpu, map_key;
10684
10685 cpu = p->cpu_cnt > 0 ? p->cpus[i] : i;
10686 map_key = p->cpu_cnt > 0 ? p->map_keys[i] : i;
10687
783b8f01
AN
10688 /* in case user didn't explicitly requested particular CPUs to
10689 * be attached to, skip offline/not present CPUs
10690 */
10691 if (p->cpu_cnt <= 0 && (cpu >= n || !online[cpu]))
10692 continue;
10693
fb84b822
AN
10694 cpu_buf = perf_buffer__open_cpu_buf(pb, p->attr, cpu, map_key);
10695 if (IS_ERR(cpu_buf)) {
10696 err = PTR_ERR(cpu_buf);
10697 goto error;
10698 }
10699
783b8f01 10700 pb->cpu_bufs[j] = cpu_buf;
fb84b822
AN
10701
10702 err = bpf_map_update_elem(pb->map_fd, &map_key,
10703 &cpu_buf->fd, 0);
10704 if (err) {
10705 err = -errno;
be18010e
KW
10706 pr_warn("failed to set cpu #%d, key %d -> perf FD %d: %s\n",
10707 cpu, map_key, cpu_buf->fd,
10708 libbpf_strerror_r(err, msg, sizeof(msg)));
fb84b822
AN
10709 goto error;
10710 }
10711
783b8f01
AN
10712 pb->events[j].events = EPOLLIN;
10713 pb->events[j].data.ptr = cpu_buf;
fb84b822 10714 if (epoll_ctl(pb->epoll_fd, EPOLL_CTL_ADD, cpu_buf->fd,
783b8f01 10715 &pb->events[j]) < 0) {
fb84b822 10716 err = -errno;
be18010e
KW
10717 pr_warn("failed to epoll_ctl cpu #%d perf FD %d: %s\n",
10718 cpu, cpu_buf->fd,
10719 libbpf_strerror_r(err, msg, sizeof(msg)));
fb84b822
AN
10720 goto error;
10721 }
783b8f01 10722 j++;
fb84b822 10723 }
783b8f01
AN
10724 pb->cpu_cnt = j;
10725 free(online);
fb84b822
AN
10726
10727 return pb;
10728
10729error:
783b8f01 10730 free(online);
fb84b822
AN
10731 if (pb)
10732 perf_buffer__free(pb);
10733 return ERR_PTR(err);
10734}
10735
10736struct perf_sample_raw {
10737 struct perf_event_header header;
10738 uint32_t size;
385bbf7b 10739 char data[];
fb84b822
AN
10740};
10741
10742struct perf_sample_lost {
10743 struct perf_event_header header;
10744 uint64_t id;
10745 uint64_t lost;
10746 uint64_t sample_id;
10747};
10748
10749static enum bpf_perf_event_ret
10750perf_buffer__process_record(struct perf_event_header *e, void *ctx)
10751{
10752 struct perf_cpu_buf *cpu_buf = ctx;
10753 struct perf_buffer *pb = cpu_buf->pb;
10754 void *data = e;
10755
10756 /* user wants full control over parsing perf event */
10757 if (pb->event_cb)
10758 return pb->event_cb(pb->ctx, cpu_buf->cpu, e);
10759
10760 switch (e->type) {
10761 case PERF_RECORD_SAMPLE: {
10762 struct perf_sample_raw *s = data;
10763
10764 if (pb->sample_cb)
10765 pb->sample_cb(pb->ctx, cpu_buf->cpu, s->data, s->size);
10766 break;
10767 }
10768 case PERF_RECORD_LOST: {
10769 struct perf_sample_lost *s = data;
10770
10771 if (pb->lost_cb)
10772 pb->lost_cb(pb->ctx, cpu_buf->cpu, s->lost);
10773 break;
10774 }
10775 default:
be18010e 10776 pr_warn("unknown perf sample type %d\n", e->type);
fb84b822
AN
10777 return LIBBPF_PERF_EVENT_ERROR;
10778 }
10779 return LIBBPF_PERF_EVENT_CONT;
10780}
10781
10782static int perf_buffer__process_records(struct perf_buffer *pb,
10783 struct perf_cpu_buf *cpu_buf)
10784{
10785 enum bpf_perf_event_ret ret;
10786
10787 ret = bpf_perf_event_read_simple(cpu_buf->base, pb->mmap_size,
10788 pb->page_size, &cpu_buf->buf,
10789 &cpu_buf->buf_size,
10790 perf_buffer__process_record, cpu_buf);
10791 if (ret != LIBBPF_PERF_EVENT_CONT)
10792 return ret;
10793 return 0;
10794}
10795
dca5612f
AN
10796int perf_buffer__epoll_fd(const struct perf_buffer *pb)
10797{
10798 return pb->epoll_fd;
10799}
10800
fb84b822
AN
10801int perf_buffer__poll(struct perf_buffer *pb, int timeout_ms)
10802{
10803 int i, cnt, err;
10804
10805 cnt = epoll_wait(pb->epoll_fd, pb->events, pb->cpu_cnt, timeout_ms);
e9fc3ce9 10806 if (cnt < 0)
af0efa05 10807 return -errno;
e9fc3ce9 10808
fb84b822
AN
10809 for (i = 0; i < cnt; i++) {
10810 struct perf_cpu_buf *cpu_buf = pb->events[i].data.ptr;
10811
10812 err = perf_buffer__process_records(pb, cpu_buf);
10813 if (err) {
be18010e 10814 pr_warn("error while processing records: %d\n", err);
e9fc3ce9 10815 return libbpf_err(err);
fb84b822
AN
10816 }
10817 }
e9fc3ce9 10818 return cnt;
fb84b822
AN
10819}
10820
dca5612f
AN
10821/* Return number of PERF_EVENT_ARRAY map slots set up by this perf_buffer
10822 * manager.
10823 */
10824size_t perf_buffer__buffer_cnt(const struct perf_buffer *pb)
10825{
10826 return pb->cpu_cnt;
10827}
10828
10829/*
10830 * Return perf_event FD of a ring buffer in *buf_idx* slot of
10831 * PERF_EVENT_ARRAY BPF map. This FD can be polled for new data using
10832 * select()/poll()/epoll() Linux syscalls.
10833 */
10834int perf_buffer__buffer_fd(const struct perf_buffer *pb, size_t buf_idx)
10835{
10836 struct perf_cpu_buf *cpu_buf;
10837
10838 if (buf_idx >= pb->cpu_cnt)
e9fc3ce9 10839 return libbpf_err(-EINVAL);
dca5612f
AN
10840
10841 cpu_buf = pb->cpu_bufs[buf_idx];
10842 if (!cpu_buf)
e9fc3ce9 10843 return libbpf_err(-ENOENT);
dca5612f
AN
10844
10845 return cpu_buf->fd;
10846}
10847
10848/*
10849 * Consume data from perf ring buffer corresponding to slot *buf_idx* in
10850 * PERF_EVENT_ARRAY BPF map without waiting/polling. If there is no data to
10851 * consume, do nothing and return success.
10852 * Returns:
10853 * - 0 on success;
10854 * - <0 on failure.
10855 */
10856int perf_buffer__consume_buffer(struct perf_buffer *pb, size_t buf_idx)
10857{
10858 struct perf_cpu_buf *cpu_buf;
10859
10860 if (buf_idx >= pb->cpu_cnt)
e9fc3ce9 10861 return libbpf_err(-EINVAL);
dca5612f
AN
10862
10863 cpu_buf = pb->cpu_bufs[buf_idx];
10864 if (!cpu_buf)
e9fc3ce9 10865 return libbpf_err(-ENOENT);
dca5612f
AN
10866
10867 return perf_buffer__process_records(pb, cpu_buf);
10868}
10869
272d51af
EC
10870int perf_buffer__consume(struct perf_buffer *pb)
10871{
10872 int i, err;
10873
10874 for (i = 0; i < pb->cpu_cnt; i++) {
10875 struct perf_cpu_buf *cpu_buf = pb->cpu_bufs[i];
10876
10877 if (!cpu_buf)
10878 continue;
10879
10880 err = perf_buffer__process_records(pb, cpu_buf);
10881 if (err) {
dca5612f 10882 pr_warn("perf_buffer: failed to process records in buffer #%d: %d\n", i, err);
e9fc3ce9 10883 return libbpf_err(err);
272d51af
EC
10884 }
10885 }
10886 return 0;
10887}
10888
34be1646
SL
10889struct bpf_prog_info_array_desc {
10890 int array_offset; /* e.g. offset of jited_prog_insns */
10891 int count_offset; /* e.g. offset of jited_prog_len */
10892 int size_offset; /* > 0: offset of rec size,
10893 * < 0: fix size of -size_offset
10894 */
10895};
10896
10897static struct bpf_prog_info_array_desc bpf_prog_info_array_desc[] = {
10898 [BPF_PROG_INFO_JITED_INSNS] = {
10899 offsetof(struct bpf_prog_info, jited_prog_insns),
10900 offsetof(struct bpf_prog_info, jited_prog_len),
10901 -1,
10902 },
10903 [BPF_PROG_INFO_XLATED_INSNS] = {
10904 offsetof(struct bpf_prog_info, xlated_prog_insns),
10905 offsetof(struct bpf_prog_info, xlated_prog_len),
10906 -1,
10907 },
10908 [BPF_PROG_INFO_MAP_IDS] = {
10909 offsetof(struct bpf_prog_info, map_ids),
10910 offsetof(struct bpf_prog_info, nr_map_ids),
10911 -(int)sizeof(__u32),
10912 },
10913 [BPF_PROG_INFO_JITED_KSYMS] = {
10914 offsetof(struct bpf_prog_info, jited_ksyms),
10915 offsetof(struct bpf_prog_info, nr_jited_ksyms),
10916 -(int)sizeof(__u64),
10917 },
10918 [BPF_PROG_INFO_JITED_FUNC_LENS] = {
10919 offsetof(struct bpf_prog_info, jited_func_lens),
10920 offsetof(struct bpf_prog_info, nr_jited_func_lens),
10921 -(int)sizeof(__u32),
10922 },
10923 [BPF_PROG_INFO_FUNC_INFO] = {
10924 offsetof(struct bpf_prog_info, func_info),
10925 offsetof(struct bpf_prog_info, nr_func_info),
10926 offsetof(struct bpf_prog_info, func_info_rec_size),
10927 },
10928 [BPF_PROG_INFO_LINE_INFO] = {
10929 offsetof(struct bpf_prog_info, line_info),
10930 offsetof(struct bpf_prog_info, nr_line_info),
10931 offsetof(struct bpf_prog_info, line_info_rec_size),
10932 },
10933 [BPF_PROG_INFO_JITED_LINE_INFO] = {
10934 offsetof(struct bpf_prog_info, jited_line_info),
10935 offsetof(struct bpf_prog_info, nr_jited_line_info),
10936 offsetof(struct bpf_prog_info, jited_line_info_rec_size),
10937 },
10938 [BPF_PROG_INFO_PROG_TAGS] = {
10939 offsetof(struct bpf_prog_info, prog_tags),
10940 offsetof(struct bpf_prog_info, nr_prog_tags),
10941 -(int)sizeof(__u8) * BPF_TAG_SIZE,
10942 },
10943
10944};
10945
8983b731
AN
10946static __u32 bpf_prog_info_read_offset_u32(struct bpf_prog_info *info,
10947 int offset)
34be1646
SL
10948{
10949 __u32 *array = (__u32 *)info;
10950
10951 if (offset >= 0)
10952 return array[offset / sizeof(__u32)];
10953 return -(int)offset;
10954}
10955
8983b731
AN
10956static __u64 bpf_prog_info_read_offset_u64(struct bpf_prog_info *info,
10957 int offset)
34be1646
SL
10958{
10959 __u64 *array = (__u64 *)info;
10960
10961 if (offset >= 0)
10962 return array[offset / sizeof(__u64)];
10963 return -(int)offset;
10964}
10965
10966static void bpf_prog_info_set_offset_u32(struct bpf_prog_info *info, int offset,
10967 __u32 val)
10968{
10969 __u32 *array = (__u32 *)info;
10970
10971 if (offset >= 0)
10972 array[offset / sizeof(__u32)] = val;
10973}
10974
10975static void bpf_prog_info_set_offset_u64(struct bpf_prog_info *info, int offset,
10976 __u64 val)
10977{
10978 __u64 *array = (__u64 *)info;
10979
10980 if (offset >= 0)
10981 array[offset / sizeof(__u64)] = val;
10982}
10983
10984struct bpf_prog_info_linear *
10985bpf_program__get_prog_info_linear(int fd, __u64 arrays)
10986{
10987 struct bpf_prog_info_linear *info_linear;
10988 struct bpf_prog_info info = {};
10989 __u32 info_len = sizeof(info);
10990 __u32 data_len = 0;
10991 int i, err;
10992 void *ptr;
10993
10994 if (arrays >> BPF_PROG_INFO_LAST_ARRAY)
e9fc3ce9 10995 return libbpf_err_ptr(-EINVAL);
34be1646
SL
10996
10997 /* step 1: get array dimensions */
10998 err = bpf_obj_get_info_by_fd(fd, &info, &info_len);
10999 if (err) {
11000 pr_debug("can't get prog info: %s", strerror(errno));
e9fc3ce9 11001 return libbpf_err_ptr(-EFAULT);
34be1646
SL
11002 }
11003
11004 /* step 2: calculate total size of all arrays */
11005 for (i = BPF_PROG_INFO_FIRST_ARRAY; i < BPF_PROG_INFO_LAST_ARRAY; ++i) {
11006 bool include_array = (arrays & (1UL << i)) > 0;
11007 struct bpf_prog_info_array_desc *desc;
11008 __u32 count, size;
11009
11010 desc = bpf_prog_info_array_desc + i;
11011
11012 /* kernel is too old to support this field */
11013 if (info_len < desc->array_offset + sizeof(__u32) ||
11014 info_len < desc->count_offset + sizeof(__u32) ||
11015 (desc->size_offset > 0 && info_len < desc->size_offset))
11016 include_array = false;
11017
11018 if (!include_array) {
11019 arrays &= ~(1UL << i); /* clear the bit */
11020 continue;
11021 }
11022
11023 count = bpf_prog_info_read_offset_u32(&info, desc->count_offset);
11024 size = bpf_prog_info_read_offset_u32(&info, desc->size_offset);
11025
11026 data_len += count * size;
11027 }
11028
11029 /* step 3: allocate continuous memory */
11030 data_len = roundup(data_len, sizeof(__u64));
11031 info_linear = malloc(sizeof(struct bpf_prog_info_linear) + data_len);
11032 if (!info_linear)
e9fc3ce9 11033 return libbpf_err_ptr(-ENOMEM);
34be1646
SL
11034
11035 /* step 4: fill data to info_linear->info */
11036 info_linear->arrays = arrays;
11037 memset(&info_linear->info, 0, sizeof(info));
11038 ptr = info_linear->data;
11039
11040 for (i = BPF_PROG_INFO_FIRST_ARRAY; i < BPF_PROG_INFO_LAST_ARRAY; ++i) {
11041 struct bpf_prog_info_array_desc *desc;
11042 __u32 count, size;
11043
11044 if ((arrays & (1UL << i)) == 0)
11045 continue;
11046
11047 desc = bpf_prog_info_array_desc + i;
11048 count = bpf_prog_info_read_offset_u32(&info, desc->count_offset);
11049 size = bpf_prog_info_read_offset_u32(&info, desc->size_offset);
11050 bpf_prog_info_set_offset_u32(&info_linear->info,
11051 desc->count_offset, count);
11052 bpf_prog_info_set_offset_u32(&info_linear->info,
11053 desc->size_offset, size);
11054 bpf_prog_info_set_offset_u64(&info_linear->info,
11055 desc->array_offset,
11056 ptr_to_u64(ptr));
11057 ptr += count * size;
11058 }
11059
11060 /* step 5: call syscall again to get required arrays */
11061 err = bpf_obj_get_info_by_fd(fd, &info_linear->info, &info_len);
11062 if (err) {
11063 pr_debug("can't get prog info: %s", strerror(errno));
11064 free(info_linear);
e9fc3ce9 11065 return libbpf_err_ptr(-EFAULT);
34be1646
SL
11066 }
11067
11068 /* step 6: verify the data */
11069 for (i = BPF_PROG_INFO_FIRST_ARRAY; i < BPF_PROG_INFO_LAST_ARRAY; ++i) {
11070 struct bpf_prog_info_array_desc *desc;
11071 __u32 v1, v2;
11072
11073 if ((arrays & (1UL << i)) == 0)
11074 continue;
11075
11076 desc = bpf_prog_info_array_desc + i;
11077 v1 = bpf_prog_info_read_offset_u32(&info, desc->count_offset);
11078 v2 = bpf_prog_info_read_offset_u32(&info_linear->info,
11079 desc->count_offset);
11080 if (v1 != v2)
be18010e 11081 pr_warn("%s: mismatch in element count\n", __func__);
34be1646
SL
11082
11083 v1 = bpf_prog_info_read_offset_u32(&info, desc->size_offset);
11084 v2 = bpf_prog_info_read_offset_u32(&info_linear->info,
11085 desc->size_offset);
11086 if (v1 != v2)
be18010e 11087 pr_warn("%s: mismatch in rec size\n", __func__);
34be1646
SL
11088 }
11089
11090 /* step 7: update info_len and data_len */
11091 info_linear->info_len = sizeof(struct bpf_prog_info);
11092 info_linear->data_len = data_len;
11093
11094 return info_linear;
11095}
11096
11097void bpf_program__bpil_addr_to_offs(struct bpf_prog_info_linear *info_linear)
11098{
11099 int i;
11100
11101 for (i = BPF_PROG_INFO_FIRST_ARRAY; i < BPF_PROG_INFO_LAST_ARRAY; ++i) {
11102 struct bpf_prog_info_array_desc *desc;
11103 __u64 addr, offs;
11104
11105 if ((info_linear->arrays & (1UL << i)) == 0)
11106 continue;
11107
11108 desc = bpf_prog_info_array_desc + i;
11109 addr = bpf_prog_info_read_offset_u64(&info_linear->info,
11110 desc->array_offset);
11111 offs = addr - ptr_to_u64(info_linear->data);
11112 bpf_prog_info_set_offset_u64(&info_linear->info,
11113 desc->array_offset, offs);
11114 }
11115}
11116
11117void bpf_program__bpil_offs_to_addr(struct bpf_prog_info_linear *info_linear)
11118{
11119 int i;
11120
11121 for (i = BPF_PROG_INFO_FIRST_ARRAY; i < BPF_PROG_INFO_LAST_ARRAY; ++i) {
11122 struct bpf_prog_info_array_desc *desc;
11123 __u64 addr, offs;
11124
11125 if ((info_linear->arrays & (1UL << i)) == 0)
11126 continue;
11127
11128 desc = bpf_prog_info_array_desc + i;
11129 offs = bpf_prog_info_read_offset_u64(&info_linear->info,
11130 desc->array_offset);
11131 addr = offs + ptr_to_u64(info_linear->data);
11132 bpf_prog_info_set_offset_u64(&info_linear->info,
11133 desc->array_offset, addr);
11134 }
11135}
6446b315 11136
ff26ce5c
EC
11137int bpf_program__set_attach_target(struct bpf_program *prog,
11138 int attach_prog_fd,
11139 const char *attach_func_name)
11140{
fe62de31 11141 int btf_obj_fd = 0, btf_id = 0, err;
ff26ce5c 11142
2d5ec1c6 11143 if (!prog || attach_prog_fd < 0)
e9fc3ce9 11144 return libbpf_err(-EINVAL);
ff26ce5c 11145
fe62de31 11146 if (prog->obj->loaded)
e9fc3ce9 11147 return libbpf_err(-EINVAL);
fe62de31 11148
2d5ec1c6
AN
11149 if (attach_prog_fd && !attach_func_name) {
11150 /* remember attach_prog_fd and let bpf_program__load() find
11151 * BTF ID during the program load
11152 */
11153 prog->attach_prog_fd = attach_prog_fd;
11154 return 0;
11155 }
11156
fe62de31 11157 if (attach_prog_fd) {
ff26ce5c
EC
11158 btf_id = libbpf_find_prog_btf_id(attach_func_name,
11159 attach_prog_fd);
fe62de31 11160 if (btf_id < 0)
e9fc3ce9 11161 return libbpf_err(btf_id);
fe62de31 11162 } else {
2d5ec1c6
AN
11163 if (!attach_func_name)
11164 return libbpf_err(-EINVAL);
11165
fe62de31
AN
11166 /* load btf_vmlinux, if not yet */
11167 err = bpf_object__load_vmlinux_btf(prog->obj, true);
11168 if (err)
e9fc3ce9 11169 return libbpf_err(err);
fe62de31
AN
11170 err = find_kernel_btf_id(prog->obj, attach_func_name,
11171 prog->expected_attach_type,
11172 &btf_obj_fd, &btf_id);
11173 if (err)
e9fc3ce9 11174 return libbpf_err(err);
fe62de31 11175 }
ff26ce5c
EC
11176
11177 prog->attach_btf_id = btf_id;
fe62de31 11178 prog->attach_btf_obj_fd = btf_obj_fd;
ff26ce5c
EC
11179 prog->attach_prog_fd = attach_prog_fd;
11180 return 0;
11181}
11182
6803ee25 11183int parse_cpu_mask_str(const char *s, bool **mask, int *mask_sz)
6446b315 11184{
6803ee25
AN
11185 int err = 0, n, len, start, end = -1;
11186 bool *tmp;
6446b315 11187
6803ee25
AN
11188 *mask = NULL;
11189 *mask_sz = 0;
11190
11191 /* Each sub string separated by ',' has format \d+-\d+ or \d+ */
11192 while (*s) {
11193 if (*s == ',' || *s == '\n') {
11194 s++;
11195 continue;
11196 }
11197 n = sscanf(s, "%d%n-%d%n", &start, &len, &end, &len);
11198 if (n <= 0 || n > 2) {
11199 pr_warn("Failed to get CPU range %s: %d\n", s, n);
11200 err = -EINVAL;
11201 goto cleanup;
11202 } else if (n == 1) {
11203 end = start;
11204 }
11205 if (start < 0 || start > end) {
11206 pr_warn("Invalid CPU range [%d,%d] in %s\n",
11207 start, end, s);
11208 err = -EINVAL;
11209 goto cleanup;
11210 }
11211 tmp = realloc(*mask, end + 1);
11212 if (!tmp) {
11213 err = -ENOMEM;
11214 goto cleanup;
11215 }
11216 *mask = tmp;
11217 memset(tmp + *mask_sz, 0, start - *mask_sz);
11218 memset(tmp + start, 1, end - start + 1);
11219 *mask_sz = end + 1;
11220 s += len;
11221 }
11222 if (!*mask_sz) {
11223 pr_warn("Empty CPU range\n");
11224 return -EINVAL;
11225 }
11226 return 0;
11227cleanup:
11228 free(*mask);
11229 *mask = NULL;
11230 return err;
11231}
11232
11233int parse_cpu_mask_file(const char *fcpu, bool **mask, int *mask_sz)
11234{
11235 int fd, err = 0, len;
11236 char buf[128];
6446b315
HL
11237
11238 fd = open(fcpu, O_RDONLY);
11239 if (fd < 0) {
6803ee25
AN
11240 err = -errno;
11241 pr_warn("Failed to open cpu mask file %s: %d\n", fcpu, err);
11242 return err;
6446b315
HL
11243 }
11244 len = read(fd, buf, sizeof(buf));
11245 close(fd);
11246 if (len <= 0) {
6803ee25
AN
11247 err = len ? -errno : -EINVAL;
11248 pr_warn("Failed to read cpu mask from %s: %d\n", fcpu, err);
11249 return err;
6446b315 11250 }
6803ee25
AN
11251 if (len >= sizeof(buf)) {
11252 pr_warn("CPU mask is too big in file %s\n", fcpu);
11253 return -E2BIG;
6446b315
HL
11254 }
11255 buf[len] = '\0';
11256
6803ee25
AN
11257 return parse_cpu_mask_str(buf, mask, mask_sz);
11258}
11259
11260int libbpf_num_possible_cpus(void)
11261{
11262 static const char *fcpu = "/sys/devices/system/cpu/possible";
11263 static int cpus;
11264 int err, n, i, tmp_cpus;
11265 bool *mask;
11266
11267 tmp_cpus = READ_ONCE(cpus);
11268 if (tmp_cpus > 0)
11269 return tmp_cpus;
11270
11271 err = parse_cpu_mask_file(fcpu, &mask, &n);
11272 if (err)
e9fc3ce9 11273 return libbpf_err(err);
6803ee25
AN
11274
11275 tmp_cpus = 0;
11276 for (i = 0; i < n; i++) {
11277 if (mask[i])
11278 tmp_cpus++;
6446b315 11279 }
6803ee25 11280 free(mask);
56fbc241
TC
11281
11282 WRITE_ONCE(cpus, tmp_cpus);
11283 return tmp_cpus;
6446b315 11284}
d66562fb
AN
11285
11286int bpf_object__open_skeleton(struct bpf_object_skeleton *s,
11287 const struct bpf_object_open_opts *opts)
11288{
11289 DECLARE_LIBBPF_OPTS(bpf_object_open_opts, skel_opts,
11290 .object_name = s->name,
11291 );
11292 struct bpf_object *obj;
e9fc3ce9 11293 int i, err;
d66562fb
AN
11294
11295 /* Attempt to preserve opts->object_name, unless overriden by user
11296 * explicitly. Overwriting object name for skeletons is discouraged,
11297 * as it breaks global data maps, because they contain object name
11298 * prefix as their own map name prefix. When skeleton is generated,
11299 * bpftool is making an assumption that this name will stay the same.
11300 */
11301 if (opts) {
11302 memcpy(&skel_opts, opts, sizeof(*opts));
11303 if (!opts->object_name)
11304 skel_opts.object_name = s->name;
11305 }
11306
11307 obj = bpf_object__open_mem(s->data, s->data_sz, &skel_opts);
e9fc3ce9
AN
11308 err = libbpf_get_error(obj);
11309 if (err) {
11310 pr_warn("failed to initialize skeleton BPF object '%s': %d\n",
11311 s->name, err);
11312 return libbpf_err(err);
d66562fb
AN
11313 }
11314
11315 *s->obj = obj;
11316
11317 for (i = 0; i < s->map_cnt; i++) {
11318 struct bpf_map **map = s->maps[i].map;
11319 const char *name = s->maps[i].name;
11320 void **mmaped = s->maps[i].mmaped;
11321
11322 *map = bpf_object__find_map_by_name(obj, name);
11323 if (!*map) {
11324 pr_warn("failed to find skeleton map '%s'\n", name);
e9fc3ce9 11325 return libbpf_err(-ESRCH);
d66562fb
AN
11326 }
11327
2ad97d47 11328 /* externs shouldn't be pre-setup from user code */
81bfdd08 11329 if (mmaped && (*map)->libbpf_type != LIBBPF_MAP_KCONFIG)
d66562fb
AN
11330 *mmaped = (*map)->mmaped;
11331 }
11332
11333 for (i = 0; i < s->prog_cnt; i++) {
11334 struct bpf_program **prog = s->progs[i].prog;
11335 const char *name = s->progs[i].name;
11336
11337 *prog = bpf_object__find_program_by_name(obj, name);
11338 if (!*prog) {
11339 pr_warn("failed to find skeleton program '%s'\n", name);
e9fc3ce9 11340 return libbpf_err(-ESRCH);
d66562fb
AN
11341 }
11342 }
11343
11344 return 0;
11345}
11346
11347int bpf_object__load_skeleton(struct bpf_object_skeleton *s)
11348{
11349 int i, err;
11350
11351 err = bpf_object__load(*s->obj);
11352 if (err) {
11353 pr_warn("failed to load BPF skeleton '%s': %d\n", s->name, err);
e9fc3ce9 11354 return libbpf_err(err);
d66562fb
AN
11355 }
11356
11357 for (i = 0; i < s->map_cnt; i++) {
11358 struct bpf_map *map = *s->maps[i].map;
11359 size_t mmap_sz = bpf_map_mmap_sz(map);
11360 int prot, map_fd = bpf_map__fd(map);
11361 void **mmaped = s->maps[i].mmaped;
d66562fb
AN
11362
11363 if (!mmaped)
11364 continue;
11365
11366 if (!(map->def.map_flags & BPF_F_MMAPABLE)) {
11367 *mmaped = NULL;
11368 continue;
11369 }
11370
11371 if (map->def.map_flags & BPF_F_RDONLY_PROG)
11372 prot = PROT_READ;
11373 else
11374 prot = PROT_READ | PROT_WRITE;
11375
11376 /* Remap anonymous mmap()-ed "map initialization image" as
11377 * a BPF map-backed mmap()-ed memory, but preserving the same
11378 * memory address. This will cause kernel to change process'
11379 * page table to point to a different piece of kernel memory,
11380 * but from userspace point of view memory address (and its
11381 * contents, being identical at this point) will stay the
11382 * same. This mapping will be released by bpf_object__close()
11383 * as per normal clean up procedure, so we don't need to worry
11384 * about it from skeleton's clean up perspective.
11385 */
2ad97d47
AN
11386 *mmaped = mmap(map->mmaped, mmap_sz, prot,
11387 MAP_SHARED | MAP_FIXED, map_fd, 0);
11388 if (*mmaped == MAP_FAILED) {
d66562fb
AN
11389 err = -errno;
11390 *mmaped = NULL;
11391 pr_warn("failed to re-mmap() map '%s': %d\n",
11392 bpf_map__name(map), err);
e9fc3ce9 11393 return libbpf_err(err);
d66562fb
AN
11394 }
11395 }
11396
11397 return 0;
11398}
11399
11400int bpf_object__attach_skeleton(struct bpf_object_skeleton *s)
11401{
e9fc3ce9 11402 int i, err;
d66562fb
AN
11403
11404 for (i = 0; i < s->prog_cnt; i++) {
11405 struct bpf_program *prog = *s->progs[i].prog;
11406 struct bpf_link **link = s->progs[i].link;
d66562fb 11407
d9297581
AN
11408 if (!prog->load)
11409 continue;
11410
5532dfd4
AN
11411 /* auto-attaching not supported for this program */
11412 if (!prog->sec_def || !prog->sec_def->attach_fn)
d66562fb
AN
11413 continue;
11414
942025c9 11415 *link = bpf_program__attach(prog);
e9fc3ce9
AN
11416 err = libbpf_get_error(*link);
11417 if (err) {
11418 pr_warn("failed to auto-attach program '%s': %d\n",
11419 bpf_program__name(prog), err);
11420 return libbpf_err(err);
d66562fb
AN
11421 }
11422 }
11423
11424 return 0;
11425}
11426
11427void bpf_object__detach_skeleton(struct bpf_object_skeleton *s)
11428{
11429 int i;
11430
11431 for (i = 0; i < s->prog_cnt; i++) {
11432 struct bpf_link **link = s->progs[i].link;
11433
50450fc7 11434 bpf_link__destroy(*link);
d66562fb
AN
11435 *link = NULL;
11436 }
11437}
11438
11439void bpf_object__destroy_skeleton(struct bpf_object_skeleton *s)
11440{
11441 if (s->progs)
11442 bpf_object__detach_skeleton(s);
11443 if (s->obj)
11444 bpf_object__close(*s->obj);
11445 free(s->maps);
11446 free(s->progs);
11447 free(s);
11448}