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