public MosaModule Load(ModuleDef moduleDef)
        {
            MosaModule mosaModule = metadata.Controller.CreateModule();

            using (var module = metadata.Controller.MutateModule(mosaModule))
            {
                module.UnderlyingObject = new UnitDesc <ModuleDef, object>(moduleDef, moduleDef, null);
                module.Name             = moduleDef.Name;
                module.Assembly         = moduleDef.Assembly.Name;

                foreach (var typeDef in moduleDef.GetTypes())
                {
                    Load(mosaModule, typeDef);
                }
            }
            metadata.Controller.AddModule(mosaModule);
            metadata.Cache.AddModule(mosaModule);
            LoadedUnits.Add(mosaModule);

            if (moduleDef.Assembly.IsCorLib())
            {
                CorLib = mosaModule;
            }

            return(mosaModule);
        }
Beispiel #2
0
        private LinkerSymbol CreateAssemblyDefinition(MosaModule module)
        {
            // Emit assembly name
            var assemblyNameSymbol = EmitStringWithLength(Metadata.NameString + module.Assembly, module.Assembly);

            // Emit assembly table
            var assemblyTableSymbol = Linker.DefineSymbol(Metadata.AssemblyDefinition + module.Assembly, SectionKind.ROData, TypeLayout.NativePointerAlignment, 0);
            var writer = new EndianAwareBinaryWriter(assemblyTableSymbol.Stream, Architecture.Endianness);

            // 1. Pointer to Assembly Name
            Linker.Link(LinkType.AbsoluteAddress, NativePatchType, assemblyTableSymbol, writer.Position, assemblyNameSymbol, 0);
            writer.WriteZeroBytes(TypeLayout.NativePointerSize);

            // 2. Pointer to Custom Attributes
            if (module.CustomAttributes.Count > 0)
            {
                var customAttributeListSymbol = CreateCustomAttributesTable(module);
                Linker.Link(LinkType.AbsoluteAddress, NativePatchType, assemblyTableSymbol, writer.Position, customAttributeListSymbol, 0);
            }
            writer.WriteZeroBytes(TypeLayout.NativePointerSize);

            // 3. Attributes - IsReflectionOnly (32bit length)
            uint flags = 0x0;

            if (module.IsReflectionOnly)
            {
                flags |= 0x1;
            }
            writer.Write(flags, TypeLayout.NativePointerSize);

            // 4. Number of Types
            uint count = 0;

            writer.WriteZeroBytes(4);

            // 5. Pointers to Types
            foreach (var type in module.Types.Values)
            {
                if (type.IsModule)
                {
                    continue;
                }

                var typeTableSymbol = CreateTypeDefinition(type, assemblyTableSymbol);

                // Link
                Linker.Link(LinkType.AbsoluteAddress, NativePatchType, assemblyTableSymbol, writer.Position, typeTableSymbol, 0);
                writer.WriteZeroBytes(TypeLayout.NativePointerSize);

                count++;
            }

            writer.Position = 3 * TypeLayout.NativePointerSize;
            writer.Write(count, TypeLayout.NativePointerSize);

            return(assemblyTableSymbol);
        }
Beispiel #3
0
        private TreeNode GetOrCreateNode(MosaModule module)
        {
            if (!map.TryGetValue(module, out TreeNode moduleNode))
            {
                moduleNode = new TreeNode(module.Name)
                {
                    Tag = module
                };

                map.Add(module, moduleNode);

                AddToTree(moduleNode, null);
            }

            return(moduleNode);
        }
Beispiel #4
0
        private TreeNode GetOrCreateNode(string name, MosaModule module)
        {
            name = (string.IsNullOrWhiteSpace(name)) ? "[No Namespace]" : name;

            string index = module + ":" + name;

            if (!map.TryGetValue(index, out TreeNode namespaceNode))
            {
                namespaceNode = new TreeNode(name);

                map.Add(index, namespaceNode);

                var moduleNode = GetOrCreateNode(module);

                AddToTree(namespaceNode, moduleNode);
            }

            return(namespaceNode);
        }
