IT lexicon › Programming › Hotpatching

Hotpatching

Programming På svenska → Updated: 2026-11-07

Replacing a function in a running program by writing a jump over its first instruction — something the compiler has to have prepared in the prologue.

The hard part is atomicity. A thread may be executing the very bytes being overwritten, and a five-byte jump cannot be written in one go. Windows solved it from XP SP2 onwards: every system function opens with the do-nothing two-byte instruction mov edi, edi, and the linker leaves five bytes of padding in front of the function. The patch first writes a long jump into the padding, then, with a single atomic store, a short jump backwards over mov edi, edi. MSVC's /hotpatch flag and the linker's /FUNCTIONPADMIN give your own code the same layout.

GCC offers the ms_hook_prologue attribute for Wine's benefit, and the more general -fpatchable-function-entry, which inserts any number of nops. The Linux kernel uses them for ftrace and for patching security holes without a reboot. The instructions cost next to nothing and are almost never used, which is exactly what you want from an emergency exit.

← Back to the lexicon