commit dbfeb07a17209f79f76fc3caa50d0a2410bcab27 (tree)
parent 66bcba6b9d796046199df647a24d10f60516b73f
Author: Alex Rønne Petersen <alex@alexrp.com>
Date: Tue, 26 May 2026 13:44:56 +0200
libc: update NetBSD crt0 to 11.0
Diffstat:
4 files changed, 102 insertions(+), 7 deletions(-)
diff --git a/lib/libc/netbsd/lib/csu/arch/riscv/crt0.S b/lib/libc/netbsd/lib/csu/arch/riscv/crt0.S
@@ -0,0 +1,62 @@
+/* $NetBSD: crt0.S,v 1.3.4.1 2026/03/04 19:27:22 martin Exp $ */
+
+/*-
+ * Copyright (c) 2014 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Matt Thomas of 3am Software Foundry.
+ *
+ * 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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>
+
+RCSID("$NetBSD: crt0.S,v 1.3.4.1 2026/03/04 19:27:22 martin Exp $")
+
+STRONG_ALIAS(_start,__start)
+
+_ENTRY(__start)
+ /*
+ * void ___start(void (*cleanup)(void), struct ps_strings *ps_strings);
+ */
+ .option push
+ .option norelax
+ lla gp, __global_pointer$
+ .option pop
+
+ j ___start
+END(__start)
+
+ .option push
+ .option norelax
+
+_ENTRY(_start_setgp)
+ lla gp, __global_pointer$
+ .option pop
+ ret
+END(_start_setgp)
+
+ .section .preinit_array, "aw"
+ .p2align PTR_SCALESHIFT
+ PTR_WORD _start_setgp
+
diff --git a/lib/libc/netbsd/lib/csu/common/crt0-common.c b/lib/libc/netbsd/lib/csu/common/crt0-common.c
@@ -1,4 +1,4 @@
-/* $NetBSD: crt0-common.c,v 1.27 2022/06/21 06:52:17 skrll Exp $ */
+/* $NetBSD: crt0-common.c,v 1.30 2025/05/02 23:04:06 riastradh Exp $ */
/*
* Copyright (c) 1998 Christos Zoulas
@@ -36,13 +36,16 @@
*/
#include <sys/cdefs.h>
-__RCSID("$NetBSD: crt0-common.c,v 1.27 2022/06/21 06:52:17 skrll Exp $");
+__RCSID("$NetBSD: crt0-common.c,v 1.30 2025/05/02 23:04:06 riastradh Exp $");
#include <sys/types.h>
#include <sys/exec.h>
#include <sys/exec_elf.h>
#include <sys/syscall.h>
+
#include <machine/profile.h>
+
+#include <limits.h>
#include <stdlib.h>
#include <unistd.h>
@@ -55,7 +58,6 @@ typedef void (*fptr_t)(void);
extern void _init(void);
extern void _fini(void);
#endif
-extern void _libc_init(void);
/*
* Arrange for _DYNAMIC to be weak and undefined (and therefore to show up
@@ -254,11 +256,19 @@ relocate_self(struct ps_strings *ps_strings)
Elf_Dyn *dynp = (Elf_Dyn *)((uint8_t *)dynphdr->p_vaddr + relocbase);
+ const Elf_Relr *relr = 0, *relrlim;
const REL_TYPE *relocs = 0, *relocslim;
- Elf_Addr relocssz = 0;
+ Elf_Addr relrsz = 0, relocssz = 0;
for (; dynp->d_tag != DT_NULL; dynp++) {
switch (dynp->d_tag) {
+ case DT_RELR:
+ relr =
+ (const Elf_Relr *)(relocbase + dynp->d_un.d_ptr);
+ break;
+ case DT_RELRSZ:
+ relrsz = dynp->d_un.d_val;
+ break;
case REL_TAG:
relocs =
(const REL_TYPE *)(relocbase + dynp->d_un.d_ptr);
@@ -268,7 +278,22 @@ relocate_self(struct ps_strings *ps_strings)
break;
}
}
+ relrlim = (const Elf_Relr *)((const uint8_t *)relr + relrsz);
relocslim = (const REL_TYPE *)((const uint8_t *)relocs + relocssz);
+ while (relr < relrlim) {
+ Elf_Addr *where;
+
+ where = (Elf_Addr *)(relocbase + *relr);
+ *where++ += relocbase;
+ while (++relr < relrlim && *relr & 1) {
+ unsigned i;
+
+ for (i = 1; i < CHAR_BIT*sizeof(*relr); i++, where++) {
+ if (*relr & ((Elf_Relr)1 << i))
+ *where += relocbase;
+ }
+ }
+ }
for (; relocs < relocslim; ++relocs) {
Elf_Addr *where;
@@ -318,9 +343,6 @@ ___start(void (*cleanup)(void), /* from shared loader */
__progname = empty_string;
}
- if (cleanup != NULL)
- atexit(cleanup);
-
_libc_init();
if (&rtld_DYNAMIC == NULL) {
@@ -334,6 +356,9 @@ ___start(void (*cleanup)(void), /* from shared loader */
_preinit();
+ if (cleanup != NULL)
+ atexit(cleanup);
+
#ifdef MCRT0
atexit(_mcleanup);
monstartup((u_long)&__eprol, (u_long)&__etext);
diff --git a/lib/libc/netbsd/lib/csu/common/csu-common.h b/lib/libc/netbsd/lib/csu/common/csu-common.h
@@ -36,3 +36,5 @@
extern char *__progname __common;
extern char **environ __common;
extern struct ps_strings *__ps_strings __common;
+
+void _libc_init(void) __attribute__((__constructor__, __used__));
diff --git a/src/libs/netbsd.zig b/src/libs/netbsd.zig
@@ -159,6 +159,12 @@ pub fn buildCrtFile(comp: *Compilation, crt_file: CrtFile, prog_node: std.Progre
},
.{
+ .path = "arch" ++ path.sep_str ++ "riscv" ++ path.sep_str ++ "crt0.S",
+ .flags = acflags.items,
+ .condition = target.cpu.arch.isRISCV(),
+ },
+
+ .{
.path = "arch" ++ path.sep_str ++ "sparc" ++ path.sep_str ++ "crt0.S",
.flags = acflags.items,
.condition = target.cpu.arch == .sparc,