Example #1
0
        public XmlDumper(bool ignoreSensitive, R2RReader r2r, TextWriter writer, Disassembler disassembler, DumpOptions options)
            : base(r2r, writer, disassembler, options)
        {
            _ignoreSensitive = ignoreSensitive;
            XmlDocument      = new XmlDocument();

            _ignoredProperties = new XmlAttributeOverrides();
            XmlAttributes attrs = new XmlAttributes();

            attrs.XmlIgnore = _ignoreSensitive;
            _ignoredProperties.Add(typeof(R2RHeader), "RelativeVirtualAddress", attrs);
            _ignoredProperties.Add(typeof(R2RHeader), "Size", attrs);
            _ignoredProperties.Add(typeof(R2RImportSection), "SectionRVA", attrs);
            _ignoredProperties.Add(typeof(R2RImportSection), "SectionSize", attrs);
            _ignoredProperties.Add(typeof(R2RImportSection), "EntrySize", attrs);
            _ignoredProperties.Add(typeof(R2RImportSection), "SignatureRVA", attrs);
            _ignoredProperties.Add(typeof(R2RImportSection), "AuxiliaryDataRVA", attrs);
            _ignoredProperties.Add(typeof(R2RImportSection.ImportSectionEntry), "SignatureSample", attrs);
            _ignoredProperties.Add(typeof(R2RImportSection.ImportSectionEntry), "SignatureRVA", attrs);
            _ignoredProperties.Add(typeof(RuntimeFunction), "StartAddress", attrs);
            _ignoredProperties.Add(typeof(RuntimeFunction), "UnwindRVA", attrs);
            _ignoredProperties.Add(typeof(R2RSection), "RelativeVirtualAddress", attrs);
            _ignoredProperties.Add(typeof(R2RSection), "Size", attrs);
        }
Example #2
0
 public TextDumper(ReadyToRunReader r2r, TextWriter writer, Disassembler disassembler, DumpOptions options)
     : base(r2r, writer, disassembler, options)
 {
 }
Example #3
0
        public TextDumper(R2RReader r2r, TextWriter writer, bool raw, bool header, bool disasm, Disassembler disassembler, bool unwind, bool gc, bool sectionContents)
        {
            _r2r    = r2r;
            _writer = writer;

            _raw             = raw;
            _header          = header;
            _disasm          = disasm;
            _disassembler    = disassembler;
            _unwind          = unwind;
            _gc              = gc;
            _sectionContents = sectionContents;
        }
