From aab5f9215cb23da631c3c988c7765b1790079f41 Mon Sep 17 00:00:00 2001
From: Simo Sorce <simo@redhat.com>
Date: Sat, 27 Apr 2019 10:44:27 -0400
Subject: [PATCH 4/4] WIP

---
 arm/machine.m4                        | 3 +++
 asm.m4                                | 2 +-
 config.m4.in                          | 2 +-
 configure.ac                          | 7 -------
 sparc32/machine.m4                    | 2 ++
 sparc64/machine.m4                    | 3 +++
 x86/aes-decrypt-internal.asm          | 1 -
 x86/aes-encrypt-internal.asm          | 1 -
 x86/arcfour-crypt.asm                 | 1 -
 x86/camellia-crypt-internal.asm       | 1 -
 x86/machine.m4                        | 4 +++-
 x86/md5-compress.asm                  | 1 -
 x86/sha1-compress.asm                 | 1 -
 x86_64/aes-decrypt-internal.asm       | 1 -
 x86_64/aes-encrypt-internal.asm       | 1 -
 x86_64/aesni/aes-decrypt-internal.asm | 1 -
 x86_64/aesni/aes-encrypt-internal.asm | 1 -
 x86_64/camellia-crypt-internal.asm    | 1 -
 x86_64/chacha-core-internal.asm       | 1 -
 x86_64/fat/cpuid.asm                  | 2 +-
 x86_64/gcm-hash8.asm                  | 1 -
 x86_64/machine.m4                     | 4 +++-
 x86_64/md5-compress.asm               | 1 -
 x86_64/memxor.asm                     | 1 -
 x86_64/memxor3.asm                    | 1 -
 x86_64/poly1305-internal.asm          | 1 -
 x86_64/salsa20-core-internal.asm      | 1 -
 x86_64/salsa20-crypt.asm              | 1 -
 x86_64/serpent-decrypt.asm            | 1 -
 x86_64/serpent-encrypt.asm            | 1 -
 x86_64/sha1-compress.asm              | 1 -
 x86_64/sha256-compress.asm            | 1 -
 x86_64/sha3-permute.asm               | 1 -
 x86_64/sha512-compress.asm            | 1 -
 x86_64/sha_ni/sha1-compress.asm       | 1 -
 x86_64/sha_ni/sha256-compress.asm     | 1 -
 x86_64/umac-nh-n.asm                  | 1 -
 x86_64/umac-nh.asm                    | 1 -
 38 files changed, 17 insertions(+), 41 deletions(-)

diff --git a/arm/machine.m4 b/arm/machine.m4
index f982a66a..6c4801ef 100644
--- a/arm/machine.m4
+++ b/arm/machine.m4
@@ -54,3 +54,6 @@ define(<D1REG>, <ifelse(
 	$1, q14, d29,
 	$1, q15, d31,
 	<NO REGISTER>)>)dnl
