[lld] [lld][LoongArch] Support TLSDESC GD/LD to IE/LE (PR #123715)

Zhaoxin Yang via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 17 20:27:28 PDT 2025


================
@@ -1453,7 +1459,17 @@ unsigned RelocationScanner::handleTlsRelocation(RelExpr expr, RelType type,
     // label, so TLSDESC=>IE will be categorized as R_RELAX_TLS_GD_TO_LE. We fix
     // the categorization in RISCV::relocateAllosec->
     if (sym.isPreemptible) {
-      sym.setFlags(NEEDS_TLSGD_TO_IE);
+      // In LoongArch, TLSDESC code sequences share relocations
+      // R_LARCH_TLS_DESC_PC_HI20 and R_LARCH_TLS_DESC_PC_LO12 in
+      // normal/medium/extreme code model. Since the extreme code model cannot
+      // be optimized to IE/LE, the flag NEEDS_TLSGD_TO_IE added previously
+      // needs to be cleared.
+      // In extreme code model, R_LARCH_TLS_DESC64_LO20 and
+      // R_LARCH_TLS_DESC64_HI12 will set NEEDS_TLSDESC flag.
+      if (ctx.arg.emachine == EM_LOONGARCH && sym.hasFlag(NEEDS_TLSDESC))
+        sym.clearFlags(NEEDS_TLSGD_TO_IE);
----------------
ylzsx wrote:

Thank you for your review. Your concern is valid. If the symbol `sym` appears simultaneously in two code models (extreme and medium),  the `NEEDS_TLSGD_TO_IE` flag might be cleared in the extreme model, but the GOT entries generated by this flag could still be needed in the medium model. This will result in an error.

I will remove the `clearFlags` code. Even though this may increase the dynamic linker’s runtime when all .o files use the extreme model, it is an acceptable trade-off since the extreme model is used in only a few applications on LoongArch64.

Alternatively, do you have a better modification suggestion? I look forward to your reply.

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


More information about the llvm-commits mailing list