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 50cc15a20dSJiri Olsa static int 51cc15a20dSJiri Olsa __resolve_symbol(struct btf *btf, int type_id) 52cc15a20dSJiri Olsa { 53cc15a20dSJiri Olsa const struct btf_type *type; 54cc15a20dSJiri Olsa const char *str; 55cc15a20dSJiri Olsa unsigned int i; 56cc15a20dSJiri Olsa 57cc15a20dSJiri Olsa type = btf__type_by_id(btf, type_id); 58cc15a20dSJiri Olsa if (!type) { 59cc15a20dSJiri Olsa PRINT_FAIL("Failed to get type for ID %d\n", type_id); 60cc15a20dSJiri Olsa return -1; 61cc15a20dSJiri Olsa } 62cc15a20dSJiri Olsa 63cc15a20dSJiri Olsa for (i = 0; i < ARRAY_SIZE(test_symbols); i++) { 64cc15a20dSJiri Olsa if (test_symbols[i].id != -1) 65cc15a20dSJiri Olsa continue; 66cc15a20dSJiri Olsa 67cc15a20dSJiri Olsa if (BTF_INFO_KIND(type->info) != test_symbols[i].type) 68cc15a20dSJiri Olsa continue; 69cc15a20dSJiri Olsa 70cc15a20dSJiri Olsa str = btf__name_by_offset(btf, type->name_off); 71cc15a20dSJiri Olsa if (!str) { 72cc15a20dSJiri Olsa PRINT_FAIL("Failed to get name for BTF ID %d\n", type_id); 73cc15a20dSJiri Olsa return -1; 74cc15a20dSJiri Olsa } 75cc15a20dSJiri Olsa 76cc15a20dSJiri Olsa if (!strcmp(str, test_symbols[i].name)) 77cc15a20dSJiri Olsa test_symbols[i].id = type_id; 78cc15a20dSJiri Olsa } 79cc15a20dSJiri Olsa 80cc15a20dSJiri Olsa return 0; 81cc15a20dSJiri Olsa } 82cc15a20dSJiri Olsa 83cc15a20dSJiri Olsa static int resolve_symbols(void) 84cc15a20dSJiri Olsa { 85cc15a20dSJiri Olsa struct btf *btf; 86cc15a20dSJiri Olsa int type_id; 87cc15a20dSJiri Olsa __u32 nr; 88cc15a20dSJiri Olsa 89cc15a20dSJiri Olsa btf = btf__parse_elf("btf_data.o", NULL); 90cc15a20dSJiri Olsa if (CHECK(libbpf_get_error(btf), "resolve", 91cc15a20dSJiri Olsa "Failed to load BTF from btf_data.o\n")) 92cc15a20dSJiri Olsa return -1; 93cc15a20dSJiri Olsa 94cc15a20dSJiri Olsa nr = btf__get_nr_types(btf); 95cc15a20dSJiri Olsa 96cc15a20dSJiri Olsa for (type_id = 1; type_id <= nr; type_id++) { 97cc15a20dSJiri Olsa if (__resolve_symbol(btf, type_id)) 98cc15a20dSJiri Olsa break; 99cc15a20dSJiri Olsa } 100cc15a20dSJiri Olsa 101cc15a20dSJiri Olsa btf__free(btf); 102cc15a20dSJiri Olsa return 0; 103cc15a20dSJiri Olsa } 104cc15a20dSJiri Olsa 105cc15a20dSJiri Olsa int test_resolve_btfids(void) 106cc15a20dSJiri Olsa { 1070f12e584SYonghong Song __u32 *test_list, *test_lists[] = { test_list_local, test_list_global }; 1080f12e584SYonghong Song unsigned int i, j; 109cc15a20dSJiri Olsa int ret = 0; 110cc15a20dSJiri Olsa 111cc15a20dSJiri Olsa if (resolve_symbols()) 112cc15a20dSJiri Olsa return -1; 113cc15a20dSJiri Olsa 1140f12e584SYonghong Song /* Check BTF_ID_LIST(test_list_local) and 1150f12e584SYonghong Song * BTF_ID_LIST_GLOBAL(test_list_global) IDs 1160f12e584SYonghong Song */ 1170f12e584SYonghong Song for (j = 0; j < ARRAY_SIZE(test_lists); j++) { 1180f12e584SYonghong Song test_list = test_lists[j]; 119cc15a20dSJiri Olsa for (i = 0; i < ARRAY_SIZE(test_symbols) && !ret; i++) { 120cc15a20dSJiri Olsa ret = CHECK(test_list[i] != test_symbols[i].id, 121cc15a20dSJiri Olsa "id_check", 1220f12e584SYonghong Song "wrong ID for %s (%d != %d)\n", 1230f12e584SYonghong Song test_symbols[i].name, 124cc15a20dSJiri Olsa test_list[i], test_symbols[i].id); 125cc15a20dSJiri Olsa } 1260f12e584SYonghong Song } 127cc15a20dSJiri Olsa 128cc15a20dSJiri Olsa return ret; 129cc15a20dSJiri Olsa } 130