git: 522126fea724 - main - csu: Teach csu about PAC and BTI

From: Andrew Turner <andrew_at_FreeBSD.org>
Date: Mon, 30 Oct 2023 17:56:17 UTC
The branch main has been updated by andrew:

URL: https://cgit.FreeBSD.org/src/commit/?id=522126fea72419fae344fbcb897b41588909308a

commit 522126fea72419fae344fbcb897b41588909308a
Author:     Andrew Turner <andrew@FreeBSD.org>
AuthorDate: 2023-10-12 10:03:37 +0000
Commit:     Andrew Turner <andrew@FreeBSD.org>
CommitDate: 2023-10-30 17:55:44 +0000

    csu: Teach csu about PAC and BTI
    
    Add the Branch Target Identification (BTI) note to libc assembly
    sources and Pointer Authentication Code (PAC) instructions to _init and
    _fini.
    
    _init and _fini may be called indirectly so need a BTI landing pad. As
    they are non-leaf functions use the appropriate PAC instruction that
    also guards against changing the link register.
    
    As all object files need the note for any binary using these object files
    we need to insert it in all asm files.
    
    Reviewed by:    markj
    Sponsored by:   Arm Ltd
    Differential Revision:  https://reviews.freebsd.org/D42227
---
 lib/csu/aarch64/crt1_s.S          | 4 ++++
 lib/csu/aarch64/crti.S            | 5 +++++
 lib/csu/aarch64/crtn.S            | 6 ++++++
 lib/csu/common/crtbrand.S         | 5 +++++
 lib/csu/common/feature_note.S     | 5 +++++
 lib/csu/common/ignore_init_note.S | 5 +++++
 6 files changed, 30 insertions(+)

diff --git a/lib/csu/aarch64/crt1_s.S b/lib/csu/aarch64/crt1_s.S
index 97b4bff35a0b..981cf4653efd 100644
--- a/lib/csu/aarch64/crt1_s.S
+++ b/lib/csu/aarch64/crt1_s.S
@@ -30,6 +30,8 @@
  */
 
 #include <machine/asm.h>
+#include <sys/elf_common.h>
+
 	/*
 	 * The program entry point
 	 * void _start(char **ap, void (*cleanup)(void)) __dead2
@@ -62,3 +64,5 @@ eprol:
 END(_start)
 
 	.section .note.GNU-stack,"",@progbits
+
+GNU_PROPERTY_AARCH64_FEATURE_1_NOTE(GNU_PROPERTY_AARCH64_FEATURE_1_VAL)
diff --git a/lib/csu/aarch64/crti.S b/lib/csu/aarch64/crti.S
index 0a20f4c5e314..8bd2dfac1494 100644
--- a/lib/csu/aarch64/crti.S
+++ b/lib/csu/aarch64/crti.S
@@ -28,11 +28,14 @@
  */
 
 #include <machine/asm.h>
+#include <sys/elf_common.h>
+
 	.section .init,"ax",@progbits
 	.align 4
 	.globl	_init
 	.type	_init,@function
 _init:
+	PAC_LR_SIGN
 	stp	x29, x30, [sp, #-16]!
 	mov	x29, sp
 
@@ -41,6 +44,8 @@ _init:
 	.globl	_fini
 	.type	_fini,@function
 _fini:
+	PAC_LR_SIGN
 	stp	x29, x30, [sp, #-16]!
 	mov	x29, sp
 
+GNU_PROPERTY_AARCH64_FEATURE_1_NOTE(GNU_PROPERTY_AARCH64_FEATURE_1_VAL)
diff --git a/lib/csu/aarch64/crtn.S b/lib/csu/aarch64/crtn.S
index 924aa36aa045..441411f0ab83 100644
--- a/lib/csu/aarch64/crtn.S
+++ b/lib/csu/aarch64/crtn.S
@@ -28,13 +28,19 @@
  */
 
 #include <machine/asm.h>
+#include <sys/elf_common.h>
+
 	.section .init,"ax",@progbits
 	ldp	x29, x30, [sp], #16
+	PAC_LR_AUTH
 	ret
 
 
 	.section .fini,"ax",@progbits
 	ldp	x29, x30, [sp], #16
+	PAC_LR_AUTH
 	ret
 
 	.section .note.GNU-stack,"",%progbits
+
+GNU_PROPERTY_AARCH64_FEATURE_1_NOTE(GNU_PROPERTY_AARCH64_FEATURE_1_VAL)
diff --git a/lib/csu/common/crtbrand.S b/lib/csu/common/crtbrand.S
index 0ed86bfba2b2..b59e2fd8056b 100644
--- a/lib/csu/common/crtbrand.S
+++ b/lib/csu/common/crtbrand.S
@@ -47,3 +47,8 @@
 4:
 
 	.section .note.GNU-stack,"",%progbits
+
+#ifdef __aarch64__
+/* This is needed in all objects for BTI to be used in the linked elf file */
+GNU_PROPERTY_AARCH64_FEATURE_1_NOTE(GNU_PROPERTY_AARCH64_FEATURE_1_VAL)
+#endif
diff --git a/lib/csu/common/feature_note.S b/lib/csu/common/feature_note.S
index 343d2a9262e2..5c1d56d4cde1 100644
--- a/lib/csu/common/feature_note.S
+++ b/lib/csu/common/feature_note.S
@@ -40,3 +40,8 @@
 4:
 
 	.section .note.GNU-stack,"",%progbits
+
+#ifdef __aarch64__
+/* This is needed in all objects for BTI to be used in the linked elf file */
+GNU_PROPERTY_AARCH64_FEATURE_1_NOTE(GNU_PROPERTY_AARCH64_FEATURE_1_VAL)
+#endif
diff --git a/lib/csu/common/ignore_init_note.S b/lib/csu/common/ignore_init_note.S
index fa37a8990538..57fb4e8f4d51 100644
--- a/lib/csu/common/ignore_init_note.S
+++ b/lib/csu/common/ignore_init_note.S
@@ -41,3 +41,8 @@
 4:
 
 	.section .note.GNU-stack,"",%progbits
+
+#ifdef __aarch64__
+/* This is needed in all objects for BTI to be used in the linked elf file */
+GNU_PROPERTY_AARCH64_FEATURE_1_NOTE(GNU_PROPERTY_AARCH64_FEATURE_1_VAL)
+#endif