MIPS/BFD: Also handle `jalr $0, $25' with R_MIPS_JALR
authorMaciej W. Rozycki <macro@imgtec.com>
Tue, 28 Feb 2017 00:14:08 +0000 (00:14 +0000)
committerMaciej W. Rozycki <macro@imgtec.com>
Tue, 28 Feb 2017 00:22:36 +0000 (00:22 +0000)
Interpret the `jalr $0, $25' instruction encoding with an R_MIPS_JALR
relocation attached as an alias to `jr $25' and convert the jump to an
equivalent branch where possible, consequently covering the MIPSr6
architecture for the purpose of this optimization too.

bfd/
* elfxx-mips.c (mips_elf_perform_relocation): Also handle the
`jalr $0, $25' instruction encoding.

gas/
* testsuite/gas/mips/jalr4.s: Add `jalr $0, $25' instructions.
* testsuite/gas/mips/jalr4.d: Adjust accordingly.  Remove MIPSr6
encoding patterns.
* testsuite/gas/mips/jalr4-n64.d: Likewise.
* testsuite/gas/mips/mipsr6@jalr4.d: New test.
* testsuite/gas/mips/mipsr6@jalr4-n32.d: New test.
* testsuite/gas/mips/mipsr6@jalr4-n64.d: New test.

ld/
* testsuite/ld-mips-elf/jalr4.dd: Adjust for `jalr $0, $25'
instructions.
* testsuite/ld-mips-elf/jalr4-r6.dd: New test.
* testsuite/ld-mips-elf/mips-elf.exp: Run the new test.

13 files changed:
bfd/ChangeLog
bfd/elfxx-mips.c
gas/ChangeLog
gas/testsuite/gas/mips/jalr4-n64.d
gas/testsuite/gas/mips/jalr4.d
gas/testsuite/gas/mips/jalr4.s
gas/testsuite/gas/mips/mipsr6@jalr4-n32.d [new file with mode: 0644]
gas/testsuite/gas/mips/mipsr6@jalr4-n64.d [new file with mode: 0644]
gas/testsuite/gas/mips/mipsr6@jalr4.d [new file with mode: 0644]
ld/ChangeLog
ld/testsuite/ld-mips-elf/jalr4-r6.dd [new file with mode: 0644]
ld/testsuite/ld-mips-elf/jalr4.dd
ld/testsuite/ld-mips-elf/mips-elf.exp

index 0f8d9eec6faaf0cc60631fd000e0f086f582c6fd..652c074f7316c427dc23288cef717f40c413d23e 100644 (file)
@@ -1,3 +1,8 @@
+2017-02-28  Maciej W. Rozycki  <macro@imgtec.com>
+
+       * elfxx-mips.c (mips_elf_perform_relocation): Also handle the
+       `jalr $0, $25' instruction encoding.
+
 2017-02-27  Nick Clifton  <nickc@redhat.com>
 
        PR ld/21180
index 9693442fbf0e8c2c1f1bb58c4151e65811ea211d..7a1d5f848bbbd15748b3a6de17d31b97a04188cd 100644 (file)
@@ -6465,13 +6465,13 @@ mips_elf_perform_relocation (struct bfd_link_info *info,
       && !cross_mode_jump_p
       && ((JAL_TO_BAL_P (input_bfd)
           && r_type == R_MIPS_26
-          && (x >> 26) == 0x3)         /* jal addr */
+          && (x >> 26) == 0x3)                 /* jal addr */
          || (JALR_TO_BAL_P (input_bfd)
              && r_type == R_MIPS_JALR
-             && x == 0x0320f809)       /* jalr t9 */
+             && x == 0x0320f809)               /* jalr t9 */
          || (JR_TO_B_P (input_bfd)
              && r_type == R_MIPS_JALR
-             && x == 0x03200008)))     /* jr t9 */
+             && (x & ~1) == 0x03200008)))      /* jr t9 / jalr zero, t9 */
     {
       bfd_vma addr;
       bfd_vma dest;
@@ -6488,7 +6488,7 @@ mips_elf_perform_relocation (struct bfd_link_info *info,
       off = dest - addr;
       if (off <= 0x1ffff && off >= -0x20000)
        {
-         if (x == 0x03200008)  /* jr t9 */
+         if ((x & ~1) == 0x03200008)           /* jr t9 / jalr zero, t9 */
            x = 0x10000000 | (((bfd_vma) off >> 2) & 0xffff);   /* b addr */
          else
            x = 0x04110000 | (((bfd_vma) off >> 2) & 0xffff);   /* bal addr */
index b2908dc61c355783869f3a81d4005a7d832101ab..ec6dbf7d66813d42d2bdc835693854fe9aaf1403 100644 (file)
@@ -1,3 +1,13 @@
+2017-02-28  Maciej W. Rozycki  <macro@imgtec.com>
+
+       * testsuite/gas/mips/jalr4.s: Add `jalr $0, $25' instructions.
+       * testsuite/gas/mips/jalr4.d: Adjust accordingly.  Remove MIPSr6
+       encoding patterns.
+       * testsuite/gas/mips/jalr4-n64.d: Likewise.
+       * testsuite/gas/mips/mipsr6@jalr4.d: New test.
+       * testsuite/gas/mips/mipsr6@jalr4-n32.d: New test.
+       * testsuite/gas/mips/mipsr6@jalr4-n64.d: New test.
+
 2017-02-25  Alan Modra  <amodra@gmail.com>
 
        * testsuite/gas/elf/strtab.s: Don't put directives on first
