18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci#include <stdbool.h>
38c2ecf20Sopenharmony_ci#include <inttypes.h>
48c2ecf20Sopenharmony_ci#include <stdlib.h>
58c2ecf20Sopenharmony_ci#include <string.h>
68c2ecf20Sopenharmony_ci#include <linux/bitops.h>
78c2ecf20Sopenharmony_ci#include <linux/kernel.h>
88c2ecf20Sopenharmony_ci#include <linux/types.h>
98c2ecf20Sopenharmony_ci#include <sys/types.h>
108c2ecf20Sopenharmony_ci#include <sys/stat.h>
118c2ecf20Sopenharmony_ci#include <unistd.h>
128c2ecf20Sopenharmony_ci#include <subcmd/exec-cmd.h>
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#include "debug.h"
158c2ecf20Sopenharmony_ci#include "util/build-id.h"
168c2ecf20Sopenharmony_ci#include "util/symbol.h"
178c2ecf20Sopenharmony_ci#include "util/dso.h"
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#include "tests.h"
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#ifdef HAVE_LIBBFD_SUPPORT
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_cistatic int run_dir(const char *d)
248c2ecf20Sopenharmony_ci{
258c2ecf20Sopenharmony_ci	char filename[PATH_MAX];
268c2ecf20Sopenharmony_ci	char debugfile[PATH_MAX];
278c2ecf20Sopenharmony_ci	struct build_id bid;
288c2ecf20Sopenharmony_ci	char debuglink[PATH_MAX];
298c2ecf20Sopenharmony_ci	char expect_build_id[] = {
308c2ecf20Sopenharmony_ci		0x5a, 0x0f, 0xd8, 0x82, 0xb5, 0x30, 0x84, 0x22,
318c2ecf20Sopenharmony_ci		0x4b, 0xa4, 0x7b, 0x62, 0x4c, 0x55, 0xa4, 0x69,
328c2ecf20Sopenharmony_ci	};
338c2ecf20Sopenharmony_ci	char expect_debuglink[PATH_MAX] = "pe-file.exe.debug";
348c2ecf20Sopenharmony_ci	struct dso *dso;
358c2ecf20Sopenharmony_ci	struct symbol *sym;
368c2ecf20Sopenharmony_ci	int ret;
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci	scnprintf(filename, PATH_MAX, "%s/pe-file.exe", d);
398c2ecf20Sopenharmony_ci	ret = filename__read_build_id(filename, &bid);
408c2ecf20Sopenharmony_ci	TEST_ASSERT_VAL("Failed to read build_id",
418c2ecf20Sopenharmony_ci			ret == sizeof(expect_build_id));
428c2ecf20Sopenharmony_ci	TEST_ASSERT_VAL("Wrong build_id", !memcmp(bid.data, expect_build_id,
438c2ecf20Sopenharmony_ci						  sizeof(expect_build_id)));
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci	ret = filename__read_debuglink(filename, debuglink, PATH_MAX);
468c2ecf20Sopenharmony_ci	TEST_ASSERT_VAL("Failed to read debuglink", ret == 0);
478c2ecf20Sopenharmony_ci	TEST_ASSERT_VAL("Wrong debuglink",
488c2ecf20Sopenharmony_ci			!strcmp(debuglink, expect_debuglink));
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci	scnprintf(debugfile, PATH_MAX, "%s/%s", d, debuglink);
518c2ecf20Sopenharmony_ci	ret = filename__read_build_id(debugfile, &bid);
528c2ecf20Sopenharmony_ci	TEST_ASSERT_VAL("Failed to read debug file build_id",
538c2ecf20Sopenharmony_ci			ret == sizeof(expect_build_id));
548c2ecf20Sopenharmony_ci	TEST_ASSERT_VAL("Wrong build_id", !memcmp(bid.data, expect_build_id,
558c2ecf20Sopenharmony_ci						  sizeof(expect_build_id)));
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	dso = dso__new(filename);
588c2ecf20Sopenharmony_ci	TEST_ASSERT_VAL("Failed to get dso", dso);
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci	ret = dso__load_bfd_symbols(dso, debugfile);
618c2ecf20Sopenharmony_ci	TEST_ASSERT_VAL("Failed to load symbols", ret == 0);
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci	dso__sort_by_name(dso);
648c2ecf20Sopenharmony_ci	sym = dso__find_symbol_by_name(dso, "main");
658c2ecf20Sopenharmony_ci	TEST_ASSERT_VAL("Failed to find main", sym);
668c2ecf20Sopenharmony_ci	dso__delete(dso);
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci	return TEST_OK;
698c2ecf20Sopenharmony_ci}
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ciint test__pe_file_parsing(struct test *test __maybe_unused,
728c2ecf20Sopenharmony_ci			  int subtest __maybe_unused)
738c2ecf20Sopenharmony_ci{
748c2ecf20Sopenharmony_ci	struct stat st;
758c2ecf20Sopenharmony_ci	char path_dir[PATH_MAX];
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	/* First try development tree tests. */
788c2ecf20Sopenharmony_ci	if (!lstat("./tests", &st))
798c2ecf20Sopenharmony_ci		return run_dir("./tests");
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	/* Then installed path. */
828c2ecf20Sopenharmony_ci	snprintf(path_dir, PATH_MAX, "%s/tests", get_argv_exec_path());
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	if (!lstat(path_dir, &st))
858c2ecf20Sopenharmony_ci		return run_dir(path_dir);
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	return TEST_SKIP;
888c2ecf20Sopenharmony_ci}
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci#else
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ciint test__pe_file_parsing(struct test *test __maybe_unused,
938c2ecf20Sopenharmony_ci			  int subtest __maybe_unused)
948c2ecf20Sopenharmony_ci{
958c2ecf20Sopenharmony_ci	return TEST_SKIP;
968c2ecf20Sopenharmony_ci}
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci#endif
99