pe-file-parsing.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // 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. #include "debug.h"
  14. #include "util/build-id.h"
  15. #include "util/symbol.h"
  16. #include "util/dso.h"
  17. #include "tests.h"
  18. #ifdef HAVE_LIBBFD_SUPPORT
  19. static int run_dir(const char *d)
  20. {
  21. char filename[PATH_MAX];
  22. char debugfile[PATH_MAX];
  23. struct build_id bid;
  24. char debuglink[PATH_MAX];
  25. char expect_build_id[] = {
  26. 0x5a, 0x0f, 0xd8, 0x82, 0xb5, 0x30, 0x84, 0x22,
  27. 0x4b, 0xa4, 0x7b, 0x62, 0x4c, 0x55, 0xa4, 0x69,
  28. };
  29. char expect_debuglink[PATH_MAX] = "pe-file.exe.debug";
  30. struct dso *dso;
  31. struct symbol *sym;
  32. int ret;
  33. scnprintf(filename, PATH_MAX, "%s/pe-file.exe", d);
  34. ret = filename__read_build_id(filename, &bid);
  35. TEST_ASSERT_VAL("Failed to read build_id",
  36. ret == sizeof(expect_build_id));
  37. TEST_ASSERT_VAL("Wrong build_id", !memcmp(bid.data, expect_build_id,
  38. sizeof(expect_build_id)));
  39. ret = filename__read_debuglink(filename, debuglink, PATH_MAX);
  40. TEST_ASSERT_VAL("Failed to read debuglink", ret == 0);
  41. TEST_ASSERT_VAL("Wrong debuglink",
  42. !strcmp(debuglink, expect_debuglink));
  43. scnprintf(debugfile, PATH_MAX, "%s/%s", d, debuglink);
  44. ret = filename__read_build_id(debugfile, &bid);
  45. TEST_ASSERT_VAL("Failed to read debug file build_id",
  46. ret == sizeof(expect_build_id));
  47. TEST_ASSERT_VAL("Wrong build_id", !memcmp(bid.data, expect_build_id,
  48. sizeof(expect_build_id)));
  49. dso = dso__new(filename);
  50. TEST_ASSERT_VAL("Failed to get dso", dso);
  51. ret = dso__load_bfd_symbols(dso, debugfile);
  52. TEST_ASSERT_VAL("Failed to load symbols", ret == 0);
  53. dso__sort_by_name(dso);
  54. sym = dso__find_symbol_by_name(dso, "main");
  55. TEST_ASSERT_VAL("Failed to find main", sym);
  56. dso__delete(dso);
  57. return TEST_OK;
  58. }
  59. int test__pe_file_parsing(struct test *test __maybe_unused,
  60. int subtest __maybe_unused)
  61. {
  62. struct stat st;
  63. char path_dir[PATH_MAX];
  64. /* First try development tree tests. */
  65. if (!lstat("./tests", &st))
  66. return run_dir("./tests");
  67. /* Then installed path. */
  68. snprintf(path_dir, PATH_MAX, "%s/tests", get_argv_exec_path());
  69. if (!lstat(path_dir, &st))
  70. return run_dir(path_dir);
  71. return TEST_SKIP;
  72. }
  73. #else
  74. int test__pe_file_parsing(struct test *test __maybe_unused,
  75. int subtest __maybe_unused)
  76. {
  77. return TEST_SKIP;
  78. }
  79. #endif