Beispiel #5
0
 public BuiltInTypes(TypeSystem typeSystem, MosaModule corlib)
 {
     Void     = typeSystem.GetTypeByName(corlib, "System", "Void");
     Boolean  = typeSystem.GetTypeByName(corlib, "System", "Boolean");
     Char     = typeSystem.GetTypeByName(corlib, "System", "Char");
     I1       = typeSystem.GetTypeByName(corlib, "System", "SByte");
     U1       = typeSystem.GetTypeByName(corlib, "System", "Byte");
     I2       = typeSystem.GetTypeByName(corlib, "System", "Int16");
     U2       = typeSystem.GetTypeByName(corlib, "System", "UInt16");
     I4       = typeSystem.GetTypeByName(corlib, "System", "Int32");
     U4       = typeSystem.GetTypeByName(corlib, "System", "UInt32");
     I8       = typeSystem.GetTypeByName(corlib, "System", "Int64");
     U8       = typeSystem.GetTypeByName(corlib, "System", "UInt64");
     R4       = typeSystem.GetTypeByName(corlib, "System", "Single");
     R8       = typeSystem.GetTypeByName(corlib, "System", "Double");
     String   = typeSystem.GetTypeByName(corlib, "System", "String");
     Object   = typeSystem.GetTypeByName(corlib, "System", "Object");
     I        = typeSystem.GetTypeByName(corlib, "System", "IntPtr");
     U        = typeSystem.GetTypeByName(corlib, "System", "UIntPtr");
     TypedRef = typeSystem.GetTypeByName(corlib, "System", "TypedReference");
     Pointer  = Void.ToUnmanagedPointer();
 }
Beispiel #6
0
 public BuiltInTypes(TypeSystem typeSystem, MosaModule corlib)
 {
     Void = typeSystem.GetTypeByName(corlib, "System", "Void");
     Boolean = typeSystem.GetTypeByName(corlib, "System", "Boolean");
     Char = typeSystem.GetTypeByName(corlib, "System", "Char");
     I1 = typeSystem.GetTypeByName(corlib, "System", "SByte");
     U1 = typeSystem.GetTypeByName(corlib, "System", "Byte");
     I2 = typeSystem.GetTypeByName(corlib, "System", "Int16");
     U2 = typeSystem.GetTypeByName(corlib, "System", "UInt16");
     I4 = typeSystem.GetTypeByName(corlib, "System", "Int32");
     U4 = typeSystem.GetTypeByName(corlib, "System", "UInt32");
     I8 = typeSystem.GetTypeByName(corlib, "System", "Int64");
     U8 = typeSystem.GetTypeByName(corlib, "System", "UInt64");
     R4 = typeSystem.GetTypeByName(corlib, "System", "Single");
     R8 = typeSystem.GetTypeByName(corlib, "System", "Double");
     String = typeSystem.GetTypeByName(corlib, "System", "String");
     Object = typeSystem.GetTypeByName(corlib, "System", "Object");
     I = typeSystem.GetTypeByName(corlib, "System", "IntPtr");
     U = typeSystem.GetTypeByName(corlib, "System", "UIntPtr");
     TypedRef = typeSystem.GetTypeByName(corlib, "System", "TypedReference");
     Pointer = Void.ToUnmanagedPointer();
 }
 public string LookupUserString(MosaModule module, uint id)
 {
     return(Cache.GetStringById(id));
 }
        private LinkerSymbol CreateAssemblyDefinition(MosaModule module)
        {
            // Emit assembly name
            var assemblyNameSymbol = EmitStringWithLength(module.Assembly + Metadata.NameString, module.Assembly);

            // Emit assembly table
            var assemblyTableSymbol = Linker.CreateSymbol(module.Assembly + Metadata.AssemblyDefinition, SectionKind.ROData, TypeLayout.NativePointerAlignment, 0);
            var writer1             = new EndianAwareBinaryWriter(assemblyTableSymbol.Stream, Architecture.Endianness);

            // 1. Pointer to Assembly Name
            Linker.Link(LinkType.AbsoluteAddress, NativePatchType, assemblyTableSymbol, (int)writer1.Position, 0, assemblyNameSymbol, 0);
            writer1.WriteZeroBytes(TypeLayout.NativePointerSize);

            // 2. Pointer to Custom Attributes
            if (module.CustomAttributes.Count > 0)
            {
                var customAttributeListSymbol = CreateCustomAttributesTable(module);
                Linker.Link(LinkType.AbsoluteAddress, NativePatchType, assemblyTableSymbol, (int)writer1.Position, 0, customAttributeListSymbol, 0);
            }
            writer1.WriteZeroBytes(TypeLayout.NativePointerSize);

            // 3. Attributes - IsReflectionOnly (32bit length)
            uint flags = 0x0;

            if (module.IsReflectionOnly)
            {
                flags |= 0x1;
            }
            writer1.Write(flags, TypeLayout.NativePointerSize);

            // 4. Number of Types
            uint count = 0;

            foreach (var type in module.Types.Values)
            {
                if (type.IsModule)
                {
                    continue;
                }

                count++;
            }
            writer1.Write(count, TypeLayout.NativePointerSize);

            // 5. Pointers to Types
            // Create the definitions along the way
            foreach (var type in module.Types.Values)
            {
                if (type.IsModule)
                {
                    continue;
                }

                // Run the type through the TypeLayout system
                //TypeLayout.ResolveType(type);

                var typeTableSymbol = CreateTypeDefinition(type, assemblyTableSymbol);

                // Link
                Linker.Link(LinkType.AbsoluteAddress, NativePatchType, assemblyTableSymbol, (int)writer1.Position, 0, typeTableSymbol, 0);
                writer1.WriteZeroBytes(TypeLayout.NativePointerSize);
            }

            // Return assemblyTableSymbol
            return(assemblyTableSymbol);
        }
