[Bug bootstrap/60160] Building with -flto in CFLAGS_FOR_TARGET / CXXFLAGS_FOR_TARGET

rguenth at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Fri Apr 8 12:49:49 GMT 2022


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60160

--- Comment #7 from Richard Biener <rguenth at gcc dot gnu.org> ---
(In reply to R. Diez from comment #6)
> I am experimenting with a GCC 11.2 cross-compiler for bare-metal embedded
> software.
> 
> There is no operating system, so no shared libraries or anything fancy. But
> there is a static libc (Newlib or Picolibc).
> 
> I wanted to build everything with LTO, including libc, libstdc++ and libgcc.
> This is the makefile I am using:
> 
> https://github.com/rdiez/JtagDue/blob/master/Toolchain/Makefile
> 
> Search for "-ffat-lto-objects" in that makefile.
> 
> As soon as I enable the LTO flags, I get linker errors. They are documented
> in the makefile next to the LTO options, and look similar to those reported
> in this bug.
> 
> I tried -fno-builtin with varying degrees of success. I also tried building
> only the application and libc with LTO, but not libstdc++ etc., to no avail.
> 
> LTO only works for the user application. As soon as libc or the other GCC
> libraries are compiled with LTO, it fails.
> 
> Is it unfortunate, because I believe that a full LTO build for a bare-metal
> environment would be rather beneficial.

I agree.

> The patch and information referenced in this bug report look dated. Is there
> a way to make LTO work now, at least for my configuration?

Problems can exist in all parts of the toolchain here, in particular the
linker needs to cope with GCC replacing say a reference to printf with
puts (which it will happily do in some cases when not using -fno-builtin).
That might be one source of unresolved symbols.  Generally GCC doesn't
like it very much seeing definitions of functions it also recognizes
as builtins.  It might be needed to teach GCC to do less agressive
dead function removal during WPA or not fold printf to puts late if it
removed a definition of puts.

Other than that I would suggest to enable emitting LTO bytecode for the
static target libraries on a per library base with new configury, say
--enable-fatlto-static-target-libraries or so and do opt-in for those
where it works.

That said - yes, it would be nice, but expect to run into problems.  When
it works, great, when it doesn't - try to analyze the issue and maybe file
a specifc bugreport with a testcase?


More information about the Gcc-bugs mailing list