This is the mail archive of the mailing list for the glibc project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[GLIBC][AARCH64] Rewrite elf_machine_dynamic using inline assembly

Hi all,

This patch is a small improvement to use inline assembly instead of high-level C
code to get the address of _DYNAMIC.

Fixed PC-relative addressing inline assembly code is used here.
It provides +/-1MB PC-relative addressing capability.

I have checked that, in, the distance between .text section and .got section is within 1MB. The whole image without debug section is less than 130kiB, which is far less than the addressing capability.
So direct PC relative addressing should be adequate and safe here.

Originally, two instructions are generated from previous C code. Now only one instruction is needed.

Tiny memory model:
ldr Rn [Rn]

Small memory model:
ldr Rn [Rn, imm]

Another benefit is that, it's guaranteed that, no runtime relocation is generated for this function. For example, if glibc is compiled using -mcmodel=large, the original C code will generate the following absolute addressing code which cannot be resolved at this early stage. It will cause segment fault.

ldr x0, .L0

aarch64-none-linux-gnu is tested without any issues.
Is Okay to commit?

Renlin Li


2016-02-19  Renlin Li  <>

    * sysdeps/aarch64/dl-machine.h (elf_machine_dynamic): Use
       PC-relative instruction to get the value of _DYNAMIC.
diff --git a/sysdeps/aarch64/dl-machine.h b/sysdeps/aarch64/dl-machine.h
index 217e179..7b66457 100644
--- a/sysdeps/aarch64/dl-machine.h
+++ b/sysdeps/aarch64/dl-machine.h
@@ -33,12 +33,21 @@ elf_machine_matches_host (const ElfW(Ehdr) *ehdr)
 /* Return the link-time address of _DYNAMIC.  Conveniently, this is the
-   first element of the GOT. */
+   first element of the GOT.
+   PC-relative addressing is used here despite the memory model.
+   The offset from the address of this instruction is in the range +/- 1MB,
+   which should be enough.
+   This also ensures no run-time relocation is generated by compiler.  */
 static inline ElfW(Addr) __attribute__ ((unused))
 elf_machine_dynamic (void)
-  extern const ElfW(Addr) _GLOBAL_OFFSET_TABLE_[] attribute_hidden;
-  return _GLOBAL_OFFSET_TABLE_[0];
+  ElfW(Addr) dynamic;
+  asm ("ldr	%0, _GLOBAL_OFFSET_TABLE_\n" : "=r" (dynamic));
+  return dynamic;
 /* Return the run-time load address of the shared object.  */

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]