93dc2d
commit aa3a97496c82a8443039248ebee650322c9480f4
93dc2d
Author: Aurelien Jarno <aurelien@aurel32.net>
93dc2d
Date:   Thu Dec 16 00:06:28 2021 +0100
93dc2d
93dc2d
    riscv: align stack before calling _dl_init [BZ #28703]
93dc2d
    
93dc2d
    Align the stack pointer to 128 bits during the call to _dl_init() as
93dc2d
    specified by the RISC-V ABI [1]. This fixes the elf/tst-align2 test.
93dc2d
    
93dc2d
    Fixes bug 28703.
93dc2d
    
93dc2d
    [1] https://github.com/riscv-non-isa/riscv-elf-psabi-doc
93dc2d
    
93dc2d
    (cherry picked from commit 225da459cebef1037dcd78b56471edc0721e1c41)
93dc2d
93dc2d
diff --git a/sysdeps/riscv/dl-machine.h b/sysdeps/riscv/dl-machine.h
93dc2d
index aedf69fcdd8aff50..951268923da26a37 100644
93dc2d
--- a/sysdeps/riscv/dl-machine.h
93dc2d
+++ b/sysdeps/riscv/dl-machine.h
93dc2d
@@ -127,8 +127,14 @@ elf_machine_load_address (void)
93dc2d
 	sll a3, a1, " STRINGXP (PTRLOG) "\n\
93dc2d
 	add a3, a3, a2\n\
93dc2d
 	add a3, a3, " STRINGXP (SZREG) "\n\
93dc2d
+	# Stash the stack pointer in s1.\n\
93dc2d
+	mv s1, sp\n\
93dc2d
+	# Align stack to 128 bits for the _dl_init call.\n\
93dc2d
+	andi sp, sp,-16\n\
93dc2d
 	# Call the function to run the initializers.\n\
93dc2d
 	jal _dl_init\n\
93dc2d
+	# Restore the stack pointer for _start.\n\
93dc2d
+	mv sp, s1\n\
93dc2d
 	# Pass our finalizer function to _start.\n\
93dc2d
 	lla a0, _dl_fini\n\
93dc2d
 	# Jump to the user entry point.\n\