[PATCH v2] lto, mingw: Do not export symbols from some frontends

Zhongteng Gui dragon-archer@outlook.com
Sat Oct 10 04:56:32 GMT 2026


On 2026/08/11 20:09, Zhongteng Gui wrote:
> This patch workarounds bootstrapping on x86_64-w64-mingw32 targets with
> bootstrap-lto-lean and --enable-plugin enabled. Note that
> i686-w64-mingw32 targets still fails with bootstrap-lto-lean even
> without --enable-plugin due to segfault in lto1.exe.
> 
> --enable-plugin adds -Wl,--export-all-symbols and -Wl,--out-implib=$@.a
> to PLUGINLIBS on mingw hosts, so that a plugin can resolve back into the
> compiler it is loaded by. These options are also passed to frontends
> that do not install compiler-proper import libraries.
> 
> With bootstrap-lto-lean, their PE export tables can exceed the 16-bit
> ordinal limit and some LTO symbols cannot be exported with their
> resulting types.
> 
> Define a backend library list without the mingw plugin export options
> and use it for gnat1, f951, lto1, lto-dump, cc1obj, and cc1objplus.
> 
> Unlike the executables, libgccjit still needs its public import library.
> Generate a PE definition file from the existing libgccjit export list,
> disable automatic exports, and keep --out-implib for the public API.
> 
> cc1 and cc1plus also still need to export their public APIs, so add
> -fno-lto to them, also add -ffat-lto-objects to the bootstrap-lto-lean
> config for mingw targets so that cc1 and cc1plus can be linked with LTO
> objects.
> 
> config/ChangeLog:
> 
> 	PR lto/115300
> 	bootstrap-lto-lean.mk (STAGE3_CFLAGS): Add -ffat-lto-objects for
> 	mingw targets.
> 	(STAGEfeedback_CFLAGS): Likewise.
> 
> gcc/ChangeLog:
> 
> 	PR lto/115300
> 	Makefile.in (BACKENDLIBS_NO_PLUGIN_EXPORTS): New variable,
> 	similar to BACKENDLIBS but without PLUGINLIBS.
> 	(PLUGIN_IMPLIB_LINKFLAGS): New variable, expands to -fno-lto if
> 	plugin_implib is yes and ALL_LINKERFLAGS contains
> 	-ffat-lto-objects.
> 
> gcc/ada/ChangeLog:
> 
> 	PR lto/115300
> 	gcc-interface/Make-lang.in (gnat1): Use
> 	$(BACKENDLIBS_NO_PLUGIN_EXPORTS) instead of $(BACKENDLIBS).
> 
> gcc/c/ChangeLog:
> 
> 	PR lto/115300
> 	Make-lang.in (cc1): Add $(PLUGIN_IMPLIB_LINKFLAGS) to the link
> 	command.
> 
> gcc/cp/ChangeLog:
> 
> 	PR lto/115300
> 	Make-lang.in (cc1plus): Add $(PLUGIN_IMPLIB_LINKFLAGS) to the
> 	link command.
> 
> gcc/fortran/ChangeLog:
> 
> 	PR lto/115300
> 	Make-lang.in (f951): Use $(BACKENDLIBS_NO_PLUGIN_EXPORTS)
> 	instead of $(BACKENDLIBS).
> 
> gcc/jit/ChangeLog:
> 
> 	PR lto/115300
> 	Make-lang.in (LIBGCCJIT_DEF_FILE): New variable, dynamically
> 	generated from libgccjit.exports.
> 	(LIBGCCJIT_EXTRA_OPTS): Add $(LIBGCCJIT_DEF_FILE).
> 	(libgccjit): Add $(LIBGCCJIT_DEF_FILE) to the link command and
> 	use $(BACKENDLIBS_NO_PLUGIN_EXPORTS) instead of $(BACKENDLIBS).
> 	(jit.mostlyclean): Add cleanup of $(LIBGCCJIT_DEF_FILE).
> 	libgccjit.exports: Adjust comments.
> 
> gcc/lto/ChangeLog:
> 
> 	PR lto/115300
> 	Make-lang.in (lto1): Use $(BACKENDLIBS_NO_PLUGIN_EXPORTS)
> 	instead of $(BACKENDLIBS).
> 	(lto-dump): Likewise.
> 
> gcc/objc/ChangeLog:
> 
> 	PR lto/115300
> 	Make-lang.in (cc1obj): Use $(BACKENDLIBS_NO_PLUGIN_EXPORTS)
> 	instead of $(BACKENDLIBS).
> 
> gcc/objcp/ChangeLog:
> 
> 	PR lto/115300
> 	Make-lang.in (cc1objplus): Use $(BACKENDLIBS_NO_PLUGIN_EXPORTS)
> 	instead of $(BACKENDLIBS).
> 
> Signed-off-by: Zhongteng Gui <dragon-archer@outlook.com>
> ---
>  config/bootstrap-lto-lean.mk       |  4 ++++
>  gcc/Makefile.in                    |  4 ++++
>  gcc/ada/gcc-interface/Make-lang.in |  2 +-
>  gcc/c/Make-lang.in                 |  2 +-
>  gcc/cp/Make-lang.in                |  2 +-
>  gcc/fortran/Make-lang.in           |  2 +-
>  gcc/jit/Make-lang.in               | 15 +++++++++++----
>  gcc/jit/libgccjit.exports          |  2 +-
>  gcc/lto/Make-lang.in               |  4 ++--
>  gcc/objc/Make-lang.in              |  2 +-
>  gcc/objcp/Make-lang.in             |  2 +-
>  11 files changed, 28 insertions(+), 13 deletions(-)
> 
Changes since v1: Group most changes to x-mingw32, so other hosts won't be affected.
Changes in bootstrap-lto-lean.mk can't be gracefully moved, but is already guarded by
`ifneq (,$(findstring mingw,$(host_os)))`. Similarily for changes to jit's Make-lang.in.

