- Use the(de)-init functions from libwut to handle logging, sd access and memory.

- Adapt logging to WHBLogPrintf
This commit is contained in:
Maschell 2020-05-28 20:53:27 +02:00
parent 8c1c3e91e3
commit a941429f88
18 changed files with 105 additions and 1372 deletions

3
.gitignore vendored
View File

@ -5,3 +5,6 @@
*.bin *.bin
*.cscope_file_list *.cscope_file_list
*.layout *.layout
cmake-build-debug/
.idea/
CMakeLists.txt

View File

@ -47,7 +47,7 @@ CFLAGS := -std=gnu11 -mcpu=750 -meabi -mhard-float -ffast-math \
CXXFLAGS := -std=gnu++11 -mcpu=750 -meabi -mhard-float -ffast-math \ CXXFLAGS := -std=gnu++11 -mcpu=750 -meabi -mhard-float -ffast-math \
-O2 -Wall -Wextra -Wno-unused-parameter -Wno-strict-aliasing $(INCLUDE) -O2 -Wall -Wextra -Wno-unused-parameter -Wno-strict-aliasing $(INCLUDE)
ASFLAGS := -mregnames ASFLAGS := -mregnames
LDFLAGS := -nostartfiles -Wl,--gc-sections LDFLAGS := -nostartfiles -Wl,--gc-sections,--allow-multiple-definition
#--------------------------------------------------------------------------------- #---------------------------------------------------------------------------------
Q := @ Q := @
@ -55,7 +55,7 @@ MAKEFLAGS += --no-print-directory
#--------------------------------------------------------------------------------- #---------------------------------------------------------------------------------
# any extra libraries we wish to link with the project # any extra libraries we wish to link with the project
#--------------------------------------------------------------------------------- #---------------------------------------------------------------------------------
LIBS := LIBS := -lwut
#--------------------------------------------------------------------------------- #---------------------------------------------------------------------------------
# list of directories containing libraries, this must be the top level containing # list of directories containing libraries, this must be the top level containing
@ -106,8 +106,7 @@ export OFILES := $(CPPFILES:.cpp=.o) $(CFILES:.c=.o) \
#--------------------------------------------------------------------------------- #---------------------------------------------------------------------------------
export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \ export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \
$(foreach dir,$(LIBDIRS),-I$(dir)/include) \ $(foreach dir,$(LIBDIRS),-I$(dir)/include) \
-I$(CURDIR)/$(BUILD) -I$(LIBOGC_INC) \ -I$(CURDIR)/$(BUILD) -I$(PORTLIBS)/include
-I$(PORTLIBS)/include -I$(PORTLIBS)/include/freetype2
#--------------------------------------------------------------------------------- #---------------------------------------------------------------------------------
# build a list of library paths # build a list of library paths

View File

