Repository navigation
Expand file tree
/
Copy pathdasm_driver.c
More file actions
73 lines (59 loc) · 1.87 KB
/
Copy pathdasm_driver.c
File metadata and controls
73 lines (59 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//
// Created by Michael Nolan on 10/7/16.
//
#include "dasm_driver.h"
// Driver file for DynASM-based JITs.
#include <assert.h>
#include <stdio.h>
#include <sys/mman.h>
#include <fcntl.h>
#define GLOB_MAX 256
void initjit(dasm_State **state, const void *actionlist);
void *jitcode(dasm_State **state);
void free_jitcode(void *code);
void* global_labels[GLOB_MAX];
int dev_zero_fileno;
void initjit(dasm_State **state, const void *actionlist) {
dasm_init(state, 1);
dasm_setupglobal(state,global_labels,GLOB_MAX);
dasm_setup(state, actionlist);
dasm_growpc(state,256);
dev_zero_fileno = open("/dev/zero", O_RDWR);
}
void *jitcode(dasm_State **state) {
size_t size;
int dasm_status = dasm_link(state, &size);
assert(dasm_status == 0);
// Allocate memory readable and writable so we can
// write the encoded instructions there.
char *mem = mmap(NULL, size + sizeof(size_t),
PROT_READ | PROT_WRITE,
MAP_PRIVATE, dev_zero_fileno, 0);
assert(mem != MAP_FAILED);
// Store length at the beginning of the region, so we
// can free it without additional context.
*(size_t*)mem = size;
void *ret = mem + sizeof(size_t);
dasm_encode(state, ret);
dasm_free(state);
// Adjust the memory permissions so it is executable
// but no longer writable.
int success = mprotect(mem, size, PROT_EXEC | PROT_READ);
assert(success == 0);
#ifndef NDEBUG
// Write generated machine code to a temporary file.
// View with:
// objdump -D -b binary -mi386 -Mx86-64 /tmp/jitcode
// Or:
// ndisasm -b 64 /tmp/jitcode
FILE *f = fopen("/tmp/jitcode", "wb");
fwrite(ret, size, 1, f);
fclose(f);
#endif
return ret;
}
void free_jitcode(void *code) {
void *mem = (char*)code - sizeof(size_t);
int status = munmap(mem, *(size_t*)mem);
assert(status == 0);
}