From 1fd9d5fc4e97ffdc073dfa30b5dbeab1c207ae6c Mon Sep 17 00:00:00 2001 From: Filip Jeremic Date: Wed, 15 Jan 2020 19:28:33 -0500 Subject: [PATCH] Add ElfSegmentType enum and accompanying tests --- BinaryTools.Elf.Tests/TestElfSegment.cs | 34 ++++++++++++++++++ BinaryTools.Elf/Bit32/ElfSegment.cs | 2 +- BinaryTools.Elf/Bit64/ElfSegment.cs | 2 +- BinaryTools.Elf/ElfSegment.cs | 2 +- BinaryTools.Elf/ElfSegmentType.cs | 48 +++++++++++++++++++++++++ 5 files changed, 85 insertions(+), 3 deletions(-) create mode 100644 BinaryTools.Elf.Tests/TestElfSegment.cs create mode 100644 BinaryTools.Elf/ElfSegmentType.cs diff --git a/BinaryTools.Elf.Tests/TestElfSegment.cs b/BinaryTools.Elf.Tests/TestElfSegment.cs new file mode 100644 index 0000000..1cc13f3 --- /dev/null +++ b/BinaryTools.Elf.Tests/TestElfSegment.cs @@ -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); + } + } +} diff --git a/BinaryTools.Elf/Bit32/ElfSegment.cs b/BinaryTools.Elf/Bit32/ElfSegment.cs index 0f155c0..0be0621 100644 --- a/BinaryTools.Elf/Bit32/ElfSegment.cs +++ b/BinaryTools.Elf/Bit32/ElfSegment.cs @@ -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(); diff --git a/BinaryTools.Elf/Bit64/ElfSegment.cs b/BinaryTools.Elf/Bit64/ElfSegment.cs index 99c5bc8..aa33a28 100644 --- a/BinaryTools.Elf/Bit64/ElfSegment.cs +++ b/BinaryTools.Elf/Bit64/ElfSegment.cs @@ -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(); diff --git a/BinaryTools.Elf/ElfSegment.cs b/BinaryTools.Elf/ElfSegment.cs index 17028f9..c88f16c 100644 --- a/BinaryTools.Elf/ElfSegment.cs +++ b/BinaryTools.Elf/ElfSegment.cs @@ -118,7 +118,7 @@ public UInt64 Alignment /// /// Gets the type of this segment. /// - public UInt32 Type + public ElfSegmentType Type { get; protected set; } diff --git a/BinaryTools.Elf/ElfSegmentType.cs b/BinaryTools.Elf/ElfSegmentType.cs new file mode 100644 index 0000000..3ab9274 --- /dev/null +++ b/BinaryTools.Elf/ElfSegmentType.cs @@ -0,0 +1,48 @@ +namespace BinaryTools.Elf +{ + /// + /// Enumerates the ELF section's contents and semantics. + /// + public enum ElfSegmentType : uint + { + /// + /// The array element is unused; other members' values are undefined. This type lets the program header table have ignored entries. + /// + Null = 0, + + /// + /// 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. + /// + Load = 1, + + /// + /// The array element specifies dynamic linking information. + /// + Dynamic = 2, + + /// + /// 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. + /// + Interp = 3, + + /// + /// The array element specifies the location and size of auxiliary information. + /// + Note = 4, + + /// + /// This segment type is reserved but has unspecified semantics. Programs that contain an array element of this type do not conform to the ABI. + /// + ShLib = 5, + + /// + /// 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. + /// + PHdr = 6, + + /// + /// The array element specifies the Thread-Local Storage template. Implementations need not support this program table entry. + /// + TLS = 7, + } +}