| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.ComponentModel; |
| | | 5 | | using System.IO; |
| | | 6 | | using System.Reflection.PortableExecutable; |
| | | 7 | | using System.Text; |
| | | 8 | | |
| | | 9 | | namespace System.Reflection.Metadata |
| | | 10 | | { |
| | | 11 | | // EditorBrowsable(Never) so that we don't clutter completion list with this type because a user that only has Syste |
| | | 12 | | // imported and has type PE is likely looking to resolve PEReader from the System.Reflection.PortableExecutable and |
| | | 13 | | // these extensions as regular statics. |
| | | 14 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| | | 15 | | public static class PEReaderExtensions |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// Returns a body block of a method with specified Relative Virtual Address (RVA); |
| | | 19 | | /// </summary> |
| | | 20 | | /// <exception cref="ArgumentNullException"><paramref name="peReader"/> is null.</exception> |
| | | 21 | | /// <exception cref="BadImageFormatException">The body is not found in the metadata or is invalid.</exception> |
| | | 22 | | /// <exception cref="InvalidOperationException">Section where the method is stored is not available.</exception> |
| | | 23 | | /// <exception cref="IOException">IO error while reading from the underlying stream.</exception> |
| | | 24 | | public static MethodBodyBlock GetMethodBody(this PEReader peReader, int relativeVirtualAddress) |
| | 0 | 25 | | { |
| | 0 | 26 | | if (peReader is null) |
| | 0 | 27 | | { |
| | 0 | 28 | | Throw.ArgumentNull(nameof(peReader)); |
| | | 29 | | } |
| | | 30 | | |
| | 0 | 31 | | var block = peReader.GetSectionData(relativeVirtualAddress); |
| | 0 | 32 | | if (block.Length == 0) |
| | 0 | 33 | | { |
| | 0 | 34 | | throw new BadImageFormatException(SR.Format(SR.InvalidMethodRva, relativeVirtualAddress)); |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | // Call to validating public BlobReader constructor is by design -- we need to throw PlatformNotSupported on |
| | 0 | 38 | | return MethodBodyBlock.Create(block.GetReader()); |
| | 0 | 39 | | } |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// Gets a <see cref="MetadataReader"/> from a <see cref="PEReader"/>. |
| | | 43 | | /// </summary> |
| | | 44 | | /// <remarks> |
| | | 45 | | /// The caller must keep the <see cref="PEReader"/> alive and undisposed throughout the lifetime of the metadata |
| | | 46 | | /// </remarks> |
| | | 47 | | /// <exception cref="ArgumentNullException"><paramref name="peReader"/> is null</exception> |
| | | 48 | | /// <exception cref="PlatformNotSupportedException">The current platform is big-endian.</exception> |
| | | 49 | | /// <exception cref="IOException">IO error while reading from the underlying stream.</exception> |
| | | 50 | | public static MetadataReader GetMetadataReader(this PEReader peReader) |
| | 0 | 51 | | { |
| | 0 | 52 | | return GetMetadataReader(peReader, MetadataReaderOptions.ApplyWindowsRuntimeProjections, null); |
| | 0 | 53 | | } |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// Gets a <see cref="MetadataReader"/> from a <see cref="PEReader"/>. |
| | | 57 | | /// </summary> |
| | | 58 | | /// <remarks> |
| | | 59 | | /// The caller must keep the <see cref="PEReader"/> alive and undisposed throughout the lifetime of the metadata |
| | | 60 | | /// </remarks> |
| | | 61 | | /// <exception cref="ArgumentNullException"><paramref name="peReader"/> is null</exception> |
| | | 62 | | /// <exception cref="PlatformNotSupportedException">The current platform is big-endian.</exception> |
| | | 63 | | /// <exception cref="IOException">IO error while reading from the underlying stream.</exception> |
| | | 64 | | public static MetadataReader GetMetadataReader(this PEReader peReader, MetadataReaderOptions options) |
| | 0 | 65 | | { |
| | 0 | 66 | | return GetMetadataReader(peReader, options, null); |
| | 0 | 67 | | } |
| | | 68 | | |
| | | 69 | | /// <summary> |
| | | 70 | | /// Gets a <see cref="MetadataReader"/> from a <see cref="PEReader"/>. |
| | | 71 | | /// </summary> |
| | | 72 | | /// <remarks> |
| | | 73 | | /// The caller must keep the <see cref="PEReader"/> undisposed throughout the lifetime of the metadata reader. |
| | | 74 | | /// </remarks> |
| | | 75 | | /// <exception cref="ArgumentNullException"><paramref name="peReader"/> is null</exception> |
| | | 76 | | /// <exception cref="ArgumentException">The encoding of <paramref name="utf8Decoder"/> is not <see cref="UTF8Enc |
| | | 77 | | /// <exception cref="PlatformNotSupportedException">The current platform is big-endian.</exception> |
| | | 78 | | /// <exception cref="IOException">IO error while reading from the underlying stream.</exception> |
| | | 79 | | public static unsafe MetadataReader GetMetadataReader(this PEReader peReader, MetadataReaderOptions options, Met |
| | 0 | 80 | | { |
| | 0 | 81 | | if (peReader is null) |
| | 0 | 82 | | { |
| | 0 | 83 | | Throw.ArgumentNull(nameof(peReader)); |
| | | 84 | | } |
| | | 85 | | |
| | 0 | 86 | | var metadata = peReader.GetMetadata(); |
| | 0 | 87 | | return new MetadataReader(metadata.Pointer, metadata.Length, options, utf8Decoder, memoryOwner: peReader); |
| | 0 | 88 | | } |
| | | 89 | | } |
| | | 90 | | } |
| | | 91 | | |