Exemple #1
0
        public R2RMethod(byte[] image, MetadataReader mdReader, NativeArray methodEntryPoints, uint offset, uint rid)
        {
            // get the id of the entry point runtime function from the MethodEntryPoints NativeArray
            Token = _mdtMethodDef | rid;
            uint id = 0; // the RUNTIME_FUNCTIONS index

            offset = methodEntryPoints.DecodeUnsigned(image, offset, ref id);
            if ((id & 1) != 0)
            {
                if ((id & 2) != 0)
                {
                    uint val = 0;
                    methodEntryPoints.DecodeUnsigned(image, offset, ref val);
                    offset -= val;
                }
                // TODO: Dump fixups

                id >>= 2;
            }
            else
            {
                id >>= 1;
            }
            EntryPointRuntimeFunctionId = id;
            NativeCode = new List <RuntimeFunction>();

            // get the method signature from the MethodDefhandle
            try
            {
                MethodDefinitionHandle methodDefHandle = MetadataTokens.MethodDefinitionHandle((int)rid);
                var             methodDef       = mdReader.GetMethodDefinition(methodDefHandle);
                BlobReader      signatureReader = mdReader.GetBlobReader(methodDef.Signature);
                SignatureHeader header          = signatureReader.ReadSignatureHeader();
                Name = mdReader.GetString(methodDef.Name);
                int argCount = signatureReader.ReadCompressedInteger();
                ReturnType = new SignatureType(ref signatureReader, ref mdReader);
                ArgTypes   = new SignatureType[argCount];
                for (int i = 0; i < argCount; i++)
                {
                    ArgTypes[i] = new SignatureType(ref signatureReader, ref mdReader);
                }
            }
            catch (System.BadImageFormatException)
            {
                R2RDump.OutputWarning("The method with rowId " + rid + " doesn't have a corresponding MethodDefHandle");
            }
        }
Exemple #2
0
        /// <summary>
        /// Initializes the fields of the R2RHeader
        /// </summary>
        /// <param name="image">PE image</param>
        /// <param name="rva">Relative virtual address of the ReadyToRun header</param>
        /// <param name="curOffset">Index in the image byte array to the start of the ReadyToRun header</param>
        /// <exception cref="BadImageFormatException">The signature must be 0x00525452</exception>
        public R2RHeader(byte[] image, int rva, int curOffset)
        {
            RelativeVirtualAddress = rva;
            int startOffset = curOffset;

            byte[] signature = new byte[sizeof(uint)];
            Array.Copy(image, curOffset, signature, 0, sizeof(uint));
            SignatureString = System.Text.Encoding.UTF8.GetString(signature);
            Signature       = NativeReader.ReadUInt32(image, ref curOffset);
            if (Signature != READYTORUN_SIGNATURE)
            {
                throw new System.BadImageFormatException("Incorrect R2R header signature");
            }

            MajorVersion = NativeReader.ReadUInt16(image, ref curOffset);
            MinorVersion = NativeReader.ReadUInt16(image, ref curOffset);
            Flags        = NativeReader.ReadUInt32(image, ref curOffset);
            int nSections = NativeReader.ReadInt32(image, ref curOffset);

            Sections = new Dictionary <R2RSection.SectionType, R2RSection>();

            for (int i = 0; i < nSections; i++)
            {
                int type        = NativeReader.ReadInt32(image, ref curOffset);
                var sectionType = (R2RSection.SectionType)type;
                if (!Enum.IsDefined(typeof(R2RSection.SectionType), type))
                {
                    R2RDump.OutputWarning("Invalid ReadyToRun section type");
                }
                Sections[sectionType] = new R2RSection(sectionType,
                                                       NativeReader.ReadInt32(image, ref curOffset),
                                                       NativeReader.ReadInt32(image, ref curOffset));
            }

            Size = curOffset - startOffset;
        }