From 113c3544f954dbb24ab0bd7c9e6ee0f4dcf0333b Mon Sep 17 00:00:00 2001 From: Max Wash Date: Tue, 12 May 2020 23:23:29 +0100 Subject: [PATCH] Updated magenta crt init --- photon/libc/sys/magenta/__fio.h | 7 ++++--- photon/libc/sys/magenta/fio.c | 12 ++++++++---- photon/libc/sys/magenta/init.c | 10 ++++++++++ photon/libc/sys/magenta/init.h | 14 ++++++++++++++ photon/libc/sys/magenta/machine/x86_64/crt0.s | 8 +++----- 5 files changed, 39 insertions(+), 12 deletions(-) create mode 100644 photon/libc/sys/magenta/init.c create mode 100644 photon/libc/sys/magenta/init.h diff --git a/photon/libc/sys/magenta/__fio.h b/photon/libc/sys/magenta/__fio.h index d99db9f..22dfbbd 100644 --- a/photon/libc/sys/magenta/__fio.h +++ b/photon/libc/sys/magenta/__fio.h @@ -1,5 +1,5 @@ -#ifndef SYS_LINUX___FIO_H_ -#define SYS_LINUX___FIO_H_ +#ifndef SYS_MAGENTA___FIO_H_ +#define SYS_MAGENTA___FIO_H_ #include @@ -11,12 +11,13 @@ extern "C" { struct __io_file { char buf[__FIO_BUFSZ]; - unsigned int buf_idx; + unsigned long buf_idx; mx_handle_t handle; char err; }; extern int __fileno(struct __io_file *f); +extern void __fio_init(mx_handle_t in, mx_handle_t out, mx_handle_t err); extern unsigned int __fio_write(struct __io_file *f, const char *buf, unsigned int sz); extern unsigned int __fio_flush(struct __io_file *f); extern int __fio_error(struct __io_file *f); diff --git a/photon/libc/sys/magenta/fio.c b/photon/libc/sys/magenta/fio.c index 214e43f..e07477a 100644 --- a/photon/libc/sys/magenta/fio.c +++ b/photon/libc/sys/magenta/fio.c @@ -10,13 +10,16 @@ struct __io_file *stdin = &__stdin; struct __io_file *stdout = &__stdout; struct __io_file *stderr = &__stderr; -void __fio_init() +void __fio_init(mx_handle_t in, mx_handle_t out, mx_handle_t err) { + __stdin.handle = in; + __stdout.handle = out; + __stderr.handle = err; } int __fileno(struct __io_file *f) { - return 0; + return (int)f->handle; } unsigned int __fio_write(struct __io_file *f, const char *buf, unsigned int sz) @@ -37,8 +40,9 @@ unsigned int __fio_write(struct __io_file *f, const char *buf, unsigned int sz) unsigned int __fio_flush(struct __io_file *f) { - ssize_t res = 0;//write(f->fd, f->buf, f->buf_idx); - if ((size_t)res != f->buf_idx) { + unsigned long written = 0; + mx_status_t ret = mx_write(f->handle, f->buf_idx, f->buf, &written); + if (ret != MX_OK || written != f->buf_idx) { f->err = 1; } diff --git a/photon/libc/sys/magenta/init.c b/photon/libc/sys/magenta/init.c new file mode 100644 index 0000000..ff26ba7 --- /dev/null +++ b/photon/libc/sys/magenta/init.c @@ -0,0 +1,10 @@ +#include "init.h" +#include "__fio.h" + +extern int main(int, const char **); + +int __crt_init(struct __proc_start_info *info) +{ + __fio_init(info->in, info->out, info->err); + return main(info->argc, info->argv); +} diff --git a/photon/libc/sys/magenta/init.h b/photon/libc/sys/magenta/init.h new file mode 100644 index 0000000..4fc3596 --- /dev/null +++ b/photon/libc/sys/magenta/init.h @@ -0,0 +1,14 @@ +#ifndef SYS_MAGENTA_INIT_H_ +#define SYS_MAGENTA_INIT_H_ + +#include + +struct __proc_start_info { + mx_handle_t in; + mx_handle_t out; + mx_handle_t err; + int argc; + const char **argv; +}; + +#endif diff --git a/photon/libc/sys/magenta/machine/x86_64/crt0.s b/photon/libc/sys/magenta/machine/x86_64/crt0.s index 6941741..d656a2a 100644 --- a/photon/libc/sys/magenta/machine/x86_64/crt0.s +++ b/photon/libc/sys/magenta/machine/x86_64/crt0.s @@ -1,19 +1,17 @@ .global _start .type _start, @function -.extern __fio_init -.type __fio_init, @function +.extern __crt_init +.type __crt_init, @function .extern main .type main, @function _start: - pop %rbp pop %rdi mov %rsp, %rsi andq $-16, %rsp - call __fio_init - call main + call __crt_init movq %rax, %rdi movq $60, %rax syscall