1ed21d6d7SRemi Bernon // SPDX-License-Identifier: GPL-2.0
2ed21d6d7SRemi Bernon #include <stdbool.h>
3ed21d6d7SRemi Bernon #include <inttypes.h>
4ed21d6d7SRemi Bernon #include <stdlib.h>
5ed21d6d7SRemi Bernon #include <string.h>
6ed21d6d7SRemi Bernon #include <linux/bitops.h>
7ed21d6d7SRemi Bernon #include <linux/kernel.h>
8ed21d6d7SRemi Bernon #include <linux/types.h>
9ed21d6d7SRemi Bernon #include <sys/types.h>
10ed21d6d7SRemi Bernon #include <sys/stat.h>
11ed21d6d7SRemi Bernon #include <unistd.h>
12ed21d6d7SRemi Bernon #include <subcmd/exec-cmd.h>
13ed21d6d7SRemi Bernon
14ed21d6d7SRemi Bernon #include "debug.h"
15ed21d6d7SRemi Bernon #include "util/build-id.h"
16ed21d6d7SRemi Bernon #include "util/symbol.h"
17ed21d6d7SRemi Bernon #include "util/dso.h"
18ed21d6d7SRemi Bernon
19ed21d6d7SRemi Bernon #include "tests.h"
20ed21d6d7SRemi Bernon
21ed21d6d7SRemi Bernon #ifdef HAVE_LIBBFD_SUPPORT
22ed21d6d7SRemi Bernon
run_dir(const char * d)23ed21d6d7SRemi Bernon static int run_dir(const char *d)
24ed21d6d7SRemi Bernon {
25ed21d6d7SRemi Bernon char filename[PATH_MAX];
26ed21d6d7SRemi Bernon char debugfile[PATH_MAX];
27f766819cSJiri Olsa struct build_id bid;
28ed21d6d7SRemi Bernon char debuglink[PATH_MAX];
29ed21d6d7SRemi Bernon char expect_build_id[] = {
30ed21d6d7SRemi Bernon 0x5a, 0x0f, 0xd8, 0x82, 0xb5, 0x30, 0x84, 0x22,
31ed21d6d7SRemi Bernon 0x4b, 0xa4, 0x7b, 0x62, 0x4c, 0x55, 0xa4, 0x69,
32ed21d6d7SRemi Bernon };
33ed21d6d7SRemi Bernon char expect_debuglink[PATH_MAX] = "pe-file.exe.debug";
34ed21d6d7SRemi Bernon struct dso *dso;
35ed21d6d7SRemi Bernon struct symbol *sym;
36ed21d6d7SRemi Bernon int ret;
37*ad5f604eSNamhyung Kim size_t idx;
38ed21d6d7SRemi Bernon
39ed21d6d7SRemi Bernon scnprintf(filename, PATH_MAX, "%s/pe-file.exe", d);
40f766819cSJiri Olsa ret = filename__read_build_id(filename, &bid);
41ed21d6d7SRemi Bernon TEST_ASSERT_VAL("Failed to read build_id",
42ed21d6d7SRemi Bernon ret == sizeof(expect_build_id));
43f766819cSJiri Olsa TEST_ASSERT_VAL("Wrong build_id", !memcmp(bid.data, expect_build_id,
44ed21d6d7SRemi Bernon sizeof(expect_build_id)));
45ed21d6d7SRemi Bernon
46ed21d6d7SRemi Bernon ret = filename__read_debuglink(filename, debuglink, PATH_MAX);
47ed21d6d7SRemi Bernon TEST_ASSERT_VAL("Failed to read debuglink", ret == 0);
48ed21d6d7SRemi Bernon TEST_ASSERT_VAL("Wrong debuglink",
49ed21d6d7SRemi Bernon !strcmp(debuglink, expect_debuglink));
50ed21d6d7SRemi Bernon
51ed21d6d7SRemi Bernon scnprintf(debugfile, PATH_MAX, "%s/%s", d, debuglink);
52f766819cSJiri Olsa ret = filename__read_build_id(debugfile, &bid);
53ed21d6d7SRemi Bernon TEST_ASSERT_VAL("Failed to read debug file build_id",
54ed21d6d7SRemi Bernon ret == sizeof(expect_build_id));
55f766819cSJiri Olsa TEST_ASSERT_VAL("Wrong build_id", !memcmp(bid.data, expect_build_id,
56ed21d6d7SRemi Bernon sizeof(expect_build_id)));
57ed21d6d7SRemi Bernon
58ed21d6d7SRemi Bernon dso = dso__new(filename);
59ed21d6d7SRemi Bernon TEST_ASSERT_VAL("Failed to get dso", dso);
60ed21d6d7SRemi Bernon
61ed21d6d7SRemi Bernon ret = dso__load_bfd_symbols(dso, debugfile);
62ed21d6d7SRemi Bernon TEST_ASSERT_VAL("Failed to load symbols", ret == 0);
63ed21d6d7SRemi Bernon
64ed21d6d7SRemi Bernon dso__sort_by_name(dso);
65*ad5f604eSNamhyung Kim sym = dso__find_symbol_by_name(dso, "main", &idx);
66ed21d6d7SRemi Bernon TEST_ASSERT_VAL("Failed to find main", sym);
67ed21d6d7SRemi Bernon dso__delete(dso);
68ed21d6d7SRemi Bernon
69ed21d6d7SRemi Bernon return TEST_OK;
70ed21d6d7SRemi Bernon }
71ed21d6d7SRemi Bernon
test__pe_file_parsing(struct test_suite * test __maybe_unused,int subtest __maybe_unused)7233f44bfdSIan Rogers static int test__pe_file_parsing(struct test_suite *test __maybe_unused,
73ed21d6d7SRemi Bernon int subtest __maybe_unused)
74ed21d6d7SRemi Bernon {
75ed21d6d7SRemi Bernon struct stat st;
76ed21d6d7SRemi Bernon char path_dir[PATH_MAX];
77ed21d6d7SRemi Bernon
78ed21d6d7SRemi Bernon /* First try development tree tests. */
79ed21d6d7SRemi Bernon if (!lstat("./tests", &st))
80ed21d6d7SRemi Bernon return run_dir("./tests");
81ed21d6d7SRemi Bernon
82ed21d6d7SRemi Bernon /* Then installed path. */
83ed21d6d7SRemi Bernon snprintf(path_dir, PATH_MAX, "%s/tests", get_argv_exec_path());
84ed21d6d7SRemi Bernon
85ed21d6d7SRemi Bernon if (!lstat(path_dir, &st))
86ed21d6d7SRemi Bernon return run_dir(path_dir);
87ed21d6d7SRemi Bernon
88ed21d6d7SRemi Bernon return TEST_SKIP;
89ed21d6d7SRemi Bernon }
90ed21d6d7SRemi Bernon
91ed21d6d7SRemi Bernon #else
92ed21d6d7SRemi Bernon
test__pe_file_parsing(struct test_suite * test __maybe_unused,int subtest __maybe_unused)9333f44bfdSIan Rogers static int test__pe_file_parsing(struct test_suite *test __maybe_unused,
94ed21d6d7SRemi Bernon int subtest __maybe_unused)
95ed21d6d7SRemi Bernon {
96ed21d6d7SRemi Bernon return TEST_SKIP;
97ed21d6d7SRemi Bernon }
98ed21d6d7SRemi Bernon
99ed21d6d7SRemi Bernon #endif
100d68f0365SIan Rogers
101d68f0365SIan Rogers DEFINE_SUITE("PE file support", pe_file_parsing);
102