Skip to content

Hot-patch __ref_* variables should be placed in .rdata, not .data #151008

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 28, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions llvm/lib/CodeGen/WindowsSecureHotPatching.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,19 @@ static GlobalVariable *getOrCreateRefVariable(
AddrOfOldGV, Twine("__ref_").concat(GV->getName()),
nullptr, GlobalVariable::NotThreadLocal);

// RefGV is created with isConstant = false, but we want to place RefGV into
// .rdata, not .data. It is important that the GlobalVariable be mutable
// from the compiler's point of view, so that the optimizer does not remove
// the global variable entirely and replace all references to it with its
// initial value.
//
// When the Windows hot-patch loader applies a hot-patch, it maps the
// pages of .rdata as read/write so that it can set each __ref_* variable
// to point to the original variable in the base image. Afterward, pages in
// .rdata are remapped as read-only. This protects the __ref_* variables from
// being overwritten during execution.
RefGV->setSection(".rdata");

// Create debug info for the replacement global variable.
DataLayout Layout = M->getDataLayout();
DIType *DebugType = DebugInfo.createPointerType(
Expand Down