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