Don't optimize relocation against _DYNAMIC
authorH.J. Lu <hjl.tools@gmail.com>
Sat, 1 Sep 2012 02:50:14 +0000 (02:50 +0000)
committerH.J. Lu <hjl.tools@gmail.com>
Sat, 1 Sep 2012 02:50:14 +0000 (02:50 +0000)
bfd/

* elf32-i386.c (elf_i386_convert_mov_to_lea): Don't optimize
_DYNAMIC.
* elf64-x86-64.c (elf_x86_64_convert_mov_to_lea): Likewise.

ld/testsuite/

* ld-i386/i386.exp: Run mov1a, mov1b.
* ld-x86-64/x86-64.exp: Run mov1a, mov1b, mov1c, mov1d.

* ld-i386/mov1.s: New file.
* ld-i386/mov1a.d: Likewise.
* ld-i386/mov1b.d: Likewise.
* ld-x86-64/mov1.s: Likewise.
* ld-x86-64/mov1a.d: Likewise.
* ld-x86-64/mov1b.d: Likewise.
* ld-x86-64/mov1c.d: Likewise.
* ld-x86-64/mov1d.d: Likewise.

14 files changed:
bfd/ChangeLog
bfd/elf32-i386.c
bfd/elf64-x86-64.c
ld/testsuite/ChangeLog
ld/testsuite/ld-i386/i386.exp
ld/testsuite/ld-i386/mov1.s [new file with mode: 0644]
ld/testsuite/ld-i386/mov1a.d [new file with mode: 0644]
ld/testsuite/ld-i386/mov1b.d [new file with mode: 0644]
ld/testsuite/ld-x86-64/mov1.s [new file with mode: 0644]
ld/testsuite/ld-x86-64/mov1a.d [new file with mode: 0644]
ld/testsuite/ld-x86-64/mov1b.d [new file with mode: 0644]
ld/testsuite/ld-x86-64/mov1c.d [new file with mode: 0644]
ld/testsuite/ld-x86-64/mov1d.d [new file with mode: 0644]
ld/testsuite/ld-x86-64/x86-64.exp

index 71933e68797ed8cc5600869cd6abcb34efe97df7..d545c1d0e1eb333514a338ffd44485e53e54d64f 100644 (file)
@@ -1,3 +1,9 @@
+2012-08-31  H.J. Lu  <hongjiu.lu@intel.com>
+
+       * elf32-i386.c (elf_i386_convert_mov_to_lea): Don't optimize
+       _DYNAMIC.
+       * elf64-x86-64.c (elf_x86_64_convert_mov_to_lea): Likewise.
+
 2012-08-31  H.J. Lu  <hongjiu.lu@intel.com>
 
        * elf32-i386.c (elf_i386_convert_mov_to_lea): New.
index c9bc6944c4f3cc36c781baae49ce6abda3c694a3..eab963c2b4ca95850c1163668b25d560729d8c52 100644 (file)
@@ -2631,9 +2631,11 @@ elf_i386_convert_mov_to_lea (bfd *abfd, asection *sec,
             || h->root.type == bfd_link_hash_warning)
        h = (struct elf_link_hash_entry *) h->root.u.i.link;
 
-      /* STT_GNU_IFUNC must keep R_386_GOT32 relocation.  */
+      /* STT_GNU_IFUNC must keep R_386_GOT32 relocation.  We also avoid
+        optimizing _DYNAMIC since ld.so may use its link-time address.  */
       if (h->def_regular
          && h->type != STT_GNU_IFUNC
+         && strcmp (h->root.root.string, "_DYNAMIC") != 0
          && SYMBOL_REFERENCES_LOCAL (link_info, h)
          && bfd_get_8 (input_bfd,
                        contents + irel->r_offset - 2) == 0x8b)
index 0c37cd3d2543d4530acec840acddf9afe06d8d29..9edc5aea3bc42bf0b93fdeec1b99b2e1f923c176 100644 (file)
@@ -2667,9 +2667,12 @@ elf_x86_64_convert_mov_to_lea (bfd *abfd, asection *sec,
             || h->root.type == bfd_link_hash_warning)
        h = (struct elf_link_hash_entry *) h->root.u.i.link;
 
