Skip to content
This repository has been archived by the owner on Oct 11, 2024. It is now read-only.

Fix POSIX_CALL return value + Library fix #168

Merged
merged 3 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 18 additions & 15 deletions core/dmem/types/directmem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -446,23 +446,26 @@ int DirectMemory::getAvailableSize(uint32_t start, uint32_t end, size_t alignmen
return Ok;
}

*startOut = start;
*sizeOut = (itItem->second.addr - DIRECTMEM_START) - start;
// if (itItem->second.addr + itItem->second.size >= DIRECTMEM_START + end) {
// *startOut = end;
// *sizeOut = 0;
// return Ok;
// }

// *startOut = start;
// *sizeOut = 0;
// *sizeOut = (itItem->second.addr - DIRECTMEM_START) - start;
if (itItem->second.addr + itItem->second.size >= DIRECTMEM_START + end) {
*startOut = end;
*sizeOut = 0;
return Ok;
}

// auto itEnd = m_objects.lower_bound(DIRECTMEM_START + end);
// for (; itItem != itEnd; ++itItem) {
// *startOut = (itItem->second.addr + itItem->second.size) - DIRECTMEM_START;
// }
*startOut = start;
*sizeOut = 0;

auto itEnd = m_objects.lower_bound(DIRECTMEM_START + end);
for (; itItem != itEnd; ++itItem) {
*startOut = (itItem->second.addr + itItem->second.size) - DIRECTMEM_START;
}

// if (*startOut > end) *sizeOut = end - *startOut;
if (*startOut > end)
*sizeOut = *startOut - end;
else
*sizeOut = end - *startOut;

return Ok;
}
Expand Down Expand Up @@ -539,4 +542,4 @@ int32_t DirectMemory::directQuery(uint64_t offset, SceKernelDirectMemoryQueryInf
LOG_DEBUG(L"Query Error: offset:0x%08llx", offset);

return getErr(ErrCode::_EACCES);
}
}
3 changes: 2 additions & 1 deletion core/fileManager/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ add_library(fileManager OBJECT
types/type_file.cpp
types/type_random.cpp
types/type_rng.cpp
types/type_lib.cpp
)

add_dependencies(fileManager third_party psOff_utility)
add_dependencies(fileManager third_party psOff_utility)
1 change: 1 addition & 0 deletions core/fileManager/ifile.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
enum class FileType {
File,
Device,
Library,
};

