1 //===-- MipsNaClELFStreamer.cpp - ELF Object Output for Mips NaCl ---------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements MCELFStreamer for Mips NaCl. It emits .o object files
11 // as required by NaCl's SFI sandbox. It inserts address-masking instructions
12 // before dangerous control-flow and memory access instructions. It inserts
13 // address-masking instructions after instructions that change the stack
14 // pointer. It ensures that the mask and the dangerous instruction are always
15 // emitted in the same bundle. It aligns call + branch delay to the bundle end,
16 // so that return address is always aligned to the start of next bundle.
18 //===----------------------------------------------------------------------===//
21 #include "MipsELFStreamer.h"
22 #include "MipsMCNaCl.h"
23 #include "llvm/MC/MCELFStreamer.h"
27 #define DEBUG_TYPE "mips-mc-nacl"
31 const unsigned IndirectBranchMaskReg = Mips::T6;
32 const unsigned LoadStoreStackMaskReg = Mips::T7;
34 /// Extend the generic MCELFStreamer class so that it can mask dangerous
37 class MipsNaClELFStreamer : public MipsELFStreamer {
39 MipsNaClELFStreamer(MCContext &Context, MCAsmBackend &TAB, raw_ostream &OS,
40 MCCodeEmitter *Emitter, const MCSubtargetInfo &STI)
41 : MipsELFStreamer(Context, TAB, OS, Emitter, STI), PendingCall(false) {}
43 ~MipsNaClELFStreamer() {}
46 // Whether we started the sandboxing sequence for calls. Calls are bundled
47 // with branch delays and aligned to the bundle end.
50 bool isIndirectJump(const MCInst &MI) {
51 return MI.getOpcode() == Mips::JR || MI.getOpcode() == Mips::RET;
54 bool isStackPointerFirstOperand(const MCInst &MI) {
55 return (MI.getNumOperands() > 0 && MI.getOperand(0).isReg()
56 && MI.getOperand(0).getReg() == Mips::SP);
59 bool isCall(unsigned Opcode, bool *IsIndirectCall) {
60 *IsIndirectCall = false;
74 *IsIndirectCall = true;
79 void emitMask(unsigned AddrReg, unsigned MaskReg,
80 const MCSubtargetInfo &STI) {
82 MaskInst.setOpcode(Mips::AND);
83 MaskInst.addOperand(MCOperand::CreateReg(AddrReg));
84 MaskInst.addOperand(MCOperand::CreateReg(AddrReg));
85 MaskInst.addOperand(MCOperand::CreateReg(MaskReg));
86 MipsELFStreamer::EmitInstruction(MaskInst, STI);
89 // Sandbox indirect branch or return instruction by inserting mask operation
91 void sandboxIndirectJump(const MCInst &MI, const MCSubtargetInfo &STI) {
92 unsigned AddrReg = MI.getOperand(0).getReg();
94 EmitBundleLock(false);
95 emitMask(AddrReg, IndirectBranchMaskReg, STI);
96 MipsELFStreamer::EmitInstruction(MI, STI);
100 // Sandbox memory access or SP change. Insert mask operation before and/or
101 // after the instruction.
102 void sandboxLoadStoreStackChange(const MCInst &MI, unsigned AddrIdx,
103 const MCSubtargetInfo &STI, bool MaskBefore,
105 EmitBundleLock(false);
107 // Sandbox memory access.
108 unsigned BaseReg = MI.getOperand(AddrIdx).getReg();
109 emitMask(BaseReg, LoadStoreStackMaskReg, STI);
111 MipsELFStreamer::EmitInstruction(MI, STI);
113 // Sandbox SP change.
114 unsigned SPReg = MI.getOperand(0).getReg();
115 assert((Mips::SP == SPReg) && "Unexpected stack-pointer register.");
116 emitMask(SPReg, LoadStoreStackMaskReg, STI);
122 /// This function is the one used to emit instruction data into the ELF
123 /// streamer. We override it to mask dangerous instructions.
124 void EmitInstruction(const MCInst &Inst,
125 const MCSubtargetInfo &STI) override {
126 // Sandbox indirect jumps.
127 if (isIndirectJump(Inst)) {
129 report_fatal_error("Dangerous instruction in branch delay slot!");
130 sandboxIndirectJump(Inst, STI);
134 // Sandbox loads, stores and SP changes.
137 bool IsMemAccess = isBasePlusOffsetMemoryAccess(Inst.getOpcode(), &AddrIdx,
139 bool IsSPFirstOperand = isStackPointerFirstOperand(Inst);
140 if (IsMemAccess || IsSPFirstOperand) {
141 bool MaskBefore = (IsMemAccess
142 && baseRegNeedsLoadStoreMask(Inst.getOperand(AddrIdx)
144 bool MaskAfter = IsSPFirstOperand && !IsStore;
145 if (MaskBefore || MaskAfter) {
147 report_fatal_error("Dangerous instruction in branch delay slot!");
148 sandboxLoadStoreStackChange(Inst, AddrIdx, STI, MaskBefore, MaskAfter);
154 // Sandbox calls by aligning call and branch delay to the bundle end.
155 // For indirect calls, emit the mask before the call.
157 if (isCall(Inst.getOpcode(), &IsIndirectCall)) {
159 report_fatal_error("Dangerous instruction in branch delay slot!");
161 // Start the sandboxing sequence by emitting call.
162 EmitBundleLock(true);
163 if (IsIndirectCall) {
164 unsigned TargetReg = Inst.getOperand(1).getReg();
165 emitMask(TargetReg, IndirectBranchMaskReg, STI);
167 MipsELFStreamer::EmitInstruction(Inst, STI);
172 // Finish the sandboxing sequence by emitting branch delay.
173 MipsELFStreamer::EmitInstruction(Inst, STI);
179 // None of the sandboxing applies, just emit the instruction.
180 MipsELFStreamer::EmitInstruction(Inst, STI);
184 } // end anonymous namespace
188 bool isBasePlusOffsetMemoryAccess(unsigned Opcode, unsigned *AddrIdx,
197 // Load instructions with base address register in position 1.
212 // Store instructions with base address register in position 1.
225 // Store instructions with base address register in position 2.
235 bool baseRegNeedsLoadStoreMask(unsigned Reg) {
236 // The contents of SP and thread pointer register do not require masking.
237 return Reg != Mips::SP && Reg != Mips::T8;
240 MCELFStreamer *createMipsNaClELFStreamer(MCContext &Context, MCAsmBackend &TAB,
242 MCCodeEmitter *Emitter,
243 const MCSubtargetInfo &STI,
244 bool RelaxAll, bool NoExecStack) {
245 MipsNaClELFStreamer *S = new MipsNaClELFStreamer(Context, TAB, OS, Emitter,
248 S->getAssembler().setRelaxAll(true);
250 S->getAssembler().setNoExecStack(true);
252 // Set bundle-alignment as required by the NaCl ABI for the target.
253 S->EmitBundleAlignMode(MIPS_NACL_BUNDLE_ALIGN);