Tested on MSYS2's GitHub CI: https://github.com/msys2/MINGW-packages/actions/runs/37966214698/job/114132474552?pr=30935

---

>From 8a1278f59a25146b31a84d62b3f027e716f01963 Mon Sep 17 00:00:00 2001
From: Zhongteng Gui <dragon-archer@outlook.com>
Date: Sat, 10 Oct 2026 01:17:17 +0800
Subject: [PATCH v2] lto, mingw: Do not export symbols from some frontends
 [PR115300]

This patch works around bootstrapping on x86_64-w64-mingw32 targets with
bootstrap-lto-lean and --enable-plugin enabled.  Note that
i686-w64-mingw32 targets still fail with bootstrap-lto-lean even without
--enable-plugin due to a segfault in lto1.exe.

--enable-plugin adds -Wl,--export-all-symbols and -Wl,--out-implib=$@.a
to PLUGINLIBS on MinGW hosts, so that a plugin can resolve back into the
compiler it is loaded by.  These options are also passed to frontends
that do not install compiler-proper import libraries.

With bootstrap-lto-lean, their PE export tables can exceed the 16-bit
ordinal limit and some LTO symbols cannot be exported with their
resulting types.

Use the MinGW host makefile fragment to omit the plugin export options
when linking gnat1, f951, lto1, lto-dump, cc1obj, and cc1objplus.  This
keeps the target-specific handling out of the language-independent
makefiles.

Unlike the executables, libgccjit still needs its public import library.
Generate a PE definition file from the existing libgccjit export list,
disable automatic exports, and keep --out-implib for the public API.

cc1 and cc1plus also still need to export their public APIs, so add
-fno-lto to their MinGW link flags when bootstrap-lto-lean provides
-ffat-lto-objects.  Add -ffat-lto-objects to the bootstrap-lto-lean
configuration for MinGW targets so that cc1 and cc1plus can be linked
with LTO objects.

	PR lto/115300

config/ChangeLog:

	bootstrap-lto-lean.mk (STAGE3_CFLAGS): Add -ffat-lto-objects for
	mingw targets.
	(STAGEfeedback_CFLAGS): Likewise.

gcc/ChangeLog:

	config/i386/x-mingw32: Avoid exporting compiler-internal symbols
	from frontends without a plugin API.  Add -fno-lto when linking
	cc1 and cc1plus with fat LTO objects.

gcc/jit/ChangeLog:

	Make-lang.in (LIBGCCJIT_DEF_FILE): New variable, dynamically
	generated from libgccjit.exports.
	(LIBGCCJIT_EXTRA_OPTS): Add LIBGCCJIT_DEF_FILE and disable
	automatic exports.
	(libgccjit): Add LIBGCCJIT_DEF_FILE to the link dependencies.
	(jit.mostlyclean): Remove LIBGCCJIT_DEF_FILE and its temporary
	file.

Signed-off-by: Zhongteng Gui <dragon-archer@outlook.com>
---
 config/bootstrap-lto-lean.mk |  4 ++++
 gcc/config/i386/x-mingw32    |  9 +++++++++
 gcc/jit/Make-lang.in         | 14 +++++++++++---
 3 files changed, 24 insertions(+), 3 deletions(-)

diff --git a/config/bootstrap-lto-lean.mk b/config/bootstrap-lto-lean.mk
index 42cb3394c70..5f3d2b56fbf 100644
--- a/config/bootstrap-lto-lean.mk
+++ b/config/bootstrap-lto-lean.mk
@@ -5,6 +5,10 @@ STAGE3_CFLAGS += -flto=jobserver
 override STAGEtrain_CFLAGS := $(filter-out -flto=jobserver,$(STAGEtrain_CFLAGS))
 STAGEtrain_GENERATOR_CFLAGS += -flto=jobserver
 STAGEfeedback_CFLAGS += -flto=jobserver
