From 798a4edc1eaeeb7f65358893458ad0f2067a6c11 Mon Sep 17 00:00:00 2001 From: Washi Date: Sat, 9 Nov 2024 23:22:28 +0100 Subject: [PATCH] BUGFIX: Add EmptyDebugDataSegment --- .../Debug/Builder/DebugDirectoryBuffer.cs | 2 +- .../Debug/EmptyDebugDataSegment.cs | 43 +++++++++++++++++++ .../Debug/SerializedDebugDataEntry.cs | 3 ++ 3 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 src/AsmResolver.PE/Debug/EmptyDebugDataSegment.cs diff --git a/src/AsmResolver.PE/Debug/Builder/DebugDirectoryBuffer.cs b/src/AsmResolver.PE/Debug/Builder/DebugDirectoryBuffer.cs index 2cc85880d..c3a0dc300 100644 --- a/src/AsmResolver.PE/Debug/Builder/DebugDirectoryBuffer.cs +++ b/src/AsmResolver.PE/Debug/Builder/DebugDirectoryBuffer.cs @@ -27,7 +27,7 @@ public class DebugDirectoryBuffer : ISegment public void AddEntry(DebugDataEntry entry) { _headers.Add(entry); - if (entry.Contents != null) + if (entry.Contents is not null and not EmptyDebugDataSegment) _streamsTable.Add(entry.Contents, 4); } diff --git a/src/AsmResolver.PE/Debug/EmptyDebugDataSegment.cs b/src/AsmResolver.PE/Debug/EmptyDebugDataSegment.cs new file mode 100644 index 000000000..402079a8e --- /dev/null +++ b/src/AsmResolver.PE/Debug/EmptyDebugDataSegment.cs @@ -0,0 +1,43 @@ +using System.Diagnostics; +using AsmResolver.IO; + +namespace AsmResolver.PE.Debug; + +/// +/// Represents the contents of an empty debug data entry. +/// +public sealed class EmptyDebugDataSegment : IDebugDataSegment +{ + /// + /// Creates a new empty debug data segment for the provided debug data content. + /// + /// The content type. + public EmptyDebugDataSegment(DebugDataType type) + { + Type = type; + } + + /// + public DebugDataType Type { get; } + + bool ISegment.CanUpdateOffsets => false; + + ulong IOffsetProvider.Offset => 0; + + uint IOffsetProvider.Rva => 0; + + /// + void ISegment.UpdateOffsets(in RelocationParameters parameters) + { + } + + /// + uint IWritable.GetPhysicalSize() => 0; + + /// + uint ISegment.GetVirtualSize() => 0; + + void IWritable.Write(BinaryStreamWriter writer) + { + } +} diff --git a/src/AsmResolver.PE/Debug/SerializedDebugDataEntry.cs b/src/AsmResolver.PE/Debug/SerializedDebugDataEntry.cs index 79a388274..311adfeaf 100644 --- a/src/AsmResolver.PE/Debug/SerializedDebugDataEntry.cs +++ b/src/AsmResolver.PE/Debug/SerializedDebugDataEntry.cs @@ -45,6 +45,9 @@ public SerializedDebugDataEntry( { BinaryStreamReader reader; + if (_sizeOfData == 0) + return new EmptyDebugDataSegment(_type); + if (_addressOfRawData == 0 || _context.File.MappingMode == File.PEMappingMode.Unmapped) { if (!_context.File.TryCreateReaderAtFileOffset(_pointerToRawData, out reader))