Skip to content

[LLD][COFF] Replace ARM64EC TLS directory chunks with native chunks when available - #212845

Merged
cjacek merged 1 commit into
llvm:mainfrom
cjacek:arm64x-tls
Aug 2, 2026
Merged

cjacek merged 1 commit into
llvm:mainfrom
cjacek:arm64x-tls

Conversation

@cjacek

@cjacek cjacek commented Jul 29, 2026 •

Copy link
Copy Markdown
Contributor

On ARM64X targets, CRT provides separate TLS directory chunks, expecting the linker to sort it out. TLS directory uses _tls_start and _tls_end symbols to reference .tls section. Those symbols use section sorting to ensure that they are emitted
at the start and end of .tls section, but that's not enough when we have two separate chunks for views: only one of them can really be the first one. Following MSVC, merge those chunks instead so that both symbol tables point to the same chunk.

Additionally apply the same logic to _tls_used and _tls_index. This allows entire TLS directory to be shared between EC and native views. To achieve that, CRT additionally needs to mark each TLS callback with -arm64xsameaddress. This matches how MSVC linker and libraries work, but it requires EC and native views to use the same set of TLS callbacks. We may emit separate TLS directories in the future to make it more robust.

@llvmorg-github-actions

llvmorg-github-actions Bot commented Jul 29, 2026 •

Copy link
Copy Markdown

@llvm/pr-subscribers-lld

@llvm/pr-subscribers-lld-coff

Author: Jacek Caban (cjacek)

Changes

Following MSVC, we don't emit separate TLS directories for EC and native views and instead use native one for both. CRT uses -arm64xsameaddress feature to ensure that it works well for callbacks. Additionally, linker needs to replace EC symbols with native ones to ensure that object files reference them as expected.

Even through EC symbols end up being unused, they still need to be present and defined, so the replacement happens after symbols are resolved. MSVC seems to have some dedicated logic to skip chunks defining EC _tls_used and _tls_index symbols. The logic seems incomplete; for example, if the same chunks defines more symbols, those symbols become equal to __ImageBase (0 RVA). Instead of trying to replicate that, I made sure that GC has a chance to clean up such sections.


Full diff: https://github.com/llvm/llvm-project/pull/212845.diff

2 Files Affected:

  • (modified) lld/COFF/Driver.cpp (+17-4)
  • (added) lld/test/COFF/arm64x-tls.s (+104)
diff --git a/lld/COFF/Driver.cpp b/lld/COFF/Driver.cpp
index 4eedefbf2ba5e..dc3fd46323032 100644
--- a/lld/COFF/Driver.cpp
+++ b/lld/COFF/Driver.cpp
@@ -2805,10 +2805,6 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
   ltoCompilationDone = true;
   ctx.forEachSymtab([](SymbolTable &symtab) { symtab.compileBitcodeFiles(); });
 
-  if (Defined *d =
-          dyn_cast_or_null<Defined>(ctx.symtab.findUnderscore("_tls_used")))
-    config->gcroot.push_back(d);
-
   // If -thinlto-index-only is given, we should create only "index
   // files" and not object files. Index file creation is already done
   // in addCombinedLTOObject, so we are done if that's the case.
@@ -2837,6 +2833,23 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
   if (errorCount())
     return;
 
