[ELF] Combine foo@v1 and foo with the same versionId if both are defined
authorFangrui Song <i@maskray.me>
Wed, 4 Aug 2021 16:06:04 +0000 (09:06 -0700)
committerFangrui Song <i@maskray.me>
Wed, 4 Aug 2021 16:06:05 +0000 (09:06 -0700)
commit66d4430492131a2205f159071c15e90c10e2fced
tree542c559fd22d7050b2da99e98fbc939865ada1ac
parentbdeb15c34eac9884f48a324004708e66ff76557b
[ELF] Combine foo@v1 and foo with the same versionId if both are defined

Due to an assembler design flaw (IMO), `.symver foo,foo@v1` produces two symbols `foo` and `foo@v1` if `foo` is defined.

* `v1 {};` produces both `foo` and `foo@v1`, but GNU ld only produces `foo@v1`
* `v1 { foo; };` produces both `foo@@v1` and `foo@v1`, but GNU ld only produces `foo@v1`
* `v2 { foo; };` produces both `foo@@v2` and `foo@v1`, matching GNU ld. (Tested by symver.s)

This patch implements the GNU ld behavior by reusing the symbol redirection mechanism
in D92259. The new test symver-non-default.s checks the first two cases.

Without the patch, the second case will produce `foo@v1` and `foo@@v1` which
looks weird and makes foo unnecessarily default versioned.

Note: `.symver foo,foo@v1,remove` exists but the unfortunate `foo` will not go
away anytime soon.

Reviewed By: peter.smith

Differential Revision: https://reviews.llvm.org/D107235
lld/ELF/Driver.cpp
lld/test/ELF/symver-non-default.s [new file with mode: 0644]
lld/test/ELF/version-script-symver.s
lld/test/ELF/version-symbol-undef.s