Exemplo n.º 1
0
 internal MetadataReader(string path)
 {
     MemoryMappedFile memmap = this.memmap = new MemoryMappedFile(path);
     try
     {
         this.cursor = new MemoryCursor(memmap);
         //^ base();
         ReadHeader();
     }
     catch
     {
         this.Dispose();
         throw;
     }
 }
Exemplo n.º 2
0
 internal MemoryCursor/*!*/ GetNewCursor(int RVA, out PESection targetSection)
 {
     MemoryCursor c = new MemoryCursor(this.cursor);
     c.Position = this.RvaToOffset(RVA, out targetSection);
     return c;
 }
Exemplo n.º 3
0
        internal static NTHeader/*!*/ ReadNTHeader(MemoryCursor/*!*/ c)
        {
            NTHeader header = new NTHeader();
            header.signature = c.ReadInt32();
            header.machine = c.ReadUInt16();
            header.numberOfSections = c.ReadUInt16();
            header.timeDateStamp = c.ReadInt32();
            header.pointerToSymbolTable = c.ReadInt32();
            header.numberOfSymbols = c.ReadInt32();
            header.sizeOfOptionalHeader = c.ReadUInt16();
            header.characteristics = c.ReadUInt16();
            header.magic = c.ReadUInt16();
            header.majorLinkerVersion = c.ReadByte();
            header.minorLinkerVersion = c.ReadByte();
            header.sizeOfCode = c.ReadInt32();
            header.sizeOfInitializedData = c.ReadInt32();
            header.sizeOfUninitializedData = c.ReadInt32();
            header.addressOfEntryPoint = c.ReadInt32();
            header.baseOfCode = c.ReadInt32();
            if (header.magic == 0x10B)
            {
                header.baseOfData = c.ReadInt32();
                header.imageBase = c.ReadInt32();
            }
            else
            {
                header.baseOfData = 0;
                header.imageBase = c.ReadInt64();
            }
            header.sectionAlignment = c.ReadInt32();
            header.fileAlignment = c.ReadInt32();
            header.majorOperatingSystemVersion = c.ReadUInt16();
            header.minorOperatingSystemVersion = c.ReadUInt16();
            header.majorImageVersion = c.ReadUInt16();
            header.minorImageVersion = c.ReadUInt16();
            header.majorSubsystemVersion = c.ReadUInt16();
            header.minorSubsystemVersion = c.ReadUInt16();
            header.win32VersionValue = c.ReadInt32();
            header.sizeOfImage = c.ReadInt32();
            header.sizeOfHeaders = c.ReadInt32();
            header.checkSum = c.ReadInt32();
            header.subsystem = c.ReadUInt16();
            header.dllCharacteristics = c.ReadUInt16();
            if (header.magic == 0x10B)
            {
                header.sizeOfStackReserve = c.ReadInt32();
                header.sizeOfStackCommit = c.ReadInt32();
                header.sizeOfHeapReserve = c.ReadInt32();
                header.sizeOfHeapCommit = c.ReadInt32();
            }
            else
            {
                header.sizeOfStackReserve = c.ReadInt64();
                header.sizeOfStackCommit = c.ReadInt64();
                header.sizeOfHeapReserve = c.ReadInt64();
                header.sizeOfHeapCommit = c.ReadInt64();
            }
            header.loaderFlags = c.ReadInt32();
            header.numberOfDataDirectories = c.ReadInt32();

            // Verify that the header signature and magic number are valid
            if (header.signature != 0x00004550 /* "PE\0\0" */)
                throw new InvalidMetadataException(ExceptionStrings.BadCOFFHeaderSignature);
            if (header.magic != 0x010B && header.magic != 0x020B)
                throw new InvalidMetadataException(ExceptionStrings.BadPEHeaderMagicNumber);

            //Read the data directories
            header.exportTable = ReadDirectoryEntry(c);
            header.importTable = ReadDirectoryEntry(c);
            header.resourceTable = ReadDirectoryEntry(c);
            header.exceptionTable = ReadDirectoryEntry(c);
            header.certificateTable = ReadDirectoryEntry(c);
            header.baseRelocationTable = ReadDirectoryEntry(c);
            header.debugTable = ReadDirectoryEntry(c);
            header.copyrightTable = ReadDirectoryEntry(c);
            header.globalPointerTable = ReadDirectoryEntry(c);
            header.threadLocalStorageTable = ReadDirectoryEntry(c);
            header.loadConfigTable = ReadDirectoryEntry(c);
            header.boundImportTable = ReadDirectoryEntry(c);
            header.importAddressTable = ReadDirectoryEntry(c);
            header.delayImportTable = ReadDirectoryEntry(c);
            header.cliHeaderTable = ReadDirectoryEntry(c);
            header.reserved = ReadDirectoryEntry(c);

            return header;
        }
