Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Rubberduckycooly
GitHub Repository: Rubberduckycooly/RSDKv5-Decompilation
Path: blob/master/dependencies/switch/libnx-dyn/application/source/main.c
774 views
1
#include <stdlib.h>
2
#include <stdio.h>
3
#include <dyn.h>
4
5
typedef struct
6
{
7
u32(*DummyFunction)();
8
} ModuleSampleStruct;
9
10
u64 getBaseAddress()
11
{
12
u32 p;
13
MemoryInfo info;
14
svcQueryMemory(&info, &p, (u64)getBaseAddress); // Query the memory region of a function to get out base address
15
return info.addr;
16
}
17
18
int main(int argc, char* argv[])
19
{
20
consoleInit(NULL);
21
Result rc = romfsInit();
22
23
if(R_FAILED(rc)) {
24
printf("romfsInit() failed: 0x%x\n", rc);
25
}
26
27
if(R_SUCCEEDED(rc)) {
28
rc = dynInitialize();
29
if(R_FAILED(rc)) {
30
printf("dynInitialize() failed: 0x%x\n", rc);
31
}
32
33
if(R_SUCCEEDED(rc)) {
34
// Got from our __nx_dynamic wrap
35
extern void *__nx_aslr_base;
36
// Got from SVC
37
u64 addr = getBaseAddress();
38
39
// addr and __nx_aslr_base must be equal!
40
printf ("Addr1: %p -> Addr2: %p (equal? %d)\n\n", __nx_aslr_base, (void*)addr, (__nx_aslr_base == (void*)addr));
41
42
// Load ourselves with our base address
43
rc = dynLoadFromMemory("main", __nx_aslr_base);
44
if(R_FAILED(rc)) {
45
printf("dynLoadFromMemory() failed: 0x%x\n", rc);
46
}
47
48
if(R_SUCCEEDED(rc)) {
49
printf("Address of self module 'main': %p\n", __nx_aslr_base);
50
DynModule sample_mod;
51
memset(&sample_mod, 0, sizeof(sample_mod));
52
53
rc = dynLoadNroModule(&sample_mod, "romfs:/libmodule.nro", false);
54
if(R_FAILED(rc)) {
55
printf("dynLoadNroModule() failed: 0x%x\n", rc);
56
}
57
58
if(R_SUCCEEDED(rc)) {
59
printf("Address of loaded module 'libmodule.nro': %p\n", sample_mod.input.base);
60
void *symbol = NULL;
61
rc = dynModuleLookupSymbol(&sample_mod, "libmodule_setConfig", &symbol);
62
if(R_FAILED(rc)) {
63
printf("dynModuleLookupSymbol() failed: 0x%x\n", rc);
64
}
65
66
if(R_SUCCEEDED(rc)) {
67
printf("Symbol 'libmodule_setConfig()' address: %p\n", symbol);
68
if(symbol) {
69
void(*func)(ModuleSampleStruct*) = (void(*)(ModuleSampleStruct*))symbol;
70
ModuleSampleStruct sample;
71
sample.DummyFunction = NULL;
72
func(&sample); // Calling module's function to set its own function in the struct
73
if(sample.DummyFunction) {
74
u32 val = sample.DummyFunction();
75
printf("Got return value %d from our module's 'DummyFunction()'!\n", val);
76
}
77
}
78
}
79
}
80
}
81
}
82
}
83
84
// Main loop
85
while(appletMainLoop())
86
{
87
//Scan all the inputs. This should be done once for each frame
88
hidScanInput();
89
90
//hidKeysDown returns information about which buttons have been just pressed (and they weren't in the previous frame)
91
u64 kDown = hidKeysDown(CONTROLLER_P1_AUTO);
92
93
if (kDown & KEY_PLUS) break; // break in order to return to hbmenu
94
95
consoleUpdate(NULL);
96
}
97
98
dynUnloadAll();
99
dynExit();
100
101
romfsExit();
102
consoleExit(NULL);
103
104
return 0;
105
}
106