Exemplo n.º 1
0
        private static int Main(string[] args)
        {
            Console.WriteLine();
            Console.WriteLine("Mosa.Tool.Disassembler.Intel.Disassembler [www.mosa-project.org]");
            Console.WriteLine("Copyright 2016. New BSD License.");
            Console.WriteLine("Written by Phil Garcia ([email protected])");
            Console.WriteLine();

            try
            {
                var options = ParseOptions(args);
                if (options == null)
                {
                    return(-1);                    //Commandline errors will be printed by the commandline lib
                }

                // Need a new instance of translator every time as they aren't thread safe
                var translator = new IntelTranslator()
                {
                    // Configure the translator to output instruction addresses and instruction binary as hex
                    IncludeAddress = true,
                    IncludeBinary  = true
                };

                var code2 = File.ReadAllBytes(options.InputFile);

                var code = new byte[code2.Length];

                for (ulong i = options.FileOffset; i < (ulong)code2.Length; i++)
                {
                    code[i - options.FileOffset] = code2[i];
                }

                //using (var disasm = new SharpDisasm.Disassembler(code, ArchitectureMode.x86_32, options.StartingAddress, true, Vendor.Any, options.FileOffset))
                using (var disasm = new SharpDisasm.Disassembler(code, ArchitectureMode.x86_32, options.StartingAddress, true, Vendor.Any))
                {
                    using (var dest = File.CreateText(options.OutputFile))
                    {
                        foreach (var instruction in disasm.Disassemble())
                        {
                            var inst = translator.Translate(instruction);
                            dest.WriteLine(inst);

                            if (options.Length != 0 && instruction.PC > options.StartingAddress + options.Length)
                            {
                                break;
                            }
                        }
                    }
                }

                return(0);
            }
            catch (Exception e)
            {
                Console.Error.WriteLine("Exception: {0}", e.ToString());
                return(-1);
            }
        }
Exemplo n.º 2
0
        private void GenerateASMFile()
        {
            var translator = new IntelTranslator()
            {
                IncludeAddress = true,
                IncludeBinary  = true
            };

            var map = new Dictionary <ulong, List <string> >();

            foreach (var symbol in Linker.Symbols)
            {
                if (!map.TryGetValue(symbol.VirtualAddress, out List <string> list))
                {
                    list = new List <string>();
                    map.Add(symbol.VirtualAddress, list);
                }

                list.Add(symbol.Name);
            }

            var textSection     = Linker.Sections[(int)SectionKind.Text];
            var startingAddress = textSection.VirtualAddress + MultibootHeaderLength;
            var fileOffset      = Linker.BaseFileOffset + MultibootHeaderLength;
            var length          = textSection.Size;

            var code2 = File.ReadAllBytes(LauncherSettings.OutputFile);

            var code = new byte[code2.Length];

            for (ulong i = fileOffset; i < (ulong)code2.Length; i++)
            {
                code[i - fileOffset] = code2[i];
            }

            var mode = ArchitectureMode.x86_32;

            switch (LauncherSettings.Platform)
            {
            case "x86": mode = ArchitectureMode.x86_32; break;

            case "x64": mode = ArchitectureMode.x86_64; break;
            }

            using (var disasm = new Disassembler(code, mode, startingAddress, true, Vendor.Any))
            {
                using (var dest = File.CreateText(LauncherSettings.AsmFile))
                {
                    if (map.TryGetValue(startingAddress, out List <string> list))
                    {
                        foreach (var entry in list)
                        {
                            dest.WriteLine($"; {entry}");
                        }
                    }

                    foreach (var instruction in disasm.Disassemble())
                    {
                        var inst = translator.Translate(instruction);
                        dest.WriteLine(inst);

                        if (map.TryGetValue(instruction.PC, out List <string> list2))
                        {
                            foreach (var entry in list2)
                            {
                                dest.WriteLine($"; {entry}");
                            }
                        }

                        if (instruction.PC > startingAddress + length)
                        {
                            break;
                        }
                    }
                }
            }
        }
