Hi, I'm forwarding the email that I sent to the other mailing list
here as it seems this is the more appropriate mailing list.

---------- Forwarded message ---------
From: Julian Waters <[email protected]>
Date: Mon, Nov 16, 2026 at 3:02 PM
Subject: Windows: Late references to symbols claimed by linker plugin
appear to cause defined in discarded section errors
To: <[email protected]>


Hi,

I've been trying to debug a mysterious issue with ld.exe when linking
with object files that were compiled with link time optimization.
Almost at random certain symbols will make the linker explode, with
something along the lines of:

`__Balloc_D2A' referenced in section `.text' of
C:/Users/vertig0/Downloads/Windows/gcc/lib/gcc/x86_64-w64-mingw32/17.0.0/../../../../x86_64-w64-mingw32/lib/../lib/libmingwex.a(lib64_libmingwex_a-strtodg.o):
defined in discarded section `.text' of lib64_libmingwex_a-misc.o
(symbol from plugin)

After much digging through the code of binutils I think I may
(Emphasis may) have figured out what's going on. It seems like the
plugin claims the problematic symbols as gcc GIMPLE rather than final
object code, and lazily only resolves early references to those
symbols, marking that they are only referenced from GIMPLE, signaling
that gcc is then free to localize or inline them entirely.

But late references to those symbols, such as from compiler inserted
calls to certain methods, are also possible, and can happen after the
plugin runs, which then pulls in references to dependent symbols as
object code rather than GIMPLE, those dependent symbols then
themselves reference the problematic symbols, and since the
problematic symbols were either entirely inlined or localized, the
linker looks up their old definition by name instead, which happens to
be in a dummy section the plugin created to store symbols that are
from the plugin, which is not actually used for linking and has the
SEC_EXCLUDE flag set.

When the linker encounters the SEC_EXCLUDE flag on the dummy section
it immediately throws an error, preventing the link from being
completed. Marking the problematic symbols attribute used from source
code, or alternatively using -Wl,--whole-archive with the archive
containing said symbols resolves this error. I haven't dug deep enough
to find the exact mechanism that attribute used fixes the issue, but I
assume that with --whole-archive the entire archive's catalog of
symbols are now GIMPLE too rather than it being lazily loaded, so the
optimizer can see the symbol is needed and knows to keep it, so there
are no late references to those symbols left.

Does this breakdown of the issue sound plausible? I struggled greatly
in analyzing what was actually going on at all since I'm not very
familiar with the deepest internals of either gcc or binutils, so I'm
not fully certain this is correct. Any corrections on what actually
happens would be appreciated. On the off chance that my theory is
correct though, is there a way to enhance the linker (Or the linker
plugin, whichever) to handle these cases to fix such linker errors?

I am still not very sure how to reduce this into a reproducer, as I
encountered this linker error while compiling as.exe in
binutils-gdb(!!!) which is quite a big program. I will try to work
towards doing so, though.

best regards,
Julian

Reply via email to