Пример #1
0
		public StorageStreamNode(HexDocument doc, StreamHeader sh, int streamNumber, DotNetStream knownStream, IMetaData md)
			: base((ulong)sh.StartOffset, (ulong)sh.EndOffset - 1) {
			this.streamNumber = streamNumber;
			this.storageStreamType = GetStorageStreamType(knownStream);
			this.storageStreamVM = new StorageStreamVM(this, doc, StartOffset, (int)(EndOffset - StartOffset + 1 - 8));

			var tblStream = knownStream as TablesStream;
			if (tblStream != null)
				this.newChild = new TablesStreamNode(doc, tblStream, md);
		}
Пример #2
0
		public StorageStreamNode(HexBuffer buffer, StreamHeader sh, int streamNumber, DotNetStream knownStream, IMetaData md)
			: base(HexSpan.FromBounds((ulong)sh.StartOffset, (ulong)sh.EndOffset)) {
			StreamNumber = streamNumber;
			StorageStreamType = GetStorageStreamType(knownStream);
			storageStreamVM = new StorageStreamVM(buffer, Span.Start, (int)(Span.Length - 8).ToUInt64());

			var tblStream = knownStream as TablesStream;
			if (tblStream != null)
				newChild = new TablesStreamNode(buffer, tblStream, md);
		}
Пример #3
0
		static StorageStreamType GetStorageStreamType(DotNetStream stream) {
			if (stream == null)
				return StorageStreamType.None;
			if (stream is StringsStream)
				return StorageStreamType.Strings;
			if (stream is USStream)
				return StorageStreamType.US;
			if (stream is BlobStream)
				return StorageStreamType.Blob;
			if (stream is GuidStream)
				return StorageStreamType.Guid;
			if (stream is TablesStream)
				return StorageStreamType.Tables;
			if (stream.Name == "#!")
				return StorageStreamType.HotHeap;
			Debug.Fail(string.Format("Shouldn't be here when stream is a known stream type: {0}", stream.GetType()));
			return StorageStreamType.None;
		}
Пример #4
0
        /// <inheritdoc/>
        protected override void Initialize2()
        {
            IImageStream imageStream = null;
            DotNetStream dns         = null;

            try {
                var mdRva = cor20Header.MetaData.VirtualAddress;
                foreach (var sh in mdHeader.StreamHeaders)
                {
                    var rva = mdRva + sh.Offset;
                    imageStream = peImage.CreateStream(rva, sh.StreamSize);
                    switch (sh.Name.ToUpperInvariant())
                    {
                    case "#STRINGS":
                        if (stringsStream == null)
                        {
                            stringsStream = new StringsStream(imageStream, sh);
                            imageStream   = null;
                            allStreams.Add(stringsStream);
                            continue;
                        }
                        break;

                    case "#US":
                        if (usStream == null)
                        {
                            usStream    = new USStream(imageStream, sh);
                            imageStream = null;
                            allStreams.Add(usStream);
                            continue;
                        }
                        break;

                    case "#BLOB":
                        if (blobStream == null)
                        {
                            blobStream  = new BlobStream(imageStream, sh);
                            imageStream = null;
                            allStreams.Add(blobStream);
                            continue;
                        }
                        break;

                    case "#GUID":
                        if (guidStream == null)
                        {
                            guidStream  = new GuidStream(imageStream, sh);
                            imageStream = null;
                            allStreams.Add(guidStream);
                            continue;
                        }
                        break;

                    case "#-":
                        if (tablesStream == null)
                        {
                            tablesStream = new TablesStream(imageStream, sh);
                            imageStream  = null;
                            allStreams.Add(tablesStream);
                            continue;
                        }
                        break;
                    }
                    dns         = new DotNetStream(imageStream, sh);
                    imageStream = null;
                    allStreams.Add(dns);
                    dns = null;
                }
            }
            finally {
                if (imageStream != null)
                {
                    imageStream.Dispose();
                }
                if (dns != null)
                {
                    dns.Dispose();
                }
            }

            if (tablesStream == null)
            {
                throw new BadImageFormatException("Missing MD stream");
            }
            tablesStream.Initialize(peImage);

            // The pointer tables are used iff row count != 0
            hasFieldPtr    = !tablesStream.FieldPtrTable.IsEmpty;
            hasMethodPtr   = !tablesStream.MethodPtrTable.IsEmpty;
            hasParamPtr    = !tablesStream.ParamPtrTable.IsEmpty;
            hasEventPtr    = !tablesStream.EventPtrTable.IsEmpty;
            hasPropertyPtr = !tablesStream.PropertyPtrTable.IsEmpty;
            hasDeletedRows = tablesStream.HasDelete;
        }
