Skip to content

Commit 2f038c4

Browse files
author
Alexander Ivchenko
committed
[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
1 parent 87f1a24 commit 2f038c4

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

llvm/lib/Target/X86/X86AsmPrinter.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "X86InstrInfo.h"
2020
#include "X86MachineFunctionInfo.h"
2121
#include "llvm/BinaryFormat/COFF.h"
22+
#include "llvm/BinaryFormat/ELF.h"
2223
#include "llvm/CodeGen/MachineConstantPool.h"
2324
#include "llvm/CodeGen/MachineModuleInfoImpls.h"
2425
#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
@@ -30,6 +31,7 @@
3031
#include "llvm/MC/MCContext.h"
3132
#include "llvm/MC/MCExpr.h"
3233
#include "llvm/MC/MCSectionCOFF.h"
34+
#include "llvm/MC/MCSectionELF.h"
3335
#include "llvm/MC/MCSectionMachO.h"
3436
#include "llvm/MC/MCStreamer.h"
3537
#include "llvm/MC/MCSymbol.h"
@@ -539,6 +541,42 @@ bool X86AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
539541
void X86AsmPrinter::EmitStartOfAsmFile(Module &M) {
540542
const Triple &TT = TM.getTargetTriple();
541543

544+
if (TT.isOSBinFormatELF()) {
545+
// Assemble feature flags that may require creation of a note section.
546+
unsigned FeatureFlagsAnd = 0;
547+
if (M.getModuleFlag("cf-protection-branch"))
548+
FeatureFlagsAnd |= ELF::GNU_PROPERTY_X86_FEATURE_1_IBT;
549+
if (M.getModuleFlag("cf-protection-return"))
550+
FeatureFlagsAnd |= ELF::GNU_PROPERTY_X86_FEATURE_1_SHSTK;
551+
552+
if (FeatureFlagsAnd) {
553+
// Emit a .note.gnu.property section with the flags.
554+
if (!TT.isArch32Bit() && !TT.isArch64Bit())
555+
llvm_unreachable("CFProtection used on invalid architecture!");
556+
MCSection *Cur = OutStreamer->getCurrentSectionOnly();
557+
MCSection *Nt = MMI->getContext().getELFSection(
558+
".note.gnu.property", ELF::SHT_NOTE, ELF::SHF_ALLOC);
559+
OutStreamer->SwitchSection(Nt);
560+
561+
// Emitting note header.
562+
int WordSize = TT.isArch64Bit() ? 8 : 4;
563+
EmitAlignment(WordSize == 4 ? 2 : 3);
564+
OutStreamer->EmitIntValue(4, 4 /*size*/); // data size for "GNU\0"
565+
OutStreamer->EmitIntValue(8 + WordSize, 4 /*size*/); // Elf_Prop size
566+
OutStreamer->EmitIntValue(ELF::NT_GNU_PROPERTY_TYPE_0, 4 /*size*/);
567+
OutStreamer->EmitBytes(StringRef("GNU", 4)); // note name
568+
569+
// Emitting an Elf_Prop for the CET properties.
570+
OutStreamer->EmitIntValue(ELF::GNU_PROPERTY_X86_FEATURE_1_AND, 4);
571+
OutStreamer->EmitIntValue(WordSize, 4); // data size
572+
OutStreamer->EmitIntValue(FeatureFlagsAnd, WordSize); // data
573+
EmitAlignment(WordSize == 4 ? 2 : 3); // padding
574+
575+
OutStreamer->endSection(Nt);
576+
OutStreamer->SwitchSection(Cur);
577+
}
578+
}
579+
542580
if (TT.isOSBinFormatMachO())
543581
OutStreamer->SwitchSection(getObjFileLowering().getTextSection());
544582

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
; RUN: llc -mtriple i686-pc-linux < %s | FileCheck %s --check-prefix=X86
2+
; RUN: llc -mtriple x86_64-pc-linux < %s | FileCheck %s --check-prefix=X86_64
3+
4+
; This test checks that the compiler emits a .note.gnu.property section for
5+
; modules with "cf-protection" module flags.
6+
7+
; X86: .section .note.gnu.property,"a",@note
8+
; X86-NEXT: .p2align 2
9+
; X86-NEXT: .long 4
10+
; X86-NEXT: .long 12
11+
; X86-NEXT: .long 5
12+
; X86-NEXT: .asciz "GNU"
13+
; X86-NEXT: .long 3221225474
14+
; X86-NEXT: .long 4
15+
; X86-NEXT: .long 3
16+
; X86-NEXT: .p2align 2
17+
18+
; X86_64: .section .note.gnu.property,"a",@note
19+
; X86_64-NEXT: .p2align 3
20+
; X86_64-NEXT: .long 4
21+
; X86_64-NEXT: .long 16
22+
; X86_64-NEXT: .long 5
23+
; X86_64-NEXT: .asciz "GNU"
24+
; X86_64-NEXT: .long 3221225474
25+
; X86_64-NEXT: .long 8
26+
; X86_64-NEXT: .quad 3
27+
; X86_64-NEXT: .p2align 3
28+
29+
!llvm.module.flags = !{!0, !1}
30+
31+
!0 = !{i32 4, !"cf-protection-return", i32 1}
32+
!1 = !{i32 4, !"cf-protection-branch", i32 1}

0 commit comments

Comments
 (0)