[PATCH] D47145: [X86][ELF][CET] Adding the .note.gnu.property ELF section in X86

Mikhail Dvoretckii via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed May 30 09:23:04 PDT 2018


mike.dvoretsky updated this revision to Diff 149134.
mike.dvoretsky added a comment.

Replaced assertion with a more explicit error at the start of the code path. Changed the emission of the "GNU\0" byte sequence to use EmitBytes with the StringRef constructor specifically to include the trailing zero byte.

The ELF.h file is no longer affected by this patch since https://reviews.llvm.org/D47473 was commited.


https://reviews.llvm.org/D47145

Files:
  llvm/lib/Target/X86/X86AsmPrinter.cpp
  llvm/test/CodeGen/X86/note-cet-property.ll


Index: llvm/test/CodeGen/X86/note-cet-property.ll
===================================================================
--- /dev/null
+++ llvm/test/CodeGen/X86/note-cet-property.ll
@@ -0,0 +1,19 @@
+; RUN: llc -mtriple x86_64-pc-linux < %s | FileCheck %s
+
+; This test checks that the compiler emits a .note.gnu.property section for
+; modules with "cf-protection" module flags.
+
+; CHECK:      .section        .note.gnu.property,"a", at note
+; CHECK-NEXT: .p2align 3
+; CHECK-NEXT: .long    4
+; CHECK-NEXT: .long    16
+; CHECK-NEXT: .long    5
+; CHECK-NEXT: .asciz   "GNU"
+; CHECK-NEXT: .long    3221225474
+; CHECK-NEXT: .long    8
+; CHECK-NEXT: .quad    2
+; CHECK-NEXT: .p2align 3
+
+!llvm.module.flags = !{!0}
+
+!0 = !{i32 4, !"cf-protection-return", i32 1}
Index: llvm/lib/Target/X86/X86AsmPrinter.cpp
===================================================================
--- llvm/lib/Target/X86/X86AsmPrinter.cpp
+++ llvm/lib/Target/X86/X86AsmPrinter.cpp
@@ -19,6 +19,7 @@
 #include "X86InstrInfo.h"
 #include "X86MachineFunctionInfo.h"
 #include "llvm/BinaryFormat/COFF.h"
+#include "llvm/BinaryFormat/ELF.h"
 #include "llvm/CodeGen/MachineConstantPool.h"
 #include "llvm/CodeGen/MachineModuleInfoImpls.h"
 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
@@ -539,6 +540,42 @@
 void X86AsmPrinter::EmitStartOfAsmFile(Module &M) {
   const Triple &TT = TM.getTargetTriple();
 
+  if (TT.isOSBinFormatELF()) {
+    // Assemble feature flags that may require creation of a note section.
+    char FeatureFlagsAnd = 0;
+    if (M.getModuleFlag("cf-protection-branch"))
+      FeatureFlagsAnd |= ELF::GNU_PROPERTY_X86_FEATURE_1_IBT;
+    if (M.getModuleFlag("cf-protection-return"))
+      FeatureFlagsAnd |= ELF::GNU_PROPERTY_X86_FEATURE_1_SHSTK;
+
+    if (FeatureFlagsAnd) {
+      // Emit a .note.gnu.property section with the flags.
+      if (!TT.isArch32Bit() && !TT.isArch64Bit())
+        llvm_unreachable("CFProtection used on invalid architecture!");
+      MCSection *Cur = OutStreamer->getCurrentSectionOnly();
+      MCSection *Nt = (MCSection *)MMI->getContext().getELFSection(
+          ".note.gnu.property", ELF::SHT_NOTE, ELF::SHF_ALLOC);
+      int WordSize = TT.isArch64Bit() ? 8 : 4;
+      OutStreamer->SwitchSection(Nt);
+
+      // Emitting note header.
+      EmitAlignment(WordSize == 4 ? 2 : 3);
+      OutStreamer->EmitIntValue(4, 4 /*size*/); // data size for "GNU\0"
+      OutStreamer->EmitIntValue(8 + WordSize, 4 /*size*/); // Elf_Prop size
+      OutStreamer->EmitIntValue(ELF::NT_GNU_PROPERTY_TYPE_0, 4 /*size*/);
+      OutStreamer->EmitBytes(StringRef("GNU", 4)); // note name
+
+      // Emitting an Elf_Prop for the CET properties.
+      OutStreamer->EmitIntValue(ELF::GNU_PROPERTY_X86_FEATURE_1_AND, 4);
+      OutStreamer->EmitIntValue(WordSize, 4);               // data size
+      OutStreamer->EmitIntValue(FeatureFlagsAnd, WordSize); // data
+      EmitAlignment(WordSize == 4 ? 2 : 3);                 // padding
+
+      OutStreamer->endSection(Nt);
+      OutStreamer->SwitchSection(Cur);
+    }
+  }
+
   if (TT.isOSBinFormatMachO())
     OutStreamer->SwitchSection(getObjFileLowering().getTextSection());
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D47145.149134.patch
Type: text/x-patch
Size: 3170 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180530/eda38c2e/attachment.bin>


More information about the llvm-commits mailing list