x86-64: Never make R_X86_64_GOT64 section relative

Message ID CAMe9rOrR6fsP+VpbAHsQAMAuykvmDhK_tS39kDdvxbs3t+pYFg@mail.gmail.com
State New
Headers
Series x86-64: Never make R_X86_64_GOT64 section relative |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_binutils_build--master-arm fail Patch failed to apply
linaro-tcwg-bot/tcwg_binutils_build--master-aarch64 fail Patch failed to apply

Commit Message

H.J. Lu Sept. 19, 2024, 9:43 p.m. UTC
  R_X86_64_GOT64 relocation should never be made section relative.  Change
tc_i386_fix_adjustable to return 0 for BFD_RELOC_X86_64_GOT64.

gas/

PR gas/32189
* config/tc-i386.c (tc_i386_fix_adjustable): Return 0 for
BFD_RELOC_X86_64_GOT64.
* testsuite/gas/i386/reloc64.d: Updated.
* testsuite/gas/i386/reloc64.s: Add more tests for R_X86_64_GOT64
and R_X86_64_GOTOFF64.

ld/

PR gas/32189
* testsuite/ld-x86-64/x86-64.exp: Run PR gas/32189 test.
* testsuite/ld-x86-64/pr32189.s: New file.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
  

Comments

H.J. Lu Sept. 19, 2024, 11:21 p.m. UTC | #1
On Fri, Sep 20, 2024 at 5:43 AM H.J. Lu <hjl.tools@gmail.com> wrote:
>
> R_X86_64_GOT64 relocation should never be made section relative.  Change
> tc_i386_fix_adjustable to return 0 for BFD_RELOC_X86_64_GOT64.
>
> gas/
>
> PR gas/32189
> * config/tc-i386.c (tc_i386_fix_adjustable): Return 0 for
> BFD_RELOC_X86_64_GOT64.
> * testsuite/gas/i386/reloc64.d: Updated.
> * testsuite/gas/i386/reloc64.s: Add more tests for R_X86_64_GOT64
> and R_X86_64_GOTOFF64.
>
> ld/
>
> PR gas/32189
> * testsuite/ld-x86-64/x86-64.exp: Run PR gas/32189 test.
> * testsuite/ld-x86-64/pr32189.s: New file.
>
> Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
> --
> H.J.

I checked in this patch to disable PIE on PR gas/32189 test, which
contains the non-PIE assembly source, to support GCC defaulted to PIE.
  
H.J. Lu Sept. 20, 2024, 9:37 p.m. UTC | #2
On Fri, Sep 20, 2024 at 7:21 AM H.J. Lu <hjl.tools@gmail.com> wrote:
>
> On Fri, Sep 20, 2024 at 5:43 AM H.J. Lu <hjl.tools@gmail.com> wrote:
> >
> > R_X86_64_GOT64 relocation should never be made section relative.  Change
> > tc_i386_fix_adjustable to return 0 for BFD_RELOC_X86_64_GOT64.
> >
> > gas/
> >
> > PR gas/32189
> > * config/tc-i386.c (tc_i386_fix_adjustable): Return 0 for
> > BFD_RELOC_X86_64_GOT64.
> > * testsuite/gas/i386/reloc64.d: Updated.
> > * testsuite/gas/i386/reloc64.s: Add more tests for R_X86_64_GOT64
> > and R_X86_64_GOTOFF64.
> >
> > ld/
> >
> > PR gas/32189
> > * testsuite/ld-x86-64/x86-64.exp: Run PR gas/32189 test.
> > * testsuite/ld-x86-64/pr32189.s: New file.
> >
> > Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
> > --
> > H.J.
>
> I checked in this patch to disable PIE on PR gas/32189 test, which
> contains the non-PIE assembly source, to support GCC defaulted to PIE.
>
>
> --
> H.J.

I am backporting both patches to 2.43 branch.
  

Patch

From 3dd8baef0362c40803481e14bfad870e11b23ca4 Mon Sep 17 00:00:00 2001
From: "H.J. Lu" <hjl.tools@gmail.com>
Date: Fri, 20 Sep 2024 05:37:04 +0800
Subject: [PATCH] x86-64: Never make R_X86_64_GOT64 section relative

R_X86_64_GOT64 relocation should never be made section relative.  Change
tc_i386_fix_adjustable to return 0 for BFD_RELOC_X86_64_GOT64.

gas/

	PR gas/32189
	* config/tc-i386.c (tc_i386_fix_adjustable): Return 0 for
	BFD_RELOC_X86_64_GOT64.
	* testsuite/gas/i386/reloc64.d: Updated.
	* testsuite/gas/i386/reloc64.s: Add more tests for R_X86_64_GOT64
	and R_X86_64_GOTOFF64.

ld/

	PR gas/32189
	* testsuite/ld-x86-64/x86-64.exp: Run PR gas/32189 test.
	* testsuite/ld-x86-64/pr32189.s: New file.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
---
 gas/config/tc-i386.c              |  1 +
 gas/testsuite/gas/i386/reloc64.d  |  7 +++++++
 gas/testsuite/gas/i386/reloc64.s  |  9 ++++++++
 ld/testsuite/ld-x86-64/pr32189.s  | 35 +++++++++++++++++++++++++++++++
 ld/testsuite/ld-x86-64/x86-64.exp |  8 +++++++
 5 files changed, 60 insertions(+)
 create mode 100644 ld/testsuite/ld-x86-64/pr32189.s

