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