Skip to content

Commit

Permalink
Add ELFSection tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fjeremic committed Jan 16, 2020
1 parent 1fd9d5f commit 34e5dc0
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 4 deletions.
106 changes: 106 additions & 0 deletions BinaryTools.Elf.Tests/TestElfSegment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,111 @@ public void TestType()
Assert.Equal(ElfSegmentType.Dynamic, elfFile.Segments[4].Type);
Assert.Equal(ElfSegmentType.Note, elfFile.Segments[5].Type);
}


[Fact]
public void TestOffset()
{
var stream = new FileStream("Binaries/base32", FileMode.Open, FileAccess.Read);
var reader = new EndianBinaryReader(stream, EndianBitConverter.NativeEndianness);
ElfFile elfFile = ElfFile.ReadElfFile(reader);

Assert.Equal(0x0000000000000040UL, elfFile.Segments[0].Offset);
Assert.Equal(0x0000000000000238UL, elfFile.Segments[1].Offset);
Assert.Equal(0x0000000000000000UL, elfFile.Segments[2].Offset);
Assert.Equal(0x0000000000008E10UL, elfFile.Segments[3].Offset);
Assert.Equal(0x0000000000008E28UL, elfFile.Segments[4].Offset);
Assert.Equal(0x0000000000000254UL, elfFile.Segments[5].Offset);
}

[Fact]
public void TestVirtualAddress()
{
var stream = new FileStream("Binaries/base32", FileMode.Open, FileAccess.Read);
var reader = new EndianBinaryReader(stream, EndianBitConverter.NativeEndianness);
ElfFile elfFile = ElfFile.ReadElfFile(reader);

Assert.Equal(0x0000000000400040UL, elfFile.Segments[0].VirtualAddress);
Assert.Equal(0x0000000000400238UL, elfFile.Segments[1].VirtualAddress);
Assert.Equal(0x0000000000400000UL, elfFile.Segments[2].VirtualAddress);
Assert.Equal(0x0000000000608e10UL, elfFile.Segments[3].VirtualAddress);
Assert.Equal(0x0000000000608e28UL, elfFile.Segments[4].VirtualAddress);
Assert.Equal(0x0000000000400254UL, elfFile.Segments[5].VirtualAddress);
}

[Fact]
public void TestPhysicalAddress()
{
var stream = new FileStream("Binaries/base32", FileMode.Open, FileAccess.Read);
var reader = new EndianBinaryReader(stream, EndianBitConverter.NativeEndianness);
ElfFile elfFile = ElfFile.ReadElfFile(reader);

Assert.Equal(0x0000000000400040UL, elfFile.Segments[0].VirtualAddress);
Assert.Equal(0x0000000000400238UL, elfFile.Segments[1].VirtualAddress);
Assert.Equal(0x0000000000400000UL, elfFile.Segments[2].VirtualAddress);
Assert.Equal(0x0000000000608e10UL, elfFile.Segments[3].VirtualAddress);
Assert.Equal(0x0000000000608e28UL, elfFile.Segments[4].VirtualAddress);
Assert.Equal(0x0000000000400254UL, elfFile.Segments[5].VirtualAddress);
}

[Fact]
public void TestFileSize()
{
var stream = new FileStream("Binaries/base32", FileMode.Open, FileAccess.Read);
var reader = new EndianBinaryReader(stream, EndianBitConverter.NativeEndianness);
ElfFile elfFile = ElfFile.ReadElfFile(reader);

Assert.Equal(0x00000000000001F8UL, elfFile.Segments[0].FileSize);
Assert.Equal(0x000000000000001CUL, elfFile.Segments[1].FileSize);
Assert.Equal(0x0000000000008164UL, elfFile.Segments[2].FileSize);
Assert.Equal(0x0000000000000464UL, elfFile.Segments[3].FileSize);
Assert.Equal(0x00000000000001D0UL, elfFile.Segments[4].FileSize);
Assert.Equal(0x0000000000000044UL, elfFile.Segments[5].FileSize);
}

[Fact]
public void TestMemorySize()
{
var stream = new FileStream("Binaries/base32", FileMode.Open, FileAccess.Read);
var reader = new EndianBinaryReader(stream, EndianBitConverter.NativeEndianness);
ElfFile elfFile = ElfFile.ReadElfFile(reader);

Assert.Equal(0x00000000000001F8UL, elfFile.Segments[0].MemorySize);
Assert.Equal(0x000000000000001CUL, elfFile.Segments[1].MemorySize);
Assert.Equal(0x0000000000008164UL, elfFile.Segments[2].MemorySize);
Assert.Equal(0x0000000000000630UL, elfFile.Segments[3].MemorySize);
Assert.Equal(0x00000000000001D0UL, elfFile.Segments[4].MemorySize);
Assert.Equal(0x0000000000000044UL, elfFile.Segments[5].MemorySize);
}

