gold: x86-64: Fix TLSDESC -> LE relaxation
authorH.J. Lu <hjl.tools@gmail.com>
Fri, 1 May 2020 17:08:31 +0000 (10:08 -0700)
committerH.J. Lu <hjl.tools@gmail.com>
Fri, 1 May 2020 17:08:48 +0000 (10:08 -0700)
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/ChangeLog
gold/testsuite/Makefile.am
gold/testsuite/Makefile.in
gold/testsuite/x86_64_gd_to_le.s [new file with mode: 0644]
gold/testsuite/x86_64_gd_to_le.sh [new file with mode: 0755]
gold/x86_64.cc

index 97044b6f7288c38f078295c0285e3dca369e14ed..0b7cb07fddf9eec920f916e2212f10726ec6b49f 100644 (file)
@@ -1,3 +1,20 @@
+2020-05-01  H.J. Lu  <hongjiu.lu@intel.com>
+
+       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.
+
 2020-05-01  H.J. Lu  <hongjiu.lu@intel.com>
 
        * testsuite/odr_violation2.cc (Ordering::operator()): Make
index 4c26f3355b7efbea0540abce3cf7c1b52d4b11c8..6187554c3251a1cb2543469be6cb513cb92000b3 100644 (file)
@@ -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
index 9f29dc638c6d6ffdd6876543e9d91935652b488f..8d2f142a6f70aac39ce8b08d33b31c20fce75044 100644 (file)
@@ -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
@@ -5667,6 +5670,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'; \
@@ -8364,6 +8374,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 (file)
index 0000000..dcdd303
--- /dev/null
@@ -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 (executable)
index 0000000..7ecc6ea
--- /dev/null
@@ -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
index 114ebb081fd76275e0a11d5a9fe1bd31a9571a77..d4f1d6b2324e44e3e1a355adc1ca3c85d6a32812 100644 (file)
@@ -5477,7 +5477,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);
@@ -5516,9 +5518,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);
     }