gold: x86-64: Fix TLSDESC -> LE relaxation

Message ID 20200201011027.478698-1-hjl.tools@gmail.com
State New
Headers show
Series
  • gold: x86-64: Fix TLSDESC -> LE relaxation
Related show

Commit Message

H.J. Lu Feb. 1, 2020, 1:10 a.m.
X86-64 TLSDESC sequences can be:

4c 8d 0d 00 00 00 00	leaq	foo@TLSDESC(%rip), %r9
4c 89 c8		movq	%r9, %rax
ff 10			call	*foo@TLSCALL(%rax)

TLSDESC -> LE relaxation can turn them into:

49 c7 c1 fc ff ff ff 	mov    $0xfffffffffffffffc,%r9
4c 89 c8             	mov    %r9,%rax
66 90                	xchg   %ax,%ax

We need to check and update the REX byte in this case.

	PR gold/25473
	* x86_64.cc (Target_x86_64<size>::Relocate::tls_desc_gd_to_ie):
	Properly check r8 - r15 in "lea foo@TLSDESC(%rip), %reg".
	(Target_x86_64<size>::Relocate::tls_desc_gd_to_le): Properly
	relax r8 - r15 in "lea foo@TLSDESC(%rip), %reg".
	* testsuite/Makefile.am (check_SCRIPTS): Add x86_64_gd_to_le.sh.
	(check_DATA): Add x86_64_gd_to_le.stdout.
	(MOSTLYCLEANFILES): Add x86_64_gd_to_le.
	(x86_64_gd_to_le.o): New target.
	(x86_64_gd_to_le): Likewise.
	(x86_64_gd_to_le.stdout): Likewise.
	* testsuite/Makefile.in: Regenerated.
	* testsuite/x86_64_gd_to_le.s: New file.
	* testsuite/x86_64_gd_to_le.sh: Likewise.
---
 gold/testsuite/Makefile.am        | 11 +++++++++++
 gold/testsuite/Makefile.in        | 17 +++++++++++++++++
 gold/testsuite/x86_64_gd_to_le.s  | 24 ++++++++++++++++++++++++
 gold/testsuite/x86_64_gd_to_le.sh | 26 ++++++++++++++++++++++++++
 gold/x86_64.cc                    | 11 ++++++++---
 5 files changed, 86 insertions(+), 3 deletions(-)
 create mode 100644 gold/testsuite/x86_64_gd_to_le.s
 create mode 100755 gold/testsuite/x86_64_gd_to_le.sh

-- 
2.24.1

Comments

Alan Modra via Binutils May 1, 2020, 3:56 p.m. | #1
On Fri, Jan 31, 2020 at 5:10 PM H.J. Lu <hjl.tools@gmail.com> wrote:
>

> X86-64 TLSDESC sequences can be:

>

> 4c 8d 0d 00 00 00 00    leaq    foo@TLSDESC(%rip), %r9

> 4c 89 c8                movq    %r9, %rax

> ff 10                   call    *foo@TLSCALL(%rax)

>

> TLSDESC -> LE relaxation can turn them into:

>

> 49 c7 c1 fc ff ff ff    mov    $0xfffffffffffffffc,%r9

> 4c 89 c8                mov    %r9,%rax

> 66 90                   xchg   %ax,%ax

>

> We need to check and update the REX byte in this case.

>

>         PR gold/25473

>         * x86_64.cc (Target_x86_64<size>::Relocate::tls_desc_gd_to_ie):

>         Properly check r8 - r15 in "lea foo@TLSDESC(%rip), %reg".

>         (Target_x86_64<size>::Relocate::tls_desc_gd_to_le): Properly

>         relax r8 - r15 in "lea foo@TLSDESC(%rip), %reg".

>         * testsuite/Makefile.am (check_SCRIPTS): Add x86_64_gd_to_le.sh.

>         (check_DATA): Add x86_64_gd_to_le.stdout.

>         (MOSTLYCLEANFILES): Add x86_64_gd_to_le.

>         (x86_64_gd_to_le.o): New target.

>         (x86_64_gd_to_le): Likewise.

>         (x86_64_gd_to_le.stdout): Likewise.

>         * testsuite/Makefile.in: Regenerated.

>         * testsuite/x86_64_gd_to_le.s: New file.

>         * testsuite/x86_64_gd_to_le.sh: Likewise.

> ---

>  gold/testsuite/Makefile.am        | 11 +++++++++++

