Skip to content

Commit

Permalink
Too complicated, making it simpler.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lloyd Kinsella committed Jul 31, 2018
1 parent 0dfde96 commit 4f3fce7
Show file tree
Hide file tree
Showing 16 changed files with 39 additions and 605 deletions.
4 changes: 2 additions & 2 deletions src/Workshell.PE.Testbed/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ static async Task RunAsync(string[] args)
var dataDirectory = image.NTHeaders.DataDirectories[DataDirectoryType.DelayImportDescriptor];
var content = await dataDirectory.GetContentAsync().ConfigureAwait(false);

var table = await ImportHintNameTable.LoadAsync(image).ConfigureAwait(false);
var entries = table.ToArray();
//var table = await ImportHintNameTable.LoadAsync(image).ConfigureAwait(false);
//var entries = table.ToArray();
}
}
}
14 changes: 0 additions & 14 deletions src/Workshell.PE/Content/Imports/DelayedImportAddressTable.cs

This file was deleted.

15 changes: 0 additions & 15 deletions src/Workshell.PE/Content/Imports/DelayedImportAddressTableEntry.cs

This file was deleted.

46 changes: 0 additions & 46 deletions src/Workshell.PE/Content/Imports/DelayedImportAddressTables.cs

This file was deleted.

13 changes: 0 additions & 13 deletions src/Workshell.PE/Content/Imports/DelayedImportHintNameEntry.cs

This file was deleted.

25 changes: 0 additions & 25 deletions src/Workshell.PE/Content/Imports/DelayedImportHintNameTable.cs

This file was deleted.