@ -39,7 +39,7 @@ bool ElfUtils::elfLinkOne(char type, size_t offset, int32_t addend, uint32_t des
*((uint16_t *)(target)) = static_cast<uint16_t>((value + 0x8000) >> 16); *((uint16_t *)(target)) = static_cast<uint16_t>((value + 0x8000) >> 16);
break; break;
case R_PPC_DTPMOD32: case R_PPC_DTPMOD32:
DEBUG_FUNCTION_LINE("################IMPLEMENT ME\n"); DEBUG_FUNCTION_LINE("################IMPLEMENT ME");
//*((int32_t *)(target)) = tlsModuleIndex; //*((int32_t *)(target)) = tlsModuleIndex;
break; break;
case R_PPC_DTPREL32: case R_PPC_DTPREL32:
@ -57,18 +57,18 @@ bool ElfUtils::elfLinkOne(char type, size_t offset, int32_t addend, uint32_t des
case R_PPC_REL14: { case R_PPC_REL14: {
auto distance = static_cast<int32_t>(value) - static_cast<int32_t>(target); auto distance = static_cast<int32_t>(value) - static_cast<int32_t>(target);
if (distance > 0x7FFC || distance < -0x7FFC) { if (distance > 0x7FFC || distance < -0x7FFC) {
DEBUG_FUNCTION_LINE("***14-bit relative branch cannot hit target.\n"); DEBUG_FUNCTION_LINE("***14-bit relative branch cannot hit target.");
return false; return false;
} }
if (distance & 3) { if (distance & 3) {
DEBUG_FUNCTION_LINE("***RELOC ERROR %d: lower 2 bits must be zero before shifting.\n", -470040); DEBUG_FUNCTION_LINE("***RELOC ERROR %d: lower 2 bits must be zero before shifting.", -470040);
return false; return false;
} }
if ((distance >= 0 && (distance & 0xFFFF8000)) || if ((distance >= 0 && (distance & 0xFFFF8000)) ||
(distance < 0 && ((distance & 0xFFFF8000) != 0xFFFF8000))) { (distance < 0 && ((distance & 0xFFFF8000) != 0xFFFF8000))) {
DEBUG_FUNCTION_LINE("***RELOC ERROR %d: upper 17 bits before shift must all be the same.\n", -470040); DEBUG_FUNCTION_LINE("***RELOC ERROR %d: upper 17 bits before shift must all be the same.", -470040);
return false; return false;
} }
@ -83,8 +83,8 @@ bool ElfUtils::elfLinkOne(char type, size_t offset, int32_t addend, uint32_t des
auto distance = static_cast<int32_t>(value) - static_cast<int32_t>(target); auto distance = static_cast<int32_t>(value) - static_cast<int32_t>(target);
if (distance > 0x1FFFFFC || distance < -0x1FFFFFC) { if (distance > 0x1FFFFFC || distance < -0x1FFFFFC) {
if(trampolin_data == NULL) { if(trampolin_data == NULL) {
DEBUG_FUNCTION_LINE("***24-bit relative branch cannot hit target. Trampolin isn't provided\n"); DEBUG_FUNCTION_LINE("***24-bit relative branch cannot hit target. Trampolin isn't provided");
DEBUG_FUNCTION_LINE("***value %08X - target %08X = distance %08X\n", value, target, distance); DEBUG_FUNCTION_LINE("***value %08X - target %08X = distance %08X", value, target, distance);
return false; return false;
} else { } else {
relocation_trampolin_entry_t * freeSlot = NULL; relocation_trampolin_entry_t * freeSlot = NULL;
@ -102,13 +102,13 @@ bool ElfUtils::elfLinkOne(char type, size_t offset, int32_t addend, uint32_t des
} }
} }
if(freeSlot != NULL) { if(freeSlot != NULL) {
DEBUG_FUNCTION_LINE("***24-bit relative branch cannot hit target. Trampolin data list is full\n"); DEBUG_FUNCTION_LINE("***24-bit relative branch cannot hit target. Trampolin data list is full");
DEBUG_FUNCTION_LINE("***value %08X - target %08X = distance %08X\n", value, target, distance); DEBUG_FUNCTION_LINE("***value %08X - target %08X = distance %08X", value, target, distance);
return false; return false;
} }
if(target - (uint32_t)&(freeSlot->trampolin[0]) > 0x1FFFFFC) { if(target - (uint32_t)&(freeSlot->trampolin[0]) > 0x1FFFFFC) {
DEBUG_FUNCTION_LINE("**Cannot link 24-bit jump (too far to tramp buffer).\n"); DEBUG_FUNCTION_LINE("**Cannot link 24-bit jump (too far to tramp buffer).");
DEBUG_FUNCTION_LINE("***value %08X - target %08X = distance %08X\n", value, target, distance); DEBUG_FUNCTION_LINE("***value %08X - target %08X = distance %08X", value, target, distance);
return false; return false;
} }
@ -128,22 +128,22 @@ bool ElfUtils::elfLinkOne(char type, size_t offset, int32_t addend, uint32_t des
uint32_t symbolValue = (uint32_t)&(freeSlot->trampolin[0]); uint32_t symbolValue = (uint32_t)&(freeSlot->trampolin[0]);
value = symbolValue + addend; value = symbolValue + addend;
distance = static_cast<int32_t>(value) - static_cast<int32_t>(target); distance = static_cast<int32_t>(value) - static_cast<int32_t>(target);
DEBUG_FUNCTION_LINE("Created tramp\n"); DEBUG_FUNCTION_LINE("Created tramp");
} }
} }
if (distance & 3) { if (distance & 3) {
DEBUG_FUNCTION_LINE("***RELOC ERROR %d: lower 2 bits must be zero before shifting.\n", -470022); DEBUG_FUNCTION_LINE("***RELOC ERROR %d: lower 2 bits must be zero before shifting.", -470022);
return false; return false;
} }
if (distance < 0 && (distance & 0xFE000000) != 0xFE000000) { if (distance < 0 && (distance & 0xFE000000) != 0xFE000000) {
DEBUG_FUNCTION_LINE("***RELOC ERROR %d: upper 7 bits before shift must all be the same (1).\n", -470040); DEBUG_FUNCTION_LINE("***RELOC ERROR %d: upper 7 bits before shift must all be the same (1).", -470040);
return false; return false;
} }
if (distance >= 0 && (distance & 0xFE000000)) { if (distance >= 0 && (distance & 0xFE000000)) {
DEBUG_FUNCTION_LINE("***RELOC ERROR %d: upper 7 bits before shift must all be the same (0).\n", -470040); DEBUG_FUNCTION_LINE("***RELOC ERROR %d: upper 7 bits before shift must all be the same (0).", -470040);
return false; return false;
} }
@ -151,7 +151,7 @@ bool ElfUtils::elfLinkOne(char type, size_t offset, int32_t addend, uint32_t des
break; break;
} }
default: default:
DEBUG_FUNCTION_LINE("***ERROR: Unsupported Relocation_Add Type (%08X):\n", type); DEBUG_FUNCTION_LINE("***ERROR: Unsupported Relocation_Add Type (%08X):", type);
return false; return false;
} }
return true; return true;

View File

