Beispiel #1
0
        /// <summary>
        /// Registers metadata token to TypeRef table from metadata scope A to metadata scope B.
        /// </summary>
        /// <param name="token">Token in metadata scope A.</param>
        /// <returns>Token in metadata scope B.</returns>
        private MetadataToken registerTypeRef(MetadataToken token)
        {
            string name;
            uint   scope, newToken;

            NewImporter.GetTypeRefProps(token.ToUInt32(), out name, out scope);
            MetadataToken assmToken = TranslateToken(new MetadataToken(scope));

            OldEmitter.CorMetaDataEmit.DefineTypeRefByName(assmToken.ToUInt32(), name, out newToken);
            return(new MetadataToken(newToken));
        }
Beispiel #2
0
        /// <summary>
        /// Registers metadata token to TypeSpec table from metadata scope A to metadata scope B.
        /// </summary>
        /// <param name="token">Token in metadata scope A.</param>
        /// <returns>Token in metadata scope B.</returns>
        private MetadataToken registerTypeSpec(MetadataToken token)
        {
            uint nToken;

            byte[]    signature = NewImporter.GetTypeSpec(token.ToUInt32());
            Signature sig       = new Signature(signature);

            sig.Migrate(this);
            signature = sig.Compress();
            OldEmitter.CorMetaDataEmit.GetTokenFromTypeSpec(signature, (uint)signature.Length, out nToken);
            return(new MetadataToken(nToken));
        }
Beispiel #3
0
        /// <summary>
        /// Registers metadata token to MemberRef table from metadata scope A to metadata scope B.
        /// </summary>
        /// <param name="token">Token in metadata scope A.</param>
        /// <returns>Token in metadata scope B.</returns>
        private MetadataToken registerMemberRef(MetadataToken token)
        {
            string name;

            byte[] sig;
            uint   ptk, nToken;

            NewImporter.GetMemberRefProps(token.ToUInt32(), out name, out ptk, out sig);
            MetadataToken scope = TranslateToken(new MetadataToken(ptk));
            Signature     signa = new Signature(sig);

            signa.Migrate(this);
            sig = signa.Compress();
            OldEmitter.CorMetaDataEmit.DefineMemberRef(scope.ToUInt32(), name, sig, (uint)sig.Length, out nToken);
            return(new MetadataToken(nToken));
        }
Beispiel #4
0
 void WriteMetadataToken(MetadataToken token)
 {
     WriteUInt32(token.ToUInt32());
 }
Beispiel #5
0
		void WriteMetadataToken (MetadataToken token)
		{
			WriteUInt32 (token.ToUInt32 ());
		}
Beispiel #6
0
 public void WriteForwardInfo(MetadataToken import_parent)
 {
     Write(CustomMetadataType.ForwardInfo, () => writer.WriteUInt32(import_parent.ToUInt32()));
 }
Beispiel #7
0
        /// <summary>
        /// This method returns the member with the specified metadata token in the given module. It supports more TokenTypes than the standard Cecil method.
        /// </summary>
        /// <param name="module"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        internal static IMetadataTokenProvider LookupTokenExtended(this ModuleDefinition module, MetadataToken token)
        {
            switch (token.TokenType)
            {
            case TokenType.TypeDef:
            case TokenType.TypeRef:
            case TokenType.MemberRef:
            case TokenType.Field:
            case TokenType.Method:
            case TokenType.MethodSpec:
            case TokenType.TypeSpec:
                return(module.LookupToken(token));

            default:
                IMetadataTokenProvider provider;
                var success = _tokenProviderCache.GetValue(module, InitializeTokenProviderCache).TryGetValue(token.ToUInt32(),
                                                                                                             out provider);
                return(success ? provider : null);
            }
        }
Beispiel #8
0
 private void WriteMetadataToken(MetadataToken token)
 {
     base.WriteUInt32(token.ToUInt32());
 }
Beispiel #9
0
 /// <summary>
 /// Used to be able to reference methods created in this round from code changed also in the same round. Must not be in cache,
 /// because it can be cleared.
 /// </summary>
 /// <param name="newToken">Token in new assembly.</param>
 /// <param name="registeredToken">Token in running assembly.</param>
 public void RegisterNewMethod(MetadataToken newToken, MetadataToken registeredToken)
 {
     addedMethods.Add(newToken.ToUInt32(), registeredToken);
 }
