genelf.c 995 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <limits.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. #include <linux/compiler.h>
  8. #include "debug.h"
  9. #include "tests.h"
  10. #ifdef HAVE_JITDUMP
  11. #include <libelf.h>
  12. #include "../util/genelf.h"
  13. #endif
  14. #define TEMPL "/tmp/perf-test-XXXXXX"
  15. int test__jit_write_elf(struct test *test __maybe_unused,
  16. int subtest __maybe_unused)
  17. {
  18. #ifdef HAVE_JITDUMP
  19. static unsigned char x86_code[] = {
  20. 0xBB, 0x2A, 0x00, 0x00, 0x00, /* movl $42, %ebx */
  21. 0xB8, 0x01, 0x00, 0x00, 0x00, /* movl $1, %eax */
  22. 0xCD, 0x80 /* int $0x80 */
  23. };
  24. char path[PATH_MAX];
  25. int fd, ret;
  26. strcpy(path, TEMPL);
  27. fd = mkstemp(path);
  28. if (fd < 0) {
  29. perror("mkstemp failed");
  30. return TEST_FAIL;
  31. }
  32. pr_info("Writing jit code to: %s\n", path);
  33. ret = jit_write_elf(fd, 0, "main", x86_code, sizeof(x86_code),
  34. NULL, 0, NULL, 0, 0);
  35. close(fd);
  36. unlink(path);
  37. return ret ? TEST_FAIL : 0;
  38. #else
  39. return TEST_SKIP;
  40. #endif
  41. }