Skip to content

Fix atexit/on_exit/_Exit#735

Draft
ZERICO2005 wants to merge 15 commits intomasterfrom
fix_atexit
Draft

Fix atexit/on_exit/_Exit#735
ZERICO2005 wants to merge 15 commits intomasterfrom
fix_atexit

Conversation

@ZERICO2005
Copy link
Copy Markdown
Contributor

@ZERICO2005 ZERICO2005 commented Mar 5, 2026

Fixes #734

  • Fixed return values from on_exit/atexit
  • Fixed the exit-status passed to on_exit functions via returning from main and via exit
  • Re-implemented C99 _Exit
  • Adds HAS_EXIT and HAS_C99__EXIT which will only include exit() and _Exit() in crt0.S if the functions are needed (similar to HAS_ABORT)

There are other issues that this PR does not fix, such as HAS_ABORT being 0 when abort is indirectly linked. Currently this PR just patches it so HAS_ABORT and HAS_EXIT are always set to 1 to prevent linker errors for the user. More here: #736

@ZERICO2005 ZERICO2005 marked this pull request as ready for review March 5, 2026 22:49
@ZERICO2005 ZERICO2005 marked this pull request as draft March 5, 2026 22:49
@ZERICO2005 ZERICO2005 mentioned this pull request Mar 5, 2026
@ZERICO2005 ZERICO2005 marked this pull request as ready for review March 5, 2026 22:54
@ZERICO2005 ZERICO2005 marked this pull request as draft March 5, 2026 23:00
@ZERICO2005
Copy link
Copy Markdown
Contributor Author

ZERICO2005 commented Apr 3, 2026

These corrections are still not enough actually, this test program fails to link

#include <ti/screen.h>
#include <ti/getcsc.h>
#include <ti/sprintf.h>

#include <errno.h>
#include <exception>

extern "C" int main(void) {
    errno = 0;
    os_ClrHome();

    char buf[sizeof("errno: -8388608\n")];
    boot_sprintf(buf, "errno: %d\n", errno);
    os_PutStrFull(buf);

    while (!os_GetCSC());

    std::terminate();
}
zerico@DESKTOP-7OMIH9B:~/programming/toolchain$ make -C test/crt/terminate
make: Entering directory '/home/zerico/programming/toolchain/test/crt/terminate'
[cc] src/main.cpp
[lto] obj/lto.bc
/home/zerico/programming/toolchain/CEdev/binutils/bin/z80-none-elf-ld: warning: indcallhl.o: missing .note.GNU-stack section implies executable stack
/home/zerico/programming/toolchain/CEdev/binutils/bin/z80-none-elf-ld: NOTE: This behaviour is deprecated and will be removed in a future version of the linker
[linking] bin/DEMO.obj
/home/zerico/programming/toolchain/CEdev/binutils/bin/z80-none-elf-ld: warning: obj/DEMO.o: requires executable stack (because the .note.GNU-stack section is executable)
/home/zerico/programming/toolchain/CEdev/binutils/bin/z80-none-elf-ld: obj/DEMO.o: in function `___abort_message':
(.text.___abort_message+0x13): undefined reference to `_sprintf'
make: *** [/home/zerico/programming/toolchain/CEdev/meta/makefile.mk:377: bin/DEMO.obj] Error 1
make: Leaving directory '/home/zerico/programming/toolchain/test/crt/terminate'
zerico@DESKTOP-7OMIH9B:~/programming/toolchain$ 

@ZERICO2005
Copy link
Copy Markdown
Contributor Author

not sure why those tests failed. It compiles fine on clang 19

zerico@DESKTOP-7OMIH9B:~/programming/toolchain$ make -C test/standalone/custom_lib clean
make: Entering directory '/home/zerico/programming/toolchain/test/standalone/custom_lib'
Removed built binaries and objects.
make: Leaving directory '/home/zerico/programming/toolchain/test/standalone/custom_lib'
zerico@DESKTOP-7OMIH9B:~/programming/toolchain$ make -C test/standalone/custom_lib
make: Entering directory '/home/zerico/programming/toolchain/test/standalone/custom_lib'
[cc] src/custom_malloc.c
[cc] src/custom_sprintf.c
[cc] src/main.c
[lto] obj/lto.bc
[as] src/custom_free.s
[convimg] description
/home/zerico/programming/toolchain/CEdev/binutils/bin/z80-none-elf-ld: warning: imulu.o: missing .note.GNU-stack section implies executable stack
/home/zerico/programming/toolchain/CEdev/binutils/bin/z80-none-elf-ld: NOTE: This behaviour is deprecated and will be removed in a future version of the linker
[linking] bin/DEMO.obj
/home/zerico/programming/toolchain/CEdev/binutils/bin/z80-none-elf-ld: warning: obj/DEMO.o: requires executable stack (because the .note.GNU-stack section is executable)
[success] bin/DEMO.8xp, 1060 bytes.
make: Leaving directory '/home/zerico/programming/toolchain/test/standalone/custom_lib'
zerico@DESKTOP-7OMIH9B:~/programming/toolchain$ 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

incorrect return value for atexit/on_exit

1 participant