Bug 1730906

Summary: ld -r fails to handle .init_array section in comdat group
Product: Red Hat Enterprise Linux 8 Reporter: H.J. Lu <hongjiu.lu>
Component: binutilsAssignee: Nick Clifton <nickc>
binutils sub component: system-version QA Contact: Miloš Prchlík <mprchlik>
Status: CLOSED ERRATA Docs Contact:
Severity: unspecified    
Priority: unspecified CC: fweimer, law, mprchlik, ohudlick
Version: 8.2   
Target Milestone: rc   
Target Release: 8.0   
Hardware: Unspecified   
OS: Unspecified   
Whiteboard:
Fixed In Version: binutils-2.30-59.el8 Doc Type: No Doc Update
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2020-04-28 16:45:36 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:
Bug Depends On:    
Bug Blocks: 1746913, 1755139    
Attachments:
Description Flags
Proposed patch none

Description H.J. Lu 2019-07-17 20:25:41 UTC
[hjl@gnu-cfl-1 reloc-3]$ cat 1.s
	.text
	.section	.text.startup.foo1,"axG",@progbits,.group1,comdat
	.p2align 4,,15
	.type	foo1, @function
foo1:
.LFB0:
	.cfi_startproc
	movl	$30, init1(%rip)
	ret
	.cfi_endproc
.LFE0:
	.size	foo1, .-foo1
	.section	.init_array,"awG",@init_array,.group1,comdat
	.align 8
	.quad	foo1
[hjl@gnu-cfl-1 reloc-3]$ cat i.s
	.text
	.section	.text.startup.foo1,"axG",@progbits,.group1,comdat
	.p2align 4,,15
	.type	foo1, @function
foo1:
.LFB0:
	.cfi_startproc
	movl	$1, init1(%rip)
	ret
	.cfi_endproc
.LFE0:
	.size	foo1, .-foo1
	.section	.init_array,"awG",@init_array,.group1,comdat
	.align 8
	.quad	foo1
	.section	.text.startup.foo2,"axG",@progbits,.group2,comdat
	.p2align 4,,15
	.type	foo2, @function
foo2:
.LFB1:
	.cfi_startproc
	movl	$2, init2(%rip)
	ret
	.cfi_endproc
.LFE1:
	.size	foo2, .-foo2
	.section	.init_array,"awG",@init_array,.group2,comdat
	.align 8
	.quad	foo2
	.section	.text.startup.foo3,"ax",@progbits
	.p2align 4,,15
	.type	foo3, @function
foo3:
.LFB2:
	.cfi_startproc
	movl	$3, init3(%rip)
	ret
	.cfi_endproc
.LFE2:
	.size	foo3, .-foo3
	.section	.init_array
	.align 8
	.quad	foo3
	.section	.note.GNU-stack,"",@progbits
[hjl@gnu-cfl-1 reloc-3]$ cat m.c
#include <stdio.h>

int init1;
int init2;
int init3;

int
main (void)
{
  printf ("init1: %d\n", init1);
  printf ("init2: %d\n", init2);
  printf ("init3: %d\n", init3);
  if (init1 == 30 && init2 == 2 && init3 == 3)
    {
      printf ("PASS\n");
      return 0;
    }
  else
    {
      printf ("FAIL\n");
      return 1;
    }
}
[hjl@gnu-cfl-1 reloc-3]$ make
as  -o 1.o 1.s
as  -o i.o i.s
cc -O2    -c -o m.o m.c
cc -O2  -o x 1.o i.o m.o
./ld -r -o i-r.o i.o
cc -O2  -o y 1.o i-r.o m.o
./x
init1: 30
init2: 2
init3: 3
PASS
./y
init1: 30
init2: 2
init3: 0
FAIL
make: *** [Makefile:8: all] Error 1
[hjl@gnu-cfl-1 reloc-3]$ 

The problem is

[hjl@gnu-cfl-1 reloc-3]$ readelf -rW i-r.o
...
Relocation section '.rela.init_array' at offset 0x430 contains 2 entries:
    Offset             Info             Type               Symbol's Value  Symbol's Name + Addend
0000000000000000  0000000f00000001 R_X86_64_64            0000000000000000 foo1 + 0
0000000000000008  0000000700000001 R_X86_64_64            0000000000000000 .text.startup.foo3 + 0

where

	.section	.init_array
	.align 8
	.quad	foo3

is merged into

	.section	.init_array,"awG",@init_array,.group1,comdat
	.align 8
	.quad	foo1

When .group1 group in i-r.o is discarded, foo3 isn't called.

A patch is posted at

https://sourceware.org/ml/binutils/2019-07/msg00169.html

Comment 1 Nick Clifton 2019-07-22 13:21:31 UTC
Created attachment 1592601 [details]
Proposed patch

Hi H.J.

  Thanks for raising this bug, and providing a patch.

  I have updated Fedora rawhide (binutils-2.32-18), and this attachment
  is the patch for RHEL-8.2 once that opens.

Cheers
  Nick

Comment 7 Nick Clifton 2019-10-28 14:06:55 UTC
Fixed in binutils-2.30-59.el8

Comment 9 Miloš Prchlík 2020-03-16 13:29:13 UTC
Verified with binutils-2.30-73.el8.

[root@opicee-1 ~]# ./x
init1: 30
init2: 2
init3: 3
PASS
[root@opicee-1 ~]# ./y
init1: 30
init2: 2
init3: 3
PASS
[root@opicee-1 ~]#

Comment 11 errata-xmlrpc 2020-04-28 16:45:36 UTC
Since the problem described in this bug report should be
resolved in a recent advisory, it has been closed with a
resolution of ERRATA.

For information on the advisory, and where to find the updated
files, follow the link below.

If the solution does not work for you, open a new bug report.

https://access.redhat.com/errata/RHSA-2020:1797