[lld] [WebAssembly] Generate a call to __wasm_apply_global_tls_relocs in __wasm_init_memory (PR #149832)

via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 21 23:53:47 PDT 2025


https://github.com/Arshia001 updated https://github.com/llvm/llvm-project/pull/149832

>From 738d8b1482f83e1404dede15e1d4e7a9b134d673 Mon Sep 17 00:00:00 2001
From: Arshia Ghafoori <arshia001 at live.com>
Date: Mon, 21 Jul 2025 12:53:32 +0000
Subject: [PATCH 1/2] Generate a call to __wasm_apply_global_tls_relocs in
 __wasm_init_memory

---
 lld/wasm/Writer.cpp | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/lld/wasm/Writer.cpp b/lld/wasm/Writer.cpp
index b704677d36c93..3cd6a73fb1a31 100644
--- a/lld/wasm/Writer.cpp
+++ b/lld/wasm/Writer.cpp
@@ -1366,6 +1366,15 @@ void Writer::createInitMemoryFunction() {
           writeUleb128(os, s->index, "segment index immediate");
           writeU8(os, 0, "memory index immediate");
         }
+
+        // After initializing the TLS segment, we also need to apply TLS
+        // relocations in the same way __wasm_init_tls does.
+        if (ctx.arg.sharedMemory && s->isTLS() &&
+            ctx.sym.applyGlobalTLSRelocs) {
+          writeU8(os, WASM_OPCODE_CALL, "CALL");
+          writeUleb128(os, ctx.sym.applyGlobalTLSRelocs->getFunctionIndex(),
+                      "function index");
+        }
       }
     }
 

>From d51762198924b3f551ce96eeee4307f0f568343d Mon Sep 17 00:00:00 2001
From: Arshia Ghafoori <arshia001 at live.com>
Date: Tue, 22 Jul 2025 10:50:01 +0400
Subject: [PATCH 2/2] * Update comment on
 `lld::wasm::Writer::createApplyGlobalTLSRelocationsFunction` * Remove
 redundant condition when generating call to `__wasm_apply_global_tls_relocs`
 in `lld::wasm::Writer::createInitMemoryFunction`

---
 lld/wasm/Writer.cpp | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/lld/wasm/Writer.cpp b/lld/wasm/Writer.cpp
index 3cd6a73fb1a31..3993fa249c13a 100644
--- a/lld/wasm/Writer.cpp
+++ b/lld/wasm/Writer.cpp
@@ -1369,8 +1369,7 @@ void Writer::createInitMemoryFunction() {
 
         // After initializing the TLS segment, we also need to apply TLS
         // relocations in the same way __wasm_init_tls does.
-        if (ctx.arg.sharedMemory && s->isTLS() &&
-            ctx.sym.applyGlobalTLSRelocs) {
+        if (s->isTLS() && ctx.sym.applyGlobalTLSRelocs) {
           writeU8(os, WASM_OPCODE_CALL, "CALL");
           writeUleb128(os, ctx.sym.applyGlobalTLSRelocs->getFunctionIndex(),
                       "function index");
@@ -1527,8 +1526,8 @@ void Writer::createApplyGlobalRelocationsFunction() {
 }
 
 // Similar to createApplyGlobalRelocationsFunction but for
-// TLS symbols.  This cannot be run during the start function
-// but must be delayed until __wasm_init_tls is called.
+// TLS symbols. Can only be called after __tls_base is
+// initialized.
 void Writer::createApplyGlobalTLSRelocationsFunction() {
   // First write the body's contents to a string.
   std::string bodyContent;



More information about the llvm-commits mailing list