client/runtime/linux_imports.c (9744B)
1 #include "linux_imports.h" 2 #include "guest_crt.h" 3 #include "guest_stdio.h" 4 #include "guest_format.h" 5 #include "guest_exception.h" 6 #include "guest_rtti.h" 7 #include "guest_jump.h" 8 #include "guest_msvcp.h" 9 #include "linux_platform.h" 10 #include "linux_sync.h" 11 #include "linux_file.h" 12 #include "linux_tls.h" 13 #include "linux_thread.h" 14 #include "linux_snapshot.h" 15 #include <pthread.h> 16 #include <stdatomic.h> 17 #include "linux_network.h" 18 #include "linux_graphics.h" 19 #include "linux_window.h" 20 #include "linux_gdi.h" 21 #include "linux_audio.h" 22 #include <strings.h> 23 24 /* External calls receive stable guest identifiers rather than host code addresses. */ 25 static struct { const char *dll, *name; uint32_t ordinal; recomp_func_t function; } imports[4096]; 26 static _Atomic unsigned import_count; 27 static _Thread_local unsigned current_import; 28 static pthread_mutex_t import_mutex = PTHREAD_MUTEX_INITIALIZER; 29 30 static recomp_func_t resolve_import(unsigned index); 31 32 static uint32_t import_bind_locked(const char *dll, const char *name, uint32_t ordinal) 33 { 34 if (!strcasecmp(dll, "msvcrt.dll") && name) { 35 uint32_t data = guest_crt_data(name); 36 if (data) return data; 37 } 38 for (unsigned i = 0; i < import_count; ++i) 39 if (!strcasecmp(dll, imports[i].dll) && ordinal == imports[i].ordinal && 40 ((!name && !imports[i].name) || 41 (name && imports[i].name && !strcmp(name, imports[i].name)))) 42 return 0xf0000000u + 4 * i; 43 if (import_count == sizeof(imports) / sizeof(imports[0])) abort(); 44 imports[import_count].dll = strdup(dll); 45 imports[import_count].name = name ? strdup(name) : NULL; 46 if (!imports[import_count].dll || (name && !imports[import_count].name)) abort(); 47 imports[import_count].ordinal = ordinal; 48 imports[import_count].function = resolve_import(import_count); 49 return 0xf0000000u + 4 * import_count++; 50 } 51 52 uint32_t linux_import_bind(const char *dll, const char *name, uint32_t ordinal) 53 { 54 if (pthread_mutex_lock(&import_mutex)) abort(); 55 uint32_t address = import_bind_locked(dll, name, ordinal); 56 if (pthread_mutex_unlock(&import_mutex)) abort(); 57 return address; 58 } 59 60 unsigned linux_import_count(void) { return import_count; } 61 62 static void external_call(void) 63 { 64 /* Preserve the game's error text when its fallback dialog is unavailable. */ 65 if (!strcasecmp(imports[current_import].dll, "user32.dll") && 66 imports[current_import].name && !strcmp(imports[current_import].name, "MessageBoxA")) { 67 uint32_t text = MEM32(g_esp + 8); 68 if (text) fprintf(stderr, "Game error: %s\n", (const char *)ADDR(text)); 69 } 70 fprintf(stderr, "Unimplemented platform call: %s!%s ordinal=%u caller=%08x\n", 71 imports[current_import].dll, 72 imports[current_import].name ? imports[current_import].name : "", 73 imports[current_import].ordinal, g_cur_func); 74 exit(3); 75 } 76 77 /* AO statically imports user32 and retains its functions after FreeLibrary. 78 * This token identifies a compiled platform boundary, never a loaded PE. */ 79 static void load_library(void) 80 { 81 const char *name = (const char *)ADDR(MEM32(g_esp + 4)); 82 if (strcasecmp(name, "user32.dll") && strcasecmp(name, "user32")) { 83 fprintf(stderr, "Unimplemented dynamic library: %s caller=%08x\n", name, g_cur_func); 84 exit(3); 85 } 86 g_eax = 0xf2000000u; 87 g_esp += 8; 88 } 89 90 static void free_library(void) 91 { 92 g_eax = MEM32(g_esp + 4) == 0xf2000000u; 93 if (!g_eax) MEM32(g_fs_base + 0x34) = 126; /* ERROR_MOD_NOT_FOUND */ 94 g_esp += 8; 95 } 96 97 static void get_proc_address(void) 98 { 99 uint32_t module = MEM32(g_esp + 4), name = MEM32(g_esp + 8); 100 g_eax = 0; 101 if (module == 0xf2000000u) { 102 if (name <= 0xffffu) { 103 fprintf(stderr, "Unimplemented user32 export ordinal: %u caller=%08x\n", name, g_cur_func); 104 exit(3); 105 } 106 /* Both branches of Clientr.exe!sub_280028F0 select these exports. 107 * Binding a symbol does not imply its platform implementation exists. */ 108 static const char *names[] = { 109 "PeekMessageA", "GetMessageA", "DispatchMessageA", "DefWindowProcA", 110 "RegisterClassA", "UnregisterClassA", "CreateWindowExA", 111 "PeekMessageW", "GetMessageW", "DispatchMessageW", "DefWindowProcW", 112 "RegisterClassW", "UnregisterClassW", "CreateWindowExW", 113 }; 114 for (unsigned i = 0; i < sizeof(names) / sizeof(names[0]); ++i) 115 if (!strcmp((const char *)ADDR(name), names[i])) { 116 g_eax = linux_import_bind("user32.dll", names[i], 0); 117 break; 118 } 119 if (!g_eax) MEM32(g_fs_base + 0x34) = 127; /* ERROR_PROC_NOT_FOUND */ 120 } else g_eax = linux_module_export(module, name > 0xffff ? (const char *)ADDR(name) : NULL, 121 name <= 0xffff ? name : 0); 122 g_esp += 12; 123 } 124 125 static void crt_exit(void) 126 { 127 int status = MEM32(g_esp + 4); 128 /* Run guest termination callbacks before flushing guest FILE buffers. */ 129 guest_crt_lookup("_cexit")(); 130 PUSH32(g_esp, 0); 131 PUSH32(g_esp, RECOMP_RETADDR); 132 guest_stdio_lookup("fflush")(); 133 #ifdef __EMSCRIPTEN__ 134 linux_graphics_shutdown(); 135 #endif 136 exit(status); 137 } 138 139 static void close_handle(void) 140 { 141 if ((MEM32(g_esp + 4) & 0xff000000u) == 0x05000000u) 142 linux_file_lookup("CloseHandle")(); 143 else if ((MEM32(g_esp + 4) & 0xff000000u) == 0x06000000u) 144 linux_thread_lookup("CloseHandle")(); 145 else if ((MEM32(g_esp + 4) & 0xff000000u) == 0x07000000u) 146 linux_snapshot_lookup("CloseHandle")(); 147 else linux_sync_lookup("CloseHandle")(); 148 } 149 150 static void wait_for_single_object(void) 151 { 152 if ((MEM32(g_esp + 4) & 0xff000000u) == 0x06000000u) 153 linux_thread_lookup("WaitForSingleObject")(); 154 else linux_sync_lookup("WaitForSingleObject")(); 155 } 156 157 static recomp_func_t library_lookup(const char *name) 158 { 159 if (!strcmp(name, "LoadLibraryA")) return load_library; 160 if (!strcmp(name, "FreeLibrary")) return free_library; 161 if (!strcmp(name, "GetProcAddress")) return get_proc_address; 162 if (!strcmp(name, "CloseHandle")) return close_handle; 163 if (!strcmp(name, "WaitForSingleObject")) return wait_for_single_object; 164 return NULL; 165 } 166 167 recomp_func_t recomp_lookup_import(uint32_t va) 168 { 169 if (va < 0xf0000000u || ((va - 0xf0000000u) & 3) || 170 (va - 0xf0000000u) / 4 >= import_count) { 171 fprintf(stderr, "Unknown guest call %08x from %08x\n", va, g_cur_func); 172 exit(3); 173 } 174 current_import = (va - 0xf0000000u) / 4; 175 return imports[current_import].function; 176 } 177 178 /* Resolve immutable bindings once, before publishing their guest tokens. */ 179 static recomp_func_t resolve_import(unsigned index) 180 { 181 if (!strcasecmp(imports[index].dll, "ddraw.dll") && imports[index].name) { 182 recomp_func_t function = linux_graphics_lookup(imports[index].name); 183 if (function) return function; 184 } 185 if (!strcasecmp(imports[index].dll, "ws2_32.dll") || 186 !strcasecmp(imports[index].dll, "wsock32.dll")) { 187 recomp_func_t function = linux_network_lookup(imports[index].name, imports[index].ordinal); 188 if (function) return function; 189 } 190 if (imports[index].name) { 191 recomp_func_t audio = linux_audio_lookup(imports[index].dll, imports[index].name); 192 if (audio) return audio; 193 if (!strcasecmp(imports[index].dll, "gdi32.dll")) { 194 recomp_func_t function = linux_gdi_lookup(imports[index].name); 195 if (function) return function; 196 } 197 recomp_func_t function = linux_platform_lookup(imports[index].dll, imports[index].name); 198 if (function) return function; 199 if (!strcasecmp(imports[index].dll, "winmm.dll")) { 200 function = linux_thread_lookup(imports[index].name); 201 if (function) return function; 202 } 203 function = linux_window_lookup(imports[index].dll, imports[index].name); 204 if (function) return function; 205 if (!strcasecmp(imports[index].dll, "kernel32.dll")) { 206 function = library_lookup(imports[index].name); 207 if (!function) function = linux_file_lookup(imports[index].name); 208 if (!function) function = linux_module_lookup(imports[index].name); 209 if (!function) function = linux_sync_lookup(imports[index].name); 210 if (!function) function = linux_tls_lookup(imports[index].name); 211 if (!function) function = linux_thread_lookup(imports[index].name); 212 if (!function) function = linux_snapshot_lookup(imports[index].name); 213 if (function) return function; 214 } 215 } 216 if (!strcasecmp(imports[index].dll, "msvcrt.dll") && imports[index].name) { 217 if (!strcmp(imports[index].name, "exit")) return crt_exit; 218 if (!strcmp(imports[index].name, "longjmp")) return guest_longjmp; 219 if (!strcmp(imports[index].name, "_CxxThrowException")) { 220 extern void native32_shim_CxxThrowException(void); 221 return native32_shim_CxxThrowException; 222 } 223 recomp_func_t function = linux_thread_lookup(imports[index].name); 224 if (!function) function = guest_stdio_lookup(imports[index].name); 225 if (!function) function = guest_crt_lookup(imports[index].name); 226 if (!function) function = guest_format_lookup(imports[index].name); 227 if (!function) function = guest_exception_lookup(imports[index].name); 228 if (!function) function = guest_rtti_lookup(imports[index].name); 229 if (function) return function; 230 } 231 if (!strcasecmp(imports[index].dll, "msvcp60.dll") && imports[index].name) { 232 recomp_func_t function = guest_msvcp_lookup(imports[index].name); 233 if (function) return function; 234 } 235 return external_call; 236 } 237