Blame 0001-Add-cet.h-for-writing-CET-enabled-assembly-code.patch

a552a96
From bcc0c894f38fd8b43af521e356a167b1a12dd497 Mon Sep 17 00:00:00 2001
a552a96
From: Xiang1 Zhang <xiang1.zhang@intel.com>
a552a96
Date: Tue, 19 May 2020 13:29:30 +0800
a552a96
Subject: [PATCH] Add cet.h for writing CET-enabled assembly code
a552a96
a552a96
Summary:
a552a96
Add x86 feature with IBT and/or SHSTK bits to ELF program property if they  are enabled. Otherwise, contents in this header file are unused.
a552a96
This file is mainly design for assembly source code which want to enable CET
a552a96
a552a96
Reviewers: hjl.tools, annita.zhang, LuoYuanke, craig.topper, tstellar, pengfei, rsmith
a552a96
a552a96
Reviewed By: LuoYuanke
a552a96
a552a96
Subscribers: cfe-commits, mgorny
a552a96
a552a96
Tags: #clang
a552a96
a552a96
Differential Revision: https://reviews.llvm.org/D79617
a552a96
---
a552a96
 clang/lib/Headers/CMakeLists.txt |  1 +
a552a96
 clang/lib/Headers/cet.h          | 66 ++++++++++++++++++++++++++++++++++++++++
a552a96
 clang/test/CodeGen/asm-cet.S     | 27 ++++++++++++++++
a552a96
 3 files changed, 94 insertions(+)
a552a96
 create mode 100644 clang/lib/Headers/cet.h
a552a96
 create mode 100644 clang/test/CodeGen/asm-cet.S
