Bug 17666

Summary: ld -r may create reloc sections with unordered relocs
Product: binutils Reporter: Alan Modra <amodra>
Component: ldAssignee: Alan Modra <amodra>
Status: RESOLVED FIXED    
Severity: normal CC: bruno
Priority: P2    
Version: unspecified   
Target Milestone: 2.26   
Host: Target:
Build: Last reconfirmed:
Bug Depends on:    
Bug Blocks: 18867    

Description Alan Modra 2014-12-01 10:21:42 UTC
cat > text1.s <<EOF
 .section ".text1","ax",%progbits
.L1:
 .dc.a .L2

 .data
.L2:
 .dc.a .L1
EOF
cat > text.s <<EOF
 .text
.L1:
 .dc.a .L2

 .data
.L2:
 .dc.a .L1
EOF
as -o text1.o text1.s
as -o text.o text.s
# remove empty sections
objcopy -R .text -R .bss text1.o
objcopy -R .bss text.o
ld -r -o out.o text1.o text.o
objdump -r -j.data out.o

shows .data relocs out of order.

This bug is caused by elflink.c processing text1.o first due to this loop:
  for (o = abfd->sections; o != NULL; o = o->next)
    {
      for (p = o->map_head.link_order; p != NULL; p = p->next)
	{
	  if (p->type == bfd_indirect_link_order
	      && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
		  == bfd_target_elf_flavour)
	      && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
	    {
	      if (! sub->output_has_begun)
		{
		  if (! elf_link_input_bfd (&flinfo, sub))
		    goto error_return;
		  sub->output_has_begun = TRUE;
		}
and the fact that reloc sections are not placed by the generic linker.
Comment 1 cvs-commit@gcc.gnu.org 2014-12-04 07:23:18 UTC
This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "gdb and binutils".

The branch, master has been updated
       via  53df40a43c968f4d97754226d62775d1fe665459 (commit)
      from  621661e3faf809d4f28bb84e94e30c1c8a8cc933 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=53df40a43c968f4d97754226d62775d1fe665459

commit 53df40a43c968f4d97754226d62775d1fe665459
Author: Alan Modra <amodra@gmail.com>
Date:   Wed Dec 3 22:00:18 2014 +1030

    Sort relocs output by ld -r
    
    bfd/
    	PR 17666
    	* elflink.c: Include bfd_stdint.h.
    	(cmp_ext32l_r_offset, cmp_ext32b_r_offset,
    	cmp_ext64l_r_offset, cmp_ext64b_r_offset): New functions.
    	(elf_link_adjust_relocs): Sort relocs.  Free reloc hashes after
    	sorting invalidates.
    ld/testsuite/
    	* ld-powerpc/vxworks-relax.rd: Update for reloc sorting.
    	* ld-powerpc/vxworks-relax-2.rd: Likewise.
    	* ld-sh/sh64/reldl32.rd: Likewise.
    	* ld-sh/sh64/reldl64.rd: Likewise.

-----------------------------------------------------------------------

Summary of changes:
 bfd/ChangeLog                              |    9 ++
 bfd/elflink.c                              |  158 ++++++++++++++++++++++++++++
 ld/testsuite/ChangeLog                     |    7 ++
 ld/testsuite/ld-powerpc/vxworks-relax-2.rd |    4 +-
 ld/testsuite/ld-powerpc/vxworks-relax.rd   |    4 +-
 ld/testsuite/ld-sh/sh64/reldl32.rd         |    4 +-
 ld/testsuite/ld-sh/sh64/reldl64.rd         |    4 +-
 7 files changed, 182 insertions(+), 8 deletions(-)
Comment 2 Alan Modra 2014-12-04 07:24:10 UTC
fixed
Comment 3 cvs-commit@gcc.gnu.org 2014-12-09 23:29:06 UTC
This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "gdb and binutils".

The branch, master has been updated
       via  28dbcedc7b1282b93c3f25c4e42ae71b44e8e2a8 (commit)
      from  4e3272393ff1ab2c07d9fa209d2ae82954f7954c (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=28dbcedc7b1282b93c3f25c4e42ae71b44e8e2a8

commit 28dbcedc7b1282b93c3f25c4e42ae71b44e8e2a8
Author: Alan Modra <amodra@gmail.com>
Date:   Tue Dec 9 14:32:22 2014 +1030

    Don't sort ld -r relocs for mips
    
    HI16/LO16 are deliberately put adjacent, which might mean relocs are
    then not sorted by r_offset.  See tc-mips.c:mips_frob_file.  Don't undo
    the HI16/LO16 sorting.
    
    	PR 17666
    	* elf-bfd.h (struct elf_backend_data): Add sort_relocs_p.
    	* elfxx-target.h (elf_backend_sort_relocs_p): Define.
    	(elfNN_bed): Init new field.
    	* elflink.c (elf_link_adjust_relocs): Conditionally sort.
    	(bfd_elf_final_link): Control sorting of relocs.
    	* elfxx-mips.c (_bfd_mips_elf_sort_relocs_p): New function.
    	* elfxx-mips.h (_bfd_mips_elf_sort_relocs_p): Declare.
    	* elf32-mips.c (elf_backend_sort_relocs_p): Define.
    	* elf64-mips.c (elf_backend_sort_relocs_p): Define.

-----------------------------------------------------------------------

Summary of changes:
 bfd/ChangeLog      |   13 +++++++++++++
 bfd/elf-bfd.h      |    5 +++++
 bfd/elf32-mips.c   |    2 ++
 bfd/elf64-mips.c   |    1 +
 bfd/elflink.c      |   51 +++++++++++++++++++++++++++++----------------------
 bfd/elfxx-mips.c   |   12 ++++++++++++
 bfd/elfxx-mips.h   |    2 ++
 bfd/elfxx-target.h |    4 ++++
 8 files changed, 68 insertions(+), 22 deletions(-)
Comment 4 cvs-commit@gcc.gnu.org 2015-02-11 12:47:54 UTC
The binutils-2_25-branch branch has been updated by Alan Modra <amodra@sourceware.org>:

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

commit ccfa71befe7824964304d73e35f4e68a3f06306c
Author: Alan Modra <amodra@gmail.com>
Date:   Wed Dec 3 22:00:18 2014 +1030

    Sort relocs output by ld -r
    
    bfd/
    	PR 17666
    	* elflink.c: Include bfd_stdint.h.
    	(cmp_ext32l_r_offset, cmp_ext32b_r_offset,
    	cmp_ext64l_r_offset, cmp_ext64b_r_offset): New functions.
    	(elf_link_adjust_relocs): Sort relocs.  Free reloc hashes after
    	sorting invalidates.
    ld/testsuite/
    	* ld-powerpc/vxworks-relax.rd: Update for reloc sorting.
    	* ld-powerpc/vxworks-relax-2.rd: Likewise.
    	* ld-sh/sh64/reldl32.rd: Likewise.
    	* ld-sh/sh64/reldl64.rd: Likewise.
Comment 5 cvs-commit@gcc.gnu.org 2015-02-11 12:48:28 UTC
The binutils-2_25-branch branch has been updated by Alan Modra <amodra@sourceware.org>:

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

commit 90e052f660418098b65f2b2245712ead75288d27
Author: Alan Modra <amodra@gmail.com>
Date:   Tue Dec 9 14:32:22 2014 +1030

    Don't sort ld -r relocs for mips
    
    HI16/LO16 are deliberately put adjacent, which might mean relocs are
    then not sorted by r_offset.  See tc-mips.c:mips_frob_file.  Don't undo
    the HI16/LO16 sorting.
    
    	PR 17666
    	* elf-bfd.h (struct elf_backend_data): Add sort_relocs_p.
    	* elfxx-target.h (elf_backend_sort_relocs_p): Define.
    	(elfNN_bed): Init new field.
    	* elflink.c (elf_link_adjust_relocs): Conditionally sort.
    	(bfd_elf_final_link): Control sorting of relocs.
    	* elfxx-mips.c (_bfd_mips_elf_sort_relocs_p): New function.
    	* elfxx-mips.h (_bfd_mips_elf_sort_relocs_p): Declare.
    	* elf32-mips.c (elf_backend_sort_relocs_p): Define.
    	* elf64-mips.c (elf_backend_sort_relocs_p): Define.
Comment 6 Bruno De Fraine 2016-08-30 07:47:00 UTC
A symptom of unsorted relocators after partial linking, is the following message at final linking:

/usr/bin/ld: error in qq.o(.eh_frame); no .eh_frame_hdr table will be created.

Also see bug #16345. This fix seems to solve the problem from that report.