Skip to content

Commit 0c340ee

Browse files
committed
Add pseudo printer tests
1 parent 37586de commit 0c340ee

File tree

7 files changed

+43
-6
lines changed

7 files changed

+43
-6
lines changed

src/LibObjectFile.Tests/LibObjectFile.Tests.csproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
<None Remove="lib_a.cpp" />
1313
<None Remove="lib_b.cpp" />
1414
<None Remove="multiple_functions.cpp" />
15+
<None Remove="PE\NativeConsole2Win64.exe" />
16+
<None Remove="PE\NativeConsoleWin64.exe" />
17+
<None Remove="PE\NativeLibraryWin64.dll" />
1518
<None Remove="small.cpp" />
1619
</ItemGroup>
1720

@@ -28,6 +31,15 @@
2831
<Content Include="lib_a.cpp">
2932
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
3033
</Content>
34+
<Content Include="PE\NativeConsole2Win64.exe">
35+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
36+
</Content>
37+
<Content Include="PE\NativeConsoleWin64.exe">
38+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
39+
</Content>
40+
<Content Include="PE\NativeLibraryWin64.dll">
41+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
42+
</Content>
3143
<Content Include="small.cpp">
3244
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
3345
</Content>
Binary file not shown.
Binary file not shown.
Binary file not shown.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright (c) Alexandre Mutel. All rights reserved.
2+
// This file is licensed under the BSD-Clause 2 license.
3+
// See the license.txt file in the project root for more information.
4+
5+
using System;
6+
using System.IO;
7+
using System.Linq;
8+
using System.Runtime.InteropServices;
9+
using LibObjectFile.PE;
10+
11+
namespace LibObjectFile.Tests.PE;
12+
13+
public class PEReaderTests
14+
{
15+
[TestCase("NativeConsoleWin64.exe")]
16+
[TestCase("NativeConsole2Win64.exe")]
17+
[TestCase("NativeLibraryWin64.dll")]
18+
19+
public void TestPrinter(string name)
20+
{
21+
var stream = File.OpenRead(Path.Combine(AppContext.BaseDirectory, "PE", name));
22+
var peImage = PEFile.Read(stream);
23+
peImage.Print(Console.Out);
24+
}
25+
}

src/LibObjectFile/PE/DataDirectory/PEExportDirectory.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,20 +124,20 @@ internal override void Bind(PEImageReader reader)
124124
var peFile = reader.File;
125125
var diagnostics = reader.Diagnostics;
126126

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

133133
var streamSectionData = container as PEStreamSectionData;
134134
if (streamSectionData is null)
135135
{
136-
diagnostics.Error(DiagnosticId.PE_ERR_ExportDirectoryInvalidName, $"The section data for Name {NameLink.RVA()} is not a stream section data");
136+
diagnostics.Error(DiagnosticId.PE_ERR_ExportDirectoryInvalidName, $"The section data for Name {(RVA)(uint)NameLink.RVO} is not a stream section data");
137137
return;
138138
}
139139

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

142142
if (ExportFunctionAddressTable is not null)
143143
{

src/LibObjectFile/PE/PEPrinter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -603,11 +603,11 @@ private static void Print(PEExportAddressTable data, ref TextWriterIndenter writ
603603
var entry = data.Values[i];
604604
if (entry.IsForwarderRVA)
605605
{
606-
writer.WriteLine($"[{i}] Forwarder RVA = {entry.ForwarderRVA}");
606+
writer.WriteLine($"[{i}] Forwarder RVA = {entry.ForwarderRVA.RVA()} ({PELink(entry.ForwarderRVA.Container)})");
607607
}
608608
else
609609
{
610-
writer.WriteLine($"[{i}] Exported RVA = {entry.ExportRVA}");
610+
writer.WriteLine($"[{i}] Exported RVA = {entry.ExportRVA.RVA()} ({PELink(entry.ExportRVA.Container)})");
611611
}
612612
}
613613
}

0 commit comments

Comments
 (0)