[clang] [llvm] [LFI][X86] Add X86 LFI target and system instruction rewrites (PR #189569)

Fangrui Song via cfe-commits cfe-commits at lists.llvm.org
Sun Jun 28 11:21:02 PDT 2026


================
@@ -20344,6 +20344,18 @@ X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
 
   if (Subtarget.isTargetELF()) {
     TLSModel::Model model = DAG.getTarget().getTLSModel(GV);
+    // LFI does not support dlopen, so all TLS is statically resolved. Upgrade
+    // dynamic models to avoid TLS indirect calls that cause problems with
+    // linker relaxation.
+    if (Subtarget.isLFI()) {
+      bool IsPIE = GV->getParent()->getPIELevel() != PIELevel::Default;
+      bool IsSharedLibrary =
+          DAG.getTarget().getRelocationModel() == Reloc::PIC_ && !IsPIE;
+      if (model == TLSModel::GeneralDynamic)
+        model = TLSModel::InitialExec;
+      else if (model == TLSModel::LocalDynamic)
+        model = IsSharedLibrary ? TLSModel::InitialExec : TLSModel::LocalExec;
----------------
MaskRay wrote:

This is ok. -fPIC suggests that the output can be dlopened. If LFI doesn't support dlopen, you can optimize GeneralDynamic to InitialExec.  I prefer to gate the whole LFI-specific logic with a single `if (isLFI())`.

https://github.com/llvm/llvm-project/pull/189569


More information about the cfe-commits mailing list