Skip to content

Commit

Permalink
Improve PEPrinter
Browse files Browse the repository at this point in the history
  • Loading branch information
xoofx committed Sep 27, 2024
1 parent f7cb66d commit 0f147b4
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,21 @@ Data Directories
[13] = PEDelayImportDirectory Position = 0x000029A8, Size = 0x00000040, RVA = 0x00003BA8, VirtualSize = 0x00000040
[14] = null

Section Headers
[00] .text PESection Position = 0x00000400, Size = 0x00001A00, RVA = 0x00001000, VirtualSize = 0x000019E9, Characteristics = 0x60000020 (ContainsCode, MemExecute, MemRead)
[01] .rdata PESection Position = 0x00001E00, Size = 0x00001A00, RVA = 0x00003000, VirtualSize = 0x0000183C, Characteristics = 0x40000040 (ContainsInitializedData, MemRead)
[02] .data PESection Position = 0x00003800, Size = 0x00000200, RVA = 0x00005000, VirtualSize = 0x000006D0, Characteristics = 0xC0000040 (ContainsInitializedData, MemRead, MemWrite)
[03] .pdata PESection Position = 0x00003A00, Size = 0x00000400, RVA = 0x00006000, VirtualSize = 0x00000210, Characteristics = 0x40000040 (ContainsInitializedData, MemRead)
[04] .rsrc PESection Position = 0x00003E00, Size = 0x00000200, RVA = 0x00007000, VirtualSize = 0x000001E0, Characteristics = 0x40000040 (ContainsInitializedData, MemRead)
[05] .reloc PESection Position = 0x00004000, Size = 0x00000200, RVA = 0x00008000, VirtualSize = 0x0000003C, Characteristics = 0x42000040 (ContainsInitializedData, MemDiscardable, MemRead)

Sections
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
[00] .text PESection Position = 0x00000400, Size = 0x00001A00, RVA = 0x00001000, VirtualSize = 0x000019E9, Characteristics = 0x60000020 (ContainsCode, MemExecute, MemRead)

[00] PEStreamSectionData Position = 0x00000400, Size = 0x000019E9, RVA = 0x00001000, VirtualSize = 0x000019E9


--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
[01] .rdata PESection Position = 0x00001E00, Size = 0x00001A00, RVA = 0x00003000, VirtualSize = 0x0000183C, Characteristics = 0x40000040 (ContainsInitializedData, MemRead)

[00] PEImportAddressTableDirectory Position = 0x00001E00, Size = 0x00000250, RVA = 0x00003000, VirtualSize = 0x00000250
Expand Down Expand Up @@ -400,7 +409,7 @@ Sections

[27] PEStreamSectionData Position = 0x00002E58, Size = 0x000007E4, RVA = 0x00004058, VirtualSize = 0x000007E4


--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
[02] .data PESection Position = 0x00003800, Size = 0x00000200, RVA = 0x00005000, VirtualSize = 0x000006D0, Characteristics = 0xC0000040 (ContainsInitializedData, MemRead, MemWrite)

[00] PEStreamSectionData Position = 0x00003800, Size = 0x00000078, RVA = 0x00005000, VirtualSize = 0x00000078
Expand All @@ -411,7 +420,7 @@ Sections

[02] PEStreamSectionData Position = 0x00003890, Size = 0x00000170, RVA = 0x00005090, VirtualSize = 0x00000170


--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
[03] .pdata PESection Position = 0x00003A00, Size = 0x00000400, RVA = 0x00006000, VirtualSize = 0x00000210, Characteristics = 0x40000040 (ContainsInitializedData, MemRead)

[00] PEExceptionDirectory Position = 0x00003A00, Size = 0x00000210, RVA = 0x00006000, VirtualSize = 0x00000210
Expand Down Expand Up @@ -592,7 +601,7 @@ Sections
[43] UnwindInfoAddress = RVA = 0x3AF0, PEStreamSectionData { RVA = 0x399C, VirtualSize = 0x20C, Position = 0x279C, Size = 0x20C }, Offset = 0x154



--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
[04] .rsrc PESection Position = 0x00003E00, Size = 0x00000200, RVA = 0x00007000, VirtualSize = 0x000001E0, Characteristics = 0x40000040 (ContainsInitializedData, MemRead)

