Exemplo n.º 1
0
        void PatchMethods()
        {
            MethodTable methodTable = (MethodTable)stripped_tables [MethodTable.RId];

            if (methodTable == null)
            {
                return;
            }

            RVA method_rva = RVA.Zero;

            for (int i = 0; i < methodTable.Rows.Count; i++)
            {
                MethodRow methodRow = methodTable[i];

                methodRow.ImplFlags |= MethodImplAttributes.NoInlining;

                MetadataToken methodToken = MetadataToken.FromMetadataRow(TokenType.Method, i);

                MethodDefinition method = (MethodDefinition)assembly.MainModule.LookupByToken(methodToken);

                if (method.HasBody)
                {
                    method_rva = method_rva != RVA.Zero
                                                ? method_rva
                                                : reflection_writer.CodeWriter.WriteMethodBody(method);

                    methodRow.RVA = method_rva;
                }
                else
                {
                    methodRow.RVA = RVA.Zero;
                }
            }
        }
        void ReadMethods()
        {
            if (!m_tHeap.HasTable(MethodTable.RId))
            {
                m_meths = new MethodDefinition [0];
                return;
            }

            MethodTable mTable = m_tableReader.GetMethodTable();

            m_meths = new MethodDefinition [mTable.Rows.Count];
            for (int i = 0; i < mTable.Rows.Count; i++)
            {
                MethodRow        mRow = mTable [i];
                MethodDefinition meth = new MethodDefinition(
                    m_root.Streams.StringsHeap [mRow.Name],
                    mRow.Flags);
                meth.RVA            = mRow.RVA;
                meth.ImplAttributes = mRow.ImplFlags;

                meth.MetadataToken = MetadataToken.FromMetadataRow(TokenType.Method, i);

                m_meths [i] = meth;
            }
        }
Exemplo n.º 3
0
        public MethodIL GetMethodBody(int num)
        {
            MethodIL il = null;

            if (img == null)
            {
                return(il);
            }
            MDStream   tabs     = Streams["#~"] as MDStream;
            TablesHeap tabsHeap = tabs.Heap as TablesHeap;

            if (tabsHeap.HasMethod)
            {
                MDTable methods = tabsHeap[TableId.Method];
                if (methods == null)
                {
                    return(il);
                }
                MethodRow row = methods[num] as MethodRow;
                if (row == null)
                {
                    return(il);
                }
                BinaryReader reader = img.reader;
                reader.BaseStream.Position = img.RVAToVA(row.RVA);
                il = new MethodIL();
                il.Read(reader);
            }
            return(il);
        }
Exemplo n.º 4
0
 private MethodDefinitionResolver(uint rid, MethodRow row, MetadataSystem metadata, TypeDefinition declaringType)
 {
     _token         = new MetadataToken(MetadataTokenType.Method, rid);
     _row           = row;
     _metadata      = metadata;
     _declaringType = declaringType;
     _signature     = new Lazy <MethodSignatureReader>(() => new MethodSignatureReader(_row, _metadata));
 }
Exemplo n.º 5
0
 public override void FromRawData(byte [] buff, int offs, int numRows)
 {
     for (int i = numRows; --i >= 0;)
     {
         Row row = new MethodRow(this);
         row.FromRawData(buff, offs);
         Add(row);
         offs += MethodRow.LogicalSize;
     }
 }
Exemplo n.º 6
0
        private void ReadSignature(MethodRow row, MetadataSystem metadata)
        {
            var blob = metadata.ResolveBlob(row.Signature);
            var sig  = new StandAloneSignature(blob);

            CallingConventions = (CallingConventions)sig.ReadByte();
            ParamCount         = sig.ReadCompressedUInt32();
            ReturnType         = sig.ReadType(metadata);

            ParamTypes = new ITypeInfo[ParamCount];
            for (int index = 0; index < ParamCount; index++)
            {
                ParamTypes[index] = sig.ReadType(metadata);
            }
        }
Exemplo n.º 7
0
        public Collection <ParameterDefinition> GetParameters(MethodRow methodRow, MethodSignatureReader signature, MethodDefinition methodDef)
        {
            var start      = methodRow.ParamList;
            var count      = signature.ParamCount;
            var parameters = new ParameterDefinition[count];

            for (uint offset = 0; offset < count; offset++)
            {
                var paramRid  = start + offset;
                var paramRow  = _reader.ParameterTable[paramRid];
                var paramType = signature.ParamTypes[paramRow.Sequence - 1];
                parameters[offset] = ParameterDefinitionResolver.Resolve(paramRid, paramRow, this, methodDef, paramType);
            }

            return(new Collection <ParameterDefinition>(parameters));
        }
Exemplo n.º 8
0
        private MethodRow CreateMethodRow(MethodDefinition entity, MetadataBuilder metadata)
        {
            var row = new MethodRow(
                metadata.WriteMethod(entity),
                entity.ImplAttributes,
                entity.Attributes,
                metadata.WriteString(entity.Name),
                metadata.WriteSignature(entity),
                (ushort)(metadata.ParamTable.Length + 1)
                );

            foreach (var param in entity.Parameters)
            {
                metadata.ResolveToken(param);
            }

            return(row);
        }