@ -17,10 +17,9 @@
#define EXPORT_VAR(type, var) type var __attribute__((section(".data"))); #define EXPORT_VAR(type, var) type var __attribute__((section(".data")));
EXPORT_VAR(uint32_t *, pMEMAllocFromDefaultHeapEx); EXPORT_VAR(uint32_t *, MEMAllocFromDefaultHeap);
EXPORT_VAR(uint32_t *, pMEMAllocFromDefaultHeap); EXPORT_VAR(uint32_t *, MEMAllocFromDefaultHeapEx);
EXPORT_VAR(uint32_t *, pMEMFreeToDefaultHeap); EXPORT_VAR(uint32_t *, MEMFreeToDefaultHeap);
void InitFunctionPointers(void) { void InitFunctionPointers(void) {
OSDynLoad_Module handle; OSDynLoad_Module handle;
@ -28,10 +27,17 @@ void InitFunctionPointers(void) {
addr_OSDynLoad_FindExport = (void*) 0x0102B828; addr_OSDynLoad_FindExport = (void*) 0x0102B828;
OSDynLoad_Acquire("coreinit.rpl", &handle); OSDynLoad_Acquire("coreinit.rpl", &handle);
OSDynLoad_FindExport(handle, 1, "MEMAllocFromDefaultHeapEx", (void**) &pMEMAllocFromDefaultHeapEx);
OSDynLoad_FindExport(handle, 1, "MEMAllocFromDefaultHeap", (void**) &pMEMAllocFromDefaultHeap); uint32_t** value = 0;
OSDynLoad_FindExport(handle, 1, "MEMFreeToDefaultHeap", (void**) &pMEMFreeToDefaultHeap); OSDynLoad_FindExport(handle, 1, "MEMAllocFromDefaultHeap", (void**) &value);
MEMAllocFromDefaultHeap = *value;
OSDynLoad_FindExport(handle, 1, "MEMAllocFromDefaultHeapEx", (void**) &value);
MEMAllocFromDefaultHeapEx = *value;
OSDynLoad_FindExport(handle, 1, "MEMFreeToDefaultHeap", (void**) &value);
MEMFreeToDefaultHeap = *value;
#include "imports.h" #include "imports.h"
// override failed __rplwrap_exit find export
OSDynLoad_FindExport(handle, 0, "exit", (void**) &addr___rplwrap_exit);
} }

View File

@ -283,7 +283,7 @@ class section_impl : public section
ret = inflate(&s, Z_FINISH); ret = inflate(&s, Z_FINISH);
if (ret != Z_OK && ret != Z_STREAM_END){ if (ret != Z_OK && ret != Z_STREAM_END){
DEBUG_FUNCTION_LINE("NOOOO\n"); DEBUG_FUNCTION_LINE("NOOOO");
} }
inflateEnd(&s); inflateEnd(&s);
@ -301,7 +301,7 @@ class section_impl : public section
} }
}else{ }else{
set_size(0); set_size(0);
DEBUG_FUNCTION_LINE("Failed to allocate memory.\n"); DEBUG_FUNCTION_LINE("Failed to allocate memory.");
} }
} }
} }

View File

@ -104,6 +104,10 @@ IMPORT(FSRename);
IMPORT(FSGetMountSource); IMPORT(FSGetMountSource);
IMPORT(FSMount); IMPORT(FSMount);
IMPORT(FSUnmount); IMPORT(FSUnmount);
IMPORT(FSChangeMode);
IMPORT(FSGetPosFile);
IMPORT(OSTicksToCalendarTime);
IMPORT(__rplwrap_exit);
IMPORT(IOS_Open); IMPORT(IOS_Open);
IMPORT(IOS_Close); IMPORT(IOS_Close);

View File

