Allocate objects on stack instead of heap

This commit is contained in:
Maschell 2020-08-23 13:06:32 +02:00
parent 598642e970
commit 09d6b9f756
8 changed files with 66 additions and 85 deletions

View File

@ -35,10 +35,9 @@
#include "dynamic.h" #include "dynamic.h"
#include "utils/logger.h" #include "utils/logger.h"
bool doRelocation(std::vector<RelocationData *> &relocData, relocation_trampolin_entry_t *tramp_data, uint32_t tramp_length); bool doRelocation(const std::vector<RelocationData> &relocData, relocation_trampolin_entry_t *tramp_data, uint32_t tramp_length);
bool CheckRunning() { bool CheckRunning() {
switch (ProcUIProcessMessages(true)) { switch (ProcUIProcessMessages(true)) {
case PROCUI_STATUS_EXITING: { case PROCUI_STATUS_EXITING: {
return false; return false;
@ -82,10 +81,10 @@ extern "C" int _start(int argc, char **argv) {
uint32_t moduleDataStartAddress = ((uint32_t) gModuleData + sizeof(module_information_t)); uint32_t moduleDataStartAddress = ((uint32_t) gModuleData + sizeof(module_information_t));
moduleDataStartAddress = (moduleDataStartAddress + 0x10000) & 0xFFFF0000; moduleDataStartAddress = (moduleDataStartAddress + 0x10000) & 0xFFFF0000;
ModuleData *moduleData = ModuleDataFactory::load("fs:/vol/external01/wiiu/payload.rpx", 0x00FFF000, 0x00FFF000 - ApplicationMemoryEnd, gModuleData->trampolines, DYN_LINK_TRAMPOLIN_LIST_LENGTH); std::optional<ModuleData> moduleData = ModuleDataFactory::load("fs:/vol/external01/wiiu/payload.rpx", 0x00FFF000, 0x00FFF000 - ApplicationMemoryEnd - (sizeof(module_information_t)), gModuleData->trampolines, DYN_LINK_TRAMPOLIN_LIST_LENGTH);
if (moduleData != NULL) { if (!moduleData) {
DEBUG_FUNCTION_LINE("Loaded module data"); DEBUG_FUNCTION_LINE("Loaded module data");
std::vector<RelocationData *> relocData = moduleData->getRelocationDataList(); std::vector<RelocationData> relocData = moduleData->getRelocationDataList();
if (!doRelocation(relocData, gModuleData->trampolines, DYN_LINK_TRAMPOLIN_LIST_LENGTH)) { if (!doRelocation(relocData, gModuleData->trampolines, DYN_LINK_TRAMPOLIN_LIST_LENGTH)) {
DEBUG_FUNCTION_LINE("relocations failed"); DEBUG_FUNCTION_LINE("relocations failed");
} }
@ -101,7 +100,6 @@ extern "C" int _start(int argc, char **argv) {
ICInvalidateRange((void *) 0x00800000, 0x00800000); ICInvalidateRange((void *) 0x00800000, 0x00800000);
DEBUG_FUNCTION_LINE("New entrypoint: %08X", moduleData->getEntrypoint()); DEBUG_FUNCTION_LINE("New entrypoint: %08X", moduleData->getEntrypoint());
((int (*)(int, char **)) moduleData->getEntrypoint())(argc, argv); ((int (*)(int, char **)) moduleData->getEntrypoint())(argc, argv);
delete moduleData;
} else { } else {
DEBUG_FUNCTION_LINE("Failed to load module"); DEBUG_FUNCTION_LINE("Failed to load module");
} }
@ -120,12 +118,12 @@ extern "C" int _start(int argc, char **argv) {
return 0; return 0;
} }
bool doRelocation(std::vector<RelocationData *> &relocData, relocation_trampolin_entry_t *tramp_data, uint32_t tramp_length) { bool doRelocation(const std::vector<RelocationData> &relocData, relocation_trampolin_entry_t *tramp_data, uint32_t tramp_length) {
for (auto const &curReloc : relocData) { for (auto const &curReloc : relocData) {
RelocationData *cur = curReloc; RelocationData cur = curReloc;
std::string functionName = cur->getName(); std::string functionName = cur.getName();
std::string rplName = cur->getImportRPLInformation()->getName(); std::string rplName = cur.getImportRPLInformation().getName();
int32_t isData = cur->getImportRPLInformation()->isData(); int32_t isData = cur.getImportRPLInformation().isData();
OSDynLoad_Module rplHandle = 0; OSDynLoad_Module rplHandle = 0;
OSDynLoad_Acquire(rplName.c_str(), &rplHandle); OSDynLoad_Acquire(rplName.c_str(), &rplHandle);
@ -134,7 +132,7 @@ bool doRelocation(std::vector<RelocationData *> &relocData, relocation_trampolin
if (functionAddress == 0) { if (functionAddress == 0) {
return false; return false;
} }
if (!ElfUtils::elfLinkOne(cur->getType(), cur->getOffset(), cur->getAddend(), (uint32_t) cur->getDestination(), functionAddress, tramp_data, tramp_length, RELOC_TYPE_IMPORT)) { if (!ElfUtils::elfLinkOne(cur.getType(), cur.getOffset(), cur.getAddend(), (uint32_t) cur.getDestination(), functionAddress, tramp_data, tramp_length, RELOC_TYPE_IMPORT)) {
DEBUG_FUNCTION_LINE("Relocation failed"); DEBUG_FUNCTION_LINE("Relocation failed");
return false; return false;
} }

View File

@ -18,6 +18,7 @@
#pragma once #pragma once
#include <string> #include <string>
#include <optional>
#include "utils/logger.h" #include "utils/logger.h"
class ImportRPLInformation { class ImportRPLInformation {
@ -31,7 +32,7 @@ public:
~ImportRPLInformation() { ~ImportRPLInformation() {
} }
static ImportRPLInformation *createImportRPLInformation(std::string rawSectionName) { static std::optional<ImportRPLInformation> createImportRPLInformation(std::string rawSectionName) {
std::string fimport = ".fimport_"; std::string fimport = ".fimport_";
std::string dimport = ".dimport_"; std::string dimport = ".dimport_";
@ -40,24 +41,24 @@ public:
std::string rplName = ""; std::string rplName = "";
if (rawSectionName.size() < fimport.size()) { if (rawSectionName.size() < fimport.size()) {
return NULL; return {};
} else if (std::equal(fimport.begin(), fimport.end(), rawSectionName.begin())) { } else if (std::equal(fimport.begin(), fimport.end(), rawSectionName.begin())) {
rplName = rawSectionName.substr(fimport.size()); rplName = rawSectionName.substr(fimport.size());
} else if (std::equal(dimport.begin(), dimport.end(), rawSectionName.begin())) { } else if (std::equal(dimport.begin(), dimport.end(), rawSectionName.begin())) {
rplName = rawSectionName.substr(dimport.size()); rplName = rawSectionName.substr(dimport.size());
data = true; data = true;
} else { } else {
DEBUG_FUNCTION_LINE("invalid section name"); DEBUG_FUNCTION_LINE("invalid section name\n");
return NULL; return {};
} }
return new ImportRPLInformation(rplName, data); return ImportRPLInformation(rplName, data);
} }
std::string getName() { std::string getName() const {
return name; return name;
} }
bool isData() { bool isData() const {
return _isData; return _isData;
} }

View File

@ -18,12 +18,10 @@
#include "ModuleData.h" #include "ModuleData.h"
#include "utils/StringTools.h" #include "utils/StringTools.h"
std::string ModuleData::toString() { std::string ModuleData::toString() const {
std::string res = StringTools::strfmt("Entrypoint %08X, bss: %08X (%d), bss: %08X (%d)\n", getEntrypoint(), getBSSAddr(), getBSSSize(), getSBSSAddr(), getSBSSSize()); std::string res = StringTools::strfmt("Entrypoint %08X, bss: %08X (%d), bss: %08X (%d)\n", getEntrypoint(), getBSSAddr(), getBSSSize(), getSBSSAddr(), getSBSSSize());
for (auto const &reloc : relocation_data_list) { for (auto const &reloc : relocation_data_list) {
if (reloc != NULL) { res += reloc.toString();
res += reloc->toString();
}
} }
return res; return res;
} }

View File

@ -27,11 +27,6 @@ public:
} }
~ModuleData() { ~ModuleData() {
for (auto const &reloc : relocation_data_list) {
if (reloc != NULL) {
delete reloc;
}
}
} }
void setBSSLocation(uint32_t addr, uint32_t size) { void setBSSLocation(uint32_t addr, uint32_t size) {
@ -48,38 +43,38 @@ public:
this->entrypoint = addr; this->entrypoint = addr;
} }
void addRelocationData(RelocationData *relocation_data) { void addRelocationData(const RelocationData &relocation_data) {
relocation_data_list.push_back(relocation_data); relocation_data_list.push_back(relocation_data);
} }
std::vector<RelocationData *> getRelocationDataList() { const std::vector<RelocationData> &getRelocationDataList() const {
return relocation_data_list; return relocation_data_list;
} }
uint32_t getBSSAddr() { uint32_t getBSSAddr() const {
return bssAddr; return bssAddr;
} }
uint32_t getBSSSize() { uint32_t getBSSSize() const {
return bssSize; return bssSize;
} }
uint32_t getSBSSAddr() { uint32_t getSBSSAddr() const {
return sbssAddr; return sbssAddr;
} }
uint32_t getSBSSSize() { uint32_t getSBSSSize() const {
return sbssSize; return sbssSize;
} }
uint32_t getEntrypoint() { uint32_t getEntrypoint() const {
return entrypoint; return entrypoint;
} }
std::string toString(); std::string toString() const;
private: private:
std::vector<RelocationData *> relocation_data_list; std::vector<RelocationData> relocation_data_list;
uint32_t bssAddr = 0; uint32_t bssAddr = 0;
uint32_t bssSize = 0; uint32_t bssSize = 0;

View File

@ -25,25 +25,20 @@
using namespace ELFIO; using namespace ELFIO;
ModuleData *ModuleDataFactory::load(std::string path, uint32_t destination_address, uint32_t maximum_size, relocation_trampolin_entry_t *trampolin_data, uint32_t trampolin_data_length) { std::optional<ModuleData> ModuleDataFactory::load(const std::string &path, uint32_t destination_address, uint32_t maximum_size, relocation_trampolin_entry_t *trampolin_data, uint32_t trampolin_data_length) {
elfio reader; elfio reader;
ModuleData *moduleData = new ModuleData(); ModuleData moduleData;
if (moduleData == NULL) {
return NULL;
}
// Load ELF data // Load ELF data
if (!reader.load(path)) { if (!reader.load(path)) {
DEBUG_FUNCTION_LINE("Can't find or process ELF file"); DEBUG_FUNCTION_LINE("Can't find or process %s", path.c_str());
delete moduleData; return {};
return NULL;
} }
uint32_t sec_num = reader.sections.size(); uint32_t sec_num = reader.sections.size();
uint8_t **destinations = (uint8_t **) malloc(sizeof(uint8_t *) * sec_num); uint8_t **destinations = (uint8_t **) malloc(sizeof(uint8_t *) * sec_num);
uint32_t sizeOfModule = 0; uint32_t sizeOfModule = 0;
for (uint32_t i = 0; i < sec_num; ++i) { for (uint32_t i = 0; i < sec_num; ++i) {
section *psec = reader.sections[i]; section *psec = reader.sections[i];
@ -58,7 +53,7 @@ ModuleData *ModuleDataFactory::load(std::string path, uint32_t destination_addre
if (sizeOfModule > maximum_size) { if (sizeOfModule > maximum_size) {
DEBUG_FUNCTION_LINE("Module is too big."); DEBUG_FUNCTION_LINE("Module is too big.");
return NULL; return {};
} }
uint32_t baseOffset = (destination_address - sizeOfModule) & 0xFFFFFF00; uint32_t baseOffset = (destination_address - sizeOfModule) & 0xFFFFFF00;
@ -97,8 +92,7 @@ ModuleData *ModuleDataFactory::load(std::string path, uint32_t destination_addre
} else { } else {
DEBUG_FUNCTION_LINE("Unhandled case"); DEBUG_FUNCTION_LINE("Unhandled case");
free(destinations); free(destinations);
delete moduleData; return {};
return NULL;
} }
const char *p = reader.sections[i]->get_data(); const char *p = reader.sections[i]->get_data();
@ -113,10 +107,10 @@ ModuleData *ModuleDataFactory::load(std::string path, uint32_t destination_addre
//nextAddress = ROUNDUP(destination + sectionSize,0x100); //nextAddress = ROUNDUP(destination + sectionSize,0x100);
if (psec->get_name().compare(".bss") == 0) { if (psec->get_name().compare(".bss") == 0) {
moduleData->setBSSLocation(destination, sectionSize); moduleData.setBSSLocation(destination, sectionSize);
DEBUG_FUNCTION_LINE("Saved %s section info. Location: %08X size: %08X", psec->get_name().c_str(), destination, sectionSize); DEBUG_FUNCTION_LINE("Saved %s section info. Location: %08X size: %08X", psec->get_name().c_str(), destination, sectionSize);
} else if (psec->get_name().compare(".sbss") == 0) { } else if (psec->get_name().compare(".sbss") == 0) {
moduleData->setSBSSLocation(destination, sectionSize); moduleData.setSBSSLocation(destination, sectionSize);
DEBUG_FUNCTION_LINE("Saved %s section info. Location: %08X size: %08X", psec->get_name().c_str(), destination, sectionSize); DEBUG_FUNCTION_LINE("Saved %s section info. Location: %08X size: %08X", psec->get_name().c_str(), destination, sectionSize);
} }
totalSize += sectionSize; totalSize += sectionSize;
@ -133,31 +127,30 @@ ModuleData *ModuleDataFactory::load(std::string path, uint32_t destination_addre
if (!linkSection(reader, psec->get_index(), (uint32_t) destinations[psec->get_index()], offset_text, offset_data, trampolin_data, trampolin_data_length)) { if (!linkSection(reader, psec->get_index(), (uint32_t) destinations[psec->get_index()], offset_text, offset_data, trampolin_data, trampolin_data_length)) {
DEBUG_FUNCTION_LINE("elfLink failed"); DEBUG_FUNCTION_LINE("elfLink failed");
free(destinations); free(destinations);
delete moduleData; return {};
return NULL;
} }
} }
} }
std::vector<RelocationData *> relocationData = getImportRelocationData(reader, destinations); std::vector<RelocationData> relocationData = getImportRelocationData(reader, destinations);
for (auto const &reloc : relocationData) { for (auto const &reloc : relocationData) {
moduleData->addRelocationData(reloc); moduleData.addRelocationData(reloc);
} }
DCFlushRange((void *) destination_address, totalSize); DCFlushRange((void *) baseOffset, totalSize);
ICInvalidateRange((void *) destination_address, totalSize); ICInvalidateRange((void *) baseOffset, totalSize);
free(destinations); free(destinations);
moduleData->setEntrypoint(entrypoint); moduleData.setEntrypoint(entrypoint);
DEBUG_FUNCTION_LINE("Saved entrypoint as %08X", entrypoint); DEBUG_FUNCTION_LINE("Saved entrypoint as %08X", entrypoint);
return moduleData; return moduleData;
} }
std::vector<RelocationData *> ModuleDataFactory::getImportRelocationData(elfio &reader, uint8_t **destinations) { std::vector<RelocationData> ModuleDataFactory::getImportRelocationData(const elfio &reader, uint8_t **destinations) {
std::vector<RelocationData *> result; std::vector<RelocationData> result;
std::map<uint32_t, std::string> infoMap; std::map<uint32_t, std::string> infoMap;
uint32_t sec_num = reader.sections.size(); uint32_t sec_num = reader.sections.size();
@ -191,8 +184,8 @@ std::vector<RelocationData *> ModuleDataFactory::getImportRelocationData(elfio &
if (adjusted_sym_value < 0xC0000000) { if (adjusted_sym_value < 0xC0000000) {
continue; continue;
} }
ImportRPLInformation *rplInfo = ImportRPLInformation::createImportRPLInformation(infoMap[sym_section_index]); std::optional<ImportRPLInformation> rplInfo = ImportRPLInformation::createImportRPLInformation(infoMap[sym_section_index]);
if (rplInfo == NULL) { if (!rplInfo) {
DEBUG_FUNCTION_LINE("Failed to create import information"); DEBUG_FUNCTION_LINE("Failed to create import information");
break; break;
} }
@ -200,7 +193,7 @@ std::vector<RelocationData *> ModuleDataFactory::getImportRelocationData(elfio &
uint32_t section_index = psec->get_info(); uint32_t section_index = psec->get_info();
// When these relocations are performed, we don't need the 0xC0000000 offset anymore. // When these relocations are performed, we don't need the 0xC0000000 offset anymore.
RelocationData *relocationData = new RelocationData(type, offset - 0x02000000, addend, (void *) (destinations[section_index] + 0x02000000), sym_name, rplInfo); RelocationData relocationData(type, offset - 0x02000000, addend, (void *) (destinations[section_index] + 0x02000000), sym_name, rplInfo.value());
//relocationData->printInformation(); //relocationData->printInformation();
result.push_back(relocationData); result.push_back(relocationData);
} }
@ -209,7 +202,7 @@ std::vector<RelocationData *> ModuleDataFactory::getImportRelocationData(elfio &
return result; return result;
} }
bool ModuleDataFactory::linkSection(elfio &reader, uint32_t section_index, uint32_t destination, uint32_t base_text, uint32_t base_data, relocation_trampolin_entry_t *trampolin_data, uint32_t trampolin_data_length) { bool ModuleDataFactory::linkSection(const elfio &reader, uint32_t section_index, uint32_t destination, uint32_t base_text, uint32_t base_data, relocation_trampolin_entry_t *trampolin_data, uint32_t trampolin_data_length) {
uint32_t sec_num = reader.sections.size(); uint32_t sec_num = reader.sections.size();
for (uint32_t i = 0; i < sec_num; ++i) { for (uint32_t i = 0; i < sec_num; ++i) {

View File

@ -26,9 +26,9 @@
class ModuleDataFactory { class ModuleDataFactory {
public: public:
static ModuleData *load(std::string path, uint32_t destination_address, uint32_t maximum_size, relocation_trampolin_entry_t *trampolin_data, uint32_t trampolin_data_length); static std::optional<ModuleData> load(const std::string &path, uint32_t destination_address, uint32_t maximum_size, relocation_trampolin_entry_t *trampolin_data, uint32_t trampolin_data_length);
static bool linkSection(ELFIO::elfio &reader, uint32_t section_index, uint32_t destination, uint32_t base_text, uint32_t base_data, relocation_trampolin_entry_t *trampolin_data, uint32_t trampolin_data_length); static bool linkSection(const ELFIO::elfio &reader, uint32_t section_index, uint32_t destination, uint32_t base_text, uint32_t base_data, relocation_trampolin_entry_t *trampolin_data, uint32_t trampolin_data_length);
static std::vector<RelocationData *> getImportRelocationData(ELFIO::elfio &reader, uint8_t **destinations); static std::vector<RelocationData> getImportRelocationData(const ELFIO::elfio &reader, uint8_t **destinations);
}; };

View File

@ -18,6 +18,6 @@
#include "RelocationData.h" #include "RelocationData.h"
#include "utils/StringTools.h" #include "utils/StringTools.h"
std::string RelocationData::toString() { std::string RelocationData::toString() const {
return StringTools::strfmt("%s destination: %08X offset: %08X type: %02X addend: %d rplName: %s isData: %d", name.c_str(), destination, offset, type, addend, rplInfo->getName().c_str(), rplInfo->isData()); return StringTools::strfmt("%s destination: %08X offset: %08X type: %02X addend: %d rplName: %s isData: %d \n", name.c_str(), destination, offset, type, addend, rplInfo.getName().c_str(), rplInfo.isData());
} }

View File

@ -23,46 +23,42 @@
class RelocationData { class RelocationData {
public: public:
RelocationData(char type, size_t offset, int32_t addend, void *destination, std::string name, ImportRPLInformation *rplInfo) { RelocationData(char type, size_t offset, int32_t addend, void *destination, const std::string &name, const ImportRPLInformation &rplInfo) : rplInfo(rplInfo) {
this->type = type; this->type = type;
this->offset = offset; this->offset = offset;
this->addend = addend; this->addend = addend;
this->destination = destination; this->destination = destination;
this->name = name; this->name = name;
this->rplInfo = rplInfo;
} }
~RelocationData() { ~RelocationData() {
if (rplInfo != NULL) {
delete rplInfo;
}
} }
char getType() { char getType() const {
return type; return type;
} }
size_t getOffset() { size_t getOffset() const {
return offset; return offset;
} }
int32_t getAddend() { int32_t getAddend() const {
return addend; return addend;
} }
void *getDestination() { void *getDestination() const {
return destination; return destination;
} }
std::string getName() { std::string getName() const {
return name; return name;
} }
ImportRPLInformation *getImportRPLInformation() { ImportRPLInformation getImportRPLInformation() const {
return rplInfo; return rplInfo;
} }
std::string toString(); std::string toString() const;
private: private:
char type; char type;
@ -70,5 +66,5 @@ private:
int32_t addend; int32_t addend;
void *destination; void *destination;
std::string name; std::string name;
ImportRPLInformation *rplInfo; const ImportRPLInformation rplInfo;
}; };