Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 1c98c6e

Browse files
committed
Parse entries of import section
1 parent b85874d commit 1c98c6e

File tree

3 files changed

+140
-21
lines changed

3 files changed

+140
-21
lines changed

src/tools/r2rdump/R2RDump.cs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,31 @@ private void DumpSectionContents(R2RReader r2r, R2RSection section)
286286
case R2RSection.SectionType.READYTORUN_SECTION_IMPORT_SECTIONS:
287287
foreach (R2RImportSection importSection in r2r.ImportSections)
288288
{
289-
_writer.WriteLine(importSection.ToString());
289+
_writer.Write(importSection.ToString());
290+
if (_raw && importSection.Entries.Count != 0)
291+
{
292+
if (importSection.SectionRVA != 0)
293+
{
294+
_writer.WriteLine("Section Bytes:");
295+
DumpBytes(r2r, importSection.SectionRVA, (uint)importSection.SectionSize);
296+
}
297+
if (importSection.SignatureRVA != 0)
298+
{
299+
_writer.WriteLine("Signature Bytes:");
300+
DumpBytes(r2r, importSection.SignatureRVA, (uint)importSection.Entries.Count * sizeof(int));
301+
}
302+
if (importSection.AuxiliaryDataRVA != 0)
303+
{
304+
_writer.WriteLine("AuxiliaryData Bytes:");
305+
DumpBytes(r2r, importSection.AuxiliaryDataRVA, (uint)importSection.AuxiliaryData.Size);
306+
}
307+
}
308+
foreach (R2RImportSection.ImportSectionEntry entry in importSection.Entries)
309+
{
310+
_writer.WriteLine();
311+
_writer.WriteLine(entry.ToString());
312+
}
313+
_writer.WriteLine();
290314
}
291315
break;
292316
}

src/tools/r2rdump/R2RImportSection.cs

Lines changed: 63 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,54 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System;
6+
using System.Collections.Generic;
7+
using System.Reflection.PortableExecutable;
68
using System.Text;
79