@ -11,13 +11,14 @@
#include "module/ModuleDataFactory.h" #include "module/ModuleDataFactory.h"
#include "common/module_defines.h" #include "common/module_defines.h"
#include <whb/log.h>
#include <whb/log_udp.h>
#include "main.h" #include "main.h"
#include "kernel.h" #include "kernel.h"
#include "dynamic.h" #include "dynamic.h"
#include "utils/logger.h" #include "utils/logger.h"
#include "utils/utils.h" #include "utils/utils.h"
#include "utils/sd_fat_devoptab.h"
bool doRelocation(std::vector<RelocationData *> &relocData, relocation_trampolin_entry_t * tramp_data, uint32_t tramp_length); bool doRelocation(std::vector<RelocationData *> &relocData, relocation_trampolin_entry_t * tramp_data, uint32_t tramp_length);
@ -41,66 +42,66 @@ bool CheckRunning() {
return true; return true;
} }
extern "C" void __init_wut();
extern "C" void __fini_wut();
extern "C" int _start(int argc, char **argv) { extern "C" int _start(int argc, char **argv) {
doKernelSetup(); doKernelSetup();
InitFunctionPointers(); InitFunctionPointers();
doKernelSetup2(); doKernelSetup2();
socket_lib_init(); __init_wut();
log_init();
WHBLogUdpInit();
int res = 0; int res = 0;
if((res = mount_sd_fat("sd")) >= 0) {
DEBUG_FUNCTION_LINE("Mounted sd card\n");
uint32_t ApplicationMemoryEnd; uint32_t ApplicationMemoryEnd;
asm volatile("lis %0, __CODE_END@h; ori %0, %0, __CODE_END@l" : "=r" (ApplicationMemoryEnd)); asm volatile("lis %0, __CODE_END@h; ori %0, %0, __CODE_END@l" : "=r" (ApplicationMemoryEnd));
ApplicationMemoryEnd = (ApplicationMemoryEnd + 0x100) & 0xFFFFFF00; ApplicationMemoryEnd = (ApplicationMemoryEnd + 0x100) & 0xFFFFFF00;
module_information_t * gModuleData = (module_information_t *) ApplicationMemoryEnd; module_information_t * gModuleData = (module_information_t *) ApplicationMemoryEnd;
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("sd:/wiiu/payload.rpx", 0x00FFF000, 0x00FFF000 - ApplicationMemoryEnd, gModuleData->trampolines, DYN_LINK_TRAMPOLIN_LIST_LENGTH); ModuleData * moduleData = ModuleDataFactory::load("fs:/vol/external01/wiiu/payload.rpx", 0x00FFF000, 0x00FFF000 - ApplicationMemoryEnd, gModuleData->trampolines, DYN_LINK_TRAMPOLIN_LIST_LENGTH);
if(moduleData != NULL) { if(moduleData != NULL) {
DEBUG_FUNCTION_LINE("Loaded module data\n"); 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\n"); DEBUG_FUNCTION_LINE("relocations failed");
}
if(moduleData->getBSSAddr() != 0) {
DEBUG_FUNCTION_LINE("memset .bss %08X (%d)\n", moduleData->getBSSAddr(), moduleData->getBSSSize());
memset((void*)moduleData->getBSSAddr(), 0, moduleData->getBSSSize());
}
if(moduleData->getSBSSAddr() != 0) {
DEBUG_FUNCTION_LINE("memset .sbss %08X (%d)\n", moduleData->getSBSSAddr(), moduleData->getSBSSSize());
memset((void*)moduleData->getSBSSAddr(), 0, moduleData->getSBSSSize());
}
DCFlushRange((void*)0x00800000, 0x00800000);
ICInvalidateRange((void*)0x00800000, 0x00800000);
DEBUG_FUNCTION_LINE("New entrypoint: %08X\n", moduleData->getEntrypoint());
((int (*)(int, char **))moduleData->getEntrypoint())(argc, argv);
delete moduleData;
} else {
DEBUG_FUNCTION_LINE("Failed to load module\n");
} }
if(moduleData->getBSSAddr() != 0) {
DEBUG_FUNCTION_LINE("memset .bss %08X (%d)", moduleData->getBSSAddr(), moduleData->getBSSSize());
memset((void*)moduleData->getBSSAddr(), 0, moduleData->getBSSSize());
}
if(moduleData->getSBSSAddr() != 0) {
DEBUG_FUNCTION_LINE("memset .sbss %08X (%d)", moduleData->getSBSSAddr(), moduleData->getSBSSSize());
memset((void*)moduleData->getSBSSAddr(), 0, moduleData->getSBSSSize());
}
DCFlushRange((void*)0x00800000, 0x00800000);
ICInvalidateRange((void*)0x00800000, 0x00800000);
DEBUG_FUNCTION_LINE("New entrypoint: %08X", moduleData->getEntrypoint());
((int (*)(int, char **))moduleData->getEntrypoint())(argc, argv);
delete moduleData;
} else { } else {
DEBUG_FUNCTION_LINE("Mounted sd card failed %d.\n", res); DEBUG_FUNCTION_LINE("Failed to load module");
} }
SYSLaunchMenu(); SYSLaunchMenu();
ProcUIInit(OSSavesDone_ReadyToRelease); ProcUIInit(OSSavesDone_ReadyToRelease);
DEBUG_FUNCTION_LINE("In ProcUI loop\n"); DEBUG_FUNCTION_LINE("In ProcUI loop");
while(CheckRunning()) { while(CheckRunning()) {
// wait. // wait.
OSSleepTicks(OSMillisecondsToTicks(100)); OSSleepTicks(OSMillisecondsToTicks(100));
} }
ProcUIShutdown(); ProcUIShutdown();
__fini_wut();
return 0; return 0;
} }
@ -119,7 +120,7 @@ bool doRelocation(std::vector<RelocationData *> &relocData, relocation_trampolin
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\n"); DEBUG_FUNCTION_LINE("Relocation failed");
return false; return false;
} }
} }

View File

