Skip to content

[LLD][COFF] Allow overriding EC alias symbols with alternate names #113456

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
Oct 25, 2024

Conversation

cjacek
Copy link
Contributor

@cjacek cjacek commented Oct 23, 2024

No description provided.

@llvmbot
Copy link
Member

llvmbot commented Oct 23, 2024

@llvm/pr-subscribers-llvm-ir
@llvm/pr-subscribers-platform-windows

@llvm/pr-subscribers-lld-coff

Author: Jacek Caban (cjacek)

Changes

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

2 Files Affected:

  • (modified) lld/COFF/Driver.cpp (+3-2)
  • (added) lld/test/COFF/arm64ec-altnames.s (+53)
diff --git a/lld/COFF/Driver.cpp b/lld/COFF/Driver.cpp
index e7f768789271fa..f9c78482e8aa79 100644
--- a/lld/COFF/Driver.cpp
+++ b/lld/COFF/Driver.cpp
@@ -2518,9 +2518,10 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
         Symbol *sym = ctx.symtab.find(from);
         if (!sym)
           continue;
-        if (auto *u = dyn_cast<Undefined>(sym))
-          if (!u->weakAlias)
+        if (auto *u = dyn_cast<Undefined>(sym)) {
+          if (!u->weakAlias || u->isECAlias(ctx.config.machine))
             u->setWeakAlias(ctx.symtab.addUndefined(to));
+        }
       }
 
       // If any inputs are bitcode files, the LTO code generator may create
diff --git a/lld/test/COFF/arm64ec-altnames.s b/lld/test/COFF/arm64ec-altnames.s
new file mode 100644
index 00000000000000..a346ced7f5b7ad
--- /dev/null
+++ b/lld/test/COFF/arm64ec-altnames.s
@@ -0,0 +1,53 @@
+# REQUIRES: aarch64
+
+# RUN: llvm-mc -filetype=obj -triple=arm64ec-windows %s -o %t.obj
+# RUN: llvm-mc -filetype=obj -triple=arm64ec-windows %S/Inputs/loadconfig-arm64ec.s -o %t-loadconfig.obj
+
+# RUN: lld-link -machine:arm64ec -dll -noentry -out:%t1.dll %t.obj %t-loadconfig.obj "-alternatename:#func=altsym"
+
+# RUN: llvm-objdump -d %t1.dll | FileCheck --check-prefix=DISASM %s
+# DISASM:      0000000180001000 <.text>:
+# DISASM-NEXT: 180001000: 52800020     mov     w0, #0x1                // =1
+# DISASM-NEXT: 180001004: d65f03c0     ret
+# DISASM-NOT: .thnk
+
+# RUN: llvm-readobj --hex-dump=.test %t1.dll | FileCheck --check-prefix=TESTSEC %s
+# TESTSEC: 0x180004000 00100000 00100000
+
+# RUN: lld-link -machine:arm64ec -dll -noentry -out:%t2.dll %t.obj %t-loadconfig.obj -alternatename:func=altsym
+
+# RUN: llvm-objdump -d %t2.dll | FileCheck --check-prefix=DISASM2 %s
+# DISASM2:      Disassembly of section .text:
+# DISASM2-EMPTY:
+# DISASM2-NEXT: 0000000180001000 <.text>:
+# DISASM2-NEXT: 180001000: 52800020     mov     w0, #0x1                // =1
+# DISASM2-NEXT: 180001004: d65f03c0     ret
+# DISASM2-EMPTY:
+# DISASM2-NEXT: Disassembly of section .thnk:
+# DISASM2-EMPTY:
+# DISASM2-NEXT: 0000000180005000 <.thnk>:
+# DISASM2-NEXT: 180005000: 52800040     mov     w0, #0x2                // =2
+# DISASM2-NEXT: 180005004: d65f03c0     ret
+
+# RUN: llvm-readobj --hex-dump=.test %t2.dll | FileCheck --check-prefix=TESTSEC2 %s
+# TESTSEC2: 0x180004000 00100000 00500000
+
+        .weak_anti_dep func
+.set func, "#func"
+        .weak_anti_dep "#func"
+.set "#func", thunksym
+
+        .section .test, "r"
+        .rva func
+        .rva "#func"
+
+        .section .thnk,"xr",discard,thunksym
+thunksym:
+        mov w0, #2
+        ret
+
+        .section .text,"xr",discard,altsym
+        .globl altsym
+altsym:
+        mov w0, #1
+        ret

@cjacek
Copy link
Contributor Author

cjacek commented Oct 23, 2024

Similar to the handling of lazy symbols, EC aliases behave like undefined symbols in this context.

@cjacek cjacek marked this pull request as draft October 23, 2024 13:50
@cjacek
Copy link
Contributor Author

cjacek commented Oct 23, 2024

I just noticed that we may need special-casing func->#func aliases. It needs more testing, I changed it to a draft for now.

@cjacek cjacek marked this pull request as ready for review October 23, 2024 22:43
@cjacek
Copy link
Contributor Author

cjacek commented Oct 23, 2024

More experimentation with MSVC revealed that -alternatename is effective on anti-dependency symbols if either the aliased symbol is undefined or the symbol is mangled. In practice, this distinction helps differentiate between an alias to the guest exit thunk (which is always mangled) and a demangled alias to the implementation symbol. I kept it compatible; it’s now ready for review.

@cjacek
Copy link
Contributor Author

cjacek commented Oct 24, 2024

Fixed a typo in test.

Copy link
Member

@mstorsjo mstorsjo left a comment

Choose a reason for hiding this comment

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

LGTM

@cjacek cjacek merged commit 6bb6922 into llvm:main Oct 25, 2024
8 checks passed
@cjacek cjacek deleted the ec-altsym branch October 25, 2024 09:53
NoumanAmir657 pushed a commit to NoumanAmir657/llvm-project that referenced this pull request Nov 4, 2024
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