]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
x86: Turn PLT32 to PC32 only for PC-relative relocations
authorH.J. Lu <hjl.tools@gmail.com>
Sun, 22 Sep 2024 09:06:45 +0000 (17:06 +0800)
committerH.J. Lu <hjl.tools@gmail.com>
Wed, 25 Sep 2024 23:11:13 +0000 (07:11 +0800)
commit 292676c15a615b5a95bede9ee91004d3f7ee7dfd
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Thu Feb 13 13:44:17 2020 -0800

    x86: Resolve PLT32 reloc aganst local symbol to section

resolved PLT32 relocation against local symbol to section and

commit 2585b7a5ce5830e60a089aa2316a329558902f0c
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Sun Jul 19 06:51:19 2020 -0700

    x86: Change PLT32 reloc against section to PC32

turned PLT32 relocation against section into PC32 relocation.  But these
transformations are valid only for PC-relative relocations.  Add fx_pcrel
check for PC-relative relocations when performing these transformations
to keep PLT32 relocation in `movq $foo@PLT, %rax`.

gas/

PR gas/32196
* config/tc-i386.c (tc_i386_fix_adjustable): Return fixP->fx_pcrel
for PLT32 relocations.
(i386_validate_fix): Turn PLT32 relocation into PC32 relocation
only if fixp->fx_pcrel is set.
* testsuite/gas/i386/reloc32.d: Updated.
* testsuite/gas/i386/reloc64.d: Likewise.
* testsuite/gas/i386/reloc32.s: Add PR gas/32196 test.
* testsuite/gas/i386/reloc64.s: Likewise.

ld/

PR gas/32196
* testsuite/ld-x86-64/plt3.s: New file.
* testsuite/ld-x86-64/x86-64.exp: Run plt3.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
(cherry picked from commit 1b714c14e40f37ea8ea02a4998c4d95f25aff7f3)

gas/config/tc-i386.c
gas/testsuite/gas/i386/reloc32.d
gas/testsuite/gas/i386/reloc32.s
gas/testsuite/gas/i386/reloc64.d
gas/testsuite/gas/i386/reloc64.s
ld/testsuite/ld-x86-64/plt3.s [new file with mode: 0644]
ld/testsuite/ld-x86-64/x86-64.exp

index 9e68ddfe0513267010c3d35ebc467393dc0622b7..bed54f18e8f76c18f5266e75fed3ad66952c334e 100644 (file)
@@ -3806,6 +3806,11 @@ tc_i386_fix_adjustable (fixS *fixP)
       || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
       || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
     return 0;
+  /* Resolve PLT32 relocation against local symbol to section only for
+     PC-relative relocations.  */
+  if (fixP->fx_r_type == BFD_RELOC_386_PLT32
+      || fixP->fx_r_type == BFD_RELOC_X86_64_PLT32)
+    return fixP->fx_pcrel;
   return 1;
 }
 #endif
@@ -18322,8 +18327,12 @@ i386_validate_fix (fixS *fixp)
     {
       /* NB: Commit 292676c1 resolved PLT32 reloc aganst local symbol
         to section.  Since PLT32 relocation must be against symbols,
-        turn such PLT32 relocation into PC32 relocation.  */
+        turn such PLT32 relocation into PC32 relocation.  NB: We can
+        turn PLT32 relocation into PC32 relocation only for PC-relative
+        relocations since non-PC-relative relocations need PLT entries.
+       */
       if (fixp->fx_addsy
+         && fixp->fx_pcrel
          && (fixp->fx_r_type == BFD_RELOC_386_PLT32
              || fixp->fx_r_type == BFD_RELOC_X86_64_PLT32)
          && symbol_section_p (fixp->fx_addsy))
index 263a742022e1478028e5b8949b4fb79bea7e53a0..96c5224393f3ff6be13ff397b941e9cfbcbeb364 100644 (file)
@@ -43,6 +43,7 @@ Disassembly of section \.text:
 .*[    ]+R_386_TLS_LE[         ]+xtrn
 .*[    ]+R_386_TLS_LE_32[      ]+xtrn
 .*[    ]+R_386_TLS_LE_32[      ]+xtrn
+.*[    ]+R_386_PLT32[  ]+ptr
 Disassembly of section \.data:
 #...
 .*[    ]+R_386_32[     ]+xtrn
index e766a3dcc258f9a4bcc74cecf213399a8f63cf41..5616cd57e3f22363902faf93313bee3fb3dd797d 100644 (file)
@@ -162,3 +162,10 @@ bad        .byte   xtrn@tpoff
        .long   xtrn@got + 4
        .long   xtrn@got - 4
 bad    .long   xtrn@plt - .
+
+       .text
+       movl    $ptr@PLT, %eax
+
+       .data
+ptr:
+       .dc.a 0
index a96072d66fb69af56209c36c8fee76ad079b62ad..3a38a26556d3c857a5d11c9a0f2b5283ff61d1d4 100644 (file)
@@ -59,6 +59,7 @@ Disassembly of section \.text:
 .*[    ]+R_X86_64_32[  ]+xtrn
 .*[    ]+R_X86_64_GOT64[       ]+ptr
 .*[    ]+R_X86_64_GOTOFF64[    ]+Ldst
+.*[    ]+R_X86_64_PLT32[       ]+ptr
 Disassembly of section \.data:
 #...
 .*[    ]+R_X86_64_64[  ]+xtrn
index 2293865df20bd2be541021bf5d728cf52eb01987..5c0f4136a09f4e27b817788654d21c393a9bc026 100644 (file)
@@ -229,6 +229,8 @@ bad .long   xtrn@plt - .
        .text
        movabs  $ptr@GOT, %rax
        movabs  $Ldst@GOTOFF, %rdx
+       movq    $ptr@PLT, %rax
+
        .data
 ptr:
        .quad 0
diff --git a/ld/testsuite/ld-x86-64/plt3.s b/ld/testsuite/ld-x86-64/plt3.s
new file mode 100644 (file)
index 0000000..c3a29b5
--- /dev/null
@@ -0,0 +1,27 @@
+       .text
+       .global _start
+_start:
+       movq $foo@PLT, %rax
+       leaq -11(%rip), %rbx
+       leaq (%rbx, %rax), %rax
+       call *%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
+
+foo:
+       ret
+
+       .section        .rodata.str1.1,"aMS",@progbits,1
+.LC0:
+       .string "PASS\n"
+       .section        .note.GNU-stack,"",@progbits
index 09b0c04d01c6145a99971068fb95a0b7cf3482c5..05556e830f31e5ca4b50f31ae2465f1dfabb2818 100644 (file)
@@ -1928,6 +1928,14 @@ if { [isnative] && [check_compiler_available] } {
            "pr32189" \
            "pass.out" \
        ] \
+       [list \
+           "Run plt3" \
+           "$NOPIE_LDFLAGS -nostdlib -nostartfiles" \
+           "" \
+           { plt3.s } \
+           "plt3" \
+           "pass.out" \
+       ] \
     ]
 
     # Run-time tests which require working ifunc attribute support.