2020-04-28 14:43:07 +02:00
# include "ModuleDataPersistence.h"
# include "DynamicLinkingHelper.h"
# include <coreinit/cache.h>
2020-05-29 17:31:53 +02:00
# include <wums.h>
2020-04-28 14:43:07 +02:00
2021-12-07 18:23:18 +01:00
std : : vector < std : : shared_ptr < ModuleDataMinimal > > ModuleDataPersistence : : loadModuleData ( module_information_t * moduleInformation ) {
std : : vector < std : : shared_ptr < ModuleDataMinimal > > result ;
2021-02-19 17:01:21 +01:00
if ( moduleInformation = = nullptr ) {
2020-04-28 14:43:07 +02:00
DEBUG_FUNCTION_LINE ( " moduleInformation == NULL \n " ) ;
return result ;
}
2020-05-17 19:05:51 +02:00
DCFlushRange ( ( void * ) moduleInformation , sizeof ( module_information_t ) ) ;
ICInvalidateRange ( ( void * ) moduleInformation , sizeof ( module_information_t ) ) ;
2020-04-28 14:43:07 +02:00
int32_t module_count = moduleInformation - > number_used_modules ;
2020-05-17 19:05:51 +02:00
if ( module_count > MAXIMUM_MODULES ) {
DEBUG_FUNCTION_LINE ( " moduleInformation->module_count was bigger then allowed. %d > %d. Limiting to %d \n " , module_count , MAXIMUM_MODULES , MAXIMUM_MODULES ) ;
2020-04-28 14:43:07 +02:00
module_count = MAXIMUM_MODULES ;
}
2020-05-17 19:05:51 +02:00
for ( int32_t i = 0 ; i < module_count ; i + + ) {
2020-04-28 14:43:07 +02:00
// Copy data from struct.
2020-05-17 19:05:51 +02:00
module_information_single_t * module_data = & ( moduleInformation - > module_data [ i ] ) ;
2022-02-04 21:44:03 +01:00
auto moduleData = std : : make_shared < ModuleDataMinimal > ( ) ;
2020-05-17 13:11:52 +02:00
2021-12-07 18:23:18 +01:00
moduleData - > setEntrypoint ( module_data - > entrypoint ) ;
moduleData - > setInitBeforeRelocationDoneHook ( module_data - > initBeforeRelocationDoneHook ) ;
2022-01-23 22:07:38 +01:00
moduleData - > setSkipInitFini ( module_data - > skipInitFini ) ;
2021-12-07 18:23:18 +01:00
moduleData - > setExportName ( module_data - > module_export_name ) ;
2020-05-29 17:31:53 +02:00
2022-02-04 21:44:03 +01:00
for ( auto & hook_entry : module_data - > hook_entries ) {
2021-09-17 16:22:54 +02:00
if ( hook_entry . target = = 0 ) {
2020-05-29 17:36:10 +02:00
continue ;
}
2021-12-07 18:23:18 +01:00
moduleData - > addHookData ( std : : make_shared < HookData > ( static_cast < wums_hook_type_t > ( hook_entry . type ) , reinterpret_cast < const void * > ( hook_entry . target ) ) ) ;
2020-05-29 17:36:10 +02:00
}
2022-02-04 21:44:03 +01:00
for ( auto & linking_entry : module_data - > linking_entries ) {
2021-02-19 17:01:21 +01:00
if ( linking_entry . destination = = nullptr ) {
2020-04-28 14:43:07 +02:00
break ;
}
2021-02-19 17:01:21 +01:00
dyn_linking_import_t * importEntry = linking_entry . importEntry ;
if ( importEntry = = nullptr ) {
2020-04-28 14:43:07 +02:00
DEBUG_FUNCTION_LINE ( " importEntry was NULL, skipping relocation entry \n " ) ;
continue ;
}
2021-02-19 17:01:21 +01:00
if ( importEntry - > importName = = nullptr ) {
2020-04-28 14:43:07 +02:00
DEBUG_FUNCTION_LINE ( " importEntry->importName was NULL, skipping relocation entry \n " ) ;
continue ;
}
2021-02-19 17:01:21 +01:00
dyn_linking_function_t * functionEntry = linking_entry . functionEntry ;
2020-04-28 14:43:07 +02:00
2021-02-19 17:01:21 +01:00
if ( functionEntry = = nullptr ) {
2020-04-28 14:43:07 +02:00
DEBUG_FUNCTION_LINE ( " functionEntry was NULL, skipping relocation entry \n " ) ;
continue ;
}
2021-02-19 17:01:21 +01:00
if ( functionEntry - > functionName = = nullptr ) {
2020-04-28 14:43:07 +02:00
DEBUG_FUNCTION_LINE ( " functionEntry->functionName was NULL, skipping relocation entry \n " ) ;
continue ;
}
2021-12-07 18:23:18 +01:00
auto rplInfo = std : : make_shared < ImportRPLInformation > ( importEntry - > importName , importEntry - > isData ) ;
2022-02-04 21:44:03 +01:00
auto reloc = std : : make_shared < RelocationData > ( linking_entry . type , linking_entry . offset , linking_entry . addend , linking_entry . destination , functionEntry - > functionName , rplInfo ) ;
2020-05-17 13:11:52 +02:00
2021-12-07 18:23:18 +01:00
moduleData - > addRelocationData ( reloc ) ;
2020-04-28 14:43:07 +02:00
}
2021-12-07 18:01:15 +01:00
2020-04-28 14:43:07 +02:00
result . push_back ( moduleData ) ;
}
return result ;
}