-      /* STT_GNU_IFUNC must keep R_X86_64_GOTPCREL relocation.  */
+      /* STT_GNU_IFUNC must keep R_X86_64_GOTPCREL relocation.  We also
+        avoid optimizing _DYNAMIC since ld.so may use its link-time
+        address.  */
       if (h->def_regular
          && h->type != STT_GNU_IFUNC
+         && strcmp (h->root.root.string, "_DYNAMIC") != 0
          && SYMBOL_REFERENCES_LOCAL (link_info, h)
          && bfd_get_8 (input_bfd,
                        contents + irel->r_offset - 2) == 0x8b)
index 35740cc75e31675dad39718a1ebaeef992730657..dc9223daa6ffa8c9457cbaf5c9467558ea17be5b 100644 (file)
@@ -1,3 +1,17 @@
+2012-08-31  H.J. Lu  <hongjiu.lu@intel.com>
+
+       * ld-i386/i386.exp: Run mov1a, mov1b.
+       * ld-x86-64/x86-64.exp: Run mov1a, mov1b, mov1c, mov1d.
+
+       * ld-i386/mov1.s: New file.
+       * ld-i386/mov1a.d: Likewise.
+       * ld-i386/mov1b.d: Likewise.
+       * ld-x86-64/mov1.s: Likewise.
+       * ld-x86-64/mov1a.d: Likewise.
+       * ld-x86-64/mov1b.d: Likewise.
+       * ld-x86-64/mov1c.d: Likewise.
+       * ld-x86-64/mov1d.d: Likewise.
+
 2012-08-31  H.J. Lu  <hongjiu.lu@intel.com>
 
        * ld-i386/i386.exp: Run lea1d, lea1f, lea1f.
index adb852e6805f7e08dfeb0689ac8e397c041ffdf8..f5835accc3f236c01166a1abd57b44041506f2f6 100644 (file)
@@ -239,6 +239,8 @@ run_dump_test "lea1c"
 run_dump_test "lea1d"
 run_dump_test "lea1e"
 run_dump_test "lea1f"
