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