From a65981af98d3e07e254ee3e6f413d7d1bd2cd9f1 Mon Sep 17 00:00:00 2001 From: Janneke Nieuwenhuizen Date: Wed, 3 May 2023 20:59:12 +0200 Subject: [PATCH] x86_64: lib: Use __init_io. * lib/linux/x86_64-mes-gcc/crt1.c (_start): Replace manual initialization of __stdin, __stdout, __stderr, and environ with __init_io (). * lib/linux/x86_64-mes-m2/crt1.M1 (_start): Likewise. * lib/linux/x86_64-mes-mescc/crt1.c (_start): Likewise. --- lib/linux/x86_64-mes-gcc/crt0.c | 2 +- lib/linux/x86_64-mes-gcc/crt1.c | 44 ++++++++----------------------- lib/linux/x86_64-mes-m2/crt1.M1 | 12 +-------- lib/linux/x86_64-mes-mescc/crt1.c | 14 ++-------- 4 files changed, 15 insertions(+), 57 deletions(-) diff --git a/lib/linux/x86_64-mes-gcc/crt0.c b/lib/linux/x86_64-mes-gcc/crt0.c index eb9547e8..d27db794 100644 --- a/lib/linux/x86_64-mes-gcc/crt0.c +++ b/lib/linux/x86_64-mes-gcc/crt0.c @@ -24,7 +24,7 @@ // let's try _main first #include -int main (int argc, char *argv[]); +int main (int argc, char *argv[], char *envp[]); // gcc x86_64 calling convention: // rdi, rsi, rdx, rcx, r8, r9, , diff --git a/lib/linux/x86_64-mes-gcc/crt1.c b/lib/linux/x86_64-mes-gcc/crt1.c index 0000d7bf..a12603d6 100644 --- a/lib/linux/x86_64-mes-gcc/crt1.c +++ b/lib/linux/x86_64-mes-gcc/crt1.c @@ -1,6 +1,6 @@ /* -*-comment-start: "//";comment-end:""-*- * GNU Mes --- Maxwell Equations of Software - * Copyright © 2017,2018 Jan (janneke) Nieuwenhuizen + * Copyright © 2017,2018,2023 Jan (janneke) Nieuwenhuizen * * This file is part of GNU Mes. * @@ -19,7 +19,7 @@ */ #include -// int main (int argc, char *argv[]); +int main (int argc, char *argv[], char *envp[]); // gcc x86_64 calling convention: // rdi, rsi, rdx, rcx, r8, r9, , @@ -29,38 +29,14 @@ void _start () { asm ( - "mov $0,%%eax\n\t" - "mov %%eax,%0\n" - : "=r" (__stdin) - : //no inputs "" - ); + "mov %rbp,%rax\n\t" + "add $8,%rax\n\t" + "mov (%rax),%rax\n\t" + "add $3,%rax\n\t" + "shl $3,%rax\n\t" + "add %rbp,%rax\n\t" + "mov %rax,%rdx\n\t" - asm ( - "mov $1,%%eax\n\t" - "mov %%eax,%0\n" - : "=r" (__stdout) - : //no inputs "" - ); - - asm ( - "mov $2,%%eax\n\t" - "mov %%eax,%0\n" - : "=r" (__stderr) - : //no inputs "" - ); - asm ( - "mov %%rbp,%%rax\n\t" - "add $8,%%rax\n\t" - "mov (%%rax),%%rax\n\t" - "add $3,%%rax\n\t" - "shl $3,%%rax\n\t" - "add %%rbp,%%rax\n\t" - "mov %%rax,%0\n\t" - "mov %%rax,%%rdx\n\t" - : "=r" (environ) - : //no inputs "" - ); - asm ( "mov %rbp,%rax\n\t" "add $16,%rax\n\t" "mov %rax,%rsi\n\t" @@ -69,6 +45,8 @@ _start () "add $8,%rax\n\t" "mov (%rax),%rax\n\t" "mov %rax,%rdi\n\t" + + "call __init_io\n\t" "call main\n\t" "mov %rax,%rdi\n\t" diff --git a/lib/linux/x86_64-mes-m2/crt1.M1 b/lib/linux/x86_64-mes-m2/crt1.M1 index 5a792ff2..4f674a82 100644 --- a/lib/linux/x86_64-mes-m2/crt1.M1 +++ b/lib/linux/x86_64-mes-m2/crt1.M1 @@ -20,14 +20,6 @@ push___%rbp mov____%rsp,%rbp - ;; mes c lib io - mov____$i8,%rax !0 - movl___%eax,0x32 &GLOBAL___stdin - mov____$i8,%rax !1 - movl___%eax,0x32 &GLOBAL___stdout - mov____$i8,%rax !2 - movl___%eax,0x32 &GLOBAL___stderr - ;; M2-Planet pushes forward mov____%rbp,%rax ;argc add____$i8,%rax !8 @@ -46,9 +38,7 @@ add____%rbp,%rax push___%rax - ;; mes c lib environ - mov____%rax,0x32 &GLOBAL_environ - + call32 %FUNCTION___init_io call32 %FUNCTION_main mov____%rax,%rdi diff --git a/lib/linux/x86_64-mes-mescc/crt1.c b/lib/linux/x86_64-mes-mescc/crt1.c index 35d33c27..0d66039f 100644 --- a/lib/linux/x86_64-mes-mescc/crt1.c +++ b/lib/linux/x86_64-mes-mescc/crt1.c @@ -1,6 +1,6 @@ /* -*-comment-start: "//";comment-end:""-*- * GNU Mes --- Maxwell Equations of Software - * Copyright © 2018 Jan (janneke) Nieuwenhuizen + * Copyright © 2018,2023 Jan (janneke) Nieuwenhuizen * * This file is part of GNU Mes. * @@ -24,15 +24,6 @@ int main (int argc, char *argv[], char *envp[]); int _start () { - asm ("mov____$i8,%rax !0"); - asm ("movl___%eax,0x32 &__stdin"); - - asm ("mov____$i8,%rax !1"); - asm ("movl___%eax,0x32 &__stdout"); - - asm ("mov____$i8,%rax !2"); - asm ("movl___%eax,0x32 &__stderr"); - #if 0 //MES_CCAMD64 asm ("add____$i32,%rbp %0x80"); // FIXME: corresponds to x86_64/as.scm function-preamble-fu #endif @@ -45,8 +36,6 @@ _start () asm ("shl____$i8,%rax !0x03"); asm ("add____%rbp,%rax"); - // FIXME: 64-bit addresses... - asm ("mov____%rax,0x32 &environ"); #if 0 //MES_CCAMD64 asm ("mov____%rax,%rdx"); // amd #else @@ -70,6 +59,7 @@ _start () asm ("push___%rax"); // bootstrap #endif + __init_io (); main (); asm ("mov____%rax,%rdi");