Example #4
0
        private int Run(string[] args)
        {
            ArgumentSyntax syntax = ParseCommandLine(args);

            // open output stream
            if (_outputFilename != null)
            {
                _writer = File.CreateText(_outputFilename);
            }
            else
            {
                _writer = Console.Out;
            }

            if (_help)
            {
                _writer.WriteLine(syntax.GetHelpText());
                return(0);
            }

            Disassembler disassembler = null;

            try
            {
                if (_inputFilenames.Count == 0)
                {
                    throw new ArgumentException("Input filename must be specified (--in <file>)");
                }

                if (_diff && _inputFilenames.Count < 2)
                {
                    throw new ArgumentException("Need at least 2 input files in diff mode");
                }

                R2RReader previousReader = null;

                foreach (string filename in _inputFilenames)
                {
                    // parse the ReadyToRun image
                    R2RReader r2r = new R2RReader(filename);

                    if (_disasm)
                    {
                        if (r2r.InputArchitectureSupported() && r2r.DisassemblerArchitectureSupported())
                        {
                            disassembler = new Disassembler(r2r.Image, r2r.Machine);
                        }
                        else
                        {
                            throw new ArgumentException($"The architecture of input file {filename} ({r2r.Machine.ToString()}) or the architecture of the disassembler tools ({System.Runtime.InteropServices.RuntimeInformation.ProcessArchitecture.ToString()}) is not supported.");
                        }
                    }

                    if (_xml)
                    {
                        _dumper = new XmlDumper(_ignoreSensitive, r2r, _writer, _raw, _header, _disasm, disassembler, _unwind, _gc, _sectionContents);
                    }
                    else
                    {
                        _dumper = new TextDumper(r2r, _writer, _raw, _header, _disasm, disassembler, _unwind, _gc, _sectionContents);
                    }

                    if (!_diff)
                    {
                        // output the ReadyToRun info
                        Dump(r2r);
                    }
                    else if (previousReader != null)
                    {
                        new R2RDiff(previousReader, r2r, _writer).Run();
                    }

                    previousReader = r2r;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: " + e.ToString());
                if (e is ArgumentException)
                {
                    Console.WriteLine();
                    Console.WriteLine(syntax.GetHelpText());
                }
                if (_xml)
                {
                    XmlDocument document = new XmlDocument();
                    XmlNode     node     = document.CreateNode("element", "Error", "");
                    node.InnerText = e.Message;
                    document.AppendChild(node);
                    if (_writer != null)
                    {
                        document.Save(_writer);
                    }
                }
                return(1);
            }
            finally
            {
                if (disassembler != null)
                {
                    disassembler.Dispose();
                }
                // close output stream
                _writer.Close();
            }

            return(0);
        }
Example #5
0
        private int Run()
        {
            Disassembler disassembler = null;

            try
            {
                if (_options.In == null || _options.In.Length == 0)
                {
                    throw new ArgumentException("Input filename must be specified (--in <file>)");
                }

                if (_options.Diff && _options.In.Length < 2)
                {
                    throw new ArgumentException("Need at least 2 input files in diff mode");
                }

                if (_options.Naked && _options.Raw)
                {
                    throw new ArgumentException("The option '--naked' is incompatible with '--raw'");
                }

                Dumper previousDumper = null;

                foreach (FileInfo filename in _options.In)
                {
                    // parse the ReadyToRun image
                    ReadyToRunReader r2r = new ReadyToRunReader(_options, filename.FullName);

                    if (_options.Disasm)
                    {
                        if (r2r.InputArchitectureSupported() && r2r.DisassemblerArchitectureSupported())
                        {
                            disassembler = new Disassembler(r2r, _options);
                        }
                        else
                        {
                            throw new ArgumentException($"The architecture of input file {filename} ({r2r.Machine.ToString()}) or the architecture of the disassembler tools ({System.Runtime.InteropServices.RuntimeInformation.ProcessArchitecture.ToString()}) is not supported.");
                        }
                    }


                    if (!_options.Diff)
                    {
                        // output the ReadyToRun info
                        _dumper = new TextDumper(r2r, _writer, disassembler, _options);
                        Dump(r2r);
                    }
                    else
                    {
                        string perFileOutput = filename.FullName + ".common-methods.r2r";
                        _dumper = new TextDumper(r2r, new StreamWriter(perFileOutput, append: false, _encoding), disassembler, _options);
                        if (previousDumper != null)
                        {
                            new R2RDiff(previousDumper, _dumper, _writer).Run();
                        }
                        previousDumper?.Writer?.Flush();
                        previousDumper = _dumper;
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: " + e.ToString());
                if (e is ArgumentException)
                {
                    Console.WriteLine();
                }
                return(1);
            }
            finally
            {
                if (disassembler != null)
                {
                    disassembler.Dispose();
                }
                // flush output stream
                _dumper?.Writer?.Flush();
                _writer?.Flush();
            }

            return(0);
        }
Example #6
0
        public XmlDumper(bool ignoreSensitive, R2RReader r2r, TextWriter writer, bool raw, bool header, bool disasm, Disassembler disassembler, bool unwind, bool gc, bool sectionContents)
        {
            _ignoreSensitive = ignoreSensitive;
            _r2r             = r2r;
            _writer          = writer;
            XmlDocument      = new XmlDocument();

            _raw             = raw;
            _header          = header;
            _disasm          = disasm;
            _disassembler    = disassembler;
            _unwind          = unwind;
            _gc              = gc;
            _sectionContents = sectionContents;

            _ignoredProperties = new XmlAttributeOverrides();
            XmlAttributes attrs = new XmlAttributes();

            attrs.XmlIgnore = _ignoreSensitive;
            _ignoredProperties.Add(typeof(R2RHeader), "RelativeVirtualAddress", attrs);
            _ignoredProperties.Add(typeof(R2RHeader), "Size", attrs);
            _ignoredProperties.Add(typeof(R2RImportSection), "SectionRVA", attrs);
            _ignoredProperties.Add(typeof(R2RImportSection), "SectionSize", attrs);
            _ignoredProperties.Add(typeof(R2RImportSection), "EntrySize", attrs);
            _ignoredProperties.Add(typeof(R2RImportSection), "SignatureRVA", attrs);
            _ignoredProperties.Add(typeof(R2RImportSection), "AuxiliaryDataRVA", attrs);
            _ignoredProperties.Add(typeof(R2RImportSection.ImportSectionEntry), "SignatureSample", attrs);
            _ignoredProperties.Add(typeof(R2RImportSection.ImportSectionEntry), "SignatureRVA", attrs);
            _ignoredProperties.Add(typeof(RuntimeFunction), "StartAddress", attrs);
            _ignoredProperties.Add(typeof(RuntimeFunction), "UnwindRVA", attrs);
            _ignoredProperties.Add(typeof(R2RSection), "RelativeVirtualAddress", attrs);
            _ignoredProperties.Add(typeof(R2RSection), "Size", attrs);
        }
Example #7
0
        private int Run()
        {
            Disassembler disassembler = null;

            try
            {
                if (_options.In == null || _options.In.Length == 0)
                {
                    throw new ArgumentException("Input filename must be specified (--in <file>)");
                }

                if (_options.Diff && _options.In.Length < 2)
                {
                    throw new ArgumentException("Need at least 2 input files in diff mode");
                }

                if (_options.Naked && _options.Raw)
                {
                    throw new ArgumentException("The option '--naked' is incompatible with '--raw'");
                }

                R2RReader previousReader = null;

                foreach (FileInfo filename in _options.In)
                {
                    // parse the ReadyToRun image
                    R2RReader r2r = new R2RReader(_options, filename.FullName);

                    if (_options.Disasm)
                    {
                        if (r2r.InputArchitectureSupported() && r2r.DisassemblerArchitectureSupported())
                        {
                            disassembler = new Disassembler(r2r, _options);
                        }
                        else
                        {
                            throw new ArgumentException($"The architecture of input file {filename} ({r2r.Machine.ToString()}) or the architecture of the disassembler tools ({System.Runtime.InteropServices.RuntimeInformation.ProcessArchitecture.ToString()}) is not supported.");
                        }
                    }

                    if (_options.Xml)
                    {
                        _dumper = new XmlDumper(_options.IgnoreSensitive, r2r, _writer, disassembler, _options);
                    }
                    else
                    {
                        _dumper = new TextDumper(r2r, _writer, disassembler, _options);
                    }

                    if (!_options.Diff)
                    {
                        // output the ReadyToRun info
                        Dump(r2r);
                    }
                    else if (previousReader != null)
                    {
                        new R2RDiff(previousReader, r2r, _writer).Run();
                    }

                    previousReader = r2r;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: " + e.ToString());
                if (e is ArgumentException)
                {
                    Console.WriteLine();
                }
                if (_options.Xml)
                {
                    XmlDocument document = new XmlDocument();
                    XmlNode     node     = document.CreateNode("element", "Error", "");
                    node.InnerText = e.Message;
                    document.AppendChild(node);
                    if (_writer != null)
                    {
                        document.Save(_writer);
                    }
                }
                return(1);
            }
            finally
            {
                if (disassembler != null)
                {
                    disassembler.Dispose();
                }
                // close output stream
                _writer.Close();
            }

            return(0);
        }
Example #8
0
        private int Run(string[] args)
        {
            ArgumentSyntax syntax = ParseCommandLine(args);

            // open output stream
            if (_outputFilename != null)
            {
                _writer = File.CreateText(_outputFilename);
            }
            else
            {
                _writer = Console.Out;
            }

            if (_help)
            {
                _writer.WriteLine(syntax.GetHelpText());
                return(0);
            }

            Disassembler disassembler = null;

            try
            {
                if (_inputFilenames.Count == 0)
                {
                    throw new ArgumentException("Input filename must be specified (--in <file>)");
                }

                if (_diff && _inputFilenames.Count < 2)
                {
                    throw new ArgumentException("Need at least 2 input files in diff mode");
                }

                R2RReader previousReader = null;

                foreach (string filename in _inputFilenames)
                {
                    // parse the ReadyToRun image
                    R2RReader r2r = new R2RReader(filename);

                    if (_disasm)
                    {
                        // TODO: Fix R2RDump issue where an x64 R2R image cannot be dissassembled with the x86 CoreDisTools
                        // For the short term, we want to error out with a decent message explaining the unexpected error
                        // Issue #19564: https://github.com/dotnet/coreclr/issues/19564
                        if (r2r.InputArchitectureMatchesDisassemblerArchitecture())
                        {
                            disassembler = new Disassembler(r2r.Image, r2r.Machine);
                        }
                        else
                        {
                            throw new ArgumentException($"The architecture of input file {filename} is {r2r.Machine.ToString()} and does not match the architecture of the disassembler tools {System.Runtime.InteropServices.RuntimeInformation.ProcessArchitecture.ToString()}");
                        }
                    }

                    if (_xml)
                    {
                        _dumper = new XmlDumper(_ignoreSensitive, r2r, _writer, _raw, _header, _disasm, disassembler, _unwind, _gc, _sectionContents);
                    }
                    else
                    {
                        _dumper = new TextDumper(r2r, _writer, _raw, _header, _disasm, disassembler, _unwind, _gc, _sectionContents);
                    }

                    if (!_diff)
                    {
                        // output the ReadyToRun info
                        Dump(r2r);
                    }
                    else if (previousReader != null)
                    {
                        new R2RDiff(previousReader, r2r, _writer).Run();
                    }

                    previousReader = r2r;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: " + e.ToString());
                if (e is ArgumentException)
                {
                    Console.WriteLine();
                    Console.WriteLine(syntax.GetHelpText());
                }
                if (_xml)
                {
                    XmlDocument document = new XmlDocument();
                    XmlNode     node     = document.CreateNode("element", "Error", "");
                    node.InnerText = e.Message;
                    document.AppendChild(node);
                    if (_writer != null)
                    {
                        document.Save(_writer);
                    }
                }
                return(1);
            }
            finally
            {
                if (disassembler != null)
                {
                    disassembler.Dispose();
                }
                // close output stream
                _writer.Close();
            }

            return(0);
        }