changeset 246:06a354b2e120

changes for dynload for macOS and OpenBSD: - reverted last commit, b/c of brainfat (wrongly assuming all libs iterated over are opened by dynload) - make use of RTLD_NOLOAD, though, when re-dlopen-ing to get path, for performance (where available)
author Tassilo Philipp
date Thu, 04 May 2017 23:38:30 +0200
parents 0ba6189a51dd
children 446d2220bc49
files dynload/dynload_unix.c
diffstat 1 files changed, 16 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/dynload/dynload_unix.c	Thu May 04 15:11:46 2017 +0200
+++ b/dynload/dynload_unix.c	Thu May 04 23:38:30 2017 +0200
@@ -68,6 +68,14 @@
 }
 
 
+/* prefer RTLD_NOLOAD for code below that merely checks lib names */
+#if defined(RTLD_NOLOAD)
+#  define RTLD_LIGHTEST RTLD_NOLOAD
+#else
+#  define RTLD_LIGHTEST RTLD_LAZY
+#endif
+
+
 /* code for dlGetLibraryPath differs on Darwin */
 #if defined(OS_Darwin)
 
@@ -89,10 +97,12 @@
   for(i=_dyld_image_count(); i>0;) /* iterate libs from end, more likely ours */
   {
     const char* libPath = _dyld_get_image_name(--i);
-    DLLib* lib = dlLoadLibrary(libPath); /* re-open same way for same handle */
+    void* lib = dlopen(libPath, RTLD_LIGHTEST);
     if(lib) {
-      dlFreeLibrary(lib);
-      if(pLib == lib) {
+      dlclose(lib);
+      /* compare handle pointers' high bits (in low 2 bits some flags might */
+      /* be stored - should be safe b/c address needs alignment, anywas) */
+      if(((intptr_t)pLib ^ (intptr_t)lib) < 4) {
         l = strlen(libPath);
         if(l < bufSize) /* l+'\0' <= bufSize */
           strcpy(sOut, libPath);
@@ -126,10 +136,10 @@
   /* unable to relate info->dlpi_addr directly to our dlopen handle, let's */
   /* do what we do on macOS above, re-dlopen the already loaded lib (just  */
   /* increases ref count) and compare handles. */
-  DLLib* lib = dlLoadLibrary(info->dlpi_name); /* re-open same way for same handle */
+  void* lib = dlopen(info->dlpi_name, RTLD_LIGHTEST);
   if(lib) {
-    dlFreeLibrary(lib);
-    if(lib == d->pLib) {
+    dlclose(lib);
+    if(lib == (void*)d->pLib) {
       l = strlen(info->dlpi_name);
       if(l < d->bufSize) /* l+'\0' <= bufSize */
         strcpy(d->sOut, info->dlpi_name);