From nobody Sat Mar 11 22:51:16 2023 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4PYynw5xYgz3wyrX; Sat, 11 Mar 2023 22:51:16 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4PYynw5ltHz4HQ8; Sat, 11 Mar 2023 22:51:16 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1678575076; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=KHfliLPaG5PmEv98HBsw1GcHRJ3Pv5xNEYxdGwjGhVc=; b=c9GWNBNnn1o7MA1rD6X762RG95DhRYYMvALi2scu9WjA3xWOOHLC7fWih3/1gfWgHqCxUo +wTXW/8A1e1EDrQSe46yMaIYfe12xfjZyI74oxW15d/BSCAKMNfP7SxGqsLLsBvPN4qaW/ vJa1jOtu1U1WC5Toa4zEZvDGqBuHwBe3Uv1k2afFEBD0B/fsxlWCDZjNrtVHbznjE/yY3D oXcEVWCUxxpZ0LWxFcnkgJ6FFLGzuOlwoIjZ/HptfNCLmxWdE0HxOllTCOParFS/71raAJ C/qfJrUzkp0vMe9CLORhVnAhJlfESHyYPRUkl1V5OUQpkOEdiBjokSQxK+g4RA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1678575076; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=KHfliLPaG5PmEv98HBsw1GcHRJ3Pv5xNEYxdGwjGhVc=; b=Nu5cRZ2yszG+kU1Vwic3UNBdv+MYyJ3z7MRVcLkgQ+IXzZE+8aL8N7Iv38ORoKFUahGdUY OJ0GNIPQ4kJ9BRMyf2uAWo1h79Mhn1SLlqS9J3I5hOHdKgEiyHbvhyp3HgA4TjOiZ0n/zf +yZKudSVSnWzJfpYQ3jC838e2PHxzKEJpLQZc+Mk0Q3OFaEfr+IH48HbvfqYzdaeu08ZWc BodZiaMkXfWtm9DNgf9cf4oq+TttiGBF2Vfut6Nbamk/WYRPKHWGhIHPiECZfKSkH13t8i oDy+R6jC/Y+a+MqnNeqHQoNEQhyfhBuC56YmGK+pqnh1SyAJZ9tFGMHVCidnlw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1678575076; a=rsa-sha256; cv=none; b=TsSyCoqluQgm6/geRsZhcXJvr1eNx4CvkgjiDNz+Rx3wqlPwPxDShnn8XLq2pyN4cR7Dyv OOKnkUXZL1JAHnibr9oBVuwM2gVHAC+dXul3lNE4BMH3Dlm6EBMgDkef7hUxvOzHpUSkJe WzeH4Lav6rzp3putYMwa7n8Br9tG2fj8fg7r3PCzzj7oMZDNQNK+/epsvGLf/VqfEW4+NT bFmvYzE2/QEoGthLURQo8KvjbEgub41iYFxxGdy+1jKXhIl8kwlPT8H4L8hPB8ADzb4Wnk U+fJ/4kJTYs93eWkasVOXo/YUTdPg9gHNFaFuhpGB8rCkkyT+QmyKG5OZgNylg== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4PYynw4jYtznsv; Sat, 11 Mar 2023 22:51:16 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 32BMpGA5090382; Sat, 11 Mar 2023 22:51:16 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 32BMpGnE090381; Sat, 11 Mar 2023 22:51:16 GMT (envelope-from git) Date: Sat, 11 Mar 2023 22:51:16 GMT Message-Id: <202303112251.32BMpGnE090381@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Konstantin Belousov Subject: git: 51015e6d0f57 - main - csu: move common code to libc List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kib X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 51015e6d0f570239b0c2088dc6cf2b018928375d Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=51015e6d0f570239b0c2088dc6cf2b018928375d commit 51015e6d0f570239b0c2088dc6cf2b018928375d Author: Konstantin Belousov AuthorDate: 2022-10-30 23:47:44 +0000 Commit: Konstantin Belousov CommitDate: 2023-03-11 22:50:03 +0000 csu: move common code to libc Why? Most trivial point, it shaves around 600 bytes from the dynamic binaries on amd64. Less trivial, the removed code is no longer part of the ABI, and we can ship updates to it with libc updates. Right now most of the csu is linked into the binaries and require us to do somewhat tricky ABI compat when it needs to change. For instance, the init_array change would be much simpler and does not require note tagging if we have init calling code in libc. This could be improved more, by splitting dynamic and static initialization. For instance, &_DYNAMIC tests can be removed then. Such change, nonetheless, would require building libc three times. I left this for later, after this change stabilizes, if ever. Reviewed by: markj Discussed with: jrtc27 (some objections, see the review), imp Tested by: markj (aarch64) Sponsored by: The FreeBSD Foundation MFC after: 3 weeks Differential revision: https://reviews.freebsd.org/D37220 --- lib/csu/aarch64/Makefile | 1 - lib/csu/aarch64/crt1_c.c | 33 +++------------ lib/csu/amd64/Makefile | 2 +- lib/csu/amd64/crt1_c.c | 30 +++----------- lib/csu/arm/Makefile | 2 - lib/csu/arm/crt1_c.c | 31 ++++---------- lib/csu/common/csu_common.h | 50 ++++++++++++++++++++++ lib/csu/i386/Makefile | 1 - lib/csu/i386/crt1_c.c | 30 +++----------- lib/csu/powerpc/Makefile | 1 - lib/csu/powerpc/crt1_c.c | 28 +++---------- lib/csu/powerpc64/Makefile | 2 +- lib/csu/powerpc64/crt1_c.c | 61 +++------------------------ lib/csu/riscv/Makefile | 2 - lib/csu/riscv/crt1_c.c | 29 +++---------- lib/libc/Makefile | 1 + lib/libc/csu/Makefile.inc | 10 +++++ lib/libc/csu/Symbol.map | 4 ++ lib/libc/csu/aarch64/Makefile.inc | 4 ++ lib/{ => libc}/csu/aarch64/reloc.c | 0 lib/libc/csu/amd64/Makefile.inc | 4 ++ lib/{ => libc}/csu/amd64/reloc.c | 0 lib/libc/csu/arm/Makefile.inc | 4 ++ lib/libc/csu/i386/Makefile.inc | 4 ++ lib/{ => libc}/csu/i386/reloc.c | 0 lib/{csu/common => libc/csu}/ignore_init.c | 66 +++++++++++++++++++++++++----- lib/libc/csu/powerpc/Makefile.inc | 4 ++ lib/libc/csu/powerpc64/Makefile.inc | 4 ++ lib/{ => libc}/csu/powerpc64/reloc.c | 26 ++++++++++++ lib/libc/csu/riscv/Makefile.inc | 4 ++ lib/libc/include/libc_private.h | 6 +++ 31 files changed, 220 insertions(+), 224 deletions(-) diff --git a/lib/csu/aarch64/Makefile b/lib/csu/aarch64/Makefile index e4c9029fcacc..d8d0d3fd8f99 100644 --- a/lib/csu/aarch64/Makefile +++ b/lib/csu/aarch64/Makefile @@ -3,7 +3,6 @@ .PATH: ${.CURDIR:H}/common CFLAGS+= -I${.CURDIR} -CFLAGS+= -DCRT_IRELOC_RELA CRT1OBJS+= crt1_s.o diff --git a/lib/csu/aarch64/crt1_c.c b/lib/csu/aarch64/crt1_c.c index 9b3ffbff22d0..7f4fea37ba1a 100644 --- a/lib/csu/aarch64/crt1_c.c +++ b/lib/csu/aarch64/crt1_c.c @@ -32,42 +32,19 @@ #include __FBSDID("$FreeBSD$"); -#include - #include "libc_private.h" -#include "ignore_init.c" - -#ifdef GCRT -extern void _mcleanup(void); -extern void monstartup(void *, void *); -extern int eprol; -extern int etext; -#endif - -extern long * _end; +#include "csu_common.h" -void __start(int, char **, char **, void (*)(void)); +void __start(int, char **, char **, void (*)(void)) __dead2; /* The entry function. */ void __start(int argc, char *argv[], char *env[], void (*cleanup)(void)) { - - handle_argv(argc, argv, env); - - if (&_DYNAMIC != NULL) - atexit(cleanup); - else { - process_irelocs(); - _init_tls(); - } - #ifdef GCRT - atexit(_mcleanup); - monstartup(&eprol, &etext); + __libc_start1_gcrt(argc, argv, env, cleanup, main, &eprol, &etext); __asm__("eprol:"); +#else + __libc_start1(argc, argv, env, cleanup, main); #endif - - handle_static_init(argc, argv, env); - exit(main(argc, argv, env)); } diff --git a/lib/csu/amd64/Makefile b/lib/csu/amd64/Makefile index 19610749b59c..9f3afb4cc1a0 100644 --- a/lib/csu/amd64/Makefile +++ b/lib/csu/amd64/Makefile @@ -3,6 +3,6 @@ .PATH: ${.CURDIR:H}/common CFLAGS+= -I${.CURDIR} -CFLAGS+= -fno-omit-frame-pointer -DCRT_IRELOC_RELA +CFLAGS+= -fno-omit-frame-pointer .include diff --git a/lib/csu/amd64/crt1_c.c b/lib/csu/amd64/crt1_c.c index f1c99de9ffe0..a7b3213e4b9b 100644 --- a/lib/csu/amd64/crt1_c.c +++ b/lib/csu/amd64/crt1_c.c @@ -29,19 +29,10 @@ #include __FBSDID("$FreeBSD$"); -#include - #include "libc_private.h" -#include "ignore_init.c" - -#ifdef GCRT -extern void _mcleanup(void); -extern void monstartup(void *, void *); -extern int eprol; -extern int etext; -#endif +#include "csu_common.h" -void _start(char **, void (*)(void)); +void _start(char **, void (*)(void)) __dead2; /* The entry function. */ void @@ -54,21 +45,10 @@ _start(char **ap, void (*cleanup)(void)) argc = *(long *)(void *)ap; argv = ap + 1; env = ap + 2 + argc; - handle_argv(argc, argv, env); - - if (&_DYNAMIC != NULL) { - atexit(cleanup); - } else { - process_irelocs(); - _init_tls(); - } - #ifdef GCRT - atexit(_mcleanup); - monstartup(&eprol, &etext); + __libc_start1_gcrt(argc, argv, env, cleanup, main, &eprol, &etext); __asm__("eprol:"); +#else + __libc_start1(argc, argv, env, cleanup, main); #endif - - handle_static_init(argc, argv, env); - exit(main(argc, argv, env)); } diff --git a/lib/csu/arm/Makefile b/lib/csu/arm/Makefile index 666bf569b786..a39afde66300 100644 --- a/lib/csu/arm/Makefile +++ b/lib/csu/arm/Makefile @@ -2,8 +2,6 @@ .PATH: ${.CURDIR:H}/common -CFLAGS+= -DCRT_IRELOC_SUPPRESS - CRT1OBJS+= crt1_s.o .include diff --git a/lib/csu/arm/crt1_c.c b/lib/csu/arm/crt1_c.c index 9725f173ff78..8a4abf491bb3 100644 --- a/lib/csu/arm/crt1_c.c +++ b/lib/csu/arm/crt1_c.c @@ -46,50 +46,33 @@ __FBSDID("$FreeBSD$"); #include #include -#include #include "libc_private.h" -#include "ignore_init.c" +#include "csu_common.h" struct Struct_Obj_Entry; struct ps_strings; -extern void _start(int, char **, char **, const struct Struct_Obj_Entry *, - void (*)(void), struct ps_strings *); - -#ifdef GCRT -extern void _mcleanup(void); -extern void monstartup(void *, void *); -extern int eprol; -extern int etext; -#endif +void _start(int, char **, char **, const struct Struct_Obj_Entry *, + void (*)(void), struct ps_strings *) __dead2; struct ps_strings *__ps_strings; void __start(int, char **, char **, struct ps_strings *, - const struct Struct_Obj_Entry *, void (*)(void)); + const struct Struct_Obj_Entry *, void (*)(void)) __dead2; -/* ARGSUSED */ void __start(int argc, char **argv, char **env, struct ps_strings *ps_strings, const struct Struct_Obj_Entry *obj __unused, void (*cleanup)(void)) { - - handle_argv(argc, argv, env); - if (ps_strings != (struct ps_strings *)0) __ps_strings = ps_strings; - if (&_DYNAMIC != NULL) - atexit(cleanup); - else - _init_tls(); #ifdef GCRT - atexit(_mcleanup); - monstartup(&eprol, &etext); + __libc_start1_gcrt(argc, argv, env, cleanup, main, &eprol, &etext); +#else + __libc_start1(argc, argv, env, cleanup, main); #endif - handle_static_init(argc, argv, env); - exit(main(argc, argv, env)); } #ifdef GCRT diff --git a/lib/csu/common/csu_common.h b/lib/csu/common/csu_common.h new file mode 100644 index 000000000000..76f192f72885 --- /dev/null +++ b/lib/csu/common/csu_common.h @@ -0,0 +1,50 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause-FreeBSD + * + * Copyright 1996-1998 John D. Polstra. + * All rights reserved. + * + * 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. + * 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. + */ + +#ifdef _CSU_COMMON_H_ + +/* + * This file includes both definitions and declarations, it can be + * included only into one compilation unit for csu objects. We cannot + * practically check this, but at least guard against + * double-inclusion. + */ +#error "Include this file only once" +#else +#define _CSU_COMMON_H_ + +char **environ; +const char *__progname = ""; + +#ifdef GCRT +extern int eprol; +extern int etext; +#endif + +int main(int, char **, char **); + +#endif /* _CSU_COMMON_H_ */ diff --git a/lib/csu/i386/Makefile b/lib/csu/i386/Makefile index cfa7b4d5b399..d8d0d3fd8f99 100644 --- a/lib/csu/i386/Makefile +++ b/lib/csu/i386/Makefile @@ -3,7 +3,6 @@ .PATH: ${.CURDIR:H}/common CFLAGS+= -I${.CURDIR} -CFLAGS+= -DCRT_IRELOC_REL CRT1OBJS+= crt1_s.o diff --git a/lib/csu/i386/crt1_c.c b/lib/csu/i386/crt1_c.c index b39134824f7e..d49c763749a3 100644 --- a/lib/csu/i386/crt1_c.c +++ b/lib/csu/i386/crt1_c.c @@ -29,20 +29,10 @@ #include __FBSDID("$FreeBSD$"); -#include - #include "libc_private.h" -#include "ignore_init.c" - -extern void _start(char *, ...); - -#ifdef GCRT -extern void _mcleanup(void); -extern void monstartup(void *, void *); -extern int eprol; -extern int etext; -#endif +#include "csu_common.h" +void _start(char *, ...); void _start1(void (*)(void), int, char *[]) __dead2; /* The entry function, C part. */ @@ -52,22 +42,12 @@ _start1(void (*cleanup)(void), int argc, char *argv[]) char **env; env = argv + argc + 1; - handle_argv(argc, argv, env); - if (&_DYNAMIC != NULL) { - atexit(cleanup); - } else { - process_irelocs(); - _init_tls(); - } - #ifdef GCRT - atexit(_mcleanup); - monstartup(&eprol, &etext); + __libc_start1_gcrt(argc, argv, env, cleanup, main, &eprol, &etext); __asm__("eprol:"); +#else + __libc_start1(argc, argv, env, cleanup, main); #endif - - handle_static_init(argc, argv, env); - exit(main(argc, argv, env)); } __asm(".hidden _start1"); diff --git a/lib/csu/powerpc/Makefile b/lib/csu/powerpc/Makefile index 44be804c7c3f..a4d7846b8574 100644 --- a/lib/csu/powerpc/Makefile +++ b/lib/csu/powerpc/Makefile @@ -3,6 +3,5 @@ .PATH: ${.CURDIR:H}/common OBJS+= crtsavres.o -CFLAGS+= -DCRT_IRELOC_SUPPRESS .include diff --git a/lib/csu/powerpc/crt1_c.c b/lib/csu/powerpc/crt1_c.c index 8ad291bb4f4d..72fc7040e237 100644 --- a/lib/csu/powerpc/crt1_c.c +++ b/lib/csu/powerpc/crt1_c.c @@ -47,22 +47,15 @@ __FBSDID("$FreeBSD$"); #include #include "libc_private.h" -#include "ignore_init.c" +#include "csu_common.h" struct Struct_Obj_Entry; struct ps_strings; -#ifdef GCRT -extern void _mcleanup(void); -extern void monstartup(void *, void *); -extern int eprol; -extern int etext; -#endif - struct ps_strings *__ps_strings; void _start(int, char **, char **, const struct Struct_Obj_Entry *, - void (*)(void), struct ps_strings *); + void (*)(void), struct ps_strings *) __dead2; /* The entry function. */ /* @@ -75,25 +68,14 @@ _start(int argc, char **argv, char **env, const struct Struct_Obj_Entry *obj __unused, void (*cleanup)(void), struct ps_strings *ps_strings) { - - - handle_argv(argc, argv, env); - if (ps_strings != (struct ps_strings *)0) __ps_strings = ps_strings; - if (&_DYNAMIC != NULL) - atexit(cleanup); - else - _init_tls(); - #ifdef GCRT - atexit(_mcleanup); - monstartup(&eprol, &etext); + __libc_start1_gcrt(argc, argv, env, cleanup, main, &eprol, &etext); +#else + __libc_start1(argc, argv, env, cleanup, main); #endif - - handle_static_init(argc, argv, env); - exit(main(argc, argv, env)); } #ifdef GCRT diff --git a/lib/csu/powerpc64/Makefile b/lib/csu/powerpc64/Makefile index 564e8dfbf288..0101b516a055 100644 --- a/lib/csu/powerpc64/Makefile +++ b/lib/csu/powerpc64/Makefile @@ -4,7 +4,7 @@ OBJS+= crtsavres.o CFLAGS+= -I${.CURDIR} \ - -mlongcall -DCRT_IRELOC_RELA + -mlongcall CLEANFILES+= crtsavres.S diff --git a/lib/csu/powerpc64/crt1_c.c b/lib/csu/powerpc64/crt1_c.c index c9115be75fba..c7b9e89a23a8 100644 --- a/lib/csu/powerpc64/crt1_c.c +++ b/lib/csu/powerpc64/crt1_c.c @@ -44,87 +44,38 @@ #include __FBSDID("$FreeBSD$"); -#include #include #include -static uint32_t cpu_features; -static uint32_t cpu_features2; - #include "libc_private.h" -#include "ignore_init.c" +#include "csu_common.h" struct Struct_Obj_Entry; struct ps_strings; -extern void _start(int, char **, char **, const struct Struct_Obj_Entry *, - void (*)(void), struct ps_strings *); - -#ifdef GCRT -extern void _mcleanup(void); -extern void monstartup(void *, void *); -extern int eprol; -extern int etext; -#endif +void _start(int, char **, char **, const struct Struct_Obj_Entry *, + void (*)(void), struct ps_strings *) __dead2; struct ps_strings *__ps_strings; -static void -init_cpu_features(char **env) -{ - const Elf_Auxinfo *aux; - - /* Find the auxiliary vector on the stack. */ - while (*env++ != 0) /* Skip over environment, and NULL terminator */ - ; - aux = (const Elf_Auxinfo *)env; - - /* Digest the auxiliary vector. */ - for (; aux->a_type != AT_NULL; aux++) { - switch (aux->a_type) { - case AT_HWCAP: - cpu_features = (uint32_t)aux->a_un.a_val; - break; - case AT_HWCAP2: - cpu_features2 = (uint32_t)aux->a_un.a_val; - break; - } - } -} - - /* The entry function. */ /* * First 5 arguments are specified by the PowerPC SVR4 ABI. * The last argument, ps_strings, is a BSD extension. */ -/* ARGSUSED */ void _start(int argc, char **argv, char **env, const struct Struct_Obj_Entry *obj __unused, void (*cleanup)(void), struct ps_strings *ps_strings) { - - handle_argv(argc, argv, env); - if (ps_strings != (struct ps_strings *)0) __ps_strings = ps_strings; - if (&_DYNAMIC != NULL) - atexit(cleanup); - else { - init_cpu_features(env); - process_irelocs(); - _init_tls(); - } - #ifdef GCRT - atexit(_mcleanup); - monstartup(&eprol, &etext); + __libc_start1_gcrt(argc, argv, env, cleanup, main, &eprol, &etext); +#else + __libc_start1(argc, argv, env, cleanup, main); #endif - - handle_static_init(argc, argv, env); - exit(main(argc, argv, env)); } #ifdef GCRT diff --git a/lib/csu/riscv/Makefile b/lib/csu/riscv/Makefile index 666bf569b786..a39afde66300 100644 --- a/lib/csu/riscv/Makefile +++ b/lib/csu/riscv/Makefile @@ -2,8 +2,6 @@ .PATH: ${.CURDIR:H}/common -CFLAGS+= -DCRT_IRELOC_SUPPRESS - CRT1OBJS+= crt1_s.o .include diff --git a/lib/csu/riscv/crt1_c.c b/lib/csu/riscv/crt1_c.c index 11519e46c7f3..e08359e388f8 100644 --- a/lib/csu/riscv/crt1_c.c +++ b/lib/csu/riscv/crt1_c.c @@ -36,37 +36,18 @@ #include __FBSDID("$FreeBSD$"); -#include - #include "libc_private.h" -#include "ignore_init.c" - -#ifdef GCRT -extern void _mcleanup(void); -extern void monstartup(void *, void *); -extern int eprol; -extern int etext; -#endif +#include "csu_common.h" -void __start(int argc, char **argv, char **env, void (*cleanup)(void)); +void __start(int argc, char **argv, char **env, void (*cleanup)(void)) __dead2; void __start(int argc, char **argv, char **env, void (*cleanup)(void)) { - - handle_argv(argc, argv, env); - - if (&_DYNAMIC != NULL) - atexit(cleanup); - else - _init_tls(); - #ifdef GCRT - atexit(_mcleanup); - monstartup(&eprol, &etext); + __libc_start1_gcrt(argc, argv, env, cleanup, main, &eprol, &etext); __asm__("eprol:"); +#else + __libc_start1(argc, argv, env, cleanup, main); #endif - - handle_static_init(argc, argv, env); - exit(main(argc, argv, env)); } diff --git a/lib/libc/Makefile b/lib/libc/Makefile index 2ca5458d1437..2c6f2da361b3 100644 --- a/lib/libc/Makefile +++ b/lib/libc/Makefile @@ -93,6 +93,7 @@ MIASM= NOASM= .include "${LIBC_SRCTOP}/${LIBC_ARCH}/Makefile.inc" +.include "${LIBC_SRCTOP}/csu/Makefile.inc" .include "${LIBC_SRCTOP}/db/Makefile.inc" .include "${LIBC_SRCTOP}/compat-43/Makefile.inc" .include "${LIBC_SRCTOP}/gdtoa/Makefile.inc" diff --git a/lib/libc/csu/Makefile.inc b/lib/libc/csu/Makefile.inc new file mode 100644 index 000000000000..453303bea608 --- /dev/null +++ b/lib/libc/csu/Makefile.inc @@ -0,0 +1,10 @@ +# + +.PATH: ${LIBC_SRCTOP}/csu +.include "${LIBC_SRCTOP}/csu/${LIBC_ARCH}/Makefile.inc" + +SRCS+= \ + ignore_init.c + +CFLAGS+= -I${LIBC_SRCTOP}/csu/${LIBC_ARCH} +SYM_MAPS+=${LIBC_SRCTOP}/csu/Symbol.map diff --git a/lib/libc/csu/Symbol.map b/lib/libc/csu/Symbol.map new file mode 100644 index 000000000000..7fc09add5e45 --- /dev/null +++ b/lib/libc/csu/Symbol.map @@ -0,0 +1,4 @@ +FBSD_1.7 { + __libc_start1; + __libc_start1_gcrt; +}; diff --git a/lib/libc/csu/aarch64/Makefile.inc b/lib/libc/csu/aarch64/Makefile.inc new file mode 100644 index 000000000000..b3420a638164 --- /dev/null +++ b/lib/libc/csu/aarch64/Makefile.inc @@ -0,0 +1,4 @@ +# + +CFLAGS+= -DCRT_IRELOC_RELA \ + -DINIT_IRELOCS="" diff --git a/lib/csu/aarch64/reloc.c b/lib/libc/csu/aarch64/reloc.c similarity index 100% rename from lib/csu/aarch64/reloc.c rename to lib/libc/csu/aarch64/reloc.c diff --git a/lib/libc/csu/amd64/Makefile.inc b/lib/libc/csu/amd64/Makefile.inc new file mode 100644 index 000000000000..b3420a638164 --- /dev/null +++ b/lib/libc/csu/amd64/Makefile.inc @@ -0,0 +1,4 @@ +# + +CFLAGS+= -DCRT_IRELOC_RELA \ + -DINIT_IRELOCS="" diff --git a/lib/csu/amd64/reloc.c b/lib/libc/csu/amd64/reloc.c similarity index 100% rename from lib/csu/amd64/reloc.c rename to lib/libc/csu/amd64/reloc.c diff --git a/lib/libc/csu/arm/Makefile.inc b/lib/libc/csu/arm/Makefile.inc new file mode 100644 index 000000000000..2534e6579f38 --- /dev/null +++ b/lib/libc/csu/arm/Makefile.inc @@ -0,0 +1,4 @@ +# + +CFLAGS+= -DCRT_IRELOC_SUPPRESS \ + -DINIT_IRELOCS="" diff --git a/lib/libc/csu/i386/Makefile.inc b/lib/libc/csu/i386/Makefile.inc new file mode 100644 index 000000000000..ac0984df2349 --- /dev/null +++ b/lib/libc/csu/i386/Makefile.inc @@ -0,0 +1,4 @@ +# + +CFLAGS+= -DCRT_IRELOC_REL \ + -DINIT_IRELOCS="" diff --git a/lib/csu/i386/reloc.c b/lib/libc/csu/i386/reloc.c similarity index 100% rename from lib/csu/i386/reloc.c rename to lib/libc/csu/i386/reloc.c diff --git a/lib/csu/common/ignore_init.c b/lib/libc/csu/ignore_init.c similarity index 77% rename from lib/csu/common/ignore_init.c rename to lib/libc/csu/ignore_init.c index 2cdb869c4a87..60c45d7e735f 100644 --- a/lib/csu/common/ignore_init.c +++ b/lib/libc/csu/ignore_init.c @@ -2,7 +2,7 @@ * SPDX-License-Identifier: BSD-1-Clause * * Copyright 2012 Konstantin Belousov - * Copyright (c) 2018 The FreeBSD Foundation + * Copyright (c) 2018, 2023 The FreeBSD Foundation * * Parts of this software was developed by Konstantin Belousov * under sponsorship from the FreeBSD Foundation. @@ -25,14 +25,11 @@ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include -__FBSDID("$FreeBSD$"); - #include #include #include - -extern int main(int, char **, char **); +#include +#include "libc_private.h" extern void (*__preinit_array_start[])(int, char **, char **) __hidden; extern void (*__preinit_array_end[])(int, char **, char **) __hidden; @@ -79,9 +76,6 @@ process_irelocs(void) #error "Define platform reloc type" #endif -char **environ; -const char *__progname = ""; - static void finalizer(void) { @@ -97,7 +91,7 @@ finalizer(void) _fini(); } -static inline void +static void handle_static_init(int argc, char **argv, char **env) { void (*fn)(int, char **, char **); @@ -123,7 +117,9 @@ handle_static_init(int argc, char **argv, char **env) } } -static inline void +extern char **environ; + +static void handle_argv(int argc, char *argv[], char **env) { const char *s; @@ -138,3 +134,51 @@ handle_argv(int argc, char *argv[], char **env) } } } + +void +__libc_start1(int argc, char *argv[], char *env[], void (*cleanup)(void), + int (*mainX)(int, char *[], char *[])) +{ + handle_argv(argc, argv, env); + + if (&_DYNAMIC != NULL) { + atexit(cleanup); + } else { +#ifndef CRT_IRELOC_SUPPRESS + INIT_IRELOCS; + process_irelocs(); +#endif + _init_tls(); + } + + handle_static_init(argc, argv, env); + exit(mainX(argc, argv, env)); +} + +/* XXXKIB _mcleanup and monstartup defs */ +extern void _mcleanup(void); +extern void monstartup(void *, void *); + +void +__libc_start1_gcrt(int argc, char *argv[], char *env[], + void (*cleanup)(void), int (*mainX)(int, char *[], char *[]), + int *eprolp, int *etextp) +{ + handle_argv(argc, argv, env); + + if (&_DYNAMIC != NULL) { + atexit(cleanup); + } else { +#ifndef CRT_IRELOC_SUPPRESS + INIT_IRELOCS; + process_irelocs(); +#endif + _init_tls(); + } + + atexit(_mcleanup); + monstartup(eprolp, etextp); + + handle_static_init(argc, argv, env); + exit(mainX(argc, argv, env)); +} diff --git a/lib/libc/csu/powerpc/Makefile.inc b/lib/libc/csu/powerpc/Makefile.inc new file mode 100644 index 000000000000..2534e6579f38 --- /dev/null +++ b/lib/libc/csu/powerpc/Makefile.inc @@ -0,0 +1,4 @@ +# + +CFLAGS+= -DCRT_IRELOC_SUPPRESS \ + -DINIT_IRELOCS="" diff --git a/lib/libc/csu/powerpc64/Makefile.inc b/lib/libc/csu/powerpc64/Makefile.inc new file mode 100644 index 000000000000..5d59d40eb393 --- /dev/null +++ b/lib/libc/csu/powerpc64/Makefile.inc @@ -0,0 +1,4 @@ +# + +CFLAGS+= -DCRT_IRELOC_RELA \ + -DINIT_IRELOCS="init_cpu_features(env)" diff --git a/lib/csu/powerpc64/reloc.c b/lib/libc/csu/powerpc64/reloc.c similarity index 74% rename from lib/csu/powerpc64/reloc.c rename to lib/libc/csu/powerpc64/reloc.c index afa3f2fbec18..5ba191d07cd9 100644 --- a/lib/csu/powerpc64/reloc.c +++ b/lib/libc/csu/powerpc64/reloc.c @@ -23,6 +23,32 @@ #include __FBSDID("$FreeBSD$"); +static uint32_t cpu_features; +static uint32_t cpu_features2; + +static void +init_cpu_features(char **env) +{ + const Elf_Auxinfo *aux; + + /* Find the auxiliary vector on the stack. */ + while (*env++ != 0) /* Skip over environment, and NULL terminator */ + ; + aux = (const Elf_Auxinfo *)env; + + /* Digest the auxiliary vector. */ + for (; aux->a_type != AT_NULL; aux++) { + switch (aux->a_type) { + case AT_HWCAP: + cpu_features = (uint32_t)aux->a_un.a_val; + break; + case AT_HWCAP2: + cpu_features2 = (uint32_t)aux->a_un.a_val; + break; + } + } +} + static void crt1_handle_rela(const Elf_Rela *r) { diff --git a/lib/libc/csu/riscv/Makefile.inc b/lib/libc/csu/riscv/Makefile.inc new file mode 100644 index 000000000000..2534e6579f38 --- /dev/null +++ b/lib/libc/csu/riscv/Makefile.inc @@ -0,0 +1,4 @@ +# + +CFLAGS+= -DCRT_IRELOC_SUPPRESS \ + -DINIT_IRELOCS="" diff --git a/lib/libc/include/libc_private.h b/lib/libc/include/libc_private.h index 0a8e531e0c3c..67750d354f22 100644 --- a/lib/libc/include/libc_private.h +++ b/lib/libc/include/libc_private.h @@ -255,6 +255,12 @@ enum { int _yp_check(char **); #endif +void __libc_start1(int, char *[], char *[], + void (*)(void), int (*)(int, char *[], char *[])) __dead2; +void __libc_start1_gcrt(int, char *[], char *[], + void (*)(void), int (*)(int, char *[], char *[]), + int *, int *) __dead2; + /* * Initialise TLS for static programs */