8#include <mach-o/loader.h>
13using namespace Qt::StringLiterals;
19#if defined(Q_PROCESSOR_X86_64)
21static const cpu_type_t my_cputype = CPU_TYPE_X86_64;
22#elif defined(Q_PROCESSOR_X86_32)
23static const cpu_type_t my_cputype = CPU_TYPE_X86;
24#elif defined(Q_PROCESSOR_POWER_64)
26static const cpu_type_t my_cputype = CPU_TYPE_POWERPC64;
27#elif defined(Q_PROCESSOR_POWER_32)
28static const cpu_type_t my_cputype = CPU_TYPE_POWERPC;
29#elif defined(Q_PROCESSOR_ARM_64)
31static const cpu_type_t my_cputype = CPU_TYPE_ARM64;
32#elif defined(Q_PROCESSOR_ARM)
33static const cpu_type_t my_cputype = CPU_TYPE_ARM;
35# error "Unknown CPU type"
40typedef mach_header_64 my_mach_header;
41typedef segment_command_64 my_segment_command;
42typedef section_64 my_section;
43static const uint32_t my_magic = MH_MAGIC_64;
52static QLibraryScanResult notfound(
const QString &reason, QString *errorString)
54 *errorString = QLibrary::tr(
"'%1' is not a valid Mach-O binary (%2)")
55 .arg(*errorString, reason.isEmpty() ? QLibrary::tr(
"file is corrupt") : reason);
62 for (uint32_t i = 0; i < header->ncmds; ++i) {
63 load_command *loadCommand =
reinterpret_cast<load_command *>(commandCursor);
64 if (loadCommand->cmd == LC_ENCRYPTION_INFO || loadCommand->cmd == LC_ENCRYPTION_INFO_64) {
67 auto encryptionInfoCommand =
reinterpret_cast<encryption_info_command*>(loadCommand);
68 return encryptionInfoCommand->cryptid != 0;
70 commandCursor += loadCommand->cmdsize;
76QLibraryScanResult QMachOParser::parse(
const char *m_s, ulong fdlen, QString *errorString)
83 static const size_t MinFileSize =
sizeof(my_mach_header) +
sizeof(my_segment_command) +
sizeof(my_section);
84 static const size_t MinFatHeaderSize =
sizeof(fat_header) + 2 *
sizeof(fat_arch);
86 if (Q_UNLIKELY(fdlen < MinFileSize))
87 return notfound(QLibrary::tr(
"file too small"), errorString);
90 const my_mach_header *header =
nullptr;
91 const fat_header *fat =
reinterpret_cast<
const fat_header *>(m_s);
92 if (fat->magic == qToBigEndian(FAT_MAGIC)) {
94 const fat_arch *arch =
reinterpret_cast<
const fat_arch *>(fat + 1);
95 if (Q_UNLIKELY(fdlen < MinFatHeaderSize)) {
96 return notfound(QLibrary::tr(
"file too small"), errorString);
99 int count = qFromBigEndian(fat->nfat_arch);
100 if (Q_UNLIKELY(fdlen <
sizeof(*fat) +
sizeof(*arch) * count))
101 return notfound(QString(), errorString);
103 for (
int i = 0; i < count; ++i) {
104 if (arch[i].cputype == qToBigEndian(my_cputype)) {
106 uint32_t size = qFromBigEndian(arch[i].size);
107 uint32_t offset = qFromBigEndian(arch[i].offset);
108 if (Q_UNLIKELY(size > fdlen) || Q_UNLIKELY(offset > fdlen)
109 || Q_UNLIKELY(size + offset > fdlen) || Q_UNLIKELY(size < MinFileSize))
110 return notfound(QString(), errorString);
112 header =
reinterpret_cast<
const my_mach_header *>(m_s + offset);
118 return notfound(QLibrary::tr(
"no suitable architecture in fat binary"), errorString);
121 if (Q_UNLIKELY(header->magic != my_magic))
122 return notfound(QString(), errorString);
124 header =
reinterpret_cast<
const my_mach_header *>(m_s);
128 if (header->magic != my_magic)
129 return notfound(QLibrary::tr(
"invalid magic %1").arg(qFromBigEndian(header->magic),
138 if (header->cputype != my_cputype) {
140 return notfound(QString(), errorString);
141 return notfound(QLibrary::tr(
"wrong architecture"), errorString);
145 if (Q_UNLIKELY(header->filetype != MH_BUNDLE && header->filetype != MH_DYLIB))
146 return notfound(QLibrary::tr(
"not a dynamic library"), errorString);
149 const my_segment_command *seg =
reinterpret_cast<
const my_segment_command *>(header + 1);
150 ulong minsize =
sizeof(*header);
152 for (uint i = 0; i < header->ncmds; ++i,
153 seg =
reinterpret_cast<
const my_segment_command *>(
reinterpret_cast<
const char *>(seg) + seg->cmdsize)) {
156 if (Q_UNLIKELY(fdlen < minsize +
sizeof(load_command)))
157 return notfound(QString(), errorString);
162 minsize += seg->cmdsize;
163 if (Q_UNLIKELY(fdlen < minsize) || Q_UNLIKELY(fdlen < seg->cmdsize))
164 return notfound(QString(), errorString);
166 const uint32_t MyLoadCommand =
sizeof(
void *) > 4 ? LC_SEGMENT_64 : LC_SEGMENT;
167 if (seg->cmd != MyLoadCommand)
171 if (strcmp(seg->segname,
"__TEXT") == 0) {
172 const my_section *sect =
reinterpret_cast<
const my_section *>(seg + 1);
173 for (uint j = 0; j < seg->nsects; ++j) {
175 if (strcmp(sect[j].sectname,
"qtmetadata") != 0)
179 if (Q_UNLIKELY(fdlen < sect[j].offset) || Q_UNLIKELY(fdlen < sect[j].size)
180 || Q_UNLIKELY(fdlen < sect[j].offset + sect[j].size))
181 return notfound(QString(), errorString);
183 if (sect[j].size <
sizeof(QPluginMetaData::MagicHeader))
184 return notfound(QLibrary::tr(
".qtmetadata section is too small"), errorString);
186 const bool binaryIsEncrypted = isEncrypted(header);
187 qsizetype pos =
reinterpret_cast<
const char *>(header) - m_s + sect[j].offset;
191 if (IncludeValidityChecks && !binaryIsEncrypted) {
192 QByteArrayView expectedMagic = QByteArrayView::fromArray(QPluginMetaData::MagicString);
193 QByteArrayView actualMagic = QByteArrayView(m_s + pos, expectedMagic.size());
194 if (expectedMagic != actualMagic)
195 return notfound(QLibrary::tr(
".qtmetadata section has incorrect magic"), errorString);
198 pos +=
sizeof(QPluginMetaData::MagicString);
199 return { pos, qsizetype(sect[j].size -
sizeof(QPluginMetaData::MagicString)), binaryIsEncrypted };
204 seg =
reinterpret_cast<
const my_segment_command *>(
reinterpret_cast<
const char *>(seg) + seg->cmdsize);
208 *errorString = QLibrary::tr(
"'%1' is not a Qt plugin").arg(*errorString);
Combined button and popup list for selecting options.
mach_header my_mach_header
static bool isEncrypted(const my_mach_header *header)
static const uint32_t my_magic
segment_command my_segment_command
static constexpr bool IncludeValidityChecks