Library:Qt/Meta-object data
Layout of the tables that Qt's moc generates for every Q_OBJECT/Q_GADGET class: the QMetaObject struct, the integer data block (QMetaObjectPrivate header, method, property, enum and constructor records) and the revision numbers that identify the Qt version. Reading these recovers class hierarchies, signals, slots and properties from stripped binaries.
QMetaObject
Every class with Q_OBJECT/Q_GADGET has a static QMetaObject staticMetaObject (symbol _ZN<len><Class>16staticMetaObjectE, MSVC ?staticMetaObject@<Class>@@2UQMetaObject@@B). Qt 6 layout (QMetaObject::d, pointer-sized members): const QMetaObject* superdata; const uint* stringdata; const uint* data; void (*static_metacall)(QObject*, QMetaObject::Call, int, void**); const SuperData* relatedMetaObjects; const QMetaTypeInterface* const* metaTypes; void* extradata; superdata is the parent class's staticMetaObject (or, for classes in other DLLs, a pointer to a pointer: SuperData::link<>). Following superdata gives the class hierarchy up to QObject. In Qt 5 the member order was superdata, stringdata (a QByteArrayData* array), data, static_metacall, relatedMetaObjects, extradata; no metaTypes (unverified, from memory).
Data block (QMetaObjectPrivate, int[] at data)
Header (14 ints, all offsets are in ints from the start of data, string references are indices into the string table): revision, className, classInfoCount, classInfoData, methodCount, methodData, propertyCount, propertyData, enumeratorCount, enumeratorData, constructorCount, constructorData, flags, signalCount. revision is the moc output revision of the Qt that built the class:
| revision | Qt |
|---|---|
| 7 | Qt 5.0 |
| 8 | Qt 5.12 (enum record gains an alias name) |
| 9 | Qt 6.0 (metatypes for properties and methods) |
| 10 | Qt 6.2 (metaobject's own metatype in the metaTypes array; method const flag) |
| 11 | Qt 6.5 (metatype for void) |
| 12 | Qt 6.6 (metatype for enums) |
| 13 | Qt 6.9 (64-bit QFlags; method revision moved) |
The generated file also compares Q_MOC_OUTPUT_REVISION (moc's mocOutputRevision, 69 in this checkout) at compile time; that number is not stored in the binary. |
Records (from QMetaMethod::Data, QMetaProperty::Data, QMetaEnum::Data)
- Method record, 6 ints in Qt 6 (5 in Qt 5):
name(string index),argc,parameters(int offset of the parameter data),tag,flags,metaTypeOffset. The parameters block holds, for argc+1 entries: the return type, then each argument's type, then each argument's name (string index). Types are either a QMetaType id/index or, when the top bit (0x80000000,IsUnresolvedType) is set, a string index of a type name resolved at runtime. - Method order: signals first (
signalCountof them), then slots, then invokable methods; constructors are in a separate block after the enums. - Method flags: access 0x00 private, 0x01 protected, 0x02 public (mask 0x03); type 0x00 method, 0x04 signal, 0x08 slot, 0x0c constructor (mask 0x0c; swapped in a future Qt 7); 0x10 compatibility, 0x20 cloned (default-argument variants), 0x40 scriptable, 0x80 revisioned, 0x100 const.
- Property record, 5 ints:
name,type,flags,notifyIndex(signal index, or -1 for none),revision. Property flags: 0x1 readable, 0x2 writable, 0x4 resettable, 0x8 enum-or-flag, 0x10 alias, 0x20 virtual, 0x40 override, 0x100 StdCppSet, 0x400 constant, 0x800 final, 0x1000 designable, 0x4000 scriptable, 0x10000 stored, 0x100000 user, 0x1000000 required, 0x2000000 bindable. The default is readable|designable|scriptable|stored. - Enum record, 5 ints in Qt 6 / Qt 5.12+ (4 earlier):
name,alias,flags(0x1 is-flag, 0x2 scoped, 0x40 64-bit),keyCount,data(offset ofkeyCountpairs of(name string index, value)). Each key is 2 ints. - Class info (
Q_CLASSINFO): 2 ints each, (key, value) string indices.
Strings
Qt 6 up to 6.8: stringdata points to a table of (offset, size) pairs followed by the NUL-terminated string bytes (moc emits qt_meta_stringdata_<Class> containing offsetsAndSizes and stringdata0). Qt 6.9+ moc output (revision 13) uses QtMocHelpers::StringRefStorage and qt_create_metaobjectdata<qt_meta_tag_<Class>_t>() templates with a relocating metaTypes array. Qt 5 uses an array of QByteArrayData headers. In every version the class name is string 0 and qt_metacast(const char*) compares its argument with it (and with each interface IID) before delegating to the parent class.
Recovery recipe
- Find
staticMetaObjectobjects: scan.data/.rdatafor 7 pointer-sized fields whose second and third point into read-only data and whose third points to an int array with a plausiblerevision(7-13) and the self-consistent offsets above. - Walk
superdatato rebuild the hierarchy; read string 0 for the class name. - Dump methods (signature = name + parameter types), properties (type and flags) and enums; connect signals to slots by name (they are called through
QObject::connect(sender, &Class::signal, receiver, &Class::slot)or the olderSIGNAL()/SLOT()string macros, where the string starts with 1 for a slot, 2 for a signal and 0 for a method). qt_static_metacall(the function pointer) is a switch over(call, index)that dispatches each method id to the real code, so the nth method record maps to the nth case: this names the implementation of every slot in a stripped binary.qt_metacallforwards_idto the parent first, then toqt_static_metacall.
Evidence & references
- qtbase commit 7faa468198d1d631452db7e5cbfab203cecbdee2:
src/corelib/kernel/qmetaobject_p.hlines 112-127 (header),qmetaobject.hlines 240-248, 319-326, 409-415, 442-447 (record layouts),kernel/qobjectdefs.hlines 614-623 (QMetaObject::Data),kernel/qtmocconstants.hlines 28-79 (revisions and flags),src/tools/moc/generator.cpplines 270-505 (generated code),src/tools/moc/outputrevision.h. - Unverified: exact Qt 5 struct layouts and the exact string-table layout of each Qt 6 minor version; check with a test binary.