33 #define DEBUG_TYPE "mips-mc-nacl"
37 const unsigned IndirectBranchMaskReg = Mips::T6;
38 const unsigned LoadStoreStackMaskReg = Mips::T7;
46 std::unique_ptr<MCObjectWriter> OW,
47 std::unique_ptr<MCCodeEmitter> Emitter)
51 ~MipsNaClELFStreamer()
override =
default;
56 bool PendingCall =
false;
58 bool isIndirectJump(
const MCInst &
MI) {
59 if (
MI.getOpcode() == Mips::JALR) {
63 return MI.getOperand(0).getReg() == Mips::ZERO;
65 return MI.getOpcode() == Mips::JR;
68 bool isStackPointerFirstOperand(
const MCInst &
MI) {
69 return (
MI.getNumOperands() > 0 &&
MI.getOperand(0).isReg()
70 &&
MI.getOperand(0).getReg() == Mips::SP);
73 bool isCall(
const MCInst &
MI,
bool *IsIndirectCall) {
74 unsigned Opcode =
MI.getOpcode();
76 *IsIndirectCall =
false;
93 if (
MI.getOperand(0).getReg() == Mips::ZERO)
96 *IsIndirectCall =
true;
101 void emitMask(
unsigned AddrReg,
unsigned MaskReg,
114 unsigned AddrReg =
MI.getOperand(0).getReg();
116 emitBundleLock(
false);
117 emitMask(AddrReg, IndirectBranchMaskReg, STI);
124 void sandboxLoadStoreStackChange(
const MCInst &
MI,
unsigned AddrIdx,
127 emitBundleLock(
false);
130 unsigned BaseReg =
MI.getOperand(AddrIdx).getReg();
131 emitMask(BaseReg, LoadStoreStackMaskReg, STI);
136 unsigned SPReg =
MI.getOperand(0).getReg();
137 assert((Mips::SP == SPReg) &&
"Unexpected stack-pointer register.");
138 emitMask(SPReg, LoadStoreStackMaskReg, STI);
146 void emitInstruction(
const MCInst &Inst,
149 if (isIndirectJump(Inst)) {
152 sandboxIndirectJump(Inst, STI);
157 unsigned AddrIdx = 0;
158 bool IsStore =
false;
161 bool IsSPFirstOperand = isStackPointerFirstOperand(Inst);
162 if (IsMemAccess || IsSPFirstOperand) {
163 bool MaskBefore = (IsMemAccess
166 bool MaskAfter = IsSPFirstOperand && !IsStore;
167 if (MaskBefore || MaskAfter) {
170 sandboxLoadStoreStackChange(Inst, AddrIdx, STI, MaskBefore, MaskAfter);
179 if (isCall(Inst, &IsIndirectCall)) {
184 emitBundleLock(
true);
185 if (IsIndirectCall) {
187 emitMask(TargetReg, IndirectBranchMaskReg, STI);
263 std::unique_ptr<MCAsmBackend> TAB,
264 std::unique_ptr<MCObjectWriter> OW,
265 std::unique_ptr<MCCodeEmitter> Emitter,
267 MipsNaClELFStreamer *
S =
new MipsNaClELFStreamer(
270 S->getAssembler().setRelaxAll(
true);