Commit 3f1b5efccaa727745b335b546a42bd650694ca08

Ozkan Sezer 2022-10-22T14:28:10

os2 loadso improvements: - SDL_LoadObject: upon failure, strip the .dll extension and retry, but only if module name has no path. - SDL_LoadFunction: upon failure, retry with an underscore prepended, e.g. for gcc-built dlls.

diff --git a/src/loadso/os2/SDL_sysloadso.c b/src/loadso/os2/SDL_sysloadso.c
index 4af8068..292196e 100644
--- a/src/loadso/os2/SDL_sysloadso.c
+++ b/src/loadso/os2/SDL_sysloadso.c
@@ -47,11 +47,20 @@ SDL_LoadObject(const char *sofile)
 
     pszModName = OS2_UTF8ToSys(sofile);
     ulRC = DosLoadModule(acError, sizeof(acError), pszModName, &hModule);
-    SDL_free(pszModName);
+
+    if (ulRC != NO_ERROR && !SDL_strrchr(pszModName, '\\') && !SDL_strrchr(pszModName, '/')) {
+        /* strip .dll extension and retry only if name has no path. */
+        size_t len = SDL_strlen(pszModName);
+        if (len > 4 && SDL_strcasecmp(&pszModName[len - 4], ".dll") == 0) {
+            pszModName[len - 4] = '\0';
+            ulRC = DosLoadModule(acError, sizeof(acError), pszModName, &hModule);
+        }
+    }
     if (ulRC != NO_ERROR) {
         SDL_SetError("Failed loading %s: %s (E%u)", sofile, acError, ulRC);
-        return NULL;
+        hModule = NULLHANDLE;
     }
+    SDL_free(pszModName);
 
     return (void *)hModule;
 }
@@ -64,6 +73,16 @@ SDL_LoadFunction(void *handle, const char *name)
 
     ulRC = DosQueryProcAddr((HMODULE)handle, 0, name, &pFN);
     if (ulRC != NO_ERROR) {
+        /* retry with an underscore prepended, e.g. for gcc-built dlls. */
+        SDL_bool isstack;
+        size_t len = SDL_strlen(name) + 1;
+        char *_name = SDL_small_alloc(char, len + 1, &isstack);
+        _name[0] = '_';
+        SDL_memcpy(&_name[1], name, len);
+        ulRC = DosQueryProcAddr((HMODULE)handle, 0, _name, &pFN);
+        SDL_small_free(_name, isstack);
+    }
+    if (ulRC != NO_ERROR) {
         SDL_SetError("Failed loading procedure %s (E%u)", name, ulRC);
         return NULL;
     }