>  gold/testsuite/Makefile.in        | 17 +++++++++++++++++

>  gold/testsuite/x86_64_gd_to_le.s  | 24 ++++++++++++++++++++++++

>  gold/testsuite/x86_64_gd_to_le.sh | 26 ++++++++++++++++++++++++++

>  gold/x86_64.cc                    | 11 ++++++++---

>  5 files changed, 86 insertions(+), 3 deletions(-)

>  create mode 100644 gold/testsuite/x86_64_gd_to_le.s

>  create mode 100755 gold/testsuite/x86_64_gd_to_le.sh

>

> diff --git a/gold/testsuite/Makefile.am b/gold/testsuite/Makefile.am

> index ed1ea1755ac..3d8c42cadfc 100644

> --- a/gold/testsuite/Makefile.am

> +++ b/gold/testsuite/Makefile.am

> @@ -1202,6 +1202,17 @@ x86_64_indirect_jump_to_direct1: x86_64_indirect_jump_to_direct1.o gcctestdir/ld

>  x86_64_indirect_jump_to_direct1.stdout: x86_64_indirect_jump_to_direct1

>         $(TEST_OBJDUMP) -dw $< > $@

>

> +check_SCRIPTS += x86_64_gd_to_le.sh

> +check_DATA += x86_64_gd_to_le.stdout

> +MOSTLYCLEANFILES += x86_64_gd_to_le

> +

> +x86_64_gd_to_le.o: x86_64_gd_to_le.s

> +       $(TEST_AS) --64 -o $@ $<

> +x86_64_gd_to_le: x86_64_gd_to_le.o gcctestdir/ld

> +       gcctestdir/ld -o $@ $<

> +x86_64_gd_to_le.stdout: x86_64_gd_to_le

> +       $(TEST_OBJDUMP) -dw $< > $@

> +

>  check_SCRIPTS += x86_64_overflow_pc32.sh

>  check_DATA += x86_64_overflow_pc32.err

>  MOSTLYCLEANFILES += x86_64_overflow_pc32.err

> diff --git a/gold/testsuite/Makefile.in b/gold/testsuite/Makefile.in

> index 9533b0e20b2..9409e1ad3d2 100644

> --- a/gold/testsuite/Makefile.in

> +++ b/gold/testsuite/Makefile.in

> @@ -291,6 +291,7 @@ check_PROGRAMS = $(am__EXEEXT_1) $(am__EXEEXT_2) $(am__EXEEXT_3) \

>  @FN_PTRS_IN_SO_WITHOUT_PIC_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@@TLS_TRUE@am__append_28 = tls_shared_nonpic_test

>  @DEFAULT_TARGET_X86_64_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@am__append_29 = x86_64_mov_to_lea.sh \

>  @DEFAULT_TARGET_X86_64_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@     x86_64_indirect_call_to_direct.sh \

> +@DEFAULT_TARGET_X86_64_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@     x86_64_gd_to_le.sh \

>  @DEFAULT_TARGET_X86_64_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@     x86_64_overflow_pc32.sh \

>  @DEFAULT_TARGET_X86_64_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@     x32_overflow_pc32.sh \

>  @DEFAULT_TARGET_X86_64_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@     pr23016_1.sh \

> @@ -311,6 +312,7 @@ check_PROGRAMS = $(am__EXEEXT_1) $(am__EXEEXT_2) $(am__EXEEXT_3) \

>  @DEFAULT_TARGET_X86_64_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@     x86_64_mov_to_lea14.stdout \

>  @DEFAULT_TARGET_X86_64_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@     x86_64_indirect_call_to_direct1.stdout \

>  @DEFAULT_TARGET_X86_64_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@     x86_64_indirect_jump_to_direct1.stdout \

> +@DEFAULT_TARGET_X86_64_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@     x86_64_gd_to_le.stdout \

>  @DEFAULT_TARGET_X86_64_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@     x86_64_overflow_pc32.err \

>  @DEFAULT_TARGET_X86_64_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@     x32_overflow_pc32.err \

>  @DEFAULT_TARGET_X86_64_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@     pr23016_1.stdout \

> @@ -332,6 +334,7 @@ check_PROGRAMS = $(am__EXEEXT_1) $(am__EXEEXT_2) $(am__EXEEXT_3) \

>  @DEFAULT_TARGET_X86_64_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@     x86_64_mov_to_lea14 \

>  @DEFAULT_TARGET_X86_64_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@     x86_64_indirect_call_to_direct1 \

