From 81b1a9272790a7d70d69753fe35379d98fc77dc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonathan=20Neusch=C3=A4fer?= Date: Mon, 24 Apr 2017 05:41:13 +0200 Subject: [PATCH 1/4] .gitignore: Ignore .o files --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index cd2946a..739adc7 100644 --- a/.gitignore +++ b/.gitignore @@ -45,3 +45,6 @@ $RECYCLE.BIN/ Network Trash Folder Temporary Items .apdisk + +# Build artifacts +*.o From 775993a09b8f26771867dc50fa9f0f1317db9f57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonathan=20Neusch=C3=A4fer?= Date: Mon, 24 Apr 2017 05:41:13 +0200 Subject: [PATCH 2/4] Move most of OS_FIND_EXPORT{,_EX} into a function This brought the file size of os_functions.o (compiled at -O2, not stripped) from 15K to 9.7K. Section sizes changed in the following ways: .text .rela.text .data .rodata.str1.4 .syntab .strtab .shstrtab before 5660 2928 276 3111 1264 1255 89 after 2860 2376 276 1175 1296 1294 89 The most obvious win is due to not including two strings per imported function ("OSFoo" and "Function OSFoo is NULL") in the .rodata section anymore, but outlining the error-handling code also reduced the size of .text significantly. --- os_functions.c | 33 ++++++++++++++++++++++++++++++++- os_functions.h | 15 +++++---------- 2 files changed, 37 insertions(+), 11 deletions(-) diff --git a/os_functions.c b/os_functions.c index 1c4b256..a0a4928 100644 --- a/os_functions.c +++ b/os_functions.c @@ -149,9 +149,40 @@ EXPORT_DECL(s32, IOS_Ioctl,s32 fd, u32 request, void *input_buffer,u32 input_buf EXPORT_DECL(s32, IOS_Open,char *path, u32 mode); EXPORT_DECL(s32, IOS_Close,s32 fd); +void _os_find_export(u32 handle, const char *funcName, void *funcPointer) +{ + OSDynLoad_FindExport(handle, 0, funcName, funcPointer); + + if(!*(u32 *)funcPointer) { + /* + * This is effectively OSFatal("Function %s is NULL", funcName), + * but we can't rely on any library functions like snprintf or + * strcpy at this point. + * + * Buffer bounds are not checked. Beware! + */ + char buf[256], *bufp = buf; + const char a[] = "Function ", b[] = " is NULL", *p; + int i; + + for (i = 0; i < sizeof(a) - 1; i++) + *bufp++ = a[i]; + + for (p = funcName; *p; p++) + *bufp++ = *p; + + for (i = 0; i < sizeof(b) - 1; i++) + *bufp++ = b[i]; + + *bufp++ = '\0'; + + OSFatal(buf); + } +} + void InitAcquireOS(void) { - //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //! Lib handle functions //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- EXPORT_FUNC_WRITE(OSDynLoad_Acquire, (s32 (*)(const char*, unsigned *))OS_SPECIFICS->addr_OSDynLoad_Acquire); diff --git a/os_functions.h b/os_functions.h index 54b9c8b..6aad386 100644 --- a/os_functions.h +++ b/os_functions.h @@ -49,25 +49,20 @@ extern "C" { #define EXPORT_FUNC_WRITE(func, val) *(u32*)(((u32)&func) + 0) = (u32)val -#define OS_FIND_EXPORT(handle, func) funcPointer = 0; \ - OSDynLoad_FindExport(handle, 0, # func, &funcPointer); \ - if(!funcPointer) \ - OSFatal("Function " # func " is NULL"); \ +#define OS_FIND_EXPORT(handle, func) _os_find_export(handle, # func, &funcPointer); \ EXPORT_FUNC_WRITE(func, funcPointer); #define OS_FIND_EXPORT_EX(handle, func, func_p) \ - funcPointer = 0; \ - OSDynLoad_FindExport(handle, 0, # func, &funcPointer); \ - if(!funcPointer) \ - OSFatal("Function " # func " is NULL"); \ + _os_find_export(handle, # func, &funcPointer); \ EXPORT_FUNC_WRITE(func_p, funcPointer); #define OS_MUTEX_SIZE 44 /* Handle for coreinit */ extern u32 coreinit_handle; -void InitOSFunctionPointers(void); -void InitAcquireOS(void); +extern void _os_find_export(u32 handle, const char *funcName, void *funcPointer); +extern void InitAcquireOS(void); +extern void InitOSFunctionPointers(void); //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //! Lib handle functions From 1afc4b899ed3365bb8f89cbd9301cd539157d95a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonathan=20Neusch=C3=A4fer?= Date: Mon, 24 Apr 2017 05:41:14 +0200 Subject: [PATCH 3/4] os_functions: Add OS{AllocFrom,FreeTo}System OSAllocFromSystem allocates a physically contiguous area of memory, and returns a pointer to it as a virtual address in the calling process's address space. OSFreeToSystem is the corresponding free function. --- os_functions.c | 4 ++++ os_functions.h | 2 ++ 2 files changed, 6 insertions(+) diff --git a/os_functions.c b/os_functions.c index a0a4928..08514a1 100644 --- a/os_functions.c +++ b/os_functions.c @@ -99,6 +99,8 @@ EXPORT_DECL(void *, MEMAllocFromExpHeapEx, s32 heap, u32 size, s32 align); EXPORT_DECL(s32 , MEMCreateExpHeapEx, void* address, u32 size, unsigned short flags); EXPORT_DECL(void *, MEMDestroyExpHeap, s32 heap); EXPORT_DECL(void, MEMFreeToExpHeap, s32 heap, void* ptr); +EXPORT_DECL(void *, OSAllocFromSystem, int size, int alignment); +EXPORT_DECL(void, OSFreeToSystem, void *addr); //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //! MCP functions @@ -268,6 +270,8 @@ void InitOSFunctionPointers(void) OS_FIND_EXPORT(coreinit_handle, MEMCreateExpHeapEx); OS_FIND_EXPORT(coreinit_handle, MEMDestroyExpHeap); OS_FIND_EXPORT(coreinit_handle, MEMFreeToExpHeap); + OS_FIND_EXPORT(coreinit_handle, OSAllocFromSystem); + OS_FIND_EXPORT(coreinit_handle, OSFreeToSystem); //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //! Other function addresses diff --git a/os_functions.h b/os_functions.h index 6aad386..474db4d 100644 --- a/os_functions.h +++ b/os_functions.h @@ -138,6 +138,8 @@ extern void *(* MEMAllocFromExpHeapEx)(s32 heap, u32 size, s32 align); extern s32 (* MEMCreateExpHeapEx)(void* address, u32 size, unsigned short flags); extern void *(* MEMDestroyExpHeap)(s32 heap); extern void (* MEMFreeToExpHeap)(s32 heap, void* ptr); +extern void* (* OSAllocFromSystem)(int size, int alignment); +extern void (* OSFreeToSystem)(void *addr); //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //! MCP functions From b289fed3b7be9295933faeb081c7eeed8fa856d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonathan=20Neusch=C3=A4fer?= Date: Mon, 24 Apr 2017 05:41:14 +0200 Subject: [PATCH 4/4] os_functions: Add OSIsAddressValid This functions checks whether a virtual address is mapped readable. --- os_functions.c | 2 ++ os_functions.h | 1 + 2 files changed, 3 insertions(+) diff --git a/os_functions.c b/os_functions.c index 08514a1..7dc077a 100644 --- a/os_functions.c +++ b/os_functions.c @@ -101,6 +101,7 @@ EXPORT_DECL(void *, MEMDestroyExpHeap, s32 heap); EXPORT_DECL(void, MEMFreeToExpHeap, s32 heap, void* ptr); EXPORT_DECL(void *, OSAllocFromSystem, int size, int alignment); EXPORT_DECL(void, OSFreeToSystem, void *addr); +EXPORT_DECL(int, OSIsAddressValid, void *ptr); //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //! MCP functions @@ -272,6 +273,7 @@ void InitOSFunctionPointers(void) OS_FIND_EXPORT(coreinit_handle, MEMFreeToExpHeap); OS_FIND_EXPORT(coreinit_handle, OSAllocFromSystem); OS_FIND_EXPORT(coreinit_handle, OSFreeToSystem); + OS_FIND_EXPORT(coreinit_handle, OSIsAddressValid); //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //! Other function addresses diff --git a/os_functions.h b/os_functions.h index 474db4d..289d602 100644 --- a/os_functions.h +++ b/os_functions.h @@ -140,6 +140,7 @@ extern void *(* MEMDestroyExpHeap)(s32 heap); extern void (* MEMFreeToExpHeap)(s32 heap, void* ptr); extern void* (* OSAllocFromSystem)(int size, int alignment); extern void (* OSFreeToSystem)(void *addr); +extern int (* OSIsAddressValid)(void *ptr); //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //! MCP functions