[4/4] x86-64/ELF: clear src_mask for all reloc types

Message ID 443cbfa1-c057-ec4e-37d0-0cbef41c1440@suse.com
State New
Headers show
Series
  • x86: assorted relocation handling related adjustments (part IV)
Related show

Commit Message

H.J. Lu via Binutils May 6, 2021, 6:08 a.m.
x86-64 uses rela relocations. The comment next to the field's declaration
says "Non-zero values for ELF USE_RELA targets should be viewed with
suspicion ..." And indeed the fields being non-zero causes section
contents to be accumulated into the final relocated values in addition to
the relocations' addends, which is contrary to the ELF spec.

bfd/
2021-05-XX  Jan Beulich  <jbeulich@suse.com>

	* elf64-x86-64.c (x86_64_elf_howto_table): Set src_mask fields
	to zero.

gas/
2021-05-XX  Jan Beulich  <jbeulich@suse.com>

	* testsuite/gas/i386/rela.s, testsuite/gas/i386/rela.d: New.
	* testsuite/gas/i386/i386.exp: Run new test.

ld/
2021-05-XX  Jan Beulich  <jbeulich@suse.com>

	* testsuite/ld-x86-64/rela.d: New.
	* testsuite/ld-x86-64/x86-64.exp: Run new test.

Patch