Exemplo n.º 4
0
 private static StreamHeader ReadStreamHeader(MemoryCursor/*!*/ c)
 {
     StreamHeader header = new StreamHeader();
     header.offset = c.ReadInt32();
     header.size = c.ReadInt32();
     header.name = c.ReadASCII();
     int n = header.name.Length + 1;
     c.Position += (4 - (n % 4)) % 4;
     return header;
 }
Exemplo n.º 5
0
 private static CLIHeader/*!*/ ReadCLIHeader(MemoryCursor/*!*/ c)
 {
     CLIHeader header = new CLIHeader();
     header.cb = c.Int32(0); c.SkipInt32(1);
     header.majorRuntimeVersion = c.UInt16(0);
     header.minorRuntimeVersion = c.UInt16(1); c.SkipUInt16(2);
     header.metaData = ReadDirectoryEntry(c);
     header.flags = c.Int32(0);
     header.entryPointToken = c.Int32(1); c.SkipInt32(2);
     header.resources = ReadDirectoryEntry(c);
     header.strongNameSignature = ReadDirectoryEntry(c);
     header.codeManagerTable = ReadDirectoryEntry(c);
     header.vtableFixups = ReadDirectoryEntry(c);
     header.exportAddressTableJumps = ReadDirectoryEntry(c);
     if (header.majorRuntimeVersion < 2)
         throw new InvalidMetadataException(ExceptionStrings.BadCLIHeader);
     return header;
 }
Exemplo n.º 6
0
 internal static void ReadDOSHeader(MemoryCursor/*!*/ c)
 {
     c.Position = 0;
     int magicNumber = c.UInt16(0);
     if (magicNumber != 0x5a4d) throw new InvalidMetadataException(ExceptionStrings.BadMagicNumber);
     c.Position = 0x3c;
     int ntHeaderOffset = c.Int32(0);
     c.Position = ntHeaderOffset;
 }
Exemplo n.º 7
0
        private TypeNodeList/*!*/ ParseParameterTypes(out TypeNodeList varArgTypes, MemoryCursor/*!*/ sigReader,
          int paramCount, ref bool genericParameterEncountered)
        {
            varArgTypes = null;
            TypeNodeList paramTypes = new TypeNodeList();

            for(int j = 0; j < paramCount; j++)
            {
                TypeNode paramType = this.ParseTypeSignature(sigReader);

                if(paramType == null)
                {
                    // Got a sentinel
                    varArgTypes = new TypeNodeList();
                    j--;
                    continue;
                }

                if(varArgTypes != null)
                {
                    varArgTypes.Add(paramType);
                    continue;
                }

                if(paramType.IsGeneric)
                    genericParameterEncountered = true;

                paramTypes.Add(paramType);
            }

            return paramTypes;
        }
Exemplo n.º 8
0
 private static int ReadWin32ResourceDirectoryHeader(MemoryCursor/*!*/ c)
 {
     c.ReadInt32(); //Characteristics
     c.ReadInt32(); //TimeDate stamp
     c.ReadInt32(); //Version
     int numberOfNamedEntries = c.ReadUInt16();
     int numberOfIdEntries = c.ReadUInt16();
     return numberOfNamedEntries + numberOfIdEntries;
 }
