1 // SPDX-License-Identifier: GPL-2.0
6 #include <bpf/libbpf.h>
8 #include <linux/kernel.h>
9 #define CONFIG_DEBUG_INFO_BTF
10 #include <linux/btf_ids.h>
11 #include "test_progs.h"
21 struct symbol test_symbols[] = {
22 { "unused", BTF_KIND_UNKN, 0 },
23 { "S", BTF_KIND_TYPEDEF, -1 },
24 { "T", BTF_KIND_TYPEDEF, -1 },
25 { "U", BTF_KIND_TYPEDEF, -1 },
26 { "S", BTF_KIND_STRUCT, -1 },
27 { "U", BTF_KIND_UNION, -1 },
28 { "func", BTF_KIND_FUNC, -1 },
31 BTF_ID_LIST(test_list_local)
40 extern __u32 test_list_global[];
41 BTF_ID_LIST_GLOBAL(test_list_global)
50 BTF_SET_START(test_set)
60 __resolve_symbol(struct btf *btf, int type_id)
62 const struct btf_type *type;
66 type = btf__type_by_id(btf, type_id);
68 PRINT_FAIL("Failed to get type for ID %d\n", type_id);
72 for (i = 0; i < ARRAY_SIZE(test_symbols); i++) {
73 if (test_symbols[i].id != -1)
76 if (BTF_INFO_KIND(type->info) != test_symbols[i].type)
79 str = btf__name_by_offset(btf, type->name_off);
81 PRINT_FAIL("Failed to get name for BTF ID %d\n", type_id);
85 if (!strcmp(str, test_symbols[i].name))
86 test_symbols[i].id = type_id;
92 static int resolve_symbols(void)
98 btf = btf__parse_elf("btf_data.o", NULL);
99 if (CHECK(libbpf_get_error(btf), "resolve",
100 "Failed to load BTF from btf_data.o\n"))
103 nr = btf__get_nr_types(btf);
105 for (type_id = 1; type_id <= nr; type_id++) {
106 if (__resolve_symbol(btf, type_id))
114 int test_resolve_btfids(void)
116 __u32 *test_list, *test_lists[] = { test_list_local, test_list_global };
120 if (resolve_symbols())
123 /* Check BTF_ID_LIST(test_list_local) and
124 * BTF_ID_LIST_GLOBAL(test_list_global) IDs
126 for (j = 0; j < ARRAY_SIZE(test_lists); j++) {
127 test_list = test_lists[j];
128 for (i = 0; i < ARRAY_SIZE(test_symbols); i++) {
129 ret = CHECK(test_list[i] != test_symbols[i].id,
131 "wrong ID for %s (%d != %d)\n",
132 test_symbols[i].name,
133 test_list[i], test_symbols[i].id);
139 /* Check BTF_SET_START(test_set) IDs */
140 for (i = 0; i < test_set.cnt; i++) {
143 for (j = 0; j < ARRAY_SIZE(test_symbols); j++) {
144 if (test_symbols[j].id != test_set.ids[i])
150 ret = CHECK(!found, "id_check",
151 "ID %d not found in test_symbols\n",
157 ret = CHECK(test_set.ids[i - 1] > test_set.ids[i],
159 "test_set is not sorted\n");