1cc15a20dSJiri Olsa // SPDX-License-Identifier: GPL-2.0
2cc15a20dSJiri Olsa 
3cc15a20dSJiri Olsa #include <linux/err.h>
4cc15a20dSJiri Olsa #include <string.h>
5cc15a20dSJiri Olsa #include <bpf/btf.h>
6cc15a20dSJiri Olsa #include <bpf/libbpf.h>
7cc15a20dSJiri Olsa #include <linux/btf.h>
8cc15a20dSJiri Olsa #include <linux/kernel.h>
9d8dfe5bfSYonghong Song #define CONFIG_DEBUG_INFO_BTF
10cc15a20dSJiri Olsa #include <linux/btf_ids.h>
11cc15a20dSJiri Olsa #include "test_progs.h"
12cc15a20dSJiri Olsa 
13cc15a20dSJiri Olsa static int duration;
14cc15a20dSJiri Olsa 
15cc15a20dSJiri Olsa struct symbol {
16cc15a20dSJiri Olsa 	const char	*name;
17cc15a20dSJiri Olsa 	int		 type;
18cc15a20dSJiri Olsa 	int		 id;
19cc15a20dSJiri Olsa };
20cc15a20dSJiri Olsa 
21cc15a20dSJiri Olsa struct symbol test_symbols[] = {
22cc15a20dSJiri Olsa 	{ "unused",  BTF_KIND_UNKN,     0 },
23cc15a20dSJiri Olsa 	{ "S",       BTF_KIND_TYPEDEF, -1 },
24cc15a20dSJiri Olsa 	{ "T",       BTF_KIND_TYPEDEF, -1 },
25cc15a20dSJiri Olsa 	{ "U",       BTF_KIND_TYPEDEF, -1 },
26cc15a20dSJiri Olsa 	{ "S",       BTF_KIND_STRUCT,  -1 },
27cc15a20dSJiri Olsa 	{ "U",       BTF_KIND_UNION,   -1 },
28cc15a20dSJiri Olsa 	{ "func",    BTF_KIND_FUNC,    -1 },
29cc15a20dSJiri Olsa };
30cc15a20dSJiri Olsa 
310f12e584SYonghong Song BTF_ID_LIST(test_list_local)
320f12e584SYonghong Song BTF_ID_UNUSED
330f12e584SYonghong Song BTF_ID(typedef, S)
340f12e584SYonghong Song BTF_ID(typedef, T)
350f12e584SYonghong Song BTF_ID(typedef, U)
360f12e584SYonghong Song BTF_ID(struct,  S)
370f12e584SYonghong Song BTF_ID(union,   U)
380f12e584SYonghong Song BTF_ID(func,    func)
390f12e584SYonghong Song 
400f12e584SYonghong Song extern __u32 test_list_global[];
410f12e584SYonghong Song BTF_ID_LIST_GLOBAL(test_list_global)
42cc15a20dSJiri Olsa BTF_ID_UNUSED
43cc15a20dSJiri Olsa BTF_ID(typedef, S)
44cc15a20dSJiri Olsa BTF_ID(typedef, T)
45cc15a20dSJiri Olsa BTF_ID(typedef, U)
46cc15a20dSJiri Olsa BTF_ID(struct,  S)
47cc15a20dSJiri Olsa BTF_ID(union,   U)
48cc15a20dSJiri Olsa BTF_ID(func,    func)
49cc15a20dSJiri Olsa 
50cd04b04dSJiri Olsa BTF_SET_START(test_set)
51cd04b04dSJiri Olsa BTF_ID(typedef, S)
52cd04b04dSJiri Olsa BTF_ID(typedef, T)
53cd04b04dSJiri Olsa BTF_ID(typedef, U)
54cd04b04dSJiri Olsa BTF_ID(struct,  S)
55cd04b04dSJiri Olsa BTF_ID(union,   U)
56cd04b04dSJiri Olsa BTF_ID(func,    func)
57cd04b04dSJiri Olsa BTF_SET_END(test_set)
58cd04b04dSJiri Olsa 
59cc15a20dSJiri Olsa static int
60cc15a20dSJiri Olsa __resolve_symbol(struct btf *btf, int type_id)
61cc15a20dSJiri Olsa {
62cc15a20dSJiri Olsa 	const struct btf_type *type;
63cc15a20dSJiri Olsa 	const char *str;
64cc15a20dSJiri Olsa 	unsigned int i;
65cc15a20dSJiri Olsa 
66cc15a20dSJiri Olsa 	type = btf__type_by_id(btf, type_id);
67cc15a20dSJiri Olsa 	if (!type) {
68cc15a20dSJiri Olsa 		PRINT_FAIL("Failed to get type for ID %d\n", type_id);
69cc15a20dSJiri Olsa 		return -1;
70cc15a20dSJiri Olsa 	}
71cc15a20dSJiri Olsa 
72cc15a20dSJiri Olsa 	for (i = 0; i < ARRAY_SIZE(test_symbols); i++) {
73cc15a20dSJiri Olsa 		if (test_symbols[i].id != -1)
74cc15a20dSJiri Olsa 			continue;
75cc15a20dSJiri Olsa 
76cc15a20dSJiri Olsa 		if (BTF_INFO_KIND(type->info) != test_symbols[i].type)
77cc15a20dSJiri Olsa 			continue;
78cc15a20dSJiri Olsa 
79cc15a20dSJiri Olsa 		str = btf__name_by_offset(btf, type->name_off);
80cc15a20dSJiri Olsa 		if (!str) {
81cc15a20dSJiri Olsa 			PRINT_FAIL("Failed to get name for BTF ID %d\n", type_id);
82cc15a20dSJiri Olsa 			return -1;
83cc15a20dSJiri Olsa 		}
84cc15a20dSJiri Olsa 
85cc15a20dSJiri Olsa 		if (!strcmp(str, test_symbols[i].name))
86cc15a20dSJiri Olsa 			test_symbols[i].id = type_id;
87cc15a20dSJiri Olsa 	}
88cc15a20dSJiri Olsa 
89cc15a20dSJiri Olsa 	return 0;
90cc15a20dSJiri Olsa }
91cc15a20dSJiri Olsa 
92cc15a20dSJiri Olsa static int resolve_symbols(void)
93cc15a20dSJiri Olsa {
94cc15a20dSJiri Olsa 	struct btf *btf;
95cc15a20dSJiri Olsa 	int type_id;
96cc15a20dSJiri Olsa 	__u32 nr;
97cc15a20dSJiri Olsa 
98cc15a20dSJiri Olsa 	btf = btf__parse_elf("btf_data.o", NULL);
99cc15a20dSJiri Olsa 	if (CHECK(libbpf_get_error(btf), "resolve",
100cc15a20dSJiri Olsa 		  "Failed to load BTF from btf_data.o\n"))
101cc15a20dSJiri Olsa 		return -1;
102cc15a20dSJiri Olsa 
103cc15a20dSJiri Olsa 	nr = btf__get_nr_types(btf);
104cc15a20dSJiri Olsa 
105cc15a20dSJiri Olsa 	for (type_id = 1; type_id <= nr; type_id++) {
106cc15a20dSJiri Olsa 		if (__resolve_symbol(btf, type_id))
107cc15a20dSJiri Olsa 			break;
108cc15a20dSJiri Olsa 	}
109cc15a20dSJiri Olsa 
110cc15a20dSJiri Olsa 	btf__free(btf);
111cc15a20dSJiri Olsa 	return 0;
112cc15a20dSJiri Olsa }
113cc15a20dSJiri Olsa 
114cc15a20dSJiri Olsa int test_resolve_btfids(void)
115cc15a20dSJiri Olsa {
1160f12e584SYonghong Song 	__u32 *test_list, *test_lists[] = { test_list_local, test_list_global };
1170f12e584SYonghong Song 	unsigned int i, j;
118cc15a20dSJiri Olsa 	int ret = 0;
119cc15a20dSJiri Olsa 
120cc15a20dSJiri Olsa 	if (resolve_symbols())
121cc15a20dSJiri Olsa 		return -1;
122cc15a20dSJiri Olsa 
1230f12e584SYonghong Song 	/* Check BTF_ID_LIST(test_list_local) and
1240f12e584SYonghong Song 	 * BTF_ID_LIST_GLOBAL(test_list_global) IDs
1250f12e584SYonghong Song 	 */
1260f12e584SYonghong Song 	for (j = 0; j < ARRAY_SIZE(test_lists); j++) {
1270f12e584SYonghong Song 		test_list = test_lists[j];
128cd04b04dSJiri Olsa 		for (i = 0; i < ARRAY_SIZE(test_symbols); i++) {
129cc15a20dSJiri Olsa 			ret = CHECK(test_list[i] != test_symbols[i].id,
130cc15a20dSJiri Olsa 				    "id_check",
1310f12e584SYonghong Song 				    "wrong ID for %s (%d != %d)\n",
1320f12e584SYonghong Song 				    test_symbols[i].name,
133cc15a20dSJiri Olsa 				    test_list[i], test_symbols[i].id);
134cd04b04dSJiri Olsa 			if (ret)
135cd04b04dSJiri Olsa 				return ret;
136cd04b04dSJiri Olsa 		}
137cd04b04dSJiri Olsa 	}
138cd04b04dSJiri Olsa 
139cd04b04dSJiri Olsa 	/* Check BTF_SET_START(test_set) IDs */
140cd04b04dSJiri Olsa 	for (i = 0; i < test_set.cnt; i++) {
141cd04b04dSJiri Olsa 		bool found = false;
142cd04b04dSJiri Olsa 
143cd04b04dSJiri Olsa 		for (j = 0; j < ARRAY_SIZE(test_symbols); j++) {
144cd04b04dSJiri Olsa 			if (test_symbols[j].id != test_set.ids[i])
145cd04b04dSJiri Olsa 				continue;
146cd04b04dSJiri Olsa 			found = true;
147cd04b04dSJiri Olsa 			break;
148cd04b04dSJiri Olsa 		}
149cd04b04dSJiri Olsa 
150cd04b04dSJiri Olsa 		ret = CHECK(!found, "id_check",
151cd04b04dSJiri Olsa 			    "ID %d not found in test_symbols\n",
152cd04b04dSJiri Olsa 			    test_set.ids[i]);
153cd04b04dSJiri Olsa 		if (ret)
154cd04b04dSJiri Olsa 			break;
155cd04b04dSJiri Olsa 
156cd04b04dSJiri Olsa 		if (i > 0) {
157cd04b04dSJiri Olsa 			ret = CHECK(test_set.ids[i - 1] > test_set.ids[i],
158cd04b04dSJiri Olsa 				    "sort_check",
159cd04b04dSJiri Olsa 				    "test_set is not sorted\n");
160cd04b04dSJiri Olsa 			if (ret)
161cd04b04dSJiri Olsa 				break;
162cc15a20dSJiri Olsa 		}
1630f12e584SYonghong Song 	}
164cc15a20dSJiri Olsa 
165cc15a20dSJiri Olsa 	return ret;
166cc15a20dSJiri Olsa }
167