| Message ID | 20260712195656.744504-1-jdx@o2.pl |
|---|---|
| State | New |
| Headers |
Return-Path: <newlib-bounces~patchwork=sourceware.org@sourceware.org> X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id EB2804BA2E0C for <patchwork@sourceware.org>; Sun, 12 Jul 2026 20:09:16 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org EB2804BA2E0C Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=o2.pl header.i=@o2.pl header.a=rsa-sha256 header.s=20241105 header.b=q09dpkJA X-Original-To: newlib@sourceware.org Delivered-To: newlib@sourceware.org Received: from mx-out.tlen.pl (mx-out.tlen.pl [193.222.135.142]) by sourceware.org (Postfix) with ESMTPS id 923044BA2E05 for <newlib@sourceware.org>; Sun, 12 Jul 2026 20:08:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 923044BA2E05 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=o2.pl Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=o2.pl ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 923044BA2E05 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=193.222.135.142 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1783886923; cv=none; b=VgKqdA6Vqm8DDWR6KiZjq9eaWj1oMB9EvoU7zX4inXtxLCAXviB39bPbupEMLYhiqVF/QRfqoYP843OpVix3F2XrJ6JgbwAGpoCaI7mmqkfSFvUfnw0HfLPObMzuUUPgeujvjVvF8iLCjZa6V+9U3fpiCfuSSuwDfIVF/OLRtvg= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1783886923; c=relaxed/simple; bh=Glt763DUHO78zof2+54HB129WyddvLpniCtW5RKu838=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=ZXv+ceKsEFVpKjLsjiUqHzcP1/9+JEz6iggYiNeuQxU7Vn+4Eb6ZrBff/z8wLQfBQTamaa0CzmBat4s9HO+BtlJjINDLi8hh8ws2cCsnM3cYRTpqVbUGb1LkaLk1xvrVMcbUUDXovlg99Em0Nm2J56fxzpJ37qZiXx8WBzpqOhk= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=o2.pl header.i=@o2.pl header.a=rsa-sha256 header.s=20241105 header.b=q09dpkJA DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 923044BA2E05 Received: (wp-smtpd smtp.tlen.pl 38436 invoked from network); 12 Jul 2026 22:02:01 +0200 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=o2.pl; s=20241105; t=1783886521; bh=rrUTWs2UcmtznytjkBREn1NtYSpmxoIZk0NOz2iUXck=; h=From:To:Cc:Subject; b=q09dpkJAeExAJR3ET3dGxngcTjIRIjoIXz1ZFDNiC0+7pUgk/sin316GpFX0PBPWr 3f6j1bJjP7gkE84AoA86fZ+Y1k8gqlkmspfGIcZUTh766QUu5kJNch2M9gAlLNIuUs cgj3fzGKMKNltnbCyudyHaYcXiTrTtVUKTE29DeyeugafOuvkoUZTshW9r/rXrElb4 bHUM118XWUDFIYWonpV+VbmWvnNQtZeDwop2MzeaBC3dt8eA0C/ICToShWqQpppp05 U2SnYh8aXSIKBFklgxJCCWZ8YeQMkxTM+8RRnBlWGSW9arVdB9A/0VfYnXhWNBgNab Gqi6LI+zHMMAg== Received: from ip-94-42-225-247.multimo.pl (HELO jdxpc) (jdx@o2.pl@[94.42.225.247]) (envelope-sender <jdx@o2.pl>) by smtp.tlen.pl (WP-SMTPD) with TLS_AES_256_GCM_SHA384 encrypted SMTP for <newlib@sourceware.org>; 12 Jul 2026 22:02:01 +0200 From: Jan Dubiec <jdx@o2.pl> To: newlib@sourceware.org Cc: Jan Dubiec <jdx@o2.pl> Subject: [PATCH] H8: Add support for preinit/init/fini array Date: Sun, 12 Jul 2026 21:54:53 +0200 Message-ID: <20260712195656.744504-1-jdx@o2.pl> X-Mailer: git-send-email 2.54.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-WP-DKIM-Status: good (id: o2.pl) X-WP-MailID: e5ac26e2c8065c8b0aabaf7404becd9a X-WP-AV: skaner antywirusowy Poczty o2 X-WP-SPAM: NO 0000000 [sVMj] X-Spam-Status: No, score=-11.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, RCVD_IN_PBL, SPF_HELO_NONE, SPF_PASS, TXREP shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on sourceware.org X-BeenThere: newlib@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Newlib mailing list <newlib.sourceware.org> List-Unsubscribe: <https://sourceware.org/mailman/options/newlib>, <mailto:newlib-request@sourceware.org?subject=unsubscribe> List-Archive: <https://sourceware.org/pipermail/newlib/> List-Post: <mailto:newlib@sourceware.org> List-Help: <mailto:newlib-request@sourceware.org?subject=help> List-Subscribe: <https://sourceware.org/mailman/listinfo/newlib>, <mailto:newlib-request@sourceware.org?subject=subscribe> Errors-To: newlib-bounces~patchwork=sourceware.org@sourceware.org |
| Series |
H8: Add support for preinit/init/fini array
|
|
Commit Message
Jan Dubiec
July 12, 2026, 7:54 p.m. UTC
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 <jdx@o2.pl>
---
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(-)
Comments
Patch merged. -- Jeff J. On Sun, Jul 12, 2026 at 4:09 PM Jan Dubiec <jdx@o2.pl> wrote: > 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 <jdx@o2.pl> > --- > 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 > >
> From: Jan Dubiec <jdx@o2.pl> > Date: Sun, 12 Jul 2026 21:54:53 +0200 > 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". To accomplish that, there should have been a fix to binutils, per below. Instead, this patch broke initfini support for targets with a USER_LABEL_PREFIX, like cris-elf. I was alerted to this commit when people tried to use newlib master with cris-elf in https://gcc.gnu.org/bugzilla/show_bug.cgi?id=126518 > 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. Since ARM isn't a USER_LABEL_PREFIX-target, your patch was neutral there. While testing the patch there too makes sense, that target wasn't in the cross-hairs; it didn't change the symbols. The parts of this patch to the generic files, should be reverted. Instead I suggest to correct binutils for h8300 to emit the "_" as a prefix to those linker-generated initfini-symbols in the default linker scripts, something like: diff --git a/ld/emulparams/h8300elf.sh b/ld/emulparams/h8300elf.sh index 45b3458d1cea..dd007fcd0ffc 100644 --- a/ld/emulparams/h8300elf.sh +++ b/ld/emulparams/h8300elf.sh @@ -9,6 +9,7 @@ MAXPAGESIZE=2 TARGET_PAGE_SIZE=128 ARCH=h8300 TEMPLATE_NAME=elf +USER_LABEL_PREFIX=_ EMBEDDED=yes STACK_ADDR=0xfefc TINY_READONLY_SECTION=".tinyrodata : (cf. emulparams/criself.sh and ld/scripttempl/elf.sc) That might of course have fallout for other symbols defined in those scripts, but would align with other targets (no special defines or asm-renames needed). brgds, H-P
The changes to fini.c and init.c have been reverted. -- Jeff J. On Tue, Aug 11, 2026 at 10:54 PM Hans-Peter Nilsson <hp@axis.com> wrote: > > From: Jan Dubiec <jdx@o2.pl> > > Date: Sun, 12 Jul 2026 21:54:53 +0200 > > > 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". > > To accomplish that, there should have been a fix to binutils, per > below. > > Instead, this patch broke initfini support for targets with a > USER_LABEL_PREFIX, like cris-elf. I was alerted to this commit when > people tried to use newlib master with cris-elf in > https://gcc.gnu.org/bugzilla/show_bug.cgi?id=126518 > > > 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. > > Since ARM isn't a USER_LABEL_PREFIX-target, your patch was neutral > there. While testing the patch there too makes sense, that target > wasn't in the cross-hairs; it didn't change the symbols. > > The parts of this patch to the generic files, should be reverted. > Instead I suggest to correct binutils for h8300 to emit the "_" as a > prefix to those linker-generated initfini-symbols in the default > linker scripts, something like: > > diff --git a/ld/emulparams/h8300elf.sh b/ld/emulparams/h8300elf.sh > index 45b3458d1cea..dd007fcd0ffc 100644 > --- a/ld/emulparams/h8300elf.sh > +++ b/ld/emulparams/h8300elf.sh > @@ -9,6 +9,7 @@ MAXPAGESIZE=2 > TARGET_PAGE_SIZE=128 > ARCH=h8300 > TEMPLATE_NAME=elf > +USER_LABEL_PREFIX=_ > EMBEDDED=yes > STACK_ADDR=0xfefc > TINY_READONLY_SECTION=".tinyrodata : > > (cf. emulparams/criself.sh and ld/scripttempl/elf.sc) > > That might of course have fallout for other symbols defined in those > scripts, but would align with other targets (no special defines or > asm-renames needed). > > brgds, H-P > >
On 12.08.2026 04:53, Hans-Peter Nilsson wrote: [...] > The parts of this patch to the generic files, should be reverted. > Instead I suggest to correct binutils for h8300 to emit the "_" as a > prefix to those linker-generated initfini-symbols in the default > linker scripts, something like: Thanks for pointing this out. It led to the discovery of an inconsistency in the H8 toolchain. I had actually considered USER_LABEL_PREFIX earlier, but was misled by gcc, which defines this macro: https://gcc.gnu.org/git/?p=gcc.git;a=blob;f=gcc/config/h8300/elf.h;hb=HEAD I have already posted a patch for binutils: https://sourceware.org/pipermail/binutils/2026-August/150796.html /J.D.
On 12.08.2026 04:53, Hans-Peter Nilsson wrote: [...] > The parts of this patch to the generic files, should be reverted. > Instead I suggest to correct binutils for h8300 to emit the "_" as a > prefix to those linker-generated initfini-symbols in the default > linker scripts, something like: Thanks for pointing this out. It led to the discovery of an inconsistency in the H8 toolchain. I had actually considered USER_LABEL_PREFIX earlier, but was misled by gcc, which defines this macro: https://gcc.gnu.org/git/?p=gcc.git;a=blob;f=gcc/config/h8300/elf.h;hb=HEAD I have already posted a patch for binutils: https://sourceware.org/pipermail/binutils/2026-August/150796.html /J.D.
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__