Back to index

lightning-sunbird  0.9+nobinonly
nsGlueLinkingDlopen.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 <dlfcn.h>
00043 #include <stdlib.h>
00044 #include <string.h>
00045 #include <stdio.h>
00046 
00047 #if defined(SUNOS4) || defined(NEXTSTEP) || \
00048     (defined(OPENBSD) || defined(NETBSD)) && !defined(__ELF__)
00049 #define LEADING_UNDERSCORE "_"
00050 #else
00051 #define LEADING_UNDERSCORE
00052 #endif
00053 
00054 struct DependentLib
00055 {
00056     void         *libHandle;
00057     DependentLib *next;
00058 };
00059 
00060 static DependentLib *sTop;
00061 static void* sXULLibHandle;
00062 
00063 static void
00064 AppendDependentLib(void *libHandle)
00065 {
00066     DependentLib *d = new DependentLib;
00067     if (!d)
00068         return;
00069 
00070     d->next = sTop;
00071     d->libHandle = libHandle;
00072 
00073     sTop = d;
00074 }
00075 
00076 static void
00077 ReadDependentCB(const char *aDependentLib)
00078 {
00079     void *libHandle = dlopen(aDependentLib, RTLD_GLOBAL | RTLD_LAZY);
00080     if (!libHandle)
00081         return;
00082 
00083     AppendDependentLib(libHandle);
00084 }
00085 
00086 GetFrozenFunctionsFunc
00087 XPCOMGlueLoad(const char *xpcomFile)
00088 {
00089     char xpcomDir[MAXPATHLEN];
00090     if (realpath(xpcomFile, xpcomDir)) {
00091         char *lastSlash = strrchr(xpcomDir, '/');
00092         if (lastSlash) {
00093             *lastSlash = '\0';
00094 
00095             XPCOMGlueLoadDependentLibs(xpcomDir, ReadDependentCB);
00096 
00097             snprintf(lastSlash, MAXPATHLEN - strlen(xpcomDir), "/" XUL_DLL);
00098 
00099             sXULLibHandle = dlopen(xpcomDir, RTLD_GLOBAL | RTLD_LAZY);
00100         }
00101     }
00102 
00103     // RTLD_DEFAULT is not defined in non-GNU toolchains, and it is
00104     // (void*) 0 in any case.
00105 
00106     void *libHandle = nsnull;
00107 
00108     if (xpcomFile[0] != '.' || xpcomFile[1] != '\0') {
00109         libHandle = dlopen(xpcomFile, RTLD_GLOBAL | RTLD_LAZY);
00110         if (libHandle) {
00111             AppendDependentLib(libHandle);
00112         }
00113     }
00114 
00115     GetFrozenFunctionsFunc sym =
00116         (GetFrozenFunctionsFunc) dlsym(libHandle,
00117                                        LEADING_UNDERSCORE "NS_GetFrozenFunctions");
00118 
00119     if (!sym)
00120         XPCOMGlueUnload();
00121 
00122     return sym;
00123 }
00124 
00125 void
00126 XPCOMGlueUnload()
00127 {
00128     while (sTop) {
00129         dlclose(sTop->libHandle);
00130 
00131         DependentLib *temp = sTop;
00132         sTop = sTop->next;
00133 
00134         delete temp;
00135     }
00136 
00137     if (sXULLibHandle) {
00138         dlclose(sXULLibHandle);
00139         sXULLibHandle = nsnull;
00140     }
00141 }
00142 
00143 nsresult
00144 XPCOMGlueLoadXULFunctions(const nsDynamicFunctionLoad *symbols)
00145 {
00146     // We don't null-check sXULLibHandle because this might work even
00147     // if it is null (same as RTLD_DEFAULT)
00148 
00149     nsresult rv = NS_OK;
00150     while (symbols->functionName) {
00151         char buffer[512];
00152         snprintf(buffer, sizeof(buffer),
00153                  LEADING_UNDERSCORE "%s", symbols->functionName);
00154 
00155         *symbols->function = (NSFuncPtr) dlsym(sXULLibHandle, buffer);
00156         if (!*symbols->function)
00157             rv = NS_ERROR_LOSS_OF_SIGNIFICANT_DATA;
00158 
00159         ++symbols;
00160     }
00161     return rv;
00162 }