Exemplo n.º 3
0
        private void GenerateASMFile()
        {
            var translator = new IntelTranslator()
            {
                IncludeAddress = true,
                IncludeBinary  = true
            };

            var asmfile = Path.Combine(LauncherOptions.DestinationDirectory, Path.GetFileNameWithoutExtension(LauncherOptions.SourceFile) + ".asm");

            var textSection = Linker.Sections[(int)SectionKind.Text];

            var map = new Dictionary <ulong, List <string> >();

            foreach (var symbol in Linker.Symbols)
            {
                if (!map.TryGetValue(symbol.VirtualAddress, out List <string> list))
                {
                    list = new List <string>();
                    map.Add(symbol.VirtualAddress, list);
                }

                list.Add(symbol.Name);
            }

            const uint multibootHeaderLength = MultibootHeaderLength;
            ulong      startingAddress       = textSection.VirtualAddress + multibootHeaderLength;
            uint       fileOffset            = textSection.FileOffset + multibootHeaderLength;
            uint       length = textSection.Size;

            var code2 = File.ReadAllBytes(CompiledFile);

            var code = new byte[code2.Length];

            for (ulong i = fileOffset; i < (ulong)code2.Length; i++)
            {
                code[i - fileOffset] = code2[i];
            }

            using (var disasm = new Disassembler(code, ArchitectureMode.x86_32, startingAddress, true, Vendor.Any))
            {
                using (var dest = File.CreateText(asmfile))
                {
                    if (map.TryGetValue(startingAddress, out List <string> list))
                    {
                        foreach (var entry in list)
                        {
                            dest.WriteLine($"; {entry}");
                        }
                    }

                    foreach (var instruction in disasm.Disassemble())
                    {
                        var inst = translator.Translate(instruction);
                        dest.WriteLine(inst);

                        if (map.TryGetValue(instruction.PC, out List <string> list2))
                        {
                            foreach (var entry in list2)
                            {
                                dest.WriteLine($"; {entry}");
                            }
                        }

                        if (instruction.PC > startingAddress + length)
                        {
                            break;
                        }
                    }
                }
            }
        }
Exemplo n.º 4
0
        private void GenerateASMFile()
        {
            // Need a new instance of translator every time as they aren't thread safe
            var translator = new IntelTranslator();

            // Configure the translator to output instruction addresses and instruction binary as hex
            translator.IncludeAddress = true;
            translator.IncludeBinary  = true;

            var asmfile = Path.Combine(Options.DestinationDirectory, Path.GetFileNameWithoutExtension(Options.SourceFile) + ".asm");

            var textSection = Linker.LinkerSections[(int)SectionKind.Text];

            var map = new Dictionary <ulong, string>();

            foreach (var symbol in Linker.Symbols)
            {
                if (map.ContainsKey(symbol.VirtualAddress))
                {
                    continue;
                }

                map.Add(symbol.VirtualAddress, symbol.Name);
            }

            uint  multibootHeaderLength = MultibootHeaderLength;
            ulong startingAddress       = textSection.VirtualAddress + multibootHeaderLength;
            uint  fileOffset            = textSection.FileOffset + multibootHeaderLength;
            uint  length = textSection.Size;

            var code2 = File.ReadAllBytes(CompiledFile);

            var code = new byte[code2.Length];

            for (ulong i = fileOffset; i < (ulong)code2.Length; i++)
            {
                code[i - fileOffset] = code2[i];
            }

            using (var disasm = new SharpDisasm.Disassembler(code, ArchitectureMode.x86_32, startingAddress, true, Vendor.Any))
            {
                using (var dest = File.CreateText(asmfile))
                {
                    if (map.ContainsKey(startingAddress))
                    {
                        dest.WriteLine("; " + map[startingAddress]);
                    }

                    foreach (var instruction in disasm.Disassemble())
                    {
                        var inst = translator.Translate(instruction);
                        dest.WriteLine(inst);

                        if (map.ContainsKey(instruction.PC))
                        {
                            dest.WriteLine("; " + map[instruction.PC]);
                        }

                        if (instruction.PC > startingAddress + length)
                        {
                            break;
                        }
                    }
                }
            }
        }