x86: modernize

arm_linker_fix
Ariadne Conill 2020-03-29 15:04:03 +00:00
parent d8cfe83e34
commit d0ccf2f96f
6 changed files with 83 additions and 79 deletions

View File

@ -1,18 +1,26 @@
#ifndef __ARCH_X86_DEFS_H #ifndef __ARCH_X86_DEFS_H
#define __ARCH_X86_DEFS_H #define __ARCH_X86_DEFS_H
#define OFFSET_REG_GS 20 #ifndef REG_GS
#define OFFSET_REG_FS 24 # define REG_GS (0)
#define OFFSET_REG_ES 28 # define REG_FS (1)
#define OFFSET_REG_DS 32 # define REG_ES (2)
#define OFFSET_REG_EDI 36 # define REG_DS (3)
#define OFFSET_REG_ESI 40 # define REG_EDI (4)
#define OFFSET_REG_EBP 44 # define REG_ESI (5)
#define OFFSET_REG_ESP 48 # define REG_EBP (6)
#define OFFSET_REG_EBX 52 # define REG_ESP (7)
#define OFFSET_REG_EDX 56 # define REG_EBX (8)
#define OFFSET_REG_ECX 60 # define REG_EDX (9)
#define OFFSET_REG_EAX 64 # define REG_ECX (10)
#define OFFSET_REG_EIP 76 # define REG_EAX (11)
# define REG_EIP (14)
#endif
#define REG_SZ (4)
#define MCONTEXT_GREGS (20)
#include "common-defs.h"
#endif #endif

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2018 Ariadne Conill <ariadne@dereferenced.org> * Copyright (c) 2018, 2020 Ariadne Conill <ariadne@dereferenced.org>
* *
* Permission to use, copy, modify, and/or distribute this software for any * Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above * purpose with or without fee is hereby granted, provided that the above
@ -12,40 +12,38 @@
#include "defs.h" #include "defs.h"
.globl __getcontext; ALIAS(getcontext, __getcontext)
__getcontext:
FUNC(__getcontext)
/* load address of the ucontext structure */ /* load address of the ucontext structure */
movl 4(%esp), %eax movl 4(%esp), %eax
/* EAX is not a preserved register */ /* EAX is not a preserved register */
movl $0, OFFSET_REG_EAX(%eax) movl $0, REG_OFFSET(REG_EAX)(%eax)
/* copy all of the current registers into the ucontext structure */ /* copy all of the current registers into the ucontext structure */
movl %ecx, OFFSET_REG_ECX(%eax) movl %ecx, REG_OFFSET(REG_ECX)(%eax)
movl %ebx, OFFSET_REG_EBX(%eax) movl %ebx, REG_OFFSET(REG_EBX)(%eax)
movl %edx, OFFSET_REG_EDX(%eax) movl %edx, REG_OFFSET(REG_EDX)(%eax)
movl %edi, OFFSET_REG_EDI(%eax) movl %edi, REG_OFFSET(REG_EDI)(%eax)
movl %esi, OFFSET_REG_ESI(%eax) movl %esi, REG_OFFSET(REG_ESI)(%eax)
movl %ebp, OFFSET_REG_EBP(%eax) movl %ebp, REG_OFFSET(REG_EBP)(%eax)
/* the first argument on the stack is the jump target (%eip), so we store it in the EIP /* the first argument on the stack is the jump target (%eip), so we store it in the EIP
register in the ucontext structure. */ register in the ucontext structure. */
movl (%esp), %ecx movl (%esp), %ecx
movl %ecx, OFFSET_REG_EIP(%eax) movl %ecx, REG_OFFSET(REG_EIP)(%eax)
/* take the stack pointer address (%esp) offsetting by 4 to skip over the jump target. */ /* take the stack pointer address (%esp) offsetting by 4 to skip over the jump target. */
leal 4(%esp), %ecx leal 4(%esp), %ecx
movl %ecx, OFFSET_REG_ESP(%eax) movl %ecx, REG_OFFSET(REG_ESP)(%eax)
/* finally, save the FS segment register */ /* finally, save the FS segment register */
xorl %ecx, %ecx xorl %ecx, %ecx
movw %fs, %cx movw %fs, %cx
movl %ecx, OFFSET_REG_FS(%eax) movl %ecx, REG_OFFSET(REG_FS)(%eax)
/* we're all done here, return 0 */ /* we're all done here, return 0 */
xorl %eax, %eax xorl %eax, %eax
ret ret
END(__getcontext)
.weak getcontext;
getcontext = __getcontext;

