[llvm-branch-commits] ELF: Do not rewrite IFUNC relocations to point to PLT if no GOT/PLT needed. (PR #133531)

Peter Collingbourne via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Tue Apr 8 19:47:42 PDT 2025


pcc wrote:

This patch has a few problems
- The check in `processAux` was moved to the wrong place (it will never get hit because this is after we deal with all symbols not defined locally). This patch moves it to approximately the right place but will need some logic changes to `isStaticLinkTimeConstant` to recognize ifuncs as not being static link time constants:
```
diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp
index 1439fd859f51..969d71461cb1 100644
--- a/lld/ELF/Relocations.cpp
+++ b/lld/ELF/Relocations.cpp
@@ -1127,6 +1127,8 @@ void RelocationScanner::processAux(RelExpr expr, RelType type, uint64_t offset,
   // handling of GOT-generating relocations.
   if (isStaticLinkTimeConstant(expr, type, sym, offset) ||
       (!ctx.arg.isPic && sym.isUndefWeak())) {
+    if (LLVM_UNLIKELY(isIfunc && !needsGot(expr) && !needsPlt(expr)))
+      sym.setFlags(HAS_DIRECT_RELOC);
     sec->addReloc({expr, type, offset, addend, &sym});
     return;
   }
@@ -1194,9 +1196,6 @@ void RelocationScanner::processAux(RelExpr expr, RelType type, uint64_t offset,
     }
   }
 
-  if (LLVM_UNLIKELY(isIfunc && !needsGot(expr) && !needsPlt(expr)))
-    sym.setFlags(HAS_DIRECT_RELOC);
-
   // When producing an executable, we can perform copy relocations (for
   // STT_OBJECT) and canonical PLT (for STT_FUNC) if sym is defined by a DSO.
   // Copy relocations/canonical PLT entries are unsupported for
```
- Need to adjust how IRELATIVE relocations get emitted. Currently we emit relocations with the wrong type (assuming they will be demoted) and fix up the type in `RelocationBaseSection::partitionRels` if they turn out to not be demoted. But this isn't good enough for PDEs which need them to be static relocations. Here's what I think we should do instead:
  - Always emit ifunc relocations as IRELATIVE during `processAux`
  - In `handleNonPreemptibleIfunc`, when we demote an ifunc to an iplt entry, loop over all irelatives pointing to the symbol and rewrite them as either relative (for PIE/shared) or static relocations (for PDE). It's fine to loop over the irelatives every time because they are rare.

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


More information about the llvm-branch-commits mailing list