[00] PEResourceDirectory Position = 0x00003E00, Size = 0x000001E0, RVA = 0x00007000, VirtualSize = 0x000001E0
Expand All @@ -603,7 +612,7 @@ Sections
[00] PEStreamSectionData Position = 0x00003FD8, Size = 0x00000008, RVA = 0x000071D8, VirtualSize = 0x00000008



--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
[05] .reloc PESection Position = 0x00004000, Size = 0x00000200, RVA = 0x00008000, VirtualSize = 0x0000003C, Characteristics = 0x42000040 (ContainsInitializedData, MemDiscardable, MemRead)

[00] PEBaseRelocationDirectory Position = 0x00004000, Size = 0x0000003C, RVA = 0x00008000, VirtualSize = 0x0000003C
Expand Down Expand Up @@ -632,4 +641,3 @@ Sections
[000] Dir64 Offset = 0x0078, RVA = 0x5078 (0x000000014000133E), SectionData = { RVA = 0x00005078 (PEBoundImportAddressTable64[1] -> .data) }
[001] Dir64 Offset = 0x0080, RVA = 0x5080 (0x0000000140001332), SectionData = { RVA = 0x00005078 (PEBoundImportAddressTable64[1] -> .data) }


Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,21 @@ Data Directories
[13] = null
[14] = null

Section Headers
[00] .text PESection Position = 0x00000400, Size = 0x00001200, RVA = 0x00001000, VirtualSize = 0x000010C9, Characteristics = 0x60000020 (ContainsCode, MemExecute, MemRead)
[01] .rdata PESection Position = 0x00001600, Size = 0x00001400, RVA = 0x00003000, VirtualSize = 0x00001288, Characteristics = 0x40000040 (ContainsInitializedData, MemRead)
[02] .data PESection Position = 0x00002A00, Size = 0x00000200, RVA = 0x00005000, VirtualSize = 0x00000680, Characteristics = 0xC0000040 (ContainsInitializedData, MemRead, MemWrite)
[03] .pdata PESection Position = 0x00002C00, Size = 0x00000200, RVA = 0x00006000, VirtualSize = 0x00000198, Characteristics = 0x40000040 (ContainsInitializedData, MemRead)
[04] .rsrc PESection Position = 0x00002E00, Size = 0x00000200, RVA = 0x00007000, VirtualSize = 0x000001E0, Characteristics = 0x40000040 (ContainsInitializedData, MemRead)
[05] .reloc PESection Position = 0x00003000, Size = 0x00000200, RVA = 0x00008000, VirtualSize = 0x00000030, Characteristics = 0x42000040 (ContainsInitializedData, MemDiscardable, MemRead)

Sections
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
[00] .text PESection Position = 0x00000400, Size = 0x00001200, RVA = 0x00001000, VirtualSize = 0x000010C9, Characteristics = 0x60000020 (ContainsCode, MemExecute, MemRead)

[00] PEStreamSectionData Position = 0x00000400, Size = 0x000010C9, RVA = 0x00001000, VirtualSize = 0x000010C9


--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
[01] .rdata PESection Position = 0x00001600, Size = 0x00001400, RVA = 0x00003000, VirtualSize = 0x00001288, Characteristics = 0x40000040 (ContainsInitializedData, MemRead)

[00] PEImportAddressTableDirectory Position = 0x00001600, Size = 0x000001F0, RVA = 0x00003000, VirtualSize = 0x000001F0
Expand Down Expand Up @@ -353,12 +362,12 @@ Sections

[21] PEStreamSectionData Position = 0x00002240, Size = 0x00000648, RVA = 0x00003C40, VirtualSize = 0x00000648


--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
[02] .data PESection Position = 0x00002A00, Size = 0x00000200, RVA = 0x00005000, VirtualSize = 0x00000680, Characteristics = 0xC0000040 (ContainsInitializedData, MemRead, MemWrite)

[00] PEStreamSectionData Position = 0x00002A00, Size = 0x00000200, RVA = 0x00005000, VirtualSize = 0x00000200


--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
[03] .pdata PESection Position = 0x00002C00, Size = 0x00000200, RVA = 0x00006000, VirtualSize = 0x00000198, Characteristics = 0x40000040 (ContainsInitializedData, MemRead)

