xref: /linux/tools/bpf/resolve_btfids/btf_colors.c (revision aac67645215d5a38cff9f86110b81c220e5e8182)
1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <bpf/libbpf_internal.h>
3 #include "btf_colors.h"
4 
5 /*
6  * Functions in this file mainly exist to refer to functions from libbpf_internal.h,
7  * which can't be included in main.c because of the u32 poison and pr_warn macro conflicts.
8  */
9 
10 void btf_sha256(const void *data, size_t len,
11 		__u8 out[BTF_SHA256_DIGEST_LENGTH])
12 {
13 	libbpf_sha256(data, len, out);
14 }
15 
16 /*
17  * Marks `root_id` and local types reachable from it with `color`.
18  * `colors` is indexed by source ID; worklist has room for every local type.
19  */
20 void btf_mark_reachable(struct btf *btf, __u32 root_id, enum btf_color color,
21 			__u8 *colors, __u32 *worklist)
22 {
23 	const struct btf *base = btf__base_btf(btf);
24 	__u32 start_id = base ? btf__type_cnt(base) : 1;
25 	__u32 pending = 0;
26 
27 	if (root_id < start_id || (colors[root_id] & color) == color)
28 		return;
29 	colors[root_id] |= color;
30 	worklist[pending++] = root_id;
31 	while (pending) {
32 		const struct btf_type *t = btf__type_by_id(btf, worklist[--pending]);
33 		struct btf_field_iter it;
34 		__u32 *id;
35 
36 		btf_field_iter_init(&it, (struct btf_type *)t, BTF_FIELD_ITER_IDS);
37 		while ((id = btf_field_iter_next(&it))) {
38 			if (*id < start_id || (colors[*id] & color) == color)
39 				continue;
40 			colors[*id] |= color;
41 			worklist[pending++] = *id;
42 		}
43 	}
44 }
45 
46 static int cmp_loc(const void *a, const void *b)
47 {
48 	const struct btf_loc *la = a, *lb = b;
49 
50 	if (la->func != lb->func)
51 		return la->func < lb->func ? -1 : 1;
52 	if (la->offset != lb->offset)
53 		return la->offset < lb->offset ? -1 : 1;
54 	if (la->loc_proto != lb->loc_proto)
55 		return la->loc_proto < lb->loc_proto ? -1 : 1;
56 	return 0;
57 }
58 
59 static void remap(struct btf *btf, const __u32 *id_map, __u32 src_start_id)
60 {
61 	const struct btf *base = btf__base_btf(btf);
62 	__u32 start_id = base ? btf__type_cnt(base) : 1;
63 	__u32 i, type_cnt = btf__type_cnt(btf);
64 
65 	for (i = start_id; i < type_cnt; i++) {
66 		struct btf_type *t = (struct btf_type *)btf__type_by_id(btf, i);
67 		struct btf_field_iter it;
68 		__u32 *id;
69 
70 		btf_field_iter_init(&it, t, BTF_FIELD_ITER_IDS);
71 		while ((id = btf_field_iter_next(&it))) {
72 			/* Void and types in the original ancestor are unchanged. */
73 			if (*id >= src_start_id)
74 				*id = id_map[*id - src_start_id];
75 		}
76 		if (btf_is_locsec(t))
77 			qsort(btf_locsec_locs(t), btf_vlen(t), sizeof(struct btf_loc), cmp_loc);
78 	}
79 }
80 
81 /*
82  * Split the `src` into `main_out` base and `inline_out`,
83  * according to `colors` array. Relative ordering remains
84  * the same as in `src`.
85  */
86 int btf_split_by_color(struct btf *src, const __u8 *colors,
87 		       struct btf **main_out, struct btf **inline_out)
88 {
89 	LIBBPF_OPTS(btf_new_opts, opts,
90 		    .base_btf = (struct btf *)btf__base_btf(src),
91 		    .add_layout = btf_header(src)->layout_len != 0,
92 	);
93 	struct btf *main_btf, *inline_btf = NULL;
94 	__u32 start_id = opts.base_btf ? btf__type_cnt(opts.base_btf) : 1;
95 	__u32 type_cnt = btf__type_cnt(src);
96 	__u32 *id_map, i;
97 	int err = -ENOMEM;
98 
99 	*main_out = NULL;
100 	*inline_out = NULL;
101 	id_map = malloc((type_cnt - start_id ?: 1) * sizeof(*id_map));
102 	if (!id_map)
103 		return -ENOMEM;
104 	main_btf = btf__new_empty_opts(&opts);
105 	if (!main_btf)
106 		goto out;
107 	btf__set_endianness(main_btf, btf__endianness(src));
108 	/* Copy MAIN and SHARED marked types to `main_out`. */
109 	for (i = start_id; i < type_cnt; i++) {
110 		if (colors[i] == BTF_COLOR_LOC)
111 			continue;
112 		err = btf__add_type(main_btf, src, btf__type_by_id(src, i));
113 		if (err < 0)
114 			goto out;
115 		id_map[i - start_id] = err;
116 	}
117 	/*
118 	 * Copy LOC marked types to `inline_out`.
119 	 * `main_btf` types count is stable at this point.
120 	 */
121 	inline_btf = btf__new_empty_split(main_btf);
122 	if (!inline_btf) {
123 		err = -ENOMEM;
124 		goto out;
125 	}
126 	for (i = start_id; i < type_cnt; i++) {
127 		if (colors[i] != BTF_COLOR_LOC)
128 			continue;
129 		err = btf__add_type(inline_btf, src, btf__type_by_id(src, i));
130 		if (err < 0)
131 			goto out;
132 		id_map[i - start_id] = err;
133 	}
134 	remap(main_btf, id_map, start_id);
135 	remap(inline_btf, id_map, start_id);
136 	*main_out = main_btf;
137 	*inline_out = inline_btf;
138 	free(id_map);
139 	return 0;
140 out:
141 	btf__free(inline_btf);
142 	btf__free(main_btf);
143 	free(id_map);
144 	return err;
145 }
146