예제 #1
0
        private Signature LoadEntryPoint()
        {
            _opFlags = _opFlags.SetBitAtIndex(LoadEntryPointFlag, false);

            int token = (int)_image.CorHeader.EntryPointToken;

            if (token == 0)
            {
                return(null);
            }

            int rid = MetadataToken.GetRID(token);

            if (rid == 0)
            {
                return(null);
            }

            switch (MetadataToken.GetType(token))
            {
            case MetadataTokenType.Method:
                return(MethodReference.LoadMethodDef(this, rid));

            case MetadataTokenType.File:
                return(FileReference.Load(this, rid));
            }

            return(null);
        }
예제 #2
0
        private static Signature LoadResolutionScope(Module module, int token)
        {
            int rid = MetadataToken.GetRID(token);

            if (rid == 0)
            {
                return(null);
            }

            switch (MetadataToken.GetType(token))
            {
            case MetadataTokenType.TypeRef:
                return(LoadTypeRef(module, rid));

            case MetadataTokenType.Assembly:                   // Current assembly.
            case MetadataTokenType.Module:                     // Current module.
                return(null);

            case MetadataTokenType.AssemblyRef:
                return(AssemblyReference.LoadRef(module, rid));

            case MetadataTokenType.ModuleRef:
                return(ModuleReference.LoadRef(module, rid));

            default:
                throw new AssemblyLoadException(string.Format(SR.AssemblyLoadError, module.Location));
            }
        }
예제 #3
0
        internal static FieldReference Load(Module module, int token)
        {
            switch (MetadataToken.GetType(token))
            {
            case MetadataTokenType.Field:
                return(LoadFieldDef(module, MetadataToken.GetRID(token)));

            case MetadataTokenType.MemberRef:
                return((FieldReference)MethodReference.LoadMemberRef(module, MetadataToken.GetRID(token)));

            default:
                throw new Exception(string.Format("Invalid token {0}. Expected FieldRef.", MetadataToken.GetType(token)));
            }
        }
예제 #4
0
        internal static TypeReference LoadTypeDefOrRef(Module module, int token, bool?isValueType = null)
        {
            switch (MetadataToken.GetType(token))
            {
            case MetadataTokenType.TypeRef:
                return(LoadTypeRef(module, MetadataToken.GetRID(token), isValueType));

            case MetadataTokenType.TypeDef:
                return(LoadTypeDef(module, MetadataToken.GetRID(token), isValueType));

            default:
                throw new InvalidDataException(string.Format("Invalid token {0}. Expected TypeDefOrRef.", MetadataToken.GetType(token)));
            }
        }
예제 #5
0
        internal static MethodReference LoadMethodDefOrRef(Module module, int token)
        {
            switch (MetadataToken.GetType(token))
            {
            case MetadataTokenType.Method:
                return(LoadMethodDef(module, MetadataToken.GetRID(token)));

            case MetadataTokenType.MemberRef:
                return((MethodReference)LoadMemberRef(module, MetadataToken.GetRID(token)));

            default:
                throw new Exception(string.Format("Invalid method reference token {0}", token.ToString()));
            }
        }
예제 #6
0
        internal static TypeSignature Load(Module module, int token)
        {
            switch (MetadataToken.GetType(token))
            {
            case MetadataTokenType.TypeRef:
                return(TypeReference.LoadTypeRef(module, MetadataToken.GetRID(token)));

            case MetadataTokenType.TypeDef:
                return(TypeReference.LoadTypeDef(module, MetadataToken.GetRID(token)));

            case MetadataTokenType.TypeSpec:
                return(LoadTypeSpec(module, MetadataToken.GetRID(token)));

            default:
                throw new InvalidDataException(string.Format("Invalid token {0}. Expected TypeDefOrRef.", MetadataToken.GetType(token)));
            }
        }