index b61fc232949afabb47825cc3bb558673d4eee453..99a334d3ebb28bf4addec64e1ff535a8d3cd59f3 100644 (file)
@@ -11,7 +11,12 @@ Disassembly of section \.text:
 [      ]*[0-9a-f]+: R_MIPS_NONE        \*ABS\*
 [      ]*[0-9a-f]+: R_MIPS_NONE        \*ABS\*
 [0-9a-f]+ <[^>]*> 00000000     nop
-[0-9a-f]+ <[^>]*> 0320000[89]  jr      t9
+[0-9a-f]+ <[^>]*> 03200009     jalr    zero,t9
+[      ]*[0-9a-f]+: R_MIPS_JALR        bar0
+[      ]*[0-9a-f]+: R_MIPS_NONE        \*ABS\*
+[      ]*[0-9a-f]+: R_MIPS_NONE        \*ABS\*
+[0-9a-f]+ <[^>]*> 00000000     nop
+[0-9a-f]+ <[^>]*> 03200008     jr      t9
 [      ]*[0-9a-f]+: R_MIPS_JALR        bar0
 [      ]*[0-9a-f]+: R_MIPS_NONE        \*ABS\*
 [      ]*[0-9a-f]+: R_MIPS_NONE        \*ABS\*
@@ -21,7 +26,12 @@ Disassembly of section \.text:
 [      ]*[0-9a-f]+: R_MIPS_NONE        \*ABS\*
 [      ]*[0-9a-f]+: R_MIPS_NONE        \*ABS\*
 [0-9a-f]+ <[^>]*> 00000000     nop
-[0-9a-f]+ <[^>]*> 0320000[89]  jr      t9
+[0-9a-f]+ <[^>]*> 03200009     jalr    zero,t9
+[      ]*[0-9a-f]+: R_MIPS_JALR        bar1
+[      ]*[0-9a-f]+: R_MIPS_NONE        \*ABS\*
+[      ]*[0-9a-f]+: R_MIPS_NONE        \*ABS\*
+[0-9a-f]+ <[^>]*> 00000000     nop
+[0-9a-f]+ <[^>]*> 03200008     jr      t9
 [      ]*[0-9a-f]+: R_MIPS_JALR        bar1
 [      ]*[0-9a-f]+: R_MIPS_NONE        \*ABS\*
 [      ]*[0-9a-f]+: R_MIPS_NONE        \*ABS\*
@@ -31,7 +41,12 @@ Disassembly of section \.text:
 [      ]*[0-9a-f]+: R_MIPS_NONE        \*ABS\*
 [      ]*[0-9a-f]+: R_MIPS_NONE        \*ABS\*
 [0-9a-f]+ <[^>]*> 00000000     nop
