1 /* -*- Mode: C++; tab-width: 6; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is Mozilla XPCOM.
17 * The Initial Developer of the Original Code is
18 * Benjamin Smedberg <benjamin@smedbergs.us>
20 * Portions created by the Initial Developer are Copyright (C) 2005
21 * the Initial Developer. All Rights Reserved.
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #include "nsGlueLinking.h"
40 #include "nsXPCOMGlue.h"
42 #include <mach-o/dyld.h>
43 #include <sys/param.h>
48 static const mach_header
* sXULLibImage
;
51 ReadDependentCB(const char *aDependentLib
)
53 (void) NSAddImage(aDependentLib
,
54 NSADDIMAGE_OPTION_RETURN_ON_ERROR
|
55 NSADDIMAGE_OPTION_MATCH_FILENAME_BY_INSTALLNAME
);
59 LookupSymbol(const mach_header
* aLib
, const char* aSymbolName
)
61 // Try to use |NSLookupSymbolInImage| since it is faster than searching
62 // the global symbol table. If we couldn't get a mach_header pointer
63 // for the XPCOM dylib, then use |NSLookupAndBindSymbol| to search the
64 // global symbol table (this shouldn't normally happen, unless the user
65 // has called XPCOMGlueStartup(".") and relies on libxpcom.dylib
66 // already being loaded).
67 NSSymbol sym
= nsnull
;
69 sym
= NSLookupSymbolInImage(aLib
, aSymbolName
,
70 NSLOOKUPSYMBOLINIMAGE_OPTION_BIND
|
71 NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR
);
73 if (NSIsSymbolNameDefined(aSymbolName
))
74 sym
= NSLookupAndBindSymbol(aSymbolName
);
79 return NSAddressOfSymbol(sym
);
82 GetFrozenFunctionsFunc
83 XPCOMGlueLoad(const char *xpcomFile
)
85 const mach_header
* lib
= nsnull
;
87 if (xpcomFile
[0] != '.' || xpcomFile
[1] != '\0') {
88 char xpcomDir
[PATH_MAX
];
89 if (realpath(xpcomFile
, xpcomDir
)) {
90 char *lastSlash
= strrchr(xpcomDir
, '/');
94 XPCOMGlueLoadDependentLibs(xpcomDir
, ReadDependentCB
);
96 snprintf(lastSlash
, PATH_MAX
- strlen(xpcomDir
), "/" XUL_DLL
);
98 sXULLibImage
= NSAddImage(xpcomDir
,
99 NSADDIMAGE_OPTION_RETURN_ON_ERROR
|
100 NSADDIMAGE_OPTION_WITH_SEARCHING
|
101 NSADDIMAGE_OPTION_MATCH_FILENAME_BY_INSTALLNAME
);
105 lib
= NSAddImage(xpcomFile
,
106 NSADDIMAGE_OPTION_RETURN_ON_ERROR
|
107 NSADDIMAGE_OPTION_WITH_SEARCHING
|
108 NSADDIMAGE_OPTION_MATCH_FILENAME_BY_INSTALLNAME
);
111 return (GetFrozenFunctionsFunc
) LookupSymbol(lib
, "_NS_GetFrozenFunctions");
117 // nothing to do, since we cannot unload dylibs on OS X
121 XPCOMGlueLoadXULFunctions(const nsDynamicFunctionLoad
*symbols
)
124 while (symbols
->functionName
) {
126 snprintf(buffer
, sizeof(buffer
), "_%s", symbols
->functionName
);
128 *symbols
->function
= (NSFuncPtr
) LookupSymbol(sXULLibImage
, buffer
);
129 if (!*symbols
->function
)
130 rv
= NS_ERROR_LOSS_OF_SIGNIFICANT_DATA
;