Skip to content

Commit

Permalink
Add section to segment mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
fjeremic committed Jan 17, 2020
1 parent 34e5dc0 commit 854b845
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 2 deletions.
60 changes: 60 additions & 0 deletions BinaryTools.Elf.Tests/TestElfSegment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}
}
2 changes: 1 addition & 1 deletion BinaryTools.Elf/ElfFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
5 changes: 4 additions & 1 deletion BinaryTools.Elf/ElfProgramHeaderTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace BinaryTools.Elf
{
Expand Down Expand Up @@ -56,7 +57,7 @@ public Int32 Count
/// <param name="header">
/// The ELF header used to extract the metadata about this program header table.
/// </param>
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++)
Expand All @@ -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);
}
}
Expand Down
9 changes: 9 additions & 0 deletions BinaryTools.Elf/ElfSegment.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;

namespace BinaryTools.Elf
{
Expand Down Expand Up @@ -130,5 +131,13 @@ public ElfSegmentFlags Flags
{
get; protected set;
}

/// <summary>
/// Gets a list of sections mapped to this segment.
/// </summary>
public IReadOnlyList<ElfSection> Sections
{
get; internal set;
}
}
}

0 comments on commit 854b845

Please sign in to comment.