Skip to content

Commit

Permalink
Loading of NE images code complete
Browse files Browse the repository at this point in the history
  • Loading branch information
uxmal committed Sep 17, 2015
1 parent f60e9b5 commit f0a9624
Show file tree
Hide file tree
Showing 13 changed files with 332 additions and 35 deletions.
4 changes: 2 additions & 2 deletions src/Arch/X86/OperandRewriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace Reko.Arch.X86
/// </summary>
public abstract class OperandRewriter
{
private readonly IntelArchitecture arch;
protected readonly IntelArchitecture arch;
private readonly Frame frame;
private readonly IRewriterHost host;

Expand Down Expand Up @@ -246,7 +246,7 @@ public OperandRewriter16(IntelArchitecture arch, Frame frame, IRewriterHost host

public override Address ImmediateAsAddress(Address address, ImmediateOperand imm)
{
return Address.SegPtr(address.Selector, imm.Value.ToUInt32());
return this.arch.ProcessorMode.CreateSegmentedAddress(address.Selector, imm.Value.ToUInt32());
}

public override MemoryAccess StackAccess(Expression expr, DataType dt)
Expand Down
36 changes: 29 additions & 7 deletions src/Arch/X86/ProcessorMode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public virtual RegisterStorage StackRegister

public abstract OperandRewriter CreateOperandRewriter(IntelArchitecture arch, Frame frame, IRewriterHost host);

public abstract Address CreateSegmentedAddress(ushort seg, uint offset);

public virtual Expression CreateStackAccess(Frame frame, int offset, DataType dataType)
{
var sp = frame.EnsureRegister(Registers.sp);
Expand All @@ -81,13 +83,13 @@ protected Address ReadSegmentedCodeAddress(int byteSize, ImageReader rdr, Proces
{
if (byteSize == PrimitiveType.Word16.Size)
{
return Address.SegPtr(state.GetRegister(Registers.cs).ToUInt16(), rdr.ReadLeUInt16());
return CreateSegmentedAddress(state.GetRegister(Registers.cs).ToUInt16(), rdr.ReadLeUInt16());
}
else
{
ushort off = rdr.ReadLeUInt16();
ushort seg = rdr.ReadLeUInt16();
return Address.SegPtr(seg, off);
return CreateSegmentedAddress(seg, off);
}
}

Expand All @@ -109,14 +111,19 @@ public override IEnumerable<Address> CreateInstructionScanner(ImageMap map, Imag

public override X86Disassembler CreateDisassembler(ImageReader rdr)
{
return new X86Disassembler(rdr, PrimitiveType.Word16, PrimitiveType.Word16, false);
return new X86Disassembler(this, rdr, PrimitiveType.Word16, PrimitiveType.Word16, false);
}

public override OperandRewriter CreateOperandRewriter(IntelArchitecture arch, Frame frame, IRewriterHost host)
{
return new OperandRewriter16(arch, frame, host);
}

public override Address CreateSegmentedAddress(ushort seg, uint offset)
{
return Address.SegPtr(seg, offset);
}

public override Address MakeAddressFromConstant(Constant c)
{
throw new NotSupportedException("Must pass segment:offset to make a segmented address.");
Expand All @@ -136,7 +143,7 @@ public override bool TryParseAddress(string txtAddress, out Address addr)
{
try
{
addr = Address.SegPtr(
addr = CreateSegmentedAddress(
Convert.ToUInt16(txtAddress.Substring(0, c), 16),
Convert.ToUInt32(txtAddress.Substring(c + 1), 16));
return true;
Expand All @@ -158,7 +165,7 @@ public SegmentedMode()

public override X86Disassembler CreateDisassembler(ImageReader rdr)
{
return new X86Disassembler(rdr, PrimitiveType.Word16, PrimitiveType.Word16, false);
return new X86Disassembler(this, rdr, PrimitiveType.Word16, PrimitiveType.Word16, false);
}

public override IEnumerable<Address> CreateInstructionScanner(ImageMap map, ImageReader rdr, IEnumerable<Address> knownAddresses, PointerScannerFlags flags)
Expand All @@ -171,6 +178,11 @@ public override OperandRewriter CreateOperandRewriter(IntelArchitecture arch, Fr
return new OperandRewriter16(arch, frame, host);
}

public override Address CreateSegmentedAddress(ushort seg, uint offset)
{
return Address.ProtectedSegPtr(seg, offset);
}

public override Address MakeAddressFromConstant(Constant c)
{
throw new NotSupportedException("Must pass segment:offset to make a segmented address.");
Expand Down Expand Up @@ -221,14 +233,19 @@ public override IEnumerable<Address> CreateInstructionScanner(

public override X86Disassembler CreateDisassembler(ImageReader rdr)
{
return new X86Disassembler(rdr, PrimitiveType.Word32, PrimitiveType.Word32, false);
return new X86Disassembler(this, rdr, PrimitiveType.Word32, PrimitiveType.Word32, false);
}

public override OperandRewriter CreateOperandRewriter(IntelArchitecture arch, Frame frame, IRewriterHost host)
{
return new OperandRewriter32(arch, frame, host);
}

public override Address CreateSegmentedAddress(ushort seg, uint offset)
{
throw new NotSupportedException();
}

public override Expression CreateStackAccess(Frame frame, int offset, DataType dataType)
{
var esp = frame.EnsureRegister(Registers.esp);
Expand Down Expand Up @@ -276,14 +293,19 @@ public override IEnumerable<Address> CreateInstructionScanner(ImageMap map, Imag

public override X86Disassembler CreateDisassembler(ImageReader rdr)
{
return new X86Disassembler(rdr, PrimitiveType.Word32, PrimitiveType.Word64, true);
return new X86Disassembler(this, rdr, PrimitiveType.Word32, PrimitiveType.Word64, true);
}

public override OperandRewriter CreateOperandRewriter(IntelArchitecture arch, Frame frame, IRewriterHost host)
{
return new OperandRewriter64(arch, frame, host);
}

public override Address CreateSegmentedAddress(ushort seg, uint offset)
{
throw new NotSupportedException();
}

public override Expression CreateStackAccess(Frame frame, int offset, DataType dataType)
{
var rsp = frame.EnsureRegister(Registers.rsp);
Expand Down
5 changes: 4 additions & 1 deletion src/Arch/X86/X86Disassembler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ namespace Reko.Arch.X86
/// </summary>
public partial class X86Disassembler : DisassemblerBase<IntelInstruction>
{
private ProcessorMode mode;
private IntelInstruction instrCur;
private PrimitiveType dataWidth;
private PrimitiveType addressWidth;
Expand All @@ -55,11 +56,13 @@ public partial class X86Disassembler : DisassemblerBase<IntelInstruction>
/// <param name="width">Default address and data widths. PrimitiveType.Word16 for
/// 16-bit operation, PrimitiveType.Word32 for 32-bit operation.</param>
public X86Disassembler(
ProcessorMode mode,
ImageReader rdr,
PrimitiveType defaultWordSize,
PrimitiveType defaultAddressSize,
bool useRexPrefix)
{
this.mode = mode;
this.rdr = rdr;
this.defaultDataWidth = defaultWordSize;
this.defaultAddressWidth = defaultAddressSize;
Expand Down Expand Up @@ -660,7 +663,7 @@ private IntelInstruction DecodeOperands(Opcode opcode, byte op, string strFormat
++i;
ushort off = rdr.ReadLeUInt16();
ushort seg = rdr.ReadLeUInt16();
pOperand = addrOp = new X86AddressOperand(Address.SegPtr(seg, off));
pOperand = addrOp = new X86AddressOperand(mode.CreateSegmentedAddress(seg, off));
break;
case 'E': // memory or register operand specified by mod & r/m fields.
width = OperandWidth(strFormat[i++]);
Expand Down
2 changes: 1 addition & 1 deletion src/Arch/X86/X86State.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public Address AddressFromSegOffset(RegisterStorage seg, uint offset)
Constant c = GetRegister(seg);
if (c.IsValid)
{
return Address.SegPtr((ushort) c.ToUInt32(), offset & 0xFFFF);
return arch.ProcessorMode.CreateSegmentedAddress((ushort) c.ToUInt32(), offset & 0xFFFF);
}
else
return null;
Expand Down
4 changes: 2 additions & 2 deletions src/Core/Address.cs
Original file line number Diff line number Diff line change
Expand Up @@ -437,12 +437,12 @@ public override ushort ToUInt16()

public override uint ToUInt32()
{
return (((uint)(uSegment & ~7)) << 12) + uOffset;
return (((uint)(uSegment & ~7)) << 9) + uOffset;
}

public override ulong ToLinear()
{
return (((ulong)(uSegment & ~7)) << 12) + uOffset;
return (((ulong)(uSegment & ~7)) << 9) + uOffset;
}

public override string ToString()
Expand Down
2 changes: 2 additions & 0 deletions src/Core/LoadedImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,7 @@ public bool TryReadBytes(ulong off, int length, byte[] membuf)
public uint ReadLeUInt32(Address addr) { return ReadLeUInt32(this.abImage, ToOffset(addr)); }
public short ReadLeInt16(Address addr) { return ReadLeInt16(this.abImage, ToOffset(addr)); }
public ushort ReadLeUInt16(Address addr) { return ReadLeUInt16(this.abImage, ToOffset(addr)); }
public byte ReadByte(Address addr) { return this.abImage[ToOffset(addr)]; }
public bool TryReadByte(Address addr, out byte b) { return TryReadByte(this.abImage, ToOffset(addr), out b); }
public bool TryReadBytes(Address addr, int length, byte[] membuf) { return TryReadBytes(ToOffset(addr), length, membuf); }

Expand Down Expand Up @@ -588,6 +589,7 @@ public static void WriteLeInt16(byte[] abImage, ulong offset, short w)
}

public void WriteByte(Address addr, byte b) { WriteByte(ToOffset(addr), b); }
public void WriteLeUInt16(Address addr, ushort w) { WriteLeUInt16(ToOffset(addr), w); }
public void WriteLeUInt32(Address addr, uint dw) { WriteLeUInt32(ToOffset(addr), dw); }

public static void WriteBytes(byte[] srcBytes, ulong offset, int count, byte[] dstBytes)
Expand Down
Loading

0 comments on commit f0a9624

Please sign in to comment.