Example #1
0
        internal CustomAttributeValueTreatment CalculateCustomAttributeValueTreatment(CustomAttributeHandle handle)
        {
            Debug.Assert(_metadataKind != MetadataKind.Ecma335);

            var parent = CustomAttributeTable.GetParent(handle);

            // Check for Windows.Foundation.Metadata.AttributeUsageAttribute.
            // WinMD rules:
            //   - The attribute is only applicable on TypeDefs.
            //   - Constructor must be a MemberRef with TypeRef.
            if (!IsWindowsAttributeUsageAttribute(parent, handle))
            {
                return(CustomAttributeValueTreatment.None);
            }

            var targetTypeDef = (TypeDefinitionHandle)parent;

            if (StringStream.EqualsRaw(TypeDefTable.GetNamespace(targetTypeDef), "Windows.Foundation.Metadata"))
            {
                if (StringStream.EqualsRaw(TypeDefTable.GetName(targetTypeDef), "VersionAttribute"))
                {
                    return(CustomAttributeValueTreatment.AttributeUsageVersionAttribute);
                }

                if (StringStream.EqualsRaw(TypeDefTable.GetName(targetTypeDef), "DeprecatedAttribute"))
                {
                    return(CustomAttributeValueTreatment.AttributeUsageDeprecatedAttribute);
                }
            }

            bool allowMultiple = HasAttribute(targetTypeDef, "Windows.Foundation.Metadata", "AllowMultipleAttribute");

            return(allowMultiple ? CustomAttributeValueTreatment.AttributeUsageAllowMultiple : CustomAttributeValueTreatment.AttributeUsageAllowSingle);
        }
Example #2
0
        private void MakeTree(TypeDefTable t1, TypeDefTable t2)
        {
            t1.Children = new ArrayList[]
            {
                new ArrayList(), new ArrayList(), new ArrayList()
            }
            ;
            ArrayList listField = this.Tables[(int)MetadataTables.Field];
            ArrayList listMethod = this.Tables[(int)MetadataTables.MethodDef];
            int       f1 = t1.FieldList, f2 = (t2 != null) ? t2.FieldList:
                                              listField.Count + 1;

            for (int i = f1; i < f2; i++)
            {
                FieldTable f = listField[i - 1] as FieldTable;
                t1.Children[(int)Children.DefField].Add(f);
                f.ParentTable = t1;
            }
            int m1 = t1.MethodList, m2 = (t2 != null) ? t2.MethodList:
                                         listMethod.Count + 1;

            for (int i = m1; i < m2; i++)
            {
                MethodDefTable m = listMethod[i - 1] as MethodDefTable;
                t1.Children[(int)Children.DefMethod].Add(m);
                m.ParentTable = t1;
                this.MakeTree(m);
            }
        }
Example #3
0
        private TypeDefTreatment GetWellKnownTypeDefinitionTreatment(TypeDefinitionHandle typeDef)
        {
            InitializeProjectedTypes();

            StringHandle name = TypeDefTable.GetName(typeDef);

            int index = StringStream.BinarySearchRaw(s_projectedTypeNames, name);

            if (index < 0)
            {
                return(TypeDefTreatment.None);
            }

            StringHandle namespaceName = TypeDefTable.GetNamespace(typeDef);

            if (StringStream.EqualsRaw(namespaceName, StringStream.GetVirtualValue(s_projectionInfos[index].ClrNamespace)))
            {
                return(s_projectionInfos[index].Treatment);
            }

            // TODO: we can avoid this comparison if info.DotNetNamespace == info.WinRtNamespace
            if (StringStream.EqualsRaw(namespaceName, s_projectionInfos[index].WinRTNamespace))
            {
                return(s_projectionInfos[index].Treatment | TypeDefTreatment.MarkInternalFlag);
            }

            return(TypeDefTreatment.None);
        }
Example #4
0
        public sealed override Type[] GetTypes()
        {
            // Note: we don't expect this process to generate any loader exceptions so no need to implement the ReflectionTypeLoadException
            // logic.

            EnsureTypeDefTableFullyFilled();
            return(TypeDefTable.ToArray <Type>(skip: 1)); // 0x02000001 is the <Module> type which is always skipped by this api.
        }
Example #5
0
        private bool IsClrImplementationType(TypeDefinitionHandle typeDef)
        {
            var attrs = TypeDefTable.GetFlags(typeDef);

            if ((attrs & (TypeAttributes.VisibilityMask | TypeAttributes.SpecialName)) != TypeAttributes.SpecialName)
            {
                return(false);
            }

            return(StringStream.StartsWithRaw(TypeDefTable.GetName(typeDef), ClrPrefix));
        }
Example #6
0
        internal uint CalculateTypeDefTreatmentAndRowId(TypeDefinitionHandle handle)
        {
            Debug.Assert(_metadataKind != MetadataKind.Ecma335);

            TypeDefTreatment treatment;

            TypeAttributes flags   = TypeDefTable.GetFlags(handle);
            EntityHandle   extends = TypeDefTable.GetExtends(handle);

            treatment = TypeDefTreatment.None;

            return(TreatmentAndRowId((byte)treatment, handle.RowId));
        }
Example #7
0
        public string GetName(TypeDefTable t)
        {
            string    n  = this.GetStringsString(t.Name);
            TableBase tb = t.ParentTable;

            if (tb is TypeDefTable)
            {
                return(this.GetName(tb as TypeDefTable) + "." + n);
            }
            if (t.Namespace != 0)
            {
                n = this.GetStringsString(t.Namespace) + "." + n;
            }
            return(n);
        }
