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