Пример #1
0
        /// <summary>
        /// Reads all Debug Directory table entries.
        /// </summary>
        /// <exception cref="BadImageFormatException">Bad format of the entry.</exception>
        /// <exception cref="IOException">IO error while reading from the underlying stream.</exception>
        /// <exception cref="InvalidOperationException">PE image not available.</exception>
        public ImmutableArray <DebugDirectoryEntry> ReadDebugDirectory()
        {
            Debug.Assert(PEHeaders.PEHeader != null);

            var debugDirectory = PEHeaders.PEHeader.DebugTableDirectory;

            if (debugDirectory.Size == 0)
            {
                return(ImmutableArray <DebugDirectoryEntry> .Empty);
            }

            int position;

            if (!PEHeaders.TryGetDirectoryOffset(debugDirectory, out position))
            {
                throw new BadImageFormatException(SR.InvalidDirectoryRVA);
            }

            if (debugDirectory.Size % DebugDirectoryEntry.Size != 0)
            {
                throw new BadImageFormatException(SR.InvalidDirectorySize);
            }

            using (AbstractMemoryBlock block = GetPEImage().GetMemoryBlock(position, debugDirectory.Size))
            {
                return(ReadDebugDirectoryEntries(block.GetReader()));
            }
        }
Пример #2
0
        /// <summary>
        /// Reads all Debug Directory table entries.
        /// </summary>
        /// <exception cref="BadImageFormatException">Bad format of the entry.</exception>
        /// <exception cref="IOException">IO error while reading from the underlying stream.</exception>
        public unsafe ImmutableArray <DebugDirectoryEntry> ReadDebugDirectory()
        {
            var debugDirectory = PEHeaders.PEHeader.DebugTableDirectory;

            if (debugDirectory.Size == 0)
            {
                return(ImmutableArray <DebugDirectoryEntry> .Empty);
            }

            int position;

            if (!PEHeaders.TryGetDirectoryOffset(debugDirectory, out position))
            {
                throw new BadImageFormatException(SR.InvalidDirectoryRVA);
            }

            if (debugDirectory.Size % DebugDirectoryEntry.Size != 0)
            {
                throw new BadImageFormatException(SR.InvalidDirectorySize);
            }

            using (AbstractMemoryBlock block = _peImage.GetMemoryBlock(position, debugDirectory.Size))
            {
                var reader = new BlobReader(block.Pointer, block.Size);
                return(ReadDebugDirectoryEntries(reader));
            }
        }
Пример #3
0
        /// <summary>
        /// Reads all Debug Directory table entries.
        /// </summary>
        /// <exception cref="BadImageFormatException">Bad format of the entry.</exception>
        public unsafe ImmutableArray <DebugDirectoryEntry> ReadDebugDirectory()
        {
            var debugDirectory = PEHeaders.PEHeader.DebugTableDirectory;

            if (debugDirectory.Size == 0)
            {
                return(ImmutableArray <DebugDirectoryEntry> .Empty);
            }

            int position;

            if (!PEHeaders.TryGetDirectoryOffset(debugDirectory, out position))
            {
                throw new BadImageFormatException(SR.InvalidDirectoryRVA);
            }

            const int entrySize = 0x1c;

            if (debugDirectory.Size % entrySize != 0)
            {
                throw new BadImageFormatException(SR.InvalidDirectorySize);
            }

            using (AbstractMemoryBlock block = _peImage.GetMemoryBlock(position, debugDirectory.Size))
            {
                var reader = new BlobReader(block.Pointer, block.Size);

                int entryCount = debugDirectory.Size / entrySize;
                var builder    = ImmutableArray.CreateBuilder <DebugDirectoryEntry>(entryCount);
                for (int i = 0; i < entryCount; i++)
                {
                    // Reserved, must be zero.
                    int characteristics = reader.ReadInt32();
                    if (characteristics != 0)
                    {
                        throw new BadImageFormatException(SR.InvalidDebugDirectoryEntryCharacteristics);
                    }

                    uint   stamp        = reader.ReadUInt32();
                    ushort majorVersion = reader.ReadUInt16();
                    ushort minorVersion = reader.ReadUInt16();

                    var type = (DebugDirectoryEntryType)reader.ReadInt32();

                    int dataSize    = reader.ReadInt32();
                    int dataRva     = reader.ReadInt32();
                    int dataPointer = reader.ReadInt32();

                    builder.Add(new DebugDirectoryEntry(stamp, majorVersion, minorVersion, type, dataSize, dataRva, dataPointer));
                }

                return(builder.MoveToImmutable());
            }
        }