Skip to content

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:

  1. Find which globals the reference initializer touches. The m2c draft of the __static_initialization_and_destruction_0 function names them directly:

    python3 tools/m2c_draft.py <initializer-id> | grep -oE '_ZN[0-9A-Za-z_]+E' | sort -u
    
  2. Define those globals in the .cpp in the same order the initializer stores them. Each must have the exact type its constructor signature carries.

  3. Delete the INCLUDE_ASM lines for the initializer and both keys.

  4. 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, no D2: the destructor is inline in the header and was inlined into every derived destructor. Put virtual ~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:

python3 tools/check_unit.py <unit>          # this unit only, ~2s
python3 tools/build_sources.py --jobs 12

The full build is the real gate: it proves no other unit lost a match.