Switch to goblin::elf

This commit is contained in:
Artemis Tosini 2024-08-04 00:19:06 +00:00
parent 0f2fa36333
commit 405cdf8f26
Signed by: artemist
GPG key ID: EE5227935FE3FF18
3 changed files with 45 additions and 19 deletions

45
Cargo.lock generated
View file

@ -20,27 +20,38 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "elf"
version = "0.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4445909572dbd556c457c849c4ca58623d84b27c8fff1e74b0b4227d8b90d17b"
[[package]] [[package]]
name = "freeloader" name = "freeloader"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"elf", "goblin",
"log", "log",
"uefi", "uefi",
] ]
[[package]]
name = "goblin"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b363a30c165f666402fe6a3024d3bec7ebc898f96a4a23bd1c99f8dbf3f4f47"
dependencies = [
"log",
"plain",
"scroll",
]
[[package]] [[package]]
name = "log" name = "log"
version = "0.4.22" version = "0.4.22"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24"
[[package]]
name = "plain"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b4596b6d070b27117e987119b4dac604f3c58cfb0b191112e24771b2faeac1a6"
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.86" version = "1.0.86"
@ -79,6 +90,26 @@ dependencies = [
"proc-macro2", "proc-macro2",
] ]
[[package]]
name = "scroll"
version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ab8598aa408498679922eff7fa985c25d58a90771bd6be794434c5277eab1a6"
dependencies = [
"scroll_derive",
]
[[package]]
name = "scroll_derive"
version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f81c2fde025af7e69b1d1420531c8a8811ca898919db177141a85313b1cb932"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.72",
]
[[package]] [[package]]
name = "syn" name = "syn"
version = "1.0.109" version = "1.0.109"

View file

@ -6,4 +6,4 @@ edition = "2021"
[dependencies] [dependencies]
uefi = { version = "0.29.0", default-features = false, features = [ "alloc", "global_allocator", "panic_handler", "logger" ] } uefi = { version = "0.29.0", default-features = false, features = [ "alloc", "global_allocator", "panic_handler", "logger" ] }
log = { version = "0.4.22", default-features = false } log = { version = "0.4.22", default-features = false }
elf = { version = "0.7.4", default_features = false } goblin = { version = "0.8.2", default-features = false, features = [ "elf64", "elf32", "endian_fd" ] }

View file

@ -2,11 +2,11 @@ extern crate alloc;
use crate::abi::{ModuleInfoItem, ModuleInfoType, ModuleInfoValue, Serialize}; use crate::abi::{ModuleInfoItem, ModuleInfoType, ModuleInfoValue, Serialize};
use alloc::vec::Vec; use alloc::vec::Vec;
use elf::{endian::NativeEndian, ElfBytes}; use goblin::elf::Elf;
use log::info; use log::info;
#[cfg(target_arch = "x86_64")] #[cfg(target_arch = "x86_64")]
const NATIVE_ELF_MACHINE: u16 = elf::abi::EM_X86_64; const NATIVE_ELF_MACHINE: u16 = goblin::elf::header::EM_X86_64;
pub trait Module { pub trait Module {
/// Associated metadata for this module, including name and type /// Associated metadata for this module, including name and type
@ -17,24 +17,19 @@ pub struct Kernel<'a> {
/// Full ELF /// Full ELF
elf: &'a [u8], elf: &'a [u8],
/// Parsed ELF /// Parsed ELF
parsed: ElfBytes<'a, NativeEndian>, parsed: Elf<'a>,
/// Raw ELF header bytes /// Raw ELF header bytes
elf_header: &'a [u8], elf_header: &'a [u8],
} }
impl<'a> Kernel<'a> { impl<'a> Kernel<'a> {
pub fn from_elf_bytes(elf: &'a [u8]) -> Self { pub fn from_elf_bytes(elf: &'a [u8]) -> Self {
let parsed = let parsed = Elf::parse(elf).expect("Failed to parse kernel");
elf::ElfBytes::<NativeEndian>::minimal_parse(elf).expect("Failed to parse kernel"); if parsed.header.e_machine != NATIVE_ELF_MACHINE {
info!("Kernel is {:?}", parsed.ehdr.class);
if parsed.ehdr.e_machine != NATIVE_ELF_MACHINE {
panic!("Kernel has wrong ELF machine"); panic!("Kernel has wrong ELF machine");
} }
let header_size = match parsed.ehdr.class { let header_size = if parsed.is_64 { 0x40 } else { 0x34 };
elf::file::Class::ELF32 => 0x34,
elf::file::Class::ELF64 => 0x40,
};
let elf_header = &elf[..header_size]; let elf_header = &elf[..header_size];
Self { Self {