>  @DEFAULT_TARGET_X86_64_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@     x86_64_indirect_jump_to_direct1 \

> +@DEFAULT_TARGET_X86_64_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@     x86_64_gd_to_le \

>  @DEFAULT_TARGET_X86_64_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@     x86_64_overflow_pc32.err \

>  @DEFAULT_TARGET_X86_64_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@     x32_overflow_pc32.err

>  @DEFAULT_TARGET_X86_64_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@am__append_32 = pr17704a_test

> @@ -5629,6 +5632,13 @@ x86_64_indirect_call_to_direct.sh.log: x86_64_indirect_call_to_direct.sh

>         --log-file $$b.log --trs-file $$b.trs \

>         $(am__common_driver_flags) $(AM_LOG_DRIVER_FLAGS) $(LOG_DRIVER_FLAGS) -- $(LOG_COMPILE) \

>         "$$tst" $(AM_TESTS_FD_REDIRECT)

> +x86_64_gd_to_le.sh.log: x86_64_gd_to_le.sh

> +       @p='x86_64_gd_to_le.sh'; \

> +       b='x86_64_gd_to_le.sh'; \

> +       $(am__check_pre) $(LOG_DRIVER) --test-name "$$f" \

> +       --log-file $$b.log --trs-file $$b.trs \

> +       $(am__common_driver_flags) $(AM_LOG_DRIVER_FLAGS) $(LOG_DRIVER_FLAGS) -- $(LOG_COMPILE) \

> +       "$$tst" $(AM_TESTS_FD_REDIRECT)

>  x86_64_overflow_pc32.sh.log: x86_64_overflow_pc32.sh

>         @p='x86_64_overflow_pc32.sh'; \

>         b='x86_64_overflow_pc32.sh'; \

> @@ -8312,6 +8322,13 @@ uninstall-am:

>  @DEFAULT_TARGET_X86_64_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@     gcctestdir/ld -o $@ $<

>  @DEFAULT_TARGET_X86_64_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@x86_64_indirect_jump_to_direct1.stdout: x86_64_indirect_jump_to_direct1

>  @DEFAULT_TARGET_X86_64_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@     $(TEST_OBJDUMP) -dw $< > $@

> +

> +@DEFAULT_TARGET_X86_64_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@x86_64_gd_to_le.o: x86_64_gd_to_le.s

> +@DEFAULT_TARGET_X86_64_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@     $(TEST_AS) --64 -o $@ $<

> +@DEFAULT_TARGET_X86_64_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@x86_64_gd_to_le: x86_64_gd_to_le.o gcctestdir/ld

> +@DEFAULT_TARGET_X86_64_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@     gcctestdir/ld -o $@ $<

> +@DEFAULT_TARGET_X86_64_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@x86_64_gd_to_le.stdout: x86_64_gd_to_le

> +@DEFAULT_TARGET_X86_64_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@     $(TEST_OBJDUMP) -dw $< > $@

>  @DEFAULT_TARGET_X86_64_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@x86_64_overflow_pc32.o: x86_64_overflow_pc32.s

>  @DEFAULT_TARGET_X86_64_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@     $(TEST_AS) -o $@ $<

>  @DEFAULT_TARGET_X86_64_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@x86_64_overflow_pc32.err: x86_64_overflow_pc32.o gcctestdir/ld

> diff --git a/gold/testsuite/x86_64_gd_to_le.s b/gold/testsuite/x86_64_gd_to_le.s

> new file mode 100644

> index 00000000000..dcdd303184d

> --- /dev/null

> +++ b/gold/testsuite/x86_64_gd_to_le.s

> @@ -0,0 +1,24 @@

> +       .text

> +       .p2align 4

> +       .globl  _start

> +       .type   _start, @function

> +_start:

> +       .cfi_startproc

> +       subq    $8, %rsp

> +       .cfi_def_cfa_offset 16

> +       leaq    foo@TLSDESC(%rip), %r9

> +       movq    %r9, %rax

> +       call    *foo@TLSCALL(%rax)

> +       addq    %fs:0, %rax

> +       addq    $8, %rsp

> +       .cfi_def_cfa_offset 8

> +       ret

> +       .cfi_endproc

> +       .size   _start, .-_start

> +       .section        .tdata,"awT",@progbits

> +       .align 4

> +       .type   foo, @object

> +       .size   foo, 4

> +foo:

> +       .long   30

> +       .section        .note.GNU-stack,"",@progbits

