Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/corelib/plugin/qlibrary.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||||||||
2 | ** | - | ||||||||||||
3 | ** Copyright (C) 2016 The Qt Company Ltd. | - | ||||||||||||
4 | ** Copyright (C) 2016 Intel Corporation. | - | ||||||||||||
5 | ** Contact: https://www.qt.io/licensing/ | - | ||||||||||||
6 | ** | - | ||||||||||||
7 | ** This file is part of the QtCore module of the Qt Toolkit. | - | ||||||||||||
8 | ** | - | ||||||||||||
9 | ** $QT_BEGIN_LICENSE:LGPL$ | - | ||||||||||||
10 | ** Commercial License Usage | - | ||||||||||||
11 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||||||||
12 | ** accordance with the commercial license agreement provided with the | - | ||||||||||||
13 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||||||||
14 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||||||||
15 | ** and conditions see https://www.qt.io/terms-conditions. For further | - | ||||||||||||
16 | ** information use the contact form at https://www.qt.io/contact-us. | - | ||||||||||||
17 | ** | - | ||||||||||||
18 | ** GNU Lesser General Public License Usage | - | ||||||||||||
19 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||||||||
20 | ** General Public License version 3 as published by the Free Software | - | ||||||||||||
21 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | - | ||||||||||||
22 | ** packaging of this file. Please review the following information to | - | ||||||||||||
23 | ** ensure the GNU Lesser General Public License version 3 requirements | - | ||||||||||||
24 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | - | ||||||||||||
25 | ** | - | ||||||||||||
26 | ** GNU General Public License Usage | - | ||||||||||||
27 | ** Alternatively, this file may be used under the terms of the GNU | - | ||||||||||||
28 | ** General Public License version 2.0 or (at your option) the GNU General | - | ||||||||||||
29 | ** Public license version 3 or any later version approved by the KDE Free | - | ||||||||||||
30 | ** Qt Foundation. The licenses are as published by the Free Software | - | ||||||||||||
31 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | - | ||||||||||||
32 | ** included in the packaging of this file. Please review the following | - | ||||||||||||
33 | ** information to ensure the GNU General Public License requirements will | - | ||||||||||||
34 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | - | ||||||||||||
35 | ** https://www.gnu.org/licenses/gpl-3.0.html. | - | ||||||||||||
36 | ** | - | ||||||||||||
37 | ** $QT_END_LICENSE$ | - | ||||||||||||
38 | ** | - | ||||||||||||
39 | ****************************************************************************/ | - | ||||||||||||
40 | #include "qplatformdefs.h" | - | ||||||||||||
41 | #include "qlibrary.h" | - | ||||||||||||
42 | - | |||||||||||||
43 | #ifndef QT_NO_LIBRARY | - | ||||||||||||
44 | - | |||||||||||||
45 | #include "qlibrary_p.h" | - | ||||||||||||
46 | #include <qstringlist.h> | - | ||||||||||||
47 | #include <qfile.h> | - | ||||||||||||
48 | #include <qfileinfo.h> | - | ||||||||||||
49 | #include <qmutex.h> | - | ||||||||||||
50 | #include <qmap.h> | - | ||||||||||||
51 | #include <private/qcoreapplication_p.h> | - | ||||||||||||
52 | #ifdef Q_OS_MAC | - | ||||||||||||
53 | # include <private/qcore_mac_p.h> | - | ||||||||||||
54 | #endif | - | ||||||||||||
55 | #ifndef NO_ERRNO_H | - | ||||||||||||
56 | #include <errno.h> | - | ||||||||||||
57 | #endif // NO_ERROR_H | - | ||||||||||||
58 | #include <qdebug.h> | - | ||||||||||||
59 | #include <qvector.h> | - | ||||||||||||
60 | #include <qdir.h> | - | ||||||||||||
61 | #include <qendian.h> | - | ||||||||||||
62 | #include <qjsondocument.h> | - | ||||||||||||
63 | #include <qjsonvalue.h> | - | ||||||||||||
64 | #include "qelfparser_p.h" | - | ||||||||||||
65 | #include "qmachparser_p.h" | - | ||||||||||||
66 | - | |||||||||||||
67 | QT_BEGIN_NAMESPACE | - | ||||||||||||
68 | - | |||||||||||||
69 | #ifdef QT_NO_DEBUG | - | ||||||||||||
70 | # define QLIBRARY_AS_DEBUG false | - | ||||||||||||
71 | #else | - | ||||||||||||
72 | # define QLIBRARY_AS_DEBUG true | - | ||||||||||||
73 | #endif | - | ||||||||||||
74 | - | |||||||||||||
75 | #if defined(Q_OS_UNIX) | - | ||||||||||||
76 | // We don't use separate debug and release libs on UNIX, so we want | - | ||||||||||||
77 | // to allow loading plugins, regardless of how they were built. | - | ||||||||||||
78 | # define QT_NO_DEBUG_PLUGIN_CHECK | - | ||||||||||||
79 | #endif | - | ||||||||||||
80 | - | |||||||||||||
81 | /*! | - | ||||||||||||
82 | \class QLibrary | - | ||||||||||||
83 | \inmodule QtCore | - | ||||||||||||
84 | \reentrant | - | ||||||||||||
85 | \brief The QLibrary class loads shared libraries at runtime. | - | ||||||||||||
86 | - | |||||||||||||
87 | - | |||||||||||||
88 | \ingroup plugins | - | ||||||||||||
89 | - | |||||||||||||
90 | An instance of a QLibrary object operates on a single shared | - | ||||||||||||
91 | object file (which we call a "library", but is also known as a | - | ||||||||||||
92 | "DLL"). A QLibrary provides access to the functionality in the | - | ||||||||||||
93 | library in a platform independent way. You can either pass a file | - | ||||||||||||
94 | name in the constructor, or set it explicitly with setFileName(). | - | ||||||||||||
95 | When loading the library, QLibrary searches in all the | - | ||||||||||||
96 | system-specific library locations (e.g. \c LD_LIBRARY_PATH on | - | ||||||||||||
97 | Unix), unless the file name has an absolute path. | - | ||||||||||||
98 | - | |||||||||||||
99 | If the file name is an absolute path then an attempt is made to | - | ||||||||||||
100 | load this path first. If the file cannot be found, QLibrary tries | - | ||||||||||||
101 | the name with different platform-specific file prefixes, like | - | ||||||||||||
102 | "lib" on Unix and Mac, and suffixes, like ".so" on Unix, ".dylib" | - | ||||||||||||
103 | on the Mac, or ".dll" on Windows. | - | ||||||||||||
104 | - | |||||||||||||
105 | If the file path is not absolute then QLibrary modifies the search | - | ||||||||||||
106 | order to try the system-specific prefixes and suffixes first, | - | ||||||||||||
107 | followed by the file path specified. | - | ||||||||||||
108 | - | |||||||||||||
109 | This makes it possible to specify shared libraries that are only | - | ||||||||||||
110 | identified by their basename (i.e. without their suffix), so the | - | ||||||||||||
111 | same code will work on different operating systems yet still | - | ||||||||||||
112 | minimise the number of attempts to find the library. | - | ||||||||||||
113 | - | |||||||||||||
114 | The most important functions are load() to dynamically load the | - | ||||||||||||
115 | library file, isLoaded() to check whether loading was successful, | - | ||||||||||||
116 | and resolve() to resolve a symbol in the library. The resolve() | - | ||||||||||||
117 | function implicitly tries to load the library if it has not been | - | ||||||||||||
118 | loaded yet. Multiple instances of QLibrary can be used to access | - | ||||||||||||
119 | the same physical library. Once loaded, libraries remain in memory | - | ||||||||||||
120 | until the application terminates. You can attempt to unload a | - | ||||||||||||
121 | library using unload(), but if other instances of QLibrary are | - | ||||||||||||
122 | using the same library, the call will fail, and unloading will | - | ||||||||||||
123 | only happen when every instance has called unload(). | - | ||||||||||||
124 | - | |||||||||||||
125 | A typical use of QLibrary is to resolve an exported symbol in a | - | ||||||||||||
126 | library, and to call the C function that this symbol represents. | - | ||||||||||||
127 | This is called "explicit linking" in contrast to "implicit | - | ||||||||||||
128 | linking", which is done by the link step in the build process when | - | ||||||||||||
129 | linking an executable against a library. | - | ||||||||||||
130 | - | |||||||||||||
131 | The following code snippet loads a library, resolves the symbol | - | ||||||||||||
132 | "mysymbol", and calls the function if everything succeeded. If | - | ||||||||||||
133 | something goes wrong, e.g. the library file does not exist or the | - | ||||||||||||
134 | symbol is not defined, the function pointer will be 0 and won't be | - | ||||||||||||
135 | called. | - | ||||||||||||
136 | - | |||||||||||||
137 | \snippet code/src_corelib_plugin_qlibrary.cpp 0 | - | ||||||||||||
138 | - | |||||||||||||
139 | The symbol must be exported as a C function from the library for | - | ||||||||||||
140 | resolve() to work. This means that the function must be wrapped in | - | ||||||||||||
141 | an \c{extern "C"} block if the library is compiled with a C++ | - | ||||||||||||
142 | compiler. On Windows, this also requires the use of a \c dllexport | - | ||||||||||||
143 | macro; see resolve() for the details of how this is done. For | - | ||||||||||||
144 | convenience, there is a static resolve() function which you can | - | ||||||||||||
145 | use if you just want to call a function in a library without | - | ||||||||||||
146 | explicitly loading the library first: | - | ||||||||||||
147 | - | |||||||||||||
148 | \snippet code/src_corelib_plugin_qlibrary.cpp 1 | - | ||||||||||||
149 | - | |||||||||||||
150 | \sa QPluginLoader | - | ||||||||||||
151 | */ | - | ||||||||||||
152 | - | |||||||||||||
153 | /*! | - | ||||||||||||
154 | \enum QLibrary::LoadHint | - | ||||||||||||
155 | - | |||||||||||||
156 | This enum describes the possible hints that can be used to change the way | - | ||||||||||||
157 | libraries are handled when they are loaded. These values indicate how | - | ||||||||||||
158 | symbols are resolved when libraries are loaded, and are specified using | - | ||||||||||||
159 | the setLoadHints() function. | - | ||||||||||||
160 | - | |||||||||||||
161 | \value ResolveAllSymbolsHint | - | ||||||||||||
162 | Causes all symbols in a library to be resolved when it is loaded, not | - | ||||||||||||
163 | simply when resolve() is called. | - | ||||||||||||
164 | \value ExportExternalSymbolsHint | - | ||||||||||||
165 | Exports unresolved and external symbols in the library so that they can be | - | ||||||||||||
166 | resolved in other dynamically-loaded libraries loaded later. | - | ||||||||||||
167 | \value LoadArchiveMemberHint | - | ||||||||||||
168 | Allows the file name of the library to specify a particular object file | - | ||||||||||||
169 | within an archive file. | - | ||||||||||||
170 | If this hint is given, the filename of the library consists of | - | ||||||||||||
171 | a path, which is a reference to an archive file, followed by | - | ||||||||||||
172 | a reference to the archive member. | - | ||||||||||||
173 | \value PreventUnloadHint | - | ||||||||||||
174 | Prevents the library from being unloaded from the address space if close() | - | ||||||||||||
175 | is called. The library's static variables are not reinitialized if open() | - | ||||||||||||
176 | is called at a later time. | - | ||||||||||||
177 | \value DeepBindHint | - | ||||||||||||
178 | Instructs the linker to prefer definitions in the loaded library | - | ||||||||||||
179 | over exported definitions in the loading application when resolving | - | ||||||||||||
180 | external symbols in the loaded library. This option is only supported | - | ||||||||||||
181 | on Linux. | - | ||||||||||||
182 | - | |||||||||||||
183 | \sa loadHints | - | ||||||||||||
184 | */ | - | ||||||||||||
185 | - | |||||||||||||
186 | - | |||||||||||||
187 | static long qt_find_pattern(const char *s, ulong s_len, | - | ||||||||||||
188 | const char *pattern, ulong p_len) | - | ||||||||||||
189 | { | - | ||||||||||||
190 | /* | - | ||||||||||||
191 | we search from the end of the file because on the supported | - | ||||||||||||
192 | systems, the read-only data/text segments are placed at the end | - | ||||||||||||
193 | of the file. HOWEVER, when building with debugging enabled, all | - | ||||||||||||
194 | the debug symbols are placed AFTER the data/text segments. | - | ||||||||||||
195 | - | |||||||||||||
196 | what does this mean? when building in release mode, the search | - | ||||||||||||
197 | is fast because the data we are looking for is at the end of the | - | ||||||||||||
198 | file... when building in debug mode, the search is slower | - | ||||||||||||
199 | because we have to skip over all the debugging symbols first | - | ||||||||||||
200 | */ | - | ||||||||||||
201 | - | |||||||||||||
202 | if (! s || ! pattern || p_len > s_len) return -1; | - | ||||||||||||
203 | ulong i, hs = 0, hp = 0, delta = s_len - p_len; | - | ||||||||||||
204 | - | |||||||||||||
205 | for (i = 0; i < p_len; ++i) { | - | ||||||||||||
206 | hs += s[delta + i]; | - | ||||||||||||
207 | hp += pattern[i]; | - | ||||||||||||
208 | } | - | ||||||||||||
209 | i = delta; | - | ||||||||||||
210 | for (;;) { | - | ||||||||||||
211 | if (hs == hp && qstrncmp(s + i, pattern, p_len) == 0) | - | ||||||||||||
212 | return i; | - | ||||||||||||
213 | if (i == 0) | - | ||||||||||||
214 | break; | - | ||||||||||||
215 | --i; | - | ||||||||||||
216 | hs -= s[i + p_len]; | - | ||||||||||||
217 | hs += s[i]; | - | ||||||||||||
218 | } | - | ||||||||||||
219 | - | |||||||||||||
220 | return -1; | - | ||||||||||||
221 | } | - | ||||||||||||
222 | - | |||||||||||||
223 | /* | - | ||||||||||||
224 | This opens the specified library, mmaps it into memory, and searches | - | ||||||||||||
225 | for the QT_PLUGIN_VERIFICATION_DATA. The advantage of this approach is that | - | ||||||||||||
226 | we can get the verification data without have to actually load the library. | - | ||||||||||||
227 | This lets us detect mismatches more safely. | - | ||||||||||||
228 | - | |||||||||||||
229 | Returns \c false if version information is not present, or if the | - | ||||||||||||
230 | information could not be read. | - | ||||||||||||
231 | Returns true if version information is present and successfully read. | - | ||||||||||||
232 | */ | - | ||||||||||||
233 | static bool findPatternUnloaded(const QString &library, QLibraryPrivate *lib) | - | ||||||||||||
234 | { | - | ||||||||||||
235 | QFile file(library); | - | ||||||||||||
236 | if (!file.open(QIODevice::ReadOnly)) {
| 0-1396 | ||||||||||||
237 | if (lib)
| 0 | ||||||||||||
238 | lib->errorString = file.errorString(); never executed: lib->errorString = file.errorString(); | 0 | ||||||||||||
239 | if (qt_debug_component()) {
| 0 | ||||||||||||
240 | qWarning("%s: %s", (const char*) QFile::encodeName(library), | - | ||||||||||||
241 | qPrintable(qt_error_string(errno))); | - | ||||||||||||
242 | } never executed: end of block | 0 | ||||||||||||
243 | return false; never executed: return false; | 0 | ||||||||||||
244 | } | - | ||||||||||||
245 | - | |||||||||||||
246 | QByteArray data; | - | ||||||||||||
247 | const char *filedata = 0; | - | ||||||||||||
248 | ulong fdlen = file.size(); | - | ||||||||||||
249 | filedata = (char *) file.map(0, fdlen); | - | ||||||||||||
250 | if (filedata == 0) {
| 0-1396 | ||||||||||||
251 | // try reading the data into memory instead | - | ||||||||||||
252 | data = file.readAll(); | - | ||||||||||||
253 | filedata = data.constData(); | - | ||||||||||||
254 | fdlen = data.size(); | - | ||||||||||||
255 | } never executed: end of block | 0 | ||||||||||||
256 | - | |||||||||||||
257 | /* | - | ||||||||||||
258 | ELF and Mach-O binaries with GCC have .qplugin sections. | - | ||||||||||||
259 | */ | - | ||||||||||||
260 | bool hasMetaData = false; | - | ||||||||||||
261 | long pos = 0; | - | ||||||||||||
262 | char pattern[] = "qTMETADATA "; | - | ||||||||||||
263 | pattern[0] = 'Q'; // Ensure the pattern "QTMETADATA" is not found in this library should QPluginLoader ever encounter it. | - | ||||||||||||
264 | const ulong plen = qstrlen(pattern); | - | ||||||||||||
265 | #if defined (Q_OF_ELF) && defined(Q_CC_GNU) | - | ||||||||||||
266 | int r = QElfParser().parse(filedata, fdlen, library, lib, &pos, &fdlen); | - | ||||||||||||
267 | if (r == QElfParser::Corrupt || r == QElfParser::NotElf) {
| 3-1393 | ||||||||||||
268 | if (lib && qt_debug_component()) {
| 0-719 | ||||||||||||
269 | qWarning("QElfParser: %s",qPrintable(lib->errorString)); | - | ||||||||||||
270 | } never executed: end of block | 0 | ||||||||||||
271 | return false; executed 719 times by 112 tests: return false; Executed by:
| 719 | ||||||||||||
272 | } else if (r == QElfParser::QtMetaDataSection) {
| 1-676 | ||||||||||||
273 | long rel = qt_find_pattern(filedata + pos, fdlen, pattern, plen); | - | ||||||||||||
274 | if (rel < 0)
| 0-676 | ||||||||||||
275 | pos = -1; never executed: pos = -1; | 0 | ||||||||||||
276 | else | - | ||||||||||||
277 | pos += rel; executed 676 times by 111 tests: pos += rel; Executed by:
| 676 | ||||||||||||
278 | hasMetaData = true; | - | ||||||||||||
279 | } executed 676 times by 111 tests: end of block Executed by:
| 676 | ||||||||||||
280 | #elif defined (Q_OF_MACH_O) | - | ||||||||||||
281 | { | - | ||||||||||||
282 | QString errorString; | - | ||||||||||||
283 | int r = QMachOParser::parse(filedata, fdlen, library, &errorString, &pos, &fdlen); | - | ||||||||||||
284 | if (r == QMachOParser::NotSuitable) { | - | ||||||||||||
285 | if (qt_debug_component()) | - | ||||||||||||
286 | qWarning("QMachOParser: %s", qPrintable(errorString)); | - | ||||||||||||
287 | if (lib) | - | ||||||||||||
288 | lib->errorString = errorString; | - | ||||||||||||
289 | return false; | - | ||||||||||||
290 | } | - | ||||||||||||
291 | // even if the metadata section was not found, the Mach-O parser will | - | ||||||||||||
292 | // at least return the boundaries of the right architecture | - | ||||||||||||
293 | long rel = qt_find_pattern(filedata + pos, fdlen, pattern, plen); | - | ||||||||||||
294 | if (rel < 0) | - | ||||||||||||
295 | pos = -1; | - | ||||||||||||
296 | else | - | ||||||||||||
297 | pos += rel; | - | ||||||||||||
298 | hasMetaData = true; | - | ||||||||||||
299 | } | - | ||||||||||||
300 | #else | - | ||||||||||||
301 | pos = qt_find_pattern(filedata, fdlen, pattern, plen); | - | ||||||||||||
302 | if (pos > 0) | - | ||||||||||||
303 | hasMetaData = true; | - | ||||||||||||
304 | #endif // defined(Q_OF_ELF) && defined(Q_CC_GNU) | - | ||||||||||||
305 | - | |||||||||||||
306 | bool ret = false; | - | ||||||||||||
307 | - | |||||||||||||
308 | if (pos >= 0) {
| 0-677 | ||||||||||||
309 | if (hasMetaData) {
| 1-676 | ||||||||||||
310 | const char *data = filedata + pos; | - | ||||||||||||
311 | QJsonDocument doc = QLibraryPrivate::fromRawMetaDataqJsonFromRawLibraryMetaData(data); | - | ||||||||||||
312 | lib->metaData = doc.object(); | - | ||||||||||||
313 | if (qt_debug_component())
| 0-676 | ||||||||||||
314 | qWarning("Found metadata in lib %s, metadata=\n%s\n", never executed: QMessageLogger(__FILE__, 314, __PRETTY_FUNCTION__).warning("Found metadata in lib %s, metadata=\n%s\n", library.toLocal8Bit().constData(), doc.toJson().constData()); | 0 | ||||||||||||
315 | library.toLocal8Bit().constData(), doc.toJson().constData()); never executed: QMessageLogger(__FILE__, 314, __PRETTY_FUNCTION__).warning("Found metadata in lib %s, metadata=\n%s\n", library.toLocal8Bit().constData(), doc.toJson().constData()); | 0 | ||||||||||||
316 | ret = !doc.isNull(); | - | ||||||||||||
317 | } executed 676 times by 111 tests: end of block Executed by:
| 676 | ||||||||||||
318 | } executed 677 times by 111 tests: end of block Executed by:
| 677 | ||||||||||||
319 | - | |||||||||||||
320 | if (!ret && lib)
| 0-676 | ||||||||||||
321 | lib->errorString = QLibrary::tr("Failed to extract plugin meta data from '%1'").arg(library); executed 1 time by 1 test: lib->errorString = QLibrary::tr("Failed to extract plugin meta data from '%1'").arg(library); Executed by:
| 1 | ||||||||||||
322 | file.close(); | - | ||||||||||||
323 | return ret; executed 677 times by 111 tests: return ret; Executed by:
| 677 | ||||||||||||
324 | } | - | ||||||||||||
325 | - | |||||||||||||
326 | static void installCoverageTool(QLibraryPrivate *libPrivate) | - | ||||||||||||
327 | { | - | ||||||||||||
328 | #ifdef __COVERAGESCANNER__ | - | ||||||||||||
329 | /* | - | ||||||||||||
330 | __COVERAGESCANNER__ is defined when Qt has been instrumented for code | - | ||||||||||||
331 | coverage by TestCocoon. CoverageScanner is the name of the tool that | - | ||||||||||||
332 | generates the code instrumentation. | - | ||||||||||||
333 | This code is required here when code coverage analysis with TestCocoon | - | ||||||||||||
334 | is enabled in order to allow the loading application to register the plugin | - | ||||||||||||
335 | and then store its execution report. The execution report gathers information | - | ||||||||||||
336 | about each part of the plugin's code that has been used when | - | ||||||||||||
337 | the plugin was loaded by the launching application. | - | ||||||||||||
338 | The execution report for the plugin will go to the same execution report | - | ||||||||||||
339 | as the one defined for the application loading it. | - | ||||||||||||
340 | */ | - | ||||||||||||
341 | - | |||||||||||||
342 | int ret = __coveragescanner_register_library(libPrivate->fileName.toLocal8Bit()); | - | ||||||||||||
343 | - | |||||||||||||
344 | if (qt_debug_component()) { | - | ||||||||||||
345 | if (ret >= 0) { | - | ||||||||||||
346 | qDebug("coverage data for %s registered", | - | ||||||||||||
347 | qPrintable(libPrivate->fileName)); | - | ||||||||||||
348 | } else { | - | ||||||||||||
349 | qWarning("could not register %s: error %d; coverage data may be incomplete", | - | ||||||||||||
350 | qPrintable(libPrivate->fileName), | - | ||||||||||||
351 | ret); | - | ||||||||||||
352 | } | - | ||||||||||||
353 | } | - | ||||||||||||
354 | #else | - | ||||||||||||
355 | Q_UNUSED(libPrivate); | - | ||||||||||||
356 | #endif | - | ||||||||||||
357 | } | - | ||||||||||||
358 | - | |||||||||||||
359 | class QLibraryStore | - | ||||||||||||
360 | { | - | ||||||||||||
361 | public: | - | ||||||||||||
362 | inline ~QLibraryStore(); | - | ||||||||||||
363 | static inline QLibraryPrivate *findOrCreate(const QString &fileName, const QString &version, QLibrary::LoadHints loadHints); | - | ||||||||||||
364 | static inline void releaseLibrary(QLibraryPrivate *lib); | - | ||||||||||||
365 | - | |||||||||||||
366 | static inline void cleanup(); | - | ||||||||||||
367 | - | |||||||||||||
368 | private: | - | ||||||||||||
369 | static inline QLibraryStore *instance(); | - | ||||||||||||
370 | - | |||||||||||||
371 | // all members and instance() are protected by qt_library_mutex | - | ||||||||||||
372 | typedef QMap<QString, QLibraryPrivate*> LibraryMap; | - | ||||||||||||
373 | LibraryMap libraryMap; | - | ||||||||||||
374 | }; | - | ||||||||||||
375 | - | |||||||||||||
376 | static QBasicMutex qt_library_mutex; | - | ||||||||||||
377 | static QLibraryStore *qt_library_data = 0; | - | ||||||||||||
378 | static bool qt_library_data_once; | - | ||||||||||||
379 | - | |||||||||||||
380 | QLibraryStore::~QLibraryStore() | - | ||||||||||||
381 | { | - | ||||||||||||
382 | qt_library_data = 0; | - | ||||||||||||
383 | } | - | ||||||||||||
384 | - | |||||||||||||
385 | inline void QLibraryStore::cleanup() | - | ||||||||||||
386 | { | - | ||||||||||||
387 | QLibraryStore *data = qt_library_data; | - | ||||||||||||
388 | if (!data)
| 0 | ||||||||||||
389 | return; never executed: return; | 0 | ||||||||||||
390 | - | |||||||||||||
391 | // find any libraries that are still loaded but have a no one attached to them | - | ||||||||||||
392 | LibraryMap::Iterator it = data->libraryMap.begin(); | - | ||||||||||||
393 | for (; it != data->libraryMap.end(); ++it) {
| 0 | ||||||||||||
394 | QLibraryPrivate *lib = it.value(); | - | ||||||||||||
395 | if (lib->libraryRefCount.load() == 1) {
| 0 | ||||||||||||
396 | if (lib->libraryUnloadCount.load() > 0) {
| 0 | ||||||||||||
397 | Q_ASSERT(lib->pHnd); | - | ||||||||||||
398 | lib->libraryUnloadCount.store(1); | - | ||||||||||||
399 | #ifdef __GLIBC__ | - | ||||||||||||
400 | // glibc has a bug in unloading from global destructors | - | ||||||||||||
401 | // see https://bugzilla.novell.com/show_bug.cgi?id=622977 | - | ||||||||||||
402 | // and http://sourceware.org/bugzilla/show_bug.cgi?id=11941 | - | ||||||||||||
403 | lib->unload(QLibraryPrivate::NoUnloadSys); | - | ||||||||||||
404 | #else | - | ||||||||||||
405 | lib->unload(); | - | ||||||||||||
406 | #endif | - | ||||||||||||
407 | } never executed: end of block | 0 | ||||||||||||
408 | delete lib; | - | ||||||||||||
409 | it.value() = 0; | - | ||||||||||||
410 | } never executed: end of block | 0 | ||||||||||||
411 | } never executed: end of block | 0 | ||||||||||||
412 | - | |||||||||||||
413 | if (qt_debug_component()) {
| 0 | ||||||||||||
414 | // dump all objects that remain | - | ||||||||||||
415 | foreachfor (QLibraryPrivate *lib ,: qAsConst(data->libraryMap))) { | - | ||||||||||||
416 | if (lib)
| 0 | ||||||||||||
417 | qDebug() << "On QtCore unload," << lib->fileName << "was leaked, with" never executed: QMessageLogger(__FILE__, 417, __PRETTY_FUNCTION__).debug() << "On QtCore unload," << lib->fileName << "was leaked, with" << lib->libraryRefCount.load() << "users"; | 0 | ||||||||||||
418 | << lib->libraryRefCount.load() << "users"; never executed: QMessageLogger(__FILE__, 417, __PRETTY_FUNCTION__).debug() << "On QtCore unload," << lib->fileName << "was leaked, with" << lib->libraryRefCount.load() << "users"; | 0 | ||||||||||||
419 | } never executed: end of block | 0 | ||||||||||||
420 | } never executed: end of block | 0 | ||||||||||||
421 | - | |||||||||||||
422 | delete data; | - | ||||||||||||
423 | } never executed: end of block | 0 | ||||||||||||
424 | - | |||||||||||||
425 | static void qlibraryCleanup() | - | ||||||||||||
426 | { | - | ||||||||||||
427 | QLibraryStore::cleanup(); | - | ||||||||||||
428 | } | - | ||||||||||||
429 | Q_DESTRUCTOR_FUNCTION(qlibraryCleanup) | - | ||||||||||||
430 | - | |||||||||||||
431 | // must be called with a locked mutex | - | ||||||||||||
432 | QLibraryStore *QLibraryStore::instance() | - | ||||||||||||
433 | { | - | ||||||||||||
434 | if (Q_UNLIKELY(!qt_library_data_once && !qt_library_data)) { | - | ||||||||||||
435 | // only create once per process lifetime | - | ||||||||||||
436 | qt_library_data = new QLibraryStore; | - | ||||||||||||
437 | qt_library_data_once = true; | - | ||||||||||||
438 | } | - | ||||||||||||
439 | return qt_library_data; | - | ||||||||||||
440 | } | - | ||||||||||||
441 | - | |||||||||||||
442 | inline QLibraryPrivate *QLibraryStore::findOrCreate(const QString &fileName, const QString &version, | - | ||||||||||||
443 | QLibrary::LoadHints loadHints) | - | ||||||||||||
444 | { | - | ||||||||||||
445 | QMutexLocker locker(&qt_library_mutex); | - | ||||||||||||
446 | QLibraryStore *data = instance(); | - | ||||||||||||
447 | - | |||||||||||||
448 | // check if this library is already loaded | - | ||||||||||||
449 | QLibraryPrivate *lib = 0; | - | ||||||||||||
450 | if (Q_LIKELY(data)) { | - | ||||||||||||
451 | lib = data->libraryMap.value(fileName); | - | ||||||||||||
452 | if (lib) | - | ||||||||||||
453 | lib->mergeLoadHints(loadHints); | - | ||||||||||||
454 | } | - | ||||||||||||
455 | if (!lib) | - | ||||||||||||
456 | lib = new QLibraryPrivate(fileName, version, loadHints); | - | ||||||||||||
457 | - | |||||||||||||
458 | // track this library | - | ||||||||||||
459 | if (Q_LIKELY(data) && !fileName.isEmpty()) | - | ||||||||||||
460 | data->libraryMap.insert(fileName, lib); | - | ||||||||||||
461 | - | |||||||||||||
462 | lib->libraryRefCount.ref(); | - | ||||||||||||
463 | return lib; | - | ||||||||||||
464 | } | - | ||||||||||||
465 | - | |||||||||||||
466 | inline void QLibraryStore::releaseLibrary(QLibraryPrivate *lib) | - | ||||||||||||
467 | { | - | ||||||||||||
468 | QMutexLocker locker(&qt_library_mutex); | - | ||||||||||||
469 | QLibraryStore *data = instance(); | - | ||||||||||||
470 | - | |||||||||||||
471 | if (lib->libraryRefCount.deref()) { | - | ||||||||||||
472 | // still in use | - | ||||||||||||
473 | return; | - | ||||||||||||
474 | } | - | ||||||||||||
475 | - | |||||||||||||
476 | // no one else is using | - | ||||||||||||
477 | Q_ASSERT(lib->libraryUnloadCount.load() == 0); | - | ||||||||||||
478 | - | |||||||||||||
479 | if (Q_LIKELY(data) && !lib->fileName.isEmpty()) { | - | ||||||||||||
480 | QLibraryPrivate *that = data->libraryMap.take(lib->fileName); | - | ||||||||||||
481 | Q_ASSERT(lib == that); | - | ||||||||||||
482 | Q_UNUSED(that); | - | ||||||||||||
483 | } | - | ||||||||||||
484 | delete lib; | - | ||||||||||||
485 | } | - | ||||||||||||
486 | - | |||||||||||||
487 | QLibraryPrivate::QLibraryPrivate(const QString &canonicalFileName, const QString &version, QLibrary::LoadHints loadHints) | - | ||||||||||||
488 | : pHnd(0), fileName(canonicalFileName), fullVersion(version), instance(0), | - | ||||||||||||
489 | libraryRefCount(0), libraryUnloadCount(0), pluginState(MightBeAPlugin) | - | ||||||||||||
490 | { | - | ||||||||||||
491 | loadHintsInt.store(loadHints); | - | ||||||||||||
492 | if (canonicalFileName.isEmpty()) | - | ||||||||||||
493 | errorString = QLibrary::tr("The shared library was not found."); | - | ||||||||||||
494 | } | - | ||||||||||||
495 | - | |||||||||||||
496 | QLibraryPrivate *QLibraryPrivate::findOrCreate(const QString &fileName, const QString &version, | - | ||||||||||||
497 | QLibrary::LoadHints loadHints) | - | ||||||||||||
498 | { | - | ||||||||||||
499 | return QLibraryStore::findOrCreate(fileName, version, loadHints); | - | ||||||||||||
500 | } | - | ||||||||||||
501 | - | |||||||||||||
502 | QLibraryPrivate::~QLibraryPrivate() | - | ||||||||||||
503 | { | - | ||||||||||||
504 | } | - | ||||||||||||
505 | - | |||||||||||||
506 | void QLibraryPrivate::mergeLoadHints(QLibrary::LoadHints lh) | - | ||||||||||||
507 | { | - | ||||||||||||
508 | // if the library is already loaded, we can't change the load hints | - | ||||||||||||
509 | if (pHnd) | - | ||||||||||||
510 | return; | - | ||||||||||||
511 | - | |||||||||||||
512 | loadHintsInt.store(lh); | - | ||||||||||||
513 | } | - | ||||||||||||
514 | - | |||||||||||||
515 | QFunctionPointer QLibraryPrivate::resolve(const char *symbol) | - | ||||||||||||
516 | { | - | ||||||||||||
517 | if (!pHnd) | - | ||||||||||||
518 | return 0; | - | ||||||||||||
519 | return resolve_sys(symbol); | - | ||||||||||||
520 | } | - | ||||||||||||
521 | - | |||||||||||||
522 | void QLibraryPrivate::setLoadHints(QLibrary::LoadHints lh) | - | ||||||||||||
523 | { | - | ||||||||||||
524 | // this locks a global mutex | - | ||||||||||||
525 | QMutexLocker lock(&qt_library_mutex); | - | ||||||||||||
526 | mergeLoadHints(lh); | - | ||||||||||||
527 | } | - | ||||||||||||
528 | - | |||||||||||||
529 | bool QLibraryPrivate::load() | - | ||||||||||||
530 | { | - | ||||||||||||
531 | if (pHnd) {
| 2-530 | ||||||||||||
532 | libraryUnloadCount.ref(); | - | ||||||||||||
533 | return true; executed 2 times by 2 tests: return true; Executed by:
| 2 | ||||||||||||
534 | } | - | ||||||||||||
535 | if (fileName.isEmpty())
| 0-530 | ||||||||||||
536 | return false; never executed: return false; | 0 | ||||||||||||
537 | - | |||||||||||||
538 | bool ret = load_sys(); | - | ||||||||||||
539 | if (qt_debug_component()) {
| 0-530 | ||||||||||||
540 | if (ret) {
| 0 | ||||||||||||
541 | qDebug() << "loaded library" << fileName; | - | ||||||||||||
542 | } else { never executed: end of block | 0 | ||||||||||||
543 | qDebug() << qUtf8Printable(errorString); | - | ||||||||||||
544 | } never executed: end of block | 0 | ||||||||||||
545 | } | - | ||||||||||||
546 | if (ret) {
| 6-524 | ||||||||||||
547 | //when loading a library we add a reference to it so that the QLibraryPrivate won't get deleted | - | ||||||||||||
548 | //this allows to unload the library at a later time | - | ||||||||||||
549 | libraryUnloadCount.ref(); | - | ||||||||||||
550 | libraryRefCount.ref(); | - | ||||||||||||
551 | installCoverageTool(this); | - | ||||||||||||
552 | } executed 524 times by 134 tests: end of block Executed by:
| 524 | ||||||||||||
553 | - | |||||||||||||
554 | return ret; executed 530 times by 134 tests: return ret; Executed by:
| 530 | ||||||||||||
555 | } | - | ||||||||||||
556 | - | |||||||||||||
557 | bool QLibraryPrivate::unload(UnloadFlag flag) | - | ||||||||||||
558 | { | - | ||||||||||||
559 | if (!pHnd) | - | ||||||||||||
560 | return false; | - | ||||||||||||
561 | if (libraryUnloadCount.load() > 0 && !libraryUnloadCount.deref()) { // only unload if ALL QLibrary instance wanted to | - | ||||||||||||
562 | delete inst.data(); | - | ||||||||||||
563 | if (flag == NoUnloadSys || unload_sys()) { | - | ||||||||||||
564 | if (qt_debug_component()) | - | ||||||||||||
565 | qWarning() << "QLibraryPrivate::unload succeeded on" << fileName | - | ||||||||||||
566 | << (flag == NoUnloadSys ? "(faked)" : ""); | - | ||||||||||||
567 | //when the library is unloaded, we release the reference on it so that 'this' | - | ||||||||||||
568 | //can get deleted | - | ||||||||||||
569 | libraryRefCount.deref(); | - | ||||||||||||
570 | pHnd = 0; | - | ||||||||||||
571 | instance = 0; | - | ||||||||||||
572 | } | - | ||||||||||||
573 | } | - | ||||||||||||
574 | - | |||||||||||||
575 | return (pHnd == 0); | - | ||||||||||||
576 | } | - | ||||||||||||
577 | - | |||||||||||||
578 | void QLibraryPrivate::release() | - | ||||||||||||
579 | { | - | ||||||||||||
580 | QLibraryStore::releaseLibrary(this); | - | ||||||||||||
581 | } | - | ||||||||||||
582 | - | |||||||||||||
583 | bool QLibraryPrivate::loadPlugin() | - | ||||||||||||
584 | { | - | ||||||||||||
585 | if (instance) { | - | ||||||||||||
586 | libraryUnloadCount.ref(); | - | ||||||||||||
587 | return true; | - | ||||||||||||
588 | } | - | ||||||||||||
589 | if (pluginState == IsNotAPlugin) | - | ||||||||||||
590 | return false; | - | ||||||||||||
591 | if (load()) { | - | ||||||||||||
592 | instance = (QtPluginInstanceFunction)resolve("qt_plugin_instance"); | - | ||||||||||||
593 | return instance; | - | ||||||||||||
594 | } | - | ||||||||||||
595 | if (qt_debug_component()) | - | ||||||||||||
596 | qWarning() << "QLibraryPrivate::loadPlugin failed on" << fileName << ":" << errorString; | - | ||||||||||||
597 | pluginState = IsNotAPlugin; | - | ||||||||||||
598 | return false; | - | ||||||||||||
599 | } | - | ||||||||||||
600 | - | |||||||||||||
601 | /*! | - | ||||||||||||
602 | Returns \c true if \a fileName has a valid suffix for a loadable | - | ||||||||||||
603 | library; otherwise returns \c false. | - | ||||||||||||
604 | - | |||||||||||||
605 | \table | - | ||||||||||||
606 | \header \li Platform \li Valid suffixes | - | ||||||||||||
607 | \row \li Windows \li \c .dll, \c .DLL | - | ||||||||||||
608 | \row \li Unix/Linux \li \c .so | - | ||||||||||||
609 | \row \li AIX \li \c .a | - | ||||||||||||
610 | \row \li HP-UX \li \c .sl, \c .so (HP-UXi) | - | ||||||||||||
611 | \row \li \macos and iOS \li \c .dylib, \c .bundle, \c .so | - | ||||||||||||
612 | \endtable | - | ||||||||||||
613 | - | |||||||||||||
614 | Trailing versioning numbers on Unix are ignored. | - | ||||||||||||
615 | */ | - | ||||||||||||
616 | bool QLibrary::isLibrary(const QString &fileName) | - | ||||||||||||
617 | { | - | ||||||||||||
618 | #if defined(Q_OS_WIN) | - | ||||||||||||
619 | return fileName.endsWith(QLatin1String(".dll"), Qt::CaseInsensitive); | - | ||||||||||||
620 | #else | - | ||||||||||||
621 | QString completeSuffix = QFileInfo(fileName).completeSuffix(); | - | ||||||||||||
622 | if (completeSuffix.isEmpty()) | - | ||||||||||||
623 | return false; | - | ||||||||||||
624 | QStringList suffixes = completeSuffix.split(QLatin1Char('.')); | - | ||||||||||||
625 | # if defined(Q_OS_DARWIN) | - | ||||||||||||
626 | - | |||||||||||||
627 | // On Mac, libs look like libmylib.1.0.0.dylib | - | ||||||||||||
628 | const QString lastSuffix = suffixes.at(suffixes.count() - 1); | - | ||||||||||||
629 | const QString firstSuffix = suffixes.at(0); | - | ||||||||||||
630 | - | |||||||||||||
631 | bool valid = (lastSuffix == QLatin1String("dylib") | - | ||||||||||||
632 | || firstSuffix == QLatin1String("so") | - | ||||||||||||
633 | || firstSuffix == QLatin1String("bundle")); | - | ||||||||||||
634 | - | |||||||||||||
635 | return valid; | - | ||||||||||||
636 | # else // Generic Unix | - | ||||||||||||
637 | QStringList validSuffixList; | - | ||||||||||||
638 | - | |||||||||||||
639 | # if defined(Q_OS_HPUX) | - | ||||||||||||
640 | /* | - | ||||||||||||
641 | See "HP-UX Linker and Libraries User's Guide", section "Link-time Differences between PA-RISC and IPF": | - | ||||||||||||
642 | "In PA-RISC (PA-32 and PA-64) shared libraries are suffixed with .sl. In IPF (32-bit and 64-bit), | - | ||||||||||||
643 | the shared libraries are suffixed with .so. For compatibility, the IPF linker also supports the .sl suffix." | - | ||||||||||||
644 | */ | - | ||||||||||||
645 | validSuffixList << QLatin1String("sl"); | - | ||||||||||||
646 | # if defined __ia64 | - | ||||||||||||
647 | validSuffixList << QLatin1String("so"); | - | ||||||||||||
648 | # endif | - | ||||||||||||
649 | # elif defined(Q_OS_AIX) | - | ||||||||||||
650 | validSuffixList << QLatin1String("a") << QLatin1String("so"); | - | ||||||||||||
651 | # elif defined(Q_OS_UNIX) | - | ||||||||||||
652 | validSuffixList << QLatin1String("so"); | - | ||||||||||||
653 | # endif | - | ||||||||||||
654 | - | |||||||||||||
655 | // Examples of valid library names: | - | ||||||||||||
656 | // libfoo.so | - | ||||||||||||
657 | // libfoo.so.0 | - | ||||||||||||
658 | // libfoo.so.0.3 | - | ||||||||||||
659 | // libfoo-0.3.so | - | ||||||||||||
660 | // libfoo-0.3.so.0.3.0 | - | ||||||||||||
661 | - | |||||||||||||
662 | int suffix; | - | ||||||||||||
663 | int suffixPos = -1; | - | ||||||||||||
664 | for (suffix = 0; suffix < validSuffixList.count() && suffixPos == -1; ++suffix) | - | ||||||||||||
665 | suffixPos = suffixes.indexOf(validSuffixList.at(suffix)); | - | ||||||||||||
666 | - | |||||||||||||
667 | bool valid = suffixPos != -1; | - | ||||||||||||
668 | for (int i = suffixPos + 1; i < suffixes.count() && valid; ++i) | - | ||||||||||||
669 | if (i != suffixPos) | - | ||||||||||||
670 | suffixes.at(i).toInt(&valid); | - | ||||||||||||
671 | return valid; | - | ||||||||||||
672 | # endif | - | ||||||||||||
673 | #endif | - | ||||||||||||
674 | - | |||||||||||||
675 | } | - | ||||||||||||
676 | - | |||||||||||||
677 | typedef const char * (*QtPluginQueryVerificationDataFunction)(); | - | ||||||||||||
678 | - | |||||||||||||
679 | static bool qt_get_metadata(QtPluginQueryVerificationDataFunction pfn, QLibraryPrivate *priv) | - | ||||||||||||
680 | { | - | ||||||||||||
681 | const char *szData = 0; | - | ||||||||||||
682 | if (!pfn)
| 2 | ||||||||||||
683 | return false; executed 2 times by 1 test: return false; Executed by:
| 2 | ||||||||||||
684 | - | |||||||||||||
685 | szData = pfn(); | - | ||||||||||||
686 | if (!szData)
| 0-2 | ||||||||||||
687 | return false; never executed: return false; | 0 | ||||||||||||
688 | - | |||||||||||||
689 | QJsonDocument doc = QLibraryPrivate::fromRawMetaDataqJsonFromRawLibraryMetaData(szData); | - | ||||||||||||
690 | if (doc.isNull())
| 0-2 | ||||||||||||
691 | return false; never executed: return false; | 0 | ||||||||||||
692 | priv->metaData = doc.object(); | - | ||||||||||||
693 | return true; executed 2 times by 1 test: return true; Executed by:
| 2 | ||||||||||||
694 | } | - | ||||||||||||
695 | - | |||||||||||||
696 | bool QLibraryPrivate::isPlugin() | - | ||||||||||||
697 | { | - | ||||||||||||
698 | if (pluginState == MightBeAPlugin) | - | ||||||||||||
699 | updatePluginState(); | - | ||||||||||||
700 | - | |||||||||||||
701 | return pluginState == IsAPlugin; | - | ||||||||||||
702 | } | - | ||||||||||||
703 | - | |||||||||||||
704 | void QLibraryPrivate::updatePluginState() | - | ||||||||||||
705 | { | - | ||||||||||||
706 | errorString.clear(); | - | ||||||||||||
707 | if (pluginState != MightBeAPlugin) | - | ||||||||||||
708 | return; | - | ||||||||||||
709 | - | |||||||||||||
710 | bool success = false; | - | ||||||||||||
711 | - | |||||||||||||
712 | #if defined(Q_OS_UNIX) && !defined(Q_OS_MAC) | - | ||||||||||||
713 | if (fileName.endsWith(QLatin1String(".debug"))) { | - | ||||||||||||
714 | // refuse to load a file that ends in .debug | - | ||||||||||||
715 | // these are the debug symbols from the libraries | - | ||||||||||||
716 | // the problem is that they are valid shared library files | - | ||||||||||||
717 | // and dlopen is known to crash while opening them | - | ||||||||||||
718 | - | |||||||||||||
719 | // pretend we didn't see the file | - | ||||||||||||
720 | errorString = QLibrary::tr("The shared library was not found."); | - | ||||||||||||
721 | pluginState = IsNotAPlugin; | - | ||||||||||||
722 | return; | - | ||||||||||||
723 | } | - | ||||||||||||
724 | #endif | - | ||||||||||||
725 | - | |||||||||||||
726 | if (!pHnd) { | - | ||||||||||||
727 | // scan for the plugin metadata without loading | - | ||||||||||||
728 | success = findPatternUnloaded(fileName, this); | - | ||||||||||||
729 | } else { | - | ||||||||||||
730 | // library is already loaded (probably via QLibrary) | - | ||||||||||||
731 | // simply get the target function and call it. | - | ||||||||||||
732 | QtPluginQueryVerificationDataFunction getMetaData = NULL; | - | ||||||||||||
733 | getMetaData = (QtPluginQueryVerificationDataFunction) resolve("qt_plugin_query_metadata"); | - | ||||||||||||
734 | success = qt_get_metadata(getMetaData, this); | - | ||||||||||||
735 | } | - | ||||||||||||
736 | - | |||||||||||||
737 | if (!success) { | - | ||||||||||||
738 | if (errorString.isEmpty()){ | - | ||||||||||||
739 | if (fileName.isEmpty()) | - | ||||||||||||
740 | errorString = QLibrary::tr("The shared library was not found."); | - | ||||||||||||
741 | else | - | ||||||||||||
742 | errorString = QLibrary::tr("The file '%1' is not a valid Qt plugin.").arg(fileName); | - | ||||||||||||
743 | } | - | ||||||||||||
744 | pluginState = IsNotAPlugin; | - | ||||||||||||
745 | return; | - | ||||||||||||
746 | } | - | ||||||||||||
747 | - | |||||||||||||
748 | pluginState = IsNotAPlugin; // be pessimistic | - | ||||||||||||
749 | - | |||||||||||||
750 | uint qt_version = (uint)metaData.value(QLatin1String("version")).toDouble(); | - | ||||||||||||
751 | bool debug = metaData.value(QLatin1String("debug")).toBool(); | - | ||||||||||||
752 | if ((qt_version & 0x00ff00) > (QT_VERSION & 0x00ff00) || (qt_version & 0xff0000) != (QT_VERSION & 0xff0000)) { | - | ||||||||||||
753 | if (qt_debug_component()) { | - | ||||||||||||
754 | qWarning("In %s:\n" | - | ||||||||||||
755 | " Plugin uses incompatible Qt library (%d.%d.%d) [%s]", | - | ||||||||||||
756 | (const char*) QFile::encodeName(fileName), | - | ||||||||||||
757 | (qt_version&0xff0000) >> 16, (qt_version&0xff00) >> 8, qt_version&0xff, | - | ||||||||||||
758 | debug ? "debug" : "release"); | - | ||||||||||||
759 | } | - | ||||||||||||
760 | errorString = QLibrary::tr("The plugin '%1' uses incompatible Qt library. (%2.%3.%4) [%5]") | - | ||||||||||||
761 | .arg(fileName) | - | ||||||||||||
762 | .arg((qt_version&0xff0000) >> 16) | - | ||||||||||||
763 | .arg((qt_version&0xff00) >> 8) | - | ||||||||||||
764 | .arg(qt_version&0xff) | - | ||||||||||||
765 | .arg(debug ? QLatin1String("debug") : QLatin1String("release")); | - | ||||||||||||
766 | #ifndef QT_NO_DEBUG_PLUGIN_CHECK | - | ||||||||||||
767 | } else if(debug != QLIBRARY_AS_DEBUG) { | - | ||||||||||||
768 | //don't issue a qWarning since we will hopefully find a non-debug? --Sam | - | ||||||||||||
769 | errorString = QLibrary::tr("The plugin '%1' uses incompatible Qt library." | - | ||||||||||||
770 | " (Cannot mix debug and release libraries.)").arg(fileName); | - | ||||||||||||
771 | #endif | - | ||||||||||||
772 | } else { | - | ||||||||||||
773 | pluginState = IsAPlugin; | - | ||||||||||||
774 | } | - | ||||||||||||
775 | } | - | ||||||||||||
776 | - | |||||||||||||
777 | /*! | - | ||||||||||||
778 | Loads the library and returns \c true if the library was loaded | - | ||||||||||||
779 | successfully; otherwise returns \c false. Since resolve() always | - | ||||||||||||
780 | calls this function before resolving any symbols it is not | - | ||||||||||||
781 | necessary to call it explicitly. In some situations you might want | - | ||||||||||||
782 | the library loaded in advance, in which case you would use this | - | ||||||||||||
783 | function. | - | ||||||||||||
784 | - | |||||||||||||
785 | \sa unload() | - | ||||||||||||
786 | */ | - | ||||||||||||
787 | bool QLibrary::load() | - | ||||||||||||
788 | { | - | ||||||||||||
789 | if (!d) | - | ||||||||||||
790 | return false; | - | ||||||||||||
791 | if (did_load) | - | ||||||||||||
792 | return d->pHnd; | - | ||||||||||||
793 | did_load = true; | - | ||||||||||||
794 | return d->load(); | - | ||||||||||||
795 | } | - | ||||||||||||
796 | - | |||||||||||||
797 | /*! | - | ||||||||||||
798 | Unloads the library and returns \c true if the library could be | - | ||||||||||||
799 | unloaded; otherwise returns \c false. | - | ||||||||||||
800 | - | |||||||||||||
801 | This happens automatically on application termination, so you | - | ||||||||||||
802 | shouldn't normally need to call this function. | - | ||||||||||||
803 | - | |||||||||||||
804 | If other instances of QLibrary are using the same library, the | - | ||||||||||||
805 | call will fail, and unloading will only happen when every instance | - | ||||||||||||
806 | has called unload(). | - | ||||||||||||
807 | - | |||||||||||||
808 | Note that on Mac OS X 10.3 (Panther), dynamic libraries cannot be unloaded. | - | ||||||||||||
809 | - | |||||||||||||
810 | \sa resolve(), load() | - | ||||||||||||
811 | */ | - | ||||||||||||
812 | bool QLibrary::unload() | - | ||||||||||||
813 | { | - | ||||||||||||
814 | if (did_load) { | - | ||||||||||||
815 | did_load = false; | - | ||||||||||||
816 | return d->unload(); | - | ||||||||||||
817 | } | - | ||||||||||||
818 | return false; | - | ||||||||||||
819 | } | - | ||||||||||||
820 | - | |||||||||||||
821 | /*! | - | ||||||||||||
822 | Returns \c true if the library is loaded; otherwise returns \c false. | - | ||||||||||||
823 | - | |||||||||||||
824 | \sa load() | - | ||||||||||||
825 | */ | - | ||||||||||||
826 | bool QLibrary::isLoaded() const | - | ||||||||||||
827 | { | - | ||||||||||||
828 | return d && d->pHnd; | - | ||||||||||||
829 | } | - | ||||||||||||
830 | - | |||||||||||||
831 | - | |||||||||||||
832 | /*! | - | ||||||||||||
833 | Constructs a library with the given \a parent. | - | ||||||||||||
834 | */ | - | ||||||||||||
835 | QLibrary::QLibrary(QObject *parent) | - | ||||||||||||
836 | :QObject(parent), d(0), did_load(false) | - | ||||||||||||
837 | { | - | ||||||||||||
838 | } | - | ||||||||||||
839 | - | |||||||||||||
840 | - | |||||||||||||
841 | /*! | - | ||||||||||||
842 | Constructs a library object with the given \a parent that will | - | ||||||||||||
843 | load the library specified by \a fileName. | - | ||||||||||||
844 | - | |||||||||||||
845 | We recommend omitting the file's suffix in \a fileName, since | - | ||||||||||||
846 | QLibrary will automatically look for the file with the appropriate | - | ||||||||||||
847 | suffix in accordance with the platform, e.g. ".so" on Unix, | - | ||||||||||||
848 | ".dylib" on \macos and iOS, and ".dll" on Windows. (See \l{fileName}.) | - | ||||||||||||
849 | */ | - | ||||||||||||
850 | QLibrary::QLibrary(const QString& fileName, QObject *parent) | - | ||||||||||||
851 | :QObject(parent), d(0), did_load(false) | - | ||||||||||||
852 | { | - | ||||||||||||
853 | setFileName(fileName); | - | ||||||||||||
854 | } | - | ||||||||||||
855 | - | |||||||||||||
856 | - | |||||||||||||
857 | /*! | - | ||||||||||||
858 | Constructs a library object with the given \a parent that will | - | ||||||||||||
859 | load the library specified by \a fileName and major version number \a verNum. | - | ||||||||||||
860 | Currently, the version number is ignored on Windows. | - | ||||||||||||
861 | - | |||||||||||||
862 | We recommend omitting the file's suffix in \a fileName, since | - | ||||||||||||
863 | QLibrary will automatically look for the file with the appropriate | - | ||||||||||||
864 | suffix in accordance with the platform, e.g. ".so" on Unix, | - | ||||||||||||
865 | ".dylib" on \macos and iOS, and ".dll" on Windows. (See \l{fileName}.) | - | ||||||||||||
866 | */ | - | ||||||||||||
867 | QLibrary::QLibrary(const QString& fileName, int verNum, QObject *parent) | - | ||||||||||||
868 | :QObject(parent), d(0), did_load(false) | - | ||||||||||||
869 | { | - | ||||||||||||
870 | setFileNameAndVersion(fileName, verNum); | - | ||||||||||||
871 | } | - | ||||||||||||
872 | - | |||||||||||||
873 | /*! | - | ||||||||||||
874 | Constructs a library object with the given \a parent that will | - | ||||||||||||
875 | load the library specified by \a fileName and full version number \a version. | - | ||||||||||||
876 | Currently, the version number is ignored on Windows. | - | ||||||||||||
877 | - | |||||||||||||
878 | We recommend omitting the file's suffix in \a fileName, since | - | ||||||||||||
879 | QLibrary will automatically look for the file with the appropriate | - | ||||||||||||
880 | suffix in accordance with the platform, e.g. ".so" on Unix, | - | ||||||||||||
881 | ".dylib" on \macos and iOS, and ".dll" on Windows. (See \l{fileName}.) | - | ||||||||||||
882 | */ | - | ||||||||||||
883 | QLibrary::QLibrary(const QString& fileName, const QString &version, QObject *parent) | - | ||||||||||||
884 | :QObject(parent), d(0), did_load(false) | - | ||||||||||||
885 | { | - | ||||||||||||
886 | setFileNameAndVersion(fileName, version); | - | ||||||||||||
887 | } | - | ||||||||||||
888 | - | |||||||||||||
889 | /*! | - | ||||||||||||
890 | Destroys the QLibrary object. | - | ||||||||||||
891 | - | |||||||||||||
892 | Unless unload() was called explicitly, the library stays in memory | - | ||||||||||||
893 | until the application terminates. | - | ||||||||||||
894 | - | |||||||||||||
895 | \sa isLoaded(), unload() | - | ||||||||||||
896 | */ | - | ||||||||||||
897 | QLibrary::~QLibrary() | - | ||||||||||||
898 | { | - | ||||||||||||
899 | if (d) | - | ||||||||||||
900 | d->release(); | - | ||||||||||||
901 | } | - | ||||||||||||
902 | - | |||||||||||||
903 | - | |||||||||||||
904 | /*! | - | ||||||||||||
905 | \property QLibrary::fileName | - | ||||||||||||
906 | \brief the file name of the library | - | ||||||||||||
907 | - | |||||||||||||
908 | We recommend omitting the file's suffix in the file name, since | - | ||||||||||||
909 | QLibrary will automatically look for the file with the appropriate | - | ||||||||||||
910 | suffix (see isLibrary()). | - | ||||||||||||
911 | - | |||||||||||||
912 | When loading the library, QLibrary searches in all system-specific | - | ||||||||||||
913 | library locations (for example, \c LD_LIBRARY_PATH on Unix), unless the | - | ||||||||||||
914 | file name has an absolute path. After loading the library | - | ||||||||||||
915 | successfully, fileName() returns the fully-qualified file name of | - | ||||||||||||
916 | the library, including the full path to the library if one was given | - | ||||||||||||
917 | in the constructor or passed to setFileName(). | - | ||||||||||||
918 | - | |||||||||||||
919 | For example, after successfully loading the "GL" library on Unix | - | ||||||||||||
920 | platforms, fileName() will return "libGL.so". If the file name was | - | ||||||||||||
921 | originally passed as "/usr/lib/libGL", fileName() will return | - | ||||||||||||
922 | "/usr/lib/libGL.so". | - | ||||||||||||
923 | */ | - | ||||||||||||
924 | - | |||||||||||||
925 | void QLibrary::setFileName(const QString &fileName) | - | ||||||||||||
926 | { | - | ||||||||||||
927 | QLibrary::LoadHints lh; | - | ||||||||||||
928 | if (d) { | - | ||||||||||||
929 | lh = d->loadHints(); | - | ||||||||||||
930 | d->release(); | - | ||||||||||||
931 | d = 0; | - | ||||||||||||
932 | did_load = false; | - | ||||||||||||
933 | } | - | ||||||||||||
934 | d = QLibraryPrivate::findOrCreate(fileName, QString(), lh); | - | ||||||||||||
935 | } | - | ||||||||||||
936 | - | |||||||||||||
937 | QString QLibrary::fileName() const | - | ||||||||||||
938 | { | - | ||||||||||||
939 | if (d) | - | ||||||||||||
940 | return d->qualifiedFileName.isEmpty() ? d->fileName : d->qualifiedFileName; | - | ||||||||||||
941 | return QString(); | - | ||||||||||||
942 | } | - | ||||||||||||
943 | - | |||||||||||||
944 | /*! | - | ||||||||||||
945 | \fn void QLibrary::setFileNameAndVersion(const QString &fileName, int versionNumber) | - | ||||||||||||
946 | - | |||||||||||||
947 | Sets the fileName property and major version number to \a fileName | - | ||||||||||||
948 | and \a versionNumber respectively. | - | ||||||||||||
949 | The \a versionNumber is ignored on Windows. | - | ||||||||||||
950 | - | |||||||||||||
951 | \sa setFileName() | - | ||||||||||||
952 | */ | - | ||||||||||||
953 | void QLibrary::setFileNameAndVersion(const QString &fileName, int verNum) | - | ||||||||||||
954 | { | - | ||||||||||||
955 | QLibrary::LoadHints lh; | - | ||||||||||||
956 | if (d) { | - | ||||||||||||
957 | lh = d->loadHints(); | - | ||||||||||||
958 | d->release(); | - | ||||||||||||
959 | d = 0; | - | ||||||||||||
960 | did_load = false; | - | ||||||||||||
961 | } | - | ||||||||||||
962 | d = QLibraryPrivate::findOrCreate(fileName, verNum >= 0 ? QString::number(verNum) : QString(), lh); | - | ||||||||||||
963 | } | - | ||||||||||||
964 | - | |||||||||||||
965 | /*! | - | ||||||||||||
966 | \since 4.4 | - | ||||||||||||
967 | - | |||||||||||||
968 | Sets the fileName property and full version number to \a fileName | - | ||||||||||||
969 | and \a version respectively. | - | ||||||||||||
970 | The \a version parameter is ignored on Windows. | - | ||||||||||||
971 | - | |||||||||||||
972 | \sa setFileName() | - | ||||||||||||
973 | */ | - | ||||||||||||
974 | void QLibrary::setFileNameAndVersion(const QString &fileName, const QString &version) | - | ||||||||||||
975 | { | - | ||||||||||||
976 | QLibrary::LoadHints lh; | - | ||||||||||||
977 | if (d) { | - | ||||||||||||
978 | lh = d->loadHints(); | - | ||||||||||||
979 | d->release(); | - | ||||||||||||
980 | d = 0; | - | ||||||||||||
981 | did_load = false; | - | ||||||||||||
982 | } | - | ||||||||||||
983 | d = QLibraryPrivate::findOrCreate(fileName, version, lh); | - | ||||||||||||
984 | } | - | ||||||||||||
985 | - | |||||||||||||
986 | /*! | - | ||||||||||||
987 | Returns the address of the exported symbol \a symbol. The library is | - | ||||||||||||
988 | loaded if necessary. The function returns 0 if the symbol could | - | ||||||||||||
989 | not be resolved or if the library could not be loaded. | - | ||||||||||||
990 | - | |||||||||||||
991 | Example: | - | ||||||||||||
992 | \snippet code/src_corelib_plugin_qlibrary.cpp 2 | - | ||||||||||||
993 | - | |||||||||||||
994 | The symbol must be exported as a C function from the library. This | - | ||||||||||||
995 | means that the function must be wrapped in an \c{extern "C"} if | - | ||||||||||||
996 | the library is compiled with a C++ compiler. On Windows you must | - | ||||||||||||
997 | also explicitly export the function from the DLL using the | - | ||||||||||||
998 | \c{__declspec(dllexport)} compiler directive, for example: | - | ||||||||||||
999 | - | |||||||||||||
1000 | \snippet code/src_corelib_plugin_qlibrary.cpp 3 | - | ||||||||||||
1001 | - | |||||||||||||
1002 | with \c MY_EXPORT defined as | - | ||||||||||||
1003 | - | |||||||||||||
1004 | \snippet code/src_corelib_plugin_qlibrary.cpp 4 | - | ||||||||||||
1005 | */ | - | ||||||||||||
1006 | QFunctionPointer QLibrary::resolve(const char *symbol) | - | ||||||||||||
1007 | { | - | ||||||||||||
1008 | if (!isLoaded() && !load()) | - | ||||||||||||
1009 | return 0; | - | ||||||||||||
1010 | return d->resolve(symbol); | - | ||||||||||||
1011 | } | - | ||||||||||||
1012 | - | |||||||||||||
1013 | /*! | - | ||||||||||||
1014 | \overload | - | ||||||||||||
1015 | - | |||||||||||||
1016 | Loads the library \a fileName and returns the address of the | - | ||||||||||||
1017 | exported symbol \a symbol. Note that \a fileName should not | - | ||||||||||||
1018 | include the platform-specific file suffix; (see \l{fileName}). The | - | ||||||||||||
1019 | library remains loaded until the application exits. | - | ||||||||||||
1020 | - | |||||||||||||
1021 | The function returns 0 if the symbol could not be resolved or if | - | ||||||||||||
1022 | the library could not be loaded. | - | ||||||||||||
1023 | - | |||||||||||||
1024 | \sa resolve() | - | ||||||||||||
1025 | */ | - | ||||||||||||
1026 | QFunctionPointer QLibrary::resolve(const QString &fileName, const char *symbol) | - | ||||||||||||
1027 | { | - | ||||||||||||
1028 | QLibrary library(fileName); | - | ||||||||||||
1029 | return library.resolve(symbol); | - | ||||||||||||
1030 | } | - | ||||||||||||
1031 | - | |||||||||||||
1032 | /*! | - | ||||||||||||
1033 | \overload | - | ||||||||||||
1034 | - | |||||||||||||
1035 | Loads the library \a fileName with major version number \a verNum and | - | ||||||||||||
1036 | returns the address of the exported symbol \a symbol. | - | ||||||||||||
1037 | Note that \a fileName should not include the platform-specific file suffix; | - | ||||||||||||
1038 | (see \l{fileName}). The library remains loaded until the application exits. | - | ||||||||||||
1039 | \a verNum is ignored on Windows. | - | ||||||||||||
1040 | - | |||||||||||||
1041 | The function returns 0 if the symbol could not be resolved or if | - | ||||||||||||
1042 | the library could not be loaded. | - | ||||||||||||
1043 | - | |||||||||||||
1044 | \sa resolve() | - | ||||||||||||
1045 | */ | - | ||||||||||||
1046 | QFunctionPointer QLibrary::resolve(const QString &fileName, int verNum, const char *symbol) | - | ||||||||||||
1047 | { | - | ||||||||||||
1048 | QLibrary library(fileName, verNum); | - | ||||||||||||
1049 | return library.resolve(symbol); | - | ||||||||||||
1050 | } | - | ||||||||||||
1051 | - | |||||||||||||
1052 | /*! | - | ||||||||||||
1053 | \overload | - | ||||||||||||
1054 | \since 4.4 | - | ||||||||||||
1055 | - | |||||||||||||
1056 | Loads the library \a fileName with full version number \a version and | - | ||||||||||||
1057 | returns the address of the exported symbol \a symbol. | - | ||||||||||||
1058 | Note that \a fileName should not include the platform-specific file suffix; | - | ||||||||||||
1059 | (see \l{fileName}). The library remains loaded until the application exits. | - | ||||||||||||
1060 | \a version is ignored on Windows. | - | ||||||||||||
1061 | - | |||||||||||||
1062 | The function returns 0 if the symbol could not be resolved or if | - | ||||||||||||
1063 | the library could not be loaded. | - | ||||||||||||
1064 | - | |||||||||||||
1065 | \sa resolve() | - | ||||||||||||
1066 | */ | - | ||||||||||||
1067 | QFunctionPointer QLibrary::resolve(const QString &fileName, const QString &version, const char *symbol) | - | ||||||||||||
1068 | { | - | ||||||||||||
1069 | QLibrary library(fileName, version); | - | ||||||||||||
1070 | return library.resolve(symbol); | - | ||||||||||||
1071 | } | - | ||||||||||||
1072 | - | |||||||||||||
1073 | /*! | - | ||||||||||||
1074 | \since 4.2 | - | ||||||||||||
1075 | - | |||||||||||||
1076 | Returns a text string with the description of the last error that occurred. | - | ||||||||||||
1077 | Currently, errorString will only be set if load(), unload() or resolve() for some reason fails. | - | ||||||||||||
1078 | */ | - | ||||||||||||
1079 | QString QLibrary::errorString() const | - | ||||||||||||
1080 | { | - | ||||||||||||
1081 | return (!d || d->errorString.isEmpty()) ? tr("Unknown error") : d->errorString; | - | ||||||||||||
1082 | } | - | ||||||||||||
1083 | - | |||||||||||||
1084 | /*! | - | ||||||||||||
1085 | \property QLibrary::loadHints | - | ||||||||||||
1086 | \brief Give the load() function some hints on how it should behave. | - | ||||||||||||
1087 | - | |||||||||||||
1088 | You can give some hints on how the symbols are resolved. Usually, | - | ||||||||||||
1089 | the symbols are not resolved at load time, but resolved lazily, | - | ||||||||||||
1090 | (that is, when resolve() is called). If you set the loadHints to | - | ||||||||||||
1091 | ResolveAllSymbolsHint, then all symbols will be resolved at load time | - | ||||||||||||
1092 | if the platform supports it. | - | ||||||||||||
1093 | - | |||||||||||||
1094 | Setting ExportExternalSymbolsHint will make the external symbols in the | - | ||||||||||||
1095 | library available for resolution in subsequent loaded libraries. | - | ||||||||||||
1096 | - | |||||||||||||
1097 | If LoadArchiveMemberHint is set, the file name | - | ||||||||||||
1098 | is composed of two components: A path which is a reference to an | - | ||||||||||||
1099 | archive file followed by the second component which is the reference to | - | ||||||||||||
1100 | the archive member. For instance, the fileName \c libGL.a(shr_64.o) will refer | - | ||||||||||||
1101 | to the library \c shr_64.o in the archive file named \c libGL.a. This | - | ||||||||||||
1102 | is only supported on the AIX platform. | - | ||||||||||||
1103 | - | |||||||||||||
1104 | The interpretation of the load hints is platform dependent, and if | - | ||||||||||||
1105 | you use it you are probably making some assumptions on which platform | - | ||||||||||||
1106 | you are compiling for, so use them only if you understand the consequences | - | ||||||||||||
1107 | of them. | - | ||||||||||||
1108 | - | |||||||||||||
1109 | By default, none of these flags are set, so libraries will be loaded with | - | ||||||||||||
1110 | lazy symbol resolution, and will not export external symbols for resolution | - | ||||||||||||
1111 | in other dynamically-loaded libraries. | - | ||||||||||||
1112 | - | |||||||||||||
1113 | \note Setting this property after the library has been loaded has no effect | - | ||||||||||||
1114 | and loadHints() will not reflect those changes. | - | ||||||||||||
1115 | - | |||||||||||||
1116 | \note This property is shared among all QLibrary instances that refer to | - | ||||||||||||
1117 | the same library. | - | ||||||||||||
1118 | */ | - | ||||||||||||
1119 | void QLibrary::setLoadHints(LoadHints hints) | - | ||||||||||||
1120 | { | - | ||||||||||||
1121 | if (!d) { | - | ||||||||||||
1122 | d = QLibraryPrivate::findOrCreate(QString()); // ugly, but we need a d-ptr | - | ||||||||||||
1123 | d->errorString.clear(); | - | ||||||||||||
1124 | } | - | ||||||||||||
1125 | d->setLoadHints(hints); | - | ||||||||||||
1126 | } | - | ||||||||||||
1127 | - | |||||||||||||
1128 | QLibrary::LoadHints QLibrary::loadHints() const | - | ||||||||||||
1129 | { | - | ||||||||||||
1130 | return d ? d->loadHints() : QLibrary::LoadHints(); | - | ||||||||||||
1131 | } | - | ||||||||||||
1132 | - | |||||||||||||
1133 | /* Internal, for debugging */ | - | ||||||||||||
1134 | bool qt_debug_component() | - | ||||||||||||
1135 | { | - | ||||||||||||
1136 | static int debug_env = QT_PREPEND_NAMESPACE(qEnvironmentVariableIntValue)("QT_DEBUG_PLUGINS"); | - | ||||||||||||
1137 | return debug_env != 0; | - | ||||||||||||
1138 | } | - | ||||||||||||
1139 | - | |||||||||||||
1140 | QT_END_NAMESPACE | - | ||||||||||||
1141 | - | |||||||||||||
1142 | #endif // QT_NO_LIBRARY | - | ||||||||||||
Source code | Switch to Preprocessed file |