about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorptitSeb <sebastien.chev@gmail.com>2023-02-04 11:11:47 +0100
committerptitSeb <sebastien.chev@gmail.com>2023-02-04 11:11:47 +0100
commit973864e045c2080425619d47538db3455e4a7435 (patch)
treea357a28e74d202830724d1b5ef49ba6262df1c3d /src
parentaf694b57a22e17a237f5daa1f1b84624dda769d6 (diff)
downloadbox64-973864e045c2080425619d47538db3455e4a7435.tar.gz
box64-973864e045c2080425619d47538db3455e4a7435.zip
Added partial support for register_printf_specifier and friend (for #503)
Diffstat (limited to 'src')
-rw-r--r--src/wrapped/generated/functions_list.txt2
-rw-r--r--src/wrapped/generated/wrappedlibctypes.h2
-rwxr-xr-xsrc/wrapped/wrappedlibc.c86
-rwxr-xr-xsrc/wrapped/wrappedlibc_private.h6
4 files changed, 92 insertions, 4 deletions
diff --git a/src/wrapped/generated/functions_list.txt b/src/wrapped/generated/functions_list.txt
index 3253641a..26f9e680 100644
--- a/src/wrapped/generated/functions_list.txt
+++ b/src/wrapped/generated/functions_list.txt
@@ -2995,6 +2995,7 @@ wrappedlibc:
   - _setjmp
   - atexit
   - getcontext
+  - register_printf_type
   - setcontext
   - setjmp
   - stime
@@ -3088,6 +3089,7 @@ wrappedlibc:
   - __xstat
   - __xstat64
   - getopt
+  - register_printf_specifier
   - sigaction
 - iFipV:
   - __printf_chk
diff --git a/src/wrapped/generated/wrappedlibctypes.h b/src/wrapped/generated/wrappedlibctypes.h
index 37b2fb8b..5a08f38d 100644
--- a/src/wrapped/generated/wrappedlibctypes.h
+++ b/src/wrapped/generated/wrappedlibctypes.h
@@ -101,6 +101,7 @@ typedef int64_t (*iFppipppp_t)(void*, void*, int64_t, void*, void*, void*, void*
 	GO(_setjmp, iFp_t) \
 	GO(atexit, iFp_t) \
 	GO(getcontext, iFp_t) \
+	GO(register_printf_type, iFp_t) \
 	GO(setcontext, iFp_t) \
 	GO(setjmp, iFp_t) \
 	GO(stime, iFp_t) \
@@ -170,6 +171,7 @@ typedef int64_t (*iFppipppp_t)(void*, void*, int64_t, void*, void*, void*, void*
 	GO(__xstat, iFipp_t) \
 	GO(__xstat64, iFipp_t) \
 	GO(getopt, iFipp_t) \
+	GO(register_printf_specifier, iFipp_t) \
 	GO(sigaction, iFipp_t) \
 	GO(__printf_chk, iFipV_t) \
 	GO(mprotect, iFpLi_t) \
diff --git a/src/wrapped/wrappedlibc.c b/src/wrapped/wrappedlibc.c
index 7a8f9c32..2f25b9ae 100755
--- a/src/wrapped/wrappedlibc.c
+++ b/src/wrapped/wrappedlibc.c
@@ -67,6 +67,7 @@
 const char* libcName = "libc.so.6";
 
 typedef int (*iFi_t)(int);
+typedef int (*iFp_t)(void*);
 typedef int (*iFL_t)(unsigned long);
 typedef void (*vFpp_t)(void*, void*);
 typedef void (*vFipp_t)(int32_t, void*, void*);
@@ -92,7 +93,9 @@ typedef void* (*pFp_t)(void*);
 
 #define SUPER() \
     GO(_ITM_addUserCommitAction, iFpup_t)   \
-    GO(_IO_file_stat, iFpp_t)
+    GO(_IO_file_stat, iFpp_t)               \
+    GO(register_printf_specifier, iFipp_t)  \
+    GO(register_printf_type, iFp_t)
 
 
 #include "wrappercallback.h"
@@ -338,6 +341,75 @@ static void* findcompare64Fct(void* fct)
     printf_log(LOG_NONE, "Warning, no more slot for libc compare64 callback\n");
     return NULL;
 }
+// printf_output
+#define GO(A)   \
+static uintptr_t my_printf_output_fct_##A = 0;                                  \
+static int my_printf_output_##A(void* a, void* b, void* c)                      \
+{                                                                               \
+    return (int)RunFunction(my_context, my_printf_output_fct_##A, 3, a, b, c);  \
+}
+SUPER()
+#undef GO
+static void* findprintf_outputFct(void* fct)
+{
+    if(!fct) return NULL;
+    void* p;
+    if((p = GetNativeFnc((uintptr_t)fct))) return p;
+    #define GO(A) if(my_printf_output_fct_##A == (uintptr_t)fct) return my_printf_output_##A;
+    SUPER()
+    #undef GO
+    #define GO(A) if(my_printf_output_fct_##A == 0) {my_printf_output_fct_##A = (uintptr_t)fct; return my_printf_output_##A; }
+    SUPER()
+    #undef GO
+    printf_log(LOG_NONE, "Warning, no more slot for libc printf_output callback\n");
+    return NULL;
+}
+// printf_arginfo
+#define GO(A)   \
+static uintptr_t my_printf_arginfo_fct_##A = 0;                                     \
+static int my_printf_arginfo_##A(void* a, size_t b, void* c, void* d)               \
+{                                                                                   \
+    return (int)RunFunction(my_context, my_printf_arginfo_fct_##A, 4, a, b, c, d);  \
+}
+SUPER()
+#undef GO
+static void* findprintf_arginfoFct(void* fct)
+{
+    if(!fct) return NULL;
+    void* p;
+    if((p = GetNativeFnc((uintptr_t)fct))) return p;
+    #define GO(A) if(my_printf_arginfo_fct_##A == (uintptr_t)fct) return my_printf_arginfo_##A;
+    SUPER()
+    #undef GO
+    #define GO(A) if(my_printf_arginfo_fct_##A == 0) {my_printf_arginfo_fct_##A = (uintptr_t)fct; return my_printf_arginfo_##A; }
+    SUPER()
+    #undef GO
+    printf_log(LOG_NONE, "Warning, no more slot for libc printf_arginfo callback\n");
+    return NULL;
+}
+// printf_type
+#define GO(A)   \
+static uintptr_t my_printf_type_fct_##A = 0;                   \
+static void my_printf_type_##A(void* a, va_list* b)            \
+{                                                              \
+    RunFunction(my_context, my_printf_type_fct_##A, 2, a, b);  \
+}
+SUPER()
+#undef GO
+static void* findprintf_typeFct(void* fct)
+{
+    if(!fct) return NULL;
+    void* p;
+    if((p = GetNativeFnc((uintptr_t)fct))) return p;
+    #define GO(A) if(my_printf_type_fct_##A == (uintptr_t)fct) return my_printf_type_##A;
+    SUPER()
+    #undef GO
+    #define GO(A) if(my_printf_type_fct_##A == 0) {my_printf_type_fct_##A = (uintptr_t)fct; return my_printf_type_##A; }
+    SUPER()
+    #undef GO
+    printf_log(LOG_NONE, "Warning, no more slot for libc printf_type callback\n");
+    return NULL;
+}
 
 #undef SUPER
 
@@ -2954,6 +3026,18 @@ EXPORT size_t my_strlcat(x64emu_t* emu, void* dst, void* src, size_t l)
     return s+strlen(src);
 }
 
+EXPORT int my_register_printf_specifier(x64emu_t* emu, int c, void* f1, void* f2)
+{
+    //TODO: defining a new sepcifier for printf, it should also be registered on myStackAlign/myStackAlignW, using f2 to get the type of arg
+    return my->register_printf_specifier(c, findprintf_outputFct(f1), findprintf_arginfoFct(f2));
+}
+
+EXPORT int my_register_printf_type(x64emu_t* emu, void* f)
+{
+    //TODO: defining a new type, probably needs to also register that for myStackAlign stuffs
+    return my->register_printf_type(findprintf_typeFct(f));
+}
+
 EXPORT void my_exit(x64emu_t* emu, int code)
 {
     if(emu->quitonexit) {
diff --git a/src/wrapped/wrappedlibc_private.h b/src/wrapped/wrappedlibc_private.h
index f9306267..1ce329d3 100755
--- a/src/wrapped/wrappedlibc_private.h
+++ b/src/wrapped/wrappedlibc_private.h
@@ -1506,9 +1506,9 @@ GO(regexec, iFppLpi)
 GOW(regfree, vFp)
 GOM(__register_atfork, iFEpppp)
 //GOW(register_printf_function, 
-//GOW(register_printf_modifier, 
-//GOW(register_printf_specifier, 
-//GOW(register_printf_type, 
+GOW(register_printf_modifier, iFp)
+GOWM(register_printf_specifier, iFEipp)
+GOWM(register_printf_type, iFEp)
 //GO(registerrpc, // Deprecated?
 GOW(remap_file_pages, iFpLiLi)
 //GOW(re_match, iFppii!)