Ejemplo n.º 1
0
        /// <summary>
        /// Enable the assembly resolver to get the right SOS.NETCore version (the one
        /// in the same directory as SOS.Hosting).
        /// </summary>
        static SOSHost()
        {
            AssemblyResolver.Enable();

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                s_coreclrModuleName = "coreclr";
            }
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                s_coreclrModuleName = "libcoreclr.so";
            }
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
            {
                s_coreclrModuleName = "libcoreclr.dylib";
            }

            // TODO: Support other architectures
            Type contextType   = typeof(AMD64Context);
            var  registerNames = new Dictionary <string, int>();
            var  offsets       = new List <int>();
            int  index         = 0;

            FieldInfo[] fields = contextType.GetFields(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic);
            foreach (FieldInfo field in fields)
            {
                registerNames.Add(field.Name.ToLower(), index);

                FieldOffsetAttribute attribute = field.GetCustomAttributes <FieldOffsetAttribute>(inherit: false).Single();
                offsets.Insert(index, attribute.Value);
                index++;
            }
            s_registersByName = registerNames;
            s_registerOffsets = offsets.ToArray();
        }
Ejemplo n.º 2
0
        protected FieldInfo GrabField(FieldInfo Field, TypeBuilder On)
        {
            if (Field == null)
            {
                return(null);
            }

            if (FieldsDone.ContainsKey(Field))
            {
                return(FieldsDone[Field]);
            }

            if (!Sources.Contains(Field.Module))
            {
                return(Field);
            }

            if (On == null)
            {
                On = GrabType(Field.DeclaringType) as TypeBuilder;
            }

            FieldBuilder CopiedField = On.DefineField(Field.Name, GrabType(Field.FieldType), Field.Attributes);

            FieldsDone.Add(Field, CopiedField);

            if (Field.IsLiteral && Field.DeclaringType.IsEnum)
            {
                CopiedField.SetConstant(Field.GetRawConstantValue());
            }

            FieldOffsetAttribute Attr = FindCustomAttribute <FieldOffsetAttribute>(Field);

            if (Attr != null)
            {
                CopiedField.SetOffset(Attr.Value);
            }

            // Fields like these mostly have a backing field that comes with them in the
            // <PrivateImplementationDetails> class of the assembly. This backing field
            // refers to a bit of data in the .sdata section, which is serialized in a
            // way not exposed by the .NET framework. The best thing we can do is to try
            // to replicate this format.
            if (Field.IsStatic && Field.IsInitOnly)
            {
                object Val     = Field.GetValue(null);
                Type   ValType = Val.GetType();

                if (ValType.IsArray)
                {
                    byte[]       Raw     = RawSerialize((Array)Val, ValType.GetElementType());
                    FieldBuilder Backing = ImplementationDetails.DefineInitializedData(Field.Name, Raw, Field.Attributes);

                    // This is used later on when we recognize the initilization pattern (see TryReplaceBackingField)
                    BackingFields.Add(CopiedField, Backing);
                }
            }

            return(CopiedField);
        }
Ejemplo n.º 3
0
 private void Init(FieldOffsetAttribute fieldOffset)
 {
     this.m_ctor = typeof(FieldOffsetAttribute).GetConstructors(BindingFlags.Public | BindingFlags.Instance)[0];
     CustomAttributeTypedArgument[] array = new CustomAttributeTypedArgument[] { new CustomAttributeTypedArgument(fieldOffset.Value) };
     this.m_typedCtorArgs = Array.AsReadOnly <CustomAttributeTypedArgument>(array);
     this.m_namedArgs     = Array.AsReadOnly <CustomAttributeNamedArgument>(new CustomAttributeNamedArgument[0]);
 }
