This is the mail archive of the
binutils@sourceware.org
mailing list for the binutils project.
Re: PATCH: Convert mov to lea for loading local function address
- From: "H.J. Lu" <hjl dot tools at gmail dot com>
- To: Binutils <binutils at sourceware dot org>
- Cc: Richard Henderson <rth at redhat dot com>
- Date: Fri, 31 Aug 2012 19:48:02 -0700
- Subject: Re: PATCH: Convert mov to lea for loading local function address
- References: <20120831042236.GA19539@intel.com> <CAMe9rOpeuHRDhVXURydFDk4nX1CmZDGSiVjiHERNjk9E0-ovWQ@mail.gmail.com> <CAMe9rOpM3p7-1uWf1kYcA7KcLcFyD3Z-vKgymfSB60idE-m1jw@mail.gmail.com>
On Fri, Aug 31, 2012 at 6:03 PM, H.J. Lu <hjl.tools@gmail.com> wrote:
> On Fri, Aug 31, 2012 at 1:44 PM, H.J. Lu <hjl.tools@gmail.com> wrote:
>> On Thu, Aug 30, 2012 at 9:22 PM, H.J. Lu <hongjiu.lu@intel.com> wrote:
>>> Hi,
>>>
>>> I am checking in this patch to convert move mov to lea for loading local
>>> function address.
>>>
>>> bfd/
>>>
>>> 2012-08-30 H.J. Lu <hongjiu.lu@intel.com>
>>>
>>> * elf32-i386.c (elf_i386_relocate_section): Convert
>>> "mov foo@GOT(%reg), %reg" to "lea foo@GOTOFF(%reg), %reg"
>>> for local symbols.
>>>
>>> * elf64-x86-64.c (elf_x86_64_relocate_section): Convert
>>> "mov foo@GOTPCREL(%rip), %reg" to "lea foo@GOTOFF(%reg), %reg"
>>> for local symbols.
>>>
>>
>> Hi,
>>
>> I checked in this patch to move mov->lea conversion to
>> size_dynamic_sections so that we can remove the unused
>> GOT entries.
>>
>
> It turned out that overdid it for _DYNAMIC. Glibc ld.so has
>
> /* Return the link-time address of _DYNAMIC. Conveniently, this is the
> first element of the GOT. This must be inlined in a function which
> uses global data. */
> static inline ElfW(Addr) __attribute__ ((unused))
> elf_machine_dynamic (void)
>
> We must avoid optimizing _DYNAMIC.
>
I am checking in this.
--
H.J.
---
bfd/
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.
ld/testsuite/
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.
diff --git a/bfd/elf32-i386.c b/bfd/elf32-i386.c
index c9bc694..eab963c 100644
--- a/bfd/elf32-i386.c
+++ b/bfd/elf32-i386.c
@@ -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)
diff --git a/bfd/elf64-x86-64.c b/bfd/elf64-x86-64.c
index 0c37cd3..9edc5ae 100644
--- a/bfd/elf64-x86-64.c
+++ b/bfd/elf64-x86-64.c
@@ -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)
diff --git a/ld/testsuite/ld-i386/i386.exp b/ld/testsuite/ld-i386/i386.exp
index adb852e..f5835ac 100644
--- a/ld/testsuite/ld-i386/i386.exp
+++ b/ld/testsuite/ld-i386/i386.exp
@@ -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
index 0000000..c813e71
--- /dev/null
+++ b/ld/testsuite/ld-i386/mov1.s
@@ -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
index 0000000..d8ce6b8
--- /dev/null
+++ b/ld/testsuite/ld-i386/mov1a.d
@@ -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
index 0000000..583dfc8
--- /dev/null
+++ b/ld/testsuite/ld-i386/mov1b.d
@@ -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
index 0000000..2a11b7a
--- /dev/null
+++ b/ld/testsuite/ld-x86-64/mov1.s
@@ -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
index 0000000..df156be
--- /dev/null
+++ b/ld/testsuite/ld-x86-64/mov1a.d
@@ -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
index 0000000..2f2f6f5
--- /dev/null
+++ b/ld/testsuite/ld-x86-64/mov1b.d
@@ -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
index 0000000..a57e1c1
--- /dev/null
+++ b/ld/testsuite/ld-x86-64/mov1c.d
@@ -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
index 0000000..955d33e
--- /dev/null
+++ b/ld/testsuite/ld-x86-64/mov1d.d
@@ -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
diff --git a/ld/testsuite/ld-x86-64/x86-64.exp
b/ld/testsuite/ld-x86-64/x86-64.exp
index 7eabf39..1ad54e6 100644
--- a/ld/testsuite/ld-x86-64/x86-64.exp
+++ b/ld/testsuite/ld-x86-64/x86-64.exp
@@ -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 } {