Exemplo n.º 9
0
 private object GetCustomAttributeLiteralValue(MemoryCursor/*!*/ sigReader, ref TypeNode/*!*/ type)
 {
     if (type == null) return sigReader.ReadInt32();
     switch (type.typeCode)
     {
         case ElementType.Boolean: return sigReader.ReadBoolean();
         case ElementType.Char: return sigReader.ReadChar();
         case ElementType.Double: return sigReader.ReadDouble();
         case ElementType.Single: return sigReader.ReadSingle();
         case ElementType.Int16: return sigReader.ReadInt16();
         case ElementType.Int32: return sigReader.ReadInt32();
         case ElementType.Int64: return sigReader.ReadInt64();
         case ElementType.Int8: return sigReader.ReadSByte();
         case ElementType.UInt16: return sigReader.ReadUInt16();
         case ElementType.UInt32: return sigReader.ReadUInt32();
         case ElementType.UInt64: return sigReader.ReadUInt64();
         case ElementType.UInt8: return sigReader.ReadByte();
         case ElementType.String: return ReadSerString(sigReader);
         case ElementType.ValueType:
             EnumNode etype = GetCustomAttributeEnumNode(ref type);
             return this.GetCustomAttributeLiteralValue(sigReader, etype.UnderlyingType);
         case ElementType.Class: return this.GetTypeFromSerializedName(ReadSerString(sigReader));
         case ElementType.SzArray:
             int numElems = sigReader.ReadInt32();
             TypeNode elemType = ((ArrayType)type).ElementType;
             return this.GetCustomAttributeLiteralArray(sigReader, numElems, elemType);
         case ElementType.Object:
             {
                 type = this.ParseTypeSignature(sigReader);
                 return this.GetCustomAttributeLiteralValue(sigReader, ref type);
             }
     }
     throw new InvalidMetadataException(ExceptionStrings.UnexpectedTypeInCustomAttribute);
 }
Exemplo n.º 10
0
 private Array GetCustomAttributeLiteralArray(MemoryCursor/*!*/ sigReader, int numElems, TypeNode/*!*/ elemType)
 {
     Array array = this.ConstructCustomAttributeLiteralArray(numElems, elemType);
     for (int i = 0; i < numElems; i++)
     {
         object elem = this.GetCustomAttributeLiteralValue(sigReader, elemType);
         array.SetValue(elem, i);
     }
     return array;
 }
Exemplo n.º 11
0
 private object GetCustomAttributeLiteralValue(MemoryCursor/*!*/ sigReader, TypeNode/*!*/ type)
 {
     TypeNode/*!*/ t = type;
     object result = this.GetCustomAttributeLiteralValue(sigReader, ref t);
     EnumNode enumType = t as EnumNode;
     if (enumType != null && type == CoreSystemTypes.Object) result = new Literal(result, enumType);
     return result;
 }
Exemplo n.º 12
0
 private void GetCustomAttributeNamedArguments(ExpressionList/*!*/ arguments, ushort numNamed, MemoryCursor/*!*/ sigReader)
 {
     for (int j = 0; j < numNamed; j++)
     {
         int nameTag = sigReader.ReadByte();
         bool mustBox = sigReader.Byte(0) == (byte)ElementType.BoxedEnum;
         TypeNode/*!*/ vType = this.ParseTypeSignature(sigReader);
         Identifier id = sigReader.ReadIdentifierFromSerString();
         object val = this.GetCustomAttributeLiteralValue(sigReader, ref vType);
         Literal lit = val as Literal;
         if (lit == null) lit = new Literal(val, vType);
         NamedArgument narg = new NamedArgument(id, lit);
         narg.Type = vType;
         narg.IsCustomAttributeProperty = nameTag == 0x54;
         narg.ValueIsBoxed = mustBox;
         arguments.Add(narg);
     }
 }
Exemplo n.º 13
0
        private AttributeNode GetCustomAttribute(Method/*!*/ cons, MemoryCursor/*!*/ sigReader, int blobLength)
        {
            AttributeNode attr = new AttributeNode();
            attr.Constructor = new MemberBinding(null, cons);

            int n = cons.Parameters == null ? 0 : cons.Parameters.Count;

            ExpressionList arguments = attr.Expressions = new ExpressionList();

            int posAtBlobStart = sigReader.Position;

            sigReader.ReadUInt16(); //Prolog

            for (int j = 0; j < n; j++)
            {
                TypeNode t = TypeNode.StripModifiers(cons.Parameters[j].Type);
                if (t == null) continue;
                TypeNode/*!*/ pt = t;
                object val = null;
                try
                {
                    val = this.GetCustomAttributeLiteralValue(sigReader, ref pt);
                }
                catch (Exception e)
                {
                    if (this.module.MetadataImportErrors == null) this.module.MetadataImportErrors = new ArrayList();
                    this.module.MetadataImportErrors.Add(e);
                }
                Literal lit = val as Literal;
                if (lit == null) lit = new Literal(val, pt);
                arguments.Add(lit);
            }
            if (sigReader.Position + 1 < posAtBlobStart + blobLength)
            {
                ushort numNamed = sigReader.ReadUInt16();
                this.GetCustomAttributeNamedArguments(arguments, numNamed, sigReader);
            }
            return attr;
        }
