Recovering generated functions¶
Compiler-generated functions are not hand-written bodies. GCC emits them from
the globals and class declarations in a translation unit, so the way to recover
one is to write the declarations it implies and then delete its INCLUDE_ASM
line. tools/build_sources.py regenerates the body and matches it by content.
There are four families. Each has a fixed recipe.
__static_initialization_and_destruction_0 + _GLOBAL__I_/_GLOBAL__D_¶
GCC emits one initializer per translation unit covering every global that needs a constructor, in source order, plus a constructor key and a destructor key named after the first such symbol in the unit.
Recipe:
-
Find which globals the reference initializer touches. The m2c draft of the
__static_initialization_and_destruction_0function names them directly: -
Define those globals in the .cpp in the same order the initializer stores them. Each must have the exact type its constructor signature carries.
-
Delete the
INCLUDE_ASMlines for the initializer and both keys. -
python3 tools/check_unit.py <unit>— all three should come back exact.
The _GLOBAL__I_ key takes the first external symbol the compiler emits from
the unit, which can be a function definition rather than a global. If the
key comes out as _GLOBAL__I_<wrong name>, move the expected first symbol
earlier in the file.
A global whose type has a trivial default constructor emits no initializer.
If check_unit reports the initializer as missing, the type is too simple:
add the constructor the reference's stores imply (see TStructEvent in
src/common/basic/zstructevent.h — it needed m_iSize = 0,
m_bEnabled = true, m_AbortEventLoop = false).
__tcf_N (function-local static destructor)¶
These belong to a function-local static with a destructor, in the function
that owns it. Recover the owning function and the __tcf_N follows. If the
numbering is wrong (__tcf_0 where the reference has __tcf_1), an earlier
function in the unit has a static that must be declared first.
D1/D0/D2 destructors¶
Three variants exist per class: D1 (complete object), D0 (deleting), D2
(base object). Which ones the reference defines tells you where the body lives:
- All three out of line: write
virtual ~Class();in the header and define it in the .cpp. - Only
D1/D0, noD2: the destructor is inline in the header and was inlined into every derived destructor. Putvirtual ~Class() {}in the header and define nothing in the .cpp.
Use the original vtable order when declaring virtual methods. python3 tools/vtable_evidence.py <Class> lists the slots.
Adjusting thunks¶
A symbol beginning with _ZThn adjusts the incoming this pointer before entering a virtual override. Recover the base classes in their original order and declare the overriding method with the original signature. GCC emits the adjustment for calls through the secondary base vtable.
Verifying¶
Always finish with:
The full build is the real gate: it proves no other unit lost a match.