Hi,
This patchset further improves porting of nolibc to new architectures, it is based on our previous v5 sysret helper series [1].
It mainly shrinks the assembly _start by moving most of its operations to a C version of _start_c() function. and also, it removes the old sys_stat() support by using the sys_statx() instead and therefore, removes all of the arch specific sys_stat_struct.
Tested 'run' on all of the supported architectures:
arch/board | result ------------|------------ arm/vexpress-a9 | 141 test(s) passed, 1 skipped, 0 failed. See all results in /labs/linux-lab/logging/nolibc/arm-vexpress-a9-nolibc-test.log arm/virt | 141 test(s) passed, 1 skipped, 0 failed. See all results in /labs/linux-lab/logging/nolibc/arm-virt-nolibc-test.log aarch64/virt | 141 test(s) passed, 1 skipped, 0 failed. See all results in /labs/linux-lab/logging/nolibc/aarch64-virt-nolibc-test.log ppc/g3beige | not supported ppc/ppce500 | not supported i386/pc | 141 test(s) passed, 1 skipped, 0 failed. See all results in /labs/linux-lab/logging/nolibc/i386-pc-nolibc-test.log x86_64/pc | 141 test(s) passed, 1 skipped, 0 failed. See all results in /labs/linux-lab/logging/nolibc/x86_64-pc-nolibc-test.log mipsel/malta | 141 test(s) passed, 1 skipped, 0 failed. See all results in /labs/linux-lab/logging/nolibc/mipsel-malta-nolibc-test.log loongarch64/virt | 141 test(s) passed, 1 skipped, 0 failed. See all results in /labs/linux-lab/logging/nolibc/loongarch64-virt-nolibc-test.log riscv64/virt | 141 test(s) passed, 1 skipped, 0 failed. See all results in /labs/linux-lab/logging/nolibc/riscv64-virt-nolibc-test.log riscv32/virt | 119 test(s) passed, 1 skipped, 22 failed. See all results in /labs/linux-lab/logging/nolibc/riscv32-virt-nolibc-test.log s390x/s390-ccw-virtio | 141 test(s) passed, 1 skipped, 0 failed. See all results in /labs/linux-lab/logging/nolibc/s390x-s390-ccw-virtio-nolibc-test.log
Notes: - ppc support are ready locally, will be sent out later. - full riscv32/virt support are ready locally, will be sent out later.
Changes:
* tools/nolibc: remove old arch specific stat support
Just like the __NR_statx we used in nolibc-test.c, Let's only reserve sys_statx() and use it to implement the stat() function.
Remove the old sys_stat() and sys_stat_struct completely.
* tools/nolibc: add new crt.h with _start_c
A new C version of _start_c() is added to only require a 'sp' argument and find the others (argc, argv, envp/environ, auxv) for us in C.
* tools/nolibc: include crt.h before arch.h
Include crt.h before arch.h to let _start() be able to call the new added _start_c() in arch-<ARCH>.h.
* tools/nolibc: arm: shrink _start with _start_c tools/nolibc: aarch64: shrink _start with _start_c tools/nolibc: i386: shrink _start with _start_c tools/nolibc: x86_64: shrink _start with _start_c tools/nolibc: mips: shrink _start with _start_c tools/nolibc: loongarch: shrink _start with _start_c tools/nolibc: riscv: shrink _start with _start_c tools/nolibc: s390: shrink _start with _start_c
Move most of the operations from the assembly _start() to the C _start_c(), only require to do minimal operations in assembly _start now.
With this patchset, porting nolibc to a new architecture become easier, the powerpc porting will be added later.
Best regards, Zhangjin --- [1]: https://lore.kernel.org/lkml/cover.1687957589.git.falcon@tinylab.org/
Zhangjin Wu (11): tools/nolibc: remove old arch specific stat support tools/nolibc: add new crt.h with _start_c tools/nolibc: include crt.h before arch.h tools/nolibc: arm: shrink _start with _start_c tools/nolibc: aarch64: shrink _start with _start_c tools/nolibc: i386: shrink _start with _start_c tools/nolibc: x86_64: shrink _start with _start_c tools/nolibc: mips: shrink _start with _start_c tools/nolibc: loongarch: shrink _start with _start_c tools/nolibc: riscv: shrink _start with _start_c tools/nolibc: s390: shrink _start with _start_c
tools/include/nolibc/Makefile | 1 + tools/include/nolibc/arch-aarch64.h | 53 ++---------------- tools/include/nolibc/arch-arm.h | 79 ++------------------------- tools/include/nolibc/arch-i386.h | 58 +++----------------- tools/include/nolibc/arch-loongarch.h | 42 ++------------ tools/include/nolibc/arch-mips.h | 73 +++---------------------- tools/include/nolibc/arch-riscv.h | 65 ++-------------------- tools/include/nolibc/arch-s390.h | 60 ++------------------ tools/include/nolibc/arch-x86_64.h | 54 ++---------------- tools/include/nolibc/crt.h | 57 +++++++++++++++++++ tools/include/nolibc/nolibc.h | 1 + tools/include/nolibc/signal.h | 1 + tools/include/nolibc/stdio.h | 1 + tools/include/nolibc/stdlib.h | 1 + tools/include/nolibc/sys.h | 64 ++++------------------ tools/include/nolibc/time.h | 1 + tools/include/nolibc/types.h | 4 +- tools/include/nolibc/unistd.h | 1 + 18 files changed, 122 insertions(+), 494 deletions(-) create mode 100644 tools/include/nolibc/crt.h
__NR_statx has been added from v4.10:
commit a528d35e8bfc ("statx: Add a system call to make enhanced file info available")
It has been supported by all of the platforms since at least from v4.20 and glibc 2.28.
Let's remove the old arch specific stat support completely.
This is friendly to the future new architecture porting.
Signed-off-by: Zhangjin Wu falcon@tinylab.org --- tools/include/nolibc/arch-aarch64.h | 28 ------------- tools/include/nolibc/arch-arm.h | 37 ----------------- tools/include/nolibc/arch-i386.h | 26 ------------ tools/include/nolibc/arch-mips.h | 28 ------------- tools/include/nolibc/arch-riscv.h | 23 ----------- tools/include/nolibc/arch-s390.h | 25 ------------ tools/include/nolibc/arch-x86_64.h | 27 ------------- tools/include/nolibc/sys.h | 63 +++++------------------------ tools/include/nolibc/types.h | 4 +- 9 files changed, 13 insertions(+), 248 deletions(-)
diff --git a/tools/include/nolibc/arch-aarch64.h b/tools/include/nolibc/arch-aarch64.h index e30056f996db..4a8a6f386865 100644 --- a/tools/include/nolibc/arch-aarch64.h +++ b/tools/include/nolibc/arch-aarch64.h @@ -9,34 +9,6 @@
#include "compiler.h"
-/* The struct returned by the newfstatat() syscall. Differs slightly from the - * x86_64's stat one by field ordering, so be careful. - */ -struct sys_stat_struct { - unsigned long st_dev; - unsigned long st_ino; - unsigned int st_mode; - unsigned int st_nlink; - unsigned int st_uid; - unsigned int st_gid; - - unsigned long st_rdev; - unsigned long __pad1; - long st_size; - int st_blksize; - int __pad2; - - long st_blocks; - long st_atime; - unsigned long st_atime_nsec; - long st_mtime; - - unsigned long st_mtime_nsec; - long st_ctime; - unsigned long st_ctime_nsec; - unsigned int __unused[2]; -}; - /* Syscalls for AARCH64 : * - registers are 64-bit * - stack is 16-byte aligned diff --git a/tools/include/nolibc/arch-arm.h b/tools/include/nolibc/arch-arm.h index 7a64290fc518..a2ea5756cef2 100644 --- a/tools/include/nolibc/arch-arm.h +++ b/tools/include/nolibc/arch-arm.h @@ -9,43 +9,6 @@
#include "compiler.h"
-/* The struct returned by the stat() syscall, 32-bit only, the syscall returns - * exactly 56 bytes (stops before the unused array). In big endian, the format - * differs as devices are returned as short only. - */ -struct sys_stat_struct { -#if defined(__ARMEB__) - unsigned short st_dev; - unsigned short __pad1; -#else - unsigned long st_dev; -#endif - unsigned long st_ino; - unsigned short st_mode; - unsigned short st_nlink; - unsigned short st_uid; - unsigned short st_gid; - -#if defined(__ARMEB__) - unsigned short st_rdev; - unsigned short __pad2; -#else - unsigned long st_rdev; -#endif - unsigned long st_size; - unsigned long st_blksize; - unsigned long st_blocks; - - unsigned long st_atime; - unsigned long st_atime_nsec; - unsigned long st_mtime; - unsigned long st_mtime_nsec; - - unsigned long st_ctime; - unsigned long st_ctime_nsec; - unsigned long __unused[2]; -}; - /* Syscalls for ARM in ARM or Thumb modes : * - registers are 32-bit * - stack is 8-byte aligned diff --git a/tools/include/nolibc/arch-i386.h b/tools/include/nolibc/arch-i386.h index c2e75ba91b6b..418554435545 100644 --- a/tools/include/nolibc/arch-i386.h +++ b/tools/include/nolibc/arch-i386.h @@ -9,32 +9,6 @@
#include "compiler.h"
-/* The struct returned by the stat() syscall, 32-bit only, the syscall returns - * exactly 56 bytes (stops before the unused array). - */ -struct sys_stat_struct { - unsigned long st_dev; - unsigned long st_ino; - unsigned short st_mode; - unsigned short st_nlink; - unsigned short st_uid; - unsigned short st_gid; - - unsigned long st_rdev; - unsigned long st_size; - unsigned long st_blksize; - unsigned long st_blocks; - - unsigned long st_atime; - unsigned long st_atime_nsec; - unsigned long st_mtime; - unsigned long st_mtime_nsec; - - unsigned long st_ctime; - unsigned long st_ctime_nsec; - unsigned long __unused[2]; -}; - /* Syscalls for i386 : * - mostly similar to x86_64 * - registers are 32-bit diff --git a/tools/include/nolibc/arch-mips.h b/tools/include/nolibc/arch-mips.h index a8b33d6914a4..ecf912a1938f 100644 --- a/tools/include/nolibc/arch-mips.h +++ b/tools/include/nolibc/arch-mips.h @@ -9,34 +9,6 @@
#include "compiler.h"
-/* The struct returned by the stat() syscall. 88 bytes are returned by the - * syscall. - */ -struct sys_stat_struct { - unsigned int st_dev; - long st_pad1[3]; - unsigned long st_ino; - unsigned int st_mode; - unsigned int st_nlink; - unsigned int st_uid; - unsigned int st_gid; - unsigned int st_rdev; - long st_pad2[2]; - long st_size; - long st_pad3; - - long st_atime; - long st_atime_nsec; - long st_mtime; - long st_mtime_nsec; - - long st_ctime; - long st_ctime_nsec; - long st_blksize; - long st_blocks; - long st_pad4[14]; -}; - /* Syscalls for MIPS ABI O32 : * - WARNING! there's always a delayed slot! * - WARNING again, the syntax is different, registers take a '$' and numbers diff --git a/tools/include/nolibc/arch-riscv.h b/tools/include/nolibc/arch-riscv.h index 4f21e862b412..7f4abcc15206 100644 --- a/tools/include/nolibc/arch-riscv.h +++ b/tools/include/nolibc/arch-riscv.h @@ -9,29 +9,6 @@
#include "compiler.h"
-struct sys_stat_struct { - unsigned long st_dev; /* Device. */ - unsigned long st_ino; /* File serial number. */ - unsigned int st_mode; /* File mode. */ - unsigned int st_nlink; /* Link count. */ - unsigned int st_uid; /* User ID of the file's owner. */ - unsigned int st_gid; /* Group ID of the file's group. */ - unsigned long st_rdev; /* Device number, if device. */ - unsigned long __pad1; - long st_size; /* Size of file, in bytes. */ - int st_blksize; /* Optimal block size for I/O. */ - int __pad2; - long st_blocks; /* Number 512-byte blocks allocated. */ - long st_atime; /* Time of last access. */ - unsigned long st_atime_nsec; - long st_mtime; /* Time of last modification. */ - unsigned long st_mtime_nsec; - long st_ctime; /* Time of last status change. */ - unsigned long st_ctime_nsec; - unsigned int __unused4; - unsigned int __unused5; -}; - #if __riscv_xlen == 64 #define PTRLOG "3" #define SZREG "8" diff --git a/tools/include/nolibc/arch-s390.h b/tools/include/nolibc/arch-s390.h index 8cff5b05e841..5139e0e37e56 100644 --- a/tools/include/nolibc/arch-s390.h +++ b/tools/include/nolibc/arch-s390.h @@ -10,31 +10,6 @@
#include "compiler.h"
-/* The struct returned by the stat() syscall, equivalent to stat64(). The - * syscall returns 116 bytes and stops in the middle of __unused. - */ - -struct sys_stat_struct { - unsigned long st_dev; - unsigned long st_ino; - unsigned long st_nlink; - unsigned int st_mode; - unsigned int st_uid; - unsigned int st_gid; - unsigned int __pad1; - unsigned long st_rdev; - unsigned long st_size; - unsigned long st_atime; - unsigned long st_atime_nsec; - unsigned long st_mtime; - unsigned long st_mtime_nsec; - unsigned long st_ctime; - unsigned long st_ctime_nsec; - unsigned long st_blksize; - long st_blocks; - unsigned long __unused[3]; -}; - /* Syscalls for s390: * - registers are 64-bit * - syscall number is passed in r1 diff --git a/tools/include/nolibc/arch-x86_64.h b/tools/include/nolibc/arch-x86_64.h index 1dc8b60f5153..4c0192815a59 100644 --- a/tools/include/nolibc/arch-x86_64.h +++ b/tools/include/nolibc/arch-x86_64.h @@ -9,33 +9,6 @@
#include "compiler.h"
-/* The struct returned by the stat() syscall, equivalent to stat64(). The - * syscall returns 116 bytes and stops in the middle of __unused. - */ -struct sys_stat_struct { - unsigned long st_dev; - unsigned long st_ino; - unsigned long st_nlink; - unsigned int st_mode; - unsigned int st_uid; - - unsigned int st_gid; - unsigned int __pad0; - unsigned long st_rdev; - long st_size; - long st_blksize; - - long st_blocks; - unsigned long st_atime; - unsigned long st_atime_nsec; - unsigned long st_mtime; - - unsigned long st_mtime_nsec; - unsigned long st_ctime; - unsigned long st_ctime_nsec; - long __unused[3]; -}; - /* Syscalls for x86_64 : * - registers are 64-bit * - syscall number is passed in rax diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h index e0ac95a4bfa1..2c302f3feb71 100644 --- a/tools/include/nolibc/sys.h +++ b/tools/include/nolibc/sys.h @@ -914,15 +914,19 @@ pid_t setsid(void) return __sysret(sys_setsid()); }
-#if defined(__NR_statx) /* * int statx(int fd, const char *path, int flags, unsigned int mask, struct statx *buf); + * int stat(const char *path, struct stat *buf); */
static __attribute__((unused)) int sys_statx(int fd, const char *path, int flags, unsigned int mask, struct statx *buf) { +#ifdef __NR_statx return my_syscall5(__NR_statx, fd, path, flags, mask, buf); +#else + return -ENOSYS; +#endif }
static __attribute__((unused)) @@ -930,24 +934,18 @@ int statx(int fd, const char *path, int flags, unsigned int mask, struct statx * { return __sysret(sys_statx(fd, path, flags, mask, buf)); } -#endif
-/* - * int stat(const char *path, struct stat *buf); - * Warning: the struct stat's layout is arch-dependent. - */
-#if defined(__NR_statx) && !defined(__NR_newfstatat) && !defined(__NR_stat) -/* - * Maybe we can just use statx() when available for all architectures? - */ static __attribute__((unused)) -int sys_stat(const char *path, struct stat *buf) +int stat(const char *path, struct stat *buf) { struct statx statx; long ret;
- ret = sys_statx(AT_FDCWD, path, AT_NO_AUTOMOUNT, STATX_BASIC_STATS, &statx); + ret = __sysret(sys_statx(AT_FDCWD, path, AT_NO_AUTOMOUNT, STATX_BASIC_STATS, &statx)); + if (ret == -1) + return ret; + buf->st_dev = ((statx.stx_dev_minor & 0xff) | (statx.stx_dev_major << 8) | ((statx.stx_dev_minor & ~0xff) << 12)); @@ -968,47 +966,8 @@ int sys_stat(const char *path, struct stat *buf) buf->st_mtim.tv_nsec = statx.stx_mtime.tv_nsec; buf->st_ctim.tv_sec = statx.stx_ctime.tv_sec; buf->st_ctim.tv_nsec = statx.stx_ctime.tv_nsec; - return ret; -} -#else -static __attribute__((unused)) -int sys_stat(const char *path, struct stat *buf) -{ - struct sys_stat_struct stat; - long ret; - -#ifdef __NR_newfstatat - /* only solution for arm64 */ - ret = my_syscall4(__NR_newfstatat, AT_FDCWD, path, &stat, 0); -#elif defined(__NR_stat) - ret = my_syscall2(__NR_stat, path, &stat); -#else - return -ENOSYS; -#endif - buf->st_dev = stat.st_dev; - buf->st_ino = stat.st_ino; - buf->st_mode = stat.st_mode; - buf->st_nlink = stat.st_nlink; - buf->st_uid = stat.st_uid; - buf->st_gid = stat.st_gid; - buf->st_rdev = stat.st_rdev; - buf->st_size = stat.st_size; - buf->st_blksize = stat.st_blksize; - buf->st_blocks = stat.st_blocks; - buf->st_atim.tv_sec = stat.st_atime; - buf->st_atim.tv_nsec = stat.st_atime_nsec; - buf->st_mtim.tv_sec = stat.st_mtime; - buf->st_mtim.tv_nsec = stat.st_mtime_nsec; - buf->st_ctim.tv_sec = stat.st_ctime; - buf->st_ctim.tv_nsec = stat.st_ctime_nsec; - return ret; -} -#endif
-static __attribute__((unused)) -int stat(const char *path, struct stat *buf) -{ - return __sysret(sys_stat(path, buf)); + return 0; }
diff --git a/tools/include/nolibc/types.h b/tools/include/nolibc/types.h index bed62da7877c..7c794ec21b46 100644 --- a/tools/include/nolibc/types.h +++ b/tools/include/nolibc/types.h @@ -14,8 +14,8 @@
/* Only the generic macros and types may be defined here. The arch-specific - * ones such as the O_RDONLY and related macros used by fcntl() and open(), or - * the layout of sys_stat_struct must not be defined here. + * ones such as the O_RDONLY and related macros used by fcntl() and open() + * must not be defined here. */
/* stat flags (WARNING, octal here). We need to check for an existing
As the environ and _auxv support added for nolibc, the assembly _start function becomes more and more complex and therefore makes the porting of nolibc to new architectures harder and harder.
To simplify portability, this crt.h is added to do most of the assembly start operations in C function: _start_c(), which reduces the complexity a lot and will eventually simplify the porting of nolibc to the new architectures.
The new _start_c() only requires a stack pointer argument, it will find argv, envp and _auxv for us, and then call main(), finally, it exit() with main's return status. With this new _start_c(), the future new architectures only require to add very few assembly instructions.
It may also easier the future init/fini support.
Signed-off-by: Zhangjin Wu falcon@tinylab.org --- tools/include/nolibc/crt.h | 57 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 tools/include/nolibc/crt.h
diff --git a/tools/include/nolibc/crt.h b/tools/include/nolibc/crt.h new file mode 100644 index 000000000000..698fe1084d26 --- /dev/null +++ b/tools/include/nolibc/crt.h @@ -0,0 +1,57 @@ +/* SPDX-License-Identifier: LGPL-2.1 OR MIT */ +/* + * C Run Time support for NOLIBC + * Copyright (C) 2023 Zhangjin Wu falcon@tinylab.org + */ + +#ifndef _NOLIBC_CRT_H +#define _NOLIBC_CRT_H + +char **environ __attribute__((weak)); +const unsigned long *_auxv __attribute__((weak)); + +int main(int argc, char *argv[], char **envp); +static void exit(int); + +void _start_c(long *sp) +{ + int argc, i; + char **argv; + char **envp; + + /* + * sp : argc <-- argument count, required by main() + * argv: argv[0] <-- argument vector, required by main() + * argv[1] + * ... + * argv[argc-1] + * null + * envp: envp[0] <-- environment variables, required by main() and getenv() + * envp[1] + * ... + * null + * _auxv: auxv[0] <-- auxiliary vector, required by getauxval() + * auxv[1] + * ... + * null + */ + + /* assign argc and argv */ + argc = sp[0]; + argv = (void *)(sp + 1); + + /* find envp */ + envp = argv + argc + 1; + environ = envp; + + /* find auxv */ + i = 0; + while (envp[i]) + i++; + _auxv = (void *)(envp + i + 1); + + /* go to application */ + exit(main(argc, argv, envp)); +} + +#endif /* _NOLIBC_CRT_H */
The crt.h provides a new _start_c() function, which is required by the new assembly _start entry of arch-<ARCH>.h (included by arch.h), let's include crt.h before arch.h.
This '#include "crt.h"' doesn't let the new _start_c() work immediately, but it is a base of the coming patches to move most of the assembly _start operations to the _start_c() function for every supported architecture.
Signed-off-by: Zhangjin Wu falcon@tinylab.org --- tools/include/nolibc/Makefile | 1 + tools/include/nolibc/nolibc.h | 1 + tools/include/nolibc/signal.h | 1 + tools/include/nolibc/stdio.h | 1 + tools/include/nolibc/stdlib.h | 1 + tools/include/nolibc/sys.h | 1 + tools/include/nolibc/time.h | 1 + tools/include/nolibc/unistd.h | 1 + 8 files changed, 8 insertions(+)
diff --git a/tools/include/nolibc/Makefile b/tools/include/nolibc/Makefile index 875e13e3c851..00471e59b11e 100644 --- a/tools/include/nolibc/Makefile +++ b/tools/include/nolibc/Makefile @@ -37,6 +37,7 @@ NARCH = $(or $(NARCH_$(ARCH)),$(ARCH)) arch_file := arch-$(NARCH).h all_files := \ compiler.h \ + crt.h \ ctype.h \ errno.h \ nolibc.h \ diff --git a/tools/include/nolibc/nolibc.h b/tools/include/nolibc/nolibc.h index 1f8d821000ac..2cc9ccd90d56 100644 --- a/tools/include/nolibc/nolibc.h +++ b/tools/include/nolibc/nolibc.h @@ -93,6 +93,7 @@ #define _NOLIBC_H
#include "std.h" +#include "crt.h" #include "arch.h" #include "types.h" #include "sys.h" diff --git a/tools/include/nolibc/signal.h b/tools/include/nolibc/signal.h index 137552216e46..f0a1418c1cb2 100644 --- a/tools/include/nolibc/signal.h +++ b/tools/include/nolibc/signal.h @@ -8,6 +8,7 @@ #define _NOLIBC_SIGNAL_H
#include "std.h" +#include "crt.h" #include "arch.h" #include "types.h" #include "sys.h" diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h index 0eef91daf289..89d3749b3620 100644 --- a/tools/include/nolibc/stdio.h +++ b/tools/include/nolibc/stdio.h @@ -10,6 +10,7 @@ #include <stdarg.h>
#include "std.h" +#include "crt.h" #include "arch.h" #include "errno.h" #include "types.h" diff --git a/tools/include/nolibc/stdlib.h b/tools/include/nolibc/stdlib.h index 902162f80337..0ff7fac40bd4 100644 --- a/tools/include/nolibc/stdlib.h +++ b/tools/include/nolibc/stdlib.h @@ -8,6 +8,7 @@ #define _NOLIBC_STDLIB_H
#include "std.h" +#include "crt.h" #include "arch.h" #include "types.h" #include "sys.h" diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h index 2c302f3feb71..b6c33c40c037 100644 --- a/tools/include/nolibc/sys.h +++ b/tools/include/nolibc/sys.h @@ -24,6 +24,7 @@ #include <linux/reboot.h> /* for LINUX_REBOOT_* */ #include <linux/prctl.h>
+#include "crt.h" #include "arch.h" #include "errno.h" #include "types.h" diff --git a/tools/include/nolibc/time.h b/tools/include/nolibc/time.h index 84655361b9ad..bbe8f9aa3e9b 100644 --- a/tools/include/nolibc/time.h +++ b/tools/include/nolibc/time.h @@ -8,6 +8,7 @@ #define _NOLIBC_TIME_H
#include "std.h" +#include "crt.h" #include "arch.h" #include "types.h" #include "sys.h" diff --git a/tools/include/nolibc/unistd.h b/tools/include/nolibc/unistd.h index e38f3660c051..f1677224bb5a 100644 --- a/tools/include/nolibc/unistd.h +++ b/tools/include/nolibc/unistd.h @@ -8,6 +8,7 @@ #define _NOLIBC_UNISTD_H
#include "std.h" +#include "crt.h" #include "arch.h" #include "types.h" #include "sys.h"
On 2023-06-29 02:54:35+0800, Zhangjin Wu wrote:
The crt.h provides a new _start_c() function, which is required by the new assembly _start entry of arch-<ARCH>.h (included by arch.h), let's include crt.h before arch.h.
This '#include "crt.h"' doesn't let the new _start_c() work immediately, but it is a base of the coming patches to move most of the assembly _start operations to the _start_c() function for every supported architecture.
Why don't the arch-*.h files include this new header? They are the users of the new symbol.
Signed-off-by: Zhangjin Wu falcon@tinylab.org
tools/include/nolibc/Makefile | 1 + tools/include/nolibc/nolibc.h | 1 + tools/include/nolibc/signal.h | 1 + tools/include/nolibc/stdio.h | 1 + tools/include/nolibc/stdlib.h | 1 + tools/include/nolibc/sys.h | 1 + tools/include/nolibc/time.h | 1 + tools/include/nolibc/unistd.h | 1 + 8 files changed, 8 insertions(+)
diff --git a/tools/include/nolibc/Makefile b/tools/include/nolibc/Makefile index 875e13e3c851..00471e59b11e 100644 --- a/tools/include/nolibc/Makefile +++ b/tools/include/nolibc/Makefile @@ -37,6 +37,7 @@ NARCH = $(or $(NARCH_$(ARCH)),$(ARCH)) arch_file := arch-$(NARCH).h all_files := \ compiler.h \
crt.h \
This should be part of the patch adding the file.
ctype.h \ errno.h \ nolibc.h \
diff --git a/tools/include/nolibc/nolibc.h b/tools/include/nolibc/nolibc.h index 1f8d821000ac..2cc9ccd90d56 100644 --- a/tools/include/nolibc/nolibc.h +++ b/tools/include/nolibc/nolibc.h @@ -93,6 +93,7 @@ #define _NOLIBC_H #include "std.h" +#include "crt.h" #include "arch.h" #include "types.h" #include "sys.h" diff --git a/tools/include/nolibc/signal.h b/tools/include/nolibc/signal.h index 137552216e46..f0a1418c1cb2 100644 --- a/tools/include/nolibc/signal.h +++ b/tools/include/nolibc/signal.h @@ -8,6 +8,7 @@ #define _NOLIBC_SIGNAL_H #include "std.h" +#include "crt.h" #include "arch.h" #include "types.h" #include "sys.h" diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h index 0eef91daf289..89d3749b3620 100644 --- a/tools/include/nolibc/stdio.h +++ b/tools/include/nolibc/stdio.h @@ -10,6 +10,7 @@ #include <stdarg.h> #include "std.h" +#include "crt.h" #include "arch.h" #include "errno.h" #include "types.h" diff --git a/tools/include/nolibc/stdlib.h b/tools/include/nolibc/stdlib.h index 902162f80337..0ff7fac40bd4 100644 --- a/tools/include/nolibc/stdlib.h +++ b/tools/include/nolibc/stdlib.h @@ -8,6 +8,7 @@ #define _NOLIBC_STDLIB_H #include "std.h" +#include "crt.h" #include "arch.h" #include "types.h" #include "sys.h" diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h index 2c302f3feb71..b6c33c40c037 100644 --- a/tools/include/nolibc/sys.h +++ b/tools/include/nolibc/sys.h @@ -24,6 +24,7 @@ #include <linux/reboot.h> /* for LINUX_REBOOT_* */ #include <linux/prctl.h> +#include "crt.h" #include "arch.h" #include "errno.h" #include "types.h" diff --git a/tools/include/nolibc/time.h b/tools/include/nolibc/time.h index 84655361b9ad..bbe8f9aa3e9b 100644 --- a/tools/include/nolibc/time.h +++ b/tools/include/nolibc/time.h @@ -8,6 +8,7 @@ #define _NOLIBC_TIME_H #include "std.h" +#include "crt.h" #include "arch.h" #include "types.h" #include "sys.h" diff --git a/tools/include/nolibc/unistd.h b/tools/include/nolibc/unistd.h index e38f3660c051..f1677224bb5a 100644 --- a/tools/include/nolibc/unistd.h +++ b/tools/include/nolibc/unistd.h @@ -8,6 +8,7 @@ #define _NOLIBC_UNISTD_H #include "std.h" +#include "crt.h" #include "arch.h" #include "types.h"
#include "sys.h"
2.25.1
Hi, Thomas
On 2023-06-29 02:54:35+0800, Zhangjin Wu wrote:
The crt.h provides a new _start_c() function, which is required by the new assembly _start entry of arch-<ARCH>.h (included by arch.h), let's include crt.h before arch.h.
This '#include "crt.h"' doesn't let the new _start_c() work immediately, but it is a base of the coming patches to move most of the assembly _start operations to the _start_c() function for every supported architecture.
Why don't the arch-*.h files include this new header? They are the users of the new symbol.
I have tried so, but since crt.h itself is not architecture specific, add it before arch.h will avoid every new arch porting add the same thing again and again, currently, we only need to add once. I have even planned to move compiler.h out of arch-*.h, but not yet ;-)
And also, crt.h may require more features in the future, like init/fini support, it may be not only used by arch-*.h files.
Signed-off-by: Zhangjin Wu falcon@tinylab.org
tools/include/nolibc/Makefile | 1 + tools/include/nolibc/nolibc.h | 1 + tools/include/nolibc/signal.h | 1 + tools/include/nolibc/stdio.h | 1 + tools/include/nolibc/stdlib.h | 1 + tools/include/nolibc/sys.h | 1 + tools/include/nolibc/time.h | 1 + tools/include/nolibc/unistd.h | 1 + 8 files changed, 8 insertions(+)
diff --git a/tools/include/nolibc/Makefile b/tools/include/nolibc/Makefile index 875e13e3c851..00471e59b11e 100644 --- a/tools/include/nolibc/Makefile +++ b/tools/include/nolibc/Makefile @@ -37,6 +37,7 @@ NARCH = $(or $(NARCH_$(ARCH)),$(ARCH)) arch_file := arch-$(NARCH).h all_files := \ compiler.h \
crt.h \
This should be part of the patch adding the file.
Ok, thanks very much.
Best regards, Zhangjin
ctype.h \ errno.h \ nolibc.h \
On 2023-07-03 17:58:32+0800, Zhangjin Wu wrote:
Hi, Thomas
On 2023-06-29 02:54:35+0800, Zhangjin Wu wrote:
The crt.h provides a new _start_c() function, which is required by the new assembly _start entry of arch-<ARCH>.h (included by arch.h), let's include crt.h before arch.h.
This '#include "crt.h"' doesn't let the new _start_c() work immediately, but it is a base of the coming patches to move most of the assembly _start operations to the _start_c() function for every supported architecture.
Why don't the arch-*.h files include this new header? They are the users of the new symbol.
I have tried so, but since crt.h itself is not architecture specific, add it before arch.h will avoid every new arch porting add the same thing again and again, currently, we only need to add once. I have even planned to move compiler.h out of arch-*.h, but not yet ;-)
While this saves a few lines of code in my opinion it hurts clarity to rely on implicitly pre-included things.
every new arch porting
That doesn't seem like a very frequent occurrence :-)
And also, crt.h may require more features in the future, like init/fini support, it may be not only used by arch-*.h files.
Do you have a mechanism in mind that supports init/fini without needing an ELF parser at runtime? I guess an ELF parser would make it a complete no-go.
Also the value added by init/fini seems fairly limited for a statically linked (tiny) application.
[..]
On 2023-07-03 17:58:32+0800, Zhangjin Wu wrote:
Hi, Thomas
On 2023-06-29 02:54:35+0800, Zhangjin Wu wrote:
The crt.h provides a new _start_c() function, which is required by the new assembly _start entry of arch-<ARCH>.h (included by arch.h), let's include crt.h before arch.h.
This '#include "crt.h"' doesn't let the new _start_c() work immediately, but it is a base of the coming patches to move most of the assembly _start operations to the _start_c() function for every supported architecture.
Why don't the arch-*.h files include this new header? They are the users of the new symbol.
I have tried so, but since crt.h itself is not architecture specific, add it before arch.h will avoid every new arch porting add the same thing again and again, currently, we only need to add once. I have even planned to move compiler.h out of arch-*.h, but not yet ;-)
While this saves a few lines of code in my opinion it hurts clarity to rely on implicitly pre-included things.
To be clearer, what about split the arch.h to sys_arch.h (my_syscall*) and crt_arch.h? (_start part) and then, we can include crt_arch.h in crt.h and at the same time, include sys_arch.h in sys.h, and at last we need to create a <ARCH> directory for the them.
crt.h: #include "crt_arch.h"
_start_c ()
sys.h: #include "sys_arch.h"
sys_xxx() { my_syscall<N>(...) }
crt_arch.h:
#ifdef ARCH #include "<ARCH>/crt_arch.h" #endif
sys_arch.h:
#ifdef ARCH #inculde "<ARCH>/sys_arch.h" #endif
I just found musl uses such structure ;-)
every new arch porting
That doesn't seem like a very frequent occurrence :-)
Yes, it is not often.
And also, crt.h may require more features in the future, like init/fini support, it may be not only used by arch-*.h files.
Do you have a mechanism in mind that supports init/fini without needing an ELF parser at runtime? I guess an ELF parser would make it a complete no-go.
I didn't really think about this yet ;-)
Also the value added by init/fini seems fairly limited for a statically linked (tiny) application.
Yeah.
Thanks, Zhangjin
[..]
Let's move most of the _start operations to _start_c().
Signed-off-by: Zhangjin Wu falcon@tinylab.org --- tools/include/nolibc/arch-arm.h | 42 ++++----------------------------- 1 file changed, 5 insertions(+), 37 deletions(-)
diff --git a/tools/include/nolibc/arch-arm.h b/tools/include/nolibc/arch-arm.h index a2ea5756cef2..573f8fe31cee 100644 --- a/tools/include/nolibc/arch-arm.h +++ b/tools/include/nolibc/arch-arm.h @@ -183,49 +183,17 @@ _arg1; \ })
- -char **environ __attribute__((weak)); -const unsigned long *_auxv __attribute__((weak)); - /* startup code */ void __attribute__((weak,noreturn,optimize("omit-frame-pointer"))) __no_stack_protector _start(void) { __asm__ volatile ( #ifdef _NOLIBC_STACKPROTECTOR - "bl __stack_chk_init\n" /* initialize stack protector */ + "bl __stack_chk_init\n" /* initialize stack protector */ #endif - "pop {%r0}\n" /* argc was in the stack */ - "mov %r1, %sp\n" /* argv = sp */ - - "add %r2, %r0, $1\n" /* envp = (argc + 1) ... */ - "lsl %r2, %r2, $2\n" /* * 4 ... */ - "add %r2, %r2, %r1\n" /* + argv */ - "ldr %r3, 1f\n" /* r3 = &environ (see below) */ - "str %r2, [r3]\n" /* store envp into environ */ - - "mov r4, r2\n" /* search for auxv (follows NULL after last env) */ - "0:\n" - "mov r5, r4\n" /* r5 = r4 */ - "add r4, r4, #4\n" /* r4 += 4 */ - "ldr r5,[r5]\n" /* r5 = *r5 = *(r4-4) */ - "cmp r5, #0\n" /* and stop at NULL after last env */ - "bne 0b\n" - "ldr %r3, 2f\n" /* r3 = &_auxv (low bits) */ - "str r4, [r3]\n" /* store r4 into _auxv */ - - "mov %r3, $8\n" /* AAPCS : sp must be 8-byte aligned in the */ - "neg %r3, %r3\n" /* callee, and bl doesn't push (lr=pc) */ - "and %r3, %r3, %r1\n" /* so we do sp = r1(=sp) & r3(=-8); */ - "mov %sp, %r3\n" - - "bl main\n" /* main() returns the status code, we'll exit with it. */ - "movs r7, $1\n" /* NR_exit == 1 */ - "svc $0x00\n" - ".align 2\n" /* below are the pointers to a few variables */ - "1:\n" - ".word environ\n" - "2:\n" - ".word _auxv\n" + "mov %r0, sp\n" /* save stack pointer to r0, as arg1 of _start_c*/ + "and ip, %r0, #-8\n" /* sp must be 8-byte aligned in the callee */ + "mov sp, ip\n" + "bl _start_c\n" /* transfer to c runtime */ ); __builtin_unreachable(); }
Let's move most of the _start operations to _start_c().
Signed-off-by: Zhangjin Wu falcon@tinylab.org --- tools/include/nolibc/arch-aarch64.h | 25 ++++--------------------- 1 file changed, 4 insertions(+), 21 deletions(-)
diff --git a/tools/include/nolibc/arch-aarch64.h b/tools/include/nolibc/arch-aarch64.h index 4a8a6f386865..ef98a751783f 100644 --- a/tools/include/nolibc/arch-aarch64.h +++ b/tools/include/nolibc/arch-aarch64.h @@ -143,33 +143,16 @@ _arg1; \ })
-char **environ __attribute__((weak)); -const unsigned long *_auxv __attribute__((weak)); - /* startup code */ void __attribute__((weak,noreturn,optimize("omit-frame-pointer"))) __no_stack_protector _start(void) { __asm__ volatile ( #ifdef _NOLIBC_STACKPROTECTOR - "bl __stack_chk_init\n" /* initialize stack protector */ + "bl __stack_chk_init\n" /* initialize stack protector */ #endif - "ldr x0, [sp]\n" /* argc (x0) was in the stack */ - "add x1, sp, 8\n" /* argv (x1) = sp */ - "lsl x2, x0, 3\n" /* envp (x2) = 8*argc ... */ - "add x2, x2, 8\n" /* + 8 (skip null) */ - "add x2, x2, x1\n" /* + argv */ - "adrp x3, environ\n" /* x3 = &environ (high bits) */ - "str x2, [x3, #:lo12:environ]\n" /* store envp into environ */ - "mov x4, x2\n" /* search for auxv (follows NULL after last env) */ - "0:\n" - "ldr x5, [x4], 8\n" /* x5 = *x4; x4 += 8 */ - "cbnz x5, 0b\n" /* and stop at NULL after last env */ - "adrp x3, _auxv\n" /* x3 = &_auxv (high bits) */ - "str x4, [x3, #:lo12:_auxv]\n" /* store x4 into _auxv */ - "and sp, x1, -16\n" /* sp must be 16-byte aligned in the callee */ - "bl main\n" /* main() returns the status code, we'll exit with it. */ - "mov x8, 93\n" /* NR_exit == 93 */ - "svc #0\n" + "mov x0, sp\n" /* save stack pointer to x0, as arg1 of _start_c*/ + "and sp, x0, -16\n" /* sp must be 16-byte aligned in the callee */ + "bl _start_c\n" /* transfer to c runtime */ ); __builtin_unreachable(); }
Let's move most of the _start operations to _start_c().
Signed-off-by: Zhangjin Wu falcon@tinylab.org --- tools/include/nolibc/arch-i386.h | 32 +++++++------------------------- 1 file changed, 7 insertions(+), 25 deletions(-)
diff --git a/tools/include/nolibc/arch-i386.h b/tools/include/nolibc/arch-i386.h index 418554435545..b56046f1d0c0 100644 --- a/tools/include/nolibc/arch-i386.h +++ b/tools/include/nolibc/arch-i386.h @@ -154,9 +154,6 @@ _eax; \ })
-char **environ __attribute__((weak)); -const unsigned long *_auxv __attribute__((weak)); - /* startup code */ /* * i386 System V ABI mandates: @@ -168,29 +165,14 @@ void __attribute__((weak,noreturn,optimize("omit-frame-pointer"))) __no_stack_pr { __asm__ volatile ( #ifdef _NOLIBC_STACKPROTECTOR - "call __stack_chk_init\n" /* initialize stack protector */ + "call __stack_chk_init\n" /* initialize stack protector */ #endif - "pop %eax\n" /* argc (first arg, %eax) */ - "mov %esp, %ebx\n" /* argv[] (second arg, %ebx) */ - "lea 4(%ebx,%eax,4),%ecx\n" /* then a NULL then envp (third arg, %ecx) */ - "mov %ecx, environ\n" /* save environ */ - "xor %ebp, %ebp\n" /* zero the stack frame */ - "mov %ecx, %edx\n" /* search for auxv (follows NULL after last env) */ - "0:\n" - "add $4, %edx\n" /* search for auxv using edx, it follows the */ - "cmp -4(%edx), %ebp\n" /* ... NULL after last env (ebp is zero here) */ - "jnz 0b\n" - "mov %edx, _auxv\n" /* save it into _auxv */ - "and $-16, %esp\n" /* x86 ABI : esp must be 16-byte aligned before */ - "sub $4, %esp\n" /* the call instruction (args are aligned) */ - "push %ecx\n" /* push all registers on the stack so that we */ - "push %ebx\n" /* support both regparm and plain stack modes */ - "push %eax\n" - "call main\n" /* main() returns the status code in %eax */ - "mov %eax, %ebx\n" /* retrieve exit code (32-bit int) */ - "movl $1, %eax\n" /* NR_exit == 1 */ - "int $0x80\n" /* exit now */ - "hlt\n" /* ensure it does not */ + "xor %ebp, %ebp\n" /* zero the stack frame */ + "mov %esp, %eax\n" /* save stack pointer to eax, as arg1 of _start_c */ + "and $-16, %esp\n" /* last pushed argument must be 16-byte aligned */ + "push %eax\n" /* push arg1 on stack to support plain stack modes too */ + "call _start_c\n" /* transfer to c runtime */ + "hlt\n" /* ensure it does not return */ ); __builtin_unreachable(); }
Let's move most of the _start operations to _start_c().
Signed-off-by: Zhangjin Wu falcon@tinylab.org --- tools/include/nolibc/arch-x86_64.h | 27 ++++++--------------------- 1 file changed, 6 insertions(+), 21 deletions(-)
diff --git a/tools/include/nolibc/arch-x86_64.h b/tools/include/nolibc/arch-x86_64.h index 4c0192815a59..116dcd8e1413 100644 --- a/tools/include/nolibc/arch-x86_64.h +++ b/tools/include/nolibc/arch-x86_64.h @@ -153,9 +153,6 @@ _ret; \ })
-char **environ __attribute__((weak)); -const unsigned long *_auxv __attribute__((weak)); - /* startup code */ /* * x86-64 System V ABI mandates: @@ -167,25 +164,13 @@ void __attribute__((weak,noreturn,optimize("omit-frame-pointer"))) __no_stack_pr { __asm__ volatile ( #ifdef _NOLIBC_STACKPROTECTOR - "call __stack_chk_init\n" /* initialize stack protector */ + "call __stack_chk_init\n" /* initialize stack protector */ #endif - "pop %rdi\n" /* argc (first arg, %rdi) */ - "mov %rsp, %rsi\n" /* argv[] (second arg, %rsi) */ - "lea 8(%rsi,%rdi,8),%rdx\n" /* then a NULL then envp (third arg, %rdx) */ - "mov %rdx, environ\n" /* save environ */ - "xor %ebp, %ebp\n" /* zero the stack frame */ - "mov %rdx, %rax\n" /* search for auxv (follows NULL after last env) */ - "0:\n" - "add $8, %rax\n" /* search for auxv using rax, it follows the */ - "cmp -8(%rax), %rbp\n" /* ... NULL after last env (rbp is zero here) */ - "jnz 0b\n" - "mov %rax, _auxv\n" /* save it into _auxv */ - "and $-16, %rsp\n" /* x86 ABI : esp must be 16-byte aligned before call */ - "call main\n" /* main() returns the status code, we'll exit with it. */ - "mov %eax, %edi\n" /* retrieve exit code (32 bit) */ - "mov $60, %eax\n" /* NR_exit == 60 */ - "syscall\n" /* really exit */ - "hlt\n" /* ensure it does not return */ + "xor %ebp, %ebp\n" /* zero the stack frame */ + "mov %rsp, %rdi\n" /* save stack pointer to rdi, as arg1 of _start_c */ + "and $-16, %rsp\n" /* rsp must be 16-byte aligned before call */ + "call _start_c\n" /* transfer to c runtime */ + "hlt\n" /* ensure it does not return */ ); __builtin_unreachable(); }
Let's move most of the _start operations to _start_c().
Also clean up the instructions in delay slots.
Signed-off-by: Zhangjin Wu falcon@tinylab.org --- tools/include/nolibc/arch-mips.h | 45 +++++++------------------------- 1 file changed, 9 insertions(+), 36 deletions(-)
diff --git a/tools/include/nolibc/arch-mips.h b/tools/include/nolibc/arch-mips.h index ecf912a1938f..d0f73f03bfa1 100644 --- a/tools/include/nolibc/arch-mips.h +++ b/tools/include/nolibc/arch-mips.h @@ -173,50 +173,23 @@ _arg4 ? -_num : _num; \ })
-char **environ __attribute__((weak)); -const unsigned long *_auxv __attribute__((weak)); - /* startup code, note that it's called __start on MIPS */ void __attribute__((weak,noreturn,optimize("omit-frame-pointer"))) __no_stack_protector __start(void) { __asm__ volatile ( - /*".set nomips16\n"*/ ".set push\n" - ".set noreorder\n" + ".set noreorder\n" ".option pic0\n" #ifdef _NOLIBC_STACKPROTECTOR - "jal __stack_chk_init\n" /* initialize stack protector */ - "nop\n" /* delayed slot */ + "jal __stack_chk_init\n" /* initialize stack protector */ + " nop\n" /* delayed slot */ #endif - /*".ent __start\n"*/ - /*"__start:\n"*/ - "lw $a0,($sp)\n" /* argc was in the stack */ - "addiu $a1, $sp, 4\n" /* argv = sp + 4 */ - "sll $a2, $a0, 2\n" /* a2 = argc * 4 */ - "add $a2, $a2, $a1\n" /* envp = argv + 4*argc ... */ - "addiu $a2, $a2, 4\n" /* ... + 4 */ - "lui $a3, %hi(environ)\n" /* load environ into a3 (hi) */ - "addiu $a3, %lo(environ)\n" /* load environ into a3 (lo) */ - "sw $a2,($a3)\n" /* store envp(a2) into environ */ - - "move $t0, $a2\n" /* iterate t0 over envp, look for NULL */ - "0:" /* do { */ - "lw $a3, ($t0)\n" /* a3=*(t0); */ - "bne $a3, $0, 0b\n" /* } while (a3); */ - "addiu $t0, $t0, 4\n" /* delayed slot: t0+=4; */ - "lui $a3, %hi(_auxv)\n" /* load _auxv into a3 (hi) */ - "addiu $a3, %lo(_auxv)\n" /* load _auxv into a3 (lo) */ - "sw $t0, ($a3)\n" /* store t0 into _auxv */ - - "li $t0, -8\n" - "and $sp, $sp, $t0\n" /* sp must be 8-byte aligned */ - "addiu $sp,$sp,-16\n" /* the callee expects to save a0..a3 there! */ - "jal main\n" /* main() returns the status code, we'll exit with it. */ - "nop\n" /* delayed slot */ - "move $a0, $v0\n" /* retrieve 32-bit exit code from v0 */ - "li $v0, 4001\n" /* NR_exit == 4001 */ - "syscall\n" - /*".end __start\n"*/ + "move $a0, $sp\n" /* save stack pointer to a0, as arg1 of _start_c*/ + "li $t0, -8\n" + "and $sp, $sp, $t0\n" /* sp must be 8-byte aligned */ + "addiu $sp, $sp, -16\n" /* the callee expects to save a0..a3 there */ + "jal _start_c\n" /* transfer to c runtime */ + " nop\n" /* delayed slot */ ".set pop\n" ); __builtin_unreachable();
Let's move most of the _start operations to _start_c().
Signed-off-by: Zhangjin Wu falcon@tinylab.org --- tools/include/nolibc/arch-loongarch.h | 42 +++------------------------ 1 file changed, 4 insertions(+), 38 deletions(-)
diff --git a/tools/include/nolibc/arch-loongarch.h b/tools/include/nolibc/arch-loongarch.h index fbb4844f7993..f5cac2e81a13 100644 --- a/tools/include/nolibc/arch-loongarch.h +++ b/tools/include/nolibc/arch-loongarch.h @@ -145,26 +145,9 @@ _arg1; \ })
-char **environ __attribute__((weak)); -const unsigned long *_auxv __attribute__((weak)); - #if __loongarch_grlen == 32 -#define LONGLOG "2" -#define SZREG "4" -#define REG_L "ld.w" -#define LONG_S "st.w" -#define LONG_ADD "add.w" -#define LONG_ADDI "addi.w" -#define LONG_SLL "slli.w" #define LONG_BSTRINS "bstrins.w" #else /* __loongarch_grlen == 64 */ -#define LONGLOG "3" -#define SZREG "8" -#define REG_L "ld.d" -#define LONG_S "st.d" -#define LONG_ADD "add.d" -#define LONG_ADDI "addi.d" -#define LONG_SLL "slli.d" #define LONG_BSTRINS "bstrins.d" #endif
@@ -173,28 +156,11 @@ void __attribute__((weak,noreturn,optimize("omit-frame-pointer"))) __no_stack_pr { __asm__ volatile ( #ifdef _NOLIBC_STACKPROTECTOR - "bl __stack_chk_init\n" /* initialize stack protector */ + "bl __stack_chk_init\n" /* initialize stack protector */ #endif - REG_L " $a0, $sp, 0\n" /* argc (a0) was in the stack */ - LONG_ADDI " $a1, $sp, "SZREG"\n" /* argv (a1) = sp + SZREG */ - LONG_SLL " $a2, $a0, "LONGLOG"\n" /* envp (a2) = SZREG*argc ... */ - LONG_ADDI " $a2, $a2, "SZREG"\n" /* + SZREG (skip null) */ - LONG_ADD " $a2, $a2, $a1\n" /* + argv */ - - "move $a3, $a2\n" /* iterate a3 over envp to find auxv (after NULL) */ - "0:\n" /* do { */ - REG_L " $a4, $a3, 0\n" /* a4 = *a3; */ - LONG_ADDI " $a3, $a3, "SZREG"\n" /* a3 += sizeof(void*); */ - "bne $a4, $zero, 0b\n" /* } while (a4); */ - "la.pcrel $a4, _auxv\n" /* a4 = &_auxv */ - LONG_S " $a3, $a4, 0\n" /* store a3 into _auxv */ - - "la.pcrel $a3, environ\n" /* a3 = &environ */ - LONG_S " $a2, $a3, 0\n" /* store envp(a2) into environ */ - LONG_BSTRINS " $sp, $zero, 3, 0\n" /* sp must be 16-byte aligned */ - "bl main\n" /* main() returns the status code, we'll exit with it. */ - "li.w $a7, 93\n" /* NR_exit == 93 */ - "syscall 0\n" + "move $a0, $sp\n" /* save stack pointer to a0, as arg1 of _start_c*/ + LONG_BSTRINS " $sp, $zero, 3, 0\n" /* sp must be 16-byte aligned */ + "bl _start_c\n" /* transfer to c runtime */ ); __builtin_unreachable(); }
Let's move most of the _start operations to _start_c().
Signed-off-by: Zhangjin Wu falcon@tinylab.org --- tools/include/nolibc/arch-riscv.h | 42 ++++--------------------------- 1 file changed, 5 insertions(+), 37 deletions(-)
diff --git a/tools/include/nolibc/arch-riscv.h b/tools/include/nolibc/arch-riscv.h index 7f4abcc15206..ec0f9c597d0b 100644 --- a/tools/include/nolibc/arch-riscv.h +++ b/tools/include/nolibc/arch-riscv.h @@ -9,18 +9,6 @@
#include "compiler.h"
-#if __riscv_xlen == 64 -#define PTRLOG "3" -#define SZREG "8" -#define REG_L "ld" -#define REG_S "sd" -#elif __riscv_xlen == 32 -#define PTRLOG "2" -#define SZREG "4" -#define REG_L "lw" -#define REG_S "sw" -#endif - /* Syscalls for RISCV : * - stack is 16-byte aligned * - syscall number is passed in a7 @@ -153,40 +141,20 @@ _arg1; \ })
-char **environ __attribute__((weak)); -const unsigned long *_auxv __attribute__((weak)); - /* startup code */ void __attribute__((weak,noreturn,optimize("omit-frame-pointer"))) __no_stack_protector _start(void) { __asm__ volatile ( ".option push\n" ".option norelax\n" - "lla gp, __global_pointer$\n" + "lla gp, __global_pointer$\n" ".option pop\n" #ifdef _NOLIBC_STACKPROTECTOR - "call __stack_chk_init\n" /* initialize stack protector */ + "call __stack_chk_init\n" /* initialize stack protector */ #endif - REG_L" a0, 0(sp)\n" /* argc (a0) was in the stack */ - "add a1, sp, "SZREG"\n" /* argv (a1) = sp */ - "slli a2, a0, "PTRLOG"\n" /* envp (a2) = SZREG*argc ... */ - "add a2, a2, "SZREG"\n" /* + SZREG (skip null) */ - "add a2,a2,a1\n" /* + argv */ - - "add a3, a2, zero\n" /* iterate a3 over envp to find auxv (after NULL) */ - "0:\n" /* do { */ - REG_L" a4, 0(a3)\n" /* a4 = *a3; */ - "add a3, a3, "SZREG"\n" /* a3 += sizeof(void*); */ - "bne a4, zero, 0b\n" /* } while (a4); */ - "lui a4, %hi(_auxv)\n" /* a4 = &_auxv (high bits) */ - REG_S" a3, %lo(_auxv)(a4)\n" /* store a3 into _auxv */ - - "lui a3, %hi(environ)\n" /* a3 = &environ (high bits) */ - REG_S" a2,%lo(environ)(a3)\n"/* store envp(a2) into environ */ - "andi sp,a1,-16\n" /* sp must be 16-byte aligned */ - "call main\n" /* main() returns the status code, we'll exit with it. */ - "li a7, 93\n" /* NR_exit == 93 */ - "ecall\n" + "mv a0, sp\n" /* save stack pointer to a0, as arg1 of _start_c*/ + "andi sp, a0, -16\n" /* sp must be 16-byte aligned */ + "call _start_c\n" /* transfer to c runtime */ ); __builtin_unreachable(); }
Let's move most of the _start operations to _start_c().
Signed-off-by: Zhangjin Wu falcon@tinylab.org --- tools/include/nolibc/arch-s390.h | 35 ++++---------------------------- 1 file changed, 4 insertions(+), 31 deletions(-)
diff --git a/tools/include/nolibc/arch-s390.h b/tools/include/nolibc/arch-s390.h index 5139e0e37e56..a28f94aa28ca 100644 --- a/tools/include/nolibc/arch-s390.h +++ b/tools/include/nolibc/arch-s390.h @@ -137,41 +137,14 @@ _arg1; \ })
-char **environ __attribute__((weak)); -const unsigned long *_auxv __attribute__((weak)); - /* startup code */ void __attribute__((weak,noreturn,optimize("omit-frame-pointer"))) __no_stack_protector _start(void) { __asm__ volatile ( - "lg %r2,0(%r15)\n" /* argument count */ - "la %r3,8(%r15)\n" /* argument pointers */ - - "xgr %r0,%r0\n" /* r0 will be our NULL value */ - /* search for envp */ - "lgr %r4,%r3\n" /* start at argv */ - "0:\n" - "clg %r0,0(%r4)\n" /* entry zero? */ - "la %r4,8(%r4)\n" /* advance pointer */ - "jnz 0b\n" /* no -> test next pointer */ - /* yes -> r4 now contains start of envp */ - "larl %r1,environ\n" - "stg %r4,0(%r1)\n" - - /* search for auxv */ - "lgr %r5,%r4\n" /* start at envp */ - "1:\n" - "clg %r0,0(%r5)\n" /* entry zero? */ - "la %r5,8(%r5)\n" /* advance pointer */ - "jnz 1b\n" /* no -> test next pointer */ - "larl %r1,_auxv\n" /* yes -> store value in _auxv */ - "stg %r5,0(%r1)\n" - - "aghi %r15,-160\n" /* allocate new stackframe */ - "xc 0(8,%r15),0(%r15)\n" /* clear backchain */ - "brasl %r14,main\n" /* ret value of main is arg to exit */ - "lghi %r1,1\n" /* __NR_exit */ - "svc 0\n" + "lgr %r2, %r15\n" /* initialize stack protector */ + "aghi %r15, -160\n" /* allocate new stackframe */ + "xc 0(8,%r15), 0(%r15)\n" /* clear backchain */ + "brasl %r14, _start_c\n" /* transfer to c runtime */ ); __builtin_unreachable(); }
linux-kselftest-mirror@lists.linaro.org