diff --git a/src/main.rs b/src/main.rs index ca6a4e5..80c2866 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,13 +2,19 @@ // Author: Garrett Dickinson // Created: 01/23/2023 // Description: Main entrypoint script for chisel. Contains basic procedures -// for gathering ELF file data. - +// for gathering ELF file and program data. use std::path; use std::env; use std::fs; use std::process::exit; +use std::collections::HashMap; + + +// ELF Header Sizes + +const ELF_FILE_HEADER_LENGTH: [u8; 2] = [0x34, 0x40]; + // Generic ELF information offsets. @@ -20,20 +26,20 @@ const ELF_TYPE_OFFSET: u8 = 0x10; // Object type identifier; 2 bytes const ELF_MACHINE_OFFSET: u8 = 0x12; // Instruction set type; 2 bytes -// Entry points and program header table inforamtion. -// Tupled offset are split by architecture: +// Offsets for file header entry points and table inforamtion. +// Arrayed offset are split by architecture: // 0 : x86 // 1 : x86_64 const ELF_ENTRYPOINT_OFFSET: u8 = 0x18; -const ELF_PHOFF_OFFSET: (u8, u8) = (0x1C, 0x20); // Program header table pointer; 2 bytes -const ELF_SHOFF_OFFSET: (u8, u8) = (0x20, 0x28); // Section table pointer; 2 bytes -const ELF_EHSIZE_OFFSET: (u8, u8) = (0x28, 0x34); // Program header table entry size pointer; 2 bytes -const ELF_PHENTSIZE_OFFSET: (u8, u8) = (0x28, 0x34); // Section table pointer; 2 bytes -const ELF_PHNUM_OFFSET: (u8, u8) = (0x2C, 0x38); -const ELF_SHENTSIZE_OFFSET: (u8, u8) = (0x2E, 0x3A); -const ELF_SHNUM_OFFSET: (u8, u8) = (0x30, 0x3C); -const ELF_SHSTRNDX_OFFSET: (u8, u8) = (0x32, 0x3E); +const ELF_PHOFF_OFFSET: [u8; 2] = [0x1C, 0x20]; // Program header table pointer; 2 bytes +const ELF_SHOFF_OFFSET: [u8; 2] = [0x20, 0x28]; // Section table pointer; 2 bytes +const ELF_EHSIZE_OFFSET: [u8; 2] = [0x28, 0x34]; // Program header table entry size pointer; 2 bytes +const ELF_PHENTSIZE_OFFSET: [u8; 2] = [0x28, 0x34]; // Section table pointer; 2 bytes +const ELF_PHNUM_OFFSET: [u8; 2] = [0x2C, 0x38]; // Program header table number of entries pointer; 2 bytes +const ELF_SHENTSIZE_OFFSET: [u8; 2] = [0x2E, 0x3A]; // Size of section header table; 2 bytes +const ELF_SHNUM_OFFSET: [u8; 2] = [0x30, 0x3C]; // Number of entries in section table pointer; 2 bytes +const ELF_SHSTRNDX_OFFSET: [u8; 2] = [0x32, 0x3E]; // Index of section header that contains names; 2 bytes fn main() { @@ -41,7 +47,13 @@ fn main() { let args: Vec = env::args().collect(); // Grab our filepath from our options - let file_path = &args[1]; + if &args.len() < &2 { + // No file given, terminate + println!("[Error] Please provied a file to open..."); + exit(0); + } + + let file_path: &String = &args[1]; if path::Path::new(file_path).exists() { println!("File exists, reading '{}'", file_path); @@ -53,7 +65,16 @@ fn main() { let magic_num: &[u8] = &bytes[0..4]; if magic_num == ELF_MAGIC_NUMBER { - println!("Found ELF Magic Number!"); + println!("Found ELF Magic Number..."); + println!("Parsing File Header..."); + + // Build the File Header data structure + let file_header_map = build_fild_header(bytes); + + for (key, value) in &file_header_map { + println!("{}: {}", key, value); + } + } else { println!("[Error] Could not find magic number, is this an ELF executable?") } @@ -65,3 +86,31 @@ fn main() { return; } + + +fn build_fild_header(data: &Vec) -> HashMap{ + let mut file_header: HashMap = HashMap::new(); + + // Determine x86 or x64 architecture + // 0 : x86 + // 1 : x64 + let arch: u8 = (data[ELF_ARCH_OFFSET as usize] - 1).into(); + + file_header.insert("e_arch".to_string(), data[ELF_ARCH_OFFSET as usize]); + file_header.insert("e_endian".to_string(), data[ELF_ENDIAN_OFFSET as usize]); + file_header.insert("e_abi".to_string(), data[ELF_ABI_OFFSET as usize]); + file_header.insert("e_type".to_string(), data[ELF_TYPE_OFFSET as usize]); + file_header.insert("e_machine".to_string(), data[ELF_MACHINE_OFFSET as usize]); + + file_header.insert("e_entry".to_string(), data[ELF_ENTRYPOINT_OFFSET as usize]); + file_header.insert("e_phoff".to_string(), data[ELF_PHOFF_OFFSET[arch as usize] as usize]); + file_header.insert("e_shoff".to_string(), data[ELF_SHOFF_OFFSET[arch as usize] as usize]); + file_header.insert("e_ehsize".to_string(), data[ELF_EHSIZE_OFFSET[arch as usize] as usize]); + file_header.insert("e_phentsize".to_string(), data[ELF_PHENTSIZE_OFFSET[arch as usize] as usize]); + file_header.insert("e_phnum".to_string(), data[ELF_PHNUM_OFFSET[arch as usize] as usize]); + file_header.insert("e_shentsize".to_string(), data[ELF_SHENTSIZE_OFFSET[arch as usize] as usize]); + file_header.insert("e_shnum".to_string(), data[ELF_SHNUM_OFFSET[arch as usize] as usize]); + file_header.insert("e_shstrndx".to_string(), data[ELF_SHSTRNDX_OFFSET[arch as usize] as usize]); + + return file_header; +} \ No newline at end of file diff --git a/testing/testfile b/testing/flatfile similarity index 100% rename from testing/testfile rename to testing/flatfile diff --git a/testing/hello32 b/testing/hello32 new file mode 100755 index 0000000..12bb70c Binary files /dev/null and b/testing/hello32 differ