Exemplo n.º 14
0
        private AttributeNode GetPermissionAttribute2(MemoryCursor/*!*/ sigReader, System.Security.Permissions.SecurityAction action)
        {
            int typeNameLength = sigReader.ReadCompressedInt();
            string serializedTypeName = sigReader.ReadUTF8(typeNameLength);
            TypeNode attrType = null;
            try
            {
                attrType = this.GetTypeFromSerializedName(serializedTypeName);
            }
            catch (InvalidMetadataException) { }
            if (attrType == null)
            {
                HandleError(this.module, String.Format(CultureInfo.CurrentCulture, ExceptionStrings.CouldNotResolveType, serializedTypeName));
                return null;
            }
            InstanceInitializer cons = attrType.GetConstructor(CoreSystemTypes.SecurityAction);
            if (cons == null)
            {
                HandleError(this.module, String.Format(CultureInfo.CurrentCulture,
                ExceptionStrings.SecurityAttributeTypeDoesNotHaveADefaultConstructor, serializedTypeName));
                return null;
            }

            sigReader.ReadCompressedInt(); //caBlobLength

            int numProps = sigReader.ReadCompressedInt(); //Skip over the number of properties in the CA blob

            ExpressionList arguments = new ExpressionList();

            arguments.Add(new Literal(action, CoreSystemTypes.SecurityAction));

            this.GetCustomAttributeNamedArguments(arguments, (ushort)numProps, sigReader);

            return new AttributeNode(new MemberBinding(null, cons), arguments);
        }
Exemplo n.º 15
0
        private TypeNodeList/*!*/ ParseTypeList(MemoryCursor/*!*/ sigReader)
        {
            int n = sigReader.ReadCompressedInt();
            TypeNodeList result = new TypeNodeList();

            for(int i = 0; i < n; i++)
            {
                TypeNode t = this.ParseTypeSignature(sigReader);

                if(t == null || t == Struct.Dummy)
                {
                    // Can happen when dealing with a primitive type that implements an interface that references the primitive type.
                    // For example, System.String implements IComparable<System.String>.
                    if(this.currentType != null && !CoreSystemTypes.Initialized)
                        t = this.currentType;
                    else
                    {
                        Debug.Assert(false);
                        t = new TypeParameter();
                        t.Name = Identifier.For("Bad type parameter in position " + i);
                        t.DeclaringModule = this.module;
                    }
                }

                result.Add(t);
            }

            return result;
        }
Exemplo n.º 16
0
 internal Win32ResourceList ReadWin32Resources()
 {
     Win32ResourceList rs = new Win32ResourceList();
     int startPos = this.win32ResourcesOffset;
     if (startPos < 0) return rs;
     MemoryCursor c = this.cursor;
     c.Position = startPos;
     int sizeOfTypeDirectory = ReadWin32ResourceDirectoryHeader(c);
     for (int i = 0; i < sizeOfTypeDirectory; i++)
     {
         string TypeName = null;
         int TypeID = c.ReadInt32();
         if (TypeID < 0)
         {
             MemoryCursor nac = new MemoryCursor(c);
             nac.Position = startPos + (TypeID & 0x7FFFFFFF);
             int strLength = nac.ReadUInt16();
             TypeName = nac.ReadUTF16(strLength);
         }
         int offset = c.ReadInt32();
         if (offset >= 0)
             rs.Add(this.ReadWin32ResourceDataEntry(c, startPos + offset, TypeName, TypeID, null, 0, 0));
         else
         {
             MemoryCursor nc = new MemoryCursor(c);
             nc.Position = startPos + (offset & 0x7FFFFFFF);
             int sizeOfNameDirectory = ReadWin32ResourceDirectoryHeader(nc);
             for (int j = 0; j < sizeOfNameDirectory; j++)
             {
                 string Name = null;
                 int ID = nc.ReadInt32();
                 if (ID < 0)
                 {
                     MemoryCursor nac = new MemoryCursor(c);
                     int strLength = nac.ReadUInt16();
                     Name = nac.ReadUTF16(strLength);
                 }
                 offset = nc.ReadInt32();
                 if (offset >= 0)
                     rs.Add(this.ReadWin32ResourceDataEntry(c, startPos + offset, TypeName, TypeID, Name, ID, 0));
                 else
                 {
                     MemoryCursor lc = new MemoryCursor(c);
                     lc.Position = startPos + (offset & 0x7FFFFFFF);
                     int sizeOfLanguageDirectory = ReadWin32ResourceDirectoryHeader(lc);
                     for (int k = 0; k < sizeOfLanguageDirectory; k++)
                     {
                         int LanguageID = lc.ReadInt32();
                         offset = lc.ReadInt32();
                         rs.Add(this.ReadWin32ResourceDataEntry(c, startPos + offset, TypeName, TypeID, Name, ID, LanguageID));
                     }
                 }
             }
         }
     }
     return rs;
 }
