Skip to content

Commit 6912082

Browse files
MaskRaytstellar
authored andcommitted
[ELF] Don't set versionId on undefined weak lazy symbols
An unfetched lazy symbol (undefined weak) should be considered to have its original versionId which is VER_NDX_GLOBAL, instead of the lazy symbol's versionId. (The original versionId cannot be non-VER_NDX_GLOBAL because a undefined versioned symbol is an error.) The regression was introduced in D77280 when making version scripts work with lazy symbols fetched by LTO calls. Fix PR49915 Differential Revision: https://reviews.llvm.org/D100624 (cherry picked from commit 1c00530)
1 parent 33d312b commit 6912082

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

lld/ELF/SyntheticSections.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3110,7 +3110,9 @@ size_t VersionTableSection::getSize() const {
31103110
void VersionTableSection::writeTo(uint8_t *buf) {
31113111
buf += 2;
31123112
for (const SymbolTableEntry &s : getPartition().dynSymTab->getSymbols()) {
3113-
write16(buf, s.sym->versionId);
3113+
// Use the original versionId for an unfetched lazy symbol (undefined weak),
3114+
// which must be VER_NDX_GLOBAL (an undefined versioned symbol is an error).
3115+
write16(buf, s.sym->isLazy() ? VER_NDX_GLOBAL : s.sym->versionId);
31143116
buf += 2;
31153117
}
31163118
}

lld/test/ELF/version-script-weak.s

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,19 @@
2424
# CHECK-NEXT: Section: Undefined
2525
# CHECK-NEXT: }
2626

27+
## The version of an unfetched lazy symbol is VER_NDX_GLOBAL. It is not affected by version scripts.
28+
# RUN: echo "v1 { *; };" > %t2.script
29+
# RUN: ld.lld -shared --version-script %t2.script %t.o --start-lib %t1.o --end-lib -o %t2.so
30+
# RUN: llvm-readelf --dyn-syms %t2.so | FileCheck %s --check-prefix=CHECK2
31+
32+
# CHECK2: NOTYPE WEAK DEFAULT UND foo{{$}}
33+
34+
# RUN: ld.lld -shared --soname=tshared --version-script %t2.script %t1.o -o %tshared.so
35+
# RUN: ld.lld -shared --version-script %t2.script %t.o --start-lib %t1.o --end-lib %tshared.so -o %t3.so
36+
# RUN: llvm-readelf --dyn-syms %t3.so | FileCheck %s --check-prefix=CHECK3
37+
38+
# CHECK3: NOTYPE WEAK DEFAULT UND foo@v1
39+
2740
.text
2841
callq foo@PLT
2942
.weak foo

0 commit comments

Comments
 (0)