mirror of
https://github.com/skyline-emu/skyline.git
synced 2024-06-02 03:18:47 +02:00
fe5061a8e0
This addresses all CR comments including more codebase-wide changes arising from certain review comments like proper usage of its/it's and consistent contraction of it is into it's. An overhaul was made to the presentation and formatting of `KThread.h` and `LoadBalance` works has been superseded by `GetOptimalCoreForThread` which can be used alongside `InsertThread` or `MigrateToCore`. It makes the API far more atomic and neater. This was a major point of contention for the design prior, it's simplified some code and potentially improved performance.
69 lines
3.3 KiB
C++
69 lines
3.3 KiB
C++
// SPDX-License-Identifier: MPL-2.0
|
|
// Copyright © 2020 Skyline Team and Contributors (https://github.com/skyline-emu/)
|
|
|
|
#include <lz4.h>
|
|
#include <nce.h>
|
|
#include <kernel/types/KProcess.h>
|
|
#include "nso.h"
|
|
|
|
namespace skyline::loader {
|
|
NsoLoader::NsoLoader(const std::shared_ptr<vfs::Backing> &backing) : backing(backing) {
|
|
u32 magic{backing->Read<u32>()};
|
|
|
|
if (magic != util::MakeMagic<u32>("NSO0"))
|
|
throw exception("Invalid NSO magic! 0x{0:X}", magic);
|
|
}
|
|
|
|
std::vector<u8> NsoLoader::GetSegment(const std::shared_ptr<vfs::Backing> &backing, const NsoSegmentHeader &segment, u32 compressedSize) {
|
|
std::vector<u8> outputBuffer(segment.decompressedSize);
|
|
|
|
if (compressedSize) {
|
|
std::vector<u8> compressedBuffer(compressedSize);
|
|
backing->Read(compressedBuffer, segment.fileOffset);
|
|
|
|
LZ4_decompress_safe(reinterpret_cast<char *>(compressedBuffer.data()), reinterpret_cast<char *>(outputBuffer.data()), compressedSize, segment.decompressedSize);
|
|
} else {
|
|
backing->Read(outputBuffer, segment.fileOffset);
|
|
}
|
|
|
|
return outputBuffer;
|
|
}
|
|
|
|
Loader::ExecutableLoadInfo NsoLoader::LoadNso(Loader *loader, const std::shared_ptr<vfs::Backing> &backing, const std::shared_ptr<kernel::type::KProcess> process, const DeviceState &state, size_t offset, const std::string &name) {
|
|
auto header{backing->Read<NsoHeader>()};
|
|
|
|
if (header.magic != util::MakeMagic<u32>("NSO0"))
|
|
throw exception("Invalid NSO magic! 0x{0:X}", header.magic);
|
|
|
|
Executable executable{};
|
|
|
|
executable.text.contents = GetSegment(backing, header.text, header.flags.textCompressed ? header.textCompressedSize : 0);
|
|
executable.text.contents.resize(util::AlignUp(executable.text.contents.size(), PAGE_SIZE));
|
|
executable.text.offset = header.text.memoryOffset;
|
|
|
|
executable.ro.contents = GetSegment(backing, header.ro, header.flags.roCompressed ? header.roCompressedSize : 0);
|
|
executable.ro.contents.resize(util::AlignUp(executable.ro.contents.size(), PAGE_SIZE));
|
|
executable.ro.offset = header.ro.memoryOffset;
|
|
|
|
executable.data.contents = GetSegment(backing, header.data, header.flags.dataCompressed ? header.dataCompressedSize : 0);
|
|
executable.data.contents.resize(util::AlignUp(executable.data.contents.size(), PAGE_SIZE));
|
|
executable.data.offset = header.data.memoryOffset;
|
|
|
|
executable.bssSize = util::AlignUp(header.bssSize, PAGE_SIZE);
|
|
|
|
if (header.dynsym.offset + header.dynsym.size <= header.ro.decompressedSize && header.dynstr.offset + header.dynstr.size <= header.ro.decompressedSize) {
|
|
executable.dynsym = {header.dynsym.offset, header.dynsym.size};
|
|
executable.dynstr = {header.dynstr.offset, header.dynstr.size};
|
|
}
|
|
|
|
return loader->LoadExecutable(process, state, executable, offset, name);
|
|
}
|
|
|
|
void *NsoLoader::LoadProcessData(const std::shared_ptr<kernel::type::KProcess> process, const DeviceState &state) {
|
|
state.process->memory.InitializeVmm(memory::AddressSpaceType::AddressSpace39Bit);
|
|
auto loadInfo{LoadNso(this, backing, process, state)};
|
|
state.process->memory.InitializeRegions(loadInfo.base, loadInfo.size);
|
|
return loadInfo.entry;
|
|
}
|
|
}
|