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] 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