dynapi: don't let system loader resolve the initializer to the wrong version. Fixes problems launching Firewatch on Linux (which statically links SDL but also dynamically loads a system-wide copy from a plugin shared library) with a newer SDL build.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
diff --git a/src/dynapi/SDL_dynapi.c b/src/dynapi/SDL_dynapi.c
index b898826..7574cf5 100644
--- a/src/dynapi/SDL_dynapi.c
+++ b/src/dynapi/SDL_dynapi.c
@@ -167,15 +167,10 @@ SDL_DYNAPI_VARARGS(,,)
#error Write me.
#endif
-
-
-/* Here's the exported entry point that fills in the jump table. */
-/* Use specific types when an "int" might suffice to keep this sane. */
-typedef Sint32 (SDLCALL *SDL_DYNAPI_ENTRYFN)(Uint32 apiver, void *table, Uint32 tablesize);
-extern DECLSPEC Sint32 SDLCALL SDL_DYNAPI_entry(Uint32, void *, Uint32);
-
-Sint32
-SDL_DYNAPI_entry(Uint32 apiver, void *table, Uint32 tablesize)
+/* we make this a static function so we can call the correct one without the
+ system's dynamic linker resolving to the wrong version of this. */
+static Sint32
+initialize_jumptable(Uint32 apiver, void *table, Uint32 tablesize)
{
SDL_DYNAPI_jump_table *output_jump_table = (SDL_DYNAPI_jump_table *) table;
@@ -202,6 +197,18 @@ SDL_DYNAPI_entry(Uint32 apiver, void *table, Uint32 tablesize)
}
+/* Here's the exported entry point that fills in the jump table. */
+/* Use specific types when an "int" might suffice to keep this sane. */
+typedef Sint32 (SDLCALL *SDL_DYNAPI_ENTRYFN)(Uint32 apiver, void *table, Uint32 tablesize);
+extern DECLSPEC Sint32 SDLCALL SDL_DYNAPI_entry(Uint32, void *, Uint32);
+
+Sint32
+SDL_DYNAPI_entry(Uint32 apiver, void *table, Uint32 tablesize)
+{
+ return initialize_jumptable(apiver, table, tablesize);
+}
+
+
/* Obviously we can't use SDL_LoadObject() to load SDL. :) */
/* Also obviously, we never close the loaded library. */
#if defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__)
@@ -260,7 +267,7 @@ static void
SDL_InitDynamicAPILocked(void)
{
const char *libname = SDL_getenv_REAL("SDL_DYNAMIC_API");
- SDL_DYNAPI_ENTRYFN entry = SDL_DYNAPI_entry; /* funcs from here by default. */
+ SDL_DYNAPI_ENTRYFN entry = NULL; /* funcs from here by default. */
if (libname) {
entry = (SDL_DYNAPI_ENTRYFN) get_sdlapi_entry(libname, "SDL_DYNAPI_entry");
@@ -268,16 +275,15 @@ SDL_InitDynamicAPILocked(void)
/* !!! FIXME: fail to startup here instead? */
/* !!! FIXME: definitely warn user. */
/* Just fill in the function pointers from this library. */
- entry = SDL_DYNAPI_entry;
}
}
- if (entry(SDL_DYNAPI_VERSION, &jump_table, sizeof (jump_table)) < 0) {
+ if (!entry || (entry(SDL_DYNAPI_VERSION, &jump_table, sizeof (jump_table)) < 0)) {
/* !!! FIXME: fail to startup here instead? */
/* !!! FIXME: definitely warn user. */
/* Just fill in the function pointers from this library. */
- if (entry != SDL_DYNAPI_entry) {
- if (!SDL_DYNAPI_entry(SDL_DYNAPI_VERSION, &jump_table, sizeof (jump_table))) {
+ if (!entry) {
+ if (!initialize_jumptable(SDL_DYNAPI_VERSION, &jump_table, sizeof (jump_table))) {
/* !!! FIXME: now we're screwed. Should definitely abort now. */
}
}