From 3f9d6f0311088a389342371acb3168b45c7dece5 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Wed, 12 Apr 2023 17:53:02 +0300 Subject: [PATCH] LibC: add needed stubs to build executables with our compiler --- kernel/arch/i386/crt0.S | 2 +- kernel/arch/x86_64/crt0.S | 2 +- libc/include/stdio.h | 3 +++ libc/include/stdlib.h | 1 + libc/include/sys/syscall.h | 3 +++ libc/include/unistd.h | 7 ++++--- libc/stdlib.cpp | 10 +++++++++- libc/unistd.cpp | 35 +++++++++++++++++++++++++++++++++++ 8 files changed, 57 insertions(+), 6 deletions(-) create mode 100644 libc/include/sys/syscall.h diff --git a/kernel/arch/i386/crt0.S b/kernel/arch/i386/crt0.S index 127309fcbe..2445332491 100644 --- a/kernel/arch/i386/crt0.S +++ b/kernel/arch/i386/crt0.S @@ -9,7 +9,7 @@ _start: movl %esp, %ebp # Prepare signals, memory allocation, stdio and such. - call initialize_standard_library + #call initialize_standard_library # Run the global constructors. call _init diff --git a/kernel/arch/x86_64/crt0.S b/kernel/arch/x86_64/crt0.S index bdc038e6cd..889e498328 100644 --- a/kernel/arch/x86_64/crt0.S +++ b/kernel/arch/x86_64/crt0.S @@ -13,7 +13,7 @@ _start: pushq %rdi # Prepare signals, memory allocation, stdio and such. - call initialize_standard_library + #call initialize_standard_library # Run the global constructors. call _init diff --git a/libc/include/stdio.h b/libc/include/stdio.h index 107d4c1616..c62de6a9b6 100644 --- a/libc/include/stdio.h +++ b/libc/include/stdio.h @@ -15,6 +15,9 @@ #define SEEK_END 1 #define SEEK_SET 2 +#define EXIT_FAILURE 1 +#define EXIT_SUCCESS 0 + __BEGIN_DECLS struct FILE; diff --git a/libc/include/stdlib.h b/libc/include/stdlib.h index e725f071f9..c0c7d3b8e9 100644 --- a/libc/include/stdlib.h +++ b/libc/include/stdlib.h @@ -6,6 +6,7 @@ __BEGIN_DECLS [[noreturn]] void abort(void); +[[noreturn]] void exit(int); int abs(int); diff --git a/libc/include/sys/syscall.h b/libc/include/sys/syscall.h new file mode 100644 index 0000000000..767850e931 --- /dev/null +++ b/libc/include/sys/syscall.h @@ -0,0 +1,3 @@ +#pragma once + +#include diff --git a/libc/include/unistd.h b/libc/include/unistd.h index 3d908d5eb3..ef50a6eff2 100644 --- a/libc/include/unistd.h +++ b/libc/include/unistd.h @@ -6,16 +6,17 @@ #define STDOUT_FILENO 1 #define STDERR_FILENO 2 -// fork(), execv(), execve(), execvp(), getpid() - __BEGIN_DECLS +[[noreturn]] void _exit(int); + pid_t fork(void); +pid_t getpid(void); int execv(const char*, char* const[]); int execve(const char*, char* const[], char* const[]); int execvp(const char*, char* const[]); -pid_t getpid(void); +long syscall(long, ...); __END_DECLS \ No newline at end of file diff --git a/libc/stdlib.cpp b/libc/stdlib.cpp index 2824937d17..b500d27f56 100644 --- a/libc/stdlib.cpp +++ b/libc/stdlib.cpp @@ -1,10 +1,18 @@ +#include +#include #include #include -#include +#include void abort(void) { + ASSERT_NOT_REACHED(); +} +void exit(int status) +{ + _exit(status); + ASSERT_NOT_REACHED(); } int abs(int val) diff --git a/libc/unistd.cpp b/libc/unistd.cpp index 2383052ab6..d28197b6cf 100644 --- a/libc/unistd.cpp +++ b/libc/unistd.cpp @@ -1,5 +1,40 @@ +#include +#include +#include +#include #include +void _exit(int status) +{ + syscall(SYS_EXIT, status); + ASSERT_NOT_REACHED(); +} + +long syscall(long syscall, ...) +{ + va_list args; + va_start(args, syscall); + + long ret = 0; + + switch (syscall) + { + case SYS_EXIT: + ret = Kernel::syscall(SYS_EXIT, va_arg(args, int)); + break; + case SYS_READ: + ret = Kernel::syscall(SYS_READ, va_arg(args, int), va_arg(args, void*), va_arg(args, size_t)); + break; + case SYS_WRITE: + ret = Kernel::syscall(SYS_WRITE, va_arg(args, int), va_arg(args, const void*), va_arg(args, size_t)); + break; + } + + va_end(args); + + return ret; +} + pid_t fork(void) { return -1;