blob: c09a9fae16897dd270392933fc4f91a2628ea72f [file] [log] [blame]
Remi Bernoned21d6d2020-08-21 18:52:38 +02001// 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
23static int run_dir(const char *d)
24{
25 char filename[PATH_MAX];
26 char debugfile[PATH_MAX];
Jiri Olsaf7668192020-10-13 21:24:34 +020027 struct build_id bid;
Remi Bernoned21d6d2020-08-21 18:52:38 +020028 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
38 scnprintf(filename, PATH_MAX, "%s/pe-file.exe", d);
Jiri Olsaf7668192020-10-13 21:24:34 +020039 ret = filename__read_build_id(filename, &bid);
Remi Bernoned21d6d2020-08-21 18:52:38 +020040 TEST_ASSERT_VAL("Failed to read build_id",
41 ret == sizeof(expect_build_id));
Jiri Olsaf7668192020-10-13 21:24:34 +020042 TEST_ASSERT_VAL("Wrong build_id", !memcmp(bid.data, expect_build_id,
Remi Bernoned21d6d2020-08-21 18:52:38 +020043 sizeof(expect_build_id)));
44
45 ret = filename__read_debuglink(filename, debuglink, PATH_MAX);
46 TEST_ASSERT_VAL("Failed to read debuglink", ret == 0);
47 TEST_ASSERT_VAL("Wrong debuglink",
48 !strcmp(debuglink, expect_debuglink));
49
50 scnprintf(debugfile, PATH_MAX, "%s/%s", d, debuglink);
Jiri Olsaf7668192020-10-13 21:24:34 +020051 ret = filename__read_build_id(debugfile, &bid);
Remi Bernoned21d6d2020-08-21 18:52:38 +020052 TEST_ASSERT_VAL("Failed to read debug file build_id",
53 ret == sizeof(expect_build_id));
Jiri Olsaf7668192020-10-13 21:24:34 +020054 TEST_ASSERT_VAL("Wrong build_id", !memcmp(bid.data, expect_build_id,
Remi Bernoned21d6d2020-08-21 18:52:38 +020055 sizeof(expect_build_id)));
56
57 dso = dso__new(filename);
58 TEST_ASSERT_VAL("Failed to get dso", dso);
59
60 ret = dso__load_bfd_symbols(dso, debugfile);
61 TEST_ASSERT_VAL("Failed to load symbols", ret == 0);
62
63 dso__sort_by_name(dso);
64 sym = dso__find_symbol_by_name(dso, "main");
65 TEST_ASSERT_VAL("Failed to find main", sym);
66 dso__delete(dso);
67
68 return TEST_OK;
69}
70
Ian Rogers33f44bf2021-11-03 23:41:51 -070071static int test__pe_file_parsing(struct test_suite *test __maybe_unused,
Remi Bernoned21d6d2020-08-21 18:52:38 +020072 int subtest __maybe_unused)
73{
74 struct stat st;
75 char path_dir[PATH_MAX];
76
77 /* First try development tree tests. */
78 if (!lstat("./tests", &st))
79 return run_dir("./tests");
80
81 /* Then installed path. */
82 snprintf(path_dir, PATH_MAX, "%s/tests", get_argv_exec_path());
83
84 if (!lstat(path_dir, &st))
85 return run_dir(path_dir);
86
87 return TEST_SKIP;
88}
89
90#else
91
Ian Rogers33f44bf2021-11-03 23:41:51 -070092static int test__pe_file_parsing(struct test_suite *test __maybe_unused,
Remi Bernoned21d6d2020-08-21 18:52:38 +020093 int subtest __maybe_unused)
94{
95 return TEST_SKIP;
96}
97
98#endif
Ian Rogersd68f0362021-11-03 23:41:50 -070099
100DEFINE_SUITE("PE file support", pe_file_parsing);