git: 5670b8cc3672 - main - libthr: Preresolve selected EABI symbols on arm.

From: Michal Meloun <mmel_at_FreeBSD.org>
Date: Thu, 25 Jul 2024 16:25:09 UTC
The branch main has been updated by mmel:

URL: https://cgit.FreeBSD.org/src/commit/?id=5670b8cc3672d5a6bc2c41eb48d7d01343c43ad0

commit 5670b8cc3672d5a6bc2c41eb48d7d01343c43ad0
Author:     Michal Meloun <mmel@FreeBSD.org>
AuthorDate: 2024-07-24 15:11:27 +0000
Commit:     Michal Meloun <mmel@FreeBSD.org>
CommitDate: 2024-07-25 16:24:22 +0000

    libthr: Preresolve selected EABI symbols on arm.
    
    Add the ability to pre-resolve architecture-specific EABI symbols and
    use it on arm for selected EABI functions. These functions can be called
    with rtld bind lock write-locked, so they should be resolved in forward.
    
    Reported by:    Mark Millard <marklmi@yahoo.com>, John F Carr <jfc@mit.edu>
    Reviewed by:    kib, imp
    MFC after:      1 week
    Differential Revision:  https://reviews.freebsd.org/D46104
---
 lib/libthr/arch/aarch64/include/pthread_md.h |  5 +++
 lib/libthr/arch/amd64/include/pthread_md.h   |  5 +++
 lib/libthr/arch/arm/Makefile.inc             |  3 ++
 lib/libthr/arch/arm/thr_rtld_arm.c           | 67 ++++++++++++++++++++++++++++
 lib/libthr/arch/i386/include/pthread_md.h    |  5 +++
 lib/libthr/arch/powerpc/include/pthread_md.h |  5 +++
 lib/libthr/arch/riscv/include/pthread_md.h   |  5 +++
 lib/libthr/thread/thr_private.h              |  1 +
 lib/libthr/thread/thr_rtld.c                 |  3 ++
 9 files changed, 99 insertions(+)

diff --git a/lib/libthr/arch/aarch64/include/pthread_md.h b/lib/libthr/arch/aarch64/include/pthread_md.h
index 75e6abf445ba..305abed55d3c 100644
--- a/lib/libthr/arch/aarch64/include/pthread_md.h
+++ b/lib/libthr/arch/aarch64/include/pthread_md.h
@@ -49,4 +49,9 @@ _get_curthread(void)
 	return (_tcb_get()->tcb_thread);
 }
 
+static __inline void
+_thr_resolve_machdep(void)
+{
+}
+
 #endif /* _PTHREAD_MD_H_ */
diff --git a/lib/libthr/arch/amd64/include/pthread_md.h b/lib/libthr/arch/amd64/include/pthread_md.h
index 995f35cb569c..85517c1aee70 100644
--- a/lib/libthr/arch/amd64/include/pthread_md.h
+++ b/lib/libthr/arch/amd64/include/pthread_md.h
@@ -52,4 +52,9 @@ _get_curthread(void)
 	return (thr);
 }
 
+static __inline void
+_thr_resolve_machdep(void)
+{
+}
+
 #endif
