Skip to content

Commit

Permalink
Moves ELF handling to kernel (obhq#806)
Browse files Browse the repository at this point in the history
  • Loading branch information
ultimaweapon authored Apr 6, 2024
1 parent 6c95c1f commit cd69979
Show file tree
Hide file tree
Showing 18 changed files with 19 additions and 41 deletions.
1 change: 0 additions & 1 deletion src/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
resolver = "2"
members = [
"core",
"elf",
"error",
"fs",
"ftp",
Expand Down
9 changes: 0 additions & 9 deletions src/elf/Cargo.toml

This file was deleted.

1 change: 0 additions & 1 deletion src/kernel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions src/kernel/src/imgact/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod orbis;
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{
use super::{
DynamicEntries, DynamicTag, LibraryFlags, LibraryInfo, ModuleInfo, Relocations, Symbols,
};
use byteorder::{ByteOrder, LE};
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{FileInfo, StringTableError};
use super::{FileInfo, StringTableError};
use byteorder::{ByteOrder, LE};
use thiserror::Error;

Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions src/kernel/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ mod errno;
mod fs;
mod hv;
mod idt;
mod imgact;
mod kqueue;
mod log;
mod namedobj;
Expand Down
2 changes: 1 addition & 1 deletion src/kernel/src/rtld/mem.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down
18 changes: 9 additions & 9 deletions src/kernel/src/rtld/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand All @@ -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)]
Expand Down
21 changes: 4 additions & 17 deletions src/kernel/src/rtld/module.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -336,17 +334,6 @@ impl Module {
.unwrap()
}

pub fn dump<P: AsRef<Path>>(&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();
Expand Down
2 changes: 1 addition & 1 deletion src/kernel/src/rtld/resolver.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down

0 comments on commit cd69979

Please sign in to comment.