From 16c3395524d59b4fdcf8dbb288eaac7635605940 Mon Sep 17 00:00:00 2001 From: Gwilym Inzani Date: Thu, 6 Apr 2023 21:09:44 +0100 Subject: [PATCH] Really basic elf parser --- agb-gbafix/.gitignore | 1 + agb-gbafix/Cargo.lock | 16 ++++++++++++++ agb-gbafix/Cargo.toml | 9 ++++++++ agb-gbafix/src/main.rs | 47 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 73 insertions(+) create mode 100644 agb-gbafix/.gitignore create mode 100644 agb-gbafix/Cargo.lock create mode 100644 agb-gbafix/Cargo.toml create mode 100644 agb-gbafix/src/main.rs diff --git a/agb-gbafix/.gitignore b/agb-gbafix/.gitignore new file mode 100644 index 00000000..0b512c76 --- /dev/null +++ b/agb-gbafix/.gitignore @@ -0,0 +1 @@ +!Cargo.lock \ No newline at end of file diff --git a/agb-gbafix/Cargo.lock b/agb-gbafix/Cargo.lock new file mode 100644 index 00000000..4677f650 --- /dev/null +++ b/agb-gbafix/Cargo.lock @@ -0,0 +1,16 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "agb-gbafix" +version = "0.1.0" +dependencies = [ + "elf", +] + +[[package]] +name = "elf" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2b183d6ce6ca4cf30e3db37abf5b52568b5f9015c97d9fbdd7026aa5dcdd758" diff --git a/agb-gbafix/Cargo.toml b/agb-gbafix/Cargo.toml new file mode 100644 index 00000000..ee86b776 --- /dev/null +++ b/agb-gbafix/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "agb-gbafix" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +elf = "0.7" \ No newline at end of file diff --git a/agb-gbafix/src/main.rs b/agb-gbafix/src/main.rs new file mode 100644 index 00000000..e3c0d3a4 --- /dev/null +++ b/agb-gbafix/src/main.rs @@ -0,0 +1,47 @@ +use std::{ + fs, + io::{self, Write}, + path::PathBuf, +}; + +fn main() -> Result<(), Box> { + let path = PathBuf::from("tests/text_render"); + let file_data = fs::read(path)?; + let file_data = file_data.as_slice(); + + let elf_file = elf::ElfBytes::::minimal_parse(file_data)?; + + let (section_headers, strtab) = elf_file.section_headers_with_strtab()?; + let section_headers = section_headers.expect("Expected section headers"); + let strtab = strtab.expect("Expected string table"); + + let output = fs::File::create("out.gba")?; + let mut buf_writer = io::BufWriter::new(output); + + for section_header in section_headers.iter() { + let section_name = strtab.get(section_header.sh_name as usize)?; + + const SHT_NOBITS: u32 = 8; + const SHT_NULL: u32 = 0; + const SHF_ALLOC: u64 = 2; + + if (section_header.sh_type == SHT_NOBITS || section_header.sh_type == SHT_NULL) + || section_header.sh_flags & SHF_ALLOC == 0 + { + continue; + } + + println!("{section_name}"); + + let (data, compression) = elf_file.section_data(§ion_header)?; + if let Some(compression) = compression { + panic!("Cannot compress elf content, but got compression header {compression:?}"); + } + + buf_writer.write_all(data)?; + } + + buf_writer.flush()?; + + Ok(()) +}