From 854b8457deaeeb25ad6416eb88aa45a7584c725a Mon Sep 17 00:00:00 2001 From: Filip Jeremic Date: Thu, 16 Jan 2020 20:02:17 -0500 Subject: [PATCH] Add section to segment mapping --- BinaryTools.Elf.Tests/TestElfSegment.cs | 60 ++++++++++++++++++++++++ BinaryTools.Elf/ElfFile.cs | 2 +- BinaryTools.Elf/ElfProgramHeaderTable.cs | 5 +- BinaryTools.Elf/ElfSegment.cs | 9 ++++ 4 files changed, 74 insertions(+), 2 deletions(-) diff --git a/BinaryTools.Elf.Tests/TestElfSegment.cs b/BinaryTools.Elf.Tests/TestElfSegment.cs index 32d0ebe..6192f4a 100644 --- a/BinaryTools.Elf.Tests/TestElfSegment.cs +++ b/BinaryTools.Elf.Tests/TestElfSegment.cs @@ -136,5 +136,65 @@ public void TestAlignment() Assert.Equal(0x8UL, elfFile.Segments[4].Alignment); Assert.Equal(0x4UL, elfFile.Segments[5].Alignment); } + + [Fact] + public void TestSectionToSegmentMapping() + { + var stream = new FileStream("Binaries/base32", FileMode.Open, FileAccess.Read); + var reader = new EndianBinaryReader(stream, EndianBitConverter.NativeEndianness); + ElfFile elfFile = ElfFile.ReadElfFile(reader); + + Assert.Equal(0, elfFile.Segments[0].Sections.Count); + Assert.Equal(1, elfFile.Segments[1].Sections.Count); + Assert.Equal(elfFile.Sections[1], elfFile.Segments[1].Sections[0]); + + Assert.Equal(18, elfFile.Segments[2].Sections.Count); + Assert.Equal(elfFile.Sections[1], elfFile.Segments[2].Sections[0]); + Assert.Equal(elfFile.Sections[2], elfFile.Segments[2].Sections[1]); + Assert.Equal(elfFile.Sections[3], elfFile.Segments[2].Sections[2]); + Assert.Equal(elfFile.Sections[4], elfFile.Segments[2].Sections[3]); + Assert.Equal(elfFile.Sections[5], elfFile.Segments[2].Sections[4]); + Assert.Equal(elfFile.Sections[6], elfFile.Segments[2].Sections[5]); + Assert.Equal(elfFile.Sections[7], elfFile.Segments[2].Sections[6]); + Assert.Equal(elfFile.Sections[8], elfFile.Segments[2].Sections[7]); + Assert.Equal(elfFile.Sections[9], elfFile.Segments[2].Sections[8]); + Assert.Equal(elfFile.Sections[10], elfFile.Segments[2].Sections[9]); + Assert.Equal(elfFile.Sections[11], elfFile.Segments[2].Sections[10]); + Assert.Equal(elfFile.Sections[12], elfFile.Segments[2].Sections[11]); + Assert.Equal(elfFile.Sections[13], elfFile.Segments[2].Sections[12]); + Assert.Equal(elfFile.Sections[14], elfFile.Segments[2].Sections[13]); + Assert.Equal(elfFile.Sections[15], elfFile.Segments[2].Sections[14]); + Assert.Equal(elfFile.Sections[16], elfFile.Segments[2].Sections[15]); + Assert.Equal(elfFile.Sections[17], elfFile.Segments[2].Sections[16]); + Assert.Equal(elfFile.Sections[18], elfFile.Segments[2].Sections[17]); + + Assert.Equal(8, elfFile.Segments[3].Sections.Count); + Assert.Equal(elfFile.Sections[19], elfFile.Segments[3].Sections[0]); + Assert.Equal(elfFile.Sections[20], elfFile.Segments[3].Sections[1]); + Assert.Equal(elfFile.Sections[21], elfFile.Segments[3].Sections[2]); + Assert.Equal(elfFile.Sections[22], elfFile.Segments[3].Sections[3]); + Assert.Equal(elfFile.Sections[23], elfFile.Segments[3].Sections[4]); + Assert.Equal(elfFile.Sections[24], elfFile.Segments[3].Sections[5]); + Assert.Equal(elfFile.Sections[25], elfFile.Segments[3].Sections[6]); + Assert.Equal(elfFile.Sections[26], elfFile.Segments[3].Sections[7]); + + Assert.Equal(1, elfFile.Segments[4].Sections.Count); + Assert.Equal(elfFile.Sections[22], elfFile.Segments[4].Sections[0]); + + Assert.Equal(2, elfFile.Segments[5].Sections.Count); + Assert.Equal(elfFile.Sections[2], elfFile.Segments[5].Sections[0]); + Assert.Equal(elfFile.Sections[3], elfFile.Segments[5].Sections[1]); + + Assert.Equal(1, elfFile.Segments[6].Sections.Count); + Assert.Equal(elfFile.Sections[17], elfFile.Segments[6].Sections[0]); + + Assert.Equal(0, elfFile.Segments[7].Sections.Count); + Assert.Equal(5, elfFile.Segments[8].Sections.Count); + Assert.Equal(elfFile.Sections[19], elfFile.Segments[8].Sections[0]); + Assert.Equal(elfFile.Sections[20], elfFile.Segments[8].Sections[1]); + Assert.Equal(elfFile.Sections[21], elfFile.Segments[8].Sections[2]); + Assert.Equal(elfFile.Sections[22], elfFile.Segments[8].Sections[3]); + Assert.Equal(elfFile.Sections[23], elfFile.Segments[8].Sections[4]); + } } } diff --git a/BinaryTools.Elf/ElfFile.cs b/BinaryTools.Elf/ElfFile.cs index 78aa4bc..dd0ad1a 100644 --- a/BinaryTools.Elf/ElfFile.cs +++ b/BinaryTools.Elf/ElfFile.cs @@ -127,8 +127,8 @@ public static ElfFile ReadElfFile(BinaryReader reader) } } - var segments = new ElfProgramHeaderTable(reader, header); var sections = new ElfSectionHeaderTable(reader, header); + var segments = new ElfProgramHeaderTable(reader, header, sections); return new ElfFile(header, segments, sections); } diff --git a/BinaryTools.Elf/ElfProgramHeaderTable.cs b/BinaryTools.Elf/ElfProgramHeaderTable.cs index 21d03a1..6b50d13 100644 --- a/BinaryTools.Elf/ElfProgramHeaderTable.cs +++ b/BinaryTools.Elf/ElfProgramHeaderTable.cs @@ -2,6 +2,7 @@ using System.Collections; using System.Collections.Generic; using System.IO; +using System.Linq; namespace BinaryTools.Elf { @@ -56,7 +57,7 @@ public Int32 Count /// /// The ELF header used to extract the metadata about this program header table. /// - internal ElfProgramHeaderTable(BinaryReader reader, ElfHeader header) + internal ElfProgramHeaderTable(BinaryReader reader, ElfHeader header, ElfSectionHeaderTable sections) { // Initialize all segments for (var i = 0; i < header.ProgramHeaderEntryCount; i++) @@ -83,6 +84,8 @@ internal ElfProgramHeaderTable(BinaryReader reader, ElfHeader header) } } + segment.Sections = sections.Where(s => s.Address >= segment.VirtualAddress && s.Address < segment.VirtualAddress + segment.MemorySize).ToList().AsReadOnly(); + segments.Add(segment); } } diff --git a/BinaryTools.Elf/ElfSegment.cs b/BinaryTools.Elf/ElfSegment.cs index 6771ace..b50942d 100644 --- a/BinaryTools.Elf/ElfSegment.cs +++ b/BinaryTools.Elf/ElfSegment.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; namespace BinaryTools.Elf { @@ -130,5 +131,13 @@ public ElfSegmentFlags Flags { get; protected set; } + + /// + /// Gets a list of sections mapped to this segment. + /// + public IReadOnlyList Sections + { + get; internal set; + } } }