Skip to content

Commit 5e23b66

Browse files
authored
[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.
1 parent 4038974 commit 5e23b66

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-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: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# REQUIRES: x86
2+
3+
# RUN: split-file %s %t.dir
4+
# RUN: llvm-mc --filetype=obj -triple=x86_64-windows-msvc %t.dir/foo.s -o %t.foo.obj
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
7+
#
8+
#--- foo.s
9+
.text
10+
bar:
11+
ret
12+
13+
.weak foo
14+
.set foo, bar
15+
#--- qux.s
16+
.text
17+
.global _DllMainCRTStartup
18+
_DllMainCRTStartup:
19+
call *__imp_foo(%rip)
20+
ret

0 commit comments

Comments
 (0)