Example #8
0
        private bool GetAttributeTypeNameRaw(CustomAttributeHandle caHandle, out StringHandle namespaceName, out StringHandle typeName)
        {
            namespaceName = typeName = default(StringHandle);

            EntityHandle typeDefOrRef = GetAttributeTypeRaw(caHandle);

            if (typeDefOrRef.IsNil)
            {
                return(false);
            }

            if (typeDefOrRef.Kind == HandleKind.TypeReference)
            {
                TypeReferenceHandle typeRef = (TypeReferenceHandle)typeDefOrRef;
                var resolutionScope         = TypeRefTable.GetResolutionScope(typeRef);

                if (!resolutionScope.IsNil && resolutionScope.Kind == HandleKind.TypeReference)
                {
                    // we don't need to handle nested types
                    return(false);
                }

                // other resolution scopes don't affect full name

                typeName      = TypeRefTable.GetName(typeRef);
                namespaceName = TypeRefTable.GetNamespace(typeRef);
            }
            else if (typeDefOrRef.Kind == HandleKind.TypeDefinition)
            {
                TypeDefinitionHandle typeDef = (TypeDefinitionHandle)typeDefOrRef;

                if (TypeDefTable.GetFlags(typeDef).IsNested())
                {
                    // we don't need to handle nested types
                    return(false);
                }

                typeName      = TypeDefTable.GetName(typeDef);
                namespaceName = TypeDefTable.GetNamespace(typeDef);
            }
            else
            {
                // invalid metadata
                return(false);
            }

            return(true);
        }
