Skip to content

Commit

Permalink
Add ELFSection tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fjeremic committed Dec 24, 2019
1 parent ed64022 commit 5de9537
Show file tree
Hide file tree
Showing 7 changed files with 378 additions and 133 deletions.
209 changes: 209 additions & 0 deletions BinaryTools.Elf.Tests/TestElfSection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
using BinaryTools.Elf.Io;
using System.IO;
using Xunit;

namespace BinaryTools.Elf.Tests
{
public class TestElfSection
{
[Fact]
public void TestCount()
{
var stream = new FileStream("Binaries/base32", FileMode.Open, FileAccess.Read);
var reader = new EndianBinaryReader(stream, EndianBitConverter.NativeEndianness);
ElfFile elfFile = ElfFile.ReadElfFile(reader);

Assert.Equal(29, elfFile.Sections.Count);
}

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

Assert.Equal("", elfFile.Sections[0].Name);
Assert.Equal(".dynsym", elfFile.Sections[5].Name);
Assert.Equal(".plt.got", elfFile.Sections[13].Name);
Assert.Equal(".dynamic", elfFile.Sections[22].Name);
Assert.Equal(".got.plt", elfFile.Sections[24].Name);
}

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

Assert.Equal(ElfSectionType.Null, elfFile.Sections[0].Type);
Assert.Equal(ElfSectionType.ProgBits, elfFile.Sections[1].Type);
Assert.Equal(ElfSectionType.Note, elfFile.Sections[2].Type);
Assert.Equal(ElfSectionType.DynSym, elfFile.Sections[5].Type);
Assert.Equal(ElfSectionType.StrTab, elfFile.Sections[6].Type);
Assert.Equal(ElfSectionType.RelA, elfFile.Sections[9].Type);
Assert.Equal(ElfSectionType.InitArray, elfFile.Sections[19].Type);
Assert.Equal(ElfSectionType.FiniArray, elfFile.Sections[20].Type);
Assert.Equal(ElfSectionType.Dynamic, elfFile.Sections[22].Type);
Assert.Equal(ElfSectionType.StrTab, elfFile.Sections[28].Type);
}

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

Assert.Equal(0x0000000000000000UL, elfFile.Sections[0].Address);
Assert.Equal(0x0000000000400238UL, elfFile.Sections[1].Address);
Assert.Equal(0x0000000000400254UL, elfFile.Sections[2].Address);
Assert.Equal(0x00000000004002e0UL, elfFile.Sections[5].Address);
Assert.Equal(0x0000000000400958UL, elfFile.Sections[6].Address);
Assert.Equal(0x0000000000400d28UL, elfFile.Sections[9].Address);
Assert.Equal(0x0000000000608e10UL, elfFile.Sections[19].Address);
Assert.Equal(0x0000000000608e18UL, elfFile.Sections[20].Address);
Assert.Equal(0x0000000000608e28UL, elfFile.Sections[22].Address);
Assert.Equal(0x0000000000000000UL, elfFile.Sections[28].Address);
}

[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(0x0000000000000000UL, elfFile.Sections[0].Offset);
Assert.Equal(0x0000000000000238UL, elfFile.Sections[1].Offset);
Assert.Equal(0x0000000000000254UL, elfFile.Sections[2].Offset);
Assert.Equal(0x00000000000002e0UL, elfFile.Sections[5].Offset);
Assert.Equal(0x0000000000000958UL, elfFile.Sections[6].Offset);
Assert.Equal(0x0000000000000d28UL, elfFile.Sections[9].Offset);
Assert.Equal(0x0000000000008e10UL, elfFile.Sections[19].Offset);
Assert.Equal(0x0000000000008e18UL, elfFile.Sections[20].Offset);
Assert.Equal(0x0000000000008e28UL, elfFile.Sections[22].Offset);
Assert.Equal(0x00000000000092a8UL, elfFile.Sections[28].Offset);
}

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

