[llvm] [ARM] Honour -mno-movt in stack protector handling (PR #109022)

Ard Biesheuvel via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 17 10:36:25 PDT 2024


https://github.com/ardbiesheuvel created https://github.com/llvm/llvm-project/pull/109022

When -mno-movt is passed to Clang, the ARM codegen correctly avoids movt/movw pairs to take the address of __stack_chk_guard in the stack protector code emitted into the function pro- and epilogues. However, the Thumb2 codegen fails to do so, and happily emits movw/movt pairs unless it is generating an ELF binary and the symbol might be in a different DSO. Let's incorporate a check for useMovt() in the logic here, so movt/movw are never emitted when -mno-movt is specified.

Suggestions welcome for how/where to add a test case for this.

>From 96bcc472b2b9694aaf352e5cbed8f6a1c27e14f4 Mon Sep 17 00:00:00 2001
From: Ard Biesheuvel <ardb at kernel.org>
Date: Tue, 17 Sep 2024 19:26:04 +0200
Subject: [PATCH] [ARM] Honour -mno-movt in stack protector handling

When -mno-movt is passed to Clang, the ARM codegen correctly avoids
movt/movw pairs to take the address of __stack_chk_guard in the stack
protector code emitted into the function pro- and epilogues. However,
the Thumb2 codegen fails to do so, and happily emits movw/movt pairs
unless it is generating an ELF binary and the symbol might be in a
different DSO. Let's incorporate a check for useMovt() in the logic
here, so movt/movw are never emitted when -mno-movt is specified.

Signed-off-by: Ard Biesheuvel <ardb at kernel.org>
---
 llvm/lib/Target/ARM/Thumb2InstrInfo.cpp | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/Target/ARM/Thumb2InstrInfo.cpp b/llvm/lib/Target/ARM/Thumb2InstrInfo.cpp
index d1e07b6703a5e6..ce6bc0681a5028 100644
--- a/llvm/lib/Target/ARM/Thumb2InstrInfo.cpp
+++ b/llvm/lib/Target/ARM/Thumb2InstrInfo.cpp
@@ -264,7 +264,8 @@ void Thumb2InstrInfo::expandLoadStackGuard(
   }
 
   const auto *GV = cast<GlobalValue>((*MI->memoperands_begin())->getValue());
-  if (MF.getSubtarget<ARMSubtarget>().isTargetELF() && !GV->isDSOLocal())
+  const ARMSubtarget &Subtarget = MF.getSubtarget<ARMSubtarget>();
+  if ((Subtarget.isTargetELF() && !GV->isDSOLocal()) || !Subtarget.useMovt())
     expandLoadStackGuardBase(MI, ARM::t2LDRLIT_ga_pcrel, ARM::t2LDRi12);
   else if (MF.getTarget().isPositionIndependent())
     expandLoadStackGuardBase(MI, ARM::t2MOV_ga_pcrel, ARM::t2LDRi12);



More information about the llvm-commits mailing list