Ejemplo n.º 4
0
 private void Init(FieldOffsetAttribute fieldOffset)
 {
     this.m_ctor          = typeof(FieldOffsetAttribute).GetConstructors(BindingFlags.Instance | BindingFlags.Public)[0];
     this.m_typedCtorArgs = (IList <CustomAttributeTypedArgument>)Array.AsReadOnly <CustomAttributeTypedArgument>(new CustomAttributeTypedArgument[1]
     {
         new CustomAttributeTypedArgument((object)fieldOffset.Value)
     });
     this.m_namedArgs = (IList <CustomAttributeNamedArgument>)Array.AsReadOnly <CustomAttributeNamedArgument>(new CustomAttributeNamedArgument[0]);
 }
Ejemplo n.º 5
0
        private Mock <FieldInfo> MockField(Type fieldType, int?fieldOffset = null)
        {
            var fieldInfoMock = new Mock <FieldInfo>();

            fieldInfoMock.Setup(i => i.FieldType).Returns(fieldType);

            if (fieldOffset.HasValue)
            {
                var fieldOffsetAttribute = new FieldOffsetAttribute(fieldOffset.Value);
                fieldInfoMock.Setup(f => f.GetCustomAttributes(typeof(FieldOffsetAttribute), true))
                .Returns(new[] { fieldOffsetAttribute });
            }

            return(fieldInfoMock);
        }
Ejemplo n.º 6
0
        // .Net implementation
        public static IntPtr fthelper_glyph_get_outline_address(IntPtr glyph)
        {
            if (OUTLINE_OFFSET == 0)
            {
                FieldOffsetAttribute fieldOffset =
                    (FieldOffsetAttribute)typeof(FT_GlyphSlotRec)
                    .GetField("outline")
                    .GetCustomAttributes(
                        typeof(FieldOffsetAttribute),
                        true
                        )[0];
                OUTLINE_OFFSET = fieldOffset.Value;
            }

            return(new IntPtr(glyph.ToInt32() + OUTLINE_OFFSET));
        }
Ejemplo n.º 7
0
        internal static Attribute[] GetCustomAttributes(RuntimeFieldInfo field, RuntimeType caType, out int count)
        {
            count = 0;
            bool flag = caType == (RuntimeType)typeof(object) || caType == (RuntimeType)typeof(Attribute);

            if (!flag && PseudoCustomAttribute.s_pca.GetValueOrDefault(caType) == null)
            {
                return(null);
            }
            Attribute[] array = new Attribute[PseudoCustomAttribute.s_pcasCount];
            if (flag || caType == (RuntimeType)typeof(MarshalAsAttribute))
            {
                Attribute customAttribute = MarshalAsAttribute.GetCustomAttribute(field);
                if (customAttribute != null)
                {
                    Attribute[] array2 = array;
                    int         num    = count;
                    count       = num + 1;
                    array2[num] = customAttribute;
                }
            }
            if (flag || caType == (RuntimeType)typeof(FieldOffsetAttribute))
            {
                Attribute customAttribute = FieldOffsetAttribute.GetCustomAttribute(field);
                if (customAttribute != null)
                {
                    Attribute[] array3 = array;
                    int         num    = count;
                    count       = num + 1;
                    array3[num] = customAttribute;
                }
            }
            if (flag || caType == (RuntimeType)typeof(NonSerializedAttribute))
            {
                Attribute customAttribute = NonSerializedAttribute.GetCustomAttribute(field);
                if (customAttribute != null)
                {
                    Attribute[] array4 = array;
                    int         num    = count;
                    count       = num + 1;
                    array4[num] = customAttribute;
                }
            }
            return(array);
        }
Ejemplo n.º 8
0
        public static void TestExplicitOffsetPseudoCustomAttribute()
        {
            Type t = typeof(ExplicitFieldOffsets).Project();

            {
                FieldInfo            f   = t.GetField("X");
                CustomAttributeData  cad = f.CustomAttributes.Single(c => c.AttributeType == typeof(FieldOffsetAttribute).Project());
                FieldOffsetAttribute foa = cad.UnprojectAndInstantiate <FieldOffsetAttribute>();
                Assert.Equal(42, foa.Value);
            }

            {
                FieldInfo            f   = t.GetField("Y");
                CustomAttributeData  cad = f.CustomAttributes.Single(c => c.AttributeType == typeof(FieldOffsetAttribute).Project());
                FieldOffsetAttribute foa = cad.UnprojectAndInstantiate <FieldOffsetAttribute>();
                Assert.Equal(65, foa.Value);
            }
        }
