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 @@ -1,10 +1,9 @@ # $FreeBSD$ .PATH: ${.CURDIR:H}/common CFLAGS+= -I${.CURDIR} -CFLAGS+= -DCRT_IRELOC_RELA CRT1OBJS+= crt1_s.o .include 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 @@ -1,73 +1,50 @@ /* LINTLIBRARY */ /*- * Copyright 1996-1998 John D. Polstra. * Copyright 2014 Andrew Turner. * Copyright 2014-2015 The FreeBSD Foundation. * All rights reserved. * * Portions of this software were developed by Andrew Turner * under sponsorship from the FreeBSD Foundation. * * 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. */ #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 @@ -1,8 +1,8 @@ # $FreeBSD$ .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 @@ -1,74 +1,54 @@ /* LINTLIBRARY */ /*- * 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. */ #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 _start(char **ap, void (*cleanup)(void)) { int argc; char **argv; char **env; 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 @@ -1,9 +1,7 @@ # $FreeBSD$ .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 @@ -1,99 +1,82 @@ /* LINTLIBRARY */ /*- * SPDX-License-Identifier: BSD-4-Clause * * Copyright 2001 David E. O'Brien. * All rights reserved. * Copyright 1996-1998 John D. Polstra. * All rights reserved. * Copyright (c) 1997 Jason R. Thorpe. * Copyright (c) 1995 Christopher G. Demetriou * 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. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed for the * FreeBSD Project. See https://www.freebsd.org/ for * information about FreeBSD. * This product includes software developed for the * NetBSD Project. See http://www.netbsd.org/ for * information about NetBSD. * 4. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission * * 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 __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 __asm__(".text"); __asm__("eprol:"); __asm__(".previous"); #endif diff --git a/lib/csu/amd64/crt1_c.c b/lib/csu/common/csu_common.h similarity index 66% copy from lib/csu/amd64/crt1_c.c copy to lib/csu/common/csu_common.h index f1c99de9ffe0..76f192f72885 100644 --- a/lib/csu/amd64/crt1_c.c +++ b/lib/csu/common/csu_common.h @@ -1,74 +1,50 @@ -/* LINTLIBRARY */ /*- * 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. */ -#include -__FBSDID("$FreeBSD$"); +#ifdef _CSU_COMMON_H_ -#include +/* + * 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_ -#include "libc_private.h" -#include "ignore_init.c" +char **environ; +const char *__progname = ""; #ifdef GCRT -extern void _mcleanup(void); -extern void monstartup(void *, void *); extern int eprol; extern int etext; #endif -void _start(char **, void (*)(void)); - -/* The entry function. */ -void -_start(char **ap, void (*cleanup)(void)) -{ - int argc; - char **argv; - char **env; - - 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); -__asm__("eprol:"); -#endif +int main(int, char **, char **); - handle_static_init(argc, argv, env); - exit(main(argc, argv, env)); -} +#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 @@ -1,10 +1,9 @@ # $FreeBSD$ .PATH: ${.CURDIR:H}/common CFLAGS+= -I${.CURDIR} -CFLAGS+= -DCRT_IRELOC_REL CRT1OBJS+= crt1_s.o .include 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 @@ -1,73 +1,53 @@ /* LINTLIBRARY */ /*- * 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. * * $FreeBSD$ */ #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. */ void _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 @@ -1,8 +1,7 @@ # $FreeBSD$ .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 @@ -1,109 +1,91 @@ /* LINTLIBRARY */ /*- * SPDX-License-Identifier: BSD-4-Clause * * Copyright 2001 David E. O'Brien. * All rights reserved. * Copyright 1996-1998 John D. Polstra. * All rights reserved. * Copyright (c) 1997 Jason R. Thorpe. * Copyright (c) 1995 Christopher G. Demetriou * 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. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed for the * FreeBSD Project. See https://www.freebsd.org/ for * information about FreeBSD. * This product includes software developed for the * NetBSD Project. See http://www.netbsd.org/ for * information about NetBSD. * 4. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission * * 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 __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. */ /* * 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_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 __asm__(".text"); __asm__("eprol:"); __asm__(".previous"); #endif #ifndef PIC __asm__(".text\n" "\t.global _GLOBAL_OFFSET_TABLE_\n" "\t.reloc 0, R_PPC_NONE, _GLOBAL_OFFSET_TABLE_"); #endif 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 @@ -1,15 +1,15 @@ # $FreeBSD$ .PATH: ${.CURDIR:H}/common OBJS+= crtsavres.o CFLAGS+= -I${.CURDIR} \ - -mlongcall -DCRT_IRELOC_RELA + -mlongcall CLEANFILES+= crtsavres.S # On powerpc64 crtsavres is an empty file crtsavres.S: touch ${.TARGET} .include 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 @@ -1,134 +1,85 @@ /* LINTLIBRARY */ /*- * SPDX-License-Identifier: BSD-4-Clause * * Copyright 2001 David E. O'Brien. * All rights reserved. * Copyright 1996-1998 John D. Polstra. * All rights reserved. * Copyright (c) 1997 Jason R. Thorpe. * Copyright (c) 1995 Christopher G. Demetriou * 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. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed for the * FreeBSD Project. See https://www.freebsd.org/ for * information about FreeBSD. * This product includes software developed for the * NetBSD Project. See http://www.netbsd.org/ for * information about NetBSD. * 4. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission * * 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 __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 __asm__(".text"); __asm__("eprol:"); __asm__(".previous"); #endif 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 @@ -1,9 +1,7 @@ # $FreeBSD$ .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 @@ -1,72 +1,53 @@ /* LINTLIBRARY */ /*- * Copyright 1996-1998 John D. Polstra. * Copyright (c) 2015-2017 Ruslan Bukin * All rights reserved. * * Portions of this software were developed by SRI International and the * University of Cambridge Computer Laboratory under DARPA/AFRL contract * FA8750-10-C-0237 ("CTSRD"), as part of the DARPA CRASH research programme. * * Portions of this software were developed by the University of Cambridge * Computer Laboratory as part of the CTSRD Project, with support from the * UK Higher Education Innovation Fund (HEIF). * * 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. */ #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 @@ -1,224 +1,225 @@ # @(#)Makefile 8.2 (Berkeley) 2/3/94 # $FreeBSD$ PACKAGE= clibs SHLIBDIR?= /lib .include # Force building of libc_pic.a MK_TOOLCHAIN= yes LIBC_SRCTOP?= ${.CURDIR} # Pick the current architecture directory for libc. In general, this is # named MACHINE_CPUARCH, but some ABIs are different enough to require # their own libc, so allow a directory named MACHINE_ARCH to override this. .if exists(${LIBC_SRCTOP}/${MACHINE_ARCH:S/powerpc64le/powerpc64/}) LIBC_ARCH=${MACHINE_ARCH:S/powerpc64le/powerpc64/} .else LIBC_ARCH=${MACHINE_CPUARCH} .endif # All library objects contain FreeBSD revision strings by default; they may be # excluded as a space-saving measure. To produce a library that does # not contain these strings, add -DSTRIP_FBSDID (see ) to CFLAGS # below. Note: there are no IDs for syscall stubs whose sources are generated. # To include legacy CSRG SCCS ID strings, remove -DNO__SCCSID from CFLAGS. # To include RCS ID strings from other BSD projects, remove -DNO__RCSID from CFLAGS. CFLAGS+=-DNO__SCCSID -DNO__RCSID LIB=c SHLIB_MAJOR= 7 .if ${MK_SSP} != "no" && \ (${LIBC_ARCH} == "i386" || ${LIBC_ARCH:Mpowerpc*} != "") SHLIB_LDSCRIPT=libc.ldscript .else SHLIB_LDSCRIPT=libc_nossp.ldscript .endif SHLIB_LDSCRIPT_LINKS=libxnet.so WARNS?= 2 CFLAGS+=-I${LIBC_SRCTOP}/include -I${SRCTOP}/include CFLAGS+=-I${LIBC_SRCTOP}/${LIBC_ARCH} .if ${MK_NLS} != "no" CFLAGS+=-DNLS .endif CLEANFILES+=tags INSTALL_PIC_ARCHIVE= BUILD_NOSSP_PIC_ARCHIVE= PRECIOUSLIB= .ifndef NO_THREAD_STACK_UNWIND CANCELPOINTS_CFLAGS=-fexceptions CFLAGS+=${CANCELPOINTS_CFLAGS} .endif # Use a more efficient TLS model for libc since we can reasonably assume that # it will be loaded during program startup. .if ${LIBC_ARCH} == "aarch64" || ${LIBC_ARCH} == "amd64" || \ ${LIBC_ARCH} == "i386" || ${LIBC_ARCH} == "riscv" || \ ${LIBC_ARCH:Mpowerpc*} != "" CFLAGS+= -ftls-model=initial-exec .endif # # Link with static libcompiler_rt.a. # LDFLAGS+= -nodefaultlibs LIBADD+= compiler_rt .if ${MK_SSP} != "no" && \ (${LIBC_ARCH} == "i386" || ${LIBC_ARCH:Mpowerpc*} != "") LIBADD+= ssp_nonshared .endif # Extras that live in either libc.a or libc_nonshared.a LIBC_NONSHARED_SRCS= RTLD_ELF_DIR=${SRCTOP}/libexec/rtld-elf .if exists(${RTLD_ELF_DIR}/${MACHINE_ARCH:S/powerpc64le/powerpc64/}) RTLD_ARCH= ${MACHINE_ARCH:S/powerpc64le/powerpc64/} .else RTLD_ARCH= ${MACHINE_CPUARCH} .endif RTLD_HDRS= -I${RTLD_ELF_DIR}/${RTLD_ARCH} -I${RTLD_ELF_DIR} # Define (empty) variables so that make doesn't give substitution # errors if the included makefiles don't change these: MDSRCS= MISRCS= MDASM= 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" .include "${LIBC_SRCTOP}/gen/Makefile.inc" .include "${LIBC_SRCTOP}/gmon/Makefile.inc" .if ${MK_ICONV} != "no" .include "${LIBC_SRCTOP}/iconv/Makefile.inc" .endif .include "${LIBC_SRCTOP}/inet/Makefile.inc" .include "${LIBC_SRCTOP}/isc/Makefile.inc" .include "${LIBC_SRCTOP}/locale/Makefile.inc" .include "${LIBC_SRCTOP}/md/Makefile.inc" .include "${LIBC_SRCTOP}/nameser/Makefile.inc" .include "${LIBC_SRCTOP}/net/Makefile.inc" .include "${LIBC_SRCTOP}/nls/Makefile.inc" .include "${LIBC_SRCTOP}/posix1e/Makefile.inc" .if ${MACHINE_ABI:Mlong32} .include "${LIBC_SRCTOP}/quad/Makefile.inc" .endif .include "${LIBC_SRCTOP}/regex/Makefile.inc" .include "${LIBC_SRCTOP}/resolv/Makefile.inc" .include "${LIBC_SRCTOP}/stdio/Makefile.inc" .include "${LIBC_SRCTOP}/stdlib/Makefile.inc" .include "${LIBC_SRCTOP}/stdlib/jemalloc/Makefile.inc" .include "${LIBC_SRCTOP}/stdtime/Makefile.inc" .include "${LIBC_SRCTOP}/string/Makefile.inc" .include "${LIBC_SRCTOP}/sys/Makefile.inc" .include "${LIBC_SRCTOP}/secure/Makefile.inc" .include "${LIBC_SRCTOP}/rpc/Makefile.inc" .include "${LIBC_SRCTOP}/uuid/Makefile.inc" .include "${LIBC_SRCTOP}/xdr/Makefile.inc" .if (${LIBC_ARCH} == "arm" && (defined(CPUTYPE) && ${CPUTYPE:M*soft*})) || \ (${LIBC_ARCH} == "riscv" && ${MACHINE_ARCH:Mriscv*sf} != "") .include "${LIBC_SRCTOP}/softfloat/Makefile.inc" .endif .if ${LIBC_ARCH} == "i386" || ${LIBC_ARCH} == "amd64" .include "${LIBC_SRCTOP}/x86/sys/Makefile.inc" .include "${LIBC_SRCTOP}/x86/gen/Makefile.inc" .endif .if ${MK_NIS} != "no" CFLAGS+= -DYP .include "${LIBC_SRCTOP}/yp/Makefile.inc" .endif .include "${LIBC_SRCTOP}/capability/Makefile.inc" .if ${MK_HESIOD} != "no" CFLAGS+= -DHESIOD .endif .if ${MK_FP_LIBC} == "no" CFLAGS+= -DNO_FLOATING_POINT .endif .if ${MK_NS_CACHING} != "no" CFLAGS+= -DNS_CACHING .endif .if defined(_FREEFALL_CONFIG) CFLAGS+=-D_FREEFALL_CONFIG .endif STATICOBJS+=${LIBC_NONSHARED_SRCS:S/.c$/.o/} VERSION_DEF=${LIBC_SRCTOP}/Versions.def SYMBOL_MAPS=${SYM_MAPS} CFLAGS+= -DSYMBOL_VERSIONING # If there are no machine dependent sources, append all the # machine-independent sources: .if empty(MDSRCS) || ${MK_MACHDEP_OPTIMIZATIONS} == no SRCS+= ${MISRCS} .else # Append machine-dependent sources, then append machine-independent sources # for which there is no machine-dependent variant. SRCS+= ${MDSRCS} .for _src in ${MISRCS} .if ${MDSRCS:R:M${_src:R}} == "" SRCS+= ${_src} .endif .endfor .endif KQSRCS= adddi3.c anddi3.c ashldi3.c ashrdi3.c cmpdi2.c divdi3.c iordi3.c \ lshldi3.c lshrdi3.c moddi3.c muldi3.c negdi2.c notdi2.c qdivrem.c \ subdi3.c ucmpdi2.c udivdi3.c umoddi3.c xordi3.c KSRCS= bcmp.c ffs.c ffsl.c fls.c flsl.c mcount.c strcat.c strchr.c \ strcmp.c strcpy.c strlen.c strncpy.c strrchr.c libkern: libkern.gen libkern.${LIBC_ARCH} libkern.gen: ${KQSRCS} ${KSRCS} ${CP} ${LIBC_SRCTOP}/quad/quad.h ${.ALLSRC} ${DESTDIR}/sys/libkern libkern.${LIBC_ARCH}:: ${KMSRCS} .if defined(KMSRCS) && !empty(KMSRCS) ${CP} ${.ALLSRC} ${DESTDIR}/sys/libkern/${LIBC_ARCH} .endif HAS_TESTS= SUBDIR.${MK_TESTS}+= tests .include .if (${LIBC_ARCH} == amd64 || ${LIBC_ARCH} == i386) && \ ${.TARGETS:Mall} == all && \ defined(LINKER_FEATURES) && ${LINKER_FEATURES:Mifunc} == "" .error ${LIBC_ARCH} libc requires linker ifunc support .endif .if !defined(_SKIP_BUILD) # We need libutil.h, get it directly to avoid # recording a build dependency CFLAGS+= -I${SRCTOP}/lib/libutil # Same issue with libm MSUN_ARCH_SUBDIR != ${MAKE} -B -C ${SRCTOP}/lib/msun -V ARCH_SUBDIR # unfortunately msun/src contains both private and public headers CFLAGS+= -I${SRCTOP}/lib/msun/${MSUN_ARCH_SUBDIR} .if ${MACHINE_CPUARCH} == "i386" || ${MACHINE_CPUARCH} == "amd64" CFLAGS+= -I${SRCTOP}/lib/msun/x86 .endif CFLAGS+= -I${SRCTOP}/lib/msun/src # and we do not want to record a dependency on msun .if ${.MAKE.LEVEL} > 0 GENDIRDEPS_FILTER+= N${RELDIR:H}/msun .endif .endif # Disable warnings in contributed sources. CWARNFLAGS:= ${.IMPSRC:Ngdtoa_*.c:C/^.+$/${CWARNFLAGS}/:C/^$/-w/} # Disable stack protection for SSP symbols. SSP_CFLAGS:= ${.IMPSRC:N*/stack_protector.c:C/^.+$/${SSP_CFLAGS}/} # Generate stack unwinding tables for cancellation points CANCELPOINTS_CFLAGS:= ${.IMPSRC:Mcancelpoints_*:C/^.+$/${CANCELPOINTS_CFLAGS}/:C/^$//} 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 @@ -1,140 +1,184 @@ /*- * 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. * * 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 -__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; extern void (*__init_array_start[])(int, char **, char **) __hidden; extern void (*__init_array_end[])(int, char **, char **) __hidden; extern void (*__fini_array_start[])(void) __hidden; extern void (*__fini_array_end[])(void) __hidden; extern void _fini(void) __hidden; extern void _init(void) __hidden; extern int _DYNAMIC; #pragma weak _DYNAMIC #if defined(CRT_IRELOC_RELA) extern const Elf_Rela __rela_iplt_start[] __weak_symbol __hidden; extern const Elf_Rela __rela_iplt_end[] __weak_symbol __hidden; #include "reloc.c" static void process_irelocs(void) { const Elf_Rela *r; for (r = &__rela_iplt_start[0]; r < &__rela_iplt_end[0]; r++) crt1_handle_rela(r); } #elif defined(CRT_IRELOC_REL) extern const Elf_Rel __rel_iplt_start[] __weak_symbol __hidden; extern const Elf_Rel __rel_iplt_end[] __weak_symbol __hidden; #include "reloc.c" static void process_irelocs(void) { const Elf_Rel *r; for (r = &__rel_iplt_start[0]; r < &__rel_iplt_end[0]; r++) crt1_handle_rel(r); } #elif defined(CRT_IRELOC_SUPPRESS) #else #error "Define platform reloc type" #endif -char **environ; -const char *__progname = ""; - static void finalizer(void) { void (*fn)(void); size_t array_size, n; array_size = __fini_array_end - __fini_array_start; for (n = array_size; n > 0; n--) { fn = __fini_array_start[n - 1]; if ((uintptr_t)fn != 0 && (uintptr_t)fn != 1) (fn)(); } _fini(); } -static inline void +static void handle_static_init(int argc, char **argv, char **env) { void (*fn)(int, char **, char **); size_t array_size, n; if (&_DYNAMIC != NULL) return; atexit(finalizer); array_size = __preinit_array_end - __preinit_array_start; for (n = 0; n < array_size; n++) { fn = __preinit_array_start[n]; if ((uintptr_t)fn != 0 && (uintptr_t)fn != 1) fn(argc, argv, env); } _init(); array_size = __init_array_end - __init_array_start; for (n = 0; n < array_size; n++) { fn = __init_array_start[n]; if ((uintptr_t)fn != 0 && (uintptr_t)fn != 1) fn(argc, argv, env); } } -static inline void +extern char **environ; + +static void handle_argv(int argc, char *argv[], char **env) { const char *s; if (environ == NULL) environ = env; if (argc > 0 && argv[0] != NULL) { __progname = argv[0]; for (s = __progname; *s != '\0'; s++) { if (*s == '/') __progname = s + 1; } } } + +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 @@ -1,43 +1,69 @@ /*- * Copyright (c) 2019 Leandro Lupori * * 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 AND CONTRIBUTORS ``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 OR CONTRIBUTORS 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 __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) { typedef Elf_Addr (*ifunc_resolver_t)( uint32_t, uint32_t, uint64_t, uint64_t, uint64_t, uint64_t, uint64_t, uint64_t); Elf_Addr *ptr, *where, target; switch (ELF_R_TYPE(r->r_info)) { case R_PPC_IRELATIVE: ptr = (Elf_Addr *)r->r_addend; where = (Elf_Addr *)r->r_offset; target = ((ifunc_resolver_t)ptr)(cpu_features, cpu_features2, 0, 0, 0, 0, 0, 0); *where = target; break; } } 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 @@ -1,444 +1,450 @@ /*- * SPDX-License-Identifier: BSD-3-Clause * * Copyright (c) 1998 John Birrell . * 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. * 3. Neither the name of the author nor the names of any co-contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``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 REGENTS OR CONTRIBUTORS 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. * * $FreeBSD$ * * Private definitions for libc, libc_r and libpthread. * */ #ifndef _LIBC_PRIVATE_H_ #define _LIBC_PRIVATE_H_ #include #include /* * The kernel doesn't expose PID_MAX to the user space. Save it here * to allow to run a newer world on a pre-1400079 kernel. */ #define _PID_MAX 99999 /* * This global flag is non-zero when a process has created one * or more threads. It is used to avoid calling locking functions * when they are not required. */ #ifndef __LIBC_ISTHREADED_DECLARED #define __LIBC_ISTHREADED_DECLARED extern int __isthreaded; #endif /* * Elf_Auxinfo *__elf_aux_vector, the pointer to the ELF aux vector * provided by kernel. Either set for us by rtld, or found at runtime * on stack for static binaries. * * Type is void to avoid polluting whole libc with ELF types. */ extern void *__elf_aux_vector; /* * libc should use libc_dlopen internally, which respects a global * flag where loading of new shared objects can be restricted. */ void *libc_dlopen(const char *, int); /* * For dynamic linker. */ void _rtld_error(const char *fmt, ...); /* * File lock contention is difficult to diagnose without knowing * where locks were set. Allow a debug library to be built which * records the source file and line number of each lock call. */ #ifdef _FLOCK_DEBUG #define _FLOCKFILE(x) _flockfile_debug(x, __FILE__, __LINE__) #else #define _FLOCKFILE(x) _flockfile(x) #endif /* * Macros for locking and unlocking FILEs. These test if the * process is threaded to avoid locking when not required. */ #define FLOCKFILE(fp) if (__isthreaded) _FLOCKFILE(fp) #define FUNLOCKFILE(fp) if (__isthreaded) _funlockfile(fp) struct _spinlock; extern struct _spinlock __stdio_thread_lock __hidden; #define STDIO_THREAD_LOCK() \ do { \ if (__isthreaded) \ _SPINLOCK(&__stdio_thread_lock); \ } while (0) #define STDIO_THREAD_UNLOCK() \ do { \ if (__isthreaded) \ _SPINUNLOCK(&__stdio_thread_lock); \ } while (0) void __libc_spinlock_stub(struct _spinlock *); void __libc_spinunlock_stub(struct _spinlock *); /* * Indexes into the pthread jump table. * * Warning! If you change this type, you must also change the threads * libraries that reference it (libc_r, libpthread). */ typedef enum { PJT_ATFORK, PJT_ATTR_DESTROY, PJT_ATTR_GETDETACHSTATE, PJT_ATTR_GETGUARDSIZE, PJT_ATTR_GETINHERITSCHED, PJT_ATTR_GETSCHEDPARAM, PJT_ATTR_GETSCHEDPOLICY, PJT_ATTR_GETSCOPE, PJT_ATTR_GETSTACKADDR, PJT_ATTR_GETSTACKSIZE, PJT_ATTR_INIT, PJT_ATTR_SETDETACHSTATE, PJT_ATTR_SETGUARDSIZE, PJT_ATTR_SETINHERITSCHED, PJT_ATTR_SETSCHEDPARAM, PJT_ATTR_SETSCHEDPOLICY, PJT_ATTR_SETSCOPE, PJT_ATTR_SETSTACKADDR, PJT_ATTR_SETSTACKSIZE, PJT_CANCEL, PJT_CLEANUP_POP, PJT_CLEANUP_PUSH, PJT_COND_BROADCAST, PJT_COND_DESTROY, PJT_COND_INIT, PJT_COND_SIGNAL, PJT_COND_TIMEDWAIT, PJT_COND_WAIT, PJT_DETACH, PJT_EQUAL, PJT_EXIT, PJT_GETSPECIFIC, PJT_JOIN, PJT_KEY_CREATE, PJT_KEY_DELETE, PJT_KILL, PJT_MAIN_NP, PJT_MUTEXATTR_DESTROY, PJT_MUTEXATTR_INIT, PJT_MUTEXATTR_SETTYPE, PJT_MUTEX_DESTROY, PJT_MUTEX_INIT, PJT_MUTEX_LOCK, PJT_MUTEX_TRYLOCK, PJT_MUTEX_UNLOCK, PJT_ONCE, PJT_RWLOCK_DESTROY, PJT_RWLOCK_INIT, PJT_RWLOCK_RDLOCK, PJT_RWLOCK_TRYRDLOCK, PJT_RWLOCK_TRYWRLOCK, PJT_RWLOCK_UNLOCK, PJT_RWLOCK_WRLOCK, PJT_SELF, PJT_SETCANCELSTATE, PJT_SETCANCELTYPE, PJT_SETSPECIFIC, PJT_SIGMASK, PJT_TESTCANCEL, PJT_CLEANUP_POP_IMP, PJT_CLEANUP_PUSH_IMP, PJT_CANCEL_ENTER, PJT_CANCEL_LEAVE, PJT_MUTEX_CONSISTENT, PJT_MUTEXATTR_GETROBUST, PJT_MUTEXATTR_SETROBUST, PJT_GETTHREADID_NP, PJT_ATTR_GET_NP, PJT_MAX } pjt_index_t; typedef int (*pthread_func_t)(void); typedef pthread_func_t pthread_func_entry_t[2]; extern pthread_func_entry_t __thr_jtable[]; 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); interpos_func_t *__libc_interposing_slot(int interposno); extern interpos_func_t __libc_interposing[] __hidden; enum { INTERPOS_accept, INTERPOS_accept4, INTERPOS_aio_suspend, INTERPOS_close, INTERPOS_connect, INTERPOS_fcntl, INTERPOS_fsync, INTERPOS_fork, INTERPOS_msync, INTERPOS_nanosleep, INTERPOS_openat, INTERPOS_poll, INTERPOS_pselect, INTERPOS_recvfrom, INTERPOS_recvmsg, INTERPOS_select, INTERPOS_sendmsg, INTERPOS_sendto, INTERPOS_setcontext, INTERPOS_sigaction, INTERPOS_sigprocmask, INTERPOS_sigsuspend, INTERPOS_sigwait, INTERPOS_sigtimedwait, INTERPOS_sigwaitinfo, INTERPOS_swapcontext, INTERPOS_system, INTERPOS_tcdrain, INTERPOS_read, INTERPOS_readv, INTERPOS_wait4, INTERPOS_write, INTERPOS_writev, INTERPOS__pthread_mutex_init_calloc_cb, INTERPOS_spinlock, INTERPOS_spinunlock, INTERPOS_kevent, INTERPOS_wait6, INTERPOS_ppoll, INTERPOS_map_stacks_exec, INTERPOS_fdatasync, INTERPOS_clock_nanosleep, INTERPOS_distribute_static_tls, INTERPOS_pdfork, INTERPOS_MAX }; /* * yplib internal interfaces */ #ifdef YP 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 */ void _init_tls(void); /* * Provides pthread_once()-like functionality for both single-threaded * and multi-threaded applications. */ int _once(pthread_once_t *, void (*)(void)); /* * This is a pointer in the C run-time startup code. It is used * by getprogname() and setprogname(). */ extern const char *__progname; /* * This function is used by the threading libraries to notify malloc that a * thread is exiting. */ void _malloc_thread_cleanup(void); /* * This function is used by the threading libraries to notify libc that a * thread is exiting, so its thread-local dtors should be called. */ void __cxa_thread_call_dtors(void); int __cxa_thread_atexit_hidden(void (*dtor_func)(void *), void *obj, void *dso_symbol) __hidden; /* * These functions are used by the threading libraries in order to protect * malloc across fork(). */ void _malloc_prefork(void); void _malloc_postfork(void); void _malloc_first_thread(void); /* * Function to clean up streams, called from abort() and exit(). */ extern void (*__cleanup)(void) __hidden; /* * Get kern.osreldate to detect ABI revisions. Explicitly * ignores value of $OSVERSION and caches result. */ int __getosreldate(void); #include #include struct aiocb; struct fd_set; struct iovec; struct kevent; struct msghdr; struct pollfd; struct rusage; struct sigaction; struct sockaddr; struct stat; struct statfs; struct timespec; struct timeval; struct timezone; 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_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 __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; /* execve() with PATH processing to implement posix_spawnp() */ int _execvpe(const char *, char * const *, char * const *); 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); struct _pthread_cleanup_info; void ___pthread_cleanup_push_imp(void (*)(void *), void *, struct _pthread_cleanup_info *); void ___pthread_cleanup_pop_imp(int); void __throw_constraint_handler_s(const char * restrict msg, int error); struct __nl_cat_d; struct _xlocale; struct __nl_cat_d *__catopen_l(const char *name, int type, struct _xlocale *locale); #endif /* _LIBC_PRIVATE_H_ */