Skip to content

Commit

Permalink
Add ElfSegmentType enum and accompanying tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fjeremic committed Jan 16, 2020
1 parent 5de9537 commit 1fd9d5f
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 3 deletions.
34 changes: 34 additions & 0 deletions BinaryTools.Elf.Tests/TestElfSegment.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using BinaryTools.Elf.Io;
using System.IO;
using Xunit;

namespace BinaryTools.Elf.Tests
{
public class TestElfSegment
{
[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(9, elfFile.Segments.Count);
}

[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(ElfSegmentType.PHdr, elfFile.Segments[0].Type);
Assert.Equal(ElfSegmentType.Interp, elfFile.Segments[1].Type);
Assert.Equal(ElfSegmentType.Load, elfFile.Segments[2].Type);
Assert.Equal(ElfSegmentType.Load, elfFile.Segments[3].Type);
Assert.Equal(ElfSegmentType.Dynamic, elfFile.Segments[4].Type);
Assert.Equal(ElfSegmentType.Note, elfFile.Segments[5].Type);
}
}
}
2 changes: 1 addition & 1 deletion BinaryTools.Elf/Bit32/ElfSegment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ internal ElfSegment(BinaryReader reader, Int64 position)
reader.BaseStream.Position = position;

// Represents Elf32_Phdr.p_type
Type = reader.ReadUInt32();
Type = (ElfSegmentType)reader.ReadUInt32();

// Represents Elf32_Phdr.p_offset
Offset = 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 @@ -24,7 +24,7 @@ internal ElfSegment(BinaryReader reader, Int64 position)
reader.BaseStream.Position = position;

// Represents Elf32_Phdr.p_type
Type = reader.ReadUInt32();
Type = (ElfSegmentType)reader.ReadUInt32();

// Represents Elf32_Phdr.p_flags
Flags = reader.ReadUInt32();
Expand Down
2 changes: 1 addition & 1 deletion BinaryTools.Elf/ElfSegment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public UInt64 Alignment
/// <summary>
/// Gets the type of this segment.
/// </summary>
public UInt32 Type
public ElfSegmentType Type
{
get; protected set;
}
Expand Down
48 changes: 48 additions & 0 deletions BinaryTools.Elf/ElfSegmentType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
namespace BinaryTools.Elf
{
/// <summary>
/// Enumerates the ELF section's contents and semantics.
/// </summary>
public enum ElfSegmentType : uint
{
/// <summary>
/// The array element is unused; other members' values are undefined. This type lets the program header table have ignored entries.
/// </summary>
Null = 0,

/// <summary>
/// The array element specifies a loadable segment, described by p_filesz and p_memsz. The bytes from the file are mapped to the beginning of the memory segment. If the segment's memory size (p_memsz) is larger than the file size (p_filesz), the "extra" bytes are defined to hold the value 0 and to follow the segment's initialized area. The file size may not be larger than the memory size. Loadable segment entries in the program header table appear in ascending order, sorted on the p_vaddr member.
/// </summary>
Load = 1,

/// <summary>
/// The array element specifies dynamic linking information.
/// </summary>
Dynamic = 2,

/// <summary>
/// The array element specifies the location and size of a null-terminated path name to invoke as an interpreter. This segment type is meaningful only for executable files (though it may occur for shared objects); it may not occur more than once in a file. If it is present, it must precede any loadable segment entry.
/// </summary>
Interp = 3,

/// <summary>
/// The array element specifies the location and size of auxiliary information.
/// </summary>
Note = 4,

/// <summary>
/// This segment type is reserved but has unspecified semantics. Programs that contain an array element of this type do not conform to the ABI.
/// </summary>
ShLib = 5,

/// <summary>
/// The array element, if present, specifies the location and size of the program header table itself, both in the file and in the memory image of the program. This segment type may not occur more than once in a file. Moreover, it may occur only if the program header table is part of the memory image of the program. If it is present, it must precede any loadable segment entry.
/// </summary>
PHdr = 6,

/// <summary>
/// The array element specifies the Thread-Local Storage template. Implementations need not support this program table entry.
/// </summary>
TLS = 7,
}
}

0 comments on commit 1fd9d5f

Please sign in to comment.