View File

@ -41,7 +41,6 @@ __makecontext(ucontext_t *ucp, void (*func)(void), int argc, ...)
argp = sp; argp = sp;
*argp++ = (uintptr_t) &__start_context; *argp++ = (uintptr_t) &__start_context;
*argp++ = (uintptr_t) ucp->uc_link;
va_start(va, argc); va_start(va, argc);
@ -49,6 +48,8 @@ __makecontext(ucontext_t *ucp, void (*func)(void), int argc, ...)
*argp++ = va_arg (va, greg_t); *argp++ = va_arg (va, greg_t);
va_end(va); va_end(va);
*argp++ = (uintptr_t) ucp->uc_link;
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2018 Ariadne Conill <ariadne@dereferenced.org> * Copyright (c) 2018, 2020 Ariadne Conill <ariadne@dereferenced.org>
* *
* Permission to use, copy, modify, and/or distribute this software for any * Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above * purpose with or without fee is hereby granted, provided that the above
@ -12,35 +12,33 @@
#include "defs.h" #include "defs.h"
.globl __setcontext; ALIAS(setcontext, __setcontext)
__setcontext:
FUNC(__setcontext)
/* load address of the ucontext structure */ /* load address of the ucontext structure */
movl 4(%esp), %eax movl 4(%esp), %eax
/* set up the FS segment register */ /* set up the FS segment register */
movl OFFSET_REG_FS(%eax), %ecx movl REG_OFFSET(REG_FS)(%eax), %ecx
movw %cx, %fs movw %cx, %fs
/* fetch the new EIP */ /* fetch the new EIP */
movl OFFSET_REG_EIP(%eax), %ecx movl REG_OFFSET(REG_EIP)(%eax), %ecx
/* set up the new stack pointer */ /* set up the new stack pointer */
movl OFFSET_REG_ESP(%eax), %esp movl REG_OFFSET(REG_ESP)(%eax), %esp
/* push the return address onto the stack */ /* push the return address onto the stack */
pushl %ecx pushl %ecx
/* set all of the registers */ /* set all of the registers */
movl OFFSET_REG_EBX(%eax), %ebx movl REG_OFFSET(REG_EBX)(%eax), %ebx
movl OFFSET_REG_ECX(%eax), %ecx movl REG_OFFSET(REG_ECX)(%eax), %ecx
movl OFFSET_REG_EDX(%eax), %edx movl REG_OFFSET(REG_EDX)(%eax), %edx
movl OFFSET_REG_EBP(%eax), %ebp movl REG_OFFSET(REG_EBP)(%eax), %ebp
movl OFFSET_REG_EDI(%eax), %edi movl REG_OFFSET(REG_EDI)(%eax), %edi
movl OFFSET_REG_ESI(%eax), %esi movl REG_OFFSET(REG_ESI)(%eax), %esi
movl OFFSET_REG_EAX(%eax), %eax movl REG_OFFSET(REG_EAX)(%eax), %eax
ret ret
END(__setcontext)
.weak setcontext;
setcontext = __setcontext;

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2018 Ariadne Conill <ariadne@dereferenced.org> * Copyright (c) 2018, 2020 Ariadne Conill <ariadne@dereferenced.org>
* *
* Permission to use, copy, modify, and/or distribute this software for any * Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above * purpose with or without fee is hereby granted, provided that the above
@ -10,8 +10,9 @@
* from the use of this software. * from the use of this software.
*/ */
.globl __start_context; #include "defs.h"
__start_context:
FUNC(__start_context)
/* get the proper context into position and test for NULL */ /* get the proper context into position and test for NULL */
leal (%esp,%ebx,4), %esp leal (%esp,%ebx,4), %esp
cmpl $0, (%esp) cmpl $0, (%esp)
@ -34,7 +35,7 @@ no_linked_context:
/* something is really hosed, call hlt to force termination */ /* something is really hosed, call hlt to force termination */
hlt hlt
__i686.get_pc_thunk.bx: __i686.get_pc_thunk.bx:
mov (%esp), %ebx mov (%esp), %ebx
ret ret
END(__start_context)

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2018 Ariadne Conill <ariadne@dereferenced.org> * Copyright (c) 2018, 2020 Ariadne Conill <ariadne@dereferenced.org>
* *
* Permission to use, copy, modify, and/or distribute this software for any * Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above * purpose with or without fee is hereby granted, provided that the above
@ -12,63 +12,61 @@
#include "defs.h" #include "defs.h"
.globl __swapcontext; ALIAS(swapcontext, __swapcontext)
__swapcontext:
FUNC(__swapcontext)
/* load address of the ucontext structure */ /* load address of the ucontext structure */
movl 4(%esp), %eax movl 4(%esp), %eax
/* EAX is not a preserved register */ /* EAX is not a preserved register */
movl $0, OFFSET_REG_EAX(%eax) movl $0, REG_OFFSET(REG_EAX)(%eax)
/* copy all of the current registers into the ucontext structure */ /* copy all of the current registers into the ucontext structure */
movl %ecx, OFFSET_REG_ECX(%eax) movl %ecx, REG_OFFSET(REG_ECX)(%eax)
movl %ebx, OFFSET_REG_EBX(%eax) movl %ebx, REG_OFFSET(REG_EBX)(%eax)
movl %edx, OFFSET_REG_EDX(%eax) movl %edx, REG_OFFSET(REG_EDX)(%eax)
movl %edi, OFFSET_REG_EDI(%eax) movl %edi, REG_OFFSET(REG_EDI)(%eax)
movl %esi, OFFSET_REG_ESI(%eax) movl %esi, REG_OFFSET(REG_ESI)(%eax)
movl %ebp, OFFSET_REG_EBP(%eax) movl %ebp, REG_OFFSET(REG_EBP)(%eax)
/* the first argument on the stack is the jump target (%eip), so we store it in the EIP /* the first argument on the stack is the jump target (%eip), so we store it in the EIP
register in the ucontext structure. */ register in the ucontext structure. */
movl (%esp), %ecx movl (%esp), %ecx
movl %ecx, OFFSET_REG_EIP(%eax) movl %ecx, REG_OFFSET(REG_EIP)(%eax)
/* take the stack pointer address (%esp) offsetting by 4 to skip over the jump target. */ /* take the stack pointer address (%esp) offsetting by 4 to skip over the jump target. */
leal 4(%esp), %ecx leal 4(%esp), %ecx
movl %ecx, OFFSET_REG_ESP(%eax) movl %ecx, REG_OFFSET(REG_ESP)(%eax)
/* finally, save the FS segment register */ /* finally, save the FS segment register */
xorl %ecx, %ecx xorl %ecx, %ecx
movw %fs, %cx movw %fs, %cx
movl %ecx, OFFSET_REG_FS(%eax) movl %ecx, REG_OFFSET(REG_FS)(%eax)
/* load address of the ucontext structure */ /* load address of the ucontext structure */
movl 8(%esp), %eax movl 8(%esp), %eax
/* set up the FS segment register */ /* set up the FS segment register */
movl OFFSET_REG_FS(%eax), %ecx movl REG_OFFSET(REG_FS)(%eax), %ecx
movw %cx, %fs movw %cx, %fs
/* fetch the new EIP */ /* fetch the new EIP */
movl OFFSET_REG_EIP(%eax), %ecx movl REG_OFFSET(REG_EIP)(%eax), %ecx
/* set up the new stack pointer */ /* set up the new stack pointer */
movl OFFSET_REG_ESP(%eax), %esp movl REG_OFFSET(REG_ESP)(%eax), %esp
/* push the return address onto the stack */ /* push the return address onto the stack */
pushl %ecx pushl %ecx
/* set all of the registers */ /* set all of the registers */
movl OFFSET_REG_EBX(%eax), %ebx movl REG_OFFSET(REG_EBX)(%eax), %ebx
movl OFFSET_REG_ECX(%eax), %ecx movl REG_OFFSET(REG_ECX)(%eax), %ecx
movl OFFSET_REG_EDX(%eax), %edx movl REG_OFFSET(REG_EDX)(%eax), %edx
movl OFFSET_REG_EBP(%eax), %ebp movl REG_OFFSET(REG_EBP)(%eax), %ebp
movl OFFSET_REG_EDI(%eax), %edi movl REG_OFFSET(REG_EDI)(%eax), %edi
movl OFFSET_REG_ESI(%eax), %esi movl REG_OFFSET(REG_ESI)(%eax), %esi
movl OFFSET_REG_EAX(%eax), %eax movl REG_OFFSET(REG_EAX)(%eax), %eax
ret ret
END(__swapcontext)
.weak swapcontext;
swapcontext = __swapcontext;