Skip to content

Commit 9db3243

Browse files
anakryikoAlexei Starovoitov
authored andcommitted
selftests/bpf: use btf__parse_elf to check presence of BTF/BTF.ext
Switch test_btf.c to rely on btf__parse_elf to check presence of BTF and BTF.ext data, instead of implementing its own ELF parsing. Signed-off-by: Andrii Nakryiko <andriin@fb.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent 58650cc commit 9db3243

File tree

1 file changed

+13
-58
lines changed

1 file changed

+13
-58
lines changed

tools/testing/selftests/bpf/test_btf.c

Lines changed: 13 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -4025,62 +4025,13 @@ static struct btf_file_test file_tests[] = {
40254025
},
40264026
};
40274027

4028-
static 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-
4072-
done:
4073-
close(elf_fd);
4074-
elf_end(elf);
4075-
return ret;
4076-
}
4077-
40784028
static int do_test_file(unsigned int test_num)
40794029
{
40804030
const struct btf_file_test *test = &file_tests[test_num - 1];
40814031
const char *expected_fnames[] = {"_dummy_tracepoint",
40824032
"test_long_fname_1",
40834033
"test_long_fname_2"};
4034+
struct btf_ext *btf_ext = NULL;
40844035
struct bpf_prog_info info = {};
40854036
struct bpf_object *obj = NULL;
40864037
struct bpf_func_info *finfo;
@@ -4095,15 +4046,19 @@ static int do_test_file(unsigned int test_num)
40954046
fprintf(stderr, "BTF libbpf test[%u] (%s): ", test_num,
40964047
test->file);
40974048

4098-
err = file_has_btf_elf(test->file, &has_btf_ext);
4099-
if (err == -1)
4100-
return err;
4101-
4102-
if (err == 0) {
4103-
fprintf(stderr, "SKIP. No ELF %s found", BTF_ELF_SEC);
4104-
skip_cnt++;
4105-
return 0;
4049+
btf = btf__parse_elf(test->file, &btf_ext);
4050+
if (IS_ERR(btf)) {
4051+
if (PTR_ERR(btf) == -ENOENT) {
4052+
fprintf(stderr, "SKIP. No ELF %s found", BTF_ELF_SEC);
4053+
skip_cnt++;
4054+
return 0;
4055+
}
4056+
return PTR_ERR(btf);
41064057
}
4058+
btf__free(btf);
4059+
4060+
has_btf_ext = btf_ext != NULL;
4061+
btf_ext__free(btf_ext);
41074062

41084063
obj = bpf_object__open(test->file);
41094064
if (CHECK(IS_ERR(obj), "obj: %ld", PTR_ERR(obj)))

0 commit comments

Comments
 (0)