-[0-9a-f]+ <[^>]*> 0320000[89]  jr      t9
+[0-9a-f]+ <[^>]*> 03200009     jalr    zero,t9
+[      ]*[0-9a-f]+: R_MIPS_JALR        bar2
+[      ]*[0-9a-f]+: R_MIPS_NONE        \*ABS\*
+[      ]*[0-9a-f]+: R_MIPS_NONE        \*ABS\*
+[0-9a-f]+ <[^>]*> 00000000     nop
+[0-9a-f]+ <[^>]*> 03200008     jr      t9
 [      ]*[0-9a-f]+: R_MIPS_JALR        bar2
 [      ]*[0-9a-f]+: R_MIPS_NONE        \*ABS\*
 [      ]*[0-9a-f]+: R_MIPS_NONE        \*ABS\*
index 1a1afdda0a395d5623304b96eb92c35930add9ec..4cafd41a931d1e7d33227bbae3d4c676e818b4e3 100644 (file)
@@ -9,19 +9,28 @@ Disassembly of section \.text:
 [0-9a-f]+ <[^>]*> 0320f809     jalr    t9
 [      ]*[0-9a-f]+: R_MIPS_JALR        bar0
 [0-9a-f]+ <[^>]*> 00000000     nop
-[0-9a-f]+ <[^>]*> 0320000[89]  jr      t9
+[0-9a-f]+ <[^>]*> 03200009     jalr    zero,t9
+[      ]*[0-9a-f]+: R_MIPS_JALR        bar0
+[0-9a-f]+ <[^>]*> 00000000     nop
+[0-9a-f]+ <[^>]*> 03200008     jr      t9
 [      ]*[0-9a-f]+: R_MIPS_JALR        bar0
 [0-9a-f]+ <[^>]*> 00000000     nop
 [0-9a-f]+ <[^>]*> 0320f809     jalr    t9
 [      ]*[0-9a-f]+: R_MIPS_JALR        bar1
 [0-9a-f]+ <[^>]*> 00000000     nop
-[0-9a-f]+ <[^>]*> 0320000[89]  jr      t9
+[0-9a-f]+ <[^>]*> 03200009     jalr    zero,t9
+[      ]*[0-9a-f]+: R_MIPS_JALR        bar1
+[0-9a-f]+ <[^>]*> 00000000     nop
+[0-9a-f]+ <[^>]*> 03200008     jr      t9
 [      ]*[0-9a-f]+: R_MIPS_JALR        bar1
 [0-9a-f]+ <[^>]*> 00000000     nop
 [0-9a-f]+ <[^>]*> 0320f809     jalr    t9
 [      ]*[0-9a-f]+: R_MIPS_JALR        bar2
 [0-9a-f]+ <[^>]*> 00000000     nop
-[0-9a-f]+ <[^>]*> 0320000[89]  jr      t9
+[0-9a-f]+ <[^>]*> 03200009     jalr    zero,t9
+[      ]*[0-9a-f]+: R_MIPS_JALR        bar2
+[0-9a-f]+ <[^>]*> 00000000     nop
+[0-9a-f]+ <[^>]*> 03200008     jr      t9
 [      ]*[0-9a-f]+: R_MIPS_JALR        bar2
 [0-9a-f]+ <[^>]*> 00000000     nop
        \.\.\.
index 4ad6f264d86bd5063b0f455d0fb19a50f1a4082c..25e863452aa8dfdae848e11d351dd6a565e11567 100644 (file)
@@ -8,14 +8,20 @@ foo:
        .reloc  1f, R_MIPS_JALR, bar0
 1:     jalr    $25
        .reloc  1f, R_MIPS_JALR, bar0
+1:     jalr    $0, $25
+       .reloc  1f, R_MIPS_JALR, bar0
 1:     jr      $25
        .reloc  1f, R_MIPS_JALR, bar1
 1:     jalr    $25
        .reloc  1f, R_MIPS_JALR, bar1
+1:     jalr    $0, $25
+       .reloc  1f, R_MIPS_JALR, bar1
 1:     jr      $25
        .reloc  1f, R_MIPS_JALR, bar2
 1:     jalr    $25
        .reloc  1f, R_MIPS_JALR, bar2
+1:     jalr    $0, $25
+       .reloc  1f, R_MIPS_JALR, bar2
 1:     jr      $25
        .end    foo
 
