summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibELF/Arch
diff options
context:
space:
mode:
authorGunnar Beutner <gbeutner@serenityos.org>2021-07-09 00:58:43 +0200
committerAndreas Kling <kling@serenityos.org>2021-07-10 01:41:57 +0200
commit06883ed8a32cc44ce5fdf26af0b5ec5ee2e2dbe7 (patch)
treef72c8f0d697bede3f9a29ea3e95a7d8d974f8393 /Userland/Libraries/LibELF/Arch
parentf4a318ee2d868d6d4f712afa8de786bba9703e36 (diff)
downloadserenity-06883ed8a32cc44ce5fdf26af0b5ec5ee2e2dbe7.zip
Kernel+Userland: Make the stack alignment comply with the System V ABI
The System V ABI for both x86 and x86_64 requires that the stack pointer is 16-byte aligned on entry. Previously we did not align the stack pointer properly. As far as "main" was concerned the stack alignment was correct even without this patch due to how the C++ _start function and the kernel interacted, i.e. the kernel misaligned the stack as far as the ABI was concerned but that misalignment (read: it was properly aligned for a regular function call - but misaligned in terms of what the ABI dictates) was actually expected by our _start function.
Diffstat (limited to 'Userland/Libraries/LibELF/Arch')
-rw-r--r--Userland/Libraries/LibELF/Arch/i386/entry.S28
-rw-r--r--Userland/Libraries/LibELF/Arch/x86_64/entry.S16
2 files changed, 44 insertions, 0 deletions
diff --git a/Userland/Libraries/LibELF/Arch/i386/entry.S b/Userland/Libraries/LibELF/Arch/i386/entry.S
new file mode 100644
index 0000000000..8fc185398c
--- /dev/null
+++ b/Userland/Libraries/LibELF/Arch/i386/entry.S
@@ -0,0 +1,28 @@
+/*
+ * Copyright (c) 2021, Gunnar Beutner <gbeutner@serenityos.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+ .align 4
+ .globl _invoke_entry
+ .hidden _invoke_entry
+ .type _invoke_entry,@function
+_invoke_entry: # (argc, argv, envp, entry)
+ addl $4, %esp # return address
+ popl %edi # argc
+ popl %esi # argv
+ popl %edx # envp
+ popl %ecx # entry
+
+ // The System V ABI for x86 and x86_64 prescribes that the stack pointer is 16-byte aligned
+ andl $~15, %esp
+
+ // We're going to push three arguments so we need to align the stack for that
+ subl $4, %esp
+
+ // FIXME: The way we're setting up the stack and passing arguments to the entry point isn't ABI-compliant
+ pushl %edx
+ pushl %esi
+ pushl %edi
+ jmp *%ecx
diff --git a/Userland/Libraries/LibELF/Arch/x86_64/entry.S b/Userland/Libraries/LibELF/Arch/x86_64/entry.S
new file mode 100644
index 0000000000..1195cfded8
--- /dev/null
+++ b/Userland/Libraries/LibELF/Arch/x86_64/entry.S
@@ -0,0 +1,16 @@
+/*
+ * Copyright (c) 2021, Gunnar Beutner <gbeutner@serenityos.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+ .align 4
+ .globl _invoke_entry
+ .hidden _invoke_entry
+ .type _invoke_entry,@function
+_invoke_entry: # (argc, argv, envp, entry)
+ // The System V ABI for x86 and x86_64 prescribes that the stack pointer is 16-byte aligned
+ andq $~15, %rsp
+
+ // FIXME: The way we're setting up the stack and passing arguments to the entry point isn't ABI-compliant
+ jmp *%rcx