Exemplo n.º 17
0
 private bool TypeSignatureIsClass(MemoryCursor/*!*/ sigReader)
 {
     ElementType tok = (ElementType)sigReader.ReadCompressedInt();
     switch (tok)
     {
         case ElementType.Pinned:
         case ElementType.Pointer:
         case ElementType.Reference:
             return this.TypeSignatureIsClass(sigReader);
         case ElementType.OptionalModifier:
         case ElementType.RequiredModifier:
             sigReader.ReadCompressedInt();
             return this.TypeSignatureIsClass(sigReader);
         case ElementType.Class:
             return true;
         case ElementType.GenericTypeInstance:
             return this.TypeSignatureIsClass(sigReader);
         case ElementType.TypeParameter:
             {
                 int pnum = sigReader.ReadCompressedInt();
                 if (this.currentTypeParameters != null && this.currentTypeParameters.Count > pnum)
                 {
                     TypeNode tPar = this.currentTypeParameters[pnum];
                     return tPar != null && tPar is Class;
                 }
                 return false;
             }
         case ElementType.MethodParameter:
             {
                 int pnum = sigReader.ReadCompressedInt();
                 if (this.currentMethodTypeParameters != null && this.currentMethodTypeParameters.Count > pnum)
                 {
                     TypeNode tPar = this.currentMethodTypeParameters[pnum];
                     return tPar != null && tPar is Class;
                 }
                 return false;
             }
         default:
             return false;
     }
 }
Exemplo n.º 18
0
 private Win32Resource ReadWin32ResourceDataEntry(MemoryCursor/*!*/ c, int position,
   string TypeName, int TypeID, string Name, int ID, int LanguageID)
 {
     Win32Resource rsrc = new Win32Resource();
     rsrc.TypeName = TypeName;
     rsrc.TypeId = TypeID;
     rsrc.Name = Name;
     rsrc.Id = ID;
     rsrc.LanguageId = LanguageID;
     c = new MemoryCursor(c);
     c.Position = position;
     int dataRVA = c.ReadInt32();
     int dataSize = c.ReadInt32();
     rsrc.CodePage = c.ReadInt32();
     c.Position = this.RvaToOffset(dataRVA);
     rsrc.Data = c.ReadBytes(dataSize);
     return rsrc;
 }
Exemplo n.º 19
0
 private TypeNode ParseTypeSignature(MemoryCursor/*!*/ sigReader, ref bool pinned)
 {
     bool junk = false;
     return this.ParseTypeSignature(sigReader, ref pinned, ref junk);
 }
Exemplo n.º 20
0
 private static DirectoryEntry ReadDirectoryEntry(MemoryCursor/*!*/ c)
 {
     DirectoryEntry entry = new DirectoryEntry();
     entry.virtualAddress = c.Int32(0);
     entry.size = c.Int32(1); c.SkipInt32(2);
     return entry;
 }
