[PATCH] D120640: [ELF] Set the priority of STB_GNU_UNIQUE the same as STB_WEAK

Fangrui Song via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Feb 27 19:10:03 PST 2022


MaskRay created this revision.
MaskRay added reviewers: ikudrin, peter.smith.
Herald added subscribers: arichardson, emaste.
MaskRay requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

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 <https://reviews.llvm.org/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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D120640

Files:
  lld/ELF/Symbols.cpp
  lld/ELF/Symbols.h
  lld/test/ELF/comdat-binding.s


Index: lld/test/ELF/comdat-binding.s
===================================================================
--- /dev/null
+++ lld/test/ELF/comdat-binding.s
@@ -0,0 +1,30 @@
+# REQUIRES: x86
+# RUN: rm -rf %t && split-file %s %t
+# RUN: llvm-mc -filetype=obj -triple=x86_64 %t/g.s -o %t/g.o
+# RUN: llvm-mc -filetype=obj -triple=x86_64 %t/w.s -o %t/w.o
+# RUN: llvm-mc -filetype=obj -triple=x86_64 %t/u.s -o %t/u.o
+# RUN: ld.lld -e 0 %t/w.o %t/u.o -o %t/w
+# RUN: llvm-readelf -s %t/w | FileCheck %s --check-prefix=WEAK
+# RUN: ld.lld -e 0 %t/u.o %t/w.o -o %t/u
+# RUN: llvm-readelf -s %t/u | FileCheck %s --check-prefix=UNIQUE
+
+# RUN: ld.lld -e 0 %t/w.o %t/g.o -o %t/w
+# RUN: llvm-readelf -s %t/w | FileCheck %s --check-prefix=WEAK
+
+# WEAK:   NOTYPE WEAK   DEFAULT [[#]] _ZZ1fvE1x
+# UNIQUE: OBJECT UNIQUE DEFAULT [[#]] _ZZ1fvE1x
+
+#--- g.s
+.section .bss._ZZ1fvE1x,"awG", at nobits,_ZZ1fvE1x,comdat
+.globl _ZZ1fvE1x
+_ZZ1fvE1x:
+
+#--- w.s
+.section .bss._ZZ1fvE1x,"awG", at nobits,_ZZ1fvE1x,comdat
+.weak _ZZ1fvE1x
+_ZZ1fvE1x:
+
+#--- u.s
+.section .bss._ZZ1fvE1x,"awG", at nobits,_ZZ1fvE1x,comdat
+.type _ZZ1fvE1x, @gnu_unique_object
+_ZZ1fvE1x:
Index: lld/ELF/Symbols.h
===================================================================
--- lld/ELF/Symbols.h
+++ lld/ELF/Symbols.h
@@ -141,6 +141,7 @@
 
   bool includeInDynsym() const;
   uint8_t computeBinding() const;
+  bool isGlobal() const { return binding == llvm::ELF::STB_GLOBAL; }
   bool isWeak() const { return binding == llvm::ELF::STB_WEAK; }
 
   bool isUndefined() const { return symbolKind == UndefinedKind; }
Index: lld/ELF/Symbols.cpp
===================================================================
--- lld/ELF/Symbols.cpp
+++ lld/ELF/Symbols.cpp
@@ -528,7 +528,12 @@
       return false;
   }
 
-  return isWeak() && !other.isWeak();
+  // Incoming STB_GLOBAL overrides STB_WEAK/STB_GNU_UNIQUE. -fgnu-unique changes
+  // some vague linkage data from STB_WEAK to STB_GNU_UNIQUE. Treat
+  // STB_GNU_UNIQUE like STB_WEAK so that we prefer existing STB_WEAK to
+  // incoming STB_GNU_UNIQUE. Otherwise there may be discarded section errors if
+  // the the incoming copy in a non-prevailing COMDAT is selected.
+  return !isGlobal() && other.isGlobal();
 }
 
 void elf::reportDuplicate(const Symbol &sym, InputFile *newFile,


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D120640.411725.patch
Type: text/x-patch
Size: 2298 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220228/d138ccbd/attachment.bin>


More information about the llvm-commits mailing list