Example #9
0
        private TypeDefTreatment GetWellKnownTypeDefinitionTreatment(TypeDefinitionHandle typeDef)
        {
            InitializeProjectedTypes();

            StringHandle name = TypeDefTable.GetName(typeDef);

            int index = StringHeap.BinarySearchRaw(s_projectedTypeNames !, name);

            if (index < 0)
            {
                return(TypeDefTreatment.None);
            }

            StringHandle namespaceName = TypeDefTable.GetNamespace(typeDef);

            if (StringHeap.EqualsRaw(namespaceName, StringHeap.GetVirtualString(s_projectionInfos ![index].ClrNamespace)))
Example #10
0
        void EncodeTypeDefTable(TypeDefTable table)
        {
            int index = 0;

            foreach (TypeDefRow row in table.Rows)
            {
                this.asm.ALIGN(Assembly.OBJECT_ALIGNMENT);
                this.asm.LABEL(moduleName + " TypeDefRow#" + index);
                this.asm.AddObjectFields(typeof(SharpOS.AOT.Metadata.TypeDefRow).ToString());

                this.asm.DATA((uint)row.Flags);
                this.asm.DATA(row.Name);
                this.asm.DATA(row.Namespace);
                this.asm.DATA(row.Extends.ToUInt());
                this.asm.DATA(row.FieldList);
                this.asm.DATA(row.MethodList);

                ++index;
            }

            this.MetadataArray("TypeDef", table);
        }
Example #11
0
    public bool WriteAsm(StreamWriter sw, TypeDefTable t)
    {
        bool ok = true;

        sw.WriteLine("; class {0}", this.pedata.idxm.GetName(t));
        bool first = true;

        foreach (object obj in t.Children[(int)Children.DefField])
        {
            FieldTable f = obj as FieldTable;
            if ((f.Flags & (int)FieldAttributes.Static) == 0 ||
                (f.Flags & (int)FieldAttributes.Literal) != 0)
            {
                continue;
            }

            if (first)
            {
                sw.WriteLine();
                first = false;
            }
            sw.WriteLine("{0} dw 0", MangleField(f));
        }
        foreach (object obj in t.Children[(int)Children.DefMethod])
        {
            MethodDefTable m  = obj as MethodDefTable;
            MethodData     md = m.Tag as MethodData;
            if (md.HasThis)
            {
                continue;                         // static only
            }
            sw.WriteLine();
            if (!this.WriteAsm(sw, md))
            {
                ok = false;
            }
        }
        return(ok);
    }
Example #12
0
        /// <summary>
        /// The backing field of a WinRT enumeration type is not public although the backing fields
        /// of managed enumerations are. To allow managed languages to directly access this field,
        /// it is made public by the metadata adapter.
        /// </summary>
        private uint CalculateFieldDefTreatmentAndRowId(FieldDefinitionHandle handle)
        {
            var flags = FieldTable.GetFlags(handle);
            FieldDefTreatment treatment = FieldDefTreatment.None;

            if ((flags & FieldAttributes.RTSpecialName) != 0 && StringStream.EqualsRaw(FieldTable.GetName(handle), "value__"))
            {
                TypeDefinitionHandle typeDef = GetDeclaringType(handle);

                EntityHandle baseTypeHandle = TypeDefTable.GetExtends(typeDef);
                if (baseTypeHandle.Kind == HandleKind.TypeReference)
                {
                    var typeRef = (TypeReferenceHandle)baseTypeHandle;

                    if (StringStream.EqualsRaw(TypeRefTable.GetName(typeRef), "Enum") &&
                        StringStream.EqualsRaw(TypeRefTable.GetNamespace(typeRef), "System"))
                    {
                        treatment = FieldDefTreatment.EnumValue;
                    }
                }
            }

            return(TreatmentAndRowId((byte)treatment, handle.RowId));
        }
Example #13
0
 internal sealed override IEnumerable <RoType> GetDefinedRoTypes()
 {
     EnsureTypeDefTableFullyFilled();
     return(TypeDefTable.EnumerateValues(skip: 1)); // 0x02000001 is the <Module> type which is always skipped by this api.
 }
Example #14
0
        private uint CalculateMethodDefTreatmentAndRowId(MethodDefinitionHandle methodDef)
        {
            MethodDefTreatment treatment = MethodDefTreatment.Implementation;

            TypeDefinitionHandle parentTypeDef = GetDeclaringType(methodDef);
            TypeAttributes       parentFlags   = TypeDefTable.GetFlags(parentTypeDef);

            if ((parentFlags & TypeAttributes.WindowsRuntime) != 0)
            {
                if (IsClrImplementationType(parentTypeDef))
                {
                    treatment = MethodDefTreatment.Implementation;
                }
                else if (parentFlags.IsNested())
                {
                    treatment = MethodDefTreatment.Implementation;
                }
                else if ((parentFlags & TypeAttributes.Interface) != 0)
                {
                    treatment = MethodDefTreatment.InterfaceMethod;
                }
                else if (_metadataKind == MetadataKind.ManagedWindowsMetadata && (parentFlags & TypeAttributes.Public) == 0)
                {
                    treatment = MethodDefTreatment.Implementation;
                }
                else
                {
                    treatment = MethodDefTreatment.Other;

                    var parentBaseType = TypeDefTable.GetExtends(parentTypeDef);
                    if (parentBaseType.Kind == HandleKind.TypeReference)
                    {
                        switch (GetSpecialTypeRefTreatment((TypeReferenceHandle)parentBaseType))
                        {
                        case TypeRefTreatment.SystemAttribute:
                            treatment = MethodDefTreatment.AttributeMethod;
                            break;

                        case TypeRefTreatment.SystemDelegate:
                            treatment = MethodDefTreatment.DelegateMethod | MethodDefTreatment.MarkPublicFlag;
                            break;
                        }
                    }
                }
            }

            if (treatment == MethodDefTreatment.Other)
            {
                // we want to hide the method if it implements
                // only redirected interfaces
                // We also want to check if the methodImpl is IClosable.Close,
                // so we can change the name
                bool seenRedirectedInterfaces    = false;
                bool seenNonRedirectedInterfaces = false;

                bool isIClosableClose = false;

                foreach (var methodImplHandle in new MethodImplementationHandleCollection(this, parentTypeDef))
                {
                    MethodImplementation methodImpl = GetMethodImplementation(methodImplHandle);
                    if (methodImpl.MethodBody == methodDef)
                    {
                        EntityHandle declaration = methodImpl.MethodDeclaration;

                        // See if this MethodImpl implements a redirected interface
                        // In WinMD, MethodImpl will always use MemberRef and TypeRefs to refer to redirected interfaces,
                        // even if they are in the same module.
                        if (declaration.Kind == HandleKind.MemberReference &&
                            ImplementsRedirectedInterface((MemberReferenceHandle)declaration, out isIClosableClose))
                        {
                            seenRedirectedInterfaces = true;
                            if (isIClosableClose)
                            {
                                // This method implements IClosable.Close
                                // Let's rename to IDisposable later
                                // Once we know this implements IClosable.Close, we are done
                                // looking
                                break;
                            }
                        }
                        else
                        {
                            // Now we know this implements a non-redirected interface
                            // But we need to keep looking, just in case we got a methodimpl that
                            // implements the IClosable.Close method and needs to be renamed
                            seenNonRedirectedInterfaces = true;
                        }
                    }
                }

                if (isIClosableClose)
                {
                    treatment = MethodDefTreatment.DisposeMethod;
                }
                else if (seenRedirectedInterfaces && !seenNonRedirectedInterfaces)
                {
                    // Only hide if all the interfaces implemented are redirected
                    treatment = MethodDefTreatment.HiddenInterfaceImplementation;
                }
            }

            // If treatment is other, then this is a non-managed WinRT runtime class definition
            // Find out about various bits that we apply via attributes and name parsing
            if (treatment == MethodDefTreatment.Other)
            {
                treatment |= GetMethodTreatmentFromCustomAttributes(methodDef);
            }

            return(TreatmentAndRowId((byte)treatment, methodDef.RowId));
        }
Example #15
0
        internal uint CalculateTypeDefTreatmentAndRowId(TypeDefinitionHandle handle)
        {
            Debug.Assert(_metadataKind != MetadataKind.Ecma335);

            TypeDefTreatment treatment;

            TypeAttributes flags   = TypeDefTable.GetFlags(handle);
            EntityHandle   extends = TypeDefTable.GetExtends(handle);

            if ((flags & TypeAttributes.WindowsRuntime) != 0)
            {
                if (_metadataKind == MetadataKind.WindowsMetadata)
                {
                    treatment = GetWellKnownTypeDefinitionTreatment(handle);
                    if (treatment != TypeDefTreatment.None)
                    {
                        return(TreatmentAndRowId((byte)treatment, handle.RowId));
                    }

                    // Is this an attribute?
                    if (extends.Kind == HandleKind.TypeReference && IsSystemAttribute((TypeReferenceHandle)extends))
                    {
                        treatment = TypeDefTreatment.NormalAttribute;
                    }
                    else
                    {
                        treatment = TypeDefTreatment.NormalNonAttribute;
                    }
                }
                else if (_metadataKind == MetadataKind.ManagedWindowsMetadata && NeedsWinRTPrefix(flags, extends))
                {
                    // WinMDExp emits two versions of RuntimeClasses and Enums:
                    //
                    //    public class Foo {}            // the WinRT reference class
                    //    internal class <CLR>Foo {}     // the implementation class that we want WinRT consumers to ignore
                    //
                    // The adapter's job is to undo WinMDExp's transformations. I.e. turn the above into:
                    //
                    //    internal class <WinRT>Foo {}   // the WinRT reference class that we want CLR consumers to ignore
                    //    public class Foo {}            // the implementation class
                    //
                    // We only add the <WinRT> prefix here since the WinRT view is the only view that is marked WindowsRuntime
                    // De-mangling the CLR name is done below.


                    // tomat: The CLR adapter implements a back-compat quirk: Enums exported with an older WinMDExp have only one version
                    // not marked with tdSpecialName. These enums should *not* be mangled and flipped to private.
                    // We don't implement this flag since the WinMDs producted by the older WinMDExp are not used in the wild.

                    treatment = TypeDefTreatment.PrefixWinRTName;
                }
                else
                {
                    treatment = TypeDefTreatment.None;
                }

                // Scan through Custom Attributes on type, looking for interesting bits. We only
                // need to do this for RuntimeClasses
                if ((treatment == TypeDefTreatment.PrefixWinRTName || treatment == TypeDefTreatment.NormalNonAttribute))
                {
                    if ((flags & TypeAttributes.Interface) == 0 &&
                        HasAttribute(handle, "Windows.UI.Xaml", "TreatAsAbstractComposableClassAttribute"))
                    {
                        treatment |= TypeDefTreatment.MarkAbstractFlag;
                    }
                }
            }
            else if (_metadataKind == MetadataKind.ManagedWindowsMetadata && IsClrImplementationType(handle))
            {
                // <CLR> implementation classes are not marked WindowsRuntime, but still need to be modified
                // by the adapter.
                treatment = TypeDefTreatment.UnmangleWinRTName;
            }
            else
            {
                treatment = TypeDefTreatment.None;
            }

            return(TreatmentAndRowId((byte)treatment, handle.RowId));
        }
 public virtual void VisitTypeDefTable(TypeDefTable table)
 {
 }
Example #17
0
        public void Read(ReaderContext ctxt)
        {
            var reader = ctxt.GetTablesReader();

            var actualReserved0 = reader.ReadUInt32();
            if (actualReserved0 != reserved0)
                throw new PEException("invalid MetadataTable header");
            var actualMajorVersion = reader.ReadByte();
            if (actualMajorVersion != majorVersion)
                throw new PEException("invalid MetadataTable header");
            var actualMinorVersion = reader.ReadByte();
            if (actualMinorVersion != minorVersion)
                throw new PEException("invalid MetadataTable header");
            var heapSizes = reader.ReadByte();
            IsStringStreamBig = (heapSizes & 0x01) != 0;
            IsGuidStreamBig = (heapSizes & 0x02) != 0;
            IsBlobStreamBig = (heapSizes & 0x04) != 0;
            Reserved1 = reader.ReadByte();

            var valid = new IntSet64(reader.ReadUInt64());
            var sorted = new IntSet64(reader.ReadUInt64());

            for (var i = 0; i < 64; i++)
            {
                var numRows = 0;
                if (valid[i])
                    numRows = (int)reader.ReadUInt32();

                switch ((TableTag)i)
                {
                case TableTag.Module:
                    ModuleTable = new ModuleTable(numRows);
                    break;
                case TableTag.Assembly:
                    AssemblyTable = new AssemblyTable(numRows);
                    break;
                case TableTag.AssemblyOS:
                    AssemblyOSTable = new AssemblyOSTable(numRows);
                    break;
                case TableTag.AssemblyProcessor:
                    AssemblyProcessorTable = new AssemblyProcessorTable(numRows);
                    break;
                case TableTag.AssemblyRef:
                    AssemblyRefTable = new AssemblyRefTable(numRows);
                    break;
                case TableTag.AssemblyRefOS:
                    AssemblyRefOSTable = new AssemblyRefOSTable(numRows);
                    break;
                case TableTag.AssemblyRefProcessor:
                    AssemblyRefProcessorTable = new AssemblyRefProcessorTable(numRows);
                    break;
                case TableTag.ClassLayout:
                    ClassLayoutTable = new ClassLayoutTable(numRows);
                    break;
                case TableTag.Constant:
                    ConstantTable = new ConstantTable(numRows);
                    break;
                case TableTag.CustomAttribute:
                    CustomAttributeTable = new CustomAttributeTable(numRows);
                    break;
                case TableTag.DeclSecurity:
                    DeclSecurityTable = new DeclSecurityTable(numRows);
                    break;
                case TableTag.EventMap:
                    EventMapTable = new EventMapTable(numRows);
                    break;
                case TableTag.Event:
                    EventTable = new EventTable(numRows);
                    break;
                case TableTag.ExportedType:
                    ExportedTypeTable = new ExportedTypeTable(numRows);
                    break;
                case TableTag.Field:
                    FieldTable = new FieldTable(numRows);
                    break;
                case TableTag.FieldLayout:
                    FieldLayoutTable = new FieldLayoutTable(numRows);
                    break;
                case TableTag.FieldMarshal:
                    FieldMarshalTable = new FieldMarshalTable(numRows);
                    break;
                case TableTag.FieldRVA:
                    FieldRVATable = new FieldRVATable(numRows);
                    break;
                case TableTag.File:
                    FileTable = new FileTable(numRows);
                    break;
                case TableTag.GenericParam:
                    GenericParamTable = new GenericParamTable(numRows);
                    break;
                case TableTag.GenericParamConstraint:
                    GenericParamConstraintTable = new GenericParamConstraintTable(numRows);
                    break;
                case TableTag.ImplMap:
                    ImplMapTable = new ImplMapTable(numRows);
                    break;
                case TableTag.InterfaceImpl:
                    InterfaceImplTable = new InterfaceImplTable(numRows);
                    break;
                case TableTag.ManifestResource:
                    ManifestResourceTable = new ManifestResourceTable(numRows);
                    break;
                case TableTag.MemberRef:
                    MemberRefTable = new MemberRefTable(numRows);
                    break;
                case TableTag.MethodDef:
                    MethodDefTable = new MethodDefTable(numRows);
                    break;
                case TableTag.MethodImpl:
                    MethodImplTable = new MethodImplTable(numRows);
                    break;
                case TableTag.MethodSemantics:
                    MethodSemanticsTable = new MethodSemanticsTable(numRows);
                    break;
                case TableTag.MethodSpec:
                    MethodSpecTable = new MethodSpecTable(numRows);
                    break;
                case TableTag.ModuleRef:
                    ModuleRefTable = new ModuleRefTable(numRows);
                    break;
                case TableTag.NestedClass:
                    NestedClassTable = new NestedClassTable(numRows);
                    break;
                case TableTag.Param:
                    ParamTable = new ParamTable(numRows);
                    break;
                case TableTag.Property:
                    PropertyTable = new PropertyTable(numRows);
                    break;
                case TableTag.PropertyMap:
                    PropertyMapTable = new PropertyMapTable(numRows);
                    break;
                case TableTag.StandAloneSig:
                    StandAloneSigTable = new StandAloneSigTable(numRows);
                    break;
                case TableTag.TypeDef:
                    TypeDefTable = new TypeDefTable(numRows);
                    break;
                case TableTag.TypeRef:
                    TypeRefTable = new TypeRefTable(numRows);
                    break;
                case TableTag.TypeSpec:
                    TypeSpecTable = new TypeSpecTable(numRows);
                    break;
                default:
                    // Ignore
                    break;
                }
            }

            DetermineIndexCodingSizes();

            for (var i = 0; i < 64; i++)
            {
                if (valid[i])
                {
                    switch ((TableTag)i)
                    {
                    case TableTag.Module:
                        ModuleTable.Read(ctxt, reader);
                        break;
                    case TableTag.Assembly:
                        AssemblyTable.Read(ctxt, reader);
                        break;
                    case TableTag.AssemblyOS:
                        AssemblyOSTable.Read(ctxt, reader);
                        break;
                    case TableTag.AssemblyProcessor:
                        AssemblyProcessorTable.Read(ctxt, reader);
                        break;
                    case TableTag.AssemblyRef:
                        AssemblyRefTable.Read(ctxt, reader);
                        break;
                    case TableTag.AssemblyRefOS:
                        AssemblyRefOSTable.Read(ctxt, reader);
                        break;
                    case TableTag.AssemblyRefProcessor:
                        AssemblyRefProcessorTable.Read(ctxt, reader);
                        break;
                    case TableTag.ClassLayout:
                        ClassLayoutTable.Read(ctxt, reader);
                        break;
                    case TableTag.Constant:
                        ConstantTable.Read(ctxt, reader);
                        break;
                    case TableTag.CustomAttribute:
                        CustomAttributeTable.Read(ctxt, reader);
                        break;
                    case TableTag.DeclSecurity:
                        DeclSecurityTable.Read(ctxt, reader);
                        break;
                    case TableTag.EventMap:
                        EventMapTable.Read(ctxt, reader);
                        break;
                    case TableTag.Event:
                        EventTable.Read(ctxt, reader);
                        break;
                    case TableTag.ExportedType:
                        ExportedTypeTable.Read(ctxt, reader);
                        break;
                    case TableTag.Field:
                        FieldTable.Read(ctxt, reader);
                        break;
                    case TableTag.FieldLayout:
                        FieldLayoutTable.Read(ctxt, reader);
                        break;
                    case TableTag.FieldMarshal:
                        FieldMarshalTable.Read(ctxt, reader);
                        break;
                    case TableTag.FieldRVA:
                        FieldRVATable.Read(ctxt, reader);
                        break;
                    case TableTag.File:
                        FileTable.Read(ctxt, reader);
                        break;
                    case TableTag.GenericParam:
                        GenericParamTable.Read(ctxt, reader);
                        break;
                    case TableTag.GenericParamConstraint:
                        GenericParamConstraintTable.Read(ctxt, reader);
                        break;
                    case TableTag.ImplMap:
                        ImplMapTable.Read(ctxt, reader);
                        break;
                    case TableTag.InterfaceImpl:
                        InterfaceImplTable.Read(ctxt, reader);
                        break;
                    case TableTag.ManifestResource:
                        ManifestResourceTable.Read(ctxt, reader);
                        break;
                    case TableTag.MemberRef:
                        MemberRefTable.Read(ctxt, reader);
                        break;
                    case TableTag.MethodDef:
                        MethodDefTable.Read(ctxt, reader);
                        break;
                    case TableTag.MethodImpl:
                        MethodImplTable.Read(ctxt, reader);
                        break;
                    case TableTag.MethodSemantics:
                        MethodSemanticsTable.Read(ctxt, reader);
                        break;
                    case TableTag.MethodSpec:
                        MethodSpecTable.Read(ctxt, reader);
                        break;
                    case TableTag.ModuleRef:
                        ModuleRefTable.Read(ctxt, reader);
                        break;
                    case TableTag.NestedClass:
                        NestedClassTable.Read(ctxt, reader);
                        break;
                    case TableTag.Param:
                        ParamTable.Read(ctxt, reader);
                        break;
                    case TableTag.Property:
                        PropertyTable.Read(ctxt, reader);
                        break;
                    case TableTag.PropertyMap:
                        PropertyMapTable.Read(ctxt, reader);
                        break;
                    case TableTag.StandAloneSig:
                        StandAloneSigTable.Read(ctxt, reader);
                        break;
                    case TableTag.TypeDef:
                        TypeDefTable.Read(ctxt, reader);
                        break;
                    case TableTag.TypeRef:
                        TypeRefTable.Read(ctxt, reader);
                        break;
                    case TableTag.TypeSpec:
                        TypeSpecTable.Read(ctxt, reader);
                        break;
                    default:
                        throw new PEException("unexpected table tag in MetadataTable body");
                    }
                }
            }

            ModuleTable.ResolveIndexes(ctxt);
            TypeRefTable.ResolveIndexes(ctxt);
            TypeDefTable.ResolveIndexes(ctxt);
            FieldTable.ResolveIndexes(ctxt);
            MethodDefTable.ResolveIndexes(ctxt);
            ParamTable.ResolveIndexes(ctxt);
            InterfaceImplTable.ResolveIndexes(ctxt);
            MemberRefTable.ResolveIndexes(ctxt);
            ConstantTable.ResolveIndexes(ctxt);
            CustomAttributeTable.ResolveIndexes(ctxt);
            FieldMarshalTable.ResolveIndexes(ctxt);
            DeclSecurityTable.ResolveIndexes(ctxt);
            ClassLayoutTable.ResolveIndexes(ctxt);
            FieldLayoutTable.ResolveIndexes(ctxt);
            StandAloneSigTable.ResolveIndexes(ctxt);
            EventMapTable.ResolveIndexes(ctxt);
            EventTable.ResolveIndexes(ctxt);
            PropertyMapTable.ResolveIndexes(ctxt);
            PropertyTable.ResolveIndexes(ctxt);
            MethodSemanticsTable.ResolveIndexes(ctxt);
            MethodImplTable.ResolveIndexes(ctxt);
            ModuleRefTable.ResolveIndexes(ctxt);
            TypeSpecTable.ResolveIndexes(ctxt);
            ImplMapTable.ResolveIndexes(ctxt);
            FieldRVATable.ResolveIndexes(ctxt);
            AssemblyTable.ResolveIndexes(ctxt);
            AssemblyProcessorTable.ResolveIndexes(ctxt);
            AssemblyOSTable.ResolveIndexes(ctxt);
            AssemblyRefTable.ResolveIndexes(ctxt);
            AssemblyRefProcessorTable.ResolveIndexes(ctxt);
            AssemblyRefOSTable.ResolveIndexes(ctxt);
            FileTable.ResolveIndexes(ctxt);
            ExportedTypeTable.ResolveIndexes(ctxt);
            ManifestResourceTable.ResolveIndexes(ctxt);
            NestedClassTable.ResolveIndexes(ctxt);
            GenericParamTable.ResolveIndexes(ctxt);
            MethodSpecTable.ResolveIndexes(ctxt);
            GenericParamConstraintTable.ResolveIndexes(ctxt);
        }
Example #18
0
        public void Read(ReaderContext ctxt)
        {
            var reader = ctxt.GetTablesReader();

            var actualReserved0 = reader.ReadUInt32();

            if (actualReserved0 != reserved0)
            {
                throw new PEException("invalid MetadataTable header");
            }
            var actualMajorVersion = reader.ReadByte();

            if (actualMajorVersion != majorVersion)
            {
                throw new PEException("invalid MetadataTable header");
            }
            var actualMinorVersion = reader.ReadByte();

            if (actualMinorVersion != minorVersion)
            {
                throw new PEException("invalid MetadataTable header");
            }
            var heapSizes = reader.ReadByte();

            IsStringStreamBig = (heapSizes & 0x01) != 0;
            IsGuidStreamBig   = (heapSizes & 0x02) != 0;
            IsBlobStreamBig   = (heapSizes & 0x04) != 0;
            Reserved1         = reader.ReadByte();

            var valid  = new IntSet64(reader.ReadUInt64());
            var sorted = new IntSet64(reader.ReadUInt64());

            for (var i = 0; i < 64; i++)
            {
                var numRows = 0;
                if (valid[i])
                {
                    numRows = (int)reader.ReadUInt32();
                }

                switch ((TableTag)i)
                {
                case TableTag.Module:
                    ModuleTable = new ModuleTable(numRows);
                    break;

                case TableTag.Assembly:
                    AssemblyTable = new AssemblyTable(numRows);
                    break;

                case TableTag.AssemblyOS:
                    AssemblyOSTable = new AssemblyOSTable(numRows);
                    break;

                case TableTag.AssemblyProcessor:
                    AssemblyProcessorTable = new AssemblyProcessorTable(numRows);
                    break;

                case TableTag.AssemblyRef:
                    AssemblyRefTable = new AssemblyRefTable(numRows);
                    break;

                case TableTag.AssemblyRefOS:
                    AssemblyRefOSTable = new AssemblyRefOSTable(numRows);
                    break;

                case TableTag.AssemblyRefProcessor:
                    AssemblyRefProcessorTable = new AssemblyRefProcessorTable(numRows);
                    break;

                case TableTag.ClassLayout:
                    ClassLayoutTable = new ClassLayoutTable(numRows);
                    break;

                case TableTag.Constant:
                    ConstantTable = new ConstantTable(numRows);
                    break;

                case TableTag.CustomAttribute:
                    CustomAttributeTable = new CustomAttributeTable(numRows);
                    break;

                case TableTag.DeclSecurity:
                    DeclSecurityTable = new DeclSecurityTable(numRows);
                    break;

                case TableTag.EventMap:
                    EventMapTable = new EventMapTable(numRows);
                    break;

                case TableTag.Event:
                    EventTable = new EventTable(numRows);
                    break;

                case TableTag.ExportedType:
                    ExportedTypeTable = new ExportedTypeTable(numRows);
                    break;

                case TableTag.Field:
                    FieldTable = new FieldTable(numRows);
                    break;

                case TableTag.FieldLayout:
                    FieldLayoutTable = new FieldLayoutTable(numRows);
                    break;

                case TableTag.FieldMarshal:
                    FieldMarshalTable = new FieldMarshalTable(numRows);
                    break;

                case TableTag.FieldRVA:
                    FieldRVATable = new FieldRVATable(numRows);
                    break;

                case TableTag.File:
                    FileTable = new FileTable(numRows);
                    break;

                case TableTag.GenericParam:
                    GenericParamTable = new GenericParamTable(numRows);
                    break;

                case TableTag.GenericParamConstraint:
                    GenericParamConstraintTable = new GenericParamConstraintTable(numRows);
                    break;

                case TableTag.ImplMap:
                    ImplMapTable = new ImplMapTable(numRows);
                    break;

                case TableTag.InterfaceImpl:
                    InterfaceImplTable = new InterfaceImplTable(numRows);
                    break;

                case TableTag.ManifestResource:
                    ManifestResourceTable = new ManifestResourceTable(numRows);
                    break;

                case TableTag.MemberRef:
                    MemberRefTable = new MemberRefTable(numRows);
                    break;

                case TableTag.MethodDef:
                    MethodDefTable = new MethodDefTable(numRows);
                    break;

                case TableTag.MethodImpl:
                    MethodImplTable = new MethodImplTable(numRows);
                    break;

                case TableTag.MethodSemantics:
                    MethodSemanticsTable = new MethodSemanticsTable(numRows);
                    break;

                case TableTag.MethodSpec:
                    MethodSpecTable = new MethodSpecTable(numRows);
                    break;

                case TableTag.ModuleRef:
                    ModuleRefTable = new ModuleRefTable(numRows);
                    break;

                case TableTag.NestedClass:
                    NestedClassTable = new NestedClassTable(numRows);
                    break;

                case TableTag.Param:
                    ParamTable = new ParamTable(numRows);
                    break;

                case TableTag.Property:
                    PropertyTable = new PropertyTable(numRows);
                    break;

                case TableTag.PropertyMap:
                    PropertyMapTable = new PropertyMapTable(numRows);
                    break;

                case TableTag.StandAloneSig:
                    StandAloneSigTable = new StandAloneSigTable(numRows);
                    break;

                case TableTag.TypeDef:
                    TypeDefTable = new TypeDefTable(numRows);
                    break;

                case TableTag.TypeRef:
                    TypeRefTable = new TypeRefTable(numRows);
                    break;

                case TableTag.TypeSpec:
                    TypeSpecTable = new TypeSpecTable(numRows);
                    break;

                default:
                    // Ignore
                    break;
                }
            }

            DetermineIndexCodingSizes();

            for (var i = 0; i < 64; i++)
            {
                if (valid[i])
                {
                    switch ((TableTag)i)
                    {
                    case TableTag.Module:
                        ModuleTable.Read(ctxt, reader);
                        break;

                    case TableTag.Assembly:
                        AssemblyTable.Read(ctxt, reader);
                        break;

                    case TableTag.AssemblyOS:
                        AssemblyOSTable.Read(ctxt, reader);
                        break;

                    case TableTag.AssemblyProcessor:
                        AssemblyProcessorTable.Read(ctxt, reader);
                        break;

                    case TableTag.AssemblyRef:
                        AssemblyRefTable.Read(ctxt, reader);
                        break;

                    case TableTag.AssemblyRefOS:
                        AssemblyRefOSTable.Read(ctxt, reader);
                        break;

                    case TableTag.AssemblyRefProcessor:
                        AssemblyRefProcessorTable.Read(ctxt, reader);
                        break;

                    case TableTag.ClassLayout:
                        ClassLayoutTable.Read(ctxt, reader);
                        break;

                    case TableTag.Constant:
                        ConstantTable.Read(ctxt, reader);
                        break;

                    case TableTag.CustomAttribute:
                        CustomAttributeTable.Read(ctxt, reader);
                        break;

                    case TableTag.DeclSecurity:
                        DeclSecurityTable.Read(ctxt, reader);
                        break;

                    case TableTag.EventMap:
                        EventMapTable.Read(ctxt, reader);
                        break;

                    case TableTag.Event:
                        EventTable.Read(ctxt, reader);
                        break;

                    case TableTag.ExportedType:
                        ExportedTypeTable.Read(ctxt, reader);
                        break;

                    case TableTag.Field:
                        FieldTable.Read(ctxt, reader);
                        break;

                    case TableTag.FieldLayout:
                        FieldLayoutTable.Read(ctxt, reader);
                        break;

                    case TableTag.FieldMarshal:
                        FieldMarshalTable.Read(ctxt, reader);
                        break;

                    case TableTag.FieldRVA:
                        FieldRVATable.Read(ctxt, reader);
                        break;

                    case TableTag.File:
                        FileTable.Read(ctxt, reader);
                        break;

                    case TableTag.GenericParam:
                        GenericParamTable.Read(ctxt, reader);
                        break;

                    case TableTag.GenericParamConstraint:
                        GenericParamConstraintTable.Read(ctxt, reader);
                        break;

                    case TableTag.ImplMap:
                        ImplMapTable.Read(ctxt, reader);
                        break;

                    case TableTag.InterfaceImpl:
                        InterfaceImplTable.Read(ctxt, reader);
                        break;

                    case TableTag.ManifestResource:
                        ManifestResourceTable.Read(ctxt, reader);
                        break;

                    case TableTag.MemberRef:
                        MemberRefTable.Read(ctxt, reader);
                        break;

                    case TableTag.MethodDef:
                        MethodDefTable.Read(ctxt, reader);
                        break;

                    case TableTag.MethodImpl:
                        MethodImplTable.Read(ctxt, reader);
                        break;

                    case TableTag.MethodSemantics:
                        MethodSemanticsTable.Read(ctxt, reader);
                        break;

                    case TableTag.MethodSpec:
                        MethodSpecTable.Read(ctxt, reader);
                        break;

                    case TableTag.ModuleRef:
                        ModuleRefTable.Read(ctxt, reader);
                        break;

                    case TableTag.NestedClass:
                        NestedClassTable.Read(ctxt, reader);
                        break;

                    case TableTag.Param:
                        ParamTable.Read(ctxt, reader);
                        break;

                    case TableTag.Property:
                        PropertyTable.Read(ctxt, reader);
                        break;

                    case TableTag.PropertyMap:
                        PropertyMapTable.Read(ctxt, reader);
                        break;

                    case TableTag.StandAloneSig:
                        StandAloneSigTable.Read(ctxt, reader);
                        break;

                    case TableTag.TypeDef:
                        TypeDefTable.Read(ctxt, reader);
                        break;

                    case TableTag.TypeRef:
                        TypeRefTable.Read(ctxt, reader);
                        break;

                    case TableTag.TypeSpec:
                        TypeSpecTable.Read(ctxt, reader);
                        break;

                    default:
                        throw new PEException("unexpected table tag in MetadataTable body");
                    }
                }
            }

            ModuleTable.ResolveIndexes(ctxt);
            TypeRefTable.ResolveIndexes(ctxt);
            TypeDefTable.ResolveIndexes(ctxt);
            FieldTable.ResolveIndexes(ctxt);
            MethodDefTable.ResolveIndexes(ctxt);
            ParamTable.ResolveIndexes(ctxt);
            InterfaceImplTable.ResolveIndexes(ctxt);
            MemberRefTable.ResolveIndexes(ctxt);
            ConstantTable.ResolveIndexes(ctxt);
            CustomAttributeTable.ResolveIndexes(ctxt);
            FieldMarshalTable.ResolveIndexes(ctxt);
            DeclSecurityTable.ResolveIndexes(ctxt);
            ClassLayoutTable.ResolveIndexes(ctxt);
            FieldLayoutTable.ResolveIndexes(ctxt);
            StandAloneSigTable.ResolveIndexes(ctxt);
            EventMapTable.ResolveIndexes(ctxt);
            EventTable.ResolveIndexes(ctxt);
            PropertyMapTable.ResolveIndexes(ctxt);
            PropertyTable.ResolveIndexes(ctxt);
            MethodSemanticsTable.ResolveIndexes(ctxt);
            MethodImplTable.ResolveIndexes(ctxt);
            ModuleRefTable.ResolveIndexes(ctxt);
            TypeSpecTable.ResolveIndexes(ctxt);
            ImplMapTable.ResolveIndexes(ctxt);
            FieldRVATable.ResolveIndexes(ctxt);
            AssemblyTable.ResolveIndexes(ctxt);
            AssemblyProcessorTable.ResolveIndexes(ctxt);
            AssemblyOSTable.ResolveIndexes(ctxt);
            AssemblyRefTable.ResolveIndexes(ctxt);
            AssemblyRefProcessorTable.ResolveIndexes(ctxt);
            AssemblyRefOSTable.ResolveIndexes(ctxt);
            FileTable.ResolveIndexes(ctxt);
            ExportedTypeTable.ResolveIndexes(ctxt);
            ManifestResourceTable.ResolveIndexes(ctxt);
            NestedClassTable.ResolveIndexes(ctxt);
            GenericParamTable.ResolveIndexes(ctxt);
            MethodSpecTable.ResolveIndexes(ctxt);
            GenericParamConstraintTable.ResolveIndexes(ctxt);
        }
Example #19
0
        public void MakeTree(PEData data)
        {
            ArrayList listType = this.Tables[(int)MetadataTables.TypeDef];
            int       len      = listType.Count;

            for (int i = 0; i < len; i++)
            {
                this.MakeTree(listType[i] as TypeDefTable, i + 1 < len ? listType[i + 1] as TypeDefTable : null);
            }
            foreach (object obj in this.Tables[(int)MetadataTables.AssemblyRef])
            {
                (obj as TableBase).Children = new ArrayList[]
                {
                    new ArrayList()
                }
                ;
            }
            foreach (object obj in this.Tables[(int)MetadataTables.TypeRef])
            {
                (obj as TableBase).Children = new ArrayList[]
                {
                    new ArrayList(), new ArrayList(), new ArrayList()
                }
                ;
            }
            foreach (object obj in this.Tables[(int)MetadataTables.TypeRef])
            {
                TypeRefTable t = obj as TypeRefTable;
                if (t.ResolutionScope == 0)
                {
                    continue;
                }
                t.ParentTable = this.GetTable(this.GetToken(CodedIndices.ResolutionScope, t.ResolutionScope));
                t.ParentTable.Children[(int)Children.RefNested].Add(t);
            }
            foreach (object obj in this.Tables[(int)MetadataTables.MethodDef])
            {
                MethodDefTable m = obj as MethodDefTable;
                m.Tag = new MethodData(data, m);
            }
            foreach (object obj in this.Tables[(int)MetadataTables.MemberRef])
            {
                MemberRefTable m  = obj as MemberRefTable;
                MethodData     md = new MethodData(data, m);
                m.Tag = md;
                if (m.Class == 0)
                {
                    continue;
                }
                m.ParentTable = this.GetTable(this.GetToken(CodedIndices.MemberRefParent, m.Class));
                if (md.IsField)
                {
                    m.ParentTable.Children[(int)Children.RefField].Add(m);
                }
                else
                {
                    m.ParentTable.Children[(int)Children.RefMethod].Add(m);
                }
            }
            foreach (object obj in this.Tables[(int)MetadataTables.ImplMap])
            {
                ImplMapTable   im = obj as ImplMapTable;
                MethodDefTable m  = this.GetTable(this.GetToken(CodedIndices.MemberForwarded, im.MemberForwarded)) as MethodDefTable;
                if (m != null)
                {
                    (m.Tag as MethodData).ImplMap = im;
                }
            }
            foreach (object obj in this.Tables[(int)MetadataTables.NestedClass])
            {
                NestedClassTable nc  = obj as NestedClassTable;
                TypeDefTable     nst = listType[nc.NestedClass - 1] as TypeDefTable;
                TypeDefTable     enc = listType[nc.EnclosingClass - 1] as TypeDefTable;
                nst.ParentTable = enc;
                enc.Children[(int)Children.DefNested].Add(nst);
            }
        }
 public virtual void VisitTypeDefTable(TypeDefTable table)
 {
 }
Example #21
0
    public bool WriteAsm(string asm)
    {
        MethodDefTable m = this.pedata.idxm.GetTable(this.pedata.cli.EntryPointToken) as MethodDefTable;

        if (m == null)
        {
            System.Console.WriteLine("Can not find entry point!");
            return(false);
        }
        this.entryPoint = m.Tag as MethodData;
        System.Console.WriteLine("Entry Point: {0}", this.entryPoint.FullName);

        bool         ok = true;
        FileStream   fs = new FileStream(asm, FileMode.Create);
        StreamWriter sw = new StreamWriter(fs);

        this.hashUS = new Hashtable();
        this.listUS = new ArrayList();
        if (this.pedata.usrstr != null)
        {
            int ad = this.pedata.usrstr.GetDataOffset(), ptr = 1;
            while (this.data[ad + ptr] != 0 && ptr < this.pedata.usrstr.Size)
            {
                DoubleInt dataSize = this.pedata.usrstr.GetDataSize(ad + ptr);
                byte[]    bytes    = Girl.PEAnalyzer.Util.GetBytes(this.data, ad + ptr + dataSize.A, dataSize.B);
                this.hashUS[ptr] = Encoding.Unicode.GetString(bytes, 0, dataSize.B - 1);
                ptr += dataSize.A + dataSize.B;
            }
        }

        this.number = 0;
        sw.WriteLine("; This file was automatically generated by IL2Asm16.");
        if (this.pre != null)
        {
            this.InsertFile(sw, this.pre);
        }
        sw.WriteLine();
        sw.WriteLine("[bits 16]");
        if (!this.noMain)
        {
            sw.WriteLine("jmp {0}", this.jumpTo);
            sw.WriteLine();

            sw.WriteLine("ILMain:");
            foreach (object obj in this.pedata.idxm.Tables[(int)MetadataTables.TypeDef])
            {
                TypeDefTable t = obj as TypeDefTable;
                foreach (object obj2 in t.Children[(int)Children.DefMethod])
                {
                    MethodDefTable mdt = obj2 as MethodDefTable;
                    MethodData     md  = mdt.Tag as MethodData;
                    if (!md.Name.EndsWith("::.cctor"))
                    {
                        continue;
                    }

                    sw.WriteLine("\tcall\t{0}", Util.MangleFunction(md));
                }
            }
            sw.WriteLine("\tcall\t{0}", Util.MangleFunction(this.entryPoint));
            sw.WriteLine("\tret");
        }

        foreach (object obj in this.pedata.idxm.Tables[(int)MetadataTables.TypeDef])
        {
            TypeDefTable t = obj as TypeDefTable;
            sw.WriteLine();
            if (!this.WriteAsm(sw, t))
            {
                ok = false;
            }
        }

        if (this.listUS.Count > 0)
        {
            sw.WriteLine();
            this.listUS.Sort();
            foreach (object obj in this.listUS)
            {
                int           ptr   = (int)obj;
                string        str   = this.hashUS[ptr] as string;
                bool          instr = false;
                StringBuilder sb    = new StringBuilder();
                foreach (char ch in str)
                {
                    if (ch < ' ')
                    {
                        if (instr)
                        {
                            sb.Append('\"');
                            instr = false;
                        }
                        if (sb.Length > 0)
                        {
                            sb.Append(", ");
                        }
                        sb.AppendFormat("0x{0:x2}", (int)ch);
                    }
                    else
                    {
                        if (!instr)
                        {
                            sb.Append('\"');
                            instr = true;
                        }
                        sb.Append(ch);
                    }
                }
                if (instr)
                {
                    sb.Append('\"');
                }
                sw.WriteLine("US_{0:X8} db {1}, {2}, 0x00", ptr, str.Length, sb);
            }
        }

        if (this.post != null)
        {
            this.InsertFile(sw, this.post);
        }
        sw.Close();
        fs.Close();
        return(ok);
    }