Exemplo n.º 21
0
 private TypeNode ParseTypeSignature(MemoryCursor/*!*/ sigReader, ref bool pinned, ref bool isTypeArgument)
 {
     TypeNode elementType;
     ElementType tok = (ElementType)sigReader.ReadCompressedInt();
     if (tok == ElementType.Pinned)
     {
         pinned = true;
         tok = (ElementType)sigReader.ReadCompressedInt();
     }
     switch (tok)
     {
         case ElementType.Boolean: return CoreSystemTypes.Boolean;
         case ElementType.Char: return CoreSystemTypes.Char;
         case ElementType.Double: return CoreSystemTypes.Double;
         case ElementType.Int16: return CoreSystemTypes.Int16;
         case ElementType.Int32: return CoreSystemTypes.Int32;
         case ElementType.Int64: return CoreSystemTypes.Int64;
         case ElementType.Int8: return CoreSystemTypes.Int8;
         case ElementType.IntPtr: return CoreSystemTypes.IntPtr;
         case ElementType.BoxedEnum:
         case ElementType.Object: return CoreSystemTypes.Object;
         case ElementType.Single: return CoreSystemTypes.Single;
         case ElementType.String: return CoreSystemTypes.String;
         case ElementType.DynamicallyTypedReference: return CoreSystemTypes.DynamicallyTypedReference;
         case ElementType.UInt16: return CoreSystemTypes.UInt16;
         case ElementType.UInt32: return CoreSystemTypes.UInt32;
         case ElementType.UInt64: return CoreSystemTypes.UInt64;
         case ElementType.UInt8: return CoreSystemTypes.UInt8;
         case ElementType.UIntPtr: return CoreSystemTypes.UIntPtr;
         case ElementType.Void: return CoreSystemTypes.Void;
         case ElementType.Pointer:
             elementType = this.ParseTypeSignature(sigReader, ref pinned);
             if (elementType == null) elementType = CoreSystemTypes.Object;
             if (elementType == null) return null;
             return elementType.GetPointerType();
         case ElementType.Reference:
             elementType = this.ParseTypeSignature(sigReader, ref pinned);
             if (elementType == null) elementType = CoreSystemTypes.Object;
             return elementType.GetReferenceType();
         case ElementType.FunctionPointer:
             return this.ParseFunctionPointer(sigReader);
         case ElementType.OptionalModifier:
         case ElementType.RequiredModifier:
             TypeNode modifier = this.DecodeAndGetTypeDefOrRefOrSpec(sigReader.ReadCompressedInt());
             if (modifier == null) modifier = CoreSystemTypes.Object;
             TypeNode modified = this.ParseTypeSignature(sigReader, ref pinned);
             if (modified == null) modified = CoreSystemTypes.Object;
             if (modified == null || modified == null) return null;
             if (tok == ElementType.RequiredModifier)
                 return RequiredModifier.For(modifier, modified);
             else
                 return OptionalModifier.For(modifier, modified);
         case ElementType.Class:
             return this.DecodeAndGetTypeDefOrRefOrSpec(sigReader.ReadCompressedInt());
         case ElementType.ValueType:
             return this.DecodeAndGetTypeDefOrRefOrSpec(sigReader.ReadCompressedInt(), true);
         case ElementType.TypeParameter:
             TypeNode tPar = null;
             int pnum = sigReader.ReadCompressedInt();
             if (this.currentTypeParameters != null && this.currentTypeParameters.Count > pnum)
                 tPar = this.currentTypeParameters[pnum];
             if (tPar == null)
             {
                 HandleError(this.module, String.Format(CultureInfo.CurrentCulture,
                     ExceptionStrings.BadTypeParameterInPositionForType, pnum, this.currentType == null ? "" : this.currentType.FullName));
                 tPar = new TypeParameter();
                 tPar.Name = Identifier.For("Bad type parameter in position " + pnum);
                 tPar.DeclaringModule = this.module;
             }
             isTypeArgument = true;
             return tPar;
         case ElementType.MethodParameter:
             TypeNode mTPar = null;
             pnum = sigReader.ReadCompressedInt();
             if (this.currentMethodTypeParameters != null && this.currentMethodTypeParameters.Count > pnum)
                 mTPar = this.currentMethodTypeParameters[pnum];
             if (mTPar == null)
             {
                 HandleError(this.module, String.Format(CultureInfo.CurrentCulture,
                     ExceptionStrings.BadMethodTypeParameterInPosition, pnum));
                 mTPar = new MethodTypeParameter();
                 mTPar.Name = Identifier.For("Bad method type parameter in position " + pnum);
             }
             isTypeArgument = true;
             return mTPar;
         case ElementType.GenericTypeInstance:
             TypeNodeList savedCurrentTypeParameters = this.currentTypeParameters;
             TypeNode template = this.ParseTypeSignature(sigReader, ref pinned);
             this.currentTypeParameters = savedCurrentTypeParameters;
             if (template == null || template.ConsolidatedTemplateParameters == null) return template; //Likely a dummy type
             if (CoreSystemTypes.Initialized)
             {
                 if (this.currentTypeParameters == null || this.currentTypeParameters.Count == 0)
                     this.currentTypeParameters = template.ConsolidatedTemplateParameters;
                 TypeNodeList genArgs = this.ParseTypeList(sigReader);
                 if (this.module == null) return null;
                 TypeNode genInst = template.GetGenericTemplateInstance(this.module, genArgs);
                 this.currentTypeParameters = savedCurrentTypeParameters;
                 return genInst;
             }
             InterfaceExpression ifaceExpr = new InterfaceExpression(null);
             ifaceExpr.Template = template;
             ifaceExpr.Namespace = template.Namespace;
             ifaceExpr.Name = template.Name;
             ifaceExpr.TemplateArguments = this.ParseTypeList(sigReader);
             this.currentTypeParameters = savedCurrentTypeParameters;
             return ifaceExpr;
         case ElementType.SzArray:
             elementType = this.ParseTypeSignature(sigReader, ref pinned);
             if (elementType == null) elementType = CoreSystemTypes.Object;
             if (elementType == null) return null;
             return elementType.GetArrayType(1);
         case ElementType.Array:
             elementType = this.ParseTypeSignature(sigReader, ref pinned);
             if (elementType == null) elementType = CoreSystemTypes.Object;
             if (elementType == null) return null;
             int rank = sigReader.ReadCompressedInt();
             int numSizes = sigReader.ReadCompressedInt();
             int[] sizes = new int[numSizes];
             for (int i = 0; i < numSizes; i++) sizes[i] = sigReader.ReadCompressedInt();
             int numLoBounds = sigReader.ReadCompressedInt();
             int[] loBounds = new int[numLoBounds];
             for (int i = 0; i < numLoBounds; i++) loBounds[i] = sigReader.ReadCompressedInt();
             return elementType.GetArrayType(rank, numSizes, numLoBounds, sizes, loBounds);
         case ElementType.Sentinel: return null;
         case ElementType.Type: return CoreSystemTypes.Type;
         case ElementType.Enum: return this.GetTypeFromSerializedName(ReadSerString(sigReader));
     }
     throw new InvalidMetadataException(ExceptionStrings.MalformedSignature);
 }
