| | | 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.Collections.Immutable; |
| | | 5 | | using System.Diagnostics; |
| | | 6 | | using System.Diagnostics.CodeAnalysis; |
| | | 7 | | using System.IO; |
| | | 8 | | using System.Reflection.Internal; |
| | | 9 | | using System.Reflection.Metadata; |
| | | 10 | | using System.Runtime.ExceptionServices; |
| | | 11 | | using System.Runtime.InteropServices; |
| | | 12 | | using System.Threading; |
| | | 13 | | using ImmutableArrayExtensions = System.Linq.ImmutableArrayExtensions; |
| | | 14 | | |
| | | 15 | | namespace System.Reflection.PortableExecutable |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// Portable Executable format reader. |
| | | 19 | | /// </summary> |
| | | 20 | | /// <remarks> |
| | | 21 | | /// The implementation is thread-safe, that is multiple threads can read data from the reader in parallel. |
| | | 22 | | /// Disposal of the reader is not thread-safe (see <see cref="Dispose"/>). |
| | | 23 | | /// </remarks> |
| | | 24 | | public sealed partial class PEReader : IDisposable |
| | | 25 | | { |
| | | 26 | | /// <summary> |
| | | 27 | | /// True if the PE image has been loaded into memory by the OS loader. |
| | | 28 | | /// </summary> |
| | 0 | 29 | | public bool IsLoadedImage { get; } |
| | | 30 | | |
| | | 31 | | // May be null in the event that the entire image is not |
| | | 32 | | // deemed necessary and we have been instructed to read |
| | | 33 | | // the image contents without being lazy. |
| | | 34 | | // |
| | | 35 | | // _lazyPEHeaders are not null in that case. |
| | | 36 | | private MemoryBlockProvider? _peImage; |
| | | 37 | | |
| | | 38 | | // If we read the data from the image lazily (peImage != null) we defer reading the PE headers. |
| | | 39 | | private PEHeaders? _lazyPEHeaders; |
| | | 40 | | |
| | | 41 | | private AbstractMemoryBlock? _lazyMetadataBlock; |
| | | 42 | | private AbstractMemoryBlock? _lazyImageBlock; |
| | | 43 | | private AbstractMemoryBlock?[]? _lazyPESectionBlocks; |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Creates a Portable Executable reader over a PE image stored in memory. |
| | | 47 | | /// </summary> |
| | | 48 | | /// <param name="peImage">Pointer to the start of the PE image.</param> |
| | | 49 | | /// <param name="size">The size of the PE image.</param> |
| | | 50 | | /// <exception cref="ArgumentNullException"><paramref name="peImage"/> is <see cref="IntPtr.Zero"/>.</exception> |
| | | 51 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="size"/> is negative.</exception> |
| | | 52 | | /// <remarks> |
| | | 53 | | /// The memory is owned by the caller and not released on disposal of the <see cref="PEReader"/>. |
| | | 54 | | /// The caller is responsible for keeping the memory alive and unmodified throughout the lifetime of the <see cr |
| | | 55 | | /// The content of the image is not read during the construction of the <see cref="PEReader"/> |
| | | 56 | | /// </remarks> |
| | | 57 | | public unsafe PEReader(byte* peImage, int size) |
| | 0 | 58 | | : this(peImage, size, isLoadedImage: false) |
| | 0 | 59 | | { |
| | 0 | 60 | | } |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// Creates a Portable Executable reader over a PE image stored in memory. |
| | | 64 | | /// </summary> |
| | | 65 | | /// <param name="peImage">Pointer to the start of the PE image.</param> |
| | | 66 | | /// <param name="size">The size of the PE image.</param> |
| | | 67 | | /// <param name="isLoadedImage">True if the PE image has been loaded into memory by the OS loader.</param> |
| | | 68 | | /// <exception cref="ArgumentNullException"><paramref name="peImage"/> is <see cref="IntPtr.Zero"/>.</exception> |
| | | 69 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="size"/> is negative.</exception> |
| | | 70 | | /// <remarks> |
| | | 71 | | /// The memory is owned by the caller and not released on disposal of the <see cref="PEReader"/>. |
| | | 72 | | /// The caller is responsible for keeping the memory alive and unmodified throughout the lifetime of the <see cr |
| | | 73 | | /// The content of the image is not read during the construction of the <see cref="PEReader"/> |
| | | 74 | | /// </remarks> |
| | 0 | 75 | | public unsafe PEReader(byte* peImage, int size, bool isLoadedImage) |
| | 0 | 76 | | { |
| | 0 | 77 | | if (peImage is null) |
| | 0 | 78 | | { |
| | 0 | 79 | | Throw.ArgumentNull(nameof(peImage)); |
| | | 80 | | } |
| | | 81 | | |
| | 0 | 82 | | if (size < 0) |
| | 0 | 83 | | { |
| | 0 | 84 | | throw new ArgumentOutOfRangeException(nameof(size)); |
| | | 85 | | } |
| | | 86 | | |
| | 0 | 87 | | _peImage = new ExternalMemoryBlockProvider(peImage, size); |
| | 0 | 88 | | IsLoadedImage = isLoadedImage; |
| | 0 | 89 | | } |
| | | 90 | | |
| | | 91 | | /// <summary> |
| | | 92 | | /// Creates a Portable Executable reader over a PE image stored in a stream. |
| | | 93 | | /// </summary> |
| | | 94 | | /// <param name="peStream">PE image stream.</param> |
| | | 95 | | /// <exception cref="ArgumentNullException"><paramref name="peStream"/> is null.</exception> |
| | | 96 | | /// <remarks> |
| | | 97 | | /// Ownership of the stream is transferred to the <see cref="PEReader"/> upon successful validation of construct |
| | | 98 | | /// disposed by the <see cref="PEReader"/> and the caller must not manipulate it. |
| | | 99 | | /// </remarks> |
| | | 100 | | public PEReader(Stream peStream) |
| | 0 | 101 | | : this(peStream, PEStreamOptions.Default) |
| | 0 | 102 | | { |
| | 0 | 103 | | } |
| | | 104 | | |
| | | 105 | | /// <summary> |
| | | 106 | | /// Creates a Portable Executable reader over a PE image stored in a stream beginning at its current position an |
| | | 107 | | /// </summary> |
| | | 108 | | /// <param name="peStream">PE image stream.</param> |
| | | 109 | | /// <param name="options"> |
| | | 110 | | /// Options specifying how sections of the PE image are read from the stream. |
| | | 111 | | /// |
| | | 112 | | /// Unless <see cref="PEStreamOptions.LeaveOpen"/> is specified, ownership of the stream is transferred to the < |
| | | 113 | | /// upon successful argument validation. It will be disposed by the <see cref="PEReader"/> and the caller must n |
| | | 114 | | /// |
| | | 115 | | /// Unless <see cref="PEStreamOptions.PrefetchMetadata"/> or <see cref="PEStreamOptions.PrefetchEntireImage"/> i |
| | | 116 | | /// is read from the stream during the construction of the <see cref="PEReader"/>. Furthermore, the stream must |
| | | 117 | | /// by caller while the <see cref="PEReader"/> is alive and undisposed. |
| | | 118 | | /// |
| | | 119 | | /// If <see cref="PEStreamOptions.PrefetchMetadata"/> or <see cref="PEStreamOptions.PrefetchEntireImage"/>, the |
| | | 120 | | /// will have read all of the data requested during construction. As such, if <see cref="PEStreamOptions.LeaveOp |
| | | 121 | | /// specified, the caller retains full ownership of the stream and is assured that it will not be manipulated by |
| | | 122 | | /// after construction. |
| | | 123 | | /// </param> |
| | | 124 | | /// <exception cref="ArgumentNullException"><paramref name="peStream"/> is null.</exception> |
| | | 125 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="options"/> has an invalid value.</exception> |
| | | 126 | | /// <exception cref="IOException">Error reading from the stream (only when prefetching data).</exception> |
| | | 127 | | /// <exception cref="BadImageFormatException"><see cref="PEStreamOptions.PrefetchMetadata"/> is specified and th |
| | | 128 | | public PEReader(Stream peStream, PEStreamOptions options) |
| | 0 | 129 | | : this(peStream, options, 0) |
| | 0 | 130 | | { |
| | 0 | 131 | | } |
| | | 132 | | |
| | | 133 | | /// <summary> |
| | | 134 | | /// Creates a Portable Executable reader over a PE image of the given size beginning at the stream's current pos |
| | | 135 | | /// </summary> |
| | | 136 | | /// <param name="peStream">PE image stream.</param> |
| | | 137 | | /// <param name="size">PE image size.</param> |
| | | 138 | | /// <param name="options"> |
| | | 139 | | /// Options specifying how sections of the PE image are read from the stream. |
| | | 140 | | /// |
| | | 141 | | /// Unless <see cref="PEStreamOptions.LeaveOpen"/> is specified, ownership of the stream is transferred to the < |
| | | 142 | | /// upon successful argument validation. It will be disposed by the <see cref="PEReader"/> and the caller must n |
| | | 143 | | /// |
| | | 144 | | /// Unless <see cref="PEStreamOptions.PrefetchMetadata"/> or <see cref="PEStreamOptions.PrefetchEntireImage"/> i |
| | | 145 | | /// is read from the stream during the construction of the <see cref="PEReader"/>. Furthermore, the stream must |
| | | 146 | | /// by caller while the <see cref="PEReader"/> is alive and undisposed. |
| | | 147 | | /// |
| | | 148 | | /// If <see cref="PEStreamOptions.PrefetchMetadata"/> or <see cref="PEStreamOptions.PrefetchEntireImage"/>, the |
| | | 149 | | /// will have read all of the data requested during construction. As such, if <see cref="PEStreamOptions.LeaveOp |
| | | 150 | | /// specified, the caller retains full ownership of the stream and is assured that it will not be manipulated by |
| | | 151 | | /// after construction. |
| | | 152 | | /// </param> |
| | | 153 | | /// <exception cref="ArgumentOutOfRangeException">Size is negative or extends past the end of the stream.</excep |
| | | 154 | | /// <exception cref="IOException">Error reading from the stream (only when prefetching data).</exception> |
| | | 155 | | /// <exception cref="BadImageFormatException"><see cref="PEStreamOptions.PrefetchMetadata"/> is specified and th |
| | 0 | 156 | | public unsafe PEReader(Stream peStream, PEStreamOptions options, int size) |
| | 0 | 157 | | { |
| | 0 | 158 | | if (peStream is null) |
| | 0 | 159 | | { |
| | 0 | 160 | | Throw.ArgumentNull(nameof(peStream)); |
| | | 161 | | } |
| | | 162 | | |
| | 0 | 163 | | if (!peStream.CanRead || !peStream.CanSeek) |
| | 0 | 164 | | { |
| | 0 | 165 | | throw new ArgumentException(SR.StreamMustSupportReadAndSeek, nameof(peStream)); |
| | | 166 | | } |
| | | 167 | | |
| | 0 | 168 | | if (!options.IsValid()) |
| | 0 | 169 | | { |
| | 0 | 170 | | throw new ArgumentOutOfRangeException(nameof(options)); |
| | | 171 | | } |
| | | 172 | | |
| | 0 | 173 | | IsLoadedImage = (options & PEStreamOptions.IsLoadedImage) != 0; |
| | | 174 | | |
| | 0 | 175 | | long start = peStream.Position; |
| | 0 | 176 | | int actualSize = StreamExtensions.GetAndValidateSize(peStream, size, nameof(peStream)); |
| | | 177 | | |
| | 0 | 178 | | bool closeStream = true; |
| | | 179 | | try |
| | 0 | 180 | | { |
| | 0 | 181 | | if ((options & (PEStreamOptions.PrefetchMetadata | PEStreamOptions.PrefetchEntireImage)) == 0) |
| | 0 | 182 | | { |
| | 0 | 183 | | _peImage = new StreamMemoryBlockProvider(peStream, start, actualSize, (options & PEStreamOptions.Lea |
| | 0 | 184 | | closeStream = false; |
| | 0 | 185 | | } |
| | | 186 | | else |
| | 0 | 187 | | { |
| | | 188 | | // Read in the entire image or metadata blob: |
| | 0 | 189 | | if ((options & PEStreamOptions.PrefetchEntireImage) != 0) |
| | 0 | 190 | | { |
| | 0 | 191 | | var imageBlock = StreamMemoryBlockProvider.ReadMemoryBlockNoLock(peStream, start, actualSize); |
| | 0 | 192 | | _lazyImageBlock = imageBlock; |
| | 0 | 193 | | _peImage = new ExternalMemoryBlockProvider(imageBlock.Pointer, imageBlock.Size); |
| | | 194 | | |
| | | 195 | | // if the caller asked for metadata initialize the PE headers (calculates metadata offset): |
| | 0 | 196 | | if ((options & PEStreamOptions.PrefetchMetadata) != 0) |
| | 0 | 197 | | { |
| | 0 | 198 | | _lazyPEHeaders = new PEHeaders(imageBlock.GetStream(), imageBlock.Size, IsLoadedImage); |
| | 0 | 199 | | } |
| | 0 | 200 | | } |
| | | 201 | | else |
| | 0 | 202 | | { |
| | | 203 | | // The peImage is left null, but the lazyMetadataBlock is initialized up front. |
| | 0 | 204 | | _lazyPEHeaders = new PEHeaders(peStream, actualSize, IsLoadedImage); |
| | | 205 | | |
| | 0 | 206 | | if (_lazyPEHeaders.MetadataStartOffset != -1) |
| | 0 | 207 | | { |
| | 0 | 208 | | _lazyMetadataBlock = StreamMemoryBlockProvider.ReadMemoryBlockNoLock(peStream, start + _lazy |
| | 0 | 209 | | } |
| | 0 | 210 | | } |
| | | 211 | | // We read all we need, the stream is going to be closed. |
| | 0 | 212 | | } |
| | 0 | 213 | | } |
| | | 214 | | finally |
| | 0 | 215 | | { |
| | 0 | 216 | | if (closeStream && (options & PEStreamOptions.LeaveOpen) == 0) |
| | 0 | 217 | | { |
| | 0 | 218 | | peStream.Dispose(); |
| | 0 | 219 | | } |
| | 0 | 220 | | } |
| | 0 | 221 | | } |
| | | 222 | | |
| | | 223 | | /// <summary> |
| | | 224 | | /// Creates a Portable Executable reader over a PE image stored in a byte array. |
| | | 225 | | /// </summary> |
| | | 226 | | /// <param name="peImage">PE image.</param> |
| | | 227 | | /// <remarks> |
| | | 228 | | /// The content of the image is not read during the construction of the <see cref="PEReader"/> |
| | | 229 | | /// </remarks> |
| | | 230 | | /// <exception cref="ArgumentNullException"><paramref name="peImage"/> is null.</exception> |
| | 0 | 231 | | public PEReader(ImmutableArray<byte> peImage) |
| | 0 | 232 | | { |
| | 0 | 233 | | if (peImage.IsDefault) |
| | 0 | 234 | | { |
| | 0 | 235 | | Throw.ArgumentNull(nameof(peImage)); |
| | | 236 | | } |
| | | 237 | | |
| | 0 | 238 | | _peImage = new ByteArrayMemoryProvider(peImage); |
| | 0 | 239 | | } |
| | | 240 | | |
| | | 241 | | /// <summary> |
| | | 242 | | /// Disposes all memory allocated by the reader. |
| | | 243 | | /// </summary> |
| | | 244 | | /// <remarks> |
| | | 245 | | /// <see cref="Dispose"/> can be called multiple times (but not in parallel). |
| | | 246 | | /// It is not safe to call <see cref="Dispose"/> in parallel with any other operation on the <see cref="PEReader |
| | | 247 | | /// or reading from <see cref="PEMemoryBlock"/>s retrieved from the reader. |
| | | 248 | | /// </remarks> |
| | | 249 | | public void Dispose() |
| | 0 | 250 | | { |
| | 0 | 251 | | _lazyPEHeaders = null; |
| | | 252 | | |
| | 0 | 253 | | _peImage?.Dispose(); |
| | 0 | 254 | | _peImage = null; |
| | | 255 | | |
| | 0 | 256 | | _lazyImageBlock?.Dispose(); |
| | 0 | 257 | | _lazyImageBlock = null; |
| | | 258 | | |
| | 0 | 259 | | _lazyMetadataBlock?.Dispose(); |
| | 0 | 260 | | _lazyMetadataBlock = null; |
| | | 261 | | |
| | 0 | 262 | | var peSectionBlocks = _lazyPESectionBlocks; |
| | 0 | 263 | | if (peSectionBlocks != null) |
| | 0 | 264 | | { |
| | 0 | 265 | | foreach (var block in peSectionBlocks) |
| | 0 | 266 | | { |
| | 0 | 267 | | block?.Dispose(); |
| | 0 | 268 | | } |
| | | 269 | | |
| | 0 | 270 | | _lazyPESectionBlocks = null; |
| | 0 | 271 | | } |
| | 0 | 272 | | } |
| | | 273 | | |
| | | 274 | | private MemoryBlockProvider GetPEImage() |
| | 0 | 275 | | { |
| | 0 | 276 | | var peImage = _peImage; |
| | 0 | 277 | | if (peImage == null) |
| | 0 | 278 | | { |
| | 0 | 279 | | if (_lazyPEHeaders == null) |
| | 0 | 280 | | { |
| | 0 | 281 | | Throw.PEReaderDisposed(); |
| | | 282 | | } |
| | | 283 | | |
| | 0 | 284 | | Throw.InvalidOperation_PEImageNotAvailable(); |
| | | 285 | | } |
| | | 286 | | |
| | 0 | 287 | | return peImage; |
| | 0 | 288 | | } |
| | | 289 | | |
| | | 290 | | /// <summary> |
| | | 291 | | /// Gets the PE headers. |
| | | 292 | | /// </summary> |
| | | 293 | | /// <exception cref="BadImageFormatException">The headers contain invalid data.</exception> |
| | | 294 | | /// <exception cref="IOException">Error reading from the stream.</exception> |
| | | 295 | | public PEHeaders PEHeaders |
| | | 296 | | { |
| | | 297 | | get |
| | 0 | 298 | | { |
| | 0 | 299 | | if (_lazyPEHeaders == null) |
| | 0 | 300 | | { |
| | 0 | 301 | | InitializePEHeaders(); |
| | 0 | 302 | | } |
| | | 303 | | |
| | 0 | 304 | | return _lazyPEHeaders; |
| | 0 | 305 | | } |
| | | 306 | | } |
| | | 307 | | |
| | | 308 | | /// <exception cref="IOException">Error reading from the stream.</exception> |
| | | 309 | | [MemberNotNull(nameof(_lazyPEHeaders))] |
| | | 310 | | private void InitializePEHeaders() |
| | 0 | 311 | | { |
| | 0 | 312 | | MemoryBlockProvider peImage = GetPEImage(); |
| | | 313 | | |
| | | 314 | | PEHeaders headers; |
| | | 315 | | // If the PE image is backed by a stream, use that to read the headers. |
| | 0 | 316 | | if (peImage.TryGetUnderlyingStream(out Stream? stream, out long imageStart, out int imageSize, out object? s |
| | 0 | 317 | | { |
| | 0 | 318 | | lock (streamGuard) |
| | 0 | 319 | | { |
| | 0 | 320 | | Debug.Assert(imageStart >= 0 && imageStart <= stream.Length); |
| | 0 | 321 | | stream.Seek(imageStart, SeekOrigin.Begin); |
| | 0 | 322 | | headers = new PEHeaders(stream, imageSize, IsLoadedImage); |
| | 0 | 323 | | } |
| | 0 | 324 | | } |
| | | 325 | | // Otherwise, get the memory block and wrap it in a stream. |
| | | 326 | | else |
| | 0 | 327 | | { |
| | | 328 | | // No need to acquire any lock here; GetStream() creates a new stream. |
| | 0 | 329 | | AbstractMemoryBlock memoryBlock = peImage.GetMemoryBlock(); |
| | 0 | 330 | | headers = new PEHeaders(memoryBlock.GetStream(), memoryBlock.Size, IsLoadedImage); |
| | 0 | 331 | | } |
| | | 332 | | |
| | 0 | 333 | | Interlocked.CompareExchange(ref _lazyPEHeaders, headers, null); |
| | 0 | 334 | | } |
| | | 335 | | |
| | | 336 | | /// <summary> |
| | | 337 | | /// Returns a view of the entire image as a pointer and length. |
| | | 338 | | /// </summary> |
| | | 339 | | /// <exception cref="InvalidOperationException">PE image not available.</exception> |
| | | 340 | | private AbstractMemoryBlock GetEntireImageBlock() |
| | 0 | 341 | | { |
| | 0 | 342 | | if (_lazyImageBlock == null) |
| | 0 | 343 | | { |
| | 0 | 344 | | var newBlock = GetPEImage().GetMemoryBlock(); |
| | 0 | 345 | | if (Interlocked.CompareExchange(ref _lazyImageBlock, newBlock, null) != null) |
| | 0 | 346 | | { |
| | | 347 | | // another thread created the block already, we need to dispose ours: |
| | 0 | 348 | | newBlock.Dispose(); |
| | 0 | 349 | | } |
| | 0 | 350 | | } |
| | | 351 | | |
| | 0 | 352 | | return _lazyImageBlock; |
| | 0 | 353 | | } |
| | | 354 | | |
| | | 355 | | /// <exception cref="IOException">IO error while reading from the underlying stream.</exception> |
| | | 356 | | /// <exception cref="InvalidOperationException">PE image doesn't have metadata.</exception> |
| | | 357 | | private AbstractMemoryBlock GetMetadataBlock() |
| | 0 | 358 | | { |
| | 0 | 359 | | if (!HasMetadata) |
| | 0 | 360 | | { |
| | 0 | 361 | | throw new InvalidOperationException(SR.PEImageDoesNotHaveMetadata); |
| | | 362 | | } |
| | | 363 | | |
| | 0 | 364 | | if (_lazyMetadataBlock == null) |
| | 0 | 365 | | { |
| | 0 | 366 | | var newBlock = GetPEImage().GetMemoryBlock(PEHeaders.MetadataStartOffset, PEHeaders.MetadataSize); |
| | 0 | 367 | | if (Interlocked.CompareExchange(ref _lazyMetadataBlock, newBlock, null) != null) |
| | 0 | 368 | | { |
| | | 369 | | // another thread created the block already, we need to dispose ours: |
| | 0 | 370 | | newBlock.Dispose(); |
| | 0 | 371 | | } |
| | 0 | 372 | | } |
| | | 373 | | |
| | 0 | 374 | | return _lazyMetadataBlock; |
| | 0 | 375 | | } |
| | | 376 | | |
| | | 377 | | /// <exception cref="IOException">IO error while reading from the underlying stream.</exception> |
| | | 378 | | /// <exception cref="InvalidOperationException">PE image not available.</exception> |
| | | 379 | | private AbstractMemoryBlock GetPESectionBlock(int index) |
| | 0 | 380 | | { |
| | 0 | 381 | | Debug.Assert(index >= 0 && index < PEHeaders.SectionHeaders.Length); |
| | | 382 | | |
| | 0 | 383 | | var peImage = GetPEImage(); |
| | | 384 | | |
| | 0 | 385 | | if (_lazyPESectionBlocks == null) |
| | 0 | 386 | | { |
| | 0 | 387 | | Interlocked.CompareExchange(ref _lazyPESectionBlocks, new AbstractMemoryBlock[PEHeaders.SectionHeaders.L |
| | 0 | 388 | | } |
| | | 389 | | |
| | 0 | 390 | | AbstractMemoryBlock? existingBlock = Volatile.Read(ref _lazyPESectionBlocks[index]); |
| | 0 | 391 | | if (existingBlock != null) |
| | 0 | 392 | | { |
| | 0 | 393 | | return existingBlock; |
| | | 394 | | } |
| | | 395 | | |
| | | 396 | | AbstractMemoryBlock newBlock; |
| | 0 | 397 | | if (IsLoadedImage) |
| | 0 | 398 | | { |
| | 0 | 399 | | newBlock = peImage.GetMemoryBlock( |
| | 0 | 400 | | PEHeaders.SectionHeaders[index].VirtualAddress, |
| | 0 | 401 | | PEHeaders.SectionHeaders[index].VirtualSize); |
| | 0 | 402 | | } |
| | | 403 | | else |
| | 0 | 404 | | { |
| | | 405 | | // Virtual size can be smaller than size in the image |
| | | 406 | | // since the size in the image is aligned. |
| | | 407 | | // Trim the alignment. |
| | | 408 | | // |
| | | 409 | | // Virtual size can also be larger than size in the image. |
| | | 410 | | // When loaded sizeInImage bytes are mapped from the image |
| | | 411 | | // and the rest of the bytes are zeroed out. |
| | | 412 | | // Only return data stored in the image. |
| | | 413 | | |
| | 0 | 414 | | int size = Math.Min( |
| | 0 | 415 | | PEHeaders.SectionHeaders[index].VirtualSize, |
| | 0 | 416 | | PEHeaders.SectionHeaders[index].SizeOfRawData); |
| | | 417 | | |
| | 0 | 418 | | newBlock = peImage.GetMemoryBlock(PEHeaders.SectionHeaders[index].PointerToRawData, size); |
| | 0 | 419 | | } |
| | | 420 | | |
| | 0 | 421 | | if (Interlocked.CompareExchange(ref _lazyPESectionBlocks[index], newBlock, null) != null) |
| | 0 | 422 | | { |
| | | 423 | | // another thread created the block already, we need to dispose ours: |
| | 0 | 424 | | newBlock.Dispose(); |
| | 0 | 425 | | } |
| | | 426 | | |
| | 0 | 427 | | return _lazyPESectionBlocks[index]!; |
| | 0 | 428 | | } |
| | | 429 | | |
| | | 430 | | /// <summary> |
| | | 431 | | /// Return true if the reader can access the entire PE image. |
| | | 432 | | /// </summary> |
| | | 433 | | /// <remarks> |
| | | 434 | | /// Returns false if the <see cref="PEReader"/> is constructed from a stream and only part of it is prefetched i |
| | | 435 | | /// </remarks> |
| | 0 | 436 | | public bool IsEntireImageAvailable => _lazyImageBlock != null || _peImage != null; |
| | | 437 | | |
| | | 438 | | /// <summary> |
| | | 439 | | /// Gets a pointer to and size of the PE image if available (<see cref="IsEntireImageAvailable"/>). |
| | | 440 | | /// </summary> |
| | | 441 | | /// <exception cref="InvalidOperationException">The entire PE image is not available.</exception> |
| | | 442 | | public PEMemoryBlock GetEntireImage() |
| | 0 | 443 | | { |
| | 0 | 444 | | return new PEMemoryBlock(GetEntireImageBlock()); |
| | 0 | 445 | | } |
| | | 446 | | |
| | | 447 | | /// <summary> |
| | | 448 | | /// Returns true if the PE image contains CLI metadata. |
| | | 449 | | /// </summary> |
| | | 450 | | /// <exception cref="BadImageFormatException">The PE headers contain invalid data.</exception> |
| | | 451 | | /// <exception cref="IOException">Error reading from the underlying stream.</exception> |
| | | 452 | | public bool HasMetadata |
| | | 453 | | { |
| | 0 | 454 | | get { return PEHeaders.MetadataSize > 0; } |
| | | 455 | | } |
| | | 456 | | |
| | | 457 | | /// <summary> |
| | | 458 | | /// Loads PE section that contains CLI metadata. |
| | | 459 | | /// </summary> |
| | | 460 | | /// <exception cref="InvalidOperationException">The PE image doesn't contain metadata (<see cref="HasMetadata"/> |
| | | 461 | | /// <exception cref="BadImageFormatException">The PE headers contain invalid data.</exception> |
| | | 462 | | /// <exception cref="IOException">IO error while reading from the underlying stream.</exception> |
| | | 463 | | public PEMemoryBlock GetMetadata() |
| | 0 | 464 | | { |
| | 0 | 465 | | return new PEMemoryBlock(GetMetadataBlock()); |
| | 0 | 466 | | } |
| | | 467 | | |
| | | 468 | | /// <summary> |
| | | 469 | | /// Loads PE section that contains the specified <paramref name="relativeVirtualAddress"/> into memory |
| | | 470 | | /// and returns a memory block that starts at <paramref name="relativeVirtualAddress"/> and ends at the end of t |
| | | 471 | | /// </summary> |
| | | 472 | | /// <param name="relativeVirtualAddress">Relative Virtual Address of the data to read.</param> |
| | | 473 | | /// <returns> |
| | | 474 | | /// An empty block if <paramref name="relativeVirtualAddress"/> doesn't represent a location in any of the PE se |
| | | 475 | | /// </returns> |
| | | 476 | | /// <exception cref="BadImageFormatException">The PE headers contain invalid data.</exception> |
| | | 477 | | /// <exception cref="IOException">IO error while reading from the underlying stream.</exception> |
| | | 478 | | /// <exception cref="InvalidOperationException">PE image not available.</exception> |
| | | 479 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="relativeVirtualAddress"/> is negative.</except |
| | | 480 | | public PEMemoryBlock GetSectionData(int relativeVirtualAddress) |
| | 0 | 481 | | { |
| | 0 | 482 | | if (relativeVirtualAddress < 0) |
| | 0 | 483 | | { |
| | 0 | 484 | | Throw.ArgumentOutOfRange(nameof(relativeVirtualAddress)); |
| | | 485 | | } |
| | | 486 | | |
| | 0 | 487 | | int sectionIndex = PEHeaders.GetContainingSectionIndex(relativeVirtualAddress); |
| | 0 | 488 | | if (sectionIndex < 0) |
| | 0 | 489 | | { |
| | 0 | 490 | | return default(PEMemoryBlock); |
| | | 491 | | } |
| | | 492 | | |
| | 0 | 493 | | var block = GetPESectionBlock(sectionIndex); |
| | | 494 | | |
| | 0 | 495 | | int relativeOffset = relativeVirtualAddress - PEHeaders.SectionHeaders[sectionIndex].VirtualAddress; |
| | 0 | 496 | | if (relativeOffset > block.Size) |
| | 0 | 497 | | { |
| | 0 | 498 | | return default(PEMemoryBlock); |
| | | 499 | | } |
| | | 500 | | |
| | 0 | 501 | | return new PEMemoryBlock(block, relativeOffset); |
| | 0 | 502 | | } |
| | | 503 | | |
| | | 504 | | /// <summary> |
| | | 505 | | /// Loads PE section of the specified name into memory and returns a memory block that spans the section. |
| | | 506 | | /// </summary> |
| | | 507 | | /// <param name="sectionName">Name of the section.</param> |
| | | 508 | | /// <returns> |
| | | 509 | | /// An empty block if no section of the given <paramref name="sectionName"/> exists in this PE image. |
| | | 510 | | /// </returns> |
| | | 511 | | /// <exception cref="ArgumentNullException"><paramref name="sectionName"/> is null.</exception> |
| | | 512 | | /// <exception cref="InvalidOperationException">PE image not available.</exception> |
| | | 513 | | public PEMemoryBlock GetSectionData(string sectionName) |
| | 0 | 514 | | { |
| | 0 | 515 | | if (sectionName is null) |
| | 0 | 516 | | { |
| | 0 | 517 | | Throw.ArgumentNull(nameof(sectionName)); |
| | | 518 | | } |
| | | 519 | | |
| | 0 | 520 | | int sectionIndex = PEHeaders.IndexOfSection(sectionName); |
| | 0 | 521 | | if (sectionIndex < 0) |
| | 0 | 522 | | { |
| | 0 | 523 | | return default(PEMemoryBlock); |
| | | 524 | | } |
| | | 525 | | |
| | 0 | 526 | | return new PEMemoryBlock(GetPESectionBlock(sectionIndex)); |
| | 0 | 527 | | } |
| | | 528 | | |
| | | 529 | | /// <summary> |
| | | 530 | | /// Reads all Debug Directory table entries. |
| | | 531 | | /// </summary> |
| | | 532 | | /// <exception cref="BadImageFormatException">Bad format of the entry.</exception> |
| | | 533 | | /// <exception cref="IOException">IO error while reading from the underlying stream.</exception> |
| | | 534 | | /// <exception cref="InvalidOperationException">PE image not available.</exception> |
| | | 535 | | public ImmutableArray<DebugDirectoryEntry> ReadDebugDirectory() |
| | 0 | 536 | | { |
| | 0 | 537 | | Debug.Assert(PEHeaders.PEHeader != null); |
| | | 538 | | |
| | 0 | 539 | | var debugDirectory = PEHeaders.PEHeader.DebugTableDirectory; |
| | 0 | 540 | | if (debugDirectory.Size == 0) |
| | 0 | 541 | | { |
| | 0 | 542 | | return ImmutableArray<DebugDirectoryEntry>.Empty; |
| | | 543 | | } |
| | | 544 | | |
| | | 545 | | int position; |
| | 0 | 546 | | if (!PEHeaders.TryGetDirectoryOffset(debugDirectory, out position)) |
| | 0 | 547 | | { |
| | 0 | 548 | | throw new BadImageFormatException(SR.InvalidDirectoryRVA); |
| | | 549 | | } |
| | | 550 | | |
| | 0 | 551 | | if (debugDirectory.Size % DebugDirectoryEntry.Size != 0) |
| | 0 | 552 | | { |
| | 0 | 553 | | throw new BadImageFormatException(SR.InvalidDirectorySize); |
| | | 554 | | } |
| | | 555 | | |
| | 0 | 556 | | using (AbstractMemoryBlock block = GetPEImage().GetMemoryBlock(position, debugDirectory.Size)) |
| | 0 | 557 | | { |
| | 0 | 558 | | return ReadDebugDirectoryEntries(block.GetReader()); |
| | | 559 | | } |
| | 0 | 560 | | } |
| | | 561 | | |
| | | 562 | | internal static ImmutableArray<DebugDirectoryEntry> ReadDebugDirectoryEntries(BlobReader reader) |
| | 0 | 563 | | { |
| | 0 | 564 | | int entryCount = reader.Length / DebugDirectoryEntry.Size; |
| | 0 | 565 | | var builder = ImmutableArray.CreateBuilder<DebugDirectoryEntry>(entryCount); |
| | 0 | 566 | | for (int i = 0; i < entryCount; i++) |
| | 0 | 567 | | { |
| | | 568 | | // Reserved, must be zero. |
| | 0 | 569 | | int characteristics = reader.ReadInt32(); |
| | 0 | 570 | | if (characteristics != 0) |
| | 0 | 571 | | { |
| | 0 | 572 | | throw new BadImageFormatException(SR.InvalidDebugDirectoryEntryCharacteristics); |
| | | 573 | | } |
| | | 574 | | |
| | 0 | 575 | | uint stamp = reader.ReadUInt32(); |
| | 0 | 576 | | ushort majorVersion = reader.ReadUInt16(); |
| | 0 | 577 | | ushort minorVersion = reader.ReadUInt16(); |
| | | 578 | | |
| | 0 | 579 | | var type = (DebugDirectoryEntryType)reader.ReadInt32(); |
| | | 580 | | |
| | 0 | 581 | | int dataSize = reader.ReadInt32(); |
| | 0 | 582 | | int dataRva = reader.ReadInt32(); |
| | 0 | 583 | | int dataPointer = reader.ReadInt32(); |
| | | 584 | | |
| | 0 | 585 | | builder.Add(new DebugDirectoryEntry(stamp, majorVersion, minorVersion, type, dataSize, dataRva, dataPoin |
| | 0 | 586 | | } |
| | | 587 | | |
| | 0 | 588 | | return builder.MoveToImmutable(); |
| | 0 | 589 | | } |
| | | 590 | | |
| | | 591 | | private AbstractMemoryBlock GetDebugDirectoryEntryDataBlock(DebugDirectoryEntry entry) |
| | 0 | 592 | | { |
| | 0 | 593 | | int dataOffset = IsLoadedImage ? entry.DataRelativeVirtualAddress : entry.DataPointer; |
| | 0 | 594 | | return GetPEImage().GetMemoryBlock(dataOffset, entry.DataSize); |
| | 0 | 595 | | } |
| | | 596 | | |
| | | 597 | | /// <summary> |
| | | 598 | | /// Reads the data pointed to by the specified Debug Directory entry and interprets them as CodeView. |
| | | 599 | | /// </summary> |
| | | 600 | | /// <exception cref="ArgumentException"><paramref name="entry"/> is not a CodeView entry.</exception> |
| | | 601 | | /// <exception cref="BadImageFormatException">Bad format of the data.</exception> |
| | | 602 | | /// <exception cref="IOException">IO error while reading from the underlying stream.</exception> |
| | | 603 | | /// <exception cref="InvalidOperationException">PE image not available.</exception> |
| | | 604 | | public CodeViewDebugDirectoryData ReadCodeViewDebugDirectoryData(DebugDirectoryEntry entry) |
| | 0 | 605 | | { |
| | 0 | 606 | | if (entry.Type != DebugDirectoryEntryType.CodeView) |
| | 0 | 607 | | { |
| | 0 | 608 | | Throw.InvalidArgument(SR.Format(SR.UnexpectedDebugDirectoryType, nameof(DebugDirectoryEntryType.CodeView |
| | | 609 | | } |
| | | 610 | | |
| | 0 | 611 | | using (var block = GetDebugDirectoryEntryDataBlock(entry)) |
| | 0 | 612 | | { |
| | 0 | 613 | | return DecodeCodeViewDebugDirectoryData(block); |
| | | 614 | | } |
| | 0 | 615 | | } |
| | | 616 | | |
| | | 617 | | // internal for testing |
| | | 618 | | internal static CodeViewDebugDirectoryData DecodeCodeViewDebugDirectoryData(AbstractMemoryBlock block) |
| | 0 | 619 | | { |
| | 0 | 620 | | var reader = block.GetReader(); |
| | | 621 | | |
| | 0 | 622 | | if (reader.ReadByte() != (byte)'R' || |
| | 0 | 623 | | reader.ReadByte() != (byte)'S' || |
| | 0 | 624 | | reader.ReadByte() != (byte)'D' || |
| | 0 | 625 | | reader.ReadByte() != (byte)'S') |
| | 0 | 626 | | { |
| | 0 | 627 | | throw new BadImageFormatException(SR.UnexpectedCodeViewDataSignature); |
| | | 628 | | } |
| | | 629 | | |
| | 0 | 630 | | Guid guid = reader.ReadGuid(); |
| | 0 | 631 | | int age = reader.ReadInt32(); |
| | 0 | 632 | | string path = reader.ReadUtf8NullTerminated(); |
| | | 633 | | |
| | 0 | 634 | | return new CodeViewDebugDirectoryData(guid, age, path); |
| | 0 | 635 | | } |
| | | 636 | | |
| | | 637 | | /// <summary> |
| | | 638 | | /// Reads the data pointed to by the specified Debug Directory entry and interprets them as PDB Checksum entry. |
| | | 639 | | /// </summary> |
| | | 640 | | /// <exception cref="ArgumentException"><paramref name="entry"/> is not a PDB Checksum entry.</exception> |
| | | 641 | | /// <exception cref="BadImageFormatException">Bad format of the data.</exception> |
| | | 642 | | /// <exception cref="IOException">IO error while reading from the underlying stream.</exception> |
| | | 643 | | /// <exception cref="InvalidOperationException">PE image not available.</exception> |
| | | 644 | | public PdbChecksumDebugDirectoryData ReadPdbChecksumDebugDirectoryData(DebugDirectoryEntry entry) |
| | 0 | 645 | | { |
| | 0 | 646 | | if (entry.Type != DebugDirectoryEntryType.PdbChecksum) |
| | 0 | 647 | | { |
| | 0 | 648 | | Throw.InvalidArgument(SR.Format(SR.UnexpectedDebugDirectoryType, nameof(DebugDirectoryEntryType.PdbCheck |
| | | 649 | | } |
| | | 650 | | |
| | 0 | 651 | | using (var block = GetDebugDirectoryEntryDataBlock(entry)) |
| | 0 | 652 | | { |
| | 0 | 653 | | return DecodePdbChecksumDebugDirectoryData(block); |
| | | 654 | | } |
| | 0 | 655 | | } |
| | | 656 | | |
| | | 657 | | // internal for testing |
| | | 658 | | internal static PdbChecksumDebugDirectoryData DecodePdbChecksumDebugDirectoryData(AbstractMemoryBlock block) |
| | 0 | 659 | | { |
| | 0 | 660 | | var reader = block.GetReader(); |
| | | 661 | | |
| | 0 | 662 | | var algorithmName = reader.ReadUtf8NullTerminated(); |
| | 0 | 663 | | byte[]? checksum = reader.ReadBytes(reader.RemainingBytes); |
| | 0 | 664 | | if (algorithmName.Length == 0 || checksum.Length == 0) |
| | 0 | 665 | | { |
| | 0 | 666 | | throw new BadImageFormatException(SR.InvalidPdbChecksumDataFormat); |
| | | 667 | | } |
| | | 668 | | |
| | 0 | 669 | | return new PdbChecksumDebugDirectoryData( |
| | 0 | 670 | | algorithmName, |
| | 0 | 671 | | ImmutableCollectionsMarshal.AsImmutableArray(checksum)); |
| | 0 | 672 | | } |
| | | 673 | | |
| | | 674 | | /// <summary> |
| | | 675 | | /// Opens a Portable PDB associated with this PE image. |
| | | 676 | | /// </summary> |
| | | 677 | | /// <param name="peImagePath"> |
| | | 678 | | /// The path to the PE image. The path is used to locate the PDB file located in the directory containing the PE |
| | | 679 | | /// </param> |
| | | 680 | | /// <param name="pdbFileStreamProvider"> |
| | | 681 | | /// If specified, called to open a <see cref="Stream"/> for a given file path. |
| | | 682 | | /// The provider is expected to either return a readable and seekable <see cref="Stream"/>, |
| | | 683 | | /// or <c>null</c> if the target file doesn't exist or should be ignored for some reason. |
| | | 684 | | /// |
| | | 685 | | /// The provider shall throw <see cref="IOException"/> if it fails to open the file due to an unexpected IO erro |
| | | 686 | | /// </param> |
| | | 687 | | /// <param name="pdbReaderProvider"> |
| | | 688 | | /// If successful, a new instance of <see cref="MetadataReaderProvider"/> to be used to read the Portable PDB,. |
| | | 689 | | /// </param> |
| | | 690 | | /// <param name="pdbPath"> |
| | | 691 | | /// If successful and the PDB is found in a file, the path to the file. Returns <c>null</c> if the PDB is embedd |
| | | 692 | | /// </param> |
| | | 693 | | /// <returns> |
| | | 694 | | /// True if the PE image has a PDB associated with it and the PDB has been successfully opened. |
| | | 695 | | /// </returns> |
| | | 696 | | /// <remarks> |
| | | 697 | | /// Implements a simple PDB file lookup based on the content of the PE image Debug Directory. |
| | | 698 | | /// A sophisticated tool might need to follow up with additional lookup on search paths or symbol server. |
| | | 699 | | /// |
| | | 700 | | /// The method looks the PDB up in the following steps in the listed order: |
| | | 701 | | /// 1) Check for a matching PDB file of the name found in the CodeView entry in the directory containing the PE |
| | | 702 | | /// 2) Check for a PDB embedded in the PE image itself. |
| | | 703 | | /// |
| | | 704 | | /// The first PDB that matches the information specified in the Debug Directory is returned. |
| | | 705 | | /// </remarks> |
| | | 706 | | /// <exception cref="ArgumentNullException"><paramref name="peImagePath"/> or <paramref name="pdbFileStreamProvi |
| | | 707 | | /// <exception cref="InvalidOperationException">The stream returned from <paramref name="pdbFileStreamProvider"/ |
| | | 708 | | /// <exception cref="BadImageFormatException">No matching PDB file is found due to an error: The PE image or the |
| | | 709 | | /// <exception cref="IOException">No matching PDB file is found due to an error: An IO error occurred while read |
| | | 710 | | public bool TryOpenAssociatedPortablePdb(string peImagePath, Func<string, Stream?> pdbFileStreamProvider, out Me |
| | 0 | 711 | | { |
| | 0 | 712 | | if (peImagePath is null) |
| | 0 | 713 | | { |
| | 0 | 714 | | Throw.ArgumentNull(nameof(peImagePath)); |
| | | 715 | | } |
| | 0 | 716 | | if (pdbFileStreamProvider is null) |
| | 0 | 717 | | { |
| | 0 | 718 | | Throw.ArgumentNull(nameof(pdbFileStreamProvider)); |
| | | 719 | | } |
| | | 720 | | |
| | 0 | 721 | | pdbReaderProvider = null; |
| | 0 | 722 | | pdbPath = null; |
| | | 723 | | |
| | | 724 | | string? peImageDirectory; |
| | | 725 | | try |
| | 0 | 726 | | { |
| | 0 | 727 | | peImageDirectory = Path.GetDirectoryName(peImagePath); |
| | 0 | 728 | | } |
| | 0 | 729 | | catch (Exception e) |
| | 0 | 730 | | { |
| | 0 | 731 | | throw new ArgumentException(e.Message, nameof(peImagePath), e); |
| | | 732 | | } |
| | | 733 | | |
| | 0 | 734 | | Exception? errorToReport = null; |
| | 0 | 735 | | var entries = ReadDebugDirectory(); |
| | | 736 | | |
| | | 737 | | // First try .pdb file specified in CodeView data (we prefer .pdb file on disk over embedded PDB |
| | | 738 | | // since embedded PDB needs decompression which is less efficient than memory-mapping the file). |
| | 0 | 739 | | var codeViewEntry = ImmutableArrayExtensions.FirstOrDefault(entries, e => e.IsPortableCodeView); |
| | 0 | 740 | | if (codeViewEntry.DataSize != 0 && |
| | 0 | 741 | | TryOpenCodeViewPortablePdb(codeViewEntry, peImageDirectory!, pdbFileStreamProvider, out pdbReaderProvide |
| | 0 | 742 | | { |
| | 0 | 743 | | return true; |
| | | 744 | | } |
| | | 745 | | |
| | | 746 | | // if it failed try Embedded Portable PDB (if available): |
| | 0 | 747 | | var embeddedPdbEntry = ImmutableArrayExtensions.FirstOrDefault(entries, e => e.Type == DebugDirectoryEntryTy |
| | 0 | 748 | | if (embeddedPdbEntry.DataSize != 0) |
| | 0 | 749 | | { |
| | 0 | 750 | | bool openedEmbeddedPdb = false; |
| | 0 | 751 | | pdbReaderProvider = null; |
| | 0 | 752 | | TryOpenEmbeddedPortablePdb(embeddedPdbEntry, ref openedEmbeddedPdb, ref pdbReaderProvider, ref errorToRe |
| | 0 | 753 | | if (openedEmbeddedPdb) |
| | 0 | 754 | | return true; |
| | 0 | 755 | | } |
| | | 756 | | |
| | | 757 | | // Report any metadata and IO errors. PDB might exist but we couldn't read some metadata. |
| | | 758 | | // The caller might chose to ignore the failure or report it to the user. |
| | 0 | 759 | | if (errorToReport != null) |
| | 0 | 760 | | { |
| | 0 | 761 | | Debug.Assert(errorToReport is BadImageFormatException || errorToReport is IOException); |
| | 0 | 762 | | ExceptionDispatchInfo.Capture(errorToReport).Throw(); |
| | | 763 | | } |
| | | 764 | | |
| | 0 | 765 | | return false; |
| | 0 | 766 | | } |
| | | 767 | | |
| | | 768 | | private bool TryOpenCodeViewPortablePdb(DebugDirectoryEntry codeViewEntry, string peImageDirectory, Func<string, |
| | 0 | 769 | | { |
| | 0 | 770 | | pdbPath = null; |
| | 0 | 771 | | provider = null; |
| | | 772 | | |
| | | 773 | | CodeViewDebugDirectoryData data; |
| | | 774 | | |
| | | 775 | | try |
| | 0 | 776 | | { |
| | 0 | 777 | | data = ReadCodeViewDebugDirectoryData(codeViewEntry); |
| | 0 | 778 | | } |
| | 0 | 779 | | catch (Exception e) when (e is BadImageFormatException || e is IOException) |
| | 0 | 780 | | { |
| | 0 | 781 | | errorToReport ??= e; |
| | 0 | 782 | | return false; |
| | | 783 | | } |
| | | 784 | | |
| | 0 | 785 | | var id = new BlobContentId(data.Guid, codeViewEntry.Stamp); |
| | | 786 | | |
| | | 787 | | // The interpretation os the path in the CodeView needs to be platform agnostic, |
| | | 788 | | // so that PDBs built on Windows work on Unix-like systems and vice versa. |
| | | 789 | | // System.IO.Path.GetFileName() on Unix-like systems doesn't treat '\' as a file name separator, |
| | | 790 | | // so we need a custom implementation. Also avoid throwing an exception if the path contains invalid charact |
| | | 791 | | // they might not be invalid on the other platform. It's up to the FS APIs to deal with that when opening th |
| | 0 | 792 | | string collocatedPdbPath = PathUtilities.CombinePathWithRelativePath(peImageDirectory, PathUtilities.GetFile |
| | | 793 | | |
| | 0 | 794 | | if (TryOpenPortablePdbFile(collocatedPdbPath, id, pdbFileStreamProvider, out provider, ref errorToReport)) |
| | 0 | 795 | | { |
| | 0 | 796 | | pdbPath = collocatedPdbPath; |
| | 0 | 797 | | return true; |
| | | 798 | | } |
| | | 799 | | |
| | 0 | 800 | | return false; |
| | 0 | 801 | | } |
| | | 802 | | |
| | | 803 | | private static bool TryOpenPortablePdbFile(string path, BlobContentId id, Func<string, Stream?> pdbFileStreamPro |
| | 0 | 804 | | { |
| | 0 | 805 | | provider = null; |
| | 0 | 806 | | MetadataReaderProvider? candidate = null; |
| | | 807 | | |
| | | 808 | | try |
| | 0 | 809 | | { |
| | | 810 | | Stream? pdbStream; |
| | | 811 | | |
| | | 812 | | try |
| | 0 | 813 | | { |
| | 0 | 814 | | pdbStream = pdbFileStreamProvider(path); |
| | 0 | 815 | | } |
| | 0 | 816 | | catch (FileNotFoundException) |
| | 0 | 817 | | { |
| | | 818 | | // Not an unexpected IO exception, continue witout reporting the error. |
| | 0 | 819 | | pdbStream = null; |
| | 0 | 820 | | } |
| | | 821 | | |
| | 0 | 822 | | if (pdbStream == null) |
| | 0 | 823 | | { |
| | 0 | 824 | | return false; |
| | | 825 | | } |
| | | 826 | | |
| | 0 | 827 | | if (!pdbStream.CanRead || !pdbStream.CanSeek) |
| | 0 | 828 | | { |
| | 0 | 829 | | throw new InvalidOperationException(SR.StreamMustSupportReadAndSeek); |
| | | 830 | | } |
| | | 831 | | |
| | 0 | 832 | | candidate = MetadataReaderProvider.FromPortablePdbStream(pdbStream); |
| | | 833 | | |
| | | 834 | | // Validate that the PDB matches the assembly version |
| | 0 | 835 | | if (new BlobContentId(candidate.GetMetadataReader().DebugMetadataHeader!.Id) != id) |
| | 0 | 836 | | { |
| | 0 | 837 | | return false; |
| | | 838 | | } |
| | | 839 | | |
| | 0 | 840 | | provider = candidate; |
| | 0 | 841 | | return true; |
| | | 842 | | } |
| | 0 | 843 | | catch (Exception e) when (e is BadImageFormatException || e is IOException) |
| | 0 | 844 | | { |
| | 0 | 845 | | errorToReport ??= e; |
| | 0 | 846 | | return false; |
| | | 847 | | } |
| | | 848 | | finally |
| | 0 | 849 | | { |
| | 0 | 850 | | if (provider == null) |
| | 0 | 851 | | { |
| | 0 | 852 | | candidate?.Dispose(); |
| | 0 | 853 | | } |
| | 0 | 854 | | } |
| | 0 | 855 | | } |
| | | 856 | | |
| | | 857 | | partial void TryOpenEmbeddedPortablePdb(DebugDirectoryEntry embeddedPdbEntry, ref bool openedEmbeddedPdb, ref Me |
| | | 858 | | } |
| | | 859 | | } |
| | | 860 | | |