> diff --git a/gold/testsuite/x86_64_gd_to_le.sh b/gold/testsuite/x86_64_gd_to_le.sh

> new file mode 100755

> index 00000000000..7ecc6ea5a23

> --- /dev/null

> +++ b/gold/testsuite/x86_64_gd_to_le.sh

> @@ -0,0 +1,26 @@

> +#!/bin/sh

> +

> +# x86_64_gd_to_ie.sh -- a test for GDesc -> IE conversion.

> +

> +# Copyright (C) 2020 Free Software Foundation, Inc.

> +

> +# This file is part of gold.

> +

> +# This program is free software; you can redistribute it and/or modify

> +# it under the terms of the GNU General Public License as published by

> +# the Free Software Foundation; either version 3 of the License, or

> +# (at your option) any later version.

> +

> +# This program is distributed in the hope that it will be useful,

> +# but WITHOUT ANY WARRANTY; without even the implied warranty of

> +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the

> +# GNU General Public License for more details.

> +

> +# You should have received a copy of the GNU General Public License

> +# along with this program; if not, write to the Free Software

> +# Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,

> +# MA 02110-1301, USA.

> +

> +set -e

> +

> +grep -q "mov[ \t]\+\$0x[a-f0-9]\+,%r9" x86_64_gd_to_le.stdout

> diff --git a/gold/x86_64.cc b/gold/x86_64.cc

> index a65e2c55e9e..622e9431021 100644

> --- a/gold/x86_64.cc

> +++ b/gold/x86_64.cc

> @@ -5479,7 +5479,9 @@ Target_x86_64<size>::Relocate::tls_desc_gd_to_ie(

>        tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);

>        tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);

>        tls::check_tls(relinfo, relnum, rela.get_r_offset(),

> -                    view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);

> +                    ((view[-3] & 0xfb) == 0x48

> +                     && view[-2] == 0x8d

> +                     && (view[-1] & 0xc7) == 0x05));

>        view[-2] = 0x8b;

>        const elfcpp::Elf_Xword addend = rela.get_r_addend();

>        Relocate_functions<size, false>::pcrela32(view, value, addend, address);

> @@ -5518,9 +5520,12 @@ Target_x86_64<size>::Relocate::tls_desc_gd_to_le(

>        tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);

>        tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);

>        tls::check_tls(relinfo, relnum, rela.get_r_offset(),

> -                    view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);

> +                    ((view[-3] & 0xfb) == 0x48

> +                     && view[-2] == 0x8d

> +                     && (view[-1] & 0xc7) == 0x05));

> +      view[-3] = 0x48 | ((view[-3] >> 2) & 1);

>        view[-2] = 0xc7;

> -      view[-1] = 0xc0;

> +      view[-1] = 0xc0 | ((view[-1] >> 3) & 7);

>        value -= tls_segment->memsz();

>        Relocate_functions<size, false>::rela32(view, value, 0);

>      }

> --

> 2.24.1

>


I am checking in this patch.

-- 
H.J.

Patch

diff --git a/gold/testsuite/Makefile.am b/gold/testsuite/Makefile.am
index ed1ea1755ac..3d8c42cadfc 100644
--- a/gold/testsuite/Makefile.am
+++ b/gold/testsuite/Makefile.am
@@ -1202,6 +1202,17 @@  x86_64_indirect_jump_to_direct1: x86_64_indirect_jump_to_direct1.o gcctestdir/ld
 x86_64_indirect_jump_to_direct1.stdout: x86_64_indirect_jump_to_direct1
 	$(TEST_OBJDUMP) -dw $< > $@
 
+check_SCRIPTS += x86_64_gd_to_le.sh
+check_DATA += x86_64_gd_to_le.stdout
+MOSTLYCLEANFILES += x86_64_gd_to_le
+
+x86_64_gd_to_le.o: x86_64_gd_to_le.s
+	$(TEST_AS) --64 -o $@ $<
+x86_64_gd_to_le: x86_64_gd_to_le.o gcctestdir/ld
+	gcctestdir/ld -o $@ $<
+x86_64_gd_to_le.stdout: x86_64_gd_to_le
+	$(TEST_OBJDUMP) -dw $< > $@
+
 check_SCRIPTS += x86_64_overflow_pc32.sh
 check_DATA += x86_64_overflow_pc32.err
 MOSTLYCLEANFILES += x86_64_overflow_pc32.err
