diff options
| author | Alexander Ivchenko <alexander.ivchenko@intel.com> | 2018-06-04 21:07:35 +0000 |
|---|---|---|
| committer | Alexander Ivchenko <alexander.ivchenko@intel.com> | 2018-06-04 21:07:35 +0000 |
| commit | 2f038c4094d25597f01bfcffc3d7d9761f7aa3d4 (patch) | |
| tree | ffab882e66869a629196b859746206c76efb5de2 /llvm | |
| parent | 87f1a240ba60563bc54d33bf2a16c0af391ba00c (diff) | |
| download | bcm5719-llvm-2f038c4094d25597f01bfcffc3d7d9761f7aa3d4.tar.gz bcm5719-llvm-2f038c4094d25597f01bfcffc3d7d9761f7aa3d4.zip | |
[X86][ELF][CET] Adding the .note.gnu.property ELF section in X86
In preparation for the proposed linker ABI changes
(https://github.com/hjl-tools/linux-abi/wiki/linux-abi-draft.pdf,
https://github.com/hjl-tools/x86-psABI/wiki/x86-64-psABI-cet.pdf),
this patch enables emission of the .note.gnu.property section to
ELF object files when building CET-enabled modules.
patch by mike.dvoretsky
Differential Revision: https://reviews.llvm.org/D47145
llvm-svn: 333951
Diffstat (limited to 'llvm')
| -rw-r--r-- | llvm/lib/Target/X86/X86AsmPrinter.cpp | 38 | ||||
| -rw-r--r-- | llvm/test/CodeGen/X86/note-cet-property.ll | 32 |
2 files changed, 70 insertions, 0 deletions
diff --git a/llvm/lib/Target/X86/X86AsmPrinter.cpp b/llvm/lib/Target/X86/X86AsmPrinter.cpp index be361d5f93b..b241af43b9c 100644 --- a/llvm/lib/Target/X86/X86AsmPrinter.cpp +++ b/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" @@ -30,6 +31,7 @@ #include "llvm/MC/MCContext.h" #include "llvm/MC/MCExpr.h" #include "llvm/MC/MCSectionCOFF.h" +#include "llvm/MC/MCSectionELF.h" #include "llvm/MC/MCSectionMachO.h" #include "llvm/MC/MCStreamer.h" #include "llvm/MC/MCSymbol.h" @@ -539,6 +541,42 @@ bool X86AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, void X86AsmPrinter::EmitStartOfAsmFile(Module &M) { const Triple &TT = TM.getTargetTriple(); + if (TT.isOSBinFormatELF()) { + // Assemble feature flags that may require creation of a note section. + unsigned 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 = MMI->getContext().getELFSection( + ".note.gnu.property", ELF::SHT_NOTE, ELF::SHF_ALLOC); + OutStreamer->SwitchSection(Nt); + + // Emitting note header. + int WordSize = TT.isArch64Bit() ? 8 : 4; + 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()); diff --git a/llvm/test/CodeGen/X86/note-cet-property.ll b/llvm/test/CodeGen/X86/note-cet-property.ll new file mode 100644 index 00000000000..2b7dbbeb38c --- /dev/null +++ b/llvm/test/CodeGen/X86/note-cet-property.ll @@ -0,0 +1,32 @@ +; RUN: llc -mtriple i686-pc-linux < %s | FileCheck %s --check-prefix=X86 +; RUN: llc -mtriple x86_64-pc-linux < %s | FileCheck %s --check-prefix=X86_64 + +; This test checks that the compiler emits a .note.gnu.property section for +; modules with "cf-protection" module flags. + +; X86: .section .note.gnu.property,"a",@note +; X86-NEXT: .p2align 2 +; X86-NEXT: .long 4 +; X86-NEXT: .long 12 +; X86-NEXT: .long 5 +; X86-NEXT: .asciz "GNU" +; X86-NEXT: .long 3221225474 +; X86-NEXT: .long 4 +; X86-NEXT: .long 3 +; X86-NEXT: .p2align 2 + +; X86_64: .section .note.gnu.property,"a",@note +; X86_64-NEXT: .p2align 3 +; X86_64-NEXT: .long 4 +; X86_64-NEXT: .long 16 +; X86_64-NEXT: .long 5 +; X86_64-NEXT: .asciz "GNU" +; X86_64-NEXT: .long 3221225474 +; X86_64-NEXT: .long 8 +; X86_64-NEXT: .quad 3 +; X86_64-NEXT: .p2align 3 + +!llvm.module.flags = !{!0, !1} + +!0 = !{i32 4, !"cf-protection-return", i32 1} +!1 = !{i32 4, !"cf-protection-branch", i32 1} |

