[ELF] Set the priority of STB_GNU_UNIQUE the same as STB_WEAK
In GCC -fgnu-unique output, STB_GNU_UNIQUE symbols are always defined relative
to a section in a COMDAT group. Currently `other` cannot be STB_GNU_UNIQUE for
valid input, so this patch is NFC.
If we switch to the model that ignores COMDAT resolution when performing symbol
resolution (D120626), this will fix bogus `relocation refers to a symbol in a
discarded section` errors when mixing -fno-gnu-unique objects with -fgnu-unique
objects.
Differential Revision: https://reviews.llvm.org/D120640
diff --git a/lld/ELF/Symbols.cpp b/lld/ELF/Symbols.cpp
index 8b74725..e8ead0d 100644
--- a/lld/ELF/Symbols.cpp
+++ b/lld/ELF/Symbols.cpp
@@ -528,7 +528,13 @@
return false;
}
- return isWeak() && !other.isWeak();
+ // Incoming STB_GLOBAL overrides STB_WEAK/STB_GNU_UNIQUE. -fgnu-unique changes
+ // some vague linkage data in COMDAT from STB_WEAK to STB_GNU_UNIQUE. Treat
+ // STB_GNU_UNIQUE like STB_WEAK so that we prefer the first among all
+ // STB_WEAK/STB_GNU_UNIQUE copies. If we prefer an incoming STB_GNU_UNIQUE to
+ // an existing STB_WEAK, there may be discarded section errors because the
+ // selected copy may be in a non-prevailing COMDAT.
+ return !isGlobal() && other.isGlobal();
}
void elf::reportDuplicate(const Symbol &sym, InputFile *newFile,