810
namespace R2RDump
911
{
1012
struct R2RImportSection
1113
{
12-
public enum ImportSectionType
14+
public enum CorCompileImportType
1315
{
14-
READYTORUN_IMPORT_SECTION_TYPE_UNKNOWN = 0,
16+
CORCOMPILE_IMPORT_TYPE_UNKNOWN = 0,
17+
CORCOMPILE_IMPORT_TYPE_EXTERNAL_METHOD = 1,
18+
CORCOMPILE_IMPORT_TYPE_STUB_DISPATCH = 2,
19+
CORCOMPILE_IMPORT_TYPE_STRING_HANDLE = 3,
20+
CORCOMPILE_IMPORT_TYPE_TYPE_HANDLE = 4,
21+
CORCOMPILE_IMPORT_TYPE_METHOD_HANDLE = 5,
22+
CORCOMPILE_IMPORT_TYPE_VIRTUAL_METHOD = 6,
1523
};
1624

17-
public enum ImportSectionFlags
25+
public enum CorCompileImportFlags
1826
{
19-
READYTORUN_IMPORT_SECTION_FLAGS_EAGER = 0x0001,
27+
CORCOMPILE_IMPORT_FLAGS_EAGER = 0x0001, // Section at module load time.
28+
CORCOMPILE_IMPORT_FLAGS_CODE = 0x0002, // Section contains code.
29+
CORCOMPILE_IMPORT_FLAGS_PCODE = 0x0004, // Section contains pointers to code.
2030
};
2131

32+
public struct ImportSectionEntry
33+
{
34+
public long Section { get; }
35+
public uint SignatureRVA { get; }
36+
public uint Signature { get; }
37+
public ImportSectionEntry(long section, uint signatureRVA, uint signature)
38+
{
39+
Section = section;
40+
SignatureRVA = signatureRVA;
41+
Signature = signature;
42+
}
43+
44+
public override string ToString()
45+
{
46+
StringBuilder sb = new StringBuilder();
47+
sb.AppendLine($"\tSection: 0x{Section:X8} ({Section})");
48+
sb.AppendLine($"\tSignatureRVA: 0x{SignatureRVA:X8} ({SignatureRVA})");
49+
sb.AppendLine($"\tSection: 0x{Signature:X8} ({Signature})");
50+
return sb.ToString();
51+
}
52+
}
53+
2254
/// <summary>
2355
/// Section containing values to be fixed up
2456
/// </summary>
@@ -28,12 +60,12 @@ public enum ImportSectionFlags
2860
/// <summary>
2961
/// One or more of ImportSectionFlags
3062
/// </summary>
31-
public ushort Flags { get; }
63+
public CorCompileImportFlags Flags { get; }
3264

3365
/// <summary>
3466
/// One of ImportSectionType
3567
/// </summary>
36-
public ImportSectionType Type { get; }
68+
public CorCompileImportType Type { get; }
3769

3870
/// <summary>
3971
///
@@ -43,34 +75,49 @@ public enum ImportSectionFlags
4375
/// <summary>
4476
/// RVA of optional signature descriptors
4577
/// </summary>
46-
public int Signatures { get; }
78+
public int SignatureRVA { get; }
79+
public List<ImportSectionEntry> Entries { get; }
4780

4881
/// <summary>
4982
/// RVA of optional auxiliary data (typically GC info)
5083
/// </summary>
51-
public int AuxiliaryData { get; }
84+
public int AuxiliaryDataRVA { get; }
85+
public GcInfo AuxiliaryData { get; }
5286

53-
public R2RImportSection(int rva, int size, ushort flags, byte type, byte entrySize, int sig, int data)
87+
public R2RImportSection(byte[] image, int rva, int size, CorCompileImportFlags flags, byte type, byte entrySize, int signatureRVA, List<ImportSectionEntry> entries, int auxDataRVA, int auxDataOffset, Machine machine, ushort majorVersion)
5488
{
5589
SectionRVA = rva;
5690
SectionSize = size;
5791
Flags = flags;
58-
Type = (ImportSectionType)type;
92+
Type = (CorCompileImportType)type;
5993
EntrySize = entrySize;
60-
Signatures = sig;
61-
AuxiliaryData = data;
94+
95+
SignatureRVA = signatureRVA;
96+
Entries = entries;
97+
98+
AuxiliaryDataRVA = auxDataRVA;
99+
AuxiliaryData = null;
100+
if (AuxiliaryDataRVA != 0)
101+
{
102+
AuxiliaryData = new GcInfo(image, auxDataOffset, machine, majorVersion);
103+
}
62104
}
63105

64106
public override string ToString()
65107
{
66108
StringBuilder sb = new StringBuilder();
67-
sb.AppendLine($"SectionRVA: 0x{SectionRVA:X8}");
109+
sb.AppendLine($"SectionRVA: 0x{SectionRVA:X8} ({SectionRVA})");
68110
sb.AppendLine($"SectionSize: {SectionSize} bytes");
69-
sb.AppendLine($"Flags: {Enum.GetName(typeof(ImportSectionFlags), Flags)}({Flags})");
111+
sb.AppendLine($"Flags: {Flags}");
70112
sb.AppendLine($"Type: {Type}");
71113
sb.AppendLine($"EntrySize: {EntrySize}");
72-
sb.AppendLine($"Signatures: 0x{Signatures:X8}");
73-
sb.AppendLine($"AuxiliaryData: {AuxiliaryData}");
114+
sb.AppendLine($"SignatureRVA: 0x{SignatureRVA:X8} ({SignatureRVA})");
115+
sb.AppendLine($"AuxiliaryDataRVA: 0x{AuxiliaryDataRVA:X8} ({AuxiliaryDataRVA})");
116+
if (AuxiliaryDataRVA != 0)
117+
{
118+
sb.AppendLine("AuxiliaryData:");
119+
sb.AppendLine(AuxiliaryData.ToString());
120+
}
74121
return sb.ToString();
75122
}
76123
}

src/tools/r2rdump/R2RReader.cs

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -301,13 +301,61 @@ private void ParseImportSections()
301301
while (offset < endOffset)
302302
{
303303
int rva = NativeReader.ReadInt32(Image, ref offset);
304+
int sectionOffset = GetOffset(rva);
304305
int size = NativeReader.ReadInt32(Image, ref offset);
305-
ushort flags = NativeReader.ReadUInt16(Image, ref offset);
306+
R2RImportSection.CorCompileImportFlags flags = (R2RImportSection.CorCompileImportFlags)NativeReader.ReadUInt16(Image, ref offset);
306307
byte type = NativeReader.ReadByte(Image, ref offset);
307308
byte entrySize = NativeReader.ReadByte(Image, ref offset);
308-
int sig = NativeReader.ReadInt32(Image, ref offset);
309-
int data = NativeReader.ReadInt32(Image, ref offset);
310-
ImportSections.Add(new R2RImportSection(rva, size, flags, type, entrySize, sig, data));
309+
int entryCount = 0;
310+
if (entrySize != 0)
311+
{
312+
entryCount = size / entrySize;
313+
}
314+
int signatureRVA = NativeReader.ReadInt32(Image, ref offset);
315+
316+
int signatureOffset = 0;
317+
if (signatureRVA != 0)
318+
{
319+
signatureOffset = GetOffset(signatureRVA);
320+
}
321+
List<R2RImportSection.ImportSectionEntry> entries = new List<R2RImportSection.ImportSectionEntry>();
322+
switch (flags)
323+
{
324+
case R2RImportSection.CorCompileImportFlags.CORCOMPILE_IMPORT_FLAGS_EAGER:
325+
{
326+
int tempSignatureOffset = signatureOffset;
327+
int firstSigRva = NativeReader.ReadInt32(Image, ref tempSignatureOffset);
328+
uint sigRva = 0;
329+
while (sigRva != firstSigRva)
330+
{
331+
sigRva = NativeReader.ReadUInt32(Image, ref signatureOffset);
332+
long section = NativeReader.ReadInt64(Image, ref sectionOffset);
333+
int sigOff = GetOffset((int)sigRva);
334+
uint signature = NativeReader.ReadUInt32(Image, ref sigOff);
335+
entries.Add(new R2RImportSection.ImportSectionEntry(section, sigRva, signature));
336+
}
337+
}
338+
break;
339+
case R2RImportSection.CorCompileImportFlags.CORCOMPILE_IMPORT_FLAGS_CODE:
340+
case R2RImportSection.CorCompileImportFlags.CORCOMPILE_IMPORT_FLAGS_PCODE:
341+
for (int i = 0; i < entryCount; i++)
342+
{
343+
long section = NativeReader.ReadInt64(Image, ref sectionOffset);
344+
uint sigRva = NativeReader.ReadUInt32(Image, ref signatureOffset);
345+
int sigOff = GetOffset((int)sigRva);
346+
uint signature = NativeReader.ReadUInt32(Image, ref sigOff);
347+
entries.Add(new R2RImportSection.ImportSectionEntry(section, sigRva, signature));
348+
}
349+
break;
350+
}
351+
352+
int auxDataRVA = NativeReader.ReadInt32(Image, ref offset);
353+
int auxDataOffset = 0;
354+
if (auxDataRVA != 0)
355+
{
356+
auxDataOffset = GetOffset(auxDataRVA);
357+
}
358+
ImportSections.Add(new R2RImportSection(Image, rva, size, flags, type, entrySize, signatureRVA, entries, auxDataRVA, auxDataOffset, Machine, R2RHeader.MajorVersion));
311359
}
312360
}
313361

0 commit comments

Comments
 (0)