[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.
This commit is contained in:
Mike Hommey 2024-09-18 20:42:42 +09:00 committed by GitHub
parent 403897484f
commit 5e23b66699
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 28 additions and 0 deletions

View File

@ -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);

View File

@ -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