Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
codehz committed Feb 8, 2020
1 parent 25129f1 commit 298562d
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 23 deletions.
4 changes: 2 additions & 2 deletions EatPdb/PEAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;

namespace EatPdb {

internal class PEAction : IDisposable {
private readonly FileStream File;
private readonly DataDir[] Dirs;
Expand Down Expand Up @@ -89,4 +89,4 @@ internal IEnumerable<string> GetImportSymbols() {

public void Dispose() => Reader.Dispose();
}
}
}
16 changes: 13 additions & 3 deletions EatPdb/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,35 @@
using System.Runtime.InteropServices;

namespace EatPdb {
class Program {

internal class Program {

public class Options {

[Option('i', "Input", Required = true, HelpText = "Input File")]
public string InputFile { get; set; }

[Option('p', "Pdb", HelpText = "Pdb File")]
public string PdbFile { get; set; }

[Option('o', "Ouput", Required = true, HelpText = "Output File")]
public string OuputFile { get; set; }

[Option("DllName", HelpText = "DllName")]
public string DllName { get; set; }

[Option('d', "Definition", HelpText = "Module-Definition file")]
public string Definition { get; set; }

[Option('v', "Verbose", Default = false, HelpText = "Verbose Output")]
public bool Verbose { get; set; }
}
static void Main(string[] args) => Parser.Default.ParseArguments<Options>(args)

private static void Main(string[] args) => Parser.Default.ParseArguments<Options>(args)
.WithParsed(RealMain);

private static uint GetAlign(uint length, uint align = 512) => length % align == 0 ? length : (length / align + 1) * align;

private static void RealMain(Options options) {
try {
if (options.PdbFile == null)
Expand Down Expand Up @@ -141,4 +151,4 @@ private static void RealMain(Options options) {
}
}
}
}
}
4 changes: 3 additions & 1 deletion EatPdb/SymbolDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
using System.Linq;

namespace EatPdb {

internal class SymbolDatabase : IEnumerable<KeyValuePair<uint, SortedSet<string>>> {
private readonly SortedDictionary<uint, SortedSet<string>> fullmap = new SortedDictionary<uint, SortedSet<string>>();
private readonly SortedDictionary<string, uint> revmap = new SortedDictionary<string, uint>();

public void Add(uint RVA, string name) {
if (fullmap.TryGetValue(RVA, out var set))
set.Add(name);
Expand Down Expand Up @@ -44,4 +46,4 @@ public KeyValuePair<string, ushort>[] Build() {
return ret.ToArray();
}
}
}
}
16 changes: 11 additions & 5 deletions PEReader/Program.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
using CommandLine;
using PESupport;
using System;
using System.IO;
using PESupport;

namespace PEReader {
class Program {

internal class Program {

public class Options {

[Option('i', "Input", Required = true, HelpText = "Input File")]
public string InputFile { get; set; }
}
static void Main(string[] args) => Parser.Default.ParseArguments<Options>(args)

private static void Main(string[] args) => Parser.Default.ParseArguments<Options>(args)
.WithParsed(RealMain);

private static readonly string[] DataDirName = new string[]{
"Export",
"Import",
Expand All @@ -29,6 +34,7 @@ static void Main(string[] args) => Parser.Default.ParseArguments<Options>(args)
"COMRuntimedescriptor",
"Reserved",
};

private static void RealMain(Options options) {
try {
using var file = File.OpenRead(options.InputFile);
Expand Down Expand Up @@ -113,7 +119,7 @@ private static void RealMain(Options options) {
if (thunk.TryGetOrdinal(out var ord)) {
Console.WriteLine("#{0}", ord);
} else {
file.SeekRVA(resolver, (uint)thunk.Value);
file.SeekRVA(resolver, (uint) thunk.Value);
var hint = reader.ReadStruct<ImportDirThunkHint>().Hint;
Console.WriteLine("\t{0:X4}:{1}", hint, reader.ReadByteString());
}
Expand All @@ -125,4 +131,4 @@ private static void RealMain(Options options) {
}
}
}
}
}
11 changes: 9 additions & 2 deletions PESupport/AddressResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,21 @@
using System.Collections.Generic;

namespace PESupport {

public class AddressResolver {
private readonly List<SectionHeader> Sections;

[Serializable]
private class AddressNotFoundException : Exception {
public AddressNotFoundException(uint RVA) : base(string.Format("RVA {0} not mapped in this PE file", RVA)) { }

public AddressNotFoundException(uint RVA) : base(string.Format("RVA {0} not mapped in this PE file", RVA)) {
}
}

public AddressResolver(uint number) => Sections = new List<SectionHeader>();

public void Put(SectionHeader header) => Sections.Add(header);

public SectionHeader GetSection(uint RVA) {
foreach (var secheader in Sections) {
if (secheader.VirtualAddress <= RVA && secheader.VirtualAddress + secheader.Misc.VirtualSize > RVA) {
Expand All @@ -20,6 +25,7 @@ public SectionHeader GetSection(uint RVA) {
}
throw new AddressNotFoundException(RVA);
}

public uint Resolve(uint RVA) {
foreach (var secheader in Sections) {
if (secheader.VirtualAddress <= RVA && secheader.VirtualAddress + secheader.Misc.VirtualSize > RVA) {
Expand All @@ -28,6 +34,7 @@ public uint Resolve(uint RVA) {
}
throw new AddressNotFoundException(RVA);
}

public uint RevResolve(uint FOA) {
foreach (var secheader in Sections) {
if (secheader.PointerToRawData <= FOA && secheader.Misc.VirtualSize > FOA) {
Expand All @@ -37,4 +44,4 @@ public uint RevResolve(uint FOA) {
throw new AddressNotFoundException(FOA);
}
}
}
}
7 changes: 6 additions & 1 deletion PESupport/BinaryEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
using System.Text;

namespace PESupport {

public static class BinaryEx {

public static string ReadByteString(this BinaryReader reader) {
var builder = new StringBuilder();
while (true) {
Expand All @@ -13,12 +15,15 @@ public static string ReadByteString(this BinaryReader reader) {
}
return builder.ToString();
}

public static void WriteByteString(this BinaryWriter writer, string data) {
var bytes = Encoding.ASCII.GetBytes(data);
writer.Write(bytes);
writer.Write((byte) 0);
}

public static T ReadStruct<T>(this BinaryReader reader) where T : unmanaged => StructOP<T>.Read(reader);

public static void WriteStruct<T>(this BinaryWriter reader, T input) where T : unmanaged => StructOP<T>.Write(input, reader);
}
}
}
4 changes: 3 additions & 1 deletion PESupport/FileEx.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System.IO;

namespace PESupport {

public static class FileEx {

public static void SeekRVA(this FileStream file, AddressResolver resolver, uint RVA) => file.Seek(resolver.Resolve(RVA), SeekOrigin.Begin);
}
}
}
6 changes: 5 additions & 1 deletion PESupport/PESupport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public struct NtHeader {
public OptionalHeader OptionalHeader;

public bool CheckHealth() => Signature == 0x4550 && FileHeader.Machine == 0x8664 && OptionalHeader.Magic == 0x20B;

public void AssertHealth() {
if (!CheckHealth())
throw new NotSupportedException("x86_64 PE file expected");
Expand Down Expand Up @@ -126,8 +127,10 @@ public unsafe void SetName(string name) {

[StructLayout(LayoutKind.Explicit)]
public unsafe struct SectionHeaderMisc {

[FieldOffset(0)]
public uint PhysicalAddress;

[FieldOffset(0)]
public uint VirtualSize;
}
Expand Down Expand Up @@ -171,6 +174,7 @@ public struct ImportDirThunk {
public ulong Value;

public bool IsEmpty() => Value == 0;

public bool TryGetOrdinal(out uint ordinal) {
if ((Value & 0x8000000000000000) != 0) {
ordinal = (uint) Value;
Expand All @@ -185,4 +189,4 @@ public bool TryGetOrdinal(out uint ordinal) {
public struct ImportDirThunkHint {
public ushort Hint;
}
}
}
5 changes: 4 additions & 1 deletion PESupport/StructOP.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@
using System.Runtime.InteropServices;

namespace PESupport {

internal static class StructOP<T> where T : unmanaged {
private static readonly int Size = Marshal.SizeOf<T>();

public static unsafe T Read(BinaryReader reader) {
T val;
var ptr = (byte*) &val;
for (uint i = 0; i < Size; i++)
*ptr++ = reader.ReadByte();
return val;
}

public static unsafe void Write(T input, BinaryWriter writer) {
var ptr = (byte*) &input;
for (uint i = 0; i < Size; i++)
writer.Write(*ptr++);
}
}
}
}
21 changes: 15 additions & 6 deletions PdbReader/Program.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,36 @@
using System;
using System.Linq;
using CommandLine;
using SharpPdb.Native;
using CommandLine;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;

namespace PdbReader {
class Program {

internal class Program {

public class Options {

[Option('v', "Verbose", Default = false, HelpText = "Verbose output")]
public bool Verbose { get; set; }

[Option('d', "Demangle", Default = false, HelpText = "Demangle function name")]
public bool Demangle { get; set; }

[Option('i', "Input", Required = true, HelpText = "Input File")]
public string InputFile { get; set; }
}
static void Main(string[] args) => Parser.Default.ParseArguments<Options>(args)

private static void Main(string[] args) => Parser.Default.ParseArguments<Options>(args)
.WithParsed(RealMain);

private class SymbolEqualityComparer : IEqualityComparer<PdbPublicSymbol> {

public bool Equals([AllowNull] PdbPublicSymbol x, [AllowNull] PdbPublicSymbol y) => x.RelativeVirtualAddress == y.RelativeVirtualAddress;

public int GetHashCode([DisallowNull] PdbPublicSymbol obj) => obj.RelativeVirtualAddress.GetHashCode();
}

private static void RealMain(Options options) {
try {
using var reader = new PdbFileReader(options.InputFile);
Expand All @@ -40,4 +49,4 @@ private static void RealMain(Options options) {
}
}
}
}
}

0 comments on commit 298562d

Please sign in to comment.