Page MenuHomeFreeBSD

D59892.1794717755.diff
No OneTemporary

Authored By
Unknown
Size
5 KB
Referenced Files
None
Subscribers
None

D59892.1794717755.diff

diff --git a/lib/csu/Makefile.inc b/lib/csu/Makefile.inc
--- a/lib/csu/Makefile.inc
+++ b/lib/csu/Makefile.inc
@@ -13,7 +13,11 @@
OBJS+= Scrt1.o crt1.o gcrt1.o
OBJS+= crtbegin.o crtbeginS.o crtbeginT.o
OBJS+= crtend.o crtendS.o
+# crti.o/crtn.o provide the obsolete _init/_fini interface. New
+# architectures support .init_array/.fini_array only and do not build them.
+.if ${MACHINE_CPUARCH} != "loongarch"
OBJS+= crti.o crtn.o
+.endif
CRT1OBJS+= crtbrand.o feature_note.o ignore_init_note.o
@@ -72,11 +76,13 @@
${CC} ${CFLAGS} -I${.CURDIR} ${CFLAGS_CRTS} -c -o ${.TARGET} \
${.ALLSRC:N*.h:[1]}
+.if ${MACHINE_CPUARCH} != "loongarch"
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
.endif
diff --git a/lib/csu/loongarch/Makefile b/lib/csu/loongarch/Makefile
new file mode 100644
--- /dev/null
+++ b/lib/csu/loongarch/Makefile
@@ -0,0 +1,3 @@
+.PATH: ${.CURDIR:H}/common
+CRT1OBJS+= crt1_s.o
+.include <bsd.lib.mk>
diff --git a/lib/csu/loongarch/Makefile.depend b/lib/csu/loongarch/Makefile.depend
new file mode 100644
--- /dev/null
+++ b/lib/csu/loongarch/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/loongarch/crt.h b/lib/csu/loongarch/crt.h
new file mode 100644
--- /dev/null
+++ b/lib/csu/loongarch/crt.h
@@ -0,0 +1,10 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2026 Xiaoqiang Zhao <zhaoxiaoqiang007@gmail.com>
+ */
+
+#ifndef _CRT_H_
+#define _CRT_H_
+
+#endif
diff --git a/lib/csu/loongarch/crt1_c.c b/lib/csu/loongarch/crt1_c.c
new file mode 100644
--- /dev/null
+++ b/lib/csu/loongarch/crt1_c.c
@@ -0,0 +1,28 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2026 Xiaoqiang Zhao <zhaoxiaoqiang007@gmail.com>
+ */
+
+#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);
+#else
+ __libc_start1(argc, argv, env, cleanup, main);
+#endif
+}
+
+#ifdef GCRT
+__asm__(".text");
+__asm__("eprol:");
+__asm__(".previous");
+#endif
diff --git a/lib/csu/loongarch/crt1_s.S b/lib/csu/loongarch/crt1_s.S
new file mode 100644
--- /dev/null
+++ b/lib/csu/loongarch/crt1_s.S
@@ -0,0 +1,24 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2026 Xiaoqiang Zhao <zhaoxiaoqiang007@gmail.com>
+ */
+
+#include <machine/asm.h>
+ENTRY(_start)
+ .cfi_undefined 1 /* Do not attempt to unwind any further. */
+ move $a3, $a2 # cleanup
+ addi.d $a1, $a0, 8 # argv
+ ld.d $a0, $a0, 0 # argc
+ slli.d $t0, $a0, 3 # argv + argc * 8 => argv end
+ add.d $a2, $a1, $t0 # env is after argv
+ addi.d $a2, $a2, 8 # argv is null terminated
+
+ /* Adjust $sp for 16-aligned */
+ bstrins.d $sp, $zero, 3, 0
+ /* __start(argc, argv, env, cleanup) */
+ bl __start
+
+END(_start)
+ .section .note.GNU-stack,"",%progbits
+
diff --git a/lib/libc/csu/libc_start1.c b/lib/libc/csu/libc_start1.c
--- a/lib/libc/csu/libc_start1.c
+++ b/lib/libc/csu/libc_start1.c
@@ -38,8 +38,10 @@
extern void (*__init_array_end[])(int, char **, char **) __hidden;
extern void (*__fini_array_start[])(void) __hidden;
extern void (*__fini_array_end[])(void) __hidden;
+#ifndef CRT_NO_INIT
extern void _fini(void) __hidden;
extern void _init(void) __hidden;
+#endif
extern int _DYNAMIC;
#pragma weak _DYNAMIC
@@ -90,7 +92,9 @@
if ((uintptr_t)fn != 0 && (uintptr_t)fn != 1)
(fn)();
}
+#ifndef CRT_NO_INIT
_fini();
+#endif
}
#endif
@@ -112,7 +116,9 @@
if ((uintptr_t)fn != 0 && (uintptr_t)fn != 1)
fn(argc, argv, env);
}
+#ifndef CRT_NO_INIT
_init();
+#endif
array_size = __init_array_end - __init_array_start;
for (n = 0; n < array_size; n++) {
fn = __init_array_start[n];
diff --git a/lib/libc/csu/loongarch/Makefile.inc b/lib/libc/csu/loongarch/Makefile.inc
new file mode 100644
--- /dev/null
+++ b/lib/libc/csu/loongarch/Makefile.inc
@@ -0,0 +1,3 @@
+CFLAGS+= -DCRT_IRELOC_RELA
+# No crti.o/crtn.o: the obsolete _init/_fini functions do not exist.
+CFLAGS+= -DCRT_NO_INIT
diff --git a/lib/libc/csu/loongarch/reloc.c b/lib/libc/csu/loongarch/reloc.c
new file mode 100644
--- /dev/null
+++ b/lib/libc/csu/loongarch/reloc.c
@@ -0,0 +1,30 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2026 Xiaoqiang Zhao <zhaoxiaoqiang007@gmail.com>
+ */
+
+static void
+ifunc_init(const Elf_Auxinfo *aux __unused)
+{
+}
+
+static void
+crt1_handle_rela(const Elf_Rela *r)
+{
+ /*
+ * LoongArch ifunc resolvers take no arguments, matching the
+ * call_ifunc_resolver() convention in rtld's rtld_machdep.h.
+ */
+ typedef Elf_Addr (*ifunc_resolver_t)(void);
+ Elf_Addr *ptr, *where, target;
+
+ switch (ELF_R_TYPE(r->r_info)) {
+ case R_LARCH_IRELATIVE:
+ ptr = (Elf_Addr *)r->r_addend;
+ where = (Elf_Addr *)r->r_offset;
+ target = ((ifunc_resolver_t)ptr)();
+ *where = target;
+ break;
+ }
+}

File Metadata

Mime Type
text/plain
Expires
Sun, Nov 15, 4:42 AM (14 h, 17 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
41715887
Default Alt Text
D59892.1794717755.diff (5 KB)

Event Timeline