aboutsummaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/bpf/test_btf.c
diff options
context:
space:
mode:
Diffstat (limited to 'tools/testing/selftests/bpf/test_btf.c')
-rw-r--r--tools/testing/selftests/bpf/test_btf.c71
1 files changed, 13 insertions, 58 deletions
diff --git a/tools/testing/selftests/bpf/test_btf.c b/tools/testing/selftests/bpf/test_btf.c
index 42c1ce988945..289daf54dec4 100644
--- a/tools/testing/selftests/bpf/test_btf.c
+++ b/tools/testing/selftests/bpf/test_btf.c
@@ -4025,62 +4025,13 @@ static struct btf_file_test file_tests[] = {
4025}, 4025},
4026}; 4026};
4027 4027
4028static int file_has_btf_elf(const char *fn, bool *has_btf_ext)
4029{
4030 Elf_Scn *scn = NULL;
4031 GElf_Ehdr ehdr;
4032 int ret = 0;
4033 int elf_fd;
4034 Elf *elf;
4035
4036 if (CHECK(elf_version(EV_CURRENT) == EV_NONE,
4037 "elf_version(EV_CURRENT) == EV_NONE"))
4038 return -1;
4039
4040 elf_fd = open(fn, O_RDONLY);
4041 if (CHECK(elf_fd == -1, "open(%s): errno:%d", fn, errno))
4042 return -1;
4043
4044 elf = elf_begin(elf_fd, ELF_C_READ, NULL);
4045 if (CHECK(!elf, "elf_begin(%s): %s", fn, elf_errmsg(elf_errno()))) {
4046 ret = -1;
4047 goto done;
4048 }
4049
4050 if (CHECK(!gelf_getehdr(elf, &ehdr), "!gelf_getehdr(%s)", fn)) {
4051 ret = -1;
4052 goto done;
4053 }
4054
4055 while ((scn = elf_nextscn(elf, scn))) {
4056 const char *sh_name;
4057 GElf_Shdr sh;
4058
4059 if (CHECK(gelf_getshdr(scn, &sh) != &sh,
4060 "file:%s gelf_getshdr != &sh", fn)) {
4061 ret = -1;
4062 goto done;
4063 }
4064
4065 sh_name = elf_strptr(elf, ehdr.e_shstrndx, sh.sh_name);
4066 if (!strcmp(sh_name, BTF_ELF_SEC))
4067 ret = 1;
4068 if (!strcmp(sh_name, BTF_EXT_ELF_SEC))
4069 *has_btf_ext = true;
4070 }
4071
4072done:
4073 close(elf_fd);
4074 elf_end(elf);
4075 return ret;
4076}
4077
4078static int do_test_file(unsigned int test_num) 4028static int do_test_file(unsigned int test_num)
4079{ 4029{
4080 const struct btf_file_test *test = &file_tests[test_num - 1]; 4030 const struct btf_file_test *test = &file_tests[test_num - 1];
4081 const char *expected_fnames[] = {"_dummy_tracepoint", 4031 const char *expected_fnames[] = {"_dummy_tracepoint",
4082 "test_long_fname_1", 4032 "test_long_fname_1",
4083 "test_long_fname_2"}; 4033 "test_long_fname_2"};
4034 struct btf_ext *btf_ext = NULL;
4084 struct bpf_prog_info info = {}; 4035 struct bpf_prog_info info = {};
4085 struct bpf_object *obj = NULL; 4036 struct bpf_object *obj = NULL;
4086 struct bpf_func_info *finfo; 4037 struct bpf_func_info *finfo;
@@ -4095,15 +4046,19 @@ static int do_test_file(unsigned int test_num)
4095 fprintf(stderr, "BTF libbpf test[%u] (%s): ", test_num, 4046 fprintf(stderr, "BTF libbpf test[%u] (%s): ", test_num,
4096 test->file); 4047 test->file);
4097 4048
4098 err = file_has_btf_elf(test->file, &has_btf_ext); 4049 btf = btf__parse_elf(test->file, &btf_ext);
4099 if (err == -1) 4050 if (IS_ERR(btf)) {
4100 return err; 4051 if (PTR_ERR(btf) == -ENOENT) {
4101 4052 fprintf(stderr, "SKIP. No ELF %s found", BTF_ELF_SEC);
4102 if (err == 0) { 4053 skip_cnt++;
4103 fprintf(stderr, "SKIP. No ELF %s found", BTF_ELF_SEC); 4054 return 0;
4104 skip_cnt++; 4055 }
4105 return 0; 4056 return PTR_ERR(btf);
4106 } 4057 }
4058 btf__free(btf);
4059
4060 has_btf_ext = btf_ext != NULL;
4061 btf_ext__free(btf_ext);
4107 4062
4108 obj = bpf_object__open(test->file); 4063 obj = bpf_object__open(test->file);
4109 if (CHECK(IS_ERR(obj), "obj: %ld", PTR_ERR(obj))) 4064 if (CHECK(IS_ERR(obj), "obj: %ld", PTR_ERR(obj)))