Exemplo n.º 22
0
 private static MetadataHeader/*!*/ ReadMetadataHeader(MemoryCursor/*!*/ c)
 {
     MetadataHeader header = new MetadataHeader();
     header.signature = c.ReadInt32();
     if (header.signature != 0x424a5342)
         throw new InvalidMetadataException(ExceptionStrings.BadMetadataHeaderSignature);
     header.majorVersion = c.ReadUInt16();
     header.minorVersion = c.ReadUInt16();
     header.reserved = c.ReadInt32();
     int len = c.ReadInt32();
     header.versionString = c.ReadASCII(len);
     while (len++ % 4 != 0) c.ReadByte();
     header.flags = c.ReadUInt16();
     int n = c.ReadUInt16();
     StreamHeader[] streamHeaders = header.streamHeaders = new StreamHeader[n];
     for (int i = 0; i < n; i++)
         streamHeaders[i] = ReadStreamHeader(c);
     return header;
 }
Exemplo n.º 23
0
        private FunctionPointer/*!*/ ParseFunctionPointer(MemoryCursor/*!*/ sigReader)
        {
            CallingConventionFlags convention = (CallingConventionFlags)sigReader.ReadByte();
            int n = sigReader.ReadCompressedInt();
            TypeNode returnType = this.ParseTypeSignature(sigReader);

            if(returnType == null)
                returnType = CoreSystemTypes.Object;

            TypeNodeList parameterTypes = new TypeNodeList();
            int m = n;

            for(int i = 0; i < n; i++)
            {
                TypeNode t = this.ParseTypeSignature(sigReader);

                if (t == null)
                    m = i--;
                else
                    parameterTypes.Add(t);
            }

            FunctionPointer fp = FunctionPointer.For(parameterTypes, returnType);
            fp.CallingConvention = convention;
            fp.VarArgStart = m;

            return fp;
        }
Exemplo n.º 24
0
 internal static SectionHeader ReadSectionHeader(MemoryCursor/*!*/ c)
 {
     SectionHeader header = new SectionHeader();
     header.name = c.ReadASCII(8);
     header.virtualSize = c.Int32(0);
     header.virtualAddress = c.Int32(1);
     header.sizeOfRawData = c.Int32(2);
     header.pointerToRawData = c.Int32(3);
     header.pointerToRelocations = c.Int32(4);
     header.pointerToLinenumbers = c.Int32(5); c.SkipInt32(6);
     header.numberOfRelocations = c.UInt16(0);
     header.numberOfLinenumbers = c.UInt16(1); c.SkipInt16(2);
     header.characteristics = c.Int32(0); c.SkipInt32(1);
     return header;
 }
Exemplo n.º 25
0
 internal ILParser(Reader/*!*/ reader, Method/*!*/ method, int methodIndex, int RVA)
 {
     this.reader = reader;
     this.bodyReader = reader.tables.GetNewCursor();
     this.method = method;
     this.method.LocalList = this.locals;
     this.methodIndex = methodIndex;
     this.RVA = RVA;
     //^ base();
 }