7 changes: 3 additions & 4 deletions src/Workshell.PE/Content/Imports/ImportAddressTable.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace Workshell.PE.Content
namespace Workshell.PE.Content.Imports
{
public sealed class ImportAddressTable : ImportAddressTableBase<ImportAddressTableEntry, ImportDirectoryEntry>
public sealed class ImportAddressTable : ImportAddressTableBase<ImportAddressTableEntry>
{
internal ImportAddressTable(PortableExecutableImage image, ImportDirectoryEntry directoryEntry, uint tableRVA, ulong[] tableEntries) : base(image, directoryEntry, tableRVA, tableEntries, false)
internal ImportAddressTable(PortableExecutableImage image, uint rva, ulong[] entries) : base(image, rva, entries, false)
{
}
}
Expand Down
55 changes: 25 additions & 30 deletions src/Workshell.PE/Content/Imports/ImportAddressTableBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,42 @@

namespace Workshell.PE.Content
{
public abstract class ImportAddressTableBase<TEntry, TDirectoryEntry> : ISupportsLocation, ISupportsBytes, IEnumerable<TEntry>
public abstract class ImportAddressTableBase<TEntry> : IEnumerable<TEntry>, ISupportsLocation, ISupportsBytes
where TEntry : ImportAddressTableEntryBase
where TDirectoryEntry : ImportDirectoryEntryBase
{
private readonly PortableExecutableImage _image;
private readonly TEntry[] _entries;

protected internal ImportAddressTableBase(PortableExecutableImage image, TDirectoryEntry directoryEntry, uint rva, ulong[] entries, bool isDelayed)
protected internal ImportAddressTableBase(PortableExecutableImage image, uint rva, ulong[] entries, bool isDelayed)
{
_image = image;
_entries = BuildEntries(image, rva, entries);

var calc = image.GetCalculator();
var imageBase = image.NTHeaders.OptionalHeader.ImageBase;
var va = imageBase + rva;
var offset = calc.RVAToOffset(rva);
var size = (entries.Length * (image.Is64Bit ? sizeof(ulong) : sizeof(uint))).ToUInt64();
var section = calc.RVAToSection(rva);

_image = image;
_entries = BuildEntries(image, offset, entries);

DirectoryEntry = directoryEntry;
Location = new Location(calc, offset, rva, va, size, size);
Location = new Location(offset, rva, va, size, size, section);
Count = _entries.Length;
IsDelayed = isDelayed;
}

#region Methods

public IEnumerator<TEntry> GetEnumerator()
{
foreach (var entry in _entries)
yield return entry;
}

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}

public byte[] GetBytes()
{
return GetBytesAsync().GetAwaiter().GetResult();
Expand All @@ -49,28 +59,14 @@ public async Task<byte[]> GetBytesAsync()
return buffer;
}

public IEnumerator<TEntry> GetEnumerator()
{
foreach (var entry in _entries)
yield return entry;
}

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}

private TEntry[] BuildEntries(PortableExecutableImage image, uint rva, ulong[] entries)
private TEntry[] BuildEntries(PortableExecutableImage image, ulong tableOffset, ulong[] entries)
{
var results = new TEntry[entries.Length];
var calc = image.GetCalculator();
var offset = calc.RVAToOffset(rva);

var entryType = typeof(TEntry);
var ctors = entryType.GetConstructors(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
var ctor = ctors.First(); // TODO: Should probably match it better somehow
var ctors = typeof(TEntry).GetConstructors(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
var ctor = ctors.First();
var offset = tableOffset;

for (var i = 0; i < entries.Length; i++)
for(var i = 0; i < entries.Length; i++)
{
var addrOrOrd = entries[i];
ushort ordinal = 0;
Expand All @@ -84,7 +80,7 @@ private TEntry[] BuildEntries(PortableExecutableImage image, uint rva, ulong[] e
{
value &= 0x7fffffff;

ordinal = Convert.ToUInt16(value);
ordinal = value.ToUInt16();
isOrdinal = true;
}
}
Expand All @@ -96,7 +92,7 @@ private TEntry[] BuildEntries(PortableExecutableImage image, uint rva, ulong[] e
{
value &= 0x7fffffffffffffff;

ordinal = Convert.ToUInt16(value);
ordinal = value.ToUInt16();
isOrdinal = true;
}
}
Expand All @@ -123,7 +119,6 @@ private TEntry[] BuildEntries(PortableExecutableImage image, uint rva, ulong[] e

#region Properties

public TDirectoryEntry DirectoryEntry { get; }
public Location Location { get; }
public int Count { get; }
public TEntry this[int index] => _entries[index];
Expand Down
4 changes: 1 addition & 3 deletions src/Workshell.PE/Content/Imports/ImportAddressTableEntry.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Workshell.PE.Extensions;

namespace Workshell.PE.Content
{
public sealed class ImportAddressTableEntry : ImportAddressTableEntryBase
{
internal ImportAddressTableEntry(PortableExecutableImage image, ulong offset, ulong value, uint address, ushort ordinal, bool isOrdinal) : base(image, offset, value, address, ordinal, isOrdinal, false)
internal ImportAddressTableEntry(PortableExecutableImage image, ulong entryOffset, ulong entryValue, uint entryAddress, ushort entryOrdinal, bool isOrdinal) : base(image, entryOffset, entryValue, entryAddress, entryOrdinal, isOrdinal, false)
{
}
}
Expand Down
16 changes: 8 additions & 8 deletions src/Workshell.PE/Content/Imports/ImportAddressTableEntryBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ public abstract class ImportAddressTableEntryBase : ISupportsLocation, ISupports
{
private readonly PortableExecutableImage _image;

protected ImportAddressTableEntryBase(PortableExecutableImage image, ulong offset, ulong value, uint address, ushort ordinal,
bool isOrdinal, bool isDelayed)
protected internal ImportAddressTableEntryBase(PortableExecutableImage image, ulong entryOffset, ulong entryValue, uint entryAddress, ushort entryOrdinal, bool isOrdinal, bool isDelayed)
{
_image = image;

var calc = image.GetCalculator();
var rva = calc.OffsetToRVA(offset);
var va = image.NTHeaders.OptionalHeader.ImageBase + rva;
var rva = calc.OffsetToRVA(entryOffset);
var imageBase = image.NTHeaders.OptionalHeader.ImageBase;
var va = imageBase + rva;
var size = (image.Is64Bit ? sizeof(ulong) : sizeof(uint)).ToUInt64();

Location = new Location(calc, offset, rva, va, size, size);
Value = value;
Address = address;
Ordinal = ordinal;
Location = new Location(calc, entryOffset, rva, va, size, size);
Value = entryValue;
Address = entryAddress;
Ordinal = entryOrdinal;
IsOrdinal = isOrdinal;
IsDelayed = isDelayed;
}
Expand Down
46 changes: 0 additions & 46 deletions src/Workshell.PE/Content/Imports/ImportAddressTables.cs

This file was deleted.

Loading

0 comments on commit 4f3fce7

Please sign in to comment.