diff --git a/gold/testsuite/Makefile.in b/gold/testsuite/Makefile.in
index 9533b0e20b2..9409e1ad3d2 100644
--- a/gold/testsuite/Makefile.in
+++ b/gold/testsuite/Makefile.in
@@ -291,6 +291,7 @@  check_PROGRAMS = $(am__EXEEXT_1) $(am__EXEEXT_2) $(am__EXEEXT_3) \
 @FN_PTRS_IN_SO_WITHOUT_PIC_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@@TLS_TRUE@am__append_28 = tls_shared_nonpic_test
 @DEFAULT_TARGET_X86_64_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@am__append_29 = x86_64_mov_to_lea.sh \
 @DEFAULT_TARGET_X86_64_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@	x86_64_indirect_call_to_direct.sh \
+@DEFAULT_TARGET_X86_64_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@	x86_64_gd_to_le.sh \
 @DEFAULT_TARGET_X86_64_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@	x86_64_overflow_pc32.sh \
 @DEFAULT_TARGET_X86_64_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@	x32_overflow_pc32.sh \
 @DEFAULT_TARGET_X86_64_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@	pr23016_1.sh \
@@ -311,6 +312,7 @@  check_PROGRAMS = $(am__EXEEXT_1) $(am__EXEEXT_2) $(am__EXEEXT_3) \
 @DEFAULT_TARGET_X86_64_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@	x86_64_mov_to_lea14.stdout \
 @DEFAULT_TARGET_X86_64_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@	x86_64_indirect_call_to_direct1.stdout \
 @DEFAULT_TARGET_X86_64_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@	x86_64_indirect_jump_to_direct1.stdout \
+@DEFAULT_TARGET_X86_64_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@	x86_64_gd_to_le.stdout \
 @DEFAULT_TARGET_X86_64_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@	x86_64_overflow_pc32.err \
 @DEFAULT_TARGET_X86_64_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@	x32_overflow_pc32.err \
 @DEFAULT_TARGET_X86_64_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@	pr23016_1.stdout \
@@ -332,6 +334,7 @@  check_PROGRAMS = $(am__EXEEXT_1) $(am__EXEEXT_2) $(am__EXEEXT_3) \
 @DEFAULT_TARGET_X86_64_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@	x86_64_mov_to_lea14 \
 @DEFAULT_TARGET_X86_64_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@	x86_64_indirect_call_to_direct1 \
 @DEFAULT_TARGET_X86_64_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@	x86_64_indirect_jump_to_direct1 \
+@DEFAULT_TARGET_X86_64_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@	x86_64_gd_to_le \
 @DEFAULT_TARGET_X86_64_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@	x86_64_overflow_pc32.err \
 @DEFAULT_TARGET_X86_64_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@	x32_overflow_pc32.err
 @DEFAULT_TARGET_X86_64_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@am__append_32 = pr17704a_test
@@ -5629,6 +5632,13 @@  x86_64_indirect_call_to_direct.sh.log: x86_64_indirect_call_to_direct.sh
 	--log-file $$b.log --trs-file $$b.trs \
 	$(am__common_driver_flags) $(AM_LOG_DRIVER_FLAGS) $(LOG_DRIVER_FLAGS) -- $(LOG_COMPILE) \
 	"$$tst" $(AM_TESTS_FD_REDIRECT)
+x86_64_gd_to_le.sh.log: x86_64_gd_to_le.sh
+	@p='x86_64_gd_to_le.sh'; \
+	b='x86_64_gd_to_le.sh'; \
+	$(am__check_pre) $(LOG_DRIVER) --test-name "$$f" \
+	--log-file $$b.log --trs-file $$b.trs \
+	$(am__common_driver_flags) $(AM_LOG_DRIVER_FLAGS) $(LOG_DRIVER_FLAGS) -- $(LOG_COMPILE) \
+	"$$tst" $(AM_TESTS_FD_REDIRECT)
 x86_64_overflow_pc32.sh.log: x86_64_overflow_pc32.sh
 	@p='x86_64_overflow_pc32.sh'; \
 	b='x86_64_overflow_pc32.sh'; \
@@ -8312,6 +8322,13 @@  uninstall-am:
 @DEFAULT_TARGET_X86_64_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@	gcctestdir/ld -o $@ $<
 @DEFAULT_TARGET_X86_64_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@x86_64_indirect_jump_to_direct1.stdout: x86_64_indirect_jump_to_direct1
 @DEFAULT_TARGET_X86_64_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@	$(TEST_OBJDUMP) -dw $< > $@
