linux/tools/lib/bpf/libbpf.c
<<
>>
Prefs
   1/*
   2 * Common eBPF ELF object loading operations.
   3 *
   4 * Copyright (C) 2013-2015 Alexei Starovoitov <ast@kernel.org>
   5 * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com>
   6 * Copyright (C) 2015 Huawei Inc.
   7 * Copyright (C) 2017 Nicira, Inc.
   8 *
   9 * This program is free software; you can redistribute it and/or
  10 * modify it under the terms of the GNU Lesser General Public
  11 * License as published by the Free Software Foundation;
  12 * version 2.1 of the License (not later!)
  13 *
  14 * This program is distributed in the hope that it will be useful,
  15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17 * GNU Lesser General Public License for more details.
  18 *
  19 * You should have received a copy of the GNU Lesser General Public
  20 * License along with this program; if not,  see <http://www.gnu.org/licenses>
  21 */
  22
  23#include <stdlib.h>
  24#include <stdio.h>
  25#include <stdarg.h>
  26#include <libgen.h>
  27#include <inttypes.h>
  28#include <string.h>
  29#include <unistd.h>
  30#include <fcntl.h>
  31#include <errno.h>
  32#include <asm/unistd.h>
  33#include <linux/err.h>
  34#include <linux/kernel.h>
  35#include <linux/bpf.h>
  36#include <linux/list.h>
  37#include <linux/limits.h>
  38#include <sys/stat.h>
  39#include <sys/types.h>
  40#include <sys/vfs.h>
  41#include <libelf.h>
  42#include <gelf.h>
  43
  44#include "libbpf.h"
  45#include "bpf.h"
  46
  47#ifndef EM_BPF
  48#define EM_BPF 247
  49#endif
  50
  51#ifndef BPF_FS_MAGIC
  52#define BPF_FS_MAGIC            0xcafe4a11
  53#endif
  54
  55#define __printf(a, b)  __attribute__((format(printf, a, b)))
  56
  57__printf(1, 2)
  58static int __base_pr(const char *format, ...)
  59{
  60        va_list args;
  61        int err;
  62
  63        va_start(args, format);
  64        err = vfprintf(stderr, format, args);
  65        va_end(args);
  66        return err;
  67}
  68
  69static __printf(1, 2) libbpf_print_fn_t __pr_warning = __base_pr;
  70static __printf(1, 2) libbpf_print_fn_t __pr_info = __base_pr;
  71static __printf(1, 2) libbpf_print_fn_t __pr_debug;
  72
  73#define __pr(func, fmt, ...)    \
  74do {                            \
  75        if ((func))             \
  76                (func)("libbpf: " fmt, ##__VA_ARGS__); \
  77} while (0)
  78
  79#define pr_warning(fmt, ...)    __pr(__pr_warning, fmt, ##__VA_ARGS__)
  80#define pr_info(fmt, ...)       __pr(__pr_info, fmt, ##__VA_ARGS__)
  81#define pr_debug(fmt, ...)      __pr(__pr_debug, fmt, ##__VA_ARGS__)
  82
  83void libbpf_set_print(libbpf_print_fn_t warn,
  84                      libbpf_print_fn_t info,
  85                      libbpf_print_fn_t debug)
  86{
  87        __pr_warning = warn;
  88        __pr_info = info;
  89        __pr_debug = debug;
  90}
  91
  92#define STRERR_BUFSIZE  128
  93
  94#define ERRNO_OFFSET(e)         ((e) - __LIBBPF_ERRNO__START)
  95#define ERRCODE_OFFSET(c)       ERRNO_OFFSET(LIBBPF_ERRNO__##c)
  96#define NR_ERRNO        (__LIBBPF_ERRNO__END - __LIBBPF_ERRNO__START)
  97
  98static const char *libbpf_strerror_table[NR_ERRNO] = {
  99        [ERRCODE_OFFSET(LIBELF)]        = "Something wrong in libelf",
 100        [ERRCODE_OFFSET(FORMAT)]        = "BPF object format invalid",
 101        [ERRCODE_OFFSET(KVERSION)]      = "'version' section incorrect or lost",
 102        [ERRCODE_OFFSET(ENDIAN)]        = "Endian mismatch",
 103        [ERRCODE_OFFSET(INTERNAL)]      = "Internal error in libbpf",
 104        [ERRCODE_OFFSET(RELOC)]         = "Relocation failed",
 105        [ERRCODE_OFFSET(VERIFY)]        = "Kernel verifier blocks program loading",
 106        [ERRCODE_OFFSET(PROG2BIG)]      = "Program too big",
 107        [ERRCODE_OFFSET(KVER)]          = "Incorrect kernel version",
 108        [ERRCODE_OFFSET(PROGTYPE)]      = "Kernel doesn't support this program type",
 109};
 110
 111int libbpf_strerror(int err, char *buf, size_t size)
 112{
 113        if (!buf || !size)
 114                return -1;
 115
 116        err = err > 0 ? err : -err;
 117
 118        if (err < __LIBBPF_ERRNO__START) {
 119                int ret;
 120
 121                ret = strerror_r(err, buf, size);
 122                buf[size - 1] = '\0';
 123                return ret;
 124        }
 125
 126        if (err < __LIBBPF_ERRNO__END) {
 127                const char *msg;
 128
 129                msg = libbpf_strerror_table[ERRNO_OFFSET(err)];
 130                snprintf(buf, size, "%s", msg);
 131                buf[size - 1] = '\0';
 132                return 0;
 133        }
 134
 135        snprintf(buf, size, "Unknown libbpf error %d", err);
 136        buf[size - 1] = '\0';
 137        return -1;
 138}
 139
 140#define CHECK_ERR(action, err, out) do {        \
 141        err = action;                   \
 142        if (err)                        \
 143                goto out;               \
 144} while(0)
 145
 146
 147/* Copied from tools/perf/util/util.h */
 148#ifndef zfree
 149# define zfree(ptr) ({ free(*ptr); *ptr = NULL; })
 150#endif
 151
 152#ifndef zclose
 153# define zclose(fd) ({                  \
 154        int ___err = 0;                 \
 155        if ((fd) >= 0)                  \
 156                ___err = close((fd));   \
 157        fd = -1;                        \
 158        ___err; })
 159#endif
 160
 161#ifdef HAVE_LIBELF_MMAP_SUPPORT
 162# define LIBBPF_ELF_C_READ_MMAP ELF_C_READ_MMAP
 163#else
 164# define LIBBPF_ELF_C_READ_MMAP ELF_C_READ
 165#endif
 166
 167/*
 168 * bpf_prog should be a better name but it has been used in
 169 * linux/filter.h.
 170 */
 171struct bpf_program {
 172        /* Index in elf obj file, for relocation use. */
 173        int idx;
 174        char *section_name;
 175        struct bpf_insn *insns;
 176        size_t insns_cnt;
 177        enum bpf_prog_type type;
 178
 179        struct {
 180                int insn_idx;
 181                int map_idx;
 182        } *reloc_desc;
 183        int nr_reloc;
 184
 185        struct {
 186                int nr;
 187                int *fds;
 188        } instances;
 189        bpf_program_prep_t preprocessor;
 190
 191        struct bpf_object *obj;
 192        void *priv;
 193        bpf_program_clear_priv_t clear_priv;
 194};
 195
 196struct bpf_map {
 197        int fd;
 198        char *name;
 199        size_t offset;
 200        struct bpf_map_def def;
 201        void *priv;
 202        bpf_map_clear_priv_t clear_priv;
 203};
 204
 205static LIST_HEAD(bpf_objects_list);
 206
 207struct bpf_object {
 208        char license[64];
 209        u32 kern_version;
 210
 211        struct bpf_program *programs;
 212        size_t nr_programs;
 213        struct bpf_map *maps;
 214        size_t nr_maps;
 215
 216        bool loaded;
 217
 218        /*
 219         * Information when doing elf related work. Only valid if fd
 220         * is valid.
 221         */
 222        struct {
 223                int fd;
 224                void *obj_buf;
 225                size_t obj_buf_sz;
 226                Elf *elf;
 227                GElf_Ehdr ehdr;
 228                Elf_Data *symbols;
 229                size_t strtabidx;
 230                struct {
 231                        GElf_Shdr shdr;
 232                        Elf_Data *data;
 233                } *reloc;
 234                int nr_reloc;
 235                int maps_shndx;
 236        } efile;
 237        /*
 238         * All loaded bpf_object is linked in a list, which is
 239         * hidden to caller. bpf_objects__<func> handlers deal with
 240         * all objects.
 241         */
 242        struct list_head list;
 243
 244        void *priv;
 245        bpf_object_clear_priv_t clear_priv;
 246
 247        char path[];
 248};
 249#define obj_elf_valid(o)        ((o)->efile.elf)
 250
 251static void bpf_program__unload(struct bpf_program *prog)
 252{
 253        int i;
 254
 255        if (!prog)
 256                return;
 257
 258        /*
 259         * If the object is opened but the program was never loaded,
 260         * it is possible that prog->instances.nr == -1.
 261         */
 262        if (prog->instances.nr > 0) {
 263                for (i = 0; i < prog->instances.nr; i++)
 264                        zclose(prog->instances.fds[i]);
 265        } else if (prog->instances.nr != -1) {
 266                pr_warning("Internal error: instances.nr is %d\n",
 267                           prog->instances.nr);
 268        }
 269
 270        prog->instances.nr = -1;
 271        zfree(&prog->instances.fds);
 272}
 273
 274static void bpf_program__exit(struct bpf_program *prog)
 275{
 276        if (!prog)
 277                return;
 278
 279        if (prog->clear_priv)
 280                prog->clear_priv(prog, prog->priv);
 281
 282        prog->priv = NULL;
 283        prog->clear_priv = NULL;
 284
 285        bpf_program__unload(prog);
 286        zfree(&prog->section_name);
 287        zfree(&prog->insns);
 288        zfree(&prog->reloc_desc);
 289
 290        prog->nr_reloc = 0;
 291        prog->insns_cnt = 0;
 292        prog->idx = -1;
 293}
 294
 295static int
 296bpf_program__init(void *data, size_t size, char *name, int idx,
 297                    struct bpf_program *prog)
 298{
 299        if (size < sizeof(struct bpf_insn)) {
 300                pr_warning("corrupted section '%s'\n", name);
 301                return -EINVAL;
 302        }
 303
 304        bzero(prog, sizeof(*prog));
 305
 306        prog->section_name = strdup(name);
 307        if (!prog->section_name) {
 308                pr_warning("failed to alloc name for prog %s\n",
 309                           name);
 310                goto errout;
 311        }
 312
 313        prog->insns = malloc(size);
 314        if (!prog->insns) {
 315                pr_warning("failed to alloc insns for %s\n", name);
 316                goto errout;
 317        }
 318        prog->insns_cnt = size / sizeof(struct bpf_insn);
 319        memcpy(prog->insns, data,
 320               prog->insns_cnt * sizeof(struct bpf_insn));
 321        prog->idx = idx;
 322        prog->instances.fds = NULL;
 323        prog->instances.nr = -1;
 324        prog->type = BPF_PROG_TYPE_KPROBE;
 325
 326        return 0;
 327errout:
 328        bpf_program__exit(prog);
 329        return -ENOMEM;
 330}
 331
 332static int
 333bpf_object__add_program(struct bpf_object *obj, void *data, size_t size,
 334                        char *name, int idx)
 335{
 336        struct bpf_program prog, *progs;
 337        int nr_progs, err;
 338
 339        err = bpf_program__init(data, size, name, idx, &prog);
 340        if (err)
 341                return err;
 342
 343        progs = obj->programs;
 344        nr_progs = obj->nr_programs;
 345
 346        progs = realloc(progs, sizeof(progs[0]) * (nr_progs + 1));
 347        if (!progs) {
 348                /*
 349                 * In this case the original obj->programs
 350                 * is still valid, so don't need special treat for
 351                 * bpf_close_object().
 352                 */
 353                pr_warning("failed to alloc a new program '%s'\n",
 354                           name);
 355                bpf_program__exit(&prog);
 356                return -ENOMEM;
 357        }
 358
 359        pr_debug("found program %s\n", prog.section_name);
 360        obj->programs = progs;
 361        obj->nr_programs = nr_progs + 1;
 362        prog.obj = obj;
 363        progs[nr_progs] = prog;
 364        return 0;
 365}
 366
 367static struct bpf_object *bpf_object__new(const char *path,
 368                                          void *obj_buf,
 369                                          size_t obj_buf_sz)
 370{
 371        struct bpf_object *obj;
 372
 373        obj = calloc(1, sizeof(struct bpf_object) + strlen(path) + 1);
 374        if (!obj) {
 375                pr_warning("alloc memory failed for %s\n", path);
 376                return ERR_PTR(-ENOMEM);
 377        }
 378
 379        strcpy(obj->path, path);
 380        obj->efile.fd = -1;
 381
 382        /*
 383         * Caller of this function should also calls
 384         * bpf_object__elf_finish() after data collection to return
 385         * obj_buf to user. If not, we should duplicate the buffer to
 386         * avoid user freeing them before elf finish.
 387         */
 388        obj->efile.obj_buf = obj_buf;
 389        obj->efile.obj_buf_sz = obj_buf_sz;
 390        obj->efile.maps_shndx = -1;
 391
 392        obj->loaded = false;
 393
 394        INIT_LIST_HEAD(&obj->list);
 395        list_add(&obj->list, &bpf_objects_list);
 396        return obj;
 397}
 398
 399static void bpf_object__elf_finish(struct bpf_object *obj)
 400{
 401        if (!obj_elf_valid(obj))
 402                return;
 403
 404        if (obj->efile.elf) {
 405                elf_end(obj->efile.elf);
 406                obj->efile.elf = NULL;
 407        }
 408        obj->efile.symbols = NULL;
 409
 410        zfree(&obj->efile.reloc);
 411        obj->efile.nr_reloc = 0;
 412        zclose(obj->efile.fd);
 413        obj->efile.obj_buf = NULL;
 414        obj->efile.obj_buf_sz = 0;
 415}
 416
 417static int bpf_object__elf_init(struct bpf_object *obj)
 418{
 419        int err = 0;
 420        GElf_Ehdr *ep;
 421
 422        if (obj_elf_valid(obj)) {
 423                pr_warning("elf init: internal error\n");
 424                return -LIBBPF_ERRNO__LIBELF;
 425        }
 426
 427        if (obj->efile.obj_buf_sz > 0) {
 428                /*
 429                 * obj_buf should have been validated by
 430                 * bpf_object__open_buffer().
 431                 */
 432                obj->efile.elf = elf_memory(obj->efile.obj_buf,
 433                                            obj->efile.obj_buf_sz);
 434        } else {
 435                obj->efile.fd = open(obj->path, O_RDONLY);
 436                if (obj->efile.fd < 0) {
 437                        pr_warning("failed to open %s: %s\n", obj->path,
 438                                        strerror(errno));
 439                        return -errno;
 440                }
 441
 442                obj->efile.elf = elf_begin(obj->efile.fd,
 443                                LIBBPF_ELF_C_READ_MMAP,
 444                                NULL);
 445        }
 446
 447        if (!obj->efile.elf) {
 448                pr_warning("failed to open %s as ELF file\n",
 449                                obj->path);
 450                err = -LIBBPF_ERRNO__LIBELF;
 451                goto errout;
 452        }
 453
 454        if (!gelf_getehdr(obj->efile.elf, &obj->efile.ehdr)) {
 455                pr_warning("failed to get EHDR from %s\n",
 456                                obj->path);
 457                err = -LIBBPF_ERRNO__FORMAT;
 458                goto errout;
 459        }
 460        ep = &obj->efile.ehdr;
 461
 462        /* Old LLVM set e_machine to EM_NONE */
 463        if ((ep->e_type != ET_REL) || (ep->e_machine && (ep->e_machine != EM_BPF))) {
 464                pr_warning("%s is not an eBPF object file\n",
 465                        obj->path);
 466                err = -LIBBPF_ERRNO__FORMAT;
 467                goto errout;
 468        }
 469
 470        return 0;
 471errout:
 472        bpf_object__elf_finish(obj);
 473        return err;
 474}
 475
 476static int
 477bpf_object__check_endianness(struct bpf_object *obj)
 478{
 479        static unsigned int const endian = 1;
 480
 481        switch (obj->efile.ehdr.e_ident[EI_DATA]) {
 482        case ELFDATA2LSB:
 483                /* We are big endian, BPF obj is little endian. */
 484                if (*(unsigned char const *)&endian != 1)
 485                        goto mismatch;
 486                break;
 487
 488        case ELFDATA2MSB:
 489                /* We are little endian, BPF obj is big endian. */
 490                if (*(unsigned char const *)&endian != 0)
 491                        goto mismatch;
 492                break;
 493        default:
 494                return -LIBBPF_ERRNO__ENDIAN;
 495        }
 496
 497        return 0;
 498
 499mismatch:
 500        pr_warning("Error: endianness mismatch.\n");
 501        return -LIBBPF_ERRNO__ENDIAN;
 502}
 503
 504static int
 505bpf_object__init_license(struct bpf_object *obj,
 506                         void *data, size_t size)
 507{
 508        memcpy(obj->license, data,
 509               min(size, sizeof(obj->license) - 1));
 510        pr_debug("license of %s is %s\n", obj->path, obj->license);
 511        return 0;
 512}
 513
 514static int
 515bpf_object__init_kversion(struct bpf_object *obj,
 516                          void *data, size_t size)
 517{
 518        u32 kver;
 519
 520        if (size != sizeof(kver)) {
 521                pr_warning("invalid kver section in %s\n", obj->path);
 522                return -LIBBPF_ERRNO__FORMAT;
 523        }
 524        memcpy(&kver, data, sizeof(kver));
 525        obj->kern_version = kver;
 526        pr_debug("kernel version of %s is %x\n", obj->path,
 527                 obj->kern_version);
 528        return 0;
 529}
 530
 531static int
 532bpf_object__validate_maps(struct bpf_object *obj)
 533{
 534        int i;
 535
 536        /*
 537         * If there's only 1 map, the only error case should have been
 538         * catched in bpf_object__init_maps().
 539         */
 540        if (!obj->maps || !obj->nr_maps || (obj->nr_maps == 1))
 541                return 0;
 542
 543        for (i = 1; i < obj->nr_maps; i++) {
 544                const struct bpf_map *a = &obj->maps[i - 1];
 545                const struct bpf_map *b = &obj->maps[i];
 546
 547                if (b->offset - a->offset < sizeof(struct bpf_map_def)) {
 548                        pr_warning("corrupted map section in %s: map \"%s\" too small\n",
 549                                   obj->path, a->name);
 550                        return -EINVAL;
 551                }
 552        }
 553        return 0;
 554}
 555
 556static int compare_bpf_map(const void *_a, const void *_b)
 557{
 558        const struct bpf_map *a = _a;
 559        const struct bpf_map *b = _b;
 560
 561        return a->offset - b->offset;
 562}
 563
 564static int
 565bpf_object__init_maps(struct bpf_object *obj)
 566{
 567        int i, map_idx, nr_maps = 0;
 568        Elf_Scn *scn;
 569        Elf_Data *data;
 570        Elf_Data *symbols = obj->efile.symbols;
 571
 572        if (obj->efile.maps_shndx < 0)
 573                return -EINVAL;
 574        if (!symbols)
 575                return -EINVAL;
 576
 577        scn = elf_getscn(obj->efile.elf, obj->efile.maps_shndx);
 578        if (scn)
 579                data = elf_getdata(scn, NULL);
 580        if (!scn || !data) {
 581                pr_warning("failed to get Elf_Data from map section %d\n",
 582                           obj->efile.maps_shndx);
 583                return -EINVAL;
 584        }
 585
 586        /*
 587         * Count number of maps. Each map has a name.
 588         * Array of maps is not supported: only the first element is
 589         * considered.
 590         *
 591         * TODO: Detect array of map and report error.
 592         */
 593        for (i = 0; i < symbols->d_size / sizeof(GElf_Sym); i++) {
 594                GElf_Sym sym;
 595
 596                if (!gelf_getsym(symbols, i, &sym))
 597                        continue;
 598                if (sym.st_shndx != obj->efile.maps_shndx)
 599                        continue;
 600                nr_maps++;
 601        }
 602
 603        /* Alloc obj->maps and fill nr_maps. */
 604        pr_debug("maps in %s: %d maps in %zd bytes\n", obj->path,
 605                 nr_maps, data->d_size);
 606
 607        if (!nr_maps)
 608                return 0;
 609
 610        obj->maps = calloc(nr_maps, sizeof(obj->maps[0]));
 611        if (!obj->maps) {
 612                pr_warning("alloc maps for object failed\n");
 613                return -ENOMEM;
 614        }
 615        obj->nr_maps = nr_maps;
 616
 617        /*
 618         * fill all fd with -1 so won't close incorrect
 619         * fd (fd=0 is stdin) when failure (zclose won't close
 620         * negative fd)).
 621         */
 622        for (i = 0; i < nr_maps; i++)
 623                obj->maps[i].fd = -1;
 624
 625        /*
 626         * Fill obj->maps using data in "maps" section.
 627         */
 628        for (i = 0, map_idx = 0; i < symbols->d_size / sizeof(GElf_Sym); i++) {
 629                GElf_Sym sym;
 630                const char *map_name;
 631                struct bpf_map_def *def;
 632
 633                if (!gelf_getsym(symbols, i, &sym))
 634                        continue;
 635                if (sym.st_shndx != obj->efile.maps_shndx)
 636                        continue;
 637
 638                map_name = elf_strptr(obj->efile.elf,
 639                                      obj->efile.strtabidx,
 640                                      sym.st_name);
 641                obj->maps[map_idx].offset = sym.st_value;
 642                if (sym.st_value + sizeof(struct bpf_map_def) > data->d_size) {
 643                        pr_warning("corrupted maps section in %s: last map \"%s\" too small\n",
 644                                   obj->path, map_name);
 645                        return -EINVAL;
 646                }
 647
 648                obj->maps[map_idx].name = strdup(map_name);
 649                if (!obj->maps[map_idx].name) {
 650                        pr_warning("failed to alloc map name\n");
 651                        return -ENOMEM;
 652                }
 653                pr_debug("map %d is \"%s\"\n", map_idx,
 654                         obj->maps[map_idx].name);
 655                def = (struct bpf_map_def *)(data->d_buf + sym.st_value);
 656                obj->maps[map_idx].def = *def;
 657                map_idx++;
 658        }
 659
 660        qsort(obj->maps, obj->nr_maps, sizeof(obj->maps[0]), compare_bpf_map);
 661        return bpf_object__validate_maps(obj);
 662}
 663
 664static int bpf_object__elf_collect(struct bpf_object *obj)
 665{
 666        Elf *elf = obj->efile.elf;
 667        GElf_Ehdr *ep = &obj->efile.ehdr;
 668        Elf_Scn *scn = NULL;
 669        int idx = 0, err = 0;
 670
 671        /* Elf is corrupted/truncated, avoid calling elf_strptr. */
 672        if (!elf_rawdata(elf_getscn(elf, ep->e_shstrndx), NULL)) {
 673                pr_warning("failed to get e_shstrndx from %s\n",
 674                           obj->path);
 675                return -LIBBPF_ERRNO__FORMAT;
 676        }
 677
 678        while ((scn = elf_nextscn(elf, scn)) != NULL) {
 679                char *name;
 680                GElf_Shdr sh;
 681                Elf_Data *data;
 682
 683                idx++;
 684                if (gelf_getshdr(scn, &sh) != &sh) {
 685                        pr_warning("failed to get section header from %s\n",
 686                                   obj->path);
 687                        err = -LIBBPF_ERRNO__FORMAT;
 688                        goto out;
 689                }
 690
 691                name = elf_strptr(elf, ep->e_shstrndx, sh.sh_name);
 692                if (!name) {
 693                        pr_warning("failed to get section name from %s\n",
 694                                   obj->path);
 695                        err = -LIBBPF_ERRNO__FORMAT;
 696                        goto out;
 697                }
 698
 699                data = elf_getdata(scn, 0);
 700                if (!data) {
 701                        pr_warning("failed to get section data from %s(%s)\n",
 702                                   name, obj->path);
 703                        err = -LIBBPF_ERRNO__FORMAT;
 704                        goto out;
 705                }
 706                pr_debug("section %s, size %ld, link %d, flags %lx, type=%d\n",
 707                         name, (unsigned long)data->d_size,
 708                         (int)sh.sh_link, (unsigned long)sh.sh_flags,
 709                         (int)sh.sh_type);
 710
 711                if (strcmp(name, "license") == 0)
 712                        err = bpf_object__init_license(obj,
 713                                                       data->d_buf,
 714                                                       data->d_size);
 715                else if (strcmp(name, "version") == 0)
 716                        err = bpf_object__init_kversion(obj,
 717                                                        data->d_buf,
 718                                                        data->d_size);
 719                else if (strcmp(name, "maps") == 0)
 720                        obj->efile.maps_shndx = idx;
 721                else if (sh.sh_type == SHT_SYMTAB) {
 722                        if (obj->efile.symbols) {
 723                                pr_warning("bpf: multiple SYMTAB in %s\n",
 724                                           obj->path);
 725                                err = -LIBBPF_ERRNO__FORMAT;
 726                        } else {
 727                                obj->efile.symbols = data;
 728                                obj->efile.strtabidx = sh.sh_link;
 729                        }
 730                } else if ((sh.sh_type == SHT_PROGBITS) &&
 731                           (sh.sh_flags & SHF_EXECINSTR) &&
 732                           (data->d_size > 0)) {
 733                        err = bpf_object__add_program(obj, data->d_buf,
 734                                                      data->d_size, name, idx);
 735                        if (err) {
 736                                char errmsg[STRERR_BUFSIZE];
 737
 738                                strerror_r(-err, errmsg, sizeof(errmsg));
 739                                pr_warning("failed to alloc program %s (%s): %s",
 740                                           name, obj->path, errmsg);
 741                        }
 742                } else if (sh.sh_type == SHT_REL) {
 743                        void *reloc = obj->efile.reloc;
 744                        int nr_reloc = obj->efile.nr_reloc + 1;
 745
 746                        reloc = realloc(reloc,
 747                                        sizeof(*obj->efile.reloc) * nr_reloc);
 748                        if (!reloc) {
 749                                pr_warning("realloc failed\n");
 750                                err = -ENOMEM;
 751                        } else {
 752                                int n = nr_reloc - 1;
 753
 754                                obj->efile.reloc = reloc;
 755                                obj->efile.nr_reloc = nr_reloc;
 756
 757                                obj->efile.reloc[n].shdr = sh;
 758                                obj->efile.reloc[n].data = data;
 759                        }
 760                }
 761                if (err)
 762                        goto out;
 763        }
 764
 765        if (!obj->efile.strtabidx || obj->efile.strtabidx >= idx) {
 766                pr_warning("Corrupted ELF file: index of strtab invalid\n");
 767                return LIBBPF_ERRNO__FORMAT;
 768        }
 769        if (obj->efile.maps_shndx >= 0)
 770                err = bpf_object__init_maps(obj);
 771out:
 772        return err;
 773}
 774
 775static struct bpf_program *
 776bpf_object__find_prog_by_idx(struct bpf_object *obj, int idx)
 777{
 778        struct bpf_program *prog;
 779        size_t i;
 780
 781        for (i = 0; i < obj->nr_programs; i++) {
 782                prog = &obj->programs[i];
 783                if (prog->idx == idx)
 784                        return prog;
 785        }
 786        return NULL;
 787}
 788
 789static int
 790bpf_program__collect_reloc(struct bpf_program *prog,
 791                           size_t nr_maps, GElf_Shdr *shdr,
 792                           Elf_Data *data, Elf_Data *symbols,
 793                           int maps_shndx, struct bpf_map *maps)
 794{
 795        int i, nrels;
 796
 797        pr_debug("collecting relocating info for: '%s'\n",
 798                 prog->section_name);
 799        nrels = shdr->sh_size / shdr->sh_entsize;
 800
 801        prog->reloc_desc = malloc(sizeof(*prog->reloc_desc) * nrels);
 802        if (!prog->reloc_desc) {
 803                pr_warning("failed to alloc memory in relocation\n");
 804                return -ENOMEM;
 805        }
 806        prog->nr_reloc = nrels;
 807
 808        for (i = 0; i < nrels; i++) {
 809                GElf_Sym sym;
 810                GElf_Rel rel;
 811                unsigned int insn_idx;
 812                struct bpf_insn *insns = prog->insns;
 813                size_t map_idx;
 814
 815                if (!gelf_getrel(data, i, &rel)) {
 816                        pr_warning("relocation: failed to get %d reloc\n", i);
 817                        return -LIBBPF_ERRNO__FORMAT;
 818                }
 819
 820                if (!gelf_getsym(symbols,
 821                                 GELF_R_SYM(rel.r_info),
 822                                 &sym)) {
 823                        pr_warning("relocation: symbol %"PRIx64" not found\n",
 824                                   GELF_R_SYM(rel.r_info));
 825                        return -LIBBPF_ERRNO__FORMAT;
 826                }
 827
 828                if (sym.st_shndx != maps_shndx) {
 829                        pr_warning("Program '%s' contains non-map related relo data pointing to section %u\n",
 830                                   prog->section_name, sym.st_shndx);
 831                        return -LIBBPF_ERRNO__RELOC;
 832                }
 833
 834                insn_idx = rel.r_offset / sizeof(struct bpf_insn);
 835                pr_debug("relocation: insn_idx=%u\n", insn_idx);
 836
 837                if (insns[insn_idx].code != (BPF_LD | BPF_IMM | BPF_DW)) {
 838                        pr_warning("bpf: relocation: invalid relo for insns[%d].code 0x%x\n",
 839                                   insn_idx, insns[insn_idx].code);
 840                        return -LIBBPF_ERRNO__RELOC;
 841                }
 842
 843                /* TODO: 'maps' is sorted. We can use bsearch to make it faster. */
 844                for (map_idx = 0; map_idx < nr_maps; map_idx++) {
 845                        if (maps[map_idx].offset == sym.st_value) {
 846                                pr_debug("relocation: find map %zd (%s) for insn %u\n",
 847                                         map_idx, maps[map_idx].name, insn_idx);
 848                                break;
 849                        }
 850                }
 851
 852                if (map_idx >= nr_maps) {
 853                        pr_warning("bpf relocation: map_idx %d large than %d\n",
 854                                   (int)map_idx, (int)nr_maps - 1);
 855                        return -LIBBPF_ERRNO__RELOC;
 856                }
 857
 858                prog->reloc_desc[i].insn_idx = insn_idx;
 859                prog->reloc_desc[i].map_idx = map_idx;
 860        }
 861        return 0;
 862}
 863
 864static int
 865bpf_object__create_maps(struct bpf_object *obj)
 866{
 867        unsigned int i;
 868
 869        for (i = 0; i < obj->nr_maps; i++) {
 870                struct bpf_map_def *def = &obj->maps[i].def;
 871                int *pfd = &obj->maps[i].fd;
 872
 873                *pfd = bpf_create_map(def->type,
 874                                      def->key_size,
 875                                      def->value_size,
 876                                      def->max_entries,
 877                                      0);
 878                if (*pfd < 0) {
 879                        size_t j;
 880                        int err = *pfd;
 881
 882                        pr_warning("failed to create map (name: '%s'): %s\n",
 883                                   obj->maps[i].name,
 884                                   strerror(errno));
 885                        for (j = 0; j < i; j++)
 886                                zclose(obj->maps[j].fd);
 887                        return err;
 888                }
 889                pr_debug("create map %s: fd=%d\n", obj->maps[i].name, *pfd);
 890        }
 891
 892        return 0;
 893}
 894
 895static int
 896bpf_program__relocate(struct bpf_program *prog, struct bpf_object *obj)
 897{
 898        int i;
 899
 900        if (!prog || !prog->reloc_desc)
 901                return 0;
 902
 903        for (i = 0; i < prog->nr_reloc; i++) {
 904                int insn_idx, map_idx;
 905                struct bpf_insn *insns = prog->insns;
 906
 907                insn_idx = prog->reloc_desc[i].insn_idx;
 908                map_idx = prog->reloc_desc[i].map_idx;
 909
 910                if (insn_idx >= (int)prog->insns_cnt) {
 911                        pr_warning("relocation out of range: '%s'\n",
 912                                   prog->section_name);
 913                        return -LIBBPF_ERRNO__RELOC;
 914                }
 915                insns[insn_idx].src_reg = BPF_PSEUDO_MAP_FD;
 916                insns[insn_idx].imm = obj->maps[map_idx].fd;
 917        }
 918
 919        zfree(&prog->reloc_desc);
 920        prog->nr_reloc = 0;
 921        return 0;
 922}
 923
 924
 925static int
 926bpf_object__relocate(struct bpf_object *obj)
 927{
 928        struct bpf_program *prog;
 929        size_t i;
 930        int err;
 931
 932        for (i = 0; i < obj->nr_programs; i++) {
 933                prog = &obj->programs[i];
 934
 935                err = bpf_program__relocate(prog, obj);
 936                if (err) {
 937                        pr_warning("failed to relocate '%s'\n",
 938                                   prog->section_name);
 939                        return err;
 940                }
 941        }
 942        return 0;
 943}
 944
 945static int bpf_object__collect_reloc(struct bpf_object *obj)
 946{
 947        int i, err;
 948
 949        if (!obj_elf_valid(obj)) {
 950                pr_warning("Internal error: elf object is closed\n");
 951                return -LIBBPF_ERRNO__INTERNAL;
 952        }
 953
 954        for (i = 0; i < obj->efile.nr_reloc; i++) {
 955                GElf_Shdr *shdr = &obj->efile.reloc[i].shdr;
 956                Elf_Data *data = obj->efile.reloc[i].data;
 957                int idx = shdr->sh_info;
 958                struct bpf_program *prog;
 959                size_t nr_maps = obj->nr_maps;
 960
 961                if (shdr->sh_type != SHT_REL) {
 962                        pr_warning("internal error at %d\n", __LINE__);
 963                        return -LIBBPF_ERRNO__INTERNAL;
 964                }
 965
 966                prog = bpf_object__find_prog_by_idx(obj, idx);
 967                if (!prog) {
 968                        pr_warning("relocation failed: no %d section\n",
 969                                   idx);
 970                        return -LIBBPF_ERRNO__RELOC;
 971                }
 972
 973                err = bpf_program__collect_reloc(prog, nr_maps,
 974                                                 shdr, data,
 975                                                 obj->efile.symbols,
 976                                                 obj->efile.maps_shndx,
 977                                                 obj->maps);
 978                if (err)
 979                        return err;
 980        }
 981        return 0;
 982}
 983
 984static int
 985load_program(enum bpf_prog_type type, struct bpf_insn *insns,
 986             int insns_cnt, char *license, u32 kern_version, int *pfd)
 987{
 988        int ret;
 989        char *log_buf;
 990
 991        if (!insns || !insns_cnt)
 992                return -EINVAL;
 993
 994        log_buf = malloc(BPF_LOG_BUF_SIZE);
 995        if (!log_buf)
 996                pr_warning("Alloc log buffer for bpf loader error, continue without log\n");
 997
 998        ret = bpf_load_program(type, insns, insns_cnt, license,
 999                               kern_version, log_buf, BPF_LOG_BUF_SIZE);
1000
1001        if (ret >= 0) {
1002                *pfd = ret;
1003                ret = 0;
1004                goto out;
1005        }
1006
1007        ret = -LIBBPF_ERRNO__LOAD;
1008        pr_warning("load bpf program failed: %s\n", strerror(errno));
1009
1010        if (log_buf && log_buf[0] != '\0') {
1011                ret = -LIBBPF_ERRNO__VERIFY;
1012                pr_warning("-- BEGIN DUMP LOG ---\n");
1013                pr_warning("\n%s\n", log_buf);
1014                pr_warning("-- END LOG --\n");
1015        } else if (insns_cnt >= BPF_MAXINSNS) {
1016                pr_warning("Program too large (%d insns), at most %d insns\n",
1017                           insns_cnt, BPF_MAXINSNS);
1018                ret = -LIBBPF_ERRNO__PROG2BIG;
1019        } else {
1020                /* Wrong program type? */
1021                if (type != BPF_PROG_TYPE_KPROBE) {
1022                        int fd;
1023
1024                        fd = bpf_load_program(BPF_PROG_TYPE_KPROBE, insns,
1025                                              insns_cnt, license, kern_version,
1026                                              NULL, 0);
1027                        if (fd >= 0) {
1028                                close(fd);
1029                                ret = -LIBBPF_ERRNO__PROGTYPE;
1030                                goto out;
1031                        }
1032                }
1033
1034                if (log_buf)
1035                        ret = -LIBBPF_ERRNO__KVER;
1036        }
1037
1038out:
1039        free(log_buf);
1040        return ret;
1041}
1042
1043static int
1044bpf_program__load(struct bpf_program *prog,
1045                  char *license, u32 kern_version)
1046{
1047        int err = 0, fd, i;
1048
1049        if (prog->instances.nr < 0 || !prog->instances.fds) {
1050                if (prog->preprocessor) {
1051                        pr_warning("Internal error: can't load program '%s'\n",
1052                                   prog->section_name);
1053                        return -LIBBPF_ERRNO__INTERNAL;
1054                }
1055
1056                prog->instances.fds = malloc(sizeof(int));
1057                if (!prog->instances.fds) {
1058                        pr_warning("Not enough memory for BPF fds\n");
1059                        return -ENOMEM;
1060                }
1061                prog->instances.nr = 1;
1062                prog->instances.fds[0] = -1;
1063        }
1064
1065        if (!prog->preprocessor) {
1066                if (prog->instances.nr != 1) {
1067                        pr_warning("Program '%s' is inconsistent: nr(%d) != 1\n",
1068                                   prog->section_name, prog->instances.nr);
1069                }
1070                err = load_program(prog->type, prog->insns, prog->insns_cnt,
1071                                   license, kern_version, &fd);
1072                if (!err)
1073                        prog->instances.fds[0] = fd;
1074                goto out;
1075        }
1076
1077        for (i = 0; i < prog->instances.nr; i++) {
1078                struct bpf_prog_prep_result result;
1079                bpf_program_prep_t preprocessor = prog->preprocessor;
1080
1081                bzero(&result, sizeof(result));
1082                err = preprocessor(prog, i, prog->insns,
1083                                   prog->insns_cnt, &result);
1084                if (err) {
1085                        pr_warning("Preprocessing the %dth instance of program '%s' failed\n",
1086                                   i, prog->section_name);
1087                        goto out;
1088                }
1089
1090                if (!result.new_insn_ptr || !result.new_insn_cnt) {
1091                        pr_debug("Skip loading the %dth instance of program '%s'\n",
1092                                 i, prog->section_name);
1093                        prog->instances.fds[i] = -1;
1094                        if (result.pfd)
1095                                *result.pfd = -1;
1096                        continue;
1097                }
1098
1099                err = load_program(prog->type, result.new_insn_ptr,
1100                                   result.new_insn_cnt,
1101                                   license, kern_version, &fd);
1102
1103                if (err) {
1104                        pr_warning("Loading the %dth instance of program '%s' failed\n",
1105                                        i, prog->section_name);
1106                        goto out;
1107                }
1108
1109                if (result.pfd)
1110                        *result.pfd = fd;
1111                prog->instances.fds[i] = fd;
1112        }
1113out:
1114        if (err)
1115                pr_warning("failed to load program '%s'\n",
1116                           prog->section_name);
1117        zfree(&prog->insns);
1118        prog->insns_cnt = 0;
1119        return err;
1120}
1121
1122static int
1123bpf_object__load_progs(struct bpf_object *obj)
1124{
1125        size_t i;
1126        int err;
1127
1128        for (i = 0; i < obj->nr_programs; i++) {
1129                err = bpf_program__load(&obj->programs[i],
1130                                        obj->license,
1131                                        obj->kern_version);
1132                if (err)
1133                        return err;
1134        }
1135        return 0;
1136}
1137
1138static int bpf_object__validate(struct bpf_object *obj)
1139{
1140        if (obj->kern_version == 0) {
1141                pr_warning("%s doesn't provide kernel version\n",
1142                           obj->path);
1143                return -LIBBPF_ERRNO__KVERSION;
1144        }
1145        return 0;
1146}
1147
1148static struct bpf_object *
1149__bpf_object__open(const char *path, void *obj_buf, size_t obj_buf_sz)
1150{
1151        struct bpf_object *obj;
1152        int err;
1153
1154        if (elf_version(EV_CURRENT) == EV_NONE) {
1155                pr_warning("failed to init libelf for %s\n", path);
1156                return ERR_PTR(-LIBBPF_ERRNO__LIBELF);
1157        }
1158
1159        obj = bpf_object__new(path, obj_buf, obj_buf_sz);
1160        if (IS_ERR(obj))
1161                return obj;
1162
1163        CHECK_ERR(bpf_object__elf_init(obj), err, out);
1164        CHECK_ERR(bpf_object__check_endianness(obj), err, out);
1165        CHECK_ERR(bpf_object__elf_collect(obj), err, out);
1166        CHECK_ERR(bpf_object__collect_reloc(obj), err, out);
1167        CHECK_ERR(bpf_object__validate(obj), err, out);
1168
1169        bpf_object__elf_finish(obj);
1170        return obj;
1171out:
1172        bpf_object__close(obj);
1173        return ERR_PTR(err);
1174}
1175
1176struct bpf_object *bpf_object__open(const char *path)
1177{
1178        /* param validation */
1179        if (!path)
1180                return NULL;
1181
1182        pr_debug("loading %s\n", path);
1183
1184        return __bpf_object__open(path, NULL, 0);
1185}
1186
1187struct bpf_object *bpf_object__open_buffer(void *obj_buf,
1188                                           size_t obj_buf_sz,
1189                                           const char *name)
1190{
1191        char tmp_name[64];
1192
1193        /* param validation */
1194        if (!obj_buf || obj_buf_sz <= 0)
1195                return NULL;
1196
1197        if (!name) {
1198                snprintf(tmp_name, sizeof(tmp_name), "%lx-%lx",
1199                         (unsigned long)obj_buf,
1200                         (unsigned long)obj_buf_sz);
1201                tmp_name[sizeof(tmp_name) - 1] = '\0';
1202                name = tmp_name;
1203        }
1204        pr_debug("loading object '%s' from buffer\n",
1205                 name);
1206
1207        return __bpf_object__open(name, obj_buf, obj_buf_sz);
1208}
1209
1210int bpf_object__unload(struct bpf_object *obj)
1211{
1212        size_t i;
1213
1214        if (!obj)
1215                return -EINVAL;
1216
1217        for (i = 0; i < obj->nr_maps; i++)
1218                zclose(obj->maps[i].fd);
1219
1220        for (i = 0; i < obj->nr_programs; i++)
1221                bpf_program__unload(&obj->programs[i]);
1222
1223        return 0;
1224}
1225
1226int bpf_object__load(struct bpf_object *obj)
1227{
1228        int err;
1229
1230        if (!obj)
1231                return -EINVAL;
1232
1233        if (obj->loaded) {
1234                pr_warning("object should not be loaded twice\n");
1235                return -EINVAL;
1236        }
1237
1238        obj->loaded = true;
1239
1240        CHECK_ERR(bpf_object__create_maps(obj), err, out);
1241        CHECK_ERR(bpf_object__relocate(obj), err, out);
1242        CHECK_ERR(bpf_object__load_progs(obj), err, out);
1243
1244        return 0;
1245out:
1246        bpf_object__unload(obj);
1247        pr_warning("failed to load object '%s'\n", obj->path);
1248        return err;
1249}
1250
1251static int check_path(const char *path)
1252{
1253        struct statfs st_fs;
1254        char *dname, *dir;
1255        int err = 0;
1256
1257        if (path == NULL)
1258                return -EINVAL;
1259
1260        dname = strdup(path);
1261        if (dname == NULL)
1262                return -ENOMEM;
1263
1264        dir = dirname(dname);
1265        if (statfs(dir, &st_fs)) {
1266                pr_warning("failed to statfs %s: %s\n", dir, strerror(errno));
1267                err = -errno;
1268        }
1269        free(dname);
1270
1271        if (!err && st_fs.f_type != BPF_FS_MAGIC) {
1272                pr_warning("specified path %s is not on BPF FS\n", path);
1273                err = -EINVAL;
1274        }
1275
1276        return err;
1277}
1278
1279int bpf_program__pin_instance(struct bpf_program *prog, const char *path,
1280                              int instance)
1281{
1282        int err;
1283
1284        err = check_path(path);
1285        if (err)
1286                return err;
1287
1288        if (prog == NULL) {
1289                pr_warning("invalid program pointer\n");
1290                return -EINVAL;
1291        }
1292
1293        if (instance < 0 || instance >= prog->instances.nr) {
1294                pr_warning("invalid prog instance %d of prog %s (max %d)\n",
1295                           instance, prog->section_name, prog->instances.nr);
1296                return -EINVAL;
1297        }
1298
1299        if (bpf_obj_pin(prog->instances.fds[instance], path)) {
1300                pr_warning("failed to pin program: %s\n", strerror(errno));
1301                return -errno;
1302        }
1303        pr_debug("pinned program '%s'\n", path);
1304
1305        return 0;
1306}
1307
1308static int make_dir(const char *path)
1309{
1310        int err = 0;
1311
1312        if (mkdir(path, 0700) && errno != EEXIST)
1313                err = -errno;
1314
1315        if (err)
1316                pr_warning("failed to mkdir %s: %s\n", path, strerror(-err));
1317        return err;
1318}
1319
1320int bpf_program__pin(struct bpf_program *prog, const char *path)
1321{
1322        int i, err;
1323
1324        err = check_path(path);
1325        if (err)
1326                return err;
1327
1328        if (prog == NULL) {
1329                pr_warning("invalid program pointer\n");
1330                return -EINVAL;
1331        }
1332
1333        if (prog->instances.nr <= 0) {
1334                pr_warning("no instances of prog %s to pin\n",
1335                           prog->section_name);
1336                return -EINVAL;
1337        }
1338
1339        err = make_dir(path);
1340        if (err)
1341                return err;
1342
1343        for (i = 0; i < prog->instances.nr; i++) {
1344                char buf[PATH_MAX];
1345                int len;
1346
1347                len = snprintf(buf, PATH_MAX, "%s/%d", path, i);
1348                if (len < 0)
1349                        return -EINVAL;
1350                else if (len >= PATH_MAX)
1351                        return -ENAMETOOLONG;
1352
1353                err = bpf_program__pin_instance(prog, buf, i);
1354                if (err)
1355                        return err;
1356        }
1357
1358        return 0;
1359}
1360
1361int bpf_map__pin(struct bpf_map *map, const char *path)
1362{
1363        int err;
1364
1365        err = check_path(path);
1366        if (err)
1367                return err;
1368
1369        if (map == NULL) {
1370                pr_warning("invalid map pointer\n");
1371                return -EINVAL;
1372        }
1373
1374        if (bpf_obj_pin(map->fd, path)) {
1375                pr_warning("failed to pin map: %s\n", strerror(errno));
1376                return -errno;
1377        }
1378
1379        pr_debug("pinned map '%s'\n", path);
1380        return 0;
1381}
1382
1383int bpf_object__pin(struct bpf_object *obj, const char *path)
1384{
1385        struct bpf_program *prog;
1386        struct bpf_map *map;
1387        int err;
1388
1389        if (!obj)
1390                return -ENOENT;
1391
1392        if (!obj->loaded) {
1393                pr_warning("object not yet loaded; load it first\n");
1394                return -ENOENT;
1395        }
1396
1397        err = make_dir(path);
1398        if (err)
1399                return err;
1400
1401        bpf_map__for_each(map, obj) {
1402                char buf[PATH_MAX];
1403                int len;
1404
1405                len = snprintf(buf, PATH_MAX, "%s/%s", path,
1406                               bpf_map__name(map));
1407                if (len < 0)
1408                        return -EINVAL;
1409                else if (len >= PATH_MAX)
1410                        return -ENAMETOOLONG;
1411
1412                err = bpf_map__pin(map, buf);
1413                if (err)
1414                        return err;
1415        }
1416
1417        bpf_object__for_each_program(prog, obj) {
1418                char buf[PATH_MAX];
1419                int len;
1420
1421                len = snprintf(buf, PATH_MAX, "%s/%s", path,
1422                               prog->section_name);
1423                if (len < 0)
1424                        return -EINVAL;
1425                else if (len >= PATH_MAX)
1426                        return -ENAMETOOLONG;
1427
1428                err = bpf_program__pin(prog, buf);
1429                if (err)
1430                        return err;
1431        }
1432
1433        return 0;
1434}
1435
1436void bpf_object__close(struct bpf_object *obj)
1437{
1438        size_t i;
1439
1440        if (!obj)
1441                return;
1442
1443        if (obj->clear_priv)
1444                obj->clear_priv(obj, obj->priv);
1445
1446        bpf_object__elf_finish(obj);
1447        bpf_object__unload(obj);
1448
1449        for (i = 0; i < obj->nr_maps; i++) {
1450                zfree(&obj->maps[i].name);
1451                if (obj->maps[i].clear_priv)
1452                        obj->maps[i].clear_priv(&obj->maps[i],
1453                                                obj->maps[i].priv);
1454                obj->maps[i].priv = NULL;
1455                obj->maps[i].clear_priv = NULL;
1456        }
1457        zfree(&obj->maps);
1458        obj->nr_maps = 0;
1459
1460        if (obj->programs && obj->nr_programs) {
1461                for (i = 0; i < obj->nr_programs; i++)
1462                        bpf_program__exit(&obj->programs[i]);
1463        }
1464        zfree(&obj->programs);
1465
1466        list_del(&obj->list);
1467        free(obj);
1468}
1469
1470struct bpf_object *
1471bpf_object__next(struct bpf_object *prev)
1472{
1473        struct bpf_object *next;
1474
1475        if (!prev)
1476                next = list_first_entry(&bpf_objects_list,
1477                                        struct bpf_object,
1478                                        list);
1479        else
1480                next = list_next_entry(prev, list);
1481
1482        /* Empty list is noticed here so don't need checking on entry. */
1483        if (&next->list == &bpf_objects_list)
1484                return NULL;
1485
1486        return next;
1487}
1488
1489const char *bpf_object__name(struct bpf_object *obj)
1490{
1491        return obj ? obj->path : ERR_PTR(-EINVAL);
1492}
1493
1494unsigned int bpf_object__kversion(struct bpf_object *obj)
1495{
1496        return obj ? obj->kern_version : 0;
1497}
1498
1499int bpf_object__set_priv(struct bpf_object *obj, void *priv,
1500                         bpf_object_clear_priv_t clear_priv)
1501{
1502        if (obj->priv && obj->clear_priv)
1503                obj->clear_priv(obj, obj->priv);
1504
1505        obj->priv = priv;
1506        obj->clear_priv = clear_priv;
1507        return 0;
1508}
1509
1510void *bpf_object__priv(struct bpf_object *obj)
1511{
1512        return obj ? obj->priv : ERR_PTR(-EINVAL);
1513}
1514
1515struct bpf_program *
1516bpf_program__next(struct bpf_program *prev, struct bpf_object *obj)
1517{
1518        size_t idx;
1519
1520        if (!obj->programs)
1521                return NULL;
1522        /* First handler */
1523        if (prev == NULL)
1524                return &obj->programs[0];
1525
1526        if (prev->obj != obj) {
1527                pr_warning("error: program handler doesn't match object\n");
1528                return NULL;
1529        }
1530
1531        idx = (prev - obj->programs) + 1;
1532        if (idx >= obj->nr_programs)
1533                return NULL;
1534        return &obj->programs[idx];
1535}
1536
1537int bpf_program__set_priv(struct bpf_program *prog, void *priv,
1538                          bpf_program_clear_priv_t clear_priv)
1539{
1540        if (prog->priv && prog->clear_priv)
1541                prog->clear_priv(prog, prog->priv);
1542
1543        prog->priv = priv;
1544        prog->clear_priv = clear_priv;
1545        return 0;
1546}
1547
1548void *bpf_program__priv(struct bpf_program *prog)
1549{
1550        return prog ? prog->priv : ERR_PTR(-EINVAL);
1551}
1552
1553const char *bpf_program__title(struct bpf_program *prog, bool needs_copy)
1554{
1555        const char *title;
1556
1557        title = prog->section_name;
1558        if (needs_copy) {
1559                title = strdup(title);
1560                if (!title) {
1561                        pr_warning("failed to strdup program title\n");
1562                        return ERR_PTR(-ENOMEM);
1563                }
1564        }
1565
1566        return title;
1567}
1568
1569int bpf_program__fd(struct bpf_program *prog)
1570{
1571        return bpf_program__nth_fd(prog, 0);
1572}
1573
1574int bpf_program__set_prep(struct bpf_program *prog, int nr_instances,
1575                          bpf_program_prep_t prep)
1576{
1577        int *instances_fds;
1578
1579        if (nr_instances <= 0 || !prep)
1580                return -EINVAL;
1581
1582        if (prog->instances.nr > 0 || prog->instances.fds) {
1583                pr_warning("Can't set pre-processor after loading\n");
1584                return -EINVAL;
1585        }
1586
1587        instances_fds = malloc(sizeof(int) * nr_instances);
1588        if (!instances_fds) {
1589                pr_warning("alloc memory failed for fds\n");
1590                return -ENOMEM;
1591        }
1592
1593        /* fill all fd with -1 */
1594        memset(instances_fds, -1, sizeof(int) * nr_instances);
1595
1596        prog->instances.nr = nr_instances;
1597        prog->instances.fds = instances_fds;
1598        prog->preprocessor = prep;
1599        return 0;
1600}
1601
1602int bpf_program__nth_fd(struct bpf_program *prog, int n)
1603{
1604        int fd;
1605
1606        if (n >= prog->instances.nr || n < 0) {
1607                pr_warning("Can't get the %dth fd from program %s: only %d instances\n",
1608                           n, prog->section_name, prog->instances.nr);
1609                return -EINVAL;
1610        }
1611
1612        fd = prog->instances.fds[n];
1613        if (fd < 0) {
1614                pr_warning("%dth instance of program '%s' is invalid\n",
1615                           n, prog->section_name);
1616                return -ENOENT;
1617        }
1618
1619        return fd;
1620}
1621
1622void bpf_program__set_type(struct bpf_program *prog, enum bpf_prog_type type)
1623{
1624        prog->type = type;
1625}
1626
1627static bool bpf_program__is_type(struct bpf_program *prog,
1628                                 enum bpf_prog_type type)
1629{
1630        return prog ? (prog->type == type) : false;
1631}
1632
1633#define BPF_PROG_TYPE_FNS(NAME, TYPE)                   \
1634int bpf_program__set_##NAME(struct bpf_program *prog)   \
1635{                                                       \
1636        if (!prog)                                      \
1637                return -EINVAL;                         \
1638        bpf_program__set_type(prog, TYPE);              \
1639        return 0;                                       \
1640}                                                       \
1641                                                        \
1642bool bpf_program__is_##NAME(struct bpf_program *prog)   \
1643{                                                       \
1644        return bpf_program__is_type(prog, TYPE);        \
1645}                                                       \
1646
1647BPF_PROG_TYPE_FNS(socket_filter, BPF_PROG_TYPE_SOCKET_FILTER);
1648BPF_PROG_TYPE_FNS(kprobe, BPF_PROG_TYPE_KPROBE);
1649BPF_PROG_TYPE_FNS(sched_cls, BPF_PROG_TYPE_SCHED_CLS);
1650BPF_PROG_TYPE_FNS(sched_act, BPF_PROG_TYPE_SCHED_ACT);
1651BPF_PROG_TYPE_FNS(tracepoint, BPF_PROG_TYPE_TRACEPOINT);
1652BPF_PROG_TYPE_FNS(xdp, BPF_PROG_TYPE_XDP);
1653BPF_PROG_TYPE_FNS(perf_event, BPF_PROG_TYPE_PERF_EVENT);
1654
1655int bpf_map__fd(struct bpf_map *map)
1656{
1657        return map ? map->fd : -EINVAL;
1658}
1659
1660const struct bpf_map_def *bpf_map__def(struct bpf_map *map)
1661{
1662        return map ? &map->def : ERR_PTR(-EINVAL);
1663}
1664
1665const char *bpf_map__name(struct bpf_map *map)
1666{
1667        return map ? map->name : NULL;
1668}
1669
1670int bpf_map__set_priv(struct bpf_map *map, void *priv,
1671                     bpf_map_clear_priv_t clear_priv)
1672{
1673        if (!map)
1674                return -EINVAL;
1675
1676        if (map->priv) {
1677                if (map->clear_priv)
1678                        map->clear_priv(map, map->priv);
1679        }
1680
1681        map->priv = priv;
1682        map->clear_priv = clear_priv;
1683        return 0;
1684}
1685
1686void *bpf_map__priv(struct bpf_map *map)
1687{
1688        return map ? map->priv : ERR_PTR(-EINVAL);
1689}
1690
1691struct bpf_map *
1692bpf_map__next(struct bpf_map *prev, struct bpf_object *obj)
1693{
1694        size_t idx;
1695        struct bpf_map *s, *e;
1696
1697        if (!obj || !obj->maps)
1698                return NULL;
1699
1700        s = obj->maps;
1701        e = obj->maps + obj->nr_maps;
1702
1703        if (prev == NULL)
1704                return s;
1705
1706        if ((prev < s) || (prev >= e)) {
1707                pr_warning("error in %s: map handler doesn't belong to object\n",
1708                           __func__);
1709                return NULL;
1710        }
1711
1712        idx = (prev - obj->maps) + 1;
1713        if (idx >= obj->nr_maps)
1714                return NULL;
1715        return &obj->maps[idx];
1716}
1717
1718struct bpf_map *
1719bpf_object__find_map_by_name(struct bpf_object *obj, const char *name)
1720{
1721        struct bpf_map *pos;
1722
1723        bpf_map__for_each(pos, obj) {
1724                if (pos->name && !strcmp(pos->name, name))
1725                        return pos;
1726        }
1727        return NULL;
1728}
1729
1730struct bpf_map *
1731bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset)
1732{
1733        int i;
1734
1735        for (i = 0; i < obj->nr_maps; i++) {
1736                if (obj->maps[i].offset == offset)
1737                        return &obj->maps[i];
1738        }
1739        return ERR_PTR(-ENOENT);
1740}
1741
1742long libbpf_get_error(const void *ptr)
1743{
1744        if (IS_ERR(ptr))
1745                return PTR_ERR(ptr);
1746        return 0;
1747}
1748
1749int bpf_prog_load(const char *file, enum bpf_prog_type type,
1750                  struct bpf_object **pobj, int *prog_fd)
1751{
1752        struct bpf_program *prog;
1753        struct bpf_object *obj;
1754        int err;
1755
1756        obj = bpf_object__open(file);
1757        if (IS_ERR(obj))
1758                return -ENOENT;
1759
1760        prog = bpf_program__next(NULL, obj);
1761        if (!prog) {
1762                bpf_object__close(obj);
1763                return -ENOENT;
1764        }
1765
1766        bpf_program__set_type(prog, type);
1767        err = bpf_object__load(obj);
1768        if (err) {
1769                bpf_object__close(obj);
1770                return -EINVAL;
1771        }
1772
1773        *pobj = obj;
1774        *prog_fd = bpf_program__fd(prog);
1775        return 0;
1776}
1777