We've started doing builds with -O0 in Buildroot, and we get build failures in glibc that look like this: /home/autobuild/autobuild/instance-11/output-1/host/lib/gcc/sh4eb-buildroot-linux-gnu/11.3.0/../../../../sh4eb-buildroot-linux-gnu/bin/ld: /home/autobuild/autobuild/instance-11/output-1/build/glibc-2.36-66-ga1dc0be03c9dd850b864bd7a9c03cf8e396eb7ca/build/elf/librtld.os: in function `__register_frame': /home/autobuild/autobuild/instance-11/output-1/build/host-gcc-initial-11.3.0/build/sh4eb-buildroot-linux-gnu/libgcc/../../../libgcc/unwind-dw2-fde.c:138: undefined reference to `malloc' /home/autobuild/autobuild/instance-11/output-1/host/lib/gcc/sh4eb-buildroot-linux-gnu/11.3.0/../../../../sh4eb-buildroot-linux-gnu/bin/ld: /home/autobuild/autobuild/instance-11/output-1/build/glibc-2.36-66-ga1dc0be03c9dd850b864bd7a9c03cf8e396eb7ca/build/elf/librtld.os: in function `__register_frame_table': /home/autobuild/autobuild/instance-11/output-1/build/host-gcc-initial-11.3.0/build/sh4eb-buildroot-linux-gnu/libgcc/../../../libgcc/unwind-dw2-fde.c:186: undefined reference to `malloc' /home/autobuild/autobuild/instance-11/output-1/host/lib/gcc/sh4eb-buildroot-linux-gnu/11.3.0/../../../../sh4eb-buildroot-linux-gnu/bin/ld: /home/autobuild/autobuild/instance-11/output-1/build/glibc-2.36-66-ga1dc0be03c9dd850b864bd7a9c03cf8e396eb7ca/build/elf/librtld.os: in function `__deregister_frame_info_bases': /home/autobuild/autobuild/instance-11/output-1/build/host-gcc-initial-11.3.0/build/sh4eb-buildroot-linux-gnu/libgcc/../../../libgcc/unwind-dw2-fde.c:246: undefined reference to `free' /home/autobuild/autobuild/instance-11/output-1/host/lib/gcc/sh4eb-buildroot-linux-gnu/11.3.0/../../../../sh4eb-buildroot-linux-gnu/bin/ld: /home/autobuild/autobuild/instance-11/output-1/build/glibc-2.36-66-ga1dc0be03c9dd850b864bd7a9c03cf8e396eb7ca/build/elf/librtld.os: in function `__deregister_frame': /home/autobuild/autobuild/instance-11/output-1/build/host-gcc-initial-11.3.0/build/sh4eb-buildroot-linux-gnu/libgcc/../../../libgcc/unwind-dw2-fde.c:260: undefined reference to `free' /home/autobuild/autobuild/instance-11/output-1/host/lib/gcc/sh4eb-buildroot-linux-gnu/11.3.0/../../../../sh4eb-buildroot-linux-gnu/bin/ld: /home/autobuild/autobuild/instance-11/output-1/build/glibc-2.36-66-ga1dc0be03c9dd850b864bd7a9c03cf8e396eb7ca/build/elf/librtld.os: in function `start_fde_sort': /home/autobuild/autobuild/instance-11/output-1/build/host-gcc-initial-11.3.0/build/sh4eb-buildroot-linux-gnu/libgcc/../../../libgcc/unwind-dw2-fde.c:449: undefined reference to `malloc' /home/autobuild/autobuild/instance-11/output-1/host/lib/gcc/sh4eb-buildroot-linux-gnu/11.3.0/../../../../sh4eb-buildroot-linux-gnu/bin/ld: /home/autobuild/autobuild/instance-11/output-1/build/host-gcc-initial-11.3.0/build/sh4eb-buildroot-linux-gnu/libgcc/../../../libgcc/unwind-dw2-fde.c:449: undefined reference to `malloc' /home/autobuild/autobuild/instance-11/output-1/host/lib/gcc/sh4eb-buildroot-linux-gnu/11.3.0/../../../../sh4eb-buildroot-linux-gnu/bin/ld: /home/autobuild/autobuild/instance-11/output-1/build/glibc-2.36-66-ga1dc0be03c9dd850b864bd7a9c03cf8e396eb7ca/build/elf/librtld.os: in function `end_fde_sort': /home/autobuild/autobuild/instance-11/output-1/build/host-gcc-initial-11.3.0/build/sh4eb-buildroot-linux-gnu/libgcc/../../../libgcc/unwind-dw2-fde.c:629: undefined reference to `free' /home/autobuild/autobuild/instance-11/output-1/host/lib/gcc/sh4eb-buildroot-linux-gnu/11.3.0/../../../../sh4eb-buildroot-linux-gnu/bin/ld: /home/autobuild/autobuild/instance-11/output-1/build/glibc-2.36-66-ga1dc0be03c9dd850b864bd7a9c03cf8e396eb7ca/build/elf/librtld.os: in function `abort': (.text+0x2345c): undefined reference to `__pthread_mutex_lock' /home/autobuild/autobuild/instance-11/output-1/host/lib/gcc/sh4eb-buildroot-linux-gnu/11.3.0/../../../../sh4eb-buildroot-linux-gnu/bin/ld: (.text+0x23464): undefined reference to `__pthread_mutex_unlock' /home/autobuild/autobuild/instance-11/output-1/host/lib/gcc/sh4eb-buildroot-linux-gnu/11.3.0/../../../../sh4eb-buildroot-linux-gnu/bin/ld: (.text+0x2346c): undefined reference to `__pthread_mutex_lock' collect2: error: ld returned 1 exit status make[3]: *** [Makefile:1339: /home/autobuild/autobuild/instance-11/output-1/build/glibc-2.36-66-ga1dc0be03c9dd850b864bd7a9c03cf8e396eb7ca/build/elf/ld.so] Error 1 See http://autobuild.buildroot.net/results/7df/7df2d0648dd666dd07845614b34f59d6afba96c1/build-end.log for a more complete build log. The reason is that a number of functions in libgcc/unwind-dw2-fde.c call into C library functions. With an optimization level of -O1 at least, these functions get compiled out, and there is no problem. But at -O0, the code of those functions is kept, causing the above build failure. To reproduce: $ git clone https://gitlab.com/buildroot.org/buildroot.git $ cd buildroot $ cat > .config <<EOF BR2_sh=y BR2_sh4eb=y BR2_KERNEL_HEADERS_4_14=y BR2_GCC_VERSION_12_X=y BR2_TOOLCHAIN_BUILDROOT_CXX=y # BR2_STRIP_strip is not set BR2_OPTIMIZE_0=y EOF $ make olddefconfig $ make (and wait)
This is not a GCC issue as __register_frame_table is declared as extern. This is almost definitely a glibc issue really. But I doubt glibc can be compiled at -O0 because of these issues.
Move over, the functions are not optimized out at -O2 but rather they don't get pulled in glibc building because another function is referenced. Either libgcc should be built with -fdata-sections -ffunction-sections or glibc builds needs to be fixed such that it does not reference the function in the unwinder at -O0. You need to look at the linker map to figure why the object file is being pulled into glibc at -O0 and not at -O2. But it is still a bug in glibc's ld.so really.
Thanks for the super quick feedback. Could you clarify "Move over, the functions are not optimized out at -O2 but rather they don't get pulled in glibc building because another function is referenced." ? For the record, we are able to work around this issue by passing -O1 in CFLAGS_FOR_TARGET/CXXFLAGS_FOR_TARGET when building the first stage gcc.
This bug was already reported before at glibc: https://sourceware.org/bugzilla/show_bug.cgi?id=29621 There it was concluded that it is due to gcc stage1 being built with CFLAGS_FOR_TARGET="-O0". This is not something that is tested in glibc's testing infrastructure. It's not clear to me whether the issue needs to be solved in GCC or in glibc.
Based on > glibc builds needs to be fixed such that it does not reference the function in the unwinder at -O0 I've traced through the map file why this symbol is pulled in: /.../libgcc.a(unwind-dw2.o) /.../libgcc.a(_umoddi3.o) (_Unwind_Resume) /.../libgcc.a(unwind-dw2-fde-dip.o) /.../libgcc.a(unwind-dw2.o) (_Unwind_Find_FDE) In other words, _umoddi3.o is the one that pulls in the unwinder.
There still might not much to be done really. But let's see ...