1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtCore module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qplatformdefs.h"
45 #include "qlibrary_p.h"
46 #include <qfileinfo.h>
47 #include <qcoreapplication.h>
52 # include <private/qcore_mac_p.h>
55 #if defined(QT_AOUT_UNDERSCORE)
59 #if defined(Q_OS_VXWORKS) || defined (Q_OS_NACL)
60 #define QT_NO_DYNAMIC_LIBRARY
65 #if !defined(QT_HPUX_LD) && !defined(QT_NO_DYNAMIC_LIBRARY)
66 QT_BEGIN_INCLUDE_NAMESPACE
68 QT_END_INCLUDE_NAMESPACE
71 static QString qdlerror()
73 #if defined(QT_NO_DYNAMIC_LIBRARY)
74 const char *err = "This platform does not support dynamic libraries.";
75 #elif !defined(QT_HPUX_LD)
76 const char *err = dlerror();
78 const char *err = strerror(errno);
80 return err ? QLatin1Char('(') + QString::fromLocal8Bit(err) + QLatin1Char(')'): QString();
83 bool QLibraryPrivate::load_sys()
86 #if !defined(QT_NO_DYNAMIC_LIBRARY)
87 QFileInfo fi(fileName);
89 #if defined(Q_OS_SYMBIAN)
90 QString path; // In Symbian, always resolve with just the filename
93 // Replace possible ".qtplugin" suffix with ".dll"
94 if (fi.suffix() == QLatin1String("qtplugin"))
95 name = fi.completeBaseName() + QLatin1String(".dll");
99 QString path = fi.path();
100 QString name = fi.fileName();
101 if (path == QLatin1String(".") && !fileName.startsWith(path))
104 path += QLatin1Char('/');
106 // The first filename we want to attempt to load is the filename as the callee specified.
107 // Thus, the first attempt we do must be with an empty prefix and empty suffix.
108 QStringList suffixes(QLatin1String("")), prefixes(QLatin1String(""));
109 if (pluginState != IsAPlugin) {
110 #if !defined(Q_OS_SYMBIAN)
111 prefixes << QLatin1String("lib");
113 #if defined(Q_OS_HPUX)
115 // http://docs.hp.com/en/B2355-90968/linkerdifferencesiapa.htm
117 // In PA-RISC (PA-32 and PA-64) shared libraries are suffixed
118 // with .sl. In IPF (32-bit and 64-bit), the shared libraries
119 // are suffixed with .so. For compatibility, the IPF linker
120 // also supports the .sl suffix.
122 // But since we don't know if we are built on HPUX or HPUXi,
123 // we support both .sl (and .<version>) and .so suffixes but
126 if (!fullVersion.isEmpty()) {
127 suffixes << QString::fromLatin1(".so.%1").arg(fullVersion);
129 suffixes << QLatin1String(".so");
132 if (!fullVersion.isEmpty()) {
133 suffixes << QString::fromLatin1(".sl.%1").arg(fullVersion);
134 suffixes << QString::fromLatin1(".%1").arg(fullVersion);
136 suffixes << QLatin1String(".sl");
138 #elif defined(Q_OS_SYMBIAN)
139 suffixes << QLatin1String(".dll");
144 if (!fullVersion.isEmpty()) {
145 suffixes << QString::fromLatin1(".so.%1").arg(fullVersion);
147 suffixes << QLatin1String(".so");
151 if (!fullVersion.isEmpty()) {
152 suffixes << QString::fromLatin1(".%1.bundle").arg(fullVersion);
153 suffixes << QString::fromLatin1(".%1.dylib").arg(fullVersion);
155 suffixes << QLatin1String(".bundle") << QLatin1String(".dylib");
160 #if defined(QT_HPUX_LD)
161 dlFlags = DYNAMIC_PATH | BIND_NONFATAL;
162 if (loadHints & QLibrary::ResolveAllSymbolsHint) {
163 dlFlags |= BIND_IMMEDIATE;
165 dlFlags |= BIND_DEFERRED;
168 if (loadHints & QLibrary::ResolveAllSymbolsHint) {
171 dlFlags |= RTLD_LAZY;
173 if (loadHints & QLibrary::ExportExternalSymbolsHint) {
174 dlFlags |= RTLD_GLOBAL;
176 #if !defined(Q_OS_CYGWIN)
178 #if defined(Q_OS_MAC)
179 if (QSysInfo::MacintoshVersion >= QSysInfo::MV_10_4)
181 dlFlags |= RTLD_LOCAL;
184 #if defined(Q_OS_AIX) // Not sure if any other platform actually support this thing.
185 if (loadHints & QLibrary::LoadArchiveMemberHint) {
186 dlFlags |= RTLD_MEMBER;
191 for(int prefix = 0; retry && !pHnd && prefix < prefixes.size(); prefix++) {
192 for(int suffix = 0; retry && !pHnd && suffix < suffixes.size(); suffix++) {
193 if (!prefixes.at(prefix).isEmpty() && name.startsWith(prefixes.at(prefix)))
195 if (!suffixes.at(suffix).isEmpty() && name.endsWith(suffixes.at(suffix)))
197 if (loadHints & QLibrary::LoadArchiveMemberHint) {
199 int lparen = attempt.indexOf(QLatin1Char('('));
201 lparen = attempt.count();
202 attempt = path + prefixes.at(prefix) + attempt.insert(lparen, suffixes.at(suffix));
204 attempt = path + prefixes.at(prefix) + name + suffixes.at(suffix);
206 #if defined(QT_HPUX_LD)
207 pHnd = (void*)shl_load(QFile::encodeName(attempt), dlFlags, 0);
209 pHnd = dlopen(QFile::encodeName(attempt), dlFlags);
212 #if defined(Q_OS_SYMBIAN)
213 // Never try again in symbian, dlopen already handles the library search logic,
214 // and there is only one possible suffix.
217 if (!pHnd && fileName.startsWith(QLatin1Char('/')) && QFile::exists(attempt)) {
218 // We only want to continue if dlopen failed due to that the shared library did not exist.
219 // However, we are only able to apply this check for absolute filenames (since they are
220 // not influenced by the content of LD_LIBRARY_PATH, /etc/ld.so.cache, DT_RPATH etc...)
221 // This is all because dlerror is flawed and cannot tell us the reason why it failed.
230 QByteArray utf8Bundle = fileName.toUtf8();
231 QCFType<CFURLRef> bundleUrl = CFURLCreateFromFileSystemRepresentation(NULL, reinterpret_cast<const UInt8*>(utf8Bundle.data()), utf8Bundle.length(), true);
232 QCFType<CFBundleRef> bundle = CFBundleCreate(NULL, bundleUrl);
234 QCFType<CFURLRef> url = CFBundleCopyExecutableURL(bundle);
235 char executableFile[FILENAME_MAX];
236 CFURLGetFileSystemRepresentation(url, true, reinterpret_cast<UInt8*>(executableFile), FILENAME_MAX);
237 attempt = QString::fromUtf8(executableFile);
238 pHnd = dlopen(QFile::encodeName(attempt), dlFlags);
242 #endif // QT_NO_DYNAMIC_LIBRARY
244 errorString = QLibrary::tr("Cannot load library %1: %2").arg(fileName).arg(qdlerror());
247 qualifiedFileName = attempt;
253 bool QLibraryPrivate::unload_sys()
255 #if !defined(QT_NO_DYNAMIC_LIBRARY)
256 # if defined(QT_HPUX_LD)
257 if (shl_unload((shl_t)pHnd)) {
261 errorString = QLibrary::tr("Cannot unload library %1: %2").arg(fileName).arg(qdlerror());
270 Q_CORE_EXPORT void *qt_mac_resolve_sys(void *handle, const char *symbol)
272 return dlsym(handle, symbol);
276 void* QLibraryPrivate::resolve_sys(const char* symbol)
278 #if defined(QT_AOUT_UNDERSCORE)
279 // older a.out systems add an underscore in front of symbols
280 char* undrscr_symbol = new char[strlen(symbol)+2];
281 undrscr_symbol[0] = '_';
282 strcpy(undrscr_symbol+1, symbol);
283 void* address = dlsym(pHnd, undrscr_symbol);
284 delete [] undrscr_symbol;
285 #elif defined(QT_HPUX_LD)
287 if (shl_findsym((shl_t*)&pHnd, symbol, TYPE_UNDEFINED, &address) < 0)
289 #elif defined (QT_NO_DYNAMIC_LIBRARY)
292 void* address = dlsym(pHnd, symbol);
295 errorString = QLibrary::tr("Cannot resolve symbol \"%1\" in %2: %3").arg(
296 QString::fromAscii(symbol)).arg(fileName).arg(qdlerror());
305 #endif // QT_NO_LIBRARY