Skip to content

Commit d17d0dd

Browse files
committed
Hot-patch __ref_* variables should be placed in .rdata, not .data
1 parent 33465bb commit d17d0dd

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

llvm/lib/CodeGen/WindowsSecureHotPatching.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,19 @@ static GlobalVariable *getOrCreateRefVariable(
369369
AddrOfOldGV, Twine("__ref_").concat(GV->getName()),
370370
nullptr, GlobalVariable::NotThreadLocal);
371371

372+
// RefGV is created with isConstant = false, but we want to place RefGV into
373+
// .rdata, not .data. It is important that the GlobalVariable be mutable
374+
// from the compiler's point of view, so that the optimizer does not remove
375+
// the global variable entirely and replace all references to it with its
376+
// initial value.
377+
//
378+
// When the Windows hot-patch loader applies a hot-patch, it maps the
379+
// pages of .rdata as read/write so that it can set each __ref_* variable
380+
// to point to the original variable in the base image. Afterward, pages in
381+
// .rdata are remapped as read-only. This protects the __ref_* variables from
382+
// being overwritten during execution.
383+
RefGV->setSection(".rdata");
384+
372385
// Create debug info for the replacement global variable.
373386
DataLayout Layout = M->getDataLayout();
374387
DIType *DebugType = DebugInfo.createPointerType(

0 commit comments

Comments
 (0)