Example #1
0
        public override void DecompileMethod(IMethod method, ITextOutput output, DecompilationOptions options)
        {
            PEFile module = method.ParentModule.PEFile;
            R2RReaderCacheEntry r2rReaderCacheEntry = GetReader(module.GetLoadedAssembly(), module);

            if (r2rReaderCacheEntry.r2rReader == null)
            {
                WriteCommentLine(output, r2rReaderCacheEntry.failureReason);
            }
            else
            {
                R2RReader reader  = r2rReaderCacheEntry.r2rReader;
                int       bitness = -1;
                if (reader.Machine == Machine.Amd64)
                {
                    bitness = 64;
                }
                else
                {
                    Debug.Assert(reader.Machine == Machine.I386);
                    bitness = 32;
                }
                foreach (var m in reader.R2RMethods)
                {
                    if (m.MethodHandle == method.MetadataToken)
                    {
                        // TODO: Indexing
                        foreach (RuntimeFunction runtimeFunction in m.RuntimeFunctions)
                        {
                            WriteCommentLine(output, m.SignatureString);
                            byte[] code = new byte[runtimeFunction.Size];
                            for (int i = 0; i < runtimeFunction.Size; i++)
                            {
                                code[i] = reader.Image[reader.GetOffset(runtimeFunction.StartAddress) + i];
                            }
                            Disassemble(output, code, bitness, (ulong)runtimeFunction.StartAddress);
                            output.WriteLine();
                        }
                    }
                }
            }
        }
Example #2
0
        public override ProjectId DecompileAssembly(LoadedAssembly assembly, ITextOutput output, DecompilationOptions options)
        {
            PEFile module = assembly.GetPEFileOrNull();
            R2RReaderCacheEntry r2rReaderCacheEntry = GetReader(assembly, module);

            if (r2rReaderCacheEntry.r2rReader == null)
            {
                WriteCommentLine(output, r2rReaderCacheEntry.failureReason);
            }
            else
            {
                R2RReader reader = r2rReaderCacheEntry.r2rReader;
                WriteCommentLine(output, "TODO - display ready to run information");
                // TODO: display other header information
                foreach (var method in reader.R2RMethods)
                {
                    WriteCommentLine(output, method.SignatureString);
                }
            }

            return(base.DecompileAssembly(assembly, output, options));
        }
Example #3
0
        public uint DecodeUnsigned(byte[] image, uint offset, ref uint pValue)
        {
            if (offset >= image.Length)
            {
                throw new System.BadImageFormatException("NativeArray offset out of bounds");
            }

            int  off = (int)offset;
            uint val = R2RReader.ReadByte(image, ref off);

            if ((val & 1) == 0)
            {
                pValue  = (val >> 1);
                offset += 1;
            }
            else if ((val & 2) == 0)
            {
                if (offset + 1 >= image.Length)
                {
                    throw new System.BadImageFormatException("NativeArray offset out of bounds");
                }

                pValue = (val >> 2) |
                         ((uint)R2RReader.ReadByte(image, ref off) << 6);
                offset += 2;
            }
            else if ((val & 4) == 0)
            {
                if (offset + 2 >= image.Length)
                {
                    throw new System.BadImageFormatException("NativeArray offset out of bounds");
                }

                pValue = (val >> 3) |
                         ((uint)R2RReader.ReadByte(image, ref off) << 5) |
                         ((uint)R2RReader.ReadByte(image, ref off) << 13);
                offset += 3;
            }
            else if ((val & 8) == 0)
            {
                if (offset + 3 >= image.Length)
                {
                    throw new System.BadImageFormatException("NativeArray offset out of bounds");
                }

                pValue = (val >> 4) |
                         ((uint)R2RReader.ReadByte(image, ref off) << 4) |
                         ((uint)R2RReader.ReadByte(image, ref off) << 12) |
                         ((uint)R2RReader.ReadByte(image, ref off) << 20);
                offset += 4;
            }
            else if ((val & 16) == 0)
            {
                pValue  = R2RReader.ReadUInt32(image, ref off);
                offset += 5;
            }
            else
            {
                throw new System.BadImageFormatException("NativeArray");
            }

            return(offset);
        }
Example #4
0
        public bool TryGetAt(byte[] image, uint index, ref uint pOffset)
        {
            if (index >= _nElements)
            {
                return(false);
            }

            uint offset = 0;

            if (_entryIndexSize == 0)
            {
                int i = (int)(_baseOffset + (index / _blockSize));
                offset = R2RReader.ReadByte(image, ref i);
            }
            else if (_entryIndexSize == 1)
            {
                int i = (int)(_baseOffset + 2 * (index / _blockSize));
                offset = R2RReader.ReadUInt16(image, ref i);
            }
            else
            {
                int i = (int)(_baseOffset + 4 * (index / _blockSize));
                offset = R2RReader.ReadUInt32(image, ref i);
            }
            offset += _baseOffset;

            for (uint bit = _blockSize >> 1; bit > 0; bit >>= 1)
            {
                uint val     = 0;
                uint offset2 = DecodeUnsigned(image, offset, ref val);
                if ((index & bit) != 0)
                {
                    if ((val & 2) != 0)
                    {
                        offset = offset + (val >> 2);
                        continue;
                    }
                }
                else
                {
                    if ((val & 1) != 0)
                    {
                        offset = offset2;
                        continue;
                    }
                }

                // Not found
                if ((val & 3) == 0)
                {
                    // Matching special leaf node?
                    if ((val >> 2) == (index & (_blockSize - 1)))
                    {
                        offset = offset2;
                        break;
                    }
                }
                return(false);
            }
            pOffset = offset;
            return(true);
        }