a552a96
a552a96
diff --git a/clang/lib/Headers/CMakeLists.txt b/clang/lib/Headers/CMakeLists.txt
a552a96
index 60d359f..c5215ee 100644
a552a96
--- a/clang/lib/Headers/CMakeLists.txt
a552a96
+++ b/clang/lib/Headers/CMakeLists.txt
a552a96
@@ -46,6 +46,7 @@ set(files
a552a96
   __clang_cuda_math_forward_declares.h
a552a96
   __clang_cuda_runtime_wrapper.h
a552a96
   cetintrin.h
a552a96
+  cet.h
a552a96
   cldemoteintrin.h
a552a96
   clzerointrin.h
a552a96
   cpuid.h
a552a96
diff --git a/clang/lib/Headers/cet.h b/clang/lib/Headers/cet.h
a552a96
new file mode 100644
a552a96
index 0000000..ffb19de
a552a96
--- /dev/null
a552a96
+++ b/clang/lib/Headers/cet.h
a552a96
@@ -0,0 +1,66 @@
a552a96
+/*===------ cet.h -Control-flow Enforcement Technology  feature ------------===
a552a96
+ * Add x86 feature with IBT and/or SHSTK bits to ELF program property if they
a552a96
+ * are enabled. Otherwise, contents in this header file are unused. This file
a552a96
+ * is mainly design for assembly source code which want to enable CET.
a552a96
+ *
a552a96
+ * Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
a552a96
+ * See https://llvm.org/LICENSE.txt for license information.
a552a96
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
a552a96
+ *
a552a96
+ *===-----------------------------------------------------------------------===
a552a96
+ */
a552a96
+#ifndef __CET_H
a552a96
+#define __CET_H
a552a96
+
a552a96
+#ifdef __ASSEMBLER__
a552a96
+
a552a96
+#ifndef __CET__
a552a96
+# define _CET_ENDBR
a552a96
+#endif
a552a96
+
a552a96
+#ifdef __CET__
a552a96
+
a552a96
+# ifdef __LP64__
a552a96
+#  if __CET__ & 0x1
a552a96
+#    define _CET_ENDBR endbr64
a552a96
+#  else
a552a96
+#    define _CET_ENDBR
a552a96
+#  endif
a552a96
+# else
a552a96
+#  if __CET__ & 0x1
a552a96
+#    define _CET_ENDBR endbr32
a552a96
+#  else
a552a96
+#    define _CET_ENDBR
a552a96
+#  endif
a552a96
+# endif
a552a96
+
a552a96
+
a552a96
+#  ifdef __LP64__
a552a96
+#   define __PROPERTY_ALIGN 3
a552a96
+#  else
a552a96
+#   define __PROPERTY_ALIGN 2
a552a96
+#  endif
a552a96
+
a552a96
+	.pushsection ".note.gnu.property", "a"
a552a96
+	.p2align __PROPERTY_ALIGN
a552a96
+	.long 1f - 0f		/* name length.  */
a552a96
+	.long 4f - 1f		/* data length.  */
a552a96
+	/* NT_GNU_PROPERTY_TYPE_0.   */
a552a96
+	.long 5			/* note type.  */
a552a96
+0:
a552a96
+	.asciz "GNU"		/* vendor name.  */
a552a96
+1:
a552a96
+	.p2align __PROPERTY_ALIGN
a552a96
+	/* GNU_PROPERTY_X86_FEATURE_1_AND.  */
a552a96
+	.long 0xc0000002	/* pr_type.  */
a552a96
+	.long 3f - 2f		/* pr_datasz.  */
a552a96
+2:
a552a96
+	/* GNU_PROPERTY_X86_FEATURE_1_XXX.  */
a552a96
+	.long __CET__
a552a96
+3:
a552a96
+	.p2align __PROPERTY_ALIGN
a552a96
+4:
a552a96
+	.popsection
a552a96
+#endif
a552a96
+#endif
a552a96
+#endif
a552a96
diff --git a/clang/test/CodeGen/asm-cet.S b/clang/test/CodeGen/asm-cet.S
a552a96
new file mode 100644
a552a96
index 0000000..3644ed7
a552a96
--- /dev/null
a552a96
+++ b/clang/test/CodeGen/asm-cet.S
a552a96
@@ -0,0 +1,27 @@
a552a96
+// REQUIRES: x86-registered-target
a552a96
+// RUN: %clang --target=x86_64-pc-linux -fcf-protection  -include cet.h -c %s -o - | llvm-readelf -n | FileCheck %s
a552a96
+// RUN: %clang --target=x86_64-pc-linux -include cet.h -c %s -o - | llvm-readelf -S | FileCheck %s --check-prefixes=NOCET
a552a96
+// RUN: %clang --target=x86_64-pc-linux -include cet.h -S %s -o - | FileCheck %s --check-prefixes=NOENDBR
a552a96
+// RUN: %clang --target=x86_64-pc-linux -fcf-protection  -include cet.h -S %s -o - | FileCheck %s --check-prefixes=ENDBR64
a552a96
+
a552a96
+// RUN: %clang --target=i386-pc-linux -fcf-protection  -include cet.h -c %s -o - | llvm-readelf -n | FileCheck %s
a552a96
+// RUN: %clang --target=i386-pc-linux -include cet.h -c %s -o - | llvm-readelf -S | FileCheck %s --check-prefixes=NOCET
a552a96
+// RUN: %clang --target=i386-pc-linux -include cet.h -S %s -o - | FileCheck %s --check-prefixes=NOENDBR
a552a96
+// RUN: %clang --target=i386-pc-linux -fcf-protection  -include cet.h -S %s -o - | FileCheck %s --check-prefixes=ENDBR32
a552a96
+
a552a96
+// CHECK: IBT, SHSTK
a552a96
+
a552a96
+// NOCET:     Section Headers
a552a96
+// NOCET-NOT: .note.gnu.property
a552a96
+
a552a96
+// NOENDBR:   foo
a552a96
+// NOENDBR-NOT: endbr
a552a96
+
a552a96
+// ENDBR64: endbr64
a552a96
+// ENDBR32: endbr32
a552a96
+        .text
a552a96
+        .globl  foo
a552a96
+        .type   foo, @function
a552a96
+foo:
a552a96
+        _CET_ENDBR
a552a96
+        ret
a552a96
-- 
a552a96
1.8.3.1
a552a96