diff --git a/gas/testsuite/gas/mips/mipsr6@jalr4-n32.d b/gas/testsuite/gas/mips/mipsr6@jalr4-n32.d
new file mode 100644 (file)
index 0000000..0a17cbf
--- /dev/null
@@ -0,0 +1,5 @@
+#objdump: -dr --prefix-addresses --show-raw-insn
+#name: MIPS JALR reloc unaligned/cross-mode (n32)
+#as: -n32
+#source: jalr4.s
+#dump: mipsr6@jalr4.d
diff --git a/gas/testsuite/gas/mips/mipsr6@jalr4-n64.d b/gas/testsuite/gas/mips/mipsr6@jalr4-n64.d
new file mode 100644 (file)
index 0000000..b7424d6
--- /dev/null
@@ -0,0 +1,57 @@
+#objdump: -dr --prefix-addresses --show-raw-insn
+#name: MIPS JALR reloc unaligned/cross-mode (n64)
+#as: -64
+#source: jalr4.s
+
+.*: +file format .*mips.*
+
+Disassembly of section \.text:
+[0-9a-f]+ <[^>]*> 0320f809     jalr    t9
+[      ]*[0-9a-f]+: R_MIPS_JALR        bar0
+[      ]*[0-9a-f]+: R_MIPS_NONE        \*ABS\*
+[      ]*[0-9a-f]+: R_MIPS_NONE        \*ABS\*
+[0-9a-f]+ <[^>]*> 00000000     nop
+[0-9a-f]+ <[^>]*> 03200009     jr      t9
+[      ]*[0-9a-f]+: R_MIPS_JALR        bar0
+[      ]*[0-9a-f]+: R_MIPS_NONE        \*ABS\*
+[      ]*[0-9a-f]+: R_MIPS_NONE        \*ABS\*
+[0-9a-f]+ <[^>]*> 00000000     nop
+[0-9a-f]+ <[^>]*> 03200009     jr      t9
+[      ]*[0-9a-f]+: R_MIPS_JALR        bar0
+[      ]*[0-9a-f]+: R_MIPS_NONE        \*ABS\*
+[      ]*[0-9a-f]+: R_MIPS_NONE        \*ABS\*
+[0-9a-f]+ <[^>]*> 00000000     nop
+[0-9a-f]+ <[^>]*> 0320f809     jalr    t9
+[      ]*[0-9a-f]+: R_MIPS_JALR        bar1
+[      ]*[0-9a-f]+: R_MIPS_NONE        \*ABS\*
+[      ]*[0-9a-f]+: R_MIPS_NONE        \*ABS\*
+[0-9a-f]+ <[^>]*> 00000000     nop
+[0-9a-f]+ <[^>]*> 03200009     jr      t9
+[      ]*[0-9a-f]+: R_MIPS_JALR        bar1
+[      ]*[0-9a-f]+: R_MIPS_NONE        \*ABS\*
+[      ]*[0-9a-f]+: R_MIPS_NONE        \*ABS\*
+[0-9a-f]+ <[^>]*> 00000000     nop
+[0-9a-f]+ <[^>]*> 03200009     jr      t9
+[      ]*[0-9a-f]+: R_MIPS_JALR        bar1
+[      ]*[0-9a-f]+: R_MIPS_NONE        \*ABS\*
+[      ]*[0-9a-f]+: R_MIPS_NONE        \*ABS\*
+[0-9a-f]+ <[^>]*> 00000000     nop
+[0-9a-f]+ <[^>]*> 0320f809     jalr    t9
+[      ]*[0-9a-f]+: R_MIPS_JALR        bar2
+[      ]*[0-9a-f]+: R_MIPS_NONE        \*ABS\*
+[      ]*[0-9a-f]+: R_MIPS_NONE        \*ABS\*
+[0-9a-f]+ <[^>]*> 00000000     nop
+[0-9a-f]+ <[^>]*> 03200009     jr      t9
+[      ]*[0-9a-f]+: R_MIPS_JALR        bar2
+[      ]*[0-9a-f]+: R_MIPS_NONE        \*ABS\*
+[      ]*[0-9a-f]+: R_MIPS_NONE        \*ABS\*
+[0-9a-f]+ <[^>]*> 00000000     nop
+[0-9a-f]+ <[^>]*> 03200009     jr      t9
+[      ]*[0-9a-f]+: R_MIPS_JALR        bar2
+[      ]*[0-9a-f]+: R_MIPS_NONE        \*ABS\*
+[      ]*[0-9a-f]+: R_MIPS_NONE        \*ABS\*
+[0-9a-f]+ <[^>]*> 00000000     nop
+       \.\.\.
+       \.\.\.
+       \.\.\.
+       \.\.\.
diff --git a/gas/testsuite/gas/mips/mipsr6@jalr4.d b/gas/testsuite/gas/mips/mipsr6@jalr4.d
new file mode 100644 (file)
index 0000000..9b49339
--- /dev/null
@@ -0,0 +1,39 @@
+#objdump: -dr --prefix-addresses --show-raw-insn
+#name: MIPS JALR reloc unaligned/cross-mode (o32)
+#as: -32
+#source: jalr4.s
+
+.*: +file format .*mips.*
+
+Disassembly of section \.text:
+[0-9a-f]+ <[^>]*> 0320f809     jalr    t9
+[      ]*[0-9a-f]+: R_MIPS_JALR        bar0
+[0-9a-f]+ <[^>]*> 00000000     nop
+[0-9a-f]+ <[^>]*> 03200009     jr      t9
+[      ]*[0-9a-f]+: R_MIPS_JALR        bar0
+[0-9a-f]+ <[^>]*> 00000000     nop
+[0-9a-f]+ <[^>]*> 03200009     jr      t9
+[      ]*[0-9a-f]+: R_MIPS_JALR        bar0
+[0-9a-f]+ <[^>]*> 00000000     nop
+[0-9a-f]+ <[^>]*> 0320f809     jalr    t9
+[      ]*[0-9a-f]+: R_MIPS_JALR        bar1
+[0-9a-f]+ <[^>]*> 00000000     nop
+[0-9a-f]+ <[^>]*> 03200009     jr      t9
+[      ]*[0-9a-f]+: R_MIPS_JALR        bar1
+[0-9a-f]+ <[^>]*> 00000000     nop
+[0-9a-f]+ <[^>]*> 03200009     jr      t9
+[      ]*[0-9a-f]+: R_MIPS_JALR        bar1
+[0-9a-f]+ <[^>]*> 00000000     nop
+[0-9a-f]+ <[^>]*> 0320f809     jalr    t9
+[      ]*[0-9a-f]+: R_MIPS_JALR        bar2
+[0-9a-f]+ <[^>]*> 00000000     nop
+[0-9a-f]+ <[^>]*> 03200009     jr      t9
+[      ]*[0-9a-f]+: R_MIPS_JALR        bar2
+[0-9a-f]+ <[^>]*> 00000000     nop
+[0-9a-f]+ <[^>]*> 03200009     jr      t9
+[      ]*[0-9a-f]+: R_MIPS_JALR        bar2
+[0-9a-f]+ <[^>]*> 00000000     nop
+       \.\.\.
+       \.\.\.
+       \.\.\.
+       \.\.\.
index a4f2406f21a72e312f2f2f3139053191977a8495..ceee695c0062217742f822a606f96300ed9dd6dd 100644 (file)
@@ -1,3 +1,10 @@
+2017-02-28  Maciej W. Rozycki  <macro@imgtec.com>
+
+       * testsuite/ld-mips-elf/jalr4.dd: Adjust for `jalr $0, $25'
+       instructions.
+       * testsuite/ld-mips-elf/jalr4-r6.dd: New test.
+       * testsuite/ld-mips-elf/mips-elf.exp: Run the new test.
+
 2017-02-27   Georg-Johann Lay  <gjl@gcc.gnu.org>
 
        PR target/20849
diff --git a/ld/testsuite/ld-mips-elf/jalr4-r6.dd b/ld/testsuite/ld-mips-elf/jalr4-r6.dd
new file mode 100644 (file)
index 0000000..f587513
--- /dev/null
@@ -0,0 +1,25 @@
+.*: +file format .*mips.*
+
+Disassembly of section \.text:
+[0-9a-f]+ <[^>]*> 04110017     bal     0+000060 <bar0>
+[0-9a-f]+ <[^>]*> 00000000     nop
+[0-9a-f]+ <[^>]*> 10000015     b       0+000060 <bar0>
+[0-9a-f]+ <[^>]*> 00000000     nop
+[0-9a-f]+ <[^>]*> 10000013     b       0+000060 <bar0>
+[0-9a-f]+ <[^>]*> 00000000     nop
+[0-9a-f]+ <[^>]*> 0320f809     jalr    t9
+[0-9a-f]+ <[^>]*> 00000000     nop
+[0-9a-f]+ <[^>]*> 03200009     jr      t9
+[0-9a-f]+ <[^>]*> 00000000     nop
+[0-9a-f]+ <[^>]*> 03200009     jr      t9
+[0-9a-f]+ <[^>]*> 00000000     nop
+[0-9a-f]+ <[^>]*> 0320f809     jalr    t9
+[0-9a-f]+ <[^>]*> 00000000     nop
+[0-9a-f]+ <[^>]*> 03200009     jr      t9
+[0-9a-f]+ <[^>]*> 00000000     nop
+[0-9a-f]+ <[^>]*> 03200009     jr      t9
+[0-9a-f]+ <[^>]*> 00000000     nop
+       \.\.\.
+       \.\.\.
+       \.\.\.
+       \.\.\.
index 8e4175647f2a156b36330f39c63f3025ebeeff56..56ee0940b74b2f4a8054e6286fabe0e48e41467a 100644 (file)
@@ -1,16 +1,22 @@
 .*: +file format .*mips.*
 
 Disassembly of section \.text:
-[0-9a-f]+ <[^>]*> 0411000f     bal     0+000040 <bar0>
+[0-9a-f]+ <[^>]*> 04110017     bal     0+000060 <bar0>
 [0-9a-f]+ <[^>]*> 00000000     nop
-[0-9a-f]+ <[^>]*> 1000000d     b       0+000040 <bar0>
+[0-9a-f]+ <[^>]*> 10000015     b       0+000060 <bar0>
+[0-9a-f]+ <[^>]*> 00000000     nop
+[0-9a-f]+ <[^>]*> 10000013     b       0+000060 <bar0>
 [0-9a-f]+ <[^>]*> 00000000     nop
 [0-9a-f]+ <[^>]*> 0320f809     jalr    t9
 [0-9a-f]+ <[^>]*> 00000000     nop
+[0-9a-f]+ <[^>]*> 03200009     jalr    zero,t9
+[0-9a-f]+ <[^>]*> 00000000     nop
 [0-9a-f]+ <[^>]*> 03200008     jr      t9
 [0-9a-f]+ <[^>]*> 00000000     nop
 [0-9a-f]+ <[^>]*> 0320f809     jalr    t9
 [0-9a-f]+ <[^>]*> 00000000     nop
+[0-9a-f]+ <[^>]*> 03200009     jalr    zero,t9
+[0-9a-f]+ <[^>]*> 00000000     nop
 [0-9a-f]+ <[^>]*> 03200008     jr      t9
 [0-9a-f]+ <[^>]*> 00000000     nop
        \.\.\.
index dab0e0fbab95672f9584af8e8586d2883e7c125a..73d59697a4636fb0bcb313497903a0b1ec771696 100644 (file)
@@ -1006,6 +1006,17 @@ foreach { abi } $abis {
            "$abi_asflags($abi)" \
            [list ../../../gas/testsuite/gas/mips/jalr4.s] \
            [list "objdump {-d --prefix-addresses --show-raw-insn} jalr4.dd"] \
+           "jalr4-${abi}"] \
+       [list \
+           "MIPSr6 JALR reloc unaligned/cross-mode link test ($abi)" \
+           "$abi_ldflags($abi) -T jalr3.ld" "" \
+           [string map \
+               [list -32 "-mips32r6 -32" \
+                   -n32 "-mips64r6 -n32" \
+                   -64 "-mips64r6 -64"] \
+               [regsub -- -march=from-abi $abi_asflags($abi) ""]] \
+           {../../../gas/testsuite/gas/mips/jalr4.s} \
+           {{objdump {-d --prefix-addresses --show-raw-insn} jalr4-r6.dd}} \
            "jalr4-${abi}"]]
 }