Skip to content

Commit

Permalink
Merge remote-tracking branch 'remotes/origin/pei-changes' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Lloyd Kinsella committed May 19, 2023
2 parents c320fb1 + bbdf0b6 commit 34c1dd7
Show file tree
Hide file tree
Showing 61 changed files with 2,126 additions and 1,788 deletions.
10 changes: 5 additions & 5 deletions src/Workshell.PE/Content/CLR/CLRHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ internal static async Task<CLRHeader> GetAsync(PortableExecutableImage image, Lo
var location = new Location(image, clrLocation.FileOffset, clrLocation.RelativeVirtualAddress, clrLocation.VirtualAddress, size.ToUInt32(), size.ToUInt32(), clrLocation.Section);
var stream = image.GetStream();

stream.Seek(clrLocation.FileOffset.ToInt64(), SeekOrigin.Begin);
stream.Seek(clrLocation.FileOffset, SeekOrigin.Begin);

IMAGE_COR20_HEADER header;

Expand Down Expand Up @@ -207,10 +207,10 @@ public CLRDataDirectory GetManagedNativeHeader()
[FieldAnnotation("Minor Runtime Version", Order = 3)]
public ushort MinorRuntimeVersion { get; }

[FieldAnnotation("MetaData Virtual Address", Order = 4)]
[FieldAnnotation("Meta-data Virtual Address", Order = 4)]
public uint MetaDataAddress { get; }

[FieldAnnotation("MetaData Size", Order = 5)]
[FieldAnnotation("Meta-data Size", Order = 5)]
public uint MetaDataSize { get; }

[FieldAnnotation("Flags", Order = 6)]
Expand All @@ -225,10 +225,10 @@ public CLRDataDirectory GetManagedNativeHeader()
[FieldAnnotation("Resources Size", Order = 9)]
public uint ResourcesSize { get; }

[FieldAnnotation("Strongname Signature Virtual Address", Order = 10)]
[FieldAnnotation("Strong-name Signature Virtual Address", Order = 10)]
public uint StrongNameSignatureAddress { get; }

[FieldAnnotation("Strongname Signature Size", Order = 11)]
[FieldAnnotation("Strong-name Signature Size", Order = 11)]
public uint StrongNameSignatureSize { get; }

[FieldAnnotation("Code Manager Table Virtual Address", Order = 12)]
Expand Down
4 changes: 3 additions & 1 deletion src/Workshell.PE/Content/CLR/CLRMetaDataHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,14 @@ public static async Task<CLRMetaDataHeader> LoadAsync(PortableExecutableImage im
var offset = mdLocation.FileOffset;
var section = mdLocation.Section;

stream.Seek(offset.ToInt64(), SeekOrigin.Begin);
stream.Seek(offset, SeekOrigin.Begin);

var signature = await stream.ReadUInt32Async().ConfigureAwait(false);

if (signature != CLR_METADATA_SIGNATURE)
{
throw new PortableExecutableImageException(image, "Incorrect signature found in CLR meta-data header.");
}

var majorVersion = await stream.ReadUInt16Async().ConfigureAwait(false);
var minorVersion = await stream.ReadUInt16Async().ConfigureAwait(false);
Expand Down
8 changes: 5 additions & 3 deletions src/Workshell.PE/Content/CLR/CLRMetaDataStreamTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public static async Task<CLRMetaDataStreamTable> LoadAsync(PortableExecutableIma
var rva = calc.OffsetToRVA(offset);
var va = imageBase + rva;
var entries = await LoadTableAsync(image, header, offset, imageBase).ConfigureAwait(false);
ulong size = 0;
var size = 0L;

foreach (var strm in entries)
{
Expand All @@ -75,11 +75,11 @@ public static async Task<CLRMetaDataStreamTable> LoadAsync(PortableExecutableIma
}
}

private static async Task<CLRMetaDataStreamTableEntry[]> LoadTableAsync(PortableExecutableImage image, CLRMetaDataHeader header, ulong baseOffset, ulong imageBase)
private static async Task<CLRMetaDataStreamTableEntry[]> LoadTableAsync(PortableExecutableImage image, CLRMetaDataHeader header, long baseOffset, ulong imageBase)
{
var stream = image.GetStream();

stream.Seek(baseOffset.ToInt64(),SeekOrigin.Begin);
stream.Seek(baseOffset,SeekOrigin.Begin);

var entries = new List<CLRMetaDataStreamTableEntry>();
var offset = baseOffset;
Expand All @@ -104,7 +104,9 @@ private static async Task<CLRMetaDataStreamTableEntry[]> LoadTableAsync(Portable
size += 1;

if (b <= 0)
{
break;
}

streamName.Append((char)b);
}
Expand Down
10 changes: 6 additions & 4 deletions src/Workshell.PE/Content/CLR/CLRMetaDataStreams.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ internal static Task<CLRMetaDataStreams> LoadAsync(PortableExecutableImage image
streams[i] = stream;
}

uint rva = 0;
ulong va = 0;
ulong offset = 0;
ulong size = 0;
var rva = 0U;
var va = 0UL;
var offset = 0L;
var size = 0L;

if (streams.Length > 0)
{
Expand All @@ -76,7 +76,9 @@ internal static Task<CLRMetaDataStreams> LoadAsync(PortableExecutableImage image
}

foreach (var stream in streams)
{
size += stream.Location.FileSize;
}

var location = new Location(image, offset, rva, va, size, size);
var result = new CLRMetaDataStreams(image, location, streams);
Expand Down
4 changes: 3 additions & 1 deletion src/Workshell.PE/Content/Certificate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,15 @@ public async Task<byte[]> GetCertificateDataAsync()
var offset = Location.FileOffset + Utils.SizeOf<WIN_CERTIFICATE>().ToUInt32();
var stream = Image.GetStream();

stream.Seek(offset.ToInt64(), SeekOrigin.Begin);
stream.Seek(offset, SeekOrigin.Begin);

var buffer = new byte[Length];
var numRead = await stream.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false);

if (numRead == 0)
{
throw new PortableExecutableImageException(Image, "Could not read certificate data from stream.");
}

return buffer;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Workshell.PE/Content/Debug/DebugDirectory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ public static async Task<DebugDirectory> GetAsync(PortableExecutableImage image)

var entrySize = Utils.SizeOf<IMAGE_DEBUG_DIRECTORY>();
var entryCount = dataDirectory.Size / entrySize;
var entries = new Tuple<ulong, IMAGE_DEBUG_DIRECTORY>[entryCount];
var entries = new Tuple<long, IMAGE_DEBUG_DIRECTORY>[entryCount];

for (var i = 0; i < entryCount; i++)
{
var entry = await stream.ReadStructAsync<IMAGE_DEBUG_DIRECTORY>(entrySize).ConfigureAwait(false);

entries[i] = new Tuple<ulong, IMAGE_DEBUG_DIRECTORY>(fileOffset, entry);
entries[i] = new Tuple<long, IMAGE_DEBUG_DIRECTORY>(fileOffset, entry);
}

var directoryEntries = LoadEntries(image, entrySize, entries);
Expand All @@ -98,7 +98,7 @@ public static async Task<DebugDirectory> GetAsync(PortableExecutableImage image)
}
}

private static DebugDirectoryEntry[] LoadEntries(PortableExecutableImage image, int entrySize, Tuple<ulong, IMAGE_DEBUG_DIRECTORY>[] entries)
private static DebugDirectoryEntry[] LoadEntries(PortableExecutableImage image, int entrySize, Tuple<long, IMAGE_DEBUG_DIRECTORY>[] entries)
{
var calc = image.GetCalculator();
var imageBase = image.NTHeaders.OptionalHeader.ImageBase;
Expand Down
3 changes: 2 additions & 1 deletion src/Workshell.PE/Content/Debug/DebugDirectoryEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ public DebugData GetData()
return null;
}

var calc = _image.GetCalculator();
var rva = AddressOfRawData;
var imageBase = _image.NTHeaders.OptionalHeader.ImageBase;
var location = new Location(_image, PointerToRawData, rva, imageBase + rva, SizeOfData, SizeOfData);
Expand Down Expand Up @@ -175,6 +174,8 @@ public DebugData GetData()
[FieldAnnotation("Pointer to Raw Data", Order = 8)]
public uint PointerToRawData { get; }

public bool IsEmpty => (PointerToRawData == 0 && SizeOfData == 0);

#endregion
}
}
9 changes: 9 additions & 0 deletions src/Workshell.PE/Content/Exceptions/ExceptionTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,24 @@ protected internal ExceptionTable(PortableExecutableImage image, DataDirectory d

#region Static Methods

public static ExceptionTable Get(PortableExecutableImage image)
{
return GetAsync(image).GetAwaiter().GetResult();
}

public static async Task<ExceptionTable> GetAsync(PortableExecutableImage image)
{
if (!image.NTHeaders.DataDirectories.Exists(DataDirectoryType.ExceptionTable))
{
return null;
}

var dataDirectory = image.NTHeaders.DataDirectories[DataDirectoryType.ExceptionTable];

if (DataDirectory.IsNullOrEmpty(dataDirectory))
{
return null;
}

var calc = image.GetCalculator();
var section = calc.RVAToSection(dataDirectory.VirtualAddress);
Expand Down
2 changes: 1 addition & 1 deletion src/Workshell.PE/Content/Exceptions/ExceptionTable64.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ internal static async Task<ExceptionTable> GetAsync(PortableExecutableImage imag
var offset = calc.RVAToOffset(dataDirectory.VirtualAddress);
var rva = dataDirectory.VirtualAddress;

stream.Seek(offset.ToInt64(), SeekOrigin.Begin);
stream.Seek(offset, SeekOrigin.Begin);

var entrySize = Utils.SizeOf<IMAGE_RUNTIME_FUNCTION_64>();
var entries = new List<ExceptionTableEntry>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public async Task<ExceptionUnwindInfo> GetUnwindInfoAsync()
{
var offset = calc.RVAToOffset(UnwindInfoAddress);

stream.Seek(offset.ToInt64(), SeekOrigin.Begin);
stream.Seek(offset, SeekOrigin.Begin);

var versionFlags = await stream.ReadByteAsync().ConfigureAwait(false);
var sizeOfProlog = await stream.ReadByteAsync().ConfigureAwait(false);
Expand Down
136 changes: 68 additions & 68 deletions src/Workshell.PE/Content/Exports/Export.cs
Original file line number Diff line number Diff line change
@@ -1,68 +1,68 @@
#region License
// Copyright(c) Workshell Ltd
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#endregion

using System;
using System.Collections.Generic;
using System.Text;

namespace Workshell.PE.Content
{
public sealed class Export
{
internal Export(uint entryPoint, string name, uint ord, string forwardName)
{
EntryPoint = entryPoint;
Name = name;
Ordinal = ord;
ForwardName = forwardName;
}

#region Methods

public override string ToString()
{
string result;

if (string.IsNullOrWhiteSpace(ForwardName))
{
result = $"0x{EntryPoint:X8} {Ordinal:D4} {Name}";
}
else
{
result = $"0x{EntryPoint:X8} {Ordinal:D4} {Name} -> {ForwardName}";
}

return result;
}

#endregion

#region Properties

public uint EntryPoint { get; }
public string Name { get; }
public uint Ordinal { get; }
public string ForwardName { get; }

#endregion
}
}
#region License
// Copyright(c) Workshell Ltd
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#endregion

using System;
using System.Collections.Generic;
using System.Text;

namespace Workshell.PE.Content
{
public sealed class Export
{
internal Export(uint entryPoint, string name, uint ord, string forwardName)
{
EntryPoint = entryPoint;
Name = name ?? string.Empty;
Ordinal = ord;
ForwardName = forwardName ?? string.Empty;
}

#region Methods

public override string ToString()
{
string result;

if (string.IsNullOrWhiteSpace(ForwardName))
{
result = $"0x{EntryPoint:X8} {Ordinal:D4} {Name}";
}
else
{
result = $"0x{EntryPoint:X8} {Ordinal:D4} {Name} -> {ForwardName}";
}

return result;
}

#endregion

#region Properties

public uint EntryPoint { get; }
public string Name { get; }
public uint Ordinal { get; }
public string ForwardName { get; }

#endregion
}
}
Loading

0 comments on commit 34c1dd7

Please sign in to comment.