GNU Linux-libre 4.19.211-gnu1
[releases.git] / tools / lib / bpf / libbpf.c
1 // SPDX-License-Identifier: LGPL-2.1
2
3 /*
4  * Common eBPF ELF object loading operations.
5  *
6  * Copyright (C) 2013-2015 Alexei Starovoitov <ast@kernel.org>
7  * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com>
8  * Copyright (C) 2015 Huawei Inc.
9  * Copyright (C) 2017 Nicira, Inc.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation;
14  * version 2.1 of the License (not later!)
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with this program; if not,  see <http://www.gnu.org/licenses>
23  */
24
25 #ifndef _GNU_SOURCE
26 #define _GNU_SOURCE
27 #endif
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <stdarg.h>
31 #include <libgen.h>
32 #include <inttypes.h>
33 #include <string.h>
34 #include <unistd.h>
35 #include <fcntl.h>
36 #include <errno.h>
37 #include <perf-sys.h>
38 #include <asm/unistd.h>
39 #include <linux/err.h>
40 #include <linux/kernel.h>
41 #include <linux/bpf.h>
42 #include <linux/btf.h>
43 #include <linux/list.h>
44 #include <linux/limits.h>
45 #include <sys/stat.h>
46 #include <sys/types.h>
47 #include <sys/vfs.h>
48 #include <tools/libc_compat.h>
49 #include <libelf.h>
50 #include <gelf.h>
51
52 #include "libbpf.h"
53 #include "bpf.h"
54 #include "btf.h"
55 #include "str_error.h"
56
57 #ifndef EM_BPF
58 #define EM_BPF 247
59 #endif
60
61 #ifndef BPF_FS_MAGIC
62 #define BPF_FS_MAGIC            0xcafe4a11
63 #endif
64
65 #define __printf(a, b)  __attribute__((format(printf, a, b)))
66
67 __printf(1, 2)
68 static int __base_pr(const char *format, ...)
69 {
70         va_list args;
71         int err;
72
73         va_start(args, format);
74         err = vfprintf(stderr, format, args);
75         va_end(args);
76         return err;
77 }
78
79 static __printf(1, 2) libbpf_print_fn_t __pr_warning = __base_pr;
80 static __printf(1, 2) libbpf_print_fn_t __pr_info = __base_pr;
81 static __printf(1, 2) libbpf_print_fn_t __pr_debug;
82
83 #define __pr(func, fmt, ...)    \
84 do {                            \
85         if ((func))             \
86                 (func)("libbpf: " fmt, ##__VA_ARGS__); \
87 } while (0)
88
89 #define pr_warning(fmt, ...)    __pr(__pr_warning, fmt, ##__VA_ARGS__)
90 #define pr_info(fmt, ...)       __pr(__pr_info, fmt, ##__VA_ARGS__)
91 #define pr_debug(fmt, ...)      __pr(__pr_debug, fmt, ##__VA_ARGS__)
92
93 void libbpf_set_print(libbpf_print_fn_t warn,
94                       libbpf_print_fn_t info,
95                       libbpf_print_fn_t debug)
96 {
97         __pr_warning = warn;
98         __pr_info = info;
99         __pr_debug = debug;
100 }
101
102 #define STRERR_BUFSIZE  128
103
104 #define CHECK_ERR(action, err, out) do {        \
105         err = action;                   \
106         if (err)                        \
107                 goto out;               \
108 } while(0)
109
110
111 /* Copied from tools/perf/util/util.h */
112 #ifndef zfree
113 # define zfree(ptr) ({ free(*ptr); *ptr = NULL; })
114 #endif
115
116 #ifndef zclose
117 # define zclose(fd) ({                  \
118         int ___err = 0;                 \
119         if ((fd) >= 0)                  \
120                 ___err = close((fd));   \
121         fd = -1;                        \
122         ___err; })
123 #endif
124
125 #ifdef HAVE_LIBELF_MMAP_SUPPORT
126 # define LIBBPF_ELF_C_READ_MMAP ELF_C_READ_MMAP
127 #else
128 # define LIBBPF_ELF_C_READ_MMAP ELF_C_READ
129 #endif
130
131 /*
132  * bpf_prog should be a better name but it has been used in
133  * linux/filter.h.
134  */
135 struct bpf_program {
136         /* Index in elf obj file, for relocation use. */
137         int idx;
138         char *name;
139         int prog_ifindex;
140         char *section_name;
141         struct bpf_insn *insns;
142         size_t insns_cnt, main_prog_cnt;
143         enum bpf_prog_type type;
144
145         struct reloc_desc {
146                 enum {
147                         RELO_LD64,
148                         RELO_CALL,
149                 } type;
150                 int insn_idx;
151                 union {
152                         int map_idx;
153                         int text_off;
154                 };
155         } *reloc_desc;
156         int nr_reloc;
157
158         struct {
159                 int nr;
160                 int *fds;
161         } instances;
162         bpf_program_prep_t preprocessor;
163
164         struct bpf_object *obj;
165         void *priv;
166         bpf_program_clear_priv_t clear_priv;
167
168         enum bpf_attach_type expected_attach_type;
169 };
170
171 struct bpf_map {
172         int fd;
173         char *name;
174         size_t offset;
175         int map_ifindex;
176         struct bpf_map_def def;
177         __u32 btf_key_type_id;
178         __u32 btf_value_type_id;
179         void *priv;
180         bpf_map_clear_priv_t clear_priv;
181 };
182
183 static LIST_HEAD(bpf_objects_list);
184
185 struct bpf_object {
186         char license[64];
187         u32 kern_version;
188
189         struct bpf_program *programs;
190         size_t nr_programs;
191         struct bpf_map *maps;
192         size_t nr_maps;
193
194         bool loaded;
195         bool has_pseudo_calls;
196
197         /*
198          * Information when doing elf related work. Only valid if fd
199          * is valid.
200          */
201         struct {
202                 int fd;
203                 void *obj_buf;
204                 size_t obj_buf_sz;
205                 Elf *elf;
206                 GElf_Ehdr ehdr;
207                 Elf_Data *symbols;
208                 size_t strtabidx;
209                 struct {
210                         GElf_Shdr shdr;
211                         Elf_Data *data;
212                 } *reloc;
213                 int nr_reloc;
214                 int maps_shndx;
215                 int text_shndx;
216         } efile;
217         /*
218          * All loaded bpf_object is linked in a list, which is
219          * hidden to caller. bpf_objects__<func> handlers deal with
220          * all objects.
221          */
222         struct list_head list;
223
224         struct btf *btf;
225
226         void *priv;
227         bpf_object_clear_priv_t clear_priv;
228
229         char path[];
230 };
231 #define obj_elf_valid(o)        ((o)->efile.elf)
232
233 static void bpf_program__unload(struct bpf_program *prog)
234 {
235         int i;
236
237         if (!prog)
238                 return;
239
240         /*
241          * If the object is opened but the program was never loaded,
242          * it is possible that prog->instances.nr == -1.
243          */
244         if (prog->instances.nr > 0) {
245                 for (i = 0; i < prog->instances.nr; i++)
246                         zclose(prog->instances.fds[i]);
247         } else if (prog->instances.nr != -1) {
248                 pr_warning("Internal error: instances.nr is %d\n",
249                            prog->instances.nr);
250         }
251
252         prog->instances.nr = -1;
253         zfree(&prog->instances.fds);
254 }
255
256 static void bpf_program__exit(struct bpf_program *prog)
257 {
258         if (!prog)
259                 return;
260
261         if (prog->clear_priv)
262                 prog->clear_priv(prog, prog->priv);
263
264         prog->priv = NULL;
265         prog->clear_priv = NULL;
266
267         bpf_program__unload(prog);
268         zfree(&prog->name);
269         zfree(&prog->section_name);
270         zfree(&prog->insns);
271         zfree(&prog->reloc_desc);
272
273         prog->nr_reloc = 0;
274         prog->insns_cnt = 0;
275         prog->idx = -1;
276 }
277
278 static int
279 bpf_program__init(void *data, size_t size, char *section_name, int idx,
280                   struct bpf_program *prog)
281 {
282         if (size < sizeof(struct bpf_insn)) {
283                 pr_warning("corrupted section '%s'\n", section_name);
284                 return -EINVAL;
285         }
286
287         bzero(prog, sizeof(*prog));
288
289         prog->section_name = strdup(section_name);
290         if (!prog->section_name) {
291                 pr_warning("failed to alloc name for prog under section(%d) %s\n",
292                            idx, section_name);
293                 goto errout;
294         }
295
296         prog->insns = malloc(size);
297         if (!prog->insns) {
298                 pr_warning("failed to alloc insns for prog under section %s\n",
299                            section_name);
300                 goto errout;
301         }
302         prog->insns_cnt = size / sizeof(struct bpf_insn);
303         memcpy(prog->insns, data,
304                prog->insns_cnt * sizeof(struct bpf_insn));
305         prog->idx = idx;
306         prog->instances.fds = NULL;
307         prog->instances.nr = -1;
308         prog->type = BPF_PROG_TYPE_KPROBE;
309
310         return 0;
311 errout:
312         bpf_program__exit(prog);
313         return -ENOMEM;
314 }
315
316 static int
317 bpf_object__add_program(struct bpf_object *obj, void *data, size_t size,
318                         char *section_name, int idx)
319 {
320         struct bpf_program prog, *progs;
321         int nr_progs, err;
322
323         err = bpf_program__init(data, size, section_name, idx, &prog);
324         if (err)
325                 return err;
326
327         progs = obj->programs;
328         nr_progs = obj->nr_programs;
329
330         progs = reallocarray(progs, nr_progs + 1, sizeof(progs[0]));
331         if (!progs) {
332                 /*
333                  * In this case the original obj->programs
334                  * is still valid, so don't need special treat for
335                  * bpf_close_object().
336                  */
337                 pr_warning("failed to alloc a new program under section '%s'\n",
338                            section_name);
339                 bpf_program__exit(&prog);
340                 return -ENOMEM;
341         }
342
343         pr_debug("found program %s\n", prog.section_name);
344         obj->programs = progs;
345         obj->nr_programs = nr_progs + 1;
346         prog.obj = obj;
347         progs[nr_progs] = prog;
348         return 0;
349 }
350
351 static int
352 bpf_object__init_prog_names(struct bpf_object *obj)
353 {
354         Elf_Data *symbols = obj->efile.symbols;
355         struct bpf_program *prog;
356         size_t pi, si;
357
358         for (pi = 0; pi < obj->nr_programs; pi++) {
359                 const char *name = NULL;
360
361                 prog = &obj->programs[pi];
362
363                 for (si = 0; si < symbols->d_size / sizeof(GElf_Sym) && !name;
364                      si++) {
365                         GElf_Sym sym;
366
367                         if (!gelf_getsym(symbols, si, &sym))
368                                 continue;
369                         if (sym.st_shndx != prog->idx)
370                                 continue;
371                         if (GELF_ST_BIND(sym.st_info) != STB_GLOBAL)
372                                 continue;
373
374                         name = elf_strptr(obj->efile.elf,
375                                           obj->efile.strtabidx,
376                                           sym.st_name);
377                         if (!name) {
378                                 pr_warning("failed to get sym name string for prog %s\n",
379                                            prog->section_name);
380                                 return -LIBBPF_ERRNO__LIBELF;
381                         }
382                 }
383
384                 if (!name && prog->idx == obj->efile.text_shndx)
385                         name = ".text";
386
387                 if (!name) {
388                         pr_warning("failed to find sym for prog %s\n",
389                                    prog->section_name);
390                         return -EINVAL;
391                 }
392
393                 prog->name = strdup(name);
394                 if (!prog->name) {
395                         pr_warning("failed to allocate memory for prog sym %s\n",
396                                    name);
397                         return -ENOMEM;
398                 }
399         }
400
401         return 0;
402 }
403
404 static struct bpf_object *bpf_object__new(const char *path,
405                                           void *obj_buf,
406                                           size_t obj_buf_sz)
407 {
408         struct bpf_object *obj;
409
410         obj = calloc(1, sizeof(struct bpf_object) + strlen(path) + 1);
411         if (!obj) {
412                 pr_warning("alloc memory failed for %s\n", path);
413                 return ERR_PTR(-ENOMEM);
414         }
415
416         strcpy(obj->path, path);
417         obj->efile.fd = -1;
418
419         /*
420          * Caller of this function should also calls
421          * bpf_object__elf_finish() after data collection to return
422          * obj_buf to user. If not, we should duplicate the buffer to
423          * avoid user freeing them before elf finish.
424          */
425         obj->efile.obj_buf = obj_buf;
426         obj->efile.obj_buf_sz = obj_buf_sz;
427         obj->efile.maps_shndx = -1;
428
429         obj->loaded = false;
430
431         INIT_LIST_HEAD(&obj->list);
432         list_add(&obj->list, &bpf_objects_list);
433         return obj;
434 }
435
436 static void bpf_object__elf_finish(struct bpf_object *obj)
437 {
438         if (!obj_elf_valid(obj))
439                 return;
440
441         if (obj->efile.elf) {
442                 elf_end(obj->efile.elf);
443                 obj->efile.elf = NULL;
444         }
445         obj->efile.symbols = NULL;
446
447         zfree(&obj->efile.reloc);
448         obj->efile.nr_reloc = 0;
449         zclose(obj->efile.fd);
450         obj->efile.obj_buf = NULL;
451         obj->efile.obj_buf_sz = 0;
452 }
453
454 static int bpf_object__elf_init(struct bpf_object *obj)
455 {
456         int err = 0;
457         GElf_Ehdr *ep;
458
459         if (obj_elf_valid(obj)) {
460                 pr_warning("elf init: internal error\n");
461                 return -LIBBPF_ERRNO__LIBELF;
462         }
463
464         if (obj->efile.obj_buf_sz > 0) {
465                 /*
466                  * obj_buf should have been validated by
467                  * bpf_object__open_buffer().
468                  */
469                 obj->efile.elf = elf_memory(obj->efile.obj_buf,
470                                             obj->efile.obj_buf_sz);
471         } else {
472                 obj->efile.fd = open(obj->path, O_RDONLY);
473                 if (obj->efile.fd < 0) {
474                         char errmsg[STRERR_BUFSIZE];
475                         char *cp = str_error(errno, errmsg, sizeof(errmsg));
476
477                         pr_warning("failed to open %s: %s\n", obj->path, cp);
478                         return -errno;
479                 }
480
481                 obj->efile.elf = elf_begin(obj->efile.fd,
482                                 LIBBPF_ELF_C_READ_MMAP,
483                                 NULL);
484         }
485
486         if (!obj->efile.elf) {
487                 pr_warning("failed to open %s as ELF file\n",
488                                 obj->path);
489                 err = -LIBBPF_ERRNO__LIBELF;
490                 goto errout;
491         }
492
493         if (!gelf_getehdr(obj->efile.elf, &obj->efile.ehdr)) {
494                 pr_warning("failed to get EHDR from %s\n",
495                                 obj->path);
496                 err = -LIBBPF_ERRNO__FORMAT;
497                 goto errout;
498         }
499         ep = &obj->efile.ehdr;
500
501         /* Old LLVM set e_machine to EM_NONE */
502         if ((ep->e_type != ET_REL) || (ep->e_machine && (ep->e_machine != EM_BPF))) {
503                 pr_warning("%s is not an eBPF object file\n",
504                         obj->path);
505                 err = -LIBBPF_ERRNO__FORMAT;
506                 goto errout;
507         }
508
509         return 0;
510 errout:
511         bpf_object__elf_finish(obj);
512         return err;
513 }
514
515 static int
516 bpf_object__check_endianness(struct bpf_object *obj)
517 {
518         static unsigned int const endian = 1;
519
520         switch (obj->efile.ehdr.e_ident[EI_DATA]) {
521         case ELFDATA2LSB:
522                 /* We are big endian, BPF obj is little endian. */
523                 if (*(unsigned char const *)&endian != 1)
524                         goto mismatch;
525                 break;
526
527         case ELFDATA2MSB:
528                 /* We are little endian, BPF obj is big endian. */
529                 if (*(unsigned char const *)&endian != 0)
530                         goto mismatch;
531                 break;
532         default:
533                 return -LIBBPF_ERRNO__ENDIAN;
534         }
535
536         return 0;
537
538 mismatch:
539         pr_warning("Error: endianness mismatch.\n");
540         return -LIBBPF_ERRNO__ENDIAN;
541 }
542
543 static int
544 bpf_object__init_license(struct bpf_object *obj,
545                          void *data, size_t size)
546 {
547         memcpy(obj->license, data,
548                min(size, sizeof(obj->license) - 1));
549         pr_debug("license of %s is %s\n", obj->path, obj->license);
550         return 0;
551 }
552
553 static int
554 bpf_object__init_kversion(struct bpf_object *obj,
555                           void *data, size_t size)
556 {
557         u32 kver;
558
559         if (size != sizeof(kver)) {
560                 pr_warning("invalid kver section in %s\n", obj->path);
561                 return -LIBBPF_ERRNO__FORMAT;
562         }
563         memcpy(&kver, data, sizeof(kver));
564         obj->kern_version = kver;
565         pr_debug("kernel version of %s is %x\n", obj->path,
566                  obj->kern_version);
567         return 0;
568 }
569
570 static int compare_bpf_map(const void *_a, const void *_b)
571 {
572         const struct bpf_map *a = _a;
573         const struct bpf_map *b = _b;
574
575         return a->offset - b->offset;
576 }
577
578 static int
579 bpf_object__init_maps(struct bpf_object *obj)
580 {
581         int i, map_idx, map_def_sz, nr_maps = 0;
582         Elf_Scn *scn;
583         Elf_Data *data;
584         Elf_Data *symbols = obj->efile.symbols;
585
586         if (obj->efile.maps_shndx < 0)
587                 return -EINVAL;
588         if (!symbols)
589                 return -EINVAL;
590
591         scn = elf_getscn(obj->efile.elf, obj->efile.maps_shndx);
592         if (scn)
593                 data = elf_getdata(scn, NULL);
594         if (!scn || !data) {
595                 pr_warning("failed to get Elf_Data from map section %d\n",
596                            obj->efile.maps_shndx);
597                 return -EINVAL;
598         }
599
600         /*
601          * Count number of maps. Each map has a name.
602          * Array of maps is not supported: only the first element is
603          * considered.
604          *
605          * TODO: Detect array of map and report error.
606          */
607         for (i = 0; i < symbols->d_size / sizeof(GElf_Sym); i++) {
608                 GElf_Sym sym;
609
610                 if (!gelf_getsym(symbols, i, &sym))
611                         continue;
612                 if (sym.st_shndx != obj->efile.maps_shndx)
613                         continue;
614                 nr_maps++;
615         }
616
617         /* Alloc obj->maps and fill nr_maps. */
618         pr_debug("maps in %s: %d maps in %zd bytes\n", obj->path,
619                  nr_maps, data->d_size);
620
621         if (!nr_maps)
622                 return 0;
623
624         /* Assume equally sized map definitions */
625         map_def_sz = data->d_size / nr_maps;
626         if (!data->d_size || (data->d_size % nr_maps) != 0) {
627                 pr_warning("unable to determine map definition size "
628                            "section %s, %d maps in %zd bytes\n",
629                            obj->path, nr_maps, data->d_size);
630                 return -EINVAL;
631         }
632
633         obj->maps = calloc(nr_maps, sizeof(obj->maps[0]));
634         if (!obj->maps) {
635                 pr_warning("alloc maps for object failed\n");
636                 return -ENOMEM;
637         }
638         obj->nr_maps = nr_maps;
639
640         /*
641          * fill all fd with -1 so won't close incorrect
642          * fd (fd=0 is stdin) when failure (zclose won't close
643          * negative fd)).
644          */
645         for (i = 0; i < nr_maps; i++)
646                 obj->maps[i].fd = -1;
647
648         /*
649          * Fill obj->maps using data in "maps" section.
650          */
651         for (i = 0, map_idx = 0; i < symbols->d_size / sizeof(GElf_Sym); i++) {
652                 GElf_Sym sym;
653                 const char *map_name;
654                 struct bpf_map_def *def;
655
656                 if (!gelf_getsym(symbols, i, &sym))
657                         continue;
658                 if (sym.st_shndx != obj->efile.maps_shndx)
659                         continue;
660
661                 map_name = elf_strptr(obj->efile.elf,
662                                       obj->efile.strtabidx,
663                                       sym.st_name);
664                 obj->maps[map_idx].offset = sym.st_value;
665                 if (sym.st_value + map_def_sz > data->d_size) {
666                         pr_warning("corrupted maps section in %s: last map \"%s\" too small\n",
667                                    obj->path, map_name);
668                         return -EINVAL;
669                 }
670
671                 obj->maps[map_idx].name = strdup(map_name);
672                 if (!obj->maps[map_idx].name) {
673                         pr_warning("failed to alloc map name\n");
674                         return -ENOMEM;
675                 }
676                 pr_debug("map %d is \"%s\"\n", map_idx,
677                          obj->maps[map_idx].name);
678                 def = (struct bpf_map_def *)(data->d_buf + sym.st_value);
679                 /*
680                  * If the definition of the map in the object file fits in
681                  * bpf_map_def, copy it.  Any extra fields in our version
682                  * of bpf_map_def will default to zero as a result of the
683                  * calloc above.
684                  */
685                 if (map_def_sz <= sizeof(struct bpf_map_def)) {
686                         memcpy(&obj->maps[map_idx].def, def, map_def_sz);
687                 } else {
688                         /*
689                          * Here the map structure being read is bigger than what
690                          * we expect, truncate if the excess bits are all zero.
691                          * If they are not zero, reject this map as
692                          * incompatible.
693                          */
694                         char *b;
695                         for (b = ((char *)def) + sizeof(struct bpf_map_def);
696                              b < ((char *)def) + map_def_sz; b++) {
697                                 if (*b != 0) {
698                                         pr_warning("maps section in %s: \"%s\" "
699                                                    "has unrecognized, non-zero "
700                                                    "options\n",
701                                                    obj->path, map_name);
702                                         return -EINVAL;
703                                 }
704                         }
705                         memcpy(&obj->maps[map_idx].def, def,
706                                sizeof(struct bpf_map_def));
707                 }
708                 map_idx++;
709         }
710
711         qsort(obj->maps, obj->nr_maps, sizeof(obj->maps[0]), compare_bpf_map);
712         return 0;
713 }
714
715 static bool section_have_execinstr(struct bpf_object *obj, int idx)
716 {
717         Elf_Scn *scn;
718         GElf_Shdr sh;
719
720         scn = elf_getscn(obj->efile.elf, idx);
721         if (!scn)
722                 return false;
723
724         if (gelf_getshdr(scn, &sh) != &sh)
725                 return false;
726
727         if (sh.sh_flags & SHF_EXECINSTR)
728                 return true;
729
730         return false;
731 }
732
733 static int bpf_object__elf_collect(struct bpf_object *obj)
734 {
735         Elf *elf = obj->efile.elf;
736         GElf_Ehdr *ep = &obj->efile.ehdr;
737         Elf_Scn *scn = NULL;
738         int idx = 0, err = 0;
739
740         /* Elf is corrupted/truncated, avoid calling elf_strptr. */
741         if (!elf_rawdata(elf_getscn(elf, ep->e_shstrndx), NULL)) {
742                 pr_warning("failed to get e_shstrndx from %s\n",
743                            obj->path);
744                 return -LIBBPF_ERRNO__FORMAT;
745         }
746
747         while ((scn = elf_nextscn(elf, scn)) != NULL) {
748                 char *name;
749                 GElf_Shdr sh;
750                 Elf_Data *data;
751
752                 idx++;
753                 if (gelf_getshdr(scn, &sh) != &sh) {
754                         pr_warning("failed to get section(%d) header from %s\n",
755                                    idx, obj->path);
756                         err = -LIBBPF_ERRNO__FORMAT;
757                         goto out;
758                 }
759
760                 name = elf_strptr(elf, ep->e_shstrndx, sh.sh_name);
761                 if (!name) {
762                         pr_warning("failed to get section(%d) name from %s\n",
763                                    idx, obj->path);
764                         err = -LIBBPF_ERRNO__FORMAT;
765                         goto out;
766                 }
767
768                 data = elf_getdata(scn, 0);
769                 if (!data) {
770                         pr_warning("failed to get section(%d) data from %s(%s)\n",
771                                    idx, name, obj->path);
772                         err = -LIBBPF_ERRNO__FORMAT;
773                         goto out;
774                 }
775                 pr_debug("section(%d) %s, size %ld, link %d, flags %lx, type=%d\n",
776                          idx, name, (unsigned long)data->d_size,
777                          (int)sh.sh_link, (unsigned long)sh.sh_flags,
778                          (int)sh.sh_type);
779
780                 if (strcmp(name, "license") == 0)
781                         err = bpf_object__init_license(obj,
782                                                        data->d_buf,
783                                                        data->d_size);
784                 else if (strcmp(name, "version") == 0)
785                         err = bpf_object__init_kversion(obj,
786                                                         data->d_buf,
787                                                         data->d_size);
788                 else if (strcmp(name, "maps") == 0)
789                         obj->efile.maps_shndx = idx;
790                 else if (strcmp(name, BTF_ELF_SEC) == 0) {
791                         obj->btf = btf__new(data->d_buf, data->d_size,
792                                             __pr_debug);
793                         if (IS_ERR(obj->btf)) {
794                                 pr_warning("Error loading ELF section %s: %ld. Ignored and continue.\n",
795                                            BTF_ELF_SEC, PTR_ERR(obj->btf));
796                                 obj->btf = NULL;
797                         }
798                 } else if (sh.sh_type == SHT_SYMTAB) {
799                         if (obj->efile.symbols) {
800                                 pr_warning("bpf: multiple SYMTAB in %s\n",
801                                            obj->path);
802                                 err = -LIBBPF_ERRNO__FORMAT;
803                         } else {
804                                 obj->efile.symbols = data;
805                                 obj->efile.strtabidx = sh.sh_link;
806                         }
807                 } else if ((sh.sh_type == SHT_PROGBITS) &&
808                            (sh.sh_flags & SHF_EXECINSTR) &&
809                            (data->d_size > 0)) {
810                         if (strcmp(name, ".text") == 0)
811                                 obj->efile.text_shndx = idx;
812                         err = bpf_object__add_program(obj, data->d_buf,
813                                                       data->d_size, name, idx);
814                         if (err) {
815                                 char errmsg[STRERR_BUFSIZE];
816                                 char *cp = str_error(-err, errmsg, sizeof(errmsg));
817
818                                 pr_warning("failed to alloc program %s (%s): %s",
819                                            name, obj->path, cp);
820                         }
821                 } else if (sh.sh_type == SHT_REL) {
822                         void *reloc = obj->efile.reloc;
823                         int nr_reloc = obj->efile.nr_reloc + 1;
824                         int sec = sh.sh_info; /* points to other section */
825
826                         /* Only do relo for section with exec instructions */
827                         if (!section_have_execinstr(obj, sec)) {
828                                 pr_debug("skip relo %s(%d) for section(%d)\n",
829                                          name, idx, sec);
830                                 continue;
831                         }
832
833                         reloc = reallocarray(reloc, nr_reloc,
834                                              sizeof(*obj->efile.reloc));
835                         if (!reloc) {
836                                 pr_warning("realloc failed\n");
837                                 err = -ENOMEM;
838                         } else {
839                                 int n = nr_reloc - 1;
840
841                                 obj->efile.reloc = reloc;
842                                 obj->efile.nr_reloc = nr_reloc;
843
844                                 obj->efile.reloc[n].shdr = sh;
845                                 obj->efile.reloc[n].data = data;
846                         }
847                 } else {
848                         pr_debug("skip section(%d) %s\n", idx, name);
849                 }
850                 if (err)
851                         goto out;
852         }
853
854         if (!obj->efile.strtabidx || obj->efile.strtabidx >= idx) {
855                 pr_warning("Corrupted ELF file: index of strtab invalid\n");
856                 return LIBBPF_ERRNO__FORMAT;
857         }
858         if (obj->efile.maps_shndx >= 0) {
859                 err = bpf_object__init_maps(obj);
860                 if (err)
861                         goto out;
862         }
863         err = bpf_object__init_prog_names(obj);
864 out:
865         return err;
866 }
867
868 static struct bpf_program *
869 bpf_object__find_prog_by_idx(struct bpf_object *obj, int idx)
870 {
871         struct bpf_program *prog;
872         size_t i;
873
874         for (i = 0; i < obj->nr_programs; i++) {
875                 prog = &obj->programs[i];
876                 if (prog->idx == idx)
877                         return prog;
878         }
879         return NULL;
880 }
881
882 struct bpf_program *
883 bpf_object__find_program_by_title(struct bpf_object *obj, const char *title)
884 {
885         struct bpf_program *pos;
886
887         bpf_object__for_each_program(pos, obj) {
888                 if (pos->section_name && !strcmp(pos->section_name, title))
889                         return pos;
890         }
891         return NULL;
892 }
893
894 static int
895 bpf_program__collect_reloc(struct bpf_program *prog, GElf_Shdr *shdr,
896                            Elf_Data *data, struct bpf_object *obj)
897 {
898         Elf_Data *symbols = obj->efile.symbols;
899         int text_shndx = obj->efile.text_shndx;
900         int maps_shndx = obj->efile.maps_shndx;
901         struct bpf_map *maps = obj->maps;
902         size_t nr_maps = obj->nr_maps;
903         int i, nrels;
904
905         pr_debug("collecting relocating info for: '%s'\n",
906                  prog->section_name);
907         nrels = shdr->sh_size / shdr->sh_entsize;
908
909         prog->reloc_desc = malloc(sizeof(*prog->reloc_desc) * nrels);
910         if (!prog->reloc_desc) {
911                 pr_warning("failed to alloc memory in relocation\n");
912                 return -ENOMEM;
913         }
914         prog->nr_reloc = nrels;
915
916         for (i = 0; i < nrels; i++) {
917                 GElf_Sym sym;
918                 GElf_Rel rel;
919                 unsigned int insn_idx;
920                 struct bpf_insn *insns = prog->insns;
921                 size_t map_idx;
922
923                 if (!gelf_getrel(data, i, &rel)) {
924                         pr_warning("relocation: failed to get %d reloc\n", i);
925                         return -LIBBPF_ERRNO__FORMAT;
926                 }
927
928                 if (!gelf_getsym(symbols,
929                                  GELF_R_SYM(rel.r_info),
930                                  &sym)) {
931                         pr_warning("relocation: symbol %"PRIx64" not found\n",
932                                    GELF_R_SYM(rel.r_info));
933                         return -LIBBPF_ERRNO__FORMAT;
934                 }
935                 pr_debug("relo for %lld value %lld name %d\n",
936                          (long long) (rel.r_info >> 32),
937                          (long long) sym.st_value, sym.st_name);
938
939                 if (sym.st_shndx != maps_shndx && sym.st_shndx != text_shndx) {
940                         pr_warning("Program '%s' contains non-map related relo data pointing to section %u\n",
941                                    prog->section_name, sym.st_shndx);
942                         return -LIBBPF_ERRNO__RELOC;
943                 }
944
945                 insn_idx = rel.r_offset / sizeof(struct bpf_insn);
946                 pr_debug("relocation: insn_idx=%u\n", insn_idx);
947
948                 if (insns[insn_idx].code == (BPF_JMP | BPF_CALL)) {
949                         if (insns[insn_idx].src_reg != BPF_PSEUDO_CALL) {
950                                 pr_warning("incorrect bpf_call opcode\n");
951                                 return -LIBBPF_ERRNO__RELOC;
952                         }
953                         prog->reloc_desc[i].type = RELO_CALL;
954                         prog->reloc_desc[i].insn_idx = insn_idx;
955                         prog->reloc_desc[i].text_off = sym.st_value;
956                         obj->has_pseudo_calls = true;
957                         continue;
958                 }
959
960                 if (insns[insn_idx].code != (BPF_LD | BPF_IMM | BPF_DW)) {
961                         pr_warning("bpf: relocation: invalid relo for insns[%d].code 0x%x\n",
962                                    insn_idx, insns[insn_idx].code);
963                         return -LIBBPF_ERRNO__RELOC;
964                 }
965
966                 /* TODO: 'maps' is sorted. We can use bsearch to make it faster. */
967                 for (map_idx = 0; map_idx < nr_maps; map_idx++) {
968                         if (maps[map_idx].offset == sym.st_value) {
969                                 pr_debug("relocation: find map %zd (%s) for insn %u\n",
970                                          map_idx, maps[map_idx].name, insn_idx);
971                                 break;
972                         }
973                 }
974
975                 if (map_idx >= nr_maps) {
976                         pr_warning("bpf relocation: map_idx %d large than %d\n",
977                                    (int)map_idx, (int)nr_maps - 1);
978                         return -LIBBPF_ERRNO__RELOC;
979                 }
980
981                 prog->reloc_desc[i].type = RELO_LD64;
982                 prog->reloc_desc[i].insn_idx = insn_idx;
983                 prog->reloc_desc[i].map_idx = map_idx;
984         }
985         return 0;
986 }
987
988 static int bpf_map_find_btf_info(struct bpf_map *map, const struct btf *btf)
989 {
990         const struct btf_type *container_type;
991         const struct btf_member *key, *value;
992         struct bpf_map_def *def = &map->def;
993         const size_t max_name = 256;
994         char container_name[max_name];
995         __s64 key_size, value_size;
996         __s32 container_id;
997
998         if (snprintf(container_name, max_name, "____btf_map_%s", map->name) ==
999             max_name) {
1000                 pr_warning("map:%s length of '____btf_map_%s' is too long\n",
1001                            map->name, map->name);
1002                 return -EINVAL;
1003         }
1004
1005         container_id = btf__find_by_name(btf, container_name);
1006         if (container_id < 0) {
1007                 pr_debug("map:%s container_name:%s cannot be found in BTF. Missing BPF_ANNOTATE_KV_PAIR?\n",
1008                          map->name, container_name);
1009                 return container_id;
1010         }
1011
1012         container_type = btf__type_by_id(btf, container_id);
1013         if (!container_type) {
1014                 pr_warning("map:%s cannot find BTF type for container_id:%u\n",
1015                            map->name, container_id);
1016                 return -EINVAL;
1017         }
1018
1019         if (BTF_INFO_KIND(container_type->info) != BTF_KIND_STRUCT ||
1020             BTF_INFO_VLEN(container_type->info) < 2) {
1021                 pr_warning("map:%s container_name:%s is an invalid container struct\n",
1022                            map->name, container_name);
1023                 return -EINVAL;
1024         }
1025
1026         key = (struct btf_member *)(container_type + 1);
1027         value = key + 1;
1028
1029         key_size = btf__resolve_size(btf, key->type);
1030         if (key_size < 0) {
1031                 pr_warning("map:%s invalid BTF key_type_size\n",
1032                            map->name);
1033                 return key_size;
1034         }
1035
1036         if (def->key_size != key_size) {
1037                 pr_warning("map:%s btf_key_type_size:%u != map_def_key_size:%u\n",
1038                            map->name, (__u32)key_size, def->key_size);
1039                 return -EINVAL;
1040         }
1041
1042         value_size = btf__resolve_size(btf, value->type);
1043         if (value_size < 0) {
1044                 pr_warning("map:%s invalid BTF value_type_size\n", map->name);
1045                 return value_size;
1046         }
1047
1048         if (def->value_size != value_size) {
1049                 pr_warning("map:%s btf_value_type_size:%u != map_def_value_size:%u\n",
1050                            map->name, (__u32)value_size, def->value_size);
1051                 return -EINVAL;
1052         }
1053
1054         map->btf_key_type_id = key->type;
1055         map->btf_value_type_id = value->type;
1056
1057         return 0;
1058 }
1059
1060 int bpf_map__reuse_fd(struct bpf_map *map, int fd)
1061 {
1062         struct bpf_map_info info = {};
1063         __u32 len = sizeof(info);
1064         int new_fd, err;
1065         char *new_name;
1066
1067         err = bpf_obj_get_info_by_fd(fd, &info, &len);
1068         if (err)
1069                 return err;
1070
1071         new_name = strdup(info.name);
1072         if (!new_name)
1073                 return -errno;
1074
1075         new_fd = open("/", O_RDONLY | O_CLOEXEC);
1076         if (new_fd < 0) {
1077                 err = -errno;
1078                 goto err_free_new_name;
1079         }
1080
1081         new_fd = dup3(fd, new_fd, O_CLOEXEC);
1082         if (new_fd < 0) {
1083                 err = -errno;
1084                 goto err_close_new_fd;
1085         }
1086
1087         err = zclose(map->fd);
1088         if (err) {
1089                 err = -errno;
1090                 goto err_close_new_fd;
1091         }
1092         free(map->name);
1093
1094         map->fd = new_fd;
1095         map->name = new_name;
1096         map->def.type = info.type;
1097         map->def.key_size = info.key_size;
1098         map->def.value_size = info.value_size;
1099         map->def.max_entries = info.max_entries;
1100         map->def.map_flags = info.map_flags;
1101         map->btf_key_type_id = info.btf_key_type_id;
1102         map->btf_value_type_id = info.btf_value_type_id;
1103
1104         return 0;
1105
1106 err_close_new_fd:
1107         close(new_fd);
1108 err_free_new_name:
1109         free(new_name);
1110         return err;
1111 }
1112
1113 static int
1114 bpf_object__create_maps(struct bpf_object *obj)
1115 {
1116         struct bpf_create_map_attr create_attr = {};
1117         unsigned int i;
1118         int err;
1119
1120         for (i = 0; i < obj->nr_maps; i++) {
1121                 struct bpf_map *map = &obj->maps[i];
1122                 struct bpf_map_def *def = &map->def;
1123                 char *cp, errmsg[STRERR_BUFSIZE];
1124                 int *pfd = &map->fd;
1125
1126                 if (map->fd >= 0) {
1127                         pr_debug("skip map create (preset) %s: fd=%d\n",
1128                                  map->name, map->fd);
1129                         continue;
1130                 }
1131
1132                 create_attr.name = map->name;
1133                 create_attr.map_ifindex = map->map_ifindex;
1134                 create_attr.map_type = def->type;
1135                 create_attr.map_flags = def->map_flags;
1136                 create_attr.key_size = def->key_size;
1137                 create_attr.value_size = def->value_size;
1138                 create_attr.max_entries = def->max_entries;
1139                 create_attr.btf_fd = 0;
1140                 create_attr.btf_key_type_id = 0;
1141                 create_attr.btf_value_type_id = 0;
1142
1143                 if (obj->btf && !bpf_map_find_btf_info(map, obj->btf)) {
1144                         create_attr.btf_fd = btf__fd(obj->btf);
1145                         create_attr.btf_key_type_id = map->btf_key_type_id;
1146                         create_attr.btf_value_type_id = map->btf_value_type_id;
1147                 }
1148
1149                 *pfd = bpf_create_map_xattr(&create_attr);
1150                 if (*pfd < 0 && create_attr.btf_key_type_id) {
1151                         cp = str_error(errno, errmsg, sizeof(errmsg));
1152                         pr_warning("Error in bpf_create_map_xattr(%s):%s(%d). Retrying without BTF.\n",
1153                                    map->name, cp, errno);
1154                         create_attr.btf_fd = 0;
1155                         create_attr.btf_key_type_id = 0;
1156                         create_attr.btf_value_type_id = 0;
1157                         map->btf_key_type_id = 0;
1158                         map->btf_value_type_id = 0;
1159                         *pfd = bpf_create_map_xattr(&create_attr);
1160                 }
1161
1162                 if (*pfd < 0) {
1163                         size_t j;
1164
1165                         err = *pfd;
1166                         cp = str_error(errno, errmsg, sizeof(errmsg));
1167                         pr_warning("failed to create map (name: '%s'): %s\n",
1168                                    map->name, cp);
1169                         for (j = 0; j < i; j++)
1170                                 zclose(obj->maps[j].fd);
1171                         return err;
1172                 }
1173                 pr_debug("create map %s: fd=%d\n", map->name, *pfd);
1174         }
1175
1176         return 0;
1177 }
1178
1179 static int
1180 bpf_program__reloc_text(struct bpf_program *prog, struct bpf_object *obj,
1181                         struct reloc_desc *relo)
1182 {
1183         struct bpf_insn *insn, *new_insn;
1184         struct bpf_program *text;
1185         size_t new_cnt;
1186
1187         if (relo->type != RELO_CALL)
1188                 return -LIBBPF_ERRNO__RELOC;
1189
1190         if (prog->idx == obj->efile.text_shndx) {
1191                 pr_warning("relo in .text insn %d into off %d\n",
1192                            relo->insn_idx, relo->text_off);
1193                 return -LIBBPF_ERRNO__RELOC;
1194         }
1195
1196         if (prog->main_prog_cnt == 0) {
1197                 text = bpf_object__find_prog_by_idx(obj, obj->efile.text_shndx);
1198                 if (!text) {
1199                         pr_warning("no .text section found yet relo into text exist\n");
1200                         return -LIBBPF_ERRNO__RELOC;
1201                 }
1202                 new_cnt = prog->insns_cnt + text->insns_cnt;
1203                 new_insn = reallocarray(prog->insns, new_cnt, sizeof(*insn));
1204                 if (!new_insn) {
1205                         pr_warning("oom in prog realloc\n");
1206                         return -ENOMEM;
1207                 }
1208                 memcpy(new_insn + prog->insns_cnt, text->insns,
1209                        text->insns_cnt * sizeof(*insn));
1210                 prog->insns = new_insn;
1211                 prog->main_prog_cnt = prog->insns_cnt;
1212                 prog->insns_cnt = new_cnt;
1213                 pr_debug("added %zd insn from %s to prog %s\n",
1214                          text->insns_cnt, text->section_name,
1215                          prog->section_name);
1216         }
1217         insn = &prog->insns[relo->insn_idx];
1218         insn->imm += prog->main_prog_cnt - relo->insn_idx;
1219         return 0;
1220 }
1221
1222 static int
1223 bpf_program__relocate(struct bpf_program *prog, struct bpf_object *obj)
1224 {
1225         int i, err;
1226
1227         if (!prog || !prog->reloc_desc)
1228                 return 0;
1229
1230         for (i = 0; i < prog->nr_reloc; i++) {
1231                 if (prog->reloc_desc[i].type == RELO_LD64) {
1232                         struct bpf_insn *insns = prog->insns;
1233                         int insn_idx, map_idx;
1234
1235                         insn_idx = prog->reloc_desc[i].insn_idx;
1236                         map_idx = prog->reloc_desc[i].map_idx;
1237
1238                         if (insn_idx >= (int)prog->insns_cnt) {
1239                                 pr_warning("relocation out of range: '%s'\n",
1240                                            prog->section_name);
1241                                 return -LIBBPF_ERRNO__RELOC;
1242                         }
1243                         insns[insn_idx].src_reg = BPF_PSEUDO_MAP_FD;
1244                         insns[insn_idx].imm = obj->maps[map_idx].fd;
1245                 } else {
1246                         err = bpf_program__reloc_text(prog, obj,
1247                                                       &prog->reloc_desc[i]);
1248                         if (err)
1249                                 return err;
1250                 }
1251         }
1252
1253         zfree(&prog->reloc_desc);
1254         prog->nr_reloc = 0;
1255         return 0;
1256 }
1257
1258
1259 static int
1260 bpf_object__relocate(struct bpf_object *obj)
1261 {
1262         struct bpf_program *prog;
1263         size_t i;
1264         int err;
1265
1266         for (i = 0; i < obj->nr_programs; i++) {
1267                 prog = &obj->programs[i];
1268
1269                 err = bpf_program__relocate(prog, obj);
1270                 if (err) {
1271                         pr_warning("failed to relocate '%s'\n",
1272                                    prog->section_name);
1273                         return err;
1274                 }
1275         }
1276         return 0;
1277 }
1278
1279 static int bpf_object__collect_reloc(struct bpf_object *obj)
1280 {
1281         int i, err;
1282
1283         if (!obj_elf_valid(obj)) {
1284                 pr_warning("Internal error: elf object is closed\n");
1285                 return -LIBBPF_ERRNO__INTERNAL;
1286         }
1287
1288         for (i = 0; i < obj->efile.nr_reloc; i++) {
1289                 GElf_Shdr *shdr = &obj->efile.reloc[i].shdr;
1290                 Elf_Data *data = obj->efile.reloc[i].data;
1291                 int idx = shdr->sh_info;
1292                 struct bpf_program *prog;
1293
1294                 if (shdr->sh_type != SHT_REL) {
1295                         pr_warning("internal error at %d\n", __LINE__);
1296                         return -LIBBPF_ERRNO__INTERNAL;
1297                 }
1298
1299                 prog = bpf_object__find_prog_by_idx(obj, idx);
1300                 if (!prog) {
1301                         pr_warning("relocation failed: no section(%d)\n", idx);
1302                         return -LIBBPF_ERRNO__RELOC;
1303                 }
1304
1305                 err = bpf_program__collect_reloc(prog,
1306                                                  shdr, data,
1307                                                  obj);
1308                 if (err)
1309                         return err;
1310         }
1311         return 0;
1312 }
1313
1314 static int
1315 load_program(enum bpf_prog_type type, enum bpf_attach_type expected_attach_type,
1316              const char *name, struct bpf_insn *insns, int insns_cnt,
1317              char *license, u32 kern_version, int *pfd, int prog_ifindex)
1318 {
1319         struct bpf_load_program_attr load_attr;
1320         char *cp, errmsg[STRERR_BUFSIZE];
1321         char *log_buf;
1322         int ret;
1323
1324         memset(&load_attr, 0, sizeof(struct bpf_load_program_attr));
1325         load_attr.prog_type = type;
1326         load_attr.expected_attach_type = expected_attach_type;
1327         load_attr.name = name;
1328         load_attr.insns = insns;
1329         load_attr.insns_cnt = insns_cnt;
1330         load_attr.license = license;
1331         load_attr.kern_version = kern_version;
1332         load_attr.prog_ifindex = prog_ifindex;
1333
1334         if (!load_attr.insns || !load_attr.insns_cnt)
1335                 return -EINVAL;
1336
1337         log_buf = malloc(BPF_LOG_BUF_SIZE);
1338         if (!log_buf)
1339                 pr_warning("Alloc log buffer for bpf loader error, continue without log\n");
1340
1341         ret = bpf_load_program_xattr(&load_attr, log_buf, BPF_LOG_BUF_SIZE);
1342
1343         if (ret >= 0) {
1344                 *pfd = ret;
1345                 ret = 0;
1346                 goto out;
1347         }
1348
1349         ret = -LIBBPF_ERRNO__LOAD;
1350         cp = str_error(errno, errmsg, sizeof(errmsg));
1351         pr_warning("load bpf program failed: %s\n", cp);
1352
1353         if (log_buf && log_buf[0] != '\0') {
1354                 ret = -LIBBPF_ERRNO__VERIFY;
1355                 pr_warning("-- BEGIN DUMP LOG ---\n");
1356                 pr_warning("\n%s\n", log_buf);
1357                 pr_warning("-- END LOG --\n");
1358         } else if (load_attr.insns_cnt >= BPF_MAXINSNS) {
1359                 pr_warning("Program too large (%zu insns), at most %d insns\n",
1360                            load_attr.insns_cnt, BPF_MAXINSNS);
1361                 ret = -LIBBPF_ERRNO__PROG2BIG;
1362         } else {
1363                 /* Wrong program type? */
1364                 if (load_attr.prog_type != BPF_PROG_TYPE_KPROBE) {
1365                         int fd;
1366
1367                         load_attr.prog_type = BPF_PROG_TYPE_KPROBE;
1368                         load_attr.expected_attach_type = 0;
1369                         fd = bpf_load_program_xattr(&load_attr, NULL, 0);
1370                         if (fd >= 0) {
1371                                 close(fd);
1372                                 ret = -LIBBPF_ERRNO__PROGTYPE;
1373                                 goto out;
1374                         }
1375                 }
1376
1377                 if (log_buf)
1378                         ret = -LIBBPF_ERRNO__KVER;
1379         }
1380
1381 out:
1382         free(log_buf);
1383         return ret;
1384 }
1385
1386 static int
1387 bpf_program__load(struct bpf_program *prog,
1388                   char *license, u32 kern_version)
1389 {
1390         int err = 0, fd, i;
1391
1392         if (prog->instances.nr < 0 || !prog->instances.fds) {
1393                 if (prog->preprocessor) {
1394                         pr_warning("Internal error: can't load program '%s'\n",
1395                                    prog->section_name);
1396                         return -LIBBPF_ERRNO__INTERNAL;
1397                 }
1398
1399                 prog->instances.fds = malloc(sizeof(int));
1400                 if (!prog->instances.fds) {
1401                         pr_warning("Not enough memory for BPF fds\n");
1402                         return -ENOMEM;
1403                 }
1404                 prog->instances.nr = 1;
1405                 prog->instances.fds[0] = -1;
1406         }
1407
1408         if (!prog->preprocessor) {
1409                 if (prog->instances.nr != 1) {
1410                         pr_warning("Program '%s' is inconsistent: nr(%d) != 1\n",
1411                                    prog->section_name, prog->instances.nr);
1412                 }
1413                 err = load_program(prog->type, prog->expected_attach_type,
1414                                    prog->name, prog->insns, prog->insns_cnt,
1415                                    license, kern_version, &fd,
1416                                    prog->prog_ifindex);
1417                 if (!err)
1418                         prog->instances.fds[0] = fd;
1419                 goto out;
1420         }
1421
1422         for (i = 0; i < prog->instances.nr; i++) {
1423                 struct bpf_prog_prep_result result;
1424                 bpf_program_prep_t preprocessor = prog->preprocessor;
1425
1426                 bzero(&result, sizeof(result));
1427                 err = preprocessor(prog, i, prog->insns,
1428                                    prog->insns_cnt, &result);
1429                 if (err) {
1430                         pr_warning("Preprocessing the %dth instance of program '%s' failed\n",
1431                                    i, prog->section_name);
1432                         goto out;
1433                 }
1434
1435                 if (!result.new_insn_ptr || !result.new_insn_cnt) {
1436                         pr_debug("Skip loading the %dth instance of program '%s'\n",
1437                                  i, prog->section_name);
1438                         prog->instances.fds[i] = -1;
1439                         if (result.pfd)
1440                                 *result.pfd = -1;
1441                         continue;
1442                 }
1443
1444                 err = load_program(prog->type, prog->expected_attach_type,
1445                                    prog->name, result.new_insn_ptr,
1446                                    result.new_insn_cnt,
1447                                    license, kern_version, &fd,
1448                                    prog->prog_ifindex);
1449
1450                 if (err) {
1451                         pr_warning("Loading the %dth instance of program '%s' failed\n",
1452                                         i, prog->section_name);
1453                         goto out;
1454                 }
1455
1456                 if (result.pfd)
1457                         *result.pfd = fd;
1458                 prog->instances.fds[i] = fd;
1459         }
1460 out:
1461         if (err)
1462                 pr_warning("failed to load program '%s'\n",
1463                            prog->section_name);
1464         zfree(&prog->insns);
1465         prog->insns_cnt = 0;
1466         return err;
1467 }
1468
1469 static bool bpf_program__is_function_storage(struct bpf_program *prog,
1470                                              struct bpf_object *obj)
1471 {
1472         return prog->idx == obj->efile.text_shndx && obj->has_pseudo_calls;
1473 }
1474
1475 static int
1476 bpf_object__load_progs(struct bpf_object *obj)
1477 {
1478         size_t i;
1479         int err;
1480
1481         for (i = 0; i < obj->nr_programs; i++) {
1482                 if (bpf_program__is_function_storage(&obj->programs[i], obj))
1483                         continue;
1484                 err = bpf_program__load(&obj->programs[i],
1485                                         obj->license,
1486                                         obj->kern_version);
1487                 if (err)
1488                         return err;
1489         }
1490         return 0;
1491 }
1492
1493 static bool bpf_prog_type__needs_kver(enum bpf_prog_type type)
1494 {
1495         switch (type) {
1496         case BPF_PROG_TYPE_SOCKET_FILTER:
1497         case BPF_PROG_TYPE_SCHED_CLS:
1498         case BPF_PROG_TYPE_SCHED_ACT:
1499         case BPF_PROG_TYPE_XDP:
1500         case BPF_PROG_TYPE_CGROUP_SKB:
1501         case BPF_PROG_TYPE_CGROUP_SOCK:
1502         case BPF_PROG_TYPE_LWT_IN:
1503         case BPF_PROG_TYPE_LWT_OUT:
1504         case BPF_PROG_TYPE_LWT_XMIT:
1505         case BPF_PROG_TYPE_LWT_SEG6LOCAL:
1506         case BPF_PROG_TYPE_SOCK_OPS:
1507         case BPF_PROG_TYPE_SK_SKB:
1508         case BPF_PROG_TYPE_CGROUP_DEVICE:
1509         case BPF_PROG_TYPE_SK_MSG:
1510         case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
1511         case BPF_PROG_TYPE_LIRC_MODE2:
1512         case BPF_PROG_TYPE_SK_REUSEPORT:
1513                 return false;
1514         case BPF_PROG_TYPE_UNSPEC:
1515         case BPF_PROG_TYPE_KPROBE:
1516         case BPF_PROG_TYPE_TRACEPOINT:
1517         case BPF_PROG_TYPE_PERF_EVENT:
1518         case BPF_PROG_TYPE_RAW_TRACEPOINT:
1519         default:
1520                 return true;
1521         }
1522 }
1523
1524 static int bpf_object__validate(struct bpf_object *obj, bool needs_kver)
1525 {
1526         if (needs_kver && obj->kern_version == 0) {
1527                 pr_warning("%s doesn't provide kernel version\n",
1528                            obj->path);
1529                 return -LIBBPF_ERRNO__KVERSION;
1530         }
1531         return 0;
1532 }
1533
1534 static struct bpf_object *
1535 __bpf_object__open(const char *path, void *obj_buf, size_t obj_buf_sz,
1536                    bool needs_kver)
1537 {
1538         struct bpf_object *obj;
1539         int err;
1540
1541         if (elf_version(EV_CURRENT) == EV_NONE) {
1542                 pr_warning("failed to init libelf for %s\n", path);
1543                 return ERR_PTR(-LIBBPF_ERRNO__LIBELF);
1544         }
1545
1546         obj = bpf_object__new(path, obj_buf, obj_buf_sz);
1547         if (IS_ERR(obj))
1548                 return obj;
1549
1550         CHECK_ERR(bpf_object__elf_init(obj), err, out);
1551         CHECK_ERR(bpf_object__check_endianness(obj), err, out);
1552         CHECK_ERR(bpf_object__elf_collect(obj), err, out);
1553         CHECK_ERR(bpf_object__collect_reloc(obj), err, out);
1554         CHECK_ERR(bpf_object__validate(obj, needs_kver), err, out);
1555
1556         bpf_object__elf_finish(obj);
1557         return obj;
1558 out:
1559         bpf_object__close(obj);
1560         return ERR_PTR(err);
1561 }
1562
1563 struct bpf_object *bpf_object__open_xattr(struct bpf_object_open_attr *attr)
1564 {
1565         /* param validation */
1566         if (!attr->file)
1567                 return NULL;
1568
1569         pr_debug("loading %s\n", attr->file);
1570
1571         return __bpf_object__open(attr->file, NULL, 0,
1572                                   bpf_prog_type__needs_kver(attr->prog_type));
1573 }
1574
1575 struct bpf_object *bpf_object__open(const char *path)
1576 {
1577         struct bpf_object_open_attr attr = {
1578                 .file           = path,
1579                 .prog_type      = BPF_PROG_TYPE_UNSPEC,
1580         };
1581
1582         return bpf_object__open_xattr(&attr);
1583 }
1584
1585 struct bpf_object *bpf_object__open_buffer(void *obj_buf,
1586                                            size_t obj_buf_sz,
1587                                            const char *name)
1588 {
1589         char tmp_name[64];
1590
1591         /* param validation */
1592         if (!obj_buf || obj_buf_sz <= 0)
1593                 return NULL;
1594
1595         if (!name) {
1596                 snprintf(tmp_name, sizeof(tmp_name), "%lx-%lx",
1597                          (unsigned long)obj_buf,
1598                          (unsigned long)obj_buf_sz);
1599                 tmp_name[sizeof(tmp_name) - 1] = '\0';
1600                 name = tmp_name;
1601         }
1602         pr_debug("loading object '%s' from buffer\n",
1603                  name);
1604
1605         return __bpf_object__open(name, obj_buf, obj_buf_sz, true);
1606 }
1607
1608 int bpf_object__unload(struct bpf_object *obj)
1609 {
1610         size_t i;
1611
1612         if (!obj)
1613                 return -EINVAL;
1614
1615         for (i = 0; i < obj->nr_maps; i++)
1616                 zclose(obj->maps[i].fd);
1617
1618         for (i = 0; i < obj->nr_programs; i++)
1619                 bpf_program__unload(&obj->programs[i]);
1620
1621         return 0;
1622 }
1623
1624 int bpf_object__load(struct bpf_object *obj)
1625 {
1626         int err;
1627
1628         if (!obj)
1629                 return -EINVAL;
1630
1631         if (obj->loaded) {
1632                 pr_warning("object should not be loaded twice\n");
1633                 return -EINVAL;
1634         }
1635
1636         obj->loaded = true;
1637
1638         CHECK_ERR(bpf_object__create_maps(obj), err, out);
1639         CHECK_ERR(bpf_object__relocate(obj), err, out);
1640         CHECK_ERR(bpf_object__load_progs(obj), err, out);
1641
1642         return 0;
1643 out:
1644         bpf_object__unload(obj);
1645         pr_warning("failed to load object '%s'\n", obj->path);
1646         return err;
1647 }
1648
1649 static int check_path(const char *path)
1650 {
1651         char *cp, errmsg[STRERR_BUFSIZE];
1652         struct statfs st_fs;
1653         char *dname, *dir;
1654         int err = 0;
1655
1656         if (path == NULL)
1657                 return -EINVAL;
1658
1659         dname = strdup(path);
1660         if (dname == NULL)
1661                 return -ENOMEM;
1662
1663         dir = dirname(dname);
1664         if (statfs(dir, &st_fs)) {
1665                 cp = str_error(errno, errmsg, sizeof(errmsg));
1666                 pr_warning("failed to statfs %s: %s\n", dir, cp);
1667                 err = -errno;
1668         }
1669         free(dname);
1670
1671         if (!err && st_fs.f_type != BPF_FS_MAGIC) {
1672                 pr_warning("specified path %s is not on BPF FS\n", path);
1673                 err = -EINVAL;
1674         }
1675
1676         return err;
1677 }
1678
1679 int bpf_program__pin_instance(struct bpf_program *prog, const char *path,
1680                               int instance)
1681 {
1682         char *cp, errmsg[STRERR_BUFSIZE];
1683         int err;
1684
1685         err = check_path(path);
1686         if (err)
1687                 return err;
1688
1689         if (prog == NULL) {
1690                 pr_warning("invalid program pointer\n");
1691                 return -EINVAL;
1692         }
1693
1694         if (instance < 0 || instance >= prog->instances.nr) {
1695                 pr_warning("invalid prog instance %d of prog %s (max %d)\n",
1696                            instance, prog->section_name, prog->instances.nr);
1697                 return -EINVAL;
1698         }
1699
1700         if (bpf_obj_pin(prog->instances.fds[instance], path)) {
1701                 cp = str_error(errno, errmsg, sizeof(errmsg));
1702                 pr_warning("failed to pin program: %s\n", cp);
1703                 return -errno;
1704         }
1705         pr_debug("pinned program '%s'\n", path);
1706
1707         return 0;
1708 }
1709
1710 static int make_dir(const char *path)
1711 {
1712         char *cp, errmsg[STRERR_BUFSIZE];
1713         int err = 0;
1714
1715         if (mkdir(path, 0700) && errno != EEXIST)
1716                 err = -errno;
1717
1718         if (err) {
1719                 cp = str_error(-err, errmsg, sizeof(errmsg));
1720                 pr_warning("failed to mkdir %s: %s\n", path, cp);
1721         }
1722         return err;
1723 }
1724
1725 int bpf_program__pin(struct bpf_program *prog, const char *path)
1726 {
1727         int i, err;
1728
1729         err = check_path(path);
1730         if (err)
1731                 return err;
1732
1733         if (prog == NULL) {
1734                 pr_warning("invalid program pointer\n");
1735                 return -EINVAL;
1736         }
1737
1738         if (prog->instances.nr <= 0) {
1739                 pr_warning("no instances of prog %s to pin\n",
1740                            prog->section_name);
1741                 return -EINVAL;
1742         }
1743
1744         err = make_dir(path);
1745         if (err)
1746                 return err;
1747
1748         for (i = 0; i < prog->instances.nr; i++) {
1749                 char buf[PATH_MAX];
1750                 int len;
1751
1752                 len = snprintf(buf, PATH_MAX, "%s/%d", path, i);
1753                 if (len < 0)
1754                         return -EINVAL;
1755                 else if (len >= PATH_MAX)
1756                         return -ENAMETOOLONG;
1757
1758                 err = bpf_program__pin_instance(prog, buf, i);
1759                 if (err)
1760                         return err;
1761         }
1762
1763         return 0;
1764 }
1765
1766 int bpf_map__pin(struct bpf_map *map, const char *path)
1767 {
1768         char *cp, errmsg[STRERR_BUFSIZE];
1769         int err;
1770
1771         err = check_path(path);
1772         if (err)
1773                 return err;
1774
1775         if (map == NULL) {
1776                 pr_warning("invalid map pointer\n");
1777                 return -EINVAL;
1778         }
1779
1780         if (bpf_obj_pin(map->fd, path)) {
1781                 cp = str_error(errno, errmsg, sizeof(errmsg));
1782                 pr_warning("failed to pin map: %s\n", cp);
1783                 return -errno;
1784         }
1785
1786         pr_debug("pinned map '%s'\n", path);
1787         return 0;
1788 }
1789
1790 int bpf_object__pin(struct bpf_object *obj, const char *path)
1791 {
1792         struct bpf_program *prog;
1793         struct bpf_map *map;
1794         int err;
1795
1796         if (!obj)
1797                 return -ENOENT;
1798
1799         if (!obj->loaded) {
1800                 pr_warning("object not yet loaded; load it first\n");
1801                 return -ENOENT;
1802         }
1803
1804         err = make_dir(path);
1805         if (err)
1806                 return err;
1807
1808         bpf_map__for_each(map, obj) {
1809                 char buf[PATH_MAX];
1810                 int len;
1811
1812                 len = snprintf(buf, PATH_MAX, "%s/%s", path,
1813                                bpf_map__name(map));
1814                 if (len < 0)
1815                         return -EINVAL;
1816                 else if (len >= PATH_MAX)
1817                         return -ENAMETOOLONG;
1818
1819                 err = bpf_map__pin(map, buf);
1820                 if (err)
1821                         return err;
1822         }
1823
1824         bpf_object__for_each_program(prog, obj) {
1825                 char buf[PATH_MAX];
1826                 int len;
1827
1828                 len = snprintf(buf, PATH_MAX, "%s/%s", path,
1829                                prog->section_name);
1830                 if (len < 0)
1831                         return -EINVAL;
1832                 else if (len >= PATH_MAX)
1833                         return -ENAMETOOLONG;
1834
1835                 err = bpf_program__pin(prog, buf);
1836                 if (err)
1837                         return err;
1838         }
1839
1840         return 0;
1841 }
1842
1843 void bpf_object__close(struct bpf_object *obj)
1844 {
1845         size_t i;
1846
1847         if (!obj)
1848                 return;
1849
1850         if (obj->clear_priv)
1851                 obj->clear_priv(obj, obj->priv);
1852
1853         bpf_object__elf_finish(obj);
1854         bpf_object__unload(obj);
1855         btf__free(obj->btf);
1856
1857         for (i = 0; i < obj->nr_maps; i++) {
1858                 zfree(&obj->maps[i].name);
1859                 if (obj->maps[i].clear_priv)
1860                         obj->maps[i].clear_priv(&obj->maps[i],
1861                                                 obj->maps[i].priv);
1862                 obj->maps[i].priv = NULL;
1863                 obj->maps[i].clear_priv = NULL;
1864         }
1865         zfree(&obj->maps);
1866         obj->nr_maps = 0;
1867
1868         if (obj->programs && obj->nr_programs) {
1869                 for (i = 0; i < obj->nr_programs; i++)
1870                         bpf_program__exit(&obj->programs[i]);
1871         }
1872         zfree(&obj->programs);
1873
1874         list_del(&obj->list);
1875         free(obj);
1876 }
1877
1878 struct bpf_object *
1879 bpf_object__next(struct bpf_object *prev)
1880 {
1881         struct bpf_object *next;
1882
1883         if (!prev)
1884                 next = list_first_entry(&bpf_objects_list,
1885                                         struct bpf_object,
1886                                         list);
1887         else
1888                 next = list_next_entry(prev, list);
1889
1890         /* Empty list is noticed here so don't need checking on entry. */
1891         if (&next->list == &bpf_objects_list)
1892                 return NULL;
1893
1894         return next;
1895 }
1896
1897 const char *bpf_object__name(struct bpf_object *obj)
1898 {
1899         return obj ? obj->path : ERR_PTR(-EINVAL);
1900 }
1901
1902 unsigned int bpf_object__kversion(struct bpf_object *obj)
1903 {
1904         return obj ? obj->kern_version : 0;
1905 }
1906
1907 int bpf_object__btf_fd(const struct bpf_object *obj)
1908 {
1909         return obj->btf ? btf__fd(obj->btf) : -1;
1910 }
1911
1912 int bpf_object__set_priv(struct bpf_object *obj, void *priv,
1913                          bpf_object_clear_priv_t clear_priv)
1914 {
1915         if (obj->priv && obj->clear_priv)
1916                 obj->clear_priv(obj, obj->priv);
1917
1918         obj->priv = priv;
1919         obj->clear_priv = clear_priv;
1920         return 0;
1921 }
1922
1923 void *bpf_object__priv(struct bpf_object *obj)
1924 {
1925         return obj ? obj->priv : ERR_PTR(-EINVAL);
1926 }
1927
1928 static struct bpf_program *
1929 __bpf_program__next(struct bpf_program *prev, struct bpf_object *obj)
1930 {
1931         size_t idx;
1932
1933         if (!obj->programs)
1934                 return NULL;
1935         /* First handler */
1936         if (prev == NULL)
1937                 return &obj->programs[0];
1938
1939         if (prev->obj != obj) {
1940                 pr_warning("error: program handler doesn't match object\n");
1941                 return NULL;
1942         }
1943
1944         idx = (prev - obj->programs) + 1;
1945         if (idx >= obj->nr_programs)
1946                 return NULL;
1947         return &obj->programs[idx];
1948 }
1949
1950 struct bpf_program *
1951 bpf_program__next(struct bpf_program *prev, struct bpf_object *obj)
1952 {
1953         struct bpf_program *prog = prev;
1954
1955         do {
1956                 prog = __bpf_program__next(prog, obj);
1957         } while (prog && bpf_program__is_function_storage(prog, obj));
1958
1959         return prog;
1960 }
1961
1962 int bpf_program__set_priv(struct bpf_program *prog, void *priv,
1963                           bpf_program_clear_priv_t clear_priv)
1964 {
1965         if (prog->priv && prog->clear_priv)
1966                 prog->clear_priv(prog, prog->priv);
1967
1968         prog->priv = priv;
1969         prog->clear_priv = clear_priv;
1970         return 0;
1971 }
1972
1973 void *bpf_program__priv(struct bpf_program *prog)
1974 {
1975         return prog ? prog->priv : ERR_PTR(-EINVAL);
1976 }
1977
1978 void bpf_program__set_ifindex(struct bpf_program *prog, __u32 ifindex)
1979 {
1980         prog->prog_ifindex = ifindex;
1981 }
1982
1983 const char *bpf_program__title(struct bpf_program *prog, bool needs_copy)
1984 {
1985         const char *title;
1986
1987         title = prog->section_name;
1988         if (needs_copy) {
1989                 title = strdup(title);
1990                 if (!title) {
1991                         pr_warning("failed to strdup program title\n");
1992                         return ERR_PTR(-ENOMEM);
1993                 }
1994         }
1995
1996         return title;
1997 }
1998
1999 int bpf_program__fd(struct bpf_program *prog)
2000 {
2001         return bpf_program__nth_fd(prog, 0);
2002 }
2003
2004 int bpf_program__set_prep(struct bpf_program *prog, int nr_instances,
2005                           bpf_program_prep_t prep)
2006 {
2007         int *instances_fds;
2008
2009         if (nr_instances <= 0 || !prep)
2010                 return -EINVAL;
2011
2012         if (prog->instances.nr > 0 || prog->instances.fds) {
2013                 pr_warning("Can't set pre-processor after loading\n");
2014                 return -EINVAL;
2015         }
2016
2017         instances_fds = malloc(sizeof(int) * nr_instances);
2018         if (!instances_fds) {
2019                 pr_warning("alloc memory failed for fds\n");
2020                 return -ENOMEM;
2021         }
2022
2023         /* fill all fd with -1 */
2024         memset(instances_fds, -1, sizeof(int) * nr_instances);
2025
2026         prog->instances.nr = nr_instances;
2027         prog->instances.fds = instances_fds;
2028         prog->preprocessor = prep;
2029         return 0;
2030 }
2031
2032 int bpf_program__nth_fd(struct bpf_program *prog, int n)
2033 {
2034         int fd;
2035
2036         if (!prog)
2037                 return -EINVAL;
2038
2039         if (n >= prog->instances.nr || n < 0) {
2040                 pr_warning("Can't get the %dth fd from program %s: only %d instances\n",
2041                            n, prog->section_name, prog->instances.nr);
2042                 return -EINVAL;
2043         }
2044
2045         fd = prog->instances.fds[n];
2046         if (fd < 0) {
2047                 pr_warning("%dth instance of program '%s' is invalid\n",
2048                            n, prog->section_name);
2049                 return -ENOENT;
2050         }
2051
2052         return fd;
2053 }
2054
2055 void bpf_program__set_type(struct bpf_program *prog, enum bpf_prog_type type)
2056 {
2057         prog->type = type;
2058 }
2059
2060 static bool bpf_program__is_type(struct bpf_program *prog,
2061                                  enum bpf_prog_type type)
2062 {
2063         return prog ? (prog->type == type) : false;
2064 }
2065
2066 #define BPF_PROG_TYPE_FNS(NAME, TYPE)                   \
2067 int bpf_program__set_##NAME(struct bpf_program *prog)   \
2068 {                                                       \
2069         if (!prog)                                      \
2070                 return -EINVAL;                         \
2071         bpf_program__set_type(prog, TYPE);              \
2072         return 0;                                       \
2073 }                                                       \
2074                                                         \
2075 bool bpf_program__is_##NAME(struct bpf_program *prog)   \
2076 {                                                       \
2077         return bpf_program__is_type(prog, TYPE);        \
2078 }                                                       \
2079
2080 BPF_PROG_TYPE_FNS(socket_filter, BPF_PROG_TYPE_SOCKET_FILTER);
2081 BPF_PROG_TYPE_FNS(kprobe, BPF_PROG_TYPE_KPROBE);
2082 BPF_PROG_TYPE_FNS(sched_cls, BPF_PROG_TYPE_SCHED_CLS);
2083 BPF_PROG_TYPE_FNS(sched_act, BPF_PROG_TYPE_SCHED_ACT);
2084 BPF_PROG_TYPE_FNS(tracepoint, BPF_PROG_TYPE_TRACEPOINT);
2085 BPF_PROG_TYPE_FNS(raw_tracepoint, BPF_PROG_TYPE_RAW_TRACEPOINT);
2086 BPF_PROG_TYPE_FNS(xdp, BPF_PROG_TYPE_XDP);
2087 BPF_PROG_TYPE_FNS(perf_event, BPF_PROG_TYPE_PERF_EVENT);
2088
2089 void bpf_program__set_expected_attach_type(struct bpf_program *prog,
2090                                            enum bpf_attach_type type)
2091 {
2092         prog->expected_attach_type = type;
2093 }
2094
2095 #define BPF_PROG_SEC_FULL(string, ptype, atype) \
2096         { string, sizeof(string) - 1, ptype, atype }
2097
2098 #define BPF_PROG_SEC(string, ptype) BPF_PROG_SEC_FULL(string, ptype, 0)
2099
2100 #define BPF_S_PROG_SEC(string, ptype) \
2101         BPF_PROG_SEC_FULL(string, BPF_PROG_TYPE_CGROUP_SOCK, ptype)
2102
2103 #define BPF_SA_PROG_SEC(string, ptype) \
2104         BPF_PROG_SEC_FULL(string, BPF_PROG_TYPE_CGROUP_SOCK_ADDR, ptype)
2105
2106 static const struct {
2107         const char *sec;
2108         size_t len;
2109         enum bpf_prog_type prog_type;
2110         enum bpf_attach_type expected_attach_type;
2111 } section_names[] = {
2112         BPF_PROG_SEC("socket",          BPF_PROG_TYPE_SOCKET_FILTER),
2113         BPF_PROG_SEC("kprobe/",         BPF_PROG_TYPE_KPROBE),
2114         BPF_PROG_SEC("kretprobe/",      BPF_PROG_TYPE_KPROBE),
2115         BPF_PROG_SEC("classifier",      BPF_PROG_TYPE_SCHED_CLS),
2116         BPF_PROG_SEC("action",          BPF_PROG_TYPE_SCHED_ACT),
2117         BPF_PROG_SEC("tracepoint/",     BPF_PROG_TYPE_TRACEPOINT),
2118         BPF_PROG_SEC("raw_tracepoint/", BPF_PROG_TYPE_RAW_TRACEPOINT),
2119         BPF_PROG_SEC("xdp",             BPF_PROG_TYPE_XDP),
2120         BPF_PROG_SEC("perf_event",      BPF_PROG_TYPE_PERF_EVENT),
2121         BPF_PROG_SEC("cgroup/skb",      BPF_PROG_TYPE_CGROUP_SKB),
2122         BPF_PROG_SEC("cgroup/sock",     BPF_PROG_TYPE_CGROUP_SOCK),
2123         BPF_PROG_SEC("cgroup/dev",      BPF_PROG_TYPE_CGROUP_DEVICE),
2124         BPF_PROG_SEC("lwt_in",          BPF_PROG_TYPE_LWT_IN),
2125         BPF_PROG_SEC("lwt_out",         BPF_PROG_TYPE_LWT_OUT),
2126         BPF_PROG_SEC("lwt_xmit",        BPF_PROG_TYPE_LWT_XMIT),
2127         BPF_PROG_SEC("lwt_seg6local",   BPF_PROG_TYPE_LWT_SEG6LOCAL),
2128         BPF_PROG_SEC("sockops",         BPF_PROG_TYPE_SOCK_OPS),
2129         BPF_PROG_SEC("sk_skb",          BPF_PROG_TYPE_SK_SKB),
2130         BPF_PROG_SEC("sk_msg",          BPF_PROG_TYPE_SK_MSG),
2131         BPF_PROG_SEC("lirc_mode2",      BPF_PROG_TYPE_LIRC_MODE2),
2132         BPF_SA_PROG_SEC("cgroup/bind4", BPF_CGROUP_INET4_BIND),
2133         BPF_SA_PROG_SEC("cgroup/bind6", BPF_CGROUP_INET6_BIND),
2134         BPF_SA_PROG_SEC("cgroup/connect4", BPF_CGROUP_INET4_CONNECT),
2135         BPF_SA_PROG_SEC("cgroup/connect6", BPF_CGROUP_INET6_CONNECT),
2136         BPF_SA_PROG_SEC("cgroup/sendmsg4", BPF_CGROUP_UDP4_SENDMSG),
2137         BPF_SA_PROG_SEC("cgroup/sendmsg6", BPF_CGROUP_UDP6_SENDMSG),
2138         BPF_S_PROG_SEC("cgroup/post_bind4", BPF_CGROUP_INET4_POST_BIND),
2139         BPF_S_PROG_SEC("cgroup/post_bind6", BPF_CGROUP_INET6_POST_BIND),
2140 };
2141
2142 #undef BPF_PROG_SEC
2143 #undef BPF_PROG_SEC_FULL
2144 #undef BPF_S_PROG_SEC
2145 #undef BPF_SA_PROG_SEC
2146
2147 int libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,
2148                              enum bpf_attach_type *expected_attach_type)
2149 {
2150         int i;
2151
2152         if (!name)
2153                 return -EINVAL;
2154
2155         for (i = 0; i < ARRAY_SIZE(section_names); i++) {
2156                 if (strncmp(name, section_names[i].sec, section_names[i].len))
2157                         continue;
2158                 *prog_type = section_names[i].prog_type;
2159                 *expected_attach_type = section_names[i].expected_attach_type;
2160                 return 0;
2161         }
2162         return -EINVAL;
2163 }
2164
2165 static int
2166 bpf_program__identify_section(struct bpf_program *prog,
2167                               enum bpf_prog_type *prog_type,
2168                               enum bpf_attach_type *expected_attach_type)
2169 {
2170         return libbpf_prog_type_by_name(prog->section_name, prog_type,
2171                                         expected_attach_type);
2172 }
2173
2174 int bpf_map__fd(struct bpf_map *map)
2175 {
2176         return map ? map->fd : -EINVAL;
2177 }
2178
2179 const struct bpf_map_def *bpf_map__def(struct bpf_map *map)
2180 {
2181         return map ? &map->def : ERR_PTR(-EINVAL);
2182 }
2183
2184 const char *bpf_map__name(struct bpf_map *map)
2185 {
2186         return map ? map->name : NULL;
2187 }
2188
2189 __u32 bpf_map__btf_key_type_id(const struct bpf_map *map)
2190 {
2191         return map ? map->btf_key_type_id : 0;
2192 }
2193
2194 __u32 bpf_map__btf_value_type_id(const struct bpf_map *map)
2195 {
2196         return map ? map->btf_value_type_id : 0;
2197 }
2198
2199 int bpf_map__set_priv(struct bpf_map *map, void *priv,
2200                      bpf_map_clear_priv_t clear_priv)
2201 {
2202         if (!map)
2203                 return -EINVAL;
2204
2205         if (map->priv) {
2206                 if (map->clear_priv)
2207                         map->clear_priv(map, map->priv);
2208         }
2209
2210         map->priv = priv;
2211         map->clear_priv = clear_priv;
2212         return 0;
2213 }
2214
2215 void *bpf_map__priv(struct bpf_map *map)
2216 {
2217         return map ? map->priv : ERR_PTR(-EINVAL);
2218 }
2219
2220 bool bpf_map__is_offload_neutral(struct bpf_map *map)
2221 {
2222         return map->def.type == BPF_MAP_TYPE_PERF_EVENT_ARRAY;
2223 }
2224
2225 void bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex)
2226 {
2227         map->map_ifindex = ifindex;
2228 }
2229
2230 struct bpf_map *
2231 bpf_map__next(struct bpf_map *prev, struct bpf_object *obj)
2232 {
2233         size_t idx;
2234         struct bpf_map *s, *e;
2235
2236         if (!obj || !obj->maps)
2237                 return NULL;
2238
2239         s = obj->maps;
2240         e = obj->maps + obj->nr_maps;
2241
2242         if (prev == NULL)
2243                 return s;
2244
2245         if ((prev < s) || (prev >= e)) {
2246                 pr_warning("error in %s: map handler doesn't belong to object\n",
2247                            __func__);
2248                 return NULL;
2249         }
2250
2251         idx = (prev - obj->maps) + 1;
2252         if (idx >= obj->nr_maps)
2253                 return NULL;
2254         return &obj->maps[idx];
2255 }
2256
2257 struct bpf_map *
2258 bpf_object__find_map_by_name(struct bpf_object *obj, const char *name)
2259 {
2260         struct bpf_map *pos;
2261
2262         bpf_map__for_each(pos, obj) {
2263                 if (pos->name && !strcmp(pos->name, name))
2264                         return pos;
2265         }
2266         return NULL;
2267 }
2268
2269 struct bpf_map *
2270 bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset)
2271 {
2272         int i;
2273
2274         for (i = 0; i < obj->nr_maps; i++) {
2275                 if (obj->maps[i].offset == offset)
2276                         return &obj->maps[i];
2277         }
2278         return ERR_PTR(-ENOENT);
2279 }
2280
2281 long libbpf_get_error(const void *ptr)
2282 {
2283         if (IS_ERR(ptr))
2284                 return PTR_ERR(ptr);
2285         return 0;
2286 }
2287
2288 int bpf_prog_load(const char *file, enum bpf_prog_type type,
2289                   struct bpf_object **pobj, int *prog_fd)
2290 {
2291         struct bpf_prog_load_attr attr;
2292
2293         memset(&attr, 0, sizeof(struct bpf_prog_load_attr));
2294         attr.file = file;
2295         attr.prog_type = type;
2296         attr.expected_attach_type = 0;
2297
2298         return bpf_prog_load_xattr(&attr, pobj, prog_fd);
2299 }
2300
2301 int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,
2302                         struct bpf_object **pobj, int *prog_fd)
2303 {
2304         struct bpf_object_open_attr open_attr = {};
2305         struct bpf_program *prog, *first_prog = NULL;
2306         enum bpf_attach_type expected_attach_type;
2307         enum bpf_prog_type prog_type;
2308         struct bpf_object *obj;
2309         struct bpf_map *map;
2310         int err;
2311
2312         if (!attr)
2313                 return -EINVAL;
2314         if (!attr->file)
2315                 return -EINVAL;
2316
2317         open_attr.file = attr->file;
2318         open_attr.prog_type = attr->prog_type;
2319
2320         obj = bpf_object__open_xattr(&open_attr);
2321         if (IS_ERR_OR_NULL(obj))
2322                 return -ENOENT;
2323
2324         bpf_object__for_each_program(prog, obj) {
2325                 /*
2326                  * If type is not specified, try to guess it based on
2327                  * section name.
2328                  */
2329                 prog_type = attr->prog_type;
2330                 prog->prog_ifindex = attr->ifindex;
2331                 expected_attach_type = attr->expected_attach_type;
2332                 if (prog_type == BPF_PROG_TYPE_UNSPEC) {
2333                         err = bpf_program__identify_section(prog, &prog_type,
2334                                                             &expected_attach_type);
2335                         if (err < 0) {
2336                                 pr_warning("failed to guess program type based on section name %s\n",
2337                                            prog->section_name);
2338                                 bpf_object__close(obj);
2339                                 return -EINVAL;
2340                         }
2341                 }
2342
2343                 bpf_program__set_type(prog, prog_type);
2344                 bpf_program__set_expected_attach_type(prog,
2345                                                       expected_attach_type);
2346
2347                 if (!bpf_program__is_function_storage(prog, obj) && !first_prog)
2348                         first_prog = prog;
2349         }
2350
2351         bpf_map__for_each(map, obj) {
2352                 if (!bpf_map__is_offload_neutral(map))
2353                         map->map_ifindex = attr->ifindex;
2354         }
2355
2356         if (!first_prog) {
2357                 pr_warning("object file doesn't contain bpf program\n");
2358                 bpf_object__close(obj);
2359                 return -ENOENT;
2360         }
2361
2362         err = bpf_object__load(obj);
2363         if (err) {
2364                 bpf_object__close(obj);
2365                 return -EINVAL;
2366         }
2367
2368         *pobj = obj;
2369         *prog_fd = bpf_program__fd(first_prog);
2370         return 0;
2371 }
2372
2373 enum bpf_perf_event_ret
2374 bpf_perf_event_read_simple(void *mem, unsigned long size,
2375                            unsigned long page_size, void **buf, size_t *buf_len,
2376                            bpf_perf_event_print_t fn, void *priv)
2377 {
2378         volatile struct perf_event_mmap_page *header = mem;
2379         __u64 data_tail = header->data_tail;
2380         __u64 data_head = header->data_head;
2381         int ret = LIBBPF_PERF_EVENT_ERROR;
2382         void *base, *begin, *end;
2383
2384         asm volatile("" ::: "memory"); /* in real code it should be smp_rmb() */
2385         if (data_head == data_tail)
2386                 return LIBBPF_PERF_EVENT_CONT;
2387
2388         base = ((char *)header) + page_size;
2389
2390         begin = base + data_tail % size;
2391         end = base + data_head % size;
2392
2393         while (begin != end) {
2394                 struct perf_event_header *ehdr;
2395
2396                 ehdr = begin;
2397                 if (begin + ehdr->size > base + size) {
2398                         long len = base + size - begin;
2399
2400                         if (*buf_len < ehdr->size) {
2401                                 free(*buf);
2402                                 *buf = malloc(ehdr->size);
2403                                 if (!*buf) {
2404                                         ret = LIBBPF_PERF_EVENT_ERROR;
2405                                         break;
2406                                 }
2407                                 *buf_len = ehdr->size;
2408                         }
2409
2410                         memcpy(*buf, begin, len);
2411                         memcpy(*buf + len, base, ehdr->size - len);
2412                         ehdr = (void *)*buf;
2413                         begin = base + ehdr->size - len;
2414                 } else if (begin + ehdr->size == base + size) {
2415                         begin = base;
2416                 } else {
2417                         begin += ehdr->size;
2418                 }
2419
2420                 ret = fn(ehdr, priv);
2421                 if (ret != LIBBPF_PERF_EVENT_CONT)
2422                         break;
2423
2424                 data_tail += ehdr->size;
2425         }
2426
2427         __sync_synchronize(); /* smp_mb() */
2428         header->data_tail = data_tail;
2429
2430         return ret;
2431 }