Beispiel #10
0
        /// <summary>
        /// Registers any metadata token from tables
        /// (MemberRef,UserString,TypeRef,Method,TypeDef,Field,ModuleRef,AssemblyRef)
        /// from metadata scope A to metadata scope B.
        /// </summary>
        /// <param name="token">Token in metadata scope A.(new assembly)</param>
        /// <returns>Token in metadata scope B.(running assembly)</returns>
        public MetadataToken TranslateToken(MetadataToken cecToken)
        {
            uint          token = cecToken.ToUInt32();
            MetadataToken new_token;

            // Look in cache first
            if (cache.TryGetValue(token, out new_token))
            {
                return(new_token);
            }
            switch (cecToken.TokenType)
            {
            case TokenType.MemberRef:
                new_token = registerMemberRef(cecToken);
                break;

            case TokenType.String:
            {
                uint   nToken;
                string str = NewImporter.GetUserString(token);
                OldEmitter.CorMetaDataEmit.DefineUserString(str, (uint)str.Length, out nToken);
                new_token = new MetadataToken(nToken);
            }
            break;

            case TokenType.TypeRef:
                new_token = registerTypeRef(cecToken);
                break;

            case TokenType.Method:
            {
                if (addedMethods.TryGetValue(token, out new_token))
                {
                    break;
                }
                string name; uint classTk; byte[] signature; uint attr; uint rva; uint flags;
                NewImporter.GetMethodProps(token, out name, out classTk, out attr, out signature, out rva, out flags);
                MetadataToken classToken = TranslateToken(new MetadataToken(classTk));
                Signature     sig        = new Signature(signature);
                sig.Migrate(this);
                signature = sig.Compress();
                new_token = new MetadataToken(OldImporter.FindMethod(classToken.ToUInt32(), name, signature));
            }
            break;

            case TokenType.Field:
            {
                FieldProps fProps    = NewImporter.GetFieldProps(token);
                uint       newMClass = TranslateToken(new MetadataToken(fProps.mClass)).ToUInt32();
                fProps.sigBlob = Signature.Migrate(fProps.sigBlob, this, 1);
                //metadata.OldEmitter.CorMetaDataEmit.DefineMemberRef(newMClass,fProps.fName,fProps.sigBlob,(uint)fProps.sigBlob.Length,out new_token);
                uint nCorToken = OldImporter.FindField(newMClass, fProps.fName, fProps.sigBlob);
                if (nCorToken == 0)
                {
                    OldEmitter.CorMetaDataEmit.DefineField(newMClass, fProps.fName, fProps.pdwAttr, fProps.sigBlob, (uint)fProps.sigBlob.Length, fProps.pdwCPlusTypeFlag, fProps.ppValue, (uint)fProps.ppValue.Length, out nCorToken);
                }
                new_token = new MetadataToken(nCorToken);
            }
            break;

            case TokenType.TypeDef:
                TypeDefProps tProps = NewImporter.GetTypeDefProps(token);
                new_token = new MetadataToken(OldImporter.FindTypeDef(tProps.tName));
                break;

            case TokenType.ModuleRef:
            {
                uint   nToken;
                string name = NewImporter.GetModuleRefProps(token);
                OldEmitter.CorMetaDataEmit.DefineModuleRef(name, out nToken);
                new_token = new MetadataToken(nToken);
            }
            break;

            case TokenType.AssemblyRef:
            {
                int index = manager.ResourceManager.NewAssembly.Modules[0].AssemblyReferences.FindIndex(delegate(AssemblyNameReference asmRef){
                        return(asmRef.MetadataToken.ToUInt32() == token);
                    });
                string scopeName = manager.ResourceManager.NewAssembly.Modules[0].AssemblyReferences[index].FullName;

                index = manager.ResourceManager.OldAssembly.Modules[0].AssemblyReferences.FindIndex(delegate(AssemblyNameReference asmRef){
                        return(asmRef.FullName == scopeName);
                    });
                if (index == -1)
                {
                    throw new TranslatingException("Assembly reference not found, maybe it wasn't in used in original version");
                }
                else
                {
                    new_token = manager.ResourceManager.OldAssembly.Modules[0].AssemblyReferences[index].MetadataToken;
                }
            }
            break;

            case TokenType.TypeSpec:
                new_token = registerTypeSpec(cecToken);
                break;

            case TokenType.MethodSpec:
                uint            new_token_cor;
                MethodSpecProps props = newImporter.GetMethodSpecProps(token);
                props.tkParent  = TranslateToken(new MetadataToken(props.tkParent)).ToUInt32();
                props.signature = Signature.Migrate(props.signature, this);
                OldEmitter.CorMetaDataEmit.DefineMethodSpec(props.tkParent, props.signature, (uint)props.signature.Length, out new_token_cor);
                new_token = new MetadataToken(new_token_cor);
                break;

            case TokenType.Property:
            default:
                throw new NotImplementedException();
            }
            // Add to cache
            cache.Add(token, new_token);
            return(new_token);
        }
