From 7a6c259ea07131770a3685b5e9db57b4f23b3940 Mon Sep 17 00:00:00 2001 From: Xiaotian Wu Date: Thu, 5 Sep 2024 11:23:46 +0800 Subject: [PATCH 1/4] LoongArch: update musl tlsdesc Signed-off-by: Xiaotian Wu --- arch/loongarch64/reloc.h | 1 + include/elf.h | 2 ++ src/ldso/loongarch64/tlsdesc.s | 51 +++++++++++++++++----------------- 3 files changed, 28 insertions(+), 26 deletions(-) diff --git a/arch/loongarch64/reloc.h b/arch/loongarch64/reloc.h index 61eaca9e3..a4db6a9c5 100644 --- a/arch/loongarch64/reloc.h +++ b/arch/loongarch64/reloc.h @@ -17,6 +17,7 @@ #define REL_TPOFF R_LARCH_TLS_TPREL64 #define REL_RELATIVE R_LARCH_RELATIVE #define REL_SYMBOLIC R_LARCH_64 +#define REL_TLSDESC R_LARCH_TLS_DESC64 #define CRTJMP(pc,sp) __asm__ __volatile__( \ "move $sp, %1 ; jr %0" : : "r"(pc), "r"(sp) : "memory" ) diff --git a/include/elf.h b/include/elf.h index 0e535db61..a852c6643 100644 --- a/include/elf.h +++ b/include/elf.h @@ -3347,6 +3347,8 @@ enum #define R_LARCH_TLS_TPREL32 10 #define R_LARCH_TLS_TPREL64 11 #define R_LARCH_IRELATIVE 12 +#define R_LARCH_TLS_DESC32 13 +#define R_LARCH_TLS_DESC64 14 #define R_LARCH_MARK_LA 20 #define R_LARCH_MARK_PCREL 21 #define R_LARCH_SOP_PUSH_PCREL 22 diff --git a/src/ldso/loongarch64/tlsdesc.s b/src/ldso/loongarch64/tlsdesc.s index 5bf18e46e..4b6ea0e51 100644 --- a/src/ldso/loongarch64/tlsdesc.s +++ b/src/ldso/loongarch64/tlsdesc.s @@ -1,38 +1,37 @@ -# size_t __tlsdesc_static(size_t *a) -# { -# return a[1]; -# } .text .global __tlsdesc_static .hidden __tlsdesc_static .type __tlsdesc_static,%function __tlsdesc_static: - ld.d $a0, $a0, 8 - jr $ra - + ld.d $a0, $a0, 8 + jr $ra # 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; +# 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: - addi.d $sp, $sp, -16 - st.d $t1, $sp, 0 - st.d $t2, $sp, 8 - ld.d $t2, $tp, -8 # t2=dtv - ld.d $a0, $a0, 8 # a0=&{modidx,off} - ld.d $t1, $a0, 8 # t1=off - ld.d $a0, $a0, 0 # a0=modidx - slli.d $a0, $a0, 3 # a0=8*modidx - add.d $a0, $a0, $t2 # a0=dtv+8*modidx - ld.d $a0, $a0, 0 # a0=dtv[modidx] - add.d $a0, $a0, $t1 # a0=dtv[modidx]+off - sub.d $a0, $a1, $tp - ld.d $t1, $sp, 0 - ld.d $t2, $sp, 8 - addi.d $sp, $sp, 16 - jr $ra + addi.d $sp, $sp, -16 + st.d $t1, $sp, 0 + st.d $t2, $sp, 8 + + ld.d $t2, $tp, -8 # t2=dtv + + ld.d $a0, $a0, 8 # a0=&{modidx,off} + ld.d $t1, $a0, 8 # t1=off + ld.d $a0, $a0, 0 # a0=modidx + slli.d $a0, $a0, 3 # a0=8*modidx + + add.d $a0, $a0, $t2 # a0=dtv+8*modidx + ld.d $a0, $a0, 0 # a0=dtv[modidx] + add.d $a0, $a0, $t1 # a0=dtv[modidx]+off + sub.d $a0, $a0, $tp # a0=dtv[modidx]+off-tp + + ld.d $t1, $sp, 0 + ld.d $t2, $sp, 8 + addi.d $sp, $sp, 16 + jr $ra -- Gitee From f6bf998a51e618c2f07d861726ebcf8c2bdd84f6 Mon Sep 17 00:00:00 2001 From: sunyuanbo Date: Tue, 14 Jan 2025 19:35:15 +0800 Subject: [PATCH 2/4] LoongArch: fix clone syscall This commit fixes the clone syscall implementation for LoongArch. It introduces the GET_SP_REG macro specific to LoongArch, ensuring that the stack pointer is correctly retrieved during the clone process. Signed-off-by: Xiaotian Wu --- porting/linux/user/src/linux/clone.c | 2 ++ src/linux/linux/clone.c | 2 ++ 2 files changed, 4 insertions(+) diff --git a/porting/linux/user/src/linux/clone.c b/porting/linux/user/src/linux/clone.c index c20833e18..5c0c9783e 100644 --- a/porting/linux/user/src/linux/clone.c +++ b/porting/linux/user/src/linux/clone.c @@ -20,6 +20,8 @@ struct __clone_args { __asm__ __volatile__ ("mov %%rsp, %0;" : "=r"(stack) :); \ stack = (void *)((uintptr_t)stack - 16); \ } while (0) +#elif defined(__loongarch_lp64) +#define GET_SP_REG(stack) __asm__ __volatile__ ("addi.d %0, $sp, 0" : "=r"(stack)) #else #define GET_SP_REG(stack) #endif diff --git a/src/linux/linux/clone.c b/src/linux/linux/clone.c index d91aefca6..814aa9d0a 100644 --- a/src/linux/linux/clone.c +++ b/src/linux/linux/clone.c @@ -35,6 +35,8 @@ struct __clone_args { __asm__ __volatile__ ("mov %%rsp, %0;" : "=r"(stack) :); \ stack = (void *)((uintptr_t)stack - 16); \ } while (0) +#elif defined(__loongarch_lp64) +#define GET_SP_REG(stack) __asm__ __volatile__ ("addi.d %0, $sp, 0" : "=r"(stack)) #else #define GET_SP_REG(stack) #endif -- Gitee From 3c9679caa984a812a7f5729b67eb773ae11dcb8e Mon Sep 17 00:00:00 2001 From: Xiaotian Wu Date: Fri, 28 Feb 2025 14:49:58 +0800 Subject: [PATCH 3/4] fix build error on loongarch64 ``` obj/third_party/musl/usr/include/loongarch64-linux-ohos/bits/stat.h:17:19: error: expected member name or ';' after declaration specifiers unsigned __unused[2]; ~~~~~~~~ ^ 1 error generated. ``` Signed-off-by: Xiaotian Wu --- arch/loongarch64/bits/stat.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/loongarch64/bits/stat.h b/arch/loongarch64/bits/stat.h index b7f4221be..2b9cd6df9 100644 --- a/arch/loongarch64/bits/stat.h +++ b/arch/loongarch64/bits/stat.h @@ -14,5 +14,5 @@ struct stat { struct timespec st_atim; struct timespec st_mtim; struct timespec st_ctim; - unsigned __unused[2]; + unsigned __unused1[2]; }; -- Gitee From f45e3fe653bc0a0966cb956b851508f3551ce3eb Mon Sep 17 00:00:00 2001 From: Xiaotian Wu Date: Tue, 1 Jul 2025 20:30:14 +0800 Subject: [PATCH 4/4] add dlvsym.s for loongarch64 Signed-off-by: Xiaotian Wu --- libc-test/test_template.gni | 2 ++ musl_src.gni | 7 ++++++- porting/linux/user/src/ldso/loongarch64/dlvsym.s | 6 ++++++ scripts/build_lite/Makefile | 1 + scripts/porting.sh | 1 + src/ldso/loongarch64/linux/dlvsym.s | 6 ++++++ 6 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 porting/linux/user/src/ldso/loongarch64/dlvsym.s create mode 100644 src/ldso/loongarch64/linux/dlvsym.s diff --git a/libc-test/test_template.gni b/libc-test/test_template.gni index 60eea22cc..68f009044 100644 --- a/libc-test/test_template.gni +++ b/libc-test/test_template.gni @@ -9,6 +9,8 @@ if (current_cpu == "arm") { musl_arch = "arm" } else if (current_cpu == "arm64") { musl_arch = "aarch64" +} else if (current_cpu == "loongarch64") { + musl_arch = "loongarch64" } else if (current_cpu == "x86_64") { musl_arch = "x86_64" } diff --git a/musl_src.gni b/musl_src.gni index 1abfabd56..031fedb93 100644 --- a/musl_src.gni +++ b/musl_src.gni @@ -2366,6 +2366,7 @@ musl_src_files_ext = [ "include/linux/syscall_hooks.h", "src/ldso/arm/linux/dlvsym.s", "src/ldso/aarch64/linux/dlvsym.s", + "src/ldso/loongarch64/linux/dlvsym.s", "src/ldso/x86_64/linux/dlvsym.s", "src/misc/aarch64/linux/fstat.s", "src/misc/aarch64/linux/syscall.s", @@ -2481,6 +2482,7 @@ musl_src_linux_files = [ "include/syscall_hooks.h", "src/ldso/arm/dlvsym.s", "src/ldso/aarch64/dlvsym.s", + "src/ldso/loongarch64/dlvsym.s", "src/ldso/x86_64/dlvsym.s", "src/misc/aarch64/fstat.s", "src/misc/aarch64/syscall.s", @@ -2502,7 +2504,10 @@ if (musl_arch == "arm") { "src/ldso/aarch64/dlvsym.s", ] } else if (musl_arch == "loongarch64") { - musl_src_file += [ "arch/loongarch64/syscall_arch.h" ] + musl_src_file += [ + "arch/loongarch64/syscall_arch.h", + "src/ldso/loongarch64/dlvsym.s", + ] } else if (musl_arch == "x86_64") { musl_src_file += [ "src/process/x86_64/__vfork.s", diff --git a/porting/linux/user/src/ldso/loongarch64/dlvsym.s b/porting/linux/user/src/ldso/loongarch64/dlvsym.s new file mode 100644 index 000000000..be26177fa --- /dev/null +++ b/porting/linux/user/src/ldso/loongarch64/dlvsym.s @@ -0,0 +1,6 @@ +.global dlvsym +.hidden __dlvsym +dlvsym: + move $a3, $ra + la.global $t0, __dlvsym + jr $t0 diff --git a/scripts/build_lite/Makefile b/scripts/build_lite/Makefile index 15f54cec4..1be462242 100644 --- a/scripts/build_lite/Makefile +++ b/scripts/build_lite/Makefile @@ -208,6 +208,7 @@ musl_patch_for_linux_user: musl_copy_for_linux_user mv -f src/internal/linux/* src/internal/ && \ mv -f src/ldso/aarch64/linux/* src/ldso/aarch64/ && \ mv -f src/ldso/arm/linux/* src/ldso/arm/ && \ + mv -f src/ldso/loongarch64/linux/* src/ldso/loongarch64/ && \ mv -f src/ldso/riscv64/linux/* src/ldso/riscv64/ && \ mv -f src/ldso/x86_64/linux/* src/ldso/x86_64/ && \ mv -f src/linux/aarch64/linux/* src/linux/aarch64/ && \ diff --git a/scripts/porting.sh b/scripts/porting.sh index 45a07039f..630671b5d 100755 --- a/scripts/porting.sh +++ b/scripts/porting.sh @@ -54,6 +54,7 @@ cp -rfp ${SRC_DIR}/include/fortify/linux/* ${DST_DIR}/include/fortify cp -rfp ${SRC_DIR}/include/linux/* ${DST_DIR}/include cp -rfp ${SRC_DIR}/src/ldso/arm/linux/* ${DST_DIR}/src/ldso/arm cp -rfp ${SRC_DIR}/src/ldso/aarch64/linux/* ${DST_DIR}/src/ldso/aarch64 +cp -rfp ${SRC_DIR}/src/ldso/loongarch64/linux/* ${DST_DIR}/src/ldso/loongarch64 cp -rfp ${SRC_DIR}/src/ldso/x86_64/linux/* ${DST_DIR}/src/ldso/x86_64 cp -rfp ${SRC_DIR}/src/misc/aarch64/linux/* ${DST_DIR}/src/misc/aarch64 cp -rfp ${SRC_DIR}/src/malloc/linux/* ${DST_DIR}/src/malloc diff --git a/src/ldso/loongarch64/linux/dlvsym.s b/src/ldso/loongarch64/linux/dlvsym.s new file mode 100644 index 000000000..be26177fa --- /dev/null +++ b/src/ldso/loongarch64/linux/dlvsym.s @@ -0,0 +1,6 @@ +.global dlvsym +.hidden __dlvsym +dlvsym: + move $a3, $ra + la.global $t0, __dlvsym + jr $t0 -- Gitee