From 81d376d7bce4f78b46df70f6c40a5d07a2c49839 Mon Sep 17 00:00:00 2001 From: Filip Jeremic Date: Thu, 19 Dec 2019 18:39:53 -0500 Subject: [PATCH] Add ElfType enum and accompanying test --- BinaryTools.Elf.Tests/TestElfHeader.cs | 10 +++++++ BinaryTools.Elf/Bit32/ElfHeader.cs | 2 +- BinaryTools.Elf/Bit64/ElfHeader.cs | 2 +- BinaryTools.Elf/ElfHeader.cs | 2 +- BinaryTools.Elf/ElfType.cs | 40 ++++++++++++++++++++++++++ 5 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 BinaryTools.Elf/ElfType.cs diff --git a/BinaryTools.Elf.Tests/TestElfHeader.cs b/BinaryTools.Elf.Tests/TestElfHeader.cs index 1dc817b..38aa72f 100644 --- a/BinaryTools.Elf.Tests/TestElfHeader.cs +++ b/BinaryTools.Elf.Tests/TestElfHeader.cs @@ -85,5 +85,15 @@ public void CorrectFlags() Assert.Equal(0U, elfFile.Header.Flags); } + + [Fact] + public void CorrectType() + { + var stream = new FileStream("Binaries/base32", FileMode.Open, FileAccess.Read); + var reader = new EndianBinaryReader(stream, EndianBitConverter.NativeEndianness); + ElfFile elfFile = ElfFile.ReadElfFile(reader); + + Assert.Equal(ElfType.Executable, elfFile.Header.Type); + } } } diff --git a/BinaryTools.Elf/Bit32/ElfHeader.cs b/BinaryTools.Elf/Bit32/ElfHeader.cs index b71de03..c171c5d 100644 --- a/BinaryTools.Elf/Bit32/ElfHeader.cs +++ b/BinaryTools.Elf/Bit32/ElfHeader.cs @@ -72,7 +72,7 @@ internal ElfHeader(BinaryReader reader, Int64 position) reader.BaseStream.Position += 1; // Represents Elf32_Ehdr.e_type - Type = reader.ReadUInt16(); + Type = (ElfType)reader.ReadUInt16(); // Represents Elf32_Ehdr.e_machine Machine = reader.ReadUInt16(); diff --git a/BinaryTools.Elf/Bit64/ElfHeader.cs b/BinaryTools.Elf/Bit64/ElfHeader.cs index 5412b90..249a17d 100644 --- a/BinaryTools.Elf/Bit64/ElfHeader.cs +++ b/BinaryTools.Elf/Bit64/ElfHeader.cs @@ -72,7 +72,7 @@ internal ElfHeader(BinaryReader reader, Int64 position) reader.BaseStream.Position += 1; // Represents Elf64_Ehdr.e_type - Type = reader.ReadUInt16(); + Type = (ElfType)reader.ReadUInt16(); // Represents Elf64_Ehdr.e_machine Machine = reader.ReadUInt16(); diff --git a/BinaryTools.Elf/ElfHeader.cs b/BinaryTools.Elf/ElfHeader.cs index 3ffa4d3..5dea0a1 100644 --- a/BinaryTools.Elf/ElfHeader.cs +++ b/BinaryTools.Elf/ElfHeader.cs @@ -155,7 +155,7 @@ public UInt32 Flags /// /// Gets the ELF file type. /// - public UInt16 Type + public ElfType Type { get; protected set; } diff --git a/BinaryTools.Elf/ElfType.cs b/BinaryTools.Elf/ElfType.cs new file mode 100644 index 0000000..89fa53d --- /dev/null +++ b/BinaryTools.Elf/ElfType.cs @@ -0,0 +1,40 @@ +using System.ComponentModel; + +namespace BinaryTools.Elf +{ + /// + /// Enumerates the object file type. + /// + public enum ElfType : ushort + { + /// + /// No file type + /// + [Description("No file type")] + None, + + /// + /// Relocatable file + /// + [Description("Relocatable file")] + Relocatable, + + /// + /// Executable file + /// + [Description("Executable file")] + Executable, + + /// + /// Shared object file + /// + [Description("Shared object file")] + Shared, + + /// + /// Core file + /// + [Description("Core file")] + Core, + } +}