Skip to content

Commit c3f49f9

Browse files
committed
[LLD][COFF] Handle imported weak aliases consistently
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.
1 parent f8eceb4 commit c3f49f9

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

lld/COFF/SymbolTable.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,14 @@ void SymbolTable::resolveRemainingUndefines() {
502502
// This odd rule is for compatibility with MSVC linker.
503503
if (name.starts_with("__imp_")) {
504504
Symbol *imp = find(name.substr(strlen("__imp_")));
505+
if (imp) {
506+
// The unprefixed symbol might come later in symMap, so handle it now
507+
// so that the condition below can be appropriately applied.
508+
auto *undef = dyn_cast<Undefined>(imp);
509+
if (undef) {
510+
undef->resolveWeakAlias();
511+
}
512+
}
505513
if (imp && isa<Defined>(imp)) {
506514
auto *d = cast<Defined>(imp);
507515
replaceSymbol<DefinedLocalImport>(sym, ctx, name, d);

lld/test/COFF/import_weak_alias.test

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# REQUIRES: x86
2+
3+
# RUN: split-file %s %t.dir
4+
# RUN: llc --filetype=obj -o %t.foo.obj %t.dir/foo.ll
5+
# RUN: llvm-mc --filetype=obj -triple=x86_64-windows-msvc %t.dir/qux.s -o %t.qux.obj
6+
# RUN: lld-link %t.qux.obj %t.foo.obj -out:%t.dll -dll 2>&1 | FileCheck %s
7+
#
8+
# CHECK-NOT: lld-link: error: undefined symbol: __declspec(dllimport) foo
9+
10+
#--- foo.ll
11+
target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
12+
target triple = "x86_64-pc-windows-msvc"
13+
14+
@foo = weak alias void(), ptr @bar
15+
16+
define void @bar() {
17+
ret void
18+
}
19+
20+
#--- qux.s
21+
.text
22+
.global _DllMainCRTStartup
23+
_DllMainCRTStartup:
24+
call __imp_foo
25+
ret

0 commit comments

Comments
 (0)