3 # User Ryan C. Gordon <icculus@icculus.org>
4 # Date 1526575846 14400
5 # Node ID 7babfecee045fac18d95e5936fede534ca54ed24
6 # Parent 9e46f3dd75fd2e85e0e3ebb8a77329bc74a16e70
7 dynapi: don't let system loader resolve the initializer to the wrong version.
9 Fixes problems launching Firewatch on Linux (which statically links SDL but
10 also dynamically loads a system-wide copy from a plugin shared library) with
13 diff -r 9e46f3dd75fd -r 7babfecee045 src/dynapi/SDL_dynapi.c
14 --- a/src/dynapi/SDL_dynapi.c Fri May 11 09:37:00 2018 +0300
15 +++ b/src/dynapi/SDL_dynapi.c Thu May 17 12:50:46 2018 -0400
22 -/* Here's the exported entry point that fills in the jump table. */
23 -/* Use specific types when an "int" might suffice to keep this sane. */
24 -typedef Sint32 (SDLCALL *SDL_DYNAPI_ENTRYFN)(Uint32 apiver, void *table, Uint32 tablesize);
25 -extern DECLSPEC Sint32 SDLCALL SDL_DYNAPI_entry(Uint32, void *, Uint32);
28 -SDL_DYNAPI_entry(Uint32 apiver, void *table, Uint32 tablesize)
29 +/* we make this a static function so we can call the correct one without the
30 + system's dynamic linker resolving to the wrong version of this. */
32 +initialize_jumptable(Uint32 apiver, void *table, Uint32 tablesize)
34 SDL_DYNAPI_jump_table *output_jump_table = (SDL_DYNAPI_jump_table *) table;
40 +/* Here's the exported entry point that fills in the jump table. */
41 +/* Use specific types when an "int" might suffice to keep this sane. */
42 +typedef Sint32 (SDLCALL *SDL_DYNAPI_ENTRYFN)(Uint32 apiver, void *table, Uint32 tablesize);
43 +extern DECLSPEC Sint32 SDLCALL SDL_DYNAPI_entry(Uint32, void *, Uint32);
46 +SDL_DYNAPI_entry(Uint32 apiver, void *table, Uint32 tablesize)
48 + return initialize_jumptable(apiver, table, tablesize);
52 /* Obviously we can't use SDL_LoadObject() to load SDL. :) */
53 /* Also obviously, we never close the loaded library. */
54 #if defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__)
56 SDL_InitDynamicAPILocked(void)
58 const char *libname = SDL_getenv_REAL("SDL_DYNAMIC_API");
59 - SDL_DYNAPI_ENTRYFN entry = SDL_DYNAPI_entry; /* funcs from here by default. */
60 + SDL_DYNAPI_ENTRYFN entry = NULL; /* funcs from here by default. */
63 entry = (SDL_DYNAPI_ENTRYFN) get_sdlapi_entry(libname, "SDL_DYNAPI_entry");
65 /* !!! FIXME: fail to startup here instead? */
66 /* !!! FIXME: definitely warn user. */
67 /* Just fill in the function pointers from this library. */
68 - entry = SDL_DYNAPI_entry;
72 - if (entry(SDL_DYNAPI_VERSION, &jump_table, sizeof (jump_table)) < 0) {
73 + if (!entry || (entry(SDL_DYNAPI_VERSION, &jump_table, sizeof (jump_table)) < 0)) {
74 /* !!! FIXME: fail to startup here instead? */
75 /* !!! FIXME: definitely warn user. */
76 /* Just fill in the function pointers from this library. */
77 - if (entry != SDL_DYNAPI_entry) {
78 - if (!SDL_DYNAPI_entry(SDL_DYNAPI_VERSION, &jump_table, sizeof (jump_table))) {
80 + if (!initialize_jumptable(SDL_DYNAPI_VERSION, &jump_table, sizeof (jump_table))) {
81 /* !!! FIXME: now we're screwed. Should definitely abort now. */