linux/tools/lib/bpf/btf.c
<<
>>
Prefs
   1// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
   2/* Copyright (c) 2018 Facebook */
   3
   4#include <byteswap.h>
   5#include <endian.h>
   6#include <stdio.h>
   7#include <stdlib.h>
   8#include <string.h>
   9#include <fcntl.h>
  10#include <unistd.h>
  11#include <errno.h>
  12#include <sys/utsname.h>
  13#include <sys/param.h>
  14#include <sys/stat.h>
  15#include <linux/kernel.h>
  16#include <linux/err.h>
  17#include <linux/btf.h>
  18#include <gelf.h>
  19#include "btf.h"
  20#include "bpf.h"
  21#include "libbpf.h"
  22#include "libbpf_internal.h"
  23#include "hashmap.h"
  24
  25#define BTF_MAX_NR_TYPES 0x7fffffffU
  26#define BTF_MAX_STR_OFFSET 0x7fffffffU
  27
  28static struct btf_type btf_void;
  29
  30struct btf {
  31        /* raw BTF data in native endianness */
  32        void *raw_data;
  33        /* raw BTF data in non-native endianness */
  34        void *raw_data_swapped;
  35        __u32 raw_size;
  36        /* whether target endianness differs from the native one */
  37        bool swapped_endian;
  38
  39        /*
  40         * When BTF is loaded from an ELF or raw memory it is stored
  41         * in a contiguous memory block. The hdr, type_data, and, strs_data
  42         * point inside that memory region to their respective parts of BTF
  43         * representation:
  44         *
  45         * +--------------------------------+
  46         * |  Header  |  Types  |  Strings  |
  47         * +--------------------------------+
  48         * ^          ^         ^
  49         * |          |         |
  50         * hdr        |         |
  51         * types_data-+         |
  52         * strs_data------------+
  53         *
  54         * If BTF data is later modified, e.g., due to types added or
  55         * removed, BTF deduplication performed, etc, this contiguous
  56         * representation is broken up into three independently allocated
  57         * memory regions to be able to modify them independently.
  58         * raw_data is nulled out at that point, but can be later allocated
  59         * and cached again if user calls btf__get_raw_data(), at which point
  60         * raw_data will contain a contiguous copy of header, types, and
  61         * strings:
  62         *
  63         * +----------+  +---------+  +-----------+
  64         * |  Header  |  |  Types  |  |  Strings  |
  65         * +----------+  +---------+  +-----------+
  66         * ^             ^            ^
  67         * |             |            |
  68         * hdr           |            |
  69         * types_data----+            |
  70         * strs_data------------------+
  71         *
  72         *               +----------+---------+-----------+
  73         *               |  Header  |  Types  |  Strings  |
  74         * raw_data----->+----------+---------+-----------+
  75         */
  76        struct btf_header *hdr;
  77
  78        void *types_data;
  79        size_t types_data_cap; /* used size stored in hdr->type_len */
  80
  81        /* type ID to `struct btf_type *` lookup index
  82         * type_offs[0] corresponds to the first non-VOID type:
  83         *   - for base BTF it's type [1];
  84         *   - for split BTF it's the first non-base BTF type.
  85         */
  86        __u32 *type_offs;
  87        size_t type_offs_cap;
  88        /* number of types in this BTF instance:
  89         *   - doesn't include special [0] void type;
  90         *   - for split BTF counts number of types added on top of base BTF.
  91         */
  92        __u32 nr_types;
  93        /* if not NULL, points to the base BTF on top of which the current
  94         * split BTF is based
  95         */
  96        struct btf *base_btf;
  97        /* BTF type ID of the first type in this BTF instance:
  98         *   - for base BTF it's equal to 1;
  99         *   - for split BTF it's equal to biggest type ID of base BTF plus 1.
 100         */
 101        int start_id;
 102        /* logical string offset of this BTF instance:
 103         *   - for base BTF it's equal to 0;
 104         *   - for split BTF it's equal to total size of base BTF's string section size.
 105         */
 106        int start_str_off;
 107
 108        void *strs_data;
 109        size_t strs_data_cap; /* used size stored in hdr->str_len */
 110
 111        /* lookup index for each unique string in strings section */
 112        struct hashmap *strs_hash;
 113        /* whether strings are already deduplicated */
 114        bool strs_deduped;
 115        /* extra indirection layer to make strings hashmap work with stable
 116         * string offsets and ability to transparently choose between
 117         * btf->strs_data or btf_dedup->strs_data as a source of strings.
 118         * This is used for BTF strings dedup to transfer deduplicated strings
 119         * data back to struct btf without re-building strings index.
 120         */
 121        void **strs_data_ptr;
 122
 123        /* BTF object FD, if loaded into kernel */
 124        int fd;
 125
 126        /* Pointer size (in bytes) for a target architecture of this BTF */
 127        int ptr_sz;
 128};
 129
 130static inline __u64 ptr_to_u64(const void *ptr)
 131{
 132        return (__u64) (unsigned long) ptr;
 133}
 134
 135/* Ensure given dynamically allocated memory region pointed to by *data* with
 136 * capacity of *cap_cnt* elements each taking *elem_sz* bytes has enough
 137 * memory to accomodate *add_cnt* new elements, assuming *cur_cnt* elements
 138 * are already used. At most *max_cnt* elements can be ever allocated.
 139 * If necessary, memory is reallocated and all existing data is copied over,
 140 * new pointer to the memory region is stored at *data, new memory region
 141 * capacity (in number of elements) is stored in *cap.
 142 * On success, memory pointer to the beginning of unused memory is returned.
 143 * On error, NULL is returned.
 144 */
 145void *btf_add_mem(void **data, size_t *cap_cnt, size_t elem_sz,
 146                  size_t cur_cnt, size_t max_cnt, size_t add_cnt)
 147{
 148        size_t new_cnt;
 149        void *new_data;
 150
 151        if (cur_cnt + add_cnt <= *cap_cnt)
 152                return *data + cur_cnt * elem_sz;
 153
 154        /* requested more than the set limit */
 155        if (cur_cnt + add_cnt > max_cnt)
 156                return NULL;
 157
 158        new_cnt = *cap_cnt;
 159        new_cnt += new_cnt / 4;           /* expand by 25% */
 160        if (new_cnt < 16)                 /* but at least 16 elements */
 161                new_cnt = 16;
 162        if (new_cnt > max_cnt)            /* but not exceeding a set limit */
 163                new_cnt = max_cnt;
 164        if (new_cnt < cur_cnt + add_cnt)  /* also ensure we have enough memory */
 165                new_cnt = cur_cnt + add_cnt;
 166
 167        new_data = libbpf_reallocarray(*data, new_cnt, elem_sz);
 168        if (!new_data)
 169                return NULL;
 170
 171        /* zero out newly allocated portion of memory */
 172        memset(new_data + (*cap_cnt) * elem_sz, 0, (new_cnt - *cap_cnt) * elem_sz);
 173
 174        *data = new_data;
 175        *cap_cnt = new_cnt;
 176        return new_data + cur_cnt * elem_sz;
 177}
 178
 179/* Ensure given dynamically allocated memory region has enough allocated space
 180 * to accommodate *need_cnt* elements of size *elem_sz* bytes each
 181 */
 182int btf_ensure_mem(void **data, size_t *cap_cnt, size_t elem_sz, size_t need_cnt)
 183{
 184        void *p;
 185
 186        if (need_cnt <= *cap_cnt)
 187                return 0;
 188
 189        p = btf_add_mem(data, cap_cnt, elem_sz, *cap_cnt, SIZE_MAX, need_cnt - *cap_cnt);
 190        if (!p)
 191                return -ENOMEM;
 192
 193        return 0;
 194}
 195
 196static int btf_add_type_idx_entry(struct btf *btf, __u32 type_off)
 197{
 198        __u32 *p;
 199
 200        p = btf_add_mem((void **)&btf->type_offs, &btf->type_offs_cap, sizeof(__u32),
 201                        btf->nr_types, BTF_MAX_NR_TYPES, 1);
 202        if (!p)
 203                return -ENOMEM;
 204
 205        *p = type_off;
 206        return 0;
 207}
 208
 209static void btf_bswap_hdr(struct btf_header *h)
 210{
 211        h->magic = bswap_16(h->magic);
 212        h->hdr_len = bswap_32(h->hdr_len);
 213        h->type_off = bswap_32(h->type_off);
 214        h->type_len = bswap_32(h->type_len);
 215        h->str_off = bswap_32(h->str_off);
 216        h->str_len = bswap_32(h->str_len);
 217}
 218
 219static int btf_parse_hdr(struct btf *btf)
 220{
 221        struct btf_header *hdr = btf->hdr;
 222        __u32 meta_left;
 223
 224        if (btf->raw_size < sizeof(struct btf_header)) {
 225                pr_debug("BTF header not found\n");
 226                return -EINVAL;
 227        }
 228
 229        if (hdr->magic == bswap_16(BTF_MAGIC)) {
 230                btf->swapped_endian = true;
 231                if (bswap_32(hdr->hdr_len) != sizeof(struct btf_header)) {
 232                        pr_warn("Can't load BTF with non-native endianness due to unsupported header length %u\n",
 233                                bswap_32(hdr->hdr_len));
 234                        return -ENOTSUP;
 235                }
 236                btf_bswap_hdr(hdr);
 237        } else if (hdr->magic != BTF_MAGIC) {
 238                pr_debug("Invalid BTF magic:%x\n", hdr->magic);
 239                return -EINVAL;
 240        }
 241
 242        meta_left = btf->raw_size - sizeof(*hdr);
 243        if (meta_left < hdr->str_off + hdr->str_len) {
 244                pr_debug("Invalid BTF total size:%u\n", btf->raw_size);
 245                return -EINVAL;
 246        }
 247
 248        if (hdr->type_off + hdr->type_len > hdr->str_off) {
 249                pr_debug("Invalid BTF data sections layout: type data at %u + %u, strings data at %u + %u\n",
 250                         hdr->type_off, hdr->type_len, hdr->str_off, hdr->str_len);
 251                return -EINVAL;
 252        }
 253
 254        if (hdr->type_off % 4) {
 255                pr_debug("BTF type section is not aligned to 4 bytes\n");
 256                return -EINVAL;
 257        }
 258
 259        return 0;
 260}
 261
 262static int btf_parse_str_sec(struct btf *btf)
 263{
 264        const struct btf_header *hdr = btf->hdr;
 265        const char *start = btf->strs_data;
 266        const char *end = start + btf->hdr->str_len;
 267
 268        if (btf->base_btf && hdr->str_len == 0)
 269                return 0;
 270        if (!hdr->str_len || hdr->str_len - 1 > BTF_MAX_STR_OFFSET || end[-1]) {
 271                pr_debug("Invalid BTF string section\n");
 272                return -EINVAL;
 273        }
 274        if (!btf->base_btf && start[0]) {
 275                pr_debug("Invalid BTF string section\n");
 276                return -EINVAL;
 277        }
 278        return 0;
 279}
 280
 281static int btf_type_size(const struct btf_type *t)
 282{
 283        const int base_size = sizeof(struct btf_type);
 284        __u16 vlen = btf_vlen(t);
 285
 286        switch (btf_kind(t)) {
 287        case BTF_KIND_FWD:
 288        case BTF_KIND_CONST:
 289        case BTF_KIND_VOLATILE:
 290        case BTF_KIND_RESTRICT:
 291        case BTF_KIND_PTR:
 292        case BTF_KIND_TYPEDEF:
 293        case BTF_KIND_FUNC:
 294        case BTF_KIND_FLOAT:
 295                return base_size;
 296        case BTF_KIND_INT:
 297                return base_size + sizeof(__u32);
 298        case BTF_KIND_ENUM:
 299                return base_size + vlen * sizeof(struct btf_enum);
 300        case BTF_KIND_ARRAY:
 301                return base_size + sizeof(struct btf_array);
 302        case BTF_KIND_STRUCT:
 303        case BTF_KIND_UNION:
 304                return base_size + vlen * sizeof(struct btf_member);
 305        case BTF_KIND_FUNC_PROTO:
 306                return base_size + vlen * sizeof(struct btf_param);
 307        case BTF_KIND_VAR:
 308                return base_size + sizeof(struct btf_var);
 309        case BTF_KIND_DATASEC:
 310                return base_size + vlen * sizeof(struct btf_var_secinfo);
 311        default:
 312                pr_debug("Unsupported BTF_KIND:%u\n", btf_kind(t));
 313                return -EINVAL;
 314        }
 315}
 316
 317static void btf_bswap_type_base(struct btf_type *t)
 318{
 319        t->name_off = bswap_32(t->name_off);
 320        t->info = bswap_32(t->info);
 321        t->type = bswap_32(t->type);
 322}
 323
 324static int btf_bswap_type_rest(struct btf_type *t)
 325{
 326        struct btf_var_secinfo *v;
 327        struct btf_member *m;
 328        struct btf_array *a;
 329        struct btf_param *p;
 330        struct btf_enum *e;
 331        __u16 vlen = btf_vlen(t);
 332        int i;
 333
 334        switch (btf_kind(t)) {
 335        case BTF_KIND_FWD:
 336        case BTF_KIND_CONST:
 337        case BTF_KIND_VOLATILE:
 338        case BTF_KIND_RESTRICT:
 339        case BTF_KIND_PTR:
 340        case BTF_KIND_TYPEDEF:
 341        case BTF_KIND_FUNC:
 342        case BTF_KIND_FLOAT:
 343                return 0;
 344        case BTF_KIND_INT:
 345                *(__u32 *)(t + 1) = bswap_32(*(__u32 *)(t + 1));
 346                return 0;
 347        case BTF_KIND_ENUM:
 348                for (i = 0, e = btf_enum(t); i < vlen; i++, e++) {
 349                        e->name_off = bswap_32(e->name_off);
 350                        e->val = bswap_32(e->val);
 351                }
 352                return 0;
 353        case BTF_KIND_ARRAY:
 354                a = btf_array(t);
 355                a->type = bswap_32(a->type);
 356                a->index_type = bswap_32(a->index_type);
 357                a->nelems = bswap_32(a->nelems);
 358                return 0;
 359        case BTF_KIND_STRUCT:
 360        case BTF_KIND_UNION:
 361                for (i = 0, m = btf_members(t); i < vlen; i++, m++) {
 362                        m->name_off = bswap_32(m->name_off);
 363                        m->type = bswap_32(m->type);
 364                        m->offset = bswap_32(m->offset);
 365                }
 366                return 0;
 367        case BTF_KIND_FUNC_PROTO:
 368                for (i = 0, p = btf_params(t); i < vlen; i++, p++) {
 369                        p->name_off = bswap_32(p->name_off);
 370                        p->type = bswap_32(p->type);
 371                }
 372                return 0;
 373        case BTF_KIND_VAR:
 374                btf_var(t)->linkage = bswap_32(btf_var(t)->linkage);
 375                return 0;
 376        case BTF_KIND_DATASEC:
 377                for (i = 0, v = btf_var_secinfos(t); i < vlen; i++, v++) {
 378                        v->type = bswap_32(v->type);
 379                        v->offset = bswap_32(v->offset);
 380                        v->size = bswap_32(v->size);
 381                }
 382                return 0;
 383        default:
 384                pr_debug("Unsupported BTF_KIND:%u\n", btf_kind(t));
 385                return -EINVAL;
 386        }
 387}
 388
 389static int btf_parse_type_sec(struct btf *btf)
 390{
 391        struct btf_header *hdr = btf->hdr;
 392        void *next_type = btf->types_data;
 393        void *end_type = next_type + hdr->type_len;
 394        int err, type_size;
 395
 396        while (next_type + sizeof(struct btf_type) <= end_type) {
 397                if (btf->swapped_endian)
 398                        btf_bswap_type_base(next_type);
 399
 400                type_size = btf_type_size(next_type);
 401                if (type_size < 0)
 402                        return type_size;
 403                if (next_type + type_size > end_type) {
 404                        pr_warn("BTF type [%d] is malformed\n", btf->start_id + btf->nr_types);
 405                        return -EINVAL;
 406                }
 407
 408                if (btf->swapped_endian && btf_bswap_type_rest(next_type))
 409                        return -EINVAL;
 410
 411                err = btf_add_type_idx_entry(btf, next_type - btf->types_data);
 412                if (err)
 413                        return err;
 414
 415                next_type += type_size;
 416                btf->nr_types++;
 417        }
 418
 419        if (next_type != end_type) {
 420                pr_warn("BTF types data is malformed\n");
 421                return -EINVAL;
 422        }
 423
 424        return 0;
 425}
 426
 427__u32 btf__get_nr_types(const struct btf *btf)
 428{
 429        return btf->start_id + btf->nr_types - 1;
 430}
 431
 432const struct btf *btf__base_btf(const struct btf *btf)
 433{
 434        return btf->base_btf;
 435}
 436
 437/* internal helper returning non-const pointer to a type */
 438static struct btf_type *btf_type_by_id(struct btf *btf, __u32 type_id)
 439{
 440        if (type_id == 0)
 441                return &btf_void;
 442        if (type_id < btf->start_id)
 443                return btf_type_by_id(btf->base_btf, type_id);
 444        return btf->types_data + btf->type_offs[type_id - btf->start_id];
 445}
 446
 447const struct btf_type *btf__type_by_id(const struct btf *btf, __u32 type_id)
 448{
 449        if (type_id >= btf->start_id + btf->nr_types)
 450                return NULL;
 451        return btf_type_by_id((struct btf *)btf, type_id);
 452}
 453
 454static int determine_ptr_size(const struct btf *btf)
 455{
 456        const struct btf_type *t;
 457        const char *name;
 458        int i, n;
 459
 460        if (btf->base_btf && btf->base_btf->ptr_sz > 0)
 461                return btf->base_btf->ptr_sz;
 462
 463        n = btf__get_nr_types(btf);
 464        for (i = 1; i <= n; i++) {
 465                t = btf__type_by_id(btf, i);
 466                if (!btf_is_int(t))
 467                        continue;
 468
 469                name = btf__name_by_offset(btf, t->name_off);
 470                if (!name)
 471                        continue;
 472
 473                if (strcmp(name, "long int") == 0 ||
 474                    strcmp(name, "long unsigned int") == 0) {
 475                        if (t->size != 4 && t->size != 8)
 476                                continue;
 477                        return t->size;
 478                }
 479        }
 480
 481        return -1;
 482}
 483
 484static size_t btf_ptr_sz(const struct btf *btf)
 485{
 486        if (!btf->ptr_sz)
 487                ((struct btf *)btf)->ptr_sz = determine_ptr_size(btf);
 488        return btf->ptr_sz < 0 ? sizeof(void *) : btf->ptr_sz;
 489}
 490
 491/* Return pointer size this BTF instance assumes. The size is heuristically
 492 * determined by looking for 'long' or 'unsigned long' integer type and
 493 * recording its size in bytes. If BTF type information doesn't have any such
 494 * type, this function returns 0. In the latter case, native architecture's
 495 * pointer size is assumed, so will be either 4 or 8, depending on
 496 * architecture that libbpf was compiled for. It's possible to override
 497 * guessed value by using btf__set_pointer_size() API.
 498 */
 499size_t btf__pointer_size(const struct btf *btf)
 500{
 501        if (!btf->ptr_sz)
 502                ((struct btf *)btf)->ptr_sz = determine_ptr_size(btf);
 503
 504        if (btf->ptr_sz < 0)
 505                /* not enough BTF type info to guess */
 506                return 0;
 507
 508        return btf->ptr_sz;
 509}
 510
 511/* Override or set pointer size in bytes. Only values of 4 and 8 are
 512 * supported.
 513 */
 514int btf__set_pointer_size(struct btf *btf, size_t ptr_sz)
 515{
 516        if (ptr_sz != 4 && ptr_sz != 8)
 517                return -EINVAL;
 518        btf->ptr_sz = ptr_sz;
 519        return 0;
 520}
 521
 522static bool is_host_big_endian(void)
 523{
 524#if __BYTE_ORDER == __LITTLE_ENDIAN
 525        return false;
 526#elif __BYTE_ORDER == __BIG_ENDIAN
 527        return true;
 528#else
 529# error "Unrecognized __BYTE_ORDER__"
 530#endif
 531}
 532
 533enum btf_endianness btf__endianness(const struct btf *btf)
 534{
 535        if (is_host_big_endian())
 536                return btf->swapped_endian ? BTF_LITTLE_ENDIAN : BTF_BIG_ENDIAN;
 537        else
 538                return btf->swapped_endian ? BTF_BIG_ENDIAN : BTF_LITTLE_ENDIAN;
 539}
 540
 541int btf__set_endianness(struct btf *btf, enum btf_endianness endian)
 542{
 543        if (endian != BTF_LITTLE_ENDIAN && endian != BTF_BIG_ENDIAN)
 544                return -EINVAL;
 545
 546        btf->swapped_endian = is_host_big_endian() != (endian == BTF_BIG_ENDIAN);
 547        if (!btf->swapped_endian) {
 548                free(btf->raw_data_swapped);
 549                btf->raw_data_swapped = NULL;
 550        }
 551        return 0;
 552}
 553
 554static bool btf_type_is_void(const struct btf_type *t)
 555{
 556        return t == &btf_void || btf_is_fwd(t);
 557}
 558
 559static bool btf_type_is_void_or_null(const struct btf_type *t)
 560{
 561        return !t || btf_type_is_void(t);
 562}
 563
 564#define MAX_RESOLVE_DEPTH 32
 565
 566__s64 btf__resolve_size(const struct btf *btf, __u32 type_id)
 567{
 568        const struct btf_array *array;
 569        const struct btf_type *t;
 570        __u32 nelems = 1;
 571        __s64 size = -1;
 572        int i;
 573
 574        t = btf__type_by_id(btf, type_id);
 575        for (i = 0; i < MAX_RESOLVE_DEPTH && !btf_type_is_void_or_null(t);
 576             i++) {
 577                switch (btf_kind(t)) {
 578                case BTF_KIND_INT:
 579                case BTF_KIND_STRUCT:
 580                case BTF_KIND_UNION:
 581                case BTF_KIND_ENUM:
 582                case BTF_KIND_DATASEC:
 583                case BTF_KIND_FLOAT:
 584                        size = t->size;
 585                        goto done;
 586                case BTF_KIND_PTR:
 587                        size = btf_ptr_sz(btf);
 588                        goto done;
 589                case BTF_KIND_TYPEDEF:
 590                case BTF_KIND_VOLATILE:
 591                case BTF_KIND_CONST:
 592                case BTF_KIND_RESTRICT:
 593                case BTF_KIND_VAR:
 594                        type_id = t->type;
 595                        break;
 596                case BTF_KIND_ARRAY:
 597                        array = btf_array(t);
 598                        if (nelems && array->nelems > UINT32_MAX / nelems)
 599                                return -E2BIG;
 600                        nelems *= array->nelems;
 601                        type_id = array->type;
 602                        break;
 603                default:
 604                        return -EINVAL;
 605                }
 606
 607                t = btf__type_by_id(btf, type_id);
 608        }
 609
 610done:
 611        if (size < 0)
 612                return -EINVAL;
 613        if (nelems && size > UINT32_MAX / nelems)
 614                return -E2BIG;
 615
 616        return nelems * size;
 617}
 618
 619int btf__align_of(const struct btf *btf, __u32 id)
 620{
 621        const struct btf_type *t = btf__type_by_id(btf, id);
 622        __u16 kind = btf_kind(t);
 623
 624        switch (kind) {
 625        case BTF_KIND_INT:
 626        case BTF_KIND_ENUM:
 627        case BTF_KIND_FLOAT:
 628                return min(btf_ptr_sz(btf), (size_t)t->size);
 629        case BTF_KIND_PTR:
 630                return btf_ptr_sz(btf);
 631        case BTF_KIND_TYPEDEF:
 632        case BTF_KIND_VOLATILE:
 633        case BTF_KIND_CONST:
 634        case BTF_KIND_RESTRICT:
 635                return btf__align_of(btf, t->type);
 636        case BTF_KIND_ARRAY:
 637                return btf__align_of(btf, btf_array(t)->type);
 638        case BTF_KIND_STRUCT:
 639        case BTF_KIND_UNION: {
 640                const struct btf_member *m = btf_members(t);
 641                __u16 vlen = btf_vlen(t);
 642                int i, max_align = 1, align;
 643
 644                for (i = 0; i < vlen; i++, m++) {
 645                        align = btf__align_of(btf, m->type);
 646                        if (align <= 0)
 647                                return align;
 648                        max_align = max(max_align, align);
 649                }
 650
 651                return max_align;
 652        }
 653        default:
 654                pr_warn("unsupported BTF_KIND:%u\n", btf_kind(t));
 655                return 0;
 656        }
 657}
 658
 659int btf__resolve_type(const struct btf *btf, __u32 type_id)
 660{
 661        const struct btf_type *t;
 662        int depth = 0;
 663
 664        t = btf__type_by_id(btf, type_id);
 665        while (depth < MAX_RESOLVE_DEPTH &&
 666               !btf_type_is_void_or_null(t) &&
 667               (btf_is_mod(t) || btf_is_typedef(t) || btf_is_var(t))) {
 668                type_id = t->type;
 669                t = btf__type_by_id(btf, type_id);
 670                depth++;
 671        }
 672
 673        if (depth == MAX_RESOLVE_DEPTH || btf_type_is_void_or_null(t))
 674                return -EINVAL;
 675
 676        return type_id;
 677}
 678
 679__s32 btf__find_by_name(const struct btf *btf, const char *type_name)
 680{
 681        __u32 i, nr_types = btf__get_nr_types(btf);
 682
 683        if (!strcmp(type_name, "void"))
 684                return 0;
 685
 686        for (i = 1; i <= nr_types; i++) {
 687                const struct btf_type *t = btf__type_by_id(btf, i);
 688                const char *name = btf__name_by_offset(btf, t->name_off);
 689
 690                if (name && !strcmp(type_name, name))
 691                        return i;
 692        }
 693
 694        return -ENOENT;
 695}
 696
 697__s32 btf__find_by_name_kind(const struct btf *btf, const char *type_name,
 698                             __u32 kind)
 699{
 700        __u32 i, nr_types = btf__get_nr_types(btf);
 701
 702        if (kind == BTF_KIND_UNKN || !strcmp(type_name, "void"))
 703                return 0;
 704
 705        for (i = 1; i <= nr_types; i++) {
 706                const struct btf_type *t = btf__type_by_id(btf, i);
 707                const char *name;
 708
 709                if (btf_kind(t) != kind)
 710                        continue;
 711                name = btf__name_by_offset(btf, t->name_off);
 712                if (name && !strcmp(type_name, name))
 713                        return i;
 714        }
 715
 716        return -ENOENT;
 717}
 718
 719static bool btf_is_modifiable(const struct btf *btf)
 720{
 721        return (void *)btf->hdr != btf->raw_data;
 722}
 723
 724void btf__free(struct btf *btf)
 725{
 726        if (IS_ERR_OR_NULL(btf))
 727                return;
 728
 729        if (btf->fd >= 0)
 730                close(btf->fd);
 731
 732        if (btf_is_modifiable(btf)) {
 733                /* if BTF was modified after loading, it will have a split
 734                 * in-memory representation for header, types, and strings
 735                 * sections, so we need to free all of them individually. It
 736                 * might still have a cached contiguous raw data present,
 737                 * which will be unconditionally freed below.
 738                 */
 739                free(btf->hdr);
 740                free(btf->types_data);
 741                free(btf->strs_data);
 742        }
 743        free(btf->raw_data);
 744        free(btf->raw_data_swapped);
 745        free(btf->type_offs);
 746        free(btf);
 747}
 748
 749static struct btf *btf_new_empty(struct btf *base_btf)
 750{
 751        struct btf *btf;
 752
 753        btf = calloc(1, sizeof(*btf));
 754        if (!btf)
 755                return ERR_PTR(-ENOMEM);
 756
 757        btf->nr_types = 0;
 758        btf->start_id = 1;
 759        btf->start_str_off = 0;
 760        btf->fd = -1;
 761        btf->ptr_sz = sizeof(void *);
 762        btf->swapped_endian = false;
 763
 764        if (base_btf) {
 765                btf->base_btf = base_btf;
 766                btf->start_id = btf__get_nr_types(base_btf) + 1;
 767                btf->start_str_off = base_btf->hdr->str_len;
 768        }
 769
 770        /* +1 for empty string at offset 0 */
 771        btf->raw_size = sizeof(struct btf_header) + (base_btf ? 0 : 1);
 772        btf->raw_data = calloc(1, btf->raw_size);
 773        if (!btf->raw_data) {
 774                free(btf);
 775                return ERR_PTR(-ENOMEM);
 776        }
 777
 778        btf->hdr = btf->raw_data;
 779        btf->hdr->hdr_len = sizeof(struct btf_header);
 780        btf->hdr->magic = BTF_MAGIC;
 781        btf->hdr->version = BTF_VERSION;
 782
 783        btf->types_data = btf->raw_data + btf->hdr->hdr_len;
 784        btf->strs_data = btf->raw_data + btf->hdr->hdr_len;
 785        btf->hdr->str_len = base_btf ? 0 : 1; /* empty string at offset 0 */
 786
 787        return btf;
 788}
 789
 790struct btf *btf__new_empty(void)
 791{
 792        return btf_new_empty(NULL);
 793}
 794
 795struct btf *btf__new_empty_split(struct btf *base_btf)
 796{
 797        return btf_new_empty(base_btf);
 798}
 799
 800static struct btf *btf_new(const void *data, __u32 size, struct btf *base_btf)
 801{
 802        struct btf *btf;
 803        int err;
 804
 805        btf = calloc(1, sizeof(struct btf));
 806        if (!btf)
 807                return ERR_PTR(-ENOMEM);
 808
 809        btf->nr_types = 0;
 810        btf->start_id = 1;
 811        btf->start_str_off = 0;
 812
 813        if (base_btf) {
 814                btf->base_btf = base_btf;
 815                btf->start_id = btf__get_nr_types(base_btf) + 1;
 816                btf->start_str_off = base_btf->hdr->str_len;
 817        }
 818
 819        btf->raw_data = malloc(size);
 820        if (!btf->raw_data) {
 821                err = -ENOMEM;
 822                goto done;
 823        }
 824        memcpy(btf->raw_data, data, size);
 825        btf->raw_size = size;
 826
 827        btf->hdr = btf->raw_data;
 828        err = btf_parse_hdr(btf);
 829        if (err)
 830                goto done;
 831
 832        btf->strs_data = btf->raw_data + btf->hdr->hdr_len + btf->hdr->str_off;
 833        btf->types_data = btf->raw_data + btf->hdr->hdr_len + btf->hdr->type_off;
 834
 835        err = btf_parse_str_sec(btf);
 836        err = err ?: btf_parse_type_sec(btf);
 837        if (err)
 838                goto done;
 839
 840        btf->fd = -1;
 841
 842done:
 843        if (err) {
 844                btf__free(btf);
 845                return ERR_PTR(err);
 846        }
 847
 848        return btf;
 849}
 850
 851struct btf *btf__new(const void *data, __u32 size)
 852{
 853        return btf_new(data, size, NULL);
 854}
 855
 856static struct btf *btf_parse_elf(const char *path, struct btf *base_btf,
 857                                 struct btf_ext **btf_ext)
 858{
 859        Elf_Data *btf_data = NULL, *btf_ext_data = NULL;
 860        int err = 0, fd = -1, idx = 0;
 861        struct btf *btf = NULL;
 862        Elf_Scn *scn = NULL;
 863        Elf *elf = NULL;
 864        GElf_Ehdr ehdr;
 865
 866        if (elf_version(EV_CURRENT) == EV_NONE) {
 867                pr_warn("failed to init libelf for %s\n", path);
 868                return ERR_PTR(-LIBBPF_ERRNO__LIBELF);
 869        }
 870
 871        fd = open(path, O_RDONLY);
 872        if (fd < 0) {
 873                err = -errno;
 874                pr_warn("failed to open %s: %s\n", path, strerror(errno));
 875                return ERR_PTR(err);
 876        }
 877
 878        err = -LIBBPF_ERRNO__FORMAT;
 879
 880        elf = elf_begin(fd, ELF_C_READ, NULL);
 881        if (!elf) {
 882                pr_warn("failed to open %s as ELF file\n", path);
 883                goto done;
 884        }
 885        if (!gelf_getehdr(elf, &ehdr)) {
 886                pr_warn("failed to get EHDR from %s\n", path);
 887                goto done;
 888        }
 889        if (!elf_rawdata(elf_getscn(elf, ehdr.e_shstrndx), NULL)) {
 890                pr_warn("failed to get e_shstrndx from %s\n", path);
 891                goto done;
 892        }
 893
 894        while ((scn = elf_nextscn(elf, scn)) != NULL) {
 895                GElf_Shdr sh;
 896                char *name;
 897
 898                idx++;
 899                if (gelf_getshdr(scn, &sh) != &sh) {
 900                        pr_warn("failed to get section(%d) header from %s\n",
 901                                idx, path);
 902                        goto done;
 903                }
 904                name = elf_strptr(elf, ehdr.e_shstrndx, sh.sh_name);
 905                if (!name) {
 906                        pr_warn("failed to get section(%d) name from %s\n",
 907                                idx, path);
 908                        goto done;
 909                }
 910                if (strcmp(name, BTF_ELF_SEC) == 0) {
 911                        btf_data = elf_getdata(scn, 0);
 912                        if (!btf_data) {
 913                                pr_warn("failed to get section(%d, %s) data from %s\n",
 914                                        idx, name, path);
 915                                goto done;
 916                        }
 917                        continue;
 918                } else if (btf_ext && strcmp(name, BTF_EXT_ELF_SEC) == 0) {
 919                        btf_ext_data = elf_getdata(scn, 0);
 920                        if (!btf_ext_data) {
 921                                pr_warn("failed to get section(%d, %s) data from %s\n",
 922                                        idx, name, path);
 923                                goto done;
 924                        }
 925                        continue;
 926                }
 927        }
 928
 929        err = 0;
 930
 931        if (!btf_data) {
 932                err = -ENOENT;
 933                goto done;
 934        }
 935        btf = btf_new(btf_data->d_buf, btf_data->d_size, base_btf);
 936        if (IS_ERR(btf))
 937                goto done;
 938
 939        switch (gelf_getclass(elf)) {
 940        case ELFCLASS32:
 941                btf__set_pointer_size(btf, 4);
 942                break;
 943        case ELFCLASS64:
 944                btf__set_pointer_size(btf, 8);
 945                break;
 946        default:
 947                pr_warn("failed to get ELF class (bitness) for %s\n", path);
 948                break;
 949        }
 950
 951        if (btf_ext && btf_ext_data) {
 952                *btf_ext = btf_ext__new(btf_ext_data->d_buf,
 953                                        btf_ext_data->d_size);
 954                if (IS_ERR(*btf_ext))
 955                        goto done;
 956        } else if (btf_ext) {
 957                *btf_ext = NULL;
 958        }
 959done:
 960        if (elf)
 961                elf_end(elf);
 962        close(fd);
 963
 964        if (err)
 965                return ERR_PTR(err);
 966        /*
 967         * btf is always parsed before btf_ext, so no need to clean up
 968         * btf_ext, if btf loading failed
 969         */
 970        if (IS_ERR(btf))
 971                return btf;
 972        if (btf_ext && IS_ERR(*btf_ext)) {
 973                btf__free(btf);
 974                err = PTR_ERR(*btf_ext);
 975                return ERR_PTR(err);
 976        }
 977        return btf;
 978}
 979
 980struct btf *btf__parse_elf(const char *path, struct btf_ext **btf_ext)
 981{
 982        return btf_parse_elf(path, NULL, btf_ext);
 983}
 984
 985struct btf *btf__parse_elf_split(const char *path, struct btf *base_btf)
 986{
 987        return btf_parse_elf(path, base_btf, NULL);
 988}
 989
 990static struct btf *btf_parse_raw(const char *path, struct btf *base_btf)
 991{
 992        struct btf *btf = NULL;
 993        void *data = NULL;
 994        FILE *f = NULL;
 995        __u16 magic;
 996        int err = 0;
 997        long sz;
 998
 999        f = fopen(path, "rb");
1000        if (!f) {
1001                err = -errno;
1002                goto err_out;
1003        }
1004
1005        /* check BTF magic */
1006        if (fread(&magic, 1, sizeof(magic), f) < sizeof(magic)) {
1007                err = -EIO;
1008                goto err_out;
1009        }
1010        if (magic == __bswap_16(BTF_MAGIC)) {
1011                /* non-native endian raw BTF */
1012                pr_warn("non-native BTF endianness is not supported\n");
1013                err = -LIBBPF_ERRNO__ENDIAN;
1014                goto err_out;
1015        }
1016        if (magic != BTF_MAGIC && magic != bswap_16(BTF_MAGIC)) {
1017                /* definitely not a raw BTF */
1018                err = -EPROTO;
1019                goto err_out;
1020        }
1021
1022        /* get file size */
1023        if (fseek(f, 0, SEEK_END)) {
1024                err = -errno;
1025                goto err_out;
1026        }
1027        sz = ftell(f);
1028        if (sz < 0) {
1029                err = -errno;
1030                goto err_out;
1031        }
1032        /* rewind to the start */
1033        if (fseek(f, 0, SEEK_SET)) {
1034                err = -errno;
1035                goto err_out;
1036        }
1037
1038        /* pre-alloc memory and read all of BTF data */
1039        data = malloc(sz);
1040        if (!data) {
1041                err = -ENOMEM;
1042                goto err_out;
1043        }
1044        if (fread(data, 1, sz, f) < sz) {
1045                err = -EIO;
1046                goto err_out;
1047        }
1048
1049        /* finally parse BTF data */
1050        btf = btf_new(data, sz, base_btf);
1051
1052err_out:
1053        free(data);
1054        if (f)
1055                fclose(f);
1056        return err ? ERR_PTR(err) : btf;
1057}
1058
1059struct btf *btf__parse_raw(const char *path)
1060{
1061        return btf_parse_raw(path, NULL);
1062}
1063
1064struct btf *btf__parse_raw_split(const char *path, struct btf *base_btf)
1065{
1066        return btf_parse_raw(path, base_btf);
1067}
1068
1069static struct btf *btf_parse(const char *path, struct btf *base_btf, struct btf_ext **btf_ext)
1070{
1071        struct btf *btf;
1072
1073        if (btf_ext)
1074                *btf_ext = NULL;
1075
1076        btf = btf_parse_raw(path, base_btf);
1077        if (!IS_ERR(btf) || PTR_ERR(btf) != -EPROTO)
1078                return btf;
1079
1080        return btf_parse_elf(path, base_btf, btf_ext);
1081}
1082
1083struct btf *btf__parse(const char *path, struct btf_ext **btf_ext)
1084{
1085        return btf_parse(path, NULL, btf_ext);
1086}
1087
1088struct btf *btf__parse_split(const char *path, struct btf *base_btf)
1089{
1090        return btf_parse(path, base_btf, NULL);
1091}
1092
1093static int compare_vsi_off(const void *_a, const void *_b)
1094{
1095        const struct btf_var_secinfo *a = _a;
1096        const struct btf_var_secinfo *b = _b;
1097
1098        return a->offset - b->offset;
1099}
1100
1101static int btf_fixup_datasec(struct bpf_object *obj, struct btf *btf,
1102                             struct btf_type *t)
1103{
1104        __u32 size = 0, off = 0, i, vars = btf_vlen(t);
1105        const char *name = btf__name_by_offset(btf, t->name_off);
1106        const struct btf_type *t_var;
1107        struct btf_var_secinfo *vsi;
1108        const struct btf_var *var;
1109        int ret;
1110
1111        if (!name) {
1112                pr_debug("No name found in string section for DATASEC kind.\n");
1113                return -ENOENT;
1114        }
1115
1116        /* .extern datasec size and var offsets were set correctly during
1117         * extern collection step, so just skip straight to sorting variables
1118         */
1119        if (t->size)
1120                goto sort_vars;
1121
1122        ret = bpf_object__section_size(obj, name, &size);
1123        if (ret || !size || (t->size && t->size != size)) {
1124                pr_debug("Invalid size for section %s: %u bytes\n", name, size);
1125                return -ENOENT;
1126        }
1127
1128        t->size = size;
1129
1130        for (i = 0, vsi = btf_var_secinfos(t); i < vars; i++, vsi++) {
1131                t_var = btf__type_by_id(btf, vsi->type);
1132                var = btf_var(t_var);
1133
1134                if (!btf_is_var(t_var)) {
1135                        pr_debug("Non-VAR type seen in section %s\n", name);
1136                        return -EINVAL;
1137                }
1138
1139                if (var->linkage == BTF_VAR_STATIC)
1140                        continue;
1141
1142                name = btf__name_by_offset(btf, t_var->name_off);
1143                if (!name) {
1144                        pr_debug("No name found in string section for VAR kind\n");
1145                        return -ENOENT;
1146                }
1147
1148                ret = bpf_object__variable_offset(obj, name, &off);
1149                if (ret) {
1150                        pr_debug("No offset found in symbol table for VAR %s\n",
1151                                 name);
1152                        return -ENOENT;
1153                }
1154
1155                vsi->offset = off;
1156        }
1157
1158sort_vars:
1159        qsort(btf_var_secinfos(t), vars, sizeof(*vsi), compare_vsi_off);
1160        return 0;
1161}
1162
1163int btf__finalize_data(struct bpf_object *obj, struct btf *btf)
1164{
1165        int err = 0;
1166        __u32 i;
1167
1168        for (i = 1; i <= btf->nr_types; i++) {
1169                struct btf_type *t = btf_type_by_id(btf, i);
1170
1171                /* Loader needs to fix up some of the things compiler
1172                 * couldn't get its hands on while emitting BTF. This
1173                 * is section size and global variable offset. We use
1174                 * the info from the ELF itself for this purpose.
1175                 */
1176                if (btf_is_datasec(t)) {
1177                        err = btf_fixup_datasec(obj, btf, t);
1178                        if (err)
1179                                break;
1180                }
1181        }
1182
1183        return err;
1184}
1185
1186static void *btf_get_raw_data(const struct btf *btf, __u32 *size, bool swap_endian);
1187
1188int btf__load(struct btf *btf)
1189{
1190        __u32 log_buf_size = 0, raw_size;
1191        char *log_buf = NULL;
1192        void *raw_data;
1193        int err = 0;
1194
1195        if (btf->fd >= 0)
1196                return -EEXIST;
1197
1198retry_load:
1199        if (log_buf_size) {
1200                log_buf = malloc(log_buf_size);
1201                if (!log_buf)
1202                        return -ENOMEM;
1203
1204                *log_buf = 0;
1205        }
1206
1207        raw_data = btf_get_raw_data(btf, &raw_size, false);
1208        if (!raw_data) {
1209                err = -ENOMEM;
1210                goto done;
1211        }
1212        /* cache native raw data representation */
1213        btf->raw_size = raw_size;
1214        btf->raw_data = raw_data;
1215
1216        btf->fd = bpf_load_btf(raw_data, raw_size, log_buf, log_buf_size, false);
1217        if (btf->fd < 0) {
1218                if (!log_buf || errno == ENOSPC) {
1219                        log_buf_size = max((__u32)BPF_LOG_BUF_SIZE,
1220                                           log_buf_size << 1);
1221                        free(log_buf);
1222                        goto retry_load;
1223                }
1224
1225                err = -errno;
1226                pr_warn("Error loading BTF: %s(%d)\n", strerror(errno), errno);
1227                if (*log_buf)
1228                        pr_warn("%s\n", log_buf);
1229                goto done;
1230        }
1231
1232done:
1233        free(log_buf);
1234        return err;
1235}
1236
1237int btf__fd(const struct btf *btf)
1238{
1239        return btf->fd;
1240}
1241
1242void btf__set_fd(struct btf *btf, int fd)
1243{
1244        btf->fd = fd;
1245}
1246
1247static void *btf_get_raw_data(const struct btf *btf, __u32 *size, bool swap_endian)
1248{
1249        struct btf_header *hdr = btf->hdr;
1250        struct btf_type *t;
1251        void *data, *p;
1252        __u32 data_sz;
1253        int i;
1254
1255        data = swap_endian ? btf->raw_data_swapped : btf->raw_data;
1256        if (data) {
1257                *size = btf->raw_size;
1258                return data;
1259        }
1260
1261        data_sz = hdr->hdr_len + hdr->type_len + hdr->str_len;
1262        data = calloc(1, data_sz);
1263        if (!data)
1264                return NULL;
1265        p = data;
1266
1267        memcpy(p, hdr, hdr->hdr_len);
1268        if (swap_endian)
1269                btf_bswap_hdr(p);
1270        p += hdr->hdr_len;
1271
1272        memcpy(p, btf->types_data, hdr->type_len);
1273        if (swap_endian) {
1274                for (i = 0; i < btf->nr_types; i++) {
1275                        t = p + btf->type_offs[i];
1276                        /* btf_bswap_type_rest() relies on native t->info, so
1277                         * we swap base type info after we swapped all the
1278                         * additional information
1279                         */
1280                        if (btf_bswap_type_rest(t))
1281                                goto err_out;
1282                        btf_bswap_type_base(t);
1283                }
1284        }
1285        p += hdr->type_len;
1286
1287        memcpy(p, btf->strs_data, hdr->str_len);
1288        p += hdr->str_len;
1289
1290        *size = data_sz;
1291        return data;
1292err_out:
1293        free(data);
1294        return NULL;
1295}
1296
1297const void *btf__get_raw_data(const struct btf *btf_ro, __u32 *size)
1298{
1299        struct btf *btf = (struct btf *)btf_ro;
1300        __u32 data_sz;
1301        void *data;
1302
1303        data = btf_get_raw_data(btf, &data_sz, btf->swapped_endian);
1304        if (!data)
1305                return NULL;
1306
1307        btf->raw_size = data_sz;
1308        if (btf->swapped_endian)
1309                btf->raw_data_swapped = data;
1310        else
1311                btf->raw_data = data;
1312        *size = data_sz;
1313        return data;
1314}
1315
1316const char *btf__str_by_offset(const struct btf *btf, __u32 offset)
1317{
1318        if (offset < btf->start_str_off)
1319                return btf__str_by_offset(btf->base_btf, offset);
1320        else if (offset - btf->start_str_off < btf->hdr->str_len)
1321                return btf->strs_data + (offset - btf->start_str_off);
1322        else
1323                return NULL;
1324}
1325
1326const char *btf__name_by_offset(const struct btf *btf, __u32 offset)
1327{
1328        return btf__str_by_offset(btf, offset);
1329}
1330
1331struct btf *btf_get_from_fd(int btf_fd, struct btf *base_btf)
1332{
1333        struct bpf_btf_info btf_info;
1334        __u32 len = sizeof(btf_info);
1335        __u32 last_size;
1336        struct btf *btf;
1337        void *ptr;
1338        int err;
1339
1340        /* we won't know btf_size until we call bpf_obj_get_info_by_fd(). so
1341         * let's start with a sane default - 4KiB here - and resize it only if
1342         * bpf_obj_get_info_by_fd() needs a bigger buffer.
1343         */
1344        last_size = 4096;
1345        ptr = malloc(last_size);
1346        if (!ptr)
1347                return ERR_PTR(-ENOMEM);
1348
1349        memset(&btf_info, 0, sizeof(btf_info));
1350        btf_info.btf = ptr_to_u64(ptr);
1351        btf_info.btf_size = last_size;
1352        err = bpf_obj_get_info_by_fd(btf_fd, &btf_info, &len);
1353
1354        if (!err && btf_info.btf_size > last_size) {
1355                void *temp_ptr;
1356
1357                last_size = btf_info.btf_size;
1358                temp_ptr = realloc(ptr, last_size);
1359                if (!temp_ptr) {
1360                        btf = ERR_PTR(-ENOMEM);
1361                        goto exit_free;
1362                }
1363                ptr = temp_ptr;
1364
1365                len = sizeof(btf_info);
1366                memset(&btf_info, 0, sizeof(btf_info));
1367                btf_info.btf = ptr_to_u64(ptr);
1368                btf_info.btf_size = last_size;
1369
1370                err = bpf_obj_get_info_by_fd(btf_fd, &btf_info, &len);
1371        }
1372
1373        if (err || btf_info.btf_size > last_size) {
1374                btf = err ? ERR_PTR(-errno) : ERR_PTR(-E2BIG);
1375                goto exit_free;
1376        }
1377
1378        btf = btf_new(ptr, btf_info.btf_size, base_btf);
1379
1380exit_free:
1381        free(ptr);
1382        return btf;
1383}
1384
1385int btf__get_from_id(__u32 id, struct btf **btf)
1386{
1387        struct btf *res;
1388        int btf_fd;
1389
1390        *btf = NULL;
1391        btf_fd = bpf_btf_get_fd_by_id(id);
1392        if (btf_fd < 0)
1393                return -errno;
1394
1395        res = btf_get_from_fd(btf_fd, NULL);
1396        close(btf_fd);
1397        if (IS_ERR(res))
1398                return PTR_ERR(res);
1399
1400        *btf = res;
1401        return 0;
1402}
1403
1404int btf__get_map_kv_tids(const struct btf *btf, const char *map_name,
1405                         __u32 expected_key_size, __u32 expected_value_size,
1406                         __u32 *key_type_id, __u32 *value_type_id)
1407{
1408        const struct btf_type *container_type;
1409        const struct btf_member *key, *value;
1410        const size_t max_name = 256;
1411        char container_name[max_name];
1412        __s64 key_size, value_size;
1413        __s32 container_id;
1414
1415        if (snprintf(container_name, max_name, "____btf_map_%s", map_name) ==
1416            max_name) {
1417                pr_warn("map:%s length of '____btf_map_%s' is too long\n",
1418                        map_name, map_name);
1419                return -EINVAL;
1420        }
1421
1422        container_id = btf__find_by_name(btf, container_name);
1423        if (container_id < 0) {
1424                pr_debug("map:%s container_name:%s cannot be found in BTF. Missing BPF_ANNOTATE_KV_PAIR?\n",
1425                         map_name, container_name);
1426                return container_id;
1427        }
1428
1429        container_type = btf__type_by_id(btf, container_id);
1430        if (!container_type) {
1431                pr_warn("map:%s cannot find BTF type for container_id:%u\n",
1432                        map_name, container_id);
1433                return -EINVAL;
1434        }
1435
1436        if (!btf_is_struct(container_type) || btf_vlen(container_type) < 2) {
1437                pr_warn("map:%s container_name:%s is an invalid container struct\n",
1438                        map_name, container_name);
1439                return -EINVAL;
1440        }
1441
1442        key = btf_members(container_type);
1443        value = key + 1;
1444
1445        key_size = btf__resolve_size(btf, key->type);
1446        if (key_size < 0) {
1447                pr_warn("map:%s invalid BTF key_type_size\n", map_name);
1448                return key_size;
1449        }
1450
1451        if (expected_key_size != key_size) {
1452                pr_warn("map:%s btf_key_type_size:%u != map_def_key_size:%u\n",
1453                        map_name, (__u32)key_size, expected_key_size);
1454                return -EINVAL;
1455        }
1456
1457        value_size = btf__resolve_size(btf, value->type);
1458        if (value_size < 0) {
1459                pr_warn("map:%s invalid BTF value_type_size\n", map_name);
1460                return value_size;
1461        }
1462
1463        if (expected_value_size != value_size) {
1464                pr_warn("map:%s btf_value_type_size:%u != map_def_value_size:%u\n",
1465                        map_name, (__u32)value_size, expected_value_size);
1466                return -EINVAL;
1467        }
1468
1469        *key_type_id = key->type;
1470        *value_type_id = value->type;
1471
1472        return 0;
1473}
1474
1475static size_t strs_hash_fn(const void *key, void *ctx)
1476{
1477        const struct btf *btf = ctx;
1478        const char *strs = *btf->strs_data_ptr;
1479        const char *str = strs + (long)key;
1480
1481        return str_hash(str);
1482}
1483
1484static bool strs_hash_equal_fn(const void *key1, const void *key2, void *ctx)
1485{
1486        const struct btf *btf = ctx;
1487        const char *strs = *btf->strs_data_ptr;
1488        const char *str1 = strs + (long)key1;
1489        const char *str2 = strs + (long)key2;
1490
1491        return strcmp(str1, str2) == 0;
1492}
1493
1494static void btf_invalidate_raw_data(struct btf *btf)
1495{
1496        if (btf->raw_data) {
1497                free(btf->raw_data);
1498                btf->raw_data = NULL;
1499        }
1500        if (btf->raw_data_swapped) {
1501                free(btf->raw_data_swapped);
1502                btf->raw_data_swapped = NULL;
1503        }
1504}
1505
1506/* Ensure BTF is ready to be modified (by splitting into a three memory
1507 * regions for header, types, and strings). Also invalidate cached
1508 * raw_data, if any.
1509 */
1510static int btf_ensure_modifiable(struct btf *btf)
1511{
1512        void *hdr, *types, *strs, *strs_end, *s;
1513        struct hashmap *hash = NULL;
1514        long off;
1515        int err;
1516
1517        if (btf_is_modifiable(btf)) {
1518                /* any BTF modification invalidates raw_data */
1519                btf_invalidate_raw_data(btf);
1520                return 0;
1521        }
1522
1523        /* split raw data into three memory regions */
1524        hdr = malloc(btf->hdr->hdr_len);
1525        types = malloc(btf->hdr->type_len);
1526        strs = malloc(btf->hdr->str_len);
1527        if (!hdr || !types || !strs)
1528                goto err_out;
1529
1530        memcpy(hdr, btf->hdr, btf->hdr->hdr_len);
1531        memcpy(types, btf->types_data, btf->hdr->type_len);
1532        memcpy(strs, btf->strs_data, btf->hdr->str_len);
1533
1534        /* make hashmap below use btf->strs_data as a source of strings */
1535        btf->strs_data_ptr = &btf->strs_data;
1536
1537        /* build lookup index for all strings */
1538        hash = hashmap__new(strs_hash_fn, strs_hash_equal_fn, btf);
1539        if (IS_ERR(hash)) {
1540                err = PTR_ERR(hash);
1541                hash = NULL;
1542                goto err_out;
1543        }
1544
1545        strs_end = strs + btf->hdr->str_len;
1546        for (off = 0, s = strs; s < strs_end; off += strlen(s) + 1, s = strs + off) {
1547                /* hashmap__add() returns EEXIST if string with the same
1548                 * content already is in the hash map
1549                 */
1550                err = hashmap__add(hash, (void *)off, (void *)off);
1551                if (err == -EEXIST)
1552                        continue; /* duplicate */
1553                if (err)
1554                        goto err_out;
1555        }
1556
1557        /* only when everything was successful, update internal state */
1558        btf->hdr = hdr;
1559        btf->types_data = types;
1560        btf->types_data_cap = btf->hdr->type_len;
1561        btf->strs_data = strs;
1562        btf->strs_data_cap = btf->hdr->str_len;
1563        btf->strs_hash = hash;
1564        /* if BTF was created from scratch, all strings are guaranteed to be
1565         * unique and deduplicated
1566         */
1567        if (btf->hdr->str_len == 0)
1568                btf->strs_deduped = true;
1569        if (!btf->base_btf && btf->hdr->str_len == 1)
1570                btf->strs_deduped = true;
1571
1572        /* invalidate raw_data representation */
1573        btf_invalidate_raw_data(btf);
1574
1575        return 0;
1576
1577err_out:
1578        hashmap__free(hash);
1579        free(hdr);
1580        free(types);
1581        free(strs);
1582        return -ENOMEM;
1583}
1584
1585static void *btf_add_str_mem(struct btf *btf, size_t add_sz)
1586{
1587        return btf_add_mem(&btf->strs_data, &btf->strs_data_cap, 1,
1588                           btf->hdr->str_len, BTF_MAX_STR_OFFSET, add_sz);
1589}
1590
1591/* Find an offset in BTF string section that corresponds to a given string *s*.
1592 * Returns:
1593 *   - >0 offset into string section, if string is found;
1594 *   - -ENOENT, if string is not in the string section;
1595 *   - <0, on any other error.
1596 */
1597int btf__find_str(struct btf *btf, const char *s)
1598{
1599        long old_off, new_off, len;
1600        void *p;
1601
1602        if (btf->base_btf) {
1603                int ret;
1604
1605                ret = btf__find_str(btf->base_btf, s);
1606                if (ret != -ENOENT)
1607                        return ret;
1608        }
1609
1610        /* BTF needs to be in a modifiable state to build string lookup index */
1611        if (btf_ensure_modifiable(btf))
1612                return -ENOMEM;
1613
1614        /* see btf__add_str() for why we do this */
1615        len = strlen(s) + 1;
1616        p = btf_add_str_mem(btf, len);
1617        if (!p)
1618                return -ENOMEM;
1619
1620        new_off = btf->hdr->str_len;
1621        memcpy(p, s, len);
1622
1623        if (hashmap__find(btf->strs_hash, (void *)new_off, (void **)&old_off))
1624                return btf->start_str_off + old_off;
1625
1626        return -ENOENT;
1627}
1628
1629/* Add a string s to the BTF string section.
1630 * Returns:
1631 *   - > 0 offset into string section, on success;
1632 *   - < 0, on error.
1633 */
1634int btf__add_str(struct btf *btf, const char *s)
1635{
1636        long old_off, new_off, len;
1637        void *p;
1638        int err;
1639
1640        if (btf->base_btf) {
1641                int ret;
1642
1643                ret = btf__find_str(btf->base_btf, s);
1644                if (ret != -ENOENT)
1645                        return ret;
1646        }
1647
1648        if (btf_ensure_modifiable(btf))
1649                return -ENOMEM;
1650
1651        /* Hashmap keys are always offsets within btf->strs_data, so to even
1652         * look up some string from the "outside", we need to first append it
1653         * at the end, so that it can be addressed with an offset. Luckily,
1654         * until btf->hdr->str_len is incremented, that string is just a piece
1655         * of garbage for the rest of BTF code, so no harm, no foul. On the
1656         * other hand, if the string is unique, it's already appended and
1657         * ready to be used, only a simple btf->hdr->str_len increment away.
1658         */
1659        len = strlen(s) + 1;
1660        p = btf_add_str_mem(btf, len);
1661        if (!p)
1662                return -ENOMEM;
1663
1664        new_off = btf->hdr->str_len;
1665        memcpy(p, s, len);
1666
1667        /* Now attempt to add the string, but only if the string with the same
1668         * contents doesn't exist already (HASHMAP_ADD strategy). If such
1669         * string exists, we'll get its offset in old_off (that's old_key).
1670         */
1671        err = hashmap__insert(btf->strs_hash, (void *)new_off, (void *)new_off,
1672                              HASHMAP_ADD, (const void **)&old_off, NULL);
1673        if (err == -EEXIST)
1674                return btf->start_str_off + old_off; /* duplicated string, return existing offset */
1675        if (err)
1676                return err;
1677
1678        btf->hdr->str_len += len; /* new unique string, adjust data length */
1679        return btf->start_str_off + new_off;
1680}
1681
1682static void *btf_add_type_mem(struct btf *btf, size_t add_sz)
1683{
1684        return btf_add_mem(&btf->types_data, &btf->types_data_cap, 1,
1685                           btf->hdr->type_len, UINT_MAX, add_sz);
1686}
1687
1688static __u32 btf_type_info(int kind, int vlen, int kflag)
1689{
1690        return (kflag << 31) | (kind << 24) | vlen;
1691}
1692
1693static void btf_type_inc_vlen(struct btf_type *t)
1694{
1695        t->info = btf_type_info(btf_kind(t), btf_vlen(t) + 1, btf_kflag(t));
1696}
1697
1698static int btf_commit_type(struct btf *btf, int data_sz)
1699{
1700        int err;
1701
1702        err = btf_add_type_idx_entry(btf, btf->hdr->type_len);
1703        if (err)
1704                return err;
1705
1706        btf->hdr->type_len += data_sz;
1707        btf->hdr->str_off += data_sz;
1708        btf->nr_types++;
1709        return btf->start_id + btf->nr_types - 1;
1710}
1711
1712/*
1713 * Append new BTF_KIND_INT type with:
1714 *   - *name* - non-empty, non-NULL type name;
1715 *   - *sz* - power-of-2 (1, 2, 4, ..) size of the type, in bytes;
1716 *   - encoding is a combination of BTF_INT_SIGNED, BTF_INT_CHAR, BTF_INT_BOOL.
1717 * Returns:
1718 *   - >0, type ID of newly added BTF type;
1719 *   - <0, on error.
1720 */
1721int btf__add_int(struct btf *btf, const char *name, size_t byte_sz, int encoding)
1722{
1723        struct btf_type *t;
1724        int sz, name_off;
1725
1726        /* non-empty name */
1727        if (!name || !name[0])
1728                return -EINVAL;
1729        /* byte_sz must be power of 2 */
1730        if (!byte_sz || (byte_sz & (byte_sz - 1)) || byte_sz > 16)
1731                return -EINVAL;
1732        if (encoding & ~(BTF_INT_SIGNED | BTF_INT_CHAR | BTF_INT_BOOL))
1733                return -EINVAL;
1734
1735        /* deconstruct BTF, if necessary, and invalidate raw_data */
1736        if (btf_ensure_modifiable(btf))
1737                return -ENOMEM;
1738
1739        sz = sizeof(struct btf_type) + sizeof(int);
1740        t = btf_add_type_mem(btf, sz);
1741        if (!t)
1742                return -ENOMEM;
1743
1744        /* if something goes wrong later, we might end up with an extra string,
1745         * but that shouldn't be a problem, because BTF can't be constructed
1746         * completely anyway and will most probably be just discarded
1747         */
1748        name_off = btf__add_str(btf, name);
1749        if (name_off < 0)
1750                return name_off;
1751
1752        t->name_off = name_off;
1753        t->info = btf_type_info(BTF_KIND_INT, 0, 0);
1754        t->size = byte_sz;
1755        /* set INT info, we don't allow setting legacy bit offset/size */
1756        *(__u32 *)(t + 1) = (encoding << 24) | (byte_sz * 8);
1757
1758        return btf_commit_type(btf, sz);
1759}
1760
1761/*
1762 * Append new BTF_KIND_FLOAT type with:
1763 *   - *name* - non-empty, non-NULL type name;
1764 *   - *sz* - size of the type, in bytes;
1765 * Returns:
1766 *   - >0, type ID of newly added BTF type;
1767 *   - <0, on error.
1768 */
1769int btf__add_float(struct btf *btf, const char *name, size_t byte_sz)
1770{
1771        struct btf_type *t;
1772        int sz, name_off;
1773
1774        /* non-empty name */
1775        if (!name || !name[0])
1776                return -EINVAL;
1777
1778        /* byte_sz must be one of the explicitly allowed values */
1779        if (byte_sz != 2 && byte_sz != 4 && byte_sz != 8 && byte_sz != 12 &&
1780            byte_sz != 16)
1781                return -EINVAL;
1782
1783        if (btf_ensure_modifiable(btf))
1784                return -ENOMEM;
1785
1786        sz = sizeof(struct btf_type);
1787        t = btf_add_type_mem(btf, sz);
1788        if (!t)
1789                return -ENOMEM;
1790
1791        name_off = btf__add_str(btf, name);
1792        if (name_off < 0)
1793                return name_off;
1794
1795        t->name_off = name_off;
1796        t->info = btf_type_info(BTF_KIND_FLOAT, 0, 0);
1797        t->size = byte_sz;
1798
1799        return btf_commit_type(btf, sz);
1800}
1801
1802/* it's completely legal to append BTF types with type IDs pointing forward to
1803 * types that haven't been appended yet, so we only make sure that id looks
1804 * sane, we can't guarantee that ID will always be valid
1805 */
1806static int validate_type_id(int id)
1807{
1808        if (id < 0 || id > BTF_MAX_NR_TYPES)
1809                return -EINVAL;
1810        return 0;
1811}
1812
1813/* generic append function for PTR, TYPEDEF, CONST/VOLATILE/RESTRICT */
1814static int btf_add_ref_kind(struct btf *btf, int kind, const char *name, int ref_type_id)
1815{
1816        struct btf_type *t;
1817        int sz, name_off = 0;
1818
1819        if (validate_type_id(ref_type_id))
1820                return -EINVAL;
1821
1822        if (btf_ensure_modifiable(btf))
1823                return -ENOMEM;
1824
1825        sz = sizeof(struct btf_type);
1826        t = btf_add_type_mem(btf, sz);
1827        if (!t)
1828                return -ENOMEM;
1829
1830        if (name && name[0]) {
1831                name_off = btf__add_str(btf, name);
1832                if (name_off < 0)
1833                        return name_off;
1834        }
1835
1836        t->name_off = name_off;
1837        t->info = btf_type_info(kind, 0, 0);
1838        t->type = ref_type_id;
1839
1840        return btf_commit_type(btf, sz);
1841}
1842
1843/*
1844 * Append new BTF_KIND_PTR type with:
1845 *   - *ref_type_id* - referenced type ID, it might not exist yet;
1846 * Returns:
1847 *   - >0, type ID of newly added BTF type;
1848 *   - <0, on error.
1849 */
1850int btf__add_ptr(struct btf *btf, int ref_type_id)
1851{
1852        return btf_add_ref_kind(btf, BTF_KIND_PTR, NULL, ref_type_id);
1853}
1854
1855/*
1856 * Append new BTF_KIND_ARRAY type with:
1857 *   - *index_type_id* - type ID of the type describing array index;
1858 *   - *elem_type_id* - type ID of the type describing array element;
1859 *   - *nr_elems* - the size of the array;
1860 * Returns:
1861 *   - >0, type ID of newly added BTF type;
1862 *   - <0, on error.
1863 */
1864int btf__add_array(struct btf *btf, int index_type_id, int elem_type_id, __u32 nr_elems)
1865{
1866        struct btf_type *t;
1867        struct btf_array *a;
1868        int sz;
1869
1870        if (validate_type_id(index_type_id) || validate_type_id(elem_type_id))
1871                return -EINVAL;
1872
1873        if (btf_ensure_modifiable(btf))
1874                return -ENOMEM;
1875
1876        sz = sizeof(struct btf_type) + sizeof(struct btf_array);
1877        t = btf_add_type_mem(btf, sz);
1878        if (!t)
1879                return -ENOMEM;
1880
1881        t->name_off = 0;
1882        t->info = btf_type_info(BTF_KIND_ARRAY, 0, 0);
1883        t->size = 0;
1884
1885        a = btf_array(t);
1886        a->type = elem_type_id;
1887        a->index_type = index_type_id;
1888        a->nelems = nr_elems;
1889
1890        return btf_commit_type(btf, sz);
1891}
1892
1893/* generic STRUCT/UNION append function */
1894static int btf_add_composite(struct btf *btf, int kind, const char *name, __u32 bytes_sz)
1895{
1896        struct btf_type *t;
1897        int sz, name_off = 0;
1898
1899        if (btf_ensure_modifiable(btf))
1900                return -ENOMEM;
1901
1902        sz = sizeof(struct btf_type);
1903        t = btf_add_type_mem(btf, sz);
1904        if (!t)
1905                return -ENOMEM;
1906
1907        if (name && name[0]) {
1908                name_off = btf__add_str(btf, name);
1909                if (name_off < 0)
1910                        return name_off;
1911        }
1912
1913        /* start out with vlen=0 and no kflag; this will be adjusted when
1914         * adding each member
1915         */
1916        t->name_off = name_off;
1917        t->info = btf_type_info(kind, 0, 0);
1918        t->size = bytes_sz;
1919
1920        return btf_commit_type(btf, sz);
1921}
1922
1923/*
1924 * Append new BTF_KIND_STRUCT type with:
1925 *   - *name* - name of the struct, can be NULL or empty for anonymous structs;
1926 *   - *byte_sz* - size of the struct, in bytes;
1927 *
1928 * Struct initially has no fields in it. Fields can be added by
1929 * btf__add_field() right after btf__add_struct() succeeds. 
1930 *
1931 * Returns:
1932 *   - >0, type ID of newly added BTF type;
1933 *   - <0, on error.
1934 */
1935int btf__add_struct(struct btf *btf, const char *name, __u32 byte_sz)
1936{
1937        return btf_add_composite(btf, BTF_KIND_STRUCT, name, byte_sz);
1938}
1939
1940/*
1941 * Append new BTF_KIND_UNION type with:
1942 *   - *name* - name of the union, can be NULL or empty for anonymous union;
1943 *   - *byte_sz* - size of the union, in bytes;
1944 *
1945 * Union initially has no fields in it. Fields can be added by
1946 * btf__add_field() right after btf__add_union() succeeds. All fields
1947 * should have *bit_offset* of 0.
1948 *
1949 * Returns:
1950 *   - >0, type ID of newly added BTF type;
1951 *   - <0, on error.
1952 */
1953int btf__add_union(struct btf *btf, const char *name, __u32 byte_sz)
1954{
1955        return btf_add_composite(btf, BTF_KIND_UNION, name, byte_sz);
1956}
1957
1958static struct btf_type *btf_last_type(struct btf *btf)
1959{
1960        return btf_type_by_id(btf, btf__get_nr_types(btf));
1961}
1962
1963/*
1964 * Append new field for the current STRUCT/UNION type with:
1965 *   - *name* - name of the field, can be NULL or empty for anonymous field;
1966 *   - *type_id* - type ID for the type describing field type;
1967 *   - *bit_offset* - bit offset of the start of the field within struct/union;
1968 *   - *bit_size* - bit size of a bitfield, 0 for non-bitfield fields;
1969 * Returns:
1970 *   -  0, on success;
1971 *   - <0, on error.
1972 */
1973int btf__add_field(struct btf *btf, const char *name, int type_id,
1974                   __u32 bit_offset, __u32 bit_size)
1975{
1976        struct btf_type *t;
1977        struct btf_member *m;
1978        bool is_bitfield;
1979        int sz, name_off = 0;
1980
1981        /* last type should be union/struct */
1982        if (btf->nr_types == 0)
1983                return -EINVAL;
1984        t = btf_last_type(btf);
1985        if (!btf_is_composite(t))
1986                return -EINVAL;
1987
1988        if (validate_type_id(type_id))
1989                return -EINVAL;
1990        /* best-effort bit field offset/size enforcement */
1991        is_bitfield = bit_size || (bit_offset % 8 != 0);
1992        if (is_bitfield && (bit_size == 0 || bit_size > 255 || bit_offset > 0xffffff))
1993                return -EINVAL;
1994
1995        /* only offset 0 is allowed for unions */
1996        if (btf_is_union(t) && bit_offset)
1997                return -EINVAL;
1998
1999        /* decompose and invalidate raw data */
2000        if (btf_ensure_modifiable(btf))
2001                return -ENOMEM;
2002
2003        sz = sizeof(struct btf_member);
2004        m = btf_add_type_mem(btf, sz);
2005        if (!m)
2006                return -ENOMEM;
2007
2008        if (name && name[0]) {
2009                name_off = btf__add_str(btf, name);
2010                if (name_off < 0)
2011                        return name_off;
2012        }
2013
2014        m->name_off = name_off;
2015        m->type = type_id;
2016        m->offset = bit_offset | (bit_size << 24);
2017
2018        /* btf_add_type_mem can invalidate t pointer */
2019        t = btf_last_type(btf);
2020        /* update parent type's vlen and kflag */
2021        t->info = btf_type_info(btf_kind(t), btf_vlen(t) + 1, is_bitfield || btf_kflag(t));
2022
2023        btf->hdr->type_len += sz;
2024        btf->hdr->str_off += sz;
2025        return 0;
2026}
2027
2028/*
2029 * Append new BTF_KIND_ENUM type with:
2030 *   - *name* - name of the enum, can be NULL or empty for anonymous enums;
2031 *   - *byte_sz* - size of the enum, in bytes.
2032 *
2033 * Enum initially has no enum values in it (and corresponds to enum forward
2034 * declaration). Enumerator values can be added by btf__add_enum_value()
2035 * immediately after btf__add_enum() succeeds.
2036 *
2037 * Returns:
2038 *   - >0, type ID of newly added BTF type;
2039 *   - <0, on error.
2040 */
2041int btf__add_enum(struct btf *btf, const char *name, __u32 byte_sz)
2042{
2043        struct btf_type *t;
2044        int sz, name_off = 0;
2045
2046        /* byte_sz must be power of 2 */
2047        if (!byte_sz || (byte_sz & (byte_sz - 1)) || byte_sz > 8)
2048                return -EINVAL;
2049
2050        if (btf_ensure_modifiable(btf))
2051                return -ENOMEM;
2052
2053        sz = sizeof(struct btf_type);
2054        t = btf_add_type_mem(btf, sz);
2055        if (!t)
2056                return -ENOMEM;
2057
2058        if (name && name[0]) {
2059                name_off = btf__add_str(btf, name);
2060                if (name_off < 0)
2061                        return name_off;
2062        }
2063
2064        /* start out with vlen=0; it will be adjusted when adding enum values */
2065        t->name_off = name_off;
2066        t->info = btf_type_info(BTF_KIND_ENUM, 0, 0);
2067        t->size = byte_sz;
2068
2069        return btf_commit_type(btf, sz);
2070}
2071
2072/*
2073 * Append new enum value for the current ENUM type with:
2074 *   - *name* - name of the enumerator value, can't be NULL or empty;
2075 *   - *value* - integer value corresponding to enum value *name*;
2076 * Returns:
2077 *   -  0, on success;
2078 *   - <0, on error.
2079 */
2080int btf__add_enum_value(struct btf *btf, const char *name, __s64 value)
2081{
2082        struct btf_type *t;
2083        struct btf_enum *v;
2084        int sz, name_off;
2085
2086        /* last type should be BTF_KIND_ENUM */
2087        if (btf->nr_types == 0)
2088                return -EINVAL;
2089        t = btf_last_type(btf);
2090        if (!btf_is_enum(t))
2091                return -EINVAL;
2092
2093        /* non-empty name */
2094        if (!name || !name[0])
2095                return -EINVAL;
2096        if (value < INT_MIN || value > UINT_MAX)
2097                return -E2BIG;
2098
2099        /* decompose and invalidate raw data */
2100        if (btf_ensure_modifiable(btf))
2101                return -ENOMEM;
2102
2103        sz = sizeof(struct btf_enum);
2104        v = btf_add_type_mem(btf, sz);
2105        if (!v)
2106                return -ENOMEM;
2107
2108        name_off = btf__add_str(btf, name);
2109        if (name_off < 0)
2110                return name_off;
2111
2112        v->name_off = name_off;
2113        v->val = value;
2114
2115        /* update parent type's vlen */
2116        t = btf_last_type(btf);
2117        btf_type_inc_vlen(t);
2118
2119        btf->hdr->type_len += sz;
2120        btf->hdr->str_off += sz;
2121        return 0;
2122}
2123
2124/*
2125 * Append new BTF_KIND_FWD type with:
2126 *   - *name*, non-empty/non-NULL name;
2127 *   - *fwd_kind*, kind of forward declaration, one of BTF_FWD_STRUCT,
2128 *     BTF_FWD_UNION, or BTF_FWD_ENUM;
2129 * Returns:
2130 *   - >0, type ID of newly added BTF type;
2131 *   - <0, on error.
2132 */
2133int btf__add_fwd(struct btf *btf, const char *name, enum btf_fwd_kind fwd_kind)
2134{
2135        if (!name || !name[0])
2136                return -EINVAL;
2137
2138        switch (fwd_kind) {
2139        case BTF_FWD_STRUCT:
2140        case BTF_FWD_UNION: {
2141                struct btf_type *t;
2142                int id;
2143
2144                id = btf_add_ref_kind(btf, BTF_KIND_FWD, name, 0);
2145                if (id <= 0)
2146                        return id;
2147                t = btf_type_by_id(btf, id);
2148                t->info = btf_type_info(BTF_KIND_FWD, 0, fwd_kind == BTF_FWD_UNION);
2149                return id;
2150        }
2151        case BTF_FWD_ENUM:
2152                /* enum forward in BTF currently is just an enum with no enum
2153                 * values; we also assume a standard 4-byte size for it
2154                 */
2155                return btf__add_enum(btf, name, sizeof(int));
2156        default:
2157                return -EINVAL;
2158        }
2159}
2160
2161/*
2162 * Append new BTF_KING_TYPEDEF type with:
2163 *   - *name*, non-empty/non-NULL name;
2164 *   - *ref_type_id* - referenced type ID, it might not exist yet;
2165 * Returns:
2166 *   - >0, type ID of newly added BTF type;
2167 *   - <0, on error.
2168 */
2169int btf__add_typedef(struct btf *btf, const char *name, int ref_type_id)
2170{
2171        if (!name || !name[0])
2172                return -EINVAL;
2173
2174        return btf_add_ref_kind(btf, BTF_KIND_TYPEDEF, name, ref_type_id);
2175}
2176
2177/*
2178 * Append new BTF_KIND_VOLATILE type with:
2179 *   - *ref_type_id* - referenced type ID, it might not exist yet;
2180 * Returns:
2181 *   - >0, type ID of newly added BTF type;
2182 *   - <0, on error.
2183 */
2184int btf__add_volatile(struct btf *btf, int ref_type_id)
2185{
2186        return btf_add_ref_kind(btf, BTF_KIND_VOLATILE, NULL, ref_type_id);
2187}
2188
2189/*
2190 * Append new BTF_KIND_CONST type with:
2191 *   - *ref_type_id* - referenced type ID, it might not exist yet;
2192 * Returns:
2193 *   - >0, type ID of newly added BTF type;
2194 *   - <0, on error.
2195 */
2196int btf__add_const(struct btf *btf, int ref_type_id)
2197{
2198        return btf_add_ref_kind(btf, BTF_KIND_CONST, NULL, ref_type_id);
2199}
2200
2201/*
2202 * Append new BTF_KIND_RESTRICT type with:
2203 *   - *ref_type_id* - referenced type ID, it might not exist yet;
2204 * Returns:
2205 *   - >0, type ID of newly added BTF type;
2206 *   - <0, on error.
2207 */
2208int btf__add_restrict(struct btf *btf, int ref_type_id)
2209{
2210        return btf_add_ref_kind(btf, BTF_KIND_RESTRICT, NULL, ref_type_id);
2211}
2212
2213/*
2214 * Append new BTF_KIND_FUNC type with:
2215 *   - *name*, non-empty/non-NULL name;
2216 *   - *proto_type_id* - FUNC_PROTO's type ID, it might not exist yet;
2217 * Returns:
2218 *   - >0, type ID of newly added BTF type;
2219 *   - <0, on error.
2220 */
2221int btf__add_func(struct btf *btf, const char *name,
2222                  enum btf_func_linkage linkage, int proto_type_id)
2223{
2224        int id;
2225
2226        if (!name || !name[0])
2227                return -EINVAL;
2228        if (linkage != BTF_FUNC_STATIC && linkage != BTF_FUNC_GLOBAL &&
2229            linkage != BTF_FUNC_EXTERN)
2230                return -EINVAL;
2231
2232        id = btf_add_ref_kind(btf, BTF_KIND_FUNC, name, proto_type_id);
2233        if (id > 0) {
2234                struct btf_type *t = btf_type_by_id(btf, id);
2235
2236                t->info = btf_type_info(BTF_KIND_FUNC, linkage, 0);
2237        }
2238        return id;
2239}
2240
2241/*
2242 * Append new BTF_KIND_FUNC_PROTO with:
2243 *   - *ret_type_id* - type ID for return result of a function.
2244 *
2245 * Function prototype initially has no arguments, but they can be added by
2246 * btf__add_func_param() one by one, immediately after
2247 * btf__add_func_proto() succeeded.
2248 *
2249 * Returns:
2250 *   - >0, type ID of newly added BTF type;
2251 *   - <0, on error.
2252 */
2253int btf__add_func_proto(struct btf *btf, int ret_type_id)
2254{
2255        struct btf_type *t;
2256        int sz;
2257
2258        if (validate_type_id(ret_type_id))
2259                return -EINVAL;
2260
2261        if (btf_ensure_modifiable(btf))
2262                return -ENOMEM;
2263
2264        sz = sizeof(struct btf_type);
2265        t = btf_add_type_mem(btf, sz);
2266        if (!t)
2267                return -ENOMEM;
2268
2269        /* start out with vlen=0; this will be adjusted when adding enum
2270         * values, if necessary
2271         */
2272        t->name_off = 0;
2273        t->info = btf_type_info(BTF_KIND_FUNC_PROTO, 0, 0);
2274        t->type = ret_type_id;
2275
2276        return btf_commit_type(btf, sz);
2277}
2278
2279/*
2280 * Append new function parameter for current FUNC_PROTO type with:
2281 *   - *name* - parameter name, can be NULL or empty;
2282 *   - *type_id* - type ID describing the type of the parameter.
2283 * Returns:
2284 *   -  0, on success;
2285 *   - <0, on error.
2286 */
2287int btf__add_func_param(struct btf *btf, const char *name, int type_id)
2288{
2289        struct btf_type *t;
2290        struct btf_param *p;
2291        int sz, name_off = 0;
2292
2293        if (validate_type_id(type_id))
2294                return -EINVAL;
2295
2296        /* last type should be BTF_KIND_FUNC_PROTO */
2297        if (btf->nr_types == 0)
2298                return -EINVAL;
2299        t = btf_last_type(btf);
2300        if (!btf_is_func_proto(t))
2301                return -EINVAL;
2302
2303        /* decompose and invalidate raw data */
2304        if (btf_ensure_modifiable(btf))
2305                return -ENOMEM;
2306
2307        sz = sizeof(struct btf_param);
2308        p = btf_add_type_mem(btf, sz);
2309        if (!p)
2310                return -ENOMEM;
2311
2312        if (name && name[0]) {
2313                name_off = btf__add_str(btf, name);
2314                if (name_off < 0)
2315                        return name_off;
2316        }
2317
2318        p->name_off = name_off;
2319        p->type = type_id;
2320
2321        /* update parent type's vlen */
2322        t = btf_last_type(btf);
2323        btf_type_inc_vlen(t);
2324
2325        btf->hdr->type_len += sz;
2326        btf->hdr->str_off += sz;
2327        return 0;
2328}
2329
2330/*
2331 * Append new BTF_KIND_VAR type with:
2332 *   - *name* - non-empty/non-NULL name;
2333 *   - *linkage* - variable linkage, one of BTF_VAR_STATIC,
2334 *     BTF_VAR_GLOBAL_ALLOCATED, or BTF_VAR_GLOBAL_EXTERN;
2335 *   - *type_id* - type ID of the type describing the type of the variable.
2336 * Returns:
2337 *   - >0, type ID of newly added BTF type;
2338 *   - <0, on error.
2339 */
2340int btf__add_var(struct btf *btf, const char *name, int linkage, int type_id)
2341{
2342        struct btf_type *t;
2343        struct btf_var *v;
2344        int sz, name_off;
2345
2346        /* non-empty name */
2347        if (!name || !name[0])
2348                return -EINVAL;
2349        if (linkage != BTF_VAR_STATIC && linkage != BTF_VAR_GLOBAL_ALLOCATED &&
2350            linkage != BTF_VAR_GLOBAL_EXTERN)
2351                return -EINVAL;
2352        if (validate_type_id(type_id))
2353                return -EINVAL;
2354
2355        /* deconstruct BTF, if necessary, and invalidate raw_data */
2356        if (btf_ensure_modifiable(btf))
2357                return -ENOMEM;
2358
2359        sz = sizeof(struct btf_type) + sizeof(struct btf_var);
2360        t = btf_add_type_mem(btf, sz);
2361        if (!t)
2362                return -ENOMEM;
2363
2364        name_off = btf__add_str(btf, name);
2365        if (name_off < 0)
2366                return name_off;
2367
2368        t->name_off = name_off;
2369        t->info = btf_type_info(BTF_KIND_VAR, 0, 0);
2370        t->type = type_id;
2371
2372        v = btf_var(t);
2373        v->linkage = linkage;
2374
2375        return btf_commit_type(btf, sz);
2376}
2377
2378/*
2379 * Append new BTF_KIND_DATASEC type with:
2380 *   - *name* - non-empty/non-NULL name;
2381 *   - *byte_sz* - data section size, in bytes.
2382 *
2383 * Data section is initially empty. Variables info can be added with
2384 * btf__add_datasec_var_info() calls, after btf__add_datasec() succeeds.
2385 *
2386 * Returns:
2387 *   - >0, type ID of newly added BTF type;
2388 *   - <0, on error.
2389 */
2390int btf__add_datasec(struct btf *btf, const char *name, __u32 byte_sz)
2391{
2392        struct btf_type *t;
2393        int sz, name_off;
2394
2395        /* non-empty name */
2396        if (!name || !name[0])
2397                return -EINVAL;
2398
2399        if (btf_ensure_modifiable(btf))
2400                return -ENOMEM;
2401
2402        sz = sizeof(struct btf_type);
2403        t = btf_add_type_mem(btf, sz);
2404        if (!t)
2405                return -ENOMEM;
2406
2407        name_off = btf__add_str(btf, name);
2408        if (name_off < 0)
2409                return name_off;
2410
2411        /* start with vlen=0, which will be update as var_secinfos are added */
2412        t->name_off = name_off;
2413        t->info = btf_type_info(BTF_KIND_DATASEC, 0, 0);
2414        t->size = byte_sz;
2415
2416        return btf_commit_type(btf, sz);
2417}
2418
2419/*
2420 * Append new data section variable information entry for current DATASEC type:
2421 *   - *var_type_id* - type ID, describing type of the variable;
2422 *   - *offset* - variable offset within data section, in bytes;
2423 *   - *byte_sz* - variable size, in bytes.
2424 *
2425 * Returns:
2426 *   -  0, on success;
2427 *   - <0, on error.
2428 */
2429int btf__add_datasec_var_info(struct btf *btf, int var_type_id, __u32 offset, __u32 byte_sz)
2430{
2431        struct btf_type *t;
2432        struct btf_var_secinfo *v;
2433        int sz;
2434
2435        /* last type should be BTF_KIND_DATASEC */
2436        if (btf->nr_types == 0)
2437                return -EINVAL;
2438        t = btf_last_type(btf);
2439        if (!btf_is_datasec(t))
2440                return -EINVAL;
2441
2442        if (validate_type_id(var_type_id))
2443                return -EINVAL;
2444
2445        /* decompose and invalidate raw data */
2446        if (btf_ensure_modifiable(btf))
2447                return -ENOMEM;
2448
2449        sz = sizeof(struct btf_var_secinfo);
2450        v = btf_add_type_mem(btf, sz);
2451        if (!v)
2452                return -ENOMEM;
2453
2454        v->type = var_type_id;
2455        v->offset = offset;
2456        v->size = byte_sz;
2457
2458        /* update parent type's vlen */
2459        t = btf_last_type(btf);
2460        btf_type_inc_vlen(t);
2461
2462        btf->hdr->type_len += sz;
2463        btf->hdr->str_off += sz;
2464        return 0;
2465}
2466
2467struct btf_ext_sec_setup_param {
2468        __u32 off;
2469        __u32 len;
2470        __u32 min_rec_size;
2471        struct btf_ext_info *ext_info;
2472        const char *desc;
2473};
2474
2475static int btf_ext_setup_info(struct btf_ext *btf_ext,
2476                              struct btf_ext_sec_setup_param *ext_sec)
2477{
2478        const struct btf_ext_info_sec *sinfo;
2479        struct btf_ext_info *ext_info;
2480        __u32 info_left, record_size;
2481        /* The start of the info sec (including the __u32 record_size). */
2482        void *info;
2483
2484        if (ext_sec->len == 0)
2485                return 0;
2486
2487        if (ext_sec->off & 0x03) {
2488                pr_debug(".BTF.ext %s section is not aligned to 4 bytes\n",
2489                     ext_sec->desc);
2490                return -EINVAL;
2491        }
2492
2493        info = btf_ext->data + btf_ext->hdr->hdr_len + ext_sec->off;
2494        info_left = ext_sec->len;
2495
2496        if (btf_ext->data + btf_ext->data_size < info + ext_sec->len) {
2497                pr_debug("%s section (off:%u len:%u) is beyond the end of the ELF section .BTF.ext\n",
2498                         ext_sec->desc, ext_sec->off, ext_sec->len);
2499                return -EINVAL;
2500        }
2501
2502        /* At least a record size */
2503        if (info_left < sizeof(__u32)) {
2504                pr_debug(".BTF.ext %s record size not found\n", ext_sec->desc);
2505                return -EINVAL;
2506        }
2507
2508        /* The record size needs to meet the minimum standard */
2509        record_size = *(__u32 *)info;
2510        if (record_size < ext_sec->min_rec_size ||
2511            record_size & 0x03) {
2512                pr_debug("%s section in .BTF.ext has invalid record size %u\n",
2513                         ext_sec->desc, record_size);
2514                return -EINVAL;
2515        }
2516
2517        sinfo = info + sizeof(__u32);
2518        info_left -= sizeof(__u32);
2519
2520        /* If no records, return failure now so .BTF.ext won't be used. */
2521        if (!info_left) {
2522                pr_debug("%s section in .BTF.ext has no records", ext_sec->desc);
2523                return -EINVAL;
2524        }
2525
2526        while (info_left) {
2527                unsigned int sec_hdrlen = sizeof(struct btf_ext_info_sec);
2528                __u64 total_record_size;
2529                __u32 num_records;
2530
2531                if (info_left < sec_hdrlen) {
2532                        pr_debug("%s section header is not found in .BTF.ext\n",
2533                             ext_sec->desc);
2534                        return -EINVAL;
2535                }
2536
2537                num_records = sinfo->num_info;
2538                if (num_records == 0) {
2539                        pr_debug("%s section has incorrect num_records in .BTF.ext\n",
2540                             ext_sec->desc);
2541                        return -EINVAL;
2542                }
2543
2544                total_record_size = sec_hdrlen +
2545                                    (__u64)num_records * record_size;
2546                if (info_left < total_record_size) {
2547                        pr_debug("%s section has incorrect num_records in .BTF.ext\n",
2548                             ext_sec->desc);
2549                        return -EINVAL;
2550                }
2551
2552                info_left -= total_record_size;
2553                sinfo = (void *)sinfo + total_record_size;
2554        }
2555
2556        ext_info = ext_sec->ext_info;
2557        ext_info->len = ext_sec->len - sizeof(__u32);
2558        ext_info->rec_size = record_size;
2559        ext_info->info = info + sizeof(__u32);
2560
2561        return 0;
2562}
2563
2564static int btf_ext_setup_func_info(struct btf_ext *btf_ext)
2565{
2566        struct btf_ext_sec_setup_param param = {
2567                .off = btf_ext->hdr->func_info_off,
2568                .len = btf_ext->hdr->func_info_len,
2569                .min_rec_size = sizeof(struct bpf_func_info_min),
2570                .ext_info = &btf_ext->func_info,
2571                .desc = "func_info"
2572        };
2573
2574        return btf_ext_setup_info(btf_ext, &param);
2575}
2576
2577static int btf_ext_setup_line_info(struct btf_ext *btf_ext)
2578{
2579        struct btf_ext_sec_setup_param param = {
2580                .off = btf_ext->hdr->line_info_off,
2581                .len = btf_ext->hdr->line_info_len,
2582                .min_rec_size = sizeof(struct bpf_line_info_min),
2583                .ext_info = &btf_ext->line_info,
2584                .desc = "line_info",
2585        };
2586
2587        return btf_ext_setup_info(btf_ext, &param);
2588}
2589
2590static int btf_ext_setup_core_relos(struct btf_ext *btf_ext)
2591{
2592        struct btf_ext_sec_setup_param param = {
2593                .off = btf_ext->hdr->core_relo_off,
2594                .len = btf_ext->hdr->core_relo_len,
2595                .min_rec_size = sizeof(struct bpf_core_relo),
2596                .ext_info = &btf_ext->core_relo_info,
2597                .desc = "core_relo",
2598        };
2599
2600        return btf_ext_setup_info(btf_ext, &param);
2601}
2602
2603static int btf_ext_parse_hdr(__u8 *data, __u32 data_size)
2604{
2605        const struct btf_ext_header *hdr = (struct btf_ext_header *)data;
2606
2607        if (data_size < offsetofend(struct btf_ext_header, hdr_len) ||
2608            data_size < hdr->hdr_len) {
2609                pr_debug("BTF.ext header not found");
2610                return -EINVAL;
2611        }
2612
2613        if (hdr->magic == bswap_16(BTF_MAGIC)) {
2614                pr_warn("BTF.ext in non-native endianness is not supported\n");
2615                return -ENOTSUP;
2616        } else if (hdr->magic != BTF_MAGIC) {
2617                pr_debug("Invalid BTF.ext magic:%x\n", hdr->magic);
2618                return -EINVAL;
2619        }
2620
2621        if (hdr->version != BTF_VERSION) {
2622                pr_debug("Unsupported BTF.ext version:%u\n", hdr->version);
2623                return -ENOTSUP;
2624        }
2625
2626        if (hdr->flags) {
2627                pr_debug("Unsupported BTF.ext flags:%x\n", hdr->flags);
2628                return -ENOTSUP;
2629        }
2630
2631        if (data_size == hdr->hdr_len) {
2632                pr_debug("BTF.ext has no data\n");
2633                return -EINVAL;
2634        }
2635
2636        return 0;
2637}
2638
2639void btf_ext__free(struct btf_ext *btf_ext)
2640{
2641        if (IS_ERR_OR_NULL(btf_ext))
2642                return;
2643        free(btf_ext->data);
2644        free(btf_ext);
2645}
2646
2647struct btf_ext *btf_ext__new(__u8 *data, __u32 size)
2648{
2649        struct btf_ext *btf_ext;
2650        int err;
2651
2652        err = btf_ext_parse_hdr(data, size);
2653        if (err)
2654                return ERR_PTR(err);
2655
2656        btf_ext = calloc(1, sizeof(struct btf_ext));
2657        if (!btf_ext)
2658                return ERR_PTR(-ENOMEM);
2659
2660        btf_ext->data_size = size;
2661        btf_ext->data = malloc(size);
2662        if (!btf_ext->data) {
2663                err = -ENOMEM;
2664                goto done;
2665        }
2666        memcpy(btf_ext->data, data, size);
2667
2668        if (btf_ext->hdr->hdr_len <
2669            offsetofend(struct btf_ext_header, line_info_len))
2670                goto done;
2671        err = btf_ext_setup_func_info(btf_ext);
2672        if (err)
2673                goto done;
2674
2675        err = btf_ext_setup_line_info(btf_ext);
2676        if (err)
2677                goto done;
2678
2679        if (btf_ext->hdr->hdr_len < offsetofend(struct btf_ext_header, core_relo_len))
2680                goto done;
2681        err = btf_ext_setup_core_relos(btf_ext);
2682        if (err)
2683                goto done;
2684
2685done:
2686        if (err) {
2687                btf_ext__free(btf_ext);
2688                return ERR_PTR(err);
2689        }
2690
2691        return btf_ext;
2692}
2693
2694const void *btf_ext__get_raw_data(const struct btf_ext *btf_ext, __u32 *size)
2695{
2696        *size = btf_ext->data_size;
2697        return btf_ext->data;
2698}
2699
2700static int btf_ext_reloc_info(const struct btf *btf,
2701                              const struct btf_ext_info *ext_info,
2702                              const char *sec_name, __u32 insns_cnt,
2703                              void **info, __u32 *cnt)
2704{
2705        __u32 sec_hdrlen = sizeof(struct btf_ext_info_sec);
2706        __u32 i, record_size, existing_len, records_len;
2707        struct btf_ext_info_sec *sinfo;
2708        const char *info_sec_name;
2709        __u64 remain_len;
2710        void *data;
2711
2712        record_size = ext_info->rec_size;
2713        sinfo = ext_info->info;
2714        remain_len = ext_info->len;
2715        while (remain_len > 0) {
2716                records_len = sinfo->num_info * record_size;
2717                info_sec_name = btf__name_by_offset(btf, sinfo->sec_name_off);
2718                if (strcmp(info_sec_name, sec_name)) {
2719                        remain_len -= sec_hdrlen + records_len;
2720                        sinfo = (void *)sinfo + sec_hdrlen + records_len;
2721                        continue;
2722                }
2723
2724                existing_len = (*cnt) * record_size;
2725                data = realloc(*info, existing_len + records_len);
2726                if (!data)
2727                        return -ENOMEM;
2728
2729                memcpy(data + existing_len, sinfo->data, records_len);
2730                /* adjust insn_off only, the rest data will be passed
2731                 * to the kernel.
2732                 */
2733                for (i = 0; i < sinfo->num_info; i++) {
2734                        __u32 *insn_off;
2735
2736                        insn_off = data + existing_len + (i * record_size);
2737                        *insn_off = *insn_off / sizeof(struct bpf_insn) +
2738                                insns_cnt;
2739                }
2740                *info = data;
2741                *cnt += sinfo->num_info;
2742                return 0;
2743        }
2744
2745        return -ENOENT;
2746}
2747
2748int btf_ext__reloc_func_info(const struct btf *btf,
2749                             const struct btf_ext *btf_ext,
2750                             const char *sec_name, __u32 insns_cnt,
2751                             void **func_info, __u32 *cnt)
2752{
2753        return btf_ext_reloc_info(btf, &btf_ext->func_info, sec_name,
2754                                  insns_cnt, func_info, cnt);
2755}
2756
2757int btf_ext__reloc_line_info(const struct btf *btf,
2758                             const struct btf_ext *btf_ext,
2759                             const char *sec_name, __u32 insns_cnt,
2760                             void **line_info, __u32 *cnt)
2761{
2762        return btf_ext_reloc_info(btf, &btf_ext->line_info, sec_name,
2763                                  insns_cnt, line_info, cnt);
2764}
2765
2766__u32 btf_ext__func_info_rec_size(const struct btf_ext *btf_ext)
2767{
2768        return btf_ext->func_info.rec_size;
2769}
2770
2771__u32 btf_ext__line_info_rec_size(const struct btf_ext *btf_ext)
2772{
2773        return btf_ext->line_info.rec_size;
2774}
2775
2776struct btf_dedup;
2777
2778static struct btf_dedup *btf_dedup_new(struct btf *btf, struct btf_ext *btf_ext,
2779                                       const struct btf_dedup_opts *opts);
2780static void btf_dedup_free(struct btf_dedup *d);
2781static int btf_dedup_prep(struct btf_dedup *d);
2782static int btf_dedup_strings(struct btf_dedup *d);
2783static int btf_dedup_prim_types(struct btf_dedup *d);
2784static int btf_dedup_struct_types(struct btf_dedup *d);
2785static int btf_dedup_ref_types(struct btf_dedup *d);
2786static int btf_dedup_compact_types(struct btf_dedup *d);
2787static int btf_dedup_remap_types(struct btf_dedup *d);
2788
2789/*
2790 * Deduplicate BTF types and strings.
2791 *
2792 * BTF dedup algorithm takes as an input `struct btf` representing `.BTF` ELF
2793 * section with all BTF type descriptors and string data. It overwrites that
2794 * memory in-place with deduplicated types and strings without any loss of
2795 * information. If optional `struct btf_ext` representing '.BTF.ext' ELF section
2796 * is provided, all the strings referenced from .BTF.ext section are honored
2797 * and updated to point to the right offsets after deduplication.
2798 *
2799 * If function returns with error, type/string data might be garbled and should
2800 * be discarded.
2801 *
2802 * More verbose and detailed description of both problem btf_dedup is solving,
2803 * as well as solution could be found at:
2804 * https://facebookmicrosites.github.io/bpf/blog/2018/11/14/btf-enhancement.html
2805 *
2806 * Problem description and justification
2807 * =====================================
2808 *
2809 * BTF type information is typically emitted either as a result of conversion
2810 * from DWARF to BTF or directly by compiler. In both cases, each compilation
2811 * unit contains information about a subset of all the types that are used
2812 * in an application. These subsets are frequently overlapping and contain a lot
2813 * of duplicated information when later concatenated together into a single
2814 * binary. This algorithm ensures that each unique type is represented by single
2815 * BTF type descriptor, greatly reducing resulting size of BTF data.
2816 *
2817 * Compilation unit isolation and subsequent duplication of data is not the only
2818 * problem. The same type hierarchy (e.g., struct and all the type that struct
2819 * references) in different compilation units can be represented in BTF to
2820 * various degrees of completeness (or, rather, incompleteness) due to
2821 * struct/union forward declarations.
2822 *
2823 * Let's take a look at an example, that we'll use to better understand the
2824 * problem (and solution). Suppose we have two compilation units, each using
2825 * same `struct S`, but each of them having incomplete type information about
2826 * struct's fields:
2827 *
2828 * // CU #1:
2829 * struct S;
2830 * struct A {
2831 *      int a;
2832 *      struct A* self;
2833 *      struct S* parent;
2834 * };
2835 * struct B;
2836 * struct S {
2837 *      struct A* a_ptr;
2838 *      struct B* b_ptr;
2839 * };
2840 *
2841 * // CU #2:
2842 * struct S;
2843 * struct A;
2844 * struct B {
2845 *      int b;
2846 *      struct B* self;
2847 *      struct S* parent;
2848 * };
2849 * struct S {
2850 *      struct A* a_ptr;
2851 *      struct B* b_ptr;
2852 * };
2853 *
2854 * In case of CU #1, BTF data will know only that `struct B` exist (but no
2855 * more), but will know the complete type information about `struct A`. While
2856 * for CU #2, it will know full type information about `struct B`, but will
2857 * only know about forward declaration of `struct A` (in BTF terms, it will
2858 * have `BTF_KIND_FWD` type descriptor with name `B`).
2859 *
2860 * This compilation unit isolation means that it's possible that there is no
2861 * single CU with complete type information describing structs `S`, `A`, and
2862 * `B`. Also, we might get tons of duplicated and redundant type information.
2863 *
2864 * Additional complication we need to keep in mind comes from the fact that
2865 * types, in general, can form graphs containing cycles, not just DAGs.
2866 *
2867 * While algorithm does deduplication, it also merges and resolves type
2868 * information (unless disabled throught `struct btf_opts`), whenever possible.
2869 * E.g., in the example above with two compilation units having partial type
2870 * information for structs `A` and `B`, the output of algorithm will emit
2871 * a single copy of each BTF type that describes structs `A`, `B`, and `S`
2872 * (as well as type information for `int` and pointers), as if they were defined
2873 * in a single compilation unit as:
2874 *
2875 * struct A {
2876 *      int a;
2877 *      struct A* self;
2878 *      struct S* parent;
2879 * };
2880 * struct B {
2881 *      int b;
2882 *      struct B* self;
2883 *      struct S* parent;
2884 * };
2885 * struct S {
2886 *      struct A* a_ptr;
2887 *      struct B* b_ptr;
2888 * };
2889 *
2890 * Algorithm summary
2891 * =================
2892 *
2893 * Algorithm completes its work in 6 separate passes:
2894 *
2895 * 1. Strings deduplication.
2896 * 2. Primitive types deduplication (int, enum, fwd).
2897 * 3. Struct/union types deduplication.
2898 * 4. Reference types deduplication (pointers, typedefs, arrays, funcs, func
2899 *    protos, and const/volatile/restrict modifiers).
2900 * 5. Types compaction.
2901 * 6. Types remapping.
2902 *
2903 * Algorithm determines canonical type descriptor, which is a single
2904 * representative type for each truly unique type. This canonical type is the
2905 * one that will go into final deduplicated BTF type information. For
2906 * struct/unions, it is also the type that algorithm will merge additional type
2907 * information into (while resolving FWDs), as it discovers it from data in
2908 * other CUs. Each input BTF type eventually gets either mapped to itself, if
2909 * that type is canonical, or to some other type, if that type is equivalent
2910 * and was chosen as canonical representative. This mapping is stored in
2911 * `btf_dedup->map` array. This map is also used to record STRUCT/UNION that
2912 * FWD type got resolved to.
2913 *
2914 * To facilitate fast discovery of canonical types, we also maintain canonical
2915 * index (`btf_dedup->dedup_table`), which maps type descriptor's signature hash
2916 * (i.e., hashed kind, name, size, fields, etc) into a list of canonical types
2917 * that match that signature. With sufficiently good choice of type signature
2918 * hashing function, we can limit number of canonical types for each unique type
2919 * signature to a very small number, allowing to find canonical type for any
2920 * duplicated type very quickly.
2921 *
2922 * Struct/union deduplication is the most critical part and algorithm for
2923 * deduplicating structs/unions is described in greater details in comments for
2924 * `btf_dedup_is_equiv` function.
2925 */
2926int btf__dedup(struct btf *btf, struct btf_ext *btf_ext,
2927               const struct btf_dedup_opts *opts)
2928{
2929        struct btf_dedup *d = btf_dedup_new(btf, btf_ext, opts);
2930        int err;
2931
2932        if (IS_ERR(d)) {
2933                pr_debug("btf_dedup_new failed: %ld", PTR_ERR(d));
2934                return -EINVAL;
2935        }
2936
2937        if (btf_ensure_modifiable(btf))
2938                return -ENOMEM;
2939
2940        err = btf_dedup_prep(d);
2941        if (err) {
2942                pr_debug("btf_dedup_prep failed:%d\n", err);
2943                goto done;
2944        }
2945        err = btf_dedup_strings(d);
2946        if (err < 0) {
2947                pr_debug("btf_dedup_strings failed:%d\n", err);
2948                goto done;
2949        }
2950        err = btf_dedup_prim_types(d);
2951        if (err < 0) {
2952                pr_debug("btf_dedup_prim_types failed:%d\n", err);
2953                goto done;
2954        }
2955        err = btf_dedup_struct_types(d);
2956        if (err < 0) {
2957                pr_debug("btf_dedup_struct_types failed:%d\n", err);
2958                goto done;
2959        }
2960        err = btf_dedup_ref_types(d);
2961        if (err < 0) {
2962                pr_debug("btf_dedup_ref_types failed:%d\n", err);
2963                goto done;
2964        }
2965        err = btf_dedup_compact_types(d);
2966        if (err < 0) {
2967                pr_debug("btf_dedup_compact_types failed:%d\n", err);
2968                goto done;
2969        }
2970        err = btf_dedup_remap_types(d);
2971        if (err < 0) {
2972                pr_debug("btf_dedup_remap_types failed:%d\n", err);
2973                goto done;
2974        }
2975
2976done:
2977        btf_dedup_free(d);
2978        return err;
2979}
2980
2981#define BTF_UNPROCESSED_ID ((__u32)-1)
2982#define BTF_IN_PROGRESS_ID ((__u32)-2)
2983
2984struct btf_dedup {
2985        /* .BTF section to be deduped in-place */
2986        struct btf *btf;
2987        /*
2988         * Optional .BTF.ext section. When provided, any strings referenced
2989         * from it will be taken into account when deduping strings
2990         */
2991        struct btf_ext *btf_ext;
2992        /*
2993         * This is a map from any type's signature hash to a list of possible
2994         * canonical representative type candidates. Hash collisions are
2995         * ignored, so even types of various kinds can share same list of
2996         * candidates, which is fine because we rely on subsequent
2997         * btf_xxx_equal() checks to authoritatively verify type equality.
2998         */
2999        struct hashmap *dedup_table;
3000        /* Canonical types map */
3001        __u32 *map;
3002        /* Hypothetical mapping, used during type graph equivalence checks */
3003        __u32 *hypot_map;
3004        __u32 *hypot_list;
3005        size_t hypot_cnt;
3006        size_t hypot_cap;
3007        /* Whether hypothetical mapping, if successful, would need to adjust
3008         * already canonicalized types (due to a new forward declaration to
3009         * concrete type resolution). In such case, during split BTF dedup
3010         * candidate type would still be considered as different, because base
3011         * BTF is considered to be immutable.
3012         */
3013        bool hypot_adjust_canon;
3014        /* Various option modifying behavior of algorithm */
3015        struct btf_dedup_opts opts;
3016        /* temporary strings deduplication state */
3017        void *strs_data;
3018        size_t strs_cap;
3019        size_t strs_len;
3020        struct hashmap* strs_hash;
3021};
3022
3023static long hash_combine(long h, long value)
3024{
3025        return h * 31 + value;
3026}
3027
3028#define for_each_dedup_cand(d, node, hash) \
3029        hashmap__for_each_key_entry(d->dedup_table, node, (void *)hash)
3030
3031static int btf_dedup_table_add(struct btf_dedup *d, long hash, __u32 type_id)
3032{
3033        return hashmap__append(d->dedup_table,
3034                               (void *)hash, (void *)(long)type_id);
3035}
3036
3037static int btf_dedup_hypot_map_add(struct btf_dedup *d,
3038                                   __u32 from_id, __u32 to_id)
3039{
3040        if (d->hypot_cnt == d->hypot_cap) {
3041                __u32 *new_list;
3042
3043                d->hypot_cap += max((size_t)16, d->hypot_cap / 2);
3044                new_list = libbpf_reallocarray(d->hypot_list, d->hypot_cap, sizeof(__u32));
3045                if (!new_list)
3046                        return -ENOMEM;
3047                d->hypot_list = new_list;
3048        }
3049        d->hypot_list[d->hypot_cnt++] = from_id;
3050        d->hypot_map[from_id] = to_id;
3051        return 0;
3052}
3053
3054static void btf_dedup_clear_hypot_map(struct btf_dedup *d)
3055{
3056        int i;
3057
3058        for (i = 0; i < d->hypot_cnt; i++)
3059                d->hypot_map[d->hypot_list[i]] = BTF_UNPROCESSED_ID;
3060        d->hypot_cnt = 0;
3061        d->hypot_adjust_canon = false;
3062}
3063
3064static void btf_dedup_free(struct btf_dedup *d)
3065{
3066        hashmap__free(d->dedup_table);
3067        d->dedup_table = NULL;
3068
3069        free(d->map);
3070        d->map = NULL;
3071
3072        free(d->hypot_map);
3073        d->hypot_map = NULL;
3074
3075        free(d->hypot_list);
3076        d->hypot_list = NULL;
3077
3078        free(d);
3079}
3080
3081static size_t btf_dedup_identity_hash_fn(const void *key, void *ctx)
3082{
3083        return (size_t)key;
3084}
3085
3086static size_t btf_dedup_collision_hash_fn(const void *key, void *ctx)
3087{
3088        return 0;
3089}
3090
3091static bool btf_dedup_equal_fn(const void *k1, const void *k2, void *ctx)
3092{
3093        return k1 == k2;
3094}
3095
3096static struct btf_dedup *btf_dedup_new(struct btf *btf, struct btf_ext *btf_ext,
3097                                       const struct btf_dedup_opts *opts)
3098{
3099        struct btf_dedup *d = calloc(1, sizeof(struct btf_dedup));
3100        hashmap_hash_fn hash_fn = btf_dedup_identity_hash_fn;
3101        int i, err = 0, type_cnt;
3102
3103        if (!d)
3104                return ERR_PTR(-ENOMEM);
3105
3106        d->opts.dont_resolve_fwds = opts && opts->dont_resolve_fwds;
3107        /* dedup_table_size is now used only to force collisions in tests */
3108        if (opts && opts->dedup_table_size == 1)
3109                hash_fn = btf_dedup_collision_hash_fn;
3110
3111        d->btf = btf;
3112        d->btf_ext = btf_ext;
3113
3114        d->dedup_table = hashmap__new(hash_fn, btf_dedup_equal_fn, NULL);
3115        if (IS_ERR(d->dedup_table)) {
3116                err = PTR_ERR(d->dedup_table);
3117                d->dedup_table = NULL;
3118                goto done;
3119        }
3120
3121        type_cnt = btf__get_nr_types(btf) + 1;
3122        d->map = malloc(sizeof(__u32) * type_cnt);
3123        if (!d->map) {
3124                err = -ENOMEM;
3125                goto done;
3126        }
3127        /* special BTF "void" type is made canonical immediately */
3128        d->map[0] = 0;
3129        for (i = 1; i < type_cnt; i++) {
3130                struct btf_type *t = btf_type_by_id(d->btf, i);
3131
3132                /* VAR and DATASEC are never deduped and are self-canonical */
3133                if (btf_is_var(t) || btf_is_datasec(t))
3134                        d->map[i] = i;
3135                else
3136                        d->map[i] = BTF_UNPROCESSED_ID;
3137        }
3138
3139        d->hypot_map = malloc(sizeof(__u32) * type_cnt);
3140        if (!d->hypot_map) {
3141                err = -ENOMEM;
3142                goto done;
3143        }
3144        for (i = 0; i < type_cnt; i++)
3145                d->hypot_map[i] = BTF_UNPROCESSED_ID;
3146
3147done:
3148        if (err) {
3149                btf_dedup_free(d);
3150                return ERR_PTR(err);
3151        }
3152
3153        return d;
3154}
3155
3156typedef int (*str_off_fn_t)(__u32 *str_off_ptr, void *ctx);
3157
3158/*
3159 * Iterate over all possible places in .BTF and .BTF.ext that can reference
3160 * string and pass pointer to it to a provided callback `fn`.
3161 */
3162static int btf_for_each_str_off(struct btf_dedup *d, str_off_fn_t fn, void *ctx)
3163{
3164        void *line_data_cur, *line_data_end;
3165        int i, j, r, rec_size;
3166        struct btf_type *t;
3167
3168        for (i = 0; i < d->btf->nr_types; i++) {
3169                t = btf_type_by_id(d->btf, d->btf->start_id + i);
3170                r = fn(&t->name_off, ctx);
3171                if (r)
3172                        return r;
3173
3174                switch (btf_kind(t)) {
3175                case BTF_KIND_STRUCT:
3176                case BTF_KIND_UNION: {
3177                        struct btf_member *m = btf_members(t);
3178                        __u16 vlen = btf_vlen(t);
3179
3180                        for (j = 0; j < vlen; j++) {
3181                                r = fn(&m->name_off, ctx);
3182                                if (r)
3183                                        return r;
3184                                m++;
3185                        }
3186                        break;
3187                }
3188                case BTF_KIND_ENUM: {
3189                        struct btf_enum *m = btf_enum(t);
3190                        __u16 vlen = btf_vlen(t);
3191
3192                        for (j = 0; j < vlen; j++) {
3193                                r = fn(&m->name_off, ctx);
3194                                if (r)
3195                                        return r;
3196                                m++;
3197                        }
3198                        break;
3199                }
3200                case BTF_KIND_FUNC_PROTO: {
3201                        struct btf_param *m = btf_params(t);
3202                        __u16 vlen = btf_vlen(t);
3203
3204                        for (j = 0; j < vlen; j++) {
3205                                r = fn(&m->name_off, ctx);
3206                                if (r)
3207                                        return r;
3208                                m++;
3209                        }
3210                        break;
3211                }
3212                default:
3213                        break;
3214                }
3215        }
3216
3217        if (!d->btf_ext)
3218                return 0;
3219
3220        line_data_cur = d->btf_ext->line_info.info;
3221        line_data_end = d->btf_ext->line_info.info + d->btf_ext->line_info.len;
3222        rec_size = d->btf_ext->line_info.rec_size;
3223
3224        while (line_data_cur < line_data_end) {
3225                struct btf_ext_info_sec *sec = line_data_cur;
3226                struct bpf_line_info_min *line_info;
3227                __u32 num_info = sec->num_info;
3228
3229                r = fn(&sec->sec_name_off, ctx);
3230                if (r)
3231                        return r;
3232
3233                line_data_cur += sizeof(struct btf_ext_info_sec);
3234                for (i = 0; i < num_info; i++) {
3235                        line_info = line_data_cur;
3236                        r = fn(&line_info->file_name_off, ctx);
3237                        if (r)
3238                                return r;
3239                        r = fn(&line_info->line_off, ctx);
3240                        if (r)
3241                                return r;
3242                        line_data_cur += rec_size;
3243                }
3244        }
3245
3246        return 0;
3247}
3248
3249static int strs_dedup_remap_str_off(__u32 *str_off_ptr, void *ctx)
3250{
3251        struct btf_dedup *d = ctx;
3252        __u32 str_off = *str_off_ptr;
3253        long old_off, new_off, len;
3254        const char *s;
3255        void *p;
3256        int err;
3257
3258        /* don't touch empty string or string in main BTF */
3259        if (str_off == 0 || str_off < d->btf->start_str_off)
3260                return 0;
3261
3262        s = btf__str_by_offset(d->btf, str_off);
3263        if (d->btf->base_btf) {
3264                err = btf__find_str(d->btf->base_btf, s);
3265                if (err >= 0) {
3266                        *str_off_ptr = err;
3267                        return 0;
3268                }
3269                if (err != -ENOENT)
3270                        return err;
3271        }
3272
3273        len = strlen(s) + 1;
3274
3275        new_off = d->strs_len;
3276        p = btf_add_mem(&d->strs_data, &d->strs_cap, 1, new_off, BTF_MAX_STR_OFFSET, len);
3277        if (!p)
3278                return -ENOMEM;
3279
3280        memcpy(p, s, len);
3281
3282        /* Now attempt to add the string, but only if the string with the same
3283         * contents doesn't exist already (HASHMAP_ADD strategy). If such
3284         * string exists, we'll get its offset in old_off (that's old_key).
3285         */
3286        err = hashmap__insert(d->strs_hash, (void *)new_off, (void *)new_off,
3287                              HASHMAP_ADD, (const void **)&old_off, NULL);
3288        if (err == -EEXIST) {
3289                *str_off_ptr = d->btf->start_str_off + old_off;
3290        } else if (err) {
3291                return err;
3292        } else {
3293                *str_off_ptr = d->btf->start_str_off + new_off;
3294                d->strs_len += len;
3295        }
3296        return 0;
3297}
3298
3299/*
3300 * Dedup string and filter out those that are not referenced from either .BTF
3301 * or .BTF.ext (if provided) sections.
3302 *
3303 * This is done by building index of all strings in BTF's string section,
3304 * then iterating over all entities that can reference strings (e.g., type
3305 * names, struct field names, .BTF.ext line info, etc) and marking corresponding
3306 * strings as used. After that all used strings are deduped and compacted into
3307 * sequential blob of memory and new offsets are calculated. Then all the string
3308 * references are iterated again and rewritten using new offsets.
3309 */
3310static int btf_dedup_strings(struct btf_dedup *d)
3311{
3312        char *s;
3313        int err;
3314
3315        if (d->btf->strs_deduped)
3316                return 0;
3317
3318        /* temporarily switch to use btf_dedup's strs_data for strings for hash
3319         * functions; later we'll just transfer hashmap to struct btf as is,
3320         * along the strs_data
3321         */
3322        d->btf->strs_data_ptr = &d->strs_data;
3323
3324        d->strs_hash = hashmap__new(strs_hash_fn, strs_hash_equal_fn, d->btf);
3325        if (IS_ERR(d->strs_hash)) {
3326                err = PTR_ERR(d->strs_hash);
3327                d->strs_hash = NULL;
3328                goto err_out;
3329        }
3330
3331        if (!d->btf->base_btf) {
3332                s = btf_add_mem(&d->strs_data, &d->strs_cap, 1, d->strs_len, BTF_MAX_STR_OFFSET, 1);
3333                if (!s)
3334                        return -ENOMEM;
3335                /* initial empty string */
3336                s[0] = 0;
3337                d->strs_len = 1;
3338
3339                /* insert empty string; we won't be looking it up during strings
3340                 * dedup, but it's good to have it for generic BTF string lookups
3341                 */
3342                err = hashmap__insert(d->strs_hash, (void *)0, (void *)0,
3343                                      HASHMAP_ADD, NULL, NULL);
3344                if (err)
3345                        goto err_out;
3346        }
3347
3348        /* remap string offsets */
3349        err = btf_for_each_str_off(d, strs_dedup_remap_str_off, d);
3350        if (err)
3351                goto err_out;
3352
3353        /* replace BTF string data and hash with deduped ones */
3354        free(d->btf->strs_data);
3355        hashmap__free(d->btf->strs_hash);
3356        d->btf->strs_data = d->strs_data;
3357        d->btf->strs_data_cap = d->strs_cap;
3358        d->btf->hdr->str_len = d->strs_len;
3359        d->btf->strs_hash = d->strs_hash;
3360        /* now point strs_data_ptr back to btf->strs_data */
3361        d->btf->strs_data_ptr = &d->btf->strs_data;
3362
3363        d->strs_data = d->strs_hash = NULL;
3364        d->strs_len = d->strs_cap = 0;
3365        d->btf->strs_deduped = true;
3366        return 0;
3367
3368err_out:
3369        free(d->strs_data);
3370        hashmap__free(d->strs_hash);
3371        d->strs_data = d->strs_hash = NULL;
3372        d->strs_len = d->strs_cap = 0;
3373
3374        /* restore strings pointer for existing d->btf->strs_hash back */
3375        d->btf->strs_data_ptr = &d->strs_data;
3376
3377        return err;
3378}
3379
3380static long btf_hash_common(struct btf_type *t)
3381{
3382        long h;
3383
3384        h = hash_combine(0, t->name_off);
3385        h = hash_combine(h, t->info);
3386        h = hash_combine(h, t->size);
3387        return h;
3388}
3389
3390static bool btf_equal_common(struct btf_type *t1, struct btf_type *t2)
3391{
3392        return t1->name_off == t2->name_off &&
3393               t1->info == t2->info &&
3394               t1->size == t2->size;
3395}
3396
3397/* Calculate type signature hash of INT. */
3398static long btf_hash_int(struct btf_type *t)
3399{
3400        __u32 info = *(__u32 *)(t + 1);
3401        long h;
3402
3403        h = btf_hash_common(t);
3404        h = hash_combine(h, info);
3405        return h;
3406}
3407
3408/* Check structural equality of two INTs. */
3409static bool btf_equal_int(struct btf_type *t1, struct btf_type *t2)
3410{
3411        __u32 info1, info2;
3412
3413        if (!btf_equal_common(t1, t2))
3414                return false;
3415        info1 = *(__u32 *)(t1 + 1);
3416        info2 = *(__u32 *)(t2 + 1);
3417        return info1 == info2;
3418}
3419
3420/* Calculate type signature hash of ENUM. */
3421static long btf_hash_enum(struct btf_type *t)
3422{
3423        long h;
3424
3425        /* don't hash vlen and enum members to support enum fwd resolving */
3426        h = hash_combine(0, t->name_off);
3427        h = hash_combine(h, t->info & ~0xffff);
3428        h = hash_combine(h, t->size);
3429        return h;
3430}
3431
3432/* Check structural equality of two ENUMs. */
3433static bool btf_equal_enum(struct btf_type *t1, struct btf_type *t2)
3434{
3435        const struct btf_enum *m1, *m2;
3436        __u16 vlen;
3437        int i;
3438
3439        if (!btf_equal_common(t1, t2))
3440                return false;
3441
3442        vlen = btf_vlen(t1);
3443        m1 = btf_enum(t1);
3444        m2 = btf_enum(t2);
3445        for (i = 0; i < vlen; i++) {
3446                if (m1->name_off != m2->name_off || m1->val != m2->val)
3447                        return false;
3448                m1++;
3449                m2++;
3450        }
3451        return true;
3452}
3453
3454static inline bool btf_is_enum_fwd(struct btf_type *t)
3455{
3456        return btf_is_enum(t) && btf_vlen(t) == 0;
3457}
3458
3459static bool btf_compat_enum(struct btf_type *t1, struct btf_type *t2)
3460{
3461        if (!btf_is_enum_fwd(t1) && !btf_is_enum_fwd(t2))
3462                return btf_equal_enum(t1, t2);
3463        /* ignore vlen when comparing */
3464        return t1->name_off == t2->name_off &&
3465               (t1->info & ~0xffff) == (t2->info & ~0xffff) &&
3466               t1->size == t2->size;
3467}
3468
3469/*
3470 * Calculate type signature hash of STRUCT/UNION, ignoring referenced type IDs,
3471 * as referenced type IDs equivalence is established separately during type
3472 * graph equivalence check algorithm.
3473 */
3474static long btf_hash_struct(struct btf_type *t)
3475{
3476        const struct btf_member *member = btf_members(t);
3477        __u32 vlen = btf_vlen(t);
3478        long h = btf_hash_common(t);
3479        int i;
3480
3481        for (i = 0; i < vlen; i++) {
3482                h = hash_combine(h, member->name_off);
3483                h = hash_combine(h, member->offset);
3484                /* no hashing of referenced type ID, it can be unresolved yet */
3485                member++;
3486        }
3487        return h;
3488}
3489
3490/*
3491 * Check structural compatibility of two FUNC_PROTOs, ignoring referenced type
3492 * IDs. This check is performed during type graph equivalence check and
3493 * referenced types equivalence is checked separately.
3494 */
3495static bool btf_shallow_equal_struct(struct btf_type *t1, struct btf_type *t2)
3496{
3497        const struct btf_member *m1, *m2;
3498        __u16 vlen;
3499        int i;
3500
3501        if (!btf_equal_common(t1, t2))
3502                return false;
3503
3504        vlen = btf_vlen(t1);
3505        m1 = btf_members(t1);
3506        m2 = btf_members(t2);
3507        for (i = 0; i < vlen; i++) {
3508                if (m1->name_off != m2->name_off || m1->offset != m2->offset)
3509                        return false;
3510                m1++;
3511                m2++;
3512        }
3513        return true;
3514}
3515
3516/*
3517 * Calculate type signature hash of ARRAY, including referenced type IDs,
3518 * under assumption that they were already resolved to canonical type IDs and
3519 * are not going to change.
3520 */
3521static long btf_hash_array(struct btf_type *t)
3522{
3523        const struct btf_array *info = btf_array(t);
3524        long h = btf_hash_common(t);
3525
3526        h = hash_combine(h, info->type);
3527        h = hash_combine(h, info->index_type);
3528        h = hash_combine(h, info->nelems);
3529        return h;
3530}
3531
3532/*
3533 * Check exact equality of two ARRAYs, taking into account referenced
3534 * type IDs, under assumption that they were already resolved to canonical
3535 * type IDs and are not going to change.
3536 * This function is called during reference types deduplication to compare
3537 * ARRAY to potential canonical representative.
3538 */
3539static bool btf_equal_array(struct btf_type *t1, struct btf_type *t2)
3540{
3541        const struct btf_array *info1, *info2;
3542
3543        if (!btf_equal_common(t1, t2))
3544                return false;
3545
3546        info1 = btf_array(t1);
3547        info2 = btf_array(t2);
3548        return info1->type == info2->type &&
3549               info1->index_type == info2->index_type &&
3550               info1->nelems == info2->nelems;
3551}
3552
3553/*
3554 * Check structural compatibility of two ARRAYs, ignoring referenced type
3555 * IDs. This check is performed during type graph equivalence check and
3556 * referenced types equivalence is checked separately.
3557 */
3558static bool btf_compat_array(struct btf_type *t1, struct btf_type *t2)
3559{
3560        if (!btf_equal_common(t1, t2))
3561                return false;
3562
3563        return btf_array(t1)->nelems == btf_array(t2)->nelems;
3564}
3565
3566/*
3567 * Calculate type signature hash of FUNC_PROTO, including referenced type IDs,
3568 * under assumption that they were already resolved to canonical type IDs and
3569 * are not going to change.
3570 */
3571static long btf_hash_fnproto(struct btf_type *t)
3572{
3573        const struct btf_param *member = btf_params(t);
3574        __u16 vlen = btf_vlen(t);
3575        long h = btf_hash_common(t);
3576        int i;
3577
3578        for (i = 0; i < vlen; i++) {
3579                h = hash_combine(h, member->name_off);
3580                h = hash_combine(h, member->type);
3581                member++;
3582        }
3583        return h;
3584}
3585
3586/*
3587 * Check exact equality of two FUNC_PROTOs, taking into account referenced
3588 * type IDs, under assumption that they were already resolved to canonical
3589 * type IDs and are not going to change.
3590 * This function is called during reference types deduplication to compare
3591 * FUNC_PROTO to potential canonical representative.
3592 */
3593static bool btf_equal_fnproto(struct btf_type *t1, struct btf_type *t2)
3594{
3595        const struct btf_param *m1, *m2;
3596        __u16 vlen;
3597        int i;
3598
3599        if (!btf_equal_common(t1, t2))
3600                return false;
3601
3602        vlen = btf_vlen(t1);
3603        m1 = btf_params(t1);
3604        m2 = btf_params(t2);
3605        for (i = 0; i < vlen; i++) {
3606                if (m1->name_off != m2->name_off || m1->type != m2->type)
3607                        return false;
3608                m1++;
3609                m2++;
3610        }
3611        return true;
3612}
3613
3614/*
3615 * Check structural compatibility of two FUNC_PROTOs, ignoring referenced type
3616 * IDs. This check is performed during type graph equivalence check and
3617 * referenced types equivalence is checked separately.
3618 */
3619static bool btf_compat_fnproto(struct btf_type *t1, struct btf_type *t2)
3620{
3621        const struct btf_param *m1, *m2;
3622        __u16 vlen;
3623        int i;
3624
3625        /* skip return type ID */
3626        if (t1->name_off != t2->name_off || t1->info != t2->info)
3627                return false;
3628
3629        vlen = btf_vlen(t1);
3630        m1 = btf_params(t1);
3631        m2 = btf_params(t2);
3632        for (i = 0; i < vlen; i++) {
3633                if (m1->name_off != m2->name_off)
3634                        return false;
3635                m1++;
3636                m2++;
3637        }
3638        return true;
3639}
3640
3641/* Prepare split BTF for deduplication by calculating hashes of base BTF's
3642 * types and initializing the rest of the state (canonical type mapping) for
3643 * the fixed base BTF part.
3644 */
3645static int btf_dedup_prep(struct btf_dedup *d)
3646{
3647        struct btf_type *t;
3648        int type_id;
3649        long h;
3650
3651        if (!d->btf->base_btf)
3652                return 0;
3653
3654        for (type_id = 1; type_id < d->btf->start_id; type_id++) {
3655                t = btf_type_by_id(d->btf, type_id);
3656
3657                /* all base BTF types are self-canonical by definition */
3658                d->map[type_id] = type_id;
3659
3660                switch (btf_kind(t)) {
3661                case BTF_KIND_VAR:
3662                case BTF_KIND_DATASEC:
3663                        /* VAR and DATASEC are never hash/deduplicated */
3664                        continue;
3665                case BTF_KIND_CONST:
3666                case BTF_KIND_VOLATILE:
3667                case BTF_KIND_RESTRICT:
3668                case BTF_KIND_PTR:
3669                case BTF_KIND_FWD:
3670                case BTF_KIND_TYPEDEF:
3671                case BTF_KIND_FUNC:
3672                case BTF_KIND_FLOAT:
3673                        h = btf_hash_common(t);
3674                        break;
3675                case BTF_KIND_INT:
3676                        h = btf_hash_int(t);
3677                        break;
3678                case BTF_KIND_ENUM:
3679                        h = btf_hash_enum(t);
3680                        break;
3681                case BTF_KIND_STRUCT:
3682                case BTF_KIND_UNION:
3683                        h = btf_hash_struct(t);
3684                        break;
3685                case BTF_KIND_ARRAY:
3686                        h = btf_hash_array(t);
3687                        break;
3688                case BTF_KIND_FUNC_PROTO:
3689                        h = btf_hash_fnproto(t);
3690                        break;
3691                default:
3692                        pr_debug("unknown kind %d for type [%d]\n", btf_kind(t), type_id);
3693                        return -EINVAL;
3694                }
3695                if (btf_dedup_table_add(d, h, type_id))
3696                        return -ENOMEM;
3697        }
3698
3699        return 0;
3700}
3701
3702/*
3703 * Deduplicate primitive types, that can't reference other types, by calculating
3704 * their type signature hash and comparing them with any possible canonical
3705 * candidate. If no canonical candidate matches, type itself is marked as
3706 * canonical and is added into `btf_dedup->dedup_table` as another candidate.
3707 */
3708static int btf_dedup_prim_type(struct btf_dedup *d, __u32 type_id)
3709{
3710        struct btf_type *t = btf_type_by_id(d->btf, type_id);
3711        struct hashmap_entry *hash_entry;
3712        struct btf_type *cand;
3713        /* if we don't find equivalent type, then we are canonical */
3714        __u32 new_id = type_id;
3715        __u32 cand_id;
3716        long h;
3717
3718        switch (btf_kind(t)) {
3719        case BTF_KIND_CONST:
3720        case BTF_KIND_VOLATILE:
3721        case BTF_KIND_RESTRICT:
3722        case BTF_KIND_PTR:
3723        case BTF_KIND_TYPEDEF:
3724        case BTF_KIND_ARRAY:
3725        case BTF_KIND_STRUCT:
3726        case BTF_KIND_UNION:
3727        case BTF_KIND_FUNC:
3728        case BTF_KIND_FUNC_PROTO:
3729        case BTF_KIND_VAR:
3730        case BTF_KIND_DATASEC:
3731                return 0;
3732
3733        case BTF_KIND_INT:
3734                h = btf_hash_int(t);
3735                for_each_dedup_cand(d, hash_entry, h) {
3736                        cand_id = (__u32)(long)hash_entry->value;
3737                        cand = btf_type_by_id(d->btf, cand_id);
3738                        if (btf_equal_int(t, cand)) {
3739                                new_id = cand_id;
3740                                break;
3741                        }
3742                }
3743                break;
3744
3745        case BTF_KIND_ENUM:
3746                h = btf_hash_enum(t);
3747                for_each_dedup_cand(d, hash_entry, h) {
3748                        cand_id = (__u32)(long)hash_entry->value;
3749                        cand = btf_type_by_id(d->btf, cand_id);
3750                        if (btf_equal_enum(t, cand)) {
3751                                new_id = cand_id;
3752                                break;
3753                        }
3754                        if (d->opts.dont_resolve_fwds)
3755                                continue;
3756                        if (btf_compat_enum(t, cand)) {
3757                                if (btf_is_enum_fwd(t)) {
3758                                        /* resolve fwd to full enum */
3759                                        new_id = cand_id;
3760                                        break;
3761                                }
3762                                /* resolve canonical enum fwd to full enum */
3763                                d->map[cand_id] = type_id;
3764                        }
3765                }
3766                break;
3767
3768        case BTF_KIND_FWD:
3769        case BTF_KIND_FLOAT:
3770                h = btf_hash_common(t);
3771                for_each_dedup_cand(d, hash_entry, h) {
3772                        cand_id = (__u32)(long)hash_entry->value;
3773                        cand = btf_type_by_id(d->btf, cand_id);
3774                        if (btf_equal_common(t, cand)) {
3775                                new_id = cand_id;
3776                                break;
3777                        }
3778                }
3779                break;
3780
3781        default:
3782                return -EINVAL;
3783        }
3784
3785        d->map[type_id] = new_id;
3786        if (type_id == new_id && btf_dedup_table_add(d, h, type_id))
3787                return -ENOMEM;
3788
3789        return 0;
3790}
3791
3792static int btf_dedup_prim_types(struct btf_dedup *d)
3793{
3794        int i, err;
3795
3796        for (i = 0; i < d->btf->nr_types; i++) {
3797                err = btf_dedup_prim_type(d, d->btf->start_id + i);
3798                if (err)
3799                        return err;
3800        }
3801        return 0;
3802}
3803
3804/*
3805 * Check whether type is already mapped into canonical one (could be to itself).
3806 */
3807static inline bool is_type_mapped(struct btf_dedup *d, uint32_t type_id)
3808{
3809        return d->map[type_id] <= BTF_MAX_NR_TYPES;
3810}
3811
3812/*
3813 * Resolve type ID into its canonical type ID, if any; otherwise return original
3814 * type ID. If type is FWD and is resolved into STRUCT/UNION already, follow
3815 * STRUCT/UNION link and resolve it into canonical type ID as well.
3816 */
3817static inline __u32 resolve_type_id(struct btf_dedup *d, __u32 type_id)
3818{
3819        while (is_type_mapped(d, type_id) && d->map[type_id] != type_id)
3820                type_id = d->map[type_id];
3821        return type_id;
3822}
3823
3824/*
3825 * Resolve FWD to underlying STRUCT/UNION, if any; otherwise return original
3826 * type ID.
3827 */
3828static uint32_t resolve_fwd_id(struct btf_dedup *d, uint32_t type_id)
3829{
3830        __u32 orig_type_id = type_id;
3831
3832        if (!btf_is_fwd(btf__type_by_id(d->btf, type_id)))
3833                return type_id;
3834
3835        while (is_type_mapped(d, type_id) && d->map[type_id] != type_id)
3836                type_id = d->map[type_id];
3837
3838        if (!btf_is_fwd(btf__type_by_id(d->btf, type_id)))
3839                return type_id;
3840
3841        return orig_type_id;
3842}
3843
3844
3845static inline __u16 btf_fwd_kind(struct btf_type *t)
3846{
3847        return btf_kflag(t) ? BTF_KIND_UNION : BTF_KIND_STRUCT;
3848}
3849
3850/* Check if given two types are identical ARRAY definitions */
3851static int btf_dedup_identical_arrays(struct btf_dedup *d, __u32 id1, __u32 id2)
3852{
3853        struct btf_type *t1, *t2;
3854
3855        t1 = btf_type_by_id(d->btf, id1);
3856        t2 = btf_type_by_id(d->btf, id2);
3857        if (!btf_is_array(t1) || !btf_is_array(t2))
3858                return 0;
3859
3860        return btf_equal_array(t1, t2);
3861}
3862
3863/*
3864 * Check equivalence of BTF type graph formed by candidate struct/union (we'll
3865 * call it "candidate graph" in this description for brevity) to a type graph
3866 * formed by (potential) canonical struct/union ("canonical graph" for brevity
3867 * here, though keep in mind that not all types in canonical graph are
3868 * necessarily canonical representatives themselves, some of them might be
3869 * duplicates or its uniqueness might not have been established yet).
3870 * Returns:
3871 *  - >0, if type graphs are equivalent;
3872 *  -  0, if not equivalent;
3873 *  - <0, on error.
3874 *
3875 * Algorithm performs side-by-side DFS traversal of both type graphs and checks
3876 * equivalence of BTF types at each step. If at any point BTF types in candidate
3877 * and canonical graphs are not compatible structurally, whole graphs are
3878 * incompatible. If types are structurally equivalent (i.e., all information
3879 * except referenced type IDs is exactly the same), a mapping from `canon_id` to
3880 * a `cand_id` is recored in hypothetical mapping (`btf_dedup->hypot_map`).
3881 * If a type references other types, then those referenced types are checked
3882 * for equivalence recursively.
3883 *
3884 * During DFS traversal, if we find that for current `canon_id` type we
3885 * already have some mapping in hypothetical map, we check for two possible
3886 * situations:
3887 *   - `canon_id` is mapped to exactly the same type as `cand_id`. This will
3888 *     happen when type graphs have cycles. In this case we assume those two
3889 *     types are equivalent.
3890 *   - `canon_id` is mapped to different type. This is contradiction in our
3891 *     hypothetical mapping, because same graph in canonical graph corresponds
3892 *     to two different types in candidate graph, which for equivalent type
3893 *     graphs shouldn't happen. This condition terminates equivalence check
3894 *     with negative result.
3895 *
3896 * If type graphs traversal exhausts types to check and find no contradiction,
3897 * then type graphs are equivalent.
3898 *
3899 * When checking types for equivalence, there is one special case: FWD types.
3900 * If FWD type resolution is allowed and one of the types (either from canonical
3901 * or candidate graph) is FWD and other is STRUCT/UNION (depending on FWD's kind
3902 * flag) and their names match, hypothetical mapping is updated to point from
3903 * FWD to STRUCT/UNION. If graphs will be determined as equivalent successfully,
3904 * this mapping will be used to record FWD -> STRUCT/UNION mapping permanently.
3905 *
3906 * Technically, this could lead to incorrect FWD to STRUCT/UNION resolution,
3907 * if there are two exactly named (or anonymous) structs/unions that are
3908 * compatible structurally, one of which has FWD field, while other is concrete
3909 * STRUCT/UNION, but according to C sources they are different structs/unions
3910 * that are referencing different types with the same name. This is extremely
3911 * unlikely to happen, but btf_dedup API allows to disable FWD resolution if
3912 * this logic is causing problems.
3913 *
3914 * Doing FWD resolution means that both candidate and/or canonical graphs can
3915 * consists of portions of the graph that come from multiple compilation units.
3916 * This is due to the fact that types within single compilation unit are always
3917 * deduplicated and FWDs are already resolved, if referenced struct/union
3918 * definiton is available. So, if we had unresolved FWD and found corresponding
3919 * STRUCT/UNION, they will be from different compilation units. This
3920 * consequently means that when we "link" FWD to corresponding STRUCT/UNION,
3921 * type graph will likely have at least two different BTF types that describe
3922 * same type (e.g., most probably there will be two different BTF types for the
3923 * same 'int' primitive type) and could even have "overlapping" parts of type
3924 * graph that describe same subset of types.
3925 *
3926 * This in turn means that our assumption that each type in canonical graph
3927 * must correspond to exactly one type in candidate graph might not hold
3928 * anymore and will make it harder to detect contradictions using hypothetical
3929 * map. To handle this problem, we allow to follow FWD -> STRUCT/UNION
3930 * resolution only in canonical graph. FWDs in candidate graphs are never
3931 * resolved. To see why it's OK, let's check all possible situations w.r.t. FWDs
3932 * that can occur:
3933 *   - Both types in canonical and candidate graphs are FWDs. If they are
3934 *     structurally equivalent, then they can either be both resolved to the
3935 *     same STRUCT/UNION or not resolved at all. In both cases they are
3936 *     equivalent and there is no need to resolve FWD on candidate side.
3937 *   - Both types in canonical and candidate graphs are concrete STRUCT/UNION,
3938 *     so nothing to resolve as well, algorithm will check equivalence anyway.
3939 *   - Type in canonical graph is FWD, while type in candidate is concrete
3940 *     STRUCT/UNION. In this case candidate graph comes from single compilation
3941 *     unit, so there is exactly one BTF type for each unique C type. After
3942 *     resolving FWD into STRUCT/UNION, there might be more than one BTF type
3943 *     in canonical graph mapping to single BTF type in candidate graph, but
3944 *     because hypothetical mapping maps from canonical to candidate types, it's
3945 *     alright, and we still maintain the property of having single `canon_id`
3946 *     mapping to single `cand_id` (there could be two different `canon_id`
3947 *     mapped to the same `cand_id`, but it's not contradictory).
3948 *   - Type in canonical graph is concrete STRUCT/UNION, while type in candidate
3949 *     graph is FWD. In this case we are just going to check compatibility of
3950 *     STRUCT/UNION and corresponding FWD, and if they are compatible, we'll
3951 *     assume that whatever STRUCT/UNION FWD resolves to must be equivalent to
3952 *     a concrete STRUCT/UNION from canonical graph. If the rest of type graphs
3953 *     turn out equivalent, we'll re-resolve FWD to concrete STRUCT/UNION from
3954 *     canonical graph.
3955 */
3956static int btf_dedup_is_equiv(struct btf_dedup *d, __u32 cand_id,
3957                              __u32 canon_id)
3958{
3959        struct btf_type *cand_type;
3960        struct btf_type *canon_type;
3961        __u32 hypot_type_id;
3962        __u16 cand_kind;
3963        __u16 canon_kind;
3964        int i, eq;
3965
3966        /* if both resolve to the same canonical, they must be equivalent */
3967        if (resolve_type_id(d, cand_id) == resolve_type_id(d, canon_id))
3968                return 1;
3969
3970        canon_id = resolve_fwd_id(d, canon_id);
3971
3972        hypot_type_id = d->hypot_map[canon_id];
3973        if (hypot_type_id <= BTF_MAX_NR_TYPES) {
3974                /* In some cases compiler will generate different DWARF types
3975                 * for *identical* array type definitions and use them for
3976                 * different fields within the *same* struct. This breaks type
3977                 * equivalence check, which makes an assumption that candidate
3978                 * types sub-graph has a consistent and deduped-by-compiler
3979                 * types within a single CU. So work around that by explicitly
3980                 * allowing identical array types here.
3981                 */
3982                return hypot_type_id == cand_id ||
3983                       btf_dedup_identical_arrays(d, hypot_type_id, cand_id);
3984        }
3985
3986        if (btf_dedup_hypot_map_add(d, canon_id, cand_id))
3987                return -ENOMEM;
3988
3989        cand_type = btf_type_by_id(d->btf, cand_id);
3990        canon_type = btf_type_by_id(d->btf, canon_id);
3991        cand_kind = btf_kind(cand_type);
3992        canon_kind = btf_kind(canon_type);
3993
3994        if (cand_type->name_off != canon_type->name_off)
3995                return 0;
3996
3997        /* FWD <--> STRUCT/UNION equivalence check, if enabled */
3998        if (!d->opts.dont_resolve_fwds
3999            && (cand_kind == BTF_KIND_FWD || canon_kind == BTF_KIND_FWD)
4000            && cand_kind != canon_kind) {
4001                __u16 real_kind;
4002                __u16 fwd_kind;
4003
4004                if (cand_kind == BTF_KIND_FWD) {
4005                        real_kind = canon_kind;
4006                        fwd_kind = btf_fwd_kind(cand_type);
4007                } else {
4008                        real_kind = cand_kind;
4009                        fwd_kind = btf_fwd_kind(canon_type);
4010                        /* we'd need to resolve base FWD to STRUCT/UNION */
4011                        if (fwd_kind == real_kind && canon_id < d->btf->start_id)
4012                                d->hypot_adjust_canon = true;
4013                }
4014                return fwd_kind == real_kind;
4015        }
4016
4017        if (cand_kind != canon_kind)
4018                return 0;
4019
4020        switch (cand_kind) {
4021        case BTF_KIND_INT:
4022                return btf_equal_int(cand_type, canon_type);
4023
4024        case BTF_KIND_ENUM:
4025                if (d->opts.dont_resolve_fwds)
4026                        return btf_equal_enum(cand_type, canon_type);
4027                else
4028                        return btf_compat_enum(cand_type, canon_type);
4029
4030        case BTF_KIND_FWD:
4031        case BTF_KIND_FLOAT:
4032                return btf_equal_common(cand_type, canon_type);
4033
4034        case BTF_KIND_CONST:
4035        case BTF_KIND_VOLATILE:
4036        case BTF_KIND_RESTRICT:
4037        case BTF_KIND_PTR:
4038        case BTF_KIND_TYPEDEF:
4039        case BTF_KIND_FUNC:
4040                if (cand_type->info != canon_type->info)
4041                        return 0;
4042                return btf_dedup_is_equiv(d, cand_type->type, canon_type->type);
4043
4044        case BTF_KIND_ARRAY: {
4045                const struct btf_array *cand_arr, *canon_arr;
4046
4047                if (!btf_compat_array(cand_type, canon_type))
4048                        return 0;
4049                cand_arr = btf_array(cand_type);
4050                canon_arr = btf_array(canon_type);
4051                eq = btf_dedup_is_equiv(d, cand_arr->index_type, canon_arr->index_type);
4052                if (eq <= 0)
4053                        return eq;
4054                return btf_dedup_is_equiv(d, cand_arr->type, canon_arr->type);
4055        }
4056
4057        case BTF_KIND_STRUCT:
4058        case BTF_KIND_UNION: {
4059                const struct btf_member *cand_m, *canon_m;
4060                __u16 vlen;
4061
4062                if (!btf_shallow_equal_struct(cand_type, canon_type))
4063                        return 0;
4064                vlen = btf_vlen(cand_type);
4065                cand_m = btf_members(cand_type);
4066                canon_m = btf_members(canon_type);
4067                for (i = 0; i < vlen; i++) {
4068                        eq = btf_dedup_is_equiv(d, cand_m->type, canon_m->type);
4069                        if (eq <= 0)
4070                                return eq;
4071                        cand_m++;
4072                        canon_m++;
4073                }
4074
4075                return 1;
4076        }
4077
4078        case BTF_KIND_FUNC_PROTO: {
4079                const struct btf_param *cand_p, *canon_p;
4080                __u16 vlen;
4081
4082                if (!btf_compat_fnproto(cand_type, canon_type))
4083                        return 0;
4084                eq = btf_dedup_is_equiv(d, cand_type->type, canon_type->type);
4085                if (eq <= 0)
4086                        return eq;
4087                vlen = btf_vlen(cand_type);
4088                cand_p = btf_params(cand_type);
4089                canon_p = btf_params(canon_type);
4090                for (i = 0; i < vlen; i++) {
4091                        eq = btf_dedup_is_equiv(d, cand_p->type, canon_p->type);
4092                        if (eq <= 0)
4093                                return eq;
4094                        cand_p++;
4095                        canon_p++;
4096                }
4097                return 1;
4098        }
4099
4100        default:
4101                return -EINVAL;
4102        }
4103        return 0;
4104}
4105
4106/*
4107 * Use hypothetical mapping, produced by successful type graph equivalence
4108 * check, to augment existing struct/union canonical mapping, where possible.
4109 *
4110 * If BTF_KIND_FWD resolution is allowed, this mapping is also used to record
4111 * FWD -> STRUCT/UNION correspondence as well. FWD resolution is bidirectional:
4112 * it doesn't matter if FWD type was part of canonical graph or candidate one,
4113 * we are recording the mapping anyway. As opposed to carefulness required
4114 * for struct/union correspondence mapping (described below), for FWD resolution
4115 * it's not important, as by the time that FWD type (reference type) will be
4116 * deduplicated all structs/unions will be deduped already anyway.
4117 *
4118 * Recording STRUCT/UNION mapping is purely a performance optimization and is
4119 * not required for correctness. It needs to be done carefully to ensure that
4120 * struct/union from candidate's type graph is not mapped into corresponding
4121 * struct/union from canonical type graph that itself hasn't been resolved into
4122 * canonical representative. The only guarantee we have is that canonical
4123 * struct/union was determined as canonical and that won't change. But any
4124 * types referenced through that struct/union fields could have been not yet
4125 * resolved, so in case like that it's too early to establish any kind of
4126 * correspondence between structs/unions.
4127 *
4128 * No canonical correspondence is derived for primitive types (they are already
4129 * deduplicated completely already anyway) or reference types (they rely on
4130 * stability of struct/union canonical relationship for equivalence checks).
4131 */
4132static void btf_dedup_merge_hypot_map(struct btf_dedup *d)
4133{
4134        __u32 canon_type_id, targ_type_id;
4135        __u16 t_kind, c_kind;
4136        __u32 t_id, c_id;
4137        int i;
4138
4139        for (i = 0; i < d->hypot_cnt; i++) {
4140                canon_type_id = d->hypot_list[i];
4141                targ_type_id = d->hypot_map[canon_type_id];
4142                t_id = resolve_type_id(d, targ_type_id);
4143                c_id = resolve_type_id(d, canon_type_id);
4144                t_kind = btf_kind(btf__type_by_id(d->btf, t_id));
4145                c_kind = btf_kind(btf__type_by_id(d->btf, c_id));
4146                /*
4147                 * Resolve FWD into STRUCT/UNION.
4148                 * It's ok to resolve FWD into STRUCT/UNION that's not yet
4149                 * mapped to canonical representative (as opposed to
4150                 * STRUCT/UNION <--> STRUCT/UNION mapping logic below), because
4151                 * eventually that struct is going to be mapped and all resolved
4152                 * FWDs will automatically resolve to correct canonical
4153                 * representative. This will happen before ref type deduping,
4154                 * which critically depends on stability of these mapping. This
4155                 * stability is not a requirement for STRUCT/UNION equivalence
4156                 * checks, though.
4157                 */
4158
4159                /* if it's the split BTF case, we still need to point base FWD
4160                 * to STRUCT/UNION in a split BTF, because FWDs from split BTF
4161                 * will be resolved against base FWD. If we don't point base
4162                 * canonical FWD to the resolved STRUCT/UNION, then all the
4163                 * FWDs in split BTF won't be correctly resolved to a proper
4164                 * STRUCT/UNION.
4165                 */
4166                if (t_kind != BTF_KIND_FWD && c_kind == BTF_KIND_FWD)
4167                        d->map[c_id] = t_id;
4168
4169                /* if graph equivalence determined that we'd need to adjust
4170                 * base canonical types, then we need to only point base FWDs
4171                 * to STRUCTs/UNIONs and do no more modifications. For all
4172                 * other purposes the type graphs were not equivalent.
4173                 */
4174                if (d->hypot_adjust_canon)
4175                        continue;
4176                
4177                if (t_kind == BTF_KIND_FWD && c_kind != BTF_KIND_FWD)
4178                        d->map[t_id] = c_id;
4179
4180                if ((t_kind == BTF_KIND_STRUCT || t_kind == BTF_KIND_UNION) &&
4181                    c_kind != BTF_KIND_FWD &&
4182                    is_type_mapped(d, c_id) &&
4183                    !is_type_mapped(d, t_id)) {
4184                        /*
4185                         * as a perf optimization, we can map struct/union
4186                         * that's part of type graph we just verified for
4187                         * equivalence. We can do that for struct/union that has
4188                         * canonical representative only, though.
4189                         */
4190                        d->map[t_id] = c_id;
4191                }
4192        }
4193}
4194
4195/*
4196 * Deduplicate struct/union types.
4197 *
4198 * For each struct/union type its type signature hash is calculated, taking
4199 * into account type's name, size, number, order and names of fields, but
4200 * ignoring type ID's referenced from fields, because they might not be deduped
4201 * completely until after reference types deduplication phase. This type hash
4202 * is used to iterate over all potential canonical types, sharing same hash.
4203 * For each canonical candidate we check whether type graphs that they form
4204 * (through referenced types in fields and so on) are equivalent using algorithm
4205 * implemented in `btf_dedup_is_equiv`. If such equivalence is found and
4206 * BTF_KIND_FWD resolution is allowed, then hypothetical mapping
4207 * (btf_dedup->hypot_map) produced by aforementioned type graph equivalence
4208 * algorithm is used to record FWD -> STRUCT/UNION mapping. It's also used to
4209 * potentially map other structs/unions to their canonical representatives,
4210 * if such relationship hasn't yet been established. This speeds up algorithm
4211 * by eliminating some of the duplicate work.
4212 *
4213 * If no matching canonical representative was found, struct/union is marked
4214 * as canonical for itself and is added into btf_dedup->dedup_table hash map
4215 * for further look ups.
4216 */
4217static int btf_dedup_struct_type(struct btf_dedup *d, __u32 type_id)
4218{
4219        struct btf_type *cand_type, *t;
4220        struct hashmap_entry *hash_entry;
4221        /* if we don't find equivalent type, then we are canonical */
4222        __u32 new_id = type_id;
4223        __u16 kind;
4224        long h;
4225
4226        /* already deduped or is in process of deduping (loop detected) */
4227        if (d->map[type_id] <= BTF_MAX_NR_TYPES)
4228                return 0;
4229
4230        t = btf_type_by_id(d->btf, type_id);
4231        kind = btf_kind(t);
4232
4233        if (kind != BTF_KIND_STRUCT && kind != BTF_KIND_UNION)
4234                return 0;
4235
4236        h = btf_hash_struct(t);
4237        for_each_dedup_cand(d, hash_entry, h) {
4238                __u32 cand_id = (__u32)(long)hash_entry->value;
4239                int eq;
4240
4241                /*
4242                 * Even though btf_dedup_is_equiv() checks for
4243                 * btf_shallow_equal_struct() internally when checking two
4244                 * structs (unions) for equivalence, we need to guard here
4245                 * from picking matching FWD type as a dedup candidate.
4246                 * This can happen due to hash collision. In such case just
4247                 * relying on btf_dedup_is_equiv() would lead to potentially
4248                 * creating a loop (FWD -> STRUCT and STRUCT -> FWD), because
4249                 * FWD and compatible STRUCT/UNION are considered equivalent.
4250                 */
4251                cand_type = btf_type_by_id(d->btf, cand_id);
4252                if (!btf_shallow_equal_struct(t, cand_type))
4253                        continue;
4254
4255                btf_dedup_clear_hypot_map(d);
4256                eq = btf_dedup_is_equiv(d, type_id, cand_id);
4257                if (eq < 0)
4258                        return eq;
4259                if (!eq)
4260                        continue;
4261                btf_dedup_merge_hypot_map(d);
4262                if (d->hypot_adjust_canon) /* not really equivalent */
4263                        continue;
4264                new_id = cand_id;
4265                break;
4266        }
4267
4268        d->map[type_id] = new_id;
4269        if (type_id == new_id && btf_dedup_table_add(d, h, type_id))
4270                return -ENOMEM;
4271
4272        return 0;
4273}
4274
4275static int btf_dedup_struct_types(struct btf_dedup *d)
4276{
4277        int i, err;
4278
4279        for (i = 0; i < d->btf->nr_types; i++) {
4280                err = btf_dedup_struct_type(d, d->btf->start_id + i);
4281                if (err)
4282                        return err;
4283        }
4284        return 0;
4285}
4286
4287/*
4288 * Deduplicate reference type.
4289 *
4290 * Once all primitive and struct/union types got deduplicated, we can easily
4291 * deduplicate all other (reference) BTF types. This is done in two steps:
4292 *
4293 * 1. Resolve all referenced type IDs into their canonical type IDs. This
4294 * resolution can be done either immediately for primitive or struct/union types
4295 * (because they were deduped in previous two phases) or recursively for
4296 * reference types. Recursion will always terminate at either primitive or
4297 * struct/union type, at which point we can "unwind" chain of reference types
4298 * one by one. There is no danger of encountering cycles because in C type
4299 * system the only way to form type cycle is through struct/union, so any chain
4300 * of reference types, even those taking part in a type cycle, will inevitably
4301 * reach struct/union at some point.
4302 *
4303 * 2. Once all referenced type IDs are resolved into canonical ones, BTF type
4304 * becomes "stable", in the sense that no further deduplication will cause
4305 * any changes to it. With that, it's now possible to calculate type's signature
4306 * hash (this time taking into account referenced type IDs) and loop over all
4307 * potential canonical representatives. If no match was found, current type
4308 * will become canonical representative of itself and will be added into
4309 * btf_dedup->dedup_table as another possible canonical representative.
4310 */
4311static int btf_dedup_ref_type(struct btf_dedup *d, __u32 type_id)
4312{
4313        struct hashmap_entry *hash_entry;
4314        __u32 new_id = type_id, cand_id;
4315        struct btf_type *t, *cand;
4316        /* if we don't find equivalent type, then we are representative type */
4317        int ref_type_id;
4318        long h;
4319
4320        if (d->map[type_id] == BTF_IN_PROGRESS_ID)
4321                return -ELOOP;
4322        if (d->map[type_id] <= BTF_MAX_NR_TYPES)
4323                return resolve_type_id(d, type_id);
4324
4325        t = btf_type_by_id(d->btf, type_id);
4326        d->map[type_id] = BTF_IN_PROGRESS_ID;
4327
4328        switch (btf_kind(t)) {
4329        case BTF_KIND_CONST:
4330        case BTF_KIND_VOLATILE:
4331        case BTF_KIND_RESTRICT:
4332        case BTF_KIND_PTR:
4333        case BTF_KIND_TYPEDEF:
4334        case BTF_KIND_FUNC:
4335                ref_type_id = btf_dedup_ref_type(d, t->type);
4336                if (ref_type_id < 0)
4337                        return ref_type_id;
4338                t->type = ref_type_id;
4339
4340                h = btf_hash_common(t);
4341                for_each_dedup_cand(d, hash_entry, h) {
4342                        cand_id = (__u32)(long)hash_entry->value;
4343                        cand = btf_type_by_id(d->btf, cand_id);
4344                        if (btf_equal_common(t, cand)) {
4345                                new_id = cand_id;
4346                                break;
4347                        }
4348                }
4349                break;
4350
4351        case BTF_KIND_ARRAY: {
4352                struct btf_array *info = btf_array(t);
4353
4354                ref_type_id = btf_dedup_ref_type(d, info->type);
4355                if (ref_type_id < 0)
4356                        return ref_type_id;
4357                info->type = ref_type_id;
4358
4359                ref_type_id = btf_dedup_ref_type(d, info->index_type);
4360                if (ref_type_id < 0)
4361                        return ref_type_id;
4362                info->index_type = ref_type_id;
4363
4364                h = btf_hash_array(t);
4365                for_each_dedup_cand(d, hash_entry, h) {
4366                        cand_id = (__u32)(long)hash_entry->value;
4367                        cand = btf_type_by_id(d->btf, cand_id);
4368                        if (btf_equal_array(t, cand)) {
4369                                new_id = cand_id;
4370                                break;
4371                        }
4372                }
4373                break;
4374        }
4375
4376        case BTF_KIND_FUNC_PROTO: {
4377                struct btf_param *param;
4378                __u16 vlen;
4379                int i;
4380
4381                ref_type_id = btf_dedup_ref_type(d, t->type);
4382                if (ref_type_id < 0)
4383                        return ref_type_id;
4384                t->type = ref_type_id;
4385
4386                vlen = btf_vlen(t);
4387                param = btf_params(t);
4388                for (i = 0; i < vlen; i++) {
4389                        ref_type_id = btf_dedup_ref_type(d, param->type);
4390                        if (ref_type_id < 0)
4391                                return ref_type_id;
4392                        param->type = ref_type_id;
4393                        param++;
4394                }
4395
4396                h = btf_hash_fnproto(t);
4397                for_each_dedup_cand(d, hash_entry, h) {
4398                        cand_id = (__u32)(long)hash_entry->value;
4399                        cand = btf_type_by_id(d->btf, cand_id);
4400                        if (btf_equal_fnproto(t, cand)) {
4401                                new_id = cand_id;
4402                                break;
4403                        }
4404                }
4405                break;
4406        }
4407
4408        default:
4409                return -EINVAL;
4410        }
4411
4412        d->map[type_id] = new_id;
4413        if (type_id == new_id && btf_dedup_table_add(d, h, type_id))
4414                return -ENOMEM;
4415
4416        return new_id;
4417}
4418
4419static int btf_dedup_ref_types(struct btf_dedup *d)
4420{
4421        int i, err;
4422
4423        for (i = 0; i < d->btf->nr_types; i++) {
4424                err = btf_dedup_ref_type(d, d->btf->start_id + i);
4425                if (err < 0)
4426                        return err;
4427        }
4428        /* we won't need d->dedup_table anymore */
4429        hashmap__free(d->dedup_table);
4430        d->dedup_table = NULL;
4431        return 0;
4432}
4433
4434/*
4435 * Compact types.
4436 *
4437 * After we established for each type its corresponding canonical representative
4438 * type, we now can eliminate types that are not canonical and leave only
4439 * canonical ones layed out sequentially in memory by copying them over
4440 * duplicates. During compaction btf_dedup->hypot_map array is reused to store
4441 * a map from original type ID to a new compacted type ID, which will be used
4442 * during next phase to "fix up" type IDs, referenced from struct/union and
4443 * reference types.
4444 */
4445static int btf_dedup_compact_types(struct btf_dedup *d)
4446{
4447        __u32 *new_offs;
4448        __u32 next_type_id = d->btf->start_id;
4449        const struct btf_type *t;
4450        void *p;
4451        int i, id, len;
4452
4453        /* we are going to reuse hypot_map to store compaction remapping */
4454        d->hypot_map[0] = 0;
4455        /* base BTF types are not renumbered */
4456        for (id = 1; id < d->btf->start_id; id++)
4457                d->hypot_map[id] = id;
4458        for (i = 0, id = d->btf->start_id; i < d->btf->nr_types; i++, id++)
4459                d->hypot_map[id] = BTF_UNPROCESSED_ID;
4460
4461        p = d->btf->types_data;
4462
4463        for (i = 0, id = d->btf->start_id; i < d->btf->nr_types; i++, id++) {
4464                if (d->map[id] != id)
4465                        continue;
4466
4467                t = btf__type_by_id(d->btf, id);
4468                len = btf_type_size(t);
4469                if (len < 0)
4470                        return len;
4471
4472                memmove(p, t, len);
4473                d->hypot_map[id] = next_type_id;
4474                d->btf->type_offs[next_type_id - d->btf->start_id] = p - d->btf->types_data;
4475                p += len;
4476                next_type_id++;
4477        }
4478
4479        /* shrink struct btf's internal types index and update btf_header */
4480        d->btf->nr_types = next_type_id - d->btf->start_id;
4481        d->btf->type_offs_cap = d->btf->nr_types;
4482        d->btf->hdr->type_len = p - d->btf->types_data;
4483        new_offs = libbpf_reallocarray(d->btf->type_offs, d->btf->type_offs_cap,
4484                                       sizeof(*new_offs));
4485        if (d->btf->type_offs_cap && !new_offs)
4486                return -ENOMEM;
4487        d->btf->type_offs = new_offs;
4488        d->btf->hdr->str_off = d->btf->hdr->type_len;
4489        d->btf->raw_size = d->btf->hdr->hdr_len + d->btf->hdr->type_len + d->btf->hdr->str_len;
4490        return 0;
4491}
4492
4493/*
4494 * Figure out final (deduplicated and compacted) type ID for provided original
4495 * `type_id` by first resolving it into corresponding canonical type ID and
4496 * then mapping it to a deduplicated type ID, stored in btf_dedup->hypot_map,
4497 * which is populated during compaction phase.
4498 */
4499static int btf_dedup_remap_type_id(struct btf_dedup *d, __u32 type_id)
4500{
4501        __u32 resolved_type_id, new_type_id;
4502
4503        resolved_type_id = resolve_type_id(d, type_id);
4504        new_type_id = d->hypot_map[resolved_type_id];
4505        if (new_type_id > BTF_MAX_NR_TYPES)
4506                return -EINVAL;
4507        return new_type_id;
4508}
4509
4510/*
4511 * Remap referenced type IDs into deduped type IDs.
4512 *
4513 * After BTF types are deduplicated and compacted, their final type IDs may
4514 * differ from original ones. The map from original to a corresponding
4515 * deduped type ID is stored in btf_dedup->hypot_map and is populated during
4516 * compaction phase. During remapping phase we are rewriting all type IDs
4517 * referenced from any BTF type (e.g., struct fields, func proto args, etc) to
4518 * their final deduped type IDs.
4519 */
4520static int btf_dedup_remap_type(struct btf_dedup *d, __u32 type_id)
4521{
4522        struct btf_type *t = btf_type_by_id(d->btf, type_id);
4523        int i, r;
4524
4525        switch (btf_kind(t)) {
4526        case BTF_KIND_INT:
4527        case BTF_KIND_ENUM:
4528        case BTF_KIND_FLOAT:
4529                break;
4530
4531        case BTF_KIND_FWD:
4532        case BTF_KIND_CONST:
4533        case BTF_KIND_VOLATILE:
4534        case BTF_KIND_RESTRICT:
4535        case BTF_KIND_PTR:
4536        case BTF_KIND_TYPEDEF:
4537        case BTF_KIND_FUNC:
4538        case BTF_KIND_VAR:
4539                r = btf_dedup_remap_type_id(d, t->type);
4540                if (r < 0)
4541                        return r;
4542                t->type = r;
4543                break;
4544
4545        case BTF_KIND_ARRAY: {
4546                struct btf_array *arr_info = btf_array(t);
4547
4548                r = btf_dedup_remap_type_id(d, arr_info->type);
4549                if (r < 0)
4550                        return r;
4551                arr_info->type = r;
4552                r = btf_dedup_remap_type_id(d, arr_info->index_type);
4553                if (r < 0)
4554                        return r;
4555                arr_info->index_type = r;
4556                break;
4557        }
4558
4559        case BTF_KIND_STRUCT:
4560        case BTF_KIND_UNION: {
4561                struct btf_member *member = btf_members(t);
4562                __u16 vlen = btf_vlen(t);
4563
4564                for (i = 0; i < vlen; i++) {
4565                        r = btf_dedup_remap_type_id(d, member->type);
4566                        if (r < 0)
4567                                return r;
4568                        member->type = r;
4569                        member++;
4570                }
4571                break;
4572        }
4573
4574        case BTF_KIND_FUNC_PROTO: {
4575                struct btf_param *param = btf_params(t);
4576                __u16 vlen = btf_vlen(t);
4577
4578                r = btf_dedup_remap_type_id(d, t->type);
4579                if (r < 0)
4580                        return r;
4581                t->type = r;
4582
4583                for (i = 0; i < vlen; i++) {
4584                        r = btf_dedup_remap_type_id(d, param->type);
4585                        if (r < 0)
4586                                return r;
4587                        param->type = r;
4588                        param++;
4589                }
4590                break;
4591        }
4592
4593        case BTF_KIND_DATASEC: {
4594                struct btf_var_secinfo *var = btf_var_secinfos(t);
4595                __u16 vlen = btf_vlen(t);
4596
4597                for (i = 0; i < vlen; i++) {
4598                        r = btf_dedup_remap_type_id(d, var->type);
4599                        if (r < 0)
4600                                return r;
4601                        var->type = r;
4602                        var++;
4603                }
4604                break;
4605        }
4606
4607        default:
4608                return -EINVAL;
4609        }
4610
4611        return 0;
4612}
4613
4614static int btf_dedup_remap_types(struct btf_dedup *d)
4615{
4616        int i, r;
4617
4618        for (i = 0; i < d->btf->nr_types; i++) {
4619                r = btf_dedup_remap_type(d, d->btf->start_id + i);
4620                if (r < 0)
4621                        return r;
4622        }
4623        return 0;
4624}
4625
4626/*
4627 * Probe few well-known locations for vmlinux kernel image and try to load BTF
4628 * data out of it to use for target BTF.
4629 */
4630struct btf *libbpf_find_kernel_btf(void)
4631{
4632        struct {
4633                const char *path_fmt;
4634                bool raw_btf;
4635        } locations[] = {
4636                /* try canonical vmlinux BTF through sysfs first */
4637                { "/sys/kernel/btf/vmlinux", true /* raw BTF */ },
4638                /* fall back to trying to find vmlinux ELF on disk otherwise */
4639                { "/boot/vmlinux-%1$s" },
4640                { "/lib/modules/%1$s/vmlinux-%1$s" },
4641                { "/lib/modules/%1$s/build/vmlinux" },
4642                { "/usr/lib/modules/%1$s/kernel/vmlinux" },
4643                { "/usr/lib/debug/boot/vmlinux-%1$s" },
4644                { "/usr/lib/debug/boot/vmlinux-%1$s.debug" },
4645                { "/usr/lib/debug/lib/modules/%1$s/vmlinux" },
4646        };
4647        char path[PATH_MAX + 1];
4648        struct utsname buf;
4649        struct btf *btf;
4650        int i;
4651
4652        uname(&buf);
4653
4654        for (i = 0; i < ARRAY_SIZE(locations); i++) {
4655                snprintf(path, PATH_MAX, locations[i].path_fmt, buf.release);
4656
4657                if (access(path, R_OK))
4658                        continue;
4659
4660                if (locations[i].raw_btf)
4661                        btf = btf__parse_raw(path);
4662                else
4663                        btf = btf__parse_elf(path, NULL);
4664
4665                pr_debug("loading kernel BTF '%s': %ld\n",
4666                         path, IS_ERR(btf) ? PTR_ERR(btf) : 0);
4667                if (IS_ERR(btf))
4668                        continue;
4669
4670                return btf;
4671        }
4672
4673        pr_warn("failed to find valid kernel BTF\n");
4674        return ERR_PTR(-ESRCH);
4675}
4676