Move generic start code and memset into arch/generic.h

This commit is contained in:
Lucas Schumacher 2025-05-02 11:36:05 -04:00
parent ab5dd8d9fb
commit d47709b03c
7 changed files with 40 additions and 25 deletions

View File

@ -1,6 +1,8 @@
#if defined(__aarch64__) #if defined(__aarch64__)
#include "../int.h" #include "../int.h"
#include "generic.h"
void exit(int8_t status){ void exit(int8_t status){
asm ( "mov x0, %0\n" // Move the exit status into register x0 asm ( "mov x0, %0\n" // Move the exit status into register x0
"mov x8, #93\n" // Syscall number for 'exit' is 93 in AArch64 "mov x8, #93\n" // Syscall number for 'exit' is 93 in AArch64

31
arch/generic.h Normal file
View File

@ -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 <arch>.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

View File

@ -1,6 +1,8 @@
#if defined(__mips__) #if defined(__mips__)
#include "../int.h" #include "../int.h"
#include "generic.h"
void exit(int8_t status){ void exit(int8_t status){
asm ( asm (
"move $a0, %0\n" "move $a0, %0\n"

View File

@ -1,6 +1,8 @@
#if defined(__x86_64__) #if defined(__x86_64__)
#include "../int.h" #include "../int.h"
#include "generic.h"
#define SYS_EXIT 60 #define SYS_EXIT 60
void exit(int8_t status) { void exit(int8_t status) {
asm volatile( asm volatile(

View File

@ -60,13 +60,3 @@ int main() {
return 69; return 69;
} }
void __libc_start_main() {exit(main());}
void _start() {
__libc_start_main();
}
void __start() {
_start();
}

View File

@ -25,7 +25,7 @@ int main() {
// Set socket options // Set socket options
int opt = 1; 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); write(STDERR, "Socket error\n", 13);
close(socket_fd); close(socket_fd);
return 2; return 2;
@ -89,10 +89,3 @@ int main() {
close(socket_fd); close(socket_fd);
return 0; return 0;
} }
void _start() {
exit(main());
}
void __start() {
exit(main());
}

9
sys.h
View File

@ -142,11 +142,6 @@ int clock_nanosleep(clockid_t clockid, int flags, const struct timespec *t, stru
uint32_t fork(); uint32_t fork();
// Provide memset for clang void *memset(void* s, int c, unsigned long n);
// 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;
}
#endif // !MINIMALSYS_H #endif // !MINIMALSYS_H