public void DumpData(SegmentMap map, Address address, long cbBytes, TextWriter stm) { ulong cSkip = address.ToLinear() & 0x0F; ImageSegment segment; if (!map.TryFindSegment(address, out segment) || segment.MemoryArea == null) { return; } ImageReader rdr = arch.CreateImageReader(segment.MemoryArea, address); while (cbBytes > 0) { StringBuilder sb = new StringBuilder(0x12); try { stm.Write("{0} ", rdr.Address); for (int i = 0; i < 16; ++i) { if (cbBytes > 0 && cSkip == 0) { byte b = rdr.ReadByte(); stm.Write("{0:X2} ", b); sb.Append(0x20 <= b && b < 0x7F ? (char)b : '.'); --cbBytes; } else { stm.Write(" "); if (cSkip > 0) { sb.Append(' '); } --cSkip; } } } catch { stm.WriteLine(); stm.WriteLine("...end of image"); return; } stm.WriteLine(sb.ToString()); } }
public void DumpAssembler(SegmentMap map, IProcessorArchitecture arch, Address addrStart, Address addrLast, Formatter formatter) { if (!map.TryFindSegment(addrStart, out var segment)) { return; } var dasm = arch.CreateDisassembler(arch.CreateImageReader(segment.MemoryArea, addrStart)); try { var writer = new InstrWriter(formatter); foreach (var instr in dasm) { if (instr.Address >= addrLast) { break; } if (!DumpAssemblerLine(segment.MemoryArea, arch, instr, writer)) { break; } } } catch (Exception ex) { formatter.WriteLine(ex.Message); formatter.WriteLine(); } }
/// <summary> /// Creates an image reader that scans all available memory in the segment. /// </summary> /// <param name="platform"></param> /// <returns></returns> public ImageReader CreateImageReader(IProcessorArchitecture arch) { var addrBegin = Address.Max(this.Address, this.MemoryArea.BaseAddress); var addrEnd = Address.Min(this.Address + this.Size, this.MemoryArea.EndAddress); return(arch.CreateImageReader(this.MemoryArea, addrBegin, addrEnd)); }
public IEnumerable <WorkItem> VisitPointer(Pointer ptr) { Debug.Print("Iterating pointer at {0:X}", gOffset); var rdr = arch.CreateImageReader(image, (ulong)gOffset - image.BaseAddress.ToLinear()); if (!rdr.IsValid) { return(null); } var c = rdr.Read(PrimitiveType.Create(Domain.Pointer, ptr.Size)); int offset = c.ToInt32(); Debug.Print(" pointer value: {0:X}", offset); if (visited.Contains(offset) || !image.IsValidLinearAddress((uint)offset)) { return(Enumerable.Empty <WorkItem>()); } // We've successfully traversed a pointer to a valid destination! // The address must therefore be of type ptr.Pointee. visited.Add(offset); if (globalStr.Fields.AtOffset(offset) == null) { Debug.Print(" Discovery: {0:X} {1}", offset, ptr.Pointee); Discoveries.Add(new StructureField(offset, ptr.Pointee)); } return(Single(new WorkItem { DataType = ptr.Pointee, GlobalOffset = c.ToInt32() })); }
public IEnumerable <WorkItem>?VisitPointer(Pointer ptr) { Debug.Print("Iterating pointer at {0:X}", gOffset); if (!segmentMap.TryFindSegment(segmentMap.MapLinearAddressToAddress((ulong)gOffset), out ImageSegment segment)) { return(null); } var rdr = arch.CreateImageReader(segment.MemoryArea, gOffset - (long)segment.MemoryArea.BaseAddress.ToLinear()); if (!rdr.TryRead(PrimitiveType.Create(Domain.Pointer, ptr.BitSize), out var c)) { return(null); } long offset = c.ToInt64(); Debug.Print(" pointer value: {0:X}", offset); if (visited.Contains(offset) || !segment.MemoryArea.IsValidLinearAddress((ulong)offset)) { return(Enumerable.Empty <WorkItem>()); } // We've successfully traversed a pointer to a valid destination! // The address must therefore be of type ptr.Pointee. visited.Add(offset); if (globalStr.Fields.AtOffset((int)offset) == null) { Debug.Print(" Discovery: {0:X} {1}", offset, ptr.Pointee); Discoveries.Add(new StructureField((int)offset, ptr.Pointee)); } return(Single(new WorkItem { DataType = ptr.Pointee, GlobalOffset = c.ToInt32() })); }
public static Address GetRawBinaryEntryAddress( RawFileDefinition rawFile, byte[] image, IProcessorArchitecture arch, Address baseAddr) { if (!string.IsNullOrEmpty(rawFile.EntryPoint.Address)) { if (arch.TryParseAddress(rawFile.EntryPoint.Address, out Address entryAddr)) { if (rawFile.EntryPoint.Follow) { var rdr = arch.CreateImageReader(new MemoryArea(baseAddr, image), entryAddr); var addr = arch.ReadCodeAddress(0, rdr, arch.CreateProcessorState()); return(addr); } else { return(entryAddr); } } else { return(baseAddr); } } return(baseAddr); }
public void DumpAssembler( IProcessorArchitecture arch, MemoryArea mem, Address addrStart, long cbBytes, Formatter formatter) { var dasm = arch.CreateDisassembler(arch.CreateImageReader(mem, addrStart)); try { var writer = new InstrWriter(formatter); var options = new MachineInstructionRendererOptions( flags: MachineInstructionRendererFlags.ResolvePcRelativeAddress, syntax: ""); foreach (var instr in dasm) { if (cbBytes <= 0) { break; } if (!DumpAssemblerLine(mem, arch, instr, writer, options)) { break; } cbBytes -= instr.Length; } } catch (Exception ex) { formatter.WriteLine(ex.Message); formatter.WriteLine(); } }
public void DumpData( IProcessorArchitecture arch, MemoryArea mem, Address address, long cUnits, Formatter stm) { long offset = address - mem.BaseAddress; if (offset < 0 || cUnits <= 0) { return; } var rdr = arch.CreateImageReader(mem, address, cUnits); var memfmt = mem.Formatter; var output = new MemoryFormatterOutput(stm); //try { memfmt.RenderMemory(rdr, program.TextEncoding, output); } //catch //{ // stm.WriteLine(); // stm.WriteLine(";;; ...end of image"); //} }
public void DumpAssembler(SegmentMap map, IProcessorArchitecture arch, Address addrStart, Address addrLast, Formatter formatter) { if (!map.TryFindSegment(addrStart, out var segment)) { return; } var dasm = arch.CreateDisassembler(arch.CreateImageReader(segment.MemoryArea, addrStart)); try { var writer = new InstrWriter(formatter); var options = new MachineInstructionRendererOptions( flags: MachineInstructionRendererFlags.ResolvePcRelativeAddress, syntax: ""); foreach (var instr in dasm) { if (instr.Address ! >= addrLast) { break; } if (!DumpAssemblerLine(segment.MemoryArea, arch, instr, writer, options)) { break; } } } catch (Exception ex) { formatter.WriteLine(ex.Message); formatter.WriteLine(); } }
/// <summary> /// Creates an image reader that scans all available memory in the segment. /// </summary> /// <param name="platform"></param> /// <returns></returns> public EndianImageReader CreateImageReader(IProcessorArchitecture arch) { var offsetBegin = Math.Max(this.Address - this.MemoryArea.BaseAddress, 0); var offsetEnd = Math.Min(this.Size, this.MemoryArea.Length); return(arch.CreateImageReader(this.MemoryArea, offsetBegin, offsetEnd)); }
// Convenience functions. public EndianImageReader CreateImageReader(IProcessorArchitecture arch, Address addr) { if (!SegmentMap.TryFindSegment(addr, out var segment)) { throw new ArgumentException(string.Format("The address {0} is invalid.", addr)); } return(arch.CreateImageReader(segment.MemoryArea, addr)); }
public IEnumerable <MachineInstruction> CreateDisassembler(IProcessorArchitecture arch, Address addr) { if (!SegmentMap.TryFindSegment(addr, out var segment)) { throw new ArgumentException(string.Format("The address {0} is invalid.", addr)); } return(arch.CreateDisassembler( arch.CreateImageReader(segment.MemoryArea, addr))); }
public void DumpData(LoadedImage image, Address address, long cbBytes, TextWriter stm) { ulong cSkip = address.ToLinear() & 0x0F; ImageReader rdr = arch.CreateImageReader(image, address); while (cbBytes > 0) { StringBuilder sb = new StringBuilder(0x12); try { stm.Write("{0} ", rdr.Address); for (int i = 0; i < 16; ++i) { if (cbBytes > 0 && cSkip == 0) { byte b = rdr.ReadByte(); stm.Write("{0:X2} ", b); sb.Append(0x20 <= b && b < 0x7F ? (char)b : '.'); --cbBytes; } else { stm.Write(" "); if (cSkip > 0) { sb.Append(' '); } --cSkip; } } } catch { stm.WriteLine(); stm.WriteLine("...end of image"); return; } stm.WriteLine(sb.ToString()); } }
private void DumpTypedData(SegmentMap map, IProcessorArchitecture arch, ImageMapItem item, Formatter w) { if (!map.TryFindSegment(item.Address, out var segment) || segment.MemoryArea == null) { return; } WriteLabel(item.Address, w); var rdr = arch.CreateImageReader(segment.MemoryArea, item.Address); item.DataType.Accept(new TypedDataDumper(rdr, item.Size, w)); }
public void WriteOpcodes(MemoryArea image, IProcessorArchitecture arch, Address begin, Address addrEnd, InstrWriter writer) { EndianImageReader rdr = arch.CreateImageReader(image, begin); var byteSize = (7 + arch.InstructionBitSize) / 8; string instrByteFormat = $"{{0:X{byteSize * 2}}} "; // each byte is two nybbles. var instrByteSize = PrimitiveType.CreateWord(arch.InstructionBitSize); while (rdr.Address < addrEnd && rdr.TryRead(instrByteSize, out var v)) { writer.WriteFormat(instrByteFormat, v.ToUInt64()); } }
public Sifter(string[] args) { this.cfgSvc = RekoConfigurationService.Load("reko/reko.config"); this.processInstr = new Action <byte[], MachineInstruction?>(ProcessInstruction); (this.arch, this.instrRenderer) = ProcessArgs(args); var baseAddress = Address.Ptr32(0x00000000); //$TODO allow customization? this.mem = new MemoryArea(baseAddress, new byte[100]); this.rdr = arch.CreateImageReader(mem, 0); this.dasm = arch.CreateDisassembler(rdr); this.progress = new Progress(); }
private Constant ReadValue(DataType dt, Expression eAddr) { if (eAddr is Constant cAddr) { var addr = arch.MakeAddressFromConstant(cAddr); if (!segmentMap.TryFindSegment(addr, out ImageSegment seg)) { return(Constant.Invalid); } var rdr = arch.CreateImageReader(seg.MemoryArea, addr); memAccesses[addr] = dt; if (!rdr.TryRead((PrimitiveType)dt, out var c)) { return(Constant.Invalid); } else { return(c); } } throw new NotImplementedException(); }
public void WriteByteRange(MemoryArea image, IProcessorArchitecture arch, Address begin, Address addrEnd, InstrWriter writer) { EndianImageReader rdr = arch.CreateImageReader(image, begin); var byteSize = (7 + arch.InstructionBitSize) / 8; this.instrByteFormat = $"{{0:X{byteSize * 2}}} "; // each byte is two nybbles. this.instrByteSize = PrimitiveType.CreateWord(arch.InstructionBitSize); while (rdr.Address < addrEnd) { var v = rdr.Read(this.instrByteSize); writer.WriteFormat(this.instrByteFormat, v.ToUInt64()); } }
private long PerformanceTest_A32Dasm(IProcessorArchitecture arch, byte[] buf) { var mem = new MemoryArea(Address.Ptr32(0x00100000), buf); var rdr = arch.CreateImageReader(mem, mem.BaseAddress); var dasm = arch.CreateDisassembler(rdr); Stopwatch sw = new Stopwatch(); sw.Start(); foreach (var instr in dasm) { } sw.Stop(); var time = sw.ElapsedMilliseconds; return(time); }
private long PerformanceTest_A32Rewriter(IProcessorArchitecture arch, byte[] buf) { var mem = new MemoryArea(Address.Ptr32(0x00100000), buf); var rdr = arch.CreateImageReader(mem, mem.BaseAddress); var dasm = arch.CreateRewriter(rdr, arch.CreateProcessorState(), new StorageBinder(), new RewriterPerformanceDialog.RewriterHost(new Dictionary <Address, ImportReference>())); Stopwatch sw = new Stopwatch(); sw.Start(); foreach (var instr in dasm) { } sw.Stop(); var time = sw.ElapsedMilliseconds; return(time); }
/// <summary> /// Creates an image reader that scans all available memory in the segment. /// </summary> /// <param name="platform"></param> /// <returns></returns> public ImageReader CreateImageReader(IProcessorArchitecture arch) { var addrBegin = Address.Max(this.Address, this.MemoryArea.BaseAddress); var addrEnd = Address.Min(this.Address + this.Size, this.MemoryArea.EndAddress); return arch.CreateImageReader(this.MemoryArea, addrBegin, addrEnd); }
public EndianImageReader CreateReader(IProcessorArchitecture arch, Address addr) { return(arch.CreateImageReader(mem, addr)); }
private void InitializeRekoDisassembler() { this.rdr = arch.CreateImageReader(mem, 0); this.dasm = arch.CreateDisassembler(rdr); }
protected override IEnumerable <RtlInstructionCluster> GetRtlStream(MemoryArea mem, IStorageBinder binder, IRewriterHost host) { var state = new CrayProcessorState(arch); return(arch.CreateRewriter(arch.CreateImageReader(mem, 0), state, binder, host)); }
public void DumpData(SegmentMap map, IProcessorArchitecture arch, Address address, long cbBytes, Formatter stm) { const int BytesPerLine = 16; var linAddr = address.ToLinear(); ulong cSkip = linAddr - BytesPerLine * (linAddr / BytesPerLine); if (!map.TryFindSegment(address, out var segment) || segment.MemoryArea == null) { return; } byte[]? prevLine = null; bool showEllipsis = true; cbBytes = Math.Min(cbBytes, segment.MemoryArea.Length - (address - segment.MemoryArea.BaseAddress)); if (cbBytes <= 0) { return; } var rdr = arch.CreateImageReader(segment.MemoryArea, address); while (cbBytes > 0) { StringBuilder sb = new StringBuilder(0x12); var bytes = new List <byte>(); var sbBytes = new StringBuilder(); try { sbBytes.AppendFormat("{0} ", rdr.Address); for (int i = 0; i < BytesPerLine; ++i) { if (cbBytes > 0 && cSkip == 0) { byte b = rdr.ReadByte(); bytes.Add(b); sbBytes.AppendFormat("{0:X2} ", b); sb.Append(0x20 <= b && b < 0x7F ? (char)b : '.'); --cbBytes; } else { sbBytes.Append(" "); if (cSkip > 0) { sb.Append(' '); } --cSkip; } } var ab = bytes.ToArray(); if (!HaveSameZeroBytes(prevLine, ab)) { stm.Write(sbBytes.ToString()); stm.WriteLine(sb.ToString()); showEllipsis = true; } else { if (showEllipsis) { stm.WriteLine("; ..."); showEllipsis = false; } } prevLine = ab; } catch { stm.WriteLine(); stm.WriteLine(";;; ...end of image"); return; } } }