Exemplo n.º 9
0
        void PatchMethods()
        {
            MethodTable methodTable = (MethodTable)stripped_tables [MethodTable.RId];

            if (methodTable == null)
            {
                return;
            }

            for (int i = 0; i < methodTable.Rows.Count; i++)
            {
                MethodRow methodRow = methodTable[i];

                MetadataToken methodToken = MetadataToken.FromMetadataRow(TokenType.Method, i);

                MethodDefinition method = (MethodDefinition)assembly.MainModule.LookupByToken(methodToken);

                methodRow.RVA = reflection_writer.CodeWriter.WriteMethodBody(method);
            }
        }
Exemplo n.º 10
0
 public virtual void VisitMethodRow(MethodRow row)
 {
 }
        void CompleteMethods()
        {
            TypeDefTable tdefTable = m_tableReader.GetTypeDefTable();

            if (!m_tHeap.HasTable(MethodTable.RId))
            {
                m_meths = new MethodDefinition [0];
                return;
            }

            MethodTable methTable  = m_tableReader.GetMethodTable();
            ParamTable  paramTable = m_tableReader.GetParamTable();

            if (!m_tHeap.HasTable(ParamTable.RId))
            {
                m_parameters = new ParameterDefinition [0];
            }
            else
            {
                m_parameters = new ParameterDefinition [paramTable.Rows.Count];
            }

            for (int i = 0; i < m_typeDefs.Length; i++)
            {
                TypeDefinition dec = m_typeDefs [i];

                int index = i, next;

                if (index == tdefTable.Rows.Count - 1)
                {
                    next = methTable.Rows.Count + 1;
                }
                else
                {
                    next = (int)(tdefTable [index + 1]).MethodList;
                }

                for (int j = (int)tdefTable [index].MethodList; j < next; j++)
                {
                    MethodRow        methRow = methTable [j - 1];
                    MethodDefinition mdef    = m_meths [j - 1];
                    if (mdef.IsConstructor)
                    {
                        dec.Constructors.Add(mdef);
                    }
                    else
                    {
                        dec.Methods.Add(mdef);
                    }
                    GenericContext context = new GenericContext(mdef);

                    MethodDefSig msig = m_sigReader.GetMethodDefSig(methRow.Signature);
                    mdef.HasThis           = msig.HasThis;
                    mdef.ExplicitThis      = msig.ExplicitThis;
                    mdef.CallingConvention = msig.MethCallConv;

                    int prms;
                    if (j == methTable.Rows.Count)
                    {
                        prms = m_parameters.Length + 1;
                    }
                    else
                    {
                        prms = (int)(methTable [j]).ParamList;
                    }

                    ParameterDefinition retparam = null;

                    //TODO: optimize this
                    ParamRow pRow  = null;
                    int      start = (int)methRow.ParamList - 1;

                    if (paramTable != null && start < prms - 1)
                    {
                        pRow = paramTable [start];
                    }

                    if (pRow != null && pRow.Sequence == 0)                       // ret type
                    {
                        retparam = new ParameterDefinition(
                            m_root.Streams.StringsHeap [pRow.Name],
                            0,
                            pRow.Flags,
                            null);
                        retparam.Method      = mdef;
                        m_parameters [start] = retparam;
                        start++;
                    }

                    for (int k = 0; k < msig.ParamCount; k++)
                    {
                        int pointer = start + k;

                        if (paramTable != null && pointer < prms - 1)
                        {
                            pRow = paramTable [pointer];
                        }

                        Param psig = msig.Parameters [k];

                        ParameterDefinition pdef;
                        if (pRow != null)
                        {
                            pdef = BuildParameterDefinition(
                                m_root.Streams.StringsHeap [pRow.Name],
                                pRow.Sequence, pRow.Flags, psig, context);
                            pdef.MetadataToken     = MetadataToken.FromMetadataRow(TokenType.Param, pointer);
                            m_parameters [pointer] = pdef;
                        }
                        else
                        {
                            pdef = BuildParameterDefinition(
                                string.Concat("A_", mdef.IsStatic ? k : k + 1),
                                k + 1, (ParamAttributes)0, psig, context);
                        }

                        pdef.Method = mdef;
                        mdef.Parameters.Add(pdef);
                    }

                    mdef.ReturnType = GetMethodReturnType(msig, context);
                    MethodReturnType mrt = mdef.ReturnType as MethodReturnType;
                    mrt.Method = mdef;
                    if (retparam != null)
                    {
                        mrt.Parameter = retparam;
                        mrt.Parameter.ParameterType = mrt.ReturnType;
                    }
                }
            }

            uint eprid = CodeReader.GetRid((int)m_reader.Image.CLIHeader.EntryPointToken);

            if (eprid > 0 && eprid <= m_meths.Length)
            {
                m_module.Assembly.EntryPoint = GetMethodDefAt(eprid);
            }
        }
Exemplo n.º 12
0
        public static MethodDefinition Resolve(uint rid, MethodRow row, MetadataSystem metadata, TypeDefinition declaringType)
        {
            var resolver = new MethodDefinitionResolver(rid, row, metadata, declaringType);

            return(new MethodDefinition(resolver));
        }
 public virtual void VisitMethodRow(MethodRow row)
 {
 }
Exemplo n.º 14
0
 public MethodSignatureReader(MethodRow row, MetadataSystem metadata)
 {
     ReadSignature(row, metadata);
 }