Assert.Equal(0x0000000000000000UL, elfFile.Sections[0].Size);
Assert.Equal(0x000000000000001cUL, elfFile.Sections[1].Size);
Assert.Equal(0x0000000000000020UL, elfFile.Sections[2].Size);
Assert.Equal(0x0000000000000678UL, elfFile.Sections[5].Size);
Assert.Equal(0x00000000000002e0UL, elfFile.Sections[6].Size);
Assert.Equal(0x00000000000000c0UL, elfFile.Sections[9].Size);
Assert.Equal(0x0000000000000008UL, elfFile.Sections[19].Size);
Assert.Equal(0x0000000000000008UL, elfFile.Sections[20].Size);
Assert.Equal(0x00000000000001d0UL, elfFile.Sections[22].Size);
Assert.Equal(0x0000000000000102UL, elfFile.Sections[28].Size);
}

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

Assert.Equal(0x0000000000000000UL, elfFile.Sections[0].EntrySize);
Assert.Equal(0x0000000000000000UL, elfFile.Sections[1].EntrySize);
Assert.Equal(0x0000000000000000UL, elfFile.Sections[2].EntrySize);
Assert.Equal(0x0000000000000018UL, elfFile.Sections[5].EntrySize);
Assert.Equal(0x0000000000000000UL, elfFile.Sections[6].EntrySize);
Assert.Equal(0x0000000000000018UL, elfFile.Sections[9].EntrySize);
Assert.Equal(0x0000000000000000UL, elfFile.Sections[19].EntrySize);
Assert.Equal(0x0000000000000000UL, elfFile.Sections[20].EntrySize);
Assert.Equal(0x0000000000000010UL, elfFile.Sections[22].EntrySize);
Assert.Equal(0x0000000000000000UL, elfFile.Sections[28].EntrySize);
}

[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(ElfSectionFlags.None, elfFile.Sections[0].Flags);
Assert.Equal(ElfSectionFlags.Alloc, elfFile.Sections[1].Flags);
Assert.Equal(ElfSectionFlags.Alloc, elfFile.Sections[2].Flags);
Assert.Equal(ElfSectionFlags.Alloc, elfFile.Sections[5].Flags);
Assert.Equal(ElfSectionFlags.Alloc, elfFile.Sections[6].Flags);
Assert.Equal(ElfSectionFlags.Alloc, elfFile.Sections[9].Flags);
Assert.Equal(ElfSectionFlags.Alloc | ElfSectionFlags.InfoLink, elfFile.Sections[10].Flags);
Assert.Equal(ElfSectionFlags.Alloc | ElfSectionFlags.Exec, elfFile.Sections[12].Flags);
Assert.Equal(ElfSectionFlags.Alloc | ElfSectionFlags.Write, elfFile.Sections[19].Flags);
Assert.Equal(ElfSectionFlags.Alloc | ElfSectionFlags.Write, elfFile.Sections[20].Flags);
Assert.Equal(ElfSectionFlags.Alloc | ElfSectionFlags.Write, elfFile.Sections[22].Flags);
Assert.Equal(ElfSectionFlags.None, elfFile.Sections[28].Flags);
}

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

Assert.Equal(0U, elfFile.Sections[0].Link);
Assert.Equal(0U, elfFile.Sections[1].Link);
Assert.Equal(0U, elfFile.Sections[2].Link);
Assert.Equal(6U, elfFile.Sections[5].Link);
Assert.Equal(0U, elfFile.Sections[6].Link);
Assert.Equal(5U, elfFile.Sections[9].Link);
Assert.Equal(0U, elfFile.Sections[19].Link);
Assert.Equal(0U, elfFile.Sections[20].Link);
Assert.Equal(6U, elfFile.Sections[22].Link);
Assert.Equal(0U, elfFile.Sections[28].Link);
}

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

