回复: [PATCH v4 04/13] LoongArch: ABI Implementation

Message ID 628CAA45.0420B4.01722@loongson.cn
State Not applicable
Headers
Series 回复: [PATCH v4 04/13] LoongArch: ABI Implementation |

Checks

Context Check Description
dj/TryBot-apply_patch fail Patch failed to apply to master at the time it was sent
dj/TryBot-32bit fail Patch series failed to apply

Commit Message

caiyinyu May 24, 2022, 9:49 a.m. UTC
  从 Windows 版邮件发送

发件人: Fangrui Song
发送时间: 2022年5月9日 16:24
收件人: caiyinyu
抄送: libc-alpha@sourceware.org; xuchenghua@loongson.cn; joseph_myers@mentor.com
主题: Re: [PATCH v4 04/13] LoongArch: ABI Implementation

>+/* Runtime address of .got */
>+#define _GLOBAL_OFFSET_TABLE_ \
>+  ({ \
>+    ElfW (Addr) * r; \
>+    asm("la.pcrel %0, _GLOBAL_OFFSET_TABLE_" : "=r"(r)); \
>+    r; \
>+  })
>+
>+/* Return the link-time address of _DYNAMIC.  */
>+static inline ElfW (Addr) elf_machine_dynamic (void)
>+{
>+  return _GLOBAL_OFFSET_TABLE_[0];
>+}

_GLOBAL_OFFSET_TABLE_[0] == link_time_dynamic is not recommended.
See
https://maskray.me/blog/2021-09-05-build-glibc-with-lld#global_offset_table_0
aarch64/riscv/x86_64/etc have been updated to use the preferred
approach.

Fixed.





>
  

Comments

Fangrui Song May 24, 2022, 7:55 p.m. UTC | #1
On 2022-05-24, caiyinyu@loongson.cn wrote:
>
>
>从 Windows 版邮件发送
>
>发件人: Fangrui Song
>发送时间: 2022年5月9日 16:24
>收件人: caiyinyu
>抄送: libc-alpha@sourceware.org; xuchenghua@loongson.cn; joseph_myers@mentor.com
>主题: Re: [PATCH v4 04/13] LoongArch: ABI Implementation
>
>>+/* Runtime address of .got */
>>+#define _GLOBAL_OFFSET_TABLE_ \
>>+  ({ \
>>+    ElfW (Addr) * r; \
>>+    asm("la.pcrel %0, _GLOBAL_OFFSET_TABLE_" : "=r"(r)); \
>>+    r; \
>>+  })
>>+
>>+/* Return the link-time address of _DYNAMIC.  */
>>+static inline ElfW (Addr) elf_machine_dynamic (void)
>>+{
>>+  return _GLOBAL_OFFSET_TABLE_[0];
>>+}
>
>_GLOBAL_OFFSET_TABLE_[0] == link_time_dynamic is not recommended.
>See
>https://maskray.me/blog/2021-09-05-build-glibc-with-lld#global_offset_table_0
>aarch64/riscv/x86_64/etc have been updated to use the preferred
>approach.
>
>Fixed.
>
>
>diff --git a/sysdeps/loongarch/dl-machine.h b/sysdeps/loongarch/dl-machine.h
>index f902410213..361f7555f9 100644
>--- a/sysdeps/loongarch/dl-machine.h
>+++ b/sysdeps/loongarch/dl-machine.h
>@@ -70,28 +70,18 @@ elf_machine_matches_host (const ElfW (Ehdr) * ehdr)
>   return 1;
> }
>
>-/* Runtime address of .got */
>-#define _GLOBAL_OFFSET_TABLE_ \
>-  ({ \
>-    ElfW (Addr) * r; \
>-    asm("la.pcrel %0, _GLOBAL_OFFSET_TABLE_" : "=r"(r)); \
>-    r; \
>-  })
>-
>-/* Return the link-time address of _DYNAMIC.  */
>-static inline ElfW (Addr) elf_machine_dynamic (void)
>+/* Return the run-time load address of the shared object.  */
>+static inline ElfW (Addr) elf_machine_load_address (void)
> {
>-  return _GLOBAL_OFFSET_TABLE_[0];
>+  extern const ElfW(Ehdr) __ehdr_start attribute_hidden;
>+  return (ElfW(Addr)) &__ehdr_start;
> }
>
>-/* Return the run-time load address of the shared object.  */
>-static inline ElfW (Addr) elf_machine_load_address (void)
>+/* Return the link-time address of _DYNAMIC.  */
>+static inline ElfW (Addr) elf_machine_dynamic (void)
> {
>-  ElfW (Addr) got_linktime_addr;
>-  asm("la.got %0, _GLOBAL_OFFSET_TABLE_"
>-      /* Link-time address in GOT entry before runtime relocation */
>-      : "=r"(got_linktime_addr));
>-  return (ElfW (Addr)) _GLOBAL_OFFSET_TABLE_ - got_linktime_addr;
>+  extern ElfW(Dyn) _DYNAMIC[] attribute_hidden;
>+  return (ElfW(Addr)) _DYNAMIC - elf_machine_load_address ();
> }

Thank you. The updated elf_machine_load_address and elf_machine_dynamic
look good.
  

Patch

diff --git a/sysdeps/loongarch/dl-machine.h b/sysdeps/loongarch/dl-machine.h
index f902410213..361f7555f9 100644
--- a/sysdeps/loongarch/dl-machine.h
+++ b/sysdeps/loongarch/dl-machine.h
@@ -70,28 +70,18 @@  elf_machine_matches_host (const ElfW (Ehdr) * ehdr)
   return 1;
 }

-/* Runtime address of .got */
-#define _GLOBAL_OFFSET_TABLE_ \
-  ({ \
-    ElfW (Addr) * r; \
-    asm("la.pcrel %0, _GLOBAL_OFFSET_TABLE_" : "=r"(r)); \
-    r; \
-  })
-
-/* Return the link-time address of _DYNAMIC.  */
-static inline ElfW (Addr) elf_machine_dynamic (void)
+/* Return the run-time load address of the shared object.  */
+static inline ElfW (Addr) elf_machine_load_address (void)
 {
-  return _GLOBAL_OFFSET_TABLE_[0];
+  extern const ElfW(Ehdr) __ehdr_start attribute_hidden;
+  return (ElfW(Addr)) &__ehdr_start;
 }

-/* Return the run-time load address of the shared object.  */
-static inline ElfW (Addr) elf_machine_load_address (void)
+/* Return the link-time address of _DYNAMIC.  */
+static inline ElfW (Addr) elf_machine_dynamic (void)
 {
-  ElfW (Addr) got_linktime_addr;
-  asm("la.got %0, _GLOBAL_OFFSET_TABLE_"
-      /* Link-time address in GOT entry before runtime relocation */
-      : "=r"(got_linktime_addr));
-  return (ElfW (Addr)) _GLOBAL_OFFSET_TABLE_ - got_linktime_addr;
+  extern ElfW(Dyn) _DYNAMIC[] attribute_hidden;
+  return (ElfW(Addr)) _DYNAMIC - elf_machine_load_address ();
 }