diff --git a/lld/COFF/SymbolTable.cpp b/lld/COFF/SymbolTable.cpp index efea16ccbbfea..1488ad95d0da6 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(imp); + if (undef) { + undef->resolveWeakAlias(); + } + } if (imp && isa(imp)) { auto *d = cast(imp); replaceSymbol(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 0000000000000..ae1817c67a20a --- /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