aboutsummaryrefslogtreecommitdiff
path: root/lib/csu
diff options
context:
space:
mode:
Diffstat (limited to 'lib/csu')
-rw-r--r--lib/csu/Makefile12
-rw-r--r--lib/csu/Makefile.inc83
-rw-r--r--lib/csu/aarch64/Makefile8
-rw-r--r--lib/csu/aarch64/Makefile.depend11
-rw-r--r--lib/csu/aarch64/crt.h1
-rw-r--r--lib/csu/aarch64/crt1_c.c33
-rw-r--r--lib/csu/aarch64/crt1_s.S68
-rw-r--r--lib/csu/aarch64/crti.S51
-rw-r--r--lib/csu/aarch64/crtn.S46
-rw-r--r--lib/csu/amd64/Makefile9
-rw-r--r--lib/csu/amd64/Makefile.depend11
-rw-r--r--lib/csu/amd64/crt.h30
-rw-r--r--lib/csu/amd64/crt1_c.c30
-rw-r--r--lib/csu/amd64/crt1_s.S86
-rw-r--r--lib/csu/amd64/crti.S43
-rw-r--r--lib/csu/amd64/crtn.S35
-rw-r--r--lib/csu/arm/Makefile5
-rw-r--r--lib/csu/arm/Makefile.depend11
-rw-r--r--lib/csu/arm/crt.h1
-rw-r--r--lib/csu/arm/crt1_c.c79
-rw-r--r--lib/csu/arm/crt1_s.S77
-rw-r--r--lib/csu/arm/crti.S20
-rw-r--r--lib/csu/arm/crtn.S10
-rw-r--r--lib/csu/common/crtbegin.c100
-rw-r--r--lib/csu/common/crtbrand.S54
-rw-r--r--lib/csu/common/crtend.c70
-rw-r--r--lib/csu/common/csu_common.h50
-rw-r--r--lib/csu/common/feature_note.S47
-rw-r--r--lib/csu/common/ignore_init_note.S48
-rw-r--r--lib/csu/common/notes.h32
-rw-r--r--lib/csu/i386/Makefile8
-rw-r--r--lib/csu/i386/Makefile.depend11
-rw-r--r--lib/csu/i386/crt.h30
-rw-r--r--lib/csu/i386/crt1_c.c30
-rw-r--r--lib/csu/i386/crt1_s.S91
-rw-r--r--lib/csu/i386/crti.S45
-rw-r--r--lib/csu/i386/crtn.S37
-rw-r--r--lib/csu/powerpc/Makefile5
-rw-r--r--lib/csu/powerpc/Makefile.depend11
-rw-r--r--lib/csu/powerpc/crt.h31
-rw-r--r--lib/csu/powerpc/crt1_c.c89
-rw-r--r--lib/csu/powerpc/crti.S49
-rw-r--r--lib/csu/powerpc/crtn.S44
-rw-r--r--lib/csu/powerpc/crtsavres.S189
-rw-r--r--lib/csu/powerpc64/Makefile13
-rw-r--r--lib/csu/powerpc64/Makefile.depend11
-rw-r--r--lib/csu/powerpc64/crt.h31
-rw-r--r--lib/csu/powerpc64/crt1_c.c83
-rw-r--r--lib/csu/powerpc64/crti.S84
-rw-r--r--lib/csu/powerpc64/crtn.S40
-rw-r--r--lib/csu/riscv/Makefile5
-rw-r--r--lib/csu/riscv/Makefile.depend11
-rw-r--r--lib/csu/riscv/crt.h8
-rw-r--r--lib/csu/riscv/crt1_c.c51
-rw-r--r--lib/csu/riscv/crt1_s.S51
-rw-r--r--lib/csu/riscv/crti.S57
-rw-r--r--lib/csu/riscv/crtn.S46
-rw-r--r--lib/csu/tests/Makefile12
-rw-r--r--lib/csu/tests/Makefile.inc1
-rw-r--r--lib/csu/tests/Makefile.tests11
-rw-r--r--lib/csu/tests/cxx_constructors.cc102
-rw-r--r--lib/csu/tests/dso/Makefile25
-rw-r--r--lib/csu/tests/dynamic/Makefile7
-rw-r--r--lib/csu/tests/dynamiclib/Makefile15
-rw-r--r--lib/csu/tests/dynamicpie/Makefile7
-rw-r--r--lib/csu/tests/errno/Makefile18
-rw-r--r--lib/csu/tests/errno/errno_test.c23
-rw-r--r--lib/csu/tests/fini_test.c168
-rw-r--r--lib/csu/tests/init_test.c140
-rw-r--r--lib/csu/tests/static/Makefile5
70 files changed, 2856 insertions, 0 deletions
diff --git a/lib/csu/Makefile b/lib/csu/Makefile
new file mode 100644
index 000000000000..1e7a17bd35e9
--- /dev/null
+++ b/lib/csu/Makefile
@@ -0,0 +1,12 @@
+.include <src.opts.mk>
+
+.if exists(${.CURDIR}/${MACHINE_ARCH:S/powerpc64le/powerpc64/})
+SUBDIR+= ${MACHINE_ARCH:S/powerpc64le/powerpc64/}
+.else
+SUBDIR+= ${MACHINE_CPUARCH}
+.endif
+
+HAS_TESTS=
+SUBDIR.${MK_TESTS}+= tests
+
+.include <bsd.subdir.mk>
diff --git a/lib/csu/Makefile.inc b/lib/csu/Makefile.inc
new file mode 100644
index 000000000000..c4a1f90b173f
--- /dev/null
+++ b/lib/csu/Makefile.inc
@@ -0,0 +1,83 @@
+PACKAGE?= clibs-dev
+
+NO_WMISSING_VARIABLE_DECLARATIONS=
+# Can't instrument these files since that breaks non-sanitized programs.
+MK_ASAN:= no
+MK_UBSAN:= no
+
+.include <src.opts.mk>
+MK_SSP= no
+
+.if !defined(BUILDING_TESTS)
+
+OBJS+= Scrt1.o crt1.o gcrt1.o
+OBJS+= crtbegin.o crtbeginS.o crtbeginT.o
+OBJS+= crtend.o crtendS.o
+OBJS+= crti.o crtn.o
+
+CRT1OBJS+= crtbrand.o feature_note.o ignore_init_note.o
+
+ACFLAGS+= -DLOCORE
+
+CFLAGS+= -DSTRIP_FBSDID
+CFLAGS+= -fno-omit-frame-pointer
+CFLAGS+= -I${.CURDIR:H}/common \
+ -I${SRCTOP}/lib/libc/include \
+ -I${SRCTOP}/lib/libsys \
+
+CFLAGS_CRTS= -DSHARED ${PICFLAG}
+
+FILES= ${OBJS}
+FILESMODE= ${LIBMODE}
+FILESOWN= ${LIBOWN}
+FILESGRP= ${LIBGRP}
+FILESDIR= ${LIBDIR}
+# These FILES qualify as libraries for the purpose of LIBRARIES_ONLY.
+.undef LIBRARIES_ONLY
+
+CRT1SRC?= crt1_c.c
+CRT1OBJ?=
+
+CLEANFILES+= ${OBJS} ${CRT1OBJS} crt1_c.o gcrt1_c.o Scrt1_c.o
+CLEANFILES+= crti_s.o ${CRT1SRC:C/.[S|c]$/.o/}
+
+crt1.o: ${CRT1SRC:C/.[S|c]$/.o/} ${CRT1OBJS} ${CRT1OBJ}
+ ${LD} ${_LDFLAGS} -o ${.TARGET} -r ${.ALLSRC:M*.o}
+
+gcrt1_c.o: ${CRT1SRC}
+ ${CC} ${CFLAGS} -DGCRT -c -o ${.TARGET} ${.CURDIR}/${CRT1SRC}
+
+gcrt1.o: gcrt1_c.o ${CRT1OBJS} ${CRT1OBJ}
+ ${LD} ${_LDFLAGS} -o ${.TARGET} -r ${.ALLSRC:M*.o}
+
+Scrt1_c.o: ${CRT1SRC}
+ ${CC} ${CFLAGS} -fPIC -DPIC -c -o ${.TARGET} ${.CURDIR}/${CRT1SRC}
+
+Scrt1.o: Scrt1_c.o ${CRT1OBJS} ${CRT1OBJ}
+ ${LD} ${_LDFLAGS} -o ${.TARGET} -r ${.ALLSRC:M*.o}
+
+# __FreeBSD_version is recorded in crt1.o et al via crtbrand.
+crtbrand.o: ${SRCTOP}/sys/sys/param.h
+
+crtbegin.o: crtbegin.c
+crtbeginS.o: crtbegin.c
+crtbeginT.o: crtbegin.c
+crtend.o: crtend.c
+crtendS.o: crtend.c
+
+crtbegin.o crtend.o crtbeginT.o:
+ ${CC} ${CFLAGS} -I${.CURDIR} -c -o ${.TARGET} ${.ALLSRC:N*.h:[1]}
+
+crtbeginS.o crtendS.o:
+ ${CC} ${CFLAGS} -I${.CURDIR} ${CFLAGS_CRTS} -c -o ${.TARGET} \
+ ${.ALLSRC:N*.h:[1]}
+
+crti_s.o: crti.S
+ ${CC} ${CFLAGS} ${ACFLAGS} -c ${.ALLSRC:M*.S} -o ${.TARGET}
+
+crti.o: crti_s.o crtbrand.o
+ ${LD} ${_LDFLAGS} -o ${.TARGET} -r ${.ALLSRC:M*.o}
+
+.endif
+
+.include "../Makefile.inc"
diff --git a/lib/csu/aarch64/Makefile b/lib/csu/aarch64/Makefile
new file mode 100644
index 000000000000..24abd0b47585
--- /dev/null
+++ b/lib/csu/aarch64/Makefile
@@ -0,0 +1,8 @@
+.PATH: ${.CURDIR:H}/common
+
+CFLAGS+= -I${.CURDIR}
+
+CRT1SRC= crt1_s.S
+CRT1OBJ= crt1_c.o
+
+.include <bsd.lib.mk>
diff --git a/lib/csu/aarch64/Makefile.depend b/lib/csu/aarch64/Makefile.depend
new file mode 100644
index 000000000000..993ab0638f4a
--- /dev/null
+++ b/lib/csu/aarch64/Makefile.depend
@@ -0,0 +1,11 @@
+# Autogenerated - do NOT edit!
+
+DIRDEPS = \
+ include \
+
+
+.include <dirdeps.mk>
+
+.if ${DEP_RELDIR} == ${_DEP_RELDIR}
+# local dependencies - needed for -jN in clean tree
+.endif
diff --git a/lib/csu/aarch64/crt.h b/lib/csu/aarch64/crt.h
new file mode 100644
index 000000000000..d3dcf25e9039
--- /dev/null
+++ b/lib/csu/aarch64/crt.h
@@ -0,0 +1 @@
+/* Empty so we can include this unconditionally */
diff --git a/lib/csu/aarch64/crt1_c.c b/lib/csu/aarch64/crt1_c.c
new file mode 100644
index 000000000000..22158aaa42e2
--- /dev/null
+++ b/lib/csu/aarch64/crt1_c.c
@@ -0,0 +1,33 @@
+/* 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 <sys/cdefs.h>
+#include "csu_common.h"
diff --git a/lib/csu/aarch64/crt1_s.S b/lib/csu/aarch64/crt1_s.S
new file mode 100644
index 000000000000..1fb60f756a7a
--- /dev/null
+++ b/lib/csu/aarch64/crt1_s.S
@@ -0,0 +1,68 @@
+/* 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 <machine/asm.h>
+#include <sys/elf_common.h>
+
+ /*
+ * The program entry point
+ * void _start(char **ap, void (*cleanup)(void)) __dead2
+ */
+ENTRY(_start)
+ .cfi_undefined x30
+ mov x3, x2 /* cleanup */
+ add x1, x0, #8 /* load argv */
+ ldr x0, [x0] /* load argc */
+ add x2, x1, x0, lsl #3 /* env is after argv */
+ add x2, x2, #8 /* argv is null terminated */
+#ifdef PIC
+ adrp x4, :got:main
+ ldr x4, [x4, :got_lo12:main]
+#else
+ ldr x4, =main
+#endif
+#ifdef GCRT
+ ldr x5, =eprol
+ ldr x6, =etext
+ /*
+ * __libc_start1_gcrt(argc, argv, env, cleanup, main, &eprol, &etext)
+ */
+ bl __libc_start1_gcrt
+eprol:
+#else
+ /* __libc_start1(argc, argv, env, cleanup, main) */
+ bl __libc_start1
+#endif
+END(_start)
+
+ .section .note.GNU-stack,"",@progbits
+
+GNU_PROPERTY_AARCH64_FEATURE_1_NOTE(GNU_PROPERTY_AARCH64_FEATURE_1_VAL)
diff --git a/lib/csu/aarch64/crti.S b/lib/csu/aarch64/crti.S
new file mode 100644
index 000000000000..8bd2dfac1494
--- /dev/null
+++ b/lib/csu/aarch64/crti.S
@@ -0,0 +1,51 @@
+/*-
+ * Copyright 2001 David E. O'Brien
+ * Copyright 2014 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 <machine/asm.h>
+#include <sys/elf_common.h>
+
+ .section .init,"ax",@progbits
+ .align 4
+ .globl _init
+ .type _init,@function
+_init:
+ PAC_LR_SIGN
+ stp x29, x30, [sp, #-16]!
+ mov x29, sp
+
+ .section .fini,"ax",@progbits
+ .align 4
+ .globl _fini
+ .type _fini,@function
+_fini:
+ PAC_LR_SIGN
+ stp x29, x30, [sp, #-16]!
+ mov x29, sp
+
+GNU_PROPERTY_AARCH64_FEATURE_1_NOTE(GNU_PROPERTY_AARCH64_FEATURE_1_VAL)
diff --git a/lib/csu/aarch64/crtn.S b/lib/csu/aarch64/crtn.S
new file mode 100644
index 000000000000..441411f0ab83
--- /dev/null
+++ b/lib/csu/aarch64/crtn.S
@@ -0,0 +1,46 @@
+/*-
+ * Copyright 2001 David E. O'Brien
+ * Copyright 2014 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 <machine/asm.h>
+#include <sys/elf_common.h>
+
+ .section .init,"ax",@progbits
+ ldp x29, x30, [sp], #16
+ PAC_LR_AUTH
+ ret
+
+
+ .section .fini,"ax",@progbits
+ ldp x29, x30, [sp], #16
+ PAC_LR_AUTH
+ ret
+
+ .section .note.GNU-stack,"",%progbits
+
+GNU_PROPERTY_AARCH64_FEATURE_1_NOTE(GNU_PROPERTY_AARCH64_FEATURE_1_VAL)
diff --git a/lib/csu/amd64/Makefile b/lib/csu/amd64/Makefile
new file mode 100644
index 000000000000..80ae5484d662
--- /dev/null
+++ b/lib/csu/amd64/Makefile
@@ -0,0 +1,9 @@
+.PATH: ${.CURDIR:H}/common
+
+CFLAGS+= -I${.CURDIR}
+CFLAGS+= -fno-omit-frame-pointer
+
+CRT1SRC= crt1_s.S
+CRT1OBJ= crt1_c.o
+
+.include <bsd.lib.mk>
diff --git a/lib/csu/amd64/Makefile.depend b/lib/csu/amd64/Makefile.depend
new file mode 100644
index 000000000000..993ab0638f4a
--- /dev/null
+++ b/lib/csu/amd64/Makefile.depend
@@ -0,0 +1,11 @@
+# Autogenerated - do NOT edit!
+
+DIRDEPS = \
+ include \
+
+
+.include <dirdeps.mk>
+
+.if ${DEP_RELDIR} == ${_DEP_RELDIR}
+# local dependencies - needed for -jN in clean tree
+.endif
diff --git a/lib/csu/amd64/crt.h b/lib/csu/amd64/crt.h
new file mode 100644
index 000000000000..7ce1eb826905
--- /dev/null
+++ b/lib/csu/amd64/crt.h
@@ -0,0 +1,30 @@
+/*-
+ * 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.
+ */
+
+#ifndef _CRT_H_
+#define _CRT_H_
+
+#define HAVE_CTORS
+#define INIT_CALL_SEQ(func) "call " __STRING(func)
+
+#endif
diff --git a/lib/csu/amd64/crt1_c.c b/lib/csu/amd64/crt1_c.c
new file mode 100644
index 000000000000..85fe0309600c
--- /dev/null
+++ b/lib/csu/amd64/crt1_c.c
@@ -0,0 +1,30 @@
+/* LINTLIBRARY */
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * 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 <sys/cdefs.h>
+#include "csu_common.h"
diff --git a/lib/csu/amd64/crt1_s.S b/lib/csu/amd64/crt1_s.S
new file mode 100644
index 000000000000..3ceea9289330
--- /dev/null
+++ b/lib/csu/amd64/crt1_s.S
@@ -0,0 +1,86 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (C) 2023 Dmitry Chagin <dchagin@FreeBSD.org>
+ *
+ * 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 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 <machine/asm.h>
+
+
+ .text
+ .align 8
+
+ /*
+ * The program entry point
+ * %rdi %rsi
+ * void _start(char **ap, void (*cleanup)(void)) __dead2
+ */
+ .globl _start
+ .type _start, @function
+_start:
+ .cfi_startproc
+ .cfi_undefined %rip /* Terminate call chain. */
+ pushq %rbp /* Align stack, terminate call chain. */
+ .cfi_def_cfa_offset 8
+ movq %rsp, %rbp
+ .cfi_offset %rbp, -16
+ .cfi_def_cfa_register %rbp
+#ifdef GCRT
+ subq $16, %rsp
+#endif
+ 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
+ * large memory model due to the offset from %rip being limited
+ * to 32 bits.
+ */
+ leaq main@plt(%rip), %r8
+#else
+ movabsq $main, %r8
+#endif
+#ifdef GCRT
+ movabsq $eprol, %r9
+ movabsq $etext, %rax
+ movq %rax, (%rsp)
+ /*
+ * %edi %rsi %rdx %rcx %r8 %r9 (%rsp)
+ * __libc_start1_gcrt(argc, argv, env, cleanup, main, &eprol, &etext)
+ */
+ callq __libc_start1_gcrt
+eprol:
+#else
+ /* __libc_start1(argc, argv, env, cleanup, main) */
+ callq __libc_start1
+#endif
+ int3
+ .cfi_endproc
+ .size _start, . - _start
+
+ .section .note.GNU-stack,"",%progbits
diff --git a/lib/csu/amd64/crti.S b/lib/csu/amd64/crti.S
new file mode 100644
index 000000000000..9fc507e5fff5
--- /dev/null
+++ b/lib/csu/amd64/crti.S
@@ -0,0 +1,43 @@
+/*-
+ * Copyright 1996, 1997, 1998, 2000 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 <machine/asm.h>
+ .section .init,"ax",@progbits
+ .align 4
+ .globl _init
+ .type _init,@function
+_init:
+ pushq %rbp
+ movq %rsp,%rbp
+
+ .section .fini,"ax",@progbits
+ .align 4
+ .globl _fini
+ .type _fini,@function
+_fini:
+ pushq %rbp
+ movq %rsp,%rbp
+
+ .section .note.GNU-stack,"",%progbits
diff --git a/lib/csu/amd64/crtn.S b/lib/csu/amd64/crtn.S
new file mode 100644
index 000000000000..d6336688d4ce
--- /dev/null
+++ b/lib/csu/amd64/crtn.S
@@ -0,0 +1,35 @@
+/*-
+ * Copyright 1996, 1997, 1998, 2000 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 <machine/asm.h>
+ .section .init,"ax",@progbits
+ popq %rbp
+ ret
+
+ .section .fini,"ax",@progbits
+ popq %rbp
+ ret
+
+ .section .note.GNU-stack,"",%progbits
diff --git a/lib/csu/arm/Makefile b/lib/csu/arm/Makefile
new file mode 100644
index 000000000000..5f5de9f9c61a
--- /dev/null
+++ b/lib/csu/arm/Makefile
@@ -0,0 +1,5 @@
+.PATH: ${.CURDIR:H}/common
+
+CRT1OBJS+= crt1_s.o
+
+.include <bsd.lib.mk>
diff --git a/lib/csu/arm/Makefile.depend b/lib/csu/arm/Makefile.depend
new file mode 100644
index 000000000000..993ab0638f4a
--- /dev/null
+++ b/lib/csu/arm/Makefile.depend
@@ -0,0 +1,11 @@
+# Autogenerated - do NOT edit!
+
+DIRDEPS = \
+ include \
+
+
+.include <dirdeps.mk>
+
+.if ${DEP_RELDIR} == ${_DEP_RELDIR}
+# local dependencies - needed for -jN in clean tree
+.endif
diff --git a/lib/csu/arm/crt.h b/lib/csu/arm/crt.h
new file mode 100644
index 000000000000..d3dcf25e9039
--- /dev/null
+++ b/lib/csu/arm/crt.h
@@ -0,0 +1 @@
+/* Empty so we can include this unconditionally */
diff --git a/lib/csu/arm/crt1_c.c b/lib/csu/arm/crt1_c.c
new file mode 100644
index 000000000000..bb40f262f5c2
--- /dev/null
+++ b/lib/csu/arm/crt1_c.c
@@ -0,0 +1,79 @@
+/* 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 <sys/param.h>
+#include <sys/elf_common.h>
+
+#include "libc_private.h"
+#include "csu_common.h"
+
+struct Struct_Obj_Entry;
+struct ps_strings;
+
+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)) __dead2;
+
+void
+__start(int argc, char **argv, char **env, struct ps_strings *ps_strings,
+ const struct Struct_Obj_Entry *obj __unused, void (*cleanup)(void))
+{
+ if (ps_strings != (struct ps_strings *)0)
+ __ps_strings = ps_strings;
+
+#ifdef GCRT
+ __libc_start1_gcrt(argc, argv, env, cleanup, main, &eprol, &etext);
+#else
+ __libc_start1(argc, argv, env, cleanup, main);
+#endif
+}
+
+#ifdef GCRT
+__asm__(".text");
+__asm__("eprol:");
+__asm__(".previous");
+#endif
diff --git a/lib/csu/arm/crt1_s.S b/lib/csu/arm/crt1_s.S
new file mode 100644
index 000000000000..1f864f217f0b
--- /dev/null
+++ b/lib/csu/arm/crt1_s.S
@@ -0,0 +1,77 @@
+/* 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 <machine/asm.h>
+#include <sys/param.h>
+#include <sys/elf_common.h>
+#include "notes.h"
+
+ENTRY(_start)
+ mov r5, r2 /* cleanup */
+ mov r4, r1 /* obj_main */
+ mov r3, r0 /* ps_strings */
+ /* Get argc, argv, and envp from stack */
+ ldr r0, [sp, #0x0000]
+ add r1, sp, #0x0004
+ add r2, r1, r0, lsl #2
+ add r2, r2, #0x0004
+ /* Ensure the stack is properly aligned before calling C code. */
+ bic sp, sp, #7
+ sub sp, sp, #8
+ str r5, [sp, #4]
+ str r4, [sp, #0]
+
+ b __start
+END(_start)
+
+ .section .note.tag,"a",%note
+ .p2align 2
+ .4byte 2f-1f
+ .4byte 4f-3f
+ .4byte NT_FREEBSD_ARCH_TAG
+1: .asciz NOTE_FREEBSD_VENDOR
+2: .p2align 2
+3: .asciz MACHINE_ARCH
+4:
+
+ .section .note.GNU-stack,"",%progbits
diff --git a/lib/csu/arm/crti.S b/lib/csu/arm/crti.S
new file mode 100644
index 000000000000..ce2058d3fa65
--- /dev/null
+++ b/lib/csu/arm/crti.S
@@ -0,0 +1,20 @@
+#include <machine/asm.h>
+ .section .init,"ax",%progbits
+ .align 4
+ .globl _init
+ .type _init,%function
+_init:
+ mov ip, sp
+ stmdb sp!, {fp, ip, lr, pc}
+ sub fp, ip, #4
+
+ .section .fini,"ax",%progbits
+ .align 4
+ .globl _fini
+ .type _fini,%function
+_fini:
+ mov ip, sp
+ stmdb sp!, {fp, ip, lr, pc}
+ sub fp, ip, #4
+
+ .section .note.GNU-stack,"",%progbits
diff --git a/lib/csu/arm/crtn.S b/lib/csu/arm/crtn.S
new file mode 100644
index 000000000000..9be6d6cef76c
--- /dev/null
+++ b/lib/csu/arm/crtn.S
@@ -0,0 +1,10 @@
+#include <machine/asm.h>
+ .section .init,"ax",%progbits
+ ldmea fp, {fp, sp, pc}
+ mov pc, lr
+
+ .section .fini,"ax",%progbits
+ ldmea fp, {fp, sp, pc}
+ mov pc, lr
+
+ .section .note.GNU-stack,"",%progbits
diff --git a/lib/csu/common/crtbegin.c b/lib/csu/common/crtbegin.c
new file mode 100644
index 000000000000..8ef9b8923c21
--- /dev/null
+++ b/lib/csu/common/crtbegin.c
@@ -0,0 +1,100 @@
+/*-
+ * 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 <sys/param.h>
+
+#include "crt.h"
+
+typedef void (*crt_func)(void);
+
+extern void *__dso_handle __hidden;
+
+#ifndef SHARED
+void *__dso_handle = 0;
+#else
+void *__dso_handle = &__dso_handle;
+void __cxa_finalize(void *) __weak_symbol;
+
+/*
+ * Call __cxa_finalize with the dso handle in shared objects.
+ * When we have ctors/dtors call from the dtor handler before calling
+ * any dtors, otherwise use a destructor.
+ */
+#ifndef HAVE_CTORS
+__attribute__((destructor))
+#endif
+static void
+run_cxa_finalize(void)
+{
+
+ if (__cxa_finalize != NULL)
+ __cxa_finalize(__dso_handle);
+}
+#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
+};
+
+extern const char startof_dtors[] __asm(".startof..dtors")
+ __weak_symbol __hidden;
+extern const char sizeof_dtors[] __asm(".sizeof..dtors")
+ __weak_symbol __hidden;
+
+static void
+__do_global_dtors_aux(void)
+{
+ crt_func fn;
+ uintptr_t dtors_end;
+ int n;
+
+#ifdef SHARED
+ run_cxa_finalize();
+#endif
+
+ dtors_end = (uintptr_t)&startof_dtors + (uintptr_t)&sizeof_dtors;
+ for (n = 1;; n++) {
+ fn = __DTOR_LIST__[n];
+ if (fn == (crt_func)0 || fn == (crt_func)-1 || (dtors_end > 0 &&
+ (uintptr_t)&__DTOR_LIST__[n] >= dtors_end))
+ break;
+ fn();
+ }
+}
+
+asm (
+ ".pushsection .fini \n"
+ "\t" INIT_CALL_SEQ(__do_global_dtors_aux) "\n"
+ ".popsection \n"
+);
+#endif
diff --git a/lib/csu/common/crtbrand.S b/lib/csu/common/crtbrand.S
new file mode 100644
index 000000000000..a06f72daa99d
--- /dev/null
+++ b/lib/csu/common/crtbrand.S
@@ -0,0 +1,54 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright 2000 David E. O'Brien, 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 <machine/asm.h>
+#include <sys/param.h>
+#include <sys/elf_common.h>
+#include "notes.h"
+
+/*
+ * Special ".note.tag" entry specifying the ABI version. See
+ * http://www.netbsd.org/Documentation/kernel/elf-notes.html
+ * for more information.
+ */
+
+ .section .note.tag,"aGR",%note,.freebsd.noteG,comdat
+ .p2align 2
+ .4byte 2f-1f
+ .4byte 4f-3f
+ .4byte NT_FREEBSD_ABI_TAG
+1: .asciz NOTE_FREEBSD_VENDOR
+2: .p2align 2
+3: .4byte __FreeBSD_version
+4:
+
+ .section .note.GNU-stack,"",%progbits
+
+#ifdef __aarch64__
+/* This is needed in all objects for BTI to be used in the linked elf file */
+GNU_PROPERTY_AARCH64_FEATURE_1_NOTE(GNU_PROPERTY_AARCH64_FEATURE_1_VAL)
+#endif
diff --git a/lib/csu/common/crtend.c b/lib/csu/common/crtend.c
new file mode 100644
index 000000000000..9eb75de2ef60
--- /dev/null
+++ b/lib/csu/common/crtend.c
@@ -0,0 +1,70 @@
+/*-
+ * 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 <sys/types.h>
+#include "crt.h"
+
+typedef void (*crt_func)(void);
+
+#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
+};
+
+extern const char startof_ctors[] __asm(".startof..ctors")
+ __weak_symbol __hidden;
+
+static void
+__do_global_ctors_aux(void)
+{
+ crt_func fn;
+ uintptr_t ctors_start;
+ int n;
+
+ ctors_start = (uintptr_t)&startof_ctors;
+ for (n = 1;; n++) {
+ fn = __CTOR_END__[-n];
+ if (fn == (crt_func)0 || fn == (crt_func)-1 ||
+ (ctors_start > 0 &&
+ (uintptr_t)&__CTOR_END__[-n] < ctors_start))
+ break;
+ fn();
+ }
+}
+
+asm (
+ ".pushsection .init \n"
+ "\t" INIT_CALL_SEQ(__do_global_ctors_aux) "\n"
+ ".popsection \n"
+);
+#endif
diff --git a/lib/csu/common/csu_common.h b/lib/csu/common/csu_common.h
new file mode 100644
index 000000000000..c7d4a640e2b0
--- /dev/null
+++ b/lib/csu/common/csu_common.h
@@ -0,0 +1,50 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * 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/common/feature_note.S b/lib/csu/common/feature_note.S
new file mode 100644
index 000000000000..fb9f6141699e
--- /dev/null
+++ b/lib/csu/common/feature_note.S
@@ -0,0 +1,47 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright 2000 David E. O'Brien, 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 <machine/asm.h>
+#include <sys/elf_common.h>
+#include "notes.h"
+
+ .section .note.tag,"aR",%note
+ .p2align 2
+ .4byte 2f-1f
+ .4byte 4f-3f
+ .4byte NT_FREEBSD_FEATURE_CTL
+1: .asciz NOTE_FREEBSD_VENDOR
+2: .p2align 2
+3: .4byte 0
+4:
+
+ .section .note.GNU-stack,"",%progbits
+
+#ifdef __aarch64__
+/* This is needed in all objects for BTI to be used in the linked elf file */
+GNU_PROPERTY_AARCH64_FEATURE_1_NOTE(GNU_PROPERTY_AARCH64_FEATURE_1_VAL)
+#endif
diff --git a/lib/csu/common/ignore_init_note.S b/lib/csu/common/ignore_init_note.S
new file mode 100644
index 000000000000..d78be61f17a9
--- /dev/null
+++ b/lib/csu/common/ignore_init_note.S
@@ -0,0 +1,48 @@
+/*-
+ * SPDX-License-Identifier: BSD-1-Clause
+ *
+ * Copyright 2012 Konstantin Belousov <kib@FreeBSD.org>
+ * Copyright (c) 2018 The FreeBSD Foundation
+ *
+ * Parts of this software was developed by Konstantin Belousov
+ * <kib@FreeBSD.org> 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 <machine/asm.h>
+#include <sys/elf_common.h>
+
+#include "notes.h"
+
+ .section .note.tag,"aR",%note
+ .p2align 2
+ .4byte 2f-1f
+ .4byte 4f-3f
+ .4byte NT_FREEBSD_NOINIT_TAG
+1: .asciz NOTE_FREEBSD_VENDOR
+2: .p2align 2
+3: .4byte 0
+4:
+
+ .section .note.GNU-stack,"",%progbits
+
+#ifdef __aarch64__
+/* This is needed in all objects for BTI to be used in the linked elf file */
+GNU_PROPERTY_AARCH64_FEATURE_1_NOTE(GNU_PROPERTY_AARCH64_FEATURE_1_VAL)
+#endif
diff --git a/lib/csu/common/notes.h b/lib/csu/common/notes.h
new file mode 100644
index 000000000000..fc4e4d082c21
--- /dev/null
+++ b/lib/csu/common/notes.h
@@ -0,0 +1,32 @@
+/*-
+ * SPDX-License-Identifier: BSD-1-Clause
+ *
+ * Copyright 2012 Konstantin Belousov <kib@FreeBSD.org>
+ * 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.
+ *
+ * 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.
+ */
+
+#ifndef CSU_COMMON_NOTES_H
+#define CSU_COMMON_NOTES_H
+
+#define NOTE_FREEBSD_VENDOR "FreeBSD"
+
+#define NOTE_SECTION ".note.tag"
+
+#endif
diff --git a/lib/csu/i386/Makefile b/lib/csu/i386/Makefile
new file mode 100644
index 000000000000..24abd0b47585
--- /dev/null
+++ b/lib/csu/i386/Makefile
@@ -0,0 +1,8 @@
+.PATH: ${.CURDIR:H}/common
+
+CFLAGS+= -I${.CURDIR}
+
+CRT1SRC= crt1_s.S
+CRT1OBJ= crt1_c.o
+
+.include <bsd.lib.mk>
diff --git a/lib/csu/i386/Makefile.depend b/lib/csu/i386/Makefile.depend
new file mode 100644
index 000000000000..993ab0638f4a
--- /dev/null
+++ b/lib/csu/i386/Makefile.depend
@@ -0,0 +1,11 @@
+# Autogenerated - do NOT edit!
+
+DIRDEPS = \
+ include \
+
+
+.include <dirdeps.mk>
+
+.if ${DEP_RELDIR} == ${_DEP_RELDIR}
+# local dependencies - needed for -jN in clean tree
+.endif
diff --git a/lib/csu/i386/crt.h b/lib/csu/i386/crt.h
new file mode 100644
index 000000000000..7ce1eb826905
--- /dev/null
+++ b/lib/csu/i386/crt.h
@@ -0,0 +1,30 @@
+/*-
+ * 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.
+ */
+
+#ifndef _CRT_H_
+#define _CRT_H_
+
+#define HAVE_CTORS
+#define INIT_CALL_SEQ(func) "call " __STRING(func)
+
+#endif
diff --git a/lib/csu/i386/crt1_c.c b/lib/csu/i386/crt1_c.c
new file mode 100644
index 000000000000..5755a66afbb2
--- /dev/null
+++ b/lib/csu/i386/crt1_c.c
@@ -0,0 +1,30 @@
+/* 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.
+ */
+
+#include <sys/cdefs.h>
+#include "csu_common.h"
+
+void _start(char *, ...) __dead2;
diff --git a/lib/csu/i386/crt1_s.S b/lib/csu/i386/crt1_s.S
new file mode 100644
index 000000000000..c29e97320ac8
--- /dev/null
+++ b/lib/csu/i386/crt1_s.S
@@ -0,0 +1,91 @@
+/*-
+ * Copyright 2009 Konstantin Belousov.
+ * 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.
+ *
+ * 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 <machine/asm.h>
+ .text
+ .align 4
+ .globl _start
+ .type _start, @function
+_start:
+ .cfi_startproc
+ .cfi_undefined %eip
+ popl %esi # Pop argc
+ .cfi_def_cfa_offset -4
+ movl %esp,%edi # argv starts at stack top
+ xorl %ebp,%ebp
+ pushl %ebp
+ .cfi_def_cfa_offset 0
+ movl %esp,%ebp
+ .cfi_offset %ebp,-4
+ .cfi_def_cfa_register %ebp
+ andl $0xfffffff0,%esp # align stack
+
+#ifdef GCRT
+ subl $4,%esp # Align stack for 7 arguments
+ pushl $etext
+ pushl $eprol
+eprol:
+#else
+ subl $12,%esp # Align stack for 5 arguments
+#endif /* GCRT */
+
+#ifdef PIC
+ calll 1f
+1: popl %ebx
+ addl $_GLOBAL_OFFSET_TABLE_+[.-1b],%ebx
+ leal main@GOTOFF(%ebx),%eax
+ pushl %eax
+#else
+ pushl $main
+#endif /* PIC */
+
+ pushl %edx # rtld cleanup
+ /* env = argv + argc + 1 */
+ movl %edi,%eax # env = argv
+ movl %esi,%ecx
+ shll $2,%ecx # argc * 4
+ addl %ecx,%eax # env += argc
+ addl $4,%eax # env += 1
+ pushl %eax # env
+ pushl %edi # argv
+ pushl %esi # argc
+
+#ifdef GCRT
+ /*
+ * __libc_start1_gcrt(argc, argv, env, cleanup, main, &eprol, &etext);
+ */
+ calll __libc_start1_gcrt
+#else
+ /*
+ * __libc_start1(argc, argv, env, cleanup, main);
+ */
+#ifdef PIC
+ calll __libc_start1@PLT
+#else
+ calll __libc_start1
+#endif
+#endif /* GCRT */
+ int3
+ .cfi_endproc
+ .size _start, . - _start
+
+ .section .note.GNU-stack,"",%progbits
diff --git a/lib/csu/i386/crti.S b/lib/csu/i386/crti.S
new file mode 100644
index 000000000000..c006ed02ee64
--- /dev/null
+++ b/lib/csu/i386/crti.S
@@ -0,0 +1,45 @@
+/*-
+ * Copyright 1996, 1997, 1998, 2000 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 <machine/asm.h>
+ .section .init,"ax",@progbits
+ .align 4
+ .globl _init
+ .type _init,@function
+_init:
+ pushl %ebp
+ movl %esp,%ebp
+ subl $8,%esp /* re-align stack pointer */
+
+ .section .fini,"ax",@progbits
+ .align 4
+ .globl _fini
+ .type _fini,@function
+_fini:
+ pushl %ebp
+ movl %esp,%ebp
+ subl $8,%esp /* re-align stack pointer */
+
+ .section .note.GNU-stack,"",%progbits
diff --git a/lib/csu/i386/crtn.S b/lib/csu/i386/crtn.S
new file mode 100644
index 000000000000..3e720a2137ff
--- /dev/null
+++ b/lib/csu/i386/crtn.S
@@ -0,0 +1,37 @@
+/*-
+ * Copyright 1996, 1997, 1998, 2000 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 <machine/asm.h>
+ .section .init,"ax",@progbits
+ addl $8,%esp
+ popl %ebp
+ ret
+
+ .section .fini,"ax",@progbits
+ addl $8,%esp
+ popl %ebp
+ ret
+
+ .section .note.GNU-stack,"",%progbits
diff --git a/lib/csu/powerpc/Makefile b/lib/csu/powerpc/Makefile
new file mode 100644
index 000000000000..b6928446d005
--- /dev/null
+++ b/lib/csu/powerpc/Makefile
@@ -0,0 +1,5 @@
+.PATH: ${.CURDIR:H}/common
+
+OBJS+= crtsavres.o
+
+.include <bsd.lib.mk>
diff --git a/lib/csu/powerpc/Makefile.depend b/lib/csu/powerpc/Makefile.depend
new file mode 100644
index 000000000000..993ab0638f4a
--- /dev/null
+++ b/lib/csu/powerpc/Makefile.depend
@@ -0,0 +1,11 @@
+# Autogenerated - do NOT edit!
+
+DIRDEPS = \
+ include \
+
+
+.include <dirdeps.mk>
+
+.if ${DEP_RELDIR} == ${_DEP_RELDIR}
+# local dependencies - needed for -jN in clean tree
+.endif
diff --git a/lib/csu/powerpc/crt.h b/lib/csu/powerpc/crt.h
new file mode 100644
index 000000000000..fd589b9adf37
--- /dev/null
+++ b/lib/csu/powerpc/crt.h
@@ -0,0 +1,31 @@
+/*-
+ * 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.
+ */
+
+#ifndef _CRT_H_
+#define _CRT_H_
+
+#define HAVE_CTORS
+#define CTORS_CONSTRUCTORS
+#define INIT_CALL_SEQ(func) "bl " __STRING(func) "; nop"
+
+#endif
diff --git a/lib/csu/powerpc/crt1_c.c b/lib/csu/powerpc/crt1_c.c
new file mode 100644
index 000000000000..656ca9cde9ea
--- /dev/null
+++ b/lib/csu/powerpc/crt1_c.c
@@ -0,0 +1,89 @@
+/* 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 <sys/cdefs.h>
+#include <stdlib.h>
+
+#include "libc_private.h"
+#include "csu_common.h"
+
+struct Struct_Obj_Entry;
+struct ps_strings;
+
+struct ps_strings *__ps_strings;
+
+void _start(int, char **, char **, const struct Struct_Obj_Entry *,
+ 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)
+{
+ if (ps_strings != (struct ps_strings *)0)
+ __ps_strings = ps_strings;
+
+#ifdef GCRT
+ __libc_start1_gcrt(argc, argv, env, cleanup, main, &eprol, &etext);
+#else
+ __libc_start1(argc, argv, env, cleanup, main);
+#endif
+}
+
+#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/powerpc/crti.S b/lib/csu/powerpc/crti.S
new file mode 100644
index 000000000000..25d0df585bb9
--- /dev/null
+++ b/lib/csu/powerpc/crti.S
@@ -0,0 +1,49 @@
+/*-
+ * Copyright 2001 David E. O'Brien
+ * 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 <machine/asm.h>
+ .section .init,"ax",@progbits
+ .align 2
+ .globl _init
+ .type _init,@function
+_init:
+ stwu 1,-16(1)
+ mflr 0
+ stw 31,12(1)
+ stw 0,20(1)
+ mr 31,1
+
+
+ .section .fini,"ax",@progbits
+ .align 2
+ .globl _fini
+_fini:
+ stwu 1,-16(1)
+ mflr 0
+ stw 31,12(1)
+ stw 0,20(1)
+ mr 31,1
+
+ .section .note.GNU-stack,"",%progbits
diff --git a/lib/csu/powerpc/crtn.S b/lib/csu/powerpc/crtn.S
new file mode 100644
index 000000000000..026cf4fe3e55
--- /dev/null
+++ b/lib/csu/powerpc/crtn.S
@@ -0,0 +1,44 @@
+/*-
+ * Copyright 2001 David E. O'Brien
+ * 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 <machine/asm.h>
+ .section .init,"ax",@progbits
+ lwz 11,0(1)
+ lwz 0,4(11)
+ mtlr 0
+ lwz 31,-4(11)
+ mr 1,11
+ blr
+
+
+ .section .fini,"ax",@progbits
+ lwz 11,0(1)
+ lwz 0,4(11)
+ mtlr 0
+ lwz 31,-4(11)
+ mr 1,11
+ blr
+
+ .section .note.GNU-stack,"",%progbits
diff --git a/lib/csu/powerpc/crtsavres.S b/lib/csu/powerpc/crtsavres.S
new file mode 100644
index 000000000000..95ac7171213f
--- /dev/null
+++ b/lib/csu/powerpc/crtsavres.S
@@ -0,0 +1,189 @@
+/*-
+ * SPDX-License-Identifier: BSD-1-Clause
+ *
+ * Copyright 2019 Justin Hibbits
+ *
+ * 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 <machine/asm.h>
+.text
+
+/*
+ * The PowerPC ABI spec requires the following save/restore functions to be
+ * provided:
+ *
+ * _savefpr_N
+ * _restfpr_N
+ * _restfpr_N_x
+ * _savegpr_N
+ * _restgpr_N
+ * _restgpr_N_x
+ *
+ * With N ranging from 14 to 31, to save the nonvolatile registers.
+ */
+
+#define _CRTENTRY(name) \
+ .text; \
+ .globl name; \
+ .type name,@function; \
+ name:
+
+#define SAVEFPR(r) _CRTENTRY(__CONCAT(_savefpr_,r)) \
+ stfd r,(-256 + r * 8)(11)
+
+SAVEFPR(14)
+SAVEFPR(15)
+SAVEFPR(16)
+SAVEFPR(17)
+SAVEFPR(18)
+SAVEFPR(19)
+SAVEFPR(20)
+SAVEFPR(21)
+SAVEFPR(22)
+SAVEFPR(23)
+SAVEFPR(24)
+SAVEFPR(25)
+SAVEFPR(26)
+SAVEFPR(27)
+SAVEFPR(28)
+SAVEFPR(29)
+SAVEFPR(30)
+SAVEFPR(31)
+ blr
+
+#define RESTFPR(r) _CRTENTRY(__CONCAT(_restfpr_,r)) \
+ lfd r,(-256 + r * 8)(11)
+
+RESTFPR(14)
+RESTFPR(15)
+RESTFPR(16)
+RESTFPR(17)
+RESTFPR(18)
+RESTFPR(19)
+RESTFPR(20)
+RESTFPR(21)
+RESTFPR(22)
+RESTFPR(23)
+RESTFPR(24)
+RESTFPR(25)
+RESTFPR(26)
+RESTFPR(27)
+RESTFPR(28)
+RESTFPR(29)
+RESTFPR(30)
+RESTFPR(31)
+ blr
+
+#define SAVEGPR(r) _CRTENTRY(__CONCAT(_savegpr_,r)) \
+ stw r,(-128 + r * 4)(11)
+
+SAVEGPR(14)
+SAVEGPR(15)
+SAVEGPR(16)
+SAVEGPR(17)
+SAVEGPR(18)
+SAVEGPR(19)
+SAVEGPR(20)
+SAVEGPR(21)
+SAVEGPR(22)
+SAVEGPR(23)
+SAVEGPR(24)
+SAVEGPR(25)
+SAVEGPR(26)
+SAVEGPR(27)
+SAVEGPR(28)
+SAVEGPR(29)
+SAVEGPR(30)
+SAVEGPR(31)
+ blr
+
+#define RESTGPR(r) _CRTENTRY(__CONCAT(_restgpr_,r)) \
+ lwz r,(-128 + r*4)(11)
+
+RESTGPR(14)
+RESTGPR(15)
+RESTGPR(16)
+RESTGPR(17)
+RESTGPR(18)
+RESTGPR(19)
+RESTGPR(20)
+RESTGPR(21)
+RESTGPR(22)
+RESTGPR(23)
+RESTGPR(24)
+RESTGPR(25)
+RESTGPR(26)
+RESTGPR(27)
+RESTGPR(28)
+RESTGPR(29)
+RESTGPR(30)
+RESTGPR(31)
+ blr
+
+#define RESTFPR_X(r) _CRTENTRY(__CONCAT(__CONCAT(_restfpr_,r),_x)) \
+ lfd r,(-256 + r * 8)(11)
+
+RESTFPR_X(14)
+RESTFPR_X(15)
+RESTFPR_X(16)
+RESTFPR_X(17)
+RESTFPR_X(18)
+RESTFPR_X(19)
+RESTFPR_X(20)
+RESTFPR_X(21)
+RESTFPR_X(22)
+RESTFPR_X(23)
+RESTFPR_X(24)
+RESTFPR_X(25)
+RESTFPR_X(26)
+RESTFPR_X(27)
+RESTFPR_X(28)
+RESTFPR_X(29)
+RESTFPR_X(30)
+RESTFPR_X(31)
+ lwz 0,4(11)
+ mtlr 0
+ mr 1,11
+ blr
+
+#define RESTGPR_X(r) _CRTENTRY(__CONCAT(__CONCAT(_restgpr_,r),_x)) \
+ lwz r,(-128 + r * 4)(11)
+
+RESTGPR_X(14)
+RESTGPR_X(15)
+RESTGPR_X(16)
+RESTGPR_X(17)
+RESTGPR_X(18)
+RESTGPR_X(19)
+RESTGPR_X(20)
+RESTGPR_X(21)
+RESTGPR_X(22)
+RESTGPR_X(23)
+RESTGPR_X(24)
+RESTGPR_X(25)
+RESTGPR_X(26)
+RESTGPR_X(27)
+RESTGPR_X(28)
+RESTGPR_X(29)
+RESTGPR_X(30)
+RESTGPR_X(31)
+ lwz 0,4(11)
+ mtlr 0
+ mr 1,11
+ blr
diff --git a/lib/csu/powerpc64/Makefile b/lib/csu/powerpc64/Makefile
new file mode 100644
index 000000000000..97df3f389115
--- /dev/null
+++ b/lib/csu/powerpc64/Makefile
@@ -0,0 +1,13 @@
+.PATH: ${.CURDIR:H}/common
+
+OBJS+= crtsavres.o
+CFLAGS+= -I${.CURDIR} \
+ -mlongcall
+
+CLEANFILES+= crtsavres.S
+
+# On powerpc64 crtsavres is an empty file
+crtsavres.S:
+ touch ${.TARGET}
+
+.include <bsd.lib.mk>
diff --git a/lib/csu/powerpc64/Makefile.depend b/lib/csu/powerpc64/Makefile.depend
new file mode 100644
index 000000000000..993ab0638f4a
--- /dev/null
+++ b/lib/csu/powerpc64/Makefile.depend
@@ -0,0 +1,11 @@
+# Autogenerated - do NOT edit!
+
+DIRDEPS = \
+ include \
+
+
+.include <dirdeps.mk>
+
+.if ${DEP_RELDIR} == ${_DEP_RELDIR}
+# local dependencies - needed for -jN in clean tree
+.endif
diff --git a/lib/csu/powerpc64/crt.h b/lib/csu/powerpc64/crt.h
new file mode 100644
index 000000000000..fd589b9adf37
--- /dev/null
+++ b/lib/csu/powerpc64/crt.h
@@ -0,0 +1,31 @@
+/*-
+ * 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.
+ */
+
+#ifndef _CRT_H_
+#define _CRT_H_
+
+#define HAVE_CTORS
+#define CTORS_CONSTRUCTORS
+#define INIT_CALL_SEQ(func) "bl " __STRING(func) "; nop"
+
+#endif
diff --git a/lib/csu/powerpc64/crt1_c.c b/lib/csu/powerpc64/crt1_c.c
new file mode 100644
index 000000000000..8d389b87a2a5
--- /dev/null
+++ b/lib/csu/powerpc64/crt1_c.c
@@ -0,0 +1,83 @@
+/* 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 <sys/cdefs.h>
+#include <stdint.h>
+#include <sys/elf.h>
+
+#include "libc_private.h"
+#include "csu_common.h"
+
+struct Struct_Obj_Entry;
+struct ps_strings;
+
+void _start(int, char **, char **, const struct Struct_Obj_Entry *,
+ void (*)(void), struct ps_strings *) __dead2;
+
+struct ps_strings *__ps_strings;
+
+/* The entry function. */
+/*
+ * First 5 arguments are specified by the PowerPC SVR4 ABI.
+ * The last argument, ps_strings, is a BSD extension.
+ */
+void
+_start(int argc, char **argv, char **env,
+ const struct Struct_Obj_Entry *obj __unused, void (*cleanup)(void),
+ struct ps_strings *ps_strings)
+{
+ if (ps_strings != (struct ps_strings *)0)
+ __ps_strings = ps_strings;
+
+#ifdef GCRT
+ __libc_start1_gcrt(argc, argv, env, cleanup, main, &eprol, &etext);
+#else
+ __libc_start1(argc, argv, env, cleanup, main);
+#endif
+}
+
+#ifdef GCRT
+__asm__(".text");
+__asm__("eprol:");
+__asm__(".previous");
+#endif
diff --git a/lib/csu/powerpc64/crti.S b/lib/csu/powerpc64/crti.S
new file mode 100644
index 000000000000..33e9ca4f42ae
--- /dev/null
+++ b/lib/csu/powerpc64/crti.S
@@ -0,0 +1,84 @@
+/*-
+ * Copyright 2001 David E. O'Brien
+ * 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 <machine/asm.h>
+#ifdef _CALL_ELF
+.abiversion _CALL_ELF
+#endif
+
+ .section .init,"ax",@progbits
+ .p2align 2
+ .globl _init
+#if !defined(_CALL_ELF) || _CALL_ELF == 1
+ .section ".opd","aw"
+ .p2align 3
+_init:
+ .quad .L._init,.TOC.@tocbase,0
+ .previous
+ .type _init,@function
+
+ .p2align 4
+.L._init:
+#else
+ .p2align 4
+ .globl _init
+ .type _init,@function
+_init:
+ addis %r2, %r12, (.TOC.-_init)@ha
+ addi %r2, %r2, (.TOC.-_init)@l
+ .localentry _init, .-_init
+#endif
+ stdu 1,-48(1)
+ mflr 0
+ std 0,64(1)
+
+/* Fini */
+ .section .fini,"ax",@progbits
+ .p2align 2
+ .globl _fini
+#if !defined(_CALL_ELF) || _CALL_ELF == 1
+ .section ".opd","aw"
+ .p2align 3
+_fini:
+ .quad .L._fini,.TOC.@tocbase,0
+ .previous
+ .type _fini,@function
+
+ .p2align 4
+.L._fini:
+#else
+ .p2align 4
+ .globl _fini
+ .type _fini,@function
+_fini:
+ addis %r2, %r12, (.TOC.-_fini)@ha
+ addi %r2, %r2, (.TOC.-_fini)@l
+ .localentry _fini, .-_fini
+#endif
+ stdu 1,-48(1)
+ mflr 0
+ std 0,64(1)
+
+ .section .note.GNU-stack,"",%progbits
diff --git a/lib/csu/powerpc64/crtn.S b/lib/csu/powerpc64/crtn.S
new file mode 100644
index 000000000000..a7619f3fbf62
--- /dev/null
+++ b/lib/csu/powerpc64/crtn.S
@@ -0,0 +1,40 @@
+/*-
+ * Copyright 2001 David E. O'Brien
+ * 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 <machine/asm.h>
+ .section .init,"ax",@progbits
+ ld %r1,0(%r1)
+ ld 0,16(%r1)
+ mtlr 0
+ blr
+
+
+ .section .fini,"ax",@progbits
+ ld %r1,0(%r1)
+ ld 0,16(%r1)
+ mtlr 0
+ blr
+
+ .section .note.GNU-stack,"",%progbits
diff --git a/lib/csu/riscv/Makefile b/lib/csu/riscv/Makefile
new file mode 100644
index 000000000000..5f5de9f9c61a
--- /dev/null
+++ b/lib/csu/riscv/Makefile
@@ -0,0 +1,5 @@
+.PATH: ${.CURDIR:H}/common
+
+CRT1OBJS+= crt1_s.o
+
+.include <bsd.lib.mk>
diff --git a/lib/csu/riscv/Makefile.depend b/lib/csu/riscv/Makefile.depend
new file mode 100644
index 000000000000..993ab0638f4a
--- /dev/null
+++ b/lib/csu/riscv/Makefile.depend
@@ -0,0 +1,11 @@
+# Autogenerated - do NOT edit!
+
+DIRDEPS = \
+ include \
+
+
+.include <dirdeps.mk>
+
+.if ${DEP_RELDIR} == ${_DEP_RELDIR}
+# local dependencies - needed for -jN in clean tree
+.endif
diff --git a/lib/csu/riscv/crt.h b/lib/csu/riscv/crt.h
new file mode 100644
index 000000000000..e6b6b97f2525
--- /dev/null
+++ b/lib/csu/riscv/crt.h
@@ -0,0 +1,8 @@
+
+#ifndef _CRT_H_
+#define _CRT_H_
+
+#define HAVE_CTORS
+#define INIT_CALL_SEQ(func) "call " __STRING(func)
+
+#endif
diff --git a/lib/csu/riscv/crt1_c.c b/lib/csu/riscv/crt1_c.c
new file mode 100644
index 000000000000..df97fb50bff1
--- /dev/null
+++ b/lib/csu/riscv/crt1_c.c
@@ -0,0 +1,51 @@
+/* LINTLIBRARY */
+/*-
+ * Copyright 1996-1998 John D. Polstra.
+ * Copyright (c) 2015-2017 Ruslan Bukin <br@bsdpad.com>
+ * 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 <sys/cdefs.h>
+#include "libc_private.h"
+#include "csu_common.h"
+
+void __start(int argc, char **argv, char **env, void (*cleanup)(void)) __dead2;
+
+void
+__start(int argc, char **argv, char **env, void (*cleanup)(void))
+{
+#ifdef GCRT
+ __libc_start1_gcrt(argc, argv, env, cleanup, main, &eprol, &etext);
+__asm__("eprol:");
+#else
+ __libc_start1(argc, argv, env, cleanup, main);
+#endif
+}
diff --git a/lib/csu/riscv/crt1_s.S b/lib/csu/riscv/crt1_s.S
new file mode 100644
index 000000000000..8f77d2358ae7
--- /dev/null
+++ b/lib/csu/riscv/crt1_s.S
@@ -0,0 +1,51 @@
+/* LINTLIBRARY */
+/*-
+ * Copyright 1996-1998 John D. Polstra.
+ * Copyright (c) 2015-2017 Ruslan Bukin <br@bsdpad.com>
+ * 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 <machine/asm.h>
+ENTRY(_start)
+ mv a3, a2 # cleanup
+ addi a1, a0, 8 # get argv
+ ld a0, 0(a0) # load argc
+ slli t0, a0, 3 # mult by arg size
+ add a2, a1, t0 # env is after argv
+ addi a2, a2, 8 # argv is null terminated
+ .option push
+ .option norelax
+ lla gp, __global_pointer$
+ .option pop
+ call __start
+END(_start)
+
+ .section .note.GNU-stack,"",%progbits
diff --git a/lib/csu/riscv/crti.S b/lib/csu/riscv/crti.S
new file mode 100644
index 000000000000..92f6814a9c42
--- /dev/null
+++ b/lib/csu/riscv/crti.S
@@ -0,0 +1,57 @@
+/*-
+ * Copyright (c) 2015-2017 Ruslan Bukin <br@bsdpad.com>
+ * 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 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 <machine/asm.h>
+# this puts __global_pointer$ into .dynsym, so symlook_obj can now find that
+# (see reloc.c)
+ .weak __global_pointer$
+__global_pointer$:
+
+ .section .init,"ax",@progbits
+ .align 0
+ .globl _init
+ .type _init,@function
+_init:
+ addi sp, sp, -16
+ sd ra, 0(sp)
+
+ .section .fini,"ax",@progbits
+ .align 0
+ .globl _fini
+ .type _fini,@function
+_fini:
+ addi sp, sp, -16
+ sd ra, 0(sp)
+
+ .section .note.GNU-stack,"",%progbits
diff --git a/lib/csu/riscv/crtn.S b/lib/csu/riscv/crtn.S
new file mode 100644
index 000000000000..50f6e3f9ece5
--- /dev/null
+++ b/lib/csu/riscv/crtn.S
@@ -0,0 +1,46 @@
+/*-
+ * Copyright (c) 2015 Ruslan Bukin <br@bsdpad.com>
+ * 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 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 <machine/asm.h>
+ .section .init,"ax",@progbits
+ ld ra, 0(sp)
+ addi sp, sp, 16
+ ret
+
+ .section .fini,"ax",@progbits
+ ld ra, 0(sp)
+ addi sp, sp, 16
+ ret
+
+ .section .note.GNU-stack,"",%progbits
diff --git a/lib/csu/tests/Makefile b/lib/csu/tests/Makefile
new file mode 100644
index 000000000000..b76ef590c88f
--- /dev/null
+++ b/lib/csu/tests/Makefile
@@ -0,0 +1,12 @@
+PACKAGE= tests
+
+SUBDIR= dso
+TESTS_SUBDIRS= dynamic
+TESTS_SUBDIRS+= dynamiclib
+TESTS_SUBDIRS+= dynamicpie
+TESTS_SUBDIRS+= errno
+TESTS_SUBDIRS+= static
+
+SUBDIR_DEPEND_dynamiclib=dso
+
+.include <bsd.test.mk>
diff --git a/lib/csu/tests/Makefile.inc b/lib/csu/tests/Makefile.inc
new file mode 100644
index 000000000000..2cb85b8d5d43
--- /dev/null
+++ b/lib/csu/tests/Makefile.inc
@@ -0,0 +1 @@
+TESTSDIR:= ${TESTSBASE}/${RELDIR:C/csu\/tests/csu/}
diff --git a/lib/csu/tests/Makefile.tests b/lib/csu/tests/Makefile.tests
new file mode 100644
index 000000000000..52179057b2a3
--- /dev/null
+++ b/lib/csu/tests/Makefile.tests
@@ -0,0 +1,11 @@
+ATF_TESTS_C+= init_test
+ATF_TESTS_C+= fini_test
+ATF_TESTS_CXX+= cxx_constructors
+
+WARNS?= 3
+
+.if exists(${.CURDIR:H:H}/${MACHINE_ARCH})
+CFLAGS+= -I${.CURDIR:H:H}/${MACHINE_ARCH}
+.else
+CFLAGS+= -I${.CURDIR:H:H}/${MACHINE_CPUARCH}
+.endif
diff --git a/lib/csu/tests/cxx_constructors.cc b/lib/csu/tests/cxx_constructors.cc
new file mode 100644
index 000000000000..3c04053884d2
--- /dev/null
+++ b/lib/csu/tests/cxx_constructors.cc
@@ -0,0 +1,102 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2018 Andrew Turner
+ *
+ * This software was 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.
+ *
+ * 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 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 <sys/types.h>
+#include <sys/wait.h>
+
+#include <errno.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#ifndef DSO_LIB
+#include <atf-c++.hpp>
+#endif
+
+extern volatile int constructor_run;
+extern bool run_destructor_test;
+
+#ifndef DSO_BASE
+volatile int constructor_run;
+bool run_destructor_test = false;
+#endif
+
+struct Foo {
+ Foo() {
+ constructor_run = 1;
+ }
+ ~Foo() {
+ if (run_destructor_test)
+ _exit(1);
+ }
+};
+extern Foo foo;
+
+#ifndef DSO_BASE
+Foo foo;
+#endif
+
+#ifndef DSO_LIB
+ATF_TEST_CASE_WITHOUT_HEAD(cxx_constructor);
+ATF_TEST_CASE_BODY(cxx_constructor)
+{
+
+ ATF_REQUIRE(constructor_run == 1);
+}
+
+ATF_TEST_CASE_WITHOUT_HEAD(cxx_destructor);
+ATF_TEST_CASE_BODY(cxx_destructor)
+{
+ pid_t pid, wpid;
+ int status;
+
+ pid = fork();
+ switch(pid) {
+ case -1:
+ break;
+ case 0:
+ run_destructor_test = true;
+ exit(0);
+ default:
+ while ((wpid = waitpid(pid, &status, 0)) == -1 &&
+ errno == EINTR)
+ ;
+ ATF_REQUIRE(WEXITSTATUS(status) == 1);
+ break;
+ }
+}
+
+ATF_INIT_TEST_CASES(tcs)
+{
+
+ ATF_ADD_TEST_CASE(tcs, cxx_constructor);
+ ATF_ADD_TEST_CASE(tcs, cxx_destructor);
+}
+#endif
diff --git a/lib/csu/tests/dso/Makefile b/lib/csu/tests/dso/Makefile
new file mode 100644
index 000000000000..431168de0328
--- /dev/null
+++ b/lib/csu/tests/dso/Makefile
@@ -0,0 +1,25 @@
+.PATH: ${.CURDIR:H}
+
+PACKAGE= tests
+SHLIB= h_csu
+SHLIB_NAME= libh_csu.so
+SHLIB_MAJOR= 1
+
+WITHOUT_STATIC=
+WITHOUT_PROFILE=
+WITHOUT_PIC=
+
+CFLAGS+= -DDSO_LIB
+
+.include "../Makefile.tests"
+SRCS=
+.for src in ${ATF_TESTS_C}
+SRCS+= ${src}.c
+.endfor
+.for src in ${ATF_TESTS_CXX}
+SRCS+= ${src}.cc
+.endfor
+
+LIBDIR= ${TESTSBASE}/lib/csu/dynamiclib
+
+.include <bsd.lib.mk>
diff --git a/lib/csu/tests/dynamic/Makefile b/lib/csu/tests/dynamic/Makefile
new file mode 100644
index 000000000000..e9e57201cb9a
--- /dev/null
+++ b/lib/csu/tests/dynamic/Makefile
@@ -0,0 +1,7 @@
+.PATH: ${.CURDIR:H}
+
+.include <src.opts.mk>
+MK_PIE= no
+
+.include "../Makefile.tests"
+.include <bsd.test.mk>
diff --git a/lib/csu/tests/dynamiclib/Makefile b/lib/csu/tests/dynamiclib/Makefile
new file mode 100644
index 000000000000..13a9b837684e
--- /dev/null
+++ b/lib/csu/tests/dynamiclib/Makefile
@@ -0,0 +1,15 @@
+.PATH: ${.CURDIR:H}
+CFLAGS+= -DDSO_BASE
+DPADD+= ${.OBJDIR:H}/dso/libh_csu.so
+LDFLAGS+= -Wl,-rpath,${TESTSDIR} -L${.OBJDIR:H}/dso
+LDADD+= -lh_csu
+
+.include "../Makefile.tests"
+
+.for test in ${ATF_TESTS_C}
+ATF_TESTS_CXX+= ${test}
+SRCS.${test}= ${test}.c
+.endfor
+ATF_TESTS_C:=
+
+.include <bsd.test.mk>
diff --git a/lib/csu/tests/dynamicpie/Makefile b/lib/csu/tests/dynamicpie/Makefile
new file mode 100644
index 000000000000..204bef0c7d10
--- /dev/null
+++ b/lib/csu/tests/dynamicpie/Makefile
@@ -0,0 +1,7 @@
+.PATH: ${.CURDIR:H}
+
+.include <src.opts.mk>
+MK_PIE= yes
+
+.include "../Makefile.tests"
+.include <bsd.test.mk>
diff --git a/lib/csu/tests/errno/Makefile b/lib/csu/tests/errno/Makefile
new file mode 100644
index 000000000000..eae54a936294
--- /dev/null
+++ b/lib/csu/tests/errno/Makefile
@@ -0,0 +1,18 @@
+PLAIN_TESTS_C= errno_test \
+ errno_static_test \
+ errno_thr_test \
+ errno_thr_static_test
+
+SRCS.errno_static_test= errno_test.c
+LDFLAGS.errno_static_test= -static
+
+SRCS.errno_thr_test= errno_test.c
+LIBADD.errno_thr_test= pthread
+
+SRCS.errno_thr_static_test= errno_test.c
+LDFLAGS.errno_thr_static_test= -static
+LIBADD.errno_thr_static_test= pthread
+
+MK_PIE:= no
+
+.include <bsd.test.mk>
diff --git a/lib/csu/tests/errno/errno_test.c b/lib/csu/tests/errno/errno_test.c
new file mode 100644
index 000000000000..d190c7fd2959
--- /dev/null
+++ b/lib/csu/tests/errno/errno_test.c
@@ -0,0 +1,23 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2025 Mark Johnston <markj@FreeBSD.org>
+ */
+
+#include <errno.h>
+#include <stdlib.h>
+
+static void __attribute__((constructor))
+f(void)
+{
+ errno = 42;
+}
+
+int
+main(void)
+{
+ /* errno must be zero upon program startup. */
+ if (errno != 0)
+ exit(1);
+ exit(0);
+}
diff --git a/lib/csu/tests/fini_test.c b/lib/csu/tests/fini_test.c
new file mode 100644
index 000000000000..79dbceb8a37c
--- /dev/null
+++ b/lib/csu/tests/fini_test.c
@@ -0,0 +1,168 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2018 Andrew Turner
+ *
+ * This software was 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.
+ *
+ * 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 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 <sys/types.h>
+#include <sys/wait.h>
+
+#include <errno.h>
+#include <stdbool.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include <atf-c.h>
+
+#include <crt.h>
+
+extern bool run_dtors_test;
+extern bool run_fini_array_test;
+void dso_handle_check(void);
+
+
+#ifndef DSO_BASE
+typedef void (*func_ptr)(void);
+
+bool run_dtors_test = false;
+bool run_fini_array_test = false;
+
+static void
+dtors_handler(void)
+{
+
+ if (run_dtors_test)
+ _exit(1);
+}
+__section(".dtors") __used static func_ptr dtors_func =
+ &dtors_handler;
+#endif
+
+#ifndef DSO_LIB
+ATF_TC_WITHOUT_HEAD(dtors_test);
+ATF_TC_BODY(dtors_test, tc)
+{
+ pid_t pid, wpid;
+ int status;
+
+ pid = fork();
+ switch(pid) {
+ case -1:
+ break;
+ case 0:
+ run_dtors_test = true;
+ exit(0);
+ default:
+ while ((wpid = waitpid(pid, &status, 0)) == -1 &&
+ errno == EINTR)
+ ;
+#ifdef HAVE_CTORS
+ ATF_REQUIRE_MSG(WEXITSTATUS(status) == 1,
+ ".dtors failed to run");
+#else
+ ATF_REQUIRE_MSG(WEXITSTATUS(status) == 0,
+ ".dtors incorrectly ran");
+#endif
+ break;
+ }
+}
+#endif
+
+#ifndef DSO_BASE
+static void
+fini_array_handler(void)
+{
+
+ if (run_fini_array_test)
+ _exit(1);
+}
+__section(".fini_array") __used static func_ptr fini_array_func =
+ &fini_array_handler;
+#endif
+
+#ifndef DSO_LIB
+ATF_TC_WITHOUT_HEAD(fini_array_test);
+ATF_TC_BODY(fini_array_test, tc)
+{
+ pid_t pid, wpid;
+ int status;
+
+ pid = fork();
+ switch(pid) {
+ case -1:
+ break;
+ case 0:
+ run_fini_array_test = true;
+ exit(0);
+ default:
+ while ((wpid = waitpid(pid, &status, 0)) == -1 &&
+ errno == EINTR)
+ ;
+ ATF_REQUIRE_MSG(WEXITSTATUS(status) == 1,
+ ".fini_array failed to run");
+ break;
+ }
+}
+#endif
+
+#ifndef DSO_BASE
+extern void *__dso_handle;
+
+void
+dso_handle_check(void)
+{
+ void *dso = __dso_handle;
+
+#if defined(DSO_LIB) || defined(__PIE__)
+ ATF_REQUIRE_MSG(dso != NULL,
+ "Null __dso_handle in DSO/PIE");
+#else
+ ATF_REQUIRE_MSG(dso == NULL,
+ "Invalid __dso_handle in non-DSO");
+#endif
+}
+#endif
+
+#ifndef DSO_LIB
+ATF_TC_WITHOUT_HEAD(dso_handle_test);
+ATF_TC_BODY(dso_handle_test, tc)
+{
+
+ dso_handle_check();
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+
+ ATF_TP_ADD_TC(tp, dtors_test);
+ ATF_TP_ADD_TC(tp, fini_array_test);
+ ATF_TP_ADD_TC(tp, dso_handle_test);
+
+ return (atf_no_error());
+}
+#endif
diff --git a/lib/csu/tests/init_test.c b/lib/csu/tests/init_test.c
new file mode 100644
index 000000000000..2d4484735f76
--- /dev/null
+++ b/lib/csu/tests/init_test.c
@@ -0,0 +1,140 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2018 Andrew Turner
+ *
+ * This software was 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.
+ *
+ * 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 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 <sys/cdefs.h>
+#ifndef DSO_LIB
+#include <atf-c.h>
+#endif
+
+#include <crt.h>
+
+typedef void (*func_ptr)(void);
+
+extern volatile int ctors_run;
+extern volatile int preinit_array_run;
+extern volatile int preinit_array_state;
+extern volatile int init_array_run;
+extern volatile int init_array_state;
+
+#ifndef DSO_BASE
+volatile int ctors_run;
+volatile int preinit_array_run;
+volatile int preinit_array_state = -1;
+volatile int init_array_run;
+volatile int init_array_state = -1;
+#endif
+
+#ifndef DSO_BASE
+static void
+ctors_handler(void)
+{
+
+ ctors_run = 1;
+}
+__section(".ctors") __used static func_ptr ctors_func =
+ &ctors_handler;
+#endif
+
+#ifndef DSO_LIB
+ATF_TC_WITHOUT_HEAD(ctors_test);
+ATF_TC_BODY(ctors_test, tc)
+{
+
+#ifdef HAVE_CTORS
+ ATF_REQUIRE_MSG(ctors_run == 1, ".ctors not run");
+#else
+ ATF_REQUIRE_MSG(ctors_run == 0, ".ctors run");
+#endif
+}
+#endif
+
+#if !defined(DSO_BASE) && !defined(DSO_LIB)
+static void
+preinit_array_handler(void)
+{
+
+ preinit_array_run = 1;
+ preinit_array_state = init_array_run;
+}
+__section(".preinit_array") __used static func_ptr preinit_array_func =
+ &preinit_array_handler;
+#endif
+
+#ifndef DSO_LIB
+ATF_TC_WITHOUT_HEAD(preinit_array_test);
+ATF_TC_BODY(preinit_array_test, tc)
+{
+
+#ifdef DSO_BASE
+ /* Check .preinit_array wasn't run in a DSO */
+ ATF_REQUIRE_MSG(preinit_array_run == 0, ".preinit_array run in DSO");
+#else
+ ATF_REQUIRE_MSG(preinit_array_run == 1, ".preinit_array not run");
+ ATF_REQUIRE_MSG(preinit_array_state == 0,
+ ".preinit_array was not run before .init_array");
+#endif
+}
+#endif
+
+#ifndef DSO_BASE
+static void
+init_array_handler(void)
+{
+
+ init_array_run = 1;
+ init_array_state = preinit_array_run;
+}
+__section(".init_array") __used static func_ptr init_array_func =
+ &init_array_handler;
+#endif
+
+#ifndef DSO_LIB
+ATF_TC_WITHOUT_HEAD(init_array_test);
+ATF_TC_BODY(init_array_test, tc)
+{
+
+ ATF_REQUIRE_MSG(init_array_run == 1, ".init_array not run");
+#ifndef DSO_BASE
+ ATF_REQUIRE_MSG(init_array_state == 1,
+ ".init_array was not run after .preinit_array");
+#endif
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+
+ ATF_TP_ADD_TC(tp, ctors_test);
+ ATF_TP_ADD_TC(tp, preinit_array_test);
+ ATF_TP_ADD_TC(tp, init_array_test);
+
+ return (atf_no_error());
+}
+#endif
diff --git a/lib/csu/tests/static/Makefile b/lib/csu/tests/static/Makefile
new file mode 100644
index 000000000000..e76c49c93a1a
--- /dev/null
+++ b/lib/csu/tests/static/Makefile
@@ -0,0 +1,5 @@
+.PATH: ${.CURDIR:H}
+NO_SHARED=
+
+.include "../Makefile.tests"
+.include <bsd.test.mk>