예제 #7
0
        protected void Load()
        {
            var image = _module.Image;

            ManifestResourceRow row;

            image.GetManifestResource(_rid, out row);

            _name       = image.GetString(row.Name);
            _visibility = (ResourceVisibilityFlags)row.Flags;
            _offset     = row.Offset;

            int implementationToken = MetadataToken.DecompressImplementation(row.Implementation);

            switch (MetadataToken.GetType(implementationToken))
            {
            case MetadataTokenType.AssemblyRef:
            {
                int rid = MetadataToken.GetRID(implementationToken);
                _owner = AssemblyReference.LoadRef(_module, rid);
            }
            break;

            case MetadataTokenType.File:
            {
                int rid = MetadataToken.GetRID(implementationToken);
                _owner = FileReference.Load(_module, rid);
            }
            break;

            default:
            {
                _opFlags = _opFlags.SetBitAtIndex(LoadDataFromImageFlag, true);
            }
            break;
            }
        }
        private void Load(IBinaryAccessor accessor, Module module)
        {
            int flags = accessor.ReadByte();

            if ((flags & ILMethodFlags.FormatMask) == ILMethodFlags.FatFormat)
            {
                // Fat format
                accessor.ReadByte();
                _maxStackSize = accessor.ReadUInt16();
                int codeSize = accessor.ReadInt32();

                _initLocals    = ((flags & ILMethodFlags.InitLocals) == ILMethodFlags.InitLocals);
                _localVarToken = accessor.ReadInt32();

                if (MetadataToken.GetType(_localVarToken) == MetadataTokenType.Signature)
                {
                    LoadLocalVariables(module, MetadataToken.GetRID(_localVarToken));
                }

                LoadInstructions(accessor, module, codeSize);

                if ((flags & ILMethodFlags.MoreSects) == ILMethodFlags.MoreSects)
                {
                    // More sections
                    LoadSection(accessor, module);
                }
            }
            else
            {
                // Tiny format
                // Used when the method is tiny (< 64 bytes), and there are no local vars
                _maxStackSize = 8;
                int codeSize = flags >> 2;
                LoadInstructions(accessor, module, codeSize);
            }
        }
예제 #9
0
        private static Signature LoadExportedTypeImplementation(Module module, int token)
        {
            int rid = MetadataToken.GetRID(token);

            if (rid == 0)
            {
                return(null);
            }

            switch (MetadataToken.GetType(token))
            {
            case MetadataTokenType.File:
                return(ModuleReference.LoadFile(module, rid));

            case MetadataTokenType.AssemblyRef:
                return(AssemblyReference.LoadRef(module, rid));

            case MetadataTokenType.ExportedType:
                return(LoadExportedType(module, rid));

            default:
                throw new AssemblyLoadException(string.Format(SR.AssemblyLoadError, module.Location));
            }
        }
        private void LoadInstructions(IBinaryAccessor accessor, Module module, int codeSize)
        {
            long startOffset = accessor.Position;

            var image = module.Image;

            _instructions = new List <Instruction>();

            while (accessor.Position < startOffset + codeSize)
            {
                OpCode opCode;
                byte   opByte = accessor.ReadByte();
                if (opByte == 0xFE)
                {
                    opByte = accessor.ReadByte();
                    opCode = OpCodes.OpCodeArray[256 + opByte];
                }
                else
                {
                    opCode = OpCodes.OpCodeArray[opByte];
                }

                if (opCode == null)
                {
                    throw new CodeModelException(string.Format(SR.AssemblyLoadError, module.Location));
                }

                object value;
                switch (opCode.OperandType)
                {
                case OperandType.InlineBrTarget:
                {
                    value = accessor.ReadInt32();
                }
                break;

                case OperandType.InlineField:
                {
                    int token = accessor.ReadInt32();
                    value = FieldReference.Load(module, token);
                }
                break;

                case OperandType.InlineI:
                {
                    value = accessor.ReadInt32();
                }
                break;

                case OperandType.InlineI8:
                {
                    value = accessor.ReadInt64();
                }
                break;

                case OperandType.InlineMethod:
                {
                    int token = accessor.ReadInt32();
                    value = MethodReference.Load(module, token);
                }
                break;

                case OperandType.InlineR:
                {
                    value = accessor.ReadDouble();
                }
                break;

                case OperandType.InlineSig:
                {
                    int token = accessor.ReadInt32();
                    if (MetadataToken.GetType(token) == MetadataTokenType.Signature)
                    {
                        int rid = MetadataToken.GetRID(token);
                        value = CallSite.LoadStandAloneSig(module, rid);
                    }
                    else
                    {
                        throw new CodeModelException(SR.MethodBodyBlobNotValid);
                    }
                }
                break;

                case OperandType.InlineString:
                {
                    // Token of a userdefined string, whose RID portion is actually an offset in the #US blob stream.
                    uint token = accessor.ReadUInt32();
                    int  rid   = (int)(token & 0x00ffffff);
                    value = image.GetUserString(rid);
                }
                break;

                case OperandType.InlineSwitch:
                {
                    int   count   = accessor.ReadInt32();
                    int[] targets = new int[count];
                    for (int i = 0; i < count; i++)
                    {
                        targets[i] = accessor.ReadInt32();
                    }

                    value = targets;
                }
                break;

                case OperandType.InlineTok:
                {
                    int token = accessor.ReadInt32();
                    int rid   = MetadataToken.GetRID(token);
                    switch (MetadataToken.GetType(token))
                    {
                    case MetadataTokenType.Method:
                        value = MethodReference.LoadMethodDef(module, rid);
                        break;

                    case MetadataTokenType.MethodSpec:
                        value = GenericMethodReference.LoadMethodSpec(module, rid);
                        break;

                    case MetadataTokenType.MemberRef:
                        value = MethodReference.LoadMemberRef(module, rid);
                        break;

                    case MetadataTokenType.Field:
                        value = FieldReference.LoadFieldDef(module, rid);
                        break;

                    case MetadataTokenType.TypeDef:
                        value = TypeReference.LoadTypeDef(module, rid);
                        break;

                    case MetadataTokenType.TypeRef:
                        value = TypeReference.LoadTypeRef(module, rid);
                        break;

                    case MetadataTokenType.TypeSpec:
                        value = TypeSignature.LoadTypeSpec(module, rid);
                        break;

                    default:
                        throw new CodeModelException(SR.MethodBodyBlobNotValid);
                    }
                }
                break;

                case OperandType.InlineType:
                {
                    int token = accessor.ReadInt32();
                    value = TypeSignature.Load(module, token);
                }
                break;

                case OperandType.InlineVar:
                {
                    value = accessor.ReadInt16();
                }
                break;

                case OperandType.ShortInlineBrTarget:
                {
                    value = accessor.ReadSByte();
                }
                break;

                case OperandType.ShortInlineI:
                {
                    value = accessor.ReadByte();
                }
                break;

                case OperandType.ShortInlineR:
                {
                    value = accessor.ReadSingle();
                }
                break;

                case OperandType.ShortInlineVar:
                {
                    value = accessor.ReadByte();
                }
                break;

                default:
                {
                    value = null;
                }
                break;
                }

                _instructions.Add(new Instruction(opCode, value));
            }
        }