diff --git a/lib/libthr/arch/arm/Makefile.inc b/lib/libthr/arch/arm/Makefile.inc
new file mode 100644
index 000000000000..4e770cf6b90f
--- /dev/null
+++ b/lib/libthr/arch/arm/Makefile.inc
@@ -0,0 +1,3 @@
+.PATH: ${.CURDIR}/arch/arm
+SRCS+= \
+ thr_rtld_arm.c
diff --git a/lib/libthr/arch/arm/thr_rtld_arm.c b/lib/libthr/arch/arm/thr_rtld_arm.c
new file mode 100644
index 000000000000..35f5fe56cfff
--- /dev/null
+++ b/lib/libthr/arch/arm/thr_rtld_arm.c
@@ -0,0 +1,67 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2024, Michal Meloun <mmel@freebsd.org>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice unmodified, this list of conditions, and the following
+ *    disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <stdlib.h>
+#include <string.h>
+
+#include "thr_private.h"
+
+int __aeabi_idiv(int , int );
+unsigned __aeabi_uidiv(unsigned, unsigned );
+
+struct {int q; int r;} __aeabi_idivmod(int, int );
+struct {unsigned q; unsigned r;} __aeabi_uidivmod(unsigned, unsigned);
+
+struct {int64_t q; int64_t r;} __aeabi_ldivmod(int64_t, int64_t);
+struct {uint64_t q; uint64_t r;} __aeabi_uldivmod(uint64_t, uint64_t);
+
+void __aeabi_memset(void *dest, size_t n, int c);
+void __aeabi_memclr(void *dest, size_t n);
+void __aeabi_memmove(void *dest, void *src, size_t n);
+void __aeabi_memcpy(void *dest, void *src, size_t n);
+void __aeabi_memcmp(void *dest, void *src, size_t n);
+
+void
+_thr_resolve_machdep(void)
+{
+	char tmp[2];
+
+	__aeabi_idiv(1, 1);
+	__aeabi_uidiv(1, 1);
+
+	__aeabi_idivmod(1, 1);
+	__aeabi_uidivmod(1, 1);
+
+	__aeabi_ldivmod(1, 1);
+	__aeabi_uldivmod(1, 1);
+
+	__aeabi_memset(tmp, 1, 0);
+	__aeabi_memclr(tmp, 1);
+	__aeabi_memmove(tmp, tmp + 1, 1);
+	__aeabi_memcpy(tmp, tmp + 1, 1);
+	__aeabi_memcmp(tmp, tmp + 1, 1);
+}
diff --git a/lib/libthr/arch/i386/include/pthread_md.h b/lib/libthr/arch/i386/include/pthread_md.h
index 9008a4f11bdb..7e2c8d7330fc 100644
--- a/lib/libthr/arch/i386/include/pthread_md.h
+++ b/lib/libthr/arch/i386/include/pthread_md.h
@@ -52,4 +52,9 @@ _get_curthread(void)
 	return (thr);
 }
 
+static __inline void
+_thr_resolve_machdep(void)
+{
+}
+
 #endif
diff --git a/lib/libthr/arch/powerpc/include/pthread_md.h b/lib/libthr/arch/powerpc/include/pthread_md.h
index 89fae48328cb..a5bc0265ed3a 100644
--- a/lib/libthr/arch/powerpc/include/pthread_md.h
+++ b/lib/libthr/arch/powerpc/include/pthread_md.h
@@ -49,4 +49,9 @@ _get_curthread(void)
 	return (NULL);
 }
 
+static __inline void
+_thr_resolve_machdep(void)
+{
+}
+
 #endif /* _PTHREAD_MD_H_ */
diff --git a/lib/libthr/arch/riscv/include/pthread_md.h b/lib/libthr/arch/riscv/include/pthread_md.h
index 343f4cae8486..baddfe3ecb22 100644
--- a/lib/libthr/arch/riscv/include/pthread_md.h
+++ b/lib/libthr/arch/riscv/include/pthread_md.h
@@ -56,4 +56,9 @@ _get_curthread(void)
 	return (NULL);
 }
 
+static __inline void
+_thr_resolve_machdep(void)
+{
+}
+
 #endif /* _PTHREAD_MD_H_ */
diff --git a/lib/libthr/thread/thr_private.h b/lib/libthr/thread/thr_private.h
index de65e8e7353d..023558100e2a 100644
--- a/lib/libthr/thread/thr_private.h
+++ b/lib/libthr/thread/thr_private.h
@@ -1103,6 +1103,7 @@ int __Tthr_mutex_trylock(pthread_mutex_t *);
 bool __thr_get_main_stack_base(char **base);
 bool __thr_get_main_stack_lim(size_t *lim);
 int _Tthr_sigqueue(pthread_t pthread, int sig, const union sigval value);
+void _thr_resolve_machdep(void);
 
 __END_DECLS
 __NULLABILITY_PRAGMA_POP
diff --git a/lib/libthr/thread/thr_rtld.c b/lib/libthr/thread/thr_rtld.c
index 25ecb83a11a3..68a02e9aca1b 100644
--- a/lib/libthr/thread/thr_rtld.c
+++ b/lib/libthr/thread/thr_rtld.c
@@ -276,6 +276,9 @@ _thr_rtld_init(void)
 	_thr_signal_block_check_fast();
 	_thr_signal_block_setup(curthread);
 
+	/* resolve machine depended functions, if any */
+	_thr_resolve_machdep();
+
 	uc_len = __getcontextx_size();
 	uc = alloca(uc_len);
 	getcontext(uc);