Assert.Equal(0U, elfFile.Sections[0].Info);
Assert.Equal(0U, elfFile.Sections[1].Info);
Assert.Equal(0U, elfFile.Sections[2].Info);
Assert.Equal(1U, elfFile.Sections[5].Info);
Assert.Equal(0U, elfFile.Sections[6].Info);
Assert.Equal(0U, elfFile.Sections[9].Info);
Assert.Equal(24U, elfFile.Sections[10].Info);
Assert.Equal(0U, elfFile.Sections[19].Info);
Assert.Equal(0U, elfFile.Sections[20].Info);
Assert.Equal(0U, elfFile.Sections[22].Info);
Assert.Equal(0U, elfFile.Sections[28].Info);
}

[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(0UL, elfFile.Sections[0].Alignment);
Assert.Equal(1UL, elfFile.Sections[1].Alignment);
Assert.Equal(4UL, elfFile.Sections[2].Alignment);
Assert.Equal(8UL, elfFile.Sections[5].Alignment);
Assert.Equal(1UL, elfFile.Sections[6].Alignment);
Assert.Equal(8UL, elfFile.Sections[9].Alignment);
Assert.Equal(8UL, elfFile.Sections[10].Alignment);
Assert.Equal(8UL, elfFile.Sections[19].Alignment);
Assert.Equal(8UL, elfFile.Sections[20].Alignment);
Assert.Equal(8UL, elfFile.Sections[22].Alignment);
Assert.Equal(32UL, elfFile.Sections[25].Alignment);
Assert.Equal(1UL, elfFile.Sections[28].Alignment);
}
}
}
4 changes: 2 additions & 2 deletions BinaryTools.Elf/Bit32/ElfSection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ internal ElfSection(BinaryReader reader, Int64 position)
NameOffset = reader.ReadUInt32();

// Represents Elf32_Shdr.sh_type
Type = reader.ReadUInt32();
Type = (ElfSectionType)reader.ReadUInt32();

// Represents Elf32_Shdr.sh_flags
Flags = reader.ReadUInt32();
Flags = (ElfSectionFlags)reader.ReadUInt32();

// Represents Elf32_Shdr.sh_addr
Address = reader.ReadUInt32();
Expand Down
4 changes: 2 additions & 2 deletions BinaryTools.Elf/Bit64/ElfSection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ internal ElfSection(BinaryReader reader, Int64 position)
NameOffset = reader.ReadUInt32();

// Represents Elf64_Shdr.sh_type
Type = reader.ReadUInt32();
Type = (ElfSectionType)reader.ReadUInt32();

// Represents Elf64_Shdr.sh_flags
Flags = reader.ReadUInt64();
Flags = (ElfSectionFlags)reader.ReadUInt64();

