Coverage Report

Created: 2024-09-30 06:44

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