Exemplo n.º 1
0
        private MosaCustomAttribute.Argument ToMosaCAArgument(CAArgument arg)
        {
            var value = arg.Value;

            if (value is UTF8String)
            {
                value = ((UTF8String)value).String;
            }
            else if (value is TypeSig)
            {
                value = metadata.Loader.GetType((TypeSig)value);
            }
            else if (value is CAArgument[] valueArray)
            {
                var resultArray = new MosaCustomAttribute.Argument[valueArray.Length];
                for (int i = 0; i < resultArray.Length; i++)
                {
                    resultArray[i] = ToMosaCAArgument(valueArray[i]);
                }
            }

            return(new MosaCustomAttribute.Argument(metadata.Loader.GetType(arg.Type), value));
        }
Exemplo n.º 2
0
        private void ResolveCustomAttributes(MosaUnit.MutatorBase unit, IHasCustomAttribute obj)
        {
            foreach (var attr in obj.CustomAttributes)
            {
                MosaType   type     = metadata.Loader.GetType(attr.AttributeType.ToTypeSig());
                MethodDef  ctor     = ((IMethodDefOrRef)attr.Constructor).ResolveMethod();
                MosaMethod mosaCtor = null;
                foreach (var method in type.Methods)
                {
                    var desc = method.GetUnderlyingObject <UnitDesc <MethodDef, MethodSig> >();
                    if (desc.Token.Token == ctor.MDToken)
                    {
                        mosaCtor = method;
                        break;
                    }
                }
                if (mosaCtor == null)
                {
                    throw new AssemblyLoadException();
                }

                var values = new MosaCustomAttribute.Argument[attr.ConstructorArguments.Count];
                for (int i = 0; i < values.Length; i++)
                {
                    values[i] = ToMosaCAArgument(attr.ConstructorArguments[i]);
                }

                var namedArgs = new MosaCustomAttribute.NamedArgument[attr.NamedArguments.Count];
                for (int i = 0; i < namedArgs.Length; i++)
                {
                    var namedArg = attr.NamedArguments[i];
                    namedArgs[i] = new MosaCustomAttribute.NamedArgument(namedArg.Name, namedArg.IsField, ToMosaCAArgument(namedArg.Argument));
                }

                unit.CustomAttributes.Add(new MosaCustomAttribute(mosaCtor, values, namedArgs));
            }
        }
Exemplo n.º 3
0
        private MosaCustomAttribute.Argument ToMosaCAArgument(CAArgument arg)
        {
            var value = arg.Value;
            if (value is UTF8String)
            {
                value = ((UTF8String)value).String;
            }
            else if (value is TypeSig)
            {
                value = metadata.Loader.GetType((TypeSig)value);
            }
            else if (value is CAArgument[])
            {
                var valueArray = (CAArgument[])value;
                var resultArray = new MosaCustomAttribute.Argument[valueArray.Length];
                for (int i = 0; i < resultArray.Length; i++)
                    resultArray[i] = ToMosaCAArgument(valueArray[i]);
            }

            return new MosaCustomAttribute.Argument(metadata.Loader.GetType(arg.Type), value);
        }
Exemplo n.º 4
0
        private void ResolveCustomAttributes(MosaUnit.MutatorBase unit, IHasCustomAttribute obj)
        {
            foreach (var attr in obj.CustomAttributes)
            {
                MosaType type = metadata.Loader.GetType(attr.AttributeType.ToTypeSig());
                MethodDef ctor = ((IMethodDefOrRef)attr.Constructor).ResolveMethod();
                MosaMethod mosaCtor = null;
                foreach (var method in type.Methods)
                {
                    var desc = method.GetUnderlyingObject<UnitDesc<MethodDef, MethodSig>>();
                    if (desc.Token.Token == ctor.MDToken)
                    {
                        mosaCtor = method;
                        break;
                    }
                }
                if (mosaCtor == null)
                    throw new AssemblyLoadException();

                var values = new MosaCustomAttribute.Argument[attr.ConstructorArguments.Count];
                for (int i = 0; i < values.Length; i++)
                    values[i] = ToMosaCAArgument(attr.ConstructorArguments[i]);

                var namedArgs = new MosaCustomAttribute.NamedArgument[attr.NamedArguments.Count];
                for (int i = 0; i < namedArgs.Length; i++)
                {
                    var namedArg = attr.NamedArguments[i];
                    namedArgs[i] = new MosaCustomAttribute.NamedArgument(namedArg.Name, namedArg.IsField, ToMosaCAArgument(namedArg.Argument));
                }

                unit.CustomAttributes.Add(new MosaCustomAttribute(mosaCtor, values, namedArgs));
            }
        }
