From: Simon Schuster <schuster.simon@siemens-energy.com>
To: Arnd Bergmann <arnd@arndb.de>
Cc: "Dinh Nguyen" <dinguyen@kernel.org>,
"Thomas Weißschuh" <thomas.weissschuh@linutronix.de>,
linux-kernel@vger.kernel.org,
"Chung-Lin Tang" <cltang@baylibre.com>
Subject: Re: [PATCH 1/2] nios2: remove libgcc for lshrdi3, ashldi3, ashrdi3, muldi3, and ucmpdi2
Date: Tue, 20 Oct 2026 14:31:49 +0200 [thread overview]
Message-ID: <20261020123149.cyjzz4uo25oiiu2m@dev-vm-schuster> (raw)
In-Reply-To: <686f4ca2-0fb6-4a73-9cdb-0edcccafdd62@app.fastmail.com>
On Tue, Oct 20, 2026 at 11:41:59AM +0200, Arnd Bergmann wrote:
> The underlying problem is that the kernel is its own special environment
> that is neither a standard C library nor a 'freestanding' implementation
> and only vaguely similar to a mix of the two. We need certain parts
> of libgcc but for convenience we want to force a link failure if the
> compiler ever generates calls into functions we explicitly avoid
> in the kernel like 64-bit division and floating point.
Hmm, makes sense. The question is to what degree we assume those
particular implementations in libgcc to not do bad things... But if we
do, what about something like that:
$ nios2-buildroot-linux-gnu-objcopy \
--keep-global-symbol '__divsi3' \
--keep-global-symbol '__modsi3' \
--keep-global-symbol '__mulsi3' \
--keep-global-symbol '__udivsi3' \
--keep-global-symbol '__umodsi3' \
--keep-global-symbol '__divsi3_table' \
"$(nios2-buildroot-linux-gnu-gcc -print-libgcc-file-name)" /tmp/libgcc.a
$ nios2-buildroot-linux-gnu-nm /tmp/libgcc.a | grep '\sT\|D\s'
00000000 T __divsi3
00000088 T __modsi3
000000fc T __udivsi3
00000160 T __umodsi3
00000000 D __divsi3_table
00000000 T __mulsi3
Wouldn't that fulfil the requirement as well? Only "vetted"/trustworthy
symbols get exported/linked, while it is easy to add additional symbols
if the need arises? (If we gracefully ignore the downsides, such as some
degree of brittleness such as implicitly assuming that all
dependent symbols reside in the same file as they will be switched
to local, and the fact that this is a hack, and that we tie us more
closely to gcc on architectures using said "hack").
> > On a more practical note, while technically feasible, I'm not sure
> > whether we can actually vendor/copy directly from libgcc; I think all
> > code there is GPLv3 with Runtime Exception. (I'm not a lawyer yadayada),
> > but I guess direct reuse in that way is problematic, given that the
> > kernel is GPLv2 only? So I guess we would need to do a reimplementation?
>
> Right, the easiest way would be if you can locate a downstream gcc
> (or clang) fork from Altera or Codesourcery before it was merged,
> if that still has GPLv2 or a more liberal license. Reimplementing
> the functions otherwise is of course annoying.
>
> Adding Chung-Lin Tang to Cc, they contributed the original files
> to gcc back in 2013 and may know a copyright holder that can
> provide a GPLv2 compatible copy.
>
> I also found a similar implemention for m32r that was still
> GPLv2, maybe that one works, see https://github.com/gcc-mirror/gcc/commit/38b2d07632#diff-4de4f3b573100be00cf40045c85faaca8ded1f13ef80df1822889cbd6ad639a8
Nice find, m32c-lib2.c seems identical to the modern nios2 lib2-divmod.c
(save whitespace changes). This already covers:
__divsi3
__modsi3
__udivsi3
__umodsi3
Regarding __divsi3_table:
This basically seems to be an array of 16 x 16 (even though its
linearized into memory) that caches the result of row_index / column_idx
(with divisions by 0 being assigned to 0), so "reimplementation" should
be trivial.
Regarding __mulsi3:
The iq2000 target seems to have a GPLv2 implementation; slight
difference in declared datatypes (iq2000 uses unsigned, nios2 uses
signed, I'd need to think a bit more about this), but should generally
be viable.
https://github.com/gcc-mirror/gcc/blob/releases/gcc-4.2/gcc/config/iq2000/lib2extra-funcs.c
So we should probably be able to assemble a libgcc-free nios2 kernel
runtime without the risk of introducing new bugs in potential
reimplementations if desired.
Best regards
Simon
next prev parent reply other threads:[~2026-10-20 12:31 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-19 16:50 Dinh Nguyen
2026-10-19 16:50 ` [PATCH 2/2] nios2: remove 64-bit division Dinh Nguyen
2026-10-19 21:14 ` Arnd Bergmann
2026-10-19 21:20 ` [PATCH 1/2] nios2: remove libgcc for lshrdi3, ashldi3, ashrdi3, muldi3, and ucmpdi2 Arnd Bergmann
2026-10-20 8:56 ` Simon Schuster
2026-10-20 9:41 ` Arnd Bergmann
2026-10-20 12:31 ` Simon Schuster [this message]
2026-10-20 12:55 ` Arnd Bergmann
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261020123149.cyjzz4uo25oiiu2m@dev-vm-schuster \
--to=schuster.simon@siemens-energy.com \
--cc=arnd@arndb.de \
--cc=cltang@baylibre.com \
--cc=dinguyen@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=thomas.weissschuh@linutronix.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®