[PATCH RFC 12/43] x86,rethook: Adapt assembly for PIE support

From: Hou Wenlong
Date: Fri Apr 28 2023 - 05:53:17 EST


Change the assembly code to use only relative references of symbols for
the kernel to be PIE compatible.

Signed-off-by: Hou Wenlong <houwenlong.hwl@xxxxxxxxxxxx>
Cc: Thomas Garnier <thgarnie@xxxxxxxxxxxx>
Cc: Lai Jiangshan <jiangshan.ljs@xxxxxxxxxxxx>
Cc: Kees Cook <keescook@xxxxxxxxxxxx>
---
arch/x86/kernel/rethook.c | 8 ++++++++
1 file changed, 8 insertions(+)

diff --git a/arch/x86/kernel/rethook.c b/arch/x86/kernel/rethook.c
index 8a1c0111ae79..ff3733b765e0 100644
--- a/arch/x86/kernel/rethook.c
+++ b/arch/x86/kernel/rethook.c
@@ -27,7 +27,15 @@ asm(
#ifdef CONFIG_X86_64
ANNOTATE_NOENDBR /* This is only jumped from ret instruction */
/* Push a fake return address to tell the unwinder it's a rethook. */
+#ifdef CONFIG_X86_PIE
+ " pushq $0\n"
+ " pushq %rdi\n"
+ " leaq arch_rethook_trampoline(%rip), %rdi\n"
+ " movq %rdi, 8(%rsp)\n"
+ " popq %rdi\n"
+#else
" pushq $arch_rethook_trampoline\n"
+#endif
UNWIND_HINT_FUNC
" pushq $" __stringify(__KERNEL_DS) "\n"
/* Save the 'sp - 16', this will be fixed later. */
--
2.31.1