From 51f62ea918248817ad50fe4c8123aa694b5fbc85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Sun, 7 Dec 2025 09:17:55 +0100 Subject: [PATCH] libc: update startup code from freebsd 15 --- lib/libc/freebsd/COPYRIGHT | 3 +- lib/libc/freebsd/lib/csu/amd64/crt1_s.S | 15 +-- lib/libc/freebsd/lib/csu/arm/crt1_c.c | 1 - lib/libc/freebsd/lib/csu/common/crtbegin.c | 45 +------- lib/libc/freebsd/lib/csu/common/crtbrand.S | 2 +- lib/libc/freebsd/lib/csu/common/crtend.c | 65 ----------- .../freebsd/lib/csu/common/feature_note.S | 2 +- .../freebsd/lib/csu/common/ignore_init_note.S | 2 +- lib/libc/freebsd/lib/csu/powerpc/crt.h | 3 +- lib/libc/freebsd/lib/csu/powerpc64/crt.h | 3 +- .../freebsd/lib/libc/include/libc_private.h | 108 ++++-------------- src/libs/freebsd.zig | 4 - 12 files changed, 36 insertions(+), 217 deletions(-) delete mode 100644 lib/libc/freebsd/lib/csu/common/crtend.c diff --git a/lib/libc/freebsd/COPYRIGHT b/lib/libc/freebsd/COPYRIGHT index 9ed4f98d95..9f8b3d4c01 100644 --- a/lib/libc/freebsd/COPYRIGHT +++ b/lib/libc/freebsd/COPYRIGHT @@ -1,9 +1,8 @@ -# @(#)COPYRIGHT 8.2 (Berkeley) 3/21/94 The compilation of software known as FreeBSD is distributed under the following terms: -Copyright (c) 1992-2023 The FreeBSD Project. +Copyright (c) 1992-2025 The FreeBSD Project. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions diff --git a/lib/libc/freebsd/lib/csu/amd64/crt1_s.S b/lib/libc/freebsd/lib/csu/amd64/crt1_s.S index f7ea076d94..3ceea92893 100644 --- a/lib/libc/freebsd/lib/csu/amd64/crt1_s.S +++ b/lib/libc/freebsd/lib/csu/amd64/crt1_s.S @@ -49,15 +49,12 @@ _start: #ifdef GCRT subq $16, %rsp #endif - movq %rsi, %rcx - movq %rdi, %rsi /* argv = ap */ - addq $8, %rsi /* argv += 1 */ - movq %rdi, %rdx /* env = ap */ - addq $16, %rdx /* env += 2 */ - movslq (%rdi), %rax - movl %eax, %edi /* argc = *(long *)(void *)ap */ - shlq $3, %rax - addq %rax, %rdx /* env += argc */ + movq %rsi, %rcx /* cleanup */ + movslq (%rdi), %rax /* long *ap; tmpargc = *ap */ + leaq 0x8(%rdi), %rsi /* argv = ap + 1 */ + leaq 0x10(%rdi, %rax, 8), %rdx /* env = ap + 2 + tmpargc */ + movl %eax, %edi /* argc = tmpargc */ + #ifdef PIC /* * XXX. %rip relative addressing is not intended for use in the diff --git a/lib/libc/freebsd/lib/csu/arm/crt1_c.c b/lib/libc/freebsd/lib/csu/arm/crt1_c.c index 7de2d333a5..bb40f262f5 100644 --- a/lib/libc/freebsd/lib/csu/arm/crt1_c.c +++ b/lib/libc/freebsd/lib/csu/arm/crt1_c.c @@ -41,7 +41,6 @@ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include #include #include diff --git a/lib/libc/freebsd/lib/csu/common/crtbegin.c b/lib/libc/freebsd/lib/csu/common/crtbegin.c index 20e6ea085d..c6443d580c 100644 --- a/lib/libc/freebsd/lib/csu/common/crtbegin.c +++ b/lib/libc/freebsd/lib/csu/common/crtbegin.c @@ -21,7 +21,6 @@ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include #include #include "crt.h" @@ -41,9 +40,8 @@ void __cxa_finalize(void *) __weak_symbol; * When we have ctors/dtors call from the dtor handler before calling * any dtors, otherwise use a destructor. */ -#ifndef HAVE_CTORS +/* zig patch: no HAVE_CTORS */ __attribute__((destructor)) -#endif static void run_cxa_finalize(void) { @@ -53,43 +51,4 @@ run_cxa_finalize(void) } #endif -/* - * On some architectures and toolchains we may need to call the .dtors. - * These are called in the order they are in the ELF file. - */ -#ifdef HAVE_CTORS -static void __do_global_dtors_aux(void) __used; - -static crt_func __CTOR_LIST__[] __section(".ctors") __used = { - (crt_func)-1 -}; -static crt_func __DTOR_LIST__[] __section(".dtors") __used = { - (crt_func)-1 -}; - -static void -__do_global_dtors_aux(void) -{ - crt_func fn; - int n; - -#ifdef SHARED - run_cxa_finalize(); -#endif - - for (n = 1;; n++) { - fn = __DTOR_LIST__[n]; - if (fn == (crt_func)0 || fn == (crt_func)-1) - break; - fn(); - } -} - -asm ( - ".pushsection .fini \n" - "\t" INIT_CALL_SEQ(__do_global_dtors_aux) "\n" - ".popsection \n" -); -#endif - -/* zig patch: remove gcj nonsense */ +/* zig patch: no HAVE_CTORS */ diff --git a/lib/libc/freebsd/lib/csu/common/crtbrand.S b/lib/libc/freebsd/lib/csu/common/crtbrand.S index bff07ac18d..a53e5b70ba 100644 --- a/lib/libc/freebsd/lib/csu/common/crtbrand.S +++ b/lib/libc/freebsd/lib/csu/common/crtbrand.S @@ -36,7 +36,7 @@ * for more information. */ - .section .note.tag,"aG",%note,.freebsd.noteG,comdat + .section .note.tag,"aGR",%note,.freebsd.noteG,comdat .p2align 2 .4byte 2f-1f .4byte 4f-3f diff --git a/lib/libc/freebsd/lib/csu/common/crtend.c b/lib/libc/freebsd/lib/csu/common/crtend.c deleted file mode 100644 index 68a935b516..0000000000 --- a/lib/libc/freebsd/lib/csu/common/crtend.c +++ /dev/null @@ -1,65 +0,0 @@ -/*- - * SPDX-License-Identifier: BSD-1-Clause - * - * Copyright 2018 Andrew Turner - * - * 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, this list of conditions and the following disclaimer. - * - * 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 -#include "crt.h" - -typedef void (*crt_func)(void); - -/* zig patch: remove gcj nonsense */ - -#ifdef HAVE_CTORS - -/* - * On some architectures and toolchains we may need to call the .ctors. - * These are called in the reverse order they are in the ELF file. - */ -static void __do_global_ctors_aux(void) __used; - -static crt_func __CTOR_END__[] __section(".ctors") __used = { - (crt_func)0 -}; -static crt_func __DTOR_END__[] __section(".dtors") __used = { - (crt_func)0 -}; - -static void -__do_global_ctors_aux(void) -{ - crt_func fn; - int n; - - for (n = 1;; n++) { - fn = __CTOR_END__[-n]; - if (fn == (crt_func)0 || fn == (crt_func)-1) - break; - fn(); - } -} - -asm ( - ".pushsection .init \n" - "\t" INIT_CALL_SEQ(__do_global_ctors_aux) "\n" - ".popsection \n" -); -#endif diff --git a/lib/libc/freebsd/lib/csu/common/feature_note.S b/lib/libc/freebsd/lib/csu/common/feature_note.S index 5c1d56d4cd..fb9f614169 100644 --- a/lib/libc/freebsd/lib/csu/common/feature_note.S +++ b/lib/libc/freebsd/lib/csu/common/feature_note.S @@ -29,7 +29,7 @@ #include #include "notes.h" - .section .note.tag,"a",%note + .section .note.tag,"aR",%note .p2align 2 .4byte 2f-1f .4byte 4f-3f diff --git a/lib/libc/freebsd/lib/csu/common/ignore_init_note.S b/lib/libc/freebsd/lib/csu/common/ignore_init_note.S index 57fb4e8f4d..d78be61f17 100644 --- a/lib/libc/freebsd/lib/csu/common/ignore_init_note.S +++ b/lib/libc/freebsd/lib/csu/common/ignore_init_note.S @@ -30,7 +30,7 @@ #include "notes.h" - .section .note.tag,"a",%note + .section .note.tag,"aR",%note .p2align 2 .4byte 2f-1f .4byte 4f-3f diff --git a/lib/libc/freebsd/lib/csu/powerpc/crt.h b/lib/libc/freebsd/lib/csu/powerpc/crt.h index f8b37f6584..90da2bb9ca 100644 --- a/lib/libc/freebsd/lib/csu/powerpc/crt.h +++ b/lib/libc/freebsd/lib/csu/powerpc/crt.h @@ -24,8 +24,7 @@ #ifndef _CRT_H_ #define _CRT_H_ -/* zig patch: no HAVE_CTORS */ -#define CTORS_CONSTRUCTORS +/* zig patch: no HAVE_CTORS/CTORS_CONSTRUCTORS */ #define INIT_CALL_SEQ(func) "bl " __STRING(func) "; nop" #endif diff --git a/lib/libc/freebsd/lib/csu/powerpc64/crt.h b/lib/libc/freebsd/lib/csu/powerpc64/crt.h index f8b37f6584..90da2bb9ca 100644 --- a/lib/libc/freebsd/lib/csu/powerpc64/crt.h +++ b/lib/libc/freebsd/lib/csu/powerpc64/crt.h @@ -24,8 +24,7 @@ #ifndef _CRT_H_ #define _CRT_H_ -/* zig patch: no HAVE_CTORS */ -#define CTORS_CONSTRUCTORS +/* zig patch: no HAVE_CTORS/CTORS_CONSTRUCTORS */ #define INIT_CALL_SEQ(func) "bl " __STRING(func) "; nop" #endif diff --git a/lib/libc/freebsd/lib/libc/include/libc_private.h b/lib/libc/freebsd/lib/libc/include/libc_private.h index e051961712..db4cbc32be 100644 --- a/lib/libc/freebsd/lib/libc/include/libc_private.h +++ b/lib/libc/freebsd/lib/libc/include/libc_private.h @@ -37,6 +37,8 @@ #include #include +#include + extern char **environ; /* @@ -185,10 +187,12 @@ typedef enum { PJT_GETTHREADID_NP, PJT_ATTR_GET_NP, PJT_GETNAME_NP, + PJT_SUSPEND_ALL_NP, + PJT_RESUME_ALL_NP, PJT_MAX } pjt_index_t; -typedef int (*pthread_func_t)(void); +typedef void (*pthread_func_t)(void); typedef pthread_func_t pthread_func_entry_t[2]; extern pthread_func_entry_t __thr_jtable[]; @@ -197,9 +201,10 @@ void __set_error_selector(int *(*arg)(void)); int _pthread_mutex_init_calloc_cb_stub(pthread_mutex_t *mutex, void *(calloc_cb)(__size_t, __size_t)); -typedef int (*interpos_func_t)(void); +typedef void (*interpos_func_t)(void); interpos_func_t *__libc_interposing_slot(int interposno); extern interpos_func_t __libc_interposing[] __hidden; +interpos_func_t *__libsys_interposing_slot(int interposno); enum { INTERPOS_accept, @@ -244,11 +249,18 @@ enum { INTERPOS_map_stacks_exec, INTERPOS_fdatasync, INTERPOS_clock_nanosleep, - INTERPOS_distribute_static_tls, + INTERPOS__reserved0, /* was distribute_static_tls */ INTERPOS_pdfork, + INTERPOS_uexterr_gettext, INTERPOS_MAX }; +#define _INTERPOS_SYS(type, idx, ...) \ + ((type *)*(__libc_interposing_slot(idx)))(__VA_ARGS__) +#define INTERPOS_SYS(syscall, ...) \ + _INTERPOS_SYS(__sys_## syscall ##_t, INTERPOS_## syscall \ + __VA_OPT__(,) __VA_ARGS__) + /* * yplib internal interfaces */ @@ -333,102 +345,22 @@ struct __siginfo; struct __ucontext; struct __wrusage; enum idtype; -int __sys_aio_suspend(const struct aiocb * const[], int, - const struct timespec *); -int __sys_accept(int, struct sockaddr *, __socklen_t *); -int __sys_accept4(int, struct sockaddr *, __socklen_t *, int); -int __sys_clock_gettime(__clockid_t, struct timespec *ts); -int __sys_clock_nanosleep(__clockid_t, int, - const struct timespec *, struct timespec *); -int __sys_close(int); -int __sys_close_range(unsigned, unsigned, int); -int __sys_connect(int, const struct sockaddr *, __socklen_t); -int __sys_fcntl(int, int, ...); -int __sys_fdatasync(int); -int __sys_fstat(int fd, struct stat *); -int __sys_fstatfs(int fd, struct statfs *); -int __sys_fstatat(int, const char *, struct stat *, int); -int __sys_fsync(int); -__pid_t __sys_fork(void); -int __sys_ftruncate(int, __off_t); -__ssize_t __sys_getdirentries(int, char *, __size_t, __off_t *); -int __sys_getfsstat(struct statfs *, long, int); -int __sys_gettimeofday(struct timeval *, struct timezone *); -int __sys_kevent(int, const struct kevent *, int, struct kevent *, - int, const struct timespec *); -__off_t __sys_lseek(int, __off_t, int); -void *__sys_mmap(void *, __size_t, int, int, int, __off_t); -int __sys_msync(void *, __size_t, int); -int __sys_nanosleep(const struct timespec *, struct timespec *); -int __sys_open(const char *, int, ...); -int __sys_openat(int, const char *, int, ...); -int __sys_pdfork(int *, int); -int __sys_pselect(int, struct fd_set *, struct fd_set *, - struct fd_set *, const struct timespec *, - const __sigset_t *); -int __sys_ptrace(int, __pid_t, char *, int); -int __sys_poll(struct pollfd *, unsigned, int); -int __sys_ppoll(struct pollfd *, unsigned, const struct timespec *, - const __sigset_t *); -__ssize_t __sys_pread(int, void *, __size_t, __off_t); -__ssize_t __sys_pwrite(int, const void *, __size_t, __off_t); -__ssize_t __sys_read(int, void *, __size_t); -__ssize_t __sys_readv(int, const struct iovec *, int); -__ssize_t __sys_recv(int, void *, __size_t, int); -__ssize_t __sys_recvfrom(int, void *, __size_t, int, struct sockaddr *, - __socklen_t *); -__ssize_t __sys_recvmsg(int, struct msghdr *, int); -int __sys_sched_getcpu(void); -int __sys_select(int, struct fd_set *, struct fd_set *, - struct fd_set *, struct timeval *); -__ssize_t __sys_sendmsg(int, const struct msghdr *, int); -__ssize_t __sys_sendto(int, const void *, __size_t, int, - const struct sockaddr *, __socklen_t); -int __sys_setcontext(const struct __ucontext *); -int __sys_sigaction(int, const struct sigaction *, - struct sigaction *); -int __sys_sigprocmask(int, const __sigset_t *, __sigset_t *); -int __sys_sigsuspend(const __sigset_t *); -int __sys_sigtimedwait(const __sigset_t *, struct __siginfo *, - const struct timespec *); -int __sys_sigwait(const __sigset_t *, int *); -int __sys_sigwaitinfo(const __sigset_t *, struct __siginfo *); -int __sys___specialfd(int, const void *, __size_t); -int __sys_statfs(const char *, struct statfs *); -int __sys_swapcontext(struct __ucontext *, - const struct __ucontext *); -int __sys_thr_kill(long, int); -int __sys_thr_self(long *); -int __sys_truncate(const char *, __off_t); -__pid_t __sys_wait4(__pid_t, int *, int, struct rusage *); -__pid_t __sys_wait6(enum idtype, __id_t, int *, int, - struct __wrusage *, struct __siginfo *); -__ssize_t __sys_write(int, const void *, __size_t); -__ssize_t __sys_writev(int, const struct iovec *, int); -int __sys_shm_open2(const char *, int, __mode_t, int, const char *); +int __libc_execvpe(const char *, char * const *, char * const *); int __libc_sigaction(int, const struct sigaction *, struct sigaction *) __hidden; int __libc_sigprocmask(int, const __sigset_t *, __sigset_t *) __hidden; int __libc_sigsuspend(const __sigset_t *) __hidden; -int __libc_sigwait(const __sigset_t * __restrict, - int * restrict sig); +int __libsys_sigwait(const __sigset_t *, int *) __hidden; int __libc_system(const char *); int __libc_tcdrain(int); -int __fcntl_compat(int fd, int cmd, ...); - -int __sys_futimens(int fd, const struct timespec *times) __hidden; -int __sys_utimensat(int fd, const char *path, - const struct timespec *times, int flag) __hidden; int _elf_aux_info(int aux, void *buf, int buflen); struct dl_phdr_info; int __elf_phdr_match_addr(struct dl_phdr_info *, void *); void __init_elf_aux_vector(void); void __libc_map_stacks_exec(void); -void __libc_distribute_static_tls(__size_t, void *, __size_t, __size_t); -__uintptr_t __libc_static_tls_base(__size_t); void _pthread_cancel_enter(int); void _pthread_cancel_leave(int); @@ -444,7 +376,11 @@ struct __nl_cat_d; struct _xlocale; struct __nl_cat_d *__catopen_l(const char *name, int type, struct _xlocale *locale); -int __strerror_rl(int errnum, char *strerrbuf, __size_t buflen, +int __strerror_rl(int errnum, char *strerrbuf, size_t buflen, struct _xlocale *locale); +struct uexterror; +int __uexterr_format(const struct uexterror *ue, char *buf, size_t bufsz); +int __libc_uexterr_gettext(char *buf, size_t bufsz); + #endif /* _LIBC_PRIVATE_H_ */ diff --git a/src/libs/freebsd.zig b/src/libs/freebsd.zig index 439ee903a5..47216c2a62 100644 --- a/src/libs/freebsd.zig +++ b/src/libs/freebsd.zig @@ -139,10 +139,6 @@ pub fn buildCrtFile(comp: *Compilation, crt_file: CrtFile, prog_node: std.Progre .path = "common" ++ path.sep_str ++ "crtbrand.S", .flags = acflags.items, }, - .{ - .path = "common" ++ path.sep_str ++ "crtend.c", - .flags = cflags.items, - }, .{ .path = "common" ++ path.sep_str ++ "feature_note.S", .flags = acflags.items,