Skip to content

Commit

Permalink
Finishes syscall 608 and fixes weak symbol (#312)
Browse files Browse the repository at this point in the history
  • Loading branch information
ultimaweapon authored Aug 25, 2023
1 parent c6598eb commit fbf61a5
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 22 deletions.
7 changes: 4 additions & 3 deletions src/kernel/src/ee/llvm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ mod codegen;

/// An implementation of [`ExecutionEngine`] using JIT powered by LLVM IR.
pub struct LlvmEngine<'a, 'b: 'a> {
llvm: &'static Llvm,
rtld: &'a RuntimeLinker<'b>,
}

impl<'a, 'b: 'a> LlvmEngine<'a, 'b> {
pub fn new(rtld: &'a RuntimeLinker<'b>) -> Self {
Self { rtld }
pub fn new(llvm: &'static Llvm, rtld: &'a RuntimeLinker<'b>) -> Self {
Self { llvm, rtld }
}

pub fn lift_initial_modules(&mut self) -> Result<(), LiftError> {
Expand Down Expand Up @@ -49,7 +50,7 @@ impl<'a, 'b: 'a> LlvmEngine<'a, 'b> {
disasm.fixup();

// Lift the public functions.
let mut lifting = Llvm::current().create_module(path);
let mut lifting = self.llvm.create_module(path);
let mut codegen = Codegen::new(disasm, &mut lifting);

for &addr in &targets {
Expand Down
19 changes: 4 additions & 15 deletions src/kernel/src/llvm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use llvm_sys::core::{LLVMContextCreate, LLVMContextDispose, LLVMModuleCreateWith
use llvm_sys::prelude::LLVMContextRef;
use std::ffi::{c_char, CStr, CString};
use std::fmt::Display;
use std::sync::{Mutex, OnceLock};
use std::sync::Mutex;

pub mod module;

Expand All @@ -14,21 +14,12 @@ pub struct Llvm {
}

impl Llvm {
/// # Panics
/// If this method called a second time.
pub fn init() {
pub fn new() -> Self {
let context = unsafe { LLVMContextCreate() };

LLVM.set(Self {
Self {
context: Mutex::new(context),
})
.unwrap();
}

/// # Panics
/// If [`init()`] has not invoked yet.
pub fn current() -> &'static Llvm {
LLVM.get().unwrap()
}
}

pub fn create_module(&self, name: &str) -> LlvmModule<'_> {
Expand Down Expand Up @@ -85,5 +76,3 @@ impl Display for Error {
}

impl std::error::Error for Error {}

static LLVM: OnceLock<Llvm> = OnceLock::new();
4 changes: 2 additions & 2 deletions src/kernel/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ fn main() -> ExitCode {

// Initialize LLVM.
info!("Initializing LLVM.");
Llvm::init();

let llvm: &'static Llvm = Box::leak(Llvm::new().into());
let sysctl = Sysctl::new(arc4);

// Initialize filesystem.
Expand Down Expand Up @@ -245,7 +245,7 @@ fn main() -> ExitCode {
return ExitCode::FAILURE;
}
ExecutionEngine::Llvm => {
let mut ee = ee::llvm::LlvmEngine::new(&ld);
let mut ee = ee::llvm::LlvmEngine::new(llvm, &ld);

info!("Lifting modules.");

Expand Down
12 changes: 10 additions & 2 deletions src/kernel/src/rtld/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ impl<'a> SymbolResolver<'a> {
};

// TODO: Handle LinkerFlags::UNK2.
let mut result = None;

for md in list {
// TODO: Implement DoneList.
if let Some(name) = symmod {
Expand Down Expand Up @@ -170,12 +172,18 @@ impl<'a> SymbolResolver<'a> {
None => continue,
};

if md.symbol(index).unwrap().binding() != Symbol::STB_WEAK {
// Return the symbol if it is not a weak binding.
let sym = md.symbol(index).unwrap();

if sym.binding() != Symbol::STB_WEAK {
return Some((md, index));
} else if result.is_none() {
// Use the first weak, not the last weak; if no non-weak.
result = Some((md, index));
}
}

None
result
}

/// See `symlook_obj` on the PS4 for a reference.
Expand Down
4 changes: 4 additions & 0 deletions src/kernel/src/syscalls/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,8 @@ pub struct DynlibInfoEx {
pub unk3: u32, // Always 5.
pub database: usize,
pub datasize: u32,
pub unk4: u32, // Always 3.
pub unk5: [u8; 0x20], // Always zeroes.
pub unk6: u32, // Always 2.
pub refcount: u32,
}
4 changes: 4 additions & 0 deletions src/kernel/src/syscalls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::thread::VThread;
use crate::warn;
use kernel_macros::cpu_abi;
use std::mem::{size_of, zeroed};
use std::sync::Arc;

mod error;
mod input;
Expand Down Expand Up @@ -285,6 +286,9 @@ impl<'a, 'b: 'a> Syscalls<'a, 'b> {
(*info).unk3 = 5;
(*info).database = addr + mem.data_segment().start();
(*info).datasize = mem.data_segment().len().try_into().unwrap();
(*info).unk4 = 3;
(*info).unk6 = 2;
(*info).refcount = Arc::strong_count(md).try_into().unwrap();

// Copy module name.
if flags & 2 == 0 || !md.flags().contains(ModuleFlags::UNK1) {
Expand Down

0 comments on commit fbf61a5

Please sign in to comment.