Beispiel #1
0
        internal static FileReference Load(Module module, int rid)
        {
            var image = module.Image;

            var file = image.FileSignatures[rid - 1];

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

            FileRow row;

            image.GetFile(rid, out row);

            file                   = new FileReference();
            file._name             = image.GetString(row.Name);
            file._hashValue        = image.GetBlob(row.HashValue);
            file._containsMetadata = (row.Flags == FileFlags.ContainsMetaData);

            module.AddSignature(ref file);
            image.FileSignatures[rid - 1] = file;

            return(file);
        }
Beispiel #2
0
        internal static AssemblyReference LoadDef(Module module)
        {
            var image = module.Image;

            var assemblyRef = image.AssemblyDefSignature;

            if (assemblyRef == null)
            {
                return(assemblyRef);
            }

            AssemblyRow row;

            image.GetAssembly(1, out row);

            assemblyRef          = new AssemblyReference();
            assemblyRef._name    = image.GetString(row.Name);
            assemblyRef._culture = image.GetString(row.Locale);
            assemblyRef._processorArchitecture = (ProcessorArchitecture)((int)(row.Flags & AssemblyFlags.PA_Mask) >> (int)AssemblyFlags.PA_Shift);
            assemblyRef._version = new Version(row.MajorVersion, row.MinorVersion, row.BuildNumber, row.RevisionNumber);

            var publicKey = image.GetBlob(row.PublicKey);

            if (publicKey != null && publicKey.Length > 0)
            {
                assemblyRef._publicKeyToken = StrongNameUtils.CreateTokenFromPublicKey(publicKey);
            }

            module.AddSignature(ref assemblyRef);
            image.AssemblyDefSignature = assemblyRef;

            return(assemblyRef);
        }
Beispiel #3
0
        internal static TypeReference LoadTypeRef(Module module, int rid, bool?isValueType = null)
        {
            var image = module.Image;

            var typeRef = image.TypeRefSignatures[rid - 1];

            if (typeRef != null)
            {
                if (isValueType.HasValue)
                {
                    typeRef._isValueType = isValueType;
                }

                return(typeRef);
            }

            TypeRefRow row;

            image.GetTypeRef(rid, out row);

            int resolutionScopeToken = MetadataToken.DecompressResolutionScope(row.ResolutionScope);

            typeRef              = new TypeReference();
            typeRef._name        = image.GetString(row.Name);
            typeRef._namespace   = image.GetString(row.Namespace);
            typeRef._owner       = LoadResolutionScope(module, resolutionScopeToken);
            typeRef._isValueType = isValueType;

            module.AddSignature(ref typeRef);
            image.TypeRefSignatures[rid - 1] = typeRef;

            return(typeRef);
        }
Beispiel #4
0
        internal static TypeReference LoadExportedType(Module module, int rid)
        {
            var image = module.Image;

            var typeRef = image.ExportedTypeSignatures[rid - 1];

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

            ExportedTypeRow row;

            image.GetExportedType(rid, out row);

            typeRef              = new TypeReference();
            typeRef._name        = image.GetString(row.TypeName);
            typeRef._namespace   = image.GetString(row.TypeNamespace);
            typeRef._owner       = LoadExportedTypeImplementation(module, MetadataToken.DecompressImplementation(row.Implementation));
            typeRef._isValueType = false;

            module.AddSignature(ref typeRef);
            image.ExportedTypeSignatures[rid - 1] = typeRef;

            return(typeRef);
        }
Beispiel #5
0
        internal static MethodReference LoadMethodDef(Module module, int rid)
        {
            var image = module.Image;

            var methodRef = image.MethodSignatures[rid - 1] as MethodReference;

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

            MethodRow row;

            image.GetMethod(rid, out row);

            methodRef = new MethodReference();

            methodRef._name = image.GetString(row.Name);

            int typeRID = image.GetTypeByMethod(rid);

            methodRef._owner = TypeReference.LoadTypeDef(module, typeRID);

            using (var accessor = image.OpenBlob(row.Signature))
            {
                methodRef._callSite = CallSite.LoadCallSite(accessor, module);
            }

            module.AddSignature(ref methodRef);
            image.MethodSignatures[rid - 1] = methodRef;

            return(methodRef);
        }
