Skip to content

[llvm-lib] Use ARM64EC machine type for import libraries when -machine:arm64x is used. #85972

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 21, 2024

Conversation

cjacek
Copy link
Contributor

@cjacek cjacek commented Mar 20, 2024

This is compatible with MSVC, -machine:arm64x is essentially an alias to -machine:arm64ec. To make a type library that exposes both native and EC symbols, an additional -defArm64Native argument is needed in both cases.

@llvmbot
Copy link
Member

llvmbot commented Mar 20, 2024

@llvm/pr-subscribers-llvm-binary-utilities

Author: Jacek Caban (cjacek)

Changes

This is compatible with MSVC, -machine:arm64x is essentially an alias to -machine:arm64ec. To make a type library that exposes both native and EC symbols, an additional -defArm64Native argument is needed in both cases.


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

2 Files Affected:

  • (modified) llvm/lib/Object/COFFImportFile.cpp (+5-2)
  • (modified) llvm/test/tools/llvm-lib/arm64ec-implib.test (+7)
diff --git a/llvm/lib/Object/COFFImportFile.cpp b/llvm/lib/Object/COFFImportFile.cpp
index 46c8e702581eac..8224a1492502f6 100644
--- a/llvm/lib/Object/COFFImportFile.cpp
+++ b/llvm/lib/Object/COFFImportFile.cpp
@@ -626,8 +626,11 @@ Error writeImportLibrary(StringRef ImportName, StringRef Path,
                          MachineTypes Machine, bool MinGW,
                          ArrayRef<COFFShortExport> NativeExports) {
 
-  MachineTypes NativeMachine =
-      isArm64EC(Machine) ? IMAGE_FILE_MACHINE_ARM64 : Machine;
+  MachineTypes NativeMachine = Machine;
+  if (isArm64EC(Machine)) {
+    NativeMachine = IMAGE_FILE_MACHINE_ARM64;
+    Machine = IMAGE_FILE_MACHINE_ARM64EC;
+  }
 
   std::vector<NewArchiveMember> Members;
   ObjectFactory OF(llvm::sys::path::filename(ImportName), NativeMachine);
diff --git a/llvm/test/tools/llvm-lib/arm64ec-implib.test b/llvm/test/tools/llvm-lib/arm64ec-implib.test
index 00eddd2a475268..d6bb80fde6b50d 100644
--- a/llvm/test/tools/llvm-lib/arm64ec-implib.test
+++ b/llvm/test/tools/llvm-lib/arm64ec-implib.test
@@ -96,6 +96,11 @@ READOBJ-NEXT: Name type: name
 READOBJ-NEXT: Export name: dataexp
 READOBJ-NEXT: Symbol: __imp_dataexp
 
+Using -machine:arm64x gives the same output.
+RUN: llvm-lib -machine:arm64x -def:test.def -out:test.lib
+RUN: llvm-nm --print-armap test.lib | FileCheck -check-prefix=ARMAP %s
+RUN: llvm-readobj test.lib | FileCheck -check-prefix=READOBJ %s
+
 Creating a new lib containing the existing lib:
 RUN: llvm-lib -machine:arm64ec test.lib -out:test2.lib
 RUN: llvm-nm --print-armap test2.lib | FileCheck -check-prefix=ARMAP %s
@@ -246,7 +251,9 @@ READOBJX-NEXT: Symbol: __imp_dataexp
 
 
 RUN: llvm-lib -machine:arm64ec -def:test.def -defArm64Native:test2.def -out:test2.lib
+RUN: llvm-lib -machine:arm64ec -def:test.def -defArm64Native:test2.def -out:test2x.lib
 RUN: llvm-nm --print-armap test2.lib | FileCheck -check-prefix=ARMAPX2 %s
+RUN: llvm-nm --print-armap test2x.lib | FileCheck -check-prefix=ARMAPX2 %s
 
 ARMAPX2:      Archive map
 ARMAPX2-NEXT: __IMPORT_DESCRIPTOR_test2 in test2.dll

@@ -96,6 +96,11 @@ READOBJ-NEXT: Name type: name
READOBJ-NEXT: Export name: dataexp
READOBJ-NEXT: Symbol: __imp_dataexp

Using -machine:arm64x gives the same output.
RUN: llvm-lib -machine:arm64x -def:test.def -out:test.lib
Copy link
Contributor

Choose a reason for hiding this comment

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

Use a different name for this lib file, for debuggability?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I changed it in the new version (it required changing check patterns to accept different names).

…e:arm64x is used.

This is compatible with MSVC, -machine:arm64x is essentially an alias to -machine:arm64ec.
To make a type library that exposes both native and EC symbols, an additional
-defArm64Native argument is needed in both cases.
@cjacek cjacek merged commit 8ecc377 into llvm:main Mar 21, 2024
@cjacek cjacek deleted the llvm-lib-arm64x branch March 21, 2024 10:40
chencha3 pushed a commit to chencha3/llvm-project that referenced this pull request Mar 23, 2024
…e:arm64x is used. (llvm#85972)

This is compatible with MSVC, `-machine:arm64x` is essentially an alias
to `-machine:arm64ec`. To make a type library that exposes both native
and EC symbols, an additional `-defArm64Native` argument is needed in
both cases.
cjacek added a commit to cjacek/llvm-project that referenced this pull request Mar 27, 2024
…RM64EC targets.

Currently, Clang doesn't explicitly pass the -machine argument to the linker, relying
on the linker to infer it from the input. However, MSVC's link.exe requires -machine
to be specified explicitly for ARM64EC/ARM64X targets. Although lld-link allows
inferring the target from ARM64EC object files, this detection isn't entirely reliable
as AMD64 object files are also valid input for ARM64EC target.

Handling of -machine:arm64ec is straightforward. For ARM64X, this PR extends the
ARM64EC target triple to accept the "arm64x" subarch string, which serves as an alias
to "arm64ec" in most cases. However, during linker invocation, "arm64x" distinguishes
between -machine:arm64ec and -machine:arm64x.

The only analogue to MSVC's behavior is cl.exe -arm64EC, which is handled by
-machine:arm64ec part of this PR. As far as I can tell, linking ARM64X images with
MSVC requires a direct link.exe invocation. In cases like lib.exe (llvm#85972) or some
aspects of link.exe, -machine:arm64x serves as an alias to -machine:arm64ec too.
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.

3 participants