[Fact]
public void TestFlags()
{
var stream = new FileStream("Binaries/base32", FileMode.Open, FileAccess.Read);
var reader = new EndianBinaryReader(stream, EndianBitConverter.NativeEndianness);
ElfFile elfFile = ElfFile.ReadElfFile(reader);

Assert.Equal(ElfSegmentFlags.Read | ElfSegmentFlags.Exec, elfFile.Segments[0].Flags);
Assert.Equal(ElfSegmentFlags.Read, elfFile.Segments[1].Flags);
Assert.Equal(ElfSegmentFlags.Read | ElfSegmentFlags.Exec, elfFile.Segments[2].Flags);
Assert.Equal(ElfSegmentFlags.Read | ElfSegmentFlags.Write, elfFile.Segments[3].Flags);
Assert.Equal(ElfSegmentFlags.Read | ElfSegmentFlags.Write, elfFile.Segments[4].Flags);
Assert.Equal(ElfSegmentFlags.Read, elfFile.Segments[5].Flags);
}

[Fact]
public void TestAlignment()
{
var stream = new FileStream("Binaries/base32", FileMode.Open, FileAccess.Read);
var reader = new EndianBinaryReader(stream, EndianBitConverter.NativeEndianness);
ElfFile elfFile = ElfFile.ReadElfFile(reader);

Assert.Equal(0x8UL, elfFile.Segments[0].Alignment);
Assert.Equal(0x1UL, elfFile.Segments[1].Alignment);
Assert.Equal(0x200000UL, elfFile.Segments[2].Alignment);
Assert.Equal(0x200000UL, elfFile.Segments[3].Alignment);
Assert.Equal(0x8UL, elfFile.Segments[4].Alignment);
Assert.Equal(0x4UL, elfFile.Segments[5].Alignment);
}
}
}
2 changes: 1 addition & 1 deletion BinaryTools.Elf/Bit32/ElfSegment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ internal ElfSegment(BinaryReader reader, Int64 position)
MemorySize = reader.ReadUInt32();

// Represents Elf32_Phdr.p_flags
Flags = reader.ReadUInt32();
Flags = (ElfSegmentFlags)reader.ReadUInt32();

// Represents Elf32_Phdr.p_align
Alignment = reader.ReadUInt32();
Expand Down
2 changes: 1 addition & 1 deletion BinaryTools.Elf/Bit64/ElfSegment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ internal ElfSegment(BinaryReader reader, Int64 position)
Type = (ElfSegmentType)reader.ReadUInt32();

// Represents Elf32_Phdr.p_flags
Flags = reader.ReadUInt32();
Flags = (ElfSegmentFlags)reader.ReadUInt32();

// Represents Elf32_Phdr.p_offset
Offset = reader.ReadUInt64();
Expand Down
2 changes: 1 addition & 1 deletion BinaryTools.Elf/ElfSegment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public ElfSegmentType Type
/// <summary>
/// Gets the flags of this segment.
/// </summary>
public UInt32 Flags
public ElfSegmentFlags Flags
{
get; protected set;
}
Expand Down
26 changes: 26 additions & 0 deletions BinaryTools.Elf/ElfSegmentFlags.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;

namespace BinaryTools.Elf
{
/// <summary>
/// Enumerates the ELF section's contents and semantics.
/// </summary>
[Flags]
public enum ElfSegmentFlags : uint
{
/// <summary>
/// The segement is executable.
/// </summary>
Exec = 0x1,

/// <summary>
/// The segement is writeable.
/// </summary>
Write = 0x2,

/// <summary>
/// The segement is readable.
/// </summary>
Read = 0x4,
}
}
2 changes: 1 addition & 1 deletion BinaryTools.Elf/ElfSegmentType.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace BinaryTools.Elf
{
/// <summary>
/// Enumerates the ELF section's contents and semantics.
/// Enumerates the ELF segment's contents and semantics.
/// </summary>
public enum ElfSegmentType : uint
{
Expand Down

0 comments on commit 34e5dc0

Please sign in to comment.