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