From e446d06c82d3f475076862f162b5da7b3add2bff Mon Sep 17 00:00:00 2001 From: Garrett Dickinson Date: Mon, 30 Jan 2023 01:10:35 -0600 Subject: [PATCH] Add file header parsing for x86 32 and 64 bit archs --- src/main.rs | 77 +++++++++++++++++++++++++++------ testing/{testfile => flatfile} | 0 testing/hello32 | Bin 0 -> 14940 bytes 3 files changed, 63 insertions(+), 14 deletions(-) rename testing/{testfile => flatfile} (100%) create mode 100755 testing/hello32 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 0000000000000000000000000000000000000000..12bb70c14922467944a46eeb86b2d86d00094af8 GIT binary patch literal 14940 zcmeHOYit}>6~4RPB&IR;+6}GSyr!wurqE2*j)?+A?fM-T$B7?FTa|V)_Kv+v_hEKt zVI4&@?zW=FHMlLRcncH|;Rhm8B7_8pCYzQ5{n3{Ga77V;ChA7GG*tt#h}GfyX6D*s z*QnxeHOD%0&bjA1_ue`8+ShmY-iJGSIs<`#kW?Wm1g){K5Y3RUe$w$Y3q`CK+r?(F zNvuT?eUc9&fE+>@xdQwR*pjb>O{NdUHbX4Zs|X;|FVHSAl#=IBKwjgP+kORx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