+ifneq (,$(findstring mingw,$(host_os)))
+STAGE3_CFLAGS += -ffat-lto-objects
+STAGEfeedback_CFLAGS += -ffat-lto-objects
+endif
 
 # assumes the host supports the linker plugin
 LTO_AR = $$r/$(HOST_SUBDIR)/prev-gcc/gcc-ar$(exeext) -B$$r/$(HOST_SUBDIR)/prev-gcc/
diff --git a/gcc/config/i386/x-mingw32 b/gcc/config/i386/x-mingw32
index 3ac2ba57631..e5062047bcf 100644
--- a/gcc/config/i386/x-mingw32
+++ b/gcc/config/i386/x-mingw32
@@ -24,6 +24,15 @@ local_includedir=$(libsubdir)/$(unlibsubdir)/..`echo $(exec_prefix) | sed -e 's|
 # Add Windows socket library.
 LIBS += -lws2_32
 
+ifeq ($(plugin_implib),yes)
+# Avoid exporting compiler-internal symbols from frontends without a plugin API.
+gnat1$(exeext) f951$(exeext) lto1$(exeext) lto-dump$(exeext) \
+cc1obj$(exeext) cc1objplus$(exeext) $(LIBGCCJIT_FILENAME): PLUGINLIBS =
+
+ALL_LINKERFLAGS += $(if $(filter cc1$(exeext) cc1plus$(exeext),$@),\
+	$(if $(filter -ffat-lto-objects,$(ALL_CXXFLAGS)),-fno-lto))
+endif
+
 # On MinGW, we use "%IA64d" to print 64-bit integers, and the format-checking
 # code does not handle that, so we have to disable checking here.
 WERROR_FLAGS += -Wno-format
diff --git a/gcc/jit/Make-lang.in b/gcc/jit/Make-lang.in
index eb1c9966062..d87c90b7a76 100644
--- a/gcc/jit/Make-lang.in
+++ b/gcc/jit/Make-lang.in
@@ -154,8 +154,16 @@ jit_OBJS = $(JIT_OBJS) $(JIT_TARGET_OBJS)
 jit-warn = $(STRICT_WARN)
 
 ifneq (,$(findstring mingw,$(target)))
+LIBGCCJIT_DEF_FILE = libgccjit.def
+
 # Create import library
-LIBGCCJIT_EXTRA_OPTS = -Wl,--out-implib,$(LIBGCCJIT_IMPORT_LIB)
+LIBGCCJIT_EXTRA_OPTS = $(LIBGCCJIT_DEF_FILE) -Wl,--exclude-all-symbols \
+ -Wl,--out-implib,$(LIBGCCJIT_IMPORT_LIB)
+
+$(LIBGCCJIT_DEF_FILE): $(srcdir)/jit/libgccjit.exports
+	echo EXPORTS > $@.tmp
+	$(AWK) '/^_/ { sub(/^_/, ""); print }' $< >> $@.tmp
+	$(SHELL) $(srcdir)/../move-if-change $@.tmp $@
 else
 ifneq (,$(findstring darwin,$(host)))
 LIBGCCJIT_VERS = $(LIBGCCJIT_CURRENT).$(LIBGCCJIT_REVISION).$(LIBGCCJIT_AGE)
@@ -184,7 +192,7 @@ EXTRA_GCC_OBJS_EXCLUSIVE = $(foreach _obj1, $(EXTRA_GCC_OBJS), \
 $(LIBGCCJIT_FILENAME): $(jit_OBJS) \
 	libbackend.a libcommon-target.a libcommon.a \
 	$(CPPLIB) $(LIBDECNUMBER) \
-	$(LIBDEPS) $(srcdir)/jit/libgccjit.map \
+	$(LIBDEPS) $(srcdir)/jit/libgccjit.map $(LIBGCCJIT_DEF_FILE) \
 	$(EXTRA_GCC_OBJS_EXCLUSIVE) $(jit.prev)
 	@$(call LINK_PROGRESS,$(INDEX.jit),start)
 	+$(LLINKER) $(ALL_LINKERFLAGS) $(JIT_LDFLAGS) -o $@ -shared \
@@ -428,7 +436,7 @@ jit.uninstall:
 jit.mostlyclean:
 	-rm -f $(LIBGCCJIT_FILENAME) $(LIBGCCJIT_SYMLINK)
 	-rm -f $(LIBGCCJIT_LINKER_NAME_SYMLINK) $(FULL_DRIVER_NAME)
-	-rm -f $(LIBGCCJIT_SONAME)
+	-rm -f $(LIBGCCJIT_SONAME) $(LIBGCCJIT_DEF_FILE) $(LIBGCCJIT_DEF_FILE).tmp
 	-rm -f $(jit_OBJS)
 
 jit.clean:
-- 
2.56.0.windows.2




More information about the Gcc-patches mailing list