Beispiel #6
0
        internal static TypeSignature LoadTypeSpec(Module module, int rid)
        {
            var image = module.Image;

            var typeSig = image.TypeSpecSignatures[rid - 1];

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

            int blobID = image.GetTypeSpec(rid);

            using (var accessor = image.OpenBlob(blobID))
            {
                // Note that a TypeSpecBlob does not begin with a callingconvention byte,
                // so it differs from the various other signatures that are stored into Metadata.
                typeSig = Load(accessor, module);
            }

            module.AddSignature(ref typeSig);
            image.TypeSpecSignatures[rid - 1] = typeSig;

            return(typeSig);
        }
Beispiel #7
0
        internal static TypeReference LoadTypeDef(Module module, int rid, bool?isValueType = null)
        {
            var image = module.Image;

            var typeRef = image.TypeSignatures[rid - 1];

            if (typeRef != null)
            {
                if (isValueType.HasValue)
                {
                    typeRef._isValueType = isValueType;
                }

                return(typeRef);
            }

            TypeDefRow row;

            image.GetTypeDef(rid, out row);

            typeRef            = new TypeReference();
            typeRef._name      = image.GetString(row.Name);
            typeRef._namespace = image.GetString(row.Namespace);

            // Owner
            int enclosingRID;

            if (image.GetEnclosingTypeByNested(rid, out enclosingRID))
            {
                typeRef._owner = LoadTypeDef(module, enclosingRID);
            }
            else
            {
                typeRef._owner = ModuleReference.LoadDef(module);
            }

            module.AddSignature(ref typeRef);
            image.TypeSignatures[rid - 1] = typeRef;

            // Is value type.
            if (isValueType.HasValue)
            {
                typeRef._isValueType = isValueType.Value;
            }
            else
            {
                int extendsToken = MetadataToken.DecompressTypeDefOrRef(row.Extends);
                if (!MetadataToken.IsNull(extendsToken))
                {
                    var baseType = TypeSignature.Load(module, extendsToken);
                    typeRef._isValueType = GetIsValueType(typeRef, baseType, module);
                }
            }

            return(typeRef);
        }
        protected internal override void InternMembers(Module module)
        {
            module.AddSignature(ref _returnType);

            int count = _arguments.Count;

            if (count > 0)
            {
                var arguments = new TypeSignature[count];
                for (int i = 0; i < count; i++)
                {
                    var argument = _arguments[i];
                    module.AddSignature(ref argument);
                    arguments[i] = argument;
                }

                _arguments = ReadOnlyList <TypeSignature> .Create(arguments);
            }
        }
Beispiel #9
0
        protected internal override void InternMembers(Module module)
        {
            module.AddSignature(ref _declaringMethod);

            int count = _genericArguments.Count;

            if (count > 0)
            {
                var genericArguments = new TypeSignature[count];
                for (int i = 0; i < count; i++)
                {
                    var argument = _genericArguments[i];
                    module.AddSignature(ref argument);
                    genericArguments[i] = argument;
                }

                _genericArguments = ReadOnlyList <TypeSignature> .Create(genericArguments);
            }
        }
        private void BuildLocalVariables(Blob blob, ref int pos, Module module)
        {
            var list = LocalVariables;

            blob.Write7BitEncodedInt(ref pos, (int)list.Count);

            for (int i = 0; i < list.Count; i++)
            {
                var localVar = list[i];
                int rid      = module.AddSignature(ref localVar);
                blob.Write(ref pos, (int)rid);
            }
        }
