private void DecompileAssembler(string archName, Address loadAddress) { var sc = new ServiceContainer(); var cfg = RekoConfigurationService.Load(sc); var arch = cfg.GetArchitecture(archName); var asm = arch.CreateAssembler(null); var program = asm.AssembleFragment(loadAddress, txtAssembler.Text + Environment.NewLine); var loader = new Loader(sc); var decomp = new Decompiler(loader, sc); var proj = new Project { Programs = { program } }; decomp.Project = proj; decomp.ScanPrograms(); decomp.AnalyzeDataFlow(); decomp.ReconstructTypes(); decomp.StructureProgram(); decomp.WriteDecompilerProducts(); plcOutput.Text = host.DisassemblyWriter.ToString(); plcDecompiled.Text = host.DecompiledCodeWriter.ToString(); }
private void DecompileC() { string tmpName = Guid.NewGuid().ToString(); string tmpDir = Server.MapPath("tmp"); string cFile = Path.Combine(tmpDir, tmpName + ".c"); string asmFile = Path.Combine(tmpDir, tmpName + ".asm"); try { CopyCSourceToTempFile(txtAssembler.Text, cFile); if (CompileCFile(tmpDir, cFile)) { var sc = new ServiceContainer(); var ldr = new Loader(sc); var cfg = RekoConfigurationService.Load(sc); var arch = cfg.GetArchitecture("x86-protected-32"); var env = cfg.GetEnvironment("win32"); var platform = env.Load(sc, arch); var asm = arch.CreateAssembler(null); var program = asm.AssembleFragment(Address.Ptr32(0x10000000), txtAssembler.Text + Environment.NewLine); program.Platform = platform; var decomp = new Decompiler(ldr, sc); var project = new Project { Programs = { program } }; decomp.Project = project; decomp.ScanPrograms(); decomp.AnalyzeDataFlow(); decomp.ReconstructTypes(); decomp.StructureProgram(); decomp.WriteDecompilerProducts(); plcOutput.Text = host.DisassemblyWriter.ToString(); plcDecompiled.Text = host.DecompiledCodeWriter.ToString(); } } finally { if (File.Exists(asmFile)) { File.Delete(asmFile); } } }
public override void PerformWork(IWorkerDialogService workerDlgSvc) { workerDlgSvc.SetCaption("Generating intermediate code"); Decompiler.AnalyzeDataFlow(); }
private void Decompile(Dictionary <string, object> pArgs) { pArgs.TryGetValue("--loader", out object imgLoader); var addrLoad = ParseAddress(pArgs, "--base"); try { var fileName = (string)pArgs["filename"]; var filePath = Path.GetFullPath(fileName); var imageLocation = ImageLocation.FromUri(filePath); var loadedImage = ldr.Load(imageLocation, (string)imgLoader, addrLoad); Project project; switch (loadedImage) { case Program loadedProgram: project = Project.FromSingleProgram(loadedProgram); break; case Project loadedProject: project = loadedProject; break; default: this.listener.Error("Cannot decompile {0}.", fileName); return; } var decompiler = new Decompiler(project, services); dcSvc.Decompiler = decompiler; var program = decompiler.Project.Programs[0]; program.User.ExtractResources = ShouldExtractResources(program, pArgs); if (pArgs.TryGetValue("heuristics", out var oHeur)) { decompiler.Project.Programs[0].User.Heuristics = ((string[])oHeur).ToSortedSet(); } if (pArgs.TryGetValue("metadata", out var oMetadata)) { decompiler.Project.MetadataFiles.Add(new MetadataFile { Location = ImageLocation.FromUri((string)oMetadata) }); } if (pArgs.ContainsKey("dasm-address")) { decompiler.Project.Programs[0].User.ShowAddressesInDisassembly = true; } if (pArgs.ContainsKey("dasm-bytes")) { decompiler.Project.Programs[0].User.ShowBytesInDisassembly = true; } if (pArgs.TryGetValue("aggressive-branch-removal", out object oAggressiveBranchRemoval)) { decompiler.Project.Programs[0].User.AggressiveBranchRemoval = oAggressiveBranchRemoval is bool flag && flag; } if (pArgs.TryGetValue("debug-types", out var oProcRange)) { decompiler.Project.Programs[0].DebugProcedureRange = ((int, int))oProcRange; } if (pArgs.TryGetValue("debug-trace-proc", out object oTraceProcs)) { decompiler.Project.Programs[0].User.DebugTraceProcedures = (HashSet <string>)oTraceProcs; } decompiler.ExtractResources(); decompiler.ScanPrograms(); if (!pArgs.ContainsKey("scan-only")) { decompiler.AnalyzeDataFlow(); decompiler.ReconstructTypes(); decompiler.StructureProgram(); decompiler.WriteDecompilerProducts(); } } catch (Exception ex) { listener.Error(ex, "An error occurred during decompilation."); } }