0001
0002
0003 #include <linux/err.h>
0004 #include <string.h>
0005 #include <bpf/btf.h>
0006 #include <bpf/libbpf.h>
0007 #include <linux/btf.h>
0008 #include <linux/kernel.h>
0009 #define CONFIG_DEBUG_INFO_BTF
0010 #include <linux/btf_ids.h>
0011 #include "test_progs.h"
0012
0013 static int duration;
0014
0015 struct symbol {
0016 const char *name;
0017 int type;
0018 int id;
0019 };
0020
0021 struct symbol test_symbols[] = {
0022 { "unused", BTF_KIND_UNKN, 0 },
0023 { "S", BTF_KIND_TYPEDEF, -1 },
0024 { "T", BTF_KIND_TYPEDEF, -1 },
0025 { "U", BTF_KIND_TYPEDEF, -1 },
0026 { "S", BTF_KIND_STRUCT, -1 },
0027 { "U", BTF_KIND_UNION, -1 },
0028 { "func", BTF_KIND_FUNC, -1 },
0029 };
0030
0031
0032 asm (
0033 ".pushsection " BTF_IDS_SECTION " ,\"a\"; \n"
0034 ".balign 4, 0; \n"
0035 ".popsection; \n");
0036
0037 BTF_ID_LIST(test_list_local)
0038 BTF_ID_UNUSED
0039 BTF_ID(typedef, S)
0040 BTF_ID(typedef, T)
0041 BTF_ID(typedef, U)
0042 BTF_ID(struct, S)
0043 BTF_ID(union, U)
0044 BTF_ID(func, func)
0045
0046 extern __u32 test_list_global[];
0047 BTF_ID_LIST_GLOBAL(test_list_global, 1)
0048 BTF_ID_UNUSED
0049 BTF_ID(typedef, S)
0050 BTF_ID(typedef, T)
0051 BTF_ID(typedef, U)
0052 BTF_ID(struct, S)
0053 BTF_ID(union, U)
0054 BTF_ID(func, func)
0055
0056 BTF_SET_START(test_set)
0057 BTF_ID(typedef, S)
0058 BTF_ID(typedef, T)
0059 BTF_ID(typedef, U)
0060 BTF_ID(struct, S)
0061 BTF_ID(union, U)
0062 BTF_ID(func, func)
0063 BTF_SET_END(test_set)
0064
0065 static int
0066 __resolve_symbol(struct btf *btf, int type_id)
0067 {
0068 const struct btf_type *type;
0069 const char *str;
0070 unsigned int i;
0071
0072 type = btf__type_by_id(btf, type_id);
0073 if (!type) {
0074 PRINT_FAIL("Failed to get type for ID %d\n", type_id);
0075 return -1;
0076 }
0077
0078 for (i = 0; i < ARRAY_SIZE(test_symbols); i++) {
0079 if (test_symbols[i].id >= 0)
0080 continue;
0081
0082 if (BTF_INFO_KIND(type->info) != test_symbols[i].type)
0083 continue;
0084
0085 str = btf__name_by_offset(btf, type->name_off);
0086 if (!str) {
0087 PRINT_FAIL("Failed to get name for BTF ID %d\n", type_id);
0088 return -1;
0089 }
0090
0091 if (!strcmp(str, test_symbols[i].name))
0092 test_symbols[i].id = type_id;
0093 }
0094
0095 return 0;
0096 }
0097
0098 static int resolve_symbols(void)
0099 {
0100 struct btf *btf;
0101 int type_id;
0102 __u32 nr;
0103
0104 btf = btf__parse_elf("btf_data.o", NULL);
0105 if (CHECK(libbpf_get_error(btf), "resolve",
0106 "Failed to load BTF from btf_data.o\n"))
0107 return -1;
0108
0109 nr = btf__type_cnt(btf);
0110
0111 for (type_id = 1; type_id < nr; type_id++) {
0112 if (__resolve_symbol(btf, type_id))
0113 break;
0114 }
0115
0116 btf__free(btf);
0117 return 0;
0118 }
0119
0120 void test_resolve_btfids(void)
0121 {
0122 __u32 *test_list, *test_lists[] = { test_list_local, test_list_global };
0123 unsigned int i, j;
0124 int ret = 0;
0125
0126 if (resolve_symbols())
0127 return;
0128
0129
0130
0131
0132 for (j = 0; j < ARRAY_SIZE(test_lists); j++) {
0133 test_list = test_lists[j];
0134 for (i = 0; i < ARRAY_SIZE(test_symbols); i++) {
0135 ret = CHECK(test_list[i] != test_symbols[i].id,
0136 "id_check",
0137 "wrong ID for %s (%d != %d)\n",
0138 test_symbols[i].name,
0139 test_list[i], test_symbols[i].id);
0140 if (ret)
0141 return;
0142 }
0143 }
0144
0145
0146 for (i = 0; i < test_set.cnt; i++) {
0147 bool found = false;
0148
0149 for (j = 0; j < ARRAY_SIZE(test_symbols); j++) {
0150 if (test_symbols[j].id != test_set.ids[i])
0151 continue;
0152 found = true;
0153 break;
0154 }
0155
0156 ret = CHECK(!found, "id_check",
0157 "ID %d not found in test_symbols\n",
0158 test_set.ids[i]);
0159 if (ret)
0160 break;
0161
0162 if (i > 0) {
0163 if (!ASSERT_LE(test_set.ids[i - 1], test_set.ids[i], "sort_check"))
0164 return;
0165 }
0166 }
0167 }