Ejemplo n.º 9
0
        internal object[]? GetPseudoCustomAttributes()
        {
            int count = 0;

            if (IsNotSerialized)
            {
                count++;
            }

            if (DeclaringType !.IsExplicitLayout)
            {
                count++;
            }

            MarshalAsAttribute marshalAs = get_marshal_info();

            if (marshalAs != null)
            {
                count++;
            }

            if (count == 0)
            {
                return(null);
            }
            object[] attrs = new object[count];
            count = 0;

            if (IsNotSerialized)
            {
                attrs[count++] = new NonSerializedAttribute();
            }
            if (DeclaringType.IsExplicitLayout)
            {
                attrs[count++] = new FieldOffsetAttribute(GetFieldOffset());
            }
            if (marshalAs != null)
            {
                attrs[count++] = marshalAs;
            }

            return(attrs);
        }
Ejemplo n.º 10
0
        internal object[] GetPseudoCustomAttributes()
        {
            int count = 0;

            if (IsNotSerialized)
            {
                count++;
            }

            if (DeclaringType.IsExplicitLayout)
            {
                count++;
            }
#if !MICRO_LIB
            UnmanagedMarshal marshalAs = UMarshal;
            if (marshalAs != null)
            {
                count++;
            }
#endif
            if (count == 0)
            {
                return(null);
            }
            object[] attrs = new object [count];
            count = 0;

            if (IsNotSerialized)
            {
                attrs [count++] = new NonSerializedAttribute();
            }
            if (DeclaringType.IsExplicitLayout)
            {
                attrs [count++] = new FieldOffsetAttribute(GetFieldOffset());
            }
#if !MICRO_LIB
            if (marshalAs != null)
            {
                attrs [count++] = marshalAs.ToMarshalAsAttribute();
            }
#endif
            return(attrs);
        }
Ejemplo n.º 11
0
 public bool PosTest3()
 {
     bool retVal = true;
     TestLibrary.TestFramework.BeginScenario("PosTest3:Initialize a new instance of the FieldOffsetAttribute class with the offset in the structure to the beginning of the field 3");
     try
     {
         int offset = TestLibrary.Generator.GetInt32(-55);
         FieldOffsetAttribute myFieldOffsetAttribute = new FieldOffsetAttribute(offset);
         if (myFieldOffsetAttribute == null || myFieldOffsetAttribute.Value != offset)
         {
             TestLibrary.TestFramework.LogError("005", "the initialized FieldOffsetAttribute ExpectedValue is " + offset + " but the ActualValue is " + myFieldOffsetAttribute.Value.ToString());
             retVal = false;
         }
     }
     catch (Exception e)
     {
         TestLibrary.TestFramework.LogError("006", "Unexpect exception:" + e);
         retVal = false;
     }
     return retVal;
 }
 public bool PosTest2()
 {
     bool retVal = true;
     TestLibrary.TestFramework.BeginScenario("PosTest2:Verify the value of initialized instance of the FieldOffsetAttribute 2");
     try
     {
         int offset = 0;
         FieldOffsetAttribute myFieldOffsetAttribute = new FieldOffsetAttribute(offset);
         if (myFieldOffsetAttribute.Value != offset)
         {
             TestLibrary.TestFramework.LogError("003", "the initialized FieldOffsetAttribute ExpectedValue is " + offset + " but the ActualValue is " + myFieldOffsetAttribute.Value.ToString());
             retVal = false;
         }
     }
     catch (Exception e)
     {
         TestLibrary.TestFramework.LogError("004", "Unexpect exception:" + e);
         retVal = false;
     }
     return retVal;
 }