Beispiel #11
0
        internal static AssemblyReference LoadRef(Module module, int rid)
        {
            var image = module.Image;

            var assemblyRef = image.AssemblyRefSignatures[rid - 1];

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

            AssemblyRefRow row;

            image.GetAssemblyRef(rid, out row);

            assemblyRef          = new AssemblyReference();
            assemblyRef._name    = image.GetString(row.Name);
            assemblyRef._culture = image.GetString(row.Locale);
            assemblyRef._processorArchitecture = (ProcessorArchitecture)((int)(row.Flags & AssemblyFlags.PA_Mask) >> (int)AssemblyFlags.PA_Shift);
            assemblyRef._version = new Version(row.MajorVersion, row.MinorVersion, row.BuildNumber, row.RevisionNumber);

            byte[] publicKeyToken = image.GetBlob(row.PublicKeyOrToken).NullIfEmpty();
            if ((row.Flags & AssemblyFlags.PublicKey) == AssemblyFlags.PublicKey &&
                publicKeyToken != null && publicKeyToken.Length > 0)
            {
                try
                {
                    publicKeyToken = StrongNameUtils.CreateTokenFromPublicKey(publicKeyToken);
                }
                catch (InvalidOperationException)
                {
                    publicKeyToken = null;
                }
            }

            assemblyRef._publicKeyToken = publicKeyToken;

            module.AddSignature(ref assemblyRef);
            image.AssemblyRefSignatures[rid - 1] = assemblyRef;

            return(assemblyRef);
        }
        private void BuildExceptionHandler(Blob blob, ref int pos, ExceptionHandler handler, Module module)
        {
            blob.Write(ref pos, (int)handler.Type);
            blob.Write(ref pos, (int)handler.TryOffset);
            blob.Write(ref pos, (int)handler.TryLength);
            blob.Write(ref pos, (int)handler.HandlerOffset);
            blob.Write(ref pos, (int)handler.HandlerLength);
            blob.Write(ref pos, (int)handler.FilterOffset);

            var catchType = handler.CatchType;

            if (catchType != null)
            {
                int rid = module.AddSignature(ref catchType);
                blob.Write(ref pos, (int)rid);
            }
            else
            {
                blob.Write(ref pos, (int)0);
            }
        }
Beispiel #13
0
        internal static FieldReference LoadFieldDef(Module module, int rid)
        {
            var image = module.Image;

            var fieldRef = image.FieldSignatures[rid - 1] as FieldReference;

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

            FieldRow row;

            image.GetField(rid, out row);

            fieldRef = new FieldReference();

            fieldRef._name = image.GetString(row.Name);

            int typeRID = image.GetTypeByField(rid);

            fieldRef._owner = TypeReference.LoadTypeDef(module, typeRID);

            using (var accessor = image.OpenBlob(row.Signature))
            {
                byte sigType = accessor.ReadByte();
                if (sigType != Metadata.SignatureType.Field)
                {
                    throw new CodeModelException(string.Format(SR.AssemblyLoadError, module.Location));
                }

                fieldRef._fieldType = TypeSignature.Load(accessor, module);
            }

            module.AddSignature(ref fieldRef);
            image.FieldSignatures[rid - 1] = fieldRef;

            return(fieldRef);
        }
        internal static ModuleReference LoadDef(Module module)
        {
            var image = module.Image;

            var moduleRef = image.ModuleDefSignature;

            if (moduleRef == null)
            {
                return(moduleRef);
            }

            ModuleRow row;

            image.GetModule(1, out row);

            moduleRef       = new ModuleReference();
            moduleRef._name = image.GetString(row.Name);

            module.AddSignature(ref moduleRef);
            image.ModuleDefSignature = moduleRef;

            return(moduleRef);
        }
        internal static CallSite LoadStandAloneSig(Module module, int rid)
        {
            var image = module.Image;

            var callSite = image.StandAloneSigSignatures[rid - 1] as CallSite;

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

            int blobID = image.GetStandAloneSig(rid);

            using (var accessor = image.OpenBlob(blobID))
            {
                callSite = LoadCallSite(accessor, module);
            }

            module.AddSignature(ref callSite);
            image.StandAloneSigSignatures[rid - 1] = callSite;

            return(callSite);
        }
        internal static ModuleReference LoadRef(Module module, int rid)
        {
            var image = module.Image;

            var moduleRef = image.ModuleRefSignatures[rid - 1];

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

            ModuleRefRow row;

            image.GetModuleRef(rid, out row);

            moduleRef       = new ModuleReference();
            moduleRef._name = image.GetString(row.Name);

            module.AddSignature(ref moduleRef);
            image.ModuleRefSignatures[rid - 1] = moduleRef;

            return(moduleRef);
        }
