[lld] 5e23b66 - [LLD][COFF] Handle imported weak aliases consistently (#109105)

via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 18 04:42:47 PDT 2024


Author: Mike Hommey
Date: 2024-09-18T14:42:42+03:00
New Revision: 5e23b66699d1066ce10b14a74d6137303517b2f3

URL: https://github.com/llvm/llvm-project/commit/5e23b66699d1066ce10b14a74d6137303517b2f3
DIFF: https://github.com/llvm/llvm-project/commit/5e23b66699d1066ce10b14a74d6137303517b2f3.diff

LOG: [LLD][COFF] Handle imported weak aliases consistently (#109105)

symTab being a DenseMap, the order in which a symbol and its
corresponding import symbol are processed is not guaranteed, and when
the latter comes first, it is left undefined.

Added: 
    lld/test/COFF/import_weak_alias.test

Modified: 
    lld/COFF/SymbolTable.cpp

Removed: 
    


################################################################################
diff  --git a/lld/COFF/SymbolTable.cpp b/lld/COFF/SymbolTable.cpp
index efea16ccbbfea0..1488ad95d0da62 100644
--- a/lld/COFF/SymbolTable.cpp
+++ b/lld/COFF/SymbolTable.cpp
@@ -502,6 +502,14 @@ void SymbolTable::resolveRemainingUndefines() {
     // This odd rule is for compatibility with MSVC linker.
     if (name.starts_with("__imp_")) {
       Symbol *imp = find(name.substr(strlen("__imp_")));
+      if (imp) {
+        // The unprefixed symbol might come later in symMap, so handle it now
+        // so that the condition below can be appropriately applied.
+        auto *undef = dyn_cast<Undefined>(imp);
+        if (undef) {
+          undef->resolveWeakAlias();
+        }
+      }
       if (imp && isa<Defined>(imp)) {
         auto *d = cast<Defined>(imp);
         replaceSymbol<DefinedLocalImport>(sym, ctx, name, d);

diff  --git a/lld/test/COFF/import_weak_alias.test b/lld/test/COFF/import_weak_alias.test
new file mode 100644
index 00000000000000..ae1817c67a20ac
--- /dev/null
+++ b/lld/test/COFF/import_weak_alias.test
@@ -0,0 +1,20 @@
+# REQUIRES: x86
+
+# RUN: split-file %s %t.dir
+# RUN: llvm-mc --filetype=obj -triple=x86_64-windows-msvc %t.dir/foo.s -o %t.foo.obj
+# RUN: llvm-mc --filetype=obj -triple=x86_64-windows-msvc %t.dir/qux.s -o %t.qux.obj
+# RUN: lld-link %t.qux.obj %t.foo.obj -out:%t.dll -dll
+#
+#--- foo.s
+.text
+bar:
+  ret
+
+.weak foo
+.set foo, bar
+#--- qux.s
+.text
+.global _DllMainCRTStartup
+_DllMainCRTStartup:
+  call *__imp_foo(%rip)
+  ret


        


More information about the llvm-commits mailing list