Ejemplo n.º 13
0
    public bool PosTest3()
    {
        bool retVal = true;

        TestLibrary.TestFramework.BeginScenario("PosTest3:Initialize a new instance of the FieldOffsetAttribute class with the offset in the structure to the beginning of the field 3");
        try
        {
            int offset = TestLibrary.Generator.GetInt32(-55);
            FieldOffsetAttribute myFieldOffsetAttribute = new FieldOffsetAttribute(offset);
            if (myFieldOffsetAttribute == null || myFieldOffsetAttribute.Value != offset)
            {
                TestLibrary.TestFramework.LogError("005", "the initialized FieldOffsetAttribute ExpectedValue is " + offset + " but the ActualValue is " + myFieldOffsetAttribute.Value.ToString());
                retVal = false;
            }
        }
        catch (Exception e)
        {
            TestLibrary.TestFramework.LogError("006", "Unexpect exception:" + e);
            retVal = false;
        }
        return(retVal);
    }
Ejemplo n.º 14
0
    public bool PosTest2()
    {
        bool retVal = true;

        TestLibrary.TestFramework.BeginScenario("PosTest2:Verify the value of initialized instance of the FieldOffsetAttribute 2");
        try
        {
            int offset = 0;
            FieldOffsetAttribute myFieldOffsetAttribute = new FieldOffsetAttribute(offset);
            if (myFieldOffsetAttribute.Value != offset)
            {
                TestLibrary.TestFramework.LogError("003", "the initialized FieldOffsetAttribute ExpectedValue is " + offset + " but the ActualValue is " + myFieldOffsetAttribute.Value.ToString());
                retVal = false;
            }
        }
        catch (Exception e)
        {
            TestLibrary.TestFramework.LogError("004", "Unexpect exception:" + e);
            retVal = false;
        }
        return(retVal);
    }
Ejemplo n.º 15
0
        internal static Attribute[] GetCustomAttributes(RuntimeFieldInfo field, Type caType, out int count)
        {
            count = 0;
            bool flag = (caType == typeof(object)) || (caType == typeof(Attribute));

            if (!flag && (s_pca[caType] == null))
            {
                return(null);
            }
            Attribute[] attributeArray  = new Attribute[s_pcasCount];
            Attribute   customAttribute = null;

            if (flag || (caType == typeof(MarshalAsAttribute)))
            {
                customAttribute = MarshalAsAttribute.GetCustomAttribute(field);
                if (customAttribute != null)
                {
                    attributeArray[count++] = customAttribute;
                }
            }
            if (flag || (caType == typeof(FieldOffsetAttribute)))
            {
                customAttribute = FieldOffsetAttribute.GetCustomAttribute(field);
                if (customAttribute != null)
                {
                    attributeArray[count++] = customAttribute;
                }
            }
            if (flag || (caType == typeof(NonSerializedAttribute)))
            {
                customAttribute = NonSerializedAttribute.GetCustomAttribute(field);
                if (customAttribute != null)
                {
                    attributeArray[count++] = customAttribute;
                }
            }
            return(attributeArray);
        }
Ejemplo n.º 16
0
        internal object[] GetPseudoCustomAttributes()
        {
            int num = 0;

            if (this.IsNotSerialized)
            {
                num++;
            }
            if (this.DeclaringType.IsExplicitLayout)
            {
                num++;
            }
            UnmanagedMarshal umarshal = this.UMarshal;

            if (umarshal != null)
            {
                num++;
            }
            if (num == 0)
            {
                return(null);
            }
            object[] array = new object[num];
            num = 0;
            if (this.IsNotSerialized)
            {
                array[num++] = new NonSerializedAttribute();
            }
            if (this.DeclaringType.IsExplicitLayout)
            {
                array[num++] = new FieldOffsetAttribute(this.GetFieldOffset());
            }
            if (umarshal != null)
            {
                array[num++] = umarshal.ToMarshalAsAttribute();
            }
            return(array);
        }
