15#if __has_include(<elf.h>)
17#elif __has_include(<sys/elf.h>)
20# error "Need ELF header to parse plugins."
34#ifdef QT_BUILD_INTERNAL
35# define QELFPARSER_DEBUG
37#if defined(QELFPARSER_DEBUG)
39# define qEDebug qCDebug(lcElfParser) << reinterpret_cast<const char16_t *>(error.errMsg->constData()) << ':'
41# define qEDebug if (false) {} else QNoDebug()
44#ifndef PT_GNU_EH_FRAME
45# define PT_GNU_EH_FRAME 0x6474e550
48# define PT_GNU_STACK 0x6474e551
51# define PT_GNU_RELRO 0x6474e552
53#ifndef PT_GNU_PROPERTY
54# define PT_GNU_PROPERTY 0x6474e553
58# define PN_XNUM 0xffff
65template <QSysInfo::Endian Order>
struct ElfEndianTraits
67 static constexpr unsigned char DataOrder = ELFDATA2LSB;
70template <>
struct ElfEndianTraits<
QSysInfo::BigEndian>
72 static constexpr unsigned char DataOrder = ELFDATA2MSB;
76template <
typename EquivalentPo
interType>
struct ElfTypeTraits
78 static constexpr unsigned char Class = ELFCLASS64;
81 using Half = Elf64_Half;
82 using Word = Elf64_Word;
83 using Addr = Elf64_Addr;
84 using Off = Elf64_Off;
87 using Ehdr = Elf64_Ehdr;
88 using Shdr = Elf64_Shdr;
89 using Phdr = Elf64_Phdr;
90 using Nhdr = Elf64_Nhdr;
92template <>
struct ElfTypeTraits<
quint32>
94 static constexpr unsigned char Class = ELFCLASS32;
97 using Half = Elf32_Half;
98 using Word = Elf32_Word;
99 using Addr = Elf32_Addr;
100 using Off = Elf32_Off;
103 using Ehdr = Elf32_Ehdr;
104 using Shdr = Elf32_Shdr;
105 using Phdr = Elf32_Phdr;
106 using Nhdr = Elf32_Nhdr;
109struct ElfMachineCheck
114#elif defined(Q_PROCESSOR_ALPHA)
116#elif defined(Q_PROCESSOR_ARM_32)
118#elif defined(Q_PROCESSOR_ARM_64)
120#elif defined(Q_PROCESSOR_BLACKFIN)
122#elif defined(Q_PROCESSOR_HPPA)
124#elif defined(Q_PROCESSOR_IA64)
126#elif defined(Q_PROCESSOR_LOONGARCH)
128#elif defined(Q_PROCESSOR_M68K)
130#elif defined(Q_PROCESSOR_MIPS)
132#elif defined(Q_PROCESSOR_POWER_32)
134#elif defined(Q_PROCESSOR_POWER_64)
136#elif defined(Q_PROCESSOR_RISCV)
138#elif defined(Q_PROCESSOR_S390)
140#elif defined(Q_PROCESSOR_SH)
142#elif defined(Q_PROCESSOR_SPARC_V9)
144#elif defined(Q_PROCESSOR_SPARC_64)
146#elif defined(Q_PROCESSOR_SPARC)
148#elif defined(Q_PROCESSOR_WASM)
149#elif defined(Q_PROCESSOR_X86_32)
151#elif defined(Q_PROCESSOR_X86_64)
154# error "Unknown Q_PROCESSOR_xxx macro, please update."
160struct ElfHeaderCommonCheck
162 static_assert(std::is_same_v<
decltype(Elf32_Ehdr::e_ident),
decltype(Elf64_Ehdr::e_ident)>,
163 "e_ident field is not the same in both Elf32_Ehdr and Elf64_Ehdr");
168 return memcmp(
ident, ELFMAG, SELFMAG) == 0;
172 static bool checkElfVersion(
const uchar *
ident)
175 return elfversion == EV_CURRENT;
178 struct CommonHeader {
185template <
typename EquivalentPo
interType = qu
intptr, QSysInfo::Endian Order = QSysInfo::ByteOrder>
186struct ElfHeaderCheck :
public ElfHeaderCommonCheck
188 using TypeTraits = ElfTypeTraits<EquivalentPointerType>;
189 using EndianTraits = ElfEndianTraits<Order>;
190 using Ehdr =
typename TypeTraits::Ehdr;
196 return klass == TypeTraits::Class;
200 static bool checkDataOrder(
const uchar *
ident)
203 return data == EndianTraits::DataOrder;
216 static bool checkAbiVersion(
const uchar *
ident)
232 static bool checkIdent(
const Ehdr &
header)
234 return checkElfMagic(
header.e_ident)
235 && checkClass(
header.e_ident)
236 && checkDataOrder(
header.e_ident)
237 && checkElfVersion(
header.e_ident)
238 && checkOsAbi(
header.e_ident)
239 && checkAbiVersion(
header.e_ident)
240 && checkPadding(
header.e_ident);
243 static bool checkType(
const Ehdr &
header)
245 return header.e_type == ET_DYN;
248 static bool checkMachine(
const Ehdr &
header)
250 return header.e_machine == ElfMachineCheck::ExpectedMachine;
253 static bool checkFileVersion(
const Ehdr &
header)
255 return header.e_version == EV_CURRENT;
258 static bool checkHeader(
const Ehdr &
header)
266 && checkFileVersion(
header);
271 if (!checkElfMagic(
header.e_ident))
272 return QLibrary::tr(
"invalid signature");
273 if (!checkClass(
header.e_ident))
274 return QLibrary::tr(
"file is for a different word size");
275 if (!checkDataOrder(
header.e_ident))
276 return QLibrary::tr(
"file is for the wrong endianness");
277 if (!checkElfVersion(
header.e_ident) || !checkFileVersion(
header))
278 return QLibrary::tr(
"file has an unknown ELF version");
279 if (!checkOsAbi(
header.e_ident) || !checkAbiVersion(
header.e_ident))
280 return QLibrary::tr(
"file has an unexpected ABI");
282 return QLibrary::tr(
"file is not a shared object");
283 if (!checkMachine(
header))
284 return QLibrary::tr(
"file is for a different processor");
288 static CommonHeader extractCommonHeader(
const uchar *
data)
290 auto header =
reinterpret_cast<const Ehdr *
>(
data);
292 r.type = EndianTraits::fromEndian(
header->e_type);
293 r.machine = EndianTraits::fromEndian(
header->e_machine);
294 r.version = EndianTraits::fromEndian(
header->e_version);
299struct ElfHeaderDebug {
const uchar *e_ident; };
302 const uchar *e_ident =
h.e_ident;
303 if (!ElfHeaderCommonCheck::checkElfMagic(e_ident)) {
304 d <<
"Not an ELF file (invalid signature)";
310 quint8 elfclass = e_ident[EI_CLASS];
314 d <<
"Invalid ELF file (class " << e_ident[EI_CLASS] <<
"), ";
324 quint8 dataorder = e_ident[EI_DATA];
328 d <<
"invalid endianness (" << e_ident[EI_DATA] <<
')';
338 switch (e_ident[EI_OSABI]) {
339 case ELFOSABI_SYSV:
d <<
" (SYSV";
break;
340 case ELFOSABI_HPUX:
d <<
" (HP-UX";
break;
341 case ELFOSABI_NETBSD:
d <<
" (NetBSD";
break;
342 case ELFOSABI_LINUX:
d <<
" (GNU/Linux";
break;
343 case ELFOSABI_SOLARIS:
d <<
" (Solaris";
break;
344 case ELFOSABI_AIX:
d <<
" (AIX";
break;
345 case ELFOSABI_IRIX:
d <<
" (IRIX";
break;
346 case ELFOSABI_FREEBSD:
d <<
" (FreeBSD";
break;
347 case ELFOSABI_OPENBSD:
d <<
" (OpenBSD";
break;
348 default:
d <<
" (OS ABI " << e_ident[EI_VERSION];
break;
351 if (e_ident[EI_ABIVERSION])
352 d <<
" v" << e_ident[EI_ABIVERSION];
355 if (e_ident[EI_VERSION] != 1) {
356 d <<
", file version " << e_ident[EI_VERSION];
360 ElfHeaderCommonCheck::CommonHeader
r;
361 if (elfclass == ELFCLASS64 && dataorder == ELFDATA2LSB)
362 r = ElfHeaderCheck<quint64, QSysInfo::LittleEndian>::extractCommonHeader(e_ident);
363 else if (elfclass == ELFCLASS32 && dataorder == ELFDATA2LSB)
364 r = ElfHeaderCheck<quint32, QSysInfo::LittleEndian>::extractCommonHeader(e_ident);
365 else if (elfclass == ELFCLASS64 && dataorder == ELFDATA2MSB)
366 r = ElfHeaderCheck<quint64, QSysInfo::BigEndian>::extractCommonHeader(e_ident);
367 else if (elfclass == ELFCLASS32 && dataorder == ELFDATA2MSB)
368 r = ElfHeaderCheck<quint32, QSysInfo::BigEndian>::extractCommonHeader(e_ident);
372 d <<
", version " <<
r.version;
375 case ET_NONE:
d <<
", no type";
break;
376 case ET_REL:
d <<
", relocatable";
break;
377 case ET_EXEC:
d <<
", executable";
break;
378 case ET_DYN:
d <<
", shared library or PIC executable";
break;
379 case ET_CORE:
d <<
", core dump";
break;
380 default:
d <<
", unknown type " <<
r.type;
break;
385 case EM_NONE:
d <<
", no machine";
break;
386 case EM_ALPHA:
d <<
", Alpha";
break;
387 case EM_68K:
d <<
", MC68000";
break;
388 case EM_ARM:
d <<
", ARM";
break;
389 case EM_AARCH64:
d <<
", AArch64";
break;
391 case EM_BLACKFIN:
d <<
", Blackfin";
break;
393 case EM_IA_64:
d <<
", IA-64";
break;
395 case EM_LOONGARCH:
d <<
", LoongArch";
break;
397 case EM_MIPS:
d <<
", MIPS";
break;
398 case EM_PARISC:
d <<
", HPPA";
break;
399 case EM_PPC:
d <<
", PowerPC";
break;
400 case EM_PPC64:
d <<
", PowerPC 64-bit";
break;
402 case EM_RISCV:
d <<
", RISC-V";
break;
405 case EM_S390:
d <<
", S/390";
break;
407 case EM_SH:
d <<
", SuperH";
break;
408 case EM_SPARC:
d <<
", SPARC";
break;
409 case EM_SPARCV9:
d <<
", SPARCv9";
break;
410 case EM_386:
d <<
", i386";
break;
411 case EM_X86_64:
d <<
", x86-64";
break;
412 default:
d <<
", other machine type " <<
r.machine;
break;
418struct ElfSectionDebug {
const ElfHeaderCheck<>::TypeTraits::Shdr *shdr; };
425 switch (
s.shdr->sh_type) {
426 case SHT_NULL:
d <<
"NULL";
break;
427 case SHT_PROGBITS:
d <<
"PROGBITS";
break;
428 case SHT_SYMTAB:
d <<
"SYMTAB";
break;
429 case SHT_STRTAB:
d <<
"STRTAB";
break;
430 case SHT_RELA:
d <<
"RELA";
break;
431 case SHT_HASH:
d <<
"HASH";
break;
432 case SHT_DYNAMIC:
d <<
"DYNAMIC";
break;
433 case SHT_NOTE:
d <<
"NOTE";
break;
434 case SHT_NOBITS:
d <<
"NOBITS";
break;
435 case SHT_DYNSYM:
d <<
"DYNSYM";
break;
436 case SHT_INIT_ARRAY:
d <<
"INIT_ARRAY";
break;
437 case SHT_FINI_ARRAY:
d <<
"FINI_ARRAY";
break;
438 default:
d <<
s.shdr->sh_type;
443 if (
s.shdr->sh_flags & SHF_WRITE)
445 if (
s.shdr->sh_flags & SHF_ALLOC)
447 if (
s.shdr->sh_flags & SHF_EXECINSTR)
449 if (
s.shdr->sh_flags & SHF_STRINGS)
451 if (
s.shdr->sh_flags & SHF_TLS)
454 d.space() <<
"offset" <<
s.shdr->sh_offset <<
"size" <<
s.shdr->sh_size;
458struct ElfProgramDebug {
const ElfHeaderCheck<>::TypeTraits::Phdr *phdr; };
463 switch (
p.phdr->p_type) {
464 case PT_NULL:
d <<
"NULL";
break;
465 case PT_LOAD:
d <<
"LOAD";
break;
466 case PT_DYNAMIC:
d <<
"DYNAMIC";
break;
467 case PT_INTERP:
d <<
"INTERP";
break;
468 case PT_NOTE:
d <<
"NOTE";
break;
469 case PT_PHDR:
d <<
"PHDR";
break;
470 case PT_TLS:
d <<
"TLS";
break;
471 case PT_GNU_EH_FRAME:
d <<
"GNU_EH_FRAME";
break;
472 case PT_GNU_STACK:
d <<
"GNU_STACK";
break;
473 case PT_GNU_RELRO:
d <<
"GNU_RELRO";
break;
474 case PT_GNU_PROPERTY:
d <<
"GNU_PROPERTY";
break;
475 default:
d <<
"type" <<
p.phdr->p_type;
break;
478 d <<
"offset" <<
p.phdr->p_offset
479 <<
"virtaddr" <<
p.phdr->p_vaddr
480 <<
"filesz" <<
p.phdr->p_filesz
481 <<
"memsz" <<
p.phdr->p_memsz
482 <<
"align" <<
p.phdr->p_align
486 if (
p.phdr->p_flags & PF_R)
488 if (
p.phdr->p_flags & PF_W)
490 if (
p.phdr->p_flags & PF_X)
504 *
errMsg = QLibrary::tr(
"'%1' is not a valid ELF object (%2)").
arg(*errMsg, std::move(
text));
510 *
errMsg = QLibrary::tr(
"'%1' is not a Qt plugin (%2)").
arg(*errMsg, explanation);
516 return notplugin(QLibrary::tr(
"metadata not found"));
523using T = ElfHeaderCheck<>::TypeTraits;
528 auto header =
reinterpret_cast<const T::Ehdr *
>(
data.data());
531 auto phdr =
reinterpret_cast<const T::Phdr *
>(
data.data() +
header->e_phoff);
532 auto phdr_end = phdr +
header->e_phnum;
533 for ( ; phdr != phdr_end; ++phdr) {
542 auto header =
reinterpret_cast<const T::Ehdr *
>(
data.data());
545 T::Word e_phnum =
header->e_phnum;
546 if (e_phnum == PN_XNUM)
547 return error(QLibrary::tr(
"unimplemented: PN_XNUM program headers")),
false;
548 T::Off
offset = e_phnum *
sizeof(T::Phdr);
550 return error(QLibrary::tr(
"program header table extends past the end of the file")),
false;
553 bool hasCode =
false;
554 auto checker = [&](
const T::Phdr *phdr) {
555 qEDebug << ElfProgramDebug{phdr};
559 return error(QLibrary::tr(
"a program header entry extends past the end of the file")),
false;
562 if (phdr->p_type == PT_LOAD && phdr->p_filesz != 0 && (phdr->p_flags & PF_X))
567 && phdr->p_offset & (phdr->p_align - 1)) {
568 return error(QLibrary::tr(
"a note segment start is not properly aligned "
569 "(offset 0x%1, alignment %2)")
570 .
arg(phdr->p_offset, 6, 16,
QChar(u
'0'))
571 .
arg(phdr->p_align)),
false;
576 if (!scanProgramHeaders(
data,
error, checker))
579 return error.notplugin(QLibrary::tr(
"file has no code")),
false;
591 static_assert(MinNoteSize > PayloadStartDelta);
592 static_assert((PayloadStartDelta & (NoteAlignment - 1)) == 0);
595 auto noteFinder = [&](
const T::Phdr *phdr) {
596 if (phdr->p_type != PT_NOTE || phdr->p_align != NoteAlignment)
602 auto h =
reinterpret_cast<const T::Ehdr *
>(
data.data());
603 auto segments =
reinterpret_cast<const T::Phdr *
>(
data.data() +
h->e_phoff);
604 qEDebug <<
"segment" << (phdr -
segments) <<
"contains a note with size"
606 <<
"which is larger than half the virtual memory space";
611 T::Off
offset = phdr->p_offset;
612 const T::Off end_offset =
offset + phdr->p_filesz;
614 auto nhdr =
reinterpret_cast<const T::Nhdr *
>(
data.data() +
offset);
615 T::Word n_namesz = nhdr->n_namesz;
616 T::Word n_descsz = nhdr->n_descsz;
617 T::Word n_type = nhdr->n_type;
620 T::Off next_offset =
offset;
621 next_offset +=
sizeof(T::Nhdr);
622 next_offset += NoteAlignment - 3;
623 if (qAddOverflow<T::Off>(next_offset, n_namesz, &next_offset))
625 next_offset &= -NoteAlignment;
627 next_offset += NoteAlignment - 3;
628 if (qAddOverflow<T::Off>(next_offset, n_descsz, &next_offset))
630 next_offset &= -NoteAlignment;
631 if (next_offset > end_offset)
634 if (n_namesz == NoteNameSize && n_descsz >= MinPayloadSize
638 r.pos =
offset + PayloadStartDelta;
639 r.length = nhdr->n_descsz;
646 scanProgramHeaders(
data,
error, noteFinder);
651 qEDebug <<
"found Qt metadata in ELF note at"
658 auto header =
reinterpret_cast<const T::Ehdr *
>(
data.data());
664 T::Word e_shnum =
header->e_shnum;
665 T::Off
offset = e_shnum *
sizeof(T::Shdr);
667 return error(QLibrary::tr(
"section table extends past the end of the file"));
671 auto sections =
reinterpret_cast<const T::Shdr *
>(
data.data() +
header->e_shoff);
672 auto sections_end = sections + e_shnum;
673 auto shdr = sections +
header->e_shstrndx;
677 T::Off shstrtab_size = shdr->sh_size;
678 qEDebug <<
"shstrtab section is located at offset" <<
offset <<
"size" << shstrtab_size;
679 if (T::Off
end; qAddOverflow<T::Off>(
offset, shstrtab_size, &
end)
681 return error(QLibrary::tr(
"section header string table extends past the end of the file"));
684 const char *shstrtab_start =
data.data() +
offset;
686 for (
int section = 0; shdr != sections_end; ++section, ++shdr) {
688 if (shdr->sh_name < shstrtab_size) {
689 const char *namestart = shstrtab_start + shdr->sh_name;
690 size_t len =
qstrnlen(namestart, shstrtab_size - shdr->sh_name);
693 qEDebug <<
"section" << section <<
"name" <<
name << ElfSectionDebug{shdr};
697 return error(QLibrary::tr(
"a section name extends past the end of the file"));
701 if (shdr->sh_type == SHT_NOBITS)
704 ||
end > size_t(
data.size())) {
705 return error(QLibrary::tr(
"section contents extend past the end of the file"));
708 if (
name !=
".qtmetadata"_L1)
710 qEDebug <<
"found .qtmetadata section";
712 return error(QLibrary::tr(
".qtmetadata section is too small"));
717 if (expectedMagic != actualMagic)
718 return error(QLibrary::tr(
".qtmetadata section has incorrect magic"));
720 if (shdr->sh_flags & SHF_WRITE)
721 return error(QLibrary::tr(
".qtmetadata section is writable"));
722 if (shdr->sh_flags & SHF_EXECINSTR)
723 return error(QLibrary::tr(
".qtmetadata section is executable"));
731 return error.notfound();
737 if (
size_t(
data.size()) <
sizeof(T::Ehdr)) {
738 qEDebug <<
"file too small:" << size_t(
data.size());
739 return error(QLibrary::tr(
"file too small"));
742 qEDebug << ElfHeaderDebug{
reinterpret_cast<const uchar *
>(
data.data()) };
744 auto header =
reinterpret_cast<const T::Ehdr *
>(
data.data());
745 if (!ElfHeaderCheck<>::checkHeader(*
header))
746 return error(ElfHeaderCheck<>::explainCheckFailure(*
header));
748 qEDebug <<
"contains" <<
header->e_phnum <<
"program headers of"
749 <<
header->e_phentsize <<
"bytes at offset" <<
header->e_phoff;
750 qEDebug <<
"contains" <<
header->e_shnum <<
"sections of" <<
header->e_shentsize
751 <<
"bytes at offset" <<
header->e_shoff
752 <<
"; section header string table (shstrtab) is entry" <<
header->e_shstrndx;
756 if (
header->e_phentsize !=
sizeof(T::Phdr))
757 return error(QLibrary::tr(
"unexpected program header entry size (%1)")
758 .arg(
header->e_phentsize));
767 if (!ElfNotesAreMandatory) {
769 if (
header->e_shentsize !=
sizeof(T::Shdr))
770 return error(QLibrary::tr(
"unexpected section entry size (%1)")
771 .arg(
header->e_shentsize));
775 qEDebug <<
"no section table present, not able to find Qt metadata";
776 return error.notfound();
780 return error(QLibrary::tr(
"e_shstrndx greater than the number of sections e_shnum (%1 >= %2)")
784 return error.notfound();
constexpr QByteArrayView sliced(qsizetype pos) const
static constexpr QByteArrayView fromArray(const Byte(&data)[Size]) noexcept
\macro QT_RESTRICTED_CAST_FROM_ASCII
QString arg(qlonglong a, int fieldwidth=0, int base=10, QChar fillChar=u' ') const
Combined button and popup list for selecting options.
QTextStream & hex(QTextStream &stream)
Calls QTextStream::setIntegerBase(16) on stream and returns stream.
QTextStream & showbase(QTextStream &stream)
Calls QTextStream::setNumberFlags(QTextStream::numberFlags() | QTextStream::ShowBase) on stream and r...
QTextStream & reset(QTextStream &stream)
Calls QTextStream::reset() on stream and returns stream.
Q_DECL_CONST_FUNCTION QT_POPCOUNT_CONSTEXPR uint qPopulationCount(quint32 v) noexcept
size_t qstrnlen(const char *str, size_t maxlen)
static QT_WARNING_PUSH const WORD ExpectedMachine
static constexpr bool IncludeValidityChecks
#define Q_DECL_COLD_FUNCTION
#define QT_WARNING_DISABLE_CLANG(text)
DBusConnection const char DBusError * error
static QString header(const QString &name)
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
constexpr T qFromLittleEndian(T source)
constexpr T qFromBigEndian(T source)
qsizetype QString * errMsg
#define Q_LOGGING_CATEGORY(name,...)
static Q_DECL_COLD_FUNCTION QLibraryScanResult notfound(const QString &reason, QString *errorString)
std::enable_if_t< std::is_unsigned_v< T >, bool > qAddOverflow(T v1, T v2, T *r)
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLenum GLuint GLintptr offset
GLfloat GLfloat GLfloat GLfloat h
#define QT_VERSION_CHECK(major, minor, patch)