Exemplo n.º 26
0
 private static TablesHeader/*!*/ ReadTablesHeader(MemoryCursor/*!*/ c)
 {
     TablesHeader header = new TablesHeader();
     header.reserved = c.ReadInt32(); // Must be zero
     header.majorVersion = c.ReadByte();  // Must be one
     header.minorVersion = c.ReadByte();  // Must be zero
     header.heapSizes = c.ReadByte();  // Bits for heap sizes
     header.rowId = c.ReadByte();  // log-base-2 of largest rowId
     header.maskValid = c.ReadInt64(); // Present table counts
     header.maskSorted = c.ReadInt64(); // Sorted tables
     int n = 0;
     ulong mask = (ulong)header.maskValid;
     while (mask != 0)
     {
         if (mask % 2 == 1) n++;
         mask /= 2;
     }
     int[] countArray = header.countArray = new int[n];
     for (int i = 0; i < n; i++)
         countArray[i] = c.ReadInt32();
     return header;
 }
Exemplo n.º 27
0
        protected void ParseHeader()
        {
            byte header = this.reader.tables.GetMethodBodyHeaderByte(this.RVA);
            if ((header & 0x3) == 2)
            {
                this.size = header >> 2;
                this.bodyReader = this.reader.tables.GetNewCursor();
                this.reader.tables.Skip(size);
            }
            else
            {
                method.InitLocals = (header & 0x10) != 0;
                byte header2 = this.reader.tables.GetByte();
                int fatHeaderSize = header2 >> 4;
                if (fatHeaderSize == 2) return;
                if (fatHeaderSize != 3) throw new InvalidMetadataException(ExceptionStrings.InvalidFatMethodHeader);
                this.reader.tables.Skip(2); //Skip over maxstack. No need to remember it.
                this.size = this.reader.tables.GetInt32();
                int localIndex = this.reader.tables.GetInt32();
                this.bodyReader = this.reader.tables.GetNewCursor();
                this.reader.tables.Skip(size);
                this.reader.tables.AlignTo32BitBoundary();
                while ((header & 0x8) != 0)
                {
                    header = this.reader.tables.GetByte();
                    if ((header & 3) != 1) throw new InvalidMetadataException(ExceptionStrings.BadMethodHeaderSection);
                    if ((header & 0x80) != 0) throw new InvalidMetadataException(ExceptionStrings.TooManyMethodHeaderSections);
                    this.ParseExceptionHandlerEntry((header & 0x40) == 0);
                }

                Hashtable localSourceNames = new Hashtable();

                if (this.reader.getDebugSymbols && this.reader.debugReader != null)
                {
                    ISymUnmanagedMethod methodInfo = null;
                    try
                    {
                        try
                        {
                            this.reader.debugReader.GetMethod(0x6000000 | (uint)methodIndex, ref methodInfo);
                            if (methodInfo != null)
                            {
                                ISymUnmanagedScope rootScope = methodInfo.GetRootScope();
                                try
                                {
                                    this.reader.GetLocalSourceNames(rootScope, localSourceNames);
                                }
                                finally
                                {
                                    if (rootScope != null)
                                        Marshal.ReleaseComObject(rootScope);
                                }
                            }
                        }
                        catch (COMException)
                        {
                        }
                        catch (InvalidCastException)
                        {
                        }
                        catch (System.Runtime.InteropServices.InvalidComObjectException) { }
                    }
                    finally
                    {
                        if (methodInfo != null)
                            Marshal.ReleaseComObject(methodInfo);
                    }
                }

                this.reader.GetLocals(localIndex, this.locals, localSourceNames);
            }
        }
Exemplo n.º 28
0
 internal MetadataReader(byte* buffer, int length)
 {
     this.cursor = new MemoryCursor(buffer, length);
     //^ base();
     ReadHeader();
 }
Exemplo n.º 29
0
 private static string ReadSerString(MemoryCursor/*!*/ sigReader)
 {
     int n = sigReader.ReadCompressedInt();
     if (n < 0) return null;
     return sigReader.ReadUTF8(n);
 }
Exemplo n.º 30
0
 internal MemoryCursor(MemoryCursor/*!*/ c)
 {
     this.buffer = c.buffer;
     this.pb = c.pb;
     this.Length = c.Length;
 }
Exemplo n.º 31
0
 internal MemoryCursor(MemoryCursor /*!*/ c)
 {
     this.buffer = c.buffer;
     this.pb     = c.pb;
     this.Length = c.Length;
 }