Loader: Add ldr:ro->LoadNro()

This commit is contained in:
Michael Scire 2018-04-27 03:17:07 -06:00
parent 10171313df
commit 772e41971d
6 changed files with 288 additions and 25 deletions

View file

@ -77,13 +77,16 @@ struct MappedCodeMemory {
void *mapped_address;
bool IsActive() {
return this->code_memory_address != 0;
}
bool IsMapped() {
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;
}
@ -91,40 +94,64 @@ struct MappedCodeMemory {
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 = MapUtils::MapCodeMemoryForProcess(this->process_handle, is_64_bit_address_space, this->base_address, this->size, &this->code_memory_address)))) {
Close();
}
return rc;
}
Result OpenAtAddress(Handle process_h, u64 address, u64 size, u64 target_code_memory_address) {
Result rc;
if (this->IsActive()) {
return 0x19009;
}
this->process_handle = process_h;
this->base_address = address;
this->size = size;
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};
if (R_SUCCEEDED((rc = svcMapProcessCodeMemory(this->process_handle, target_code_memory_address, this->base_address, this->size)))) {
this->code_memory_address = target_code_memory_address;
} else {
Close();
}
return rc;
}
void Close() {
if (this->IsActive()) {
Result Map() {
Result rc;
u64 try_address;
if (this->IsMapped()) {
return 0x19009;
}
if (R_FAILED(rc = MapUtils::LocateSpaceForMap(&try_address, size))) {
return rc;
}
if (R_FAILED((rc = svcMapProcessMemory((void *)try_address, this->process_handle, try_address, size)))) {
return rc;
}
this->mapped_address = (void *)try_address;
return rc;
}
void Unmap() {
if (this->IsMapped()) {
if (R_FAILED(svcUnmapProcessMemory(this->mapped_address, this->process_handle, this->base_address, this->size))) {
/* TODO: panic(). */
}
}
this->mapped_address = NULL;
}
void Close() {
Unmap();
if (this->IsActive()) {
if (R_FAILED(svcUnmapProcessCodeMemory(this->process_handle, this->code_memory_address, this->base_address, this->size))) {
/* TODO: panic(). */
}
*this = (const MappedCodeMemory){0};
}
*this = (const MappedCodeMemory){0};
}
};