|
19 | 19 | #include "X86InstrInfo.h"
|
20 | 20 | #include "X86MachineFunctionInfo.h"
|
21 | 21 | #include "llvm/BinaryFormat/COFF.h"
|
| 22 | +#include "llvm/BinaryFormat/ELF.h" |
22 | 23 | #include "llvm/CodeGen/MachineConstantPool.h"
|
23 | 24 | #include "llvm/CodeGen/MachineModuleInfoImpls.h"
|
24 | 25 | #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
|
|
30 | 31 | #include "llvm/MC/MCContext.h"
|
31 | 32 | #include "llvm/MC/MCExpr.h"
|
32 | 33 | #include "llvm/MC/MCSectionCOFF.h"
|
| 34 | +#include "llvm/MC/MCSectionELF.h" |
33 | 35 | #include "llvm/MC/MCSectionMachO.h"
|
34 | 36 | #include "llvm/MC/MCStreamer.h"
|
35 | 37 | #include "llvm/MC/MCSymbol.h"
|
@@ -539,6 +541,42 @@ bool X86AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
|
539 | 541 | void X86AsmPrinter::EmitStartOfAsmFile(Module &M) {
|
540 | 542 | const Triple &TT = TM.getTargetTriple();
|
541 | 543 |
|
| 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 | + |
542 | 580 | if (TT.isOSBinFormatMachO())
|
543 | 581 | OutStreamer->SwitchSection(getObjFileLowering().getTextSection());
|
544 | 582 |
|
|
0 commit comments