Add unstable hotpatch flag to rustc by nebulark · Pull Request #134004 · rust-lang/rust (original) (raw)
This PR adds an unstable hotpatch flag, which ensures the first instruction of a function being two bytes long, otherwise inserting a nop. This is needed for code hotpatching (such as Live++ or Recode) tools to work, so they can provide very quick iteration cycles for development.
This uses a LLVM feature that is only implemented on x86/x86_64. On aarch64 this is not needed as there are no 1 byte instructions.
Hotpatching also requires a linker argument (e.g. functionpadmin on link.exe/lld), which ensures a minimum padding between functions. This can not implemented in rustc as this is linker specific. However, we do mark functions to be hotpatchable when using the hotpatch flag, as the link.exe/lld need it for their functionpadmin flag to work.
Typical usage with cargo for hotpatching the would be "RUSTFLAGS=-Z hotpatch -C link-arg=-functionpadmin". This currently only works on Windows due to linker support. But there should be nothing blocking other linkers to implement a flag similar to functionpadmin.
There is a similar flag "patchable-function-entry". The main difference is that hotpatch + functionpadmin, should leave functions that are already hotpatchable, an extremely common case, untouched. This reduces bloat, but requires linker support.
This is a first step to establish a proof of concept for rust-lang/compiler-team#745. The follow up step would be to add support for lld (or another linker) so this also works on linux.