[PATCH] H8: Add support for preinit/init/fini array

Jan Dubiec <[email protected]> Sun, 12 Jul 2026 21:54:53 +0200
Newsgroups gmane.comp.lib.newlib
Message-ID <[email protected]>
This patch adds support for .preinit_array, .init_array, and .fini_array
for the H8 family when binutils and GCC are configured with "--enable-initfini-array".

The changes to init.c and fini.c ensure that linker-defined symbols
referenced from C code are not affected by target-specific symbol naming
conventions. For example, GCC for H8 prefixes external symbol names with
an underscore, whereas GCC for ARM does not. The asm keyword is used to
associate C identifiers with the corresponding linker-defined symbol names
in a target-independent manner.

The patch has been tested on H8 wher it enables support for the
initialization and finalization arrays and on ARM in order to verify that
the changes do not introduce any regressions.

Signed-off-by: Jan Dubiec <[email protected]>
---
 newlib/libc/misc/fini.c         |  6 ++++--
 newlib/libc/misc/init.c         | 12 ++++++++----
 newlib/libc/sys/h8300hms/crt0.S |  6 ++++++
 3 files changed, 18 insertions(+), 6 deletions(-)

diff --git a/newlib/libc/misc/fini.c b/newlib/libc/misc/fini.c
index 5f2016070..0c3a1e0d5 100644
--- a/newlib/libc/misc/fini.c
+++ b/newlib/libc/misc/fini.c
@@ -14,8 +14,10 @@
 #include <sys/types.h>
 
 #ifdef _HAVE_INITFINI_ARRAY
-extern void (*__fini_array_start []) (void) __attribute__((weak));
-extern void (*__fini_array_end []) (void) __attribute__((weak));
+extern void (*__fini_array_start []) (void) asm("__fini_array_start")
+	__attribute__((weak));
+extern void (*__fini_array_end []) (void) asm("__fini_array_end")
+	__attribute__((weak));
 
 #ifdef _HAVE_INIT_FINI
 extern void _fini (void);
diff --git a/newlib/libc/misc/init.c b/newlib/libc/misc/init.c
index 3aef7ad06..5781535f9 100644
--- a/newlib/libc/misc/init.c
+++ b/newlib/libc/misc/init.c
@@ -16,10 +16,14 @@
 #ifdef _HAVE_INITFINI_ARRAY
 
 /* These magic symbols are provided by the linker.  */
-extern void (*__preinit_array_start []) (void) __attribute__((weak));
-extern void (*__preinit_array_end []) (void) __attribute__((weak));
-extern void (*__init_array_start []) (void) __attribute__((weak));
-extern void (*__init_array_end []) (void) __attribute__((weak));
+extern void (*__preinit_array_start []) (void) asm("__preinit_array_start")
+	__attribute__((weak));
+extern void (*__preinit_array_end []) (void) asm("__preinit_array_end")
+	__attribute__((weak));
+extern void (*__init_array_start []) (void) asm("__init_array_start")
+	__attribute__((weak));
+extern void (*__init_array_end []) (void) asm("__init_array_end")
+	__attribute__((weak));
 
 #ifdef _HAVE_INIT_FINI
 extern void _init (void);
diff --git a/newlib/libc/sys/h8300hms/crt0.S b/newlib/libc/sys/h8300hms/crt0.S
index 757fb0477..020b957d6 100644
--- a/newlib/libc/sys/h8300hms/crt0.S
+++ b/newlib/libc/sys/h8300hms/crt0.S
@@ -1,6 +1,12 @@
 ; H8/300, H8/300H, H8/300S and H8SX start up file.
 
 #include "setarch.h"
+#include "newlib.h"
+
+#ifdef _HAVE_INITFINI_ARRAY
+#define __init	___libc_init_array
+#define __fini	___libc_fini_array
+#endif
 
 #ifdef __H8300__
 
-- 
2.54.0