예제 #11
0
        internal static Signature LoadMemberRef(Module module, int rid)
        {
            var image = module.Image;

            var memberRef = image.MemberRefSignatures[rid - 1] as Signature;

            if (memberRef != null)
            {
                return(memberRef);
            }

            MemberRefRow row;

            image.GetMemberRef(rid, out row);

            string name = image.GetString(row.Name);

            // Owner
            TypeSignature owner;
            int           classToken = MetadataToken.DecompressMemberRefParent(row.Class);

            switch (MetadataToken.GetType(classToken))
            {
            case MetadataTokenType.ModuleRef:
            {
                // A ModuleRef token, if the member is defined, in another module of the same image,
                // as a global function or variable.
                var moduleRef = ModuleReference.LoadRef(module, MetadataToken.GetRID(classToken));
                var typeRef   = new TypeReference(CodeModelUtils.GlobalTypeName, null, moduleRef);
                module.AddSignature(ref typeRef);
                owner = typeRef;
            }
            break;

            case MetadataTokenType.Method:
            {
                // A MethodDef token, when used to supply a call-site signature for a vararg method that is
                // defined in this module. The Name shall match the Name in the corresponding MethodDef row.
                // The Signature shall match the Signature in the target method definition.
                int typeRID = image.GetTypeByMethod(MetadataToken.GetRID(classToken));
                owner = TypeReference.LoadTypeDef(module, typeRID);
            }
            break;

            default:
            {
                owner = TypeReference.Load(module, classToken);
            }
            break;
            }

            // Signature
            using (var accessor = image.OpenBlob(row.Signature))
            {
                byte sigType = accessor.ReadByte();
                if (sigType == Metadata.SignatureType.Field)
                {
                    var fieldType = TypeSignature.Load(accessor, module);
                    memberRef = new FieldReference(name, fieldType, owner);
                }
                else
                {
                    var callSite = CallSite.LoadCallSite(accessor, module, sigType);
                    memberRef = new MethodReference(name, owner, callSite);
                }
            }

            module.AddSignature(ref memberRef);
            image.MemberRefSignatures[rid - 1] = memberRef;

            return(memberRef);
        }