From 9d267a83faef7724f2901bcdc8b18c47713fe174 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20T=C3=B6rnblom?= Date: Sun, 2 Aug 2026 17:12:35 +0200 Subject: [PATCH] crt: make sure stack is aligned (and restored on exit) --- crt/Makefile | 5 ++++- crt/_start.S | 37 +++++++++++++++++++++++++++++++++++++ crt/crt.c | 2 +- 3 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 crt/_start.S diff --git a/crt/Makefile b/crt/Makefile index 614526f..2d41339 100644 --- a/crt/Makefile +++ b/crt/Makefile @@ -23,7 +23,7 @@ CFLAGS += -fms-extensions -Wno-microsoft-anon-tag CFLAGS += -O1 OBJ := crt.o syscall.o klog.o nid.o kernel.o rtld.o rtld_so.o rtld_sprx.o \ - rtld_payload.o rtld_dlfcn.o mdbg.o patch.o kernel_procio.o + rtld_payload.o rtld_dlfcn.o mdbg.o patch.o kernel_procio.o _start.o .PHONY: all clean install @@ -53,6 +53,9 @@ crt1.o: $(OBJ) %.o: %.c $(CC) -c $(CFLAGS) -o $@ $^ +%.o: %.S + $(CC) -c $(CFLAGS) -masm=intel -o $@ $^ + clean: rm -f *.o *.a diff --git a/crt/_start.S b/crt/_start.S new file mode 100644 index 0000000..76bc94d --- /dev/null +++ b/crt/_start.S @@ -0,0 +1,37 @@ +/* Copyright (C) 2026 John Törnblom + +This program is free software; you can redistribute it and/or modify it +under the terms of the GNU General Public License as published by the +Free Software Foundation; either version 3, or (at your option) any +later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; see the file COPYING. If not, see +. */ + +.intel_syntax noprefix + +.data +.local __crt_reg_backup +.type __crt_reg_backup, @object +__crt_reg_backup: + .quad 0 + +.text +.extern __crt_start +.type __crt_start, @function + +.text +.global _start +.type _start, @function +_start: + mov [__crt_reg_backup + rip], rsp + and rsp, -16 + call __crt_start + mov rsp, [__crt_reg_backup + rip] + ret diff --git a/crt/crt.c b/crt/crt.c index 0bac16e..857ffe5 100644 --- a/crt/crt.c +++ b/crt/crt.c @@ -194,7 +194,7 @@ payload_get_args(void) { * Entry-point invoked by the ELF loader. **/ int -_start(payload_args_t *args) { +__crt_start(payload_args_t *args) { int err; // clear .bss section