0001
0002 #include <stdbool.h>
0003 #include <inttypes.h>
0004 #include <stdlib.h>
0005 #include <string.h>
0006 #include <linux/bitops.h>
0007 #include <linux/kernel.h>
0008 #include <linux/types.h>
0009 #include <sys/types.h>
0010 #include <sys/stat.h>
0011 #include <unistd.h>
0012 #include <subcmd/exec-cmd.h>
0013
0014 #include "debug.h"
0015 #include "util/build-id.h"
0016 #include "util/symbol.h"
0017 #include "util/dso.h"
0018
0019 #include "tests.h"
0020
0021 #ifdef HAVE_LIBBFD_SUPPORT
0022
0023 static int run_dir(const char *d)
0024 {
0025 char filename[PATH_MAX];
0026 char debugfile[PATH_MAX];
0027 struct build_id bid;
0028 char debuglink[PATH_MAX];
0029 char expect_build_id[] = {
0030 0x5a, 0x0f, 0xd8, 0x82, 0xb5, 0x30, 0x84, 0x22,
0031 0x4b, 0xa4, 0x7b, 0x62, 0x4c, 0x55, 0xa4, 0x69,
0032 };
0033 char expect_debuglink[PATH_MAX] = "pe-file.exe.debug";
0034 struct dso *dso;
0035 struct symbol *sym;
0036 int ret;
0037
0038 scnprintf(filename, PATH_MAX, "%s/pe-file.exe", d);
0039 ret = filename__read_build_id(filename, &bid);
0040 TEST_ASSERT_VAL("Failed to read build_id",
0041 ret == sizeof(expect_build_id));
0042 TEST_ASSERT_VAL("Wrong build_id", !memcmp(bid.data, expect_build_id,
0043 sizeof(expect_build_id)));
0044
0045 ret = filename__read_debuglink(filename, debuglink, PATH_MAX);
0046 TEST_ASSERT_VAL("Failed to read debuglink", ret == 0);
0047 TEST_ASSERT_VAL("Wrong debuglink",
0048 !strcmp(debuglink, expect_debuglink));
0049
0050 scnprintf(debugfile, PATH_MAX, "%s/%s", d, debuglink);
0051 ret = filename__read_build_id(debugfile, &bid);
0052 TEST_ASSERT_VAL("Failed to read debug file build_id",
0053 ret == sizeof(expect_build_id));
0054 TEST_ASSERT_VAL("Wrong build_id", !memcmp(bid.data, expect_build_id,
0055 sizeof(expect_build_id)));
0056
0057 dso = dso__new(filename);
0058 TEST_ASSERT_VAL("Failed to get dso", dso);
0059
0060 ret = dso__load_bfd_symbols(dso, debugfile);
0061 TEST_ASSERT_VAL("Failed to load symbols", ret == 0);
0062
0063 dso__sort_by_name(dso);
0064 sym = dso__find_symbol_by_name(dso, "main");
0065 TEST_ASSERT_VAL("Failed to find main", sym);
0066 dso__delete(dso);
0067
0068 return TEST_OK;
0069 }
0070
0071 static int test__pe_file_parsing(struct test_suite *test __maybe_unused,
0072 int subtest __maybe_unused)
0073 {
0074 struct stat st;
0075 char path_dir[PATH_MAX];
0076
0077
0078 if (!lstat("./tests", &st))
0079 return run_dir("./tests");
0080
0081
0082 snprintf(path_dir, PATH_MAX, "%s/tests", get_argv_exec_path());
0083
0084 if (!lstat(path_dir, &st))
0085 return run_dir(path_dir);
0086
0087 return TEST_SKIP;
0088 }
0089
0090 #else
0091
0092 static int test__pe_file_parsing(struct test_suite *test __maybe_unused,
0093 int subtest __maybe_unused)
0094 {
0095 return TEST_SKIP;
0096 }
0097
0098 #endif
0099
0100 DEFINE_SUITE("PE file support", pe_file_parsing);