From d2083d510f00afeb59537a4e2f916c39fa57d88c Mon Sep 17 00:00:00 2001 From: Stas Sergeev Date: Mon, 29 Jun 2026 12:09:30 +0300 Subject: [PATCH 1/6] crt0: disable ___register_frame_info [#73] It does nothing and causes undefined symbols w/o -whole-archive --- src/libc/crt0/crt0.S | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/libc/crt0/crt0.S b/src/libc/crt0/crt0.S index 7e47f21f..6720d1aa 100644 --- a/src/libc/crt0/crt0.S +++ b/src/libc/crt0/crt0.S @@ -325,7 +325,9 @@ use_stubinfo_stack_size: xorl %ebp, %ebp pushl $_frame_struct pushl $_CRT0_EH_FRAME_BEGIN_ +#ifndef DJ64 call ___register_frame_info +#endif addl $8, %esp #if USE64 pushl __plt_handle From 9b827c184bd6336617bf4345b29851df66152f67 Mon Sep 17 00:00:00 2001 From: Stas Sergeev Date: Mon, 29 Jun 2026 12:15:56 +0300 Subject: [PATCH 2/6] dj32.pc: get rid of -whole-archive [#73] With the use of --require-defined we can manually pull in needed symbols. --- dj32.pc.in | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dj32.pc.in b/dj32.pc.in index e0c1ae5d..60222da4 100644 --- a/dj32.pc.in +++ b/dj32.pc.in @@ -9,4 +9,4 @@ makeinc=${dj32prefix}/share/dj32.mk cppflags=-I${dj32prefix}/include -DDJ64 -DDJ32 -nostdinc xcppflags=${cppflags} -imacros ${dj32prefix}/include/dj64/a_mac.h Cflags: -fno-pic -fno-pie -ffreestanding -fno-stack-protector -march=i386 -fcf-protection=none -Libs: -L${libdir32} -Wl,-melf_i386 -static -nostdlib -Wl,-whole-archive ${libdir}/libc32_s.a -Wl,-no-whole-archive -ldj32_s -lgcc +Libs: -L${libdir32} -L${libdir} -Wl,-melf_i386 -static -nostdlib -Wl,--require-defined=____crt1_startup -ldj32_s -lc32_s -lgcc From debd3d3d0c527383267708bdab16e9eb836d2049 Mon Sep 17 00:00:00 2001 From: Stas Sergeev Date: Mon, 29 Jun 2026 11:42:48 +0300 Subject: [PATCH 3/6] Revert "fix conio_init" This reverts commit f5d6cf94510396986d385baf7df14fe859bbe12e. As identified by @jwt27 its the source of symbol clash with ncurses. See #73 --- src/libc/crt0/crt1.c | 2 -- src/libc/pc_hw/co80/conio.c | 2 ++ 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libc/crt0/crt1.c b/src/libc/crt0/crt1.c index 48fbd74b..df00198b 100644 --- a/src/libc/crt0/crt1.c +++ b/src/libc/crt0/crt1.c @@ -19,7 +19,6 @@ #include #include #include -#include #include #include #include @@ -234,7 +233,6 @@ __crt1_startup(main_t *main) __crt0_setup_arguments(); _npxsetup(__crt0_argv ? __crt0_argv[0] : __dos_argv0); _crt0_init_mcount(); - gppconio_init(); errno = 0; /* ANSI says errno should be zero at program startup */ exit(main(__crt0_argc, __crt0_argv, _environ)); } diff --git a/src/libc/pc_hw/co80/conio.c b/src/libc/pc_hw/co80/conio.c index 047ca2cf..9d33291d 100644 --- a/src/libc/pc_hw/co80/conio.c +++ b/src/libc/pc_hw/co80/conio.c @@ -1093,3 +1093,5 @@ gppconio_init(void) ScreenAttrib = txinfo.normattr = txinfo.attribute = oldattrib; #endif } + +__asm__(".section .ctor; .long _gppconio_init; .section .text"); From c3838e206b1ac5c9eaa7505d8f5a46fdbb2a8fc9 Mon Sep 17 00:00:00 2001 From: Stas Sergeev Date: Mon, 29 Jun 2026 13:05:54 +0300 Subject: [PATCH 4/6] Revert "remove _main.c" This reverts commit bf18cfe3427ccdb346df3014dc732145687fba05. See #73 --- include/libc/internal.h | 1 + src/libc/crt0/_main.c | 17 +++++++++++++++++ src/libc/crt0/crt1.c | 2 ++ src/libc/crt0/makefile | 1 + 4 files changed, 21 insertions(+) create mode 100644 src/libc/crt0/_main.c diff --git a/include/libc/internal.h b/include/libc/internal.h index 70b06ce7..92069399 100644 --- a/include/libc/internal.h +++ b/include/libc/internal.h @@ -24,6 +24,7 @@ typedef int (main_t)(int, char **, char **); void __crt1_startup(main_t *main); void ___crt1_startup(void); int dj32_init(void); +void __main(void); void _npxsetup(char *argv0); void __emu387_exception_handler(void); void __djgpp_exception_processor(void); diff --git a/src/libc/crt0/_main.c b/src/libc/crt0/_main.c new file mode 100644 index 00000000..bdca4c5c --- /dev/null +++ b/src/libc/crt0/_main.c @@ -0,0 +1,17 @@ +/* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */ +#include +#include + +void +__main(void) +{ +#if 0 + static int been_there_done_that = -1; + int i; + if (been_there_done_that == __bss_count) + return; + been_there_done_that = __bss_count; + for (i=0; i Date: Mon, 29 Jun 2026 12:21:24 +0300 Subject: [PATCH 5/6] implement constructors [fixes #73] As identified by @jwt27 we can't call gppconio_init() directly, as that pulls in conio and clashes with ncurses. So I had to implement constructors. For dj32 just revive the appropriate code in _main.c. For dj64 use custom constructors because the automatic ones are called too early, before the runtime is initialized. --- include/libc/djthunks.h | 2 ++ src/libc/crt0/_main.c | 14 +++++++++++--- src/libc/crt0/crt1.c | 1 - src/libc/dj64/thunks.c | 25 +++++++++++++++++++++++++ src/libc/pc_hw/co80/conio.c | 6 +++++- 5 files changed, 43 insertions(+), 5 deletions(-) diff --git a/include/libc/djthunks.h b/include/libc/djthunks.h index 1f1d75f1..efcdc842 100644 --- a/include/libc/djthunks.h +++ b/include/libc/djthunks.h @@ -31,5 +31,7 @@ uint32_t djthunk_get_h(int handle, const char *name); void *djsbrk(int increment); int djelf_load(int num, int libid); int djelf_exec(void); +void djregister_init_hook(void (*hook)(void)); +void djprocess_init_hooks(void); #endif diff --git a/src/libc/crt0/_main.c b/src/libc/crt0/_main.c index bdca4c5c..7287d764 100644 --- a/src/libc/crt0/_main.c +++ b/src/libc/crt0/_main.c @@ -2,16 +2,24 @@ #include #include +#if !USE64 +typedef void (*FUNC)(void); +extern FUNC __init_array_start[] __attribute__((weak)); +extern FUNC __init_array_end[] __attribute__((weak)); +#endif + void __main(void) { -#if 0 +#if !USE64 static int been_there_done_that = -1; int i; + int count = __init_array_end - __init_array_start; if (been_there_done_that == __bss_count) return; been_there_done_that = __bss_count; - for (i=0; iget_handle()]; return u->eops->exec(); } + +void djregister_init_hook(void (*hook)(void)) +{ + struct init_hooks *c; + + assert(num_ihooks < MAX_INIT_HOOKS); + c = &ihooks[num_ihooks++]; + c->init = hook; +} + +void djprocess_init_hooks(void) +{ + int i; + + for (i = 0; i < num_ihooks; i++) + ihooks[i].init(); +} diff --git a/src/libc/pc_hw/co80/conio.c b/src/libc/pc_hw/co80/conio.c index 9d33291d..11a990e5 100644 --- a/src/libc/pc_hw/co80/conio.c +++ b/src/libc/pc_hw/co80/conio.c @@ -1094,4 +1094,8 @@ gppconio_init(void) #endif } -__asm__(".section .ctor; .long _gppconio_init; .section .text"); +__attribute__((constructor)) +static void _gppconio_init(void) +{ + djregister_init_hook(gppconio_init); +} From 5c5d93d7c2337938ce3677c76709cd16e9188e31 Mon Sep 17 00:00:00 2001 From: Stas Sergeev Date: Mon, 29 Jun 2026 14:07:29 +0300 Subject: [PATCH 6/6] stub: fix another regression of 30c9605c45 --- src/djdev64/stub/stub.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/djdev64/stub/stub.c b/src/djdev64/stub/stub.c index 11129c2c..be181725 100644 --- a/src/djdev64/stub/stub.c +++ b/src/djdev64/stub/stub.c @@ -372,6 +372,8 @@ int djstub_main(int argc, char *argv[], char *envp[], noffset = offs; moff = 4; done = 1; + assert(dyn); + OPEN_DYN(); } else { pl32++; coffset = offs;