From cd69979b26cdba0193c45337c410777ec6ec10eb Mon Sep 17 00:00:00 2001 From: Putta Khunchalee Date: Sat, 6 Apr 2024 16:33:56 +0700 Subject: [PATCH] Moves ELF handling to kernel (#806) --- src/Cargo.toml | 1 - src/elf/Cargo.toml | 9 -------- src/kernel/Cargo.toml | 1 - src/kernel/src/imgact/mod.rs | 1 + .../src/imgact/orbis}/dynamic.rs | 0 .../src => kernel/src/imgact/orbis}/info.rs | 2 +- .../src/imgact/orbis}/library.rs | 0 .../lib.rs => kernel/src/imgact/orbis/mod.rs} | 0 .../src => kernel/src/imgact/orbis}/module.rs | 0 .../src/imgact/orbis}/program.rs | 0 .../src => kernel/src/imgact/orbis}/reloc.rs | 0 .../src => kernel/src/imgact/orbis}/symbol.rs | 2 +- .../src => kernel/src/imgact/orbis}/ty.rs | 0 src/kernel/src/main.rs | 1 + src/kernel/src/rtld/mem.rs | 2 +- src/kernel/src/rtld/mod.rs | 18 ++++++++-------- src/kernel/src/rtld/module.rs | 21 ++++--------------- src/kernel/src/rtld/resolver.rs | 2 +- 18 files changed, 19 insertions(+), 41 deletions(-) delete mode 100644 src/elf/Cargo.toml create mode 100644 src/kernel/src/imgact/mod.rs rename src/{elf/src => kernel/src/imgact/orbis}/dynamic.rs (100%) rename src/{elf/src => kernel/src/imgact/orbis}/info.rs (99%) rename src/{elf/src => kernel/src/imgact/orbis}/library.rs (100%) rename src/{elf/src/lib.rs => kernel/src/imgact/orbis/mod.rs} (100%) rename src/{elf/src => kernel/src/imgact/orbis}/module.rs (100%) rename src/{elf/src => kernel/src/imgact/orbis}/program.rs (100%) rename src/{elf/src => kernel/src/imgact/orbis}/reloc.rs (100%) rename src/{elf/src => kernel/src/imgact/orbis}/symbol.rs (98%) rename src/{elf/src => kernel/src/imgact/orbis}/ty.rs (100%) diff --git a/src/Cargo.toml b/src/Cargo.toml index 568be57c4..c75ebc24d 100644 --- a/src/Cargo.toml +++ b/src/Cargo.toml @@ -2,7 +2,6 @@ resolver = "2" members = [ "core", - "elf", "error", "fs", "ftp", diff --git a/src/elf/Cargo.toml b/src/elf/Cargo.toml deleted file mode 100644 index a311d4152..000000000 --- a/src/elf/Cargo.toml +++ /dev/null @@ -1,9 +0,0 @@ -[package] -name = "elf" -version = "0.1.0" -edition = "2021" - -[dependencies] -bitflags = "2.1" -byteorder = "1.4" -thiserror = "1.0" diff --git a/src/kernel/Cargo.toml b/src/kernel/Cargo.toml index 0d357fad6..8e8f5754f 100644 --- a/src/kernel/Cargo.toml +++ b/src/kernel/Cargo.toml @@ -13,7 +13,6 @@ bytemuck = "1.14.0" byteorder = "1.4" clap = { version = "4.1", features = ["derive"] } discord-rich-presence = "0.2" -elf = { path = "../elf" } gmtx = { path = "../gmtx" } iced-x86 = { version = "1.18", features = ["code_asm"] } libc = "0.2" diff --git a/src/kernel/src/imgact/mod.rs b/src/kernel/src/imgact/mod.rs new file mode 100644 index 000000000..2fd96a9c4 --- /dev/null +++ b/src/kernel/src/imgact/mod.rs @@ -0,0 +1 @@ +pub mod orbis; diff --git a/src/elf/src/dynamic.rs b/src/kernel/src/imgact/orbis/dynamic.rs similarity index 100% rename from src/elf/src/dynamic.rs rename to src/kernel/src/imgact/orbis/dynamic.rs diff --git a/src/elf/src/info.rs b/src/kernel/src/imgact/orbis/info.rs similarity index 99% rename from src/elf/src/info.rs rename to src/kernel/src/imgact/orbis/info.rs index 8cb0a2074..959ea54f9 100644 --- a/src/elf/src/info.rs +++ b/src/kernel/src/imgact/orbis/info.rs @@ -1,4 +1,4 @@ -use crate::{ +use super::{ DynamicEntries, DynamicTag, LibraryFlags, LibraryInfo, ModuleInfo, Relocations, Symbols, }; use byteorder::{ByteOrder, LE}; diff --git a/src/elf/src/library.rs b/src/kernel/src/imgact/orbis/library.rs similarity index 100% rename from src/elf/src/library.rs rename to src/kernel/src/imgact/orbis/library.rs diff --git a/src/elf/src/lib.rs b/src/kernel/src/imgact/orbis/mod.rs similarity index 100% rename from src/elf/src/lib.rs rename to src/kernel/src/imgact/orbis/mod.rs diff --git a/src/elf/src/module.rs b/src/kernel/src/imgact/orbis/module.rs similarity index 100% rename from src/elf/src/module.rs rename to src/kernel/src/imgact/orbis/module.rs diff --git a/src/elf/src/program.rs b/src/kernel/src/imgact/orbis/program.rs similarity index 100% rename from src/elf/src/program.rs rename to src/kernel/src/imgact/orbis/program.rs diff --git a/src/elf/src/reloc.rs b/src/kernel/src/imgact/orbis/reloc.rs similarity index 100% rename from src/elf/src/reloc.rs rename to src/kernel/src/imgact/orbis/reloc.rs diff --git a/src/elf/src/symbol.rs b/src/kernel/src/imgact/orbis/symbol.rs similarity index 98% rename from src/elf/src/symbol.rs rename to src/kernel/src/imgact/orbis/symbol.rs index c9fb0a5ea..c94d9b18e 100644 --- a/src/elf/src/symbol.rs +++ b/src/kernel/src/imgact/orbis/symbol.rs @@ -1,4 +1,4 @@ -use crate::{FileInfo, StringTableError}; +use super::{FileInfo, StringTableError}; use byteorder::{ByteOrder, LE}; use thiserror::Error; diff --git a/src/elf/src/ty.rs b/src/kernel/src/imgact/orbis/ty.rs similarity index 100% rename from src/elf/src/ty.rs rename to src/kernel/src/imgact/orbis/ty.rs diff --git a/src/kernel/src/main.rs b/src/kernel/src/main.rs index f08455766..f8a528e2d 100644 --- a/src/kernel/src/main.rs +++ b/src/kernel/src/main.rs @@ -49,6 +49,7 @@ mod errno; mod fs; mod hv; mod idt; +mod imgact; mod kqueue; mod log; mod namedobj; diff --git a/src/kernel/src/rtld/mem.rs b/src/kernel/src/rtld/mem.rs index 96194801a..e128e6643 100644 --- a/src/kernel/src/rtld/mem.rs +++ b/src/kernel/src/rtld/mem.rs @@ -1,8 +1,8 @@ use super::MapError; use crate::fs::VFile; +use crate::imgact::orbis::{Elf, ProgramFlags, ProgramType}; use crate::process::VProc; use crate::vm::{MappingFlags, MemoryUpdateError, Protections}; -use elf::{Elf, ProgramFlags, ProgramType}; use gmtx::{Gutex, GutexGroup, GutexWriteGuard}; use std::alloc::Layout; use std::fmt::{Debug, Formatter}; diff --git a/src/kernel/src/rtld/mod.rs b/src/kernel/src/rtld/mod.rs index ce1eaf89f..26c559117 100644 --- a/src/kernel/src/rtld/mod.rs +++ b/src/kernel/src/rtld/mod.rs @@ -6,13 +6,13 @@ use crate::ee::native::{NativeEngine, SetupModuleError}; use crate::errno::{Errno, EINVAL, ENOENT, ENOEXEC, ENOMEM, EPERM, ESRCH}; use crate::fs::{Fs, OpenError, VPath, VPathBuf}; use crate::idt::Entry; +use crate::imgact::orbis::{DynamicFlags, Elf, FileType, ReadProgramError, Relocation, Symbol}; use crate::info; use crate::log::print; use crate::process::{Binaries, VThread}; use crate::syscalls::{SysErr, SysIn, SysOut, Syscalls}; use crate::vm::{MemoryUpdateError, MmapError, Protections}; use bitflags::bitflags; -use elf::{DynamicFlags, Elf, FileType, ReadProgramError, Relocation, Symbol}; use gmtx::{Gutex, GutexGroup, GutexWriteGuard}; use macros::Errno; use sha1::{Digest, Sha1}; @@ -93,7 +93,6 @@ impl RuntimeLinker { .fs .open(path, Some(td)) .map_err(ExecError::OpenExeFailed)?; - let elf = Elf::open(path.as_str(), file).map_err(ExecError::ReadExeFailed)?; // Check image type. @@ -146,6 +145,7 @@ impl RuntimeLinker { } } + // TODO: Move modules preload to here for dynamic executable. // TODO: Apply logic from dmem_handle_process_exec_begin. // TODO: Apply logic from procexec_handler. // TODO: Apply logic from umtx_exec_hook. @@ -1223,7 +1223,7 @@ pub enum ExecError { OpenExeFailed(#[source] OpenError), #[error("cannot read the executable")] - ReadExeFailed(#[source] elf::OpenError), + ReadExeFailed(#[source] crate::imgact::orbis::OpenError), #[error("invalid executable")] InvalidExe, @@ -1266,22 +1266,22 @@ pub enum MapError { UnprotectMemoryFailed(#[source] UnprotectError), #[error("cannot read symbol entry {0}")] - ReadSymbolFailed(usize, #[source] elf::ReadSymbolError), + ReadSymbolFailed(usize, #[source] crate::imgact::orbis::ReadSymbolError), #[error("cannot read DT_NEEDED from dynamic entry {0}")] - ReadNeededFailed(usize, #[source] elf::StringTableError), + ReadNeededFailed(usize, #[source] crate::imgact::orbis::StringTableError), #[error("cannot read DT_SONAME from dynamic entry {0}")] - ReadNameFailed(usize, #[source] elf::StringTableError), + ReadNameFailed(usize, #[source] crate::imgact::orbis::StringTableError), #[error("{0} is obsolete")] ObsoleteFlags(DynamicFlags), #[error("cannot read module info from dynamic entry {0}")] - ReadModuleInfoFailed(usize, #[source] elf::ReadModuleError), + ReadModuleInfoFailed(usize, #[source] crate::imgact::orbis::ReadModuleError), #[error("cannot read libraru info from dynamic entry {0}")] - ReadLibraryInfoFailed(usize, #[source] elf::ReadLibraryError), + ReadLibraryInfoFailed(usize, #[source] crate::imgact::orbis::ReadLibraryError), } /// Represents an error for (S)ELF loading. @@ -1297,7 +1297,7 @@ pub enum LoadError { #[error("cannot open (S)ELF")] #[errno(ENOEXEC)] - OpenElfFailed(#[source] elf::OpenError), + OpenElfFailed(#[source] crate::imgact::orbis::OpenError), #[error("the specified file is not valid module")] #[errno(ENOEXEC)] diff --git a/src/kernel/src/rtld/module.rs b/src/kernel/src/rtld/module.rs index c17f7ff5a..e0d3c7fb0 100644 --- a/src/kernel/src/rtld/module.rs +++ b/src/kernel/src/rtld/module.rs @@ -1,19 +1,17 @@ use super::{MapError, Memory}; use crate::ee::native::{NativeEngine, RawFn}; use crate::fs::{VFile, VPath, VPathBuf}; +use crate::imgact::orbis::{ + DynamicFlags, DynamicTag, Elf, FileInfo, FileType, LibraryFlags, LibraryInfo, ModuleInfo, + Program, Symbol, +}; use crate::log::{print, LogEntry}; use crate::process::VProc; use bitflags::bitflags; use byteorder::{ByteOrder, LE}; -use elf::{ - DynamicFlags, DynamicTag, Elf, FileInfo, FileType, LibraryFlags, LibraryInfo, ModuleInfo, - Program, Symbol, -}; use gmtx::{Gutex, GutexGroup, GutexReadGuard, GutexWriteGuard}; use std::fmt::{Display, Formatter}; -use std::fs::OpenOptions; use std::io::Write; -use std::path::Path; use std::sync::Arc; /// An implementation of @@ -336,17 +334,6 @@ impl Module { .unwrap() } - pub fn dump>(&mut self, path: P) -> Result<(), std::io::Error> { - let path = path.as_ref(); - let mut file = OpenOptions::new() - .write(true) - .create(true) - .truncate(true) - .open(path)?; - - file.write_all(unsafe { self.memory.as_bytes() }) - } - pub fn print(&self, mut entry: LogEntry) { // Lock all required fields first so the output is consistent. let flags = self.flags.read(); diff --git a/src/kernel/src/rtld/resolver.rs b/src/kernel/src/rtld/resolver.rs index bf83f8d7f..b7760ccfe 100644 --- a/src/kernel/src/rtld/resolver.rs +++ b/src/kernel/src/rtld/resolver.rs @@ -1,7 +1,7 @@ use super::Module; +use crate::imgact::orbis::Symbol; use crate::process::Binaries; use bitflags::bitflags; -use elf::Symbol; use std::borrow::Cow; use std::ops::Deref; use std::sync::Arc;