Back to index

lightning-sunbird  0.9+nobinonly
nsGlueLinkingOSX.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 6; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is Mozilla XPCOM.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Benjamin Smedberg <benjamin@smedbergs.us>
00019  *
00020  * Portions created by the Initial Developer are Copyright (C) 2005
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "nsGlueLinking.h"
00040 #include "nsXPCOMGlue.h"
00041 
00042 #include <mach-o/dyld.h>
00043 #include <sys/param.h>
00044 #include <stdlib.h>
00045 #include <string.h>
00046 #include <stdio.h>
00047 
00048 static const mach_header* sXULLibImage;
00049 
00050 static void
00051 ReadDependentCB(const char *aDependentLib)
00052 {
00053     (void) NSAddImage(aDependentLib,
00054                       NSADDIMAGE_OPTION_RETURN_ON_ERROR |
00055                       NSADDIMAGE_OPTION_MATCH_FILENAME_BY_INSTALLNAME);
00056 }
00057 
00058 static void*
00059 LookupSymbol(const mach_header* aLib, const char* aSymbolName)
00060 {
00061     // Try to use |NSLookupSymbolInImage| since it is faster than searching
00062     // the global symbol table.  If we couldn't get a mach_header pointer
00063     // for the XPCOM dylib, then use |NSLookupAndBindSymbol| to search the
00064     // global symbol table (this shouldn't normally happen, unless the user
00065     // has called XPCOMGlueStartup(".") and relies on libxpcom.dylib
00066     // already being loaded).
00067     NSSymbol sym = nsnull;
00068     if (aLib) {
00069         sym = NSLookupSymbolInImage(aLib, aSymbolName,
00070                                  NSLOOKUPSYMBOLINIMAGE_OPTION_BIND |
00071                                  NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR);
00072     } else {
00073         if (NSIsSymbolNameDefined(aSymbolName))
00074             sym = NSLookupAndBindSymbol(aSymbolName);
00075     }
00076     if (!sym)
00077         return nsnull;
00078 
00079     return NSAddressOfSymbol(sym);
00080 }
00081 
00082 GetFrozenFunctionsFunc
00083 XPCOMGlueLoad(const char *xpcomFile)
00084 {
00085     const mach_header* lib = nsnull;
00086 
00087     if (xpcomFile[0] != '.' || xpcomFile[1] != '\0') {
00088         char xpcomDir[PATH_MAX];
00089         if (realpath(xpcomFile, xpcomDir)) {
00090             char *lastSlash = strrchr(xpcomDir, '/');
00091             if (lastSlash) {
00092                 *lastSlash = '\0';
00093 
00094                 XPCOMGlueLoadDependentLibs(xpcomDir, ReadDependentCB);
00095 
00096                 snprintf(lastSlash, PATH_MAX - strlen(xpcomDir), "/" XUL_DLL);
00097 
00098                 sXULLibImage = NSAddImage(xpcomDir,
00099                               NSADDIMAGE_OPTION_RETURN_ON_ERROR |
00100                               NSADDIMAGE_OPTION_WITH_SEARCHING |
00101                               NSADDIMAGE_OPTION_MATCH_FILENAME_BY_INSTALLNAME);
00102             }
00103         }
00104 
00105         lib = NSAddImage(xpcomFile,
00106                          NSADDIMAGE_OPTION_RETURN_ON_ERROR |
00107                          NSADDIMAGE_OPTION_WITH_SEARCHING |
00108                          NSADDIMAGE_OPTION_MATCH_FILENAME_BY_INSTALLNAME);
00109     }
00110 
00111     return (GetFrozenFunctionsFunc) LookupSymbol(lib, "_NS_GetFrozenFunctions");
00112 }
00113 
00114 void
00115 XPCOMGlueUnload()
00116 {
00117   // nothing to do, since we cannot unload dylibs on OS X
00118 }
00119 
00120 nsresult
00121 XPCOMGlueLoadXULFunctions(const nsDynamicFunctionLoad *symbols)
00122 {
00123     nsresult rv = NS_OK;
00124     while (symbols->functionName) {
00125         char buffer[512];
00126         snprintf(buffer, sizeof(buffer), "_%s", symbols->functionName);
00127 
00128         *symbols->function = (NSFuncPtr) LookupSymbol(sXULLibImage, buffer);
00129         if (!*symbols->function)
00130             rv = NS_ERROR_LOSS_OF_SIGNIFICANT_DATA;
00131 
00132         ++symbols;
00133     }
00134 
00135     return rv;
00136 }