diff --git a/gas/config/tc-i386.c b/gas/config/tc-i386.c
index 4739a6d0c21..72de448728a 100644
--- a/gas/config/tc-i386.c
+++ b/gas/config/tc-i386.c
@@ -3893,6 +3893,7 @@  tc_i386_fix_adjustable (fixS *fixP)
       || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF32
       || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF64
       || fixP->fx_r_type == BFD_RELOC_X86_64_GOTOFF64
+      || fixP->fx_r_type == BFD_RELOC_X86_64_GOT64
       || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPC32_TLSDESC
       || fixP->fx_r_type == BFD_RELOC_X86_64_CODE_4_GOTPC32_TLSDESC
       || fixP->fx_r_type == BFD_RELOC_X86_64_TLSDESC_CALL
diff --git a/gas/testsuite/gas/i386/reloc64.d b/gas/testsuite/gas/i386/reloc64.d
index 540a9b77d35..a96072d66fb 100644
--- a/gas/testsuite/gas/i386/reloc64.d
+++ b/gas/testsuite/gas/i386/reloc64.d
@@ -57,6 +57,8 @@  Disassembly of section \.text:
 .*[ 	]+R_X86_64_32[ 	]+xtrn
 .*[ 	]+R_X86_64_32S[ 	]+xtrn
 .*[ 	]+R_X86_64_32[ 	]+xtrn
+.*[ 	]+R_X86_64_GOT64[ 	]+ptr
+.*[ 	]+R_X86_64_GOTOFF64[ 	]+Ldst
 Disassembly of section \.data:
 #...
 .*[ 	]+R_X86_64_64[ 	]+xtrn
@@ -97,3 +99,8 @@  Disassembly of section \.data:
 .*[ 	]+R_X86_64_GOT32[ 	]+xtrn-0x4
 .*[ 	]+R_X86_64_GOT32[ 	]+xtrn\+0x4
 .*[ 	]+R_X86_64_GOTPLT64[ 	]+xtrn
+
+.* <ptr>:
+	...
+.* <Ldst>:
+	...
diff --git a/gas/testsuite/gas/i386/reloc64.s b/gas/testsuite/gas/i386/reloc64.s
index bc6f0fa6cc3..2293865df20 100644
--- a/gas/testsuite/gas/i386/reloc64.s
+++ b/gas/testsuite/gas/i386/reloc64.s
@@ -225,3 +225,12 @@  bad	.byte	xtrn@gotplt
 	vgatherdps %xmm2, xtrn(,%xmm1), %xmm0
 	addr32 vgatherdps %xmm2, xtrn(,%xmm1), %xmm0
 bad	.long	xtrn@plt - .
+
+	.text
+	movabs	$ptr@GOT, %rax
+	movabs	$Ldst@GOTOFF, %rdx
+	.data
+ptr:
+	.quad 0
+Ldst:
+	.quad 0
diff --git a/ld/testsuite/ld-x86-64/pr32189.s b/ld/testsuite/ld-x86-64/pr32189.s
new file mode 100644
index 00000000000..3d0f682d663
--- /dev/null
+++ b/ld/testsuite/ld-x86-64/pr32189.s
@@ -0,0 +1,35 @@ 
+	.text
+	.global _start
+_start:
+	leaq 1f(%rip), %r11
+1:
+	movabs $_GLOBAL_OFFSET_TABLE_, %r15
+	leaq (%r11,%r15), %r15
+
+	movabs $ptr@GOT, %rax
+	movabs $Ldst@GOTOFF, %rdx
+	movq (%rax,%r15), %rax
+	leaq (%rdx,%r15), %rcx
+	movq %rcx, (%rax)
+
+	# Write out "PASS\n".
+	movl	$5, %edx
+	movl	$.LC0, %esi
+	movl	$1, %edi
+	movl	$1, %eax
+	syscall
+
+	# exit
+	movq $60, %rax
+	movq $0, %rdi
+	syscall
+
+	.data
+Ldst:
+	.quad 0
+ptr:
+	.quad 0
+	.section	.rodata.str1.1,"aMS",@progbits,1
+.LC0:
+	.string	"PASS\n"
+	.section	.note.GNU-stack,"",@progbits
diff --git a/ld/testsuite/ld-x86-64/x86-64.exp b/ld/testsuite/ld-x86-64/x86-64.exp
index e729b69a7cd..c203fbe4037 100644
--- a/ld/testsuite/ld-x86-64/x86-64.exp
+++ b/ld/testsuite/ld-x86-64/x86-64.exp
@@ -1921,6 +1921,14 @@  if { [isnative] && [check_compiler_available] } {
 	    "pass.out" \
 	    "-fPIE" \
 	] \
+	[list \
+	    "Run pr32189" \
+	    "-nostdlib -nostartfiles" \
+	    "" \
+	    { pr32189.s } \
+	    "pr32189" \
+	    "pass.out" \
+	] \
     ]
 
     # Run-time tests which require working ifunc attribute support.
-- 
2.46.1