This is the mail archive of the
binutils@sourceware.org
mailing list for the binutils project.
Re: [PATCH] Fix -flto on mingw (PR ld/18199)
- From: "H.J. Lu" <hjl dot tools at gmail dot com>
- To: Kwok Cheung Yeung <kcy at codesourcery dot com>
- Cc: Binutils <binutils at sourceware dot org>
- Date: Wed, 9 Dec 2015 19:47:40 -0800
- Subject: Re: [PATCH] Fix -flto on mingw (PR ld/18199)
- Authentication-results: sourceware.org; auth=none
- References: <5668C2A8 dot 1020405 at codesourcery dot com>
On Wed, Dec 9, 2015 at 4:09 PM, Kwok Cheung Yeung <kcy@codesourcery.com> wrote:
> As noted in PR65581 on the GCC bug tracker, on i686-mingw32, compiling a
> program in GCC with -flto results in this type of failure:
>
> $ gcc gcc/testsuite/gcc.c-torture/execute/20000113-1.c
> -fno-diagnostics-show-caret -fdiagnostics-color=never -O2 -flto
> -flto-partition=none -w -DNO_TRAMPOLINES -lm -o ./20000113-1.exe
> /tmp/ccSMtMEn.lto.o:<artificial>:(.text.startup+0x0): multiple definition of
> `main'
> /opt/codesourcery/bin/../lib/gcc/i686-mingw32/5.2.0/../../../../i686-mingw32/lib/libmingw32.a(main.o):main.c:(.text.startup+0x0):
> first defined here
> /opt/codesourcery/bin/../lib/gcc/i686-mingw32/5.2.0/../../../../i686-mingw32/lib/libmingw32.a(main.o):main.c:(.text.startup+0xa7):
> undefined reference to `WinMain@16'
> collect2: error: ld returned 1 exit status
>
> By bisection, I found that the issue started with commit
> 5ae0078cd2b6b69e6119864e20987c8724916b29 (Merge linker plugin handling into
> BFD plugin support). The problem seems to be this change:
>
> @@ -295,14 +298,19 @@ plugin_get_ir_dummy_bfd (const char *name, bfd
> *srctemplate)
>
> bfd_use_reserved_id = 1;
> abfd = bfd_create (concat (name, IRONLY_SUFFIX, (const char *) NULL),
> - srctemplate);
> + link_info.output_bfd);
> if (abfd != NULL)
> {
> abfd->flags |= BFD_LINKER_CREATED | BFD_PLUGIN;
> - bfd_set_arch_info (abfd, bfd_get_arch_info (srctemplate));
> - bfd_set_gp_size (abfd, bfd_get_gp_size (srctemplate));
> - if (bfd_make_writable (abfd)
> - && bfd_copy_private_bfd_data (srctemplate, abfd))
> + if (!bfd_make_writable (abfd))
> + goto report_error;
> + if (! bfd_plugin_target_p (srctemplate->xvec))
> + {
> + bfd_set_arch_info (abfd, bfd_get_arch_info (srctemplate));
> + bfd_set_gp_size (abfd, bfd_get_gp_size (srctemplate));
> + if (!bfd_copy_private_bfd_data (srctemplate, abfd))
> + goto report_error;
> + }
> {
> flagword flags;
>
> I believe the intent of this change is for things to proceed as they were
> before patching if bfd_plugin_target_p (srctemplate->xvec) returns false?
> However, since abfd was created using link_info.output_bfd as a template,
> abfd->xvec is set to link_info.output_bfd->xvec rather than
> srctemplate->xvec as before. On most targets, the input and output file
> formats will be the same so this does not really matter, but on Windows
> these can be different (i386_pe_vec/x86_64_pe_vec versus
> i386_pei_vec/x86_64_pei_vec), and that seems to be causing the problem here.
> If abfd->xvec is set to srctemplate->xvec when bfd_plugin_target_p returns
> false, the -flto problem no longer appears.
>
> Okay to commit on trunk?
>
> Kwok Cheung Yeung
>
> 2015-12-10 Kwok Cheung Yeung <kcy@codesourcery.com>
>
> PR ld/18199
> * plugin.c (plugin_get_ir_dummy_bfd): Set target vector from
> srctemplate if it is not the BFD plugin target vector.
>
> diff --git a/ld/plugin.c b/ld/plugin.c
> index 8e53255..1f1b2b1 100644
> --- a/ld/plugin.c
> +++ b/ld/plugin.c
> @@ -306,6 +306,7 @@ plugin_get_ir_dummy_bfd (const char *name, bfd
> *srctemplate)
> goto report_error;
> if (! bfd_plugin_target_p (srctemplate->xvec))
> {
> + abfd->xvec = srctemplate->xvec;
> bfd_set_arch_info (abfd, bfd_get_arch_info (srctemplate));
> bfd_set_gp_size (abfd, bfd_get_gp_size (srctemplate));
> if (!bfd_copy_private_bfd_data (srctemplate, abfd))
How about this patch instead?
--
H.J.
diff --git a/ld/plugin.c b/ld/plugin.c
index 8e53255..9725c2b 100644
--- a/ld/plugin.c
+++ b/ld/plugin.c
@@ -298,7 +298,8 @@ plugin_get_ir_dummy_bfd (const char *name, bfd *srctemplate)
bfd_use_reserved_id = 1;
abfd = bfd_create (concat (name, IRONLY_SUFFIX, (const char *) NULL),
- link_info.output_bfd);
+ (bfd_plugin_target_p (srctemplate->xvec)
+ ? link_info.output_bfd : srctemplate));
if (abfd != NULL)
{
abfd->flags |= BFD_LINKER_CREATED | BFD_PLUGIN;