[00] PEExceptionDirectory Position = 0x00002C00, Size = 0x00000198, RVA = 0x00006000, VirtualSize = 0x00000198
Expand Down Expand Up @@ -499,7 +508,7 @@ Sections
[33] UnwindInfoAddress = RVA = 0x3930, PEStreamSectionData { RVA = 0x37E8, VirtualSize = 0x19C, Position = 0x1DE8, Size = 0x19C }, Offset = 0x148



--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
[04] .rsrc PESection Position = 0x00002E00, Size = 0x00000200, RVA = 0x00007000, VirtualSize = 0x000001E0, Characteristics = 0x40000040 (ContainsInitializedData, MemRead)

[00] PEResourceDirectory Position = 0x00002E00, Size = 0x000001E0, RVA = 0x00007000, VirtualSize = 0x000001E0
Expand All @@ -510,7 +519,7 @@ Sections
[00] PEStreamSectionData Position = 0x00002FD8, Size = 0x00000008, RVA = 0x000071D8, VirtualSize = 0x00000008



--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
[05] .reloc PESection Position = 0x00003000, Size = 0x00000200, RVA = 0x00008000, VirtualSize = 0x00000030, Characteristics = 0x42000040 (ContainsInitializedData, MemDiscardable, MemRead)

[00] PEBaseRelocationDirectory Position = 0x00003000, Size = 0x00000030, RVA = 0x00008000, VirtualSize = 0x00000030
Expand All @@ -536,4 +545,3 @@ Sections
[018] Dir64 Offset = 0x03E0, RVA = 0x33E0 (0x0000000140003218), SectionData = { RVA = 0x000032B0 (PELoadConfigDirectory64[2] -> .rdata) }
[019] Dir64 Offset = 0x03E8, RVA = 0x33E8 (0x0000000140003220), SectionData = { RVA = 0x000032B0 (PELoadConfigDirectory64[2] -> .rdata) }


Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,21 @@ Data Directories
[13] = null
[14] = null

Section Headers
[00] .text PESection Position = 0x00000400, Size = 0x00001000, RVA = 0x00001000, VirtualSize = 0x00000F18, Characteristics = 0x60000020 (ContainsCode, MemExecute, MemRead)
[01] .rdata PESection Position = 0x00001400, Size = 0x00000E00, RVA = 0x00002000, VirtualSize = 0x00000C96, Characteristics = 0x40000040 (ContainsInitializedData, MemRead)
[02] .data PESection Position = 0x00002200, Size = 0x00000200, RVA = 0x00003000, VirtualSize = 0x00000680, Characteristics = 0xC0000040 (ContainsInitializedData, MemRead, MemWrite)
[03] .pdata PESection Position = 0x00002400, Size = 0x00000200, RVA = 0x00004000, VirtualSize = 0x000001A4, Characteristics = 0x40000040 (ContainsInitializedData, MemRead)
[04] .rsrc PESection Position = 0x00002600, Size = 0x00000200, RVA = 0x00005000, VirtualSize = 0x000000F8, Characteristics = 0x40000040 (ContainsInitializedData, MemRead)
[05] .reloc PESection Position = 0x00002800, Size = 0x00000200, RVA = 0x00006000, VirtualSize = 0x0000002C, Characteristics = 0x42000040 (ContainsInitializedData, MemDiscardable, MemRead)

Sections
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
[00] .text PESection Position = 0x00000400, Size = 0x00001000, RVA = 0x00001000, VirtualSize = 0x00000F18, Characteristics = 0x60000020 (ContainsCode, MemExecute, MemRead)

[00] PEStreamSectionData Position = 0x00000400, Size = 0x00000F18, RVA = 0x00001000, VirtualSize = 0x00000F18


--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
[01] .rdata PESection Position = 0x00001400, Size = 0x00000E00, RVA = 0x00002000, VirtualSize = 0x00000C96, Characteristics = 0x40000040 (ContainsInitializedData, MemRead)

[00] PEImportAddressTableDirectory Position = 0x00001400, Size = 0x000000E8, RVA = 0x00002000, VirtualSize = 0x000000E8
Expand Down Expand Up @@ -273,12 +282,12 @@ Sections