Beispiel #11
0
        /// <summary>
        /// Stores byte representation of operand to the IL stream. If neccessary,
        /// translate metadata token to original metadata scope.
        /// </summary>
        /// <param name="operand">Operand, that is being processed.</param>
        private void processOperand(Object operand)
        {
            if (operand is Mono.Cecil.Cil.Operand)
            {
                //operand with token
                MetadataToken token = metadata.TranslateToken(((Mono.Cecil.Cil.Operand)operand).Token);
                buffer.AddRange(BitConverter.GetBytes(token.ToUInt32()));//uintToByteArray(token.ToUInt32()));
            }
            else if (operand != null)
            {
                // operand as value

                if (operand is SByte)
                {
                    System.SByte oper = (SByte)operand;
                    byte         a;
                    unchecked {
                        a = (byte)((IConvertible)oper).ToSByte(null);
                    }
                    buffer.Add(a);
                }
                else if (operand is Int32)
                {
                    Int32 oper = (Int32)operand;
                    buffer.AddRange(BitConverter.GetBytes(((IConvertible)oper).ToInt32(null)));
                }
                else if (operand is Int64)
                {
                    Int64 oper = (Int64)operand;
                    buffer.AddRange(BitConverter.GetBytes(((IConvertible)oper).ToInt64(null)));
                }
                else if (operand is Double)
                {
                    byte[] arr = BitConverter.GetBytes(((IConvertible)operand).ToDouble(null));
                    buffer.AddRange(arr);
                }
                else if (operand is Single)
                {
                    byte[] arr = BitConverter.GetBytes(((IConvertible)operand).ToSingle(null));
                    buffer.AddRange(arr);
                }
                else if (operand is UInt16)
                {
                    byte[] arr = BitConverter.GetBytes(((IConvertible)operand).ToUInt16(null));
                    buffer.AddRange(arr);
                }
                else if (operand is byte)
                {
                    buffer.Add((byte)operand);
                }
                else if (operand is Int32[])
                {
                    Int32[] oper = (Int32[])operand;
                    buffer.AddRange(BitConverter.GetBytes(((IConvertible)oper.Length).ToInt32(null)));
                    foreach (Int32 a in oper)
                    {
                        byte[] arr = BitConverter.GetBytes(((IConvertible)a).ToInt32(null));
                        buffer.AddRange(arr);
                    }
                }
                else
                {
                    throw new NotImplementedException();
                }
            }
        }
Beispiel #12
0
 public static string ToScopeId(this MetadataToken token)
 {
     return(token.ToUInt32().ToString("x8", CultureInfo.InvariantCulture));
 }
Beispiel #13
0
        private void PatchRawCode(ByteBuffer buffer, int code_size, CodeWriter writer)
        {
            MetadataBuilder metadata = writer.metadata;

            buffer.WriteBytes(ReadBytes(code_size));
            int position = buffer.position;

            buffer.position -= code_size;
            while (buffer.position < position)
            {
                byte   b = buffer.ReadByte();
                OpCode opCode;
                if (b != 254)
                {
                    opCode = OpCodes.OneByteOpCode[b];
                }
                else
                {
                    byte b2 = buffer.ReadByte();
                    opCode = OpCodes.TwoBytesOpCode[b2];
                }
                MetadataToken metadataToken;
                switch (opCode.OperandType)
                {
                case OperandType.ShortInlineBrTarget:
                case OperandType.ShortInlineI:
                case OperandType.ShortInlineVar:
                case OperandType.ShortInlineArg:
                    buffer.position++;
                    break;

                case OperandType.InlineVar:
                case OperandType.InlineArg:
                    buffer.position += 2;
                    break;

                case OperandType.InlineBrTarget:
                case OperandType.InlineI:
                case OperandType.ShortInlineR:
                    buffer.position += 4;
                    break;

                case OperandType.InlineI8:
                case OperandType.InlineR:
                    buffer.position += 8;
                    break;

                case OperandType.InlineSwitch:
                {
                    int num = buffer.ReadInt32();
                    buffer.position += num * 4;
                    break;
                }

                case OperandType.InlineString:
                {
                    string @string = GetString(new MetadataToken(buffer.ReadUInt32()));
                    buffer.position -= 4;
                    metadataToken    = new MetadataToken(TokenType.String, metadata.user_string_heap.GetStringIndex(@string));
                    buffer.WriteUInt32(metadataToken.ToUInt32());
                    break;
                }

                case OperandType.InlineSig:
                {
                    CallSite callSite = GetCallSite(new MetadataToken(buffer.ReadUInt32()));
                    buffer.position -= 4;
                    metadataToken    = writer.GetStandAloneSignature(callSite);
                    buffer.WriteUInt32(metadataToken.ToUInt32());
                    break;
                }

                case OperandType.InlineField:
                case OperandType.InlineMethod:
                case OperandType.InlineTok:
                case OperandType.InlineType:
                {
                    IMetadataTokenProvider provider = reader.LookupToken(new MetadataToken(buffer.ReadUInt32()));
                    buffer.position -= 4;
                    metadataToken    = metadata.LookupToken(provider);
                    buffer.WriteUInt32(metadataToken.ToUInt32());
                    break;
                }
                }
            }
        }
 public static string GetFullMetadataTokenString(MetadataToken mt)
 {
     return(UintToHexString(mt.ToUInt32(), 8));
 }
