Really basic elf parser

This commit is contained in:
Gwilym Inzani 2023-04-06 21:09:44 +01:00
parent 812e99cef5
commit 16c3395524
4 changed files with 73 additions and 0 deletions

1
agb-gbafix/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
!Cargo.lock

16
agb-gbafix/Cargo.lock generated Normal file
View file

@ -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"

9
agb-gbafix/Cargo.toml Normal file
View file

@ -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"

47
agb-gbafix/src/main.rs Normal file
View file

@ -0,0 +1,47 @@
use std::{
fs,
io::{self, Write},
path::PathBuf,
};
fn main() -> Result<(), Box<dyn std::error::Error>> {
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::<elf::endian::AnyEndian>::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(&section_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(())
}