private void memindex_emit(BinaryWriter writer, Byte r, X86Register32 basereg, Int32 disp, X86Register32 indexreg, Byte shift) { if (basereg == X86Register32.None) { address_byte(writer, 0, (byte)r, 4); address_byte(writer, (byte)shift, (byte)indexreg, 5); writer.Write((Int32)disp); } else if (disp == 0 && basereg == X86Register32.EBP) { address_byte(writer, 0, (byte)r, 4); address_byte(writer, (byte)shift, (byte)indexreg, (byte)basereg); } else if (X86Writer.is_imm8(disp)) { address_byte(writer, 1, (byte)r, 4); address_byte(writer, (byte)shift, (byte)indexreg, (byte)basereg); writer.Write((Byte)disp); } else { address_byte(writer, 2, (byte)r, 4); address_byte(writer, (byte)shift, (byte)indexreg, 5); writer.Write((Int32)disp); } }
private void membase_emit(BinaryWriter writer, Byte r, X86Register32 basereg, Int32 disp) { do { if ((basereg) == X86Register32.ESP) { if ((disp) == 0) { address_byte(writer, 0, (byte)r, (byte)X86Register32.ESP); address_byte(writer, 0, (byte)X86Register32.ESP, (byte)X86Register32.ESP); } else if (X86Writer.is_imm8(disp)) { address_byte(writer, 1, (byte)r, (byte)X86Register32.ESP); address_byte(writer, 0, (byte)X86Register32.ESP, (byte)X86Register32.ESP); writer.Write((byte)(disp)); } else { address_byte(writer, 2, (byte)r, (byte)X86Register32.ESP); address_byte(writer, 0, (byte)X86Register32.ESP, (byte)X86Register32.ESP); writer.Write(disp); } break; } else if (disp == 0 && basereg != X86Register32.EBP) { address_byte(writer, 0, (byte)r, (byte)basereg); } else if (X86Writer.is_imm8(disp)) { address_byte(writer, 1, (byte)r, (byte)basereg); writer.Write((byte)(disp)); } else { address_byte(writer, 2, (byte)r, (byte)basereg); writer.Write(disp); } } while (false); }
internal X86Label(X86Writer writer, IntPtr position) : this(writer) { mark = position; }
internal X86Label(X86Writer writer) { this.writer = writer; }
unsafe static public MemoryPatch Patch(IntPtr target, Action action) { int size = 8; var mr = GetPermissions(target); if (mr != null) { MemoryProtection(target, size, mprot.Read|mprot.Write); var ums = new UnmanagedMemoryStream((byte *)target, 0, size, FileAccess.ReadWrite); byte[] unpatched = new byte[size]; ums.Read(unpatched, 0, size); ums.Seek(0, SeekOrigin.Begin); var writer = new X86Writer(ums, (IntPtr)size); writer.Mov32(X86Register32.EAX, Marshal.GetFunctionPointerForDelegate(action).ToInt32()); writer.Jmp(X86Register32.EAX); ums.Seek(0, SeekOrigin.Begin); byte[] patched = new byte[size]; ums.Read(patched, 0, size); return new MemoryPatch(target, action, patched, unpatched); } else { throw new Exception(); } }