Esempio n. 1
0
 /// <summary>
 /// Adds all string constants from <paramref name="fakeStringTable"/> table into this one.
 /// </summary>
 /// <param name="fakeStringTable">Additional string table for merging with this one.</param>
 internal void MergeValues(
     TinyStringTable fakeStringTable)
 {
     foreach (var item in _stringSorter.Sort(fakeStringTable._idsByStrings.Keys))
     {
         GetOrCreateStringId(item, false);
     }
 }
Esempio n. 2
0
        /// <summary>
        /// Creates new instance of <see cref="Mono.Cecil.Cil.CodeWriter"/> object.
        /// </summary>
        /// <param name="method">Original method body in Mono.Cecil format.</param>
        /// <param name="writer">Binary writer for writing byte code in correct endianess.</param>
        /// <param name="stringTable">String references table (for obtaining string ID).</param>
        /// <param name="context">
        /// Assembly tables context - contains all tables used for building target assembly.
        /// </param>
        public CodeWriter(
	        MethodDefinition method,
            TinyBinaryWriter writer,
            TinyStringTable stringTable,
            TinyTablesContext context)
	    {
            _stringTable = stringTable;

            _body = method.Body;
            _context = context;
            _writer = writer;
        }
Esempio n. 3
0
        /// <summary>
        /// Fixes instructions offsets according .NET Micro Framework operands sizes.
        /// </summary>
        /// <param name="methodDefinition">Target method for fixing offsets</param>
        /// <param name="stringTable">String table for populating strings from method.</param>
        public static IEnumerable <Tuple <UInt32, UInt32> > PreProcessMethod(
            MethodDefinition methodDefinition,
            TinyStringTable stringTable)
        {
            if (!methodDefinition.HasBody)
            {
                yield break;
            }

            var offset        = 0;
            var offsetChanged = true;

            foreach (var instruction in methodDefinition.Body.Instructions)
            {
                if (offset != 0)
                {
                    if (offsetChanged)
                    {
                        yield return(new Tuple <UInt32, UInt32>(
                                         (UInt32)instruction.Offset, (UInt32)(instruction.Offset + offset)));

                        offsetChanged = false;
                    }
                    instruction.Offset += offset;
                }

                switch (instruction.OpCode.OperandType)
                {
                case OperandType.InlineSwitch:
                    var targets = (Instruction[])instruction.Operand;
                    offset       -= 3;                  // One bye used instead of Int32
                    offset       -= 2 * targets.Length; // each target use Int16 instead of Int32
                    offsetChanged = true;
                    break;

                case OperandType.InlineString:
                    stringTable.GetOrCreateStringId((String)instruction.Operand, false);
                    offset       -= 2;
                    offsetChanged = true;
                    break;

                case OperandType.InlineMethod:
                case OperandType.InlineField:
                case OperandType.InlineType:
                case OperandType.InlineBrTarget:
                    // In full .NET these instructions followed by double word operand
                    // but in .NET Micro Framework these instruction's operand are word
                    offset       -= 2;
                    offsetChanged = true;
                    break;
                }
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Creates new instance of <see cref="Mono.Cecil.Cil.CodeWriter"/> object.
        /// </summary>
        /// <param name="method">Original method body in Mono.Cecil format.</param>
        /// <param name="writer">Binary writer for writing byte code in correct endianess.</param>
        /// <param name="stringTable">String references table (for obtaining string ID).</param>
        /// <param name="context">
        /// Assembly tables context - contains all tables used for building target assembly.
        /// </param>
        public CodeWriter(
            MethodDefinition method,
            TinyBinaryWriter writer,
            TinyStringTable stringTable,
            TinyTablesContext context)
        {
            _stringTable = stringTable;

            _body    = method.Body;
            _context = context;
            _writer  = writer;
        }
Esempio n. 5
0
        /// <summary>
        /// Fixes instructions offsets according .NET Micro Framework operands sizes.
        /// </summary>
        /// <param name="methodDefinition">Target method for fixing offsets</param>
        /// <param name="stringTable">String table for populating strings from method.</param>
        public static IEnumerable<Tuple<UInt32, UInt32>> PreProcessMethod(
            MethodDefinition methodDefinition,
            TinyStringTable stringTable)
        {
            if (!methodDefinition.HasBody)
            {
                yield break;
            }

            var offset = 0;
            var offsetChanged = true;
            foreach (var instruction in methodDefinition.Body.Instructions)
            {
                if (offset != 0)
                {
                    if (offsetChanged)
                    {
                        yield return new Tuple<UInt32, UInt32>(
                            (UInt32)instruction.Offset, (UInt32)(instruction.Offset + offset));
                        offsetChanged = false;
                    }
                    instruction.Offset += offset;
                }

                switch (instruction.OpCode.OperandType)
                {
                    case OperandType.InlineSwitch:
		                var targets = (Instruction[]) instruction.Operand;
                        offset -= 3; // One bye used instead of Int32
		                offset -= 2 * targets.Length; // each target use Int16 instead of Int32
                        offsetChanged = true;
                        break;
                    case OperandType.InlineString:
                        stringTable.GetOrCreateStringId((String) instruction.Operand, false);
                        offset -= 2;
                        offsetChanged = true;
                        break;
                    case OperandType.InlineMethod:
                    case OperandType.InlineField:
                    case OperandType.InlineType:
                    case OperandType.InlineBrTarget:
                        // In full .NET these instructions followed by double word operand
                        // but in .NET Micro Framework these instruction's operand are word
                        offset -= 2;
                        offsetChanged = true;
                        break;
                }
            }
        }
 /// <summary>
 /// Adds all string constants from <paramref name="fakeStringTable"/> table into this one.
 /// </summary>
 /// <param name="fakeStringTable">Additional string table for merging with this one.</param>
 internal void MergeValues(
     TinyStringTable fakeStringTable)
 {
     foreach (var item in _stringSorter.Sort(fakeStringTable._idsByStrings.Keys))
     {
         GetOrCreateStringId(item, false);
     }
 }
        public TinyTablesContext(
            AssemblyDefinition assemblyDefinition,
            List<String> explicitTypesOrder,
            ICustomStringSorter stringSorter,
            Boolean applyAttributesCompression)
        {
            AssemblyDefinition = assemblyDefinition;

            foreach (var item in assemblyDefinition.CustomAttributes)
            {
                _ignoringAttributes.Add(item.AttributeType.FullName);
            }

            NativeMethodsCrc = new NativeMethodsCrc(assemblyDefinition);

            var mainModule = AssemblyDefinition.MainModule;

            // External references

            AssemblyReferenceTable = new TinyAssemblyReferenceTable(
                mainModule.AssemblyReferences, this);

            var typeReferences = mainModule.GetTypeReferences()
                .Where(item => !IsAttribute(item))
                .ToList();
            TypeReferencesTable = new TinyTypeReferenceTable(
                typeReferences, this);

            var typeReferencesNames = new HashSet<String>(
                typeReferences.Select(item => item.FullName),
                StringComparer.Ordinal);
            var memberReferences = mainModule.GetMemberReferences()
                .Where(item => typeReferencesNames.Contains(item.DeclaringType.FullName))
                .ToList();
            FieldReferencesTable = new TinyFieldReferenceTable(
                memberReferences.OfType<FieldReference>(), this);
            MethodReferencesTable = new TinyMethodReferenceTable(
                memberReferences.OfType<MethodReference>(), this);

            // Internal types definitions

            var types = GetOrderedTypes(mainModule, explicitTypesOrder);

            TypeDefinitionTable = new TinyTypeDefinitionTable(types, this);
            
            var fields = types
                .SelectMany(item => GetOrderedFields(item.Fields.Where(field => !field.HasConstant)))
                .ToList();
            FieldsTable = new TinyFieldDefinitionTable(fields, this);

            var methods = types.SelectMany(item => GetOrderedMethods(item.Methods)).ToList();

            MethodDefinitionTable = new TinyMethodDefinitionTable(methods, this);

            AttributesTable = new TinyAttributesTable(
                GetAttributes(types, applyAttributesCompression),
                GetAttributes(fields, applyAttributesCompression),
                GetAttributes(methods, applyAttributesCompression),
                this);

            TypeSpecificationsTable = new TinyTypeSpecificationsTable(this);

            // Resources information

            ResourcesTable = new TinyResourcesTable(
                mainModule.Resources, this);
            ResourceDataTable = new TinyResourceDataTable();

            // Strings and signatures

            SignaturesTable = new TinySignaturesTable(this);
            StringTable = new TinyStringTable(stringSorter);

            // Byte code table
            ByteCodeTable = new TinyByteCodeTable(this);

            // Additional information

            ResourceFileTable = new TinyResourceFileTable(this);

            // Pre-allocate strings from some tables
            AssemblyReferenceTable.AllocateStrings();
            TypeReferencesTable.AllocateStrings();
            foreach (var item in memberReferences)
            {
                StringTable.GetOrCreateStringId(item.Name);
                
                var fieldReference = item as FieldReference;
                if (fieldReference != null)
                {
                    SignaturesTable.GetOrCreateSignatureId(fieldReference);
                }

                var methodReference = item as MethodReference;
                if (methodReference != null)
                {
                    SignaturesTable.GetOrCreateSignatureId(methodReference);
                }
            }
        }
        public TinyTablesContext(
            AssemblyDefinition assemblyDefinition,
            List <String> explicitTypesOrder,
            ICustomStringSorter stringSorter,
            Boolean applyAttributesCompression)
        {
            AssemblyDefinition = assemblyDefinition;

            var assemblyAttributes = new HashSet <String>(
                assemblyDefinition.CustomAttributes.Select(item => item.AttributeType.FullName),
                StringComparer.Ordinal)
            {
                "System.Reflection.AssemblyCultureAttribute",
                "System.Reflection.AssemblyVersionAttribute",
                "System.Runtime.CompilerServices.MethodImplAttribute",
                "System.Runtime.CompilerServices.MethodImplOptions",
                "System.Runtime.InteropServices.StructLayoutAttribute",
                "System.Runtime.InteropServices.OutAttribute",
                "System.Runtime.InteropServices.LayoutKind",
                "System.SerializableAttribute",
                "System.Runtime.CompilerServices.ExtensionAttribute",
                "System.Diagnostics.DebuggerBrowsableAttribute",
                "System.Diagnostics.DebuggerBrowsableState",
                "System.Diagnostics.DebuggerHiddenAttribute",
                "System.Diagnostics.ConditionalAttribute",
                "System.ParamArrayAttribute"
            };

            NativeMethodsCrc = new NativeMethodsCrc(assemblyDefinition);

            var mainModule = AssemblyDefinition.MainModule;

            // External references

            AssemblyReferenceTable = new TinyAssemblyReferenceTable(
                mainModule.AssemblyReferences, this);

            var typeReferences = mainModule.GetTypeReferences()
                                 .Where(item => !IsAttribute(item, assemblyAttributes))
                                 .ToList();

            TypeReferencesTable = new TinyTypeReferenceTable(
                typeReferences, this);

            var typeReferencesNames = new HashSet <String>(
                typeReferences.Select(item => item.FullName),
                StringComparer.Ordinal);
            var memberReferences = mainModule.GetMemberReferences()
                                   .Where(item => typeReferencesNames.Contains(item.DeclaringType.FullName))
                                   .ToList();

            FieldReferencesTable = new TinyFieldReferenceTable(
                memberReferences.OfType <FieldReference>(), this);
            MethodReferencesTable = new TinyMethodReferenceTable(
                memberReferences.OfType <MethodReference>(), this);

            // Internal types definitions

            var types = GetOrderedTypes(mainModule, explicitTypesOrder);

            TypeDefinitionTable = new TinyTypeDefinitionTable(types, this);

            var fields = types
                         .SelectMany(item => GetOrderedFields(item.Fields.Where(field => !field.HasConstant)))
                         .ToList();

            FieldsTable = new TinyFieldDefinitionTable(fields, this);

            var methods = types.SelectMany(item => GetOrderedMethods(item.Methods)).ToList();

            MethodDefinitionTable = new TinyMethodDefinitionTable(methods, this);

            var ignoringAttributes = new HashSet <String>(StringComparer.Ordinal)
            {
                "System.Runtime.CompilerServices.ExtensionAttribute",
                "System.Diagnostics.DebuggerBrowsableAttribute",
                "System.Diagnostics.DebuggerBrowsableState",
                "System.Diagnostics.DebuggerHiddenAttribute",
                "System.Diagnostics.ConditionalAttribute",
                "System.ParamArrayAttribute"
            };

            AttributesTable = new TinyAttributesTable(
                GetAttributes(types, applyAttributesCompression, ignoringAttributes),
                GetAttributes(fields, applyAttributesCompression, ignoringAttributes),
                GetAttributes(methods, applyAttributesCompression, ignoringAttributes),
                this);

            TypeSpecificationsTable = new TinyTypeSpecificationsTable(this);

            // Resources information

            ResourcesTable = new TinyResourcesTable(
                mainModule.Resources, this);
            ResourceDataTable = new TinyResourceDataTable();

            // Strings and signatures

            SignaturesTable = new TinySignaturesTable(this);
            StringTable     = new TinyStringTable(stringSorter);

            // Byte code table
            ByteCodeTable = new TinyByteCodeTable(this);

            // Additional information

            ResourceFileTable = new TinyResourceFileTable(this);

            // Pre-allocate strings from some tables
            AssemblyReferenceTable.AllocateStrings();
            TypeReferencesTable.AllocateStrings();
            foreach (var item in memberReferences)
            {
                StringTable.GetOrCreateStringId(item.Name);

                var fieldReference = item as FieldReference;
                if (fieldReference != null)
                {
                    SignaturesTable.GetOrCreateSignatureId(fieldReference);
                }

                var methodReference = item as MethodReference;
                if (methodReference != null)
                {
                    SignaturesTable.GetOrCreateSignatureId(methodReference);
                }
            }
        }
        public TinyTablesContext(
            AssemblyDefinition assemblyDefinition,
            List <String> explicitTypesOrder,
            ICustomStringSorter stringSorter,
            Boolean applyAttributesCompression)
        {
            AssemblyDefinition = assemblyDefinition;

            foreach (var item in assemblyDefinition.CustomAttributes)
            {
                _ignoringAttributes.Add(item.AttributeType.FullName);
            }

            NativeMethodsCrc = new NativeMethodsCrc(assemblyDefinition);

            var mainModule = AssemblyDefinition.MainModule;

            // External references

            AssemblyReferenceTable = new TinyAssemblyReferenceTable(
                mainModule.AssemblyReferences, this);

            var typeReferences = mainModule.GetTypeReferences()
                                 .Where(item => !IsAttribute(item))
                                 .ToList();

            TypeReferencesTable = new TinyTypeReferenceTable(
                typeReferences, this);

            var typeReferencesNames = new HashSet <String>(
                typeReferences.Select(item => item.FullName),
                StringComparer.Ordinal);
            var memberReferences = mainModule.GetMemberReferences()
                                   .Where(item => typeReferencesNames.Contains(item.DeclaringType.FullName))
                                   .ToList();

            FieldReferencesTable = new TinyFieldReferenceTable(
                memberReferences.OfType <FieldReference>(), this);
            MethodReferencesTable = new TinyMethodReferenceTable(
                memberReferences.OfType <MethodReference>(), this);

            // Internal types definitions

            var types = GetOrderedTypes(mainModule, explicitTypesOrder);

            TypeDefinitionTable = new TinyTypeDefinitionTable(types, this);

            var fields = types
                         .SelectMany(item => GetOrderedFields(item.Fields.Where(field => !field.HasConstant)))
                         .ToList();

            FieldsTable = new TinyFieldDefinitionTable(fields, this);

            var methods = types.SelectMany(item => GetOrderedMethods(item.Methods)).ToList();

            MethodDefinitionTable = new TinyMethodDefinitionTable(methods, this);

            AttributesTable = new TinyAttributesTable(
                GetAttributes(types, applyAttributesCompression),
                GetAttributes(fields, applyAttributesCompression),
                GetAttributes(methods, applyAttributesCompression),
                this);

            TypeSpecificationsTable = new TinyTypeSpecificationsTable(this);

            // Resources information

            ResourcesTable = new TinyResourcesTable(
                mainModule.Resources, this);
            ResourceDataTable = new TinyResourceDataTable();

            // Strings and signatures

            SignaturesTable = new TinySignaturesTable(this);
            StringTable     = new TinyStringTable(stringSorter);

            // Byte code table
            ByteCodeTable = new TinyByteCodeTable(this);

            // Additional information

            ResourceFileTable = new TinyResourceFileTable(this);

            // Pre-allocate strings from some tables
            AssemblyReferenceTable.AllocateStrings();
            TypeReferencesTable.AllocateStrings();
            foreach (var item in memberReferences)
            {
                StringTable.GetOrCreateStringId(item.Name);

                var fieldReference = item as FieldReference;
                if (fieldReference != null)
                {
                    SignaturesTable.GetOrCreateSignatureId(fieldReference);
                }

                var methodReference = item as MethodReference;
                if (methodReference != null)
                {
                    SignaturesTable.GetOrCreateSignatureId(methodReference);
                }
            }
        }