Bug 15041 - ld -r bugs for pe format (mingw)
Summary: ld -r bugs for pe format (mingw)
Alias: None
Product: binutils
Classification: Unclassified
Component: ld (show other bugs)
Version: unspecified
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
Depends on:
Reported: 2013-01-19 19:19 UTC by martin.koegler
Modified: 2017-03-10 02:38 UTC (History)
3 users (show)

See Also:
Last reconfirmed:

GCC generated assembler code for easier reproduction (1.02 KB, text/plain)
2013-01-19 19:21 UTC, martin.koegler
Fix a number of ld -r script problems (823 bytes, patch)
2017-02-20 07:47 UTC, Alan Modra
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description martin.koegler 2013-01-19 19:19:31 UTC
Sample code t.cxx:
class __declspec(dllexport) test
  int x;
  static int y;
  void a() { x = y + 2; }
x86_64-w64-mingw32-gcc -c -o t.o t.cxx
x86_64-w64-mingw32-ld -r -o t1.o t.o t.o
x86_64-w64-mingw32-ld -r -o t2.o t1.o t.o

yields to the warnings:
x86_64-w64-mingw32-ld: t1.o: warning: COMDAT symbol '.pdata$_ZN4test1aEv' does not match section name '.pdata'
x86_64-w64-mingw32-ld: t1.o: warning: COMDAT symbol '.xdata$_ZN4test1aEv' does not match section name '.xdata'

It is caused by the fact, that the PE linker scripts of all recent binutils add the .pdata/.xdata with (.pdata*) / (.xdata*). This way, the sections get merged into the .pdata/.xdata section, which breaks the link once behaviour. Changing the linker script to only use (.pdata) / (.xdata) fixes the problem.

Similar problems can occur for other sections (eg. g++ is called with -g).
Comment 1 martin.koegler 2013-01-19 19:21:08 UTC
Created attachment 6828 [details]
GCC generated assembler code for easier reproduction
Comment 2 cvs-commit@gcc.gnu.org 2013-01-23 15:12:07 UTC
CVSROOT:	/cvs/src
Module name:	src
Changes by:	nickc@sourceware.org	2013-01-23 15:12:01

Modified files:
	ld             : ChangeLog 
	ld/scripttempl : pep.sc 

Log message:
	PR ld/15041
	* scripttempl/pep.sc (.pdata): Only accept .pdata sections.
	(.xdata): Similarly.
	(.debug_frame): Similarly.

Comment 3 Nick Clifton 2013-01-23 15:12:53 UTC
Hi Martin,

  I have applied your suggested fix.  Let me know if you have any more problems.

Comment 4 martin.koegler 2013-01-23 21:44:06 UTC
I have not mention it explicitly, but 32 bit pe is also affected:
i686-w64-mingw32-g++ -c -g -o t32.o t.cxx
i686-w64-mingw32-ld -r -o x t32.o t32.o
i686-w64-mingw32-ld -r -o y x t32.o
i686-w64-mingw32-ld: x: warning: COMDAT symbol '.debug_frame$_ZN4test1aEv' does not match section name '.debug_frame'
Comment 5 Alex 2017-02-16 21:01:07 UTC
This issue, and associated patch, was reverted a month after it was added in, by b138e9bffdf97f9c172c1138418c3670178d7c7b.

This change to *(.pdata*) and *(.xdata*) causes a large project I've been building lately to crash when an exception is thrown and caught, as the .pdata/.xdata sections seem to have missing and messed up symbols. These warnings are thrown during compilation, which is how I found this page.

Changing these back to *(.pdata) and *(.xdata) (as suggested in the patch for this bug) cause the exception handling to work correctly.

Why is matching with the * necessary? The above-mentioned commit does not specify the reasoning behind the change, and the commit against the pe.sc template is also vague (that was .pdata for a while, but recently, in December, was changed back to .pdata*).

Note that the changes a while back to add KEEP() around these symbols is still there, and seems to work fine.
Comment 6 Alan Modra 2017-02-16 22:51:37 UTC
Likely you want


and similarly for .xdata.
Comment 7 Alex 2017-02-17 19:16:35 UTC
Yes, that seems to have done the trick. I'm getting the impression that ld -r has not been put through its paces for pe targets. Do you want to make a patch for that, or shall I?
Comment 8 Alan Modra 2017-02-20 07:47:15 UTC
Created attachment 9839 [details]
Fix a number of ld -r script problems

Please test
Comment 9 cvs-commit@gcc.gnu.org 2017-02-20 13:45:17 UTC
The master branch has been updated by Alan Modra <amodra@sourceware.org>:


commit c48cfeddf730d181648182097dbb179dc82c5b58
Author: Alan Modra <amodra@gmail.com>
Date:   Mon Feb 20 13:24:28 2017 +1030

    PE ld -r script fixes
    	PR 15041
    	* scripttempl/pe.sc: Don't combine sections for ld -r.
    	* scripttempl/pep.sc: Likewise.
Comment 10 Alan Modra 2017-03-10 02:38:02 UTC