1/****************************************************************************
2**
3** Copyright (C) 2016 Intel Corporation.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtCore module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qmachparser_p.h"
41
42#if defined(Q_OF_MACH_O)
43
44#include <qendian.h>
45#include "qlibrary_p.h"
46
47#include <mach-o/loader.h>
48#include <mach-o/fat.h>
49
50QT_BEGIN_NAMESPACE
51
52#if defined(Q_PROCESSOR_X86_64)
53# define MACHO64
54static const cpu_type_t my_cputype = CPU_TYPE_X86_64;
55#elif defined(Q_PROCESSOR_X86_32)
56static const cpu_type_t my_cputype = CPU_TYPE_X86;
57#elif defined(Q_PROCESSOR_POWER_64)
58# define MACHO64
59static const cpu_type_t my_cputype = CPU_TYPE_POWERPC64;
60#elif defined(Q_PROCESSOR_POWER_32)
61static const cpu_type_t my_cputype = CPU_TYPE_POWERPC;
62#elif defined(Q_PROCESSOR_ARM_64)
63# define MACHO64
64static const cpu_type_t my_cputype = CPU_TYPE_ARM64;
65#elif defined(Q_PROCESSOR_ARM)
66static const cpu_type_t my_cputype = CPU_TYPE_ARM;
67#else
68# error "Unknown CPU type"
69#endif
70
71#ifdef MACHO64
72# undef MACHO64
73typedef mach_header_64 my_mach_header;
74typedef segment_command_64 my_segment_command;
75typedef section_64 my_section;
76static const uint32_t my_magic = MH_MAGIC_64;
77#else
78typedef mach_header my_mach_header;
79typedef segment_command my_segment_command;
80typedef section my_section;
81static const uint32_t my_magic = MH_MAGIC;
82#endif
83
84static int ns(const QString &reason, const QString &library, QString *errorString)
85{
86 if (errorString)
87 *errorString = QLibrary::tr("'%1' is not a valid Mach-O binary (%2)")
88 .arg(library, reason.isEmpty() ? QLibrary::tr("file is corrupt") : reason);
89 return QMachOParser::NotSuitable;
90}
91
92int QMachOParser::parse(const char *m_s, ulong fdlen, const QString &library, QString *errorString, qsizetype *pos, qsizetype *sectionlen)
93{
94 // The minimum size of a Mach-O binary we're interested in.
95 // It must have a full Mach header, at least one segment and at least one
96 // section. It's probably useless with just the "qtmetadata" section, but
97 // it's valid nonetheless.
98 // A fat binary must have this plus the fat header, of course.
99 static const size_t MinFileSize = sizeof(my_mach_header) + sizeof(my_segment_command) + sizeof(my_section);
100 static const size_t MinFatHeaderSize = sizeof(fat_header) + 2 * sizeof(fat_arch);
101
102 if (Q_UNLIKELY(fdlen < MinFileSize))
103 return ns(QLibrary::tr("file too small"), library, errorString);
104
105 // find out if this is a fat Mach-O binary first
106 const my_mach_header *header = 0;
107 const fat_header *fat = reinterpret_cast<const fat_header *>(m_s);
108 if (fat->magic == qToBigEndian(FAT_MAGIC)) {
109 // find our architecture in the binary
110 const fat_arch *arch = reinterpret_cast<const fat_arch *>(fat + 1);
111 if (Q_UNLIKELY(fdlen < MinFatHeaderSize)) {
112 return ns(QLibrary::tr("file too small"), library, errorString);
113 }
114
115 int count = qFromBigEndian(fat->nfat_arch);
116 if (Q_UNLIKELY(fdlen < sizeof(*fat) + sizeof(*arch) * count))
117 return ns(QString(), library, errorString);
118
119 for (int i = 0; i < count; ++i) {
120 if (arch[i].cputype == qToBigEndian(my_cputype)) {
121 // ### should we check the CPU subtype? Maybe on ARM?
122 uint32_t size = qFromBigEndian(arch[i].size);
123 uint32_t offset = qFromBigEndian(arch[i].offset);
124 if (Q_UNLIKELY(size > fdlen) || Q_UNLIKELY(offset > fdlen)
125 || Q_UNLIKELY(size + offset > fdlen) || Q_UNLIKELY(size < MinFileSize))
126 return ns(QString(), library, errorString);
127
128 header = reinterpret_cast<const my_mach_header *>(m_s + offset);
129 fdlen = size;
130 break;
131 }
132 }
133 if (!header)
134 return ns(QLibrary::tr("no suitable architecture in fat binary"), library, errorString);
135
136 // check the magic again
137 if (Q_UNLIKELY(header->magic != my_magic))
138 return ns(QString(), library, errorString);
139 } else {
140 header = reinterpret_cast<const my_mach_header *>(m_s);
141 fat = 0;
142
143 // check magic
144 if (header->magic != my_magic)
145 return ns(QLibrary::tr("invalid magic %1").arg(qFromBigEndian(header->magic), 8, 16, QLatin1Char('0')),
146 library, errorString);
147 }
148
149 // from this point on, fdlen is specific to this architecture
150 // from this point on, everything is in host byte order
151 *pos = reinterpret_cast<const char *>(header) - m_s;
152
153 // (re-)check the CPU type
154 // ### should we check the CPU subtype? Maybe on ARM?
155 if (header->cputype != my_cputype) {
156 if (fat)
157 return ns(QString(), library, errorString);
158 return ns(QLibrary::tr("wrong architecture"), library, errorString);
159 }
160
161 // check the file type
162 if (Q_UNLIKELY(header->filetype != MH_BUNDLE && header->filetype != MH_DYLIB))
163 return ns(QLibrary::tr("not a dynamic library"), library, errorString);
164
165 // find the __TEXT segment, "qtmetadata" section
166 const my_segment_command *seg = reinterpret_cast<const my_segment_command *>(header + 1);
167 ulong minsize = sizeof(*header);
168
169 for (uint i = 0; i < header->ncmds; ++i,
170 seg = reinterpret_cast<const my_segment_command *>(reinterpret_cast<const char *>(seg) + seg->cmdsize)) {
171 // We're sure that the file size includes at least one load command
172 // but we have to check anyway if we're past the first
173 if (Q_UNLIKELY(fdlen < minsize + sizeof(load_command)))
174 return ns(QString(), library, errorString);
175
176 // cmdsize can't be trusted until validated
177 // so check it against fdlen anyway
178 // (these are unsigned operations, with overflow behavior specified in the standard)
179 minsize += seg->cmdsize;
180 if (Q_UNLIKELY(fdlen < minsize) || Q_UNLIKELY(fdlen < seg->cmdsize))
181 return ns(QString(), library, errorString);
182
183 const uint32_t MyLoadCommand = sizeof(void *) > 4 ? LC_SEGMENT_64 : LC_SEGMENT;
184 if (seg->cmd != MyLoadCommand)
185 continue;
186
187 // is this the __TEXT segment?
188 if (strcmp(seg->segname, "__TEXT") == 0) {
189 const my_section *sect = reinterpret_cast<const my_section *>(seg + 1);
190 for (uint j = 0; j < seg->nsects; ++j) {
191 // is this the "qtmetadata" section?
192 if (strcmp(sect[j].sectname, "qtmetadata") != 0)
193 continue;
194
195 // found it!
196 if (Q_UNLIKELY(fdlen < sect[j].offset) || Q_UNLIKELY(fdlen < sect[j].size)
197 || Q_UNLIKELY(fdlen < sect[j].offset + sect[j].size))
198 return ns(QString(), library, errorString);
199
200 *pos += sect[j].offset;
201 *sectionlen = sect[j].size;
202 return QtMetaDataSection;
203 }
204 }
205
206 // other type of segment
207 seg = reinterpret_cast<const my_segment_command *>(reinterpret_cast<const char *>(seg) + seg->cmdsize);
208 }
209
210// // No Qt section was found, but at least we know that where the proper architecture's boundaries are
211// return NoQtSection;
212 if (errorString)
213 *errorString = QLibrary::tr("'%1' is not a Qt plugin").arg(library);
214 return NotSuitable;
215}
216
217QT_END_NAMESPACE
218
219#endif
220

source code of qtbase/src/corelib/plugin/qmachparser.cpp