@ -18,7 +18,7 @@ dyn_linking_function_t * DynamicLinkingHelper::getOrAddFunctionEntryByName(dyn_l
dyn_linking_function_t * curEntry = &(data->functions[i]); dyn_linking_function_t * curEntry = &(data->functions[i]);
if(strlen(curEntry->functionName) == 0) { if(strlen(curEntry->functionName) == 0) {
if(strlen(functionName) > DYN_LINK_FUNCTION_NAME_LENGTH) { if(strlen(functionName) > DYN_LINK_FUNCTION_NAME_LENGTH) {
DEBUG_FUNCTION_LINE("Failed to add function name, it's too long.\n"); DEBUG_FUNCTION_LINE("Failed to add function name, it's too long.");
return NULL; return NULL;
} }
strncpy(curEntry->functionName,functionName,DYN_LINK_FUNCTION_NAME_LENGTH); strncpy(curEntry->functionName,functionName,DYN_LINK_FUNCTION_NAME_LENGTH);
@ -50,7 +50,7 @@ dyn_linking_import_t * DynamicLinkingHelper::getOrAddImport(dyn_linking_relocati
dyn_linking_import_t * curEntry = &(data->imports[i]); dyn_linking_import_t * curEntry = &(data->imports[i]);
if(strlen(curEntry->importName) == 0) { if(strlen(curEntry->importName) == 0) {
if(strlen(importName) > DYN_LINK_IMPORT_NAME_LENGTH) { if(strlen(importName) > DYN_LINK_IMPORT_NAME_LENGTH) {
DEBUG_FUNCTION_LINE("Failed to add Import, it's too long.\n"); DEBUG_FUNCTION_LINE("Failed to add Import, it's too long.");
return NULL; return NULL;
} }
strncpy(curEntry->importName,importName,DYN_LINK_IMPORT_NAME_LENGTH); strncpy(curEntry->importName,importName,DYN_LINK_IMPORT_NAME_LENGTH);
@ -72,13 +72,13 @@ bool DynamicLinkingHelper::addReloationEntry(dyn_linking_relocation_data_t * lin
bool DynamicLinkingHelper::addReloationEntry(dyn_linking_relocation_data_t * linking_data, dyn_linking_relocation_entry_t * linking_entries, uint32_t linking_entry_length, char type, size_t offset, int32_t addend, void *destination, std::string name, ImportRPLInformation * rplInfo) { bool DynamicLinkingHelper::addReloationEntry(dyn_linking_relocation_data_t * linking_data, dyn_linking_relocation_entry_t * linking_entries, uint32_t linking_entry_length, char type, size_t offset, int32_t addend, void *destination, std::string name, ImportRPLInformation * rplInfo) {
dyn_linking_import_t * importInfoGbl = DynamicLinkingHelper::getOrAddImport(linking_data, rplInfo->getName().c_str(),rplInfo->isData()); dyn_linking_import_t * importInfoGbl = DynamicLinkingHelper::getOrAddImport(linking_data, rplInfo->getName().c_str(),rplInfo->isData());
if(importInfoGbl == NULL) { if(importInfoGbl == NULL) {
DEBUG_FUNCTION_LINE("Getting import info failed. Probably maximum of %d rpl files to import reached.\n",DYN_LINK_IMPORT_LIST_LENGTH); DEBUG_FUNCTION_LINE("Getting import info failed. Probably maximum of %d rpl files to import reached.",DYN_LINK_IMPORT_LIST_LENGTH);
return false; return false;
} }
dyn_linking_function_t * functionInfo = DynamicLinkingHelper::getOrAddFunctionEntryByName(linking_data, name.c_str()); dyn_linking_function_t * functionInfo = DynamicLinkingHelper::getOrAddFunctionEntryByName(linking_data, name.c_str());
if(functionInfo == NULL) { if(functionInfo == NULL) {
DEBUG_FUNCTION_LINE("Getting import info failed. Probably maximum of %d function to be relocated reached.\n",DYN_LINK_FUNCTION_LIST_LENGTH); DEBUG_FUNCTION_LINE("Getting import info failed. Probably maximum of %d function to be relocated reached.",DYN_LINK_FUNCTION_LIST_LENGTH);
return false; return false;
} }

View File

@ -47,7 +47,7 @@ public:
rplName = rawSectionName.substr(dimport.size()); rplName = rawSectionName.substr(dimport.size());
data = true; data = true;
} else { } else {
DEBUG_FUNCTION_LINE("invalid section name\n"); DEBUG_FUNCTION_LINE("invalid section name");
return NULL; return NULL;
} }
return new ImportRPLInformation(rplName, data); return new ImportRPLInformation(rplName, data);

View File

@ -35,7 +35,7 @@ ModuleData * ModuleDataFactory::load(std::string path, uint32_t destination_addr
// Load ELF data // Load ELF data
if (!reader.load(path)) { if (!reader.load(path)) {
DEBUG_FUNCTION_LINE("Can't find or process ELF file\n"); DEBUG_FUNCTION_LINE("Can't find or process ELF file");
delete moduleData; delete moduleData;
return NULL; return NULL;
} }
@ -96,7 +96,7 @@ ModuleData * ModuleDataFactory::load(std::string path, uint32_t destination_addr
destination -= 0xC0000000; destination -= 0xC0000000;
destinations[psec->get_index()] -= 0xC0000000; destinations[psec->get_index()] -= 0xC0000000;
} else { } else {
DEBUG_FUNCTION_LINE("Unhandled case\n"); DEBUG_FUNCTION_LINE("Unhandled case");
free(destinations); free(destinations);
delete moduleData; delete moduleData;
return NULL; return NULL;
@ -105,20 +105,20 @@ ModuleData * ModuleDataFactory::load(std::string path, uint32_t destination_addr
const char* p = reader.sections[i]->get_data(); const char* p = reader.sections[i]->get_data();
if(psec->get_type() == SHT_NOBITS) { if(psec->get_type() == SHT_NOBITS) {
DEBUG_FUNCTION_LINE("memset section %s %08X [%08X] to 0 (%d bytes)\n", psec->get_name().c_str(), destination, destination + sectionSize, sectionSize); DEBUG_FUNCTION_LINE("memset section %s %08X [%08X] to 0 (%d bytes)", psec->get_name().c_str(), destination, destination + sectionSize, sectionSize);
memset((void*) destination, 0, sectionSize); memset((void*) destination, 0, sectionSize);
} else if(psec->get_type() == SHT_PROGBITS) { } else if(psec->get_type() == SHT_PROGBITS) {
DEBUG_FUNCTION_LINE("Copy section %s %08X -> %08X [%08X] (%d bytes)\n", psec->get_name().c_str(), p, destination, destination + sectionSize, sectionSize); DEBUG_FUNCTION_LINE("Copy section %s %08X -> %08X [%08X] (%d bytes)", psec->get_name().c_str(), p, destination, destination + sectionSize, sectionSize);
memcpy((void*) destination, p, sectionSize); memcpy((void*) destination, p, sectionSize);
} }
//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\n", 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\n", 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;
@ -130,9 +130,9 @@ ModuleData * ModuleDataFactory::load(std::string path, uint32_t destination_addr
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];
if ((psec->get_type() == SHT_PROGBITS || psec->get_type() == SHT_NOBITS) && (psec->get_flags() & SHF_ALLOC)) { if ((psec->get_type() == SHT_PROGBITS || psec->get_type() == SHT_NOBITS) && (psec->get_flags() & SHF_ALLOC)) {
DEBUG_FUNCTION_LINE("Linking (%d)... %s\n",i,psec->get_name().c_str()); DEBUG_FUNCTION_LINE("Linking (%d)... %s",i,psec->get_name().c_str());
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\n"); DEBUG_FUNCTION_LINE("elfLink failed");
free(destinations); free(destinations);
delete moduleData; delete moduleData;
return NULL; return NULL;
@ -151,7 +151,7 @@ ModuleData * ModuleDataFactory::load(std::string path, uint32_t destination_addr
free(destinations); free(destinations);
moduleData->setEntrypoint(entrypoint); moduleData->setEntrypoint(entrypoint);
DEBUG_FUNCTION_LINE("Saved entrypoint as %08X\n", entrypoint); DEBUG_FUNCTION_LINE("Saved entrypoint as %08X", entrypoint);
return moduleData; return moduleData;
} }
@ -173,7 +173,7 @@ std::vector<RelocationData*> ModuleDataFactory::getImportRelocationData(elfio& r
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];
if(psec->get_type() == SHT_RELA || psec->get_type() == SHT_REL) { if(psec->get_type() == SHT_RELA || psec->get_type() == SHT_REL) {
DEBUG_FUNCTION_LINE("Found relocation section %s\n",psec->get_name().c_str()); DEBUG_FUNCTION_LINE("Found relocation section %s",psec->get_name().c_str());
relocation_section_accessor rel(reader, psec); relocation_section_accessor rel(reader, psec);
for ( uint32_t j = 0; j < (uint32_t) rel.get_entries_num(); ++j ) { for ( uint32_t j = 0; j < (uint32_t) rel.get_entries_num(); ++j ) {
Elf64_Addr offset; Elf64_Addr offset;
@ -184,7 +184,7 @@ std::vector<RelocationData*> ModuleDataFactory::getImportRelocationData(elfio& r
Elf_Half sym_section_index; Elf_Half sym_section_index;
if(!rel.get_entry(j, offset, sym_value, sym_name, type, addend, sym_section_index)) { if(!rel.get_entry(j, offset, sym_value, sym_name, type, addend, sym_section_index)) {
DEBUG_FUNCTION_LINE("Failed to get relocation\n"); DEBUG_FUNCTION_LINE("Failed to get relocation");
break; break;
} }
@ -194,7 +194,7 @@ std::vector<RelocationData*> ModuleDataFactory::getImportRelocationData(elfio& r
} }
ImportRPLInformation * rplInfo = ImportRPLInformation::createImportRPLInformation(infoMap[sym_section_index]); ImportRPLInformation * rplInfo = ImportRPLInformation::createImportRPLInformation(infoMap[sym_section_index]);
if(rplInfo == NULL) { if(rplInfo == NULL) {
DEBUG_FUNCTION_LINE("Failed to create import information\n"); DEBUG_FUNCTION_LINE("Failed to create import information");
break; break;
} }
@ -215,7 +215,7 @@ bool ModuleDataFactory::linkSection(elfio& reader, uint32_t section_index, uint3
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];
if(psec->get_info() == section_index) { if(psec->get_info() == section_index) {
DEBUG_FUNCTION_LINE("Found relocation section %s\n",psec->get_name().c_str()); DEBUG_FUNCTION_LINE("Found relocation section %s",psec->get_name().c_str());
relocation_section_accessor rel(reader, psec); relocation_section_accessor rel(reader, psec);
for ( uint32_t j = 0; j < (uint32_t) rel.get_entries_num(); ++j ) { for ( uint32_t j = 0; j < (uint32_t) rel.get_entries_num(); ++j ) {
Elf64_Addr offset; Elf64_Addr offset;
@ -226,7 +226,7 @@ bool ModuleDataFactory::linkSection(elfio& reader, uint32_t section_index, uint3
Elf_Half sym_section_index; Elf_Half sym_section_index;
if(!rel.get_entry(j, offset, sym_value, sym_name, type, addend, sym_section_index)) { if(!rel.get_entry(j, offset, sym_value, sym_name, type, addend, sym_section_index)) {
DEBUG_FUNCTION_LINE("Failed to get relocation\n"); DEBUG_FUNCTION_LINE("Failed to get relocation");
break; break;
} }
@ -243,23 +243,23 @@ bool ModuleDataFactory::linkSection(elfio& reader, uint32_t section_index, uint3
} else if(adjusted_sym_value == 0x0) { } else if(adjusted_sym_value == 0x0) {
// //
} else { } else {
DEBUG_FUNCTION_LINE("Unhandled case %08X\n",adjusted_sym_value); DEBUG_FUNCTION_LINE("Unhandled case %08X",adjusted_sym_value);
return false; return false;
} }
if(sym_section_index == SHN_ABS) { if(sym_section_index == SHN_ABS) {
// //
} else if(sym_section_index > SHN_LORESERVE) { } else if(sym_section_index > SHN_LORESERVE) {
DEBUG_FUNCTION_LINE("NOT IMPLEMENTED: %04X\n", sym_section_index); DEBUG_FUNCTION_LINE("NOT IMPLEMENTED: %04X", sym_section_index);
return false; return false;
} }
if(!ElfUtils::elfLinkOne(type, offset, addend, destination, adjusted_sym_value, trampolin_data, trampolin_data_length, RELOC_TYPE_FIXED)) { if(!ElfUtils::elfLinkOne(type, offset, addend, destination, adjusted_sym_value, trampolin_data, trampolin_data_length, RELOC_TYPE_FIXED)) {
DEBUG_FUNCTION_LINE("Link failed\n"); DEBUG_FUNCTION_LINE("Link failed");
return false; return false;
} }
} }
DEBUG_FUNCTION_LINE("done\n"); DEBUG_FUNCTION_LINE("done");
} }
} }
return true; return true;

View File

@ -2,5 +2,5 @@
#include "utils/StringTools.h" #include "utils/StringTools.h"
std::string RelocationData::toString(){ std::string RelocationData::toString(){
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() ); 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() );
} }

View File

@ -1,60 +0,0 @@
#include <malloc.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <coreinit/filesystem.h>
#include "utils/logger.h"
#include "FSOSUtils.h"
#define FS_MOUNT_SOURCE_SIZE 0x300
#define FS_MAX_MOUNTPATH_SIZE 128
#define FS_SOURCETYPE_EXTERNAL 0
int32_t FSOSUtils::MountFS(FSClient *pClient, FSCmdBlock *pCmd, char **mount_path) {
int32_t result = -1;
DEBUG_FUNCTION_LINE("\n");
FSMountSource *mountSrc = (FSMountSource*) malloc(sizeof(FSMountSource));
if(!mountSrc) {
return -3;
}
DEBUG_FUNCTION_LINE("\n");
char* mountPath = (char*) malloc(FS_MAX_MOUNTPATH_SIZE);
if(!mountPath) {
free(mountSrc);
mountSrc = NULL;
return -4;
}
DEBUG_FUNCTION_LINE("\n");
memset(mountSrc, 0, FS_MOUNT_SOURCE_SIZE);
memset(mountPath, 0, FS_MAX_MOUNTPATH_SIZE);
// Mount sdcard
if (FSGetMountSource(pClient, pCmd, FS_MOUNT_SOURCE_SD, mountSrc, -1) == 0) {
DEBUG_FUNCTION_LINE("\n");
result = FSMount(pClient, pCmd, mountSrc, mountPath, FS_MAX_MOUNTPATH_SIZE, -1);
if((result == 0) && mount_path) {
*mount_path = (char*)malloc(strlen(mountPath) + 1);
if(*mount_path)
strcpy(*mount_path, mountPath);
}
}
DEBUG_FUNCTION_LINE("\n");
free(mountPath);
free(mountSrc);
mountPath = NULL;
mountSrc = NULL;
return result;
}
int32_t FSOSUtils::UmountFS(FSClient *pClient, FSCmdBlock *pCmd, const char *mountPath) {
int32_t result = -1;
result = FSUnmount(pClient, pCmd, mountPath, -1);
return result;
}

View File

@ -1,13 +0,0 @@
#ifndef __FS_OS_UTILS_H_
#define __FS_OS_UTILS_H_
#include <stdint.h>
#include <coreinit/filesystem.h>
class FSOSUtils {
public:
static int32_t MountFS(FSClient *pClient, FSCmdBlock *pCmd, char **mount_path);
static int32_t UmountFS(FSClient *pClient, FSCmdBlock *pCmd, const char *mountPath);
};
#endif // __FS_OS_UTILS_H_

View File

@ -1,11 +1,11 @@
#ifndef __LOGGER_H_ #pragma once
#define __LOGGER_H_
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
#include <string.h> #include <string.h>
#include <whb/log.h>
void log_init_(); void log_init_();
//void log_deinit_(void); //void log_deinit_(void);
@ -28,11 +28,9 @@ void OSFatal_printf(const char *format, ...);
#define log_printf(FMT, ARGS...) log_printf_(FMT, ## ARGS); #define log_printf(FMT, ARGS...) log_printf_(FMT, ## ARGS);
#define DEBUG_FUNCTION_LINE(FMT, ARGS...)do { \ #define DEBUG_FUNCTION_LINE(FMT, ARGS...)do { \
log_printf("[%23s]%30s@L%04d: " FMT "",__FILENAME__,__FUNCTION__, __LINE__, ## ARGS); \ WHBLogPrintf("[%23s]%30s@L%04d: " FMT "",__FILENAME__,__FUNCTION__, __LINE__, ## ARGS); \
} while (0) } while (0)
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif

View File

@ -1,107 +0,0 @@
/****************************************************************************
* Copyright (C) 2015 Dimok
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/
#include <coreinit/memexpheap.h>
#include <coreinit/memdefaultheap.h>
#include <coreinit/memorymap.h>
#include <malloc.h>
#include <string.h>
#include <errno.h>
extern uint32_t * pMEMAllocFromDefaultHeapEx;
extern uint32_t * pMEMAllocFromDefaultHeap;
extern uint32_t * pMEMFreeToDefaultHeap;
//!-------------------------------------------------------------------------------------------
//! reent versions
//!-------------------------------------------------------------------------------------------
void *_malloc_r(struct _reent *r, size_t size) {
void *ptr = ((void * (*)(size_t))(*pMEMAllocFromDefaultHeap))(size);
if (!ptr) {
r->_errno = ENOMEM;
}
return ptr;
}
void *_calloc_r(struct _reent *r, size_t num, size_t size) {
void *ptr = ((void * (*)(size_t))(*pMEMAllocFromDefaultHeap))(size);
if (ptr) {
memset(ptr, 0, num * size);
} else {
r->_errno = ENOMEM;
}
return ptr;
}
void *_memalign_r(struct _reent *r, size_t align, size_t size) {
return ((void * (*)(size_t, size_t))(*pMEMAllocFromDefaultHeapEx))(size, align);
}
void _free_r(struct _reent *r, void *ptr) {
if (ptr) {
((void (*)(void *))(*pMEMFreeToDefaultHeap))(ptr);
}
}
void *_realloc_r(struct _reent *r, void *p, size_t size) {
void *new_ptr = ((void * (*)(size_t))(*pMEMAllocFromDefaultHeap))(size);
if (!new_ptr) {
r->_errno = ENOMEM;
return new_ptr;
}
if (p) {
size_t old_size = MEMGetSizeForMBlockExpHeap(p);
memcpy(new_ptr, p, old_size <= size ? old_size : size);
((void (*)(void *))(*pMEMFreeToDefaultHeap))(p);
}
return new_ptr;
}
struct mallinfo _mallinfo_r(struct _reent *r) {
struct mallinfo info = { 0 };
return info;
}
void
_malloc_stats_r(struct _reent *r) {
}
int
_mallopt_r(struct _reent *r, int param, int value) {
return 0;
}
size_t
_malloc_usable_size_r(struct _reent *r, void *ptr) {
return MEMGetSizeForMBlockExpHeap(ptr);
}
void *
_valloc_r(struct _reent *r, size_t size) {
return ((void * (*)(size_t, size_t))(*pMEMAllocFromDefaultHeapEx))(size, OS_PAGE_SIZE);
}
void *
_pvalloc_r(struct _reent *r, size_t size) {
return ((void * (*)(size_t, size_t))(*pMEMAllocFromDefaultHeapEx))((size + (OS_PAGE_SIZE - 1)) & ~(OS_PAGE_SIZE - 1), OS_PAGE_SIZE);
}
int
_malloc_trim_r(struct _reent *r, size_t pad) {
return 0;
}

View File

@ -1,42 +0,0 @@
/****************************************************************************
* Copyright (C) 2015 Dimok
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/
#ifndef __MEMORY_H_
#define __MEMORY_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <malloc.h>
void memoryInitialize(void);
void memoryRelease(void);
void * MEM2_alloc(u32 size, u32 align);
void MEM2_free(void *ptr);
void * MEM1_alloc(u32 size, u32 align);
void MEM1_free(void *ptr);
void * MEMBucket_alloc(u32 size, u32 align);
void MEMBucket_free(void *ptr);
#ifdef __cplusplus
}
#endif
#endif // __MEMORY_H_

File diff suppressed because it is too large Load Diff

View File

@ -1,40 +0,0 @@
/***************************************************************************
* Copyright (C) 2015
* by Dimok
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any
* damages arising from the use of this software.
*
* Permission is granted to anyone to use this software for any
* purpose, including commercial applications, and to alter it and
* redistribute it freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you
* must not claim that you wrote the original software. If you use
* this software in a product, an acknowledgment in the product
* documentation would be appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and
* must not be misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source
* distribution.
***************************************************************************/
#ifndef __SD_FAT_DEVOPTAB_H_
#define __SD_FAT_DEVOPTAB_H_
#ifdef __cplusplus
extern "C" {
#endif
int32_t mount_sd_fat(const char *path);
int32_t unmount_sd_fat(const char *path);
int32_t mount_fake();
int32_t unmount_fake();
void deleteDevTabsNames();
#ifdef __cplusplus
}
#endif
#endif // __SD_FAT_DEVOPTAB_H_