diff options
Diffstat (limited to 'src/corelib/plugin')
-rw-r--r-- | src/corelib/plugin/plugin.pri | 6 | ||||
-rw-r--r-- | src/corelib/plugin/qlibrary.cpp | 141 | ||||
-rw-r--r-- | src/corelib/plugin/qmachparser.cpp | 218 | ||||
-rw-r--r-- | src/corelib/plugin/qmachparser_p.h | 79 |
4 files changed, 336 insertions, 108 deletions
diff --git a/src/corelib/plugin/plugin.pri b/src/corelib/plugin/plugin.pri index eb7a7f7fa8..338b3d0972 100644 --- a/src/corelib/plugin/plugin.pri +++ b/src/corelib/plugin/plugin.pri @@ -9,14 +9,16 @@ HEADERS += \ plugin/quuid.h \ plugin/qfactoryloader_p.h \ plugin/qsystemlibrary_p.h \ - plugin/qelfparser_p.h + plugin/qelfparser_p.h \ + plugin/qmachparser_p.h SOURCES += \ plugin/qpluginloader.cpp \ plugin/qfactoryloader.cpp \ plugin/quuid.cpp \ plugin/qlibrary.cpp \ - plugin/qelfparser_p.cpp + plugin/qelfparser_p.cpp \ + plugin/qmachparser.cpp win32 { SOURCES += \ diff --git a/src/corelib/plugin/qlibrary.cpp b/src/corelib/plugin/qlibrary.cpp index 3432f9619d..92e60c2216 100644 --- a/src/corelib/plugin/qlibrary.cpp +++ b/src/corelib/plugin/qlibrary.cpp @@ -1,7 +1,7 @@ - /**************************************************************************** ** ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Copyright (C) 2013 Intel Corporation ** Contact: http://www.qt-project.org/legal ** ** This file is part of the QtCore module of the Qt Toolkit. @@ -64,6 +64,7 @@ #include <qjsondocument.h> #include <qjsonvalue.h> #include "qelfparser_p.h" +#include "qmachparser_p.h" QT_BEGIN_NAMESPACE @@ -180,8 +181,6 @@ QT_BEGIN_NAMESPACE */ -#if defined(Q_OS_UNIX) && !defined(Q_OS_MAC) - static long qt_find_pattern(const char *s, ulong s_len, const char *pattern, ulong p_len) { @@ -228,7 +227,7 @@ static long qt_find_pattern(const char *s, ulong s_len, information could not be read. Returns true if version information is present and successfully read. */ -static bool qt_unix_query(const QString &library, QLibraryPrivate *lib) +static bool findPatternUnloaded(const QString &library, QLibraryPrivate *lib) { QFile file(library); if (!file.open(QIODevice::ReadOnly)) { @@ -253,7 +252,7 @@ static bool qt_unix_query(const QString &library, QLibraryPrivate *lib) } /* - ELF binaries on GNU, have .qplugin sections. + ELF and Mach-O binaries with GCC have .qplugin sections. */ bool hasMetaData = false; long pos = 0; @@ -274,6 +273,26 @@ static bool qt_unix_query(const QString &library, QLibraryPrivate *lib) pos += rel; hasMetaData = true; } +#elif defined (Q_OF_MACH_O) + { + QString errorString; + int r = QMachOParser::parse(filedata, fdlen, library, &errorString, &pos, &fdlen); + if (r == QMachOParser::NotSuitable) { + if (qt_debug_component()) + qWarning("QMachOParser: %s", qPrintable(errorString)); + if (lib) + lib->errorString = errorString; + return false; + } + // even if the metadata section was not found, the Mach-O parser will + // at least return the boundaries of the right architecture + long rel = qt_find_pattern(filedata + pos, fdlen, pattern, plen); + if (rel < 0) + pos = -1; + else + pos += rel; + hasMetaData = true; + } #else pos = qt_find_pattern(filedata, fdlen, pattern, plen); if (pos > 0) @@ -300,8 +319,6 @@ static bool qt_unix_query(const QString &library, QLibraryPrivate *lib) return ret; } -#endif // Q_OS_UNIX && !Q_OS_MAC - static void installCoverageTool(QLibraryPrivate *libPrivate) { #ifdef __COVERAGESCANNER__ @@ -618,41 +635,18 @@ bool QLibrary::isLibrary(const QString &fileName) } -#if defined (Q_OS_WIN) && defined(_MSC_VER) && _MSC_VER >= 1400 && !defined(Q_CC_INTEL) -#define QT_USE_MS_STD_EXCEPTION 1 -const char* qt_try_versioninfo(void *pfn, bool *exceptionThrown) -{ - *exceptionThrown = false; - const char *szData = 0; - typedef const char * (*VerificationFunction)(); - VerificationFunction func = reinterpret_cast<VerificationFunction>(pfn); - __try { - if(func) - szData = func(); - } __except(EXCEPTION_EXECUTE_HANDLER) { - *exceptionThrown = true; - } - return szData; -} -#endif - typedef const char * (*QtPluginQueryVerificationDataFunction)(); -bool qt_get_metadata(QtPluginQueryVerificationDataFunction pfn, QLibraryPrivate *priv, bool *exceptionThrown) +static bool qt_get_metadata(QtPluginQueryVerificationDataFunction pfn, QLibraryPrivate *priv) { - *exceptionThrown = false; const char *szData = 0; if (!pfn) return false; -#ifdef QT_USE_MS_STD_EXCEPTION - szData = qt_try_versioninfo((void *)pfn, exceptionThrown); - if (*exceptionThrown) - return false; -#else + szData = pfn(); -#endif if (!szData) return false; + QJsonDocument doc = QLibraryPrivate::fromRawMetaData(szData); if (doc.isNull()) return false; @@ -690,80 +684,15 @@ void QLibraryPrivate::updatePluginState() } #endif -#if defined(Q_OS_UNIX) && !defined(Q_OS_MAC) if (!pHnd) { - // use unix shortcut to avoid loading the library - success = qt_unix_query(fileName, this); - } else -#endif - { - bool retryLoadLibrary = false; // Only used on Windows with MS compiler.(false in other cases) - do { - bool temporary_load = false; -#ifdef Q_OS_WIN - HMODULE hTempModule = 0; -#endif - if (!pHnd) { -#ifdef Q_OS_WIN - DWORD dwFlags = (retryLoadLibrary) ? 0: DONT_RESOLVE_DLL_REFERENCES; - //avoid 'Bad Image' message box - UINT oldmode = SetErrorMode(SEM_FAILCRITICALERRORS|SEM_NOOPENFILEERRORBOX); - hTempModule = ::LoadLibraryEx((wchar_t*)QDir::toNativeSeparators(fileName).utf16(), 0, dwFlags); - SetErrorMode(oldmode); -#else - temporary_load = load(); -#endif - } - QtPluginQueryVerificationDataFunction getMetaData = NULL; - - bool exceptionThrown = false; - bool ret = false; -#ifdef Q_OS_WIN - if (hTempModule) { - getMetaData = (QtPluginQueryVerificationDataFunction) -#ifdef Q_OS_WINCE - ::GetProcAddress(hTempModule, L"qt_plugin_query_metadata") -#else - ::GetProcAddress(hTempModule, "qt_plugin_query_metadata") -#endif - ; - } else -#endif - { - getMetaData = (QtPluginQueryVerificationDataFunction) resolve("qt_plugin_query_metadata"); - } - - if (getMetaData) - ret = qt_get_metadata(getMetaData, this, &exceptionThrown); - - if (temporary_load) - unload(); - if (!exceptionThrown) { - if (ret) { - success = true; - } - retryLoadLibrary = false; - } -#ifdef QT_USE_MS_STD_EXCEPTION - else { - // An exception was thrown when calling qt_plugin_query_verification_data(). - // This usually happens when plugin is compiled with the /clr compiler flag, - // & will only work if the dependencies are loaded & DLLMain() is called. - // LoadLibrary() will do this, try once with this & if it fails don't load. - retryLoadLibrary = !retryLoadLibrary; - } -#endif -#ifdef Q_OS_WIN - if (hTempModule) { - BOOL ok = ::FreeLibrary(hTempModule); - if (ok) { - hTempModule = 0; - } - - } -#endif - } while (retryLoadLibrary); // Will be 'false' in all cases other than when an - // exception is thrown(will happen only when using a MS compiler) + // scan for the plugin metadata without loading + success = findPatternUnloaded(fileName, this); + } else { + // library is already loaded (probably via QLibrary) + // simply get the target function and call it. + QtPluginQueryVerificationDataFunction getMetaData = NULL; + getMetaData = (QtPluginQueryVerificationDataFunction) resolve("qt_plugin_query_metadata"); + success = qt_get_metadata(getMetaData, this); } if (!success) { diff --git a/src/corelib/plugin/qmachparser.cpp b/src/corelib/plugin/qmachparser.cpp new file mode 100644 index 0000000000..5152a92d1d --- /dev/null +++ b/src/corelib/plugin/qmachparser.cpp @@ -0,0 +1,218 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Intel Corporation +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtCore module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qmachparser_p.h" + +#if defined(Q_OF_MACH_O) && !defined(QT_NO_LIBRARY) + +#include <qendian.h> +#include "qlibrary_p.h" + +#include <mach-o/loader.h> +#include <mach-o/fat.h> + +QT_BEGIN_NAMESPACE + +#if defined(Q_PROCESSOR_X86_64) +# define MACHO64 +static const cpu_type_t my_cputype = CPU_TYPE_X86_64; +#elif defined(Q_PROCESSOR_X86_32) +static const cpu_type_t my_cputype = CPU_TYPE_X86; +#elif defined(Q_PROCESSOR_POWER_64) +# define MACHO64 +static const cpu_type_t my_cputype = CPU_TYPE_POWERPC64; +#elif defined(Q_PROCESSOR_POWER_32) +static const cpu_type_t my_cputype = CPU_TYPE_POWERPC; +#elif defined(Q_PROCESSOR_ARM) +static const cpu_type_t my_cputype = CPU_TYPE_ARM; +#else +# error "Unknown CPU type" +#endif + +#ifdef MACHO64 +# undef MACHO64 +typedef mach_header_64 my_mach_header; +typedef segment_command_64 my_segment_command; +typedef section_64 my_section; +static const uint32_t my_magic = MH_MAGIC_64; +#else +typedef mach_header my_mach_header; +typedef segment_command my_segment_command; +typedef section my_section; +static const uint32_t my_magic = MH_MAGIC; +#endif + +static int ns(const QString &reason, const QString &library, QString *errorString) +{ + if (errorString) + *errorString = QLibrary::tr("'%1' is not a valid Mach-O binary (%2)") + .arg(library, reason.isEmpty() ? QLibrary::tr("file is corrupt") : reason); + return QMachOParser::NotSuitable; +} + +int QMachOParser::parse(const char *m_s, ulong fdlen, const QString &library, QString *errorString, long *pos, ulong *sectionlen) +{ + // The minimum size of a Mach-O binary we're interested in. + // It must have a full Mach header, at least one segment and at least one + // section. It's probably useless with just the "qtmetadata" section, but + // it's valid nonetheless. + // A fat binary must have this plus the fat header, of course. + static const size_t MinFileSize = sizeof(my_mach_header) + sizeof(my_segment_command) + sizeof(my_section); + static const size_t MinFatHeaderSize = sizeof(fat_header) + 2 * sizeof(fat_arch); + + if (Q_UNLIKELY(fdlen < MinFileSize)) + return ns(QLibrary::tr("file too small"), library, errorString); + + // find out if this is a fat Mach-O binary first + const my_mach_header *header = 0; + const fat_header *fat = reinterpret_cast<const fat_header *>(m_s); + if (fat->magic == qToBigEndian(FAT_MAGIC)) { + // find our architecture in the binary + const fat_arch *arch = reinterpret_cast<const fat_arch *>(fat + 1); + if (Q_UNLIKELY(fdlen < MinFatHeaderSize)) { + return ns(QLibrary::tr("file too small"), library, errorString); + } + + int count = qFromBigEndian(fat->nfat_arch); + if (Q_UNLIKELY(fdlen < sizeof(*fat) + sizeof(*arch) * count)) + return ns(QString(), library, errorString); + + for (int i = 0; i < count; ++i) { + if (arch[i].cputype == qToBigEndian(my_cputype)) { + // ### should we check the CPU subtype? Maybe on ARM? + uint32_t size = qFromBigEndian(arch[i].size); + uint32_t offset = qFromBigEndian(arch[i].offset); + if (Q_UNLIKELY(size > fdlen) || Q_UNLIKELY(offset > fdlen) + || Q_UNLIKELY(size + offset > fdlen) || Q_UNLIKELY(size < MinFileSize)) + return ns(QString(), library, errorString); + + header = reinterpret_cast<const my_mach_header *>(m_s + offset); + fdlen = size; + break; + } + } + if (!header) + return ns(QLibrary::tr("no suitable architecture in fat binary"), library, errorString); + + // check the magic again + if (Q_UNLIKELY(header->magic != my_magic)) + return ns(QString(), library, errorString); + } else { + header = reinterpret_cast<const my_mach_header *>(m_s); + fat = 0; + + // check magic + if (header->magic != my_magic) + return ns(QLibrary::tr("invalid magic %1").arg(qFromBigEndian(header->magic), 8, 16, QLatin1Char('0')), + library, errorString); + } + + // from this point on, fdlen is specific to this architecture + // from this point on, everything is in host byte order + *pos = reinterpret_cast<const char *>(header) - m_s; + + // (re-)check the CPU type + // ### should we check the CPU subtype? Maybe on ARM? + if (header->cputype != my_cputype) { + if (fat) + return ns(QString(), library, errorString); + return ns(QLibrary::tr("wrong architecture"), library, errorString); + } + + // check the file type + if (Q_UNLIKELY(header->filetype != MH_BUNDLE && header->filetype != MH_DYLIB)) + return ns(QLibrary::tr("not a dynamic library"), library, errorString); + + // find the __TEXT segment, "qtmetadata" section + const my_segment_command *seg = reinterpret_cast<const my_segment_command *>(header + 1); + ulong minsize = sizeof(*header); + + for (uint i = 0; i < header->ncmds; ++i, + seg = reinterpret_cast<const my_segment_command *>(reinterpret_cast<const char *>(seg) + seg->cmdsize)) { + // We're sure that the file size includes at least one load command + // but we have to check anyway if we're past the first + if (Q_UNLIKELY(fdlen < minsize + sizeof(load_command))) + return ns(QString(), library, errorString); + + // cmdsize can't be trusted until validated + // so check it against fdlen anyway + // (these are unsigned operations, with overflow behavior specified in the standard) + minsize += seg->cmdsize; + if (Q_UNLIKELY(fdlen < minsize) || Q_UNLIKELY(fdlen < seg->cmdsize)) + return ns(QString(), library, errorString); + + const uint32_t MyLoadCommand = sizeof(void *) > 4 ? LC_SEGMENT_64 : LC_SEGMENT; + if (seg->cmd != MyLoadCommand) + continue; + + // is this the __TEXT segment? + if (strcmp(seg->segname, "__TEXT") == 0) { + const my_section *sect = reinterpret_cast<const my_section *>(seg + 1); + for (uint j = 0; j < seg->nsects; ++j) { + // is this the "qtmetadata" section? + if (strcmp(sect[j].sectname, "qtmetadata") != 0) + continue; + + // found it! + if (Q_UNLIKELY(fdlen < sect[j].offset) || Q_UNLIKELY(fdlen < sect[j].size) + || Q_UNLIKELY(fdlen < sect[j].offset + sect[j].size)) + return ns(QString(), library, errorString); + + *pos += sect[j].offset; + *sectionlen = sect[j].size; + return QtMetaDataSection; + } + } + + // other type of segment + seg = reinterpret_cast<const my_segment_command *>(reinterpret_cast<const char *>(seg) + seg->cmdsize); + } + +// // No Qt section was found, but at least we know that where the proper architecture's boundaries are +// return NoQtSection; + if (errorString) + *errorString = QLibrary::tr("'%1' is not a Qt plugin").arg(library); + return NotSuitable; +} + +QT_END_NAMESPACE + +#endif diff --git a/src/corelib/plugin/qmachparser_p.h b/src/corelib/plugin/qmachparser_p.h new file mode 100644 index 0000000000..6b2774dab6 --- /dev/null +++ b/src/corelib/plugin/qmachparser_p.h @@ -0,0 +1,79 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Intel Corporation +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtCore module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QMACHPARSER_P_H +#define QMACHPARSER_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include <qendian.h> +#include <qglobal.h> + +#ifndef QT_NO_LIBRARY +#if defined(Q_OF_MACH_O) + +QT_BEGIN_NAMESPACE + +class QString; +class QLibraryPrivate; + +class Q_AUTOTEST_EXPORT QMachOParser +{ +public: + enum { QtMetaDataSection, NoQtSection, NotSuitable }; + static int parse(const char *m_s, ulong fdlen, const QString &library, QString *errorString, long *pos, ulong *sectionlen); +}; + +QT_END_NAMESPACE + +#endif // defined(Q_OF_ELF) && defined(Q_CC_GNU) +#endif // QT_NO_LIBRARY + +#endif // QMACHPARSER_P_H |