llvm.org GIT mirror llvm / 4ec907c
[PowerPC] Secure PLT support This patch supports secure PLT mode for PowerPC 32 architecture. Differential Revision: https://reviews.llvm.org/D42112 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@328617 91177308-0d34-0410-b5e6-96231b3b80d8 Strahinja Petrovic 2 years ago
6 changed file(s) with 96 addition(s) and 27 deletion(s). Raw diff Collapse all Expand all
118118 [FeatureBookE]>;
119119 def FeatureE500 : SubtargetFeature<"e500", "IsE500", "true",
120120 "Enable E500/E500mc instructions">;
121 def FeatureSecurePlt : SubtargetFeature<"secure-plt","SecurePlt", "true",
122 "Enable secure plt mode">;
121123 def FeaturePPC4xx : SubtargetFeature<"ppc4xx", "IsPPC4xx", "true",
122124 "Enable PPC 4xx instructions">;
123125 def FeaturePPC6xx : SubtargetFeature<"ppc6xx", "IsPPC6xx", "true",
562562 // Transform %rd = UpdateGBR(%rt, %ri)
563563 // Into: lwz %rt, .L0$poff - .L0$pb(%ri)
564564 // add %rd, %rt, %ri
565 // or into (if secure plt mode is on):
566 // addis r30, r30, .LTOC - .L0$pb@ha
567 // addi r30, r30, .LTOC - .L0$pb@l
565568 // Get the offset from the GOT Base Register to the GOT
566569 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, isDarwin);
567 MCSymbol *PICOffset =
568 MF->getInfo()->getPICOffsetSymbol();
569 TmpInst.setOpcode(PPC::LWZ);
570 const MCExpr *Exp =
571 MCSymbolRefExpr::create(PICOffset, MCSymbolRefExpr::VK_None, OutContext);
572 const MCExpr *PB =
573 MCSymbolRefExpr::create(MF->getPICBaseSymbol(),
574 MCSymbolRefExpr::VK_None,
575 OutContext);
576 const MCOperand TR = TmpInst.getOperand(1);
577 const MCOperand PICR = TmpInst.getOperand(0);
578
579 // Step 1: lwz %rt, .L$poff - .L$pb(%ri)
580 TmpInst.getOperand(1) =
581 MCOperand::createExpr(MCBinaryExpr::createSub(Exp, PB, OutContext));
582 TmpInst.getOperand(0) = TR;
583 TmpInst.getOperand(2) = PICR;
584 EmitToStreamer(*OutStreamer, TmpInst);
585
586 TmpInst.setOpcode(PPC::ADD4);
587 TmpInst.getOperand(0) = PICR;
588 TmpInst.getOperand(1) = TR;
589 TmpInst.getOperand(2) = PICR;
590 EmitToStreamer(*OutStreamer, TmpInst);
591 return;
570 if (Subtarget->isSecurePlt() && isPositionIndependent() ) {
571 unsigned PICR = TmpInst.getOperand(0).getReg();
572 MCSymbol *LTOCSymbol = OutContext.getOrCreateSymbol(StringRef(".LTOC"));
573 const MCExpr *PB =
574 MCSymbolRefExpr::create(MF->getPICBaseSymbol(),
575 OutContext);
576
577 const MCExpr *LTOCDeltaExpr =
578 MCBinaryExpr::createSub(MCSymbolRefExpr::create(LTOCSymbol, OutContext),
579 PB, OutContext);
580
581 const MCExpr *LTOCDeltaHi =
582 PPCMCExpr::createHa(LTOCDeltaExpr, false, OutContext);
583 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADDIS)
584 .addReg(PICR)
585 .addReg(PICR)
586 .addExpr(LTOCDeltaHi));
587
588 const MCExpr *LTOCDeltaLo =
589 PPCMCExpr::createLo(LTOCDeltaExpr, false, OutContext);
590 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADDI)
591 .addReg(PICR)
592 .addReg(PICR)
593 .addExpr(LTOCDeltaLo));
594 return;
595 } else {
596 MCSymbol *PICOffset =
597 MF->getInfo()->getPICOffsetSymbol();
598 TmpInst.setOpcode(PPC::LWZ);
599 const MCExpr *Exp =
600 MCSymbolRefExpr::create(PICOffset, MCSymbolRefExpr::VK_None, OutContext);
601 const MCExpr *PB =
602 MCSymbolRefExpr::create(MF->getPICBaseSymbol(),
603 MCSymbolRefExpr::VK_None,
604 OutContext);
605 const MCOperand TR = TmpInst.getOperand(1);
606 const MCOperand PICR = TmpInst.getOperand(0);
607
608 // Step 1: lwz %rt, .L$poff - .L$pb(%ri)
609 TmpInst.getOperand(1) =
610 MCOperand::createExpr(MCBinaryExpr::createSub(Exp, PB, OutContext));
611 TmpInst.getOperand(0) = TR;
612 TmpInst.getOperand(2) = PICR;
613 EmitToStreamer(*OutStreamer, TmpInst);
614
615 TmpInst.setOpcode(PPC::ADD4);
616 TmpInst.getOperand(0) = PICR;
617 TmpInst.getOperand(1) = TR;
618 TmpInst.getOperand(2) = PICR;
619 EmitToStreamer(*OutStreamer, TmpInst);
620 return;
621 }
592622 }
593623 case PPC::LWZtoc: {
594624 // Transform %r3 = LWZtoc @min1, %r2
12321262
12331263 if (!Subtarget->isPPC64()) {
12341264 const PPCFunctionInfo *PPCFI = MF->getInfo();
1235 if (PPCFI->usesPICBase()) {
1265 if (PPCFI->usesPICBase() && !Subtarget->isSecurePlt()) {
12361266 MCSymbol *RelocSymbol = PPCFI->getPICOffsetSymbol();
12371267 MCSymbol *PICBase = MF->getPICBaseSymbol();
12381268 OutStreamer->EmitLabel(RelocSymbol);
39983998 case ISD::SETCC:
39993999 if (trySETCC(N))
40004000 return;
4001 break;
4002
4003 case PPCISD::CALL: {
4004 const Module *M = MF->getFunction().getParent();
4005
4006 if (PPCLowering->getPointerTy(CurDAG->getDataLayout()) != MVT::i32 ||
4007 !PPCSubTarget->isSecurePlt() || !PPCSubTarget->isTargetELF() ||
4008 M->getPICLevel() == PICLevel::SmallPIC)
4009 break;
4010
4011 SDValue Op = N->getOperand(1);
4012
4013 if (GlobalAddressSDNode *GA = dyn_cast(Op)) {
4014 if (GA->getTargetFlags() == PPCII::MO_PLT)
4015 getGlobalBaseReg();
4016 }
4017 else if (ExternalSymbolSDNode *ES = dyn_cast(Op)) {
4018 if (ES->getTargetFlags() == PPCII::MO_PLT)
4019 getGlobalBaseReg();
4020 }
4021 }
40014022 break;
40024023
40034024 case PPCISD::GlobalBaseReg:
106106 break;
107107 }
108108
109 if (MO.getTargetFlags() == PPCII::MO_PLT)
109 if (MO.getTargetFlags() == PPCII::MO_PLT)
110110 RefKind = MCSymbolRefExpr::VK_PLT;
111111
112 const MachineFunction *MF = MO.getParent()->getParent()->getParent();
113 const PPCSubtarget *Subtarget = &(MF->getSubtarget());
114 const TargetMachine &TM = Printer.TM;
112115 const MCExpr *Expr = MCSymbolRefExpr::create(Symbol, RefKind, Ctx);
116 // -msecure-plt option works only in PIC mode. If secure plt mode
117 // is on add 32768 to symbol.
118 if (Subtarget->isSecurePlt() && TM.isPositionIndependent() &&
119 MO.getTargetFlags() == PPCII::MO_PLT)
120 Expr = MCBinaryExpr::createAdd(Expr,
121 MCConstantExpr::create(32768, Ctx),
122 Ctx);
113123
114124 if (!MO.isJTI() && MO.getOffset())
115125 Expr = MCBinaryExpr::createAdd(Expr,
132132 bool HasFloat128;
133133 bool IsISA3_0;
134134 bool UseLongCalls;
135 bool SecurePlt;
135136
136137 POPCNTDKind HasPOPCNTD;
137138
254255 bool hasOnlyMSYNC() const { return HasOnlyMSYNC; }
255256 bool isPPC4xx() const { return IsPPC4xx; }
256257 bool isPPC6xx() const { return IsPPC6xx; }
258 bool isSecurePlt() const {return SecurePlt; }
257259 bool isE500() const { return IsE500; }
258260 bool isFeatureMFTB() const { return FeatureMFTB; }
259261 bool isDeprecatedDST() const { return DeprecatedDST; }
0 ; RUN: llc < %s -mtriple=powerpc-unknown-linux-gnu -relocation-model=pic | FileCheck -check-prefix=LARGE-BSS %s
1 ; RUN: llc < %s -mtriple=powerpc-unknown-linux-gnu -mattr=+secure-plt -relocation-model=pic | FileCheck -check-prefix=LARGE-SECUREPLT %s
12 @bar = common global i32 0, align 4
23
34 declare i32 @call_foo(i32, ...)
2829 ; LARGE-BSS: [[VREF]]:
2930 ; LARGE-BSS-NEXT: .p2align 2
3031 ; LARGE-BSS-NEXT: .long bar
32 ; LARGE-SECUREPLT: addis 30, 30, .LTOC-.L0$pb@ha
33 ; LARGE-SECUREPLT: addi 30, 30, .LTOC-.L0$pb@l
34 ; LARGE-SECUREPLT: bl call_foo@PLT+32768