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 23ed21d6d7SRemi Bernon static int run_dir(const char *d) 24ed21d6d7SRemi Bernon { 25ed21d6d7SRemi Bernon char filename[PATH_MAX]; 26ed21d6d7SRemi Bernon char debugfile[PATH_MAX]; 27*f766819cSJiri 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; 37ed21d6d7SRemi Bernon 38ed21d6d7SRemi Bernon scnprintf(filename, PATH_MAX, "%s/pe-file.exe", d); 39*f766819cSJiri Olsa ret = filename__read_build_id(filename, &bid); 40ed21d6d7SRemi Bernon TEST_ASSERT_VAL("Failed to read build_id", 41ed21d6d7SRemi Bernon ret == sizeof(expect_build_id)); 42*f766819cSJiri Olsa TEST_ASSERT_VAL("Wrong build_id", !memcmp(bid.data, expect_build_id, 43ed21d6d7SRemi Bernon sizeof(expect_build_id))); 44ed21d6d7SRemi Bernon 45ed21d6d7SRemi Bernon ret = filename__read_debuglink(filename, debuglink, PATH_MAX); 46ed21d6d7SRemi Bernon TEST_ASSERT_VAL("Failed to read debuglink", ret == 0); 47ed21d6d7SRemi Bernon TEST_ASSERT_VAL("Wrong debuglink", 48ed21d6d7SRemi Bernon !strcmp(debuglink, expect_debuglink)); 49ed21d6d7SRemi Bernon 50ed21d6d7SRemi Bernon scnprintf(debugfile, PATH_MAX, "%s/%s", d, debuglink); 51*f766819cSJiri Olsa ret = filename__read_build_id(debugfile, &bid); 52ed21d6d7SRemi Bernon TEST_ASSERT_VAL("Failed to read debug file build_id", 53ed21d6d7SRemi Bernon ret == sizeof(expect_build_id)); 54*f766819cSJiri Olsa TEST_ASSERT_VAL("Wrong build_id", !memcmp(bid.data, expect_build_id, 55ed21d6d7SRemi Bernon sizeof(expect_build_id))); 56ed21d6d7SRemi Bernon 57ed21d6d7SRemi Bernon dso = dso__new(filename); 58ed21d6d7SRemi Bernon TEST_ASSERT_VAL("Failed to get dso", dso); 59ed21d6d7SRemi Bernon 60ed21d6d7SRemi Bernon ret = dso__load_bfd_symbols(dso, debugfile); 61ed21d6d7SRemi Bernon TEST_ASSERT_VAL("Failed to load symbols", ret == 0); 62ed21d6d7SRemi Bernon 63ed21d6d7SRemi Bernon dso__sort_by_name(dso); 64ed21d6d7SRemi Bernon sym = dso__find_symbol_by_name(dso, "main"); 65ed21d6d7SRemi Bernon TEST_ASSERT_VAL("Failed to find main", sym); 66ed21d6d7SRemi Bernon dso__delete(dso); 67ed21d6d7SRemi Bernon 68ed21d6d7SRemi Bernon return TEST_OK; 69ed21d6d7SRemi Bernon } 70ed21d6d7SRemi Bernon 71ed21d6d7SRemi Bernon int test__pe_file_parsing(struct test *test __maybe_unused, 72ed21d6d7SRemi Bernon int subtest __maybe_unused) 73ed21d6d7SRemi Bernon { 74ed21d6d7SRemi Bernon struct stat st; 75ed21d6d7SRemi Bernon char path_dir[PATH_MAX]; 76ed21d6d7SRemi Bernon 77ed21d6d7SRemi Bernon /* First try development tree tests. */ 78ed21d6d7SRemi Bernon if (!lstat("./tests", &st)) 79ed21d6d7SRemi Bernon return run_dir("./tests"); 80ed21d6d7SRemi Bernon 81ed21d6d7SRemi Bernon /* Then installed path. */ 82ed21d6d7SRemi Bernon snprintf(path_dir, PATH_MAX, "%s/tests", get_argv_exec_path()); 83ed21d6d7SRemi Bernon 84ed21d6d7SRemi Bernon if (!lstat(path_dir, &st)) 85ed21d6d7SRemi Bernon return run_dir(path_dir); 86ed21d6d7SRemi Bernon 87ed21d6d7SRemi Bernon return TEST_SKIP; 88ed21d6d7SRemi Bernon } 89ed21d6d7SRemi Bernon 90ed21d6d7SRemi Bernon #else 91ed21d6d7SRemi Bernon 92ed21d6d7SRemi Bernon int test__pe_file_parsing(struct test *test __maybe_unused, 93ed21d6d7SRemi Bernon int subtest __maybe_unused) 94ed21d6d7SRemi Bernon { 95ed21d6d7SRemi Bernon return TEST_SKIP; 96ed21d6d7SRemi Bernon } 97ed21d6d7SRemi Bernon 98ed21d6d7SRemi Bernon #endif 99