public Program(SegmentMap segmentMap, IProcessorArchitecture arch, IPlatform platform) : this() { this.SegmentMap = segmentMap; this.ImageMap = segmentMap.CreateImageMap(); this.Architecture = arch; this.Platform = platform; }
public void Sm_Overlaps() { SegmentMap im = new SegmentMap(Address.SegPtr(0x8000, 0)); var mem = new MemoryArea(im.BaseAddress, new byte[40]); var seg = new ImageSegment("8000", Address.SegPtr(0x8000, 10), mem, AccessMode.ReadWrite); im.AddSegment(seg); }
public void Setup() { mr = new MockRepository(); mockFactory = new MockFactory(mr); var platform = mockFactory.CreatePlatform(); var imageMap = new SegmentMap(Address32.Ptr32(0x05)); program = new Program(imageMap, platform.Architecture, platform); interactor = new CombinedCodeViewInteractor(); var uiPreferencesSvc = mr.Stub<IUiPreferencesService>(); var uiSvc = mr.Stub<IDecompilerShellUiService>(); var styles = new Dictionary<string, UiStyle>() { { UiStyles.CodeWindow, new UiStyle { Background = new SolidBrush(Color.White), } } }; uiPreferencesSvc.Stub(u => u.Styles).Return(styles); var sc = new ServiceContainer(); sc.AddService<IUiPreferencesService>(uiPreferencesSvc); sc.AddService<IDecompilerShellUiService>(uiSvc); interactor.SetSite(sc); }
public void Setup() { mr = new MockRepository(); form = new MainForm(); sc = new ServiceContainer(); loader = mr.StrictMock<ILoader>(); dec = mr.StrictMock<IDecompiler>(); sc = new ServiceContainer(); uiSvc = new FakeShellUiService(); host = mr.StrictMock<DecompilerHost>(); memSvc = mr.StrictMock<ILowLevelViewService>(); var mem = new MemoryArea(Address.Ptr32(0x10000), new byte[1000]); var imageMap = new SegmentMap( mem.BaseAddress, new ImageSegment("code", mem, AccessMode.ReadWriteExecute)); var arch = mr.StrictMock<IProcessorArchitecture>(); var platform = mr.StrictMock<IPlatform>(); program = new Program(imageMap, arch, platform); project = new Project { Programs = { program } }; browserSvc = mr.StrictMock<IProjectBrowserService>(); sc.AddService<IDecompilerUIService>(uiSvc); sc.AddService(typeof(IDecompilerShellUiService), uiSvc); sc.AddService(typeof(IDecompilerService), new DecompilerService()); sc.AddService(typeof(IWorkerDialogService), new FakeWorkerDialogService()); sc.AddService(typeof(DecompilerEventListener), new FakeDecompilerEventListener()); sc.AddService(typeof(IProjectBrowserService), browserSvc); sc.AddService(typeof(ILowLevelViewService), memSvc); sc.AddService<ILoader>(loader); sc.AddService<DecompilerHost>(host); i = new TestInitialPageInteractor(sc, dec); }
public X86Emulator(IntelArchitecture arch, SegmentMap segmentMap, IPlatformEmulator envEmulator) { this.arch = arch; this.map = segmentMap; this.Registers = new ulong[40]; this.Valid = new bool[40]; this.envEmulator = envEmulator; }
public void Sm_AddSegment() { var map = new SegmentMap(addrBase); var mem = new MemoryArea(addrBase, new byte[0x4000]); var seg = new ImageSegment("8100", Address.SegPtr(0x8100, 0), mem, AccessMode.ReadWriteExecute); map.AddSegment(seg); Assert.AreEqual(0x3000, seg.Size); }
public void Setup() { mr = new MockRepository(); seg1 = new ImageSegment("seg1", new MemoryArea(Address.Ptr32(0x01000), new byte[0x1000]), AccessMode.Execute); seg2 = new ImageSegment("seg2", new MemoryArea(Address.Ptr32(0x02000), new byte[0x1000]), AccessMode.Execute); map = new SegmentMap(seg1.Address, seg1, seg2); }
private ImageWriter Memory(uint address) { mem = new MemoryArea(Address.Ptr32(address), new byte[1024]); imageMap = new SegmentMap( mem.BaseAddress, new ImageSegment(".data", mem, AccessMode.ReadWrite)); var writer = new LeImageWriter(mem.Bytes); return writer; }
public override Program Load(Address addrLoad) { int iImageStart = (exe.e_cparHeader * 0x10); int cbImageSize = exe.e_cpImage * ExeImageLoader.CbPageSize - iImageStart; byte[] bytes = new byte[cbImageSize]; int cbCopy = Math.Min(cbImageSize, RawImage.Length - iImageStart); Array.Copy(RawImage, iImageStart, bytes, 0, cbCopy); imgLoaded = new MemoryArea(addrLoad, bytes); segmentMap = new SegmentMap(addrLoad); return new Program(segmentMap, arch, platform); }
public Win32Emulator(SegmentMap map, IPlatform platform, Dictionary<Address, ImportReference> importReferences) { this.map = map; this.platform = platform; this.uPseudoFn = 0xDEAD0000u; // unlikely to be a real pointer to a function this.InterceptedCalls = new Dictionary<uint, ExternalProcedure>(); modules = new Dictionary<string, Module>(StringComparer.InvariantCultureIgnoreCase); AddWellKnownProcedures(); InterceptCallsToImports(importReferences); }
private void btnLoad_Click(object sender, EventArgs e) { var mem = new MemoryArea(Address.Ptr32(0x12312300),new byte[0x1000]); var imageMap = new SegmentMap( mem.BaseAddress, new ImageSegment("code", mem, AccessMode.ReadWriteExecute)); var arch = new X86ArchitectureFlat32(); var program = new Core.Program(imageMap, arch, new DefaultPlatform(null, arch)); var project = new Project { Programs = { program } }; pbs.Load(project); }
public void Setup() { mr = new MockRepository(); sc = new ServiceContainer(); var mem = new MemoryArea(Address.SegPtr(0xC00, 0), Enumerable.Range(0x0, 0x100).Select(b => (byte)b).ToArray()); var imageMap = new SegmentMap( mem.BaseAddress, new ImageSegment( "code", mem, AccessMode.ReadWriteExecute)); var arch = new Mocks.FakeArchitecture(); this.program = new Program(imageMap, arch, new DefaultPlatform(sc, arch)); }
private void AssertMap(string sExp, SegmentMap sm) { var strs = sm.Segments.Values .Select(ss => string.Format("{0} {1:X8} {2}{3}{4} {5}", ss.Address, ss.Size, (ss.Access & AccessMode.Read) != 0 ? "r" : "-", (ss.Access & AccessMode.Write) != 0 ? "w" : "-", (ss.Access & AccessMode.Execute) != 0 ? "x" : "-", ss.Name)); string s = string.Join(Environment.NewLine, strs); if (s != sExp) Debug.Print(s); Assert.AreEqual(sExp, s); }
private void Given_Image(uint addr, params byte[] bytes) { mem = new MemoryArea(Address.Ptr32(addr), bytes); imageMap = new SegmentMap( mem.BaseAddress, new ImageSegment(".text", mem, AccessMode.ReadExecute)); host.Stub(h => h.SegmentMap).Return(imageMap); host.Stub(h => h.TE_GetMemoryInfo( Arg<ulong>.Is.Anything, out Arg<MEMORY_BASIC_INFORMATION>.Out(new MEMORY_BASIC_INFORMATION { BaseAddress = mem.BaseAddress.ToLinear(), RegionSize = (uint)mem.Length, AllocationBase = mem.BaseAddress.ToLinear() }).Dummy)).Return(true); }
private void Given_Code(Action<X86Assembler> coder) { var asm = new X86Assembler(sc, new DefaultPlatform(sc, arch), Address.Ptr32(0x00100000), new List<ImageSymbol>()); coder(asm); var program = asm.GetImage(); this.segmentMap = program.SegmentMap; Given_Platform(); var win32 = new Win32Emulator(program.SegmentMap, platform, importReferences); emu = new X86Emulator(arch, program.SegmentMap, win32); emu.InstructionPointer = program.ImageMap.BaseAddress; emu.WriteRegister(Registers.esp, (uint)program.ImageMap.BaseAddress.ToLinear() + 0x0FFC); emu.ExceptionRaised += delegate { throw new Exception(); }; }
public void Sm_Creation() { SegmentMap sm = new SegmentMap(addrBase, new ImageSegment("", new MemoryArea(addrBase, img), AccessMode.ReadWriteExecute)); sm.AddSegment(Address.SegPtr(0x8000, 2), "", AccessMode.ReadWrite, 10); sm.AddSegment(Address.SegPtr(0x8000, 3), "", AccessMode.ReadWrite, 10); sm.AddSegment(Address.SegPtr(0x8000, 0), "", AccessMode.ReadWrite, 10); // Verify var sExp = @"8000:0000 00000002 rwx 8000:0002 00000001 rw- 8000:0003 0000001D rw- "; AssertMap(sExp, sm); }
private void DisassemblyControlForm_Load(object sender, EventArgs e) { var random = new Random(0x4711); var mem = new MemoryArea(Address.Ptr32(0x00100000), Enumerable.Range(0, 10000) .Select(i => (byte)random.Next(256)).ToArray()); var seg = new ImageSegment(".text", mem, AccessMode.ReadExecute); var segmentMap = new SegmentMap(mem.BaseAddress, seg); disassemblyControl1.Model = new DisassemblyTextModel( new CoreProgram { //new Decompiler.Arch.X86.X86ArchitectureFlat32(); Architecture = new Reko.Arch.PowerPC.PowerPcArchitecture32(), SegmentMap = segmentMap }, seg); disassemblyControl1.StartAddress = mem.BaseAddress; }
public override Program Load(Address addrLoad) { if (!ParseDMKHeader()) return null; var tracks = BuildTrackList(TrackLength); var bytes = tracks.SelectMany(t => t.Sectors) .SelectMany(s => s.GetData()) .ToArray(); var mem = new MemoryArea(addrLoad, bytes); var segmentMap = new SegmentMap(addrLoad, new ImageSegment("", mem, AccessMode.ReadWriteExecute)); return new Program { Architecture = new Z80ProcessorArchitecture(), SegmentMap = segmentMap, }; }
public void Setup() { mr = new MockRepository(); sc = new ServiceContainer(); mem = new MemoryArea(Address.Ptr32(0x00400000), new byte[2000]); segmentMap = new SegmentMap(mem.BaseAddress); arch = mr.Stub<IProcessorArchitecture>(); platform = mr.Stub<IPlatform>(); searchSvc = mr.Stub<ISearchResultService>(); sc.AddService<ISearchResultService>(searchSvc); program = new Program { Architecture = arch, Platform = platform, SegmentMap = segmentMap, }; }
private void chkShowData_CheckedChanged(object sender, EventArgs e) { if (chkShowData.Checked) { var mem = new MemoryArea(Address.Ptr32(0x00100000), new byte[2560]); var segMap = new SegmentMap(mem.BaseAddress, new ImageSegment("", mem, AccessMode.ReadWriteExecute)); var imgMap = segMap.CreateImageMap(); imgMap.AddItemWithSize(Address.Ptr32(0x00100000), new ImageMapBlock { Size = 30 }); imgMap.AddItemWithSize(Address.Ptr32(0x00100100), new ImageMapBlock { Size = 300 }); imgMap.AddItemWithSize(Address.Ptr32(0x00100500), new ImageMapBlock { Size = 600 }); memoryControl1.Architecture = new X86ArchitectureFlat32(); imageMapView1.ImageMap = imgMap; } else { memoryControl1.Architecture = null; } }
public override Program Load(Address addrLoad) { BinHexDecoder dec = new BinHexDecoder(new StringReader(Encoding.ASCII.GetString(RawImage))); IEnumerator<byte> stm = dec.GetBytes().GetEnumerator(); BinHexHeader hdr = LoadBinHexHeader(stm); byte[] dataFork = LoadFork(hdr.DataForkLength, stm); byte[] rsrcFork = LoadFork(hdr.ResourceForkLength, stm); var arch = new M68kArchitecture(); var platform = new MacOSClassic(Services, arch); if (hdr.FileType == "PACT") { Cpt.CompactProArchive archive = new Cpt.CompactProArchive(); List<ArchiveDirectoryEntry> items = archive.Load(new MemoryStream(dataFork)); IArchiveBrowserService abSvc = Services.GetService<IArchiveBrowserService>(); if (abSvc != null) { var selectedFile = abSvc.UserSelectFileFromArchive(items); if (selectedFile != null) { var image = selectedFile.GetBytes(); this.rsrcFork = new ResourceFork(image, arch); this.mem = new MemoryArea(addrLoad, image); this.segmentMap = new SegmentMap(addrLoad, new ImageSegment("", mem, AccessMode.ReadWriteExecute)); return new Program(this.segmentMap, arch, platform); } } } this.mem = new MemoryArea(addrLoad, dataFork); return new Program( new SegmentMap(mem.BaseAddress, new ImageSegment("", mem, AccessMode.ReadWriteExecute)), arch, platform); }
public override abstract IEnumerable<Address> CreatePointerScanner( SegmentMap map, ImageReader rdr, IEnumerable<Address> addrs, PointerScannerFlags flags);
public void DumpData(SegmentMap map, IProcessorArchitecture arch, AddressRange range, Formatter stm) { DumpData(map, arch, range.Begin, (long)(range.End - range.Begin), stm); }
public void DumpData(SegmentMap map, AddressRange range, Formatter stm) { DumpData(map, range.Begin, (long)(range.End - range.Begin), stm); }
public override IEnumerable<Address> CreatePointerScanner(SegmentMap map, ImageReader rdr, IEnumerable<Address> knownAddresses, PointerScannerFlags flags) { var knownLinAddresses = knownAddresses.Select(a => (uint)a.ToLinear()).ToHashSet(); return new MipsPointerScanner32(rdr, knownLinAddresses, flags).Select(l => Address.Ptr32(l)); }
public void Sm_AddNamedSegment() { var mem = new MemoryArea(Address.SegPtr(0x0B00, 0), new byte[0x2000]); SegmentMap segmentMap = new SegmentMap(mem.BaseAddress, new ImageSegment("base", mem, AccessMode.ReadWriteExecute)); segmentMap.AddSegment(Address.SegPtr(0xC00, 0), "0C00", AccessMode.ReadWrite, 6000); ImageSegment s = segmentMap.Segments.Values.ElementAt(1); Assert.AreEqual("0C00", s.Name); Assert.AreEqual(0x1000, s.Size); }
public void DumpData(SegmentMap map, 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; 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; } } }
public abstract IEnumerable <Address> CreatePointerScanner(SegmentMap map, EndianImageReader rdr, IEnumerable <Address> knownAddresses, PointerScannerFlags flags);
void parseSection64(uint protection, SegmentMap segmentMap) { var abSectname = rdr.ReadBytes(16); var abSegname = rdr.ReadBytes(16); ulong addr; ulong size; uint offset; uint align; uint reloff; uint nreloc; uint flags; uint reserved1; uint reserved2; uint reserved3; if (!rdr.TryReadUInt64(out addr) || !rdr.TryReadUInt64(out size) || !rdr.TryReadUInt32(out offset) || !rdr.TryReadUInt32(out align) || !rdr.TryReadUInt32(out reloff) || !rdr.TryReadUInt32(out nreloc) || !rdr.TryReadUInt32(out flags) || !rdr.TryReadUInt32(out reserved1) || !rdr.TryReadUInt32(out reserved2) || !rdr.TryReadUInt32(out reserved3)) { throw new BadImageFormatException("Could not read Mach-O section."); } var sectionName = GetAsciizString(abSectname); var segmentName = GetAsciizString(abSegname); Debug.Print("Found section '{0}' in segment '{1}, addr = 0x{2:X}, size = 0x{3:X}.", sectionName, segmentName, addr, size); AccessMode am = 0; if ((protection & VM_PROT_READ) != 0) am |= AccessMode.Read; if ((protection & VM_PROT_WRITE) != 0) am |= AccessMode.Write; if ((protection & VM_PROT_EXECUTE) != 0) am |= AccessMode.Execute; var bytes = rdr.CreateNew(this.ldr.RawImage, offset); var mem = new MemoryArea( Address.Ptr64(addr), bytes.ReadBytes((uint)size)); var imageSection = new ImageSegment( string.Format("{0},{1}", segmentName, sectionName), mem, am); //imageSection.setBss((section.flags & SECTION_TYPE) == S_ZEROFILL); //if (!imageSection.isBss()) { // auto pos = source_->pos(); // if (!source_->seek(section.offset)) { // throw ParseError("Could not seek to the beginning of the section's content."); // } // auto bytes = source_->read(section.size); // if (checked_cast<uint>(bytes.size()) != section.size) { // log_.warning("Could not read all the section's content."); // } else { // imageSection->setContent(std::move(bytes)); // } // source_->seek(pos); //} //sections_.push_back(imageSection.get()); //image_->addSection(std::move(imageSection)); segmentMap.AddSegment(imageSection); }
void parseSegmentCommand64(SegmentMap imageMap) { var abSegname = rdr.ReadBytes(16); var cChars = Array.IndexOf<byte>(abSegname, 0); if (cChars == -1) cChars = 16; string segname = Encoding.ASCII.GetString(abSegname, 0, cChars); ulong vmaddr; ulong vmsize; ulong fileoff; ulong filesize; uint maxprot; uint initprot; uint nsects; uint flags; if (!rdr.TryReadUInt64(out vmaddr) || !rdr.TryReadUInt64(out vmsize) || !rdr.TryReadUInt64(out fileoff) || !rdr.TryReadUInt64(out filesize) || !rdr.TryReadUInt32(out maxprot) || !rdr.TryReadUInt32(out initprot) || !rdr.TryReadUInt32(out nsects) || !rdr.TryReadUInt32(out flags)) { throw new BadImageFormatException("Could not read segment command."); } Debug.Print("Found segment '{0}' with {1} sections.", segname, nsects); for (uint i = 0; i < nsects; ++i) { Debug.Print("Parsing section number {0}.", i); parseSection64(initprot, imageMap); } }
public SegmentMap ParseLoadCommands(uint ncmds, Address addrLoad) { var imageMap = new SegmentMap(addrLoad); Debug.Print("Parsing load commands, {0} of them.", ncmds); var lookup = GetType() .GetFields( BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy) .Where(fi => fi.IsLiteral && !fi.IsInitOnly && fi.Name.StartsWith("LC_")) .ToDictionary(fi => (uint)fi.GetValue(null), fi => fi.Name); for (uint i = 0; i < ncmds; ++i) { var pos = rdr.Offset; uint cmd; uint cmdsize; if (!rdr.TryReadUInt32(out cmd) || !rdr.TryReadUInt32(out cmdsize)) { throw new BadImageFormatException(string.Format( "Unable to read Mach-O command ({0:X}).", rdr.Offset)); } Debug.Print("{0,3}: Read load command 0x{1:X} {2} of size {3}.", i, cmd, lookup.ContainsKey(cmd) ? lookup[cmd] : "", cmdsize); switch (cmd & ~LC_REQ_DYLD) { //case LC_SEGMENT: // parseSegmentCommand<segment_command, section>(); // break; case LC_SEGMENT_64: parseSegmentCommand64(imageMap); break; //case LC_SYMTAB: // parseSymtabCommand<Mach>(); // break; case LC_FUNCTION_STARTS: parseFunctionStarts(rdr.Clone()); break; } rdr.Offset = pos + cmdsize; } return imageMap; }
public abstract IEnumerable<Address> CreateInstructionScanner(SegmentMap map, ImageReader rdr, IEnumerable<Address> knownAddresses, PointerScannerFlags flags);
public override IEnumerable<Address> CreatePointerScanner( SegmentMap map, ImageReader rdr, IEnumerable<Address> knownAddresses, PointerScannerFlags flags) { var knownLinAddresses = knownAddresses .Select(a => a.ToLinear()) .ToHashSet(); return new PowerPcPointerScanner64(rdr, knownLinAddresses, flags) .Select(u => Address.Ptr64(u)); }
public override IEnumerable<Address> CreatePointerScanner(SegmentMap map, ImageReader rdr, IEnumerable<Address> knownAddresses, PointerScannerFlags flags) { throw new NotImplementedException(); }
private Expression GetMemoryValue(Address addr, DataType dt, SegmentMap segmentMap) { if (!(dt is PrimitiveType pt)) { return(Constant.Invalid); }