+  if (ctx.hybridSymtab) {
+    // On ARM64X, use native _tls_used and _tls_index for both views.
+    auto maybeReplaceWithNative = [&](StringRef name) {
+      Symbol *nativeSym = ctx.hybridSymtab->findUnderscore(name);
+      if (!nativeSym || !nativeSym->getDefined())
+        return;
+      if (Symbol *ecSym = ctx.symtab.findUnderscore(name))
+        ecSym->replaceKeepingName(nativeSym, sizeof(SymbolUnion));
+    };
+    maybeReplaceWithNative("_tls_used");
+    maybeReplaceWithNative("_tls_index");
+  }
+
+  if (Defined *d =
+          dyn_cast_or_null<Defined>(ctx.symtab.findUnderscore("_tls_used")))
+    config->gcroot.push_back(d);
+
   ctx.forEachActiveSymtab([](SymbolTable &symtab) {
     symtab.initializeECThunks();
     symtab.initializeLoadConfig();
diff --git a/lld/test/COFF/arm64x-tls.s b/lld/test/COFF/arm64x-tls.s
new file mode 100644
index 0000000000000..21ff0ab26d2af
--- /dev/null
+++ b/lld/test/COFF/arm64x-tls.s
@@ -0,0 +1,104 @@
+// REQUIRES: aarch64
+// RUN: split-file %s %t.dir && cd %t.dir
+
+// RUN: llvm-mc -filetype=obj -triple=aarch64-windows aarch64.s -o aarch64.obj
+// RUN: llvm-mc -filetype=obj -triple=arm64ec-windows arm64ec.s -o arm64ec.obj
+// RUN: llvm-mc -filetype=obj -triple=aarch64-windows tls-aarch64.s -o tls-aarch64.obj
+// RUN: llvm-mc -filetype=obj -triple=arm64ec-windows tls-arm64ec.s -o tls-arm64ec.obj
+// RUN: llvm-mc -filetype=obj -triple=arm64ec-windows %S/Inputs/loadconfig-arm64ec.s -o loadconfig-arm64ec.obj
+// RUN: llvm-mc -filetype=obj -triple=aarch64-windows %S/Inputs/loadconfig-arm64.s -o loadconfig-arm64.obj
+
+// RUN: rm -f tls.lib
+// RUN: llvm-lib -machine:arm64x -out:tls.lib tls-aarch64.obj tls-arm64ec.obj loadconfig-arm64ec.obj loadconfig-arm64.obj
+// RUN: lld-link -machine:arm64x -dll -noentry aarch64.obj arm64ec.obj tls.lib -out:out.dll
+
+// Check that we're using native _tls_index and _tls_used for both views.
+
+// RUN: llvm-readobj --coff-tls-directory --hex-dump=.ec --hex-dump=.a64 out.dll | FileCheck %s
+// CHECK:      Format: COFF-ARM64X
+// CHECK-NEXT: Arch: aarch64
+// CHECK-NEXT: AddressSize: 64bit
+// CHECK-EMPTY:
+// CHECK-NEXT: Hex dump of section '.a64':
+// CHECK-NEXT: 0x180004000 00500000 04500000
+// CHECK-EMPTY:
+// CHECK-NEXT: Hex dump of section '.ec':
+// CHECK-NEXT: 0x180006000 00500000 04500000
+// CHECK-NEXT: TLSDirectory {
+// CHECK-NEXT:   StartAddressOfRawData: 0x0
+// CHECK-NEXT:   EndAddressOfRawData: 0x0
+// CHECK-NEXT:   AddressOfIndex: 0x180005000
+// CHECK-NEXT:   AddressOfCallBacks: 0x0
+// CHECK-NEXT:   SizeOfZeroFill: 0x0
+// CHECK-NEXT:   Characteristics [ (0x0)
+// CHECK-NEXT:   ]
+// CHECK-NEXT: }
+// CHECK-NEXT: HybridObject {
+// CHECK-NEXT:   Format: COFF-ARM64EC
+// CHECK-NEXT:   Arch: aarch64
+// CHECK-NEXT:   AddressSize: 64bit
+// CHECK-EMPTY:
+// CHECK-NEXT:   Hex dump of section '.a64':
+// CHECK-NEXT:   0x180004000 00500000 04500000
+// CHECK-EMPTY:
+// CHECK-NEXT:   Hex dump of section '.ec':
+// CHECK-NEXT:   0x180006000 00500000 04500000
+// CHECK-NEXT:   TLSDirectory {
+// CHECK-NEXT:     StartAddressOfRawData: 0x0
+// CHECK-NEXT:     EndAddressOfRawData: 0x0
+// CHECK-NEXT:     AddressOfIndex: 0x180005000
+// CHECK-NEXT:     AddressOfCallBacks: 0x0
+// CHECK-NEXT:     SizeOfZeroFill: 0x0
+// CHECK-NEXT:     Characteristics [ (0x0)
+// CHECK-NEXT:     ]
+// CHECK-NEXT:   }
+// CHECK-NEXT: }
+
+// Check that EC _tls_index and _tls_used pull the object file, but their chunks are collected by GC.
+// RUN: llvm-readobj --sections out.dll | FileCheck --check-prefix=DEFEC %s
+// DEFEC-NOT: defec
+// DEFEC:     .test
+// DEFEC-NOT: defec
+
+#--- tls-aarch64.s
+        .section .defa64,"dr",discard,_tls_index
+        .globl _tls_index
+_tls_index:
+        .long 0
+
+        .section .defa64,"dr",discard,_tls_used
+        .globl _tls_used
+_tls_used:
+        .xword 0
+        .xword 0
+        .xword _tls_index
+        .xword 0
+        .xword 0
+
+#--- aarch64.s
+        .section .a64,"dr"
+        .rva _tls_index
+        .rva _tls_used
+
+#--- tls-arm64ec.s
+        .section .defec,"dr",discard,_tls_index
+        .globl _tls_index
+_tls_index:
+        .long 0
+
+        .section .defec,"dr",discard,_tls_used
+        .globl _tls_used
+_tls_used:
+        .xword 1
+        .xword 1
+        .xword _tls_index
+        .xword 1
+        .xword 1
+
+        .section .test
+        .long 0
+
+#--- arm64ec.s
+        .section .ec,"dr"
+        .rva _tls_index
+        .rva _tls_used

@llvmorg-github-actions

Copy link
Copy Markdown

@llvm/pr-subscribers-platform-windows

Author: Jacek Caban (cjacek)

Changes

Following MSVC, we don't emit separate TLS directories for EC and native views and instead use native one for both. CRT uses -arm64xsameaddress feature to ensure that it works well for callbacks. Additionally, linker needs to replace EC symbols with native ones to ensure that object files reference them as expected.

Even through EC symbols end up being unused, they still need to be present and defined, so the replacement happens after symbols are resolved. MSVC seems to have some dedicated logic to skip chunks defining EC _tls_used and _tls_index symbols. The logic seems incomplete; for example, if the same chunks defines more symbols, those symbols become equal to __ImageBase (0 RVA). Instead of trying to replicate that, I made sure that GC has a chance to clean up such sections.


Full diff: https://github.com/llvm/llvm-project/pull/212845.diff

2 Files Affected:

  • (modified) lld/COFF/Driver.cpp (+17-4)
  • (added) lld/test/COFF/arm64x-tls.s (+104)
diff --git a/lld/COFF/Driver.cpp b/lld/COFF/Driver.cpp
index 4eedefbf2ba5e..dc3fd46323032 100644
--- a/lld/COFF/Driver.cpp
+++ b/lld/COFF/Driver.cpp
@@ -2805,10 +2805,6 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
   ltoCompilationDone = true;
   ctx.forEachSymtab([](SymbolTable &symtab) { symtab.compileBitcodeFiles(); });
 
-  if (Defined *d =
-          dyn_cast_or_null<Defined>(ctx.symtab.findUnderscore("_tls_used")))
-    config->gcroot.push_back(d);
-
   // If -thinlto-index-only is given, we should create only "index
   // files" and not object files. Index file creation is already done
   // in addCombinedLTOObject, so we are done if that's the case.
@@ -2837,6 +2833,23 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
   if (errorCount())
     return;
 
+  if (ctx.hybridSymtab) {
+    // On ARM64X, use native _tls_used and _tls_index for both views.
+    auto maybeReplaceWithNative = [&](StringRef name) {
+      Symbol *nativeSym = ctx.hybridSymtab->findUnderscore(name);
+      if (!nativeSym || !nativeSym->getDefined())
+        return;
+      if (Symbol *ecSym = ctx.symtab.findUnderscore(name))
+        ecSym->replaceKeepingName(nativeSym, sizeof(SymbolUnion));
+    };
+    maybeReplaceWithNative("_tls_used");
+    maybeReplaceWithNative("_tls_index");
+  }
+
+  if (Defined *d =
+          dyn_cast_or_null<Defined>(ctx.symtab.findUnderscore("_tls_used")))
+    config->gcroot.push_back(d);
+
   ctx.forEachActiveSymtab([](SymbolTable &symtab) {
     symtab.initializeECThunks();
     symtab.initializeLoadConfig();
diff --git a/lld/test/COFF/arm64x-tls.s b/lld/test/COFF/arm64x-tls.s
new file mode 100644
index 0000000000000..21ff0ab26d2af
--- /dev/null
+++ b/lld/test/COFF/arm64x-tls.s
@@ -0,0 +1,104 @@
+// REQUIRES: aarch64
+// RUN: split-file %s %t.dir && cd %t.dir
+
+// RUN: llvm-mc -filetype=obj -triple=aarch64-windows aarch64.s -o aarch64.obj
+// RUN: llvm-mc -filetype=obj -triple=arm64ec-windows arm64ec.s -o arm64ec.obj
+// RUN: llvm-mc -filetype=obj -triple=aarch64-windows tls-aarch64.s -o tls-aarch64.obj
+// RUN: llvm-mc -filetype=obj -triple=arm64ec-windows tls-arm64ec.s -o tls-arm64ec.obj
+// RUN: llvm-mc -filetype=obj -triple=arm64ec-windows %S/Inputs/loadconfig-arm64ec.s -o loadconfig-arm64ec.obj
+// RUN: llvm-mc -filetype=obj -triple=aarch64-windows %S/Inputs/loadconfig-arm64.s -o loadconfig-arm64.obj
+
+// RUN: rm -f tls.lib
+// RUN: llvm-lib -machine:arm64x -out:tls.lib tls-aarch64.obj tls-arm64ec.obj loadconfig-arm64ec.obj loadconfig-arm64.obj
+// RUN: lld-link -machine:arm64x -dll -noentry aarch64.obj arm64ec.obj tls.lib -out:out.dll
+
+// Check that we're using native _tls_index and _tls_used for both views.
+
+// RUN: llvm-readobj --coff-tls-directory --hex-dump=.ec --hex-dump=.a64 out.dll | FileCheck %s
+// CHECK:      Format: COFF-ARM64X
+// CHECK-NEXT: Arch: aarch64
+// CHECK-NEXT: AddressSize: 64bit
+// CHECK-EMPTY:
+// CHECK-NEXT: Hex dump of section '.a64':
+// CHECK-NEXT: 0x180004000 00500000 04500000
+// CHECK-EMPTY:
+// CHECK-NEXT: Hex dump of section '.ec':
+// CHECK-NEXT: 0x180006000 00500000 04500000
+// CHECK-NEXT: TLSDirectory {
+// CHECK-NEXT:   StartAddressOfRawData: 0x0
+// CHECK-NEXT:   EndAddressOfRawData: 0x0
+// CHECK-NEXT:   AddressOfIndex: 0x180005000
+// CHECK-NEXT:   AddressOfCallBacks: 0x0
+// CHECK-NEXT:   SizeOfZeroFill: 0x0
+// CHECK-NEXT:   Characteristics [ (0x0)
+// CHECK-NEXT:   ]
+// CHECK-NEXT: }
+// CHECK-NEXT: HybridObject {
+// CHECK-NEXT:   Format: COFF-ARM64EC
+// CHECK-NEXT:   Arch: aarch64
+// CHECK-NEXT:   AddressSize: 64bit
+// CHECK-EMPTY:
+// CHECK-NEXT:   Hex dump of section '.a64':
+// CHECK-NEXT:   0x180004000 00500000 04500000
+// CHECK-EMPTY:
+// CHECK-NEXT:   Hex dump of section '.ec':
+// CHECK-NEXT:   0x180006000 00500000 04500000
+// CHECK-NEXT:   TLSDirectory {
+// CHECK-NEXT:     StartAddressOfRawData: 0x0
+// CHECK-NEXT:     EndAddressOfRawData: 0x0
+// CHECK-NEXT:     AddressOfIndex: 0x180005000
+// CHECK-NEXT:     AddressOfCallBacks: 0x0
+// CHECK-NEXT:     SizeOfZeroFill: 0x0
+// CHECK-NEXT:     Characteristics [ (0x0)
+// CHECK-NEXT:     ]
+// CHECK-NEXT:   }
+// CHECK-NEXT: }
+
+// Check that EC _tls_index and _tls_used pull the object file, but their chunks are collected by GC.
+// RUN: llvm-readobj --sections out.dll | FileCheck --check-prefix=DEFEC %s
+// DEFEC-NOT: defec
+// DEFEC:     .test
+// DEFEC-NOT: defec
+
+#--- tls-aarch64.s
+        .section .defa64,"dr",discard,_tls_index
+        .globl _tls_index
+_tls_index:
+        .long 0
+
+        .section .defa64,"dr",discard,_tls_used
+        .globl _tls_used
+_tls_used:
+        .xword 0
+        .xword 0
+        .xword _tls_index
+        .xword 0
+        .xword 0
+
+#--- aarch64.s
+        .section .a64,"dr"
+        .rva _tls_index
+        .rva _tls_used
+
+#--- tls-arm64ec.s
+        .section .defec,"dr",discard,_tls_index
+        .globl _tls_index
+_tls_index:
+        .long 0
+
+        .section .defec,"dr",discard,_tls_used
+        .globl _tls_used
+_tls_used:
+        .xword 1
+        .xword 1
+        .xword _tls_index
+        .xword 1
+        .xword 1
+
+        .section .test
+        .long 0
+
+#--- arm64ec.s
+        .section .ec,"dr"
+        .rva _tls_index
+        .rva _tls_used

@mstorsjo

Copy link
Copy Markdown
Member

It would be good to add (included in the commit message), that this feels roundabout, and it would seem more straightforward to just redirect the TLS directory entry to the EC version with a dynamic relocation, just like is done for other directories - this wouldn't need any trickery about replacing symbols and -arm64xsameaddress to make the callbacks work. (This approach requires any TLS callback in user code to be annotated with -arm64xsameaddress in the same way.) And that we're not aware of any drawbacks to doing it with a dynamic relocation (and it would also work with the MSVC libraries), but we're doing things this way to match the MS link.exe behaviour.

Out of curiosity - did you try fixing this with a dynamic relocation, or would you be open to trying doing that? It would be nice to have as reference, even if I agree that we probably should go with this solution.

@cjacek

cjacek commented Jul 29, 2026 •

Copy link
Copy Markdown
Contributor Author

FWIW, this is not just counter-intuitive, but I feel like MSVC approach is just fragile and not entirely correct. The -arm64xsameaddress feature depends on the fact that arm64ec object files have a chance to flag functions as such. But if native code is different than EC code and uses callbacks, while EC does not, then that's not the case. If you do that and load the image as EC, then it will try to execute native-only callback and crash. I guess that's not very common as usually both views contain similar enough code, but it still does not feel right. This, combined with the general complications related to the approach, really make me wonder why is it like that.

Yes, I have an alternative solution, see cjacek@f0897c7. As far as I tested, that seems to work fine. In fact, I like that solution more, but I can't guarantee that there are subtle problems, either with Windows loader or MSVC libs, that I'm missing in my testing (and led MSVC devs to use the other approach)...

@mstorsjo

Copy link
Copy Markdown
Member

FWIW, this is not just counter-intuitive, but I feel like MSVC approach is just fragile and not entirely correct. The -arm64xsameaddress feature depends on the fact that arm64ec object files have a chance to flag functions as such. But if native code is different than EC code and uses callbacks, while EC does not, then that's not the case. If you do that and load the image as EC, then it will try to execute native-only callback and crash. I guess that's not very common as usually both views contain similar enough code, but it still does not feel right. This, combined with the general complications related to the approach, really make me wonder why is it like that.

Those are indeed really good points. It would be great if someone from MS would be able to comment on this - although I'm not sure how much they've been willing to tell about the ARM64X implementation details though. CC @pmsjt @dpaoliello

Yes, I have an alternative solution, see cjacek@f0897c7. As far as I tested, that seems to work fine. In fact, I like that solution more, but I can't guarantee that there are subtle problems, either with Windows loader or MSVC libs, that I'm missing in my testing (and led MSVC devs to use the other approach)...

Indeed, I also like that approach much more. But I also want to avoid subtle bugs due to handling this in a different way...

If we practically can't figure out a case when the simpler and more robust solution wouldn't work (even for the MSVC libraries), and MS can't/won't tell, I'm actually somewhat leaning towards going with the simpler and robuster solution. Worst case, if someone notices an issue with it, we can probably (begrudgingly) switch at that point. (The problem unfortunately is that if there is an issue in this area, debugging anything of it is really nontrivial in most cases.)

@cjacek

cjacek commented Jul 30, 2026

Copy link
Copy Markdown
Contributor Author

I noticed one more problem with _tls_start and _tls_end symbols. Those really need to be merged, there may be only one real start and end. This also explains the merging behavior I mentioned earlier: for those we really want to make sure that the unused copy is not emitted.

With that logic in place, _tls_used and _tls_index may be just more of the same (just two more lines with this implementation), so I guess simplicity may be an argument for that... I would suggest starting with this PR, because we need it for _tls_start and _tls_end anyway, and follow-up with the robuster version.

@mstorsjo

Copy link
Copy Markdown
Member

I noticed one more problem with _tls_start and _tls_end symbols. Those really need to be merged, there may be only one real start and end. This also explains the merging behavior I mentioned earlier: for those we really want to make sure that the unused copy is not emitted.

I think I understand, maybe. But let's try to spell it out to see if I'm getting it right.

So the reason is that the generated code uses the section relative offset of variables, within .tls. But the loader just allocates an area of the size _tls_end - _tls_start. So if _tls_start isn't at the start of the .tls section, the offsets end up wrong.

So theoretically, instead of using section relative offset of the variables in .tls, the correct thing to emit would be the offset of the variable from _tls_start. But there's no relocation type that does that. (But if the .tls sections of the native/arm64ec views would be considered separate, so that the section relative offset is equal to the offset to _tls_start in the current view, it would work, right?)

So if the _tls_start and _tls_end variables are merged across the two views, doesn't this mean that all the actual TLS data between the two views need to be visible between the two marker symbols? But I guess that's what's happening anyway; the native _tls_end gets sorted further away after the hybrid tls data symbols in .tls$<foo>?

With that logic in place, _tls_used and _tls_index may be just more of the same (just two more lines with this implementation), so I guess simplicity may be an argument for that... I would suggest starting with this PR, because we need it for _tls_start and _tls_end anyway, and follow-up with the robuster version.

Yeah with this explanation I think I understand why we need to do it this way. (Or the alternative way, of treating the two views entirely separately, would require treating the two views .tls sections separately, with respect to section relative offsets.)

@cjacek

cjacek commented Jul 31, 2026

Copy link
Copy Markdown
Contributor Author

I could have been more specific, sorry, but you got it right.

Relocations for .tls data are relative to section, while TLS directory is filled with _tls_start/_tls_end symbols. For that to match, _tls_start must be really at the beginning of .tls section. This is usually guaranteed by section sorting: _tls_start uses a plain .tls section, data goes to .tls$ and _tls_end goes to .tls$ZZZ. That's not enough when we have two _tls_start chunks for both views, only one of them would be at the actual beginning of .tls section (and it depends on which one happens to be pulled in first).

I think that having separate TLS directories is still an improvement, but that's only about _tls_used and _tls_index. Here is a version on top of this PR: cjacek@d8b4211

I will update the description.

…hen available

On ARM64X targets, CRT provides separate TLS directory chunks, expecting the linker
to sort it out. TLS directory uses _tls_start and _tls_end symbols to reference
.tls section. Those symbols use section sorting to ensure that they are emitted
at the start and end of .tls section, but that's not enough when we have two
separate chunks for views: only one of them can really be the first one. Following
MSVC, merge those chunks instead so that both symbol tables point to the same chunk.

Additionally apply the same logic to _tls_used and _tls_index. This allows entire
TLS directory to be shared between EC and native views. To achieve that, CRT
additionally needs to mark each TLS callback with -arm64xsameaddress. This matches
how MSVC linker and libraries work, but it requires EC and native views to use
the same set of TLS callbacks. We may emit separate TLS directories in the future
to make it more robust.
@cjacek cjacek changed the title [LLD][COFF] Replace ARM64EC TLS symbols with native symbols when available [LLD][COFF] Replace ARM64EC TLS directory chunks with native chunks when available Jul 31, 2026
@pmsjt

pmsjt commented Jul 31, 2026 •

Copy link
Copy Markdown

This is a very long thread and I just came back from vacation so, for now, I'll merely reply to the side-comment that I was quoted on.

FWIW, this is not just counter-intuitive, but I feel like MSVC approach is just fragile and not entirely correct. The -arm64xsameaddress feature depends on the fact that arm64ec object files have a chance to flag functions as such. But if native code is different than EC code and uses callbacks, while EC does not, then that's not the case. If you do that and load the image as EC, then it will try to execute native-only callback and crash. I guess that's not very common as usually both views contain similar enough code, but it still does not feel right. This, combined with the general complications related to the approach, really make me wonder why is it like that.

-arm64xsameaddress does not assume or depend on EC and Classic versions of the function having the same implementation - they can contain very different code. If the linker is not able to fuse both into one, it will assign the symbol to a jump-thunk with an associated DVRT with will point at the right implementation.

This means that, even in a case where a Classic process does CreateRemoteThread (... , handle_of_an_ec_process, .... , &same_address_function, ...); (and assuming the module containing same_address_function was loaded at the same addess), the right implementation should be executed (the EC code, in this example, since the target process is EC).

@cjacek

cjacek commented Jul 31, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks. Here, I meant specifically about the same set of TLS callbacks being pulled by the linker. See the attached
test.cpp as an example. It uses TLS callbacks from Classic, but not from EC. If I build it like:

cl -c test.cpp -Fo:test.arm64.obj
cl -c test.cpp -Fo:test.arm64ec.obj -arm64EC
link -out:test.exe test.arm64.obj test.arm64ec.obj -machine:arm64x

test.exe will crash when ran in EC mode. I think that, since EC doesn't use TLS callbacks, -arm64xsameaddress directive never has a chance to mark the function as such, but its Classic variant still present in TLS directory as seen by EC loader.

If I remove entirely TLS usage from EC (remove tl variable from the test), then no TLS directory is emitted, even through it's required by Classic, so then it crashes when ran in native mode.

@mstorsjo mstorsjo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks - I think this LGTM now. Thanks for taking the time to figure it out properly! Fragile or not, this is what MS link.exe does, so this is a great first step.

@pmsjt

pmsjt commented Aug 1, 2026

Copy link
Copy Markdown

Thanks. Here, I meant specifically about the same set of TLS callbacks being pulled by the linker. See the attached test.cpp as an example. It uses TLS callbacks from Classic, but not from EC. If I build it like:

cl -c test.cpp -Fo:test.arm64.obj
cl -c test.cpp -Fo:test.arm64ec.obj -arm64EC
link -out:test.exe test.arm64.obj test.arm64ec.obj -machine:arm64x

test.exe will crash when ran in EC mode. I think that, since EC doesn't use TLS callbacks, -arm64xsameaddress directive never has a chance to mark the function as such, but its Classic variant still present in TLS directory as seen by EC loader.

If I remove entirely TLS usage from EC (remove tl variable from the test), then no TLS directory is emitted, even through it's required by Classic, so then it crashes when ran in native mode.

Thank you. I'll take a look as soon as I can - I'll aim for late next week.

@cjacek
cjacek merged commit e096d2f into llvm:main Aug 2, 2026
10 checks passed
@cjacek
cjacek deleted the arm64x-tls branch August 2, 2026 10:39
@cjacek

cjacek commented Aug 2, 2026

Copy link
Copy Markdown
Contributor Author

Thanks! I merged and created #213529 for a possible solution for discussed problems.

@cjacek cjacek added this to the LLVM 23.x Release milestone Aug 2, 2026
@github-project-automation github-project-automation Bot moved this from Needs Triage to Done in LLVM Release Status Aug 2, 2026
@github-project-automation github-project-automation Bot moved this to Needs Triage in LLVM Release Status Aug 2, 2026
@cjacek

cjacek commented Aug 2, 2026

Copy link
Copy Markdown
Contributor Author

/cherry-pick e096d2f

@llvmbot

llvmbot commented Aug 2, 2026

Copy link
Copy Markdown
Member

/pull-request #213533

Comment thread lld/COFF/Chunks.h
SectionChunk *assocChildren = nullptr;

// Used for ICF (Identical COMDAT Folding)
void replace(SectionChunk *other);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A small retrospective comment here: The comment above (about these being used for ICF) now only refers to one out of two members relating to this. I'm not sure if this is worth trying to fix up... (And it doesn't need to go along with the backport.)

chudur-budur pushed a commit to chudur-budur/llvm-project that referenced this pull request Aug 2, 2026
…hen available (llvm#212845)

On ARM64X targets, CRT provides separate TLS directory chunks, expecting
the linker to sort it out. TLS directory uses _tls_start and _tls_end
symbols to reference .tls section. Those symbols use section sorting to
ensure that they are emitted at the start and end of .tls section, but that's
not enough when we have two separate chunks for views: only one of them
can really be the first one. Following MSVC, merge those chunks instead so
that both symbol tables point to the same chunk.

Additionally apply the same logic to _tls_used and _tls_index. This
allows entire TLS directory to be shared between EC and native views. To
achieve that, CRT additionally needs to mark each TLS callback with
-arm64xsameaddress. This matches how MSVC linker and libraries work, but
it requires EC and native views to use the same set of TLS callbacks. We
may emit separate TLS directories in the future to make it more robust.
frederik-h pushed a commit to frederik-h/llvm-project that referenced this pull request Aug 3, 2026
…hen available (llvm#212845)

On ARM64X targets, CRT provides separate TLS directory chunks, expecting
the linker to sort it out. TLS directory uses _tls_start and _tls_end
symbols to reference .tls section. Those symbols use section sorting to
ensure that they are emitted at the start and end of .tls section, but that's
not enough when we have two separate chunks for views: only one of them
can really be the first one. Following MSVC, merge those chunks instead so
that both symbol tables point to the same chunk.

Additionally apply the same logic to _tls_used and _tls_index. This
allows entire TLS directory to be shared between EC and native views. To
achieve that, CRT additionally needs to mark each TLS callback with
-arm64xsameaddress. This matches how MSVC linker and libraries work, but
it requires EC and native views to use the same set of TLS callbacks. We
may emit separate TLS directories in the future to make it more robust.
jgreenbaum pushed a commit to jgreenbaum/llvm-project that referenced this pull request Aug 3, 2026
…hen available (llvm#212845)

On ARM64X targets, CRT provides separate TLS directory chunks, expecting
the linker to sort it out. TLS directory uses _tls_start and _tls_end
symbols to reference .tls section. Those symbols use section sorting to
ensure that they are emitted at the start and end of .tls section, but that's
not enough when we have two separate chunks for views: only one of them
can really be the first one. Following MSVC, merge those chunks instead so
that both symbol tables point to the same chunk.

Additionally apply the same logic to _tls_used and _tls_index. This
allows entire TLS directory to be shared between EC and native views. To
achieve that, CRT additionally needs to mark each TLS callback with
-arm64xsameaddress. This matches how MSVC linker and libraries work, but
it requires EC and native views to use the same set of TLS callbacks. We
may emit separate TLS directories in the future to make it more robust.
dyung pushed a commit to llvmbot/llvm-project that referenced this pull request Aug 4, 2026
…hen available (llvm#212845)

On ARM64X targets, CRT provides separate TLS directory chunks, expecting
the linker to sort it out. TLS directory uses _tls_start and _tls_end
symbols to reference .tls section. Those symbols use section sorting to
ensure that they are emitted at the start and end of .tls section, but that's
not enough when we have two separate chunks for views: only one of them
can really be the first one. Following MSVC, merge those chunks instead so
that both symbol tables point to the same chunk.

Additionally apply the same logic to _tls_used and _tls_index. This
allows entire TLS directory to be shared between EC and native views. To
achieve that, CRT additionally needs to mark each TLS callback with
-arm64xsameaddress. This matches how MSVC linker and libraries work, but
it requires EC and native views to use the same set of TLS callbacks. We
may emit separate TLS directories in the future to make it more robust.

(cherry picked from commit e096d2f)
tfzee pushed a commit to tfzee/llvm-project that referenced this pull request Aug 6, 2026
…hen available (llvm#212845)

On ARM64X targets, CRT provides separate TLS directory chunks, expecting
the linker to sort it out. TLS directory uses _tls_start and _tls_end
symbols to reference .tls section. Those symbols use section sorting to
ensure that they are emitted at the start and end of .tls section, but that's
not enough when we have two separate chunks for views: only one of them
can really be the first one. Following MSVC, merge those chunks instead so
that both symbol tables point to the same chunk.

Additionally apply the same logic to _tls_used and _tls_index. This
allows entire TLS directory to be shared between EC and native views. To
achieve that, CRT additionally needs to mark each TLS callback with
-arm64xsameaddress. This matches how MSVC linker and libraries work, but
it requires EC and native views to use the same set of TLS callbacks. We
may emit separate TLS directories in the future to make it more robust.
@ththoma-msft

Copy link
Copy Markdown

Thanks. Here, I meant specifically about the same set of TLS callbacks being pulled by the linker. See the attached test.cpp as an example. It uses TLS callbacks from Classic, but not from EC. If I build it like:

cl -c test.cpp -Fo:test.arm64.obj
cl -c test.cpp -Fo:test.arm64ec.obj -arm64EC
link -out:test.exe test.arm64.obj test.arm64ec.obj -machine:arm64x

test.exe will crash when ran in EC mode. I think that, since EC doesn't use TLS callbacks, -arm64xsameaddress directive never has a chance to mark the function as such, but its Classic variant still present in TLS directory as seen by EC loader.

If I remove entirely TLS usage from EC (remove tl variable from the test), then no TLS directory is emitted, even through it's required by Classic, so then it crashes when ran in native mode.

Following up on this, a DevComm tickets has been filed for the second issue: https://developercommunity.visualstudio.com/t/ARM64X-linker-omits-tls-section-when-on/11140662?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Development

Successfully merging this pull request may close these issues.

5 participants