|
|
Message-ID: <20251208174940.949856-8-bill.roberts@arm.com>
Date: Mon, 8 Dec 2025 11:44:50 -0600
From: Bill Roberts <bill.roberts@....com>
To: musl@...ts.openwall.com
Cc: Bill Roberts <bill.roberts@....com>
Subject: [RFC 07/14] aarch64: rewrite tlsdesc reoutines in C using inline asm
Rewrite the AArch64 __tlsdesc_dynamic and __tlsdesc_static
routines from assembly into implementations using inline assembly.
This change eliminates the need for handwritten function prologues and
epilogues in tlsdesc.s, which simplifies maintenance and allows the compiler
to automatically insert architecture features such as BTI landing pads and
pointer authentication (PAC) sequences where applicable.
Moving to C also enables the compiler to manage register allocation,
stack usage, and ABI compliance automatically while keeping the low-level
behavior (bitmasks and register accesses) explicit and verifiable.
No functional changes intended.
Signed-off-by: Bill Roberts <bill.roberts@....com>
---
src/ldso/aarch64/tlsdesc.c | 50 ++++++++++++++++++++++++++++++++++++++
src/ldso/aarch64/tlsdesc.s | 31 -----------------------
2 files changed, 50 insertions(+), 31 deletions(-)
create mode 100644 src/ldso/aarch64/tlsdesc.c
delete mode 100644 src/ldso/aarch64/tlsdesc.s
diff --git a/src/ldso/aarch64/tlsdesc.c b/src/ldso/aarch64/tlsdesc.c
new file mode 100644
index 00000000..224a9387
--- /dev/null
+++ b/src/ldso/aarch64/tlsdesc.c
@@ -0,0 +1,50 @@
+#include <stddef.h>
+#include <stdint.h>
+
+/* size_t __tlsdesc_static(size_t *a) { return a[1]; } */
+__attribute__((visibility("hidden")))
+size_t __tlsdesc_static(size_t *a)
+{
+ size_t result;
+
+ __asm__ __volatile__(
+ "ldr %0, [%1, #8]\n\t" /* result = *(a + 8) */
+ : "=r"(result)
+ : "r"(a)
+ : "memory"
+ );
+
+ return result;
+}
+
+/*
+ * size_t __tlsdesc_dynamic(size_t *a)
+ * {
+ * struct { size_t modidx, off; } *p = (void*)a[1];
+ * size_t tp = read_tpidr_el0();
+ * size_t *dtv = *(size_t **)(tp - 8);
+ * return dtv[p->modidx] + p->off - tp;
+ * }
+ */
+__attribute__((visibility("hidden")))
+size_t __tlsdesc_dynamic(size_t *a)
+{
+ size_t result;
+
+ __asm__ __volatile__(
+ "mrs x1, tpidr_el0\n\t" /* x1 := tp */
+ "ldr x0, [x0, #8]\n\t" /* x0 := p = (void*)a[1] */
+ "ldp x0, x2, [x0]\n\t" /* x0 := p->modidx, x2 := p->off */
+ "sub x2, x2, x1\n\t" /* x2 := p->off - tp */
+ "ldr x1, [x1, #-8]\n\t" /* x1 := dtv = *(tp - 8) */
+ "ldr x1, [x1, x0, lsl #3]\n\t" /* x1 := dtv[p->modidx] */
+ "add x0, x1, x2\n\t" /* x0 := dtv[p->modidx] + p->off - tp */
+
+ "mov %0, x0\n\t"
+ : "=r"(result)
+ :
+ : "x1","x2","memory","cc"
+ );
+
+ return result;
+}
diff --git a/src/ldso/aarch64/tlsdesc.s b/src/ldso/aarch64/tlsdesc.s
deleted file mode 100644
index c6c685b3..00000000
--- a/src/ldso/aarch64/tlsdesc.s
+++ /dev/null
@@ -1,31 +0,0 @@
-// size_t __tlsdesc_static(size_t *a)
-// {
-// return a[1];
-// }
-.global __tlsdesc_static
-.hidden __tlsdesc_static
-.type __tlsdesc_static,@function
-__tlsdesc_static:
- ldr x0,[x0,#8]
- ret
-
-// size_t __tlsdesc_dynamic(size_t *a)
-// {
-// struct {size_t modidx,off;} *p = (void*)a[1];
-// size_t *dtv = *(size_t**)(tp - 8);
-// return dtv[p->modidx] + p->off - tp;
-// }
-.global __tlsdesc_dynamic
-.hidden __tlsdesc_dynamic
-.type __tlsdesc_dynamic,@function
-__tlsdesc_dynamic:
- stp x1,x2,[sp,#-16]!
- mrs x1,tpidr_el0 // tp
- ldr x0,[x0,#8] // p
- ldp x0,x2,[x0] // p->modidx, p->off
- sub x2,x2,x1 // p->off - tp
- ldr x1,[x1,#-8] // dtv
- ldr x1,[x1,x0,lsl #3] // dtv[p->modidx]
- add x0,x1,x2 // dtv[p->modidx] + p->off - tp
- ldp x1,x2,[sp],#16
- ret
--
2.51.0
Powered by blists - more mailing lists
Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.