]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
aarch64: Add BTI support to assembly files
authorSudakshina Das <sudi.das@arm.com>
Tue, 17 Mar 2020 15:44:18 +0000 (15:44 +0000)
committerSzabolcs Nagy <szabolcs.nagy@arm.com>
Wed, 8 Jul 2020 14:02:37 +0000 (15:02 +0100)
To enable building glibc with branch protection, assembly code
needs BTI landing pads and ELF object file markings in the form
of a GNU property note.

The landing pads are unconditionally added to all functions that
may be indirectly called. When the code segment is not mapped
with PROT_BTI these instructions are nops. They are kept in the
code when BTI is not supported so that the layout of performance
critical code is unchanged across configurations.

The GNU property notes are only added when there is support for
BTI in the toolchain, because old binutils does not handle the
notes right. (Does not know how to merge them nor to put them in
PT_GNU_PROPERTY segment instead of PT_NOTE, and some versions
of binutils emit warnings about the unknown GNU property. In
such cases the produced libc binaries would not have valid
ELF marking so BTI would not be enabled.)

Note: functions using ENTRY or ENTRY_ALIGN now start with an
additional BTI c, so alignment of the following code changes,
but ENTRY_ALIGN_AND_PAD was fixed so there is no change to the
existing code layout. Some string functions may need to be
tuned for optimal performance after this commit.

Co-authored-by: Szabolcs Nagy <szabolcs.nagy@arm.com>
Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
sysdeps/aarch64/crti.S
sysdeps/aarch64/crtn.S
sysdeps/aarch64/dl-tlsdesc.S
sysdeps/aarch64/dl-trampoline.S
sysdeps/aarch64/multiarch/memset_emag.S
sysdeps/aarch64/multiarch/memset_falkor.S
sysdeps/aarch64/multiarch/memset_generic.S
sysdeps/aarch64/multiarch/rtld-memset.S
sysdeps/aarch64/start.S
sysdeps/aarch64/sysdep.h

index 1728eac37aa1ed5453da37136bac2a3684ae44ce..c346bcad72d0ce10b4400178bb5deb19a5b8da6c 100644 (file)
@@ -75,6 +75,7 @@ call_weak_fn:
        .hidden _init
        .type   _init, %function
 _init:
+       BTI_C
        stp     x29, x30, [sp, -16]!
        mov     x29, sp
 #if PREINIT_FUNCTION_WEAK
@@ -89,5 +90,6 @@ _init:
        .hidden _fini
        .type   _fini, %function
 _fini:
+       BTI_C
        stp     x29, x30, [sp, -16]!
        mov     x29, sp
index c3e97cc449d7012f8de12707de88fa09546a6a07..0c1ef112c2379a3a95e115bffbf555d3241d7cb3 100644 (file)
@@ -37,6 +37,8 @@
 /* crtn.S puts function epilogues in the .init and .fini sections
    corresponding to the prologues in crti.S. */
 
+#include <sysdep.h>
+
        .section .init,"ax",%progbits
        ldp     x29, x30, [sp], 16
        RET
index 557ad1d5057deb84020571c6e506a659087b45f0..9d96c8632a01cb3bab67f3fc3d5165492cb3b342 100644 (file)
@@ -74,6 +74,7 @@
        cfi_startproc
        .align 2
 _dl_tlsdesc_return:
