[Git][ghc/ghc][wip/romes/rts-linker-direct-symbol-lookup] fixup! rts: Make addDLL a wrapper around loadNativeObj

Rodrigo Mesquita (@alt-romes) gitlab at gitlab.haskell.org
Tue Apr 2 13:22:45 UTC 2024



Rodrigo Mesquita pushed to branch wip/romes/rts-linker-direct-symbol-lookup at Glasgow Haskell Compiler / GHC


Commits:
d445f1b3 by Rodrigo Mesquita at 2024-04-02T14:21:53+01:00
fixup! rts: Make addDLL a wrapper around loadNativeObj

- - - - -


4 changed files:

- rts/Linker.c
- rts/linker/LoadNativeObjPosix.c
- rts/linker/PEi386.c
- rts/linker/PEi386.h


Changes:

=====================================
rts/Linker.c
=====================================
@@ -643,7 +643,13 @@ void *lookupSymbolInNativeObj(void *handle, const char *symbol_name)
     CHECK(symbol_name[0] == '_');
     symbol_name = symbol_name+1;
 #endif
+#if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
     void *result = dlsym(handle, symbol_name);
+#elif defined(OBJFORMAT_PEi386)
+    void *result = lookupSymbolInDLL_PEi386(symbol_name, handle, NULL, NULL);
+#else
+    barf("lookupSymbolInNativeObj: Unsupported platform");
+#endif
 
     RELEASE_LOCK(&linker_mutex);
     return result;
@@ -652,7 +658,6 @@ void *lookupSymbolInNativeObj(void *handle, const char *symbol_name)
 
 const char *addDLL(pathchar* dll_name)
 {
-#  if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
    char *errmsg;
    if (loadNativeObj(dll_name, &errmsg)) {
      return NULL;
@@ -660,13 +665,6 @@ const char *addDLL(pathchar* dll_name)
      ASSERT(errmsg != NULL);
      return errmsg;
    }
-
-#  elif defined(OBJFORMAT_PEi386)
-   return addDLL_PEi386(dll_name);
-
-#  else
-   barf("addDLL: not implemented on this platform");
-#  endif
 }
 
 /* -----------------------------------------------------------------------------
@@ -1861,12 +1859,19 @@ addSection (Section *s, SectionKind kind, SectionAlloc alloc,
 
 #define UNUSED(x) (void)(x)
 
-#if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
 void * loadNativeObj (pathchar *path, char **errmsg)
 {
-   IF_DEBUG(linker, debugBelch("loadNativeObj: path = '%s'\n", path));
+   IF_DEBUG(linker, debugBelch("loadNativeObj: path = '%" PATH_FMT "'\n", path));
    ACQUIRE_LOCK(&linker_mutex);
+
+#if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
    void *r = loadNativeObj_POSIX(path, errmsg);
+#elif defined(OBJFORMAT_PEi386)
+   void *r;
+   *errmsg = (char*)addDLL_PEi386(path, (HINSTANCE*)&r);
+#else
+   barf("loadNativeObj: not implemented on this platform");
+#endif
 
 #if defined(OBJFORMAT_ELF)
    if (!r) {
@@ -1879,15 +1884,6 @@ void * loadNativeObj (pathchar *path, char **errmsg)
    RELEASE_LOCK(&linker_mutex);
    return r;
 }
-#else
-void * STG_NORETURN
-loadNativeObj (pathchar *path, char **errmsg)
-{
-   UNUSED(path);
-   UNUSED(errmsg);
-   barf("loadNativeObj: not implemented on this platform");
-}
-#endif
 
 static HsInt unloadNativeObj_(void *handle)
 {


=====================================
rts/linker/LoadNativeObjPosix.c
=====================================
@@ -1,7 +1,10 @@
-#include "CheckUnload.h"
-#include "ForeignExports.h"
 #include "LinkerInternals.h"
 #include "Rts.h"
+
+#if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
+
+#include "CheckUnload.h"
+#include "ForeignExports.h"
 #include "RtsUtils.h"
 #include "Profiling.h"
 
@@ -208,4 +211,4 @@ success:
    return retval;
 }
 
-
+#endif /* elf + macho */