+
+@DEFAULT_TARGET_X86_64_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@x86_64_gd_to_le.o: x86_64_gd_to_le.s
+@DEFAULT_TARGET_X86_64_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@	$(TEST_AS) --64 -o $@ $<
+@DEFAULT_TARGET_X86_64_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@x86_64_gd_to_le: x86_64_gd_to_le.o gcctestdir/ld
+@DEFAULT_TARGET_X86_64_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@	gcctestdir/ld -o $@ $<
+@DEFAULT_TARGET_X86_64_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@x86_64_gd_to_le.stdout: x86_64_gd_to_le
+@DEFAULT_TARGET_X86_64_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@	$(TEST_OBJDUMP) -dw $< > $@
 @DEFAULT_TARGET_X86_64_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@x86_64_overflow_pc32.o: x86_64_overflow_pc32.s
 @DEFAULT_TARGET_X86_64_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@	$(TEST_AS) -o $@ $<
 @DEFAULT_TARGET_X86_64_TRUE@@GCC_TRUE@@NATIVE_LINKER_TRUE@x86_64_overflow_pc32.err: x86_64_overflow_pc32.o gcctestdir/ld
diff --git a/gold/testsuite/x86_64_gd_to_le.s b/gold/testsuite/x86_64_gd_to_le.s
new file mode 100644
index 00000000000..dcdd303184d
--- /dev/null
+++ b/gold/testsuite/x86_64_gd_to_le.s
@@ -0,0 +1,24 @@ 
+	.text
+	.p2align 4
+	.globl	_start
+	.type	_start, @function
+_start:
+	.cfi_startproc
+	subq	$8, %rsp
+	.cfi_def_cfa_offset 16
+	leaq	foo@TLSDESC(%rip), %r9
+	movq	%r9, %rax
+	call	*foo@TLSCALL(%rax)
+	addq	%fs:0, %rax
+	addq	$8, %rsp
+	.cfi_def_cfa_offset 8
+	ret
+	.cfi_endproc
+	.size	_start, .-_start
+	.section	.tdata,"awT",@progbits
+	.align 4
+	.type	foo, @object
+	.size	foo, 4
+foo:
+	.long	30
+	.section	.note.GNU-stack,"",@progbits
diff --git a/gold/testsuite/x86_64_gd_to_le.sh b/gold/testsuite/x86_64_gd_to_le.sh
new file mode 100755
index 00000000000..7ecc6ea5a23
--- /dev/null
+++ b/gold/testsuite/x86_64_gd_to_le.sh
@@ -0,0 +1,26 @@ 
+#!/bin/sh
+
+# x86_64_gd_to_ie.sh -- a test for GDesc -> IE conversion.
+
+# Copyright (C) 2020 Free Software Foundation, Inc.
+
+# This file is part of gold.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
+# MA 02110-1301, USA.
+
+set -e
+
+grep -q "mov[ \t]\+\$0x[a-f0-9]\+,%r9" x86_64_gd_to_le.stdout
diff --git a/gold/x86_64.cc b/gold/x86_64.cc
index a65e2c55e9e..622e9431021 100644
--- a/gold/x86_64.cc
+++ b/gold/x86_64.cc
@@ -5479,7 +5479,9 @@  Target_x86_64<size>::Relocate::tls_desc_gd_to_ie(
       tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
       tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
       tls::check_tls(relinfo, relnum, rela.get_r_offset(),
-		     view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
+		     ((view[-3] & 0xfb) == 0x48
+		      && view[-2] == 0x8d
+		      && (view[-1] & 0xc7) == 0x05));
       view[-2] = 0x8b;
       const elfcpp::Elf_Xword addend = rela.get_r_addend();
       Relocate_functions<size, false>::pcrela32(view, value, addend, address);
@@ -5518,9 +5520,12 @@  Target_x86_64<size>::Relocate::tls_desc_gd_to_le(
       tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
       tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
       tls::check_tls(relinfo, relnum, rela.get_r_offset(),
-		     view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
+		     ((view[-3] & 0xfb) == 0x48
+		      && view[-2] == 0x8d
+		      && (view[-1] & 0xc7) == 0x05));
+      view[-3] = 0x48 | ((view[-3] >> 2) & 1);
       view[-2] = 0xc7;
-      view[-1] = 0xc0;
+      view[-1] = 0xc0 | ((view[-1] >> 3) & 7);
       value -= tls_segment->memsz();
       Relocate_functions<size, false>::rela32(view, value, 0);
     }