Skip to content

Commit 9b009f2

Browse files
committed
Use .s file
Signed-off-by: Pablo Galindo <pablogsal@gmail.com>
1 parent 8748cbe commit 9b009f2

File tree

3 files changed

+24
-57
lines changed

3 files changed

+24
-57
lines changed

Makefile.pre.in

+1
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,7 @@ OBJECT_OBJS= \
444444
Objects/cellobject.o \
445445
Objects/classobject.o \
446446
Objects/codeobject.o \
447+
Objects/asm_trampoline.o \
447448
Objects/complexobject.o \
448449
Objects/descrobject.o \
449450
Objects/enumobject.o \

Objects/asm_trampoline.s

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
.text
2+
.globl _Py_trampoline_func_start
3+
_Py_trampoline_func_start:
4+
push %rbp
5+
mov %rsp,%rbp
6+
mov %rdi,%rax
7+
mov %rsi,%rdi
8+
mov %rdx,%rsi
9+
mov %ecx,%edx
10+
call *%rax
11+
pop %rbp
12+
ret
13+
.globl _Py_trampoline_func_end
14+
_Py_trampoline_func_end:

Objects/codeobject.c

+9-57
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,10 @@ _PyCode_Validate(struct _PyCodeConstructor *con)
300300
return 0;
301301
}
302302

303-
py_trampoline compile_blech(void) {
303+
extern void* _Py_trampoline_func_start;
304+
extern void* _Py_trampoline_func_end;
305+
306+
py_trampoline compile_trampoline(void) {
304307
char *memory = mmap(NULL, // address
305308
4096, // size
306309
PROT_READ | PROT_WRITE | PROT_EXEC,
@@ -312,57 +315,10 @@ py_trampoline compile_blech(void) {
312315
exit(1);
313316
}
314317

315-
int i = 0;
316-
317-
memory[i++] = 0x55;
318-
memory[i++] = 0x48;
319-
memory[i++] = 0x89;
320-
memory[i++] = 0xe5;
321-
memory[i++] = 0x48;
322-
memory[i++] = 0x83;
323-
memory[i++] = 0xec;
324-
memory[i++] = 0x20;
325-
memory[i++] = 0x48;
326-
memory[i++] = 0x89;
327-
memory[i++] = 0x7d;
328-
memory[i++] = 0xf8;
329-
memory[i++] = 0x48;
330-
memory[i++] = 0x89;
331-
memory[i++] = 0x75;
332-
memory[i++] = 0xf0;
333-
memory[i++] = 0x48;
334-
memory[i++] = 0x89;
335-
memory[i++] = 0x55;
336-
memory[i++] = 0xe8;
337-
memory[i++] = 0x89;
338-
memory[i++] = 0x4d;
339-
memory[i++] = 0xe4;
340-
memory[i++] = 0x8b;
341-
memory[i++] = 0x55;
342-
memory[i++] = 0xe4;
343-
memory[i++] = 0x48;
344-
memory[i++] = 0x8b;
345-
memory[i++] = 0x4d;
346-
memory[i++] = 0xe8;
347-
memory[i++] = 0x48;
348-
memory[i++] = 0x8b;
349-
memory[i++] = 0x45;
350-
memory[i++] = 0xf0;
351-
memory[i++] = 0x4c;
352-
memory[i++] = 0x8b;
353-
memory[i++] = 0x45;
354-
memory[i++] = 0xf8;
355-
memory[i++] = 0x48;
356-
memory[i++] = 0x89;
357-
memory[i++] = 0xce;
358-
memory[i++] = 0x48;
359-
memory[i++] = 0x89;
360-
memory[i++] = 0xc7;
361-
memory[i++] = 0x41;
362-
memory[i++] = 0xff;
363-
memory[i++] = 0xd0;
364-
memory[i++] = 0xc9;
365-
memory[i++] = 0xc3;
318+
void* start = &_Py_trampoline_func_start;
319+
void* end = &_Py_trampoline_func_end;
320+
size_t ss = end-start;
321+
memcpy(memory, start, ss*sizeof(char));
366322

367323
return (py_trampoline) memory;
368324
}
@@ -391,10 +347,6 @@ void perf_map_write_entry(FILE *method_file, const void* code_addr, unsigned int
391347

392348
typedef PyObject* (*py_evaluator)(PyThreadState *, _PyInterpreterFrame *, int throwflag);
393349

394-
PyObject* the_trampoline(py_evaluator eval, PyThreadState* t, _PyInterpreterFrame* f, int p) {
395-
return eval(t, f,p);
396-
}
397-
398350
static void
399351
init_code(PyCodeObject *co, struct _PyCodeConstructor *con)
400352
{
@@ -410,7 +362,7 @@ init_code(PyCodeObject *co, struct _PyCodeConstructor *con)
410362
Py_INCREF(con->qualname);
411363
co->co_qualname = con->qualname;
412364

413-
py_trampoline f = compile_blech();
365+
py_trampoline f = compile_trampoline();
414366
FILE* pfile = perf_map_open(getpid());
415367
perf_map_write_entry(pfile, f, 4096, PyUnicode_AsUTF8(con->qualname));
416368
perf_map_close(pfile);

0 commit comments

Comments
 (0)