source: pkg/viola/main/xulrunner/branches/upstream/current/xpcom/glue/standalone/nsGlueLinkingDlopen.cpp @ 4081

Revision 4081, 4.4 KB checked in by alanbach-guest, 6 years ago (diff)

[svn-inject] Installing original source of xulrunner

Line 
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
4 *
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/
9 *
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
13 * License.
14 *
15 * The Original Code is Mozilla XPCOM.
16 *
17 * The Initial Developer of the Original Code is
18 * Benjamin Smedberg <benjamin@smedbergs.us>
19 *
20 * Portions created by the Initial Developer are Copyright (C) 2005
21 * the Initial Developer. All Rights Reserved.
22 *
23 * Contributor(s):
24 *
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.
36 *
37 * ***** END LICENSE BLOCK ***** */
38
39#include "nsGlueLinking.h"
40#include "nsXPCOMGlue.h"
41
42#include <dlfcn.h>
43#include <stdlib.h>
44#include <string.h>
45#include <stdio.h>
46
47#if defined(SUNOS4) || defined(NEXTSTEP) || \
48    (defined(OPENBSD) || defined(NETBSD)) && !defined(__ELF__)
49#define LEADING_UNDERSCORE "_"
50#else
51#define LEADING_UNDERSCORE
52#endif
53
54struct DependentLib
55{
56    void         *libHandle;
57    DependentLib *next;
58};
59
60static DependentLib *sTop;
61static void* sXULLibHandle;
62
63static void
64AppendDependentLib(void *libHandle)
65{
66    DependentLib *d = new DependentLib;
67    if (!d)
68        return;
69
70    d->next = sTop;
71    d->libHandle = libHandle;
72
73    sTop = d;
74}
75
76static void
77ReadDependentCB(const char *aDependentLib)
78{
79    void *libHandle = dlopen(aDependentLib, RTLD_GLOBAL | RTLD_LAZY);
80    if (!libHandle)
81        return;
82
83    AppendDependentLib(libHandle);
84}
85
86GetFrozenFunctionsFunc
87XPCOMGlueLoad(const char *xpcomFile)
88{
89    char xpcomDir[MAXPATHLEN];
90    if (realpath(xpcomFile, xpcomDir)) {
91        char *lastSlash = strrchr(xpcomDir, '/');
92        if (lastSlash) {
93            *lastSlash = '\0';
94
95            XPCOMGlueLoadDependentLibs(xpcomDir, ReadDependentCB);
96
97            snprintf(lastSlash, MAXPATHLEN - strlen(xpcomDir), "/" XUL_DLL);
98
99            sXULLibHandle = dlopen(xpcomDir, RTLD_GLOBAL | RTLD_LAZY);
100        }
101    }
102
103    // RTLD_DEFAULT is not defined in non-GNU toolchains, and it is
104    // (void*) 0 in any case.
105
106    void *libHandle = nsnull;
107
108    if (xpcomFile[0] != '.' || xpcomFile[1] != '\0') {
109        libHandle = dlopen(xpcomFile, RTLD_GLOBAL | RTLD_LAZY);
110        if (libHandle) {
111            AppendDependentLib(libHandle);
112        }
113    }
114
115    GetFrozenFunctionsFunc sym =
116        (GetFrozenFunctionsFunc) dlsym(libHandle,
117                                       LEADING_UNDERSCORE "NS_GetFrozenFunctions");
118
119    if (!sym)
120        XPCOMGlueUnload();
121
122    return sym;
123}
124
125void
126XPCOMGlueUnload()
127{
128    while (sTop) {
129        dlclose(sTop->libHandle);
130
131        DependentLib *temp = sTop;
132        sTop = sTop->next;
133
134        delete temp;
135    }
136
137    if (sXULLibHandle) {
138        dlclose(sXULLibHandle);
139        sXULLibHandle = nsnull;
140    }
141}
142
143nsresult
144XPCOMGlueLoadXULFunctions(const nsDynamicFunctionLoad *symbols)
145{
146    // We don't null-check sXULLibHandle because this might work even
147    // if it is null (same as RTLD_DEFAULT)
148
149    nsresult rv = NS_OK;
150    while (symbols->functionName) {
151        char buffer[512];
152        snprintf(buffer, sizeof(buffer),
153                 LEADING_UNDERSCORE "%s", symbols->functionName);
154
155        *symbols->function = (NSFuncPtr) dlsym(sXULLibHandle, buffer);
156        if (!*symbols->function)
157            rv = NS_ERROR_LOSS_OF_SIGNIFICANT_DATA;
158
159        ++symbols;
160    }
161    return rv;
162}
Note: See TracBrowser for help on using the repository browser.