Skip to content

Commit

Permalink
Add pseudo printer tests
Browse files Browse the repository at this point in the history
  • Loading branch information
xoofx committed Sep 25, 2024
1 parent 37586de commit 0c340ee
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 6 deletions.
12 changes: 12 additions & 0 deletions src/LibObjectFile.Tests/LibObjectFile.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
<None Remove="lib_a.cpp" />
<None Remove="lib_b.cpp" />
<None Remove="multiple_functions.cpp" />
<None Remove="PE\NativeConsole2Win64.exe" />
<None Remove="PE\NativeConsoleWin64.exe" />
<None Remove="PE\NativeLibraryWin64.dll" />
<None Remove="small.cpp" />
</ItemGroup>

Expand All @@ -28,6 +31,15 @@
<Content Include="lib_a.cpp">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="PE\NativeConsole2Win64.exe">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="PE\NativeConsoleWin64.exe">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="PE\NativeLibraryWin64.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="small.cpp">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
Expand Down
Binary file not shown.
Binary file added src/LibObjectFile.Tests/PE/NativeConsoleWin64.exe
Binary file not shown.
Binary file added src/LibObjectFile.Tests/PE/NativeLibraryWin64.dll
Binary file not shown.
25 changes: 25 additions & 0 deletions src/LibObjectFile.Tests/PE/PEReaderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) Alexandre Mutel. All rights reserved.
// This file is licensed under the BSD-Clause 2 license.
// See the license.txt file in the project root for more information.

using System;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using LibObjectFile.PE;

namespace LibObjectFile.Tests.PE;

public class PEReaderTests
{
[TestCase("NativeConsoleWin64.exe")]
[TestCase("NativeConsole2Win64.exe")]
[TestCase("NativeLibraryWin64.dll")]

public void TestPrinter(string name)
{
var stream = File.OpenRead(Path.Combine(AppContext.BaseDirectory, "PE", name));
var peImage = PEFile.Read(stream);
peImage.Print(Console.Out);
}
}
8 changes: 4 additions & 4 deletions src/LibObjectFile/PE/DataDirectory/PEExportDirectory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,20 +124,20 @@ internal override void Bind(PEImageReader reader)
var peFile = reader.File;
var diagnostics = reader.Diagnostics;

if (!peFile.TryFindContainerByRVA(NameLink.RVA(), out var container))
if (!peFile.TryFindContainerByRVA((RVA)(uint)NameLink.RVO, out var container))
{
diagnostics.Error(DiagnosticId.PE_ERR_ExportDirectoryInvalidName, $"Unable to find the section data for Name {NameLink.RVA()}");
diagnostics.Error(DiagnosticId.PE_ERR_ExportDirectoryInvalidName, $"Unable to find the section data for Name {(RVA)(uint)NameLink.RVO}");
return;
}

var streamSectionData = container as PEStreamSectionData;
if (streamSectionData is null)
{
diagnostics.Error(DiagnosticId.PE_ERR_ExportDirectoryInvalidName, $"The section data for Name {NameLink.RVA()} is not a stream section data");
diagnostics.Error(DiagnosticId.PE_ERR_ExportDirectoryInvalidName, $"The section data for Name {(RVA)(uint)NameLink.RVO} is not a stream section data");
return;
}

NameLink = new PEAsciiStringLink(streamSectionData, NameLink.RVO);
NameLink = new PEAsciiStringLink(streamSectionData, NameLink.RVO - streamSectionData.RVA);

if (ExportFunctionAddressTable is not null)
{
Expand Down
4 changes: 2 additions & 2 deletions src/LibObjectFile/PE/PEPrinter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -603,11 +603,11 @@ private static void Print(PEExportAddressTable data, ref TextWriterIndenter writ
var entry = data.Values[i];
if (entry.IsForwarderRVA)
{
writer.WriteLine($"[{i}] Forwarder RVA = {entry.ForwarderRVA}");
writer.WriteLine($"[{i}] Forwarder RVA = {entry.ForwarderRVA.RVA()} ({PELink(entry.ForwarderRVA.Container)})");
}
else
{
writer.WriteLine($"[{i}] Exported RVA = {entry.ExportRVA}");
writer.WriteLine($"[{i}] Exported RVA = {entry.ExportRVA.RVA()} ({PELink(entry.ExportRVA.Container)})");
}
}
}
Expand Down

0 comments on commit 0c340ee

Please sign in to comment.