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)
51 __resolve_symbol(struct btf *btf, int type_id)
53 const struct btf_type *type;
57 type = btf__type_by_id(btf, type_id);
59 PRINT_FAIL("Failed to get type for ID %d\n", type_id);
63 for (i = 0; i < ARRAY_SIZE(test_symbols); i++) {
64 if (test_symbols[i].id != -1)
67 if (BTF_INFO_KIND(type->info) != test_symbols[i].type)
70 str = btf__name_by_offset(btf, type->name_off);
72 PRINT_FAIL("Failed to get name for BTF ID %d\n", type_id);
76 if (!strcmp(str, test_symbols[i].name))
77 test_symbols[i].id = type_id;
83 static int resolve_symbols(void)
89 btf = btf__parse_elf("btf_data.o", NULL);
90 if (CHECK(libbpf_get_error(btf), "resolve",
91 "Failed to load BTF from btf_data.o\n"))
94 nr = btf__get_nr_types(btf);
96 for (type_id = 1; type_id <= nr; type_id++) {
97 if (__resolve_symbol(btf, type_id))
105 int test_resolve_btfids(void)
107 __u32 *test_list, *test_lists[] = { test_list_local, test_list_global };
111 if (resolve_symbols())
114 /* Check BTF_ID_LIST(test_list_local) and
115 * BTF_ID_LIST_GLOBAL(test_list_global) IDs
117 for (j = 0; j < ARRAY_SIZE(test_lists); j++) {
118 test_list = test_lists[j];
119 for (i = 0; i < ARRAY_SIZE(test_symbols) && !ret; i++) {
120 ret = CHECK(test_list[i] != test_symbols[i].id,
122 "wrong ID for %s (%d != %d)\n",
123 test_symbols[i].name,
124 test_list[i], test_symbols[i].id);