libgcc/crtstuff.c compiled twice, and forced to -O2

R. Diez rdiez-2006@rd10.de
Sat Jun 20 19:21:52 GMT 2026


>> Proper termination is not very common in embedded software, but I am

> It will become common once you start running testsuites.

No doubt, but then, like I said, how come crtstuff.c is not build with -DCRT_END according to gcc-16.1.0/libgcc/Makefile.in:

# This is a version of crtbegin for -static links.
crtbeginT$(objext): $(srcdir)/crtstuff.c
	$(crt_compile) $(CRTSTUFF_T_CFLAGS) -c $< -DCRT_BEGIN -DCRTSTUFFT_O

Is the test suite not exercising global termination of the -static links?


>> I would say it does not make sense to always force -O2, or does it?
>
> Can't you override it in your MULTILIB_CFLAGS?

I am not using MULTILIB_CFLAGS, but I am using CFLAGS_FOR_TARGET, and you are right: I just looked more closely, and -O2 is not forced, only -g0 is, because it comes afterwards in gcc-16.1.0/libgcc/Makefile.in:

# Options to use when compiling crtbegin/end.
CRTSTUFF_CFLAGS = -O2 $(GCC_CFLAGS) $(INCLUDES) $(MULTILIB_CFLAGS) -g0 \
   $(NO_PIE_CFLAGS) -finhibit-size-directive -fno-inline -fno-exceptions \
   -fno-zero-initialized-in-bss -fno-toplevel-reorder -fno-tree-vectorize \
   -fbuilding-libgcc -fno-stack-protector $(FORCE_EXPLICIT_EH_REGISTRY) \
   $(INHIBIT_LIBC_CFLAGS) $(USE_TM_CLONE_REGISTRY)

I already asked in the mentioned bug report why GCC is forcibly turning off debug information with -g0 for crtstuff.c, but that is a different question.

In any case, your answer does not actually address the question: why specify -O2 here? Why is CRTSTUFF_CFLAGS not just (indirectly) taking CFLAGS_FOR_TARGET or its default value? I wouldn't think that crtstuff.c is so special that it should have a different optimisation than CFLAGS_FOR_TARGET or its default.


>> Does it make sense anyway to specify -O2 für a single file? Will it not
>> be overridden by LTO anyway?
> 
> It's not only for a single file.  Despite it's named "CRTSTUFF_CFLAGS"
> it's actually pulled into ALL_CRT_CFLAGS:
> ALL_CRT_CFLAGS = $(CFLAGS) $(CRTSTUFF_CFLAGS) $(INCLUDES)
> 
> which is used for several files in libgcc.

Yes, and the comment "when compiling crtbegin/end" is also wrong then, but the question remains: why specify -O2 for a bunch of initialisation files? Did someone think the default would be wrong here, and for what reason?

Using special options here, and building crtstuff.c twice for each variant (-DCRT_BEGIN and -DCRT_END), just makes the sources and the build system more complicated. I wonder if the initial reasons, which are unfortunately not documented in any comments, are still valid after so many years.

Regards,
   rdiez


More information about the Gcc-help mailing list