summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authortzlil <tzlils@protonmail.com>2023-02-21 22:59:26 +0200
committertzlil <tzlils@protonmail.com>2023-02-21 22:59:26 +0200
commite954723194fe8e5faa9522d94a81a17451300ebc (patch)
tree929981a4b2775fb1f4908f3181bfcd4a1fc8dfdd /src
intial commit, added all structs
Diffstat (limited to 'src')
-rw-r--r--src/main.rs25
-rw-r--r--src/structs.rs119
2 files changed, 144 insertions, 0 deletions
diff --git a/src/main.rs b/src/main.rs
new file mode 100644
index 0000000..7a189bf
--- /dev/null
+++ b/src/main.rs
@@ -0,0 +1,25 @@
+mod structs;
+use crate::structs::ElfHeader;
+
+#[repr(C)]
+#[derive(Debug)]
+struct Elf {
+    header: &'static ElfHeader,
+}
+
+impl Elf {
+    pub fn new(bytes: &[u8]) -> Elf {
+        let header = unsafe { &*(bytes.as_ptr() as *const ElfHeader) };
+        assert_eq!(&header.ident.magic, b"\x7fELF");
+        
+        Elf {
+            header
+        }
+    }
+} 
+
+fn main() {
+    let data = include_bytes!("../elf");
+    let elf = Elf::new(&data[..]);
+    println!("{:?}", elf);
+}
diff --git a/src/structs.rs b/src/structs.rs
new file mode 100644
index 0000000..c055350
--- /dev/null
+++ b/src/structs.rs
@@ -0,0 +1,119 @@
+#[repr(C, packed)]
+#[derive(Debug, Copy, Clone)]
+pub struct ElfIdent {
+	/// Magic number - 0x7F, then 'ELF' in ASCII
+	pub magic: [u8; 4],
+	
+	/// 1 = 32 bit, 2 = 64 bit
+	pub class: Class,
+
+	// 1 = little endian, 2 = big endian
+	pub data: Endian,
+
+	/// ELF header version
+	pub version: u8,
+
+	/// OS ABI - usually 0 for System V
+	pub OSABI: OSABI,
+
+	// This field is used to distinguish among incompatible versions of an ABI.
+	pub ABIVersion: u8,
+
+	/// Unused/padding
+	pub _unused: [u8; 7],
+}
+
+// https://man7.org/linux/man-pages/man5/elf.5.html
+// https://wiki.osdev.org/ELF
+// https://en.wikipedia.org/wiki/Executable_and_Linkable_Format
+#[repr(C, packed)]
+#[derive(Debug)]
+pub struct ElfHeader {
+	pub ident: ElfIdent,
+
+	/// 1 = relocatable, 2 = executable, 3 = shared, 4 = core
+	pub r#type: Type,
+	
+	/// Instruction set - see table below
+	pub machine: Machine,
+
+	/// ELF Version
+	pub version: u32,
+
+	/// Program entry position (virtual)
+	pub entry: u64,
+
+	/// Program header table position (bytes into file)
+	pub phoff: u64,
+
+	/// Section header table position (bytes into file)
+	pub shoff: u64,
+
+	/// This member holds processor-specific flags associated with the file
+	/// Currently, no flags have been defined.
+	pub flags: u32,
+
+	/// Header size
+	pub ehsize: u16,
+
+	/// Size of an entry in the program header table
+	pub phentsize: u16,
+
+	/// Number of entries in the program header table
+	pub phnum: u16,
+
+	/// Size of an entry in the section header table
+	pub shentsize: u16,
+
+	/// Number of entries in the section header table
+	pub shnum: u16,
+	
+	/// Index in section header table with the section names
+	pub shstrndx: u16,
+}
+
+#[derive(Debug, Copy, Clone)]
+#[repr(u16)]
+pub enum Type {
+	None = 0,
+	Rel = 1,
+	Exec = 2,
+	Dyn = 3,
+	Core = 4,
+}
+
+#[derive(Debug, Copy, Clone)]
+#[repr(u16)]
+pub enum Machine {
+	None = 1,
+	X86_64 = 62,
+}
+
+
+#[derive(Debug, Copy, Clone)]
+#[repr(u8)]
+pub enum Class {
+	ELF32 = 1,
+	ELF64 = 2,
+}
+
+#[derive(Debug, Copy, Clone)]
+#[repr(u8)]
+pub enum OSABI {
+	None = 0x00,
+	SystemV = 0x01,
+	HPUX = 0x02,
+	Solaris = 0x03,
+	IRIX = 0x04,
+	FreeBSD = 0x05,
+	TRU64 = 0x06,
+	ARM = 0x07,
+	Standalone = 0x08,
+}
+
+#[derive(Debug, Copy, Clone)]
+#[repr(u8)]
+pub enum Endian {
+	Little = 1,
+	Big = 2,
+}
\ No newline at end of file