Skip to content

Commit 52bfd2c

Browse files
igorkudrintstellar
authored andcommitted
[ELF] Do not report undefined weak references in shared libraries
This fixes an issue introduced in D101996. A weak reference in a shared library could be incorrectly reported if there is another library that has a strong reference to the same symbol. Differential Revision: https://reviews.llvm.org/D115041 (cherry picked from commit ce25eb1)
1 parent 19b8368 commit 52bfd2c

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

lld/ELF/InputFiles.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1567,7 +1567,7 @@ template <class ELFT> void SharedFile::parse() {
15671567
Symbol *s = symtab->addSymbol(
15681568
Undefined{this, name, sym.getBinding(), sym.st_other, sym.getType()});
15691569
s->exportDynamic = true;
1570-
if (s->isUndefined() && !s->isWeak() &&
1570+
if (s->isUndefined() && sym.getBinding() != STB_WEAK &&
15711571
config->unresolvedSymbolsInShlib != UnresolvedPolicy::Ignore)
15721572
requiredSymbols.push_back(s);
15731573
continue;
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# REQUIRES: x86
2+
3+
# RUN: rm -rf %t.dir
4+
# RUN: split-file %s %t.dir
5+
# RUN: cd %t.dir
6+
7+
## Verify that in the following case:
8+
##
9+
## <exec>
10+
## +- ref.so (weak reference to foo)
11+
## +- wrap.so (non-weak reference to foo)
12+
## +- def.so (defines foo)
13+
##
14+
## we don't report that foo is undefined in ref.so when linking <exec>.
15+
16+
# RUN: llvm-mc -filetype=obj -triple=x86_64 ref.s -o ref.o
17+
# RUN: llvm-mc -filetype=obj -triple=x86_64 wrap.s -o wrap.o
18+
# RUN: llvm-mc -filetype=obj -triple=x86_64 def.s -o def.o
19+
# RUN: ld.lld -shared ref.o -o ref.so
20+
# RUN: ld.lld -shared def.o -soname def.so -o def.so
21+
# RUN: ld.lld -shared wrap.o def.so -o wrap.so
22+
23+
# RUN: llvm-mc -filetype=obj -triple=x86_64 start.s -o start.o
24+
# RUN: ld.lld --no-allow-shlib-undefined start.o wrap.so ref.so -o /dev/null 2>&1 | count 0
25+
26+
#--- start.s
27+
.globl _start
28+
_start:
29+
callq wrap_get_foo@PLT
30+
31+
#--- ref.s
32+
.weak foo
33+
.globl ref_get_foo
34+
ref_get_foo:
35+
movq foo@GOTPCREL(%rip), %rax
36+
retq
37+
38+
#--- wrap.s
39+
.globl wrap_get_foo
40+
wrap_get_foo:
41+
movq foo@GOTPCREL(%rip), %rax
42+
retq
43+
44+
#--- def.s
45+
.data
46+
.globl foo
47+
foo:
48+
.long 0

0 commit comments

Comments
 (0)