Beispiel #9
0
 public string LookupUserString(MosaModule module, uint id)
 {
     return Cache.GetStringById(id);
 }
Beispiel #10
0
        public void Resolve()
        {
            foreach (var unit in metadata.Loader.LoadedUnits)
            {
                if (unit is MosaType)
                {
                    MosaType type = (MosaType)unit;
                    using (var mosaType = metadata.Controller.MutateType(type))
                    {
                        TypeDef typeDef = type.GetUnderlyingObject <UnitDesc <TypeDef, TypeSig> >().Definition;

                        if (typeDef.BaseType != null)
                        {
                            mosaType.BaseType = metadata.Loader.GetType(typeDef.BaseType.ToTypeSig());
                        }

                        if (typeDef.DeclaringType != null)
                        {
                            mosaType.DeclaringType = metadata.Loader.GetType(typeDef.DeclaringType.ToTypeSig());
                        }

                        if (typeDef.IsEnum)
                        {
                            mosaType.ElementType = metadata.Loader.GetType(typeDef.GetEnumUnderlyingType());
                        }

                        foreach (var iface in typeDef.Interfaces)
                        {
                            mosaType.Interfaces.Add(metadata.Loader.GetType(iface.Interface.ToTypeSig()));
                        }

                        if (typeDef.BaseType != null)
                        {
                            ResolveInterfacesInBaseTypes(mosaType, type.BaseType);
                        }
                    }
                    ResolveType(type);
                }
                else if (unit is MosaField || unit is MosaMethod || unit is MosaModule || unit is MosaProperty)
                {
                    resolveQueue.Enqueue(unit);
                }
            }

            while (resolveQueue.Count > 0)
            {
                MosaUnit unit = resolveQueue.Dequeue();
                if (unit is MosaType)
                {
                    ResolveType((MosaType)unit);
                }
                if (unit is MosaField)
                {
                    ResolveField((MosaField)unit);
                }
                if (unit is MosaMethod)
                {
                    ResolveMethod((MosaMethod)unit);
                }
                if (unit is MosaProperty)
                {
                    ResolveProperty((MosaProperty)unit);
                }
                if (unit is MosaModule)
                {
                    MosaModule module = (MosaModule)unit;
                    using (var mosaModule = metadata.Controller.MutateModule(module))
                        ResolveCustomAttributes(mosaModule, module.GetUnderlyingObject <UnitDesc <ModuleDef, object> >().Definition);
                }
            }

            foreach (var module in metadata.Cache.Modules.Values)
            {
                ModuleDef moduleDef = module.GetUnderlyingObject <UnitDesc <ModuleDef, object> >().Definition;
                if (moduleDef.EntryPoint != null)
                {
                    using (var mosaModule = metadata.Controller.MutateModule(module))
                        mosaModule.EntryPoint = metadata.Cache.GetMethodByToken(new ScopedToken(moduleDef, moduleDef.EntryPoint.MDToken));
                }
            }

            while (arrayResolveQueue.Count > 0)
            {
                MosaType type = arrayResolveQueue.Dequeue();
                type.FinishSZArray();
            }
        }