Beispiel #15
0
        private void PatchRawCode(ByteBuffer buffer, int code_size, CodeWriter writer)
        {
            OpCode          twoBytesOpCode;
            MetadataToken   standAloneSignature;
            MetadataBuilder metadataBuilder = writer.metadata;

            buffer.WriteBytes(base.ReadBytes(code_size));
            int num = buffer.position;

            buffer.position -= code_size;
            while (buffer.position < num)
            {
                byte num1 = buffer.ReadByte();
                if (num1 == 254)
                {
                    byte num2 = buffer.ReadByte();
                    twoBytesOpCode = OpCodes.TwoBytesOpCode[num2];
                }
                else
                {
                    twoBytesOpCode = OpCodes.OneByteOpCode[num1];
                }
                switch (twoBytesOpCode.OperandType)
                {
                case OperandType.InlineBrTarget:
                case OperandType.InlineI:
                case OperandType.ShortInlineR:
                {
                    buffer.position += 4;
                    continue;
                }

                case OperandType.InlineField:
                case OperandType.InlineMethod:
                case OperandType.InlineTok:
                case OperandType.InlineType:
                {
                    IMetadataTokenProvider metadataTokenProvider = this.reader.LookupToken(new MetadataToken(buffer.ReadUInt32()));
                    buffer.position    -= 4;
                    standAloneSignature = metadataBuilder.LookupToken(metadataTokenProvider);
                    buffer.WriteUInt32(standAloneSignature.ToUInt32());
                    continue;
                }

                case OperandType.InlineI8:
                case OperandType.InlineR:
                {
                    buffer.position += 8;
                    continue;
                }

                case OperandType.InlineSig:
                {
                    Mono.Cecil.CallSite callSite = this.GetCallSite(new MetadataToken(buffer.ReadUInt32()));
                    buffer.position    -= 4;
                    standAloneSignature = writer.GetStandAloneSignature(callSite);
                    buffer.WriteUInt32(standAloneSignature.ToUInt32());
                    continue;
                }

                case OperandType.InlineString:
                {
                    string str = this.GetString(new MetadataToken(buffer.ReadUInt32()));
                    buffer.position    -= 4;
                    standAloneSignature = new MetadataToken(Mono.Cecil.TokenType.String, metadataBuilder.user_string_heap.GetStringIndex(str));
                    buffer.WriteUInt32(standAloneSignature.ToUInt32());
                    continue;
                }

                case OperandType.InlineSwitch:
                {
                    int        num3       = buffer.ReadInt32();
                    ByteBuffer byteBuffer = buffer;
                    byteBuffer.position = byteBuffer.position + num3 * 4;
                    continue;
                }

                case OperandType.InlineVar:
                case OperandType.InlineArg:
                {
                    buffer.position += 2;
                    continue;
                }

                case OperandType.ShortInlineBrTarget:
                case OperandType.ShortInlineI:
                case OperandType.ShortInlineVar:
                case OperandType.ShortInlineArg:
                {
                    buffer.position++;
                    continue;
                }

                default:
                {
                    continue;
                }
                }
            }
        }