mirror of
https://github.com/mii443/elf-parser.git
synced 2025-08-22 15:05:43 +00:00
init
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
/target
|
45
.vscode/launch.json
vendored
Normal file
45
.vscode/launch.json
vendored
Normal file
@ -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}"
|
||||
}
|
||||
]
|
||||
}
|
39
Cargo.lock
generated
Normal file
39
Cargo.lock
generated
Normal file
@ -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"
|
9
Cargo.toml
Normal file
9
Cargo.toml
Normal file
@ -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"
|
16
src/elf_parser.rs
Normal file
16
src/elf_parser.rs
Normal file
@ -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<u8>
|
||||
}
|
||||
|
||||
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))
|
||||
}
|
||||
}
|
17
src/elf_struct.rs
Normal file
17
src/elf_struct.rs
Normal file
@ -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
|
||||
}
|
27
src/main.rs
Normal file
27
src/main.rs
Normal file
@ -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
|
Reference in New Issue
Block a user