+run_dump_test "mov1a"
+run_dump_test "mov1b"
 
 if { !([istarget "i?86-*-linux*"]
        || [istarget "i?86-*-gnu*"]
diff --git a/ld/testsuite/ld-i386/mov1.s b/ld/testsuite/ld-i386/mov1.s
new file mode 100644 (file)
index 0000000..c813e71
--- /dev/null
@@ -0,0 +1,6 @@
+       .text
+       .globl  _start
+       .type   _start, @function
+_start:
+       movl    _DYNAMIC@GOT(%ecx), %eax
+       .size   _start, .-_start
diff --git a/ld/testsuite/ld-i386/mov1a.d b/ld/testsuite/ld-i386/mov1a.d
new file mode 100644 (file)
index 0000000..d8ce6b8
--- /dev/null
@@ -0,0 +1,13 @@
+#source: mov1.s
+#as: --32
+#ld: -shared -melf_i386
+#objdump: -dw
+
+.*: +file format .*
+
+
+Disassembly of section .text:
+
+#...
+[      ]*[a-f0-9]+:    8b 81 ([0-9a-f]{2} ){4} *       mov    -0x[a-f0-9]+\(%ecx\),%eax
+#pass
diff --git a/ld/testsuite/ld-i386/mov1b.d b/ld/testsuite/ld-i386/mov1b.d
new file mode 100644 (file)
index 0000000..583dfc8
--- /dev/null
@@ -0,0 +1,13 @@
+#source: mov1.s
+#as: --32
+#ld: -pie -melf_i386
+#objdump: -dw
+
+.*: +file format .*
+
+
+Disassembly of section .text:
+
+#...
+[      ]*[a-f0-9]+:    8b 81 ([0-9a-f]{2} ){4} *       mov    -0x[a-f0-9]+\(%ecx\),%eax
+#pass
diff --git a/ld/testsuite/ld-x86-64/mov1.s b/ld/testsuite/ld-x86-64/mov1.s
new file mode 100644 (file)
index 0000000..2a11b7a
--- /dev/null
@@ -0,0 +1,6 @@
+       .text
+       .globl  _start
+       .type   _start, @function
+_start:
+       movq    _DYNAMIC@GOTPCREL(%rip), %rax
+       .size   _start, .-_start
diff --git a/ld/testsuite/ld-x86-64/mov1a.d b/ld/testsuite/ld-x86-64/mov1a.d
new file mode 100644 (file)
index 0000000..df156be
--- /dev/null
@@ -0,0 +1,13 @@
+#source: mov1.s
+#as: --64
+#ld: -shared -melf_x86_64
+#objdump: -dw
+
+.*: +file format .*
+
+
+Disassembly of section .text:
+
+#...
+[      ]*[a-f0-9]+:    48 8b 05 ([0-9a-f]{2} ){4} *    mov    0x[a-f0-9]+\(%rip\),%rax        # [a-f0-9]+ <_DYNAMIC\+0x[a-f0-9]+>
+#pass
diff --git a/ld/testsuite/ld-x86-64/mov1b.d b/ld/testsuite/ld-x86-64/mov1b.d
new file mode 100644 (file)
index 0000000..2f2f6f5
--- /dev/null
@@ -0,0 +1,13 @@
+#source: mov1.s
+#as: --64
+#ld: -pie -melf_x86_64
+#objdump: -dw
+
+.*: +file format .*
+
+
+Disassembly of section .text:
+
+#...
+[      ]*[a-f0-9]+:    48 8b 05 ([0-9a-f]{2} ){4} *    mov    0x[a-f0-9]+\(%rip\),%rax        # [a-f0-9]+ <_DYNAMIC\+0x[a-f0-9]+>
+#pass
diff --git a/ld/testsuite/ld-x86-64/mov1c.d b/ld/testsuite/ld-x86-64/mov1c.d
new file mode 100644 (file)
index 0000000..a57e1c1
--- /dev/null
@@ -0,0 +1,13 @@
+#source: mov1.s
+#as: --x32
+#ld: -shared -melf32_x86_64
+#objdump: -dw
+
+.*: +file format .*
+
+
+Disassembly of section .text:
+
+#...
+[      ]*[a-f0-9]+:    48 8b 05 ([0-9a-f]{2} ){4} *    mov    0x[a-f0-9]+\(%rip\),%rax        # [a-f0-9]+ <_DYNAMIC\+0x[a-f0-9]+>
+#pass
diff --git a/ld/testsuite/ld-x86-64/mov1d.d b/ld/testsuite/ld-x86-64/mov1d.d
new file mode 100644 (file)
index 0000000..955d33e
--- /dev/null
@@ -0,0 +1,13 @@
+#source: mov1.s
+#as: --x32
+#ld: -pie -melf32_x86_64
+#objdump: -dw
+
+.*: +file format .*
+
+
+Disassembly of section .text:
+
+#...
+[      ]*[a-f0-9]+:    48 8b 05 ([0-9a-f]{2} ){4} *    mov    0x[a-f0-9]+\(%rip\),%rax        # [a-f0-9]+ <_DYNAMIC\+0x[a-f0-9]+>
+#pass
index 7eabf3939036325c921464b831b6e386a1a17e32..1ad54e6fac869f757e0878eabc0108fc5c091723 100644 (file)
@@ -288,6 +288,10 @@ run_dump_test "lea1i"
 run_dump_test "lea1j"
 run_dump_test "lea1k"
 run_dump_test "lea1l"
+run_dump_test "mov1a"
+run_dump_test "mov1b"
+run_dump_test "mov1c"
+run_dump_test "mov1d"
 
 # Must be native with the C compiler
 if { [isnative] && [which $CC] != 0 } {