=====================================
rts/linker/PEi386.c
=====================================
@@ -1141,47 +1141,55 @@ SymbolAddr*
 lookupSymbolInDLLs ( const SymbolName* lbl, ObjectCode *dependent )
 {
     OpenedDLL* o_dll;
-    SymbolAddr* sym;
 
-    for (o_dll = opened_dlls; o_dll != NULL; o_dll = o_dll->next) {
-        /* debugBelch("look in %ls for %s\n", o_dll->name, lbl); */
+    for (o_dll = opened_dlls; o_dll != NULL; o_dll = o_dll->next)
+        lookupSymbolInDLL_PEi386(lbl, o_dll->instance, o_dll->name, dependent);
+    return NULL;
+}
 
-        sym = GetProcAddress(o_dll->instance, lbl+STRIP_LEADING_UNDERSCORE);
-        if (sym != NULL) {
-            /*debugBelch("found %s in %s\n", lbl+1,o_dll->name);*/
-            return sym;
-        }
+SymbolAddr*
+lookupSymbolInDLL_PEi386 ( const SymbolName* lbl, HINSTANCE instance, pathchar* dll_name, ObjectCode *dependent)
+{
+    SymbolAddr* sym;
 
-        // TODO: Drop this
-        /* Ticket #2283.
-           Long description: http://support.microsoft.com/kb/132044
-           tl;dr:
-             If C/C++ compiler sees __declspec(dllimport) ... foo ...
-             it generates call *__imp_foo, and __imp_foo here has exactly
-             the same semantics as in __imp_foo = GetProcAddress(..., "foo")
-         */
-        if (sym == NULL && strncmp (lbl, "__imp_", 6) == 0) {
-            sym = GetProcAddress(o_dll->instance,
-                                 lbl + 6 + STRIP_LEADING_UNDERSCORE);
-            if (sym != NULL) {
-                SymbolAddr** indirect = m32_alloc(dependent->rw_m32, sizeof(SymbolAddr*), 8);
-                if (indirect == NULL) {
-                    barf("lookupSymbolInDLLs: Failed to allocation indirection");
-                }
-                *indirect = sym;
-                IF_DEBUG(linker,
-                  debugBelch("warning: %s from %S is linked instead of %s\n",
-                             lbl+6+STRIP_LEADING_UNDERSCORE, o_dll->name, lbl));
-                return (void*) indirect;
-               }
-        }
+    /* debugBelch("look in %ls for %s\n", dll_name, lbl); */
 
-        sym = GetProcAddress(o_dll->instance, lbl);
+    sym = GetProcAddress(instance, lbl+STRIP_LEADING_UNDERSCORE);
+    if (sym != NULL) {
+        /*debugBelch("found %s in %s\n", lbl+1,dll_name);*/
+        return sym;
+    }
+
+    // TODO: Drop this
+    /* Ticket #2283.
+       Long description: http://support.microsoft.com/kb/132044
+       tl;dr:
+         If C/C++ compiler sees __declspec(dllimport) ... foo ...
+         it generates call *__imp_foo, and __imp_foo here has exactly
+         the same semantics as in __imp_foo = GetProcAddress(..., "foo")
+     */
+    if (sym == NULL && strncmp (lbl, "__imp_", 6) == 0) {
+        sym = GetProcAddress(instance,
+                             lbl + 6 + STRIP_LEADING_UNDERSCORE);
         if (sym != NULL) {
-            /*debugBelch("found %s in %s\n", lbl,o_dll->name);*/
-            return sym;
+            SymbolAddr** indirect = m32_alloc(dependent->rw_m32, sizeof(SymbolAddr*), 8);
+            if (indirect == NULL) {
+                barf("lookupSymbolInDLLs: Failed to allocation indirection");
+            }
+            *indirect = sym;
+            IF_DEBUG(linker,
+              debugBelch("warning: %s from %S is linked instead of %s\n",
+                         lbl+6+STRIP_LEADING_UNDERSCORE, dll_name, lbl));
+            return (void*) indirect;
            }
     }
+
+    sym = GetProcAddress(instance, lbl);
+    if (sym != NULL) {
+        /*debugBelch("found %s in %s\n", lbl,dll_name);*/
+        return sym;
+       }
+
     return NULL;
 }
 


=====================================
rts/linker/PEi386.h
=====================================
@@ -60,6 +60,7 @@ bool ocRunFini_PEi386     ( ObjectCode *oc );
 bool ocGetNames_PEi386    ( ObjectCode* oc );
 bool ocVerifyImage_PEi386 ( ObjectCode* oc );
 SymbolAddr *lookupSymbol_PEi386(SymbolName *lbl, ObjectCode *dependent, SymType *type);
+SymbolAddr *lookupSymbolInDLL_PEi386 (const SymbolName* lbl, HINSTANCE instance, pathchar* dll_name, ObjectCode *dependent);
 
 /* See Note [mingw-w64 name decoration scheme] */
 /* We use myindex to calculate array addresses, rather than



View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/d445f1b32208c16908707bc8a930c4b856d53b26

-- 
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/d445f1b32208c16908707bc8a930c4b856d53b26
You're receiving this email because of your account on gitlab.haskell.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-commits/attachments/20240402/ab504b8b/attachment-0001.html>


More information about the ghc-commits mailing list