Bug 24267

Summary: ld discards a symbol with -flto and -static
Product: binutils Reporter: Martin Liška <marxin.liska>
Component: ldAssignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED FIXED    
Severity: normal CC: amodra, dave.korn.cygwin, dj, eliz, freddy77, hjl.tools, hp
Priority: P2    
Version: 2.33   
Target Milestone: 2.33   
Host: Target:
Build: Last reconfirmed: 2019-02-25 00:00:00
Attachments: A patch

Description Martin Liška 2019-02-25 13:19:57 UTC
This is follow up of:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81879

$ cat simpler.ii
namespace std {
template <class> struct char_traits;
template <typename a, typename = char_traits<a>> class basic_ostream;
typedef basic_ostream<char> b;
class ios_base {
public:
  class Init {
  public:
    Init();
  };
};
template <typename> class ctype {
  virtual char do_widen(char c) const { return c; }
};
class d {
  ctype<char> e;
};
template <typename, typename> class basic_ostream : d {};
template <class f>
basic_ostream<char, f> &operator<<(basic_ostream<char, f> &, const char *);
b cout;
ios_base::Init g;
} // namespace std
int main() { std::cout << "ok"; }

When compiler with:

$ x86_64-w64-mingw32-g++ -v
Using built-in specs.
COLLECT_GCC=x86_64-w64-mingw32-g++
COLLECT_LTO_WRAPPER=/usr/lib64/gcc/x86_64-w64-mingw32/8.2.0/lto-wrapper
Target: x86_64-w64-mingw32
Configured with: ../configure --prefix=/usr --bindir=/usr/bin --includedir=/usr/include --libdir=/usr/lib64 --libexecdir=/usr/lib64 --mandir=/usr/share/man --infodir=/usr/share/info --datadir=/usr/share --build=x86_64-suse-linux-gnu --host=x86_64-suse-linux-gnu --target=x86_64-w64-mingw32 --with-gnu-as --with-gnu-ld --verbose --without-newlib --disable-multilib --enable-shared --disable-plugin --with-system-zlib --disable-nls --without-included-gettext --disable-win32-registry --enable-threads=posix --enable-version-specific-runtime-libs --with-sysroot=/usr/x86_64-w64-mingw32/sys-root --enable-languages=c,c++,fortran,objc,obj-c++ --without-x --enable-hash-synchronization --enable-fully-dynamic-string --enable-libgomp --enable-linker-build-id --disable-vtable-verify --with-pkgversion='SUSE Linux'
Thread model: posix
gcc version 8.2.0 (SUSE Linux) 

$ x86_64-w64-mingw32-g++ simpler.ii -flto -O2 -static --save-temps --verbose
...

$ cat simpler.res
1
simpler.o 10
257 4b7905b9947ad39f PREEMPTED_REG _ZNKSt5ctypeIcE8do_widenEc
260 4b7905b9947ad39f PREVAILING_DEF main
279 4b7905b9947ad39f PREEMPTED_REG _ZSt4cout
282 4b7905b9947ad39f PREVAILING_DEF_IRONLY _ZSt1g
291 4b7905b9947ad39f PREEMPTED_REG _ZTSSt5ctypeIcE
305 4b7905b9947ad39f PREEMPTED_REG _ZTISt5ctypeIcE
207 4b7905b9947ad39f PREEMPTED_REG _ZTVSt5ctypeIcE
316 4b7905b9947ad39f RESOLVED_EXEC _ZTVN10__cxxabiv117__class_type_infoE
271 4b7905b9947ad39f RESOLVED_EXEC _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
276 4b7905b9947ad39f RESOLVED_EXEC _ZNSt8ios_base4InitC1Ev

as seen _ZNKSt5ctypeIcE8do_widenEc is both defined in the static libstdc++ and simpler.ii. Linker tells to not to emit assembly for the symbol. However we then end up with a binary where the symbol is missing:

$ x86_64-w64-mingw32-nm a.exe | grep _ZNKSt5ctypeIcE8do_widenEc
0000000000acda70 N .debug_frame$_ZNKSt5ctypeIcE8do_widenEc
00000000004b9150 p .pdata$_ZNKSt5ctypeIcE8do_widenEc
00000000004c51b4 r .xdata$_ZNKSt5ctypeIcE8do_widenEc
                 U _ZNKSt5ctypeIcE8do_widenEc
                 U _ZNKSt5ctypeIcE8do_widenEc
                 U _ZNKSt5ctypeIcE8do_widenEc
                 U _ZNKSt5ctypeIcE8do_widenEc
                 U _ZNKSt5ctypeIcE8do_widenEc
                 U _ZNKSt5ctypeIcE8do_widenEc
                 U _ZNKSt5ctypeIcE8do_widenEc

Which is causing wine crash:

 wine a.exe
wine: Unhandled page fault on read access to 0x00000000 at address 0x4a3a2e (thread 002b), starting debugger...

So I suspect ld.bfd for not copying the _ZNKSt5ctypeIcE8do_widenEc from static libstdc++:

$ x86_64-w64-mingw32-nm /usr/lib64/gcc/x86_64-w64-mingw32/8.2.0/libstdc++.dll.a | grep _ZNKSt5ctypeIcE8do_widenEc
0000000000000000 I __imp__ZNKSt5ctypeIcE8do_widenEc
0000000000000000 T _ZNKSt5ctypeIcE8do_widenEc

If needed I can upload somewhere libstdc++.dll.a file.
Comment 1 H.J. Lu 2019-02-25 14:04:15 UTC
Does it happen on Linux?
Comment 2 Martin Liška 2019-02-25 14:06:29 UTC
(In reply to H.J. Lu from comment #1)
> Does it happen on Linux?

No, it's specific to w64-mingw32 target. I'm testing that on Linux where I use cross compiler + wine:

$ wine --version
wine-4.1
Comment 3 H.J. Lu 2019-02-25 16:02:24 UTC
(In reply to Martin Liška from comment #2)
> (In reply to H.J. Lu from comment #1)
> > Does it happen on Linux?
> 
> No, it's specific to w64-mingw32 target. I'm testing that on Linux where I
> use cross compiler + wine:

Some LTO fixes are ELF specific.  Someone needs to take a look at
them to see if COFF needs similar fixes.
Comment 4 Martin Liška 2019-02-25 20:39:40 UTC
(In reply to H.J. Lu from comment #3)
> (In reply to Martin Liška from comment #2)
> > (In reply to H.J. Lu from comment #1)
> > > Does it happen on Linux?
> > 
> > No, it's specific to w64-mingw32 target. I'm testing that on Linux where I
> > use cross compiler + wine:
> 
> Some LTO fixes are ELF specific.  Someone needs to take a look at
> them to see if COFF needs similar fixes.

Are you talking about any recent regression that were just fixed for ELF?
Btw. do you have a COFF specific maintainer or a person who can help us?
Comment 5 H.J. Lu 2019-02-25 21:00:23 UTC
(In reply to Martin Liška from comment #4)
> (In reply to H.J. Lu from comment #3)
> > (In reply to Martin Liška from comment #2)
> > > (In reply to H.J. Lu from comment #1)
> > > > Does it happen on Linux?
> > > 
> > > No, it's specific to w64-mingw32 target. I'm testing that on Linux where I
> > > use cross compiler + wine:
> > 
> > Some LTO fixes are ELF specific.  Someone needs to take a look at
> > them to see if COFF needs similar fixes.
> 
> Are you talking about any recent regression that were just fixed for ELF?

Not regression.  They are LTO bug fixes.

> Btw. do you have a COFF specific maintainer or a person who can help us?

DJ Delorie <dj@redhat.com> and Dave Korn <dave.korn.cygwin@gmail.com> are
COFF maintainers.
Comment 6 Martin Liška 2019-02-25 21:05:10 UTC
(In reply to H.J. Lu from comment #5)
> (In reply to Martin Liška from comment #4)
> > (In reply to H.J. Lu from comment #3)
> > > (In reply to Martin Liška from comment #2)
> > > > (In reply to H.J. Lu from comment #1)
> > > > > Does it happen on Linux?
> > > > 
> > > > No, it's specific to w64-mingw32 target. I'm testing that on Linux where I
> > > > use cross compiler + wine:
> > > 
> > > Some LTO fixes are ELF specific.  Someone needs to take a look at
> > > them to see if COFF needs similar fixes.
> > 
> > Are you talking about any recent regression that were just fixed for ELF?
> 
> Not regression.  They are LTO bug fixes.

Can you be please more concrete?

> 
> > Btw. do you have a COFF specific maintainer or a person who can help us?
> 
> DJ Delorie <dj@redhat.com> and Dave Korn <dave.korn.cygwin@gmail.com> are
> COFF maintainers.

Good, I'm adding them.
Comment 7 H.J. Lu 2019-02-25 21:16:07 UTC
(In reply to Martin Liška from comment #6)
> > 
> > Not regression.  They are LTO bug fixes.
> 
> Can you be please more concrete?

Check PR 23958, PR 23818, PR 23309, PR 22983, PR 22751, PR 22502,
PR 22220, PR 21382, PR 18250, PR 20267, PR 20321.
Comment 8 Martin Liška 2019-02-26 13:53:46 UTC
(In reply to H.J. Lu from comment #7)
> (In reply to Martin Liška from comment #6)
> > > 
> > > Not regression.  They are LTO bug fixes.
> > 
> > Can you be please more concrete?
> 
> Check PR 23958, PR 23818, PR 23309, PR 22983, PR 22751, PR 22502,
> PR 22220, PR 21382, PR 18250, PR 20267, PR 20321.

I would like to mention that I also tested binutils 2.32 and it's affected.
Let me know if you'll need any help with this?
Comment 9 Frediano Ziglio 2019-02-27 09:57:25 UTC
(In reply to Martin Liška from comment #8)
> (In reply to H.J. Lu from comment #7)
> > (In reply to Martin Liška from comment #6)
> > > > 
> > > > Not regression.  They are LTO bug fixes.
> > > 
> > > Can you be please more concrete?
> > 
> > Check PR 23958, PR 23818, PR 23309, PR 22983, PR 22751, PR 22502,
> > PR 22220, PR 21382, PR 18250, PR 20267, PR 20321.
> 
> I would like to mention that I also tested binutils 2.32 and it's affected.
> Let me know if you'll need any help with this?

I don't understand much the details but I think what H.J. Lu was trying to say is that maybe was fixed for ELF but not for PE/COFF so to have a look at the mentioned PRs.
I cannot suggest any of them, PR 22220 refers to PREVAILING_DEF_IRONLY but the issue seems the opposite.
Comment 10 Martin Liška 2019-02-27 14:10:36 UTC
> I don't understand much the details but I think what H.J. Lu was trying to
> say is that maybe was fixed for ELF but not for PE/COFF so to have a look at
> the mentioned PRs.
> I cannot suggest any of them, PR 22220 refers to PREVAILING_DEF_IRONLY but
> the issue seems the opposite.

I know, I filtered these PRs:

PR23958 - elf-related change, but related to symbol versioning
PR23818 - likewise
PR23309 - changes only in generic code
PR22983 - related to dynamic-list only
PR22751 - changes only in generic code
PR22502 - elf-related change, might by a candidate for COFF
PR20321 - a segfault, changes in generic code
PR20267 - llvm-related crash
PR18250 - likewise
PR21382 - elf-related change, might by a candidate for COFF
PR22220 - elf-related change, might by a candidate for COFF, but's symbol versioning related

I'll investigate PR22502 and PR21382
Comment 11 Martin Liška 2019-02-28 12:40:23 UTC
I've got a patch candidate that can solve it:

diff --git a/bfd/coffgen.c b/bfd/coffgen.c
index 309e1249ac..1d200b066b 100644
--- a/bfd/coffgen.c
+++ b/bfd/coffgen.c
@@ -2678,9 +2678,9 @@ _bfd_coff_section_already_linked (bfd *abfd,
 	 and match any comdat section with comdat name of <key>, and
 	 any linkonce section with the same suffix, ie.
 	 .gnu.linkonce.*.<key>.  */
-      if (((s_comdat != NULL) == (l_comdat != NULL)
-	   && strcmp (name, l->sec->name) == 0)
-	  || (l->sec->owner->flags & BFD_PLUGIN) != 0)
+      if (((s_comdat != NULL) == (l_comdat != NULL))
+          && ((strcmp (name, l->sec->name) == 0)
+            || (l->sec->owner->flags & BFD_PLUGIN) != 0))
 	{
 	  /* The section has already been linked.  See if we should
 	     issue a warning.  */

However, Honza is still thinking that LDPR_PREVAILING_DEF_IRONLY should be used for the symbol. If I see correctly it's set in ld/plugin.c base on
 else if (owner_sec->owner == abfd).

Can you please help me where sections are set for each symbol? Is it a COFF specific code?
Comment 12 H.J. Lu 2019-02-28 12:51:56 UTC
(In reply to Martin Liška from comment #11)
> I've got a patch candidate that can solve it:
> 
> diff --git a/bfd/coffgen.c b/bfd/coffgen.c
> index 309e1249ac..1d200b066b 100644
> --- a/bfd/coffgen.c
> +++ b/bfd/coffgen.c
> @@ -2678,9 +2678,9 @@ _bfd_coff_section_already_linked (bfd *abfd,
>  	 and match any comdat section with comdat name of <key>, and
>  	 any linkonce section with the same suffix, ie.
>  	 .gnu.linkonce.*.<key>.  */
> -      if (((s_comdat != NULL) == (l_comdat != NULL)
> -	   && strcmp (name, l->sec->name) == 0)
> -	  || (l->sec->owner->flags & BFD_PLUGIN) != 0)
> +      if (((s_comdat != NULL) == (l_comdat != NULL))
> +          && ((strcmp (name, l->sec->name) == 0)
> +            || (l->sec->owner->flags & BFD_PLUGIN) != 0))
>  	{
>  	  /* The section has already been linked.  See if we should
>  	     issue a warning.  */
> 
> However, Honza is still thinking that LDPR_PREVAILING_DEF_IRONLY should be
> used for the symbol. If I see correctly it's set in ld/plugin.c base on
>  else if (owner_sec->owner == abfd).

What do ELF linkers (gold and bfd) get?
Comment 13 Martin Liška 2019-02-28 12:54:38 UTC
> 
> What do ELF linkers (gold and bfd) get?

We get:

(gdb) p owner_sec->owner->filename
$5 = 0x69ae80 "main.o (symbol from plugin)"
Comment 14 H.J. Lu 2019-02-28 12:55:57 UTC
(In reply to Martin Liška from comment #13)
> > 
> > What do ELF linkers (gold and bfd) get?
> 
> We get:
> 
> (gdb) p owner_sec->owner->filename
> $5 = 0x69ae80 "main.o (symbol from plugin)"

Do gold and bfd get the same symbol resolution?
Comment 15 Martin Liška 2019-02-28 12:58:50 UTC
Yes, both return:

...
262 545ca41eb4de6c9c PREVAILING_DEF _ZNKSt5ctypeIcE8do_widenEc
Comment 16 H.J. Lu 2019-02-28 16:39:48 UTC
(In reply to Martin Liška from comment #15)
> Yes, both return:
> 
> ...
> 262 545ca41eb4de6c9c PREVAILING_DEF _ZNKSt5ctypeIcE8do_widenEc

Since it is referenced from istream-inst.o, it must be
_ZNKSt5ctypeIcE8do_widenEc.
Comment 17 H.J. Lu 2019-02-28 16:40:32 UTC
(In reply to H.J. Lu from comment #16)
> (In reply to Martin Liška from comment #15)
> > Yes, both return:
> > 
> > ...
> > 262 545ca41eb4de6c9c PREVAILING_DEF _ZNKSt5ctypeIcE8do_widenEc
> 
> Since it is referenced from istream-inst.o, it must be
> _ZNKSt5ctypeIcE8do_widenEc.

I meant it must be PREVAILING_DEF and can't be PREVAILING_DEF_IRONLY.
Comment 18 Martin Liška 2019-03-01 09:18:01 UTC
> I meant it must be PREVAILING_DEF and can't be PREVAILING_DEF_IRONLY.

Yes, PREVAILING_DEF would be fine as well for COFF.
Comment 19 Martin Liška 2019-03-06 15:05:52 UTC
H.J. : Can you please help me how to find a place which makes a real decision about which BFD (object) will be used for each symbol?
Comment 20 H.J. Lu 2019-03-10 22:33:26 UTC
(In reply to Martin Liška from comment #19)
> H.J. : Can you please help me how to find a place which makes a real
> decision about which BFD (object) will be used for each symbol?

Is there a way to easily reproduce it on Fedora 29?
Comment 21 Martin Liška 2019-03-11 11:39:37 UTC
(In reply to H.J. Lu from comment #20)
> (In reply to Martin Liška from comment #19)
> > H.J. : Can you please help me how to find a place which makes a real
> > decision about which BFD (object) will be used for each symbol?
> 
> Is there a way to easily reproduce it on Fedora 29?

I guess so, you'll need to install cross compiler, linker, etc.:
https://fedoraproject.org/wiki/MinGW/Tutorial
https://fedora.pkgs.org/27/fedora-x86_64/mingw64-gcc-7.2.0-1.fc27.x86_64.rpm.html
Comment 22 H.J. Lu 2019-03-12 01:10:17 UTC
Works for me with binutils 2.32:

[hjl@gnu-cfl-2 pr24267]$ cat x.ii 
namespace std {
template <class> struct char_traits;
template <typename a, typename = char_traits<a>> class basic_ostream;
typedef basic_ostream<char> b;
class ios_base {
public:
  class Init {
  public:
    Init();
  };
};
template <typename> class ctype {
  virtual char do_widen(char c) const { return c; }
};
class d {
  ctype<char> e;
};
template <typename, typename> class basic_ostream : d {};
template <class f>
basic_ostream<char, f> &operator<<(basic_ostream<char, f> &, const char *);
b cout;
ios_base::Init g;
} // namespace std
int main() { std::cout << "ok"; }

[hjl@gnu-cfl-2 pr24267]$ x86_64-w64-mingw32-g++ -B./ -v -Wl,-v -flto -O2 -static x.ii
Using built-in specs.
COLLECT_GCC=x86_64-w64-mingw32-g++
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-w64-mingw32/8.3.0/lto-wrapper
Target: x86_64-w64-mingw32
Configured with: ../configure --prefix=/usr --bindir=/usr/bin --includedir=/usr/include --mandir=/usr/share/man --infodir=/usr/share/info --datadir=/usr/share --build=x86_64-redhat-linux-gnu --host=x86_64-redhat-linux-gnu --with-gnu-as --with-gnu-ld --verbose --without-newlib --disable-multilib --disable-plugin --with-system-zlib --disable-nls --without-included-gettext --disable-win32-registry --enable-languages=c,c++,objc,obj-c++,fortran --with-bugurl=http://bugzilla.redhat.com/bugzilla --with-cloog --enable-threads=posix --enable-libgomp --target=x86_64-w64-mingw32 --with-sysroot=/usr/x86_64-w64-mingw32/sys-root --with-gxx-include-dir=/usr/x86_64-w64-mingw32/sys-root/mingw/include/c++
Thread model: posix
gcc version 8.3.0 20190222 (Fedora MinGW 8.3.0-1.fc29) (GCC) 
COLLECT_GCC_OPTIONS='-B' './' '-v' '-flto' '-O2' '-static' '-mtune=generic' '-march=x86-64'
 /usr/libexec/gcc/x86_64-w64-mingw32/8.3.0/cc1plus -fpreprocessed x.ii -quiet -dumpbase x.ii -mtune=generic -march=x86-64 -auxbase x -O2 -version -flto -o /tmp/ccgxROxd.s
GNU C++14 (GCC) version 8.3.0 20190222 (Fedora MinGW 8.3.0-1.fc29) (x86_64-w64-mingw32)
	compiled by GNU C version 8.2.1 20181215 (Red Hat 8.2.1-6), GMP version 6.1.2, MPFR version 3.1.6-p2, MPC version 1.1.0, isl version isl-0.16.1-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
GNU C++14 (GCC) version 8.3.0 20190222 (Fedora MinGW 8.3.0-1.fc29) (x86_64-w64-mingw32)
	compiled by GNU C version 8.2.1 20181215 (Red Hat 8.2.1-6), GMP version 6.1.2, MPFR version 3.1.6-p2, MPC version 1.1.0, isl version isl-0.16.1-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 782ecece326e64c4c9691227b10ab17e
COLLECT_GCC_OPTIONS='-B' './' '-v' '-flto' '-O2' '-static' '-mtune=generic' '-march=x86-64'
 /usr/lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/as -v -o /tmp/ccYNIbg4.o /tmp/ccgxROxd.s
GNU assembler version 2.30 (x86_64-w64-mingw32) using BFD version (GNU Binutils) 2.30
COMPILER_PATH=./:/usr/libexec/gcc/x86_64-w64-mingw32/8.3.0/:/usr/libexec/gcc/x86_64-w64-mingw32/8.3.0/:/usr/libexec/gcc/x86_64-w64-mingw32/:/usr/lib/gcc/x86_64-w64-mingw32/8.3.0/:/usr/lib/gcc/x86_64-w64-mingw32/:/usr/lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/
LIBRARY_PATH=./:/usr/lib/gcc/x86_64-w64-mingw32/8.3.0/:/usr/lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/lib/../lib/:/usr/x86_64-w64-mingw32/sys-root/mingw/lib/../lib/:/usr/lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/lib/:/usr/x86_64-w64-mingw32/sys-root/mingw/lib/
COLLECT_GCC_OPTIONS='-B' './' '-v' '-flto' '-O2' '-static' '-mtune=generic' '-march=x86-64'
 /usr/libexec/gcc/x86_64-w64-mingw32/8.3.0/collect2 -plugin /usr/libexec/gcc/x86_64-w64-mingw32/8.3.0/liblto_plugin.so -plugin-opt=/usr/libexec/gcc/x86_64-w64-mingw32/8.3.0/lto-wrapper -plugin-opt=-fresolution=/tmp/ccg7K7YU.res -plugin-opt=-pass-through=-lmingw32 -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_eh -plugin-opt=-pass-through=-lmoldname -plugin-opt=-pass-through=-lmingwex -plugin-opt=-pass-through=-lmsvcrt -plugin-opt=-pass-through=-lpthread -plugin-opt=-pass-through=-ladvapi32 -plugin-opt=-pass-through=-lshell32 -plugin-opt=-pass-through=-luser32 -plugin-opt=-pass-through=-lkernel32 -plugin-opt=-pass-through=-lmingw32 -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_eh -plugin-opt=-pass-through=-lmoldname -plugin-opt=-pass-through=-lmingwex -plugin-opt=-pass-through=-lmsvcrt -flto --sysroot=/usr/x86_64-w64-mingw32/sys-root -m i386pep -Bstatic /usr/x86_64-w64-mingw32/sys-root/mingw/lib/../lib/crt2.o /usr/lib/gcc/x86_64-w64-mingw32/8.3.0/crtbegin.o -L. -L/usr/lib/gcc/x86_64-w64-mingw32/8.3.0 -L/usr/lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/lib/../lib -L/usr/x86_64-w64-mingw32/sys-root/mingw/lib/../lib -L/usr/lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/lib -L/usr/x86_64-w64-mingw32/sys-root/mingw/lib -v /tmp/ccYNIbg4.o -lstdc++ -lmingw32 -lgcc -lgcc_eh -lmoldname -lmingwex -lmsvcrt -lpthread -ladvapi32 -lshell32 -luser32 -lkernel32 -lmingw32 -lgcc -lgcc_eh -lmoldname -lmingwex -lmsvcrt /usr/lib/gcc/x86_64-w64-mingw32/8.3.0/crtend.o
collect2 version 8.3.0 20190222 (Fedora MinGW 8.3.0-1.fc29)
./ld -plugin /usr/libexec/gcc/x86_64-w64-mingw32/8.3.0/liblto_plugin.so -plugin-opt=/usr/libexec/gcc/x86_64-w64-mingw32/8.3.0/lto-wrapper -plugin-opt=-fresolution=/tmp/ccg7K7YU.res -plugin-opt=-pass-through=-lmingw32 -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_eh -plugin-opt=-pass-through=-lmoldname -plugin-opt=-pass-through=-lmingwex -plugin-opt=-pass-through=-lmsvcrt -plugin-opt=-pass-through=-lpthread -plugin-opt=-pass-through=-ladvapi32 -plugin-opt=-pass-through=-lshell32 -plugin-opt=-pass-through=-luser32 -plugin-opt=-pass-through=-lkernel32 -plugin-opt=-pass-through=-lmingw32 -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_eh -plugin-opt=-pass-through=-lmoldname -plugin-opt=-pass-through=-lmingwex -plugin-opt=-pass-through=-lmsvcrt --sysroot=/usr/x86_64-w64-mingw32/sys-root -m i386pep -Bstatic /usr/x86_64-w64-mingw32/sys-root/mingw/lib/../lib/crt2.o /usr/lib/gcc/x86_64-w64-mingw32/8.3.0/crtbegin.o -L. -L/usr/lib/gcc/x86_64-w64-mingw32/8.3.0 -L/usr/lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/lib/../lib -L/usr/x86_64-w64-mingw32/sys-root/mingw/lib/../lib -L/usr/lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/lib -L/usr/x86_64-w64-mingw32/sys-root/mingw/lib -v /tmp/ccYNIbg4.o -lstdc++ -lmingw32 -lgcc -lgcc_eh -lmoldname -lmingwex -lmsvcrt -lpthread -ladvapi32 -lshell32 -luser32 -lkernel32 -lmingw32 -lgcc -lgcc_eh -lmoldname -lmingwex -lmsvcrt /usr/lib/gcc/x86_64-w64-mingw32/8.3.0/crtend.o
GNU ld (GNU Binutils) 2.32.51.20190308
/usr/libexec/gcc/x86_64-w64-mingw32/8.3.0/lto-wrapper -fresolution=/tmp/ccg7K7YU.res -flinker-output=exec /tmp/ccYNIbg4.o 
x86_64-w64-mingw32-g++ @/tmp/ccAPPPih
Using built-in specs.
COLLECT_GCC=x86_64-w64-mingw32-g++
Target: x86_64-w64-mingw32
Configured with: ../configure --prefix=/usr --bindir=/usr/bin --includedir=/usr/include --mandir=/usr/share/man --infodir=/usr/share/info --datadir=/usr/share --build=x86_64-redhat-linux-gnu --host=x86_64-redhat-linux-gnu --with-gnu-as --with-gnu-ld --verbose --without-newlib --disable-multilib --disable-plugin --with-system-zlib --disable-nls --without-included-gettext --disable-win32-registry --enable-languages=c,c++,objc,obj-c++,fortran --with-bugurl=http://bugzilla.redhat.com/bugzilla --with-cloog --enable-threads=posix --enable-libgomp --target=x86_64-w64-mingw32 --with-sysroot=/usr/x86_64-w64-mingw32/sys-root --with-gxx-include-dir=/usr/x86_64-w64-mingw32/sys-root/mingw/include/c++
Thread model: posix
gcc version 8.3.0 20190222 (Fedora MinGW 8.3.0-1.fc29) (GCC) 
COLLECT_GCC_OPTIONS='-c' '-fno-openmp' '-fno-openacc' '-fpic' '-mtune=generic' '-march=x86-64' '-O2' '-B' './' '-v' '-O2' '-static' '-mtune=generic' '-march=x86-64' '-fltrans-output-list=/tmp/ccT5HKcq.ltrans.out' '-fwpa' '-fresolution=/tmp/ccg7K7YU.res' '-flinker-output=exec'
 /usr/libexec/gcc/x86_64-w64-mingw32/8.3.0/lto1 -quiet -dumpbase ccYNIbg4.o -mtune=generic -march=x86-64 -mtune=generic -march=x86-64 -auxbase ccYNIbg4 -O2 -O2 -version -fno-openmp -fno-openacc -fpic -fltrans-output-list=/tmp/ccT5HKcq.ltrans.out -fwpa -fresolution=/tmp/ccg7K7YU.res -flinker-output=exec @/tmp/ccC2SV8p
GNU GIMPLE (GCC) version 8.3.0 20190222 (Fedora MinGW 8.3.0-1.fc29) (x86_64-w64-mingw32)
	compiled by GNU C version 8.2.1 20181215 (Red Hat 8.2.1-6), GMP version 6.1.2, MPFR version 3.1.6-p2, MPC version 1.1.0, isl version isl-0.16.1-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
GNU GIMPLE (GCC) version 8.3.0 20190222 (Fedora MinGW 8.3.0-1.fc29) (x86_64-w64-mingw32)
	compiled by GNU C version 8.2.1 20181215 (Red Hat 8.2.1-6), GMP version 6.1.2, MPFR version 3.1.6-p2, MPC version 1.1.0, isl version isl-0.16.1-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
COMPILER_PATH=./:./:/usr/libexec/gcc/x86_64-w64-mingw32/8.3.0/:/usr/libexec/gcc/x86_64-w64-mingw32/8.3.0/:/usr/libexec/gcc/x86_64-w64-mingw32/:/usr/lib/gcc/x86_64-w64-mingw32/8.3.0/:/usr/lib/gcc/x86_64-w64-mingw32/:/usr/lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/:/usr/libexec/gcc/x86_64-w64-mingw32/8.3.0/:/usr/libexec/gcc/x86_64-w64-mingw32/8.3.0/:/usr/libexec/gcc/x86_64-w64-mingw32/:/usr/lib/gcc/x86_64-w64-mingw32/8.3.0/:/usr/lib/gcc/x86_64-w64-mingw32/:/usr/lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/
LIBRARY_PATH=./:/usr/lib/gcc/x86_64-w64-mingw32/8.3.0/:/usr/lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/lib/../lib/:/usr/x86_64-w64-mingw32/sys-root/mingw/lib/../lib/:/usr/lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/lib/:/usr/x86_64-w64-mingw32/sys-root/mingw/lib/
COLLECT_GCC_OPTIONS='-c' '-fno-openmp' '-fno-openacc' '-fpic' '-mtune=generic' '-march=x86-64' '-O2' '-B' './' '-v' '-O2' '-static' '-mtune=generic' '-march=x86-64' '-fltrans-output-list=/tmp/ccT5HKcq.ltrans.out' '-fwpa' '-fresolution=/tmp/ccg7K7YU.res' '-flinker-output=exec'
x86_64-w64-mingw32-g++ @/tmp/ccuzoexZ
Using built-in specs.
COLLECT_GCC=x86_64-w64-mingw32-g++
Target: x86_64-w64-mingw32
Configured with: ../configure --prefix=/usr --bindir=/usr/bin --includedir=/usr/include --mandir=/usr/share/man --infodir=/usr/share/info --datadir=/usr/share --build=x86_64-redhat-linux-gnu --host=x86_64-redhat-linux-gnu --with-gnu-as --with-gnu-ld --verbose --without-newlib --disable-multilib --disable-plugin --with-system-zlib --disable-nls --without-included-gettext --disable-win32-registry --enable-languages=c,c++,objc,obj-c++,fortran --with-bugurl=http://bugzilla.redhat.com/bugzilla --with-cloog --enable-threads=posix --enable-libgomp --target=x86_64-w64-mingw32 --with-sysroot=/usr/x86_64-w64-mingw32/sys-root --with-gxx-include-dir=/usr/x86_64-w64-mingw32/sys-root/mingw/include/c++
Thread model: posix
gcc version 8.3.0 20190222 (Fedora MinGW 8.3.0-1.fc29) (GCC) 
COLLECT_GCC_OPTIONS='-c' '-fno-openmp' '-fno-openacc' '-fpic' '-mtune=generic' '-march=x86-64' '-O2' '-B' './' '-v' '-O2' '-static' '-mtune=generic' '-march=x86-64' '-fltrans-output-list=/tmp/ccT5HKcq.ltrans.out' '-fltrans' '-o' '/tmp/ccT5HKcq.ltrans0.ltrans.o'
 /usr/libexec/gcc/x86_64-w64-mingw32/8.3.0/lto1 -quiet -dumpbase ccT5HKcq.ltrans0.o -mtune=generic -march=x86-64 -mtune=generic -march=x86-64 -auxbase-strip /tmp/ccT5HKcq.ltrans0.ltrans.o -O2 -O2 -version -fno-openmp -fno-openacc -fpic -fltrans-output-list=/tmp/ccT5HKcq.ltrans.out -fltrans @/tmp/cc0ZRpTq -o /tmp/cclMLM0h.s
GNU GIMPLE (GCC) version 8.3.0 20190222 (Fedora MinGW 8.3.0-1.fc29) (x86_64-w64-mingw32)
	compiled by GNU C version 8.2.1 20181215 (Red Hat 8.2.1-6), GMP version 6.1.2, MPFR version 3.1.6-p2, MPC version 1.1.0, isl version isl-0.16.1-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
GNU GIMPLE (GCC) version 8.3.0 20190222 (Fedora MinGW 8.3.0-1.fc29) (x86_64-w64-mingw32)
	compiled by GNU C version 8.2.1 20181215 (Red Hat 8.2.1-6), GMP version 6.1.2, MPFR version 3.1.6-p2, MPC version 1.1.0, isl version isl-0.16.1-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
COLLECT_GCC_OPTIONS='-c' '-fno-openmp' '-fno-openacc' '-fpic' '-mtune=generic' '-march=x86-64' '-O2' '-B' './' '-v' '-O2' '-static' '-mtune=generic' '-march=x86-64' '-fltrans-output-list=/tmp/ccT5HKcq.ltrans.out' '-fltrans' '-o' '/tmp/ccT5HKcq.ltrans0.ltrans.o'
 /usr/lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/as -v -o /tmp/ccT5HKcq.ltrans0.ltrans.o /tmp/cclMLM0h.s
GNU assembler version 2.30 (x86_64-w64-mingw32) using BFD version (GNU Binutils) 2.30
COMPILER_PATH=./:./:/usr/libexec/gcc/x86_64-w64-mingw32/8.3.0/:/usr/libexec/gcc/x86_64-w64-mingw32/8.3.0/:/usr/libexec/gcc/x86_64-w64-mingw32/:/usr/lib/gcc/x86_64-w64-mingw32/8.3.0/:/usr/lib/gcc/x86_64-w64-mingw32/:/usr/lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/:/usr/libexec/gcc/x86_64-w64-mingw32/8.3.0/:/usr/libexec/gcc/x86_64-w64-mingw32/8.3.0/:/usr/libexec/gcc/x86_64-w64-mingw32/:/usr/lib/gcc/x86_64-w64-mingw32/8.3.0/:/usr/lib/gcc/x86_64-w64-mingw32/:/usr/lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/
LIBRARY_PATH=./:/usr/lib/gcc/x86_64-w64-mingw32/8.3.0/:/usr/lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/lib/../lib/:/usr/x86_64-w64-mingw32/sys-root/mingw/lib/../lib/:/usr/lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/lib/:/usr/x86_64-w64-mingw32/sys-root/mingw/lib/
COLLECT_GCC_OPTIONS='-c' '-fno-openmp' '-fno-openacc' '-fpic' '-mtune=generic' '-march=x86-64' '-O2' '-B' './' '-v' '-O2' '-static' '-mtune=generic' '-march=x86-64' '-fltrans-output-list=/tmp/ccT5HKcq.ltrans.out' '-fltrans' '-o' '/tmp/ccT5HKcq.ltrans0.ltrans.o'
COLLECT_GCC_OPTIONS='-B' './' '-v' '-flto' '-O2' '-static' '-mtune=generic' '-march=x86-64'
[hjl@gnu-cfl-2 pr24267]$
Comment 23 Martin Liška 2019-03-12 09:04:38 UTC
(In reply to H.J. Lu from comment #22)
> Works for me with binutils 2.32:
> 

Have you tried to run the binary with wine?
You'll probably see something like:
wine: Unhandled page fault on read access to 0x00000000 at address 0x4a39fe (thread 002a), starting debugger...

Issue is that the linker claims that all is fine, but final binary is missing the symbol.
Comment 24 Frediano Ziglio 2019-03-12 10:45:14 UTC
Just update binutils on Fedora 29 to 2.32 and compiled the small program at https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81879, still crash,

wine: Unhandled page fault on execute access to 0x00000000 at address (nil) (thread 002a), starting debugger...
Comment 25 H.J. Lu 2019-03-14 21:34:07 UTC
Created attachment 11681 [details]
A patch

Please try this.
Comment 26 Martin Liška 2019-03-15 08:32:33 UTC
(In reply to H.J. Lu from comment #25)
> Created attachment 11681 [details]
> A patch
> 
> Please try this.

Good job H.J. I can confirm it works for a simple test-case and I see:
737 54db81cc670131ad PREVAILING_DEF _ZNKSt5ctypeIcE8do_widenEc
740 54db81cc670131ad PREVAILING_DEF main

which is good. I'm going to ask Godot engine guy who use it for much bigger code base.
Comment 27 Frediano Ziglio 2019-03-15 09:47:39 UTC
I compiled 2.32 version with the patch provided under Fedora 29 and the initial issue disappeared.

Thanks.
Comment 28 Sourceware Commits 2019-03-15 14:20:35 UTC
The master branch has been updated by H.J. Lu <hjl@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=2219ae0b0ebe14373850b000c2abaa31dab1d741

commit 2219ae0b0ebe14373850b000c2abaa31dab1d741
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Fri Mar 15 22:19:01 2019 +0800

    COFF: Check for symbols defined in discarded section
    
    For LTO, a symbol may defined in discarded section.  We should mark it
    as undefined so that LTO plugin will make IR definition available.
    
    	PR ld/24267
    	* coffgen.c (_bfd_coff_section_already_linked): Skip discarded
    	section.
    	* cofflink.c (coff_link_add_symbols): Check for symbols defined
    	in discarded section.
Comment 29 Frediano Ziglio 2019-03-15 14:42:42 UTC
Wonderful!

Some question:
- why not adding a test based on the code from Martin?
- is this fix not similar to a previous issue (PR 23958, PR 23818, PR 23309, PR 22983, PR 22751, PR 22502, PR 22220, PR 21382, PR 18250, PR 20267, PR 20321)?
Comment 30 H.J. Lu 2019-03-15 23:06:18 UTC
(In reply to Frediano Ziglio from comment #29)
> Wonderful!
> 
> Some question:
> - why not adding a test based on the code from Martin?

Please feel free to submit a patch.

> - is this fix not similar to a previous issue (PR 23958, PR 23818, PR 23309,
> PR 22983, PR 22751, PR 22502, PR 22220, PR 21382, PR 18250, PR 20267, PR
> 20321)?

This was fixed for ELF by

commit 529fcb958524a45f737111d1a42787fdee4d7c66
Author: Paul Brook <paul@codesourcery.com>
Date:   Wed Dec 1 23:23:12 2004 +0000

    2004-12-01  Paul Brook  <paul@codesourcery.com>
    
    bfd/
            * elflink.c (elf_link_add_object_symbols): Make symbols from discarded
            sections undefined.
    ld/testsuite/
            * ld-elf/group1.d: New test.
            * ld-elf/group.ld, ld-elf/group1a.s, ld-elf/group1b.s: New test.