Skip to content

[LLD][COFF] Add support for multi-arch ARM64X object files - #207868

Merged
cjacek merged 1 commit into
llvm:mainfrom
cjacek:lld-arm64x
Jul 8, 2026
Merged

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

Conversation

@cjacek

@cjacek cjacek commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

No description provided.

@cjacek

cjacek commented Jul 6, 2026

Copy link
Copy Markdown
Contributor Author

Split from #202740.

@llvmorg-github-actions

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

Copy link
Copy Markdown

@llvm/pr-subscribers-platform-windows
@llvm/pr-subscribers-lld-coff

@llvm/pr-subscribers-lld

Author: Jacek Caban (cjacek)

Changes

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

2 Files Affected:

  • (modified) lld/COFF/Driver.cpp (+12)
  • (added) lld/test/COFF/arm64x-hybridobj.s (+74)
diff --git a/lld/COFF/Driver.cpp b/lld/COFF/Driver.cpp
index 38dad4ef71050..dc4903a5fe126 100644
--- a/lld/COFF/Driver.cpp
+++ b/lld/COFF/Driver.cpp
@@ -269,6 +269,18 @@ InputFile *LinkerDriver::addObjectFile(COFFLinkerContext &ctx,
   std::unique_ptr<COFFObjectFile> coffObj = ObjFile::createCOFFObject(ctx, mb);
   InputFile *obj = nullptr;
 
+  // On ARM64EC, check for a hybrid object section and use it for the EC object.
+  if (ctx.symtab.isEC()) {
+    if (std::optional<MemoryBufferRef> hybridSec =
+            coffObj->findHybridObjectSection()) {
+      InputFile *hybridObj =
+          addObjectFile(ctx, *hybridSec, archiveName, offsetInArchive, lazy);
+      // For the ARM64X target, continue processing the native file.
+      if (ctx.config.machine != ARM64X)
+        return hybridObj;
+    }
+  }
+
   if (ctx.config.fatLTOObjects) {
     Expected<MemoryBufferRef> fatLTOData =
         IRObjectFile::findBitcodeInObject(*coffObj);
diff --git a/lld/test/COFF/arm64x-hybridobj.s b/lld/test/COFF/arm64x-hybridobj.s
new file mode 100644
index 0000000000000..fe6fda3ec415d
--- /dev/null
+++ b/lld/test/COFF/arm64x-hybridobj.s
@@ -0,0 +1,74 @@
+// REQUIRES: aarch64
+// RUN: split-file %s %t.dir && cd %t.dir
+
+// RUN: llvm-mc -filetype=obj -triple=aarch64-windows sym.s -o sym-arm64.obj
+// RUN: llvm-mc -filetype=obj -triple=arm64ec-windows sym.s -o sym-arm64ec.obj
+// RUN: llvm-objcopy --add-section=.obj.arm64ec=sym-arm64ec.obj --set-section-flags=.obj.arm64ec=debug \
+// RUN:              sym-arm64.obj sym.obj
+
+// RUN: llvm-mc -filetype=obj -triple=aarch64-windows ref.s -o ref-arm64.obj
+// RUN: llvm-mc -filetype=obj -triple=arm64ec-windows ref.s -o ref-arm64ec.obj
+// RUN: llvm-objcopy --add-section=.obj.arm64ec=ref-arm64ec.obj --set-section-flags=.obj.arm64ec=debug \
+// RUN:              ref-arm64.obj ref.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: llvm-objcopy --add-section=.obj.arm64ec=loadconfig-arm64ec.obj --set-section-flags=.obj.arm64ec=debug \
+// RUN:              loadconfig-arm64.obj loadconfig.obj
+
+// Test linking hybrid object files, verify that both native and EC exports are present.
+// RUN: lld-link -machine:arm64x -dll -noentry -out:out.dll sym.obj loadconfig.obj
+// RUN: llvm-readobj --coff-exports out.dll | FileCheck %s
+
+// RUN: lld-link -machine:arm64ec -dll -noentry -out:out-ec.dll sym.obj loadconfig.obj
+// RUN: lld-link -machine:arm64 -dll -noentry -out:out-native.dll sym.obj loadconfig.obj
+
+// Test linking symbols from an archive.
+// RUN: llvm-ar cr sym.lib sym.obj
+// RUN: lld-link -machine:arm64x -dll -noentry -out:out2.dll ref.obj sym.lib loadconfig.obj
+// RUN: llvm-readobj --coff-exports out2.dll | FileCheck %s
+// RUN: lld-link -machine:arm64ec -dll -noentry -out:out-ec2.dll sym.obj loadconfig.obj
+// RUN: lld-link -machine:arm64 -dll -noentry -out:out-native2.dll sym.obj loadconfig.obj
+
+// Test linking symbols from a thin archive.
+// RUN: llvm-ar cr --thin sym-thin.lib sym.obj
+// RUN: lld-link -machine:arm64x -dll -noentry -out:out3.dll ref.obj sym-thin.lib loadconfig.obj
+// RUN: llvm-readobj --coff-exports out3.dll | FileCheck %s
+// RUN: lld-link -machine:arm64x -dll -noentry -out:out4.dll -wholearchive:sym-thin.lib loadconfig.obj
+// RUN: llvm-readobj --coff-exports out4.dll | FileCheck %s
+
+// Test -start-lib/-end-lib with hybrid object files.
+// RUN: lld-link -machine:arm64x -dll -noentry -out:out5.dll ref.obj -start-lib sym.obj loadconfig.obj -end-lib
+// RUN: llvm-readobj --coff-exports out5.dll | FileCheck %s
+
+// CHECK:      Format: COFF-ARM64X
+// CHECK-NEXT: Arch: aarch64
+// CHECK-NEXT: AddressSize: 64bit
+// CHECK-NEXT: Export {
+// CHECK-NEXT:   Ordinal: 1
+// CHECK-NEXT:   Name: sym
+// CHECK-NEXT:   RVA: 0x4004
+// CHECK-NEXT: }
+// CHECK-NEXT: HybridObject {
+// CHECK-NEXT:   Format: COFF-ARM64EC
+// CHECK-NEXT:   Arch: aarch64
+// CHECK-NEXT:   AddressSize: 64bit
+// CHECK-NEXT:   Export {
+// CHECK-NEXT:     Ordinal: 1
+// CHECK-NEXT:     Name: sym
+// CHECK-NEXT:     RVA: 0x4000
+// CHECK-NEXT:   }
+// CHECK-NEXT: }
+
+#--- sym.s
+        .section .sym,"dr"
+        .globl sym
+sym:
+        .long 0
+
+        .section .drectve
+        .ascii "-export:sym"
+
+#--- ref.s
+        .data
+        .rva sym

@mstorsjo

mstorsjo commented Jul 7, 2026

Copy link
Copy Markdown
Member

There's a typo in the PR/commit subject, multi-atch

@cjacek cjacek changed the title [LLD][COFF] Add support for multi-atch ARM64X object files [LLD][COFF] Add support for multi-arch ARM64X object files Jul 7, 2026
@cjacek

cjacek commented Jul 7, 2026

Copy link
Copy Markdown
Contributor Author

There's a typo in the PR/commit subject, multi-atch

Fixed, thanks.

@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.

This looks very straightforward to me, thanks! Happy to see that linking one or the other architecture from these objects works as one would hope.

Just one request to the testcase.

Comment thread lld/test/COFF/arm64x-hybridobj.s Resolved
@cjacek
cjacek force-pushed the lld-arm64x branch 2 times, most recently from ea47ed4 to 99bf6ed Compare July 7, 2026 22:43

@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.

Still LGTM, but one new question about the code.

Comment thread lld/COFF/Driver.cpp
addObjectFile(ctx, *hybridSec, archiveName, offsetInArchive, lazy);
// For the ARM64X target, continue processing the native file.
if (ctx.config.machine != ARM64X)
return hybridObj;

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.

Oh btw - one question here. For the ARM64X case, where we add two object files - we still just return one of them. What do the callers of this function do with the return value - is there any drawback to the fact that we don't return one of the object files that we actually add here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It's only used in addArchiveBuffer for logging and for that any object file should be fine.

Comment thread lld/test/COFF/arm64x-hybridobj.s Resolved
@cjacek
cjacek merged commit e1f7b53 into llvm:main Jul 8, 2026
9 checks passed
@cjacek
cjacek deleted the lld-arm64x branch July 8, 2026 09:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants