Skip to content

Commit

Permalink
Lots of work coming out of PEI.
Browse files Browse the repository at this point in the history
  • Loading branch information
lkinsella committed Nov 5, 2019
1 parent bc88145 commit 13a6bb0
Show file tree
Hide file tree
Showing 16 changed files with 675 additions and 557 deletions.
8 changes: 4 additions & 4 deletions Src/Workshell.PE/Content/CLR/CLRHeader.cs
Original file line number Diff line number Diff line change
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
1 change: 0 additions & 1 deletion Src/Workshell.PE/Location.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ internal Location(PortableExecutableImage image, long fileOffset, uint rva, ulon
VirtualSize = virtualSize;
}


#region Methods

public override string ToString()
Expand Down
15 changes: 15 additions & 0 deletions Src/Workshell.PE/SectionTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,19 @@ public SectionCharacteristicsType GetCharacteristics()
return (SectionCharacteristicsType)_header.Characteristics;
}

public Section GetSection()
{
foreach(var section in _image.Sections)
{
if (section.TableEntry == this)
{
return section;
}
}

return null;
}

private string GetName()
{
var builder = new StringBuilder(16);
Expand All @@ -257,6 +270,8 @@ private string GetName()

public string Name { get; }

public bool IsEmpty => (_header.SizeOfRawData == 0);

public uint VirtualSizeOrPhysicalAddress => _header.VirtualSize;
public uint VirtualAddress => _header.VirtualAddress;
public uint SizeOfRawData => _header.SizeOfRawData;
Expand Down
1 change: 0 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
27 changes: 27 additions & 0 deletions src/Workshell.PE/Content/Exports/ExportTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,27 @@ protected ExportTable(PortableExecutableImage image, DataDirectory dataDirectory

#region Static Methods

public static ExportTable<uint> GetFunctionAddressTable(PortableExecutableImage image, ExportDirectory directory = null)
{
return GetFunctionAddressTableAsync(image, directory).GetAwaiter().GetResult();
}

public static ExportTable<uint> GetNameAddressTable(PortableExecutableImage image, ExportDirectory directory = null)
{
return GetNameAddressTableAsync(image, directory).GetAwaiter().GetResult();
}

public static ExportTable<ushort> GetOrdinalTable(PortableExecutableImage image, ExportDirectory directory = null)
{
return GetOrdinalTableAsync(image, directory).GetAwaiter().GetResult();
}

public static async Task<ExportTable<uint>> GetFunctionAddressTableAsync(PortableExecutableImage image, ExportDirectory directory = null)
{
if (directory == null)
{
directory = await ExportDirectory.GetAsync(image).ConfigureAwait(false);
}

var calc = image.GetCalculator();
var section = calc.RVAToSection(directory.AddressOfFunctions);
Expand All @@ -58,7 +75,9 @@ public static async Task<ExportTable<uint>> GetFunctionAddressTableAsync(Portabl
try
{
for (var i = 0; i < directory.NumberOfFunctions; i++)
{
addresses[i] = await stream.ReadUInt32Async().ConfigureAwait(false);
}
}
catch (Exception ex)
{
Expand All @@ -75,7 +94,9 @@ public static async Task<ExportTable<uint>> GetFunctionAddressTableAsync(Portabl
public static async Task<ExportTable<uint>> GetNameAddressTableAsync(PortableExecutableImage image, ExportDirectory directory = null)
{
if (directory == null)
{
directory = await ExportDirectory.GetAsync(image).ConfigureAwait(false);
}

var calc = image.GetCalculator();
var section = calc.RVAToSection(directory.AddressOfNames);
Expand All @@ -92,7 +113,9 @@ public static async Task<ExportTable<uint>> GetNameAddressTableAsync(PortableExe
try
{
for (var i = 0; i < directory.NumberOfNames; i++)
{
addresses[i] = await stream.ReadUInt32Async().ConfigureAwait(false);
}
}
catch (Exception ex)
{
Expand All @@ -109,7 +132,9 @@ public static async Task<ExportTable<uint>> GetNameAddressTableAsync(PortableExe
public static async Task<ExportTable<ushort>> GetOrdinalTableAsync(PortableExecutableImage image, ExportDirectory directory = null)
{
if (directory == null)
{
directory = await ExportDirectory.GetAsync(image).ConfigureAwait(false);
}

var calc = image.GetCalculator();
var section = calc.RVAToSection(directory.AddressOfNameOrdinals);
Expand All @@ -126,7 +151,9 @@ public static async Task<ExportTable<ushort>> GetOrdinalTableAsync(PortableExecu
try
{
for (var i = 0; i < directory.NumberOfNames; i++)
{
ordinals[i] = await stream.ReadUInt16Async().ConfigureAwait(false);
}
}
catch (Exception ex)
{
Expand Down
73 changes: 38 additions & 35 deletions src/Workshell.PE/Content/Imports/DelayedImportAddressTable.cs
Original file line number Diff line number Diff line change
@@ -1,35 +1,38 @@
#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 DelayedImportAddressTable : ImportAddressTableBase<DelayedImportAddressTableEntry>
{
internal DelayedImportAddressTable(PortableExecutableImage image, uint rva, ulong[] entries, ImportDirectoryEntryBase directoryEntry) : base(image, rva, entries, directoryEntry, true)
{
}
}
}
#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.IO;
using System.Text;
using System.Threading.Tasks;
using Workshell.PE.Extensions;

namespace Workshell.PE.Content
{
public sealed class DelayedImportAddressTable : ImportAddressTableBase<DelayedImportAddressTableEntry>
{
internal DelayedImportAddressTable(PortableExecutableImage image, uint rva, ulong[] entries, ImportDirectoryEntryBase directoryEntry) : base(image, rva, entries, directoryEntry, true)
{
}
}
}
40 changes: 35 additions & 5 deletions src/Workshell.PE/Content/Imports/DelayedImportAddressTables.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,47 @@ internal DelayedImportAddressTables(PortableExecutableImage image, DataDirectory

#region Static Methods

public static async Task<DelayedImportAddressTables> GetLookupTableAsync(PortableExecutableImage image, DelayedImportDirectory directory = null)
public static DelayedImportAddressTables GetLookupTables(PortableExecutableImage image, DelayedImportDirectory directory = null)
{
return await GetTableAsync(image, directory, (entry) => entry.DelayNameTable).ConfigureAwait(false);
return GetLookupTablesAsync(image, directory).GetAwaiter().GetResult();
}

public static async Task<DelayedImportAddressTables> GetAddressTableAsync(PortableExecutableImage image, DelayedImportDirectory directory = null)
public static DelayedImportAddressTables GetAddressTables(PortableExecutableImage image, DelayedImportDirectory directory = null)
{
return await GetTableAsync(image, directory, (entry) => entry.DelayAddressTable).ConfigureAwait(false);
return GetAddressTablesAsync(image, directory).GetAwaiter().GetResult();
}

private static async Task<DelayedImportAddressTables> GetTableAsync(PortableExecutableImage image, DelayedImportDirectory directory, Func<DelayedImportDirectoryEntry, uint> thunkHandler)
public static DelayedImportAddressTables GetBoundAddressTables(PortableExecutableImage image, DelayedImportDirectory directory = null)
{
return GetBoundAddressTablesAsync(image, directory).GetAwaiter().GetResult();
}

public static DelayedImportAddressTables GetUnloadAddressTables(PortableExecutableImage image, DelayedImportDirectory directory = null)
{
return GetUnloadAddressTablesAsync(image, directory).GetAwaiter().GetResult();
}

public static async Task<DelayedImportAddressTables> GetLookupTablesAsync(PortableExecutableImage image, DelayedImportDirectory directory = null)
{
return await GetTablesAsync(image, directory, (entry) => entry.DelayNameTable).ConfigureAwait(false);
}

public static async Task<DelayedImportAddressTables> GetAddressTablesAsync(PortableExecutableImage image, DelayedImportDirectory directory = null)
{
return await GetTablesAsync(image, directory, (entry) => entry.DelayAddressTable).ConfigureAwait(false);
}

public static async Task<DelayedImportAddressTables> GetBoundAddressTablesAsync(PortableExecutableImage image, DelayedImportDirectory directory = null)
{
return await GetTablesAsync(image, directory, (entry) => entry.BoundDelayAddressTable).ConfigureAwait(false);
}

public static async Task<DelayedImportAddressTables> GetUnloadAddressTablesAsync(PortableExecutableImage image, DelayedImportDirectory directory = null)
{
return await GetTablesAsync(image, directory, (entry) => entry.UnloadDelayAddressTable).ConfigureAwait(false);
}

private static async Task<DelayedImportAddressTables> GetTablesAsync(PortableExecutableImage image, DelayedImportDirectory directory, Func<DelayedImportDirectoryEntry, uint> thunkHandler)
{
if (directory == null)
directory = await DelayedImportDirectory.GetAsync(image).ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ internal DelayedImportDirectoryEntry(PortableExecutableImage image, Location loc
ModuleHandle = descriptor.ModuleHandle;
DelayAddressTable = descriptor.DelayAddressTable;
DelayNameTable = descriptor.DelayNameTable;
BoundDelayIAT = descriptor.BoundDelayIAT;
UnloadDelayIAT = descriptor.UnloadDelayIAT;
BoundDelayAddressTable = descriptor.BoundDelayIAT;
UnloadDelayAddressTable = descriptor.UnloadDelayIAT;
TimeDateStamp = descriptor.TimeDateStamp;
}

Expand Down Expand Up @@ -78,10 +78,10 @@ public string GetName()
public uint DelayNameTable { get; }

[FieldAnnotation("Bound Delay Import Address Table", Order = 6)]
public uint BoundDelayIAT { get; }
public uint BoundDelayAddressTable { get; }

[FieldAnnotation("Unload Delay Import Address Table", Order = 7)]
public uint UnloadDelayIAT { get; }
public uint UnloadDelayAddressTable { get; }

[FieldAnnotation("Date/Time Stamp", Order = 8)]
public uint TimeDateStamp { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,20 @@ internal DelayedImportHintNameTable(PortableExecutableImage image, DataDirectory

#region Static Methods

public static DelayedImportHintNameTable Get(PortableExecutableImage image, DelayedImportDirectory directory = null)
{
return GetAsync(image, directory).GetAwaiter().GetResult();
}

public static async Task<DelayedImportHintNameTable> GetAsync(PortableExecutableImage image, DelayedImportDirectory directory = null)
{
if (directory == null)
{
directory = await DelayedImportDirectory.GetAsync(image).ConfigureAwait(false);
}

var entries = new Dictionary<uint, Tuple<long, uint, ushort, string, bool>>();
var ilt = await DelayedImportAddressTables.GetLookupTableAsync(image, directory).ConfigureAwait(false);
var ilt = await DelayedImportAddressTables.GetLookupTablesAsync(image, directory).ConfigureAwait(false);
var calc = image.GetCalculator();
var stream = image.GetStream();

Expand Down
2 changes: 1 addition & 1 deletion src/Workshell.PE/Content/Imports/DelayedImports.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static async Task<DelayedImports> GetAsync(PortableExecutableImage image)
return null;
}

var ilt = await DelayedImportAddressTables.GetLookupTableAsync(image, directory).ConfigureAwait(false);
var ilt = await DelayedImportAddressTables.GetLookupTablesAsync(image, directory).ConfigureAwait(false);

if (ilt == null)
{
Expand Down
Loading

0 comments on commit 13a6bb0

Please sign in to comment.