From ef0a39547c1e4122a9e7d6c0256f05ed3a0512f6 Mon Sep 17 00:00:00 2001 From: Jan Nieuwenhuizen Date: Thu, 7 Jun 2018 18:02:21 +0200 Subject: [PATCH] mescc: Support gcc-3.0: Implement atexit. --- include/stdlib.h | 4 +++- include/unistd.h | 3 --- lib/gcc.c | 8 ++++++++ lib/linux-mini-gcc.c | 14 ++++++++++++-- lib/linux-mini-mes.c | 12 +++++++++++- 5 files changed, 34 insertions(+), 7 deletions(-) diff --git a/include/stdlib.h b/include/stdlib.h index 1c747e4b..46cbd286 100644 --- a/include/stdlib.h +++ b/include/stdlib.h @@ -40,8 +40,10 @@ void * alloca (unsigned size); void * alloca (size_t size); #endif int atoi (char const *s); +int atexit (void (*function) (void)); void * calloc (size_t nmemb, size_t size); -void exit (int); +void _exit (int status); +void exit (int status); void free (void *ptr); char* getenv (char const* s); int setenv (char const* s, char const* v, int overwrite_p); diff --git a/include/unistd.h b/include/unistd.h index 391b19a4..26632a2b 100644 --- a/include/unistd.h +++ b/include/unistd.h @@ -90,9 +90,6 @@ typedef int pid_t; int access (char const *s, int mode); unsigned int alarm (unsigned int seconds); -#define alarm(x) {eputs ("alarm x="); eputs (itoa (x)); eputs ("\n"); kill (getpid (), 14);} -#define atexit(x) eputs ("atexit\n") - int close (int fd); int execv (char const *file_name, char *const argv[]); int execve (char const *file, char *const argv[], char *const env[]); diff --git a/lib/gcc.c b/lib/gcc.c index 45fa72fd..e74d505e 100644 --- a/lib/gcc.c +++ b/lib/gcc.c @@ -80,3 +80,11 @@ unsetenv (char const *name) p++; } } + +// gcc-3.0 +int +atexit (void (*function) (void)) +{ + __call_at_exit = function; +} + diff --git a/lib/linux-mini-gcc.c b/lib/linux-mini-gcc.c index da05ccf4..3fe3829d 100644 --- a/lib/linux-mini-gcc.c +++ b/lib/linux-mini-gcc.c @@ -22,7 +22,7 @@ #define SYS_write "0x04" void -exit (int code) +_exit (int code) { #if !__TINYC__ asm ( @@ -42,7 +42,17 @@ exit (int code) ); #endif // __TINYC__ // not reached - exit (0); + _exit (0); +} + +void (*__call_at_exit) (void); + +void +exit (int code) +{ + if (__call_at_exit) + (*__call_at_exit) (); + _exit (code); } ssize_t diff --git a/lib/linux-mini-mes.c b/lib/linux-mini-mes.c index 34955216..f2858a1f 100644 --- a/lib/linux-mini-mes.c +++ b/lib/linux-mini-mes.c @@ -19,13 +19,23 @@ */ void -exit () +_exit () { asm ("mov____$i32,%eax SYS_exit"); asm ("mov____0x8(%ebp),%ebx !8"); asm ("int____$0x80"); } +void (*__call_at_exit) (void); + +void +exit (int code) +{ + if (__call_at_exit) + (*__call_at_exit) (); + _exit (code); +} + void write () {