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"
47 #if defined(SUNOS4) || defined(NEXTSTEP) || \
48 (defined(OPENBSD) || defined(NETBSD)) && !defined(__ELF__)
49 #define LEADING_UNDERSCORE "_"
51 #define LEADING_UNDERSCORE
60 static DependentLib
*sTop
;
61 static void* sXULLibHandle
;
64 AppendDependentLib(void *libHandle
)
66 DependentLib
*d
= new DependentLib
;
71 d
->libHandle
= libHandle
;
77 ReadDependentCB(const char *aDependentLib
)
79 void *libHandle
= dlopen(aDependentLib
, RTLD_GLOBAL
| RTLD_LAZY
);
83 AppendDependentLib(libHandle
);
86 GetFrozenFunctionsFunc
87 XPCOMGlueLoad(const char *xpcomFile
)
89 char xpcomDir
[MAXPATHLEN
];
90 if (realpath(xpcomFile
, xpcomDir
)) {
91 char *lastSlash
= strrchr(xpcomDir
, '/');
95 XPCOMGlueLoadDependentLibs(xpcomDir
, ReadDependentCB
);
97 snprintf(lastSlash
, MAXPATHLEN
- strlen(xpcomDir
), "/" XUL_DLL
);
99 sXULLibHandle
= dlopen(xpcomDir
, RTLD_GLOBAL
| RTLD_LAZY
);
103 // RTLD_DEFAULT is not defined in non-GNU toolchains, and it is
104 // (void*) 0 in any case.
106 void *libHandle
= nsnull
;
108 if (xpcomFile
[0] != '.' || xpcomFile
[1] != '\0') {
109 libHandle
= dlopen(xpcomFile
, RTLD_GLOBAL
| RTLD_LAZY
);
111 AppendDependentLib(libHandle
);
115 GetFrozenFunctionsFunc sym
=
116 (GetFrozenFunctionsFunc
) dlsym(libHandle
,
117 LEADING_UNDERSCORE
"NS_GetFrozenFunctions");
129 dlclose(sTop
->libHandle
);
131 DependentLib
*temp
= sTop
;
138 dlclose(sXULLibHandle
);
139 sXULLibHandle
= nsnull
;
144 XPCOMGlueLoadXULFunctions(const nsDynamicFunctionLoad
*symbols
)
146 // We don't null-check sXULLibHandle because this might work even
147 // if it is null (same as RTLD_DEFAULT)
150 while (symbols
->functionName
) {
152 snprintf(buffer
, sizeof(buffer
),
153 LEADING_UNDERSCORE
"%s", symbols
->functionName
);
155 *symbols
->function
= (NSFuncPtr
) dlsym(sXULLibHandle
, buffer
);
156 if (!*symbols
->function
)
157 rv
= NS_ERROR_LOSS_OF_SIGNIFICANT_DATA
;