Path: blob/master/dependencies/switch/libnx-dyn/application/source/main.c
774 views
#include <stdlib.h>1#include <stdio.h>2#include <dyn.h>34typedef struct5{6u32(*DummyFunction)();7} ModuleSampleStruct;89u64 getBaseAddress()10{11u32 p;12MemoryInfo info;13svcQueryMemory(&info, &p, (u64)getBaseAddress); // Query the memory region of a function to get out base address14return info.addr;15}1617int main(int argc, char* argv[])18{19consoleInit(NULL);20Result rc = romfsInit();2122if(R_FAILED(rc)) {23printf("romfsInit() failed: 0x%x\n", rc);24}2526if(R_SUCCEEDED(rc)) {27rc = dynInitialize();28if(R_FAILED(rc)) {29printf("dynInitialize() failed: 0x%x\n", rc);30}3132if(R_SUCCEEDED(rc)) {33// Got from our __nx_dynamic wrap34extern void *__nx_aslr_base;35// Got from SVC36u64 addr = getBaseAddress();3738// addr and __nx_aslr_base must be equal!39printf ("Addr1: %p -> Addr2: %p (equal? %d)\n\n", __nx_aslr_base, (void*)addr, (__nx_aslr_base == (void*)addr));4041// Load ourselves with our base address42rc = dynLoadFromMemory("main", __nx_aslr_base);43if(R_FAILED(rc)) {44printf("dynLoadFromMemory() failed: 0x%x\n", rc);45}4647if(R_SUCCEEDED(rc)) {48printf("Address of self module 'main': %p\n", __nx_aslr_base);49DynModule sample_mod;50memset(&sample_mod, 0, sizeof(sample_mod));5152rc = dynLoadNroModule(&sample_mod, "romfs:/libmodule.nro", false);53if(R_FAILED(rc)) {54printf("dynLoadNroModule() failed: 0x%x\n", rc);55}5657if(R_SUCCEEDED(rc)) {58printf("Address of loaded module 'libmodule.nro': %p\n", sample_mod.input.base);59void *symbol = NULL;60rc = dynModuleLookupSymbol(&sample_mod, "libmodule_setConfig", &symbol);61if(R_FAILED(rc)) {62printf("dynModuleLookupSymbol() failed: 0x%x\n", rc);63}6465if(R_SUCCEEDED(rc)) {66printf("Symbol 'libmodule_setConfig()' address: %p\n", symbol);67if(symbol) {68void(*func)(ModuleSampleStruct*) = (void(*)(ModuleSampleStruct*))symbol;69ModuleSampleStruct sample;70sample.DummyFunction = NULL;71func(&sample); // Calling module's function to set its own function in the struct72if(sample.DummyFunction) {73u32 val = sample.DummyFunction();74printf("Got return value %d from our module's 'DummyFunction()'!\n", val);75}76}77}78}79}80}81}8283// Main loop84while(appletMainLoop())85{86//Scan all the inputs. This should be done once for each frame87hidScanInput();8889//hidKeysDown returns information about which buttons have been just pressed (and they weren't in the previous frame)90u64 kDown = hidKeysDown(CONTROLLER_P1_AUTO);9192if (kDown & KEY_PLUS) break; // break in order to return to hbmenu9394consoleUpdate(NULL);95}9697dynUnloadAll();98dynExit();99100romfsExit();101consoleExit(NULL);102103return 0;104}105106