Bug 1305159: replace NSAddImage with dlopen on OSX. r=froydnj
authorNils Ohlmeier [:drno] <drno@ohlmeier.org>
Fri, 23 Sep 2016 15:31:39 -0700
changeset 315782 b9b87a4102da1a9857a89cfbc859a6bf23bfe0e5
parent 315781 d46155cc719e323b61da814f4db8e2e3bc07c9e3
child 315783 dc1b725b47d42a888c3cf88afbf0b73c6b291838
push id20634
push usercbook@mozilla.com
push dateFri, 30 Sep 2016 10:10:13 +0000
treeherderfx-team@afe79b010d13 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj
bugs1305159
milestone52.0a1
Bug 1305159: replace NSAddImage with dlopen on OSX. r=froydnj MozReview-Commit-ID: FHZthgRxPha
xpcom/glue/standalone/nsXPCOMGlue.cpp
--- a/xpcom/glue/standalone/nsXPCOMGlue.cpp
+++ b/xpcom/glue/standalone/nsXPCOMGlue.cpp
@@ -24,24 +24,16 @@ static XPCOMFunctions xpcomFunctions;
 static bool do_preload = false;
 
 #if defined(XP_WIN)
 #define READ_TEXTMODE L"rt"
 #else
 #define READ_TEXTMODE "r"
 #endif
 
-#if defined(SUNOS4) || defined(NEXTSTEP) || \
-    defined(XP_MACOSX) || \
-    (defined(OPENBSD) || defined(NETBSD)) && !defined(__ELF__)
-#define LEADING_UNDERSCORE "_"
-#else
-#define LEADING_UNDERSCORE
-#endif
-
 #if defined(XP_WIN)
 #include <windows.h>
 #include <mbstring.h>
 
 typedef HINSTANCE LibHandleType;
 
 static LibHandleType
 GetLibHandle(pathstr_t aDependentLib)
@@ -79,71 +71,16 @@ GetSymbol(LibHandleType aLibHandle, cons
 }
 
 static void
 CloseLibHandle(LibHandleType aLibHandle)
 {
   FreeLibrary(aLibHandle);
 }
 
-#elif defined(XP_MACOSX)
-#include <mach-o/dyld.h>
-
-typedef const mach_header* LibHandleType;
-
-static LibHandleType
-GetLibHandle(pathstr_t aDependentLib)
-{
-  LibHandleType libHandle = NSAddImage(aDependentLib,
-                                       NSADDIMAGE_OPTION_RETURN_ON_ERROR |
-                                       NSADDIMAGE_OPTION_MATCH_FILENAME_BY_INSTALLNAME);
-  if (!libHandle) {
-    NSLinkEditErrors linkEditError;
-    int errorNum;
-    const char* errorString;
-    const char* fileName;
-    NSLinkEditError(&linkEditError, &errorNum, &fileName, &errorString);
-    fprintf(stderr, "XPCOMGlueLoad error %d:%d for file %s:\n%s\n",
-            linkEditError, errorNum, fileName, errorString);
-  }
-  return libHandle;
-}
-
-static NSFuncPtr
-GetSymbol(LibHandleType aLibHandle, const char* aSymbol)
-{
-  // Try to use |NSLookupSymbolInImage| since it is faster than searching
-  // the global symbol table.  If we couldn't get a mach_header pointer
-  // for the XPCOM dylib, then use |NSLookupAndBindSymbol| to search the
-  // global symbol table (this shouldn't normally happen, unless the user
-  // has called XPCOMGlueStartup(".") and relies on libxpcom.dylib
-  // already being loaded).
-  NSSymbol sym = nullptr;
-  if (aLibHandle) {
-    sym = NSLookupSymbolInImage(aLibHandle, aSymbol,
-                                NSLOOKUPSYMBOLINIMAGE_OPTION_BIND |
-                                NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR);
-  } else {
-    if (NSIsSymbolNameDefined(aSymbol)) {
-      sym = NSLookupAndBindSymbol(aSymbol);
-    }
-  }
-  if (!sym) {
-    return nullptr;
-  }
-
-  return (NSFuncPtr)NSAddressOfSymbol(sym);
-}
-
-static void
-CloseLibHandle(LibHandleType aLibHandle)
-{
-  // we cannot unload dylibs on OS X
-}
-
 #else
 #include <dlfcn.h>
 
 #if defined(MOZ_LINKER) && !defined(ANDROID)
 extern "C" {
 NS_HIDDEN __typeof(dlopen) __wrap_dlopen;
 NS_HIDDEN __typeof(dlsym) __wrap_dlsym;
 NS_HIDDEN __typeof(dlclose) __wrap_dlclose;
@@ -154,17 +91,22 @@ NS_HIDDEN __typeof(dlclose) __wrap_dlclo
 #define dlclose __wrap_dlclose
 #endif
 
 typedef void* LibHandleType;
 
 static LibHandleType
 GetLibHandle(pathstr_t aDependentLib)
 {
-  LibHandleType libHandle = dlopen(aDependentLib, RTLD_GLOBAL | RTLD_LAZY);
+  LibHandleType libHandle = dlopen(aDependentLib,
+                                   RTLD_GLOBAL | RTLD_LAZY
+#ifdef XP_MACOSX
+                                   | RTLD_FIRST
+#endif
+                                   );
   if (!libHandle) {
     fprintf(stderr, "XPCOMGlueLoad error for file %s:\n%s\n", aDependentLib,
             dlerror());
   }
   return libHandle;
 }
 
 static NSFuncPtr
@@ -380,17 +322,17 @@ XPCOMGlueLoad(const char* aXPCOMFile)
     if (!ReadDependentCB(xpcomDir, do_preload)) {
       XPCOMGlueUnload();
       return nullptr;
     }
   }
 
   GetFrozenFunctionsFunc sym =
     (GetFrozenFunctionsFunc)GetSymbol(sTop->libHandle,
-                                      LEADING_UNDERSCORE "NS_GetFrozenFunctions");
+                                      "NS_GetFrozenFunctions");
 
   if (!sym) { // No symbol found.
     XPCOMGlueUnload();
     return nullptr;
   }
 
   return sym;
 }
@@ -399,18 +341,17 @@ nsresult
 XPCOMGlueLoadXULFunctions(const nsDynamicFunctionLoad* aSymbols)
 {
   // We don't null-check sXULLibHandle because this might work even
   // if it is null (same as RTLD_DEFAULT)
 
   nsresult rv = NS_OK;
   while (aSymbols->functionName) {
     char buffer[512];
-    snprintf(buffer, sizeof(buffer),
-             LEADING_UNDERSCORE "%s", aSymbols->functionName);
+    snprintf(buffer, sizeof(buffer), "%s", aSymbols->functionName);
 
     *aSymbols->function = (NSFuncPtr)GetSymbol(sTop->libHandle, buffer);
     if (!*aSymbols->function) {
       rv = NS_ERROR_LOSS_OF_SIGNIFICANT_DATA;
     }
 
     ++aSymbols;
   }