9#include <mach-o/loader.h>
10#include <mach-o/fat.h>
14using namespace Qt::StringLiterals;
20#if defined(Q_PROCESSOR_X86_64)
22static const cpu_type_t my_cputype = CPU_TYPE_X86_64;
23#elif defined(Q_PROCESSOR_X86_32)
24static const cpu_type_t my_cputype = CPU_TYPE_X86;
25#elif defined(Q_PROCESSOR_POWER_64)
27static const cpu_type_t my_cputype = CPU_TYPE_POWERPC64;
28#elif defined(Q_PROCESSOR_POWER_32)
29static const cpu_type_t my_cputype = CPU_TYPE_POWERPC;
30#elif defined(Q_PROCESSOR_ARM_64)
32static const cpu_type_t my_cputype = CPU_TYPE_ARM64;
33#elif defined(Q_PROCESSOR_ARM)
34static const cpu_type_t my_cputype = CPU_TYPE_ARM;
36# error "Unknown CPU type"
41typedef mach_header_64 my_mach_header;
42typedef segment_command_64 my_segment_command;
43typedef section_64 my_section;
44static const uint32_t my_magic = MH_MAGIC_64;
53static QLibraryScanResult notfound(
const QString &reason, QString *errorString)
55 *errorString = QLibrary::tr(
"'%1' is not a valid Mach-O binary (%2)")
56 .arg(*errorString, reason.isEmpty() ? QLibrary::tr(
"file is corrupt") : reason);
63 for (uint32_t i = 0; i < header->ncmds; ++i) {
64 load_command *loadCommand =
reinterpret_cast<load_command *>(commandCursor);
65 if (loadCommand->cmd == LC_ENCRYPTION_INFO || loadCommand->cmd == LC_ENCRYPTION_INFO_64) {
68 auto encryptionInfoCommand =
reinterpret_cast<encryption_info_command*>(loadCommand);
69 return encryptionInfoCommand->cryptid != 0;
71 commandCursor += loadCommand->cmdsize;
77QLibraryScanResult QMachOParser::parse(
const char *m_s, ulong fdlen, QString *errorString)
84 static const size_t MinFileSize =
sizeof(my_mach_header) +
sizeof(my_segment_command) +
sizeof(my_section);
85 static const size_t MinFatHeaderSize =
sizeof(fat_header) + 2 *
sizeof(fat_arch);
87 if (Q_UNLIKELY(fdlen < MinFileSize))
88 return notfound(QLibrary::tr(
"file too small"), errorString);
91 const my_mach_header *header =
nullptr;
92 const fat_header *fat =
reinterpret_cast<
const fat_header *>(m_s);
93 if (fat->magic == qToBigEndian(FAT_MAGIC)) {
95 const fat_arch *arch =
reinterpret_cast<
const fat_arch *>(fat + 1);
96 if (Q_UNLIKELY(fdlen < MinFatHeaderSize)) {
97 return notfound(QLibrary::tr(
"file too small"), errorString);
100 int count = qFromBigEndian(fat->nfat_arch);
101 if (Q_UNLIKELY(fdlen <
sizeof(*fat) +
sizeof(*arch) * count))
102 return notfound(QString(), errorString);
104 for (
int i = 0; i < count; ++i) {
105 if (arch[i].cputype == qToBigEndian(my_cputype)) {
107 uint32_t size = qFromBigEndian(arch[i].size);
108 uint32_t offset = qFromBigEndian(arch[i].offset);
109 if (Q_UNLIKELY(size > fdlen) || Q_UNLIKELY(offset > fdlen)
110 || Q_UNLIKELY(size + offset > fdlen) || Q_UNLIKELY(size < MinFileSize))
111 return notfound(QString(), errorString);
113 header =
reinterpret_cast<
const my_mach_header *>(m_s + offset);
119 return notfound(QLibrary::tr(
"no suitable architecture in fat binary"), errorString);
122 if (Q_UNLIKELY(header->magic != my_magic))
123 return notfound(QString(), errorString);
125 header =
reinterpret_cast<
const my_mach_header *>(m_s);
129 if (header->magic != my_magic)
130 return notfound(QLibrary::tr(
"invalid magic %1").arg(qFromBigEndian(header->magic),
139 if (header->cputype != my_cputype) {
141 return notfound(QString(), errorString);
142 return notfound(QLibrary::tr(
"wrong architecture"), errorString);
146 if (Q_UNLIKELY(header->filetype != MH_BUNDLE && header->filetype != MH_DYLIB))
147 return notfound(QLibrary::tr(
"not a dynamic library"), errorString);
150 const my_segment_command *seg =
reinterpret_cast<
const my_segment_command *>(header + 1);
151 ulong minsize =
sizeof(*header);
153 for (uint i = 0; i < header->ncmds; ++i,
154 seg =
reinterpret_cast<
const my_segment_command *>(
reinterpret_cast<
const char *>(seg) + seg->cmdsize)) {
157 if (Q_UNLIKELY(fdlen < minsize +
sizeof(load_command)))
158 return notfound(QString(), errorString);
163 minsize += seg->cmdsize;
164 if (Q_UNLIKELY(fdlen < minsize) || Q_UNLIKELY(fdlen < seg->cmdsize))
165 return notfound(QString(), errorString);
167 const uint32_t MyLoadCommand =
sizeof(
void *) > 4 ? LC_SEGMENT_64 : LC_SEGMENT;
168 if (seg->cmd != MyLoadCommand)
172 if (strcmp(seg->segname,
"__TEXT") == 0) {
173 const my_section *sect =
reinterpret_cast<
const my_section *>(seg + 1);
174 for (uint j = 0; j < seg->nsects; ++j) {
176 if (strcmp(sect[j].sectname,
"qtmetadata") != 0)
180 if (Q_UNLIKELY(fdlen < sect[j].offset) || Q_UNLIKELY(fdlen < sect[j].size)
181 || Q_UNLIKELY(fdlen < sect[j].offset + sect[j].size))
182 return notfound(QString(), errorString);
184 if (sect[j].size <
sizeof(QPluginMetaData::MagicHeader))
185 return notfound(QLibrary::tr(
".qtmetadata section is too small"), errorString);
187 const bool binaryIsEncrypted = isEncrypted(header);
188 qsizetype pos =
reinterpret_cast<
const char *>(header) - m_s + sect[j].offset;
192 if (IncludeValidityChecks && !binaryIsEncrypted) {
193 QByteArrayView expectedMagic = QByteArrayView::fromArray(QPluginMetaData::MagicString);
194 QByteArrayView actualMagic = QByteArrayView(m_s + pos, expectedMagic.size());
195 if (expectedMagic != actualMagic)
196 return notfound(QLibrary::tr(
".qtmetadata section has incorrect magic"), errorString);
199 pos +=
sizeof(QPluginMetaData::MagicString);
200 return { pos, qsizetype(sect[j].size -
sizeof(QPluginMetaData::MagicString)), binaryIsEncrypted };
205 seg =
reinterpret_cast<
const my_segment_command *>(
reinterpret_cast<
const char *>(seg) + seg->cmdsize);
209 *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