On Tue, 2024-01-09 at 01:48 -0600, Segher Boessenkool wrote: > On Tue, Jan 09, 2024 at 11:05:57AM +0800, Xi Ruoyao via Gcc-help wrote: > > But the Linux kernel cannot use neither libc.so nor libgcc.a. > > I have built Linux using libgcc for many years. It is as easy as > > +LIBGCC := $(shell $(CC) $(KBUILD_CFLAGS) -print-libgcc-file-name) > +libs-y += $(LIBGCC) > > > (I know > > some non-Linux kernel developers are overusing libgcc.a for kernels, but > > IMO this is just wrong and Linux developers also do not do this. If the > > Linux kernel needs a symbol from libgcc the developers just provide > > their own implementation.) > > Yes, and often they have fallen behind. When they eventually catch up > they usually just copy the GCC code anyway. > > Originally the only reasonable argument for not linking against libgcc > was so kernel code would not accidentally use double-length divisions. > There are other simple ways to have all uses of __divti3 and similar > create link errors, so that is not really a good argument. > > libgcc is an essential part of the compiler. For most targets, for most > code, GCC will not generate function calls, there usually are faster (or > smaller) things it can do, but it still is necessary to have libgcc for > more uncommon things. Using a partial copy of it, behind the times, > and maybe even incompatible, is not a great idea. But for many targets the kernel uses the soft-float ABI while the user space uses the hard-float ABI and the linker generally refuses to link them altogether. You may argue that "hey, build all multilibs" but I'd say it's stupid and it does not solve all issues: If GCC is configured with things like --with-arch=something, libgcc can contain (FP/vector) instructions unsafe to use in kernel w/o special handling. Currently this issue is making the process very nasty to build systemd-boot on LoongArch desktop distros where the toolchain configured with vector extensions enabled by default. If libgcc is so vital GCC needs to provide a way to make it work for a set of compiler switches incompatible with any pre-built multilib. For example, installing the source of libgcc into /usr/lib/gcc and provide a tool to build a libgcc.a with the specified options. -- Xi Ruoyao <xry111@xxxxxxxxxxx> School of Aerospace Science and Technology, Xidian University