Ejemplo n.º 17
0
        public void PseudoCustomAttributes()
        {
            object [] attrs;
            Type      t = typeof(FieldInfoTest);

#if NET_2_0
            Assert.AreEqual(1, t.GetField("i").GetCustomAttributes(typeof(NonSerializedAttribute), true).Length);
#else
            Assert.AreEqual(0, t.GetField("i").GetCustomAttributes(typeof(NonSerializedAttribute), true).Length);
#endif

            attrs = typeof(Class1).GetField("i").GetCustomAttributes(true);
#if NET_2_0
            Assert.AreEqual(1, attrs.Length, "#B1");
            FieldOffsetAttribute field_attr = (FieldOffsetAttribute)attrs [0];
            Assert.AreEqual(32, field_attr.Value, "#B2");
#else
            Assert.AreEqual(0, attrs.Length, "#B1");
#endif

            MarshalAsAttribute attr;

            attrs = typeof(Class2).GetField("f0").GetCustomAttributes(true);
#if NET_2_0
            Assert.AreEqual(1, attrs.Length, "#C1");
            attr = (MarshalAsAttribute)attrs [0];
            Assert.AreEqual(UnmanagedType.Bool, attr.Value, "#C2");
#else
            Assert.AreEqual(0, attrs.Length, "#C1");
#endif

            attrs = typeof(Class2).GetField("f1").GetCustomAttributes(true);
#if NET_2_0
            Assert.AreEqual(1, attrs.Length, "#D1");
            attr = (MarshalAsAttribute)attrs [0];
            Assert.AreEqual(UnmanagedType.LPArray, attr.Value, "#D2");
            Assert.AreEqual(UnmanagedType.LPStr, attr.ArraySubType, "#D3");
#else
            Assert.AreEqual(0, attrs.Length, "#D1");
#endif

            attrs = typeof(Class2).GetField("f2").GetCustomAttributes(true);
#if NET_2_0
            Assert.AreEqual(1, attrs.Length, "#E1");
            attr = (MarshalAsAttribute)attrs [0];
            Assert.AreEqual(UnmanagedType.ByValTStr, attr.Value, "#E2");
            Assert.AreEqual(100, attr.SizeConst, "#E3");
#else
            Assert.AreEqual(0, attrs.Length, "#E1");
#endif

            attrs = typeof(Class2).GetField("f3").GetCustomAttributes(true);
#if NET_2_0
            Assert.AreEqual(1, attrs.Length, "#F1");
            attr = (MarshalAsAttribute)attrs [0];
            Assert.AreEqual(UnmanagedType.CustomMarshaler, attr.Value, "#F2");
            Assert.AreEqual("5", attr.MarshalCookie, "#F3");
            Assert.AreEqual(typeof(Marshal1), Type.GetType(attr.MarshalType), "#F4");
#else
            Assert.AreEqual(0, attrs.Length, "#F1");
#endif

            attrs = typeof(Class3).GetField("f3").GetCustomAttributes(false);
#if NET_2_0
            Assert.AreEqual(1, attrs.Length, "#G1");
            attr = (MarshalAsAttribute)attrs [0];
            Assert.AreEqual(UnmanagedType.CustomMarshaler, attr.Value, "#G2");
            Assert.AreEqual("5", attr.MarshalCookie, "#G3");
            Assert.AreEqual(typeof(Marshal1), Type.GetType(attr.MarshalType), "#G4");
#else
            Assert.AreEqual(0, attrs.Length, "#G1");
#endif

            attrs = typeof(Class3).GetField("f3").GetCustomAttributes(true);
#if NET_2_0
            Assert.AreEqual(1, attrs.Length, "#H1");
            attr = (MarshalAsAttribute)attrs [0];
            Assert.AreEqual(UnmanagedType.CustomMarshaler, attr.Value, "#H2");
            Assert.AreEqual("5", attr.MarshalCookie, "#H3");
            Assert.AreEqual(typeof(Marshal1), Type.GetType(attr.MarshalType), "#H4");
#else
            Assert.AreEqual(0, attrs.Length, "#H1");
#endif

            // bug #82465
            attrs = typeof(Class2).GetField("f3").GetCustomAttributes(true);
#if NET_2_0
            Assert.AreEqual(1, attrs.Length, "#I1");
            attr = (MarshalAsAttribute)attrs [0];
            Assert.AreEqual(UnmanagedType.CustomMarshaler, attr.Value, "#I2");
            Assert.AreEqual("5", attr.MarshalCookie, "#I3");
            Assert.AreEqual(typeof(Marshal1), Type.GetType(attr.MarshalType), "#I4");
#else
            Assert.AreEqual(0, attrs.Length, "#I1");
#endif
        }
