From 06a254df2fd3f565365fd6381d825ce578474aa8 Mon Sep 17 00:00:00 2001 From: mii Date: Thu, 25 Nov 2021 08:07:47 +0900 Subject: [PATCH] init --- .gitignore | 1 + .vscode/launch.json | 45 ++++++++++++++++++++++++++++++++++++++++++++ Cargo.lock | 39 ++++++++++++++++++++++++++++++++++++++ Cargo.toml | 9 +++++++++ src/elf_parser.rs | 16 ++++++++++++++++ src/elf_struct.rs | 17 +++++++++++++++++ src/main.rs | 27 ++++++++++++++++++++++++++ test.elf | Bin 0 -> 6320 bytes 8 files changed, 154 insertions(+) create mode 100644 .gitignore create mode 100644 .vscode/launch.json create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/elf_parser.rs create mode 100644 src/elf_struct.rs create mode 100644 src/main.rs create mode 100644 test.elf diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..98c8e23 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,45 @@ +{ + // IntelliSense を使用して利用可能な属性を学べます。 + // 既存の属性の説明をホバーして表示します。 + // 詳細情報は次を確認してください: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "lldb", + "request": "launch", + "name": "Debug executable 'elf-parser'", + "cargo": { + "args": [ + "build", + "--bin=elf-parser", + "--package=elf-parser" + ], + "filter": { + "name": "elf-parser", + "kind": "bin" + } + }, + "args": [], + "cwd": "${workspaceFolder}" + }, + { + "type": "lldb", + "request": "launch", + "name": "Debug unit tests in executable 'elf-parser'", + "cargo": { + "args": [ + "test", + "--no-run", + "--bin=elf-parser", + "--package=elf-parser" + ], + "filter": { + "name": "elf-parser", + "kind": "bin" + } + }, + "args": [], + "cwd": "${workspaceFolder}" + } + ] +} \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..a7bacf9 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,39 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "elf-parser" +version = "0.1.0" +dependencies = [ + "nom", +] + +[[package]] +name = "memchr" +version = "2.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a" + +[[package]] +name = "minimal-lexical" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" + +[[package]] +name = "nom" +version = "7.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b1d11e1ef389c76fe5b81bcaf2ea32cf88b62bc494e19f493d0b30e7a930109" +dependencies = [ + "memchr", + "minimal-lexical", + "version_check", +] + +[[package]] +name = "version_check" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5fecdca9a5291cc2b8dcf7dc02453fee791a280f3743cb0905f8822ae463b3fe" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..b12c330 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "elf-parser" +version = "0.1.0" +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +nom = "7" \ No newline at end of file diff --git a/src/elf_parser.rs b/src/elf_parser.rs new file mode 100644 index 0000000..838d5d8 --- /dev/null +++ b/src/elf_parser.rs @@ -0,0 +1,16 @@ +use nom::{IResult, bytes::complete::tag, number::complete::{le_u16, le_u32, le_u64}}; + +use crate::elf_struct::ElfHeader; + +pub struct ElfParser { + pub file: Vec +} + +impl ElfParser { + pub fn parse_header(input: &[u8]) -> IResult<&[u8], u64> { + let (input, ident) = le_u64(input)?; + let (input, elf_type) = le_u16(input)?; + let (input, version) = le_u32(input)?; + Ok((input, ident)) + } +} \ No newline at end of file diff --git a/src/elf_struct.rs b/src/elf_struct.rs new file mode 100644 index 0000000..1170f76 --- /dev/null +++ b/src/elf_struct.rs @@ -0,0 +1,17 @@ +#[derive(Debug)] +pub struct ElfHeader { + pub magic_number: u64, + pub elf_type: u16, + pub machine: u16, + pub version: u32, + pub entry: u64, + pub program_header_offset: u64, + pub section_header_offset: u64, + pub flags: u32, + pub elf_header_size: u16, + pub program_header_entry_size: u16, + pub program_header_num: u16, + pub section_header_entry_size: u16, + pub section_header_num: u16, + pub section_header_name_index: u16 +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..b68c1cc --- /dev/null +++ b/src/main.rs @@ -0,0 +1,27 @@ +mod elf_struct; +mod elf_parser; +use std::io::Read; + +use crate::elf_parser::ElfParser; + +fn main() { + println!("Hello, world!"); + let mut file = std::fs::File::open("./test.elf").expect("Failed to open a file."); + let mut buf = Vec::new(); + file.read_to_end(&mut buf).expect("Failed to read a file."); + let mut elf_parser = ElfParser { file: buf }; + + match ElfParser::parse_header(&elf_parser.file) { + Ok(input) => { + println!("{:?}", input.0[0]); + println!("{:?}", input.0[1]); + println!("{:?}", input.0[2]); + println!("{:?}", input.0[3]); + println!("{:?}", input.1); + }, + Err(_) => { + println!("Error."); + } + } +} +// 282584257676671 \ No newline at end of file diff --git a/test.elf b/test.elf new file mode 100644 index 0000000000000000000000000000000000000000..899bda14f02b036c2be81750bc6e959bf783743a GIT binary patch literal 6320 zcmeHLeM}t36@UA2cM!V{aI`qF9b+!#9AsU&KniIb0@w#OH6)FlDvsnX%ie*{<~Y2! zIHrn~`{0ZriCya=AZn{dP`Q>{MX8+hq*Sf#QVWzSQmn>`rBoF*B=CnN>=xTeYtDVM zI|IjYr%L^&io{4WZ{|0@_vX!;ncdla)wQdR=Qv=AhkpWTUC>K_YT8lyO`~E$Q4N*A zW4Q*tp|c3Kn3yd?UKWiWCLzaYlU_6CPiX%q`39fKS1bnaJt~=A4BLHUH6E+!+s{04 zIvgC_1P}{xqt+;Q$9gRERrZOHYZ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