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 /* Align the .BTF_ids section to 4 bytes */
33 ".pushsection " BTF_IDS_SECTION " ,\"a\"; \n"
37 BTF_ID_LIST(test_list_local)
46 extern __u32 test_list_global[];
47 BTF_ID_LIST_GLOBAL(test_list_global, 1)
56 BTF_SET_START(test_set)
66 __resolve_symbol(struct btf *btf, int type_id)
68 const struct btf_type *type;
72 type = btf__type_by_id(btf, type_id);
74 PRINT_FAIL("Failed to get type for ID %d\n", type_id);
78 for (i = 0; i < ARRAY_SIZE(test_symbols); i++) {
79 if (test_symbols[i].id >= 0)
82 if (BTF_INFO_KIND(type->info) != test_symbols[i].type)
85 str = btf__name_by_offset(btf, type->name_off);
87 PRINT_FAIL("Failed to get name for BTF ID %d\n", type_id);
91 if (!strcmp(str, test_symbols[i].name))
92 test_symbols[i].id = type_id;
98 static int resolve_symbols(void)
104 btf = btf__parse_elf("btf_data.o", NULL);
105 if (CHECK(libbpf_get_error(btf), "resolve",
106 "Failed to load BTF from btf_data.o\n"))
109 nr = btf__type_cnt(btf);
111 for (type_id = 1; type_id < nr; type_id++) {
112 if (__resolve_symbol(btf, type_id))
120 void test_resolve_btfids(void)
122 __u32 *test_list, *test_lists[] = { test_list_local, test_list_global };
126 if (resolve_symbols())
129 /* Check BTF_ID_LIST(test_list_local) and
130 * BTF_ID_LIST_GLOBAL(test_list_global) IDs
132 for (j = 0; j < ARRAY_SIZE(test_lists); j++) {
133 test_list = test_lists[j];
134 for (i = 0; i < ARRAY_SIZE(test_symbols); i++) {
135 ret = CHECK(test_list[i] != test_symbols[i].id,
137 "wrong ID for %s (%d != %d)\n",
138 test_symbols[i].name,
139 test_list[i], test_symbols[i].id);
145 /* Check BTF_SET_START(test_set) IDs */
146 for (i = 0; i < test_set.cnt; i++) {
149 for (j = 0; j < ARRAY_SIZE(test_symbols); j++) {
150 if (test_symbols[j].id != test_set.ids[i])
156 ret = CHECK(!found, "id_check",
157 "ID %d not found in test_symbols\n",
163 if (!ASSERT_LE(test_set.ids[i - 1], test_set.ids[i], "sort_check"))