Beispiel #11
0
 public void AddModule(MosaModule module)
 {
     Modules.Add(module.Name, module);
     //var desc = module.GetUnderlyingObject<UnitDesc<ModuleDef, object>>();
 }
        private void Load(MosaModule module, TypeDef typeDef)
        {
            TypeSig typeSig = typeDef.ToTypeSig();

            // Check to see if its one of our classes we need for SZ Arrays
            if (typeDef.Name.Contains("SZArrayHelper`1"))
            {
                szHelperSig = typeSig as ClassOrValueTypeSig;
            }
            else if (typeDef.Name.Contains("IList`1"))
            {
                iListSig = typeSig as ClassOrValueTypeSig;
            }

            MosaType mosaType = metadata.Controller.CreateType();

            using (var type = metadata.Controller.MutateType(mosaType))
            {
                type.Module           = module;
                type.UnderlyingObject = new UnitDesc <TypeDef, TypeSig>(typeDef.Module, typeDef, typeSig);

                type.Namespace = typeDef.Namespace;
                type.Name      = typeDef.Name;

                type.IsInterface = typeDef.IsInterface;
                type.IsEnum      = typeDef.IsEnum;
                type.IsDelegate  =
                    typeDef.BaseType != null && typeDef.BaseType.DefinitionAssembly.IsCorLib() &&
                    (typeDef.BaseType.FullName == "System.Delegate" || typeDef.BaseType.FullName == "System.MulticastDelegate");
                type.IsModule = typeDef.IsGlobalModuleType;

                type.IsExplicitLayout = typeDef.IsExplicitLayout;
                if (typeDef.IsExplicitLayout)
                {
                    type.ClassSize   = (int)typeDef.ClassSize;
                    type.PackingSize = typeDef.PackingSize;
                }
                type.TypeAttributes = (MosaTypeAttributes)typeDef.Attributes;
                type.TypeCode       = (MosaTypeCode)typeSig.ElementType;

                // Load members

                foreach (var fieldDef in typeDef.Fields)
                {
                    MosaField mosaField = metadata.Controller.CreateField();

                    using (var field = metadata.Controller.MutateField(mosaField))
                        LoadField(mosaType, field, fieldDef);

                    type.Fields.Add(mosaField);
                    metadata.Cache.AddField(mosaField);
                    LoadedUnits.Add(mosaField);
                }

                foreach (var methodDef in typeDef.Methods)
                {
                    MosaMethod mosaMethod = metadata.Controller.CreateMethod();

                    using (var method = metadata.Controller.MutateMethod(mosaMethod))
                        LoadMethod(mosaType, method, methodDef);

                    type.Methods.Add(mosaMethod);
                    metadata.Cache.AddMethod(mosaMethod);
                    LoadedUnits.Add(mosaMethod);
                }

                foreach (var propertyDef in typeDef.Properties)
                {
                    MosaProperty mosaProperty = metadata.Controller.CreateProperty();

                    using (var property = metadata.Controller.MutateProperty(mosaProperty))
                        LoadProperty(mosaType, property, propertyDef);

                    type.Properties.Add(mosaProperty);
                    metadata.Cache.AddProperty(mosaProperty);
                    LoadedUnits.Add(mosaProperty);
                }
            }
            typeCache[typeSig] = mosaType;
            metadata.Controller.AddType(mosaType);
            metadata.Cache.AddType(mosaType);
            LoadedUnits.Add(mosaType);
        }
Beispiel #13
0
        public void AddModule(MosaModule module)
        {
            Modules.Add(module.Name, module);

            //var desc = module.GetUnderlyingObject<UnitDesc<ModuleDef, object>>();
        }