From 9cb81a170fcddbc342804cb8e42eb9fe50d1afc5 Mon Sep 17 00:00:00 2001
From: Jonathan Wright <jonathan.wright@arm.com>
Date: Mon, 28 Sep 2020 13:35:51 +0100
Subject: [PATCH] aarch64: Add Armv8.5-A BTI support

Add Branch Target Identifiers (BTIs) to all functions defined in
AArch64 assembly files. Most of the BTI landing pads are added
automatically by the 'function' macro.

BTI support is turned on or off at compile time based on the presence
of the __ARM_FEATURE_BTI_DEFAULT feature macro.

A binary compiled with BTI support can be executed on an Armv8-A
processor without BTI support because the instructions are defined in
NOP space.

Signed-off-by: Jonathan Wright <jonathan.wright@arm.com>
Signed-off-by: Elijah Ahmad <elijah.ahmad@arm.com>
Change-Id: I94182ac3059dece5152203b717633f6dbcb9c0ac
---
 libavcodec/aarch64/fft_neon.S      |  4 ++++
 libavcodec/aarch64/h264idct_neon.S |  4 ++++
 libavutil/aarch64/asm.S            | 30 ++++++++++++++++++++++++++++++
 3 files changed, 38 insertions(+)

diff --git a/libavcodec/aarch64/fft_neon.S b/libavcodec/aarch64/fft_neon.S
index 862039f97d..b4020fc8c7 100644
--- a/libavcodec/aarch64/fft_neon.S
+++ b/libavcodec/aarch64/fft_neon.S
@@ -36,6 +36,7 @@
 
 
 function fft4_neon
+        AARCH64_VALID_JUMP_TARGET
         ld1             {v0.2s,v1.2s,v2.2s,v3.2s}, [x0]
 
         fadd            v4.2s,  v0.2s,  v1.2s   // r0+r1,i0+i1
@@ -58,6 +59,7 @@ function fft4_neon
 endfunc
 
 function fft8_neon
+        AARCH64_VALID_JUMP_TARGET
         mov             x1,  x0
         ld1             {v0.2s, v1.2s, v2.2s, v3.2s},  [x0], #32
         ld1             {v16.2s,v17.2s,v18.2s,v19.2s}, [x0]
@@ -108,6 +110,7 @@ function fft8_neon
 endfunc
 
 function fft16_neon
+        AARCH64_VALID_JUMP_TARGET
         mov             x1,  x0
         ld1             {v0.2s, v1.2s, v2.2s, v3.2s},  [x0], #32
         ld1             {v16.2s,v17.2s,v18.2s,v19.2s}, [x0], #32
@@ -337,6 +340,7 @@ endfunc
 
 .macro  def_fft n, n2, n4
 function fft\n\()_neon, align=6
+        AARCH64_VALID_JUMP_TARGET
         sub             sp,  sp,  #16
         stp             x28, x30, [sp]
         add             x28, x0,  #\n4*2*8
diff --git a/libavcodec/aarch64/h264idct_neon.S b/libavcodec/aarch64/h264idct_neon.S
index 7de44205d3..7d2879b0ce 100644
--- a/libavcodec/aarch64/h264idct_neon.S
+++ b/libavcodec/aarch64/h264idct_neon.S
@@ -24,6 +24,7 @@
 
 function ff_h264_idct_add_neon, export=1
 .L_ff_h264_idct_add_neon:
+        AARCH64_VALID_CALL_TARGET
         ld1             {v0.4H, v1.4H, v2.4H, v3.4H},  [x1]
         sxtw            x2,     w2
         movi            v30.8H, #0
@@ -79,6 +80,7 @@ endfunc
 
 function ff_h264_idct_dc_add_neon, export=1
 .L_ff_h264_idct_dc_add_neon:
+        AARCH64_VALID_CALL_TARGET
         sxtw            x2,  w2
         mov             w3,       #0
         ld1r            {v2.8H},  [x1]
@@ -266,6 +268,7 @@ endfunc
 
 function ff_h264_idct8_add_neon, export=1
 .L_ff_h264_idct8_add_neon:
+        AARCH64_VALID_CALL_TARGET
         movi            v19.8H,   #0
         sxtw            x2,       w2
         ld1             {v24.8H, v25.8H}, [x1]
@@ -330,6 +333,7 @@ endfunc
 
 function ff_h264_idct8_dc_add_neon, export=1
 .L_ff_h264_idct8_dc_add_neon:
+        AARCH64_VALID_CALL_TARGET
         mov             w3,       #0
         sxtw            x2,       w2
         ld1r            {v31.8H}, [x1]
diff --git a/libavutil/aarch64/asm.S b/libavutil/aarch64/asm.S
index d1fa72b3c6..f78942eb86 100644
--- a/libavutil/aarch64/asm.S
+++ b/libavutil/aarch64/asm.S
@@ -36,6 +36,35 @@
 #   define __has_feature(x) 0
 #endif
 
+/* Support macros for the Armv8.5-A Branch Target Identification feature which
+ * requires emitting a .note.gnu.property section with the appropriate
+ * architecture-dependent feature bits set.
+ * Read more: "ELF for the Arm® 64-bit Architecture"
+ */
+#if defined(__ARM_FEATURE_BTI_DEFAULT) && (__ARM_FEATURE_BTI_DEFAULT == 1)
+#   define GNU_PROPERTY_AARCH64_BTI (1 << 0)   // Has BTI
+#   define AARCH64_VALID_CALL_TARGET hint #34  // BTI 'c'
+#   define AARCH64_VALID_JUMP_TARGET hint #38  // BTI 'j'
+#else
+#   define GNU_PROPERTY_AARCH64_BTI 0          // No BTI
+#   define AARCH64_VALID_CALL_TARGET
+#   define AARCH64_VALID_JUMP_TARGET
+#endif
+
+#if (GNU_PROPERTY_AARCH64_BTI != 0)
+    .pushsection .note.gnu.property, "a";
+    .balign 8;
+    .long 4;
+    .long 0x10;
+    .long 0x5;
+    .asciz "GNU";
+    .long 0xc0000000; /* GNU_PROPERTY_AARCH64_FEATURE_1_AND */
+    .long 4;
+    .long GNU_PROPERTY_AARCH64_BTI;
+    .long 0
+    .popsection
+#endif
+
 .macro  function name, export=0, align=2
     .macro endfunc
 ELF     .size   \name, . - \name
@@ -49,6 +78,7 @@ FUNC    .endfunc
 ELF     .type   EXTERN_ASM\name, %function
 FUNC    .func   EXTERN_ASM\name
 EXTERN_ASM\name:
+    AARCH64_VALID_CALL_TARGET
     .else
 ELF     .type   \name, %function
 FUNC    .func   \name
-- 
2.30.1 (Apple Git-130)