[15] PEStreamSectionData Position = 0x00001E18, Size = 0x0000027E, RVA = 0x00002A18, VirtualSize = 0x0000027E


--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
[02] .data PESection Position = 0x00002200, Size = 0x00000200, RVA = 0x00003000, VirtualSize = 0x00000680, Characteristics = 0xC0000040 (ContainsInitializedData, MemRead, MemWrite)

[00] PEStreamSectionData Position = 0x00002200, Size = 0x00000200, RVA = 0x00003000, VirtualSize = 0x00000200


--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
[03] .pdata PESection Position = 0x00002400, Size = 0x00000200, RVA = 0x00004000, VirtualSize = 0x000001A4, Characteristics = 0x40000040 (ContainsInitializedData, MemRead)

[00] PEExceptionDirectory Position = 0x00002400, Size = 0x000001A4, RVA = 0x00004000, VirtualSize = 0x000001A4
Expand Down Expand Up @@ -423,7 +432,7 @@ Sections
[34] UnwindInfoAddress = RVA = 0x2814, PEStreamSectionData { RVA = 0x26C4, VirtualSize = 0x1AC, Position = 0x1AC4, Size = 0x1AC }, Offset = 0x150



--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
[04] .rsrc PESection Position = 0x00002600, Size = 0x00000200, RVA = 0x00005000, VirtualSize = 0x000000F8, Characteristics = 0x40000040 (ContainsInitializedData, MemRead)

[00] PEResourceDirectory Position = 0x00002600, Size = 0x000000F8, RVA = 0x00005000, VirtualSize = 0x000000F8
Expand All @@ -434,7 +443,7 @@ Sections
[00] PEStreamSectionData Position = 0x000026EC, Size = 0x0000000C, RVA = 0x000050EC, VirtualSize = 0x0000000C



--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
[05] .reloc PESection Position = 0x00002800, Size = 0x00000200, RVA = 0x00006000, VirtualSize = 0x0000002C, Characteristics = 0x42000040 (ContainsInitializedData, MemDiscardable, MemRead)

[00] PEBaseRelocationDirectory Position = 0x00002800, Size = 0x0000002C, RVA = 0x00006000, VirtualSize = 0x0000002C
Expand All @@ -458,4 +467,3 @@ Sections
[016] Dir64 Offset = 0x02C8, RVA = 0x22C8 (0x0000000180002118), SectionData = { RVA = 0x00002190 (PELoadConfigDirectory64[2] -> .rdata) }
[017] Absolute Zero padding


33 changes: 24 additions & 9 deletions src/LibObjectFile/PE/PEPrinter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,25 +151,40 @@ private static void PrintDataDirectories(PEFile file, ref TextWriterIndenter wri

private static void PrintSections(PEFile file, ref TextWriterIndenter writer)
{
writer.WriteLine("Section Headers");
writer.Indent();
for (var i = 0; i < file.Sections.Count; i++)
{
var section = file.Sections[i];
writer.WriteLine($"[{i:00}] {section.Name,8} {PEDescribe(section)}, Characteristics = 0x{(uint)section.Characteristics:X8} ({section.Characteristics})");
}
writer.Unindent();
writer.WriteLine();

writer.WriteLine("Sections");

string heading = new string('-', 224);
writer.Indent();
for (var i = 0; i < file.Sections.Count; i++)
{
var section = file.Sections[i];
writer.Indent();

writer.WriteLine(heading);
writer.WriteLine($"[{i:00}] {section.Name,8} {PEDescribe(section)}, Characteristics = 0x{(uint)section.Characteristics:X8} ({section.Characteristics})");
writer.WriteLine();
foreach (var data in section.Content)
if (section.Content.Count > 0)
{
writer.Indent();
PrintSectionData(file, data, ref writer);
writer.Unindent();
foreach (var data in section.Content)
{
writer.Indent();
PrintSectionData(file, data, ref writer);
writer.Unindent();
}
}

writer.Unindent();
writer.WriteLine();
}
writer.Unindent();
}

private static void PrintSectionData(PEFile file, PESectionData data, ref TextWriterIndenter writer)
{
writer.WriteLine($"[{data.Index:00}] {PEDescribe(data)}");
Expand Down

0 comments on commit 0f147b4

Please sign in to comment.