Beispiel #17
0
        internal static GenericMethodReference LoadMethodSpec(Module module, int rid)
        {
            var image = module.Image;

            var genericMethodRef = image.MethodSpecSignatures[rid - 1] as GenericMethodReference;

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

            MethodSpecRow row;

            image.GetMethodSpec(rid, out row);

            genericMethodRef = new GenericMethodReference();

            genericMethodRef._declaringMethod = MethodReference.LoadMethodDefOrRef(module, MetadataToken.DecompressMethodDefOrRef(row.Method));

            using (var accessor = image.OpenBlob(row.Instantiation))
            {
                byte sigType = accessor.ReadByte();                 // Should be equal to SignatureType.GenericInst
                if (sigType != Metadata.SignatureType.GenericInst)
                {
                    throw new CodeModelException(string.Format(SR.AssemblyLoadError, module.Location));
                }

                var genericArguments = TypeSignature.LoadGenericArguments(accessor, module);
                genericMethodRef._genericArguments = ReadOnlyList <TypeSignature> .Create(genericArguments);
            }

            module.AddSignature(ref genericMethodRef);
            image.MethodSpecSignatures[rid - 1] = genericMethodRef;

            return(genericMethodRef);
        }
Beispiel #18
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);
        }
 protected internal override void InternMembers(Module module)
 {
     module.AddSignature(ref _elementType);
 }
 protected internal override void InternMembers(Module module)
 {
     module.AddSignature(ref _callSite);
 }
        private void BuildInstructions(Blob blob, ref int pos, Module module)
        {
            blob.Write7BitEncodedInt(ref pos, (int)Instructions.Count);

            foreach (var instruction in Instructions)
            {
                var opCode = instruction.OpCode;
                if (opCode.OpByte1 == 0xff)
                {
                    // one byte opcode
                    blob.Write(ref pos, (byte)opCode.OpByte2);
                }
                else
                {
                    // two bytes opcode
                    blob.Write(ref pos, (byte)opCode.OpByte1);
                    blob.Write(ref pos, (byte)opCode.OpByte2);
                }

                object value = instruction.Value;
                switch (opCode.OperandType)
                {
                case OperandType.InlineBrTarget:
                {
                    blob.Write(ref pos, (int)value);
                }
                break;

                case OperandType.InlineField:
                {
                    var signature = (Signature)value;
                    int rid       = module.AddSignature(ref signature);
                    blob.Write(ref pos, (int)rid);
                }
                break;

                case OperandType.InlineI:
                {
                    blob.Write(ref pos, (int)value);
                }
                break;

                case OperandType.InlineI8:
                {
                    blob.Write(ref pos, (long)value);
                }
                break;

                case OperandType.InlineMethod:
                {
                    var signature = (Signature)value;
                    int rid       = module.AddSignature(ref signature);
                    blob.Write(ref pos, (int)rid);
                }
                break;

                case OperandType.InlineR:
                {
                    blob.Write(ref pos, (double)value);
                }
                break;

                case OperandType.InlineSig:
                {
                    var signature = (Signature)value;
                    int rid       = module.AddSignature(ref signature);
                    blob.Write(ref pos, (int)rid);
                }
                break;

                case OperandType.InlineString:
                {
                    // Token of a userdefined string, whose RID portion is actually an offset in the #US blob stream.
                    blob.WriteLengthPrefixedString(ref pos, (string)value, Encoding.Unicode);
                }
                break;

                case OperandType.InlineSwitch:
                {
                    int[] branches = (int[])value;

                    blob.Write(ref pos, (int)branches.Length);

                    for (int i = 0; i < branches.Length; i++)
                    {
                        blob.Write(ref pos, (int)branches[i]);
                    }
                }
                break;

                case OperandType.InlineTok:
                {
                    var signature = (Signature)value;
                    int rid       = module.AddSignature(ref signature);
                    blob.Write(ref pos, (int)rid);
                }
                break;

                case OperandType.InlineType:
                {
                    var signature = (Signature)value;
                    int rid       = module.AddSignature(ref signature);
                    blob.Write(ref pos, (int)rid);
                }
                break;

                case OperandType.InlineVar:
                {
                    blob.Write(ref pos, (short)value);
                }
                break;

                case OperandType.ShortInlineBrTarget:
                {
                    blob.Write(ref pos, (sbyte)value);
                }
                break;

                case OperandType.ShortInlineI:
                {
                    blob.Write(ref pos, (byte)value);
                }
                break;

                case OperandType.ShortInlineR:
                {
                    blob.Write(ref pos, (float)value);
                }
                break;

                case OperandType.ShortInlineVar:
                {
                    blob.Write(ref pos, (byte)value);
                }
                break;
                }
            }
        }
Beispiel #22
0
 protected internal override void InternMembers(Module module)
 {
     module.AddSignature(ref _fieldType);
     module.AddSignature(ref _owner);
 }