Ejemplo n.º 18
0
        internal static bool IsDefined(RuntimeFieldInfo field, Type caType)
        {
            bool flag = (caType == typeof(object)) || (caType == typeof(Attribute));

            if (!flag && (s_pca[caType] == null))
            {
                return(false);
            }
            return(((flag || (caType == typeof(MarshalAsAttribute))) && MarshalAsAttribute.IsDefined(field)) || (((flag || (caType == typeof(FieldOffsetAttribute))) && FieldOffsetAttribute.IsDefined(field)) || ((flag || (caType == typeof(NonSerializedAttribute))) && NonSerializedAttribute.IsDefined(field))));
        }
Ejemplo n.º 19
0
        public RegisterService(DataTarget target)
        {
            _target = target;

            Type contextType;

            switch (target.Architecture)
            {
            case Architecture.Amd64:
                _contextSize  = AMD64Context.Size;
                _contextFlags = AMD64Context.ContextControl | AMD64Context.ContextInteger | AMD64Context.ContextSegments;
                contextType   = typeof(AMD64Context);
                break;

            case Architecture.X86:
                _contextSize  = X86Context.Size;
                _contextFlags = X86Context.ContextControl | X86Context.ContextInteger | X86Context.ContextSegments;
                contextType   = typeof(X86Context);
                break;

            case Architecture.Arm64:
                _contextSize  = Arm64Context.Size;
                _contextFlags = Arm64Context.ContextControl | Arm64Context.ContextInteger;
                contextType   = typeof(Arm64Context);
                break;

            case Architecture.Arm:
                _contextSize  = ArmContext.Size;
                _contextFlags = ArmContext.ContextControl | ArmContext.ContextInteger;
                contextType   = typeof(ArmContext);
                break;

            default:
                throw new PlatformNotSupportedException($"Unsupported architecture: {target.Architecture}");
            }

            var registers = new List <RegisterInfo>();
            int index     = 0;

            FieldInfo[] fields = contextType.GetFields(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic);
            foreach (FieldInfo field in fields)
            {
                RegisterAttribute registerAttribute = field.GetCustomAttributes <RegisterAttribute>(inherit: false).SingleOrDefault();
                if (registerAttribute == null)
                {
                    continue;
                }
                RegisterType registerType = registerAttribute.RegisterType & RegisterType.TypeMask;
                switch (registerType)
                {
                case RegisterType.Control:
                case RegisterType.General:
                case RegisterType.Segments:
                    break;

                default:
                    continue;
                }
                if ((registerAttribute.RegisterType & RegisterType.ProgramCounter) != 0)
                {
                    InstructionPointerIndex = index;
                }
                if ((registerAttribute.RegisterType & RegisterType.StackPointer) != 0)
                {
                    StackPointerIndex = index;
                }
                if ((registerAttribute.RegisterType & RegisterType.FramePointer) != 0)
                {
                    FramePointerIndex = index;
                }
                FieldOffsetAttribute offsetAttribute = field.GetCustomAttributes <FieldOffsetAttribute>(inherit: false).Single();
                var registerInfo = new RegisterInfo(index, offsetAttribute.Value, Marshal.SizeOf(field.FieldType), registerAttribute.Name ?? field.Name.ToLower());
                registers.Add(registerInfo);
                index++;
            }

            _lookupByName  = registers.ToDictionary((info) => info.RegisterName);
            _lookupByIndex = registers.ToDictionary((info) => info.RegisterIndex);

            Registers = registers;
        }
