This is the mail archive of the
mailing list for the glibc project.
[GLIBC][AARCH64] Rewrite elf_machine_dynamic using inline assembly
- From: Renlin Li <renlin dot li at foss dot arm dot com>
- To: libc-alpha at sourceware dot org
- Cc: Marcus Shawcroft <Marcus dot Shawcroft at arm dot com>, Szabolcs dot Nagy at arm dot com
- Date: Fri, 19 Feb 2016 11:59:26 +0000
- Subject: [GLIBC][AARCH64] Rewrite elf_machine_dynamic using inline assembly
- Authentication-results: sourceware.org; auth=none
This patch is a small improvement to use inline assembly instead of
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 ld.so, 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:
adr Rn _GLOBAL_OFFSET_TABLE_
ldr Rn [Rn]
Small memory model:
adrp Rn _GLOBAL_OFFSET_TABLE_
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?
2016-02-19 Renlin Li <email@example.com>
* 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
@@ -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))
- extern const ElfW(Addr) _GLOBAL_OFFSET_TABLE_ attribute_hidden;
- return _GLOBAL_OFFSET_TABLE_;
+ ElfW(Addr) dynamic;
+ asm ("ldr %0, _GLOBAL_OFFSET_TABLE_\n" : "=r" (dynamic));
+ return dynamic;
/* Return the run-time load address of the shared object. */