+       BTI_C
        DELOUSE (0)
        ldr     PTR_REG (0), [x0, #PTR_SIZE]
        RET
@@ -95,6 +96,7 @@ _dl_tlsdesc_return:
        cfi_startproc
        .align  2
 _dl_tlsdesc_undefweak:
+       BTI_C
        str     x1, [sp, #-16]!
        cfi_adjust_cfa_offset (16)
        DELOUSE (0)
@@ -142,6 +144,7 @@ _dl_tlsdesc_undefweak:
        cfi_startproc
        .align 2
 _dl_tlsdesc_dynamic:
+       BTI_C
        DELOUSE (0)
 
        /* Save just enough registers to support fast path, if we fall
index 94e965c0964c5a0b15241a2c510f7c9a63dadf66..2cbfa814348c7db8cf3a42c73b6141fbf25960e1 100644 (file)
@@ -35,6 +35,7 @@
        cfi_startproc
        .align 2
 _dl_runtime_resolve:
+       BTI_C
        /* AArch64 we get called with:
           ip0          &PLTGOT[2]
           ip1          temp(dl resolver entry point)
@@ -126,6 +127,7 @@ _dl_runtime_resolve:
        cfi_startproc
        .align 2
 _dl_runtime_profile:
+       BTI_C
        /* AArch64 we get called with:
           ip0          &PLTGOT[2]
           ip1          temp(dl resolver entry point)
index c4d3533c14b451982957376ee31507dc675a804b..3c2e9d2903686fdc74a0934cf36f31bfff0e626f 100644 (file)
@@ -17,6 +17,8 @@
    License along with the GNU C Library.  If not, see
    <https://www.gnu.org/licenses/>.  */
 
+#include <sysdep.h>
+
 #if IS_IN (libc)
 # define MEMSET __memset_emag
 
index 54fd5abffb1b6638ef8a5fc29e58b2f67765b28a..154527398f435aa578bfc2d67ff0d527dd848298 100644 (file)
@@ -17,6 +17,7 @@
    License along with the GNU C Library.  If not, see
    <https://www.gnu.org/licenses/>.  */
 
+#include <sysdep.h>
 #include <memset-reg.h>
 
 /* Reading dczid_el0 is expensive on falkor so move it into the ifunc
index 46c5329cdfc7d840ee63b50e666cf4eeb0e29987..d746d1d00cd2c50d3f2089ca2f6f7ad52015644f 100644 (file)
@@ -17,6 +17,8 @@
    License along with the GNU C Library.  If not, see
    <https://www.gnu.org/licenses/>.  */
 
+#include <sysdep.h>
+
 #if IS_IN (libc)
 # define MEMSET __memset_generic
 /* Add a hidden definition for use within libc.so.  */
index 44bc4794118c62dcaa3a05807d611fef284cdb73..f9845bdd62a49ff11c00c7489148874b92015a0e 100644 (file)
@@ -17,6 +17,8 @@
    License along with the GNU C Library.  If not, see
    <https://www.gnu.org/licenses/>.  */
 
+#include <sysdep.h>
+
 #if IS_IN (rtld)
 # define MEMSET memset
 # include <sysdeps/aarch64/memset.S>
index d96cf57e2da901bc21e69bf5a87ed034820453f0..75393e1c1809a36b5784878d91670bc3672f56b2 100644 (file)
@@ -46,6 +46,7 @@
        .globl _start
        .type _start,#function
 _start:
+       BTI_C
        /* Create an initial frame with 0 LR and FP */
        mov     x29, #0
        mov     x30, #0
index 604c48917059e770b146fd85b0c18c67fbf138e6..0eeb0bb2f19764ad94ea403d380259adc20dc1a0 100644 (file)
 
 #define ASM_SIZE_DIRECTIVE(name) .size name,.-name
 
+/* Branch Target Identitication support.  */
+#define BTI_C          hint    34
+#define BTI_J          hint    36
+
+/* GNU_PROPERTY_AARCH64_* macros from elf.h for use in asm code.  */
+#define FEATURE_1_AND 0xc0000000
+#define FEATURE_1_BTI 1
+#define FEATURE_1_PAC 2
+
+/* Add a NT_GNU_PROPERTY_TYPE_0 note.  */
+#define GNU_PROPERTY(type, value)      \
+  .section .note.gnu.property, "a";    \
+  .p2align 3;                          \
+  .word 4;                             \
+  .word 16;                            \
+  .word 5;                             \
+  .asciz "GNU";                                \
+  .word type;                          \
+  .word 4;                             \
+  .word value;                         \
+  .word 0;                             \
+  .text
+
+/* Add GNU property note with the supported features to all asm code
+   where sysdep.h is included.  */
+#if HAVE_AARCH64_BTI
+GNU_PROPERTY (FEATURE_1_AND, FEATURE_1_BTI)
+#endif
+
 /* Define an entry point visible from C.  */
 #define ENTRY(name)                                            \
   .globl C_SYMBOL_NAME(name);                                  \
@@ -48,6 +77,7 @@
   .align 4;                                                    \
   C_LABEL(name)                                                        \
   cfi_startproc;                                               \
+  BTI_C;                                                       \
   CALL_MCOUNT
 
 /* Define an entry point visible from C.  */
@@ -57,6 +87,7 @@
   .p2align align;                                              \
   C_LABEL(name)                                                        \
   cfi_startproc;                                               \
+  BTI_C;                                                       \
   CALL_MCOUNT
 
 /* Define an entry point visible from C with a specified alignment and
   .globl C_SYMBOL_NAME(name);                                  \
   .type C_SYMBOL_NAME(name),%function;                         \
   .p2align align;                                              \
-  .rep padding;                                                        \
+  .rep padding - 1; /* -1 for bti c.  */                       \
   nop;                                                         \
   .endr;                                                       \
   C_LABEL(name)                                                        \
   cfi_startproc;                                               \
+  BTI_C;                                                       \
   CALL_MCOUNT
 
 #undef END