+
+define(<GNU_PROPERTY_NOTES>, <>)
+define(<CODEFROM>, <>)
diff --git a/asm.m4 b/asm.m4
index 5a5d4ac2..cc31d97c 100644
--- a/asm.m4
+++ b/asm.m4
@@ -33,7 +33,7 @@ define(<PROLOGUE>,
 <.globl C_NAME($1)
 DECLARE_FUNC(C_NAME($1))
 C_NAME($1):
-CET_ENDBR>)
+CODEFROM()>)
 
 define(<EPILOGUE>,
 <ifelse(ELF_STYLE,yes,
diff --git a/config.m4.in b/config.m4.in
index c3ebad60..90796f26 100644
--- a/config.m4.in
+++ b/config.m4.in
@@ -9,7 +9,7 @@ define(<W64_ABI>, <@W64_ABI@>)dnl
 define(<RODATA>, <@ASM_RODATA@>)dnl
 define(<WORDS_BIGENDIAN>, <@ASM_WORDS_BIGENDIAN@>)dnl
 define(<CET_PROTECTION>, <@ASM_CET_PROTECTION@>)dnl
-define(<CET_ENDBR>, <@ASM_CET_ENDBR@>)dnl
 divert(1)
 @ASM_MARK_NOEXEC_STACK@
+GNU_PROPERTY_NOTES()
 divert
diff --git a/configure.ac b/configure.ac
index 7beb35d9..aba942ce 100644
--- a/configure.ac
+++ b/configure.ac
@@ -706,7 +706,6 @@ ASM_TYPE_FUNCTION='@function'
 ASM_TYPE_PROGBITS='@progbits'
 ASM_MARK_NOEXEC_STACK=''
 ASM_CET_PROTECTION='no'
-ASM_CET_ENDBR=''
 ASM_ALIGN_LOG=''
 ASM_GEN_BUILD_NOTES=''
 
@@ -842,11 +841,6 @@ EOF
 
   if test "x$enable_cet_protection" = xyes ; then
     ASM_CET_PROTECTION=yes
-    if test "$ABI" = 64 ; then
-      ASM_CET_ENDBR=endbr64
-    else
-      ASM_CET_ENDBR=endbr32
-    fi
   fi
 fi
 
@@ -861,7 +855,6 @@ AC_SUBST(W64_ABI)
 AC_SUBST(ASM_WORDS_BIGENDIAN)
 AC_SUBST(ASM_GEN_BUILD_NOTES)
 AC_SUBST(ASM_CET_PROTECTION)
-AC_SUBST(ASM_CET_ENDBR)
 AC_SUBST(EMULATOR)
 
 AC_SUBST(LIBNETTLE_MAJOR)
diff --git a/sparc32/machine.m4 b/sparc32/machine.m4
index e69de29b..59b43f8a 100644
--- a/sparc32/machine.m4
+++ b/sparc32/machine.m4
@@ -0,0 +1,2 @@
+define(<GNU_PROPERTY_NOTES>, <>)
+define(<CODEFROM>, <>)
diff --git a/sparc64/machine.m4 b/sparc64/machine.m4
index 4c1c0e5a..fe5cf5ef 100644
--- a/sparc64/machine.m4
+++ b/sparc64/machine.m4
@@ -2,3 +2,6 @@ define(<BIAS>, 2047) C Magic stack bias for the Sparc64 ABI
 
 .register %g2,#scratch
 .register %g3,#scratch
+
+define(<GNU_PROPERTY_NOTES>, <>)
+define(<CODEFROM>, <>)
diff --git a/x86/aes-decrypt-internal.asm b/x86/aes-decrypt-internal.asm
index 1d16f6db..ff535b6a 100644
--- a/x86/aes-decrypt-internal.asm
+++ b/x86/aes-decrypt-internal.asm
@@ -175,4 +175,3 @@ PROLOGUE(_nettle_aes_decrypt)
 	popl	%ebx
 	ret
 EPILOGUE(_nettle_aes_decrypt)
-GNU_CET_SECTION()
diff --git a/x86/aes-encrypt-internal.asm b/x86/aes-encrypt-internal.asm
index d9579e04..934158f7 100644
--- a/x86/aes-encrypt-internal.asm
+++ b/x86/aes-encrypt-internal.asm
@@ -175,4 +175,3 @@ PROLOGUE(_nettle_aes_encrypt)
 	popl	%ebx
 	ret
 EPILOGUE(_nettle_aes_encrypt)
-GNU_CET_SECTION()
diff --git a/x86/arcfour-crypt.asm b/x86/arcfour-crypt.asm
index 11f592e7..df3fe869 100644
--- a/x86/arcfour-crypt.asm
+++ b/x86/arcfour-crypt.asm
@@ -123,4 +123,3 @@ C .Lloop_done:
 	popl	%ebx
 	ret
 EPILOGUE(nettle_arcfour_crypt)
-GNU_CET_SECTION()
diff --git a/x86/camellia-crypt-internal.asm b/x86/camellia-crypt-internal.asm
index afac1fcc..ce8c57f0 100644
--- a/x86/camellia-crypt-internal.asm
+++ b/x86/camellia-crypt-internal.asm
@@ -223,4 +223,3 @@ PROLOGUE(_nettle_camellia_crypt)
 	popl	%ebx
 	ret
 EPILOGUE(_nettle_camellia_crypt)
-GNU_CET_SECTION()
diff --git a/x86/machine.m4 b/x86/machine.m4
index 1153c757..9341d837 100644
--- a/x86/machine.m4
+++ b/x86/machine.m4
@@ -29,7 +29,7 @@ dnl Shadow Stack
 define(<GNU_PROPERTY_X86_FEATURE_1_SHSTK>, <0x02>)
 
 dnl NOTE: GNU Property sections MUST have alignment of 8
-define(<GNU_CET_SECTION>,
+define(<GNU_PROPERTY_NOTES>,
 <ifelse(CET_PROTECTION,yes,
 <.pushsection .note.gnu.property,"a"
 ALIGN(8)
@@ -49,3 +49,5 @@ ALIGN(8)
 4:
 .popsection
 >,<>)>)
+
+define(<CODEFROM>, <ifelse(CET_PROTECTION,yes,<endbr32>,<>)>)
diff --git a/x86/md5-compress.asm b/x86/md5-compress.asm
index 6293f052..c849c082 100644
--- a/x86/md5-compress.asm
+++ b/x86/md5-compress.asm
@@ -185,4 +185,3 @@ PROLOGUE(nettle_md5_compress)
 	popl	%ebx
 	ret
 EPILOGUE(nettle_md5_compress)
-GNU_CET_SECTION()
diff --git a/x86/sha1-compress.asm b/x86/sha1-compress.asm
index 4e1f121c..03bdcdc9 100644
--- a/x86/sha1-compress.asm
+++ b/x86/sha1-compress.asm
@@ -1541,7 +1541,6 @@ C 	ROUND_F2(SB, SC, SD, SE, SA, 79, K4VALUE)
 	popl	%ebx
 	ret
 EPILOGUE(nettle_sha1_compress)
-GNU_CET_SECTION()
 
 C TODO:
 
diff --git a/x86_64/aes-decrypt-internal.asm b/x86_64/aes-decrypt-internal.asm
index eedfaaf0..43f2f394 100644
--- a/x86_64/aes-decrypt-internal.asm
+++ b/x86_64/aes-decrypt-internal.asm
@@ -150,4 +150,3 @@ PROLOGUE(_nettle_aes_decrypt)
 	W64_EXIT(6, 0)
 	ret
 EPILOGUE(_nettle_aes_decrypt)
-GNU_CET_SECTION()
diff --git a/x86_64/aes-encrypt-internal.asm b/x86_64/aes-encrypt-internal.asm
index 3a5a1e86..dfb498f5 100644
--- a/x86_64/aes-encrypt-internal.asm
+++ b/x86_64/aes-encrypt-internal.asm
@@ -151,4 +151,3 @@ PROLOGUE(_nettle_aes_encrypt)
 	W64_EXIT(6, 0)
 	ret
 EPILOGUE(_nettle_aes_encrypt)
-GNU_CET_SECTION()
diff --git a/x86_64/aesni/aes-decrypt-internal.asm b/x86_64/aesni/aes-decrypt-internal.asm
index 1b1a1a4d..3d6d6e30 100644
--- a/x86_64/aesni/aes-decrypt-internal.asm
+++ b/x86_64/aesni/aes-decrypt-internal.asm
@@ -132,4 +132,3 @@ PROLOGUE(_nettle_aes_decrypt)
 	W64_EXIT(6, 16)
 	ret
 EPILOGUE(_nettle_aes_decrypt)
-GNU_CET_SECTION()
diff --git a/x86_64/aesni/aes-encrypt-internal.asm b/x86_64/aesni/aes-encrypt-internal.asm
index f7338ef6..99caf1f8 100644
--- a/x86_64/aesni/aes-encrypt-internal.asm
+++ b/x86_64/aesni/aes-encrypt-internal.asm
@@ -132,4 +132,3 @@ PROLOGUE(_nettle_aes_encrypt)
 	W64_EXIT(6, 16)
 	ret
 EPILOGUE(_nettle_aes_encrypt)
-GNU_CET_SECTION()
diff --git a/x86_64/camellia-crypt-internal.asm b/x86_64/camellia-crypt-internal.asm
index 71750172..040e030f 100644
--- a/x86_64/camellia-crypt-internal.asm
+++ b/x86_64/camellia-crypt-internal.asm
@@ -200,4 +200,3 @@ PROLOGUE(_nettle_camellia_crypt)
 	W64_EXIT(6, 0)
 	ret
 EPILOGUE(_nettle_camellia_crypt)
-GNU_CET_SECTION()
diff --git a/x86_64/chacha-core-internal.asm b/x86_64/chacha-core-internal.asm
index 3125dee7..9e5dc394 100644
--- a/x86_64/chacha-core-internal.asm
+++ b/x86_64/chacha-core-internal.asm
@@ -126,4 +126,3 @@ PROLOGUE(_nettle_chacha_core)
 	W64_EXIT(3, 6)
 	ret
 EPILOGUE(_nettle_chacha_core)
-GNU_CET_SECTION()
diff --git a/x86_64/fat/cpuid.asm b/x86_64/fat/cpuid.asm
index c4a5b538..f317d56e 100644
--- a/x86_64/fat/cpuid.asm
+++ b/x86_64/fat/cpuid.asm
@@ -56,4 +56,4 @@ PROLOGUE(_nettle_cpuid)
 	W64_EXIT(2)
 	ret
 EPILOGUE(_nettle_cpuid)
-GNU_CET_SECTION()
+
diff --git a/x86_64/gcm-hash8.asm b/x86_64/gcm-hash8.asm
index 54608ae8..bfaa6ef8 100644
--- a/x86_64/gcm-hash8.asm
+++ b/x86_64/gcm-hash8.asm
@@ -199,7 +199,6 @@ ALIGN(16)
 	jnz	.Lread_loop
 	ret
 EPILOGUE(_nettle_gcm_hash8)
-GNU_CET_SECTION()
 
 define(<W>, <0x$2$1>)
 	RODATA
diff --git a/x86_64/machine.m4 b/x86_64/machine.m4
index a64241ce..1c07665d 100644
--- a/x86_64/machine.m4
+++ b/x86_64/machine.m4
@@ -186,7 +186,7 @@ dnl Shadow Stack
 define(<GNU_PROPERTY_X86_FEATURE_1_SHSTK>, <0x02>)
 
 dnl NOTE: GNU Property sections MUST have alignment of 8
-define(<GNU_CET_SECTION>,
+define(<GNU_PROPERTY_NOTES>,
 <ifelse(CET_PROTECTION,yes,
 <.pushsection .note.gnu.property,"a"
 ALIGN(8)
@@ -206,3 +206,5 @@ ALIGN(8)
 4:
 .popsection
 >,<>)>)
+
+define(<CODEFROM>, <ifelse(CET_PROTECTION,yes,<endbr64>,<>)>)
diff --git a/x86_64/md5-compress.asm b/x86_64/md5-compress.asm
index ef1fcb89..182b8f18 100644
--- a/x86_64/md5-compress.asm
+++ b/x86_64/md5-compress.asm
@@ -174,4 +174,3 @@ PROLOGUE(nettle_md5_compress)
 
 	ret
 EPILOGUE(nettle_md5_compress)
-GNU_CET_SECTION()
diff --git a/x86_64/memxor.asm b/x86_64/memxor.asm
index a0ea94b4..f07f0017 100644
--- a/x86_64/memxor.asm
+++ b/x86_64/memxor.asm
@@ -171,4 +171,3 @@ ifdef(<USE_SSE2>, <
 >)	
 
 EPILOGUE(nettle_memxor)
-GNU_CET_SECTION()
diff --git a/x86_64/memxor3.asm b/x86_64/memxor3.asm
index b0c0e35c..8ff3e79c 100644
--- a/x86_64/memxor3.asm
+++ b/x86_64/memxor3.asm
@@ -261,4 +261,3 @@ ifelse(USE_SSE2, yes, <
 	
 
 EPILOGUE(nettle_memxor3)
-GNU_CET_SECTION()
diff --git a/x86_64/poly1305-internal.asm b/x86_64/poly1305-internal.asm
index 3737450f..98159ad3 100644
--- a/x86_64/poly1305-internal.asm
+++ b/x86_64/poly1305-internal.asm
@@ -184,4 +184,3 @@ define(<T1>, <%rax>)
 	ret
 EPILOGUE(nettle_poly1305_digest)
 
-GNU_CET_SECTION()
diff --git a/x86_64/salsa20-core-internal.asm b/x86_64/salsa20-core-internal.asm
index c1690880..4ef07be0 100644
--- a/x86_64/salsa20-core-internal.asm
+++ b/x86_64/salsa20-core-internal.asm
@@ -109,4 +109,3 @@ PROLOGUE(_nettle_salsa20_core)
 	W64_EXIT(3, 9)
 	ret
 EPILOGUE(_nettle_salsa20_core)
-GNU_CET_SECTION()
diff --git a/x86_64/salsa20-crypt.asm b/x86_64/salsa20-crypt.asm
index e0348956..cc1d58ca 100644
--- a/x86_64/salsa20-crypt.asm
+++ b/x86_64/salsa20-crypt.asm
@@ -245,4 +245,3 @@ PROLOGUE(nettle_salsa20_crypt)
 	ret
 
 EPILOGUE(nettle_salsa20_crypt)
-GNU_CET_SECTION()
diff --git a/x86_64/serpent-decrypt.asm b/x86_64/serpent-decrypt.asm
index 5f03fa4b..031c41c8 100644
--- a/x86_64/serpent-decrypt.asm
+++ b/x86_64/serpent-decrypt.asm
@@ -714,4 +714,3 @@ PROLOGUE(nettle_serpent_decrypt)
 	W64_EXIT(4, 13)
 	ret
 EPILOGUE(nettle_serpent_decrypt)
-GNU_CET_SECTION()
diff --git a/x86_64/serpent-encrypt.asm b/x86_64/serpent-encrypt.asm
index 6bee5d18..99cba00c 100644
--- a/x86_64/serpent-encrypt.asm
+++ b/x86_64/serpent-encrypt.asm
@@ -749,4 +749,3 @@ C parallell.
 	W64_EXIT(4, 13)
 	ret
 EPILOGUE(nettle_serpent_encrypt)
-GNU_CET_SECTION()
diff --git a/x86_64/sha1-compress.asm b/x86_64/sha1-compress.asm
index 54dfa313..dd48de0e 100644
--- a/x86_64/sha1-compress.asm
+++ b/x86_64/sha1-compress.asm
@@ -305,4 +305,3 @@ PROLOGUE(nettle_sha1_compress)
 	W64_EXIT(2, 0)
 	ret
 EPILOGUE(nettle_sha1_compress)
-GNU_CET_SECTION()
diff --git a/x86_64/sha256-compress.asm b/x86_64/sha256-compress.asm
index 8dbccc5b..5b7d0dcd 100644
--- a/x86_64/sha256-compress.asm
+++ b/x86_64/sha256-compress.asm
@@ -208,4 +208,3 @@ PROLOGUE(_nettle_sha256_compress)
 	W64_EXIT(3, 0)
 	ret
 EPILOGUE(_nettle_sha256_compress)
-GNU_CET_SECTION()
diff --git a/x86_64/sha3-permute.asm b/x86_64/sha3-permute.asm
index a4d0cf0b..805b59af 100644
--- a/x86_64/sha3-permute.asm
+++ b/x86_64/sha3-permute.asm
@@ -107,7 +107,6 @@ define(<ROTL64>, <
 	
 	C sha3_permute(struct sha3_state *ctx)
 	.text
-GNU_CET_SECTION()
 	ALIGN(16)
 PROLOGUE(nettle_sha3_permute)
 	W64_ENTRY(1, 16)
diff --git a/x86_64/sha512-compress.asm b/x86_64/sha512-compress.asm
index 37563e93..4ff1f32a 100644
--- a/x86_64/sha512-compress.asm
+++ b/x86_64/sha512-compress.asm
@@ -208,4 +208,3 @@ PROLOGUE(_nettle_sha512_compress)
 	W64_EXIT(3, 0)
 	ret
 EPILOGUE(_nettle_sha512_compress)
-GNU_CET_SECTION()
diff --git a/x86_64/sha_ni/sha1-compress.asm b/x86_64/sha_ni/sha1-compress.asm
index 3cbca5e2..ab848fdd 100644
--- a/x86_64/sha_ni/sha1-compress.asm
+++ b/x86_64/sha_ni/sha1-compress.asm
@@ -146,4 +146,3 @@ PROLOGUE(nettle_sha1_compress)
 	W64_EXIT(2, 10)
 	ret
 EPILOGUE(nettle_sha1_compress)
-GNU_CET_SECTION()
diff --git a/x86_64/sha_ni/sha256-compress.asm b/x86_64/sha_ni/sha256-compress.asm
index f9fe3757..f2a4bd32 100644
--- a/x86_64/sha_ni/sha256-compress.asm
+++ b/x86_64/sha_ni/sha256-compress.asm
@@ -173,4 +173,3 @@ PROLOGUE(_nettle_sha256_compress)
 	W64_EXIT(3, 10)
 	ret
 EPILOGUE(_nettle_sha256_compress)
-GNU_CET_SECTION()
diff --git a/x86_64/umac-nh-n.asm b/x86_64/umac-nh-n.asm
index 195d5886..ecb6396a 100644
--- a/x86_64/umac-nh-n.asm
+++ b/x86_64/umac-nh-n.asm
@@ -273,4 +273,3 @@ PROLOGUE(_nettle_umac_nh_n)
 	W64_EXIT(5, 14)
 	ret
 EPILOGUE(_nettle_umac_nh_n)
-GNU_CET_SECTION()
diff --git a/x86_64/umac-nh.asm b/x86_64/umac-nh.asm
index 7bfc87ba..a6938e02 100644
--- a/x86_64/umac-nh.asm
+++ b/x86_64/umac-nh.asm
@@ -79,4 +79,3 @@ PROLOGUE(_nettle_umac_nh)
 	W64_EXIT(3, 7)
 	ret
 EPILOGUE(_nettle_umac_nh)
-GNU_CET_SECTION()
-- 
2.20.1

