[PATCH] PE-COFF: Fix link failure of C++ code with debug info after partial linking

Eric Botcazou botcazou@adacore.com
Wed Mar 25 15:56:26 GMT 2026


Hi,

if you apply the following recipe to the attached C/C++ files with a PE-COFF 
toolchain, you get the specified output:

1. g++ -c clib.cpp cpplib.cpp test.c -g
2. g++ -o pl.o clib.o cpplib.o -nostdlib -Wl,-r
3. objcopy pl.o
objcopy.exe: pl.o: warning: COMDAT symbol 
'.debug_frame$_ZNSt12_Vector_baseIiSaIiEE12_Vector_implD1Ev' does not match 
section name '.debug_frame'
4. g++ -o test test.o pl.o
ld.exe: pl.o: warning: COMDAT symbol 
'.debug_frame$_ZNSt12_Vector_baseIiSaIiEE12_Vector_implD1Ev' does not match 
section name '.debug_frame'
pl.o:clib.cpp:(.pdata$_ZNSt12_Vector_baseIiSaIiEE13_M_deallocateEPiy+0x0): 
relocation truncated to fit: IMAGE_REL_AMD64_ADDR32NB against 
`.text$_ZNSt12_Vector_baseIiSaIiEE13_M_deallocateEPiy'
pl.o:clib.cpp:(.pdata$_ZNSt12_Vector_baseIiSaIiEE13_M_deallocateEPiy+0x4): 
relocation truncated to fit: IMAGE_REL_AMD64_ADDR32NB against 
`.text$_ZNSt12_Vector_baseIiSaIiEE13_M_deallocateEPiy'
pl.o:clib.cpp:(.pdata$_ZNSt12_Vector_baseIiSaIiEE19_M_get_Tp_allocatorEv+0x0): 
relocation truncated to fit: IMAGE_REL_AMD64_ADDR32NB against 
`.text$_ZNSt12_Vector_baseIiSaIiEE19_M_get_Tp_allocatorEv'
pl.o:clib.cpp:(.pdata$_ZNSt12_Vector_baseIiSaIiEE19_M_get_Tp_allocatorEv+0x4): 
relocation truncated to fit: IMAGE_REL_AMD64_ADDR32NB against 
`.text$_ZNSt12_Vector_baseIiSaIiEE19_M_get_Tp_allocatorEv'
pl.o:clib.cpp:(.pdata$_ZNSt15__new_allocatorIiE10deallocateEPiy+0x0): 
relocation truncated to fit: IMAGE_REL_AMD64_ADDR32NB against 
`.text$_ZNSt15__new_allocatorIiE10deallocateEPiy'
pl.o:clib.cpp:(.pdata$_ZNSt15__new_allocatorIiE10deallocateEPiy+0x4): 
relocation truncated to fit: IMAGE_REL_AMD64_ADDR32NB against 
`.text$_ZNSt15__new_allocatorIiE10deallocateEPiy'
collect2.exe: error: ld returned 1 exit status

The problem pertains to section symbols generated for COMDAT sections: they
are marked as local symbols as per Microsoft's PE-COFF specification, but
partial linking discards the duplicate COMDAT sections without being able
to either merge them, or remove them when they are used in a relocation.

So they end up as undefined local symbols after the partial link, which in
turn may cause the final link to fail (in practice you need e.g. a call to
objcopy in between, because it moves them to the end of the symbol list).

This change instructs the linker to "relocate" them instead, that is to say
to attach them to the one COMDAT section that is output among the multiple
COMDAT sections that are duplicate.  It also prevents partial linking from
prematurely globing the .[z]debug_frame* sections together, as already done
for the .eh_frame* sections.

Tested on x86_64-w64-mingw32, OK for the mainline?


2026-03-25  Eric Botcazou  <ebotcazou@adacore.com>

bfd/
	* cofflink.c (_bfd_coff_link_input_bfd): For a relocatable output,
	relocate section symbols for input sections that are not going to
	be emitted because they are duplicate of another one in the link.
ld/
	* scripttempl/pe.sc (.debug_frame): Do not glob all .debug_frame*
	sections together when not relocating.
	(.zdebug_frame): Likewise for .zdebug_frame* sections.
	* scripttempl/pep.sc (.debug_frame): Likewise.
	(.zdebug_frame): Likewise.

-- 
Eric Botcazou
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test.c
Type: text/x-csrc
Size: 62 bytes
Desc: not available
URL: <https://sourceware.org/pipermail/binutils/attachments/20260325/a2a50aea/attachment.bin>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: clib.h
Type: text/x-chdr
Size: 127 bytes
Desc: not available
URL: <https://sourceware.org/pipermail/binutils/attachments/20260325/a2a50aea/attachment-0001.bin>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: cpplib.h
Type: text/x-chdr
Size: 116 bytes
Desc: not available
URL: <https://sourceware.org/pipermail/binutils/attachments/20260325/a2a50aea/attachment-0002.bin>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: clib.cpp
Type: text/x-c++src
Size: 132 bytes
Desc: not available
URL: <https://sourceware.org/pipermail/binutils/attachments/20260325/a2a50aea/attachment-0003.bin>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: cpplib.cpp
Type: text/x-c++src
Size: 83 bytes
Desc: not available
URL: <https://sourceware.org/pipermail/binutils/attachments/20260325/a2a50aea/attachment-0004.bin>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: p.diff
Type: text/x-patch
Size: 2432 bytes
Desc: not available
URL: <https://sourceware.org/pipermail/binutils/attachments/20260325/a2a50aea/attachment-0005.bin>


More information about the Binutils mailing list