diff --git a/arch/aarch64.c b/arch/aarch64.c index 0fd1d87..67af633 100644 --- a/arch/aarch64.c +++ b/arch/aarch64.c @@ -1,6 +1,8 @@ #if defined(__aarch64__) #include "../int.h" +#include "generic.h" + void exit(int8_t status){ asm ( "mov x0, %0\n" // Move the exit status into register x0 "mov x8, #93\n" // Syscall number for 'exit' is 93 in AArch64 diff --git a/arch/generic.h b/arch/generic.h new file mode 100644 index 0000000..96f6478 --- /dev/null +++ b/arch/generic.h @@ -0,0 +1,31 @@ +/* + * This file is not meant to be included by user applications + * It contains generic arch agnostic implementations of helper functions + * and start code that can be included in .c when an architecture + * specific version is not nessisary. + * + * Copyright (c) 2025 Lucas Schumacher. All Rights Reserved. + */ + +#ifndef MEMSET_DEFINED +#define MEMSET_DEFINED +// Generic memset implementation +void *memset(void* s, int c, unsigned long n) { + int8_t* mem = s; + for(long int i = 0; i < n; ++i) mem[i] = c; + return s; +} +#endif // !MEMSET_DEFINED + +#ifndef _START_DEFINED +#define _START_DEFINED +// Generic _start implementation. Can't access any args +extern void exit(int8_t status); +extern int main(int argc, char** argv); +void _start() { + exit(main(0, 0)); +} +void __start() { + exit(main(0, 0)); +} +#endif // !_START_DEFINED diff --git a/arch/mips.c b/arch/mips.c index 4d5f52a..46cb7c4 100644 --- a/arch/mips.c +++ b/arch/mips.c @@ -1,6 +1,8 @@ #if defined(__mips__) #include "../int.h" +#include "generic.h" + void exit(int8_t status){ asm ( "move $a0, %0\n" diff --git a/arch/x86_64.c b/arch/x86_64.c index 24c4f4c..3dfe803 100644 --- a/arch/x86_64.c +++ b/arch/x86_64.c @@ -1,6 +1,8 @@ #if defined(__x86_64__) #include "../int.h" +#include "generic.h" + #define SYS_EXIT 60 void exit(int8_t status) { asm volatile( diff --git a/buildtest.c b/buildtest.c index f439db2..9d70bd0 100644 --- a/buildtest.c +++ b/buildtest.c @@ -60,13 +60,3 @@ int main() { return 69; } - -void __libc_start_main() {exit(main());} - -void _start() { - __libc_start_main(); -} - -void __start() { - _start(); -} diff --git a/socktest.c b/socktest.c index 4e510bb..6beadbe 100644 --- a/socktest.c +++ b/socktest.c @@ -25,7 +25,7 @@ int main() { // Set socket options int opt = 1; - if(0 > setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(optlen))) { + if(0 > setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt))) { write(STDERR, "Socket error\n", 13); close(socket_fd); return 2; @@ -89,10 +89,3 @@ int main() { close(socket_fd); return 0; } - -void _start() { - exit(main()); -} -void __start() { - exit(main()); -} diff --git a/sys.h b/sys.h index 2405368..af6e18d 100644 --- a/sys.h +++ b/sys.h @@ -142,11 +142,6 @@ int clock_nanosleep(clockid_t clockid, int flags, const struct timespec *t, stru uint32_t fork(); -// Provide memset for clang -// TODO: move this (and start code?) to separate .c file -void *memset(void* s, int c, unsigned long n) { - int8_t* mem = s; - for(long int i = 0; i < n; ++i) mem[i] = c; - return s; -} +void *memset(void* s, int c, unsigned long n); + #endif // !MINIMALSYS_H