Loader: Implement ldr:ro->LoadNRR(). NOTE: No sigchecks, at the moment.

This commit is contained in:
Michael Scire 2018-04-26 20:27:52 -06:00
parent 789afe7929
commit 8524f284fd
6 changed files with 143 additions and 25 deletions

View file

@ -1,8 +1,6 @@
#pragma once
#include <switch.h>
#include "ldr_registration.hpp"
class MapUtils {
public:
struct AddressSpaceInfo {
@ -69,4 +67,64 @@ class AutoCloseMap {
this->mapped_address = NULL;
}
}
};
struct MappedCodeMemory {
Handle process_handle;
u64 base_address;
u64 size;
u64 code_memory_address;
void *mapped_address;
bool IsActive() {
return this->mapped_address != NULL;
}
/* Utility functions. */
Result Open(Handle process_h, bool is_64_bit_address_space, u64 address, u64 size) {
Result rc;
u64 try_address;
if (this->IsActive()) {
return 0x19009;
}
this->process_handle = process_h;
this->base_address = address;
this->size = size;
if (R_FAILED((rc = MapUtils::MapCodeMemoryForProcess(process_h, is_64_bit_address_space, address, size, &this->code_memory_address)))) {
goto CODE_MEMORY_OPEN_END;
}
if (R_FAILED(rc = MapUtils::LocateSpaceForMap(&try_address, size))) {
goto CODE_MEMORY_OPEN_END;
}
if (R_FAILED((rc = svcMapProcessMemory((void *)try_address, process_h, try_address, size)))) {
goto CODE_MEMORY_OPEN_END;
}
this->mapped_address = (void *)try_address;
CODE_MEMORY_OPEN_END:
if (R_FAILED(rc)) {
if (this->code_memory_address && R_FAILED(svcUnmapProcessCodeMemory(this->process_handle, this->code_memory_address, this->base_address, this->size))) {
/* TODO: panic(). */
}
*this = (const MappedCodeMemory){0};
}
return rc;
}
void Close() {
if (this->IsActive()) {
if (R_FAILED(svcUnmapProcessMemory(this->mapped_address, this->process_handle, this->base_address, this->size))) {
/* TODO: panic(). */
}
if (R_FAILED(svcUnmapProcessCodeMemory(this->process_handle, this->code_memory_address, this->base_address, this->size))) {
/* TODO: panic(). */
}
*this = (const MappedCodeMemory){0};
}
}
};