--- a/bfd/elf64-x86-64.c
+++ b/bfd/elf64-x86-64.c
@@ -45,133 +45,131 @@ 
 static reloc_howto_type x86_64_elf_howto_table[] =
 {
   HOWTO(R_X86_64_NONE, 0, 3, 0, false, 0, complain_overflow_dont,
-	bfd_elf_generic_reloc, "R_X86_64_NONE",	false, 0x00000000, 0x00000000,
+	bfd_elf_generic_reloc, "R_X86_64_NONE",	false, 0, 0x00000000,
 	false),
   HOWTO(R_X86_64_64, 0, 4, 64, false, 0, complain_overflow_dont,
-	bfd_elf_generic_reloc, "R_X86_64_64", false, MINUS_ONE, MINUS_ONE,
+	bfd_elf_generic_reloc, "R_X86_64_64", false, 0, MINUS_ONE,
 	false),
   HOWTO(R_X86_64_PC32, 0, 2, 32, true, 0, complain_overflow_signed,
-	bfd_elf_generic_reloc, "R_X86_64_PC32", false, 0xffffffff, 0xffffffff,
+	bfd_elf_generic_reloc, "R_X86_64_PC32", false, 0, 0xffffffff,
 	true),
   HOWTO(R_X86_64_GOT32, 0, 2, 32, false, 0, complain_overflow_signed,
-	bfd_elf_generic_reloc, "R_X86_64_GOT32", false, 0xffffffff, 0xffffffff,
+	bfd_elf_generic_reloc, "R_X86_64_GOT32", false, 0, 0xffffffff,
 	false),
   HOWTO(R_X86_64_PLT32, 0, 2, 32, true, 0, complain_overflow_signed,
-	bfd_elf_generic_reloc, "R_X86_64_PLT32", false, 0xffffffff, 0xffffffff,
+	bfd_elf_generic_reloc, "R_X86_64_PLT32", false, 0, 0xffffffff,
 	true),
   HOWTO(R_X86_64_COPY, 0, 2, 32, false, 0, complain_overflow_bitfield,
-	bfd_elf_generic_reloc, "R_X86_64_COPY", false, 0xffffffff, 0xffffffff,
+	bfd_elf_generic_reloc, "R_X86_64_COPY", false, 0, 0xffffffff,
 	false),
   HOWTO(R_X86_64_GLOB_DAT, 0, 4, 64, false, 0, complain_overflow_dont,
-	bfd_elf_generic_reloc, "R_X86_64_GLOB_DAT", false, MINUS_ONE,
-	MINUS_ONE, false),
+	bfd_elf_generic_reloc, "R_X86_64_GLOB_DAT", false, 0, MINUS_ONE,
+	false),
   HOWTO(R_X86_64_JUMP_SLOT, 0, 4, 64, false, 0, complain_overflow_dont,
-	bfd_elf_generic_reloc, "R_X86_64_JUMP_SLOT", false, MINUS_ONE,
-	MINUS_ONE, false),
+	bfd_elf_generic_reloc, "R_X86_64_JUMP_SLOT", false, 0, MINUS_ONE,
+	false),
   HOWTO(R_X86_64_RELATIVE, 0, 4, 64, false, 0, complain_overflow_dont,
-	bfd_elf_generic_reloc, "R_X86_64_RELATIVE", false, MINUS_ONE,
-	MINUS_ONE, false),
+	bfd_elf_generic_reloc, "R_X86_64_RELATIVE", false, 0, MINUS_ONE,
+	false),
   HOWTO(R_X86_64_GOTPCREL, 0, 2, 32, true, 0, complain_overflow_signed,
-	bfd_elf_generic_reloc, "R_X86_64_GOTPCREL", false, 0xffffffff,
-	0xffffffff, true),
+	bfd_elf_generic_reloc, "R_X86_64_GOTPCREL", false, 0, 0xffffffff,
+	true),
   HOWTO(R_X86_64_32, 0, 2, 32, false, 0, complain_overflow_unsigned,
-	bfd_elf_generic_reloc, "R_X86_64_32", false, 0xffffffff, 0xffffffff,
+	bfd_elf_generic_reloc, "R_X86_64_32", false, 0, 0xffffffff,
 	false),
   HOWTO(R_X86_64_32S, 0, 2, 32, false, 0, complain_overflow_signed,
-	bfd_elf_generic_reloc, "R_X86_64_32S", false, 0xffffffff, 0xffffffff,
+	bfd_elf_generic_reloc, "R_X86_64_32S", false, 0, 0xffffffff,
 	false),
   HOWTO(R_X86_64_16, 0, 1, 16, false, 0, complain_overflow_bitfield,
-	bfd_elf_generic_reloc, "R_X86_64_16", false, 0xffff, 0xffff, false),
+	bfd_elf_generic_reloc, "R_X86_64_16", false, 0, 0xffff, false),
   HOWTO(R_X86_64_PC16, 0, 1, 16, true, 0, complain_overflow_signed,
-	bfd_elf_generic_reloc, "R_X86_64_PC16", false, 0xffff, 0xffff, true),
+	bfd_elf_generic_reloc, "R_X86_64_PC16", false, 0, 0xffff, true),
   HOWTO(R_X86_64_8, 0, 0, 8, false, 0, complain_overflow_bitfield,
-	bfd_elf_generic_reloc, "R_X86_64_8", false, 0xff, 0xff, false),
+	bfd_elf_generic_reloc, "R_X86_64_8", false, 0, 0xff, false),
   HOWTO(R_X86_64_PC8, 0, 0, 8, true, 0, complain_overflow_signed,
-	bfd_elf_generic_reloc, "R_X86_64_PC8", false, 0xff, 0xff, true),
+	bfd_elf_generic_reloc, "R_X86_64_PC8", false, 0, 0xff, true),
   HOWTO(R_X86_64_DTPMOD64, 0, 4, 64, false, 0, complain_overflow_dont,
-	bfd_elf_generic_reloc, "R_X86_64_DTPMOD64", false, MINUS_ONE,
-	MINUS_ONE, false),
+	bfd_elf_generic_reloc, "R_X86_64_DTPMOD64", false, 0, MINUS_ONE,
+	false),
   HOWTO(R_X86_64_DTPOFF64, 0, 4, 64, false, 0, complain_overflow_dont,
-	bfd_elf_generic_reloc, "R_X86_64_DTPOFF64", false, MINUS_ONE,
-	MINUS_ONE, false),
+	bfd_elf_generic_reloc, "R_X86_64_DTPOFF64", false, 0, MINUS_ONE,
+	false),
   HOWTO(R_X86_64_TPOFF64, 0, 4, 64, false, 0, complain_overflow_dont,
-	bfd_elf_generic_reloc, "R_X86_64_TPOFF64", false, MINUS_ONE,
-	MINUS_ONE, false),
+	bfd_elf_generic_reloc, "R_X86_64_TPOFF64", false, 0, MINUS_ONE,
+	false),
   HOWTO(R_X86_64_TLSGD, 0, 2, 32, true, 0, complain_overflow_signed,
-	bfd_elf_generic_reloc, "R_X86_64_TLSGD", false, 0xffffffff,
-	0xffffffff, true),
+	bfd_elf_generic_reloc, "R_X86_64_TLSGD", false, 0, 0xffffffff,
+	true),
   HOWTO(R_X86_64_TLSLD, 0, 2, 32, true, 0, complain_overflow_signed,
-	bfd_elf_generic_reloc, "R_X86_64_TLSLD", false, 0xffffffff,
-	0xffffffff, true),
+	bfd_elf_generic_reloc, "R_X86_64_TLSLD", false, 0, 0xffffffff,
+	true),
   HOWTO(R_X86_64_DTPOFF32, 0, 2, 32, false, 0, complain_overflow_signed,
-	bfd_elf_generic_reloc, "R_X86_64_DTPOFF32", false, 0xffffffff,
-	0xffffffff, false),
+	bfd_elf_generic_reloc, "R_X86_64_DTPOFF32", false, 0, 0xffffffff,
+	false),
   HOWTO(R_X86_64_GOTTPOFF, 0, 2, 32, true, 0, complain_overflow_signed,
-	bfd_elf_generic_reloc, "R_X86_64_GOTTPOFF", false, 0xffffffff,
-	0xffffffff, true),
+	bfd_elf_generic_reloc, "R_X86_64_GOTTPOFF", false, 0, 	0xffffffff,
+	true),
   HOWTO(R_X86_64_TPOFF32, 0, 2, 32, false, 0, complain_overflow_signed,
-	bfd_elf_generic_reloc, "R_X86_64_TPOFF32", false, 0xffffffff,
-	0xffffffff, false),
+	bfd_elf_generic_reloc, "R_X86_64_TPOFF32", false, 0, 0xffffffff,
+	false),
   HOWTO(R_X86_64_PC64, 0, 4, 64, true, 0, complain_overflow_dont,
-	bfd_elf_generic_reloc, "R_X86_64_PC64", false, MINUS_ONE, MINUS_ONE,
+	bfd_elf_generic_reloc, "R_X86_64_PC64", false, 0, MINUS_ONE,
 	true),
   HOWTO(R_X86_64_GOTOFF64, 0, 4, 64, false, 0, complain_overflow_dont,
-	bfd_elf_generic_reloc, "R_X86_64_GOTOFF64",
-	false, MINUS_ONE, MINUS_ONE, false),
+	bfd_elf_generic_reloc, "R_X86_64_GOTOFF64", false, 0, MINUS_ONE,
+	false),
   HOWTO(R_X86_64_GOTPC32, 0, 2, 32, true, 0, complain_overflow_signed,
-	bfd_elf_generic_reloc, "R_X86_64_GOTPC32",
-	false, 0xffffffff, 0xffffffff, true),
+	bfd_elf_generic_reloc, "R_X86_64_GOTPC32", false, 0, 0xffffffff,
+	true),
   HOWTO(R_X86_64_GOT64, 0, 4, 64, false, 0, complain_overflow_signed,
-	bfd_elf_generic_reloc, "R_X86_64_GOT64", false, MINUS_ONE, MINUS_ONE,
+	bfd_elf_generic_reloc, "R_X86_64_GOT64", false, 0, MINUS_ONE,
 	false),
   HOWTO(R_X86_64_GOTPCREL64, 0, 4, 64, true, 0, complain_overflow_signed,
-	bfd_elf_generic_reloc, "R_X86_64_GOTPCREL64", false, MINUS_ONE,
-	MINUS_ONE, true),
+	bfd_elf_generic_reloc, "R_X86_64_GOTPCREL64", false, 0, MINUS_ONE,
+	true),
   HOWTO(R_X86_64_GOTPC64, 0, 4, 64, true, 0, complain_overflow_signed,
-	bfd_elf_generic_reloc, "R_X86_64_GOTPC64",
-	false, MINUS_ONE, MINUS_ONE, true),
+	bfd_elf_generic_reloc, "R_X86_64_GOTPC64", false, 0, MINUS_ONE,
+	true),
   HOWTO(R_X86_64_GOTPLT64, 0, 4, 64, false, 0, complain_overflow_signed,
-	bfd_elf_generic_reloc, "R_X86_64_GOTPLT64", false, MINUS_ONE,
-	MINUS_ONE, false),
+	bfd_elf_generic_reloc, "R_X86_64_GOTPLT64", false, 0, MINUS_ONE,
+	false),
   HOWTO(R_X86_64_PLTOFF64, 0, 4, 64, false, 0, complain_overflow_signed,
-	bfd_elf_generic_reloc, "R_X86_64_PLTOFF64", false, MINUS_ONE,
-	MINUS_ONE, false),
+	bfd_elf_generic_reloc, "R_X86_64_PLTOFF64", false, 0, MINUS_ONE,
+	false),
   HOWTO(R_X86_64_SIZE32, 0, 2, 32, false, 0, complain_overflow_unsigned,
-	bfd_elf_generic_reloc, "R_X86_64_SIZE32", false, 0xffffffff, 0xffffffff,
+	bfd_elf_generic_reloc, "R_X86_64_SIZE32", false, 0, 0xffffffff,
 	false),
   HOWTO(R_X86_64_SIZE64, 0, 4, 64, false, 0, complain_overflow_dont,
-	bfd_elf_generic_reloc, "R_X86_64_SIZE64", false, MINUS_ONE, MINUS_ONE,
+	bfd_elf_generic_reloc, "R_X86_64_SIZE64", false, 0, MINUS_ONE,
 	false),
   HOWTO(R_X86_64_GOTPC32_TLSDESC, 0, 2, 32, true, 0,
 	complain_overflow_bitfield, bfd_elf_generic_reloc,
-	"R_X86_64_GOTPC32_TLSDESC",
-	false, 0xffffffff, 0xffffffff, true),
+	"R_X86_64_GOTPC32_TLSDESC", false, 0, 0xffffffff, true),
   HOWTO(R_X86_64_TLSDESC_CALL, 0, 3, 0, false, 0,
 	complain_overflow_dont, bfd_elf_generic_reloc,
 	"R_X86_64_TLSDESC_CALL",
 	false, 0, 0, false),
   HOWTO(R_X86_64_TLSDESC, 0, 4, 64, false, 0,
 	complain_overflow_dont, bfd_elf_generic_reloc,
-	"R_X86_64_TLSDESC",
-	false, MINUS_ONE, MINUS_ONE, false),
+	"R_X86_64_TLSDESC", false, 0, MINUS_ONE, false),
   HOWTO(R_X86_64_IRELATIVE, 0, 4, 64, false, 0, complain_overflow_dont,
-	bfd_elf_generic_reloc, "R_X86_64_IRELATIVE", false, MINUS_ONE,
-	MINUS_ONE, false),
+	bfd_elf_generic_reloc, "R_X86_64_IRELATIVE", false, 0, MINUS_ONE,
+	false),
   HOWTO(R_X86_64_RELATIVE64, 0, 4, 64, false, 0, complain_overflow_dont,
-	bfd_elf_generic_reloc, "R_X86_64_RELATIVE64", false, MINUS_ONE,
-	MINUS_ONE, false),
+	bfd_elf_generic_reloc, "R_X86_64_RELATIVE64", false, 0, MINUS_ONE,
+	false),
   HOWTO(R_X86_64_PC32_BND, 0, 2, 32, true, 0, complain_overflow_signed,
-	bfd_elf_generic_reloc, "R_X86_64_PC32_BND", false, 0xffffffff, 0xffffffff,
+	bfd_elf_generic_reloc, "R_X86_64_PC32_BND", false, 0, 0xffffffff,
 	true),
   HOWTO(R_X86_64_PLT32_BND, 0, 2, 32, true, 0, complain_overflow_signed,
-	bfd_elf_generic_reloc, "R_X86_64_PLT32_BND", false, 0xffffffff, 0xffffffff,
+	bfd_elf_generic_reloc, "R_X86_64_PLT32_BND", false, 0, 0xffffffff,
 	true),
   HOWTO(R_X86_64_GOTPCRELX, 0, 2, 32, true, 0, complain_overflow_signed,
-	bfd_elf_generic_reloc, "R_X86_64_GOTPCRELX", false, 0xffffffff,
-	0xffffffff, true),
+	bfd_elf_generic_reloc, "R_X86_64_GOTPCRELX", false, 0, 0xffffffff,
+	true),
   HOWTO(R_X86_64_REX_GOTPCRELX, 0, 2, 32, true, 0, complain_overflow_signed,
-	bfd_elf_generic_reloc, "R_X86_64_REX_GOTPCRELX", false, 0xffffffff,
-	0xffffffff, true),
+	bfd_elf_generic_reloc, "R_X86_64_REX_GOTPCRELX", false, 0, 0xffffffff,
+	true),
 
   /* We have a gap in the reloc numbers here.
      R_X86_64_standard counts the number up to this point, and
@@ -191,7 +189,7 @@  static reloc_howto_type x86_64_elf_howto
 
 /* Use complain_overflow_bitfield on R_X86_64_32 for x32.  */
   HOWTO(R_X86_64_32, 0, 2, 32, false, 0, complain_overflow_bitfield,
-	bfd_elf_generic_reloc, "R_X86_64_32", false, 0xffffffff, 0xffffffff,
+	bfd_elf_generic_reloc, "R_X86_64_32", false, 0, 0xffffffff,
 	false)
 };
 