Exemplo n.º 5
0
        private LinkerSymbol CreateCustomAttributeArgument(string name, int count, string argName, MosaCustomAttribute.Argument arg, bool isField)
        {
            var attributeName = name + ":" + (argName ?? count.ToString());
            var symbolName    = Metadata.CustomAttributeArgument + attributeName;

            var customAttributeArgumentSymbol = Linker.GetSymbol(symbolName);

            if (customAttributeArgumentSymbol.Size != 0)
            {
                return(customAttributeArgumentSymbol);
            }

            customAttributeArgumentSymbol = Linker.DefineSymbol(symbolName, SectionKind.ROData, TypeLayout.NativePointerAlignment, 0);
            var writer1 = new EndianAwareBinaryWriter(customAttributeArgumentSymbol.Stream, Architecture.Endianness);

            // 1. Pointer to name (if named)
            if (argName != null)
            {
                var nameSymbol = EmitStringWithLength(Metadata.NameString + attributeName, argName);
                Linker.Link(LinkType.AbsoluteAddress, NativePatchType, customAttributeArgumentSymbol, writer1.Position, nameSymbol, 0);
            }
            writer1.WriteZeroBytes(TypeLayout.NativePointerSize);

            // 2. Is Argument A Field
            writer1.Write(isField, TypeLayout.NativePointerSize);

            // 3. Argument Type Pointer
            Linker.Link(LinkType.AbsoluteAddress, NativePatchType, customAttributeArgumentSymbol, writer1.Position, Metadata.TypeDefinition + arg.Type.FullName, 0);
            writer1.WriteZeroBytes(TypeLayout.NativePointerSize);

            // 4. Argument Size
            writer1.Write(ComputeArgumentSize(arg.Type, arg.Value), TypeLayout.NativePointerSize);

            // 5. Argument Value
            WriteArgument(writer1, customAttributeArgumentSymbol, arg.Type, arg.Value);

            return(customAttributeArgumentSymbol);
        }
Exemplo n.º 6
0
        private LinkerSymbol CreateCustomAttributeArgument(string symbolName, int count, string name, MosaCustomAttribute.Argument arg, bool isField)
        {
            string nameForSymbol = (name == null) ? count.ToString() : name;

            nameForSymbol = symbolName + ":" + nameForSymbol;
            var symbol  = Linker.CreateSymbol(nameForSymbol + Metadata.CustomAttributeArgument, SectionKind.ROData, TypeLayout.NativePointerAlignment, 0);
            var writer1 = new EndianAwareBinaryWriter(symbol.Stream, Architecture.Endianness);

            // 1. Pointer to name (if named)
            if (name != null)
            {
                var nameSymbol = EmitStringWithLength(nameForSymbol + Metadata.NameString, name);
                Linker.Link(LinkType.AbsoluteAddress, NativePatchType, symbol, (int)writer1.Position, 0, nameSymbol, 0);
            }
            writer1.WriteZeroBytes(TypeLayout.NativePointerSize);

            // 2. Is Argument A Field
            writer1.Write(isField, TypeLayout.NativePointerSize);

            // 3. Argument Type Pointer
            Linker.Link(LinkType.AbsoluteAddress, NativePatchType, symbol, (int)writer1.Position, 0, arg.Type.FullName + Metadata.TypeDefinition, SectionKind.ROData, 0);
            writer1.WriteZeroBytes(TypeLayout.NativePointerSize);

            // 4. Argument Size
            writer1.Write(ComputeArgumentSize(arg.Type, arg.Value), TypeLayout.NativePointerSize);

            // 5. Argument Value
            WriteArgument(writer1, symbol, arg.Type, arg.Value);

            // Return symbol for linker usage
            return(symbol);
        }