Skip to content

Commit 21ac457

Browse files
committed
[ELF] Priorize the last catch-all pattern in version scripts
When there are multiple catch-all patterns (i.e. a single `*`), GNU ld and gold select the last pattern. Match their behavior. This change was inspired by a correction made by Michael Kerrisk to a blog post I wrote at https://maskray.me/blog/2020-11-26-all-about-symbol-versioning , following the current lld rules. Note: GNU ld prefers global: patterns to local: patterns, which might seem awkward (https://www.airs.com/blog/archives/300). gold doesn't follow this behavior, and we do not either.
1 parent 89bacc0 commit 21ac457

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

lld/ELF/SymbolTable.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ void SymbolTable::scanVersionScript() {
313313

314314
// Then, assign versions to "*". In GNU linkers they have lower priority than
315315
// other wildcards.
316-
for (VersionDefinition &v : config->versionDefinitions) {
316+
for (VersionDefinition &v : llvm::reverse(config->versionDefinitions)) {
317317
for (SymbolVersion &pat : v.nonLocalPatterns)
318318
if (pat.hasWildcard && pat.name == "*")
319319
assignWildcard(pat, v.id, v.name);

lld/test/ELF/version-script-reassign-glob.s

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,18 @@
99
# RUN: ld.lld --version-script %t.ver %t.o -shared -o %t.so --fatal-warnings
1010
# RUN: llvm-readelf --dyn-syms %t.so | FileCheck --check-prefix=BAR %s
1111

12+
# RUN: echo 'bar1 { *; }; bar2 { *; };' > %t2.ver
13+
# RUN: ld.lld --version-script %t2.ver %t.o -shared -o %t2.so --fatal-warnings
14+
# RUN: llvm-readelf --dyn-syms %t2.so | FileCheck --check-prefix=BAR2 %s
15+
1216
## If both a non-* glob and a * match, non-* wins.
1317
## This is GNU linkers' behavior. We don't feel strongly this should be supported.
1418
# FOO: GLOBAL DEFAULT 7 foo@@foo
1519

1620
# BAR: GLOBAL DEFAULT 7 foo@@bar
1721

22+
## When there are multiple * patterns, the last wins.
23+
# BAR2: GLOBAL DEFAULT 7 foo@@bar2
24+
1825
.globl foo
1926
foo:

0 commit comments

Comments
 (0)