btf.c (2b1333b80885b896807ffb6ccf4bc21d29aa65e0) btf.c (aaf6886d9b53d34f0a3d2f577ddc1224026d12ab)
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>

--- 116 unchanged lines hidden (view full) ---

125
126static inline __u64 ptr_to_u64(const void *ptr)
127{
128 return (__u64) (unsigned long) ptr;
129}
130
131/* Ensure given dynamically allocated memory region pointed to by *data* with
132 * capacity of *cap_cnt* elements each taking *elem_sz* bytes has enough
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>

--- 116 unchanged lines hidden (view full) ---

125
126static inline __u64 ptr_to_u64(const void *ptr)
127{
128 return (__u64) (unsigned long) ptr;
129}
130
131/* Ensure given dynamically allocated memory region pointed to by *data* with
132 * capacity of *cap_cnt* elements each taking *elem_sz* bytes has enough
133 * memory to accomodate *add_cnt* new elements, assuming *cur_cnt* elements
133 * memory to accommodate *add_cnt* new elements, assuming *cur_cnt* elements
134 * are already used. At most *max_cnt* elements can be ever allocated.
135 * If necessary, memory is reallocated and all existing data is copied over,
136 * new pointer to the memory region is stored at *data, new memory region
137 * capacity (in number of elements) is stored in *cap.
138 * On success, memory pointer to the beginning of unused memory is returned.
139 * On error, NULL is returned.
140 */
141void *libbpf_add_mem(void **data, size_t *cap_cnt, size_t elem_sz,

--- 158 unchanged lines hidden (view full) ---

300 case BTF_KIND_FUNC:
301 case BTF_KIND_FLOAT:
302 case BTF_KIND_TYPE_TAG:
303 return base_size;
304 case BTF_KIND_INT:
305 return base_size + sizeof(__u32);
306 case BTF_KIND_ENUM:
307 return base_size + vlen * sizeof(struct btf_enum);
134 * are already used. At most *max_cnt* elements can be ever allocated.
135 * If necessary, memory is reallocated and all existing data is copied over,
136 * new pointer to the memory region is stored at *data, new memory region
137 * capacity (in number of elements) is stored in *cap.
138 * On success, memory pointer to the beginning of unused memory is returned.
139 * On error, NULL is returned.
140 */
141void *libbpf_add_mem(void **data, size_t *cap_cnt, size_t elem_sz,

--- 158 unchanged lines hidden (view full) ---

300 case BTF_KIND_FUNC:
301 case BTF_KIND_FLOAT:
302 case BTF_KIND_TYPE_TAG:
303 return base_size;
304 case BTF_KIND_INT:
305 return base_size + sizeof(__u32);
306 case BTF_KIND_ENUM:
307 return base_size + vlen * sizeof(struct btf_enum);
308 case BTF_KIND_ENUM64:
309 return base_size + vlen * sizeof(struct btf_enum64);
308 case BTF_KIND_ARRAY:
309 return base_size + sizeof(struct btf_array);
310 case BTF_KIND_STRUCT:
311 case BTF_KIND_UNION:
312 return base_size + vlen * sizeof(struct btf_member);
313 case BTF_KIND_FUNC_PROTO:
314 return base_size + vlen * sizeof(struct btf_param);
315 case BTF_KIND_VAR:

--- 13 unchanged lines hidden (view full) ---

329 t->name_off = bswap_32(t->name_off);
330 t->info = bswap_32(t->info);
331 t->type = bswap_32(t->type);
332}
333
334static int btf_bswap_type_rest(struct btf_type *t)
335{
336 struct btf_var_secinfo *v;
310 case BTF_KIND_ARRAY:
311 return base_size + sizeof(struct btf_array);
312 case BTF_KIND_STRUCT:
313 case BTF_KIND_UNION:
314 return base_size + vlen * sizeof(struct btf_member);
315 case BTF_KIND_FUNC_PROTO:
316 return base_size + vlen * sizeof(struct btf_param);
317 case BTF_KIND_VAR:

--- 13 unchanged lines hidden (view full) ---

331 t->name_off = bswap_32(t->name_off);
332 t->info = bswap_32(t->info);
333 t->type = bswap_32(t->type);
334}
335
336static int btf_bswap_type_rest(struct btf_type *t)
337{
338 struct btf_var_secinfo *v;
339 struct btf_enum64 *e64;
337 struct btf_member *m;
338 struct btf_array *a;
339 struct btf_param *p;
340 struct btf_enum *e;
341 __u16 vlen = btf_vlen(t);
342 int i;
343
344 switch (btf_kind(t)) {

--- 11 unchanged lines hidden (view full) ---

356 *(__u32 *)(t + 1) = bswap_32(*(__u32 *)(t + 1));
357 return 0;
358 case BTF_KIND_ENUM:
359 for (i = 0, e = btf_enum(t); i < vlen; i++, e++) {
360 e->name_off = bswap_32(e->name_off);
361 e->val = bswap_32(e->val);
362 }
363 return 0;
340 struct btf_member *m;
341 struct btf_array *a;
342 struct btf_param *p;
343 struct btf_enum *e;
344 __u16 vlen = btf_vlen(t);
345 int i;
346
347 switch (btf_kind(t)) {

--- 11 unchanged lines hidden (view full) ---

359 *(__u32 *)(t + 1) = bswap_32(*(__u32 *)(t + 1));
360 return 0;
361 case BTF_KIND_ENUM:
362 for (i = 0, e = btf_enum(t); i < vlen; i++, e++) {
363 e->name_off = bswap_32(e->name_off);
364 e->val = bswap_32(e->val);
365 }
366 return 0;
367 case BTF_KIND_ENUM64:
368 for (i = 0, e64 = btf_enum64(t); i < vlen; i++, e64++) {
369 e64->name_off = bswap_32(e64->name_off);
370 e64->val_lo32 = bswap_32(e64->val_lo32);
371 e64->val_hi32 = bswap_32(e64->val_hi32);
372 }
373 return 0;
364 case BTF_KIND_ARRAY:
365 a = btf_array(t);
366 a->type = bswap_32(a->type);
367 a->index_type = bswap_32(a->index_type);
368 a->nelems = bswap_32(a->nelems);
369 return 0;
370 case BTF_KIND_STRUCT:
371 case BTF_KIND_UNION:

--- 61 unchanged lines hidden (view full) ---

433 if (next_type != end_type) {
434 pr_warn("BTF types data is malformed\n");
435 return -EINVAL;
436 }
437
438 return 0;
439}
440
374 case BTF_KIND_ARRAY:
375 a = btf_array(t);
376 a->type = bswap_32(a->type);
377 a->index_type = bswap_32(a->index_type);
378 a->nelems = bswap_32(a->nelems);
379 return 0;
380 case BTF_KIND_STRUCT:
381 case BTF_KIND_UNION:

--- 61 unchanged lines hidden (view full) ---

443 if (next_type != end_type) {
444 pr_warn("BTF types data is malformed\n");
445 return -EINVAL;
446 }
447
448 return 0;
449}
450
441__u32 btf__get_nr_types(const struct btf *btf)
442{
443 return btf->start_id + btf->nr_types - 1;
444}
445
446__u32 btf__type_cnt(const struct btf *btf)
447{
448 return btf->start_id + btf->nr_types;
449}
450
451const struct btf *btf__base_btf(const struct btf *btf)
452{
453 return btf->base_btf;

--- 13 unchanged lines hidden (view full) ---

467{
468 if (type_id >= btf->start_id + btf->nr_types)
469 return errno = EINVAL, NULL;
470 return btf_type_by_id((struct btf *)btf, type_id);
471}
472
473static int determine_ptr_size(const struct btf *btf)
474{
451__u32 btf__type_cnt(const struct btf *btf)
452{
453 return btf->start_id + btf->nr_types;
454}
455
456const struct btf *btf__base_btf(const struct btf *btf)
457{
458 return btf->base_btf;

--- 13 unchanged lines hidden (view full) ---

472{
473 if (type_id >= btf->start_id + btf->nr_types)
474 return errno = EINVAL, NULL;
475 return btf_type_by_id((struct btf *)btf, type_id);
476}
477
478static int determine_ptr_size(const struct btf *btf)
479{
480 static const char * const long_aliases[] = {
481 "long",
482 "long int",
483 "int long",
484 "unsigned long",
485 "long unsigned",
486 "unsigned long int",
487 "unsigned int long",
488 "long unsigned int",
489 "long int unsigned",
490 "int unsigned long",
491 "int long unsigned",
492 };
475 const struct btf_type *t;
476 const char *name;
493 const struct btf_type *t;
494 const char *name;
477 int i, n;
495 int i, j, n;
478
479 if (btf->base_btf && btf->base_btf->ptr_sz > 0)
480 return btf->base_btf->ptr_sz;
481
482 n = btf__type_cnt(btf);
483 for (i = 1; i < n; i++) {
484 t = btf__type_by_id(btf, i);
485 if (!btf_is_int(t))
486 continue;
487
496
497 if (btf->base_btf && btf->base_btf->ptr_sz > 0)
498 return btf->base_btf->ptr_sz;
499
500 n = btf__type_cnt(btf);
501 for (i = 1; i < n; i++) {
502 t = btf__type_by_id(btf, i);
503 if (!btf_is_int(t))
504 continue;
505
506 if (t->size != 4 && t->size != 8)
507 continue;
508
488 name = btf__name_by_offset(btf, t->name_off);
489 if (!name)
490 continue;
491
509 name = btf__name_by_offset(btf, t->name_off);
510 if (!name)
511 continue;
512
492 if (strcmp(name, "long int") == 0 ||
493 strcmp(name, "long unsigned int") == 0) {
494 if (t->size != 4 && t->size != 8)
495 continue;
496 return t->size;
513 for (j = 0; j < ARRAY_SIZE(long_aliases); j++) {
514 if (strcmp(name, long_aliases[j]) == 0)
515 return t->size;
497 }
498 }
499
500 return -1;
501}
502
503static size_t btf_ptr_sz(const struct btf *btf)
504{

--- 87 unchanged lines hidden (view full) ---

592
593 t = btf__type_by_id(btf, type_id);
594 for (i = 0; i < MAX_RESOLVE_DEPTH && !btf_type_is_void_or_null(t); i++) {
595 switch (btf_kind(t)) {
596 case BTF_KIND_INT:
597 case BTF_KIND_STRUCT:
598 case BTF_KIND_UNION:
599 case BTF_KIND_ENUM:
516 }
517 }
518
519 return -1;
520}
521
522static size_t btf_ptr_sz(const struct btf *btf)
523{

--- 87 unchanged lines hidden (view full) ---

611
612 t = btf__type_by_id(btf, type_id);
613 for (i = 0; i < MAX_RESOLVE_DEPTH && !btf_type_is_void_or_null(t); i++) {
614 switch (btf_kind(t)) {
615 case BTF_KIND_INT:
616 case BTF_KIND_STRUCT:
617 case BTF_KIND_UNION:
618 case BTF_KIND_ENUM:
619 case BTF_KIND_ENUM64:
600 case BTF_KIND_DATASEC:
601 case BTF_KIND_FLOAT:
602 size = t->size;
603 goto done;
604 case BTF_KIND_PTR:
605 size = btf_ptr_sz(btf);
606 goto done;
607 case BTF_KIND_TYPEDEF:

--- 31 unchanged lines hidden (view full) ---

639int btf__align_of(const struct btf *btf, __u32 id)
640{
641 const struct btf_type *t = btf__type_by_id(btf, id);
642 __u16 kind = btf_kind(t);
643
644 switch (kind) {
645 case BTF_KIND_INT:
646 case BTF_KIND_ENUM:
620 case BTF_KIND_DATASEC:
621 case BTF_KIND_FLOAT:
622 size = t->size;
623 goto done;
624 case BTF_KIND_PTR:
625 size = btf_ptr_sz(btf);
626 goto done;
627 case BTF_KIND_TYPEDEF:

--- 31 unchanged lines hidden (view full) ---

659int btf__align_of(const struct btf *btf, __u32 id)
660{
661 const struct btf_type *t = btf__type_by_id(btf, id);
662 __u16 kind = btf_kind(t);
663
664 switch (kind) {
665 case BTF_KIND_INT:
666 case BTF_KIND_ENUM:
667 case BTF_KIND_ENUM64:
647 case BTF_KIND_FLOAT:
648 return min(btf_ptr_sz(btf), (size_t)t->size);
649 case BTF_KIND_PTR:
650 return btf_ptr_sz(btf);
651 case BTF_KIND_TYPEDEF:
652 case BTF_KIND_VOLATILE:
653 case BTF_KIND_CONST:
654 case BTF_KIND_RESTRICT:

--- 722 unchanged lines hidden (view full) ---

1377 return libbpf_ptr(btf);
1378}
1379
1380struct btf *btf__load_from_kernel_by_id(__u32 id)
1381{
1382 return btf__load_from_kernel_by_id_split(id, NULL);
1383}
1384
668 case BTF_KIND_FLOAT:
669 return min(btf_ptr_sz(btf), (size_t)t->size);
670 case BTF_KIND_PTR:
671 return btf_ptr_sz(btf);
672 case BTF_KIND_TYPEDEF:
673 case BTF_KIND_VOLATILE:
674 case BTF_KIND_CONST:
675 case BTF_KIND_RESTRICT:

--- 722 unchanged lines hidden (view full) ---

1398 return libbpf_ptr(btf);
1399}
1400
1401struct btf *btf__load_from_kernel_by_id(__u32 id)
1402{
1403 return btf__load_from_kernel_by_id_split(id, NULL);
1404}
1405
1385int btf__get_from_id(__u32 id, struct btf **btf)
1386{
1387 struct btf *res;
1388 int err;
1389
1390 *btf = NULL;
1391 res = btf__load_from_kernel_by_id(id);
1392 err = libbpf_get_error(res);
1393
1394 if (err)
1395 return libbpf_err(err);
1396
1397 *btf = res;
1398 return 0;
1399}
1400
1401int btf__get_map_kv_tids(const struct btf *btf, const char *map_name,
1402 __u32 expected_key_size, __u32 expected_value_size,
1403 __u32 *key_type_id, __u32 *value_type_id)
1404{
1405 const struct btf_type *container_type;
1406 const struct btf_member *key, *value;
1407 const size_t max_name = 256;
1408 char container_name[max_name];
1409 __s64 key_size, value_size;
1410 __s32 container_id;
1411
1412 if (snprintf(container_name, max_name, "____btf_map_%s", map_name) == max_name) {
1413 pr_warn("map:%s length of '____btf_map_%s' is too long\n",
1414 map_name, map_name);
1415 return libbpf_err(-EINVAL);
1416 }
1417
1418 container_id = btf__find_by_name(btf, container_name);
1419 if (container_id < 0) {
1420 pr_debug("map:%s container_name:%s cannot be found in BTF. Missing BPF_ANNOTATE_KV_PAIR?\n",
1421 map_name, container_name);
1422 return libbpf_err(container_id);
1423 }
1424
1425 container_type = btf__type_by_id(btf, container_id);
1426 if (!container_type) {
1427 pr_warn("map:%s cannot find BTF type for container_id:%u\n",
1428 map_name, container_id);
1429 return libbpf_err(-EINVAL);
1430 }
1431
1432 if (!btf_is_struct(container_type) || btf_vlen(container_type) < 2) {
1433 pr_warn("map:%s container_name:%s is an invalid container struct\n",
1434 map_name, container_name);
1435 return libbpf_err(-EINVAL);
1436 }
1437
1438 key = btf_members(container_type);
1439 value = key + 1;
1440
1441 key_size = btf__resolve_size(btf, key->type);
1442 if (key_size < 0) {
1443 pr_warn("map:%s invalid BTF key_type_size\n", map_name);
1444 return libbpf_err(key_size);
1445 }
1446
1447 if (expected_key_size != key_size) {
1448 pr_warn("map:%s btf_key_type_size:%u != map_def_key_size:%u\n",
1449 map_name, (__u32)key_size, expected_key_size);
1450 return libbpf_err(-EINVAL);
1451 }
1452
1453 value_size = btf__resolve_size(btf, value->type);
1454 if (value_size < 0) {
1455 pr_warn("map:%s invalid BTF value_type_size\n", map_name);
1456 return libbpf_err(value_size);
1457 }
1458
1459 if (expected_value_size != value_size) {
1460 pr_warn("map:%s btf_value_type_size:%u != map_def_value_size:%u\n",
1461 map_name, (__u32)value_size, expected_value_size);
1462 return libbpf_err(-EINVAL);
1463 }
1464
1465 *key_type_id = key->type;
1466 *value_type_id = value->type;
1467
1468 return 0;
1469}
1470
1471static void btf_invalidate_raw_data(struct btf *btf)
1472{
1473 if (btf->raw_data) {
1474 free(btf->raw_data);
1475 btf->raw_data = NULL;
1476 }
1477 if (btf->raw_data_swapped) {
1478 free(btf->raw_data_swapped);

--- 631 unchanged lines hidden (view full) ---

2110 /* update parent type's vlen and kflag */
2111 t->info = btf_type_info(btf_kind(t), btf_vlen(t) + 1, is_bitfield || btf_kflag(t));
2112
2113 btf->hdr->type_len += sz;
2114 btf->hdr->str_off += sz;
2115 return 0;
2116}
2117
1406static void btf_invalidate_raw_data(struct btf *btf)
1407{
1408 if (btf->raw_data) {
1409 free(btf->raw_data);
1410 btf->raw_data = NULL;
1411 }
1412 if (btf->raw_data_swapped) {
1413 free(btf->raw_data_swapped);

--- 631 unchanged lines hidden (view full) ---

2045 /* update parent type's vlen and kflag */
2046 t->info = btf_type_info(btf_kind(t), btf_vlen(t) + 1, is_bitfield || btf_kflag(t));
2047
2048 btf->hdr->type_len += sz;
2049 btf->hdr->str_off += sz;
2050 return 0;
2051}
2052
2118/*
2119 * Append new BTF_KIND_ENUM type with:
2120 * - *name* - name of the enum, can be NULL or empty for anonymous enums;
2121 * - *byte_sz* - size of the enum, in bytes.
2122 *
2123 * Enum initially has no enum values in it (and corresponds to enum forward
2124 * declaration). Enumerator values can be added by btf__add_enum_value()
2125 * immediately after btf__add_enum() succeeds.
2126 *
2127 * Returns:
2128 * - >0, type ID of newly added BTF type;
2129 * - <0, on error.
2130 */
2131int btf__add_enum(struct btf *btf, const char *name, __u32 byte_sz)
2053static int btf_add_enum_common(struct btf *btf, const char *name, __u32 byte_sz,
2054 bool is_signed, __u8 kind)
2132{
2133 struct btf_type *t;
2134 int sz, name_off = 0;
2135
2136 /* byte_sz must be power of 2 */
2137 if (!byte_sz || (byte_sz & (byte_sz - 1)) || byte_sz > 8)
2138 return libbpf_err(-EINVAL);
2139

--- 8 unchanged lines hidden (view full) ---

2148 if (name && name[0]) {
2149 name_off = btf__add_str(btf, name);
2150 if (name_off < 0)
2151 return name_off;
2152 }
2153
2154 /* start out with vlen=0; it will be adjusted when adding enum values */
2155 t->name_off = name_off;
2055{
2056 struct btf_type *t;
2057 int sz, name_off = 0;
2058
2059 /* byte_sz must be power of 2 */
2060 if (!byte_sz || (byte_sz & (byte_sz - 1)) || byte_sz > 8)
2061 return libbpf_err(-EINVAL);
2062

--- 8 unchanged lines hidden (view full) ---

2071 if (name && name[0]) {
2072 name_off = btf__add_str(btf, name);
2073 if (name_off < 0)
2074 return name_off;
2075 }
2076
2077 /* start out with vlen=0; it will be adjusted when adding enum values */
2078 t->name_off = name_off;
2156 t->info = btf_type_info(BTF_KIND_ENUM, 0, 0);
2079 t->info = btf_type_info(kind, 0, is_signed);
2157 t->size = byte_sz;
2158
2159 return btf_commit_type(btf, sz);
2160}
2161
2162/*
2080 t->size = byte_sz;
2081
2082 return btf_commit_type(btf, sz);
2083}
2084
2085/*
2086 * Append new BTF_KIND_ENUM type with:
2087 * - *name* - name of the enum, can be NULL or empty for anonymous enums;
2088 * - *byte_sz* - size of the enum, in bytes.
2089 *
2090 * Enum initially has no enum values in it (and corresponds to enum forward
2091 * declaration). Enumerator values can be added by btf__add_enum_value()
2092 * immediately after btf__add_enum() succeeds.
2093 *
2094 * Returns:
2095 * - >0, type ID of newly added BTF type;
2096 * - <0, on error.
2097 */
2098int btf__add_enum(struct btf *btf, const char *name, __u32 byte_sz)
2099{
2100 /*
2101 * set the signedness to be unsigned, it will change to signed
2102 * if any later enumerator is negative.
2103 */
2104 return btf_add_enum_common(btf, name, byte_sz, false, BTF_KIND_ENUM);
2105}
2106
2107/*
2163 * Append new enum value for the current ENUM type with:
2164 * - *name* - name of the enumerator value, can't be NULL or empty;
2165 * - *value* - integer value corresponding to enum value *name*;
2166 * Returns:
2167 * - 0, on success;
2168 * - <0, on error.
2169 */
2170int btf__add_enum_value(struct btf *btf, const char *name, __s64 value)

--- 30 unchanged lines hidden (view full) ---

2201
2202 v->name_off = name_off;
2203 v->val = value;
2204
2205 /* update parent type's vlen */
2206 t = btf_last_type(btf);
2207 btf_type_inc_vlen(t);
2208
2108 * Append new enum value for the current ENUM type with:
2109 * - *name* - name of the enumerator value, can't be NULL or empty;
2110 * - *value* - integer value corresponding to enum value *name*;
2111 * Returns:
2112 * - 0, on success;
2113 * - <0, on error.
2114 */
2115int btf__add_enum_value(struct btf *btf, const char *name, __s64 value)

--- 30 unchanged lines hidden (view full) ---

2146
2147 v->name_off = name_off;
2148 v->val = value;
2149
2150 /* update parent type's vlen */
2151 t = btf_last_type(btf);
2152 btf_type_inc_vlen(t);
2153
2154 /* if negative value, set signedness to signed */
2155 if (value < 0)
2156 t->info = btf_type_info(btf_kind(t), btf_vlen(t), true);
2157
2209 btf->hdr->type_len += sz;
2210 btf->hdr->str_off += sz;
2211 return 0;
2212}
2213
2214/*
2158 btf->hdr->type_len += sz;
2159 btf->hdr->str_off += sz;
2160 return 0;
2161}
2162
2163/*
2164 * Append new BTF_KIND_ENUM64 type with:
2165 * - *name* - name of the enum, can be NULL or empty for anonymous enums;
2166 * - *byte_sz* - size of the enum, in bytes.
2167 * - *is_signed* - whether the enum values are signed or not;
2168 *
2169 * Enum initially has no enum values in it (and corresponds to enum forward
2170 * declaration). Enumerator values can be added by btf__add_enum64_value()
2171 * immediately after btf__add_enum64() succeeds.
2172 *
2173 * Returns:
2174 * - >0, type ID of newly added BTF type;
2175 * - <0, on error.
2176 */
2177int btf__add_enum64(struct btf *btf, const char *name, __u32 byte_sz,
2178 bool is_signed)
2179{
2180 return btf_add_enum_common(btf, name, byte_sz, is_signed,
2181 BTF_KIND_ENUM64);
2182}
2183
2184/*
2185 * Append new enum value for the current ENUM64 type with:
2186 * - *name* - name of the enumerator value, can't be NULL or empty;
2187 * - *value* - integer value corresponding to enum value *name*;
2188 * Returns:
2189 * - 0, on success;
2190 * - <0, on error.
2191 */
2192int btf__add_enum64_value(struct btf *btf, const char *name, __u64 value)
2193{
2194 struct btf_enum64 *v;
2195 struct btf_type *t;
2196 int sz, name_off;
2197
2198 /* last type should be BTF_KIND_ENUM64 */
2199 if (btf->nr_types == 0)
2200 return libbpf_err(-EINVAL);
2201 t = btf_last_type(btf);
2202 if (!btf_is_enum64(t))
2203 return libbpf_err(-EINVAL);
2204
2205 /* non-empty name */
2206 if (!name || !name[0])
2207 return libbpf_err(-EINVAL);
2208
2209 /* decompose and invalidate raw data */
2210 if (btf_ensure_modifiable(btf))
2211 return libbpf_err(-ENOMEM);
2212
2213 sz = sizeof(struct btf_enum64);
2214 v = btf_add_type_mem(btf, sz);
2215 if (!v)
2216 return libbpf_err(-ENOMEM);
2217
2218 name_off = btf__add_str(btf, name);
2219 if (name_off < 0)
2220 return name_off;
2221
2222 v->name_off = name_off;
2223 v->val_lo32 = (__u32)value;
2224 v->val_hi32 = value >> 32;
2225
2226 /* update parent type's vlen */
2227 t = btf_last_type(btf);
2228 btf_type_inc_vlen(t);
2229
2230 btf->hdr->type_len += sz;
2231 btf->hdr->str_off += sz;
2232 return 0;
2233}
2234
2235/*
2215 * Append new BTF_KIND_FWD type with:
2216 * - *name*, non-empty/non-NULL name;
2217 * - *fwd_kind*, kind of forward declaration, one of BTF_FWD_STRUCT,
2218 * BTF_FWD_UNION, or BTF_FWD_ENUM;
2219 * Returns:
2220 * - >0, type ID of newly added BTF type;
2221 * - <0, on error.
2222 */

--- 625 unchanged lines hidden (view full) ---

2848}
2849
2850const void *btf_ext__get_raw_data(const struct btf_ext *btf_ext, __u32 *size)
2851{
2852 *size = btf_ext->data_size;
2853 return btf_ext->data;
2854}
2855
2236 * Append new BTF_KIND_FWD type with:
2237 * - *name*, non-empty/non-NULL name;
2238 * - *fwd_kind*, kind of forward declaration, one of BTF_FWD_STRUCT,
2239 * BTF_FWD_UNION, or BTF_FWD_ENUM;
2240 * Returns:
2241 * - >0, type ID of newly added BTF type;
2242 * - <0, on error.
2243 */

--- 625 unchanged lines hidden (view full) ---

2869}
2870
2871const void *btf_ext__get_raw_data(const struct btf_ext *btf_ext, __u32 *size)
2872{
2873 *size = btf_ext->data_size;
2874 return btf_ext->data;
2875}
2876
2856static int btf_ext_reloc_info(const struct btf *btf,
2857 const struct btf_ext_info *ext_info,
2858 const char *sec_name, __u32 insns_cnt,
2859 void **info, __u32 *cnt)
2860{
2861 __u32 sec_hdrlen = sizeof(struct btf_ext_info_sec);
2862 __u32 i, record_size, existing_len, records_len;
2863 struct btf_ext_info_sec *sinfo;
2864 const char *info_sec_name;
2865 __u64 remain_len;
2866 void *data;
2867
2868 record_size = ext_info->rec_size;
2869 sinfo = ext_info->info;
2870 remain_len = ext_info->len;
2871 while (remain_len > 0) {
2872 records_len = sinfo->num_info * record_size;
2873 info_sec_name = btf__name_by_offset(btf, sinfo->sec_name_off);
2874 if (strcmp(info_sec_name, sec_name)) {
2875 remain_len -= sec_hdrlen + records_len;
2876 sinfo = (void *)sinfo + sec_hdrlen + records_len;
2877 continue;
2878 }
2879
2880 existing_len = (*cnt) * record_size;
2881 data = realloc(*info, existing_len + records_len);
2882 if (!data)
2883 return libbpf_err(-ENOMEM);
2884
2885 memcpy(data + existing_len, sinfo->data, records_len);
2886 /* adjust insn_off only, the rest data will be passed
2887 * to the kernel.
2888 */
2889 for (i = 0; i < sinfo->num_info; i++) {
2890 __u32 *insn_off;
2891
2892 insn_off = data + existing_len + (i * record_size);
2893 *insn_off = *insn_off / sizeof(struct bpf_insn) + insns_cnt;
2894 }
2895 *info = data;
2896 *cnt += sinfo->num_info;
2897 return 0;
2898 }
2899
2900 return libbpf_err(-ENOENT);
2901}
2902
2903int btf_ext__reloc_func_info(const struct btf *btf,
2904 const struct btf_ext *btf_ext,
2905 const char *sec_name, __u32 insns_cnt,
2906 void **func_info, __u32 *cnt)
2907{
2908 return btf_ext_reloc_info(btf, &btf_ext->func_info, sec_name,
2909 insns_cnt, func_info, cnt);
2910}
2911
2912int btf_ext__reloc_line_info(const struct btf *btf,
2913 const struct btf_ext *btf_ext,
2914 const char *sec_name, __u32 insns_cnt,
2915 void **line_info, __u32 *cnt)
2916{
2917 return btf_ext_reloc_info(btf, &btf_ext->line_info, sec_name,
2918 insns_cnt, line_info, cnt);
2919}
2920
2921__u32 btf_ext__func_info_rec_size(const struct btf_ext *btf_ext)
2922{
2923 return btf_ext->func_info.rec_size;
2924}
2925
2926__u32 btf_ext__line_info_rec_size(const struct btf_ext *btf_ext)
2927{
2928 return btf_ext->line_info.rec_size;
2929}
2930
2931struct btf_dedup;
2932
2933static struct btf_dedup *btf_dedup_new(struct btf *btf, const struct btf_dedup_opts *opts);
2934static void btf_dedup_free(struct btf_dedup *d);
2935static int btf_dedup_prep(struct btf_dedup *d);
2936static int btf_dedup_strings(struct btf_dedup *d);
2937static int btf_dedup_prim_types(struct btf_dedup *d);
2938static int btf_dedup_struct_types(struct btf_dedup *d);

--- 133 unchanged lines hidden (view full) ---

3072 * hashing function, we can limit number of canonical types for each unique type
3073 * signature to a very small number, allowing to find canonical type for any
3074 * duplicated type very quickly.
3075 *
3076 * Struct/union deduplication is the most critical part and algorithm for
3077 * deduplicating structs/unions is described in greater details in comments for
3078 * `btf_dedup_is_equiv` function.
3079 */
2877struct btf_dedup;
2878
2879static struct btf_dedup *btf_dedup_new(struct btf *btf, const struct btf_dedup_opts *opts);
2880static void btf_dedup_free(struct btf_dedup *d);
2881static int btf_dedup_prep(struct btf_dedup *d);
2882static int btf_dedup_strings(struct btf_dedup *d);
2883static int btf_dedup_prim_types(struct btf_dedup *d);
2884static int btf_dedup_struct_types(struct btf_dedup *d);

--- 133 unchanged lines hidden (view full) ---

3018 * hashing function, we can limit number of canonical types for each unique type
3019 * signature to a very small number, allowing to find canonical type for any
3020 * duplicated type very quickly.
3021 *
3022 * Struct/union deduplication is the most critical part and algorithm for
3023 * deduplicating structs/unions is described in greater details in comments for
3024 * `btf_dedup_is_equiv` function.
3025 */
3080
3081DEFAULT_VERSION(btf__dedup_v0_6_0, btf__dedup, LIBBPF_0.6.0)
3082int btf__dedup_v0_6_0(struct btf *btf, const struct btf_dedup_opts *opts)
3026int btf__dedup(struct btf *btf, const struct btf_dedup_opts *opts)
3083{
3084 struct btf_dedup *d;
3085 int err;
3086
3087 if (!OPTS_VALID(opts, btf_dedup_opts))
3088 return libbpf_err(-EINVAL);
3089
3090 d = btf_dedup_new(btf, opts);

--- 43 unchanged lines hidden (view full) ---

3134 goto done;
3135 }
3136
3137done:
3138 btf_dedup_free(d);
3139 return libbpf_err(err);
3140}
3141
3027{
3028 struct btf_dedup *d;
3029 int err;
3030
3031 if (!OPTS_VALID(opts, btf_dedup_opts))
3032 return libbpf_err(-EINVAL);
3033
3034 d = btf_dedup_new(btf, opts);

--- 43 unchanged lines hidden (view full) ---

3078 goto done;
3079 }
3080
3081done:
3082 btf_dedup_free(d);
3083 return libbpf_err(err);
3084}
3085
3142COMPAT_VERSION(btf__dedup_deprecated, btf__dedup, LIBBPF_0.0.2)
3143int btf__dedup_deprecated(struct btf *btf, struct btf_ext *btf_ext, const void *unused_opts)
3144{
3145 LIBBPF_OPTS(btf_dedup_opts, opts, .btf_ext = btf_ext);
3146
3147 if (unused_opts) {
3148 pr_warn("please use new version of btf__dedup() that supports options\n");
3149 return libbpf_err(-ENOTSUP);
3150 }
3151
3152 return btf__dedup(btf, &opts);
3153}
3154
3155#define BTF_UNPROCESSED_ID ((__u32)-1)
3156#define BTF_IN_PROGRESS_ID ((__u32)-2)
3157
3158struct btf_dedup {
3159 /* .BTF section to be deduped in-place */
3160 struct btf *btf;
3161 /*
3162 * Optional .BTF.ext section. When provided, any strings referenced

--- 302 unchanged lines hidden (view full) ---

3465
3466 if (!btf_equal_common(t1, t2))
3467 return false;
3468 info1 = *(__u32 *)(t1 + 1);
3469 info2 = *(__u32 *)(t2 + 1);
3470 return info1 == info2;
3471}
3472
3086#define BTF_UNPROCESSED_ID ((__u32)-1)
3087#define BTF_IN_PROGRESS_ID ((__u32)-2)
3088
3089struct btf_dedup {
3090 /* .BTF section to be deduped in-place */
3091 struct btf *btf;
3092 /*
3093 * Optional .BTF.ext section. When provided, any strings referenced

--- 302 unchanged lines hidden (view full) ---

3396
3397 if (!btf_equal_common(t1, t2))
3398 return false;
3399 info1 = *(__u32 *)(t1 + 1);
3400 info2 = *(__u32 *)(t2 + 1);
3401 return info1 == info2;
3402}
3403
3473/* Calculate type signature hash of ENUM. */
3404/* Calculate type signature hash of ENUM/ENUM64. */
3474static long btf_hash_enum(struct btf_type *t)
3475{
3476 long h;
3477
3478 /* don't hash vlen and enum members to support enum fwd resolving */
3479 h = hash_combine(0, t->name_off);
3480 h = hash_combine(h, t->info & ~0xffff);
3481 h = hash_combine(h, t->size);

--- 17 unchanged lines hidden (view full) ---

3499 if (m1->name_off != m2->name_off || m1->val != m2->val)
3500 return false;
3501 m1++;
3502 m2++;
3503 }
3504 return true;
3505}
3506
3405static long btf_hash_enum(struct btf_type *t)
3406{
3407 long h;
3408
3409 /* don't hash vlen and enum members to support enum fwd resolving */
3410 h = hash_combine(0, t->name_off);
3411 h = hash_combine(h, t->info & ~0xffff);
3412 h = hash_combine(h, t->size);

--- 17 unchanged lines hidden (view full) ---

3430 if (m1->name_off != m2->name_off || m1->val != m2->val)
3431 return false;
3432 m1++;
3433 m2++;
3434 }
3435 return true;
3436}
3437
3438static bool btf_equal_enum64(struct btf_type *t1, struct btf_type *t2)
3439{
3440 const struct btf_enum64 *m1, *m2;
3441 __u16 vlen;
3442 int i;
3443
3444 if (!btf_equal_common(t1, t2))
3445 return false;
3446
3447 vlen = btf_vlen(t1);
3448 m1 = btf_enum64(t1);
3449 m2 = btf_enum64(t2);
3450 for (i = 0; i < vlen; i++) {
3451 if (m1->name_off != m2->name_off || m1->val_lo32 != m2->val_lo32 ||
3452 m1->val_hi32 != m2->val_hi32)
3453 return false;
3454 m1++;
3455 m2++;
3456 }
3457 return true;
3458}
3459
3507static inline bool btf_is_enum_fwd(struct btf_type *t)
3508{
3460static inline bool btf_is_enum_fwd(struct btf_type *t)
3461{
3509 return btf_is_enum(t) && btf_vlen(t) == 0;
3462 return btf_is_any_enum(t) && btf_vlen(t) == 0;
3510}
3511
3512static bool btf_compat_enum(struct btf_type *t1, struct btf_type *t2)
3513{
3514 if (!btf_is_enum_fwd(t1) && !btf_is_enum_fwd(t2))
3515 return btf_equal_enum(t1, t2);
3516 /* ignore vlen when comparing */
3517 return t1->name_off == t2->name_off &&
3518 (t1->info & ~0xffff) == (t2->info & ~0xffff) &&
3519 t1->size == t2->size;
3520}
3521
3463}
3464
3465static bool btf_compat_enum(struct btf_type *t1, struct btf_type *t2)
3466{
3467 if (!btf_is_enum_fwd(t1) && !btf_is_enum_fwd(t2))
3468 return btf_equal_enum(t1, t2);
3469 /* ignore vlen when comparing */
3470 return t1->name_off == t2->name_off &&
3471 (t1->info & ~0xffff) == (t2->info & ~0xffff) &&
3472 t1->size == t2->size;
3473}
3474
3475static bool btf_compat_enum64(struct btf_type *t1, struct btf_type *t2)
3476{
3477 if (!btf_is_enum_fwd(t1) && !btf_is_enum_fwd(t2))
3478 return btf_equal_enum64(t1, t2);
3479
3480 /* ignore vlen when comparing */
3481 return t1->name_off == t2->name_off &&
3482 (t1->info & ~0xffff) == (t2->info & ~0xffff) &&
3483 t1->size == t2->size;
3484}
3485
3522/*
3523 * Calculate type signature hash of STRUCT/UNION, ignoring referenced type IDs,
3524 * as referenced type IDs equivalence is established separately during type
3525 * graph equivalence check algorithm.
3526 */
3527static long btf_hash_struct(struct btf_type *t)
3528{
3529 const struct btf_member *member = btf_members(t);

--- 196 unchanged lines hidden (view full) ---

3726 case BTF_KIND_TYPE_TAG:
3727 h = btf_hash_common(t);
3728 break;
3729 case BTF_KIND_INT:
3730 case BTF_KIND_DECL_TAG:
3731 h = btf_hash_int_decl_tag(t);
3732 break;
3733 case BTF_KIND_ENUM:
3486/*
3487 * Calculate type signature hash of STRUCT/UNION, ignoring referenced type IDs,
3488 * as referenced type IDs equivalence is established separately during type
3489 * graph equivalence check algorithm.
3490 */
3491static long btf_hash_struct(struct btf_type *t)
3492{
3493 const struct btf_member *member = btf_members(t);

--- 196 unchanged lines hidden (view full) ---

3690 case BTF_KIND_TYPE_TAG:
3691 h = btf_hash_common(t);
3692 break;
3693 case BTF_KIND_INT:
3694 case BTF_KIND_DECL_TAG:
3695 h = btf_hash_int_decl_tag(t);
3696 break;
3697 case BTF_KIND_ENUM:
3698 case BTF_KIND_ENUM64:
3734 h = btf_hash_enum(t);
3735 break;
3736 case BTF_KIND_STRUCT:
3737 case BTF_KIND_UNION:
3738 h = btf_hash_struct(t);
3739 break;
3740 case BTF_KIND_ARRAY:
3741 h = btf_hash_array(t);

--- 73 unchanged lines hidden (view full) ---

3815 break;
3816 }
3817 /* resolve canonical enum fwd to full enum */
3818 d->map[cand_id] = type_id;
3819 }
3820 }
3821 break;
3822
3699 h = btf_hash_enum(t);
3700 break;
3701 case BTF_KIND_STRUCT:
3702 case BTF_KIND_UNION:
3703 h = btf_hash_struct(t);
3704 break;
3705 case BTF_KIND_ARRAY:
3706 h = btf_hash_array(t);

--- 73 unchanged lines hidden (view full) ---

3780 break;
3781 }
3782 /* resolve canonical enum fwd to full enum */
3783 d->map[cand_id] = type_id;
3784 }
3785 }
3786 break;
3787
3788 case BTF_KIND_ENUM64:
3789 h = btf_hash_enum(t);
3790 for_each_dedup_cand(d, hash_entry, h) {
3791 cand_id = (__u32)(long)hash_entry->value;
3792 cand = btf_type_by_id(d->btf, cand_id);
3793 if (btf_equal_enum64(t, cand)) {
3794 new_id = cand_id;
3795 break;
3796 }
3797 if (btf_compat_enum64(t, cand)) {
3798 if (btf_is_enum_fwd(t)) {
3799 /* resolve fwd to full enum */
3800 new_id = cand_id;
3801 break;
3802 }
3803 /* resolve canonical enum fwd to full enum */
3804 d->map[cand_id] = type_id;
3805 }
3806 }
3807 break;
3808
3823 case BTF_KIND_FWD:
3824 case BTF_KIND_FLOAT:
3825 h = btf_hash_common(t);
3826 for_each_dedup_cand(d, hash_entry, h) {
3827 cand_id = (__u32)(long)hash_entry->value;
3828 cand = btf_type_by_id(d->btf, cand_id);
3829 if (btf_equal_common(t, cand)) {
3830 new_id = cand_id;

--- 279 unchanged lines hidden (view full) ---

4110
4111 switch (cand_kind) {
4112 case BTF_KIND_INT:
4113 return btf_equal_int_tag(cand_type, canon_type);
4114
4115 case BTF_KIND_ENUM:
4116 return btf_compat_enum(cand_type, canon_type);
4117
3809 case BTF_KIND_FWD:
3810 case BTF_KIND_FLOAT:
3811 h = btf_hash_common(t);
3812 for_each_dedup_cand(d, hash_entry, h) {
3813 cand_id = (__u32)(long)hash_entry->value;
3814 cand = btf_type_by_id(d->btf, cand_id);
3815 if (btf_equal_common(t, cand)) {
3816 new_id = cand_id;

--- 279 unchanged lines hidden (view full) ---

4096
4097 switch (cand_kind) {
4098 case BTF_KIND_INT:
4099 return btf_equal_int_tag(cand_type, canon_type);
4100
4101 case BTF_KIND_ENUM:
4102 return btf_compat_enum(cand_type, canon_type);
4103
4104 case BTF_KIND_ENUM64:
4105 return btf_compat_enum64(cand_type, canon_type);
4106
4118 case BTF_KIND_FWD:
4119 case BTF_KIND_FLOAT:
4120 return btf_equal_common(cand_type, canon_type);
4121
4122 case BTF_KIND_CONST:
4123 case BTF_KIND_VOLATILE:
4124 case BTF_KIND_RESTRICT:
4125 case BTF_KIND_PTR:

--- 586 unchanged lines hidden (view full) ---

4712int btf_type_visit_type_ids(struct btf_type *t, type_id_visit_fn visit, void *ctx)
4713{
4714 int i, n, err;
4715
4716 switch (btf_kind(t)) {
4717 case BTF_KIND_INT:
4718 case BTF_KIND_FLOAT:
4719 case BTF_KIND_ENUM:
4107 case BTF_KIND_FWD:
4108 case BTF_KIND_FLOAT:
4109 return btf_equal_common(cand_type, canon_type);
4110
4111 case BTF_KIND_CONST:
4112 case BTF_KIND_VOLATILE:
4113 case BTF_KIND_RESTRICT:
4114 case BTF_KIND_PTR:

--- 586 unchanged lines hidden (view full) ---

4701int btf_type_visit_type_ids(struct btf_type *t, type_id_visit_fn visit, void *ctx)
4702{
4703 int i, n, err;
4704
4705 switch (btf_kind(t)) {
4706 case BTF_KIND_INT:
4707 case BTF_KIND_FLOAT:
4708 case BTF_KIND_ENUM:
4709 case BTF_KIND_ENUM64:
4720 return 0;
4721
4722 case BTF_KIND_FWD:
4723 case BTF_KIND_CONST:
4724 case BTF_KIND_VOLATILE:
4725 case BTF_KIND_RESTRICT:
4726 case BTF_KIND_PTR:
4727 case BTF_KIND_TYPEDEF:

--- 78 unchanged lines hidden (view full) ---

4806
4807 for (i = 0, n = btf_vlen(t); i < n; i++, m++) {
4808 err = visit(&m->name_off, ctx);
4809 if (err)
4810 return err;
4811 }
4812 break;
4813 }
4710 return 0;
4711
4712 case BTF_KIND_FWD:
4713 case BTF_KIND_CONST:
4714 case BTF_KIND_VOLATILE:
4715 case BTF_KIND_RESTRICT:
4716 case BTF_KIND_PTR:
4717 case BTF_KIND_TYPEDEF:

--- 78 unchanged lines hidden (view full) ---

4796
4797 for (i = 0, n = btf_vlen(t); i < n; i++, m++) {
4798 err = visit(&m->name_off, ctx);
4799 if (err)
4800 return err;
4801 }
4802 break;
4803 }
4804 case BTF_KIND_ENUM64: {
4805 struct btf_enum64 *m = btf_enum64(t);
4806
4807 for (i = 0, n = btf_vlen(t); i < n; i++, m++) {
4808 err = visit(&m->name_off, ctx);
4809 if (err)
4810 return err;
4811 }
4812 break;
4813 }
4814 case BTF_KIND_FUNC_PROTO: {
4815 struct btf_param *m = btf_params(t);
4816
4817 for (i = 0, n = btf_vlen(t); i < n; i++, m++) {
4818 err = visit(&m->name_off, ctx);
4819 if (err)
4820 return err;
4821 }

--- 88 unchanged lines hidden ---
4814 case BTF_KIND_FUNC_PROTO: {
4815 struct btf_param *m = btf_params(t);
4816
4817 for (i = 0, n = btf_vlen(t); i < n; i++, m++) {
4818 err = visit(&m->name_off, ctx);
4819 if (err)
4820 return err;
4821 }

--- 88 unchanged lines hidden ---