Пример #5
0
        /// <inheritdoc/>
        protected override void InitializeInternal(DataReaderFactory mdReaderFactory, uint metadataBaseOffset)
        {
            DotNetStream dns = null;

            try {
                if (runtime == CLRRuntimeReaderKind.Mono)
                {
                    var newAllStreams = new List <DotNetStream>(allStreams);
                    for (int i = mdHeader.StreamHeaders.Count - 1; i >= 0; i--)
                    {
                        var sh = mdHeader.StreamHeaders[i];
                        switch (sh.Name)
                        {
                        case "#Strings":
                            if (stringsStream is null)
                            {
                                stringsStream = new StringsStream(mdReaderFactory, metadataBaseOffset, sh);
                                newAllStreams.Add(stringsStream);
                                continue;
                            }
                            break;

                        case "#US":
                            if (usStream is null)
                            {
                                usStream = new USStream(mdReaderFactory, metadataBaseOffset, sh);
                                newAllStreams.Add(usStream);
                                continue;
                            }
                            break;

                        case "#Blob":
                            if (blobStream is null)
                            {
                                blobStream = new BlobStream(mdReaderFactory, metadataBaseOffset, sh);
                                newAllStreams.Add(blobStream);
                                continue;
                            }
                            break;

                        case "#GUID":
                            if (guidStream is null)
                            {
                                guidStream = new GuidStream(mdReaderFactory, metadataBaseOffset, sh);
                                newAllStreams.Add(guidStream);
                                continue;
                            }
                            break;

                        case "#~":
                        case "#-":
                            if (tablesStream is null)
                            {
                                tablesStream = new TablesStream(mdReaderFactory, metadataBaseOffset, sh, runtime);
                                newAllStreams.Add(tablesStream);
                                continue;
                            }
                            break;

                        case "#Pdb":
                            if (isStandalonePortablePdb && pdbStream is null)
                            {
                                pdbStream = new PdbStream(mdReaderFactory, metadataBaseOffset, sh);
                                newAllStreams.Add(pdbStream);
                                continue;
                            }
                            break;
                        }
                        dns = new CustomDotNetStream(mdReaderFactory, metadataBaseOffset, sh);
                        newAllStreams.Add(dns);
                        dns = null;
                    }
                    newAllStreams.Reverse();
                    allStreams = newAllStreams;
                }
                else
                {
                    Debug.Assert(runtime == CLRRuntimeReaderKind.CLR);
                    foreach (var sh in mdHeader.StreamHeaders)
                    {
                        switch (sh.Name.ToUpperInvariant())
                        {
                        case "#STRINGS":
                            if (stringsStream is null)
                            {
                                stringsStream = new StringsStream(mdReaderFactory, metadataBaseOffset, sh);
                                allStreams.Add(stringsStream);
                                continue;
                            }
                            break;

                        case "#US":
                            if (usStream is null)
                            {
                                usStream = new USStream(mdReaderFactory, metadataBaseOffset, sh);
                                allStreams.Add(usStream);
                                continue;
                            }
                            break;

                        case "#BLOB":
                            if (blobStream is null)
                            {
                                blobStream = new BlobStream(mdReaderFactory, metadataBaseOffset, sh);
                                allStreams.Add(blobStream);
                                continue;
                            }
                            break;

                        case "#GUID":
                            if (guidStream is null)
                            {
                                guidStream = new GuidStream(mdReaderFactory, metadataBaseOffset, sh);
                                allStreams.Add(guidStream);
                                continue;
                            }
                            break;

                        case "#~":                          // Only if #Schema is used
                        case "#-":
                            if (tablesStream is null)
                            {
                                tablesStream = new TablesStream(mdReaderFactory, metadataBaseOffset, sh, runtime);
                                allStreams.Add(tablesStream);
                                continue;
                            }
                            break;

                        case "#PDB":
                            // Case sensitive comparison since it's a stream that's not read by the CLR,
                            // only by other libraries eg. System.Reflection.Metadata.
                            if (isStandalonePortablePdb && pdbStream is null && sh.Name == "#Pdb")
                            {
                                pdbStream = new PdbStream(mdReaderFactory, metadataBaseOffset, sh);
                                allStreams.Add(pdbStream);
                                continue;
                            }
                            break;
                        }
                        dns = new CustomDotNetStream(mdReaderFactory, metadataBaseOffset, sh);
                        allStreams.Add(dns);
                        dns = null;
                    }
                }
            }
            finally {
                dns?.Dispose();
            }

            if (tablesStream is null)
            {
                throw new BadImageFormatException("Missing MD stream");
            }

            if (!(pdbStream is null))
            {
                tablesStream.Initialize(pdbStream.TypeSystemTableRows);
            }
Пример #6
0
		public MDStreamModel(IMetaData metadata, DotNetStream stream) {
			MetaData = metadata;
			Stream = stream;
			Text = stream.Name;
		}
Пример #7
0
		/// <inheritdoc/>
		protected override void InitializeInternal() {
			var hotHeapVersion = GetHotHeapVersion(peImage.FileName, mdHeader.VersionString);

			IImageStream imageStream = null, fullStream = null;
			DotNetStream dns = null;
			List<HotStream> hotStreams = null;
			HotStream hotStream = null;
			var newAllStreams = new List<DotNetStream>(allStreams);
			try {
				var mdRva = cor20Header.MetaData.VirtualAddress;
				for (int i = mdHeader.StreamHeaders.Count - 1; i >= 0; i--) {
					var sh = mdHeader.StreamHeaders[i];
					var rva = mdRva + sh.Offset;
					var fileOffset = peImage.ToFileOffset(rva);
					imageStream = peImage.CreateStream(fileOffset, sh.StreamSize);
					switch (sh.Name) {
					case "#Strings":
						if (stringsStream == null) {
							stringsStream = new StringsStream(imageStream, sh);
							imageStream = null;
							newAllStreams.Add(stringsStream);
							continue;
						}
						break;

					case "#US":
						if (usStream == null) {
							usStream = new USStream(imageStream, sh);
							imageStream = null;
							newAllStreams.Add(usStream);
							continue;
						}
						break;

					case "#Blob":
						if (blobStream == null) {
							blobStream = new BlobStream(imageStream, sh);
							imageStream = null;
							newAllStreams.Add(blobStream);
							continue;
						}
						break;

					case "#GUID":
						if (guidStream == null) {
							guidStream = new GuidStream(imageStream, sh);
							imageStream = null;
							newAllStreams.Add(guidStream);
							continue;
						}
						break;

					case "#~":
						if (tablesStream == null) {
							tablesStream = new TablesStream(imageStream, sh);
							imageStream = null;
							newAllStreams.Add(tablesStream);
							continue;
						}
						break;

					case "#!":
						if (hotStreams == null)
							hotStreams = new List<HotStream>();
						fullStream = peImage.CreateFullStream();
						hotStream = HotStream.Create(hotHeapVersion, imageStream, sh, fullStream, fileOffset);
						fullStream = null;
						hotStreams.Add(hotStream);
						newAllStreams.Add(hotStream);
						hotStream = null;
						imageStream = null;
						continue;
					}
					dns = new DotNetStream(imageStream, sh);
					imageStream = null;
					newAllStreams.Add(dns);
					dns = null;
				}
			}
			finally {
				if (imageStream != null)
					imageStream.Dispose();
				if (fullStream != null)
					fullStream.Dispose();
				if (dns != null)
					dns.Dispose();
				if (hotStream != null)
					hotStream.Dispose();
				newAllStreams.Reverse();
				allStreams = ThreadSafeListCreator.MakeThreadSafe(newAllStreams);
			}

			if (tablesStream == null)
				throw new BadImageFormatException("Missing MD stream");

			if (hotStreams != null) {
				hotStreams.Reverse();
				InitializeHotStreams(hotStreams);
			}

			tablesStream.Initialize(peImage);
		}
Пример #8
0
		/// <inheritdoc/>
		protected override void InitializeInternal() {
			IImageStream imageStream = null;
			DotNetStream dns = null;
			try {
				var mdRva = cor20Header.MetaData.VirtualAddress;
				foreach (var sh in mdHeader.StreamHeaders) {
					var rva = mdRva + sh.Offset;
					imageStream = peImage.CreateStream(rva, sh.StreamSize);
					switch (sh.Name.ToUpperInvariant()) {
					case "#STRINGS":
						if (stringsStream == null) {
							stringsStream = new StringsStream(imageStream, sh);
							imageStream = null;
							allStreams.Add(stringsStream);
							continue;
						}
						break;

					case "#US":
						if (usStream == null) {
							usStream = new USStream(imageStream, sh);
							imageStream = null;
							allStreams.Add(usStream);
							continue;
						}
						break;

					case "#BLOB":
						if (blobStream == null) {
							blobStream = new BlobStream(imageStream, sh);
							imageStream = null;
							allStreams.Add(blobStream);
							continue;
						}
						break;

					case "#GUID":
						if (guidStream == null) {
							guidStream = new GuidStream(imageStream, sh);
							imageStream = null;
							allStreams.Add(guidStream);
							continue;
						}
						break;

					case "#~":	// Only if #Schema is used
					case "#-":
						if (tablesStream == null) {
							tablesStream = new TablesStream(imageStream, sh);
							imageStream = null;
							allStreams.Add(tablesStream);
							continue;
						}
						break;
					}
					dns = new DotNetStream(imageStream, sh);
					imageStream = null;
					allStreams.Add(dns);
					dns = null;
				}
			}
			finally {
				if (imageStream != null)
					imageStream.Dispose();
				if (dns != null)
					dns.Dispose();
			}

			if (tablesStream == null)
				throw new BadImageFormatException("Missing MD stream");
			tablesStream.Initialize(peImage);

			// The pointer tables are used iff row count != 0
			hasFieldPtr = !tablesStream.FieldPtrTable.IsEmpty;
			hasMethodPtr = !tablesStream.MethodPtrTable.IsEmpty;
			hasParamPtr = !tablesStream.ParamPtrTable.IsEmpty;
			hasEventPtr = !tablesStream.EventPtrTable.IsEmpty;
			hasPropertyPtr = !tablesStream.PropertyPtrTable.IsEmpty;
			hasDeletedRows = tablesStream.HasDelete;
		}
Пример #9
0
        /// <inheritdoc/>
        protected override void InitializeInternal(DataReaderFactory mdReaderFactory, uint metadataBaseOffset)
        {
            DotNetStream dns         = null;
            bool         forceAllBig = false;

            try {
                if (runtime == CLRRuntimeReaderKind.Mono)
                {
                    var newAllStreams = new List <DotNetStream>(allStreams);
                    for (int i = mdHeader.StreamHeaders.Count - 1; i >= 0; i--)
                    {
                        var sh = mdHeader.StreamHeaders[i];
                        switch (sh.Name)
                        {
                        case "#Strings":
                            if (stringsStream is null)
                            {
                                stringsStream = new StringsStream(mdReaderFactory, metadataBaseOffset, sh);
                                newAllStreams.Add(stringsStream);
                                continue;
                            }
                            break;

                        case "#US":
                            if (usStream is null)
                            {
                                usStream = new USStream(mdReaderFactory, metadataBaseOffset, sh);
                                newAllStreams.Add(usStream);
                                continue;
                            }
                            break;

                        case "#Blob":
                            if (blobStream is null)
                            {
                                blobStream = new BlobStream(mdReaderFactory, metadataBaseOffset, sh);
                                newAllStreams.Add(blobStream);
                                continue;
                            }
                            break;

                        case "#GUID":
                            if (guidStream is null)
                            {
                                guidStream = new GuidStream(mdReaderFactory, metadataBaseOffset, sh);
                                newAllStreams.Add(guidStream);
                                continue;
                            }
                            break;

                        case "#~":
                        case "#-":
                            if (tablesStream is null)
                            {
                                tablesStream = new TablesStream(mdReaderFactory, metadataBaseOffset, sh, runtime);
                                newAllStreams.Add(tablesStream);
                                continue;
                            }
                            break;

                        case "#Pdb":
                            if (isStandalonePortablePdb && pdbStream is null)
                            {
                                pdbStream = new PdbStream(mdReaderFactory, metadataBaseOffset, sh);
                                newAllStreams.Add(pdbStream);
                                continue;
                            }
                            break;

                        case "#JTD":
                            forceAllBig = true;
                            continue;
                        }
                        dns = new CustomDotNetStream(mdReaderFactory, metadataBaseOffset, sh);
                        newAllStreams.Add(dns);
                        dns = null;
                    }
                    newAllStreams.Reverse();
                    allStreams = newAllStreams;
                }
                else
                {
                    Debug.Assert(runtime == CLRRuntimeReaderKind.CLR);
                    foreach (var sh in mdHeader.StreamHeaders)
                    {
                        switch (sh.Name.ToUpperInvariant())
                        {
                        case "#STRINGS":
                            if (stringsStream is null)
                            {
                                stringsStream = new StringsStream(mdReaderFactory, metadataBaseOffset, sh);
                                allStreams.Add(stringsStream);
                                continue;
                            }
                            break;

                        case "#US":
                            if (usStream is null)
                            {
                                usStream = new USStream(mdReaderFactory, metadataBaseOffset, sh);
                                allStreams.Add(usStream);
                                continue;
                            }
                            break;

                        case "#BLOB":
                            if (blobStream is null)
                            {
                                blobStream = new BlobStream(mdReaderFactory, metadataBaseOffset, sh);
                                allStreams.Add(blobStream);
                                continue;
                            }
                            break;

                        case "#GUID":
                            if (guidStream is null)
                            {
                                guidStream = new GuidStream(mdReaderFactory, metadataBaseOffset, sh);
                                allStreams.Add(guidStream);
                                continue;
                            }
                            break;

                        case "#~":                          // Only if #Schema is used
                        case "#-":
                            if (tablesStream is null)
                            {
                                tablesStream = new TablesStream(mdReaderFactory, metadataBaseOffset, sh, runtime);
                                allStreams.Add(tablesStream);
                                continue;
                            }
                            break;

                        case "#PDB":
                            // Case sensitive comparison since it's a stream that's not read by the CLR,
                            // only by other libraries eg. System.Reflection.Metadata.
                            if (isStandalonePortablePdb && pdbStream is null && sh.Name == "#Pdb")
                            {
                                pdbStream = new PdbStream(mdReaderFactory, metadataBaseOffset, sh);
                                allStreams.Add(pdbStream);
                                continue;
                            }
                            break;

                        case "#JTD":
                            forceAllBig = true;
                            continue;
                        }
                        dns = new CustomDotNetStream(mdReaderFactory, metadataBaseOffset, sh);
                        allStreams.Add(dns);
                        dns = null;
                    }
                }
            }
            finally {
                dns?.Dispose();
            }

            if (tablesStream is null)
            {
                throw new BadImageFormatException("Missing MD stream");
            }

            if (pdbStream is not null)
            {
                tablesStream.Initialize(pdbStream.TypeSystemTableRows, forceAllBig);
            }
            else
            {
                tablesStream.Initialize(null, forceAllBig);
            }

            // The pointer tables are used iff row count != 0
            hasFieldPtr    = !tablesStream.FieldPtrTable.IsEmpty;
            hasMethodPtr   = !tablesStream.MethodPtrTable.IsEmpty;
            hasParamPtr    = !tablesStream.ParamPtrTable.IsEmpty;
            hasEventPtr    = !tablesStream.EventPtrTable.IsEmpty;
            hasPropertyPtr = !tablesStream.PropertyPtrTable.IsEmpty;

            switch (runtime)
            {
            case CLRRuntimeReaderKind.CLR:
                hasDeletedFields    = tablesStream.HasDelete;
                hasDeletedNonFields = tablesStream.HasDelete;
                break;

            case CLRRuntimeReaderKind.Mono:
                hasDeletedFields    = true;
                hasDeletedNonFields = false;
                break;

            default:
                throw new InvalidOperationException();
            }
        }
        /// <inheritdoc/>
        protected override void InitializeInternal(DataReaderFactory mdReaderFactory, uint metadataBaseOffset)
        {
            DotNetStream dns = null;

            try {
                foreach (var sh in mdHeader.StreamHeaders)
                {
                    switch (sh.Name.ToUpperInvariant())
                    {
                    case "#STRINGS":
                        if (stringsStream == null)
                        {
                            stringsStream = new StringsStream(mdReaderFactory, metadataBaseOffset, sh);
                            allStreams.Add(stringsStream);
                            continue;
                        }
                        break;

                    case "#US":
                        if (usStream == null)
                        {
                            usStream = new USStream(mdReaderFactory, metadataBaseOffset, sh);
                            allStreams.Add(usStream);
                            continue;
                        }
                        break;

                    case "#BLOB":
                        if (blobStream == null)
                        {
                            blobStream = new BlobStream(mdReaderFactory, metadataBaseOffset, sh);
                            allStreams.Add(blobStream);
                            continue;
                        }
                        break;

                    case "#GUID":
                        if (guidStream == null)
                        {
                            guidStream = new GuidStream(mdReaderFactory, metadataBaseOffset, sh);
                            allStreams.Add(guidStream);
                            continue;
                        }
                        break;

                    case "#~":                          // Only if #Schema is used
                    case "#-":
                        if (tablesStream == null)
                        {
                            tablesStream = new TablesStream(mdReaderFactory, metadataBaseOffset, sh);
                            allStreams.Add(tablesStream);
                            continue;
                        }
                        break;

                    case "#PDB":
                        // Case sensitive comparison since it's a stream that's not read by the CLR,
                        // only by other libraries eg. System.Reflection.Metadata.
                        if (isStandalonePortablePdb && pdbStream == null && sh.Name == "#Pdb")
                        {
                            pdbStream = new PdbStream(mdReaderFactory, metadataBaseOffset, sh);
                            allStreams.Add(pdbStream);
                            continue;
                        }
                        break;
                    }
                    dns = new CustomDotNetStream(mdReaderFactory, metadataBaseOffset, sh);
                    allStreams.Add(dns);
                    dns = null;
                }
            }
            finally {
                dns?.Dispose();
            }

            if (tablesStream == null)
            {
                throw new BadImageFormatException("Missing MD stream");
            }

            if (pdbStream != null)
            {
                tablesStream.Initialize(pdbStream.TypeSystemTableRows);
            }
            else
            {
                tablesStream.Initialize(null);
            }

            // The pointer tables are used iff row count != 0
            hasFieldPtr    = !tablesStream.FieldPtrTable.IsEmpty;
            hasMethodPtr   = !tablesStream.MethodPtrTable.IsEmpty;
            hasParamPtr    = !tablesStream.ParamPtrTable.IsEmpty;
            hasEventPtr    = !tablesStream.EventPtrTable.IsEmpty;
            hasPropertyPtr = !tablesStream.PropertyPtrTable.IsEmpty;
            hasDeletedRows = tablesStream.HasDelete;
        }
Пример #11
0
		static ContextMenuStrip CreateStreamContextMenu(MDTableHeapView view, IMetaData metadata, DotNetStream stream) {
			var menu = new ContextMenuStrip();

			var show = new ToolStripMenuItem("Show in " + stream.Name + " Stream");
			show.Click += (sender, e) => {
				var gridView = (GridView)menu.SourceControl;
				var rowIndex = gridView.SelectedCells[0].RowIndex;

				if (gridView[4, rowIndex].Value == InvalidValue) {
					MessageBox.Show("Invalid offset.", view.App.AppName, MessageBoxButtons.OK, MessageBoxIcon.Error);
					return;
				}

				var offset = Convert.ToUInt32(gridView[3, rowIndex].Value);
				if (offset == 0)
					return;

				var desc = gridView[4, rowIndex].Value;
				uint size = 1;
				if (stream is StringsStream) {
					size = (uint)((UTF8String)desc).DataLength + 1;
				}
				else if (stream is BlobStream) {
					size = (uint)gridView[4, rowIndex].Value;
					size += (uint)Utils.GetCompressedUInt32Length(size);
				}
				else if (stream is GuidStream) {
					offset = (offset - 1) * 0x10;
					size = 0x10;
				}


				ViewUtils.ShowStream(view.App, view.Model, metadata.PEImage, stream, offset, size);
			};
			menu.Items.Add(show);

			return menu;
		}
Пример #12
0
        /// <inheritdoc/>
        protected override void InitializeInternal(IImageStream mdStream)
        {
            bool         disposeOfMdStream = false;
            IImageStream imageStream       = null;
            DotNetStream dns = null;

            try {
                if (peImage != null)
                {
                    Debug.Assert(mdStream == null);
                    Debug.Assert(cor20Header != null);
                    var mdOffset = peImage.ToFileOffset(cor20Header.MetaData.VirtualAddress);
                    mdStream          = peImage.CreateStream(mdOffset, cor20Header.MetaData.Size);
                    disposeOfMdStream = true;
                }
                else
                {
                    Debug.Assert(mdStream != null);
                }
                foreach (var sh in mdHeader.StreamHeaders)
                {
                    imageStream = mdStream.Create((FileOffset)sh.Offset, sh.StreamSize);
                    switch (sh.Name.ToUpperInvariant())
                    {
                    case "#STRINGS":
                        if (stringsStream == null)
                        {
                            stringsStream = new StringsStream(imageStream, sh);
                            imageStream   = null;
                            allStreams.Add(stringsStream);
                            continue;
                        }
                        break;

                    case "#US":
                        if (usStream == null)
                        {
                            usStream    = new USStream(imageStream, sh);
                            imageStream = null;
                            allStreams.Add(usStream);
                            continue;
                        }
                        break;

                    case "#BLOB":
                        if (blobStream == null)
                        {
                            blobStream  = new BlobStream(imageStream, sh);
                            imageStream = null;
                            allStreams.Add(blobStream);
                            continue;
                        }
                        break;

                    case "#GUID":
                        if (guidStream == null)
                        {
                            guidStream  = new GuidStream(imageStream, sh);
                            imageStream = null;
                            allStreams.Add(guidStream);
                            continue;
                        }
                        break;

                    case "#~":                          // Only if #Schema is used
                    case "#-":
                        if (tablesStream == null)
                        {
                            tablesStream = new TablesStream(imageStream, sh);
                            imageStream  = null;
                            allStreams.Add(tablesStream);
                            continue;
                        }
                        break;

                    case "#PDB":
                        // Case sensitive comparison since it's a stream that's not read by the CLR,
                        // only by other libraries eg. System.Reflection.Metadata.
                        if (isStandalonePortablePdb && pdbStream == null && sh.Name == "#Pdb")
                        {
                            pdbStream   = new PdbStream(imageStream, sh);
                            imageStream = null;
                            allStreams.Add(pdbStream);
                            continue;
                        }
                        break;
                    }
                    dns         = new DotNetStream(imageStream, sh);
                    imageStream = null;
                    allStreams.Add(dns);
                    dns = null;
                }
            }
            finally {
                if (disposeOfMdStream)
                {
                    mdStream.Dispose();
                }
                if (imageStream != null)
                {
                    imageStream.Dispose();
                }
                if (dns != null)
                {
                    dns.Dispose();
                }
            }

            if (tablesStream == null)
            {
                throw new BadImageFormatException("Missing MD stream");
            }

            if (pdbStream != null)
            {
                tablesStream.Initialize(pdbStream.TypeSystemTableRows);
            }
            else
            {
                tablesStream.Initialize(null);
            }

            // The pointer tables are used iff row count != 0
            hasFieldPtr    = !tablesStream.FieldPtrTable.IsEmpty;
            hasMethodPtr   = !tablesStream.MethodPtrTable.IsEmpty;
            hasParamPtr    = !tablesStream.ParamPtrTable.IsEmpty;
            hasEventPtr    = !tablesStream.EventPtrTable.IsEmpty;
            hasPropertyPtr = !tablesStream.PropertyPtrTable.IsEmpty;
            hasDeletedRows = tablesStream.HasDelete;
        }
Пример #13
0
        /// <inheritdoc/>
        protected override void Initialize2()
        {
            IImageStream imageStream = null;
            DotNetStream dns         = null;

            try {
                var mdRva = cor20Header.MetaData.VirtualAddress;
                for (int i = mdHeader.StreamHeaders.Count - 1; i >= 0; i--)
                {
                    var sh  = mdHeader.StreamHeaders[i];
                    var rva = mdRva + sh.Offset;
                    imageStream = peImage.CreateStream(rva, sh.StreamSize);
                    switch (sh.Name)
                    {
                    case "#Strings":
                        if (stringsStream == null)
                        {
                            stringsStream = new StringsStream(imageStream, sh);
                            imageStream   = null;
                            allStreams.Add(stringsStream);
                            continue;
                        }
                        break;

                    case "#US":
                        if (usStream == null)
                        {
                            usStream    = new USStream(imageStream, sh);
                            imageStream = null;
                            allStreams.Add(usStream);
                            continue;
                        }
                        break;

                    case "#Blob":
                        if (blobStream == null)
                        {
                            blobStream  = new BlobStream(imageStream, sh);
                            imageStream = null;
                            allStreams.Add(blobStream);
                            continue;
                        }
                        break;

                    case "#GUID":
                        if (guidStream == null)
                        {
                            guidStream  = new GuidStream(imageStream, sh);
                            imageStream = null;
                            allStreams.Add(guidStream);
                            continue;
                        }
                        break;

                    case "#~":
                        if (tablesStream == null)
                        {
                            tablesStream = new TablesStream(imageStream, sh);
                            imageStream  = null;
                            allStreams.Add(tablesStream);
                            continue;
                        }
                        break;
                    }
                    dns         = new DotNetStream(imageStream, sh);
                    imageStream = null;
                    allStreams.Add(dns);
                    dns = null;
                }
            }
            finally {
                if (imageStream != null)
                {
                    imageStream.Dispose();
                }
                if (dns != null)
                {
                    dns.Dispose();
                }
            }

            allStreams.Reverse();

            if (tablesStream == null)
            {
                throw new BadImageFormatException("Missing MD stream");
            }
            tablesStream.Initialize(peImage);
        }
		/// <inheritdoc/>
		protected override void Initialize2() {
			IImageStream imageStream = null;
			DotNetStream dns = null;
			try {
				var mdRva = cor20Header.MetaData.VirtualAddress;
				for (int i = mdHeader.StreamHeaders.Count - 1; i >= 0; i--) {
					var sh = mdHeader.StreamHeaders[i];
					var rva = mdRva + sh.Offset;
					imageStream = peImage.CreateStream(rva, sh.StreamSize);
					switch (sh.Name) {
					case "#Strings":
						if (stringsStream == null) {
							stringsStream = new StringsStream(imageStream, sh);
							imageStream = null;
							allStreams.Add(stringsStream);
							continue;
						}
						break;

					case "#US":
						if (usStream == null) {
							usStream = new USStream(imageStream, sh);
							imageStream = null;
							allStreams.Add(usStream);
							continue;
						}
						break;

					case "#Blob":
						if (blobStream == null) {
							blobStream = new BlobStream(imageStream, sh);
							imageStream = null;
							allStreams.Add(blobStream);
							continue;
						}
						break;

					case "#GUID":
						if (guidStream == null) {
							guidStream = new GuidStream(imageStream, sh);
							imageStream = null;
							allStreams.Add(guidStream);
							continue;
						}
						break;

					case "#~":
						if (tablesStream == null) {
							tablesStream = new TablesStream(imageStream, sh);
							imageStream = null;
							allStreams.Add(tablesStream);
							continue;
						}
						break;
					}
					dns = new DotNetStream(imageStream, sh);
					imageStream = null;
					allStreams.Add(dns);
					dns = null;
				}
			}
			finally {
				if (imageStream != null)
					imageStream.Dispose();
				if (dns != null)
					dns.Dispose();
			}

			allStreams.Reverse();

			if (tablesStream == null)
				throw new BadImageFormatException("Missing MD stream");
			tablesStream.Initialize(peImage);
		}
Пример #15
0
        /// <inheritdoc/>
        protected override void InitializeInternal(DataReaderFactory mdReaderFactory, uint metadataBaseOffset)
        {
            DotNetStream dns           = null;
            var          newAllStreams = new List <DotNetStream>(allStreams);
            bool         forceAllBig   = false;

            try {
                for (int i = mdHeader.StreamHeaders.Count - 1; i >= 0; i--)
                {
                    var sh = mdHeader.StreamHeaders[i];
                    switch (sh.Name)
                    {
                    case "#Strings":
                        if (stringsStream is null)
                        {
                            stringsStream = new StringsStream(mdReaderFactory, metadataBaseOffset, sh);
                            newAllStreams.Add(stringsStream);
                            continue;
                        }
                        break;

                    case "#US":
                        if (usStream is null)
                        {
                            usStream = new USStream(mdReaderFactory, metadataBaseOffset, sh);
                            newAllStreams.Add(usStream);
                            continue;
                        }
                        break;

                    case "#Blob":
                        if (blobStream is null)
                        {
                            blobStream = new BlobStream(mdReaderFactory, metadataBaseOffset, sh);
                            newAllStreams.Add(blobStream);
                            continue;
                        }
                        break;

                    case "#GUID":
                        if (guidStream is null)
                        {
                            guidStream = new GuidStream(mdReaderFactory, metadataBaseOffset, sh);
                            newAllStreams.Add(guidStream);
                            continue;
                        }
                        break;

                    case "#~":
                        if (tablesStream is null)
                        {
                            tablesStream = new TablesStream(mdReaderFactory, metadataBaseOffset, sh, runtime);
                            newAllStreams.Add(tablesStream);
                            continue;
                        }
                        break;

                    case "#Pdb":
                        if (isStandalonePortablePdb && pdbStream is null)
                        {
                            pdbStream = new PdbStream(mdReaderFactory, metadataBaseOffset, sh);
                            newAllStreams.Add(pdbStream);
                            continue;
                        }
                        break;

                    case "#JTD":
                        if (runtime == CLRRuntimeReaderKind.Mono)
                        {
                            forceAllBig = true;
                            continue;
                        }
                        break;
                    }
                    dns = new CustomDotNetStream(mdReaderFactory, metadataBaseOffset, sh);
                    newAllStreams.Add(dns);
                    dns = null;
                }
            }
            finally {
                dns?.Dispose();
                newAllStreams.Reverse();
                allStreams = newAllStreams;
            }

            if (tablesStream is null)
            {
                throw new BadImageFormatException("Missing MD stream");
            }

            if (pdbStream is not null)
            {
                tablesStream.Initialize(pdbStream.TypeSystemTableRows, forceAllBig);
            }
            else
            {
                tablesStream.Initialize(null, forceAllBig);
            }
        }
Пример #16
0
        /// <inheritdoc/>
        protected override void InitializeInternal()
        {
            var hotHeapVersion = GetHotHeapVersion(peImage.FileName, mdHeader.VersionString);

            IImageStream     imageStream = null, fullStream = null;
            DotNetStream     dns        = null;
            List <HotStream> hotStreams = null;
            HotStream        hotStream  = null;
            var newAllStreams           = new List <DotNetStream>(allStreams);

            try {
                var mdRva = cor20Header.MetaData.VirtualAddress;
                for (int i = mdHeader.StreamHeaders.Count - 1; i >= 0; i--)
                {
                    var sh         = mdHeader.StreamHeaders[i];
                    var rva        = mdRva + sh.Offset;
                    var fileOffset = peImage.ToFileOffset(rva);
                    imageStream = peImage.CreateStream(fileOffset, sh.StreamSize);
                    switch (sh.Name)
                    {
                    case "#Strings":
                        if (stringsStream == null)
                        {
                            stringsStream = new StringsStream(imageStream, sh);
                            imageStream   = null;
                            newAllStreams.Add(stringsStream);
                            continue;
                        }
                        break;

                    case "#US":
                        if (usStream == null)
                        {
                            usStream    = new USStream(imageStream, sh);
                            imageStream = null;
                            newAllStreams.Add(usStream);
                            continue;
                        }
                        break;

                    case "#Blob":
                        if (blobStream == null)
                        {
                            blobStream  = new BlobStream(imageStream, sh);
                            imageStream = null;
                            newAllStreams.Add(blobStream);
                            continue;
                        }
                        break;

                    case "#GUID":
                        if (guidStream == null)
                        {
                            guidStream  = new GuidStream(imageStream, sh);
                            imageStream = null;
                            newAllStreams.Add(guidStream);
                            continue;
                        }
                        break;

                    case "#~":
                        if (tablesStream == null)
                        {
                            tablesStream = new TablesStream(imageStream, sh);
                            imageStream  = null;
                            newAllStreams.Add(tablesStream);
                            continue;
                        }
                        break;

                    case "#!":
                        if (hotStreams == null)
                        {
                            hotStreams = new List <HotStream>();
                        }
                        fullStream = peImage.CreateFullStream();
                        hotStream  = HotStream.Create(hotHeapVersion, imageStream, sh, fullStream, fileOffset);
                        fullStream = null;
                        hotStreams.Add(hotStream);
                        newAllStreams.Add(hotStream);
                        hotStream   = null;
                        imageStream = null;
                        continue;
                    }
                    dns         = new DotNetStream(imageStream, sh);
                    imageStream = null;
                    newAllStreams.Add(dns);
                    dns = null;
                }
            }
            finally {
                if (imageStream != null)
                {
                    imageStream.Dispose();
                }
                if (fullStream != null)
                {
                    fullStream.Dispose();
                }
                if (dns != null)
                {
                    dns.Dispose();
                }
                if (hotStream != null)
                {
                    hotStream.Dispose();
                }
                newAllStreams.Reverse();
                allStreams = ThreadSafeListCreator.MakeThreadSafe(newAllStreams);
            }

            if (tablesStream == null)
            {
                throw new BadImageFormatException("Missing MD stream");
            }

            if (hotStreams != null)
            {
                hotStreams.Reverse();
                InitializeHotStreams(hotStreams);
            }

            tablesStream.Initialize(peImage);
        }
Пример #17
0
        /// <inheritdoc/>
        protected override void InitializeInternal(IImageStream mdStream)
        {
            var hotHeapVersion = peImage == null ? HotHeapVersion.CLR20 : GetHotHeapVersion(peImage.FileName, mdHeader.VersionString);

            bool             disposeOfMdStream = false;
            IImageStream     imageStream = null, fullStream = null;
            DotNetStream     dns = null;
            List <HotStream> hotStreams = null;
            HotStream        hotStream  = null;
            var newAllStreams = new List <DotNetStream>(allStreams);

            try {
                if (peImage != null)
                {
                    Debug.Assert(mdStream == null);
                    Debug.Assert(cor20Header != null);
                    var mdOffset = peImage.ToFileOffset(cor20Header.MetaData.VirtualAddress);
                    mdStream          = peImage.CreateStream(mdOffset, cor20Header.MetaData.Size);
                    disposeOfMdStream = true;
                }
                else
                {
                    Debug.Assert(mdStream != null);
                }
                for (int i = mdHeader.StreamHeaders.Count - 1; i >= 0; i--)
                {
                    var sh = mdHeader.StreamHeaders[i];
                    imageStream = mdStream.Create((FileOffset)sh.Offset, sh.StreamSize);
                    switch (sh.Name)
                    {
                    case "#Strings":
                        if (stringsStream == null)
                        {
                            stringsStream = new StringsStream(imageStream, sh);
                            imageStream   = null;
                            newAllStreams.Add(stringsStream);
                            continue;
                        }
                        break;

                    case "#US":
                        if (usStream == null)
                        {
                            usStream    = new USStream(imageStream, sh);
                            imageStream = null;
                            newAllStreams.Add(usStream);
                            continue;
                        }
                        break;

                    case "#Blob":
                        if (blobStream == null)
                        {
                            blobStream  = new BlobStream(imageStream, sh);
                            imageStream = null;
                            newAllStreams.Add(blobStream);
                            continue;
                        }
                        break;

                    case "#GUID":
                        if (guidStream == null)
                        {
                            guidStream  = new GuidStream(imageStream, sh);
                            imageStream = null;
                            newAllStreams.Add(guidStream);
                            continue;
                        }
                        break;

                    case "#~":
                        if (tablesStream == null)
                        {
                            tablesStream = new TablesStream(imageStream, sh);
                            imageStream  = null;
                            newAllStreams.Add(tablesStream);
                            continue;
                        }
                        break;

                    case "#!":
                        if (peImage == null)
                        {
                            break;
                        }
                        if (hotStreams == null)
                        {
                            hotStreams = new List <HotStream>();
                        }
                        fullStream = peImage.CreateFullStream();
                        hotStream  = HotStream.Create(hotHeapVersion, imageStream, sh, fullStream, mdStream.FileOffset + sh.Offset);
                        fullStream = null;
                        hotStreams.Add(hotStream);
                        newAllStreams.Add(hotStream);
                        hotStream   = null;
                        imageStream = null;
                        continue;

                    case "#Pdb":
                        if (isStandalonePortablePdb && pdbStream == null)
                        {
                            pdbStream   = new PdbStream(imageStream, sh);
                            imageStream = null;
                            allStreams.Add(pdbStream);
                            continue;
                        }
                        break;
                    }
                    dns         = new DotNetStream(imageStream, sh);
                    imageStream = null;
                    newAllStreams.Add(dns);
                    dns = null;
                }
            }
            finally {
                if (disposeOfMdStream)
                {
                    mdStream.Dispose();
                }
                if (imageStream != null)
                {
                    imageStream.Dispose();
                }
                if (fullStream != null)
                {
                    fullStream.Dispose();
                }
                if (dns != null)
                {
                    dns.Dispose();
                }
                if (hotStream != null)
                {
                    hotStream.Dispose();
                }
                newAllStreams.Reverse();
                allStreams = ThreadSafeListCreator.MakeThreadSafe(newAllStreams);
            }

            if (tablesStream == null)
            {
                throw new BadImageFormatException("Missing MD stream");
            }

            if (hotStreams != null)
            {
                hotStreams.Reverse();
                InitializeHotStreams(hotStreams);
            }

            if (pdbStream != null)
            {
                tablesStream.Initialize(pdbStream.TypeSystemTableRows);
            }
            else
            {
                tablesStream.Initialize(null);
            }
        }