enum class SceWhence : int {
Expand Down
54 changes: 54 additions & 0 deletions core/fileManager/types/type_lib.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include "type_lib.h"

#include "logging.h"

LOG_DEFINE_MODULE(IODevice_LIB);

class TypeLib: public IFile {
std::unique_ptr<Program>& m_prog;

public:
TypeLib(std::unique_ptr<Program>& prog): IFile(FileType::Library), m_prog(prog) {}

~TypeLib() {}

// ### Interface
size_t read(void* buf, size_t nbytes) final;
size_t write(void* buf, size_t nbytes) final;
void sync() final;
int ioctl(int request, SceVariadicList argp) final;
int fcntl(int cmd, SceVariadicList argp) final;
int64_t lseek(int64_t offset, SceWhence whence) final;

void* getNative() final { return nullptr; }
};

std::unique_ptr<IFile> createType_lib(std::unique_ptr<Program>& prog) {
return std::make_unique<TypeLib>(prog);
}

size_t TypeLib::read(void* buf, size_t nbytes) {
LOG_USE_MODULE(IODevice_LIB);
LOG_CRIT(L"Program(%p)->read(%p, %llu)", m_prog.get(), buf, nbytes);
return 0;
}

size_t TypeLib::write(void* buf, size_t nbytes) {
LOG_USE_MODULE(IODevice_LIB);
LOG_CRIT(L"Program(%p)->write(%p, %llu)", m_prog.get(), buf, nbytes);
return 0;
}

void TypeLib::sync() {}

int TypeLib::ioctl(int request, SceVariadicList argp) {
return 0;
}

int TypeLib::fcntl(int cmd, SceVariadicList argp) {
return 0;
}

int64_t TypeLib::lseek(int64_t offset, SceWhence whence) {
return -1;
}
6 changes: 6 additions & 0 deletions core/fileManager/types/type_lib.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#pragma once

#include "../ifile.h"
#include "core/runtime/program.h"

std::unique_ptr<IFile> createType_lib(std::unique_ptr<Program>& prog);
7 changes: 4 additions & 3 deletions core/kernel/errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ __APICALL void setError_pthread(int);
static int32_t POSIX_CALL(int32_t result) {
if (result >= 0) return result;

setError_pthread(result - (int32_t)0x80020000);
return -1;
int res = result - (int32_t)0x80020000;
setError_pthread(res);
return res;
}

static int32_t POSIX_SET(ErrCode error) {
setError_pthread((int32_t)error);
return -1;
}

#undef __APICALL
#undef __APICALL
6 changes: 3 additions & 3 deletions core/runtime/formats/elf64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1117,8 +1117,8 @@ bool Parser_ELF64::load2Mem(Program* prog) {
prog->baseVaddr = memory::alloc(prog->desiredBaseAddr, prog->baseSizeAligned + /*fs_table: */ sizeof(uint64_t) * (1 + fsPatchAddrList.size()),
SceProtExecute | SceProtRead | SceProtWrite);

printf("[%llu] %S| Image allocation, addr:0x%08llx size:0x%08llx\n", prog->id, m_filename.c_str(), prog->baseVaddr, prog->allocSize);
LOG_DEBUG(L"[%llu] %s| Image allocation, addr:0x%08llx(0x%08llx) size:0x%08llx", prog->id, m_filename.c_str(), prog->baseVaddr, prog->desiredBaseAddr,
printf("[%d] %S| Image allocation, addr:0x%08llx size:0x%08llx\n", prog->id, m_filename.c_str(), prog->baseVaddr, prog->allocSize);
LOG_DEBUG(L"[%d] %s| Image allocation, addr:0x%08llx(0x%08llx) size:0x%08llx", prog->id, m_filename.c_str(), prog->baseVaddr, prog->desiredBaseAddr,
prog->allocSize);

m_baseVaddr = prog->baseVaddr;
Expand Down Expand Up @@ -1535,4 +1535,4 @@ void Parser_ELF64::dump(std::fstream& outFile, std::fstream& mapFile) {

void elf64::dump(IFormat* format, std::fstream& outFile, std::fstream& mapFile) {
((Parser_ELF64*)format)->dump(outFile, mapFile);
}
}
7 changes: 3 additions & 4 deletions core/runtime/program.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ struct Program {
std::filesystem::path const filename;
std::filesystem::path const path;

size_t const id;
int32_t id;
uint64_t const baseSize;
uint64_t const baseSizeAligned;
uint64_t const desiredBaseAddr;
Expand All @@ -59,11 +59,10 @@ struct Program {

std::vector<CxaDestructor> cxa;

Program(std::filesystem::path path_, size_t uniqueIndex_, uint64_t baseSize_, uint64_t baseSizeAligned_, uint64_t desiredBaseAddr_, uint64_t allocSize_,
bool useStaticTLS_)
Program(std::filesystem::path path_, uint64_t baseSize_, uint64_t baseSizeAligned_, uint64_t desiredBaseAddr_, uint64_t allocSize_, bool useStaticTLS_)
: filename(path_.filename()),
path(path_),
id(uniqueIndex_),
id(-1),
baseSize(baseSize_),
baseSizeAligned(baseSizeAligned_),
desiredBaseAddr(desiredBaseAddr_),
Expand Down
15 changes: 9 additions & 6 deletions core/runtime/runtimeLinker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include "runtimeLinker.h"
#undef __APICALL_EXTERN

#include "core/fileManager/fileManager.h"
#include "core/fileManager/types/type_lib.h"
#include "core/kernel/pthread.h"
#include "core/kernel/pthread_intern.h"
#include "core/memory/memory.h"
Expand Down Expand Up @@ -184,8 +186,7 @@ class RuntimeLinker: public IRuntimeLinker {
private:
mutable std::mutex m_mutex_int;

uint64_t m_invalidMemoryAddr = 0;
size_t m_countcreatePrograms = 0;
uint64_t m_invalidMemoryAddr = 0;

EntryParams m_entryParams = {
.argc = 1,
Expand Down Expand Up @@ -240,8 +241,10 @@ class RuntimeLinker: public IRuntimeLinker {
std::unique_ptr<Program> createProgram(std::filesystem::path const filepath, uint64_t const baseSize, uint64_t const baseSizeAligned, uint64_t const alocSize,
bool useStaticTLS) final {
std::unique_lock const lock(m_mutex_int);
++m_countcreatePrograms;
auto inst = std::make_unique<Program>(filepath, m_countcreatePrograms, baseSize, baseSizeAligned, IMAGE_BASE, alocSize, useStaticTLS);

auto inst = std::make_unique<Program>(filepath, baseSize, baseSizeAligned, IMAGE_BASE, alocSize, useStaticTLS);
inst->id = accessFileManager().addFile(createType_lib(inst), filepath);

return inst;
}

Expand Down Expand Up @@ -661,7 +664,7 @@ uint32_t RuntimeLinker::createTLSKey(void* destructor) {

std::unique_lock const lock(m_mutex_int);

for (uint64_t n = m_countcreatePrograms; n < m_dtvKeys.size(); ++n) {
for (uint64_t n = 0; n < m_dtvKeys.size(); ++n) {
if (!m_dtvKeys[n].used) {
m_dtvKeys[n].used = true;
m_dtvKeys[n].destructor = (pthread_key_destructor_func_t)destructor;
Expand Down Expand Up @@ -695,7 +698,7 @@ void RuntimeLinker::destroyTLSKeys(uint8_t* obj) {

std::unique_lock const lock(m_mutex_int);

for (uint64_t n = m_countcreatePrograms; n < m_dtvKeys.size(); ++n, ++pDtvKey) {
for (uint64_t n = 0; n < m_dtvKeys.size(); ++n, ++pDtvKey) {
if (m_dtvKeys[n].destructor != nullptr) {
if (pDtvKey[n] != 0) m_dtvKeys[n].destructor((void*)pDtvKey[n]);
}
Expand Down
9 changes: 0 additions & 9 deletions index.html

This file was deleted.

Loading