diff options
author | Nicholas <nbnoll@eml.cc> | 2021-11-20 17:07:23 -0800 |
---|---|---|
committer | Nicholas <nbnoll@eml.cc> | 2021-11-20 17:07:23 -0800 |
commit | c9a32c1a43d2bdded07eaa45732c3a6e195a5442 (patch) | |
tree | cb7e9cb8c34cbe6551b801eff4201afcf71dd0fd /sys/rt/boot.c | |
parent | e97c8c469db0aa27985dab2879dc1f14905c7387 (diff) |
Chore: cleaned up the exit code to cleanly interface with libc
We use weak linking to ensure we clean up at exit time correctly. If
libc is linked, then we call our cleanup function by registering an
atexit callback with the library. If libc is not linked, we have a weak
symbol that results in a noop. Similarly, if we call rt·exit while
linked with libc, this immediately calls libc's exit (which will call
our cleanup as we registered it). If we are not linked to libc, exit()
is given as a weak link to a noop function.
Diffstat (limited to 'sys/rt/boot.c')
-rw-r--r-- | sys/rt/boot.c | 4 |
1 files changed, 4 insertions, 0 deletions
diff --git a/sys/rt/boot.c b/sys/rt/boot.c index 320a596..8c6616d 100644 --- a/sys/rt/boot.c +++ b/sys/rt/boot.c @@ -3,7 +3,10 @@ #include <elf.h> /* tell linker to go find */ +int atexit(void (*)(void)); int __libc_start_main(int (*main)(int,char **,char **), int argc, char **argv); + +void rt·clean(void); void rt·guardstack(void); #define NAUX 38 @@ -30,6 +33,7 @@ rt·boot(int (*main)(), int argc, char **argv, void (*init)(), void (*fini)(), v rt·init(env, argv[0]); /* ring libc, anyone home? */ + atexit(rt·clean); __libc_start_main(main, argc, argv); /* no? ok we continue on if there is no libc linked */ |