// Represents Elf64_Shdr.sh_addr
Address = reader.ReadUInt64();
Expand Down
129 changes: 2 additions & 127 deletions BinaryTools.Elf/ElfSection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,131 +7,6 @@ namespace BinaryTools.Elf
/// </summary>
public abstract class ElfSection
{
/// <summary>
/// Gets the index value of a section representing an undefined section.
/// </summary>
public const UInt16 SHN_UNDEF = 0x0000;

/// <summary>
/// Gets the index value of a section representing the lower bound of the range of reserved indexes.
/// </summary>
public const UInt16 SHN_LORESERVE = 0xFF00;

/// <summary>
/// Gets the index value of a section representing the upper bound of the range of reserved indexes.
/// </summary>
public const UInt16 SHN_HIRESERVE = 0xFFFF;

/// <summary>
/// Gets the index value of a section representing the lower bound of the indices reserved for processor specific semantics.
/// </summary>
public const UInt16 SHN_LOPROC = 0xFF00;

/// <summary>
/// Gets the index value of a section representing the upper bound of the indices reserved for processor specific semantics.
/// </summary>
public const UInt16 SHN_HIPROC = 0xFF1F;

/// <summary>
/// Gets the index value of a section representing the absolute values for the corresponding reference.
/// </summary>
public const UInt16 SHN_ABS = 0xFFF1;

/// <summary>
/// Gets the index value of a section representing that the symbols are defined relative to this section are common symbols.
/// </summary>
public const UInt16 SHN_COMMON = 0xFFF2;

/// <summary>
/// Gets the type value of a section representing an inactive section.
/// </summary>
public const UInt32 SHT_NULL = 0x00000000;

/// <summary>
/// Gets the type value of a section representing information defined by the program.
/// </summary>
public const UInt32 SHT_PROGBITS = 0x00000001;

/// <summary>
/// Gets the type value of a section representing a symbol table.
/// </summary>
public const UInt32 SHT_SYMTAB = 0x00000002;

/// <summary>
/// Gets the type value of a section representing a string table.
/// </summary>
public const UInt32 SHT_STRTAB = 0x00000003;

/// <summary>
/// Gets the type value of a section representing relIBM.OMR.CoreAnalyzer.ion entries.
/// </summary>
public const UInt32 SHT_RELA = 0x00000004;

/// <summary>
/// Gets the type value of a section representing a symbol hash table.
/// </summary>
public const UInt32 SHT_HASH = 0x00000005;

/// <summary>
/// Gets the type value of a section representing dynamic linking information.
/// </summary>
public const UInt32 SHT_DYN = 0x00000006;

/// <summary>
/// Gets the type value of a section representing auxiliary information.
/// </summary>
public const UInt32 SHT_NOTE = 0x00000007;

/// <summary>
/// Gets the type value of a section representing that the section occupies no space in the file.
/// </summary>
public const UInt32 SHT_NOBITS = 0x00000008;

/// <summary>
/// Gets the type value of a section representing relIBM.OMR.CoreAnalyzer.ion entires.
/// </summary>
public const UInt32 SHT_REL = 0x00000009;

/// <summary>
/// Gets the type value of a section representing unspecified semantics.
/// </summary>
public const UInt32 SHT_SHLIB = 0x0000000A;

/// <summary>
/// Gets the type value of a section representing a symbol table.
/// </summary>
public const UInt32 SHT_DYNSYM = 0x0000000B;

/// <summary>
/// Gets the type value of a section representing the lower bound of a section holding OS specific semantics.
/// </summary>
public const UInt32 SHT_LOOS = 0x60000000;

/// <summary>
/// Gets the type value of a section representing the upper bound of a section holding OS specific semantics.
/// </summary>
public const UInt32 SHT_HIOS = 0x6FFFFFFF;

/// <summary>
/// Gets the type value of a section representing the lower bound of a section holding processor specific semantics.
/// </summary>
public const UInt32 SHT_LOPROC = 0x70000000;

/// <summary>
/// Gets the type value of a section representing the upper bound of a section holding processor specific semantics.
/// </summary>
public const UInt32 SHT_HIPROC = 0x7FFFFFFF;

/// <summary>
/// Gets the type value of a section representing the lower bound of a section holding application specific semantics.
/// </summary>
public const UInt32 SHT_LOUSER = 0x80000000;

/// <summary>
/// Gets the type value of a section representing the upper bound of a section holding application specific semantics.
/// </summary>
public const UInt32 SHT_HIUSER = 0xFFFFFFFF;

/// <summary>
/// Gets the name of this section.
/// </summary>
Expand Down Expand Up @@ -191,15 +66,15 @@ public UInt64 Alignment
/// <summary>
/// Gets the type of this section.
/// </summary>
public UInt32 Type
public ElfSectionType Type
{
get; protected set;
}

/// <summary>
/// Gets the flags of this section.
/// </summary>
public UInt64 Flags
public ElfSectionFlags Flags
{
get; protected set;
}
Expand Down
Loading

0 comments on commit 5de9537

Please sign in to comment.