1 | // Copyright (C) 2022 Intel Corporation. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #if !defined(QVERSIONTAGGING_H) |
5 | #define QVERSIONTAGGING_H |
6 | |
7 | #include <QtCore/qcompilerdetection.h> |
8 | #include <QtCore/qtconfigmacros.h> |
9 | #include <QtCore/qtversionchecks.h> |
10 | #include <QtCore/qtypes.h> |
11 | |
12 | QT_BEGIN_NAMESPACE |
13 | |
14 | /* |
15 | * Explanation |
16 | * |
17 | * This file causes all libraries, plugins, and applications that #include this |
18 | * file to automatically pull in a symbol found in QtCore that encodes the |
19 | * current Qt version number at the time of compilation. The relocation is |
20 | * designed so that it's impossible for the dynamic linker to perform lazy |
21 | * binding. Instead, it must resolve at load time or fail. That way, attempting |
22 | * to load such a library or plugin while an older QtCore is loaded will fail. |
23 | * Similarly, if an older QtCore is found when launching an application, the |
24 | * application will fail to launch. |
25 | * |
26 | * It's also possible to inspect which version is required by decoding the |
27 | * .qtversion section. The second pointer-sized variable is the required |
28 | * version, for example, for Qt 6.4.1: |
29 | * |
30 | * Hex dump of section [18] '.qtversion', 16 bytes at offset 0x1ee48: |
31 | * 0x00000000 b0ffffff ffffffff 01040600 00000000 ................ |
32 | * ^^^^^^^^ ^^^^^^^^ |
33 | * |
34 | * There will only be one copy of the section in the output library or application. |
35 | * |
36 | * This functionality can be disabled by defining QT_NO_VERSION_TAGGING. It's |
37 | * disabled if Qt was built statically. |
38 | * |
39 | * Windows notes: |
40 | * |
41 | * On Windows, the address of a __declspec(dllimport) variable is not a |
42 | * constant expression, unlike Unix systems. So we instead use the address of |
43 | * the import variable, which is created by prefixing the external name with |
44 | * "__imp_". Using that variable causes an import of the corresponding symbol |
45 | * from QtCore DLL. |
46 | * |
47 | * With MinGW (GCC and Clang), we use a C++17 inline variable, so the compiler |
48 | * and linker automatically merge the variables. The "used" __attribute__ |
49 | * tells the compiler to always emit that variable, whether it's used or not. |
50 | * |
51 | * MSVC has no equivalent to that attribute, so instead we create an extern |
52 | * const variable and tell the linker to merge them all via |
53 | * __declspec(selectany). |
54 | * |
55 | * Unix notes: |
56 | * |
57 | * On Unix, we use the same C++17 inline variable solution as MinGW, but we |
58 | * don't need the "__imp_" trick. |
59 | * |
60 | * Additionally, on ELF systems like Linux and FreeBSD, the symbol in question |
61 | * is simply "qt_version_tag" in both QtCore and in this ELF module, but it |
62 | * has an ELF version attached to it (see qversiontagging.cpp and |
63 | * QtFlagHandlingHelpers.cmake). That way, the error message from the dynamic |
64 | * linker will say it can't find version "Qt_6.x". |
65 | */ |
66 | |
67 | namespace QtPrivate { |
68 | struct QVersionTag |
69 | { |
70 | const void *symbol; |
71 | quintptr version; |
72 | constexpr QVersionTag(const void *sym, int currentVersion = QT_VERSION) |
73 | : symbol(sym), version(currentVersion) |
74 | {} |
75 | }; |
76 | } |
77 | |
78 | #if !defined(QT_NO_VERSION_TAGGING) && (defined(QT_BUILD_CORE_LIB) || defined(QT_BOOTSTRAPPED) || defined(QT_STATIC)) |
79 | // don't make tags in QtCore, bootstrapped systems or if the user asked not to |
80 | # define QT_NO_VERSION_TAGGING |
81 | #endif |
82 | |
83 | #if defined(Q_OS_WIN) |
84 | # ifdef Q_PROCESSOR_X86_32 |
85 | // 32-bit x86 convention does prepend a _ |
86 | # define QT_MANGLE_IMPORT_PREFIX _imp__ |
87 | # else |
88 | // Calling convention on other architectures does not prepend a _ |
89 | # define QT_MANGLE_IMPORT_PREFIX __imp_ |
90 | # endif |
91 | # if defined(Q_CC_MSVC_ONLY) |
92 | # pragma section(".qtversion",read,shared) |
93 | # define QT_VERSION_TAG_SECTION __declspec(allocate(".qtversion")) |
94 | # define QT_VERSION_TAG_ATTRIBUTE __declspec(selectany) extern const |
95 | # else |
96 | # define QT_VERSION_TAG_ATTRIBUTE __attribute__((used)) constexpr inline |
97 | # endif |
98 | # define QT_VERSION_TAG2(sym, imp) \ |
99 | extern "C" const char * const imp; \ |
100 | QT_VERSION_TAG_ATTRIBUTE QT_VERSION_TAG_SECTION QtPrivate::QVersionTag sym ## _used(&imp) |
101 | # define QT_VERSION_TAG(sym, imp) QT_VERSION_TAG2(sym, imp) |
102 | #elif defined(Q_CC_GNU) && __has_attribute(used) |
103 | # ifdef Q_OS_DARWIN |
104 | # define QT_VERSION_TAG_SECTION __attribute__((section("__DATA,.qtversion"))) |
105 | # endif |
106 | # define QT_VERSION_TAG_ATTRIBUTE __attribute__((visibility("hidden"), used)) |
107 | # define QT_VERSION_TAG2(sym, imp) \ |
108 | extern "C" Q_DECL_IMPORT const char sym; \ |
109 | QT_VERSION_TAG_ATTRIBUTE QT_VERSION_TAG_SECTION constexpr inline QtPrivate::QVersionTag sym ## _use(&sym) |
110 | # define QT_VERSION_TAG(sym, imp) QT_VERSION_TAG2(sym, imp) |
111 | #endif |
112 | |
113 | #ifdef Q_OF_ELF |
114 | # define QT_VERSION_TAG_SYMBOL(prefix, sym, m, n) sym |
115 | #else |
116 | # define QT_VERSION_TAG_SYMBOL2(prefix, sym, m, n) prefix ## sym ## _ ## m ## _ ## n |
117 | # define QT_VERSION_TAG_SYMBOL(prefix, sym, m, n) QT_VERSION_TAG_SYMBOL2(prefix, sym, m, n) |
118 | #endif |
119 | |
120 | #if defined(QT_VERSION_TAG) && !defined(QT_NO_VERSION_TAGGING) |
121 | # ifndef QT_VERSION_TAG_SECTION |
122 | # define QT_VERSION_TAG_SECTION __attribute__((section(".qtversion"))) |
123 | # endif |
124 | # define QT_MANGLED_VERSION_TAG_IMPORT QT_VERSION_TAG_SYMBOL(QT_MANGLE_IMPORT_PREFIX, QT_MANGLE_NAMESPACE(qt_version_tag), QT_VERSION_MAJOR, QT_VERSION_MINOR) |
125 | # define QT_MANGLED_VERSION_TAG QT_VERSION_TAG_SYMBOL(, QT_MANGLE_NAMESPACE(qt_version_tag), QT_VERSION_MAJOR, QT_VERSION_MINOR) |
126 | |
127 | QT_VERSION_TAG(QT_MANGLED_VERSION_TAG, QT_MANGLED_VERSION_TAG_IMPORT); |
128 | |
129 | # undef QT_MANGLED_VERSION_TAG |
130 | # undef QT_MANGLED_VERSION_TAG_IMPORT |
131 | # undef QT_VERSION_TAG_SECTION |
132 | #endif |
133 | |
134 | QT_END_NAMESPACE |
135 | |
136 | #endif // QVERSIONTAGGING_H |
137 | |