--- a/gas/testsuite/gas/i386/i386.exp
+++ b/gas/testsuite/gas/i386/i386.exp
@@ -1246,6 +1246,7 @@  if [gas_64_check] then {
 	run_dump_test "reloc64"
 	run_list_test "reloc64" "--defsym _bad_=1"
 	run_dump_test "mixed-mode-reloc64"
+	run_dump_test "rela"
 	run_dump_test "x86-64-ifunc"
 	run_dump_test "x86-64-opcode-inval"
 	run_dump_test "x86-64-opcode-inval-intel"
--- /dev/null
+++ b/gas/testsuite/gas/i386/rela.d
@@ -0,0 +1,13 @@ 
+#name: x86-64 rela relocs w/ non-zero relocated fields
+#objdump: -rsj .data
+
+.*: +file format .*
+
+RELOCATION RECORDS FOR \[\.data\]:
+
+OFFSET +TYPE +VALUE *
+0*0 R_X86_64_64 *q
+0*8 R_X86_64_32 *l
+
+Contents of section .data:
+ 0+0 11 ?11 ?11 ?11 22 ?22 ?22 ?22 33 ?33 ?33 ?33 44 ?44 ?44 ?44 .*
--- /dev/null
+++ b/gas/testsuite/gas/i386/rela.s
@@ -0,0 +1,14 @@ 
+# Note: This file is also used by an ld test case.
+
+	.text
+	.global _start
+_start:
+	ret
+
+	.data
+	.p2align 4
+l:	.long	0x11111111, 0x22222222
+q:	.quad	0x4444444433333333
+
+	.reloc l, BFD_RELOC_64, q
+	.reloc q, BFD_RELOC_32, l
--- /dev/null
+++ b/ld/testsuite/ld-x86-64/rela.d
@@ -0,0 +1,10 @@ 
+#name: x86-64 rela relocs w/ non-zero relocated fields
+#as: --64
+#source: ${srcdir}/../../../gas/testsuite/gas/i386/rela.s
+#ld: -melf_x86_64
+#objdump: -sj .data
+
+.*: +file format .*
+
+Contents of section .data:
+ *[0-9a-f]*0 08 ?.. ?.. ?.. 00 ?00 ?00 ?00 00 ?.. ?.. ?.. 44 ?44 ?44 ?44 .*
--- a/ld/testsuite/ld-x86-64/x86-64.exp
+++ b/ld/testsuite/ld-x86-64/x86-64.exp
@@ -262,6 +262,7 @@  run_dump_test "apic"
 run_dump_test "pcrel8"
 run_dump_test "pcrel16"
 run_dump_test "pcrel16-2"
+run_dump_test "rela"
 run_dump_test "tlsgd2"
 run_dump_test "tlsgd3"
 run_dump_test "tlsgd12"