Ejemplo n.º 20
0
        internal static bool IsDefined(RuntimeFieldInfo field, RuntimeType caType)
        {
            bool flag = caType == (RuntimeType)typeof(object) || caType == (RuntimeType)typeof(Attribute);

            return((flag || !(PseudoCustomAttribute.s_pca.GetValueOrDefault(caType) == null)) && (((flag || caType == (RuntimeType)typeof(MarshalAsAttribute)) && MarshalAsAttribute.IsDefined(field)) || ((flag || caType == (RuntimeType)typeof(FieldOffsetAttribute)) && FieldOffsetAttribute.IsDefined(field)) || ((flag || caType == (RuntimeType)typeof(NonSerializedAttribute)) && NonSerializedAttribute.IsDefined(field))));
        }
Ejemplo n.º 21
0
        public ThreadService(ITarget target)
        {
            Target = target;

            target.OnFlushEvent.Register(() => {
                _threads?.Clear();
                _threads = null;
            });

            Type contextType;

            switch (target.Architecture)
            {
            case Architecture.X64:
                // Dumps generated with newer dbgeng have bigger context buffers and clrmd requires the context size to at least be that size.
                _contextSize  = target.OperatingSystem == OSPlatform.Windows ? 0x700 : AMD64Context.Size;
                _contextFlags = AMD64Context.ContextControl | AMD64Context.ContextInteger | AMD64Context.ContextSegments | AMD64Context.ContextFloatingPoint;
                contextType   = typeof(AMD64Context);
                break;

            case Architecture.X86:
                _contextSize  = X86Context.Size;
                _contextFlags = X86Context.ContextControl | X86Context.ContextInteger | X86Context.ContextSegments | X86Context.ContextFloatingPoint;
                contextType   = typeof(X86Context);
                break;

            case Architecture.Arm64:
                _contextSize  = Arm64Context.Size;
                _contextFlags = Arm64Context.ContextControl | Arm64Context.ContextInteger | Arm64Context.ContextFloatingPoint;
                contextType   = typeof(Arm64Context);
                break;

            case Architecture.Arm:
                _contextSize  = ArmContext.Size;
                _contextFlags = ArmContext.ContextControl | ArmContext.ContextInteger | ArmContext.ContextFloatingPoint;
                contextType   = typeof(ArmContext);
                break;

            default:
                throw new PlatformNotSupportedException($"Unsupported architecture: {target.Architecture}");
            }

            var registers = new List <RegisterInfo>();
            int index     = 0;

            FieldInfo[] fields = contextType.GetFields(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic);
            foreach (FieldInfo field in fields)
            {
                RegisterAttribute registerAttribute = field.GetCustomAttributes <RegisterAttribute>(inherit: false).SingleOrDefault();
                if (registerAttribute == null)
                {
                    continue;
                }
                RegisterType registerType = registerAttribute.RegisterType & RegisterType.TypeMask;
                switch (registerType)
                {
                case RegisterType.Control:
                case RegisterType.General:
                case RegisterType.Segments:
                    break;

                default:
                    continue;
                }
                if ((registerAttribute.RegisterType & RegisterType.ProgramCounter) != 0)
                {
                    InstructionPointerIndex = index;
                }
                if ((registerAttribute.RegisterType & RegisterType.StackPointer) != 0)
                {
                    StackPointerIndex = index;
                }
                if ((registerAttribute.RegisterType & RegisterType.FramePointer) != 0)
                {
                    FramePointerIndex = index;
                }
                FieldOffsetAttribute offsetAttribute = field.GetCustomAttributes <FieldOffsetAttribute>(inherit: false).Single();
                var registerInfo = new RegisterInfo(index, offsetAttribute.Value, Marshal.SizeOf(field.FieldType), registerAttribute.Name ?? field.Name.ToLower());
                registers.Add(registerInfo);
                index++;
            }

            _lookupByName  = registers.ToDictionary((info) => info.RegisterName);
            _lookupByIndex = registers.ToDictionary((info) => info.RegisterIndex);
            Registers      = registers;
        }